f2fs: protect gc_thread lifetime for published users
PATCH 1/1 f2fs: protect gc_thread lifetime for published users
f2fs publishes the GC worker object through sbi->gc_thread. Several paths use that published pointer after only checking that it is non-NULL:
foreground GC merge waits on the worker waitqueues, sysfs reads and writes access GC worker tunables, and shutdown/remount/unmount paths stop the worker through f2fs_stop_gc_thread().
Those users do not all share the same exclusion. In particular, the dead-device shutdown path reaches f2fs_shutdown() under shared s_umount and then calls f2fs_do_shutdown(..., false), while ordinary filesystem paths and sysfs callbacks can still have borrowed the published worker pointer. f2fs_stop_gc_thread() also used to free the worker before clearing sbi->gc_thread, so a second stopper could observe the same object.
The buggy scenarios are all variants of the same lifetime problem:
f2fs_balance_fs() reader: dead-device shutdown: 1. sees GC_MERGE 1. enters f2fs_shutdown() 2. reads sbi->gc_thread 2. calls f2fs_stop_gc_thread() 3. waits on gc_th->fggc_wq 3. stops gc_th->f2fs_gc_task 4. resumes in finish_wait() 4. frees gc_th
GC-thread sysfs callback: dead-device shutdown:
1. enters f2fs_attr_show() 1. enters f2fs_shutdown()
or f2fs_sbi_store()
2. reaches f2fs_sbi_show() 2. calls f2fs_stop_gc_thread()
or __sbi_store() 3. reads sbi->gc_thread 3. stops and frees gc_th
4. dereferences gc_thread fields
or updates a GC tunable
first stopper: second stopper: 1. enters f2fs_stop_gc_thread() 1. enters f2fs_stop_gc_thread() 2. reads sbi->gc_thread 2. reads the same sbi->gc_thread 3. stops gc_th->f2fs_gc_task 3. resumes after the first free 4. frees gc_th 4. dereferences or frees gc_th again
5. clears sbi->gc_thread
The same underlying lifetime gap was also reproduced through GC-thread sysfs show/store paths and through competing f2fs_stop_gc_thread() callers, so handle all published GC worker users with one lifetime rule instead of sending narrower fixes for each symptom.
Add a small lifetime rule for the published GC worker. Protect the published pointer with a dedicated rwsem, let readers acquire a refcounted reference before dereferencing it, clear the published pointer before stopping the worker, and wait for all borrowed references before freeing the object. Route the waitqueue, scalar, and sysfs users that dereference the GC worker through the same borrow/release helpers.
Sanitizer validation reported:
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/gc.c:197) f2fs_fill_super() (fs/f2fs/super.c:4975)
---
fs/f2fs/f2fs.h +1 -0
@@ -1873,6 +1873,7 @@ struct f2fs_sb_info {* semaphore for GC, avoid * race between GC and GC or CP */ +struct f2fs_rwsem gc_thread_lock;/* protects gc_thread pointer */ struct f2fs_gc_kthread *gc_thread;/* GC thread */ struct atgc_management am;/* atgc management */ unsigned int cur_victim_sec;/* current victim section num */
fs/f2fs/gc.c +79 -23
173 lines not on this page · raw message
fs/f2fs/gc.h +11 -3
@@ -1,4 +1,5 @@/* SPDX-License-Identifier: GPL-2.0 */ +#include <linux/refcount.h> /* * fs/f2fs/gc.h *
@@ -46,6 +47,9 @@#define NR_GC_CHECKPOINT_SECS (3)/* data/node/dentry sections */ struct f2fs_gc_kthread { +struct f2fs_sb_info *sbi; +refcount_t refcnt; +struct completion refcnt_completion;struct task_struct *f2fs_gc_task;wait_queue_head_t gc_wait_queue_head;
@@ -84,6 +88,10 @@ struct victim_entry {struct list_head list; }; +bool f2fs_get_gc_thread (struct f2fs_sb_info *sbi, +struct f2fs_gc_kthread **gc_thread); +void f2fs_put_gc_thread (struct f2fs_gc_kthread *gc_th); +/* * inline functions */
@@ -193,10 +201,10 @@ 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); +return !has_enough_free_blocks (sbi, gc_th->boost_zoned_gc_percent);return has_enough_invalid_blocks (sbi); }
fs/f2fs/segment.c +11 -6
@@ -424,6 +424,8 @@ int f2fs_commit_atomic_write(struct inode *inode)*/ void f2fs_balance_fs (struct f2fs_sb_info *sbi,bool need) { +struct f2fs_gc_kthread *gc_th =NULL ; +if (f2fs_cp_error (sbi))return ;
@@ -444,15 +446,16 @@ 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) { +if (test_opt (sbi,GC_MERGE ) && +f2fs_get_gc_thread (sbi, &gc_th) && + gc_th->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); +prepare_to_wait (&gc_th->fggc_wq, &wait,TASK_UNINTERRUPTIBLE ); +wake_up (&gc_th->gc_wait_queue_head);io_schedule (); -finish_wait (&sbi->gc_thread->fggc_wq, &wait); +finish_wait (&gc_th->fggc_wq, &wait); +f2fs_put_gc_thread (gc_th); }else {struct f2fs_gc_control gc_control = { .victim_segno =NULL_SEGNO ,
@@ -462,6 +465,8 @@ void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need) .should_migrate_blocks = false, .err_gc_skipped = false, .nr_free_secs = 1 }; +if (gc_th) +f2fs_put_gc_thread (gc_th);f2fs_down_write_trace (&sbi->gc_lock, &gc_control.lc);stat_inc_gc_call_count (sbi,FOREGROUND );f2fs_gc (sbi, &gc_control);
fs/f2fs/super.c +1 -0
@@ -4973,6 +4973,7 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)/* initialize locks within allocated memory */ init_f2fs_rwsem_trace (&sbi->gc_lock, sbi,LOCK_NAME_GC_LOCK ); +init_f2fs_rwsem (&sbi->gc_thread_lock);mutex_init (&sbi->writepages);init_f2fs_rwsem_trace (&sbi->cp_global_sem, sbi,LOCK_NAME_CP_GLOBAL );#ifdef CONFIG_DEBUG_LOCK_ALLOC
fs/f2fs/sysfs.c +21 -5
@@ -981,6 +981,8 @@ static ssize_t __sbi_store(struct f2fs_attr *a, }if (!strcmp (a->attr.name,"critical_task_priority" )) { +struct f2fs_gc_kthread *gc_th; +if (t <NICE_TO_PRIO (MIN_NICE ) || t >NICE_TO_PRIO (MAX_NICE ))return -EINVAL ;if (!capable (CAP_SYS_NICE ))
@@ -989,9 +991,12 @@ static ssize_t __sbi_store(struct f2fs_attr *a,if (sbi->cprc_info.f2fs_issue_ckpt)set_user_nice (sbi->cprc_info.f2fs_issue_ckpt,PRIO_TO_NICE (sbi->critical_task_priority)); -if (sbi->gc_thread && sbi->gc_thread->f2fs_gc_task) -set_user_nice (sbi->gc_thread->f2fs_gc_task, -PRIO_TO_NICE (sbi->critical_task_priority)); +if (f2fs_get_gc_thread (sbi, &gc_th)) { +if (gc_th->f2fs_gc_task) +set_user_nice (gc_th->f2fs_gc_task, +PRIO_TO_NICE (sbi->critical_task_priority)); +f2fs_put_gc_thread (gc_th); + }return count; }
@@ -1011,10 +1016,13 @@ static ssize_t f2fs_sbi_store(struct f2fs_attr *a,if (gc_entry) {if (!down_read_trylock (&sbi->sb->s_umount))return -EAGAIN ; +f2fs_down_read (&sbi->gc_thread_lock); } ret =__sbi_store (a, sbi, buf, count); -if (gc_entry) +if (gc_entry) { +f2fs_up_read (&sbi->gc_thread_lock);up_read (&sbi->sb->s_umount); + }return ret; }
@@ -1025,8 +1033,16 @@ static ssize_t f2fs_attr_show(struct kobject *kobj, struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info, s_kobj); struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr); +ssize_t ret; -return a->show ? a->show (a, sbi, buf) :0 ; +if (a->struct_type !=GC_THREAD ) +return a->show ? a->show (a, sbi, buf) :0 ; + +f2fs_down_read (&sbi->gc_thread_lock); + ret = a->show ? a->show (a, sbi, buf) :0 ; +f2fs_up_read (&sbi->gc_thread_lock); + +return ret; } static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,