xref: /freebsd/sys/vm/vm_reserv.c (revision 8b5e1472d27c83dc1a15bb1d84481dbe753399b5)
1f8a47341SAlan Cox /*-
2f8a47341SAlan Cox  * Copyright (c) 2002-2006 Rice University
3ec179322SAlan Cox  * Copyright (c) 2007-2011 Alan L. Cox <alc@cs.rice.edu>
4f8a47341SAlan Cox  * All rights reserved.
5f8a47341SAlan Cox  *
6f8a47341SAlan Cox  * This software was developed for the FreeBSD Project by Alan L. Cox,
7f8a47341SAlan Cox  * Olivier Crameri, Peter Druschel, Sitaram Iyer, and Juan Navarro.
8f8a47341SAlan Cox  *
9f8a47341SAlan Cox  * Redistribution and use in source and binary forms, with or without
10f8a47341SAlan Cox  * modification, are permitted provided that the following conditions
11f8a47341SAlan Cox  * are met:
12f8a47341SAlan Cox  * 1. Redistributions of source code must retain the above copyright
13f8a47341SAlan Cox  *    notice, this list of conditions and the following disclaimer.
14f8a47341SAlan Cox  * 2. Redistributions in binary form must reproduce the above copyright
15f8a47341SAlan Cox  *    notice, this list of conditions and the following disclaimer in the
16f8a47341SAlan Cox  *    documentation and/or other materials provided with the distribution.
17f8a47341SAlan Cox  *
18f8a47341SAlan Cox  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19f8a47341SAlan Cox  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20f8a47341SAlan Cox  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21f8a47341SAlan Cox  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
22f8a47341SAlan Cox  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23f8a47341SAlan Cox  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24f8a47341SAlan Cox  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25f8a47341SAlan Cox  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26f8a47341SAlan Cox  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27f8a47341SAlan Cox  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
28f8a47341SAlan Cox  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29f8a47341SAlan Cox  * POSSIBILITY OF SUCH DAMAGE.
30f8a47341SAlan Cox  */
31f8a47341SAlan Cox 
32f8a47341SAlan Cox /*
33f8a47341SAlan Cox  *	Superpage reservation management module
34c68c3537SAlan Cox  *
35c68c3537SAlan Cox  * Any external functions defined by this module are only to be used by the
36c68c3537SAlan Cox  * virtual memory system.
37f8a47341SAlan Cox  */
38f8a47341SAlan Cox 
39f8a47341SAlan Cox #include <sys/cdefs.h>
40f8a47341SAlan Cox __FBSDID("$FreeBSD$");
41f8a47341SAlan Cox 
42f8a47341SAlan Cox #include "opt_vm.h"
43f8a47341SAlan Cox 
44f8a47341SAlan Cox #include <sys/param.h>
45f8a47341SAlan Cox #include <sys/kernel.h>
46f8a47341SAlan Cox #include <sys/lock.h>
47f8a47341SAlan Cox #include <sys/malloc.h>
48f8a47341SAlan Cox #include <sys/mutex.h>
49f8a47341SAlan Cox #include <sys/queue.h>
5089f6b863SAttilio Rao #include <sys/rwlock.h>
51f8a47341SAlan Cox #include <sys/sbuf.h>
52f8a47341SAlan Cox #include <sys/sysctl.h>
53f8a47341SAlan Cox #include <sys/systm.h>
549ed01c32SGleb Smirnoff #include <sys/vmmeter.h>
55f8a47341SAlan Cox 
56f8a47341SAlan Cox #include <vm/vm.h>
57f8a47341SAlan Cox #include <vm/vm_param.h>
58f8a47341SAlan Cox #include <vm/vm_object.h>
59f8a47341SAlan Cox #include <vm/vm_page.h>
60f8a47341SAlan Cox #include <vm/vm_phys.h>
61774d251dSAttilio Rao #include <vm/vm_radix.h>
62f8a47341SAlan Cox #include <vm/vm_reserv.h>
63f8a47341SAlan Cox 
64f8a47341SAlan Cox /*
65f8a47341SAlan Cox  * The reservation system supports the speculative allocation of large physical
663453bca8SAlan Cox  * pages ("superpages").  Speculative allocation enables the fully automatic
67f8a47341SAlan Cox  * utilization of superpages by the virtual memory system.  In other words, no
68f8a47341SAlan Cox  * programmatic directives are required to use superpages.
69f8a47341SAlan Cox  */
70f8a47341SAlan Cox 
71f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
72f8a47341SAlan Cox 
73f8a47341SAlan Cox /*
74f8a47341SAlan Cox  * The number of small pages that are contained in a level 0 reservation
75f8a47341SAlan Cox  */
76f8a47341SAlan Cox #define	VM_LEVEL_0_NPAGES	(1 << VM_LEVEL_0_ORDER)
77f8a47341SAlan Cox 
78f8a47341SAlan Cox /*
79f8a47341SAlan Cox  * The number of bits by which a physical address is shifted to obtain the
80f8a47341SAlan Cox  * reservation number
81f8a47341SAlan Cox  */
82f8a47341SAlan Cox #define	VM_LEVEL_0_SHIFT	(VM_LEVEL_0_ORDER + PAGE_SHIFT)
83f8a47341SAlan Cox 
84f8a47341SAlan Cox /*
85f8a47341SAlan Cox  * The size of a level 0 reservation in bytes
86f8a47341SAlan Cox  */
87f8a47341SAlan Cox #define	VM_LEVEL_0_SIZE		(1 << VM_LEVEL_0_SHIFT)
88f8a47341SAlan Cox 
89f8a47341SAlan Cox /*
90f8a47341SAlan Cox  * Computes the index of the small page underlying the given (object, pindex)
91f8a47341SAlan Cox  * within the reservation's array of small pages.
92f8a47341SAlan Cox  */
93f8a47341SAlan Cox #define	VM_RESERV_INDEX(object, pindex)	\
94f8a47341SAlan Cox     (((object)->pg_color + (pindex)) & (VM_LEVEL_0_NPAGES - 1))
95f8a47341SAlan Cox 
96f8a47341SAlan Cox /*
97ec179322SAlan Cox  * The size of a population map entry
98ec179322SAlan Cox  */
99ec179322SAlan Cox typedef	u_long		popmap_t;
100ec179322SAlan Cox 
101ec179322SAlan Cox /*
102ec179322SAlan Cox  * The number of bits in a population map entry
103ec179322SAlan Cox  */
104ec179322SAlan Cox #define	NBPOPMAP	(NBBY * sizeof(popmap_t))
105ec179322SAlan Cox 
106ec179322SAlan Cox /*
107ec179322SAlan Cox  * The number of population map entries in a reservation
108ec179322SAlan Cox  */
109ec179322SAlan Cox #define	NPOPMAP		howmany(VM_LEVEL_0_NPAGES, NBPOPMAP)
110ec179322SAlan Cox 
111ec179322SAlan Cox /*
1123180f757SAlan Cox  * Clear a bit in the population map.
1133180f757SAlan Cox  */
1143180f757SAlan Cox static __inline void
1153180f757SAlan Cox popmap_clear(popmap_t popmap[], int i)
1163180f757SAlan Cox {
1173180f757SAlan Cox 
1183180f757SAlan Cox 	popmap[i / NBPOPMAP] &= ~(1UL << (i % NBPOPMAP));
1193180f757SAlan Cox }
1203180f757SAlan Cox 
1213180f757SAlan Cox /*
1223180f757SAlan Cox  * Set a bit in the population map.
1233180f757SAlan Cox  */
1243180f757SAlan Cox static __inline void
1253180f757SAlan Cox popmap_set(popmap_t popmap[], int i)
1263180f757SAlan Cox {
1273180f757SAlan Cox 
1283180f757SAlan Cox 	popmap[i / NBPOPMAP] |= 1UL << (i % NBPOPMAP);
1293180f757SAlan Cox }
1303180f757SAlan Cox 
1313180f757SAlan Cox /*
1323180f757SAlan Cox  * Is a bit in the population map clear?
1333180f757SAlan Cox  */
1343180f757SAlan Cox static __inline boolean_t
1353180f757SAlan Cox popmap_is_clear(popmap_t popmap[], int i)
1363180f757SAlan Cox {
1373180f757SAlan Cox 
1383180f757SAlan Cox 	return ((popmap[i / NBPOPMAP] & (1UL << (i % NBPOPMAP))) == 0);
1393180f757SAlan Cox }
1403180f757SAlan Cox 
1413180f757SAlan Cox /*
1423180f757SAlan Cox  * Is a bit in the population map set?
1433180f757SAlan Cox  */
1443180f757SAlan Cox static __inline boolean_t
1453180f757SAlan Cox popmap_is_set(popmap_t popmap[], int i)
1463180f757SAlan Cox {
1473180f757SAlan Cox 
1483180f757SAlan Cox 	return ((popmap[i / NBPOPMAP] & (1UL << (i % NBPOPMAP))) != 0);
1493180f757SAlan Cox }
1503180f757SAlan Cox 
1513180f757SAlan Cox /*
152f8a47341SAlan Cox  * The reservation structure
153f8a47341SAlan Cox  *
154f8a47341SAlan Cox  * A reservation structure is constructed whenever a large physical page is
155f8a47341SAlan Cox  * speculatively allocated to an object.  The reservation provides the small
156f8a47341SAlan Cox  * physical pages for the range [pindex, pindex + VM_LEVEL_0_NPAGES) of offsets
157f8a47341SAlan Cox  * within that object.  The reservation's "popcnt" tracks the number of these
158f8a47341SAlan Cox  * small physical pages that are in use at any given time.  When and if the
1593453bca8SAlan Cox  * reservation is not fully utilized, it appears in the queue of partially
160f8a47341SAlan Cox  * populated reservations.  The reservation always appears on the containing
161f8a47341SAlan Cox  * object's list of reservations.
162f8a47341SAlan Cox  *
1633453bca8SAlan Cox  * A partially populated reservation can be broken and reclaimed at any time.
164f8a47341SAlan Cox  */
165f8a47341SAlan Cox struct vm_reserv {
166f8a47341SAlan Cox 	TAILQ_ENTRY(vm_reserv) partpopq;
167f8a47341SAlan Cox 	LIST_ENTRY(vm_reserv) objq;
168f8a47341SAlan Cox 	vm_object_t	object;			/* containing object */
169f8a47341SAlan Cox 	vm_pindex_t	pindex;			/* offset within object */
170f8a47341SAlan Cox 	vm_page_t	pages;			/* first page of a superpage */
171f8a47341SAlan Cox 	int		popcnt;			/* # of pages in use */
172f8a47341SAlan Cox 	char		inpartpopq;
173ec179322SAlan Cox 	popmap_t	popmap[NPOPMAP];	/* bit vector of used pages */
174f8a47341SAlan Cox };
175f8a47341SAlan Cox 
176f8a47341SAlan Cox /*
177f8a47341SAlan Cox  * The reservation array
178f8a47341SAlan Cox  *
179f8a47341SAlan Cox  * This array is analoguous in function to vm_page_array.  It differs in the
180f8a47341SAlan Cox  * respect that it may contain a greater number of useful reservation
181f8a47341SAlan Cox  * structures than there are (physical) superpages.  These "invalid"
182f8a47341SAlan Cox  * reservation structures exist to trade-off space for time in the
183f8a47341SAlan Cox  * implementation of vm_reserv_from_page().  Invalid reservation structures are
184f8a47341SAlan Cox  * distinguishable from "valid" reservation structures by inspecting the
185f8a47341SAlan Cox  * reservation's "pages" field.  Invalid reservation structures have a NULL
186f8a47341SAlan Cox  * "pages" field.
187f8a47341SAlan Cox  *
188f8a47341SAlan Cox  * vm_reserv_from_page() maps a small (physical) page to an element of this
189f8a47341SAlan Cox  * array by computing a physical reservation number from the page's physical
190f8a47341SAlan Cox  * address.  The physical reservation number is used as the array index.
191f8a47341SAlan Cox  *
192f8a47341SAlan Cox  * An "active" reservation is a valid reservation structure that has a non-NULL
193f8a47341SAlan Cox  * "object" field and a non-zero "popcnt" field.  In other words, every active
194f8a47341SAlan Cox  * reservation belongs to a particular object.  Moreover, every active
195f8a47341SAlan Cox  * reservation has an entry in the containing object's list of reservations.
196f8a47341SAlan Cox  */
197f8a47341SAlan Cox static vm_reserv_t vm_reserv_array;
198f8a47341SAlan Cox 
199f8a47341SAlan Cox /*
2003453bca8SAlan Cox  * The partially populated reservation queue
201f8a47341SAlan Cox  *
2023453bca8SAlan Cox  * This queue enables the fast recovery of an unused free small page from a
2033453bca8SAlan Cox  * partially populated reservation.  The reservation at the head of this queue
2043453bca8SAlan Cox  * is the least recently changed, partially populated reservation.
205f8a47341SAlan Cox  *
206f8a47341SAlan Cox  * Access to this queue is synchronized by the free page queue lock.
207f8a47341SAlan Cox  */
208f8a47341SAlan Cox static TAILQ_HEAD(, vm_reserv) vm_rvq_partpop =
209f8a47341SAlan Cox 			    TAILQ_HEAD_INITIALIZER(vm_rvq_partpop);
210f8a47341SAlan Cox 
211f8a47341SAlan Cox static SYSCTL_NODE(_vm, OID_AUTO, reserv, CTLFLAG_RD, 0, "Reservation Info");
212f8a47341SAlan Cox 
213f8a47341SAlan Cox static long vm_reserv_broken;
214f8a47341SAlan Cox SYSCTL_LONG(_vm_reserv, OID_AUTO, broken, CTLFLAG_RD,
215f8a47341SAlan Cox     &vm_reserv_broken, 0, "Cumulative number of broken reservations");
216f8a47341SAlan Cox 
217f8a47341SAlan Cox static long vm_reserv_freed;
218f8a47341SAlan Cox SYSCTL_LONG(_vm_reserv, OID_AUTO, freed, CTLFLAG_RD,
219f8a47341SAlan Cox     &vm_reserv_freed, 0, "Cumulative number of freed reservations");
220f8a47341SAlan Cox 
221e0a63baaSAlan Cox static int sysctl_vm_reserv_fullpop(SYSCTL_HANDLER_ARGS);
222e0a63baaSAlan Cox 
223e0a63baaSAlan Cox SYSCTL_PROC(_vm_reserv, OID_AUTO, fullpop, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
224e0a63baaSAlan Cox     sysctl_vm_reserv_fullpop, "I", "Current number of full reservations");
225e0a63baaSAlan Cox 
226f8a47341SAlan Cox static int sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS);
227f8a47341SAlan Cox 
228f8a47341SAlan Cox SYSCTL_OID(_vm_reserv, OID_AUTO, partpopq, CTLTYPE_STRING | CTLFLAG_RD, NULL, 0,
2293453bca8SAlan Cox     sysctl_vm_reserv_partpopq, "A", "Partially populated reservation queues");
230f8a47341SAlan Cox 
231f8a47341SAlan Cox static long vm_reserv_reclaimed;
232f8a47341SAlan Cox SYSCTL_LONG(_vm_reserv, OID_AUTO, reclaimed, CTLFLAG_RD,
233f8a47341SAlan Cox     &vm_reserv_reclaimed, 0, "Cumulative number of reclaimed reservations");
234f8a47341SAlan Cox 
235ec179322SAlan Cox static void		vm_reserv_break(vm_reserv_t rv, vm_page_t m);
236ec179322SAlan Cox static void		vm_reserv_depopulate(vm_reserv_t rv, int index);
237f8a47341SAlan Cox static vm_reserv_t	vm_reserv_from_page(vm_page_t m);
238f8a47341SAlan Cox static boolean_t	vm_reserv_has_pindex(vm_reserv_t rv,
239f8a47341SAlan Cox 			    vm_pindex_t pindex);
240ec179322SAlan Cox static void		vm_reserv_populate(vm_reserv_t rv, int index);
24144aab2c3SAlan Cox static void		vm_reserv_reclaim(vm_reserv_t rv);
242f8a47341SAlan Cox 
243f8a47341SAlan Cox /*
244e0a63baaSAlan Cox  * Returns the current number of full reservations.
245e0a63baaSAlan Cox  *
246e0a63baaSAlan Cox  * Since the number of full reservations is computed without acquiring the
247e0a63baaSAlan Cox  * free page queue lock, the returned value may be inexact.
248e0a63baaSAlan Cox  */
249e0a63baaSAlan Cox static int
250e0a63baaSAlan Cox sysctl_vm_reserv_fullpop(SYSCTL_HANDLER_ARGS)
251e0a63baaSAlan Cox {
252e0a63baaSAlan Cox 	vm_paddr_t paddr;
253e0a63baaSAlan Cox 	struct vm_phys_seg *seg;
254e0a63baaSAlan Cox 	vm_reserv_t rv;
255e0a63baaSAlan Cox 	int fullpop, segind;
256e0a63baaSAlan Cox 
257e0a63baaSAlan Cox 	fullpop = 0;
258e0a63baaSAlan Cox 	for (segind = 0; segind < vm_phys_nsegs; segind++) {
259e0a63baaSAlan Cox 		seg = &vm_phys_segs[segind];
260e0a63baaSAlan Cox 		paddr = roundup2(seg->start, VM_LEVEL_0_SIZE);
261e0a63baaSAlan Cox 		while (paddr + VM_LEVEL_0_SIZE <= seg->end) {
262e0a63baaSAlan Cox 			rv = &vm_reserv_array[paddr >> VM_LEVEL_0_SHIFT];
263e0a63baaSAlan Cox 			fullpop += rv->popcnt == VM_LEVEL_0_NPAGES;
264e0a63baaSAlan Cox 			paddr += VM_LEVEL_0_SIZE;
265e0a63baaSAlan Cox 		}
266e0a63baaSAlan Cox 	}
267e0a63baaSAlan Cox 	return (sysctl_handle_int(oidp, &fullpop, 0, req));
268e0a63baaSAlan Cox }
269e0a63baaSAlan Cox 
270e0a63baaSAlan Cox /*
2713453bca8SAlan Cox  * Describes the current state of the partially populated reservation queue.
272f8a47341SAlan Cox  */
273f8a47341SAlan Cox static int
274f8a47341SAlan Cox sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS)
275f8a47341SAlan Cox {
276f8a47341SAlan Cox 	struct sbuf sbuf;
277f8a47341SAlan Cox 	vm_reserv_t rv;
278f8a47341SAlan Cox 	int counter, error, level, unused_pages;
279f8a47341SAlan Cox 
28000f0e671SMatthew D Fleming 	error = sysctl_wire_old_buffer(req, 0);
28100f0e671SMatthew D Fleming 	if (error != 0)
28200f0e671SMatthew D Fleming 		return (error);
2834e657159SMatthew D Fleming 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
284f8a47341SAlan Cox 	sbuf_printf(&sbuf, "\nLEVEL     SIZE  NUMBER\n\n");
285f8a47341SAlan Cox 	for (level = -1; level <= VM_NRESERVLEVEL - 2; level++) {
286f8a47341SAlan Cox 		counter = 0;
287f8a47341SAlan Cox 		unused_pages = 0;
288f8a47341SAlan Cox 		mtx_lock(&vm_page_queue_free_mtx);
289f8a47341SAlan Cox 		TAILQ_FOREACH(rv, &vm_rvq_partpop/*[level]*/, partpopq) {
290f8a47341SAlan Cox 			counter++;
291f8a47341SAlan Cox 			unused_pages += VM_LEVEL_0_NPAGES - rv->popcnt;
292f8a47341SAlan Cox 		}
293f8a47341SAlan Cox 		mtx_unlock(&vm_page_queue_free_mtx);
294d689bc00SAlan Cox 		sbuf_printf(&sbuf, "%5d: %6dK, %6d\n", level,
2952cf36c8fSAlan Cox 		    unused_pages * ((int)PAGE_SIZE / 1024), counter);
296f8a47341SAlan Cox 	}
2974e657159SMatthew D Fleming 	error = sbuf_finish(&sbuf);
298f8a47341SAlan Cox 	sbuf_delete(&sbuf);
299f8a47341SAlan Cox 	return (error);
300f8a47341SAlan Cox }
301f8a47341SAlan Cox 
302f8a47341SAlan Cox /*
303f8a47341SAlan Cox  * Reduces the given reservation's population count.  If the population count
304f8a47341SAlan Cox  * becomes zero, the reservation is destroyed.  Additionally, moves the
3053453bca8SAlan Cox  * reservation to the tail of the partially populated reservation queue if the
306f8a47341SAlan Cox  * population count is non-zero.
307f8a47341SAlan Cox  *
308f8a47341SAlan Cox  * The free page queue lock must be held.
309f8a47341SAlan Cox  */
310f8a47341SAlan Cox static void
311ec179322SAlan Cox vm_reserv_depopulate(vm_reserv_t rv, int index)
312f8a47341SAlan Cox {
313f8a47341SAlan Cox 
314f8a47341SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
315f8a47341SAlan Cox 	KASSERT(rv->object != NULL,
316f8a47341SAlan Cox 	    ("vm_reserv_depopulate: reserv %p is free", rv));
3173180f757SAlan Cox 	KASSERT(popmap_is_set(rv->popmap, index),
318a08c1515SAlan Cox 	    ("vm_reserv_depopulate: reserv %p's popmap[%d] is clear", rv,
319a08c1515SAlan Cox 	    index));
320f8a47341SAlan Cox 	KASSERT(rv->popcnt > 0,
321f8a47341SAlan Cox 	    ("vm_reserv_depopulate: reserv %p's popcnt is corrupted", rv));
322f8a47341SAlan Cox 	if (rv->inpartpopq) {
323f8a47341SAlan Cox 		TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq);
324f8a47341SAlan Cox 		rv->inpartpopq = FALSE;
325dd05fa19SAlan Cox 	} else {
326dd05fa19SAlan Cox 		KASSERT(rv->pages->psind == 1,
327dd05fa19SAlan Cox 		    ("vm_reserv_depopulate: reserv %p is already demoted",
328dd05fa19SAlan Cox 		    rv));
329dd05fa19SAlan Cox 		rv->pages->psind = 0;
330f8a47341SAlan Cox 	}
3313180f757SAlan Cox 	popmap_clear(rv->popmap, index);
332f8a47341SAlan Cox 	rv->popcnt--;
333f8a47341SAlan Cox 	if (rv->popcnt == 0) {
334f8a47341SAlan Cox 		LIST_REMOVE(rv, objq);
335f8a47341SAlan Cox 		rv->object = NULL;
336f8a47341SAlan Cox 		vm_phys_free_pages(rv->pages, VM_LEVEL_0_ORDER);
337f8a47341SAlan Cox 		vm_reserv_freed++;
338f8a47341SAlan Cox 	} else {
339f8a47341SAlan Cox 		rv->inpartpopq = TRUE;
340ab5378cfSAlan Cox 		TAILQ_INSERT_TAIL(&vm_rvq_partpop, rv, partpopq);
341f8a47341SAlan Cox 	}
342f8a47341SAlan Cox }
343f8a47341SAlan Cox 
344f8a47341SAlan Cox /*
345f8a47341SAlan Cox  * Returns the reservation to which the given page might belong.
346f8a47341SAlan Cox  */
347f8a47341SAlan Cox static __inline vm_reserv_t
348f8a47341SAlan Cox vm_reserv_from_page(vm_page_t m)
349f8a47341SAlan Cox {
350f8a47341SAlan Cox 
351f8a47341SAlan Cox 	return (&vm_reserv_array[VM_PAGE_TO_PHYS(m) >> VM_LEVEL_0_SHIFT]);
352f8a47341SAlan Cox }
353f8a47341SAlan Cox 
354f8a47341SAlan Cox /*
355f8a47341SAlan Cox  * Returns TRUE if the given reservation contains the given page index and
356f8a47341SAlan Cox  * FALSE otherwise.
357f8a47341SAlan Cox  */
358f8a47341SAlan Cox static __inline boolean_t
359f8a47341SAlan Cox vm_reserv_has_pindex(vm_reserv_t rv, vm_pindex_t pindex)
360f8a47341SAlan Cox {
361f8a47341SAlan Cox 
362f8a47341SAlan Cox 	return (((pindex - rv->pindex) & ~(VM_LEVEL_0_NPAGES - 1)) == 0);
363f8a47341SAlan Cox }
364f8a47341SAlan Cox 
365f8a47341SAlan Cox /*
366f8a47341SAlan Cox  * Increases the given reservation's population count.  Moves the reservation
3673453bca8SAlan Cox  * to the tail of the partially populated reservation queue.
368f8a47341SAlan Cox  *
369f8a47341SAlan Cox  * The free page queue must be locked.
370f8a47341SAlan Cox  */
371f8a47341SAlan Cox static void
372ec179322SAlan Cox vm_reserv_populate(vm_reserv_t rv, int index)
373f8a47341SAlan Cox {
374f8a47341SAlan Cox 
375f8a47341SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
376f8a47341SAlan Cox 	KASSERT(rv->object != NULL,
377f8a47341SAlan Cox 	    ("vm_reserv_populate: reserv %p is free", rv));
3783180f757SAlan Cox 	KASSERT(popmap_is_clear(rv->popmap, index),
379a08c1515SAlan Cox 	    ("vm_reserv_populate: reserv %p's popmap[%d] is set", rv,
380a08c1515SAlan Cox 	    index));
381f8a47341SAlan Cox 	KASSERT(rv->popcnt < VM_LEVEL_0_NPAGES,
382f8a47341SAlan Cox 	    ("vm_reserv_populate: reserv %p is already full", rv));
383dd05fa19SAlan Cox 	KASSERT(rv->pages->psind == 0,
384dd05fa19SAlan Cox 	    ("vm_reserv_populate: reserv %p is already promoted", rv));
385f8a47341SAlan Cox 	if (rv->inpartpopq) {
386f8a47341SAlan Cox 		TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq);
387f8a47341SAlan Cox 		rv->inpartpopq = FALSE;
388f8a47341SAlan Cox 	}
3893180f757SAlan Cox 	popmap_set(rv->popmap, index);
390f8a47341SAlan Cox 	rv->popcnt++;
391f8a47341SAlan Cox 	if (rv->popcnt < VM_LEVEL_0_NPAGES) {
392f8a47341SAlan Cox 		rv->inpartpopq = TRUE;
393f8a47341SAlan Cox 		TAILQ_INSERT_TAIL(&vm_rvq_partpop, rv, partpopq);
394dd05fa19SAlan Cox 	} else
395dd05fa19SAlan Cox 		rv->pages->psind = 1;
396f8a47341SAlan Cox }
397f8a47341SAlan Cox 
398f8a47341SAlan Cox /*
399c68c3537SAlan Cox  * Allocates a contiguous set of physical pages of the given size "npages"
40064f096eeSAlan Cox  * from existing or newly created reservations.  All of the physical pages
401c68c3537SAlan Cox  * must be at or above the given physical address "low" and below the given
402c68c3537SAlan Cox  * physical address "high".  The given value "alignment" determines the
403c68c3537SAlan Cox  * alignment of the first physical page in the set.  If the given value
404c68c3537SAlan Cox  * "boundary" is non-zero, then the set of physical pages cannot cross any
405c68c3537SAlan Cox  * physical address boundary that is a multiple of that value.  Both
406c68c3537SAlan Cox  * "alignment" and "boundary" must be a power of two.
407c68c3537SAlan Cox  *
408920da7e4SAlan Cox  * The page "mpred" must immediately precede the offset "pindex" within the
409920da7e4SAlan Cox  * specified object.
410920da7e4SAlan Cox  *
411c68c3537SAlan Cox  * The object and free page queue must be locked.
412c68c3537SAlan Cox  */
413c68c3537SAlan Cox vm_page_t
414c68c3537SAlan Cox vm_reserv_alloc_contig(vm_object_t object, vm_pindex_t pindex, u_long npages,
415920da7e4SAlan Cox     vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary,
416920da7e4SAlan Cox     vm_page_t mpred)
417c68c3537SAlan Cox {
418c68c3537SAlan Cox 	vm_paddr_t pa, size;
419920da7e4SAlan Cox 	vm_page_t m, m_ret, msucc;
420c68c3537SAlan Cox 	vm_pindex_t first, leftcap, rightcap;
421c68c3537SAlan Cox 	vm_reserv_t rv;
422c68c3537SAlan Cox 	u_long allocpages, maxpages, minpages;
423c68c3537SAlan Cox 	int i, index, n;
424c68c3537SAlan Cox 
425c68c3537SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
42689f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
427c68c3537SAlan Cox 	KASSERT(npages != 0, ("vm_reserv_alloc_contig: npages is 0"));
428c68c3537SAlan Cox 
429c68c3537SAlan Cox 	/*
430c68c3537SAlan Cox 	 * Is a reservation fundamentally impossible?
431c68c3537SAlan Cox 	 */
432c68c3537SAlan Cox 	if (pindex < VM_RESERV_INDEX(object, pindex) ||
433c68c3537SAlan Cox 	    pindex + npages > object->size)
434c68c3537SAlan Cox 		return (NULL);
435c68c3537SAlan Cox 
436c68c3537SAlan Cox 	/*
437c68c3537SAlan Cox 	 * All reservations of a particular size have the same alignment.
438c68c3537SAlan Cox 	 * Assuming that the first page is allocated from a reservation, the
439c68c3537SAlan Cox 	 * least significant bits of its physical address can be determined
440c68c3537SAlan Cox 	 * from its offset from the beginning of the reservation and the size
441c68c3537SAlan Cox 	 * of the reservation.
442c68c3537SAlan Cox 	 *
443c68c3537SAlan Cox 	 * Could the specified index within a reservation of the smallest
444c68c3537SAlan Cox 	 * possible size satisfy the alignment and boundary requirements?
445c68c3537SAlan Cox 	 */
446c68c3537SAlan Cox 	pa = VM_RESERV_INDEX(object, pindex) << PAGE_SHIFT;
447c68c3537SAlan Cox 	if ((pa & (alignment - 1)) != 0)
448c68c3537SAlan Cox 		return (NULL);
449c68c3537SAlan Cox 	size = npages << PAGE_SHIFT;
450c68c3537SAlan Cox 	if (((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0)
451c68c3537SAlan Cox 		return (NULL);
452c68c3537SAlan Cox 
453c68c3537SAlan Cox 	/*
454c68c3537SAlan Cox 	 * Look for an existing reservation.
455c68c3537SAlan Cox 	 */
456774d251dSAttilio Rao 	if (mpred != NULL) {
457920da7e4SAlan Cox 		KASSERT(mpred->object == object,
458920da7e4SAlan Cox 		    ("vm_reserv_alloc_contig: object doesn't contain mpred"));
459774d251dSAttilio Rao 		KASSERT(mpred->pindex < pindex,
460920da7e4SAlan Cox 		    ("vm_reserv_alloc_contig: mpred doesn't precede pindex"));
461c68c3537SAlan Cox 		rv = vm_reserv_from_page(mpred);
462c68c3537SAlan Cox 		if (rv->object == object && vm_reserv_has_pindex(rv, pindex))
463c68c3537SAlan Cox 			goto found;
464774d251dSAttilio Rao 		msucc = TAILQ_NEXT(mpred, listq);
465774d251dSAttilio Rao 	} else
466774d251dSAttilio Rao 		msucc = TAILQ_FIRST(&object->memq);
467774d251dSAttilio Rao 	if (msucc != NULL) {
468774d251dSAttilio Rao 		KASSERT(msucc->pindex > pindex,
469920da7e4SAlan Cox 		    ("vm_reserv_alloc_contig: msucc doesn't succeed pindex"));
470c68c3537SAlan Cox 		rv = vm_reserv_from_page(msucc);
471774d251dSAttilio Rao 		if (rv->object == object && vm_reserv_has_pindex(rv, pindex))
472c68c3537SAlan Cox 			goto found;
473c68c3537SAlan Cox 	}
474c68c3537SAlan Cox 
475c68c3537SAlan Cox 	/*
476c68c3537SAlan Cox 	 * Could at least one reservation fit between the first index to the
47764f096eeSAlan Cox 	 * left that can be used ("leftcap") and the first index to the right
47864f096eeSAlan Cox 	 * that cannot be used ("rightcap")?
479c68c3537SAlan Cox 	 */
480c68c3537SAlan Cox 	first = pindex - VM_RESERV_INDEX(object, pindex);
481c68c3537SAlan Cox 	if (mpred != NULL) {
482c68c3537SAlan Cox 		if ((rv = vm_reserv_from_page(mpred))->object != object)
483c68c3537SAlan Cox 			leftcap = mpred->pindex + 1;
484c68c3537SAlan Cox 		else
485c68c3537SAlan Cox 			leftcap = rv->pindex + VM_LEVEL_0_NPAGES;
486c68c3537SAlan Cox 		if (leftcap > first)
487c68c3537SAlan Cox 			return (NULL);
488c68c3537SAlan Cox 	}
489c68c3537SAlan Cox 	minpages = VM_RESERV_INDEX(object, pindex) + npages;
490c68c3537SAlan Cox 	maxpages = roundup2(minpages, VM_LEVEL_0_NPAGES);
491c68c3537SAlan Cox 	allocpages = maxpages;
492c68c3537SAlan Cox 	if (msucc != NULL) {
493c68c3537SAlan Cox 		if ((rv = vm_reserv_from_page(msucc))->object != object)
494c68c3537SAlan Cox 			rightcap = msucc->pindex;
495c68c3537SAlan Cox 		else
496c68c3537SAlan Cox 			rightcap = rv->pindex;
497c68c3537SAlan Cox 		if (first + maxpages > rightcap) {
498c68c3537SAlan Cox 			if (maxpages == VM_LEVEL_0_NPAGES)
499c68c3537SAlan Cox 				return (NULL);
50064f096eeSAlan Cox 
50164f096eeSAlan Cox 			/*
50264f096eeSAlan Cox 			 * At least one reservation will fit between "leftcap"
50364f096eeSAlan Cox 			 * and "rightcap".  However, a reservation for the
50464f096eeSAlan Cox 			 * last of the requested pages will not fit.  Reduce
50564f096eeSAlan Cox 			 * the size of the upcoming allocation accordingly.
50664f096eeSAlan Cox 			 */
507c68c3537SAlan Cox 			allocpages = minpages;
508c68c3537SAlan Cox 		}
509c68c3537SAlan Cox 	}
510c68c3537SAlan Cox 
511c68c3537SAlan Cox 	/*
512c68c3537SAlan Cox 	 * Would the last new reservation extend past the end of the object?
513c68c3537SAlan Cox 	 */
514c68c3537SAlan Cox 	if (first + maxpages > object->size) {
515c68c3537SAlan Cox 		/*
516c68c3537SAlan Cox 		 * Don't allocate the last new reservation if the object is a
517c68c3537SAlan Cox 		 * vnode or backed by another object that is a vnode.
518c68c3537SAlan Cox 		 */
519c68c3537SAlan Cox 		if (object->type == OBJT_VNODE ||
520c68c3537SAlan Cox 		    (object->backing_object != NULL &&
521c68c3537SAlan Cox 		    object->backing_object->type == OBJT_VNODE)) {
522c68c3537SAlan Cox 			if (maxpages == VM_LEVEL_0_NPAGES)
523c68c3537SAlan Cox 				return (NULL);
524c68c3537SAlan Cox 			allocpages = minpages;
525c68c3537SAlan Cox 		}
526c68c3537SAlan Cox 		/* Speculate that the object may grow. */
527c68c3537SAlan Cox 	}
528c68c3537SAlan Cox 
529c68c3537SAlan Cox 	/*
53064f096eeSAlan Cox 	 * Allocate the physical pages.  The alignment and boundary specified
53164f096eeSAlan Cox 	 * for this allocation may be different from the alignment and
53264f096eeSAlan Cox 	 * boundary specified for the requested pages.  For instance, the
53364f096eeSAlan Cox 	 * specified index may not be the first page within the first new
53464f096eeSAlan Cox 	 * reservation.
535c68c3537SAlan Cox 	 */
536c68c3537SAlan Cox 	m = vm_phys_alloc_contig(allocpages, low, high, ulmax(alignment,
537c68c3537SAlan Cox 	    VM_LEVEL_0_SIZE), boundary > VM_LEVEL_0_SIZE ? boundary : 0);
538c68c3537SAlan Cox 	if (m == NULL)
539c68c3537SAlan Cox 		return (NULL);
54064f096eeSAlan Cox 
54164f096eeSAlan Cox 	/*
54264f096eeSAlan Cox 	 * The allocated physical pages always begin at a reservation
54364f096eeSAlan Cox 	 * boundary, but they do not always end at a reservation boundary.
54464f096eeSAlan Cox 	 * Initialize every reservation that is completely covered by the
54564f096eeSAlan Cox 	 * allocated physical pages.
54664f096eeSAlan Cox 	 */
547c68c3537SAlan Cox 	m_ret = NULL;
548c68c3537SAlan Cox 	index = VM_RESERV_INDEX(object, pindex);
549c68c3537SAlan Cox 	do {
550c68c3537SAlan Cox 		rv = vm_reserv_from_page(m);
551c68c3537SAlan Cox 		KASSERT(rv->pages == m,
552c68c3537SAlan Cox 		    ("vm_reserv_alloc_contig: reserv %p's pages is corrupted",
553c68c3537SAlan Cox 		    rv));
554c68c3537SAlan Cox 		KASSERT(rv->object == NULL,
555c68c3537SAlan Cox 		    ("vm_reserv_alloc_contig: reserv %p isn't free", rv));
556c68c3537SAlan Cox 		LIST_INSERT_HEAD(&object->rvq, rv, objq);
557c68c3537SAlan Cox 		rv->object = object;
558c68c3537SAlan Cox 		rv->pindex = first;
559c68c3537SAlan Cox 		KASSERT(rv->popcnt == 0,
560c68c3537SAlan Cox 		    ("vm_reserv_alloc_contig: reserv %p's popcnt is corrupted",
561c68c3537SAlan Cox 		    rv));
562c68c3537SAlan Cox 		KASSERT(!rv->inpartpopq,
563c68c3537SAlan Cox 		    ("vm_reserv_alloc_contig: reserv %p's inpartpopq is TRUE",
564c68c3537SAlan Cox 		    rv));
565ec179322SAlan Cox 		for (i = 0; i < NPOPMAP; i++)
566ec179322SAlan Cox 			KASSERT(rv->popmap[i] == 0,
567ec179322SAlan Cox 		    ("vm_reserv_alloc_contig: reserv %p's popmap is corrupted",
568ec179322SAlan Cox 			    rv));
569c68c3537SAlan Cox 		n = ulmin(VM_LEVEL_0_NPAGES - index, npages);
570c68c3537SAlan Cox 		for (i = 0; i < n; i++)
571ec179322SAlan Cox 			vm_reserv_populate(rv, index + i);
572c68c3537SAlan Cox 		npages -= n;
573c68c3537SAlan Cox 		if (m_ret == NULL) {
574c68c3537SAlan Cox 			m_ret = &rv->pages[index];
575c68c3537SAlan Cox 			index = 0;
576c68c3537SAlan Cox 		}
577c68c3537SAlan Cox 		m += VM_LEVEL_0_NPAGES;
578c68c3537SAlan Cox 		first += VM_LEVEL_0_NPAGES;
579c68c3537SAlan Cox 		allocpages -= VM_LEVEL_0_NPAGES;
58064f096eeSAlan Cox 	} while (allocpages >= VM_LEVEL_0_NPAGES);
581c68c3537SAlan Cox 	return (m_ret);
582c68c3537SAlan Cox 
583c68c3537SAlan Cox 	/*
584c68c3537SAlan Cox 	 * Found a matching reservation.
585c68c3537SAlan Cox 	 */
586c68c3537SAlan Cox found:
587c68c3537SAlan Cox 	index = VM_RESERV_INDEX(object, pindex);
588c68c3537SAlan Cox 	/* Does the allocation fit within the reservation? */
589c68c3537SAlan Cox 	if (index + npages > VM_LEVEL_0_NPAGES)
590c68c3537SAlan Cox 		return (NULL);
591c68c3537SAlan Cox 	m = &rv->pages[index];
592c68c3537SAlan Cox 	pa = VM_PAGE_TO_PHYS(m);
593c68c3537SAlan Cox 	if (pa < low || pa + size > high || (pa & (alignment - 1)) != 0 ||
594c68c3537SAlan Cox 	    ((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0)
595c68c3537SAlan Cox 		return (NULL);
596c68c3537SAlan Cox 	/* Handle vm_page_rename(m, new_object, ...). */
597c68c3537SAlan Cox 	for (i = 0; i < npages; i++)
5983180f757SAlan Cox 		if (popmap_is_set(rv->popmap, index + i))
599c68c3537SAlan Cox 			return (NULL);
600c68c3537SAlan Cox 	for (i = 0; i < npages; i++)
601ec179322SAlan Cox 		vm_reserv_populate(rv, index + i);
602c68c3537SAlan Cox 	return (m);
603c68c3537SAlan Cox }
604c68c3537SAlan Cox 
605c68c3537SAlan Cox /*
6063453bca8SAlan Cox  * Allocates a page from an existing or newly created reservation.
607f8a47341SAlan Cox  *
608404eb1b3SAlan Cox  * The page "mpred" must immediately precede the offset "pindex" within the
609404eb1b3SAlan Cox  * specified object.
610404eb1b3SAlan Cox  *
611f8a47341SAlan Cox  * The object and free page queue must be locked.
612f8a47341SAlan Cox  */
613f8a47341SAlan Cox vm_page_t
614404eb1b3SAlan Cox vm_reserv_alloc_page(vm_object_t object, vm_pindex_t pindex, vm_page_t mpred)
615f8a47341SAlan Cox {
616404eb1b3SAlan Cox 	vm_page_t m, msucc;
617f8a47341SAlan Cox 	vm_pindex_t first, leftcap, rightcap;
618f8a47341SAlan Cox 	vm_reserv_t rv;
619ec179322SAlan Cox 	int i, index;
620f8a47341SAlan Cox 
621f8a47341SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
62289f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
623f8a47341SAlan Cox 
624f8a47341SAlan Cox 	/*
625c68c3537SAlan Cox 	 * Is a reservation fundamentally impossible?
626f8a47341SAlan Cox 	 */
627f8a47341SAlan Cox 	if (pindex < VM_RESERV_INDEX(object, pindex) ||
628f8a47341SAlan Cox 	    pindex >= object->size)
629f8a47341SAlan Cox 		return (NULL);
630f8a47341SAlan Cox 
631f8a47341SAlan Cox 	/*
632f8a47341SAlan Cox 	 * Look for an existing reservation.
633f8a47341SAlan Cox 	 */
634774d251dSAttilio Rao 	if (mpred != NULL) {
6359eab5484SKonstantin Belousov 		KASSERT(mpred->object == object,
636404eb1b3SAlan Cox 		    ("vm_reserv_alloc_page: object doesn't contain mpred"));
637774d251dSAttilio Rao 		KASSERT(mpred->pindex < pindex,
638404eb1b3SAlan Cox 		    ("vm_reserv_alloc_page: mpred doesn't precede pindex"));
639f8a47341SAlan Cox 		rv = vm_reserv_from_page(mpred);
640c68c3537SAlan Cox 		if (rv->object == object && vm_reserv_has_pindex(rv, pindex))
641c68c3537SAlan Cox 			goto found;
642774d251dSAttilio Rao 		msucc = TAILQ_NEXT(mpred, listq);
643774d251dSAttilio Rao 	} else
644774d251dSAttilio Rao 		msucc = TAILQ_FIRST(&object->memq);
645774d251dSAttilio Rao 	if (msucc != NULL) {
646774d251dSAttilio Rao 		KASSERT(msucc->pindex > pindex,
647404eb1b3SAlan Cox 		    ("vm_reserv_alloc_page: msucc doesn't succeed pindex"));
648f8a47341SAlan Cox 		rv = vm_reserv_from_page(msucc);
649774d251dSAttilio Rao 		if (rv->object == object && vm_reserv_has_pindex(rv, pindex))
650c68c3537SAlan Cox 			goto found;
651f8a47341SAlan Cox 	}
652f8a47341SAlan Cox 
653f8a47341SAlan Cox 	/*
654c68c3537SAlan Cox 	 * Could a reservation fit between the first index to the left that
655c68c3537SAlan Cox 	 * can be used and the first index to the right that cannot be used?
656f8a47341SAlan Cox 	 */
657c68c3537SAlan Cox 	first = pindex - VM_RESERV_INDEX(object, pindex);
658c68c3537SAlan Cox 	if (mpred != NULL) {
659c68c3537SAlan Cox 		if ((rv = vm_reserv_from_page(mpred))->object != object)
660f8a47341SAlan Cox 			leftcap = mpred->pindex + 1;
661f8a47341SAlan Cox 		else
662f8a47341SAlan Cox 			leftcap = rv->pindex + VM_LEVEL_0_NPAGES;
663c68c3537SAlan Cox 		if (leftcap > first)
664c68c3537SAlan Cox 			return (NULL);
665c68c3537SAlan Cox 	}
666c68c3537SAlan Cox 	if (msucc != NULL) {
667c68c3537SAlan Cox 		if ((rv = vm_reserv_from_page(msucc))->object != object)
668f8a47341SAlan Cox 			rightcap = msucc->pindex;
669f8a47341SAlan Cox 		else
670f8a47341SAlan Cox 			rightcap = rv->pindex;
671c68c3537SAlan Cox 		if (first + VM_LEVEL_0_NPAGES > rightcap)
672f8a47341SAlan Cox 			return (NULL);
673c68c3537SAlan Cox 	}
674f8a47341SAlan Cox 
675f8a47341SAlan Cox 	/*
676c68c3537SAlan Cox 	 * Would a new reservation extend past the end of the object?
677f8a47341SAlan Cox 	 */
678c68c3537SAlan Cox 	if (first + VM_LEVEL_0_NPAGES > object->size) {
679f8a47341SAlan Cox 		/*
680f8a47341SAlan Cox 		 * Don't allocate a new reservation if the object is a vnode or
681f8a47341SAlan Cox 		 * backed by another object that is a vnode.
682f8a47341SAlan Cox 		 */
683f8a47341SAlan Cox 		if (object->type == OBJT_VNODE ||
684f8a47341SAlan Cox 		    (object->backing_object != NULL &&
685f8a47341SAlan Cox 		    object->backing_object->type == OBJT_VNODE))
686f8a47341SAlan Cox 			return (NULL);
687f8a47341SAlan Cox 		/* Speculate that the object may grow. */
688f8a47341SAlan Cox 	}
689f8a47341SAlan Cox 
690f8a47341SAlan Cox 	/*
691c68c3537SAlan Cox 	 * Allocate and populate the new reservation.
692f8a47341SAlan Cox 	 */
693f8a47341SAlan Cox 	m = vm_phys_alloc_pages(VM_FREEPOOL_DEFAULT, VM_LEVEL_0_ORDER);
694c68c3537SAlan Cox 	if (m == NULL)
695c68c3537SAlan Cox 		return (NULL);
696f8a47341SAlan Cox 	rv = vm_reserv_from_page(m);
697f8a47341SAlan Cox 	KASSERT(rv->pages == m,
698c68c3537SAlan Cox 	    ("vm_reserv_alloc_page: reserv %p's pages is corrupted", rv));
699f8a47341SAlan Cox 	KASSERT(rv->object == NULL,
700f8a47341SAlan Cox 	    ("vm_reserv_alloc_page: reserv %p isn't free", rv));
701f8a47341SAlan Cox 	LIST_INSERT_HEAD(&object->rvq, rv, objq);
702f8a47341SAlan Cox 	rv->object = object;
703f8a47341SAlan Cox 	rv->pindex = first;
704f8a47341SAlan Cox 	KASSERT(rv->popcnt == 0,
705c68c3537SAlan Cox 	    ("vm_reserv_alloc_page: reserv %p's popcnt is corrupted", rv));
706f8a47341SAlan Cox 	KASSERT(!rv->inpartpopq,
707c68c3537SAlan Cox 	    ("vm_reserv_alloc_page: reserv %p's inpartpopq is TRUE", rv));
708ec179322SAlan Cox 	for (i = 0; i < NPOPMAP; i++)
709ec179322SAlan Cox 		KASSERT(rv->popmap[i] == 0,
710ec179322SAlan Cox 		    ("vm_reserv_alloc_page: reserv %p's popmap is corrupted",
711ec179322SAlan Cox 		    rv));
712ec179322SAlan Cox 	index = VM_RESERV_INDEX(object, pindex);
713ec179322SAlan Cox 	vm_reserv_populate(rv, index);
714ec179322SAlan Cox 	return (&rv->pages[index]);
715c68c3537SAlan Cox 
716c68c3537SAlan Cox 	/*
717c68c3537SAlan Cox 	 * Found a matching reservation.
718c68c3537SAlan Cox 	 */
719c68c3537SAlan Cox found:
720ec179322SAlan Cox 	index = VM_RESERV_INDEX(object, pindex);
721ec179322SAlan Cox 	m = &rv->pages[index];
722c68c3537SAlan Cox 	/* Handle vm_page_rename(m, new_object, ...). */
7233180f757SAlan Cox 	if (popmap_is_set(rv->popmap, index))
724c68c3537SAlan Cox 		return (NULL);
725ec179322SAlan Cox 	vm_reserv_populate(rv, index);
726f8a47341SAlan Cox 	return (m);
727f8a47341SAlan Cox }
728f8a47341SAlan Cox 
729f8a47341SAlan Cox /*
7303453bca8SAlan Cox  * Breaks the given reservation.  Except for the specified free page, all free
7313453bca8SAlan Cox  * pages in the reservation are returned to the physical memory allocator.
7323453bca8SAlan Cox  * The reservation's population count and map are reset to their initial
7333453bca8SAlan Cox  * state.
734ec179322SAlan Cox  *
7353453bca8SAlan Cox  * The given reservation must not be in the partially populated reservation
736ec179322SAlan Cox  * queue.  The free page queue lock must be held.
737ec179322SAlan Cox  */
738ec179322SAlan Cox static void
739ec179322SAlan Cox vm_reserv_break(vm_reserv_t rv, vm_page_t m)
740ec179322SAlan Cox {
741ec179322SAlan Cox 	int begin_zeroes, hi, i, lo;
742ec179322SAlan Cox 
743ec179322SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
744ec179322SAlan Cox 	KASSERT(rv->object != NULL,
745ec179322SAlan Cox 	    ("vm_reserv_break: reserv %p is free", rv));
746ec179322SAlan Cox 	KASSERT(!rv->inpartpopq,
747ec179322SAlan Cox 	    ("vm_reserv_break: reserv %p's inpartpopq is TRUE", rv));
748ec179322SAlan Cox 	LIST_REMOVE(rv, objq);
749ec179322SAlan Cox 	rv->object = NULL;
750ec179322SAlan Cox 	if (m != NULL) {
751ec179322SAlan Cox 		/*
752ec179322SAlan Cox 		 * Since the reservation is being broken, there is no harm in
753ec179322SAlan Cox 		 * abusing the population map to stop "m" from being returned
754ec179322SAlan Cox 		 * to the physical memory allocator.
755ec179322SAlan Cox 		 */
756ec179322SAlan Cox 		i = m - rv->pages;
7573180f757SAlan Cox 		KASSERT(popmap_is_clear(rv->popmap, i),
758ec179322SAlan Cox 		    ("vm_reserv_break: reserv %p's popmap is corrupted", rv));
7593180f757SAlan Cox 		popmap_set(rv->popmap, i);
760ec179322SAlan Cox 		rv->popcnt++;
761ec179322SAlan Cox 	}
762ec179322SAlan Cox 	i = hi = 0;
763ec179322SAlan Cox 	do {
764ec179322SAlan Cox 		/* Find the next 0 bit.  Any previous 0 bits are < "hi". */
765ec179322SAlan Cox 		lo = ffsl(~(((1UL << hi) - 1) | rv->popmap[i]));
766ec179322SAlan Cox 		if (lo == 0) {
767ec179322SAlan Cox 			/* Redundantly clears bits < "hi". */
768ec179322SAlan Cox 			rv->popmap[i] = 0;
769ec179322SAlan Cox 			rv->popcnt -= NBPOPMAP - hi;
770ec179322SAlan Cox 			while (++i < NPOPMAP) {
771ec179322SAlan Cox 				lo = ffsl(~rv->popmap[i]);
772ec179322SAlan Cox 				if (lo == 0) {
773ec179322SAlan Cox 					rv->popmap[i] = 0;
774ec179322SAlan Cox 					rv->popcnt -= NBPOPMAP;
775ec179322SAlan Cox 				} else
776ec179322SAlan Cox 					break;
777ec179322SAlan Cox 			}
778ec179322SAlan Cox 			if (i == NPOPMAP)
779ec179322SAlan Cox 				break;
780ec179322SAlan Cox 			hi = 0;
781ec179322SAlan Cox 		}
782ec179322SAlan Cox 		KASSERT(lo > 0, ("vm_reserv_break: lo is %d", lo));
783ec179322SAlan Cox 		/* Convert from ffsl() to ordinary bit numbering. */
784ec179322SAlan Cox 		lo--;
785ec179322SAlan Cox 		if (lo > 0) {
786ec179322SAlan Cox 			/* Redundantly clears bits < "hi". */
787ec179322SAlan Cox 			rv->popmap[i] &= ~((1UL << lo) - 1);
788ec179322SAlan Cox 			rv->popcnt -= lo - hi;
789ec179322SAlan Cox 		}
790ec179322SAlan Cox 		begin_zeroes = NBPOPMAP * i + lo;
791ec179322SAlan Cox 		/* Find the next 1 bit. */
792ec179322SAlan Cox 		do
793ec179322SAlan Cox 			hi = ffsl(rv->popmap[i]);
794ec179322SAlan Cox 		while (hi == 0 && ++i < NPOPMAP);
795ec179322SAlan Cox 		if (i != NPOPMAP)
796ec179322SAlan Cox 			/* Convert from ffsl() to ordinary bit numbering. */
797ec179322SAlan Cox 			hi--;
798ec179322SAlan Cox 		vm_phys_free_contig(&rv->pages[begin_zeroes], NBPOPMAP * i +
799ec179322SAlan Cox 		    hi - begin_zeroes);
800ec179322SAlan Cox 	} while (i < NPOPMAP);
801ec179322SAlan Cox 	KASSERT(rv->popcnt == 0,
802ec179322SAlan Cox 	    ("vm_reserv_break: reserv %p's popcnt is corrupted", rv));
803ec179322SAlan Cox 	vm_reserv_broken++;
804ec179322SAlan Cox }
805ec179322SAlan Cox 
806ec179322SAlan Cox /*
807f8a47341SAlan Cox  * Breaks all reservations belonging to the given object.
808f8a47341SAlan Cox  */
809f8a47341SAlan Cox void
810f8a47341SAlan Cox vm_reserv_break_all(vm_object_t object)
811f8a47341SAlan Cox {
812f8a47341SAlan Cox 	vm_reserv_t rv;
813f8a47341SAlan Cox 
814f8a47341SAlan Cox 	mtx_lock(&vm_page_queue_free_mtx);
815f8a47341SAlan Cox 	while ((rv = LIST_FIRST(&object->rvq)) != NULL) {
816f8a47341SAlan Cox 		KASSERT(rv->object == object,
817f8a47341SAlan Cox 		    ("vm_reserv_break_all: reserv %p is corrupted", rv));
818f8a47341SAlan Cox 		if (rv->inpartpopq) {
819f8a47341SAlan Cox 			TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq);
820f8a47341SAlan Cox 			rv->inpartpopq = FALSE;
821f8a47341SAlan Cox 		}
822ec179322SAlan Cox 		vm_reserv_break(rv, NULL);
823f8a47341SAlan Cox 	}
824f8a47341SAlan Cox 	mtx_unlock(&vm_page_queue_free_mtx);
825f8a47341SAlan Cox }
826f8a47341SAlan Cox 
827f8a47341SAlan Cox /*
828f8a47341SAlan Cox  * Frees the given page if it belongs to a reservation.  Returns TRUE if the
829f8a47341SAlan Cox  * page is freed and FALSE otherwise.
830f8a47341SAlan Cox  *
831f8a47341SAlan Cox  * The free page queue lock must be held.
832f8a47341SAlan Cox  */
833f8a47341SAlan Cox boolean_t
834f8a47341SAlan Cox vm_reserv_free_page(vm_page_t m)
835f8a47341SAlan Cox {
836f8a47341SAlan Cox 	vm_reserv_t rv;
837f8a47341SAlan Cox 
838f8a47341SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
839f8a47341SAlan Cox 	rv = vm_reserv_from_page(m);
840908e3da1SAlan Cox 	if (rv->object == NULL)
841908e3da1SAlan Cox 		return (FALSE);
842ec179322SAlan Cox 	vm_reserv_depopulate(rv, m - rv->pages);
843f8a47341SAlan Cox 	return (TRUE);
844f8a47341SAlan Cox }
845f8a47341SAlan Cox 
846f8a47341SAlan Cox /*
847f8a47341SAlan Cox  * Initializes the reservation management system.  Specifically, initializes
848f8a47341SAlan Cox  * the reservation array.
849f8a47341SAlan Cox  *
850f8a47341SAlan Cox  * Requires that vm_page_array and first_page are initialized!
851f8a47341SAlan Cox  */
852f8a47341SAlan Cox void
853f8a47341SAlan Cox vm_reserv_init(void)
854f8a47341SAlan Cox {
855f8a47341SAlan Cox 	vm_paddr_t paddr;
85609e5f3c4SAlan Cox 	struct vm_phys_seg *seg;
85709e5f3c4SAlan Cox 	int segind;
858f8a47341SAlan Cox 
859f8a47341SAlan Cox 	/*
860f8a47341SAlan Cox 	 * Initialize the reservation array.  Specifically, initialize the
861f8a47341SAlan Cox 	 * "pages" field for every element that has an underlying superpage.
862f8a47341SAlan Cox 	 */
86309e5f3c4SAlan Cox 	for (segind = 0; segind < vm_phys_nsegs; segind++) {
86409e5f3c4SAlan Cox 		seg = &vm_phys_segs[segind];
86509e5f3c4SAlan Cox 		paddr = roundup2(seg->start, VM_LEVEL_0_SIZE);
86609e5f3c4SAlan Cox 		while (paddr + VM_LEVEL_0_SIZE <= seg->end) {
867f8a47341SAlan Cox 			vm_reserv_array[paddr >> VM_LEVEL_0_SHIFT].pages =
868f8a47341SAlan Cox 			    PHYS_TO_VM_PAGE(paddr);
869f8a47341SAlan Cox 			paddr += VM_LEVEL_0_SIZE;
870f8a47341SAlan Cox 		}
871f8a47341SAlan Cox 	}
872f8a47341SAlan Cox }
873f8a47341SAlan Cox 
874f8a47341SAlan Cox /*
875c869e672SAlan Cox  * Returns true if the given page belongs to a reservation and that page is
876c869e672SAlan Cox  * free.  Otherwise, returns false.
877c869e672SAlan Cox  */
878c869e672SAlan Cox bool
879c869e672SAlan Cox vm_reserv_is_page_free(vm_page_t m)
880c869e672SAlan Cox {
881c869e672SAlan Cox 	vm_reserv_t rv;
882c869e672SAlan Cox 
883c869e672SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
884c869e672SAlan Cox 	rv = vm_reserv_from_page(m);
885c869e672SAlan Cox 	if (rv->object == NULL)
886c869e672SAlan Cox 		return (false);
887c869e672SAlan Cox 	return (popmap_is_clear(rv->popmap, m - rv->pages));
888c869e672SAlan Cox }
889c869e672SAlan Cox 
890c869e672SAlan Cox /*
891c869e672SAlan Cox  * If the given page belongs to a reservation, returns the level of that
892c869e672SAlan Cox  * reservation.  Otherwise, returns -1.
893c869e672SAlan Cox  */
894c869e672SAlan Cox int
895c869e672SAlan Cox vm_reserv_level(vm_page_t m)
896c869e672SAlan Cox {
897c869e672SAlan Cox 	vm_reserv_t rv;
898c869e672SAlan Cox 
899c869e672SAlan Cox 	rv = vm_reserv_from_page(m);
900c869e672SAlan Cox 	return (rv->object != NULL ? 0 : -1);
901c869e672SAlan Cox }
902c869e672SAlan Cox 
903c869e672SAlan Cox /*
9043453bca8SAlan Cox  * Returns a reservation level if the given page belongs to a fully populated
905f8a47341SAlan Cox  * reservation and -1 otherwise.
906f8a47341SAlan Cox  */
907f8a47341SAlan Cox int
908f8a47341SAlan Cox vm_reserv_level_iffullpop(vm_page_t m)
909f8a47341SAlan Cox {
910f8a47341SAlan Cox 	vm_reserv_t rv;
911f8a47341SAlan Cox 
912f8a47341SAlan Cox 	rv = vm_reserv_from_page(m);
913f8a47341SAlan Cox 	return (rv->popcnt == VM_LEVEL_0_NPAGES ? 0 : -1);
914f8a47341SAlan Cox }
915f8a47341SAlan Cox 
916f8a47341SAlan Cox /*
9173453bca8SAlan Cox  * Breaks the given partially populated reservation, releasing its free pages
9183453bca8SAlan Cox  * to the physical memory allocator.
919f8a47341SAlan Cox  *
920f8a47341SAlan Cox  * The free page queue lock must be held.
921f8a47341SAlan Cox  */
92244aab2c3SAlan Cox static void
92344aab2c3SAlan Cox vm_reserv_reclaim(vm_reserv_t rv)
924f8a47341SAlan Cox {
925f8a47341SAlan Cox 
926f8a47341SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
927f8a47341SAlan Cox 	KASSERT(rv->inpartpopq,
928ec179322SAlan Cox 	    ("vm_reserv_reclaim: reserv %p's inpartpopq is FALSE", rv));
929f8a47341SAlan Cox 	TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq);
930f8a47341SAlan Cox 	rv->inpartpopq = FALSE;
931ec179322SAlan Cox 	vm_reserv_break(rv, NULL);
932f8a47341SAlan Cox 	vm_reserv_reclaimed++;
93344aab2c3SAlan Cox }
93444aab2c3SAlan Cox 
93544aab2c3SAlan Cox /*
9363453bca8SAlan Cox  * Breaks the reservation at the head of the partially populated reservation
9373453bca8SAlan Cox  * queue, releasing its free pages to the physical memory allocator.  Returns
9383453bca8SAlan Cox  * TRUE if a reservation is broken and FALSE otherwise.
93944aab2c3SAlan Cox  *
94044aab2c3SAlan Cox  * The free page queue lock must be held.
94144aab2c3SAlan Cox  */
94244aab2c3SAlan Cox boolean_t
94344aab2c3SAlan Cox vm_reserv_reclaim_inactive(void)
94444aab2c3SAlan Cox {
94544aab2c3SAlan Cox 	vm_reserv_t rv;
94644aab2c3SAlan Cox 
94744aab2c3SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
94844aab2c3SAlan Cox 	if ((rv = TAILQ_FIRST(&vm_rvq_partpop)) != NULL) {
94944aab2c3SAlan Cox 		vm_reserv_reclaim(rv);
950f8a47341SAlan Cox 		return (TRUE);
951f8a47341SAlan Cox 	}
952f8a47341SAlan Cox 	return (FALSE);
953f8a47341SAlan Cox }
954f8a47341SAlan Cox 
955f8a47341SAlan Cox /*
9563453bca8SAlan Cox  * Searches the partially populated reservation queue for the least recently
9573453bca8SAlan Cox  * changed reservation with free pages that satisfy the given request for
9583453bca8SAlan Cox  * contiguous physical memory.  If a satisfactory reservation is found, it is
9593453bca8SAlan Cox  * broken.  Returns 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
964c68c3537SAlan Cox vm_reserv_reclaim_contig(u_long npages, vm_paddr_t low, vm_paddr_t high,
9655c1f2cc4SAlan Cox     u_long alignment, vm_paddr_t boundary)
96644aab2c3SAlan Cox {
967ec179322SAlan Cox 	vm_paddr_t pa, size;
96844aab2c3SAlan Cox 	vm_reserv_t rv;
96967b7e434SAlan Cox 	int hi, i, lo, low_index, next_free;
97044aab2c3SAlan Cox 
97144aab2c3SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
972c68c3537SAlan Cox 	if (npages > VM_LEVEL_0_NPAGES - 1)
97344aab2c3SAlan Cox 		return (FALSE);
974c68c3537SAlan Cox 	size = npages << PAGE_SHIFT;
97544aab2c3SAlan Cox 	TAILQ_FOREACH(rv, &vm_rvq_partpop, partpopq) {
97644aab2c3SAlan Cox 		pa = VM_PAGE_TO_PHYS(&rv->pages[VM_LEVEL_0_NPAGES - 1]);
97744aab2c3SAlan Cox 		if (pa + PAGE_SIZE - size < low) {
978ec179322SAlan Cox 			/* This entire reservation is too low; go to next. */
97944aab2c3SAlan Cox 			continue;
98044aab2c3SAlan Cox 		}
981ec179322SAlan Cox 		pa = VM_PAGE_TO_PHYS(&rv->pages[0]);
98244aab2c3SAlan Cox 		if (pa + size > high) {
983ec179322SAlan Cox 			/* This entire reservation is too high; go to next. */
984ec179322SAlan Cox 			continue;
98585f2a0c9SMax Laier 		}
986ec179322SAlan Cox 		if (pa < low) {
987ec179322SAlan Cox 			/* Start the search for free pages at "low". */
98867b7e434SAlan Cox 			low_index = (low + PAGE_MASK - pa) >> PAGE_SHIFT;
98967b7e434SAlan Cox 			i = low_index / NBPOPMAP;
99067b7e434SAlan Cox 			hi = low_index % NBPOPMAP;
991ec179322SAlan Cox 		} else
992ec179322SAlan Cox 			i = hi = 0;
993ec179322SAlan Cox 		do {
994ec179322SAlan Cox 			/* Find the next free page. */
995ec179322SAlan Cox 			lo = ffsl(~(((1UL << hi) - 1) | rv->popmap[i]));
996ec179322SAlan Cox 			while (lo == 0 && ++i < NPOPMAP)
997ec179322SAlan Cox 				lo = ffsl(~rv->popmap[i]);
998ec179322SAlan Cox 			if (i == NPOPMAP)
999ec179322SAlan Cox 				break;
1000ec179322SAlan Cox 			/* Convert from ffsl() to ordinary bit numbering. */
1001ec179322SAlan Cox 			lo--;
1002ec179322SAlan Cox 			next_free = NBPOPMAP * i + lo;
1003ec179322SAlan Cox 			pa = VM_PAGE_TO_PHYS(&rv->pages[next_free]);
1004ec179322SAlan Cox 			KASSERT(pa >= low,
1005ec179322SAlan Cox 			    ("vm_reserv_reclaim_contig: pa is too low"));
1006ec179322SAlan Cox 			if (pa + size > high) {
1007ec179322SAlan Cox 				/* The rest of this reservation is too high. */
1008ec179322SAlan Cox 				break;
1009ec179322SAlan Cox 			} else if ((pa & (alignment - 1)) != 0 ||
1010ec179322SAlan Cox 			    ((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0) {
10116a93e36bSAlan Cox 				/*
10126a93e36bSAlan Cox 				 * The current page doesn't meet the alignment
10136a93e36bSAlan Cox 				 * and/or boundary requirements.  Continue
10146a93e36bSAlan Cox 				 * searching this reservation until the rest
10156a93e36bSAlan Cox 				 * of its free pages are either excluded or
10166a93e36bSAlan Cox 				 * exhausted.
10176a93e36bSAlan Cox 				 */
10186a93e36bSAlan Cox 				hi = lo + 1;
10196a93e36bSAlan Cox 				if (hi >= NBPOPMAP) {
10206a93e36bSAlan Cox 					hi = 0;
10216a93e36bSAlan Cox 					i++;
10226a93e36bSAlan Cox 				}
1023ec179322SAlan Cox 				continue;
1024ec179322SAlan Cox 			}
1025ec179322SAlan Cox 			/* Find the next used page. */
1026ec179322SAlan Cox 			hi = ffsl(rv->popmap[i] & ~((1UL << lo) - 1));
1027ec179322SAlan Cox 			while (hi == 0 && ++i < NPOPMAP) {
1028ec179322SAlan Cox 				if ((NBPOPMAP * i - next_free) * PAGE_SIZE >=
1029ec179322SAlan Cox 				    size) {
103044aab2c3SAlan Cox 					vm_reserv_reclaim(rv);
103144aab2c3SAlan Cox 					return (TRUE);
103244aab2c3SAlan Cox 				}
1033ec179322SAlan Cox 				hi = ffsl(rv->popmap[i]);
1034ec179322SAlan Cox 			}
1035ec179322SAlan Cox 			/* Convert from ffsl() to ordinary bit numbering. */
1036ec179322SAlan Cox 			if (i != NPOPMAP)
1037ec179322SAlan Cox 				hi--;
1038ec179322SAlan Cox 			if ((NBPOPMAP * i + hi - next_free) * PAGE_SIZE >=
1039ec179322SAlan Cox 			    size) {
1040ec179322SAlan Cox 				vm_reserv_reclaim(rv);
1041ec179322SAlan Cox 				return (TRUE);
1042ec179322SAlan Cox 			}
1043ec179322SAlan Cox 		} while (i < NPOPMAP);
104444aab2c3SAlan Cox 	}
104544aab2c3SAlan Cox 	return (FALSE);
104644aab2c3SAlan Cox }
104744aab2c3SAlan Cox 
104844aab2c3SAlan Cox /*
1049f8a47341SAlan Cox  * Transfers the reservation underlying the given page to a new object.
1050f8a47341SAlan Cox  *
1051f8a47341SAlan Cox  * The object must be locked.
1052f8a47341SAlan Cox  */
1053f8a47341SAlan Cox void
1054f8a47341SAlan Cox vm_reserv_rename(vm_page_t m, vm_object_t new_object, vm_object_t old_object,
1055f8a47341SAlan Cox     vm_pindex_t old_object_offset)
1056f8a47341SAlan Cox {
1057f8a47341SAlan Cox 	vm_reserv_t rv;
1058f8a47341SAlan Cox 
105989f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(new_object);
1060f8a47341SAlan Cox 	rv = vm_reserv_from_page(m);
1061f8a47341SAlan Cox 	if (rv->object == old_object) {
1062f8a47341SAlan Cox 		mtx_lock(&vm_page_queue_free_mtx);
1063f8a47341SAlan Cox 		if (rv->object == old_object) {
1064f8a47341SAlan Cox 			LIST_REMOVE(rv, objq);
1065f8a47341SAlan Cox 			LIST_INSERT_HEAD(&new_object->rvq, rv, objq);
1066f8a47341SAlan Cox 			rv->object = new_object;
1067f8a47341SAlan Cox 			rv->pindex -= old_object_offset;
1068f8a47341SAlan Cox 		}
1069f8a47341SAlan Cox 		mtx_unlock(&vm_page_queue_free_mtx);
1070f8a47341SAlan Cox 	}
1071f8a47341SAlan Cox }
1072f8a47341SAlan Cox 
1073f8a47341SAlan Cox /*
1074c869e672SAlan Cox  * Returns the size (in bytes) of a reservation of the specified level.
1075c869e672SAlan Cox  */
1076c869e672SAlan Cox int
1077c869e672SAlan Cox vm_reserv_size(int level)
1078c869e672SAlan Cox {
1079c869e672SAlan Cox 
1080c869e672SAlan Cox 	switch (level) {
1081c869e672SAlan Cox 	case 0:
1082c869e672SAlan Cox 		return (VM_LEVEL_0_SIZE);
1083c869e672SAlan Cox 	case -1:
1084c869e672SAlan Cox 		return (PAGE_SIZE);
1085c869e672SAlan Cox 	default:
1086c869e672SAlan Cox 		return (0);
1087c869e672SAlan Cox 	}
1088c869e672SAlan Cox }
1089c869e672SAlan Cox 
1090c869e672SAlan Cox /*
1091f8a47341SAlan Cox  * Allocates the virtual and physical memory required by the reservation
1092f8a47341SAlan Cox  * management system's data structures, in particular, the reservation array.
1093f8a47341SAlan Cox  */
1094f8a47341SAlan Cox vm_paddr_t
1095f8a47341SAlan Cox vm_reserv_startup(vm_offset_t *vaddr, vm_paddr_t end, vm_paddr_t high_water)
1096f8a47341SAlan Cox {
1097f8a47341SAlan Cox 	vm_paddr_t new_end;
1098f8a47341SAlan Cox 	size_t size;
1099f8a47341SAlan Cox 
1100f8a47341SAlan Cox 	/*
1101f8a47341SAlan Cox 	 * Calculate the size (in bytes) of the reservation array.  Round up
1102f8a47341SAlan Cox 	 * from "high_water" because every small page is mapped to an element
1103f8a47341SAlan Cox 	 * in the reservation array based on its physical address.  Thus, the
1104f8a47341SAlan Cox 	 * number of elements in the reservation array can be greater than the
1105f8a47341SAlan Cox 	 * number of superpages.
1106f8a47341SAlan Cox 	 */
1107f8a47341SAlan Cox 	size = howmany(high_water, VM_LEVEL_0_SIZE) * sizeof(struct vm_reserv);
1108f8a47341SAlan Cox 
1109f8a47341SAlan Cox 	/*
1110f8a47341SAlan Cox 	 * Allocate and map the physical memory for the reservation array.  The
1111f8a47341SAlan Cox 	 * next available virtual address is returned by reference.
1112f8a47341SAlan Cox 	 */
1113f8a47341SAlan Cox 	new_end = end - round_page(size);
1114f8a47341SAlan Cox 	vm_reserv_array = (void *)(uintptr_t)pmap_map(vaddr, new_end, end,
1115f8a47341SAlan Cox 	    VM_PROT_READ | VM_PROT_WRITE);
1116f8a47341SAlan Cox 	bzero(vm_reserv_array, size);
1117f8a47341SAlan Cox 
1118f8a47341SAlan Cox 	/*
1119f8a47341SAlan Cox 	 * Return the next available physical address.
1120f8a47341SAlan Cox 	 */
1121f8a47341SAlan Cox 	return (new_end);
1122f8a47341SAlan Cox }
1123f8a47341SAlan Cox 
1124*8b5e1472SAlan Cox /*
1125*8b5e1472SAlan Cox  * Returns the superpage containing the given page.
1126*8b5e1472SAlan Cox  */
1127*8b5e1472SAlan Cox vm_page_t
1128*8b5e1472SAlan Cox vm_reserv_to_superpage(vm_page_t m)
1129*8b5e1472SAlan Cox {
1130*8b5e1472SAlan Cox 	vm_reserv_t rv;
1131*8b5e1472SAlan Cox 
1132*8b5e1472SAlan Cox 	VM_OBJECT_ASSERT_LOCKED(m->object);
1133*8b5e1472SAlan Cox 	rv = vm_reserv_from_page(m);
1134*8b5e1472SAlan Cox 	return (rv->object == m->object && rv->popcnt == VM_LEVEL_0_NPAGES ?
1135*8b5e1472SAlan Cox 	    rv->pages : NULL);
1136*8b5e1472SAlan Cox }
1137*8b5e1472SAlan Cox 
1138f8a47341SAlan Cox #endif	/* VM_NRESERVLEVEL > 0 */
1139