From dbe055f855081c495865720f7614c1c93ac1860b Mon Sep 17 00:00:00 2001 From: "Liam R. Howlett" Date: Wed, 31 Jul 2019 22:02:24 -0400 Subject: [PATCH] maple_tree: checkpatch fixes Signed-off-by: Liam R. Howlett --- include/linux/maple_tree.h | 38 +++--- lib/maple_tree.c | 191 ++++++++++++++++--------------- lib/test_maple_tree.c | 122 ++++++++++++-------- tools/testing/radix-tree/maple.c | 1 + 4 files changed, 195 insertions(+), 157 deletions(-) diff --git a/include/linux/maple_tree.h b/include/linux/maple_tree.h index e960b091f921..9a5821a67ebd 100644 --- a/include/linux/maple_tree.h +++ b/include/linux/maple_tree.h @@ -4,8 +4,8 @@ /* * Maple Tree - An RCU-safe adaptive tree for storing ranges * Copyright (c) 2018 Oracle - * Authors: Liam R. Howlett - * Matthew Wilcox + * Authors: Liam R. Howlett + * Matthew Wilcox */ #include @@ -156,7 +156,7 @@ enum maple_type { struct maple_tree { spinlock_t ma_lock; unsigned int ma_flags; - void __rcu * ma_root; + void __rcu *ma_root; }; #define MTREE_INIT(name, flags) { \ @@ -168,16 +168,17 @@ struct maple_tree { #define DEFINE_MTREE(name) \ struct maple_tree name = MTREE_INIT(name, 0) -#define mtree_lock(mt) spin_lock(&mt->ma_lock); -#define mtree_unlock(mt) spin_unlock(&mt->ma_lock); +#define mtree_lock(mt) spin_lock(&mt->ma_lock) +#define mtree_unlock(mt) spin_unlock(&mt->ma_lock) -void mtree_init(struct maple_tree *, unsigned int ma_flags); -void *mtree_load(struct maple_tree *, unsigned long index); -int mtree_insert(struct maple_tree *, unsigned long index, void *entry, gfp_t); -int mtree_insert_range(struct maple_tree *, unsigned long first, - unsigned long last, void *entry, gfp_t); -int mtree_erase(struct maple_tree *, unsigned long index); -void mtree_destroy(struct maple_tree *); +void mtree_init(struct maple_tree *mt, unsigned int ma_flags); +void *mtree_load(struct maple_tree *mt, unsigned long index); +int mtree_insert(struct maple_tree *mt, unsigned long index, + void *entry, gfp_t gfp); +int mtree_insert_range(struct maple_tree mt*, unsigned long first, + unsigned long last, void *entry, gfp_t gfp); +int mtree_erase(struct maple_tree mt*, unsigned long index); +void mtree_destroy(struct maple_tree mt*); /** * mtree_empty() - Determine if a tree has any present entries. @@ -241,7 +242,8 @@ struct ma_state { #define MAS_START ((struct maple_enode *)1UL) #define MAS_ROOT ((struct maple_enode *)5UL) #define MAS_NONE ((struct maple_enode *)9UL) -#define MA_ERROR(err) ((struct maple_enode *)(((unsigned long)err << 2) | 2UL)) +#define MA_ERROR(err) \ + ((struct maple_enode *)(((unsigned long)err << 2) | 2UL)) #define MA_STATE(name, mt, first, end) \ struct ma_state name = { \ @@ -253,11 +255,11 @@ struct ma_state { .max = ULONG_MAX, \ } -void *mas_walk(struct ma_state *); -void *mas_store(struct ma_state *, void *entry); -void *mas_find(struct ma_state *, unsigned long max); +void *mas_walk(struct ma_state *mas); +void *mas_store(struct ma_state *mas, void *entry); +void *mas_find(struct ma_state *mas, unsigned long max); -bool mas_nomem(struct ma_state *, gfp_t); -void mas_pause(struct ma_state *); +bool mas_nomem(struct ma_state *mas, gfp_t gfp); +void mas_pause(struct ma_state *mas); void maple_tree_init(void); #endif diff --git a/lib/maple_tree.c b/lib/maple_tree.c index 140735796602..38929095837c 100644 --- a/lib/maple_tree.c +++ b/lib/maple_tree.c @@ -12,9 +12,9 @@ #include #define MA_ROOT_PARENT 1 -#define ma_parent_ptr(x) ((struct maple_pnode*)(x)) -#define ma_mnode_ptr(x) ((struct maple_node*)(x)) -#define ma_enode_ptr(x) ((struct maple_enode*)(x)) +#define ma_parent_ptr(x) ((struct maple_pnode *)(x)) +#define ma_mnode_ptr(x) ((struct maple_node *)(x)) +#define ma_enode_ptr(x) ((struct maple_enode *)(x)) /** * mas_for_each() - Iterate over a range of the maple tree. @@ -130,6 +130,7 @@ static struct maple_node *mt_alloc_one(gfp_t gfp) static void mt_free_rcu(struct rcu_head *head) { struct maple_node *node = container_of(head, struct maple_node, rcu); + kmem_cache_free(maple_node_cache, node); } @@ -287,7 +288,7 @@ static inline enum maple_type mt_parent_enum(struct ma_state *mas, if (!ma_is_root(mas->node)) { parent = (unsigned long) mt_to_node(node)->parent; slot_shift = mt_parent_shift(parent); - parent &= (1 << slot_shift ) - 1; + parent &= (1 << slot_shift) - 1; } if (mt_is_alloc(mas->tree)) @@ -362,6 +363,7 @@ static inline struct maple_node *mt_parent(const struct maple_enode *node) static inline bool mt_dead_node(const struct maple_enode *enode) { struct maple_node *parent, *node = mt_to_node(enode); + parent = mt_parent(enode); return (parent == node); } @@ -375,6 +377,7 @@ static inline int ma_get_node_alloc_cnt(const struct maple_node *node) { int ret = 1; int slot = 0; + while (slot < MAPLE_NODE_SLOTS) { if (!node->slot[slot]) return ret; @@ -465,6 +468,7 @@ static inline unsigned long ma_get_safe_pivot(const struct ma_state *mas, unsigned char slot) { enum maple_type type = mt_node_type(mas->node); + if (slot >= mt_pivots[type]) return mas->max; return _ma_get_pivot(mas->node, slot, type); @@ -511,7 +515,6 @@ static inline void ma_set_pivot(struct maple_enode *mn, unsigned char slot, (&mt_to_node(mn)->mr32)->pivot[slot] = val; break; } - return; } static inline void ma_cp_pivot(struct maple_enode *dst, unsigned char dloc, struct maple_enode *src, unsigned long sloc) @@ -683,6 +686,7 @@ static inline void ma_set_gap(const struct maple_enode *mn, unsigned char gap, unsigned long val) { enum maple_type type = mt_node_type(mn); + switch (type) { default: break; @@ -778,6 +782,7 @@ static inline struct maple_node *ma_next_alloc(struct ma_state *ms) } else { unsigned char slot = cnt - 2; struct maple_node *zero = mn; + if (cnt - 1 >= MAPLE_NODE_SLOTS) { slot /= MAPLE_NODE_SLOTS; slot--; @@ -809,6 +814,7 @@ static inline void ma_push_node(struct ma_state *mas, struct maple_enode *used) node->slot[cnt] = reuse; } else { struct maple_node *smn; + cnt--; smn = node->slot[cnt/15]; smn->slot[cnt % 15] = reuse; @@ -930,12 +936,11 @@ static inline struct maple_enode *mas_start(struct ma_state *mas) mas->node = MAS_ROOT; ma_set_slot(mas, MAPLE_NODE_SLOTS); return NULL; - } else { - struct maple_enode *root = mt_safe_root( - mas->tree->ma_root); - mas->max = mt_node_max(root); - return root; } + struct maple_enode *root = mt_safe_root( + mas->tree->ma_root); + mas->max = mt_node_max(root); + return root; } return mas->node; } @@ -956,12 +961,10 @@ static inline unsigned char ma_data_end(const struct ma_state *mas, prev_piv = *last_piv; } - if (!_ma_get_rcu_slot(mn, data_end, type)) { + if (!_ma_get_rcu_slot(mn, data_end, type)) data_end--; - } else { + else *last_piv = mas->max; - } - return data_end; } @@ -980,9 +983,9 @@ static inline unsigned char ma_no_dense_calc_split(struct ma_state *mas, unsigned long last_pivot; unsigned char data_end = ma_data_end(mas, type, &last_pivot); - *left = (struct maple_enode*)ma_next_alloc(mas); + *left = (struct maple_enode *)ma_next_alloc(mas); *left = mt_mk_node(ma_mnode_ptr(*left), type); - *right = (struct maple_enode*)ma_next_alloc(mas); + *right = (struct maple_enode *)ma_next_alloc(mas); *right = mt_mk_node(ma_mnode_ptr(*right), type); if (ma_is_root(mas->node)) @@ -1040,12 +1043,12 @@ static inline unsigned char ma_dense_calc_split(struct ma_state *mas, if (ma_is_root(mas->node)) { i = half; - *left = (struct maple_enode*)ma_next_alloc(mas); - *right = (struct maple_enode*)ma_next_alloc(mas); + *left = (struct maple_enode *)ma_next_alloc(mas); + *right = (struct maple_enode *)ma_next_alloc(mas); goto even_split; } - *left = (struct maple_enode*)ma_next_alloc(mas); + *left = (struct maple_enode *)ma_next_alloc(mas); for (i = 0; i < data_end; i++) { max = ma_get_pivot(mas->node, i); if ((max - min) > 15) { @@ -1058,7 +1061,7 @@ static inline unsigned char ma_dense_calc_split(struct ma_state *mas, if (i >= data_end) { *left = mt_mk_node(ma_mnode_ptr(*left), maple_dense); if (mas->last >= mas->min + mt_max[type]) { - *right = (struct maple_enode*)ma_next_alloc(mas); + *right = (struct maple_enode *)ma_next_alloc(mas); *right = mt_mk_node(ma_mnode_ptr(*right), type); } if (!i) @@ -1066,7 +1069,7 @@ static inline unsigned char ma_dense_calc_split(struct ma_state *mas, return i; } - *right = (struct maple_enode*)ma_next_alloc(mas); + *right = (struct maple_enode *)ma_next_alloc(mas); if (i >= half) { *left = mt_mk_node(ma_mnode_ptr(*left), maple_dense); *right = mt_mk_node(ma_mnode_ptr(*right), type); @@ -1106,15 +1109,12 @@ static inline void ma_dense_cp(struct ma_state *mas, struct ma_cp *cp) unsigned long min, piv; void *ptr; - if (!sloc) { + if (!sloc) min = mas->min; - } - else { + else min = ma_get_pivot(cp->src, sloc - 1) + 1; - } piv = min; - while (sloc <= cp->src_end) { unsigned long end; unsigned char slot, end_slot; @@ -1130,9 +1130,8 @@ static inline void ma_dense_cp(struct ma_state *mas, struct ma_cp *cp) slot = piv - min; end_slot = end - min; - for ( ; slot <= end_slot; slot++) { + for (; slot <= end_slot; slot++) ma_set_rcu_slot(cp->dst, slot, ptr); - } piv = end + 1; sloc++; @@ -1201,14 +1200,17 @@ static inline unsigned long ma_max_gap(struct ma_state *mas, { unsigned long max_gap = 0; unsigned char i; + for (i = 0; i < mt_slot_count(mas->node); i++) { unsigned long gap; + gap = ma_get_gap(mas->node, i); - if ( gap > max_gap) { + if (gap > max_gap) { *slot = i; max_gap = gap; } } + return max_gap; } static inline void ma_parent_gap(struct ma_state *mas, unsigned char slot, @@ -1225,10 +1227,8 @@ static inline void ma_parent_gap(struct ma_state *mas, unsigned char slot, max_gap = ma_max_gap(mas, &max_slot); - if (slot == max_slot || max_gap < new) { - unsigned char pslot = mt_parent_slot(mas->node); - ma_parent_gap(mas, pslot, new); - } + if (slot == max_slot || max_gap < new) + ma_parent_gap(mas, mt_parent_slot(mas->node), new); } /* Private */ @@ -1258,13 +1258,13 @@ static inline void ma_update_gap(struct ma_state *mas) // First non-null entry in mas->node -static inline unsigned long -mas_first_node (struct ma_state *mas, unsigned long max) +static inline unsigned long mas_first_node(struct ma_state *mas, + unsigned long max) { unsigned char slot = ma_get_slot(mas) - 1; unsigned char count = mt_slot_count(mas->node); - while(++slot < count) { + while (++slot < count) { struct maple_enode *mn; unsigned long pivot; @@ -1288,13 +1288,12 @@ no_entry: return mas->max; } -static inline unsigned long -mas_first_entry (struct ma_state *mas, unsigned long max) +static inline unsigned long mas_first_entry(struct ma_state *mas, + unsigned long max) { unsigned long pivot; - while (1) - { + while (1) { pivot = mas_first_node(mas, max); if (mas->node == MAS_NONE) return pivot; @@ -1307,8 +1306,6 @@ mas_first_entry (struct ma_state *mas, unsigned long max) } -static inline void mas_prev_entry (struct ma_state *mas) { -} void ma_destroy_walk(struct maple_enode *mn) { struct maple_enode *node; @@ -1333,7 +1330,7 @@ void ma_destroy_walk(struct maple_enode *mn) mt_free(mt_to_node(mn)); } -static inline void ma_copy (struct ma_state *mas, struct ma_cp *cp) +static inline void ma_copy(struct ma_state *mas, struct ma_cp *cp) { unsigned char sloc = cp->src_start; // src location unsigned char dloc = cp->dst_start; // dst location @@ -1444,6 +1441,7 @@ static inline void _mt_replace(struct ma_state *mas, bool free, bool push) prev = mas->tree->ma_root; } else { enum maple_type ptype = mt_parent_enum(mas, mas->node); + parent = mt_mk_node(mt_parent(mas->node), ptype); slot = mt_parent_slot(mas->node); prev = ma_get_rcu_slot(parent, slot); @@ -1495,7 +1493,6 @@ static inline void mas_partial_copy(struct ma_state *mas, mt_to_node(cp.dst)->parent = mt_to_node(cp.src)->parent; mas->node = cp.dst; - return; } static inline void ma_gap_link(struct ma_state *mas, struct maple_enode *parent, @@ -1558,13 +1555,14 @@ static inline int ma_split(struct ma_state *mas, unsigned char slot, if (ma_is_root(mas->node)) { old_parent = full; - if(mt_is_alloc(mas->tree)) + if (mt_is_alloc(mas->tree)) ptype = maple_arange_64; else ptype = maple_range_64; p_slot = 0; } else { unsigned long last_pivot; + p_slot = mt_parent_slot(mas->node); ma_encoded_parent(mas); old_parent = mas->node; @@ -1598,6 +1596,7 @@ static inline int ma_split(struct ma_state *mas, unsigned char slot, // Copy the parents information if (!ma_is_root(full)) { unsigned long c_max = mas->max; + mas->max = p_max; // Copy the parent data except p_slot, which will later be // replaced. @@ -1626,7 +1625,6 @@ static inline int ma_split(struct ma_state *mas, unsigned char slot, pivot = mas->min + split - 1; } else { pivot = mt_node_max(left); - if (!p_slot || mt_node_type(left) == maple_dense) pivot += mas->min; else @@ -1645,6 +1643,7 @@ static inline int ma_split(struct ma_state *mas, unsigned char slot, if (mt_is_alloc(mas->tree)) { if (right) { unsigned long min = mas->min; + mas->node = right; ma_gap_link(mas, new_parent, p_slot + 1, mas->max); mas->min = min; @@ -1695,7 +1694,8 @@ static inline int ma_split(struct ma_state *mas, unsigned char slot, return split; } -static inline enum maple_type ma_ptype_leaf(struct ma_state *mas) { +static inline enum maple_type ma_ptype_leaf(struct ma_state *mas) +{ enum maple_type pt = mt_node_type(mas->node); switch (pt) { @@ -1716,7 +1716,7 @@ static inline enum maple_type ma_determine_type(struct ma_state *mas, { struct maple_enode *sibling; unsigned char sibling_slot = slot; - enum maple_type stype, mt = ma_ptype_leaf(mas);; + enum maple_type stype, mt = ma_ptype_leaf(mas); if (slot > 0) sibling_slot -= 1; @@ -1750,15 +1750,15 @@ static inline int _ma_add_dense(struct ma_state *mas, void *entry, // FIXME: Check entire range, not what we would insert this time. if (!overwrite) { - do + do { if (_ma_get_rcu_slot(mas->node, min++, this_type)) return 0; - while (min < max); + } while (min < max); } - do + do { ma_update_rcu_slot(mas->node, min++, entry); - while (min < max); + } while (min < max); if (max != mas->last - mas->min) { mas->index = mas->min + max + 1; @@ -1821,7 +1821,8 @@ static inline int _ma_add(struct ma_state *mas, void *entry, bool overwrite, } /* Dense node type */ if (!pivot_cnt) { - ret = _ma_add_dense(mas, entry, slot, this_type, overwrite, active); + ret = _ma_add_dense(mas, entry, slot, this_type, overwrite, + active); if (!ret) return ret; goto update_gap; @@ -1885,9 +1886,10 @@ static inline int _ma_add(struct ma_state *mas, void *entry, bool overwrite, } /* Check for splitting */ new_end += ret; - if (new_end > slot_cnt ) { + if (new_end > slot_cnt) { /* Not enough space in the node */ unsigned char split = ma_split(mas, slot, active); + if (mas_is_err(mas)) return 0; @@ -1955,11 +1957,12 @@ static inline int _ma_add(struct ma_state *mas, void *entry, bool overwrite, /* Write NULL entry */ ma_set_rcu_slot(mas->node, --slot, NULL); if (append) - wmb(); + wmb(); /* NULL needs to be written before pivot. */ ma_set_pivot(mas->node, slot, mas->index - 1); if (mt_is_alloc(mas->tree)) { unsigned long gap = mas->min; + if (slot > 0) gap = ma_get_pivot(mas->node, slot - 1); gap = (mas->index - 1) - gap; @@ -1970,7 +1973,7 @@ static inline int _ma_add(struct ma_state *mas, void *entry, bool overwrite, /* Write the entry */ ma_set_rcu_slot(mas->node, slot, entry); if (old_end == new_end + 1) // Append. - wmb(); + wmb(); /* Entry needs to be visible prior to pivot. */ if (slot < pivot_cnt) ma_set_pivot(mas->node, slot++, mas->last); @@ -2061,7 +2064,7 @@ static inline int mas_dead_node(struct ma_state *mas, unsigned long index); * Find the prev non-null entry at the same level in the tree. The prev value * will be mas->node[ma_get_slot(mas)] or MAS_NONE. */ -static inline void mas_prev_node (struct ma_state *mas, unsigned long min) +static inline void mas_prev_node(struct ma_state *mas, unsigned long min) { int level; unsigned char slot; @@ -2111,7 +2114,7 @@ restart_prev_node: level--; mas->node = mn; slot = ma_data_end(mas, mt_node_type(mn), &last_pivot); - } while(slot-- > 0); + } while (slot-- > 0); ascend: if (ma_is_root(mas->node)) @@ -2122,7 +2125,6 @@ ascend: no_entry: mas->node = MAS_NONE; - return; } /* @@ -2130,8 +2132,8 @@ no_entry: * will be mas->node[ma_get_slot(mas)] or MAS_NONE. * */ -static inline unsigned long -mas_next_node (struct ma_state *mas, unsigned long max) +static inline unsigned long mas_next_node(struct ma_state *mas, + unsigned long max) { int level; unsigned long start_piv; @@ -2200,8 +2202,8 @@ no_entry: } // Next node entry or return 0 on none. -static inline bool -mas_next_nentry (struct ma_state *mas, unsigned long max, unsigned long *piv) +static inline bool mas_next_nentry(struct ma_state *mas, unsigned long max, + unsigned long *piv) { unsigned long pivot = mas->min; unsigned char slot = ma_get_slot(mas); @@ -2238,8 +2240,8 @@ no_entry: return false; } -static inline unsigned long -mas_next_entry (struct ma_state *mas, unsigned long max) +static inline unsigned long mas_next_entry(struct ma_state *mas, + unsigned long max) { unsigned long pivot = max; @@ -2251,6 +2253,7 @@ mas_next_entry (struct ma_state *mas, unsigned long max) else { while (mas->node != MAS_NONE) { unsigned char p_slot; + if (mas_next_nentry(mas, max, &pivot)) break; @@ -2263,10 +2266,11 @@ mas_next_entry (struct ma_state *mas, unsigned long max) } return pivot; } -static inline unsigned long -mas_safe_next_entry (struct ma_state *mas, unsigned long max) +static inline unsigned long mas_safe_next_entry(struct ma_state *mas, + unsigned long max) { unsigned long piv; + retry: piv = mas_next_entry(mas, max); if (mas->node == MAS_NONE) @@ -2421,6 +2425,7 @@ next_slot: do { unsigned long this_gap; + if (!i) min = mas->min; else @@ -2452,7 +2457,7 @@ next: goto ascend; max = min - 1; - if (mas->index > max ) { + if (mas->index > max) { mas_set_err(mas, -EBUSY); return false; } @@ -2469,6 +2474,7 @@ next: if (!ma_is_leaf(type)) { //descend struct maple_enode *next; + next = _ma_get_rcu_slot(mas->node, i, type); mas->min = min; mas->max = max; @@ -2553,6 +2559,7 @@ next: i = ma_get_slot(mas); for (; i < pivot_cnt; i++) { unsigned long this_gap; + pivot = _ma_get_pivot(mas->node, i, type); if (i && !pivot) goto ascend; @@ -2590,6 +2597,7 @@ next: if (!ma_is_leaf(type)) { //descend struct maple_enode *next; + next = _ma_get_rcu_slot(mas->node, i, type); mas->min = min; mas->max = max; @@ -2658,7 +2666,6 @@ static inline bool __mas_walk(struct ma_state *mas) // Linear node. i = mas->index - mas->min; goto done; - break; } next = _ma_get_rcu_slot(mas->node, i, type); @@ -2684,7 +2691,7 @@ static inline int mas_dead_node(struct ma_state *mas, unsigned long index) { if (!mt_dead_node(mas->node)) return 0; - + mas->index = index; _mas_walk(mas); return 1; @@ -2712,7 +2719,7 @@ void *mas_find(struct ma_state *mas, unsigned long max) if (mas->node == MAS_START) { _mas_walk(mas); - do {} while(mas_dead_node(mas, mas->index)); + do {} while (mas_dead_node(mas, mas->index)); slot = ma_get_slot(mas); @@ -2833,13 +2840,11 @@ static inline int mas_replace_tree(struct ma_state *mas, void *new_entry) long l_node_cnt = -1, r_node_cnt = 0; unsigned int r_slot_cnt = 0, slot_cnt = 0; long node_cnt = 0, nodes = 0; - MA_STATE(r_mas, mas->tree, mas->last + 1, mas->last + 1); void *entry; struct maple_enode *r_node = NULL, *last = NULL; unsigned char r_slot = 0, slot; - - + MA_STATE(r_mas, mas->tree, mas->last + 1, mas->last + 1); slot_cnt = 1 + ma_get_slot(mas); ma_set_slot(mas, mt_parent_slot(mas->node)); while (mas->node != MAS_NONE) { @@ -2852,9 +2857,12 @@ static inline int mas_replace_tree(struct ma_state *mas, void *new_entry) if (ma_get_slot(&r_mas) != MAPLE_NODE_SLOTS) { unsigned long piv; - r_slot_cnt += ma_data_end(&r_mas, mt_node_type(r_mas.node), &piv); + + r_slot_cnt += ma_data_end(&r_mas, mt_node_type(r_mas.node), + &piv); if (piv != ULONG_MAX) { unsigned char p_slot = mt_parent_slot(r_mas.node); + r_node = r_mas.node; r_slot = ma_get_slot(&r_mas); r_slot_cnt -= r_slot; @@ -2888,6 +2896,7 @@ static inline int mas_replace_tree(struct ma_state *mas, void *new_entry) // Create a new tree. DEFINE_MTREE(new_tree); + MA_STATE(new_mas, &new_tree, 0, 0); new_mas.alloc = mas->alloc; @@ -3011,7 +3020,6 @@ static inline void mas_rev_awalk(struct ma_state *mas, unsigned long size) else last = mas->node; } - return; } static inline bool mas_skip_node(struct ma_state *mas); static inline void mas_awalk(struct ma_state *mas, unsigned long size) @@ -3033,7 +3041,6 @@ static inline void mas_awalk(struct ma_state *mas, unsigned long size) else last = mas->node; } - return; } static inline int ma_root_ptr(struct ma_state *mas, void *entry, @@ -3076,7 +3083,6 @@ static inline int ma_add(struct ma_state *mas, void *entry, bool overwrite, retry: leaf = _mas_walk(mas); slot = ma_get_slot(mas); - // Clean this node if (mas_coalesce(mas, 0)) { if (mas_is_err(mas)) @@ -3089,7 +3095,6 @@ retry: goto exists; } - /* Do the add */ return _ma_add(mas, entry, overwrite, active); @@ -3110,8 +3115,10 @@ static inline void ma_insert(struct ma_state *mas, void *entry) static int mas_fill_gap(struct ma_state *mas, void *entry, unsigned char slot, unsigned long size, unsigned long *index) { - //mas->index is the start address for the search, which may be done with? - //mas->last is the end address for the search + /* mas->index is the start address for the search + * which may no longer be needed. + * mas->last is the end address for the search + */ *index = mas->index; mas->last = mas->index + size - 1; @@ -3323,12 +3330,13 @@ static inline int ma_erase(struct ma_state *mas) } /* Walk down and set all the previous pivots with NULLs to piv_val */ - while(--slot >= 0 && ma_get_rcu_slot(mas->node, slot) == NULL) { + while (--slot >= 0 && ma_get_rcu_slot(mas->node, slot) == NULL) { ma_set_pivot(mas->node, slot, piv_val); ret++; } - mas_coalesce(mas, ++slot); /* The error on allocation failure can be ignored */ + /* The error on allocation failure can be ignored */ + mas_coalesce(mas, ++slot); return ret; } @@ -3347,12 +3355,12 @@ void mtree_init(struct maple_tree *mt, unsigned int ma_flags) mt->ma_flags = ma_flags; rcu_assign_pointer(mt->ma_root, NULL); } - EXPORT_SYMBOL(mtree_init); void *mtree_load(struct maple_tree *mt, unsigned long index) { void *entry; + MA_STATE(mas, mt, index, index); rcu_read_lock(); entry = mas_walk(&mas); @@ -3362,6 +3370,7 @@ void *mtree_load(struct maple_tree *mt, unsigned long index) return entry; } EXPORT_SYMBOL(mtree_load); + int mtree_store_range(struct maple_tree *mt, unsigned long first, unsigned long last, void *entry, gfp_t gfp) { @@ -3500,6 +3509,7 @@ retry: int mtree_next(struct maple_tree *mt, unsigned long index, unsigned long *next) { int ret = -ENOENT; + MA_STATE(mas, mt, index, index); rcu_read_lock(); //mas_walk_next(&mas); @@ -3534,11 +3544,9 @@ void mtree_destroy(struct maple_tree *mt) mtree_lock(mt); destroyed = mt->ma_root; - - - if (xa_is_node(destroyed)) { + if (xa_is_node(destroyed)) ma_destroy_walk(destroyed); - } + mt->ma_flags = 0; rcu_assign_pointer(mt->ma_root, NULL); mtree_unlock(mt); @@ -3552,6 +3560,7 @@ void mt_dump_node(void *entry, unsigned long min, unsigned long max, void mt_dump_range(unsigned long min, unsigned long max, unsigned int depth) { static const char spaces[] = " "; + if (min == max) pr_info("%.*s%lu: ", depth * 2, spaces, min); else @@ -3604,7 +3613,8 @@ void mt_dump_range64(void *entry, unsigned long min, unsigned long max, if (last == max) break; if (last > max) { - pr_err("node %p last (%lu) > max (%lu) at pivot %d!\n", node, last, max, i); + pr_err("node %p last (%lu) > max (%lu) at pivot %d!\n", + node, last, max, i); break; } first = last + 1; @@ -3642,7 +3652,8 @@ void mt_dump_arange64(void *entry, unsigned long min, unsigned long max, if (last == max) break; if (last > max) { - pr_err("node %p last (%lu) > max (%lu) at pivot %d!\n", node, last, max, i); + pr_err("node %p last (%lu) > max (%lu) at pivot %d!\n", + node, last, max, i); break; } first = last + 1; diff --git a/lib/test_maple_tree.c b/lib/test_maple_tree.c index 4b4998cc8291..ac74e7adce38 100644 --- a/lib/test_maple_tree.c +++ b/lib/test_maple_tree.c @@ -109,6 +109,7 @@ static noinline void check_store_range(struct maple_tree *mt, { int ret = -EINVAL; unsigned long i; + ret = mtree_test_store_range(mt, start, end, ptr); printk("ret %d expected %d\n", ret, expected); MT_BUG_ON(mt, ret != expected); @@ -116,9 +117,8 @@ static noinline void check_store_range(struct maple_tree *mt, if (ret) return; - for (i = start; i <= end; i++) { + for (i = start; i <= end; i++) check_load(mt, i, ptr); - } } static noinline void check_insert_range(struct maple_tree *mt, @@ -126,21 +126,22 @@ static noinline void check_insert_range(struct maple_tree *mt, { int ret = -EINVAL; unsigned long i; + ret = mtree_test_insert_range(mt, start, end, ptr); MT_BUG_ON(mt, ret != expected); if (ret) return; - for (i = start; i <= end; i++) { + for (i = start; i <= end; i++) check_load(mt, i, ptr); - } } static noinline void check_insert(struct maple_tree *mt, unsigned long index, void *ptr) { int ret = -EINVAL; + ret = mtree_test_insert(mt, index, ptr); MT_BUG_ON(mt, ret != 0); } @@ -148,6 +149,7 @@ static noinline void check_insert(struct maple_tree *mt, unsigned long index, static noinline void check_erase(struct maple_tree *mt, unsigned long index) { int ret = -EINVAL; + ret = mtree_test_erase(mt, index); MT_BUG_ON(mt, ret < 0); } @@ -155,6 +157,7 @@ static noinline void check_dup_insert(struct maple_tree *mt, unsigned long index, void *ptr) { int ret = -EINVAL; + ret = mtree_test_insert(mt, index, ptr); MT_BUG_ON(mt, ret != -EEXIST); } @@ -204,13 +207,17 @@ static noinline void check_new_node(struct maple_tree *mt) struct maple_node *mn, *smn; int cnt = 0; + MA_STATE(mas, mt, 0, 0); /* Try allocating 3 nodes */ mtree_lock(mt); - mas_node_cnt(&mas, 3); // request 3 nodes to be allocated. - MT_BUG_ON(mt, ma_get_alloc_req(&mas) != 3); // Allocation request of 3. - MT_BUG_ON(mt, mas.node != MA_ERROR(-ENOMEM)); // Allocate failed. + // request 3 nodes to be allocated. + mas_node_cnt(&mas, 3); + // Allocation request of 3. + MT_BUG_ON(mt, ma_get_alloc_req(&mas) != 3); + // Allocate failed. + MT_BUG_ON(mt, mas.node != MA_ERROR(-ENOMEM)); MT_BUG_ON(mt, !mas_nomem(&mas, GFP_KERNEL)); MT_BUG_ON(mt, ma_get_alloc_cnt(&mas) != 3); @@ -224,11 +231,15 @@ static noinline void check_new_node(struct maple_tree *mt) /* Try allocating 1 node, then 2 more */ mtree_lock(mt); - ma_set_alloc_req(&mas, 1); // Set allocation request to 1. - MT_BUG_ON(mt, ma_get_alloc_req(&mas) != 1); // Check Allocation request of 1. + // Set allocation request to 1. + ma_set_alloc_req(&mas, 1); + // Check Allocation request of 1. + MT_BUG_ON(mt, ma_get_alloc_req(&mas) != 1); mas_set_err(&mas, -ENOMEM); - MT_BUG_ON(mt, !mas_nomem(&mas, GFP_KERNEL)); // Validate allocation request. - mn = ma_next_alloc(&mas); // Eat the requested node. + // Validate allocation request. + MT_BUG_ON(mt, !mas_nomem(&mas, GFP_KERNEL)); + // Eat the requested node. + mn = ma_next_alloc(&mas); MT_BUG_ON(mt, mn == NULL); MT_BUG_ON(mt, mn->slot[0] != NULL); @@ -236,30 +247,36 @@ static noinline void check_new_node(struct maple_tree *mt) MT_BUG_ON(mt, ma_get_alloc_cnt(&mas) != 0); mt_free(mn); - mas_node_cnt(&mas, 3); // Allocate 3 nodes, will fail. - mas_nomem(&mas, GFP_KERNEL); // Drop the lock and allocate 3 nodes. - MT_BUG_ON(mt, ma_get_alloc_cnt(&mas) != 3); // Ensure 3 are allocated. - MT_BUG_ON(mt, ma_get_alloc_req(&mas) != 0); // Allocation request of 0. + // Allocate 3 nodes, will fail. + mas_node_cnt(&mas, 3); + // Drop the lock and allocate 3 nodes. + mas_nomem(&mas, GFP_KERNEL); + // Ensure 3 are allocated. + MT_BUG_ON(mt, ma_get_alloc_cnt(&mas) != 3); + // Allocation request of 0. + MT_BUG_ON(mt, ma_get_alloc_req(&mas) != 0); mn = mas.alloc; MT_BUG_ON(mt, mn == NULL); MT_BUG_ON(mt, mn->slot[0] == NULL); MT_BUG_ON(mt, mn->slot[1] == NULL); - MT_BUG_ON(mt, ma_get_alloc_cnt(&mas) != 3); // Ensure we counted 3. - mas_nomem(&mas, GFP_KERNEL); // Free. + // Ensure we counted 3. + MT_BUG_ON(mt, ma_get_alloc_cnt(&mas) != 3); + // Free. + mas_nomem(&mas, GFP_KERNEL); - printk("Testing max allocation\n"); - mas_node_cnt(&mas, 127); // Set allocation request to 127. - mas_nomem(&mas, GFP_KERNEL); // Drop the lock and allocate 3 nodes. + // Set allocation request to 127. + mas_node_cnt(&mas, 127); + // Drop the lock and allocate 3 nodes. + mas_nomem(&mas, GFP_KERNEL); mn = ma_get_alloc(&mas); MT_BUG_ON(mt, mn == NULL); cnt++; - for(int i = 0; i < 7; i++) { + for (int i = 0; i < 7; i++) { smn = mn->slot[i]; MT_BUG_ON(mt, smn == NULL); cnt++; for (int j = 0; j < 15; j++) { - printk("Checking %d %p[%d] [%d]\n", cnt, smn, i, j); MT_BUG_ON(mt, smn->slot[j] == NULL); cnt++; } @@ -271,14 +288,12 @@ static noinline void check_new_node(struct maple_tree *mt) for (int j = 0; j < 6; j++) { MT_BUG_ON(mt, smn->slot[j] == NULL); cnt++; - } - for(int i = 8; i < 15; i++) { + for (int i = 8; i < 15; i++) { MT_BUG_ON(mt, mn->slot[i] == NULL); cnt++; } - printk("Checked %d\n", cnt); MT_BUG_ON(mt, ma_get_alloc_cnt(&mas) != 127); mas_nomem(&mas, GFP_KERNEL); // Free. @@ -296,9 +311,9 @@ static noinline void check_seq(struct maple_tree *mt, unsigned long max, for (i = 0; i <= max; i++) { MT_BUG_ON(mt, mtree_insert_index(mt, i, GFP_KERNEL)); - for (j = 0; j <= i; j++) { + for (j = 0; j <= i; j++) check_index_load(mt, j); - } + check_load(mt, i + 1, NULL); } if (verbose) { @@ -317,10 +332,10 @@ static noinline void check_lb_not_empty(struct maple_tree *mt) i = huge; while (i > 4096) { - check_insert(mt, i, (void*) i); + check_insert(mt, i, (void *) i); for (j = huge; j >= i; j /= 2) { check_load(mt, j-1, NULL); - check_load(mt, j, (void*) j); + check_load(mt, j, (void *) j); check_load(mt, j+1, NULL); } i /= 2; @@ -342,10 +357,10 @@ static noinline void check_upper_bound_split(struct maple_tree *mt) i = 4096; while (i < huge) { - check_insert(mt, i, (void*) i); + check_insert(mt, i, (void *) i); for (j = i; j >= huge; j *= 2) { check_load(mt, j-1, NULL); - check_load(mt, j, (void*) j); + check_load(mt, j, (void *) j); check_load(mt, j+1, NULL); } i *= 2; @@ -355,8 +370,9 @@ static noinline void check_upper_bound_split(struct maple_tree *mt) static noinline void check_mid_split(struct maple_tree *mt) { unsigned long huge = 8000UL * 1000 * 1000; - check_insert(mt, huge, (void*) huge); - check_insert(mt, 0, (void*) 0); + + check_insert(mt, huge, (void *) huge); + check_insert(mt, 0, (void *) 0); check_lb_not_empty(mt); } @@ -367,6 +383,7 @@ static noinline void check_find(struct maple_tree *mt) unsigned long max; unsigned long index = 0; void *entry; + MA_STATE(mas, mt, 0, 0); // Insert 0. @@ -386,7 +403,7 @@ static noinline void check_find(struct maple_tree *mt) mas_reset(&mas); mas.index = val; - while ( (entry = mas_find(&mas, 268435456)) != NULL) { + while ((entry = mas_find(&mas, 268435456)) != NULL) { if (val != 64) MT_BUG_ON(mt, xa_mk_value(val) != entry); else @@ -428,7 +445,11 @@ static noinline void check_find(struct maple_tree *mt) } static noinline void check_alloc_rev_range(struct maple_tree *mt) { - // cat /proc/self/maps|awk '{print $1}'| awk -F "-" '{printf "0x%s, 0x%s, ", $1, $2}' + /* Generated by: + * cat /proc/self/maps | awk '{print $1}'| + * awk -F "-" '{printf "0x%s, 0x%s, ", $1, $2}' + */ + unsigned long range[] = { // Inclusive , Exclusive. 0x565234af2000, 0x565234af4000, @@ -468,7 +489,7 @@ static noinline void check_alloc_rev_range(struct maple_tree *mt) 0x565234af9000, // Min 0x7fff58791000, // Max 0x1000, // Size - 0x7fff5878d << 12, // First rev hole in our data of size 0x1000 + 0x7fff5878d << 12, // First rev hole of size 0x1000 0, // Return value success. 0x0, // Min @@ -480,13 +501,13 @@ static noinline void check_alloc_rev_range(struct maple_tree *mt) 0x0, // Min 0x0, // Max 0x1000, // Size - 0x0, // First rev hole in our data of size 0x1000 + 0x0, // First rev hole of size 0x1000 0, // Return value success. 0x0, // Min 0x7F36D510A << 12, // Max 0x4000, // Size - 0x7F36D5107 << 12, // First rev hole in our data of size 0x4000 + 0x7F36D5107 << 12, // First rev hole of size 0x4000 0, // Return value success. // Ascend test. @@ -507,17 +528,17 @@ static noinline void check_alloc_rev_range(struct maple_tree *mt) MT_BUG_ON(mt, !mtree_empty(mt)); - int i, range_cnt = sizeof(range)/sizeof(range[0]); - int req_range_cnt = sizeof(req_range)/sizeof(req_range[0]); + int i, range_cnt = ARRAY_SIZE(range); + int req_range_cnt = ARRAY_SIZE(req_range); - for (i = 0; i < range_cnt; i+=2) { + for (i = 0; i < range_cnt; i += 2) { // Inclusive , Inclusive (with the -1) check_insert_range(mt, range[i] >> 12, (range[i + 1] >> 12) - 1, xa_mk_value(range[i] >> 12), 0); } printk("Starting reverse range allocation tests.\n"); - for (i = 0; i < req_range_cnt; i+=5) { + for (i = 0; i < req_range_cnt; i += 5) { mt_dump(mt); printk("%s %d: alloc %ld in range %ld - %ld\n", __func__, i, @@ -545,7 +566,11 @@ static noinline void check_alloc_rev_range(struct maple_tree *mt) static noinline void check_alloc_range(struct maple_tree *mt) { - // cat /proc/self/maps|awk '{print $1}'| awk -F "-" '{printf "0x%s, 0x%s, ", $1, $2}' + /* Generated by: + * cat /proc/self/maps|awk '{print $1}'| + * awk -F "-" '{printf "0x%s, 0x%s, ", $1, $2}' + */ + unsigned long range[] = { // Inclusive , Exclusive. 0x565234af2000, 0x565234af4000, @@ -609,7 +634,7 @@ static noinline void check_alloc_range(struct maple_tree *mt) -EBUSY, // Return value failed. // Test filling entire gap. - 34148798623<< 12, // Min + 34148798623 << 12, // Min 0x7fff587AF000, // Max 0x10000, // Size 34148798632 << 12, // Expected location @@ -629,19 +654,19 @@ static noinline void check_alloc_range(struct maple_tree *mt) 34359052178 << 12, // Expected location -EBUSY, // Return failure. }; - int i, range_cnt = sizeof(range)/sizeof(range[0]); - int req_range_cnt = sizeof(req_range)/sizeof(req_range[0]); + int i, range_cnt = ARRAY_SIZE(range); + int req_range_cnt = ARRAY_SIZE(req_range); MT_BUG_ON(mt, !mtree_empty(mt)); - for (i = 0; i < range_cnt; i+=2) { + for (i = 0; i < range_cnt; i += 2) { mt_dump(mt); printk("Starting check insert %d\n", i); check_insert_range(mt, range[i] >> 12, (range[i + 1] >> 12) - 1, xa_mk_value(range[i] >> 12), 0); } - for (i = 0; i < req_range_cnt; i+=5) { + for (i = 0; i < req_range_cnt; i += 5) { mt_dump(mt); printk("%s %d: alloc %ld in range %ld - %ld\n", __func__, i, @@ -750,7 +775,6 @@ static int maple_tree_seed(void) mtree_init(&tree, 0); check_next_entry(&tree); - /* Test ranges (store and insert) */ mtree_init(&tree, 0); diff --git a/tools/testing/radix-tree/maple.c b/tools/testing/radix-tree/maple.c index ba7c3c46b595..896e3c0264c1 100644 --- a/tools/testing/radix-tree/maple.c +++ b/tools/testing/radix-tree/maple.c @@ -21,6 +21,7 @@ void farmer_tests(void) { struct maple_node *node; DEFINE_MTREE(tree); + mt_dump(&tree); tree.ma_root = xa_mk_value(0); -- 2.50.1