Skip to content

Commit 1ba72d8

Browse files
bmaurermorbidrsa
authored andcommitted
btrfs: use lockless read in nr_cached_objects shrinker callback
Under heavy memcg-driven slab reclaim with many memcgs and CPUs, shrink_slab_memcg() invokes the per-superblock count callback once per (memcg, NUMA node) tuple. For btrfs that callback reaches percpu_counter_sum_positive() on fs_info->evictable_extent_maps, which takes the percpu_counter's raw spinlock with IRQs disabled and walks every online CPU. With hundreds of memcgs driving reclaim on a host with dozens of CPUs, this counter lock becomes a global serialization point: profiles show CPU pinned in the spin_lock_irqsave acquire under __percpu_counter_sum, with cross-CPU IPIs hitting csd_lock_wait_toolong while waiting for spinning vCPUs. The shrinker count is advisory -- super_cache_count() already notes "counts can change between super_cache_count and super_cache_scan, so we really don't need locks here." Use percpu_counter_read_positive(), which is lockless. Worst-case skew is bounded by batch * num_online_cpus (a few thousand), negligible compared to the millions of extent maps a busy filesystem accumulates and well within the noise that the shrinker already tolerates. Tested-by: Boris Burkov <boris@bur.io> Reviewed-by: Qu Wenruo <wqu@suse.com> Reviewed-by: Shakeel Butt <shakeel.butt@linux.dev> Signed-off-by: Ben Maurer <bmaurer@meta.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 79bdd88 commit 1ba72d8

1 file changed

Lines changed: 1 addition & 1 deletion

File tree

fs/btrfs/super.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2432,7 +2432,7 @@ static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
24322432
static long btrfs_nr_cached_objects(struct super_block *sb, struct shrink_control *sc)
24332433
{
24342434
struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2435-
const s64 nr = percpu_counter_sum_positive(&fs_info->evictable_extent_maps);
2435+
const s64 nr = percpu_counter_read_positive(&fs_info->evictable_extent_maps);
24362436

24372437
trace_btrfs_extent_map_shrinker_count(fs_info, nr);
24382438

0 commit comments

Comments
 (0)