The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] btrfs: skip the extent map tree lock for inodes without extent maps
@ 2026-08-21 10:38 Breno Leitao
  2026-08-21 11:13 ` Qu Wenruo
  2026-08-21 11:17 ` Filipe Manana
  0 siblings, 2 replies; 6+ messages in thread
From: Breno Leitao @ 2026-08-21 10:38 UTC (permalink / raw)
  To: Chris Mason, David Sterba, fdmanana
  Cc: boris, wqu, layton, linux-btrfs, linux-kernel, kernel-team,
	Breno Leitao

The shrinker (find_first_inode_to_shrink()) takes inode->extent_tree.lock in write
mode on every inode it walks, only to find out whether that inode has
any extent maps.

Most have none, from what I understand, so, avoid the lock  by testing the
tree with a plain read before taking the lock. tree->root is only
modified with the tree lock held for write, so the unlocked read is
a benign race: a false negative just defers the inode to a later scan.

On my tests, find_first_inode_to_shrink() was a bit faster, so, if this
patch is correct, I think it is worth having to reduce lock contention.

Signed-off-by: Breno Leitao <leitao@debian.org>
---
 fs/btrfs/extent_map.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/fs/btrfs/extent_map.c b/fs/btrfs/extent_map.c
index 6ad7b39ae358b..cdcd2b779050d 100644
--- a/fs/btrfs/extent_map.c
+++ b/fs/btrfs/extent_map.c
@@ -1219,6 +1219,9 @@ static struct btrfs_inode *find_first_inode_to_shrink(struct btrfs_root *root,
 
 		tree = &inode->extent_tree;
 
+		if (!READ_ONCE(tree->root.rb_node))
+			goto next;
+
 		/*
 		 * We want to be fast so if the lock is busy we don't want to
 		 * spend time waiting for it (some task is about to do IO for

---
base-commit: 6a746cd265aed59107ebdaa9ce039bb832922969
change-id: 20260820-b4-btrfs-em-shrinker-7382d7f0dd05

Best regards,
--  
Breno Leitao <leitao@debian.org>


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

end of thread, other threads:[~2026-08-21 18:18 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-21 10:38 [PATCH] btrfs: skip the extent map tree lock for inodes without extent maps Breno Leitao
2026-08-21 11:13 ` Qu Wenruo
2026-08-21 16:34   ` Breno Leitao
2026-08-21 11:17 ` Filipe Manana
2026-08-21 16:07   ` Breno Leitao
2026-08-21 18:18     ` Filipe Manana

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox