xref: /freebsd/sys/vm/vm_reserv.c (revision ef435ae7de5e4f25bfc9d1cdd92c421ad7b9793f)
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>
569ed01c32SGleb Smirnoff #include <sys/vmmeter.h>
57f8a47341SAlan Cox 
58f8a47341SAlan Cox #include <vm/vm.h>
59f8a47341SAlan Cox #include <vm/vm_param.h>
60f8a47341SAlan Cox #include <vm/vm_object.h>
61f8a47341SAlan Cox #include <vm/vm_page.h>
62f8a47341SAlan Cox #include <vm/vm_phys.h>
63774d251dSAttilio Rao #include <vm/vm_radix.h>
64f8a47341SAlan Cox #include <vm/vm_reserv.h>
65f8a47341SAlan Cox 
66f8a47341SAlan Cox /*
67f8a47341SAlan Cox  * The reservation system supports the speculative allocation of large physical
683453bca8SAlan Cox  * pages ("superpages").  Speculative allocation enables the fully automatic
69f8a47341SAlan Cox  * utilization of superpages by the virtual memory system.  In other words, no
70f8a47341SAlan Cox  * programmatic directives are required to use superpages.
71f8a47341SAlan Cox  */
72f8a47341SAlan Cox 
73f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
74f8a47341SAlan Cox 
75f8a47341SAlan Cox /*
76f8a47341SAlan Cox  * The number of small pages that are contained in a level 0 reservation
77f8a47341SAlan Cox  */
78f8a47341SAlan Cox #define	VM_LEVEL_0_NPAGES	(1 << VM_LEVEL_0_ORDER)
79f8a47341SAlan Cox 
80f8a47341SAlan Cox /*
81f8a47341SAlan Cox  * The number of bits by which a physical address is shifted to obtain the
82f8a47341SAlan Cox  * reservation number
83f8a47341SAlan Cox  */
84f8a47341SAlan Cox #define	VM_LEVEL_0_SHIFT	(VM_LEVEL_0_ORDER + PAGE_SHIFT)
85f8a47341SAlan Cox 
86f8a47341SAlan Cox /*
87f8a47341SAlan Cox  * The size of a level 0 reservation in bytes
88f8a47341SAlan Cox  */
89f8a47341SAlan Cox #define	VM_LEVEL_0_SIZE		(1 << VM_LEVEL_0_SHIFT)
90f8a47341SAlan Cox 
91f8a47341SAlan Cox /*
92f8a47341SAlan Cox  * Computes the index of the small page underlying the given (object, pindex)
93f8a47341SAlan Cox  * within the reservation's array of small pages.
94f8a47341SAlan Cox  */
95f8a47341SAlan Cox #define	VM_RESERV_INDEX(object, pindex)	\
96f8a47341SAlan Cox     (((object)->pg_color + (pindex)) & (VM_LEVEL_0_NPAGES - 1))
97f8a47341SAlan Cox 
98f8a47341SAlan Cox /*
99ec179322SAlan Cox  * The size of a population map entry
100ec179322SAlan Cox  */
101ec179322SAlan Cox typedef	u_long		popmap_t;
102ec179322SAlan Cox 
103ec179322SAlan Cox /*
104ec179322SAlan Cox  * The number of bits in a population map entry
105ec179322SAlan Cox  */
106ec179322SAlan Cox #define	NBPOPMAP	(NBBY * sizeof(popmap_t))
107ec179322SAlan Cox 
108ec179322SAlan Cox /*
109ec179322SAlan Cox  * The number of population map entries in a reservation
110ec179322SAlan Cox  */
111ec179322SAlan Cox #define	NPOPMAP		howmany(VM_LEVEL_0_NPAGES, NBPOPMAP)
112ec179322SAlan Cox 
113ec179322SAlan Cox /*
1143180f757SAlan Cox  * Clear a bit in the population map.
1153180f757SAlan Cox  */
1163180f757SAlan Cox static __inline void
1173180f757SAlan Cox popmap_clear(popmap_t popmap[], int i)
1183180f757SAlan Cox {
1193180f757SAlan Cox 
1203180f757SAlan Cox 	popmap[i / NBPOPMAP] &= ~(1UL << (i % NBPOPMAP));
1213180f757SAlan Cox }
1223180f757SAlan Cox 
1233180f757SAlan Cox /*
1243180f757SAlan Cox  * Set a bit in the population map.
1253180f757SAlan Cox  */
1263180f757SAlan Cox static __inline void
1273180f757SAlan Cox popmap_set(popmap_t popmap[], int i)
1283180f757SAlan Cox {
1293180f757SAlan Cox 
1303180f757SAlan Cox 	popmap[i / NBPOPMAP] |= 1UL << (i % NBPOPMAP);
1313180f757SAlan Cox }
1323180f757SAlan Cox 
1333180f757SAlan Cox /*
1343180f757SAlan Cox  * Is a bit in the population map clear?
1353180f757SAlan Cox  */
1363180f757SAlan Cox static __inline boolean_t
1373180f757SAlan Cox popmap_is_clear(popmap_t popmap[], int i)
1383180f757SAlan Cox {
1393180f757SAlan Cox 
1403180f757SAlan Cox 	return ((popmap[i / NBPOPMAP] & (1UL << (i % NBPOPMAP))) == 0);
1413180f757SAlan Cox }
1423180f757SAlan Cox 
1433180f757SAlan Cox /*
1443180f757SAlan Cox  * Is a bit in the population map set?
1453180f757SAlan Cox  */
1463180f757SAlan Cox static __inline boolean_t
1473180f757SAlan Cox popmap_is_set(popmap_t popmap[], int i)
1483180f757SAlan Cox {
1493180f757SAlan Cox 
1503180f757SAlan Cox 	return ((popmap[i / NBPOPMAP] & (1UL << (i % NBPOPMAP))) != 0);
1513180f757SAlan Cox }
1523180f757SAlan Cox 
1533180f757SAlan Cox /*
154f8a47341SAlan Cox  * The reservation structure
155f8a47341SAlan Cox  *
156f8a47341SAlan Cox  * A reservation structure is constructed whenever a large physical page is
157f8a47341SAlan Cox  * speculatively allocated to an object.  The reservation provides the small
158f8a47341SAlan Cox  * physical pages for the range [pindex, pindex + VM_LEVEL_0_NPAGES) of offsets
159f8a47341SAlan Cox  * within that object.  The reservation's "popcnt" tracks the number of these
160f8a47341SAlan Cox  * small physical pages that are in use at any given time.  When and if the
1613453bca8SAlan Cox  * reservation is not fully utilized, it appears in the queue of partially
162f8a47341SAlan Cox  * populated reservations.  The reservation always appears on the containing
163f8a47341SAlan Cox  * object's list of reservations.
164f8a47341SAlan Cox  *
1653453bca8SAlan Cox  * A partially populated reservation can be broken and reclaimed at any time.
166f8a47341SAlan Cox  */
167f8a47341SAlan Cox struct vm_reserv {
168f8a47341SAlan Cox 	TAILQ_ENTRY(vm_reserv) partpopq;
169f8a47341SAlan Cox 	LIST_ENTRY(vm_reserv) objq;
170f8a47341SAlan Cox 	vm_object_t	object;			/* containing object */
171f8a47341SAlan Cox 	vm_pindex_t	pindex;			/* offset within object */
172f8a47341SAlan Cox 	vm_page_t	pages;			/* first page of a superpage */
173*ef435ae7SJeff Roberson 	int		domain;			/* NUMA domain */
174f8a47341SAlan Cox 	int		popcnt;			/* # of pages in use */
175f8a47341SAlan Cox 	char		inpartpopq;
176ec179322SAlan Cox 	popmap_t	popmap[NPOPMAP];	/* bit vector of used pages */
177f8a47341SAlan Cox };
178f8a47341SAlan Cox 
179f8a47341SAlan Cox /*
180f8a47341SAlan Cox  * The reservation array
181f8a47341SAlan Cox  *
182f8a47341SAlan Cox  * This array is analoguous in function to vm_page_array.  It differs in the
183f8a47341SAlan Cox  * respect that it may contain a greater number of useful reservation
184f8a47341SAlan Cox  * structures than there are (physical) superpages.  These "invalid"
185f8a47341SAlan Cox  * reservation structures exist to trade-off space for time in the
186f8a47341SAlan Cox  * implementation of vm_reserv_from_page().  Invalid reservation structures are
187f8a47341SAlan Cox  * distinguishable from "valid" reservation structures by inspecting the
188f8a47341SAlan Cox  * reservation's "pages" field.  Invalid reservation structures have a NULL
189f8a47341SAlan Cox  * "pages" field.
190f8a47341SAlan Cox  *
191f8a47341SAlan Cox  * vm_reserv_from_page() maps a small (physical) page to an element of this
192f8a47341SAlan Cox  * array by computing a physical reservation number from the page's physical
193f8a47341SAlan Cox  * address.  The physical reservation number is used as the array index.
194f8a47341SAlan Cox  *
195f8a47341SAlan Cox  * An "active" reservation is a valid reservation structure that has a non-NULL
196f8a47341SAlan Cox  * "object" field and a non-zero "popcnt" field.  In other words, every active
197f8a47341SAlan Cox  * reservation belongs to a particular object.  Moreover, every active
198f8a47341SAlan Cox  * reservation has an entry in the containing object's list of reservations.
199f8a47341SAlan Cox  */
200f8a47341SAlan Cox static vm_reserv_t vm_reserv_array;
201f8a47341SAlan Cox 
202f8a47341SAlan Cox /*
2033453bca8SAlan Cox  * The partially populated reservation queue
204f8a47341SAlan Cox  *
2053453bca8SAlan Cox  * This queue enables the fast recovery of an unused free small page from a
2063453bca8SAlan Cox  * partially populated reservation.  The reservation at the head of this queue
2073453bca8SAlan Cox  * is the least recently changed, partially populated reservation.
208f8a47341SAlan Cox  *
209f8a47341SAlan Cox  * Access to this queue is synchronized by the free page queue lock.
210f8a47341SAlan Cox  */
211*ef435ae7SJeff Roberson static TAILQ_HEAD(, vm_reserv) vm_rvq_partpop[MAXMEMDOM];
212f8a47341SAlan Cox 
213f8a47341SAlan Cox static SYSCTL_NODE(_vm, OID_AUTO, reserv, CTLFLAG_RD, 0, "Reservation Info");
214f8a47341SAlan Cox 
215f8a47341SAlan Cox static long vm_reserv_broken;
216f8a47341SAlan Cox SYSCTL_LONG(_vm_reserv, OID_AUTO, broken, CTLFLAG_RD,
217f8a47341SAlan Cox     &vm_reserv_broken, 0, "Cumulative number of broken reservations");
218f8a47341SAlan Cox 
219f8a47341SAlan Cox static long vm_reserv_freed;
220f8a47341SAlan Cox SYSCTL_LONG(_vm_reserv, OID_AUTO, freed, CTLFLAG_RD,
221f8a47341SAlan Cox     &vm_reserv_freed, 0, "Cumulative number of freed reservations");
222f8a47341SAlan Cox 
223e0a63baaSAlan Cox static int sysctl_vm_reserv_fullpop(SYSCTL_HANDLER_ARGS);
224e0a63baaSAlan Cox 
225e0a63baaSAlan Cox SYSCTL_PROC(_vm_reserv, OID_AUTO, fullpop, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
226e0a63baaSAlan Cox     sysctl_vm_reserv_fullpop, "I", "Current number of full reservations");
227e0a63baaSAlan Cox 
228f8a47341SAlan Cox static int sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS);
229f8a47341SAlan Cox 
230f8a47341SAlan Cox SYSCTL_OID(_vm_reserv, OID_AUTO, partpopq, CTLTYPE_STRING | CTLFLAG_RD, NULL, 0,
2313453bca8SAlan Cox     sysctl_vm_reserv_partpopq, "A", "Partially populated reservation queues");
232f8a47341SAlan Cox 
233f8a47341SAlan Cox static long vm_reserv_reclaimed;
234f8a47341SAlan Cox SYSCTL_LONG(_vm_reserv, OID_AUTO, reclaimed, CTLFLAG_RD,
235f8a47341SAlan Cox     &vm_reserv_reclaimed, 0, "Cumulative number of reclaimed reservations");
236f8a47341SAlan Cox 
237ec179322SAlan Cox static void		vm_reserv_break(vm_reserv_t rv, vm_page_t m);
238ec179322SAlan Cox static void		vm_reserv_depopulate(vm_reserv_t rv, int index);
239f8a47341SAlan Cox static vm_reserv_t	vm_reserv_from_page(vm_page_t m);
240f8a47341SAlan Cox static boolean_t	vm_reserv_has_pindex(vm_reserv_t rv,
241f8a47341SAlan Cox 			    vm_pindex_t pindex);
242ec179322SAlan Cox static void		vm_reserv_populate(vm_reserv_t rv, int index);
24344aab2c3SAlan Cox static void		vm_reserv_reclaim(vm_reserv_t rv);
244f8a47341SAlan Cox 
245f8a47341SAlan Cox /*
246e0a63baaSAlan Cox  * Returns the current number of full reservations.
247e0a63baaSAlan Cox  *
248e0a63baaSAlan Cox  * Since the number of full reservations is computed without acquiring the
249e0a63baaSAlan Cox  * free page queue lock, the returned value may be inexact.
250e0a63baaSAlan Cox  */
251e0a63baaSAlan Cox static int
252e0a63baaSAlan Cox sysctl_vm_reserv_fullpop(SYSCTL_HANDLER_ARGS)
253e0a63baaSAlan Cox {
254e0a63baaSAlan Cox 	vm_paddr_t paddr;
255e0a63baaSAlan Cox 	struct vm_phys_seg *seg;
256e0a63baaSAlan Cox 	vm_reserv_t rv;
257e0a63baaSAlan Cox 	int fullpop, segind;
258e0a63baaSAlan Cox 
259e0a63baaSAlan Cox 	fullpop = 0;
260e0a63baaSAlan Cox 	for (segind = 0; segind < vm_phys_nsegs; segind++) {
261e0a63baaSAlan Cox 		seg = &vm_phys_segs[segind];
262e0a63baaSAlan Cox 		paddr = roundup2(seg->start, VM_LEVEL_0_SIZE);
263e0a63baaSAlan Cox 		while (paddr + VM_LEVEL_0_SIZE <= seg->end) {
264e0a63baaSAlan Cox 			rv = &vm_reserv_array[paddr >> VM_LEVEL_0_SHIFT];
265e0a63baaSAlan Cox 			fullpop += rv->popcnt == VM_LEVEL_0_NPAGES;
266e0a63baaSAlan Cox 			paddr += VM_LEVEL_0_SIZE;
267e0a63baaSAlan Cox 		}
268e0a63baaSAlan Cox 	}
269e0a63baaSAlan Cox 	return (sysctl_handle_int(oidp, &fullpop, 0, req));
270e0a63baaSAlan Cox }
271e0a63baaSAlan Cox 
272e0a63baaSAlan Cox /*
2733453bca8SAlan Cox  * Describes the current state of the partially populated reservation queue.
274f8a47341SAlan Cox  */
275f8a47341SAlan Cox static int
276f8a47341SAlan Cox sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS)
277f8a47341SAlan Cox {
278f8a47341SAlan Cox 	struct sbuf sbuf;
279f8a47341SAlan Cox 	vm_reserv_t rv;
280*ef435ae7SJeff Roberson 	int counter, error, domain, level, unused_pages;
281f8a47341SAlan Cox 
28200f0e671SMatthew D Fleming 	error = sysctl_wire_old_buffer(req, 0);
28300f0e671SMatthew D Fleming 	if (error != 0)
28400f0e671SMatthew D Fleming 		return (error);
2854e657159SMatthew D Fleming 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
286*ef435ae7SJeff Roberson 	sbuf_printf(&sbuf, "\nDOMAIN    LEVEL     SIZE  NUMBER\n\n");
287*ef435ae7SJeff Roberson 	for (domain = 0; domain < vm_ndomains; domain++) {
288f8a47341SAlan Cox 		for (level = -1; level <= VM_NRESERVLEVEL - 2; level++) {
289f8a47341SAlan Cox 			counter = 0;
290f8a47341SAlan Cox 			unused_pages = 0;
291f8a47341SAlan Cox 			mtx_lock(&vm_page_queue_free_mtx);
292*ef435ae7SJeff Roberson 			TAILQ_FOREACH(rv, &vm_rvq_partpop[domain], partpopq) {
293f8a47341SAlan Cox 				counter++;
294f8a47341SAlan Cox 				unused_pages += VM_LEVEL_0_NPAGES - rv->popcnt;
295f8a47341SAlan Cox 			}
296f8a47341SAlan Cox 			mtx_unlock(&vm_page_queue_free_mtx);
297*ef435ae7SJeff Roberson 			sbuf_printf(&sbuf, "%6d, %7d, %6dK, %6d\n",
298*ef435ae7SJeff Roberson 			    domain, level,
2992cf36c8fSAlan Cox 			    unused_pages * ((int)PAGE_SIZE / 1024), counter);
300f8a47341SAlan Cox 		}
301*ef435ae7SJeff Roberson 	}
3024e657159SMatthew D Fleming 	error = sbuf_finish(&sbuf);
303f8a47341SAlan Cox 	sbuf_delete(&sbuf);
304f8a47341SAlan Cox 	return (error);
305f8a47341SAlan Cox }
306f8a47341SAlan Cox 
307f8a47341SAlan Cox /*
308f8a47341SAlan Cox  * Reduces the given reservation's population count.  If the population count
309f8a47341SAlan Cox  * becomes zero, the reservation is destroyed.  Additionally, moves the
3103453bca8SAlan Cox  * reservation to the tail of the partially populated reservation queue if the
311f8a47341SAlan Cox  * population count is non-zero.
312f8a47341SAlan Cox  *
313f8a47341SAlan Cox  * The free page queue lock must be held.
314f8a47341SAlan Cox  */
315f8a47341SAlan Cox static void
316ec179322SAlan Cox vm_reserv_depopulate(vm_reserv_t rv, int index)
317f8a47341SAlan Cox {
318f8a47341SAlan Cox 
319f8a47341SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
320f8a47341SAlan Cox 	KASSERT(rv->object != NULL,
321f8a47341SAlan Cox 	    ("vm_reserv_depopulate: reserv %p is free", rv));
3223180f757SAlan Cox 	KASSERT(popmap_is_set(rv->popmap, index),
323a08c1515SAlan Cox 	    ("vm_reserv_depopulate: reserv %p's popmap[%d] is clear", rv,
324a08c1515SAlan Cox 	    index));
325f8a47341SAlan Cox 	KASSERT(rv->popcnt > 0,
326f8a47341SAlan Cox 	    ("vm_reserv_depopulate: reserv %p's popcnt is corrupted", rv));
327*ef435ae7SJeff Roberson 	KASSERT(rv->domain >= 0 && rv->domain < vm_ndomains,
328*ef435ae7SJeff Roberson 	    ("vm_reserv_depopulate: reserv %p's domain is corrupted %d",
329*ef435ae7SJeff Roberson 	    rv, rv->domain));
330f8a47341SAlan Cox 	if (rv->inpartpopq) {
331*ef435ae7SJeff Roberson 		TAILQ_REMOVE(&vm_rvq_partpop[rv->domain], rv, partpopq);
332f8a47341SAlan Cox 		rv->inpartpopq = FALSE;
333dd05fa19SAlan Cox 	} else {
334dd05fa19SAlan Cox 		KASSERT(rv->pages->psind == 1,
335dd05fa19SAlan Cox 		    ("vm_reserv_depopulate: reserv %p is already demoted",
336dd05fa19SAlan Cox 		    rv));
337dd05fa19SAlan Cox 		rv->pages->psind = 0;
338f8a47341SAlan Cox 	}
3393180f757SAlan Cox 	popmap_clear(rv->popmap, index);
340f8a47341SAlan Cox 	rv->popcnt--;
341f8a47341SAlan Cox 	if (rv->popcnt == 0) {
342f8a47341SAlan Cox 		LIST_REMOVE(rv, objq);
343f8a47341SAlan Cox 		rv->object = NULL;
344*ef435ae7SJeff Roberson 		rv->domain = -1;
345f8a47341SAlan Cox 		vm_phys_free_pages(rv->pages, VM_LEVEL_0_ORDER);
346f8a47341SAlan Cox 		vm_reserv_freed++;
347f8a47341SAlan Cox 	} else {
348f8a47341SAlan Cox 		rv->inpartpopq = TRUE;
349*ef435ae7SJeff Roberson 		TAILQ_INSERT_TAIL(&vm_rvq_partpop[rv->domain], rv, partpopq);
350f8a47341SAlan Cox 	}
351f8a47341SAlan Cox }
352f8a47341SAlan Cox 
353f8a47341SAlan Cox /*
354f8a47341SAlan Cox  * Returns the reservation to which the given page might belong.
355f8a47341SAlan Cox  */
356f8a47341SAlan Cox static __inline vm_reserv_t
357f8a47341SAlan Cox vm_reserv_from_page(vm_page_t m)
358f8a47341SAlan Cox {
359f8a47341SAlan Cox 
360f8a47341SAlan Cox 	return (&vm_reserv_array[VM_PAGE_TO_PHYS(m) >> VM_LEVEL_0_SHIFT]);
361f8a47341SAlan Cox }
362f8a47341SAlan Cox 
363f8a47341SAlan Cox /*
364f8a47341SAlan Cox  * Returns TRUE if the given reservation contains the given page index and
365f8a47341SAlan Cox  * FALSE otherwise.
366f8a47341SAlan Cox  */
367f8a47341SAlan Cox static __inline boolean_t
368f8a47341SAlan Cox vm_reserv_has_pindex(vm_reserv_t rv, vm_pindex_t pindex)
369f8a47341SAlan Cox {
370f8a47341SAlan Cox 
371f8a47341SAlan Cox 	return (((pindex - rv->pindex) & ~(VM_LEVEL_0_NPAGES - 1)) == 0);
372f8a47341SAlan Cox }
373f8a47341SAlan Cox 
374f8a47341SAlan Cox /*
375f8a47341SAlan Cox  * Increases the given reservation's population count.  Moves the reservation
3763453bca8SAlan Cox  * to the tail of the partially populated reservation queue.
377f8a47341SAlan Cox  *
378f8a47341SAlan Cox  * The free page queue must be locked.
379f8a47341SAlan Cox  */
380f8a47341SAlan Cox static void
381ec179322SAlan Cox vm_reserv_populate(vm_reserv_t rv, int index)
382f8a47341SAlan Cox {
383f8a47341SAlan Cox 
384f8a47341SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
385f8a47341SAlan Cox 	KASSERT(rv->object != NULL,
386f8a47341SAlan Cox 	    ("vm_reserv_populate: reserv %p is free", rv));
3873180f757SAlan Cox 	KASSERT(popmap_is_clear(rv->popmap, index),
388a08c1515SAlan Cox 	    ("vm_reserv_populate: reserv %p's popmap[%d] is set", rv,
389a08c1515SAlan Cox 	    index));
390f8a47341SAlan Cox 	KASSERT(rv->popcnt < VM_LEVEL_0_NPAGES,
391f8a47341SAlan Cox 	    ("vm_reserv_populate: reserv %p is already full", rv));
392dd05fa19SAlan Cox 	KASSERT(rv->pages->psind == 0,
393dd05fa19SAlan Cox 	    ("vm_reserv_populate: reserv %p is already promoted", rv));
394*ef435ae7SJeff Roberson 	KASSERT(rv->domain >= 0 && rv->domain < vm_ndomains,
395*ef435ae7SJeff Roberson 	    ("vm_reserv_populate: reserv %p's domain is corrupted %d",
396*ef435ae7SJeff Roberson 	    rv, rv->domain));
397f8a47341SAlan Cox 	if (rv->inpartpopq) {
398*ef435ae7SJeff Roberson 		TAILQ_REMOVE(&vm_rvq_partpop[rv->domain], rv, partpopq);
399f8a47341SAlan Cox 		rv->inpartpopq = FALSE;
400f8a47341SAlan Cox 	}
4013180f757SAlan Cox 	popmap_set(rv->popmap, index);
402f8a47341SAlan Cox 	rv->popcnt++;
403f8a47341SAlan Cox 	if (rv->popcnt < VM_LEVEL_0_NPAGES) {
404f8a47341SAlan Cox 		rv->inpartpopq = TRUE;
405*ef435ae7SJeff Roberson 		TAILQ_INSERT_TAIL(&vm_rvq_partpop[rv->domain], rv, partpopq);
406dd05fa19SAlan Cox 	} else
407dd05fa19SAlan Cox 		rv->pages->psind = 1;
408f8a47341SAlan Cox }
409f8a47341SAlan Cox 
410f8a47341SAlan Cox /*
411c68c3537SAlan Cox  * Allocates a contiguous set of physical pages of the given size "npages"
41264f096eeSAlan Cox  * from existing or newly created reservations.  All of the physical pages
413c68c3537SAlan Cox  * must be at or above the given physical address "low" and below the given
414c68c3537SAlan Cox  * physical address "high".  The given value "alignment" determines the
415c68c3537SAlan Cox  * alignment of the first physical page in the set.  If the given value
416c68c3537SAlan Cox  * "boundary" is non-zero, then the set of physical pages cannot cross any
417c68c3537SAlan Cox  * physical address boundary that is a multiple of that value.  Both
418c68c3537SAlan Cox  * "alignment" and "boundary" must be a power of two.
419c68c3537SAlan Cox  *
420920da7e4SAlan Cox  * The page "mpred" must immediately precede the offset "pindex" within the
421920da7e4SAlan Cox  * specified object.
422920da7e4SAlan Cox  *
423c68c3537SAlan Cox  * The object and free page queue must be locked.
424c68c3537SAlan Cox  */
425c68c3537SAlan Cox vm_page_t
426*ef435ae7SJeff Roberson vm_reserv_alloc_contig(vm_object_t object, vm_pindex_t pindex, int domain,
427*ef435ae7SJeff Roberson     u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
428*ef435ae7SJeff Roberson     vm_paddr_t boundary, vm_page_t mpred)
429c68c3537SAlan Cox {
430c68c3537SAlan Cox 	vm_paddr_t pa, size;
431920da7e4SAlan Cox 	vm_page_t m, m_ret, msucc;
432c68c3537SAlan Cox 	vm_pindex_t first, leftcap, rightcap;
433c68c3537SAlan Cox 	vm_reserv_t rv;
434c68c3537SAlan Cox 	u_long allocpages, maxpages, minpages;
435c68c3537SAlan Cox 	int i, index, n;
436c68c3537SAlan Cox 
437c68c3537SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
43889f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
439c68c3537SAlan Cox 	KASSERT(npages != 0, ("vm_reserv_alloc_contig: npages is 0"));
440c68c3537SAlan Cox 
441c68c3537SAlan Cox 	/*
442c68c3537SAlan Cox 	 * Is a reservation fundamentally impossible?
443c68c3537SAlan Cox 	 */
444c68c3537SAlan Cox 	if (pindex < VM_RESERV_INDEX(object, pindex) ||
445c68c3537SAlan Cox 	    pindex + npages > object->size)
446c68c3537SAlan Cox 		return (NULL);
447c68c3537SAlan Cox 
448c68c3537SAlan Cox 	/*
449c68c3537SAlan Cox 	 * All reservations of a particular size have the same alignment.
450c68c3537SAlan Cox 	 * Assuming that the first page is allocated from a reservation, the
451c68c3537SAlan Cox 	 * least significant bits of its physical address can be determined
452c68c3537SAlan Cox 	 * from its offset from the beginning of the reservation and the size
453c68c3537SAlan Cox 	 * of the reservation.
454c68c3537SAlan Cox 	 *
455c68c3537SAlan Cox 	 * Could the specified index within a reservation of the smallest
456c68c3537SAlan Cox 	 * possible size satisfy the alignment and boundary requirements?
457c68c3537SAlan Cox 	 */
458c68c3537SAlan Cox 	pa = VM_RESERV_INDEX(object, pindex) << PAGE_SHIFT;
459c68c3537SAlan Cox 	if ((pa & (alignment - 1)) != 0)
460c68c3537SAlan Cox 		return (NULL);
461c68c3537SAlan Cox 	size = npages << PAGE_SHIFT;
462c68c3537SAlan Cox 	if (((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0)
463c68c3537SAlan Cox 		return (NULL);
464c68c3537SAlan Cox 
465c68c3537SAlan Cox 	/*
466c68c3537SAlan Cox 	 * Look for an existing reservation.
467c68c3537SAlan Cox 	 */
468774d251dSAttilio Rao 	if (mpred != NULL) {
469920da7e4SAlan Cox 		KASSERT(mpred->object == object,
470920da7e4SAlan Cox 		    ("vm_reserv_alloc_contig: object doesn't contain mpred"));
471774d251dSAttilio Rao 		KASSERT(mpred->pindex < pindex,
472920da7e4SAlan Cox 		    ("vm_reserv_alloc_contig: mpred doesn't precede pindex"));
473c68c3537SAlan Cox 		rv = vm_reserv_from_page(mpred);
474c68c3537SAlan Cox 		if (rv->object == object && vm_reserv_has_pindex(rv, pindex))
475c68c3537SAlan Cox 			goto found;
476774d251dSAttilio Rao 		msucc = TAILQ_NEXT(mpred, listq);
477774d251dSAttilio Rao 	} else
478774d251dSAttilio Rao 		msucc = TAILQ_FIRST(&object->memq);
479774d251dSAttilio Rao 	if (msucc != NULL) {
480774d251dSAttilio Rao 		KASSERT(msucc->pindex > pindex,
481920da7e4SAlan Cox 		    ("vm_reserv_alloc_contig: msucc doesn't succeed pindex"));
482c68c3537SAlan Cox 		rv = vm_reserv_from_page(msucc);
483774d251dSAttilio Rao 		if (rv->object == object && vm_reserv_has_pindex(rv, pindex))
484c68c3537SAlan Cox 			goto found;
485c68c3537SAlan Cox 	}
486c68c3537SAlan Cox 
487c68c3537SAlan Cox 	/*
488c68c3537SAlan Cox 	 * Could at least one reservation fit between the first index to the
48964f096eeSAlan Cox 	 * left that can be used ("leftcap") and the first index to the right
49064f096eeSAlan Cox 	 * that cannot be used ("rightcap")?
491c68c3537SAlan Cox 	 */
492c68c3537SAlan Cox 	first = pindex - VM_RESERV_INDEX(object, pindex);
493c68c3537SAlan Cox 	if (mpred != NULL) {
494c68c3537SAlan Cox 		if ((rv = vm_reserv_from_page(mpred))->object != object)
495c68c3537SAlan Cox 			leftcap = mpred->pindex + 1;
496c68c3537SAlan Cox 		else
497c68c3537SAlan Cox 			leftcap = rv->pindex + VM_LEVEL_0_NPAGES;
498c68c3537SAlan Cox 		if (leftcap > first)
499c68c3537SAlan Cox 			return (NULL);
500c68c3537SAlan Cox 	}
501c68c3537SAlan Cox 	minpages = VM_RESERV_INDEX(object, pindex) + npages;
502c68c3537SAlan Cox 	maxpages = roundup2(minpages, VM_LEVEL_0_NPAGES);
503c68c3537SAlan Cox 	allocpages = maxpages;
504c68c3537SAlan Cox 	if (msucc != NULL) {
505c68c3537SAlan Cox 		if ((rv = vm_reserv_from_page(msucc))->object != object)
506c68c3537SAlan Cox 			rightcap = msucc->pindex;
507c68c3537SAlan Cox 		else
508c68c3537SAlan Cox 			rightcap = rv->pindex;
509c68c3537SAlan Cox 		if (first + maxpages > rightcap) {
510c68c3537SAlan Cox 			if (maxpages == VM_LEVEL_0_NPAGES)
511c68c3537SAlan Cox 				return (NULL);
51264f096eeSAlan Cox 
51364f096eeSAlan Cox 			/*
51464f096eeSAlan Cox 			 * At least one reservation will fit between "leftcap"
51564f096eeSAlan Cox 			 * and "rightcap".  However, a reservation for the
51664f096eeSAlan Cox 			 * last of the requested pages will not fit.  Reduce
51764f096eeSAlan Cox 			 * the size of the upcoming allocation accordingly.
51864f096eeSAlan Cox 			 */
519c68c3537SAlan Cox 			allocpages = minpages;
520c68c3537SAlan Cox 		}
521c68c3537SAlan Cox 	}
522c68c3537SAlan Cox 
523c68c3537SAlan Cox 	/*
524c68c3537SAlan Cox 	 * Would the last new reservation extend past the end of the object?
525c68c3537SAlan Cox 	 */
526c68c3537SAlan Cox 	if (first + maxpages > object->size) {
527c68c3537SAlan Cox 		/*
528c68c3537SAlan Cox 		 * Don't allocate the last new reservation if the object is a
529c68c3537SAlan Cox 		 * vnode or backed by another object that is a vnode.
530c68c3537SAlan Cox 		 */
531c68c3537SAlan Cox 		if (object->type == OBJT_VNODE ||
532c68c3537SAlan Cox 		    (object->backing_object != NULL &&
533c68c3537SAlan Cox 		    object->backing_object->type == OBJT_VNODE)) {
534c68c3537SAlan Cox 			if (maxpages == VM_LEVEL_0_NPAGES)
535c68c3537SAlan Cox 				return (NULL);
536c68c3537SAlan Cox 			allocpages = minpages;
537c68c3537SAlan Cox 		}
538c68c3537SAlan Cox 		/* Speculate that the object may grow. */
539c68c3537SAlan Cox 	}
540c68c3537SAlan Cox 
541c68c3537SAlan Cox 	/*
54264f096eeSAlan Cox 	 * Allocate the physical pages.  The alignment and boundary specified
54364f096eeSAlan Cox 	 * for this allocation may be different from the alignment and
54464f096eeSAlan Cox 	 * boundary specified for the requested pages.  For instance, the
54564f096eeSAlan Cox 	 * specified index may not be the first page within the first new
54664f096eeSAlan Cox 	 * reservation.
547c68c3537SAlan Cox 	 */
548*ef435ae7SJeff Roberson 	m = vm_phys_alloc_contig(domain, allocpages, low, high, ulmax(alignment,
549c68c3537SAlan Cox 	    VM_LEVEL_0_SIZE), boundary > VM_LEVEL_0_SIZE ? boundary : 0);
550c68c3537SAlan Cox 	if (m == NULL)
551c68c3537SAlan Cox 		return (NULL);
55264f096eeSAlan Cox 
55364f096eeSAlan Cox 	/*
55464f096eeSAlan Cox 	 * The allocated physical pages always begin at a reservation
55564f096eeSAlan Cox 	 * boundary, but they do not always end at a reservation boundary.
55664f096eeSAlan Cox 	 * Initialize every reservation that is completely covered by the
55764f096eeSAlan Cox 	 * allocated physical pages.
55864f096eeSAlan Cox 	 */
559c68c3537SAlan Cox 	m_ret = NULL;
560c68c3537SAlan Cox 	index = VM_RESERV_INDEX(object, pindex);
561c68c3537SAlan Cox 	do {
562c68c3537SAlan Cox 		rv = vm_reserv_from_page(m);
563c68c3537SAlan Cox 		KASSERT(rv->pages == m,
564c68c3537SAlan Cox 		    ("vm_reserv_alloc_contig: reserv %p's pages is corrupted",
565c68c3537SAlan Cox 		    rv));
566c68c3537SAlan Cox 		KASSERT(rv->object == NULL,
567c68c3537SAlan Cox 		    ("vm_reserv_alloc_contig: reserv %p isn't free", rv));
568c68c3537SAlan Cox 		LIST_INSERT_HEAD(&object->rvq, rv, objq);
569c68c3537SAlan Cox 		rv->object = object;
570c68c3537SAlan Cox 		rv->pindex = first;
571*ef435ae7SJeff Roberson 		rv->domain = vm_phys_domidx(m);
572c68c3537SAlan Cox 		KASSERT(rv->popcnt == 0,
573c68c3537SAlan Cox 		    ("vm_reserv_alloc_contig: reserv %p's popcnt is corrupted",
574c68c3537SAlan Cox 		    rv));
575c68c3537SAlan Cox 		KASSERT(!rv->inpartpopq,
576c68c3537SAlan Cox 		    ("vm_reserv_alloc_contig: reserv %p's inpartpopq is TRUE",
577c68c3537SAlan Cox 		    rv));
578ec179322SAlan Cox 		for (i = 0; i < NPOPMAP; i++)
579ec179322SAlan Cox 			KASSERT(rv->popmap[i] == 0,
580ec179322SAlan Cox 		    ("vm_reserv_alloc_contig: reserv %p's popmap is corrupted",
581ec179322SAlan Cox 			    rv));
582c68c3537SAlan Cox 		n = ulmin(VM_LEVEL_0_NPAGES - index, npages);
583c68c3537SAlan Cox 		for (i = 0; i < n; i++)
584ec179322SAlan Cox 			vm_reserv_populate(rv, index + i);
585c68c3537SAlan Cox 		npages -= n;
586c68c3537SAlan Cox 		if (m_ret == NULL) {
587c68c3537SAlan Cox 			m_ret = &rv->pages[index];
588c68c3537SAlan Cox 			index = 0;
589c68c3537SAlan Cox 		}
590c68c3537SAlan Cox 		m += VM_LEVEL_0_NPAGES;
591c68c3537SAlan Cox 		first += VM_LEVEL_0_NPAGES;
592c68c3537SAlan Cox 		allocpages -= VM_LEVEL_0_NPAGES;
59364f096eeSAlan Cox 	} while (allocpages >= VM_LEVEL_0_NPAGES);
594c68c3537SAlan Cox 	return (m_ret);
595c68c3537SAlan Cox 
596c68c3537SAlan Cox 	/*
597c68c3537SAlan Cox 	 * Found a matching reservation.
598c68c3537SAlan Cox 	 */
599c68c3537SAlan Cox found:
600c68c3537SAlan Cox 	index = VM_RESERV_INDEX(object, pindex);
601c68c3537SAlan Cox 	/* Does the allocation fit within the reservation? */
602c68c3537SAlan Cox 	if (index + npages > VM_LEVEL_0_NPAGES)
603c68c3537SAlan Cox 		return (NULL);
604c68c3537SAlan Cox 	m = &rv->pages[index];
605c68c3537SAlan Cox 	pa = VM_PAGE_TO_PHYS(m);
606c68c3537SAlan Cox 	if (pa < low || pa + size > high || (pa & (alignment - 1)) != 0 ||
607c68c3537SAlan Cox 	    ((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0)
608c68c3537SAlan Cox 		return (NULL);
609c68c3537SAlan Cox 	/* Handle vm_page_rename(m, new_object, ...). */
610c68c3537SAlan Cox 	for (i = 0; i < npages; i++)
6113180f757SAlan Cox 		if (popmap_is_set(rv->popmap, index + i))
612c68c3537SAlan Cox 			return (NULL);
613c68c3537SAlan Cox 	for (i = 0; i < npages; i++)
614ec179322SAlan Cox 		vm_reserv_populate(rv, index + i);
615c68c3537SAlan Cox 	return (m);
616c68c3537SAlan Cox }
617c68c3537SAlan Cox 
618c68c3537SAlan Cox /*
6193453bca8SAlan Cox  * Allocates a page from an existing or newly created reservation.
620f8a47341SAlan Cox  *
621404eb1b3SAlan Cox  * The page "mpred" must immediately precede the offset "pindex" within the
622404eb1b3SAlan Cox  * specified object.
623404eb1b3SAlan Cox  *
624f8a47341SAlan Cox  * The object and free page queue must be locked.
625f8a47341SAlan Cox  */
626f8a47341SAlan Cox vm_page_t
627*ef435ae7SJeff Roberson vm_reserv_alloc_page(vm_object_t object, vm_pindex_t pindex, int domain,
628*ef435ae7SJeff Roberson     vm_page_t mpred)
629f8a47341SAlan Cox {
630404eb1b3SAlan Cox 	vm_page_t m, msucc;
631f8a47341SAlan Cox 	vm_pindex_t first, leftcap, rightcap;
632f8a47341SAlan Cox 	vm_reserv_t rv;
633ec179322SAlan Cox 	int i, index;
634f8a47341SAlan Cox 
635f8a47341SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
63689f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
637f8a47341SAlan Cox 
638f8a47341SAlan Cox 	/*
639c68c3537SAlan Cox 	 * Is a reservation fundamentally impossible?
640f8a47341SAlan Cox 	 */
641f8a47341SAlan Cox 	if (pindex < VM_RESERV_INDEX(object, pindex) ||
642f8a47341SAlan Cox 	    pindex >= object->size)
643f8a47341SAlan Cox 		return (NULL);
644f8a47341SAlan Cox 
645f8a47341SAlan Cox 	/*
646f8a47341SAlan Cox 	 * Look for an existing reservation.
647f8a47341SAlan Cox 	 */
648774d251dSAttilio Rao 	if (mpred != NULL) {
6499eab5484SKonstantin Belousov 		KASSERT(mpred->object == object,
650404eb1b3SAlan Cox 		    ("vm_reserv_alloc_page: object doesn't contain mpred"));
651774d251dSAttilio Rao 		KASSERT(mpred->pindex < pindex,
652404eb1b3SAlan Cox 		    ("vm_reserv_alloc_page: mpred doesn't precede pindex"));
653f8a47341SAlan Cox 		rv = vm_reserv_from_page(mpred);
654c68c3537SAlan Cox 		if (rv->object == object && vm_reserv_has_pindex(rv, pindex))
655c68c3537SAlan Cox 			goto found;
656774d251dSAttilio Rao 		msucc = TAILQ_NEXT(mpred, listq);
657774d251dSAttilio Rao 	} else
658774d251dSAttilio Rao 		msucc = TAILQ_FIRST(&object->memq);
659774d251dSAttilio Rao 	if (msucc != NULL) {
660774d251dSAttilio Rao 		KASSERT(msucc->pindex > pindex,
661404eb1b3SAlan Cox 		    ("vm_reserv_alloc_page: msucc doesn't succeed pindex"));
662f8a47341SAlan Cox 		rv = vm_reserv_from_page(msucc);
663774d251dSAttilio Rao 		if (rv->object == object && vm_reserv_has_pindex(rv, pindex))
664c68c3537SAlan Cox 			goto found;
665f8a47341SAlan Cox 	}
666f8a47341SAlan Cox 
667f8a47341SAlan Cox 	/*
668c68c3537SAlan Cox 	 * Could a reservation fit between the first index to the left that
669c68c3537SAlan Cox 	 * can be used and the first index to the right that cannot be used?
670f8a47341SAlan Cox 	 */
671c68c3537SAlan Cox 	first = pindex - VM_RESERV_INDEX(object, pindex);
672c68c3537SAlan Cox 	if (mpred != NULL) {
673c68c3537SAlan Cox 		if ((rv = vm_reserv_from_page(mpred))->object != object)
674f8a47341SAlan Cox 			leftcap = mpred->pindex + 1;
675f8a47341SAlan Cox 		else
676f8a47341SAlan Cox 			leftcap = rv->pindex + VM_LEVEL_0_NPAGES;
677c68c3537SAlan Cox 		if (leftcap > first)
678c68c3537SAlan Cox 			return (NULL);
679c68c3537SAlan Cox 	}
680c68c3537SAlan Cox 	if (msucc != NULL) {
681c68c3537SAlan Cox 		if ((rv = vm_reserv_from_page(msucc))->object != object)
682f8a47341SAlan Cox 			rightcap = msucc->pindex;
683f8a47341SAlan Cox 		else
684f8a47341SAlan Cox 			rightcap = rv->pindex;
685c68c3537SAlan Cox 		if (first + VM_LEVEL_0_NPAGES > rightcap)
686f8a47341SAlan Cox 			return (NULL);
687c68c3537SAlan Cox 	}
688f8a47341SAlan Cox 
689f8a47341SAlan Cox 	/*
690c68c3537SAlan Cox 	 * Would a new reservation extend past the end of the object?
691f8a47341SAlan Cox 	 */
692c68c3537SAlan Cox 	if (first + VM_LEVEL_0_NPAGES > object->size) {
693f8a47341SAlan Cox 		/*
694f8a47341SAlan Cox 		 * Don't allocate a new reservation if the object is a vnode or
695f8a47341SAlan Cox 		 * backed by another object that is a vnode.
696f8a47341SAlan Cox 		 */
697f8a47341SAlan Cox 		if (object->type == OBJT_VNODE ||
698f8a47341SAlan Cox 		    (object->backing_object != NULL &&
699f8a47341SAlan Cox 		    object->backing_object->type == OBJT_VNODE))
700f8a47341SAlan Cox 			return (NULL);
701f8a47341SAlan Cox 		/* Speculate that the object may grow. */
702f8a47341SAlan Cox 	}
703f8a47341SAlan Cox 
704f8a47341SAlan Cox 	/*
705c68c3537SAlan Cox 	 * Allocate and populate the new reservation.
706f8a47341SAlan Cox 	 */
707*ef435ae7SJeff Roberson 	m = vm_phys_alloc_pages(domain, VM_FREEPOOL_DEFAULT, VM_LEVEL_0_ORDER);
708c68c3537SAlan Cox 	if (m == NULL)
709c68c3537SAlan Cox 		return (NULL);
710f8a47341SAlan Cox 	rv = vm_reserv_from_page(m);
711f8a47341SAlan Cox 	KASSERT(rv->pages == m,
712c68c3537SAlan Cox 	    ("vm_reserv_alloc_page: reserv %p's pages is corrupted", rv));
713f8a47341SAlan Cox 	KASSERT(rv->object == NULL,
714f8a47341SAlan Cox 	    ("vm_reserv_alloc_page: reserv %p isn't free", rv));
715f8a47341SAlan Cox 	LIST_INSERT_HEAD(&object->rvq, rv, objq);
716f8a47341SAlan Cox 	rv->object = object;
717f8a47341SAlan Cox 	rv->pindex = first;
718*ef435ae7SJeff Roberson 	rv->domain = vm_phys_domidx(m);
719f8a47341SAlan Cox 	KASSERT(rv->popcnt == 0,
720c68c3537SAlan Cox 	    ("vm_reserv_alloc_page: reserv %p's popcnt is corrupted", rv));
721f8a47341SAlan Cox 	KASSERT(!rv->inpartpopq,
722c68c3537SAlan Cox 	    ("vm_reserv_alloc_page: reserv %p's inpartpopq is TRUE", rv));
723ec179322SAlan Cox 	for (i = 0; i < NPOPMAP; i++)
724ec179322SAlan Cox 		KASSERT(rv->popmap[i] == 0,
725ec179322SAlan Cox 		    ("vm_reserv_alloc_page: reserv %p's popmap is corrupted",
726ec179322SAlan Cox 		    rv));
727ec179322SAlan Cox 	index = VM_RESERV_INDEX(object, pindex);
728ec179322SAlan Cox 	vm_reserv_populate(rv, index);
729ec179322SAlan Cox 	return (&rv->pages[index]);
730c68c3537SAlan Cox 
731c68c3537SAlan Cox 	/*
732c68c3537SAlan Cox 	 * Found a matching reservation.
733c68c3537SAlan Cox 	 */
734c68c3537SAlan Cox found:
735ec179322SAlan Cox 	index = VM_RESERV_INDEX(object, pindex);
736ec179322SAlan Cox 	m = &rv->pages[index];
737c68c3537SAlan Cox 	/* Handle vm_page_rename(m, new_object, ...). */
7383180f757SAlan Cox 	if (popmap_is_set(rv->popmap, index))
739c68c3537SAlan Cox 		return (NULL);
740ec179322SAlan Cox 	vm_reserv_populate(rv, index);
741f8a47341SAlan Cox 	return (m);
742f8a47341SAlan Cox }
743f8a47341SAlan Cox 
744f8a47341SAlan Cox /*
7453453bca8SAlan Cox  * Breaks the given reservation.  Except for the specified free page, all free
7463453bca8SAlan Cox  * pages in the reservation are returned to the physical memory allocator.
7473453bca8SAlan Cox  * The reservation's population count and map are reset to their initial
7483453bca8SAlan Cox  * state.
749ec179322SAlan Cox  *
7503453bca8SAlan Cox  * The given reservation must not be in the partially populated reservation
751ec179322SAlan Cox  * queue.  The free page queue lock must be held.
752ec179322SAlan Cox  */
753ec179322SAlan Cox static void
754ec179322SAlan Cox vm_reserv_break(vm_reserv_t rv, vm_page_t m)
755ec179322SAlan Cox {
756ec179322SAlan Cox 	int begin_zeroes, hi, i, lo;
757ec179322SAlan Cox 
758ec179322SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
759ec179322SAlan Cox 	KASSERT(rv->object != NULL,
760ec179322SAlan Cox 	    ("vm_reserv_break: reserv %p is free", rv));
761ec179322SAlan Cox 	KASSERT(!rv->inpartpopq,
762ec179322SAlan Cox 	    ("vm_reserv_break: reserv %p's inpartpopq is TRUE", rv));
763ec179322SAlan Cox 	LIST_REMOVE(rv, objq);
764ec179322SAlan Cox 	rv->object = NULL;
765*ef435ae7SJeff Roberson 	rv->domain = -1;
766ec179322SAlan Cox 	if (m != NULL) {
767ec179322SAlan Cox 		/*
768ec179322SAlan Cox 		 * Since the reservation is being broken, there is no harm in
769ec179322SAlan Cox 		 * abusing the population map to stop "m" from being returned
770ec179322SAlan Cox 		 * to the physical memory allocator.
771ec179322SAlan Cox 		 */
772ec179322SAlan Cox 		i = m - rv->pages;
7733180f757SAlan Cox 		KASSERT(popmap_is_clear(rv->popmap, i),
774ec179322SAlan Cox 		    ("vm_reserv_break: reserv %p's popmap is corrupted", rv));
7753180f757SAlan Cox 		popmap_set(rv->popmap, i);
776ec179322SAlan Cox 		rv->popcnt++;
777ec179322SAlan Cox 	}
778ec179322SAlan Cox 	i = hi = 0;
779ec179322SAlan Cox 	do {
780ec179322SAlan Cox 		/* Find the next 0 bit.  Any previous 0 bits are < "hi". */
781ec179322SAlan Cox 		lo = ffsl(~(((1UL << hi) - 1) | rv->popmap[i]));
782ec179322SAlan Cox 		if (lo == 0) {
783ec179322SAlan Cox 			/* Redundantly clears bits < "hi". */
784ec179322SAlan Cox 			rv->popmap[i] = 0;
785ec179322SAlan Cox 			rv->popcnt -= NBPOPMAP - hi;
786ec179322SAlan Cox 			while (++i < NPOPMAP) {
787ec179322SAlan Cox 				lo = ffsl(~rv->popmap[i]);
788ec179322SAlan Cox 				if (lo == 0) {
789ec179322SAlan Cox 					rv->popmap[i] = 0;
790ec179322SAlan Cox 					rv->popcnt -= NBPOPMAP;
791ec179322SAlan Cox 				} else
792ec179322SAlan Cox 					break;
793ec179322SAlan Cox 			}
794ec179322SAlan Cox 			if (i == NPOPMAP)
795ec179322SAlan Cox 				break;
796ec179322SAlan Cox 			hi = 0;
797ec179322SAlan Cox 		}
798ec179322SAlan Cox 		KASSERT(lo > 0, ("vm_reserv_break: lo is %d", lo));
799ec179322SAlan Cox 		/* Convert from ffsl() to ordinary bit numbering. */
800ec179322SAlan Cox 		lo--;
801ec179322SAlan Cox 		if (lo > 0) {
802ec179322SAlan Cox 			/* Redundantly clears bits < "hi". */
803ec179322SAlan Cox 			rv->popmap[i] &= ~((1UL << lo) - 1);
804ec179322SAlan Cox 			rv->popcnt -= lo - hi;
805ec179322SAlan Cox 		}
806ec179322SAlan Cox 		begin_zeroes = NBPOPMAP * i + lo;
807ec179322SAlan Cox 		/* Find the next 1 bit. */
808ec179322SAlan Cox 		do
809ec179322SAlan Cox 			hi = ffsl(rv->popmap[i]);
810ec179322SAlan Cox 		while (hi == 0 && ++i < NPOPMAP);
811ec179322SAlan Cox 		if (i != NPOPMAP)
812ec179322SAlan Cox 			/* Convert from ffsl() to ordinary bit numbering. */
813ec179322SAlan Cox 			hi--;
814ec179322SAlan Cox 		vm_phys_free_contig(&rv->pages[begin_zeroes], NBPOPMAP * i +
815ec179322SAlan Cox 		    hi - begin_zeroes);
816ec179322SAlan Cox 	} while (i < NPOPMAP);
817ec179322SAlan Cox 	KASSERT(rv->popcnt == 0,
818ec179322SAlan Cox 	    ("vm_reserv_break: reserv %p's popcnt is corrupted", rv));
819ec179322SAlan Cox 	vm_reserv_broken++;
820ec179322SAlan Cox }
821ec179322SAlan Cox 
822ec179322SAlan Cox /*
823f8a47341SAlan Cox  * Breaks all reservations belonging to the given object.
824f8a47341SAlan Cox  */
825f8a47341SAlan Cox void
826f8a47341SAlan Cox vm_reserv_break_all(vm_object_t object)
827f8a47341SAlan Cox {
828f8a47341SAlan Cox 	vm_reserv_t rv;
829f8a47341SAlan Cox 
830f8a47341SAlan Cox 	mtx_lock(&vm_page_queue_free_mtx);
831f8a47341SAlan Cox 	while ((rv = LIST_FIRST(&object->rvq)) != NULL) {
832f8a47341SAlan Cox 		KASSERT(rv->object == object,
833f8a47341SAlan Cox 		    ("vm_reserv_break_all: reserv %p is corrupted", rv));
834f8a47341SAlan Cox 		if (rv->inpartpopq) {
835*ef435ae7SJeff Roberson 			TAILQ_REMOVE(&vm_rvq_partpop[rv->domain], rv, partpopq);
836f8a47341SAlan Cox 			rv->inpartpopq = FALSE;
837f8a47341SAlan Cox 		}
838ec179322SAlan Cox 		vm_reserv_break(rv, NULL);
839f8a47341SAlan Cox 	}
840f8a47341SAlan Cox 	mtx_unlock(&vm_page_queue_free_mtx);
841f8a47341SAlan Cox }
842f8a47341SAlan Cox 
843f8a47341SAlan Cox /*
844f8a47341SAlan Cox  * Frees the given page if it belongs to a reservation.  Returns TRUE if the
845f8a47341SAlan Cox  * page is freed and FALSE otherwise.
846f8a47341SAlan Cox  *
847f8a47341SAlan Cox  * The free page queue lock must be held.
848f8a47341SAlan Cox  */
849f8a47341SAlan Cox boolean_t
850f8a47341SAlan Cox vm_reserv_free_page(vm_page_t m)
851f8a47341SAlan Cox {
852f8a47341SAlan Cox 	vm_reserv_t rv;
853f8a47341SAlan Cox 
854f8a47341SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
855f8a47341SAlan Cox 	rv = vm_reserv_from_page(m);
856908e3da1SAlan Cox 	if (rv->object == NULL)
857908e3da1SAlan Cox 		return (FALSE);
858ec179322SAlan Cox 	vm_reserv_depopulate(rv, m - rv->pages);
859f8a47341SAlan Cox 	return (TRUE);
860f8a47341SAlan Cox }
861f8a47341SAlan Cox 
862f8a47341SAlan Cox /*
863f8a47341SAlan Cox  * Initializes the reservation management system.  Specifically, initializes
864f8a47341SAlan Cox  * the reservation array.
865f8a47341SAlan Cox  *
866f8a47341SAlan Cox  * Requires that vm_page_array and first_page are initialized!
867f8a47341SAlan Cox  */
868f8a47341SAlan Cox void
869f8a47341SAlan Cox vm_reserv_init(void)
870f8a47341SAlan Cox {
871f8a47341SAlan Cox 	vm_paddr_t paddr;
87209e5f3c4SAlan Cox 	struct vm_phys_seg *seg;
873*ef435ae7SJeff Roberson 	int i, segind;
874f8a47341SAlan Cox 
875f8a47341SAlan Cox 	/*
876f8a47341SAlan Cox 	 * Initialize the reservation array.  Specifically, initialize the
877f8a47341SAlan Cox 	 * "pages" field for every element that has an underlying superpage.
878f8a47341SAlan Cox 	 */
87909e5f3c4SAlan Cox 	for (segind = 0; segind < vm_phys_nsegs; segind++) {
88009e5f3c4SAlan Cox 		seg = &vm_phys_segs[segind];
88109e5f3c4SAlan Cox 		paddr = roundup2(seg->start, VM_LEVEL_0_SIZE);
88209e5f3c4SAlan Cox 		while (paddr + VM_LEVEL_0_SIZE <= seg->end) {
883f8a47341SAlan Cox 			vm_reserv_array[paddr >> VM_LEVEL_0_SHIFT].pages =
884f8a47341SAlan Cox 			    PHYS_TO_VM_PAGE(paddr);
885f8a47341SAlan Cox 			paddr += VM_LEVEL_0_SIZE;
886f8a47341SAlan Cox 		}
887f8a47341SAlan Cox 	}
888*ef435ae7SJeff Roberson 	for (i = 0; i < MAXMEMDOM; i++)
889*ef435ae7SJeff Roberson 		TAILQ_INIT(&vm_rvq_partpop[i]);
890f8a47341SAlan Cox }
891f8a47341SAlan Cox 
892f8a47341SAlan Cox /*
893c869e672SAlan Cox  * Returns true if the given page belongs to a reservation and that page is
894c869e672SAlan Cox  * free.  Otherwise, returns false.
895c869e672SAlan Cox  */
896c869e672SAlan Cox bool
897c869e672SAlan Cox vm_reserv_is_page_free(vm_page_t m)
898c869e672SAlan Cox {
899c869e672SAlan Cox 	vm_reserv_t rv;
900c869e672SAlan Cox 
901c869e672SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
902c869e672SAlan Cox 	rv = vm_reserv_from_page(m);
903c869e672SAlan Cox 	if (rv->object == NULL)
904c869e672SAlan Cox 		return (false);
905c869e672SAlan Cox 	return (popmap_is_clear(rv->popmap, m - rv->pages));
906c869e672SAlan Cox }
907c869e672SAlan Cox 
908c869e672SAlan Cox /*
909c869e672SAlan Cox  * If the given page belongs to a reservation, returns the level of that
910c869e672SAlan Cox  * reservation.  Otherwise, returns -1.
911c869e672SAlan Cox  */
912c869e672SAlan Cox int
913c869e672SAlan Cox vm_reserv_level(vm_page_t m)
914c869e672SAlan Cox {
915c869e672SAlan Cox 	vm_reserv_t rv;
916c869e672SAlan Cox 
917c869e672SAlan Cox 	rv = vm_reserv_from_page(m);
918c869e672SAlan Cox 	return (rv->object != NULL ? 0 : -1);
919c869e672SAlan Cox }
920c869e672SAlan Cox 
921c869e672SAlan Cox /*
9223453bca8SAlan Cox  * Returns a reservation level if the given page belongs to a fully populated
923f8a47341SAlan Cox  * reservation and -1 otherwise.
924f8a47341SAlan Cox  */
925f8a47341SAlan Cox int
926f8a47341SAlan Cox vm_reserv_level_iffullpop(vm_page_t m)
927f8a47341SAlan Cox {
928f8a47341SAlan Cox 	vm_reserv_t rv;
929f8a47341SAlan Cox 
930f8a47341SAlan Cox 	rv = vm_reserv_from_page(m);
931f8a47341SAlan Cox 	return (rv->popcnt == VM_LEVEL_0_NPAGES ? 0 : -1);
932f8a47341SAlan Cox }
933f8a47341SAlan Cox 
934f8a47341SAlan Cox /*
9353453bca8SAlan Cox  * Breaks the given partially populated reservation, releasing its free pages
9363453bca8SAlan Cox  * to the physical memory allocator.
937f8a47341SAlan Cox  *
938f8a47341SAlan Cox  * The free page queue lock must be held.
939f8a47341SAlan Cox  */
94044aab2c3SAlan Cox static void
94144aab2c3SAlan Cox vm_reserv_reclaim(vm_reserv_t rv)
942f8a47341SAlan Cox {
943f8a47341SAlan Cox 
944f8a47341SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
945f8a47341SAlan Cox 	KASSERT(rv->inpartpopq,
946ec179322SAlan Cox 	    ("vm_reserv_reclaim: reserv %p's inpartpopq is FALSE", rv));
947*ef435ae7SJeff Roberson 	KASSERT(rv->domain >= 0 && rv->domain < vm_ndomains,
948*ef435ae7SJeff Roberson 	    ("vm_reserv_reclaim: reserv %p's domain is corrupted %d",
949*ef435ae7SJeff Roberson 	    rv, rv->domain));
950*ef435ae7SJeff Roberson 	TAILQ_REMOVE(&vm_rvq_partpop[rv->domain], rv, partpopq);
951f8a47341SAlan Cox 	rv->inpartpopq = FALSE;
952ec179322SAlan Cox 	vm_reserv_break(rv, NULL);
953f8a47341SAlan Cox 	vm_reserv_reclaimed++;
95444aab2c3SAlan Cox }
95544aab2c3SAlan Cox 
95644aab2c3SAlan Cox /*
9573453bca8SAlan Cox  * Breaks the reservation at the head of the partially populated reservation
9583453bca8SAlan Cox  * queue, releasing its free pages to the physical memory allocator.  Returns
9593453bca8SAlan Cox  * TRUE if a reservation is broken and FALSE otherwise.
96044aab2c3SAlan Cox  *
96144aab2c3SAlan Cox  * The free page queue lock must be held.
96244aab2c3SAlan Cox  */
96344aab2c3SAlan Cox boolean_t
964*ef435ae7SJeff Roberson vm_reserv_reclaim_inactive(int domain)
96544aab2c3SAlan Cox {
96644aab2c3SAlan Cox 	vm_reserv_t rv;
96744aab2c3SAlan Cox 
96844aab2c3SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
969*ef435ae7SJeff Roberson 	if ((rv = TAILQ_FIRST(&vm_rvq_partpop[domain])) != NULL) {
97044aab2c3SAlan Cox 		vm_reserv_reclaim(rv);
971f8a47341SAlan Cox 		return (TRUE);
972f8a47341SAlan Cox 	}
973f8a47341SAlan Cox 	return (FALSE);
974f8a47341SAlan Cox }
975f8a47341SAlan Cox 
976f8a47341SAlan Cox /*
9773453bca8SAlan Cox  * Searches the partially populated reservation queue for the least recently
9783453bca8SAlan Cox  * changed reservation with free pages that satisfy the given request for
9793453bca8SAlan Cox  * contiguous physical memory.  If a satisfactory reservation is found, it is
9803453bca8SAlan Cox  * broken.  Returns TRUE if a reservation is broken and FALSE otherwise.
98144aab2c3SAlan Cox  *
98244aab2c3SAlan Cox  * The free page queue lock must be held.
98344aab2c3SAlan Cox  */
98444aab2c3SAlan Cox boolean_t
985*ef435ae7SJeff Roberson vm_reserv_reclaim_contig(int domain, u_long npages, vm_paddr_t low,
986*ef435ae7SJeff Roberson     vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
98744aab2c3SAlan Cox {
988ec179322SAlan Cox 	vm_paddr_t pa, size;
98944aab2c3SAlan Cox 	vm_reserv_t rv;
99067b7e434SAlan Cox 	int hi, i, lo, low_index, next_free;
99144aab2c3SAlan Cox 
99244aab2c3SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
993c68c3537SAlan Cox 	if (npages > VM_LEVEL_0_NPAGES - 1)
99444aab2c3SAlan Cox 		return (FALSE);
995c68c3537SAlan Cox 	size = npages << PAGE_SHIFT;
996*ef435ae7SJeff Roberson 	TAILQ_FOREACH(rv, &vm_rvq_partpop[domain], partpopq) {
99744aab2c3SAlan Cox 		pa = VM_PAGE_TO_PHYS(&rv->pages[VM_LEVEL_0_NPAGES - 1]);
99844aab2c3SAlan Cox 		if (pa + PAGE_SIZE - size < low) {
999ec179322SAlan Cox 			/* This entire reservation is too low; go to next. */
100044aab2c3SAlan Cox 			continue;
100144aab2c3SAlan Cox 		}
1002ec179322SAlan Cox 		pa = VM_PAGE_TO_PHYS(&rv->pages[0]);
100344aab2c3SAlan Cox 		if (pa + size > high) {
1004ec179322SAlan Cox 			/* This entire reservation is too high; go to next. */
1005ec179322SAlan Cox 			continue;
100685f2a0c9SMax Laier 		}
1007ec179322SAlan Cox 		if (pa < low) {
1008ec179322SAlan Cox 			/* Start the search for free pages at "low". */
100967b7e434SAlan Cox 			low_index = (low + PAGE_MASK - pa) >> PAGE_SHIFT;
101067b7e434SAlan Cox 			i = low_index / NBPOPMAP;
101167b7e434SAlan Cox 			hi = low_index % NBPOPMAP;
1012ec179322SAlan Cox 		} else
1013ec179322SAlan Cox 			i = hi = 0;
1014ec179322SAlan Cox 		do {
1015ec179322SAlan Cox 			/* Find the next free page. */
1016ec179322SAlan Cox 			lo = ffsl(~(((1UL << hi) - 1) | rv->popmap[i]));
1017ec179322SAlan Cox 			while (lo == 0 && ++i < NPOPMAP)
1018ec179322SAlan Cox 				lo = ffsl(~rv->popmap[i]);
1019ec179322SAlan Cox 			if (i == NPOPMAP)
1020ec179322SAlan Cox 				break;
1021ec179322SAlan Cox 			/* Convert from ffsl() to ordinary bit numbering. */
1022ec179322SAlan Cox 			lo--;
1023ec179322SAlan Cox 			next_free = NBPOPMAP * i + lo;
1024ec179322SAlan Cox 			pa = VM_PAGE_TO_PHYS(&rv->pages[next_free]);
1025ec179322SAlan Cox 			KASSERT(pa >= low,
1026ec179322SAlan Cox 			    ("vm_reserv_reclaim_contig: pa is too low"));
1027ec179322SAlan Cox 			if (pa + size > high) {
1028ec179322SAlan Cox 				/* The rest of this reservation is too high. */
1029ec179322SAlan Cox 				break;
1030ec179322SAlan Cox 			} else if ((pa & (alignment - 1)) != 0 ||
1031ec179322SAlan Cox 			    ((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0) {
10326a93e36bSAlan Cox 				/*
10336a93e36bSAlan Cox 				 * The current page doesn't meet the alignment
10346a93e36bSAlan Cox 				 * and/or boundary requirements.  Continue
10356a93e36bSAlan Cox 				 * searching this reservation until the rest
10366a93e36bSAlan Cox 				 * of its free pages are either excluded or
10376a93e36bSAlan Cox 				 * exhausted.
10386a93e36bSAlan Cox 				 */
10396a93e36bSAlan Cox 				hi = lo + 1;
10406a93e36bSAlan Cox 				if (hi >= NBPOPMAP) {
10416a93e36bSAlan Cox 					hi = 0;
10426a93e36bSAlan Cox 					i++;
10436a93e36bSAlan Cox 				}
1044ec179322SAlan Cox 				continue;
1045ec179322SAlan Cox 			}
1046ec179322SAlan Cox 			/* Find the next used page. */
1047ec179322SAlan Cox 			hi = ffsl(rv->popmap[i] & ~((1UL << lo) - 1));
1048ec179322SAlan Cox 			while (hi == 0 && ++i < NPOPMAP) {
1049ec179322SAlan Cox 				if ((NBPOPMAP * i - next_free) * PAGE_SIZE >=
1050ec179322SAlan Cox 				    size) {
105144aab2c3SAlan Cox 					vm_reserv_reclaim(rv);
105244aab2c3SAlan Cox 					return (TRUE);
105344aab2c3SAlan Cox 				}
1054ec179322SAlan Cox 				hi = ffsl(rv->popmap[i]);
1055ec179322SAlan Cox 			}
1056ec179322SAlan Cox 			/* Convert from ffsl() to ordinary bit numbering. */
1057ec179322SAlan Cox 			if (i != NPOPMAP)
1058ec179322SAlan Cox 				hi--;
1059ec179322SAlan Cox 			if ((NBPOPMAP * i + hi - next_free) * PAGE_SIZE >=
1060ec179322SAlan Cox 			    size) {
1061ec179322SAlan Cox 				vm_reserv_reclaim(rv);
1062ec179322SAlan Cox 				return (TRUE);
1063ec179322SAlan Cox 			}
1064ec179322SAlan Cox 		} while (i < NPOPMAP);
106544aab2c3SAlan Cox 	}
106644aab2c3SAlan Cox 	return (FALSE);
106744aab2c3SAlan Cox }
106844aab2c3SAlan Cox 
106944aab2c3SAlan Cox /*
1070f8a47341SAlan Cox  * Transfers the reservation underlying the given page to a new object.
1071f8a47341SAlan Cox  *
1072f8a47341SAlan Cox  * The object must be locked.
1073f8a47341SAlan Cox  */
1074f8a47341SAlan Cox void
1075f8a47341SAlan Cox vm_reserv_rename(vm_page_t m, vm_object_t new_object, vm_object_t old_object,
1076f8a47341SAlan Cox     vm_pindex_t old_object_offset)
1077f8a47341SAlan Cox {
1078f8a47341SAlan Cox 	vm_reserv_t rv;
1079f8a47341SAlan Cox 
108089f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(new_object);
1081f8a47341SAlan Cox 	rv = vm_reserv_from_page(m);
1082f8a47341SAlan Cox 	if (rv->object == old_object) {
1083f8a47341SAlan Cox 		mtx_lock(&vm_page_queue_free_mtx);
1084f8a47341SAlan Cox 		if (rv->object == old_object) {
1085f8a47341SAlan Cox 			LIST_REMOVE(rv, objq);
1086f8a47341SAlan Cox 			LIST_INSERT_HEAD(&new_object->rvq, rv, objq);
1087f8a47341SAlan Cox 			rv->object = new_object;
1088f8a47341SAlan Cox 			rv->pindex -= old_object_offset;
1089f8a47341SAlan Cox 		}
1090f8a47341SAlan Cox 		mtx_unlock(&vm_page_queue_free_mtx);
1091f8a47341SAlan Cox 	}
1092f8a47341SAlan Cox }
1093f8a47341SAlan Cox 
1094f8a47341SAlan Cox /*
1095c869e672SAlan Cox  * Returns the size (in bytes) of a reservation of the specified level.
1096c869e672SAlan Cox  */
1097c869e672SAlan Cox int
1098c869e672SAlan Cox vm_reserv_size(int level)
1099c869e672SAlan Cox {
1100c869e672SAlan Cox 
1101c869e672SAlan Cox 	switch (level) {
1102c869e672SAlan Cox 	case 0:
1103c869e672SAlan Cox 		return (VM_LEVEL_0_SIZE);
1104c869e672SAlan Cox 	case -1:
1105c869e672SAlan Cox 		return (PAGE_SIZE);
1106c869e672SAlan Cox 	default:
1107c869e672SAlan Cox 		return (0);
1108c869e672SAlan Cox 	}
1109c869e672SAlan Cox }
1110c869e672SAlan Cox 
1111c869e672SAlan Cox /*
1112f8a47341SAlan Cox  * Allocates the virtual and physical memory required by the reservation
1113f8a47341SAlan Cox  * management system's data structures, in particular, the reservation array.
1114f8a47341SAlan Cox  */
1115f8a47341SAlan Cox vm_paddr_t
1116f8a47341SAlan Cox vm_reserv_startup(vm_offset_t *vaddr, vm_paddr_t end, vm_paddr_t high_water)
1117f8a47341SAlan Cox {
1118f8a47341SAlan Cox 	vm_paddr_t new_end;
1119f8a47341SAlan Cox 	size_t size;
1120f8a47341SAlan Cox 
1121f8a47341SAlan Cox 	/*
1122f8a47341SAlan Cox 	 * Calculate the size (in bytes) of the reservation array.  Round up
1123f8a47341SAlan Cox 	 * from "high_water" because every small page is mapped to an element
1124f8a47341SAlan Cox 	 * in the reservation array based on its physical address.  Thus, the
1125f8a47341SAlan Cox 	 * number of elements in the reservation array can be greater than the
1126f8a47341SAlan Cox 	 * number of superpages.
1127f8a47341SAlan Cox 	 */
1128f8a47341SAlan Cox 	size = howmany(high_water, VM_LEVEL_0_SIZE) * sizeof(struct vm_reserv);
1129f8a47341SAlan Cox 
1130f8a47341SAlan Cox 	/*
1131f8a47341SAlan Cox 	 * Allocate and map the physical memory for the reservation array.  The
1132f8a47341SAlan Cox 	 * next available virtual address is returned by reference.
1133f8a47341SAlan Cox 	 */
1134f8a47341SAlan Cox 	new_end = end - round_page(size);
1135f8a47341SAlan Cox 	vm_reserv_array = (void *)(uintptr_t)pmap_map(vaddr, new_end, end,
1136f8a47341SAlan Cox 	    VM_PROT_READ | VM_PROT_WRITE);
1137f8a47341SAlan Cox 	bzero(vm_reserv_array, size);
1138f8a47341SAlan Cox 
1139f8a47341SAlan Cox 	/*
1140f8a47341SAlan Cox 	 * Return the next available physical address.
1141f8a47341SAlan Cox 	 */
1142f8a47341SAlan Cox 	return (new_end);
1143f8a47341SAlan Cox }
1144f8a47341SAlan Cox 
11458b5e1472SAlan Cox /*
11468b5e1472SAlan Cox  * Returns the superpage containing the given page.
11478b5e1472SAlan Cox  */
11488b5e1472SAlan Cox vm_page_t
11498b5e1472SAlan Cox vm_reserv_to_superpage(vm_page_t m)
11508b5e1472SAlan Cox {
11518b5e1472SAlan Cox 	vm_reserv_t rv;
11528b5e1472SAlan Cox 
11538b5e1472SAlan Cox 	VM_OBJECT_ASSERT_LOCKED(m->object);
11548b5e1472SAlan Cox 	rv = vm_reserv_from_page(m);
11558b5e1472SAlan Cox 	return (rv->object == m->object && rv->popcnt == VM_LEVEL_0_NPAGES ?
11568b5e1472SAlan Cox 	    rv->pages : NULL);
11578b5e1472SAlan Cox }
11588b5e1472SAlan Cox 
1159f8a47341SAlan Cox #endif	/* VM_NRESERVLEVEL > 0 */
1160