All of lore.kernel.org
 help / color / mirror / Atom feed
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>
Subject: [PATCH v3 3/3] dm thin: free thins with kfree_rcu() instead of waiting in thin_dtr()
Date: Thu, 19 Nov 2026 06:05:08 -0500	[thread overview]
Message-ID: <20261119-bug-dm-thin-rcu-gp-v3-3-c80ecc475e1e@linux.dev> (raw)
In-Reply-To: <20261119-bug-dm-thin-rcu-gp-v3-0-c80ecc475e1e@linux.dev>

thin_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%

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

diff --git a/drivers/md/dm-thin.c b/drivers/md/dm-thin.c
index 62ef96bf08cb3..e695fc356c4dc 100644
--- a/drivers/md/dm-thin.c
+++ b/drivers/md/dm-thin.c
@@ -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);
 }

-- 
2.43.0


  parent reply	other threads:[~2026-11-19 11:05 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 11:05 [PATCH v3 0/3] dm thin: stop waiting for grace periods when activating and removing thins Qiliang Yuan
2026-11-19 11:05 ` [PATCH v3 1/3] dm thin: drop the grace period from thin_ctr() Qiliang Yuan
2026-11-19 11:05 ` [PATCH v3 2/3] dm thin: keep a thin on active_thins until its last reference is dropped Qiliang Yuan
2026-11-19 19:20   ` Benjamin Marzinski
2026-11-19 11:05 ` Qiliang Yuan [this message]
2026-11-19 19:20   ` [PATCH v3 3/3] dm thin: free thins with kfree_rcu() instead of waiting in thin_dtr() Benjamin Marzinski

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-v3-3-c80ecc475e1e@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 \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.