linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] btrfs: skip reclaim if block_group is empty
@ 2022-10-10 18:49 Boris Burkov
  2022-10-11  9:43 ` Filipe Manana
  0 siblings, 1 reply; 4+ messages in thread
From: Boris Burkov @ 2022-10-10 18:49 UTC (permalink / raw)
  To: linux-btrfs, kernel-team

As we delete extents from a block group, at some deletion we cross below
the reclaim threshold. It is possible we are still in the middle of
deleting more extents and might soon hit 0. If that occurs, we would
leave the block group on the reclaim list, not in the care of unused
deletion or async discard.

It is pointless and wasteful to relocate empty block groups, so if we do
notice that case (we might not if the reclaim worker runs *before* we
finish emptying it), don't bother with relocating the block group.

Signed-off-by: Boris Burkov <boris@bur.io>
---
 fs/btrfs/block-group.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index 11fd52657b76..c3ea627d2457 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -1608,6 +1608,25 @@ void btrfs_reclaim_bgs_work(struct work_struct *work)
 			up_write(&space_info->groups_sem);
 			goto next;
 		}
+		if (bg->used == 0) {
+			/*
+			 * It is possible that we trigger relocation on a block
+			 * group as its extents are deleted and it first goes
+			 * below the threshold, then shortly goes empty. In that
+			 * case, we will do relocation, even though we could
+			 * more cheaply just delete the unused block group. Try
+			 * to catch that case here, though of course it is
+			 * possible there is a delete still coming the future,
+			 * so we can't avoid needless relocation of this sort
+			 * altogether. We can at least avoid relocating empty
+			 * block groups.
+			 */
+			if (!btrfs_test_opt(fs_info, DISCARD_ASYNC))
+				btrfs_mark_bg_unused(bg);
+			spin_unlock(&bg->lock);
+			up_write(&space_info->groups_sem);
+			goto next;
+		}
 		spin_unlock(&bg->lock);
 
 		/* Get out fast, in case we're unmounting the filesystem */
-- 
2.37.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-10-12  9:51 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-10-10 18:49 [PATCH] btrfs: skip reclaim if block_group is empty Boris Burkov
2022-10-11  9:43 ` Filipe Manana
2022-10-11 18:05   ` Boris Burkov
2022-10-12  9:51     ` Filipe Manana

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).