Skip to content

Commit 8ffc0cd

Browse files
Kemeng Shitytso
authored andcommitted
ext4: alloc test super block from sget
This fix the oops in ext4 unit test which is cuased by NULL sb.s_user_ns as following: <4>[ 14.344565] map_id_range_down (kernel/user_namespace.c:318) <4>[ 14.345378] make_kuid (kernel/user_namespace.c:415) <4>[ 14.345998] inode_init_always (include/linux/fs.h:1375 fs/inode.c:174) <4>[ 14.346696] alloc_inode (fs/inode.c:268) <4>[ 14.347353] new_inode_pseudo (fs/inode.c:1007) <4>[ 14.348016] new_inode (fs/inode.c:1033) <4>[ 14.348644] ext4_mb_init (fs/ext4/mballoc.c:3404 fs/ext4/mballoc.c:3719) <4>[ 14.349312] mbt_kunit_init (fs/ext4/mballoc-test.c:57 fs/ext4/mballoc-test.c:314) <4>[ 14.349983] kunit_try_run_case (lib/kunit/test.c:388 lib/kunit/test.c:443) <4>[ 14.350696] kunit_generic_run_threadfn_adapter (lib/kunit/try-catch.c:30) <4>[ 14.351530] kthread (kernel/kthread.c:388) <4>[ 14.352168] ret_from_fork (arch/arm64/kernel/entry.S:861) <0>[ 14.353385] Code: 52808004 b8236ae7 72be5e44 b90004c4 (38e368a1) Alloc test super block from sget to properly initialize test super block to fix the issue. Signed-off-by: Kemeng Shi <shikemeng@huaweicloud.com> Reported-by: Linux Kernel Functional Testing <lkft@linaro.org> Reported-by: Guenter Roeck <linux@roeck-us.net> Tested-by: Guenter Roeck <linux@roeck-us.net> Link: https://lore.kernel.org/r/20240304163543.6700-2-shikemeng@huaweicloud.com Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent d60c536 commit 8ffc0cd

File tree

1 file changed

+39
-16
lines changed

1 file changed

+39
-16
lines changed

fs/ext4/mballoc-test.c

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,28 @@ struct mbt_ctx {
2121
};
2222

2323
struct mbt_ext4_super_block {
24-
struct super_block sb;
24+
struct ext4_super_block es;
25+
struct ext4_sb_info sbi;
2526
struct mbt_ctx mbt_ctx;
2627
};
2728

28-
#define MBT_CTX(_sb) (&(container_of((_sb), struct mbt_ext4_super_block, sb)->mbt_ctx))
29+
#define MBT_SB(_sb) (container_of((_sb)->s_fs_info, struct mbt_ext4_super_block, sbi))
30+
#define MBT_CTX(_sb) (&MBT_SB(_sb)->mbt_ctx)
2931
#define MBT_GRP_CTX(_sb, _group) (&MBT_CTX(_sb)->grp_ctx[_group])
3032

3133
static const struct super_operations mbt_sops = {
3234
};
3335

36+
static void mbt_kill_sb(struct super_block *sb)
37+
{
38+
generic_shutdown_super(sb);
39+
}
40+
41+
static struct file_system_type mbt_fs_type = {
42+
.name = "mballoc test",
43+
.kill_sb = mbt_kill_sb,
44+
};
45+
3446
static int mbt_mb_init(struct super_block *sb)
3547
{
3648
int ret;
@@ -72,43 +84,54 @@ static void mbt_mb_release(struct super_block *sb)
7284
kfree(sb->s_bdev);
7385
}
7486

87+
static int mbt_set(struct super_block *sb, void *data)
88+
{
89+
return 0;
90+
}
91+
7592
static struct super_block *mbt_ext4_alloc_super_block(void)
7693
{
77-
struct ext4_super_block *es = kzalloc(sizeof(*es), GFP_KERNEL);
78-
struct ext4_sb_info *sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
79-
struct mbt_ext4_super_block *fsb = kzalloc(sizeof(*fsb), GFP_KERNEL);
94+
struct mbt_ext4_super_block *fsb;
95+
struct super_block *sb;
96+
struct ext4_sb_info *sbi;
8097

81-
if (fsb == NULL || sbi == NULL || es == NULL)
98+
fsb = kzalloc(sizeof(*fsb), GFP_KERNEL);
99+
if (fsb == NULL)
100+
return NULL;
101+
102+
sb = sget(&mbt_fs_type, NULL, mbt_set, 0, NULL);
103+
if (IS_ERR(sb))
82104
goto out;
83105

106+
sbi = &fsb->sbi;
107+
84108
sbi->s_blockgroup_lock =
85109
kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
86110
if (!sbi->s_blockgroup_lock)
87-
goto out;
111+
goto out_deactivate;
88112

89113
bgl_lock_init(sbi->s_blockgroup_lock);
90114

91-
sbi->s_es = es;
92-
fsb->sb.s_fs_info = sbi;
115+
sbi->s_es = &fsb->es;
116+
sb->s_fs_info = sbi;
93117

94-
return &fsb->sb;
118+
up_write(&sb->s_umount);
119+
return sb;
95120

121+
out_deactivate:
122+
deactivate_locked_super(sb);
96123
out:
97124
kfree(fsb);
98-
kfree(sbi);
99-
kfree(es);
100125
return NULL;
101126
}
102127

103128
static void mbt_ext4_free_super_block(struct super_block *sb)
104129
{
105-
struct mbt_ext4_super_block *fsb =
106-
container_of(sb, struct mbt_ext4_super_block, sb);
130+
struct mbt_ext4_super_block *fsb = MBT_SB(sb);
107131
struct ext4_sb_info *sbi = EXT4_SB(sb);
108132

109133
kfree(sbi->s_blockgroup_lock);
110-
kfree(sbi->s_es);
111-
kfree(sbi);
134+
deactivate_super(sb);
112135
kfree(fsb);
113136
}
114137

0 commit comments

Comments
 (0)