xref: /freebsd/sys/vm/vm_reserv.c (revision a3cf0ef5a295c885c895fabfd56470c0d1db322d)
1 /*-
2  * Copyright (c) 2002-2006 Rice University
3  * Copyright (c) 2007-2008 Alan L. Cox <alc@cs.rice.edu>
4  * All rights reserved.
5  *
6  * This software was developed for the FreeBSD Project by Alan L. Cox,
7  * Olivier Crameri, Peter Druschel, Sitaram Iyer, and Juan Navarro.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
22  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
28  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  *	Superpage reservation management module
34  */
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include "opt_vm.h"
40 
41 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/mutex.h>
46 #include <sys/queue.h>
47 #include <sys/sbuf.h>
48 #include <sys/sysctl.h>
49 #include <sys/systm.h>
50 
51 #include <vm/vm.h>
52 #include <vm/vm_param.h>
53 #include <vm/vm_object.h>
54 #include <vm/vm_page.h>
55 #include <vm/vm_phys.h>
56 #include <vm/vm_reserv.h>
57 
58 /*
59  * The reservation system supports the speculative allocation of large physical
60  * pages ("superpages").  Speculative allocation enables the fully-automatic
61  * utilization of superpages by the virtual memory system.  In other words, no
62  * programmatic directives are required to use superpages.
63  */
64 
65 #if VM_NRESERVLEVEL > 0
66 
67 /*
68  * The number of small pages that are contained in a level 0 reservation
69  */
70 #define	VM_LEVEL_0_NPAGES	(1 << VM_LEVEL_0_ORDER)
71 
72 /*
73  * The number of bits by which a physical address is shifted to obtain the
74  * reservation number
75  */
76 #define	VM_LEVEL_0_SHIFT	(VM_LEVEL_0_ORDER + PAGE_SHIFT)
77 
78 /*
79  * The size of a level 0 reservation in bytes
80  */
81 #define	VM_LEVEL_0_SIZE		(1 << VM_LEVEL_0_SHIFT)
82 
83 /*
84  * Computes the index of the small page underlying the given (object, pindex)
85  * within the reservation's array of small pages.
86  */
87 #define	VM_RESERV_INDEX(object, pindex)	\
88     (((object)->pg_color + (pindex)) & (VM_LEVEL_0_NPAGES - 1))
89 
90 /*
91  * The reservation structure
92  *
93  * A reservation structure is constructed whenever a large physical page is
94  * speculatively allocated to an object.  The reservation provides the small
95  * physical pages for the range [pindex, pindex + VM_LEVEL_0_NPAGES) of offsets
96  * within that object.  The reservation's "popcnt" tracks the number of these
97  * small physical pages that are in use at any given time.  When and if the
98  * reservation is not fully utilized, it appears in the queue of partially-
99  * populated reservations.  The reservation always appears on the containing
100  * object's list of reservations.
101  *
102  * A partially-populated reservation can be broken and reclaimed at any time.
103  */
104 struct vm_reserv {
105 	TAILQ_ENTRY(vm_reserv) partpopq;
106 	LIST_ENTRY(vm_reserv) objq;
107 	vm_object_t	object;			/* containing object */
108 	vm_pindex_t	pindex;			/* offset within object */
109 	vm_page_t	pages;			/* first page of a superpage */
110 	int		popcnt;			/* # of pages in use */
111 	char		inpartpopq;
112 };
113 
114 /*
115  * The reservation array
116  *
117  * This array is analoguous in function to vm_page_array.  It differs in the
118  * respect that it may contain a greater number of useful reservation
119  * structures than there are (physical) superpages.  These "invalid"
120  * reservation structures exist to trade-off space for time in the
121  * implementation of vm_reserv_from_page().  Invalid reservation structures are
122  * distinguishable from "valid" reservation structures by inspecting the
123  * reservation's "pages" field.  Invalid reservation structures have a NULL
124  * "pages" field.
125  *
126  * vm_reserv_from_page() maps a small (physical) page to an element of this
127  * array by computing a physical reservation number from the page's physical
128  * address.  The physical reservation number is used as the array index.
129  *
130  * An "active" reservation is a valid reservation structure that has a non-NULL
131  * "object" field and a non-zero "popcnt" field.  In other words, every active
132  * reservation belongs to a particular object.  Moreover, every active
133  * reservation has an entry in the containing object's list of reservations.
134  */
135 static vm_reserv_t vm_reserv_array;
136 
137 /*
138  * The partially-populated reservation queue
139  *
140  * This queue enables the fast recovery of an unused cached or free small page
141  * from a partially-populated reservation.  The reservation at the head of
142  * this queue is the least-recently-changed, partially-populated reservation.
143  *
144  * Access to this queue is synchronized by the free page queue lock.
145  */
146 static TAILQ_HEAD(, vm_reserv) vm_rvq_partpop =
147 			    TAILQ_HEAD_INITIALIZER(vm_rvq_partpop);
148 
149 static SYSCTL_NODE(_vm, OID_AUTO, reserv, CTLFLAG_RD, 0, "Reservation Info");
150 
151 static long vm_reserv_broken;
152 SYSCTL_LONG(_vm_reserv, OID_AUTO, broken, CTLFLAG_RD,
153     &vm_reserv_broken, 0, "Cumulative number of broken reservations");
154 
155 static long vm_reserv_freed;
156 SYSCTL_LONG(_vm_reserv, OID_AUTO, freed, CTLFLAG_RD,
157     &vm_reserv_freed, 0, "Cumulative number of freed reservations");
158 
159 static int sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS);
160 
161 SYSCTL_OID(_vm_reserv, OID_AUTO, partpopq, CTLTYPE_STRING | CTLFLAG_RD, NULL, 0,
162     sysctl_vm_reserv_partpopq, "A", "Partially-populated reservation queues");
163 
164 static long vm_reserv_reclaimed;
165 SYSCTL_LONG(_vm_reserv, OID_AUTO, reclaimed, CTLFLAG_RD,
166     &vm_reserv_reclaimed, 0, "Cumulative number of reclaimed reservations");
167 
168 static void		vm_reserv_depopulate(vm_reserv_t rv);
169 static vm_reserv_t	vm_reserv_from_page(vm_page_t m);
170 static boolean_t	vm_reserv_has_pindex(vm_reserv_t rv,
171 			    vm_pindex_t pindex);
172 static void		vm_reserv_populate(vm_reserv_t rv);
173 static void		vm_reserv_reclaim(vm_reserv_t rv);
174 
175 /*
176  * Describes the current state of the partially-populated reservation queue.
177  */
178 static int
179 sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS)
180 {
181 	struct sbuf sbuf;
182 	vm_reserv_t rv;
183 	int counter, error, level, unused_pages;
184 
185 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
186 	sbuf_printf(&sbuf, "\nLEVEL     SIZE  NUMBER\n\n");
187 	for (level = -1; level <= VM_NRESERVLEVEL - 2; level++) {
188 		counter = 0;
189 		unused_pages = 0;
190 		mtx_lock(&vm_page_queue_free_mtx);
191 		TAILQ_FOREACH(rv, &vm_rvq_partpop/*[level]*/, partpopq) {
192 			counter++;
193 			unused_pages += VM_LEVEL_0_NPAGES - rv->popcnt;
194 		}
195 		mtx_unlock(&vm_page_queue_free_mtx);
196 		sbuf_printf(&sbuf, "%5d: %6dK, %6d\n", level,
197 		    unused_pages * ((int)PAGE_SIZE / 1024), counter);
198 	}
199 	error = sbuf_finish(&sbuf);
200 	sbuf_delete(&sbuf);
201 	return (error);
202 }
203 
204 /*
205  * Reduces the given reservation's population count.  If the population count
206  * becomes zero, the reservation is destroyed.  Additionally, moves the
207  * reservation to the tail of the partially-populated reservations queue if the
208  * population count is non-zero.
209  *
210  * The free page queue lock must be held.
211  */
212 static void
213 vm_reserv_depopulate(vm_reserv_t rv)
214 {
215 
216 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
217 	KASSERT(rv->object != NULL,
218 	    ("vm_reserv_depopulate: reserv %p is free", rv));
219 	KASSERT(rv->popcnt > 0,
220 	    ("vm_reserv_depopulate: reserv %p's popcnt is corrupted", rv));
221 	if (rv->inpartpopq) {
222 		TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq);
223 		rv->inpartpopq = FALSE;
224 	}
225 	rv->popcnt--;
226 	if (rv->popcnt == 0) {
227 		LIST_REMOVE(rv, objq);
228 		rv->object = NULL;
229 		vm_phys_free_pages(rv->pages, VM_LEVEL_0_ORDER);
230 		vm_reserv_freed++;
231 	} else {
232 		rv->inpartpopq = TRUE;
233 		TAILQ_INSERT_TAIL(&vm_rvq_partpop, rv, partpopq);
234 	}
235 }
236 
237 /*
238  * Returns the reservation to which the given page might belong.
239  */
240 static __inline vm_reserv_t
241 vm_reserv_from_page(vm_page_t m)
242 {
243 
244 	return (&vm_reserv_array[VM_PAGE_TO_PHYS(m) >> VM_LEVEL_0_SHIFT]);
245 }
246 
247 /*
248  * Returns TRUE if the given reservation contains the given page index and
249  * FALSE otherwise.
250  */
251 static __inline boolean_t
252 vm_reserv_has_pindex(vm_reserv_t rv, vm_pindex_t pindex)
253 {
254 
255 	return (((pindex - rv->pindex) & ~(VM_LEVEL_0_NPAGES - 1)) == 0);
256 }
257 
258 /*
259  * Increases the given reservation's population count.  Moves the reservation
260  * to the tail of the partially-populated reservation queue.
261  *
262  * The free page queue must be locked.
263  */
264 static void
265 vm_reserv_populate(vm_reserv_t rv)
266 {
267 
268 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
269 	KASSERT(rv->object != NULL,
270 	    ("vm_reserv_populate: reserv %p is free", rv));
271 	KASSERT(rv->popcnt < VM_LEVEL_0_NPAGES,
272 	    ("vm_reserv_populate: reserv %p is already full", rv));
273 	if (rv->inpartpopq) {
274 		TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq);
275 		rv->inpartpopq = FALSE;
276 	}
277 	rv->popcnt++;
278 	if (rv->popcnt < VM_LEVEL_0_NPAGES) {
279 		rv->inpartpopq = TRUE;
280 		TAILQ_INSERT_TAIL(&vm_rvq_partpop, rv, partpopq);
281 	}
282 }
283 
284 /*
285  * Allocates a page from an existing or newly-created reservation.
286  *
287  * The object and free page queue must be locked.
288  */
289 vm_page_t
290 vm_reserv_alloc_page(vm_object_t object, vm_pindex_t pindex)
291 {
292 	vm_page_t m, mpred, msucc;
293 	vm_pindex_t first, leftcap, rightcap;
294 	vm_reserv_t rv;
295 
296 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
297 
298 	/*
299 	 * Is a reservation fundamentally not possible?
300 	 */
301 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
302 	if (pindex < VM_RESERV_INDEX(object, pindex) ||
303 	    pindex >= object->size)
304 		return (NULL);
305 
306 	/*
307 	 * Look for an existing reservation.
308 	 */
309 	msucc = NULL;
310 	mpred = object->root;
311 	while (mpred != NULL) {
312 		KASSERT(mpred->pindex != pindex,
313 		    ("vm_reserv_alloc_page: pindex already allocated"));
314 		rv = vm_reserv_from_page(mpred);
315 		if (rv->object == object && vm_reserv_has_pindex(rv, pindex)) {
316 			m = &rv->pages[VM_RESERV_INDEX(object, pindex)];
317 			/* Handle vm_page_rename(m, new_object, ...). */
318 			if ((m->flags & (PG_CACHED | PG_FREE)) == 0)
319 				return (NULL);
320 			vm_reserv_populate(rv);
321 			return (m);
322 		} else if (mpred->pindex < pindex) {
323 			if (msucc != NULL ||
324 			    (msucc = TAILQ_NEXT(mpred, listq)) == NULL)
325 				break;
326 			KASSERT(msucc->pindex != pindex,
327 			    ("vm_reserv_alloc_page: pindex already allocated"));
328 			rv = vm_reserv_from_page(msucc);
329 			if (rv->object == object &&
330 			    vm_reserv_has_pindex(rv, pindex)) {
331 				m = &rv->pages[VM_RESERV_INDEX(object, pindex)];
332 				/* Handle vm_page_rename(m, new_object, ...). */
333 				if ((m->flags & (PG_CACHED | PG_FREE)) == 0)
334 					return (NULL);
335 				vm_reserv_populate(rv);
336 				return (m);
337 			} else if (pindex < msucc->pindex)
338 				break;
339 		} else if (msucc == NULL) {
340 			msucc = mpred;
341 			mpred = TAILQ_PREV(msucc, pglist, listq);
342 			continue;
343 		}
344 		msucc = NULL;
345 		mpred = object->root = vm_page_splay(pindex, object->root);
346 	}
347 
348 	/*
349 	 * Determine the first index to the left that can be used.
350 	 */
351 	if (mpred == NULL)
352 		leftcap = 0;
353 	else if ((rv = vm_reserv_from_page(mpred))->object != object)
354 		leftcap = mpred->pindex + 1;
355 	else
356 		leftcap = rv->pindex + VM_LEVEL_0_NPAGES;
357 
358 	/*
359 	 * Determine the first index to the right that cannot be used.
360 	 */
361 	if (msucc == NULL)
362 		rightcap = pindex + VM_LEVEL_0_NPAGES;
363 	else if ((rv = vm_reserv_from_page(msucc))->object != object)
364 		rightcap = msucc->pindex;
365 	else
366 		rightcap = rv->pindex;
367 
368 	/*
369 	 * Determine if a reservation fits between the first index to
370 	 * the left that can be used and the first index to the right
371 	 * that cannot be used.
372 	 */
373 	first = pindex - VM_RESERV_INDEX(object, pindex);
374 	if (first < leftcap || first + VM_LEVEL_0_NPAGES > rightcap)
375 		return (NULL);
376 
377 	/*
378 	 * Would a new reservation extend past the end of the given object?
379 	 */
380 	if (object->size < first + VM_LEVEL_0_NPAGES) {
381 		/*
382 		 * Don't allocate a new reservation if the object is a vnode or
383 		 * backed by another object that is a vnode.
384 		 */
385 		if (object->type == OBJT_VNODE ||
386 		    (object->backing_object != NULL &&
387 		    object->backing_object->type == OBJT_VNODE))
388 			return (NULL);
389 		/* Speculate that the object may grow. */
390 	}
391 
392 	/*
393 	 * Allocate a new reservation.
394 	 */
395 	m = vm_phys_alloc_pages(VM_FREEPOOL_DEFAULT, VM_LEVEL_0_ORDER);
396 	if (m != NULL) {
397 		rv = vm_reserv_from_page(m);
398 		KASSERT(rv->pages == m,
399 		    ("vm_reserv_alloc_page: reserv %p's pages is corrupted",
400 		    rv));
401 		KASSERT(rv->object == NULL,
402 		    ("vm_reserv_alloc_page: reserv %p isn't free", rv));
403 		LIST_INSERT_HEAD(&object->rvq, rv, objq);
404 		rv->object = object;
405 		rv->pindex = first;
406 		KASSERT(rv->popcnt == 0,
407 		    ("vm_reserv_alloc_page: reserv %p's popcnt is corrupted",
408 		    rv));
409 		KASSERT(!rv->inpartpopq,
410 		    ("vm_reserv_alloc_page: reserv %p's inpartpopq is TRUE",
411 		    rv));
412 		vm_reserv_populate(rv);
413 		m = &rv->pages[VM_RESERV_INDEX(object, pindex)];
414 	}
415 	return (m);
416 }
417 
418 /*
419  * Breaks all reservations belonging to the given object.
420  */
421 void
422 vm_reserv_break_all(vm_object_t object)
423 {
424 	vm_reserv_t rv;
425 	int i;
426 
427 	mtx_lock(&vm_page_queue_free_mtx);
428 	while ((rv = LIST_FIRST(&object->rvq)) != NULL) {
429 		KASSERT(rv->object == object,
430 		    ("vm_reserv_break_all: reserv %p is corrupted", rv));
431 		if (rv->inpartpopq) {
432 			TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq);
433 			rv->inpartpopq = FALSE;
434 		}
435 		LIST_REMOVE(rv, objq);
436 		rv->object = NULL;
437 		for (i = 0; i < VM_LEVEL_0_NPAGES; i++) {
438 			if ((rv->pages[i].flags & (PG_CACHED | PG_FREE)) != 0)
439 				vm_phys_free_pages(&rv->pages[i], 0);
440 			else
441 				rv->popcnt--;
442 		}
443 		KASSERT(rv->popcnt == 0,
444 		    ("vm_reserv_break_all: reserv %p's popcnt is corrupted",
445 		    rv));
446 		vm_reserv_broken++;
447 	}
448 	mtx_unlock(&vm_page_queue_free_mtx);
449 }
450 
451 /*
452  * Frees the given page if it belongs to a reservation.  Returns TRUE if the
453  * page is freed and FALSE otherwise.
454  *
455  * The free page queue lock must be held.
456  */
457 boolean_t
458 vm_reserv_free_page(vm_page_t m)
459 {
460 	vm_reserv_t rv;
461 
462 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
463 	rv = vm_reserv_from_page(m);
464 	if (rv->object != NULL) {
465 		vm_reserv_depopulate(rv);
466 		return (TRUE);
467 	}
468 	return (FALSE);
469 }
470 
471 /*
472  * Initializes the reservation management system.  Specifically, initializes
473  * the reservation array.
474  *
475  * Requires that vm_page_array and first_page are initialized!
476  */
477 void
478 vm_reserv_init(void)
479 {
480 	vm_paddr_t paddr;
481 	int i;
482 
483 	/*
484 	 * Initialize the reservation array.  Specifically, initialize the
485 	 * "pages" field for every element that has an underlying superpage.
486 	 */
487 	for (i = 0; phys_avail[i + 1] != 0; i += 2) {
488 		paddr = roundup2(phys_avail[i], VM_LEVEL_0_SIZE);
489 		while (paddr + VM_LEVEL_0_SIZE <= phys_avail[i + 1]) {
490 			vm_reserv_array[paddr >> VM_LEVEL_0_SHIFT].pages =
491 			    PHYS_TO_VM_PAGE(paddr);
492 			paddr += VM_LEVEL_0_SIZE;
493 		}
494 	}
495 }
496 
497 /*
498  * Returns a reservation level if the given page belongs to a fully-populated
499  * reservation and -1 otherwise.
500  */
501 int
502 vm_reserv_level_iffullpop(vm_page_t m)
503 {
504 	vm_reserv_t rv;
505 
506 	rv = vm_reserv_from_page(m);
507 	return (rv->popcnt == VM_LEVEL_0_NPAGES ? 0 : -1);
508 }
509 
510 /*
511  * Prepare for the reactivation of a cached page.
512  *
513  * First, suppose that the given page "m" was allocated individually, i.e., not
514  * as part of a reservation, and cached.  Then, suppose a reservation
515  * containing "m" is allocated by the same object.  Although "m" and the
516  * reservation belong to the same object, "m"'s pindex may not match the
517  * reservation's.
518  *
519  * The free page queue must be locked.
520  */
521 boolean_t
522 vm_reserv_reactivate_page(vm_page_t m)
523 {
524 	vm_reserv_t rv;
525 	int i, m_index;
526 
527 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
528 	rv = vm_reserv_from_page(m);
529 	if (rv->object == NULL)
530 		return (FALSE);
531 	KASSERT((m->flags & PG_CACHED) != 0,
532 	    ("vm_reserv_uncache_page: page %p is not cached", m));
533 	if (m->object == rv->object &&
534 	    m->pindex - rv->pindex == VM_RESERV_INDEX(m->object, m->pindex))
535 		vm_reserv_populate(rv);
536 	else {
537 		KASSERT(rv->inpartpopq,
538 		    ("vm_reserv_uncache_page: reserv %p's inpartpopq is FALSE",
539 		    rv));
540 		TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq);
541 		rv->inpartpopq = FALSE;
542 		LIST_REMOVE(rv, objq);
543 		rv->object = NULL;
544 		/* Don't vm_phys_free_pages(m, 0). */
545 		m_index = m - rv->pages;
546 		for (i = 0; i < m_index; i++) {
547 			if ((rv->pages[i].flags & (PG_CACHED | PG_FREE)) != 0)
548 				vm_phys_free_pages(&rv->pages[i], 0);
549 			else
550 				rv->popcnt--;
551 		}
552 		for (i++; i < VM_LEVEL_0_NPAGES; i++) {
553 			if ((rv->pages[i].flags & (PG_CACHED | PG_FREE)) != 0)
554 				vm_phys_free_pages(&rv->pages[i], 0);
555 			else
556 				rv->popcnt--;
557 		}
558 		KASSERT(rv->popcnt == 0,
559 		    ("vm_reserv_uncache_page: reserv %p's popcnt is corrupted",
560 		    rv));
561 		vm_reserv_broken++;
562 	}
563 	return (TRUE);
564 }
565 
566 /*
567  * Breaks the given partially-populated reservation, releasing its cached and
568  * free pages to the physical memory allocator.
569  *
570  * The free page queue lock must be held.
571  */
572 static void
573 vm_reserv_reclaim(vm_reserv_t rv)
574 {
575 	int i;
576 
577 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
578 	KASSERT(rv->inpartpopq,
579 	    ("vm_reserv_reclaim: reserv %p's inpartpopq is corrupted", rv));
580 	TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq);
581 	rv->inpartpopq = FALSE;
582 	KASSERT(rv->object != NULL,
583 	    ("vm_reserv_reclaim: reserv %p is free", rv));
584 	LIST_REMOVE(rv, objq);
585 	rv->object = NULL;
586 	for (i = 0; i < VM_LEVEL_0_NPAGES; i++) {
587 		if ((rv->pages[i].flags & (PG_CACHED | PG_FREE)) != 0)
588 			vm_phys_free_pages(&rv->pages[i], 0);
589 		else
590 			rv->popcnt--;
591 	}
592 	KASSERT(rv->popcnt == 0,
593 	    ("vm_reserv_reclaim: reserv %p's popcnt is corrupted", rv));
594 	vm_reserv_reclaimed++;
595 }
596 
597 /*
598  * Breaks the reservation at the head of the partially-populated reservation
599  * queue, releasing its cached and free pages to the physical memory
600  * allocator.  Returns TRUE if a reservation is broken and FALSE otherwise.
601  *
602  * The free page queue lock must be held.
603  */
604 boolean_t
605 vm_reserv_reclaim_inactive(void)
606 {
607 	vm_reserv_t rv;
608 
609 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
610 	if ((rv = TAILQ_FIRST(&vm_rvq_partpop)) != NULL) {
611 		vm_reserv_reclaim(rv);
612 		return (TRUE);
613 	}
614 	return (FALSE);
615 }
616 
617 /*
618  * Searches the partially-populated reservation queue for the least recently
619  * active reservation with unused pages, i.e., cached or free, that satisfy the
620  * given request for contiguous physical memory.  If a satisfactory reservation
621  * is found, it is broken.  Returns TRUE if a reservation is broken and FALSE
622  * otherwise.
623  *
624  * The free page queue lock must be held.
625  */
626 boolean_t
627 vm_reserv_reclaim_contig(vm_paddr_t size, vm_paddr_t low, vm_paddr_t high,
628     unsigned long alignment, unsigned long boundary)
629 {
630 	vm_paddr_t pa, pa_length;
631 	vm_reserv_t rv;
632 	int i;
633 
634 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
635 	if (size > VM_LEVEL_0_SIZE - PAGE_SIZE)
636 		return (FALSE);
637 	TAILQ_FOREACH(rv, &vm_rvq_partpop, partpopq) {
638 		pa = VM_PAGE_TO_PHYS(&rv->pages[VM_LEVEL_0_NPAGES - 1]);
639 		if (pa + PAGE_SIZE - size < low) {
640 			/* this entire reservation is too low; go to next */
641 			continue;
642 		}
643 		pa_length = 0;
644 		for (i = 0; i < VM_LEVEL_0_NPAGES; i++)
645 			if ((rv->pages[i].flags & (PG_CACHED | PG_FREE)) != 0) {
646 				pa_length += PAGE_SIZE;
647 				if (pa_length == PAGE_SIZE) {
648 					pa = VM_PAGE_TO_PHYS(&rv->pages[i]);
649 					if (pa + size > high) {
650 						/* skip to next reservation */
651 						break;
652 					} else if (pa < low ||
653 					    (pa & (alignment - 1)) != 0 ||
654 					    ((pa ^ (pa + size - 1)) &
655 					    ~(boundary - 1)) != 0)
656 						pa_length = 0;
657 				} else if (pa_length >= size) {
658 					vm_reserv_reclaim(rv);
659 					return (TRUE);
660 				}
661 			} else
662 				pa_length = 0;
663 	}
664 	return (FALSE);
665 }
666 
667 /*
668  * Transfers the reservation underlying the given page to a new object.
669  *
670  * The object must be locked.
671  */
672 void
673 vm_reserv_rename(vm_page_t m, vm_object_t new_object, vm_object_t old_object,
674     vm_pindex_t old_object_offset)
675 {
676 	vm_reserv_t rv;
677 
678 	VM_OBJECT_LOCK_ASSERT(new_object, MA_OWNED);
679 	rv = vm_reserv_from_page(m);
680 	if (rv->object == old_object) {
681 		mtx_lock(&vm_page_queue_free_mtx);
682 		if (rv->object == old_object) {
683 			LIST_REMOVE(rv, objq);
684 			LIST_INSERT_HEAD(&new_object->rvq, rv, objq);
685 			rv->object = new_object;
686 			rv->pindex -= old_object_offset;
687 		}
688 		mtx_unlock(&vm_page_queue_free_mtx);
689 	}
690 }
691 
692 /*
693  * Allocates the virtual and physical memory required by the reservation
694  * management system's data structures, in particular, the reservation array.
695  */
696 vm_paddr_t
697 vm_reserv_startup(vm_offset_t *vaddr, vm_paddr_t end, vm_paddr_t high_water)
698 {
699 	vm_paddr_t new_end;
700 	size_t size;
701 
702 	/*
703 	 * Calculate the size (in bytes) of the reservation array.  Round up
704 	 * from "high_water" because every small page is mapped to an element
705 	 * in the reservation array based on its physical address.  Thus, the
706 	 * number of elements in the reservation array can be greater than the
707 	 * number of superpages.
708 	 */
709 	size = howmany(high_water, VM_LEVEL_0_SIZE) * sizeof(struct vm_reserv);
710 
711 	/*
712 	 * Allocate and map the physical memory for the reservation array.  The
713 	 * next available virtual address is returned by reference.
714 	 */
715 	new_end = end - round_page(size);
716 	vm_reserv_array = (void *)(uintptr_t)pmap_map(vaddr, new_end, end,
717 	    VM_PROT_READ | VM_PROT_WRITE);
718 	bzero(vm_reserv_array, size);
719 
720 	/*
721 	 * Return the next available physical address.
722 	 */
723 	return (new_end);
724 }
725 
726 #endif	/* VM_NRESERVLEVEL > 0 */
727