* [PATCH v3] block: don't wait for all RCU callbacks when freeing zone write plugs
@ 2026-11-25 8:41 Qiliang Yuan
2026-11-25 17:00 ` Bart Van Assche
0 siblings, 1 reply; 3+ messages in thread
From: Qiliang Yuan @ 2026-11-25 8:41 UTC (permalink / raw)
To: Damien Le Moal, Jens Axboe; +Cc: linux-block, Qiliang Yuan
Zone write plugs are allocated from a per-disk mempool and freed with
call_rcu(). Before destroying the mempool, which happens every time a
zoned disk is released, disk_destroy_zone_wplugs_hash_table() calls
rcu_barrier() so that no zone write plug is still waiting to be returned
to it.
rcu_barrier() waits for the RCU callbacks queued on every CPU by anyone,
which usually takes a grace period or more, even when the disk had no
zone write plug left to free. Removing many zoned disks, such as the SMR
drives of a storage node taken offline, pays this once per disk.
Count the zone write plugs of the disk that wait for their RCU callback,
and wait only for that count to drop to zero.
Removing 32 zoned null_blk or scsi_debug devices, base and patched
kernels alternating over 2 rounds of 3 runs, median of the 6 runs and
range in parentheses; "with plugs" leaves four partially written zones,
and so four zone write plugs, on each null_blk device:
devices before (ms) after (ms) change
null_blk 820 (805-885) 438 (424-454) -47%
null_blk, with plugs 876 (852-883) 844 (805-877) -4%
scsi_debug, host-managed 958 (945-991) 642 (630-652) -33%
Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
Reviewed-by: Damien Le Moal <dlemoal@kernel.org>
---
V2 -> V3:
- Initialize nr_zone_wplugs_freeing in disk_init_zone_resources() (Damien Le Moal)
V1 -> V2:
- Keep the original comment above the wait (Damien Le Moal)
- Add Reviewed-by (Damien Le Moal)
v2: https://lore.kernel.org/r/20261124-feat-blk-zoned-rcu-barrier-v2-1-6d2212a48c5c@linux.dev
v1: https://lore.kernel.org/r/20261121-feat-blk-zoned-rcu-barrier-v1-1-766875b525be@linux.dev
---
block/blk-zoned.c | 11 +++++++++--
include/linux/blkdev.h | 1 +
2 files changed, 10 insertions(+), 2 deletions(-)
diff --git a/block/blk-zoned.c b/block/blk-zoned.c
index 4c58892961912..2e61a9f23b270 100644
--- a/block/blk-zoned.c
+++ b/block/blk-zoned.c
@@ -18,6 +18,7 @@
#include <linux/mempool.h>
#include <linux/kthread.h>
#include <linux/freezer.h>
+#include <linux/wait_bit.h>
#include <trace/events/block.h>
@@ -724,8 +725,11 @@ static void disk_free_zone_wplug_rcu(struct rcu_head *rcu_head)
{
struct blk_zone_wplug *zwplug =
container_of(rcu_head, struct blk_zone_wplug, rcu_head);
+ struct gendisk *disk = zwplug->disk;
- mempool_free(zwplug, zwplug->disk->zone_wplugs_pool);
+ mempool_free(zwplug, disk->zone_wplugs_pool);
+ if (atomic_dec_and_test(&disk->nr_zone_wplugs_freeing))
+ wake_up_var(&disk->nr_zone_wplugs_freeing);
}
static void disk_free_zone_wplug(struct blk_zone_wplug *zwplug)
@@ -745,6 +749,7 @@ static void disk_free_zone_wplug(struct blk_zone_wplug *zwplug)
atomic_dec(&disk->nr_zone_wplugs);
spin_unlock_irqrestore(&disk->zone_wplugs_hash_lock, flags);
+ atomic_inc(&disk->nr_zone_wplugs_freeing);
call_rcu(&zwplug->rcu_head, disk_free_zone_wplug_rcu);
}
@@ -2022,6 +2027,7 @@ void disk_init_zone_resources(struct gendisk *disk)
{
mutex_init(&disk->zone_revalidate_mutex);
atomic_set(&disk->nr_zone_wplugs, 0);
+ atomic_set(&disk->nr_zone_wplugs_freeing, 0);
spin_lock_init(&disk->zone_wplugs_hash_lock);
spin_lock_init(&disk->zone_wplugs_list_lock);
INIT_LIST_HEAD(&disk->zone_wplugs_list);
@@ -2149,7 +2155,8 @@ static void disk_destroy_zone_wplugs_hash_table(struct gendisk *disk)
* Wait for the zone write plugs to be RCU-freed before destroying the
* mempool.
*/
- rcu_barrier();
+ wait_var_event(&disk->nr_zone_wplugs_freeing,
+ !atomic_read_acquire(&disk->nr_zone_wplugs_freeing));
mempool_destroy(disk->zone_wplugs_pool);
disk->zone_wplugs_pool = NULL;
}
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index 215753171cef0..b3b356f832267 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -204,6 +204,7 @@ struct gendisk {
u8 __rcu *zones_state;
unsigned int zone_wplugs_hash_bits;
atomic_t nr_zone_wplugs;
+ atomic_t nr_zone_wplugs_freeing;
spinlock_t zone_wplugs_hash_lock;
struct mempool *zone_wplugs_pool;
struct hlist_head *zone_wplugs_hash;
---
base-commit: 5f22fb20497f4983094131f00a734362300d22d2
change-id: 20261120-feat-blk-zoned-rcu-barrier-cb9044e67ab3
Best regards,
--
Qiliang Yuan <qiliang.yuan@linux.dev>
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v3] block: don't wait for all RCU callbacks when freeing zone write plugs
2026-11-25 8:41 [PATCH v3] block: don't wait for all RCU callbacks when freeing zone write plugs Qiliang Yuan
@ 2026-11-25 17:00 ` Bart Van Assche
2026-11-25 21:59 ` Damien Le Moal
0 siblings, 1 reply; 3+ messages in thread
From: Bart Van Assche @ 2026-11-25 17:00 UTC (permalink / raw)
To: Qiliang Yuan, Damien Le Moal, Jens Axboe; +Cc: linux-block
On 11/25/26 00:41, Qiliang Yuan wrote:
> Removing 32 zoned null_blk or scsi_debug devices, base and patched
> kernels alternating over 2 rounds of 3 runs, median of the 6 runs and
> range in parentheses; "with plugs" leaves four partially written zones,
> and so four zone write plugs, on each null_blk device:
>
> devices before (ms) after (ms) change
> null_blk 820 (805-885) 438 (424-454) -47%
> null_blk, with plugs 876 (852-883) 844 (805-877) -4%
> scsi_debug, host-managed 958 (945-991) 642 (630-652) -33%
Real zoned drives taken offline on a storage node typically have
partially written zones and therefore still have active zone write plugs
in disk->zone_wplugs_hash at teardown time. The reason "null_blk, with
plugs" only improves by 4% is that disk_destroy_zone_wplugs_hash_table()
frees those remaining plugs via disk_mark_zone_wplug_dead() ->
disk_free_zone_wplug() -> call_rcu(), which still forces an RCU grace
period wait for every disk that had at least one active plug.
By the time disk_destroy_zone_wplugs_hash_table() runs from
disk_release(), no RCU readers can still be accessing
disk->zone_wplugs_hash (the function already iterates the hash table
without zone_wplugs_hash_lock, assumes every plug has ref == 1, and
frees disk->zone_wplugs_hash synchronously with kfree()). Nor is
updating disk->zones_state needed, as disk_release_zone_resources()
clears it immediately afterward.
Have you considered unhashing and freeing the plugs still in
disk->zone_wplugs_hash directly with mempool_free() (keeping the
WARN_ON_ONCE() checks) instead of going through call_rcu()? Then
nr_zone_wplugs_freeing would only wait for plugs that were already
queued for RCU freeing shortly before disk_release(), and the "with
plugs" case would skip the RCU wait as well.
> diff --git a/block/blk-zoned.c b/block/blk-zoned.c
> index 4c58892961912..2e61a9f23b270 100644
> --- a/block/blk-zoned.c
> +++ b/block/blk-zoned.c
> @@ -724,8 +725,11 @@ static void disk_free_zone_wplug_rcu(struct
rcu_head *rcu_head)
> {
> struct blk_zone_wplug *zwplug =
> container_of(rcu_head, struct blk_zone_wplug, rcu_head);
> + struct gendisk *disk = zwplug->disk;
>
> - mempool_free(zwplug, zwplug->disk->zone_wplugs_pool);
> + mempool_free(zwplug, disk->zone_wplugs_pool);
> + if (atomic_dec_and_test(&disk->nr_zone_wplugs_freeing))
> + wake_up_var(&disk->nr_zone_wplugs_freeing);
> }
If keeping wait_var_event(), this can be simplified with
atomic_dec_and_wake_up(&disk->nr_zone_wplugs_freeing) from
<linux/wait_bit.h>.
> static void disk_free_zone_wplug(struct blk_zone_wplug *zwplug)
> @@ -745,6 +749,7 @@ static void disk_free_zone_wplug(struct
blk_zone_wplug *zwplug)
> atomic_dec(&disk->nr_zone_wplugs);
> spin_unlock_irqrestore(&disk->zone_wplugs_hash_lock, flags);
>
> + atomic_inc(&disk->nr_zone_wplugs_freeing);
> call_rcu(&zwplug->rcu_head, disk_free_zone_wplug_rcu);
> }
>
> @@ -2149,7 +2155,8 @@ static void
disk_destroy_zone_wplugs_hash_table(struct gendisk *disk)
> * Wait for the zone write plugs to be RCU-freed before destroying the
> * mempool.
> */
> - rcu_barrier();
> + wait_var_event(&disk->nr_zone_wplugs_freeing,
> + !atomic_read_acquire(&disk->nr_zone_wplugs_freeing));
> mempool_destroy(disk->zone_wplugs_pool);
> disk->zone_wplugs_pool = NULL;
> }
There is a regression here with CONFIG_RCU_LAZY=y on rcu_nocbs CPUs:
call_rcu() queues disk_free_zone_wplug_rcu() lazily, which can delay
starting the grace period by up to LAZY_FLUSH_JIFFIES (10 seconds).
Previously, rcu_barrier() explicitly hurried all pending lazy callbacks
(via rcu_nocb_flush_bypass()), whereas wait_var_event() does not, so
removing a zoned disk with pending callbacks can stall for up to 10
seconds per disk.
Either:
- switch disk_free_zone_wplug() to call_rcu_hurry(), or
- keep call_rcu() and conditionally call rcu_barrier() only when
atomic_read_acquire(&disk->nr_zone_wplugs_freeing) is non-zero (with
smp_mb__before_atomic() before atomic_dec() in
disk_free_zone_wplug_rcu()). Combined with freeing remaining hashed
plugs synchronously in disk_destroy_zone_wplugs_hash_table(), that
preserves lazy RCU batching during normal operation, avoids
wake_up_var() on every zone free, and still hurries lazy callbacks
if any happen to be in flight at teardown.
Thanks,
Bart.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v3] block: don't wait for all RCU callbacks when freeing zone write plugs
2026-11-25 17:00 ` Bart Van Assche
@ 2026-11-25 21:59 ` Damien Le Moal
0 siblings, 0 replies; 3+ messages in thread
From: Damien Le Moal @ 2026-11-25 21:59 UTC (permalink / raw)
To: Bart Van Assche, Qiliang Yuan, Jens Axboe; +Cc: linux-block
On 11/26/26 02:00, Bart Van Assche wrote:
> On 11/25/26 00:41, Qiliang Yuan wrote:
> > Removing 32 zoned null_blk or scsi_debug devices, base and patched
> > kernels alternating over 2 rounds of 3 runs, median of the 6 runs and
> > range in parentheses; "with plugs" leaves four partially written zones,
> > and so four zone write plugs, on each null_blk device:
> >
> > devices before (ms) after (ms) change
> > null_blk 820 (805-885) 438 (424-454) -47%
> > null_blk, with plugs 876 (852-883) 844 (805-877) -4%
> > scsi_debug, host-managed 958 (945-991) 642 (630-652) -33%
>
> Real zoned drives taken offline on a storage node typically have
> partially written zones and therefore still have active zone write plugs
> in disk->zone_wplugs_hash at teardown time. The reason "null_blk, with
> plugs" only improves by 4% is that disk_destroy_zone_wplugs_hash_table()
> frees those remaining plugs via disk_mark_zone_wplug_dead() ->
> disk_free_zone_wplug() -> call_rcu(), which still forces an RCU grace
> period wait for every disk that had at least one active plug.
>
> By the time disk_destroy_zone_wplugs_hash_table() runs from
> disk_release(), no RCU readers can still be accessing
> disk->zone_wplugs_hash (the function already iterates the hash table
> without zone_wplugs_hash_lock, assumes every plug has ref == 1, and
> frees disk->zone_wplugs_hash synchronously with kfree()). Nor is
> updating disk->zones_state needed, as disk_release_zone_resources()
> clears it immediately afterward.
That is indeed a good point. Thanks Bart.
> Have you considered unhashing and freeing the plugs still in
> disk->zone_wplugs_hash directly with mempool_free() (keeping the
> WARN_ON_ONCE() checks) instead of going through call_rcu()? Then
> nr_zone_wplugs_freeing would only wait for plugs that were already
> queued for RCU freeing shortly before disk_release(), and the "with
> plugs" case would skip the RCU wait as well.
Yes, that should speeds things up significantly, since under normal
circumstances, calls to disk_release() are done with the drive already idle for
some time, and so no zone write plugs rcu free should be pending, most of the time.
--
Damien Le Moal
Western Digital Research
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-25 21:59 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 8:41 [PATCH v3] block: don't wait for all RCU callbacks when freeing zone write plugs Qiliang Yuan
2026-11-25 17:00 ` Bart Van Assche
2026-11-25 21:59 ` Damien Le Moal
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox