xref: /freebsd/sys/vm/vm_reserv.c (revision 5bf5ca772c6de2d53344a78cf461447cc322ccea)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2002-2006 Rice University
5  * Copyright (c) 2007-2011 Alan L. Cox <alc@cs.rice.edu>
6  * All rights reserved.
7  *
8  * This software was developed for the FreeBSD Project by Alan L. Cox,
9  * Olivier Crameri, Peter Druschel, Sitaram Iyer, and Juan Navarro.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
24  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
30  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  *	Superpage reservation management module
36  *
37  * Any external functions defined by this module are only to be used by the
38  * virtual memory system.
39  */
40 
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #include "opt_vm.h"
45 
46 #include <sys/param.h>
47 #include <sys/kernel.h>
48 #include <sys/lock.h>
49 #include <sys/malloc.h>
50 #include <sys/mutex.h>
51 #include <sys/queue.h>
52 #include <sys/rwlock.h>
53 #include <sys/sbuf.h>
54 #include <sys/sysctl.h>
55 #include <sys/systm.h>
56 #include <sys/vmmeter.h>
57 
58 #include <vm/vm.h>
59 #include <vm/vm_param.h>
60 #include <vm/vm_object.h>
61 #include <vm/vm_page.h>
62 #include <vm/vm_pageout.h>
63 #include <vm/vm_phys.h>
64 #include <vm/vm_pagequeue.h>
65 #include <vm/vm_radix.h>
66 #include <vm/vm_reserv.h>
67 
68 /*
69  * The reservation system supports the speculative allocation of large physical
70  * pages ("superpages").  Speculative allocation enables the fully automatic
71  * utilization of superpages by the virtual memory system.  In other words, no
72  * programmatic directives are required to use superpages.
73  */
74 
75 #if VM_NRESERVLEVEL > 0
76 
77 /*
78  * The number of small pages that are contained in a level 0 reservation
79  */
80 #define	VM_LEVEL_0_NPAGES	(1 << VM_LEVEL_0_ORDER)
81 
82 /*
83  * The number of bits by which a physical address is shifted to obtain the
84  * reservation number
85  */
86 #define	VM_LEVEL_0_SHIFT	(VM_LEVEL_0_ORDER + PAGE_SHIFT)
87 
88 /*
89  * The size of a level 0 reservation in bytes
90  */
91 #define	VM_LEVEL_0_SIZE		(1 << VM_LEVEL_0_SHIFT)
92 
93 /*
94  * Computes the index of the small page underlying the given (object, pindex)
95  * within the reservation's array of small pages.
96  */
97 #define	VM_RESERV_INDEX(object, pindex)	\
98     (((object)->pg_color + (pindex)) & (VM_LEVEL_0_NPAGES - 1))
99 
100 /*
101  * The size of a population map entry
102  */
103 typedef	u_long		popmap_t;
104 
105 /*
106  * The number of bits in a population map entry
107  */
108 #define	NBPOPMAP	(NBBY * sizeof(popmap_t))
109 
110 /*
111  * The number of population map entries in a reservation
112  */
113 #define	NPOPMAP		howmany(VM_LEVEL_0_NPAGES, NBPOPMAP)
114 
115 /*
116  * Clear a bit in the population map.
117  */
118 static __inline void
119 popmap_clear(popmap_t popmap[], int i)
120 {
121 
122 	popmap[i / NBPOPMAP] &= ~(1UL << (i % NBPOPMAP));
123 }
124 
125 /*
126  * Set a bit in the population map.
127  */
128 static __inline void
129 popmap_set(popmap_t popmap[], int i)
130 {
131 
132 	popmap[i / NBPOPMAP] |= 1UL << (i % NBPOPMAP);
133 }
134 
135 /*
136  * Is a bit in the population map clear?
137  */
138 static __inline boolean_t
139 popmap_is_clear(popmap_t popmap[], int i)
140 {
141 
142 	return ((popmap[i / NBPOPMAP] & (1UL << (i % NBPOPMAP))) == 0);
143 }
144 
145 /*
146  * Is a bit in the population map set?
147  */
148 static __inline boolean_t
149 popmap_is_set(popmap_t popmap[], int i)
150 {
151 
152 	return ((popmap[i / NBPOPMAP] & (1UL << (i % NBPOPMAP))) != 0);
153 }
154 
155 /*
156  * The reservation structure
157  *
158  * A reservation structure is constructed whenever a large physical page is
159  * speculatively allocated to an object.  The reservation provides the small
160  * physical pages for the range [pindex, pindex + VM_LEVEL_0_NPAGES) of offsets
161  * within that object.  The reservation's "popcnt" tracks the number of these
162  * small physical pages that are in use at any given time.  When and if the
163  * reservation is not fully utilized, it appears in the queue of partially
164  * populated reservations.  The reservation always appears on the containing
165  * object's list of reservations.
166  *
167  * A partially populated reservation can be broken and reclaimed at any time.
168  *
169  * f - vm_domain_free_lock
170  * o - vm_reserv_object_lock
171  * c - constant after boot
172  */
173 struct vm_reserv {
174 	TAILQ_ENTRY(vm_reserv) partpopq;	/* (f) per-domain queue. */
175 	LIST_ENTRY(vm_reserv) objq;		/* (o, f) object queue */
176 	vm_object_t	object;			/* (o, f) containing object */
177 	vm_pindex_t	pindex;			/* (o, f) offset in object */
178 	vm_page_t	pages;			/* (c) first page  */
179 	int		domain;			/* (c) NUMA domain. */
180 	int		popcnt;			/* (f) # of pages in use */
181 	char		inpartpopq;		/* (f) */
182 	popmap_t	popmap[NPOPMAP];	/* (f) bit vector, used pages */
183 };
184 
185 /*
186  * The reservation array
187  *
188  * This array is analoguous in function to vm_page_array.  It differs in the
189  * respect that it may contain a greater number of useful reservation
190  * structures than there are (physical) superpages.  These "invalid"
191  * reservation structures exist to trade-off space for time in the
192  * implementation of vm_reserv_from_page().  Invalid reservation structures are
193  * distinguishable from "valid" reservation structures by inspecting the
194  * reservation's "pages" field.  Invalid reservation structures have a NULL
195  * "pages" field.
196  *
197  * vm_reserv_from_page() maps a small (physical) page to an element of this
198  * array by computing a physical reservation number from the page's physical
199  * address.  The physical reservation number is used as the array index.
200  *
201  * An "active" reservation is a valid reservation structure that has a non-NULL
202  * "object" field and a non-zero "popcnt" field.  In other words, every active
203  * reservation belongs to a particular object.  Moreover, every active
204  * reservation has an entry in the containing object's list of reservations.
205  */
206 static vm_reserv_t vm_reserv_array;
207 
208 /*
209  * The partially populated reservation queue
210  *
211  * This queue enables the fast recovery of an unused free small page from a
212  * partially populated reservation.  The reservation at the head of this queue
213  * is the least recently changed, partially populated reservation.
214  *
215  * Access to this queue is synchronized by the free page queue lock.
216  */
217 static TAILQ_HEAD(, vm_reserv) vm_rvq_partpop[MAXMEMDOM];
218 
219 static SYSCTL_NODE(_vm, OID_AUTO, reserv, CTLFLAG_RD, 0, "Reservation Info");
220 
221 static long vm_reserv_broken;
222 SYSCTL_LONG(_vm_reserv, OID_AUTO, broken, CTLFLAG_RD,
223     &vm_reserv_broken, 0, "Cumulative number of broken reservations");
224 
225 static long vm_reserv_freed;
226 SYSCTL_LONG(_vm_reserv, OID_AUTO, freed, CTLFLAG_RD,
227     &vm_reserv_freed, 0, "Cumulative number of freed reservations");
228 
229 static int sysctl_vm_reserv_fullpop(SYSCTL_HANDLER_ARGS);
230 
231 SYSCTL_PROC(_vm_reserv, OID_AUTO, fullpop, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
232     sysctl_vm_reserv_fullpop, "I", "Current number of full reservations");
233 
234 static int sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS);
235 
236 SYSCTL_OID(_vm_reserv, OID_AUTO, partpopq, CTLTYPE_STRING | CTLFLAG_RD, NULL, 0,
237     sysctl_vm_reserv_partpopq, "A", "Partially populated reservation queues");
238 
239 static long vm_reserv_reclaimed;
240 SYSCTL_LONG(_vm_reserv, OID_AUTO, reclaimed, CTLFLAG_RD,
241     &vm_reserv_reclaimed, 0, "Cumulative number of reclaimed reservations");
242 
243 /*
244  * The object lock pool is used to synchronize the rvq.  We can not use a
245  * pool mutex because it is required before malloc works.
246  *
247  * The "hash" function could be made faster without divide and modulo.
248  */
249 #define	VM_RESERV_OBJ_LOCK_COUNT	MAXCPU
250 
251 struct mtx_padalign vm_reserv_object_mtx[VM_RESERV_OBJ_LOCK_COUNT];
252 
253 #define	vm_reserv_object_lock_idx(object)			\
254 	    (((uintptr_t)object / sizeof(*object)) % VM_RESERV_OBJ_LOCK_COUNT)
255 #define	vm_reserv_object_lock_ptr(object)			\
256 	    &vm_reserv_object_mtx[vm_reserv_object_lock_idx((object))]
257 #define	vm_reserv_object_lock(object)				\
258 	    mtx_lock(vm_reserv_object_lock_ptr((object)))
259 #define	vm_reserv_object_unlock(object)				\
260 	    mtx_unlock(vm_reserv_object_lock_ptr((object)))
261 
262 static void		vm_reserv_break(vm_reserv_t rv);
263 static void		vm_reserv_depopulate(vm_reserv_t rv, int index);
264 static vm_reserv_t	vm_reserv_from_page(vm_page_t m);
265 static boolean_t	vm_reserv_has_pindex(vm_reserv_t rv,
266 			    vm_pindex_t pindex);
267 static void		vm_reserv_populate(vm_reserv_t rv, int index);
268 static void		vm_reserv_reclaim(vm_reserv_t rv);
269 
270 /*
271  * Returns the current number of full reservations.
272  *
273  * Since the number of full reservations is computed without acquiring the
274  * free page queue lock, the returned value may be inexact.
275  */
276 static int
277 sysctl_vm_reserv_fullpop(SYSCTL_HANDLER_ARGS)
278 {
279 	vm_paddr_t paddr;
280 	struct vm_phys_seg *seg;
281 	vm_reserv_t rv;
282 	int fullpop, segind;
283 
284 	fullpop = 0;
285 	for (segind = 0; segind < vm_phys_nsegs; segind++) {
286 		seg = &vm_phys_segs[segind];
287 		paddr = roundup2(seg->start, VM_LEVEL_0_SIZE);
288 		while (paddr + VM_LEVEL_0_SIZE <= seg->end) {
289 			rv = &vm_reserv_array[paddr >> VM_LEVEL_0_SHIFT];
290 			fullpop += rv->popcnt == VM_LEVEL_0_NPAGES;
291 			paddr += VM_LEVEL_0_SIZE;
292 		}
293 	}
294 	return (sysctl_handle_int(oidp, &fullpop, 0, req));
295 }
296 
297 /*
298  * Describes the current state of the partially populated reservation queue.
299  */
300 static int
301 sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS)
302 {
303 	struct sbuf sbuf;
304 	vm_reserv_t rv;
305 	int counter, error, domain, level, unused_pages;
306 
307 	error = sysctl_wire_old_buffer(req, 0);
308 	if (error != 0)
309 		return (error);
310 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
311 	sbuf_printf(&sbuf, "\nDOMAIN    LEVEL     SIZE  NUMBER\n\n");
312 	for (domain = 0; domain < vm_ndomains; domain++) {
313 		for (level = -1; level <= VM_NRESERVLEVEL - 2; level++) {
314 			counter = 0;
315 			unused_pages = 0;
316 			vm_domain_free_lock(VM_DOMAIN(domain));
317 			TAILQ_FOREACH(rv, &vm_rvq_partpop[domain], partpopq) {
318 				counter++;
319 				unused_pages += VM_LEVEL_0_NPAGES - rv->popcnt;
320 			}
321 			vm_domain_free_unlock(VM_DOMAIN(domain));
322 			sbuf_printf(&sbuf, "%6d, %7d, %6dK, %6d\n",
323 			    domain, level,
324 			    unused_pages * ((int)PAGE_SIZE / 1024), counter);
325 		}
326 	}
327 	error = sbuf_finish(&sbuf);
328 	sbuf_delete(&sbuf);
329 	return (error);
330 }
331 
332 /*
333  * Remove a reservation from the object's objq.
334  */
335 static void
336 vm_reserv_remove(vm_reserv_t rv)
337 {
338 	vm_object_t object;
339 
340 	KASSERT(rv->object != NULL,
341 	    ("vm_reserv_remove: reserv %p is free", rv));
342 	KASSERT(!rv->inpartpopq,
343 	    ("vm_reserv_remove: reserv %p's inpartpopq is TRUE", rv));
344 	object = rv->object;
345 	vm_reserv_object_lock(object);
346 	LIST_REMOVE(rv, objq);
347 	rv->object = NULL;
348 	vm_reserv_object_unlock(object);
349 }
350 
351 /*
352  * Insert a new reservation into the object's objq.
353  */
354 static void
355 vm_reserv_insert(vm_reserv_t rv, vm_object_t object, vm_pindex_t pindex)
356 {
357 	int i;
358 
359 	KASSERT(rv->object == NULL,
360 	    ("vm_reserv_insert: reserv %p isn't free", rv));
361 	KASSERT(rv->popcnt == 0,
362 	    ("vm_reserv_insert: reserv %p's popcnt is corrupted", rv));
363 	KASSERT(!rv->inpartpopq,
364 	    ("vm_reserv_insert: reserv %p's inpartpopq is TRUE", rv));
365 	for (i = 0; i < NPOPMAP; i++)
366 		KASSERT(rv->popmap[i] == 0,
367 		    ("vm_reserv_insert: reserv %p's popmap is corrupted", rv));
368 	vm_reserv_object_lock(object);
369 	rv->pindex = pindex;
370 	rv->object = object;
371 	LIST_INSERT_HEAD(&object->rvq, rv, objq);
372 	vm_reserv_object_unlock(object);
373 }
374 
375 /*
376  * Reduces the given reservation's population count.  If the population count
377  * becomes zero, the reservation is destroyed.  Additionally, moves the
378  * reservation to the tail of the partially populated reservation queue if the
379  * population count is non-zero.
380  *
381  * The free page queue lock must be held.
382  */
383 static void
384 vm_reserv_depopulate(vm_reserv_t rv, int index)
385 {
386 
387 	vm_domain_free_assert_locked(VM_DOMAIN(rv->domain));
388 	KASSERT(rv->object != NULL,
389 	    ("vm_reserv_depopulate: reserv %p is free", rv));
390 	KASSERT(popmap_is_set(rv->popmap, index),
391 	    ("vm_reserv_depopulate: reserv %p's popmap[%d] is clear", rv,
392 	    index));
393 	KASSERT(rv->popcnt > 0,
394 	    ("vm_reserv_depopulate: reserv %p's popcnt is corrupted", rv));
395 	KASSERT(rv->domain >= 0 && rv->domain < vm_ndomains,
396 	    ("vm_reserv_depopulate: reserv %p's domain is corrupted %d",
397 	    rv, rv->domain));
398 	if (rv->inpartpopq) {
399 		TAILQ_REMOVE(&vm_rvq_partpop[rv->domain], rv, partpopq);
400 		rv->inpartpopq = FALSE;
401 	} else {
402 		KASSERT(rv->pages->psind == 1,
403 		    ("vm_reserv_depopulate: reserv %p is already demoted",
404 		    rv));
405 		rv->pages->psind = 0;
406 	}
407 	popmap_clear(rv->popmap, index);
408 	rv->popcnt--;
409 	if (rv->popcnt == 0) {
410 		vm_reserv_remove(rv);
411 		vm_phys_free_pages(rv->pages, VM_LEVEL_0_ORDER);
412 		vm_reserv_freed++;
413 	} else {
414 		rv->inpartpopq = TRUE;
415 		TAILQ_INSERT_TAIL(&vm_rvq_partpop[rv->domain], rv, partpopq);
416 	}
417 }
418 
419 /*
420  * Returns the reservation to which the given page might belong.
421  */
422 static __inline vm_reserv_t
423 vm_reserv_from_page(vm_page_t m)
424 {
425 
426 	return (&vm_reserv_array[VM_PAGE_TO_PHYS(m) >> VM_LEVEL_0_SHIFT]);
427 }
428 
429 /*
430  * Returns an existing reservation or NULL and initialized successor pointer.
431  */
432 static vm_reserv_t
433 vm_reserv_from_object(vm_object_t object, vm_pindex_t pindex,
434     vm_page_t mpred, vm_page_t *msuccp)
435 {
436 	vm_reserv_t rv;
437 	vm_page_t msucc;
438 
439 	msucc = NULL;
440 	if (mpred != NULL) {
441 		KASSERT(mpred->object == object,
442 		    ("vm_reserv_from_object: object doesn't contain mpred"));
443 		KASSERT(mpred->pindex < pindex,
444 		    ("vm_reserv_from_object: mpred doesn't precede pindex"));
445 		rv = vm_reserv_from_page(mpred);
446 		if (rv->object == object && vm_reserv_has_pindex(rv, pindex))
447 			goto found;
448 		msucc = TAILQ_NEXT(mpred, listq);
449 	} else
450 		msucc = TAILQ_FIRST(&object->memq);
451 	if (msucc != NULL) {
452 		KASSERT(msucc->pindex > pindex,
453 		    ("vm_reserv_from_object: msucc doesn't succeed pindex"));
454 		rv = vm_reserv_from_page(msucc);
455 		if (rv->object == object && vm_reserv_has_pindex(rv, pindex))
456 			goto found;
457 	}
458 	rv = NULL;
459 
460 found:
461 	*msuccp = msucc;
462 
463 	return (rv);
464 }
465 
466 /*
467  * Returns TRUE if the given reservation contains the given page index and
468  * FALSE otherwise.
469  */
470 static __inline boolean_t
471 vm_reserv_has_pindex(vm_reserv_t rv, vm_pindex_t pindex)
472 {
473 
474 	return (((pindex - rv->pindex) & ~(VM_LEVEL_0_NPAGES - 1)) == 0);
475 }
476 
477 /*
478  * Increases the given reservation's population count.  Moves the reservation
479  * to the tail of the partially populated reservation queue.
480  *
481  * The free page queue must be locked.
482  */
483 static void
484 vm_reserv_populate(vm_reserv_t rv, int index)
485 {
486 
487 	vm_domain_free_assert_locked(VM_DOMAIN(rv->domain));
488 	KASSERT(rv->object != NULL,
489 	    ("vm_reserv_populate: reserv %p is free", rv));
490 	KASSERT(popmap_is_clear(rv->popmap, index),
491 	    ("vm_reserv_populate: reserv %p's popmap[%d] is set", rv,
492 	    index));
493 	KASSERT(rv->popcnt < VM_LEVEL_0_NPAGES,
494 	    ("vm_reserv_populate: reserv %p is already full", rv));
495 	KASSERT(rv->pages->psind == 0,
496 	    ("vm_reserv_populate: reserv %p is already promoted", rv));
497 	KASSERT(rv->domain >= 0 && rv->domain < vm_ndomains,
498 	    ("vm_reserv_populate: reserv %p's domain is corrupted %d",
499 	    rv, rv->domain));
500 	if (rv->inpartpopq) {
501 		TAILQ_REMOVE(&vm_rvq_partpop[rv->domain], rv, partpopq);
502 		rv->inpartpopq = FALSE;
503 	}
504 	popmap_set(rv->popmap, index);
505 	rv->popcnt++;
506 	if (rv->popcnt < VM_LEVEL_0_NPAGES) {
507 		rv->inpartpopq = TRUE;
508 		TAILQ_INSERT_TAIL(&vm_rvq_partpop[rv->domain], rv, partpopq);
509 	} else
510 		rv->pages->psind = 1;
511 }
512 
513 /*
514  * Attempts to allocate a contiguous set of physical pages from existing
515  * reservations.  See vm_reserv_alloc_contig() for a description of the
516  * function's parameters.
517  *
518  * The page "mpred" must immediately precede the offset "pindex" within the
519  * specified object.
520  *
521  * The object must be locked.
522  */
523 vm_page_t
524 vm_reserv_extend_contig(int req, vm_object_t object, vm_pindex_t pindex,
525     int domain, u_long npages, vm_paddr_t low, vm_paddr_t high,
526     u_long alignment, vm_paddr_t boundary, vm_page_t mpred)
527 {
528 	struct vm_domain *vmd;
529 	vm_paddr_t pa, size;
530 	vm_page_t m, msucc;
531 	vm_reserv_t rv;
532 	int i, index;
533 
534 	VM_OBJECT_ASSERT_WLOCKED(object);
535 	KASSERT(npages != 0, ("vm_reserv_alloc_contig: npages is 0"));
536 
537 	/*
538 	 * Is a reservation fundamentally impossible?
539 	 */
540 	if (pindex < VM_RESERV_INDEX(object, pindex) ||
541 	    pindex + npages > object->size || object->resident_page_count == 0)
542 		return (NULL);
543 
544 	/*
545 	 * All reservations of a particular size have the same alignment.
546 	 * Assuming that the first page is allocated from a reservation, the
547 	 * least significant bits of its physical address can be determined
548 	 * from its offset from the beginning of the reservation and the size
549 	 * of the reservation.
550 	 *
551 	 * Could the specified index within a reservation of the smallest
552 	 * possible size satisfy the alignment and boundary requirements?
553 	 */
554 	pa = VM_RESERV_INDEX(object, pindex) << PAGE_SHIFT;
555 	if ((pa & (alignment - 1)) != 0)
556 		return (NULL);
557 	size = npages << PAGE_SHIFT;
558 	if (((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0)
559 		return (NULL);
560 
561 	/*
562 	 * Look for an existing reservation.
563 	 */
564 	rv = vm_reserv_from_object(object, pindex, mpred, &msucc);
565 	if (rv == NULL)
566 		return (NULL);
567 	KASSERT(object != kernel_object || rv->domain == domain,
568 	    ("vm_reserv_extend_contig: Domain mismatch from reservation."));
569 	index = VM_RESERV_INDEX(object, pindex);
570 	/* Does the allocation fit within the reservation? */
571 	if (index + npages > VM_LEVEL_0_NPAGES)
572 		return (NULL);
573 	domain = rv->domain;
574 	vmd = VM_DOMAIN(domain);
575 	vm_domain_free_lock(vmd);
576 	if (rv->object != object || !vm_domain_available(vmd, req, npages)) {
577 		m = NULL;
578 		goto out;
579 	}
580 	m = &rv->pages[index];
581 	pa = VM_PAGE_TO_PHYS(m);
582 	if (pa < low || pa + size > high || (pa & (alignment - 1)) != 0 ||
583 	    ((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0) {
584 		m = NULL;
585 		goto out;
586 	}
587 	/* Handle vm_page_rename(m, new_object, ...). */
588 	for (i = 0; i < npages; i++) {
589 		if (popmap_is_set(rv->popmap, index + i)) {
590 			m = NULL;
591 			goto out;
592 		}
593 	}
594 	for (i = 0; i < npages; i++)
595 		vm_reserv_populate(rv, index + i);
596 	vm_domain_freecnt_adj(vmd, -npages);
597 out:
598 	vm_domain_free_unlock(vmd);
599 	return (m);
600 }
601 
602 /*
603  * Allocates a contiguous set of physical pages of the given size "npages"
604  * from newly created reservations.  All of the physical pages
605  * must be at or above the given physical address "low" and below the given
606  * physical address "high".  The given value "alignment" determines the
607  * alignment of the first physical page in the set.  If the given value
608  * "boundary" is non-zero, then the set of physical pages cannot cross any
609  * physical address boundary that is a multiple of that value.  Both
610  * "alignment" and "boundary" must be a power of two.
611  *
612  * Callers should first invoke vm_reserv_extend_contig() to attempt an
613  * allocation from existing reservations.
614  *
615  * The page "mpred" must immediately precede the offset "pindex" within the
616  * specified object.
617  *
618  * The object and free page queue must be locked.
619  */
620 vm_page_t
621 vm_reserv_alloc_contig(vm_object_t object, vm_pindex_t pindex, int domain,
622     u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
623     vm_paddr_t boundary, vm_page_t mpred)
624 {
625 	vm_paddr_t pa, size;
626 	vm_page_t m, m_ret, msucc;
627 	vm_pindex_t first, leftcap, rightcap;
628 	vm_reserv_t rv;
629 	u_long allocpages, maxpages, minpages;
630 	int i, index, n;
631 
632 	vm_domain_free_assert_locked(VM_DOMAIN(domain));
633 	VM_OBJECT_ASSERT_WLOCKED(object);
634 	KASSERT(npages != 0, ("vm_reserv_alloc_contig: npages is 0"));
635 
636 	/*
637 	 * Is a reservation fundamentally impossible?
638 	 */
639 	if (pindex < VM_RESERV_INDEX(object, pindex) ||
640 	    pindex + npages > object->size)
641 		return (NULL);
642 
643 	/*
644 	 * All reservations of a particular size have the same alignment.
645 	 * Assuming that the first page is allocated from a reservation, the
646 	 * least significant bits of its physical address can be determined
647 	 * from its offset from the beginning of the reservation and the size
648 	 * of the reservation.
649 	 *
650 	 * Could the specified index within a reservation of the smallest
651 	 * possible size satisfy the alignment and boundary requirements?
652 	 */
653 	pa = VM_RESERV_INDEX(object, pindex) << PAGE_SHIFT;
654 	if ((pa & (alignment - 1)) != 0)
655 		return (NULL);
656 	size = npages << PAGE_SHIFT;
657 	if (((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0)
658 		return (NULL);
659 
660 	/*
661 	 * Callers should've extended an existing reservation prior to
662 	 * calling this function.  If a reservation exists it is
663 	 * incompatible with the allocation.
664 	 */
665 	rv = vm_reserv_from_object(object, pindex, mpred, &msucc);
666 	if (rv != NULL)
667 		return (NULL);
668 
669 	/*
670 	 * Could at least one reservation fit between the first index to the
671 	 * left that can be used ("leftcap") and the first index to the right
672 	 * that cannot be used ("rightcap")?
673 	 *
674 	 * We must synchronize with the reserv object lock to protect the
675 	 * pindex/object of the resulting reservations against rename while
676 	 * we are inspecting.
677 	 */
678 	first = pindex - VM_RESERV_INDEX(object, pindex);
679 	minpages = VM_RESERV_INDEX(object, pindex) + npages;
680 	maxpages = roundup2(minpages, VM_LEVEL_0_NPAGES);
681 	allocpages = maxpages;
682 	vm_reserv_object_lock(object);
683 	if (mpred != NULL) {
684 		if ((rv = vm_reserv_from_page(mpred))->object != object)
685 			leftcap = mpred->pindex + 1;
686 		else
687 			leftcap = rv->pindex + VM_LEVEL_0_NPAGES;
688 		if (leftcap > first) {
689 			vm_reserv_object_unlock(object);
690 			return (NULL);
691 		}
692 	}
693 	if (msucc != NULL) {
694 		if ((rv = vm_reserv_from_page(msucc))->object != object)
695 			rightcap = msucc->pindex;
696 		else
697 			rightcap = rv->pindex;
698 		if (first + maxpages > rightcap) {
699 			if (maxpages == VM_LEVEL_0_NPAGES) {
700 				vm_reserv_object_unlock(object);
701 				return (NULL);
702 			}
703 
704 			/*
705 			 * At least one reservation will fit between "leftcap"
706 			 * and "rightcap".  However, a reservation for the
707 			 * last of the requested pages will not fit.  Reduce
708 			 * the size of the upcoming allocation accordingly.
709 			 */
710 			allocpages = minpages;
711 		}
712 	}
713 	vm_reserv_object_unlock(object);
714 
715 	/*
716 	 * Would the last new reservation extend past the end of the object?
717 	 */
718 	if (first + maxpages > object->size) {
719 		/*
720 		 * Don't allocate the last new reservation if the object is a
721 		 * vnode or backed by another object that is a vnode.
722 		 */
723 		if (object->type == OBJT_VNODE ||
724 		    (object->backing_object != NULL &&
725 		    object->backing_object->type == OBJT_VNODE)) {
726 			if (maxpages == VM_LEVEL_0_NPAGES)
727 				return (NULL);
728 			allocpages = minpages;
729 		}
730 		/* Speculate that the object may grow. */
731 	}
732 
733 	/*
734 	 * Allocate the physical pages.  The alignment and boundary specified
735 	 * for this allocation may be different from the alignment and
736 	 * boundary specified for the requested pages.  For instance, the
737 	 * specified index may not be the first page within the first new
738 	 * reservation.
739 	 */
740 	m = vm_phys_alloc_contig(domain, allocpages, low, high, ulmax(alignment,
741 	    VM_LEVEL_0_SIZE), boundary > VM_LEVEL_0_SIZE ? boundary : 0);
742 	if (m == NULL)
743 		return (NULL);
744 	KASSERT(vm_phys_domain(m) == domain,
745 	    ("vm_reserv_alloc_contig: Page domain does not match requested."));
746 
747 	/*
748 	 * The allocated physical pages always begin at a reservation
749 	 * boundary, but they do not always end at a reservation boundary.
750 	 * Initialize every reservation that is completely covered by the
751 	 * allocated physical pages.
752 	 */
753 	m_ret = NULL;
754 	index = VM_RESERV_INDEX(object, pindex);
755 	do {
756 		rv = vm_reserv_from_page(m);
757 		KASSERT(rv->pages == m,
758 		    ("vm_reserv_alloc_contig: reserv %p's pages is corrupted",
759 		    rv));
760 		vm_reserv_insert(rv, object, first);
761 		n = ulmin(VM_LEVEL_0_NPAGES - index, npages);
762 		for (i = 0; i < n; i++)
763 			vm_reserv_populate(rv, index + i);
764 		npages -= n;
765 		if (m_ret == NULL) {
766 			m_ret = &rv->pages[index];
767 			index = 0;
768 		}
769 		m += VM_LEVEL_0_NPAGES;
770 		first += VM_LEVEL_0_NPAGES;
771 		allocpages -= VM_LEVEL_0_NPAGES;
772 	} while (allocpages >= VM_LEVEL_0_NPAGES);
773 	return (m_ret);
774 }
775 
776 /*
777  * Attempts to extend an existing reservation and allocate the page to the
778  * object.
779  *
780  * The page "mpred" must immediately precede the offset "pindex" within the
781  * specified object.
782  *
783  * The object must be locked.
784  */
785 vm_page_t
786 vm_reserv_extend(int req, vm_object_t object, vm_pindex_t pindex, int domain,
787     vm_page_t mpred)
788 {
789 	struct vm_domain *vmd;
790 	vm_page_t m, msucc;
791 	vm_reserv_t rv;
792 	int index, free_count;
793 
794 	VM_OBJECT_ASSERT_WLOCKED(object);
795 
796 	/*
797 	 * Could a reservation currently exist?
798 	 */
799 	if (pindex < VM_RESERV_INDEX(object, pindex) ||
800 	    pindex >= object->size || object->resident_page_count == 0)
801 		return (NULL);
802 
803 	/*
804 	 * Look for an existing reservation.
805 	 */
806 	rv = vm_reserv_from_object(object, pindex, mpred, &msucc);
807 	if (rv == NULL)
808 		return (NULL);
809 
810 	KASSERT(object != kernel_object || rv->domain == domain,
811 	    ("vm_reserv_extend: Domain mismatch from reservation."));
812 	domain = rv->domain;
813 	vmd = VM_DOMAIN(domain);
814 	index = VM_RESERV_INDEX(object, pindex);
815 	m = &rv->pages[index];
816 	vm_domain_free_lock(vmd);
817 	if (vm_domain_available(vmd, req, 1) == 0 ||
818 	    /* Handle reclaim race. */
819 	    rv->object != object ||
820 	    /* Handle vm_page_rename(m, new_object, ...). */
821 	    popmap_is_set(rv->popmap, index))
822 		m = NULL;
823 	if (m != NULL) {
824 		vm_reserv_populate(rv, index);
825 		free_count = vm_domain_freecnt_adj(vmd, -1);
826 	} else
827 		free_count = vmd->vmd_free_count;
828 	vm_domain_free_unlock(vmd);
829 
830 	if (vm_paging_needed(vmd, free_count))
831 		pagedaemon_wakeup(domain);
832 
833 	return (m);
834 }
835 
836 /*
837  * Attempts to allocate a new reservation for the object, and allocates a
838  * page from that reservation.  Callers should first invoke vm_reserv_extend()
839  * to attempt an allocation from an existing reservation.
840  *
841  * The page "mpred" must immediately precede the offset "pindex" within the
842  * specified object.
843  *
844  * The object and free page queue must be locked.
845  */
846 vm_page_t
847 vm_reserv_alloc_page(vm_object_t object, vm_pindex_t pindex, int domain,
848     vm_page_t mpred)
849 {
850 	vm_page_t m, msucc;
851 	vm_pindex_t first, leftcap, rightcap;
852 	vm_reserv_t rv;
853 	int index;
854 
855 	vm_domain_free_assert_locked(VM_DOMAIN(domain));
856 	VM_OBJECT_ASSERT_WLOCKED(object);
857 
858 	/*
859 	 * Is a reservation fundamentally impossible?
860 	 */
861 	if (pindex < VM_RESERV_INDEX(object, pindex) ||
862 	    pindex >= object->size)
863 		return (NULL);
864 
865 	/*
866 	 * Callers should've extended an existing reservation prior to
867 	 * calling this function.  If a reservation exists it is
868 	 * incompatible with the allocation.
869 	 */
870 	rv = vm_reserv_from_object(object, pindex, mpred, &msucc);
871 	if (rv != NULL)
872 		return (NULL);
873 
874 	/*
875 	 * Could a reservation fit between the first index to the left that
876 	 * can be used and the first index to the right that cannot be used?
877 	 *
878 	 * We must synchronize with the reserv object lock to protect the
879 	 * pindex/object of the resulting reservations against rename while
880 	 * we are inspecting.
881 	 */
882 	first = pindex - VM_RESERV_INDEX(object, pindex);
883 	vm_reserv_object_lock(object);
884 	if (mpred != NULL) {
885 		if ((rv = vm_reserv_from_page(mpred))->object != object)
886 			leftcap = mpred->pindex + 1;
887 		else
888 			leftcap = rv->pindex + VM_LEVEL_0_NPAGES;
889 		if (leftcap > first) {
890 			vm_reserv_object_unlock(object);
891 			return (NULL);
892 		}
893 	}
894 	if (msucc != NULL) {
895 		if ((rv = vm_reserv_from_page(msucc))->object != object)
896 			rightcap = msucc->pindex;
897 		else
898 			rightcap = rv->pindex;
899 		if (first + VM_LEVEL_0_NPAGES > rightcap) {
900 			vm_reserv_object_unlock(object);
901 			return (NULL);
902 		}
903 	}
904 	vm_reserv_object_unlock(object);
905 
906 	/*
907 	 * Would a new reservation extend past the end of the object?
908 	 */
909 	if (first + VM_LEVEL_0_NPAGES > object->size) {
910 		/*
911 		 * Don't allocate a new reservation if the object is a vnode or
912 		 * backed by another object that is a vnode.
913 		 */
914 		if (object->type == OBJT_VNODE ||
915 		    (object->backing_object != NULL &&
916 		    object->backing_object->type == OBJT_VNODE))
917 			return (NULL);
918 		/* Speculate that the object may grow. */
919 	}
920 
921 	/*
922 	 * Allocate and populate the new reservation.
923 	 */
924 	m = vm_phys_alloc_pages(domain, VM_FREEPOOL_DEFAULT, VM_LEVEL_0_ORDER);
925 	if (m == NULL)
926 		return (NULL);
927 	rv = vm_reserv_from_page(m);
928 	KASSERT(rv->pages == m,
929 	    ("vm_reserv_alloc_page: reserv %p's pages is corrupted", rv));
930 	vm_reserv_insert(rv, object, first);
931 	index = VM_RESERV_INDEX(object, pindex);
932 	vm_reserv_populate(rv, index);
933 	return (&rv->pages[index]);
934 }
935 
936 /*
937  * Breaks the given reservation.  All free pages in the reservation
938  * are returned to the physical memory allocator.  The reservation's
939  * population count and map are reset to their initial state.
940  *
941  * The given reservation must not be in the partially populated reservation
942  * queue.  The free page queue lock must be held.
943  */
944 static void
945 vm_reserv_break(vm_reserv_t rv)
946 {
947 	int begin_zeroes, hi, i, lo;
948 
949 	vm_domain_free_assert_locked(VM_DOMAIN(rv->domain));
950 	vm_reserv_remove(rv);
951 	rv->pages->psind = 0;
952 	i = hi = 0;
953 	do {
954 		/* Find the next 0 bit.  Any previous 0 bits are < "hi". */
955 		lo = ffsl(~(((1UL << hi) - 1) | rv->popmap[i]));
956 		if (lo == 0) {
957 			/* Redundantly clears bits < "hi". */
958 			rv->popmap[i] = 0;
959 			rv->popcnt -= NBPOPMAP - hi;
960 			while (++i < NPOPMAP) {
961 				lo = ffsl(~rv->popmap[i]);
962 				if (lo == 0) {
963 					rv->popmap[i] = 0;
964 					rv->popcnt -= NBPOPMAP;
965 				} else
966 					break;
967 			}
968 			if (i == NPOPMAP)
969 				break;
970 			hi = 0;
971 		}
972 		KASSERT(lo > 0, ("vm_reserv_break: lo is %d", lo));
973 		/* Convert from ffsl() to ordinary bit numbering. */
974 		lo--;
975 		if (lo > 0) {
976 			/* Redundantly clears bits < "hi". */
977 			rv->popmap[i] &= ~((1UL << lo) - 1);
978 			rv->popcnt -= lo - hi;
979 		}
980 		begin_zeroes = NBPOPMAP * i + lo;
981 		/* Find the next 1 bit. */
982 		do
983 			hi = ffsl(rv->popmap[i]);
984 		while (hi == 0 && ++i < NPOPMAP);
985 		if (i != NPOPMAP)
986 			/* Convert from ffsl() to ordinary bit numbering. */
987 			hi--;
988 		vm_phys_free_contig(&rv->pages[begin_zeroes], NBPOPMAP * i +
989 		    hi - begin_zeroes);
990 	} while (i < NPOPMAP);
991 	KASSERT(rv->popcnt == 0,
992 	    ("vm_reserv_break: reserv %p's popcnt is corrupted", rv));
993 	vm_reserv_broken++;
994 }
995 
996 /*
997  * Breaks all reservations belonging to the given object.
998  */
999 void
1000 vm_reserv_break_all(vm_object_t object)
1001 {
1002 	vm_reserv_t rv;
1003 	struct vm_domain *vmd;
1004 
1005 	/*
1006 	 * This access of object->rvq is unsynchronized so that the
1007 	 * object rvq lock can nest after the domain_free lock.  We
1008 	 * must check for races in the results.  However, the object
1009 	 * lock prevents new additions, so we are guaranteed that when
1010 	 * it returns NULL the object is properly empty.
1011 	 */
1012 	vmd = NULL;
1013 	while ((rv = LIST_FIRST(&object->rvq)) != NULL) {
1014 		if (vmd != VM_DOMAIN(rv->domain)) {
1015 			if (vmd != NULL)
1016 				vm_domain_free_unlock(vmd);
1017 			vmd = VM_DOMAIN(rv->domain);
1018 			vm_domain_free_lock(vmd);
1019 		}
1020 		/* Reclaim race. */
1021 		if (rv->object != object)
1022 			continue;
1023 		KASSERT(rv->object == object,
1024 		    ("vm_reserv_break_all: reserv %p is corrupted", rv));
1025 		if (rv->inpartpopq) {
1026 			TAILQ_REMOVE(&vm_rvq_partpop[rv->domain], rv, partpopq);
1027 			rv->inpartpopq = FALSE;
1028 		}
1029 		vm_reserv_break(rv);
1030 	}
1031 	if (vmd != NULL)
1032 		vm_domain_free_unlock(vmd);
1033 }
1034 
1035 /*
1036  * Frees the given page if it belongs to a reservation.  Returns TRUE if the
1037  * page is freed and FALSE otherwise.
1038  *
1039  * The free page queue lock must be held.
1040  */
1041 boolean_t
1042 vm_reserv_free_page(vm_page_t m)
1043 {
1044 	vm_reserv_t rv;
1045 
1046 	rv = vm_reserv_from_page(m);
1047 	vm_domain_free_assert_locked(VM_DOMAIN(rv->domain));
1048 	if (rv->object == NULL)
1049 		return (FALSE);
1050 	vm_reserv_depopulate(rv, m - rv->pages);
1051 	return (TRUE);
1052 }
1053 
1054 /*
1055  * Initializes the reservation management system.  Specifically, initializes
1056  * the reservation array.
1057  *
1058  * Requires that vm_page_array and first_page are initialized!
1059  */
1060 void
1061 vm_reserv_init(void)
1062 {
1063 	vm_paddr_t paddr;
1064 	struct vm_phys_seg *seg;
1065 	int i, segind;
1066 
1067 	/*
1068 	 * Initialize the reservation array.  Specifically, initialize the
1069 	 * "pages" field for every element that has an underlying superpage.
1070 	 */
1071 	for (segind = 0; segind < vm_phys_nsegs; segind++) {
1072 		seg = &vm_phys_segs[segind];
1073 		paddr = roundup2(seg->start, VM_LEVEL_0_SIZE);
1074 		while (paddr + VM_LEVEL_0_SIZE <= seg->end) {
1075 			vm_reserv_array[paddr >> VM_LEVEL_0_SHIFT].pages =
1076 			    PHYS_TO_VM_PAGE(paddr);
1077 			vm_reserv_array[paddr >> VM_LEVEL_0_SHIFT].domain =
1078 			    seg->domain;
1079 			paddr += VM_LEVEL_0_SIZE;
1080 		}
1081 	}
1082 	for (i = 0; i < MAXMEMDOM; i++)
1083 		TAILQ_INIT(&vm_rvq_partpop[i]);
1084 }
1085 
1086 /*
1087  * Returns true if the given page belongs to a reservation and that page is
1088  * free.  Otherwise, returns false.
1089  */
1090 bool
1091 vm_reserv_is_page_free(vm_page_t m)
1092 {
1093 	vm_reserv_t rv;
1094 
1095 	rv = vm_reserv_from_page(m);
1096 	vm_domain_free_assert_locked(VM_DOMAIN(rv->domain));
1097 	if (rv->object == NULL)
1098 		return (false);
1099 	return (popmap_is_clear(rv->popmap, m - rv->pages));
1100 }
1101 
1102 /*
1103  * If the given page belongs to a reservation, returns the level of that
1104  * reservation.  Otherwise, returns -1.
1105  */
1106 int
1107 vm_reserv_level(vm_page_t m)
1108 {
1109 	vm_reserv_t rv;
1110 
1111 	rv = vm_reserv_from_page(m);
1112 	return (rv->object != NULL ? 0 : -1);
1113 }
1114 
1115 /*
1116  * Returns a reservation level if the given page belongs to a fully populated
1117  * reservation and -1 otherwise.
1118  */
1119 int
1120 vm_reserv_level_iffullpop(vm_page_t m)
1121 {
1122 	vm_reserv_t rv;
1123 
1124 	rv = vm_reserv_from_page(m);
1125 	return (rv->popcnt == VM_LEVEL_0_NPAGES ? 0 : -1);
1126 }
1127 
1128 /*
1129  * Breaks the given partially populated reservation, releasing its free pages
1130  * to the physical memory allocator.
1131  *
1132  * The free page queue lock must be held.
1133  */
1134 static void
1135 vm_reserv_reclaim(vm_reserv_t rv)
1136 {
1137 
1138 	vm_domain_free_assert_locked(VM_DOMAIN(rv->domain));
1139 	KASSERT(rv->inpartpopq,
1140 	    ("vm_reserv_reclaim: reserv %p's inpartpopq is FALSE", rv));
1141 	KASSERT(rv->domain >= 0 && rv->domain < vm_ndomains,
1142 	    ("vm_reserv_reclaim: reserv %p's domain is corrupted %d",
1143 	    rv, rv->domain));
1144 	TAILQ_REMOVE(&vm_rvq_partpop[rv->domain], rv, partpopq);
1145 	rv->inpartpopq = FALSE;
1146 	vm_reserv_break(rv);
1147 	vm_reserv_reclaimed++;
1148 }
1149 
1150 /*
1151  * Breaks the reservation at the head of the partially populated reservation
1152  * queue, releasing its free pages to the physical memory allocator.  Returns
1153  * TRUE if a reservation is broken and FALSE otherwise.
1154  *
1155  * The free page queue lock must be held.
1156  */
1157 boolean_t
1158 vm_reserv_reclaim_inactive(int domain)
1159 {
1160 	vm_reserv_t rv;
1161 
1162 	vm_domain_free_assert_locked(VM_DOMAIN(domain));
1163 	if ((rv = TAILQ_FIRST(&vm_rvq_partpop[domain])) != NULL) {
1164 		vm_reserv_reclaim(rv);
1165 		return (TRUE);
1166 	}
1167 	return (FALSE);
1168 }
1169 
1170 /*
1171  * Searches the partially populated reservation queue for the least recently
1172  * changed reservation with free pages that satisfy the given request for
1173  * contiguous physical memory.  If a satisfactory reservation is found, it is
1174  * broken.  Returns TRUE if a reservation is broken and FALSE otherwise.
1175  *
1176  * The free page queue lock must be held.
1177  */
1178 boolean_t
1179 vm_reserv_reclaim_contig(int domain, u_long npages, vm_paddr_t low,
1180     vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
1181 {
1182 	vm_paddr_t pa, size;
1183 	vm_reserv_t rv;
1184 	int hi, i, lo, low_index, next_free;
1185 
1186 	vm_domain_free_assert_locked(VM_DOMAIN(domain));
1187 	if (npages > VM_LEVEL_0_NPAGES - 1)
1188 		return (FALSE);
1189 	size = npages << PAGE_SHIFT;
1190 	TAILQ_FOREACH(rv, &vm_rvq_partpop[domain], partpopq) {
1191 		pa = VM_PAGE_TO_PHYS(&rv->pages[VM_LEVEL_0_NPAGES - 1]);
1192 		if (pa + PAGE_SIZE - size < low) {
1193 			/* This entire reservation is too low; go to next. */
1194 			continue;
1195 		}
1196 		pa = VM_PAGE_TO_PHYS(&rv->pages[0]);
1197 		if (pa + size > high) {
1198 			/* This entire reservation is too high; go to next. */
1199 			continue;
1200 		}
1201 		if (pa < low) {
1202 			/* Start the search for free pages at "low". */
1203 			low_index = (low + PAGE_MASK - pa) >> PAGE_SHIFT;
1204 			i = low_index / NBPOPMAP;
1205 			hi = low_index % NBPOPMAP;
1206 		} else
1207 			i = hi = 0;
1208 		do {
1209 			/* Find the next free page. */
1210 			lo = ffsl(~(((1UL << hi) - 1) | rv->popmap[i]));
1211 			while (lo == 0 && ++i < NPOPMAP)
1212 				lo = ffsl(~rv->popmap[i]);
1213 			if (i == NPOPMAP)
1214 				break;
1215 			/* Convert from ffsl() to ordinary bit numbering. */
1216 			lo--;
1217 			next_free = NBPOPMAP * i + lo;
1218 			pa = VM_PAGE_TO_PHYS(&rv->pages[next_free]);
1219 			KASSERT(pa >= low,
1220 			    ("vm_reserv_reclaim_contig: pa is too low"));
1221 			if (pa + size > high) {
1222 				/* The rest of this reservation is too high. */
1223 				break;
1224 			} else if ((pa & (alignment - 1)) != 0 ||
1225 			    ((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0) {
1226 				/*
1227 				 * The current page doesn't meet the alignment
1228 				 * and/or boundary requirements.  Continue
1229 				 * searching this reservation until the rest
1230 				 * of its free pages are either excluded or
1231 				 * exhausted.
1232 				 */
1233 				hi = lo + 1;
1234 				if (hi >= NBPOPMAP) {
1235 					hi = 0;
1236 					i++;
1237 				}
1238 				continue;
1239 			}
1240 			/* Find the next used page. */
1241 			hi = ffsl(rv->popmap[i] & ~((1UL << lo) - 1));
1242 			while (hi == 0 && ++i < NPOPMAP) {
1243 				if ((NBPOPMAP * i - next_free) * PAGE_SIZE >=
1244 				    size) {
1245 					vm_reserv_reclaim(rv);
1246 					return (TRUE);
1247 				}
1248 				hi = ffsl(rv->popmap[i]);
1249 			}
1250 			/* Convert from ffsl() to ordinary bit numbering. */
1251 			if (i != NPOPMAP)
1252 				hi--;
1253 			if ((NBPOPMAP * i + hi - next_free) * PAGE_SIZE >=
1254 			    size) {
1255 				vm_reserv_reclaim(rv);
1256 				return (TRUE);
1257 			}
1258 		} while (i < NPOPMAP);
1259 	}
1260 	return (FALSE);
1261 }
1262 
1263 /*
1264  * Transfers the reservation underlying the given page to a new object.
1265  *
1266  * The object must be locked.
1267  */
1268 void
1269 vm_reserv_rename(vm_page_t m, vm_object_t new_object, vm_object_t old_object,
1270     vm_pindex_t old_object_offset)
1271 {
1272 	vm_reserv_t rv;
1273 
1274 	VM_OBJECT_ASSERT_WLOCKED(new_object);
1275 	rv = vm_reserv_from_page(m);
1276 	if (rv->object == old_object) {
1277 		vm_domain_free_lock(VM_DOMAIN(rv->domain));
1278 		if (rv->object == old_object) {
1279 			vm_reserv_object_lock(old_object);
1280 			rv->object = NULL;
1281 			LIST_REMOVE(rv, objq);
1282 			vm_reserv_object_unlock(old_object);
1283 			vm_reserv_object_lock(new_object);
1284 			rv->object = new_object;
1285 			rv->pindex -= old_object_offset;
1286 			LIST_INSERT_HEAD(&new_object->rvq, rv, objq);
1287 			vm_reserv_object_unlock(new_object);
1288 		}
1289 		vm_domain_free_unlock(VM_DOMAIN(rv->domain));
1290 	}
1291 }
1292 
1293 /*
1294  * Returns the size (in bytes) of a reservation of the specified level.
1295  */
1296 int
1297 vm_reserv_size(int level)
1298 {
1299 
1300 	switch (level) {
1301 	case 0:
1302 		return (VM_LEVEL_0_SIZE);
1303 	case -1:
1304 		return (PAGE_SIZE);
1305 	default:
1306 		return (0);
1307 	}
1308 }
1309 
1310 /*
1311  * Allocates the virtual and physical memory required by the reservation
1312  * management system's data structures, in particular, the reservation array.
1313  */
1314 vm_paddr_t
1315 vm_reserv_startup(vm_offset_t *vaddr, vm_paddr_t end, vm_paddr_t high_water)
1316 {
1317 	vm_paddr_t new_end;
1318 	size_t size;
1319 	int i;
1320 
1321 	/*
1322 	 * Calculate the size (in bytes) of the reservation array.  Round up
1323 	 * from "high_water" because every small page is mapped to an element
1324 	 * in the reservation array based on its physical address.  Thus, the
1325 	 * number of elements in the reservation array can be greater than the
1326 	 * number of superpages.
1327 	 */
1328 	size = howmany(high_water, VM_LEVEL_0_SIZE) * sizeof(struct vm_reserv);
1329 
1330 	/*
1331 	 * Allocate and map the physical memory for the reservation array.  The
1332 	 * next available virtual address is returned by reference.
1333 	 */
1334 	new_end = end - round_page(size);
1335 	vm_reserv_array = (void *)(uintptr_t)pmap_map(vaddr, new_end, end,
1336 	    VM_PROT_READ | VM_PROT_WRITE);
1337 	bzero(vm_reserv_array, size);
1338 
1339 	for (i = 0; i < VM_RESERV_OBJ_LOCK_COUNT; i++)
1340 		mtx_init(&vm_reserv_object_mtx[i], "resv obj lock", NULL,
1341 		    MTX_DEF);
1342 
1343 	/*
1344 	 * Return the next available physical address.
1345 	 */
1346 	return (new_end);
1347 }
1348 
1349 /*
1350  * Returns the superpage containing the given page.
1351  */
1352 vm_page_t
1353 vm_reserv_to_superpage(vm_page_t m)
1354 {
1355 	vm_reserv_t rv;
1356 
1357 	VM_OBJECT_ASSERT_LOCKED(m->object);
1358 	rv = vm_reserv_from_page(m);
1359 	return (rv->object == m->object && rv->popcnt == VM_LEVEL_0_NPAGES ?
1360 	    rv->pages : NULL);
1361 }
1362 
1363 #endif	/* VM_NRESERVLEVEL > 0 */
1364