mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/3] dm thin: stop waiting for grace periods when activating and removing thins
@ 2026-11-19  5:45 Qiliang Yuan
  2026-11-19  5:45 ` [PATCH v2 1/3] dm thin: drop the grace period from thin_ctr() Qiliang Yuan
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Qiliang Yuan @ 2026-11-19  5:45 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, Mikulas Patocka, Benjamin Marzinski
  Cc: dm-devel, linux-kernel, Qiliang Yuan, stable

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: a thin now stays on the list until 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 51% faster and
removing one 19%.

Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
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

v1: https://lore.kernel.org/r/20261116-bug-dm-thin-rcu-gp-v1-0-14ed3567da3e@linux.dev

---
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 | 60 +++++++++++++++++++++++++++++-----------------------
 1 file changed, 34 insertions(+), 26 deletions(-)
---
base-commit: 63fdb511c27afcebce797e3db580b3534ddfa639
change-id: 20261114-bug-dm-thin-rcu-gp-d29d4b74a0b8

Best regards,
-- 
Qiliang Yuan <qiliang.yuan@linux.dev>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 1/3] dm thin: drop the grace period from thin_ctr()
  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 ` Qiliang Yuan
  2026-11-19  5:45 ` [PATCH v2 2/3] dm thin: keep a thin on active_thins until its last reference is dropped Qiliang Yuan
  2026-11-19  5:45 ` [PATCH v2 3/3] dm thin: free thins with kfree_rcu() instead of waiting in thin_dtr() Qiliang Yuan
  2 siblings, 0 replies; 4+ messages in thread
From: Qiliang Yuan @ 2026-11-19  5:45 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, Mikulas Patocka, Benjamin Marzinski
  Cc: dm-devel, linux-kernel, Qiliang Yuan

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.3 (14.8-15.7)   7.5 (7.3-7.7)      -51%

Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
Reviewed-by: Benjamin Marzinski <bmarzins@redhat.com>
---
 drivers/md/dm-thin.c | 7 -------
 1 file changed, 7 deletions(-)

diff --git a/drivers/md/dm-thin.c b/drivers/md/dm-thin.c
index 0b65c1b09f47b..bdd250d1eb2e1 100644
--- a/drivers/md/dm-thin.c
+++ b/drivers/md/dm-thin.c
@@ -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);
 

-- 
2.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 2/3] dm thin: keep a thin on active_thins until its last reference is dropped
  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
  2026-11-19  5:45 ` [PATCH v2 3/3] dm thin: free thins with kfree_rcu() instead of waiting in thin_dtr() Qiliang Yuan
  2 siblings, 0 replies; 4+ messages in thread
From: Qiliang Yuan @ 2026-11-19  5:45 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, Mikulas Patocka, Benjamin Marzinski
  Cc: dm-devel, linux-kernel, Qiliang Yuan, stable

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 3/3] dm thin: free thins with kfree_rcu() instead of waiting in thin_dtr()
  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 ` [PATCH v2 2/3] dm thin: keep a thin on active_thins until its last reference is dropped Qiliang Yuan
@ 2026-11-19  5:45 ` Qiliang Yuan
  2 siblings, 0 replies; 4+ messages in thread
From: Qiliang Yuan @ 2026-11-19  5:45 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, Mikulas Patocka, Benjamin Marzinski
  Cc: dm-devel, linux-kernel, Qiliang Yuan

thin_dtr() waits until the last reference on the thin is dropped, which
also takes it off pool->active_thins, and then 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
  16.6 (16.2-17.0)   13.5 (13.2-14.0)     -19%

Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
 drivers/md/dm-thin.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/md/dm-thin.c b/drivers/md/dm-thin.c
index f79f5982ae36c..9d9b3cd817f7a 100644
--- a/drivers/md/dm-thin.c
+++ b/drivers/md/dm-thin.c
@@ -362,6 +362,7 @@ struct thin_c {
 	refcount_t refcount;
 	struct completion can_destroy;
 	bool dead;
+	struct rcu_head rcu;
 };
 
 /*----------------------------------------------------------------*/
@@ -4166,8 +4167,6 @@ static void thin_dtr(struct dm_target *ti)
 	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);
 
@@ -4176,7 +4175,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);
 }

-- 
2.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-19  5:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH v2 2/3] dm thin: keep a thin on active_thins until its last reference is dropped Qiliang Yuan
2026-11-19  5:45 ` [PATCH v2 3/3] dm thin: free thins with kfree_rcu() instead of waiting in thin_dtr() Qiliang Yuan

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®