f2fs: annotate lockless last_time[] accesses
PATCH 1/1 f2fs: annotate lockless last_time[] accesses
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.
--- fs/f2fs/f2fs.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
fs/f2fs/f2fs.h +5 -5
@@ -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);
Signature · 1 line
2.43.0
Quoted 10 lines · Cen Zhang
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.
Quoted 5 lines
Use
WRITE_ONCE()inf2fs_update_time()andREAD_ONCE()inf2fs_time_over()andf2fs_time_to_wait(). This preserves the existing heuristic behavior and avoids adding locking to hot paths.Signed-off-by Cen Zhang <[email protected]>
Thanks,
This patch was applied to jaegeuk/f2fs.git (dev) by Jaegeuk Kim <[email protected]>:
Hello:
This patch was applied to jaegeuk/f2fs.git (dev)
by Jaegeuk Kim <[email protected]>:
Quoted 11 lines · On Wed, 6 May 2026 09:07:09 +0800 you wrote:
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.
[...]
Here is the summary with links:
- [f2fs-dev] f2fs: annotate lockless last_time[] accesses
https://git.kernel.org/jaegeuk/f2fs/c/c4bbbc96e530
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html