Re: [PATCH v3 2/3] dm thin: keep a thin on active_thins until its last reference is dropped
From: Ming Hung Tsai
Date: Wed Nov 25 2026 - 14:17:22 EST
On Thu, Nov 19, 2026 at 7:12 PM Qiliang Yuan <qiliang.yuan@xxxxxxxxx> wrote:
>
> get_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.
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().
> 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%
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.
> Fixes: 17181fb7a0c3 ("dm thin: fix a race in thin_dtr")
> Cc: stable@xxxxxxxxxxxxxxx
> Reported-by: Benjamin Marzinski <bmarzins@xxxxxxxxxx>
> Closes: https://lore.kernel.org/r/avz7qDWd2g-uubmL@xxxxxxxxxx
> Co-developed-by: Benjamin Marzinski <bmarzins@xxxxxxxxxx>
> Signed-off-by: Benjamin Marzinski <bmarzins@xxxxxxxxxx>
> Signed-off-by: Qiliang Yuan <qiliang.yuan@xxxxxxxxx>