maple_tree: lock checking and clean ups
Subsystems (7)
Liam R. Howlett (Oracle)
maintainercover maple_tree: lock checking and clean ups
The goals of this series are:
1. Lock issue detection
A number of syzbot reports are incorrectly pointing to the mm exit as a source of the locking error. The first three patches attempt to help users detect errors in their locking - but they still have to use LOCKDEP. I guess it's still down to hope and prayers.
2. Documentation fixes
The documentation was lacking clarity, there are updates to try and help the users, especially around the erase() cases.
3. Two benign issues
The cyclic allocator may have a race, although no in-kernel user can hit it.
The erase functions may cause allocation issues if used with the incorrect locking type, but none are present in-tree.
Beyond these goals there are some test fixes, some general speed-up patches targeting extra work and cycles, and dropping dead code.
Liam R. Howlett (Oracle) (18):
maple_tree: Add rcu locking check when LOCKDEP is enabled locking/lockdep: Add sequence counter to held_lock
maple_tree: Add write lock checking with lockdep sequence numbers
maple_tree: Documentation fix
maple_tree: Drop dead code from mas_extend_spanning_null()
maple_tree: Drop MAPLE_ALLOC_SLOTS
maple_tree: Clarify comments on mas_nomem()
maple_tree: Use prefetched value in mas_wr_store_type()
maple_tree: Optimise mas_wr_node_store() when not in rcu mode
maple_tree: micro optimisation of mas_wr_store_type()
maple_tree: Add bulk parent set helper
maple_tree: Catch race in mas_alloc_cyclic()
maple_tree: Document that erase may use GFP_KERNEL for allocations
maple_tree: WARN_ON_ONCE when allocations fail
maple_tree: Document erase and allocations better
maple_tree: Change two GFP flags in tests
maple_tree: Fix argument name in header
maple_tree: Avoid extra gap calculation
Documentation/core-api/maple_tree.rst | 21 +- include/linux/lockdep.h | 3 + include/linux/lockdep_types.h | 3 +- include/linux/maple_tree.h | 10 +- include/linux/sched.h | 1 + kernel/locking/lockdep.c | 49 ++++- lib/maple_tree.c | 280 ++++++++++++++++++++------ tools/testing/radix-tree/maple.c | 4 +- 8 files changed, 295 insertions(+), 76 deletions(-)
Signature · 1 line
2.47.3
Liam R. Howlett (Oracle)
maintainerPATCH 1/18 maple_tree: Add rcu locking check when LOCKDEP is enabled
When CONFIG_LOCKDEP and CONFIG_RCU_STRICT_GRACE_PERIOD is enabled, check for rcu locking issues by recording the grace period in the maple state and checking the rcu window is still valid whenever the maple state is reused with a state that is not MA_START or MA_PAUSED.
--- include/linux/maple_tree.h | 3 +++ lib/maple_tree.c | 49 +++++++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-)
include/linux/maple_tree.h +3 -0
@@ -485,6 +485,9 @@ struct ma_state {unsigned char mas_flags;unsigned char end;/* The end of the node */ enum store_type store_type;/* The type of store needed for this operation */ +#if IS_ENABLED (CONFIG_LOCKDEP ) &&IS_ENABLED (CONFIG_RCU_STRICT_GRACE_PERIOD ) +unsigned long rcu_gp; +#endif };struct ma_wr_state {
lib/maple_tree.c +48 -1
127 lines not on this page · raw message
Signature · 1 line
2.47.3
Liam R. Howlett (Oracle)
maintainerPATCH 2/18 locking/lockdep: Add sequence counter to held_lock
In reply to Liam R. Howlett (Oracle)Add an 8 bit small sequence counter to the held_lock struct to detect if the lock as been dropped and reacquired. This is useful when a data structure depends on a constant locking context, but is not able to detect locking and unlocking of the lock through its own API.
--- include/linux/lockdep.h | 3 +++ include/linux/lockdep_types.h | 3 ++- include/linux/sched.h | 1 + kernel/locking/lockdep.c | 49 ++++++++++++++++++++++++++++++++--- 4 files changed, 51 insertions(+), 5 deletions(-)
include/linux/lockdep.h +3 -0
@@ -273,6 +273,9 @@ extern struct pin_cookie lock_pin_lock(struct lockdep_map *lock);extern void lock_repin_lock (struct lockdep_map *lock,struct pin_cookie );extern void lock_unpin_lock (struct lockdep_map *lock,struct pin_cookie ); +extern u32 lock_sequence (struct lockdep_map *lock); +#define lockdep_sequence (lock) lock_sequence(&(lock)->dep_map) +#define lockdep_depth (tsk) (debug_locks ? (tsk)->lockdep_depth : 0)#define lockdep_assert (cond) \
include/linux/lockdep_types.h +2 -1
@@ -253,7 +253,8 @@ struct held_lock { unsigned int hardirqs_off:1; unsigned int sync:1; unsigned int references:11; /* 32 bits */ - unsigned int pin_count; + unsigned int pin_count:24; + unsigned int seq_count:8; };#else /* !CONFIG_LOCKDEP */
include/linux/sched.h +1 -0
@@ -1288,6 +1288,7 @@ struct task_struct {u64 curr_chain_key;int lockdep_depth;unsigned int lockdep_recursion; +unsigned int lockdep_seq;struct held_lock held_locks[MAX_LOCK_DEPTH ];#endif
kernel/locking/lockdep.c +45 -4
91 lines not on this page · raw message
Signature · 1 line
2.47.3
Liam R. Howlett (Oracle)
maintainerPATCH 3/18 maple_tree: Add write lock checking with lockdep sequence numbers
In reply to Liam R. Howlett (Oracle)Use the lockdep sequence numbers to ensure the write lock is not dropped between write operations. The lockdep sequence is recorded on any walk that starts from the top of the tree and re-checked prior to any operation using an active node.
--- include/linux/maple_tree.h | 7 +++-- lib/maple_tree.c | 55 +++++++++++++++++++++++++++++--------- 2 files changed, 48 insertions(+), 14 deletions(-)
include/linux/maple_tree.h +5 -2
@@ -485,9 +485,12 @@ struct ma_state {unsigned char mas_flags;unsigned char end;/* The end of the node */ enum store_type store_type;/* The type of store needed for this operation */ -#if IS_ENABLED (CONFIG_LOCKDEP ) &&IS_ENABLED (CONFIG_RCU_STRICT_GRACE_PERIOD ) +#ifdef CONFIG_LOCKDEP +u32 ld_seq; +#ifdef CONFIG_RCU_STRICT_GRACE_PERIOD unsigned long rcu_gp; -#endif +#endif /* CONFIG_RCU_STRICT_GRACE_PERIOD */ +#endif /* CONFIG_LOCKDEP */ };struct ma_wr_state {
lib/maple_tree.c +43 -12
114 lines not on this page · raw message
Signature · 1 line
2.47.3
Liam R. Howlett (Oracle)
maintainerPATCH 1/1 maple_tree: Static define mas_lockdep_map()
mas_lockdep_map() is only used internally and without static define, there is a build warning.
--- lib/maple_tree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Andrew,
Please apply this fixup to [PATCH 03/18] maple_tree: Add write lock checking with lockdep sequence numbers
Thanks,
Liam
lib/maple_tree.c +1 -1
@@ -1155,7 +1155,7 @@ static inline void mas_free(struct ma_state *mas, struct maple_enode *used)#ifdef CONFIG_LOCKDEP -struct lockdep_map *mas_lockdep_map (struct ma_state *mas) +static struct lockdep_map *mas_lockdep_map (struct ma_state *mas) {struct maple_tree *mt = mas->tree;
Signature · 1 line
2.47.3
Liam R. Howlett (Oracle)
maintainerPATCH 4/18 maple_tree: Documentation fix
In reply to Liam R. Howlett (Oracle)Don't include the word flag in the quotes with the actual flag.
--- Documentation/core-api/maple_tree.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Documentation/core-api/maple_tree.rst +1 -1
@@ -211,7 +211,7 @@ Advanced Locking The maple tree uses a spinlock by default, but external locks can be used for tree updates as well. To use an external lock, the tree must be initialized -with the ``MT_FLAGS_LOCK_EXTERN flag``, this is usually done with the +with the ``MT_FLAGS_LOCK_EXTERN`` flag, this is usually done with the MTREE_INIT_EXT() #define, which takes an external lock as an argument. Functions and structures
Signature · 1 line
2.47.3
Liam R. Howlett (Oracle)
maintainerPATCH 5/18 maple_tree: Drop dead code from mas_extend_spanning_null()
In reply to Liam R. Howlett (Oracle)mas_extend_spanning_null() had a clause if the end of the range being written (mas->last) is the same as the end of the existing range it is overwriting (wr_mas->r_max), action will be taken.
This code path is not possible because the only calling function increments mas->last (unless it's ULONG_MAX) to walk to one beyond the write and then resets the value back to the initial value.
In the case of mas->last == ULONG_MAX, then the second part of the statement will always be false - mas->last cannot be less than the node max.
This code never executed and is flawed anyways (the arguments are incorrectly ordered), so removing it is the safest action. Since the code never executes, it is not fixing any issue so Fixes tag is not given.
--- lib/maple_tree.c | 7 ------- 1 file changed, 7 deletions(-)
lib/maple_tree.c +0 -7
@@ -2993,13 +2993,6 @@ static inline void mas_extend_spanning_null(struct ma_wr_state *l_wr_mas,if (r_mas->last < r_wr_mas->r_max) r_mas->last = r_wr_mas->r_max; r_mas->offset++; - }else if ((r_mas->last == r_wr_mas->r_max) && - (r_mas->last < r_mas->max) && - !mas_slot_locked (r_mas, r_wr_mas->slots, r_mas->offset +1 )) { - r_mas->last =mas_safe_pivot (r_mas, r_wr_mas->pivots, - r_wr_mas->type, r_mas->offset +1 ); - r_mas->offset++; - r_wr_mas->r_max = r_mas->last; } }
Signature · 1 line
2.47.3
Liam R. Howlett (Oracle)
maintainerPATCH 6/18 maple_tree: Drop MAPLE_ALLOC_SLOTS
In reply to Liam R. Howlett (Oracle)MAPLE_ALLOC_SLOTS is no longer used, so remove it.
--- include/linux/maple_tree.h | 2 -- 1 file changed, 2 deletions(-)
include/linux/maple_tree.h +0 -2
@@ -29,13 +29,11 @@#define MAPLE_NODE_SLOTS 31/* 256 bytes including ->parent */ #define MAPLE_RANGE64_SLOTS 16/* 256 bytes */ #define MAPLE_ARANGE64_SLOTS 10/* 240 bytes */ -#define MAPLE_ALLOC_SLOTS (MAPLE_NODE_SLOTS - 1)#else /* 32bit sizes */ #define MAPLE_NODE_SLOTS 63/* 256 bytes including ->parent */ #define MAPLE_RANGE64_SLOTS 32/* 256 bytes */ #define MAPLE_ARANGE64_SLOTS 21/* 240 bytes */ -#define MAPLE_ALLOC_SLOTS (MAPLE_NODE_SLOTS - 2)#endif /* defined(CONFIG_64BIT) || defined(BUILD_VDSO32_64) */ #define MAPLE_NODE_MASK 255UL
Signature · 1 line
2.47.3
Liam R. Howlett (Oracle)
maintainerPATCH 7/18 maple_tree: Clarify comments on mas_nomem()
In reply to Liam R. Howlett (Oracle)When an allocation completely fails, the return is false. If the allocation succeeds or partially succeeds, return true to indicate a retry of the operation. Note that since the lock may have been dropped, the operation is retried from the start - including potentially allocating more memory.
--- lib/maple_tree.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
lib/maple_tree.c +7 -2
@@ -5672,10 +5672,11 @@ EXPORT_SYMBOL_GPL(mas_erase);/** * mas_nomem() - Check if there was an error allocating and do the allocation -* if necessary If there are allocations, then free them. +* if necessary. +* * @mas: The maple state * @gfp: The GFP_FLAGS to use for allocations -* Return: true on allocation, false otherwise. +* Return: False on no memory. True otherwise (partial success as well) */ bool mas_nomem(struct ma_state *mas, gfp_t gfp) __must_hold(mas->tree->ma_lock)
@@ -5691,6 +5692,10 @@ bool mas_nomem(struct ma_state *mas, gfp_t gfp)mas_alloc_nodes (mas, gfp); } +/* +* Return false on zero forward progress. Partial allocations are kept +* so the retry path will attempt to get the rest. +*/ if (!mas->sheaf && !mas->alloc)return false;
Signature · 1 line
2.47.3
Liam R. Howlett (Oracle)
maintainerPATCH 8/18 maple_tree: Use prefetched value in mas_wr_store_type()
In reply to Liam R. Howlett (Oracle)The slot contents exist in wr_mas->content, which has less overhead than reading the slot again.
--- lib/maple_tree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/maple_tree.c +1 -1
@@ -3276,7 +3276,7 @@ static inline void mas_wr_slot_store(struct ma_wr_state *wr_mas)void __rcu **slots = wr_mas->slots;bool gap = false; - gap |= !mt_slot_locked (mas->tree, slots, offset); + gap |= !wr_mas->content; gap |= !mt_slot_locked (mas->tree, slots, offset +1 );if (wr_mas->offset_end - offset ==1 ) {
Signature · 1 line
2.47.3
Liam R. Howlett (Oracle)
maintainerPATCH 9/18 maple_tree: Optimise mas_wr_node_store() when not in rcu mode
In reply to Liam R. Howlett (Oracle)Clearing the entire node on the stack is unnecessary since most of the node will be overwritten anyways. Just clear what isn't used after the data is in place.
Benchmarking shows a speedup of 0.67% on a height 4 tree with 2048 entries.
--- lib/maple_tree.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
lib/maple_tree.c +13 -3
@@ -3183,7 +3183,7 @@ static void mas_wr_spanning_store(struct ma_wr_state *wr_mas)static inline void mas_wr_node_store (struct ma_wr_state *wr_mas) {unsigned char dst_offset, offset_end; -unsigned char copy_size, node_pivots; +unsigned char copy_size, node_pivots, node_slots;struct maple_node reuse, *newnode;unsigned long *dst_pivots;void __rcu **dst_slots;
@@ -3196,6 +3196,7 @@ static inline void mas_wr_node_store(struct ma_wr_state *wr_mas) in_rcu =mt_in_rcu (mas->tree); offset_end = wr_mas->offset_end; node_pivots = mt_pivots[wr_mas->type]; + node_slots = mt_slots[wr_mas->type];/* Assume last adds an entry */ new_end = mas->end +1 - offset_end + mas->offset;if (mas->last == wr_mas->end_piv) {
@@ -3207,7 +3208,6 @@ static inline void mas_wr_node_store(struct ma_wr_state *wr_mas)if (in_rcu) { newnode =mas_pop_node (mas); }else { -memset (&reuse,0 ,sizeof (struct maple_node )); newnode = &reuse; }
@@ -3251,7 +3251,17 @@ static inline void mas_wr_node_store(struct ma_wr_state *wr_mas) dst_pivots[new_end] = mas->max; done: -mas_leaf_set_meta (newnode, maple_leaf_64, new_end); +if (!in_rcu && new_end +2 < node_slots) { +unsigned char clear_from = new_end +1 ; + +memset (dst_slots + clear_from,0 , +sizeof (void __rcu *) * (node_slots - clear_from)); +if (clear_from < node_pivots) +memset (dst_pivots + clear_from,0 , +sizeof (unsigned long ) * (node_pivots - clear_from)); + } + +mas_leaf_set_meta (newnode, wr_mas->type, new_end);if (in_rcu) {struct maple_enode *old_enode = mas->node;
Signature · 1 line
2.47.3
Liam R. Howlett (Oracle)
maintainerPATCH 10/18 maple_tree: micro optimisation of mas_wr_store_type()
In reply to Liam R. Howlett (Oracle)Use three new local booleans instead of reading other structures. This has shown an increase of 0.62% on a 2048 entry tree of height 4.
--- lib/maple_tree.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-)
lib/maple_tree.c +15 -3
@@ -3729,6 +3729,9 @@ static inline enum store_type mas_wr_store_type(struct ma_wr_state *wr_mas) {struct ma_state *mas = wr_mas->mas;unsigned char new_end; +bool appending; +bool one_slot; +bool in_rcu;if (unlikely (mas_is_none (mas) ||mas_is_ptr (mas)))return wr_store_root;
@@ -3748,21 +3751,30 @@ static inline enum store_type mas_wr_store_type(struct ma_wr_state *wr_mas)return wr_new_root; new_end =mas_wr_new_end (wr_mas); + in_rcu =mt_in_rcu (mas->tree); + appending = mas->offset == mas->end; + one_slot = wr_mas->offset_end - mas->offset ==1 ; +/* Potential spanning rebalance collapsing a node */ if (new_end < mt_min_slots[wr_mas->type]) {if (!mte_is_root (mas->node))return wr_rebalance; +if (!in_rcu) { +if (appending) +return wr_append; +else if (mas->end == new_end && one_slot) +return wr_slot_store; + }return wr_node_store; }if (new_end >= mt_slots[wr_mas->type])return wr_split_store; -if (!mt_in_rcu (mas->tree) && (mas->offset == mas->end)) +if (!in_rcu && appending)return wr_append; -if ((new_end == mas->end) && (!mt_in_rcu (mas->tree) || - (wr_mas->offset_end - mas->offset ==1 ))) +if (new_end == mas->end && (!in_rcu || one_slot))return wr_slot_store;return wr_node_store;
Signature · 1 line
2.47.3
Liam R. Howlett (Oracle)
maintainerPATCH 11/18 maple_tree: Add bulk parent set helper
In reply to Liam R. Howlett (Oracle)Instead of calculating the parent pointer each time for a child, cache the majority of the parent pointer and only change the slot per child.
Testing on a tree containing 2048 entries of height 4 had an increased gain of 3.51% on nodes tracking gaps.
--- lib/maple_tree.c | 59 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 16 deletions(-)
lib/maple_tree.c +43 -16
@@ -469,7 +469,7 @@ static inlinevoid mas_set_parent (struct ma_state *mas,struct maple_enode *enode,const struct maple_enode *parent,unsigned char slot) { -unsigned long val = (unsigned long )parent; +unsigned long val;unsigned long shift;unsigned long type;enum maple_type p_type =mte_node_type (parent);
@@ -490,11 +490,47 @@ void mas_set_parent(struct ma_state *mas, struct maple_enode *enode,break ; } + val = (unsigned long )parent; val &= ~MAPLE_NODE_MASK ;/* Clear all node metadata in parent */ val |= (slot << shift) | type;mte_to_node (enode)->parent =ma_parent_ptr (val); } +/* +* mas_set_parent_slots() - Bulk operation to set many slot parent pointers +* @mas: The maple state +* @parent: The encoded maple node that is the parent of @enode. +* @slot: The slot that of the @enode. +* @start_slot: The offset into @slot +* @count: The number of slots to set (eg: exclusive) +*/ +static inline +void mas_set_parent_slots (struct ma_state *mas,struct maple_enode *parent, +void __rcu **slots,unsigned char start_slot,unsigned char count) +{ +unsigned long val; +unsigned long shift; +unsigned long type; +enum maple_type p_type =mte_node_type (parent); +unsigned char i; + +MAS_BUG_ON (mas, p_type != maple_range_64 && + p_type != maple_arange_64); + + shift =MAPLE_PARENT_SLOT_SHIFT ; + type =MAPLE_PARENT_RANGE64 ; + + val = (unsigned long )parent; + val &= ~MAPLE_NODE_MASK ; + +for (i =0 ; i < count; i++) { +unsigned long pval = val | ((start_slot + i) << shift) | type; +struct maple_enode *child = (struct maple_enode *)slots[i]; + +mte_to_node (child)->parent =ma_parent_ptr (pval); + } +} +/* * mte_parent_slot() - get the parent slot of @enode. * @enode: The encoded maple node.
@@ -1605,14 +1641,10 @@ static inline void mas_adopt_children(struct ma_state *mas,struct maple_node *node =mte_to_node (parent);void __rcu **slots =ma_slots (node, type);unsigned long *pivots =ma_pivots (node, type); -struct maple_enode *child; -unsigned char offset; +unsigned char end; - offset =ma_data_end (node, type, pivots, mas->max); -do { - child =mas_slot_locked (mas, slots, offset); -mas_set_parent (mas, child, parent, offset); - }while (offset--); + end =ma_data_end (node, type, pivots, mas->max); +mas_set_parent_slots (mas, parent, slots,0 , end +1 ); }/*
@@ -1994,15 +2026,10 @@ unsigned long node_copy(struct ma_state *mas, struct maple_node *src, s_slots =ma_slots (src, s_mt) + start; s_pivots =ma_pivots (src, s_mt) + start;memcpy (d_slots, s_slots, size *sizeof (void __rcu *)); -if (!ma_is_leaf (d_mt) && s_mt == maple_copy) { -struct maple_enode *edst =mt_mk_node (dst, d_mt); - -for (int i =0 ; i < size; i++) -mas_set_parent (mas, -mt_slot_locked (mas->tree, d_slots, i), - edst, d_start + i); - } +if (!ma_is_leaf (d_mt) && s_mt == maple_copy) +mas_set_parent_slots (mas,mt_mk_node (dst, d_mt), + d_slots, d_start, size); d_gaps =ma_gaps (dst, d_mt);if (d_gaps) {
Signature · 1 line
2.47.3
Liam R. Howlett (Oracle)
maintainerPATCH 1/1 maple_tree: Fix dereference of rcu type
When getting the slot, the rcu type of the slot needs to use rcu_dereference_protected() to ensure no locking issues.
--- lib/maple_tree.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
Andrew,
Please apply this fix to "[PATCH 11/18] maple_tree: Add bulk parent set helper" to avoid type complaints.
Thanks,
Liam
lib/maple_tree.c +3 -1
@@ -525,8 +525,10 @@ void mas_set_parent_slots(struct ma_state *mas, struct maple_enode *parent,for (i =0 ; i < count; i++) {unsigned long pval = val | ((start_slot + i) << shift) | type; -struct maple_enode *child = (struct maple_enode *)slots[i]; +struct maple_enode *child; + child =ma_enode_ptr (rcu_dereference_protected (slots[i], +mt_locked (mas->tree)));mte_to_node (child)->parent =ma_parent_ptr (pval); } }
Signature · 1 line
2.47.3
Liam R. Howlett (Oracle)
maintainerPATCH 12/18 maple_tree: Catch race in mas_alloc_cyclic()
In reply to Liam R. Howlett (Oracle)If mas_alloc_cyclic() is called during a low memory situation, it is possible the lock may be dropped so reclaim can occur. There is a window where some other task may allocate the same id and cause the mas_insert() to fail with -EEXIST. In this scenario the function will return -EEXIST, which is not expected.
Modifying the retry on mas_nomem() to re-search for a slot means that any race with other writes will not matter as the lock will be held between finding the index and writing the index.
No existing users are exposed to this issue.
--- lib/maple_tree.c | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-)
lib/maple_tree.c +18 -16
@@ -3900,27 +3900,29 @@ int mas_alloc_cyclic(struct ma_state *mas, unsigned long *startp, void *entry, unsigned long range_lo, unsigned long range_hi, unsigned long *next, gfp_t gfp) { - unsigned long min = range_lo; -int ret =0 ; + int ret; +unsigned long min; - range_lo =max (min, *next); - ret =mas_empty_area (mas, range_lo, range_hi,1 ); -if ((mas->tree->ma_flags &MT_FLAGS_ALLOC_WRAPPED ) && ret ==0 ) { - mas->tree->ma_flags &= ~MT_FLAGS_ALLOC_WRAPPED ; - ret =1 ; - } -if (ret <0 && range_lo > min) { -mas_reset (mas); - ret =mas_empty_area (mas, min, range_hi,1 ); -if (ret ==0 ) + min = range_lo; +do { + range_lo =max (min, *next); + ret =mas_empty_area (mas, range_lo, range_hi,1 ); +if ((mas->tree->ma_flags &MT_FLAGS_ALLOC_WRAPPED ) && ret ==0 ) { + mas->tree->ma_flags &= ~MT_FLAGS_ALLOC_WRAPPED ; ret =1 ; - } -if (ret <0 ) -return ret; + } +if (ret <0 && range_lo > min) { +mas_reset (mas); + ret =mas_empty_area (mas, min, range_hi,1 ); +if (ret ==0 ) + ret =1 ; + } +if (ret <0 ) +return ret; -do {mas_insert (mas, entry); }while (mas_nomem (mas, gfp)); +if (mas_is_err (mas))return xa_err (mas->node);
Signature · 1 line
2.47.3
Liam R. Howlett (Oracle)
maintainerPATCH 13/18 maple_tree: Document that erase may use GFP_KERNEL for allocations
In reply to Liam R. Howlett (Oracle)State that the mas_erase() and mtree_erase() functions may use GFP_KERNEL on allocation retry. Don't just depend on people reading the documentation by adding a check that will warn of the use.
--- lib/maple_tree.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
lib/maple_tree.c +16 -0
@@ -5686,6 +5686,10 @@ EXPORT_SYMBOL_GPL(mas_find_range_rev);* Searches for @mas->index, sets @mas->index and @mas->last to the range and * erases that range. * +* Note that erase requires allocations and will use GFP_KERNEL to do so if +* necessary. If the allocation fails, the internal lock will be dropped to +* retry. +* * Return: the entry that was erased or %NULL, @mas->index and @mas->last are updated. */ void *mas_erase (struct ma_state *mas)
@@ -5694,6 +5698,14 @@ void *mas_erase(struct ma_state *mas)unsigned long index = mas->index;MA_WR_STATE (wr_mas, mas,NULL ); +/* +* In low memory situations, the allocation is retried with the gfp flag +* GFP_KERNEL. The internal spinlock is dropped in mas_nomem(), however +* the external lock is not dropped. +*/ +if (mt_external_lock (mas->tree)) +might_alloc (GFP_KERNEL ); +if (!mas_is_active (mas) || !mas_is_start (mas)) mas->status = ma_start;
@@ -6040,6 +6052,10 @@ EXPORT_SYMBOL(mtree_alloc_rrange);* Erasing is the same as a walk to an entry then a store of a NULL to that * ENTIRE range. In fact, it is implemented as such using the advanced API. * +* Note that erase requires allocations and will use GFP_KERNEL to do so if +* necessary. If the allocation fails, the internal lock will be dropped to +* retry. +* * Return: The entry stored at the @index or %NULL */ void *mtree_erase (struct maple_tree *mt,unsigned long index)
Signature · 1 line
2.47.3
Liam R. Howlett (Oracle)
maintainerPATCH 14/18 maple_tree: WARN_ON_ONCE when allocations fail
In reply to Liam R. Howlett (Oracle)Allocations should never fail in the circumstances that are expected to occur. Add checks in the code to ensure the circumstances are correctly set up by the user and warn if they are not.
Also add a warning on failure to allocate, which should never happen.
--- lib/maple_tree.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
lib/maple_tree.c +9 -2
@@ -5747,6 +5747,10 @@ bool mas_nomem(struct ma_state *mas, gfp_t gfp)if (likely (mas->node !=MA_ERROR (-ENOMEM )))return false; +/* Allocations can fail, don't do this. */ +WARN_ON_ONCE (!gfpflags_allow_blocking (gfp) && +mt_external_lock (mas->tree)); +if (gfpflags_allow_blocking (gfp) && !mt_external_lock (mas->tree)) {mtree_unlock (mas->tree);mas_alloc_nodes (mas, gfp);
@@ -5757,9 +5761,12 @@ bool mas_nomem(struct ma_state *mas, gfp_t gfp)/* * Return false on zero forward progress. Partial allocations are kept -* so the retry path will attempt to get the rest. +* so the retry path will attempt to get the rest. The failure should +* not happen as we try our best to reclaim. The user would need an +* external lock with a non-blocking gfp in a low memory situation - +* which would have triggered the first warning in this function. */ -if (!mas->sheaf && !mas->alloc) +if (WARN_ON_ONCE (!mas->sheaf && !mas->alloc))return false;mas_reset (mas);
Signature · 1 line
2.47.3
Liam R. Howlett (Oracle)
maintainerPATCH 15/18 maple_tree: Document erase and allocations better
In reply to Liam R. Howlett (Oracle)During a discussion on the maple tree erase process and GFP flags, Jason suggested there be an amendment to the documentation to clarify the situation on allocations within the tree.
The added text is an attempt to better explain that the tree may allocate, even when erasing, and provide some guidance on how to work around such issues.
--- Documentation/core-api/maple_tree.rst | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)
Documentation/core-api/maple_tree.rst +16 -3
@@ -17,7 +17,8 @@ supports iterating over a range of entries and going to the previous or next entry in a cache-efficient manner. The tree can also be put into an RCU-safe mode of operation which allows reading and writing concurrently. Writers must synchronize on a lock, which can be the default spinlock, or the user can set -the lock to an external lock of a different type. +the lock to an external lock of a different type. Note that external locks may +interfere with allocations in a low memory situation. The Maple Tree maintains a small memory footprint and was designed to use modern processor cache efficiently. The majority of the users will be able to
@@ -42,6 +43,15 @@ successful store operation within a given code segment when allocating cannot be done. Allocations of nodes are relatively small at around 256 bytes. +Since the maple tree uses internal nodes that are allocated and has rules on +data density, erasing an entry may cause allocations to occur. That is, +erasing an entry may consume memory. Users must take care to ensure that they +do not violate the larger system constraints on when and how memory is +allocated. Most situations are fine to allocate, but the pre-allocation +support is provided as a mechanism to avoid trickier situations. There is also +the possibility of using special entries and clean up the tree later, in +extreme circumstances. + .. _maple-tree-normal-api: Normal API
@@ -63,7 +73,8 @@ success or an error code otherwise. mtree_store_range() works in the same way but takes a range. mtree_load() is used to retrieve the entry stored at a given index. You can use mtree_erase() to erase an entire range by only knowing one value within that range, or mtree_store() call with an entry of -NULL may be used to partially erase a range or many ranges at once. +NULL may be used to partially erase a range or many ranges at once. Note that +mtree_erase() may use GFP_KERNEL on allocations. If you want to only store a new entry to a range (or index) if that range is currently ``NULL``, you can use mtree_insert_range() or mtree_insert() which
@@ -163,7 +174,9 @@ You can use mas_erase() to erase an entire range by setting index and last of the maple state to the desired range to erase. This will erase the first range that is found in that range, set the maple state index and last as the range that was erased and return the entry that existed -at that location. +at that location. Note that mas_erase() may allocate with the GFP_KERNEL flag. +If this is not okay, consider using mas_store_gfp() and pass it a ``NULL``, +after setting up the correct range by walking to the entry. You can walk each entry within a range by using mas_for_each(). If you want to walk each element of the tree then ``0`` and ``ULONG_MAX`` may be used as
Signature · 1 line
2.47.3
Liam R. Howlett (Oracle)
maintainerPATCH 16/18 maple_tree: Change two GFP flags in tests
In reply to Liam R. Howlett (Oracle)The GFP flags in two tests are obviously incorrect. Make the tests correctly run by updating the GFP flags.
--- tools/testing/radix-tree/maple.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/radix-tree/maple.c +2 -2
@@ -35234,7 +35234,7 @@ static noinline void __init check_prealloc(struct maple_tree *mt)mt_set_non_kernel (1 );/* Spanning store */ mas_set_range (&mas,1 ,100 ); -MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_KERNEL &GFP_NOWAIT ) ==0 ); +MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_NOWAIT ) ==0 ); allocated =mas_allocated (&mas); height =mas_mt_height (&mas);MT_BUG_ON (mt, allocated !=0 );
@@ -35257,7 +35257,7 @@ static noinline void __init check_prealloc(struct maple_tree *mt)MT_BUG_ON (mt,mas_allocated (&mas) !=0 );mas_set_range (&mas,0 ,200 );mt_set_non_kernel (1 ); -MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_KERNEL &GFP_NOWAIT ) ==0 ); +MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_NOWAIT ) ==0 ); allocated =mas_allocated (&mas); height =mas_mt_height (&mas);MT_BUG_ON (mt, allocated !=0 );
Signature · 1 line
2.47.3
Liam R. Howlett (Oracle)
maintainerPATCH 17/18 maple_tree: Fix argument name in header
In reply to Liam R. Howlett (Oracle)The mas_prev_range() function takes a min and not a max.
--- include/linux/maple_tree.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/maple_tree.h +1 -1
@@ -576,7 +576,7 @@ void maple_tree_init(void);void mas_destroy (struct ma_state *mas);void *mas_prev (struct ma_state *mas,unsigned long min); -void *mas_prev_range (struct ma_state *mas,unsigned long max); +void *mas_prev_range (struct ma_state *mas,unsigned long min);void *mas_next (struct ma_state *mas,unsigned long max);void *mas_next_range (struct ma_state *mas,unsigned long max);
Signature · 1 line
2.47.3
Liam R. Howlett (Oracle)
maintainerPATCH 18/18 maple_tree: Avoid extra gap calculation
In reply to Liam R. Howlett (Oracle)Prior to ending the ascension loop of larger operations like split, rebalance, and spanning store the gap in the node had been calculated. Once the node is inserted into the tree, the gap is recalculated in mas_update_gap(). This can be avoided by creating a helper for mas_update_gap() that accepts the known gap value, which reduces the operations required for gap updating path.
--- lib/maple_tree.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-)
lib/maple_tree.c +17 -11
@@ -1602,14 +1602,26 @@ static inline void mas_parent_gap(struct ma_state *mas, unsigned char offset, goto ascend; } +static __always_inline void mas_update_gap_known(struct ma_state *mas, +unsigned long gap) +{ +unsigned char pslot; +unsigned long p_gap; + + pslot =mte_parent_slot (mas->node); + p_gap =ma_gaps (mte_parent (mas->node), +mas_parent_type (mas, mas->node))[pslot]; + +if (p_gap != gap) +mas_parent_gap (mas, pslot, gap); +} +/* * mas_update_gap() - Update a nodes gaps and propagate up if necessary. * @mas: the maple state. */ static inline void mas_update_gap (struct ma_state *mas) { -unsigned char pslot; -unsigned long p_gap;unsigned long max_gap;if (!mt_is_alloc (mas->tree))
@@ -1619,13 +1631,7 @@ static inline void mas_update_gap(struct ma_state *mas)return ; max_gap =mas_max_gap (mas); - - pslot =mte_parent_slot (mas->node); - p_gap =ma_gaps (mte_parent (mas->node), -mas_parent_type (mas, mas->node))[pslot]; - -if (p_gap != max_gap) -mas_parent_gap (mas, pslot, max_gap); +mas_update_gap_known (mas, max_gap); }/*
@@ -2173,8 +2179,8 @@ static inline void mas_wmb_replace(struct ma_state *mas, struct maple_copy *cp) mas->node =mt_slot_locked (mas->tree, cp->slot,0 );/* Insert the new data in the tree */ mas_topiary_replace (mas, old_enode, cp->height); -if (!mte_is_leaf (mas->node)) -mas_update_gap (mas); +if (mt_is_alloc (mas->tree) && !mte_is_root (mas->node)) +mas_update_gap_known (mas, cp->gap[0 ]);mtree_range_walk (mas); }
Signature · 1 line
2.47.3