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