summaryrefslogtreecommitdiffstats
path: root/sys/vm/vm_radix.c
blob: 96d9d8bd8e7edf3141db413184ce56e7138d18e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
/*
 * Copyright (c) 2011 Jeffrey Roberson <jeff@freebsd.org>
 * Copyright (c) 2008 Mayur Shardul <mayur.shardul@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */


/*
 * Radix tree implementation.
 */

#include <sys/cdefs.h>

#include <sys/param.h>
#include <sys/conf.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/queue.h>
#include <sys/param.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/ktr.h>
#include <vm/uma.h>
#include <vm/vm.h>
#include <vm/vm_param.h>
#include <vm/vm_extern.h>
#include <vm/vm_kern.h>
#include <vm/vm_page.h>
#ifndef UMA_MD_SMALL_ALLOC
#include <vm/vm_map.h>
#endif
#include <vm/vm_radix.h>
#include <vm/vm_object.h>

#include <sys/kdb.h>

#ifndef UMA_MD_SMALL_ALLOC
#define	VM_RADIX_RNODE_MAP_SCALE	(1024 * 1024 / 2)
#define	VM_RADIX_WIDTH	4

/*
 * Bits of height in root.
 * The mask of smaller power of 2 containing VM_RADIX_LIMIT.
 */
#define	VM_RADIX_HEIGHT	0x1f
#else
#define	VM_RADIX_WIDTH	5

/* See the comment above. */
#define	VM_RADIX_HEIGHT	0xf
#endif

#define	VM_RADIX_COUNT	(1 << VM_RADIX_WIDTH)
#define	VM_RADIX_MASK	(VM_RADIX_COUNT - 1)
#define	VM_RADIX_MAXVAL	((vm_pindex_t)-1)
#define	VM_RADIX_LIMIT	howmany((sizeof(vm_pindex_t) * NBBY), VM_RADIX_WIDTH)

/* Flag bits stored in node pointers. */
#define VM_RADIX_FLAGS	0x3

/* Calculates maximum value for a tree of height h. */
#define	VM_RADIX_MAX(h)							\
	    ((h) == VM_RADIX_LIMIT ? VM_RADIX_MAXVAL :			\
	    (((vm_pindex_t)1 << ((h) * VM_RADIX_WIDTH)) - 1))

/*
 * On 32-bits architectures KTR cannot handle 64-bits values.
 * Add macros for splitting in 32-bits quantity and provide format strings.
 * Note that braces are not used because they would break compilation.
 * Also, note that arguments are cast to u_long in order to follow KTR
 * convention.
 */
#ifdef KTR
#define	KFRMT64(x)	__STRING(x)"l 0x%08lx, "__STRING(x)"h 0x%08lx"
#define	KSPLT64L(x)	((u_long)((x) & 0xFFFFFFFF))
#define	KSPLT64H(x)	((u_long)(((x) >> 32) & 0xFFFFFFFF))
#endif

CTASSERT(VM_RADIX_HEIGHT >= VM_RADIX_LIMIT);

struct vm_radix_node {
	void		*rn_child[VM_RADIX_COUNT];	/* Child nodes. */
	volatile uint32_t rn_count;			/* Valid children. */
};

CTASSERT(sizeof(struct vm_radix_node) < PAGE_SIZE);

static uma_zone_t vm_radix_node_zone;

#ifndef UMA_MD_SMALL_ALLOC
static vm_map_t rnode_map;
static u_long rnode_map_scale;

static void *
vm_radix_node_zone_allocf(uma_zone_t zone, int size, uint8_t *flags, int wait)
{
	vm_offset_t addr;
	vm_page_t m;
	int pflags;

	/* Inform UMA that this allocator uses rnode_map. */
	*flags = UMA_SLAB_KERNEL;

	pflags = VM_ALLOC_WIRED | VM_ALLOC_NOOBJ;

	/*
	 * As kmem_alloc_nofault() can however fail, let just assume that
	 * M_NOWAIT is on and act accordingly.
	 */
	pflags |= ((wait & M_USE_RESERVE) != 0) ? VM_ALLOC_INTERRUPT :
	    VM_ALLOC_SYSTEM;
	if ((wait & M_ZERO) != 0)
		pflags |= VM_ALLOC_ZERO; 
	addr = kmem_alloc_nofault(rnode_map, size);
	if (addr == 0)
		return (NULL);

	/* Just one page allocation is assumed here. */
	m = vm_page_alloc(NULL, OFF_TO_IDX(addr - VM_MIN_KERNEL_ADDRESS),
	    pflags);
	if (m == NULL) {
		kmem_free(rnode_map, addr, size);
		return (NULL);
	}
	if ((wait & M_ZERO) != 0 && (m->flags & PG_ZERO) == 0)
		pmap_zero_page(m);
	pmap_qenter(addr, &m, 1);
	return ((void *)addr);
}

static void
vm_radix_node_zone_freef(void *item, int size, uint8_t flags)
{
	vm_page_t m;
	vm_offset_t voitem;

	MPASS((flags & UMA_SLAB_KERNEL) != 0);

	/* Just one page allocation is assumed here. */
	voitem = (vm_offset_t)item;
	m = PHYS_TO_VM_PAGE(pmap_kextract(voitem));
	pmap_qremove(voitem, 1);
	vm_page_lock(m);
	vm_page_unwire(m, 0);
	vm_page_free(m);
	vm_page_unlock(m);
	kmem_free(rnode_map, voitem, size);
}

static void
init_vm_radix_alloc(void *dummy __unused)
{

	uma_zone_set_max(vm_radix_node_zone, rnode_map_scale);
	uma_zone_set_allocf(vm_radix_node_zone, vm_radix_node_zone_allocf);
	uma_zone_set_freef(vm_radix_node_zone, vm_radix_node_zone_freef);
}
SYSINIT(vm_radix, SI_SUB_KMEM, SI_ORDER_SECOND, init_vm_radix_alloc, NULL);
#endif

/*
 * Radix node zone destructor.
 */
#ifdef INVARIANTS
static void
vm_radix_node_zone_dtor(void *mem, int size, void *arg)
{
	struct vm_radix_node *rnode;

	rnode = mem;
	KASSERT(rnode->rn_count == 0,
	    ("vm_radix_node_put: Freeing a node with %d children\n",
	    rnode->rn_count));
}
#endif

/*
 * Allocate a radix node.  Initializes all elements to 0.
 */
static __inline struct vm_radix_node *
vm_radix_node_get(void)
{

	return (uma_zalloc(vm_radix_node_zone, M_NOWAIT | M_ZERO));
}

/*
 * Free radix node.
 */
static __inline void
vm_radix_node_put(struct vm_radix_node *rnode)
{

	uma_zfree(vm_radix_node_zone, rnode);
}

/*
 * Return the position in the array for a given level.
 */
static __inline int
vm_radix_slot(vm_pindex_t index, int level)
{

	return ((index >> (level * VM_RADIX_WIDTH)) & VM_RADIX_MASK);
}

/*
 * Initialize the radix node submap (for architectures not supporting
 * direct-mapping) and the radix node zone.
 *
 * WITNESS reports a lock order reversal, for architectures not
 * supporting direct-mapping,  between the "system map" lock
 * and the "vm object" lock.  This is because the well established ordering
 * "system map" -> "vm object" is not honoured in this case as allocating
 * from the radix node submap ends up adding a mapping entry to it, meaning
 * it is necessary to lock the submap.  However, the radix node submap is
 * a leaf and self-contained, thus a deadlock cannot happen here and
 * adding MTX_NOWITNESS to all map locks would be largerly sub-optimal.
 */
void
vm_radix_init(void)
{
#ifndef UMA_MD_SMALL_ALLOC
	vm_offset_t maxaddr, minaddr;

	rnode_map_scale = VM_RADIX_RNODE_MAP_SCALE;
	TUNABLE_ULONG_FETCH("hw.rnode_map_scale", &rnode_map_scale);
	rnode_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
	    rnode_map_scale * sizeof(struct vm_radix_node), FALSE);
	rnode_map->system_map = 1;
#endif

	vm_radix_node_zone = uma_zcreate("RADIX NODE",
	    sizeof(struct vm_radix_node), NULL,
#ifdef INVARIANTS
	    vm_radix_node_zone_dtor,
#else
	    NULL,
#endif
	    NULL, NULL, VM_RADIX_HEIGHT, UMA_ZONE_VM);
}

/*
 * Extract the root node and height from a radix tree with a single load.
 */
static __inline int
vm_radix_height(struct vm_radix *rtree, struct vm_radix_node **rnode)
{
	uintptr_t root;
	int height;

	root = rtree->rt_root;
	height = root & VM_RADIX_HEIGHT;
	*rnode = (struct vm_radix_node *)(root - height);
	return (height);
}


/*
 * Set the root node and height for a radix tree.
 */
static inline void
vm_radix_setroot(struct vm_radix *rtree, struct vm_radix_node *rnode,
    int height)
{
	uintptr_t root;

	root = (uintptr_t)rnode | height;
	rtree->rt_root = root;
}

static inline void *
vm_radix_match(void *child)
{
	uintptr_t c;

	c = (uintptr_t)child;

	return ((void *)(c & ~VM_RADIX_FLAGS));
}

static void
vm_radix_reclaim_allnodes_internal(struct vm_radix_node *rnode, int level)
{
	int slot;

	MPASS(rnode != NULL && level >= 0);

	/*
	 * Level 0 just contains pages as children, thus make it a special
	 * case, free the node and return.
	 */
	if (level == 0) {
		CTR2(KTR_VM, "reclaiming: node %p, level %d", rnode, level);
		rnode->rn_count = 0;
		vm_radix_node_put(rnode);
		return;
	}
	for (slot = 0; slot < VM_RADIX_COUNT && rnode->rn_count != 0; slot++) {
		if (rnode->rn_child[slot] == NULL)
			continue;
		CTR3(KTR_VM,
		    "reclaiming: node %p, level %d recursing in slot %d",
		    rnode, level, slot);
		vm_radix_reclaim_allnodes_internal(rnode->rn_child[slot],
		    level - 1);
		rnode->rn_count--;
	}
	MPASS(rnode->rn_count == 0);
	CTR2(KTR_VM, "reclaiming: node %p, level %d", rnode, level);
	vm_radix_node_put(rnode);
}

/*
 * Inserts the key-value pair in to the radix tree.  Returns errno.
 * Panics if the key already exists.
 */
int
vm_radix_insert(struct vm_radix *rtree, vm_pindex_t index, void *val)
{
	struct vm_radix_node *rnode;
	struct vm_radix_node *root;
	int level;
	int slot;

	CTR4(KTR_VM,
	    "insert: tree %p, " KFRMT64(index) ", val %p", rtree,
	    KSPLT64L(index), KSPLT64H(index), val);
	if (index == -1)
		panic("vm_radix_insert: -1 is not a valid index.\n");
	level = vm_radix_height(rtree, &root);
	/*
	 * Increase the height by adding nodes at the root until
	 * there is sufficient space.
	 */
	while (level == 0 || index > VM_RADIX_MAX(level)) {
		CTR5(KTR_VM,
	    "insert: expanding " KFRMT64(index) ">" KFRMT64(mxl) ", height %d",
		    KSPLT64L(index), KSPLT64H(index),
		    KSPLT64L(VM_RADIX_MAX(level)),
		    KSPLT64H(VM_RADIX_MAX(level)), level);
		level++;
		KASSERT(level <= VM_RADIX_LIMIT,
		    ("vm_radix_insert: Tree %p height %d too tall",
		    rtree, level));
		/*
		 * Only allocate tree nodes if they are needed.
		 */
		if (root == NULL || root->rn_count != 0) {
			rnode = vm_radix_node_get();
			if (rnode == NULL) {
				CTR5(KTR_VM,
	    "insert: tree %p, root %p, " KFRMT64(index) ", level %d ENOMEM",
				    rtree, root, KSPLT64L(index),
				    KSPLT64H(index), level);
				return (ENOMEM);
			}
			/*
			 * Store the new pointer with a memory barrier so
			 * that it is visible before the new root.
			 */
			if (root) {
				atomic_store_rel_ptr((volatile uintptr_t *)
				    &rnode->rn_child[0], (uintptr_t)root);
				rnode->rn_count = 1;
			}
			root = rnode;
		}
		vm_radix_setroot(rtree, root, level);
	}

	/* Now that the tree is tall enough, fill in the path to the index. */
	rnode = root;
	for (level = level - 1; level > 0; level--) {
		slot = vm_radix_slot(index, level);
		/* Add the required intermidiate nodes. */
		if (rnode->rn_child[slot] == NULL) {
			rnode->rn_child[slot] = vm_radix_node_get();
    			if (rnode->rn_child[slot] == NULL) {
				CTR6(KTR_VM,
    "insert: tree %p, " KFRMT64(index) ", level %d, slot %d, rnode %p ENOMEM",
				     rtree, KSPLT64L(index), KSPLT64H(index),
				    level, slot, rnode);
				CTR4(KTR_VM,
			"insert: tree %p, rnode %p, child %p, count %u ENOMEM",
		    		    rtree, rnode, rnode->rn_child[slot],
				    rnode->rn_count);
				return (ENOMEM);
			}
			rnode->rn_count++;
	    	}
		CTR6(KTR_VM,
	    "insert: tree %p, " KFRMT64(index) ", level %d, slot %d, rnode %p",
		    rtree, KSPLT64L(index), KSPLT64H(index), level, slot,
		    rnode);
		CTR4(KTR_VM,
		    "insert: tree %p, rnode %p, child %p, count %u",
		    rtree, rnode, rnode->rn_child[slot], rnode->rn_count);
		rnode = rnode->rn_child[slot];
	}

	slot = vm_radix_slot(index, 0);
	MPASS(rnode != NULL);
	KASSERT(rnode->rn_child[slot] == NULL,
	    ("vm_radix_insert: Duplicate value %p at index: %lu\n", 
	    rnode->rn_child[slot], (u_long)index));
	rnode->rn_child[slot] = val;
	rnode->rn_count++;
	CTR5(KTR_VM,
	    "insert: tree %p, " KFRMT64(index) ", level %d, slot %d",
	    rtree, KSPLT64L(index), KSPLT64H(index), level, slot);
	CTR3(KTR_VM, "insert: slot %d, rnode %p, count %u", slot, rnode,
	    rnode->rn_count);

	return 0;
}

/*
 * Returns the value stored at the index.  If the index is not present
 * NULL is returned.
 */
void *
vm_radix_lookup(struct vm_radix *rtree, vm_pindex_t index)
{
	struct vm_radix_node *rnode;
	int slot;
	int level;

	level = vm_radix_height(rtree, &rnode);
	if (index > VM_RADIX_MAX(level))
		return NULL;
	level--;
	while (rnode) {
		slot = vm_radix_slot(index, level);
		CTR6(KTR_VM,
	    "lookup: tree %p, " KFRMT64(index) ", level %d, slot %d, rnode %p",
		    rtree, KSPLT64L(index), KSPLT64H(index), level, slot,
		    rnode);
		CTR2(KTR_VM, "lookup: rnode %p, child %p", rnode,
		    rnode->rn_child[slot]);
		if (level == 0)
			return vm_radix_match(rnode->rn_child[slot]);
		rnode = rnode->rn_child[slot];
		level--;
	}
	CTR3(KTR_VM, "lookup: tree %p, " KFRMT64(index) " failed", rtree,
	     KSPLT64L(index), KSPLT64H(index));

	return NULL;
}

/*
 * Find the first leaf with a valid node between *startp and end.  Return
 * the index of the first valid item in the leaf in *startp.
 */
static struct vm_radix_node *
vm_radix_leaf(struct vm_radix *rtree, vm_pindex_t *startp)
{
	struct vm_radix_node *rnode;
	vm_pindex_t start;
	vm_pindex_t inc;
	int slot;
	int level;

	start = *startp;
restart:
	level = vm_radix_height(rtree, &rnode);
	if (start > VM_RADIX_MAX(level)) {
		rnode = NULL;
		goto out;
	}
	/*
	 * Search the tree from the top for any leaf node holding an index
	 * between start and maxval.
	 */
	for (level--; level; level--) {
		slot = vm_radix_slot(start, level);
		CTR6(KTR_VM,
	    "leaf: tree %p, " KFRMT64(start) ", level %d, slot %d, rnode %p",
		    rtree, KSPLT64L(start), KSPLT64H(start), level, slot,
		    rnode);
		CTR2(KTR_VM, "leaf: rnode %p, child %p", rnode,
		    rnode->rn_child[slot]);
		if (rnode->rn_child[slot] != NULL) {
			rnode = rnode->rn_child[slot];
			continue;
		}
		/*
		 * Calculate how much to increment our index by
		 * based on the tree level.  We must truncate the
		 * lower bits to start from the begnning of the
		 * next leaf.
	 	 */
		inc = 1LL << (level * VM_RADIX_WIDTH);
		start &= ~VM_RADIX_MAX(level);

		/* Avoid start address wrapping up. */
		if ((VM_RADIX_MAXVAL - start) < inc) {
			rnode = NULL;
			goto out;
		}
		start += inc;
		slot++;
		CTR4(KTR_VM,
		    "leaf: " KFRMT64(start) ", " KFRMT64(inc),
		    KSPLT64L(start), KSPLT64H(start), KSPLT64L(inc),
		    KSPLT64H(inc));
		CTR2(KTR_VM, "leaf: level %d, slot %d", level, slot);
		for (; slot < VM_RADIX_COUNT; slot++, start += inc) {
			if (rnode->rn_child[slot]) {
				rnode = rnode->rn_child[slot];
				break;
			}
			if ((VM_RADIX_MAXVAL - start) < inc) {
				rnode = NULL;
				goto out;
			}
		}
		if (slot == VM_RADIX_COUNT)
			goto restart;
	}

out:
	*startp = start;
	return (rnode);
}

/*
 * Look up any entry at a position bigger than or equal to index.
 */
void *
vm_radix_lookup_ge(struct vm_radix *rtree, vm_pindex_t start)
{
	struct vm_radix_node *rnode;
	void *val;
	int slot;

	CTR3(KTR_VM, "lookupn: tree %p, " KFRMT64(start), rtree,
	    KSPLT64L(start), KSPLT64H(start));
	if (rtree->rt_root == 0)
		return (NULL);
	while ((rnode = vm_radix_leaf(rtree, &start)) != NULL) {
		slot = vm_radix_slot(start, 0);
		for (; slot < VM_RADIX_COUNT; slot++, start++) {
			val = vm_radix_match(rnode->rn_child[slot]);
			if (val == NULL) {

				/*
				 * The start address can wrap at the
				 * VM_RADIX_MAXVAL value.
				 * We need to make sure that start address
				 * point to the next chunk (even if wrapping)
				 * to stay consistent with default scanning
				 * behaviour. Also, because of the nature
				 * of the wrapping, the wrap up checks must
				 * be done after all the necessary controls
				 * on start are completed.
				 */
				if ((VM_RADIX_MAXVAL - start) == 0)
					return (NULL);
				continue;
			}
			CTR5(KTR_VM,
	    "lookupn: tree %p " KFRMT64(index) " slot %d found child %p",
			    rtree, KSPLT64L(start), KSPLT64H(start), slot, val);
			return (val);
		} 
		MPASS((VM_RADIX_MAXVAL - start) != 0);
	}
	return (NULL);
}

/*
 * Look up any entry at a position less than or equal to index.
 */
void *
vm_radix_lookup_le(struct vm_radix *rtree, vm_pindex_t index)
{
	struct vm_radix_node *rnode;
	struct vm_radix_node *child;
	vm_pindex_t max;
	vm_pindex_t inc;
	void *val;
	int slot;
	int level;

	CTR3(KTR_VM, "lookup_le: tree %p, " KFRMT64(index),  rtree,
	    KSPLT64L(index), KSPLT64H(index));
restart:
	level = vm_radix_height(rtree, &rnode);
	if (rnode == NULL)
		return (NULL);
	max = VM_RADIX_MAX(level);
	if (index > max || index == 0)
		index = max;
	/*
	 * Search the tree from the top for any leaf node holding an index
	 * lower than 'index'.
	 */
	level--;
	while (rnode) {
		slot = vm_radix_slot(index, level);
		CTR6(KTR_VM,
	"lookup_le: tree %p, " KFRMT64(index) ", level %d, slot %d, rnode %p",
		    rtree, KSPLT64L(index), KSPLT64H(index), level, slot,
		    rnode);
		CTR2(KTR_VM, "lookup_le:  rnode %p, child %p", rnode,
		    rnode->rn_child[slot]);
		if (level == 0)
			break;
		/*
		 * If we don't have an exact match we must start our search
		 * from the next leaf and adjust our index appropriately.
		 */
		if ((child = rnode->rn_child[slot]) == NULL) {
			/*
			 * Calculate how much to decrement our index by
			 * based on the tree level.  We must set the
			 * lower bits to start from the end of the next
			 * leaf.
		 	 */
			inc = 1LL << (level * VM_RADIX_WIDTH);
			index |= VM_RADIX_MAX(level);
			index -= inc;
			slot--;
			CTR6(KTR_VM,
	    "lookup_le: " KFRMT64(start) ", " KFRMT64(inc) ", level %d slot %d",
			    KSPLT64L(index), KSPLT64H(index), KSPLT64L(inc),
			    KSPLT64H(inc), level, slot);
			for (; slot >= 0; slot--, index -= inc) {
				child = rnode->rn_child[slot];
				if (child)
					break;
			}
		}
		rnode = child;
		level--;
	}
	if (rnode) {
		for (; slot >= 0; slot--, index--) {
			val = vm_radix_match(rnode->rn_child[slot]);
			if (val)
				return (val);
		}
	}
	if (index != -1)
		goto restart;
	return (NULL);
}

/*
 * Remove the specified index from the tree.  If possible the height of the
 * tree is adjusted after deletion.  The value stored at index is returned
 * panics if the key is not present.
 */
void
vm_radix_remove(struct vm_radix *rtree, vm_pindex_t index)
{
	struct vm_radix_node *stack[VM_RADIX_LIMIT];
	struct vm_radix_node *rnode, *root;
	int level;
	int slot;

	level = vm_radix_height(rtree, &root);
	KASSERT(index <= VM_RADIX_MAX(level),
	    ("vm_radix_remove: %p index out of range %jd.", rtree,
	    VM_RADIX_MAX(level)));
	rnode = root;
	level--;
	/*
	 * Find the node and record the path in stack.
	 */
	while (level && rnode) {
		stack[level] = rnode;
		slot = vm_radix_slot(index, level);
		CTR6(KTR_VM,
	    "remove: tree %p, " KFRMT64(index) ", level %d, slot %d, rnode %p",
		    rtree, KSPLT64L(index), KSPLT64H(index), level, slot,
		    rnode);
		CTR4(KTR_VM, "remove: tree %p, rnode %p, child %p, count %u",
		    rtree, rnode, rnode->rn_child[slot], rnode->rn_count);
		rnode = rnode->rn_child[slot];
		level--;
	}
	KASSERT(rnode != NULL,
		("vm_radix_remove: index not present in the tree.\n"));
	slot = vm_radix_slot(index, 0);
	KASSERT(vm_radix_match(rnode->rn_child[slot]) != NULL,
		("vm_radix_remove: index not present in the tree.\n"));

	for (;;) {
		CTR6(KTR_VM,
"remove: resetting tree %p, " KFRMT64(index) ", level %d, slot %d, rnode %p",
		    rtree, KSPLT64L(index), KSPLT64H(index), level, slot,
		    rnode);
		CTR4(KTR_VM,
		    "remove: resetting tree %p, rnode %p, child %p, count %u",
		    rtree, rnode,
		    (rnode != NULL) ? rnode->rn_child[slot] : NULL,
		    (rnode != NULL) ? rnode->rn_count : 0);
		rnode->rn_child[slot] = NULL;
		/*
		 * Use a write memory barrier here in order to avoid
		 * rn_count reaching 0 before to fetch the actual pointer.
		 * Concurrent node removal, infact, may want to reclaim
		 * the radix node itself before to read it.
		 */
		rnode->rn_count--;
		wmb();
		if (rnode->rn_count > 0)
			break;
		vm_radix_node_put(rnode);
		if (rnode == root) {
			vm_radix_setroot(rtree, NULL, 0);
			break;
		}
		rnode = stack[++level];
		slot = vm_radix_slot(index, level);
			
	}
}

/*
 * Remove and free all the nodes from the radix tree.
 * This function is recrusive but there is a tight control on it as the
 * maximum depth of the tree is fixed.
 */
void
vm_radix_reclaim_allnodes(struct vm_radix *rtree)
{
	struct vm_radix_node *root;
	int level;

	if (rtree->rt_root == 0)
		return;
	level = vm_radix_height(rtree, &root);
	vm_radix_reclaim_allnodes_internal(root, level - 1);
	rtree->rt_root = 0;
}
OpenPOWER on IntegriCloud