2 * Copyright (c) 1991 Regents of the University of California.
4 * Copyright (c) 1998 Matthew Dillon. All Rights Reserved.
6 * This code is derived from software contributed to Berkeley by
7 * The Mach Operating System project at Carnegie-Mellon University.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * from: @(#)vm_page.c 7.4 (Berkeley) 5/7/91
37 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
38 * All rights reserved.
40 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
42 * Permission to use, copy, modify and distribute this software and
43 * its documentation is hereby granted, provided that both the copyright
44 * notice and this permission notice appear in all copies of the
45 * software, derivative works or modified versions, and any portions
46 * thereof, and that both notices appear in supporting documentation.
48 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
50 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
52 * Carnegie Mellon requests users of this software to return to
54 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
55 * School of Computer Science
56 * Carnegie Mellon University
57 * Pittsburgh PA 15213-3890
59 * any improvements or extensions that they make and grant Carnegie the
60 * rights to redistribute these changes.
64 * GENERAL RULES ON VM_PAGE MANIPULATION
66 * - A page queue lock is required when adding or removing a page from a
67 * page queue regardless of other locks or the busy state of a page.
69 * * In general, no thread besides the page daemon can acquire or
70 * hold more than one page queue lock at a time.
72 * * The page daemon can acquire and hold any pair of page queue
75 * - The object lock is required when inserting or removing
76 * pages from an object (vm_page_insert() or vm_page_remove()).
81 * Resident memory management module.
84 #include <sys/cdefs.h>
85 __FBSDID("$FreeBSD$");
89 #include <sys/param.h>
90 #include <sys/systm.h>
92 #include <sys/kernel.h>
93 #include <sys/limits.h>
94 #include <sys/linker.h>
95 #include <sys/malloc.h>
97 #include <sys/msgbuf.h>
98 #include <sys/mutex.h>
100 #include <sys/rwlock.h>
101 #include <sys/sbuf.h>
103 #include <sys/sysctl.h>
104 #include <sys/vmmeter.h>
105 #include <sys/vnode.h>
109 #include <vm/vm_param.h>
110 #include <vm/vm_kern.h>
111 #include <vm/vm_object.h>
112 #include <vm/vm_page.h>
113 #include <vm/vm_pageout.h>
114 #include <vm/vm_pager.h>
115 #include <vm/vm_phys.h>
116 #include <vm/vm_radix.h>
117 #include <vm/vm_reserv.h>
118 #include <vm/vm_extern.h>
120 #include <vm/uma_int.h>
122 #include <machine/md_var.h>
125 * Associated with page of user-allocatable memory is a
129 struct vm_domain vm_dom[MAXMEMDOM];
130 struct mtx_padalign vm_page_queue_free_mtx;
132 struct mtx_padalign pa_lock[PA_LOCK_COUNT];
134 vm_page_t vm_page_array;
135 long vm_page_array_size;
138 static int boot_pages = UMA_BOOT_PAGES;
139 SYSCTL_INT(_vm, OID_AUTO, boot_pages, CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
141 "number of pages allocated for bootstrapping the VM system");
143 static int pa_tryrelock_restart;
144 SYSCTL_INT(_vm, OID_AUTO, tryrelock_restart, CTLFLAG_RD,
145 &pa_tryrelock_restart, 0, "Number of tryrelock restarts");
147 static TAILQ_HEAD(, vm_page) blacklist_head;
148 static int sysctl_vm_page_blacklist(SYSCTL_HANDLER_ARGS);
149 SYSCTL_PROC(_vm, OID_AUTO, page_blacklist, CTLTYPE_STRING | CTLFLAG_RD |
150 CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_page_blacklist, "A", "Blacklist pages");
152 /* Is the page daemon waiting for free pages? */
153 static int vm_pageout_pages_needed;
155 static uma_zone_t fakepg_zone;
157 static void vm_page_alloc_check(vm_page_t m);
158 static void vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits);
159 static void vm_page_enqueue(uint8_t queue, vm_page_t m);
160 static void vm_page_free_wakeup(void);
161 static void vm_page_init_fakepg(void *dummy);
162 static int vm_page_insert_after(vm_page_t m, vm_object_t object,
163 vm_pindex_t pindex, vm_page_t mpred);
164 static void vm_page_insert_radixdone(vm_page_t m, vm_object_t object,
166 static int vm_page_reclaim_run(int req_class, u_long npages, vm_page_t m_run,
169 SYSINIT(vm_page, SI_SUB_VM, SI_ORDER_SECOND, vm_page_init_fakepg, NULL);
172 vm_page_init_fakepg(void *dummy)
175 fakepg_zone = uma_zcreate("fakepg", sizeof(struct vm_page), NULL, NULL,
176 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE | UMA_ZONE_VM);
179 /* Make sure that u_long is at least 64 bits when PAGE_SIZE is 32K. */
180 #if PAGE_SIZE == 32768
182 CTASSERT(sizeof(u_long) >= 8);
187 * Try to acquire a physical address lock while a pmap is locked. If we
188 * fail to trylock we unlock and lock the pmap directly and cache the
189 * locked pa in *locked. The caller should then restart their loop in case
190 * the virtual to physical mapping has changed.
193 vm_page_pa_tryrelock(pmap_t pmap, vm_paddr_t pa, vm_paddr_t *locked)
200 PA_LOCK_ASSERT(lockpa, MA_OWNED);
201 if (PA_LOCKPTR(pa) == PA_LOCKPTR(lockpa))
208 atomic_add_int(&pa_tryrelock_restart, 1);
217 * Sets the page size, perhaps based upon the memory
218 * size. Must be called before any use of page-size
219 * dependent functions.
222 vm_set_page_size(void)
224 if (vm_cnt.v_page_size == 0)
225 vm_cnt.v_page_size = PAGE_SIZE;
226 if (((vm_cnt.v_page_size - 1) & vm_cnt.v_page_size) != 0)
227 panic("vm_set_page_size: page size not a power of two");
231 * vm_page_blacklist_next:
233 * Find the next entry in the provided string of blacklist
234 * addresses. Entries are separated by space, comma, or newline.
235 * If an invalid integer is encountered then the rest of the
236 * string is skipped. Updates the list pointer to the next
237 * character, or NULL if the string is exhausted or invalid.
240 vm_page_blacklist_next(char **list, char *end)
245 if (list == NULL || *list == NULL)
253 * If there's no end pointer then the buffer is coming from
254 * the kenv and we know it's null-terminated.
257 end = *list + strlen(*list);
259 /* Ensure that strtoq() won't walk off the end */
261 if (*end == '\n' || *end == ' ' || *end == ',')
264 printf("Blacklist not terminated, skipping\n");
270 for (pos = *list; *pos != '\0'; pos = cp) {
271 bad = strtoq(pos, &cp, 0);
272 if (*cp == '\0' || *cp == ' ' || *cp == ',' || *cp == '\n') {
281 if (*cp == '\0' || ++cp >= end)
285 return (trunc_page(bad));
287 printf("Garbage in RAM blacklist, skipping\n");
293 * vm_page_blacklist_check:
295 * Iterate through the provided string of blacklist addresses, pulling
296 * each entry out of the physical allocator free list and putting it
297 * onto a list for reporting via the vm.page_blacklist sysctl.
300 vm_page_blacklist_check(char *list, char *end)
308 while (next != NULL) {
309 if ((pa = vm_page_blacklist_next(&next, end)) == 0)
311 m = vm_phys_paddr_to_vm_page(pa);
314 mtx_lock(&vm_page_queue_free_mtx);
315 ret = vm_phys_unfree_page(m);
316 mtx_unlock(&vm_page_queue_free_mtx);
318 TAILQ_INSERT_TAIL(&blacklist_head, m, listq);
320 printf("Skipping page with pa 0x%jx\n",
327 * vm_page_blacklist_load:
329 * Search for a special module named "ram_blacklist". It'll be a
330 * plain text file provided by the user via the loader directive
334 vm_page_blacklist_load(char **list, char **end)
343 mod = preload_search_by_type("ram_blacklist");
345 ptr = preload_fetch_addr(mod);
346 len = preload_fetch_size(mod);
357 sysctl_vm_page_blacklist(SYSCTL_HANDLER_ARGS)
364 error = sysctl_wire_old_buffer(req, 0);
367 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
368 TAILQ_FOREACH(m, &blacklist_head, listq) {
369 sbuf_printf(&sbuf, "%s%#jx", first ? "" : ",",
370 (uintmax_t)m->phys_addr);
373 error = sbuf_finish(&sbuf);
379 vm_page_domain_init(struct vm_domain *vmd)
381 struct vm_pagequeue *pq;
384 *__DECONST(char **, &vmd->vmd_pagequeues[PQ_INACTIVE].pq_name) =
385 "vm inactive pagequeue";
386 *__DECONST(u_int **, &vmd->vmd_pagequeues[PQ_INACTIVE].pq_vcnt) =
387 &vm_cnt.v_inactive_count;
388 *__DECONST(char **, &vmd->vmd_pagequeues[PQ_ACTIVE].pq_name) =
389 "vm active pagequeue";
390 *__DECONST(u_int **, &vmd->vmd_pagequeues[PQ_ACTIVE].pq_vcnt) =
391 &vm_cnt.v_active_count;
392 *__DECONST(char **, &vmd->vmd_pagequeues[PQ_LAUNDRY].pq_name) =
393 "vm laundry pagequeue";
394 *__DECONST(int **, &vmd->vmd_pagequeues[PQ_LAUNDRY].pq_vcnt) =
395 &vm_cnt.v_laundry_count;
396 vmd->vmd_page_count = 0;
397 vmd->vmd_free_count = 0;
399 vmd->vmd_oom = FALSE;
400 for (i = 0; i < PQ_COUNT; i++) {
401 pq = &vmd->vmd_pagequeues[i];
402 TAILQ_INIT(&pq->pq_pl);
403 mtx_init(&pq->pq_mutex, pq->pq_name, "vm pagequeue",
404 MTX_DEF | MTX_DUPOK);
411 * Initializes the resident memory module.
413 * Allocates memory for the page cells, and
414 * for the object/offset-to-page hash table headers.
415 * Each page cell is initialized and placed on the free list.
418 vm_page_startup(vm_offset_t vaddr)
421 vm_paddr_t page_range;
426 char *list, *listend;
428 vm_paddr_t biggestsize;
429 vm_paddr_t low_water, high_water;
435 vaddr = round_page(vaddr);
437 for (i = 0; phys_avail[i + 1]; i += 2) {
438 phys_avail[i] = round_page(phys_avail[i]);
439 phys_avail[i + 1] = trunc_page(phys_avail[i + 1]);
442 low_water = phys_avail[0];
443 high_water = phys_avail[1];
445 for (i = 0; i < vm_phys_nsegs; i++) {
446 if (vm_phys_segs[i].start < low_water)
447 low_water = vm_phys_segs[i].start;
448 if (vm_phys_segs[i].end > high_water)
449 high_water = vm_phys_segs[i].end;
451 for (i = 0; phys_avail[i + 1]; i += 2) {
452 vm_paddr_t size = phys_avail[i + 1] - phys_avail[i];
454 if (size > biggestsize) {
458 if (phys_avail[i] < low_water)
459 low_water = phys_avail[i];
460 if (phys_avail[i + 1] > high_water)
461 high_water = phys_avail[i + 1];
464 end = phys_avail[biggestone+1];
467 * Initialize the page and queue locks.
469 mtx_init(&vm_page_queue_free_mtx, "vm page free queue", NULL, MTX_DEF);
470 for (i = 0; i < PA_LOCK_COUNT; i++)
471 mtx_init(&pa_lock[i], "vm page", NULL, MTX_DEF);
472 for (i = 0; i < vm_ndomains; i++)
473 vm_page_domain_init(&vm_dom[i]);
476 * Almost all of the pages needed for boot strapping UMA are used
477 * for zone structures, so if the number of CPUs results in those
478 * structures taking more than one page each, we set aside more pages
479 * in proportion to the zone structure size.
481 pages_per_zone = howmany(sizeof(struct uma_zone) +
482 sizeof(struct uma_cache) * (mp_maxid + 1), UMA_SLAB_SIZE);
483 if (pages_per_zone > 1) {
484 /* Reserve more pages so that we don't run out. */
485 boot_pages = UMA_BOOT_PAGES_ZONES * pages_per_zone;
489 * Allocate memory for use when boot strapping the kernel memory
492 * CTFLAG_RDTUN doesn't work during the early boot process, so we must
493 * manually fetch the value.
495 TUNABLE_INT_FETCH("vm.boot_pages", &boot_pages);
496 new_end = end - (boot_pages * UMA_SLAB_SIZE);
497 new_end = trunc_page(new_end);
498 mapped = pmap_map(&vaddr, new_end, end,
499 VM_PROT_READ | VM_PROT_WRITE);
500 bzero((void *)mapped, end - new_end);
501 uma_startup((void *)mapped, boot_pages);
503 #if defined(__aarch64__) || defined(__amd64__) || defined(__arm__) || \
504 defined(__i386__) || defined(__mips__)
506 * Allocate a bitmap to indicate that a random physical page
507 * needs to be included in a minidump.
509 * The amd64 port needs this to indicate which direct map pages
510 * need to be dumped, via calls to dump_add_page()/dump_drop_page().
512 * However, i386 still needs this workspace internally within the
513 * minidump code. In theory, they are not needed on i386, but are
514 * included should the sf_buf code decide to use them.
517 for (i = 0; dump_avail[i + 1] != 0; i += 2)
518 if (dump_avail[i + 1] > last_pa)
519 last_pa = dump_avail[i + 1];
520 page_range = last_pa / PAGE_SIZE;
521 vm_page_dump_size = round_page(roundup2(page_range, NBBY) / NBBY);
522 new_end -= vm_page_dump_size;
523 vm_page_dump = (void *)(uintptr_t)pmap_map(&vaddr, new_end,
524 new_end + vm_page_dump_size, VM_PROT_READ | VM_PROT_WRITE);
525 bzero((void *)vm_page_dump, vm_page_dump_size);
529 * Request that the physical pages underlying the message buffer be
530 * included in a crash dump. Since the message buffer is accessed
531 * through the direct map, they are not automatically included.
533 pa = DMAP_TO_PHYS((vm_offset_t)msgbufp->msg_ptr);
534 last_pa = pa + round_page(msgbufsize);
535 while (pa < last_pa) {
541 * Compute the number of pages of memory that will be available for
542 * use (taking into account the overhead of a page structure per
545 first_page = low_water / PAGE_SIZE;
546 #ifdef VM_PHYSSEG_SPARSE
548 for (i = 0; i < vm_phys_nsegs; i++) {
549 page_range += atop(vm_phys_segs[i].end -
550 vm_phys_segs[i].start);
552 for (i = 0; phys_avail[i + 1] != 0; i += 2)
553 page_range += atop(phys_avail[i + 1] - phys_avail[i]);
554 #elif defined(VM_PHYSSEG_DENSE)
555 page_range = high_water / PAGE_SIZE - first_page;
557 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
562 * Reserve an unmapped guard page to trap access to vm_page_array[-1].
567 * Initialize the mem entry structures now, and put them in the free
570 new_end = trunc_page(end - page_range * sizeof(struct vm_page));
571 mapped = pmap_map(&vaddr, new_end, end,
572 VM_PROT_READ | VM_PROT_WRITE);
573 vm_page_array = (vm_page_t) mapped;
574 #if VM_NRESERVLEVEL > 0
576 * Allocate memory for the reservation management system's data
579 new_end = vm_reserv_startup(&vaddr, new_end, high_water);
581 #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__)
583 * pmap_map on arm64, amd64, and mips can come out of the direct-map,
584 * not kvm like i386, so the pages must be tracked for a crashdump to
585 * include this data. This includes the vm_page_array and the early
586 * UMA bootstrap pages.
588 for (pa = new_end; pa < phys_avail[biggestone + 1]; pa += PAGE_SIZE)
591 phys_avail[biggestone + 1] = new_end;
594 * Add physical memory segments corresponding to the available
597 for (i = 0; phys_avail[i + 1] != 0; i += 2)
598 vm_phys_add_seg(phys_avail[i], phys_avail[i + 1]);
601 * Clear all of the page structures
603 bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page));
604 for (i = 0; i < page_range; i++)
605 vm_page_array[i].order = VM_NFREEORDER;
606 vm_page_array_size = page_range;
609 * Initialize the physical memory allocator.
614 * Add every available physical page that is not blacklisted to
617 vm_cnt.v_page_count = 0;
618 vm_cnt.v_free_count = 0;
619 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
621 last_pa = phys_avail[i + 1];
622 while (pa < last_pa) {
623 vm_phys_add_page(pa);
628 TAILQ_INIT(&blacklist_head);
629 vm_page_blacklist_load(&list, &listend);
630 vm_page_blacklist_check(list, listend);
632 list = kern_getenv("vm.blacklist");
633 vm_page_blacklist_check(list, NULL);
636 #if VM_NRESERVLEVEL > 0
638 * Initialize the reservation management system.
646 vm_page_reference(vm_page_t m)
649 vm_page_aflag_set(m, PGA_REFERENCED);
653 * vm_page_busy_downgrade:
655 * Downgrade an exclusive busy page into a single shared busy page.
658 vm_page_busy_downgrade(vm_page_t m)
663 vm_page_assert_xbusied(m);
664 locked = mtx_owned(vm_page_lockptr(m));
668 x &= VPB_BIT_WAITERS;
669 if (x != 0 && !locked)
671 if (atomic_cmpset_rel_int(&m->busy_lock,
672 VPB_SINGLE_EXCLUSIVER | x, VPB_SHARERS_WORD(1)))
674 if (x != 0 && !locked)
687 * Return a positive value if the page is shared busied, 0 otherwise.
690 vm_page_sbusied(vm_page_t m)
695 return ((x & VPB_BIT_SHARED) != 0 && x != VPB_UNBUSIED);
701 * Shared unbusy a page.
704 vm_page_sunbusy(vm_page_t m)
708 vm_page_assert_sbusied(m);
712 if (VPB_SHARERS(x) > 1) {
713 if (atomic_cmpset_int(&m->busy_lock, x,
718 if ((x & VPB_BIT_WAITERS) == 0) {
719 KASSERT(x == VPB_SHARERS_WORD(1),
720 ("vm_page_sunbusy: invalid lock state"));
721 if (atomic_cmpset_int(&m->busy_lock,
722 VPB_SHARERS_WORD(1), VPB_UNBUSIED))
726 KASSERT(x == (VPB_SHARERS_WORD(1) | VPB_BIT_WAITERS),
727 ("vm_page_sunbusy: invalid lock state for waiters"));
730 if (!atomic_cmpset_int(&m->busy_lock, x, VPB_UNBUSIED)) {
741 * vm_page_busy_sleep:
743 * Sleep and release the page lock, using the page pointer as wchan.
744 * This is used to implement the hard-path of busying mechanism.
746 * The given page must be locked.
748 * If nonshared is true, sleep only if the page is xbusy.
751 vm_page_busy_sleep(vm_page_t m, const char *wmesg, bool nonshared)
755 vm_page_assert_locked(m);
758 if (x == VPB_UNBUSIED || (nonshared && (x & VPB_BIT_SHARED) != 0) ||
759 ((x & VPB_BIT_WAITERS) == 0 &&
760 !atomic_cmpset_int(&m->busy_lock, x, x | VPB_BIT_WAITERS))) {
764 msleep(m, vm_page_lockptr(m), PVM | PDROP, wmesg, 0);
770 * Try to shared busy a page.
771 * If the operation succeeds 1 is returned otherwise 0.
772 * The operation never sleeps.
775 vm_page_trysbusy(vm_page_t m)
781 if ((x & VPB_BIT_SHARED) == 0)
783 if (atomic_cmpset_acq_int(&m->busy_lock, x, x + VPB_ONE_SHARER))
789 vm_page_xunbusy_locked(vm_page_t m)
792 vm_page_assert_xbusied(m);
793 vm_page_assert_locked(m);
795 atomic_store_rel_int(&m->busy_lock, VPB_UNBUSIED);
796 /* There is a waiter, do wakeup() instead of vm_page_flash(). */
801 vm_page_xunbusy_maybelocked(vm_page_t m)
805 vm_page_assert_xbusied(m);
808 * Fast path for unbusy. If it succeeds, we know that there
809 * are no waiters, so we do not need a wakeup.
811 if (atomic_cmpset_rel_int(&m->busy_lock, VPB_SINGLE_EXCLUSIVER,
815 lockacq = !mtx_owned(vm_page_lockptr(m));
818 vm_page_xunbusy_locked(m);
824 * vm_page_xunbusy_hard:
826 * Called after the first try the exclusive unbusy of a page failed.
827 * It is assumed that the waiters bit is on.
830 vm_page_xunbusy_hard(vm_page_t m)
833 vm_page_assert_xbusied(m);
836 vm_page_xunbusy_locked(m);
843 * Wakeup anyone waiting for the page.
844 * The ownership bits do not change.
846 * The given page must be locked.
849 vm_page_flash(vm_page_t m)
853 vm_page_lock_assert(m, MA_OWNED);
857 if ((x & VPB_BIT_WAITERS) == 0)
859 if (atomic_cmpset_int(&m->busy_lock, x,
860 x & (~VPB_BIT_WAITERS)))
867 * Keep page from being freed by the page daemon
868 * much of the same effect as wiring, except much lower
869 * overhead and should be used only for *very* temporary
870 * holding ("wiring").
873 vm_page_hold(vm_page_t mem)
876 vm_page_lock_assert(mem, MA_OWNED);
881 vm_page_unhold(vm_page_t mem)
884 vm_page_lock_assert(mem, MA_OWNED);
885 KASSERT(mem->hold_count >= 1, ("vm_page_unhold: hold count < 0!!!"));
887 if (mem->hold_count == 0 && (mem->flags & PG_UNHOLDFREE) != 0)
888 vm_page_free_toq(mem);
892 * vm_page_unhold_pages:
894 * Unhold each of the pages that is referenced by the given array.
897 vm_page_unhold_pages(vm_page_t *ma, int count)
899 struct mtx *mtx, *new_mtx;
902 for (; count != 0; count--) {
904 * Avoid releasing and reacquiring the same page lock.
906 new_mtx = vm_page_lockptr(*ma);
907 if (mtx != new_mtx) {
921 PHYS_TO_VM_PAGE(vm_paddr_t pa)
925 #ifdef VM_PHYSSEG_SPARSE
926 m = vm_phys_paddr_to_vm_page(pa);
928 m = vm_phys_fictitious_to_vm_page(pa);
930 #elif defined(VM_PHYSSEG_DENSE)
934 if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
935 m = &vm_page_array[pi - first_page];
938 return (vm_phys_fictitious_to_vm_page(pa));
940 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
947 * Create a fictitious page with the specified physical address and
948 * memory attribute. The memory attribute is the only the machine-
949 * dependent aspect of a fictitious page that must be initialized.
952 vm_page_getfake(vm_paddr_t paddr, vm_memattr_t memattr)
956 m = uma_zalloc(fakepg_zone, M_WAITOK | M_ZERO);
957 vm_page_initfake(m, paddr, memattr);
962 vm_page_initfake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
965 if ((m->flags & PG_FICTITIOUS) != 0) {
967 * The page's memattr might have changed since the
968 * previous initialization. Update the pmap to the
973 m->phys_addr = paddr;
975 /* Fictitious pages don't use "segind". */
976 m->flags = PG_FICTITIOUS;
977 /* Fictitious pages don't use "order" or "pool". */
978 m->oflags = VPO_UNMANAGED;
979 m->busy_lock = VPB_SINGLE_EXCLUSIVER;
983 pmap_page_set_memattr(m, memattr);
989 * Release a fictitious page.
992 vm_page_putfake(vm_page_t m)
995 KASSERT((m->oflags & VPO_UNMANAGED) != 0, ("managed %p", m));
996 KASSERT((m->flags & PG_FICTITIOUS) != 0,
997 ("vm_page_putfake: bad page %p", m));
998 uma_zfree(fakepg_zone, m);
1002 * vm_page_updatefake:
1004 * Update the given fictitious page to the specified physical address and
1008 vm_page_updatefake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
1011 KASSERT((m->flags & PG_FICTITIOUS) != 0,
1012 ("vm_page_updatefake: bad page %p", m));
1013 m->phys_addr = paddr;
1014 pmap_page_set_memattr(m, memattr);
1023 vm_page_free(vm_page_t m)
1026 m->flags &= ~PG_ZERO;
1027 vm_page_free_toq(m);
1031 * vm_page_free_zero:
1033 * Free a page to the zerod-pages queue
1036 vm_page_free_zero(vm_page_t m)
1039 m->flags |= PG_ZERO;
1040 vm_page_free_toq(m);
1044 * Unbusy and handle the page queueing for a page from a getpages request that
1045 * was optionally read ahead or behind.
1048 vm_page_readahead_finish(vm_page_t m)
1051 /* We shouldn't put invalid pages on queues. */
1052 KASSERT(m->valid != 0, ("%s: %p is invalid", __func__, m));
1055 * Since the page is not the actually needed one, whether it should
1056 * be activated or deactivated is not obvious. Empirical results
1057 * have shown that deactivating the page is usually the best choice,
1058 * unless the page is wanted by another thread.
1061 if ((m->busy_lock & VPB_BIT_WAITERS) != 0)
1062 vm_page_activate(m);
1064 vm_page_deactivate(m);
1070 * vm_page_sleep_if_busy:
1072 * Sleep and release the page queues lock if the page is busied.
1073 * Returns TRUE if the thread slept.
1075 * The given page must be unlocked and object containing it must
1079 vm_page_sleep_if_busy(vm_page_t m, const char *msg)
1083 vm_page_lock_assert(m, MA_NOTOWNED);
1084 VM_OBJECT_ASSERT_WLOCKED(m->object);
1086 if (vm_page_busied(m)) {
1088 * The page-specific object must be cached because page
1089 * identity can change during the sleep, causing the
1090 * re-lock of a different object.
1091 * It is assumed that a reference to the object is already
1092 * held by the callers.
1096 VM_OBJECT_WUNLOCK(obj);
1097 vm_page_busy_sleep(m, msg, false);
1098 VM_OBJECT_WLOCK(obj);
1105 * vm_page_dirty_KBI: [ internal use only ]
1107 * Set all bits in the page's dirty field.
1109 * The object containing the specified page must be locked if the
1110 * call is made from the machine-independent layer.
1112 * See vm_page_clear_dirty_mask().
1114 * This function should only be called by vm_page_dirty().
1117 vm_page_dirty_KBI(vm_page_t m)
1120 /* Refer to this operation by its public name. */
1121 KASSERT(m->valid == VM_PAGE_BITS_ALL,
1122 ("vm_page_dirty: page is invalid!"));
1123 m->dirty = VM_PAGE_BITS_ALL;
1127 * vm_page_insert: [ internal use only ]
1129 * Inserts the given mem entry into the object and object list.
1131 * The object must be locked.
1134 vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex)
1138 VM_OBJECT_ASSERT_WLOCKED(object);
1139 mpred = vm_radix_lookup_le(&object->rtree, pindex);
1140 return (vm_page_insert_after(m, object, pindex, mpred));
1144 * vm_page_insert_after:
1146 * Inserts the page "m" into the specified object at offset "pindex".
1148 * The page "mpred" must immediately precede the offset "pindex" within
1149 * the specified object.
1151 * The object must be locked.
1154 vm_page_insert_after(vm_page_t m, vm_object_t object, vm_pindex_t pindex,
1159 VM_OBJECT_ASSERT_WLOCKED(object);
1160 KASSERT(m->object == NULL,
1161 ("vm_page_insert_after: page already inserted"));
1162 if (mpred != NULL) {
1163 KASSERT(mpred->object == object,
1164 ("vm_page_insert_after: object doesn't contain mpred"));
1165 KASSERT(mpred->pindex < pindex,
1166 ("vm_page_insert_after: mpred doesn't precede pindex"));
1167 msucc = TAILQ_NEXT(mpred, listq);
1169 msucc = TAILQ_FIRST(&object->memq);
1171 KASSERT(msucc->pindex > pindex,
1172 ("vm_page_insert_after: msucc doesn't succeed pindex"));
1175 * Record the object/offset pair in this page
1181 * Now link into the object's ordered list of backed pages.
1183 if (vm_radix_insert(&object->rtree, m)) {
1188 vm_page_insert_radixdone(m, object, mpred);
1193 * vm_page_insert_radixdone:
1195 * Complete page "m" insertion into the specified object after the
1196 * radix trie hooking.
1198 * The page "mpred" must precede the offset "m->pindex" within the
1201 * The object must be locked.
1204 vm_page_insert_radixdone(vm_page_t m, vm_object_t object, vm_page_t mpred)
1207 VM_OBJECT_ASSERT_WLOCKED(object);
1208 KASSERT(object != NULL && m->object == object,
1209 ("vm_page_insert_radixdone: page %p has inconsistent object", m));
1210 if (mpred != NULL) {
1211 KASSERT(mpred->object == object,
1212 ("vm_page_insert_after: object doesn't contain mpred"));
1213 KASSERT(mpred->pindex < m->pindex,
1214 ("vm_page_insert_after: mpred doesn't precede pindex"));
1218 TAILQ_INSERT_AFTER(&object->memq, mpred, m, listq);
1220 TAILQ_INSERT_HEAD(&object->memq, m, listq);
1223 * Show that the object has one more resident page.
1225 object->resident_page_count++;
1228 * Hold the vnode until the last page is released.
1230 if (object->resident_page_count == 1 && object->type == OBJT_VNODE)
1231 vhold(object->handle);
1234 * Since we are inserting a new and possibly dirty page,
1235 * update the object's OBJ_MIGHTBEDIRTY flag.
1237 if (pmap_page_is_write_mapped(m))
1238 vm_object_set_writeable_dirty(object);
1244 * Removes the specified page from its containing object, but does not
1245 * invalidate any backing storage.
1247 * The object must be locked. The page must be locked if it is managed.
1250 vm_page_remove(vm_page_t m)
1255 if ((m->oflags & VPO_UNMANAGED) == 0)
1256 vm_page_assert_locked(m);
1257 if ((object = m->object) == NULL)
1259 VM_OBJECT_ASSERT_WLOCKED(object);
1260 if (vm_page_xbusied(m))
1261 vm_page_xunbusy_maybelocked(m);
1262 mrem = vm_radix_remove(&object->rtree, m->pindex);
1263 KASSERT(mrem == m, ("removed page %p, expected page %p", mrem, m));
1266 * Now remove from the object's list of backed pages.
1268 TAILQ_REMOVE(&object->memq, m, listq);
1271 * And show that the object has one fewer resident page.
1273 object->resident_page_count--;
1276 * The vnode may now be recycled.
1278 if (object->resident_page_count == 0 && object->type == OBJT_VNODE)
1279 vdrop(object->handle);
1287 * Returns the page associated with the object/offset
1288 * pair specified; if none is found, NULL is returned.
1290 * The object must be locked.
1293 vm_page_lookup(vm_object_t object, vm_pindex_t pindex)
1296 VM_OBJECT_ASSERT_LOCKED(object);
1297 return (vm_radix_lookup(&object->rtree, pindex));
1301 * vm_page_find_least:
1303 * Returns the page associated with the object with least pindex
1304 * greater than or equal to the parameter pindex, or NULL.
1306 * The object must be locked.
1309 vm_page_find_least(vm_object_t object, vm_pindex_t pindex)
1313 VM_OBJECT_ASSERT_LOCKED(object);
1314 if ((m = TAILQ_FIRST(&object->memq)) != NULL && m->pindex < pindex)
1315 m = vm_radix_lookup_ge(&object->rtree, pindex);
1320 * Returns the given page's successor (by pindex) within the object if it is
1321 * resident; if none is found, NULL is returned.
1323 * The object must be locked.
1326 vm_page_next(vm_page_t m)
1330 VM_OBJECT_ASSERT_LOCKED(m->object);
1331 if ((next = TAILQ_NEXT(m, listq)) != NULL &&
1332 next->pindex != m->pindex + 1)
1338 * Returns the given page's predecessor (by pindex) within the object if it is
1339 * resident; if none is found, NULL is returned.
1341 * The object must be locked.
1344 vm_page_prev(vm_page_t m)
1348 VM_OBJECT_ASSERT_LOCKED(m->object);
1349 if ((prev = TAILQ_PREV(m, pglist, listq)) != NULL &&
1350 prev->pindex != m->pindex - 1)
1356 * Uses the page mnew as a replacement for an existing page at index
1357 * pindex which must be already present in the object.
1359 * The existing page must not be on a paging queue.
1362 vm_page_replace(vm_page_t mnew, vm_object_t object, vm_pindex_t pindex)
1366 VM_OBJECT_ASSERT_WLOCKED(object);
1367 KASSERT(mnew->object == NULL,
1368 ("vm_page_replace: page already in object"));
1371 * This function mostly follows vm_page_insert() and
1372 * vm_page_remove() without the radix, object count and vnode
1373 * dance. Double check such functions for more comments.
1376 mnew->object = object;
1377 mnew->pindex = pindex;
1378 mold = vm_radix_replace(&object->rtree, mnew);
1379 KASSERT(mold->queue == PQ_NONE,
1380 ("vm_page_replace: mold is on a paging queue"));
1382 /* Keep the resident page list in sorted order. */
1383 TAILQ_INSERT_AFTER(&object->memq, mold, mnew, listq);
1384 TAILQ_REMOVE(&object->memq, mold, listq);
1386 mold->object = NULL;
1387 vm_page_xunbusy_maybelocked(mold);
1390 * The object's resident_page_count does not change because we have
1391 * swapped one page for another, but OBJ_MIGHTBEDIRTY.
1393 if (pmap_page_is_write_mapped(mnew))
1394 vm_object_set_writeable_dirty(object);
1401 * Move the given memory entry from its
1402 * current object to the specified target object/offset.
1404 * Note: swap associated with the page must be invalidated by the move. We
1405 * have to do this for several reasons: (1) we aren't freeing the
1406 * page, (2) we are dirtying the page, (3) the VM system is probably
1407 * moving the page from object A to B, and will then later move
1408 * the backing store from A to B and we can't have a conflict.
1410 * Note: we *always* dirty the page. It is necessary both for the
1411 * fact that we moved it, and because we may be invalidating
1414 * The objects must be locked.
1417 vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex)
1422 VM_OBJECT_ASSERT_WLOCKED(new_object);
1424 mpred = vm_radix_lookup_le(&new_object->rtree, new_pindex);
1425 KASSERT(mpred == NULL || mpred->pindex != new_pindex,
1426 ("vm_page_rename: pindex already renamed"));
1429 * Create a custom version of vm_page_insert() which does not depend
1430 * by m_prev and can cheat on the implementation aspects of the
1434 m->pindex = new_pindex;
1435 if (vm_radix_insert(&new_object->rtree, m)) {
1441 * The operation cannot fail anymore. The removal must happen before
1442 * the listq iterator is tainted.
1448 /* Return back to the new pindex to complete vm_page_insert(). */
1449 m->pindex = new_pindex;
1450 m->object = new_object;
1452 vm_page_insert_radixdone(m, new_object, mpred);
1460 * Allocate and return a page that is associated with the specified
1461 * object and offset pair. By default, this page is exclusive busied.
1463 * The caller must always specify an allocation class.
1465 * allocation classes:
1466 * VM_ALLOC_NORMAL normal process request
1467 * VM_ALLOC_SYSTEM system *really* needs a page
1468 * VM_ALLOC_INTERRUPT interrupt time request
1470 * optional allocation flags:
1471 * VM_ALLOC_COUNT(number) the number of additional pages that the caller
1472 * intends to allocate
1473 * VM_ALLOC_NOBUSY do not exclusive busy the page
1474 * VM_ALLOC_NODUMP do not include the page in a kernel core dump
1475 * VM_ALLOC_NOOBJ page is not associated with an object and
1476 * should not be exclusive busy
1477 * VM_ALLOC_SBUSY shared busy the allocated page
1478 * VM_ALLOC_WIRED wire the allocated page
1479 * VM_ALLOC_ZERO prefer a zeroed page
1481 * This routine may not sleep.
1484 vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req)
1487 int flags, req_class;
1489 mpred = 0; /* XXX: pacify gcc */
1490 KASSERT((object != NULL) == ((req & VM_ALLOC_NOOBJ) == 0) &&
1491 (object != NULL || (req & VM_ALLOC_SBUSY) == 0) &&
1492 ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) !=
1493 (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)),
1494 ("vm_page_alloc: inconsistent object(%p)/req(%x)", (void *)object,
1497 VM_OBJECT_ASSERT_WLOCKED(object);
1499 req_class = req & VM_ALLOC_CLASS_MASK;
1502 * The page daemon is allowed to dig deeper into the free page list.
1504 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
1505 req_class = VM_ALLOC_SYSTEM;
1507 if (object != NULL) {
1508 mpred = vm_radix_lookup_le(&object->rtree, pindex);
1509 KASSERT(mpred == NULL || mpred->pindex != pindex,
1510 ("vm_page_alloc: pindex already allocated"));
1514 * Allocate a page if the number of free pages exceeds the minimum
1515 * for the request class.
1517 mtx_lock(&vm_page_queue_free_mtx);
1518 if (vm_cnt.v_free_count > vm_cnt.v_free_reserved ||
1519 (req_class == VM_ALLOC_SYSTEM &&
1520 vm_cnt.v_free_count > vm_cnt.v_interrupt_free_min) ||
1521 (req_class == VM_ALLOC_INTERRUPT &&
1522 vm_cnt.v_free_count > 0)) {
1524 * Can we allocate the page from a reservation?
1526 #if VM_NRESERVLEVEL > 0
1527 if (object == NULL || (object->flags & (OBJ_COLORED |
1528 OBJ_FICTITIOUS)) != OBJ_COLORED || (m =
1529 vm_reserv_alloc_page(object, pindex, mpred)) == NULL)
1533 * If not, allocate it from the free page queues.
1535 m = vm_phys_alloc_pages(object != NULL ?
1536 VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT, 0);
1537 #if VM_NRESERVLEVEL > 0
1538 if (m == NULL && vm_reserv_reclaim_inactive()) {
1539 m = vm_phys_alloc_pages(object != NULL ?
1540 VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT,
1547 * Not allocatable, give up.
1549 mtx_unlock(&vm_page_queue_free_mtx);
1550 atomic_add_int(&vm_pageout_deficit,
1551 max((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1));
1552 pagedaemon_wakeup();
1557 * At this point we had better have found a good page.
1559 KASSERT(m != NULL, ("vm_page_alloc: missing page"));
1560 vm_phys_freecnt_adj(m, -1);
1561 mtx_unlock(&vm_page_queue_free_mtx);
1562 vm_page_alloc_check(m);
1565 * Initialize the page. Only the PG_ZERO flag is inherited.
1568 if ((req & VM_ALLOC_ZERO) != 0)
1571 if ((req & VM_ALLOC_NODUMP) != 0)
1575 m->oflags = object == NULL || (object->flags & OBJ_UNMANAGED) != 0 ?
1577 m->busy_lock = VPB_UNBUSIED;
1578 if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ | VM_ALLOC_SBUSY)) == 0)
1579 m->busy_lock = VPB_SINGLE_EXCLUSIVER;
1580 if ((req & VM_ALLOC_SBUSY) != 0)
1581 m->busy_lock = VPB_SHARERS_WORD(1);
1582 if (req & VM_ALLOC_WIRED) {
1584 * The page lock is not required for wiring a page until that
1585 * page is inserted into the object.
1587 atomic_add_int(&vm_cnt.v_wire_count, 1);
1592 if (object != NULL) {
1593 if (vm_page_insert_after(m, object, pindex, mpred)) {
1594 pagedaemon_wakeup();
1595 if (req & VM_ALLOC_WIRED) {
1596 atomic_subtract_int(&vm_cnt.v_wire_count, 1);
1600 m->oflags = VPO_UNMANAGED;
1601 m->busy_lock = VPB_UNBUSIED;
1606 /* Ignore device objects; the pager sets "memattr" for them. */
1607 if (object->memattr != VM_MEMATTR_DEFAULT &&
1608 (object->flags & OBJ_FICTITIOUS) == 0)
1609 pmap_page_set_memattr(m, object->memattr);
1614 * Don't wakeup too often - wakeup the pageout daemon when
1615 * we would be nearly out of memory.
1617 if (vm_paging_needed())
1618 pagedaemon_wakeup();
1624 * vm_page_alloc_contig:
1626 * Allocate a contiguous set of physical pages of the given size "npages"
1627 * from the free lists. All of the physical pages must be at or above
1628 * the given physical address "low" and below the given physical address
1629 * "high". The given value "alignment" determines the alignment of the
1630 * first physical page in the set. If the given value "boundary" is
1631 * non-zero, then the set of physical pages cannot cross any physical
1632 * address boundary that is a multiple of that value. Both "alignment"
1633 * and "boundary" must be a power of two.
1635 * If the specified memory attribute, "memattr", is VM_MEMATTR_DEFAULT,
1636 * then the memory attribute setting for the physical pages is configured
1637 * to the object's memory attribute setting. Otherwise, the memory
1638 * attribute setting for the physical pages is configured to "memattr",
1639 * overriding the object's memory attribute setting. However, if the
1640 * object's memory attribute setting is not VM_MEMATTR_DEFAULT, then the
1641 * memory attribute setting for the physical pages cannot be configured
1642 * to VM_MEMATTR_DEFAULT.
1644 * The caller must always specify an allocation class.
1646 * allocation classes:
1647 * VM_ALLOC_NORMAL normal process request
1648 * VM_ALLOC_SYSTEM system *really* needs a page
1649 * VM_ALLOC_INTERRUPT interrupt time request
1651 * optional allocation flags:
1652 * VM_ALLOC_NOBUSY do not exclusive busy the page
1653 * VM_ALLOC_NODUMP do not include the page in a kernel core dump
1654 * VM_ALLOC_NOOBJ page is not associated with an object and
1655 * should not be exclusive busy
1656 * VM_ALLOC_SBUSY shared busy the allocated page
1657 * VM_ALLOC_WIRED wire the allocated page
1658 * VM_ALLOC_ZERO prefer a zeroed page
1660 * This routine may not sleep.
1663 vm_page_alloc_contig(vm_object_t object, vm_pindex_t pindex, int req,
1664 u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
1665 vm_paddr_t boundary, vm_memattr_t memattr)
1667 vm_page_t m, m_tmp, m_ret;
1671 KASSERT((object != NULL) == ((req & VM_ALLOC_NOOBJ) == 0) &&
1672 (object != NULL || (req & VM_ALLOC_SBUSY) == 0) &&
1673 ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) !=
1674 (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)),
1675 ("vm_page_alloc: inconsistent object(%p)/req(%x)", (void *)object,
1677 if (object != NULL) {
1678 VM_OBJECT_ASSERT_WLOCKED(object);
1679 KASSERT(object->type == OBJT_PHYS,
1680 ("vm_page_alloc_contig: object %p isn't OBJT_PHYS",
1683 KASSERT(npages > 0, ("vm_page_alloc_contig: npages is zero"));
1684 req_class = req & VM_ALLOC_CLASS_MASK;
1687 * The page daemon is allowed to dig deeper into the free page list.
1689 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
1690 req_class = VM_ALLOC_SYSTEM;
1692 mtx_lock(&vm_page_queue_free_mtx);
1693 if (vm_cnt.v_free_count >= npages + vm_cnt.v_free_reserved ||
1694 (req_class == VM_ALLOC_SYSTEM &&
1695 vm_cnt.v_free_count >= npages + vm_cnt.v_interrupt_free_min) ||
1696 (req_class == VM_ALLOC_INTERRUPT &&
1697 vm_cnt.v_free_count >= npages)) {
1698 #if VM_NRESERVLEVEL > 0
1700 if (object == NULL || (object->flags & OBJ_COLORED) == 0 ||
1701 (m_ret = vm_reserv_alloc_contig(object, pindex, npages,
1702 low, high, alignment, boundary)) == NULL)
1704 m_ret = vm_phys_alloc_contig(npages, low, high,
1705 alignment, boundary);
1707 mtx_unlock(&vm_page_queue_free_mtx);
1708 atomic_add_int(&vm_pageout_deficit, npages);
1709 pagedaemon_wakeup();
1713 vm_phys_freecnt_adj(m_ret, -npages);
1715 #if VM_NRESERVLEVEL > 0
1716 if (vm_reserv_reclaim_contig(npages, low, high, alignment,
1721 mtx_unlock(&vm_page_queue_free_mtx);
1724 for (m = m_ret; m < &m_ret[npages]; m++)
1725 vm_page_alloc_check(m);
1728 * Initialize the pages. Only the PG_ZERO flag is inherited.
1731 if ((req & VM_ALLOC_ZERO) != 0)
1733 if ((req & VM_ALLOC_NODUMP) != 0)
1735 if ((req & VM_ALLOC_WIRED) != 0)
1736 atomic_add_int(&vm_cnt.v_wire_count, npages);
1737 if (object != NULL) {
1738 if (object->memattr != VM_MEMATTR_DEFAULT &&
1739 memattr == VM_MEMATTR_DEFAULT)
1740 memattr = object->memattr;
1742 for (m = m_ret; m < &m_ret[npages]; m++) {
1744 m->flags = (m->flags | PG_NODUMP) & flags;
1745 m->busy_lock = VPB_UNBUSIED;
1746 if (object != NULL) {
1747 if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) == 0)
1748 m->busy_lock = VPB_SINGLE_EXCLUSIVER;
1749 if ((req & VM_ALLOC_SBUSY) != 0)
1750 m->busy_lock = VPB_SHARERS_WORD(1);
1752 if ((req & VM_ALLOC_WIRED) != 0)
1754 /* Unmanaged pages don't use "act_count". */
1755 m->oflags = VPO_UNMANAGED;
1756 if (object != NULL) {
1757 if (vm_page_insert(m, object, pindex)) {
1758 if (vm_paging_needed())
1759 pagedaemon_wakeup();
1760 if ((req & VM_ALLOC_WIRED) != 0)
1761 atomic_subtract_int(&vm_cnt.v_wire_count,
1763 for (m_tmp = m, m = m_ret;
1764 m < &m_ret[npages]; m++) {
1765 if ((req & VM_ALLOC_WIRED) != 0)
1769 m->oflags |= VPO_UNMANAGED;
1771 m->busy_lock = VPB_UNBUSIED;
1778 if (memattr != VM_MEMATTR_DEFAULT)
1779 pmap_page_set_memattr(m, memattr);
1782 if (vm_paging_needed())
1783 pagedaemon_wakeup();
1788 * Check a page that has been freshly dequeued from a freelist.
1791 vm_page_alloc_check(vm_page_t m)
1794 KASSERT(m->queue == PQ_NONE,
1795 ("page %p has unexpected queue %d", m, m->queue));
1796 KASSERT(m->wire_count == 0, ("page %p is wired", m));
1797 KASSERT(m->hold_count == 0, ("page %p is held", m));
1798 KASSERT(!vm_page_busied(m), ("page %p is busy", m));
1799 KASSERT(m->dirty == 0, ("page %p is dirty", m));
1800 KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT,
1801 ("page %p has unexpected memattr %d",
1802 m, pmap_page_get_memattr(m)));
1803 KASSERT(m->valid == 0, ("free page %p is valid", m));
1807 * vm_page_alloc_freelist:
1809 * Allocate a physical page from the specified free page list.
1811 * The caller must always specify an allocation class.
1813 * allocation classes:
1814 * VM_ALLOC_NORMAL normal process request
1815 * VM_ALLOC_SYSTEM system *really* needs a page
1816 * VM_ALLOC_INTERRUPT interrupt time request
1818 * optional allocation flags:
1819 * VM_ALLOC_COUNT(number) the number of additional pages that the caller
1820 * intends to allocate
1821 * VM_ALLOC_WIRED wire the allocated page
1822 * VM_ALLOC_ZERO prefer a zeroed page
1824 * This routine may not sleep.
1827 vm_page_alloc_freelist(int flind, int req)
1833 req_class = req & VM_ALLOC_CLASS_MASK;
1836 * The page daemon is allowed to dig deeper into the free page list.
1838 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
1839 req_class = VM_ALLOC_SYSTEM;
1842 * Do not allocate reserved pages unless the req has asked for it.
1844 mtx_lock(&vm_page_queue_free_mtx);
1845 if (vm_cnt.v_free_count > vm_cnt.v_free_reserved ||
1846 (req_class == VM_ALLOC_SYSTEM &&
1847 vm_cnt.v_free_count > vm_cnt.v_interrupt_free_min) ||
1848 (req_class == VM_ALLOC_INTERRUPT &&
1849 vm_cnt.v_free_count > 0))
1850 m = vm_phys_alloc_freelist_pages(flind, VM_FREEPOOL_DIRECT, 0);
1852 mtx_unlock(&vm_page_queue_free_mtx);
1853 atomic_add_int(&vm_pageout_deficit,
1854 max((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1));
1855 pagedaemon_wakeup();
1859 mtx_unlock(&vm_page_queue_free_mtx);
1862 vm_phys_freecnt_adj(m, -1);
1863 mtx_unlock(&vm_page_queue_free_mtx);
1864 vm_page_alloc_check(m);
1867 * Initialize the page. Only the PG_ZERO flag is inherited.
1871 if ((req & VM_ALLOC_ZERO) != 0)
1874 if ((req & VM_ALLOC_WIRED) != 0) {
1876 * The page lock is not required for wiring a page that does
1877 * not belong to an object.
1879 atomic_add_int(&vm_cnt.v_wire_count, 1);
1882 /* Unmanaged pages don't use "act_count". */
1883 m->oflags = VPO_UNMANAGED;
1884 if (vm_paging_needed())
1885 pagedaemon_wakeup();
1889 #define VPSC_ANY 0 /* No restrictions. */
1890 #define VPSC_NORESERV 1 /* Skip reservations; implies VPSC_NOSUPER. */
1891 #define VPSC_NOSUPER 2 /* Skip superpages. */
1894 * vm_page_scan_contig:
1896 * Scan vm_page_array[] between the specified entries "m_start" and
1897 * "m_end" for a run of contiguous physical pages that satisfy the
1898 * specified conditions, and return the lowest page in the run. The
1899 * specified "alignment" determines the alignment of the lowest physical
1900 * page in the run. If the specified "boundary" is non-zero, then the
1901 * run of physical pages cannot span a physical address that is a
1902 * multiple of "boundary".
1904 * "m_end" is never dereferenced, so it need not point to a vm_page
1905 * structure within vm_page_array[].
1907 * "npages" must be greater than zero. "m_start" and "m_end" must not
1908 * span a hole (or discontiguity) in the physical address space. Both
1909 * "alignment" and "boundary" must be a power of two.
1912 vm_page_scan_contig(u_long npages, vm_page_t m_start, vm_page_t m_end,
1913 u_long alignment, vm_paddr_t boundary, int options)
1915 struct mtx *m_mtx, *new_mtx;
1919 #if VM_NRESERVLEVEL > 0
1922 int m_inc, order, run_ext, run_len;
1924 KASSERT(npages > 0, ("npages is 0"));
1925 KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
1926 KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
1930 for (m = m_start; m < m_end && run_len < npages; m += m_inc) {
1931 KASSERT((m->flags & (PG_FICTITIOUS | PG_MARKER)) == 0,
1932 ("page %p is PG_FICTITIOUS or PG_MARKER", m));
1935 * If the current page would be the start of a run, check its
1936 * physical address against the end, alignment, and boundary
1937 * conditions. If it doesn't satisfy these conditions, either
1938 * terminate the scan or advance to the next page that
1939 * satisfies the failed condition.
1942 KASSERT(m_run == NULL, ("m_run != NULL"));
1943 if (m + npages > m_end)
1945 pa = VM_PAGE_TO_PHYS(m);
1946 if ((pa & (alignment - 1)) != 0) {
1947 m_inc = atop(roundup2(pa, alignment) - pa);
1950 if (rounddown2(pa ^ (pa + ptoa(npages) - 1),
1952 m_inc = atop(roundup2(pa, boundary) - pa);
1956 KASSERT(m_run != NULL, ("m_run == NULL"));
1959 * Avoid releasing and reacquiring the same page lock.
1961 new_mtx = vm_page_lockptr(m);
1962 if (m_mtx != new_mtx) {
1970 if (m->wire_count != 0 || m->hold_count != 0)
1972 #if VM_NRESERVLEVEL > 0
1973 else if ((level = vm_reserv_level(m)) >= 0 &&
1974 (options & VPSC_NORESERV) != 0) {
1976 /* Advance to the end of the reservation. */
1977 pa = VM_PAGE_TO_PHYS(m);
1978 m_inc = atop(roundup2(pa + 1, vm_reserv_size(level)) -
1982 else if ((object = m->object) != NULL) {
1984 * The page is considered eligible for relocation if
1985 * and only if it could be laundered or reclaimed by
1988 if (!VM_OBJECT_TRYRLOCK(object)) {
1990 VM_OBJECT_RLOCK(object);
1992 if (m->object != object) {
1994 * The page may have been freed.
1996 VM_OBJECT_RUNLOCK(object);
1998 } else if (m->wire_count != 0 ||
1999 m->hold_count != 0) {
2004 KASSERT((m->flags & PG_UNHOLDFREE) == 0,
2005 ("page %p is PG_UNHOLDFREE", m));
2006 /* Don't care: PG_NODUMP, PG_ZERO. */
2007 if (object->type != OBJT_DEFAULT &&
2008 object->type != OBJT_SWAP &&
2009 object->type != OBJT_VNODE) {
2011 #if VM_NRESERVLEVEL > 0
2012 } else if ((options & VPSC_NOSUPER) != 0 &&
2013 (level = vm_reserv_level_iffullpop(m)) >= 0) {
2015 /* Advance to the end of the superpage. */
2016 pa = VM_PAGE_TO_PHYS(m);
2017 m_inc = atop(roundup2(pa + 1,
2018 vm_reserv_size(level)) - pa);
2020 } else if (object->memattr == VM_MEMATTR_DEFAULT &&
2021 m->queue != PQ_NONE && !vm_page_busied(m)) {
2023 * The page is allocated but eligible for
2024 * relocation. Extend the current run by one
2027 KASSERT(pmap_page_get_memattr(m) ==
2029 ("page %p has an unexpected memattr", m));
2030 KASSERT((m->oflags & (VPO_SWAPINPROG |
2031 VPO_SWAPSLEEP | VPO_UNMANAGED)) == 0,
2032 ("page %p has unexpected oflags", m));
2033 /* Don't care: VPO_NOSYNC. */
2038 VM_OBJECT_RUNLOCK(object);
2039 #if VM_NRESERVLEVEL > 0
2040 } else if (level >= 0) {
2042 * The page is reserved but not yet allocated. In
2043 * other words, it is still free. Extend the current
2048 } else if ((order = m->order) < VM_NFREEORDER) {
2050 * The page is enqueued in the physical memory
2051 * allocator's free page queues. Moreover, it is the
2052 * first page in a power-of-two-sized run of
2053 * contiguous free pages. Add these pages to the end
2054 * of the current run, and jump ahead.
2056 run_ext = 1 << order;
2060 * Skip the page for one of the following reasons: (1)
2061 * It is enqueued in the physical memory allocator's
2062 * free page queues. However, it is not the first
2063 * page in a run of contiguous free pages. (This case
2064 * rarely occurs because the scan is performed in
2065 * ascending order.) (2) It is not reserved, and it is
2066 * transitioning from free to allocated. (Conversely,
2067 * the transition from allocated to free for managed
2068 * pages is blocked by the page lock.) (3) It is
2069 * allocated but not contained by an object and not
2070 * wired, e.g., allocated by Xen's balloon driver.
2076 * Extend or reset the current run of pages.
2091 if (run_len >= npages)
2097 * vm_page_reclaim_run:
2099 * Try to relocate each of the allocated virtual pages within the
2100 * specified run of physical pages to a new physical address. Free the
2101 * physical pages underlying the relocated virtual pages. A virtual page
2102 * is relocatable if and only if it could be laundered or reclaimed by
2103 * the page daemon. Whenever possible, a virtual page is relocated to a
2104 * physical address above "high".
2106 * Returns 0 if every physical page within the run was already free or
2107 * just freed by a successful relocation. Otherwise, returns a non-zero
2108 * value indicating why the last attempt to relocate a virtual page was
2111 * "req_class" must be an allocation class.
2114 vm_page_reclaim_run(int req_class, u_long npages, vm_page_t m_run,
2117 struct mtx *m_mtx, *new_mtx;
2118 struct spglist free;
2121 vm_page_t m, m_end, m_new;
2122 int error, order, req;
2124 KASSERT((req_class & VM_ALLOC_CLASS_MASK) == req_class,
2125 ("req_class is not an allocation class"));
2129 m_end = m_run + npages;
2131 for (; error == 0 && m < m_end; m++) {
2132 KASSERT((m->flags & (PG_FICTITIOUS | PG_MARKER)) == 0,
2133 ("page %p is PG_FICTITIOUS or PG_MARKER", m));
2136 * Avoid releasing and reacquiring the same page lock.
2138 new_mtx = vm_page_lockptr(m);
2139 if (m_mtx != new_mtx) {
2146 if (m->wire_count != 0 || m->hold_count != 0)
2148 else if ((object = m->object) != NULL) {
2150 * The page is relocated if and only if it could be
2151 * laundered or reclaimed by the page daemon.
2153 if (!VM_OBJECT_TRYWLOCK(object)) {
2155 VM_OBJECT_WLOCK(object);
2157 if (m->object != object) {
2159 * The page may have been freed.
2161 VM_OBJECT_WUNLOCK(object);
2163 } else if (m->wire_count != 0 ||
2164 m->hold_count != 0) {
2169 KASSERT((m->flags & PG_UNHOLDFREE) == 0,
2170 ("page %p is PG_UNHOLDFREE", m));
2171 /* Don't care: PG_NODUMP, PG_ZERO. */
2172 if (object->type != OBJT_DEFAULT &&
2173 object->type != OBJT_SWAP &&
2174 object->type != OBJT_VNODE)
2176 else if (object->memattr != VM_MEMATTR_DEFAULT)
2178 else if (m->queue != PQ_NONE && !vm_page_busied(m)) {
2179 KASSERT(pmap_page_get_memattr(m) ==
2181 ("page %p has an unexpected memattr", m));
2182 KASSERT((m->oflags & (VPO_SWAPINPROG |
2183 VPO_SWAPSLEEP | VPO_UNMANAGED)) == 0,
2184 ("page %p has unexpected oflags", m));
2185 /* Don't care: VPO_NOSYNC. */
2186 if (m->valid != 0) {
2188 * First, try to allocate a new page
2189 * that is above "high". Failing
2190 * that, try to allocate a new page
2191 * that is below "m_run". Allocate
2192 * the new page between the end of
2193 * "m_run" and "high" only as a last
2196 req = req_class | VM_ALLOC_NOOBJ;
2197 if ((m->flags & PG_NODUMP) != 0)
2198 req |= VM_ALLOC_NODUMP;
2199 if (trunc_page(high) !=
2200 ~(vm_paddr_t)PAGE_MASK) {
2201 m_new = vm_page_alloc_contig(
2206 VM_MEMATTR_DEFAULT);
2209 if (m_new == NULL) {
2210 pa = VM_PAGE_TO_PHYS(m_run);
2211 m_new = vm_page_alloc_contig(
2213 0, pa - 1, PAGE_SIZE, 0,
2214 VM_MEMATTR_DEFAULT);
2216 if (m_new == NULL) {
2218 m_new = vm_page_alloc_contig(
2220 pa, high, PAGE_SIZE, 0,
2221 VM_MEMATTR_DEFAULT);
2223 if (m_new == NULL) {
2227 KASSERT(m_new->wire_count == 0,
2228 ("page %p is wired", m));
2231 * Replace "m" with the new page. For
2232 * vm_page_replace(), "m" must be busy
2233 * and dequeued. Finally, change "m"
2234 * as if vm_page_free() was called.
2236 if (object->ref_count != 0)
2238 m_new->aflags = m->aflags;
2239 KASSERT(m_new->oflags == VPO_UNMANAGED,
2240 ("page %p is managed", m));
2241 m_new->oflags = m->oflags & VPO_NOSYNC;
2242 pmap_copy_page(m, m_new);
2243 m_new->valid = m->valid;
2244 m_new->dirty = m->dirty;
2245 m->flags &= ~PG_ZERO;
2248 vm_page_replace_checked(m_new, object,
2254 * The new page must be deactivated
2255 * before the object is unlocked.
2257 new_mtx = vm_page_lockptr(m_new);
2258 if (m_mtx != new_mtx) {
2263 vm_page_deactivate(m_new);
2265 m->flags &= ~PG_ZERO;
2268 KASSERT(m->dirty == 0,
2269 ("page %p is dirty", m));
2271 SLIST_INSERT_HEAD(&free, m, plinks.s.ss);
2275 VM_OBJECT_WUNLOCK(object);
2277 mtx_lock(&vm_page_queue_free_mtx);
2279 if (order < VM_NFREEORDER) {
2281 * The page is enqueued in the physical memory
2282 * allocator's free page queues. Moreover, it
2283 * is the first page in a power-of-two-sized
2284 * run of contiguous free pages. Jump ahead
2285 * to the last page within that run, and
2286 * continue from there.
2288 m += (1 << order) - 1;
2290 #if VM_NRESERVLEVEL > 0
2291 else if (vm_reserv_is_page_free(m))
2294 mtx_unlock(&vm_page_queue_free_mtx);
2295 if (order == VM_NFREEORDER)
2301 if ((m = SLIST_FIRST(&free)) != NULL) {
2302 mtx_lock(&vm_page_queue_free_mtx);
2304 SLIST_REMOVE_HEAD(&free, plinks.s.ss);
2305 vm_phys_freecnt_adj(m, 1);
2306 #if VM_NRESERVLEVEL > 0
2307 if (!vm_reserv_free_page(m))
2311 vm_phys_free_pages(m, 0);
2312 } while ((m = SLIST_FIRST(&free)) != NULL);
2313 vm_page_free_wakeup();
2314 mtx_unlock(&vm_page_queue_free_mtx);
2321 CTASSERT(powerof2(NRUNS));
2323 #define RUN_INDEX(count) ((count) & (NRUNS - 1))
2325 #define MIN_RECLAIM 8
2328 * vm_page_reclaim_contig:
2330 * Reclaim allocated, contiguous physical memory satisfying the specified
2331 * conditions by relocating the virtual pages using that physical memory.
2332 * Returns true if reclamation is successful and false otherwise. Since
2333 * relocation requires the allocation of physical pages, reclamation may
2334 * fail due to a shortage of free pages. When reclamation fails, callers
2335 * are expected to perform VM_WAIT before retrying a failed allocation
2336 * operation, e.g., vm_page_alloc_contig().
2338 * The caller must always specify an allocation class through "req".
2340 * allocation classes:
2341 * VM_ALLOC_NORMAL normal process request
2342 * VM_ALLOC_SYSTEM system *really* needs a page
2343 * VM_ALLOC_INTERRUPT interrupt time request
2345 * The optional allocation flags are ignored.
2347 * "npages" must be greater than zero. Both "alignment" and "boundary"
2348 * must be a power of two.
2351 vm_page_reclaim_contig(int req, u_long npages, vm_paddr_t low, vm_paddr_t high,
2352 u_long alignment, vm_paddr_t boundary)
2354 vm_paddr_t curr_low;
2355 vm_page_t m_run, m_runs[NRUNS];
2356 u_long count, reclaimed;
2357 int error, i, options, req_class;
2359 KASSERT(npages > 0, ("npages is 0"));
2360 KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
2361 KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
2362 req_class = req & VM_ALLOC_CLASS_MASK;
2365 * The page daemon is allowed to dig deeper into the free page list.
2367 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
2368 req_class = VM_ALLOC_SYSTEM;
2371 * Return if the number of free pages cannot satisfy the requested
2374 count = vm_cnt.v_free_count;
2375 if (count < npages + vm_cnt.v_free_reserved || (count < npages +
2376 vm_cnt.v_interrupt_free_min && req_class == VM_ALLOC_SYSTEM) ||
2377 (count < npages && req_class == VM_ALLOC_INTERRUPT))
2381 * Scan up to three times, relaxing the restrictions ("options") on
2382 * the reclamation of reservations and superpages each time.
2384 for (options = VPSC_NORESERV;;) {
2386 * Find the highest runs that satisfy the given constraints
2387 * and restrictions, and record them in "m_runs".
2392 m_run = vm_phys_scan_contig(npages, curr_low, high,
2393 alignment, boundary, options);
2396 curr_low = VM_PAGE_TO_PHYS(m_run) + ptoa(npages);
2397 m_runs[RUN_INDEX(count)] = m_run;
2402 * Reclaim the highest runs in LIFO (descending) order until
2403 * the number of reclaimed pages, "reclaimed", is at least
2404 * MIN_RECLAIM. Reset "reclaimed" each time because each
2405 * reclamation is idempotent, and runs will (likely) recur
2406 * from one scan to the next as restrictions are relaxed.
2409 for (i = 0; count > 0 && i < NRUNS; i++) {
2411 m_run = m_runs[RUN_INDEX(count)];
2412 error = vm_page_reclaim_run(req_class, npages, m_run,
2415 reclaimed += npages;
2416 if (reclaimed >= MIN_RECLAIM)
2422 * Either relax the restrictions on the next scan or return if
2423 * the last scan had no restrictions.
2425 if (options == VPSC_NORESERV)
2426 options = VPSC_NOSUPER;
2427 else if (options == VPSC_NOSUPER)
2429 else if (options == VPSC_ANY)
2430 return (reclaimed != 0);
2435 * vm_wait: (also see VM_WAIT macro)
2437 * Sleep until free pages are available for allocation.
2438 * - Called in various places before memory allocations.
2444 mtx_lock(&vm_page_queue_free_mtx);
2445 if (curproc == pageproc) {
2446 vm_pageout_pages_needed = 1;
2447 msleep(&vm_pageout_pages_needed, &vm_page_queue_free_mtx,
2448 PDROP | PSWP, "VMWait", 0);
2450 if (__predict_false(pageproc == NULL))
2451 panic("vm_wait in early boot");
2452 if (!vm_pageout_wanted) {
2453 vm_pageout_wanted = true;
2454 wakeup(&vm_pageout_wanted);
2456 vm_pages_needed = true;
2457 msleep(&vm_cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PVM,
2463 * vm_waitpfault: (also see VM_WAITPFAULT macro)
2465 * Sleep until free pages are available for allocation.
2466 * - Called only in vm_fault so that processes page faulting
2467 * can be easily tracked.
2468 * - Sleeps at a lower priority than vm_wait() so that vm_wait()ing
2469 * processes will be able to grab memory first. Do not change
2470 * this balance without careful testing first.
2476 mtx_lock(&vm_page_queue_free_mtx);
2477 if (!vm_pageout_wanted) {
2478 vm_pageout_wanted = true;
2479 wakeup(&vm_pageout_wanted);
2481 vm_pages_needed = true;
2482 msleep(&vm_cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PUSER,
2486 struct vm_pagequeue *
2487 vm_page_pagequeue(vm_page_t m)
2490 if (vm_page_in_laundry(m))
2491 return (&vm_dom[0].vmd_pagequeues[m->queue]);
2493 return (&vm_phys_domain(m)->vmd_pagequeues[m->queue]);
2499 * Remove the given page from its current page queue.
2501 * The page must be locked.
2504 vm_page_dequeue(vm_page_t m)
2506 struct vm_pagequeue *pq;
2508 vm_page_assert_locked(m);
2509 KASSERT(m->queue < PQ_COUNT, ("vm_page_dequeue: page %p is not queued",
2511 pq = vm_page_pagequeue(m);
2512 vm_pagequeue_lock(pq);
2514 TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
2515 vm_pagequeue_cnt_dec(pq);
2516 vm_pagequeue_unlock(pq);
2520 * vm_page_dequeue_locked:
2522 * Remove the given page from its current page queue.
2524 * The page and page queue must be locked.
2527 vm_page_dequeue_locked(vm_page_t m)
2529 struct vm_pagequeue *pq;
2531 vm_page_lock_assert(m, MA_OWNED);
2532 pq = vm_page_pagequeue(m);
2533 vm_pagequeue_assert_locked(pq);
2535 TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
2536 vm_pagequeue_cnt_dec(pq);
2542 * Add the given page to the specified page queue.
2544 * The page must be locked.
2547 vm_page_enqueue(uint8_t queue, vm_page_t m)
2549 struct vm_pagequeue *pq;
2551 vm_page_lock_assert(m, MA_OWNED);
2552 KASSERT(queue < PQ_COUNT,
2553 ("vm_page_enqueue: invalid queue %u request for page %p",
2555 if (queue == PQ_LAUNDRY)
2556 pq = &vm_dom[0].vmd_pagequeues[queue];
2558 pq = &vm_phys_domain(m)->vmd_pagequeues[queue];
2559 vm_pagequeue_lock(pq);
2561 TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
2562 vm_pagequeue_cnt_inc(pq);
2563 vm_pagequeue_unlock(pq);
2569 * Move the given page to the tail of its current page queue.
2571 * The page must be locked.
2574 vm_page_requeue(vm_page_t m)
2576 struct vm_pagequeue *pq;
2578 vm_page_lock_assert(m, MA_OWNED);
2579 KASSERT(m->queue != PQ_NONE,
2580 ("vm_page_requeue: page %p is not queued", m));
2581 pq = vm_page_pagequeue(m);
2582 vm_pagequeue_lock(pq);
2583 TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
2584 TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
2585 vm_pagequeue_unlock(pq);
2589 * vm_page_requeue_locked:
2591 * Move the given page to the tail of its current page queue.
2593 * The page queue must be locked.
2596 vm_page_requeue_locked(vm_page_t m)
2598 struct vm_pagequeue *pq;
2600 KASSERT(m->queue != PQ_NONE,
2601 ("vm_page_requeue_locked: page %p is not queued", m));
2602 pq = vm_page_pagequeue(m);
2603 vm_pagequeue_assert_locked(pq);
2604 TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
2605 TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
2611 * Put the specified page on the active list (if appropriate).
2612 * Ensure that act_count is at least ACT_INIT but do not otherwise
2615 * The page must be locked.
2618 vm_page_activate(vm_page_t m)
2622 vm_page_lock_assert(m, MA_OWNED);
2623 if ((queue = m->queue) != PQ_ACTIVE) {
2624 if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) {
2625 if (m->act_count < ACT_INIT)
2626 m->act_count = ACT_INIT;
2627 if (queue != PQ_NONE)
2629 vm_page_enqueue(PQ_ACTIVE, m);
2631 KASSERT(queue == PQ_NONE,
2632 ("vm_page_activate: wired page %p is queued", m));
2634 if (m->act_count < ACT_INIT)
2635 m->act_count = ACT_INIT;
2640 * vm_page_free_wakeup:
2642 * Helper routine for vm_page_free_toq(). This routine is called
2643 * when a page is added to the free queues.
2645 * The page queues must be locked.
2648 vm_page_free_wakeup(void)
2651 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
2653 * if pageout daemon needs pages, then tell it that there are
2656 if (vm_pageout_pages_needed &&
2657 vm_cnt.v_free_count >= vm_cnt.v_pageout_free_min) {
2658 wakeup(&vm_pageout_pages_needed);
2659 vm_pageout_pages_needed = 0;
2662 * wakeup processes that are waiting on memory if we hit a
2663 * high water mark. And wakeup scheduler process if we have
2664 * lots of memory. this process will swapin processes.
2666 if (vm_pages_needed && !vm_page_count_min()) {
2667 vm_pages_needed = false;
2668 wakeup(&vm_cnt.v_free_count);
2675 * Returns the given page to the free list,
2676 * disassociating it with any VM object.
2678 * The object must be locked. The page must be locked if it is managed.
2681 vm_page_free_toq(vm_page_t m)
2684 if ((m->oflags & VPO_UNMANAGED) == 0) {
2685 vm_page_lock_assert(m, MA_OWNED);
2686 KASSERT(!pmap_page_is_mapped(m),
2687 ("vm_page_free_toq: freeing mapped page %p", m));
2689 KASSERT(m->queue == PQ_NONE,
2690 ("vm_page_free_toq: unmanaged page %p is queued", m));
2691 PCPU_INC(cnt.v_tfree);
2693 if (vm_page_sbusied(m))
2694 panic("vm_page_free: freeing busy page %p", m);
2697 * Unqueue, then remove page. Note that we cannot destroy
2698 * the page here because we do not want to call the pager's
2699 * callback routine until after we've put the page on the
2700 * appropriate free queue.
2706 * If fictitious remove object association and
2707 * return, otherwise delay object association removal.
2709 if ((m->flags & PG_FICTITIOUS) != 0) {
2716 if (m->wire_count != 0)
2717 panic("vm_page_free: freeing wired page %p", m);
2718 if (m->hold_count != 0) {
2719 m->flags &= ~PG_ZERO;
2720 KASSERT((m->flags & PG_UNHOLDFREE) == 0,
2721 ("vm_page_free: freeing PG_UNHOLDFREE page %p", m));
2722 m->flags |= PG_UNHOLDFREE;
2725 * Restore the default memory attribute to the page.
2727 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT)
2728 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT);
2731 * Insert the page into the physical memory allocator's free
2734 mtx_lock(&vm_page_queue_free_mtx);
2735 vm_phys_freecnt_adj(m, 1);
2736 #if VM_NRESERVLEVEL > 0
2737 if (!vm_reserv_free_page(m))
2741 vm_phys_free_pages(m, 0);
2742 vm_page_free_wakeup();
2743 mtx_unlock(&vm_page_queue_free_mtx);
2750 * Mark this page as wired down by yet
2751 * another map, removing it from paging queues
2754 * If the page is fictitious, then its wire count must remain one.
2756 * The page must be locked.
2759 vm_page_wire(vm_page_t m)
2763 * Only bump the wire statistics if the page is not already wired,
2764 * and only unqueue the page if it is on some queue (if it is unmanaged
2765 * it is already off the queues).
2767 vm_page_lock_assert(m, MA_OWNED);
2768 if ((m->flags & PG_FICTITIOUS) != 0) {
2769 KASSERT(m->wire_count == 1,
2770 ("vm_page_wire: fictitious page %p's wire count isn't one",
2774 if (m->wire_count == 0) {
2775 KASSERT((m->oflags & VPO_UNMANAGED) == 0 ||
2776 m->queue == PQ_NONE,
2777 ("vm_page_wire: unmanaged page %p is queued", m));
2779 atomic_add_int(&vm_cnt.v_wire_count, 1);
2782 KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m));
2788 * Release one wiring of the specified page, potentially allowing it to be
2789 * paged out. Returns TRUE if the number of wirings transitions to zero and
2792 * Only managed pages belonging to an object can be paged out. If the number
2793 * of wirings transitions to zero and the page is eligible for page out, then
2794 * the page is added to the specified paging queue (unless PQ_NONE is
2797 * If a page is fictitious, then its wire count must always be one.
2799 * A managed page must be locked.
2802 vm_page_unwire(vm_page_t m, uint8_t queue)
2805 KASSERT(queue < PQ_COUNT || queue == PQ_NONE,
2806 ("vm_page_unwire: invalid queue %u request for page %p",
2808 if ((m->oflags & VPO_UNMANAGED) == 0)
2809 vm_page_assert_locked(m);
2810 if ((m->flags & PG_FICTITIOUS) != 0) {
2811 KASSERT(m->wire_count == 1,
2812 ("vm_page_unwire: fictitious page %p's wire count isn't one", m));
2815 if (m->wire_count > 0) {
2817 if (m->wire_count == 0) {
2818 atomic_subtract_int(&vm_cnt.v_wire_count, 1);
2819 if ((m->oflags & VPO_UNMANAGED) == 0 &&
2820 m->object != NULL && queue != PQ_NONE)
2821 vm_page_enqueue(queue, m);
2826 panic("vm_page_unwire: page %p's wire count is zero", m);
2830 * Move the specified page to the inactive queue.
2832 * Normally, "noreuse" is FALSE, resulting in LRU ordering of the inactive
2833 * queue. However, setting "noreuse" to TRUE will accelerate the specified
2834 * page's reclamation, but it will not unmap the page from any address space.
2835 * This is implemented by inserting the page near the head of the inactive
2836 * queue, using a marker page to guide FIFO insertion ordering.
2838 * The page must be locked.
2841 _vm_page_deactivate(vm_page_t m, boolean_t noreuse)
2843 struct vm_pagequeue *pq;
2846 vm_page_assert_locked(m);
2849 * Ignore if the page is already inactive, unless it is unlikely to be
2852 if ((queue = m->queue) == PQ_INACTIVE && !noreuse)
2854 if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) {
2855 pq = &vm_phys_domain(m)->vmd_pagequeues[PQ_INACTIVE];
2856 /* Avoid multiple acquisitions of the inactive queue lock. */
2857 if (queue == PQ_INACTIVE) {
2858 vm_pagequeue_lock(pq);
2859 vm_page_dequeue_locked(m);
2861 if (queue != PQ_NONE)
2863 vm_pagequeue_lock(pq);
2865 m->queue = PQ_INACTIVE;
2867 TAILQ_INSERT_BEFORE(&vm_phys_domain(m)->vmd_inacthead,
2870 TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
2871 vm_pagequeue_cnt_inc(pq);
2872 vm_pagequeue_unlock(pq);
2877 * Move the specified page to the inactive queue.
2879 * The page must be locked.
2882 vm_page_deactivate(vm_page_t m)
2885 _vm_page_deactivate(m, FALSE);
2889 * Move the specified page to the inactive queue with the expectation
2890 * that it is unlikely to be reused.
2892 * The page must be locked.
2895 vm_page_deactivate_noreuse(vm_page_t m)
2898 _vm_page_deactivate(m, TRUE);
2904 * Put a page in the laundry.
2907 vm_page_launder(vm_page_t m)
2911 vm_page_assert_locked(m);
2912 if ((queue = m->queue) != PQ_LAUNDRY) {
2913 if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) {
2914 if (queue != PQ_NONE)
2916 vm_page_enqueue(PQ_LAUNDRY, m);
2918 KASSERT(queue == PQ_NONE,
2919 ("wired page %p is queued", m));
2924 * vm_page_try_to_free()
2926 * Attempt to free the page. If we cannot free it, we do nothing.
2927 * 1 is returned on success, 0 on failure.
2930 vm_page_try_to_free(vm_page_t m)
2933 vm_page_lock_assert(m, MA_OWNED);
2934 if (m->object != NULL)
2935 VM_OBJECT_ASSERT_WLOCKED(m->object);
2936 if (m->dirty || m->hold_count || m->wire_count ||
2937 (m->oflags & VPO_UNMANAGED) != 0 || vm_page_busied(m))
2949 * Deactivate or do nothing, as appropriate.
2951 * The object and page must be locked.
2954 vm_page_advise(vm_page_t m, int advice)
2957 vm_page_assert_locked(m);
2958 VM_OBJECT_ASSERT_WLOCKED(m->object);
2959 if (advice == MADV_FREE)
2961 * Mark the page clean. This will allow the page to be freed
2962 * without first paging it out. MADV_FREE pages are often
2963 * quickly reused by malloc(3), so we do not do anything that
2964 * would result in a page fault on a later access.
2967 else if (advice != MADV_DONTNEED)
2971 * Clear any references to the page. Otherwise, the page daemon will
2972 * immediately reactivate the page.
2974 vm_page_aflag_clear(m, PGA_REFERENCED);
2976 if (advice != MADV_FREE && m->dirty == 0 && pmap_is_modified(m))
2980 * Place clean pages near the head of the inactive queue rather than
2981 * the tail, thus defeating the queue's LRU operation and ensuring that
2982 * the page will be reused quickly. Dirty pages not already in the
2983 * laundry are moved there.
2986 vm_page_deactivate_noreuse(m);
2992 * Grab a page, waiting until we are waken up due to the page
2993 * changing state. We keep on waiting, if the page continues
2994 * to be in the object. If the page doesn't exist, first allocate it
2995 * and then conditionally zero it.
2997 * This routine may sleep.
2999 * The object must be locked on entry. The lock will, however, be released
3000 * and reacquired if the routine sleeps.
3003 vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags)
3008 VM_OBJECT_ASSERT_WLOCKED(object);
3009 KASSERT((allocflags & VM_ALLOC_SBUSY) == 0 ||
3010 (allocflags & VM_ALLOC_IGN_SBUSY) != 0,
3011 ("vm_page_grab: VM_ALLOC_SBUSY/VM_ALLOC_IGN_SBUSY mismatch"));
3013 if ((m = vm_page_lookup(object, pindex)) != NULL) {
3014 sleep = (allocflags & VM_ALLOC_IGN_SBUSY) != 0 ?
3015 vm_page_xbusied(m) : vm_page_busied(m);
3017 if ((allocflags & VM_ALLOC_NOWAIT) != 0)
3020 * Reference the page before unlocking and
3021 * sleeping so that the page daemon is less
3022 * likely to reclaim it.
3024 vm_page_aflag_set(m, PGA_REFERENCED);
3026 VM_OBJECT_WUNLOCK(object);
3027 vm_page_busy_sleep(m, "pgrbwt", (allocflags &
3028 VM_ALLOC_IGN_SBUSY) != 0);
3029 VM_OBJECT_WLOCK(object);
3032 if ((allocflags & VM_ALLOC_WIRED) != 0) {
3038 (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) == 0)
3040 if ((allocflags & VM_ALLOC_SBUSY) != 0)
3045 m = vm_page_alloc(object, pindex, allocflags);
3047 if ((allocflags & VM_ALLOC_NOWAIT) != 0)
3049 VM_OBJECT_WUNLOCK(object);
3051 VM_OBJECT_WLOCK(object);
3054 if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0)
3060 * Mapping function for valid or dirty bits in a page.
3062 * Inputs are required to range within a page.
3065 vm_page_bits(int base, int size)
3071 base + size <= PAGE_SIZE,
3072 ("vm_page_bits: illegal base/size %d/%d", base, size)
3075 if (size == 0) /* handle degenerate case */
3078 first_bit = base >> DEV_BSHIFT;
3079 last_bit = (base + size - 1) >> DEV_BSHIFT;
3081 return (((vm_page_bits_t)2 << last_bit) -
3082 ((vm_page_bits_t)1 << first_bit));
3086 * vm_page_set_valid_range:
3088 * Sets portions of a page valid. The arguments are expected
3089 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
3090 * of any partial chunks touched by the range. The invalid portion of
3091 * such chunks will be zeroed.
3093 * (base + size) must be less then or equal to PAGE_SIZE.
3096 vm_page_set_valid_range(vm_page_t m, int base, int size)
3100 VM_OBJECT_ASSERT_WLOCKED(m->object);
3101 if (size == 0) /* handle degenerate case */
3105 * If the base is not DEV_BSIZE aligned and the valid
3106 * bit is clear, we have to zero out a portion of the
3109 if ((frag = rounddown2(base, DEV_BSIZE)) != base &&
3110 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0)
3111 pmap_zero_page_area(m, frag, base - frag);
3114 * If the ending offset is not DEV_BSIZE aligned and the
3115 * valid bit is clear, we have to zero out a portion of
3118 endoff = base + size;
3119 if ((frag = rounddown2(endoff, DEV_BSIZE)) != endoff &&
3120 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0)
3121 pmap_zero_page_area(m, endoff,
3122 DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
3125 * Assert that no previously invalid block that is now being validated
3128 KASSERT((~m->valid & vm_page_bits(base, size) & m->dirty) == 0,
3129 ("vm_page_set_valid_range: page %p is dirty", m));
3132 * Set valid bits inclusive of any overlap.
3134 m->valid |= vm_page_bits(base, size);
3138 * Clear the given bits from the specified page's dirty field.
3140 static __inline void
3141 vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits)
3144 #if PAGE_SIZE < 16384
3149 * If the object is locked and the page is neither exclusive busy nor
3150 * write mapped, then the page's dirty field cannot possibly be
3151 * set by a concurrent pmap operation.
3153 VM_OBJECT_ASSERT_WLOCKED(m->object);
3154 if (!vm_page_xbusied(m) && !pmap_page_is_write_mapped(m))
3155 m->dirty &= ~pagebits;
3158 * The pmap layer can call vm_page_dirty() without
3159 * holding a distinguished lock. The combination of
3160 * the object's lock and an atomic operation suffice
3161 * to guarantee consistency of the page dirty field.
3163 * For PAGE_SIZE == 32768 case, compiler already
3164 * properly aligns the dirty field, so no forcible
3165 * alignment is needed. Only require existence of
3166 * atomic_clear_64 when page size is 32768.
3168 addr = (uintptr_t)&m->dirty;
3169 #if PAGE_SIZE == 32768
3170 atomic_clear_64((uint64_t *)addr, pagebits);
3171 #elif PAGE_SIZE == 16384
3172 atomic_clear_32((uint32_t *)addr, pagebits);
3173 #else /* PAGE_SIZE <= 8192 */
3175 * Use a trick to perform a 32-bit atomic on the
3176 * containing aligned word, to not depend on the existence
3177 * of atomic_clear_{8, 16}.
3179 shift = addr & (sizeof(uint32_t) - 1);
3180 #if BYTE_ORDER == BIG_ENDIAN
3181 shift = (sizeof(uint32_t) - sizeof(m->dirty) - shift) * NBBY;
3185 addr &= ~(sizeof(uint32_t) - 1);
3186 atomic_clear_32((uint32_t *)addr, pagebits << shift);
3187 #endif /* PAGE_SIZE */
3192 * vm_page_set_validclean:
3194 * Sets portions of a page valid and clean. The arguments are expected
3195 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
3196 * of any partial chunks touched by the range. The invalid portion of
3197 * such chunks will be zero'd.
3199 * (base + size) must be less then or equal to PAGE_SIZE.
3202 vm_page_set_validclean(vm_page_t m, int base, int size)
3204 vm_page_bits_t oldvalid, pagebits;
3207 VM_OBJECT_ASSERT_WLOCKED(m->object);
3208 if (size == 0) /* handle degenerate case */
3212 * If the base is not DEV_BSIZE aligned and the valid
3213 * bit is clear, we have to zero out a portion of the
3216 if ((frag = rounddown2(base, DEV_BSIZE)) != base &&
3217 (m->valid & ((vm_page_bits_t)1 << (base >> DEV_BSHIFT))) == 0)
3218 pmap_zero_page_area(m, frag, base - frag);
3221 * If the ending offset is not DEV_BSIZE aligned and the
3222 * valid bit is clear, we have to zero out a portion of
3225 endoff = base + size;
3226 if ((frag = rounddown2(endoff, DEV_BSIZE)) != endoff &&
3227 (m->valid & ((vm_page_bits_t)1 << (endoff >> DEV_BSHIFT))) == 0)
3228 pmap_zero_page_area(m, endoff,
3229 DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
3232 * Set valid, clear dirty bits. If validating the entire
3233 * page we can safely clear the pmap modify bit. We also
3234 * use this opportunity to clear the VPO_NOSYNC flag. If a process
3235 * takes a write fault on a MAP_NOSYNC memory area the flag will
3238 * We set valid bits inclusive of any overlap, but we can only
3239 * clear dirty bits for DEV_BSIZE chunks that are fully within
3242 oldvalid = m->valid;
3243 pagebits = vm_page_bits(base, size);
3244 m->valid |= pagebits;
3246 if ((frag = base & (DEV_BSIZE - 1)) != 0) {
3247 frag = DEV_BSIZE - frag;
3253 pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1));
3255 if (base == 0 && size == PAGE_SIZE) {
3257 * The page can only be modified within the pmap if it is
3258 * mapped, and it can only be mapped if it was previously
3261 if (oldvalid == VM_PAGE_BITS_ALL)
3263 * Perform the pmap_clear_modify() first. Otherwise,
3264 * a concurrent pmap operation, such as
3265 * pmap_protect(), could clear a modification in the
3266 * pmap and set the dirty field on the page before
3267 * pmap_clear_modify() had begun and after the dirty
3268 * field was cleared here.
3270 pmap_clear_modify(m);
3272 m->oflags &= ~VPO_NOSYNC;
3273 } else if (oldvalid != VM_PAGE_BITS_ALL)
3274 m->dirty &= ~pagebits;
3276 vm_page_clear_dirty_mask(m, pagebits);
3280 vm_page_clear_dirty(vm_page_t m, int base, int size)
3283 vm_page_clear_dirty_mask(m, vm_page_bits(base, size));
3287 * vm_page_set_invalid:
3289 * Invalidates DEV_BSIZE'd chunks within a page. Both the
3290 * valid and dirty bits for the effected areas are cleared.
3293 vm_page_set_invalid(vm_page_t m, int base, int size)
3295 vm_page_bits_t bits;
3299 VM_OBJECT_ASSERT_WLOCKED(object);
3300 if (object->type == OBJT_VNODE && base == 0 && IDX_TO_OFF(m->pindex) +
3301 size >= object->un_pager.vnp.vnp_size)
3302 bits = VM_PAGE_BITS_ALL;
3304 bits = vm_page_bits(base, size);
3305 if (object->ref_count != 0 && m->valid == VM_PAGE_BITS_ALL &&
3308 KASSERT((bits == 0 && m->valid == VM_PAGE_BITS_ALL) ||
3309 !pmap_page_is_mapped(m),
3310 ("vm_page_set_invalid: page %p is mapped", m));
3316 * vm_page_zero_invalid()
3318 * The kernel assumes that the invalid portions of a page contain
3319 * garbage, but such pages can be mapped into memory by user code.
3320 * When this occurs, we must zero out the non-valid portions of the
3321 * page so user code sees what it expects.
3323 * Pages are most often semi-valid when the end of a file is mapped
3324 * into memory and the file's size is not page aligned.
3327 vm_page_zero_invalid(vm_page_t m, boolean_t setvalid)
3332 VM_OBJECT_ASSERT_WLOCKED(m->object);
3334 * Scan the valid bits looking for invalid sections that
3335 * must be zeroed. Invalid sub-DEV_BSIZE'd areas ( where the
3336 * valid bit may be set ) have already been zeroed by
3337 * vm_page_set_validclean().
3339 for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) {
3340 if (i == (PAGE_SIZE / DEV_BSIZE) ||
3341 (m->valid & ((vm_page_bits_t)1 << i))) {
3343 pmap_zero_page_area(m,
3344 b << DEV_BSHIFT, (i - b) << DEV_BSHIFT);
3351 * setvalid is TRUE when we can safely set the zero'd areas
3352 * as being valid. We can do this if there are no cache consistancy
3353 * issues. e.g. it is ok to do with UFS, but not ok to do with NFS.
3356 m->valid = VM_PAGE_BITS_ALL;
3362 * Is (partial) page valid? Note that the case where size == 0
3363 * will return FALSE in the degenerate case where the page is
3364 * entirely invalid, and TRUE otherwise.
3367 vm_page_is_valid(vm_page_t m, int base, int size)
3369 vm_page_bits_t bits;
3371 VM_OBJECT_ASSERT_LOCKED(m->object);
3372 bits = vm_page_bits(base, size);
3373 return (m->valid != 0 && (m->valid & bits) == bits);
3377 * vm_page_ps_is_valid:
3379 * Returns TRUE if the entire (super)page is valid and FALSE otherwise.
3382 vm_page_ps_is_valid(vm_page_t m)
3386 VM_OBJECT_ASSERT_LOCKED(m->object);
3387 npages = atop(pagesizes[m->psind]);
3390 * The physically contiguous pages that make up a superpage, i.e., a
3391 * page with a page size index ("psind") greater than zero, will
3392 * occupy adjacent entries in vm_page_array[].
3394 for (i = 0; i < npages; i++) {
3395 if (m[i].valid != VM_PAGE_BITS_ALL)
3402 * Set the page's dirty bits if the page is modified.
3405 vm_page_test_dirty(vm_page_t m)
3408 VM_OBJECT_ASSERT_WLOCKED(m->object);
3409 if (m->dirty != VM_PAGE_BITS_ALL && pmap_is_modified(m))
3414 vm_page_lock_KBI(vm_page_t m, const char *file, int line)
3417 mtx_lock_flags_(vm_page_lockptr(m), 0, file, line);
3421 vm_page_unlock_KBI(vm_page_t m, const char *file, int line)
3424 mtx_unlock_flags_(vm_page_lockptr(m), 0, file, line);
3428 vm_page_trylock_KBI(vm_page_t m, const char *file, int line)
3431 return (mtx_trylock_flags_(vm_page_lockptr(m), 0, file, line));
3434 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
3436 vm_page_assert_locked_KBI(vm_page_t m, const char *file, int line)
3439 vm_page_lock_assert_KBI(m, MA_OWNED, file, line);
3443 vm_page_lock_assert_KBI(vm_page_t m, int a, const char *file, int line)
3446 mtx_assert_(vm_page_lockptr(m), a, file, line);
3452 vm_page_object_lock_assert(vm_page_t m)
3456 * Certain of the page's fields may only be modified by the
3457 * holder of the containing object's lock or the exclusive busy.
3458 * holder. Unfortunately, the holder of the write busy is
3459 * not recorded, and thus cannot be checked here.
3461 if (m->object != NULL && !vm_page_xbusied(m))
3462 VM_OBJECT_ASSERT_WLOCKED(m->object);
3466 vm_page_assert_pga_writeable(vm_page_t m, uint8_t bits)
3469 if ((bits & PGA_WRITEABLE) == 0)
3473 * The PGA_WRITEABLE flag can only be set if the page is
3474 * managed, is exclusively busied or the object is locked.
3475 * Currently, this flag is only set by pmap_enter().
3477 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3478 ("PGA_WRITEABLE on unmanaged page"));
3479 if (!vm_page_xbusied(m))
3480 VM_OBJECT_ASSERT_LOCKED(m->object);
3484 #include "opt_ddb.h"
3486 #include <sys/kernel.h>
3488 #include <ddb/ddb.h>
3490 DB_SHOW_COMMAND(page, vm_page_print_page_info)
3493 db_printf("vm_cnt.v_free_count: %d\n", vm_cnt.v_free_count);
3494 db_printf("vm_cnt.v_inactive_count: %d\n", vm_cnt.v_inactive_count);
3495 db_printf("vm_cnt.v_active_count: %d\n", vm_cnt.v_active_count);
3496 db_printf("vm_cnt.v_laundry_count: %d\n", vm_cnt.v_laundry_count);
3497 db_printf("vm_cnt.v_wire_count: %d\n", vm_cnt.v_wire_count);
3498 db_printf("vm_cnt.v_free_reserved: %d\n", vm_cnt.v_free_reserved);
3499 db_printf("vm_cnt.v_free_min: %d\n", vm_cnt.v_free_min);
3500 db_printf("vm_cnt.v_free_target: %d\n", vm_cnt.v_free_target);
3501 db_printf("vm_cnt.v_inactive_target: %d\n", vm_cnt.v_inactive_target);
3504 DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info)
3508 db_printf("pq_free %d\n", vm_cnt.v_free_count);
3509 for (dom = 0; dom < vm_ndomains; dom++) {
3511 "dom %d page_cnt %d free %d pq_act %d pq_inact %d pq_laund %d\n",
3513 vm_dom[dom].vmd_page_count,
3514 vm_dom[dom].vmd_free_count,
3515 vm_dom[dom].vmd_pagequeues[PQ_ACTIVE].pq_cnt,
3516 vm_dom[dom].vmd_pagequeues[PQ_INACTIVE].pq_cnt,
3517 vm_dom[dom].vmd_pagequeues[PQ_LAUNDRY].pq_cnt);
3521 DB_SHOW_COMMAND(pginfo, vm_page_print_pginfo)
3527 db_printf("show pginfo addr\n");
3531 phys = strchr(modif, 'p') != NULL;
3533 m = PHYS_TO_VM_PAGE(addr);
3535 m = (vm_page_t)addr;
3537 "page %p obj %p pidx 0x%jx phys 0x%jx q %d hold %d wire %d\n"
3538 " af 0x%x of 0x%x f 0x%x act %d busy %x valid 0x%x dirty 0x%x\n",
3539 m, m->object, (uintmax_t)m->pindex, (uintmax_t)m->phys_addr,
3540 m->queue, m->hold_count, m->wire_count, m->aflags, m->oflags,
3541 m->flags, m->act_count, m->busy_lock, m->valid, m->dirty);