1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2013 EMC Corp. 5 * Copyright (c) 2011 Jeffrey Roberson <jeff@freebsd.org> 6 * Copyright (c) 2008 Mayur Shardul <mayur.shardul@gmail.com> 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 */ 31 32 /* 33 * Path-compressed radix trie implementation. 34 * The following code is not generalized into a general purpose library 35 * because there are way too many parameters embedded that should really 36 * be decided by the library consumers. At the same time, consumers 37 * of this code must achieve highest possible performance. 38 * 39 * The implementation takes into account the following rationale: 40 * - Size of the nodes should be as small as possible but still big enough 41 * to avoid a large maximum depth for the trie. This is a balance 42 * between the necessity to not wire too much physical memory for the nodes 43 * and the necessity to avoid too much cache pollution during the trie 44 * operations. 45 * - There is not a huge bias toward the number of lookup operations over 46 * the number of insert and remove operations. This basically implies 47 * that optimizations supposedly helping one operation but hurting the 48 * other might be carefully evaluated. 49 * - On average not many nodes are expected to be fully populated, hence 50 * level compression may just complicate things. 51 */ 52 53 #include <sys/cdefs.h> 54 __FBSDID("$FreeBSD$"); 55 56 #include "opt_ddb.h" 57 58 #include <sys/param.h> 59 #include <sys/systm.h> 60 #include <sys/kernel.h> 61 #include <sys/libkern.h> 62 #include <sys/proc.h> 63 #include <sys/vmmeter.h> 64 #include <sys/smr.h> 65 #include <sys/smr_types.h> 66 67 #include <vm/uma.h> 68 #include <vm/vm.h> 69 #include <vm/vm_param.h> 70 #include <vm/vm_object.h> 71 #include <vm/vm_page.h> 72 #include <vm/vm_radix.h> 73 74 #ifdef DDB 75 #include <ddb/ddb.h> 76 #endif 77 78 /* 79 * These widths should allow the pointers to a node's children to fit within 80 * a single cache line. The extra levels from a narrow width should not be 81 * a problem thanks to path compression. 82 */ 83 #ifdef __LP64__ 84 #define VM_RADIX_WIDTH 4 85 #else 86 #define VM_RADIX_WIDTH 3 87 #endif 88 89 #define VM_RADIX_COUNT (1 << VM_RADIX_WIDTH) 90 #define VM_RADIX_MASK (VM_RADIX_COUNT - 1) 91 #define VM_RADIX_LIMIT \ 92 (howmany(sizeof(vm_pindex_t) * NBBY, VM_RADIX_WIDTH) - 1) 93 94 /* Flag bits stored in node pointers. */ 95 #define VM_RADIX_ISLEAF 0x1 96 #define VM_RADIX_FLAGS 0x1 97 #define VM_RADIX_PAD VM_RADIX_FLAGS 98 99 /* Returns one unit associated with specified level. */ 100 #define VM_RADIX_UNITLEVEL(lev) \ 101 ((vm_pindex_t)1 << ((lev) * VM_RADIX_WIDTH)) 102 103 enum vm_radix_access { SMR, LOCKED, UNSERIALIZED }; 104 105 struct vm_radix_node; 106 typedef SMR_POINTER(struct vm_radix_node *) smrnode_t; 107 108 struct vm_radix_node { 109 vm_pindex_t rn_owner; /* Owner of record. */ 110 uint16_t rn_count; /* Valid children. */ 111 uint8_t rn_clev; /* Current level. */ 112 int8_t rn_last; /* zero last ptr. */ 113 smrnode_t rn_child[VM_RADIX_COUNT]; /* Child nodes. */ 114 }; 115 116 static uma_zone_t vm_radix_node_zone; 117 static smr_t vm_radix_smr; 118 119 static void vm_radix_node_store(smrnode_t *p, struct vm_radix_node *v, 120 enum vm_radix_access access); 121 122 /* 123 * Allocate a radix node. 124 */ 125 static struct vm_radix_node * 126 vm_radix_node_get(vm_pindex_t owner, uint16_t count, uint16_t clevel) 127 { 128 struct vm_radix_node *rnode; 129 130 rnode = uma_zalloc_smr(vm_radix_node_zone, M_NOWAIT); 131 if (rnode == NULL) 132 return (NULL); 133 134 /* 135 * We want to clear the last child pointer after the final section 136 * has exited so lookup can not return false negatives. It is done 137 * here because it will be cache-cold in the dtor callback. 138 */ 139 if (rnode->rn_last != 0) { 140 vm_radix_node_store(&rnode->rn_child[rnode->rn_last - 1], 141 NULL, UNSERIALIZED); 142 rnode->rn_last = 0; 143 } 144 rnode->rn_owner = owner; 145 rnode->rn_count = count; 146 rnode->rn_clev = clevel; 147 return (rnode); 148 } 149 150 /* 151 * Free radix node. 152 */ 153 static __inline void 154 vm_radix_node_put(struct vm_radix_node *rnode, int8_t last) 155 { 156 #ifdef INVARIANTS 157 int slot; 158 159 KASSERT(rnode->rn_count == 0, 160 ("vm_radix_node_put: rnode %p has %d children", rnode, 161 rnode->rn_count)); 162 for (slot = 0; slot < VM_RADIX_COUNT; slot++) { 163 if (slot == last) 164 continue; 165 KASSERT(smr_unserialized_load(&rnode->rn_child[slot], true) == 166 NULL, ("vm_radix_node_put: rnode %p has a child", rnode)); 167 } 168 #endif 169 /* Off by one so a freshly zero'd node is not assigned to. */ 170 rnode->rn_last = last + 1; 171 uma_zfree_smr(vm_radix_node_zone, rnode); 172 } 173 174 /* 175 * Return the position in the array for a given level. 176 */ 177 static __inline int 178 vm_radix_slot(vm_pindex_t index, uint16_t level) 179 { 180 181 return ((index >> (level * VM_RADIX_WIDTH)) & VM_RADIX_MASK); 182 } 183 184 /* Computes the key (index) with the low-order 'level' radix-digits zeroed. */ 185 static __inline vm_pindex_t 186 vm_radix_trimkey(vm_pindex_t index, uint16_t level) 187 { 188 return (index & -VM_RADIX_UNITLEVEL(level)); 189 } 190 191 /* 192 * Fetch a node pointer from a slot in another node. 193 */ 194 static __inline struct vm_radix_node * 195 vm_radix_node_load(smrnode_t *p, enum vm_radix_access access) 196 { 197 198 switch (access) { 199 case UNSERIALIZED: 200 return (smr_unserialized_load(p, true)); 201 case LOCKED: 202 return (smr_serialized_load(p, true)); 203 case SMR: 204 return (smr_entered_load(p, vm_radix_smr)); 205 } 206 __assert_unreachable(); 207 } 208 209 static __inline void 210 vm_radix_node_store(smrnode_t *p, struct vm_radix_node *v, 211 enum vm_radix_access access) 212 { 213 214 switch (access) { 215 case UNSERIALIZED: 216 smr_unserialized_store(p, v, true); 217 break; 218 case LOCKED: 219 smr_serialized_store(p, v, true); 220 break; 221 case SMR: 222 panic("vm_radix_node_store: Not supported in smr section."); 223 } 224 } 225 226 /* 227 * Get the root node for a radix tree. 228 */ 229 static __inline struct vm_radix_node * 230 vm_radix_root_load(struct vm_radix *rtree, enum vm_radix_access access) 231 { 232 233 return (vm_radix_node_load((smrnode_t *)&rtree->rt_root, access)); 234 } 235 236 /* 237 * Set the root node for a radix tree. 238 */ 239 static __inline void 240 vm_radix_root_store(struct vm_radix *rtree, struct vm_radix_node *rnode, 241 enum vm_radix_access access) 242 { 243 244 vm_radix_node_store((smrnode_t *)&rtree->rt_root, rnode, access); 245 } 246 247 /* 248 * Returns TRUE if the specified radix node is a leaf and FALSE otherwise. 249 */ 250 static __inline bool 251 vm_radix_isleaf(struct vm_radix_node *rnode) 252 { 253 254 return (((uintptr_t)rnode & VM_RADIX_ISLEAF) != 0); 255 } 256 257 /* 258 * Returns page cast to radix node with leaf bit set. 259 */ 260 static __inline struct vm_radix_node * 261 vm_radix_toleaf(vm_page_t page) 262 { 263 return ((struct vm_radix_node *)((uintptr_t)page | VM_RADIX_ISLEAF)); 264 } 265 266 /* 267 * Returns the associated page extracted from rnode. 268 */ 269 static __inline vm_page_t 270 vm_radix_topage(struct vm_radix_node *rnode) 271 { 272 273 return ((vm_page_t)((uintptr_t)rnode & ~VM_RADIX_FLAGS)); 274 } 275 276 /* 277 * Adds the page as a child of the provided node. 278 */ 279 static __inline void 280 vm_radix_addpage(struct vm_radix_node *rnode, vm_pindex_t index, uint16_t clev, 281 vm_page_t page, enum vm_radix_access access) 282 { 283 int slot; 284 285 slot = vm_radix_slot(index, clev); 286 vm_radix_node_store(&rnode->rn_child[slot], 287 vm_radix_toleaf(page), access); 288 } 289 290 /* 291 * Returns the level where two keys differ. 292 * It cannot accept 2 equal keys. 293 */ 294 static __inline uint16_t 295 vm_radix_keydiff(vm_pindex_t index1, vm_pindex_t index2) 296 { 297 298 KASSERT(index1 != index2, ("%s: passing the same key value %jx", 299 __func__, (uintmax_t)index1)); 300 CTASSERT(sizeof(long long) >= sizeof(vm_pindex_t)); 301 302 /* 303 * From the highest-order bit where the indexes differ, 304 * compute the highest level in the trie where they differ. 305 */ 306 return ((flsll(index1 ^ index2) - 1) / VM_RADIX_WIDTH); 307 } 308 309 /* 310 * Returns TRUE if it can be determined that key does not belong to the 311 * specified rnode. Otherwise, returns FALSE. 312 */ 313 static __inline bool 314 vm_radix_keybarr(struct vm_radix_node *rnode, vm_pindex_t idx) 315 { 316 317 if (rnode->rn_clev < VM_RADIX_LIMIT) { 318 idx = vm_radix_trimkey(idx, rnode->rn_clev + 1); 319 return (idx != rnode->rn_owner); 320 } 321 return (false); 322 } 323 324 /* 325 * Internal helper for vm_radix_reclaim_allnodes(). 326 * This function is recursive. 327 */ 328 static void 329 vm_radix_reclaim_allnodes_int(struct vm_radix_node *rnode) 330 { 331 struct vm_radix_node *child; 332 int slot; 333 334 KASSERT(rnode->rn_count <= VM_RADIX_COUNT, 335 ("vm_radix_reclaim_allnodes_int: bad count in rnode %p", rnode)); 336 for (slot = 0; rnode->rn_count != 0; slot++) { 337 child = vm_radix_node_load(&rnode->rn_child[slot], 338 UNSERIALIZED); 339 if (child == NULL) 340 continue; 341 if (!vm_radix_isleaf(child)) 342 vm_radix_reclaim_allnodes_int(child); 343 vm_radix_node_store(&rnode->rn_child[slot], NULL, UNSERIALIZED); 344 rnode->rn_count--; 345 } 346 vm_radix_node_put(rnode, -1); 347 } 348 349 #ifndef UMA_MD_SMALL_ALLOC 350 void vm_radix_reserve_kva(void); 351 /* 352 * Reserve the KVA necessary to satisfy the node allocation. 353 * This is mandatory in architectures not supporting direct 354 * mapping as they will need otherwise to carve into the kernel maps for 355 * every node allocation, resulting into deadlocks for consumers already 356 * working with kernel maps. 357 */ 358 void 359 vm_radix_reserve_kva(void) 360 { 361 362 /* 363 * Calculate the number of reserved nodes, discounting the pages that 364 * are needed to store them. 365 */ 366 if (!uma_zone_reserve_kva(vm_radix_node_zone, 367 ((vm_paddr_t)vm_cnt.v_page_count * PAGE_SIZE) / (PAGE_SIZE + 368 sizeof(struct vm_radix_node)))) 369 panic("%s: unable to reserve KVA", __func__); 370 } 371 #endif 372 373 /* 374 * Initialize the UMA slab zone. 375 */ 376 void 377 vm_radix_zinit(void) 378 { 379 380 vm_radix_node_zone = uma_zcreate("RADIX NODE", 381 sizeof(struct vm_radix_node), NULL, NULL, NULL, NULL, 382 VM_RADIX_PAD, UMA_ZONE_VM | UMA_ZONE_SMR | UMA_ZONE_ZINIT); 383 vm_radix_smr = uma_zone_get_smr(vm_radix_node_zone); 384 } 385 386 /* 387 * Inserts the key-value pair into the trie. 388 * Panics if the key already exists. 389 */ 390 int 391 vm_radix_insert(struct vm_radix *rtree, vm_page_t page) 392 { 393 vm_pindex_t index, newind; 394 struct vm_radix_node *rnode, *tmp; 395 smrnode_t *parentp; 396 vm_page_t m; 397 int slot; 398 uint16_t clev; 399 400 index = page->pindex; 401 402 /* 403 * The owner of record for root is not really important because it 404 * will never be used. 405 */ 406 rnode = vm_radix_root_load(rtree, LOCKED); 407 if (rnode == NULL) { 408 rtree->rt_root = (uintptr_t)vm_radix_toleaf(page); 409 return (0); 410 } 411 parentp = (smrnode_t *)&rtree->rt_root; 412 for (;;) { 413 if (vm_radix_isleaf(rnode)) { 414 m = vm_radix_topage(rnode); 415 if (m->pindex == index) 416 panic("%s: key %jx is already present", 417 __func__, (uintmax_t)index); 418 clev = vm_radix_keydiff(m->pindex, index); 419 tmp = vm_radix_node_get(vm_radix_trimkey(index, 420 clev + 1), 2, clev); 421 if (tmp == NULL) 422 return (ENOMEM); 423 /* These writes are not yet visible due to ordering. */ 424 vm_radix_addpage(tmp, index, clev, page, UNSERIALIZED); 425 vm_radix_addpage(tmp, m->pindex, clev, m, UNSERIALIZED); 426 /* Synchronize to make leaf visible. */ 427 vm_radix_node_store(parentp, tmp, LOCKED); 428 return (0); 429 } else if (vm_radix_keybarr(rnode, index)) 430 break; 431 slot = vm_radix_slot(index, rnode->rn_clev); 432 parentp = &rnode->rn_child[slot]; 433 tmp = vm_radix_node_load(parentp, LOCKED); 434 if (tmp == NULL) { 435 rnode->rn_count++; 436 vm_radix_addpage(rnode, index, rnode->rn_clev, page, 437 LOCKED); 438 return (0); 439 } 440 rnode = tmp; 441 } 442 443 /* 444 * A new node is needed because the right insertion level is reached. 445 * Setup the new intermediate node and add the 2 children: the 446 * new object and the older edge. 447 */ 448 newind = rnode->rn_owner; 449 clev = vm_radix_keydiff(newind, index); 450 tmp = vm_radix_node_get(vm_radix_trimkey(index, clev + 1), 2, clev); 451 if (tmp == NULL) 452 return (ENOMEM); 453 slot = vm_radix_slot(newind, clev); 454 /* These writes are not yet visible due to ordering. */ 455 vm_radix_addpage(tmp, index, clev, page, UNSERIALIZED); 456 vm_radix_node_store(&tmp->rn_child[slot], rnode, UNSERIALIZED); 457 /* Serializing write to make the above visible. */ 458 vm_radix_node_store(parentp, tmp, LOCKED); 459 460 return (0); 461 } 462 463 /* 464 * Returns the value stored at the index. If the index is not present, 465 * NULL is returned. 466 */ 467 static __always_inline vm_page_t 468 _vm_radix_lookup(struct vm_radix *rtree, vm_pindex_t index, 469 enum vm_radix_access access) 470 { 471 struct vm_radix_node *rnode; 472 vm_page_t m; 473 int slot; 474 475 rnode = vm_radix_root_load(rtree, access); 476 while (rnode != NULL) { 477 if (vm_radix_isleaf(rnode)) { 478 m = vm_radix_topage(rnode); 479 if (m->pindex == index) 480 return (m); 481 break; 482 } 483 if (vm_radix_keybarr(rnode, index)) 484 break; 485 slot = vm_radix_slot(index, rnode->rn_clev); 486 rnode = vm_radix_node_load(&rnode->rn_child[slot], access); 487 } 488 return (NULL); 489 } 490 491 /* 492 * Returns the value stored at the index assuming there is an external lock. 493 * 494 * If the index is not present, NULL is returned. 495 */ 496 vm_page_t 497 vm_radix_lookup(struct vm_radix *rtree, vm_pindex_t index) 498 { 499 500 return _vm_radix_lookup(rtree, index, LOCKED); 501 } 502 503 /* 504 * Returns the value stored at the index without requiring an external lock. 505 * 506 * If the index is not present, NULL is returned. 507 */ 508 vm_page_t 509 vm_radix_lookup_unlocked(struct vm_radix *rtree, vm_pindex_t index) 510 { 511 vm_page_t m; 512 513 smr_enter(vm_radix_smr); 514 m = _vm_radix_lookup(rtree, index, SMR); 515 smr_exit(vm_radix_smr); 516 517 return (m); 518 } 519 520 /* 521 * Look up the nearest entry at a position greater than or equal to index. 522 */ 523 vm_page_t 524 vm_radix_lookup_ge(struct vm_radix *rtree, vm_pindex_t index) 525 { 526 struct vm_radix_node *stack[VM_RADIX_LIMIT]; 527 vm_pindex_t inc; 528 vm_page_t m; 529 struct vm_radix_node *child, *rnode; 530 #ifdef INVARIANTS 531 int loops = 0; 532 #endif 533 int slot, tos; 534 535 rnode = vm_radix_root_load(rtree, LOCKED); 536 if (rnode == NULL) 537 return (NULL); 538 else if (vm_radix_isleaf(rnode)) { 539 m = vm_radix_topage(rnode); 540 if (m->pindex >= index) 541 return (m); 542 else 543 return (NULL); 544 } 545 tos = 0; 546 for (;;) { 547 /* 548 * If the keys differ before the current bisection node, 549 * then the search key might rollback to the earliest 550 * available bisection node or to the smallest key 551 * in the current node (if the owner is greater than the 552 * search key). 553 */ 554 if (vm_radix_keybarr(rnode, index)) { 555 if (index > rnode->rn_owner) { 556 ascend: 557 KASSERT(++loops < 1000, 558 ("vm_radix_lookup_ge: too many loops")); 559 560 /* 561 * Pop nodes from the stack until either the 562 * stack is empty or a node that could have a 563 * matching descendant is found. 564 */ 565 do { 566 if (tos == 0) 567 return (NULL); 568 rnode = stack[--tos]; 569 } while (vm_radix_slot(index, 570 rnode->rn_clev) == (VM_RADIX_COUNT - 1)); 571 572 /* 573 * The following computation cannot overflow 574 * because index's slot at the current level 575 * is less than VM_RADIX_COUNT - 1. 576 */ 577 index = vm_radix_trimkey(index, 578 rnode->rn_clev); 579 index += VM_RADIX_UNITLEVEL(rnode->rn_clev); 580 } else 581 index = rnode->rn_owner; 582 KASSERT(!vm_radix_keybarr(rnode, index), 583 ("vm_radix_lookup_ge: keybarr failed")); 584 } 585 slot = vm_radix_slot(index, rnode->rn_clev); 586 child = vm_radix_node_load(&rnode->rn_child[slot], LOCKED); 587 if (vm_radix_isleaf(child)) { 588 m = vm_radix_topage(child); 589 if (m->pindex >= index) 590 return (m); 591 } else if (child != NULL) 592 goto descend; 593 594 /* 595 * Look for an available edge or page within the current 596 * bisection node. 597 */ 598 if (slot < (VM_RADIX_COUNT - 1)) { 599 inc = VM_RADIX_UNITLEVEL(rnode->rn_clev); 600 index = vm_radix_trimkey(index, rnode->rn_clev); 601 do { 602 index += inc; 603 slot++; 604 child = vm_radix_node_load(&rnode->rn_child[slot], 605 LOCKED); 606 if (vm_radix_isleaf(child)) { 607 m = vm_radix_topage(child); 608 KASSERT(m->pindex >= index, 609 ("vm_radix_lookup_ge: leaf<index")); 610 return (m); 611 } else if (child != NULL) 612 goto descend; 613 } while (slot < (VM_RADIX_COUNT - 1)); 614 } 615 KASSERT(child == NULL || vm_radix_isleaf(child), 616 ("vm_radix_lookup_ge: child is radix node")); 617 618 /* 619 * If a page or edge greater than the search slot is not found 620 * in the current node, ascend to the next higher-level node. 621 */ 622 goto ascend; 623 descend: 624 KASSERT(rnode->rn_clev > 0, 625 ("vm_radix_lookup_ge: pushing leaf's parent")); 626 KASSERT(tos < VM_RADIX_LIMIT, 627 ("vm_radix_lookup_ge: stack overflow")); 628 stack[tos++] = rnode; 629 rnode = child; 630 } 631 } 632 633 /* 634 * Look up the nearest entry at a position less than or equal to index. 635 */ 636 vm_page_t 637 vm_radix_lookup_le(struct vm_radix *rtree, vm_pindex_t index) 638 { 639 struct vm_radix_node *stack[VM_RADIX_LIMIT]; 640 vm_pindex_t inc; 641 vm_page_t m; 642 struct vm_radix_node *child, *rnode; 643 #ifdef INVARIANTS 644 int loops = 0; 645 #endif 646 int slot, tos; 647 648 rnode = vm_radix_root_load(rtree, LOCKED); 649 if (rnode == NULL) 650 return (NULL); 651 else if (vm_radix_isleaf(rnode)) { 652 m = vm_radix_topage(rnode); 653 if (m->pindex <= index) 654 return (m); 655 else 656 return (NULL); 657 } 658 tos = 0; 659 for (;;) { 660 /* 661 * If the keys differ before the current bisection node, 662 * then the search key might rollback to the earliest 663 * available bisection node or to the largest key 664 * in the current node (if the owner is smaller than the 665 * search key). 666 */ 667 if (vm_radix_keybarr(rnode, index)) { 668 if (index > rnode->rn_owner) { 669 index = rnode->rn_owner + VM_RADIX_COUNT * 670 VM_RADIX_UNITLEVEL(rnode->rn_clev); 671 } else { 672 ascend: 673 KASSERT(++loops < 1000, 674 ("vm_radix_lookup_le: too many loops")); 675 676 /* 677 * Pop nodes from the stack until either the 678 * stack is empty or a node that could have a 679 * matching descendant is found. 680 */ 681 do { 682 if (tos == 0) 683 return (NULL); 684 rnode = stack[--tos]; 685 } while (vm_radix_slot(index, 686 rnode->rn_clev) == 0); 687 688 /* 689 * The following computation cannot overflow 690 * because index's slot at the current level 691 * is greater than 0. 692 */ 693 index = vm_radix_trimkey(index, 694 rnode->rn_clev); 695 } 696 index--; 697 KASSERT(!vm_radix_keybarr(rnode, index), 698 ("vm_radix_lookup_le: keybarr failed")); 699 } 700 slot = vm_radix_slot(index, rnode->rn_clev); 701 child = vm_radix_node_load(&rnode->rn_child[slot], LOCKED); 702 if (vm_radix_isleaf(child)) { 703 m = vm_radix_topage(child); 704 if (m->pindex <= index) 705 return (m); 706 } else if (child != NULL) 707 goto descend; 708 709 /* 710 * Look for an available edge or page within the current 711 * bisection node. 712 */ 713 if (slot > 0) { 714 inc = VM_RADIX_UNITLEVEL(rnode->rn_clev); 715 index |= inc - 1; 716 do { 717 index -= inc; 718 slot--; 719 child = vm_radix_node_load(&rnode->rn_child[slot], 720 LOCKED); 721 if (vm_radix_isleaf(child)) { 722 m = vm_radix_topage(child); 723 KASSERT(m->pindex <= index, 724 ("vm_radix_lookup_le: leaf>index")); 725 return (m); 726 } else if (child != NULL) 727 goto descend; 728 } while (slot > 0); 729 } 730 KASSERT(child == NULL || vm_radix_isleaf(child), 731 ("vm_radix_lookup_le: child is radix node")); 732 733 /* 734 * If a page or edge smaller than the search slot is not found 735 * in the current node, ascend to the next higher-level node. 736 */ 737 goto ascend; 738 descend: 739 KASSERT(rnode->rn_clev > 0, 740 ("vm_radix_lookup_le: pushing leaf's parent")); 741 KASSERT(tos < VM_RADIX_LIMIT, 742 ("vm_radix_lookup_le: stack overflow")); 743 stack[tos++] = rnode; 744 rnode = child; 745 } 746 } 747 748 /* 749 * Remove the specified index from the trie, and return the value stored at 750 * that index. If the index is not present, return NULL. 751 */ 752 vm_page_t 753 vm_radix_remove(struct vm_radix *rtree, vm_pindex_t index) 754 { 755 struct vm_radix_node *rnode, *parent, *tmp; 756 vm_page_t m; 757 int i, slot; 758 759 rnode = vm_radix_root_load(rtree, LOCKED); 760 if (vm_radix_isleaf(rnode)) { 761 m = vm_radix_topage(rnode); 762 if (m->pindex != index) 763 return (NULL); 764 vm_radix_root_store(rtree, NULL, LOCKED); 765 return (m); 766 } 767 parent = NULL; 768 for (;;) { 769 if (rnode == NULL) 770 return (NULL); 771 slot = vm_radix_slot(index, rnode->rn_clev); 772 tmp = vm_radix_node_load(&rnode->rn_child[slot], LOCKED); 773 if (vm_radix_isleaf(tmp)) { 774 m = vm_radix_topage(tmp); 775 if (m->pindex != index) 776 return (NULL); 777 vm_radix_node_store( 778 &rnode->rn_child[slot], NULL, LOCKED); 779 rnode->rn_count--; 780 if (rnode->rn_count > 1) 781 return (m); 782 for (i = 0; i < VM_RADIX_COUNT; i++) { 783 tmp = vm_radix_node_load(&rnode->rn_child[i], 784 LOCKED); 785 if (tmp != NULL) 786 break; 787 } 788 KASSERT(tmp != NULL, 789 ("%s: invalid node configuration", __func__)); 790 if (parent == NULL) 791 vm_radix_root_store(rtree, tmp, LOCKED); 792 else { 793 slot = vm_radix_slot(index, parent->rn_clev); 794 KASSERT(vm_radix_node_load( 795 &parent->rn_child[slot], LOCKED) == rnode, 796 ("%s: invalid child value", __func__)); 797 vm_radix_node_store(&parent->rn_child[slot], 798 tmp, LOCKED); 799 } 800 /* 801 * The child is still valid and we can not zero the 802 * pointer until all smr references are gone. 803 */ 804 rnode->rn_count--; 805 vm_radix_node_put(rnode, i); 806 return (m); 807 } 808 parent = rnode; 809 rnode = tmp; 810 } 811 } 812 813 /* 814 * Remove and free all the nodes from the radix tree. 815 * This function is recursive but there is a tight control on it as the 816 * maximum depth of the tree is fixed. 817 */ 818 void 819 vm_radix_reclaim_allnodes(struct vm_radix *rtree) 820 { 821 struct vm_radix_node *root; 822 823 root = vm_radix_root_load(rtree, LOCKED); 824 if (root == NULL) 825 return; 826 vm_radix_root_store(rtree, NULL, UNSERIALIZED); 827 if (!vm_radix_isleaf(root)) 828 vm_radix_reclaim_allnodes_int(root); 829 } 830 831 /* 832 * Replace an existing page in the trie with another one. 833 * Panics if there is not an old page in the trie at the new page's index. 834 */ 835 vm_page_t 836 vm_radix_replace(struct vm_radix *rtree, vm_page_t newpage) 837 { 838 struct vm_radix_node *rnode, *tmp; 839 vm_page_t m; 840 vm_pindex_t index; 841 int slot; 842 843 index = newpage->pindex; 844 rnode = vm_radix_root_load(rtree, LOCKED); 845 if (rnode == NULL) 846 panic("%s: replacing page on an empty trie", __func__); 847 if (vm_radix_isleaf(rnode)) { 848 m = vm_radix_topage(rnode); 849 if (m->pindex != index) 850 panic("%s: original replacing root key not found", 851 __func__); 852 rtree->rt_root = (uintptr_t)vm_radix_toleaf(newpage); 853 return (m); 854 } 855 for (;;) { 856 slot = vm_radix_slot(index, rnode->rn_clev); 857 tmp = vm_radix_node_load(&rnode->rn_child[slot], LOCKED); 858 if (vm_radix_isleaf(tmp)) { 859 m = vm_radix_topage(tmp); 860 if (m->pindex != index) 861 break; 862 vm_radix_node_store(&rnode->rn_child[slot], 863 vm_radix_toleaf(newpage), LOCKED); 864 return (m); 865 } else if (tmp == NULL || vm_radix_keybarr(tmp, index)) 866 break; 867 rnode = tmp; 868 } 869 panic("%s: original replacing page not found", __func__); 870 } 871 872 void 873 vm_radix_wait(void) 874 { 875 uma_zwait(vm_radix_node_zone); 876 } 877 878 #ifdef DDB 879 /* 880 * Show details about the given radix node. 881 */ 882 DB_SHOW_COMMAND(radixnode, db_show_radixnode) 883 { 884 struct vm_radix_node *rnode, *tmp; 885 int i; 886 887 if (!have_addr) 888 return; 889 rnode = (struct vm_radix_node *)addr; 890 db_printf("radixnode %p, owner %jx, children count %u, level %u:\n", 891 (void *)rnode, (uintmax_t)rnode->rn_owner, rnode->rn_count, 892 rnode->rn_clev); 893 for (i = 0; i < VM_RADIX_COUNT; i++) { 894 tmp = vm_radix_node_load(&rnode->rn_child[i], UNSERIALIZED); 895 if (tmp != NULL) 896 db_printf("slot: %d, val: %p, page: %p, clev: %d\n", 897 i, (void *)tmp, 898 vm_radix_isleaf(tmp) ? vm_radix_topage(tmp) : NULL, 899 rnode->rn_clev); 900 } 901 } 902 #endif /* DDB */ 903