Add a KUnit suite for hfs_bnode_write(), hfs_bnode_clear(), hfs_bnode_copy() and hfs_bnode_move() on 32 KiB nodes, with ranges that cross page boundaries and reach the end of the node, checking the node's contents page by page. The first page of each test node is the start of a higher-order allocation, so an operation that runs off the end of node->page[0] lands in the test's own guard pages, which are checked, instead of in other memory. Without the multi-page fix all four cases fail on systems with 4 KiB pages. Export the helpers with EXPORT_SYMBOL_IF_KUNIT(), as the existing HFS tests do for string.c and inode.c. Found and written with an LLM coding assistant. Signed-off-by: Mar Dominguez --- fs/hfs/Makefile | 2 +- fs/hfs/bnode.c | 8 ++ fs/hfs/bnode_test.c | 293 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 302 insertions(+), 1 deletion(-) create mode 100644 fs/hfs/bnode_test.c diff --git a/fs/hfs/Makefile b/fs/hfs/Makefile index be68c5305f..c19138944f 100644 --- a/fs/hfs/Makefile +++ b/fs/hfs/Makefile @@ -10,4 +10,4 @@ hfs-objs := bitmap.o bfind.o bnode.o brec.o btree.o \ part_tbl.o string.o super.o sysdep.o trans.o # KUnit tests -obj-$(CONFIG_HFS_KUNIT_TEST) += string_test.o hfs_time_test.o +obj-$(CONFIG_HFS_KUNIT_TEST) += string_test.o hfs_time_test.o bnode_test.o diff --git a/fs/hfs/bnode.c b/fs/hfs/bnode.c index d12be972ce..d1d61d7f46 100644 --- a/fs/hfs/bnode.c +++ b/fs/hfs/bnode.c @@ -12,6 +12,7 @@ #include #include #include +#include #include "btree.h" @@ -54,6 +55,7 @@ void hfs_bnode_read(struct hfs_bnode *node, void *buf, u32 off, u32 len) off = 0; /* page offset only applies to the first page */ } } +EXPORT_SYMBOL_IF_KUNIT(hfs_bnode_read); u16 hfs_bnode_read_u16(struct hfs_bnode *node, u32 off) { @@ -62,6 +64,7 @@ u16 hfs_bnode_read_u16(struct hfs_bnode *node, u32 off) hfs_bnode_read(node, &data, off, 2); return be16_to_cpu(data); } +EXPORT_SYMBOL_IF_KUNIT(hfs_bnode_read_u16); u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32 off) { @@ -131,6 +134,7 @@ void hfs_bnode_write(struct hfs_bnode *node, void *buf, u32 off, u32 len) off = 0; /* page offset only applies to the first page */ } } +EXPORT_SYMBOL_IF_KUNIT(hfs_bnode_write); void hfs_bnode_write_u16(struct hfs_bnode *node, u32 off, u16 data) { @@ -138,6 +142,7 @@ void hfs_bnode_write_u16(struct hfs_bnode *node, u32 off, u16 data) // optimize later... hfs_bnode_write(node, &v, off, 2); } +EXPORT_SYMBOL_IF_KUNIT(hfs_bnode_write_u16); void hfs_bnode_write_u8(struct hfs_bnode *node, u32 off, u8 data) { @@ -184,6 +189,7 @@ void hfs_bnode_clear(struct hfs_bnode *node, u32 off, u32 len) off = 0; /* page offset only applies to the first page */ } } +EXPORT_SYMBOL_IF_KUNIT(hfs_bnode_clear); /* Number of bytes from @off to the end of the page that contains @off */ static inline u32 hfs_bnode_page_tail(u32 off) @@ -246,6 +252,7 @@ void hfs_bnode_copy(struct hfs_bnode *dst_node, u32 dst, len -= chunk; } } +EXPORT_SYMBOL_IF_KUNIT(hfs_bnode_copy); void hfs_bnode_move(struct hfs_bnode *node, u32 dst, u32 src, u32 len) { @@ -284,6 +291,7 @@ void hfs_bnode_move(struct hfs_bnode *node, u32 dst, u32 src, u32 len) } } } +EXPORT_SYMBOL_IF_KUNIT(hfs_bnode_move); void hfs_bnode_dump(struct hfs_bnode *node) { diff --git a/fs/hfs/bnode_test.c b/fs/hfs/bnode_test.c new file mode 100644 index 0000000000..9c34df76a0 --- /dev/null +++ b/fs/hfs/bnode_test.c @@ -0,0 +1,293 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit tests for HFS b-tree node operations + * + * A b-tree node can be larger than a page (up to HFS_NODE_MXSZ bytes). + * The tests check that reads, writes, clears, copies and moves reach the + * right page of such a node. The first page of every test node is the + * first page of a higher-order allocation, so an operation that runs off + * the end of node->page[0] lands in the test's own guard pages, which are + * checked, instead of corrupting other memory. + */ + +#include +#include +#include +#include +#include +#include +#include "btree.h" + +#define TEST_NODE_SIZE HFS_NODE_MXSZ + +struct hfs_bnode_test_node { + struct hfs_bnode *node; + struct page *block; + unsigned int order; +}; + +static void hfs_bnode_test_free(void *data) +{ + struct hfs_bnode_test_node *t = data; + u32 i; + + for (i = 1; i < t->node->tree->pages_per_bnode; i++) + if (t->node->page[i]) + __free_page(t->node->page[i]); + __free_pages(t->block, t->order); +} + +static struct hfs_btree *hfs_bnode_test_tree(struct kunit *test) +{ + struct hfs_btree *tree; + + tree = kunit_kzalloc(test, sizeof(*tree), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, tree); + tree->node_size = TEST_NODE_SIZE; + tree->pages_per_bnode = DIV_ROUND_UP(TEST_NODE_SIZE, PAGE_SIZE); + + return tree; +} + +static struct hfs_bnode_test_node *hfs_bnode_test_alloc(struct kunit *test, + struct hfs_btree *tree) +{ + struct hfs_bnode_test_node *t; + struct hfs_bnode *node; + u32 i; + + t = kunit_kzalloc(test, sizeof(*t), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, t); + node = kunit_kzalloc(test, struct_size(node, page, tree->pages_per_bnode), + GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, node); + node->tree = tree; + t->node = node; + + t->order = get_order(TEST_NODE_SIZE); + t->block = alloc_pages(GFP_KERNEL | __GFP_ZERO, t->order); + KUNIT_ASSERT_NOT_NULL(test, t->block); + node->page[0] = t->block; + for (i = 1; i < tree->pages_per_bnode; i++) { + node->page[i] = alloc_page(GFP_KERNEL | __GFP_ZERO); + if (!node->page[i]) + break; + } + KUNIT_ASSERT_EQ(test, 0, + kunit_add_action_or_reset(test, hfs_bnode_test_free, t)); + KUNIT_ASSERT_EQ(test, i, tree->pages_per_bnode); + + return t; +} + +/* Offset of the first byte that differs, or @len */ +static u32 hfs_bnode_test_diff(const u8 *a, const u8 *b, u32 len) +{ + u32 i; + + for (i = 0; i < len; i++) + if (a[i] != b[i]) + break; + + return i; +} + +/* Check the node's contents page by page, and that the guard pages are untouched */ +static void hfs_bnode_test_check(struct kunit *test, struct hfs_bnode_test_node *t, + const u8 *expected) +{ + struct hfs_bnode *node = t->node; + u32 size = node->tree->node_size; + u32 i, len, written; + u8 *buf; + void *p; + + buf = kunit_kzalloc(test, size, GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, buf); + for (i = 0; i * PAGE_SIZE < size; i++) { + len = min_t(u32, size - i * PAGE_SIZE, PAGE_SIZE); + memcpy_from_page(buf + i * PAGE_SIZE, node->page[i], 0, len); + } + i = hfs_bnode_test_diff(buf, expected, size); + KUNIT_EXPECT_EQ_MSG(test, i, size, "node contents differ at offset %u", i); + + for (i = 1, written = 0; i < (1U << t->order); i++) { + p = kmap_local_page(t->block + i); + if (memchr_inv(p, 0, PAGE_SIZE)) + written++; + kunmap_local(p); + } + KUNIT_EXPECT_EQ_MSG(test, written, 0, "%u guard pages were written", written); + kunit_kfree(test, buf); +} + +/* + * An offset in the node that is a page boundary when nodes span several + * pages, and a valid offset otherwise + */ +static u32 hfs_bnode_test_boundary(struct hfs_btree *tree) +{ + return min_t(u32, PAGE_SIZE, tree->node_size / 2); +} + +static u8 *hfs_bnode_test_pattern(struct kunit *test, u32 size, u8 seed) +{ + u8 *buf; + u32 i; + + buf = kunit_kmalloc(test, size, GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, buf); + for (i = 0; i < size; i++) + buf[i] = seed + i * 7 + (i >> 8); + + return buf; +} + +/* Write a few ranges, including ones crossing page boundaries and the node's end */ +static void hfs_bnode_write_test(struct kunit *test) +{ + struct hfs_btree *tree = hfs_bnode_test_tree(test); + struct hfs_bnode_test_node *t = hfs_bnode_test_alloc(test, tree); + u32 size = tree->node_size; + u32 pb = hfs_bnode_test_boundary(tree); + const struct { u32 off, len; } ranges[] = { + { 0, sizeof(struct hfs_bnode_desc) }, + { pb - 3, 7 }, + { size / 2 - 100, 300 }, + { size - 6, 6 }, + }; + u8 *expected, *data, *buf; + u32 i; + + expected = kunit_kzalloc(test, size, GFP_KERNEL); + buf = kunit_kzalloc(test, size, GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, expected); + KUNIT_ASSERT_NOT_NULL(test, buf); + data = hfs_bnode_test_pattern(test, size, 0x11); + + for (i = 0; i < ARRAY_SIZE(ranges); i++) { + hfs_bnode_write(t->node, data, ranges[i].off, ranges[i].len); + memcpy(expected + ranges[i].off, data, ranges[i].len); + + hfs_bnode_read(t->node, buf, ranges[i].off, ranges[i].len); + KUNIT_EXPECT_MEMEQ_MSG(test, buf, data, ranges[i].len, + "read back at offset %u", ranges[i].off); + } + + /* the record offset at the end of the node */ + hfs_bnode_write_u16(t->node, size - 2, 0x1234); + expected[size - 2] = 0x12; + expected[size - 1] = 0x34; + KUNIT_EXPECT_EQ(test, 0x1234, hfs_bnode_read_u16(t->node, size - 2)); + + hfs_bnode_test_check(test, t, expected); +} + +static void hfs_bnode_clear_test(struct kunit *test) +{ + struct hfs_btree *tree = hfs_bnode_test_tree(test); + struct hfs_bnode_test_node *t = hfs_bnode_test_alloc(test, tree); + u32 size = tree->node_size; + u32 pb = hfs_bnode_test_boundary(tree); + u8 *expected; + + expected = hfs_bnode_test_pattern(test, size, 0x5a); + hfs_bnode_write(t->node, expected, 0, size); + hfs_bnode_test_check(test, t, expected); + + hfs_bnode_clear(t->node, pb - 5, 10); + memset(expected + pb - 5, 0, 10); + hfs_bnode_test_check(test, t, expected); + + hfs_bnode_clear(t->node, size / 4 + 1, size / 2); + memset(expected + size / 4 + 1, 0, size / 2); + hfs_bnode_test_check(test, t, expected); + + /* what hfs_bnode_put() does for a deleted node */ + hfs_bnode_clear(t->node, 0, size); + memset(expected, 0, size); + hfs_bnode_test_check(test, t, expected); +} + +static void hfs_bnode_copy_test(struct kunit *test) +{ + struct hfs_btree *tree = hfs_bnode_test_tree(test); + struct hfs_bnode_test_node *src = hfs_bnode_test_alloc(test, tree); + struct hfs_bnode_test_node *dst = hfs_bnode_test_alloc(test, tree); + u32 size = tree->node_size; + u32 pb = hfs_bnode_test_boundary(tree); + const struct { u32 dst, src, len; } copies[] = { + { 14, pb - 10, 300 }, + { size - 600, 100, 600 }, + { pb + 1, 3, size / 2 - 2 }, + { 3, 5, size - 8 }, + }; + u8 *data, *expected; + u32 i; + + data = hfs_bnode_test_pattern(test, size, 0x21); + hfs_bnode_write(src->node, data, 0, size); + hfs_bnode_test_check(test, src, data); + + expected = kunit_kzalloc(test, size, GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, expected); + for (i = 0; i < ARRAY_SIZE(copies); i++) { + hfs_bnode_copy(dst->node, copies[i].dst, src->node, copies[i].src, + copies[i].len); + memcpy(expected + copies[i].dst, data + copies[i].src, copies[i].len); + hfs_bnode_test_check(test, dst, expected); + } + hfs_bnode_test_check(test, src, data); +} + +static void hfs_bnode_move_test(struct kunit *test) +{ + struct hfs_btree *tree = hfs_bnode_test_tree(test); + struct hfs_bnode_test_node *t = hfs_bnode_test_alloc(test, tree); + u32 size = tree->node_size; + u32 pb = hfs_bnode_test_boundary(tree); + const struct { u32 dst, src, len; } moves[] = { + /* overlapping, towards the end (hfs_brec_insert()) */ + { 102, 100, size - 200 }, + { pb + 6, pb - 6, size / 2 - 6 }, + /* overlapping, towards the start (hfs_brec_remove()) */ + { 100, 102, size - 200 }, + { pb - 6, pb + 6, size / 2 - 6 }, + /* disjoint */ + { size / 2 + 3, 14, size / 2 - 20 }, + { 14, size / 2 + 3, size / 2 - 20 }, + /* within one page */ + { 16, 14, 2 }, + }; + u8 *expected; + u32 i; + + expected = hfs_bnode_test_pattern(test, size, 0x37); + hfs_bnode_write(t->node, expected, 0, size); + + for (i = 0; i < ARRAY_SIZE(moves); i++) { + hfs_bnode_move(t->node, moves[i].dst, moves[i].src, moves[i].len); + memmove(expected + moves[i].dst, expected + moves[i].src, + moves[i].len); + hfs_bnode_test_check(test, t, expected); + } +} + +static struct kunit_case hfs_bnode_test_cases[] = { + KUNIT_CASE(hfs_bnode_write_test), + KUNIT_CASE(hfs_bnode_clear_test), + KUNIT_CASE(hfs_bnode_copy_test), + KUNIT_CASE(hfs_bnode_move_test), + {} +}; + +static struct kunit_suite hfs_bnode_test_suite = { + .name = "hfs_bnode", + .test_cases = hfs_bnode_test_cases, +}; + +kunit_test_suite(hfs_bnode_test_suite); + +MODULE_DESCRIPTION("KUnit tests for HFS b-tree node operations"); +MODULE_LICENSE("GPL"); +MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING"); -- 2.53.0