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