]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
f2fs: annotate lockless NAT counter reads
authorCen Zhang <zzzccc427@gmail.com>
Tue, 5 May 2026 12:55:10 +0000 (20:55 +0800)
committerJaegeuk Kim <jaegeuk@kernel.org>
Mon, 22 Jun 2026 19:52:34 +0000 (19:52 +0000)
nat_cnt[] is updated while callers hold nat_tree_lock, but F2FS samples
the counters locklessly in f2fs_available_free_memory(),
excess_dirty_nats(), and excess_cached_nats(). Those helpers only steer
cache reclaim and background sync heuristics; they do not control NAT
entry lifetime or checkpoint correctness.

Document the intent with data_race(READ_ONCE()) and a short comment
instead of adding locking to the balance path.

Signed-off-by: Cen Zhang <zzzccc427@gmail.com>
Reviewed-by: Chao Yu <chao@kernel.org>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
fs/f2fs/node.c
fs/f2fs/node.h

index b1247de254115bf69a2113f98e7a842353c9c5c3..a464d5ee11244f4b81b288ea8d8721e62f50780b 100644 (file)
@@ -73,7 +73,11 @@ bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type)
                                sizeof(struct free_nid)) >> PAGE_SHIFT;
                res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
        } else if (type == NAT_ENTRIES) {
-               mem_size = (nm_i->nat_cnt[TOTAL_NAT] *
+               /*
+                * nat_cnt[] is heuristic accounting. Sample it locklessly here
+                * to avoid taking nat_tree_lock in the balance path.
+                */
+               mem_size = (data_race(READ_ONCE(nm_i->nat_cnt[TOTAL_NAT])) *
                                sizeof(struct nat_entry)) >> PAGE_SHIFT;
                res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
                if (excess_cached_nats(sbi))
index bcf2034e42639f53bbceb5ae290199416ee409da..5e114f3520998d3af010420c9991cca1a31a5a99 100644 (file)
@@ -129,13 +129,17 @@ static inline void raw_nat_from_node_info(struct f2fs_nat_entry *raw_ne,
 
 static inline bool excess_dirty_nats(struct f2fs_sb_info *sbi)
 {
-       return NM_I(sbi)->nat_cnt[DIRTY_NAT] >= NM_I(sbi)->max_nid *
+       /* nat_cnt[] is heuristic accounting sampled locklessly here. */
+       return data_race(READ_ONCE(NM_I(sbi)->nat_cnt[DIRTY_NAT])) >=
+                                       NM_I(sbi)->max_nid *
                                        NM_I(sbi)->dirty_nats_ratio / 100;
 }
 
 static inline bool excess_cached_nats(struct f2fs_sb_info *sbi)
 {
-       return NM_I(sbi)->nat_cnt[TOTAL_NAT] >= DEF_NAT_CACHE_THRESHOLD;
+       /* nat_cnt[] is heuristic accounting sampled locklessly here. */
+       return data_race(READ_ONCE(NM_I(sbi)->nat_cnt[TOTAL_NAT])) >=
+                                       DEF_NAT_CACHE_THRESHOLD;
 }
 
 enum mem_type {