162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * XArray implementation 462306a36Sopenharmony_ci * Copyright (c) 2017-2018 Microsoft Corporation 562306a36Sopenharmony_ci * Copyright (c) 2018-2020 Oracle 662306a36Sopenharmony_ci * Author: Matthew Wilcox <willy@infradead.org> 762306a36Sopenharmony_ci */ 862306a36Sopenharmony_ci 962306a36Sopenharmony_ci#include <linux/bitmap.h> 1062306a36Sopenharmony_ci#include <linux/export.h> 1162306a36Sopenharmony_ci#include <linux/list.h> 1262306a36Sopenharmony_ci#include <linux/slab.h> 1362306a36Sopenharmony_ci#include <linux/xarray.h> 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ci#include "radix-tree.h" 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ci/* 1862306a36Sopenharmony_ci * Coding conventions in this file: 1962306a36Sopenharmony_ci * 2062306a36Sopenharmony_ci * @xa is used to refer to the entire xarray. 2162306a36Sopenharmony_ci * @xas is the 'xarray operation state'. It may be either a pointer to 2262306a36Sopenharmony_ci * an xa_state, or an xa_state stored on the stack. This is an unfortunate 2362306a36Sopenharmony_ci * ambiguity. 2462306a36Sopenharmony_ci * @index is the index of the entry being operated on 2562306a36Sopenharmony_ci * @mark is an xa_mark_t; a small number indicating one of the mark bits. 2662306a36Sopenharmony_ci * @node refers to an xa_node; usually the primary one being operated on by 2762306a36Sopenharmony_ci * this function. 2862306a36Sopenharmony_ci * @offset is the index into the slots array inside an xa_node. 2962306a36Sopenharmony_ci * @parent refers to the @xa_node closer to the head than @node. 3062306a36Sopenharmony_ci * @entry refers to something stored in a slot in the xarray 3162306a36Sopenharmony_ci */ 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_cistatic inline unsigned int xa_lock_type(const struct xarray *xa) 3462306a36Sopenharmony_ci{ 3562306a36Sopenharmony_ci return (__force unsigned int)xa->xa_flags & 3; 3662306a36Sopenharmony_ci} 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_cistatic inline void xas_lock_type(struct xa_state *xas, unsigned int lock_type) 3962306a36Sopenharmony_ci{ 4062306a36Sopenharmony_ci if (lock_type == XA_LOCK_IRQ) 4162306a36Sopenharmony_ci xas_lock_irq(xas); 4262306a36Sopenharmony_ci else if (lock_type == XA_LOCK_BH) 4362306a36Sopenharmony_ci xas_lock_bh(xas); 4462306a36Sopenharmony_ci else 4562306a36Sopenharmony_ci xas_lock(xas); 4662306a36Sopenharmony_ci} 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_cistatic inline void xas_unlock_type(struct xa_state *xas, unsigned int lock_type) 4962306a36Sopenharmony_ci{ 5062306a36Sopenharmony_ci if (lock_type == XA_LOCK_IRQ) 5162306a36Sopenharmony_ci xas_unlock_irq(xas); 5262306a36Sopenharmony_ci else if (lock_type == XA_LOCK_BH) 5362306a36Sopenharmony_ci xas_unlock_bh(xas); 5462306a36Sopenharmony_ci else 5562306a36Sopenharmony_ci xas_unlock(xas); 5662306a36Sopenharmony_ci} 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_cistatic inline bool xa_track_free(const struct xarray *xa) 5962306a36Sopenharmony_ci{ 6062306a36Sopenharmony_ci return xa->xa_flags & XA_FLAGS_TRACK_FREE; 6162306a36Sopenharmony_ci} 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_cistatic inline bool xa_zero_busy(const struct xarray *xa) 6462306a36Sopenharmony_ci{ 6562306a36Sopenharmony_ci return xa->xa_flags & XA_FLAGS_ZERO_BUSY; 6662306a36Sopenharmony_ci} 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_cistatic inline void xa_mark_set(struct xarray *xa, xa_mark_t mark) 6962306a36Sopenharmony_ci{ 7062306a36Sopenharmony_ci if (!(xa->xa_flags & XA_FLAGS_MARK(mark))) 7162306a36Sopenharmony_ci xa->xa_flags |= XA_FLAGS_MARK(mark); 7262306a36Sopenharmony_ci} 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_cistatic inline void xa_mark_clear(struct xarray *xa, xa_mark_t mark) 7562306a36Sopenharmony_ci{ 7662306a36Sopenharmony_ci if (xa->xa_flags & XA_FLAGS_MARK(mark)) 7762306a36Sopenharmony_ci xa->xa_flags &= ~(XA_FLAGS_MARK(mark)); 7862306a36Sopenharmony_ci} 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_cistatic inline unsigned long *node_marks(struct xa_node *node, xa_mark_t mark) 8162306a36Sopenharmony_ci{ 8262306a36Sopenharmony_ci return node->marks[(__force unsigned)mark]; 8362306a36Sopenharmony_ci} 8462306a36Sopenharmony_ci 8562306a36Sopenharmony_cistatic inline bool node_get_mark(struct xa_node *node, 8662306a36Sopenharmony_ci unsigned int offset, xa_mark_t mark) 8762306a36Sopenharmony_ci{ 8862306a36Sopenharmony_ci return test_bit(offset, node_marks(node, mark)); 8962306a36Sopenharmony_ci} 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_ci/* returns true if the bit was set */ 9262306a36Sopenharmony_cistatic inline bool node_set_mark(struct xa_node *node, unsigned int offset, 9362306a36Sopenharmony_ci xa_mark_t mark) 9462306a36Sopenharmony_ci{ 9562306a36Sopenharmony_ci return __test_and_set_bit(offset, node_marks(node, mark)); 9662306a36Sopenharmony_ci} 9762306a36Sopenharmony_ci 9862306a36Sopenharmony_ci/* returns true if the bit was set */ 9962306a36Sopenharmony_cistatic inline bool node_clear_mark(struct xa_node *node, unsigned int offset, 10062306a36Sopenharmony_ci xa_mark_t mark) 10162306a36Sopenharmony_ci{ 10262306a36Sopenharmony_ci return __test_and_clear_bit(offset, node_marks(node, mark)); 10362306a36Sopenharmony_ci} 10462306a36Sopenharmony_ci 10562306a36Sopenharmony_cistatic inline bool node_any_mark(struct xa_node *node, xa_mark_t mark) 10662306a36Sopenharmony_ci{ 10762306a36Sopenharmony_ci return !bitmap_empty(node_marks(node, mark), XA_CHUNK_SIZE); 10862306a36Sopenharmony_ci} 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_cistatic inline void node_mark_all(struct xa_node *node, xa_mark_t mark) 11162306a36Sopenharmony_ci{ 11262306a36Sopenharmony_ci bitmap_fill(node_marks(node, mark), XA_CHUNK_SIZE); 11362306a36Sopenharmony_ci} 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ci#define mark_inc(mark) do { \ 11662306a36Sopenharmony_ci mark = (__force xa_mark_t)((__force unsigned)(mark) + 1); \ 11762306a36Sopenharmony_ci} while (0) 11862306a36Sopenharmony_ci 11962306a36Sopenharmony_ci/* 12062306a36Sopenharmony_ci * xas_squash_marks() - Merge all marks to the first entry 12162306a36Sopenharmony_ci * @xas: Array operation state. 12262306a36Sopenharmony_ci * 12362306a36Sopenharmony_ci * Set a mark on the first entry if any entry has it set. Clear marks on 12462306a36Sopenharmony_ci * all sibling entries. 12562306a36Sopenharmony_ci */ 12662306a36Sopenharmony_cistatic void xas_squash_marks(const struct xa_state *xas) 12762306a36Sopenharmony_ci{ 12862306a36Sopenharmony_ci unsigned int mark = 0; 12962306a36Sopenharmony_ci unsigned int limit = xas->xa_offset + xas->xa_sibs + 1; 13062306a36Sopenharmony_ci 13162306a36Sopenharmony_ci if (!xas->xa_sibs) 13262306a36Sopenharmony_ci return; 13362306a36Sopenharmony_ci 13462306a36Sopenharmony_ci do { 13562306a36Sopenharmony_ci unsigned long *marks = xas->xa_node->marks[mark]; 13662306a36Sopenharmony_ci if (find_next_bit(marks, limit, xas->xa_offset + 1) == limit) 13762306a36Sopenharmony_ci continue; 13862306a36Sopenharmony_ci __set_bit(xas->xa_offset, marks); 13962306a36Sopenharmony_ci bitmap_clear(marks, xas->xa_offset + 1, xas->xa_sibs); 14062306a36Sopenharmony_ci } while (mark++ != (__force unsigned)XA_MARK_MAX); 14162306a36Sopenharmony_ci} 14262306a36Sopenharmony_ci 14362306a36Sopenharmony_ci/* extracts the offset within this node from the index */ 14462306a36Sopenharmony_cistatic unsigned int get_offset(unsigned long index, struct xa_node *node) 14562306a36Sopenharmony_ci{ 14662306a36Sopenharmony_ci return (index >> node->shift) & XA_CHUNK_MASK; 14762306a36Sopenharmony_ci} 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_cistatic void xas_set_offset(struct xa_state *xas) 15062306a36Sopenharmony_ci{ 15162306a36Sopenharmony_ci xas->xa_offset = get_offset(xas->xa_index, xas->xa_node); 15262306a36Sopenharmony_ci} 15362306a36Sopenharmony_ci 15462306a36Sopenharmony_ci/* move the index either forwards (find) or backwards (sibling slot) */ 15562306a36Sopenharmony_cistatic void xas_move_index(struct xa_state *xas, unsigned long offset) 15662306a36Sopenharmony_ci{ 15762306a36Sopenharmony_ci unsigned int shift = xas->xa_node->shift; 15862306a36Sopenharmony_ci xas->xa_index &= ~XA_CHUNK_MASK << shift; 15962306a36Sopenharmony_ci xas->xa_index += offset << shift; 16062306a36Sopenharmony_ci} 16162306a36Sopenharmony_ci 16262306a36Sopenharmony_cistatic void xas_next_offset(struct xa_state *xas) 16362306a36Sopenharmony_ci{ 16462306a36Sopenharmony_ci xas->xa_offset++; 16562306a36Sopenharmony_ci xas_move_index(xas, xas->xa_offset); 16662306a36Sopenharmony_ci} 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_cistatic void *set_bounds(struct xa_state *xas) 16962306a36Sopenharmony_ci{ 17062306a36Sopenharmony_ci xas->xa_node = XAS_BOUNDS; 17162306a36Sopenharmony_ci return NULL; 17262306a36Sopenharmony_ci} 17362306a36Sopenharmony_ci 17462306a36Sopenharmony_ci/* 17562306a36Sopenharmony_ci * Starts a walk. If the @xas is already valid, we assume that it's on 17662306a36Sopenharmony_ci * the right path and just return where we've got to. If we're in an 17762306a36Sopenharmony_ci * error state, return NULL. If the index is outside the current scope 17862306a36Sopenharmony_ci * of the xarray, return NULL without changing @xas->xa_node. Otherwise 17962306a36Sopenharmony_ci * set @xas->xa_node to NULL and return the current head of the array. 18062306a36Sopenharmony_ci */ 18162306a36Sopenharmony_cistatic void *xas_start(struct xa_state *xas) 18262306a36Sopenharmony_ci{ 18362306a36Sopenharmony_ci void *entry; 18462306a36Sopenharmony_ci 18562306a36Sopenharmony_ci if (xas_valid(xas)) 18662306a36Sopenharmony_ci return xas_reload(xas); 18762306a36Sopenharmony_ci if (xas_error(xas)) 18862306a36Sopenharmony_ci return NULL; 18962306a36Sopenharmony_ci 19062306a36Sopenharmony_ci entry = xa_head(xas->xa); 19162306a36Sopenharmony_ci if (!xa_is_node(entry)) { 19262306a36Sopenharmony_ci if (xas->xa_index) 19362306a36Sopenharmony_ci return set_bounds(xas); 19462306a36Sopenharmony_ci } else { 19562306a36Sopenharmony_ci if ((xas->xa_index >> xa_to_node(entry)->shift) > XA_CHUNK_MASK) 19662306a36Sopenharmony_ci return set_bounds(xas); 19762306a36Sopenharmony_ci } 19862306a36Sopenharmony_ci 19962306a36Sopenharmony_ci xas->xa_node = NULL; 20062306a36Sopenharmony_ci return entry; 20162306a36Sopenharmony_ci} 20262306a36Sopenharmony_ci 20362306a36Sopenharmony_cistatic void *xas_descend(struct xa_state *xas, struct xa_node *node) 20462306a36Sopenharmony_ci{ 20562306a36Sopenharmony_ci unsigned int offset = get_offset(xas->xa_index, node); 20662306a36Sopenharmony_ci void *entry = xa_entry(xas->xa, node, offset); 20762306a36Sopenharmony_ci 20862306a36Sopenharmony_ci xas->xa_node = node; 20962306a36Sopenharmony_ci while (xa_is_sibling(entry)) { 21062306a36Sopenharmony_ci offset = xa_to_sibling(entry); 21162306a36Sopenharmony_ci entry = xa_entry(xas->xa, node, offset); 21262306a36Sopenharmony_ci if (node->shift && xa_is_node(entry)) 21362306a36Sopenharmony_ci entry = XA_RETRY_ENTRY; 21462306a36Sopenharmony_ci } 21562306a36Sopenharmony_ci 21662306a36Sopenharmony_ci xas->xa_offset = offset; 21762306a36Sopenharmony_ci return entry; 21862306a36Sopenharmony_ci} 21962306a36Sopenharmony_ci 22062306a36Sopenharmony_ci/** 22162306a36Sopenharmony_ci * xas_load() - Load an entry from the XArray (advanced). 22262306a36Sopenharmony_ci * @xas: XArray operation state. 22362306a36Sopenharmony_ci * 22462306a36Sopenharmony_ci * Usually walks the @xas to the appropriate state to load the entry 22562306a36Sopenharmony_ci * stored at xa_index. However, it will do nothing and return %NULL if 22662306a36Sopenharmony_ci * @xas is in an error state. xas_load() will never expand the tree. 22762306a36Sopenharmony_ci * 22862306a36Sopenharmony_ci * If the xa_state is set up to operate on a multi-index entry, xas_load() 22962306a36Sopenharmony_ci * may return %NULL or an internal entry, even if there are entries 23062306a36Sopenharmony_ci * present within the range specified by @xas. 23162306a36Sopenharmony_ci * 23262306a36Sopenharmony_ci * Context: Any context. The caller should hold the xa_lock or the RCU lock. 23362306a36Sopenharmony_ci * Return: Usually an entry in the XArray, but see description for exceptions. 23462306a36Sopenharmony_ci */ 23562306a36Sopenharmony_civoid *xas_load(struct xa_state *xas) 23662306a36Sopenharmony_ci{ 23762306a36Sopenharmony_ci void *entry = xas_start(xas); 23862306a36Sopenharmony_ci 23962306a36Sopenharmony_ci while (xa_is_node(entry)) { 24062306a36Sopenharmony_ci struct xa_node *node = xa_to_node(entry); 24162306a36Sopenharmony_ci 24262306a36Sopenharmony_ci if (xas->xa_shift > node->shift) 24362306a36Sopenharmony_ci break; 24462306a36Sopenharmony_ci entry = xas_descend(xas, node); 24562306a36Sopenharmony_ci if (node->shift == 0) 24662306a36Sopenharmony_ci break; 24762306a36Sopenharmony_ci } 24862306a36Sopenharmony_ci return entry; 24962306a36Sopenharmony_ci} 25062306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(xas_load); 25162306a36Sopenharmony_ci 25262306a36Sopenharmony_ci#define XA_RCU_FREE ((struct xarray *)1) 25362306a36Sopenharmony_ci 25462306a36Sopenharmony_cistatic void xa_node_free(struct xa_node *node) 25562306a36Sopenharmony_ci{ 25662306a36Sopenharmony_ci XA_NODE_BUG_ON(node, !list_empty(&node->private_list)); 25762306a36Sopenharmony_ci node->array = XA_RCU_FREE; 25862306a36Sopenharmony_ci call_rcu(&node->rcu_head, radix_tree_node_rcu_free); 25962306a36Sopenharmony_ci} 26062306a36Sopenharmony_ci 26162306a36Sopenharmony_ci/* 26262306a36Sopenharmony_ci * xas_destroy() - Free any resources allocated during the XArray operation. 26362306a36Sopenharmony_ci * @xas: XArray operation state. 26462306a36Sopenharmony_ci * 26562306a36Sopenharmony_ci * Most users will not need to call this function; it is called for you 26662306a36Sopenharmony_ci * by xas_nomem(). 26762306a36Sopenharmony_ci */ 26862306a36Sopenharmony_civoid xas_destroy(struct xa_state *xas) 26962306a36Sopenharmony_ci{ 27062306a36Sopenharmony_ci struct xa_node *next, *node = xas->xa_alloc; 27162306a36Sopenharmony_ci 27262306a36Sopenharmony_ci while (node) { 27362306a36Sopenharmony_ci XA_NODE_BUG_ON(node, !list_empty(&node->private_list)); 27462306a36Sopenharmony_ci next = rcu_dereference_raw(node->parent); 27562306a36Sopenharmony_ci radix_tree_node_rcu_free(&node->rcu_head); 27662306a36Sopenharmony_ci xas->xa_alloc = node = next; 27762306a36Sopenharmony_ci } 27862306a36Sopenharmony_ci} 27962306a36Sopenharmony_ci 28062306a36Sopenharmony_ci/** 28162306a36Sopenharmony_ci * xas_nomem() - Allocate memory if needed. 28262306a36Sopenharmony_ci * @xas: XArray operation state. 28362306a36Sopenharmony_ci * @gfp: Memory allocation flags. 28462306a36Sopenharmony_ci * 28562306a36Sopenharmony_ci * If we need to add new nodes to the XArray, we try to allocate memory 28662306a36Sopenharmony_ci * with GFP_NOWAIT while holding the lock, which will usually succeed. 28762306a36Sopenharmony_ci * If it fails, @xas is flagged as needing memory to continue. The caller 28862306a36Sopenharmony_ci * should drop the lock and call xas_nomem(). If xas_nomem() succeeds, 28962306a36Sopenharmony_ci * the caller should retry the operation. 29062306a36Sopenharmony_ci * 29162306a36Sopenharmony_ci * Forward progress is guaranteed as one node is allocated here and 29262306a36Sopenharmony_ci * stored in the xa_state where it will be found by xas_alloc(). More 29362306a36Sopenharmony_ci * nodes will likely be found in the slab allocator, but we do not tie 29462306a36Sopenharmony_ci * them up here. 29562306a36Sopenharmony_ci * 29662306a36Sopenharmony_ci * Return: true if memory was needed, and was successfully allocated. 29762306a36Sopenharmony_ci */ 29862306a36Sopenharmony_cibool xas_nomem(struct xa_state *xas, gfp_t gfp) 29962306a36Sopenharmony_ci{ 30062306a36Sopenharmony_ci if (xas->xa_node != XA_ERROR(-ENOMEM)) { 30162306a36Sopenharmony_ci xas_destroy(xas); 30262306a36Sopenharmony_ci return false; 30362306a36Sopenharmony_ci } 30462306a36Sopenharmony_ci if (xas->xa->xa_flags & XA_FLAGS_ACCOUNT) 30562306a36Sopenharmony_ci gfp |= __GFP_ACCOUNT; 30662306a36Sopenharmony_ci xas->xa_alloc = kmem_cache_alloc_lru(radix_tree_node_cachep, xas->xa_lru, gfp); 30762306a36Sopenharmony_ci if (!xas->xa_alloc) 30862306a36Sopenharmony_ci return false; 30962306a36Sopenharmony_ci xas->xa_alloc->parent = NULL; 31062306a36Sopenharmony_ci XA_NODE_BUG_ON(xas->xa_alloc, !list_empty(&xas->xa_alloc->private_list)); 31162306a36Sopenharmony_ci xas->xa_node = XAS_RESTART; 31262306a36Sopenharmony_ci return true; 31362306a36Sopenharmony_ci} 31462306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(xas_nomem); 31562306a36Sopenharmony_ci 31662306a36Sopenharmony_ci/* 31762306a36Sopenharmony_ci * __xas_nomem() - Drop locks and allocate memory if needed. 31862306a36Sopenharmony_ci * @xas: XArray operation state. 31962306a36Sopenharmony_ci * @gfp: Memory allocation flags. 32062306a36Sopenharmony_ci * 32162306a36Sopenharmony_ci * Internal variant of xas_nomem(). 32262306a36Sopenharmony_ci * 32362306a36Sopenharmony_ci * Return: true if memory was needed, and was successfully allocated. 32462306a36Sopenharmony_ci */ 32562306a36Sopenharmony_cistatic bool __xas_nomem(struct xa_state *xas, gfp_t gfp) 32662306a36Sopenharmony_ci __must_hold(xas->xa->xa_lock) 32762306a36Sopenharmony_ci{ 32862306a36Sopenharmony_ci unsigned int lock_type = xa_lock_type(xas->xa); 32962306a36Sopenharmony_ci 33062306a36Sopenharmony_ci if (xas->xa_node != XA_ERROR(-ENOMEM)) { 33162306a36Sopenharmony_ci xas_destroy(xas); 33262306a36Sopenharmony_ci return false; 33362306a36Sopenharmony_ci } 33462306a36Sopenharmony_ci if (xas->xa->xa_flags & XA_FLAGS_ACCOUNT) 33562306a36Sopenharmony_ci gfp |= __GFP_ACCOUNT; 33662306a36Sopenharmony_ci if (gfpflags_allow_blocking(gfp)) { 33762306a36Sopenharmony_ci xas_unlock_type(xas, lock_type); 33862306a36Sopenharmony_ci xas->xa_alloc = kmem_cache_alloc_lru(radix_tree_node_cachep, xas->xa_lru, gfp); 33962306a36Sopenharmony_ci xas_lock_type(xas, lock_type); 34062306a36Sopenharmony_ci } else { 34162306a36Sopenharmony_ci xas->xa_alloc = kmem_cache_alloc_lru(radix_tree_node_cachep, xas->xa_lru, gfp); 34262306a36Sopenharmony_ci } 34362306a36Sopenharmony_ci if (!xas->xa_alloc) 34462306a36Sopenharmony_ci return false; 34562306a36Sopenharmony_ci xas->xa_alloc->parent = NULL; 34662306a36Sopenharmony_ci XA_NODE_BUG_ON(xas->xa_alloc, !list_empty(&xas->xa_alloc->private_list)); 34762306a36Sopenharmony_ci xas->xa_node = XAS_RESTART; 34862306a36Sopenharmony_ci return true; 34962306a36Sopenharmony_ci} 35062306a36Sopenharmony_ci 35162306a36Sopenharmony_cistatic void xas_update(struct xa_state *xas, struct xa_node *node) 35262306a36Sopenharmony_ci{ 35362306a36Sopenharmony_ci if (xas->xa_update) 35462306a36Sopenharmony_ci xas->xa_update(node); 35562306a36Sopenharmony_ci else 35662306a36Sopenharmony_ci XA_NODE_BUG_ON(node, !list_empty(&node->private_list)); 35762306a36Sopenharmony_ci} 35862306a36Sopenharmony_ci 35962306a36Sopenharmony_cistatic void *xas_alloc(struct xa_state *xas, unsigned int shift) 36062306a36Sopenharmony_ci{ 36162306a36Sopenharmony_ci struct xa_node *parent = xas->xa_node; 36262306a36Sopenharmony_ci struct xa_node *node = xas->xa_alloc; 36362306a36Sopenharmony_ci 36462306a36Sopenharmony_ci if (xas_invalid(xas)) 36562306a36Sopenharmony_ci return NULL; 36662306a36Sopenharmony_ci 36762306a36Sopenharmony_ci if (node) { 36862306a36Sopenharmony_ci xas->xa_alloc = NULL; 36962306a36Sopenharmony_ci } else { 37062306a36Sopenharmony_ci gfp_t gfp = GFP_NOWAIT | __GFP_NOWARN; 37162306a36Sopenharmony_ci 37262306a36Sopenharmony_ci if (xas->xa->xa_flags & XA_FLAGS_ACCOUNT) 37362306a36Sopenharmony_ci gfp |= __GFP_ACCOUNT; 37462306a36Sopenharmony_ci 37562306a36Sopenharmony_ci node = kmem_cache_alloc_lru(radix_tree_node_cachep, xas->xa_lru, gfp); 37662306a36Sopenharmony_ci if (!node) { 37762306a36Sopenharmony_ci xas_set_err(xas, -ENOMEM); 37862306a36Sopenharmony_ci return NULL; 37962306a36Sopenharmony_ci } 38062306a36Sopenharmony_ci } 38162306a36Sopenharmony_ci 38262306a36Sopenharmony_ci if (parent) { 38362306a36Sopenharmony_ci node->offset = xas->xa_offset; 38462306a36Sopenharmony_ci parent->count++; 38562306a36Sopenharmony_ci XA_NODE_BUG_ON(node, parent->count > XA_CHUNK_SIZE); 38662306a36Sopenharmony_ci xas_update(xas, parent); 38762306a36Sopenharmony_ci } 38862306a36Sopenharmony_ci XA_NODE_BUG_ON(node, shift > BITS_PER_LONG); 38962306a36Sopenharmony_ci XA_NODE_BUG_ON(node, !list_empty(&node->private_list)); 39062306a36Sopenharmony_ci node->shift = shift; 39162306a36Sopenharmony_ci node->count = 0; 39262306a36Sopenharmony_ci node->nr_values = 0; 39362306a36Sopenharmony_ci RCU_INIT_POINTER(node->parent, xas->xa_node); 39462306a36Sopenharmony_ci node->array = xas->xa; 39562306a36Sopenharmony_ci 39662306a36Sopenharmony_ci return node; 39762306a36Sopenharmony_ci} 39862306a36Sopenharmony_ci 39962306a36Sopenharmony_ci#ifdef CONFIG_XARRAY_MULTI 40062306a36Sopenharmony_ci/* Returns the number of indices covered by a given xa_state */ 40162306a36Sopenharmony_cistatic unsigned long xas_size(const struct xa_state *xas) 40262306a36Sopenharmony_ci{ 40362306a36Sopenharmony_ci return (xas->xa_sibs + 1UL) << xas->xa_shift; 40462306a36Sopenharmony_ci} 40562306a36Sopenharmony_ci#endif 40662306a36Sopenharmony_ci 40762306a36Sopenharmony_ci/* 40862306a36Sopenharmony_ci * Use this to calculate the maximum index that will need to be created 40962306a36Sopenharmony_ci * in order to add the entry described by @xas. Because we cannot store a 41062306a36Sopenharmony_ci * multi-index entry at index 0, the calculation is a little more complex 41162306a36Sopenharmony_ci * than you might expect. 41262306a36Sopenharmony_ci */ 41362306a36Sopenharmony_cistatic unsigned long xas_max(struct xa_state *xas) 41462306a36Sopenharmony_ci{ 41562306a36Sopenharmony_ci unsigned long max = xas->xa_index; 41662306a36Sopenharmony_ci 41762306a36Sopenharmony_ci#ifdef CONFIG_XARRAY_MULTI 41862306a36Sopenharmony_ci if (xas->xa_shift || xas->xa_sibs) { 41962306a36Sopenharmony_ci unsigned long mask = xas_size(xas) - 1; 42062306a36Sopenharmony_ci max |= mask; 42162306a36Sopenharmony_ci if (mask == max) 42262306a36Sopenharmony_ci max++; 42362306a36Sopenharmony_ci } 42462306a36Sopenharmony_ci#endif 42562306a36Sopenharmony_ci 42662306a36Sopenharmony_ci return max; 42762306a36Sopenharmony_ci} 42862306a36Sopenharmony_ci 42962306a36Sopenharmony_ci/* The maximum index that can be contained in the array without expanding it */ 43062306a36Sopenharmony_cistatic unsigned long max_index(void *entry) 43162306a36Sopenharmony_ci{ 43262306a36Sopenharmony_ci if (!xa_is_node(entry)) 43362306a36Sopenharmony_ci return 0; 43462306a36Sopenharmony_ci return (XA_CHUNK_SIZE << xa_to_node(entry)->shift) - 1; 43562306a36Sopenharmony_ci} 43662306a36Sopenharmony_ci 43762306a36Sopenharmony_cistatic void xas_shrink(struct xa_state *xas) 43862306a36Sopenharmony_ci{ 43962306a36Sopenharmony_ci struct xarray *xa = xas->xa; 44062306a36Sopenharmony_ci struct xa_node *node = xas->xa_node; 44162306a36Sopenharmony_ci 44262306a36Sopenharmony_ci for (;;) { 44362306a36Sopenharmony_ci void *entry; 44462306a36Sopenharmony_ci 44562306a36Sopenharmony_ci XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE); 44662306a36Sopenharmony_ci if (node->count != 1) 44762306a36Sopenharmony_ci break; 44862306a36Sopenharmony_ci entry = xa_entry_locked(xa, node, 0); 44962306a36Sopenharmony_ci if (!entry) 45062306a36Sopenharmony_ci break; 45162306a36Sopenharmony_ci if (!xa_is_node(entry) && node->shift) 45262306a36Sopenharmony_ci break; 45362306a36Sopenharmony_ci if (xa_is_zero(entry) && xa_zero_busy(xa)) 45462306a36Sopenharmony_ci entry = NULL; 45562306a36Sopenharmony_ci xas->xa_node = XAS_BOUNDS; 45662306a36Sopenharmony_ci 45762306a36Sopenharmony_ci RCU_INIT_POINTER(xa->xa_head, entry); 45862306a36Sopenharmony_ci if (xa_track_free(xa) && !node_get_mark(node, 0, XA_FREE_MARK)) 45962306a36Sopenharmony_ci xa_mark_clear(xa, XA_FREE_MARK); 46062306a36Sopenharmony_ci 46162306a36Sopenharmony_ci node->count = 0; 46262306a36Sopenharmony_ci node->nr_values = 0; 46362306a36Sopenharmony_ci if (!xa_is_node(entry)) 46462306a36Sopenharmony_ci RCU_INIT_POINTER(node->slots[0], XA_RETRY_ENTRY); 46562306a36Sopenharmony_ci xas_update(xas, node); 46662306a36Sopenharmony_ci xa_node_free(node); 46762306a36Sopenharmony_ci if (!xa_is_node(entry)) 46862306a36Sopenharmony_ci break; 46962306a36Sopenharmony_ci node = xa_to_node(entry); 47062306a36Sopenharmony_ci node->parent = NULL; 47162306a36Sopenharmony_ci } 47262306a36Sopenharmony_ci} 47362306a36Sopenharmony_ci 47462306a36Sopenharmony_ci/* 47562306a36Sopenharmony_ci * xas_delete_node() - Attempt to delete an xa_node 47662306a36Sopenharmony_ci * @xas: Array operation state. 47762306a36Sopenharmony_ci * 47862306a36Sopenharmony_ci * Attempts to delete the @xas->xa_node. This will fail if xa->node has 47962306a36Sopenharmony_ci * a non-zero reference count. 48062306a36Sopenharmony_ci */ 48162306a36Sopenharmony_cistatic void xas_delete_node(struct xa_state *xas) 48262306a36Sopenharmony_ci{ 48362306a36Sopenharmony_ci struct xa_node *node = xas->xa_node; 48462306a36Sopenharmony_ci 48562306a36Sopenharmony_ci for (;;) { 48662306a36Sopenharmony_ci struct xa_node *parent; 48762306a36Sopenharmony_ci 48862306a36Sopenharmony_ci XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE); 48962306a36Sopenharmony_ci if (node->count) 49062306a36Sopenharmony_ci break; 49162306a36Sopenharmony_ci 49262306a36Sopenharmony_ci parent = xa_parent_locked(xas->xa, node); 49362306a36Sopenharmony_ci xas->xa_node = parent; 49462306a36Sopenharmony_ci xas->xa_offset = node->offset; 49562306a36Sopenharmony_ci xa_node_free(node); 49662306a36Sopenharmony_ci 49762306a36Sopenharmony_ci if (!parent) { 49862306a36Sopenharmony_ci xas->xa->xa_head = NULL; 49962306a36Sopenharmony_ci xas->xa_node = XAS_BOUNDS; 50062306a36Sopenharmony_ci return; 50162306a36Sopenharmony_ci } 50262306a36Sopenharmony_ci 50362306a36Sopenharmony_ci parent->slots[xas->xa_offset] = NULL; 50462306a36Sopenharmony_ci parent->count--; 50562306a36Sopenharmony_ci XA_NODE_BUG_ON(parent, parent->count > XA_CHUNK_SIZE); 50662306a36Sopenharmony_ci node = parent; 50762306a36Sopenharmony_ci xas_update(xas, node); 50862306a36Sopenharmony_ci } 50962306a36Sopenharmony_ci 51062306a36Sopenharmony_ci if (!node->parent) 51162306a36Sopenharmony_ci xas_shrink(xas); 51262306a36Sopenharmony_ci} 51362306a36Sopenharmony_ci 51462306a36Sopenharmony_ci/** 51562306a36Sopenharmony_ci * xas_free_nodes() - Free this node and all nodes that it references 51662306a36Sopenharmony_ci * @xas: Array operation state. 51762306a36Sopenharmony_ci * @top: Node to free 51862306a36Sopenharmony_ci * 51962306a36Sopenharmony_ci * This node has been removed from the tree. We must now free it and all 52062306a36Sopenharmony_ci * of its subnodes. There may be RCU walkers with references into the tree, 52162306a36Sopenharmony_ci * so we must replace all entries with retry markers. 52262306a36Sopenharmony_ci */ 52362306a36Sopenharmony_cistatic void xas_free_nodes(struct xa_state *xas, struct xa_node *top) 52462306a36Sopenharmony_ci{ 52562306a36Sopenharmony_ci unsigned int offset = 0; 52662306a36Sopenharmony_ci struct xa_node *node = top; 52762306a36Sopenharmony_ci 52862306a36Sopenharmony_ci for (;;) { 52962306a36Sopenharmony_ci void *entry = xa_entry_locked(xas->xa, node, offset); 53062306a36Sopenharmony_ci 53162306a36Sopenharmony_ci if (node->shift && xa_is_node(entry)) { 53262306a36Sopenharmony_ci node = xa_to_node(entry); 53362306a36Sopenharmony_ci offset = 0; 53462306a36Sopenharmony_ci continue; 53562306a36Sopenharmony_ci } 53662306a36Sopenharmony_ci if (entry) 53762306a36Sopenharmony_ci RCU_INIT_POINTER(node->slots[offset], XA_RETRY_ENTRY); 53862306a36Sopenharmony_ci offset++; 53962306a36Sopenharmony_ci while (offset == XA_CHUNK_SIZE) { 54062306a36Sopenharmony_ci struct xa_node *parent; 54162306a36Sopenharmony_ci 54262306a36Sopenharmony_ci parent = xa_parent_locked(xas->xa, node); 54362306a36Sopenharmony_ci offset = node->offset + 1; 54462306a36Sopenharmony_ci node->count = 0; 54562306a36Sopenharmony_ci node->nr_values = 0; 54662306a36Sopenharmony_ci xas_update(xas, node); 54762306a36Sopenharmony_ci xa_node_free(node); 54862306a36Sopenharmony_ci if (node == top) 54962306a36Sopenharmony_ci return; 55062306a36Sopenharmony_ci node = parent; 55162306a36Sopenharmony_ci } 55262306a36Sopenharmony_ci } 55362306a36Sopenharmony_ci} 55462306a36Sopenharmony_ci 55562306a36Sopenharmony_ci/* 55662306a36Sopenharmony_ci * xas_expand adds nodes to the head of the tree until it has reached 55762306a36Sopenharmony_ci * sufficient height to be able to contain @xas->xa_index 55862306a36Sopenharmony_ci */ 55962306a36Sopenharmony_cistatic int xas_expand(struct xa_state *xas, void *head) 56062306a36Sopenharmony_ci{ 56162306a36Sopenharmony_ci struct xarray *xa = xas->xa; 56262306a36Sopenharmony_ci struct xa_node *node = NULL; 56362306a36Sopenharmony_ci unsigned int shift = 0; 56462306a36Sopenharmony_ci unsigned long max = xas_max(xas); 56562306a36Sopenharmony_ci 56662306a36Sopenharmony_ci if (!head) { 56762306a36Sopenharmony_ci if (max == 0) 56862306a36Sopenharmony_ci return 0; 56962306a36Sopenharmony_ci while ((max >> shift) >= XA_CHUNK_SIZE) 57062306a36Sopenharmony_ci shift += XA_CHUNK_SHIFT; 57162306a36Sopenharmony_ci return shift + XA_CHUNK_SHIFT; 57262306a36Sopenharmony_ci } else if (xa_is_node(head)) { 57362306a36Sopenharmony_ci node = xa_to_node(head); 57462306a36Sopenharmony_ci shift = node->shift + XA_CHUNK_SHIFT; 57562306a36Sopenharmony_ci } 57662306a36Sopenharmony_ci xas->xa_node = NULL; 57762306a36Sopenharmony_ci 57862306a36Sopenharmony_ci while (max > max_index(head)) { 57962306a36Sopenharmony_ci xa_mark_t mark = 0; 58062306a36Sopenharmony_ci 58162306a36Sopenharmony_ci XA_NODE_BUG_ON(node, shift > BITS_PER_LONG); 58262306a36Sopenharmony_ci node = xas_alloc(xas, shift); 58362306a36Sopenharmony_ci if (!node) 58462306a36Sopenharmony_ci return -ENOMEM; 58562306a36Sopenharmony_ci 58662306a36Sopenharmony_ci node->count = 1; 58762306a36Sopenharmony_ci if (xa_is_value(head)) 58862306a36Sopenharmony_ci node->nr_values = 1; 58962306a36Sopenharmony_ci RCU_INIT_POINTER(node->slots[0], head); 59062306a36Sopenharmony_ci 59162306a36Sopenharmony_ci /* Propagate the aggregated mark info to the new child */ 59262306a36Sopenharmony_ci for (;;) { 59362306a36Sopenharmony_ci if (xa_track_free(xa) && mark == XA_FREE_MARK) { 59462306a36Sopenharmony_ci node_mark_all(node, XA_FREE_MARK); 59562306a36Sopenharmony_ci if (!xa_marked(xa, XA_FREE_MARK)) { 59662306a36Sopenharmony_ci node_clear_mark(node, 0, XA_FREE_MARK); 59762306a36Sopenharmony_ci xa_mark_set(xa, XA_FREE_MARK); 59862306a36Sopenharmony_ci } 59962306a36Sopenharmony_ci } else if (xa_marked(xa, mark)) { 60062306a36Sopenharmony_ci node_set_mark(node, 0, mark); 60162306a36Sopenharmony_ci } 60262306a36Sopenharmony_ci if (mark == XA_MARK_MAX) 60362306a36Sopenharmony_ci break; 60462306a36Sopenharmony_ci mark_inc(mark); 60562306a36Sopenharmony_ci } 60662306a36Sopenharmony_ci 60762306a36Sopenharmony_ci /* 60862306a36Sopenharmony_ci * Now that the new node is fully initialised, we can add 60962306a36Sopenharmony_ci * it to the tree 61062306a36Sopenharmony_ci */ 61162306a36Sopenharmony_ci if (xa_is_node(head)) { 61262306a36Sopenharmony_ci xa_to_node(head)->offset = 0; 61362306a36Sopenharmony_ci rcu_assign_pointer(xa_to_node(head)->parent, node); 61462306a36Sopenharmony_ci } 61562306a36Sopenharmony_ci head = xa_mk_node(node); 61662306a36Sopenharmony_ci rcu_assign_pointer(xa->xa_head, head); 61762306a36Sopenharmony_ci xas_update(xas, node); 61862306a36Sopenharmony_ci 61962306a36Sopenharmony_ci shift += XA_CHUNK_SHIFT; 62062306a36Sopenharmony_ci } 62162306a36Sopenharmony_ci 62262306a36Sopenharmony_ci xas->xa_node = node; 62362306a36Sopenharmony_ci return shift; 62462306a36Sopenharmony_ci} 62562306a36Sopenharmony_ci 62662306a36Sopenharmony_ci/* 62762306a36Sopenharmony_ci * xas_create() - Create a slot to store an entry in. 62862306a36Sopenharmony_ci * @xas: XArray operation state. 62962306a36Sopenharmony_ci * @allow_root: %true if we can store the entry in the root directly 63062306a36Sopenharmony_ci * 63162306a36Sopenharmony_ci * Most users will not need to call this function directly, as it is called 63262306a36Sopenharmony_ci * by xas_store(). It is useful for doing conditional store operations 63362306a36Sopenharmony_ci * (see the xa_cmpxchg() implementation for an example). 63462306a36Sopenharmony_ci * 63562306a36Sopenharmony_ci * Return: If the slot already existed, returns the contents of this slot. 63662306a36Sopenharmony_ci * If the slot was newly created, returns %NULL. If it failed to create the 63762306a36Sopenharmony_ci * slot, returns %NULL and indicates the error in @xas. 63862306a36Sopenharmony_ci */ 63962306a36Sopenharmony_cistatic void *xas_create(struct xa_state *xas, bool allow_root) 64062306a36Sopenharmony_ci{ 64162306a36Sopenharmony_ci struct xarray *xa = xas->xa; 64262306a36Sopenharmony_ci void *entry; 64362306a36Sopenharmony_ci void __rcu **slot; 64462306a36Sopenharmony_ci struct xa_node *node = xas->xa_node; 64562306a36Sopenharmony_ci int shift; 64662306a36Sopenharmony_ci unsigned int order = xas->xa_shift; 64762306a36Sopenharmony_ci 64862306a36Sopenharmony_ci if (xas_top(node)) { 64962306a36Sopenharmony_ci entry = xa_head_locked(xa); 65062306a36Sopenharmony_ci xas->xa_node = NULL; 65162306a36Sopenharmony_ci if (!entry && xa_zero_busy(xa)) 65262306a36Sopenharmony_ci entry = XA_ZERO_ENTRY; 65362306a36Sopenharmony_ci shift = xas_expand(xas, entry); 65462306a36Sopenharmony_ci if (shift < 0) 65562306a36Sopenharmony_ci return NULL; 65662306a36Sopenharmony_ci if (!shift && !allow_root) 65762306a36Sopenharmony_ci shift = XA_CHUNK_SHIFT; 65862306a36Sopenharmony_ci entry = xa_head_locked(xa); 65962306a36Sopenharmony_ci slot = &xa->xa_head; 66062306a36Sopenharmony_ci } else if (xas_error(xas)) { 66162306a36Sopenharmony_ci return NULL; 66262306a36Sopenharmony_ci } else if (node) { 66362306a36Sopenharmony_ci unsigned int offset = xas->xa_offset; 66462306a36Sopenharmony_ci 66562306a36Sopenharmony_ci shift = node->shift; 66662306a36Sopenharmony_ci entry = xa_entry_locked(xa, node, offset); 66762306a36Sopenharmony_ci slot = &node->slots[offset]; 66862306a36Sopenharmony_ci } else { 66962306a36Sopenharmony_ci shift = 0; 67062306a36Sopenharmony_ci entry = xa_head_locked(xa); 67162306a36Sopenharmony_ci slot = &xa->xa_head; 67262306a36Sopenharmony_ci } 67362306a36Sopenharmony_ci 67462306a36Sopenharmony_ci while (shift > order) { 67562306a36Sopenharmony_ci shift -= XA_CHUNK_SHIFT; 67662306a36Sopenharmony_ci if (!entry) { 67762306a36Sopenharmony_ci node = xas_alloc(xas, shift); 67862306a36Sopenharmony_ci if (!node) 67962306a36Sopenharmony_ci break; 68062306a36Sopenharmony_ci if (xa_track_free(xa)) 68162306a36Sopenharmony_ci node_mark_all(node, XA_FREE_MARK); 68262306a36Sopenharmony_ci rcu_assign_pointer(*slot, xa_mk_node(node)); 68362306a36Sopenharmony_ci } else if (xa_is_node(entry)) { 68462306a36Sopenharmony_ci node = xa_to_node(entry); 68562306a36Sopenharmony_ci } else { 68662306a36Sopenharmony_ci break; 68762306a36Sopenharmony_ci } 68862306a36Sopenharmony_ci entry = xas_descend(xas, node); 68962306a36Sopenharmony_ci slot = &node->slots[xas->xa_offset]; 69062306a36Sopenharmony_ci } 69162306a36Sopenharmony_ci 69262306a36Sopenharmony_ci return entry; 69362306a36Sopenharmony_ci} 69462306a36Sopenharmony_ci 69562306a36Sopenharmony_ci/** 69662306a36Sopenharmony_ci * xas_create_range() - Ensure that stores to this range will succeed 69762306a36Sopenharmony_ci * @xas: XArray operation state. 69862306a36Sopenharmony_ci * 69962306a36Sopenharmony_ci * Creates all of the slots in the range covered by @xas. Sets @xas to 70062306a36Sopenharmony_ci * create single-index entries and positions it at the beginning of the 70162306a36Sopenharmony_ci * range. This is for the benefit of users which have not yet been 70262306a36Sopenharmony_ci * converted to use multi-index entries. 70362306a36Sopenharmony_ci */ 70462306a36Sopenharmony_civoid xas_create_range(struct xa_state *xas) 70562306a36Sopenharmony_ci{ 70662306a36Sopenharmony_ci unsigned long index = xas->xa_index; 70762306a36Sopenharmony_ci unsigned char shift = xas->xa_shift; 70862306a36Sopenharmony_ci unsigned char sibs = xas->xa_sibs; 70962306a36Sopenharmony_ci 71062306a36Sopenharmony_ci xas->xa_index |= ((sibs + 1UL) << shift) - 1; 71162306a36Sopenharmony_ci if (xas_is_node(xas) && xas->xa_node->shift == xas->xa_shift) 71262306a36Sopenharmony_ci xas->xa_offset |= sibs; 71362306a36Sopenharmony_ci xas->xa_shift = 0; 71462306a36Sopenharmony_ci xas->xa_sibs = 0; 71562306a36Sopenharmony_ci 71662306a36Sopenharmony_ci for (;;) { 71762306a36Sopenharmony_ci xas_create(xas, true); 71862306a36Sopenharmony_ci if (xas_error(xas)) 71962306a36Sopenharmony_ci goto restore; 72062306a36Sopenharmony_ci if (xas->xa_index <= (index | XA_CHUNK_MASK)) 72162306a36Sopenharmony_ci goto success; 72262306a36Sopenharmony_ci xas->xa_index -= XA_CHUNK_SIZE; 72362306a36Sopenharmony_ci 72462306a36Sopenharmony_ci for (;;) { 72562306a36Sopenharmony_ci struct xa_node *node = xas->xa_node; 72662306a36Sopenharmony_ci if (node->shift >= shift) 72762306a36Sopenharmony_ci break; 72862306a36Sopenharmony_ci xas->xa_node = xa_parent_locked(xas->xa, node); 72962306a36Sopenharmony_ci xas->xa_offset = node->offset - 1; 73062306a36Sopenharmony_ci if (node->offset != 0) 73162306a36Sopenharmony_ci break; 73262306a36Sopenharmony_ci } 73362306a36Sopenharmony_ci } 73462306a36Sopenharmony_ci 73562306a36Sopenharmony_cirestore: 73662306a36Sopenharmony_ci xas->xa_shift = shift; 73762306a36Sopenharmony_ci xas->xa_sibs = sibs; 73862306a36Sopenharmony_ci xas->xa_index = index; 73962306a36Sopenharmony_ci return; 74062306a36Sopenharmony_cisuccess: 74162306a36Sopenharmony_ci xas->xa_index = index; 74262306a36Sopenharmony_ci if (xas->xa_node) 74362306a36Sopenharmony_ci xas_set_offset(xas); 74462306a36Sopenharmony_ci} 74562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(xas_create_range); 74662306a36Sopenharmony_ci 74762306a36Sopenharmony_cistatic void update_node(struct xa_state *xas, struct xa_node *node, 74862306a36Sopenharmony_ci int count, int values) 74962306a36Sopenharmony_ci{ 75062306a36Sopenharmony_ci if (!node || (!count && !values)) 75162306a36Sopenharmony_ci return; 75262306a36Sopenharmony_ci 75362306a36Sopenharmony_ci node->count += count; 75462306a36Sopenharmony_ci node->nr_values += values; 75562306a36Sopenharmony_ci XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE); 75662306a36Sopenharmony_ci XA_NODE_BUG_ON(node, node->nr_values > XA_CHUNK_SIZE); 75762306a36Sopenharmony_ci xas_update(xas, node); 75862306a36Sopenharmony_ci if (count < 0) 75962306a36Sopenharmony_ci xas_delete_node(xas); 76062306a36Sopenharmony_ci} 76162306a36Sopenharmony_ci 76262306a36Sopenharmony_ci/** 76362306a36Sopenharmony_ci * xas_store() - Store this entry in the XArray. 76462306a36Sopenharmony_ci * @xas: XArray operation state. 76562306a36Sopenharmony_ci * @entry: New entry. 76662306a36Sopenharmony_ci * 76762306a36Sopenharmony_ci * If @xas is operating on a multi-index entry, the entry returned by this 76862306a36Sopenharmony_ci * function is essentially meaningless (it may be an internal entry or it 76962306a36Sopenharmony_ci * may be %NULL, even if there are non-NULL entries at some of the indices 77062306a36Sopenharmony_ci * covered by the range). This is not a problem for any current users, 77162306a36Sopenharmony_ci * and can be changed if needed. 77262306a36Sopenharmony_ci * 77362306a36Sopenharmony_ci * Return: The old entry at this index. 77462306a36Sopenharmony_ci */ 77562306a36Sopenharmony_civoid *xas_store(struct xa_state *xas, void *entry) 77662306a36Sopenharmony_ci{ 77762306a36Sopenharmony_ci struct xa_node *node; 77862306a36Sopenharmony_ci void __rcu **slot = &xas->xa->xa_head; 77962306a36Sopenharmony_ci unsigned int offset, max; 78062306a36Sopenharmony_ci int count = 0; 78162306a36Sopenharmony_ci int values = 0; 78262306a36Sopenharmony_ci void *first, *next; 78362306a36Sopenharmony_ci bool value = xa_is_value(entry); 78462306a36Sopenharmony_ci 78562306a36Sopenharmony_ci if (entry) { 78662306a36Sopenharmony_ci bool allow_root = !xa_is_node(entry) && !xa_is_zero(entry); 78762306a36Sopenharmony_ci first = xas_create(xas, allow_root); 78862306a36Sopenharmony_ci } else { 78962306a36Sopenharmony_ci first = xas_load(xas); 79062306a36Sopenharmony_ci } 79162306a36Sopenharmony_ci 79262306a36Sopenharmony_ci if (xas_invalid(xas)) 79362306a36Sopenharmony_ci return first; 79462306a36Sopenharmony_ci node = xas->xa_node; 79562306a36Sopenharmony_ci if (node && (xas->xa_shift < node->shift)) 79662306a36Sopenharmony_ci xas->xa_sibs = 0; 79762306a36Sopenharmony_ci if ((first == entry) && !xas->xa_sibs) 79862306a36Sopenharmony_ci return first; 79962306a36Sopenharmony_ci 80062306a36Sopenharmony_ci next = first; 80162306a36Sopenharmony_ci offset = xas->xa_offset; 80262306a36Sopenharmony_ci max = xas->xa_offset + xas->xa_sibs; 80362306a36Sopenharmony_ci if (node) { 80462306a36Sopenharmony_ci slot = &node->slots[offset]; 80562306a36Sopenharmony_ci if (xas->xa_sibs) 80662306a36Sopenharmony_ci xas_squash_marks(xas); 80762306a36Sopenharmony_ci } 80862306a36Sopenharmony_ci if (!entry) 80962306a36Sopenharmony_ci xas_init_marks(xas); 81062306a36Sopenharmony_ci 81162306a36Sopenharmony_ci for (;;) { 81262306a36Sopenharmony_ci /* 81362306a36Sopenharmony_ci * Must clear the marks before setting the entry to NULL, 81462306a36Sopenharmony_ci * otherwise xas_for_each_marked may find a NULL entry and 81562306a36Sopenharmony_ci * stop early. rcu_assign_pointer contains a release barrier 81662306a36Sopenharmony_ci * so the mark clearing will appear to happen before the 81762306a36Sopenharmony_ci * entry is set to NULL. 81862306a36Sopenharmony_ci */ 81962306a36Sopenharmony_ci rcu_assign_pointer(*slot, entry); 82062306a36Sopenharmony_ci if (xa_is_node(next) && (!node || node->shift)) 82162306a36Sopenharmony_ci xas_free_nodes(xas, xa_to_node(next)); 82262306a36Sopenharmony_ci if (!node) 82362306a36Sopenharmony_ci break; 82462306a36Sopenharmony_ci count += !next - !entry; 82562306a36Sopenharmony_ci values += !xa_is_value(first) - !value; 82662306a36Sopenharmony_ci if (entry) { 82762306a36Sopenharmony_ci if (offset == max) 82862306a36Sopenharmony_ci break; 82962306a36Sopenharmony_ci if (!xa_is_sibling(entry)) 83062306a36Sopenharmony_ci entry = xa_mk_sibling(xas->xa_offset); 83162306a36Sopenharmony_ci } else { 83262306a36Sopenharmony_ci if (offset == XA_CHUNK_MASK) 83362306a36Sopenharmony_ci break; 83462306a36Sopenharmony_ci } 83562306a36Sopenharmony_ci next = xa_entry_locked(xas->xa, node, ++offset); 83662306a36Sopenharmony_ci if (!xa_is_sibling(next)) { 83762306a36Sopenharmony_ci if (!entry && (offset > max)) 83862306a36Sopenharmony_ci break; 83962306a36Sopenharmony_ci first = next; 84062306a36Sopenharmony_ci } 84162306a36Sopenharmony_ci slot++; 84262306a36Sopenharmony_ci } 84362306a36Sopenharmony_ci 84462306a36Sopenharmony_ci update_node(xas, node, count, values); 84562306a36Sopenharmony_ci return first; 84662306a36Sopenharmony_ci} 84762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(xas_store); 84862306a36Sopenharmony_ci 84962306a36Sopenharmony_ci/** 85062306a36Sopenharmony_ci * xas_get_mark() - Returns the state of this mark. 85162306a36Sopenharmony_ci * @xas: XArray operation state. 85262306a36Sopenharmony_ci * @mark: Mark number. 85362306a36Sopenharmony_ci * 85462306a36Sopenharmony_ci * Return: true if the mark is set, false if the mark is clear or @xas 85562306a36Sopenharmony_ci * is in an error state. 85662306a36Sopenharmony_ci */ 85762306a36Sopenharmony_cibool xas_get_mark(const struct xa_state *xas, xa_mark_t mark) 85862306a36Sopenharmony_ci{ 85962306a36Sopenharmony_ci if (xas_invalid(xas)) 86062306a36Sopenharmony_ci return false; 86162306a36Sopenharmony_ci if (!xas->xa_node) 86262306a36Sopenharmony_ci return xa_marked(xas->xa, mark); 86362306a36Sopenharmony_ci return node_get_mark(xas->xa_node, xas->xa_offset, mark); 86462306a36Sopenharmony_ci} 86562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(xas_get_mark); 86662306a36Sopenharmony_ci 86762306a36Sopenharmony_ci/** 86862306a36Sopenharmony_ci * xas_set_mark() - Sets the mark on this entry and its parents. 86962306a36Sopenharmony_ci * @xas: XArray operation state. 87062306a36Sopenharmony_ci * @mark: Mark number. 87162306a36Sopenharmony_ci * 87262306a36Sopenharmony_ci * Sets the specified mark on this entry, and walks up the tree setting it 87362306a36Sopenharmony_ci * on all the ancestor entries. Does nothing if @xas has not been walked to 87462306a36Sopenharmony_ci * an entry, or is in an error state. 87562306a36Sopenharmony_ci */ 87662306a36Sopenharmony_civoid xas_set_mark(const struct xa_state *xas, xa_mark_t mark) 87762306a36Sopenharmony_ci{ 87862306a36Sopenharmony_ci struct xa_node *node = xas->xa_node; 87962306a36Sopenharmony_ci unsigned int offset = xas->xa_offset; 88062306a36Sopenharmony_ci 88162306a36Sopenharmony_ci if (xas_invalid(xas)) 88262306a36Sopenharmony_ci return; 88362306a36Sopenharmony_ci 88462306a36Sopenharmony_ci while (node) { 88562306a36Sopenharmony_ci if (node_set_mark(node, offset, mark)) 88662306a36Sopenharmony_ci return; 88762306a36Sopenharmony_ci offset = node->offset; 88862306a36Sopenharmony_ci node = xa_parent_locked(xas->xa, node); 88962306a36Sopenharmony_ci } 89062306a36Sopenharmony_ci 89162306a36Sopenharmony_ci if (!xa_marked(xas->xa, mark)) 89262306a36Sopenharmony_ci xa_mark_set(xas->xa, mark); 89362306a36Sopenharmony_ci} 89462306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(xas_set_mark); 89562306a36Sopenharmony_ci 89662306a36Sopenharmony_ci/** 89762306a36Sopenharmony_ci * xas_clear_mark() - Clears the mark on this entry and its parents. 89862306a36Sopenharmony_ci * @xas: XArray operation state. 89962306a36Sopenharmony_ci * @mark: Mark number. 90062306a36Sopenharmony_ci * 90162306a36Sopenharmony_ci * Clears the specified mark on this entry, and walks back to the head 90262306a36Sopenharmony_ci * attempting to clear it on all the ancestor entries. Does nothing if 90362306a36Sopenharmony_ci * @xas has not been walked to an entry, or is in an error state. 90462306a36Sopenharmony_ci */ 90562306a36Sopenharmony_civoid xas_clear_mark(const struct xa_state *xas, xa_mark_t mark) 90662306a36Sopenharmony_ci{ 90762306a36Sopenharmony_ci struct xa_node *node = xas->xa_node; 90862306a36Sopenharmony_ci unsigned int offset = xas->xa_offset; 90962306a36Sopenharmony_ci 91062306a36Sopenharmony_ci if (xas_invalid(xas)) 91162306a36Sopenharmony_ci return; 91262306a36Sopenharmony_ci 91362306a36Sopenharmony_ci while (node) { 91462306a36Sopenharmony_ci if (!node_clear_mark(node, offset, mark)) 91562306a36Sopenharmony_ci return; 91662306a36Sopenharmony_ci if (node_any_mark(node, mark)) 91762306a36Sopenharmony_ci return; 91862306a36Sopenharmony_ci 91962306a36Sopenharmony_ci offset = node->offset; 92062306a36Sopenharmony_ci node = xa_parent_locked(xas->xa, node); 92162306a36Sopenharmony_ci } 92262306a36Sopenharmony_ci 92362306a36Sopenharmony_ci if (xa_marked(xas->xa, mark)) 92462306a36Sopenharmony_ci xa_mark_clear(xas->xa, mark); 92562306a36Sopenharmony_ci} 92662306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(xas_clear_mark); 92762306a36Sopenharmony_ci 92862306a36Sopenharmony_ci/** 92962306a36Sopenharmony_ci * xas_init_marks() - Initialise all marks for the entry 93062306a36Sopenharmony_ci * @xas: Array operations state. 93162306a36Sopenharmony_ci * 93262306a36Sopenharmony_ci * Initialise all marks for the entry specified by @xas. If we're tracking 93362306a36Sopenharmony_ci * free entries with a mark, we need to set it on all entries. All other 93462306a36Sopenharmony_ci * marks are cleared. 93562306a36Sopenharmony_ci * 93662306a36Sopenharmony_ci * This implementation is not as efficient as it could be; we may walk 93762306a36Sopenharmony_ci * up the tree multiple times. 93862306a36Sopenharmony_ci */ 93962306a36Sopenharmony_civoid xas_init_marks(const struct xa_state *xas) 94062306a36Sopenharmony_ci{ 94162306a36Sopenharmony_ci xa_mark_t mark = 0; 94262306a36Sopenharmony_ci 94362306a36Sopenharmony_ci for (;;) { 94462306a36Sopenharmony_ci if (xa_track_free(xas->xa) && mark == XA_FREE_MARK) 94562306a36Sopenharmony_ci xas_set_mark(xas, mark); 94662306a36Sopenharmony_ci else 94762306a36Sopenharmony_ci xas_clear_mark(xas, mark); 94862306a36Sopenharmony_ci if (mark == XA_MARK_MAX) 94962306a36Sopenharmony_ci break; 95062306a36Sopenharmony_ci mark_inc(mark); 95162306a36Sopenharmony_ci } 95262306a36Sopenharmony_ci} 95362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(xas_init_marks); 95462306a36Sopenharmony_ci 95562306a36Sopenharmony_ci#ifdef CONFIG_XARRAY_MULTI 95662306a36Sopenharmony_cistatic unsigned int node_get_marks(struct xa_node *node, unsigned int offset) 95762306a36Sopenharmony_ci{ 95862306a36Sopenharmony_ci unsigned int marks = 0; 95962306a36Sopenharmony_ci xa_mark_t mark = XA_MARK_0; 96062306a36Sopenharmony_ci 96162306a36Sopenharmony_ci for (;;) { 96262306a36Sopenharmony_ci if (node_get_mark(node, offset, mark)) 96362306a36Sopenharmony_ci marks |= 1 << (__force unsigned int)mark; 96462306a36Sopenharmony_ci if (mark == XA_MARK_MAX) 96562306a36Sopenharmony_ci break; 96662306a36Sopenharmony_ci mark_inc(mark); 96762306a36Sopenharmony_ci } 96862306a36Sopenharmony_ci 96962306a36Sopenharmony_ci return marks; 97062306a36Sopenharmony_ci} 97162306a36Sopenharmony_ci 97262306a36Sopenharmony_cistatic void node_set_marks(struct xa_node *node, unsigned int offset, 97362306a36Sopenharmony_ci struct xa_node *child, unsigned int marks) 97462306a36Sopenharmony_ci{ 97562306a36Sopenharmony_ci xa_mark_t mark = XA_MARK_0; 97662306a36Sopenharmony_ci 97762306a36Sopenharmony_ci for (;;) { 97862306a36Sopenharmony_ci if (marks & (1 << (__force unsigned int)mark)) { 97962306a36Sopenharmony_ci node_set_mark(node, offset, mark); 98062306a36Sopenharmony_ci if (child) 98162306a36Sopenharmony_ci node_mark_all(child, mark); 98262306a36Sopenharmony_ci } 98362306a36Sopenharmony_ci if (mark == XA_MARK_MAX) 98462306a36Sopenharmony_ci break; 98562306a36Sopenharmony_ci mark_inc(mark); 98662306a36Sopenharmony_ci } 98762306a36Sopenharmony_ci} 98862306a36Sopenharmony_ci 98962306a36Sopenharmony_ci/** 99062306a36Sopenharmony_ci * xas_split_alloc() - Allocate memory for splitting an entry. 99162306a36Sopenharmony_ci * @xas: XArray operation state. 99262306a36Sopenharmony_ci * @entry: New entry which will be stored in the array. 99362306a36Sopenharmony_ci * @order: Current entry order. 99462306a36Sopenharmony_ci * @gfp: Memory allocation flags. 99562306a36Sopenharmony_ci * 99662306a36Sopenharmony_ci * This function should be called before calling xas_split(). 99762306a36Sopenharmony_ci * If necessary, it will allocate new nodes (and fill them with @entry) 99862306a36Sopenharmony_ci * to prepare for the upcoming split of an entry of @order size into 99962306a36Sopenharmony_ci * entries of the order stored in the @xas. 100062306a36Sopenharmony_ci * 100162306a36Sopenharmony_ci * Context: May sleep if @gfp flags permit. 100262306a36Sopenharmony_ci */ 100362306a36Sopenharmony_civoid xas_split_alloc(struct xa_state *xas, void *entry, unsigned int order, 100462306a36Sopenharmony_ci gfp_t gfp) 100562306a36Sopenharmony_ci{ 100662306a36Sopenharmony_ci unsigned int sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1; 100762306a36Sopenharmony_ci unsigned int mask = xas->xa_sibs; 100862306a36Sopenharmony_ci 100962306a36Sopenharmony_ci /* XXX: no support for splitting really large entries yet */ 101062306a36Sopenharmony_ci if (WARN_ON(xas->xa_shift + 2 * XA_CHUNK_SHIFT < order)) 101162306a36Sopenharmony_ci goto nomem; 101262306a36Sopenharmony_ci if (xas->xa_shift + XA_CHUNK_SHIFT > order) 101362306a36Sopenharmony_ci return; 101462306a36Sopenharmony_ci 101562306a36Sopenharmony_ci do { 101662306a36Sopenharmony_ci unsigned int i; 101762306a36Sopenharmony_ci void *sibling = NULL; 101862306a36Sopenharmony_ci struct xa_node *node; 101962306a36Sopenharmony_ci 102062306a36Sopenharmony_ci node = kmem_cache_alloc_lru(radix_tree_node_cachep, xas->xa_lru, gfp); 102162306a36Sopenharmony_ci if (!node) 102262306a36Sopenharmony_ci goto nomem; 102362306a36Sopenharmony_ci node->array = xas->xa; 102462306a36Sopenharmony_ci for (i = 0; i < XA_CHUNK_SIZE; i++) { 102562306a36Sopenharmony_ci if ((i & mask) == 0) { 102662306a36Sopenharmony_ci RCU_INIT_POINTER(node->slots[i], entry); 102762306a36Sopenharmony_ci sibling = xa_mk_sibling(i); 102862306a36Sopenharmony_ci } else { 102962306a36Sopenharmony_ci RCU_INIT_POINTER(node->slots[i], sibling); 103062306a36Sopenharmony_ci } 103162306a36Sopenharmony_ci } 103262306a36Sopenharmony_ci RCU_INIT_POINTER(node->parent, xas->xa_alloc); 103362306a36Sopenharmony_ci xas->xa_alloc = node; 103462306a36Sopenharmony_ci } while (sibs-- > 0); 103562306a36Sopenharmony_ci 103662306a36Sopenharmony_ci return; 103762306a36Sopenharmony_cinomem: 103862306a36Sopenharmony_ci xas_destroy(xas); 103962306a36Sopenharmony_ci xas_set_err(xas, -ENOMEM); 104062306a36Sopenharmony_ci} 104162306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(xas_split_alloc); 104262306a36Sopenharmony_ci 104362306a36Sopenharmony_ci/** 104462306a36Sopenharmony_ci * xas_split() - Split a multi-index entry into smaller entries. 104562306a36Sopenharmony_ci * @xas: XArray operation state. 104662306a36Sopenharmony_ci * @entry: New entry to store in the array. 104762306a36Sopenharmony_ci * @order: Current entry order. 104862306a36Sopenharmony_ci * 104962306a36Sopenharmony_ci * The size of the new entries is set in @xas. The value in @entry is 105062306a36Sopenharmony_ci * copied to all the replacement entries. 105162306a36Sopenharmony_ci * 105262306a36Sopenharmony_ci * Context: Any context. The caller should hold the xa_lock. 105362306a36Sopenharmony_ci */ 105462306a36Sopenharmony_civoid xas_split(struct xa_state *xas, void *entry, unsigned int order) 105562306a36Sopenharmony_ci{ 105662306a36Sopenharmony_ci unsigned int sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1; 105762306a36Sopenharmony_ci unsigned int offset, marks; 105862306a36Sopenharmony_ci struct xa_node *node; 105962306a36Sopenharmony_ci void *curr = xas_load(xas); 106062306a36Sopenharmony_ci int values = 0; 106162306a36Sopenharmony_ci 106262306a36Sopenharmony_ci node = xas->xa_node; 106362306a36Sopenharmony_ci if (xas_top(node)) 106462306a36Sopenharmony_ci return; 106562306a36Sopenharmony_ci 106662306a36Sopenharmony_ci marks = node_get_marks(node, xas->xa_offset); 106762306a36Sopenharmony_ci 106862306a36Sopenharmony_ci offset = xas->xa_offset + sibs; 106962306a36Sopenharmony_ci do { 107062306a36Sopenharmony_ci if (xas->xa_shift < node->shift) { 107162306a36Sopenharmony_ci struct xa_node *child = xas->xa_alloc; 107262306a36Sopenharmony_ci 107362306a36Sopenharmony_ci xas->xa_alloc = rcu_dereference_raw(child->parent); 107462306a36Sopenharmony_ci child->shift = node->shift - XA_CHUNK_SHIFT; 107562306a36Sopenharmony_ci child->offset = offset; 107662306a36Sopenharmony_ci child->count = XA_CHUNK_SIZE; 107762306a36Sopenharmony_ci child->nr_values = xa_is_value(entry) ? 107862306a36Sopenharmony_ci XA_CHUNK_SIZE : 0; 107962306a36Sopenharmony_ci RCU_INIT_POINTER(child->parent, node); 108062306a36Sopenharmony_ci node_set_marks(node, offset, child, marks); 108162306a36Sopenharmony_ci rcu_assign_pointer(node->slots[offset], 108262306a36Sopenharmony_ci xa_mk_node(child)); 108362306a36Sopenharmony_ci if (xa_is_value(curr)) 108462306a36Sopenharmony_ci values--; 108562306a36Sopenharmony_ci xas_update(xas, child); 108662306a36Sopenharmony_ci } else { 108762306a36Sopenharmony_ci unsigned int canon = offset - xas->xa_sibs; 108862306a36Sopenharmony_ci 108962306a36Sopenharmony_ci node_set_marks(node, canon, NULL, marks); 109062306a36Sopenharmony_ci rcu_assign_pointer(node->slots[canon], entry); 109162306a36Sopenharmony_ci while (offset > canon) 109262306a36Sopenharmony_ci rcu_assign_pointer(node->slots[offset--], 109362306a36Sopenharmony_ci xa_mk_sibling(canon)); 109462306a36Sopenharmony_ci values += (xa_is_value(entry) - xa_is_value(curr)) * 109562306a36Sopenharmony_ci (xas->xa_sibs + 1); 109662306a36Sopenharmony_ci } 109762306a36Sopenharmony_ci } while (offset-- > xas->xa_offset); 109862306a36Sopenharmony_ci 109962306a36Sopenharmony_ci node->nr_values += values; 110062306a36Sopenharmony_ci xas_update(xas, node); 110162306a36Sopenharmony_ci} 110262306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(xas_split); 110362306a36Sopenharmony_ci#endif 110462306a36Sopenharmony_ci 110562306a36Sopenharmony_ci/** 110662306a36Sopenharmony_ci * xas_pause() - Pause a walk to drop a lock. 110762306a36Sopenharmony_ci * @xas: XArray operation state. 110862306a36Sopenharmony_ci * 110962306a36Sopenharmony_ci * Some users need to pause a walk and drop the lock they're holding in 111062306a36Sopenharmony_ci * order to yield to a higher priority thread or carry out an operation 111162306a36Sopenharmony_ci * on an entry. Those users should call this function before they drop 111262306a36Sopenharmony_ci * the lock. It resets the @xas to be suitable for the next iteration 111362306a36Sopenharmony_ci * of the loop after the user has reacquired the lock. If most entries 111462306a36Sopenharmony_ci * found during a walk require you to call xas_pause(), the xa_for_each() 111562306a36Sopenharmony_ci * iterator may be more appropriate. 111662306a36Sopenharmony_ci * 111762306a36Sopenharmony_ci * Note that xas_pause() only works for forward iteration. If a user needs 111862306a36Sopenharmony_ci * to pause a reverse iteration, we will need a xas_pause_rev(). 111962306a36Sopenharmony_ci */ 112062306a36Sopenharmony_civoid xas_pause(struct xa_state *xas) 112162306a36Sopenharmony_ci{ 112262306a36Sopenharmony_ci struct xa_node *node = xas->xa_node; 112362306a36Sopenharmony_ci 112462306a36Sopenharmony_ci if (xas_invalid(xas)) 112562306a36Sopenharmony_ci return; 112662306a36Sopenharmony_ci 112762306a36Sopenharmony_ci xas->xa_node = XAS_RESTART; 112862306a36Sopenharmony_ci if (node) { 112962306a36Sopenharmony_ci unsigned long offset = xas->xa_offset; 113062306a36Sopenharmony_ci while (++offset < XA_CHUNK_SIZE) { 113162306a36Sopenharmony_ci if (!xa_is_sibling(xa_entry(xas->xa, node, offset))) 113262306a36Sopenharmony_ci break; 113362306a36Sopenharmony_ci } 113462306a36Sopenharmony_ci xas->xa_index += (offset - xas->xa_offset) << node->shift; 113562306a36Sopenharmony_ci if (xas->xa_index == 0) 113662306a36Sopenharmony_ci xas->xa_node = XAS_BOUNDS; 113762306a36Sopenharmony_ci } else { 113862306a36Sopenharmony_ci xas->xa_index++; 113962306a36Sopenharmony_ci } 114062306a36Sopenharmony_ci} 114162306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(xas_pause); 114262306a36Sopenharmony_ci 114362306a36Sopenharmony_ci/* 114462306a36Sopenharmony_ci * __xas_prev() - Find the previous entry in the XArray. 114562306a36Sopenharmony_ci * @xas: XArray operation state. 114662306a36Sopenharmony_ci * 114762306a36Sopenharmony_ci * Helper function for xas_prev() which handles all the complex cases 114862306a36Sopenharmony_ci * out of line. 114962306a36Sopenharmony_ci */ 115062306a36Sopenharmony_civoid *__xas_prev(struct xa_state *xas) 115162306a36Sopenharmony_ci{ 115262306a36Sopenharmony_ci void *entry; 115362306a36Sopenharmony_ci 115462306a36Sopenharmony_ci if (!xas_frozen(xas->xa_node)) 115562306a36Sopenharmony_ci xas->xa_index--; 115662306a36Sopenharmony_ci if (!xas->xa_node) 115762306a36Sopenharmony_ci return set_bounds(xas); 115862306a36Sopenharmony_ci if (xas_not_node(xas->xa_node)) 115962306a36Sopenharmony_ci return xas_load(xas); 116062306a36Sopenharmony_ci 116162306a36Sopenharmony_ci if (xas->xa_offset != get_offset(xas->xa_index, xas->xa_node)) 116262306a36Sopenharmony_ci xas->xa_offset--; 116362306a36Sopenharmony_ci 116462306a36Sopenharmony_ci while (xas->xa_offset == 255) { 116562306a36Sopenharmony_ci xas->xa_offset = xas->xa_node->offset - 1; 116662306a36Sopenharmony_ci xas->xa_node = xa_parent(xas->xa, xas->xa_node); 116762306a36Sopenharmony_ci if (!xas->xa_node) 116862306a36Sopenharmony_ci return set_bounds(xas); 116962306a36Sopenharmony_ci } 117062306a36Sopenharmony_ci 117162306a36Sopenharmony_ci for (;;) { 117262306a36Sopenharmony_ci entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset); 117362306a36Sopenharmony_ci if (!xa_is_node(entry)) 117462306a36Sopenharmony_ci return entry; 117562306a36Sopenharmony_ci 117662306a36Sopenharmony_ci xas->xa_node = xa_to_node(entry); 117762306a36Sopenharmony_ci xas_set_offset(xas); 117862306a36Sopenharmony_ci } 117962306a36Sopenharmony_ci} 118062306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(__xas_prev); 118162306a36Sopenharmony_ci 118262306a36Sopenharmony_ci/* 118362306a36Sopenharmony_ci * __xas_next() - Find the next entry in the XArray. 118462306a36Sopenharmony_ci * @xas: XArray operation state. 118562306a36Sopenharmony_ci * 118662306a36Sopenharmony_ci * Helper function for xas_next() which handles all the complex cases 118762306a36Sopenharmony_ci * out of line. 118862306a36Sopenharmony_ci */ 118962306a36Sopenharmony_civoid *__xas_next(struct xa_state *xas) 119062306a36Sopenharmony_ci{ 119162306a36Sopenharmony_ci void *entry; 119262306a36Sopenharmony_ci 119362306a36Sopenharmony_ci if (!xas_frozen(xas->xa_node)) 119462306a36Sopenharmony_ci xas->xa_index++; 119562306a36Sopenharmony_ci if (!xas->xa_node) 119662306a36Sopenharmony_ci return set_bounds(xas); 119762306a36Sopenharmony_ci if (xas_not_node(xas->xa_node)) 119862306a36Sopenharmony_ci return xas_load(xas); 119962306a36Sopenharmony_ci 120062306a36Sopenharmony_ci if (xas->xa_offset != get_offset(xas->xa_index, xas->xa_node)) 120162306a36Sopenharmony_ci xas->xa_offset++; 120262306a36Sopenharmony_ci 120362306a36Sopenharmony_ci while (xas->xa_offset == XA_CHUNK_SIZE) { 120462306a36Sopenharmony_ci xas->xa_offset = xas->xa_node->offset + 1; 120562306a36Sopenharmony_ci xas->xa_node = xa_parent(xas->xa, xas->xa_node); 120662306a36Sopenharmony_ci if (!xas->xa_node) 120762306a36Sopenharmony_ci return set_bounds(xas); 120862306a36Sopenharmony_ci } 120962306a36Sopenharmony_ci 121062306a36Sopenharmony_ci for (;;) { 121162306a36Sopenharmony_ci entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset); 121262306a36Sopenharmony_ci if (!xa_is_node(entry)) 121362306a36Sopenharmony_ci return entry; 121462306a36Sopenharmony_ci 121562306a36Sopenharmony_ci xas->xa_node = xa_to_node(entry); 121662306a36Sopenharmony_ci xas_set_offset(xas); 121762306a36Sopenharmony_ci } 121862306a36Sopenharmony_ci} 121962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(__xas_next); 122062306a36Sopenharmony_ci 122162306a36Sopenharmony_ci/** 122262306a36Sopenharmony_ci * xas_find() - Find the next present entry in the XArray. 122362306a36Sopenharmony_ci * @xas: XArray operation state. 122462306a36Sopenharmony_ci * @max: Highest index to return. 122562306a36Sopenharmony_ci * 122662306a36Sopenharmony_ci * If the @xas has not yet been walked to an entry, return the entry 122762306a36Sopenharmony_ci * which has an index >= xas.xa_index. If it has been walked, the entry 122862306a36Sopenharmony_ci * currently being pointed at has been processed, and so we move to the 122962306a36Sopenharmony_ci * next entry. 123062306a36Sopenharmony_ci * 123162306a36Sopenharmony_ci * If no entry is found and the array is smaller than @max, the iterator 123262306a36Sopenharmony_ci * is set to the smallest index not yet in the array. This allows @xas 123362306a36Sopenharmony_ci * to be immediately passed to xas_store(). 123462306a36Sopenharmony_ci * 123562306a36Sopenharmony_ci * Return: The entry, if found, otherwise %NULL. 123662306a36Sopenharmony_ci */ 123762306a36Sopenharmony_civoid *xas_find(struct xa_state *xas, unsigned long max) 123862306a36Sopenharmony_ci{ 123962306a36Sopenharmony_ci void *entry; 124062306a36Sopenharmony_ci 124162306a36Sopenharmony_ci if (xas_error(xas) || xas->xa_node == XAS_BOUNDS) 124262306a36Sopenharmony_ci return NULL; 124362306a36Sopenharmony_ci if (xas->xa_index > max) 124462306a36Sopenharmony_ci return set_bounds(xas); 124562306a36Sopenharmony_ci 124662306a36Sopenharmony_ci if (!xas->xa_node) { 124762306a36Sopenharmony_ci xas->xa_index = 1; 124862306a36Sopenharmony_ci return set_bounds(xas); 124962306a36Sopenharmony_ci } else if (xas->xa_node == XAS_RESTART) { 125062306a36Sopenharmony_ci entry = xas_load(xas); 125162306a36Sopenharmony_ci if (entry || xas_not_node(xas->xa_node)) 125262306a36Sopenharmony_ci return entry; 125362306a36Sopenharmony_ci } else if (!xas->xa_node->shift && 125462306a36Sopenharmony_ci xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)) { 125562306a36Sopenharmony_ci xas->xa_offset = ((xas->xa_index - 1) & XA_CHUNK_MASK) + 1; 125662306a36Sopenharmony_ci } 125762306a36Sopenharmony_ci 125862306a36Sopenharmony_ci xas_next_offset(xas); 125962306a36Sopenharmony_ci 126062306a36Sopenharmony_ci while (xas->xa_node && (xas->xa_index <= max)) { 126162306a36Sopenharmony_ci if (unlikely(xas->xa_offset == XA_CHUNK_SIZE)) { 126262306a36Sopenharmony_ci xas->xa_offset = xas->xa_node->offset + 1; 126362306a36Sopenharmony_ci xas->xa_node = xa_parent(xas->xa, xas->xa_node); 126462306a36Sopenharmony_ci continue; 126562306a36Sopenharmony_ci } 126662306a36Sopenharmony_ci 126762306a36Sopenharmony_ci entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset); 126862306a36Sopenharmony_ci if (xa_is_node(entry)) { 126962306a36Sopenharmony_ci xas->xa_node = xa_to_node(entry); 127062306a36Sopenharmony_ci xas->xa_offset = 0; 127162306a36Sopenharmony_ci continue; 127262306a36Sopenharmony_ci } 127362306a36Sopenharmony_ci if (entry && !xa_is_sibling(entry)) 127462306a36Sopenharmony_ci return entry; 127562306a36Sopenharmony_ci 127662306a36Sopenharmony_ci xas_next_offset(xas); 127762306a36Sopenharmony_ci } 127862306a36Sopenharmony_ci 127962306a36Sopenharmony_ci if (!xas->xa_node) 128062306a36Sopenharmony_ci xas->xa_node = XAS_BOUNDS; 128162306a36Sopenharmony_ci return NULL; 128262306a36Sopenharmony_ci} 128362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(xas_find); 128462306a36Sopenharmony_ci 128562306a36Sopenharmony_ci/** 128662306a36Sopenharmony_ci * xas_find_marked() - Find the next marked entry in the XArray. 128762306a36Sopenharmony_ci * @xas: XArray operation state. 128862306a36Sopenharmony_ci * @max: Highest index to return. 128962306a36Sopenharmony_ci * @mark: Mark number to search for. 129062306a36Sopenharmony_ci * 129162306a36Sopenharmony_ci * If the @xas has not yet been walked to an entry, return the marked entry 129262306a36Sopenharmony_ci * which has an index >= xas.xa_index. If it has been walked, the entry 129362306a36Sopenharmony_ci * currently being pointed at has been processed, and so we return the 129462306a36Sopenharmony_ci * first marked entry with an index > xas.xa_index. 129562306a36Sopenharmony_ci * 129662306a36Sopenharmony_ci * If no marked entry is found and the array is smaller than @max, @xas is 129762306a36Sopenharmony_ci * set to the bounds state and xas->xa_index is set to the smallest index 129862306a36Sopenharmony_ci * not yet in the array. This allows @xas to be immediately passed to 129962306a36Sopenharmony_ci * xas_store(). 130062306a36Sopenharmony_ci * 130162306a36Sopenharmony_ci * If no entry is found before @max is reached, @xas is set to the restart 130262306a36Sopenharmony_ci * state. 130362306a36Sopenharmony_ci * 130462306a36Sopenharmony_ci * Return: The entry, if found, otherwise %NULL. 130562306a36Sopenharmony_ci */ 130662306a36Sopenharmony_civoid *xas_find_marked(struct xa_state *xas, unsigned long max, xa_mark_t mark) 130762306a36Sopenharmony_ci{ 130862306a36Sopenharmony_ci bool advance = true; 130962306a36Sopenharmony_ci unsigned int offset; 131062306a36Sopenharmony_ci void *entry; 131162306a36Sopenharmony_ci 131262306a36Sopenharmony_ci if (xas_error(xas)) 131362306a36Sopenharmony_ci return NULL; 131462306a36Sopenharmony_ci if (xas->xa_index > max) 131562306a36Sopenharmony_ci goto max; 131662306a36Sopenharmony_ci 131762306a36Sopenharmony_ci if (!xas->xa_node) { 131862306a36Sopenharmony_ci xas->xa_index = 1; 131962306a36Sopenharmony_ci goto out; 132062306a36Sopenharmony_ci } else if (xas_top(xas->xa_node)) { 132162306a36Sopenharmony_ci advance = false; 132262306a36Sopenharmony_ci entry = xa_head(xas->xa); 132362306a36Sopenharmony_ci xas->xa_node = NULL; 132462306a36Sopenharmony_ci if (xas->xa_index > max_index(entry)) 132562306a36Sopenharmony_ci goto out; 132662306a36Sopenharmony_ci if (!xa_is_node(entry)) { 132762306a36Sopenharmony_ci if (xa_marked(xas->xa, mark)) 132862306a36Sopenharmony_ci return entry; 132962306a36Sopenharmony_ci xas->xa_index = 1; 133062306a36Sopenharmony_ci goto out; 133162306a36Sopenharmony_ci } 133262306a36Sopenharmony_ci xas->xa_node = xa_to_node(entry); 133362306a36Sopenharmony_ci xas->xa_offset = xas->xa_index >> xas->xa_node->shift; 133462306a36Sopenharmony_ci } 133562306a36Sopenharmony_ci 133662306a36Sopenharmony_ci while (xas->xa_index <= max) { 133762306a36Sopenharmony_ci if (unlikely(xas->xa_offset == XA_CHUNK_SIZE)) { 133862306a36Sopenharmony_ci xas->xa_offset = xas->xa_node->offset + 1; 133962306a36Sopenharmony_ci xas->xa_node = xa_parent(xas->xa, xas->xa_node); 134062306a36Sopenharmony_ci if (!xas->xa_node) 134162306a36Sopenharmony_ci break; 134262306a36Sopenharmony_ci advance = false; 134362306a36Sopenharmony_ci continue; 134462306a36Sopenharmony_ci } 134562306a36Sopenharmony_ci 134662306a36Sopenharmony_ci if (!advance) { 134762306a36Sopenharmony_ci entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset); 134862306a36Sopenharmony_ci if (xa_is_sibling(entry)) { 134962306a36Sopenharmony_ci xas->xa_offset = xa_to_sibling(entry); 135062306a36Sopenharmony_ci xas_move_index(xas, xas->xa_offset); 135162306a36Sopenharmony_ci } 135262306a36Sopenharmony_ci } 135362306a36Sopenharmony_ci 135462306a36Sopenharmony_ci offset = xas_find_chunk(xas, advance, mark); 135562306a36Sopenharmony_ci if (offset > xas->xa_offset) { 135662306a36Sopenharmony_ci advance = false; 135762306a36Sopenharmony_ci xas_move_index(xas, offset); 135862306a36Sopenharmony_ci /* Mind the wrap */ 135962306a36Sopenharmony_ci if ((xas->xa_index - 1) >= max) 136062306a36Sopenharmony_ci goto max; 136162306a36Sopenharmony_ci xas->xa_offset = offset; 136262306a36Sopenharmony_ci if (offset == XA_CHUNK_SIZE) 136362306a36Sopenharmony_ci continue; 136462306a36Sopenharmony_ci } 136562306a36Sopenharmony_ci 136662306a36Sopenharmony_ci entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset); 136762306a36Sopenharmony_ci if (!entry && !(xa_track_free(xas->xa) && mark == XA_FREE_MARK)) 136862306a36Sopenharmony_ci continue; 136962306a36Sopenharmony_ci if (!xa_is_node(entry)) 137062306a36Sopenharmony_ci return entry; 137162306a36Sopenharmony_ci xas->xa_node = xa_to_node(entry); 137262306a36Sopenharmony_ci xas_set_offset(xas); 137362306a36Sopenharmony_ci } 137462306a36Sopenharmony_ci 137562306a36Sopenharmony_ciout: 137662306a36Sopenharmony_ci if (xas->xa_index > max) 137762306a36Sopenharmony_ci goto max; 137862306a36Sopenharmony_ci return set_bounds(xas); 137962306a36Sopenharmony_cimax: 138062306a36Sopenharmony_ci xas->xa_node = XAS_RESTART; 138162306a36Sopenharmony_ci return NULL; 138262306a36Sopenharmony_ci} 138362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(xas_find_marked); 138462306a36Sopenharmony_ci 138562306a36Sopenharmony_ci/** 138662306a36Sopenharmony_ci * xas_find_conflict() - Find the next present entry in a range. 138762306a36Sopenharmony_ci * @xas: XArray operation state. 138862306a36Sopenharmony_ci * 138962306a36Sopenharmony_ci * The @xas describes both a range and a position within that range. 139062306a36Sopenharmony_ci * 139162306a36Sopenharmony_ci * Context: Any context. Expects xa_lock to be held. 139262306a36Sopenharmony_ci * Return: The next entry in the range covered by @xas or %NULL. 139362306a36Sopenharmony_ci */ 139462306a36Sopenharmony_civoid *xas_find_conflict(struct xa_state *xas) 139562306a36Sopenharmony_ci{ 139662306a36Sopenharmony_ci void *curr; 139762306a36Sopenharmony_ci 139862306a36Sopenharmony_ci if (xas_error(xas)) 139962306a36Sopenharmony_ci return NULL; 140062306a36Sopenharmony_ci 140162306a36Sopenharmony_ci if (!xas->xa_node) 140262306a36Sopenharmony_ci return NULL; 140362306a36Sopenharmony_ci 140462306a36Sopenharmony_ci if (xas_top(xas->xa_node)) { 140562306a36Sopenharmony_ci curr = xas_start(xas); 140662306a36Sopenharmony_ci if (!curr) 140762306a36Sopenharmony_ci return NULL; 140862306a36Sopenharmony_ci while (xa_is_node(curr)) { 140962306a36Sopenharmony_ci struct xa_node *node = xa_to_node(curr); 141062306a36Sopenharmony_ci curr = xas_descend(xas, node); 141162306a36Sopenharmony_ci } 141262306a36Sopenharmony_ci if (curr) 141362306a36Sopenharmony_ci return curr; 141462306a36Sopenharmony_ci } 141562306a36Sopenharmony_ci 141662306a36Sopenharmony_ci if (xas->xa_node->shift > xas->xa_shift) 141762306a36Sopenharmony_ci return NULL; 141862306a36Sopenharmony_ci 141962306a36Sopenharmony_ci for (;;) { 142062306a36Sopenharmony_ci if (xas->xa_node->shift == xas->xa_shift) { 142162306a36Sopenharmony_ci if ((xas->xa_offset & xas->xa_sibs) == xas->xa_sibs) 142262306a36Sopenharmony_ci break; 142362306a36Sopenharmony_ci } else if (xas->xa_offset == XA_CHUNK_MASK) { 142462306a36Sopenharmony_ci xas->xa_offset = xas->xa_node->offset; 142562306a36Sopenharmony_ci xas->xa_node = xa_parent_locked(xas->xa, xas->xa_node); 142662306a36Sopenharmony_ci if (!xas->xa_node) 142762306a36Sopenharmony_ci break; 142862306a36Sopenharmony_ci continue; 142962306a36Sopenharmony_ci } 143062306a36Sopenharmony_ci curr = xa_entry_locked(xas->xa, xas->xa_node, ++xas->xa_offset); 143162306a36Sopenharmony_ci if (xa_is_sibling(curr)) 143262306a36Sopenharmony_ci continue; 143362306a36Sopenharmony_ci while (xa_is_node(curr)) { 143462306a36Sopenharmony_ci xas->xa_node = xa_to_node(curr); 143562306a36Sopenharmony_ci xas->xa_offset = 0; 143662306a36Sopenharmony_ci curr = xa_entry_locked(xas->xa, xas->xa_node, 0); 143762306a36Sopenharmony_ci } 143862306a36Sopenharmony_ci if (curr) 143962306a36Sopenharmony_ci return curr; 144062306a36Sopenharmony_ci } 144162306a36Sopenharmony_ci xas->xa_offset -= xas->xa_sibs; 144262306a36Sopenharmony_ci return NULL; 144362306a36Sopenharmony_ci} 144462306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(xas_find_conflict); 144562306a36Sopenharmony_ci 144662306a36Sopenharmony_ci/** 144762306a36Sopenharmony_ci * xa_load() - Load an entry from an XArray. 144862306a36Sopenharmony_ci * @xa: XArray. 144962306a36Sopenharmony_ci * @index: index into array. 145062306a36Sopenharmony_ci * 145162306a36Sopenharmony_ci * Context: Any context. Takes and releases the RCU lock. 145262306a36Sopenharmony_ci * Return: The entry at @index in @xa. 145362306a36Sopenharmony_ci */ 145462306a36Sopenharmony_civoid *xa_load(struct xarray *xa, unsigned long index) 145562306a36Sopenharmony_ci{ 145662306a36Sopenharmony_ci XA_STATE(xas, xa, index); 145762306a36Sopenharmony_ci void *entry; 145862306a36Sopenharmony_ci 145962306a36Sopenharmony_ci rcu_read_lock(); 146062306a36Sopenharmony_ci do { 146162306a36Sopenharmony_ci entry = xas_load(&xas); 146262306a36Sopenharmony_ci if (xa_is_zero(entry)) 146362306a36Sopenharmony_ci entry = NULL; 146462306a36Sopenharmony_ci } while (xas_retry(&xas, entry)); 146562306a36Sopenharmony_ci rcu_read_unlock(); 146662306a36Sopenharmony_ci 146762306a36Sopenharmony_ci return entry; 146862306a36Sopenharmony_ci} 146962306a36Sopenharmony_ciEXPORT_SYMBOL(xa_load); 147062306a36Sopenharmony_ci 147162306a36Sopenharmony_cistatic void *xas_result(struct xa_state *xas, void *curr) 147262306a36Sopenharmony_ci{ 147362306a36Sopenharmony_ci if (xa_is_zero(curr)) 147462306a36Sopenharmony_ci return NULL; 147562306a36Sopenharmony_ci if (xas_error(xas)) 147662306a36Sopenharmony_ci curr = xas->xa_node; 147762306a36Sopenharmony_ci return curr; 147862306a36Sopenharmony_ci} 147962306a36Sopenharmony_ci 148062306a36Sopenharmony_ci/** 148162306a36Sopenharmony_ci * __xa_erase() - Erase this entry from the XArray while locked. 148262306a36Sopenharmony_ci * @xa: XArray. 148362306a36Sopenharmony_ci * @index: Index into array. 148462306a36Sopenharmony_ci * 148562306a36Sopenharmony_ci * After this function returns, loading from @index will return %NULL. 148662306a36Sopenharmony_ci * If the index is part of a multi-index entry, all indices will be erased 148762306a36Sopenharmony_ci * and none of the entries will be part of a multi-index entry. 148862306a36Sopenharmony_ci * 148962306a36Sopenharmony_ci * Context: Any context. Expects xa_lock to be held on entry. 149062306a36Sopenharmony_ci * Return: The entry which used to be at this index. 149162306a36Sopenharmony_ci */ 149262306a36Sopenharmony_civoid *__xa_erase(struct xarray *xa, unsigned long index) 149362306a36Sopenharmony_ci{ 149462306a36Sopenharmony_ci XA_STATE(xas, xa, index); 149562306a36Sopenharmony_ci return xas_result(&xas, xas_store(&xas, NULL)); 149662306a36Sopenharmony_ci} 149762306a36Sopenharmony_ciEXPORT_SYMBOL(__xa_erase); 149862306a36Sopenharmony_ci 149962306a36Sopenharmony_ci/** 150062306a36Sopenharmony_ci * xa_erase() - Erase this entry from the XArray. 150162306a36Sopenharmony_ci * @xa: XArray. 150262306a36Sopenharmony_ci * @index: Index of entry. 150362306a36Sopenharmony_ci * 150462306a36Sopenharmony_ci * After this function returns, loading from @index will return %NULL. 150562306a36Sopenharmony_ci * If the index is part of a multi-index entry, all indices will be erased 150662306a36Sopenharmony_ci * and none of the entries will be part of a multi-index entry. 150762306a36Sopenharmony_ci * 150862306a36Sopenharmony_ci * Context: Any context. Takes and releases the xa_lock. 150962306a36Sopenharmony_ci * Return: The entry which used to be at this index. 151062306a36Sopenharmony_ci */ 151162306a36Sopenharmony_civoid *xa_erase(struct xarray *xa, unsigned long index) 151262306a36Sopenharmony_ci{ 151362306a36Sopenharmony_ci void *entry; 151462306a36Sopenharmony_ci 151562306a36Sopenharmony_ci xa_lock(xa); 151662306a36Sopenharmony_ci entry = __xa_erase(xa, index); 151762306a36Sopenharmony_ci xa_unlock(xa); 151862306a36Sopenharmony_ci 151962306a36Sopenharmony_ci return entry; 152062306a36Sopenharmony_ci} 152162306a36Sopenharmony_ciEXPORT_SYMBOL(xa_erase); 152262306a36Sopenharmony_ci 152362306a36Sopenharmony_ci/** 152462306a36Sopenharmony_ci * __xa_store() - Store this entry in the XArray. 152562306a36Sopenharmony_ci * @xa: XArray. 152662306a36Sopenharmony_ci * @index: Index into array. 152762306a36Sopenharmony_ci * @entry: New entry. 152862306a36Sopenharmony_ci * @gfp: Memory allocation flags. 152962306a36Sopenharmony_ci * 153062306a36Sopenharmony_ci * You must already be holding the xa_lock when calling this function. 153162306a36Sopenharmony_ci * It will drop the lock if needed to allocate memory, and then reacquire 153262306a36Sopenharmony_ci * it afterwards. 153362306a36Sopenharmony_ci * 153462306a36Sopenharmony_ci * Context: Any context. Expects xa_lock to be held on entry. May 153562306a36Sopenharmony_ci * release and reacquire xa_lock if @gfp flags permit. 153662306a36Sopenharmony_ci * Return: The old entry at this index or xa_err() if an error happened. 153762306a36Sopenharmony_ci */ 153862306a36Sopenharmony_civoid *__xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp) 153962306a36Sopenharmony_ci{ 154062306a36Sopenharmony_ci XA_STATE(xas, xa, index); 154162306a36Sopenharmony_ci void *curr; 154262306a36Sopenharmony_ci 154362306a36Sopenharmony_ci if (WARN_ON_ONCE(xa_is_advanced(entry))) 154462306a36Sopenharmony_ci return XA_ERROR(-EINVAL); 154562306a36Sopenharmony_ci if (xa_track_free(xa) && !entry) 154662306a36Sopenharmony_ci entry = XA_ZERO_ENTRY; 154762306a36Sopenharmony_ci 154862306a36Sopenharmony_ci do { 154962306a36Sopenharmony_ci curr = xas_store(&xas, entry); 155062306a36Sopenharmony_ci if (xa_track_free(xa)) 155162306a36Sopenharmony_ci xas_clear_mark(&xas, XA_FREE_MARK); 155262306a36Sopenharmony_ci } while (__xas_nomem(&xas, gfp)); 155362306a36Sopenharmony_ci 155462306a36Sopenharmony_ci return xas_result(&xas, curr); 155562306a36Sopenharmony_ci} 155662306a36Sopenharmony_ciEXPORT_SYMBOL(__xa_store); 155762306a36Sopenharmony_ci 155862306a36Sopenharmony_ci/** 155962306a36Sopenharmony_ci * xa_store() - Store this entry in the XArray. 156062306a36Sopenharmony_ci * @xa: XArray. 156162306a36Sopenharmony_ci * @index: Index into array. 156262306a36Sopenharmony_ci * @entry: New entry. 156362306a36Sopenharmony_ci * @gfp: Memory allocation flags. 156462306a36Sopenharmony_ci * 156562306a36Sopenharmony_ci * After this function returns, loads from this index will return @entry. 156662306a36Sopenharmony_ci * Storing into an existing multi-index entry updates the entry of every index. 156762306a36Sopenharmony_ci * The marks associated with @index are unaffected unless @entry is %NULL. 156862306a36Sopenharmony_ci * 156962306a36Sopenharmony_ci * Context: Any context. Takes and releases the xa_lock. 157062306a36Sopenharmony_ci * May sleep if the @gfp flags permit. 157162306a36Sopenharmony_ci * Return: The old entry at this index on success, xa_err(-EINVAL) if @entry 157262306a36Sopenharmony_ci * cannot be stored in an XArray, or xa_err(-ENOMEM) if memory allocation 157362306a36Sopenharmony_ci * failed. 157462306a36Sopenharmony_ci */ 157562306a36Sopenharmony_civoid *xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp) 157662306a36Sopenharmony_ci{ 157762306a36Sopenharmony_ci void *curr; 157862306a36Sopenharmony_ci 157962306a36Sopenharmony_ci xa_lock(xa); 158062306a36Sopenharmony_ci curr = __xa_store(xa, index, entry, gfp); 158162306a36Sopenharmony_ci xa_unlock(xa); 158262306a36Sopenharmony_ci 158362306a36Sopenharmony_ci return curr; 158462306a36Sopenharmony_ci} 158562306a36Sopenharmony_ciEXPORT_SYMBOL(xa_store); 158662306a36Sopenharmony_ci 158762306a36Sopenharmony_ci/** 158862306a36Sopenharmony_ci * __xa_cmpxchg() - Store this entry in the XArray. 158962306a36Sopenharmony_ci * @xa: XArray. 159062306a36Sopenharmony_ci * @index: Index into array. 159162306a36Sopenharmony_ci * @old: Old value to test against. 159262306a36Sopenharmony_ci * @entry: New entry. 159362306a36Sopenharmony_ci * @gfp: Memory allocation flags. 159462306a36Sopenharmony_ci * 159562306a36Sopenharmony_ci * You must already be holding the xa_lock when calling this function. 159662306a36Sopenharmony_ci * It will drop the lock if needed to allocate memory, and then reacquire 159762306a36Sopenharmony_ci * it afterwards. 159862306a36Sopenharmony_ci * 159962306a36Sopenharmony_ci * Context: Any context. Expects xa_lock to be held on entry. May 160062306a36Sopenharmony_ci * release and reacquire xa_lock if @gfp flags permit. 160162306a36Sopenharmony_ci * Return: The old entry at this index or xa_err() if an error happened. 160262306a36Sopenharmony_ci */ 160362306a36Sopenharmony_civoid *__xa_cmpxchg(struct xarray *xa, unsigned long index, 160462306a36Sopenharmony_ci void *old, void *entry, gfp_t gfp) 160562306a36Sopenharmony_ci{ 160662306a36Sopenharmony_ci XA_STATE(xas, xa, index); 160762306a36Sopenharmony_ci void *curr; 160862306a36Sopenharmony_ci 160962306a36Sopenharmony_ci if (WARN_ON_ONCE(xa_is_advanced(entry))) 161062306a36Sopenharmony_ci return XA_ERROR(-EINVAL); 161162306a36Sopenharmony_ci 161262306a36Sopenharmony_ci do { 161362306a36Sopenharmony_ci curr = xas_load(&xas); 161462306a36Sopenharmony_ci if (curr == old) { 161562306a36Sopenharmony_ci xas_store(&xas, entry); 161662306a36Sopenharmony_ci if (xa_track_free(xa) && entry && !curr) 161762306a36Sopenharmony_ci xas_clear_mark(&xas, XA_FREE_MARK); 161862306a36Sopenharmony_ci } 161962306a36Sopenharmony_ci } while (__xas_nomem(&xas, gfp)); 162062306a36Sopenharmony_ci 162162306a36Sopenharmony_ci return xas_result(&xas, curr); 162262306a36Sopenharmony_ci} 162362306a36Sopenharmony_ciEXPORT_SYMBOL(__xa_cmpxchg); 162462306a36Sopenharmony_ci 162562306a36Sopenharmony_ci/** 162662306a36Sopenharmony_ci * __xa_insert() - Store this entry in the XArray if no entry is present. 162762306a36Sopenharmony_ci * @xa: XArray. 162862306a36Sopenharmony_ci * @index: Index into array. 162962306a36Sopenharmony_ci * @entry: New entry. 163062306a36Sopenharmony_ci * @gfp: Memory allocation flags. 163162306a36Sopenharmony_ci * 163262306a36Sopenharmony_ci * Inserting a NULL entry will store a reserved entry (like xa_reserve()) 163362306a36Sopenharmony_ci * if no entry is present. Inserting will fail if a reserved entry is 163462306a36Sopenharmony_ci * present, even though loading from this index will return NULL. 163562306a36Sopenharmony_ci * 163662306a36Sopenharmony_ci * Context: Any context. Expects xa_lock to be held on entry. May 163762306a36Sopenharmony_ci * release and reacquire xa_lock if @gfp flags permit. 163862306a36Sopenharmony_ci * Return: 0 if the store succeeded. -EBUSY if another entry was present. 163962306a36Sopenharmony_ci * -ENOMEM if memory could not be allocated. 164062306a36Sopenharmony_ci */ 164162306a36Sopenharmony_ciint __xa_insert(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp) 164262306a36Sopenharmony_ci{ 164362306a36Sopenharmony_ci XA_STATE(xas, xa, index); 164462306a36Sopenharmony_ci void *curr; 164562306a36Sopenharmony_ci 164662306a36Sopenharmony_ci if (WARN_ON_ONCE(xa_is_advanced(entry))) 164762306a36Sopenharmony_ci return -EINVAL; 164862306a36Sopenharmony_ci if (!entry) 164962306a36Sopenharmony_ci entry = XA_ZERO_ENTRY; 165062306a36Sopenharmony_ci 165162306a36Sopenharmony_ci do { 165262306a36Sopenharmony_ci curr = xas_load(&xas); 165362306a36Sopenharmony_ci if (!curr) { 165462306a36Sopenharmony_ci xas_store(&xas, entry); 165562306a36Sopenharmony_ci if (xa_track_free(xa)) 165662306a36Sopenharmony_ci xas_clear_mark(&xas, XA_FREE_MARK); 165762306a36Sopenharmony_ci } else { 165862306a36Sopenharmony_ci xas_set_err(&xas, -EBUSY); 165962306a36Sopenharmony_ci } 166062306a36Sopenharmony_ci } while (__xas_nomem(&xas, gfp)); 166162306a36Sopenharmony_ci 166262306a36Sopenharmony_ci return xas_error(&xas); 166362306a36Sopenharmony_ci} 166462306a36Sopenharmony_ciEXPORT_SYMBOL(__xa_insert); 166562306a36Sopenharmony_ci 166662306a36Sopenharmony_ci#ifdef CONFIG_XARRAY_MULTI 166762306a36Sopenharmony_cistatic void xas_set_range(struct xa_state *xas, unsigned long first, 166862306a36Sopenharmony_ci unsigned long last) 166962306a36Sopenharmony_ci{ 167062306a36Sopenharmony_ci unsigned int shift = 0; 167162306a36Sopenharmony_ci unsigned long sibs = last - first; 167262306a36Sopenharmony_ci unsigned int offset = XA_CHUNK_MASK; 167362306a36Sopenharmony_ci 167462306a36Sopenharmony_ci xas_set(xas, first); 167562306a36Sopenharmony_ci 167662306a36Sopenharmony_ci while ((first & XA_CHUNK_MASK) == 0) { 167762306a36Sopenharmony_ci if (sibs < XA_CHUNK_MASK) 167862306a36Sopenharmony_ci break; 167962306a36Sopenharmony_ci if ((sibs == XA_CHUNK_MASK) && (offset < XA_CHUNK_MASK)) 168062306a36Sopenharmony_ci break; 168162306a36Sopenharmony_ci shift += XA_CHUNK_SHIFT; 168262306a36Sopenharmony_ci if (offset == XA_CHUNK_MASK) 168362306a36Sopenharmony_ci offset = sibs & XA_CHUNK_MASK; 168462306a36Sopenharmony_ci sibs >>= XA_CHUNK_SHIFT; 168562306a36Sopenharmony_ci first >>= XA_CHUNK_SHIFT; 168662306a36Sopenharmony_ci } 168762306a36Sopenharmony_ci 168862306a36Sopenharmony_ci offset = first & XA_CHUNK_MASK; 168962306a36Sopenharmony_ci if (offset + sibs > XA_CHUNK_MASK) 169062306a36Sopenharmony_ci sibs = XA_CHUNK_MASK - offset; 169162306a36Sopenharmony_ci if ((((first + sibs + 1) << shift) - 1) > last) 169262306a36Sopenharmony_ci sibs -= 1; 169362306a36Sopenharmony_ci 169462306a36Sopenharmony_ci xas->xa_shift = shift; 169562306a36Sopenharmony_ci xas->xa_sibs = sibs; 169662306a36Sopenharmony_ci} 169762306a36Sopenharmony_ci 169862306a36Sopenharmony_ci/** 169962306a36Sopenharmony_ci * xa_store_range() - Store this entry at a range of indices in the XArray. 170062306a36Sopenharmony_ci * @xa: XArray. 170162306a36Sopenharmony_ci * @first: First index to affect. 170262306a36Sopenharmony_ci * @last: Last index to affect. 170362306a36Sopenharmony_ci * @entry: New entry. 170462306a36Sopenharmony_ci * @gfp: Memory allocation flags. 170562306a36Sopenharmony_ci * 170662306a36Sopenharmony_ci * After this function returns, loads from any index between @first and @last, 170762306a36Sopenharmony_ci * inclusive will return @entry. 170862306a36Sopenharmony_ci * Storing into an existing multi-index entry updates the entry of every index. 170962306a36Sopenharmony_ci * The marks associated with @index are unaffected unless @entry is %NULL. 171062306a36Sopenharmony_ci * 171162306a36Sopenharmony_ci * Context: Process context. Takes and releases the xa_lock. May sleep 171262306a36Sopenharmony_ci * if the @gfp flags permit. 171362306a36Sopenharmony_ci * Return: %NULL on success, xa_err(-EINVAL) if @entry cannot be stored in 171462306a36Sopenharmony_ci * an XArray, or xa_err(-ENOMEM) if memory allocation failed. 171562306a36Sopenharmony_ci */ 171662306a36Sopenharmony_civoid *xa_store_range(struct xarray *xa, unsigned long first, 171762306a36Sopenharmony_ci unsigned long last, void *entry, gfp_t gfp) 171862306a36Sopenharmony_ci{ 171962306a36Sopenharmony_ci XA_STATE(xas, xa, 0); 172062306a36Sopenharmony_ci 172162306a36Sopenharmony_ci if (WARN_ON_ONCE(xa_is_internal(entry))) 172262306a36Sopenharmony_ci return XA_ERROR(-EINVAL); 172362306a36Sopenharmony_ci if (last < first) 172462306a36Sopenharmony_ci return XA_ERROR(-EINVAL); 172562306a36Sopenharmony_ci 172662306a36Sopenharmony_ci do { 172762306a36Sopenharmony_ci xas_lock(&xas); 172862306a36Sopenharmony_ci if (entry) { 172962306a36Sopenharmony_ci unsigned int order = BITS_PER_LONG; 173062306a36Sopenharmony_ci if (last + 1) 173162306a36Sopenharmony_ci order = __ffs(last + 1); 173262306a36Sopenharmony_ci xas_set_order(&xas, last, order); 173362306a36Sopenharmony_ci xas_create(&xas, true); 173462306a36Sopenharmony_ci if (xas_error(&xas)) 173562306a36Sopenharmony_ci goto unlock; 173662306a36Sopenharmony_ci } 173762306a36Sopenharmony_ci do { 173862306a36Sopenharmony_ci xas_set_range(&xas, first, last); 173962306a36Sopenharmony_ci xas_store(&xas, entry); 174062306a36Sopenharmony_ci if (xas_error(&xas)) 174162306a36Sopenharmony_ci goto unlock; 174262306a36Sopenharmony_ci first += xas_size(&xas); 174362306a36Sopenharmony_ci } while (first <= last); 174462306a36Sopenharmony_ciunlock: 174562306a36Sopenharmony_ci xas_unlock(&xas); 174662306a36Sopenharmony_ci } while (xas_nomem(&xas, gfp)); 174762306a36Sopenharmony_ci 174862306a36Sopenharmony_ci return xas_result(&xas, NULL); 174962306a36Sopenharmony_ci} 175062306a36Sopenharmony_ciEXPORT_SYMBOL(xa_store_range); 175162306a36Sopenharmony_ci 175262306a36Sopenharmony_ci/** 175362306a36Sopenharmony_ci * xa_get_order() - Get the order of an entry. 175462306a36Sopenharmony_ci * @xa: XArray. 175562306a36Sopenharmony_ci * @index: Index of the entry. 175662306a36Sopenharmony_ci * 175762306a36Sopenharmony_ci * Return: A number between 0 and 63 indicating the order of the entry. 175862306a36Sopenharmony_ci */ 175962306a36Sopenharmony_ciint xa_get_order(struct xarray *xa, unsigned long index) 176062306a36Sopenharmony_ci{ 176162306a36Sopenharmony_ci XA_STATE(xas, xa, index); 176262306a36Sopenharmony_ci void *entry; 176362306a36Sopenharmony_ci int order = 0; 176462306a36Sopenharmony_ci 176562306a36Sopenharmony_ci rcu_read_lock(); 176662306a36Sopenharmony_ci entry = xas_load(&xas); 176762306a36Sopenharmony_ci 176862306a36Sopenharmony_ci if (!entry) 176962306a36Sopenharmony_ci goto unlock; 177062306a36Sopenharmony_ci 177162306a36Sopenharmony_ci if (!xas.xa_node) 177262306a36Sopenharmony_ci goto unlock; 177362306a36Sopenharmony_ci 177462306a36Sopenharmony_ci for (;;) { 177562306a36Sopenharmony_ci unsigned int slot = xas.xa_offset + (1 << order); 177662306a36Sopenharmony_ci 177762306a36Sopenharmony_ci if (slot >= XA_CHUNK_SIZE) 177862306a36Sopenharmony_ci break; 177962306a36Sopenharmony_ci if (!xa_is_sibling(xas.xa_node->slots[slot])) 178062306a36Sopenharmony_ci break; 178162306a36Sopenharmony_ci order++; 178262306a36Sopenharmony_ci } 178362306a36Sopenharmony_ci 178462306a36Sopenharmony_ci order += xas.xa_node->shift; 178562306a36Sopenharmony_ciunlock: 178662306a36Sopenharmony_ci rcu_read_unlock(); 178762306a36Sopenharmony_ci 178862306a36Sopenharmony_ci return order; 178962306a36Sopenharmony_ci} 179062306a36Sopenharmony_ciEXPORT_SYMBOL(xa_get_order); 179162306a36Sopenharmony_ci#endif /* CONFIG_XARRAY_MULTI */ 179262306a36Sopenharmony_ci 179362306a36Sopenharmony_ci/** 179462306a36Sopenharmony_ci * __xa_alloc() - Find somewhere to store this entry in the XArray. 179562306a36Sopenharmony_ci * @xa: XArray. 179662306a36Sopenharmony_ci * @id: Pointer to ID. 179762306a36Sopenharmony_ci * @limit: Range for allocated ID. 179862306a36Sopenharmony_ci * @entry: New entry. 179962306a36Sopenharmony_ci * @gfp: Memory allocation flags. 180062306a36Sopenharmony_ci * 180162306a36Sopenharmony_ci * Finds an empty entry in @xa between @limit.min and @limit.max, 180262306a36Sopenharmony_ci * stores the index into the @id pointer, then stores the entry at 180362306a36Sopenharmony_ci * that index. A concurrent lookup will not see an uninitialised @id. 180462306a36Sopenharmony_ci * 180562306a36Sopenharmony_ci * Must only be operated on an xarray initialized with flag XA_FLAGS_ALLOC set 180662306a36Sopenharmony_ci * in xa_init_flags(). 180762306a36Sopenharmony_ci * 180862306a36Sopenharmony_ci * Context: Any context. Expects xa_lock to be held on entry. May 180962306a36Sopenharmony_ci * release and reacquire xa_lock if @gfp flags permit. 181062306a36Sopenharmony_ci * Return: 0 on success, -ENOMEM if memory could not be allocated or 181162306a36Sopenharmony_ci * -EBUSY if there are no free entries in @limit. 181262306a36Sopenharmony_ci */ 181362306a36Sopenharmony_ciint __xa_alloc(struct xarray *xa, u32 *id, void *entry, 181462306a36Sopenharmony_ci struct xa_limit limit, gfp_t gfp) 181562306a36Sopenharmony_ci{ 181662306a36Sopenharmony_ci XA_STATE(xas, xa, 0); 181762306a36Sopenharmony_ci 181862306a36Sopenharmony_ci if (WARN_ON_ONCE(xa_is_advanced(entry))) 181962306a36Sopenharmony_ci return -EINVAL; 182062306a36Sopenharmony_ci if (WARN_ON_ONCE(!xa_track_free(xa))) 182162306a36Sopenharmony_ci return -EINVAL; 182262306a36Sopenharmony_ci 182362306a36Sopenharmony_ci if (!entry) 182462306a36Sopenharmony_ci entry = XA_ZERO_ENTRY; 182562306a36Sopenharmony_ci 182662306a36Sopenharmony_ci do { 182762306a36Sopenharmony_ci xas.xa_index = limit.min; 182862306a36Sopenharmony_ci xas_find_marked(&xas, limit.max, XA_FREE_MARK); 182962306a36Sopenharmony_ci if (xas.xa_node == XAS_RESTART) 183062306a36Sopenharmony_ci xas_set_err(&xas, -EBUSY); 183162306a36Sopenharmony_ci else 183262306a36Sopenharmony_ci *id = xas.xa_index; 183362306a36Sopenharmony_ci xas_store(&xas, entry); 183462306a36Sopenharmony_ci xas_clear_mark(&xas, XA_FREE_MARK); 183562306a36Sopenharmony_ci } while (__xas_nomem(&xas, gfp)); 183662306a36Sopenharmony_ci 183762306a36Sopenharmony_ci return xas_error(&xas); 183862306a36Sopenharmony_ci} 183962306a36Sopenharmony_ciEXPORT_SYMBOL(__xa_alloc); 184062306a36Sopenharmony_ci 184162306a36Sopenharmony_ci/** 184262306a36Sopenharmony_ci * __xa_alloc_cyclic() - Find somewhere to store this entry in the XArray. 184362306a36Sopenharmony_ci * @xa: XArray. 184462306a36Sopenharmony_ci * @id: Pointer to ID. 184562306a36Sopenharmony_ci * @entry: New entry. 184662306a36Sopenharmony_ci * @limit: Range of allocated ID. 184762306a36Sopenharmony_ci * @next: Pointer to next ID to allocate. 184862306a36Sopenharmony_ci * @gfp: Memory allocation flags. 184962306a36Sopenharmony_ci * 185062306a36Sopenharmony_ci * Finds an empty entry in @xa between @limit.min and @limit.max, 185162306a36Sopenharmony_ci * stores the index into the @id pointer, then stores the entry at 185262306a36Sopenharmony_ci * that index. A concurrent lookup will not see an uninitialised @id. 185362306a36Sopenharmony_ci * The search for an empty entry will start at @next and will wrap 185462306a36Sopenharmony_ci * around if necessary. 185562306a36Sopenharmony_ci * 185662306a36Sopenharmony_ci * Must only be operated on an xarray initialized with flag XA_FLAGS_ALLOC set 185762306a36Sopenharmony_ci * in xa_init_flags(). 185862306a36Sopenharmony_ci * 185962306a36Sopenharmony_ci * Context: Any context. Expects xa_lock to be held on entry. May 186062306a36Sopenharmony_ci * release and reacquire xa_lock if @gfp flags permit. 186162306a36Sopenharmony_ci * Return: 0 if the allocation succeeded without wrapping. 1 if the 186262306a36Sopenharmony_ci * allocation succeeded after wrapping, -ENOMEM if memory could not be 186362306a36Sopenharmony_ci * allocated or -EBUSY if there are no free entries in @limit. 186462306a36Sopenharmony_ci */ 186562306a36Sopenharmony_ciint __xa_alloc_cyclic(struct xarray *xa, u32 *id, void *entry, 186662306a36Sopenharmony_ci struct xa_limit limit, u32 *next, gfp_t gfp) 186762306a36Sopenharmony_ci{ 186862306a36Sopenharmony_ci u32 min = limit.min; 186962306a36Sopenharmony_ci int ret; 187062306a36Sopenharmony_ci 187162306a36Sopenharmony_ci limit.min = max(min, *next); 187262306a36Sopenharmony_ci ret = __xa_alloc(xa, id, entry, limit, gfp); 187362306a36Sopenharmony_ci if ((xa->xa_flags & XA_FLAGS_ALLOC_WRAPPED) && ret == 0) { 187462306a36Sopenharmony_ci xa->xa_flags &= ~XA_FLAGS_ALLOC_WRAPPED; 187562306a36Sopenharmony_ci ret = 1; 187662306a36Sopenharmony_ci } 187762306a36Sopenharmony_ci 187862306a36Sopenharmony_ci if (ret < 0 && limit.min > min) { 187962306a36Sopenharmony_ci limit.min = min; 188062306a36Sopenharmony_ci ret = __xa_alloc(xa, id, entry, limit, gfp); 188162306a36Sopenharmony_ci if (ret == 0) 188262306a36Sopenharmony_ci ret = 1; 188362306a36Sopenharmony_ci } 188462306a36Sopenharmony_ci 188562306a36Sopenharmony_ci if (ret >= 0) { 188662306a36Sopenharmony_ci *next = *id + 1; 188762306a36Sopenharmony_ci if (*next == 0) 188862306a36Sopenharmony_ci xa->xa_flags |= XA_FLAGS_ALLOC_WRAPPED; 188962306a36Sopenharmony_ci } 189062306a36Sopenharmony_ci return ret; 189162306a36Sopenharmony_ci} 189262306a36Sopenharmony_ciEXPORT_SYMBOL(__xa_alloc_cyclic); 189362306a36Sopenharmony_ci 189462306a36Sopenharmony_ci/** 189562306a36Sopenharmony_ci * __xa_set_mark() - Set this mark on this entry while locked. 189662306a36Sopenharmony_ci * @xa: XArray. 189762306a36Sopenharmony_ci * @index: Index of entry. 189862306a36Sopenharmony_ci * @mark: Mark number. 189962306a36Sopenharmony_ci * 190062306a36Sopenharmony_ci * Attempting to set a mark on a %NULL entry does not succeed. 190162306a36Sopenharmony_ci * 190262306a36Sopenharmony_ci * Context: Any context. Expects xa_lock to be held on entry. 190362306a36Sopenharmony_ci */ 190462306a36Sopenharmony_civoid __xa_set_mark(struct xarray *xa, unsigned long index, xa_mark_t mark) 190562306a36Sopenharmony_ci{ 190662306a36Sopenharmony_ci XA_STATE(xas, xa, index); 190762306a36Sopenharmony_ci void *entry = xas_load(&xas); 190862306a36Sopenharmony_ci 190962306a36Sopenharmony_ci if (entry) 191062306a36Sopenharmony_ci xas_set_mark(&xas, mark); 191162306a36Sopenharmony_ci} 191262306a36Sopenharmony_ciEXPORT_SYMBOL(__xa_set_mark); 191362306a36Sopenharmony_ci 191462306a36Sopenharmony_ci/** 191562306a36Sopenharmony_ci * __xa_clear_mark() - Clear this mark on this entry while locked. 191662306a36Sopenharmony_ci * @xa: XArray. 191762306a36Sopenharmony_ci * @index: Index of entry. 191862306a36Sopenharmony_ci * @mark: Mark number. 191962306a36Sopenharmony_ci * 192062306a36Sopenharmony_ci * Context: Any context. Expects xa_lock to be held on entry. 192162306a36Sopenharmony_ci */ 192262306a36Sopenharmony_civoid __xa_clear_mark(struct xarray *xa, unsigned long index, xa_mark_t mark) 192362306a36Sopenharmony_ci{ 192462306a36Sopenharmony_ci XA_STATE(xas, xa, index); 192562306a36Sopenharmony_ci void *entry = xas_load(&xas); 192662306a36Sopenharmony_ci 192762306a36Sopenharmony_ci if (entry) 192862306a36Sopenharmony_ci xas_clear_mark(&xas, mark); 192962306a36Sopenharmony_ci} 193062306a36Sopenharmony_ciEXPORT_SYMBOL(__xa_clear_mark); 193162306a36Sopenharmony_ci 193262306a36Sopenharmony_ci/** 193362306a36Sopenharmony_ci * xa_get_mark() - Inquire whether this mark is set on this entry. 193462306a36Sopenharmony_ci * @xa: XArray. 193562306a36Sopenharmony_ci * @index: Index of entry. 193662306a36Sopenharmony_ci * @mark: Mark number. 193762306a36Sopenharmony_ci * 193862306a36Sopenharmony_ci * This function uses the RCU read lock, so the result may be out of date 193962306a36Sopenharmony_ci * by the time it returns. If you need the result to be stable, use a lock. 194062306a36Sopenharmony_ci * 194162306a36Sopenharmony_ci * Context: Any context. Takes and releases the RCU lock. 194262306a36Sopenharmony_ci * Return: True if the entry at @index has this mark set, false if it doesn't. 194362306a36Sopenharmony_ci */ 194462306a36Sopenharmony_cibool xa_get_mark(struct xarray *xa, unsigned long index, xa_mark_t mark) 194562306a36Sopenharmony_ci{ 194662306a36Sopenharmony_ci XA_STATE(xas, xa, index); 194762306a36Sopenharmony_ci void *entry; 194862306a36Sopenharmony_ci 194962306a36Sopenharmony_ci rcu_read_lock(); 195062306a36Sopenharmony_ci entry = xas_start(&xas); 195162306a36Sopenharmony_ci while (xas_get_mark(&xas, mark)) { 195262306a36Sopenharmony_ci if (!xa_is_node(entry)) 195362306a36Sopenharmony_ci goto found; 195462306a36Sopenharmony_ci entry = xas_descend(&xas, xa_to_node(entry)); 195562306a36Sopenharmony_ci } 195662306a36Sopenharmony_ci rcu_read_unlock(); 195762306a36Sopenharmony_ci return false; 195862306a36Sopenharmony_ci found: 195962306a36Sopenharmony_ci rcu_read_unlock(); 196062306a36Sopenharmony_ci return true; 196162306a36Sopenharmony_ci} 196262306a36Sopenharmony_ciEXPORT_SYMBOL(xa_get_mark); 196362306a36Sopenharmony_ci 196462306a36Sopenharmony_ci/** 196562306a36Sopenharmony_ci * xa_set_mark() - Set this mark on this entry. 196662306a36Sopenharmony_ci * @xa: XArray. 196762306a36Sopenharmony_ci * @index: Index of entry. 196862306a36Sopenharmony_ci * @mark: Mark number. 196962306a36Sopenharmony_ci * 197062306a36Sopenharmony_ci * Attempting to set a mark on a %NULL entry does not succeed. 197162306a36Sopenharmony_ci * 197262306a36Sopenharmony_ci * Context: Process context. Takes and releases the xa_lock. 197362306a36Sopenharmony_ci */ 197462306a36Sopenharmony_civoid xa_set_mark(struct xarray *xa, unsigned long index, xa_mark_t mark) 197562306a36Sopenharmony_ci{ 197662306a36Sopenharmony_ci xa_lock(xa); 197762306a36Sopenharmony_ci __xa_set_mark(xa, index, mark); 197862306a36Sopenharmony_ci xa_unlock(xa); 197962306a36Sopenharmony_ci} 198062306a36Sopenharmony_ciEXPORT_SYMBOL(xa_set_mark); 198162306a36Sopenharmony_ci 198262306a36Sopenharmony_ci/** 198362306a36Sopenharmony_ci * xa_clear_mark() - Clear this mark on this entry. 198462306a36Sopenharmony_ci * @xa: XArray. 198562306a36Sopenharmony_ci * @index: Index of entry. 198662306a36Sopenharmony_ci * @mark: Mark number. 198762306a36Sopenharmony_ci * 198862306a36Sopenharmony_ci * Clearing a mark always succeeds. 198962306a36Sopenharmony_ci * 199062306a36Sopenharmony_ci * Context: Process context. Takes and releases the xa_lock. 199162306a36Sopenharmony_ci */ 199262306a36Sopenharmony_civoid xa_clear_mark(struct xarray *xa, unsigned long index, xa_mark_t mark) 199362306a36Sopenharmony_ci{ 199462306a36Sopenharmony_ci xa_lock(xa); 199562306a36Sopenharmony_ci __xa_clear_mark(xa, index, mark); 199662306a36Sopenharmony_ci xa_unlock(xa); 199762306a36Sopenharmony_ci} 199862306a36Sopenharmony_ciEXPORT_SYMBOL(xa_clear_mark); 199962306a36Sopenharmony_ci 200062306a36Sopenharmony_ci/** 200162306a36Sopenharmony_ci * xa_find() - Search the XArray for an entry. 200262306a36Sopenharmony_ci * @xa: XArray. 200362306a36Sopenharmony_ci * @indexp: Pointer to an index. 200462306a36Sopenharmony_ci * @max: Maximum index to search to. 200562306a36Sopenharmony_ci * @filter: Selection criterion. 200662306a36Sopenharmony_ci * 200762306a36Sopenharmony_ci * Finds the entry in @xa which matches the @filter, and has the lowest 200862306a36Sopenharmony_ci * index that is at least @indexp and no more than @max. 200962306a36Sopenharmony_ci * If an entry is found, @indexp is updated to be the index of the entry. 201062306a36Sopenharmony_ci * This function is protected by the RCU read lock, so it may not find 201162306a36Sopenharmony_ci * entries which are being simultaneously added. It will not return an 201262306a36Sopenharmony_ci * %XA_RETRY_ENTRY; if you need to see retry entries, use xas_find(). 201362306a36Sopenharmony_ci * 201462306a36Sopenharmony_ci * Context: Any context. Takes and releases the RCU lock. 201562306a36Sopenharmony_ci * Return: The entry, if found, otherwise %NULL. 201662306a36Sopenharmony_ci */ 201762306a36Sopenharmony_civoid *xa_find(struct xarray *xa, unsigned long *indexp, 201862306a36Sopenharmony_ci unsigned long max, xa_mark_t filter) 201962306a36Sopenharmony_ci{ 202062306a36Sopenharmony_ci XA_STATE(xas, xa, *indexp); 202162306a36Sopenharmony_ci void *entry; 202262306a36Sopenharmony_ci 202362306a36Sopenharmony_ci rcu_read_lock(); 202462306a36Sopenharmony_ci do { 202562306a36Sopenharmony_ci if ((__force unsigned int)filter < XA_MAX_MARKS) 202662306a36Sopenharmony_ci entry = xas_find_marked(&xas, max, filter); 202762306a36Sopenharmony_ci else 202862306a36Sopenharmony_ci entry = xas_find(&xas, max); 202962306a36Sopenharmony_ci } while (xas_retry(&xas, entry)); 203062306a36Sopenharmony_ci rcu_read_unlock(); 203162306a36Sopenharmony_ci 203262306a36Sopenharmony_ci if (entry) 203362306a36Sopenharmony_ci *indexp = xas.xa_index; 203462306a36Sopenharmony_ci return entry; 203562306a36Sopenharmony_ci} 203662306a36Sopenharmony_ciEXPORT_SYMBOL(xa_find); 203762306a36Sopenharmony_ci 203862306a36Sopenharmony_cistatic bool xas_sibling(struct xa_state *xas) 203962306a36Sopenharmony_ci{ 204062306a36Sopenharmony_ci struct xa_node *node = xas->xa_node; 204162306a36Sopenharmony_ci unsigned long mask; 204262306a36Sopenharmony_ci 204362306a36Sopenharmony_ci if (!IS_ENABLED(CONFIG_XARRAY_MULTI) || !node) 204462306a36Sopenharmony_ci return false; 204562306a36Sopenharmony_ci mask = (XA_CHUNK_SIZE << node->shift) - 1; 204662306a36Sopenharmony_ci return (xas->xa_index & mask) > 204762306a36Sopenharmony_ci ((unsigned long)xas->xa_offset << node->shift); 204862306a36Sopenharmony_ci} 204962306a36Sopenharmony_ci 205062306a36Sopenharmony_ci/** 205162306a36Sopenharmony_ci * xa_find_after() - Search the XArray for a present entry. 205262306a36Sopenharmony_ci * @xa: XArray. 205362306a36Sopenharmony_ci * @indexp: Pointer to an index. 205462306a36Sopenharmony_ci * @max: Maximum index to search to. 205562306a36Sopenharmony_ci * @filter: Selection criterion. 205662306a36Sopenharmony_ci * 205762306a36Sopenharmony_ci * Finds the entry in @xa which matches the @filter and has the lowest 205862306a36Sopenharmony_ci * index that is above @indexp and no more than @max. 205962306a36Sopenharmony_ci * If an entry is found, @indexp is updated to be the index of the entry. 206062306a36Sopenharmony_ci * This function is protected by the RCU read lock, so it may miss entries 206162306a36Sopenharmony_ci * which are being simultaneously added. It will not return an 206262306a36Sopenharmony_ci * %XA_RETRY_ENTRY; if you need to see retry entries, use xas_find(). 206362306a36Sopenharmony_ci * 206462306a36Sopenharmony_ci * Context: Any context. Takes and releases the RCU lock. 206562306a36Sopenharmony_ci * Return: The pointer, if found, otherwise %NULL. 206662306a36Sopenharmony_ci */ 206762306a36Sopenharmony_civoid *xa_find_after(struct xarray *xa, unsigned long *indexp, 206862306a36Sopenharmony_ci unsigned long max, xa_mark_t filter) 206962306a36Sopenharmony_ci{ 207062306a36Sopenharmony_ci XA_STATE(xas, xa, *indexp + 1); 207162306a36Sopenharmony_ci void *entry; 207262306a36Sopenharmony_ci 207362306a36Sopenharmony_ci if (xas.xa_index == 0) 207462306a36Sopenharmony_ci return NULL; 207562306a36Sopenharmony_ci 207662306a36Sopenharmony_ci rcu_read_lock(); 207762306a36Sopenharmony_ci for (;;) { 207862306a36Sopenharmony_ci if ((__force unsigned int)filter < XA_MAX_MARKS) 207962306a36Sopenharmony_ci entry = xas_find_marked(&xas, max, filter); 208062306a36Sopenharmony_ci else 208162306a36Sopenharmony_ci entry = xas_find(&xas, max); 208262306a36Sopenharmony_ci 208362306a36Sopenharmony_ci if (xas_invalid(&xas)) 208462306a36Sopenharmony_ci break; 208562306a36Sopenharmony_ci if (xas_sibling(&xas)) 208662306a36Sopenharmony_ci continue; 208762306a36Sopenharmony_ci if (!xas_retry(&xas, entry)) 208862306a36Sopenharmony_ci break; 208962306a36Sopenharmony_ci } 209062306a36Sopenharmony_ci rcu_read_unlock(); 209162306a36Sopenharmony_ci 209262306a36Sopenharmony_ci if (entry) 209362306a36Sopenharmony_ci *indexp = xas.xa_index; 209462306a36Sopenharmony_ci return entry; 209562306a36Sopenharmony_ci} 209662306a36Sopenharmony_ciEXPORT_SYMBOL(xa_find_after); 209762306a36Sopenharmony_ci 209862306a36Sopenharmony_cistatic unsigned int xas_extract_present(struct xa_state *xas, void **dst, 209962306a36Sopenharmony_ci unsigned long max, unsigned int n) 210062306a36Sopenharmony_ci{ 210162306a36Sopenharmony_ci void *entry; 210262306a36Sopenharmony_ci unsigned int i = 0; 210362306a36Sopenharmony_ci 210462306a36Sopenharmony_ci rcu_read_lock(); 210562306a36Sopenharmony_ci xas_for_each(xas, entry, max) { 210662306a36Sopenharmony_ci if (xas_retry(xas, entry)) 210762306a36Sopenharmony_ci continue; 210862306a36Sopenharmony_ci dst[i++] = entry; 210962306a36Sopenharmony_ci if (i == n) 211062306a36Sopenharmony_ci break; 211162306a36Sopenharmony_ci } 211262306a36Sopenharmony_ci rcu_read_unlock(); 211362306a36Sopenharmony_ci 211462306a36Sopenharmony_ci return i; 211562306a36Sopenharmony_ci} 211662306a36Sopenharmony_ci 211762306a36Sopenharmony_cistatic unsigned int xas_extract_marked(struct xa_state *xas, void **dst, 211862306a36Sopenharmony_ci unsigned long max, unsigned int n, xa_mark_t mark) 211962306a36Sopenharmony_ci{ 212062306a36Sopenharmony_ci void *entry; 212162306a36Sopenharmony_ci unsigned int i = 0; 212262306a36Sopenharmony_ci 212362306a36Sopenharmony_ci rcu_read_lock(); 212462306a36Sopenharmony_ci xas_for_each_marked(xas, entry, max, mark) { 212562306a36Sopenharmony_ci if (xas_retry(xas, entry)) 212662306a36Sopenharmony_ci continue; 212762306a36Sopenharmony_ci dst[i++] = entry; 212862306a36Sopenharmony_ci if (i == n) 212962306a36Sopenharmony_ci break; 213062306a36Sopenharmony_ci } 213162306a36Sopenharmony_ci rcu_read_unlock(); 213262306a36Sopenharmony_ci 213362306a36Sopenharmony_ci return i; 213462306a36Sopenharmony_ci} 213562306a36Sopenharmony_ci 213662306a36Sopenharmony_ci/** 213762306a36Sopenharmony_ci * xa_extract() - Copy selected entries from the XArray into a normal array. 213862306a36Sopenharmony_ci * @xa: The source XArray to copy from. 213962306a36Sopenharmony_ci * @dst: The buffer to copy entries into. 214062306a36Sopenharmony_ci * @start: The first index in the XArray eligible to be selected. 214162306a36Sopenharmony_ci * @max: The last index in the XArray eligible to be selected. 214262306a36Sopenharmony_ci * @n: The maximum number of entries to copy. 214362306a36Sopenharmony_ci * @filter: Selection criterion. 214462306a36Sopenharmony_ci * 214562306a36Sopenharmony_ci * Copies up to @n entries that match @filter from the XArray. The 214662306a36Sopenharmony_ci * copied entries will have indices between @start and @max, inclusive. 214762306a36Sopenharmony_ci * 214862306a36Sopenharmony_ci * The @filter may be an XArray mark value, in which case entries which are 214962306a36Sopenharmony_ci * marked with that mark will be copied. It may also be %XA_PRESENT, in 215062306a36Sopenharmony_ci * which case all entries which are not %NULL will be copied. 215162306a36Sopenharmony_ci * 215262306a36Sopenharmony_ci * The entries returned may not represent a snapshot of the XArray at a 215362306a36Sopenharmony_ci * moment in time. For example, if another thread stores to index 5, then 215462306a36Sopenharmony_ci * index 10, calling xa_extract() may return the old contents of index 5 215562306a36Sopenharmony_ci * and the new contents of index 10. Indices not modified while this 215662306a36Sopenharmony_ci * function is running will not be skipped. 215762306a36Sopenharmony_ci * 215862306a36Sopenharmony_ci * If you need stronger guarantees, holding the xa_lock across calls to this 215962306a36Sopenharmony_ci * function will prevent concurrent modification. 216062306a36Sopenharmony_ci * 216162306a36Sopenharmony_ci * Context: Any context. Takes and releases the RCU lock. 216262306a36Sopenharmony_ci * Return: The number of entries copied. 216362306a36Sopenharmony_ci */ 216462306a36Sopenharmony_ciunsigned int xa_extract(struct xarray *xa, void **dst, unsigned long start, 216562306a36Sopenharmony_ci unsigned long max, unsigned int n, xa_mark_t filter) 216662306a36Sopenharmony_ci{ 216762306a36Sopenharmony_ci XA_STATE(xas, xa, start); 216862306a36Sopenharmony_ci 216962306a36Sopenharmony_ci if (!n) 217062306a36Sopenharmony_ci return 0; 217162306a36Sopenharmony_ci 217262306a36Sopenharmony_ci if ((__force unsigned int)filter < XA_MAX_MARKS) 217362306a36Sopenharmony_ci return xas_extract_marked(&xas, dst, max, n, filter); 217462306a36Sopenharmony_ci return xas_extract_present(&xas, dst, max, n); 217562306a36Sopenharmony_ci} 217662306a36Sopenharmony_ciEXPORT_SYMBOL(xa_extract); 217762306a36Sopenharmony_ci 217862306a36Sopenharmony_ci/** 217962306a36Sopenharmony_ci * xa_delete_node() - Private interface for workingset code. 218062306a36Sopenharmony_ci * @node: Node to be removed from the tree. 218162306a36Sopenharmony_ci * @update: Function to call to update ancestor nodes. 218262306a36Sopenharmony_ci * 218362306a36Sopenharmony_ci * Context: xa_lock must be held on entry and will not be released. 218462306a36Sopenharmony_ci */ 218562306a36Sopenharmony_civoid xa_delete_node(struct xa_node *node, xa_update_node_t update) 218662306a36Sopenharmony_ci{ 218762306a36Sopenharmony_ci struct xa_state xas = { 218862306a36Sopenharmony_ci .xa = node->array, 218962306a36Sopenharmony_ci .xa_index = (unsigned long)node->offset << 219062306a36Sopenharmony_ci (node->shift + XA_CHUNK_SHIFT), 219162306a36Sopenharmony_ci .xa_shift = node->shift + XA_CHUNK_SHIFT, 219262306a36Sopenharmony_ci .xa_offset = node->offset, 219362306a36Sopenharmony_ci .xa_node = xa_parent_locked(node->array, node), 219462306a36Sopenharmony_ci .xa_update = update, 219562306a36Sopenharmony_ci }; 219662306a36Sopenharmony_ci 219762306a36Sopenharmony_ci xas_store(&xas, NULL); 219862306a36Sopenharmony_ci} 219962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(xa_delete_node); /* For the benefit of the test suite */ 220062306a36Sopenharmony_ci 220162306a36Sopenharmony_ci/** 220262306a36Sopenharmony_ci * xa_destroy() - Free all internal data structures. 220362306a36Sopenharmony_ci * @xa: XArray. 220462306a36Sopenharmony_ci * 220562306a36Sopenharmony_ci * After calling this function, the XArray is empty and has freed all memory 220662306a36Sopenharmony_ci * allocated for its internal data structures. You are responsible for 220762306a36Sopenharmony_ci * freeing the objects referenced by the XArray. 220862306a36Sopenharmony_ci * 220962306a36Sopenharmony_ci * Context: Any context. Takes and releases the xa_lock, interrupt-safe. 221062306a36Sopenharmony_ci */ 221162306a36Sopenharmony_civoid xa_destroy(struct xarray *xa) 221262306a36Sopenharmony_ci{ 221362306a36Sopenharmony_ci XA_STATE(xas, xa, 0); 221462306a36Sopenharmony_ci unsigned long flags; 221562306a36Sopenharmony_ci void *entry; 221662306a36Sopenharmony_ci 221762306a36Sopenharmony_ci xas.xa_node = NULL; 221862306a36Sopenharmony_ci xas_lock_irqsave(&xas, flags); 221962306a36Sopenharmony_ci entry = xa_head_locked(xa); 222062306a36Sopenharmony_ci RCU_INIT_POINTER(xa->xa_head, NULL); 222162306a36Sopenharmony_ci xas_init_marks(&xas); 222262306a36Sopenharmony_ci if (xa_zero_busy(xa)) 222362306a36Sopenharmony_ci xa_mark_clear(xa, XA_FREE_MARK); 222462306a36Sopenharmony_ci /* lockdep checks we're still holding the lock in xas_free_nodes() */ 222562306a36Sopenharmony_ci if (xa_is_node(entry)) 222662306a36Sopenharmony_ci xas_free_nodes(&xas, xa_to_node(entry)); 222762306a36Sopenharmony_ci xas_unlock_irqrestore(&xas, flags); 222862306a36Sopenharmony_ci} 222962306a36Sopenharmony_ciEXPORT_SYMBOL(xa_destroy); 223062306a36Sopenharmony_ci 223162306a36Sopenharmony_ci#ifdef XA_DEBUG 223262306a36Sopenharmony_civoid xa_dump_node(const struct xa_node *node) 223362306a36Sopenharmony_ci{ 223462306a36Sopenharmony_ci unsigned i, j; 223562306a36Sopenharmony_ci 223662306a36Sopenharmony_ci if (!node) 223762306a36Sopenharmony_ci return; 223862306a36Sopenharmony_ci if ((unsigned long)node & 3) { 223962306a36Sopenharmony_ci pr_cont("node %px\n", node); 224062306a36Sopenharmony_ci return; 224162306a36Sopenharmony_ci } 224262306a36Sopenharmony_ci 224362306a36Sopenharmony_ci pr_cont("node %px %s %d parent %px shift %d count %d values %d " 224462306a36Sopenharmony_ci "array %px list %px %px marks", 224562306a36Sopenharmony_ci node, node->parent ? "offset" : "max", node->offset, 224662306a36Sopenharmony_ci node->parent, node->shift, node->count, node->nr_values, 224762306a36Sopenharmony_ci node->array, node->private_list.prev, node->private_list.next); 224862306a36Sopenharmony_ci for (i = 0; i < XA_MAX_MARKS; i++) 224962306a36Sopenharmony_ci for (j = 0; j < XA_MARK_LONGS; j++) 225062306a36Sopenharmony_ci pr_cont(" %lx", node->marks[i][j]); 225162306a36Sopenharmony_ci pr_cont("\n"); 225262306a36Sopenharmony_ci} 225362306a36Sopenharmony_ci 225462306a36Sopenharmony_civoid xa_dump_index(unsigned long index, unsigned int shift) 225562306a36Sopenharmony_ci{ 225662306a36Sopenharmony_ci if (!shift) 225762306a36Sopenharmony_ci pr_info("%lu: ", index); 225862306a36Sopenharmony_ci else if (shift >= BITS_PER_LONG) 225962306a36Sopenharmony_ci pr_info("0-%lu: ", ~0UL); 226062306a36Sopenharmony_ci else 226162306a36Sopenharmony_ci pr_info("%lu-%lu: ", index, index | ((1UL << shift) - 1)); 226262306a36Sopenharmony_ci} 226362306a36Sopenharmony_ci 226462306a36Sopenharmony_civoid xa_dump_entry(const void *entry, unsigned long index, unsigned long shift) 226562306a36Sopenharmony_ci{ 226662306a36Sopenharmony_ci if (!entry) 226762306a36Sopenharmony_ci return; 226862306a36Sopenharmony_ci 226962306a36Sopenharmony_ci xa_dump_index(index, shift); 227062306a36Sopenharmony_ci 227162306a36Sopenharmony_ci if (xa_is_node(entry)) { 227262306a36Sopenharmony_ci if (shift == 0) { 227362306a36Sopenharmony_ci pr_cont("%px\n", entry); 227462306a36Sopenharmony_ci } else { 227562306a36Sopenharmony_ci unsigned long i; 227662306a36Sopenharmony_ci struct xa_node *node = xa_to_node(entry); 227762306a36Sopenharmony_ci xa_dump_node(node); 227862306a36Sopenharmony_ci for (i = 0; i < XA_CHUNK_SIZE; i++) 227962306a36Sopenharmony_ci xa_dump_entry(node->slots[i], 228062306a36Sopenharmony_ci index + (i << node->shift), node->shift); 228162306a36Sopenharmony_ci } 228262306a36Sopenharmony_ci } else if (xa_is_value(entry)) 228362306a36Sopenharmony_ci pr_cont("value %ld (0x%lx) [%px]\n", xa_to_value(entry), 228462306a36Sopenharmony_ci xa_to_value(entry), entry); 228562306a36Sopenharmony_ci else if (!xa_is_internal(entry)) 228662306a36Sopenharmony_ci pr_cont("%px\n", entry); 228762306a36Sopenharmony_ci else if (xa_is_retry(entry)) 228862306a36Sopenharmony_ci pr_cont("retry (%ld)\n", xa_to_internal(entry)); 228962306a36Sopenharmony_ci else if (xa_is_sibling(entry)) 229062306a36Sopenharmony_ci pr_cont("sibling (slot %ld)\n", xa_to_sibling(entry)); 229162306a36Sopenharmony_ci else if (xa_is_zero(entry)) 229262306a36Sopenharmony_ci pr_cont("zero (%ld)\n", xa_to_internal(entry)); 229362306a36Sopenharmony_ci else 229462306a36Sopenharmony_ci pr_cont("UNKNOWN ENTRY (%px)\n", entry); 229562306a36Sopenharmony_ci} 229662306a36Sopenharmony_ci 229762306a36Sopenharmony_civoid xa_dump(const struct xarray *xa) 229862306a36Sopenharmony_ci{ 229962306a36Sopenharmony_ci void *entry = xa->xa_head; 230062306a36Sopenharmony_ci unsigned int shift = 0; 230162306a36Sopenharmony_ci 230262306a36Sopenharmony_ci pr_info("xarray: %px head %px flags %x marks %d %d %d\n", xa, entry, 230362306a36Sopenharmony_ci xa->xa_flags, xa_marked(xa, XA_MARK_0), 230462306a36Sopenharmony_ci xa_marked(xa, XA_MARK_1), xa_marked(xa, XA_MARK_2)); 230562306a36Sopenharmony_ci if (xa_is_node(entry)) 230662306a36Sopenharmony_ci shift = xa_to_node(entry)->shift + XA_CHUNK_SHIFT; 230762306a36Sopenharmony_ci xa_dump_entry(entry, 0, shift); 230862306a36Sopenharmony_ci} 230962306a36Sopenharmony_ci#endif 2310