xref: /freebsd-src/sys/vm/vm_reserv.c (revision 0078df5f025854600595462e56fa95d34e732141)
1f8a47341SAlan Cox /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni  *
4f8a47341SAlan Cox  * Copyright (c) 2002-2006 Rice University
5ec179322SAlan Cox  * Copyright (c) 2007-2011 Alan L. Cox <alc@cs.rice.edu>
6f8a47341SAlan Cox  * All rights reserved.
7f8a47341SAlan Cox  *
8f8a47341SAlan Cox  * This software was developed for the FreeBSD Project by Alan L. Cox,
9f8a47341SAlan Cox  * Olivier Crameri, Peter Druschel, Sitaram Iyer, and Juan Navarro.
10f8a47341SAlan Cox  *
11f8a47341SAlan Cox  * Redistribution and use in source and binary forms, with or without
12f8a47341SAlan Cox  * modification, are permitted provided that the following conditions
13f8a47341SAlan Cox  * are met:
14f8a47341SAlan Cox  * 1. Redistributions of source code must retain the above copyright
15f8a47341SAlan Cox  *    notice, this list of conditions and the following disclaimer.
16f8a47341SAlan Cox  * 2. Redistributions in binary form must reproduce the above copyright
17f8a47341SAlan Cox  *    notice, this list of conditions and the following disclaimer in the
18f8a47341SAlan Cox  *    documentation and/or other materials provided with the distribution.
19f8a47341SAlan Cox  *
20f8a47341SAlan Cox  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21f8a47341SAlan Cox  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22f8a47341SAlan Cox  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23f8a47341SAlan Cox  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
24f8a47341SAlan Cox  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25f8a47341SAlan Cox  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26f8a47341SAlan Cox  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27f8a47341SAlan Cox  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28f8a47341SAlan Cox  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29f8a47341SAlan Cox  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
30f8a47341SAlan Cox  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31f8a47341SAlan Cox  * POSSIBILITY OF SUCH DAMAGE.
32f8a47341SAlan Cox  */
33f8a47341SAlan Cox 
34f8a47341SAlan Cox /*
35f8a47341SAlan Cox  *	Superpage reservation management module
36c68c3537SAlan Cox  *
37c68c3537SAlan Cox  * Any external functions defined by this module are only to be used by the
38c68c3537SAlan Cox  * virtual memory system.
39f8a47341SAlan Cox  */
40f8a47341SAlan Cox 
41f8a47341SAlan Cox #include <sys/cdefs.h>
42f8a47341SAlan Cox #include "opt_vm.h"
43f8a47341SAlan Cox 
44f8a47341SAlan Cox #include <sys/param.h>
45f8a47341SAlan Cox #include <sys/kernel.h>
46f8a47341SAlan Cox #include <sys/lock.h>
47f8a47341SAlan Cox #include <sys/malloc.h>
48f8a47341SAlan Cox #include <sys/mutex.h>
49f8a47341SAlan Cox #include <sys/queue.h>
5089f6b863SAttilio Rao #include <sys/rwlock.h>
51f8a47341SAlan Cox #include <sys/sbuf.h>
52f8a47341SAlan Cox #include <sys/sysctl.h>
53f8a47341SAlan Cox #include <sys/systm.h>
5484e2ae64SDoug Moore #include <sys/bitstring.h>
5572346b22SCy Schubert #include <sys/counter.h>
5672346b22SCy Schubert #include <sys/ktr.h>
579ed01c32SGleb Smirnoff #include <sys/vmmeter.h>
585c930c89SJeff Roberson #include <sys/smp.h>
59f8a47341SAlan Cox 
60f8a47341SAlan Cox #include <vm/vm.h>
61f76916c0SDoug Moore #include <vm/vm_extern.h>
62f8a47341SAlan Cox #include <vm/vm_param.h>
63f8a47341SAlan Cox #include <vm/vm_object.h>
64f8a47341SAlan Cox #include <vm/vm_page.h>
65e2068d0bSJeff Roberson #include <vm/vm_pageout.h>
66e2068d0bSJeff Roberson #include <vm/vm_pagequeue.h>
67431fb8abSMark Johnston #include <vm/vm_phys.h>
68774d251dSAttilio Rao #include <vm/vm_radix.h>
69f8a47341SAlan Cox #include <vm/vm_reserv.h>
70f8a47341SAlan Cox 
71f8a47341SAlan Cox /*
72f8a47341SAlan Cox  * The reservation system supports the speculative allocation of large physical
733453bca8SAlan Cox  * pages ("superpages").  Speculative allocation enables the fully automatic
74f8a47341SAlan Cox  * utilization of superpages by the virtual memory system.  In other words, no
75f8a47341SAlan Cox  * programmatic directives are required to use superpages.
76f8a47341SAlan Cox  */
77f8a47341SAlan Cox 
78f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
79f8a47341SAlan Cox 
803e00c11aSAlan Cox /*
813e00c11aSAlan Cox  * Temporarily simulate two-level reservations.  Effectively, VM_LEVEL_0_* is
823e00c11aSAlan Cox  * level 1, and VM_SUBLEVEL_0_* is level 0.
833e00c11aSAlan Cox  */
843e00c11aSAlan Cox #if VM_NRESERVLEVEL == 2
853e00c11aSAlan Cox #undef VM_NRESERVLEVEL
863e00c11aSAlan Cox #define	VM_NRESERVLEVEL		1
873e00c11aSAlan Cox #if VM_LEVEL_0_ORDER == 4
883e00c11aSAlan Cox #undef VM_LEVEL_0_ORDER
893e00c11aSAlan Cox #define	VM_LEVEL_0_ORDER	(4 + VM_LEVEL_1_ORDER)
903e00c11aSAlan Cox #define	VM_SUBLEVEL_0_NPAGES	(1 << 4)
913e00c11aSAlan Cox #elif VM_LEVEL_0_ORDER == 7
923e00c11aSAlan Cox #undef VM_LEVEL_0_ORDER
933e00c11aSAlan Cox #define	VM_LEVEL_0_ORDER	(7 + VM_LEVEL_1_ORDER)
943e00c11aSAlan Cox #define	VM_SUBLEVEL_0_NPAGES	(1 << 7)
953e00c11aSAlan Cox #else
963e00c11aSAlan Cox #error "Unsupported level 0 reservation size"
973e00c11aSAlan Cox #endif
983e00c11aSAlan Cox #define	VM_LEVEL_0_PSIND	2
993e00c11aSAlan Cox #else
1003e00c11aSAlan Cox #define	VM_LEVEL_0_PSIND	1
1013e00c11aSAlan Cox #endif
1023e00c11aSAlan Cox 
103f2a496d6SKonstantin Belousov #ifndef VM_LEVEL_0_ORDER_MAX
104f2a496d6SKonstantin Belousov #define	VM_LEVEL_0_ORDER_MAX	VM_LEVEL_0_ORDER
105f2a496d6SKonstantin Belousov #endif
106f2a496d6SKonstantin Belousov 
107f8a47341SAlan Cox /*
108f8a47341SAlan Cox  * The number of small pages that are contained in a level 0 reservation
109f8a47341SAlan Cox  */
110f8a47341SAlan Cox #define	VM_LEVEL_0_NPAGES	(1 << VM_LEVEL_0_ORDER)
111f2a496d6SKonstantin Belousov #define	VM_LEVEL_0_NPAGES_MAX	(1 << VM_LEVEL_0_ORDER_MAX)
112f8a47341SAlan Cox 
113f8a47341SAlan Cox /*
114f8a47341SAlan Cox  * The number of bits by which a physical address is shifted to obtain the
115f8a47341SAlan Cox  * reservation number
116f8a47341SAlan Cox  */
117f8a47341SAlan Cox #define	VM_LEVEL_0_SHIFT	(VM_LEVEL_0_ORDER + PAGE_SHIFT)
118f8a47341SAlan Cox 
119f8a47341SAlan Cox /*
120f8a47341SAlan Cox  * The size of a level 0 reservation in bytes
121f8a47341SAlan Cox  */
122f8a47341SAlan Cox #define	VM_LEVEL_0_SIZE		(1 << VM_LEVEL_0_SHIFT)
123f8a47341SAlan Cox 
124f8a47341SAlan Cox /*
125f8a47341SAlan Cox  * Computes the index of the small page underlying the given (object, pindex)
126f8a47341SAlan Cox  * within the reservation's array of small pages.
127f8a47341SAlan Cox  */
128f8a47341SAlan Cox #define	VM_RESERV_INDEX(object, pindex)	\
129f8a47341SAlan Cox     (((object)->pg_color + (pindex)) & (VM_LEVEL_0_NPAGES - 1))
130f8a47341SAlan Cox 
131f8a47341SAlan Cox /*
1322ef6727eSJeff Roberson  * Number of elapsed ticks before we update the LRU queue position.  Used
1332ef6727eSJeff Roberson  * to reduce contention and churn on the list.
1342ef6727eSJeff Roberson  */
1352ef6727eSJeff Roberson #define	PARTPOPSLOP	1
1362ef6727eSJeff Roberson 
1372ef6727eSJeff Roberson /*
138f8a47341SAlan Cox  * The reservation structure
139f8a47341SAlan Cox  *
140f8a47341SAlan Cox  * A reservation structure is constructed whenever a large physical page is
141f8a47341SAlan Cox  * speculatively allocated to an object.  The reservation provides the small
142f8a47341SAlan Cox  * physical pages for the range [pindex, pindex + VM_LEVEL_0_NPAGES) of offsets
143f8a47341SAlan Cox  * within that object.  The reservation's "popcnt" tracks the number of these
144f8a47341SAlan Cox  * small physical pages that are in use at any given time.  When and if the
1453453bca8SAlan Cox  * reservation is not fully utilized, it appears in the queue of partially
146f8a47341SAlan Cox  * populated reservations.  The reservation always appears on the containing
147f8a47341SAlan Cox  * object's list of reservations.
148f8a47341SAlan Cox  *
1493453bca8SAlan Cox  * A partially populated reservation can be broken and reclaimed at any time.
150e2068d0bSJeff Roberson  *
151b378d296SMark Johnston  * c - constant after boot
1525c930c89SJeff Roberson  * d - vm_reserv_domain_lock
153e2068d0bSJeff Roberson  * o - vm_reserv_object_lock
154b378d296SMark Johnston  * r - vm_reserv_lock
155b378d296SMark Johnston  * s - vm_reserv_domain_scan_lock
156f8a47341SAlan Cox  */
157f8a47341SAlan Cox struct vm_reserv {
1585c930c89SJeff Roberson 	struct mtx	lock;			/* reservation lock. */
159fe6d5344SMark Johnston 	TAILQ_ENTRY(vm_reserv) partpopq;	/* (d, r) per-domain queue. */
1605c930c89SJeff Roberson 	LIST_ENTRY(vm_reserv) objq;		/* (o, r) object queue */
1615c930c89SJeff Roberson 	vm_object_t	object;			/* (o, r) containing object */
1625c930c89SJeff Roberson 	vm_pindex_t	pindex;			/* (o, r) offset in object */
163e2068d0bSJeff Roberson 	vm_page_t	pages;			/* (c) first page  */
1645c930c89SJeff Roberson 	uint16_t	popcnt;			/* (r) # of pages in use */
165fe6d5344SMark Johnston 	uint8_t		domain;			/* (c) NUMA domain. */
166fe6d5344SMark Johnston 	char		inpartpopq;		/* (d, r) */
1672ef6727eSJeff Roberson 	int		lasttick;		/* (r) last pop update tick. */
16884e2ae64SDoug Moore 	bitstr_t	bit_decl(popmap, VM_LEVEL_0_NPAGES_MAX);
16984e2ae64SDoug Moore 						/* (r) bit vector, used pages */
170f8a47341SAlan Cox };
171f8a47341SAlan Cox 
172b378d296SMark Johnston TAILQ_HEAD(vm_reserv_queue, vm_reserv);
173b378d296SMark Johnston 
1745c930c89SJeff Roberson #define	vm_reserv_lockptr(rv)		(&(rv)->lock)
1755c930c89SJeff Roberson #define	vm_reserv_assert_locked(rv)					\
1765c930c89SJeff Roberson 	    mtx_assert(vm_reserv_lockptr(rv), MA_OWNED)
1775c930c89SJeff Roberson #define	vm_reserv_lock(rv)		mtx_lock(vm_reserv_lockptr(rv))
1785c930c89SJeff Roberson #define	vm_reserv_trylock(rv)		mtx_trylock(vm_reserv_lockptr(rv))
1795c930c89SJeff Roberson #define	vm_reserv_unlock(rv)		mtx_unlock(vm_reserv_lockptr(rv))
1805c930c89SJeff Roberson 
181f8a47341SAlan Cox /*
182f8a47341SAlan Cox  * The reservation array
183f8a47341SAlan Cox  *
184f8a47341SAlan Cox  * This array is analoguous in function to vm_page_array.  It differs in the
185f8a47341SAlan Cox  * respect that it may contain a greater number of useful reservation
186f8a47341SAlan Cox  * structures than there are (physical) superpages.  These "invalid"
187f8a47341SAlan Cox  * reservation structures exist to trade-off space for time in the
188f8a47341SAlan Cox  * implementation of vm_reserv_from_page().  Invalid reservation structures are
189f8a47341SAlan Cox  * distinguishable from "valid" reservation structures by inspecting the
190f8a47341SAlan Cox  * reservation's "pages" field.  Invalid reservation structures have a NULL
191f8a47341SAlan Cox  * "pages" field.
192f8a47341SAlan Cox  *
193f8a47341SAlan Cox  * vm_reserv_from_page() maps a small (physical) page to an element of this
194f8a47341SAlan Cox  * array by computing a physical reservation number from the page's physical
195f8a47341SAlan Cox  * address.  The physical reservation number is used as the array index.
196f8a47341SAlan Cox  *
197f8a47341SAlan Cox  * An "active" reservation is a valid reservation structure that has a non-NULL
198f8a47341SAlan Cox  * "object" field and a non-zero "popcnt" field.  In other words, every active
199f8a47341SAlan Cox  * reservation belongs to a particular object.  Moreover, every active
200f8a47341SAlan Cox  * reservation has an entry in the containing object's list of reservations.
201f8a47341SAlan Cox  */
202f8a47341SAlan Cox static vm_reserv_t vm_reserv_array;
203f8a47341SAlan Cox 
204f8a47341SAlan Cox /*
205fe6d5344SMark Johnston  * The per-domain partially populated reservation queues
206f8a47341SAlan Cox  *
207fe6d5344SMark Johnston  * These queues enable the fast recovery of an unused free small page from a
208fe6d5344SMark Johnston  * partially populated reservation.  The reservation at the head of a queue
2093453bca8SAlan Cox  * is the least recently changed, partially populated reservation.
210f8a47341SAlan Cox  *
211fe6d5344SMark Johnston  * Access to this queue is synchronized by the per-domain reservation lock.
212b378d296SMark Johnston  * Threads reclaiming free pages from the queue must hold the per-domain scan
213b378d296SMark Johnston  * lock.
214f8a47341SAlan Cox  */
215fe6d5344SMark Johnston struct vm_reserv_domain {
216fe6d5344SMark Johnston 	struct mtx 		lock;
217b378d296SMark Johnston 	struct vm_reserv_queue	partpop;	/* (d) */
218b378d296SMark Johnston 	struct vm_reserv	marker;		/* (d, s) scan marker/lock */
219fe6d5344SMark Johnston } __aligned(CACHE_LINE_SIZE);
220fe6d5344SMark Johnston 
221fe6d5344SMark Johnston static struct vm_reserv_domain vm_rvd[MAXMEMDOM];
222fe6d5344SMark Johnston 
223fe6d5344SMark Johnston #define	vm_reserv_domain_lockptr(d)	(&vm_rvd[(d)].lock)
224b378d296SMark Johnston #define	vm_reserv_domain_assert_locked(d)	\
225b378d296SMark Johnston 	mtx_assert(vm_reserv_domain_lockptr(d), MA_OWNED)
226fe6d5344SMark Johnston #define	vm_reserv_domain_lock(d)	mtx_lock(vm_reserv_domain_lockptr(d))
227fe6d5344SMark Johnston #define	vm_reserv_domain_unlock(d)	mtx_unlock(vm_reserv_domain_lockptr(d))
228f8a47341SAlan Cox 
229b378d296SMark Johnston #define	vm_reserv_domain_scan_lock(d)	mtx_lock(&vm_rvd[(d)].marker.lock)
230b378d296SMark Johnston #define	vm_reserv_domain_scan_unlock(d)	mtx_unlock(&vm_rvd[(d)].marker.lock)
231b378d296SMark Johnston 
2327029da5cSPawel Biernacki static SYSCTL_NODE(_vm, OID_AUTO, reserv, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
2337029da5cSPawel Biernacki     "Reservation Info");
234f8a47341SAlan Cox 
235d869a17eSMark Johnston static COUNTER_U64_DEFINE_EARLY(vm_reserv_broken);
2365c930c89SJeff Roberson SYSCTL_COUNTER_U64(_vm_reserv, OID_AUTO, broken, CTLFLAG_RD,
2375c930c89SJeff Roberson     &vm_reserv_broken, "Cumulative number of broken reservations");
238f8a47341SAlan Cox 
239d869a17eSMark Johnston static COUNTER_U64_DEFINE_EARLY(vm_reserv_freed);
2405c930c89SJeff Roberson SYSCTL_COUNTER_U64(_vm_reserv, OID_AUTO, freed, CTLFLAG_RD,
2415c930c89SJeff Roberson     &vm_reserv_freed, "Cumulative number of freed reservations");
242f8a47341SAlan Cox 
243e0a63baaSAlan Cox static int sysctl_vm_reserv_fullpop(SYSCTL_HANDLER_ARGS);
244e0a63baaSAlan Cox 
245a314aba8SMateusz Guzik SYSCTL_PROC(_vm_reserv, OID_AUTO, fullpop, CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RD,
246a314aba8SMateusz Guzik     NULL, 0, sysctl_vm_reserv_fullpop, "I", "Current number of full reservations");
247e0a63baaSAlan Cox 
248f8a47341SAlan Cox static int sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS);
249f8a47341SAlan Cox 
2507029da5cSPawel Biernacki SYSCTL_OID(_vm_reserv, OID_AUTO, partpopq,
251114484b7SMark Johnston     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
2527029da5cSPawel Biernacki     sysctl_vm_reserv_partpopq, "A",
2537029da5cSPawel Biernacki     "Partially populated reservation queues");
254f8a47341SAlan Cox 
255d869a17eSMark Johnston static COUNTER_U64_DEFINE_EARLY(vm_reserv_reclaimed);
2565c930c89SJeff Roberson SYSCTL_COUNTER_U64(_vm_reserv, OID_AUTO, reclaimed, CTLFLAG_RD,
2575c930c89SJeff Roberson     &vm_reserv_reclaimed, "Cumulative number of reclaimed reservations");
258f8a47341SAlan Cox 
259e2068d0bSJeff Roberson /*
260e2068d0bSJeff Roberson  * The object lock pool is used to synchronize the rvq.  We can not use a
261e2068d0bSJeff Roberson  * pool mutex because it is required before malloc works.
262e2068d0bSJeff Roberson  *
263e2068d0bSJeff Roberson  * The "hash" function could be made faster without divide and modulo.
264e2068d0bSJeff Roberson  */
265e2068d0bSJeff Roberson #define	VM_RESERV_OBJ_LOCK_COUNT	MAXCPU
266e2068d0bSJeff Roberson 
267e2068d0bSJeff Roberson struct mtx_padalign vm_reserv_object_mtx[VM_RESERV_OBJ_LOCK_COUNT];
268e2068d0bSJeff Roberson 
269e2068d0bSJeff Roberson #define	vm_reserv_object_lock_idx(object)			\
270e2068d0bSJeff Roberson 	    (((uintptr_t)object / sizeof(*object)) % VM_RESERV_OBJ_LOCK_COUNT)
271e2068d0bSJeff Roberson #define	vm_reserv_object_lock_ptr(object)			\
272e2068d0bSJeff Roberson 	    &vm_reserv_object_mtx[vm_reserv_object_lock_idx((object))]
273e2068d0bSJeff Roberson #define	vm_reserv_object_lock(object)				\
274e2068d0bSJeff Roberson 	    mtx_lock(vm_reserv_object_lock_ptr((object)))
275e2068d0bSJeff Roberson #define	vm_reserv_object_unlock(object)				\
276e2068d0bSJeff Roberson 	    mtx_unlock(vm_reserv_object_lock_ptr((object)))
277e2068d0bSJeff Roberson 
278ada27a3bSKonstantin Belousov static void		vm_reserv_break(vm_reserv_t rv);
279ec179322SAlan Cox static void		vm_reserv_depopulate(vm_reserv_t rv, int index);
280f8a47341SAlan Cox static vm_reserv_t	vm_reserv_from_page(vm_page_t m);
281f8a47341SAlan Cox static boolean_t	vm_reserv_has_pindex(vm_reserv_t rv,
282f8a47341SAlan Cox 			    vm_pindex_t pindex);
283ec179322SAlan Cox static void		vm_reserv_populate(vm_reserv_t rv, int index);
28444aab2c3SAlan Cox static void		vm_reserv_reclaim(vm_reserv_t rv);
285f8a47341SAlan Cox 
286f8a47341SAlan Cox /*
287e0a63baaSAlan Cox  * Returns the current number of full reservations.
288e0a63baaSAlan Cox  *
289fe6d5344SMark Johnston  * Since the number of full reservations is computed without acquiring any
290fe6d5344SMark Johnston  * locks, the returned value is inexact.
291e0a63baaSAlan Cox  */
292e0a63baaSAlan Cox static int
293e0a63baaSAlan Cox sysctl_vm_reserv_fullpop(SYSCTL_HANDLER_ARGS)
294e0a63baaSAlan Cox {
295e0a63baaSAlan Cox 	vm_paddr_t paddr;
296e0a63baaSAlan Cox 	struct vm_phys_seg *seg;
297e0a63baaSAlan Cox 	vm_reserv_t rv;
298e0a63baaSAlan Cox 	int fullpop, segind;
299e0a63baaSAlan Cox 
300e0a63baaSAlan Cox 	fullpop = 0;
301e0a63baaSAlan Cox 	for (segind = 0; segind < vm_phys_nsegs; segind++) {
302e0a63baaSAlan Cox 		seg = &vm_phys_segs[segind];
303e0a63baaSAlan Cox 		paddr = roundup2(seg->start, VM_LEVEL_0_SIZE);
3047988971aSD Scott Phillips #ifdef VM_PHYSSEG_SPARSE
3057988971aSD Scott Phillips 		rv = seg->first_reserv + (paddr >> VM_LEVEL_0_SHIFT) -
3067988971aSD Scott Phillips 		    (seg->start >> VM_LEVEL_0_SHIFT);
3077988971aSD Scott Phillips #else
3087988971aSD Scott Phillips 		rv = &vm_reserv_array[paddr >> VM_LEVEL_0_SHIFT];
3097988971aSD Scott Phillips #endif
3106b821a74SAleksandr Rybalko 		while (paddr + VM_LEVEL_0_SIZE > paddr && paddr +
3116b821a74SAleksandr Rybalko 		    VM_LEVEL_0_SIZE <= seg->end) {
312e0a63baaSAlan Cox 			fullpop += rv->popcnt == VM_LEVEL_0_NPAGES;
313e0a63baaSAlan Cox 			paddr += VM_LEVEL_0_SIZE;
3147988971aSD Scott Phillips 			rv++;
315e0a63baaSAlan Cox 		}
316e0a63baaSAlan Cox 	}
317e0a63baaSAlan Cox 	return (sysctl_handle_int(oidp, &fullpop, 0, req));
318e0a63baaSAlan Cox }
319e0a63baaSAlan Cox 
320e0a63baaSAlan Cox /*
3213453bca8SAlan Cox  * Describes the current state of the partially populated reservation queue.
322f8a47341SAlan Cox  */
323f8a47341SAlan Cox static int
324f8a47341SAlan Cox sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS)
325f8a47341SAlan Cox {
326f8a47341SAlan Cox 	struct sbuf sbuf;
327f8a47341SAlan Cox 	vm_reserv_t rv;
328ef435ae7SJeff Roberson 	int counter, error, domain, level, unused_pages;
329f8a47341SAlan Cox 
33000f0e671SMatthew D Fleming 	error = sysctl_wire_old_buffer(req, 0);
33100f0e671SMatthew D Fleming 	if (error != 0)
33200f0e671SMatthew D Fleming 		return (error);
3334e657159SMatthew D Fleming 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
334ef435ae7SJeff Roberson 	sbuf_printf(&sbuf, "\nDOMAIN    LEVEL     SIZE  NUMBER\n\n");
335ef435ae7SJeff Roberson 	for (domain = 0; domain < vm_ndomains; domain++) {
336f8a47341SAlan Cox 		for (level = -1; level <= VM_NRESERVLEVEL - 2; level++) {
337f8a47341SAlan Cox 			counter = 0;
338f8a47341SAlan Cox 			unused_pages = 0;
3395c930c89SJeff Roberson 			vm_reserv_domain_lock(domain);
340fe6d5344SMark Johnston 			TAILQ_FOREACH(rv, &vm_rvd[domain].partpop, partpopq) {
341b378d296SMark Johnston 				if (rv == &vm_rvd[domain].marker)
342b378d296SMark Johnston 					continue;
343f8a47341SAlan Cox 				counter++;
344f8a47341SAlan Cox 				unused_pages += VM_LEVEL_0_NPAGES - rv->popcnt;
345f8a47341SAlan Cox 			}
3465c930c89SJeff Roberson 			vm_reserv_domain_unlock(domain);
347ef435ae7SJeff Roberson 			sbuf_printf(&sbuf, "%6d, %7d, %6dK, %6d\n",
348ef435ae7SJeff Roberson 			    domain, level,
3492cf36c8fSAlan Cox 			    unused_pages * ((int)PAGE_SIZE / 1024), counter);
350f8a47341SAlan Cox 		}
351ef435ae7SJeff Roberson 	}
3524e657159SMatthew D Fleming 	error = sbuf_finish(&sbuf);
353f8a47341SAlan Cox 	sbuf_delete(&sbuf);
354f8a47341SAlan Cox 	return (error);
355f8a47341SAlan Cox }
356f8a47341SAlan Cox 
357f8a47341SAlan Cox /*
358e2068d0bSJeff Roberson  * Remove a reservation from the object's objq.
359e2068d0bSJeff Roberson  */
360e2068d0bSJeff Roberson static void
361e2068d0bSJeff Roberson vm_reserv_remove(vm_reserv_t rv)
362e2068d0bSJeff Roberson {
363e2068d0bSJeff Roberson 	vm_object_t object;
364e2068d0bSJeff Roberson 
3655c930c89SJeff Roberson 	vm_reserv_assert_locked(rv);
3665c930c89SJeff Roberson 	CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d",
3675c930c89SJeff Roberson 	    __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq);
368e2068d0bSJeff Roberson 	KASSERT(rv->object != NULL,
369e2068d0bSJeff Roberson 	    ("vm_reserv_remove: reserv %p is free", rv));
370e2068d0bSJeff Roberson 	KASSERT(!rv->inpartpopq,
371e2068d0bSJeff Roberson 	    ("vm_reserv_remove: reserv %p's inpartpopq is TRUE", rv));
372e2068d0bSJeff Roberson 	object = rv->object;
373e2068d0bSJeff Roberson 	vm_reserv_object_lock(object);
374e2068d0bSJeff Roberson 	LIST_REMOVE(rv, objq);
375e2068d0bSJeff Roberson 	rv->object = NULL;
376e2068d0bSJeff Roberson 	vm_reserv_object_unlock(object);
377e2068d0bSJeff Roberson }
378e2068d0bSJeff Roberson 
379e2068d0bSJeff Roberson /*
380e2068d0bSJeff Roberson  * Insert a new reservation into the object's objq.
381e2068d0bSJeff Roberson  */
382e2068d0bSJeff Roberson static void
383e2068d0bSJeff Roberson vm_reserv_insert(vm_reserv_t rv, vm_object_t object, vm_pindex_t pindex)
384e2068d0bSJeff Roberson {
385e2068d0bSJeff Roberson 
3865c930c89SJeff Roberson 	vm_reserv_assert_locked(rv);
3875c930c89SJeff Roberson 	CTR6(KTR_VM,
3885c930c89SJeff Roberson 	    "%s: rv %p(%p) object %p new %p popcnt %d",
3895c930c89SJeff Roberson 	    __FUNCTION__, rv, rv->pages, rv->object, object,
3905c930c89SJeff Roberson 	   rv->popcnt);
391e2068d0bSJeff Roberson 	KASSERT(rv->object == NULL,
392e2068d0bSJeff Roberson 	    ("vm_reserv_insert: reserv %p isn't free", rv));
393e2068d0bSJeff Roberson 	KASSERT(rv->popcnt == 0,
394e2068d0bSJeff Roberson 	    ("vm_reserv_insert: reserv %p's popcnt is corrupted", rv));
395e2068d0bSJeff Roberson 	KASSERT(!rv->inpartpopq,
396e2068d0bSJeff Roberson 	    ("vm_reserv_insert: reserv %p's inpartpopq is TRUE", rv));
39784e2ae64SDoug Moore 	KASSERT(bit_ntest(rv->popmap, 0, VM_LEVEL_0_NPAGES - 1, 0),
398e2068d0bSJeff Roberson 	    ("vm_reserv_insert: reserv %p's popmap is corrupted", rv));
399e2068d0bSJeff Roberson 	vm_reserv_object_lock(object);
400e2068d0bSJeff Roberson 	rv->pindex = pindex;
401e2068d0bSJeff Roberson 	rv->object = object;
4022ef6727eSJeff Roberson 	rv->lasttick = ticks;
403e2068d0bSJeff Roberson 	LIST_INSERT_HEAD(&object->rvq, rv, objq);
404e2068d0bSJeff Roberson 	vm_reserv_object_unlock(object);
405e2068d0bSJeff Roberson }
406e2068d0bSJeff Roberson 
4073e00c11aSAlan Cox #ifdef VM_SUBLEVEL_0_NPAGES
4083e00c11aSAlan Cox static inline bool
4093e00c11aSAlan Cox vm_reserv_is_sublevel_full(vm_reserv_t rv, int index)
4103e00c11aSAlan Cox {
4113e00c11aSAlan Cox 	_Static_assert(VM_SUBLEVEL_0_NPAGES == 16 ||
4123e00c11aSAlan Cox 	    VM_SUBLEVEL_0_NPAGES == 128,
4133e00c11aSAlan Cox 	    "vm_reserv_is_sublevel_full: unsupported VM_SUBLEVEL_0_NPAGES");
4143e00c11aSAlan Cox 	/* An equivalent bit_ntest() compiles to more instructions. */
4153e00c11aSAlan Cox 	switch (VM_SUBLEVEL_0_NPAGES) {
4163e00c11aSAlan Cox 	case 16:
4173e00c11aSAlan Cox 		return (((uint16_t *)rv->popmap)[index / 16] == UINT16_MAX);
4183e00c11aSAlan Cox 	case 128:
4193e00c11aSAlan Cox 		index = rounddown2(index, 128) / 64;
4203e00c11aSAlan Cox 		return (((uint64_t *)rv->popmap)[index] == UINT64_MAX &&
4213e00c11aSAlan Cox 		    ((uint64_t *)rv->popmap)[index + 1] == UINT64_MAX);
4223e00c11aSAlan Cox 	default:
4233e00c11aSAlan Cox 		__unreachable();
4243e00c11aSAlan Cox 	}
4253e00c11aSAlan Cox }
4263e00c11aSAlan Cox #endif
4273e00c11aSAlan Cox 
428e2068d0bSJeff Roberson /*
429f8a47341SAlan Cox  * Reduces the given reservation's population count.  If the population count
430f8a47341SAlan Cox  * becomes zero, the reservation is destroyed.  Additionally, moves the
4313453bca8SAlan Cox  * reservation to the tail of the partially populated reservation queue if the
432f8a47341SAlan Cox  * population count is non-zero.
433f8a47341SAlan Cox  */
434f8a47341SAlan Cox static void
435ec179322SAlan Cox vm_reserv_depopulate(vm_reserv_t rv, int index)
436f8a47341SAlan Cox {
4375c930c89SJeff Roberson 	struct vm_domain *vmd;
438f8a47341SAlan Cox 
4395c930c89SJeff Roberson 	vm_reserv_assert_locked(rv);
4405c930c89SJeff Roberson 	CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d",
4415c930c89SJeff Roberson 	    __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq);
442f8a47341SAlan Cox 	KASSERT(rv->object != NULL,
443f8a47341SAlan Cox 	    ("vm_reserv_depopulate: reserv %p is free", rv));
44484e2ae64SDoug Moore 	KASSERT(bit_test(rv->popmap, index),
445a08c1515SAlan Cox 	    ("vm_reserv_depopulate: reserv %p's popmap[%d] is clear", rv,
446a08c1515SAlan Cox 	    index));
447f8a47341SAlan Cox 	KASSERT(rv->popcnt > 0,
448f8a47341SAlan Cox 	    ("vm_reserv_depopulate: reserv %p's popcnt is corrupted", rv));
4492d3f4181SJeff Roberson 	KASSERT(rv->domain < vm_ndomains,
450ef435ae7SJeff Roberson 	    ("vm_reserv_depopulate: reserv %p's domain is corrupted %d",
451ef435ae7SJeff Roberson 	    rv, rv->domain));
4525c930c89SJeff Roberson 	if (rv->popcnt == VM_LEVEL_0_NPAGES) {
4533e00c11aSAlan Cox 		KASSERT(rv->pages->psind == VM_LEVEL_0_PSIND,
454dd05fa19SAlan Cox 		    ("vm_reserv_depopulate: reserv %p is already demoted",
455dd05fa19SAlan Cox 		    rv));
4563e00c11aSAlan Cox 		rv->pages->psind = VM_LEVEL_0_PSIND - 1;
457f8a47341SAlan Cox 	}
4583e00c11aSAlan Cox #ifdef VM_SUBLEVEL_0_NPAGES
4593e00c11aSAlan Cox 	if (vm_reserv_is_sublevel_full(rv, index))
4603e00c11aSAlan Cox 		rv->pages[rounddown2(index, VM_SUBLEVEL_0_NPAGES)].psind = 0;
4613e00c11aSAlan Cox #endif
46284e2ae64SDoug Moore 	bit_clear(rv->popmap, index);
463f8a47341SAlan Cox 	rv->popcnt--;
4642ef6727eSJeff Roberson 	if ((unsigned)(ticks - rv->lasttick) >= PARTPOPSLOP ||
4652ef6727eSJeff Roberson 	    rv->popcnt == 0) {
4665c930c89SJeff Roberson 		vm_reserv_domain_lock(rv->domain);
4675c930c89SJeff Roberson 		if (rv->inpartpopq) {
468fe6d5344SMark Johnston 			TAILQ_REMOVE(&vm_rvd[rv->domain].partpop, rv, partpopq);
4695c930c89SJeff Roberson 			rv->inpartpopq = FALSE;
4705c930c89SJeff Roberson 		}
4715c930c89SJeff Roberson 		if (rv->popcnt != 0) {
472f8a47341SAlan Cox 			rv->inpartpopq = TRUE;
473fe6d5344SMark Johnston 			TAILQ_INSERT_TAIL(&vm_rvd[rv->domain].partpop, rv,
474fe6d5344SMark Johnston 			    partpopq);
475f8a47341SAlan Cox 		}
4765c930c89SJeff Roberson 		vm_reserv_domain_unlock(rv->domain);
4772ef6727eSJeff Roberson 		rv->lasttick = ticks;
4782ef6727eSJeff Roberson 	}
4795c930c89SJeff Roberson 	vmd = VM_DOMAIN(rv->domain);
4805c930c89SJeff Roberson 	if (rv->popcnt == 0) {
4815c930c89SJeff Roberson 		vm_reserv_remove(rv);
4825c930c89SJeff Roberson 		vm_domain_free_lock(vmd);
483*0078df5fSDoug Moore 		vm_phys_free_pages(rv->pages, rv->pages->pool, VM_LEVEL_0_ORDER);
4845c930c89SJeff Roberson 		vm_domain_free_unlock(vmd);
4855c930c89SJeff Roberson 		counter_u64_add(vm_reserv_freed, 1);
4865c930c89SJeff Roberson 	}
4875c930c89SJeff Roberson 	vm_domain_freecnt_inc(vmd, 1);
488f8a47341SAlan Cox }
489f8a47341SAlan Cox 
490f8a47341SAlan Cox /*
491f8a47341SAlan Cox  * Returns the reservation to which the given page might belong.
492f8a47341SAlan Cox  */
493f8a47341SAlan Cox static __inline vm_reserv_t
494f8a47341SAlan Cox vm_reserv_from_page(vm_page_t m)
495f8a47341SAlan Cox {
4967988971aSD Scott Phillips #ifdef VM_PHYSSEG_SPARSE
4977988971aSD Scott Phillips 	struct vm_phys_seg *seg;
498f8a47341SAlan Cox 
4997988971aSD Scott Phillips 	seg = &vm_phys_segs[m->segind];
5007988971aSD Scott Phillips 	return (seg->first_reserv + (VM_PAGE_TO_PHYS(m) >> VM_LEVEL_0_SHIFT) -
5017988971aSD Scott Phillips 	    (seg->start >> VM_LEVEL_0_SHIFT));
5027988971aSD Scott Phillips #else
503f8a47341SAlan Cox 	return (&vm_reserv_array[VM_PAGE_TO_PHYS(m) >> VM_LEVEL_0_SHIFT]);
5047988971aSD Scott Phillips #endif
505f8a47341SAlan Cox }
506f8a47341SAlan Cox 
507f8a47341SAlan Cox /*
508e2068d0bSJeff Roberson  * Returns an existing reservation or NULL and initialized successor pointer.
509e2068d0bSJeff Roberson  */
510e2068d0bSJeff Roberson static vm_reserv_t
511e2068d0bSJeff Roberson vm_reserv_from_object(vm_object_t object, vm_pindex_t pindex,
512e2068d0bSJeff Roberson     vm_page_t mpred, vm_page_t *msuccp)
513e2068d0bSJeff Roberson {
514e2068d0bSJeff Roberson 	vm_reserv_t rv;
515e2068d0bSJeff Roberson 	vm_page_t msucc;
516e2068d0bSJeff Roberson 
517e2068d0bSJeff Roberson 	msucc = NULL;
518e2068d0bSJeff Roberson 	if (mpred != NULL) {
519e2068d0bSJeff Roberson 		KASSERT(mpred->object == object,
520e2068d0bSJeff Roberson 		    ("vm_reserv_from_object: object doesn't contain mpred"));
521e2068d0bSJeff Roberson 		KASSERT(mpred->pindex < pindex,
522e2068d0bSJeff Roberson 		    ("vm_reserv_from_object: mpred doesn't precede pindex"));
523e2068d0bSJeff Roberson 		rv = vm_reserv_from_page(mpred);
524e2068d0bSJeff Roberson 		if (rv->object == object && vm_reserv_has_pindex(rv, pindex))
525e2068d0bSJeff Roberson 			goto found;
526e2068d0bSJeff Roberson 		msucc = TAILQ_NEXT(mpred, listq);
527e2068d0bSJeff Roberson 	} else
528e2068d0bSJeff Roberson 		msucc = TAILQ_FIRST(&object->memq);
529e2068d0bSJeff Roberson 	if (msucc != NULL) {
530e2068d0bSJeff Roberson 		KASSERT(msucc->pindex > pindex,
531e2068d0bSJeff Roberson 		    ("vm_reserv_from_object: msucc doesn't succeed pindex"));
532e2068d0bSJeff Roberson 		rv = vm_reserv_from_page(msucc);
533e2068d0bSJeff Roberson 		if (rv->object == object && vm_reserv_has_pindex(rv, pindex))
534e2068d0bSJeff Roberson 			goto found;
535e2068d0bSJeff Roberson 	}
536e2068d0bSJeff Roberson 	rv = NULL;
537e2068d0bSJeff Roberson 
538e2068d0bSJeff Roberson found:
539e2068d0bSJeff Roberson 	*msuccp = msucc;
540e2068d0bSJeff Roberson 
541e2068d0bSJeff Roberson 	return (rv);
542e2068d0bSJeff Roberson }
543e2068d0bSJeff Roberson 
544e2068d0bSJeff Roberson /*
545f8a47341SAlan Cox  * Returns TRUE if the given reservation contains the given page index and
546f8a47341SAlan Cox  * FALSE otherwise.
547f8a47341SAlan Cox  */
548f8a47341SAlan Cox static __inline boolean_t
549f8a47341SAlan Cox vm_reserv_has_pindex(vm_reserv_t rv, vm_pindex_t pindex)
550f8a47341SAlan Cox {
551f8a47341SAlan Cox 
552f8a47341SAlan Cox 	return (((pindex - rv->pindex) & ~(VM_LEVEL_0_NPAGES - 1)) == 0);
553f8a47341SAlan Cox }
554f8a47341SAlan Cox 
555f8a47341SAlan Cox /*
556f8a47341SAlan Cox  * Increases the given reservation's population count.  Moves the reservation
5573453bca8SAlan Cox  * to the tail of the partially populated reservation queue.
558f8a47341SAlan Cox  */
559f8a47341SAlan Cox static void
560ec179322SAlan Cox vm_reserv_populate(vm_reserv_t rv, int index)
561f8a47341SAlan Cox {
562f8a47341SAlan Cox 
5635c930c89SJeff Roberson 	vm_reserv_assert_locked(rv);
5645c930c89SJeff Roberson 	CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d",
5655c930c89SJeff Roberson 	    __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq);
566f8a47341SAlan Cox 	KASSERT(rv->object != NULL,
567f8a47341SAlan Cox 	    ("vm_reserv_populate: reserv %p is free", rv));
56884e2ae64SDoug Moore 	KASSERT(!bit_test(rv->popmap, index),
569a08c1515SAlan Cox 	    ("vm_reserv_populate: reserv %p's popmap[%d] is set", rv,
570a08c1515SAlan Cox 	    index));
571f8a47341SAlan Cox 	KASSERT(rv->popcnt < VM_LEVEL_0_NPAGES,
572f8a47341SAlan Cox 	    ("vm_reserv_populate: reserv %p is already full", rv));
5733e00c11aSAlan Cox 	KASSERT(rv->pages->psind >= 0 &&
5743e00c11aSAlan Cox 	    rv->pages->psind < VM_LEVEL_0_PSIND,
575dd05fa19SAlan Cox 	    ("vm_reserv_populate: reserv %p is already promoted", rv));
5762d3f4181SJeff Roberson 	KASSERT(rv->domain < vm_ndomains,
577ef435ae7SJeff Roberson 	    ("vm_reserv_populate: reserv %p's domain is corrupted %d",
578ef435ae7SJeff Roberson 	    rv, rv->domain));
57984e2ae64SDoug Moore 	bit_set(rv->popmap, index);
5803e00c11aSAlan Cox #ifdef VM_SUBLEVEL_0_NPAGES
5813e00c11aSAlan Cox 	if (vm_reserv_is_sublevel_full(rv, index))
5823e00c11aSAlan Cox 		rv->pages[rounddown2(index, VM_SUBLEVEL_0_NPAGES)].psind = 1;
5833e00c11aSAlan Cox #endif
5845c930c89SJeff Roberson 	rv->popcnt++;
5852ef6727eSJeff Roberson 	if ((unsigned)(ticks - rv->lasttick) < PARTPOPSLOP &&
5862ef6727eSJeff Roberson 	    rv->inpartpopq && rv->popcnt != VM_LEVEL_0_NPAGES)
5872ef6727eSJeff Roberson 		return;
5882ef6727eSJeff Roberson 	rv->lasttick = ticks;
5895c930c89SJeff Roberson 	vm_reserv_domain_lock(rv->domain);
590f8a47341SAlan Cox 	if (rv->inpartpopq) {
591fe6d5344SMark Johnston 		TAILQ_REMOVE(&vm_rvd[rv->domain].partpop, rv, partpopq);
592f8a47341SAlan Cox 		rv->inpartpopq = FALSE;
593f8a47341SAlan Cox 	}
594f8a47341SAlan Cox 	if (rv->popcnt < VM_LEVEL_0_NPAGES) {
595f8a47341SAlan Cox 		rv->inpartpopq = TRUE;
596fe6d5344SMark Johnston 		TAILQ_INSERT_TAIL(&vm_rvd[rv->domain].partpop, rv, partpopq);
5975c930c89SJeff Roberson 	} else {
5983e00c11aSAlan Cox 		KASSERT(rv->pages->psind == VM_LEVEL_0_PSIND - 1,
5995c930c89SJeff Roberson 		    ("vm_reserv_populate: reserv %p is already promoted",
6005c930c89SJeff Roberson 		    rv));
6013e00c11aSAlan Cox 		rv->pages->psind = VM_LEVEL_0_PSIND;
602f8a47341SAlan Cox 	}
6035c930c89SJeff Roberson 	vm_reserv_domain_unlock(rv->domain);
6045c930c89SJeff Roberson }
605f8a47341SAlan Cox 
606f8a47341SAlan Cox /*
607e2068d0bSJeff Roberson  * Allocates a contiguous set of physical pages of the given size "npages"
6082d5039dbSAlan Cox  * from existing or newly created reservations.  All of the physical pages
609e2068d0bSJeff Roberson  * must be at or above the given physical address "low" and below the given
610e2068d0bSJeff Roberson  * physical address "high".  The given value "alignment" determines the
611e2068d0bSJeff Roberson  * alignment of the first physical page in the set.  If the given value
612e2068d0bSJeff Roberson  * "boundary" is non-zero, then the set of physical pages cannot cross any
613e2068d0bSJeff Roberson  * physical address boundary that is a multiple of that value.  Both
614e2068d0bSJeff Roberson  * "alignment" and "boundary" must be a power of two.
615e2068d0bSJeff Roberson  *
616e2068d0bSJeff Roberson  * The page "mpred" must immediately precede the offset "pindex" within the
617e2068d0bSJeff Roberson  * specified object.
618e2068d0bSJeff Roberson  *
6192d5039dbSAlan Cox  * The object must be locked.
620e2068d0bSJeff Roberson  */
621e2068d0bSJeff Roberson vm_page_t
6222d5039dbSAlan Cox vm_reserv_alloc_contig(vm_object_t object, vm_pindex_t pindex, int domain,
6232d5039dbSAlan Cox     int req, vm_page_t mpred, u_long npages, vm_paddr_t low, vm_paddr_t high,
6242d5039dbSAlan Cox     u_long alignment, vm_paddr_t boundary)
625c68c3537SAlan Cox {
6265c930c89SJeff Roberson 	struct vm_domain *vmd;
627c68c3537SAlan Cox 	vm_paddr_t pa, size;
628920da7e4SAlan Cox 	vm_page_t m, m_ret, msucc;
629c68c3537SAlan Cox 	vm_pindex_t first, leftcap, rightcap;
630c68c3537SAlan Cox 	vm_reserv_t rv;
631c68c3537SAlan Cox 	u_long allocpages, maxpages, minpages;
632c68c3537SAlan Cox 	int i, index, n;
633c68c3537SAlan Cox 
63489f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
635c68c3537SAlan Cox 	KASSERT(npages != 0, ("vm_reserv_alloc_contig: npages is 0"));
636c68c3537SAlan Cox 
637c68c3537SAlan Cox 	/*
638c68c3537SAlan Cox 	 * Is a reservation fundamentally impossible?
639c68c3537SAlan Cox 	 */
640c68c3537SAlan Cox 	if (pindex < VM_RESERV_INDEX(object, pindex) ||
641c68c3537SAlan Cox 	    pindex + npages > object->size)
642c68c3537SAlan Cox 		return (NULL);
643c68c3537SAlan Cox 
644c68c3537SAlan Cox 	/*
645c68c3537SAlan Cox 	 * All reservations of a particular size have the same alignment.
646c68c3537SAlan Cox 	 * Assuming that the first page is allocated from a reservation, the
647c68c3537SAlan Cox 	 * least significant bits of its physical address can be determined
648c68c3537SAlan Cox 	 * from its offset from the beginning of the reservation and the size
649c68c3537SAlan Cox 	 * of the reservation.
650c68c3537SAlan Cox 	 *
651c68c3537SAlan Cox 	 * Could the specified index within a reservation of the smallest
652c68c3537SAlan Cox 	 * possible size satisfy the alignment and boundary requirements?
653c68c3537SAlan Cox 	 */
654c68c3537SAlan Cox 	pa = VM_RESERV_INDEX(object, pindex) << PAGE_SHIFT;
655c68c3537SAlan Cox 	size = npages << PAGE_SHIFT;
656c606ab59SDoug Moore 	if (!vm_addr_ok(pa, size, alignment, boundary))
657c68c3537SAlan Cox 		return (NULL);
658c68c3537SAlan Cox 
659c68c3537SAlan Cox 	/*
6602d5039dbSAlan Cox 	 * Look for an existing reservation.
661c68c3537SAlan Cox 	 */
662e2068d0bSJeff Roberson 	rv = vm_reserv_from_object(object, pindex, mpred, &msucc);
6632d5039dbSAlan Cox 	if (rv != NULL) {
6642d5039dbSAlan Cox 		KASSERT(object != kernel_object || rv->domain == domain,
6652d5039dbSAlan Cox 		    ("vm_reserv_alloc_contig: domain mismatch"));
6662d5039dbSAlan Cox 		index = VM_RESERV_INDEX(object, pindex);
6672d5039dbSAlan Cox 		/* Does the allocation fit within the reservation? */
6682d5039dbSAlan Cox 		if (index + npages > VM_LEVEL_0_NPAGES)
669e2068d0bSJeff Roberson 			return (NULL);
6702d5039dbSAlan Cox 		domain = rv->domain;
6712d5039dbSAlan Cox 		vmd = VM_DOMAIN(domain);
6722d5039dbSAlan Cox 		vm_reserv_lock(rv);
6732d5039dbSAlan Cox 		/* Handle reclaim race. */
6742d5039dbSAlan Cox 		if (rv->object != object)
6752d5039dbSAlan Cox 			goto out;
6762d5039dbSAlan Cox 		m = &rv->pages[index];
6772d5039dbSAlan Cox 		pa = VM_PAGE_TO_PHYS(m);
6782d5039dbSAlan Cox 		if (pa < low || pa + size > high ||
679c606ab59SDoug Moore 		    !vm_addr_ok(pa, size, alignment, boundary))
6802d5039dbSAlan Cox 			goto out;
681c296ac7eSAlan Cox 		/* Handle vm_page_iter_rename(..., m, new_object, ...). */
68284e2ae64SDoug Moore 		if (!bit_ntest(rv->popmap, index, index + npages - 1, 0))
6832d5039dbSAlan Cox 			goto out;
6842d5039dbSAlan Cox 		if (!vm_domain_allocate(vmd, req, npages))
6852d5039dbSAlan Cox 			goto out;
6862d5039dbSAlan Cox 		for (i = 0; i < npages; i++)
6872d5039dbSAlan Cox 			vm_reserv_populate(rv, index + i);
6882d5039dbSAlan Cox 		vm_reserv_unlock(rv);
6892d5039dbSAlan Cox 		return (m);
6902d5039dbSAlan Cox out:
6912d5039dbSAlan Cox 		vm_reserv_unlock(rv);
6922d5039dbSAlan Cox 		return (NULL);
6932d5039dbSAlan Cox 	}
694c68c3537SAlan Cox 
695c68c3537SAlan Cox 	/*
696c68c3537SAlan Cox 	 * Could at least one reservation fit between the first index to the
69764f096eeSAlan Cox 	 * left that can be used ("leftcap") and the first index to the right
69864f096eeSAlan Cox 	 * that cannot be used ("rightcap")?
699e2068d0bSJeff Roberson 	 *
700e2068d0bSJeff Roberson 	 * We must synchronize with the reserv object lock to protect the
701e2068d0bSJeff Roberson 	 * pindex/object of the resulting reservations against rename while
702e2068d0bSJeff Roberson 	 * we are inspecting.
703c68c3537SAlan Cox 	 */
704c68c3537SAlan Cox 	first = pindex - VM_RESERV_INDEX(object, pindex);
705e2068d0bSJeff Roberson 	minpages = VM_RESERV_INDEX(object, pindex) + npages;
706e2068d0bSJeff Roberson 	maxpages = roundup2(minpages, VM_LEVEL_0_NPAGES);
707e2068d0bSJeff Roberson 	allocpages = maxpages;
708e2068d0bSJeff Roberson 	vm_reserv_object_lock(object);
709c68c3537SAlan Cox 	if (mpred != NULL) {
710c68c3537SAlan Cox 		if ((rv = vm_reserv_from_page(mpred))->object != object)
711c68c3537SAlan Cox 			leftcap = mpred->pindex + 1;
712c68c3537SAlan Cox 		else
713c68c3537SAlan Cox 			leftcap = rv->pindex + VM_LEVEL_0_NPAGES;
714e2068d0bSJeff Roberson 		if (leftcap > first) {
715e2068d0bSJeff Roberson 			vm_reserv_object_unlock(object);
716c68c3537SAlan Cox 			return (NULL);
717c68c3537SAlan Cox 		}
718e2068d0bSJeff Roberson 	}
719c68c3537SAlan Cox 	if (msucc != NULL) {
720c68c3537SAlan Cox 		if ((rv = vm_reserv_from_page(msucc))->object != object)
721c68c3537SAlan Cox 			rightcap = msucc->pindex;
722c68c3537SAlan Cox 		else
723c68c3537SAlan Cox 			rightcap = rv->pindex;
724c68c3537SAlan Cox 		if (first + maxpages > rightcap) {
725e2068d0bSJeff Roberson 			if (maxpages == VM_LEVEL_0_NPAGES) {
726e2068d0bSJeff Roberson 				vm_reserv_object_unlock(object);
727c68c3537SAlan Cox 				return (NULL);
728e2068d0bSJeff Roberson 			}
72964f096eeSAlan Cox 
73064f096eeSAlan Cox 			/*
73164f096eeSAlan Cox 			 * At least one reservation will fit between "leftcap"
73264f096eeSAlan Cox 			 * and "rightcap".  However, a reservation for the
73364f096eeSAlan Cox 			 * last of the requested pages will not fit.  Reduce
73464f096eeSAlan Cox 			 * the size of the upcoming allocation accordingly.
73564f096eeSAlan Cox 			 */
736c68c3537SAlan Cox 			allocpages = minpages;
737c68c3537SAlan Cox 		}
738c68c3537SAlan Cox 	}
739e2068d0bSJeff Roberson 	vm_reserv_object_unlock(object);
740c68c3537SAlan Cox 
741c68c3537SAlan Cox 	/*
742c68c3537SAlan Cox 	 * Would the last new reservation extend past the end of the object?
74363967687SJeff Roberson 	 *
74463967687SJeff Roberson 	 * If the object is unlikely to grow don't allocate a reservation for
74563967687SJeff Roberson 	 * the tail.
746c68c3537SAlan Cox 	 */
74763967687SJeff Roberson 	if ((object->flags & OBJ_ANON) == 0 &&
74863967687SJeff Roberson 	    first + maxpages > object->size) {
749c68c3537SAlan Cox 		if (maxpages == VM_LEVEL_0_NPAGES)
750c68c3537SAlan Cox 			return (NULL);
751c68c3537SAlan Cox 		allocpages = minpages;
752c68c3537SAlan Cox 	}
753c68c3537SAlan Cox 
754c68c3537SAlan Cox 	/*
75564f096eeSAlan Cox 	 * Allocate the physical pages.  The alignment and boundary specified
75664f096eeSAlan Cox 	 * for this allocation may be different from the alignment and
75764f096eeSAlan Cox 	 * boundary specified for the requested pages.  For instance, the
75864f096eeSAlan Cox 	 * specified index may not be the first page within the first new
75964f096eeSAlan Cox 	 * reservation.
760c68c3537SAlan Cox 	 */
7615c930c89SJeff Roberson 	m = NULL;
7625c930c89SJeff Roberson 	vmd = VM_DOMAIN(domain);
7635c930c89SJeff Roberson 	if (vm_domain_allocate(vmd, req, npages)) {
7645c930c89SJeff Roberson 		vm_domain_free_lock(vmd);
7655c930c89SJeff Roberson 		m = vm_phys_alloc_contig(domain, allocpages, low, high,
7665c930c89SJeff Roberson 		    ulmax(alignment, VM_LEVEL_0_SIZE),
7675c930c89SJeff Roberson 		    boundary > VM_LEVEL_0_SIZE ? boundary : 0);
7685c930c89SJeff Roberson 		vm_domain_free_unlock(vmd);
7695c930c89SJeff Roberson 		if (m == NULL) {
7705c930c89SJeff Roberson 			vm_domain_freecnt_inc(vmd, npages);
7715c930c89SJeff Roberson 			return (NULL);
7725c930c89SJeff Roberson 		}
7735c930c89SJeff Roberson 	} else
774c68c3537SAlan Cox 		return (NULL);
775431fb8abSMark Johnston 	KASSERT(vm_page_domain(m) == domain,
7767a469c8eSJeff Roberson 	    ("vm_reserv_alloc_contig: Page domain does not match requested."));
77764f096eeSAlan Cox 
77864f096eeSAlan Cox 	/*
77964f096eeSAlan Cox 	 * The allocated physical pages always begin at a reservation
78064f096eeSAlan Cox 	 * boundary, but they do not always end at a reservation boundary.
78164f096eeSAlan Cox 	 * Initialize every reservation that is completely covered by the
78264f096eeSAlan Cox 	 * allocated physical pages.
78364f096eeSAlan Cox 	 */
784c68c3537SAlan Cox 	m_ret = NULL;
785c68c3537SAlan Cox 	index = VM_RESERV_INDEX(object, pindex);
786c68c3537SAlan Cox 	do {
787c68c3537SAlan Cox 		rv = vm_reserv_from_page(m);
788c68c3537SAlan Cox 		KASSERT(rv->pages == m,
789c68c3537SAlan Cox 		    ("vm_reserv_alloc_contig: reserv %p's pages is corrupted",
790c68c3537SAlan Cox 		    rv));
7915c930c89SJeff Roberson 		vm_reserv_lock(rv);
792e2068d0bSJeff Roberson 		vm_reserv_insert(rv, object, first);
793c68c3537SAlan Cox 		n = ulmin(VM_LEVEL_0_NPAGES - index, npages);
794c68c3537SAlan Cox 		for (i = 0; i < n; i++)
795ec179322SAlan Cox 			vm_reserv_populate(rv, index + i);
796c68c3537SAlan Cox 		npages -= n;
797c68c3537SAlan Cox 		if (m_ret == NULL) {
798c68c3537SAlan Cox 			m_ret = &rv->pages[index];
799c68c3537SAlan Cox 			index = 0;
800c68c3537SAlan Cox 		}
8015c930c89SJeff Roberson 		vm_reserv_unlock(rv);
802c68c3537SAlan Cox 		m += VM_LEVEL_0_NPAGES;
803c68c3537SAlan Cox 		first += VM_LEVEL_0_NPAGES;
804c68c3537SAlan Cox 		allocpages -= VM_LEVEL_0_NPAGES;
80564f096eeSAlan Cox 	} while (allocpages >= VM_LEVEL_0_NPAGES);
806c68c3537SAlan Cox 	return (m_ret);
807e2068d0bSJeff Roberson }
808c68c3537SAlan Cox 
809c68c3537SAlan Cox /*
8102d5039dbSAlan Cox  * Allocate a physical page from an existing or newly created reservation.
811e2068d0bSJeff Roberson  *
812e2068d0bSJeff Roberson  * The page "mpred" must immediately precede the offset "pindex" within the
813e2068d0bSJeff Roberson  * specified object.
814e2068d0bSJeff Roberson  *
815e2068d0bSJeff Roberson  * The object must be locked.
816c68c3537SAlan Cox  */
817e2068d0bSJeff Roberson vm_page_t
8182d5039dbSAlan Cox vm_reserv_alloc_page(vm_object_t object, vm_pindex_t pindex, int domain,
8192d5039dbSAlan Cox     int req, vm_page_t mpred)
820e2068d0bSJeff Roberson {
821e2068d0bSJeff Roberson 	struct vm_domain *vmd;
822e2068d0bSJeff Roberson 	vm_page_t m, msucc;
8232d5039dbSAlan Cox 	vm_pindex_t first, leftcap, rightcap;
824e2068d0bSJeff Roberson 	vm_reserv_t rv;
82530fbfddaSJeff Roberson 	int index;
826e2068d0bSJeff Roberson 
827e2068d0bSJeff Roberson 	VM_OBJECT_ASSERT_WLOCKED(object);
828e2068d0bSJeff Roberson 
829e2068d0bSJeff Roberson 	/*
8302d5039dbSAlan Cox 	 * Is a reservation fundamentally impossible?
831e2068d0bSJeff Roberson 	 */
832e2068d0bSJeff Roberson 	if (pindex < VM_RESERV_INDEX(object, pindex) ||
8332d5039dbSAlan Cox 	    pindex >= object->size)
834e2068d0bSJeff Roberson 		return (NULL);
835e2068d0bSJeff Roberson 
836e2068d0bSJeff Roberson 	/*
837e2068d0bSJeff Roberson 	 * Look for an existing reservation.
838e2068d0bSJeff Roberson 	 */
839e2068d0bSJeff Roberson 	rv = vm_reserv_from_object(object, pindex, mpred, &msucc);
8402d5039dbSAlan Cox 	if (rv != NULL) {
841e2068d0bSJeff Roberson 		KASSERT(object != kernel_object || rv->domain == domain,
8422d5039dbSAlan Cox 		    ("vm_reserv_alloc_page: domain mismatch"));
843e2068d0bSJeff Roberson 		domain = rv->domain;
844e2068d0bSJeff Roberson 		vmd = VM_DOMAIN(domain);
845c68c3537SAlan Cox 		index = VM_RESERV_INDEX(object, pindex);
846c68c3537SAlan Cox 		m = &rv->pages[index];
8475c930c89SJeff Roberson 		vm_reserv_lock(rv);
848e2068d0bSJeff Roberson 		/* Handle reclaim race. */
8495c930c89SJeff Roberson 		if (rv->object != object ||
850c296ac7eSAlan Cox 		    /* Handle vm_page_iter_rename(..., m, new_object, ...). */
85184e2ae64SDoug Moore 		    bit_test(rv->popmap, index)) {
852e2068d0bSJeff Roberson 			m = NULL;
8535c930c89SJeff Roberson 			goto out;
85430fbfddaSJeff Roberson 		}
8555c930c89SJeff Roberson 		if (vm_domain_allocate(vmd, req, 1) == 0)
8565c930c89SJeff Roberson 			m = NULL;
8575c930c89SJeff Roberson 		else
8585c930c89SJeff Roberson 			vm_reserv_populate(rv, index);
8595c930c89SJeff Roberson out:
8605c930c89SJeff Roberson 		vm_reserv_unlock(rv);
861c68c3537SAlan Cox 		return (m);
862c68c3537SAlan Cox 	}
863c68c3537SAlan Cox 
864c68c3537SAlan Cox 	/*
865c68c3537SAlan Cox 	 * Could a reservation fit between the first index to the left that
866c68c3537SAlan Cox 	 * can be used and the first index to the right that cannot be used?
867e2068d0bSJeff Roberson 	 *
868e2068d0bSJeff Roberson 	 * We must synchronize with the reserv object lock to protect the
869e2068d0bSJeff Roberson 	 * pindex/object of the resulting reservations against rename while
870e2068d0bSJeff Roberson 	 * we are inspecting.
871f8a47341SAlan Cox 	 */
872c68c3537SAlan Cox 	first = pindex - VM_RESERV_INDEX(object, pindex);
873e2068d0bSJeff Roberson 	vm_reserv_object_lock(object);
874c68c3537SAlan Cox 	if (mpred != NULL) {
875c68c3537SAlan Cox 		if ((rv = vm_reserv_from_page(mpred))->object != object)
876f8a47341SAlan Cox 			leftcap = mpred->pindex + 1;
877f8a47341SAlan Cox 		else
878f8a47341SAlan Cox 			leftcap = rv->pindex + VM_LEVEL_0_NPAGES;
879e2068d0bSJeff Roberson 		if (leftcap > first) {
880e2068d0bSJeff Roberson 			vm_reserv_object_unlock(object);
881c68c3537SAlan Cox 			return (NULL);
882c68c3537SAlan Cox 		}
883e2068d0bSJeff Roberson 	}
884c68c3537SAlan Cox 	if (msucc != NULL) {
885c68c3537SAlan Cox 		if ((rv = vm_reserv_from_page(msucc))->object != object)
886f8a47341SAlan Cox 			rightcap = msucc->pindex;
887f8a47341SAlan Cox 		else
888f8a47341SAlan Cox 			rightcap = rv->pindex;
889e2068d0bSJeff Roberson 		if (first + VM_LEVEL_0_NPAGES > rightcap) {
890e2068d0bSJeff Roberson 			vm_reserv_object_unlock(object);
891f8a47341SAlan Cox 			return (NULL);
892c68c3537SAlan Cox 		}
893e2068d0bSJeff Roberson 	}
894e2068d0bSJeff Roberson 	vm_reserv_object_unlock(object);
895f8a47341SAlan Cox 
896f8a47341SAlan Cox 	/*
89763967687SJeff Roberson 	 * Would the last new reservation extend past the end of the object?
89863967687SJeff Roberson 	 *
89963967687SJeff Roberson 	 * If the object is unlikely to grow don't allocate a reservation for
90063967687SJeff Roberson 	 * the tail.
901f8a47341SAlan Cox 	 */
90263967687SJeff Roberson 	if ((object->flags & OBJ_ANON) == 0 &&
90363967687SJeff Roberson 	    first + VM_LEVEL_0_NPAGES > object->size)
904f8a47341SAlan Cox 		return (NULL);
905f8a47341SAlan Cox 
906f8a47341SAlan Cox 	/*
907c68c3537SAlan Cox 	 * Allocate and populate the new reservation.
908f8a47341SAlan Cox 	 */
9095c930c89SJeff Roberson 	m = NULL;
9105c930c89SJeff Roberson 	vmd = VM_DOMAIN(domain);
9115c930c89SJeff Roberson 	if (vm_domain_allocate(vmd, req, 1)) {
9125c930c89SJeff Roberson 		vm_domain_free_lock(vmd);
9135c930c89SJeff Roberson 		m = vm_phys_alloc_pages(domain, VM_FREEPOOL_DEFAULT,
9145c930c89SJeff Roberson 		    VM_LEVEL_0_ORDER);
9155c930c89SJeff Roberson 		vm_domain_free_unlock(vmd);
9165c930c89SJeff Roberson 		if (m == NULL) {
9175c930c89SJeff Roberson 			vm_domain_freecnt_inc(vmd, 1);
9185c930c89SJeff Roberson 			return (NULL);
9195c930c89SJeff Roberson 		}
9205c930c89SJeff Roberson 	} else
921c68c3537SAlan Cox 		return (NULL);
922f8a47341SAlan Cox 	rv = vm_reserv_from_page(m);
9235c930c89SJeff Roberson 	vm_reserv_lock(rv);
924f8a47341SAlan Cox 	KASSERT(rv->pages == m,
925c68c3537SAlan Cox 	    ("vm_reserv_alloc_page: reserv %p's pages is corrupted", rv));
926e2068d0bSJeff Roberson 	vm_reserv_insert(rv, object, first);
927ec179322SAlan Cox 	index = VM_RESERV_INDEX(object, pindex);
928ec179322SAlan Cox 	vm_reserv_populate(rv, index);
9295c930c89SJeff Roberson 	vm_reserv_unlock(rv);
9305c930c89SJeff Roberson 
931ec179322SAlan Cox 	return (&rv->pages[index]);
932f8a47341SAlan Cox }
933f8a47341SAlan Cox 
934f8a47341SAlan Cox /*
935ada27a3bSKonstantin Belousov  * Breaks the given reservation.  All free pages in the reservation
936ada27a3bSKonstantin Belousov  * are returned to the physical memory allocator.  The reservation's
937ada27a3bSKonstantin Belousov  * population count and map are reset to their initial state.
938ec179322SAlan Cox  *
9393453bca8SAlan Cox  * The given reservation must not be in the partially populated reservation
940fe6d5344SMark Johnston  * queue.
941ec179322SAlan Cox  */
942ec179322SAlan Cox static void
943ada27a3bSKonstantin Belousov vm_reserv_break(vm_reserv_t rv)
944ec179322SAlan Cox {
9453e00c11aSAlan Cox 	vm_page_t m;
946*0078df5fSDoug Moore 	int pos, pos0, pos1;
947ec179322SAlan Cox 
9485c930c89SJeff Roberson 	vm_reserv_assert_locked(rv);
9495c930c89SJeff Roberson 	CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d",
9505c930c89SJeff Roberson 	    __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq);
951e2068d0bSJeff Roberson 	vm_reserv_remove(rv);
9523e00c11aSAlan Cox 	m = rv->pages;
9533e00c11aSAlan Cox #ifdef VM_SUBLEVEL_0_NPAGES
9543e00c11aSAlan Cox 	for (; m < rv->pages + VM_LEVEL_0_NPAGES; m += VM_SUBLEVEL_0_NPAGES)
9553e00c11aSAlan Cox #endif
9563e00c11aSAlan Cox 		m->psind = 0;
957*0078df5fSDoug Moore 	pos0 = bit_test(rv->popmap, 0) ? -1 : 0;
958*0078df5fSDoug Moore 	pos1 = -1 - pos0;
959*0078df5fSDoug Moore 	for (pos = 0; pos < VM_LEVEL_0_NPAGES; ) {
960*0078df5fSDoug Moore 		/* Find the first different bit after pos. */
961*0078df5fSDoug Moore 		bit_ff_at(rv->popmap, pos + 1, VM_LEVEL_0_NPAGES,
962*0078df5fSDoug Moore 		    pos1 < pos0, &pos);
96318c47eabSDoug Moore 		if (pos == -1)
96418c47eabSDoug Moore 			pos = VM_LEVEL_0_NPAGES;
965*0078df5fSDoug Moore 		if (pos0 < pos1) {
966*0078df5fSDoug Moore 			pos0 = pos;
967*0078df5fSDoug Moore 			continue;
968*0078df5fSDoug Moore 		}
969*0078df5fSDoug Moore 		/* Free unused pages from pos0 to pos. */
970*0078df5fSDoug Moore 		pos1 = pos;
9715c930c89SJeff Roberson 		vm_domain_free_lock(VM_DOMAIN(rv->domain));
972*0078df5fSDoug Moore 		vm_phys_enqueue_contig(&rv->pages[pos0], VM_FREEPOOL_DEFAULT,
973*0078df5fSDoug Moore 		    pos1 - pos0);
9745c930c89SJeff Roberson 		vm_domain_free_unlock(VM_DOMAIN(rv->domain));
975e67a5068SDoug Moore 	}
97684e2ae64SDoug Moore 	bit_nclear(rv->popmap, 0, VM_LEVEL_0_NPAGES - 1);
977e67a5068SDoug Moore 	rv->popcnt = 0;
9785c930c89SJeff Roberson 	counter_u64_add(vm_reserv_broken, 1);
979ec179322SAlan Cox }
980ec179322SAlan Cox 
981ec179322SAlan Cox /*
982f8a47341SAlan Cox  * Breaks all reservations belonging to the given object.
983f8a47341SAlan Cox  */
984f8a47341SAlan Cox void
985f8a47341SAlan Cox vm_reserv_break_all(vm_object_t object)
986f8a47341SAlan Cox {
987f8a47341SAlan Cox 	vm_reserv_t rv;
988f8a47341SAlan Cox 
989e2068d0bSJeff Roberson 	/*
990e2068d0bSJeff Roberson 	 * This access of object->rvq is unsynchronized so that the
991e2068d0bSJeff Roberson 	 * object rvq lock can nest after the domain_free lock.  We
992e2068d0bSJeff Roberson 	 * must check for races in the results.  However, the object
993e2068d0bSJeff Roberson 	 * lock prevents new additions, so we are guaranteed that when
994e2068d0bSJeff Roberson 	 * it returns NULL the object is properly empty.
995e2068d0bSJeff Roberson 	 */
996f8a47341SAlan Cox 	while ((rv = LIST_FIRST(&object->rvq)) != NULL) {
9975c930c89SJeff Roberson 		vm_reserv_lock(rv);
998e2068d0bSJeff Roberson 		/* Reclaim race. */
9995c930c89SJeff Roberson 		if (rv->object != object) {
10005c930c89SJeff Roberson 			vm_reserv_unlock(rv);
1001e2068d0bSJeff Roberson 			continue;
10025c930c89SJeff Roberson 		}
10035c930c89SJeff Roberson 		vm_reserv_domain_lock(rv->domain);
1004f8a47341SAlan Cox 		if (rv->inpartpopq) {
1005fe6d5344SMark Johnston 			TAILQ_REMOVE(&vm_rvd[rv->domain].partpop, rv, partpopq);
1006f8a47341SAlan Cox 			rv->inpartpopq = FALSE;
1007f8a47341SAlan Cox 		}
10085c930c89SJeff Roberson 		vm_reserv_domain_unlock(rv->domain);
1009ada27a3bSKonstantin Belousov 		vm_reserv_break(rv);
10105c930c89SJeff Roberson 		vm_reserv_unlock(rv);
1011f8a47341SAlan Cox 	}
1012f8a47341SAlan Cox }
1013f8a47341SAlan Cox 
1014f8a47341SAlan Cox /*
1015f8a47341SAlan Cox  * Frees the given page if it belongs to a reservation.  Returns TRUE if the
1016f8a47341SAlan Cox  * page is freed and FALSE otherwise.
1017f8a47341SAlan Cox  */
1018f8a47341SAlan Cox boolean_t
1019f8a47341SAlan Cox vm_reserv_free_page(vm_page_t m)
1020f8a47341SAlan Cox {
1021f8a47341SAlan Cox 	vm_reserv_t rv;
10225c930c89SJeff Roberson 	boolean_t ret;
1023f8a47341SAlan Cox 
1024f8a47341SAlan Cox 	rv = vm_reserv_from_page(m);
1025908e3da1SAlan Cox 	if (rv->object == NULL)
1026908e3da1SAlan Cox 		return (FALSE);
10275c930c89SJeff Roberson 	vm_reserv_lock(rv);
10285c930c89SJeff Roberson 	/* Re-validate after lock. */
10295c930c89SJeff Roberson 	if (rv->object != NULL) {
1030ec179322SAlan Cox 		vm_reserv_depopulate(rv, m - rv->pages);
10315c930c89SJeff Roberson 		ret = TRUE;
10325c930c89SJeff Roberson 	} else
10335c930c89SJeff Roberson 		ret = FALSE;
10345c930c89SJeff Roberson 	vm_reserv_unlock(rv);
10355c930c89SJeff Roberson 
10365c930c89SJeff Roberson 	return (ret);
1037f8a47341SAlan Cox }
1038f8a47341SAlan Cox 
1039f8a47341SAlan Cox /*
1040f8a47341SAlan Cox  * Initializes the reservation management system.  Specifically, initializes
1041f8a47341SAlan Cox  * the reservation array.
1042f8a47341SAlan Cox  *
1043f8a47341SAlan Cox  * Requires that vm_page_array and first_page are initialized!
1044f8a47341SAlan Cox  */
1045f8a47341SAlan Cox void
1046f8a47341SAlan Cox vm_reserv_init(void)
1047f8a47341SAlan Cox {
1048f8a47341SAlan Cox 	vm_paddr_t paddr;
104909e5f3c4SAlan Cox 	struct vm_phys_seg *seg;
10505c930c89SJeff Roberson 	struct vm_reserv *rv;
1051b378d296SMark Johnston 	struct vm_reserv_domain *rvd;
10527988971aSD Scott Phillips #ifdef VM_PHYSSEG_SPARSE
10537988971aSD Scott Phillips 	vm_pindex_t used;
10547988971aSD Scott Phillips #endif
105584e2ae64SDoug Moore 	int i, segind;
1056f8a47341SAlan Cox 
1057f8a47341SAlan Cox 	/*
1058f8a47341SAlan Cox 	 * Initialize the reservation array.  Specifically, initialize the
1059f8a47341SAlan Cox 	 * "pages" field for every element that has an underlying superpage.
1060f8a47341SAlan Cox 	 */
10617988971aSD Scott Phillips #ifdef VM_PHYSSEG_SPARSE
10627988971aSD Scott Phillips 	used = 0;
10637988971aSD Scott Phillips #endif
106409e5f3c4SAlan Cox 	for (segind = 0; segind < vm_phys_nsegs; segind++) {
106509e5f3c4SAlan Cox 		seg = &vm_phys_segs[segind];
10667988971aSD Scott Phillips #ifdef VM_PHYSSEG_SPARSE
10677988971aSD Scott Phillips 		seg->first_reserv = &vm_reserv_array[used];
10687988971aSD Scott Phillips 		used += howmany(seg->end, VM_LEVEL_0_SIZE) -
10697988971aSD Scott Phillips 		    seg->start / VM_LEVEL_0_SIZE;
10707988971aSD Scott Phillips #else
10717988971aSD Scott Phillips 		seg->first_reserv =
10727988971aSD Scott Phillips 		    &vm_reserv_array[seg->start >> VM_LEVEL_0_SHIFT];
10737988971aSD Scott Phillips #endif
107409e5f3c4SAlan Cox 		paddr = roundup2(seg->start, VM_LEVEL_0_SIZE);
10757988971aSD Scott Phillips 		rv = seg->first_reserv + (paddr >> VM_LEVEL_0_SHIFT) -
10767988971aSD Scott Phillips 		    (seg->start >> VM_LEVEL_0_SHIFT);
10776b821a74SAleksandr Rybalko 		while (paddr + VM_LEVEL_0_SIZE > paddr && paddr +
10786b821a74SAleksandr Rybalko 		    VM_LEVEL_0_SIZE <= seg->end) {
10795c930c89SJeff Roberson 			rv->pages = PHYS_TO_VM_PAGE(paddr);
10805c930c89SJeff Roberson 			rv->domain = seg->domain;
10815c930c89SJeff Roberson 			mtx_init(&rv->lock, "vm reserv", NULL, MTX_DEF);
1082f8a47341SAlan Cox 			paddr += VM_LEVEL_0_SIZE;
10837988971aSD Scott Phillips 			rv++;
1084f8a47341SAlan Cox 		}
1085f8a47341SAlan Cox 	}
10865c930c89SJeff Roberson 	for (i = 0; i < MAXMEMDOM; i++) {
1087b378d296SMark Johnston 		rvd = &vm_rvd[i];
1088b378d296SMark Johnston 		mtx_init(&rvd->lock, "vm reserv domain", NULL, MTX_DEF);
1089b378d296SMark Johnston 		TAILQ_INIT(&rvd->partpop);
1090b378d296SMark Johnston 		mtx_init(&rvd->marker.lock, "vm reserv marker", NULL, MTX_DEF);
1091b378d296SMark Johnston 
1092b378d296SMark Johnston 		/*
1093b378d296SMark Johnston 		 * Fully populated reservations should never be present in the
1094b378d296SMark Johnston 		 * partially populated reservation queues.
1095b378d296SMark Johnston 		 */
1096b378d296SMark Johnston 		rvd->marker.popcnt = VM_LEVEL_0_NPAGES;
109784e2ae64SDoug Moore 		bit_nset(rvd->marker.popmap, 0, VM_LEVEL_0_NPAGES - 1);
1098f8a47341SAlan Cox 	}
1099f8a47341SAlan Cox 
11005c930c89SJeff Roberson 	for (i = 0; i < VM_RESERV_OBJ_LOCK_COUNT; i++)
11015c930c89SJeff Roberson 		mtx_init(&vm_reserv_object_mtx[i], "resv obj lock", NULL,
11025c930c89SJeff Roberson 		    MTX_DEF);
11035c930c89SJeff Roberson }
11045c930c89SJeff Roberson 
1105f8a47341SAlan Cox /*
1106c869e672SAlan Cox  * Returns true if the given page belongs to a reservation and that page is
1107c869e672SAlan Cox  * free.  Otherwise, returns false.
1108c869e672SAlan Cox  */
1109c869e672SAlan Cox bool
1110c869e672SAlan Cox vm_reserv_is_page_free(vm_page_t m)
1111c869e672SAlan Cox {
1112c869e672SAlan Cox 	vm_reserv_t rv;
1113c869e672SAlan Cox 
1114c869e672SAlan Cox 	rv = vm_reserv_from_page(m);
1115c869e672SAlan Cox 	if (rv->object == NULL)
1116c869e672SAlan Cox 		return (false);
111784e2ae64SDoug Moore 	return (!bit_test(rv->popmap, m - rv->pages));
1118c869e672SAlan Cox }
1119c869e672SAlan Cox 
1120c869e672SAlan Cox /*
11211526667bSDoug Moore  * Returns true if the given page is part of a block of npages, starting at a
11221526667bSDoug Moore  * multiple of npages, that are all allocated.  Otherwise, returns false.
11231526667bSDoug Moore  */
11241526667bSDoug Moore bool
11251526667bSDoug Moore vm_reserv_is_populated(vm_page_t m, int npages)
11261526667bSDoug Moore {
11271526667bSDoug Moore 	vm_reserv_t rv;
11281526667bSDoug Moore 	int index;
11291526667bSDoug Moore 
11301526667bSDoug Moore 	KASSERT(npages <= VM_LEVEL_0_NPAGES,
11311526667bSDoug Moore 	    ("%s: npages %d exceeds VM_LEVEL_0_NPAGES", __func__, npages));
11321526667bSDoug Moore 	KASSERT(powerof2(npages),
11331526667bSDoug Moore 	    ("%s: npages %d is not a power of 2", __func__, npages));
11341526667bSDoug Moore 	rv = vm_reserv_from_page(m);
11351526667bSDoug Moore 	if (rv->object == NULL)
11361526667bSDoug Moore 		return (false);
11371526667bSDoug Moore 	index = rounddown2(m - rv->pages, npages);
11381526667bSDoug Moore 	return (bit_ntest(rv->popmap, index, index + npages - 1, 1));
11391526667bSDoug Moore }
11401526667bSDoug Moore 
11411526667bSDoug Moore /*
1142c869e672SAlan Cox  * If the given page belongs to a reservation, returns the level of that
1143c869e672SAlan Cox  * reservation.  Otherwise, returns -1.
1144c869e672SAlan Cox  */
1145c869e672SAlan Cox int
1146c869e672SAlan Cox vm_reserv_level(vm_page_t m)
1147c869e672SAlan Cox {
1148c869e672SAlan Cox 	vm_reserv_t rv;
1149c869e672SAlan Cox 
1150c869e672SAlan Cox 	rv = vm_reserv_from_page(m);
11513e00c11aSAlan Cox #ifdef VM_SUBLEVEL_0_NPAGES
11523e00c11aSAlan Cox 	return (rv->object != NULL ? 1 : -1);
11533e00c11aSAlan Cox #else
1154c869e672SAlan Cox 	return (rv->object != NULL ? 0 : -1);
11553e00c11aSAlan Cox #endif
1156c869e672SAlan Cox }
1157c869e672SAlan Cox 
1158c869e672SAlan Cox /*
11593453bca8SAlan Cox  * Returns a reservation level if the given page belongs to a fully populated
1160f8a47341SAlan Cox  * reservation and -1 otherwise.
1161f8a47341SAlan Cox  */
1162f8a47341SAlan Cox int
1163f8a47341SAlan Cox vm_reserv_level_iffullpop(vm_page_t m)
1164f8a47341SAlan Cox {
1165f8a47341SAlan Cox 	vm_reserv_t rv;
1166f8a47341SAlan Cox 
1167f8a47341SAlan Cox 	rv = vm_reserv_from_page(m);
11683e00c11aSAlan Cox 	if (rv->popcnt == VM_LEVEL_0_NPAGES) {
11693e00c11aSAlan Cox #ifdef VM_SUBLEVEL_0_NPAGES
11703e00c11aSAlan Cox 		return (1);
11713e00c11aSAlan Cox 	} else if (rv->pages != NULL &&
11723e00c11aSAlan Cox 	    vm_reserv_is_sublevel_full(rv, m - rv->pages)) {
11733e00c11aSAlan Cox #endif
11743e00c11aSAlan Cox 		return (0);
11753e00c11aSAlan Cox 	}
11763e00c11aSAlan Cox 	return (-1);
1177f8a47341SAlan Cox }
1178f8a47341SAlan Cox 
1179f8a47341SAlan Cox /*
1180b378d296SMark Johnston  * Remove a partially populated reservation from the queue.
1181b378d296SMark Johnston  */
1182b378d296SMark Johnston static void
1183b378d296SMark Johnston vm_reserv_dequeue(vm_reserv_t rv)
1184b378d296SMark Johnston {
1185b378d296SMark Johnston 
1186b378d296SMark Johnston 	vm_reserv_domain_assert_locked(rv->domain);
1187b378d296SMark Johnston 	vm_reserv_assert_locked(rv);
1188b378d296SMark Johnston 	CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d",
1189b378d296SMark Johnston 	    __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq);
1190b378d296SMark Johnston 	KASSERT(rv->inpartpopq,
1191b378d296SMark Johnston 	    ("vm_reserv_reclaim: reserv %p's inpartpopq is FALSE", rv));
1192b378d296SMark Johnston 
1193b378d296SMark Johnston 	TAILQ_REMOVE(&vm_rvd[rv->domain].partpop, rv, partpopq);
1194b378d296SMark Johnston 	rv->inpartpopq = FALSE;
1195b378d296SMark Johnston }
1196b378d296SMark Johnston 
1197b378d296SMark Johnston /*
11983453bca8SAlan Cox  * Breaks the given partially populated reservation, releasing its free pages
11993453bca8SAlan Cox  * to the physical memory allocator.
1200f8a47341SAlan Cox  */
120144aab2c3SAlan Cox static void
120244aab2c3SAlan Cox vm_reserv_reclaim(vm_reserv_t rv)
1203f8a47341SAlan Cox {
1204f8a47341SAlan Cox 
12055c930c89SJeff Roberson 	vm_reserv_assert_locked(rv);
12065c930c89SJeff Roberson 	CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d",
12075c930c89SJeff Roberson 	    __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq);
1208b378d296SMark Johnston 	if (rv->inpartpopq) {
12095c930c89SJeff Roberson 		vm_reserv_domain_lock(rv->domain);
1210b378d296SMark Johnston 		vm_reserv_dequeue(rv);
12115c930c89SJeff Roberson 		vm_reserv_domain_unlock(rv->domain);
1212b378d296SMark Johnston 	}
1213ada27a3bSKonstantin Belousov 	vm_reserv_break(rv);
12145c930c89SJeff Roberson 	counter_u64_add(vm_reserv_reclaimed, 1);
121544aab2c3SAlan Cox }
121644aab2c3SAlan Cox 
121744aab2c3SAlan Cox /*
1218b378d296SMark Johnston  * Breaks a reservation near the head of the partially populated reservation
12193453bca8SAlan Cox  * queue, releasing its free pages to the physical memory allocator.  Returns
12203453bca8SAlan Cox  * TRUE if a reservation is broken and FALSE otherwise.
122144aab2c3SAlan Cox  */
1222b378d296SMark Johnston bool
1223ef435ae7SJeff Roberson vm_reserv_reclaim_inactive(int domain)
122444aab2c3SAlan Cox {
122544aab2c3SAlan Cox 	vm_reserv_t rv;
122644aab2c3SAlan Cox 
1227b378d296SMark Johnston 	vm_reserv_domain_lock(domain);
1228b378d296SMark Johnston 	TAILQ_FOREACH(rv, &vm_rvd[domain].partpop, partpopq) {
1229b378d296SMark Johnston 		/*
1230b378d296SMark Johnston 		 * A locked reservation is likely being updated or reclaimed,
1231b378d296SMark Johnston 		 * so just skip ahead.
1232b378d296SMark Johnston 		 */
1233b378d296SMark Johnston 		if (rv != &vm_rvd[domain].marker && vm_reserv_trylock(rv)) {
1234b378d296SMark Johnston 			vm_reserv_dequeue(rv);
1235b378d296SMark Johnston 			break;
12365c930c89SJeff Roberson 		}
1237b378d296SMark Johnston 	}
1238b378d296SMark Johnston 	vm_reserv_domain_unlock(domain);
1239b378d296SMark Johnston 	if (rv != NULL) {
124044aab2c3SAlan Cox 		vm_reserv_reclaim(rv);
12415c930c89SJeff Roberson 		vm_reserv_unlock(rv);
1242b378d296SMark Johnston 		return (true);
1243f8a47341SAlan Cox 	}
1244b378d296SMark Johnston 	return (false);
1245f8a47341SAlan Cox }
1246f8a47341SAlan Cox 
1247f8a47341SAlan Cox /*
1248f96e8a0bSDoug Moore  * Determine whether this reservation has free pages that satisfy the given
1249f96e8a0bSDoug Moore  * request for contiguous physical memory.  Start searching from the lower
12506f1c8908SDoug Moore  * bound, defined by lo, and stop at the upper bound, hi.  Return the index
12516f1c8908SDoug Moore  * of the first satisfactory free page, or -1 if none is found.
1252f96e8a0bSDoug Moore  */
12536f1c8908SDoug Moore static int
12546f1c8908SDoug Moore vm_reserv_find_contig(vm_reserv_t rv, int npages, int lo,
12556f1c8908SDoug Moore     int hi, int ppn_align, int ppn_bound)
1256f96e8a0bSDoug Moore {
1257f96e8a0bSDoug Moore 
1258f96e8a0bSDoug Moore 	vm_reserv_assert_locked(rv);
12596f1c8908SDoug Moore 	KASSERT(npages <= VM_LEVEL_0_NPAGES - 1,
12606f1c8908SDoug Moore 	    ("%s: Too many pages", __func__));
12616f1c8908SDoug Moore 	KASSERT(ppn_bound <= VM_LEVEL_0_NPAGES,
12626f1c8908SDoug Moore 	    ("%s: Too big a boundary for reservation size", __func__));
12636f1c8908SDoug Moore 	KASSERT(npages <= ppn_bound,
12646f1c8908SDoug Moore 	    ("%s: Too many pages for given boundary", __func__));
12656f1c8908SDoug Moore 	KASSERT(ppn_align != 0 && powerof2(ppn_align),
12666f1c8908SDoug Moore 	    ("ppn_align is not a positive power of 2"));
12676f1c8908SDoug Moore 	KASSERT(ppn_bound != 0 && powerof2(ppn_bound),
12686f1c8908SDoug Moore 	    ("ppn_bound is not a positive power of 2"));
126984e2ae64SDoug Moore 	while (bit_ffc_area_at(rv->popmap, lo, hi, npages, &lo), lo != -1) {
12706f1c8908SDoug Moore 		if (lo < roundup2(lo, ppn_align)) {
1271f96e8a0bSDoug Moore 			/* Skip to next aligned page. */
12726f1c8908SDoug Moore 			lo = roundup2(lo, ppn_align);
127384e2ae64SDoug Moore 		} else if (roundup2(lo + 1, ppn_bound) >= lo + npages)
12746f1c8908SDoug Moore 			return (lo);
127584e2ae64SDoug Moore 		if (roundup2(lo + 1, ppn_bound) < lo + npages) {
127684e2ae64SDoug Moore 			/* Skip to next boundary-matching page. */
127784e2ae64SDoug Moore 			lo = roundup2(lo + 1, ppn_bound);
1278f96e8a0bSDoug Moore 		}
127984e2ae64SDoug Moore 	}
12806f1c8908SDoug Moore 	return (-1);
1281f96e8a0bSDoug Moore }
1282f96e8a0bSDoug Moore 
1283f96e8a0bSDoug Moore /*
12843453bca8SAlan Cox  * Searches the partially populated reservation queue for the least recently
12853453bca8SAlan Cox  * changed reservation with free pages that satisfy the given request for
12863453bca8SAlan Cox  * contiguous physical memory.  If a satisfactory reservation is found, it is
1287989a2cf1SMinsoo Choo  * broken.  Returns a page if a reservation is broken and NULL otherwise.
128844aab2c3SAlan Cox  */
12890d5fac28SDoug Moore vm_page_t
1290ef435ae7SJeff Roberson vm_reserv_reclaim_contig(int domain, u_long npages, vm_paddr_t low,
1291ef435ae7SJeff Roberson     vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
129244aab2c3SAlan Cox {
1293b378d296SMark Johnston 	struct vm_reserv_queue *queue;
1294ec179322SAlan Cox 	vm_paddr_t pa, size;
12950d5fac28SDoug Moore 	vm_page_t m_ret;
1296b378d296SMark Johnston 	vm_reserv_t marker, rv, rvn;
12976f1c8908SDoug Moore 	int hi, lo, posn, ppn_align, ppn_bound;
129844aab2c3SAlan Cox 
12996f1c8908SDoug Moore 	KASSERT(npages > 0, ("npages is 0"));
13006f1c8908SDoug Moore 	KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
13016f1c8908SDoug Moore 	KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
1302c68c3537SAlan Cox 	if (npages > VM_LEVEL_0_NPAGES - 1)
1303989a2cf1SMinsoo Choo 		return (NULL);
13046f1c8908SDoug Moore 	size = npages << PAGE_SHIFT;
13056f1c8908SDoug Moore 	/*
13066f1c8908SDoug Moore 	 * Ensure that a free range starting at a boundary-multiple
13076f1c8908SDoug Moore 	 * doesn't include a boundary-multiple within it.  Otherwise,
13086f1c8908SDoug Moore 	 * no boundary-constrained allocation is possible.
13096f1c8908SDoug Moore 	 */
1310c606ab59SDoug Moore 	if (!vm_addr_bound_ok(0, size, boundary))
13110d5fac28SDoug Moore 		return (NULL);
1312b378d296SMark Johnston 	marker = &vm_rvd[domain].marker;
1313b378d296SMark Johnston 	queue = &vm_rvd[domain].partpop;
13146f1c8908SDoug Moore 	/*
13156f1c8908SDoug Moore 	 * Compute shifted alignment, boundary values for page-based
13166f1c8908SDoug Moore 	 * calculations.  Constrain to range [1, VM_LEVEL_0_NPAGES] to
13176f1c8908SDoug Moore 	 * avoid overflow.
13186f1c8908SDoug Moore 	 */
13196f1c8908SDoug Moore 	ppn_align = (int)(ulmin(ulmax(PAGE_SIZE, alignment),
13206f1c8908SDoug Moore 	    VM_LEVEL_0_SIZE) >> PAGE_SHIFT);
132149fd2d51SDoug Moore 	ppn_bound = boundary == 0 ? VM_LEVEL_0_NPAGES :
132249fd2d51SDoug Moore 	    (int)(MIN(MAX(PAGE_SIZE, boundary),
13236f1c8908SDoug Moore             VM_LEVEL_0_SIZE) >> PAGE_SHIFT);
1324b378d296SMark Johnston 
1325b378d296SMark Johnston 	vm_reserv_domain_scan_lock(domain);
13265c930c89SJeff Roberson 	vm_reserv_domain_lock(domain);
1327b378d296SMark Johnston 	TAILQ_FOREACH_SAFE(rv, queue, partpopq, rvn) {
1328f96e8a0bSDoug Moore 		pa = VM_PAGE_TO_PHYS(&rv->pages[0]);
1329f96e8a0bSDoug Moore 		if (pa + VM_LEVEL_0_SIZE - size < low) {
1330ec179322SAlan Cox 			/* This entire reservation is too low; go to next. */
133144aab2c3SAlan Cox 			continue;
133244aab2c3SAlan Cox 		}
133344aab2c3SAlan Cox 		if (pa + size > high) {
1334ec179322SAlan Cox 			/* This entire reservation is too high; go to next. */
1335ec179322SAlan Cox 			continue;
133685f2a0c9SMax Laier 		}
1337c606ab59SDoug Moore 		if (!vm_addr_align_ok(pa, alignment)) {
13386f1c8908SDoug Moore 			/* This entire reservation is unaligned; go to next. */
13396f1c8908SDoug Moore 			continue;
13406f1c8908SDoug Moore 		}
1341b378d296SMark Johnston 
13425c930c89SJeff Roberson 		if (vm_reserv_trylock(rv) == 0) {
1343b378d296SMark Johnston 			TAILQ_INSERT_AFTER(queue, rv, marker, partpopq);
13445c930c89SJeff Roberson 			vm_reserv_domain_unlock(domain);
13455c930c89SJeff Roberson 			vm_reserv_lock(rv);
1346968079f2SMark Johnston 			if (TAILQ_PREV(marker, vm_reserv_queue, partpopq) !=
1347968079f2SMark Johnston 			    rv) {
1348b378d296SMark Johnston 				vm_reserv_unlock(rv);
13495c930c89SJeff Roberson 				vm_reserv_domain_lock(domain);
1350b378d296SMark Johnston 				rvn = TAILQ_NEXT(marker, partpopq);
1351b378d296SMark Johnston 				TAILQ_REMOVE(queue, marker, partpopq);
13525c930c89SJeff Roberson 				continue;
13535c930c89SJeff Roberson 			}
1354b378d296SMark Johnston 			vm_reserv_domain_lock(domain);
1355b378d296SMark Johnston 			TAILQ_REMOVE(queue, marker, partpopq);
1356b378d296SMark Johnston 		}
13575c930c89SJeff Roberson 		vm_reserv_domain_unlock(domain);
13586f1c8908SDoug Moore 		lo = (pa >= low) ? 0 :
13596f1c8908SDoug Moore 		    (int)((low + PAGE_MASK - pa) >> PAGE_SHIFT);
13606f1c8908SDoug Moore 		hi = (pa + VM_LEVEL_0_SIZE <= high) ? VM_LEVEL_0_NPAGES :
13616f1c8908SDoug Moore 		    (int)((high - pa) >> PAGE_SHIFT);
13626f1c8908SDoug Moore 		posn = vm_reserv_find_contig(rv, (int)npages, lo, hi,
13636f1c8908SDoug Moore 		    ppn_align, ppn_bound);
13646f1c8908SDoug Moore 		if (posn >= 0) {
13650d5fac28SDoug Moore 			vm_reserv_domain_scan_unlock(domain);
13660d5fac28SDoug Moore 			/* Allocate requested space */
13670d5fac28SDoug Moore 			rv->popcnt += npages;
136884e2ae64SDoug Moore 			bit_nset(rv->popmap, posn, posn + npages - 1);
13690d5fac28SDoug Moore 			vm_reserv_reclaim(rv);
13700d5fac28SDoug Moore 			vm_reserv_unlock(rv);
13710d5fac28SDoug Moore 			m_ret = &rv->pages[posn];
13720d5fac28SDoug Moore 			pa = VM_PAGE_TO_PHYS(m_ret);
1373c606ab59SDoug Moore 			KASSERT(vm_addr_ok(pa, size, alignment, boundary),
1374c606ab59SDoug Moore 			    ("%s: adjusted address not aligned/bounded to "
1375c606ab59SDoug Moore 			     "%lx/%jx",
1376c606ab59SDoug Moore 			     __func__, alignment, (uintmax_t)boundary));
13770d5fac28SDoug Moore 			return (m_ret);
137844aab2c3SAlan Cox 		}
13795c930c89SJeff Roberson 		vm_reserv_domain_lock(domain);
1380968079f2SMark Johnston 		rvn = TAILQ_NEXT(rv, partpopq);
1381968079f2SMark Johnston 		vm_reserv_unlock(rv);
138244aab2c3SAlan Cox 	}
13835c930c89SJeff Roberson 	vm_reserv_domain_unlock(domain);
1384b378d296SMark Johnston 	vm_reserv_domain_scan_unlock(domain);
13850d5fac28SDoug Moore 	return (NULL);
138644aab2c3SAlan Cox }
138744aab2c3SAlan Cox 
138844aab2c3SAlan Cox /*
1389f8a47341SAlan Cox  * Transfers the reservation underlying the given page to a new object.
1390f8a47341SAlan Cox  *
1391f8a47341SAlan Cox  * The object must be locked.
1392f8a47341SAlan Cox  */
1393f8a47341SAlan Cox void
1394f8a47341SAlan Cox vm_reserv_rename(vm_page_t m, vm_object_t new_object, vm_object_t old_object,
1395f8a47341SAlan Cox     vm_pindex_t old_object_offset)
1396f8a47341SAlan Cox {
1397f8a47341SAlan Cox 	vm_reserv_t rv;
1398f8a47341SAlan Cox 
139989f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(new_object);
1400f8a47341SAlan Cox 	rv = vm_reserv_from_page(m);
1401f8a47341SAlan Cox 	if (rv->object == old_object) {
14025c930c89SJeff Roberson 		vm_reserv_lock(rv);
14035c930c89SJeff Roberson 		CTR6(KTR_VM,
14045c930c89SJeff Roberson 		    "%s: rv %p object %p new %p popcnt %d inpartpop %d",
14055c930c89SJeff Roberson 		    __FUNCTION__, rv, rv->object, new_object, rv->popcnt,
14065c930c89SJeff Roberson 		    rv->inpartpopq);
1407f8a47341SAlan Cox 		if (rv->object == old_object) {
1408e2068d0bSJeff Roberson 			vm_reserv_object_lock(old_object);
1409e2068d0bSJeff Roberson 			rv->object = NULL;
1410f8a47341SAlan Cox 			LIST_REMOVE(rv, objq);
1411e2068d0bSJeff Roberson 			vm_reserv_object_unlock(old_object);
1412e2068d0bSJeff Roberson 			vm_reserv_object_lock(new_object);
1413f8a47341SAlan Cox 			rv->object = new_object;
1414f8a47341SAlan Cox 			rv->pindex -= old_object_offset;
1415e2068d0bSJeff Roberson 			LIST_INSERT_HEAD(&new_object->rvq, rv, objq);
1416e2068d0bSJeff Roberson 			vm_reserv_object_unlock(new_object);
1417f8a47341SAlan Cox 		}
14185c930c89SJeff Roberson 		vm_reserv_unlock(rv);
1419f8a47341SAlan Cox 	}
1420f8a47341SAlan Cox }
1421f8a47341SAlan Cox 
1422f8a47341SAlan Cox /*
1423c869e672SAlan Cox  * Returns the size (in bytes) of a reservation of the specified level.
1424c869e672SAlan Cox  */
1425c869e672SAlan Cox int
1426c869e672SAlan Cox vm_reserv_size(int level)
1427c869e672SAlan Cox {
1428c869e672SAlan Cox 
1429c869e672SAlan Cox 	switch (level) {
1430c869e672SAlan Cox 	case 0:
14313e00c11aSAlan Cox #ifdef VM_SUBLEVEL_0_NPAGES
14323e00c11aSAlan Cox 		return (VM_SUBLEVEL_0_NPAGES * PAGE_SIZE);
14333e00c11aSAlan Cox 	case 1:
14343e00c11aSAlan Cox #endif
1435c869e672SAlan Cox 		return (VM_LEVEL_0_SIZE);
1436c869e672SAlan Cox 	case -1:
1437c869e672SAlan Cox 		return (PAGE_SIZE);
1438c869e672SAlan Cox 	default:
1439c869e672SAlan Cox 		return (0);
1440c869e672SAlan Cox 	}
1441c869e672SAlan Cox }
1442c869e672SAlan Cox 
1443c869e672SAlan Cox /*
1444f8a47341SAlan Cox  * Allocates the virtual and physical memory required by the reservation
1445f8a47341SAlan Cox  * management system's data structures, in particular, the reservation array.
1446f8a47341SAlan Cox  */
1447f8a47341SAlan Cox vm_paddr_t
14483e5e1b51SJeff Roberson vm_reserv_startup(vm_offset_t *vaddr, vm_paddr_t end)
1449f8a47341SAlan Cox {
14507988971aSD Scott Phillips 	vm_paddr_t new_end;
14517988971aSD Scott Phillips 	vm_pindex_t count;
1452f8a47341SAlan Cox 	size_t size;
14533e5e1b51SJeff Roberson 	int i;
14543e5e1b51SJeff Roberson 
14557988971aSD Scott Phillips 	count = 0;
14563e5e1b51SJeff Roberson 	for (i = 0; i < vm_phys_nsegs; i++) {
14577988971aSD Scott Phillips #ifdef VM_PHYSSEG_SPARSE
14587988971aSD Scott Phillips 		count += howmany(vm_phys_segs[i].end, VM_LEVEL_0_SIZE) -
14597988971aSD Scott Phillips 		    vm_phys_segs[i].start / VM_LEVEL_0_SIZE;
14607988971aSD Scott Phillips #else
14617988971aSD Scott Phillips 		count = MAX(count,
14627988971aSD Scott Phillips 		    howmany(vm_phys_segs[i].end, VM_LEVEL_0_SIZE));
14637988971aSD Scott Phillips #endif
14643e5e1b51SJeff Roberson 	}
14653e5e1b51SJeff Roberson 
14667988971aSD Scott Phillips 	for (i = 0; phys_avail[i + 1] != 0; i += 2) {
14677988971aSD Scott Phillips #ifdef VM_PHYSSEG_SPARSE
14687988971aSD Scott Phillips 		count += howmany(phys_avail[i + 1], VM_LEVEL_0_SIZE) -
14697988971aSD Scott Phillips 		    phys_avail[i] / VM_LEVEL_0_SIZE;
14707988971aSD Scott Phillips #else
14717988971aSD Scott Phillips 		count = MAX(count,
14727988971aSD Scott Phillips 		    howmany(phys_avail[i + 1], VM_LEVEL_0_SIZE));
14737988971aSD Scott Phillips #endif
14743e5e1b51SJeff Roberson 	}
1475f8a47341SAlan Cox 
1476f8a47341SAlan Cox 	/*
14777988971aSD Scott Phillips 	 * Calculate the size (in bytes) of the reservation array.  Rounding up
14787988971aSD Scott Phillips 	 * for partial superpages at boundaries, as every small page is mapped
14797988971aSD Scott Phillips 	 * to an element in the reservation array based on its physical address.
14807988971aSD Scott Phillips 	 * Thus, the number of elements in the reservation array can be greater
14817988971aSD Scott Phillips 	 * than the number of superpages.
1482f8a47341SAlan Cox 	 */
14837988971aSD Scott Phillips 	size = count * sizeof(struct vm_reserv);
1484f8a47341SAlan Cox 
1485f8a47341SAlan Cox 	/*
1486f8a47341SAlan Cox 	 * Allocate and map the physical memory for the reservation array.  The
1487f8a47341SAlan Cox 	 * next available virtual address is returned by reference.
1488f8a47341SAlan Cox 	 */
1489f8a47341SAlan Cox 	new_end = end - round_page(size);
1490f8a47341SAlan Cox 	vm_reserv_array = (void *)(uintptr_t)pmap_map(vaddr, new_end, end,
1491f8a47341SAlan Cox 	    VM_PROT_READ | VM_PROT_WRITE);
1492f8a47341SAlan Cox 	bzero(vm_reserv_array, size);
1493f8a47341SAlan Cox 
1494f8a47341SAlan Cox 	/*
1495f8a47341SAlan Cox 	 * Return the next available physical address.
1496f8a47341SAlan Cox 	 */
1497f8a47341SAlan Cox 	return (new_end);
1498f8a47341SAlan Cox }
1499f8a47341SAlan Cox 
15008b5e1472SAlan Cox /*
15018b5e1472SAlan Cox  * Returns the superpage containing the given page.
15028b5e1472SAlan Cox  */
15038b5e1472SAlan Cox vm_page_t
15048b5e1472SAlan Cox vm_reserv_to_superpage(vm_page_t m)
15058b5e1472SAlan Cox {
15068b5e1472SAlan Cox 	vm_reserv_t rv;
15078b5e1472SAlan Cox 
15088b5e1472SAlan Cox 	VM_OBJECT_ASSERT_LOCKED(m->object);
15098b5e1472SAlan Cox 	rv = vm_reserv_from_page(m);
15103e00c11aSAlan Cox 	if (rv->object == m->object) {
15113e00c11aSAlan Cox 		if (rv->popcnt == VM_LEVEL_0_NPAGES)
15123e00c11aSAlan Cox 			return (rv->pages);
15133e00c11aSAlan Cox #ifdef VM_SUBLEVEL_0_NPAGES
15143e00c11aSAlan Cox 		if (vm_reserv_is_sublevel_full(rv, m - rv->pages))
15153e00c11aSAlan Cox 			return (rv->pages + rounddown2(m - rv->pages,
15163e00c11aSAlan Cox 			    VM_SUBLEVEL_0_NPAGES));
15173e00c11aSAlan Cox #endif
15183e00c11aSAlan Cox 	}
15193e00c11aSAlan Cox 	return (NULL);
15208b5e1472SAlan Cox }
15218b5e1472SAlan Cox 
1522f8a47341SAlan Cox #endif	/* VM_NRESERVLEVEL > 0 */
1523