dm thin: stop waiting for grace periods when activating and removing thins
Subsystems (2)
v3 cover dm thin: stop waiting for grace periods when activating and removing thins
Activating a thin device waits for an RCU grace period in thin_ctr(), and removing one waits for another in thin_dtr(). Tools that create and remove thin snapshots for every container, such as the containerd devmapper snapshotter, pay for both each time.
Patch 1 drops the grace period from thin_ctr(), which isn't needed. Patch 2 fixes the pool worker continuing its walk of active_thins from a thin that thin_dtr() already took off the list, whose successor may have been freed: thin_dtr() now marks the thin as dying and takes it off the list only after its last reference is dropped. Patch 3 then lets thin_dtr() free the thin with kfree_rcu() instead of waiting. Activating a thin device gets 50% faster and removing one 20%.
---
V2 -> V3:
- Patch 2: mark the thin as dying in thin_dtr(), take it off active_thins after wait_for_completion() instead of in the last thin_put(), and skip dying thins in requeue_bios() and error_retry_list_with_code() (Benjamin Marzinski)
- Patch 2: correct the Fixes tag to 17181fb7a0c3 (Benjamin Marzinski)
- Patch 3: keep kfree_rcu() out of patch 2, so that the fix can go to stable on its own
- Rebase onto current mainline and remeasure
V1 -> V2:
- Add patch 2: keep a thin on active_thins until its last reference is dropped, so that the worker never continues from a thin whose successor was freed (Benjamin Marzinski)
- Patch 3: rebase onto patch 2, which already skips dying thins, so it only replaces the grace period with kfree_rcu()
- Patch 1: add Benjamin's Reviewed-by
- Rebase onto current mainline and remeasure
v2: https://lore.kernel.org/r/[email protected]
v1: https://lore.kernel.org/r/[email protected]
---
Qiliang Yuan (3):
dm thin: drop the grace period from thin_ctr()
dm thin: keep a thin on active_thins until its last reference is dropped dm thin: free thins with kfree_rcu() instead of waiting in thin_dtr()
drivers/md/dm-thin.c | 61 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 25 deletions(-)
---
base-commit: 4405c18c95d2d0f99f6c581ef0ee8591a1aa6ccd
change-id: 20261114-bug-dm-thin-rcu-gp-d29d4b74a0b8
Best regards,
Signature · 1 line
Qiliang Yuan <[email protected]>
PATCH v3 1/3 dm thin: drop the grace period from thin_ctr()
thin_ctr() adds the new thin to pool->active_thins with list_add_tail_rcu() and then calls synchronize_rcu(), so that a wake_worker() call can't miss the bios of a thin that isn't visible yet.
The grace period doesn't make the entry any more visible. A worker that walks the list after list_add_tail_rcu() finds the thin, and the thin gets no bios before its table is resumed. A walk already in progress may miss it, but the wake_worker() for the first bio queues the worker again. Still, activating a thin device waits for a full grace period, which takes about half of the activation.
Remove the synchronize_rcu() call.
Activating 100 thin devices of an idle pool with dmsetup create takes per device (mean of five rounds, min-max of the rounds in parentheses):
before (ms) after (ms) change
15.5 (15.2-15.7) 7.7 (7.5-7.9) -50%
--- drivers/md/dm-thin.c | 7 ------- 1 file changed, 7 deletions(-)
drivers/md/dm-thin.c +0 -7
@@ -4292,13 +4292,6 @@ static int thin_ctr(struct dm_target *ti, unsigned int argc, char **argv)init_completion (&tc->can_destroy);list_add_tail_rcu (&tc->list, &tc->pool->active_thins);spin_unlock_irq (&tc->pool->lock); -/* -* This synchronize_rcu() call is needed here otherwise we risk a -* wake_worker() call finding no bios to process (because the newly -* added tc isn't yet visible). So this reduces latency since we -* aren't then dependent on the periodic commit to wake_worker(). -*/ -synchronize_rcu ();dm_put (pool_md);
Signature · 1 line
2.43.0
PATCH v3 2/3 dm thin: keep a thin on active_thins until its last reference is dropped
In reply to Qiliang Yuanget_first_thin() and get_next_thin() let the pool worker process the thins on pool->active_thins without holding rcu_read_lock() while it
does: they take a reference on a thin under the lock, and get_next_thin() continues from that thin in a new read-side section. thin_dtr() takes the thin off the list, waits for a grace period and then for the references still held.
Once thin_dtr() has removed a thin that the worker holds, nothing updates its list.next pointer any more. If the thin after it is removed and freed before the worker calls get_next_thin(), the walk continues from freed memory:
BUG: KASAN: slab-use-after-free in get_next_thin+0x8b/0x240 [dm_thin_pool] Write of size 4 at addr ff11000116371cc8 by task kworker/u128:0/13 Workqueue: dm-thin do_worker [dm_thin_pool] Call Trace: get_next_thin+0x8b/0x240 [dm_thin_pool] process_deferred_bios+0x34/0x650 [dm_thin_pool] do_worker+0x2b7/0x4f0 [dm_thin_pool] process_one_work+0x8ce/0x1850 worker_thread+0x639/0x1190 Allocated by task 2290: thin_ctr+0x130/0x1260 [dm_thin_pool] dm_table_add_target+0x65f/0xf60 [dm_mod] table_load+0x1c2/0x480 [dm_mod] Freed by task 2391: kfree+0x15d/0x550 thin_dtr+0x21c/0x2c0 [dm_thin_pool] dm_table_destroy+0x13c/0x570 [dm_mod] __dm_destroy+0x2eb/0x5d0 [dm_mod] dev_remove+0x2f1/0x3c0 [dm_mod]
Mark the thin as dying in thin_dtr() instead of removing it, and take it off the list only after the last reference is dropped, so that removing its neighbours keeps its list.next valid. Take references with refcount_inc_not_zero() and skip dying thins, so that thin_dtr() only waits for the references already taken, and have requeue_bios() and error_retry_list_with_code() skip dying thins as they skipped the thins already off the list.
With a test-only 3 s msleep() added to the worker after each thin it processes, thins 1 and 2 of a pool with three thins are suspended and then removed while the worker holds thin 1. Trials in which KASAN reports a slab-use-after-free in get_next_thin():
before (trials) after (trials) change
KASAN report 3 of 3 0 of 5 -100%
--- drivers/md/dm-thin.c | 49 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 16 deletions(-)
drivers/md/dm-thin.c +33 -16
@@ -355,11 +355,16 @@ struct thin_c {struct rb_root sort_bio_list;/* sorted list of deferred bios */ /* -* Ensures the thin is not destroyed until the worker has finished -* iterating the active_thins list. +* Ensures the thin stays on the active_thins list until the workers +* have moved past it while iterating the list. */ refcount_t refcount;struct completion can_destroy; +/* +* Keeps new active_thin walkers from grabbing the table while +* thin_dtr() is waiting for the existing holders to finish. +*/ +bool dying; };/*----------------------------------------------------------------*/
@@ -652,8 +657,11 @@ static void error_retry_list_with_code(struct pool *pool, blk_status_t error)struct thin_c *tc;rcu_read_lock (); -list_for_each_entry_rcu (tc, &pool->active_thins, list) +list_for_each_entry_rcu (tc, &pool->active_thins, list) { +if (READ_ONCE (tc->dying)) +continue ;error_thin_bio_list (tc, &tc->retry_on_resume_list, error); + }rcu_read_unlock (); }
@@ -2318,25 +2326,29 @@ static void process_thin_deferred_cells(struct thin_c *tc) }while (!list_empty (&cells)); } -static void thin_get (struct thin_c *tc); +static bool thin_tryget (struct thin_c *tc);static void thin_put (struct thin_c *tc);/* * We can't hold rcu_read_lock() around code that can block. So we * find a thin with the rcu lock held; bump a refcount; then drop -* the lock. +* the lock. Skip the thins marked as dying or whose refcount thin_dtr() +* already dropped to zero. */ static struct thin_c *get_first_thin (struct pool *pool) { -struct thin_c *tc =NULL ; +struct thin_c *tc;rcu_read_lock (); - tc = list_first_or_null_rcu(&pool->active_thins, struct thin_c, list); -if (tc) -thin_get (tc); +list_for_each_entry_rcu (tc, &pool->active_thins, list) { +if (thin_tryget (tc)) { +rcu_read_unlock (); +return tc; + } + }rcu_read_unlock (); -return tc; +return NULL ; }static struct thin_c *get_next_thin (struct pool *pool,struct thin_c *tc)
@@ -2345,7 +2357,8 @@ static struct thin_c *get_next_thin(struct pool *pool, struct thin_c *tc)rcu_read_lock ();list_for_each_entry_continue_rcu (tc, &pool->active_thins, list) { -thin_get (tc); +if (!thin_tryget (tc)) +continue ;thin_put (old_tc);rcu_read_unlock ();return tc;
@@ -2817,6 +2830,8 @@ static void requeue_bios(struct pool *pool)rcu_read_lock ();list_for_each_entry_rcu (tc, &pool->active_thins, list) { +if (READ_ONCE (tc->dying)) +continue ;spin_lock_irq (&tc->lock);bio_list_merge (&tc->deferred_bio_list, &tc->retry_on_resume_list);bio_list_init (&tc->retry_on_resume_list);
@@ -4131,9 +4146,9 @@ static struct target_type pool_target = {* Thin target methods *-------------------------------------------------------------- */ -static void thin_get (struct thin_c *tc) +static bool thin_tryget (struct thin_c *tc) { -refcount_inc (&tc->refcount); +return !READ_ONCE (tc->dying) &&refcount_inc_not_zero (&tc->refcount); }static void thin_put (struct thin_c *tc)
@@ -4146,14 +4161,16 @@ static void thin_dtr(struct dm_target *ti) {struct thin_c *tc = ti->private; +WRITE_ONCE (tc->dying, true); + +thin_put (tc); +wait_for_completion (&tc->can_destroy); +spin_lock_irq (&tc->pool->lock);list_del_rcu (&tc->list);spin_unlock_irq (&tc->pool->lock);synchronize_rcu (); -thin_put (tc); -wait_for_completion (&tc->can_destroy); -mutex_lock (&dm_thin_pool_table.mutex);__pool_dec (tc->pool);
Signature · 1 line
2.43.0
PATCH v3 3/3 dm thin: free thins with kfree_rcu() instead of waiting in thin_dtr()
In reply to Qiliang Yuanthin_dtr() takes the thin off pool->active_thins once its last reference is dropped and then waits for a grace period, so that requeue_bios() and error_retry_list_with_code(), which walk the list under rcu_read_lock(), are done with the thin before it is freed.
Removing a thin device thus waits for a full grace period in thin_dtr(), although those readers only need the memory of the thin to stay around.
Free the thin with kfree_rcu() instead of waiting for the grace period.
Removing 100 thin devices of an idle pool with dmsetup remove takes per device (mean of five rounds, min-max of the rounds in parentheses). The queue freeze in del_gendisk() waits for a grace period of its own, which the one in thin_dtr() used to overlap, so only part of it is saved:
before (ms) after (ms) change
17.2 (17.0-17.4) 13.7 (13.2-14.4) -20%
--- drivers/md/dm-thin.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/md/dm-thin.c +3 -2
@@ -360,6 +360,7 @@ struct thin_c {*/ refcount_t refcount;struct completion can_destroy; +struct rcu_head rcu;/* * Keeps new active_thin walkers from grabbing the table while * thin_dtr() is waiting for the existing holders to finish.
@@ -4169,7 +4170,6 @@ static void thin_dtr(struct dm_target *ti)spin_lock_irq (&tc->pool->lock);list_del_rcu (&tc->list);spin_unlock_irq (&tc->pool->lock); -synchronize_rcu ();mutex_lock (&dm_thin_pool_table.mutex);
@@ -4178,7 +4178,8 @@ static void thin_dtr(struct dm_target *ti)dm_put_device (ti, tc->pool_dev);if (tc->origin_dev)dm_put_device (ti, tc->origin_dev); -kfree (tc); +/* Let RCU readers of active_thins finish with tc before freeing it */ +kfree_rcu (tc, rcu);mutex_unlock (&dm_thin_pool_table.mutex); }
Signature · 1 line
2.43.0
Benjamin Marzinski
maintainerRe: [PATCH v3 2/3] dm thin: keep a thin on active_thins until its last reference is dropped
In reply to Qiliang YuanQuoted 48 lines · Qiliang Yuan
get_first_thin()andget_next_thin()let the pool worker process the thins onpool->active_thinswithout holdingrcu_read_lock()while it
does: they take a reference on a thin under the lock, andget_next_thin()continues from that thin in a new read-side section.thin_dtr()takes the thin off the list, waits for a grace period and then for the references still held.Once
thin_dtr()has removed a thin that the worker holds, nothing updates its list.next pointer any more. If the thin after it is removed and freed before the worker callsget_next_thin(), the walk continues from freed memory:BUG: KASAN: slab-use-after-free in get_next_thin+0x8b/0x240 [dm_thin_pool] Write of size 4 at addr ff11000116371cc8 by task kworker/u128:0/13 Workqueue: dm-thin do_worker [dm_thin_pool] Call Trace: get_next_thin+0x8b/0x240 [dm_thin_pool] process_deferred_bios+0x34/0x650 [dm_thin_pool] do_worker+0x2b7/0x4f0 [dm_thin_pool] process_one_work+0x8ce/0x1850 worker_thread+0x639/0x1190 Allocated by task 2290: thin_ctr+0x130/0x1260 [dm_thin_pool] dm_table_add_target+0x65f/0xf60 [dm_mod] table_load+0x1c2/0x480 [dm_mod] Freed by task 2391: kfree+0x15d/0x550 thin_dtr+0x21c/0x2c0 [dm_thin_pool] dm_table_destroy+0x13c/0x570 [dm_mod] __dm_destroy+0x2eb/0x5d0 [dm_mod] dev_remove+0x2f1/0x3c0 [dm_mod]Mark the thin as dying in
thin_dtr()instead of removing it, and take it off the list only after the last reference is dropped, so that removing its neighbours keeps its list.next valid. Take references withrefcount_inc_not_zero()and skip dying thins, so thatthin_dtr()only waits for the references already taken, and haverequeue_bios()anderror_retry_list_with_code()skip dying thins as they skipped the thins already off the list.With a test-only 3 s
msleep()added to the worker after each thin it processes, thins 1 and 2 of a pool with three thins are suspended and then removed while the worker holds thin 1. Trials in which KASAN reports a slab-use-after-free inget_next_thin():before (trials) after (trials) change
KASAN report 3 of 3 0 of 5 -100%
Quoted 7 lines
Fixes 17181fb7a0c3 ("dm thin: fix a race in thin_dtr")Reported-by Benjamin Marzinski <[email protected]>Co-developed-by Benjamin Marzinski <[email protected]>Signed-off-by Benjamin Marzinski <[email protected]>Signed-off-by Qiliang Yuan <[email protected]>
Thanks for updating this.
-Ben
Re: [PATCH v3 3/3] dm thin: free thins with kfree_rcu() instead of waiting in thin_dtr()
In reply to Qiliang YuanQuoted 15 lines · Qiliang Yuan
thin_dtr()takes the thin offpool->active_thinsonce its last reference is dropped and then waits for a grace period, so thatrequeue_bios()anderror_retry_list_with_code(), which walk the list underrcu_read_lock(), are done with the thin before it is freed.Removing a thin device thus waits for a full grace period in
thin_dtr(), although those readers only need the memory of the thin to stay around.Free the thin with
kfree_rcu()instead of waiting for the grace period.Removing 100 thin devices of an idle pool with dmsetup remove takes per device (mean of five rounds, min-max of the rounds in parentheses). The queue freeze in
del_gendisk()waits for a grace period of its own, which the one inthin_dtr()used to overlap, so only part of it is saved:
Quoted 4 lines
before (ms) after (ms) change
17.2 (17.0-17.4) 13.7 (13.2-14.4) -20%Signed-off-by Qiliang Yuan <[email protected]>
Re: [PATCH v3 2/3] dm thin: keep a thin on active_thins until its last reference is dropped
In reply to Qiliang YuanQuoted 33 lines · Qiliang Yuan
get_first_thin()andget_next_thin()let the pool worker process the thins onpool->active_thinswithout holdingrcu_read_lock()while it
does: they take a reference on a thin under the lock, andget_next_thin()continues from that thin in a new read-side section.thin_dtr()takes the thin off the list, waits for a grace period and then for the references still held.Once
thin_dtr()has removed a thin that the worker holds, nothing updates its list.next pointer any more. If the thin after it is removed and freed before the worker callsget_next_thin(), the walk continues from freed memory:BUG: KASAN: slab-use-after-free in get_next_thin+0x8b/0x240 [dm_thin_pool] Write of size 4 at addr ff11000116371cc8 by task kworker/u128:0/13 Workqueue: dm-thin do_worker [dm_thin_pool] Call Trace: get_next_thin+0x8b/0x240 [dm_thin_pool] process_deferred_bios+0x34/0x650 [dm_thin_pool] do_worker+0x2b7/0x4f0 [dm_thin_pool] process_one_work+0x8ce/0x1850 worker_thread+0x639/0x1190 Allocated by task 2290: thin_ctr+0x130/0x1260 [dm_thin_pool] dm_table_add_target+0x65f/0xf60 [dm_mod] table_load+0x1c2/0x480 [dm_mod] Freed by task 2391: kfree+0x15d/0x550 thin_dtr+0x21c/0x2c0 [dm_thin_pool] dm_table_destroy+0x13c/0x570 [dm_mod] __dm_destroy+0x2eb/0x5d0 [dm_mod] dev_remove+0x2f1/0x3c0 [dm_mod]
Quoted 7 lines
Mark the thin as dying in
thin_dtr()instead of removing it, and take it off the list only after the last reference is dropped, so that removing its neighbours keeps its list.next valid. Take references withrefcount_inc_not_zero()and skip dying thins, so thatthin_dtr()only waits for the references already taken, and haverequeue_bios()anderror_retry_list_with_code()skip dying thins as they skipped the thins already off the list.
The code looks correct to me. These notes are only for the record. The two checks in thin_tryget() have different roles.
refcount_inc_not_zero() is what makes the patch correct. Once the count reaches zero, no walker can take the thin again, so list_del_rcu() is safe after wait_for_completion() returns.
The dying flag is only a hint. As Ben said, it is racy. It keeps the old behavior of taking the thin off the list early, so thin_dtr() mostly waits for the references already taken. For the ref-less readers (requeue_bios() and error_retry_list_with_code()), the dying check keeps most of them away, but their actual safety comes from the synchronize_rcu() after list_del_rcu().
Quoted 7 lines
With a test-only 3 s
msleep()added to the worker after each thin it processes, thins 1 and 2 of a pool with three thins are suspended and then removed while the worker holds thin 1. Trials in which KASAN reports a slab-use-after-free inget_next_thin():before (trials) after (trials) change
KASAN report 3 of 3 0 of 5 -100%
Two thins are enough. Suspend both thins, let the worker sleep while it holds thin 1, and then remove thin 1 and thin 2. When the worker wakes up, get_next_thin() follows thin 1's stale list.next to the freed thin 2:
dmsetup suspend thin_1
dmsetup suspend thin_2
# make the worker sleep while it holds thin_1
dmsetup remove thin_1 & # thin_dtr() unlinks thin_1, then waits
dmsetup remove thin_2 # thin_dtr() unlinks and frees thin_2
The suspend step is needed on kernels before v6.19. Before be4addb1914f ("dm: Fix deadlock when reloading a multipath table"), thin_postsuspend() always called noflush_work(), which queues a work item on the ordered pool workqueue and waits for it. Removing an active device runs the presuspend and postsuspend hooks, so the removal waited until the worker returned. By then the worker had dropped thin 1, so the bug did not trigger. A suspended device skips the hooks on removal and goes straight to thin_dtr().
The walkers in pool_suspend_active_thins() and pool_resume_active_thins() hit the same bug. To reproduce it there, put the sleep in those walkers instead. It then runs in the dmsetup process, not on the pool workqueue, so the suspend step is not needed.
Quoted 7 lines
Fixes 17181fb7a0c3 ("dm thin: fix a race in thin_dtr")Reported-by Benjamin Marzinski <[email protected]>Co-developed-by Benjamin Marzinski <[email protected]>Signed-off-by Benjamin Marzinski <[email protected]>Signed-off-by Qiliang Yuan <[email protected]>
Re: [PATCH v3 3/3] dm thin: free thins with kfree_rcu() instead of waiting in thin_dtr()
In reply to Qiliang YuanQuoted 8 lines · Qiliang Yuan
thin_dtr()takes the thin offpool->active_thinsonce its last reference is dropped and then waits for a grace period, so thatrequeue_bios()anderror_retry_list_with_code(), which walk the list underrcu_read_lock(), are done with the thin before it is freed.Removing a thin device thus waits for a full grace period in
thin_dtr(), although those readers only need the memory of the thin to stay around.
For the record, this holds only because both bio lists of the thin are empty when thin_dtr() runs: the thin is suspended, inactive, or no longer open.
Without the grace period, the two ref-less readers (requeue_bios() and error_retry_list_with_code()) can still be working on the thin after thin_dtr() returns. They access only tc->lock, deferred_bio_list and retry_on_resume_list inside their RCU read-side critical section, so kfree_rcu() keeps everything they touch valid.
The empty bio lists are what make this safe. With both lists empty, error_retry_list_with_code() ends no bio, so it never reaches the target or the mapped device, both of which dm core may free right after thin_dtr() returns. Thus, kfree_rcu() is enough.
Quoted 11 lines
Free the thin with
kfree_rcu()instead of waiting for the grace period.Removing 100 thin devices of an idle pool with dmsetup remove takes per device (mean of five rounds, min-max of the rounds in parentheses). The queue freeze in
del_gendisk()waits for a grace period of its own, which the one inthin_dtr()used to overlap, so only part of it is saved:before (ms) after (ms) change
17.2 (17.0-17.4) 13.7 (13.2-14.4) -20%Signed-off-by Qiliang Yuan <[email protected]>