xref: /freebsd/sys/vm/vm_reserv.c (revision ec17932242253af3c05eebfb04394248564a670d)
1f8a47341SAlan Cox /*-
2f8a47341SAlan Cox  * Copyright (c) 2002-2006 Rice University
3*ec179322SAlan 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 /*
96*ec179322SAlan Cox  * The size of a population map entry
97*ec179322SAlan Cox  */
98*ec179322SAlan Cox typedef	u_long		popmap_t;
99*ec179322SAlan Cox 
100*ec179322SAlan Cox /*
101*ec179322SAlan Cox  * The number of bits in a population map entry
102*ec179322SAlan Cox  */
103*ec179322SAlan Cox #define	NBPOPMAP	(NBBY * sizeof(popmap_t))
104*ec179322SAlan Cox 
105*ec179322SAlan Cox /*
106*ec179322SAlan Cox  * The number of population map entries in a reservation
107*ec179322SAlan Cox  */
108*ec179322SAlan Cox #define	NPOPMAP		howmany(VM_LEVEL_0_NPAGES, NBPOPMAP)
109*ec179322SAlan Cox 
110*ec179322SAlan 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;
132*ec179322SAlan 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 
189*ec179322SAlan Cox static void		vm_reserv_break(vm_reserv_t rv, vm_page_t m);
190*ec179322SAlan 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);
194*ec179322SAlan 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
232*ec179322SAlan 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
238*ec179322SAlan 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));
244f8a47341SAlan Cox 	KASSERT(rv->popcnt > 0,
245f8a47341SAlan Cox 	    ("vm_reserv_depopulate: reserv %p's popcnt is corrupted", rv));
246f8a47341SAlan Cox 	if (rv->inpartpopq) {
247f8a47341SAlan Cox 		TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq);
248f8a47341SAlan Cox 		rv->inpartpopq = FALSE;
249f8a47341SAlan Cox 	}
250*ec179322SAlan Cox 	clrbit(rv->popmap, index);
251f8a47341SAlan Cox 	rv->popcnt--;
252f8a47341SAlan Cox 	if (rv->popcnt == 0) {
253f8a47341SAlan Cox 		LIST_REMOVE(rv, objq);
254f8a47341SAlan Cox 		rv->object = NULL;
255f8a47341SAlan Cox 		vm_phys_free_pages(rv->pages, VM_LEVEL_0_ORDER);
256f8a47341SAlan Cox 		vm_reserv_freed++;
257f8a47341SAlan Cox 	} else {
258f8a47341SAlan Cox 		rv->inpartpopq = TRUE;
259ab5378cfSAlan Cox 		TAILQ_INSERT_TAIL(&vm_rvq_partpop, rv, partpopq);
260f8a47341SAlan Cox 	}
261f8a47341SAlan Cox }
262f8a47341SAlan Cox 
263f8a47341SAlan Cox /*
264f8a47341SAlan Cox  * Returns the reservation to which the given page might belong.
265f8a47341SAlan Cox  */
266f8a47341SAlan Cox static __inline vm_reserv_t
267f8a47341SAlan Cox vm_reserv_from_page(vm_page_t m)
268f8a47341SAlan Cox {
269f8a47341SAlan Cox 
270f8a47341SAlan Cox 	return (&vm_reserv_array[VM_PAGE_TO_PHYS(m) >> VM_LEVEL_0_SHIFT]);
271f8a47341SAlan Cox }
272f8a47341SAlan Cox 
273f8a47341SAlan Cox /*
274f8a47341SAlan Cox  * Returns TRUE if the given reservation contains the given page index and
275f8a47341SAlan Cox  * FALSE otherwise.
276f8a47341SAlan Cox  */
277f8a47341SAlan Cox static __inline boolean_t
278f8a47341SAlan Cox vm_reserv_has_pindex(vm_reserv_t rv, vm_pindex_t pindex)
279f8a47341SAlan Cox {
280f8a47341SAlan Cox 
281f8a47341SAlan Cox 	return (((pindex - rv->pindex) & ~(VM_LEVEL_0_NPAGES - 1)) == 0);
282f8a47341SAlan Cox }
283f8a47341SAlan Cox 
284f8a47341SAlan Cox /*
285f8a47341SAlan Cox  * Increases the given reservation's population count.  Moves the reservation
286f8a47341SAlan Cox  * to the tail of the partially-populated reservation queue.
287f8a47341SAlan Cox  *
288f8a47341SAlan Cox  * The free page queue must be locked.
289f8a47341SAlan Cox  */
290f8a47341SAlan Cox static void
291*ec179322SAlan Cox vm_reserv_populate(vm_reserv_t rv, int index)
292f8a47341SAlan Cox {
293f8a47341SAlan Cox 
294f8a47341SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
295f8a47341SAlan Cox 	KASSERT(rv->object != NULL,
296f8a47341SAlan Cox 	    ("vm_reserv_populate: reserv %p is free", rv));
297f8a47341SAlan Cox 	KASSERT(rv->popcnt < VM_LEVEL_0_NPAGES,
298f8a47341SAlan Cox 	    ("vm_reserv_populate: reserv %p is already full", rv));
299f8a47341SAlan Cox 	if (rv->inpartpopq) {
300f8a47341SAlan Cox 		TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq);
301f8a47341SAlan Cox 		rv->inpartpopq = FALSE;
302f8a47341SAlan Cox 	}
303*ec179322SAlan Cox 	setbit(rv->popmap, index);
304f8a47341SAlan Cox 	rv->popcnt++;
305f8a47341SAlan Cox 	if (rv->popcnt < VM_LEVEL_0_NPAGES) {
306f8a47341SAlan Cox 		rv->inpartpopq = TRUE;
307f8a47341SAlan Cox 		TAILQ_INSERT_TAIL(&vm_rvq_partpop, rv, partpopq);
308f8a47341SAlan Cox 	}
309f8a47341SAlan Cox }
310f8a47341SAlan Cox 
311f8a47341SAlan Cox /*
312c68c3537SAlan Cox  * Allocates a contiguous set of physical pages of the given size "npages"
313c68c3537SAlan Cox  * from an existing or newly-created reservation.  All of the physical pages
314c68c3537SAlan Cox  * must be at or above the given physical address "low" and below the given
315c68c3537SAlan Cox  * physical address "high".  The given value "alignment" determines the
316c68c3537SAlan Cox  * alignment of the first physical page in the set.  If the given value
317c68c3537SAlan Cox  * "boundary" is non-zero, then the set of physical pages cannot cross any
318c68c3537SAlan Cox  * physical address boundary that is a multiple of that value.  Both
319c68c3537SAlan Cox  * "alignment" and "boundary" must be a power of two.
320c68c3537SAlan Cox  *
321c68c3537SAlan Cox  * The object and free page queue must be locked.
322c68c3537SAlan Cox  */
323c68c3537SAlan Cox vm_page_t
324c68c3537SAlan Cox vm_reserv_alloc_contig(vm_object_t object, vm_pindex_t pindex, u_long npages,
325c68c3537SAlan Cox     vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
326c68c3537SAlan Cox {
327c68c3537SAlan Cox 	vm_paddr_t pa, size;
328c68c3537SAlan Cox 	vm_page_t m, m_ret, mpred, msucc;
329c68c3537SAlan Cox 	vm_pindex_t first, leftcap, rightcap;
330c68c3537SAlan Cox 	vm_reserv_t rv;
331c68c3537SAlan Cox 	u_long allocpages, maxpages, minpages;
332c68c3537SAlan Cox 	int i, index, n;
333c68c3537SAlan Cox 
334c68c3537SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
33589f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
336c68c3537SAlan Cox 	KASSERT(npages != 0, ("vm_reserv_alloc_contig: npages is 0"));
337c68c3537SAlan Cox 
338c68c3537SAlan Cox 	/*
339c68c3537SAlan Cox 	 * Is a reservation fundamentally impossible?
340c68c3537SAlan Cox 	 */
341c68c3537SAlan Cox 	if (pindex < VM_RESERV_INDEX(object, pindex) ||
342c68c3537SAlan Cox 	    pindex + npages > object->size)
343c68c3537SAlan Cox 		return (NULL);
344c68c3537SAlan Cox 
345c68c3537SAlan Cox 	/*
346c68c3537SAlan Cox 	 * All reservations of a particular size have the same alignment.
347c68c3537SAlan Cox 	 * Assuming that the first page is allocated from a reservation, the
348c68c3537SAlan Cox 	 * least significant bits of its physical address can be determined
349c68c3537SAlan Cox 	 * from its offset from the beginning of the reservation and the size
350c68c3537SAlan Cox 	 * of the reservation.
351c68c3537SAlan Cox 	 *
352c68c3537SAlan Cox 	 * Could the specified index within a reservation of the smallest
353c68c3537SAlan Cox 	 * possible size satisfy the alignment and boundary requirements?
354c68c3537SAlan Cox 	 */
355c68c3537SAlan Cox 	pa = VM_RESERV_INDEX(object, pindex) << PAGE_SHIFT;
356c68c3537SAlan Cox 	if ((pa & (alignment - 1)) != 0)
357c68c3537SAlan Cox 		return (NULL);
358c68c3537SAlan Cox 	size = npages << PAGE_SHIFT;
359c68c3537SAlan Cox 	if (((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0)
360c68c3537SAlan Cox 		return (NULL);
361c68c3537SAlan Cox 
362c68c3537SAlan Cox 	/*
363c68c3537SAlan Cox 	 * Look for an existing reservation.
364c68c3537SAlan Cox 	 */
365774d251dSAttilio Rao 	mpred = vm_radix_lookup_le(&object->rtree, pindex);
366774d251dSAttilio Rao 	if (mpred != NULL) {
367774d251dSAttilio Rao 		KASSERT(mpred->pindex < pindex,
368c68c3537SAlan Cox 		    ("vm_reserv_alloc_contig: pindex already allocated"));
369c68c3537SAlan Cox 		rv = vm_reserv_from_page(mpred);
370c68c3537SAlan Cox 		if (rv->object == object && vm_reserv_has_pindex(rv, pindex))
371c68c3537SAlan Cox 			goto found;
372774d251dSAttilio Rao 		msucc = TAILQ_NEXT(mpred, listq);
373774d251dSAttilio Rao 	} else
374774d251dSAttilio Rao 		msucc = TAILQ_FIRST(&object->memq);
375774d251dSAttilio Rao 	if (msucc != NULL) {
376774d251dSAttilio Rao 		KASSERT(msucc->pindex > pindex,
377774d251dSAttilio Rao 		    ("vm_reserv_alloc_page: pindex already allocated"));
378c68c3537SAlan Cox 		rv = vm_reserv_from_page(msucc);
379774d251dSAttilio Rao 		if (rv->object == object && vm_reserv_has_pindex(rv, pindex))
380c68c3537SAlan Cox 			goto found;
381c68c3537SAlan Cox 	}
382c68c3537SAlan Cox 
383c68c3537SAlan Cox 	/*
384c68c3537SAlan Cox 	 * Could at least one reservation fit between the first index to the
385c68c3537SAlan Cox 	 * left that can be used and the first index to the right that cannot
386c68c3537SAlan Cox 	 * be used?
387c68c3537SAlan Cox 	 */
388c68c3537SAlan Cox 	first = pindex - VM_RESERV_INDEX(object, pindex);
389c68c3537SAlan Cox 	if (mpred != NULL) {
390c68c3537SAlan Cox 		if ((rv = vm_reserv_from_page(mpred))->object != object)
391c68c3537SAlan Cox 			leftcap = mpred->pindex + 1;
392c68c3537SAlan Cox 		else
393c68c3537SAlan Cox 			leftcap = rv->pindex + VM_LEVEL_0_NPAGES;
394c68c3537SAlan Cox 		if (leftcap > first)
395c68c3537SAlan Cox 			return (NULL);
396c68c3537SAlan Cox 	}
397c68c3537SAlan Cox 	minpages = VM_RESERV_INDEX(object, pindex) + npages;
398c68c3537SAlan Cox 	maxpages = roundup2(minpages, VM_LEVEL_0_NPAGES);
399c68c3537SAlan Cox 	allocpages = maxpages;
400c68c3537SAlan Cox 	if (msucc != NULL) {
401c68c3537SAlan Cox 		if ((rv = vm_reserv_from_page(msucc))->object != object)
402c68c3537SAlan Cox 			rightcap = msucc->pindex;
403c68c3537SAlan Cox 		else
404c68c3537SAlan Cox 			rightcap = rv->pindex;
405c68c3537SAlan Cox 		if (first + maxpages > rightcap) {
406c68c3537SAlan Cox 			if (maxpages == VM_LEVEL_0_NPAGES)
407c68c3537SAlan Cox 				return (NULL);
408c68c3537SAlan Cox 			allocpages = minpages;
409c68c3537SAlan Cox 		}
410c68c3537SAlan Cox 	}
411c68c3537SAlan Cox 
412c68c3537SAlan Cox 	/*
413c68c3537SAlan Cox 	 * Would the last new reservation extend past the end of the object?
414c68c3537SAlan Cox 	 */
415c68c3537SAlan Cox 	if (first + maxpages > object->size) {
416c68c3537SAlan Cox 		/*
417c68c3537SAlan Cox 		 * Don't allocate the last new reservation if the object is a
418c68c3537SAlan Cox 		 * vnode or backed by another object that is a vnode.
419c68c3537SAlan Cox 		 */
420c68c3537SAlan Cox 		if (object->type == OBJT_VNODE ||
421c68c3537SAlan Cox 		    (object->backing_object != NULL &&
422c68c3537SAlan Cox 		    object->backing_object->type == OBJT_VNODE)) {
423c68c3537SAlan Cox 			if (maxpages == VM_LEVEL_0_NPAGES)
424c68c3537SAlan Cox 				return (NULL);
425c68c3537SAlan Cox 			allocpages = minpages;
426c68c3537SAlan Cox 		}
427c68c3537SAlan Cox 		/* Speculate that the object may grow. */
428c68c3537SAlan Cox 	}
429c68c3537SAlan Cox 
430c68c3537SAlan Cox 	/*
431c68c3537SAlan Cox 	 * Allocate and populate the new reservations.  The alignment and
432c68c3537SAlan Cox 	 * boundary specified for this allocation may be different from the
433c68c3537SAlan Cox 	 * alignment and boundary specified for the requested pages.  For
434c68c3537SAlan Cox 	 * instance, the specified index may not be the first page within the
435c68c3537SAlan Cox 	 * first new reservation.
436c68c3537SAlan Cox 	 */
437c68c3537SAlan Cox 	m = vm_phys_alloc_contig(allocpages, low, high, ulmax(alignment,
438c68c3537SAlan Cox 	    VM_LEVEL_0_SIZE), boundary > VM_LEVEL_0_SIZE ? boundary : 0);
439c68c3537SAlan Cox 	if (m == NULL)
440c68c3537SAlan Cox 		return (NULL);
441c68c3537SAlan Cox 	m_ret = NULL;
442c68c3537SAlan Cox 	index = VM_RESERV_INDEX(object, pindex);
443c68c3537SAlan Cox 	do {
444c68c3537SAlan Cox 		rv = vm_reserv_from_page(m);
445c68c3537SAlan Cox 		KASSERT(rv->pages == m,
446c68c3537SAlan Cox 		    ("vm_reserv_alloc_contig: reserv %p's pages is corrupted",
447c68c3537SAlan Cox 		    rv));
448c68c3537SAlan Cox 		KASSERT(rv->object == NULL,
449c68c3537SAlan Cox 		    ("vm_reserv_alloc_contig: reserv %p isn't free", rv));
450c68c3537SAlan Cox 		LIST_INSERT_HEAD(&object->rvq, rv, objq);
451c68c3537SAlan Cox 		rv->object = object;
452c68c3537SAlan Cox 		rv->pindex = first;
453c68c3537SAlan Cox 		KASSERT(rv->popcnt == 0,
454c68c3537SAlan Cox 		    ("vm_reserv_alloc_contig: reserv %p's popcnt is corrupted",
455c68c3537SAlan Cox 		    rv));
456c68c3537SAlan Cox 		KASSERT(!rv->inpartpopq,
457c68c3537SAlan Cox 		    ("vm_reserv_alloc_contig: reserv %p's inpartpopq is TRUE",
458c68c3537SAlan Cox 		    rv));
459*ec179322SAlan Cox 		for (i = 0; i < NPOPMAP; i++)
460*ec179322SAlan Cox 			KASSERT(rv->popmap[i] == 0,
461*ec179322SAlan Cox 		    ("vm_reserv_alloc_contig: reserv %p's popmap is corrupted",
462*ec179322SAlan Cox 			    rv));
463c68c3537SAlan Cox 		n = ulmin(VM_LEVEL_0_NPAGES - index, npages);
464c68c3537SAlan Cox 		for (i = 0; i < n; i++)
465*ec179322SAlan Cox 			vm_reserv_populate(rv, index + i);
466c68c3537SAlan Cox 		npages -= n;
467c68c3537SAlan Cox 		if (m_ret == NULL) {
468c68c3537SAlan Cox 			m_ret = &rv->pages[index];
469c68c3537SAlan Cox 			index = 0;
470c68c3537SAlan Cox 		}
471c68c3537SAlan Cox 		m += VM_LEVEL_0_NPAGES;
472c68c3537SAlan Cox 		first += VM_LEVEL_0_NPAGES;
473c68c3537SAlan Cox 		allocpages -= VM_LEVEL_0_NPAGES;
474476f7f24SAlan Cox 	} while (allocpages > 0);
475c68c3537SAlan Cox 	return (m_ret);
476c68c3537SAlan Cox 
477c68c3537SAlan Cox 	/*
478c68c3537SAlan Cox 	 * Found a matching reservation.
479c68c3537SAlan Cox 	 */
480c68c3537SAlan Cox found:
481c68c3537SAlan Cox 	index = VM_RESERV_INDEX(object, pindex);
482c68c3537SAlan Cox 	/* Does the allocation fit within the reservation? */
483c68c3537SAlan Cox 	if (index + npages > VM_LEVEL_0_NPAGES)
484c68c3537SAlan Cox 		return (NULL);
485c68c3537SAlan Cox 	m = &rv->pages[index];
486c68c3537SAlan Cox 	pa = VM_PAGE_TO_PHYS(m);
487c68c3537SAlan Cox 	if (pa < low || pa + size > high || (pa & (alignment - 1)) != 0 ||
488c68c3537SAlan Cox 	    ((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0)
489c68c3537SAlan Cox 		return (NULL);
490c68c3537SAlan Cox 	/* Handle vm_page_rename(m, new_object, ...). */
491c68c3537SAlan Cox 	for (i = 0; i < npages; i++)
492*ec179322SAlan Cox 		if (isset(rv->popmap, index + i))
493c68c3537SAlan Cox 			return (NULL);
494c68c3537SAlan Cox 	for (i = 0; i < npages; i++)
495*ec179322SAlan Cox 		vm_reserv_populate(rv, index + i);
496c68c3537SAlan Cox 	return (m);
497c68c3537SAlan Cox }
498c68c3537SAlan Cox 
499c68c3537SAlan Cox /*
500f8a47341SAlan Cox  * Allocates a page from an existing or newly-created reservation.
501f8a47341SAlan Cox  *
502404eb1b3SAlan Cox  * The page "mpred" must immediately precede the offset "pindex" within the
503404eb1b3SAlan Cox  * specified object.
504404eb1b3SAlan Cox  *
505f8a47341SAlan Cox  * The object and free page queue must be locked.
506f8a47341SAlan Cox  */
507f8a47341SAlan Cox vm_page_t
508404eb1b3SAlan Cox vm_reserv_alloc_page(vm_object_t object, vm_pindex_t pindex, vm_page_t mpred)
509f8a47341SAlan Cox {
510404eb1b3SAlan Cox 	vm_page_t m, msucc;
511f8a47341SAlan Cox 	vm_pindex_t first, leftcap, rightcap;
512f8a47341SAlan Cox 	vm_reserv_t rv;
513*ec179322SAlan Cox 	int i, index;
514f8a47341SAlan Cox 
515f8a47341SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
51689f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
517f8a47341SAlan Cox 
518f8a47341SAlan Cox 	/*
519c68c3537SAlan Cox 	 * Is a reservation fundamentally impossible?
520f8a47341SAlan Cox 	 */
521f8a47341SAlan Cox 	if (pindex < VM_RESERV_INDEX(object, pindex) ||
522f8a47341SAlan Cox 	    pindex >= object->size)
523f8a47341SAlan Cox 		return (NULL);
524f8a47341SAlan Cox 
525f8a47341SAlan Cox 	/*
526f8a47341SAlan Cox 	 * Look for an existing reservation.
527f8a47341SAlan Cox 	 */
528774d251dSAttilio Rao 	if (mpred != NULL) {
5299eab5484SKonstantin Belousov 		KASSERT(mpred->object == object,
530404eb1b3SAlan Cox 		    ("vm_reserv_alloc_page: object doesn't contain mpred"));
531774d251dSAttilio Rao 		KASSERT(mpred->pindex < pindex,
532404eb1b3SAlan Cox 		    ("vm_reserv_alloc_page: mpred doesn't precede pindex"));
533f8a47341SAlan Cox 		rv = vm_reserv_from_page(mpred);
534c68c3537SAlan Cox 		if (rv->object == object && vm_reserv_has_pindex(rv, pindex))
535c68c3537SAlan Cox 			goto found;
536774d251dSAttilio Rao 		msucc = TAILQ_NEXT(mpred, listq);
537774d251dSAttilio Rao 	} else
538774d251dSAttilio Rao 		msucc = TAILQ_FIRST(&object->memq);
539774d251dSAttilio Rao 	if (msucc != NULL) {
540774d251dSAttilio Rao 		KASSERT(msucc->pindex > pindex,
541404eb1b3SAlan Cox 		    ("vm_reserv_alloc_page: msucc doesn't succeed pindex"));
542f8a47341SAlan Cox 		rv = vm_reserv_from_page(msucc);
543774d251dSAttilio Rao 		if (rv->object == object && vm_reserv_has_pindex(rv, pindex))
544c68c3537SAlan Cox 			goto found;
545f8a47341SAlan Cox 	}
546f8a47341SAlan Cox 
547f8a47341SAlan Cox 	/*
548c68c3537SAlan Cox 	 * Could a reservation fit between the first index to the left that
549c68c3537SAlan Cox 	 * can be used and the first index to the right that cannot be used?
550f8a47341SAlan Cox 	 */
551c68c3537SAlan Cox 	first = pindex - VM_RESERV_INDEX(object, pindex);
552c68c3537SAlan Cox 	if (mpred != NULL) {
553c68c3537SAlan Cox 		if ((rv = vm_reserv_from_page(mpred))->object != object)
554f8a47341SAlan Cox 			leftcap = mpred->pindex + 1;
555f8a47341SAlan Cox 		else
556f8a47341SAlan Cox 			leftcap = rv->pindex + VM_LEVEL_0_NPAGES;
557c68c3537SAlan Cox 		if (leftcap > first)
558c68c3537SAlan Cox 			return (NULL);
559c68c3537SAlan Cox 	}
560c68c3537SAlan Cox 	if (msucc != NULL) {
561c68c3537SAlan Cox 		if ((rv = vm_reserv_from_page(msucc))->object != object)
562f8a47341SAlan Cox 			rightcap = msucc->pindex;
563f8a47341SAlan Cox 		else
564f8a47341SAlan Cox 			rightcap = rv->pindex;
565c68c3537SAlan Cox 		if (first + VM_LEVEL_0_NPAGES > rightcap)
566f8a47341SAlan Cox 			return (NULL);
567c68c3537SAlan Cox 	}
568f8a47341SAlan Cox 
569f8a47341SAlan Cox 	/*
570c68c3537SAlan Cox 	 * Would a new reservation extend past the end of the object?
571f8a47341SAlan Cox 	 */
572c68c3537SAlan Cox 	if (first + VM_LEVEL_0_NPAGES > object->size) {
573f8a47341SAlan Cox 		/*
574f8a47341SAlan Cox 		 * Don't allocate a new reservation if the object is a vnode or
575f8a47341SAlan Cox 		 * backed by another object that is a vnode.
576f8a47341SAlan Cox 		 */
577f8a47341SAlan Cox 		if (object->type == OBJT_VNODE ||
578f8a47341SAlan Cox 		    (object->backing_object != NULL &&
579f8a47341SAlan Cox 		    object->backing_object->type == OBJT_VNODE))
580f8a47341SAlan Cox 			return (NULL);
581f8a47341SAlan Cox 		/* Speculate that the object may grow. */
582f8a47341SAlan Cox 	}
583f8a47341SAlan Cox 
584f8a47341SAlan Cox 	/*
585c68c3537SAlan Cox 	 * Allocate and populate the new reservation.
586f8a47341SAlan Cox 	 */
587f8a47341SAlan Cox 	m = vm_phys_alloc_pages(VM_FREEPOOL_DEFAULT, VM_LEVEL_0_ORDER);
588c68c3537SAlan Cox 	if (m == NULL)
589c68c3537SAlan Cox 		return (NULL);
590f8a47341SAlan Cox 	rv = vm_reserv_from_page(m);
591f8a47341SAlan Cox 	KASSERT(rv->pages == m,
592c68c3537SAlan Cox 	    ("vm_reserv_alloc_page: reserv %p's pages is corrupted", rv));
593f8a47341SAlan Cox 	KASSERT(rv->object == NULL,
594f8a47341SAlan Cox 	    ("vm_reserv_alloc_page: reserv %p isn't free", rv));
595f8a47341SAlan Cox 	LIST_INSERT_HEAD(&object->rvq, rv, objq);
596f8a47341SAlan Cox 	rv->object = object;
597f8a47341SAlan Cox 	rv->pindex = first;
598f8a47341SAlan Cox 	KASSERT(rv->popcnt == 0,
599c68c3537SAlan Cox 	    ("vm_reserv_alloc_page: reserv %p's popcnt is corrupted", rv));
600f8a47341SAlan Cox 	KASSERT(!rv->inpartpopq,
601c68c3537SAlan Cox 	    ("vm_reserv_alloc_page: reserv %p's inpartpopq is TRUE", rv));
602*ec179322SAlan Cox 	for (i = 0; i < NPOPMAP; i++)
603*ec179322SAlan Cox 		KASSERT(rv->popmap[i] == 0,
604*ec179322SAlan Cox 		    ("vm_reserv_alloc_page: reserv %p's popmap is corrupted",
605*ec179322SAlan Cox 		    rv));
606*ec179322SAlan Cox 	index = VM_RESERV_INDEX(object, pindex);
607*ec179322SAlan Cox 	vm_reserv_populate(rv, index);
608*ec179322SAlan Cox 	return (&rv->pages[index]);
609c68c3537SAlan Cox 
610c68c3537SAlan Cox 	/*
611c68c3537SAlan Cox 	 * Found a matching reservation.
612c68c3537SAlan Cox 	 */
613c68c3537SAlan Cox found:
614*ec179322SAlan Cox 	index = VM_RESERV_INDEX(object, pindex);
615*ec179322SAlan Cox 	m = &rv->pages[index];
616c68c3537SAlan Cox 	/* Handle vm_page_rename(m, new_object, ...). */
617*ec179322SAlan Cox 	if (isset(rv->popmap, index))
618c68c3537SAlan Cox 		return (NULL);
619*ec179322SAlan Cox 	vm_reserv_populate(rv, index);
620f8a47341SAlan Cox 	return (m);
621f8a47341SAlan Cox }
622f8a47341SAlan Cox 
623f8a47341SAlan Cox /*
624*ec179322SAlan Cox  * Breaks the given reservation.  Except for the specified cached or free
625*ec179322SAlan Cox  * page, all cached and free pages in the reservation are returned to the
626*ec179322SAlan Cox  * physical memory allocator.  The reservation's population count and map are
627*ec179322SAlan Cox  * reset to their initial state.
628*ec179322SAlan Cox  *
629*ec179322SAlan Cox  * The given reservation must not be in the partially-populated reservation
630*ec179322SAlan Cox  * queue.  The free page queue lock must be held.
631*ec179322SAlan Cox  */
632*ec179322SAlan Cox static void
633*ec179322SAlan Cox vm_reserv_break(vm_reserv_t rv, vm_page_t m)
634*ec179322SAlan Cox {
635*ec179322SAlan Cox 	int begin_zeroes, hi, i, lo;
636*ec179322SAlan Cox 
637*ec179322SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
638*ec179322SAlan Cox 	KASSERT(rv->object != NULL,
639*ec179322SAlan Cox 	    ("vm_reserv_break: reserv %p is free", rv));
640*ec179322SAlan Cox 	KASSERT(!rv->inpartpopq,
641*ec179322SAlan Cox 	    ("vm_reserv_break: reserv %p's inpartpopq is TRUE", rv));
642*ec179322SAlan Cox 	LIST_REMOVE(rv, objq);
643*ec179322SAlan Cox 	rv->object = NULL;
644*ec179322SAlan Cox 	if (m != NULL) {
645*ec179322SAlan Cox 		/*
646*ec179322SAlan Cox 		 * Since the reservation is being broken, there is no harm in
647*ec179322SAlan Cox 		 * abusing the population map to stop "m" from being returned
648*ec179322SAlan Cox 		 * to the physical memory allocator.
649*ec179322SAlan Cox 		 */
650*ec179322SAlan Cox 		i = m - rv->pages;
651*ec179322SAlan Cox 		KASSERT(isclr(rv->popmap, i),
652*ec179322SAlan Cox 		    ("vm_reserv_break: reserv %p's popmap is corrupted", rv));
653*ec179322SAlan Cox 		setbit(rv->popmap, i);
654*ec179322SAlan Cox 		rv->popcnt++;
655*ec179322SAlan Cox 	}
656*ec179322SAlan Cox 	i = hi = 0;
657*ec179322SAlan Cox 	do {
658*ec179322SAlan Cox 		/* Find the next 0 bit.  Any previous 0 bits are < "hi". */
659*ec179322SAlan Cox 		lo = ffsl(~(((1UL << hi) - 1) | rv->popmap[i]));
660*ec179322SAlan Cox 		if (lo == 0) {
661*ec179322SAlan Cox 			/* Redundantly clears bits < "hi". */
662*ec179322SAlan Cox 			rv->popmap[i] = 0;
663*ec179322SAlan Cox 			rv->popcnt -= NBPOPMAP - hi;
664*ec179322SAlan Cox 			while (++i < NPOPMAP) {
665*ec179322SAlan Cox 				lo = ffsl(~rv->popmap[i]);
666*ec179322SAlan Cox 				if (lo == 0) {
667*ec179322SAlan Cox 					rv->popmap[i] = 0;
668*ec179322SAlan Cox 					rv->popcnt -= NBPOPMAP;
669*ec179322SAlan Cox 				} else
670*ec179322SAlan Cox 					break;
671*ec179322SAlan Cox 			}
672*ec179322SAlan Cox 			if (i == NPOPMAP)
673*ec179322SAlan Cox 				break;
674*ec179322SAlan Cox 			hi = 0;
675*ec179322SAlan Cox 		}
676*ec179322SAlan Cox 		KASSERT(lo > 0, ("vm_reserv_break: lo is %d", lo));
677*ec179322SAlan Cox 		/* Convert from ffsl() to ordinary bit numbering. */
678*ec179322SAlan Cox 		lo--;
679*ec179322SAlan Cox 		if (lo > 0) {
680*ec179322SAlan Cox 			/* Redundantly clears bits < "hi". */
681*ec179322SAlan Cox 			rv->popmap[i] &= ~((1UL << lo) - 1);
682*ec179322SAlan Cox 			rv->popcnt -= lo - hi;
683*ec179322SAlan Cox 		}
684*ec179322SAlan Cox 		begin_zeroes = NBPOPMAP * i + lo;
685*ec179322SAlan Cox 		/* Find the next 1 bit. */
686*ec179322SAlan Cox 		do
687*ec179322SAlan Cox 			hi = ffsl(rv->popmap[i]);
688*ec179322SAlan Cox 		while (hi == 0 && ++i < NPOPMAP);
689*ec179322SAlan Cox 		if (i != NPOPMAP)
690*ec179322SAlan Cox 			/* Convert from ffsl() to ordinary bit numbering. */
691*ec179322SAlan Cox 			hi--;
692*ec179322SAlan Cox 		vm_phys_free_contig(&rv->pages[begin_zeroes], NBPOPMAP * i +
693*ec179322SAlan Cox 		    hi - begin_zeroes);
694*ec179322SAlan Cox 	} while (i < NPOPMAP);
695*ec179322SAlan Cox 	KASSERT(rv->popcnt == 0,
696*ec179322SAlan Cox 	    ("vm_reserv_break: reserv %p's popcnt is corrupted", rv));
697*ec179322SAlan Cox 	vm_reserv_broken++;
698*ec179322SAlan Cox }
699*ec179322SAlan Cox 
700*ec179322SAlan Cox /*
701f8a47341SAlan Cox  * Breaks all reservations belonging to the given object.
702f8a47341SAlan Cox  */
703f8a47341SAlan Cox void
704f8a47341SAlan Cox vm_reserv_break_all(vm_object_t object)
705f8a47341SAlan Cox {
706f8a47341SAlan Cox 	vm_reserv_t rv;
707f8a47341SAlan Cox 
708f8a47341SAlan Cox 	mtx_lock(&vm_page_queue_free_mtx);
709f8a47341SAlan Cox 	while ((rv = LIST_FIRST(&object->rvq)) != NULL) {
710f8a47341SAlan Cox 		KASSERT(rv->object == object,
711f8a47341SAlan Cox 		    ("vm_reserv_break_all: reserv %p is corrupted", rv));
712f8a47341SAlan Cox 		if (rv->inpartpopq) {
713f8a47341SAlan Cox 			TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq);
714f8a47341SAlan Cox 			rv->inpartpopq = FALSE;
715f8a47341SAlan Cox 		}
716*ec179322SAlan Cox 		vm_reserv_break(rv, NULL);
717f8a47341SAlan Cox 	}
718f8a47341SAlan Cox 	mtx_unlock(&vm_page_queue_free_mtx);
719f8a47341SAlan Cox }
720f8a47341SAlan Cox 
721f8a47341SAlan Cox /*
722f8a47341SAlan Cox  * Frees the given page if it belongs to a reservation.  Returns TRUE if the
723f8a47341SAlan Cox  * page is freed and FALSE otherwise.
724f8a47341SAlan Cox  *
725f8a47341SAlan Cox  * The free page queue lock must be held.
726f8a47341SAlan Cox  */
727f8a47341SAlan Cox boolean_t
728f8a47341SAlan Cox vm_reserv_free_page(vm_page_t m)
729f8a47341SAlan Cox {
730f8a47341SAlan Cox 	vm_reserv_t rv;
731f8a47341SAlan Cox 
732f8a47341SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
733f8a47341SAlan Cox 	rv = vm_reserv_from_page(m);
734908e3da1SAlan Cox 	if (rv->object == NULL)
735908e3da1SAlan Cox 		return (FALSE);
736908e3da1SAlan Cox 	if ((m->flags & PG_CACHED) != 0 && m->pool != VM_FREEPOOL_CACHE)
737908e3da1SAlan Cox 		vm_phys_set_pool(VM_FREEPOOL_CACHE, rv->pages,
738908e3da1SAlan Cox 		    VM_LEVEL_0_ORDER);
739*ec179322SAlan Cox 	vm_reserv_depopulate(rv, m - rv->pages);
740f8a47341SAlan Cox 	return (TRUE);
741f8a47341SAlan Cox }
742f8a47341SAlan Cox 
743f8a47341SAlan Cox /*
744f8a47341SAlan Cox  * Initializes the reservation management system.  Specifically, initializes
745f8a47341SAlan Cox  * the reservation array.
746f8a47341SAlan Cox  *
747f8a47341SAlan Cox  * Requires that vm_page_array and first_page are initialized!
748f8a47341SAlan Cox  */
749f8a47341SAlan Cox void
750f8a47341SAlan Cox vm_reserv_init(void)
751f8a47341SAlan Cox {
752f8a47341SAlan Cox 	vm_paddr_t paddr;
753f8a47341SAlan Cox 	int i;
754f8a47341SAlan Cox 
755f8a47341SAlan Cox 	/*
756f8a47341SAlan Cox 	 * Initialize the reservation array.  Specifically, initialize the
757f8a47341SAlan Cox 	 * "pages" field for every element that has an underlying superpage.
758f8a47341SAlan Cox 	 */
759f8a47341SAlan Cox 	for (i = 0; phys_avail[i + 1] != 0; i += 2) {
760f8a47341SAlan Cox 		paddr = roundup2(phys_avail[i], VM_LEVEL_0_SIZE);
761f8a47341SAlan Cox 		while (paddr + VM_LEVEL_0_SIZE <= phys_avail[i + 1]) {
762f8a47341SAlan Cox 			vm_reserv_array[paddr >> VM_LEVEL_0_SHIFT].pages =
763f8a47341SAlan Cox 			    PHYS_TO_VM_PAGE(paddr);
764f8a47341SAlan Cox 			paddr += VM_LEVEL_0_SIZE;
765f8a47341SAlan Cox 		}
766f8a47341SAlan Cox 	}
767f8a47341SAlan Cox }
768f8a47341SAlan Cox 
769f8a47341SAlan Cox /*
770f8a47341SAlan Cox  * Returns a reservation level if the given page belongs to a fully-populated
771f8a47341SAlan Cox  * reservation and -1 otherwise.
772f8a47341SAlan Cox  */
773f8a47341SAlan Cox int
774f8a47341SAlan Cox vm_reserv_level_iffullpop(vm_page_t m)
775f8a47341SAlan Cox {
776f8a47341SAlan Cox 	vm_reserv_t rv;
777f8a47341SAlan Cox 
778f8a47341SAlan Cox 	rv = vm_reserv_from_page(m);
779f8a47341SAlan Cox 	return (rv->popcnt == VM_LEVEL_0_NPAGES ? 0 : -1);
780f8a47341SAlan Cox }
781f8a47341SAlan Cox 
782f8a47341SAlan Cox /*
783f8a47341SAlan Cox  * Prepare for the reactivation of a cached page.
784f8a47341SAlan Cox  *
785f8a47341SAlan Cox  * First, suppose that the given page "m" was allocated individually, i.e., not
786f8a47341SAlan Cox  * as part of a reservation, and cached.  Then, suppose a reservation
787f8a47341SAlan Cox  * containing "m" is allocated by the same object.  Although "m" and the
788f8a47341SAlan Cox  * reservation belong to the same object, "m"'s pindex may not match the
789f8a47341SAlan Cox  * reservation's.
790f8a47341SAlan Cox  *
791f8a47341SAlan Cox  * The free page queue must be locked.
792f8a47341SAlan Cox  */
793f8a47341SAlan Cox boolean_t
794f8a47341SAlan Cox vm_reserv_reactivate_page(vm_page_t m)
795f8a47341SAlan Cox {
796f8a47341SAlan Cox 	vm_reserv_t rv;
797*ec179322SAlan Cox 	int index;
798f8a47341SAlan Cox 
799f8a47341SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
800f8a47341SAlan Cox 	rv = vm_reserv_from_page(m);
801f8a47341SAlan Cox 	if (rv->object == NULL)
802f8a47341SAlan Cox 		return (FALSE);
803f8a47341SAlan Cox 	KASSERT((m->flags & PG_CACHED) != 0,
804*ec179322SAlan Cox 	    ("vm_reserv_reactivate_page: page %p is not cached", m));
805f8a47341SAlan Cox 	if (m->object == rv->object &&
806*ec179322SAlan Cox 	    m->pindex - rv->pindex == (index = VM_RESERV_INDEX(m->object,
807*ec179322SAlan Cox 	    m->pindex)))
808*ec179322SAlan Cox 		vm_reserv_populate(rv, index);
809f8a47341SAlan Cox 	else {
810f8a47341SAlan Cox 		KASSERT(rv->inpartpopq,
811*ec179322SAlan Cox 	    ("vm_reserv_reactivate_page: reserv %p's inpartpopq is FALSE",
812f8a47341SAlan Cox 		    rv));
813f8a47341SAlan Cox 		TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq);
814f8a47341SAlan Cox 		rv->inpartpopq = FALSE;
815*ec179322SAlan Cox 		/* Don't release "m" to the physical memory allocator. */
816*ec179322SAlan Cox 		vm_reserv_break(rv, m);
817f8a47341SAlan Cox 	}
818f8a47341SAlan Cox 	return (TRUE);
819f8a47341SAlan Cox }
820f8a47341SAlan Cox 
821f8a47341SAlan Cox /*
82244aab2c3SAlan Cox  * Breaks the given partially-populated reservation, releasing its cached and
82344aab2c3SAlan Cox  * free pages to the physical memory allocator.
824f8a47341SAlan Cox  *
825f8a47341SAlan Cox  * The free page queue lock must be held.
826f8a47341SAlan Cox  */
82744aab2c3SAlan Cox static void
82844aab2c3SAlan Cox vm_reserv_reclaim(vm_reserv_t rv)
829f8a47341SAlan Cox {
830f8a47341SAlan Cox 
831f8a47341SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
832f8a47341SAlan Cox 	KASSERT(rv->inpartpopq,
833*ec179322SAlan Cox 	    ("vm_reserv_reclaim: reserv %p's inpartpopq is FALSE", rv));
834f8a47341SAlan Cox 	TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq);
835f8a47341SAlan Cox 	rv->inpartpopq = FALSE;
836*ec179322SAlan Cox 	vm_reserv_break(rv, NULL);
837f8a47341SAlan Cox 	vm_reserv_reclaimed++;
83844aab2c3SAlan Cox }
83944aab2c3SAlan Cox 
84044aab2c3SAlan Cox /*
84144aab2c3SAlan Cox  * Breaks the reservation at the head of the partially-populated reservation
84244aab2c3SAlan Cox  * queue, releasing its cached and free pages to the physical memory
84344aab2c3SAlan Cox  * allocator.  Returns TRUE if a reservation is broken and FALSE otherwise.
84444aab2c3SAlan Cox  *
84544aab2c3SAlan Cox  * The free page queue lock must be held.
84644aab2c3SAlan Cox  */
84744aab2c3SAlan Cox boolean_t
84844aab2c3SAlan Cox vm_reserv_reclaim_inactive(void)
84944aab2c3SAlan Cox {
85044aab2c3SAlan Cox 	vm_reserv_t rv;
85144aab2c3SAlan Cox 
85244aab2c3SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
85344aab2c3SAlan Cox 	if ((rv = TAILQ_FIRST(&vm_rvq_partpop)) != NULL) {
85444aab2c3SAlan Cox 		vm_reserv_reclaim(rv);
855f8a47341SAlan Cox 		return (TRUE);
856f8a47341SAlan Cox 	}
857f8a47341SAlan Cox 	return (FALSE);
858f8a47341SAlan Cox }
859f8a47341SAlan Cox 
860f8a47341SAlan Cox /*
86144aab2c3SAlan Cox  * Searches the partially-populated reservation queue for the least recently
86244aab2c3SAlan Cox  * active reservation with unused pages, i.e., cached or free, that satisfy the
86344aab2c3SAlan Cox  * given request for contiguous physical memory.  If a satisfactory reservation
86444aab2c3SAlan Cox  * is found, it is broken.  Returns TRUE if a reservation is broken and FALSE
86544aab2c3SAlan Cox  * otherwise.
86644aab2c3SAlan Cox  *
86744aab2c3SAlan Cox  * The free page queue lock must be held.
86844aab2c3SAlan Cox  */
86944aab2c3SAlan Cox boolean_t
870c68c3537SAlan Cox vm_reserv_reclaim_contig(u_long npages, vm_paddr_t low, vm_paddr_t high,
8715c1f2cc4SAlan Cox     u_long alignment, vm_paddr_t boundary)
87244aab2c3SAlan Cox {
873*ec179322SAlan Cox 	vm_paddr_t pa, size;
87444aab2c3SAlan Cox 	vm_reserv_t rv;
875*ec179322SAlan Cox 	int hi, i, lo, next_free;
87644aab2c3SAlan Cox 
87744aab2c3SAlan Cox 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
878c68c3537SAlan Cox 	if (npages > VM_LEVEL_0_NPAGES - 1)
87944aab2c3SAlan Cox 		return (FALSE);
880c68c3537SAlan Cox 	size = npages << PAGE_SHIFT;
88144aab2c3SAlan Cox 	TAILQ_FOREACH(rv, &vm_rvq_partpop, partpopq) {
88244aab2c3SAlan Cox 		pa = VM_PAGE_TO_PHYS(&rv->pages[VM_LEVEL_0_NPAGES - 1]);
88344aab2c3SAlan Cox 		if (pa + PAGE_SIZE - size < low) {
884*ec179322SAlan Cox 			/* This entire reservation is too low; go to next. */
88544aab2c3SAlan Cox 			continue;
88644aab2c3SAlan Cox 		}
887*ec179322SAlan Cox 		pa = VM_PAGE_TO_PHYS(&rv->pages[0]);
88844aab2c3SAlan Cox 		if (pa + size > high) {
889*ec179322SAlan Cox 			/* This entire reservation is too high; go to next. */
890*ec179322SAlan Cox 			continue;
89185f2a0c9SMax Laier 		}
892*ec179322SAlan Cox 		if (pa < low) {
893*ec179322SAlan Cox 			/* Start the search for free pages at "low". */
894*ec179322SAlan Cox 			i = (low - pa) / NBPOPMAP;
895*ec179322SAlan Cox 			hi = (low - pa) % NBPOPMAP;
896*ec179322SAlan Cox 		} else
897*ec179322SAlan Cox 			i = hi = 0;
898*ec179322SAlan Cox 		do {
899*ec179322SAlan Cox 			/* Find the next free page. */
900*ec179322SAlan Cox 			lo = ffsl(~(((1UL << hi) - 1) | rv->popmap[i]));
901*ec179322SAlan Cox 			while (lo == 0 && ++i < NPOPMAP)
902*ec179322SAlan Cox 				lo = ffsl(~rv->popmap[i]);
903*ec179322SAlan Cox 			if (i == NPOPMAP)
904*ec179322SAlan Cox 				break;
905*ec179322SAlan Cox 			/* Convert from ffsl() to ordinary bit numbering. */
906*ec179322SAlan Cox 			lo--;
907*ec179322SAlan Cox 			next_free = NBPOPMAP * i + lo;
908*ec179322SAlan Cox 			pa = VM_PAGE_TO_PHYS(&rv->pages[next_free]);
909*ec179322SAlan Cox 			KASSERT(pa >= low,
910*ec179322SAlan Cox 			    ("vm_reserv_reclaim_contig: pa is too low"));
911*ec179322SAlan Cox 			if (pa + size > high) {
912*ec179322SAlan Cox 				/* The rest of this reservation is too high. */
913*ec179322SAlan Cox 				break;
914*ec179322SAlan Cox 			} else if ((pa & (alignment - 1)) != 0 ||
915*ec179322SAlan Cox 			    ((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0) {
916*ec179322SAlan Cox 				/* Continue with this reservation. */
917*ec179322SAlan Cox 				hi = lo;
918*ec179322SAlan Cox 				continue;
919*ec179322SAlan Cox 			}
920*ec179322SAlan Cox 			/* Find the next used page. */
921*ec179322SAlan Cox 			hi = ffsl(rv->popmap[i] & ~((1UL << lo) - 1));
922*ec179322SAlan Cox 			while (hi == 0 && ++i < NPOPMAP) {
923*ec179322SAlan Cox 				if ((NBPOPMAP * i - next_free) * PAGE_SIZE >=
924*ec179322SAlan Cox 				    size) {
92544aab2c3SAlan Cox 					vm_reserv_reclaim(rv);
92644aab2c3SAlan Cox 					return (TRUE);
92744aab2c3SAlan Cox 				}
928*ec179322SAlan Cox 				hi = ffsl(rv->popmap[i]);
929*ec179322SAlan Cox 			}
930*ec179322SAlan Cox 			/* Convert from ffsl() to ordinary bit numbering. */
931*ec179322SAlan Cox 			if (i != NPOPMAP)
932*ec179322SAlan Cox 				hi--;
933*ec179322SAlan Cox 			if ((NBPOPMAP * i + hi - next_free) * PAGE_SIZE >=
934*ec179322SAlan Cox 			    size) {
935*ec179322SAlan Cox 				vm_reserv_reclaim(rv);
936*ec179322SAlan Cox 				return (TRUE);
937*ec179322SAlan Cox 			}
938*ec179322SAlan Cox 		} while (i < NPOPMAP);
93944aab2c3SAlan Cox 	}
94044aab2c3SAlan Cox 	return (FALSE);
94144aab2c3SAlan Cox }
94244aab2c3SAlan Cox 
94344aab2c3SAlan Cox /*
944f8a47341SAlan Cox  * Transfers the reservation underlying the given page to a new object.
945f8a47341SAlan Cox  *
946f8a47341SAlan Cox  * The object must be locked.
947f8a47341SAlan Cox  */
948f8a47341SAlan Cox void
949f8a47341SAlan Cox vm_reserv_rename(vm_page_t m, vm_object_t new_object, vm_object_t old_object,
950f8a47341SAlan Cox     vm_pindex_t old_object_offset)
951f8a47341SAlan Cox {
952f8a47341SAlan Cox 	vm_reserv_t rv;
953f8a47341SAlan Cox 
95489f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(new_object);
955f8a47341SAlan Cox 	rv = vm_reserv_from_page(m);
956f8a47341SAlan Cox 	if (rv->object == old_object) {
957f8a47341SAlan Cox 		mtx_lock(&vm_page_queue_free_mtx);
958f8a47341SAlan Cox 		if (rv->object == old_object) {
959f8a47341SAlan Cox 			LIST_REMOVE(rv, objq);
960f8a47341SAlan Cox 			LIST_INSERT_HEAD(&new_object->rvq, rv, objq);
961f8a47341SAlan Cox 			rv->object = new_object;
962f8a47341SAlan Cox 			rv->pindex -= old_object_offset;
963f8a47341SAlan Cox 		}
964f8a47341SAlan Cox 		mtx_unlock(&vm_page_queue_free_mtx);
965f8a47341SAlan Cox 	}
966f8a47341SAlan Cox }
967f8a47341SAlan Cox 
968f8a47341SAlan Cox /*
969f8a47341SAlan Cox  * Allocates the virtual and physical memory required by the reservation
970f8a47341SAlan Cox  * management system's data structures, in particular, the reservation array.
971f8a47341SAlan Cox  */
972f8a47341SAlan Cox vm_paddr_t
973f8a47341SAlan Cox vm_reserv_startup(vm_offset_t *vaddr, vm_paddr_t end, vm_paddr_t high_water)
974f8a47341SAlan Cox {
975f8a47341SAlan Cox 	vm_paddr_t new_end;
976f8a47341SAlan Cox 	size_t size;
977f8a47341SAlan Cox 
978f8a47341SAlan Cox 	/*
979f8a47341SAlan Cox 	 * Calculate the size (in bytes) of the reservation array.  Round up
980f8a47341SAlan Cox 	 * from "high_water" because every small page is mapped to an element
981f8a47341SAlan Cox 	 * in the reservation array based on its physical address.  Thus, the
982f8a47341SAlan Cox 	 * number of elements in the reservation array can be greater than the
983f8a47341SAlan Cox 	 * number of superpages.
984f8a47341SAlan Cox 	 */
985f8a47341SAlan Cox 	size = howmany(high_water, VM_LEVEL_0_SIZE) * sizeof(struct vm_reserv);
986f8a47341SAlan Cox 
987f8a47341SAlan Cox 	/*
988f8a47341SAlan Cox 	 * Allocate and map the physical memory for the reservation array.  The
989f8a47341SAlan Cox 	 * next available virtual address is returned by reference.
990f8a47341SAlan Cox 	 */
991f8a47341SAlan Cox 	new_end = end - round_page(size);
992f8a47341SAlan Cox 	vm_reserv_array = (void *)(uintptr_t)pmap_map(vaddr, new_end, end,
993f8a47341SAlan Cox 	    VM_PROT_READ | VM_PROT_WRITE);
994f8a47341SAlan Cox 	bzero(vm_reserv_array, size);
995f8a47341SAlan Cox 
996f8a47341SAlan Cox 	/*
997f8a47341SAlan Cox 	 * Return the next available physical address.
998f8a47341SAlan Cox 	 */
999f8a47341SAlan Cox 	return (new_end);
1000f8a47341SAlan Cox }
1001f8a47341SAlan Cox 
1002f8a47341SAlan Cox #endif	/* VM_NRESERVLEVEL > 0 */
1003