[PATCH v2 3/3] dm thin: free thins with kfree_rcu() instead of waiting in thin_dtr()
From: Qiliang Yuan
Date: Thu Nov 19 2026 - 00:46:13 EST
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@xxxxxxxxx>
---
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