[PATCH v2 2/3] dm thin: keep a thin on active_thins until its last reference is dropped
From: Qiliang Yuan
Date: Thu Nov 19 2026 - 00:46:03 EST
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.
Leave a thin on the list until its last reference is dropped and unlink
it then, under the pool lock, so that removing its neighbours keeps its
list.next valid. Have thin_dtr() mark the thin dead, so that no new
walker takes a reference, and make the walkers skip dead thins and the
ones whose last reference is already gone. thin_dtr() waits for the
grace period after the thin is unlinked, before freeing it.
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%
Fixes: b10ebd34cccae ("dm thin: fix rcu_read_lock being held in code that can sleep")
Cc: stable@xxxxxxxxxxxxxxx
Reported-by: Benjamin Marzinski <bmarzins@xxxxxxxxxx>
Closes: https://lore.kernel.org/r/avz7qDWd2g-uubmL@xxxxxxxxxx
Suggested-by: Benjamin Marzinski <bmarzins@xxxxxxxxxx>
Signed-off-by: Qiliang Yuan <qiliang.yuan@xxxxxxxxx>
---
drivers/md/dm-thin.c | 51 +++++++++++++++++++++++++++++++++------------------
1 file changed, 33 insertions(+), 18 deletions(-)
diff --git a/drivers/md/dm-thin.c b/drivers/md/dm-thin.c
index bdd250d1eb2e1..f79f5982ae36c 100644
--- a/drivers/md/dm-thin.c
+++ b/drivers/md/dm-thin.c
@@ -356,10 +356,12 @@ struct thin_c {
/*
* Ensures the thin is not destroyed until the worker has finished
- * iterating the active_thins list.
+ * iterating the active_thins list. Set dead once thin_dtr() starts,
+ * so that no new walker picks the thin up.
*/
refcount_t refcount;
struct completion can_destroy;
+ bool dead;
};
/*----------------------------------------------------------------*/
@@ -2318,25 +2320,28 @@ 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 that thin_dtr() is destroying.
*/
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 +2350,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;
@@ -4131,28 +4137,37 @@ 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->dead) && refcount_inc_not_zero(&tc->refcount);
}
static void thin_put(struct thin_c *tc)
{
- if (refcount_dec_and_test(&tc->refcount))
- complete(&tc->can_destroy);
+ unsigned long flags;
+
+ if (!refcount_dec_and_test(&tc->refcount))
+ return;
+
+ /*
+ * Unlink tc only once nobody holds it: a worker holding it continues
+ * its walk from tc->list.next.
+ */
+ spin_lock_irqsave(&tc->pool->lock, flags);
+ list_del_rcu(&tc->list);
+ spin_unlock_irqrestore(&tc->pool->lock, flags);
+ complete(&tc->can_destroy);
}
static void thin_dtr(struct dm_target *ti)
{
struct thin_c *tc = ti->private;
- spin_lock_irq(&tc->pool->lock);
- list_del_rcu(&tc->list);
- spin_unlock_irq(&tc->pool->lock);
- synchronize_rcu();
-
+ WRITE_ONCE(tc->dead, true);
thin_put(tc);
wait_for_completion(&tc->can_destroy);
+ /* Let RCU readers of active_thins finish with tc */
+ synchronize_rcu();
mutex_lock(&dm_thin_pool_table.mutex);
--
2.43.0