Skip to content

Commit 3f77316

Browse files
Kiyoshi Uedakergon
authored andcommitted
dm: separate device deletion from dm_put
This patch separates the device deletion code from dm_put() to make sure the deletion happens in the process context. By this patch, device deletion always occurs in an ioctl (process) context and dm_put() can be called in interrupt context. As a result, the request-based dm's bad dm_put() usage pointed out by Mikulas below disappears. http://marc.info/?l=dm-devel&m=126699981019735&w=2 Without this patch, I confirmed there is a case to crash the system: dm_put() => dm_table_destroy() => vfree() => BUG_ON(in_interrupt()) Some more backgrounds and details: In request-based dm, a device opener can remove a mapped_device while the last request is still completing, because bios in the last request complete first and then the device opener can close and remove the mapped_device before the last request completes: CPU0 CPU1 ================================================================= <<INTERRUPT>> blk_end_request_all(clone_rq) blk_update_request(clone_rq) bio_endio(clone_bio) == end_clone_bio blk_update_request(orig_rq) bio_endio(orig_bio) <<I/O completed>> dm_blk_close() dev_remove() dm_put(md) <<Free md>> blk_finish_request(clone_rq) .... dm_end_request(clone_rq) free_rq_clone(clone_rq) blk_end_request_all(orig_rq) rq_completed(md) So request-based dm used dm_get()/dm_put() to hold md for each I/O until its request completion handling is fully done. However, the final dm_put() can call the device deletion code which must not be run in interrupt context and may cause kernel panic. To solve the problem, this patch moves the device deletion code, dm_destroy(), to predetermined places that is actually deleting the mapped_device in ioctl (process) context, and changes dm_put() just to decrement the reference count of the mapped_device. By this change, dm_put() can be used in any context and the symmetric model below is introduced: dm_create(): create a mapped_device dm_destroy(): destroy a mapped_device dm_get(): increment the reference count of a mapped_device dm_put(): decrement the reference count of a mapped_device dm_destroy() waits for all references of the mapped_device to disappear, then deletes the mapped_device. dm_destroy() uses active waiting with msleep(1), since deleting the mapped_device isn't performance-critical task. And since at this point, nobody opens the mapped_device and no new reference will be taken, the pending counts are just for racing completing activity and will eventually decrease to zero. For the unlikely case of the forced module unload, dm_destroy_immediate(), which doesn't wait and forcibly deletes the mapped_device, is also introduced and used in dm_hash_remove_all(). Otherwise, "rmmod -f" may be stuck and never return. And now, because the mapped_device is deleted at this point, subsequent accesses to the mapped_device may cause NULL pointer references. Cc: stable@kernel.org Signed-off-by: Kiyoshi Ueda <k-ueda@ct.jp.nec.com> Signed-off-by: Jun'ichi Nomura <j-nomura@ce.jp.nec.com> Signed-off-by: Alasdair G Kergon <agk@redhat.com>
1 parent 98f3328 commit 3f77316

3 files changed

Lines changed: 62 additions & 20 deletions

File tree

‎drivers/md/dm-ioctl.c‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ static void dm_hash_remove_all(int keep_open_devices)
274274
up_write(&_hash_lock);
275275

276276
dm_put(md);
277+
if (likely(keep_open_devices))
278+
dm_destroy(md);
279+
else
280+
dm_destroy_immediate(md);
277281

278282
/*
279283
* Some mapped devices may be using other mapped
@@ -644,17 +648,19 @@ static int dev_create(struct dm_ioctl *param, size_t param_size)
644648
return r;
645649

646650
r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md);
647-
if (r)
648-
goto out;
651+
if (r) {
652+
dm_put(md);
653+
dm_destroy(md);
654+
return r;
655+
}
649656

650657
param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
651658

652659
__dev_status(md, param);
653660

654-
out:
655661
dm_put(md);
656662

657-
return r;
663+
return 0;
658664
}
659665

660666
/*
@@ -748,6 +754,7 @@ static int dev_remove(struct dm_ioctl *param, size_t param_size)
748754
param->flags |= DM_UEVENT_GENERATED_FLAG;
749755

750756
dm_put(md);
757+
dm_destroy(md);
751758
return 0;
752759
}
753760

‎drivers/md/dm.c‎

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <linux/slab.h>
2121
#include <linux/idr.h>
2222
#include <linux/hdreg.h>
23+
#include <linux/delay.h>
2324

2425
#include <trace/events/block.h>
2526

@@ -2171,6 +2172,7 @@ void dm_set_mdptr(struct mapped_device *md, void *ptr)
21712172
void dm_get(struct mapped_device *md)
21722173
{
21732174
atomic_inc(&md->holders);
2175+
BUG_ON(test_bit(DMF_FREEING, &md->flags));
21742176
}
21752177

21762178
const char *dm_device_name(struct mapped_device *md)
@@ -2179,27 +2181,55 @@ const char *dm_device_name(struct mapped_device *md)
21792181
}
21802182
EXPORT_SYMBOL_GPL(dm_device_name);
21812183

2182-
void dm_put(struct mapped_device *md)
2184+
static void __dm_destroy(struct mapped_device *md, bool wait)
21832185
{
21842186
struct dm_table *map;
21852187

2186-
BUG_ON(test_bit(DMF_FREEING, &md->flags));
2188+
might_sleep();
21872189

2188-
if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
2189-
map = dm_get_live_table(md);
2190-
idr_replace(&_minor_idr, MINOR_ALLOCED,
2191-
MINOR(disk_devt(dm_disk(md))));
2192-
set_bit(DMF_FREEING, &md->flags);
2193-
spin_unlock(&_minor_lock);
2194-
if (!dm_suspended_md(md)) {
2195-
dm_table_presuspend_targets(map);
2196-
dm_table_postsuspend_targets(map);
2197-
}
2198-
dm_sysfs_exit(md);
2199-
dm_table_put(map);
2200-
dm_table_destroy(__unbind(md));
2201-
free_dev(md);
2190+
spin_lock(&_minor_lock);
2191+
map = dm_get_live_table(md);
2192+
idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2193+
set_bit(DMF_FREEING, &md->flags);
2194+
spin_unlock(&_minor_lock);
2195+
2196+
if (!dm_suspended_md(md)) {
2197+
dm_table_presuspend_targets(map);
2198+
dm_table_postsuspend_targets(map);
22022199
}
2200+
2201+
/*
2202+
* Rare, but there may be I/O requests still going to complete,
2203+
* for example. Wait for all references to disappear.
2204+
* No one should increment the reference count of the mapped_device,
2205+
* after the mapped_device state becomes DMF_FREEING.
2206+
*/
2207+
if (wait)
2208+
while (atomic_read(&md->holders))
2209+
msleep(1);
2210+
else if (atomic_read(&md->holders))
2211+
DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2212+
dm_device_name(md), atomic_read(&md->holders));
2213+
2214+
dm_sysfs_exit(md);
2215+
dm_table_put(map);
2216+
dm_table_destroy(__unbind(md));
2217+
free_dev(md);
2218+
}
2219+
2220+
void dm_destroy(struct mapped_device *md)
2221+
{
2222+
__dm_destroy(md, true);
2223+
}
2224+
2225+
void dm_destroy_immediate(struct mapped_device *md)
2226+
{
2227+
__dm_destroy(md, false);
2228+
}
2229+
2230+
void dm_put(struct mapped_device *md)
2231+
{
2232+
atomic_dec(&md->holders);
22032233
}
22042234
EXPORT_SYMBOL_GPL(dm_put);
22052235

‎drivers/md/dm.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ void dm_linear_exit(void);
122122
int dm_stripe_init(void);
123123
void dm_stripe_exit(void);
124124

125+
/*
126+
* mapped_device operations
127+
*/
128+
void dm_destroy(struct mapped_device *md);
129+
void dm_destroy_immediate(struct mapped_device *md);
125130
int dm_open_count(struct mapped_device *md);
126131
int dm_lock_for_deletion(struct mapped_device *md);
127132

0 commit comments

Comments
 (0)