dm zoned: fix two idle reclaim problems
v3 cover dm zoned: fix two idle reclaim problems
Changes in v3:
- Patch 2: reworded the comment and dropped the redundant dmz_nr_cache_zones() test, so the condition fits on one line (Damien Le Moal). Patches 1 and 3 are unchanged.
- From and Signed-off-by now carry my full name instead of the GitHub handle, as you preferred on v2. Thanks for checking.
Changes in v2:
- No code changes; only my author address changed.
These patches fix two problems in dm-zoned's reclaim worker, found while running md RAID5 on three dm-zoned targets on 27 TB host-managed SMR drives (WD Ultrastar DC HC680), each target with a regular cache device.
1. On a target with cache zones and no free sequential zone, an idle target keeps copying random zones into other random zones, forever (patch 2; patch 1 makes the resulting -ENOSPC back off instead of spinning). After a full md rebuild every chunk is mapped, so this happens right after a rebuild: the idle member read and wrote about 72 MB/s nonstop for hours.
2. After a reclaim pass that ends while the target is busy, the idle poll is never re-armed, so zones filled under load are not reclaimed when the target goes idle (patch 3). Users notice it as "the buffer never drains"; the workaround has been a timer sending "dmsetup message <dev> 0 reclaim".
Testing: the three patches were built as an out-of-tree dm-zoned module for 7.2.6 (the changed code is identical in current master) and tested
- on emulated host-managed drives (tcmu-runner ZBC handler) in a VM: the idle copy loop reproduced with the stock module (340 MB/s on an idle target, zone counters unchanged), 0 MB/s with the patches;
- on the three real drives since 2026-10-05: crash test, three 1.5 TiB write benchmarks, and eight conversions between single-device and cache-device layouts; emptied zones were reclaimed within minutes of the targets going idle without the reclaim timer.
- with a reproducer that needs no special hardware (scsi_debug zbc=managed, 64 MiB zones, plus a 1 GiB loop cache device): stock module ~600 MB/s of read+write on the idle target with unchanged zone counters, patched module 0 MB/s:
https://github.com/illofspeed/synology-host-managed-smr/tree/main/repro
Not done: a test on current master itself (only 7.2.6), and a rebuild onto a cached member with the patches (the first problem was seen after a rebuild with the stock module).
Related, not addressed here: the double-free in dmz_load_sb() reported on dm-devel on 2026-05-30.
The problems were found and the patches written with the help of an AI coding assistant (hence Assisted-by); the analysis, the tests on real hardware and this description were reviewed by me.
Kyrylo Biryuk (3):
dm zoned: back off when reclaim finds no destination zone dm zoned: do not reclaim a random zone into another random zone dm zoned: keep polling for idle reclaim after a reclaim pass
drivers/md/dm-zoned-reclaim.c | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-)
Signature · 1 line
2.47.3
PATCH v3 1/3 dm zoned: back off when reclaim finds no destination zone
When dmz_do_reclaim() fails with -ENOSPC because no free zone is available to copy the data into, dmz_reclaim_work() still ends with dmz_schedule_reclaim(). If the target is idle, dmz_should_reclaim() keeps returning true, the work is queued again with no delay and the worker spins on the same failing pass.
Re-arm the idle poll instead, so that reclaim is retried after DMZ_IDLE_PERIOD. The next patch makes -ENOSPC the expected result when an idle target has random zones but no free sequential zone.
Tested on a 7.2.6 kernel as an out-of-tree dm-zoned module, together with the next two patches, on emulated host-managed drives (tcmu-runner ZBC handler) and on three 27 TB host-managed drives used as md RAID5 members.
--- drivers/md/dm-zoned-reclaim.c | 8 ++++++++ 1 file changed, 8 insertions(+)
drivers/md/dm-zoned-reclaim.c +8 -0
@@ -540,6 +540,14 @@ static void dmz_reclaim_work(struct work_struct *work)if (ret && ret != -EINTR ) {if (!dmz_check_dev (zmd))return ; +if (ret == -ENOSPC ) { +/* +* No zone to copy into: nothing can progress now. +* Rescheduling immediately would spin; poll instead. +*/ +mod_delayed_work (zrc->wq, &zrc->work,DMZ_IDLE_PERIOD ); +return ; + } }dmz_schedule_reclaim (zrc);
Signature · 1 line
2.43.0
Re: [PATCH v3 1/3] dm zoned: back off when reclaim finds no destination zone
Quoted 10 lines · Kyrylo Biryuk
When
dmz_do_reclaim()fails with-ENOSPCbecause no free zone is available to copy the data into,dmz_reclaim_work()still ends withdmz_schedule_reclaim(). If the target is idle,dmz_should_reclaim()keeps returning true, the work is queued again with no delay and the worker spins on the same failing pass.Re-arm the idle poll instead, so that reclaim is retried after
DMZ_IDLE_PERIOD. The next patch makes-ENOSPCthe expected result when an idle target has random zones but no free sequential zone.
Quoted 7 lines
Tested on a 7.2.6 kernel as an out-of-tree dm-zoned module, together with the next two patches, on emulated host-managed drives (tcmu-runner ZBC handler) and on three 27 TB host-managed drives used as md RAID5 members.
Assisted-by LLMSigned-off-by Kyrylo Biryuk <[email protected]>
Looks OK but testing should always be done on the latest mainline kernel, or linux-next.
This also needs a Fixes tag. With that added, please feel free to add:
Signature · 2 lines
Damien Le Moal Western Digital Research
Re: [PATCH v3 1/3] dm zoned: back off when reclaim finds no destination zone
In reply to Kyrylo BiryukQuoted 30 lines · Kyrylo Biryuk
When
dmz_do_reclaim()fails with-ENOSPCbecause no free zone is available to copy the data into,dmz_reclaim_work()still ends withdmz_schedule_reclaim(). If the target is idle,dmz_should_reclaim()keeps returning true, the work is queued again with no delay and the worker spins on the same failing pass.Re-arm the idle poll instead, so that reclaim is retried after
DMZ_IDLE_PERIOD. The next patch makes-ENOSPCthe expected result when an idle target has random zones but no free sequential zone.Tested on a 7.2.6 kernel as an out-of-tree dm-zoned module, together with the next two patches, on emulated host-managed drives (tcmu-runner ZBC handler) and on three 27 TB host-managed drives used as md RAID5 members.
Assisted-by LLMSigned-off-by Kyrylo Biryuk <[email protected]>--- drivers/md/dm-zoned-reclaim.c | 8 ++++++++ 1 file changed, 8 insertions(+)drivers/md/dm-zoned-reclaim.c +1
-0@@ -540,6 +540,14 @@ static void dmz_reclaim_work(struct work_struct *work)if (ret && ret != -EINTR ) {if (!dmz_check_dev (zmd))return ; +if (ret == -ENOSPC ) {
drivers/md/dm-zoned-reclaim.c static void dmz_reclaim_work(struct work_struct *work) · line 553
Quoted 10 lines
drivers/md/dm-zoned-reclaim.c +7
-0+/* +* No zone to copy into: nothing can progress now. +* Rescheduling immediately would spin; poll instead. +*/ +mod_delayed_work (zrc->wq, &zrc->work,DMZ_IDLE_PERIOD ); +return ; + } }dmz_schedule_reclaim (zrc);
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
PATCH v3 2/3 dm zoned: do not reclaim a random zone into another random zone
In reply to Kyrylo Biryukdmz_reclaim_rnd_data() falls back to a random destination zone when no free sequential zone is available and the target has cache zones. When the target is idle and the cache zone list is empty, dmz_get_rnd_zone_for_reclaim() selects random zones of the zoned device as well, so a random zone is then copied into another random zone. That frees nothing: the next pass picks the next random zone and copies it again, for as long as the target stays idle.
Seen on a two-device target (256 GiB regular cache device plus a 27 TB host-managed drive) used as an md RAID5 member: after a full rebuild every chunk was mapped and no sequential zone was free, and the idle drive read and wrote about 72 MB/s each without pause for hours while the free zone counters in "dmsetup status" did not change. Reproduced on emulated host-managed drives (tcmu-runner ZBC handler): 340 MB/s of copying on an idle target, 0 MB/s with this patch.
Only switch to a random destination zone when the zone being reclaimed is a cache zone: moving it to the zoned device still frees cache space.
--- drivers/md/dm-zoned-reclaim.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/md/dm-zoned-reclaim.c +7 -1
@@ -288,7 +288,13 @@ static int dmz_reclaim_rnd_data(struct dmz_reclaim *zrc, struct dm_zone *dzone) again: szone =dmz_alloc_zone (zmd, zrc->dev_idx, alloc_flags |DMZ_ALLOC_RECLAIM ); -if (!szone && alloc_flags ==DMZ_ALLOC_SEQ &&dmz_nr_cache_zones (zmd)) { +/* +* If no free sequential zone is available, a cache zone can still be +* moved into a free random zone of the zoned device, which frees cache +* space. Do not do this for a random zone: moving it into another +* random zone frees nothing and, on an idle target, repeats forever. +*/ +if (!szone && alloc_flags ==DMZ_ALLOC_SEQ &&dmz_is_cache (dzone)) { alloc_flags =DMZ_ALLOC_RND ; goto again; }
Signature · 1 line
2.43.0
Re: [PATCH v3 2/3] dm zoned: do not reclaim a random zone into another random zone
Quoted 16 lines · Kyrylo Biryuk
dmz_reclaim_rnd_data()falls back to a random destination zone when no free sequential zone is available and the target has cache zones. When the target is idle and the cache zone list is empty,dmz_get_rnd_zone_for_reclaim()selects random zones of the zoned device as well, so a random zone is then copied into another random zone. That frees nothing: the next pass picks the next random zone and copies it again, for as long as the target stays idle.Seen on a two-device target (256 GiB regular cache device plus a 27 TB host-managed drive) used as an md RAID5 member: after a full rebuild every chunk was mapped and no sequential zone was free, and the idle drive read and wrote about 72 MB/s each without pause for hours while the free zone counters in "dmsetup status" did not change. Reproduced on emulated host-managed drives (tcmu-runner ZBC handler): 340 MB/s of copying on an idle target, 0 MB/s with this patch.
Quoted 6 lines
Only switch to a random destination zone when the zone being reclaimed is a cache zone: moving it to the zoned device still frees cache space.
Fixes c5c788595292 ("dm zoned: start reclaim with sequential zones")Assisted-by LLMSigned-off-by Kyrylo Biryuk <[email protected]>
Looks good.
Signature · 2 lines
Damien Le Moal Western Digital Research
Re: [PATCH v3 2/3] dm zoned: do not reclaim a random zone into another random zone
In reply to Kyrylo BiryukQuoted 35 lines · Kyrylo Biryuk
dmz_reclaim_rnd_data()falls back to a random destination zone when no free sequential zone is available and the target has cache zones. When the target is idle and the cache zone list is empty,dmz_get_rnd_zone_for_reclaim()selects random zones of the zoned device as well, so a random zone is then copied into another random zone. That frees nothing: the next pass picks the next random zone and copies it again, for as long as the target stays idle.Seen on a two-device target (256 GiB regular cache device plus a 27 TB host-managed drive) used as an md RAID5 member: after a full rebuild every chunk was mapped and no sequential zone was free, and the idle drive read and wrote about 72 MB/s each without pause for hours while the free zone counters in "dmsetup status" did not change. Reproduced on emulated host-managed drives (tcmu-runner ZBC handler): 340 MB/s of copying on an idle target, 0 MB/s with this patch.
Only switch to a random destination zone when the zone being reclaimed is a cache zone: moving it to the zoned device still frees cache space.
Fixes c5c788595292 ("dm zoned: start reclaim with sequential zones")Assisted-by LLMSigned-off-by Kyrylo Biryuk <[email protected]>--- drivers/md/dm-zoned-reclaim.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)drivers/md/dm-zoned-reclaim.c +0
-1@@ -288,7 +288,13 @@ static int dmz_reclaim_rnd_data(struct dmz_reclaim *zrc, struct dm_zone *dzone) again: szone =dmz_alloc_zone (zmd, zrc->dev_idx, alloc_flags |DMZ_ALLOC_RECLAIM ); -if (!szone && alloc_flags ==DMZ_ALLOC_SEQ &&dmz_nr_cache_zones (zmd)) {
drivers/md/dm-zoned-reclaim.c static int dmz_reclaim_rnd_data(struct dmz_reclaim *zrc, struct dm_zone *dzone) · line 300
Quoted 10 lines
drivers/md/dm-zoned-reclaim.c +7
-0+/* +* If no free sequential zone is available, a cache zone can still be +* moved into a free random zone of the zoned device, which frees cache +* space. Do not do this for a random zone: moving it into another +* random zone frees nothing and, on an idle target, repeats forever. +*/ +if (!szone && alloc_flags ==DMZ_ALLOC_SEQ &&dmz_is_cache (dzone)) { alloc_flags =DMZ_ALLOC_RND ; goto again; }
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
PATCH v3 3/3 dm zoned: keep polling for idle reclaim after a reclaim pass
In reply to Kyrylo Biryukdmz_reclaim_work() re-arms its DMZ_IDLE_PERIOD poll only when dmz_should_reclaim() returns false at the start of a pass. After a pass it calls dmz_schedule_reclaim(), which queues the work only if reclaim is needed right now. When a pass ends while the target is still busy and has enough free zones, nothing is queued at all. Once the target goes idle, nothing re-evaluates dmz_should_reclaim() until new I/O arrives or user space sends a "reclaim" message, so the random or cache zones that filled up under load stay full.
Seen on md RAID5 members on 27 TB host-managed drives: after heavy writes the random zones stayed full for hours on idle targets; the workaround was a timer sending "dmsetup message <dev> 0 reclaim". With this patch the emptied and filled zones were reclaimed within minutes of the targets going idle, without the timer.
After a pass, requeue the work immediately if reclaim is still needed, otherwise re-arm the idle poll.
--- drivers/md/dm-zoned-reclaim.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/md/dm-zoned-reclaim.c +10 -1
@@ -556,7 +556,16 @@ static void dmz_reclaim_work(struct work_struct *work) } } -dmz_schedule_reclaim (zrc); +/* +* Keep the worker alive: when a pass ends while the target is busy +* with enough free zones, dmz_schedule_reclaim() queues nothing and +* the idle poll is never re-armed, so zones filled under load are not +* reclaimed once the target goes idle. +*/ +if (dmz_should_reclaim (zrc,dmz_reclaim_percentage (zrc))) +mod_delayed_work (zrc->wq, &zrc->work,0 ); +else +mod_delayed_work (zrc->wq, &zrc->work,DMZ_IDLE_PERIOD ); }/*
Signature · 1 line
2.43.0
Re: [PATCH v3 3/3] dm zoned: keep polling for idle reclaim after a reclaim pass
Quoted 34 lines · Kyrylo Biryuk
dmz_reclaim_work()re-arms itsDMZ_IDLE_PERIODpoll only whendmz_should_reclaim()returns false at the start of a pass. After a pass it callsdmz_schedule_reclaim(), which queues the work only if reclaim is needed right now. When a pass ends while the target is still busy and has enough free zones, nothing is queued at all. Once the target goes idle, nothing re-evaluatesdmz_should_reclaim()until new I/O arrives or user space sends a "reclaim" message, so the random or cache zones that filled up under load stay full.Seen on md RAID5 members on 27 TB host-managed drives: after heavy writes the random zones stayed full for hours on idle targets; the workaround was a timer sending "dmsetup message <dev> 0 reclaim". With this patch the emptied and filled zones were reclaimed within minutes of the targets going idle, without the timer.
After a pass, requeue the work immediately if reclaim is still needed, otherwise re-arm the idle poll.
Fixes 3b1a94c88b79 ("dm zoned: drive-managed zoned block device target")Assisted-by LLMSigned-off-by Kyrylo Biryuk <[email protected]>--- drivers/md/dm-zoned-reclaim.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)drivers/md/dm-zoned-reclaim.c +0
-1@@ -556,7 +556,16 @@ static void dmz_reclaim_work(struct work_struct *work) } } -dmz_schedule_reclaim (zrc);
drivers/md/dm-zoned-reclaim.c static void dmz_reclaim_work(struct work_struct *work) · line 568
Quoted 10 lines
drivers/md/dm-zoned-reclaim.c +10
-0+/* +* Keep the worker alive: when a pass ends while the target is busy +* with enough free zones, dmz_schedule_reclaim() queues nothing and +* the idle poll is never re-armed, so zones filled under load are not +* reclaimed once the target goes idle. +*/ +if (dmz_should_reclaim (zrc,dmz_reclaim_percentage (zrc))) +mod_delayed_work (zrc->wq, &zrc->work,0 ); +else +mod_delayed_work (zrc->wq, &zrc->work,DMZ_IDLE_PERIOD );
This looks OK, but why hardcode this here instead of modifying dmz_schedule_reclaim() to add the "else" case?
Quoted 3 lines
drivers/md/dm-zoned-reclaim.c +0
-0}/*
Signature · 2 lines
Damien Le Moal Western Digital Research
Re: [PATCH v3 3/3] dm zoned: keep polling for idle reclaim after a reclaim pass
In reply to Kyrylo BiryukQuoted 37 lines · Kyrylo Biryuk
dmz_reclaim_work()re-arms itsDMZ_IDLE_PERIODpoll only whendmz_should_reclaim()returns false at the start of a pass. After a pass it callsdmz_schedule_reclaim(), which queues the work only if reclaim is needed right now. When a pass ends while the target is still busy and has enough free zones, nothing is queued at all. Once the target goes idle, nothing re-evaluatesdmz_should_reclaim()until new I/O arrives or user space sends a "reclaim" message, so the random or cache zones that filled up under load stay full.Seen on md RAID5 members on 27 TB host-managed drives: after heavy writes the random zones stayed full for hours on idle targets; the workaround was a timer sending "dmsetup message <dev> 0 reclaim". With this patch the emptied and filled zones were reclaimed within minutes of the targets going idle, without the timer.
After a pass, requeue the work immediately if reclaim is still needed, otherwise re-arm the idle poll.
Fixes 3b1a94c88b79 ("dm zoned: drive-managed zoned block device target")Assisted-by LLMSigned-off-by Kyrylo Biryuk <[email protected]>--- drivers/md/dm-zoned-reclaim.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)drivers/md/dm-zoned-reclaim.c +3
-1@@ -556,7 +556,16 @@ static void dmz_reclaim_work(struct work_struct *work) } } -dmz_schedule_reclaim (zrc); +/* +* Keep the worker alive: when a pass ends while the target is busy +* with enough free zones, dmz_schedule_reclaim() queues nothing and
drivers/md/dm-zoned-reclaim.c static void dmz_reclaim_work(struct work_struct *work) · line 571
Quoted 10 lines
drivers/md/dm-zoned-reclaim.c +7
-0+* the idle poll is never re-armed, so zones filled under load are not +* reclaimed once the target goes idle. +*/ +if (dmz_should_reclaim (zrc,dmz_reclaim_percentage (zrc))) +mod_delayed_work (zrc->wq, &zrc->work,0 ); +else +mod_delayed_work (zrc->wq, &zrc->work,DMZ_IDLE_PERIOD ); }/*
You could simply reschedule here
(ie call 'mod_delayed_work(zrc->wq, &zrc->work, 0)' directly), as the second branch of the condition will be exercised at the start of the function anyway.
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich