f2fs: protect published gc_thread during teardown
PATCH v2 1/1 f2fs: protect published gc_thread during teardown
f2fs stores the background GC worker in a heap-allocated sbi->gc_thread. Foreground GC merge waiters, GC-thread sysfs callbacks, and victim- selection readers can still touch waitqueues, policy knobs, or f2fs_gc_task through that published object, but the dead-device shutdown path can reach f2fs_stop_gc_thread() under shared s_umount coverage and free it.
The buggy scenario involves two paths, with each column showing the order within that path:
f2fs_balance_fs() reader: dead-device shutdown: 1. sees low free space and 1. enters f2fs_shutdown() enters GC_MERGE 2. calls f2fs_do_shutdown(..., false) 2. borrows sbi->gc_thread 3. calls f2fs_stop_gc_thread() 3. queues on gc_th->fggc_wq 4. stops gc_th->f2fs_gc_task
4. wakes gc_th->gc_wait_queue_head 5. frees gc_th
5. sleeps in io_schedule() 6. clears sbi->gc_thread
6. resumes in finish_wait()
Fix this by making gc_thread a properly published object. Initialize it fully before publishing it, remove it from sbi->gc_thread with xchg() before teardown, keep f2fs_gc_task alive until an SRCU grace period has drained the borrowed readers, and route the waitqueue, scalar, and sysfs users through shared get/put helpers. Recheck f2fs_gc_task after prepare_to_wait() in f2fs_balance_fs() so teardown cannot miss an already-queued foreground waiter.
Validation reproduced this kernel report:
KASAN slab-use-after-free in _raw_spin_lock_irqsave()
Read of size 1 Call trace: dump_stack_lvl() (?:?) print_address_description() (mm/kasan/report.c:373) _raw_spin_lock_irqsave() (?:?) print_report() (?:?) __virt_addr_valid() (?:?) srso_alias_return_thunk() (arch/x86/include/asm/nospec-branch.h:375) kasan_addr_to_slab() (mm/kasan/common.c:45) kasan_report() (?:?) __kasan_check_byte() (mm/kasan/common.c:571) lock_acquire() (?:?) rcu_is_watching() (?:?) prepare_to_wait() (kernel/sched/wait.c:248) f2fs_balance_fs() (fs/f2fs/segment.c:448) f2fs_write_single_data_page() (fs/f2fs/f2fs.h:4184) do_raw_spin_lock() (?:?) find_held_lock() (kernel/locking/lockdep.c:5340) folio_mkclean() (?:?) f2fs_write_cache_pages() (fs/f2fs/data.c:3238) __lock_acquire() (kernel/locking/lockdep.c:5077) unwind_next_frame() (?:?) arch_stack_walk() (?:?) __f2fs_write_data_pages() (fs/f2fs/data.c:3565) validate_chain() (kernel/locking/lockdep.c:3861) insert_work() (kernel/workqueue.c:2220) do_writepages() (mm/page-writeback.c:2560) filemap_writeback() (mm/filemap.c:371) __queue_work() (kernel/workqueue.c:2275) file_write_and_wait_range() (?:?) f2fs_do_sync_file() (fs/f2fs/file.c:285) __lock_release() (kernel/locking/lockdep.c:5511) f2fs_sync_file() (fs/f2fs/f2fs.h:3781) do_fsync() (fs/sync.c:204) __x64_sys_fsync() (?:?) do_syscall_64() (arch/x86/entry/syscall_64.c:87) entry_SYSCALL_64_after_hwframe() (?:?) kasan_save_stack() (mm/kasan/common.c:52) kasan_save_track() (mm/kasan/common.c:74) __kasan_kmalloc() (?:?) f2fs_start_gc_thread() (fs/f2fs/f2fs.h:4203) f2fs_fill_super() (fs/f2fs/super.c:4993)
---
v2:
Sashiko.dev pointed out that the sysfs critical_task_priority path could use a stale f2fs_gc_task.
Sashiko.dev pointed out that the GC_MERGE foreground wait could miss teardown wakeups and sleep forever.
Sashiko.dev pointed out that holding gc_thread_lock across kthread_run() could deadlock with reclaim.
Sashiko.dev pointed out that the gc_urgent and gc_idle sysfs paths still touched GC state without safe lifetime protection.
While rechecking the mount retry path, I found that gc_thread_srcu_inited must be reset per f2fs_fill_super() attempt so cleanup only covers the current SRCU state.
fs/f2fs/f2fs.h | 26 ++++++++++- fs/f2fs/gc.c | 75 ++++++++++++++++++++---------- fs/f2fs/gc.h | 8 +++- fs/f2fs/segment.c | 34 ++++++++++---- fs/f2fs/super.c | 11 +++++ fs/f2fs/sysfs.c | 116 ++++++++++++++++++++++++++++++++++++---------- 6 files changed, 207 insertions(+), 63 deletions(-)
fs/f2fs/f2fs.h +25 -1
@@ -25,6 +25,7 @@#include <linux/quotaops.h> #include <linux/part_stat.h> #include <linux/rw_hint.h> +#include <linux/srcu.h> #include <linux/fscrypt.h> #include <linux/fsverity.h>
@@ -156,6 +157,8 @@ typedef u32 block_t; /**/ typedef u32 nid_t ; +struct f2fs_gc_kthread ; +#define COMPRESS_EXT_NUM 16enum blkzone_allocation_policy {
@@ -1873,7 +1876,8 @@ struct f2fs_sb_info {* semaphore for GC, avoid * race between GC and GC or CP */ -struct f2fs_gc_kthread *gc_thread;/* GC thread */ +struct f2fs_gc_kthread *gc_thread;/* published GC thread */ +struct srcu_struct gc_thread_srcu;/* protects gc_thread readers */ struct atgc_management am;/* atgc management */ unsigned int cur_victim_sec;/* current victim section num */ unsigned int gc_mode;/* current GC state */
@@ -4200,6 +4204,26 @@ extern const struct iomap_ops f2fs_iomap_ops;/* * gc.c */ +static inline struct f2fs_gc_kthread *f2fs_get_gc_thread ( +struct f2fs_sb_info *sbi,int *srcu_idx) +{ +struct f2fs_gc_kthread *gc_th; + + *srcu_idx =srcu_read_lock (&sbi->gc_thread_srcu); + gc_th =smp_load_acquire (&sbi->gc_thread); +if (!gc_th) { +srcu_read_unlock (&sbi->gc_thread_srcu, *srcu_idx); + *srcu_idx =-1 ; + } + +return gc_th; +} + +static inline void f2fs_put_gc_thread (struct f2fs_sb_info *sbi,int srcu_idx) +{ +srcu_read_unlock (&sbi->gc_thread_srcu, srcu_idx); +} +int f2fs_start_gc_thread (struct f2fs_sb_info *sbi);void f2fs_stop_gc_thread (struct f2fs_sb_info *sbi);block_t f2fs_start_bidx_of_node (unsigned int node_ofs,struct inode *inode);
fs/f2fs/gc.c +50 -25
128 lines not on this page · raw message
fs/f2fs/gc.h +6 -2
@@ -45,7 +45,10 @@#define NR_GC_CHECKPOINT_SECS (3)/* data/node/dentry sections */ +struct f2fs_sb_info ; +struct f2fs_gc_kthread { +struct f2fs_sb_info *sbi;struct task_struct *f2fs_gc_task;wait_queue_head_t gc_wait_queue_head;
@@ -193,10 +196,11 @@ static inline bool has_enough_invalid_blocks(struct f2fs_sb_info *sbi) limit_free_user_blocks(invalid_user_blocks)); } -static inline bool need_to_boost_gc (struct f2fs_sb_info *sbi) +static inline bool need_to_boost_gc (struct f2fs_sb_info *sbi, +struct f2fs_gc_kthread *gc_th) {if (f2fs_sb_has_blkzoned (sbi))return !has_enough_free_blocks (sbi, - sbi->gc_thread->boost_zoned_gc_percent); + gc_th->boost_zoned_gc_percent);return has_enough_invalid_blocks (sbi); }
fs/f2fs/segment.c +24 -10
@@ -444,16 +444,30 @@ void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need)if (has_enough_free_secs (sbi,0 ,0 ))return ; -if (test_opt (sbi,GC_MERGE ) && sbi->gc_thread && - sbi->gc_thread->f2fs_gc_task) { -DEFINE_WAIT (wait); - -prepare_to_wait (&sbi->gc_thread->fggc_wq, &wait, -TASK_UNINTERRUPTIBLE ); -wake_up (&sbi->gc_thread->gc_wait_queue_head); -io_schedule (); -finish_wait (&sbi->gc_thread->fggc_wq, &wait); - } else { +if (test_opt (sbi,GC_MERGE )) { +struct f2fs_gc_kthread *gc_th; +int srcu_idx; + + gc_th =f2fs_get_gc_thread (sbi, &srcu_idx); +if (gc_th) { +if (READ_ONCE (gc_th->f2fs_gc_task)) { +DEFINE_WAIT (wait); + +prepare_to_wait (&gc_th->fggc_wq, &wait, +TASK_UNINTERRUPTIBLE ); +if (READ_ONCE (gc_th->f2fs_gc_task)) { +wake_up (&gc_th->gc_wait_queue_head); +io_schedule (); + } +finish_wait (&gc_th->fggc_wq, &wait); +f2fs_put_gc_thread (sbi, srcu_idx); +return ; + } +f2fs_put_gc_thread (sbi, srcu_idx); + } + } + + { struct f2fs_gc_control gc_control = { .victim_segno = NULL_SEGNO, .init_gc_type = f2fs_sb_has_blkzoned(sbi) ?
fs/f2fs/super.c +11 -0
@@ -4954,11 +4954,13 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)int recovery, i, valid_super_block;struct curseg_info *seg_i;int retry_cnt =1 ; +bool gc_thread_srcu_inited = false;#ifdef CONFIG_QUOTA bool quota_enabled = false;#endif try_onemore: + gc_thread_srcu_inited = false; err = -EINVAL ; raw_super =NULL ; valid_super_block =-1 ;
@@ -4993,6 +4995,10 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)spin_lock_init (&sbi->inode_lock[i]); }mutex_init (&sbi->flush_lock); + err =init_srcu_struct (&sbi->gc_thread_srcu); +if (err) + goto free_sbi; + gc_thread_srcu_inited = true;/* set a block size */ if (unlikely (!sb_set_blocksize (sb,F2FS_BLKSIZE ))) {
@@ -5454,6 +5460,10 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)#ifdef CONFIG_DEBUG_LOCK_ALLOC lockdep_unregister_key (&sbi->cp_global_sem_key);#endif +if (gc_thread_srcu_inited) { +cleanup_srcu_struct (&sbi->gc_thread_srcu); + gc_thread_srcu_inited = false; + }kfree (sbi); sb->s_fs_info =NULL ;
@@ -5538,6 +5548,7 @@ static void kill_f2fs_super(struct super_block *sb)#ifdef CONFIG_DEBUG_LOCK_ALLOC lockdep_unregister_key (&sbi->cp_global_sem_key);#endif +cleanup_srcu_struct (&sbi->gc_thread_srcu);kfree (sbi); sb->s_fs_info =NULL ; }
fs/f2fs/sysfs.c +91 -25
195 lines not on this page · raw message
Signature · 1 line
2.43.0