* [PATCH 0/2] Minor fixes to periodic reclaim arming @ 2026-12-01 21:03 Boris Burkov 2026-12-01 21:03 ` [PATCH 1/2] btrfs: arm periodic reclaim on bytes freed, not net bytes freed Boris Burkov 2026-12-01 21:03 ` [PATCH 2/2] btrfs: arm periodic reclaim when it is enabled Boris Burkov 0 siblings, 2 replies; 5+ messages in thread From: Boris Burkov @ 2026-12-01 21:03 UTC (permalink / raw) To: linux-btrfs, kernel-team Periodic reclaiming arming has slightly bitrotted due to being originally committed with a fatal bug. Re-visit it and make the condition possible while still preventing reclaim loops on full inactive filesystems. Boris Burkov (2): btrfs: arm periodic reclaim on bytes freed, not net bytes freed btrfs: arm periodic reclaim when it is enabled fs/btrfs/block-group.c | 8 +++----- fs/btrfs/block-group.h | 2 ++ fs/btrfs/relocation.c | 3 +++ fs/btrfs/space-info.c | 5 ++--- fs/btrfs/space-info.h | 8 +++++--- fs/btrfs/sysfs.c | 3 +++ 6 files changed, 18 insertions(+), 11 deletions(-) -- 2.56.0 ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 1/2] btrfs: arm periodic reclaim on bytes freed, not net bytes freed 2026-12-01 21:03 [PATCH 0/2] Minor fixes to periodic reclaim arming Boris Burkov @ 2026-12-01 21:03 ` Boris Burkov 2026-12-03 17:23 ` Filipe Manana 2026-12-01 21:03 ` [PATCH 2/2] btrfs: arm periodic reclaim when it is enabled Boris Burkov 1 sibling, 1 reply; 5+ messages in thread From: Boris Burkov @ 2026-12-01 21:03 UTC (permalink / raw) To: linux-btrfs, kernel-team Periodic reclaim is designed to avoid getting stuck in busy loops by having a gate (sinfo->periodic_reclaim_ready) which is opened only when we make forward progress or when we NET free a chunk. It also starts shut when periodic reclaim is enabled in the first place. However, it is quite likely on many filesystems that they will grow over time as users write files. As a result, it is totally normal for the net free counter (sinfo->reclaimable_bytes) to just sit at some huge negative gigabytes number and never come close to 0 much less a whole chunk. The *reason* I used net in the first place is that the reclaims themselves free space which tricked a gross counter. But I think net freeing is simply the wrong signal (we can net free 0 while producing fragmentation) so gross freeing is the signal the fs state could be worth sweeping and still prevent terrible loops (you do need to gross free still..) and the reclaiming attempts will be commensurate with freeing. To handle the frees from relocation itself, add a simple bg flag set while it is relocating and exclude those bgs from the reclaimablye_bytes counter update. This situation made it past the original testing of periodic reclaim because the original implementation had a signed arithmetic bug which left the gate always armed in the steady growth case. This was fixed by the patch 19eff93dc738 ("btrfs: fix periodic reclaim condition"), but left the current state of affairs where it never arms. So this might not be perfect in terms of its behavior in some weird edge case of a full filesystem constantly writing and deleting data, but it should be much better than the broken status quo. Fixes: 813d4c642251 ("btrfs: prevent pathological periodic reclaim loops") Signed-off-by: Boris Burkov <boris@bur.io> --- fs/btrfs/block-group.c | 8 +++----- fs/btrfs/block-group.h | 2 ++ fs/btrfs/relocation.c | 3 +++ fs/btrfs/space-info.c | 5 ++--- fs/btrfs/space-info.h | 8 +++++--- 5 files changed, 15 insertions(+), 11 deletions(-) diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c index 0736132624f8..dd13856c4b0d 100644 --- a/fs/btrfs/block-group.c +++ b/fs/btrfs/block-group.c @@ -3594,8 +3594,6 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans, space_info->bytes_reserved -= num_bytes; space_info->bytes_used += num_bytes; space_info->disk_used += num_bytes * factor; - if (READ_ONCE(space_info->periodic_reclaim)) - btrfs_space_info_update_reclaimable(space_info, -num_bytes); spin_unlock(&cache->lock); spin_unlock(&space_info->lock); } else { @@ -3606,10 +3604,10 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans, btrfs_space_info_update_bytes_pinned(space_info, num_bytes); space_info->bytes_used -= num_bytes; space_info->disk_used -= num_bytes * factor; - if (READ_ONCE(space_info->periodic_reclaim)) - btrfs_space_info_update_reclaimable(space_info, num_bytes); - else + if (!READ_ONCE(space_info->periodic_reclaim)) reclaim = should_reclaim_block_group(cache, num_bytes); + else if (!test_bit(BLOCK_GROUP_FLAG_RELOCATING, &cache->runtime_flags)) + btrfs_space_info_update_reclaimable(space_info, num_bytes); spin_unlock(&cache->lock); spin_unlock(&space_info->lock); diff --git a/fs/btrfs/block-group.h b/fs/btrfs/block-group.h index d567ed822e55..7acc82edc1f5 100644 --- a/fs/btrfs/block-group.h +++ b/fs/btrfs/block-group.h @@ -88,6 +88,8 @@ enum btrfs_block_group_flags { BLOCK_GROUP_FLAG_NEW, BLOCK_GROUP_FLAG_FULLY_REMAPPED, BLOCK_GROUP_FLAG_STRIPE_REMOVAL_PENDING, + /* Set while btrfs_relocate_block_group() is emptying the block group. */ + BLOCK_GROUP_FLAG_RELOCATING, }; enum btrfs_caching_type { diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c index b60d9a0c5c1d..7e8e7952f799 100644 --- a/fs/btrfs/relocation.c +++ b/fs/btrfs/relocation.c @@ -5503,6 +5503,7 @@ int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start, if (ret) goto out; bg_is_ro = true; + set_bit(BLOCK_GROUP_FLAG_RELOCATING, &rc->block_group->runtime_flags); path = btrfs_alloc_path(); if (!path) { @@ -5563,6 +5564,8 @@ int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start, } out: + if (bg_is_ro) + clear_bit(BLOCK_GROUP_FLAG_RELOCATING, &rc->block_group->runtime_flags); if (ret && bg_is_ro) btrfs_dec_block_group_ro(rc->block_group); if (!btrfs_fs_incompat(fs_info, REMAP_TREE)) diff --git a/fs/btrfs/space-info.c b/fs/btrfs/space-info.c index d5a84fa97462..325908da0c65 100644 --- a/fs/btrfs/space-info.c +++ b/fs/btrfs/space-info.c @@ -2174,15 +2174,14 @@ static bool do_reclaim_sweep(struct btrfs_space_info *space_info, int raid) return will_reclaim; } -void btrfs_space_info_update_reclaimable(struct btrfs_space_info *space_info, s64 bytes) +void btrfs_space_info_update_reclaimable(struct btrfs_space_info *space_info, u64 bytes) { u64 chunk_sz = calc_effective_data_chunk_size(space_info->fs_info); lockdep_assert_held(&space_info->lock); space_info->reclaimable_bytes += bytes; - if (space_info->reclaimable_bytes > 0 && - space_info->reclaimable_bytes >= chunk_sz) + if (space_info->reclaimable_bytes >= chunk_sz) btrfs_set_periodic_reclaim_ready(space_info, true); } diff --git a/fs/btrfs/space-info.h b/fs/btrfs/space-info.h index 0456b0d2d1b4..c4cc380edbc0 100644 --- a/fs/btrfs/space-info.h +++ b/fs/btrfs/space-info.h @@ -244,9 +244,11 @@ struct btrfs_space_info { bool periodic_reclaim_ready; /* - * Net bytes freed or allocated since the last reclaim pass. + * Bytes freed since periodic_reclaim_ready was last cleared, other + * than from block groups with BLOCK_GROUP_FLAG_RELOCATING set. + * Periodic reclaim can try again when this exceeds one chunk's worth. */ - s64 reclaimable_bytes; + u64 reclaimable_bytes; }; static inline bool btrfs_mixed_space_info(const struct btrfs_space_info *space_info) @@ -325,7 +327,7 @@ void btrfs_dump_space_info_for_trans_abort(struct btrfs_fs_info *fs_info); void btrfs_init_async_reclaim_work(struct btrfs_fs_info *fs_info); u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo); -void btrfs_space_info_update_reclaimable(struct btrfs_space_info *space_info, s64 bytes); +void btrfs_space_info_update_reclaimable(struct btrfs_space_info *space_info, u64 bytes); void btrfs_set_periodic_reclaim_ready(struct btrfs_space_info *space_info, bool ready); int btrfs_calc_reclaim_threshold(const struct btrfs_space_info *space_info); void btrfs_reclaim_sweep(const struct btrfs_fs_info *fs_info); -- 2.56.0 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 1/2] btrfs: arm periodic reclaim on bytes freed, not net bytes freed 2026-12-01 21:03 ` [PATCH 1/2] btrfs: arm periodic reclaim on bytes freed, not net bytes freed Boris Burkov @ 2026-12-03 17:23 ` Filipe Manana 0 siblings, 0 replies; 5+ messages in thread From: Filipe Manana @ 2026-12-03 17:23 UTC (permalink / raw) To: Boris Burkov; +Cc: linux-btrfs, kernel-team On Tue, Dec 1, 2026 at 9:04 PM Boris Burkov <boris@bur.io> wrote: > > Periodic reclaim is designed to avoid getting stuck in busy loops by > having a gate (sinfo->periodic_reclaim_ready) which is opened only when > we make forward progress or when we NET free a chunk. It also starts > shut when periodic reclaim is enabled in the first place. > > However, it is quite likely on many filesystems that they will grow over > time as users write files. As a result, it is totally normal for the net > free counter (sinfo->reclaimable_bytes) to just sit at some huge > negative gigabytes number and never come close to 0 much less a whole > chunk. > > The *reason* I used net in the first place is that the reclaims > themselves free space which tricked a gross counter. But I think net > freeing is simply the wrong signal (we can net free 0 while producing > fragmentation) so gross freeing is the signal the fs state could be > worth sweeping and still prevent terrible loops (you do need to gross > free still..) and the reclaiming attempts will be commensurate with > freeing. > > To handle the frees from relocation itself, add a simple bg flag set > while it is relocating and exclude those bgs from the reclaimablye_bytes typo: reclaimablye_bytes -> reclaimable_bytes Otherwise: Reviewed-by: Filipe Manana <fdmanana@suse.com> Thanks. > counter update. > > This situation made it past the original testing of periodic reclaim > because the original implementation had a signed arithmetic bug which > left the gate always armed in the steady growth case. This was fixed by > the patch 19eff93dc738 ("btrfs: fix periodic reclaim condition"), but > left the current state of affairs where it never arms. > > So this might not be perfect in terms of its behavior in some weird edge > case of a full filesystem constantly writing and deleting data, but it > should be much better than the broken status quo. > > Fixes: 813d4c642251 ("btrfs: prevent pathological periodic reclaim loops") > Signed-off-by: Boris Burkov <boris@bur.io> > --- > fs/btrfs/block-group.c | 8 +++----- > fs/btrfs/block-group.h | 2 ++ > fs/btrfs/relocation.c | 3 +++ > fs/btrfs/space-info.c | 5 ++--- > fs/btrfs/space-info.h | 8 +++++--- > 5 files changed, 15 insertions(+), 11 deletions(-) > > diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c > index 0736132624f8..dd13856c4b0d 100644 > --- a/fs/btrfs/block-group.c > +++ b/fs/btrfs/block-group.c > @@ -3594,8 +3594,6 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans, > space_info->bytes_reserved -= num_bytes; > space_info->bytes_used += num_bytes; > space_info->disk_used += num_bytes * factor; > - if (READ_ONCE(space_info->periodic_reclaim)) > - btrfs_space_info_update_reclaimable(space_info, -num_bytes); > spin_unlock(&cache->lock); > spin_unlock(&space_info->lock); > } else { > @@ -3606,10 +3604,10 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans, > btrfs_space_info_update_bytes_pinned(space_info, num_bytes); > space_info->bytes_used -= num_bytes; > space_info->disk_used -= num_bytes * factor; > - if (READ_ONCE(space_info->periodic_reclaim)) > - btrfs_space_info_update_reclaimable(space_info, num_bytes); > - else > + if (!READ_ONCE(space_info->periodic_reclaim)) > reclaim = should_reclaim_block_group(cache, num_bytes); > + else if (!test_bit(BLOCK_GROUP_FLAG_RELOCATING, &cache->runtime_flags)) > + btrfs_space_info_update_reclaimable(space_info, num_bytes); > > spin_unlock(&cache->lock); > spin_unlock(&space_info->lock); > diff --git a/fs/btrfs/block-group.h b/fs/btrfs/block-group.h > index d567ed822e55..7acc82edc1f5 100644 > --- a/fs/btrfs/block-group.h > +++ b/fs/btrfs/block-group.h > @@ -88,6 +88,8 @@ enum btrfs_block_group_flags { > BLOCK_GROUP_FLAG_NEW, > BLOCK_GROUP_FLAG_FULLY_REMAPPED, > BLOCK_GROUP_FLAG_STRIPE_REMOVAL_PENDING, > + /* Set while btrfs_relocate_block_group() is emptying the block group. */ > + BLOCK_GROUP_FLAG_RELOCATING, > }; > > enum btrfs_caching_type { > diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c > index b60d9a0c5c1d..7e8e7952f799 100644 > --- a/fs/btrfs/relocation.c > +++ b/fs/btrfs/relocation.c > @@ -5503,6 +5503,7 @@ int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start, > if (ret) > goto out; > bg_is_ro = true; > + set_bit(BLOCK_GROUP_FLAG_RELOCATING, &rc->block_group->runtime_flags); > > path = btrfs_alloc_path(); > if (!path) { > @@ -5563,6 +5564,8 @@ int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start, > } > > out: > + if (bg_is_ro) > + clear_bit(BLOCK_GROUP_FLAG_RELOCATING, &rc->block_group->runtime_flags); > if (ret && bg_is_ro) > btrfs_dec_block_group_ro(rc->block_group); > if (!btrfs_fs_incompat(fs_info, REMAP_TREE)) > diff --git a/fs/btrfs/space-info.c b/fs/btrfs/space-info.c > index d5a84fa97462..325908da0c65 100644 > --- a/fs/btrfs/space-info.c > +++ b/fs/btrfs/space-info.c > @@ -2174,15 +2174,14 @@ static bool do_reclaim_sweep(struct btrfs_space_info *space_info, int raid) > return will_reclaim; > } > > -void btrfs_space_info_update_reclaimable(struct btrfs_space_info *space_info, s64 bytes) > +void btrfs_space_info_update_reclaimable(struct btrfs_space_info *space_info, u64 bytes) > { > u64 chunk_sz = calc_effective_data_chunk_size(space_info->fs_info); > > lockdep_assert_held(&space_info->lock); > space_info->reclaimable_bytes += bytes; > > - if (space_info->reclaimable_bytes > 0 && > - space_info->reclaimable_bytes >= chunk_sz) > + if (space_info->reclaimable_bytes >= chunk_sz) > btrfs_set_periodic_reclaim_ready(space_info, true); > } > > diff --git a/fs/btrfs/space-info.h b/fs/btrfs/space-info.h > index 0456b0d2d1b4..c4cc380edbc0 100644 > --- a/fs/btrfs/space-info.h > +++ b/fs/btrfs/space-info.h > @@ -244,9 +244,11 @@ struct btrfs_space_info { > bool periodic_reclaim_ready; > > /* > - * Net bytes freed or allocated since the last reclaim pass. > + * Bytes freed since periodic_reclaim_ready was last cleared, other > + * than from block groups with BLOCK_GROUP_FLAG_RELOCATING set. > + * Periodic reclaim can try again when this exceeds one chunk's worth. > */ > - s64 reclaimable_bytes; > + u64 reclaimable_bytes; > }; > > static inline bool btrfs_mixed_space_info(const struct btrfs_space_info *space_info) > @@ -325,7 +327,7 @@ void btrfs_dump_space_info_for_trans_abort(struct btrfs_fs_info *fs_info); > void btrfs_init_async_reclaim_work(struct btrfs_fs_info *fs_info); > u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo); > > -void btrfs_space_info_update_reclaimable(struct btrfs_space_info *space_info, s64 bytes); > +void btrfs_space_info_update_reclaimable(struct btrfs_space_info *space_info, u64 bytes); > void btrfs_set_periodic_reclaim_ready(struct btrfs_space_info *space_info, bool ready); > int btrfs_calc_reclaim_threshold(const struct btrfs_space_info *space_info); > void btrfs_reclaim_sweep(const struct btrfs_fs_info *fs_info); > -- > 2.56.0 > > ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 2/2] btrfs: arm periodic reclaim when it is enabled 2026-12-01 21:03 [PATCH 0/2] Minor fixes to periodic reclaim arming Boris Burkov 2026-12-01 21:03 ` [PATCH 1/2] btrfs: arm periodic reclaim on bytes freed, not net bytes freed Boris Burkov @ 2026-12-01 21:03 ` Boris Burkov 2026-12-03 17:24 ` Filipe Manana 1 sibling, 1 reply; 5+ messages in thread From: Boris Burkov @ 2026-12-01 21:03 UTC (permalink / raw) To: linux-btrfs, kernel-team periodic_reclaim_ready starts out clear and is only set by freeing space or making forward progress on relocation. The point of it is to avoid senselessly re-running relocation sweeps. Allowing one sweep after enablement to get past potentially horrible fragmentation (that might have gotten the user to want to enable reclaim) seems like a reasonable tradeoff. Signed-off-by: Boris Burkov <boris@bur.io> --- fs/btrfs/sysfs.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c index 89b0a4ef4063..51ad79220057 100644 --- a/fs/btrfs/sysfs.c +++ b/fs/btrfs/sysfs.c @@ -893,7 +893,10 @@ static ssize_t btrfs_sinfo_periodic_reclaim_store(struct kobject *kobj, if (periodic_reclaim < 0) return -EINVAL; + spin_lock(&space_info->lock); WRITE_ONCE(space_info->periodic_reclaim, periodic_reclaim != 0); + btrfs_set_periodic_reclaim_ready(space_info, true); + spin_unlock(&space_info->lock); return len; } -- 2.56.0 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 2/2] btrfs: arm periodic reclaim when it is enabled 2026-12-01 21:03 ` [PATCH 2/2] btrfs: arm periodic reclaim when it is enabled Boris Burkov @ 2026-12-03 17:24 ` Filipe Manana 0 siblings, 0 replies; 5+ messages in thread From: Filipe Manana @ 2026-12-03 17:24 UTC (permalink / raw) To: Boris Burkov; +Cc: linux-btrfs, kernel-team On Tue, Dec 1, 2026 at 9:04 PM Boris Burkov <boris@bur.io> wrote: > > periodic_reclaim_ready starts out clear and is only set by freeing > space or making forward progress on relocation. The point of it is to > avoid senselessly re-running relocation sweeps. Allowing one sweep after > enablement to get past potentially horrible fragmentation (that might > have gotten the user to want to enable reclaim) seems like a reasonable > tradeoff. > > Signed-off-by: Boris Burkov <boris@bur.io> Reviewed-by: Filipe Manana <fdmanana@suse.com> Thanks. > --- > fs/btrfs/sysfs.c | 3 +++ > 1 file changed, 3 insertions(+) > > diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c > index 89b0a4ef4063..51ad79220057 100644 > --- a/fs/btrfs/sysfs.c > +++ b/fs/btrfs/sysfs.c > @@ -893,7 +893,10 @@ static ssize_t btrfs_sinfo_periodic_reclaim_store(struct kobject *kobj, > if (periodic_reclaim < 0) > return -EINVAL; > > + spin_lock(&space_info->lock); > WRITE_ONCE(space_info->periodic_reclaim, periodic_reclaim != 0); > + btrfs_set_periodic_reclaim_ready(space_info, true); > + spin_unlock(&space_info->lock); > > return len; > } > -- > 2.56.0 > > ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-03 17:25 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-12-01 21:03 [PATCH 0/2] Minor fixes to periodic reclaim arming Boris Burkov 2026-12-01 21:03 ` [PATCH 1/2] btrfs: arm periodic reclaim on bytes freed, not net bytes freed Boris Burkov 2026-12-03 17:23 ` Filipe Manana 2026-12-01 21:03 ` [PATCH 2/2] btrfs: arm periodic reclaim when it is enabled Boris Burkov 2026-12-03 17:24 ` Filipe Manana
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox