Re: [PATCH v3 3/3] dm thin: free thins with kfree_rcu() instead of waiting in thin_dtr()
From: Ming Hung Tsai
Date: Wed Nov 25 2026 - 14:37:11 EST
On Thu, Nov 19, 2026 at 8:24 PM Qiliang Yuan <qiliang.yuan@xxxxxxxxx> wrote:
>
> 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.
For the record, this holds only because both bio lists of the thin are
empty when thin_dtr() runs: the thin is suspended, inactive, or no
longer open.
Without the grace period, the two ref-less readers (requeue_bios() and
error_retry_list_with_code()) can still be working on the thin after
thin_dtr() returns. They access only tc->lock, deferred_bio_list and
retry_on_resume_list inside their RCU read-side critical section, so
kfree_rcu() keeps everything they touch valid.
The empty bio lists are what make this safe. With both lists empty,
error_retry_list_with_code() ends no bio, so it never reaches the
target or the mapped device, both of which dm core may free right
after thin_dtr() returns. Thus, kfree_rcu() is enough.
> 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@xxxxxxxxx>