f2fs: fix to use bio_in_atomic() in f2fs_write_end_io()
Chao Yu
maintainerPATCH 1/2 f2fs: fix to use bio_in_atomic() in f2fs_write_end_io()
From: Chao Yu <[email protected]>
f2fs_write_end_io() uses in_atomic() to decide whether a write bio which exceeds max_atc_write_bio_size or max_atc_write_bio_entry_cnt should be completed in sbi->wq rather than in the current context.
However, in_atomic() is not meant to be used outside core kernel code (checkpatch.pl warns about it), as it only looks at preempt_count(): it misses irqs-disabled and RCU read-side critical sections, and on kernels without CONFIG_PREEMPT_COUNT it cannot see spinlock-held sections at all. In those contexts, a large write bio will still be completed inline, which defeats the purpose of the above knobs.
The block layer provides bio_in_atomic() for exactly this purpose, it checks rcu_preempt_depth() and preemptible(), and conservatively returns true if CONFIG_PREEMPT_COUNT is disabled, let's use it instead. The difference is as below:
Context | in_atomic() | bio_in_atomic()
-------------------------------------+----------------+----------------
Hard IRQ / softirq | true | true IRQs disabled, preempt count 0 | false (missed) | true RCU read lock (CONFIG_PREEMPTION) | false (missed) | true Kernel w/o CONFIG_PREEMPT_COUNT | IRQ ctx only | always true
Note that on kernels without CONFIG_PREEMPT_COUNT, write bios exceeding the thresholds will always be offloaded to sbi->wq, since there is no way to tell whether the current context is atomic. The thresholds default to UINT_MAX, so the default behavior is not changed.
--- fs/f2fs/data.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
fs/f2fs/data.c +3 -2
@@ -398,8 +398,9 @@ static void f2fs_write_end_io(struct bio *bio) sbi = bio->bi_private; -if (in_atomic () && (bio->bi_iter.bi_size > sbi->max_atc_write_bio_size || -F2FS_BIO (bio)->entry_cnt > sbi->max_atc_write_bio_entry_cnt)) { +if (bio_in_atomic () && + (bio->bi_iter.bi_size > sbi->max_atc_write_bio_size || +F2FS_BIO (bio)->entry_cnt > sbi->max_atc_write_bio_entry_cnt)) {struct work_struct *w; w = &container_of(bio, struct f2fs_bio, bio)->work;
Signature · 1 line
2.49.0
Chao Yu
maintainerPATCH 2/2 f2fs: fix to use bio_in_atomic() in f2fs_read_end_io()
From: Chao Yu <[email protected]>
f2fs_read_end_io() uses "in_task() && !irqs_disabled()" to decide whether it is safe to sleep, the result is passed down as @in_task to:
- f2fs_put_dic() -> f2fs_free_dic() -> f2fs_release_decomp_mem() -> vm_unmap_ram(), which may sleep.
- f2fs_end_read_compressed_page() -> f2fs_cache_compressed_page() -> f2fs_grab_cache() -> f2fs_lock_cache(), which may sleep in wait_on_bit_lock().
However, the check still treats below task contexts as sleepable:
- with spinlock held via spin_lock(), i.e. preempt count != 0 while irqs are enabled.
- with BH disabled via local_bh_disable().
- inside an RCU read lock on kernels with CONFIG_PREEMPTION.
- with any spinlock held on kernels without CONFIG_PREEMPT_COUNT.
So the "sleeping function called from invalid context" issue fixed by commit 08a7efc5b02a ("f2fs: vm_unmap_ram() may be called from an invalid context") can still be triggered.
The block layer provides bio_in_atomic() for exactly this purpose, it checks rcu_preempt_depth() and preemptible(), and conservatively returns true if CONFIG_PREEMPT_COUNT is disabled, let's use it instead. The difference is as below:
old: intask = in_task() && !irqs_disabled()
new: intask = !bio_in_atomic()
Context | old intask | new intask
-------------------------------------+----------------+----------------
Hard IRQ / softirq | false | false IRQs disabled | false | false spin_lock() held, IRQs enabled | true (wrong) | false BH disabled by local_bh_disable() | true (wrong) | false RCU read lock (CONFIG_PREEMPTION) | true (wrong) | false Spinlock held, w/o PREEMPT_COUNT | true (wrong) | false
Note that on kernels without CONFIG_PREEMPT_COUNT, bio_in_atomic() always returns true, so dic freeing will always be offloaded to sbi->wq, and compressed pages will not be cached in f2fs_read_end_io(); this is the price of correctness, since there is no way to tell whether the current context can sleep.
--- fs/f2fs/data.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/f2fs/data.c +1 -1
@@ -271,7 +271,7 @@ static void f2fs_read_end_io(struct bio *bio) {struct f2fs_sb_info *sbi =F2FS_F_SB (bio_first_folio_all (bio));struct bio_post_read_ctx *ctx; -bool intask =in_task () && !irqs_disabled (); +bool intask = !bio_in_atomic ();iostat_update_and_unbind_ctx (bio); ctx = bio->bi_private;
Signature · 1 line
2.49.0
This series was applied to jaegeuk/f2fs.git (dev) by Jaegeuk Kim <[email protected]>:
Hello:
This series was applied to jaegeuk/f2fs.git (dev)
by Jaegeuk Kim <[email protected]>:
Quoted 6 lines · On Sat, 10 Oct 2026 14:55:45 +0800 you wrote:
From: Chao Yu <[email protected]>
f2fs_write_end_io()usesin_atomic()to decide whether a write bio which exceedsmax_atc_write_bio_sizeormax_atc_write_bio_entry_cntshould be completed insbi->wqrather than in the current context.
Quoted 8 lines
However,
in_atomic()is not meant to be used outside core kernel code (checkpatch.pl warns about it), as it only looks atpreempt_count(): it misses irqs-disabled and RCU read-side critical sections, and on kernels withoutCONFIG_PREEMPT_COUNTit cannot see spinlock-held sections at all. In those contexts, a large write bio will still be completed inline, which defeats the purpose of the above knobs.[...]
Here is the summary with links:
- [f2fs-dev,1/2] f2fs: fix to use bio_in_atomic() in f2fs_write_end_io()
https://git.kernel.org/jaegeuk/f2fs/c/e4582e5f3b43 - [f2fs-dev,2/2] f2fs: fix to use bio_in_atomic() in f2fs_read_end_io() https://git.kernel.org/jaegeuk/f2fs/c/a605d5d71a6d
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html