* Peng Zhang <zhangpeng.00@xxxxxxxxxxxxx> [230726 04:10]: > Add test for mt_dup(). > > Signed-off-by: Peng Zhang <zhangpeng.00@xxxxxxxxxxxxx> > --- > tools/testing/radix-tree/maple.c | 202 +++++++++++++++++++++++++++++++ > 1 file changed, 202 insertions(+) > > diff --git a/tools/testing/radix-tree/maple.c b/tools/testing/radix-tree/maple.c > index e5da1cad70ba..3052e899e5df 100644 > --- a/tools/testing/radix-tree/maple.c > +++ b/tools/testing/radix-tree/maple.c > @@ -35857,6 +35857,204 @@ static noinline void __init check_locky(struct maple_tree *mt) > mt_clear_in_rcu(mt); > } > > +/* > + * Compare two nodes and return 0 if they are the same, non-zero otherwise. > + */ > +static int __init compare_node(struct maple_enode *enode_a, > + struct maple_enode *enode_b) > +{ > + struct maple_node *node_a, *node_b; > + struct maple_node a, b; > + void **slots_a, **slots_b; /* Do not use the rcu tag. */ > + enum maple_type type; > + int i; > + > + if (((unsigned long)enode_a & MAPLE_NODE_MASK) != > + ((unsigned long)enode_b & MAPLE_NODE_MASK)) { > + pr_err("The lower 8 bits of enode are different.\n"); > + return -1; > + } > + > + type = mte_node_type(enode_a); > + node_a = mte_to_node(enode_a); > + node_b = mte_to_node(enode_b); > + a = *node_a; > + b = *node_b; > + > + /* Do not compare addresses. */ > + if (ma_is_root(node_a) || ma_is_root(node_b)) { > + a.parent = (struct maple_pnode *)((unsigned long)a.parent & > + MA_ROOT_PARENT); > + b.parent = (struct maple_pnode *)((unsigned long)b.parent & > + MA_ROOT_PARENT); > + } else { > + a.parent = (struct maple_pnode *)((unsigned long)a.parent & > + MAPLE_NODE_MASK); > + b.parent = (struct maple_pnode *)((unsigned long)b.parent & > + MAPLE_NODE_MASK); > + } > + > + if (a.parent != b.parent) { > + pr_err("The lower 8 bits of parents are different. %p %p\n", > + a.parent, b.parent); > + return -1; > + } > + > + /* > + * If it is a leaf node, the slots do not contain the node address, and > + * no special processing of slots is required. > + */ > + if (ma_is_leaf(type)) > + goto cmp; > + > + slots_a = ma_slots(&a, type); > + slots_b = ma_slots(&b, type); > + > + for (i = 0; i < mt_slots[type]; i++) { > + if (!slots_a[i] && !slots_b[i]) > + break; > + > + if (!slots_a[i] || !slots_b[i]) { > + pr_err("The number of slots is different.\n"); > + return -1; > + } > + > + /* Do not compare addresses in slots. */ > + ((unsigned long *)slots_a)[i] &= MAPLE_NODE_MASK; > + ((unsigned long *)slots_b)[i] &= MAPLE_NODE_MASK; > + } > + > +cmp: > + /* > + * Compare all contents of two nodes, including parent (except address), > + * slots (except address), pivots, gaps and metadata. > + */ > + return memcmp(&a, &b, sizeof(struct maple_node)); > +} > + > +/* > + * Compare two trees and return 0 if they are the same, non-zero otherwise. > + */ > +static int __init compare_tree(struct maple_tree *mt_a, struct maple_tree *mt_b) > +{ > + MA_STATE(mas_a, mt_a, 0, 0); > + MA_STATE(mas_b, mt_b, 0, 0); > + > + if (mt_a->ma_flags != mt_b->ma_flags) { > + pr_err("The flags of the two trees are different.\n"); > + return -1; > + } > + > + mas_dfs_preorder(&mas_a); > + mas_dfs_preorder(&mas_b); > + > + if (mas_is_ptr(&mas_a) || mas_is_ptr(&mas_b)) { > + if (!(mas_is_ptr(&mas_a) && mas_is_ptr(&mas_b))) { > + pr_err("One is MAS_ROOT and the other is not.\n"); > + return -1; > + } > + return 0; > + } > + > + while (!mas_is_none(&mas_a) || !mas_is_none(&mas_b)) { > + > + if (mas_is_none(&mas_a) || mas_is_none(&mas_b)) { > + pr_err("One is MAS_NONE and the other is not.\n"); > + return -1; > + } > + > + if (mas_a.min != mas_b.min || > + mas_a.max != mas_b.max) { > + pr_err("mas->min, mas->max do not match.\n"); > + return -1; > + } > + > + if (compare_node(mas_a.node, mas_b.node)) { > + pr_err("The contents of nodes %p and %p are different.\n", > + mas_a.node, mas_b.node); > + mt_dump(mt_a, mt_dump_dec); > + mt_dump(mt_b, mt_dump_dec); > + return -1; > + } > + > + mas_dfs_preorder(&mas_a); > + mas_dfs_preorder(&mas_b); > + } > + > + return 0; > +} > + > +static noinline void __init check_mt_dup(struct maple_tree *mt) > +{ > + DEFINE_MTREE(new); > + int i, j, ret, count = 0; > + > + /* stored in the root pointer*/ > + mt_init_flags(&tree, 0); > + mtree_store_range(&tree, 0, 0, xa_mk_value(0), GFP_KERNEL); > + mt_dup(&tree, &new, GFP_KERNEL); > + mt_validate(&new); > + if (compare_tree(&tree, &new)) > + MT_BUG_ON(&new, 1); > + > + mtree_destroy(&tree); > + mtree_destroy(&new); > + > + for (i = 0; i < 1000; i += 3) { > + if (i & 1) > + mt_init_flags(&tree, 0); > + else > + mt_init_flags(&tree, MT_FLAGS_ALLOC_RANGE); > + > + for (j = 0; j < i; j++) { > + mtree_store_range(&tree, j * 10, j * 10 + 5, > + xa_mk_value(j), GFP_KERNEL); > + } Storing in this way is probably not checking a full tree. I think it's important to check the full tree/full nodes since you have changes to detect the metadata. > + > + ret = mt_dup(&tree, &new, GFP_KERNEL); > + MT_BUG_ON(&new, ret != 0); > + mt_validate(&new); > + if (compare_tree(&tree, &new)) > + MT_BUG_ON(&new, 1); > + > + mtree_destroy(&tree); > + mtree_destroy(&new); > + } > + > + /* Test memory allocation failed. */ > + for (i = 0; i < 1000; i += 3) { > + if (i & 1) > + mt_init_flags(&tree, 0); > + else > + mt_init_flags(&tree, MT_FLAGS_ALLOC_RANGE); > + > + for (j = 0; j < i; j++) { > + mtree_store_range(&tree, j * 10, j * 10 + 5, > + xa_mk_value(j), GFP_KERNEL); > + } > + > + mt_set_non_kernel(50); It may be worth while allowing more/less than 50 allocations. > + ret = mt_dup(&tree, &new, GFP_NOWAIT); > + mt_set_non_kernel(0); > + if (ret != 0) { > + MT_BUG_ON(&new, ret != -ENOMEM); > + count++; > + mtree_destroy(&tree); > + continue; > + } > + > + mt_validate(&new); > + if (compare_tree(&tree, &new)) > + MT_BUG_ON(&new, 1); > + > + mtree_destroy(&tree); > + mtree_destroy(&new); > + } > + > + /* pr_info("mt_dup() fail %d times\n", count); */ > + BUG_ON(!count); > +} > + > extern void test_kmem_cache_bulk(void); > > void farmer_tests(void) > @@ -35904,6 +36102,10 @@ void farmer_tests(void) > check_null_expand(&tree); > mtree_destroy(&tree); > > + mt_init_flags(&tree, 0); > + check_mt_dup(&tree); > + mtree_destroy(&tree); > + > /* RCU testing */ > mt_init_flags(&tree, 0); > check_erase_testset(&tree); > -- > 2.20.1 > >