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