Skip to content

Commit be6b973

Browse files
Ye Bingregkh
authored andcommitted
ext4: fix mballoc-test.c is not compiled when EXT4_KUNIT_TESTS=M
[ Upstream commit 519b76a ] Now, only EXT4_KUNIT_TESTS=Y testcase will be compiled in 'mballoc.c'. To solve this issue, the ext4 test code needs to be decoupled. The ext4 test module is compiled into a separate module. Reported-by: ChenXiaoSong <chenxiaosong@kylinos.cn> Closes: https://patchwork.kernel.org/project/cifs-client/patch/20260118091313.1988168-2-chenxiaosong.chenxiaosong@linux.dev/ Fixes: 7c9fa39 ("ext4: add first unit test for ext4_mb_new_blocks_simple in mballoc") Signed-off-by: Ye Bin <yebin10@huawei.com> Reviewed-by: Jan Kara <jack@suse.cz> Link: https://patch.msgid.link/20260314075258.1317579-3-yebin@huaweicloud.com Signed-off-by: Theodore Ts'o <tytso@mit.edu> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent ba749e9 commit be6b973

4 files changed

Lines changed: 172 additions & 45 deletions

File tree

fs/ext4/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ ext4-y := balloc.o bitmap.o block_validity.o dir.o ext4_jbd2.o extents.o \
1414

1515
ext4-$(CONFIG_EXT4_FS_POSIX_ACL) += acl.o
1616
ext4-$(CONFIG_EXT4_FS_SECURITY) += xattr_security.o
17-
ext4-inode-test-objs += inode-test.o
18-
obj-$(CONFIG_EXT4_KUNIT_TESTS) += ext4-inode-test.o
17+
ext4-test-objs += inode-test.o mballoc-test.o
18+
obj-$(CONFIG_EXT4_KUNIT_TESTS) += ext4-test.o
1919
ext4-$(CONFIG_FS_VERITY) += verity.o
2020
ext4-$(CONFIG_FS_ENCRYPTION) += crypto.o

fs/ext4/mballoc-test.c

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <linux/random.h>
99

1010
#include "ext4.h"
11+
#include "mballoc.h"
1112

1213
struct mbt_grp_ctx {
1314
struct buffer_head bitmap_bh;
@@ -337,7 +338,7 @@ ext4_mb_mark_context_stub(handle_t *handle, struct super_block *sb, bool state,
337338
if (state)
338339
mb_set_bits(bitmap_bh->b_data, blkoff, len);
339340
else
340-
mb_clear_bits(bitmap_bh->b_data, blkoff, len);
341+
mb_clear_bits_test(bitmap_bh->b_data, blkoff, len);
341342

342343
return 0;
343344
}
@@ -414,22 +415,22 @@ static void test_new_blocks_simple(struct kunit *test)
414415

415416
/* get block at goal */
416417
ar.goal = ext4_group_first_block_no(sb, goal_group);
417-
found = ext4_mb_new_blocks_simple(&ar, &err);
418+
found = ext4_mb_new_blocks_simple_test(&ar, &err);
418419
KUNIT_ASSERT_EQ_MSG(test, ar.goal, found,
419420
"failed to alloc block at goal, expected %llu found %llu",
420421
ar.goal, found);
421422

422423
/* get block after goal in goal group */
423424
ar.goal = ext4_group_first_block_no(sb, goal_group);
424-
found = ext4_mb_new_blocks_simple(&ar, &err);
425+
found = ext4_mb_new_blocks_simple_test(&ar, &err);
425426
KUNIT_ASSERT_EQ_MSG(test, ar.goal + EXT4_C2B(sbi, 1), found,
426427
"failed to alloc block after goal in goal group, expected %llu found %llu",
427428
ar.goal + 1, found);
428429

429430
/* get block after goal group */
430431
mbt_ctx_mark_used(sb, goal_group, 0, EXT4_CLUSTERS_PER_GROUP(sb));
431432
ar.goal = ext4_group_first_block_no(sb, goal_group);
432-
found = ext4_mb_new_blocks_simple(&ar, &err);
433+
found = ext4_mb_new_blocks_simple_test(&ar, &err);
433434
KUNIT_ASSERT_EQ_MSG(test,
434435
ext4_group_first_block_no(sb, goal_group + 1), found,
435436
"failed to alloc block after goal group, expected %llu found %llu",
@@ -439,7 +440,7 @@ static void test_new_blocks_simple(struct kunit *test)
439440
for (i = goal_group; i < ext4_get_groups_count(sb); i++)
440441
mbt_ctx_mark_used(sb, i, 0, EXT4_CLUSTERS_PER_GROUP(sb));
441442
ar.goal = ext4_group_first_block_no(sb, goal_group);
442-
found = ext4_mb_new_blocks_simple(&ar, &err);
443+
found = ext4_mb_new_blocks_simple_test(&ar, &err);
443444
KUNIT_ASSERT_EQ_MSG(test,
444445
ext4_group_first_block_no(sb, 0) + EXT4_C2B(sbi, 1), found,
445446
"failed to alloc block before goal group, expected %llu found %llu",
@@ -449,7 +450,7 @@ static void test_new_blocks_simple(struct kunit *test)
449450
for (i = 0; i < ext4_get_groups_count(sb); i++)
450451
mbt_ctx_mark_used(sb, i, 0, EXT4_CLUSTERS_PER_GROUP(sb));
451452
ar.goal = ext4_group_first_block_no(sb, goal_group);
452-
found = ext4_mb_new_blocks_simple(&ar, &err);
453+
found = ext4_mb_new_blocks_simple_test(&ar, &err);
453454
KUNIT_ASSERT_NE_MSG(test, err, 0,
454455
"unexpectedly get block when no block is available");
455456
}
@@ -493,16 +494,16 @@ validate_free_blocks_simple(struct kunit *test, struct super_block *sb,
493494
continue;
494495

495496
bitmap = mbt_ctx_bitmap(sb, i);
496-
bit = mb_find_next_zero_bit(bitmap, max, 0);
497+
bit = mb_find_next_zero_bit_test(bitmap, max, 0);
497498
KUNIT_ASSERT_EQ_MSG(test, bit, max,
498499
"free block on unexpected group %d", i);
499500
}
500501

501502
bitmap = mbt_ctx_bitmap(sb, goal_group);
502-
bit = mb_find_next_zero_bit(bitmap, max, 0);
503+
bit = mb_find_next_zero_bit_test(bitmap, max, 0);
503504
KUNIT_ASSERT_EQ(test, bit, start);
504505

505-
bit = mb_find_next_bit(bitmap, max, bit + 1);
506+
bit = mb_find_next_bit_test(bitmap, max, bit + 1);
506507
KUNIT_ASSERT_EQ(test, bit, start + len);
507508
}
508509

@@ -525,7 +526,7 @@ test_free_blocks_simple_range(struct kunit *test, ext4_group_t goal_group,
525526

526527
block = ext4_group_first_block_no(sb, goal_group) +
527528
EXT4_C2B(sbi, start);
528-
ext4_free_blocks_simple(inode, block, len);
529+
ext4_free_blocks_simple_test(inode, block, len);
529530
validate_free_blocks_simple(test, sb, goal_group, start, len);
530531
mbt_ctx_mark_used(sb, goal_group, 0, EXT4_CLUSTERS_PER_GROUP(sb));
531532
}
@@ -567,15 +568,15 @@ test_mark_diskspace_used_range(struct kunit *test,
567568

568569
bitmap = mbt_ctx_bitmap(sb, TEST_GOAL_GROUP);
569570
memset(bitmap, 0, sb->s_blocksize);
570-
ret = ext4_mb_mark_diskspace_used(ac, NULL);
571+
ret = ext4_mb_mark_diskspace_used_test(ac, NULL);
571572
KUNIT_ASSERT_EQ(test, ret, 0);
572573

573574
max = EXT4_CLUSTERS_PER_GROUP(sb);
574-
i = mb_find_next_bit(bitmap, max, 0);
575+
i = mb_find_next_bit_test(bitmap, max, 0);
575576
KUNIT_ASSERT_EQ(test, i, start);
576-
i = mb_find_next_zero_bit(bitmap, max, i + 1);
577+
i = mb_find_next_zero_bit_test(bitmap, max, i + 1);
577578
KUNIT_ASSERT_EQ(test, i, start + len);
578-
i = mb_find_next_bit(bitmap, max, i + 1);
579+
i = mb_find_next_bit_test(bitmap, max, i + 1);
579580
KUNIT_ASSERT_EQ(test, max, i);
580581
}
581582

@@ -618,54 +619,54 @@ static void mbt_generate_buddy(struct super_block *sb, void *buddy,
618619
max = EXT4_CLUSTERS_PER_GROUP(sb);
619620
bb_h = buddy + sbi->s_mb_offsets[1];
620621

621-
off = mb_find_next_zero_bit(bb, max, 0);
622+
off = mb_find_next_zero_bit_test(bb, max, 0);
622623
grp->bb_first_free = off;
623624
while (off < max) {
624625
grp->bb_counters[0]++;
625626
grp->bb_free++;
626627

627-
if (!(off & 1) && !mb_test_bit(off + 1, bb)) {
628+
if (!(off & 1) && !mb_test_bit_test(off + 1, bb)) {
628629
grp->bb_free++;
629630
grp->bb_counters[0]--;
630-
mb_clear_bit(off >> 1, bb_h);
631+
mb_clear_bit_test(off >> 1, bb_h);
631632
grp->bb_counters[1]++;
632633
grp->bb_largest_free_order = 1;
633634
off++;
634635
}
635636

636-
off = mb_find_next_zero_bit(bb, max, off + 1);
637+
off = mb_find_next_zero_bit_test(bb, max, off + 1);
637638
}
638639

639640
for (order = 1; order < MB_NUM_ORDERS(sb) - 1; order++) {
640641
bb = buddy + sbi->s_mb_offsets[order];
641642
bb_h = buddy + sbi->s_mb_offsets[order + 1];
642643
max = max >> 1;
643-
off = mb_find_next_zero_bit(bb, max, 0);
644+
off = mb_find_next_zero_bit_test(bb, max, 0);
644645

645646
while (off < max) {
646-
if (!(off & 1) && !mb_test_bit(off + 1, bb)) {
647+
if (!(off & 1) && !mb_test_bit_test(off + 1, bb)) {
647648
mb_set_bits(bb, off, 2);
648649
grp->bb_counters[order] -= 2;
649-
mb_clear_bit(off >> 1, bb_h);
650+
mb_clear_bit_test(off >> 1, bb_h);
650651
grp->bb_counters[order + 1]++;
651652
grp->bb_largest_free_order = order + 1;
652653
off++;
653654
}
654655

655-
off = mb_find_next_zero_bit(bb, max, off + 1);
656+
off = mb_find_next_zero_bit_test(bb, max, off + 1);
656657
}
657658
}
658659

659660
max = EXT4_CLUSTERS_PER_GROUP(sb);
660-
off = mb_find_next_zero_bit(bitmap, max, 0);
661+
off = mb_find_next_zero_bit_test(bitmap, max, 0);
661662
while (off < max) {
662663
grp->bb_fragments++;
663664

664-
off = mb_find_next_bit(bitmap, max, off + 1);
665+
off = mb_find_next_bit_test(bitmap, max, off + 1);
665666
if (off + 1 >= max)
666667
break;
667668

668-
off = mb_find_next_zero_bit(bitmap, max, off + 1);
669+
off = mb_find_next_zero_bit_test(bitmap, max, off + 1);
669670
}
670671
}
671672

@@ -707,7 +708,7 @@ do_test_generate_buddy(struct kunit *test, struct super_block *sb, void *bitmap,
707708
/* needed by validation in ext4_mb_generate_buddy */
708709
ext4_grp->bb_free = mbt_grp->bb_free;
709710
memset(ext4_buddy, 0xff, sb->s_blocksize);
710-
ext4_mb_generate_buddy(sb, ext4_buddy, bitmap, TEST_GOAL_GROUP,
711+
ext4_mb_generate_buddy_test(sb, ext4_buddy, bitmap, TEST_GOAL_GROUP,
711712
ext4_grp);
712713

713714
KUNIT_ASSERT_EQ(test, memcmp(mbt_buddy, ext4_buddy, sb->s_blocksize),
@@ -761,7 +762,7 @@ test_mb_mark_used_range(struct kunit *test, struct ext4_buddy *e4b,
761762
ex.fe_group = TEST_GOAL_GROUP;
762763

763764
ext4_lock_group(sb, TEST_GOAL_GROUP);
764-
mb_mark_used(e4b, &ex);
765+
mb_mark_used_test(e4b, &ex);
765766
ext4_unlock_group(sb, TEST_GOAL_GROUP);
766767

767768
mb_set_bits(bitmap, start, len);
@@ -770,7 +771,7 @@ test_mb_mark_used_range(struct kunit *test, struct ext4_buddy *e4b,
770771
memset(buddy, 0xff, sb->s_blocksize);
771772
for (i = 0; i < MB_NUM_ORDERS(sb); i++)
772773
grp->bb_counters[i] = 0;
773-
ext4_mb_generate_buddy(sb, buddy, bitmap, 0, grp);
774+
ext4_mb_generate_buddy_test(sb, buddy, bitmap, 0, grp);
774775

775776
KUNIT_ASSERT_EQ(test, memcmp(buddy, e4b->bd_buddy, sb->s_blocksize),
776777
0);
@@ -799,7 +800,7 @@ static void test_mb_mark_used(struct kunit *test)
799800
bb_counters[MB_NUM_ORDERS(sb)]), GFP_KERNEL);
800801
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, grp);
801802

802-
ret = ext4_mb_load_buddy(sb, TEST_GOAL_GROUP, &e4b);
803+
ret = ext4_mb_load_buddy_test(sb, TEST_GOAL_GROUP, &e4b);
803804
KUNIT_ASSERT_EQ(test, ret, 0);
804805

805806
grp->bb_free = EXT4_CLUSTERS_PER_GROUP(sb);
@@ -810,7 +811,7 @@ static void test_mb_mark_used(struct kunit *test)
810811
test_mb_mark_used_range(test, &e4b, ranges[i].start,
811812
ranges[i].len, bitmap, buddy, grp);
812813

813-
ext4_mb_unload_buddy(&e4b);
814+
ext4_mb_unload_buddy_test(&e4b);
814815
}
815816

816817
static void
@@ -826,16 +827,16 @@ test_mb_free_blocks_range(struct kunit *test, struct ext4_buddy *e4b,
826827
return;
827828

828829
ext4_lock_group(sb, e4b->bd_group);
829-
mb_free_blocks(NULL, e4b, start, len);
830+
mb_free_blocks_test(NULL, e4b, start, len);
830831
ext4_unlock_group(sb, e4b->bd_group);
831832

832-
mb_clear_bits(bitmap, start, len);
833+
mb_clear_bits_test(bitmap, start, len);
833834
/* bypass bb_free validatoin in ext4_mb_generate_buddy */
834835
grp->bb_free += len;
835836
memset(buddy, 0xff, sb->s_blocksize);
836837
for (i = 0; i < MB_NUM_ORDERS(sb); i++)
837838
grp->bb_counters[i] = 0;
838-
ext4_mb_generate_buddy(sb, buddy, bitmap, 0, grp);
839+
ext4_mb_generate_buddy_test(sb, buddy, bitmap, 0, grp);
839840

840841
KUNIT_ASSERT_EQ(test, memcmp(buddy, e4b->bd_buddy, sb->s_blocksize),
841842
0);
@@ -866,15 +867,15 @@ static void test_mb_free_blocks(struct kunit *test)
866867
bb_counters[MB_NUM_ORDERS(sb)]), GFP_KERNEL);
867868
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, grp);
868869

869-
ret = ext4_mb_load_buddy(sb, TEST_GOAL_GROUP, &e4b);
870+
ret = ext4_mb_load_buddy_test(sb, TEST_GOAL_GROUP, &e4b);
870871
KUNIT_ASSERT_EQ(test, ret, 0);
871872

872873
ex.fe_start = 0;
873874
ex.fe_len = EXT4_CLUSTERS_PER_GROUP(sb);
874875
ex.fe_group = TEST_GOAL_GROUP;
875876

876877
ext4_lock_group(sb, TEST_GOAL_GROUP);
877-
mb_mark_used(&e4b, &ex);
878+
mb_mark_used_test(&e4b, &ex);
878879
ext4_unlock_group(sb, TEST_GOAL_GROUP);
879880

880881
grp->bb_free = 0;
@@ -887,7 +888,7 @@ static void test_mb_free_blocks(struct kunit *test)
887888
test_mb_free_blocks_range(test, &e4b, ranges[i].start,
888889
ranges[i].len, bitmap, buddy, grp);
889890

890-
ext4_mb_unload_buddy(&e4b);
891+
ext4_mb_unload_buddy_test(&e4b);
891892
}
892893

893894
#define COUNT_FOR_ESTIMATE 100000
@@ -905,7 +906,7 @@ static void test_mb_mark_used_cost(struct kunit *test)
905906
if (sb->s_blocksize > PAGE_SIZE)
906907
kunit_skip(test, "blocksize exceeds pagesize");
907908

908-
ret = ext4_mb_load_buddy(sb, TEST_GOAL_GROUP, &e4b);
909+
ret = ext4_mb_load_buddy_test(sb, TEST_GOAL_GROUP, &e4b);
909910
KUNIT_ASSERT_EQ(test, ret, 0);
910911

911912
ex.fe_group = TEST_GOAL_GROUP;
@@ -919,7 +920,7 @@ static void test_mb_mark_used_cost(struct kunit *test)
919920
ex.fe_start = ranges[i].start;
920921
ex.fe_len = ranges[i].len;
921922
ext4_lock_group(sb, TEST_GOAL_GROUP);
922-
mb_mark_used(&e4b, &ex);
923+
mb_mark_used_test(&e4b, &ex);
923924
ext4_unlock_group(sb, TEST_GOAL_GROUP);
924925
}
925926
end = jiffies;
@@ -930,14 +931,14 @@ static void test_mb_mark_used_cost(struct kunit *test)
930931
continue;
931932

932933
ext4_lock_group(sb, TEST_GOAL_GROUP);
933-
mb_free_blocks(NULL, &e4b, ranges[i].start,
934+
mb_free_blocks_test(NULL, &e4b, ranges[i].start,
934935
ranges[i].len);
935936
ext4_unlock_group(sb, TEST_GOAL_GROUP);
936937
}
937938
}
938939

939940
kunit_info(test, "costed jiffies %lu\n", all);
940-
ext4_mb_unload_buddy(&e4b);
941+
ext4_mb_unload_buddy_test(&e4b);
941942
}
942943

943944
static const struct mbt_ext4_block_layout mbt_test_layouts[] = {

0 commit comments

Comments
 (0)