xref: /freebsd/sys/vm/vm_reserv.c (revision d869a17e6262d087df9934ff8731b9476a4097e1)
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 
80f2a496d6SKonstantin Belousov #ifndef VM_LEVEL_0_ORDER_MAX
81f2a496d6SKonstantin Belousov #define	VM_LEVEL_0_ORDER_MAX	VM_LEVEL_0_ORDER
82f2a496d6SKonstantin Belousov #endif
83f2a496d6SKonstantin Belousov 
84f8a47341SAlan Cox /*
85f8a47341SAlan Cox  * The number of small pages that are contained in a level 0 reservation
86f8a47341SAlan Cox  */
87f8a47341SAlan Cox #define	VM_LEVEL_0_NPAGES	(1 << VM_LEVEL_0_ORDER)
88f2a496d6SKonstantin Belousov #define	VM_LEVEL_0_NPAGES_MAX	(1 << VM_LEVEL_0_ORDER_MAX)
89f8a47341SAlan Cox 
90f8a47341SAlan Cox /*
91f8a47341SAlan Cox  * The number of bits by which a physical address is shifted to obtain the
92f8a47341SAlan Cox  * reservation number
93f8a47341SAlan Cox  */
94f8a47341SAlan Cox #define	VM_LEVEL_0_SHIFT	(VM_LEVEL_0_ORDER + PAGE_SHIFT)
95f8a47341SAlan Cox 
96f8a47341SAlan Cox /*
97f8a47341SAlan Cox  * The size of a level 0 reservation in bytes
98f8a47341SAlan Cox  */
99f8a47341SAlan Cox #define	VM_LEVEL_0_SIZE		(1 << VM_LEVEL_0_SHIFT)
100f8a47341SAlan Cox 
101f8a47341SAlan Cox /*
102f8a47341SAlan Cox  * Computes the index of the small page underlying the given (object, pindex)
103f8a47341SAlan Cox  * within the reservation's array of small pages.
104f8a47341SAlan Cox  */
105f8a47341SAlan Cox #define	VM_RESERV_INDEX(object, pindex)	\
106f8a47341SAlan Cox     (((object)->pg_color + (pindex)) & (VM_LEVEL_0_NPAGES - 1))
107f8a47341SAlan Cox 
108f8a47341SAlan Cox /*
109ec179322SAlan Cox  * The size of a population map entry
110ec179322SAlan Cox  */
111ec179322SAlan Cox typedef	u_long		popmap_t;
112ec179322SAlan Cox 
113ec179322SAlan Cox /*
114ec179322SAlan Cox  * The number of bits in a population map entry
115ec179322SAlan Cox  */
116ec179322SAlan Cox #define	NBPOPMAP	(NBBY * sizeof(popmap_t))
117ec179322SAlan Cox 
118ec179322SAlan Cox /*
119ec179322SAlan Cox  * The number of population map entries in a reservation
120ec179322SAlan Cox  */
121ec179322SAlan Cox #define	NPOPMAP		howmany(VM_LEVEL_0_NPAGES, NBPOPMAP)
122f2a496d6SKonstantin Belousov #define	NPOPMAP_MAX	howmany(VM_LEVEL_0_NPAGES_MAX, NBPOPMAP)
123ec179322SAlan Cox 
124ec179322SAlan Cox /*
1252ef6727eSJeff Roberson  * Number of elapsed ticks before we update the LRU queue position.  Used
1262ef6727eSJeff Roberson  * to reduce contention and churn on the list.
1272ef6727eSJeff Roberson  */
1282ef6727eSJeff Roberson #define	PARTPOPSLOP	1
1292ef6727eSJeff Roberson 
1302ef6727eSJeff Roberson /*
1313180f757SAlan Cox  * Clear a bit in the population map.
1323180f757SAlan Cox  */
1333180f757SAlan Cox static __inline void
1343180f757SAlan Cox popmap_clear(popmap_t popmap[], int i)
1353180f757SAlan Cox {
1363180f757SAlan Cox 
1373180f757SAlan Cox 	popmap[i / NBPOPMAP] &= ~(1UL << (i % NBPOPMAP));
1383180f757SAlan Cox }
1393180f757SAlan Cox 
1403180f757SAlan Cox /*
1413180f757SAlan Cox  * Set a bit in the population map.
1423180f757SAlan Cox  */
1433180f757SAlan Cox static __inline void
1443180f757SAlan Cox popmap_set(popmap_t popmap[], int i)
1453180f757SAlan Cox {
1463180f757SAlan Cox 
1473180f757SAlan Cox 	popmap[i / NBPOPMAP] |= 1UL << (i % NBPOPMAP);
1483180f757SAlan Cox }
1493180f757SAlan Cox 
1503180f757SAlan Cox /*
1513180f757SAlan Cox  * Is a bit in the population map clear?
1523180f757SAlan Cox  */
1533180f757SAlan Cox static __inline boolean_t
1543180f757SAlan Cox popmap_is_clear(popmap_t popmap[], int i)
1553180f757SAlan Cox {
1563180f757SAlan Cox 
1573180f757SAlan Cox 	return ((popmap[i / NBPOPMAP] & (1UL << (i % NBPOPMAP))) == 0);
1583180f757SAlan Cox }
1593180f757SAlan Cox 
1603180f757SAlan Cox /*
1613180f757SAlan Cox  * Is a bit in the population map set?
1623180f757SAlan Cox  */
1633180f757SAlan Cox static __inline boolean_t
1643180f757SAlan Cox popmap_is_set(popmap_t popmap[], int i)
1653180f757SAlan Cox {
1663180f757SAlan Cox 
1673180f757SAlan Cox 	return ((popmap[i / NBPOPMAP] & (1UL << (i % NBPOPMAP))) != 0);
1683180f757SAlan Cox }
1693180f757SAlan Cox 
1703180f757SAlan Cox /*
171f8a47341SAlan Cox  * The reservation structure
172f8a47341SAlan Cox  *
173f8a47341SAlan Cox  * A reservation structure is constructed whenever a large physical page is
174f8a47341SAlan Cox  * speculatively allocated to an object.  The reservation provides the small
175f8a47341SAlan Cox  * physical pages for the range [pindex, pindex + VM_LEVEL_0_NPAGES) of offsets
176f8a47341SAlan Cox  * within that object.  The reservation's "popcnt" tracks the number of these
177f8a47341SAlan Cox  * small physical pages that are in use at any given time.  When and if the
1783453bca8SAlan Cox  * reservation is not fully utilized, it appears in the queue of partially
179f8a47341SAlan Cox  * populated reservations.  The reservation always appears on the containing
180f8a47341SAlan Cox  * object's list of reservations.
181f8a47341SAlan Cox  *
1823453bca8SAlan Cox  * A partially populated reservation can be broken and reclaimed at any time.
183e2068d0bSJeff Roberson  *
184b378d296SMark Johnston  * c - constant after boot
1855c930c89SJeff Roberson  * d - vm_reserv_domain_lock
186e2068d0bSJeff Roberson  * o - vm_reserv_object_lock
187b378d296SMark Johnston  * r - vm_reserv_lock
188b378d296SMark Johnston  * s - vm_reserv_domain_scan_lock
189f8a47341SAlan Cox  */
190f8a47341SAlan Cox struct vm_reserv {
1915c930c89SJeff Roberson 	struct mtx	lock;			/* reservation lock. */
192fe6d5344SMark Johnston 	TAILQ_ENTRY(vm_reserv) partpopq;	/* (d, r) per-domain queue. */
1935c930c89SJeff Roberson 	LIST_ENTRY(vm_reserv) objq;		/* (o, r) object queue */
1945c930c89SJeff Roberson 	vm_object_t	object;			/* (o, r) containing object */
1955c930c89SJeff Roberson 	vm_pindex_t	pindex;			/* (o, r) offset in object */
196e2068d0bSJeff Roberson 	vm_page_t	pages;			/* (c) first page  */
1975c930c89SJeff Roberson 	uint16_t	popcnt;			/* (r) # of pages in use */
198fe6d5344SMark Johnston 	uint8_t		domain;			/* (c) NUMA domain. */
199fe6d5344SMark Johnston 	char		inpartpopq;		/* (d, r) */
2002ef6727eSJeff Roberson 	int		lasttick;		/* (r) last pop update tick. */
201f2a496d6SKonstantin Belousov 	popmap_t	popmap[NPOPMAP_MAX];	/* (r) bit vector, used pages */
202f8a47341SAlan Cox };
203f8a47341SAlan Cox 
204b378d296SMark Johnston TAILQ_HEAD(vm_reserv_queue, vm_reserv);
205b378d296SMark Johnston 
2065c930c89SJeff Roberson #define	vm_reserv_lockptr(rv)		(&(rv)->lock)
2075c930c89SJeff Roberson #define	vm_reserv_assert_locked(rv)					\
2085c930c89SJeff Roberson 	    mtx_assert(vm_reserv_lockptr(rv), MA_OWNED)
2095c930c89SJeff Roberson #define	vm_reserv_lock(rv)		mtx_lock(vm_reserv_lockptr(rv))
2105c930c89SJeff Roberson #define	vm_reserv_trylock(rv)		mtx_trylock(vm_reserv_lockptr(rv))
2115c930c89SJeff Roberson #define	vm_reserv_unlock(rv)		mtx_unlock(vm_reserv_lockptr(rv))
2125c930c89SJeff Roberson 
213f8a47341SAlan Cox /*
214f8a47341SAlan Cox  * The reservation array
215f8a47341SAlan Cox  *
216f8a47341SAlan Cox  * This array is analoguous in function to vm_page_array.  It differs in the
217f8a47341SAlan Cox  * respect that it may contain a greater number of useful reservation
218f8a47341SAlan Cox  * structures than there are (physical) superpages.  These "invalid"
219f8a47341SAlan Cox  * reservation structures exist to trade-off space for time in the
220f8a47341SAlan Cox  * implementation of vm_reserv_from_page().  Invalid reservation structures are
221f8a47341SAlan Cox  * distinguishable from "valid" reservation structures by inspecting the
222f8a47341SAlan Cox  * reservation's "pages" field.  Invalid reservation structures have a NULL
223f8a47341SAlan Cox  * "pages" field.
224f8a47341SAlan Cox  *
225f8a47341SAlan Cox  * vm_reserv_from_page() maps a small (physical) page to an element of this
226f8a47341SAlan Cox  * array by computing a physical reservation number from the page's physical
227f8a47341SAlan Cox  * address.  The physical reservation number is used as the array index.
228f8a47341SAlan Cox  *
229f8a47341SAlan Cox  * An "active" reservation is a valid reservation structure that has a non-NULL
230f8a47341SAlan Cox  * "object" field and a non-zero "popcnt" field.  In other words, every active
231f8a47341SAlan Cox  * reservation belongs to a particular object.  Moreover, every active
232f8a47341SAlan Cox  * reservation has an entry in the containing object's list of reservations.
233f8a47341SAlan Cox  */
234f8a47341SAlan Cox static vm_reserv_t vm_reserv_array;
235f8a47341SAlan Cox 
236f8a47341SAlan Cox /*
237fe6d5344SMark Johnston  * The per-domain partially populated reservation queues
238f8a47341SAlan Cox  *
239fe6d5344SMark Johnston  * These queues enable the fast recovery of an unused free small page from a
240fe6d5344SMark Johnston  * partially populated reservation.  The reservation at the head of a queue
2413453bca8SAlan Cox  * is the least recently changed, partially populated reservation.
242f8a47341SAlan Cox  *
243fe6d5344SMark Johnston  * Access to this queue is synchronized by the per-domain reservation lock.
244b378d296SMark Johnston  * Threads reclaiming free pages from the queue must hold the per-domain scan
245b378d296SMark Johnston  * lock.
246f8a47341SAlan Cox  */
247fe6d5344SMark Johnston struct vm_reserv_domain {
248fe6d5344SMark Johnston 	struct mtx 		lock;
249b378d296SMark Johnston 	struct vm_reserv_queue	partpop;	/* (d) */
250b378d296SMark Johnston 	struct vm_reserv	marker;		/* (d, s) scan marker/lock */
251fe6d5344SMark Johnston } __aligned(CACHE_LINE_SIZE);
252fe6d5344SMark Johnston 
253fe6d5344SMark Johnston static struct vm_reserv_domain vm_rvd[MAXMEMDOM];
254fe6d5344SMark Johnston 
255fe6d5344SMark Johnston #define	vm_reserv_domain_lockptr(d)	(&vm_rvd[(d)].lock)
256b378d296SMark Johnston #define	vm_reserv_domain_assert_locked(d)	\
257b378d296SMark Johnston 	mtx_assert(vm_reserv_domain_lockptr(d), MA_OWNED)
258fe6d5344SMark Johnston #define	vm_reserv_domain_lock(d)	mtx_lock(vm_reserv_domain_lockptr(d))
259fe6d5344SMark Johnston #define	vm_reserv_domain_unlock(d)	mtx_unlock(vm_reserv_domain_lockptr(d))
260f8a47341SAlan Cox 
261b378d296SMark Johnston #define	vm_reserv_domain_scan_lock(d)	mtx_lock(&vm_rvd[(d)].marker.lock)
262b378d296SMark Johnston #define	vm_reserv_domain_scan_unlock(d)	mtx_unlock(&vm_rvd[(d)].marker.lock)
263b378d296SMark Johnston 
2647029da5cSPawel Biernacki static SYSCTL_NODE(_vm, OID_AUTO, reserv, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
2657029da5cSPawel Biernacki     "Reservation Info");
266f8a47341SAlan Cox 
267*d869a17eSMark Johnston static COUNTER_U64_DEFINE_EARLY(vm_reserv_broken);
2685c930c89SJeff Roberson SYSCTL_COUNTER_U64(_vm_reserv, OID_AUTO, broken, CTLFLAG_RD,
2695c930c89SJeff Roberson     &vm_reserv_broken, "Cumulative number of broken reservations");
270f8a47341SAlan Cox 
271*d869a17eSMark Johnston static COUNTER_U64_DEFINE_EARLY(vm_reserv_freed);
2725c930c89SJeff Roberson SYSCTL_COUNTER_U64(_vm_reserv, OID_AUTO, freed, CTLFLAG_RD,
2735c930c89SJeff Roberson     &vm_reserv_freed, "Cumulative number of freed reservations");
274f8a47341SAlan Cox 
275e0a63baaSAlan Cox static int sysctl_vm_reserv_fullpop(SYSCTL_HANDLER_ARGS);
276e0a63baaSAlan Cox 
277a314aba8SMateusz Guzik SYSCTL_PROC(_vm_reserv, OID_AUTO, fullpop, CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RD,
278a314aba8SMateusz Guzik     NULL, 0, sysctl_vm_reserv_fullpop, "I", "Current number of full reservations");
279e0a63baaSAlan Cox 
280f8a47341SAlan Cox static int sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS);
281f8a47341SAlan Cox 
2827029da5cSPawel Biernacki SYSCTL_OID(_vm_reserv, OID_AUTO, partpopq,
2837029da5cSPawel Biernacki     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, NULL, 0,
2847029da5cSPawel Biernacki     sysctl_vm_reserv_partpopq, "A",
2857029da5cSPawel Biernacki     "Partially populated reservation queues");
286f8a47341SAlan Cox 
287*d869a17eSMark Johnston static COUNTER_U64_DEFINE_EARLY(vm_reserv_reclaimed);
2885c930c89SJeff Roberson SYSCTL_COUNTER_U64(_vm_reserv, OID_AUTO, reclaimed, CTLFLAG_RD,
2895c930c89SJeff Roberson     &vm_reserv_reclaimed, "Cumulative number of reclaimed reservations");
290f8a47341SAlan Cox 
291e2068d0bSJeff Roberson /*
292e2068d0bSJeff Roberson  * The object lock pool is used to synchronize the rvq.  We can not use a
293e2068d0bSJeff Roberson  * pool mutex because it is required before malloc works.
294e2068d0bSJeff Roberson  *
295e2068d0bSJeff Roberson  * The "hash" function could be made faster without divide and modulo.
296e2068d0bSJeff Roberson  */
297e2068d0bSJeff Roberson #define	VM_RESERV_OBJ_LOCK_COUNT	MAXCPU
298e2068d0bSJeff Roberson 
299e2068d0bSJeff Roberson struct mtx_padalign vm_reserv_object_mtx[VM_RESERV_OBJ_LOCK_COUNT];
300e2068d0bSJeff Roberson 
301e2068d0bSJeff Roberson #define	vm_reserv_object_lock_idx(object)			\
302e2068d0bSJeff Roberson 	    (((uintptr_t)object / sizeof(*object)) % VM_RESERV_OBJ_LOCK_COUNT)
303e2068d0bSJeff Roberson #define	vm_reserv_object_lock_ptr(object)			\
304e2068d0bSJeff Roberson 	    &vm_reserv_object_mtx[vm_reserv_object_lock_idx((object))]
305e2068d0bSJeff Roberson #define	vm_reserv_object_lock(object)				\
306e2068d0bSJeff Roberson 	    mtx_lock(vm_reserv_object_lock_ptr((object)))
307e2068d0bSJeff Roberson #define	vm_reserv_object_unlock(object)				\
308e2068d0bSJeff Roberson 	    mtx_unlock(vm_reserv_object_lock_ptr((object)))
309e2068d0bSJeff Roberson 
310ada27a3bSKonstantin Belousov static void		vm_reserv_break(vm_reserv_t rv);
311ec179322SAlan Cox static void		vm_reserv_depopulate(vm_reserv_t rv, int index);
312f8a47341SAlan Cox static vm_reserv_t	vm_reserv_from_page(vm_page_t m);
313f8a47341SAlan Cox static boolean_t	vm_reserv_has_pindex(vm_reserv_t rv,
314f8a47341SAlan Cox 			    vm_pindex_t pindex);
315ec179322SAlan Cox static void		vm_reserv_populate(vm_reserv_t rv, int index);
31644aab2c3SAlan Cox static void		vm_reserv_reclaim(vm_reserv_t rv);
317f8a47341SAlan Cox 
318f8a47341SAlan Cox /*
319e0a63baaSAlan Cox  * Returns the current number of full reservations.
320e0a63baaSAlan Cox  *
321fe6d5344SMark Johnston  * Since the number of full reservations is computed without acquiring any
322fe6d5344SMark Johnston  * locks, the returned value is inexact.
323e0a63baaSAlan Cox  */
324e0a63baaSAlan Cox static int
325e0a63baaSAlan Cox sysctl_vm_reserv_fullpop(SYSCTL_HANDLER_ARGS)
326e0a63baaSAlan Cox {
327e0a63baaSAlan Cox 	vm_paddr_t paddr;
328e0a63baaSAlan Cox 	struct vm_phys_seg *seg;
329e0a63baaSAlan Cox 	vm_reserv_t rv;
330e0a63baaSAlan Cox 	int fullpop, segind;
331e0a63baaSAlan Cox 
332e0a63baaSAlan Cox 	fullpop = 0;
333e0a63baaSAlan Cox 	for (segind = 0; segind < vm_phys_nsegs; segind++) {
334e0a63baaSAlan Cox 		seg = &vm_phys_segs[segind];
335e0a63baaSAlan Cox 		paddr = roundup2(seg->start, VM_LEVEL_0_SIZE);
3366b821a74SAleksandr Rybalko 		while (paddr + VM_LEVEL_0_SIZE > paddr && paddr +
3376b821a74SAleksandr Rybalko 		    VM_LEVEL_0_SIZE <= seg->end) {
338e0a63baaSAlan Cox 			rv = &vm_reserv_array[paddr >> VM_LEVEL_0_SHIFT];
339e0a63baaSAlan Cox 			fullpop += rv->popcnt == VM_LEVEL_0_NPAGES;
340e0a63baaSAlan Cox 			paddr += VM_LEVEL_0_SIZE;
341e0a63baaSAlan Cox 		}
342e0a63baaSAlan Cox 	}
343e0a63baaSAlan Cox 	return (sysctl_handle_int(oidp, &fullpop, 0, req));
344e0a63baaSAlan Cox }
345e0a63baaSAlan Cox 
346e0a63baaSAlan Cox /*
3473453bca8SAlan Cox  * Describes the current state of the partially populated reservation queue.
348f8a47341SAlan Cox  */
349f8a47341SAlan Cox static int
350f8a47341SAlan Cox sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS)
351f8a47341SAlan Cox {
352f8a47341SAlan Cox 	struct sbuf sbuf;
353f8a47341SAlan Cox 	vm_reserv_t rv;
354ef435ae7SJeff Roberson 	int counter, error, domain, level, unused_pages;
355f8a47341SAlan Cox 
35600f0e671SMatthew D Fleming 	error = sysctl_wire_old_buffer(req, 0);
35700f0e671SMatthew D Fleming 	if (error != 0)
35800f0e671SMatthew D Fleming 		return (error);
3594e657159SMatthew D Fleming 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
360ef435ae7SJeff Roberson 	sbuf_printf(&sbuf, "\nDOMAIN    LEVEL     SIZE  NUMBER\n\n");
361ef435ae7SJeff Roberson 	for (domain = 0; domain < vm_ndomains; domain++) {
362f8a47341SAlan Cox 		for (level = -1; level <= VM_NRESERVLEVEL - 2; level++) {
363f8a47341SAlan Cox 			counter = 0;
364f8a47341SAlan Cox 			unused_pages = 0;
3655c930c89SJeff Roberson 			vm_reserv_domain_lock(domain);
366fe6d5344SMark Johnston 			TAILQ_FOREACH(rv, &vm_rvd[domain].partpop, partpopq) {
367b378d296SMark Johnston 				if (rv == &vm_rvd[domain].marker)
368b378d296SMark Johnston 					continue;
369f8a47341SAlan Cox 				counter++;
370f8a47341SAlan Cox 				unused_pages += VM_LEVEL_0_NPAGES - rv->popcnt;
371f8a47341SAlan Cox 			}
3725c930c89SJeff Roberson 			vm_reserv_domain_unlock(domain);
373ef435ae7SJeff Roberson 			sbuf_printf(&sbuf, "%6d, %7d, %6dK, %6d\n",
374ef435ae7SJeff Roberson 			    domain, level,
3752cf36c8fSAlan Cox 			    unused_pages * ((int)PAGE_SIZE / 1024), counter);
376f8a47341SAlan Cox 		}
377ef435ae7SJeff Roberson 	}
3784e657159SMatthew D Fleming 	error = sbuf_finish(&sbuf);
379f8a47341SAlan Cox 	sbuf_delete(&sbuf);
380f8a47341SAlan Cox 	return (error);
381f8a47341SAlan Cox }
382f8a47341SAlan Cox 
383f8a47341SAlan Cox /*
384e2068d0bSJeff Roberson  * Remove a reservation from the object's objq.
385e2068d0bSJeff Roberson  */
386e2068d0bSJeff Roberson static void
387e2068d0bSJeff Roberson vm_reserv_remove(vm_reserv_t rv)
388e2068d0bSJeff Roberson {
389e2068d0bSJeff Roberson 	vm_object_t object;
390e2068d0bSJeff Roberson 
3915c930c89SJeff Roberson 	vm_reserv_assert_locked(rv);
3925c930c89SJeff Roberson 	CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d",
3935c930c89SJeff Roberson 	    __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq);
394e2068d0bSJeff Roberson 	KASSERT(rv->object != NULL,
395e2068d0bSJeff Roberson 	    ("vm_reserv_remove: reserv %p is free", rv));
396e2068d0bSJeff Roberson 	KASSERT(!rv->inpartpopq,
397e2068d0bSJeff Roberson 	    ("vm_reserv_remove: reserv %p's inpartpopq is TRUE", rv));
398e2068d0bSJeff Roberson 	object = rv->object;
399e2068d0bSJeff Roberson 	vm_reserv_object_lock(object);
400e2068d0bSJeff Roberson 	LIST_REMOVE(rv, objq);
401e2068d0bSJeff Roberson 	rv->object = NULL;
402e2068d0bSJeff Roberson 	vm_reserv_object_unlock(object);
403e2068d0bSJeff Roberson }
404e2068d0bSJeff Roberson 
405e2068d0bSJeff Roberson /*
406e2068d0bSJeff Roberson  * Insert a new reservation into the object's objq.
407e2068d0bSJeff Roberson  */
408e2068d0bSJeff Roberson static void
409e2068d0bSJeff Roberson vm_reserv_insert(vm_reserv_t rv, vm_object_t object, vm_pindex_t pindex)
410e2068d0bSJeff Roberson {
411e2068d0bSJeff Roberson 	int i;
412e2068d0bSJeff Roberson 
4135c930c89SJeff Roberson 	vm_reserv_assert_locked(rv);
4145c930c89SJeff Roberson 	CTR6(KTR_VM,
4155c930c89SJeff Roberson 	    "%s: rv %p(%p) object %p new %p popcnt %d",
4165c930c89SJeff Roberson 	    __FUNCTION__, rv, rv->pages, rv->object, object,
4175c930c89SJeff Roberson 	   rv->popcnt);
418e2068d0bSJeff Roberson 	KASSERT(rv->object == NULL,
419e2068d0bSJeff Roberson 	    ("vm_reserv_insert: reserv %p isn't free", rv));
420e2068d0bSJeff Roberson 	KASSERT(rv->popcnt == 0,
421e2068d0bSJeff Roberson 	    ("vm_reserv_insert: reserv %p's popcnt is corrupted", rv));
422e2068d0bSJeff Roberson 	KASSERT(!rv->inpartpopq,
423e2068d0bSJeff Roberson 	    ("vm_reserv_insert: reserv %p's inpartpopq is TRUE", rv));
424e2068d0bSJeff Roberson 	for (i = 0; i < NPOPMAP; i++)
425e2068d0bSJeff Roberson 		KASSERT(rv->popmap[i] == 0,
426e2068d0bSJeff Roberson 		    ("vm_reserv_insert: reserv %p's popmap is corrupted", rv));
427e2068d0bSJeff Roberson 	vm_reserv_object_lock(object);
428e2068d0bSJeff Roberson 	rv->pindex = pindex;
429e2068d0bSJeff Roberson 	rv->object = object;
4302ef6727eSJeff Roberson 	rv->lasttick = ticks;
431e2068d0bSJeff Roberson 	LIST_INSERT_HEAD(&object->rvq, rv, objq);
432e2068d0bSJeff Roberson 	vm_reserv_object_unlock(object);
433e2068d0bSJeff Roberson }
434e2068d0bSJeff Roberson 
435e2068d0bSJeff Roberson /*
436f8a47341SAlan Cox  * Reduces the given reservation's population count.  If the population count
437f8a47341SAlan Cox  * becomes zero, the reservation is destroyed.  Additionally, moves the
4383453bca8SAlan Cox  * reservation to the tail of the partially populated reservation queue if the
439f8a47341SAlan Cox  * population count is non-zero.
440f8a47341SAlan Cox  */
441f8a47341SAlan Cox static void
442ec179322SAlan Cox vm_reserv_depopulate(vm_reserv_t rv, int index)
443f8a47341SAlan Cox {
4445c930c89SJeff Roberson 	struct vm_domain *vmd;
445f8a47341SAlan Cox 
4465c930c89SJeff Roberson 	vm_reserv_assert_locked(rv);
4475c930c89SJeff Roberson 	CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d",
4485c930c89SJeff Roberson 	    __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq);
449f8a47341SAlan Cox 	KASSERT(rv->object != NULL,
450f8a47341SAlan Cox 	    ("vm_reserv_depopulate: reserv %p is free", rv));
4513180f757SAlan Cox 	KASSERT(popmap_is_set(rv->popmap, index),
452a08c1515SAlan Cox 	    ("vm_reserv_depopulate: reserv %p's popmap[%d] is clear", rv,
453a08c1515SAlan Cox 	    index));
454f8a47341SAlan Cox 	KASSERT(rv->popcnt > 0,
455f8a47341SAlan Cox 	    ("vm_reserv_depopulate: reserv %p's popcnt is corrupted", rv));
4562d3f4181SJeff Roberson 	KASSERT(rv->domain < vm_ndomains,
457ef435ae7SJeff Roberson 	    ("vm_reserv_depopulate: reserv %p's domain is corrupted %d",
458ef435ae7SJeff Roberson 	    rv, rv->domain));
4595c930c89SJeff Roberson 	if (rv->popcnt == VM_LEVEL_0_NPAGES) {
460dd05fa19SAlan Cox 		KASSERT(rv->pages->psind == 1,
461dd05fa19SAlan Cox 		    ("vm_reserv_depopulate: reserv %p is already demoted",
462dd05fa19SAlan Cox 		    rv));
463dd05fa19SAlan Cox 		rv->pages->psind = 0;
464f8a47341SAlan Cox 	}
4653180f757SAlan Cox 	popmap_clear(rv->popmap, index);
466f8a47341SAlan Cox 	rv->popcnt--;
4672ef6727eSJeff Roberson 	if ((unsigned)(ticks - rv->lasttick) >= PARTPOPSLOP ||
4682ef6727eSJeff Roberson 	    rv->popcnt == 0) {
4695c930c89SJeff Roberson 		vm_reserv_domain_lock(rv->domain);
4705c930c89SJeff Roberson 		if (rv->inpartpopq) {
471fe6d5344SMark Johnston 			TAILQ_REMOVE(&vm_rvd[rv->domain].partpop, rv, partpopq);
4725c930c89SJeff Roberson 			rv->inpartpopq = FALSE;
4735c930c89SJeff Roberson 		}
4745c930c89SJeff Roberson 		if (rv->popcnt != 0) {
475f8a47341SAlan Cox 			rv->inpartpopq = TRUE;
476fe6d5344SMark Johnston 			TAILQ_INSERT_TAIL(&vm_rvd[rv->domain].partpop, rv,
477fe6d5344SMark Johnston 			    partpopq);
478f8a47341SAlan Cox 		}
4795c930c89SJeff Roberson 		vm_reserv_domain_unlock(rv->domain);
4802ef6727eSJeff Roberson 		rv->lasttick = ticks;
4812ef6727eSJeff Roberson 	}
4825c930c89SJeff Roberson 	vmd = VM_DOMAIN(rv->domain);
4835c930c89SJeff Roberson 	if (rv->popcnt == 0) {
4845c930c89SJeff Roberson 		vm_reserv_remove(rv);
4855c930c89SJeff Roberson 		vm_domain_free_lock(vmd);
4865c930c89SJeff Roberson 		vm_phys_free_pages(rv->pages, VM_LEVEL_0_ORDER);
4875c930c89SJeff Roberson 		vm_domain_free_unlock(vmd);
4885c930c89SJeff Roberson 		counter_u64_add(vm_reserv_freed, 1);
4895c930c89SJeff Roberson 	}
4905c930c89SJeff Roberson 	vm_domain_freecnt_inc(vmd, 1);
491f8a47341SAlan Cox }
492f8a47341SAlan Cox 
493f8a47341SAlan Cox /*
494f8a47341SAlan Cox  * Returns the reservation to which the given page might belong.
495f8a47341SAlan Cox  */
496f8a47341SAlan Cox static __inline vm_reserv_t
497f8a47341SAlan Cox vm_reserv_from_page(vm_page_t m)
498f8a47341SAlan Cox {
499f8a47341SAlan Cox 
500f8a47341SAlan Cox 	return (&vm_reserv_array[VM_PAGE_TO_PHYS(m) >> VM_LEVEL_0_SHIFT]);
501f8a47341SAlan Cox }
502f8a47341SAlan Cox 
503f8a47341SAlan Cox /*
504e2068d0bSJeff Roberson  * Returns an existing reservation or NULL and initialized successor pointer.
505e2068d0bSJeff Roberson  */
506e2068d0bSJeff Roberson static vm_reserv_t
507e2068d0bSJeff Roberson vm_reserv_from_object(vm_object_t object, vm_pindex_t pindex,
508e2068d0bSJeff Roberson     vm_page_t mpred, vm_page_t *msuccp)
509e2068d0bSJeff Roberson {
510e2068d0bSJeff Roberson 	vm_reserv_t rv;
511e2068d0bSJeff Roberson 	vm_page_t msucc;
512e2068d0bSJeff Roberson 
513e2068d0bSJeff Roberson 	msucc = NULL;
514e2068d0bSJeff Roberson 	if (mpred != NULL) {
515e2068d0bSJeff Roberson 		KASSERT(mpred->object == object,
516e2068d0bSJeff Roberson 		    ("vm_reserv_from_object: object doesn't contain mpred"));
517e2068d0bSJeff Roberson 		KASSERT(mpred->pindex < pindex,
518e2068d0bSJeff Roberson 		    ("vm_reserv_from_object: mpred doesn't precede pindex"));
519e2068d0bSJeff Roberson 		rv = vm_reserv_from_page(mpred);
520e2068d0bSJeff Roberson 		if (rv->object == object && vm_reserv_has_pindex(rv, pindex))
521e2068d0bSJeff Roberson 			goto found;
522e2068d0bSJeff Roberson 		msucc = TAILQ_NEXT(mpred, listq);
523e2068d0bSJeff Roberson 	} else
524e2068d0bSJeff Roberson 		msucc = TAILQ_FIRST(&object->memq);
525e2068d0bSJeff Roberson 	if (msucc != NULL) {
526e2068d0bSJeff Roberson 		KASSERT(msucc->pindex > pindex,
527e2068d0bSJeff Roberson 		    ("vm_reserv_from_object: msucc doesn't succeed pindex"));
528e2068d0bSJeff Roberson 		rv = vm_reserv_from_page(msucc);
529e2068d0bSJeff Roberson 		if (rv->object == object && vm_reserv_has_pindex(rv, pindex))
530e2068d0bSJeff Roberson 			goto found;
531e2068d0bSJeff Roberson 	}
532e2068d0bSJeff Roberson 	rv = NULL;
533e2068d0bSJeff Roberson 
534e2068d0bSJeff Roberson found:
535e2068d0bSJeff Roberson 	*msuccp = msucc;
536e2068d0bSJeff Roberson 
537e2068d0bSJeff Roberson 	return (rv);
538e2068d0bSJeff Roberson }
539e2068d0bSJeff Roberson 
540e2068d0bSJeff Roberson /*
541f8a47341SAlan Cox  * Returns TRUE if the given reservation contains the given page index and
542f8a47341SAlan Cox  * FALSE otherwise.
543f8a47341SAlan Cox  */
544f8a47341SAlan Cox static __inline boolean_t
545f8a47341SAlan Cox vm_reserv_has_pindex(vm_reserv_t rv, vm_pindex_t pindex)
546f8a47341SAlan Cox {
547f8a47341SAlan Cox 
548f8a47341SAlan Cox 	return (((pindex - rv->pindex) & ~(VM_LEVEL_0_NPAGES - 1)) == 0);
549f8a47341SAlan Cox }
550f8a47341SAlan Cox 
551f8a47341SAlan Cox /*
552f8a47341SAlan Cox  * Increases the given reservation's population count.  Moves the reservation
5533453bca8SAlan Cox  * to the tail of the partially populated reservation queue.
554f8a47341SAlan Cox  */
555f8a47341SAlan Cox static void
556ec179322SAlan Cox vm_reserv_populate(vm_reserv_t rv, int index)
557f8a47341SAlan Cox {
558f8a47341SAlan Cox 
5595c930c89SJeff Roberson 	vm_reserv_assert_locked(rv);
5605c930c89SJeff Roberson 	CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d",
5615c930c89SJeff Roberson 	    __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq);
562f8a47341SAlan Cox 	KASSERT(rv->object != NULL,
563f8a47341SAlan Cox 	    ("vm_reserv_populate: reserv %p is free", rv));
5643180f757SAlan Cox 	KASSERT(popmap_is_clear(rv->popmap, index),
565a08c1515SAlan Cox 	    ("vm_reserv_populate: reserv %p's popmap[%d] is set", rv,
566a08c1515SAlan Cox 	    index));
567f8a47341SAlan Cox 	KASSERT(rv->popcnt < VM_LEVEL_0_NPAGES,
568f8a47341SAlan Cox 	    ("vm_reserv_populate: reserv %p is already full", rv));
569dd05fa19SAlan Cox 	KASSERT(rv->pages->psind == 0,
570dd05fa19SAlan Cox 	    ("vm_reserv_populate: reserv %p is already promoted", rv));
5712d3f4181SJeff Roberson 	KASSERT(rv->domain < vm_ndomains,
572ef435ae7SJeff Roberson 	    ("vm_reserv_populate: reserv %p's domain is corrupted %d",
573ef435ae7SJeff Roberson 	    rv, rv->domain));
5745c930c89SJeff Roberson 	popmap_set(rv->popmap, index);
5755c930c89SJeff Roberson 	rv->popcnt++;
5762ef6727eSJeff Roberson 	if ((unsigned)(ticks - rv->lasttick) < PARTPOPSLOP &&
5772ef6727eSJeff Roberson 	    rv->inpartpopq && rv->popcnt != VM_LEVEL_0_NPAGES)
5782ef6727eSJeff Roberson 		return;
5792ef6727eSJeff Roberson 	rv->lasttick = ticks;
5805c930c89SJeff Roberson 	vm_reserv_domain_lock(rv->domain);
581f8a47341SAlan Cox 	if (rv->inpartpopq) {
582fe6d5344SMark Johnston 		TAILQ_REMOVE(&vm_rvd[rv->domain].partpop, rv, partpopq);
583f8a47341SAlan Cox 		rv->inpartpopq = FALSE;
584f8a47341SAlan Cox 	}
585f8a47341SAlan Cox 	if (rv->popcnt < VM_LEVEL_0_NPAGES) {
586f8a47341SAlan Cox 		rv->inpartpopq = TRUE;
587fe6d5344SMark Johnston 		TAILQ_INSERT_TAIL(&vm_rvd[rv->domain].partpop, rv, partpopq);
5885c930c89SJeff Roberson 	} else {
5895c930c89SJeff Roberson 		KASSERT(rv->pages->psind == 0,
5905c930c89SJeff Roberson 		    ("vm_reserv_populate: reserv %p is already promoted",
5915c930c89SJeff Roberson 		    rv));
592dd05fa19SAlan Cox 		rv->pages->psind = 1;
593f8a47341SAlan Cox 	}
5945c930c89SJeff Roberson 	vm_reserv_domain_unlock(rv->domain);
5955c930c89SJeff Roberson }
596f8a47341SAlan Cox 
597f8a47341SAlan Cox /*
598e2068d0bSJeff Roberson  * Allocates a contiguous set of physical pages of the given size "npages"
5992d5039dbSAlan Cox  * from existing or newly created reservations.  All of the physical pages
600e2068d0bSJeff Roberson  * must be at or above the given physical address "low" and below the given
601e2068d0bSJeff Roberson  * physical address "high".  The given value "alignment" determines the
602e2068d0bSJeff Roberson  * alignment of the first physical page in the set.  If the given value
603e2068d0bSJeff Roberson  * "boundary" is non-zero, then the set of physical pages cannot cross any
604e2068d0bSJeff Roberson  * physical address boundary that is a multiple of that value.  Both
605e2068d0bSJeff Roberson  * "alignment" and "boundary" must be a power of two.
606e2068d0bSJeff Roberson  *
607e2068d0bSJeff Roberson  * The page "mpred" must immediately precede the offset "pindex" within the
608e2068d0bSJeff Roberson  * specified object.
609e2068d0bSJeff Roberson  *
6102d5039dbSAlan Cox  * The object must be locked.
611e2068d0bSJeff Roberson  */
612e2068d0bSJeff Roberson vm_page_t
6132d5039dbSAlan Cox vm_reserv_alloc_contig(vm_object_t object, vm_pindex_t pindex, int domain,
6142d5039dbSAlan Cox     int req, vm_page_t mpred, u_long npages, vm_paddr_t low, vm_paddr_t high,
6152d5039dbSAlan Cox     u_long alignment, vm_paddr_t boundary)
616c68c3537SAlan Cox {
6175c930c89SJeff Roberson 	struct vm_domain *vmd;
618c68c3537SAlan Cox 	vm_paddr_t pa, size;
619920da7e4SAlan Cox 	vm_page_t m, m_ret, msucc;
620c68c3537SAlan Cox 	vm_pindex_t first, leftcap, rightcap;
621c68c3537SAlan Cox 	vm_reserv_t rv;
622c68c3537SAlan Cox 	u_long allocpages, maxpages, minpages;
623c68c3537SAlan Cox 	int i, index, n;
624c68c3537SAlan Cox 
62589f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
626c68c3537SAlan Cox 	KASSERT(npages != 0, ("vm_reserv_alloc_contig: npages is 0"));
627c68c3537SAlan Cox 
628c68c3537SAlan Cox 	/*
629c68c3537SAlan Cox 	 * Is a reservation fundamentally impossible?
630c68c3537SAlan Cox 	 */
631c68c3537SAlan Cox 	if (pindex < VM_RESERV_INDEX(object, pindex) ||
632c68c3537SAlan Cox 	    pindex + npages > object->size)
633c68c3537SAlan Cox 		return (NULL);
634c68c3537SAlan Cox 
635c68c3537SAlan Cox 	/*
636c68c3537SAlan Cox 	 * All reservations of a particular size have the same alignment.
637c68c3537SAlan Cox 	 * Assuming that the first page is allocated from a reservation, the
638c68c3537SAlan Cox 	 * least significant bits of its physical address can be determined
639c68c3537SAlan Cox 	 * from its offset from the beginning of the reservation and the size
640c68c3537SAlan Cox 	 * of the reservation.
641c68c3537SAlan Cox 	 *
642c68c3537SAlan Cox 	 * Could the specified index within a reservation of the smallest
643c68c3537SAlan Cox 	 * possible size satisfy the alignment and boundary requirements?
644c68c3537SAlan Cox 	 */
645c68c3537SAlan Cox 	pa = VM_RESERV_INDEX(object, pindex) << PAGE_SHIFT;
646c68c3537SAlan Cox 	if ((pa & (alignment - 1)) != 0)
647c68c3537SAlan Cox 		return (NULL);
648c68c3537SAlan Cox 	size = npages << PAGE_SHIFT;
649c68c3537SAlan Cox 	if (((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0)
650c68c3537SAlan Cox 		return (NULL);
651c68c3537SAlan Cox 
652c68c3537SAlan Cox 	/*
6532d5039dbSAlan Cox 	 * Look for an existing reservation.
654c68c3537SAlan Cox 	 */
655e2068d0bSJeff Roberson 	rv = vm_reserv_from_object(object, pindex, mpred, &msucc);
6562d5039dbSAlan Cox 	if (rv != NULL) {
6572d5039dbSAlan Cox 		KASSERT(object != kernel_object || rv->domain == domain,
6582d5039dbSAlan Cox 		    ("vm_reserv_alloc_contig: domain mismatch"));
6592d5039dbSAlan Cox 		index = VM_RESERV_INDEX(object, pindex);
6602d5039dbSAlan Cox 		/* Does the allocation fit within the reservation? */
6612d5039dbSAlan Cox 		if (index + npages > VM_LEVEL_0_NPAGES)
662e2068d0bSJeff Roberson 			return (NULL);
6632d5039dbSAlan Cox 		domain = rv->domain;
6642d5039dbSAlan Cox 		vmd = VM_DOMAIN(domain);
6652d5039dbSAlan Cox 		vm_reserv_lock(rv);
6662d5039dbSAlan Cox 		/* Handle reclaim race. */
6672d5039dbSAlan Cox 		if (rv->object != object)
6682d5039dbSAlan Cox 			goto out;
6692d5039dbSAlan Cox 		m = &rv->pages[index];
6702d5039dbSAlan Cox 		pa = VM_PAGE_TO_PHYS(m);
6712d5039dbSAlan Cox 		if (pa < low || pa + size > high ||
6722d5039dbSAlan Cox 		    (pa & (alignment - 1)) != 0 ||
6732d5039dbSAlan Cox 		    ((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0)
6742d5039dbSAlan Cox 			goto out;
6752d5039dbSAlan Cox 		/* Handle vm_page_rename(m, new_object, ...). */
6762d5039dbSAlan Cox 		for (i = 0; i < npages; i++)
6772d5039dbSAlan Cox 			if (popmap_is_set(rv->popmap, index + i))
6782d5039dbSAlan Cox 				goto out;
6792d5039dbSAlan Cox 		if (!vm_domain_allocate(vmd, req, npages))
6802d5039dbSAlan Cox 			goto out;
6812d5039dbSAlan Cox 		for (i = 0; i < npages; i++)
6822d5039dbSAlan Cox 			vm_reserv_populate(rv, index + i);
6832d5039dbSAlan Cox 		vm_reserv_unlock(rv);
6842d5039dbSAlan Cox 		return (m);
6852d5039dbSAlan Cox out:
6862d5039dbSAlan Cox 		vm_reserv_unlock(rv);
6872d5039dbSAlan Cox 		return (NULL);
6882d5039dbSAlan Cox 	}
689c68c3537SAlan Cox 
690c68c3537SAlan Cox 	/*
691c68c3537SAlan Cox 	 * Could at least one reservation fit between the first index to the
69264f096eeSAlan Cox 	 * left that can be used ("leftcap") and the first index to the right
69364f096eeSAlan Cox 	 * that cannot be used ("rightcap")?
694e2068d0bSJeff Roberson 	 *
695e2068d0bSJeff Roberson 	 * We must synchronize with the reserv object lock to protect the
696e2068d0bSJeff Roberson 	 * pindex/object of the resulting reservations against rename while
697e2068d0bSJeff Roberson 	 * we are inspecting.
698c68c3537SAlan Cox 	 */
699c68c3537SAlan Cox 	first = pindex - VM_RESERV_INDEX(object, pindex);
700e2068d0bSJeff Roberson 	minpages = VM_RESERV_INDEX(object, pindex) + npages;
701e2068d0bSJeff Roberson 	maxpages = roundup2(minpages, VM_LEVEL_0_NPAGES);
702e2068d0bSJeff Roberson 	allocpages = maxpages;
703e2068d0bSJeff Roberson 	vm_reserv_object_lock(object);
704c68c3537SAlan Cox 	if (mpred != NULL) {
705c68c3537SAlan Cox 		if ((rv = vm_reserv_from_page(mpred))->object != object)
706c68c3537SAlan Cox 			leftcap = mpred->pindex + 1;
707c68c3537SAlan Cox 		else
708c68c3537SAlan Cox 			leftcap = rv->pindex + VM_LEVEL_0_NPAGES;
709e2068d0bSJeff Roberson 		if (leftcap > first) {
710e2068d0bSJeff Roberson 			vm_reserv_object_unlock(object);
711c68c3537SAlan Cox 			return (NULL);
712c68c3537SAlan Cox 		}
713e2068d0bSJeff Roberson 	}
714c68c3537SAlan Cox 	if (msucc != NULL) {
715c68c3537SAlan Cox 		if ((rv = vm_reserv_from_page(msucc))->object != object)
716c68c3537SAlan Cox 			rightcap = msucc->pindex;
717c68c3537SAlan Cox 		else
718c68c3537SAlan Cox 			rightcap = rv->pindex;
719c68c3537SAlan Cox 		if (first + maxpages > rightcap) {
720e2068d0bSJeff Roberson 			if (maxpages == VM_LEVEL_0_NPAGES) {
721e2068d0bSJeff Roberson 				vm_reserv_object_unlock(object);
722c68c3537SAlan Cox 				return (NULL);
723e2068d0bSJeff Roberson 			}
72464f096eeSAlan Cox 
72564f096eeSAlan Cox 			/*
72664f096eeSAlan Cox 			 * At least one reservation will fit between "leftcap"
72764f096eeSAlan Cox 			 * and "rightcap".  However, a reservation for the
72864f096eeSAlan Cox 			 * last of the requested pages will not fit.  Reduce
72964f096eeSAlan Cox 			 * the size of the upcoming allocation accordingly.
73064f096eeSAlan Cox 			 */
731c68c3537SAlan Cox 			allocpages = minpages;
732c68c3537SAlan Cox 		}
733c68c3537SAlan Cox 	}
734e2068d0bSJeff Roberson 	vm_reserv_object_unlock(object);
735c68c3537SAlan Cox 
736c68c3537SAlan Cox 	/*
737c68c3537SAlan Cox 	 * Would the last new reservation extend past the end of the object?
73863967687SJeff Roberson 	 *
73963967687SJeff Roberson 	 * If the object is unlikely to grow don't allocate a reservation for
74063967687SJeff Roberson 	 * the tail.
741c68c3537SAlan Cox 	 */
74263967687SJeff Roberson 	if ((object->flags & OBJ_ANON) == 0 &&
74363967687SJeff Roberson 	    first + maxpages > object->size) {
744c68c3537SAlan Cox 		if (maxpages == VM_LEVEL_0_NPAGES)
745c68c3537SAlan Cox 			return (NULL);
746c68c3537SAlan Cox 		allocpages = minpages;
747c68c3537SAlan Cox 	}
748c68c3537SAlan Cox 
749c68c3537SAlan Cox 	/*
75064f096eeSAlan Cox 	 * Allocate the physical pages.  The alignment and boundary specified
75164f096eeSAlan Cox 	 * for this allocation may be different from the alignment and
75264f096eeSAlan Cox 	 * boundary specified for the requested pages.  For instance, the
75364f096eeSAlan Cox 	 * specified index may not be the first page within the first new
75464f096eeSAlan Cox 	 * reservation.
755c68c3537SAlan Cox 	 */
7565c930c89SJeff Roberson 	m = NULL;
7575c930c89SJeff Roberson 	vmd = VM_DOMAIN(domain);
7585c930c89SJeff Roberson 	if (vm_domain_allocate(vmd, req, npages)) {
7595c930c89SJeff Roberson 		vm_domain_free_lock(vmd);
7605c930c89SJeff Roberson 		m = vm_phys_alloc_contig(domain, allocpages, low, high,
7615c930c89SJeff Roberson 		    ulmax(alignment, VM_LEVEL_0_SIZE),
7625c930c89SJeff Roberson 		    boundary > VM_LEVEL_0_SIZE ? boundary : 0);
7635c930c89SJeff Roberson 		vm_domain_free_unlock(vmd);
7645c930c89SJeff Roberson 		if (m == NULL) {
7655c930c89SJeff Roberson 			vm_domain_freecnt_inc(vmd, npages);
7665c930c89SJeff Roberson 			return (NULL);
7675c930c89SJeff Roberson 		}
7685c930c89SJeff Roberson 	} else
769c68c3537SAlan Cox 		return (NULL);
770e2068d0bSJeff Roberson 	KASSERT(vm_phys_domain(m) == domain,
7717a469c8eSJeff Roberson 	    ("vm_reserv_alloc_contig: Page domain does not match requested."));
77264f096eeSAlan Cox 
77364f096eeSAlan Cox 	/*
77464f096eeSAlan Cox 	 * The allocated physical pages always begin at a reservation
77564f096eeSAlan Cox 	 * boundary, but they do not always end at a reservation boundary.
77664f096eeSAlan Cox 	 * Initialize every reservation that is completely covered by the
77764f096eeSAlan Cox 	 * allocated physical pages.
77864f096eeSAlan Cox 	 */
779c68c3537SAlan Cox 	m_ret = NULL;
780c68c3537SAlan Cox 	index = VM_RESERV_INDEX(object, pindex);
781c68c3537SAlan Cox 	do {
782c68c3537SAlan Cox 		rv = vm_reserv_from_page(m);
783c68c3537SAlan Cox 		KASSERT(rv->pages == m,
784c68c3537SAlan Cox 		    ("vm_reserv_alloc_contig: reserv %p's pages is corrupted",
785c68c3537SAlan Cox 		    rv));
7865c930c89SJeff Roberson 		vm_reserv_lock(rv);
787e2068d0bSJeff Roberson 		vm_reserv_insert(rv, object, first);
788c68c3537SAlan Cox 		n = ulmin(VM_LEVEL_0_NPAGES - index, npages);
789c68c3537SAlan Cox 		for (i = 0; i < n; i++)
790ec179322SAlan Cox 			vm_reserv_populate(rv, index + i);
791c68c3537SAlan Cox 		npages -= n;
792c68c3537SAlan Cox 		if (m_ret == NULL) {
793c68c3537SAlan Cox 			m_ret = &rv->pages[index];
794c68c3537SAlan Cox 			index = 0;
795c68c3537SAlan Cox 		}
7965c930c89SJeff Roberson 		vm_reserv_unlock(rv);
797c68c3537SAlan Cox 		m += VM_LEVEL_0_NPAGES;
798c68c3537SAlan Cox 		first += VM_LEVEL_0_NPAGES;
799c68c3537SAlan Cox 		allocpages -= VM_LEVEL_0_NPAGES;
80064f096eeSAlan Cox 	} while (allocpages >= VM_LEVEL_0_NPAGES);
801c68c3537SAlan Cox 	return (m_ret);
802e2068d0bSJeff Roberson }
803c68c3537SAlan Cox 
804c68c3537SAlan Cox /*
8052d5039dbSAlan Cox  * Allocate a physical page from an existing or newly created reservation.
806e2068d0bSJeff Roberson  *
807e2068d0bSJeff Roberson  * The page "mpred" must immediately precede the offset "pindex" within the
808e2068d0bSJeff Roberson  * specified object.
809e2068d0bSJeff Roberson  *
810e2068d0bSJeff Roberson  * The object must be locked.
811c68c3537SAlan Cox  */
812e2068d0bSJeff Roberson vm_page_t
8132d5039dbSAlan Cox vm_reserv_alloc_page(vm_object_t object, vm_pindex_t pindex, int domain,
8142d5039dbSAlan Cox     int req, vm_page_t mpred)
815e2068d0bSJeff Roberson {
816e2068d0bSJeff Roberson 	struct vm_domain *vmd;
817e2068d0bSJeff Roberson 	vm_page_t m, msucc;
8182d5039dbSAlan Cox 	vm_pindex_t first, leftcap, rightcap;
819e2068d0bSJeff Roberson 	vm_reserv_t rv;
82030fbfddaSJeff Roberson 	int index;
821e2068d0bSJeff Roberson 
822e2068d0bSJeff Roberson 	VM_OBJECT_ASSERT_WLOCKED(object);
823e2068d0bSJeff Roberson 
824e2068d0bSJeff Roberson 	/*
8252d5039dbSAlan Cox 	 * Is a reservation fundamentally impossible?
826e2068d0bSJeff Roberson 	 */
827e2068d0bSJeff Roberson 	if (pindex < VM_RESERV_INDEX(object, pindex) ||
8282d5039dbSAlan Cox 	    pindex >= object->size)
829e2068d0bSJeff Roberson 		return (NULL);
830e2068d0bSJeff Roberson 
831e2068d0bSJeff Roberson 	/*
832e2068d0bSJeff Roberson 	 * Look for an existing reservation.
833e2068d0bSJeff Roberson 	 */
834e2068d0bSJeff Roberson 	rv = vm_reserv_from_object(object, pindex, mpred, &msucc);
8352d5039dbSAlan Cox 	if (rv != NULL) {
836e2068d0bSJeff Roberson 		KASSERT(object != kernel_object || rv->domain == domain,
8372d5039dbSAlan Cox 		    ("vm_reserv_alloc_page: domain mismatch"));
838e2068d0bSJeff Roberson 		domain = rv->domain;
839e2068d0bSJeff Roberson 		vmd = VM_DOMAIN(domain);
840c68c3537SAlan Cox 		index = VM_RESERV_INDEX(object, pindex);
841c68c3537SAlan Cox 		m = &rv->pages[index];
8425c930c89SJeff Roberson 		vm_reserv_lock(rv);
843e2068d0bSJeff Roberson 		/* Handle reclaim race. */
8445c930c89SJeff Roberson 		if (rv->object != object ||
845c68c3537SAlan Cox 		    /* Handle vm_page_rename(m, new_object, ...). */
8465c930c89SJeff Roberson 		    popmap_is_set(rv->popmap, index)) {
847e2068d0bSJeff Roberson 			m = NULL;
8485c930c89SJeff Roberson 			goto out;
84930fbfddaSJeff Roberson 		}
8505c930c89SJeff Roberson 		if (vm_domain_allocate(vmd, req, 1) == 0)
8515c930c89SJeff Roberson 			m = NULL;
8525c930c89SJeff Roberson 		else
8535c930c89SJeff Roberson 			vm_reserv_populate(rv, index);
8545c930c89SJeff Roberson out:
8555c930c89SJeff Roberson 		vm_reserv_unlock(rv);
856c68c3537SAlan Cox 		return (m);
857c68c3537SAlan Cox 	}
858c68c3537SAlan Cox 
859c68c3537SAlan Cox 	/*
860c68c3537SAlan Cox 	 * Could a reservation fit between the first index to the left that
861c68c3537SAlan Cox 	 * can be used and the first index to the right that cannot be used?
862e2068d0bSJeff Roberson 	 *
863e2068d0bSJeff Roberson 	 * We must synchronize with the reserv object lock to protect the
864e2068d0bSJeff Roberson 	 * pindex/object of the resulting reservations against rename while
865e2068d0bSJeff Roberson 	 * we are inspecting.
866f8a47341SAlan Cox 	 */
867c68c3537SAlan Cox 	first = pindex - VM_RESERV_INDEX(object, pindex);
868e2068d0bSJeff Roberson 	vm_reserv_object_lock(object);
869c68c3537SAlan Cox 	if (mpred != NULL) {
870c68c3537SAlan Cox 		if ((rv = vm_reserv_from_page(mpred))->object != object)
871f8a47341SAlan Cox 			leftcap = mpred->pindex + 1;
872f8a47341SAlan Cox 		else
873f8a47341SAlan Cox 			leftcap = rv->pindex + VM_LEVEL_0_NPAGES;
874e2068d0bSJeff Roberson 		if (leftcap > first) {
875e2068d0bSJeff Roberson 			vm_reserv_object_unlock(object);
876c68c3537SAlan Cox 			return (NULL);
877c68c3537SAlan Cox 		}
878e2068d0bSJeff Roberson 	}
879c68c3537SAlan Cox 	if (msucc != NULL) {
880c68c3537SAlan Cox 		if ((rv = vm_reserv_from_page(msucc))->object != object)
881f8a47341SAlan Cox 			rightcap = msucc->pindex;
882f8a47341SAlan Cox 		else
883f8a47341SAlan Cox 			rightcap = rv->pindex;
884e2068d0bSJeff Roberson 		if (first + VM_LEVEL_0_NPAGES > rightcap) {
885e2068d0bSJeff Roberson 			vm_reserv_object_unlock(object);
886f8a47341SAlan Cox 			return (NULL);
887c68c3537SAlan Cox 		}
888e2068d0bSJeff Roberson 	}
889e2068d0bSJeff Roberson 	vm_reserv_object_unlock(object);
890f8a47341SAlan Cox 
891f8a47341SAlan Cox 	/*
89263967687SJeff Roberson 	 * Would the last new reservation extend past the end of the object?
89363967687SJeff Roberson 	 *
89463967687SJeff Roberson 	 * If the object is unlikely to grow don't allocate a reservation for
89563967687SJeff Roberson 	 * the tail.
896f8a47341SAlan Cox 	 */
89763967687SJeff Roberson 	if ((object->flags & OBJ_ANON) == 0 &&
89863967687SJeff Roberson 	    first + VM_LEVEL_0_NPAGES > object->size)
899f8a47341SAlan Cox 		return (NULL);
900f8a47341SAlan Cox 
901f8a47341SAlan Cox 	/*
902c68c3537SAlan Cox 	 * Allocate and populate the new reservation.
903f8a47341SAlan Cox 	 */
9045c930c89SJeff Roberson 	m = NULL;
9055c930c89SJeff Roberson 	vmd = VM_DOMAIN(domain);
9065c930c89SJeff Roberson 	if (vm_domain_allocate(vmd, req, 1)) {
9075c930c89SJeff Roberson 		vm_domain_free_lock(vmd);
9085c930c89SJeff Roberson 		m = vm_phys_alloc_pages(domain, VM_FREEPOOL_DEFAULT,
9095c930c89SJeff Roberson 		    VM_LEVEL_0_ORDER);
9105c930c89SJeff Roberson 		vm_domain_free_unlock(vmd);
9115c930c89SJeff Roberson 		if (m == NULL) {
9125c930c89SJeff Roberson 			vm_domain_freecnt_inc(vmd, 1);
9135c930c89SJeff Roberson 			return (NULL);
9145c930c89SJeff Roberson 		}
9155c930c89SJeff Roberson 	} else
916c68c3537SAlan Cox 		return (NULL);
917f8a47341SAlan Cox 	rv = vm_reserv_from_page(m);
9185c930c89SJeff Roberson 	vm_reserv_lock(rv);
919f8a47341SAlan Cox 	KASSERT(rv->pages == m,
920c68c3537SAlan Cox 	    ("vm_reserv_alloc_page: reserv %p's pages is corrupted", rv));
921e2068d0bSJeff Roberson 	vm_reserv_insert(rv, object, first);
922ec179322SAlan Cox 	index = VM_RESERV_INDEX(object, pindex);
923ec179322SAlan Cox 	vm_reserv_populate(rv, index);
9245c930c89SJeff Roberson 	vm_reserv_unlock(rv);
9255c930c89SJeff Roberson 
926ec179322SAlan Cox 	return (&rv->pages[index]);
927f8a47341SAlan Cox }
928f8a47341SAlan Cox 
929f8a47341SAlan Cox /*
930ada27a3bSKonstantin Belousov  * Breaks the given reservation.  All free pages in the reservation
931ada27a3bSKonstantin Belousov  * are returned to the physical memory allocator.  The reservation's
932ada27a3bSKonstantin Belousov  * population count and map are reset to their initial state.
933ec179322SAlan Cox  *
9343453bca8SAlan Cox  * The given reservation must not be in the partially populated reservation
935fe6d5344SMark Johnston  * queue.
936ec179322SAlan Cox  */
937ec179322SAlan Cox static void
938ada27a3bSKonstantin Belousov vm_reserv_break(vm_reserv_t rv)
939ec179322SAlan Cox {
940e67a5068SDoug Moore 	u_long changes;
941e67a5068SDoug Moore 	int bitpos, hi, i, lo;
942ec179322SAlan Cox 
9435c930c89SJeff Roberson 	vm_reserv_assert_locked(rv);
9445c930c89SJeff Roberson 	CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d",
9455c930c89SJeff Roberson 	    __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq);
946e2068d0bSJeff Roberson 	vm_reserv_remove(rv);
947c4be9169SKonstantin Belousov 	rv->pages->psind = 0;
948e67a5068SDoug Moore 	hi = lo = -1;
949e67a5068SDoug Moore 	for (i = 0; i <= NPOPMAP; i++) {
950e67a5068SDoug Moore 		/*
951e67a5068SDoug Moore 		 * "changes" is a bitmask that marks where a new sequence of
952e67a5068SDoug Moore 		 * 0s or 1s begins in popmap[i], with last bit in popmap[i-1]
953e67a5068SDoug Moore 		 * considered to be 1 if and only if lo == hi.  The bits of
954e67a5068SDoug Moore 		 * popmap[-1] and popmap[NPOPMAP] are considered all 1s.
955e67a5068SDoug Moore 		 */
956ec179322SAlan Cox 		if (i == NPOPMAP)
957e67a5068SDoug Moore 			changes = lo != hi;
958e67a5068SDoug Moore 		else {
959e67a5068SDoug Moore 			changes = rv->popmap[i];
960e67a5068SDoug Moore 			changes ^= (changes << 1) | (lo == hi);
961e67a5068SDoug Moore 			rv->popmap[i] = 0;
962ec179322SAlan Cox 		}
963e67a5068SDoug Moore 		while (changes != 0) {
964e67a5068SDoug Moore 			/*
965e67a5068SDoug Moore 			 * If the next change marked begins a run of 0s, set
966e67a5068SDoug Moore 			 * lo to mark that position.  Otherwise set hi and
967e67a5068SDoug Moore 			 * free pages from lo up to hi.
968e67a5068SDoug Moore 			 */
969e67a5068SDoug Moore 			bitpos = ffsl(changes) - 1;
970e67a5068SDoug Moore 			changes ^= 1UL << bitpos;
971e67a5068SDoug Moore 			if (lo == hi)
972e67a5068SDoug Moore 				lo = NBPOPMAP * i + bitpos;
973e67a5068SDoug Moore 			else {
974e67a5068SDoug Moore 				hi = NBPOPMAP * i + bitpos;
9755c930c89SJeff Roberson 				vm_domain_free_lock(VM_DOMAIN(rv->domain));
976b8590daeSDoug Moore 				vm_phys_enqueue_contig(&rv->pages[lo], hi - lo);
9775c930c89SJeff Roberson 				vm_domain_free_unlock(VM_DOMAIN(rv->domain));
978e67a5068SDoug Moore 				lo = hi;
979e67a5068SDoug Moore 			}
980e67a5068SDoug Moore 		}
981e67a5068SDoug Moore 	}
982e67a5068SDoug Moore 	rv->popcnt = 0;
9835c930c89SJeff Roberson 	counter_u64_add(vm_reserv_broken, 1);
984ec179322SAlan Cox }
985ec179322SAlan Cox 
986ec179322SAlan Cox /*
987f8a47341SAlan Cox  * Breaks all reservations belonging to the given object.
988f8a47341SAlan Cox  */
989f8a47341SAlan Cox void
990f8a47341SAlan Cox vm_reserv_break_all(vm_object_t object)
991f8a47341SAlan Cox {
992f8a47341SAlan Cox 	vm_reserv_t rv;
993f8a47341SAlan Cox 
994e2068d0bSJeff Roberson 	/*
995e2068d0bSJeff Roberson 	 * This access of object->rvq is unsynchronized so that the
996e2068d0bSJeff Roberson 	 * object rvq lock can nest after the domain_free lock.  We
997e2068d0bSJeff Roberson 	 * must check for races in the results.  However, the object
998e2068d0bSJeff Roberson 	 * lock prevents new additions, so we are guaranteed that when
999e2068d0bSJeff Roberson 	 * it returns NULL the object is properly empty.
1000e2068d0bSJeff Roberson 	 */
1001f8a47341SAlan Cox 	while ((rv = LIST_FIRST(&object->rvq)) != NULL) {
10025c930c89SJeff Roberson 		vm_reserv_lock(rv);
1003e2068d0bSJeff Roberson 		/* Reclaim race. */
10045c930c89SJeff Roberson 		if (rv->object != object) {
10055c930c89SJeff Roberson 			vm_reserv_unlock(rv);
1006e2068d0bSJeff Roberson 			continue;
10075c930c89SJeff Roberson 		}
10085c930c89SJeff Roberson 		vm_reserv_domain_lock(rv->domain);
1009f8a47341SAlan Cox 		if (rv->inpartpopq) {
1010fe6d5344SMark Johnston 			TAILQ_REMOVE(&vm_rvd[rv->domain].partpop, rv, partpopq);
1011f8a47341SAlan Cox 			rv->inpartpopq = FALSE;
1012f8a47341SAlan Cox 		}
10135c930c89SJeff Roberson 		vm_reserv_domain_unlock(rv->domain);
1014ada27a3bSKonstantin Belousov 		vm_reserv_break(rv);
10155c930c89SJeff Roberson 		vm_reserv_unlock(rv);
1016f8a47341SAlan Cox 	}
1017f8a47341SAlan Cox }
1018f8a47341SAlan Cox 
1019f8a47341SAlan Cox /*
1020f8a47341SAlan Cox  * Frees the given page if it belongs to a reservation.  Returns TRUE if the
1021f8a47341SAlan Cox  * page is freed and FALSE otherwise.
1022f8a47341SAlan Cox  */
1023f8a47341SAlan Cox boolean_t
1024f8a47341SAlan Cox vm_reserv_free_page(vm_page_t m)
1025f8a47341SAlan Cox {
1026f8a47341SAlan Cox 	vm_reserv_t rv;
10275c930c89SJeff Roberson 	boolean_t ret;
1028f8a47341SAlan Cox 
1029f8a47341SAlan Cox 	rv = vm_reserv_from_page(m);
1030908e3da1SAlan Cox 	if (rv->object == NULL)
1031908e3da1SAlan Cox 		return (FALSE);
10325c930c89SJeff Roberson 	vm_reserv_lock(rv);
10335c930c89SJeff Roberson 	/* Re-validate after lock. */
10345c930c89SJeff Roberson 	if (rv->object != NULL) {
1035ec179322SAlan Cox 		vm_reserv_depopulate(rv, m - rv->pages);
10365c930c89SJeff Roberson 		ret = TRUE;
10375c930c89SJeff Roberson 	} else
10385c930c89SJeff Roberson 		ret = FALSE;
10395c930c89SJeff Roberson 	vm_reserv_unlock(rv);
10405c930c89SJeff Roberson 
10415c930c89SJeff Roberson 	return (ret);
1042f8a47341SAlan Cox }
1043f8a47341SAlan Cox 
1044f8a47341SAlan Cox /*
1045f8a47341SAlan Cox  * Initializes the reservation management system.  Specifically, initializes
1046f8a47341SAlan Cox  * the reservation array.
1047f8a47341SAlan Cox  *
1048f8a47341SAlan Cox  * Requires that vm_page_array and first_page are initialized!
1049f8a47341SAlan Cox  */
1050f8a47341SAlan Cox void
1051f8a47341SAlan Cox vm_reserv_init(void)
1052f8a47341SAlan Cox {
1053f8a47341SAlan Cox 	vm_paddr_t paddr;
105409e5f3c4SAlan Cox 	struct vm_phys_seg *seg;
10555c930c89SJeff Roberson 	struct vm_reserv *rv;
1056b378d296SMark Johnston 	struct vm_reserv_domain *rvd;
1057b378d296SMark Johnston 	int i, j, segind;
1058f8a47341SAlan Cox 
1059f8a47341SAlan Cox 	/*
1060f8a47341SAlan Cox 	 * Initialize the reservation array.  Specifically, initialize the
1061f8a47341SAlan Cox 	 * "pages" field for every element that has an underlying superpage.
1062f8a47341SAlan Cox 	 */
106309e5f3c4SAlan Cox 	for (segind = 0; segind < vm_phys_nsegs; segind++) {
106409e5f3c4SAlan Cox 		seg = &vm_phys_segs[segind];
106509e5f3c4SAlan Cox 		paddr = roundup2(seg->start, VM_LEVEL_0_SIZE);
10666b821a74SAleksandr Rybalko 		while (paddr + VM_LEVEL_0_SIZE > paddr && paddr +
10676b821a74SAleksandr Rybalko 		    VM_LEVEL_0_SIZE <= seg->end) {
10685c930c89SJeff Roberson 			rv = &vm_reserv_array[paddr >> VM_LEVEL_0_SHIFT];
10695c930c89SJeff Roberson 			rv->pages = PHYS_TO_VM_PAGE(paddr);
10705c930c89SJeff Roberson 			rv->domain = seg->domain;
10715c930c89SJeff Roberson 			mtx_init(&rv->lock, "vm reserv", NULL, MTX_DEF);
1072f8a47341SAlan Cox 			paddr += VM_LEVEL_0_SIZE;
1073f8a47341SAlan Cox 		}
1074f8a47341SAlan Cox 	}
10755c930c89SJeff Roberson 	for (i = 0; i < MAXMEMDOM; i++) {
1076b378d296SMark Johnston 		rvd = &vm_rvd[i];
1077b378d296SMark Johnston 		mtx_init(&rvd->lock, "vm reserv domain", NULL, MTX_DEF);
1078b378d296SMark Johnston 		TAILQ_INIT(&rvd->partpop);
1079b378d296SMark Johnston 		mtx_init(&rvd->marker.lock, "vm reserv marker", NULL, MTX_DEF);
1080b378d296SMark Johnston 
1081b378d296SMark Johnston 		/*
1082b378d296SMark Johnston 		 * Fully populated reservations should never be present in the
1083b378d296SMark Johnston 		 * partially populated reservation queues.
1084b378d296SMark Johnston 		 */
1085b378d296SMark Johnston 		rvd->marker.popcnt = VM_LEVEL_0_NPAGES;
1086b378d296SMark Johnston 		for (j = 0; j < NBPOPMAP; j++)
1087b378d296SMark Johnston 			popmap_set(rvd->marker.popmap, j);
1088f8a47341SAlan Cox 	}
1089f8a47341SAlan Cox 
10905c930c89SJeff Roberson 	for (i = 0; i < VM_RESERV_OBJ_LOCK_COUNT; i++)
10915c930c89SJeff Roberson 		mtx_init(&vm_reserv_object_mtx[i], "resv obj lock", NULL,
10925c930c89SJeff Roberson 		    MTX_DEF);
10935c930c89SJeff Roberson }
10945c930c89SJeff Roberson 
1095f8a47341SAlan Cox /*
1096c869e672SAlan Cox  * Returns true if the given page belongs to a reservation and that page is
1097c869e672SAlan Cox  * free.  Otherwise, returns false.
1098c869e672SAlan Cox  */
1099c869e672SAlan Cox bool
1100c869e672SAlan Cox vm_reserv_is_page_free(vm_page_t m)
1101c869e672SAlan Cox {
1102c869e672SAlan Cox 	vm_reserv_t rv;
1103c869e672SAlan Cox 
1104c869e672SAlan Cox 	rv = vm_reserv_from_page(m);
1105c869e672SAlan Cox 	if (rv->object == NULL)
1106c869e672SAlan Cox 		return (false);
1107c869e672SAlan Cox 	return (popmap_is_clear(rv->popmap, m - rv->pages));
1108c869e672SAlan Cox }
1109c869e672SAlan Cox 
1110c869e672SAlan Cox /*
1111c869e672SAlan Cox  * If the given page belongs to a reservation, returns the level of that
1112c869e672SAlan Cox  * reservation.  Otherwise, returns -1.
1113c869e672SAlan Cox  */
1114c869e672SAlan Cox int
1115c869e672SAlan Cox vm_reserv_level(vm_page_t m)
1116c869e672SAlan Cox {
1117c869e672SAlan Cox 	vm_reserv_t rv;
1118c869e672SAlan Cox 
1119c869e672SAlan Cox 	rv = vm_reserv_from_page(m);
1120c869e672SAlan Cox 	return (rv->object != NULL ? 0 : -1);
1121c869e672SAlan Cox }
1122c869e672SAlan Cox 
1123c869e672SAlan Cox /*
11243453bca8SAlan Cox  * Returns a reservation level if the given page belongs to a fully populated
1125f8a47341SAlan Cox  * reservation and -1 otherwise.
1126f8a47341SAlan Cox  */
1127f8a47341SAlan Cox int
1128f8a47341SAlan Cox vm_reserv_level_iffullpop(vm_page_t m)
1129f8a47341SAlan Cox {
1130f8a47341SAlan Cox 	vm_reserv_t rv;
1131f8a47341SAlan Cox 
1132f8a47341SAlan Cox 	rv = vm_reserv_from_page(m);
1133f8a47341SAlan Cox 	return (rv->popcnt == VM_LEVEL_0_NPAGES ? 0 : -1);
1134f8a47341SAlan Cox }
1135f8a47341SAlan Cox 
1136f8a47341SAlan Cox /*
1137b378d296SMark Johnston  * Remove a partially populated reservation from the queue.
1138b378d296SMark Johnston  */
1139b378d296SMark Johnston static void
1140b378d296SMark Johnston vm_reserv_dequeue(vm_reserv_t rv)
1141b378d296SMark Johnston {
1142b378d296SMark Johnston 
1143b378d296SMark Johnston 	vm_reserv_domain_assert_locked(rv->domain);
1144b378d296SMark Johnston 	vm_reserv_assert_locked(rv);
1145b378d296SMark Johnston 	CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d",
1146b378d296SMark Johnston 	    __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq);
1147b378d296SMark Johnston 	KASSERT(rv->inpartpopq,
1148b378d296SMark Johnston 	    ("vm_reserv_reclaim: reserv %p's inpartpopq is FALSE", rv));
1149b378d296SMark Johnston 
1150b378d296SMark Johnston 	TAILQ_REMOVE(&vm_rvd[rv->domain].partpop, rv, partpopq);
1151b378d296SMark Johnston 	rv->inpartpopq = FALSE;
1152b378d296SMark Johnston }
1153b378d296SMark Johnston 
1154b378d296SMark Johnston /*
11553453bca8SAlan Cox  * Breaks the given partially populated reservation, releasing its free pages
11563453bca8SAlan Cox  * to the physical memory allocator.
1157f8a47341SAlan Cox  */
115844aab2c3SAlan Cox static void
115944aab2c3SAlan Cox vm_reserv_reclaim(vm_reserv_t rv)
1160f8a47341SAlan Cox {
1161f8a47341SAlan Cox 
11625c930c89SJeff Roberson 	vm_reserv_assert_locked(rv);
11635c930c89SJeff Roberson 	CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d",
11645c930c89SJeff Roberson 	    __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq);
1165b378d296SMark Johnston 	if (rv->inpartpopq) {
11665c930c89SJeff Roberson 		vm_reserv_domain_lock(rv->domain);
1167b378d296SMark Johnston 		vm_reserv_dequeue(rv);
11685c930c89SJeff Roberson 		vm_reserv_domain_unlock(rv->domain);
1169b378d296SMark Johnston 	}
1170ada27a3bSKonstantin Belousov 	vm_reserv_break(rv);
11715c930c89SJeff Roberson 	counter_u64_add(vm_reserv_reclaimed, 1);
117244aab2c3SAlan Cox }
117344aab2c3SAlan Cox 
117444aab2c3SAlan Cox /*
1175b378d296SMark Johnston  * Breaks a reservation near the head of the partially populated reservation
11763453bca8SAlan Cox  * queue, releasing its free pages to the physical memory allocator.  Returns
11773453bca8SAlan Cox  * TRUE if a reservation is broken and FALSE otherwise.
117844aab2c3SAlan Cox  */
1179b378d296SMark Johnston bool
1180ef435ae7SJeff Roberson vm_reserv_reclaim_inactive(int domain)
118144aab2c3SAlan Cox {
118244aab2c3SAlan Cox 	vm_reserv_t rv;
118344aab2c3SAlan Cox 
1184b378d296SMark Johnston 	vm_reserv_domain_lock(domain);
1185b378d296SMark Johnston 	TAILQ_FOREACH(rv, &vm_rvd[domain].partpop, partpopq) {
1186b378d296SMark Johnston 		/*
1187b378d296SMark Johnston 		 * A locked reservation is likely being updated or reclaimed,
1188b378d296SMark Johnston 		 * so just skip ahead.
1189b378d296SMark Johnston 		 */
1190b378d296SMark Johnston 		if (rv != &vm_rvd[domain].marker && vm_reserv_trylock(rv)) {
1191b378d296SMark Johnston 			vm_reserv_dequeue(rv);
1192b378d296SMark Johnston 			break;
11935c930c89SJeff Roberson 		}
1194b378d296SMark Johnston 	}
1195b378d296SMark Johnston 	vm_reserv_domain_unlock(domain);
1196b378d296SMark Johnston 	if (rv != NULL) {
119744aab2c3SAlan Cox 		vm_reserv_reclaim(rv);
11985c930c89SJeff Roberson 		vm_reserv_unlock(rv);
1199b378d296SMark Johnston 		return (true);
1200f8a47341SAlan Cox 	}
1201b378d296SMark Johnston 	return (false);
1202f8a47341SAlan Cox }
1203f8a47341SAlan Cox 
1204f8a47341SAlan Cox /*
1205f96e8a0bSDoug Moore  * Determine whether this reservation has free pages that satisfy the given
1206f96e8a0bSDoug Moore  * request for contiguous physical memory.  Start searching from the lower
1207f96e8a0bSDoug Moore  * bound, defined by low_index.
1208f96e8a0bSDoug Moore  */
1209f96e8a0bSDoug Moore static bool
1210f96e8a0bSDoug Moore vm_reserv_test_contig(vm_reserv_t rv, u_long npages, vm_paddr_t low,
1211f96e8a0bSDoug Moore     vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
1212f96e8a0bSDoug Moore {
1213f96e8a0bSDoug Moore 	vm_paddr_t pa, size;
1214f96e8a0bSDoug Moore 	u_long changes;
1215f96e8a0bSDoug Moore 	int bitpos, bits_left, i, hi, lo, n;
1216f96e8a0bSDoug Moore 
1217f96e8a0bSDoug Moore 	vm_reserv_assert_locked(rv);
1218f96e8a0bSDoug Moore 	size = npages << PAGE_SHIFT;
1219f96e8a0bSDoug Moore 	pa = VM_PAGE_TO_PHYS(&rv->pages[0]);
1220f96e8a0bSDoug Moore 	lo = (pa < low) ?
1221f96e8a0bSDoug Moore 	    ((low + PAGE_MASK - pa) >> PAGE_SHIFT) : 0;
1222f96e8a0bSDoug Moore 	i = lo / NBPOPMAP;
1223f96e8a0bSDoug Moore 	changes = rv->popmap[i] | ((1UL << (lo % NBPOPMAP)) - 1);
1224f96e8a0bSDoug Moore 	hi = (pa + VM_LEVEL_0_SIZE > high) ?
1225f96e8a0bSDoug Moore 	    ((high + PAGE_MASK - pa) >> PAGE_SHIFT) : VM_LEVEL_0_NPAGES;
1226f96e8a0bSDoug Moore 	n = hi / NBPOPMAP;
1227f96e8a0bSDoug Moore 	bits_left = hi % NBPOPMAP;
1228f96e8a0bSDoug Moore 	hi = lo = -1;
1229f96e8a0bSDoug Moore 	for (;;) {
1230f96e8a0bSDoug Moore 		/*
1231f96e8a0bSDoug Moore 		 * "changes" is a bitmask that marks where a new sequence of
1232f96e8a0bSDoug Moore 		 * 0s or 1s begins in popmap[i], with last bit in popmap[i-1]
1233f96e8a0bSDoug Moore 		 * considered to be 1 if and only if lo == hi.  The bits of
1234f96e8a0bSDoug Moore 		 * popmap[-1] and popmap[NPOPMAP] are considered all 1s.
1235f96e8a0bSDoug Moore 		 */
1236f96e8a0bSDoug Moore 		changes ^= (changes << 1) | (lo == hi);
1237f96e8a0bSDoug Moore 		while (changes != 0) {
1238f96e8a0bSDoug Moore 			/*
1239f96e8a0bSDoug Moore 			 * If the next change marked begins a run of 0s, set
1240f96e8a0bSDoug Moore 			 * lo to mark that position.  Otherwise set hi and
1241f96e8a0bSDoug Moore 			 * look for a satisfactory first page from lo up to hi.
1242f96e8a0bSDoug Moore 			 */
1243f96e8a0bSDoug Moore 			bitpos = ffsl(changes) - 1;
1244f96e8a0bSDoug Moore 			changes ^= 1UL << bitpos;
1245f96e8a0bSDoug Moore 			if (lo == hi) {
1246f96e8a0bSDoug Moore 				lo = NBPOPMAP * i + bitpos;
1247f96e8a0bSDoug Moore 				continue;
1248f96e8a0bSDoug Moore 			}
1249f96e8a0bSDoug Moore 			hi = NBPOPMAP * i + bitpos;
1250f96e8a0bSDoug Moore 			pa = VM_PAGE_TO_PHYS(&rv->pages[lo]);
1251f96e8a0bSDoug Moore 			if ((pa & (alignment - 1)) != 0) {
1252f96e8a0bSDoug Moore 				/* Skip to next aligned page. */
1253f96e8a0bSDoug Moore 				lo += (((pa - 1) | (alignment - 1)) + 1) >>
1254f96e8a0bSDoug Moore 				    PAGE_SHIFT;
1255f96e8a0bSDoug Moore 				if (lo >= VM_LEVEL_0_NPAGES)
1256f96e8a0bSDoug Moore 					return (false);
1257f96e8a0bSDoug Moore 				pa = VM_PAGE_TO_PHYS(&rv->pages[lo]);
1258f96e8a0bSDoug Moore 			}
1259f96e8a0bSDoug Moore 			if (((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0) {
1260f96e8a0bSDoug Moore 				/* Skip to next boundary-matching page. */
1261f96e8a0bSDoug Moore 				lo += (((pa - 1) | (boundary - 1)) + 1) >>
1262f96e8a0bSDoug Moore 				    PAGE_SHIFT;
1263f96e8a0bSDoug Moore 				if (lo >= VM_LEVEL_0_NPAGES)
1264f96e8a0bSDoug Moore 					return (false);
1265f96e8a0bSDoug Moore 				pa = VM_PAGE_TO_PHYS(&rv->pages[lo]);
1266f96e8a0bSDoug Moore 			}
1267f96e8a0bSDoug Moore 			if (lo * PAGE_SIZE + size <= hi * PAGE_SIZE)
1268f96e8a0bSDoug Moore 				return (true);
1269f96e8a0bSDoug Moore 			lo = hi;
1270f96e8a0bSDoug Moore 		}
1271f96e8a0bSDoug Moore 		if (++i < n)
1272f96e8a0bSDoug Moore 			changes = rv->popmap[i];
1273f96e8a0bSDoug Moore 		else if (i == n)
1274f96e8a0bSDoug Moore 			changes = bits_left == 0 ? -1UL :
1275f96e8a0bSDoug Moore 			    (rv->popmap[n] | (-1UL << bits_left));
1276f96e8a0bSDoug Moore 		else
1277f96e8a0bSDoug Moore 			return (false);
1278f96e8a0bSDoug Moore 	}
1279f96e8a0bSDoug Moore }
1280f96e8a0bSDoug Moore 
1281f96e8a0bSDoug Moore /*
12823453bca8SAlan Cox  * Searches the partially populated reservation queue for the least recently
12833453bca8SAlan Cox  * changed reservation with free pages that satisfy the given request for
12843453bca8SAlan Cox  * contiguous physical memory.  If a satisfactory reservation is found, it is
1285f96e8a0bSDoug Moore  * broken.  Returns true if a reservation is broken and false otherwise.
128644aab2c3SAlan Cox  */
1287b378d296SMark Johnston bool
1288ef435ae7SJeff Roberson vm_reserv_reclaim_contig(int domain, u_long npages, vm_paddr_t low,
1289ef435ae7SJeff Roberson     vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
129044aab2c3SAlan Cox {
1291b378d296SMark Johnston 	struct vm_reserv_queue *queue;
1292ec179322SAlan Cox 	vm_paddr_t pa, size;
1293b378d296SMark Johnston 	vm_reserv_t marker, rv, rvn;
129444aab2c3SAlan Cox 
1295c68c3537SAlan Cox 	if (npages > VM_LEVEL_0_NPAGES - 1)
1296f96e8a0bSDoug Moore 		return (false);
1297b378d296SMark Johnston 	marker = &vm_rvd[domain].marker;
1298b378d296SMark Johnston 	queue = &vm_rvd[domain].partpop;
1299c68c3537SAlan Cox 	size = npages << PAGE_SHIFT;
1300b378d296SMark Johnston 
1301b378d296SMark Johnston 	vm_reserv_domain_scan_lock(domain);
13025c930c89SJeff Roberson 	vm_reserv_domain_lock(domain);
1303b378d296SMark Johnston 	TAILQ_FOREACH_SAFE(rv, queue, partpopq, rvn) {
1304f96e8a0bSDoug Moore 		pa = VM_PAGE_TO_PHYS(&rv->pages[0]);
1305f96e8a0bSDoug Moore 		if (pa + VM_LEVEL_0_SIZE - size < low) {
1306ec179322SAlan Cox 			/* This entire reservation is too low; go to next. */
130744aab2c3SAlan Cox 			continue;
130844aab2c3SAlan Cox 		}
130944aab2c3SAlan Cox 		if (pa + size > high) {
1310ec179322SAlan Cox 			/* This entire reservation is too high; go to next. */
1311ec179322SAlan Cox 			continue;
131285f2a0c9SMax Laier 		}
1313b378d296SMark Johnston 
13145c930c89SJeff Roberson 		if (vm_reserv_trylock(rv) == 0) {
1315b378d296SMark Johnston 			TAILQ_INSERT_AFTER(queue, rv, marker, partpopq);
13165c930c89SJeff Roberson 			vm_reserv_domain_unlock(domain);
13175c930c89SJeff Roberson 			vm_reserv_lock(rv);
1318b378d296SMark Johnston 			if (!rv->inpartpopq ||
1319b378d296SMark Johnston 			    TAILQ_NEXT(rv, partpopq) != marker) {
1320b378d296SMark Johnston 				vm_reserv_unlock(rv);
13215c930c89SJeff Roberson 				vm_reserv_domain_lock(domain);
1322b378d296SMark Johnston 				rvn = TAILQ_NEXT(marker, partpopq);
1323b378d296SMark Johnston 				TAILQ_REMOVE(queue, marker, partpopq);
13245c930c89SJeff Roberson 				continue;
13255c930c89SJeff Roberson 			}
1326b378d296SMark Johnston 			vm_reserv_domain_lock(domain);
1327b378d296SMark Johnston 			TAILQ_REMOVE(queue, marker, partpopq);
1328b378d296SMark Johnston 		}
13295c930c89SJeff Roberson 		vm_reserv_domain_unlock(domain);
1330f96e8a0bSDoug Moore 		if (vm_reserv_test_contig(rv, npages, low, high,
1331f96e8a0bSDoug Moore 		    alignment, boundary)) {
1332b378d296SMark Johnston 			vm_reserv_domain_scan_unlock(domain);
133344aab2c3SAlan Cox 			vm_reserv_reclaim(rv);
13345c930c89SJeff Roberson 			vm_reserv_unlock(rv);
1335f96e8a0bSDoug Moore 			return (true);
133644aab2c3SAlan Cox 		}
13375c930c89SJeff Roberson 		vm_reserv_unlock(rv);
13385c930c89SJeff Roberson 		vm_reserv_domain_lock(domain);
133944aab2c3SAlan Cox 	}
13405c930c89SJeff Roberson 	vm_reserv_domain_unlock(domain);
1341b378d296SMark Johnston 	vm_reserv_domain_scan_unlock(domain);
1342f96e8a0bSDoug Moore 	return (false);
134344aab2c3SAlan Cox }
134444aab2c3SAlan Cox 
134544aab2c3SAlan Cox /*
1346f8a47341SAlan Cox  * Transfers the reservation underlying the given page to a new object.
1347f8a47341SAlan Cox  *
1348f8a47341SAlan Cox  * The object must be locked.
1349f8a47341SAlan Cox  */
1350f8a47341SAlan Cox void
1351f8a47341SAlan Cox vm_reserv_rename(vm_page_t m, vm_object_t new_object, vm_object_t old_object,
1352f8a47341SAlan Cox     vm_pindex_t old_object_offset)
1353f8a47341SAlan Cox {
1354f8a47341SAlan Cox 	vm_reserv_t rv;
1355f8a47341SAlan Cox 
135689f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(new_object);
1357f8a47341SAlan Cox 	rv = vm_reserv_from_page(m);
1358f8a47341SAlan Cox 	if (rv->object == old_object) {
13595c930c89SJeff Roberson 		vm_reserv_lock(rv);
13605c930c89SJeff Roberson 		CTR6(KTR_VM,
13615c930c89SJeff Roberson 		    "%s: rv %p object %p new %p popcnt %d inpartpop %d",
13625c930c89SJeff Roberson 		    __FUNCTION__, rv, rv->object, new_object, rv->popcnt,
13635c930c89SJeff Roberson 		    rv->inpartpopq);
1364f8a47341SAlan Cox 		if (rv->object == old_object) {
1365e2068d0bSJeff Roberson 			vm_reserv_object_lock(old_object);
1366e2068d0bSJeff Roberson 			rv->object = NULL;
1367f8a47341SAlan Cox 			LIST_REMOVE(rv, objq);
1368e2068d0bSJeff Roberson 			vm_reserv_object_unlock(old_object);
1369e2068d0bSJeff Roberson 			vm_reserv_object_lock(new_object);
1370f8a47341SAlan Cox 			rv->object = new_object;
1371f8a47341SAlan Cox 			rv->pindex -= old_object_offset;
1372e2068d0bSJeff Roberson 			LIST_INSERT_HEAD(&new_object->rvq, rv, objq);
1373e2068d0bSJeff Roberson 			vm_reserv_object_unlock(new_object);
1374f8a47341SAlan Cox 		}
13755c930c89SJeff Roberson 		vm_reserv_unlock(rv);
1376f8a47341SAlan Cox 	}
1377f8a47341SAlan Cox }
1378f8a47341SAlan Cox 
1379f8a47341SAlan Cox /*
1380c869e672SAlan Cox  * Returns the size (in bytes) of a reservation of the specified level.
1381c869e672SAlan Cox  */
1382c869e672SAlan Cox int
1383c869e672SAlan Cox vm_reserv_size(int level)
1384c869e672SAlan Cox {
1385c869e672SAlan Cox 
1386c869e672SAlan Cox 	switch (level) {
1387c869e672SAlan Cox 	case 0:
1388c869e672SAlan Cox 		return (VM_LEVEL_0_SIZE);
1389c869e672SAlan Cox 	case -1:
1390c869e672SAlan Cox 		return (PAGE_SIZE);
1391c869e672SAlan Cox 	default:
1392c869e672SAlan Cox 		return (0);
1393c869e672SAlan Cox 	}
1394c869e672SAlan Cox }
1395c869e672SAlan Cox 
1396c869e672SAlan Cox /*
1397f8a47341SAlan Cox  * Allocates the virtual and physical memory required by the reservation
1398f8a47341SAlan Cox  * management system's data structures, in particular, the reservation array.
1399f8a47341SAlan Cox  */
1400f8a47341SAlan Cox vm_paddr_t
14013e5e1b51SJeff Roberson vm_reserv_startup(vm_offset_t *vaddr, vm_paddr_t end)
1402f8a47341SAlan Cox {
14033e5e1b51SJeff Roberson 	vm_paddr_t new_end, high_water;
1404f8a47341SAlan Cox 	size_t size;
14053e5e1b51SJeff Roberson 	int i;
14063e5e1b51SJeff Roberson 
14073e5e1b51SJeff Roberson 	high_water = phys_avail[1];
14083e5e1b51SJeff Roberson 	for (i = 0; i < vm_phys_nsegs; i++) {
14093e5e1b51SJeff Roberson 		if (vm_phys_segs[i].end > high_water)
14103e5e1b51SJeff Roberson 			high_water = vm_phys_segs[i].end;
14113e5e1b51SJeff Roberson 	}
14123e5e1b51SJeff Roberson 
14133e5e1b51SJeff Roberson 	/* Skip the first chunk.  It is already accounted for. */
14143e5e1b51SJeff Roberson 	for (i = 2; phys_avail[i + 1] != 0; i += 2) {
14153e5e1b51SJeff Roberson 		if (phys_avail[i + 1] > high_water)
14163e5e1b51SJeff Roberson 			high_water = phys_avail[i + 1];
14173e5e1b51SJeff Roberson 	}
1418f8a47341SAlan Cox 
1419f8a47341SAlan Cox 	/*
1420f8a47341SAlan Cox 	 * Calculate the size (in bytes) of the reservation array.  Round up
1421f8a47341SAlan Cox 	 * from "high_water" because every small page is mapped to an element
1422f8a47341SAlan Cox 	 * in the reservation array based on its physical address.  Thus, the
1423f8a47341SAlan Cox 	 * number of elements in the reservation array can be greater than the
1424f8a47341SAlan Cox 	 * number of superpages.
1425f8a47341SAlan Cox 	 */
1426f8a47341SAlan Cox 	size = howmany(high_water, VM_LEVEL_0_SIZE) * sizeof(struct vm_reserv);
1427f8a47341SAlan Cox 
1428f8a47341SAlan Cox 	/*
1429f8a47341SAlan Cox 	 * Allocate and map the physical memory for the reservation array.  The
1430f8a47341SAlan Cox 	 * next available virtual address is returned by reference.
1431f8a47341SAlan Cox 	 */
1432f8a47341SAlan Cox 	new_end = end - round_page(size);
1433f8a47341SAlan Cox 	vm_reserv_array = (void *)(uintptr_t)pmap_map(vaddr, new_end, end,
1434f8a47341SAlan Cox 	    VM_PROT_READ | VM_PROT_WRITE);
1435f8a47341SAlan Cox 	bzero(vm_reserv_array, size);
1436f8a47341SAlan Cox 
1437f8a47341SAlan Cox 	/*
1438f8a47341SAlan Cox 	 * Return the next available physical address.
1439f8a47341SAlan Cox 	 */
1440f8a47341SAlan Cox 	return (new_end);
1441f8a47341SAlan Cox }
1442f8a47341SAlan Cox 
14438b5e1472SAlan Cox /*
14448b5e1472SAlan Cox  * Returns the superpage containing the given page.
14458b5e1472SAlan Cox  */
14468b5e1472SAlan Cox vm_page_t
14478b5e1472SAlan Cox vm_reserv_to_superpage(vm_page_t m)
14488b5e1472SAlan Cox {
14498b5e1472SAlan Cox 	vm_reserv_t rv;
14508b5e1472SAlan Cox 
14518b5e1472SAlan Cox 	VM_OBJECT_ASSERT_LOCKED(m->object);
14528b5e1472SAlan Cox 	rv = vm_reserv_from_page(m);
14535c930c89SJeff Roberson 	if (rv->object == m->object && rv->popcnt == VM_LEVEL_0_NPAGES)
14545c930c89SJeff Roberson 		m = rv->pages;
14555c930c89SJeff Roberson 	else
14565c930c89SJeff Roberson 		m = NULL;
14575c930c89SJeff Roberson 
14585c930c89SJeff Roberson 	return (m);
14598b5e1472SAlan Cox }
14608b5e1472SAlan Cox 
1461f8a47341SAlan Cox #endif	/* VM_NRESERVLEVEL > 0 */
1462