Skip to content

Commit 496bb99

Browse files
Zqiangtytso
authored andcommitted
ext4: fix the might_sleep() warnings in kvfree()
Use the kvfree() in the RCU read critical section can trigger the following warnings: EXT4-fs (vdb): unmounting filesystem cd983e5b-3c83-4f5a-a136-17b00eb9d018. WARNING: suspicious RCU usage ./include/linux/rcupdate.h:409 Illegal context switch in RCU read-side critical section! other info that might help us debug this: rcu_scheduler_active = 2, debug_locks = 1 Call Trace: <TASK> dump_stack_lvl+0xbb/0xd0 dump_stack+0x14/0x20 lockdep_rcu_suspicious+0x15a/0x1b0 __might_resched+0x375/0x4d0 ? put_object.part.0+0x2c/0x50 __might_sleep+0x108/0x160 vfree+0x58/0x910 ? ext4_group_desc_free+0x27/0x270 kvfree+0x23/0x40 ext4_group_desc_free+0x111/0x270 ext4_put_super+0x3c8/0xd40 generic_shutdown_super+0x14c/0x4a0 ? __pfx_shrinker_free+0x10/0x10 kill_block_super+0x40/0x90 ext4_kill_sb+0x6d/0xb0 deactivate_locked_super+0xb4/0x180 deactivate_super+0x7e/0xa0 cleanup_mnt+0x296/0x3e0 __cleanup_mnt+0x16/0x20 task_work_run+0x157/0x250 ? __pfx_task_work_run+0x10/0x10 ? exit_to_user_mode_loop+0x6a/0x550 exit_to_user_mode_loop+0x102/0x550 do_syscall_64+0x44a/0x500 entry_SYSCALL_64_after_hwframe+0x77/0x7f </TASK> BUG: sleeping function called from invalid context at mm/vmalloc.c:3441 in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 556, name: umount preempt_count: 1, expected: 0 CPU: 3 UID: 0 PID: 556 Comm: umount Call Trace: <TASK> dump_stack_lvl+0xbb/0xd0 dump_stack+0x14/0x20 __might_resched+0x275/0x4d0 ? put_object.part.0+0x2c/0x50 __might_sleep+0x108/0x160 vfree+0x58/0x910 ? ext4_group_desc_free+0x27/0x270 kvfree+0x23/0x40 ext4_group_desc_free+0x111/0x270 ext4_put_super+0x3c8/0xd40 generic_shutdown_super+0x14c/0x4a0 ? __pfx_shrinker_free+0x10/0x10 kill_block_super+0x40/0x90 ext4_kill_sb+0x6d/0xb0 deactivate_locked_super+0xb4/0x180 deactivate_super+0x7e/0xa0 cleanup_mnt+0x296/0x3e0 __cleanup_mnt+0x16/0x20 task_work_run+0x157/0x250 ? __pfx_task_work_run+0x10/0x10 ? exit_to_user_mode_loop+0x6a/0x550 exit_to_user_mode_loop+0x102/0x550 do_syscall_64+0x44a/0x500 entry_SYSCALL_64_after_hwframe+0x77/0x7f The above scenarios occur in initialization failures and teardown paths, there are no parallel operations on the resources released by kvfree(), this commit therefore remove rcu_read_lock/unlock() and use rcu_access_pointer() instead of rcu_dereference() operations. Fixes: 7c99072 ("ext4: fix potential race between s_flex_groups online resizing and access") Fixes: df3da4e ("ext4: fix potential race between s_group_info online resizing and access") Signed-off-by: Zqiang <qiang.zhang@linux.dev> Reviewed-by: Baokun Li <libaokun@linux.alibaba.com> Link: https://patch.msgid.link/20260319094545.19291-1-qiang.zhang@linux.dev Signed-off-by: Theodore Ts'o <tytso@mit.edu> Cc: stable@kernel.org
1 parent 3822743 commit 496bb99

2 files changed

Lines changed: 5 additions & 13 deletions

File tree

fs/ext4/mballoc.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3584,9 +3584,7 @@ static int ext4_mb_init_backend(struct super_block *sb)
35843584
rcu_read_unlock();
35853585
iput(sbi->s_buddy_cache);
35863586
err_freesgi:
3587-
rcu_read_lock();
3588-
kvfree(rcu_dereference(sbi->s_group_info));
3589-
rcu_read_unlock();
3587+
kvfree(rcu_access_pointer(sbi->s_group_info));
35903588
return -ENOMEM;
35913589
}
35923590

@@ -3901,7 +3899,8 @@ void ext4_mb_release(struct super_block *sb)
39013899
WARN_ON_ONCE(!list_empty(&sbi->s_discard_list));
39023900
}
39033901

3904-
if (sbi->s_group_info) {
3902+
group_info = rcu_access_pointer(sbi->s_group_info);
3903+
if (group_info) {
39053904
for (i = 0; i < ngroups; i++) {
39063905
cond_resched();
39073906
grinfo = ext4_get_group_info(sb, i);
@@ -3919,12 +3918,9 @@ void ext4_mb_release(struct super_block *sb)
39193918
num_meta_group_infos = (ngroups +
39203919
EXT4_DESC_PER_BLOCK(sb) - 1) >>
39213920
EXT4_DESC_PER_BLOCK_BITS(sb);
3922-
rcu_read_lock();
3923-
group_info = rcu_dereference(sbi->s_group_info);
39243921
for (i = 0; i < num_meta_group_infos; i++)
39253922
kfree(group_info[i]);
39263923
kvfree(group_info);
3927-
rcu_read_unlock();
39283924
}
39293925
ext4_mb_avg_fragment_size_destroy(sbi);
39303926
ext4_mb_largest_free_orders_destroy(sbi);

fs/ext4/super.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,27 +1254,23 @@ static void ext4_group_desc_free(struct ext4_sb_info *sbi)
12541254
struct buffer_head **group_desc;
12551255
int i;
12561256

1257-
rcu_read_lock();
1258-
group_desc = rcu_dereference(sbi->s_group_desc);
1257+
group_desc = rcu_access_pointer(sbi->s_group_desc);
12591258
for (i = 0; i < sbi->s_gdb_count; i++)
12601259
brelse(group_desc[i]);
12611260
kvfree(group_desc);
1262-
rcu_read_unlock();
12631261
}
12641262

12651263
static void ext4_flex_groups_free(struct ext4_sb_info *sbi)
12661264
{
12671265
struct flex_groups **flex_groups;
12681266
int i;
12691267

1270-
rcu_read_lock();
1271-
flex_groups = rcu_dereference(sbi->s_flex_groups);
1268+
flex_groups = rcu_access_pointer(sbi->s_flex_groups);
12721269
if (flex_groups) {
12731270
for (i = 0; i < sbi->s_flex_groups_allocated; i++)
12741271
kvfree(flex_groups[i]);
12751272
kvfree(flex_groups);
12761273
}
1277-
rcu_read_unlock();
12781274
}
12791275

12801276
static void ext4_put_super(struct super_block *sb)

0 commit comments

Comments
 (0)