2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002-2006 Rice University
5 * Copyright (c) 2007-2011 Alan L. Cox <alc@cs.rice.edu>
8 * This software was developed for the FreeBSD Project by Alan L. Cox,
9 * Olivier Crameri, Peter Druschel, Sitaram Iyer, and Juan Navarro.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
35 * Superpage reservation management module
37 * Any external functions defined by this module are only to be used by the
38 * virtual memory system.
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/mutex.h>
51 #include <sys/queue.h>
52 #include <sys/rwlock.h>
54 #include <sys/sysctl.h>
55 #include <sys/systm.h>
56 #include <sys/counter.h>
58 #include <sys/vmmeter.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>
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.
78 #if VM_NRESERVLEVEL > 0
80 #ifndef VM_LEVEL_0_ORDER_MAX
81 #define VM_LEVEL_0_ORDER_MAX VM_LEVEL_0_ORDER
85 * The number of small pages that are contained in a level 0 reservation
87 #define VM_LEVEL_0_NPAGES (1 << VM_LEVEL_0_ORDER)
88 #define VM_LEVEL_0_NPAGES_MAX (1 << VM_LEVEL_0_ORDER_MAX)
91 * The number of bits by which a physical address is shifted to obtain the
94 #define VM_LEVEL_0_SHIFT (VM_LEVEL_0_ORDER + PAGE_SHIFT)
97 * The size of a level 0 reservation in bytes
99 #define VM_LEVEL_0_SIZE (1 << VM_LEVEL_0_SHIFT)
102 * Computes the index of the small page underlying the given (object, pindex)
103 * within the reservation's array of small pages.
105 #define VM_RESERV_INDEX(object, pindex) \
106 (((object)->pg_color + (pindex)) & (VM_LEVEL_0_NPAGES - 1))
109 * The size of a population map entry
111 typedef u_long popmap_t;
114 * The number of bits in a population map entry
116 #define NBPOPMAP (NBBY * sizeof(popmap_t))
119 * The number of population map entries in a reservation
121 #define NPOPMAP howmany(VM_LEVEL_0_NPAGES, NBPOPMAP)
122 #define NPOPMAP_MAX howmany(VM_LEVEL_0_NPAGES_MAX, NBPOPMAP)
125 * Number of elapsed ticks before we update the LRU queue position. Used
126 * to reduce contention and churn on the list.
128 #define PARTPOPSLOP 1
131 * Clear a bit in the population map.
134 popmap_clear(popmap_t popmap[], int i)
137 popmap[i / NBPOPMAP] &= ~(1UL << (i % NBPOPMAP));
141 * Set a bit in the population map.
144 popmap_set(popmap_t popmap[], int i)
147 popmap[i / NBPOPMAP] |= 1UL << (i % NBPOPMAP);
151 * Is a bit in the population map clear?
153 static __inline boolean_t
154 popmap_is_clear(popmap_t popmap[], int i)
157 return ((popmap[i / NBPOPMAP] & (1UL << (i % NBPOPMAP))) == 0);
161 * Is a bit in the population map set?
163 static __inline boolean_t
164 popmap_is_set(popmap_t popmap[], int i)
167 return ((popmap[i / NBPOPMAP] & (1UL << (i % NBPOPMAP))) != 0);
171 * The reservation structure
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.
182 * A partially populated reservation can be broken and reclaimed at any time.
185 * d - vm_reserv_domain_lock
186 * o - vm_reserv_object_lock
187 * c - constant after boot
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 */
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))
210 static struct mtx_padalign vm_reserv_domain_locks[MAXMEMDOM];
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))
217 * The reservation array
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
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.
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.
237 static vm_reserv_t vm_reserv_array;
240 * The partially populated reservation queue
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.
246 * Access to this queue is synchronized by the free page queue lock.
248 static TAILQ_HEAD(, vm_reserv) vm_rvq_partpop[MAXMEMDOM];
250 static SYSCTL_NODE(_vm, OID_AUTO, reserv, CTLFLAG_RD, 0, "Reservation Info");
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");
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");
260 static int sysctl_vm_reserv_fullpop(SYSCTL_HANDLER_ARGS);
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");
265 static int sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS);
267 SYSCTL_OID(_vm_reserv, OID_AUTO, partpopq, CTLTYPE_STRING | CTLFLAG_RD, NULL, 0,
268 sysctl_vm_reserv_partpopq, "A", "Partially populated reservation queues");
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");
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.
278 * The "hash" function could be made faster without divide and modulo.
280 #define VM_RESERV_OBJ_LOCK_COUNT MAXCPU
282 struct mtx_padalign vm_reserv_object_mtx[VM_RESERV_OBJ_LOCK_COUNT];
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)))
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,
298 static void vm_reserv_populate(vm_reserv_t rv, int index);
299 static void vm_reserv_reclaim(vm_reserv_t rv);
302 * Returns the current number of full reservations.
304 * Since the number of full reservations is computed without acquiring the
305 * free page queue lock, the returned value may be inexact.
308 sysctl_vm_reserv_fullpop(SYSCTL_HANDLER_ARGS)
311 struct vm_phys_seg *seg;
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 <= seg->end) {
320 rv = &vm_reserv_array[paddr >> VM_LEVEL_0_SHIFT];
321 fullpop += rv->popcnt == VM_LEVEL_0_NPAGES;
322 paddr += VM_LEVEL_0_SIZE;
325 return (sysctl_handle_int(oidp, &fullpop, 0, req));
329 * Describes the current state of the partially populated reservation queue.
332 sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS)
336 int counter, error, domain, level, unused_pages;
338 error = sysctl_wire_old_buffer(req, 0);
341 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
342 sbuf_printf(&sbuf, "\nDOMAIN LEVEL SIZE NUMBER\n\n");
343 for (domain = 0; domain < vm_ndomains; domain++) {
344 for (level = -1; level <= VM_NRESERVLEVEL - 2; level++) {
347 vm_reserv_domain_lock(domain);
348 TAILQ_FOREACH(rv, &vm_rvq_partpop[domain], partpopq) {
350 unused_pages += VM_LEVEL_0_NPAGES - rv->popcnt;
352 vm_reserv_domain_unlock(domain);
353 sbuf_printf(&sbuf, "%6d, %7d, %6dK, %6d\n",
355 unused_pages * ((int)PAGE_SIZE / 1024), counter);
358 error = sbuf_finish(&sbuf);
364 * Remove a reservation from the object's objq.
367 vm_reserv_remove(vm_reserv_t rv)
371 vm_reserv_assert_locked(rv);
372 CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d",
373 __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq);
374 KASSERT(rv->object != NULL,
375 ("vm_reserv_remove: reserv %p is free", rv));
376 KASSERT(!rv->inpartpopq,
377 ("vm_reserv_remove: reserv %p's inpartpopq is TRUE", rv));
379 vm_reserv_object_lock(object);
380 LIST_REMOVE(rv, objq);
382 vm_reserv_object_unlock(object);
386 * Insert a new reservation into the object's objq.
389 vm_reserv_insert(vm_reserv_t rv, vm_object_t object, vm_pindex_t pindex)
393 vm_reserv_assert_locked(rv);
395 "%s: rv %p(%p) object %p new %p popcnt %d",
396 __FUNCTION__, rv, rv->pages, rv->object, object,
398 KASSERT(rv->object == NULL,
399 ("vm_reserv_insert: reserv %p isn't free", rv));
400 KASSERT(rv->popcnt == 0,
401 ("vm_reserv_insert: reserv %p's popcnt is corrupted", rv));
402 KASSERT(!rv->inpartpopq,
403 ("vm_reserv_insert: reserv %p's inpartpopq is TRUE", rv));
404 for (i = 0; i < NPOPMAP; i++)
405 KASSERT(rv->popmap[i] == 0,
406 ("vm_reserv_insert: reserv %p's popmap is corrupted", rv));
407 vm_reserv_object_lock(object);
410 rv->lasttick = ticks;
411 LIST_INSERT_HEAD(&object->rvq, rv, objq);
412 vm_reserv_object_unlock(object);
416 * Reduces the given reservation's population count. If the population count
417 * becomes zero, the reservation is destroyed. Additionally, moves the
418 * reservation to the tail of the partially populated reservation queue if the
419 * population count is non-zero.
422 vm_reserv_depopulate(vm_reserv_t rv, int index)
424 struct vm_domain *vmd;
426 vm_reserv_assert_locked(rv);
427 CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d",
428 __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq);
429 KASSERT(rv->object != NULL,
430 ("vm_reserv_depopulate: reserv %p is free", rv));
431 KASSERT(popmap_is_set(rv->popmap, index),
432 ("vm_reserv_depopulate: reserv %p's popmap[%d] is clear", rv,
434 KASSERT(rv->popcnt > 0,
435 ("vm_reserv_depopulate: reserv %p's popcnt is corrupted", rv));
436 KASSERT(rv->domain < vm_ndomains,
437 ("vm_reserv_depopulate: reserv %p's domain is corrupted %d",
439 if (rv->popcnt == VM_LEVEL_0_NPAGES) {
440 KASSERT(rv->pages->psind == 1,
441 ("vm_reserv_depopulate: reserv %p is already demoted",
443 rv->pages->psind = 0;
445 popmap_clear(rv->popmap, index);
447 if ((unsigned)(ticks - rv->lasttick) >= PARTPOPSLOP ||
449 vm_reserv_domain_lock(rv->domain);
450 if (rv->inpartpopq) {
451 TAILQ_REMOVE(&vm_rvq_partpop[rv->domain], rv, partpopq);
452 rv->inpartpopq = FALSE;
454 if (rv->popcnt != 0) {
455 rv->inpartpopq = TRUE;
456 TAILQ_INSERT_TAIL(&vm_rvq_partpop[rv->domain], rv, partpopq);
458 vm_reserv_domain_unlock(rv->domain);
459 rv->lasttick = ticks;
461 vmd = VM_DOMAIN(rv->domain);
462 if (rv->popcnt == 0) {
463 vm_reserv_remove(rv);
464 vm_domain_free_lock(vmd);
465 vm_phys_free_pages(rv->pages, VM_LEVEL_0_ORDER);
466 vm_domain_free_unlock(vmd);
467 counter_u64_add(vm_reserv_freed, 1);
469 vm_domain_freecnt_inc(vmd, 1);
473 * Returns the reservation to which the given page might belong.
475 static __inline vm_reserv_t
476 vm_reserv_from_page(vm_page_t m)
479 return (&vm_reserv_array[VM_PAGE_TO_PHYS(m) >> VM_LEVEL_0_SHIFT]);
483 * Returns an existing reservation or NULL and initialized successor pointer.
486 vm_reserv_from_object(vm_object_t object, vm_pindex_t pindex,
487 vm_page_t mpred, vm_page_t *msuccp)
494 KASSERT(mpred->object == object,
495 ("vm_reserv_from_object: object doesn't contain mpred"));
496 KASSERT(mpred->pindex < pindex,
497 ("vm_reserv_from_object: mpred doesn't precede pindex"));
498 rv = vm_reserv_from_page(mpred);
499 if (rv->object == object && vm_reserv_has_pindex(rv, pindex))
501 msucc = TAILQ_NEXT(mpred, listq);
503 msucc = TAILQ_FIRST(&object->memq);
505 KASSERT(msucc->pindex > pindex,
506 ("vm_reserv_from_object: msucc doesn't succeed pindex"));
507 rv = vm_reserv_from_page(msucc);
508 if (rv->object == object && vm_reserv_has_pindex(rv, pindex))
520 * Returns TRUE if the given reservation contains the given page index and
523 static __inline boolean_t
524 vm_reserv_has_pindex(vm_reserv_t rv, vm_pindex_t pindex)
527 return (((pindex - rv->pindex) & ~(VM_LEVEL_0_NPAGES - 1)) == 0);
531 * Increases the given reservation's population count. Moves the reservation
532 * to the tail of the partially populated reservation queue.
534 * The free page queue must be locked.
537 vm_reserv_populate(vm_reserv_t rv, int index)
540 vm_reserv_assert_locked(rv);
541 CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d",
542 __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq);
543 KASSERT(rv->object != NULL,
544 ("vm_reserv_populate: reserv %p is free", rv));
545 KASSERT(popmap_is_clear(rv->popmap, index),
546 ("vm_reserv_populate: reserv %p's popmap[%d] is set", rv,
548 KASSERT(rv->popcnt < VM_LEVEL_0_NPAGES,
549 ("vm_reserv_populate: reserv %p is already full", rv));
550 KASSERT(rv->pages->psind == 0,
551 ("vm_reserv_populate: reserv %p is already promoted", rv));
552 KASSERT(rv->domain < vm_ndomains,
553 ("vm_reserv_populate: reserv %p's domain is corrupted %d",
555 popmap_set(rv->popmap, index);
557 if ((unsigned)(ticks - rv->lasttick) < PARTPOPSLOP &&
558 rv->inpartpopq && rv->popcnt != VM_LEVEL_0_NPAGES)
560 rv->lasttick = ticks;
561 vm_reserv_domain_lock(rv->domain);
562 if (rv->inpartpopq) {
563 TAILQ_REMOVE(&vm_rvq_partpop[rv->domain], rv, partpopq);
564 rv->inpartpopq = FALSE;
566 if (rv->popcnt < VM_LEVEL_0_NPAGES) {
567 rv->inpartpopq = TRUE;
568 TAILQ_INSERT_TAIL(&vm_rvq_partpop[rv->domain], rv, partpopq);
570 KASSERT(rv->pages->psind == 0,
571 ("vm_reserv_populate: reserv %p is already promoted",
573 rv->pages->psind = 1;
575 vm_reserv_domain_unlock(rv->domain);
579 * Allocates a contiguous set of physical pages of the given size "npages"
580 * from existing or newly created reservations. All of the physical pages
581 * must be at or above the given physical address "low" and below the given
582 * physical address "high". The given value "alignment" determines the
583 * alignment of the first physical page in the set. If the given value
584 * "boundary" is non-zero, then the set of physical pages cannot cross any
585 * physical address boundary that is a multiple of that value. Both
586 * "alignment" and "boundary" must be a power of two.
588 * The page "mpred" must immediately precede the offset "pindex" within the
591 * The object must be locked.
594 vm_reserv_alloc_contig(vm_object_t object, vm_pindex_t pindex, int domain,
595 int req, vm_page_t mpred, u_long npages, vm_paddr_t low, vm_paddr_t high,
596 u_long alignment, vm_paddr_t boundary)
598 struct vm_domain *vmd;
600 vm_page_t m, m_ret, msucc;
601 vm_pindex_t first, leftcap, rightcap;
603 u_long allocpages, maxpages, minpages;
606 VM_OBJECT_ASSERT_WLOCKED(object);
607 KASSERT(npages != 0, ("vm_reserv_alloc_contig: npages is 0"));
610 * Is a reservation fundamentally impossible?
612 if (pindex < VM_RESERV_INDEX(object, pindex) ||
613 pindex + npages > object->size)
617 * All reservations of a particular size have the same alignment.
618 * Assuming that the first page is allocated from a reservation, the
619 * least significant bits of its physical address can be determined
620 * from its offset from the beginning of the reservation and the size
621 * of the reservation.
623 * Could the specified index within a reservation of the smallest
624 * possible size satisfy the alignment and boundary requirements?
626 pa = VM_RESERV_INDEX(object, pindex) << PAGE_SHIFT;
627 if ((pa & (alignment - 1)) != 0)
629 size = npages << PAGE_SHIFT;
630 if (((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0)
634 * Look for an existing reservation.
636 rv = vm_reserv_from_object(object, pindex, mpred, &msucc);
638 KASSERT(object != kernel_object || rv->domain == domain,
639 ("vm_reserv_alloc_contig: domain mismatch"));
640 index = VM_RESERV_INDEX(object, pindex);
641 /* Does the allocation fit within the reservation? */
642 if (index + npages > VM_LEVEL_0_NPAGES)
645 vmd = VM_DOMAIN(domain);
647 /* Handle reclaim race. */
648 if (rv->object != object)
650 m = &rv->pages[index];
651 pa = VM_PAGE_TO_PHYS(m);
652 if (pa < low || pa + size > high ||
653 (pa & (alignment - 1)) != 0 ||
654 ((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0)
656 /* Handle vm_page_rename(m, new_object, ...). */
657 for (i = 0; i < npages; i++)
658 if (popmap_is_set(rv->popmap, index + i))
660 if (!vm_domain_allocate(vmd, req, npages))
662 for (i = 0; i < npages; i++)
663 vm_reserv_populate(rv, index + i);
664 vm_reserv_unlock(rv);
667 vm_reserv_unlock(rv);
672 * Could at least one reservation fit between the first index to the
673 * left that can be used ("leftcap") and the first index to the right
674 * that cannot be used ("rightcap")?
676 * We must synchronize with the reserv object lock to protect the
677 * pindex/object of the resulting reservations against rename while
680 first = pindex - VM_RESERV_INDEX(object, pindex);
681 minpages = VM_RESERV_INDEX(object, pindex) + npages;
682 maxpages = roundup2(minpages, VM_LEVEL_0_NPAGES);
683 allocpages = maxpages;
684 vm_reserv_object_lock(object);
686 if ((rv = vm_reserv_from_page(mpred))->object != object)
687 leftcap = mpred->pindex + 1;
689 leftcap = rv->pindex + VM_LEVEL_0_NPAGES;
690 if (leftcap > first) {
691 vm_reserv_object_unlock(object);
696 if ((rv = vm_reserv_from_page(msucc))->object != object)
697 rightcap = msucc->pindex;
699 rightcap = rv->pindex;
700 if (first + maxpages > rightcap) {
701 if (maxpages == VM_LEVEL_0_NPAGES) {
702 vm_reserv_object_unlock(object);
707 * At least one reservation will fit between "leftcap"
708 * and "rightcap". However, a reservation for the
709 * last of the requested pages will not fit. Reduce
710 * the size of the upcoming allocation accordingly.
712 allocpages = minpages;
715 vm_reserv_object_unlock(object);
718 * Would the last new reservation extend past the end of the object?
720 if (first + maxpages > object->size) {
722 * Don't allocate the last new reservation if the object is a
723 * vnode or backed by another object that is a vnode.
725 if (object->type == OBJT_VNODE ||
726 (object->backing_object != NULL &&
727 object->backing_object->type == OBJT_VNODE)) {
728 if (maxpages == VM_LEVEL_0_NPAGES)
730 allocpages = minpages;
732 /* Speculate that the object may grow. */
736 * Allocate the physical pages. The alignment and boundary specified
737 * for this allocation may be different from the alignment and
738 * boundary specified for the requested pages. For instance, the
739 * specified index may not be the first page within the first new
743 vmd = VM_DOMAIN(domain);
744 if (vm_domain_allocate(vmd, req, npages)) {
745 vm_domain_free_lock(vmd);
746 m = vm_phys_alloc_contig(domain, allocpages, low, high,
747 ulmax(alignment, VM_LEVEL_0_SIZE),
748 boundary > VM_LEVEL_0_SIZE ? boundary : 0);
749 vm_domain_free_unlock(vmd);
751 vm_domain_freecnt_inc(vmd, npages);
756 KASSERT(vm_phys_domain(m) == domain,
757 ("vm_reserv_alloc_contig: Page domain does not match requested."));
760 * The allocated physical pages always begin at a reservation
761 * boundary, but they do not always end at a reservation boundary.
762 * Initialize every reservation that is completely covered by the
763 * allocated physical pages.
766 index = VM_RESERV_INDEX(object, pindex);
768 rv = vm_reserv_from_page(m);
769 KASSERT(rv->pages == m,
770 ("vm_reserv_alloc_contig: reserv %p's pages is corrupted",
773 vm_reserv_insert(rv, object, first);
774 n = ulmin(VM_LEVEL_0_NPAGES - index, npages);
775 for (i = 0; i < n; i++)
776 vm_reserv_populate(rv, index + i);
779 m_ret = &rv->pages[index];
782 vm_reserv_unlock(rv);
783 m += VM_LEVEL_0_NPAGES;
784 first += VM_LEVEL_0_NPAGES;
785 allocpages -= VM_LEVEL_0_NPAGES;
786 } while (allocpages >= VM_LEVEL_0_NPAGES);
791 * Allocate a physical page from an existing or newly created reservation.
793 * The page "mpred" must immediately precede the offset "pindex" within the
796 * The object must be locked.
799 vm_reserv_alloc_page(vm_object_t object, vm_pindex_t pindex, int domain,
800 int req, vm_page_t mpred)
802 struct vm_domain *vmd;
804 vm_pindex_t first, leftcap, rightcap;
808 VM_OBJECT_ASSERT_WLOCKED(object);
811 * Is a reservation fundamentally impossible?
813 if (pindex < VM_RESERV_INDEX(object, pindex) ||
814 pindex >= object->size)
818 * Look for an existing reservation.
820 rv = vm_reserv_from_object(object, pindex, mpred, &msucc);
822 KASSERT(object != kernel_object || rv->domain == domain,
823 ("vm_reserv_alloc_page: domain mismatch"));
825 vmd = VM_DOMAIN(domain);
826 index = VM_RESERV_INDEX(object, pindex);
827 m = &rv->pages[index];
829 /* Handle reclaim race. */
830 if (rv->object != object ||
831 /* Handle vm_page_rename(m, new_object, ...). */
832 popmap_is_set(rv->popmap, index)) {
836 if (vm_domain_allocate(vmd, req, 1) == 0)
839 vm_reserv_populate(rv, index);
841 vm_reserv_unlock(rv);
846 * Could a reservation fit between the first index to the left that
847 * can be used and the first index to the right that cannot be used?
849 * We must synchronize with the reserv object lock to protect the
850 * pindex/object of the resulting reservations against rename while
853 first = pindex - VM_RESERV_INDEX(object, pindex);
854 vm_reserv_object_lock(object);
856 if ((rv = vm_reserv_from_page(mpred))->object != object)
857 leftcap = mpred->pindex + 1;
859 leftcap = rv->pindex + VM_LEVEL_0_NPAGES;
860 if (leftcap > first) {
861 vm_reserv_object_unlock(object);
866 if ((rv = vm_reserv_from_page(msucc))->object != object)
867 rightcap = msucc->pindex;
869 rightcap = rv->pindex;
870 if (first + VM_LEVEL_0_NPAGES > rightcap) {
871 vm_reserv_object_unlock(object);
875 vm_reserv_object_unlock(object);
878 * Would a new reservation extend past the end of the object?
880 if (first + VM_LEVEL_0_NPAGES > object->size) {
882 * Don't allocate a new reservation if the object is a vnode or
883 * backed by another object that is a vnode.
885 if (object->type == OBJT_VNODE ||
886 (object->backing_object != NULL &&
887 object->backing_object->type == OBJT_VNODE))
889 /* Speculate that the object may grow. */
893 * Allocate and populate the new reservation.
896 vmd = VM_DOMAIN(domain);
897 if (vm_domain_allocate(vmd, req, 1)) {
898 vm_domain_free_lock(vmd);
899 m = vm_phys_alloc_pages(domain, VM_FREEPOOL_DEFAULT,
901 vm_domain_free_unlock(vmd);
903 vm_domain_freecnt_inc(vmd, 1);
908 rv = vm_reserv_from_page(m);
910 KASSERT(rv->pages == m,
911 ("vm_reserv_alloc_page: reserv %p's pages is corrupted", rv));
912 vm_reserv_insert(rv, object, first);
913 index = VM_RESERV_INDEX(object, pindex);
914 vm_reserv_populate(rv, index);
915 vm_reserv_unlock(rv);
917 return (&rv->pages[index]);
921 * Breaks the given reservation. All free pages in the reservation
922 * are returned to the physical memory allocator. The reservation's
923 * population count and map are reset to their initial state.
925 * The given reservation must not be in the partially populated reservation
926 * queue. The free page queue lock must be held.
929 vm_reserv_break(vm_reserv_t rv)
932 int bitpos, hi, i, lo;
934 vm_reserv_assert_locked(rv);
935 CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d",
936 __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq);
937 vm_reserv_remove(rv);
938 rv->pages->psind = 0;
940 for (i = 0; i <= NPOPMAP; i++) {
942 * "changes" is a bitmask that marks where a new sequence of
943 * 0s or 1s begins in popmap[i], with last bit in popmap[i-1]
944 * considered to be 1 if and only if lo == hi. The bits of
945 * popmap[-1] and popmap[NPOPMAP] are considered all 1s.
950 changes = rv->popmap[i];
951 changes ^= (changes << 1) | (lo == hi);
954 while (changes != 0) {
956 * If the next change marked begins a run of 0s, set
957 * lo to mark that position. Otherwise set hi and
958 * free pages from lo up to hi.
960 bitpos = ffsl(changes) - 1;
961 changes ^= 1UL << bitpos;
963 lo = NBPOPMAP * i + bitpos;
965 hi = NBPOPMAP * i + bitpos;
966 vm_domain_free_lock(VM_DOMAIN(rv->domain));
967 vm_phys_enqueue_contig(&rv->pages[lo], hi - lo);
968 vm_domain_free_unlock(VM_DOMAIN(rv->domain));
974 counter_u64_add(vm_reserv_broken, 1);
978 * Breaks all reservations belonging to the given object.
981 vm_reserv_break_all(vm_object_t object)
986 * This access of object->rvq is unsynchronized so that the
987 * object rvq lock can nest after the domain_free lock. We
988 * must check for races in the results. However, the object
989 * lock prevents new additions, so we are guaranteed that when
990 * it returns NULL the object is properly empty.
992 while ((rv = LIST_FIRST(&object->rvq)) != NULL) {
995 if (rv->object != object) {
996 vm_reserv_unlock(rv);
999 vm_reserv_domain_lock(rv->domain);
1000 if (rv->inpartpopq) {
1001 TAILQ_REMOVE(&vm_rvq_partpop[rv->domain], rv, partpopq);
1002 rv->inpartpopq = FALSE;
1004 vm_reserv_domain_unlock(rv->domain);
1005 vm_reserv_break(rv);
1006 vm_reserv_unlock(rv);
1011 * Frees the given page if it belongs to a reservation. Returns TRUE if the
1012 * page is freed and FALSE otherwise.
1014 * The free page queue lock must be held.
1017 vm_reserv_free_page(vm_page_t m)
1022 rv = vm_reserv_from_page(m);
1023 if (rv->object == NULL)
1026 /* Re-validate after lock. */
1027 if (rv->object != NULL) {
1028 vm_reserv_depopulate(rv, m - rv->pages);
1032 vm_reserv_unlock(rv);
1038 * Initializes the reservation management system. Specifically, initializes
1039 * the reservation array.
1041 * Requires that vm_page_array and first_page are initialized!
1044 vm_reserv_init(void)
1047 struct vm_phys_seg *seg;
1048 struct vm_reserv *rv;
1052 * Initialize the reservation array. Specifically, initialize the
1053 * "pages" field for every element that has an underlying superpage.
1055 for (segind = 0; segind < vm_phys_nsegs; segind++) {
1056 seg = &vm_phys_segs[segind];
1057 paddr = roundup2(seg->start, VM_LEVEL_0_SIZE);
1058 while (paddr + VM_LEVEL_0_SIZE <= seg->end) {
1059 rv = &vm_reserv_array[paddr >> VM_LEVEL_0_SHIFT];
1060 rv->pages = PHYS_TO_VM_PAGE(paddr);
1061 rv->domain = seg->domain;
1062 mtx_init(&rv->lock, "vm reserv", NULL, MTX_DEF);
1063 paddr += VM_LEVEL_0_SIZE;
1066 for (i = 0; i < MAXMEMDOM; i++) {
1067 mtx_init(&vm_reserv_domain_locks[i], "VM reserv domain", NULL,
1069 TAILQ_INIT(&vm_rvq_partpop[i]);
1072 for (i = 0; i < VM_RESERV_OBJ_LOCK_COUNT; i++)
1073 mtx_init(&vm_reserv_object_mtx[i], "resv obj lock", NULL,
1078 * Returns true if the given page belongs to a reservation and that page is
1079 * free. Otherwise, returns false.
1082 vm_reserv_is_page_free(vm_page_t m)
1086 rv = vm_reserv_from_page(m);
1087 if (rv->object == NULL)
1089 return (popmap_is_clear(rv->popmap, m - rv->pages));
1093 * If the given page belongs to a reservation, returns the level of that
1094 * reservation. Otherwise, returns -1.
1097 vm_reserv_level(vm_page_t m)
1101 rv = vm_reserv_from_page(m);
1102 return (rv->object != NULL ? 0 : -1);
1106 * Returns a reservation level if the given page belongs to a fully populated
1107 * reservation and -1 otherwise.
1110 vm_reserv_level_iffullpop(vm_page_t m)
1114 rv = vm_reserv_from_page(m);
1115 return (rv->popcnt == VM_LEVEL_0_NPAGES ? 0 : -1);
1119 * Breaks the given partially populated reservation, releasing its free pages
1120 * to the physical memory allocator.
1122 * The free page queue lock must be held.
1125 vm_reserv_reclaim(vm_reserv_t rv)
1128 vm_reserv_assert_locked(rv);
1129 CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d",
1130 __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq);
1131 vm_reserv_domain_lock(rv->domain);
1132 KASSERT(rv->inpartpopq,
1133 ("vm_reserv_reclaim: reserv %p's inpartpopq is FALSE", rv));
1134 KASSERT(rv->domain < vm_ndomains,
1135 ("vm_reserv_reclaim: reserv %p's domain is corrupted %d",
1137 TAILQ_REMOVE(&vm_rvq_partpop[rv->domain], rv, partpopq);
1138 rv->inpartpopq = FALSE;
1139 vm_reserv_domain_unlock(rv->domain);
1140 vm_reserv_break(rv);
1141 counter_u64_add(vm_reserv_reclaimed, 1);
1145 * Breaks the reservation at the head of the partially populated reservation
1146 * queue, releasing its free pages to the physical memory allocator. Returns
1147 * TRUE if a reservation is broken and FALSE otherwise.
1149 * The free page queue lock must be held.
1152 vm_reserv_reclaim_inactive(int domain)
1156 while ((rv = TAILQ_FIRST(&vm_rvq_partpop[domain])) != NULL) {
1158 if (rv != TAILQ_FIRST(&vm_rvq_partpop[domain])) {
1159 vm_reserv_unlock(rv);
1162 vm_reserv_reclaim(rv);
1163 vm_reserv_unlock(rv);
1170 * Determine whether this reservation has free pages that satisfy the given
1171 * request for contiguous physical memory. Start searching from the lower
1172 * bound, defined by low_index.
1174 * The free page queue lock must be held.
1177 vm_reserv_test_contig(vm_reserv_t rv, u_long npages, vm_paddr_t low,
1178 vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
1180 vm_paddr_t pa, size;
1182 int bitpos, bits_left, i, hi, lo, n;
1184 vm_reserv_assert_locked(rv);
1185 size = npages << PAGE_SHIFT;
1186 pa = VM_PAGE_TO_PHYS(&rv->pages[0]);
1188 ((low + PAGE_MASK - pa) >> PAGE_SHIFT) : 0;
1190 changes = rv->popmap[i] | ((1UL << (lo % NBPOPMAP)) - 1);
1191 hi = (pa + VM_LEVEL_0_SIZE > high) ?
1192 ((high + PAGE_MASK - pa) >> PAGE_SHIFT) : VM_LEVEL_0_NPAGES;
1194 bits_left = hi % NBPOPMAP;
1198 * "changes" is a bitmask that marks where a new sequence of
1199 * 0s or 1s begins in popmap[i], with last bit in popmap[i-1]
1200 * considered to be 1 if and only if lo == hi. The bits of
1201 * popmap[-1] and popmap[NPOPMAP] are considered all 1s.
1203 changes ^= (changes << 1) | (lo == hi);
1204 while (changes != 0) {
1206 * If the next change marked begins a run of 0s, set
1207 * lo to mark that position. Otherwise set hi and
1208 * look for a satisfactory first page from lo up to hi.
1210 bitpos = ffsl(changes) - 1;
1211 changes ^= 1UL << bitpos;
1213 lo = NBPOPMAP * i + bitpos;
1216 hi = NBPOPMAP * i + bitpos;
1217 pa = VM_PAGE_TO_PHYS(&rv->pages[lo]);
1218 if ((pa & (alignment - 1)) != 0) {
1219 /* Skip to next aligned page. */
1220 lo += (((pa - 1) | (alignment - 1)) + 1) >>
1222 if (lo >= VM_LEVEL_0_NPAGES)
1224 pa = VM_PAGE_TO_PHYS(&rv->pages[lo]);
1226 if (((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0) {
1227 /* Skip to next boundary-matching page. */
1228 lo += (((pa - 1) | (boundary - 1)) + 1) >>
1230 if (lo >= VM_LEVEL_0_NPAGES)
1232 pa = VM_PAGE_TO_PHYS(&rv->pages[lo]);
1234 if (lo * PAGE_SIZE + size <= hi * PAGE_SIZE)
1239 changes = rv->popmap[i];
1241 changes = bits_left == 0 ? -1UL :
1242 (rv->popmap[n] | (-1UL << bits_left));
1249 * Searches the partially populated reservation queue for the least recently
1250 * changed reservation with free pages that satisfy the given request for
1251 * contiguous physical memory. If a satisfactory reservation is found, it is
1252 * broken. Returns true if a reservation is broken and false otherwise.
1254 * The free page queue lock must be held.
1257 vm_reserv_reclaim_contig(int domain, u_long npages, vm_paddr_t low,
1258 vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
1260 vm_paddr_t pa, size;
1261 vm_reserv_t rv, rvn;
1263 if (npages > VM_LEVEL_0_NPAGES - 1)
1265 size = npages << PAGE_SHIFT;
1266 vm_reserv_domain_lock(domain);
1268 for (rv = TAILQ_FIRST(&vm_rvq_partpop[domain]); rv != NULL; rv = rvn) {
1269 rvn = TAILQ_NEXT(rv, partpopq);
1270 pa = VM_PAGE_TO_PHYS(&rv->pages[0]);
1271 if (pa + VM_LEVEL_0_SIZE - size < low) {
1272 /* This entire reservation is too low; go to next. */
1275 if (pa + size > high) {
1276 /* This entire reservation is too high; go to next. */
1279 if (vm_reserv_trylock(rv) == 0) {
1280 vm_reserv_domain_unlock(domain);
1282 if (!rv->inpartpopq) {
1283 vm_reserv_domain_lock(domain);
1284 if (!rvn->inpartpopq)
1289 vm_reserv_domain_unlock(domain);
1290 if (vm_reserv_test_contig(rv, npages, low, high,
1291 alignment, boundary)) {
1292 vm_reserv_reclaim(rv);
1293 vm_reserv_unlock(rv);
1296 vm_reserv_unlock(rv);
1297 vm_reserv_domain_lock(domain);
1298 if (rvn != NULL && !rvn->inpartpopq)
1301 vm_reserv_domain_unlock(domain);
1306 * Transfers the reservation underlying the given page to a new object.
1308 * The object must be locked.
1311 vm_reserv_rename(vm_page_t m, vm_object_t new_object, vm_object_t old_object,
1312 vm_pindex_t old_object_offset)
1316 VM_OBJECT_ASSERT_WLOCKED(new_object);
1317 rv = vm_reserv_from_page(m);
1318 if (rv->object == old_object) {
1321 "%s: rv %p object %p new %p popcnt %d inpartpop %d",
1322 __FUNCTION__, rv, rv->object, new_object, rv->popcnt,
1324 if (rv->object == old_object) {
1325 vm_reserv_object_lock(old_object);
1327 LIST_REMOVE(rv, objq);
1328 vm_reserv_object_unlock(old_object);
1329 vm_reserv_object_lock(new_object);
1330 rv->object = new_object;
1331 rv->pindex -= old_object_offset;
1332 LIST_INSERT_HEAD(&new_object->rvq, rv, objq);
1333 vm_reserv_object_unlock(new_object);
1335 vm_reserv_unlock(rv);
1340 * Returns the size (in bytes) of a reservation of the specified level.
1343 vm_reserv_size(int level)
1348 return (VM_LEVEL_0_SIZE);
1357 * Allocates the virtual and physical memory required by the reservation
1358 * management system's data structures, in particular, the reservation array.
1361 vm_reserv_startup(vm_offset_t *vaddr, vm_paddr_t end, vm_paddr_t high_water)
1367 * Calculate the size (in bytes) of the reservation array. Round up
1368 * from "high_water" because every small page is mapped to an element
1369 * in the reservation array based on its physical address. Thus, the
1370 * number of elements in the reservation array can be greater than the
1371 * number of superpages.
1373 size = howmany(high_water, VM_LEVEL_0_SIZE) * sizeof(struct vm_reserv);
1376 * Allocate and map the physical memory for the reservation array. The
1377 * next available virtual address is returned by reference.
1379 new_end = end - round_page(size);
1380 vm_reserv_array = (void *)(uintptr_t)pmap_map(vaddr, new_end, end,
1381 VM_PROT_READ | VM_PROT_WRITE);
1382 bzero(vm_reserv_array, size);
1385 * Return the next available physical address.
1391 * Initializes the reservation management system. Specifically, initializes
1392 * the reservation counters.
1395 vm_reserv_counter_init(void *unused)
1398 vm_reserv_freed = counter_u64_alloc(M_WAITOK);
1399 vm_reserv_broken = counter_u64_alloc(M_WAITOK);
1400 vm_reserv_reclaimed = counter_u64_alloc(M_WAITOK);
1402 SYSINIT(vm_reserv_counter_init, SI_SUB_CPU, SI_ORDER_ANY,
1403 vm_reserv_counter_init, NULL);
1406 * Returns the superpage containing the given page.
1409 vm_reserv_to_superpage(vm_page_t m)
1413 VM_OBJECT_ASSERT_LOCKED(m->object);
1414 rv = vm_reserv_from_page(m);
1415 if (rv->object == m->object && rv->popcnt == VM_LEVEL_0_NPAGES)
1423 #endif /* VM_NRESERVLEVEL > 0 */