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