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 void *rn_child[VM_RADIX_COUNT]; /* Child nodes. */ 98 vm_pindex_t rn_owner; /* Owner of record. */ 99 uint16_t rn_count; /* Valid children. */ 100 uint16_t rn_clev; /* Current level. */ 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 & ~VM_RADIX_FLAGS)); 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 the associated page extracted from rnode if available, 193 * and NULL otherwise. 194 */ 195 static __inline vm_page_t 196 vm_radix_node_page(struct vm_radix_node *rnode) 197 { 198 199 return ((((uintptr_t)rnode & VM_RADIX_ISLEAF) != 0) ? 200 (vm_page_t)((uintptr_t)rnode & ~VM_RADIX_FLAGS) : NULL); 201 } 202 203 /* 204 * Adds the page as a child of the provided node. 205 */ 206 static __inline void 207 vm_radix_addpage(struct vm_radix_node *rnode, vm_pindex_t index, uint16_t clev, 208 vm_page_t page) 209 { 210 int slot; 211 212 slot = vm_radix_slot(index, clev); 213 rnode->rn_child[slot] = (void *)((uintptr_t)page | VM_RADIX_ISLEAF); 214 } 215 216 /* 217 * Returns the slot where two keys differ. 218 * It cannot accept 2 equal keys. 219 */ 220 static __inline uint16_t 221 vm_radix_keydiff(vm_pindex_t index1, vm_pindex_t index2) 222 { 223 uint16_t clev; 224 225 KASSERT(index1 != index2, ("%s: passing the same key value %jx", 226 __func__, (uintmax_t)index1)); 227 228 index1 ^= index2; 229 for (clev = 0; clev <= VM_RADIX_LIMIT ; clev++) 230 if (vm_radix_slot(index1, clev)) 231 return (clev); 232 panic("%s: cannot reach this point", __func__); 233 return (0); 234 } 235 236 /* 237 * Returns TRUE if it can be determined that key does not belong to the 238 * specified rnode. Otherwise, returns FALSE. 239 */ 240 static __inline boolean_t 241 vm_radix_keybarr(struct vm_radix_node *rnode, vm_pindex_t idx) 242 { 243 244 if (rnode->rn_clev > 0) { 245 idx = vm_radix_trimkey(idx, rnode->rn_clev - 1); 246 idx -= rnode->rn_owner; 247 if (idx != 0) 248 return (TRUE); 249 } 250 return (FALSE); 251 } 252 253 /* 254 * Adjusts the idx key to the first upper level available, based on a valid 255 * initial level and map of available levels. 256 * Returns a value bigger than 0 to signal that there are not valid levels 257 * available. 258 */ 259 static __inline int 260 vm_radix_addlev(vm_pindex_t *idx, boolean_t *levels, uint16_t ilev) 261 { 262 vm_pindex_t wrapidx; 263 264 for (; levels[ilev] == FALSE || 265 vm_radix_slot(*idx, ilev) == (VM_RADIX_COUNT - 1); ilev--) 266 if (ilev == 0) 267 break; 268 KASSERT(ilev > 0 || levels[0], 269 ("%s: levels back-scanning problem", __func__)); 270 if (ilev == 0 && vm_radix_slot(*idx, ilev) == (VM_RADIX_COUNT - 1)) 271 return (1); 272 wrapidx = *idx; 273 *idx = vm_radix_trimkey(*idx, ilev); 274 *idx += VM_RADIX_UNITLEVEL(ilev); 275 return (*idx < wrapidx); 276 } 277 278 /* 279 * Adjusts the idx key to the first lower level available, based on a valid 280 * initial level and map of available levels. 281 * Returns a value bigger than 0 to signal that there are not valid levels 282 * available. 283 */ 284 static __inline int 285 vm_radix_declev(vm_pindex_t *idx, boolean_t *levels, uint16_t ilev) 286 { 287 vm_pindex_t wrapidx; 288 289 for (; levels[ilev] == FALSE || 290 vm_radix_slot(*idx, ilev) == 0; ilev--) 291 if (ilev == 0) 292 break; 293 KASSERT(ilev > 0 || levels[0], 294 ("%s: levels back-scanning problem", __func__)); 295 if (ilev == 0 && vm_radix_slot(*idx, ilev) == 0) 296 return (1); 297 wrapidx = *idx; 298 *idx = vm_radix_trimkey(*idx, ilev); 299 *idx |= VM_RADIX_UNITLEVEL(ilev) - 1; 300 *idx -= VM_RADIX_UNITLEVEL(ilev); 301 return (*idx > wrapidx); 302 } 303 304 /* 305 * Internal helper for vm_radix_reclaim_allnodes(). 306 * This function is recursive. 307 */ 308 static void 309 vm_radix_reclaim_allnodes_int(struct vm_radix_node *rnode) 310 { 311 int slot; 312 313 KASSERT(rnode->rn_count <= VM_RADIX_COUNT, 314 ("vm_radix_reclaim_allnodes_int: bad count in rnode %p", rnode)); 315 for (slot = 0; rnode->rn_count != 0; slot++) { 316 if (rnode->rn_child[slot] == NULL) 317 continue; 318 if (vm_radix_node_page(rnode->rn_child[slot]) == NULL) 319 vm_radix_reclaim_allnodes_int(rnode->rn_child[slot]); 320 rnode->rn_child[slot] = NULL; 321 rnode->rn_count--; 322 } 323 vm_radix_node_put(rnode); 324 } 325 326 #ifdef INVARIANTS 327 /* 328 * Radix node zone destructor. 329 */ 330 static void 331 vm_radix_node_zone_dtor(void *mem, int size __unused, void *arg __unused) 332 { 333 struct vm_radix_node *rnode; 334 int slot; 335 336 rnode = mem; 337 KASSERT(rnode->rn_count == 0, 338 ("vm_radix_node_put: rnode %p has %d children", rnode, 339 rnode->rn_count)); 340 for (slot = 0; slot < VM_RADIX_COUNT; slot++) 341 KASSERT(rnode->rn_child[slot] == NULL, 342 ("vm_radix_node_put: rnode %p has a child", rnode)); 343 } 344 #endif 345 346 /* 347 * Radix node zone initializer. 348 */ 349 static int 350 vm_radix_node_zone_init(void *mem, int size __unused, int flags __unused) 351 { 352 struct vm_radix_node *rnode; 353 354 rnode = mem; 355 memset(rnode->rn_child, 0, sizeof(rnode->rn_child)); 356 return (0); 357 } 358 359 /* 360 * Pre-allocate intermediate nodes from the UMA slab zone. 361 */ 362 static void 363 vm_radix_prealloc(void *arg __unused) 364 { 365 366 if (!uma_zone_reserve_kva(vm_radix_node_zone, cnt.v_page_count)) 367 panic("%s: unable to create new zone", __func__); 368 uma_prealloc(vm_radix_node_zone, cnt.v_page_count); 369 } 370 SYSINIT(vm_radix_prealloc, SI_SUB_KMEM, SI_ORDER_SECOND, vm_radix_prealloc, 371 NULL); 372 373 /* 374 * Initialize the UMA slab zone. 375 * Until vm_radix_prealloc() is called, the zone will be served by the 376 * UMA boot-time pre-allocated pool of pages. 377 */ 378 void 379 vm_radix_init(void) 380 { 381 382 vm_radix_node_zone = uma_zcreate("RADIX NODE", 383 sizeof(struct vm_radix_node), NULL, 384 #ifdef INVARIANTS 385 vm_radix_node_zone_dtor, 386 #else 387 NULL, 388 #endif 389 vm_radix_node_zone_init, NULL, VM_RADIX_PAD, UMA_ZONE_VM | 390 UMA_ZONE_NOFREE); 391 } 392 393 /* 394 * Inserts the key-value pair into the trie. 395 * Panics if the key already exists. 396 */ 397 void 398 vm_radix_insert(struct vm_radix *rtree, vm_page_t page) 399 { 400 vm_pindex_t index, newind; 401 struct vm_radix_node *rnode, *tmp, *tmp2; 402 vm_page_t m; 403 int slot; 404 uint16_t clev; 405 406 index = page->pindex; 407 408 /* 409 * The owner of record for root is not really important because it 410 * will never be used. 411 */ 412 rnode = vm_radix_getroot(rtree); 413 if (rnode == NULL) { 414 rnode = vm_radix_node_get(0, 1, 0); 415 vm_radix_setroot(rtree, rnode); 416 vm_radix_addpage(rnode, index, 0, page); 417 return; 418 } 419 do { 420 slot = vm_radix_slot(index, rnode->rn_clev); 421 m = vm_radix_node_page(rnode->rn_child[slot]); 422 if (m != NULL) { 423 if (m->pindex == index) 424 panic("%s: key %jx is already present", 425 __func__, (uintmax_t)index); 426 clev = vm_radix_keydiff(m->pindex, index); 427 tmp = vm_radix_node_get(vm_radix_trimkey(index, 428 clev - 1), 2, clev); 429 rnode->rn_child[slot] = tmp; 430 vm_radix_addpage(tmp, index, clev, page); 431 vm_radix_addpage(tmp, m->pindex, clev, m); 432 return; 433 } 434 if (rnode->rn_child[slot] == NULL) { 435 rnode->rn_count++; 436 vm_radix_addpage(rnode, index, rnode->rn_clev, page); 437 return; 438 } 439 rnode = rnode->rn_child[slot]; 440 } while (!vm_radix_keybarr(rnode, index)); 441 442 /* 443 * Scan the trie from the top and find the parent to insert 444 * the new object. 445 */ 446 newind = rnode->rn_owner; 447 clev = vm_radix_keydiff(newind, index); 448 slot = VM_RADIX_COUNT; 449 for (rnode = vm_radix_getroot(rtree); ; rnode = tmp) { 450 KASSERT(rnode != NULL, ("%s: edge cannot be NULL in the scan", 451 __func__)); 452 KASSERT(clev >= rnode->rn_clev, 453 ("%s: unexpected trie depth: clev: %d, rnode->rn_clev: %d", 454 __func__, clev, rnode->rn_clev)); 455 slot = vm_radix_slot(index, rnode->rn_clev); 456 tmp = rnode->rn_child[slot]; 457 KASSERT(tmp != NULL && vm_radix_node_page(tmp) == NULL, 458 ("%s: unexpected lookup interruption", __func__)); 459 if (tmp->rn_clev > clev) 460 break; 461 } 462 KASSERT(rnode != NULL && tmp != NULL && slot < VM_RADIX_COUNT, 463 ("%s: invalid scan parameters rnode: %p, tmp: %p, slot: %d", 464 __func__, (void *)rnode, (void *)tmp, slot)); 465 466 /* 467 * A new node is needed because the right insertion level is reached. 468 * Setup the new intermediate node and add the 2 children: the 469 * new object and the older edge. 470 */ 471 tmp2 = vm_radix_node_get(vm_radix_trimkey(index, clev - 1), 2, 472 clev); 473 rnode->rn_child[slot] = tmp2; 474 vm_radix_addpage(tmp2, index, clev, page); 475 slot = vm_radix_slot(newind, clev); 476 tmp2->rn_child[slot] = tmp; 477 } 478 479 /* 480 * Returns the value stored at the index. If the index is not present, 481 * NULL is returned. 482 */ 483 vm_page_t 484 vm_radix_lookup(struct vm_radix *rtree, vm_pindex_t index) 485 { 486 struct vm_radix_node *rnode; 487 vm_page_t m; 488 int slot; 489 490 rnode = vm_radix_getroot(rtree); 491 while (rnode != NULL) { 492 if (vm_radix_keybarr(rnode, index)) 493 return (NULL); 494 slot = vm_radix_slot(index, rnode->rn_clev); 495 rnode = rnode->rn_child[slot]; 496 m = vm_radix_node_page(rnode); 497 if (m != NULL) { 498 if (m->pindex == index) 499 return (m); 500 else 501 return (NULL); 502 } 503 } 504 return (NULL); 505 } 506 507 /* 508 * Look up the nearest entry at a position bigger than or equal to index. 509 */ 510 vm_page_t 511 vm_radix_lookup_ge(struct vm_radix *rtree, vm_pindex_t index) 512 { 513 vm_pindex_t inc; 514 vm_page_t m; 515 struct vm_radix_node *rnode; 516 int slot; 517 uint16_t difflev; 518 boolean_t maplevels[VM_RADIX_LIMIT + 1]; 519 #ifdef INVARIANTS 520 int loops = 0; 521 #endif 522 523 restart: 524 KASSERT(++loops < 1000, ("%s: too many loops", __func__)); 525 for (difflev = 0; difflev < (VM_RADIX_LIMIT + 1); difflev++) 526 maplevels[difflev] = FALSE; 527 rnode = vm_radix_getroot(rtree); 528 while (rnode != NULL) { 529 maplevels[rnode->rn_clev] = TRUE; 530 531 /* 532 * If the keys differ before the current bisection node 533 * the search key might rollback to the earliest 534 * available bisection node, or to the smaller value 535 * in the current domain (if the owner is bigger than the 536 * search key). 537 * The maplevels array records any node has been seen 538 * at a given level. This aids the search for a valid 539 * bisection node. 540 */ 541 if (vm_radix_keybarr(rnode, index)) { 542 difflev = vm_radix_keydiff(index, rnode->rn_owner); 543 if (index > rnode->rn_owner) { 544 if (vm_radix_addlev(&index, maplevels, 545 difflev) > 0) 546 break; 547 } else 548 index = vm_radix_trimkey(rnode->rn_owner, 549 difflev); 550 goto restart; 551 } 552 slot = vm_radix_slot(index, rnode->rn_clev); 553 m = vm_radix_node_page(rnode->rn_child[slot]); 554 if (m != NULL && m->pindex >= index) 555 return (m); 556 if (rnode->rn_child[slot] != NULL && m == NULL) { 557 rnode = rnode->rn_child[slot]; 558 continue; 559 } 560 561 /* 562 * Look for an available edge or page within the current 563 * bisection node. 564 */ 565 if (slot < (VM_RADIX_COUNT - 1)) { 566 inc = VM_RADIX_UNITLEVEL(rnode->rn_clev); 567 index = vm_radix_trimkey(index, rnode->rn_clev); 568 index += inc; 569 slot++; 570 for (;; index += inc, slot++) { 571 m = vm_radix_node_page(rnode->rn_child[slot]); 572 if (m != NULL && m->pindex >= index) 573 return (m); 574 if ((rnode->rn_child[slot] != NULL && 575 m == NULL) || slot == (VM_RADIX_COUNT - 1)) 576 break; 577 } 578 } 579 580 /* 581 * If a valid page or edge bigger than the search slot is 582 * found in the traversal, skip to the next higher-level key. 583 */ 584 if (slot == (VM_RADIX_COUNT - 1) && 585 (rnode->rn_child[slot] == NULL || m != NULL)) { 586 if (rnode->rn_clev == 0 || vm_radix_addlev(&index, 587 maplevels, rnode->rn_clev - 1) > 0) 588 break; 589 goto restart; 590 } 591 rnode = rnode->rn_child[slot]; 592 } 593 return (NULL); 594 } 595 596 /* 597 * Look up the nearest entry at a position less than or equal to index. 598 */ 599 vm_page_t 600 vm_radix_lookup_le(struct vm_radix *rtree, vm_pindex_t index) 601 { 602 vm_pindex_t inc; 603 vm_page_t m; 604 struct vm_radix_node *rnode; 605 int slot; 606 uint16_t difflev; 607 boolean_t maplevels[VM_RADIX_LIMIT + 1]; 608 #ifdef INVARIANTS 609 int loops = 0; 610 #endif 611 612 restart: 613 KASSERT(++loops < 1000, ("%s: too many loops", __func__)); 614 for (difflev = 0; difflev < (VM_RADIX_LIMIT + 1); difflev++) 615 maplevels[difflev] = FALSE; 616 rnode = vm_radix_getroot(rtree); 617 while (rnode != NULL) { 618 maplevels[rnode->rn_clev] = TRUE; 619 620 /* 621 * If the keys differ before the current bisection node 622 * the search key might rollback to the earliest 623 * available bisection node, or to the higher value 624 * in the current domain (if the owner is smaller than the 625 * search key). 626 * The maplevels array records any node has been seen 627 * at a given level. This aids the search for a valid 628 * bisection node. 629 */ 630 if (vm_radix_keybarr(rnode, index)) { 631 difflev = vm_radix_keydiff(index, rnode->rn_owner); 632 if (index > rnode->rn_owner) { 633 index = vm_radix_trimkey(rnode->rn_owner, 634 difflev); 635 index |= VM_RADIX_UNITLEVEL(difflev) - 1; 636 } else if (vm_radix_declev(&index, maplevels, 637 difflev) > 0) 638 break; 639 goto restart; 640 } 641 slot = vm_radix_slot(index, rnode->rn_clev); 642 m = vm_radix_node_page(rnode->rn_child[slot]); 643 if (m != NULL && m->pindex <= index) 644 return (m); 645 if (rnode->rn_child[slot] != NULL && m == NULL) { 646 rnode = rnode->rn_child[slot]; 647 continue; 648 } 649 650 /* 651 * Look for an available edge or page within the current 652 * bisection node. 653 */ 654 if (slot > 0) { 655 inc = VM_RADIX_UNITLEVEL(rnode->rn_clev); 656 index = vm_radix_trimkey(index, rnode->rn_clev); 657 index |= inc - 1; 658 index -= inc; 659 slot--; 660 for (;; index -= inc, slot--) { 661 m = vm_radix_node_page(rnode->rn_child[slot]); 662 if (m != NULL && m->pindex <= index) 663 return (m); 664 if ((rnode->rn_child[slot] != NULL && 665 m == NULL) || slot == 0) 666 break; 667 } 668 } 669 670 /* 671 * If a valid page or edge smaller than the search slot is 672 * found in the traversal, skip to the next higher-level key. 673 */ 674 if (slot == 0 && (rnode->rn_child[slot] == NULL || m != NULL)) { 675 if (rnode->rn_clev == 0 || vm_radix_declev(&index, 676 maplevels, rnode->rn_clev - 1) > 0) 677 break; 678 goto restart; 679 } 680 rnode = rnode->rn_child[slot]; 681 } 682 return (NULL); 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 parent = NULL; 697 rnode = vm_radix_getroot(rtree); 698 for (;;) { 699 if (rnode == NULL) 700 panic("vm_radix_remove: impossible to locate the key"); 701 slot = vm_radix_slot(index, rnode->rn_clev); 702 m = vm_radix_node_page(rnode->rn_child[slot]); 703 if (m != NULL && m->pindex == index) { 704 rnode->rn_child[slot] = NULL; 705 rnode->rn_count--; 706 if (rnode->rn_count > 1) 707 break; 708 if (parent == NULL) { 709 if (rnode->rn_count == 0) { 710 vm_radix_node_put(rnode); 711 vm_radix_setroot(rtree, NULL); 712 } 713 break; 714 } 715 for (i = 0; i < VM_RADIX_COUNT; i++) 716 if (rnode->rn_child[i] != NULL) 717 break; 718 KASSERT(i != VM_RADIX_COUNT, 719 ("%s: invalid node configuration", __func__)); 720 slot = vm_radix_slot(index, parent->rn_clev); 721 KASSERT(parent->rn_child[slot] == rnode, 722 ("%s: invalid child value", __func__)); 723 parent->rn_child[slot] = rnode->rn_child[i]; 724 rnode->rn_count--; 725 rnode->rn_child[i] = NULL; 726 vm_radix_node_put(rnode); 727 break; 728 } 729 if (m != NULL && m->pindex != index) 730 panic("%s: invalid key found", __func__); 731 parent = rnode; 732 rnode = rnode->rn_child[slot]; 733 } 734 } 735 736 /* 737 * Remove and free all the nodes from the radix tree. 738 * This function is recursive but there is a tight control on it as the 739 * maximum depth of the tree is fixed. 740 */ 741 void 742 vm_radix_reclaim_allnodes(struct vm_radix *rtree) 743 { 744 struct vm_radix_node *root; 745 746 root = vm_radix_getroot(rtree); 747 if (root == NULL) 748 return; 749 vm_radix_setroot(rtree, NULL); 750 vm_radix_reclaim_allnodes_int(root); 751 } 752 753 #ifdef DDB 754 /* 755 * Show details about the given radix node. 756 */ 757 DB_SHOW_COMMAND(radixnode, db_show_radixnode) 758 { 759 struct vm_radix_node *rnode; 760 int i; 761 762 if (!have_addr) 763 return; 764 rnode = (struct vm_radix_node *)addr; 765 db_printf("radixnode %p, owner %jx, children count %u, level %u:\n", 766 (void *)rnode, (uintmax_t)rnode->rn_owner, rnode->rn_count, 767 rnode->rn_clev); 768 for (i = 0; i < VM_RADIX_COUNT; i++) 769 if (rnode->rn_child[i] != NULL) 770 db_printf("slot: %d, val: %p, page: %p, clev: %d\n", 771 i, (void *)rnode->rn_child[i], 772 (void *)vm_radix_node_page(rnode->rn_child[i]), 773 rnode->rn_clev); 774 } 775 #endif /* DDB */ 776