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 | |
| 131 | /* move the index either forwards (find) or backwards (sibling slot) */ |
| 132 | static void xas_move_index(struct xa_state *xas, unsigned long offset) |
| 133 | { |
| 134 | unsigned int shift = xas->xa_node->shift; |
| 135 | xas->xa_index &= ~XA_CHUNK_MASK << shift; |
| 136 | xas->xa_index += offset << shift; |
| 137 | } |
| 138 | |
| 139 | static void *set_bounds(struct xa_state *xas) |
| 140 | { |
| 141 | xas->xa_node = XAS_BOUNDS; |
| 142 | return NULL; |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Starts a walk. If the @xas is already valid, we assume that it's on |
| 147 | * the right path and just return where we've got to. If we're in an |
| 148 | * error state, return NULL. If the index is outside the current scope |
| 149 | * of the xarray, return NULL without changing @xas->xa_node. Otherwise |
| 150 | * set @xas->xa_node to NULL and return the current head of the array. |
| 151 | */ |
| 152 | static void *xas_start(struct xa_state *xas) |
| 153 | { |
| 154 | void *entry; |
| 155 | |
| 156 | if (xas_valid(xas)) |
| 157 | return xas_reload(xas); |
| 158 | if (xas_error(xas)) |
| 159 | return NULL; |
| 160 | |
| 161 | entry = xa_head(xas->xa); |
| 162 | if (!xa_is_node(entry)) { |
| 163 | if (xas->xa_index) |
| 164 | return set_bounds(xas); |
| 165 | } else { |
| 166 | if ((xas->xa_index >> xa_to_node(entry)->shift) > XA_CHUNK_MASK) |
| 167 | return set_bounds(xas); |
| 168 | } |
| 169 | |
| 170 | xas->xa_node = NULL; |
| 171 | return entry; |
| 172 | } |
| 173 | |
| 174 | static void *xas_descend(struct xa_state *xas, struct xa_node *node) |
| 175 | { |
| 176 | unsigned int offset = get_offset(xas->xa_index, node); |
| 177 | void *entry = xa_entry(xas->xa, node, offset); |
| 178 | |
| 179 | xas->xa_node = node; |
| 180 | if (xa_is_sibling(entry)) { |
| 181 | offset = xa_to_sibling(entry); |
| 182 | entry = xa_entry(xas->xa, node, offset); |
| 183 | } |
| 184 | |
| 185 | xas->xa_offset = offset; |
| 186 | return entry; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * xas_load() - Load an entry from the XArray (advanced). |
| 191 | * @xas: XArray operation state. |
| 192 | * |
| 193 | * Usually walks the @xas to the appropriate state to load the entry |
| 194 | * stored at xa_index. However, it will do nothing and return %NULL if |
| 195 | * @xas is in an error state. xas_load() will never expand the tree. |
| 196 | * |
| 197 | * If the xa_state is set up to operate on a multi-index entry, xas_load() |
| 198 | * may return %NULL or an internal entry, even if there are entries |
| 199 | * present within the range specified by @xas. |
| 200 | * |
| 201 | * Context: Any context. The caller should hold the xa_lock or the RCU lock. |
| 202 | * Return: Usually an entry in the XArray, but see description for exceptions. |
| 203 | */ |
| 204 | void *xas_load(struct xa_state *xas) |
| 205 | { |
| 206 | void *entry = xas_start(xas); |
| 207 | |
| 208 | while (xa_is_node(entry)) { |
| 209 | struct xa_node *node = xa_to_node(entry); |
| 210 | |
| 211 | if (xas->xa_shift > node->shift) |
| 212 | break; |
| 213 | entry = xas_descend(xas, node); |
| 214 | } |
| 215 | return entry; |
| 216 | } |
| 217 | EXPORT_SYMBOL_GPL(xas_load); |
| 218 | |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame^] | 219 | /* Move the radix tree node cache here */ |
| 220 | extern struct kmem_cache *radix_tree_node_cachep; |
| 221 | extern void radix_tree_node_rcu_free(struct rcu_head *head); |
| 222 | |
| 223 | #define XA_RCU_FREE ((struct xarray *)1) |
| 224 | |
| 225 | static void xa_node_free(struct xa_node *node) |
| 226 | { |
| 227 | XA_NODE_BUG_ON(node, !list_empty(&node->private_list)); |
| 228 | node->array = XA_RCU_FREE; |
| 229 | call_rcu(&node->rcu_head, radix_tree_node_rcu_free); |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * xas_destroy() - Free any resources allocated during the XArray operation. |
| 234 | * @xas: XArray operation state. |
| 235 | * |
| 236 | * This function is now internal-only. |
| 237 | */ |
| 238 | static void xas_destroy(struct xa_state *xas) |
| 239 | { |
| 240 | struct xa_node *node = xas->xa_alloc; |
| 241 | |
| 242 | if (!node) |
| 243 | return; |
| 244 | XA_NODE_BUG_ON(node, !list_empty(&node->private_list)); |
| 245 | kmem_cache_free(radix_tree_node_cachep, node); |
| 246 | xas->xa_alloc = NULL; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * xas_nomem() - Allocate memory if needed. |
| 251 | * @xas: XArray operation state. |
| 252 | * @gfp: Memory allocation flags. |
| 253 | * |
| 254 | * If we need to add new nodes to the XArray, we try to allocate memory |
| 255 | * with GFP_NOWAIT while holding the lock, which will usually succeed. |
| 256 | * If it fails, @xas is flagged as needing memory to continue. The caller |
| 257 | * should drop the lock and call xas_nomem(). If xas_nomem() succeeds, |
| 258 | * the caller should retry the operation. |
| 259 | * |
| 260 | * Forward progress is guaranteed as one node is allocated here and |
| 261 | * stored in the xa_state where it will be found by xas_alloc(). More |
| 262 | * nodes will likely be found in the slab allocator, but we do not tie |
| 263 | * them up here. |
| 264 | * |
| 265 | * Return: true if memory was needed, and was successfully allocated. |
| 266 | */ |
| 267 | bool xas_nomem(struct xa_state *xas, gfp_t gfp) |
| 268 | { |
| 269 | if (xas->xa_node != XA_ERROR(-ENOMEM)) { |
| 270 | xas_destroy(xas); |
| 271 | return false; |
| 272 | } |
| 273 | xas->xa_alloc = kmem_cache_alloc(radix_tree_node_cachep, gfp); |
| 274 | if (!xas->xa_alloc) |
| 275 | return false; |
| 276 | XA_NODE_BUG_ON(xas->xa_alloc, !list_empty(&xas->xa_alloc->private_list)); |
| 277 | xas->xa_node = XAS_RESTART; |
| 278 | return true; |
| 279 | } |
| 280 | EXPORT_SYMBOL_GPL(xas_nomem); |
| 281 | |
| 282 | /* |
| 283 | * __xas_nomem() - Drop locks and allocate memory if needed. |
| 284 | * @xas: XArray operation state. |
| 285 | * @gfp: Memory allocation flags. |
| 286 | * |
| 287 | * Internal variant of xas_nomem(). |
| 288 | * |
| 289 | * Return: true if memory was needed, and was successfully allocated. |
| 290 | */ |
| 291 | static bool __xas_nomem(struct xa_state *xas, gfp_t gfp) |
| 292 | __must_hold(xas->xa->xa_lock) |
| 293 | { |
| 294 | unsigned int lock_type = xa_lock_type(xas->xa); |
| 295 | |
| 296 | if (xas->xa_node != XA_ERROR(-ENOMEM)) { |
| 297 | xas_destroy(xas); |
| 298 | return false; |
| 299 | } |
| 300 | if (gfpflags_allow_blocking(gfp)) { |
| 301 | xas_unlock_type(xas, lock_type); |
| 302 | xas->xa_alloc = kmem_cache_alloc(radix_tree_node_cachep, gfp); |
| 303 | xas_lock_type(xas, lock_type); |
| 304 | } else { |
| 305 | xas->xa_alloc = kmem_cache_alloc(radix_tree_node_cachep, gfp); |
| 306 | } |
| 307 | if (!xas->xa_alloc) |
| 308 | return false; |
| 309 | XA_NODE_BUG_ON(xas->xa_alloc, !list_empty(&xas->xa_alloc->private_list)); |
| 310 | xas->xa_node = XAS_RESTART; |
| 311 | return true; |
| 312 | } |
| 313 | |
| 314 | static void xas_update(struct xa_state *xas, struct xa_node *node) |
| 315 | { |
| 316 | if (xas->xa_update) |
| 317 | xas->xa_update(node); |
| 318 | else |
| 319 | XA_NODE_BUG_ON(node, !list_empty(&node->private_list)); |
| 320 | } |
| 321 | |
| 322 | static void *xas_alloc(struct xa_state *xas, unsigned int shift) |
| 323 | { |
| 324 | struct xa_node *parent = xas->xa_node; |
| 325 | struct xa_node *node = xas->xa_alloc; |
| 326 | |
| 327 | if (xas_invalid(xas)) |
| 328 | return NULL; |
| 329 | |
| 330 | if (node) { |
| 331 | xas->xa_alloc = NULL; |
| 332 | } else { |
| 333 | node = kmem_cache_alloc(radix_tree_node_cachep, |
| 334 | GFP_NOWAIT | __GFP_NOWARN); |
| 335 | if (!node) { |
| 336 | xas_set_err(xas, -ENOMEM); |
| 337 | return NULL; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | if (parent) { |
| 342 | node->offset = xas->xa_offset; |
| 343 | parent->count++; |
| 344 | XA_NODE_BUG_ON(node, parent->count > XA_CHUNK_SIZE); |
| 345 | xas_update(xas, parent); |
| 346 | } |
| 347 | XA_NODE_BUG_ON(node, shift > BITS_PER_LONG); |
| 348 | XA_NODE_BUG_ON(node, !list_empty(&node->private_list)); |
| 349 | node->shift = shift; |
| 350 | node->count = 0; |
| 351 | node->nr_values = 0; |
| 352 | RCU_INIT_POINTER(node->parent, xas->xa_node); |
| 353 | node->array = xas->xa; |
| 354 | |
| 355 | return node; |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | * Use this to calculate the maximum index that will need to be created |
| 360 | * in order to add the entry described by @xas. Because we cannot store a |
| 361 | * multiple-index entry at index 0, the calculation is a little more complex |
| 362 | * than you might expect. |
| 363 | */ |
| 364 | static unsigned long xas_max(struct xa_state *xas) |
| 365 | { |
| 366 | unsigned long max = xas->xa_index; |
| 367 | |
| 368 | #ifdef CONFIG_XARRAY_MULTI |
| 369 | if (xas->xa_shift || xas->xa_sibs) { |
| 370 | unsigned long mask; |
| 371 | mask = (((xas->xa_sibs + 1UL) << xas->xa_shift) - 1); |
| 372 | max |= mask; |
| 373 | if (mask == max) |
| 374 | max++; |
| 375 | } |
| 376 | #endif |
| 377 | |
| 378 | return max; |
| 379 | } |
| 380 | |
| 381 | /* The maximum index that can be contained in the array without expanding it */ |
| 382 | static unsigned long max_index(void *entry) |
| 383 | { |
| 384 | if (!xa_is_node(entry)) |
| 385 | return 0; |
| 386 | return (XA_CHUNK_SIZE << xa_to_node(entry)->shift) - 1; |
| 387 | } |
| 388 | |
| 389 | static void xas_shrink(struct xa_state *xas) |
| 390 | { |
| 391 | struct xarray *xa = xas->xa; |
| 392 | struct xa_node *node = xas->xa_node; |
| 393 | |
| 394 | for (;;) { |
| 395 | void *entry; |
| 396 | |
| 397 | XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE); |
| 398 | if (node->count != 1) |
| 399 | break; |
| 400 | entry = xa_entry_locked(xa, node, 0); |
| 401 | if (!entry) |
| 402 | break; |
| 403 | if (!xa_is_node(entry) && node->shift) |
| 404 | break; |
| 405 | xas->xa_node = XAS_BOUNDS; |
| 406 | |
| 407 | RCU_INIT_POINTER(xa->xa_head, entry); |
| 408 | |
| 409 | node->count = 0; |
| 410 | node->nr_values = 0; |
| 411 | if (!xa_is_node(entry)) |
| 412 | RCU_INIT_POINTER(node->slots[0], XA_RETRY_ENTRY); |
| 413 | xas_update(xas, node); |
| 414 | xa_node_free(node); |
| 415 | if (!xa_is_node(entry)) |
| 416 | break; |
| 417 | node = xa_to_node(entry); |
| 418 | node->parent = NULL; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | /* |
| 423 | * xas_delete_node() - Attempt to delete an xa_node |
| 424 | * @xas: Array operation state. |
| 425 | * |
| 426 | * Attempts to delete the @xas->xa_node. This will fail if xa->node has |
| 427 | * a non-zero reference count. |
| 428 | */ |
| 429 | static void xas_delete_node(struct xa_state *xas) |
| 430 | { |
| 431 | struct xa_node *node = xas->xa_node; |
| 432 | |
| 433 | for (;;) { |
| 434 | struct xa_node *parent; |
| 435 | |
| 436 | XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE); |
| 437 | if (node->count) |
| 438 | break; |
| 439 | |
| 440 | parent = xa_parent_locked(xas->xa, node); |
| 441 | xas->xa_node = parent; |
| 442 | xas->xa_offset = node->offset; |
| 443 | xa_node_free(node); |
| 444 | |
| 445 | if (!parent) { |
| 446 | xas->xa->xa_head = NULL; |
| 447 | xas->xa_node = XAS_BOUNDS; |
| 448 | return; |
| 449 | } |
| 450 | |
| 451 | parent->slots[xas->xa_offset] = NULL; |
| 452 | parent->count--; |
| 453 | XA_NODE_BUG_ON(parent, parent->count > XA_CHUNK_SIZE); |
| 454 | node = parent; |
| 455 | xas_update(xas, node); |
| 456 | } |
| 457 | |
| 458 | if (!node->parent) |
| 459 | xas_shrink(xas); |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * xas_free_nodes() - Free this node and all nodes that it references |
| 464 | * @xas: Array operation state. |
| 465 | * @top: Node to free |
| 466 | * |
| 467 | * This node has been removed from the tree. We must now free it and all |
| 468 | * of its subnodes. There may be RCU walkers with references into the tree, |
| 469 | * so we must replace all entries with retry markers. |
| 470 | */ |
| 471 | static void xas_free_nodes(struct xa_state *xas, struct xa_node *top) |
| 472 | { |
| 473 | unsigned int offset = 0; |
| 474 | struct xa_node *node = top; |
| 475 | |
| 476 | for (;;) { |
| 477 | void *entry = xa_entry_locked(xas->xa, node, offset); |
| 478 | |
| 479 | if (xa_is_node(entry)) { |
| 480 | node = xa_to_node(entry); |
| 481 | offset = 0; |
| 482 | continue; |
| 483 | } |
| 484 | if (entry) |
| 485 | RCU_INIT_POINTER(node->slots[offset], XA_RETRY_ENTRY); |
| 486 | offset++; |
| 487 | while (offset == XA_CHUNK_SIZE) { |
| 488 | struct xa_node *parent; |
| 489 | |
| 490 | parent = xa_parent_locked(xas->xa, node); |
| 491 | offset = node->offset + 1; |
| 492 | node->count = 0; |
| 493 | node->nr_values = 0; |
| 494 | xas_update(xas, node); |
| 495 | xa_node_free(node); |
| 496 | if (node == top) |
| 497 | return; |
| 498 | node = parent; |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | /* |
| 504 | * xas_expand adds nodes to the head of the tree until it has reached |
| 505 | * sufficient height to be able to contain @xas->xa_index |
| 506 | */ |
| 507 | static int xas_expand(struct xa_state *xas, void *head) |
| 508 | { |
| 509 | struct xarray *xa = xas->xa; |
| 510 | struct xa_node *node = NULL; |
| 511 | unsigned int shift = 0; |
| 512 | unsigned long max = xas_max(xas); |
| 513 | |
| 514 | if (!head) { |
| 515 | if (max == 0) |
| 516 | return 0; |
| 517 | while ((max >> shift) >= XA_CHUNK_SIZE) |
| 518 | shift += XA_CHUNK_SHIFT; |
| 519 | return shift + XA_CHUNK_SHIFT; |
| 520 | } else if (xa_is_node(head)) { |
| 521 | node = xa_to_node(head); |
| 522 | shift = node->shift + XA_CHUNK_SHIFT; |
| 523 | } |
| 524 | xas->xa_node = NULL; |
| 525 | |
| 526 | while (max > max_index(head)) { |
| 527 | xa_mark_t mark = 0; |
| 528 | |
| 529 | XA_NODE_BUG_ON(node, shift > BITS_PER_LONG); |
| 530 | node = xas_alloc(xas, shift); |
| 531 | if (!node) |
| 532 | return -ENOMEM; |
| 533 | |
| 534 | node->count = 1; |
| 535 | if (xa_is_value(head)) |
| 536 | node->nr_values = 1; |
| 537 | RCU_INIT_POINTER(node->slots[0], head); |
| 538 | |
| 539 | /* Propagate the aggregated mark info to the new child */ |
| 540 | for (;;) { |
| 541 | if (xa_marked(xa, mark)) |
| 542 | node_set_mark(node, 0, mark); |
| 543 | if (mark == XA_MARK_MAX) |
| 544 | break; |
| 545 | mark_inc(mark); |
| 546 | } |
| 547 | |
| 548 | /* |
| 549 | * Now that the new node is fully initialised, we can add |
| 550 | * it to the tree |
| 551 | */ |
| 552 | if (xa_is_node(head)) { |
| 553 | xa_to_node(head)->offset = 0; |
| 554 | rcu_assign_pointer(xa_to_node(head)->parent, node); |
| 555 | } |
| 556 | head = xa_mk_node(node); |
| 557 | rcu_assign_pointer(xa->xa_head, head); |
| 558 | xas_update(xas, node); |
| 559 | |
| 560 | shift += XA_CHUNK_SHIFT; |
| 561 | } |
| 562 | |
| 563 | xas->xa_node = node; |
| 564 | return shift; |
| 565 | } |
| 566 | |
| 567 | /* |
| 568 | * xas_create() - Create a slot to store an entry in. |
| 569 | * @xas: XArray operation state. |
| 570 | * |
| 571 | * Most users will not need to call this function directly, as it is called |
| 572 | * by xas_store(). It is useful for doing conditional store operations |
| 573 | * (see the xa_cmpxchg() implementation for an example). |
| 574 | * |
| 575 | * Return: If the slot already existed, returns the contents of this slot. |
| 576 | * If the slot was newly created, returns NULL. If it failed to create the |
| 577 | * slot, returns NULL and indicates the error in @xas. |
| 578 | */ |
| 579 | static void *xas_create(struct xa_state *xas) |
| 580 | { |
| 581 | struct xarray *xa = xas->xa; |
| 582 | void *entry; |
| 583 | void __rcu **slot; |
| 584 | struct xa_node *node = xas->xa_node; |
| 585 | int shift; |
| 586 | unsigned int order = xas->xa_shift; |
| 587 | |
| 588 | if (xas_top(node)) { |
| 589 | entry = xa_head_locked(xa); |
| 590 | xas->xa_node = NULL; |
| 591 | shift = xas_expand(xas, entry); |
| 592 | if (shift < 0) |
| 593 | return NULL; |
| 594 | entry = xa_head_locked(xa); |
| 595 | slot = &xa->xa_head; |
| 596 | } else if (xas_error(xas)) { |
| 597 | return NULL; |
| 598 | } else if (node) { |
| 599 | unsigned int offset = xas->xa_offset; |
| 600 | |
| 601 | shift = node->shift; |
| 602 | entry = xa_entry_locked(xa, node, offset); |
| 603 | slot = &node->slots[offset]; |
| 604 | } else { |
| 605 | shift = 0; |
| 606 | entry = xa_head_locked(xa); |
| 607 | slot = &xa->xa_head; |
| 608 | } |
| 609 | |
| 610 | while (shift > order) { |
| 611 | shift -= XA_CHUNK_SHIFT; |
| 612 | if (!entry) { |
| 613 | node = xas_alloc(xas, shift); |
| 614 | if (!node) |
| 615 | break; |
| 616 | rcu_assign_pointer(*slot, xa_mk_node(node)); |
| 617 | } else if (xa_is_node(entry)) { |
| 618 | node = xa_to_node(entry); |
| 619 | } else { |
| 620 | break; |
| 621 | } |
| 622 | entry = xas_descend(xas, node); |
| 623 | slot = &node->slots[xas->xa_offset]; |
| 624 | } |
| 625 | |
| 626 | return entry; |
| 627 | } |
| 628 | |
| 629 | static void update_node(struct xa_state *xas, struct xa_node *node, |
| 630 | int count, int values) |
| 631 | { |
| 632 | if (!node || (!count && !values)) |
| 633 | return; |
| 634 | |
| 635 | node->count += count; |
| 636 | node->nr_values += values; |
| 637 | XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE); |
| 638 | XA_NODE_BUG_ON(node, node->nr_values > XA_CHUNK_SIZE); |
| 639 | xas_update(xas, node); |
| 640 | if (count < 0) |
| 641 | xas_delete_node(xas); |
| 642 | } |
| 643 | |
| 644 | /** |
| 645 | * xas_store() - Store this entry in the XArray. |
| 646 | * @xas: XArray operation state. |
| 647 | * @entry: New entry. |
| 648 | * |
| 649 | * If @xas is operating on a multi-index entry, the entry returned by this |
| 650 | * function is essentially meaningless (it may be an internal entry or it |
| 651 | * may be %NULL, even if there are non-NULL entries at some of the indices |
| 652 | * covered by the range). This is not a problem for any current users, |
| 653 | * and can be changed if needed. |
| 654 | * |
| 655 | * Return: The old entry at this index. |
| 656 | */ |
| 657 | void *xas_store(struct xa_state *xas, void *entry) |
| 658 | { |
| 659 | struct xa_node *node; |
| 660 | void __rcu **slot = &xas->xa->xa_head; |
| 661 | unsigned int offset, max; |
| 662 | int count = 0; |
| 663 | int values = 0; |
| 664 | void *first, *next; |
| 665 | bool value = xa_is_value(entry); |
| 666 | |
| 667 | if (entry) |
| 668 | first = xas_create(xas); |
| 669 | else |
| 670 | first = xas_load(xas); |
| 671 | |
| 672 | if (xas_invalid(xas)) |
| 673 | return first; |
| 674 | node = xas->xa_node; |
| 675 | if (node && (xas->xa_shift < node->shift)) |
| 676 | xas->xa_sibs = 0; |
| 677 | if ((first == entry) && !xas->xa_sibs) |
| 678 | return first; |
| 679 | |
| 680 | next = first; |
| 681 | offset = xas->xa_offset; |
| 682 | max = xas->xa_offset + xas->xa_sibs; |
| 683 | if (node) { |
| 684 | slot = &node->slots[offset]; |
| 685 | if (xas->xa_sibs) |
| 686 | xas_squash_marks(xas); |
| 687 | } |
| 688 | if (!entry) |
| 689 | xas_init_marks(xas); |
| 690 | |
| 691 | for (;;) { |
| 692 | /* |
| 693 | * Must clear the marks before setting the entry to NULL, |
| 694 | * otherwise xas_for_each_marked may find a NULL entry and |
| 695 | * stop early. rcu_assign_pointer contains a release barrier |
| 696 | * so the mark clearing will appear to happen before the |
| 697 | * entry is set to NULL. |
| 698 | */ |
| 699 | rcu_assign_pointer(*slot, entry); |
| 700 | if (xa_is_node(next)) |
| 701 | xas_free_nodes(xas, xa_to_node(next)); |
| 702 | if (!node) |
| 703 | break; |
| 704 | count += !next - !entry; |
| 705 | values += !xa_is_value(first) - !value; |
| 706 | if (entry) { |
| 707 | if (offset == max) |
| 708 | break; |
| 709 | if (!xa_is_sibling(entry)) |
| 710 | entry = xa_mk_sibling(xas->xa_offset); |
| 711 | } else { |
| 712 | if (offset == XA_CHUNK_MASK) |
| 713 | break; |
| 714 | } |
| 715 | next = xa_entry_locked(xas->xa, node, ++offset); |
| 716 | if (!xa_is_sibling(next)) { |
| 717 | if (!entry && (offset > max)) |
| 718 | break; |
| 719 | first = next; |
| 720 | } |
| 721 | slot++; |
| 722 | } |
| 723 | |
| 724 | update_node(xas, node, count, values); |
| 725 | return first; |
| 726 | } |
| 727 | EXPORT_SYMBOL_GPL(xas_store); |
| 728 | |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 729 | /** |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 730 | * xas_get_mark() - Returns the state of this mark. |
| 731 | * @xas: XArray operation state. |
| 732 | * @mark: Mark number. |
| 733 | * |
| 734 | * Return: true if the mark is set, false if the mark is clear or @xas |
| 735 | * is in an error state. |
| 736 | */ |
| 737 | bool xas_get_mark(const struct xa_state *xas, xa_mark_t mark) |
| 738 | { |
| 739 | if (xas_invalid(xas)) |
| 740 | return false; |
| 741 | if (!xas->xa_node) |
| 742 | return xa_marked(xas->xa, mark); |
| 743 | return node_get_mark(xas->xa_node, xas->xa_offset, mark); |
| 744 | } |
| 745 | EXPORT_SYMBOL_GPL(xas_get_mark); |
| 746 | |
| 747 | /** |
| 748 | * xas_set_mark() - Sets the mark on this entry and its parents. |
| 749 | * @xas: XArray operation state. |
| 750 | * @mark: Mark number. |
| 751 | * |
| 752 | * Sets the specified mark on this entry, and walks up the tree setting it |
| 753 | * on all the ancestor entries. Does nothing if @xas has not been walked to |
| 754 | * an entry, or is in an error state. |
| 755 | */ |
| 756 | void xas_set_mark(const struct xa_state *xas, xa_mark_t mark) |
| 757 | { |
| 758 | struct xa_node *node = xas->xa_node; |
| 759 | unsigned int offset = xas->xa_offset; |
| 760 | |
| 761 | if (xas_invalid(xas)) |
| 762 | return; |
| 763 | |
| 764 | while (node) { |
| 765 | if (node_set_mark(node, offset, mark)) |
| 766 | return; |
| 767 | offset = node->offset; |
| 768 | node = xa_parent_locked(xas->xa, node); |
| 769 | } |
| 770 | |
| 771 | if (!xa_marked(xas->xa, mark)) |
| 772 | xa_mark_set(xas->xa, mark); |
| 773 | } |
| 774 | EXPORT_SYMBOL_GPL(xas_set_mark); |
| 775 | |
| 776 | /** |
| 777 | * xas_clear_mark() - Clears the mark on this entry and its parents. |
| 778 | * @xas: XArray operation state. |
| 779 | * @mark: Mark number. |
| 780 | * |
| 781 | * Clears the specified mark on this entry, and walks back to the head |
| 782 | * attempting to clear it on all the ancestor entries. Does nothing if |
| 783 | * @xas has not been walked to an entry, or is in an error state. |
| 784 | */ |
| 785 | void xas_clear_mark(const struct xa_state *xas, xa_mark_t mark) |
| 786 | { |
| 787 | struct xa_node *node = xas->xa_node; |
| 788 | unsigned int offset = xas->xa_offset; |
| 789 | |
| 790 | if (xas_invalid(xas)) |
| 791 | return; |
| 792 | |
| 793 | while (node) { |
| 794 | if (!node_clear_mark(node, offset, mark)) |
| 795 | return; |
| 796 | if (node_any_mark(node, mark)) |
| 797 | return; |
| 798 | |
| 799 | offset = node->offset; |
| 800 | node = xa_parent_locked(xas->xa, node); |
| 801 | } |
| 802 | |
| 803 | if (xa_marked(xas->xa, mark)) |
| 804 | xa_mark_clear(xas->xa, mark); |
| 805 | } |
| 806 | EXPORT_SYMBOL_GPL(xas_clear_mark); |
| 807 | |
| 808 | /** |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame^] | 809 | * xas_init_marks() - Initialise all marks for the entry |
| 810 | * @xas: Array operations state. |
| 811 | * |
| 812 | * Initialise all marks for the entry specified by @xas. If we're tracking |
| 813 | * free entries with a mark, we need to set it on all entries. All other |
| 814 | * marks are cleared. |
| 815 | * |
| 816 | * This implementation is not as efficient as it could be; we may walk |
| 817 | * up the tree multiple times. |
| 818 | */ |
| 819 | void xas_init_marks(const struct xa_state *xas) |
| 820 | { |
| 821 | xa_mark_t mark = 0; |
| 822 | |
| 823 | for (;;) { |
| 824 | xas_clear_mark(xas, mark); |
| 825 | if (mark == XA_MARK_MAX) |
| 826 | break; |
| 827 | mark_inc(mark); |
| 828 | } |
| 829 | } |
| 830 | EXPORT_SYMBOL_GPL(xas_init_marks); |
| 831 | |
| 832 | /** |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 833 | * xa_init_flags() - Initialise an empty XArray with flags. |
| 834 | * @xa: XArray. |
| 835 | * @flags: XA_FLAG values. |
| 836 | * |
| 837 | * If you need to initialise an XArray with special flags (eg you need |
| 838 | * to take the lock from interrupt context), use this function instead |
| 839 | * of xa_init(). |
| 840 | * |
| 841 | * Context: Any context. |
| 842 | */ |
| 843 | void xa_init_flags(struct xarray *xa, gfp_t flags) |
| 844 | { |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame^] | 845 | unsigned int lock_type; |
| 846 | static struct lock_class_key xa_lock_irq; |
| 847 | static struct lock_class_key xa_lock_bh; |
| 848 | |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 849 | spin_lock_init(&xa->xa_lock); |
| 850 | xa->xa_flags = flags; |
| 851 | xa->xa_head = NULL; |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame^] | 852 | |
| 853 | lock_type = xa_lock_type(xa); |
| 854 | if (lock_type == XA_LOCK_IRQ) |
| 855 | lockdep_set_class(&xa->xa_lock, &xa_lock_irq); |
| 856 | else if (lock_type == XA_LOCK_BH) |
| 857 | lockdep_set_class(&xa->xa_lock, &xa_lock_bh); |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 858 | } |
| 859 | EXPORT_SYMBOL(xa_init_flags); |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 860 | |
| 861 | /** |
| 862 | * xa_load() - Load an entry from an XArray. |
| 863 | * @xa: XArray. |
| 864 | * @index: index into array. |
| 865 | * |
| 866 | * Context: Any context. Takes and releases the RCU lock. |
| 867 | * Return: The entry at @index in @xa. |
| 868 | */ |
| 869 | void *xa_load(struct xarray *xa, unsigned long index) |
| 870 | { |
| 871 | XA_STATE(xas, xa, index); |
| 872 | void *entry; |
| 873 | |
| 874 | rcu_read_lock(); |
| 875 | do { |
| 876 | entry = xas_load(&xas); |
| 877 | } while (xas_retry(&xas, entry)); |
| 878 | rcu_read_unlock(); |
| 879 | |
| 880 | return entry; |
| 881 | } |
| 882 | EXPORT_SYMBOL(xa_load); |
| 883 | |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame^] | 884 | static void *xas_result(struct xa_state *xas, void *curr) |
| 885 | { |
| 886 | XA_NODE_BUG_ON(xas->xa_node, xa_is_internal(curr)); |
| 887 | if (xas_error(xas)) |
| 888 | curr = xas->xa_node; |
| 889 | return curr; |
| 890 | } |
| 891 | |
| 892 | /** |
| 893 | * __xa_erase() - Erase this entry from the XArray while locked. |
| 894 | * @xa: XArray. |
| 895 | * @index: Index into array. |
| 896 | * |
| 897 | * If the entry at this index is a multi-index entry then all indices will |
| 898 | * be erased, and the entry will no longer be a multi-index entry. |
| 899 | * This function expects the xa_lock to be held on entry. |
| 900 | * |
| 901 | * Context: Any context. Expects xa_lock to be held on entry. May |
| 902 | * release and reacquire xa_lock if @gfp flags permit. |
| 903 | * Return: The old entry at this index. |
| 904 | */ |
| 905 | void *__xa_erase(struct xarray *xa, unsigned long index) |
| 906 | { |
| 907 | XA_STATE(xas, xa, index); |
| 908 | return xas_result(&xas, xas_store(&xas, NULL)); |
| 909 | } |
| 910 | EXPORT_SYMBOL_GPL(__xa_erase); |
| 911 | |
| 912 | /** |
| 913 | * xa_store() - Store this entry in the XArray. |
| 914 | * @xa: XArray. |
| 915 | * @index: Index into array. |
| 916 | * @entry: New entry. |
| 917 | * @gfp: Memory allocation flags. |
| 918 | * |
| 919 | * After this function returns, loads from this index will return @entry. |
| 920 | * Storing into an existing multislot entry updates the entry of every index. |
| 921 | * The marks associated with @index are unaffected unless @entry is %NULL. |
| 922 | * |
| 923 | * Context: Process context. Takes and releases the xa_lock. May sleep |
| 924 | * if the @gfp flags permit. |
| 925 | * Return: The old entry at this index on success, xa_err(-EINVAL) if @entry |
| 926 | * cannot be stored in an XArray, or xa_err(-ENOMEM) if memory allocation |
| 927 | * failed. |
| 928 | */ |
| 929 | void *xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp) |
| 930 | { |
| 931 | XA_STATE(xas, xa, index); |
| 932 | void *curr; |
| 933 | |
| 934 | if (WARN_ON_ONCE(xa_is_internal(entry))) |
| 935 | return XA_ERROR(-EINVAL); |
| 936 | |
| 937 | do { |
| 938 | xas_lock(&xas); |
| 939 | curr = xas_store(&xas, entry); |
| 940 | xas_unlock(&xas); |
| 941 | } while (xas_nomem(&xas, gfp)); |
| 942 | |
| 943 | return xas_result(&xas, curr); |
| 944 | } |
| 945 | EXPORT_SYMBOL(xa_store); |
| 946 | |
| 947 | /** |
| 948 | * __xa_store() - Store this entry in the XArray. |
| 949 | * @xa: XArray. |
| 950 | * @index: Index into array. |
| 951 | * @entry: New entry. |
| 952 | * @gfp: Memory allocation flags. |
| 953 | * |
| 954 | * You must already be holding the xa_lock when calling this function. |
| 955 | * It will drop the lock if needed to allocate memory, and then reacquire |
| 956 | * it afterwards. |
| 957 | * |
| 958 | * Context: Any context. Expects xa_lock to be held on entry. May |
| 959 | * release and reacquire xa_lock if @gfp flags permit. |
| 960 | * Return: The old entry at this index or xa_err() if an error happened. |
| 961 | */ |
| 962 | void *__xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp) |
| 963 | { |
| 964 | XA_STATE(xas, xa, index); |
| 965 | void *curr; |
| 966 | |
| 967 | if (WARN_ON_ONCE(xa_is_internal(entry))) |
| 968 | return XA_ERROR(-EINVAL); |
| 969 | |
| 970 | do { |
| 971 | curr = xas_store(&xas, entry); |
| 972 | } while (__xas_nomem(&xas, gfp)); |
| 973 | |
| 974 | return xas_result(&xas, curr); |
| 975 | } |
| 976 | EXPORT_SYMBOL(__xa_store); |
| 977 | |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 978 | /** |
| 979 | * __xa_set_mark() - Set this mark on this entry while locked. |
| 980 | * @xa: XArray. |
| 981 | * @index: Index of entry. |
| 982 | * @mark: Mark number. |
| 983 | * |
| 984 | * Attempting to set a mark on a NULL entry does not succeed. |
| 985 | * |
| 986 | * Context: Any context. Expects xa_lock to be held on entry. |
| 987 | */ |
| 988 | void __xa_set_mark(struct xarray *xa, unsigned long index, xa_mark_t mark) |
| 989 | { |
| 990 | XA_STATE(xas, xa, index); |
| 991 | void *entry = xas_load(&xas); |
| 992 | |
| 993 | if (entry) |
| 994 | xas_set_mark(&xas, mark); |
| 995 | } |
| 996 | EXPORT_SYMBOL_GPL(__xa_set_mark); |
| 997 | |
| 998 | /** |
| 999 | * __xa_clear_mark() - Clear this mark on this entry while locked. |
| 1000 | * @xa: XArray. |
| 1001 | * @index: Index of entry. |
| 1002 | * @mark: Mark number. |
| 1003 | * |
| 1004 | * Context: Any context. Expects xa_lock to be held on entry. |
| 1005 | */ |
| 1006 | void __xa_clear_mark(struct xarray *xa, unsigned long index, xa_mark_t mark) |
| 1007 | { |
| 1008 | XA_STATE(xas, xa, index); |
| 1009 | void *entry = xas_load(&xas); |
| 1010 | |
| 1011 | if (entry) |
| 1012 | xas_clear_mark(&xas, mark); |
| 1013 | } |
| 1014 | EXPORT_SYMBOL_GPL(__xa_clear_mark); |
| 1015 | |
| 1016 | /** |
| 1017 | * xa_get_mark() - Inquire whether this mark is set on this entry. |
| 1018 | * @xa: XArray. |
| 1019 | * @index: Index of entry. |
| 1020 | * @mark: Mark number. |
| 1021 | * |
| 1022 | * This function uses the RCU read lock, so the result may be out of date |
| 1023 | * by the time it returns. If you need the result to be stable, use a lock. |
| 1024 | * |
| 1025 | * Context: Any context. Takes and releases the RCU lock. |
| 1026 | * Return: True if the entry at @index has this mark set, false if it doesn't. |
| 1027 | */ |
| 1028 | bool xa_get_mark(struct xarray *xa, unsigned long index, xa_mark_t mark) |
| 1029 | { |
| 1030 | XA_STATE(xas, xa, index); |
| 1031 | void *entry; |
| 1032 | |
| 1033 | rcu_read_lock(); |
| 1034 | entry = xas_start(&xas); |
| 1035 | while (xas_get_mark(&xas, mark)) { |
| 1036 | if (!xa_is_node(entry)) |
| 1037 | goto found; |
| 1038 | entry = xas_descend(&xas, xa_to_node(entry)); |
| 1039 | } |
| 1040 | rcu_read_unlock(); |
| 1041 | return false; |
| 1042 | found: |
| 1043 | rcu_read_unlock(); |
| 1044 | return true; |
| 1045 | } |
| 1046 | EXPORT_SYMBOL(xa_get_mark); |
| 1047 | |
| 1048 | /** |
| 1049 | * xa_set_mark() - Set this mark on this entry. |
| 1050 | * @xa: XArray. |
| 1051 | * @index: Index of entry. |
| 1052 | * @mark: Mark number. |
| 1053 | * |
| 1054 | * Attempting to set a mark on a NULL entry does not succeed. |
| 1055 | * |
| 1056 | * Context: Process context. Takes and releases the xa_lock. |
| 1057 | */ |
| 1058 | void xa_set_mark(struct xarray *xa, unsigned long index, xa_mark_t mark) |
| 1059 | { |
| 1060 | xa_lock(xa); |
| 1061 | __xa_set_mark(xa, index, mark); |
| 1062 | xa_unlock(xa); |
| 1063 | } |
| 1064 | EXPORT_SYMBOL(xa_set_mark); |
| 1065 | |
| 1066 | /** |
| 1067 | * xa_clear_mark() - Clear this mark on this entry. |
| 1068 | * @xa: XArray. |
| 1069 | * @index: Index of entry. |
| 1070 | * @mark: Mark number. |
| 1071 | * |
| 1072 | * Clearing a mark always succeeds. |
| 1073 | * |
| 1074 | * Context: Process context. Takes and releases the xa_lock. |
| 1075 | */ |
| 1076 | void xa_clear_mark(struct xarray *xa, unsigned long index, xa_mark_t mark) |
| 1077 | { |
| 1078 | xa_lock(xa); |
| 1079 | __xa_clear_mark(xa, index, mark); |
| 1080 | xa_unlock(xa); |
| 1081 | } |
| 1082 | EXPORT_SYMBOL(xa_clear_mark); |
| 1083 | |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 1084 | #ifdef XA_DEBUG |
| 1085 | void xa_dump_node(const struct xa_node *node) |
| 1086 | { |
| 1087 | unsigned i, j; |
| 1088 | |
| 1089 | if (!node) |
| 1090 | return; |
| 1091 | if ((unsigned long)node & 3) { |
| 1092 | pr_cont("node %px\n", node); |
| 1093 | return; |
| 1094 | } |
| 1095 | |
| 1096 | pr_cont("node %px %s %d parent %px shift %d count %d values %d " |
| 1097 | "array %px list %px %px marks", |
| 1098 | node, node->parent ? "offset" : "max", node->offset, |
| 1099 | node->parent, node->shift, node->count, node->nr_values, |
| 1100 | node->array, node->private_list.prev, node->private_list.next); |
| 1101 | for (i = 0; i < XA_MAX_MARKS; i++) |
| 1102 | for (j = 0; j < XA_MARK_LONGS; j++) |
| 1103 | pr_cont(" %lx", node->marks[i][j]); |
| 1104 | pr_cont("\n"); |
| 1105 | } |
| 1106 | |
| 1107 | void xa_dump_index(unsigned long index, unsigned int shift) |
| 1108 | { |
| 1109 | if (!shift) |
| 1110 | pr_info("%lu: ", index); |
| 1111 | else if (shift >= BITS_PER_LONG) |
| 1112 | pr_info("0-%lu: ", ~0UL); |
| 1113 | else |
| 1114 | pr_info("%lu-%lu: ", index, index | ((1UL << shift) - 1)); |
| 1115 | } |
| 1116 | |
| 1117 | void xa_dump_entry(const void *entry, unsigned long index, unsigned long shift) |
| 1118 | { |
| 1119 | if (!entry) |
| 1120 | return; |
| 1121 | |
| 1122 | xa_dump_index(index, shift); |
| 1123 | |
| 1124 | if (xa_is_node(entry)) { |
| 1125 | if (shift == 0) { |
| 1126 | pr_cont("%px\n", entry); |
| 1127 | } else { |
| 1128 | unsigned long i; |
| 1129 | struct xa_node *node = xa_to_node(entry); |
| 1130 | xa_dump_node(node); |
| 1131 | for (i = 0; i < XA_CHUNK_SIZE; i++) |
| 1132 | xa_dump_entry(node->slots[i], |
| 1133 | index + (i << node->shift), node->shift); |
| 1134 | } |
| 1135 | } else if (xa_is_value(entry)) |
| 1136 | pr_cont("value %ld (0x%lx) [%px]\n", xa_to_value(entry), |
| 1137 | xa_to_value(entry), entry); |
| 1138 | else if (!xa_is_internal(entry)) |
| 1139 | pr_cont("%px\n", entry); |
| 1140 | else if (xa_is_retry(entry)) |
| 1141 | pr_cont("retry (%ld)\n", xa_to_internal(entry)); |
| 1142 | else if (xa_is_sibling(entry)) |
| 1143 | pr_cont("sibling (slot %ld)\n", xa_to_sibling(entry)); |
| 1144 | else |
| 1145 | pr_cont("UNKNOWN ENTRY (%px)\n", entry); |
| 1146 | } |
| 1147 | |
| 1148 | void xa_dump(const struct xarray *xa) |
| 1149 | { |
| 1150 | void *entry = xa->xa_head; |
| 1151 | unsigned int shift = 0; |
| 1152 | |
| 1153 | 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] | 1154 | xa->xa_flags, xa_marked(xa, XA_MARK_0), |
| 1155 | xa_marked(xa, XA_MARK_1), xa_marked(xa, XA_MARK_2)); |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 1156 | if (xa_is_node(entry)) |
| 1157 | shift = xa_to_node(entry)->shift + XA_CHUNK_SHIFT; |
| 1158 | xa_dump_entry(entry, 0, shift); |
| 1159 | } |
| 1160 | #endif |