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 << ((VM_RADIX_LIMIT - (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. Pre-allocation should ensure that the request 107 * will always be satisfied. 108 */ 109 static __inline struct vm_radix_node * 110 vm_radix_node_get(vm_pindex_t owner, uint16_t count, uint16_t clevel) 111 { 112 struct vm_radix_node *rnode; 113 114 rnode = uma_zalloc(vm_radix_node_zone, M_NOWAIT); 115 116 /* 117 * The required number of nodes should already be pre-allocated 118 * by vm_radix_prealloc(). However, UMA can hold a few nodes 119 * in per-CPU buckets, which will not be accessible by the 120 * current CPU. Thus, the allocation could return NULL when 121 * the pre-allocated pool is close to exhaustion. Anyway, 122 * in practice this should never occur because a new node 123 * is not always required for insert. Thus, the pre-allocated 124 * pool should have some extra pages that prevent this from 125 * becoming a problem. 126 */ 127 if (rnode == NULL) 128 panic("%s: uma_zalloc() returned NULL for a new node", 129 __func__); 130 rnode->rn_owner = owner; 131 rnode->rn_count = count; 132 rnode->rn_clev = clevel; 133 return (rnode); 134 } 135 136 /* 137 * Free radix node. 138 */ 139 static __inline void 140 vm_radix_node_put(struct vm_radix_node *rnode) 141 { 142 143 uma_zfree(vm_radix_node_zone, rnode); 144 } 145 146 /* 147 * Return the position in the array for a given level. 148 */ 149 static __inline int 150 vm_radix_slot(vm_pindex_t index, uint16_t level) 151 { 152 153 return ((index >> ((VM_RADIX_LIMIT - level) * VM_RADIX_WIDTH)) & 154 VM_RADIX_MASK); 155 } 156 157 /* Trims the key after the specified level. */ 158 static __inline vm_pindex_t 159 vm_radix_trimkey(vm_pindex_t index, uint16_t level) 160 { 161 vm_pindex_t ret; 162 163 ret = index; 164 if (level < VM_RADIX_LIMIT) { 165 ret >>= (VM_RADIX_LIMIT - level) * VM_RADIX_WIDTH; 166 ret <<= (VM_RADIX_LIMIT - level) * VM_RADIX_WIDTH; 167 } 168 return (ret); 169 } 170 171 /* 172 * Get the root node for a radix tree. 173 */ 174 static __inline struct vm_radix_node * 175 vm_radix_getroot(struct vm_radix *rtree) 176 { 177 178 return ((struct vm_radix_node *)rtree->rt_root); 179 } 180 181 /* 182 * Set the root node for a radix tree. 183 */ 184 static __inline void 185 vm_radix_setroot(struct vm_radix *rtree, struct vm_radix_node *rnode) 186 { 187 188 rtree->rt_root = (uintptr_t)rnode; 189 } 190 191 /* 192 * Returns TRUE if the specified radix node is a leaf and FALSE otherwise. 193 */ 194 static __inline boolean_t 195 vm_radix_isleaf(struct vm_radix_node *rnode) 196 { 197 198 return (((uintptr_t)rnode & VM_RADIX_ISLEAF) != 0); 199 } 200 201 /* 202 * Returns the associated page extracted from rnode. 203 */ 204 static __inline vm_page_t 205 vm_radix_topage(struct vm_radix_node *rnode) 206 { 207 208 return ((vm_page_t)((uintptr_t)rnode & ~VM_RADIX_FLAGS)); 209 } 210 211 /* 212 * Adds the page as a child of the provided node. 213 */ 214 static __inline void 215 vm_radix_addpage(struct vm_radix_node *rnode, vm_pindex_t index, uint16_t clev, 216 vm_page_t page) 217 { 218 int slot; 219 220 slot = vm_radix_slot(index, clev); 221 rnode->rn_child[slot] = (void *)((uintptr_t)page | VM_RADIX_ISLEAF); 222 } 223 224 /* 225 * Returns the slot where two keys differ. 226 * It cannot accept 2 equal keys. 227 */ 228 static __inline uint16_t 229 vm_radix_keydiff(vm_pindex_t index1, vm_pindex_t index2) 230 { 231 uint16_t clev; 232 233 KASSERT(index1 != index2, ("%s: passing the same key value %jx", 234 __func__, (uintmax_t)index1)); 235 236 index1 ^= index2; 237 for (clev = 0; clev <= VM_RADIX_LIMIT ; clev++) 238 if (vm_radix_slot(index1, clev)) 239 return (clev); 240 panic("%s: cannot reach this point", __func__); 241 return (0); 242 } 243 244 /* 245 * Returns TRUE if it can be determined that key does not belong to the 246 * specified rnode. Otherwise, returns FALSE. 247 */ 248 static __inline boolean_t 249 vm_radix_keybarr(struct vm_radix_node *rnode, vm_pindex_t idx) 250 { 251 252 if (rnode->rn_clev > 0) { 253 idx = vm_radix_trimkey(idx, rnode->rn_clev - 1); 254 return (idx != rnode->rn_owner); 255 } 256 return (FALSE); 257 } 258 259 /* 260 * Internal helper for vm_radix_reclaim_allnodes(). 261 * This function is recursive. 262 */ 263 static void 264 vm_radix_reclaim_allnodes_int(struct vm_radix_node *rnode) 265 { 266 int slot; 267 268 KASSERT(rnode->rn_count <= VM_RADIX_COUNT, 269 ("vm_radix_reclaim_allnodes_int: bad count in rnode %p", rnode)); 270 for (slot = 0; rnode->rn_count != 0; slot++) { 271 if (rnode->rn_child[slot] == NULL) 272 continue; 273 if (!vm_radix_isleaf(rnode->rn_child[slot])) 274 vm_radix_reclaim_allnodes_int(rnode->rn_child[slot]); 275 rnode->rn_child[slot] = NULL; 276 rnode->rn_count--; 277 } 278 vm_radix_node_put(rnode); 279 } 280 281 #ifdef INVARIANTS 282 /* 283 * Radix node zone destructor. 284 */ 285 static void 286 vm_radix_node_zone_dtor(void *mem, int size __unused, void *arg __unused) 287 { 288 struct vm_radix_node *rnode; 289 int slot; 290 291 rnode = mem; 292 KASSERT(rnode->rn_count == 0, 293 ("vm_radix_node_put: rnode %p has %d children", rnode, 294 rnode->rn_count)); 295 for (slot = 0; slot < VM_RADIX_COUNT; slot++) 296 KASSERT(rnode->rn_child[slot] == NULL, 297 ("vm_radix_node_put: rnode %p has a child", rnode)); 298 } 299 #endif 300 301 /* 302 * Radix node zone initializer. 303 */ 304 static int 305 vm_radix_node_zone_init(void *mem, int size __unused, int flags __unused) 306 { 307 struct vm_radix_node *rnode; 308 309 rnode = mem; 310 memset(rnode->rn_child, 0, sizeof(rnode->rn_child)); 311 return (0); 312 } 313 314 /* 315 * Pre-allocate intermediate nodes from the UMA slab zone. 316 */ 317 static void 318 vm_radix_prealloc(void *arg __unused) 319 { 320 int nodes; 321 322 /* 323 * Calculate the number of reserved nodes, discounting the pages that 324 * are needed to store them. 325 */ 326 nodes = ((vm_paddr_t)cnt.v_page_count * PAGE_SIZE) / (PAGE_SIZE + 327 sizeof(struct vm_radix_node)); 328 if (!uma_zone_reserve_kva(vm_radix_node_zone, nodes)) 329 panic("%s: unable to create new zone", __func__); 330 uma_prealloc(vm_radix_node_zone, nodes); 331 } 332 SYSINIT(vm_radix_prealloc, SI_SUB_KMEM, SI_ORDER_SECOND, vm_radix_prealloc, 333 NULL); 334 335 /* 336 * Initialize the UMA slab zone. 337 * Until vm_radix_prealloc() is called, the zone will be served by the 338 * UMA boot-time pre-allocated pool of pages. 339 */ 340 void 341 vm_radix_init(void) 342 { 343 344 vm_radix_node_zone = uma_zcreate("RADIX NODE", 345 sizeof(struct vm_radix_node), NULL, 346 #ifdef INVARIANTS 347 vm_radix_node_zone_dtor, 348 #else 349 NULL, 350 #endif 351 vm_radix_node_zone_init, NULL, VM_RADIX_PAD, UMA_ZONE_VM | 352 UMA_ZONE_NOFREE); 353 } 354 355 /* 356 * Inserts the key-value pair into the trie. 357 * Panics if the key already exists. 358 */ 359 void 360 vm_radix_insert(struct vm_radix *rtree, vm_page_t page) 361 { 362 vm_pindex_t index, newind; 363 void **parentp; 364 struct vm_radix_node *rnode, *tmp; 365 vm_page_t m; 366 int slot; 367 uint16_t clev; 368 369 index = page->pindex; 370 371 /* 372 * The owner of record for root is not really important because it 373 * will never be used. 374 */ 375 rnode = vm_radix_getroot(rtree); 376 if (rnode == NULL) { 377 rtree->rt_root = (uintptr_t)page | VM_RADIX_ISLEAF; 378 return; 379 } 380 parentp = (void **)&rtree->rt_root; 381 for (;;) { 382 if (vm_radix_isleaf(rnode)) { 383 m = vm_radix_topage(rnode); 384 if (m->pindex == index) 385 panic("%s: key %jx is already present", 386 __func__, (uintmax_t)index); 387 clev = vm_radix_keydiff(m->pindex, index); 388 tmp = vm_radix_node_get(vm_radix_trimkey(index, 389 clev - 1), 2, clev); 390 *parentp = tmp; 391 vm_radix_addpage(tmp, index, clev, page); 392 vm_radix_addpage(tmp, m->pindex, clev, m); 393 return; 394 } else if (vm_radix_keybarr(rnode, index)) 395 break; 396 slot = vm_radix_slot(index, rnode->rn_clev); 397 if (rnode->rn_child[slot] == NULL) { 398 rnode->rn_count++; 399 vm_radix_addpage(rnode, index, rnode->rn_clev, page); 400 return; 401 } 402 parentp = &rnode->rn_child[slot]; 403 rnode = rnode->rn_child[slot]; 404 } 405 406 /* 407 * A new node is needed because the right insertion level is reached. 408 * Setup the new intermediate node and add the 2 children: the 409 * new object and the older edge. 410 */ 411 newind = rnode->rn_owner; 412 clev = vm_radix_keydiff(newind, index); 413 tmp = vm_radix_node_get(vm_radix_trimkey(index, clev - 1), 2, 414 clev); 415 *parentp = tmp; 416 vm_radix_addpage(tmp, index, clev, page); 417 slot = vm_radix_slot(newind, clev); 418 tmp->rn_child[slot] = rnode; 419 } 420 421 /* 422 * Returns the value stored at the index. If the index is not present, 423 * NULL is returned. 424 */ 425 vm_page_t 426 vm_radix_lookup(struct vm_radix *rtree, vm_pindex_t index) 427 { 428 struct vm_radix_node *rnode; 429 vm_page_t m; 430 int slot; 431 432 rnode = vm_radix_getroot(rtree); 433 while (rnode != NULL) { 434 if (vm_radix_isleaf(rnode)) { 435 m = vm_radix_topage(rnode); 436 if (m->pindex == index) 437 return (m); 438 else 439 break; 440 } else if (vm_radix_keybarr(rnode, index)) 441 break; 442 slot = vm_radix_slot(index, rnode->rn_clev); 443 rnode = rnode->rn_child[slot]; 444 } 445 return (NULL); 446 } 447 448 /* 449 * Look up the nearest entry at a position bigger than or equal to index. 450 */ 451 vm_page_t 452 vm_radix_lookup_ge(struct vm_radix *rtree, vm_pindex_t index) 453 { 454 struct vm_radix_node *stack[VM_RADIX_LIMIT]; 455 vm_pindex_t inc; 456 vm_page_t m; 457 struct vm_radix_node *child, *rnode; 458 #ifdef INVARIANTS 459 int loops = 0; 460 #endif 461 int slot, tos; 462 463 rnode = vm_radix_getroot(rtree); 464 if (rnode == NULL) 465 return (NULL); 466 else if (vm_radix_isleaf(rnode)) { 467 m = vm_radix_topage(rnode); 468 if (m->pindex >= index) 469 return (m); 470 else 471 return (NULL); 472 } 473 tos = 0; 474 for (;;) { 475 /* 476 * If the keys differ before the current bisection node, 477 * then the search key might rollback to the earliest 478 * available bisection node or to the smallest key 479 * in the current node (if the owner is bigger than the 480 * search key). 481 */ 482 if (vm_radix_keybarr(rnode, index)) { 483 if (index > rnode->rn_owner) { 484 ascend: 485 KASSERT(++loops < 1000, 486 ("vm_radix_lookup_ge: too many loops")); 487 488 /* 489 * Pop nodes from the stack until either the 490 * stack is empty or a node that could have a 491 * matching descendant is found. 492 */ 493 do { 494 if (tos == 0) 495 return (NULL); 496 rnode = stack[--tos]; 497 } while (vm_radix_slot(index, 498 rnode->rn_clev) == (VM_RADIX_COUNT - 1)); 499 500 /* 501 * The following computation cannot overflow 502 * because index's slot at the current level 503 * is less than VM_RADIX_COUNT - 1. 504 */ 505 index = vm_radix_trimkey(index, 506 rnode->rn_clev); 507 index += VM_RADIX_UNITLEVEL(rnode->rn_clev); 508 } else 509 index = rnode->rn_owner; 510 KASSERT(!vm_radix_keybarr(rnode, index), 511 ("vm_radix_lookup_ge: keybarr failed")); 512 } 513 slot = vm_radix_slot(index, rnode->rn_clev); 514 child = rnode->rn_child[slot]; 515 if (vm_radix_isleaf(child)) { 516 m = vm_radix_topage(child); 517 if (m->pindex >= index) 518 return (m); 519 } else if (child != NULL) 520 goto descend; 521 522 /* 523 * Look for an available edge or page within the current 524 * bisection node. 525 */ 526 if (slot < (VM_RADIX_COUNT - 1)) { 527 inc = VM_RADIX_UNITLEVEL(rnode->rn_clev); 528 index = vm_radix_trimkey(index, rnode->rn_clev); 529 do { 530 index += inc; 531 slot++; 532 child = rnode->rn_child[slot]; 533 if (vm_radix_isleaf(child)) { 534 m = vm_radix_topage(child); 535 if (m->pindex >= index) 536 return (m); 537 } else if (child != NULL) 538 goto descend; 539 } while (slot < (VM_RADIX_COUNT - 1)); 540 } 541 KASSERT(child == NULL || vm_radix_isleaf(child), 542 ("vm_radix_lookup_ge: child is radix node")); 543 544 /* 545 * If a page or edge bigger than the search slot is not found 546 * in the current node, ascend to the next higher-level node. 547 */ 548 goto ascend; 549 descend: 550 KASSERT(rnode->rn_clev < VM_RADIX_LIMIT, 551 ("vm_radix_lookup_ge: pushing leaf's parent")); 552 KASSERT(tos < VM_RADIX_LIMIT, 553 ("vm_radix_lookup_ge: stack overflow")); 554 stack[tos++] = rnode; 555 rnode = child; 556 } 557 } 558 559 /* 560 * Look up the nearest entry at a position less than or equal to index. 561 */ 562 vm_page_t 563 vm_radix_lookup_le(struct vm_radix *rtree, vm_pindex_t index) 564 { 565 struct vm_radix_node *stack[VM_RADIX_LIMIT]; 566 vm_pindex_t inc; 567 vm_page_t m; 568 struct vm_radix_node *child, *rnode; 569 #ifdef INVARIANTS 570 int loops = 0; 571 #endif 572 int slot, tos; 573 574 rnode = vm_radix_getroot(rtree); 575 if (rnode == NULL) 576 return (NULL); 577 else if (vm_radix_isleaf(rnode)) { 578 m = vm_radix_topage(rnode); 579 if (m->pindex <= index) 580 return (m); 581 else 582 return (NULL); 583 } 584 tos = 0; 585 for (;;) { 586 /* 587 * If the keys differ before the current bisection node, 588 * then the search key might rollback to the earliest 589 * available bisection node or to the largest key 590 * in the current node (if the owner is smaller than the 591 * search key). 592 */ 593 if (vm_radix_keybarr(rnode, index)) { 594 if (index > rnode->rn_owner) { 595 index = rnode->rn_owner + VM_RADIX_COUNT * 596 VM_RADIX_UNITLEVEL(rnode->rn_clev); 597 } else { 598 ascend: 599 KASSERT(++loops < 1000, 600 ("vm_radix_lookup_le: too many loops")); 601 602 /* 603 * Pop nodes from the stack until either the 604 * stack is empty or a node that could have a 605 * matching descendant is found. 606 */ 607 do { 608 if (tos == 0) 609 return (NULL); 610 rnode = stack[--tos]; 611 } while (vm_radix_slot(index, 612 rnode->rn_clev) == 0); 613 614 /* 615 * The following computation cannot overflow 616 * because index's slot at the current level 617 * is greater than 0. 618 */ 619 index = vm_radix_trimkey(index, 620 rnode->rn_clev); 621 } 622 index--; 623 KASSERT(!vm_radix_keybarr(rnode, index), 624 ("vm_radix_lookup_le: keybarr failed")); 625 } 626 slot = vm_radix_slot(index, rnode->rn_clev); 627 child = rnode->rn_child[slot]; 628 if (vm_radix_isleaf(child)) { 629 m = vm_radix_topage(child); 630 if (m->pindex <= index) 631 return (m); 632 } else if (child != NULL) 633 goto descend; 634 635 /* 636 * Look for an available edge or page within the current 637 * bisection node. 638 */ 639 if (slot > 0) { 640 inc = VM_RADIX_UNITLEVEL(rnode->rn_clev); 641 index |= inc - 1; 642 do { 643 index -= inc; 644 slot--; 645 child = rnode->rn_child[slot]; 646 if (vm_radix_isleaf(child)) { 647 m = vm_radix_topage(child); 648 if (m->pindex <= index) 649 return (m); 650 } else if (child != NULL) 651 goto descend; 652 } while (slot > 0); 653 } 654 KASSERT(child == NULL || vm_radix_isleaf(child), 655 ("vm_radix_lookup_le: child is radix node")); 656 657 /* 658 * If a page or edge smaller than the search slot is not found 659 * in the current node, ascend to the next higher-level node. 660 */ 661 goto ascend; 662 descend: 663 KASSERT(rnode->rn_clev < VM_RADIX_LIMIT, 664 ("vm_radix_lookup_le: pushing leaf's parent")); 665 KASSERT(tos < VM_RADIX_LIMIT, 666 ("vm_radix_lookup_le: stack overflow")); 667 stack[tos++] = rnode; 668 rnode = child; 669 } 670 } 671 672 /* 673 * Remove the specified index from the tree. 674 * Panics if the key is not present. 675 */ 676 void 677 vm_radix_remove(struct vm_radix *rtree, vm_pindex_t index) 678 { 679 struct vm_radix_node *rnode, *parent; 680 vm_page_t m; 681 int i, slot; 682 683 rnode = vm_radix_getroot(rtree); 684 if (vm_radix_isleaf(rnode)) { 685 m = vm_radix_topage(rnode); 686 if (m->pindex != index) 687 panic("%s: invalid key found", __func__); 688 vm_radix_setroot(rtree, NULL); 689 return; 690 } 691 parent = NULL; 692 for (;;) { 693 if (rnode == NULL) 694 panic("vm_radix_remove: impossible to locate the key"); 695 slot = vm_radix_slot(index, rnode->rn_clev); 696 if (vm_radix_isleaf(rnode->rn_child[slot])) { 697 m = vm_radix_topage(rnode->rn_child[slot]); 698 if (m->pindex != index) 699 panic("%s: invalid key found", __func__); 700 rnode->rn_child[slot] = NULL; 701 rnode->rn_count--; 702 if (rnode->rn_count > 1) 703 break; 704 for (i = 0; i < VM_RADIX_COUNT; i++) 705 if (rnode->rn_child[i] != NULL) 706 break; 707 KASSERT(i != VM_RADIX_COUNT, 708 ("%s: invalid node configuration", __func__)); 709 if (parent == NULL) 710 vm_radix_setroot(rtree, rnode->rn_child[i]); 711 else { 712 slot = vm_radix_slot(index, parent->rn_clev); 713 KASSERT(parent->rn_child[slot] == rnode, 714 ("%s: invalid child value", __func__)); 715 parent->rn_child[slot] = rnode->rn_child[i]; 716 } 717 rnode->rn_count--; 718 rnode->rn_child[i] = NULL; 719 vm_radix_node_put(rnode); 720 break; 721 } 722 parent = rnode; 723 rnode = rnode->rn_child[slot]; 724 } 725 } 726 727 /* 728 * Remove and free all the nodes from the radix tree. 729 * This function is recursive but there is a tight control on it as the 730 * maximum depth of the tree is fixed. 731 */ 732 void 733 vm_radix_reclaim_allnodes(struct vm_radix *rtree) 734 { 735 struct vm_radix_node *root; 736 737 root = vm_radix_getroot(rtree); 738 if (root == NULL) 739 return; 740 vm_radix_setroot(rtree, NULL); 741 if (!vm_radix_isleaf(root)) 742 vm_radix_reclaim_allnodes_int(root); 743 } 744 745 #ifdef DDB 746 /* 747 * Show details about the given radix node. 748 */ 749 DB_SHOW_COMMAND(radixnode, db_show_radixnode) 750 { 751 struct vm_radix_node *rnode; 752 int i; 753 754 if (!have_addr) 755 return; 756 rnode = (struct vm_radix_node *)addr; 757 db_printf("radixnode %p, owner %jx, children count %u, level %u:\n", 758 (void *)rnode, (uintmax_t)rnode->rn_owner, rnode->rn_count, 759 rnode->rn_clev); 760 for (i = 0; i < VM_RADIX_COUNT; i++) 761 if (rnode->rn_child[i] != NULL) 762 db_printf("slot: %d, val: %p, page: %p, clev: %d\n", 763 i, (void *)rnode->rn_child[i], 764 vm_radix_isleaf(rnode->rn_child[i]) ? 765 vm_radix_topage(rnode->rn_child[i]) : NULL, 766 rnode->rn_clev); 767 } 768 #endif /* DDB */ 769