Semaphore bug in dm-thin
Mikulas Patocka
maintainerHi Joe
Zdenek recently got a lockdep warning in dm-thin. See this:
http://pastebin.test.redhat.com/65346
I looked at the warning and realized that it is not a false alarm, but a real bug.
Lockdep warns about dependency &md->io_lock -> &md->map_lock, but io_lock is a semaphore and map_lock is a spinlock, so such lock ordering should be always allowed (you can't take them in reverse order, because you can't take a semaphore while holding a spinlock).
Looking deeper in the warning, it complains that there is an exeisting dependency chain &md->map_lock --> &pmd->root_lock --> &md->io_lock
Here, map_lock is a spinlock and root_lock is a semaphore, this order of taking locks is obviously wrong.
Cutting relevant part of the backtrace, we see:
41. [ 3836.656384] -> #1 (&pmd->root_lock){+++++.}:
42. [ 3836.656384] [<ffffffff81090740>] lock_acquire+0x90/0x1f0
43. [ 3836.656384] [<ffffffff814ac1ce>] down_read+0x3e/0x86
44. [ 3836.656384] [<ffffffffa02aaafe>]
dm_pool_get_data_dev_size+0x2e/0x60 [dm_thin_pool]
45. [ 3836.656384] [<ffffffffa02a615b>]
thin_iterate_devices+0x3b/0x80 [dm_thin_pool]
46. [ 3836.656384] [<ffffffffa006b2bf>]
dm_table_supports_flush+0x5f/0x90 [dm_mod]
47. [ 3836.656384] [<ffffffffa006d2ec>]
dm_table_set_restrictions+0x9c/0x180 [dm_mod]
48. [ 3836.656384] [<ffffffffa006a88a>] dm_swap_table+0x1da/0x2e0
[dm_mod]
49. [ 3836.656384] [<ffffffffa00701db>] dev_suspend+0x11b/0x260
[dm_mod]
50. [ 3836.656384] [<ffffffffa007089a>] ctl_ioctl+0x15a/0x2c0
[dm_mod]dm_swap_table calls __bind (which is not shown because it is inlined), __bind takes a spinlock &md->map_lock and calls dm_table_set_restrictions, from there, the code goes to thin_iterate_devices and it tries to take a semaphore &pmd->root_lock.
So there is a clear bug, dm-thin is taking a semaphore while spinlock &md->map_lock is held.
Mikulas
Thanks Mikulas, Good analysis. Looking at this now.
Quoted 47 lines · Mikulas Patocka
Hi Joe
Zdenek recently got a lockdep warning in dm-thin. See this:
http://pastebin.test.redhat.com/65346I looked at the warning and realized that it is not a false alarm, but a real bug.
Lockdep warns about dependency
&md->io_lock->&md->map_lock, butio_lockis a semaphore andmap_lockis a spinlock, so such lock ordering should be always allowed (you can't take them in reverse order, because you can't take a semaphore while holding a spinlock).Looking deeper in the warning, it complains that there is an exeisting dependency chain
&md->map_lock-->&pmd->root_lock-->&md->io_lockHere,
map_lockis a spinlock androot_lockis a semaphore, this order of taking locks is obviously wrong.Cutting relevant part of the backtrace, we see:
41. [ 3836.656384] -> #1 (&pmd->root_lock){+++++.}:42. [ 3836.656384] [<ffffffff81090740>] lock_acquire+0x90/0x1f0 43. [ 3836.656384] [<ffffffff814ac1ce>] down_read+0x3e/0x86 44. [ 3836.656384] [<ffffffffa02aaafe>] dm_pool_get_data_dev_size+0x2e/0x60 [dm_thin_pool] 45. [ 3836.656384] [<ffffffffa02a615b>] thin_iterate_devices+0x3b/0x80 [dm_thin_pool] 46. [ 3836.656384] [<ffffffffa006b2bf>] dm_table_supports_flush+0x5f/0x90 [dm_mod] 47. [ 3836.656384] [<ffffffffa006d2ec>] dm_table_set_restrictions+0x9c/0x180 [dm_mod] 48. [ 3836.656384] [<ffffffffa006a88a>] dm_swap_table+0x1da/0x2e0 [dm_mod] 49. [ 3836.656384] [<ffffffffa00701db>] dev_suspend+0x11b/0x260 [dm_mod] 50. [ 3836.656384] [<ffffffffa007089a>] ctl_ioctl+0x15a/0x2c0 [dm_mod]
dm_swap_tablecalls__bind(which is not shown because it is inlined),__bindtakes a spinlock&md->map_lockand callsdm_table_set_restrictions, from there, the code goes tothin_iterate_devicesand it tries to take a semaphore&pmd->root_lock.So there is a clear bug, dm-thin is taking a semaphore while spinlock
&md->map_lockis held.Mikulas
PATCH 1/2 Document the fact that iterate_device method shouldn't block.
--- include/linux/device-mapper.h | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-)
include/linux/device-mapper.h +4 -0
@@ -148,6 +148,10 @@ struct target_type {dm_ioctl_fn ioctl;dm_merge_fn merge;dm_busy_fn busy; + +/* +* iterate_devices must not block. +*/ dm_iterate_devices_fn iterate_devices;dm_io_hints_fn io_hints;
Signature · 1 line
1.7.5.4
PATCH 2/2 in thin_iterate_devices(), get the pool dev size a non-blocking way.
--- drivers/md/dm-thin.c | 10 +++++++--- 1 files changed, 7 insertions(+), 3 deletions(-)
drivers/md/dm-thin.c +7 -3
@@ -2359,10 +2359,14 @@ static int thin_iterate_devices(struct dm_target *ti,dm_block_t blocks;struct thin_c *tc = ti->private; - r =dm_pool_get_data_dev_size (tc->pool->pmd, &blocks); -if (r) -return r; +/* +* We can't call dm_pool_get_data_dev_size() since that blocks. So +* we follow a more convoluted path through to the pool's target. +*/ +if (!tc->pool->ti) +return 0 ;/* nothing is bound */ + blocks = tc->pool->ti->len >> tc->pool->block_shift;if (blocks)return fn (ti, tc->pool_dev,0 , tc->pool->sectors_per_block * blocks, data);
Signature · 1 line
1.7.5.4
Mikulas Patocka
maintainerRe: [PATCH 2/2] [thin] in thin_iterate_devices(), get the pool dev size a non-blocking way.
Ack.
Mikulas
Quoted 29 lines · Joe Thornber
--- drivers/md/dm-thin.c | 10 +++++++--- 1 files changed, 7 insertions(+), 3 deletions(-)drivers/md/dm-thin.c +7
-3@@ -2359,10 +2359,14 @@ static int thin_iterate_devices(struct dm_target *ti,dm_block_t blocks;struct thin_c *tc = ti->private; - r =dm_pool_get_data_dev_size (tc->pool->pmd, &blocks); -if (r) -return r; +/* +* We can't call dm_pool_get_data_dev_size() since that blocks. So +* we follow a more convoluted path through to the pool's target. +*/ +if (!tc->pool->ti) +return 0 ;/* nothing is bound */ + blocks = tc->pool->ti->len >> tc->pool->block_shift;if (blocks)return fn (ti, tc->pool_dev,0 , tc->pool->sectors_per_block * blocks, data);Signature · 1 line
1.7.5.4
Mikulas Patocka
maintainerRe: [PATCH 1/2] [device-mapper] Document the fact that iterate_device method shouldn't block.
In reply to Joe ThornberAck.
Mikulas
Quoted 22 lines · Joe Thornber
--- include/linux/device-mapper.h | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-)include/linux/device-mapper.h +4
-0@@ -148,6 +148,10 @@ struct target_type {dm_ioctl_fn ioctl;dm_merge_fn merge;dm_busy_fn busy; + +/* +* iterate_devices must not block. +*/ dm_iterate_devices_fn iterate_devices;dm_io_hints_fn io_hints;Signature · 1 line
1.7.5.4
Alasdair G Kergon
maintainerRe: [PATCH 1/2] [device-mapper] Document the fact that iterate_device method shouldn't block.
Quoted 7 lines · Mikulas Patocka
Ack.
include/linux/device-mapper.h struct target_type { · line 155
Quoted 4 lines · Joe Thornber
include/linux/device-mapper.h +3
-0+/* +* iterate_devices must not block. +*/ dm_iterate_devices_fn iterate_devices;
I'm leaving that one for now as I think singling out one function as 'must not block' may mislead people into thinking the other adjacent ones there are allowed to block.
Alasdair