From: Qiliang Yuan <qiliang.yuan@linux.dev>
To: Alasdair Kergon <agk@redhat.com>,
Mike Snitzer <snitzer@kernel.org>,
Mikulas Patocka <mpatocka@redhat.com>,
Benjamin Marzinski <bmarzins@redhat.com>
Cc: dm-devel@lists.linux.dev, linux-kernel@vger.kernel.org,
Qiliang Yuan <qiliang.yuan@linux.dev>,
stable@vger.kernel.org
Subject: [PATCH v2 2/3] dm thin: keep a thin on active_thins until its last reference is dropped
Date: Thu, 19 Nov 2026 00:45:33 -0500 [thread overview]
Message-ID: <20261119-bug-dm-thin-rcu-gp-v2-2-055d627763c3@linux.dev> (raw)
In-Reply-To: <20261119-bug-dm-thin-rcu-gp-v2-0-055d627763c3@linux.dev>
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@vger.kernel.org
Reported-by: Benjamin Marzinski <bmarzins@redhat.com>
Closes: https://lore.kernel.org/r/avz7qDWd2g-uubmL@redhat.com
Suggested-by: Benjamin Marzinski <bmarzins@redhat.com>
Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
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
next prev parent reply other threads:[~2026-11-19 5:45 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-19 5:45 [PATCH v2 0/3] dm thin: stop waiting for grace periods when activating and removing thins Qiliang Yuan
2026-11-19 5:45 ` [PATCH v2 1/3] dm thin: drop the grace period from thin_ctr() Qiliang Yuan
2026-11-19 5:45 ` Qiliang Yuan [this message]
2026-11-19 5:45 ` [PATCH v2 3/3] dm thin: free thins with kfree_rcu() instead of waiting in thin_dtr() Qiliang Yuan
2026-11-19 6:25 ` [PATCH v2 0/3] dm thin: stop waiting for grace periods when activating and removing thins Qiliang Yuan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261119-bug-dm-thin-rcu-gp-v2-2-055d627763c3@linux.dev \
--to=qiliang.yuan@linux.dev \
--cc=agk@redhat.com \
--cc=bmarzins@redhat.com \
--cc=dm-devel@lists.linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=mpatocka@redhat.com \
--cc=snitzer@kernel.org \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®