ext4: kunit: extents-test: Fix percpu_counters list corruption
Ritesh Harjani (IBM)
reviewerPATCH 1/1 ext4: kunit: extents-test: Fix percpu_counters list corruption
commit 82f80e2e3b23 ("ext4: add extent status cache support to kunit tests"), added ext4_es_register_shrinker() in extents_kunit_init() function but failed to add the unregister shrinker routine in extents_kunit_exit().
This could cause the following percpu_counters list corruption bug.
ok 1 split unwrit extent to 2 extents and convert 1st half writ
slab kmalloc-4k start c0000002007ff000 pointer offset 1448 size 4096
list_add corruption. next->prev should be prev (c000000004bc9e60), but was 0000000000000000. (next=c0000002007ff5a8).
------------[ cut here ]------------
kernel BUG at lib/list_debug.c:29!
cpu 0x2: Vector: 700 (Program Check) at [c000000241927a30]
pc: c000000000f26ed0: __list_add_valid_or_report+0x120/0x164
lr: c000000000f26ecc: __list_add_valid_or_report+0x11c/0x164
sp: c000000241927cd0 msr: 800000000282b033
current = 0xc000000241215200
paca = 0xc0000003fffff300 irqmask: 0x03 irq_happened: 0x09
pid = 258, comm = kunit_try_catch
kernel BUG at lib/list_debug.c:29!
enter ? for help
__percpu_counter_init_many+0x148/0x184 ext4_es_register_shrinker+0x74/0x23c extents_kunit_init+0x100/0x308 kunit_try_run_case+0x78/0x1f8 kunit_generic_run_threadfn_adapter+0x40/0x70 kthread+0x190/0x1a0 start_kernel_thread+0x14/0x18
2:mon>
This happens because:
extents_kunit_init(test N):
ext4_es_register_shrinker(sbi)
percpu_counters_init() x 4; // this adds 4 list nodes to global percpu_counters list
list_add(&fbc->list, &percpu_counters);
shrinker_register();extents_kunit_exit(test N):
kfree(sbi); // frees sbi w/o removing those 4 list nodes.
// So, those list node now becomes dangling pointers
extents_kunit_init(test N+1):
kzalloc_obj(ext4_sb_info) // allocator returns same page, but zeroed.
ext4_es_register_shrinker(sbi)
percpu_counters_init()
list_add(&fbc->list, &percpu_counters);
__list_add_valid(new, prev, next);
next->prev != prev // list corruption bug detected, since next->prev = NULL--- fs/ext4/extents-test.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
fs/ext4/extents-test.c +3 -1
@@ -142,8 +142,10 @@ static struct file_system_type ext_fs_type = {static void extents_kunit_exit (struct kunit *test) { -struct ext4_sb_info *sbi = k_ctx.k_ei->vfs_inode.i_sb->s_fs_info; +struct super_block *sb = k_ctx.k_ei->vfs_inode.i_sb; +struct ext4_sb_info *sbi = sb->s_fs_info; +ext4_es_unregister_shrinker (sbi);kfree (sbi);kfree (k_ctx.k_ei);kfree (k_ctx.k_data);
--
2.53.0
Quoted 45 lines · Ritesh Harjani (IBM)
commit
82f80e2e3b23("ext4: add extent status cache support to kunit tests"), addedext4_es_register_shrinker()inextents_kunit_init()function but failed to add the unregister shrinker routine inextents_kunit_exit().This could cause the following
percpu_counterslist corruption bug.ok 1 split unwrit extent to 2 extents and convert 1st half writ
slab kmalloc-4k startc0000002007ff000pointer offset 1448 size 4096
list_addcorruption.next->prevshould be prev (c000000004bc9e60), but was 0000000000000000. (next=c0000002007ff5a8).------------[ cut here ]------------ kernel BUG at lib/list_debug.c:29! cpu 0x2: Vector: 700 (Program Check) at [c000000241927a30] pc: c000000000f26ed0: __list_add_valid_or_report+0x120/0x164 lr: c000000000f26ecc: __list_add_valid_or_report+0x11c/0x164 sp: c000000241927cd0msr:
800000000282b033
current =0xc000000241215200
paca =0xc0000003fffff300irqmask:0x03irq_happened:0x09
pid = 258, comm =kunit_try_catch
kernel BUG atlib/list_debug.c:29!
enter ? for help__percpu_counter_init_many+0x148/0x184 ext4_es_register_shrinker+0x74/0x23c extents_kunit_init+0x100/0x308 kunit_try_run_case+0x78/0x1f8 kunit_generic_run_threadfn_adapter+0x40/0x70 kthread+0x190/0x1a0 start_kernel_thread+0x14/0x182:mon>
This happens because:
extents_kunit_init(test N):
ext4_es_register_shrinker(sbi)percpu_counters_init() x 4; // this adds 4 list nodes to global percpu_counters list list_add(&fbc->list, &percpu_counters); shrinker_register();
extents_kunit_exit(test N):
kfree(sbi); // frees sbi w/o removing those 4 list nodes.
// So, those list node now becomes dangling pointers
extents_kunit_init(test N+1):
kzalloc_obj(ext4_sb_info)// allocator returns same page, but zeroed.
ext4_es_register_shrinker(sbi)
Quoted 8 lines
percpu_counters_init() list_add(&fbc->list, &percpu_counters); __list_add_valid(new, prev, next); next->prev != prev // list corruption bug detected, since next->prev = NULLFixes 82f80e2e3b23 ("ext4: add extent status cache support to kunit tests")Signed-off-by Ritesh Harjani (IBM) <[email protected]>---
Hey Ritesh,
I can confirm that the patch fixes the issue. Thanks for looking into this. Feel free to add:
Regards,
ojaswin
Quoted 22 lines
fs/ext4/extents-test.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)fs/ext4/extents-test.c +3
-2@@ -142,8 +142,10 @@ static struct file_system_type ext_fs_type = {static void extents_kunit_exit (struct kunit *test) { -struct ext4_sb_info *sbi = k_ctx.k_ei->vfs_inode.i_sb->s_fs_info; +struct super_block *sb = k_ctx.k_ei->vfs_inode.i_sb; +struct ext4_sb_info *sbi = sb->s_fs_info; +ext4_es_unregister_shrinker (sbi);kfree (sbi);kfree (k_ctx.k_ei);kfree (k_ctx.k_data); --2.53.0
Quoted 3 lines · Ritesh Harjani (IBM)
commit
82f80e2e3b23("ext4: add extent status cache support to kunit tests"), addedext4_es_register_shrinker()inextents_kunit_init()function but failed to add the unregister shrinker routine inextents_kunit_exit().
This is breaking KUnit in -next which is causing problems for other trees, can we please get it merged promptly? Discussion here:
Ojaswin Mujoo
reviewerQuoted 9 lines · Mark Brown
Quoted 3 lines · Ritesh Harjani (IBM)
commit
82f80e2e3b23("ext4: add extent status cache support to kunit tests"), addedext4_es_register_shrinker()inextents_kunit_init()function but failed to add the unregister shrinker routine inextents_kunit_exit().This is breaking KUnit in -next which is causing problems for other trees, can we please get it merged promptly? Discussion here:
https://lore.kernel.org/r/[email protected]
@Ted, the fixes here [1] supersede this patch. I've asked Ye Bin to post the fixes independent of other patches. Once we have those, we can get that merged and it should fix linux-next
[1] https://lore.kernel.org/linux-ext4/[email protected]/
Regards,
ojaswin
Ojaswin Mujoo
reviewerQuoted 11 lines · Ojaswin Mujoo
Quoted 9 lines · Mark Brown
Quoted 3 lines · Ritesh Harjani (IBM)
commit
82f80e2e3b23("ext4: add extent status cache support to kunit tests"), addedext4_es_register_shrinker()inextents_kunit_init()function but failed to add the unregister shrinker routine inextents_kunit_exit().This is breaking KUnit in -next which is causing problems for other trees, can we please get it merged promptly? Discussion here:
https://lore.kernel.org/r/[email protected]
Quoted 8 lines
@Ted, the fixes here [1] supersede this patch. I've asked Ye Bin to post the fixes independent of other patches. Once we have those, we can get that merged and it should fix linux-next
[1] https://lore.kernel.org/linux-ext4/[email protected]/
Regards,
ojaswin
Hi Ted, upon discussing internally with Ritesh, I believe we still need this patch and [1] doesn't fix the issue of linux-next, just hides it.
Basically I was assuming that the patch 5 of Ye's patch will handle unregistering the shrinker via:
deactivate_super()
ext4_kill_sb()
... generic_super_shutdown() sop->put_super() ext4_put_super() ext4_es_unregister_shrinker() kfree(sbi)
However I missed the fact that in extents_kunit_init() we never set sb->s_op so ext4_put_super() is never called. In Ye's series, the issue became hidden because the kfree(sbi) was never called anymore (memory leak) causing slab to never return the corrupted page.
Hence this patch is needed for the immediate fix of linux-next. And then we can look into merging Ye's fixes separately, once the review is complete.
Sorry for the confusion.
Regards,
ojaswin
Quoted 3 lines · Ojaswin Mujoo
Hence this patch is needed for the immediate fix of linux-next. And then we can look into merging Ye's fixes separately, once the review is complete.
Given the disruption to other trees I've started carrying this as a fix but that's not ideal, any news on getting this applied?
Quoted 28 lines · Ritesh Harjani (IBM)
commit
82f80e2e3b23("ext4: add extent status cache support to kunit tests"), addedext4_es_register_shrinker()inextents_kunit_init()function but failed to add the unregister shrinker routine inextents_kunit_exit().This could cause the following
percpu_counterslist corruption bug.ok 1 split unwrit extent to 2 extents and convert 1st half writ
slab kmalloc-4k startc0000002007ff000pointer offset 1448 size 4096
list_addcorruption.next->prevshould be prev (c000000004bc9e60), but was 0000000000000000. (next=c0000002007ff5a8).------------[ cut here ]------------ kernel BUG at lib/list_debug.c:29! cpu 0x2: Vector: 700 (Program Check) at [c000000241927a30] pc: c000000000f26ed0: __list_add_valid_or_report+0x120/0x164 lr: c000000000f26ecc: __list_add_valid_or_report+0x11c/0x164 sp: c000000241927cd0msr:
800000000282b033
current =0xc000000241215200
paca =0xc0000003fffff300irqmask:0x03irq_happened:0x09
pid = 258, comm =kunit_try_catch
kernel BUG atlib/list_debug.c:29!
enter ? for help__percpu_counter_init_many+0x148/0x184 ext4_es_register_shrinker+0x74/0x23c extents_kunit_init+0x100/0x308 kunit_try_run_case+0x78/0x1f8 kunit_generic_run_threadfn_adapter+0x40/0x70 kthread+0x190/0x1a0 start_kernel_thread+0x14/0x18
Quoted 3 lines
2:mon>
[...]
Applied, thanks!
[1/1] ext4: kunit: extents-test: Fix percpu_counters list corruption
commit: bd7f1a0278d2d1fe88a734c3b8273c1a62db35cc
Best regards,
Signature · 1 line
Theodore Ts'o <[email protected]>