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