xref: /freebsd-12.1/sys/vm/vm_reserv.c (revision d3575add)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2002-2006 Rice University
5  * Copyright (c) 2007-2011 Alan L. Cox <[email protected]>
6  * All rights reserved.
7  *
8  * This software was developed for the FreeBSD Project by Alan L. Cox,
9  * Olivier Crameri, Peter Druschel, Sitaram Iyer, and Juan Navarro.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
24  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
30  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  *	Superpage reservation management module
36  *
37  * Any external functions defined by this module are only to be used by the
38  * virtual memory system.
39  */
40 
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #include "opt_vm.h"
45 
46 #include <sys/param.h>
47 #include <sys/kernel.h>
48 #include <sys/lock.h>
49 #include <sys/malloc.h>
50 #include <sys/mutex.h>
51 #include <sys/queue.h>
52 #include <sys/rwlock.h>
53 #include <sys/sbuf.h>
54 #include <sys/sysctl.h>
55 #include <sys/systm.h>
56 #include <sys/counter.h>
57 #include <sys/ktr.h>
58 #include <sys/vmmeter.h>
59 #include <sys/smp.h>
60 
61 #include <vm/vm.h>
62 #include <vm/vm_param.h>
63 #include <vm/vm_object.h>
64 #include <vm/vm_page.h>
65 #include <vm/vm_pageout.h>
66 #include <vm/vm_phys.h>
67 #include <vm/vm_pagequeue.h>
68 #include <vm/vm_radix.h>
69 #include <vm/vm_reserv.h>
70 
71 /*
72  * The reservation system supports the speculative allocation of large physical
73  * pages ("superpages").  Speculative allocation enables the fully automatic
74  * utilization of superpages by the virtual memory system.  In other words, no
75  * programmatic directives are required to use superpages.
76  */
77 
78 #if VM_NRESERVLEVEL > 0
79 
80 #ifndef VM_LEVEL_0_ORDER_MAX
81 #define	VM_LEVEL_0_ORDER_MAX	VM_LEVEL_0_ORDER
82 #endif
83 
84 /*
85  * The number of small pages that are contained in a level 0 reservation
86  */
87 #define	VM_LEVEL_0_NPAGES	(1 << VM_LEVEL_0_ORDER)
88 #define	VM_LEVEL_0_NPAGES_MAX	(1 << VM_LEVEL_0_ORDER_MAX)
89 
90 /*
91  * The number of bits by which a physical address is shifted to obtain the
92  * reservation number
93  */
94 #define	VM_LEVEL_0_SHIFT	(VM_LEVEL_0_ORDER + PAGE_SHIFT)
95 
96 /*
97  * The size of a level 0 reservation in bytes
98  */
99 #define	VM_LEVEL_0_SIZE		(1 << VM_LEVEL_0_SHIFT)
100 
101 /*
102  * Computes the index of the small page underlying the given (object, pindex)
103  * within the reservation's array of small pages.
104  */
105 #define	VM_RESERV_INDEX(object, pindex)	\
106     (((object)->pg_color + (pindex)) & (VM_LEVEL_0_NPAGES - 1))
107 
108 /*
109  * The size of a population map entry
110  */
111 typedef	u_long		popmap_t;
112 
113 /*
114  * The number of bits in a population map entry
115  */
116 #define	NBPOPMAP	(NBBY * sizeof(popmap_t))
117 
118 /*
119  * The number of population map entries in a reservation
120  */
121 #define	NPOPMAP		howmany(VM_LEVEL_0_NPAGES, NBPOPMAP)
122 #define	NPOPMAP_MAX	howmany(VM_LEVEL_0_NPAGES_MAX, NBPOPMAP)
123 
124 /*
125  * Number of elapsed ticks before we update the LRU queue position.  Used
126  * to reduce contention and churn on the list.
127  */
128 #define	PARTPOPSLOP	1
129 
130 /*
131  * Clear a bit in the population map.
132  */
133 static __inline void
popmap_clear(popmap_t popmap[],int i)134 popmap_clear(popmap_t popmap[], int i)
135 {
136 
137 	popmap[i / NBPOPMAP] &= ~(1UL << (i % NBPOPMAP));
138 }
139 
140 /*
141  * Set a bit in the population map.
142  */
143 static __inline void
popmap_set(popmap_t popmap[],int i)144 popmap_set(popmap_t popmap[], int i)
145 {
146 
147 	popmap[i / NBPOPMAP] |= 1UL << (i % NBPOPMAP);
148 }
149 
150 /*
151  * Is a bit in the population map clear?
152  */
153 static __inline boolean_t
popmap_is_clear(popmap_t popmap[],int i)154 popmap_is_clear(popmap_t popmap[], int i)
155 {
156 
157 	return ((popmap[i / NBPOPMAP] & (1UL << (i % NBPOPMAP))) == 0);
158 }
159 
160 /*
161  * Is a bit in the population map set?
162  */
163 static __inline boolean_t
popmap_is_set(popmap_t popmap[],int i)164 popmap_is_set(popmap_t popmap[], int i)
165 {
166 
167 	return ((popmap[i / NBPOPMAP] & (1UL << (i % NBPOPMAP))) != 0);
168 }
169 
170 /*
171  * The reservation structure
172  *
173  * A reservation structure is constructed whenever a large physical page is
174  * speculatively allocated to an object.  The reservation provides the small
175  * physical pages for the range [pindex, pindex + VM_LEVEL_0_NPAGES) of offsets
176  * within that object.  The reservation's "popcnt" tracks the number of these
177  * small physical pages that are in use at any given time.  When and if the
178  * reservation is not fully utilized, it appears in the queue of partially
179  * populated reservations.  The reservation always appears on the containing
180  * object's list of reservations.
181  *
182  * A partially populated reservation can be broken and reclaimed at any time.
183  *
184  * r - vm_reserv_lock
185  * d - vm_reserv_domain_lock
186  * o - vm_reserv_object_lock
187  * c - constant after boot
188  */
189 struct vm_reserv {
190 	struct mtx	lock;			/* reservation lock. */
191 	TAILQ_ENTRY(vm_reserv) partpopq;	/* (d) per-domain queue. */
192 	LIST_ENTRY(vm_reserv) objq;		/* (o, r) object queue */
193 	vm_object_t	object;			/* (o, r) containing object */
194 	vm_pindex_t	pindex;			/* (o, r) offset in object */
195 	vm_page_t	pages;			/* (c) first page  */
196 	uint16_t	domain;			/* (c) NUMA domain. */
197 	uint16_t	popcnt;			/* (r) # of pages in use */
198 	int		lasttick;		/* (r) last pop update tick. */
199 	char		inpartpopq;		/* (d) */
200 	popmap_t	popmap[NPOPMAP_MAX];	/* (r) bit vector, used pages */
201 };
202 
203 #define	vm_reserv_lockptr(rv)		(&(rv)->lock)
204 #define	vm_reserv_assert_locked(rv)					\
205 	    mtx_assert(vm_reserv_lockptr(rv), MA_OWNED)
206 #define	vm_reserv_lock(rv)		mtx_lock(vm_reserv_lockptr(rv))
207 #define	vm_reserv_trylock(rv)		mtx_trylock(vm_reserv_lockptr(rv))
208 #define	vm_reserv_unlock(rv)		mtx_unlock(vm_reserv_lockptr(rv))
209 
210 static struct mtx_padalign vm_reserv_domain_locks[MAXMEMDOM];
211 
212 #define	vm_reserv_domain_lockptr(d)	&vm_reserv_domain_locks[(d)]
213 #define	vm_reserv_domain_lock(d)	mtx_lock(vm_reserv_domain_lockptr(d))
214 #define	vm_reserv_domain_unlock(d)	mtx_unlock(vm_reserv_domain_lockptr(d))
215 
216 /*
217  * The reservation array
218  *
219  * This array is analoguous in function to vm_page_array.  It differs in the
220  * respect that it may contain a greater number of useful reservation
221  * structures than there are (physical) superpages.  These "invalid"
222  * reservation structures exist to trade-off space for time in the
223  * implementation of vm_reserv_from_page().  Invalid reservation structures are
224  * distinguishable from "valid" reservation structures by inspecting the
225  * reservation's "pages" field.  Invalid reservation structures have a NULL
226  * "pages" field.
227  *
228  * vm_reserv_from_page() maps a small (physical) page to an element of this
229  * array by computing a physical reservation number from the page's physical
230  * address.  The physical reservation number is used as the array index.
231  *
232  * An "active" reservation is a valid reservation structure that has a non-NULL
233  * "object" field and a non-zero "popcnt" field.  In other words, every active
234  * reservation belongs to a particular object.  Moreover, every active
235  * reservation has an entry in the containing object's list of reservations.
236  */
237 static vm_reserv_t vm_reserv_array;
238 
239 /*
240  * The partially populated reservation queue
241  *
242  * This queue enables the fast recovery of an unused free small page from a
243  * partially populated reservation.  The reservation at the head of this queue
244  * is the least recently changed, partially populated reservation.
245  *
246  * Access to this queue is synchronized by the free page queue lock.
247  */
248 static TAILQ_HEAD(, vm_reserv) vm_rvq_partpop[MAXMEMDOM];
249 
250 static SYSCTL_NODE(_vm, OID_AUTO, reserv, CTLFLAG_RD, 0, "Reservation Info");
251 
252 static counter_u64_t vm_reserv_broken = EARLY_COUNTER;
253 SYSCTL_COUNTER_U64(_vm_reserv, OID_AUTO, broken, CTLFLAG_RD,
254     &vm_reserv_broken, "Cumulative number of broken reservations");
255 
256 static counter_u64_t vm_reserv_freed = EARLY_COUNTER;
257 SYSCTL_COUNTER_U64(_vm_reserv, OID_AUTO, freed, CTLFLAG_RD,
258     &vm_reserv_freed, "Cumulative number of freed reservations");
259 
260 static int sysctl_vm_reserv_fullpop(SYSCTL_HANDLER_ARGS);
261 
262 SYSCTL_PROC(_vm_reserv, OID_AUTO, fullpop, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
263     sysctl_vm_reserv_fullpop, "I", "Current number of full reservations");
264 
265 static int sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS);
266 
267 SYSCTL_OID(_vm_reserv, OID_AUTO, partpopq, CTLTYPE_STRING | CTLFLAG_RD, NULL, 0,
268     sysctl_vm_reserv_partpopq, "A", "Partially populated reservation queues");
269 
270 static counter_u64_t vm_reserv_reclaimed = EARLY_COUNTER;
271 SYSCTL_COUNTER_U64(_vm_reserv, OID_AUTO, reclaimed, CTLFLAG_RD,
272     &vm_reserv_reclaimed, "Cumulative number of reclaimed reservations");
273 
274 /*
275  * The object lock pool is used to synchronize the rvq.  We can not use a
276  * pool mutex because it is required before malloc works.
277  *
278  * The "hash" function could be made faster without divide and modulo.
279  */
280 #define	VM_RESERV_OBJ_LOCK_COUNT	MAXCPU
281 
282 struct mtx_padalign vm_reserv_object_mtx[VM_RESERV_OBJ_LOCK_COUNT];
283 
284 #define	vm_reserv_object_lock_idx(object)			\
285 	    (((uintptr_t)object / sizeof(*object)) % VM_RESERV_OBJ_LOCK_COUNT)
286 #define	vm_reserv_object_lock_ptr(object)			\
287 	    &vm_reserv_object_mtx[vm_reserv_object_lock_idx((object))]
288 #define	vm_reserv_object_lock(object)				\
289 	    mtx_lock(vm_reserv_object_lock_ptr((object)))
290 #define	vm_reserv_object_unlock(object)				\
291 	    mtx_unlock(vm_reserv_object_lock_ptr((object)))
292 
293 static void		vm_reserv_break(vm_reserv_t rv);
294 static void		vm_reserv_depopulate(vm_reserv_t rv, int index);
295 static vm_reserv_t	vm_reserv_from_page(vm_page_t m);
296 static boolean_t	vm_reserv_has_pindex(vm_reserv_t rv,
297 			    vm_pindex_t pindex);
298 static void		vm_reserv_populate(vm_reserv_t rv, int index);
299 static void		vm_reserv_reclaim(vm_reserv_t rv);
300 
301 /*
302  * Returns the current number of full reservations.
303  *
304  * Since the number of full reservations is computed without acquiring the
305  * free page queue lock, the returned value may be inexact.
306  */
307 static int
sysctl_vm_reserv_fullpop(SYSCTL_HANDLER_ARGS)308 sysctl_vm_reserv_fullpop(SYSCTL_HANDLER_ARGS)
309 {
310 	vm_paddr_t paddr;
311 	struct vm_phys_seg *seg;
312 	vm_reserv_t rv;
313 	int fullpop, segind;
314 
315 	fullpop = 0;
316 	for (segind = 0; segind < vm_phys_nsegs; segind++) {
317 		seg = &vm_phys_segs[segind];
318 		paddr = roundup2(seg->start, VM_LEVEL_0_SIZE);
319 		while (paddr + VM_LEVEL_0_SIZE > paddr && paddr +
320 		    VM_LEVEL_0_SIZE <= seg->end) {
321 			rv = &vm_reserv_array[paddr >> VM_LEVEL_0_SHIFT];
322 			fullpop += rv->popcnt == VM_LEVEL_0_NPAGES;
323 			paddr += VM_LEVEL_0_SIZE;
324 		}
325 	}
326 	return (sysctl_handle_int(oidp, &fullpop, 0, req));
327 }
328 
329 /*
330  * Describes the current state of the partially populated reservation queue.
331  */
332 static int
sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS)333 sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS)
334 {
335 	struct sbuf sbuf;
336 	vm_reserv_t rv;
337 	int counter, error, domain, level, unused_pages;
338 
339 	error = sysctl_wire_old_buffer(req, 0);
340 	if (error != 0)
341 		return (error);
342 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
343 	sbuf_printf(&sbuf, "\nDOMAIN    LEVEL     SIZE  NUMBER\n\n");
344 	for (domain = 0; domain < vm_ndomains; domain++) {
345 		for (level = -1; level <= VM_NRESERVLEVEL - 2; level++) {
346 			counter = 0;
347 			unused_pages = 0;
348 			vm_reserv_domain_lock(domain);
349 			TAILQ_FOREACH(rv, &vm_rvq_partpop[domain], partpopq) {
350 				counter++;
351 				unused_pages += VM_LEVEL_0_NPAGES - rv->popcnt;
352 			}
353 			vm_reserv_domain_unlock(domain);
354 			sbuf_printf(&sbuf, "%6d, %7d, %6dK, %6d\n",
355 			    domain, level,
356 			    unused_pages * ((int)PAGE_SIZE / 1024), counter);
357 		}
358 	}
359 	error = sbuf_finish(&sbuf);
360 	sbuf_delete(&sbuf);
361 	return (error);
362 }
363 
364 /*
365  * Remove a reservation from the object's objq.
366  */
367 static void
vm_reserv_remove(vm_reserv_t rv)368 vm_reserv_remove(vm_reserv_t rv)
369 {
370 	vm_object_t object;
371 
372 	vm_reserv_assert_locked(rv);
373 	CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d",
374 	    __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq);
375 	KASSERT(rv->object != NULL,
376 	    ("vm_reserv_remove: reserv %p is free", rv));
377 	KASSERT(!rv->inpartpopq,
378 	    ("vm_reserv_remove: reserv %p's inpartpopq is TRUE", rv));
379 	object = rv->object;
380 	vm_reserv_object_lock(object);
381 	LIST_REMOVE(rv, objq);
382 	rv->object = NULL;
383 	vm_reserv_object_unlock(object);
384 }
385 
386 /*
387  * Insert a new reservation into the object's objq.
388  */
389 static void
vm_reserv_insert(vm_reserv_t rv,vm_object_t object,vm_pindex_t pindex)390 vm_reserv_insert(vm_reserv_t rv, vm_object_t object, vm_pindex_t pindex)
391 {
392 	int i;
393 
394 	vm_reserv_assert_locked(rv);
395 	CTR6(KTR_VM,
396 	    "%s: rv %p(%p) object %p new %p popcnt %d",
397 	    __FUNCTION__, rv, rv->pages, rv->object, object,
398 	   rv->popcnt);
399 	KASSERT(rv->object == NULL,
400 	    ("vm_reserv_insert: reserv %p isn't free", rv));
401 	KASSERT(rv->popcnt == 0,
402 	    ("vm_reserv_insert: reserv %p's popcnt is corrupted", rv));
403 	KASSERT(!rv->inpartpopq,
404 	    ("vm_reserv_insert: reserv %p's inpartpopq is TRUE", rv));
405 	for (i = 0; i < NPOPMAP; i++)
406 		KASSERT(rv->popmap[i] == 0,
407 		    ("vm_reserv_insert: reserv %p's popmap is corrupted", rv));
408 	vm_reserv_object_lock(object);
409 	rv->pindex = pindex;
410 	rv->object = object;
411 	rv->lasttick = ticks;
412 	LIST_INSERT_HEAD(&object->rvq, rv, objq);
413 	vm_reserv_object_unlock(object);
414 }
415 
416 /*
417  * Reduces the given reservation's population count.  If the population count
418  * becomes zero, the reservation is destroyed.  Additionally, moves the
419  * reservation to the tail of the partially populated reservation queue if the
420  * population count is non-zero.
421  */
422 static void
vm_reserv_depopulate(vm_reserv_t rv,int index)423 vm_reserv_depopulate(vm_reserv_t rv, int index)
424 {
425 	struct vm_domain *vmd;
426 
427 	vm_reserv_assert_locked(rv);
428 	CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d",
429 	    __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq);
430 	KASSERT(rv->object != NULL,
431 	    ("vm_reserv_depopulate: reserv %p is free", rv));
432 	KASSERT(popmap_is_set(rv->popmap, index),
433 	    ("vm_reserv_depopulate: reserv %p's popmap[%d] is clear", rv,
434 	    index));
435 	KASSERT(rv->popcnt > 0,
436 	    ("vm_reserv_depopulate: reserv %p's popcnt is corrupted", rv));
437 	KASSERT(rv->domain < vm_ndomains,
438 	    ("vm_reserv_depopulate: reserv %p's domain is corrupted %d",
439 	    rv, rv->domain));
440 	if (rv->popcnt == VM_LEVEL_0_NPAGES) {
441 		KASSERT(rv->pages->psind == 1,
442 		    ("vm_reserv_depopulate: reserv %p is already demoted",
443 		    rv));
444 		rv->pages->psind = 0;
445 	}
446 	popmap_clear(rv->popmap, index);
447 	rv->popcnt--;
448 	if ((unsigned)(ticks - rv->lasttick) >= PARTPOPSLOP ||
449 	    rv->popcnt == 0) {
450 		vm_reserv_domain_lock(rv->domain);
451 		if (rv->inpartpopq) {
452 			TAILQ_REMOVE(&vm_rvq_partpop[rv->domain], rv, partpopq);
453 			rv->inpartpopq = FALSE;
454 		}
455 		if (rv->popcnt != 0) {
456 			rv->inpartpopq = TRUE;
457 			TAILQ_INSERT_TAIL(&vm_rvq_partpop[rv->domain], rv, partpopq);
458 		}
459 		vm_reserv_domain_unlock(rv->domain);
460 		rv->lasttick = ticks;
461 	}
462 	vmd = VM_DOMAIN(rv->domain);
463 	if (rv->popcnt == 0) {
464 		vm_reserv_remove(rv);
465 		vm_domain_free_lock(vmd);
466 		vm_phys_free_pages(rv->pages, VM_LEVEL_0_ORDER);
467 		vm_domain_free_unlock(vmd);
468 		counter_u64_add(vm_reserv_freed, 1);
469 	}
470 	vm_domain_freecnt_inc(vmd, 1);
471 }
472 
473 /*
474  * Returns the reservation to which the given page might belong.
475  */
476 static __inline vm_reserv_t
vm_reserv_from_page(vm_page_t m)477 vm_reserv_from_page(vm_page_t m)
478 {
479 
480 	return (&vm_reserv_array[VM_PAGE_TO_PHYS(m) >> VM_LEVEL_0_SHIFT]);
481 }
482 
483 /*
484  * Returns an existing reservation or NULL and initialized successor pointer.
485  */
486 static vm_reserv_t
vm_reserv_from_object(vm_object_t object,vm_pindex_t pindex,vm_page_t mpred,vm_page_t * msuccp)487 vm_reserv_from_object(vm_object_t object, vm_pindex_t pindex,
488     vm_page_t mpred, vm_page_t *msuccp)
489 {
490 	vm_reserv_t rv;
491 	vm_page_t msucc;
492 
493 	msucc = NULL;
494 	if (mpred != NULL) {
495 		KASSERT(mpred->object == object,
496 		    ("vm_reserv_from_object: object doesn't contain mpred"));
497 		KASSERT(mpred->pindex < pindex,
498 		    ("vm_reserv_from_object: mpred doesn't precede pindex"));
499 		rv = vm_reserv_from_page(mpred);
500 		if (rv->object == object && vm_reserv_has_pindex(rv, pindex))
501 			goto found;
502 		msucc = TAILQ_NEXT(mpred, listq);
503 	} else
504 		msucc = TAILQ_FIRST(&object->memq);
505 	if (msucc != NULL) {
506 		KASSERT(msucc->pindex > pindex,
507 		    ("vm_reserv_from_object: msucc doesn't succeed pindex"));
508 		rv = vm_reserv_from_page(msucc);
509 		if (rv->object == object && vm_reserv_has_pindex(rv, pindex))
510 			goto found;
511 	}
512 	rv = NULL;
513 
514 found:
515 	*msuccp = msucc;
516 
517 	return (rv);
518 }
519 
520 /*
521  * Returns TRUE if the given reservation contains the given page index and
522  * FALSE otherwise.
523  */
524 static __inline boolean_t
vm_reserv_has_pindex(vm_reserv_t rv,vm_pindex_t pindex)525 vm_reserv_has_pindex(vm_reserv_t rv, vm_pindex_t pindex)
526 {
527 
528 	return (((pindex - rv->pindex) & ~(VM_LEVEL_0_NPAGES - 1)) == 0);
529 }
530 
531 /*
532  * Increases the given reservation's population count.  Moves the reservation
533  * to the tail of the partially populated reservation queue.
534  *
535  * The free page queue must be locked.
536  */
537 static void
vm_reserv_populate(vm_reserv_t rv,int index)538 vm_reserv_populate(vm_reserv_t rv, int index)
539 {
540 
541 	vm_reserv_assert_locked(rv);
542 	CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d",
543 	    __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq);
544 	KASSERT(rv->object != NULL,
545 	    ("vm_reserv_populate: reserv %p is free", rv));
546 	KASSERT(popmap_is_clear(rv->popmap, index),
547 	    ("vm_reserv_populate: reserv %p's popmap[%d] is set", rv,
548 	    index));
549 	KASSERT(rv->popcnt < VM_LEVEL_0_NPAGES,
550 	    ("vm_reserv_populate: reserv %p is already full", rv));
551 	KASSERT(rv->pages->psind == 0,
552 	    ("vm_reserv_populate: reserv %p is already promoted", rv));
553 	KASSERT(rv->domain < vm_ndomains,
554 	    ("vm_reserv_populate: reserv %p's domain is corrupted %d",
555 	    rv, rv->domain));
556 	popmap_set(rv->popmap, index);
557 	rv->popcnt++;
558 	if ((unsigned)(ticks - rv->lasttick) < PARTPOPSLOP &&
559 	    rv->inpartpopq && rv->popcnt != VM_LEVEL_0_NPAGES)
560 		return;
561 	rv->lasttick = ticks;
562 	vm_reserv_domain_lock(rv->domain);
563 	if (rv->inpartpopq) {
564 		TAILQ_REMOVE(&vm_rvq_partpop[rv->domain], rv, partpopq);
565 		rv->inpartpopq = FALSE;
566 	}
567 	if (rv->popcnt < VM_LEVEL_0_NPAGES) {
568 		rv->inpartpopq = TRUE;
569 		TAILQ_INSERT_TAIL(&vm_rvq_partpop[rv->domain], rv, partpopq);
570 	} else {
571 		KASSERT(rv->pages->psind == 0,
572 		    ("vm_reserv_populate: reserv %p is already promoted",
573 		    rv));
574 		rv->pages->psind = 1;
575 	}
576 	vm_reserv_domain_unlock(rv->domain);
577 }
578 
579 /*
580  * Attempts to allocate a contiguous set of physical pages from existing
581  * reservations.  See vm_reserv_alloc_contig() for a description of the
582  * function's parameters.
583  *
584  * The page "mpred" must immediately precede the offset "pindex" within the
585  * specified object.
586  *
587  * The object must be locked.
588  */
589 vm_page_t
vm_reserv_extend_contig(int req,vm_object_t object,vm_pindex_t pindex,int domain,u_long npages,vm_paddr_t low,vm_paddr_t high,u_long alignment,vm_paddr_t boundary,vm_page_t mpred)590 vm_reserv_extend_contig(int req, vm_object_t object, vm_pindex_t pindex,
591     int domain, u_long npages, vm_paddr_t low, vm_paddr_t high,
592     u_long alignment, vm_paddr_t boundary, vm_page_t mpred)
593 {
594 	struct vm_domain *vmd;
595 	vm_paddr_t pa, size;
596 	vm_page_t m, msucc;
597 	vm_reserv_t rv;
598 	int i, index;
599 
600 	VM_OBJECT_ASSERT_WLOCKED(object);
601 	KASSERT(npages != 0, ("vm_reserv_alloc_contig: npages is 0"));
602 
603 	/*
604 	 * Is a reservation fundamentally impossible?
605 	 */
606 	if (pindex < VM_RESERV_INDEX(object, pindex) ||
607 	    pindex + npages > object->size || object->resident_page_count == 0)
608 		return (NULL);
609 
610 	/*
611 	 * All reservations of a particular size have the same alignment.
612 	 * Assuming that the first page is allocated from a reservation, the
613 	 * least significant bits of its physical address can be determined
614 	 * from its offset from the beginning of the reservation and the size
615 	 * of the reservation.
616 	 *
617 	 * Could the specified index within a reservation of the smallest
618 	 * possible size satisfy the alignment and boundary requirements?
619 	 */
620 	pa = VM_RESERV_INDEX(object, pindex) << PAGE_SHIFT;
621 	if ((pa & (alignment - 1)) != 0)
622 		return (NULL);
623 	size = npages << PAGE_SHIFT;
624 	if (((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0)
625 		return (NULL);
626 
627 	/*
628 	 * Look for an existing reservation.
629 	 */
630 	rv = vm_reserv_from_object(object, pindex, mpred, &msucc);
631 	if (rv == NULL)
632 		return (NULL);
633 	KASSERT(object != kernel_object || rv->domain == domain,
634 	    ("vm_reserv_extend_contig: Domain mismatch from reservation."));
635 	index = VM_RESERV_INDEX(object, pindex);
636 	/* Does the allocation fit within the reservation? */
637 	if (index + npages > VM_LEVEL_0_NPAGES)
638 		return (NULL);
639 	domain = rv->domain;
640 	vmd = VM_DOMAIN(domain);
641 	vm_reserv_lock(rv);
642 	if (rv->object != object)
643 		goto out;
644 	m = &rv->pages[index];
645 	pa = VM_PAGE_TO_PHYS(m);
646 	if (pa < low || pa + size > high || (pa & (alignment - 1)) != 0 ||
647 	    ((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0)
648 		goto out;
649 	/* Handle vm_page_rename(m, new_object, ...). */
650 	for (i = 0; i < npages; i++) {
651 		if (popmap_is_set(rv->popmap, index + i))
652 			goto out;
653 	}
654 	if (!vm_domain_allocate(vmd, req, npages))
655 		goto out;
656 	for (i = 0; i < npages; i++)
657 		vm_reserv_populate(rv, index + i);
658 	vm_reserv_unlock(rv);
659 	return (m);
660 
661 out:
662 	vm_reserv_unlock(rv);
663 	return (NULL);
664 }
665 
666 /*
667  * Allocates a contiguous set of physical pages of the given size "npages"
668  * from newly created reservations.  All of the physical pages
669  * must be at or above the given physical address "low" and below the given
670  * physical address "high".  The given value "alignment" determines the
671  * alignment of the first physical page in the set.  If the given value
672  * "boundary" is non-zero, then the set of physical pages cannot cross any
673  * physical address boundary that is a multiple of that value.  Both
674  * "alignment" and "boundary" must be a power of two.
675  *
676  * Callers should first invoke vm_reserv_extend_contig() to attempt an
677  * allocation from existing reservations.
678  *
679  * The page "mpred" must immediately precede the offset "pindex" within the
680  * specified object.
681  *
682  * The object and free page queue must be locked.
683  */
684 vm_page_t
vm_reserv_alloc_contig(int req,vm_object_t object,vm_pindex_t pindex,int domain,u_long npages,vm_paddr_t low,vm_paddr_t high,u_long alignment,vm_paddr_t boundary,vm_page_t mpred)685 vm_reserv_alloc_contig(int req, vm_object_t object, vm_pindex_t pindex, int domain,
686     u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
687     vm_paddr_t boundary, vm_page_t mpred)
688 {
689 	struct vm_domain *vmd;
690 	vm_paddr_t pa, size;
691 	vm_page_t m, m_ret, msucc;
692 	vm_pindex_t first, leftcap, rightcap;
693 	vm_reserv_t rv;
694 	u_long allocpages, maxpages, minpages;
695 	int i, index, n;
696 
697 	VM_OBJECT_ASSERT_WLOCKED(object);
698 	KASSERT(npages != 0, ("vm_reserv_alloc_contig: npages is 0"));
699 
700 	/*
701 	 * Is a reservation fundamentally impossible?
702 	 */
703 	if (pindex < VM_RESERV_INDEX(object, pindex) ||
704 	    pindex + npages > object->size)
705 		return (NULL);
706 
707 	/*
708 	 * All reservations of a particular size have the same alignment.
709 	 * Assuming that the first page is allocated from a reservation, the
710 	 * least significant bits of its physical address can be determined
711 	 * from its offset from the beginning of the reservation and the size
712 	 * of the reservation.
713 	 *
714 	 * Could the specified index within a reservation of the smallest
715 	 * possible size satisfy the alignment and boundary requirements?
716 	 */
717 	pa = VM_RESERV_INDEX(object, pindex) << PAGE_SHIFT;
718 	if ((pa & (alignment - 1)) != 0)
719 		return (NULL);
720 	size = npages << PAGE_SHIFT;
721 	if (((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0)
722 		return (NULL);
723 
724 	/*
725 	 * Callers should've extended an existing reservation prior to
726 	 * calling this function.  If a reservation exists it is
727 	 * incompatible with the allocation.
728 	 */
729 	rv = vm_reserv_from_object(object, pindex, mpred, &msucc);
730 	if (rv != NULL)
731 		return (NULL);
732 
733 	/*
734 	 * Could at least one reservation fit between the first index to the
735 	 * left that can be used ("leftcap") and the first index to the right
736 	 * that cannot be used ("rightcap")?
737 	 *
738 	 * We must synchronize with the reserv object lock to protect the
739 	 * pindex/object of the resulting reservations against rename while
740 	 * we are inspecting.
741 	 */
742 	first = pindex - VM_RESERV_INDEX(object, pindex);
743 	minpages = VM_RESERV_INDEX(object, pindex) + npages;
744 	maxpages = roundup2(minpages, VM_LEVEL_0_NPAGES);
745 	allocpages = maxpages;
746 	vm_reserv_object_lock(object);
747 	if (mpred != NULL) {
748 		if ((rv = vm_reserv_from_page(mpred))->object != object)
749 			leftcap = mpred->pindex + 1;
750 		else
751 			leftcap = rv->pindex + VM_LEVEL_0_NPAGES;
752 		if (leftcap > first) {
753 			vm_reserv_object_unlock(object);
754 			return (NULL);
755 		}
756 	}
757 	if (msucc != NULL) {
758 		if ((rv = vm_reserv_from_page(msucc))->object != object)
759 			rightcap = msucc->pindex;
760 		else
761 			rightcap = rv->pindex;
762 		if (first + maxpages > rightcap) {
763 			if (maxpages == VM_LEVEL_0_NPAGES) {
764 				vm_reserv_object_unlock(object);
765 				return (NULL);
766 			}
767 
768 			/*
769 			 * At least one reservation will fit between "leftcap"
770 			 * and "rightcap".  However, a reservation for the
771 			 * last of the requested pages will not fit.  Reduce
772 			 * the size of the upcoming allocation accordingly.
773 			 */
774 			allocpages = minpages;
775 		}
776 	}
777 	vm_reserv_object_unlock(object);
778 
779 	/*
780 	 * Would the last new reservation extend past the end of the object?
781 	 */
782 	if (first + maxpages > object->size) {
783 		/*
784 		 * Don't allocate the last new reservation if the object is a
785 		 * vnode or backed by another object that is a vnode.
786 		 */
787 		if (object->type == OBJT_VNODE ||
788 		    (object->backing_object != NULL &&
789 		    object->backing_object->type == OBJT_VNODE)) {
790 			if (maxpages == VM_LEVEL_0_NPAGES)
791 				return (NULL);
792 			allocpages = minpages;
793 		}
794 		/* Speculate that the object may grow. */
795 	}
796 
797 	/*
798 	 * Allocate the physical pages.  The alignment and boundary specified
799 	 * for this allocation may be different from the alignment and
800 	 * boundary specified for the requested pages.  For instance, the
801 	 * specified index may not be the first page within the first new
802 	 * reservation.
803 	 */
804 	m = NULL;
805 	vmd = VM_DOMAIN(domain);
806 	if (vm_domain_allocate(vmd, req, npages)) {
807 		vm_domain_free_lock(vmd);
808 		m = vm_phys_alloc_contig(domain, allocpages, low, high,
809 		    ulmax(alignment, VM_LEVEL_0_SIZE),
810 		    boundary > VM_LEVEL_0_SIZE ? boundary : 0);
811 		vm_domain_free_unlock(vmd);
812 		if (m == NULL) {
813 			vm_domain_freecnt_inc(vmd, npages);
814 			return (NULL);
815 		}
816 	} else
817 		return (NULL);
818 	KASSERT(vm_phys_domain(m) == domain,
819 	    ("vm_reserv_alloc_contig: Page domain does not match requested."));
820 
821 	/*
822 	 * The allocated physical pages always begin at a reservation
823 	 * boundary, but they do not always end at a reservation boundary.
824 	 * Initialize every reservation that is completely covered by the
825 	 * allocated physical pages.
826 	 */
827 	m_ret = NULL;
828 	index = VM_RESERV_INDEX(object, pindex);
829 	do {
830 		rv = vm_reserv_from_page(m);
831 		KASSERT(rv->pages == m,
832 		    ("vm_reserv_alloc_contig: reserv %p's pages is corrupted",
833 		    rv));
834 		vm_reserv_lock(rv);
835 		vm_reserv_insert(rv, object, first);
836 		n = ulmin(VM_LEVEL_0_NPAGES - index, npages);
837 		for (i = 0; i < n; i++)
838 			vm_reserv_populate(rv, index + i);
839 		npages -= n;
840 		if (m_ret == NULL) {
841 			m_ret = &rv->pages[index];
842 			index = 0;
843 		}
844 		vm_reserv_unlock(rv);
845 		m += VM_LEVEL_0_NPAGES;
846 		first += VM_LEVEL_0_NPAGES;
847 		allocpages -= VM_LEVEL_0_NPAGES;
848 	} while (allocpages >= VM_LEVEL_0_NPAGES);
849 	return (m_ret);
850 }
851 
852 /*
853  * Attempts to extend an existing reservation and allocate the page to the
854  * object.
855  *
856  * The page "mpred" must immediately precede the offset "pindex" within the
857  * specified object.
858  *
859  * The object must be locked.
860  */
861 vm_page_t
vm_reserv_extend(int req,vm_object_t object,vm_pindex_t pindex,int domain,vm_page_t mpred)862 vm_reserv_extend(int req, vm_object_t object, vm_pindex_t pindex, int domain,
863     vm_page_t mpred)
864 {
865 	struct vm_domain *vmd;
866 	vm_page_t m, msucc;
867 	vm_reserv_t rv;
868 	int index;
869 
870 	VM_OBJECT_ASSERT_WLOCKED(object);
871 
872 	/*
873 	 * Could a reservation currently exist?
874 	 */
875 	if (pindex < VM_RESERV_INDEX(object, pindex) ||
876 	    pindex >= object->size || object->resident_page_count == 0)
877 		return (NULL);
878 
879 	/*
880 	 * Look for an existing reservation.
881 	 */
882 	rv = vm_reserv_from_object(object, pindex, mpred, &msucc);
883 	if (rv == NULL)
884 		return (NULL);
885 
886 	KASSERT(object != kernel_object || rv->domain == domain,
887 	    ("vm_reserv_extend: Domain mismatch from reservation."));
888 	domain = rv->domain;
889 	vmd = VM_DOMAIN(domain);
890 	index = VM_RESERV_INDEX(object, pindex);
891 	m = &rv->pages[index];
892 	vm_reserv_lock(rv);
893 	/* Handle reclaim race. */
894 	if (rv->object != object ||
895 	    /* Handle vm_page_rename(m, new_object, ...). */
896 	    popmap_is_set(rv->popmap, index)) {
897 		m = NULL;
898 		goto out;
899 	}
900 	if (vm_domain_allocate(vmd, req, 1) == 0)
901 		m = NULL;
902 	else
903 		vm_reserv_populate(rv, index);
904 out:
905 	vm_reserv_unlock(rv);
906 
907 	return (m);
908 }
909 
910 /*
911  * Attempts to allocate a new reservation for the object, and allocates a
912  * page from that reservation.  Callers should first invoke vm_reserv_extend()
913  * to attempt an allocation from an existing reservation.
914  *
915  * The page "mpred" must immediately precede the offset "pindex" within the
916  * specified object.
917  *
918  * The object and free page queue must be locked.
919  */
920 vm_page_t
vm_reserv_alloc_page(int req,vm_object_t object,vm_pindex_t pindex,int domain,vm_page_t mpred)921 vm_reserv_alloc_page(int req, vm_object_t object, vm_pindex_t pindex, int domain,
922     vm_page_t mpred)
923 {
924 	struct vm_domain *vmd;
925 	vm_page_t m, msucc;
926 	vm_pindex_t first, leftcap, rightcap;
927 	vm_reserv_t rv;
928 	int index;
929 
930 	VM_OBJECT_ASSERT_WLOCKED(object);
931 
932 	/*
933 	 * Is a reservation fundamentally impossible?
934 	 */
935 	if (pindex < VM_RESERV_INDEX(object, pindex) ||
936 	    pindex >= object->size)
937 		return (NULL);
938 
939 	/*
940 	 * Callers should've extended an existing reservation prior to
941 	 * calling this function.  If a reservation exists it is
942 	 * incompatible with the allocation.
943 	 */
944 	rv = vm_reserv_from_object(object, pindex, mpred, &msucc);
945 	if (rv != NULL)
946 		return (NULL);
947 
948 	/*
949 	 * Could a reservation fit between the first index to the left that
950 	 * can be used and the first index to the right that cannot be used?
951 	 *
952 	 * We must synchronize with the reserv object lock to protect the
953 	 * pindex/object of the resulting reservations against rename while
954 	 * we are inspecting.
955 	 */
956 	first = pindex - VM_RESERV_INDEX(object, pindex);
957 	vm_reserv_object_lock(object);
958 	if (mpred != NULL) {
959 		if ((rv = vm_reserv_from_page(mpred))->object != object)
960 			leftcap = mpred->pindex + 1;
961 		else
962 			leftcap = rv->pindex + VM_LEVEL_0_NPAGES;
963 		if (leftcap > first) {
964 			vm_reserv_object_unlock(object);
965 			return (NULL);
966 		}
967 	}
968 	if (msucc != NULL) {
969 		if ((rv = vm_reserv_from_page(msucc))->object != object)
970 			rightcap = msucc->pindex;
971 		else
972 			rightcap = rv->pindex;
973 		if (first + VM_LEVEL_0_NPAGES > rightcap) {
974 			vm_reserv_object_unlock(object);
975 			return (NULL);
976 		}
977 	}
978 	vm_reserv_object_unlock(object);
979 
980 	/*
981 	 * Would a new reservation extend past the end of the object?
982 	 */
983 	if (first + VM_LEVEL_0_NPAGES > object->size) {
984 		/*
985 		 * Don't allocate a new reservation if the object is a vnode or
986 		 * backed by another object that is a vnode.
987 		 */
988 		if (object->type == OBJT_VNODE ||
989 		    (object->backing_object != NULL &&
990 		    object->backing_object->type == OBJT_VNODE))
991 			return (NULL);
992 		/* Speculate that the object may grow. */
993 	}
994 
995 	/*
996 	 * Allocate and populate the new reservation.
997 	 */
998 	m = NULL;
999 	vmd = VM_DOMAIN(domain);
1000 	if (vm_domain_allocate(vmd, req, 1)) {
1001 		vm_domain_free_lock(vmd);
1002 		m = vm_phys_alloc_pages(domain, VM_FREEPOOL_DEFAULT,
1003 		    VM_LEVEL_0_ORDER);
1004 		vm_domain_free_unlock(vmd);
1005 		if (m == NULL) {
1006 			vm_domain_freecnt_inc(vmd, 1);
1007 			return (NULL);
1008 		}
1009 	} else
1010 		return (NULL);
1011 	rv = vm_reserv_from_page(m);
1012 	vm_reserv_lock(rv);
1013 	KASSERT(rv->pages == m,
1014 	    ("vm_reserv_alloc_page: reserv %p's pages is corrupted", rv));
1015 	vm_reserv_insert(rv, object, first);
1016 	index = VM_RESERV_INDEX(object, pindex);
1017 	vm_reserv_populate(rv, index);
1018 	vm_reserv_unlock(rv);
1019 
1020 	return (&rv->pages[index]);
1021 }
1022 
1023 /*
1024  * Breaks the given reservation.  All free pages in the reservation
1025  * are returned to the physical memory allocator.  The reservation's
1026  * population count and map are reset to their initial state.
1027  *
1028  * The given reservation must not be in the partially populated reservation
1029  * queue.  The free page queue lock must be held.
1030  */
1031 static void
vm_reserv_break(vm_reserv_t rv)1032 vm_reserv_break(vm_reserv_t rv)
1033 {
1034 	int begin_zeroes, hi, i, lo;
1035 
1036 	vm_reserv_assert_locked(rv);
1037 	CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d",
1038 	    __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq);
1039 	vm_reserv_remove(rv);
1040 	rv->pages->psind = 0;
1041 	i = hi = 0;
1042 	do {
1043 		/* Find the next 0 bit.  Any previous 0 bits are < "hi". */
1044 		lo = ffsl(~(((1UL << hi) - 1) | rv->popmap[i]));
1045 		if (lo == 0) {
1046 			/* Redundantly clears bits < "hi". */
1047 			rv->popmap[i] = 0;
1048 			rv->popcnt -= NBPOPMAP - hi;
1049 			while (++i < NPOPMAP) {
1050 				lo = ffsl(~rv->popmap[i]);
1051 				if (lo == 0) {
1052 					rv->popmap[i] = 0;
1053 					rv->popcnt -= NBPOPMAP;
1054 				} else
1055 					break;
1056 			}
1057 			if (i == NPOPMAP)
1058 				break;
1059 			hi = 0;
1060 		}
1061 		KASSERT(lo > 0, ("vm_reserv_break: lo is %d", lo));
1062 		/* Convert from ffsl() to ordinary bit numbering. */
1063 		lo--;
1064 		if (lo > 0) {
1065 			/* Redundantly clears bits < "hi". */
1066 			rv->popmap[i] &= ~((1UL << lo) - 1);
1067 			rv->popcnt -= lo - hi;
1068 		}
1069 		begin_zeroes = NBPOPMAP * i + lo;
1070 		/* Find the next 1 bit. */
1071 		do
1072 			hi = ffsl(rv->popmap[i]);
1073 		while (hi == 0 && ++i < NPOPMAP);
1074 		if (i != NPOPMAP)
1075 			/* Convert from ffsl() to ordinary bit numbering. */
1076 			hi--;
1077 		vm_domain_free_lock(VM_DOMAIN(rv->domain));
1078 		vm_phys_free_contig(&rv->pages[begin_zeroes], NBPOPMAP * i +
1079 		    hi - begin_zeroes);
1080 		vm_domain_free_unlock(VM_DOMAIN(rv->domain));
1081 	} while (i < NPOPMAP);
1082 	KASSERT(rv->popcnt == 0,
1083 	    ("vm_reserv_break: reserv %p's popcnt is corrupted", rv));
1084 	counter_u64_add(vm_reserv_broken, 1);
1085 }
1086 
1087 /*
1088  * Breaks all reservations belonging to the given object.
1089  */
1090 void
vm_reserv_break_all(vm_object_t object)1091 vm_reserv_break_all(vm_object_t object)
1092 {
1093 	vm_reserv_t rv;
1094 
1095 	/*
1096 	 * This access of object->rvq is unsynchronized so that the
1097 	 * object rvq lock can nest after the domain_free lock.  We
1098 	 * must check for races in the results.  However, the object
1099 	 * lock prevents new additions, so we are guaranteed that when
1100 	 * it returns NULL the object is properly empty.
1101 	 */
1102 	while ((rv = LIST_FIRST(&object->rvq)) != NULL) {
1103 		vm_reserv_lock(rv);
1104 		/* Reclaim race. */
1105 		if (rv->object != object) {
1106 			vm_reserv_unlock(rv);
1107 			continue;
1108 		}
1109 		vm_reserv_domain_lock(rv->domain);
1110 		if (rv->inpartpopq) {
1111 			TAILQ_REMOVE(&vm_rvq_partpop[rv->domain], rv, partpopq);
1112 			rv->inpartpopq = FALSE;
1113 		}
1114 		vm_reserv_domain_unlock(rv->domain);
1115 		vm_reserv_break(rv);
1116 		vm_reserv_unlock(rv);
1117 	}
1118 }
1119 
1120 /*
1121  * Frees the given page if it belongs to a reservation.  Returns TRUE if the
1122  * page is freed and FALSE otherwise.
1123  *
1124  * The free page queue lock must be held.
1125  */
1126 boolean_t
vm_reserv_free_page(vm_page_t m)1127 vm_reserv_free_page(vm_page_t m)
1128 {
1129 	vm_reserv_t rv;
1130 	boolean_t ret;
1131 
1132 	rv = vm_reserv_from_page(m);
1133 	if (rv->object == NULL)
1134 		return (FALSE);
1135 	vm_reserv_lock(rv);
1136 	/* Re-validate after lock. */
1137 	if (rv->object != NULL) {
1138 		vm_reserv_depopulate(rv, m - rv->pages);
1139 		ret = TRUE;
1140 	} else
1141 		ret = FALSE;
1142 	vm_reserv_unlock(rv);
1143 
1144 	return (ret);
1145 }
1146 
1147 /*
1148  * Initializes the reservation management system.  Specifically, initializes
1149  * the reservation array.
1150  *
1151  * Requires that vm_page_array and first_page are initialized!
1152  */
1153 void
vm_reserv_init(void)1154 vm_reserv_init(void)
1155 {
1156 	vm_paddr_t paddr;
1157 	struct vm_phys_seg *seg;
1158 	struct vm_reserv *rv;
1159 	int i, segind;
1160 
1161 	/*
1162 	 * Initialize the reservation array.  Specifically, initialize the
1163 	 * "pages" field for every element that has an underlying superpage.
1164 	 */
1165 	for (segind = 0; segind < vm_phys_nsegs; segind++) {
1166 		seg = &vm_phys_segs[segind];
1167 		paddr = roundup2(seg->start, VM_LEVEL_0_SIZE);
1168 		while (paddr + VM_LEVEL_0_SIZE > paddr && paddr +
1169 		    VM_LEVEL_0_SIZE <= seg->end) {
1170 			rv = &vm_reserv_array[paddr >> VM_LEVEL_0_SHIFT];
1171 			rv->pages = PHYS_TO_VM_PAGE(paddr);
1172 			rv->domain = seg->domain;
1173 			mtx_init(&rv->lock, "vm reserv", NULL, MTX_DEF);
1174 			paddr += VM_LEVEL_0_SIZE;
1175 		}
1176 	}
1177 	for (i = 0; i < MAXMEMDOM; i++) {
1178 		mtx_init(&vm_reserv_domain_locks[i], "VM reserv domain", NULL,
1179 		    MTX_DEF);
1180 		TAILQ_INIT(&vm_rvq_partpop[i]);
1181 	}
1182 
1183 	for (i = 0; i < VM_RESERV_OBJ_LOCK_COUNT; i++)
1184 		mtx_init(&vm_reserv_object_mtx[i], "resv obj lock", NULL,
1185 		    MTX_DEF);
1186 }
1187 
1188 /*
1189  * Returns true if the given page belongs to a reservation and that page is
1190  * free.  Otherwise, returns false.
1191  */
1192 bool
vm_reserv_is_page_free(vm_page_t m)1193 vm_reserv_is_page_free(vm_page_t m)
1194 {
1195 	vm_reserv_t rv;
1196 
1197 	rv = vm_reserv_from_page(m);
1198 	if (rv->object == NULL)
1199 		return (false);
1200 	return (popmap_is_clear(rv->popmap, m - rv->pages));
1201 }
1202 
1203 /*
1204  * If the given page belongs to a reservation, returns the level of that
1205  * reservation.  Otherwise, returns -1.
1206  */
1207 int
vm_reserv_level(vm_page_t m)1208 vm_reserv_level(vm_page_t m)
1209 {
1210 	vm_reserv_t rv;
1211 
1212 	rv = vm_reserv_from_page(m);
1213 	return (rv->object != NULL ? 0 : -1);
1214 }
1215 
1216 /*
1217  * Returns a reservation level if the given page belongs to a fully populated
1218  * reservation and -1 otherwise.
1219  */
1220 int
vm_reserv_level_iffullpop(vm_page_t m)1221 vm_reserv_level_iffullpop(vm_page_t m)
1222 {
1223 	vm_reserv_t rv;
1224 
1225 	rv = vm_reserv_from_page(m);
1226 	return (rv->popcnt == VM_LEVEL_0_NPAGES ? 0 : -1);
1227 }
1228 
1229 /*
1230  * Breaks the given partially populated reservation, releasing its free pages
1231  * to the physical memory allocator.
1232  *
1233  * The free page queue lock must be held.
1234  */
1235 static void
vm_reserv_reclaim(vm_reserv_t rv)1236 vm_reserv_reclaim(vm_reserv_t rv)
1237 {
1238 
1239 	vm_reserv_assert_locked(rv);
1240 	CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d",
1241 	    __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq);
1242 	vm_reserv_domain_lock(rv->domain);
1243 	KASSERT(rv->inpartpopq,
1244 	    ("vm_reserv_reclaim: reserv %p's inpartpopq is FALSE", rv));
1245 	KASSERT(rv->domain < vm_ndomains,
1246 	    ("vm_reserv_reclaim: reserv %p's domain is corrupted %d",
1247 	    rv, rv->domain));
1248 	TAILQ_REMOVE(&vm_rvq_partpop[rv->domain], rv, partpopq);
1249 	rv->inpartpopq = FALSE;
1250 	vm_reserv_domain_unlock(rv->domain);
1251 	vm_reserv_break(rv);
1252 	counter_u64_add(vm_reserv_reclaimed, 1);
1253 }
1254 
1255 /*
1256  * Breaks the reservation at the head of the partially populated reservation
1257  * queue, releasing its free pages to the physical memory allocator.  Returns
1258  * TRUE if a reservation is broken and FALSE otherwise.
1259  *
1260  * The free page queue lock must be held.
1261  */
1262 boolean_t
vm_reserv_reclaim_inactive(int domain)1263 vm_reserv_reclaim_inactive(int domain)
1264 {
1265 	vm_reserv_t rv;
1266 
1267 	while ((rv = TAILQ_FIRST(&vm_rvq_partpop[domain])) != NULL) {
1268 		vm_reserv_lock(rv);
1269 		if (rv != TAILQ_FIRST(&vm_rvq_partpop[domain])) {
1270 			vm_reserv_unlock(rv);
1271 			continue;
1272 		}
1273 		vm_reserv_reclaim(rv);
1274 		vm_reserv_unlock(rv);
1275 		return (TRUE);
1276 	}
1277 	return (FALSE);
1278 }
1279 
1280 /*
1281  * Searches the partially populated reservation queue for the least recently
1282  * changed reservation with free pages that satisfy the given request for
1283  * contiguous physical memory.  If a satisfactory reservation is found, it is
1284  * broken.  Returns TRUE if a reservation is broken and FALSE otherwise.
1285  *
1286  * The free page queue lock must be held.
1287  */
1288 boolean_t
vm_reserv_reclaim_contig(int domain,u_long npages,vm_paddr_t low,vm_paddr_t high,u_long alignment,vm_paddr_t boundary)1289 vm_reserv_reclaim_contig(int domain, u_long npages, vm_paddr_t low,
1290     vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
1291 {
1292 	vm_paddr_t pa, size;
1293 	vm_reserv_t rv, rvn;
1294 	int hi, i, lo, low_index, next_free;
1295 
1296 	if (npages > VM_LEVEL_0_NPAGES - 1)
1297 		return (FALSE);
1298 	size = npages << PAGE_SHIFT;
1299 	vm_reserv_domain_lock(domain);
1300 again:
1301 	for (rv = TAILQ_FIRST(&vm_rvq_partpop[domain]); rv != NULL; rv = rvn) {
1302 		rvn = TAILQ_NEXT(rv, partpopq);
1303 		pa = VM_PAGE_TO_PHYS(&rv->pages[VM_LEVEL_0_NPAGES - 1]);
1304 		if (pa + PAGE_SIZE - size < low) {
1305 			/* This entire reservation is too low; go to next. */
1306 			continue;
1307 		}
1308 		pa = VM_PAGE_TO_PHYS(&rv->pages[0]);
1309 		if (pa + size > high) {
1310 			/* This entire reservation is too high; go to next. */
1311 			continue;
1312 		}
1313 		if (vm_reserv_trylock(rv) == 0) {
1314 			vm_reserv_domain_unlock(domain);
1315 			vm_reserv_lock(rv);
1316 			if (!rv->inpartpopq) {
1317 				vm_reserv_domain_lock(domain);
1318 				if (!rvn->inpartpopq)
1319 					goto again;
1320 				continue;
1321 			}
1322 		} else
1323 			vm_reserv_domain_unlock(domain);
1324 		if (pa < low) {
1325 			/* Start the search for free pages at "low". */
1326 			low_index = (low + PAGE_MASK - pa) >> PAGE_SHIFT;
1327 			i = low_index / NBPOPMAP;
1328 			hi = low_index % NBPOPMAP;
1329 		} else
1330 			i = hi = 0;
1331 		do {
1332 			/* Find the next free page. */
1333 			lo = ffsl(~(((1UL << hi) - 1) | rv->popmap[i]));
1334 			while (lo == 0 && ++i < NPOPMAP)
1335 				lo = ffsl(~rv->popmap[i]);
1336 			if (i == NPOPMAP)
1337 				break;
1338 			/* Convert from ffsl() to ordinary bit numbering. */
1339 			lo--;
1340 			next_free = NBPOPMAP * i + lo;
1341 			pa = VM_PAGE_TO_PHYS(&rv->pages[next_free]);
1342 			KASSERT(pa >= low,
1343 			    ("vm_reserv_reclaim_contig: pa is too low"));
1344 			if (pa + size > high) {
1345 				/* The rest of this reservation is too high. */
1346 				break;
1347 			} else if ((pa & (alignment - 1)) != 0 ||
1348 			    ((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0) {
1349 				/*
1350 				 * The current page doesn't meet the alignment
1351 				 * and/or boundary requirements.  Continue
1352 				 * searching this reservation until the rest
1353 				 * of its free pages are either excluded or
1354 				 * exhausted.
1355 				 */
1356 				hi = lo + 1;
1357 				if (hi >= NBPOPMAP) {
1358 					hi = 0;
1359 					i++;
1360 				}
1361 				continue;
1362 			}
1363 			/* Find the next used page. */
1364 			hi = ffsl(rv->popmap[i] & ~((1UL << lo) - 1));
1365 			while (hi == 0 && ++i < NPOPMAP) {
1366 				if ((NBPOPMAP * i - next_free) * PAGE_SIZE >=
1367 				    size) {
1368 					vm_reserv_reclaim(rv);
1369 					vm_reserv_unlock(rv);
1370 					return (TRUE);
1371 				}
1372 				hi = ffsl(rv->popmap[i]);
1373 			}
1374 			/* Convert from ffsl() to ordinary bit numbering. */
1375 			if (i != NPOPMAP)
1376 				hi--;
1377 			if ((NBPOPMAP * i + hi - next_free) * PAGE_SIZE >=
1378 			    size) {
1379 				vm_reserv_reclaim(rv);
1380 				vm_reserv_unlock(rv);
1381 				return (TRUE);
1382 			}
1383 		} while (i < NPOPMAP);
1384 		vm_reserv_unlock(rv);
1385 		vm_reserv_domain_lock(domain);
1386 		if (rvn != NULL && !rvn->inpartpopq)
1387 			goto again;
1388 	}
1389 	vm_reserv_domain_unlock(domain);
1390 	return (FALSE);
1391 }
1392 
1393 /*
1394  * Transfers the reservation underlying the given page to a new object.
1395  *
1396  * The object must be locked.
1397  */
1398 void
vm_reserv_rename(vm_page_t m,vm_object_t new_object,vm_object_t old_object,vm_pindex_t old_object_offset)1399 vm_reserv_rename(vm_page_t m, vm_object_t new_object, vm_object_t old_object,
1400     vm_pindex_t old_object_offset)
1401 {
1402 	vm_reserv_t rv;
1403 
1404 	VM_OBJECT_ASSERT_WLOCKED(new_object);
1405 	rv = vm_reserv_from_page(m);
1406 	if (rv->object == old_object) {
1407 		vm_reserv_lock(rv);
1408 		CTR6(KTR_VM,
1409 		    "%s: rv %p object %p new %p popcnt %d inpartpop %d",
1410 		    __FUNCTION__, rv, rv->object, new_object, rv->popcnt,
1411 		    rv->inpartpopq);
1412 		if (rv->object == old_object) {
1413 			vm_reserv_object_lock(old_object);
1414 			rv->object = NULL;
1415 			LIST_REMOVE(rv, objq);
1416 			vm_reserv_object_unlock(old_object);
1417 			vm_reserv_object_lock(new_object);
1418 			rv->object = new_object;
1419 			rv->pindex -= old_object_offset;
1420 			LIST_INSERT_HEAD(&new_object->rvq, rv, objq);
1421 			vm_reserv_object_unlock(new_object);
1422 		}
1423 		vm_reserv_unlock(rv);
1424 	}
1425 }
1426 
1427 /*
1428  * Returns the size (in bytes) of a reservation of the specified level.
1429  */
1430 int
vm_reserv_size(int level)1431 vm_reserv_size(int level)
1432 {
1433 
1434 	switch (level) {
1435 	case 0:
1436 		return (VM_LEVEL_0_SIZE);
1437 	case -1:
1438 		return (PAGE_SIZE);
1439 	default:
1440 		return (0);
1441 	}
1442 }
1443 
1444 /*
1445  * Allocates the virtual and physical memory required by the reservation
1446  * management system's data structures, in particular, the reservation array.
1447  */
1448 vm_paddr_t
vm_reserv_startup(vm_offset_t * vaddr,vm_paddr_t end,vm_paddr_t high_water)1449 vm_reserv_startup(vm_offset_t *vaddr, vm_paddr_t end, vm_paddr_t high_water)
1450 {
1451 	vm_paddr_t new_end;
1452 	size_t size;
1453 
1454 	/*
1455 	 * Calculate the size (in bytes) of the reservation array.  Round up
1456 	 * from "high_water" because every small page is mapped to an element
1457 	 * in the reservation array based on its physical address.  Thus, the
1458 	 * number of elements in the reservation array can be greater than the
1459 	 * number of superpages.
1460 	 */
1461 	size = howmany(high_water, VM_LEVEL_0_SIZE) * sizeof(struct vm_reserv);
1462 
1463 	/*
1464 	 * Allocate and map the physical memory for the reservation array.  The
1465 	 * next available virtual address is returned by reference.
1466 	 */
1467 	new_end = end - round_page(size);
1468 	vm_reserv_array = (void *)(uintptr_t)pmap_map(vaddr, new_end, end,
1469 	    VM_PROT_READ | VM_PROT_WRITE);
1470 	bzero(vm_reserv_array, size);
1471 
1472 	/*
1473 	 * Return the next available physical address.
1474 	 */
1475 	return (new_end);
1476 }
1477 
1478 /*
1479  * Initializes the reservation management system.  Specifically, initializes
1480  * the reservation counters.
1481  */
1482 static void
vm_reserv_counter_init(void * unused)1483 vm_reserv_counter_init(void *unused)
1484 {
1485 
1486 	vm_reserv_freed = counter_u64_alloc(M_WAITOK);
1487 	vm_reserv_broken = counter_u64_alloc(M_WAITOK);
1488 	vm_reserv_reclaimed = counter_u64_alloc(M_WAITOK);
1489 }
1490 SYSINIT(vm_reserv_counter_init, SI_SUB_CPU, SI_ORDER_ANY,
1491     vm_reserv_counter_init, NULL);
1492 
1493 /*
1494  * Returns the superpage containing the given page.
1495  */
1496 vm_page_t
vm_reserv_to_superpage(vm_page_t m)1497 vm_reserv_to_superpage(vm_page_t m)
1498 {
1499 	vm_reserv_t rv;
1500 
1501 	VM_OBJECT_ASSERT_LOCKED(m->object);
1502 	rv = vm_reserv_from_page(m);
1503 	if (rv->object == m->object && rv->popcnt == VM_LEVEL_0_NPAGES)
1504 		m = rv->pages;
1505 	else
1506 		m = NULL;
1507 
1508 	return (m);
1509 }
1510 
1511 #endif	/* VM_NRESERVLEVEL > 0 */
1512