Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * XArray implementation |
| 4 | * Copyright (c) 2017 Microsoft Corporation |
| 5 | * Author: Matthew Wilcox <willy@infradead.org> |
| 6 | */ |
| 7 | |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 8 | #include <linux/bitmap.h> |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 9 | #include <linux/export.h> |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 10 | #include <linux/list.h> |
| 11 | #include <linux/slab.h> |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 12 | #include <linux/xarray.h> |
| 13 | |
| 14 | /* |
| 15 | * Coding conventions in this file: |
| 16 | * |
| 17 | * @xa is used to refer to the entire xarray. |
| 18 | * @xas is the 'xarray operation state'. It may be either a pointer to |
| 19 | * an xa_state, or an xa_state stored on the stack. This is an unfortunate |
| 20 | * ambiguity. |
| 21 | * @index is the index of the entry being operated on |
| 22 | * @mark is an xa_mark_t; a small number indicating one of the mark bits. |
| 23 | * @node refers to an xa_node; usually the primary one being operated on by |
| 24 | * this function. |
| 25 | * @offset is the index into the slots array inside an xa_node. |
| 26 | * @parent refers to the @xa_node closer to the head than @node. |
| 27 | * @entry refers to something stored in a slot in the xarray |
| 28 | */ |
| 29 | |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 30 | static inline unsigned int xa_lock_type(const struct xarray *xa) |
| 31 | { |
| 32 | return (__force unsigned int)xa->xa_flags & 3; |
| 33 | } |
| 34 | |
| 35 | static inline void xas_lock_type(struct xa_state *xas, unsigned int lock_type) |
| 36 | { |
| 37 | if (lock_type == XA_LOCK_IRQ) |
| 38 | xas_lock_irq(xas); |
| 39 | else if (lock_type == XA_LOCK_BH) |
| 40 | xas_lock_bh(xas); |
| 41 | else |
| 42 | xas_lock(xas); |
| 43 | } |
| 44 | |
| 45 | static inline void xas_unlock_type(struct xa_state *xas, unsigned int lock_type) |
| 46 | { |
| 47 | if (lock_type == XA_LOCK_IRQ) |
| 48 | xas_unlock_irq(xas); |
| 49 | else if (lock_type == XA_LOCK_BH) |
| 50 | xas_unlock_bh(xas); |
| 51 | else |
| 52 | xas_unlock(xas); |
| 53 | } |
| 54 | |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 55 | static inline void xa_mark_set(struct xarray *xa, xa_mark_t mark) |
| 56 | { |
| 57 | if (!(xa->xa_flags & XA_FLAGS_MARK(mark))) |
| 58 | xa->xa_flags |= XA_FLAGS_MARK(mark); |
| 59 | } |
| 60 | |
| 61 | static inline void xa_mark_clear(struct xarray *xa, xa_mark_t mark) |
| 62 | { |
| 63 | if (xa->xa_flags & XA_FLAGS_MARK(mark)) |
| 64 | xa->xa_flags &= ~(XA_FLAGS_MARK(mark)); |
| 65 | } |
| 66 | |
| 67 | static inline unsigned long *node_marks(struct xa_node *node, xa_mark_t mark) |
| 68 | { |
| 69 | return node->marks[(__force unsigned)mark]; |
| 70 | } |
| 71 | |
| 72 | static inline bool node_get_mark(struct xa_node *node, |
| 73 | unsigned int offset, xa_mark_t mark) |
| 74 | { |
| 75 | return test_bit(offset, node_marks(node, mark)); |
| 76 | } |
| 77 | |
| 78 | /* returns true if the bit was set */ |
| 79 | static inline bool node_set_mark(struct xa_node *node, unsigned int offset, |
| 80 | xa_mark_t mark) |
| 81 | { |
| 82 | return __test_and_set_bit(offset, node_marks(node, mark)); |
| 83 | } |
| 84 | |
| 85 | /* returns true if the bit was set */ |
| 86 | static inline bool node_clear_mark(struct xa_node *node, unsigned int offset, |
| 87 | xa_mark_t mark) |
| 88 | { |
| 89 | return __test_and_clear_bit(offset, node_marks(node, mark)); |
| 90 | } |
| 91 | |
| 92 | static inline bool node_any_mark(struct xa_node *node, xa_mark_t mark) |
| 93 | { |
| 94 | return !bitmap_empty(node_marks(node, mark), XA_CHUNK_SIZE); |
| 95 | } |
| 96 | |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 97 | #define mark_inc(mark) do { \ |
| 98 | mark = (__force xa_mark_t)((__force unsigned)(mark) + 1); \ |
| 99 | } while (0) |
| 100 | |
| 101 | /* |
| 102 | * xas_squash_marks() - Merge all marks to the first entry |
| 103 | * @xas: Array operation state. |
| 104 | * |
| 105 | * Set a mark on the first entry if any entry has it set. Clear marks on |
| 106 | * all sibling entries. |
| 107 | */ |
| 108 | static void xas_squash_marks(const struct xa_state *xas) |
| 109 | { |
| 110 | unsigned int mark = 0; |
| 111 | unsigned int limit = xas->xa_offset + xas->xa_sibs + 1; |
| 112 | |
| 113 | if (!xas->xa_sibs) |
| 114 | return; |
| 115 | |
| 116 | do { |
| 117 | unsigned long *marks = xas->xa_node->marks[mark]; |
| 118 | if (find_next_bit(marks, limit, xas->xa_offset + 1) == limit) |
| 119 | continue; |
| 120 | __set_bit(xas->xa_offset, marks); |
| 121 | bitmap_clear(marks, xas->xa_offset + 1, xas->xa_sibs); |
| 122 | } while (mark++ != (__force unsigned)XA_MARK_MAX); |
| 123 | } |
| 124 | |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 125 | /* extracts the offset within this node from the index */ |
| 126 | static unsigned int get_offset(unsigned long index, struct xa_node *node) |
| 127 | { |
| 128 | return (index >> node->shift) & XA_CHUNK_MASK; |
| 129 | } |
| 130 | |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 131 | static void xas_set_offset(struct xa_state *xas) |
| 132 | { |
| 133 | xas->xa_offset = get_offset(xas->xa_index, xas->xa_node); |
| 134 | } |
| 135 | |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 136 | /* move the index either forwards (find) or backwards (sibling slot) */ |
| 137 | static void xas_move_index(struct xa_state *xas, unsigned long offset) |
| 138 | { |
| 139 | unsigned int shift = xas->xa_node->shift; |
| 140 | xas->xa_index &= ~XA_CHUNK_MASK << shift; |
| 141 | xas->xa_index += offset << shift; |
| 142 | } |
| 143 | |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 144 | static void xas_advance(struct xa_state *xas) |
| 145 | { |
| 146 | xas->xa_offset++; |
| 147 | xas_move_index(xas, xas->xa_offset); |
| 148 | } |
| 149 | |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 150 | static void *set_bounds(struct xa_state *xas) |
| 151 | { |
| 152 | xas->xa_node = XAS_BOUNDS; |
| 153 | return NULL; |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * Starts a walk. If the @xas is already valid, we assume that it's on |
| 158 | * the right path and just return where we've got to. If we're in an |
| 159 | * error state, return NULL. If the index is outside the current scope |
| 160 | * of the xarray, return NULL without changing @xas->xa_node. Otherwise |
| 161 | * set @xas->xa_node to NULL and return the current head of the array. |
| 162 | */ |
| 163 | static void *xas_start(struct xa_state *xas) |
| 164 | { |
| 165 | void *entry; |
| 166 | |
| 167 | if (xas_valid(xas)) |
| 168 | return xas_reload(xas); |
| 169 | if (xas_error(xas)) |
| 170 | return NULL; |
| 171 | |
| 172 | entry = xa_head(xas->xa); |
| 173 | if (!xa_is_node(entry)) { |
| 174 | if (xas->xa_index) |
| 175 | return set_bounds(xas); |
| 176 | } else { |
| 177 | if ((xas->xa_index >> xa_to_node(entry)->shift) > XA_CHUNK_MASK) |
| 178 | return set_bounds(xas); |
| 179 | } |
| 180 | |
| 181 | xas->xa_node = NULL; |
| 182 | return entry; |
| 183 | } |
| 184 | |
| 185 | static void *xas_descend(struct xa_state *xas, struct xa_node *node) |
| 186 | { |
| 187 | unsigned int offset = get_offset(xas->xa_index, node); |
| 188 | void *entry = xa_entry(xas->xa, node, offset); |
| 189 | |
| 190 | xas->xa_node = node; |
| 191 | if (xa_is_sibling(entry)) { |
| 192 | offset = xa_to_sibling(entry); |
| 193 | entry = xa_entry(xas->xa, node, offset); |
| 194 | } |
| 195 | |
| 196 | xas->xa_offset = offset; |
| 197 | return entry; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * xas_load() - Load an entry from the XArray (advanced). |
| 202 | * @xas: XArray operation state. |
| 203 | * |
| 204 | * Usually walks the @xas to the appropriate state to load the entry |
| 205 | * stored at xa_index. However, it will do nothing and return %NULL if |
| 206 | * @xas is in an error state. xas_load() will never expand the tree. |
| 207 | * |
| 208 | * If the xa_state is set up to operate on a multi-index entry, xas_load() |
| 209 | * may return %NULL or an internal entry, even if there are entries |
| 210 | * present within the range specified by @xas. |
| 211 | * |
| 212 | * Context: Any context. The caller should hold the xa_lock or the RCU lock. |
| 213 | * Return: Usually an entry in the XArray, but see description for exceptions. |
| 214 | */ |
| 215 | void *xas_load(struct xa_state *xas) |
| 216 | { |
| 217 | void *entry = xas_start(xas); |
| 218 | |
| 219 | while (xa_is_node(entry)) { |
| 220 | struct xa_node *node = xa_to_node(entry); |
| 221 | |
| 222 | if (xas->xa_shift > node->shift) |
| 223 | break; |
| 224 | entry = xas_descend(xas, node); |
| 225 | } |
| 226 | return entry; |
| 227 | } |
| 228 | EXPORT_SYMBOL_GPL(xas_load); |
| 229 | |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 230 | /* Move the radix tree node cache here */ |
| 231 | extern struct kmem_cache *radix_tree_node_cachep; |
| 232 | extern void radix_tree_node_rcu_free(struct rcu_head *head); |
| 233 | |
| 234 | #define XA_RCU_FREE ((struct xarray *)1) |
| 235 | |
| 236 | static void xa_node_free(struct xa_node *node) |
| 237 | { |
| 238 | XA_NODE_BUG_ON(node, !list_empty(&node->private_list)); |
| 239 | node->array = XA_RCU_FREE; |
| 240 | call_rcu(&node->rcu_head, radix_tree_node_rcu_free); |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | * xas_destroy() - Free any resources allocated during the XArray operation. |
| 245 | * @xas: XArray operation state. |
| 246 | * |
| 247 | * This function is now internal-only. |
| 248 | */ |
| 249 | static void xas_destroy(struct xa_state *xas) |
| 250 | { |
| 251 | struct xa_node *node = xas->xa_alloc; |
| 252 | |
| 253 | if (!node) |
| 254 | return; |
| 255 | XA_NODE_BUG_ON(node, !list_empty(&node->private_list)); |
| 256 | kmem_cache_free(radix_tree_node_cachep, node); |
| 257 | xas->xa_alloc = NULL; |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * xas_nomem() - Allocate memory if needed. |
| 262 | * @xas: XArray operation state. |
| 263 | * @gfp: Memory allocation flags. |
| 264 | * |
| 265 | * If we need to add new nodes to the XArray, we try to allocate memory |
| 266 | * with GFP_NOWAIT while holding the lock, which will usually succeed. |
| 267 | * If it fails, @xas is flagged as needing memory to continue. The caller |
| 268 | * should drop the lock and call xas_nomem(). If xas_nomem() succeeds, |
| 269 | * the caller should retry the operation. |
| 270 | * |
| 271 | * Forward progress is guaranteed as one node is allocated here and |
| 272 | * stored in the xa_state where it will be found by xas_alloc(). More |
| 273 | * nodes will likely be found in the slab allocator, but we do not tie |
| 274 | * them up here. |
| 275 | * |
| 276 | * Return: true if memory was needed, and was successfully allocated. |
| 277 | */ |
| 278 | bool xas_nomem(struct xa_state *xas, gfp_t gfp) |
| 279 | { |
| 280 | if (xas->xa_node != XA_ERROR(-ENOMEM)) { |
| 281 | xas_destroy(xas); |
| 282 | return false; |
| 283 | } |
| 284 | xas->xa_alloc = kmem_cache_alloc(radix_tree_node_cachep, gfp); |
| 285 | if (!xas->xa_alloc) |
| 286 | return false; |
| 287 | XA_NODE_BUG_ON(xas->xa_alloc, !list_empty(&xas->xa_alloc->private_list)); |
| 288 | xas->xa_node = XAS_RESTART; |
| 289 | return true; |
| 290 | } |
| 291 | EXPORT_SYMBOL_GPL(xas_nomem); |
| 292 | |
| 293 | /* |
| 294 | * __xas_nomem() - Drop locks and allocate memory if needed. |
| 295 | * @xas: XArray operation state. |
| 296 | * @gfp: Memory allocation flags. |
| 297 | * |
| 298 | * Internal variant of xas_nomem(). |
| 299 | * |
| 300 | * Return: true if memory was needed, and was successfully allocated. |
| 301 | */ |
| 302 | static bool __xas_nomem(struct xa_state *xas, gfp_t gfp) |
| 303 | __must_hold(xas->xa->xa_lock) |
| 304 | { |
| 305 | unsigned int lock_type = xa_lock_type(xas->xa); |
| 306 | |
| 307 | if (xas->xa_node != XA_ERROR(-ENOMEM)) { |
| 308 | xas_destroy(xas); |
| 309 | return false; |
| 310 | } |
| 311 | if (gfpflags_allow_blocking(gfp)) { |
| 312 | xas_unlock_type(xas, lock_type); |
| 313 | xas->xa_alloc = kmem_cache_alloc(radix_tree_node_cachep, gfp); |
| 314 | xas_lock_type(xas, lock_type); |
| 315 | } else { |
| 316 | xas->xa_alloc = kmem_cache_alloc(radix_tree_node_cachep, gfp); |
| 317 | } |
| 318 | if (!xas->xa_alloc) |
| 319 | return false; |
| 320 | XA_NODE_BUG_ON(xas->xa_alloc, !list_empty(&xas->xa_alloc->private_list)); |
| 321 | xas->xa_node = XAS_RESTART; |
| 322 | return true; |
| 323 | } |
| 324 | |
| 325 | static void xas_update(struct xa_state *xas, struct xa_node *node) |
| 326 | { |
| 327 | if (xas->xa_update) |
| 328 | xas->xa_update(node); |
| 329 | else |
| 330 | XA_NODE_BUG_ON(node, !list_empty(&node->private_list)); |
| 331 | } |
| 332 | |
| 333 | static void *xas_alloc(struct xa_state *xas, unsigned int shift) |
| 334 | { |
| 335 | struct xa_node *parent = xas->xa_node; |
| 336 | struct xa_node *node = xas->xa_alloc; |
| 337 | |
| 338 | if (xas_invalid(xas)) |
| 339 | return NULL; |
| 340 | |
| 341 | if (node) { |
| 342 | xas->xa_alloc = NULL; |
| 343 | } else { |
| 344 | node = kmem_cache_alloc(radix_tree_node_cachep, |
| 345 | GFP_NOWAIT | __GFP_NOWARN); |
| 346 | if (!node) { |
| 347 | xas_set_err(xas, -ENOMEM); |
| 348 | return NULL; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | if (parent) { |
| 353 | node->offset = xas->xa_offset; |
| 354 | parent->count++; |
| 355 | XA_NODE_BUG_ON(node, parent->count > XA_CHUNK_SIZE); |
| 356 | xas_update(xas, parent); |
| 357 | } |
| 358 | XA_NODE_BUG_ON(node, shift > BITS_PER_LONG); |
| 359 | XA_NODE_BUG_ON(node, !list_empty(&node->private_list)); |
| 360 | node->shift = shift; |
| 361 | node->count = 0; |
| 362 | node->nr_values = 0; |
| 363 | RCU_INIT_POINTER(node->parent, xas->xa_node); |
| 364 | node->array = xas->xa; |
| 365 | |
| 366 | return node; |
| 367 | } |
| 368 | |
| 369 | /* |
| 370 | * Use this to calculate the maximum index that will need to be created |
| 371 | * in order to add the entry described by @xas. Because we cannot store a |
| 372 | * multiple-index entry at index 0, the calculation is a little more complex |
| 373 | * than you might expect. |
| 374 | */ |
| 375 | static unsigned long xas_max(struct xa_state *xas) |
| 376 | { |
| 377 | unsigned long max = xas->xa_index; |
| 378 | |
| 379 | #ifdef CONFIG_XARRAY_MULTI |
| 380 | if (xas->xa_shift || xas->xa_sibs) { |
| 381 | unsigned long mask; |
| 382 | mask = (((xas->xa_sibs + 1UL) << xas->xa_shift) - 1); |
| 383 | max |= mask; |
| 384 | if (mask == max) |
| 385 | max++; |
| 386 | } |
| 387 | #endif |
| 388 | |
| 389 | return max; |
| 390 | } |
| 391 | |
| 392 | /* The maximum index that can be contained in the array without expanding it */ |
| 393 | static unsigned long max_index(void *entry) |
| 394 | { |
| 395 | if (!xa_is_node(entry)) |
| 396 | return 0; |
| 397 | return (XA_CHUNK_SIZE << xa_to_node(entry)->shift) - 1; |
| 398 | } |
| 399 | |
| 400 | static void xas_shrink(struct xa_state *xas) |
| 401 | { |
| 402 | struct xarray *xa = xas->xa; |
| 403 | struct xa_node *node = xas->xa_node; |
| 404 | |
| 405 | for (;;) { |
| 406 | void *entry; |
| 407 | |
| 408 | XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE); |
| 409 | if (node->count != 1) |
| 410 | break; |
| 411 | entry = xa_entry_locked(xa, node, 0); |
| 412 | if (!entry) |
| 413 | break; |
| 414 | if (!xa_is_node(entry) && node->shift) |
| 415 | break; |
| 416 | xas->xa_node = XAS_BOUNDS; |
| 417 | |
| 418 | RCU_INIT_POINTER(xa->xa_head, entry); |
| 419 | |
| 420 | node->count = 0; |
| 421 | node->nr_values = 0; |
| 422 | if (!xa_is_node(entry)) |
| 423 | RCU_INIT_POINTER(node->slots[0], XA_RETRY_ENTRY); |
| 424 | xas_update(xas, node); |
| 425 | xa_node_free(node); |
| 426 | if (!xa_is_node(entry)) |
| 427 | break; |
| 428 | node = xa_to_node(entry); |
| 429 | node->parent = NULL; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | /* |
| 434 | * xas_delete_node() - Attempt to delete an xa_node |
| 435 | * @xas: Array operation state. |
| 436 | * |
| 437 | * Attempts to delete the @xas->xa_node. This will fail if xa->node has |
| 438 | * a non-zero reference count. |
| 439 | */ |
| 440 | static void xas_delete_node(struct xa_state *xas) |
| 441 | { |
| 442 | struct xa_node *node = xas->xa_node; |
| 443 | |
| 444 | for (;;) { |
| 445 | struct xa_node *parent; |
| 446 | |
| 447 | XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE); |
| 448 | if (node->count) |
| 449 | break; |
| 450 | |
| 451 | parent = xa_parent_locked(xas->xa, node); |
| 452 | xas->xa_node = parent; |
| 453 | xas->xa_offset = node->offset; |
| 454 | xa_node_free(node); |
| 455 | |
| 456 | if (!parent) { |
| 457 | xas->xa->xa_head = NULL; |
| 458 | xas->xa_node = XAS_BOUNDS; |
| 459 | return; |
| 460 | } |
| 461 | |
| 462 | parent->slots[xas->xa_offset] = NULL; |
| 463 | parent->count--; |
| 464 | XA_NODE_BUG_ON(parent, parent->count > XA_CHUNK_SIZE); |
| 465 | node = parent; |
| 466 | xas_update(xas, node); |
| 467 | } |
| 468 | |
| 469 | if (!node->parent) |
| 470 | xas_shrink(xas); |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * xas_free_nodes() - Free this node and all nodes that it references |
| 475 | * @xas: Array operation state. |
| 476 | * @top: Node to free |
| 477 | * |
| 478 | * This node has been removed from the tree. We must now free it and all |
| 479 | * of its subnodes. There may be RCU walkers with references into the tree, |
| 480 | * so we must replace all entries with retry markers. |
| 481 | */ |
| 482 | static void xas_free_nodes(struct xa_state *xas, struct xa_node *top) |
| 483 | { |
| 484 | unsigned int offset = 0; |
| 485 | struct xa_node *node = top; |
| 486 | |
| 487 | for (;;) { |
| 488 | void *entry = xa_entry_locked(xas->xa, node, offset); |
| 489 | |
| 490 | if (xa_is_node(entry)) { |
| 491 | node = xa_to_node(entry); |
| 492 | offset = 0; |
| 493 | continue; |
| 494 | } |
| 495 | if (entry) |
| 496 | RCU_INIT_POINTER(node->slots[offset], XA_RETRY_ENTRY); |
| 497 | offset++; |
| 498 | while (offset == XA_CHUNK_SIZE) { |
| 499 | struct xa_node *parent; |
| 500 | |
| 501 | parent = xa_parent_locked(xas->xa, node); |
| 502 | offset = node->offset + 1; |
| 503 | node->count = 0; |
| 504 | node->nr_values = 0; |
| 505 | xas_update(xas, node); |
| 506 | xa_node_free(node); |
| 507 | if (node == top) |
| 508 | return; |
| 509 | node = parent; |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * xas_expand adds nodes to the head of the tree until it has reached |
| 516 | * sufficient height to be able to contain @xas->xa_index |
| 517 | */ |
| 518 | static int xas_expand(struct xa_state *xas, void *head) |
| 519 | { |
| 520 | struct xarray *xa = xas->xa; |
| 521 | struct xa_node *node = NULL; |
| 522 | unsigned int shift = 0; |
| 523 | unsigned long max = xas_max(xas); |
| 524 | |
| 525 | if (!head) { |
| 526 | if (max == 0) |
| 527 | return 0; |
| 528 | while ((max >> shift) >= XA_CHUNK_SIZE) |
| 529 | shift += XA_CHUNK_SHIFT; |
| 530 | return shift + XA_CHUNK_SHIFT; |
| 531 | } else if (xa_is_node(head)) { |
| 532 | node = xa_to_node(head); |
| 533 | shift = node->shift + XA_CHUNK_SHIFT; |
| 534 | } |
| 535 | xas->xa_node = NULL; |
| 536 | |
| 537 | while (max > max_index(head)) { |
| 538 | xa_mark_t mark = 0; |
| 539 | |
| 540 | XA_NODE_BUG_ON(node, shift > BITS_PER_LONG); |
| 541 | node = xas_alloc(xas, shift); |
| 542 | if (!node) |
| 543 | return -ENOMEM; |
| 544 | |
| 545 | node->count = 1; |
| 546 | if (xa_is_value(head)) |
| 547 | node->nr_values = 1; |
| 548 | RCU_INIT_POINTER(node->slots[0], head); |
| 549 | |
| 550 | /* Propagate the aggregated mark info to the new child */ |
| 551 | for (;;) { |
| 552 | if (xa_marked(xa, mark)) |
| 553 | node_set_mark(node, 0, mark); |
| 554 | if (mark == XA_MARK_MAX) |
| 555 | break; |
| 556 | mark_inc(mark); |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | * Now that the new node is fully initialised, we can add |
| 561 | * it to the tree |
| 562 | */ |
| 563 | if (xa_is_node(head)) { |
| 564 | xa_to_node(head)->offset = 0; |
| 565 | rcu_assign_pointer(xa_to_node(head)->parent, node); |
| 566 | } |
| 567 | head = xa_mk_node(node); |
| 568 | rcu_assign_pointer(xa->xa_head, head); |
| 569 | xas_update(xas, node); |
| 570 | |
| 571 | shift += XA_CHUNK_SHIFT; |
| 572 | } |
| 573 | |
| 574 | xas->xa_node = node; |
| 575 | return shift; |
| 576 | } |
| 577 | |
| 578 | /* |
| 579 | * xas_create() - Create a slot to store an entry in. |
| 580 | * @xas: XArray operation state. |
| 581 | * |
| 582 | * Most users will not need to call this function directly, as it is called |
| 583 | * by xas_store(). It is useful for doing conditional store operations |
| 584 | * (see the xa_cmpxchg() implementation for an example). |
| 585 | * |
| 586 | * Return: If the slot already existed, returns the contents of this slot. |
| 587 | * If the slot was newly created, returns NULL. If it failed to create the |
| 588 | * slot, returns NULL and indicates the error in @xas. |
| 589 | */ |
| 590 | static void *xas_create(struct xa_state *xas) |
| 591 | { |
| 592 | struct xarray *xa = xas->xa; |
| 593 | void *entry; |
| 594 | void __rcu **slot; |
| 595 | struct xa_node *node = xas->xa_node; |
| 596 | int shift; |
| 597 | unsigned int order = xas->xa_shift; |
| 598 | |
| 599 | if (xas_top(node)) { |
| 600 | entry = xa_head_locked(xa); |
| 601 | xas->xa_node = NULL; |
| 602 | shift = xas_expand(xas, entry); |
| 603 | if (shift < 0) |
| 604 | return NULL; |
| 605 | entry = xa_head_locked(xa); |
| 606 | slot = &xa->xa_head; |
| 607 | } else if (xas_error(xas)) { |
| 608 | return NULL; |
| 609 | } else if (node) { |
| 610 | unsigned int offset = xas->xa_offset; |
| 611 | |
| 612 | shift = node->shift; |
| 613 | entry = xa_entry_locked(xa, node, offset); |
| 614 | slot = &node->slots[offset]; |
| 615 | } else { |
| 616 | shift = 0; |
| 617 | entry = xa_head_locked(xa); |
| 618 | slot = &xa->xa_head; |
| 619 | } |
| 620 | |
| 621 | while (shift > order) { |
| 622 | shift -= XA_CHUNK_SHIFT; |
| 623 | if (!entry) { |
| 624 | node = xas_alloc(xas, shift); |
| 625 | if (!node) |
| 626 | break; |
| 627 | rcu_assign_pointer(*slot, xa_mk_node(node)); |
| 628 | } else if (xa_is_node(entry)) { |
| 629 | node = xa_to_node(entry); |
| 630 | } else { |
| 631 | break; |
| 632 | } |
| 633 | entry = xas_descend(xas, node); |
| 634 | slot = &node->slots[xas->xa_offset]; |
| 635 | } |
| 636 | |
| 637 | return entry; |
| 638 | } |
| 639 | |
Matthew Wilcox | 2264f51 | 2017-12-04 00:11:48 -0500 | [diff] [blame] | 640 | /** |
| 641 | * xas_create_range() - Ensure that stores to this range will succeed |
| 642 | * @xas: XArray operation state. |
| 643 | * |
| 644 | * Creates all of the slots in the range covered by @xas. Sets @xas to |
| 645 | * create single-index entries and positions it at the beginning of the |
| 646 | * range. This is for the benefit of users which have not yet been |
| 647 | * converted to use multi-index entries. |
| 648 | */ |
| 649 | void xas_create_range(struct xa_state *xas) |
| 650 | { |
| 651 | unsigned long index = xas->xa_index; |
| 652 | unsigned char shift = xas->xa_shift; |
| 653 | unsigned char sibs = xas->xa_sibs; |
| 654 | |
| 655 | xas->xa_index |= ((sibs + 1) << shift) - 1; |
| 656 | if (xas_is_node(xas) && xas->xa_node->shift == xas->xa_shift) |
| 657 | xas->xa_offset |= sibs; |
| 658 | xas->xa_shift = 0; |
| 659 | xas->xa_sibs = 0; |
| 660 | |
| 661 | for (;;) { |
| 662 | xas_create(xas); |
| 663 | if (xas_error(xas)) |
| 664 | goto restore; |
| 665 | if (xas->xa_index <= (index | XA_CHUNK_MASK)) |
| 666 | goto success; |
| 667 | xas->xa_index -= XA_CHUNK_SIZE; |
| 668 | |
| 669 | for (;;) { |
| 670 | struct xa_node *node = xas->xa_node; |
| 671 | xas->xa_node = xa_parent_locked(xas->xa, node); |
| 672 | xas->xa_offset = node->offset - 1; |
| 673 | if (node->offset != 0) |
| 674 | break; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | restore: |
| 679 | xas->xa_shift = shift; |
| 680 | xas->xa_sibs = sibs; |
| 681 | xas->xa_index = index; |
| 682 | return; |
| 683 | success: |
| 684 | xas->xa_index = index; |
| 685 | if (xas->xa_node) |
| 686 | xas_set_offset(xas); |
| 687 | } |
| 688 | EXPORT_SYMBOL_GPL(xas_create_range); |
| 689 | |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 690 | static void update_node(struct xa_state *xas, struct xa_node *node, |
| 691 | int count, int values) |
| 692 | { |
| 693 | if (!node || (!count && !values)) |
| 694 | return; |
| 695 | |
| 696 | node->count += count; |
| 697 | node->nr_values += values; |
| 698 | XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE); |
| 699 | XA_NODE_BUG_ON(node, node->nr_values > XA_CHUNK_SIZE); |
| 700 | xas_update(xas, node); |
| 701 | if (count < 0) |
| 702 | xas_delete_node(xas); |
| 703 | } |
| 704 | |
| 705 | /** |
| 706 | * xas_store() - Store this entry in the XArray. |
| 707 | * @xas: XArray operation state. |
| 708 | * @entry: New entry. |
| 709 | * |
| 710 | * If @xas is operating on a multi-index entry, the entry returned by this |
| 711 | * function is essentially meaningless (it may be an internal entry or it |
| 712 | * may be %NULL, even if there are non-NULL entries at some of the indices |
| 713 | * covered by the range). This is not a problem for any current users, |
| 714 | * and can be changed if needed. |
| 715 | * |
| 716 | * Return: The old entry at this index. |
| 717 | */ |
| 718 | void *xas_store(struct xa_state *xas, void *entry) |
| 719 | { |
| 720 | struct xa_node *node; |
| 721 | void __rcu **slot = &xas->xa->xa_head; |
| 722 | unsigned int offset, max; |
| 723 | int count = 0; |
| 724 | int values = 0; |
| 725 | void *first, *next; |
| 726 | bool value = xa_is_value(entry); |
| 727 | |
| 728 | if (entry) |
| 729 | first = xas_create(xas); |
| 730 | else |
| 731 | first = xas_load(xas); |
| 732 | |
| 733 | if (xas_invalid(xas)) |
| 734 | return first; |
| 735 | node = xas->xa_node; |
| 736 | if (node && (xas->xa_shift < node->shift)) |
| 737 | xas->xa_sibs = 0; |
| 738 | if ((first == entry) && !xas->xa_sibs) |
| 739 | return first; |
| 740 | |
| 741 | next = first; |
| 742 | offset = xas->xa_offset; |
| 743 | max = xas->xa_offset + xas->xa_sibs; |
| 744 | if (node) { |
| 745 | slot = &node->slots[offset]; |
| 746 | if (xas->xa_sibs) |
| 747 | xas_squash_marks(xas); |
| 748 | } |
| 749 | if (!entry) |
| 750 | xas_init_marks(xas); |
| 751 | |
| 752 | for (;;) { |
| 753 | /* |
| 754 | * Must clear the marks before setting the entry to NULL, |
| 755 | * otherwise xas_for_each_marked may find a NULL entry and |
| 756 | * stop early. rcu_assign_pointer contains a release barrier |
| 757 | * so the mark clearing will appear to happen before the |
| 758 | * entry is set to NULL. |
| 759 | */ |
| 760 | rcu_assign_pointer(*slot, entry); |
| 761 | if (xa_is_node(next)) |
| 762 | xas_free_nodes(xas, xa_to_node(next)); |
| 763 | if (!node) |
| 764 | break; |
| 765 | count += !next - !entry; |
| 766 | values += !xa_is_value(first) - !value; |
| 767 | if (entry) { |
| 768 | if (offset == max) |
| 769 | break; |
| 770 | if (!xa_is_sibling(entry)) |
| 771 | entry = xa_mk_sibling(xas->xa_offset); |
| 772 | } else { |
| 773 | if (offset == XA_CHUNK_MASK) |
| 774 | break; |
| 775 | } |
| 776 | next = xa_entry_locked(xas->xa, node, ++offset); |
| 777 | if (!xa_is_sibling(next)) { |
| 778 | if (!entry && (offset > max)) |
| 779 | break; |
| 780 | first = next; |
| 781 | } |
| 782 | slot++; |
| 783 | } |
| 784 | |
| 785 | update_node(xas, node, count, values); |
| 786 | return first; |
| 787 | } |
| 788 | EXPORT_SYMBOL_GPL(xas_store); |
| 789 | |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 790 | /** |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 791 | * xas_get_mark() - Returns the state of this mark. |
| 792 | * @xas: XArray operation state. |
| 793 | * @mark: Mark number. |
| 794 | * |
| 795 | * Return: true if the mark is set, false if the mark is clear or @xas |
| 796 | * is in an error state. |
| 797 | */ |
| 798 | bool xas_get_mark(const struct xa_state *xas, xa_mark_t mark) |
| 799 | { |
| 800 | if (xas_invalid(xas)) |
| 801 | return false; |
| 802 | if (!xas->xa_node) |
| 803 | return xa_marked(xas->xa, mark); |
| 804 | return node_get_mark(xas->xa_node, xas->xa_offset, mark); |
| 805 | } |
| 806 | EXPORT_SYMBOL_GPL(xas_get_mark); |
| 807 | |
| 808 | /** |
| 809 | * xas_set_mark() - Sets the mark on this entry and its parents. |
| 810 | * @xas: XArray operation state. |
| 811 | * @mark: Mark number. |
| 812 | * |
| 813 | * Sets the specified mark on this entry, and walks up the tree setting it |
| 814 | * on all the ancestor entries. Does nothing if @xas has not been walked to |
| 815 | * an entry, or is in an error state. |
| 816 | */ |
| 817 | void xas_set_mark(const struct xa_state *xas, xa_mark_t mark) |
| 818 | { |
| 819 | struct xa_node *node = xas->xa_node; |
| 820 | unsigned int offset = xas->xa_offset; |
| 821 | |
| 822 | if (xas_invalid(xas)) |
| 823 | return; |
| 824 | |
| 825 | while (node) { |
| 826 | if (node_set_mark(node, offset, mark)) |
| 827 | return; |
| 828 | offset = node->offset; |
| 829 | node = xa_parent_locked(xas->xa, node); |
| 830 | } |
| 831 | |
| 832 | if (!xa_marked(xas->xa, mark)) |
| 833 | xa_mark_set(xas->xa, mark); |
| 834 | } |
| 835 | EXPORT_SYMBOL_GPL(xas_set_mark); |
| 836 | |
| 837 | /** |
| 838 | * xas_clear_mark() - Clears the mark on this entry and its parents. |
| 839 | * @xas: XArray operation state. |
| 840 | * @mark: Mark number. |
| 841 | * |
| 842 | * Clears the specified mark on this entry, and walks back to the head |
| 843 | * attempting to clear it on all the ancestor entries. Does nothing if |
| 844 | * @xas has not been walked to an entry, or is in an error state. |
| 845 | */ |
| 846 | void xas_clear_mark(const struct xa_state *xas, xa_mark_t mark) |
| 847 | { |
| 848 | struct xa_node *node = xas->xa_node; |
| 849 | unsigned int offset = xas->xa_offset; |
| 850 | |
| 851 | if (xas_invalid(xas)) |
| 852 | return; |
| 853 | |
| 854 | while (node) { |
| 855 | if (!node_clear_mark(node, offset, mark)) |
| 856 | return; |
| 857 | if (node_any_mark(node, mark)) |
| 858 | return; |
| 859 | |
| 860 | offset = node->offset; |
| 861 | node = xa_parent_locked(xas->xa, node); |
| 862 | } |
| 863 | |
| 864 | if (xa_marked(xas->xa, mark)) |
| 865 | xa_mark_clear(xas->xa, mark); |
| 866 | } |
| 867 | EXPORT_SYMBOL_GPL(xas_clear_mark); |
| 868 | |
| 869 | /** |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 870 | * xas_init_marks() - Initialise all marks for the entry |
| 871 | * @xas: Array operations state. |
| 872 | * |
| 873 | * Initialise all marks for the entry specified by @xas. If we're tracking |
| 874 | * free entries with a mark, we need to set it on all entries. All other |
| 875 | * marks are cleared. |
| 876 | * |
| 877 | * This implementation is not as efficient as it could be; we may walk |
| 878 | * up the tree multiple times. |
| 879 | */ |
| 880 | void xas_init_marks(const struct xa_state *xas) |
| 881 | { |
| 882 | xa_mark_t mark = 0; |
| 883 | |
| 884 | for (;;) { |
| 885 | xas_clear_mark(xas, mark); |
| 886 | if (mark == XA_MARK_MAX) |
| 887 | break; |
| 888 | mark_inc(mark); |
| 889 | } |
| 890 | } |
| 891 | EXPORT_SYMBOL_GPL(xas_init_marks); |
| 892 | |
| 893 | /** |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 894 | * xas_pause() - Pause a walk to drop a lock. |
| 895 | * @xas: XArray operation state. |
| 896 | * |
| 897 | * Some users need to pause a walk and drop the lock they're holding in |
| 898 | * order to yield to a higher priority thread or carry out an operation |
| 899 | * on an entry. Those users should call this function before they drop |
| 900 | * the lock. It resets the @xas to be suitable for the next iteration |
| 901 | * of the loop after the user has reacquired the lock. If most entries |
| 902 | * found during a walk require you to call xas_pause(), the xa_for_each() |
| 903 | * iterator may be more appropriate. |
| 904 | * |
| 905 | * Note that xas_pause() only works for forward iteration. If a user needs |
| 906 | * to pause a reverse iteration, we will need a xas_pause_rev(). |
| 907 | */ |
| 908 | void xas_pause(struct xa_state *xas) |
| 909 | { |
| 910 | struct xa_node *node = xas->xa_node; |
| 911 | |
| 912 | if (xas_invalid(xas)) |
| 913 | return; |
| 914 | |
| 915 | if (node) { |
| 916 | unsigned int offset = xas->xa_offset; |
| 917 | while (++offset < XA_CHUNK_SIZE) { |
| 918 | if (!xa_is_sibling(xa_entry(xas->xa, node, offset))) |
| 919 | break; |
| 920 | } |
| 921 | xas->xa_index += (offset - xas->xa_offset) << node->shift; |
| 922 | } else { |
| 923 | xas->xa_index++; |
| 924 | } |
| 925 | xas->xa_node = XAS_RESTART; |
| 926 | } |
| 927 | EXPORT_SYMBOL_GPL(xas_pause); |
| 928 | |
Matthew Wilcox | 64d3e9a | 2017-12-01 00:06:52 -0500 | [diff] [blame] | 929 | /* |
| 930 | * __xas_prev() - Find the previous entry in the XArray. |
| 931 | * @xas: XArray operation state. |
| 932 | * |
| 933 | * Helper function for xas_prev() which handles all the complex cases |
| 934 | * out of line. |
| 935 | */ |
| 936 | void *__xas_prev(struct xa_state *xas) |
| 937 | { |
| 938 | void *entry; |
| 939 | |
| 940 | if (!xas_frozen(xas->xa_node)) |
| 941 | xas->xa_index--; |
| 942 | if (xas_not_node(xas->xa_node)) |
| 943 | return xas_load(xas); |
| 944 | |
| 945 | if (xas->xa_offset != get_offset(xas->xa_index, xas->xa_node)) |
| 946 | xas->xa_offset--; |
| 947 | |
| 948 | while (xas->xa_offset == 255) { |
| 949 | xas->xa_offset = xas->xa_node->offset - 1; |
| 950 | xas->xa_node = xa_parent(xas->xa, xas->xa_node); |
| 951 | if (!xas->xa_node) |
| 952 | return set_bounds(xas); |
| 953 | } |
| 954 | |
| 955 | for (;;) { |
| 956 | entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset); |
| 957 | if (!xa_is_node(entry)) |
| 958 | return entry; |
| 959 | |
| 960 | xas->xa_node = xa_to_node(entry); |
| 961 | xas_set_offset(xas); |
| 962 | } |
| 963 | } |
| 964 | EXPORT_SYMBOL_GPL(__xas_prev); |
| 965 | |
| 966 | /* |
| 967 | * __xas_next() - Find the next entry in the XArray. |
| 968 | * @xas: XArray operation state. |
| 969 | * |
| 970 | * Helper function for xas_next() which handles all the complex cases |
| 971 | * out of line. |
| 972 | */ |
| 973 | void *__xas_next(struct xa_state *xas) |
| 974 | { |
| 975 | void *entry; |
| 976 | |
| 977 | if (!xas_frozen(xas->xa_node)) |
| 978 | xas->xa_index++; |
| 979 | if (xas_not_node(xas->xa_node)) |
| 980 | return xas_load(xas); |
| 981 | |
| 982 | if (xas->xa_offset != get_offset(xas->xa_index, xas->xa_node)) |
| 983 | xas->xa_offset++; |
| 984 | |
| 985 | while (xas->xa_offset == XA_CHUNK_SIZE) { |
| 986 | xas->xa_offset = xas->xa_node->offset + 1; |
| 987 | xas->xa_node = xa_parent(xas->xa, xas->xa_node); |
| 988 | if (!xas->xa_node) |
| 989 | return set_bounds(xas); |
| 990 | } |
| 991 | |
| 992 | for (;;) { |
| 993 | entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset); |
| 994 | if (!xa_is_node(entry)) |
| 995 | return entry; |
| 996 | |
| 997 | xas->xa_node = xa_to_node(entry); |
| 998 | xas_set_offset(xas); |
| 999 | } |
| 1000 | } |
| 1001 | EXPORT_SYMBOL_GPL(__xas_next); |
| 1002 | |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 1003 | /** |
| 1004 | * xas_find() - Find the next present entry in the XArray. |
| 1005 | * @xas: XArray operation state. |
| 1006 | * @max: Highest index to return. |
| 1007 | * |
| 1008 | * If the @xas has not yet been walked to an entry, return the entry |
| 1009 | * which has an index >= xas.xa_index. If it has been walked, the entry |
| 1010 | * currently being pointed at has been processed, and so we move to the |
| 1011 | * next entry. |
| 1012 | * |
| 1013 | * If no entry is found and the array is smaller than @max, the iterator |
| 1014 | * is set to the smallest index not yet in the array. This allows @xas |
| 1015 | * to be immediately passed to xas_store(). |
| 1016 | * |
| 1017 | * Return: The entry, if found, otherwise %NULL. |
| 1018 | */ |
| 1019 | void *xas_find(struct xa_state *xas, unsigned long max) |
| 1020 | { |
| 1021 | void *entry; |
| 1022 | |
| 1023 | if (xas_error(xas)) |
| 1024 | return NULL; |
| 1025 | |
| 1026 | if (!xas->xa_node) { |
| 1027 | xas->xa_index = 1; |
| 1028 | return set_bounds(xas); |
| 1029 | } else if (xas_top(xas->xa_node)) { |
| 1030 | entry = xas_load(xas); |
| 1031 | if (entry || xas_not_node(xas->xa_node)) |
| 1032 | return entry; |
| 1033 | } else if (!xas->xa_node->shift && |
| 1034 | xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)) { |
| 1035 | xas->xa_offset = ((xas->xa_index - 1) & XA_CHUNK_MASK) + 1; |
| 1036 | } |
| 1037 | |
| 1038 | xas_advance(xas); |
| 1039 | |
| 1040 | while (xas->xa_node && (xas->xa_index <= max)) { |
| 1041 | if (unlikely(xas->xa_offset == XA_CHUNK_SIZE)) { |
| 1042 | xas->xa_offset = xas->xa_node->offset + 1; |
| 1043 | xas->xa_node = xa_parent(xas->xa, xas->xa_node); |
| 1044 | continue; |
| 1045 | } |
| 1046 | |
| 1047 | entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset); |
| 1048 | if (xa_is_node(entry)) { |
| 1049 | xas->xa_node = xa_to_node(entry); |
| 1050 | xas->xa_offset = 0; |
| 1051 | continue; |
| 1052 | } |
| 1053 | if (entry && !xa_is_sibling(entry)) |
| 1054 | return entry; |
| 1055 | |
| 1056 | xas_advance(xas); |
| 1057 | } |
| 1058 | |
| 1059 | if (!xas->xa_node) |
| 1060 | xas->xa_node = XAS_BOUNDS; |
| 1061 | return NULL; |
| 1062 | } |
| 1063 | EXPORT_SYMBOL_GPL(xas_find); |
| 1064 | |
| 1065 | /** |
| 1066 | * xas_find_marked() - Find the next marked entry in the XArray. |
| 1067 | * @xas: XArray operation state. |
| 1068 | * @max: Highest index to return. |
| 1069 | * @mark: Mark number to search for. |
| 1070 | * |
| 1071 | * If the @xas has not yet been walked to an entry, return the marked entry |
| 1072 | * which has an index >= xas.xa_index. If it has been walked, the entry |
| 1073 | * currently being pointed at has been processed, and so we return the |
| 1074 | * first marked entry with an index > xas.xa_index. |
| 1075 | * |
| 1076 | * If no marked entry is found and the array is smaller than @max, @xas is |
| 1077 | * set to the bounds state and xas->xa_index is set to the smallest index |
| 1078 | * not yet in the array. This allows @xas to be immediately passed to |
| 1079 | * xas_store(). |
| 1080 | * |
| 1081 | * If no entry is found before @max is reached, @xas is set to the restart |
| 1082 | * state. |
| 1083 | * |
| 1084 | * Return: The entry, if found, otherwise %NULL. |
| 1085 | */ |
| 1086 | void *xas_find_marked(struct xa_state *xas, unsigned long max, xa_mark_t mark) |
| 1087 | { |
| 1088 | bool advance = true; |
| 1089 | unsigned int offset; |
| 1090 | void *entry; |
| 1091 | |
| 1092 | if (xas_error(xas)) |
| 1093 | return NULL; |
| 1094 | |
| 1095 | if (!xas->xa_node) { |
| 1096 | xas->xa_index = 1; |
| 1097 | goto out; |
| 1098 | } else if (xas_top(xas->xa_node)) { |
| 1099 | advance = false; |
| 1100 | entry = xa_head(xas->xa); |
| 1101 | xas->xa_node = NULL; |
| 1102 | if (xas->xa_index > max_index(entry)) |
| 1103 | goto bounds; |
| 1104 | if (!xa_is_node(entry)) { |
| 1105 | if (xa_marked(xas->xa, mark)) |
| 1106 | return entry; |
| 1107 | xas->xa_index = 1; |
| 1108 | goto out; |
| 1109 | } |
| 1110 | xas->xa_node = xa_to_node(entry); |
| 1111 | xas->xa_offset = xas->xa_index >> xas->xa_node->shift; |
| 1112 | } |
| 1113 | |
| 1114 | while (xas->xa_index <= max) { |
| 1115 | if (unlikely(xas->xa_offset == XA_CHUNK_SIZE)) { |
| 1116 | xas->xa_offset = xas->xa_node->offset + 1; |
| 1117 | xas->xa_node = xa_parent(xas->xa, xas->xa_node); |
| 1118 | if (!xas->xa_node) |
| 1119 | break; |
| 1120 | advance = false; |
| 1121 | continue; |
| 1122 | } |
| 1123 | |
| 1124 | if (!advance) { |
| 1125 | entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset); |
| 1126 | if (xa_is_sibling(entry)) { |
| 1127 | xas->xa_offset = xa_to_sibling(entry); |
| 1128 | xas_move_index(xas, xas->xa_offset); |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | offset = xas_find_chunk(xas, advance, mark); |
| 1133 | if (offset > xas->xa_offset) { |
| 1134 | advance = false; |
| 1135 | xas_move_index(xas, offset); |
| 1136 | /* Mind the wrap */ |
| 1137 | if ((xas->xa_index - 1) >= max) |
| 1138 | goto max; |
| 1139 | xas->xa_offset = offset; |
| 1140 | if (offset == XA_CHUNK_SIZE) |
| 1141 | continue; |
| 1142 | } |
| 1143 | |
| 1144 | entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset); |
| 1145 | if (!xa_is_node(entry)) |
| 1146 | return entry; |
| 1147 | xas->xa_node = xa_to_node(entry); |
| 1148 | xas_set_offset(xas); |
| 1149 | } |
| 1150 | |
| 1151 | out: |
| 1152 | if (!max) |
| 1153 | goto max; |
| 1154 | bounds: |
| 1155 | xas->xa_node = XAS_BOUNDS; |
| 1156 | return NULL; |
| 1157 | max: |
| 1158 | xas->xa_node = XAS_RESTART; |
| 1159 | return NULL; |
| 1160 | } |
| 1161 | EXPORT_SYMBOL_GPL(xas_find_marked); |
| 1162 | |
| 1163 | /** |
Matthew Wilcox | 4e99d4e | 2018-06-01 22:46:02 -0400 | [diff] [blame] | 1164 | * xas_find_conflict() - Find the next present entry in a range. |
| 1165 | * @xas: XArray operation state. |
| 1166 | * |
| 1167 | * The @xas describes both a range and a position within that range. |
| 1168 | * |
| 1169 | * Context: Any context. Expects xa_lock to be held. |
| 1170 | * Return: The next entry in the range covered by @xas or %NULL. |
| 1171 | */ |
| 1172 | void *xas_find_conflict(struct xa_state *xas) |
| 1173 | { |
| 1174 | void *curr; |
| 1175 | |
| 1176 | if (xas_error(xas)) |
| 1177 | return NULL; |
| 1178 | |
| 1179 | if (!xas->xa_node) |
| 1180 | return NULL; |
| 1181 | |
| 1182 | if (xas_top(xas->xa_node)) { |
| 1183 | curr = xas_start(xas); |
| 1184 | if (!curr) |
| 1185 | return NULL; |
| 1186 | while (xa_is_node(curr)) { |
| 1187 | struct xa_node *node = xa_to_node(curr); |
| 1188 | curr = xas_descend(xas, node); |
| 1189 | } |
| 1190 | if (curr) |
| 1191 | return curr; |
| 1192 | } |
| 1193 | |
| 1194 | if (xas->xa_node->shift > xas->xa_shift) |
| 1195 | return NULL; |
| 1196 | |
| 1197 | for (;;) { |
| 1198 | if (xas->xa_node->shift == xas->xa_shift) { |
| 1199 | if ((xas->xa_offset & xas->xa_sibs) == xas->xa_sibs) |
| 1200 | break; |
| 1201 | } else if (xas->xa_offset == XA_CHUNK_MASK) { |
| 1202 | xas->xa_offset = xas->xa_node->offset; |
| 1203 | xas->xa_node = xa_parent_locked(xas->xa, xas->xa_node); |
| 1204 | if (!xas->xa_node) |
| 1205 | break; |
| 1206 | continue; |
| 1207 | } |
| 1208 | curr = xa_entry_locked(xas->xa, xas->xa_node, ++xas->xa_offset); |
| 1209 | if (xa_is_sibling(curr)) |
| 1210 | continue; |
| 1211 | while (xa_is_node(curr)) { |
| 1212 | xas->xa_node = xa_to_node(curr); |
| 1213 | xas->xa_offset = 0; |
| 1214 | curr = xa_entry_locked(xas->xa, xas->xa_node, 0); |
| 1215 | } |
| 1216 | if (curr) |
| 1217 | return curr; |
| 1218 | } |
| 1219 | xas->xa_offset -= xas->xa_sibs; |
| 1220 | return NULL; |
| 1221 | } |
| 1222 | EXPORT_SYMBOL_GPL(xas_find_conflict); |
| 1223 | |
| 1224 | /** |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 1225 | * xa_init_flags() - Initialise an empty XArray with flags. |
| 1226 | * @xa: XArray. |
| 1227 | * @flags: XA_FLAG values. |
| 1228 | * |
| 1229 | * If you need to initialise an XArray with special flags (eg you need |
| 1230 | * to take the lock from interrupt context), use this function instead |
| 1231 | * of xa_init(). |
| 1232 | * |
| 1233 | * Context: Any context. |
| 1234 | */ |
| 1235 | void xa_init_flags(struct xarray *xa, gfp_t flags) |
| 1236 | { |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 1237 | unsigned int lock_type; |
| 1238 | static struct lock_class_key xa_lock_irq; |
| 1239 | static struct lock_class_key xa_lock_bh; |
| 1240 | |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 1241 | spin_lock_init(&xa->xa_lock); |
| 1242 | xa->xa_flags = flags; |
| 1243 | xa->xa_head = NULL; |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 1244 | |
| 1245 | lock_type = xa_lock_type(xa); |
| 1246 | if (lock_type == XA_LOCK_IRQ) |
| 1247 | lockdep_set_class(&xa->xa_lock, &xa_lock_irq); |
| 1248 | else if (lock_type == XA_LOCK_BH) |
| 1249 | lockdep_set_class(&xa->xa_lock, &xa_lock_bh); |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 1250 | } |
| 1251 | EXPORT_SYMBOL(xa_init_flags); |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 1252 | |
| 1253 | /** |
| 1254 | * xa_load() - Load an entry from an XArray. |
| 1255 | * @xa: XArray. |
| 1256 | * @index: index into array. |
| 1257 | * |
| 1258 | * Context: Any context. Takes and releases the RCU lock. |
| 1259 | * Return: The entry at @index in @xa. |
| 1260 | */ |
| 1261 | void *xa_load(struct xarray *xa, unsigned long index) |
| 1262 | { |
| 1263 | XA_STATE(xas, xa, index); |
| 1264 | void *entry; |
| 1265 | |
| 1266 | rcu_read_lock(); |
| 1267 | do { |
| 1268 | entry = xas_load(&xas); |
Matthew Wilcox | 9f14d4f | 2018-10-01 14:54:59 -0400 | [diff] [blame^] | 1269 | if (xa_is_zero(entry)) |
| 1270 | entry = NULL; |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 1271 | } while (xas_retry(&xas, entry)); |
| 1272 | rcu_read_unlock(); |
| 1273 | |
| 1274 | return entry; |
| 1275 | } |
| 1276 | EXPORT_SYMBOL(xa_load); |
| 1277 | |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 1278 | static void *xas_result(struct xa_state *xas, void *curr) |
| 1279 | { |
Matthew Wilcox | 9f14d4f | 2018-10-01 14:54:59 -0400 | [diff] [blame^] | 1280 | if (xa_is_zero(curr)) |
| 1281 | return NULL; |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 1282 | XA_NODE_BUG_ON(xas->xa_node, xa_is_internal(curr)); |
| 1283 | if (xas_error(xas)) |
| 1284 | curr = xas->xa_node; |
| 1285 | return curr; |
| 1286 | } |
| 1287 | |
| 1288 | /** |
| 1289 | * __xa_erase() - Erase this entry from the XArray while locked. |
| 1290 | * @xa: XArray. |
| 1291 | * @index: Index into array. |
| 1292 | * |
| 1293 | * If the entry at this index is a multi-index entry then all indices will |
| 1294 | * be erased, and the entry will no longer be a multi-index entry. |
| 1295 | * This function expects the xa_lock to be held on entry. |
| 1296 | * |
| 1297 | * Context: Any context. Expects xa_lock to be held on entry. May |
| 1298 | * release and reacquire xa_lock if @gfp flags permit. |
| 1299 | * Return: The old entry at this index. |
| 1300 | */ |
| 1301 | void *__xa_erase(struct xarray *xa, unsigned long index) |
| 1302 | { |
| 1303 | XA_STATE(xas, xa, index); |
| 1304 | return xas_result(&xas, xas_store(&xas, NULL)); |
| 1305 | } |
| 1306 | EXPORT_SYMBOL_GPL(__xa_erase); |
| 1307 | |
| 1308 | /** |
| 1309 | * xa_store() - Store this entry in the XArray. |
| 1310 | * @xa: XArray. |
| 1311 | * @index: Index into array. |
| 1312 | * @entry: New entry. |
| 1313 | * @gfp: Memory allocation flags. |
| 1314 | * |
| 1315 | * After this function returns, loads from this index will return @entry. |
| 1316 | * Storing into an existing multislot entry updates the entry of every index. |
| 1317 | * The marks associated with @index are unaffected unless @entry is %NULL. |
| 1318 | * |
| 1319 | * Context: Process context. Takes and releases the xa_lock. May sleep |
| 1320 | * if the @gfp flags permit. |
| 1321 | * Return: The old entry at this index on success, xa_err(-EINVAL) if @entry |
| 1322 | * cannot be stored in an XArray, or xa_err(-ENOMEM) if memory allocation |
| 1323 | * failed. |
| 1324 | */ |
| 1325 | void *xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp) |
| 1326 | { |
| 1327 | XA_STATE(xas, xa, index); |
| 1328 | void *curr; |
| 1329 | |
| 1330 | if (WARN_ON_ONCE(xa_is_internal(entry))) |
| 1331 | return XA_ERROR(-EINVAL); |
| 1332 | |
| 1333 | do { |
| 1334 | xas_lock(&xas); |
| 1335 | curr = xas_store(&xas, entry); |
| 1336 | xas_unlock(&xas); |
| 1337 | } while (xas_nomem(&xas, gfp)); |
| 1338 | |
| 1339 | return xas_result(&xas, curr); |
| 1340 | } |
| 1341 | EXPORT_SYMBOL(xa_store); |
| 1342 | |
| 1343 | /** |
| 1344 | * __xa_store() - Store this entry in the XArray. |
| 1345 | * @xa: XArray. |
| 1346 | * @index: Index into array. |
| 1347 | * @entry: New entry. |
| 1348 | * @gfp: Memory allocation flags. |
| 1349 | * |
| 1350 | * You must already be holding the xa_lock when calling this function. |
| 1351 | * It will drop the lock if needed to allocate memory, and then reacquire |
| 1352 | * it afterwards. |
| 1353 | * |
| 1354 | * Context: Any context. Expects xa_lock to be held on entry. May |
| 1355 | * release and reacquire xa_lock if @gfp flags permit. |
| 1356 | * Return: The old entry at this index or xa_err() if an error happened. |
| 1357 | */ |
| 1358 | void *__xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp) |
| 1359 | { |
| 1360 | XA_STATE(xas, xa, index); |
| 1361 | void *curr; |
| 1362 | |
| 1363 | if (WARN_ON_ONCE(xa_is_internal(entry))) |
| 1364 | return XA_ERROR(-EINVAL); |
| 1365 | |
| 1366 | do { |
| 1367 | curr = xas_store(&xas, entry); |
| 1368 | } while (__xas_nomem(&xas, gfp)); |
| 1369 | |
| 1370 | return xas_result(&xas, curr); |
| 1371 | } |
| 1372 | EXPORT_SYMBOL(__xa_store); |
| 1373 | |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 1374 | /** |
Matthew Wilcox | 41aec91 | 2017-11-10 15:34:55 -0500 | [diff] [blame] | 1375 | * xa_cmpxchg() - Conditionally replace an entry in the XArray. |
| 1376 | * @xa: XArray. |
| 1377 | * @index: Index into array. |
| 1378 | * @old: Old value to test against. |
| 1379 | * @entry: New value to place in array. |
| 1380 | * @gfp: Memory allocation flags. |
| 1381 | * |
| 1382 | * If the entry at @index is the same as @old, replace it with @entry. |
| 1383 | * If the return value is equal to @old, then the exchange was successful. |
| 1384 | * |
| 1385 | * Context: Process context. Takes and releases the xa_lock. May sleep |
| 1386 | * if the @gfp flags permit. |
| 1387 | * Return: The old value at this index or xa_err() if an error happened. |
| 1388 | */ |
| 1389 | void *xa_cmpxchg(struct xarray *xa, unsigned long index, |
| 1390 | void *old, void *entry, gfp_t gfp) |
| 1391 | { |
| 1392 | XA_STATE(xas, xa, index); |
| 1393 | void *curr; |
| 1394 | |
| 1395 | if (WARN_ON_ONCE(xa_is_internal(entry))) |
| 1396 | return XA_ERROR(-EINVAL); |
| 1397 | |
| 1398 | do { |
| 1399 | xas_lock(&xas); |
| 1400 | curr = xas_load(&xas); |
Matthew Wilcox | 9f14d4f | 2018-10-01 14:54:59 -0400 | [diff] [blame^] | 1401 | if (curr == XA_ZERO_ENTRY) |
| 1402 | curr = NULL; |
Matthew Wilcox | 41aec91 | 2017-11-10 15:34:55 -0500 | [diff] [blame] | 1403 | if (curr == old) |
| 1404 | xas_store(&xas, entry); |
| 1405 | xas_unlock(&xas); |
| 1406 | } while (xas_nomem(&xas, gfp)); |
| 1407 | |
| 1408 | return xas_result(&xas, curr); |
| 1409 | } |
| 1410 | EXPORT_SYMBOL(xa_cmpxchg); |
| 1411 | |
| 1412 | /** |
| 1413 | * __xa_cmpxchg() - Store this entry in the XArray. |
| 1414 | * @xa: XArray. |
| 1415 | * @index: Index into array. |
| 1416 | * @old: Old value to test against. |
| 1417 | * @entry: New entry. |
| 1418 | * @gfp: Memory allocation flags. |
| 1419 | * |
| 1420 | * You must already be holding the xa_lock when calling this function. |
| 1421 | * It will drop the lock if needed to allocate memory, and then reacquire |
| 1422 | * it afterwards. |
| 1423 | * |
| 1424 | * Context: Any context. Expects xa_lock to be held on entry. May |
| 1425 | * release and reacquire xa_lock if @gfp flags permit. |
| 1426 | * Return: The old entry at this index or xa_err() if an error happened. |
| 1427 | */ |
| 1428 | void *__xa_cmpxchg(struct xarray *xa, unsigned long index, |
| 1429 | void *old, void *entry, gfp_t gfp) |
| 1430 | { |
| 1431 | XA_STATE(xas, xa, index); |
| 1432 | void *curr; |
| 1433 | |
| 1434 | if (WARN_ON_ONCE(xa_is_internal(entry))) |
| 1435 | return XA_ERROR(-EINVAL); |
| 1436 | |
| 1437 | do { |
| 1438 | curr = xas_load(&xas); |
Matthew Wilcox | 9f14d4f | 2018-10-01 14:54:59 -0400 | [diff] [blame^] | 1439 | if (curr == XA_ZERO_ENTRY) |
| 1440 | curr = NULL; |
Matthew Wilcox | 41aec91 | 2017-11-10 15:34:55 -0500 | [diff] [blame] | 1441 | if (curr == old) |
| 1442 | xas_store(&xas, entry); |
| 1443 | } while (__xas_nomem(&xas, gfp)); |
| 1444 | |
| 1445 | return xas_result(&xas, curr); |
| 1446 | } |
| 1447 | EXPORT_SYMBOL(__xa_cmpxchg); |
| 1448 | |
| 1449 | /** |
Matthew Wilcox | 9f14d4f | 2018-10-01 14:54:59 -0400 | [diff] [blame^] | 1450 | * xa_reserve() - Reserve this index in the XArray. |
| 1451 | * @xa: XArray. |
| 1452 | * @index: Index into array. |
| 1453 | * @gfp: Memory allocation flags. |
| 1454 | * |
| 1455 | * Ensures there is somewhere to store an entry at @index in the array. |
| 1456 | * If there is already something stored at @index, this function does |
| 1457 | * nothing. If there was nothing there, the entry is marked as reserved. |
| 1458 | * Loads from @index will continue to see a %NULL pointer until a |
| 1459 | * subsequent store to @index. |
| 1460 | * |
| 1461 | * If you do not use the entry that you have reserved, call xa_release() |
| 1462 | * or xa_erase() to free any unnecessary memory. |
| 1463 | * |
| 1464 | * Context: Process context. Takes and releases the xa_lock, IRQ or BH safe |
| 1465 | * if specified in XArray flags. May sleep if the @gfp flags permit. |
| 1466 | * Return: 0 if the reservation succeeded or -ENOMEM if it failed. |
| 1467 | */ |
| 1468 | int xa_reserve(struct xarray *xa, unsigned long index, gfp_t gfp) |
| 1469 | { |
| 1470 | XA_STATE(xas, xa, index); |
| 1471 | unsigned int lock_type = xa_lock_type(xa); |
| 1472 | void *curr; |
| 1473 | |
| 1474 | do { |
| 1475 | xas_lock_type(&xas, lock_type); |
| 1476 | curr = xas_load(&xas); |
| 1477 | if (!curr) |
| 1478 | xas_store(&xas, XA_ZERO_ENTRY); |
| 1479 | xas_unlock_type(&xas, lock_type); |
| 1480 | } while (xas_nomem(&xas, gfp)); |
| 1481 | |
| 1482 | return xas_error(&xas); |
| 1483 | } |
| 1484 | EXPORT_SYMBOL(xa_reserve); |
| 1485 | |
| 1486 | /** |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 1487 | * __xa_set_mark() - Set this mark on this entry while locked. |
| 1488 | * @xa: XArray. |
| 1489 | * @index: Index of entry. |
| 1490 | * @mark: Mark number. |
| 1491 | * |
| 1492 | * Attempting to set a mark on a NULL entry does not succeed. |
| 1493 | * |
| 1494 | * Context: Any context. Expects xa_lock to be held on entry. |
| 1495 | */ |
| 1496 | void __xa_set_mark(struct xarray *xa, unsigned long index, xa_mark_t mark) |
| 1497 | { |
| 1498 | XA_STATE(xas, xa, index); |
| 1499 | void *entry = xas_load(&xas); |
| 1500 | |
| 1501 | if (entry) |
| 1502 | xas_set_mark(&xas, mark); |
| 1503 | } |
| 1504 | EXPORT_SYMBOL_GPL(__xa_set_mark); |
| 1505 | |
| 1506 | /** |
| 1507 | * __xa_clear_mark() - Clear this mark on this entry while locked. |
| 1508 | * @xa: XArray. |
| 1509 | * @index: Index of entry. |
| 1510 | * @mark: Mark number. |
| 1511 | * |
| 1512 | * Context: Any context. Expects xa_lock to be held on entry. |
| 1513 | */ |
| 1514 | void __xa_clear_mark(struct xarray *xa, unsigned long index, xa_mark_t mark) |
| 1515 | { |
| 1516 | XA_STATE(xas, xa, index); |
| 1517 | void *entry = xas_load(&xas); |
| 1518 | |
| 1519 | if (entry) |
| 1520 | xas_clear_mark(&xas, mark); |
| 1521 | } |
| 1522 | EXPORT_SYMBOL_GPL(__xa_clear_mark); |
| 1523 | |
| 1524 | /** |
| 1525 | * xa_get_mark() - Inquire whether this mark is set on this entry. |
| 1526 | * @xa: XArray. |
| 1527 | * @index: Index of entry. |
| 1528 | * @mark: Mark number. |
| 1529 | * |
| 1530 | * This function uses the RCU read lock, so the result may be out of date |
| 1531 | * by the time it returns. If you need the result to be stable, use a lock. |
| 1532 | * |
| 1533 | * Context: Any context. Takes and releases the RCU lock. |
| 1534 | * Return: True if the entry at @index has this mark set, false if it doesn't. |
| 1535 | */ |
| 1536 | bool xa_get_mark(struct xarray *xa, unsigned long index, xa_mark_t mark) |
| 1537 | { |
| 1538 | XA_STATE(xas, xa, index); |
| 1539 | void *entry; |
| 1540 | |
| 1541 | rcu_read_lock(); |
| 1542 | entry = xas_start(&xas); |
| 1543 | while (xas_get_mark(&xas, mark)) { |
| 1544 | if (!xa_is_node(entry)) |
| 1545 | goto found; |
| 1546 | entry = xas_descend(&xas, xa_to_node(entry)); |
| 1547 | } |
| 1548 | rcu_read_unlock(); |
| 1549 | return false; |
| 1550 | found: |
| 1551 | rcu_read_unlock(); |
| 1552 | return true; |
| 1553 | } |
| 1554 | EXPORT_SYMBOL(xa_get_mark); |
| 1555 | |
| 1556 | /** |
| 1557 | * xa_set_mark() - Set this mark on this entry. |
| 1558 | * @xa: XArray. |
| 1559 | * @index: Index of entry. |
| 1560 | * @mark: Mark number. |
| 1561 | * |
| 1562 | * Attempting to set a mark on a NULL entry does not succeed. |
| 1563 | * |
| 1564 | * Context: Process context. Takes and releases the xa_lock. |
| 1565 | */ |
| 1566 | void xa_set_mark(struct xarray *xa, unsigned long index, xa_mark_t mark) |
| 1567 | { |
| 1568 | xa_lock(xa); |
| 1569 | __xa_set_mark(xa, index, mark); |
| 1570 | xa_unlock(xa); |
| 1571 | } |
| 1572 | EXPORT_SYMBOL(xa_set_mark); |
| 1573 | |
| 1574 | /** |
| 1575 | * xa_clear_mark() - Clear this mark on this entry. |
| 1576 | * @xa: XArray. |
| 1577 | * @index: Index of entry. |
| 1578 | * @mark: Mark number. |
| 1579 | * |
| 1580 | * Clearing a mark always succeeds. |
| 1581 | * |
| 1582 | * Context: Process context. Takes and releases the xa_lock. |
| 1583 | */ |
| 1584 | void xa_clear_mark(struct xarray *xa, unsigned long index, xa_mark_t mark) |
| 1585 | { |
| 1586 | xa_lock(xa); |
| 1587 | __xa_clear_mark(xa, index, mark); |
| 1588 | xa_unlock(xa); |
| 1589 | } |
| 1590 | EXPORT_SYMBOL(xa_clear_mark); |
| 1591 | |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 1592 | /** |
| 1593 | * xa_find() - Search the XArray for an entry. |
| 1594 | * @xa: XArray. |
| 1595 | * @indexp: Pointer to an index. |
| 1596 | * @max: Maximum index to search to. |
| 1597 | * @filter: Selection criterion. |
| 1598 | * |
| 1599 | * Finds the entry in @xa which matches the @filter, and has the lowest |
| 1600 | * index that is at least @indexp and no more than @max. |
| 1601 | * If an entry is found, @indexp is updated to be the index of the entry. |
| 1602 | * This function is protected by the RCU read lock, so it may not find |
| 1603 | * entries which are being simultaneously added. It will not return an |
| 1604 | * %XA_RETRY_ENTRY; if you need to see retry entries, use xas_find(). |
| 1605 | * |
| 1606 | * Context: Any context. Takes and releases the RCU lock. |
| 1607 | * Return: The entry, if found, otherwise %NULL. |
| 1608 | */ |
| 1609 | void *xa_find(struct xarray *xa, unsigned long *indexp, |
| 1610 | unsigned long max, xa_mark_t filter) |
| 1611 | { |
| 1612 | XA_STATE(xas, xa, *indexp); |
| 1613 | void *entry; |
| 1614 | |
| 1615 | rcu_read_lock(); |
| 1616 | do { |
| 1617 | if ((__force unsigned int)filter < XA_MAX_MARKS) |
| 1618 | entry = xas_find_marked(&xas, max, filter); |
| 1619 | else |
| 1620 | entry = xas_find(&xas, max); |
| 1621 | } while (xas_retry(&xas, entry)); |
| 1622 | rcu_read_unlock(); |
| 1623 | |
| 1624 | if (entry) |
| 1625 | *indexp = xas.xa_index; |
| 1626 | return entry; |
| 1627 | } |
| 1628 | EXPORT_SYMBOL(xa_find); |
| 1629 | |
| 1630 | /** |
| 1631 | * xa_find_after() - Search the XArray for a present entry. |
| 1632 | * @xa: XArray. |
| 1633 | * @indexp: Pointer to an index. |
| 1634 | * @max: Maximum index to search to. |
| 1635 | * @filter: Selection criterion. |
| 1636 | * |
| 1637 | * Finds the entry in @xa which matches the @filter and has the lowest |
| 1638 | * index that is above @indexp and no more than @max. |
| 1639 | * If an entry is found, @indexp is updated to be the index of the entry. |
| 1640 | * This function is protected by the RCU read lock, so it may miss entries |
| 1641 | * which are being simultaneously added. It will not return an |
| 1642 | * %XA_RETRY_ENTRY; if you need to see retry entries, use xas_find(). |
| 1643 | * |
| 1644 | * Context: Any context. Takes and releases the RCU lock. |
| 1645 | * Return: The pointer, if found, otherwise %NULL. |
| 1646 | */ |
| 1647 | void *xa_find_after(struct xarray *xa, unsigned long *indexp, |
| 1648 | unsigned long max, xa_mark_t filter) |
| 1649 | { |
| 1650 | XA_STATE(xas, xa, *indexp + 1); |
| 1651 | void *entry; |
| 1652 | |
| 1653 | rcu_read_lock(); |
| 1654 | for (;;) { |
| 1655 | if ((__force unsigned int)filter < XA_MAX_MARKS) |
| 1656 | entry = xas_find_marked(&xas, max, filter); |
| 1657 | else |
| 1658 | entry = xas_find(&xas, max); |
| 1659 | if (xas.xa_shift) { |
| 1660 | if (xas.xa_index & ((1UL << xas.xa_shift) - 1)) |
| 1661 | continue; |
| 1662 | } else { |
| 1663 | if (xas.xa_offset < (xas.xa_index & XA_CHUNK_MASK)) |
| 1664 | continue; |
| 1665 | } |
| 1666 | if (!xas_retry(&xas, entry)) |
| 1667 | break; |
| 1668 | } |
| 1669 | rcu_read_unlock(); |
| 1670 | |
| 1671 | if (entry) |
| 1672 | *indexp = xas.xa_index; |
| 1673 | return entry; |
| 1674 | } |
| 1675 | EXPORT_SYMBOL(xa_find_after); |
| 1676 | |
Matthew Wilcox | 80a0a1a | 2017-11-14 16:42:22 -0500 | [diff] [blame] | 1677 | static unsigned int xas_extract_present(struct xa_state *xas, void **dst, |
| 1678 | unsigned long max, unsigned int n) |
| 1679 | { |
| 1680 | void *entry; |
| 1681 | unsigned int i = 0; |
| 1682 | |
| 1683 | rcu_read_lock(); |
| 1684 | xas_for_each(xas, entry, max) { |
| 1685 | if (xas_retry(xas, entry)) |
| 1686 | continue; |
| 1687 | dst[i++] = entry; |
| 1688 | if (i == n) |
| 1689 | break; |
| 1690 | } |
| 1691 | rcu_read_unlock(); |
| 1692 | |
| 1693 | return i; |
| 1694 | } |
| 1695 | |
| 1696 | static unsigned int xas_extract_marked(struct xa_state *xas, void **dst, |
| 1697 | unsigned long max, unsigned int n, xa_mark_t mark) |
| 1698 | { |
| 1699 | void *entry; |
| 1700 | unsigned int i = 0; |
| 1701 | |
| 1702 | rcu_read_lock(); |
| 1703 | xas_for_each_marked(xas, entry, max, mark) { |
| 1704 | if (xas_retry(xas, entry)) |
| 1705 | continue; |
| 1706 | dst[i++] = entry; |
| 1707 | if (i == n) |
| 1708 | break; |
| 1709 | } |
| 1710 | rcu_read_unlock(); |
| 1711 | |
| 1712 | return i; |
| 1713 | } |
| 1714 | |
| 1715 | /** |
| 1716 | * xa_extract() - Copy selected entries from the XArray into a normal array. |
| 1717 | * @xa: The source XArray to copy from. |
| 1718 | * @dst: The buffer to copy entries into. |
| 1719 | * @start: The first index in the XArray eligible to be selected. |
| 1720 | * @max: The last index in the XArray eligible to be selected. |
| 1721 | * @n: The maximum number of entries to copy. |
| 1722 | * @filter: Selection criterion. |
| 1723 | * |
| 1724 | * Copies up to @n entries that match @filter from the XArray. The |
| 1725 | * copied entries will have indices between @start and @max, inclusive. |
| 1726 | * |
| 1727 | * The @filter may be an XArray mark value, in which case entries which are |
| 1728 | * marked with that mark will be copied. It may also be %XA_PRESENT, in |
| 1729 | * which case all entries which are not NULL will be copied. |
| 1730 | * |
| 1731 | * The entries returned may not represent a snapshot of the XArray at a |
| 1732 | * moment in time. For example, if another thread stores to index 5, then |
| 1733 | * index 10, calling xa_extract() may return the old contents of index 5 |
| 1734 | * and the new contents of index 10. Indices not modified while this |
| 1735 | * function is running will not be skipped. |
| 1736 | * |
| 1737 | * If you need stronger guarantees, holding the xa_lock across calls to this |
| 1738 | * function will prevent concurrent modification. |
| 1739 | * |
| 1740 | * Context: Any context. Takes and releases the RCU lock. |
| 1741 | * Return: The number of entries copied. |
| 1742 | */ |
| 1743 | unsigned int xa_extract(struct xarray *xa, void **dst, unsigned long start, |
| 1744 | unsigned long max, unsigned int n, xa_mark_t filter) |
| 1745 | { |
| 1746 | XA_STATE(xas, xa, start); |
| 1747 | |
| 1748 | if (!n) |
| 1749 | return 0; |
| 1750 | |
| 1751 | if ((__force unsigned int)filter < XA_MAX_MARKS) |
| 1752 | return xas_extract_marked(&xas, dst, max, n, filter); |
| 1753 | return xas_extract_present(&xas, dst, max, n); |
| 1754 | } |
| 1755 | EXPORT_SYMBOL(xa_extract); |
| 1756 | |
Matthew Wilcox | 687149f | 2017-11-17 08:16:34 -0500 | [diff] [blame] | 1757 | /** |
| 1758 | * xa_destroy() - Free all internal data structures. |
| 1759 | * @xa: XArray. |
| 1760 | * |
| 1761 | * After calling this function, the XArray is empty and has freed all memory |
| 1762 | * allocated for its internal data structures. You are responsible for |
| 1763 | * freeing the objects referenced by the XArray. |
| 1764 | * |
| 1765 | * Context: Any context. Takes and releases the xa_lock, interrupt-safe. |
| 1766 | */ |
| 1767 | void xa_destroy(struct xarray *xa) |
| 1768 | { |
| 1769 | XA_STATE(xas, xa, 0); |
| 1770 | unsigned long flags; |
| 1771 | void *entry; |
| 1772 | |
| 1773 | xas.xa_node = NULL; |
| 1774 | xas_lock_irqsave(&xas, flags); |
| 1775 | entry = xa_head_locked(xa); |
| 1776 | RCU_INIT_POINTER(xa->xa_head, NULL); |
| 1777 | xas_init_marks(&xas); |
| 1778 | /* lockdep checks we're still holding the lock in xas_free_nodes() */ |
| 1779 | if (xa_is_node(entry)) |
| 1780 | xas_free_nodes(&xas, xa_to_node(entry)); |
| 1781 | xas_unlock_irqrestore(&xas, flags); |
| 1782 | } |
| 1783 | EXPORT_SYMBOL(xa_destroy); |
| 1784 | |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 1785 | #ifdef XA_DEBUG |
| 1786 | void xa_dump_node(const struct xa_node *node) |
| 1787 | { |
| 1788 | unsigned i, j; |
| 1789 | |
| 1790 | if (!node) |
| 1791 | return; |
| 1792 | if ((unsigned long)node & 3) { |
| 1793 | pr_cont("node %px\n", node); |
| 1794 | return; |
| 1795 | } |
| 1796 | |
| 1797 | pr_cont("node %px %s %d parent %px shift %d count %d values %d " |
| 1798 | "array %px list %px %px marks", |
| 1799 | node, node->parent ? "offset" : "max", node->offset, |
| 1800 | node->parent, node->shift, node->count, node->nr_values, |
| 1801 | node->array, node->private_list.prev, node->private_list.next); |
| 1802 | for (i = 0; i < XA_MAX_MARKS; i++) |
| 1803 | for (j = 0; j < XA_MARK_LONGS; j++) |
| 1804 | pr_cont(" %lx", node->marks[i][j]); |
| 1805 | pr_cont("\n"); |
| 1806 | } |
| 1807 | |
| 1808 | void xa_dump_index(unsigned long index, unsigned int shift) |
| 1809 | { |
| 1810 | if (!shift) |
| 1811 | pr_info("%lu: ", index); |
| 1812 | else if (shift >= BITS_PER_LONG) |
| 1813 | pr_info("0-%lu: ", ~0UL); |
| 1814 | else |
| 1815 | pr_info("%lu-%lu: ", index, index | ((1UL << shift) - 1)); |
| 1816 | } |
| 1817 | |
| 1818 | void xa_dump_entry(const void *entry, unsigned long index, unsigned long shift) |
| 1819 | { |
| 1820 | if (!entry) |
| 1821 | return; |
| 1822 | |
| 1823 | xa_dump_index(index, shift); |
| 1824 | |
| 1825 | if (xa_is_node(entry)) { |
| 1826 | if (shift == 0) { |
| 1827 | pr_cont("%px\n", entry); |
| 1828 | } else { |
| 1829 | unsigned long i; |
| 1830 | struct xa_node *node = xa_to_node(entry); |
| 1831 | xa_dump_node(node); |
| 1832 | for (i = 0; i < XA_CHUNK_SIZE; i++) |
| 1833 | xa_dump_entry(node->slots[i], |
| 1834 | index + (i << node->shift), node->shift); |
| 1835 | } |
| 1836 | } else if (xa_is_value(entry)) |
| 1837 | pr_cont("value %ld (0x%lx) [%px]\n", xa_to_value(entry), |
| 1838 | xa_to_value(entry), entry); |
| 1839 | else if (!xa_is_internal(entry)) |
| 1840 | pr_cont("%px\n", entry); |
| 1841 | else if (xa_is_retry(entry)) |
| 1842 | pr_cont("retry (%ld)\n", xa_to_internal(entry)); |
| 1843 | else if (xa_is_sibling(entry)) |
| 1844 | pr_cont("sibling (slot %ld)\n", xa_to_sibling(entry)); |
Matthew Wilcox | 9f14d4f | 2018-10-01 14:54:59 -0400 | [diff] [blame^] | 1845 | else if (xa_is_zero(entry)) |
| 1846 | pr_cont("zero (%ld)\n", xa_to_internal(entry)); |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 1847 | else |
| 1848 | pr_cont("UNKNOWN ENTRY (%px)\n", entry); |
| 1849 | } |
| 1850 | |
| 1851 | void xa_dump(const struct xarray *xa) |
| 1852 | { |
| 1853 | void *entry = xa->xa_head; |
| 1854 | unsigned int shift = 0; |
| 1855 | |
| 1856 | pr_info("xarray: %px head %px flags %x marks %d %d %d\n", xa, entry, |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 1857 | xa->xa_flags, xa_marked(xa, XA_MARK_0), |
| 1858 | xa_marked(xa, XA_MARK_1), xa_marked(xa, XA_MARK_2)); |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 1859 | if (xa_is_node(entry)) |
| 1860 | shift = xa_to_node(entry)->shift + XA_CHUNK_SHIFT; |
| 1861 | xa_dump_entry(entry, 0, shift); |
| 1862 | } |
| 1863 | #endif |