xref: /freebsd/sys/vm/vm_reserv.c (revision dd05fa1945f915a29b23ff9f0450b00df0e89e68)
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>
54f8a47341SAlan Cox 
55f8a47341SAlan Cox #include <vm/vm.h>
56f8a47341SAlan Cox #include <vm/vm_param.h>
57f8a47341SAlan Cox #include <vm/vm_object.h>
58f8a47341SAlan Cox #include <vm/vm_page.h>
59f8a47341SAlan Cox #include <vm/vm_phys.h>
60774d251dSAttilio Rao #include <vm/vm_radix.h>
61f8a47341SAlan Cox #include <vm/vm_reserv.h>
62f8a47341SAlan Cox 
63f8a47341SAlan Cox /*
64f8a47341SAlan Cox  * The reservation system supports the speculative allocation of large physical
65f8a47341SAlan Cox  * pages ("superpages").  Speculative allocation enables the fully-automatic
66f8a47341SAlan Cox  * utilization of superpages by the virtual memory system.  In other words, no
67f8a47341SAlan Cox  * programmatic directives are required to use superpages.
68f8a47341SAlan Cox  */
69f8a47341SAlan Cox 
70f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
71f8a47341SAlan Cox 
72f8a47341SAlan Cox /*
73f8a47341SAlan Cox  * The number of small pages that are contained in a level 0 reservation
74f8a47341SAlan Cox  */
75f8a47341SAlan Cox #define	VM_LEVEL_0_NPAGES	(1 << VM_LEVEL_0_ORDER)
76f8a47341SAlan Cox 
77f8a47341SAlan Cox /*
78f8a47341SAlan Cox  * The number of bits by which a physical address is shifted to obtain the
79f8a47341SAlan Cox  * reservation number
80f8a47341SAlan Cox  */
81f8a47341SAlan Cox #define	VM_LEVEL_0_SHIFT	(VM_LEVEL_0_ORDER + PAGE_SHIFT)
82f8a47341SAlan Cox 
83f8a47341SAlan Cox /*
84f8a47341SAlan Cox  * The size of a level 0 reservation in bytes
85f8a47341SAlan Cox  */
86f8a47341SAlan Cox #define	VM_LEVEL_0_SIZE		(1 << VM_LEVEL_0_SHIFT)
87f8a47341SAlan Cox 
88f8a47341SAlan Cox /*
89f8a47341SAlan Cox  * Computes the index of the small page underlying the given (object, pindex)
90f8a47341SAlan Cox  * within the reservation's array of small pages.
91f8a47341SAlan Cox  */
92f8a47341SAlan Cox #define	VM_RESERV_INDEX(object, pindex)	\
93f8a47341SAlan Cox     (((object)->pg_color + (pindex)) & (VM_LEVEL_0_NPAGES - 1))
94f8a47341SAlan Cox 
95f8a47341SAlan Cox /*
96ec179322SAlan Cox  * The size of a population map entry
97ec179322SAlan Cox  */
98ec179322SAlan Cox typedef	u_long		popmap_t;
99ec179322SAlan Cox 
100ec179322SAlan Cox /*
101ec179322SAlan Cox  * The number of bits in a population map entry
102ec179322SAlan Cox  */
103ec179322SAlan Cox #define	NBPOPMAP	(NBBY * sizeof(popmap_t))
104ec179322SAlan Cox 
105ec179322SAlan Cox /*
106ec179322SAlan Cox  * The number of population map entries in a reservation
107ec179322SAlan Cox  */
108ec179322SAlan Cox #define	NPOPMAP		howmany(VM_LEVEL_0_NPAGES, NBPOPMAP)
109ec179322SAlan Cox 
110ec179322SAlan Cox /*
111f8a47341SAlan Cox  * The reservation structure
112f8a47341SAlan Cox  *
113f8a47341SAlan Cox  * A reservation structure is constructed whenever a large physical page is
114f8a47341SAlan Cox  * speculatively allocated to an object.  The reservation provides the small
115f8a47341SAlan Cox  * physical pages for the range [pindex, pindex + VM_LEVEL_0_NPAGES) of offsets
116f8a47341SAlan Cox  * within that object.  The reservation's "popcnt" tracks the number of these
117f8a47341SAlan Cox  * small physical pages that are in use at any given time.  When and if the
118f8a47341SAlan Cox  * reservation is not fully utilized, it appears in the queue of partially-
119f8a47341SAlan Cox  * populated reservations.  The reservation always appears on the containing
120f8a47341SAlan Cox  * object's list of reservations.
121f8a47341SAlan Cox  *
122f8a47341SAlan Cox  * A partially-populated reservation can be broken and reclaimed at any time.
123f8a47341SAlan Cox  */
124f8a47341SAlan Cox struct vm_reserv {
125f8a47341SAlan Cox 	TAILQ_ENTRY(vm_reserv) partpopq;
126f8a47341SAlan Cox 	LIST_ENTRY(vm_reserv) objq;
127f8a47341SAlan Cox 	vm_object_t	object;			/* containing object */
128f8a47341SAlan Cox 	vm_pindex_t	pindex;			/* offset within object */
129f8a47341SAlan Cox 	vm_page_t	pages;			/* first page of a superpage */
130f8a47341SAlan Cox 	int		popcnt;			/* # of pages in use */
131f8a47341SAlan Cox 	char		inpartpopq;
132ec179322SAlan Cox 	popmap_t	popmap[NPOPMAP];	/* bit vector of used pages */
133f8a47341SAlan Cox };
134f8a47341SAlan Cox 
135f8a47341SAlan Cox /*
136f8a47341SAlan Cox  * The reservation array
137f8a47341SAlan Cox  *
138f8a47341SAlan Cox  * This array is analoguous in function to vm_page_array.  It differs in the
139f8a47341SAlan Cox  * respect that it may contain a greater number of useful reservation
140f8a47341SAlan Cox  * structures than there are (physical) superpages.  These "invalid"
141f8a47341SAlan Cox  * reservation structures exist to trade-off space for time in the
142f8a47341SAlan Cox  * implementation of vm_reserv_from_page().  Invalid reservation structures are
143f8a47341SAlan Cox  * distinguishable from "valid" reservation structures by inspecting the
144f8a47341SAlan Cox  * reservation's "pages" field.  Invalid reservation structures have a NULL
145f8a47341SAlan Cox  * "pages" field.
146f8a47341SAlan Cox  *
147f8a47341SAlan Cox  * vm_reserv_from_page() maps a small (physical) page to an element of this
148f8a47341SAlan Cox  * array by computing a physical reservation number from the page's physical
149f8a47341SAlan Cox  * address.  The physical reservation number is used as the array index.
150f8a47341SAlan Cox  *
151f8a47341SAlan Cox  * An "active" reservation is a valid reservation structure that has a non-NULL
152f8a47341SAlan Cox  * "object" field and a non-zero "popcnt" field.  In other words, every active
153f8a47341SAlan Cox  * reservation belongs to a particular object.  Moreover, every active
154f8a47341SAlan Cox  * reservation has an entry in the containing object's list of reservations.
155f8a47341SAlan Cox  */
156f8a47341SAlan Cox static vm_reserv_t vm_reserv_array;
157f8a47341SAlan Cox 
158f8a47341SAlan Cox /*
159f8a47341SAlan Cox  * The partially-populated reservation queue
160f8a47341SAlan Cox  *
161f8a47341SAlan Cox  * This queue enables the fast recovery of an unused cached or free small page
162ab5378cfSAlan Cox  * from a partially-populated reservation.  The reservation at the head of
163ab5378cfSAlan Cox  * this queue is the least-recently-changed, partially-populated reservation.
164f8a47341SAlan Cox  *
165f8a47341SAlan Cox  * Access to this queue is synchronized by the free page queue lock.
166f8a47341SAlan Cox  */
167f8a47341SAlan Cox static TAILQ_HEAD(, vm_reserv) vm_rvq_partpop =
168f8a47341SAlan Cox 			    TAILQ_HEAD_INITIALIZER(vm_rvq_partpop);
169f8a47341SAlan Cox 
170f8a47341SAlan Cox static SYSCTL_NODE(_vm, OID_AUTO, reserv, CTLFLAG_RD, 0, "Reservation Info");
171f8a47341SAlan Cox 
172f8a47341SAlan Cox static long vm_reserv_broken;
173f8a47341SAlan Cox SYSCTL_LONG(_vm_reserv, OID_AUTO, broken, CTLFLAG_RD,
174f8a47341SAlan Cox     &vm_reserv_broken, 0, "Cumulative number of broken reservations");
175f8a47341SAlan Cox 
176f8a47341SAlan Cox static long vm_reserv_freed;
177f8a47341SAlan Cox SYSCTL_LONG(_vm_reserv, OID_AUTO, freed, CTLFLAG_RD,
178f8a47341SAlan Cox     &vm_reserv_freed, 0, "Cumulative number of freed reservations");
179f8a47341SAlan Cox 
180f8a47341SAlan Cox static int sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS);
181f8a47341SAlan Cox 
182f8a47341SAlan Cox SYSCTL_OID(_vm_reserv, OID_AUTO, partpopq, CTLTYPE_STRING | CTLFLAG_RD, NULL, 0,
183f8a47341SAlan Cox     sysctl_vm_reserv_partpopq, "A", "Partially-populated reservation queues");
184f8a47341SAlan Cox 
185f8a47341SAlan Cox static long vm_reserv_reclaimed;
186f8a47341SAlan Cox SYSCTL_LONG(_vm_reserv, OID_AUTO, reclaimed, CTLFLAG_RD,
187f8a47341SAlan Cox     &vm_reserv_reclaimed, 0, "Cumulative number of reclaimed reservations");
188f8a47341SAlan Cox 
189ec179322SAlan Cox static void		vm_reserv_break(vm_reserv_t rv, vm_page_t m);
190ec179322SAlan Cox static void		vm_reserv_depopulate(vm_reserv_t rv, int index);
191f8a47341SAlan Cox static vm_reserv_t	vm_reserv_from_page(vm_page_t m);
192f8a47341SAlan Cox static boolean_t	vm_reserv_has_pindex(vm_reserv_t rv,
193f8a47341SAlan Cox 			    vm_pindex_t pindex);
194ec179322SAlan Cox static void		vm_reserv_populate(vm_reserv_t rv, int index);
19544aab2c3SAlan Cox static void		vm_reserv_reclaim(vm_reserv_t rv);
196f8a47341SAlan Cox 
197f8a47341SAlan Cox /*
198f8a47341SAlan Cox  * Describes the current state of the partially-populated reservation queue.
199f8a47341SAlan Cox  */
200f8a47341SAlan Cox static int
201f8a47341SAlan Cox sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS)
202f8a47341SAlan Cox {
203f8a47341SAlan Cox 	struct sbuf sbuf;
204f8a47341SAlan Cox 	vm_reserv_t rv;
205f8a47341SAlan Cox 	int counter, error, level, unused_pages;
206f8a47341SAlan Cox 
20700f0e671SMatthew D Fleming 	error = sysctl_wire_old_buffer(req, 0);
20800f0e671SMatthew D Fleming 	if (error != 0)
20900f0e671SMatthew D Fleming 		return (error);
2104e657159SMatthew D Fleming 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
211f8a47341SAlan Cox 	sbuf_printf(&sbuf, "\nLEVEL     SIZE  NUMBER\n\n");
212f8a47341SAlan Cox 	for (level = -1; level <= VM_NRESERVLEVEL - 2; level++) {
213f8a47341SAlan Cox 		counter = 0;
214f8a47341SAlan Cox 		unused_pages = 0;
215f8a47341SAlan Cox 		mtx_lock(&vm_page_queue_free_mtx);
216f8a47341SAlan Cox 		TAILQ_FOREACH(rv, &vm_rvq_partpop/*[level]*/, partpopq) {
217f8a47341SAlan Cox 			counter++;
218f8a47341SAlan Cox 			unused_pages += VM_LEVEL_0_NPAGES - rv->popcnt;
219f8a47341SAlan Cox 		}
220f8a47341SAlan Cox 		mtx_unlock(&vm_page_queue_free_mtx);
221d689bc00SAlan Cox 		sbuf_printf(&sbuf, "%5d: %6dK, %6d\n", level,
2222cf36c8fSAlan Cox 		    unused_pages * ((int)PAGE_SIZE / 1024), counter);
223f8a47341SAlan Cox 	}
2244e657159SMatthew D Fleming 	error = sbuf_finish(&sbuf);
225f8a47341SAlan Cox 	sbuf_delete(&sbuf);
226f8a47341SAlan Cox 	return (error);
227f8a47341SAlan Cox }
228f8a47341SAlan Cox 
229f8a47341SAlan Cox /*
230f8a47341SAlan Cox  * Reduces the given reservation's population count.  If the population count
231f8a47341SAlan Cox  * becomes zero, the reservation is destroyed.  Additionally, moves the
232ec179322SAlan Cox  * reservation to the tail of the partially-populated reservation queue if the
233f8a47341SAlan Cox  * population count is non-zero.
234f8a47341SAlan Cox  *
235f8a47341SAlan Cox  * The free page queue lock must be held.
236f8a47341SAlan Cox  */
237f8a47341SAlan Cox static void
238ec179322SAlan Cox vm_reserv_depopulate(vm_reserv_t rv, int index)
239f8a47341SAlan Cox {
240f8a47341SAlan Cox 
241f8a47341SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
242f8a47341SAlan Cox 	KASSERT(rv->object != NULL,
243f8a47341SAlan Cox 	    ("vm_reserv_depopulate: reserv %p is free", rv));
244a08c1515SAlan Cox 	KASSERT(isset(rv->popmap, index),
245a08c1515SAlan Cox 	    ("vm_reserv_depopulate: reserv %p's popmap[%d] is clear", rv,
246a08c1515SAlan Cox 	    index));
247f8a47341SAlan Cox 	KASSERT(rv->popcnt > 0,
248f8a47341SAlan Cox 	    ("vm_reserv_depopulate: reserv %p's popcnt is corrupted", rv));
249f8a47341SAlan Cox 	if (rv->inpartpopq) {
250f8a47341SAlan Cox 		TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq);
251f8a47341SAlan Cox 		rv->inpartpopq = FALSE;
252*dd05fa19SAlan Cox 	} else {
253*dd05fa19SAlan Cox 		KASSERT(rv->pages->psind == 1,
254*dd05fa19SAlan Cox 		    ("vm_reserv_depopulate: reserv %p is already demoted",
255*dd05fa19SAlan Cox 		    rv));
256*dd05fa19SAlan Cox 		rv->pages->psind = 0;
257f8a47341SAlan Cox 	}
258ec179322SAlan Cox 	clrbit(rv->popmap, index);
259f8a47341SAlan Cox 	rv->popcnt--;
260f8a47341SAlan Cox 	if (rv->popcnt == 0) {
261f8a47341SAlan Cox 		LIST_REMOVE(rv, objq);
262f8a47341SAlan Cox 		rv->object = NULL;
263f8a47341SAlan Cox 		vm_phys_free_pages(rv->pages, VM_LEVEL_0_ORDER);
264f8a47341SAlan Cox 		vm_reserv_freed++;
265f8a47341SAlan Cox 	} else {
266f8a47341SAlan Cox 		rv->inpartpopq = TRUE;
267ab5378cfSAlan Cox 		TAILQ_INSERT_TAIL(&vm_rvq_partpop, rv, partpopq);
268f8a47341SAlan Cox 	}
269f8a47341SAlan Cox }
270f8a47341SAlan Cox 
271f8a47341SAlan Cox /*
272f8a47341SAlan Cox  * Returns the reservation to which the given page might belong.
273f8a47341SAlan Cox  */
274f8a47341SAlan Cox static __inline vm_reserv_t
275f8a47341SAlan Cox vm_reserv_from_page(vm_page_t m)
276f8a47341SAlan Cox {
277f8a47341SAlan Cox 
278f8a47341SAlan Cox 	return (&vm_reserv_array[VM_PAGE_TO_PHYS(m) >> VM_LEVEL_0_SHIFT]);
279f8a47341SAlan Cox }
280f8a47341SAlan Cox 
281f8a47341SAlan Cox /*
282f8a47341SAlan Cox  * Returns TRUE if the given reservation contains the given page index and
283f8a47341SAlan Cox  * FALSE otherwise.
284f8a47341SAlan Cox  */
285f8a47341SAlan Cox static __inline boolean_t
286f8a47341SAlan Cox vm_reserv_has_pindex(vm_reserv_t rv, vm_pindex_t pindex)
287f8a47341SAlan Cox {
288f8a47341SAlan Cox 
289f8a47341SAlan Cox 	return (((pindex - rv->pindex) & ~(VM_LEVEL_0_NPAGES - 1)) == 0);
290f8a47341SAlan Cox }
291f8a47341SAlan Cox 
292f8a47341SAlan Cox /*
293f8a47341SAlan Cox  * Increases the given reservation's population count.  Moves the reservation
294f8a47341SAlan Cox  * to the tail of the partially-populated reservation queue.
295f8a47341SAlan Cox  *
296f8a47341SAlan Cox  * The free page queue must be locked.
297f8a47341SAlan Cox  */
298f8a47341SAlan Cox static void
299ec179322SAlan Cox vm_reserv_populate(vm_reserv_t rv, int index)
300f8a47341SAlan Cox {
301f8a47341SAlan Cox 
302f8a47341SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
303f8a47341SAlan Cox 	KASSERT(rv->object != NULL,
304f8a47341SAlan Cox 	    ("vm_reserv_populate: reserv %p is free", rv));
305a08c1515SAlan Cox 	KASSERT(isclr(rv->popmap, index),
306a08c1515SAlan Cox 	    ("vm_reserv_populate: reserv %p's popmap[%d] is set", rv,
307a08c1515SAlan Cox 	    index));
308f8a47341SAlan Cox 	KASSERT(rv->popcnt < VM_LEVEL_0_NPAGES,
309f8a47341SAlan Cox 	    ("vm_reserv_populate: reserv %p is already full", rv));
310*dd05fa19SAlan Cox 	KASSERT(rv->pages->psind == 0,
311*dd05fa19SAlan Cox 	    ("vm_reserv_populate: reserv %p is already promoted", rv));
312f8a47341SAlan Cox 	if (rv->inpartpopq) {
313f8a47341SAlan Cox 		TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq);
314f8a47341SAlan Cox 		rv->inpartpopq = FALSE;
315f8a47341SAlan Cox 	}
316ec179322SAlan Cox 	setbit(rv->popmap, index);
317f8a47341SAlan Cox 	rv->popcnt++;
318f8a47341SAlan Cox 	if (rv->popcnt < VM_LEVEL_0_NPAGES) {
319f8a47341SAlan Cox 		rv->inpartpopq = TRUE;
320f8a47341SAlan Cox 		TAILQ_INSERT_TAIL(&vm_rvq_partpop, rv, partpopq);
321*dd05fa19SAlan Cox 	} else
322*dd05fa19SAlan Cox 		rv->pages->psind = 1;
323f8a47341SAlan Cox }
324f8a47341SAlan Cox 
325f8a47341SAlan Cox /*
326c68c3537SAlan Cox  * Allocates a contiguous set of physical pages of the given size "npages"
327c68c3537SAlan Cox  * from an existing or newly-created reservation.  All of the physical pages
328c68c3537SAlan Cox  * must be at or above the given physical address "low" and below the given
329c68c3537SAlan Cox  * physical address "high".  The given value "alignment" determines the
330c68c3537SAlan Cox  * alignment of the first physical page in the set.  If the given value
331c68c3537SAlan Cox  * "boundary" is non-zero, then the set of physical pages cannot cross any
332c68c3537SAlan Cox  * physical address boundary that is a multiple of that value.  Both
333c68c3537SAlan Cox  * "alignment" and "boundary" must be a power of two.
334c68c3537SAlan Cox  *
335c68c3537SAlan Cox  * The object and free page queue must be locked.
336c68c3537SAlan Cox  */
337c68c3537SAlan Cox vm_page_t
338c68c3537SAlan Cox vm_reserv_alloc_contig(vm_object_t object, vm_pindex_t pindex, u_long npages,
339c68c3537SAlan Cox     vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
340c68c3537SAlan Cox {
341c68c3537SAlan Cox 	vm_paddr_t pa, size;
342c68c3537SAlan Cox 	vm_page_t m, m_ret, mpred, msucc;
343c68c3537SAlan Cox 	vm_pindex_t first, leftcap, rightcap;
344c68c3537SAlan Cox 	vm_reserv_t rv;
345c68c3537SAlan Cox 	u_long allocpages, maxpages, minpages;
346c68c3537SAlan Cox 	int i, index, n;
347c68c3537SAlan Cox 
348c68c3537SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
34989f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
350c68c3537SAlan Cox 	KASSERT(npages != 0, ("vm_reserv_alloc_contig: npages is 0"));
351c68c3537SAlan Cox 
352c68c3537SAlan Cox 	/*
353c68c3537SAlan Cox 	 * Is a reservation fundamentally impossible?
354c68c3537SAlan Cox 	 */
355c68c3537SAlan Cox 	if (pindex < VM_RESERV_INDEX(object, pindex) ||
356c68c3537SAlan Cox 	    pindex + npages > object->size)
357c68c3537SAlan Cox 		return (NULL);
358c68c3537SAlan Cox 
359c68c3537SAlan Cox 	/*
360c68c3537SAlan Cox 	 * All reservations of a particular size have the same alignment.
361c68c3537SAlan Cox 	 * Assuming that the first page is allocated from a reservation, the
362c68c3537SAlan Cox 	 * least significant bits of its physical address can be determined
363c68c3537SAlan Cox 	 * from its offset from the beginning of the reservation and the size
364c68c3537SAlan Cox 	 * of the reservation.
365c68c3537SAlan Cox 	 *
366c68c3537SAlan Cox 	 * Could the specified index within a reservation of the smallest
367c68c3537SAlan Cox 	 * possible size satisfy the alignment and boundary requirements?
368c68c3537SAlan Cox 	 */
369c68c3537SAlan Cox 	pa = VM_RESERV_INDEX(object, pindex) << PAGE_SHIFT;
370c68c3537SAlan Cox 	if ((pa & (alignment - 1)) != 0)
371c68c3537SAlan Cox 		return (NULL);
372c68c3537SAlan Cox 	size = npages << PAGE_SHIFT;
373c68c3537SAlan Cox 	if (((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0)
374c68c3537SAlan Cox 		return (NULL);
375c68c3537SAlan Cox 
376c68c3537SAlan Cox 	/*
377c68c3537SAlan Cox 	 * Look for an existing reservation.
378c68c3537SAlan Cox 	 */
379774d251dSAttilio Rao 	mpred = vm_radix_lookup_le(&object->rtree, pindex);
380774d251dSAttilio Rao 	if (mpred != NULL) {
381774d251dSAttilio Rao 		KASSERT(mpred->pindex < pindex,
382c68c3537SAlan Cox 		    ("vm_reserv_alloc_contig: pindex already allocated"));
383c68c3537SAlan Cox 		rv = vm_reserv_from_page(mpred);
384c68c3537SAlan Cox 		if (rv->object == object && vm_reserv_has_pindex(rv, pindex))
385c68c3537SAlan Cox 			goto found;
386774d251dSAttilio Rao 		msucc = TAILQ_NEXT(mpred, listq);
387774d251dSAttilio Rao 	} else
388774d251dSAttilio Rao 		msucc = TAILQ_FIRST(&object->memq);
389774d251dSAttilio Rao 	if (msucc != NULL) {
390774d251dSAttilio Rao 		KASSERT(msucc->pindex > pindex,
391774d251dSAttilio Rao 		    ("vm_reserv_alloc_page: pindex already allocated"));
392c68c3537SAlan Cox 		rv = vm_reserv_from_page(msucc);
393774d251dSAttilio Rao 		if (rv->object == object && vm_reserv_has_pindex(rv, pindex))
394c68c3537SAlan Cox 			goto found;
395c68c3537SAlan Cox 	}
396c68c3537SAlan Cox 
397c68c3537SAlan Cox 	/*
398c68c3537SAlan Cox 	 * Could at least one reservation fit between the first index to the
399c68c3537SAlan Cox 	 * left that can be used and the first index to the right that cannot
400c68c3537SAlan Cox 	 * be used?
401c68c3537SAlan Cox 	 */
402c68c3537SAlan Cox 	first = pindex - VM_RESERV_INDEX(object, pindex);
403c68c3537SAlan Cox 	if (mpred != NULL) {
404c68c3537SAlan Cox 		if ((rv = vm_reserv_from_page(mpred))->object != object)
405c68c3537SAlan Cox 			leftcap = mpred->pindex + 1;
406c68c3537SAlan Cox 		else
407c68c3537SAlan Cox 			leftcap = rv->pindex + VM_LEVEL_0_NPAGES;
408c68c3537SAlan Cox 		if (leftcap > first)
409c68c3537SAlan Cox 			return (NULL);
410c68c3537SAlan Cox 	}
411c68c3537SAlan Cox 	minpages = VM_RESERV_INDEX(object, pindex) + npages;
412c68c3537SAlan Cox 	maxpages = roundup2(minpages, VM_LEVEL_0_NPAGES);
413c68c3537SAlan Cox 	allocpages = maxpages;
414c68c3537SAlan Cox 	if (msucc != NULL) {
415c68c3537SAlan Cox 		if ((rv = vm_reserv_from_page(msucc))->object != object)
416c68c3537SAlan Cox 			rightcap = msucc->pindex;
417c68c3537SAlan Cox 		else
418c68c3537SAlan Cox 			rightcap = rv->pindex;
419c68c3537SAlan Cox 		if (first + maxpages > rightcap) {
420c68c3537SAlan Cox 			if (maxpages == VM_LEVEL_0_NPAGES)
421c68c3537SAlan Cox 				return (NULL);
422c68c3537SAlan Cox 			allocpages = minpages;
423c68c3537SAlan Cox 		}
424c68c3537SAlan Cox 	}
425c68c3537SAlan Cox 
426c68c3537SAlan Cox 	/*
427c68c3537SAlan Cox 	 * Would the last new reservation extend past the end of the object?
428c68c3537SAlan Cox 	 */
429c68c3537SAlan Cox 	if (first + maxpages > object->size) {
430c68c3537SAlan Cox 		/*
431c68c3537SAlan Cox 		 * Don't allocate the last new reservation if the object is a
432c68c3537SAlan Cox 		 * vnode or backed by another object that is a vnode.
433c68c3537SAlan Cox 		 */
434c68c3537SAlan Cox 		if (object->type == OBJT_VNODE ||
435c68c3537SAlan Cox 		    (object->backing_object != NULL &&
436c68c3537SAlan Cox 		    object->backing_object->type == OBJT_VNODE)) {
437c68c3537SAlan Cox 			if (maxpages == VM_LEVEL_0_NPAGES)
438c68c3537SAlan Cox 				return (NULL);
439c68c3537SAlan Cox 			allocpages = minpages;
440c68c3537SAlan Cox 		}
441c68c3537SAlan Cox 		/* Speculate that the object may grow. */
442c68c3537SAlan Cox 	}
443c68c3537SAlan Cox 
444c68c3537SAlan Cox 	/*
445c68c3537SAlan Cox 	 * Allocate and populate the new reservations.  The alignment and
446c68c3537SAlan Cox 	 * boundary specified for this allocation may be different from the
447c68c3537SAlan Cox 	 * alignment and boundary specified for the requested pages.  For
448c68c3537SAlan Cox 	 * instance, the specified index may not be the first page within the
449c68c3537SAlan Cox 	 * first new reservation.
450c68c3537SAlan Cox 	 */
451c68c3537SAlan Cox 	m = vm_phys_alloc_contig(allocpages, low, high, ulmax(alignment,
452c68c3537SAlan Cox 	    VM_LEVEL_0_SIZE), boundary > VM_LEVEL_0_SIZE ? boundary : 0);
453c68c3537SAlan Cox 	if (m == NULL)
454c68c3537SAlan Cox 		return (NULL);
455c68c3537SAlan Cox 	m_ret = NULL;
456c68c3537SAlan Cox 	index = VM_RESERV_INDEX(object, pindex);
457c68c3537SAlan Cox 	do {
458c68c3537SAlan Cox 		rv = vm_reserv_from_page(m);
459c68c3537SAlan Cox 		KASSERT(rv->pages == m,
460c68c3537SAlan Cox 		    ("vm_reserv_alloc_contig: reserv %p's pages is corrupted",
461c68c3537SAlan Cox 		    rv));
462c68c3537SAlan Cox 		KASSERT(rv->object == NULL,
463c68c3537SAlan Cox 		    ("vm_reserv_alloc_contig: reserv %p isn't free", rv));
464c68c3537SAlan Cox 		LIST_INSERT_HEAD(&object->rvq, rv, objq);
465c68c3537SAlan Cox 		rv->object = object;
466c68c3537SAlan Cox 		rv->pindex = first;
467c68c3537SAlan Cox 		KASSERT(rv->popcnt == 0,
468c68c3537SAlan Cox 		    ("vm_reserv_alloc_contig: reserv %p's popcnt is corrupted",
469c68c3537SAlan Cox 		    rv));
470c68c3537SAlan Cox 		KASSERT(!rv->inpartpopq,
471c68c3537SAlan Cox 		    ("vm_reserv_alloc_contig: reserv %p's inpartpopq is TRUE",
472c68c3537SAlan Cox 		    rv));
473ec179322SAlan Cox 		for (i = 0; i < NPOPMAP; i++)
474ec179322SAlan Cox 			KASSERT(rv->popmap[i] == 0,
475ec179322SAlan Cox 		    ("vm_reserv_alloc_contig: reserv %p's popmap is corrupted",
476ec179322SAlan Cox 			    rv));
477c68c3537SAlan Cox 		n = ulmin(VM_LEVEL_0_NPAGES - index, npages);
478c68c3537SAlan Cox 		for (i = 0; i < n; i++)
479ec179322SAlan Cox 			vm_reserv_populate(rv, index + i);
480c68c3537SAlan Cox 		npages -= n;
481c68c3537SAlan Cox 		if (m_ret == NULL) {
482c68c3537SAlan Cox 			m_ret = &rv->pages[index];
483c68c3537SAlan Cox 			index = 0;
484c68c3537SAlan Cox 		}
485c68c3537SAlan Cox 		m += VM_LEVEL_0_NPAGES;
486c68c3537SAlan Cox 		first += VM_LEVEL_0_NPAGES;
487c68c3537SAlan Cox 		allocpages -= VM_LEVEL_0_NPAGES;
488476f7f24SAlan Cox 	} while (allocpages > 0);
489c68c3537SAlan Cox 	return (m_ret);
490c68c3537SAlan Cox 
491c68c3537SAlan Cox 	/*
492c68c3537SAlan Cox 	 * Found a matching reservation.
493c68c3537SAlan Cox 	 */
494c68c3537SAlan Cox found:
495c68c3537SAlan Cox 	index = VM_RESERV_INDEX(object, pindex);
496c68c3537SAlan Cox 	/* Does the allocation fit within the reservation? */
497c68c3537SAlan Cox 	if (index + npages > VM_LEVEL_0_NPAGES)
498c68c3537SAlan Cox 		return (NULL);
499c68c3537SAlan Cox 	m = &rv->pages[index];
500c68c3537SAlan Cox 	pa = VM_PAGE_TO_PHYS(m);
501c68c3537SAlan Cox 	if (pa < low || pa + size > high || (pa & (alignment - 1)) != 0 ||
502c68c3537SAlan Cox 	    ((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0)
503c68c3537SAlan Cox 		return (NULL);
504c68c3537SAlan Cox 	/* Handle vm_page_rename(m, new_object, ...). */
505c68c3537SAlan Cox 	for (i = 0; i < npages; i++)
506ec179322SAlan Cox 		if (isset(rv->popmap, index + i))
507c68c3537SAlan Cox 			return (NULL);
508c68c3537SAlan Cox 	for (i = 0; i < npages; i++)
509ec179322SAlan Cox 		vm_reserv_populate(rv, index + i);
510c68c3537SAlan Cox 	return (m);
511c68c3537SAlan Cox }
512c68c3537SAlan Cox 
513c68c3537SAlan Cox /*
514f8a47341SAlan Cox  * Allocates a page from an existing or newly-created reservation.
515f8a47341SAlan Cox  *
516404eb1b3SAlan Cox  * The page "mpred" must immediately precede the offset "pindex" within the
517404eb1b3SAlan Cox  * specified object.
518404eb1b3SAlan Cox  *
519f8a47341SAlan Cox  * The object and free page queue must be locked.
520f8a47341SAlan Cox  */
521f8a47341SAlan Cox vm_page_t
522404eb1b3SAlan Cox vm_reserv_alloc_page(vm_object_t object, vm_pindex_t pindex, vm_page_t mpred)
523f8a47341SAlan Cox {
524404eb1b3SAlan Cox 	vm_page_t m, msucc;
525f8a47341SAlan Cox 	vm_pindex_t first, leftcap, rightcap;
526f8a47341SAlan Cox 	vm_reserv_t rv;
527ec179322SAlan Cox 	int i, index;
528f8a47341SAlan Cox 
529f8a47341SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
53089f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
531f8a47341SAlan Cox 
532f8a47341SAlan Cox 	/*
533c68c3537SAlan Cox 	 * Is a reservation fundamentally impossible?
534f8a47341SAlan Cox 	 */
535f8a47341SAlan Cox 	if (pindex < VM_RESERV_INDEX(object, pindex) ||
536f8a47341SAlan Cox 	    pindex >= object->size)
537f8a47341SAlan Cox 		return (NULL);
538f8a47341SAlan Cox 
539f8a47341SAlan Cox 	/*
540f8a47341SAlan Cox 	 * Look for an existing reservation.
541f8a47341SAlan Cox 	 */
542774d251dSAttilio Rao 	if (mpred != NULL) {
5439eab5484SKonstantin Belousov 		KASSERT(mpred->object == object,
544404eb1b3SAlan Cox 		    ("vm_reserv_alloc_page: object doesn't contain mpred"));
545774d251dSAttilio Rao 		KASSERT(mpred->pindex < pindex,
546404eb1b3SAlan Cox 		    ("vm_reserv_alloc_page: mpred doesn't precede pindex"));
547f8a47341SAlan Cox 		rv = vm_reserv_from_page(mpred);
548c68c3537SAlan Cox 		if (rv->object == object && vm_reserv_has_pindex(rv, pindex))
549c68c3537SAlan Cox 			goto found;
550774d251dSAttilio Rao 		msucc = TAILQ_NEXT(mpred, listq);
551774d251dSAttilio Rao 	} else
552774d251dSAttilio Rao 		msucc = TAILQ_FIRST(&object->memq);
553774d251dSAttilio Rao 	if (msucc != NULL) {
554774d251dSAttilio Rao 		KASSERT(msucc->pindex > pindex,
555404eb1b3SAlan Cox 		    ("vm_reserv_alloc_page: msucc doesn't succeed pindex"));
556f8a47341SAlan Cox 		rv = vm_reserv_from_page(msucc);
557774d251dSAttilio Rao 		if (rv->object == object && vm_reserv_has_pindex(rv, pindex))
558c68c3537SAlan Cox 			goto found;
559f8a47341SAlan Cox 	}
560f8a47341SAlan Cox 
561f8a47341SAlan Cox 	/*
562c68c3537SAlan Cox 	 * Could a reservation fit between the first index to the left that
563c68c3537SAlan Cox 	 * can be used and the first index to the right that cannot be used?
564f8a47341SAlan Cox 	 */
565c68c3537SAlan Cox 	first = pindex - VM_RESERV_INDEX(object, pindex);
566c68c3537SAlan Cox 	if (mpred != NULL) {
567c68c3537SAlan Cox 		if ((rv = vm_reserv_from_page(mpred))->object != object)
568f8a47341SAlan Cox 			leftcap = mpred->pindex + 1;
569f8a47341SAlan Cox 		else
570f8a47341SAlan Cox 			leftcap = rv->pindex + VM_LEVEL_0_NPAGES;
571c68c3537SAlan Cox 		if (leftcap > first)
572c68c3537SAlan Cox 			return (NULL);
573c68c3537SAlan Cox 	}
574c68c3537SAlan Cox 	if (msucc != NULL) {
575c68c3537SAlan Cox 		if ((rv = vm_reserv_from_page(msucc))->object != object)
576f8a47341SAlan Cox 			rightcap = msucc->pindex;
577f8a47341SAlan Cox 		else
578f8a47341SAlan Cox 			rightcap = rv->pindex;
579c68c3537SAlan Cox 		if (first + VM_LEVEL_0_NPAGES > rightcap)
580f8a47341SAlan Cox 			return (NULL);
581c68c3537SAlan Cox 	}
582f8a47341SAlan Cox 
583f8a47341SAlan Cox 	/*
584c68c3537SAlan Cox 	 * Would a new reservation extend past the end of the object?
585f8a47341SAlan Cox 	 */
586c68c3537SAlan Cox 	if (first + VM_LEVEL_0_NPAGES > object->size) {
587f8a47341SAlan Cox 		/*
588f8a47341SAlan Cox 		 * Don't allocate a new reservation if the object is a vnode or
589f8a47341SAlan Cox 		 * backed by another object that is a vnode.
590f8a47341SAlan Cox 		 */
591f8a47341SAlan Cox 		if (object->type == OBJT_VNODE ||
592f8a47341SAlan Cox 		    (object->backing_object != NULL &&
593f8a47341SAlan Cox 		    object->backing_object->type == OBJT_VNODE))
594f8a47341SAlan Cox 			return (NULL);
595f8a47341SAlan Cox 		/* Speculate that the object may grow. */
596f8a47341SAlan Cox 	}
597f8a47341SAlan Cox 
598f8a47341SAlan Cox 	/*
599c68c3537SAlan Cox 	 * Allocate and populate the new reservation.
600f8a47341SAlan Cox 	 */
601f8a47341SAlan Cox 	m = vm_phys_alloc_pages(VM_FREEPOOL_DEFAULT, VM_LEVEL_0_ORDER);
602c68c3537SAlan Cox 	if (m == NULL)
603c68c3537SAlan Cox 		return (NULL);
604f8a47341SAlan Cox 	rv = vm_reserv_from_page(m);
605f8a47341SAlan Cox 	KASSERT(rv->pages == m,
606c68c3537SAlan Cox 	    ("vm_reserv_alloc_page: reserv %p's pages is corrupted", rv));
607f8a47341SAlan Cox 	KASSERT(rv->object == NULL,
608f8a47341SAlan Cox 	    ("vm_reserv_alloc_page: reserv %p isn't free", rv));
609f8a47341SAlan Cox 	LIST_INSERT_HEAD(&object->rvq, rv, objq);
610f8a47341SAlan Cox 	rv->object = object;
611f8a47341SAlan Cox 	rv->pindex = first;
612f8a47341SAlan Cox 	KASSERT(rv->popcnt == 0,
613c68c3537SAlan Cox 	    ("vm_reserv_alloc_page: reserv %p's popcnt is corrupted", rv));
614f8a47341SAlan Cox 	KASSERT(!rv->inpartpopq,
615c68c3537SAlan Cox 	    ("vm_reserv_alloc_page: reserv %p's inpartpopq is TRUE", rv));
616ec179322SAlan Cox 	for (i = 0; i < NPOPMAP; i++)
617ec179322SAlan Cox 		KASSERT(rv->popmap[i] == 0,
618ec179322SAlan Cox 		    ("vm_reserv_alloc_page: reserv %p's popmap is corrupted",
619ec179322SAlan Cox 		    rv));
620ec179322SAlan Cox 	index = VM_RESERV_INDEX(object, pindex);
621ec179322SAlan Cox 	vm_reserv_populate(rv, index);
622ec179322SAlan Cox 	return (&rv->pages[index]);
623c68c3537SAlan Cox 
624c68c3537SAlan Cox 	/*
625c68c3537SAlan Cox 	 * Found a matching reservation.
626c68c3537SAlan Cox 	 */
627c68c3537SAlan Cox found:
628ec179322SAlan Cox 	index = VM_RESERV_INDEX(object, pindex);
629ec179322SAlan Cox 	m = &rv->pages[index];
630c68c3537SAlan Cox 	/* Handle vm_page_rename(m, new_object, ...). */
631ec179322SAlan Cox 	if (isset(rv->popmap, index))
632c68c3537SAlan Cox 		return (NULL);
633ec179322SAlan Cox 	vm_reserv_populate(rv, index);
634f8a47341SAlan Cox 	return (m);
635f8a47341SAlan Cox }
636f8a47341SAlan Cox 
637f8a47341SAlan Cox /*
638ec179322SAlan Cox  * Breaks the given reservation.  Except for the specified cached or free
639ec179322SAlan Cox  * page, all cached and free pages in the reservation are returned to the
640ec179322SAlan Cox  * physical memory allocator.  The reservation's population count and map are
641ec179322SAlan Cox  * reset to their initial state.
642ec179322SAlan Cox  *
643ec179322SAlan Cox  * The given reservation must not be in the partially-populated reservation
644ec179322SAlan Cox  * queue.  The free page queue lock must be held.
645ec179322SAlan Cox  */
646ec179322SAlan Cox static void
647ec179322SAlan Cox vm_reserv_break(vm_reserv_t rv, vm_page_t m)
648ec179322SAlan Cox {
649ec179322SAlan Cox 	int begin_zeroes, hi, i, lo;
650ec179322SAlan Cox 
651ec179322SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
652ec179322SAlan Cox 	KASSERT(rv->object != NULL,
653ec179322SAlan Cox 	    ("vm_reserv_break: reserv %p is free", rv));
654ec179322SAlan Cox 	KASSERT(!rv->inpartpopq,
655ec179322SAlan Cox 	    ("vm_reserv_break: reserv %p's inpartpopq is TRUE", rv));
656ec179322SAlan Cox 	LIST_REMOVE(rv, objq);
657ec179322SAlan Cox 	rv->object = NULL;
658ec179322SAlan Cox 	if (m != NULL) {
659ec179322SAlan Cox 		/*
660ec179322SAlan Cox 		 * Since the reservation is being broken, there is no harm in
661ec179322SAlan Cox 		 * abusing the population map to stop "m" from being returned
662ec179322SAlan Cox 		 * to the physical memory allocator.
663ec179322SAlan Cox 		 */
664ec179322SAlan Cox 		i = m - rv->pages;
665ec179322SAlan Cox 		KASSERT(isclr(rv->popmap, i),
666ec179322SAlan Cox 		    ("vm_reserv_break: reserv %p's popmap is corrupted", rv));
667ec179322SAlan Cox 		setbit(rv->popmap, i);
668ec179322SAlan Cox 		rv->popcnt++;
669ec179322SAlan Cox 	}
670ec179322SAlan Cox 	i = hi = 0;
671ec179322SAlan Cox 	do {
672ec179322SAlan Cox 		/* Find the next 0 bit.  Any previous 0 bits are < "hi". */
673ec179322SAlan Cox 		lo = ffsl(~(((1UL << hi) - 1) | rv->popmap[i]));
674ec179322SAlan Cox 		if (lo == 0) {
675ec179322SAlan Cox 			/* Redundantly clears bits < "hi". */
676ec179322SAlan Cox 			rv->popmap[i] = 0;
677ec179322SAlan Cox 			rv->popcnt -= NBPOPMAP - hi;
678ec179322SAlan Cox 			while (++i < NPOPMAP) {
679ec179322SAlan Cox 				lo = ffsl(~rv->popmap[i]);
680ec179322SAlan Cox 				if (lo == 0) {
681ec179322SAlan Cox 					rv->popmap[i] = 0;
682ec179322SAlan Cox 					rv->popcnt -= NBPOPMAP;
683ec179322SAlan Cox 				} else
684ec179322SAlan Cox 					break;
685ec179322SAlan Cox 			}
686ec179322SAlan Cox 			if (i == NPOPMAP)
687ec179322SAlan Cox 				break;
688ec179322SAlan Cox 			hi = 0;
689ec179322SAlan Cox 		}
690ec179322SAlan Cox 		KASSERT(lo > 0, ("vm_reserv_break: lo is %d", lo));
691ec179322SAlan Cox 		/* Convert from ffsl() to ordinary bit numbering. */
692ec179322SAlan Cox 		lo--;
693ec179322SAlan Cox 		if (lo > 0) {
694ec179322SAlan Cox 			/* Redundantly clears bits < "hi". */
695ec179322SAlan Cox 			rv->popmap[i] &= ~((1UL << lo) - 1);
696ec179322SAlan Cox 			rv->popcnt -= lo - hi;
697ec179322SAlan Cox 		}
698ec179322SAlan Cox 		begin_zeroes = NBPOPMAP * i + lo;
699ec179322SAlan Cox 		/* Find the next 1 bit. */
700ec179322SAlan Cox 		do
701ec179322SAlan Cox 			hi = ffsl(rv->popmap[i]);
702ec179322SAlan Cox 		while (hi == 0 && ++i < NPOPMAP);
703ec179322SAlan Cox 		if (i != NPOPMAP)
704ec179322SAlan Cox 			/* Convert from ffsl() to ordinary bit numbering. */
705ec179322SAlan Cox 			hi--;
706ec179322SAlan Cox 		vm_phys_free_contig(&rv->pages[begin_zeroes], NBPOPMAP * i +
707ec179322SAlan Cox 		    hi - begin_zeroes);
708ec179322SAlan Cox 	} while (i < NPOPMAP);
709ec179322SAlan Cox 	KASSERT(rv->popcnt == 0,
710ec179322SAlan Cox 	    ("vm_reserv_break: reserv %p's popcnt is corrupted", rv));
711ec179322SAlan Cox 	vm_reserv_broken++;
712ec179322SAlan Cox }
713ec179322SAlan Cox 
714ec179322SAlan Cox /*
715f8a47341SAlan Cox  * Breaks all reservations belonging to the given object.
716f8a47341SAlan Cox  */
717f8a47341SAlan Cox void
718f8a47341SAlan Cox vm_reserv_break_all(vm_object_t object)
719f8a47341SAlan Cox {
720f8a47341SAlan Cox 	vm_reserv_t rv;
721f8a47341SAlan Cox 
722f8a47341SAlan Cox 	mtx_lock(&vm_page_queue_free_mtx);
723f8a47341SAlan Cox 	while ((rv = LIST_FIRST(&object->rvq)) != NULL) {
724f8a47341SAlan Cox 		KASSERT(rv->object == object,
725f8a47341SAlan Cox 		    ("vm_reserv_break_all: reserv %p is corrupted", rv));
726f8a47341SAlan Cox 		if (rv->inpartpopq) {
727f8a47341SAlan Cox 			TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq);
728f8a47341SAlan Cox 			rv->inpartpopq = FALSE;
729f8a47341SAlan Cox 		}
730ec179322SAlan Cox 		vm_reserv_break(rv, NULL);
731f8a47341SAlan Cox 	}
732f8a47341SAlan Cox 	mtx_unlock(&vm_page_queue_free_mtx);
733f8a47341SAlan Cox }
734f8a47341SAlan Cox 
735f8a47341SAlan Cox /*
736f8a47341SAlan Cox  * Frees the given page if it belongs to a reservation.  Returns TRUE if the
737f8a47341SAlan Cox  * page is freed and FALSE otherwise.
738f8a47341SAlan Cox  *
739f8a47341SAlan Cox  * The free page queue lock must be held.
740f8a47341SAlan Cox  */
741f8a47341SAlan Cox boolean_t
742f8a47341SAlan Cox vm_reserv_free_page(vm_page_t m)
743f8a47341SAlan Cox {
744f8a47341SAlan Cox 	vm_reserv_t rv;
745f8a47341SAlan Cox 
746f8a47341SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
747f8a47341SAlan Cox 	rv = vm_reserv_from_page(m);
748908e3da1SAlan Cox 	if (rv->object == NULL)
749908e3da1SAlan Cox 		return (FALSE);
750908e3da1SAlan Cox 	if ((m->flags & PG_CACHED) != 0 && m->pool != VM_FREEPOOL_CACHE)
751908e3da1SAlan Cox 		vm_phys_set_pool(VM_FREEPOOL_CACHE, rv->pages,
752908e3da1SAlan Cox 		    VM_LEVEL_0_ORDER);
753ec179322SAlan Cox 	vm_reserv_depopulate(rv, m - rv->pages);
754f8a47341SAlan Cox 	return (TRUE);
755f8a47341SAlan Cox }
756f8a47341SAlan Cox 
757f8a47341SAlan Cox /*
758f8a47341SAlan Cox  * Initializes the reservation management system.  Specifically, initializes
759f8a47341SAlan Cox  * the reservation array.
760f8a47341SAlan Cox  *
761f8a47341SAlan Cox  * Requires that vm_page_array and first_page are initialized!
762f8a47341SAlan Cox  */
763f8a47341SAlan Cox void
764f8a47341SAlan Cox vm_reserv_init(void)
765f8a47341SAlan Cox {
766f8a47341SAlan Cox 	vm_paddr_t paddr;
767f8a47341SAlan Cox 	int i;
768f8a47341SAlan Cox 
769f8a47341SAlan Cox 	/*
770f8a47341SAlan Cox 	 * Initialize the reservation array.  Specifically, initialize the
771f8a47341SAlan Cox 	 * "pages" field for every element that has an underlying superpage.
772f8a47341SAlan Cox 	 */
773f8a47341SAlan Cox 	for (i = 0; phys_avail[i + 1] != 0; i += 2) {
774f8a47341SAlan Cox 		paddr = roundup2(phys_avail[i], VM_LEVEL_0_SIZE);
775f8a47341SAlan Cox 		while (paddr + VM_LEVEL_0_SIZE <= phys_avail[i + 1]) {
776f8a47341SAlan Cox 			vm_reserv_array[paddr >> VM_LEVEL_0_SHIFT].pages =
777f8a47341SAlan Cox 			    PHYS_TO_VM_PAGE(paddr);
778f8a47341SAlan Cox 			paddr += VM_LEVEL_0_SIZE;
779f8a47341SAlan Cox 		}
780f8a47341SAlan Cox 	}
781f8a47341SAlan Cox }
782f8a47341SAlan Cox 
783f8a47341SAlan Cox /*
784f8a47341SAlan Cox  * Returns a reservation level if the given page belongs to a fully-populated
785f8a47341SAlan Cox  * reservation and -1 otherwise.
786f8a47341SAlan Cox  */
787f8a47341SAlan Cox int
788f8a47341SAlan Cox vm_reserv_level_iffullpop(vm_page_t m)
789f8a47341SAlan Cox {
790f8a47341SAlan Cox 	vm_reserv_t rv;
791f8a47341SAlan Cox 
792f8a47341SAlan Cox 	rv = vm_reserv_from_page(m);
793f8a47341SAlan Cox 	return (rv->popcnt == VM_LEVEL_0_NPAGES ? 0 : -1);
794f8a47341SAlan Cox }
795f8a47341SAlan Cox 
796f8a47341SAlan Cox /*
797f8a47341SAlan Cox  * Prepare for the reactivation of a cached page.
798f8a47341SAlan Cox  *
799f8a47341SAlan Cox  * First, suppose that the given page "m" was allocated individually, i.e., not
800f8a47341SAlan Cox  * as part of a reservation, and cached.  Then, suppose a reservation
801f8a47341SAlan Cox  * containing "m" is allocated by the same object.  Although "m" and the
802f8a47341SAlan Cox  * reservation belong to the same object, "m"'s pindex may not match the
803f8a47341SAlan Cox  * reservation's.
804f8a47341SAlan Cox  *
805f8a47341SAlan Cox  * The free page queue must be locked.
806f8a47341SAlan Cox  */
807f8a47341SAlan Cox boolean_t
808f8a47341SAlan Cox vm_reserv_reactivate_page(vm_page_t m)
809f8a47341SAlan Cox {
810f8a47341SAlan Cox 	vm_reserv_t rv;
811ec179322SAlan Cox 	int index;
812f8a47341SAlan Cox 
813f8a47341SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
814f8a47341SAlan Cox 	rv = vm_reserv_from_page(m);
815f8a47341SAlan Cox 	if (rv->object == NULL)
816f8a47341SAlan Cox 		return (FALSE);
817f8a47341SAlan Cox 	KASSERT((m->flags & PG_CACHED) != 0,
818ec179322SAlan Cox 	    ("vm_reserv_reactivate_page: page %p is not cached", m));
819f8a47341SAlan Cox 	if (m->object == rv->object &&
820ec179322SAlan Cox 	    m->pindex - rv->pindex == (index = VM_RESERV_INDEX(m->object,
821ec179322SAlan Cox 	    m->pindex)))
822ec179322SAlan Cox 		vm_reserv_populate(rv, index);
823f8a47341SAlan Cox 	else {
824f8a47341SAlan Cox 		KASSERT(rv->inpartpopq,
825ec179322SAlan Cox 	    ("vm_reserv_reactivate_page: reserv %p's inpartpopq is FALSE",
826f8a47341SAlan Cox 		    rv));
827f8a47341SAlan Cox 		TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq);
828f8a47341SAlan Cox 		rv->inpartpopq = FALSE;
829ec179322SAlan Cox 		/* Don't release "m" to the physical memory allocator. */
830ec179322SAlan Cox 		vm_reserv_break(rv, m);
831f8a47341SAlan Cox 	}
832f8a47341SAlan Cox 	return (TRUE);
833f8a47341SAlan Cox }
834f8a47341SAlan Cox 
835f8a47341SAlan Cox /*
83644aab2c3SAlan Cox  * Breaks the given partially-populated reservation, releasing its cached and
83744aab2c3SAlan Cox  * free pages to the physical memory allocator.
838f8a47341SAlan Cox  *
839f8a47341SAlan Cox  * The free page queue lock must be held.
840f8a47341SAlan Cox  */
84144aab2c3SAlan Cox static void
84244aab2c3SAlan Cox vm_reserv_reclaim(vm_reserv_t rv)
843f8a47341SAlan Cox {
844f8a47341SAlan Cox 
845f8a47341SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
846f8a47341SAlan Cox 	KASSERT(rv->inpartpopq,
847ec179322SAlan Cox 	    ("vm_reserv_reclaim: reserv %p's inpartpopq is FALSE", rv));
848f8a47341SAlan Cox 	TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq);
849f8a47341SAlan Cox 	rv->inpartpopq = FALSE;
850ec179322SAlan Cox 	vm_reserv_break(rv, NULL);
851f8a47341SAlan Cox 	vm_reserv_reclaimed++;
85244aab2c3SAlan Cox }
85344aab2c3SAlan Cox 
85444aab2c3SAlan Cox /*
85544aab2c3SAlan Cox  * Breaks the reservation at the head of the partially-populated reservation
85644aab2c3SAlan Cox  * queue, releasing its cached and free pages to the physical memory
85744aab2c3SAlan Cox  * allocator.  Returns TRUE if a reservation is broken and FALSE otherwise.
85844aab2c3SAlan Cox  *
85944aab2c3SAlan Cox  * The free page queue lock must be held.
86044aab2c3SAlan Cox  */
86144aab2c3SAlan Cox boolean_t
86244aab2c3SAlan Cox vm_reserv_reclaim_inactive(void)
86344aab2c3SAlan Cox {
86444aab2c3SAlan Cox 	vm_reserv_t rv;
86544aab2c3SAlan Cox 
86644aab2c3SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
86744aab2c3SAlan Cox 	if ((rv = TAILQ_FIRST(&vm_rvq_partpop)) != NULL) {
86844aab2c3SAlan Cox 		vm_reserv_reclaim(rv);
869f8a47341SAlan Cox 		return (TRUE);
870f8a47341SAlan Cox 	}
871f8a47341SAlan Cox 	return (FALSE);
872f8a47341SAlan Cox }
873f8a47341SAlan Cox 
874f8a47341SAlan Cox /*
87544aab2c3SAlan Cox  * Searches the partially-populated reservation queue for the least recently
87644aab2c3SAlan Cox  * active reservation with unused pages, i.e., cached or free, that satisfy the
87744aab2c3SAlan Cox  * given request for contiguous physical memory.  If a satisfactory reservation
87844aab2c3SAlan Cox  * is found, it is broken.  Returns TRUE if a reservation is broken and FALSE
87944aab2c3SAlan Cox  * otherwise.
88044aab2c3SAlan Cox  *
88144aab2c3SAlan Cox  * The free page queue lock must be held.
88244aab2c3SAlan Cox  */
88344aab2c3SAlan Cox boolean_t
884c68c3537SAlan Cox vm_reserv_reclaim_contig(u_long npages, vm_paddr_t low, vm_paddr_t high,
8855c1f2cc4SAlan Cox     u_long alignment, vm_paddr_t boundary)
88644aab2c3SAlan Cox {
887ec179322SAlan Cox 	vm_paddr_t pa, size;
88844aab2c3SAlan Cox 	vm_reserv_t rv;
889ec179322SAlan Cox 	int hi, i, lo, next_free;
89044aab2c3SAlan Cox 
89144aab2c3SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
892c68c3537SAlan Cox 	if (npages > VM_LEVEL_0_NPAGES - 1)
89344aab2c3SAlan Cox 		return (FALSE);
894c68c3537SAlan Cox 	size = npages << PAGE_SHIFT;
89544aab2c3SAlan Cox 	TAILQ_FOREACH(rv, &vm_rvq_partpop, partpopq) {
89644aab2c3SAlan Cox 		pa = VM_PAGE_TO_PHYS(&rv->pages[VM_LEVEL_0_NPAGES - 1]);
89744aab2c3SAlan Cox 		if (pa + PAGE_SIZE - size < low) {
898ec179322SAlan Cox 			/* This entire reservation is too low; go to next. */
89944aab2c3SAlan Cox 			continue;
90044aab2c3SAlan Cox 		}
901ec179322SAlan Cox 		pa = VM_PAGE_TO_PHYS(&rv->pages[0]);
90244aab2c3SAlan Cox 		if (pa + size > high) {
903ec179322SAlan Cox 			/* This entire reservation is too high; go to next. */
904ec179322SAlan Cox 			continue;
90585f2a0c9SMax Laier 		}
906ec179322SAlan Cox 		if (pa < low) {
907ec179322SAlan Cox 			/* Start the search for free pages at "low". */
908ec179322SAlan Cox 			i = (low - pa) / NBPOPMAP;
909ec179322SAlan Cox 			hi = (low - pa) % NBPOPMAP;
910ec179322SAlan Cox 		} else
911ec179322SAlan Cox 			i = hi = 0;
912ec179322SAlan Cox 		do {
913ec179322SAlan Cox 			/* Find the next free page. */
914ec179322SAlan Cox 			lo = ffsl(~(((1UL << hi) - 1) | rv->popmap[i]));
915ec179322SAlan Cox 			while (lo == 0 && ++i < NPOPMAP)
916ec179322SAlan Cox 				lo = ffsl(~rv->popmap[i]);
917ec179322SAlan Cox 			if (i == NPOPMAP)
918ec179322SAlan Cox 				break;
919ec179322SAlan Cox 			/* Convert from ffsl() to ordinary bit numbering. */
920ec179322SAlan Cox 			lo--;
921ec179322SAlan Cox 			next_free = NBPOPMAP * i + lo;
922ec179322SAlan Cox 			pa = VM_PAGE_TO_PHYS(&rv->pages[next_free]);
923ec179322SAlan Cox 			KASSERT(pa >= low,
924ec179322SAlan Cox 			    ("vm_reserv_reclaim_contig: pa is too low"));
925ec179322SAlan Cox 			if (pa + size > high) {
926ec179322SAlan Cox 				/* The rest of this reservation is too high. */
927ec179322SAlan Cox 				break;
928ec179322SAlan Cox 			} else if ((pa & (alignment - 1)) != 0 ||
929ec179322SAlan Cox 			    ((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0) {
930ec179322SAlan Cox 				/* Continue with this reservation. */
931ec179322SAlan Cox 				hi = lo;
932ec179322SAlan Cox 				continue;
933ec179322SAlan Cox 			}
934ec179322SAlan Cox 			/* Find the next used page. */
935ec179322SAlan Cox 			hi = ffsl(rv->popmap[i] & ~((1UL << lo) - 1));
936ec179322SAlan Cox 			while (hi == 0 && ++i < NPOPMAP) {
937ec179322SAlan Cox 				if ((NBPOPMAP * i - next_free) * PAGE_SIZE >=
938ec179322SAlan Cox 				    size) {
93944aab2c3SAlan Cox 					vm_reserv_reclaim(rv);
94044aab2c3SAlan Cox 					return (TRUE);
94144aab2c3SAlan Cox 				}
942ec179322SAlan Cox 				hi = ffsl(rv->popmap[i]);
943ec179322SAlan Cox 			}
944ec179322SAlan Cox 			/* Convert from ffsl() to ordinary bit numbering. */
945ec179322SAlan Cox 			if (i != NPOPMAP)
946ec179322SAlan Cox 				hi--;
947ec179322SAlan Cox 			if ((NBPOPMAP * i + hi - next_free) * PAGE_SIZE >=
948ec179322SAlan Cox 			    size) {
949ec179322SAlan Cox 				vm_reserv_reclaim(rv);
950ec179322SAlan Cox 				return (TRUE);
951ec179322SAlan Cox 			}
952ec179322SAlan Cox 		} while (i < NPOPMAP);
95344aab2c3SAlan Cox 	}
95444aab2c3SAlan Cox 	return (FALSE);
95544aab2c3SAlan Cox }
95644aab2c3SAlan Cox 
95744aab2c3SAlan Cox /*
958f8a47341SAlan Cox  * Transfers the reservation underlying the given page to a new object.
959f8a47341SAlan Cox  *
960f8a47341SAlan Cox  * The object must be locked.
961f8a47341SAlan Cox  */
962f8a47341SAlan Cox void
963f8a47341SAlan Cox vm_reserv_rename(vm_page_t m, vm_object_t new_object, vm_object_t old_object,
964f8a47341SAlan Cox     vm_pindex_t old_object_offset)
965f8a47341SAlan Cox {
966f8a47341SAlan Cox 	vm_reserv_t rv;
967f8a47341SAlan Cox 
96889f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(new_object);
969f8a47341SAlan Cox 	rv = vm_reserv_from_page(m);
970f8a47341SAlan Cox 	if (rv->object == old_object) {
971f8a47341SAlan Cox 		mtx_lock(&vm_page_queue_free_mtx);
972f8a47341SAlan Cox 		if (rv->object == old_object) {
973f8a47341SAlan Cox 			LIST_REMOVE(rv, objq);
974f8a47341SAlan Cox 			LIST_INSERT_HEAD(&new_object->rvq, rv, objq);
975f8a47341SAlan Cox 			rv->object = new_object;
976f8a47341SAlan Cox 			rv->pindex -= old_object_offset;
977f8a47341SAlan Cox 		}
978f8a47341SAlan Cox 		mtx_unlock(&vm_page_queue_free_mtx);
979f8a47341SAlan Cox 	}
980f8a47341SAlan Cox }
981f8a47341SAlan Cox 
982f8a47341SAlan Cox /*
983f8a47341SAlan Cox  * Allocates the virtual and physical memory required by the reservation
984f8a47341SAlan Cox  * management system's data structures, in particular, the reservation array.
985f8a47341SAlan Cox  */
986f8a47341SAlan Cox vm_paddr_t
987f8a47341SAlan Cox vm_reserv_startup(vm_offset_t *vaddr, vm_paddr_t end, vm_paddr_t high_water)
988f8a47341SAlan Cox {
989f8a47341SAlan Cox 	vm_paddr_t new_end;
990f8a47341SAlan Cox 	size_t size;
991f8a47341SAlan Cox 
992f8a47341SAlan Cox 	/*
993f8a47341SAlan Cox 	 * Calculate the size (in bytes) of the reservation array.  Round up
994f8a47341SAlan Cox 	 * from "high_water" because every small page is mapped to an element
995f8a47341SAlan Cox 	 * in the reservation array based on its physical address.  Thus, the
996f8a47341SAlan Cox 	 * number of elements in the reservation array can be greater than the
997f8a47341SAlan Cox 	 * number of superpages.
998f8a47341SAlan Cox 	 */
999f8a47341SAlan Cox 	size = howmany(high_water, VM_LEVEL_0_SIZE) * sizeof(struct vm_reserv);
1000f8a47341SAlan Cox 
1001f8a47341SAlan Cox 	/*
1002f8a47341SAlan Cox 	 * Allocate and map the physical memory for the reservation array.  The
1003f8a47341SAlan Cox 	 * next available virtual address is returned by reference.
1004f8a47341SAlan Cox 	 */
1005f8a47341SAlan Cox 	new_end = end - round_page(size);
1006f8a47341SAlan Cox 	vm_reserv_array = (void *)(uintptr_t)pmap_map(vaddr, new_end, end,
1007f8a47341SAlan Cox 	    VM_PROT_READ | VM_PROT_WRITE);
1008f8a47341SAlan Cox 	bzero(vm_reserv_array, size);
1009f8a47341SAlan Cox 
1010f8a47341SAlan Cox 	/*
1011f8a47341SAlan Cox 	 * Return the next available physical address.
1012f8a47341SAlan Cox 	 */
1013f8a47341SAlan Cox 	return (new_end);
1014f8a47341SAlan Cox }
1015f8a47341SAlan Cox 
1016f8a47341SAlan Cox #endif	/* VM_NRESERVLEVEL > 0 */
1017