xref: /freebsd/sys/vm/vm_reserv.c (revision 2d3f4181de530febebb51700523d7304db68dcc5)
1f8a47341SAlan Cox /*-
2fe267a55SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
43f8a47341SAlan Cox 
44f8a47341SAlan Cox #include "opt_vm.h"
45f8a47341SAlan Cox 
46f8a47341SAlan Cox #include <sys/param.h>
47f8a47341SAlan Cox #include <sys/kernel.h>
48f8a47341SAlan Cox #include <sys/lock.h>
49f8a47341SAlan Cox #include <sys/malloc.h>
50f8a47341SAlan Cox #include <sys/mutex.h>
51f8a47341SAlan Cox #include <sys/queue.h>
5289f6b863SAttilio Rao #include <sys/rwlock.h>
53f8a47341SAlan Cox #include <sys/sbuf.h>
54f8a47341SAlan Cox #include <sys/sysctl.h>
55f8a47341SAlan Cox #include <sys/systm.h>
5672346b22SCy Schubert #include <sys/counter.h>
5772346b22SCy Schubert #include <sys/ktr.h>
589ed01c32SGleb Smirnoff #include <sys/vmmeter.h>
595c930c89SJeff Roberson #include <sys/smp.h>
60f8a47341SAlan Cox 
61f8a47341SAlan Cox #include <vm/vm.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>
66f8a47341SAlan Cox #include <vm/vm_phys.h>
67e2068d0bSJeff Roberson #include <vm/vm_pagequeue.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 
80f8a47341SAlan Cox /*
81f8a47341SAlan Cox  * The number of small pages that are contained in a level 0 reservation
82f8a47341SAlan Cox  */
83f8a47341SAlan Cox #define	VM_LEVEL_0_NPAGES	(1 << VM_LEVEL_0_ORDER)
84f8a47341SAlan Cox 
85f8a47341SAlan Cox /*
86f8a47341SAlan Cox  * The number of bits by which a physical address is shifted to obtain the
87f8a47341SAlan Cox  * reservation number
88f8a47341SAlan Cox  */
89f8a47341SAlan Cox #define	VM_LEVEL_0_SHIFT	(VM_LEVEL_0_ORDER + PAGE_SHIFT)
90f8a47341SAlan Cox 
91f8a47341SAlan Cox /*
92f8a47341SAlan Cox  * The size of a level 0 reservation in bytes
93f8a47341SAlan Cox  */
94f8a47341SAlan Cox #define	VM_LEVEL_0_SIZE		(1 << VM_LEVEL_0_SHIFT)
95f8a47341SAlan Cox 
96f8a47341SAlan Cox /*
97f8a47341SAlan Cox  * Computes the index of the small page underlying the given (object, pindex)
98f8a47341SAlan Cox  * within the reservation's array of small pages.
99f8a47341SAlan Cox  */
100f8a47341SAlan Cox #define	VM_RESERV_INDEX(object, pindex)	\
101f8a47341SAlan Cox     (((object)->pg_color + (pindex)) & (VM_LEVEL_0_NPAGES - 1))
102f8a47341SAlan Cox 
103f8a47341SAlan Cox /*
104ec179322SAlan Cox  * The size of a population map entry
105ec179322SAlan Cox  */
106ec179322SAlan Cox typedef	u_long		popmap_t;
107ec179322SAlan Cox 
108ec179322SAlan Cox /*
109ec179322SAlan Cox  * The number of bits in a population map entry
110ec179322SAlan Cox  */
111ec179322SAlan Cox #define	NBPOPMAP	(NBBY * sizeof(popmap_t))
112ec179322SAlan Cox 
113ec179322SAlan Cox /*
114ec179322SAlan Cox  * The number of population map entries in a reservation
115ec179322SAlan Cox  */
116ec179322SAlan Cox #define	NPOPMAP		howmany(VM_LEVEL_0_NPAGES, NBPOPMAP)
117ec179322SAlan Cox 
118ec179322SAlan Cox /*
1193180f757SAlan Cox  * Clear a bit in the population map.
1203180f757SAlan Cox  */
1213180f757SAlan Cox static __inline void
1223180f757SAlan Cox popmap_clear(popmap_t popmap[], int i)
1233180f757SAlan Cox {
1243180f757SAlan Cox 
1253180f757SAlan Cox 	popmap[i / NBPOPMAP] &= ~(1UL << (i % NBPOPMAP));
1263180f757SAlan Cox }
1273180f757SAlan Cox 
1283180f757SAlan Cox /*
1293180f757SAlan Cox  * Set a bit in the population map.
1303180f757SAlan Cox  */
1313180f757SAlan Cox static __inline void
1323180f757SAlan Cox popmap_set(popmap_t popmap[], int i)
1333180f757SAlan Cox {
1343180f757SAlan Cox 
1353180f757SAlan Cox 	popmap[i / NBPOPMAP] |= 1UL << (i % NBPOPMAP);
1363180f757SAlan Cox }
1373180f757SAlan Cox 
1383180f757SAlan Cox /*
1393180f757SAlan Cox  * Is a bit in the population map clear?
1403180f757SAlan Cox  */
1413180f757SAlan Cox static __inline boolean_t
1423180f757SAlan Cox popmap_is_clear(popmap_t popmap[], int i)
1433180f757SAlan Cox {
1443180f757SAlan Cox 
1453180f757SAlan Cox 	return ((popmap[i / NBPOPMAP] & (1UL << (i % NBPOPMAP))) == 0);
1463180f757SAlan Cox }
1473180f757SAlan Cox 
1483180f757SAlan Cox /*
1493180f757SAlan Cox  * Is a bit in the population map set?
1503180f757SAlan Cox  */
1513180f757SAlan Cox static __inline boolean_t
1523180f757SAlan Cox popmap_is_set(popmap_t popmap[], int i)
1533180f757SAlan Cox {
1543180f757SAlan Cox 
1553180f757SAlan Cox 	return ((popmap[i / NBPOPMAP] & (1UL << (i % NBPOPMAP))) != 0);
1563180f757SAlan Cox }
1573180f757SAlan Cox 
1583180f757SAlan Cox /*
159f8a47341SAlan Cox  * The reservation structure
160f8a47341SAlan Cox  *
161f8a47341SAlan Cox  * A reservation structure is constructed whenever a large physical page is
162f8a47341SAlan Cox  * speculatively allocated to an object.  The reservation provides the small
163f8a47341SAlan Cox  * physical pages for the range [pindex, pindex + VM_LEVEL_0_NPAGES) of offsets
164f8a47341SAlan Cox  * within that object.  The reservation's "popcnt" tracks the number of these
165f8a47341SAlan Cox  * small physical pages that are in use at any given time.  When and if the
1663453bca8SAlan Cox  * reservation is not fully utilized, it appears in the queue of partially
167f8a47341SAlan Cox  * populated reservations.  The reservation always appears on the containing
168f8a47341SAlan Cox  * object's list of reservations.
169f8a47341SAlan Cox  *
1703453bca8SAlan Cox  * A partially populated reservation can be broken and reclaimed at any time.
171e2068d0bSJeff Roberson  *
1725c930c89SJeff Roberson  * r - vm_reserv_lock
1735c930c89SJeff Roberson  * d - vm_reserv_domain_lock
174e2068d0bSJeff Roberson  * o - vm_reserv_object_lock
175e2068d0bSJeff Roberson  * c - constant after boot
176f8a47341SAlan Cox  */
177f8a47341SAlan Cox struct vm_reserv {
1785c930c89SJeff Roberson 	struct mtx	lock;			/* reservation lock. */
1795c930c89SJeff Roberson 	TAILQ_ENTRY(vm_reserv) partpopq;	/* (d) per-domain queue. */
1805c930c89SJeff Roberson 	LIST_ENTRY(vm_reserv) objq;		/* (o, r) object queue */
1815c930c89SJeff Roberson 	vm_object_t	object;			/* (o, r) containing object */
1825c930c89SJeff Roberson 	vm_pindex_t	pindex;			/* (o, r) offset in object */
183e2068d0bSJeff Roberson 	vm_page_t	pages;			/* (c) first page  */
1845c930c89SJeff Roberson 	uint16_t	domain;			/* (c) NUMA domain. */
1855c930c89SJeff Roberson 	uint16_t	popcnt;			/* (r) # of pages in use */
1865c930c89SJeff Roberson 	char		inpartpopq;		/* (d) */
1875c930c89SJeff Roberson 	popmap_t	popmap[NPOPMAP];	/* (r) bit vector, used pages */
188f8a47341SAlan Cox };
189f8a47341SAlan Cox 
1905c930c89SJeff Roberson #define	vm_reserv_lockptr(rv)		(&(rv)->lock)
1915c930c89SJeff Roberson #define	vm_reserv_assert_locked(rv)					\
1925c930c89SJeff Roberson 	    mtx_assert(vm_reserv_lockptr(rv), MA_OWNED)
1935c930c89SJeff Roberson #define	vm_reserv_lock(rv)		mtx_lock(vm_reserv_lockptr(rv))
1945c930c89SJeff Roberson #define	vm_reserv_trylock(rv)		mtx_trylock(vm_reserv_lockptr(rv))
1955c930c89SJeff Roberson #define	vm_reserv_unlock(rv)		mtx_unlock(vm_reserv_lockptr(rv))
1965c930c89SJeff Roberson 
1975c930c89SJeff Roberson static struct mtx_padalign vm_reserv_domain_locks[MAXMEMDOM];
1985c930c89SJeff Roberson 
1995c930c89SJeff Roberson #define	vm_reserv_domain_lockptr(d)	&vm_reserv_domain_locks[(d)]
2005c930c89SJeff Roberson #define	vm_reserv_domain_lock(d)	mtx_lock(vm_reserv_domain_lockptr(d))
2015c930c89SJeff Roberson #define	vm_reserv_domain_unlock(d)	mtx_unlock(vm_reserv_domain_lockptr(d))
2025c930c89SJeff Roberson 
203f8a47341SAlan Cox /*
204f8a47341SAlan Cox  * The reservation array
205f8a47341SAlan Cox  *
206f8a47341SAlan Cox  * This array is analoguous in function to vm_page_array.  It differs in the
207f8a47341SAlan Cox  * respect that it may contain a greater number of useful reservation
208f8a47341SAlan Cox  * structures than there are (physical) superpages.  These "invalid"
209f8a47341SAlan Cox  * reservation structures exist to trade-off space for time in the
210f8a47341SAlan Cox  * implementation of vm_reserv_from_page().  Invalid reservation structures are
211f8a47341SAlan Cox  * distinguishable from "valid" reservation structures by inspecting the
212f8a47341SAlan Cox  * reservation's "pages" field.  Invalid reservation structures have a NULL
213f8a47341SAlan Cox  * "pages" field.
214f8a47341SAlan Cox  *
215f8a47341SAlan Cox  * vm_reserv_from_page() maps a small (physical) page to an element of this
216f8a47341SAlan Cox  * array by computing a physical reservation number from the page's physical
217f8a47341SAlan Cox  * address.  The physical reservation number is used as the array index.
218f8a47341SAlan Cox  *
219f8a47341SAlan Cox  * An "active" reservation is a valid reservation structure that has a non-NULL
220f8a47341SAlan Cox  * "object" field and a non-zero "popcnt" field.  In other words, every active
221f8a47341SAlan Cox  * reservation belongs to a particular object.  Moreover, every active
222f8a47341SAlan Cox  * reservation has an entry in the containing object's list of reservations.
223f8a47341SAlan Cox  */
224f8a47341SAlan Cox static vm_reserv_t vm_reserv_array;
225f8a47341SAlan Cox 
226f8a47341SAlan Cox /*
2273453bca8SAlan Cox  * The partially populated reservation queue
228f8a47341SAlan Cox  *
2293453bca8SAlan Cox  * This queue enables the fast recovery of an unused free small page from a
2303453bca8SAlan Cox  * partially populated reservation.  The reservation at the head of this queue
2313453bca8SAlan Cox  * is the least recently changed, partially populated reservation.
232f8a47341SAlan Cox  *
233f8a47341SAlan Cox  * Access to this queue is synchronized by the free page queue lock.
234f8a47341SAlan Cox  */
235ef435ae7SJeff Roberson static TAILQ_HEAD(, vm_reserv) vm_rvq_partpop[MAXMEMDOM];
236f8a47341SAlan Cox 
237f8a47341SAlan Cox static SYSCTL_NODE(_vm, OID_AUTO, reserv, CTLFLAG_RD, 0, "Reservation Info");
238f8a47341SAlan Cox 
2395c930c89SJeff Roberson static counter_u64_t vm_reserv_broken = EARLY_COUNTER;
2405c930c89SJeff Roberson SYSCTL_COUNTER_U64(_vm_reserv, OID_AUTO, broken, CTLFLAG_RD,
2415c930c89SJeff Roberson     &vm_reserv_broken, "Cumulative number of broken reservations");
242f8a47341SAlan Cox 
2435c930c89SJeff Roberson static counter_u64_t vm_reserv_freed = EARLY_COUNTER;
2445c930c89SJeff Roberson SYSCTL_COUNTER_U64(_vm_reserv, OID_AUTO, freed, CTLFLAG_RD,
2455c930c89SJeff Roberson     &vm_reserv_freed, "Cumulative number of freed reservations");
246f8a47341SAlan Cox 
247e0a63baaSAlan Cox static int sysctl_vm_reserv_fullpop(SYSCTL_HANDLER_ARGS);
248e0a63baaSAlan Cox 
249e0a63baaSAlan Cox SYSCTL_PROC(_vm_reserv, OID_AUTO, fullpop, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
250e0a63baaSAlan Cox     sysctl_vm_reserv_fullpop, "I", "Current number of full reservations");
251e0a63baaSAlan Cox 
252f8a47341SAlan Cox static int sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS);
253f8a47341SAlan Cox 
254f8a47341SAlan Cox SYSCTL_OID(_vm_reserv, OID_AUTO, partpopq, CTLTYPE_STRING | CTLFLAG_RD, NULL, 0,
2553453bca8SAlan Cox     sysctl_vm_reserv_partpopq, "A", "Partially populated reservation queues");
256f8a47341SAlan Cox 
2575c930c89SJeff Roberson static counter_u64_t vm_reserv_reclaimed = EARLY_COUNTER;
2585c930c89SJeff Roberson SYSCTL_COUNTER_U64(_vm_reserv, OID_AUTO, reclaimed, CTLFLAG_RD,
2595c930c89SJeff Roberson     &vm_reserv_reclaimed, "Cumulative number of reclaimed reservations");
260f8a47341SAlan Cox 
261e2068d0bSJeff Roberson /*
262e2068d0bSJeff Roberson  * The object lock pool is used to synchronize the rvq.  We can not use a
263e2068d0bSJeff Roberson  * pool mutex because it is required before malloc works.
264e2068d0bSJeff Roberson  *
265e2068d0bSJeff Roberson  * The "hash" function could be made faster without divide and modulo.
266e2068d0bSJeff Roberson  */
267e2068d0bSJeff Roberson #define	VM_RESERV_OBJ_LOCK_COUNT	MAXCPU
268e2068d0bSJeff Roberson 
269e2068d0bSJeff Roberson struct mtx_padalign vm_reserv_object_mtx[VM_RESERV_OBJ_LOCK_COUNT];
270e2068d0bSJeff Roberson 
271e2068d0bSJeff Roberson #define	vm_reserv_object_lock_idx(object)			\
272e2068d0bSJeff Roberson 	    (((uintptr_t)object / sizeof(*object)) % VM_RESERV_OBJ_LOCK_COUNT)
273e2068d0bSJeff Roberson #define	vm_reserv_object_lock_ptr(object)			\
274e2068d0bSJeff Roberson 	    &vm_reserv_object_mtx[vm_reserv_object_lock_idx((object))]
275e2068d0bSJeff Roberson #define	vm_reserv_object_lock(object)				\
276e2068d0bSJeff Roberson 	    mtx_lock(vm_reserv_object_lock_ptr((object)))
277e2068d0bSJeff Roberson #define	vm_reserv_object_unlock(object)				\
278e2068d0bSJeff Roberson 	    mtx_unlock(vm_reserv_object_lock_ptr((object)))
279e2068d0bSJeff Roberson 
280ada27a3bSKonstantin Belousov static void		vm_reserv_break(vm_reserv_t rv);
281ec179322SAlan Cox static void		vm_reserv_depopulate(vm_reserv_t rv, int index);
282f8a47341SAlan Cox static vm_reserv_t	vm_reserv_from_page(vm_page_t m);
283f8a47341SAlan Cox static boolean_t	vm_reserv_has_pindex(vm_reserv_t rv,
284f8a47341SAlan Cox 			    vm_pindex_t pindex);
285ec179322SAlan Cox static void		vm_reserv_populate(vm_reserv_t rv, int index);
28644aab2c3SAlan Cox static void		vm_reserv_reclaim(vm_reserv_t rv);
287f8a47341SAlan Cox 
288f8a47341SAlan Cox /*
289e0a63baaSAlan Cox  * Returns the current number of full reservations.
290e0a63baaSAlan Cox  *
291e0a63baaSAlan Cox  * Since the number of full reservations is computed without acquiring the
292e0a63baaSAlan Cox  * free page queue lock, the returned value may be inexact.
293e0a63baaSAlan Cox  */
294e0a63baaSAlan Cox static int
295e0a63baaSAlan Cox sysctl_vm_reserv_fullpop(SYSCTL_HANDLER_ARGS)
296e0a63baaSAlan Cox {
297e0a63baaSAlan Cox 	vm_paddr_t paddr;
298e0a63baaSAlan Cox 	struct vm_phys_seg *seg;
299e0a63baaSAlan Cox 	vm_reserv_t rv;
300e0a63baaSAlan Cox 	int fullpop, segind;
301e0a63baaSAlan Cox 
302e0a63baaSAlan Cox 	fullpop = 0;
303e0a63baaSAlan Cox 	for (segind = 0; segind < vm_phys_nsegs; segind++) {
304e0a63baaSAlan Cox 		seg = &vm_phys_segs[segind];
305e0a63baaSAlan Cox 		paddr = roundup2(seg->start, VM_LEVEL_0_SIZE);
306e0a63baaSAlan Cox 		while (paddr + VM_LEVEL_0_SIZE <= seg->end) {
307e0a63baaSAlan Cox 			rv = &vm_reserv_array[paddr >> VM_LEVEL_0_SHIFT];
308e0a63baaSAlan Cox 			fullpop += rv->popcnt == VM_LEVEL_0_NPAGES;
309e0a63baaSAlan Cox 			paddr += VM_LEVEL_0_SIZE;
310e0a63baaSAlan Cox 		}
311e0a63baaSAlan Cox 	}
312e0a63baaSAlan Cox 	return (sysctl_handle_int(oidp, &fullpop, 0, req));
313e0a63baaSAlan Cox }
314e0a63baaSAlan Cox 
315e0a63baaSAlan Cox /*
3163453bca8SAlan Cox  * Describes the current state of the partially populated reservation queue.
317f8a47341SAlan Cox  */
318f8a47341SAlan Cox static int
319f8a47341SAlan Cox sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS)
320f8a47341SAlan Cox {
321f8a47341SAlan Cox 	struct sbuf sbuf;
322f8a47341SAlan Cox 	vm_reserv_t rv;
323ef435ae7SJeff Roberson 	int counter, error, domain, level, unused_pages;
324f8a47341SAlan Cox 
32500f0e671SMatthew D Fleming 	error = sysctl_wire_old_buffer(req, 0);
32600f0e671SMatthew D Fleming 	if (error != 0)
32700f0e671SMatthew D Fleming 		return (error);
3284e657159SMatthew D Fleming 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
329ef435ae7SJeff Roberson 	sbuf_printf(&sbuf, "\nDOMAIN    LEVEL     SIZE  NUMBER\n\n");
330ef435ae7SJeff Roberson 	for (domain = 0; domain < vm_ndomains; domain++) {
331f8a47341SAlan Cox 		for (level = -1; level <= VM_NRESERVLEVEL - 2; level++) {
332f8a47341SAlan Cox 			counter = 0;
333f8a47341SAlan Cox 			unused_pages = 0;
3345c930c89SJeff Roberson 			vm_reserv_domain_lock(domain);
335ef435ae7SJeff Roberson 			TAILQ_FOREACH(rv, &vm_rvq_partpop[domain], partpopq) {
336f8a47341SAlan Cox 				counter++;
337f8a47341SAlan Cox 				unused_pages += VM_LEVEL_0_NPAGES - rv->popcnt;
338f8a47341SAlan Cox 			}
3395c930c89SJeff Roberson 			vm_reserv_domain_unlock(domain);
340ef435ae7SJeff Roberson 			sbuf_printf(&sbuf, "%6d, %7d, %6dK, %6d\n",
341ef435ae7SJeff Roberson 			    domain, level,
3422cf36c8fSAlan Cox 			    unused_pages * ((int)PAGE_SIZE / 1024), counter);
343f8a47341SAlan Cox 		}
344ef435ae7SJeff Roberson 	}
3454e657159SMatthew D Fleming 	error = sbuf_finish(&sbuf);
346f8a47341SAlan Cox 	sbuf_delete(&sbuf);
347f8a47341SAlan Cox 	return (error);
348f8a47341SAlan Cox }
349f8a47341SAlan Cox 
350f8a47341SAlan Cox /*
351e2068d0bSJeff Roberson  * Remove a reservation from the object's objq.
352e2068d0bSJeff Roberson  */
353e2068d0bSJeff Roberson static void
354e2068d0bSJeff Roberson vm_reserv_remove(vm_reserv_t rv)
355e2068d0bSJeff Roberson {
356e2068d0bSJeff Roberson 	vm_object_t object;
357e2068d0bSJeff Roberson 
3585c930c89SJeff Roberson 	vm_reserv_assert_locked(rv);
3595c930c89SJeff Roberson 	CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d",
3605c930c89SJeff Roberson 	    __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq);
361e2068d0bSJeff Roberson 	KASSERT(rv->object != NULL,
362e2068d0bSJeff Roberson 	    ("vm_reserv_remove: reserv %p is free", rv));
363e2068d0bSJeff Roberson 	KASSERT(!rv->inpartpopq,
364e2068d0bSJeff Roberson 	    ("vm_reserv_remove: reserv %p's inpartpopq is TRUE", rv));
365e2068d0bSJeff Roberson 	object = rv->object;
366e2068d0bSJeff Roberson 	vm_reserv_object_lock(object);
367e2068d0bSJeff Roberson 	LIST_REMOVE(rv, objq);
368e2068d0bSJeff Roberson 	rv->object = NULL;
369e2068d0bSJeff Roberson 	vm_reserv_object_unlock(object);
370e2068d0bSJeff Roberson }
371e2068d0bSJeff Roberson 
372e2068d0bSJeff Roberson /*
373e2068d0bSJeff Roberson  * Insert a new reservation into the object's objq.
374e2068d0bSJeff Roberson  */
375e2068d0bSJeff Roberson static void
376e2068d0bSJeff Roberson vm_reserv_insert(vm_reserv_t rv, vm_object_t object, vm_pindex_t pindex)
377e2068d0bSJeff Roberson {
378e2068d0bSJeff Roberson 	int i;
379e2068d0bSJeff Roberson 
3805c930c89SJeff Roberson 	vm_reserv_assert_locked(rv);
3815c930c89SJeff Roberson 	CTR6(KTR_VM,
3825c930c89SJeff Roberson 	    "%s: rv %p(%p) object %p new %p popcnt %d",
3835c930c89SJeff Roberson 	    __FUNCTION__, rv, rv->pages, rv->object, object,
3845c930c89SJeff Roberson 	   rv->popcnt);
385e2068d0bSJeff Roberson 	KASSERT(rv->object == NULL,
386e2068d0bSJeff Roberson 	    ("vm_reserv_insert: reserv %p isn't free", rv));
387e2068d0bSJeff Roberson 	KASSERT(rv->popcnt == 0,
388e2068d0bSJeff Roberson 	    ("vm_reserv_insert: reserv %p's popcnt is corrupted", rv));
389e2068d0bSJeff Roberson 	KASSERT(!rv->inpartpopq,
390e2068d0bSJeff Roberson 	    ("vm_reserv_insert: reserv %p's inpartpopq is TRUE", rv));
391e2068d0bSJeff Roberson 	for (i = 0; i < NPOPMAP; i++)
392e2068d0bSJeff Roberson 		KASSERT(rv->popmap[i] == 0,
393e2068d0bSJeff Roberson 		    ("vm_reserv_insert: reserv %p's popmap is corrupted", rv));
394e2068d0bSJeff Roberson 	vm_reserv_object_lock(object);
395e2068d0bSJeff Roberson 	rv->pindex = pindex;
396e2068d0bSJeff Roberson 	rv->object = object;
397e2068d0bSJeff Roberson 	LIST_INSERT_HEAD(&object->rvq, rv, objq);
398e2068d0bSJeff Roberson 	vm_reserv_object_unlock(object);
399e2068d0bSJeff Roberson }
400e2068d0bSJeff Roberson 
401e2068d0bSJeff Roberson /*
402f8a47341SAlan Cox  * Reduces the given reservation's population count.  If the population count
403f8a47341SAlan Cox  * becomes zero, the reservation is destroyed.  Additionally, moves the
4043453bca8SAlan Cox  * reservation to the tail of the partially populated reservation queue if the
405f8a47341SAlan Cox  * population count is non-zero.
406f8a47341SAlan Cox  */
407f8a47341SAlan Cox static void
408ec179322SAlan Cox vm_reserv_depopulate(vm_reserv_t rv, int index)
409f8a47341SAlan Cox {
4105c930c89SJeff Roberson 	struct vm_domain *vmd;
411f8a47341SAlan Cox 
4125c930c89SJeff Roberson 	vm_reserv_assert_locked(rv);
4135c930c89SJeff Roberson 	CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d",
4145c930c89SJeff Roberson 	    __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq);
415f8a47341SAlan Cox 	KASSERT(rv->object != NULL,
416f8a47341SAlan Cox 	    ("vm_reserv_depopulate: reserv %p is free", rv));
4173180f757SAlan Cox 	KASSERT(popmap_is_set(rv->popmap, index),
418a08c1515SAlan Cox 	    ("vm_reserv_depopulate: reserv %p's popmap[%d] is clear", rv,
419a08c1515SAlan Cox 	    index));
420f8a47341SAlan Cox 	KASSERT(rv->popcnt > 0,
421f8a47341SAlan Cox 	    ("vm_reserv_depopulate: reserv %p's popcnt is corrupted", rv));
422*2d3f4181SJeff Roberson 	KASSERT(rv->domain < vm_ndomains,
423ef435ae7SJeff Roberson 	    ("vm_reserv_depopulate: reserv %p's domain is corrupted %d",
424ef435ae7SJeff Roberson 	    rv, rv->domain));
4255c930c89SJeff Roberson 	if (rv->popcnt == VM_LEVEL_0_NPAGES) {
426dd05fa19SAlan Cox 		KASSERT(rv->pages->psind == 1,
427dd05fa19SAlan Cox 		    ("vm_reserv_depopulate: reserv %p is already demoted",
428dd05fa19SAlan Cox 		    rv));
429dd05fa19SAlan Cox 		rv->pages->psind = 0;
430f8a47341SAlan Cox 	}
4313180f757SAlan Cox 	popmap_clear(rv->popmap, index);
432f8a47341SAlan Cox 	rv->popcnt--;
4335c930c89SJeff Roberson 	vm_reserv_domain_lock(rv->domain);
4345c930c89SJeff Roberson 	if (rv->inpartpopq) {
4355c930c89SJeff Roberson 		TAILQ_REMOVE(&vm_rvq_partpop[rv->domain], rv, partpopq);
4365c930c89SJeff Roberson 		rv->inpartpopq = FALSE;
4375c930c89SJeff Roberson 	}
4385c930c89SJeff Roberson 	if (rv->popcnt != 0) {
439f8a47341SAlan Cox 		rv->inpartpopq = TRUE;
440ef435ae7SJeff Roberson 		TAILQ_INSERT_TAIL(&vm_rvq_partpop[rv->domain], rv, partpopq);
441f8a47341SAlan Cox 	}
4425c930c89SJeff Roberson 	vm_reserv_domain_unlock(rv->domain);
4435c930c89SJeff Roberson 	vmd = VM_DOMAIN(rv->domain);
4445c930c89SJeff Roberson 	if (rv->popcnt == 0) {
4455c930c89SJeff Roberson 		vm_reserv_remove(rv);
4465c930c89SJeff Roberson 		vm_domain_free_lock(vmd);
4475c930c89SJeff Roberson 		vm_phys_free_pages(rv->pages, VM_LEVEL_0_ORDER);
4485c930c89SJeff Roberson 		vm_domain_free_unlock(vmd);
4495c930c89SJeff Roberson 		counter_u64_add(vm_reserv_freed, 1);
4505c930c89SJeff Roberson 	}
4515c930c89SJeff Roberson 	vm_domain_freecnt_inc(vmd, 1);
452f8a47341SAlan Cox }
453f8a47341SAlan Cox 
454f8a47341SAlan Cox /*
455f8a47341SAlan Cox  * Returns the reservation to which the given page might belong.
456f8a47341SAlan Cox  */
457f8a47341SAlan Cox static __inline vm_reserv_t
458f8a47341SAlan Cox vm_reserv_from_page(vm_page_t m)
459f8a47341SAlan Cox {
460f8a47341SAlan Cox 
461f8a47341SAlan Cox 	return (&vm_reserv_array[VM_PAGE_TO_PHYS(m) >> VM_LEVEL_0_SHIFT]);
462f8a47341SAlan Cox }
463f8a47341SAlan Cox 
464f8a47341SAlan Cox /*
465e2068d0bSJeff Roberson  * Returns an existing reservation or NULL and initialized successor pointer.
466e2068d0bSJeff Roberson  */
467e2068d0bSJeff Roberson static vm_reserv_t
468e2068d0bSJeff Roberson vm_reserv_from_object(vm_object_t object, vm_pindex_t pindex,
469e2068d0bSJeff Roberson     vm_page_t mpred, vm_page_t *msuccp)
470e2068d0bSJeff Roberson {
471e2068d0bSJeff Roberson 	vm_reserv_t rv;
472e2068d0bSJeff Roberson 	vm_page_t msucc;
473e2068d0bSJeff Roberson 
474e2068d0bSJeff Roberson 	msucc = NULL;
475e2068d0bSJeff Roberson 	if (mpred != NULL) {
476e2068d0bSJeff Roberson 		KASSERT(mpred->object == object,
477e2068d0bSJeff Roberson 		    ("vm_reserv_from_object: object doesn't contain mpred"));
478e2068d0bSJeff Roberson 		KASSERT(mpred->pindex < pindex,
479e2068d0bSJeff Roberson 		    ("vm_reserv_from_object: mpred doesn't precede pindex"));
480e2068d0bSJeff Roberson 		rv = vm_reserv_from_page(mpred);
481e2068d0bSJeff Roberson 		if (rv->object == object && vm_reserv_has_pindex(rv, pindex))
482e2068d0bSJeff Roberson 			goto found;
483e2068d0bSJeff Roberson 		msucc = TAILQ_NEXT(mpred, listq);
484e2068d0bSJeff Roberson 	} else
485e2068d0bSJeff Roberson 		msucc = TAILQ_FIRST(&object->memq);
486e2068d0bSJeff Roberson 	if (msucc != NULL) {
487e2068d0bSJeff Roberson 		KASSERT(msucc->pindex > pindex,
488e2068d0bSJeff Roberson 		    ("vm_reserv_from_object: msucc doesn't succeed pindex"));
489e2068d0bSJeff Roberson 		rv = vm_reserv_from_page(msucc);
490e2068d0bSJeff Roberson 		if (rv->object == object && vm_reserv_has_pindex(rv, pindex))
491e2068d0bSJeff Roberson 			goto found;
492e2068d0bSJeff Roberson 	}
493e2068d0bSJeff Roberson 	rv = NULL;
494e2068d0bSJeff Roberson 
495e2068d0bSJeff Roberson found:
496e2068d0bSJeff Roberson 	*msuccp = msucc;
497e2068d0bSJeff Roberson 
498e2068d0bSJeff Roberson 	return (rv);
499e2068d0bSJeff Roberson }
500e2068d0bSJeff Roberson 
501e2068d0bSJeff Roberson /*
502f8a47341SAlan Cox  * Returns TRUE if the given reservation contains the given page index and
503f8a47341SAlan Cox  * FALSE otherwise.
504f8a47341SAlan Cox  */
505f8a47341SAlan Cox static __inline boolean_t
506f8a47341SAlan Cox vm_reserv_has_pindex(vm_reserv_t rv, vm_pindex_t pindex)
507f8a47341SAlan Cox {
508f8a47341SAlan Cox 
509f8a47341SAlan Cox 	return (((pindex - rv->pindex) & ~(VM_LEVEL_0_NPAGES - 1)) == 0);
510f8a47341SAlan Cox }
511f8a47341SAlan Cox 
512f8a47341SAlan Cox /*
513f8a47341SAlan Cox  * Increases the given reservation's population count.  Moves the reservation
5143453bca8SAlan Cox  * to the tail of the partially populated reservation queue.
515f8a47341SAlan Cox  *
516f8a47341SAlan Cox  * The free page queue must be locked.
517f8a47341SAlan Cox  */
518f8a47341SAlan Cox static void
519ec179322SAlan Cox vm_reserv_populate(vm_reserv_t rv, int index)
520f8a47341SAlan Cox {
521f8a47341SAlan Cox 
5225c930c89SJeff Roberson 	vm_reserv_assert_locked(rv);
5235c930c89SJeff Roberson 	CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d",
5245c930c89SJeff Roberson 	    __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq);
525f8a47341SAlan Cox 	KASSERT(rv->object != NULL,
526f8a47341SAlan Cox 	    ("vm_reserv_populate: reserv %p is free", rv));
5273180f757SAlan Cox 	KASSERT(popmap_is_clear(rv->popmap, index),
528a08c1515SAlan Cox 	    ("vm_reserv_populate: reserv %p's popmap[%d] is set", rv,
529a08c1515SAlan Cox 	    index));
530f8a47341SAlan Cox 	KASSERT(rv->popcnt < VM_LEVEL_0_NPAGES,
531f8a47341SAlan Cox 	    ("vm_reserv_populate: reserv %p is already full", rv));
532dd05fa19SAlan Cox 	KASSERT(rv->pages->psind == 0,
533dd05fa19SAlan Cox 	    ("vm_reserv_populate: reserv %p is already promoted", rv));
534*2d3f4181SJeff Roberson 	KASSERT(rv->domain < vm_ndomains,
535ef435ae7SJeff Roberson 	    ("vm_reserv_populate: reserv %p's domain is corrupted %d",
536ef435ae7SJeff Roberson 	    rv, rv->domain));
5375c930c89SJeff Roberson 	popmap_set(rv->popmap, index);
5385c930c89SJeff Roberson 	rv->popcnt++;
5395c930c89SJeff Roberson 	vm_reserv_domain_lock(rv->domain);
540f8a47341SAlan Cox 	if (rv->inpartpopq) {
541ef435ae7SJeff Roberson 		TAILQ_REMOVE(&vm_rvq_partpop[rv->domain], rv, partpopq);
542f8a47341SAlan Cox 		rv->inpartpopq = FALSE;
543f8a47341SAlan Cox 	}
544f8a47341SAlan Cox 	if (rv->popcnt < VM_LEVEL_0_NPAGES) {
545f8a47341SAlan Cox 		rv->inpartpopq = TRUE;
546ef435ae7SJeff Roberson 		TAILQ_INSERT_TAIL(&vm_rvq_partpop[rv->domain], rv, partpopq);
5475c930c89SJeff Roberson 	} else {
5485c930c89SJeff Roberson 		KASSERT(rv->pages->psind == 0,
5495c930c89SJeff Roberson 		    ("vm_reserv_populate: reserv %p is already promoted",
5505c930c89SJeff Roberson 		    rv));
551dd05fa19SAlan Cox 		rv->pages->psind = 1;
552f8a47341SAlan Cox 	}
5535c930c89SJeff Roberson 	vm_reserv_domain_unlock(rv->domain);
5545c930c89SJeff Roberson }
555f8a47341SAlan Cox 
556f8a47341SAlan Cox /*
5575f70fb14SMark Johnston  * Attempts to allocate a contiguous set of physical pages from existing
5585f70fb14SMark Johnston  * reservations.  See vm_reserv_alloc_contig() for a description of the
5595f70fb14SMark Johnston  * function's parameters.
560c68c3537SAlan Cox  *
561920da7e4SAlan Cox  * The page "mpred" must immediately precede the offset "pindex" within the
562920da7e4SAlan Cox  * specified object.
563920da7e4SAlan Cox  *
5645f70fb14SMark Johnston  * The object must be locked.
565c68c3537SAlan Cox  */
566c68c3537SAlan Cox vm_page_t
567e2068d0bSJeff Roberson vm_reserv_extend_contig(int req, vm_object_t object, vm_pindex_t pindex,
568e2068d0bSJeff Roberson     int domain, u_long npages, vm_paddr_t low, vm_paddr_t high,
569e2068d0bSJeff Roberson     u_long alignment, vm_paddr_t boundary, vm_page_t mpred)
570e2068d0bSJeff Roberson {
571e2068d0bSJeff Roberson 	struct vm_domain *vmd;
572e2068d0bSJeff Roberson 	vm_paddr_t pa, size;
573e2068d0bSJeff Roberson 	vm_page_t m, msucc;
574e2068d0bSJeff Roberson 	vm_reserv_t rv;
575e2068d0bSJeff Roberson 	int i, index;
576e2068d0bSJeff Roberson 
577e2068d0bSJeff Roberson 	VM_OBJECT_ASSERT_WLOCKED(object);
578e2068d0bSJeff Roberson 	KASSERT(npages != 0, ("vm_reserv_alloc_contig: npages is 0"));
579e2068d0bSJeff Roberson 
580e2068d0bSJeff Roberson 	/*
581e2068d0bSJeff Roberson 	 * Is a reservation fundamentally impossible?
582e2068d0bSJeff Roberson 	 */
583e2068d0bSJeff Roberson 	if (pindex < VM_RESERV_INDEX(object, pindex) ||
584e2068d0bSJeff Roberson 	    pindex + npages > object->size || object->resident_page_count == 0)
585e2068d0bSJeff Roberson 		return (NULL);
586e2068d0bSJeff Roberson 
587e2068d0bSJeff Roberson 	/*
588e2068d0bSJeff Roberson 	 * All reservations of a particular size have the same alignment.
589e2068d0bSJeff Roberson 	 * Assuming that the first page is allocated from a reservation, the
590e2068d0bSJeff Roberson 	 * least significant bits of its physical address can be determined
591e2068d0bSJeff Roberson 	 * from its offset from the beginning of the reservation and the size
592e2068d0bSJeff Roberson 	 * of the reservation.
593e2068d0bSJeff Roberson 	 *
594e2068d0bSJeff Roberson 	 * Could the specified index within a reservation of the smallest
595e2068d0bSJeff Roberson 	 * possible size satisfy the alignment and boundary requirements?
596e2068d0bSJeff Roberson 	 */
597e2068d0bSJeff Roberson 	pa = VM_RESERV_INDEX(object, pindex) << PAGE_SHIFT;
598e2068d0bSJeff Roberson 	if ((pa & (alignment - 1)) != 0)
599e2068d0bSJeff Roberson 		return (NULL);
600e2068d0bSJeff Roberson 	size = npages << PAGE_SHIFT;
601e2068d0bSJeff Roberson 	if (((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0)
602e2068d0bSJeff Roberson 		return (NULL);
603e2068d0bSJeff Roberson 
604e2068d0bSJeff Roberson 	/*
605e2068d0bSJeff Roberson 	 * Look for an existing reservation.
606e2068d0bSJeff Roberson 	 */
607e2068d0bSJeff Roberson 	rv = vm_reserv_from_object(object, pindex, mpred, &msucc);
608e2068d0bSJeff Roberson 	if (rv == NULL)
609e2068d0bSJeff Roberson 		return (NULL);
610e2068d0bSJeff Roberson 	KASSERT(object != kernel_object || rv->domain == domain,
611e2068d0bSJeff Roberson 	    ("vm_reserv_extend_contig: Domain mismatch from reservation."));
612e2068d0bSJeff Roberson 	index = VM_RESERV_INDEX(object, pindex);
613e2068d0bSJeff Roberson 	/* Does the allocation fit within the reservation? */
614e2068d0bSJeff Roberson 	if (index + npages > VM_LEVEL_0_NPAGES)
615e2068d0bSJeff Roberson 		return (NULL);
616e2068d0bSJeff Roberson 	domain = rv->domain;
617e2068d0bSJeff Roberson 	vmd = VM_DOMAIN(domain);
6185c930c89SJeff Roberson 	vm_reserv_lock(rv);
6195c930c89SJeff Roberson 	if (rv->object != object)
620e2068d0bSJeff Roberson 		goto out;
621e2068d0bSJeff Roberson 	m = &rv->pages[index];
622e2068d0bSJeff Roberson 	pa = VM_PAGE_TO_PHYS(m);
623e2068d0bSJeff Roberson 	if (pa < low || pa + size > high || (pa & (alignment - 1)) != 0 ||
6245c930c89SJeff Roberson 	    ((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0)
625e2068d0bSJeff Roberson 		goto out;
626e2068d0bSJeff Roberson 	/* Handle vm_page_rename(m, new_object, ...). */
627e2068d0bSJeff Roberson 	for (i = 0; i < npages; i++) {
6285c930c89SJeff Roberson 		if (popmap_is_set(rv->popmap, index + i))
629e2068d0bSJeff Roberson 			goto out;
630e2068d0bSJeff Roberson 	}
6315c930c89SJeff Roberson 	if (!vm_domain_allocate(vmd, req, npages))
6325c930c89SJeff Roberson 		goto out;
633e2068d0bSJeff Roberson 	for (i = 0; i < npages; i++)
634e2068d0bSJeff Roberson 		vm_reserv_populate(rv, index + i);
6355c930c89SJeff Roberson 	vm_reserv_unlock(rv);
636e2068d0bSJeff Roberson 	return (m);
6375c930c89SJeff Roberson 
6385c930c89SJeff Roberson out:
6395c930c89SJeff Roberson 	vm_reserv_unlock(rv);
6405c930c89SJeff Roberson 	return (NULL);
641e2068d0bSJeff Roberson }
642e2068d0bSJeff Roberson 
643e2068d0bSJeff Roberson /*
644e2068d0bSJeff Roberson  * Allocates a contiguous set of physical pages of the given size "npages"
6455f70fb14SMark Johnston  * from newly created reservations.  All of the physical pages
646e2068d0bSJeff Roberson  * must be at or above the given physical address "low" and below the given
647e2068d0bSJeff Roberson  * physical address "high".  The given value "alignment" determines the
648e2068d0bSJeff Roberson  * alignment of the first physical page in the set.  If the given value
649e2068d0bSJeff Roberson  * "boundary" is non-zero, then the set of physical pages cannot cross any
650e2068d0bSJeff Roberson  * physical address boundary that is a multiple of that value.  Both
651e2068d0bSJeff Roberson  * "alignment" and "boundary" must be a power of two.
652e2068d0bSJeff Roberson  *
6535f70fb14SMark Johnston  * Callers should first invoke vm_reserv_extend_contig() to attempt an
6545f70fb14SMark Johnston  * allocation from existing reservations.
6555f70fb14SMark Johnston  *
656e2068d0bSJeff Roberson  * The page "mpred" must immediately precede the offset "pindex" within the
657e2068d0bSJeff Roberson  * specified object.
658e2068d0bSJeff Roberson  *
659e2068d0bSJeff Roberson  * The object and free page queue must be locked.
660e2068d0bSJeff Roberson  */
661e2068d0bSJeff Roberson vm_page_t
6625c930c89SJeff Roberson vm_reserv_alloc_contig(int req, vm_object_t object, vm_pindex_t pindex, int domain,
663ef435ae7SJeff Roberson     u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
664ef435ae7SJeff Roberson     vm_paddr_t boundary, vm_page_t mpred)
665c68c3537SAlan Cox {
6665c930c89SJeff Roberson 	struct vm_domain *vmd;
667c68c3537SAlan Cox 	vm_paddr_t pa, size;
668920da7e4SAlan Cox 	vm_page_t m, m_ret, msucc;
669c68c3537SAlan Cox 	vm_pindex_t first, leftcap, rightcap;
670c68c3537SAlan Cox 	vm_reserv_t rv;
671c68c3537SAlan Cox 	u_long allocpages, maxpages, minpages;
672c68c3537SAlan Cox 	int i, index, n;
673c68c3537SAlan Cox 
67489f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
675c68c3537SAlan Cox 	KASSERT(npages != 0, ("vm_reserv_alloc_contig: npages is 0"));
676c68c3537SAlan Cox 
677c68c3537SAlan Cox 	/*
678c68c3537SAlan Cox 	 * Is a reservation fundamentally impossible?
679c68c3537SAlan Cox 	 */
680c68c3537SAlan Cox 	if (pindex < VM_RESERV_INDEX(object, pindex) ||
681c68c3537SAlan Cox 	    pindex + npages > object->size)
682c68c3537SAlan Cox 		return (NULL);
683c68c3537SAlan Cox 
684c68c3537SAlan Cox 	/*
685c68c3537SAlan Cox 	 * All reservations of a particular size have the same alignment.
686c68c3537SAlan Cox 	 * Assuming that the first page is allocated from a reservation, the
687c68c3537SAlan Cox 	 * least significant bits of its physical address can be determined
688c68c3537SAlan Cox 	 * from its offset from the beginning of the reservation and the size
689c68c3537SAlan Cox 	 * of the reservation.
690c68c3537SAlan Cox 	 *
691c68c3537SAlan Cox 	 * Could the specified index within a reservation of the smallest
692c68c3537SAlan Cox 	 * possible size satisfy the alignment and boundary requirements?
693c68c3537SAlan Cox 	 */
694c68c3537SAlan Cox 	pa = VM_RESERV_INDEX(object, pindex) << PAGE_SHIFT;
695c68c3537SAlan Cox 	if ((pa & (alignment - 1)) != 0)
696c68c3537SAlan Cox 		return (NULL);
697c68c3537SAlan Cox 	size = npages << PAGE_SHIFT;
698c68c3537SAlan Cox 	if (((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0)
699c68c3537SAlan Cox 		return (NULL);
700c68c3537SAlan Cox 
701c68c3537SAlan Cox 	/*
702e2068d0bSJeff Roberson 	 * Callers should've extended an existing reservation prior to
703e2068d0bSJeff Roberson 	 * calling this function.  If a reservation exists it is
704e2068d0bSJeff Roberson 	 * incompatible with the allocation.
705c68c3537SAlan Cox 	 */
706e2068d0bSJeff Roberson 	rv = vm_reserv_from_object(object, pindex, mpred, &msucc);
707e2068d0bSJeff Roberson 	if (rv != NULL)
708e2068d0bSJeff Roberson 		return (NULL);
709c68c3537SAlan Cox 
710c68c3537SAlan Cox 	/*
711c68c3537SAlan Cox 	 * Could at least one reservation fit between the first index to the
71264f096eeSAlan Cox 	 * left that can be used ("leftcap") and the first index to the right
71364f096eeSAlan Cox 	 * that cannot be used ("rightcap")?
714e2068d0bSJeff Roberson 	 *
715e2068d0bSJeff Roberson 	 * We must synchronize with the reserv object lock to protect the
716e2068d0bSJeff Roberson 	 * pindex/object of the resulting reservations against rename while
717e2068d0bSJeff Roberson 	 * we are inspecting.
718c68c3537SAlan Cox 	 */
719c68c3537SAlan Cox 	first = pindex - VM_RESERV_INDEX(object, pindex);
720e2068d0bSJeff Roberson 	minpages = VM_RESERV_INDEX(object, pindex) + npages;
721e2068d0bSJeff Roberson 	maxpages = roundup2(minpages, VM_LEVEL_0_NPAGES);
722e2068d0bSJeff Roberson 	allocpages = maxpages;
723e2068d0bSJeff Roberson 	vm_reserv_object_lock(object);
724c68c3537SAlan Cox 	if (mpred != NULL) {
725c68c3537SAlan Cox 		if ((rv = vm_reserv_from_page(mpred))->object != object)
726c68c3537SAlan Cox 			leftcap = mpred->pindex + 1;
727c68c3537SAlan Cox 		else
728c68c3537SAlan Cox 			leftcap = rv->pindex + VM_LEVEL_0_NPAGES;
729e2068d0bSJeff Roberson 		if (leftcap > first) {
730e2068d0bSJeff Roberson 			vm_reserv_object_unlock(object);
731c68c3537SAlan Cox 			return (NULL);
732c68c3537SAlan Cox 		}
733e2068d0bSJeff Roberson 	}
734c68c3537SAlan Cox 	if (msucc != NULL) {
735c68c3537SAlan Cox 		if ((rv = vm_reserv_from_page(msucc))->object != object)
736c68c3537SAlan Cox 			rightcap = msucc->pindex;
737c68c3537SAlan Cox 		else
738c68c3537SAlan Cox 			rightcap = rv->pindex;
739c68c3537SAlan Cox 		if (first + maxpages > rightcap) {
740e2068d0bSJeff Roberson 			if (maxpages == VM_LEVEL_0_NPAGES) {
741e2068d0bSJeff Roberson 				vm_reserv_object_unlock(object);
742c68c3537SAlan Cox 				return (NULL);
743e2068d0bSJeff Roberson 			}
74464f096eeSAlan Cox 
74564f096eeSAlan Cox 			/*
74664f096eeSAlan Cox 			 * At least one reservation will fit between "leftcap"
74764f096eeSAlan Cox 			 * and "rightcap".  However, a reservation for the
74864f096eeSAlan Cox 			 * last of the requested pages will not fit.  Reduce
74964f096eeSAlan Cox 			 * the size of the upcoming allocation accordingly.
75064f096eeSAlan Cox 			 */
751c68c3537SAlan Cox 			allocpages = minpages;
752c68c3537SAlan Cox 		}
753c68c3537SAlan Cox 	}
754e2068d0bSJeff Roberson 	vm_reserv_object_unlock(object);
755c68c3537SAlan Cox 
756c68c3537SAlan Cox 	/*
757c68c3537SAlan Cox 	 * Would the last new reservation extend past the end of the object?
758c68c3537SAlan Cox 	 */
759c68c3537SAlan Cox 	if (first + maxpages > object->size) {
760c68c3537SAlan Cox 		/*
761c68c3537SAlan Cox 		 * Don't allocate the last new reservation if the object is a
762c68c3537SAlan Cox 		 * vnode or backed by another object that is a vnode.
763c68c3537SAlan Cox 		 */
764c68c3537SAlan Cox 		if (object->type == OBJT_VNODE ||
765c68c3537SAlan Cox 		    (object->backing_object != NULL &&
766c68c3537SAlan Cox 		    object->backing_object->type == OBJT_VNODE)) {
767c68c3537SAlan Cox 			if (maxpages == VM_LEVEL_0_NPAGES)
768c68c3537SAlan Cox 				return (NULL);
769c68c3537SAlan Cox 			allocpages = minpages;
770c68c3537SAlan Cox 		}
771c68c3537SAlan Cox 		/* Speculate that the object may grow. */
772c68c3537SAlan Cox 	}
773c68c3537SAlan Cox 
774c68c3537SAlan Cox 	/*
77564f096eeSAlan Cox 	 * Allocate the physical pages.  The alignment and boundary specified
77664f096eeSAlan Cox 	 * for this allocation may be different from the alignment and
77764f096eeSAlan Cox 	 * boundary specified for the requested pages.  For instance, the
77864f096eeSAlan Cox 	 * specified index may not be the first page within the first new
77964f096eeSAlan Cox 	 * reservation.
780c68c3537SAlan Cox 	 */
7815c930c89SJeff Roberson 	m = NULL;
7825c930c89SJeff Roberson 	vmd = VM_DOMAIN(domain);
7835c930c89SJeff Roberson 	if (vm_domain_allocate(vmd, req, npages)) {
7845c930c89SJeff Roberson 		vm_domain_free_lock(vmd);
7855c930c89SJeff Roberson 		m = vm_phys_alloc_contig(domain, allocpages, low, high,
7865c930c89SJeff Roberson 		    ulmax(alignment, VM_LEVEL_0_SIZE),
7875c930c89SJeff Roberson 		    boundary > VM_LEVEL_0_SIZE ? boundary : 0);
7885c930c89SJeff Roberson 		vm_domain_free_unlock(vmd);
7895c930c89SJeff Roberson 		if (m == NULL) {
7905c930c89SJeff Roberson 			vm_domain_freecnt_inc(vmd, npages);
7915c930c89SJeff Roberson 			return (NULL);
7925c930c89SJeff Roberson 		}
7935c930c89SJeff Roberson 	} else
794c68c3537SAlan Cox 		return (NULL);
795e2068d0bSJeff Roberson 	KASSERT(vm_phys_domain(m) == domain,
7967a469c8eSJeff Roberson 	    ("vm_reserv_alloc_contig: Page domain does not match requested."));
79764f096eeSAlan Cox 
79864f096eeSAlan Cox 	/*
79964f096eeSAlan Cox 	 * The allocated physical pages always begin at a reservation
80064f096eeSAlan Cox 	 * boundary, but they do not always end at a reservation boundary.
80164f096eeSAlan Cox 	 * Initialize every reservation that is completely covered by the
80264f096eeSAlan Cox 	 * allocated physical pages.
80364f096eeSAlan Cox 	 */
804c68c3537SAlan Cox 	m_ret = NULL;
805c68c3537SAlan Cox 	index = VM_RESERV_INDEX(object, pindex);
806c68c3537SAlan Cox 	do {
807c68c3537SAlan Cox 		rv = vm_reserv_from_page(m);
808c68c3537SAlan Cox 		KASSERT(rv->pages == m,
809c68c3537SAlan Cox 		    ("vm_reserv_alloc_contig: reserv %p's pages is corrupted",
810c68c3537SAlan Cox 		    rv));
8115c930c89SJeff Roberson 		vm_reserv_lock(rv);
812e2068d0bSJeff Roberson 		vm_reserv_insert(rv, object, first);
813c68c3537SAlan Cox 		n = ulmin(VM_LEVEL_0_NPAGES - index, npages);
814c68c3537SAlan Cox 		for (i = 0; i < n; i++)
815ec179322SAlan Cox 			vm_reserv_populate(rv, index + i);
816c68c3537SAlan Cox 		npages -= n;
817c68c3537SAlan Cox 		if (m_ret == NULL) {
818c68c3537SAlan Cox 			m_ret = &rv->pages[index];
819c68c3537SAlan Cox 			index = 0;
820c68c3537SAlan Cox 		}
8215c930c89SJeff Roberson 		vm_reserv_unlock(rv);
822c68c3537SAlan Cox 		m += VM_LEVEL_0_NPAGES;
823c68c3537SAlan Cox 		first += VM_LEVEL_0_NPAGES;
824c68c3537SAlan Cox 		allocpages -= VM_LEVEL_0_NPAGES;
82564f096eeSAlan Cox 	} while (allocpages >= VM_LEVEL_0_NPAGES);
826c68c3537SAlan Cox 	return (m_ret);
827e2068d0bSJeff Roberson }
828c68c3537SAlan Cox 
829c68c3537SAlan Cox /*
830e2068d0bSJeff Roberson  * Attempts to extend an existing reservation and allocate the page to the
831e2068d0bSJeff Roberson  * object.
832e2068d0bSJeff Roberson  *
833e2068d0bSJeff Roberson  * The page "mpred" must immediately precede the offset "pindex" within the
834e2068d0bSJeff Roberson  * specified object.
835e2068d0bSJeff Roberson  *
836e2068d0bSJeff Roberson  * The object must be locked.
837c68c3537SAlan Cox  */
838e2068d0bSJeff Roberson vm_page_t
839e2068d0bSJeff Roberson vm_reserv_extend(int req, vm_object_t object, vm_pindex_t pindex, int domain,
840e2068d0bSJeff Roberson     vm_page_t mpred)
841e2068d0bSJeff Roberson {
842e2068d0bSJeff Roberson 	struct vm_domain *vmd;
843e2068d0bSJeff Roberson 	vm_page_t m, msucc;
844e2068d0bSJeff Roberson 	vm_reserv_t rv;
84530fbfddaSJeff Roberson 	int index;
846e2068d0bSJeff Roberson 
847e2068d0bSJeff Roberson 	VM_OBJECT_ASSERT_WLOCKED(object);
848e2068d0bSJeff Roberson 
849e2068d0bSJeff Roberson 	/*
850e2068d0bSJeff Roberson 	 * Could a reservation currently exist?
851e2068d0bSJeff Roberson 	 */
852e2068d0bSJeff Roberson 	if (pindex < VM_RESERV_INDEX(object, pindex) ||
853e2068d0bSJeff Roberson 	    pindex >= object->size || object->resident_page_count == 0)
854e2068d0bSJeff Roberson 		return (NULL);
855e2068d0bSJeff Roberson 
856e2068d0bSJeff Roberson 	/*
857e2068d0bSJeff Roberson 	 * Look for an existing reservation.
858e2068d0bSJeff Roberson 	 */
859e2068d0bSJeff Roberson 	rv = vm_reserv_from_object(object, pindex, mpred, &msucc);
860e2068d0bSJeff Roberson 	if (rv == NULL)
861e2068d0bSJeff Roberson 		return (NULL);
862e2068d0bSJeff Roberson 
863e2068d0bSJeff Roberson 	KASSERT(object != kernel_object || rv->domain == domain,
864e2068d0bSJeff Roberson 	    ("vm_reserv_extend: Domain mismatch from reservation."));
865e2068d0bSJeff Roberson 	domain = rv->domain;
866e2068d0bSJeff Roberson 	vmd = VM_DOMAIN(domain);
867c68c3537SAlan Cox 	index = VM_RESERV_INDEX(object, pindex);
868c68c3537SAlan Cox 	m = &rv->pages[index];
8695c930c89SJeff Roberson 	vm_reserv_lock(rv);
870e2068d0bSJeff Roberson 	/* Handle reclaim race. */
8715c930c89SJeff Roberson 	if (rv->object != object ||
872c68c3537SAlan Cox 	    /* Handle vm_page_rename(m, new_object, ...). */
8735c930c89SJeff Roberson 	    popmap_is_set(rv->popmap, index)) {
874e2068d0bSJeff Roberson 		m = NULL;
8755c930c89SJeff Roberson 		goto out;
87630fbfddaSJeff Roberson 	}
8775c930c89SJeff Roberson 	if (vm_domain_allocate(vmd, req, 1) == 0)
8785c930c89SJeff Roberson 		m = NULL;
8795c930c89SJeff Roberson 	else
8805c930c89SJeff Roberson 		vm_reserv_populate(rv, index);
8815c930c89SJeff Roberson out:
8825c930c89SJeff Roberson 	vm_reserv_unlock(rv);
883e2068d0bSJeff Roberson 
884c68c3537SAlan Cox 	return (m);
885c68c3537SAlan Cox }
886c68c3537SAlan Cox 
887c68c3537SAlan Cox /*
8885f70fb14SMark Johnston  * Attempts to allocate a new reservation for the object, and allocates a
8895f70fb14SMark Johnston  * page from that reservation.  Callers should first invoke vm_reserv_extend()
8905f70fb14SMark Johnston  * to attempt an allocation from an existing reservation.
891f8a47341SAlan Cox  *
892404eb1b3SAlan Cox  * The page "mpred" must immediately precede the offset "pindex" within the
893404eb1b3SAlan Cox  * specified object.
894404eb1b3SAlan Cox  *
895f8a47341SAlan Cox  * The object and free page queue must be locked.
896f8a47341SAlan Cox  */
897f8a47341SAlan Cox vm_page_t
8985c930c89SJeff Roberson vm_reserv_alloc_page(int req, vm_object_t object, vm_pindex_t pindex, int domain,
899ef435ae7SJeff Roberson     vm_page_t mpred)
900f8a47341SAlan Cox {
9015c930c89SJeff Roberson 	struct vm_domain *vmd;
902404eb1b3SAlan Cox 	vm_page_t m, msucc;
903f8a47341SAlan Cox 	vm_pindex_t first, leftcap, rightcap;
904f8a47341SAlan Cox 	vm_reserv_t rv;
905e2068d0bSJeff Roberson 	int index;
906f8a47341SAlan Cox 
90789f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
908f8a47341SAlan Cox 
909f8a47341SAlan Cox 	/*
910c68c3537SAlan Cox 	 * Is a reservation fundamentally impossible?
911f8a47341SAlan Cox 	 */
912f8a47341SAlan Cox 	if (pindex < VM_RESERV_INDEX(object, pindex) ||
913f8a47341SAlan Cox 	    pindex >= object->size)
914f8a47341SAlan Cox 		return (NULL);
915f8a47341SAlan Cox 
916f8a47341SAlan Cox 	/*
917e2068d0bSJeff Roberson 	 * Callers should've extended an existing reservation prior to
918e2068d0bSJeff Roberson 	 * calling this function.  If a reservation exists it is
919e2068d0bSJeff Roberson 	 * incompatible with the allocation.
920f8a47341SAlan Cox 	 */
921e2068d0bSJeff Roberson 	rv = vm_reserv_from_object(object, pindex, mpred, &msucc);
922e2068d0bSJeff Roberson 	if (rv != NULL)
923e2068d0bSJeff Roberson 		return (NULL);
924f8a47341SAlan Cox 
925f8a47341SAlan Cox 	/*
926c68c3537SAlan Cox 	 * Could a reservation fit between the first index to the left that
927c68c3537SAlan Cox 	 * can be used and the first index to the right that cannot be used?
928e2068d0bSJeff Roberson 	 *
929e2068d0bSJeff Roberson 	 * We must synchronize with the reserv object lock to protect the
930e2068d0bSJeff Roberson 	 * pindex/object of the resulting reservations against rename while
931e2068d0bSJeff Roberson 	 * we are inspecting.
932f8a47341SAlan Cox 	 */
933c68c3537SAlan Cox 	first = pindex - VM_RESERV_INDEX(object, pindex);
934e2068d0bSJeff Roberson 	vm_reserv_object_lock(object);
935c68c3537SAlan Cox 	if (mpred != NULL) {
936c68c3537SAlan Cox 		if ((rv = vm_reserv_from_page(mpred))->object != object)
937f8a47341SAlan Cox 			leftcap = mpred->pindex + 1;
938f8a47341SAlan Cox 		else
939f8a47341SAlan Cox 			leftcap = rv->pindex + VM_LEVEL_0_NPAGES;
940e2068d0bSJeff Roberson 		if (leftcap > first) {
941e2068d0bSJeff Roberson 			vm_reserv_object_unlock(object);
942c68c3537SAlan Cox 			return (NULL);
943c68c3537SAlan Cox 		}
944e2068d0bSJeff Roberson 	}
945c68c3537SAlan Cox 	if (msucc != NULL) {
946c68c3537SAlan Cox 		if ((rv = vm_reserv_from_page(msucc))->object != object)
947f8a47341SAlan Cox 			rightcap = msucc->pindex;
948f8a47341SAlan Cox 		else
949f8a47341SAlan Cox 			rightcap = rv->pindex;
950e2068d0bSJeff Roberson 		if (first + VM_LEVEL_0_NPAGES > rightcap) {
951e2068d0bSJeff Roberson 			vm_reserv_object_unlock(object);
952f8a47341SAlan Cox 			return (NULL);
953c68c3537SAlan Cox 		}
954e2068d0bSJeff Roberson 	}
955e2068d0bSJeff Roberson 	vm_reserv_object_unlock(object);
956f8a47341SAlan Cox 
957f8a47341SAlan Cox 	/*
958c68c3537SAlan Cox 	 * Would a new reservation extend past the end of the object?
959f8a47341SAlan Cox 	 */
960c68c3537SAlan Cox 	if (first + VM_LEVEL_0_NPAGES > object->size) {
961f8a47341SAlan Cox 		/*
962f8a47341SAlan Cox 		 * Don't allocate a new reservation if the object is a vnode or
963f8a47341SAlan Cox 		 * backed by another object that is a vnode.
964f8a47341SAlan Cox 		 */
965f8a47341SAlan Cox 		if (object->type == OBJT_VNODE ||
966f8a47341SAlan Cox 		    (object->backing_object != NULL &&
967f8a47341SAlan Cox 		    object->backing_object->type == OBJT_VNODE))
968f8a47341SAlan Cox 			return (NULL);
969f8a47341SAlan Cox 		/* Speculate that the object may grow. */
970f8a47341SAlan Cox 	}
971f8a47341SAlan Cox 
972f8a47341SAlan Cox 	/*
973c68c3537SAlan Cox 	 * Allocate and populate the new reservation.
974f8a47341SAlan Cox 	 */
9755c930c89SJeff Roberson 	m = NULL;
9765c930c89SJeff Roberson 	vmd = VM_DOMAIN(domain);
9775c930c89SJeff Roberson 	if (vm_domain_allocate(vmd, req, 1)) {
9785c930c89SJeff Roberson 		vm_domain_free_lock(vmd);
9795c930c89SJeff Roberson 		m = vm_phys_alloc_pages(domain, VM_FREEPOOL_DEFAULT,
9805c930c89SJeff Roberson 		    VM_LEVEL_0_ORDER);
9815c930c89SJeff Roberson 		vm_domain_free_unlock(vmd);
9825c930c89SJeff Roberson 		if (m == NULL) {
9835c930c89SJeff Roberson 			vm_domain_freecnt_inc(vmd, 1);
9845c930c89SJeff Roberson 			return (NULL);
9855c930c89SJeff Roberson 		}
9865c930c89SJeff Roberson 	} else
987c68c3537SAlan Cox 		return (NULL);
988f8a47341SAlan Cox 	rv = vm_reserv_from_page(m);
9895c930c89SJeff Roberson 	vm_reserv_lock(rv);
990f8a47341SAlan Cox 	KASSERT(rv->pages == m,
991c68c3537SAlan Cox 	    ("vm_reserv_alloc_page: reserv %p's pages is corrupted", rv));
992e2068d0bSJeff Roberson 	vm_reserv_insert(rv, object, first);
993ec179322SAlan Cox 	index = VM_RESERV_INDEX(object, pindex);
994ec179322SAlan Cox 	vm_reserv_populate(rv, index);
9955c930c89SJeff Roberson 	vm_reserv_unlock(rv);
9965c930c89SJeff Roberson 
997ec179322SAlan Cox 	return (&rv->pages[index]);
998f8a47341SAlan Cox }
999f8a47341SAlan Cox 
1000f8a47341SAlan Cox /*
1001ada27a3bSKonstantin Belousov  * Breaks the given reservation.  All free pages in the reservation
1002ada27a3bSKonstantin Belousov  * are returned to the physical memory allocator.  The reservation's
1003ada27a3bSKonstantin Belousov  * population count and map are reset to their initial state.
1004ec179322SAlan Cox  *
10053453bca8SAlan Cox  * The given reservation must not be in the partially populated reservation
1006ec179322SAlan Cox  * queue.  The free page queue lock must be held.
1007ec179322SAlan Cox  */
1008ec179322SAlan Cox static void
1009ada27a3bSKonstantin Belousov vm_reserv_break(vm_reserv_t rv)
1010ec179322SAlan Cox {
1011ec179322SAlan Cox 	int begin_zeroes, hi, i, lo;
1012ec179322SAlan Cox 
10135c930c89SJeff Roberson 	vm_reserv_assert_locked(rv);
10145c930c89SJeff Roberson 	CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d",
10155c930c89SJeff Roberson 	    __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq);
1016e2068d0bSJeff Roberson 	vm_reserv_remove(rv);
1017c4be9169SKonstantin Belousov 	rv->pages->psind = 0;
1018ec179322SAlan Cox 	i = hi = 0;
1019ec179322SAlan Cox 	do {
1020ec179322SAlan Cox 		/* Find the next 0 bit.  Any previous 0 bits are < "hi". */
1021ec179322SAlan Cox 		lo = ffsl(~(((1UL << hi) - 1) | rv->popmap[i]));
1022ec179322SAlan Cox 		if (lo == 0) {
1023ec179322SAlan Cox 			/* Redundantly clears bits < "hi". */
1024ec179322SAlan Cox 			rv->popmap[i] = 0;
1025ec179322SAlan Cox 			rv->popcnt -= NBPOPMAP - hi;
1026ec179322SAlan Cox 			while (++i < NPOPMAP) {
1027ec179322SAlan Cox 				lo = ffsl(~rv->popmap[i]);
1028ec179322SAlan Cox 				if (lo == 0) {
1029ec179322SAlan Cox 					rv->popmap[i] = 0;
1030ec179322SAlan Cox 					rv->popcnt -= NBPOPMAP;
1031ec179322SAlan Cox 				} else
1032ec179322SAlan Cox 					break;
1033ec179322SAlan Cox 			}
1034ec179322SAlan Cox 			if (i == NPOPMAP)
1035ec179322SAlan Cox 				break;
1036ec179322SAlan Cox 			hi = 0;
1037ec179322SAlan Cox 		}
1038ec179322SAlan Cox 		KASSERT(lo > 0, ("vm_reserv_break: lo is %d", lo));
1039ec179322SAlan Cox 		/* Convert from ffsl() to ordinary bit numbering. */
1040ec179322SAlan Cox 		lo--;
1041ec179322SAlan Cox 		if (lo > 0) {
1042ec179322SAlan Cox 			/* Redundantly clears bits < "hi". */
1043ec179322SAlan Cox 			rv->popmap[i] &= ~((1UL << lo) - 1);
1044ec179322SAlan Cox 			rv->popcnt -= lo - hi;
1045ec179322SAlan Cox 		}
1046ec179322SAlan Cox 		begin_zeroes = NBPOPMAP * i + lo;
1047ec179322SAlan Cox 		/* Find the next 1 bit. */
1048ec179322SAlan Cox 		do
1049ec179322SAlan Cox 			hi = ffsl(rv->popmap[i]);
1050ec179322SAlan Cox 		while (hi == 0 && ++i < NPOPMAP);
1051ec179322SAlan Cox 		if (i != NPOPMAP)
1052ec179322SAlan Cox 			/* Convert from ffsl() to ordinary bit numbering. */
1053ec179322SAlan Cox 			hi--;
10545c930c89SJeff Roberson 		vm_domain_free_lock(VM_DOMAIN(rv->domain));
1055ec179322SAlan Cox 		vm_phys_free_contig(&rv->pages[begin_zeroes], NBPOPMAP * i +
1056ec179322SAlan Cox 		    hi - begin_zeroes);
10575c930c89SJeff Roberson 		vm_domain_free_unlock(VM_DOMAIN(rv->domain));
1058ec179322SAlan Cox 	} while (i < NPOPMAP);
1059ec179322SAlan Cox 	KASSERT(rv->popcnt == 0,
1060ec179322SAlan Cox 	    ("vm_reserv_break: reserv %p's popcnt is corrupted", rv));
10615c930c89SJeff Roberson 	counter_u64_add(vm_reserv_broken, 1);
1062ec179322SAlan Cox }
1063ec179322SAlan Cox 
1064ec179322SAlan Cox /*
1065f8a47341SAlan Cox  * Breaks all reservations belonging to the given object.
1066f8a47341SAlan Cox  */
1067f8a47341SAlan Cox void
1068f8a47341SAlan Cox vm_reserv_break_all(vm_object_t object)
1069f8a47341SAlan Cox {
1070f8a47341SAlan Cox 	vm_reserv_t rv;
1071f8a47341SAlan Cox 
1072e2068d0bSJeff Roberson 	/*
1073e2068d0bSJeff Roberson 	 * This access of object->rvq is unsynchronized so that the
1074e2068d0bSJeff Roberson 	 * object rvq lock can nest after the domain_free lock.  We
1075e2068d0bSJeff Roberson 	 * must check for races in the results.  However, the object
1076e2068d0bSJeff Roberson 	 * lock prevents new additions, so we are guaranteed that when
1077e2068d0bSJeff Roberson 	 * it returns NULL the object is properly empty.
1078e2068d0bSJeff Roberson 	 */
1079f8a47341SAlan Cox 	while ((rv = LIST_FIRST(&object->rvq)) != NULL) {
10805c930c89SJeff Roberson 		vm_reserv_lock(rv);
1081e2068d0bSJeff Roberson 		/* Reclaim race. */
10825c930c89SJeff Roberson 		if (rv->object != object) {
10835c930c89SJeff Roberson 			vm_reserv_unlock(rv);
1084e2068d0bSJeff Roberson 			continue;
10855c930c89SJeff Roberson 		}
10865c930c89SJeff Roberson 		vm_reserv_domain_lock(rv->domain);
1087f8a47341SAlan Cox 		if (rv->inpartpopq) {
1088ef435ae7SJeff Roberson 			TAILQ_REMOVE(&vm_rvq_partpop[rv->domain], rv, partpopq);
1089f8a47341SAlan Cox 			rv->inpartpopq = FALSE;
1090f8a47341SAlan Cox 		}
10915c930c89SJeff Roberson 		vm_reserv_domain_unlock(rv->domain);
1092ada27a3bSKonstantin Belousov 		vm_reserv_break(rv);
10935c930c89SJeff Roberson 		vm_reserv_unlock(rv);
1094f8a47341SAlan Cox 	}
1095f8a47341SAlan Cox }
1096f8a47341SAlan Cox 
1097f8a47341SAlan Cox /*
1098f8a47341SAlan Cox  * Frees the given page if it belongs to a reservation.  Returns TRUE if the
1099f8a47341SAlan Cox  * page is freed and FALSE otherwise.
1100f8a47341SAlan Cox  *
1101f8a47341SAlan Cox  * The free page queue lock must be held.
1102f8a47341SAlan Cox  */
1103f8a47341SAlan Cox boolean_t
1104f8a47341SAlan Cox vm_reserv_free_page(vm_page_t m)
1105f8a47341SAlan Cox {
1106f8a47341SAlan Cox 	vm_reserv_t rv;
11075c930c89SJeff Roberson 	boolean_t ret;
1108f8a47341SAlan Cox 
1109f8a47341SAlan Cox 	rv = vm_reserv_from_page(m);
1110908e3da1SAlan Cox 	if (rv->object == NULL)
1111908e3da1SAlan Cox 		return (FALSE);
11125c930c89SJeff Roberson 	vm_reserv_lock(rv);
11135c930c89SJeff Roberson 	/* Re-validate after lock. */
11145c930c89SJeff Roberson 	if (rv->object != NULL) {
1115ec179322SAlan Cox 		vm_reserv_depopulate(rv, m - rv->pages);
11165c930c89SJeff Roberson 		ret = TRUE;
11175c930c89SJeff Roberson 	} else
11185c930c89SJeff Roberson 		ret = FALSE;
11195c930c89SJeff Roberson 	vm_reserv_unlock(rv);
11205c930c89SJeff Roberson 
11215c930c89SJeff Roberson 	return (ret);
1122f8a47341SAlan Cox }
1123f8a47341SAlan Cox 
1124f8a47341SAlan Cox /*
1125f8a47341SAlan Cox  * Initializes the reservation management system.  Specifically, initializes
1126f8a47341SAlan Cox  * the reservation array.
1127f8a47341SAlan Cox  *
1128f8a47341SAlan Cox  * Requires that vm_page_array and first_page are initialized!
1129f8a47341SAlan Cox  */
1130f8a47341SAlan Cox void
1131f8a47341SAlan Cox vm_reserv_init(void)
1132f8a47341SAlan Cox {
1133f8a47341SAlan Cox 	vm_paddr_t paddr;
113409e5f3c4SAlan Cox 	struct vm_phys_seg *seg;
11355c930c89SJeff Roberson 	struct vm_reserv *rv;
1136ef435ae7SJeff Roberson 	int i, segind;
1137f8a47341SAlan Cox 
1138f8a47341SAlan Cox 	/*
1139f8a47341SAlan Cox 	 * Initialize the reservation array.  Specifically, initialize the
1140f8a47341SAlan Cox 	 * "pages" field for every element that has an underlying superpage.
1141f8a47341SAlan Cox 	 */
114209e5f3c4SAlan Cox 	for (segind = 0; segind < vm_phys_nsegs; segind++) {
114309e5f3c4SAlan Cox 		seg = &vm_phys_segs[segind];
114409e5f3c4SAlan Cox 		paddr = roundup2(seg->start, VM_LEVEL_0_SIZE);
114509e5f3c4SAlan Cox 		while (paddr + VM_LEVEL_0_SIZE <= seg->end) {
11465c930c89SJeff Roberson 			rv = &vm_reserv_array[paddr >> VM_LEVEL_0_SHIFT];
11475c930c89SJeff Roberson 			rv->pages = PHYS_TO_VM_PAGE(paddr);
11485c930c89SJeff Roberson 			rv->domain = seg->domain;
11495c930c89SJeff Roberson 			mtx_init(&rv->lock, "vm reserv", NULL, MTX_DEF);
1150f8a47341SAlan Cox 			paddr += VM_LEVEL_0_SIZE;
1151f8a47341SAlan Cox 		}
1152f8a47341SAlan Cox 	}
11535c930c89SJeff Roberson 	for (i = 0; i < MAXMEMDOM; i++) {
11545c930c89SJeff Roberson 		mtx_init(&vm_reserv_domain_locks[i], "VM reserv domain", NULL,
11555c930c89SJeff Roberson 		    MTX_DEF);
1156ef435ae7SJeff Roberson 		TAILQ_INIT(&vm_rvq_partpop[i]);
1157f8a47341SAlan Cox 	}
1158f8a47341SAlan Cox 
11595c930c89SJeff Roberson 	for (i = 0; i < VM_RESERV_OBJ_LOCK_COUNT; i++)
11605c930c89SJeff Roberson 		mtx_init(&vm_reserv_object_mtx[i], "resv obj lock", NULL,
11615c930c89SJeff Roberson 		    MTX_DEF);
11625c930c89SJeff Roberson }
11635c930c89SJeff Roberson 
1164f8a47341SAlan Cox /*
1165c869e672SAlan Cox  * Returns true if the given page belongs to a reservation and that page is
1166c869e672SAlan Cox  * free.  Otherwise, returns false.
1167c869e672SAlan Cox  */
1168c869e672SAlan Cox bool
1169c869e672SAlan Cox vm_reserv_is_page_free(vm_page_t m)
1170c869e672SAlan Cox {
1171c869e672SAlan Cox 	vm_reserv_t rv;
1172c869e672SAlan Cox 
1173c869e672SAlan Cox 	rv = vm_reserv_from_page(m);
1174c869e672SAlan Cox 	if (rv->object == NULL)
1175c869e672SAlan Cox 		return (false);
1176c869e672SAlan Cox 	return (popmap_is_clear(rv->popmap, m - rv->pages));
1177c869e672SAlan Cox }
1178c869e672SAlan Cox 
1179c869e672SAlan Cox /*
1180c869e672SAlan Cox  * If the given page belongs to a reservation, returns the level of that
1181c869e672SAlan Cox  * reservation.  Otherwise, returns -1.
1182c869e672SAlan Cox  */
1183c869e672SAlan Cox int
1184c869e672SAlan Cox vm_reserv_level(vm_page_t m)
1185c869e672SAlan Cox {
1186c869e672SAlan Cox 	vm_reserv_t rv;
1187c869e672SAlan Cox 
1188c869e672SAlan Cox 	rv = vm_reserv_from_page(m);
1189c869e672SAlan Cox 	return (rv->object != NULL ? 0 : -1);
1190c869e672SAlan Cox }
1191c869e672SAlan Cox 
1192c869e672SAlan Cox /*
11933453bca8SAlan Cox  * Returns a reservation level if the given page belongs to a fully populated
1194f8a47341SAlan Cox  * reservation and -1 otherwise.
1195f8a47341SAlan Cox  */
1196f8a47341SAlan Cox int
1197f8a47341SAlan Cox vm_reserv_level_iffullpop(vm_page_t m)
1198f8a47341SAlan Cox {
1199f8a47341SAlan Cox 	vm_reserv_t rv;
1200f8a47341SAlan Cox 
1201f8a47341SAlan Cox 	rv = vm_reserv_from_page(m);
1202f8a47341SAlan Cox 	return (rv->popcnt == VM_LEVEL_0_NPAGES ? 0 : -1);
1203f8a47341SAlan Cox }
1204f8a47341SAlan Cox 
1205f8a47341SAlan Cox /*
12063453bca8SAlan Cox  * Breaks the given partially populated reservation, releasing its free pages
12073453bca8SAlan Cox  * to the physical memory allocator.
1208f8a47341SAlan Cox  *
1209f8a47341SAlan Cox  * The free page queue lock must be held.
1210f8a47341SAlan Cox  */
121144aab2c3SAlan Cox static void
121244aab2c3SAlan Cox vm_reserv_reclaim(vm_reserv_t rv)
1213f8a47341SAlan Cox {
1214f8a47341SAlan Cox 
12155c930c89SJeff Roberson 	vm_reserv_assert_locked(rv);
12165c930c89SJeff Roberson 	CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d",
12175c930c89SJeff Roberson 	    __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq);
12185c930c89SJeff Roberson 	vm_reserv_domain_lock(rv->domain);
1219f8a47341SAlan Cox 	KASSERT(rv->inpartpopq,
1220ec179322SAlan Cox 	    ("vm_reserv_reclaim: reserv %p's inpartpopq is FALSE", rv));
1221*2d3f4181SJeff Roberson 	KASSERT(rv->domain < vm_ndomains,
1222ef435ae7SJeff Roberson 	    ("vm_reserv_reclaim: reserv %p's domain is corrupted %d",
1223ef435ae7SJeff Roberson 	    rv, rv->domain));
1224ef435ae7SJeff Roberson 	TAILQ_REMOVE(&vm_rvq_partpop[rv->domain], rv, partpopq);
1225f8a47341SAlan Cox 	rv->inpartpopq = FALSE;
12265c930c89SJeff Roberson 	vm_reserv_domain_unlock(rv->domain);
1227ada27a3bSKonstantin Belousov 	vm_reserv_break(rv);
12285c930c89SJeff Roberson 	counter_u64_add(vm_reserv_reclaimed, 1);
122944aab2c3SAlan Cox }
123044aab2c3SAlan Cox 
123144aab2c3SAlan Cox /*
12323453bca8SAlan Cox  * Breaks the reservation at the head of the partially populated reservation
12333453bca8SAlan Cox  * queue, releasing its free pages to the physical memory allocator.  Returns
12343453bca8SAlan Cox  * TRUE if a reservation is broken and FALSE otherwise.
123544aab2c3SAlan Cox  *
123644aab2c3SAlan Cox  * The free page queue lock must be held.
123744aab2c3SAlan Cox  */
123844aab2c3SAlan Cox boolean_t
1239ef435ae7SJeff Roberson vm_reserv_reclaim_inactive(int domain)
124044aab2c3SAlan Cox {
124144aab2c3SAlan Cox 	vm_reserv_t rv;
124244aab2c3SAlan Cox 
12435c930c89SJeff Roberson 	while ((rv = TAILQ_FIRST(&vm_rvq_partpop[domain])) != NULL) {
12445c930c89SJeff Roberson 		vm_reserv_lock(rv);
12455c930c89SJeff Roberson 		if (rv != TAILQ_FIRST(&vm_rvq_partpop[domain])) {
12465c930c89SJeff Roberson 			vm_reserv_unlock(rv);
12475c930c89SJeff Roberson 			continue;
12485c930c89SJeff Roberson 		}
124944aab2c3SAlan Cox 		vm_reserv_reclaim(rv);
12505c930c89SJeff Roberson 		vm_reserv_unlock(rv);
1251f8a47341SAlan Cox 		return (TRUE);
1252f8a47341SAlan Cox 	}
1253f8a47341SAlan Cox 	return (FALSE);
1254f8a47341SAlan Cox }
1255f8a47341SAlan Cox 
1256f8a47341SAlan Cox /*
12573453bca8SAlan Cox  * Searches the partially populated reservation queue for the least recently
12583453bca8SAlan Cox  * changed reservation with free pages that satisfy the given request for
12593453bca8SAlan Cox  * contiguous physical memory.  If a satisfactory reservation is found, it is
12603453bca8SAlan Cox  * broken.  Returns TRUE if a reservation is broken and FALSE otherwise.
126144aab2c3SAlan Cox  *
126244aab2c3SAlan Cox  * The free page queue lock must be held.
126344aab2c3SAlan Cox  */
126444aab2c3SAlan Cox boolean_t
1265ef435ae7SJeff Roberson vm_reserv_reclaim_contig(int domain, u_long npages, vm_paddr_t low,
1266ef435ae7SJeff Roberson     vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
126744aab2c3SAlan Cox {
1268ec179322SAlan Cox 	vm_paddr_t pa, size;
12695c930c89SJeff Roberson 	vm_reserv_t rv, rvn;
127067b7e434SAlan Cox 	int hi, i, lo, low_index, next_free;
127144aab2c3SAlan Cox 
1272c68c3537SAlan Cox 	if (npages > VM_LEVEL_0_NPAGES - 1)
127344aab2c3SAlan Cox 		return (FALSE);
1274c68c3537SAlan Cox 	size = npages << PAGE_SHIFT;
12755c930c89SJeff Roberson 	vm_reserv_domain_lock(domain);
12765c930c89SJeff Roberson again:
12775c930c89SJeff Roberson 	for (rv = TAILQ_FIRST(&vm_rvq_partpop[domain]); rv != NULL; rv = rvn) {
12785c930c89SJeff Roberson 		rvn = TAILQ_NEXT(rv, partpopq);
127944aab2c3SAlan Cox 		pa = VM_PAGE_TO_PHYS(&rv->pages[VM_LEVEL_0_NPAGES - 1]);
128044aab2c3SAlan Cox 		if (pa + PAGE_SIZE - size < low) {
1281ec179322SAlan Cox 			/* This entire reservation is too low; go to next. */
128244aab2c3SAlan Cox 			continue;
128344aab2c3SAlan Cox 		}
1284ec179322SAlan Cox 		pa = VM_PAGE_TO_PHYS(&rv->pages[0]);
128544aab2c3SAlan Cox 		if (pa + size > high) {
1286ec179322SAlan Cox 			/* This entire reservation is too high; go to next. */
1287ec179322SAlan Cox 			continue;
128885f2a0c9SMax Laier 		}
12895c930c89SJeff Roberson 		if (vm_reserv_trylock(rv) == 0) {
12905c930c89SJeff Roberson 			vm_reserv_domain_unlock(domain);
12915c930c89SJeff Roberson 			vm_reserv_lock(rv);
12925c930c89SJeff Roberson 			if (!rv->inpartpopq) {
12935c930c89SJeff Roberson 				vm_reserv_domain_lock(domain);
12945c930c89SJeff Roberson 				if (!rvn->inpartpopq)
12955c930c89SJeff Roberson 					goto again;
12965c930c89SJeff Roberson 				continue;
12975c930c89SJeff Roberson 			}
12985c930c89SJeff Roberson 		} else
12995c930c89SJeff Roberson 			vm_reserv_domain_unlock(domain);
1300ec179322SAlan Cox 		if (pa < low) {
1301ec179322SAlan Cox 			/* Start the search for free pages at "low". */
130267b7e434SAlan Cox 			low_index = (low + PAGE_MASK - pa) >> PAGE_SHIFT;
130367b7e434SAlan Cox 			i = low_index / NBPOPMAP;
130467b7e434SAlan Cox 			hi = low_index % NBPOPMAP;
1305ec179322SAlan Cox 		} else
1306ec179322SAlan Cox 			i = hi = 0;
1307ec179322SAlan Cox 		do {
1308ec179322SAlan Cox 			/* Find the next free page. */
1309ec179322SAlan Cox 			lo = ffsl(~(((1UL << hi) - 1) | rv->popmap[i]));
1310ec179322SAlan Cox 			while (lo == 0 && ++i < NPOPMAP)
1311ec179322SAlan Cox 				lo = ffsl(~rv->popmap[i]);
1312ec179322SAlan Cox 			if (i == NPOPMAP)
1313ec179322SAlan Cox 				break;
1314ec179322SAlan Cox 			/* Convert from ffsl() to ordinary bit numbering. */
1315ec179322SAlan Cox 			lo--;
1316ec179322SAlan Cox 			next_free = NBPOPMAP * i + lo;
1317ec179322SAlan Cox 			pa = VM_PAGE_TO_PHYS(&rv->pages[next_free]);
1318ec179322SAlan Cox 			KASSERT(pa >= low,
1319ec179322SAlan Cox 			    ("vm_reserv_reclaim_contig: pa is too low"));
1320ec179322SAlan Cox 			if (pa + size > high) {
1321ec179322SAlan Cox 				/* The rest of this reservation is too high. */
1322ec179322SAlan Cox 				break;
1323ec179322SAlan Cox 			} else if ((pa & (alignment - 1)) != 0 ||
1324ec179322SAlan Cox 			    ((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0) {
13256a93e36bSAlan Cox 				/*
13266a93e36bSAlan Cox 				 * The current page doesn't meet the alignment
13276a93e36bSAlan Cox 				 * and/or boundary requirements.  Continue
13286a93e36bSAlan Cox 				 * searching this reservation until the rest
13296a93e36bSAlan Cox 				 * of its free pages are either excluded or
13306a93e36bSAlan Cox 				 * exhausted.
13316a93e36bSAlan Cox 				 */
13326a93e36bSAlan Cox 				hi = lo + 1;
13336a93e36bSAlan Cox 				if (hi >= NBPOPMAP) {
13346a93e36bSAlan Cox 					hi = 0;
13356a93e36bSAlan Cox 					i++;
13366a93e36bSAlan Cox 				}
1337ec179322SAlan Cox 				continue;
1338ec179322SAlan Cox 			}
1339ec179322SAlan Cox 			/* Find the next used page. */
1340ec179322SAlan Cox 			hi = ffsl(rv->popmap[i] & ~((1UL << lo) - 1));
1341ec179322SAlan Cox 			while (hi == 0 && ++i < NPOPMAP) {
1342ec179322SAlan Cox 				if ((NBPOPMAP * i - next_free) * PAGE_SIZE >=
1343ec179322SAlan Cox 				    size) {
134444aab2c3SAlan Cox 					vm_reserv_reclaim(rv);
13455c930c89SJeff Roberson 					vm_reserv_unlock(rv);
134644aab2c3SAlan Cox 					return (TRUE);
134744aab2c3SAlan Cox 				}
1348ec179322SAlan Cox 				hi = ffsl(rv->popmap[i]);
1349ec179322SAlan Cox 			}
1350ec179322SAlan Cox 			/* Convert from ffsl() to ordinary bit numbering. */
1351ec179322SAlan Cox 			if (i != NPOPMAP)
1352ec179322SAlan Cox 				hi--;
1353ec179322SAlan Cox 			if ((NBPOPMAP * i + hi - next_free) * PAGE_SIZE >=
1354ec179322SAlan Cox 			    size) {
1355ec179322SAlan Cox 				vm_reserv_reclaim(rv);
13565c930c89SJeff Roberson 				vm_reserv_unlock(rv);
1357ec179322SAlan Cox 				return (TRUE);
1358ec179322SAlan Cox 			}
1359ec179322SAlan Cox 		} while (i < NPOPMAP);
13605c930c89SJeff Roberson 		vm_reserv_unlock(rv);
13615c930c89SJeff Roberson 		vm_reserv_domain_lock(domain);
13625c930c89SJeff Roberson 		if (rvn != NULL && !rvn->inpartpopq)
13635c930c89SJeff Roberson 			goto again;
136444aab2c3SAlan Cox 	}
13655c930c89SJeff Roberson 	vm_reserv_domain_unlock(domain);
136644aab2c3SAlan Cox 	return (FALSE);
136744aab2c3SAlan Cox }
136844aab2c3SAlan Cox 
136944aab2c3SAlan Cox /*
1370f8a47341SAlan Cox  * Transfers the reservation underlying the given page to a new object.
1371f8a47341SAlan Cox  *
1372f8a47341SAlan Cox  * The object must be locked.
1373f8a47341SAlan Cox  */
1374f8a47341SAlan Cox void
1375f8a47341SAlan Cox vm_reserv_rename(vm_page_t m, vm_object_t new_object, vm_object_t old_object,
1376f8a47341SAlan Cox     vm_pindex_t old_object_offset)
1377f8a47341SAlan Cox {
1378f8a47341SAlan Cox 	vm_reserv_t rv;
1379f8a47341SAlan Cox 
138089f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(new_object);
1381f8a47341SAlan Cox 	rv = vm_reserv_from_page(m);
1382f8a47341SAlan Cox 	if (rv->object == old_object) {
13835c930c89SJeff Roberson 		vm_reserv_lock(rv);
13845c930c89SJeff Roberson 		CTR6(KTR_VM,
13855c930c89SJeff Roberson 		    "%s: rv %p object %p new %p popcnt %d inpartpop %d",
13865c930c89SJeff Roberson 		    __FUNCTION__, rv, rv->object, new_object, rv->popcnt,
13875c930c89SJeff Roberson 		    rv->inpartpopq);
1388f8a47341SAlan Cox 		if (rv->object == old_object) {
1389e2068d0bSJeff Roberson 			vm_reserv_object_lock(old_object);
1390e2068d0bSJeff Roberson 			rv->object = NULL;
1391f8a47341SAlan Cox 			LIST_REMOVE(rv, objq);
1392e2068d0bSJeff Roberson 			vm_reserv_object_unlock(old_object);
1393e2068d0bSJeff Roberson 			vm_reserv_object_lock(new_object);
1394f8a47341SAlan Cox 			rv->object = new_object;
1395f8a47341SAlan Cox 			rv->pindex -= old_object_offset;
1396e2068d0bSJeff Roberson 			LIST_INSERT_HEAD(&new_object->rvq, rv, objq);
1397e2068d0bSJeff Roberson 			vm_reserv_object_unlock(new_object);
1398f8a47341SAlan Cox 		}
13995c930c89SJeff Roberson 		vm_reserv_unlock(rv);
1400f8a47341SAlan Cox 	}
1401f8a47341SAlan Cox }
1402f8a47341SAlan Cox 
1403f8a47341SAlan Cox /*
1404c869e672SAlan Cox  * Returns the size (in bytes) of a reservation of the specified level.
1405c869e672SAlan Cox  */
1406c869e672SAlan Cox int
1407c869e672SAlan Cox vm_reserv_size(int level)
1408c869e672SAlan Cox {
1409c869e672SAlan Cox 
1410c869e672SAlan Cox 	switch (level) {
1411c869e672SAlan Cox 	case 0:
1412c869e672SAlan Cox 		return (VM_LEVEL_0_SIZE);
1413c869e672SAlan Cox 	case -1:
1414c869e672SAlan Cox 		return (PAGE_SIZE);
1415c869e672SAlan Cox 	default:
1416c869e672SAlan Cox 		return (0);
1417c869e672SAlan Cox 	}
1418c869e672SAlan Cox }
1419c869e672SAlan Cox 
1420c869e672SAlan Cox /*
1421f8a47341SAlan Cox  * Allocates the virtual and physical memory required by the reservation
1422f8a47341SAlan Cox  * management system's data structures, in particular, the reservation array.
1423f8a47341SAlan Cox  */
1424f8a47341SAlan Cox vm_paddr_t
1425f8a47341SAlan Cox vm_reserv_startup(vm_offset_t *vaddr, vm_paddr_t end, vm_paddr_t high_water)
1426f8a47341SAlan Cox {
1427f8a47341SAlan Cox 	vm_paddr_t new_end;
1428f8a47341SAlan Cox 	size_t size;
1429f8a47341SAlan Cox 
1430f8a47341SAlan Cox 	/*
1431f8a47341SAlan Cox 	 * Calculate the size (in bytes) of the reservation array.  Round up
1432f8a47341SAlan Cox 	 * from "high_water" because every small page is mapped to an element
1433f8a47341SAlan Cox 	 * in the reservation array based on its physical address.  Thus, the
1434f8a47341SAlan Cox 	 * number of elements in the reservation array can be greater than the
1435f8a47341SAlan Cox 	 * number of superpages.
1436f8a47341SAlan Cox 	 */
1437f8a47341SAlan Cox 	size = howmany(high_water, VM_LEVEL_0_SIZE) * sizeof(struct vm_reserv);
1438f8a47341SAlan Cox 
1439f8a47341SAlan Cox 	/*
1440f8a47341SAlan Cox 	 * Allocate and map the physical memory for the reservation array.  The
1441f8a47341SAlan Cox 	 * next available virtual address is returned by reference.
1442f8a47341SAlan Cox 	 */
1443f8a47341SAlan Cox 	new_end = end - round_page(size);
1444f8a47341SAlan Cox 	vm_reserv_array = (void *)(uintptr_t)pmap_map(vaddr, new_end, end,
1445f8a47341SAlan Cox 	    VM_PROT_READ | VM_PROT_WRITE);
1446f8a47341SAlan Cox 	bzero(vm_reserv_array, size);
1447f8a47341SAlan Cox 
1448f8a47341SAlan Cox 	/*
1449f8a47341SAlan Cox 	 * Return the next available physical address.
1450f8a47341SAlan Cox 	 */
1451f8a47341SAlan Cox 	return (new_end);
1452f8a47341SAlan Cox }
1453f8a47341SAlan Cox 
14548b5e1472SAlan Cox /*
14555c930c89SJeff Roberson  * Initializes the reservation management system.  Specifically, initializes
14565c930c89SJeff Roberson  * the reservation counters.
14575c930c89SJeff Roberson  */
14585c930c89SJeff Roberson static void
14595c930c89SJeff Roberson vm_reserv_counter_init(void *unused)
14605c930c89SJeff Roberson {
14615c930c89SJeff Roberson 
14625c930c89SJeff Roberson 	vm_reserv_freed = counter_u64_alloc(M_WAITOK);
14635c930c89SJeff Roberson 	vm_reserv_broken = counter_u64_alloc(M_WAITOK);
14645c930c89SJeff Roberson 	vm_reserv_reclaimed = counter_u64_alloc(M_WAITOK);
14655c930c89SJeff Roberson }
14665c930c89SJeff Roberson SYSINIT(vm_reserv_counter_init, SI_SUB_CPU, SI_ORDER_ANY,
14675c930c89SJeff Roberson     vm_reserv_counter_init, NULL);
14685c930c89SJeff Roberson 
14695c930c89SJeff Roberson /*
14708b5e1472SAlan Cox  * Returns the superpage containing the given page.
14718b5e1472SAlan Cox  */
14728b5e1472SAlan Cox vm_page_t
14738b5e1472SAlan Cox vm_reserv_to_superpage(vm_page_t m)
14748b5e1472SAlan Cox {
14758b5e1472SAlan Cox 	vm_reserv_t rv;
14768b5e1472SAlan Cox 
14778b5e1472SAlan Cox 	VM_OBJECT_ASSERT_LOCKED(m->object);
14788b5e1472SAlan Cox 	rv = vm_reserv_from_page(m);
14795c930c89SJeff Roberson 	if (rv->object == m->object && rv->popcnt == VM_LEVEL_0_NPAGES)
14805c930c89SJeff Roberson 		m = rv->pages;
14815c930c89SJeff Roberson 	else
14825c930c89SJeff Roberson 		m = NULL;
14835c930c89SJeff Roberson 
14845c930c89SJeff Roberson 	return (m);
14858b5e1472SAlan Cox }
14868b5e1472SAlan Cox 
1487f8a47341SAlan Cox #endif	/* VM_NRESERVLEVEL > 0 */
1488