]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
maple_tree: Code clean up
authorLiam R. Howlett <Liam.Howlett@Oracle.com>
Thu, 19 Nov 2020 17:15:22 +0000 (12:15 -0500)
committerLiam R. Howlett <Liam.Howlett@Oracle.com>
Tue, 5 Jan 2021 17:31:26 +0000 (12:31 -0500)
Signed-off-by: Liam R. Howlett <Liam.Howlett@Oracle.com>
lib/maple_tree.c
lib/test_maple_tree.c

index 02505ede97db08c2d06955b8521435300abc8c68..63d4bc2e00c1a09b0ee59a26155c44874b6d6e3f 100644 (file)
@@ -60,7 +60,7 @@ static const unsigned char mt_min_slots[] = {
 };
 #define mt_min_slot_count(x) mt_min_slots[mte_node_type(x)]
 
-#define MAPLE_BIG_NODE_SLOTS   (MAPLE_RANGE64_SLOTS* 2 + 2)
+#define MAPLE_BIG_NODE_SLOTS   (MAPLE_RANGE64_SLOTS * 2 + 2)
 
 struct maple_big_node {
        struct maple_pnode *parent;
@@ -123,6 +123,7 @@ static unsigned int mt_height(const struct maple_tree *mt)
 static void mas_set_height(struct ma_state *mas)
 {
        unsigned int new_flags = mas->tree->ma_flags;
+
        new_flags &= ~MAPLE_HEIGHT_MASK;
        new_flags |= mas->depth << MAPLE_HEIGHT_OFFSET;
        mas->tree->ma_flags = new_flags;
@@ -821,7 +822,8 @@ parent_is_root:
 
        if (!max || min == ULONG_MAX) {
                if (mas->node == a_enode) {
-                       printk("Failed on node %p (%p)\n", mas_mn(mas), a_enode);
+                       pr_err("Failed on node %p (%p)\n", mas_mn(mas),
+                              a_enode);
                        //FIXME: Restart and retry if the lock is held.
                        MT_BUG_ON(mas->tree, mas->node == a_enode);
                }
@@ -929,12 +931,13 @@ static inline void mas_alloc_nodes(struct ma_state *mas, gfp_t gfp)
 
        node = mas->alloc;
        while (requested) {
-               void **slots = (void**)&node->slot;
+               void **slots = (void **)&node->slot;
                unsigned int max_req = MAPLE_NODE_SLOTS - 1;
 
                if (node->slot[0]) {
                        unsigned int offset = node->node_count + 1;
-                       slots = (void**)&node->slot[offset];
+
+                       slots = (void **)&node->slot[offset];
                        max_req -= offset;
                }
 
@@ -944,7 +947,7 @@ static inline void mas_alloc_nodes(struct ma_state *mas, gfp_t gfp)
                        goto nomem;
 
                node->node_count += count;
-               if (slots == (void**)&node->slot)
+               if (slots == (void **)&node->slot)
                        node->node_count--; // zero indexed.
 
                success += count;
@@ -1135,7 +1138,7 @@ ma_max_gap(struct maple_node *node, unsigned long *gaps, enum maple_type mt,
                        max_gap = gaps[i];
                        *offset = i;
                }
-       } while(i--);
+       } while (i--);
 
        return max_gap;
 }
@@ -1145,9 +1148,10 @@ ma_max_gap(struct maple_node *node, unsigned long *gaps, enum maple_type mt,
  */
 static inline unsigned long mas_max_gap(struct ma_state *mas)
 {
-       unsigned long *gaps;//, *pivots;
+       unsigned long *gaps;
        unsigned char offset;
        enum maple_type mt;
+
        if (mte_is_leaf(mas->node))
                return mas_leaf_max_gap(mas);
 
@@ -1203,10 +1207,10 @@ ascend:
                return;
 
        if (offset != meta_offset) {
-           if (meta_gap > new)
-               return;
+               if (meta_gap > new)
+                       return;
 
-           ma_set_meta_gap(pnode, pmt, offset);
+               ma_set_meta_gap(pnode, pmt, offset);
        } else if (new < meta_gap) {
                meta_offset = 15;
                new = ma_max_gap(pnode, pgaps, pmt, &meta_offset);
@@ -1531,7 +1535,7 @@ static inline void mas_mab_cp(struct ma_state *mas, unsigned char mas_start,
 
        memcpy(b_node->slot + mab_start,
               slots + mas_start,
-              sizeof(void*) * (j - mab_start));
+              sizeof(void *) * (j - mab_start));
 
        if (!mte_is_leaf(mas->node) && mt_is_alloc(mas->tree)) {
                gaps = ma_gaps(node, mt);
@@ -1571,12 +1575,13 @@ static inline void mab_mas_cp(struct maple_big_node *b_node,
        }
 
        memcpy(slots, b_node->slot + mab_start,
-              sizeof(void*) * (i - mab_start));
+              sizeof(void *) * (i - mab_start));
 
        if (!ma_is_leaf(mt) && mt_is_alloc(mas->tree)) {
                unsigned long max_gap = 0;
                unsigned char offset = 15, tmp;
                unsigned char end = i - mab_start;
+
                gaps = ma_gaps(mas_mn(mas), mt);
                for (tmp = 0; tmp < end; tmp++) {
                        gaps[tmp] = b_node->gap[mab_start + tmp];
@@ -1613,8 +1618,7 @@ static inline void mas_descend_adopt(struct ma_state *mas)
        mas_dup_state(&next[0], mas);
 
 
-       while (!mte_is_leaf(list[0].node))
-       {
+       while (!mte_is_leaf(list[0].node)) {
                n = 0;
                for (i = 0; i < 3; i++) {
                        if (mas_is_none(&list[i]))
@@ -1920,6 +1924,7 @@ static inline bool mast_cousin_rebalance_right(struct maple_subtree_state *mast)
 {
        struct maple_enode *old_l = mast->orig_l->node;
        struct maple_enode *old_r = mast->orig_r->node;
+
        MA_STATE(tmp, mast->orig_r->tree, mast->orig_r->index, mast->orig_r->last);
 
        mas_dup_state(&tmp, mast->orig_r);
@@ -2959,7 +2964,7 @@ static inline bool mas_node_walk(struct ma_state *mas, enum maple_type type,
                goto dense;
        }
 
-       while(mas->offset < mt_slots[type]) {
+       while (mas->offset < mt_slots[type]) {
                pivot = _mas_safe_pivot(mas, pivots, mas->offset, type);
 
                if (!pivot && mas->offset) {
@@ -3200,16 +3205,17 @@ static inline bool mas_node_store(struct ma_state *mas, void *entry,
                new_end++;
                offset_end = offset;
        } else if (mas->last == mas->max) { // runs right to the end of the node.
-                       new_end = offset;
-                       offset_end = end + 1; // no data beyond this range.
+               new_end = offset;
+               offset_end = end + 1; // no data beyond this range.
        } else {
                unsigned long piv = 0;
+
                new_end++;
                do {
                        offset_end++;
                        new_end--;
                        piv = mas_logical_pivot(mas, pivots, offset_end, mt);
-               } while(piv <= mas->last);
+               } while (piv <= mas->last);
        }
 
        if (min < mas->index) // new range starts within a range.
@@ -3239,7 +3245,7 @@ static inline bool mas_node_store(struct ma_state *mas, void *entry,
        dst_slots = ma_slots(newnode, mt);
        // Copy from start to insert point
        memcpy(dst_pivots, pivots, sizeof(unsigned long) * (offset + 1));
-       memcpy(dst_slots, slots, sizeof(void*) * (offset + 1));
+       memcpy(dst_slots, slots, sizeof(void *) * (offset + 1));
        dst_offset = offset;
 
        // Handle insert of new range starting after old range
@@ -3378,7 +3384,7 @@ static inline void *_mas_store(struct ma_state *mas, void *entry, bool overwrite
 
        if (!entry) {
                enum maple_type mt = mte_node_type(mas->node);
-               unsigned long *pivots = ma_pivots(mas_mn(mas),mt);
+               unsigned long *pivots = ma_pivots(mas_mn(mas), mt);
                void **slots = ma_slots(mas_mn(mas), mt);
                unsigned char offset_end = mas->offset;
 
@@ -3490,7 +3496,7 @@ restart_prev_node:
                mt = mte_node_type(mas->node);
                slots = ma_slots(mas_mn(mas), mt);
                offset = mt_slots[mt];
-               do {} while(!mas_get_slot(mas, --offset));
+               do {} while (!mas_get_slot(mas, --offset));
                mas->max = mas_safe_pivot(mas, offset);
        }
 
@@ -3572,7 +3578,7 @@ ascend_again:
                pivot = mas_safe_pivot(mas, offset);
        }
 
-       mas->node = mas_slot(mas, slots,offset);
+       mas->node = mas_slot(mas, slots, offset);
        mas->min = prev_piv + 1;
        mas->max = pivot;
        return mas->max;
@@ -4984,10 +4990,10 @@ int mas_entry_count(struct ma_state *mas, unsigned long nr_entries)
        nr_nodes = max(nr_entries, nr_entries * 2 + 1);
 
        if (!mt_is_alloc(mas->tree))
-           nonleaf_cap = MAPLE_RANGE64_SLOTS - 2;
+               nonleaf_cap = MAPLE_RANGE64_SLOTS - 2;
 
        // Leaves
-       nr_nodes = DIV_ROUND_UP(nr_nodes , MAPLE_RANGE64_SLOTS - 1);
+       nr_nodes = DIV_ROUND_UP(nr_nodes, MAPLE_RANGE64_SLOTS - 1);
        // Internal nodes.
        nr_nodes += DIV_ROUND_UP(nr_nodes, nonleaf_cap);
        mas_node_count(mas, nr_nodes);
@@ -5025,9 +5031,9 @@ void mas_destroy(struct ma_state *mas)
 
                __mas_walk(mas, &range_min, &range_max);
                end = mas_data_end(mas) + 1;
-               if (end < mt_min_slot_count(mas->node) - 1) {
+               if (end < mt_min_slot_count(mas->node) - 1)
                        mas_destroy_rebalance(mas, end);
-               }
+
                mas->mas_flags &= ~MA_STATE_REBALANCE;
        }
        mas->mas_flags &= ~MA_STATE_BULK;
@@ -5036,7 +5042,7 @@ void mas_destroy(struct ma_state *mas)
                node = mas->alloc;
                mas->alloc = mas->alloc->slot[0];
                if (node->node_count > 0)
-                       mt_free_bulk(node->node_count, (void**)&node->slot[1]);
+                       mt_free_bulk(node->node_count, (void **)&node->slot[1]);
                kmem_cache_free(maple_node_cache, node);
        }
        mas->alloc = NULL;
@@ -5486,7 +5492,7 @@ static inline void mas_dfs_postorder(struct ma_state *mas, unsigned long max)
 
 void mt_validate_nulls(struct maple_tree *mt)
 {
-       void *entry, *last = (void*)1;
+       void *entry, *last = (void *)1;
        unsigned char end, offset = 0;
        void **slots;
        MA_STATE(mas, mt, 0, 0);
@@ -5495,9 +5501,8 @@ void mt_validate_nulls(struct maple_tree *mt)
        if (mas_is_none(&mas) || (mas.node == MAS_ROOT))
                return;
 
-       while (!mte_is_leaf(mas.node)) {
+       while (!mte_is_leaf(mas.node))
                mas_descend(&mas);
-       }
 
        slots = ma_slots(mte_to_node(mas.node), mte_node_type(mas.node));
        end = mas_data_end(&mas);
@@ -5520,7 +5525,7 @@ void mt_validate_nulls(struct maple_tree *mt)
                } else
                        offset++;
 
-       } while(!mas_is_none(&mas));
+       } while (!mas_is_none(&mas));
 }
 /*
  * validate a maple tree by checking:
index ed68357b99d5bb9934739dc5bf9396b55f6c1585..59a1561052aa33bd736a5d255524c6465eb6b640 100644 (file)
@@ -88,8 +88,9 @@ static noinline void check_load(struct maple_tree *mt, unsigned long index,
                                void *ptr)
 {
        void *ret = mtree_test_load(mt, index);
+
        if (ret != ptr)
-               printk("Load %lu returned %p expect %p\n", index, ret, ptr);
+               pr_err("Load %lu returned %p expect %p\n", index, ret, ptr);
        MT_BUG_ON(mt, ret != ptr);
 }
 
@@ -297,6 +298,7 @@ static noinline void check_new_node(struct maple_tree *mt)
        MT_BUG_ON(mt, !mas_nomem(&mas, GFP_KERNEL));
        for (i = total; i > 0; i--) {
                unsigned int e = 0; // expected node_count
+
                if (i >= 35)
                        e = i - 35;
                else if (i >= 5)
@@ -360,7 +362,7 @@ static noinline void check_new_node(struct maple_tree *mt)
        MT_BUG_ON(mt, !mas.alloc);
        i = 1;
        smn = mas.alloc;
-       while(i < total) {
+       while (i < total) {
                for (j = 0; j < MAPLE_ALLOC_SLOTS; j++) {
                        i++;
                        MT_BUG_ON(mt, !smn->slot[j]);
@@ -494,7 +496,7 @@ static noinline void check_seq(struct maple_tree *mt, unsigned long max,
                for (j = 0; j <= i; j++)
                        check_index_load(mt, j);
 
-               if(i)
+               if (i)
                        MT_BUG_ON(mt, !mt_height(mt));
                check_load(mt, i + 1, NULL);
        }
@@ -1098,6 +1100,7 @@ int mas_ce2_over_count(struct ma_state *mas_start, struct ma_state *mas_end,
        } else { // !null_entry (store)
                bool esplit = e_max > mas_end->last;
                bool ssplit = s_min != mas_start->index;
+
                if (s_entry && e_entry) {
                        if (esplit && ssplit)
                                count--;
@@ -35225,7 +35228,7 @@ static void check_dfs_preorder(struct maple_tree *mt)
        do {
                count++;
                mas_dfs_preorder(&mas);
-       } while(!mas_is_none(&mas));
+       } while (!mas_is_none(&mas));
        // 68 + MAS_START = 69 + 1 for no jitter
        //printk("count %lu\n", count);
        MT_BUG_ON(mt, count != 70);
@@ -35238,7 +35241,7 @@ static void check_dfs_preorder(struct maple_tree *mt)
        do {
                count++;
                mas_dfs_preorder(&mas);
-       } while(!mas_is_none(&mas));
+       } while (!mas_is_none(&mas));
        //printk("count %lu\n", count);
        // 71 + MAS_START = 72 + 1 for no jitter
        MT_BUG_ON(mt, count != 72);
@@ -35251,7 +35254,7 @@ static void check_dfs_preorder(struct maple_tree *mt)
        do {
                count++;
                mas_dfs_preorder(&mas);
-       } while(!mas_is_none(&mas));
+       } while (!mas_is_none(&mas));
        // 71 + MAS_START = 72
        //printk("count %lu\n", count);
        MT_BUG_ON(mt, count != 77);
@@ -35262,7 +35265,7 @@ static void check_dfs_preorder(struct maple_tree *mt)
        nr_tallocated = 0;
        mt_set_non_kernel(200);
        mas_entry_count(&mas, max);
-       for(count = 0; count <= max; count++) {
+       for (count = 0; count <= max; count++) {
                mas.index = mas.last = count;
                mas_store(&mas, xa_mk_value(count));
                MT_BUG_ON(mt, mas_is_err(&mas));
@@ -35278,9 +35281,9 @@ static void check_dfs_preorder(struct maple_tree *mt)
 
 static noinline void bench_slot_store(struct maple_tree *mt)
 {
-       int i, brk = 105, max = 1040, brk_start= 100, count = 20000000;
+       int i, brk = 105, max = 1040, brk_start = 100, count = 20000000;
 
-       for (i = 0; i < max; i+=10)
+       for (i = 0; i < max; i += 10)
                mtree_store_range(mt, i, i + 5, xa_mk_value(i), GFP_KERNEL);
 
        for (i = 0; i < count; i++) {
@@ -35294,7 +35297,7 @@ static noinline void bench_node_store(struct maple_tree *mt)
 {
        int i, overwrite = 76, max = 240, count = 20000000;
 
-       for (i = 0; i < max; i+=10)
+       for (i = 0; i < max; i += 10)
                mtree_store_range(mt, i, i + 5, xa_mk_value(i), GFP_KERNEL);
 
        for (i = 0; i < count; i++) {
@@ -35328,7 +35331,7 @@ static noinline void check_forking(struct maple_tree *mt)
        mas.index = 0;
        mas.last = 0;
        if (mas_entry_count(&newmas, nr_entries)) {
-               printk("OOM!");
+               pr_err("OOM!");
                BUG_ON(1);
        }
        mas_for_each(&mas, val, ULONG_MAX) {
@@ -35379,6 +35382,9 @@ static noinline void bench_forking(struct maple_tree *mt)
        }
 }
 
+//#define BENCH_SLOT_STORE
+//#define BENCH_NODE_STORE
+//#define BENCH_FORK
 static DEFINE_MTREE(tree);
 static int maple_tree_seed(void)
 {
@@ -35389,19 +35395,22 @@ static int maple_tree_seed(void)
 
        pr_info("\nTEST STARTING\n\n");
 
-#if 0
+#if BENCH_SLOT_STORE
+#define BENCH
        mtree_init(&tree, MAPLE_ALLOC_RANGE);
        bench_slot_store(&tree);
        mtree_destroy(&tree);
        goto skip;
 #endif
-#if 0
+#if BENCH_NODE_STORE
+#define BENCH
        mtree_init(&tree, MAPLE_ALLOC_RANGE);
        bench_node_store(&tree);
        mtree_destroy(&tree);
        goto skip;
 #endif
-#if 0
+#if BENCH_FORK
+#define BENCH
        mtree_init(&tree, MAPLE_ALLOC_RANGE);
        bench_forking(&tree);
        mtree_destroy(&tree);
@@ -35582,7 +35591,9 @@ static int maple_tree_seed(void)
        check_node_overwrite(&tree);
        mtree_destroy(&tree);
 
+#if BENCH
 skip:
+#endif
        rcu_barrier();
        pr_info("maple_tree: %u of %u tests passed\n", maple_tree_tests_passed,
                        maple_tree_tests_run);