[PATCH] f2fs: annotate lockless last_time[] accesses

From: Cen Zhang

Date: Tue May 05 2026 - 21:07:32 EST


f2fs stores mount-wide activity timestamps in sbi->last_time[] and
samples them from background discard, GC, and balance paths without a
dedicated lock. The timestamps are used as best-effort heuristics to
decide whether background work should run now or sleep a bit longer.

The current helpers use plain loads and stores, so KCSAN can report races
between frequent foreground updates and background readers. Exact
freshness is not required here, but the intentional lockless accesses
should be marked explicitly.

Use WRITE_ONCE() in f2fs_update_time() and READ_ONCE() in
f2fs_time_over() and f2fs_time_to_wait(). This preserves the existing
heuristic behavior and avoids adding locking to hot paths.

Signed-off-by: Cen Zhang <zzzccc427@xxxxxxxxx>
---
fs/f2fs/f2fs.h | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 65c0d20df3a4..f838acc2f7a0 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -2121,12 +2121,12 @@ static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
{
unsigned long now = jiffies;

- sbi->last_time[type] = now;
+ WRITE_ONCE(sbi->last_time[type], now);

/* DISCARD_TIME and GC_TIME are based on REQ_TIME */
if (type == REQ_TIME) {
- sbi->last_time[DISCARD_TIME] = now;
- sbi->last_time[GC_TIME] = now;
+ WRITE_ONCE(sbi->last_time[DISCARD_TIME], now);
+ WRITE_ONCE(sbi->last_time[GC_TIME], now);
}
}

@@ -2134,7 +2134,7 @@ static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
{
unsigned long interval = sbi->interval_time[type] * HZ;

- return time_after(jiffies, sbi->last_time[type] + interval);
+ return time_after(jiffies, READ_ONCE(sbi->last_time[type]) + interval);
}

static inline unsigned int f2fs_time_to_wait(struct f2fs_sb_info *sbi,
@@ -2144,7 +2144,7 @@ static inline unsigned int f2fs_time_to_wait(struct f2fs_sb_info *sbi,
unsigned int wait_ms = 0;
long delta;

- delta = (sbi->last_time[type] + interval) - jiffies;
+ delta = (READ_ONCE(sbi->last_time[type]) + interval) - jiffies;
if (delta > 0)
wait_ms = jiffies_to_msecs(delta);

--
2.43.0