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 * 3. 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];
135 * bogus page -- for I/O to/from partially complete buffers,
136 * or for paging into sparsely invalid regions.
138 vm_page_t bogus_page;
140 vm_page_t vm_page_array;
141 long vm_page_array_size;
144 static int boot_pages = UMA_BOOT_PAGES;
145 SYSCTL_INT(_vm, OID_AUTO, boot_pages, CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
147 "number of pages allocated for bootstrapping the VM system");
149 static int pa_tryrelock_restart;
150 SYSCTL_INT(_vm, OID_AUTO, tryrelock_restart, CTLFLAG_RD,
151 &pa_tryrelock_restart, 0, "Number of tryrelock restarts");
153 static TAILQ_HEAD(, vm_page) blacklist_head;
154 static int sysctl_vm_page_blacklist(SYSCTL_HANDLER_ARGS);
155 SYSCTL_PROC(_vm, OID_AUTO, page_blacklist, CTLTYPE_STRING | CTLFLAG_RD |
156 CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_page_blacklist, "A", "Blacklist pages");
158 /* Is the page daemon waiting for free pages? */
159 static int vm_pageout_pages_needed;
161 static uma_zone_t fakepg_zone;
163 static void vm_page_alloc_check(vm_page_t m);
164 static void vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits);
165 static void vm_page_enqueue(uint8_t queue, vm_page_t m);
166 static void vm_page_free_wakeup(void);
167 static void vm_page_init(void *dummy);
168 static int vm_page_insert_after(vm_page_t m, vm_object_t object,
169 vm_pindex_t pindex, vm_page_t mpred);
170 static void vm_page_insert_radixdone(vm_page_t m, vm_object_t object,
172 static int vm_page_reclaim_run(int req_class, u_long npages, vm_page_t m_run,
175 SYSINIT(vm_page, SI_SUB_VM, SI_ORDER_SECOND, vm_page_init, NULL);
178 vm_page_init(void *dummy)
181 fakepg_zone = uma_zcreate("fakepg", sizeof(struct vm_page), NULL, NULL,
182 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE | UMA_ZONE_VM);
183 bogus_page = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ |
184 VM_ALLOC_NORMAL | VM_ALLOC_WIRED);
187 /* Make sure that u_long is at least 64 bits when PAGE_SIZE is 32K. */
188 #if PAGE_SIZE == 32768
190 CTASSERT(sizeof(u_long) >= 8);
195 * Try to acquire a physical address lock while a pmap is locked. If we
196 * fail to trylock we unlock and lock the pmap directly and cache the
197 * locked pa in *locked. The caller should then restart their loop in case
198 * the virtual to physical mapping has changed.
201 vm_page_pa_tryrelock(pmap_t pmap, vm_paddr_t pa, vm_paddr_t *locked)
208 PA_LOCK_ASSERT(lockpa, MA_OWNED);
209 if (PA_LOCKPTR(pa) == PA_LOCKPTR(lockpa))
216 atomic_add_int(&pa_tryrelock_restart, 1);
225 * Sets the page size, perhaps based upon the memory
226 * size. Must be called before any use of page-size
227 * dependent functions.
230 vm_set_page_size(void)
232 if (vm_cnt.v_page_size == 0)
233 vm_cnt.v_page_size = PAGE_SIZE;
234 if (((vm_cnt.v_page_size - 1) & vm_cnt.v_page_size) != 0)
235 panic("vm_set_page_size: page size not a power of two");
239 * vm_page_blacklist_next:
241 * Find the next entry in the provided string of blacklist
242 * addresses. Entries are separated by space, comma, or newline.
243 * If an invalid integer is encountered then the rest of the
244 * string is skipped. Updates the list pointer to the next
245 * character, or NULL if the string is exhausted or invalid.
248 vm_page_blacklist_next(char **list, char *end)
253 if (list == NULL || *list == NULL)
261 * If there's no end pointer then the buffer is coming from
262 * the kenv and we know it's null-terminated.
265 end = *list + strlen(*list);
267 /* Ensure that strtoq() won't walk off the end */
269 if (*end == '\n' || *end == ' ' || *end == ',')
272 printf("Blacklist not terminated, skipping\n");
278 for (pos = *list; *pos != '\0'; pos = cp) {
279 bad = strtoq(pos, &cp, 0);
280 if (*cp == '\0' || *cp == ' ' || *cp == ',' || *cp == '\n') {
289 if (*cp == '\0' || ++cp >= end)
293 return (trunc_page(bad));
295 printf("Garbage in RAM blacklist, skipping\n");
301 * vm_page_blacklist_check:
303 * Iterate through the provided string of blacklist addresses, pulling
304 * each entry out of the physical allocator free list and putting it
305 * onto a list for reporting via the vm.page_blacklist sysctl.
308 vm_page_blacklist_check(char *list, char *end)
316 while (next != NULL) {
317 if ((pa = vm_page_blacklist_next(&next, end)) == 0)
319 m = vm_phys_paddr_to_vm_page(pa);
322 mtx_lock(&vm_page_queue_free_mtx);
323 ret = vm_phys_unfree_page(m);
324 mtx_unlock(&vm_page_queue_free_mtx);
326 TAILQ_INSERT_TAIL(&blacklist_head, m, listq);
328 printf("Skipping page with pa 0x%jx\n",
335 * vm_page_blacklist_load:
337 * Search for a special module named "ram_blacklist". It'll be a
338 * plain text file provided by the user via the loader directive
342 vm_page_blacklist_load(char **list, char **end)
351 mod = preload_search_by_type("ram_blacklist");
353 ptr = preload_fetch_addr(mod);
354 len = preload_fetch_size(mod);
365 sysctl_vm_page_blacklist(SYSCTL_HANDLER_ARGS)
372 error = sysctl_wire_old_buffer(req, 0);
375 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
376 TAILQ_FOREACH(m, &blacklist_head, listq) {
377 sbuf_printf(&sbuf, "%s%#jx", first ? "" : ",",
378 (uintmax_t)m->phys_addr);
381 error = sbuf_finish(&sbuf);
387 vm_page_domain_init(struct vm_domain *vmd)
389 struct vm_pagequeue *pq;
392 *__DECONST(char **, &vmd->vmd_pagequeues[PQ_INACTIVE].pq_name) =
393 "vm inactive pagequeue";
394 *__DECONST(u_int **, &vmd->vmd_pagequeues[PQ_INACTIVE].pq_vcnt) =
395 &vm_cnt.v_inactive_count;
396 *__DECONST(char **, &vmd->vmd_pagequeues[PQ_ACTIVE].pq_name) =
397 "vm active pagequeue";
398 *__DECONST(u_int **, &vmd->vmd_pagequeues[PQ_ACTIVE].pq_vcnt) =
399 &vm_cnt.v_active_count;
400 *__DECONST(char **, &vmd->vmd_pagequeues[PQ_LAUNDRY].pq_name) =
401 "vm laundry pagequeue";
402 *__DECONST(int **, &vmd->vmd_pagequeues[PQ_LAUNDRY].pq_vcnt) =
403 &vm_cnt.v_laundry_count;
404 *__DECONST(char **, &vmd->vmd_pagequeues[PQ_UNSWAPPABLE].pq_name) =
405 "vm unswappable pagequeue";
406 /* Unswappable dirty pages are counted as being in the laundry. */
407 *__DECONST(int **, &vmd->vmd_pagequeues[PQ_UNSWAPPABLE].pq_vcnt) =
408 &vm_cnt.v_laundry_count;
409 vmd->vmd_page_count = 0;
410 vmd->vmd_free_count = 0;
412 vmd->vmd_oom = FALSE;
413 for (i = 0; i < PQ_COUNT; i++) {
414 pq = &vmd->vmd_pagequeues[i];
415 TAILQ_INIT(&pq->pq_pl);
416 mtx_init(&pq->pq_mutex, pq->pq_name, "vm pagequeue",
417 MTX_DEF | MTX_DUPOK);
424 * Initializes the resident memory module. Allocates physical memory for
425 * bootstrapping UMA and some data structures that are used to manage
426 * physical pages. Initializes these structures, and populates the free
430 vm_page_startup(vm_offset_t vaddr)
433 vm_paddr_t high_avail, low_avail, page_range, size;
438 char *list, *listend;
440 vm_paddr_t biggestsize;
446 vaddr = round_page(vaddr);
448 for (i = 0; phys_avail[i + 1]; i += 2) {
449 phys_avail[i] = round_page(phys_avail[i]);
450 phys_avail[i + 1] = trunc_page(phys_avail[i + 1]);
452 for (i = 0; phys_avail[i + 1]; i += 2) {
453 size = phys_avail[i + 1] - phys_avail[i];
454 if (size > biggestsize) {
460 end = phys_avail[biggestone+1];
463 * Initialize the page and queue locks.
465 mtx_init(&vm_page_queue_free_mtx, "vm page free queue", NULL, MTX_DEF);
466 for (i = 0; i < PA_LOCK_COUNT; i++)
467 mtx_init(&pa_lock[i], "vm page", NULL, MTX_DEF);
468 for (i = 0; i < vm_ndomains; i++)
469 vm_page_domain_init(&vm_dom[i]);
472 * Almost all of the pages needed for bootstrapping UMA are used
473 * for zone structures, so if the number of CPUs results in those
474 * structures taking more than one page each, we set aside more pages
475 * in proportion to the zone structure size.
477 pages_per_zone = howmany(sizeof(struct uma_zone) +
478 sizeof(struct uma_cache) * (mp_maxid + 1), UMA_SLAB_SIZE);
479 if (pages_per_zone > 1) {
480 /* Reserve more pages so that we don't run out. */
481 boot_pages = UMA_BOOT_PAGES_ZONES * pages_per_zone;
485 * Allocate memory for use when boot strapping the kernel memory
488 * CTFLAG_RDTUN doesn't work during the early boot process, so we must
489 * manually fetch the value.
491 TUNABLE_INT_FETCH("vm.boot_pages", &boot_pages);
492 new_end = end - (boot_pages * UMA_SLAB_SIZE);
493 new_end = trunc_page(new_end);
494 mapped = pmap_map(&vaddr, new_end, end,
495 VM_PROT_READ | VM_PROT_WRITE);
496 bzero((void *)mapped, end - new_end);
497 uma_startup((void *)mapped, boot_pages);
499 #if defined(__aarch64__) || defined(__amd64__) || defined(__arm__) || \
500 defined(__i386__) || defined(__mips__)
502 * Allocate a bitmap to indicate that a random physical page
503 * needs to be included in a minidump.
505 * The amd64 port needs this to indicate which direct map pages
506 * need to be dumped, via calls to dump_add_page()/dump_drop_page().
508 * However, i386 still needs this workspace internally within the
509 * minidump code. In theory, they are not needed on i386, but are
510 * included should the sf_buf code decide to use them.
513 for (i = 0; dump_avail[i + 1] != 0; i += 2)
514 if (dump_avail[i + 1] > last_pa)
515 last_pa = dump_avail[i + 1];
516 page_range = last_pa / PAGE_SIZE;
517 vm_page_dump_size = round_page(roundup2(page_range, NBBY) / NBBY);
518 new_end -= vm_page_dump_size;
519 vm_page_dump = (void *)(uintptr_t)pmap_map(&vaddr, new_end,
520 new_end + vm_page_dump_size, VM_PROT_READ | VM_PROT_WRITE);
521 bzero((void *)vm_page_dump, vm_page_dump_size);
523 #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__)
525 * Include the UMA bootstrap pages and vm_page_dump in a crash dump.
526 * When pmap_map() uses the direct map, they are not automatically
529 for (pa = new_end; pa < end; pa += PAGE_SIZE)
532 phys_avail[biggestone + 1] = new_end;
535 * Request that the physical pages underlying the message buffer be
536 * included in a crash dump. Since the message buffer is accessed
537 * through the direct map, they are not automatically included.
539 pa = DMAP_TO_PHYS((vm_offset_t)msgbufp->msg_ptr);
540 last_pa = pa + round_page(msgbufsize);
541 while (pa < last_pa) {
547 * Compute the number of pages of memory that will be available for
548 * use, taking into account the overhead of a page structure per page.
549 * In other words, solve
550 * "available physical memory" - round_page(page_range *
551 * sizeof(struct vm_page)) = page_range * PAGE_SIZE
554 low_avail = phys_avail[0];
555 high_avail = phys_avail[1];
556 for (i = 0; i < vm_phys_nsegs; i++) {
557 if (vm_phys_segs[i].start < low_avail)
558 low_avail = vm_phys_segs[i].start;
559 if (vm_phys_segs[i].end > high_avail)
560 high_avail = vm_phys_segs[i].end;
562 /* Skip the first chunk. It is already accounted for. */
563 for (i = 2; phys_avail[i + 1] != 0; i += 2) {
564 if (phys_avail[i] < low_avail)
565 low_avail = phys_avail[i];
566 if (phys_avail[i + 1] > high_avail)
567 high_avail = phys_avail[i + 1];
569 first_page = low_avail / PAGE_SIZE;
570 #ifdef VM_PHYSSEG_SPARSE
572 for (i = 0; i < vm_phys_nsegs; i++)
573 size += vm_phys_segs[i].end - vm_phys_segs[i].start;
574 for (i = 0; phys_avail[i + 1] != 0; i += 2)
575 size += phys_avail[i + 1] - phys_avail[i];
576 page_range = size / (PAGE_SIZE + sizeof(struct vm_page));
577 #elif defined(VM_PHYSSEG_DENSE)
579 * In the VM_PHYSSEG_DENSE case, the number of pages can account for
580 * the overhead of a page structure per page only if vm_page_array is
581 * allocated from the last physical memory chunk. Otherwise, we must
582 * allocate page structures representing the physical memory
583 * underlying vm_page_array, even though they will not be used.
585 if (new_end == high_avail)
586 page_range = (high_avail - low_avail) / (PAGE_SIZE +
587 sizeof(struct vm_page));
589 page_range = high_avail / PAGE_SIZE - first_page;
591 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
596 * Reserve an unmapped guard page to trap access to vm_page_array[-1].
597 * However, because this page is allocated from KVM, out-of-bounds
598 * accesses using the direct map will not be trapped.
603 * Allocate physical memory for the page structures, and map it.
605 new_end = trunc_page(end - page_range * sizeof(struct vm_page));
606 mapped = pmap_map(&vaddr, new_end, end,
607 VM_PROT_READ | VM_PROT_WRITE);
608 vm_page_array = (vm_page_t) mapped;
609 #if VM_NRESERVLEVEL > 0
611 * Allocate physical memory for the reservation management system's
612 * data structures, and map it.
614 if (high_avail == end)
615 high_avail = new_end;
616 new_end = vm_reserv_startup(&vaddr, new_end, high_avail);
618 #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__)
620 * Include vm_page_array and vm_reserv_array in a crash dump.
622 for (pa = new_end; pa < end; pa += PAGE_SIZE)
625 phys_avail[biggestone + 1] = new_end;
628 * Add physical memory segments corresponding to the available
631 for (i = 0; phys_avail[i + 1] != 0; i += 2)
632 vm_phys_add_seg(phys_avail[i], phys_avail[i + 1]);
635 * Clear all of the page structures
637 bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page));
638 for (i = 0; i < page_range; i++)
639 vm_page_array[i].order = VM_NFREEORDER;
640 vm_page_array_size = page_range;
643 * Initialize the physical memory allocator.
648 * Add every available physical page that is not blacklisted to
651 vm_cnt.v_page_count = 0;
652 vm_cnt.v_free_count = 0;
653 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
655 last_pa = phys_avail[i + 1];
656 while (pa < last_pa) {
657 vm_phys_add_page(pa);
662 TAILQ_INIT(&blacklist_head);
663 vm_page_blacklist_load(&list, &listend);
664 vm_page_blacklist_check(list, listend);
666 list = kern_getenv("vm.blacklist");
667 vm_page_blacklist_check(list, NULL);
670 #if VM_NRESERVLEVEL > 0
672 * Initialize the reservation management system.
680 vm_page_reference(vm_page_t m)
683 vm_page_aflag_set(m, PGA_REFERENCED);
687 * vm_page_busy_downgrade:
689 * Downgrade an exclusive busy page into a single shared busy page.
692 vm_page_busy_downgrade(vm_page_t m)
697 vm_page_assert_xbusied(m);
698 locked = mtx_owned(vm_page_lockptr(m));
702 x &= VPB_BIT_WAITERS;
703 if (x != 0 && !locked)
705 if (atomic_cmpset_rel_int(&m->busy_lock,
706 VPB_SINGLE_EXCLUSIVER | x, VPB_SHARERS_WORD(1)))
708 if (x != 0 && !locked)
721 * Return a positive value if the page is shared busied, 0 otherwise.
724 vm_page_sbusied(vm_page_t m)
729 return ((x & VPB_BIT_SHARED) != 0 && x != VPB_UNBUSIED);
735 * Shared unbusy a page.
738 vm_page_sunbusy(vm_page_t m)
742 vm_page_assert_sbusied(m);
746 if (VPB_SHARERS(x) > 1) {
747 if (atomic_cmpset_int(&m->busy_lock, x,
752 if ((x & VPB_BIT_WAITERS) == 0) {
753 KASSERT(x == VPB_SHARERS_WORD(1),
754 ("vm_page_sunbusy: invalid lock state"));
755 if (atomic_cmpset_int(&m->busy_lock,
756 VPB_SHARERS_WORD(1), VPB_UNBUSIED))
760 KASSERT(x == (VPB_SHARERS_WORD(1) | VPB_BIT_WAITERS),
761 ("vm_page_sunbusy: invalid lock state for waiters"));
764 if (!atomic_cmpset_int(&m->busy_lock, x, VPB_UNBUSIED)) {
775 * vm_page_busy_sleep:
777 * Sleep and release the page lock, using the page pointer as wchan.
778 * This is used to implement the hard-path of busying mechanism.
780 * The given page must be locked.
782 * If nonshared is true, sleep only if the page is xbusy.
785 vm_page_busy_sleep(vm_page_t m, const char *wmesg, bool nonshared)
789 vm_page_assert_locked(m);
792 if (x == VPB_UNBUSIED || (nonshared && (x & VPB_BIT_SHARED) != 0) ||
793 ((x & VPB_BIT_WAITERS) == 0 &&
794 !atomic_cmpset_int(&m->busy_lock, x, x | VPB_BIT_WAITERS))) {
798 msleep(m, vm_page_lockptr(m), PVM | PDROP, wmesg, 0);
804 * Try to shared busy a page.
805 * If the operation succeeds 1 is returned otherwise 0.
806 * The operation never sleeps.
809 vm_page_trysbusy(vm_page_t m)
815 if ((x & VPB_BIT_SHARED) == 0)
817 if (atomic_cmpset_acq_int(&m->busy_lock, x, x + VPB_ONE_SHARER))
823 vm_page_xunbusy_locked(vm_page_t m)
826 vm_page_assert_xbusied(m);
827 vm_page_assert_locked(m);
829 atomic_store_rel_int(&m->busy_lock, VPB_UNBUSIED);
830 /* There is a waiter, do wakeup() instead of vm_page_flash(). */
835 vm_page_xunbusy_maybelocked(vm_page_t m)
839 vm_page_assert_xbusied(m);
842 * Fast path for unbusy. If it succeeds, we know that there
843 * are no waiters, so we do not need a wakeup.
845 if (atomic_cmpset_rel_int(&m->busy_lock, VPB_SINGLE_EXCLUSIVER,
849 lockacq = !mtx_owned(vm_page_lockptr(m));
852 vm_page_xunbusy_locked(m);
858 * vm_page_xunbusy_hard:
860 * Called after the first try the exclusive unbusy of a page failed.
861 * It is assumed that the waiters bit is on.
864 vm_page_xunbusy_hard(vm_page_t m)
867 vm_page_assert_xbusied(m);
870 vm_page_xunbusy_locked(m);
877 * Wakeup anyone waiting for the page.
878 * The ownership bits do not change.
880 * The given page must be locked.
883 vm_page_flash(vm_page_t m)
887 vm_page_lock_assert(m, MA_OWNED);
891 if ((x & VPB_BIT_WAITERS) == 0)
893 if (atomic_cmpset_int(&m->busy_lock, x,
894 x & (~VPB_BIT_WAITERS)))
901 * Keep page from being freed by the page daemon
902 * much of the same effect as wiring, except much lower
903 * overhead and should be used only for *very* temporary
904 * holding ("wiring").
907 vm_page_hold(vm_page_t mem)
910 vm_page_lock_assert(mem, MA_OWNED);
915 vm_page_unhold(vm_page_t mem)
918 vm_page_lock_assert(mem, MA_OWNED);
919 KASSERT(mem->hold_count >= 1, ("vm_page_unhold: hold count < 0!!!"));
921 if (mem->hold_count == 0 && (mem->flags & PG_UNHOLDFREE) != 0)
922 vm_page_free_toq(mem);
926 * vm_page_unhold_pages:
928 * Unhold each of the pages that is referenced by the given array.
931 vm_page_unhold_pages(vm_page_t *ma, int count)
933 struct mtx *mtx, *new_mtx;
936 for (; count != 0; count--) {
938 * Avoid releasing and reacquiring the same page lock.
940 new_mtx = vm_page_lockptr(*ma);
941 if (mtx != new_mtx) {
955 PHYS_TO_VM_PAGE(vm_paddr_t pa)
959 #ifdef VM_PHYSSEG_SPARSE
960 m = vm_phys_paddr_to_vm_page(pa);
962 m = vm_phys_fictitious_to_vm_page(pa);
964 #elif defined(VM_PHYSSEG_DENSE)
968 if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
969 m = &vm_page_array[pi - first_page];
972 return (vm_phys_fictitious_to_vm_page(pa));
974 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
981 * Create a fictitious page with the specified physical address and
982 * memory attribute. The memory attribute is the only the machine-
983 * dependent aspect of a fictitious page that must be initialized.
986 vm_page_getfake(vm_paddr_t paddr, vm_memattr_t memattr)
990 m = uma_zalloc(fakepg_zone, M_WAITOK | M_ZERO);
991 vm_page_initfake(m, paddr, memattr);
996 vm_page_initfake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
999 if ((m->flags & PG_FICTITIOUS) != 0) {
1001 * The page's memattr might have changed since the
1002 * previous initialization. Update the pmap to the
1007 m->phys_addr = paddr;
1009 /* Fictitious pages don't use "segind". */
1010 m->flags = PG_FICTITIOUS;
1011 /* Fictitious pages don't use "order" or "pool". */
1012 m->oflags = VPO_UNMANAGED;
1013 m->busy_lock = VPB_SINGLE_EXCLUSIVER;
1017 pmap_page_set_memattr(m, memattr);
1023 * Release a fictitious page.
1026 vm_page_putfake(vm_page_t m)
1029 KASSERT((m->oflags & VPO_UNMANAGED) != 0, ("managed %p", m));
1030 KASSERT((m->flags & PG_FICTITIOUS) != 0,
1031 ("vm_page_putfake: bad page %p", m));
1032 uma_zfree(fakepg_zone, m);
1036 * vm_page_updatefake:
1038 * Update the given fictitious page to the specified physical address and
1042 vm_page_updatefake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
1045 KASSERT((m->flags & PG_FICTITIOUS) != 0,
1046 ("vm_page_updatefake: bad page %p", m));
1047 m->phys_addr = paddr;
1048 pmap_page_set_memattr(m, memattr);
1057 vm_page_free(vm_page_t m)
1060 m->flags &= ~PG_ZERO;
1061 vm_page_free_toq(m);
1065 * vm_page_free_zero:
1067 * Free a page to the zerod-pages queue
1070 vm_page_free_zero(vm_page_t m)
1073 m->flags |= PG_ZERO;
1074 vm_page_free_toq(m);
1078 * Unbusy and handle the page queueing for a page from a getpages request that
1079 * was optionally read ahead or behind.
1082 vm_page_readahead_finish(vm_page_t m)
1085 /* We shouldn't put invalid pages on queues. */
1086 KASSERT(m->valid != 0, ("%s: %p is invalid", __func__, m));
1089 * Since the page is not the actually needed one, whether it should
1090 * be activated or deactivated is not obvious. Empirical results
1091 * have shown that deactivating the page is usually the best choice,
1092 * unless the page is wanted by another thread.
1095 if ((m->busy_lock & VPB_BIT_WAITERS) != 0)
1096 vm_page_activate(m);
1098 vm_page_deactivate(m);
1104 * vm_page_sleep_if_busy:
1106 * Sleep and release the page queues lock if the page is busied.
1107 * Returns TRUE if the thread slept.
1109 * The given page must be unlocked and object containing it must
1113 vm_page_sleep_if_busy(vm_page_t m, const char *msg)
1117 vm_page_lock_assert(m, MA_NOTOWNED);
1118 VM_OBJECT_ASSERT_WLOCKED(m->object);
1120 if (vm_page_busied(m)) {
1122 * The page-specific object must be cached because page
1123 * identity can change during the sleep, causing the
1124 * re-lock of a different object.
1125 * It is assumed that a reference to the object is already
1126 * held by the callers.
1130 VM_OBJECT_WUNLOCK(obj);
1131 vm_page_busy_sleep(m, msg, false);
1132 VM_OBJECT_WLOCK(obj);
1139 * vm_page_dirty_KBI: [ internal use only ]
1141 * Set all bits in the page's dirty field.
1143 * The object containing the specified page must be locked if the
1144 * call is made from the machine-independent layer.
1146 * See vm_page_clear_dirty_mask().
1148 * This function should only be called by vm_page_dirty().
1151 vm_page_dirty_KBI(vm_page_t m)
1154 /* Refer to this operation by its public name. */
1155 KASSERT(m->valid == VM_PAGE_BITS_ALL,
1156 ("vm_page_dirty: page is invalid!"));
1157 m->dirty = VM_PAGE_BITS_ALL;
1161 * vm_page_insert: [ internal use only ]
1163 * Inserts the given mem entry into the object and object list.
1165 * The object must be locked.
1168 vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex)
1172 VM_OBJECT_ASSERT_WLOCKED(object);
1173 mpred = vm_radix_lookup_le(&object->rtree, pindex);
1174 return (vm_page_insert_after(m, object, pindex, mpred));
1178 * vm_page_insert_after:
1180 * Inserts the page "m" into the specified object at offset "pindex".
1182 * The page "mpred" must immediately precede the offset "pindex" within
1183 * the specified object.
1185 * The object must be locked.
1188 vm_page_insert_after(vm_page_t m, vm_object_t object, vm_pindex_t pindex,
1193 VM_OBJECT_ASSERT_WLOCKED(object);
1194 KASSERT(m->object == NULL,
1195 ("vm_page_insert_after: page already inserted"));
1196 if (mpred != NULL) {
1197 KASSERT(mpred->object == object,
1198 ("vm_page_insert_after: object doesn't contain mpred"));
1199 KASSERT(mpred->pindex < pindex,
1200 ("vm_page_insert_after: mpred doesn't precede pindex"));
1201 msucc = TAILQ_NEXT(mpred, listq);
1203 msucc = TAILQ_FIRST(&object->memq);
1205 KASSERT(msucc->pindex > pindex,
1206 ("vm_page_insert_after: msucc doesn't succeed pindex"));
1209 * Record the object/offset pair in this page
1215 * Now link into the object's ordered list of backed pages.
1217 if (vm_radix_insert(&object->rtree, m)) {
1222 vm_page_insert_radixdone(m, object, mpred);
1227 * vm_page_insert_radixdone:
1229 * Complete page "m" insertion into the specified object after the
1230 * radix trie hooking.
1232 * The page "mpred" must precede the offset "m->pindex" within the
1235 * The object must be locked.
1238 vm_page_insert_radixdone(vm_page_t m, vm_object_t object, vm_page_t mpred)
1241 VM_OBJECT_ASSERT_WLOCKED(object);
1242 KASSERT(object != NULL && m->object == object,
1243 ("vm_page_insert_radixdone: page %p has inconsistent object", m));
1244 if (mpred != NULL) {
1245 KASSERT(mpred->object == object,
1246 ("vm_page_insert_after: object doesn't contain mpred"));
1247 KASSERT(mpred->pindex < m->pindex,
1248 ("vm_page_insert_after: mpred doesn't precede pindex"));
1252 TAILQ_INSERT_AFTER(&object->memq, mpred, m, listq);
1254 TAILQ_INSERT_HEAD(&object->memq, m, listq);
1257 * Show that the object has one more resident page.
1259 object->resident_page_count++;
1262 * Hold the vnode until the last page is released.
1264 if (object->resident_page_count == 1 && object->type == OBJT_VNODE)
1265 vhold(object->handle);
1268 * Since we are inserting a new and possibly dirty page,
1269 * update the object's OBJ_MIGHTBEDIRTY flag.
1271 if (pmap_page_is_write_mapped(m))
1272 vm_object_set_writeable_dirty(object);
1278 * Removes the specified page from its containing object, but does not
1279 * invalidate any backing storage.
1281 * The object must be locked. The page must be locked if it is managed.
1284 vm_page_remove(vm_page_t m)
1289 if ((m->oflags & VPO_UNMANAGED) == 0)
1290 vm_page_assert_locked(m);
1291 if ((object = m->object) == NULL)
1293 VM_OBJECT_ASSERT_WLOCKED(object);
1294 if (vm_page_xbusied(m))
1295 vm_page_xunbusy_maybelocked(m);
1296 mrem = vm_radix_remove(&object->rtree, m->pindex);
1297 KASSERT(mrem == m, ("removed page %p, expected page %p", mrem, m));
1300 * Now remove from the object's list of backed pages.
1302 TAILQ_REMOVE(&object->memq, m, listq);
1305 * And show that the object has one fewer resident page.
1307 object->resident_page_count--;
1310 * The vnode may now be recycled.
1312 if (object->resident_page_count == 0 && object->type == OBJT_VNODE)
1313 vdrop(object->handle);
1321 * Returns the page associated with the object/offset
1322 * pair specified; if none is found, NULL is returned.
1324 * The object must be locked.
1327 vm_page_lookup(vm_object_t object, vm_pindex_t pindex)
1330 VM_OBJECT_ASSERT_LOCKED(object);
1331 return (vm_radix_lookup(&object->rtree, pindex));
1335 * vm_page_find_least:
1337 * Returns the page associated with the object with least pindex
1338 * greater than or equal to the parameter pindex, or NULL.
1340 * The object must be locked.
1343 vm_page_find_least(vm_object_t object, vm_pindex_t pindex)
1347 VM_OBJECT_ASSERT_LOCKED(object);
1348 if ((m = TAILQ_FIRST(&object->memq)) != NULL && m->pindex < pindex)
1349 m = vm_radix_lookup_ge(&object->rtree, pindex);
1354 * Returns the given page's successor (by pindex) within the object if it is
1355 * resident; if none is found, NULL is returned.
1357 * The object must be locked.
1360 vm_page_next(vm_page_t m)
1364 VM_OBJECT_ASSERT_LOCKED(m->object);
1365 if ((next = TAILQ_NEXT(m, listq)) != NULL) {
1366 MPASS(next->object == m->object);
1367 if (next->pindex != m->pindex + 1)
1374 * Returns the given page's predecessor (by pindex) within the object if it is
1375 * resident; if none is found, NULL is returned.
1377 * The object must be locked.
1380 vm_page_prev(vm_page_t m)
1384 VM_OBJECT_ASSERT_LOCKED(m->object);
1385 if ((prev = TAILQ_PREV(m, pglist, listq)) != NULL) {
1386 MPASS(prev->object == m->object);
1387 if (prev->pindex != m->pindex - 1)
1394 * Uses the page mnew as a replacement for an existing page at index
1395 * pindex which must be already present in the object.
1397 * The existing page must not be on a paging queue.
1400 vm_page_replace(vm_page_t mnew, vm_object_t object, vm_pindex_t pindex)
1404 VM_OBJECT_ASSERT_WLOCKED(object);
1405 KASSERT(mnew->object == NULL,
1406 ("vm_page_replace: page already in object"));
1409 * This function mostly follows vm_page_insert() and
1410 * vm_page_remove() without the radix, object count and vnode
1411 * dance. Double check such functions for more comments.
1414 mnew->object = object;
1415 mnew->pindex = pindex;
1416 mold = vm_radix_replace(&object->rtree, mnew);
1417 KASSERT(mold->queue == PQ_NONE,
1418 ("vm_page_replace: mold is on a paging queue"));
1420 /* Keep the resident page list in sorted order. */
1421 TAILQ_INSERT_AFTER(&object->memq, mold, mnew, listq);
1422 TAILQ_REMOVE(&object->memq, mold, listq);
1424 mold->object = NULL;
1425 vm_page_xunbusy_maybelocked(mold);
1428 * The object's resident_page_count does not change because we have
1429 * swapped one page for another, but OBJ_MIGHTBEDIRTY.
1431 if (pmap_page_is_write_mapped(mnew))
1432 vm_object_set_writeable_dirty(object);
1439 * Move the given memory entry from its
1440 * current object to the specified target object/offset.
1442 * Note: swap associated with the page must be invalidated by the move. We
1443 * have to do this for several reasons: (1) we aren't freeing the
1444 * page, (2) we are dirtying the page, (3) the VM system is probably
1445 * moving the page from object A to B, and will then later move
1446 * the backing store from A to B and we can't have a conflict.
1448 * Note: we *always* dirty the page. It is necessary both for the
1449 * fact that we moved it, and because we may be invalidating
1452 * The objects must be locked.
1455 vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex)
1460 VM_OBJECT_ASSERT_WLOCKED(new_object);
1462 mpred = vm_radix_lookup_le(&new_object->rtree, new_pindex);
1463 KASSERT(mpred == NULL || mpred->pindex != new_pindex,
1464 ("vm_page_rename: pindex already renamed"));
1467 * Create a custom version of vm_page_insert() which does not depend
1468 * by m_prev and can cheat on the implementation aspects of the
1472 m->pindex = new_pindex;
1473 if (vm_radix_insert(&new_object->rtree, m)) {
1479 * The operation cannot fail anymore. The removal must happen before
1480 * the listq iterator is tainted.
1486 /* Return back to the new pindex to complete vm_page_insert(). */
1487 m->pindex = new_pindex;
1488 m->object = new_object;
1490 vm_page_insert_radixdone(m, new_object, mpred);
1498 * Allocate and return a page that is associated with the specified
1499 * object and offset pair. By default, this page is exclusive busied.
1501 * The caller must always specify an allocation class.
1503 * allocation classes:
1504 * VM_ALLOC_NORMAL normal process request
1505 * VM_ALLOC_SYSTEM system *really* needs a page
1506 * VM_ALLOC_INTERRUPT interrupt time request
1508 * optional allocation flags:
1509 * VM_ALLOC_COUNT(number) the number of additional pages that the caller
1510 * intends to allocate
1511 * VM_ALLOC_NOBUSY do not exclusive busy the page
1512 * VM_ALLOC_NODUMP do not include the page in a kernel core dump
1513 * VM_ALLOC_NOOBJ page is not associated with an object and
1514 * should not be exclusive busy
1515 * VM_ALLOC_SBUSY shared busy the allocated page
1516 * VM_ALLOC_WIRED wire the allocated page
1517 * VM_ALLOC_ZERO prefer a zeroed page
1519 * This routine may not sleep.
1522 vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req)
1525 int flags, req_class;
1527 mpred = NULL; /* XXX: pacify gcc */
1528 KASSERT((object != NULL) == ((req & VM_ALLOC_NOOBJ) == 0) &&
1529 (object != NULL || (req & VM_ALLOC_SBUSY) == 0) &&
1530 ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) !=
1531 (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)),
1532 ("vm_page_alloc: inconsistent object(%p)/req(%x)", object, req));
1534 VM_OBJECT_ASSERT_WLOCKED(object);
1536 req_class = req & VM_ALLOC_CLASS_MASK;
1539 * The page daemon is allowed to dig deeper into the free page list.
1541 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
1542 req_class = VM_ALLOC_SYSTEM;
1544 if (object != NULL) {
1545 mpred = vm_radix_lookup_le(&object->rtree, pindex);
1546 KASSERT(mpred == NULL || mpred->pindex != pindex,
1547 ("vm_page_alloc: pindex already allocated"));
1551 * Allocate a page if the number of free pages exceeds the minimum
1552 * for the request class.
1554 mtx_lock(&vm_page_queue_free_mtx);
1555 if (vm_cnt.v_free_count > vm_cnt.v_free_reserved ||
1556 (req_class == VM_ALLOC_SYSTEM &&
1557 vm_cnt.v_free_count > vm_cnt.v_interrupt_free_min) ||
1558 (req_class == VM_ALLOC_INTERRUPT &&
1559 vm_cnt.v_free_count > 0)) {
1561 * Can we allocate the page from a reservation?
1563 #if VM_NRESERVLEVEL > 0
1564 if (object == NULL || (object->flags & (OBJ_COLORED |
1565 OBJ_FICTITIOUS)) != OBJ_COLORED || (m =
1566 vm_reserv_alloc_page(object, pindex, mpred)) == NULL)
1570 * If not, allocate it from the free page queues.
1572 m = vm_phys_alloc_pages(object != NULL ?
1573 VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT, 0);
1574 #if VM_NRESERVLEVEL > 0
1575 if (m == NULL && vm_reserv_reclaim_inactive()) {
1576 m = vm_phys_alloc_pages(object != NULL ?
1577 VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT,
1584 * Not allocatable, give up.
1586 mtx_unlock(&vm_page_queue_free_mtx);
1587 atomic_add_int(&vm_pageout_deficit,
1588 max((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1));
1589 pagedaemon_wakeup();
1594 * At this point we had better have found a good page.
1596 KASSERT(m != NULL, ("vm_page_alloc: missing page"));
1597 vm_phys_freecnt_adj(m, -1);
1598 mtx_unlock(&vm_page_queue_free_mtx);
1599 vm_page_alloc_check(m);
1602 * Initialize the page. Only the PG_ZERO flag is inherited.
1605 if ((req & VM_ALLOC_ZERO) != 0)
1608 if ((req & VM_ALLOC_NODUMP) != 0)
1612 m->oflags = object == NULL || (object->flags & OBJ_UNMANAGED) != 0 ?
1614 m->busy_lock = VPB_UNBUSIED;
1615 if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ | VM_ALLOC_SBUSY)) == 0)
1616 m->busy_lock = VPB_SINGLE_EXCLUSIVER;
1617 if ((req & VM_ALLOC_SBUSY) != 0)
1618 m->busy_lock = VPB_SHARERS_WORD(1);
1619 if (req & VM_ALLOC_WIRED) {
1621 * The page lock is not required for wiring a page until that
1622 * page is inserted into the object.
1624 atomic_add_int(&vm_cnt.v_wire_count, 1);
1629 if (object != NULL) {
1630 if (vm_page_insert_after(m, object, pindex, mpred)) {
1631 pagedaemon_wakeup();
1632 if (req & VM_ALLOC_WIRED) {
1633 atomic_subtract_int(&vm_cnt.v_wire_count, 1);
1636 KASSERT(m->object == NULL, ("page %p has object", m));
1637 m->oflags = VPO_UNMANAGED;
1638 m->busy_lock = VPB_UNBUSIED;
1639 /* Don't change PG_ZERO. */
1640 vm_page_free_toq(m);
1644 /* Ignore device objects; the pager sets "memattr" for them. */
1645 if (object->memattr != VM_MEMATTR_DEFAULT &&
1646 (object->flags & OBJ_FICTITIOUS) == 0)
1647 pmap_page_set_memattr(m, object->memattr);
1652 * Don't wakeup too often - wakeup the pageout daemon when
1653 * we would be nearly out of memory.
1655 if (vm_paging_needed())
1656 pagedaemon_wakeup();
1662 * vm_page_alloc_contig:
1664 * Allocate a contiguous set of physical pages of the given size "npages"
1665 * from the free lists. All of the physical pages must be at or above
1666 * the given physical address "low" and below the given physical address
1667 * "high". The given value "alignment" determines the alignment of the
1668 * first physical page in the set. If the given value "boundary" is
1669 * non-zero, then the set of physical pages cannot cross any physical
1670 * address boundary that is a multiple of that value. Both "alignment"
1671 * and "boundary" must be a power of two.
1673 * If the specified memory attribute, "memattr", is VM_MEMATTR_DEFAULT,
1674 * then the memory attribute setting for the physical pages is configured
1675 * to the object's memory attribute setting. Otherwise, the memory
1676 * attribute setting for the physical pages is configured to "memattr",
1677 * overriding the object's memory attribute setting. However, if the
1678 * object's memory attribute setting is not VM_MEMATTR_DEFAULT, then the
1679 * memory attribute setting for the physical pages cannot be configured
1680 * to VM_MEMATTR_DEFAULT.
1682 * The specified object may not contain fictitious pages.
1684 * The caller must always specify an allocation class.
1686 * allocation classes:
1687 * VM_ALLOC_NORMAL normal process request
1688 * VM_ALLOC_SYSTEM system *really* needs a page
1689 * VM_ALLOC_INTERRUPT interrupt time request
1691 * optional allocation flags:
1692 * VM_ALLOC_NOBUSY do not exclusive busy the page
1693 * VM_ALLOC_NODUMP do not include the page in a kernel core dump
1694 * VM_ALLOC_NOOBJ page is not associated with an object and
1695 * should not be exclusive busy
1696 * VM_ALLOC_SBUSY shared busy the allocated page
1697 * VM_ALLOC_WIRED wire the allocated page
1698 * VM_ALLOC_ZERO prefer a zeroed page
1700 * This routine may not sleep.
1703 vm_page_alloc_contig(vm_object_t object, vm_pindex_t pindex, int req,
1704 u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
1705 vm_paddr_t boundary, vm_memattr_t memattr)
1707 vm_page_t m, m_ret, mpred;
1708 u_int busy_lock, flags, oflags;
1711 mpred = NULL; /* XXX: pacify gcc */
1712 KASSERT((object != NULL) == ((req & VM_ALLOC_NOOBJ) == 0) &&
1713 (object != NULL || (req & VM_ALLOC_SBUSY) == 0) &&
1714 ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) !=
1715 (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)),
1716 ("vm_page_alloc_contig: inconsistent object(%p)/req(%x)", object,
1718 if (object != NULL) {
1719 VM_OBJECT_ASSERT_WLOCKED(object);
1720 KASSERT((object->flags & OBJ_FICTITIOUS) == 0,
1721 ("vm_page_alloc_contig: object %p has fictitious pages",
1724 KASSERT(npages > 0, ("vm_page_alloc_contig: npages is zero"));
1725 req_class = req & VM_ALLOC_CLASS_MASK;
1728 * The page daemon is allowed to dig deeper into the free page list.
1730 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
1731 req_class = VM_ALLOC_SYSTEM;
1733 if (object != NULL) {
1734 mpred = vm_radix_lookup_le(&object->rtree, pindex);
1735 KASSERT(mpred == NULL || mpred->pindex != pindex,
1736 ("vm_page_alloc_contig: pindex already allocated"));
1740 * Can we allocate the pages without the number of free pages falling
1741 * below the lower bound for the allocation class?
1743 mtx_lock(&vm_page_queue_free_mtx);
1744 if (vm_cnt.v_free_count >= npages + vm_cnt.v_free_reserved ||
1745 (req_class == VM_ALLOC_SYSTEM &&
1746 vm_cnt.v_free_count >= npages + vm_cnt.v_interrupt_free_min) ||
1747 (req_class == VM_ALLOC_INTERRUPT &&
1748 vm_cnt.v_free_count >= npages)) {
1750 * Can we allocate the pages from a reservation?
1752 #if VM_NRESERVLEVEL > 0
1754 if (object == NULL || (object->flags & OBJ_COLORED) == 0 ||
1755 (m_ret = vm_reserv_alloc_contig(object, pindex, npages,
1756 low, high, alignment, boundary, mpred)) == NULL)
1759 * If not, allocate them from the free page queues.
1761 m_ret = vm_phys_alloc_contig(npages, low, high,
1762 alignment, boundary);
1764 mtx_unlock(&vm_page_queue_free_mtx);
1765 atomic_add_int(&vm_pageout_deficit, npages);
1766 pagedaemon_wakeup();
1770 vm_phys_freecnt_adj(m_ret, -npages);
1772 #if VM_NRESERVLEVEL > 0
1773 if (vm_reserv_reclaim_contig(npages, low, high, alignment,
1778 mtx_unlock(&vm_page_queue_free_mtx);
1781 for (m = m_ret; m < &m_ret[npages]; m++)
1782 vm_page_alloc_check(m);
1785 * Initialize the pages. Only the PG_ZERO flag is inherited.
1788 if ((req & VM_ALLOC_ZERO) != 0)
1790 if ((req & VM_ALLOC_NODUMP) != 0)
1792 oflags = object == NULL || (object->flags & OBJ_UNMANAGED) != 0 ?
1794 busy_lock = VPB_UNBUSIED;
1795 if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ | VM_ALLOC_SBUSY)) == 0)
1796 busy_lock = VPB_SINGLE_EXCLUSIVER;
1797 if ((req & VM_ALLOC_SBUSY) != 0)
1798 busy_lock = VPB_SHARERS_WORD(1);
1799 if ((req & VM_ALLOC_WIRED) != 0)
1800 atomic_add_int(&vm_cnt.v_wire_count, npages);
1801 if (object != NULL) {
1802 if (object->memattr != VM_MEMATTR_DEFAULT &&
1803 memattr == VM_MEMATTR_DEFAULT)
1804 memattr = object->memattr;
1806 for (m = m_ret; m < &m_ret[npages]; m++) {
1808 m->flags = (m->flags | PG_NODUMP) & flags;
1809 m->busy_lock = busy_lock;
1810 if ((req & VM_ALLOC_WIRED) != 0)
1814 if (object != NULL) {
1815 if (vm_page_insert_after(m, object, pindex, mpred)) {
1816 pagedaemon_wakeup();
1817 if ((req & VM_ALLOC_WIRED) != 0)
1818 atomic_subtract_int(
1819 &vm_cnt.v_wire_count, npages);
1820 KASSERT(m->object == NULL,
1821 ("page %p has object", m));
1823 for (m = m_ret; m < &m_ret[npages]; m++) {
1825 (req & VM_ALLOC_WIRED) != 0)
1827 m->oflags = VPO_UNMANAGED;
1828 m->busy_lock = VPB_UNBUSIED;
1829 /* Don't change PG_ZERO. */
1830 vm_page_free_toq(m);
1837 if (memattr != VM_MEMATTR_DEFAULT)
1838 pmap_page_set_memattr(m, memattr);
1841 if (vm_paging_needed())
1842 pagedaemon_wakeup();
1847 * Check a page that has been freshly dequeued from a freelist.
1850 vm_page_alloc_check(vm_page_t m)
1853 KASSERT(m->object == NULL, ("page %p has object", m));
1854 KASSERT(m->queue == PQ_NONE,
1855 ("page %p has unexpected queue %d", m, m->queue));
1856 KASSERT(m->wire_count == 0, ("page %p is wired", m));
1857 KASSERT(m->hold_count == 0, ("page %p is held", m));
1858 KASSERT(!vm_page_busied(m), ("page %p is busy", m));
1859 KASSERT(m->dirty == 0, ("page %p is dirty", m));
1860 KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT,
1861 ("page %p has unexpected memattr %d",
1862 m, pmap_page_get_memattr(m)));
1863 KASSERT(m->valid == 0, ("free page %p is valid", m));
1867 * vm_page_alloc_freelist:
1869 * Allocate a physical page from the specified free page list.
1871 * The caller must always specify an allocation class.
1873 * allocation classes:
1874 * VM_ALLOC_NORMAL normal process request
1875 * VM_ALLOC_SYSTEM system *really* needs a page
1876 * VM_ALLOC_INTERRUPT interrupt time request
1878 * optional allocation flags:
1879 * VM_ALLOC_COUNT(number) the number of additional pages that the caller
1880 * intends to allocate
1881 * VM_ALLOC_WIRED wire the allocated page
1882 * VM_ALLOC_ZERO prefer a zeroed page
1884 * This routine may not sleep.
1887 vm_page_alloc_freelist(int flind, int req)
1893 req_class = req & VM_ALLOC_CLASS_MASK;
1896 * The page daemon is allowed to dig deeper into the free page list.
1898 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
1899 req_class = VM_ALLOC_SYSTEM;
1902 * Do not allocate reserved pages unless the req has asked for it.
1904 mtx_lock(&vm_page_queue_free_mtx);
1905 if (vm_cnt.v_free_count > vm_cnt.v_free_reserved ||
1906 (req_class == VM_ALLOC_SYSTEM &&
1907 vm_cnt.v_free_count > vm_cnt.v_interrupt_free_min) ||
1908 (req_class == VM_ALLOC_INTERRUPT &&
1909 vm_cnt.v_free_count > 0))
1910 m = vm_phys_alloc_freelist_pages(flind, VM_FREEPOOL_DIRECT, 0);
1912 mtx_unlock(&vm_page_queue_free_mtx);
1913 atomic_add_int(&vm_pageout_deficit,
1914 max((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1));
1915 pagedaemon_wakeup();
1919 mtx_unlock(&vm_page_queue_free_mtx);
1922 vm_phys_freecnt_adj(m, -1);
1923 mtx_unlock(&vm_page_queue_free_mtx);
1924 vm_page_alloc_check(m);
1927 * Initialize the page. Only the PG_ZERO flag is inherited.
1931 if ((req & VM_ALLOC_ZERO) != 0)
1934 if ((req & VM_ALLOC_WIRED) != 0) {
1936 * The page lock is not required for wiring a page that does
1937 * not belong to an object.
1939 atomic_add_int(&vm_cnt.v_wire_count, 1);
1942 /* Unmanaged pages don't use "act_count". */
1943 m->oflags = VPO_UNMANAGED;
1944 if (vm_paging_needed())
1945 pagedaemon_wakeup();
1949 #define VPSC_ANY 0 /* No restrictions. */
1950 #define VPSC_NORESERV 1 /* Skip reservations; implies VPSC_NOSUPER. */
1951 #define VPSC_NOSUPER 2 /* Skip superpages. */
1954 * vm_page_scan_contig:
1956 * Scan vm_page_array[] between the specified entries "m_start" and
1957 * "m_end" for a run of contiguous physical pages that satisfy the
1958 * specified conditions, and return the lowest page in the run. The
1959 * specified "alignment" determines the alignment of the lowest physical
1960 * page in the run. If the specified "boundary" is non-zero, then the
1961 * run of physical pages cannot span a physical address that is a
1962 * multiple of "boundary".
1964 * "m_end" is never dereferenced, so it need not point to a vm_page
1965 * structure within vm_page_array[].
1967 * "npages" must be greater than zero. "m_start" and "m_end" must not
1968 * span a hole (or discontiguity) in the physical address space. Both
1969 * "alignment" and "boundary" must be a power of two.
1972 vm_page_scan_contig(u_long npages, vm_page_t m_start, vm_page_t m_end,
1973 u_long alignment, vm_paddr_t boundary, int options)
1975 struct mtx *m_mtx, *new_mtx;
1979 #if VM_NRESERVLEVEL > 0
1982 int m_inc, order, run_ext, run_len;
1984 KASSERT(npages > 0, ("npages is 0"));
1985 KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
1986 KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
1990 for (m = m_start; m < m_end && run_len < npages; m += m_inc) {
1991 KASSERT((m->flags & (PG_FICTITIOUS | PG_MARKER)) == 0,
1992 ("page %p is PG_FICTITIOUS or PG_MARKER", m));
1995 * If the current page would be the start of a run, check its
1996 * physical address against the end, alignment, and boundary
1997 * conditions. If it doesn't satisfy these conditions, either
1998 * terminate the scan or advance to the next page that
1999 * satisfies the failed condition.
2002 KASSERT(m_run == NULL, ("m_run != NULL"));
2003 if (m + npages > m_end)
2005 pa = VM_PAGE_TO_PHYS(m);
2006 if ((pa & (alignment - 1)) != 0) {
2007 m_inc = atop(roundup2(pa, alignment) - pa);
2010 if (rounddown2(pa ^ (pa + ptoa(npages) - 1),
2012 m_inc = atop(roundup2(pa, boundary) - pa);
2016 KASSERT(m_run != NULL, ("m_run == NULL"));
2019 * Avoid releasing and reacquiring the same page lock.
2021 new_mtx = vm_page_lockptr(m);
2022 if (m_mtx != new_mtx) {
2030 if (m->wire_count != 0 || m->hold_count != 0)
2032 #if VM_NRESERVLEVEL > 0
2033 else if ((level = vm_reserv_level(m)) >= 0 &&
2034 (options & VPSC_NORESERV) != 0) {
2036 /* Advance to the end of the reservation. */
2037 pa = VM_PAGE_TO_PHYS(m);
2038 m_inc = atop(roundup2(pa + 1, vm_reserv_size(level)) -
2042 else if ((object = m->object) != NULL) {
2044 * The page is considered eligible for relocation if
2045 * and only if it could be laundered or reclaimed by
2048 if (!VM_OBJECT_TRYRLOCK(object)) {
2050 VM_OBJECT_RLOCK(object);
2052 if (m->object != object) {
2054 * The page may have been freed.
2056 VM_OBJECT_RUNLOCK(object);
2058 } else if (m->wire_count != 0 ||
2059 m->hold_count != 0) {
2064 KASSERT((m->flags & PG_UNHOLDFREE) == 0,
2065 ("page %p is PG_UNHOLDFREE", m));
2066 /* Don't care: PG_NODUMP, PG_ZERO. */
2067 if (object->type != OBJT_DEFAULT &&
2068 object->type != OBJT_SWAP &&
2069 object->type != OBJT_VNODE) {
2071 #if VM_NRESERVLEVEL > 0
2072 } else if ((options & VPSC_NOSUPER) != 0 &&
2073 (level = vm_reserv_level_iffullpop(m)) >= 0) {
2075 /* Advance to the end of the superpage. */
2076 pa = VM_PAGE_TO_PHYS(m);
2077 m_inc = atop(roundup2(pa + 1,
2078 vm_reserv_size(level)) - pa);
2080 } else if (object->memattr == VM_MEMATTR_DEFAULT &&
2081 m->queue != PQ_NONE && !vm_page_busied(m)) {
2083 * The page is allocated but eligible for
2084 * relocation. Extend the current run by one
2087 KASSERT(pmap_page_get_memattr(m) ==
2089 ("page %p has an unexpected memattr", m));
2090 KASSERT((m->oflags & (VPO_SWAPINPROG |
2091 VPO_SWAPSLEEP | VPO_UNMANAGED)) == 0,
2092 ("page %p has unexpected oflags", m));
2093 /* Don't care: VPO_NOSYNC. */
2098 VM_OBJECT_RUNLOCK(object);
2099 #if VM_NRESERVLEVEL > 0
2100 } else if (level >= 0) {
2102 * The page is reserved but not yet allocated. In
2103 * other words, it is still free. Extend the current
2108 } else if ((order = m->order) < VM_NFREEORDER) {
2110 * The page is enqueued in the physical memory
2111 * allocator's free page queues. Moreover, it is the
2112 * first page in a power-of-two-sized run of
2113 * contiguous free pages. Add these pages to the end
2114 * of the current run, and jump ahead.
2116 run_ext = 1 << order;
2120 * Skip the page for one of the following reasons: (1)
2121 * It is enqueued in the physical memory allocator's
2122 * free page queues. However, it is not the first
2123 * page in a run of contiguous free pages. (This case
2124 * rarely occurs because the scan is performed in
2125 * ascending order.) (2) It is not reserved, and it is
2126 * transitioning from free to allocated. (Conversely,
2127 * the transition from allocated to free for managed
2128 * pages is blocked by the page lock.) (3) It is
2129 * allocated but not contained by an object and not
2130 * wired, e.g., allocated by Xen's balloon driver.
2136 * Extend or reset the current run of pages.
2151 if (run_len >= npages)
2157 * vm_page_reclaim_run:
2159 * Try to relocate each of the allocated virtual pages within the
2160 * specified run of physical pages to a new physical address. Free the
2161 * physical pages underlying the relocated virtual pages. A virtual page
2162 * is relocatable if and only if it could be laundered or reclaimed by
2163 * the page daemon. Whenever possible, a virtual page is relocated to a
2164 * physical address above "high".
2166 * Returns 0 if every physical page within the run was already free or
2167 * just freed by a successful relocation. Otherwise, returns a non-zero
2168 * value indicating why the last attempt to relocate a virtual page was
2171 * "req_class" must be an allocation class.
2174 vm_page_reclaim_run(int req_class, u_long npages, vm_page_t m_run,
2177 struct mtx *m_mtx, *new_mtx;
2178 struct spglist free;
2181 vm_page_t m, m_end, m_new;
2182 int error, order, req;
2184 KASSERT((req_class & VM_ALLOC_CLASS_MASK) == req_class,
2185 ("req_class is not an allocation class"));
2189 m_end = m_run + npages;
2191 for (; error == 0 && m < m_end; m++) {
2192 KASSERT((m->flags & (PG_FICTITIOUS | PG_MARKER)) == 0,
2193 ("page %p is PG_FICTITIOUS or PG_MARKER", m));
2196 * Avoid releasing and reacquiring the same page lock.
2198 new_mtx = vm_page_lockptr(m);
2199 if (m_mtx != new_mtx) {
2206 if (m->wire_count != 0 || m->hold_count != 0)
2208 else if ((object = m->object) != NULL) {
2210 * The page is relocated if and only if it could be
2211 * laundered or reclaimed by the page daemon.
2213 if (!VM_OBJECT_TRYWLOCK(object)) {
2215 VM_OBJECT_WLOCK(object);
2217 if (m->object != object) {
2219 * The page may have been freed.
2221 VM_OBJECT_WUNLOCK(object);
2223 } else if (m->wire_count != 0 ||
2224 m->hold_count != 0) {
2229 KASSERT((m->flags & PG_UNHOLDFREE) == 0,
2230 ("page %p is PG_UNHOLDFREE", m));
2231 /* Don't care: PG_NODUMP, PG_ZERO. */
2232 if (object->type != OBJT_DEFAULT &&
2233 object->type != OBJT_SWAP &&
2234 object->type != OBJT_VNODE)
2236 else if (object->memattr != VM_MEMATTR_DEFAULT)
2238 else if (m->queue != PQ_NONE && !vm_page_busied(m)) {
2239 KASSERT(pmap_page_get_memattr(m) ==
2241 ("page %p has an unexpected memattr", m));
2242 KASSERT((m->oflags & (VPO_SWAPINPROG |
2243 VPO_SWAPSLEEP | VPO_UNMANAGED)) == 0,
2244 ("page %p has unexpected oflags", m));
2245 /* Don't care: VPO_NOSYNC. */
2246 if (m->valid != 0) {
2248 * First, try to allocate a new page
2249 * that is above "high". Failing
2250 * that, try to allocate a new page
2251 * that is below "m_run". Allocate
2252 * the new page between the end of
2253 * "m_run" and "high" only as a last
2256 req = req_class | VM_ALLOC_NOOBJ;
2257 if ((m->flags & PG_NODUMP) != 0)
2258 req |= VM_ALLOC_NODUMP;
2259 if (trunc_page(high) !=
2260 ~(vm_paddr_t)PAGE_MASK) {
2261 m_new = vm_page_alloc_contig(
2266 VM_MEMATTR_DEFAULT);
2269 if (m_new == NULL) {
2270 pa = VM_PAGE_TO_PHYS(m_run);
2271 m_new = vm_page_alloc_contig(
2273 0, pa - 1, PAGE_SIZE, 0,
2274 VM_MEMATTR_DEFAULT);
2276 if (m_new == NULL) {
2278 m_new = vm_page_alloc_contig(
2280 pa, high, PAGE_SIZE, 0,
2281 VM_MEMATTR_DEFAULT);
2283 if (m_new == NULL) {
2287 KASSERT(m_new->wire_count == 0,
2288 ("page %p is wired", m));
2291 * Replace "m" with the new page. For
2292 * vm_page_replace(), "m" must be busy
2293 * and dequeued. Finally, change "m"
2294 * as if vm_page_free() was called.
2296 if (object->ref_count != 0)
2298 m_new->aflags = m->aflags;
2299 KASSERT(m_new->oflags == VPO_UNMANAGED,
2300 ("page %p is managed", m));
2301 m_new->oflags = m->oflags & VPO_NOSYNC;
2302 pmap_copy_page(m, m_new);
2303 m_new->valid = m->valid;
2304 m_new->dirty = m->dirty;
2305 m->flags &= ~PG_ZERO;
2308 vm_page_replace_checked(m_new, object,
2314 * The new page must be deactivated
2315 * before the object is unlocked.
2317 new_mtx = vm_page_lockptr(m_new);
2318 if (m_mtx != new_mtx) {
2323 vm_page_deactivate(m_new);
2325 m->flags &= ~PG_ZERO;
2328 KASSERT(m->dirty == 0,
2329 ("page %p is dirty", m));
2331 SLIST_INSERT_HEAD(&free, m, plinks.s.ss);
2335 VM_OBJECT_WUNLOCK(object);
2337 mtx_lock(&vm_page_queue_free_mtx);
2339 if (order < VM_NFREEORDER) {
2341 * The page is enqueued in the physical memory
2342 * allocator's free page queues. Moreover, it
2343 * is the first page in a power-of-two-sized
2344 * run of contiguous free pages. Jump ahead
2345 * to the last page within that run, and
2346 * continue from there.
2348 m += (1 << order) - 1;
2350 #if VM_NRESERVLEVEL > 0
2351 else if (vm_reserv_is_page_free(m))
2354 mtx_unlock(&vm_page_queue_free_mtx);
2355 if (order == VM_NFREEORDER)
2361 if ((m = SLIST_FIRST(&free)) != NULL) {
2362 mtx_lock(&vm_page_queue_free_mtx);
2364 SLIST_REMOVE_HEAD(&free, plinks.s.ss);
2365 vm_phys_freecnt_adj(m, 1);
2366 #if VM_NRESERVLEVEL > 0
2367 if (!vm_reserv_free_page(m))
2371 vm_phys_free_pages(m, 0);
2372 } while ((m = SLIST_FIRST(&free)) != NULL);
2373 vm_page_free_wakeup();
2374 mtx_unlock(&vm_page_queue_free_mtx);
2381 CTASSERT(powerof2(NRUNS));
2383 #define RUN_INDEX(count) ((count) & (NRUNS - 1))
2385 #define MIN_RECLAIM 8
2388 * vm_page_reclaim_contig:
2390 * Reclaim allocated, contiguous physical memory satisfying the specified
2391 * conditions by relocating the virtual pages using that physical memory.
2392 * Returns true if reclamation is successful and false otherwise. Since
2393 * relocation requires the allocation of physical pages, reclamation may
2394 * fail due to a shortage of free pages. When reclamation fails, callers
2395 * are expected to perform VM_WAIT before retrying a failed allocation
2396 * operation, e.g., vm_page_alloc_contig().
2398 * The caller must always specify an allocation class through "req".
2400 * allocation classes:
2401 * VM_ALLOC_NORMAL normal process request
2402 * VM_ALLOC_SYSTEM system *really* needs a page
2403 * VM_ALLOC_INTERRUPT interrupt time request
2405 * The optional allocation flags are ignored.
2407 * "npages" must be greater than zero. Both "alignment" and "boundary"
2408 * must be a power of two.
2411 vm_page_reclaim_contig(int req, u_long npages, vm_paddr_t low, vm_paddr_t high,
2412 u_long alignment, vm_paddr_t boundary)
2414 vm_paddr_t curr_low;
2415 vm_page_t m_run, m_runs[NRUNS];
2416 u_long count, reclaimed;
2417 int error, i, options, req_class;
2419 KASSERT(npages > 0, ("npages is 0"));
2420 KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
2421 KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
2422 req_class = req & VM_ALLOC_CLASS_MASK;
2425 * The page daemon is allowed to dig deeper into the free page list.
2427 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
2428 req_class = VM_ALLOC_SYSTEM;
2431 * Return if the number of free pages cannot satisfy the requested
2434 count = vm_cnt.v_free_count;
2435 if (count < npages + vm_cnt.v_free_reserved || (count < npages +
2436 vm_cnt.v_interrupt_free_min && req_class == VM_ALLOC_SYSTEM) ||
2437 (count < npages && req_class == VM_ALLOC_INTERRUPT))
2441 * Scan up to three times, relaxing the restrictions ("options") on
2442 * the reclamation of reservations and superpages each time.
2444 for (options = VPSC_NORESERV;;) {
2446 * Find the highest runs that satisfy the given constraints
2447 * and restrictions, and record them in "m_runs".
2452 m_run = vm_phys_scan_contig(npages, curr_low, high,
2453 alignment, boundary, options);
2456 curr_low = VM_PAGE_TO_PHYS(m_run) + ptoa(npages);
2457 m_runs[RUN_INDEX(count)] = m_run;
2462 * Reclaim the highest runs in LIFO (descending) order until
2463 * the number of reclaimed pages, "reclaimed", is at least
2464 * MIN_RECLAIM. Reset "reclaimed" each time because each
2465 * reclamation is idempotent, and runs will (likely) recur
2466 * from one scan to the next as restrictions are relaxed.
2469 for (i = 0; count > 0 && i < NRUNS; i++) {
2471 m_run = m_runs[RUN_INDEX(count)];
2472 error = vm_page_reclaim_run(req_class, npages, m_run,
2475 reclaimed += npages;
2476 if (reclaimed >= MIN_RECLAIM)
2482 * Either relax the restrictions on the next scan or return if
2483 * the last scan had no restrictions.
2485 if (options == VPSC_NORESERV)
2486 options = VPSC_NOSUPER;
2487 else if (options == VPSC_NOSUPER)
2489 else if (options == VPSC_ANY)
2490 return (reclaimed != 0);
2495 * vm_wait: (also see VM_WAIT macro)
2497 * Sleep until free pages are available for allocation.
2498 * - Called in various places before memory allocations.
2504 mtx_lock(&vm_page_queue_free_mtx);
2505 if (curproc == pageproc) {
2506 vm_pageout_pages_needed = 1;
2507 msleep(&vm_pageout_pages_needed, &vm_page_queue_free_mtx,
2508 PDROP | PSWP, "VMWait", 0);
2510 if (__predict_false(pageproc == NULL))
2511 panic("vm_wait in early boot");
2512 if (!vm_pageout_wanted) {
2513 vm_pageout_wanted = true;
2514 wakeup(&vm_pageout_wanted);
2516 vm_pages_needed = true;
2517 msleep(&vm_cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PVM,
2523 * vm_waitpfault: (also see VM_WAITPFAULT macro)
2525 * Sleep until free pages are available for allocation.
2526 * - Called only in vm_fault so that processes page faulting
2527 * can be easily tracked.
2528 * - Sleeps at a lower priority than vm_wait() so that vm_wait()ing
2529 * processes will be able to grab memory first. Do not change
2530 * this balance without careful testing first.
2536 mtx_lock(&vm_page_queue_free_mtx);
2537 if (!vm_pageout_wanted) {
2538 vm_pageout_wanted = true;
2539 wakeup(&vm_pageout_wanted);
2541 vm_pages_needed = true;
2542 msleep(&vm_cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PUSER,
2546 struct vm_pagequeue *
2547 vm_page_pagequeue(vm_page_t m)
2550 if (vm_page_in_laundry(m))
2551 return (&vm_dom[0].vmd_pagequeues[m->queue]);
2553 return (&vm_phys_domain(m)->vmd_pagequeues[m->queue]);
2559 * Remove the given page from its current page queue.
2561 * The page must be locked.
2564 vm_page_dequeue(vm_page_t m)
2566 struct vm_pagequeue *pq;
2568 vm_page_assert_locked(m);
2569 KASSERT(m->queue < PQ_COUNT, ("vm_page_dequeue: page %p is not queued",
2571 pq = vm_page_pagequeue(m);
2572 vm_pagequeue_lock(pq);
2574 TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
2575 vm_pagequeue_cnt_dec(pq);
2576 vm_pagequeue_unlock(pq);
2580 * vm_page_dequeue_locked:
2582 * Remove the given page from its current page queue.
2584 * The page and page queue must be locked.
2587 vm_page_dequeue_locked(vm_page_t m)
2589 struct vm_pagequeue *pq;
2591 vm_page_lock_assert(m, MA_OWNED);
2592 pq = vm_page_pagequeue(m);
2593 vm_pagequeue_assert_locked(pq);
2595 TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
2596 vm_pagequeue_cnt_dec(pq);
2602 * Add the given page to the specified page queue.
2604 * The page must be locked.
2607 vm_page_enqueue(uint8_t queue, vm_page_t m)
2609 struct vm_pagequeue *pq;
2611 vm_page_lock_assert(m, MA_OWNED);
2612 KASSERT(queue < PQ_COUNT,
2613 ("vm_page_enqueue: invalid queue %u request for page %p",
2615 if (queue == PQ_LAUNDRY || queue == PQ_UNSWAPPABLE)
2616 pq = &vm_dom[0].vmd_pagequeues[queue];
2618 pq = &vm_phys_domain(m)->vmd_pagequeues[queue];
2619 vm_pagequeue_lock(pq);
2621 TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
2622 vm_pagequeue_cnt_inc(pq);
2623 vm_pagequeue_unlock(pq);
2629 * Move the given page to the tail of its current page queue.
2631 * The page must be locked.
2634 vm_page_requeue(vm_page_t m)
2636 struct vm_pagequeue *pq;
2638 vm_page_lock_assert(m, MA_OWNED);
2639 KASSERT(m->queue != PQ_NONE,
2640 ("vm_page_requeue: page %p is not queued", m));
2641 pq = vm_page_pagequeue(m);
2642 vm_pagequeue_lock(pq);
2643 TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
2644 TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
2645 vm_pagequeue_unlock(pq);
2649 * vm_page_requeue_locked:
2651 * Move the given page to the tail of its current page queue.
2653 * The page queue must be locked.
2656 vm_page_requeue_locked(vm_page_t m)
2658 struct vm_pagequeue *pq;
2660 KASSERT(m->queue != PQ_NONE,
2661 ("vm_page_requeue_locked: page %p is not queued", m));
2662 pq = vm_page_pagequeue(m);
2663 vm_pagequeue_assert_locked(pq);
2664 TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
2665 TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
2671 * Put the specified page on the active list (if appropriate).
2672 * Ensure that act_count is at least ACT_INIT but do not otherwise
2675 * The page must be locked.
2678 vm_page_activate(vm_page_t m)
2682 vm_page_lock_assert(m, MA_OWNED);
2683 if ((queue = m->queue) != PQ_ACTIVE) {
2684 if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) {
2685 if (m->act_count < ACT_INIT)
2686 m->act_count = ACT_INIT;
2687 if (queue != PQ_NONE)
2689 vm_page_enqueue(PQ_ACTIVE, m);
2691 KASSERT(queue == PQ_NONE,
2692 ("vm_page_activate: wired page %p is queued", m));
2694 if (m->act_count < ACT_INIT)
2695 m->act_count = ACT_INIT;
2700 * vm_page_free_wakeup:
2702 * Helper routine for vm_page_free_toq(). This routine is called
2703 * when a page is added to the free queues.
2705 * The page queues must be locked.
2708 vm_page_free_wakeup(void)
2711 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
2713 * if pageout daemon needs pages, then tell it that there are
2716 if (vm_pageout_pages_needed &&
2717 vm_cnt.v_free_count >= vm_cnt.v_pageout_free_min) {
2718 wakeup(&vm_pageout_pages_needed);
2719 vm_pageout_pages_needed = 0;
2722 * wakeup processes that are waiting on memory if we hit a
2723 * high water mark. And wakeup scheduler process if we have
2724 * lots of memory. this process will swapin processes.
2726 if (vm_pages_needed && !vm_page_count_min()) {
2727 vm_pages_needed = false;
2728 wakeup(&vm_cnt.v_free_count);
2735 * Returns the given page to the free list,
2736 * disassociating it with any VM object.
2738 * The object must be locked. The page must be locked if it is managed.
2741 vm_page_free_toq(vm_page_t m)
2744 if ((m->oflags & VPO_UNMANAGED) == 0) {
2745 vm_page_lock_assert(m, MA_OWNED);
2746 KASSERT(!pmap_page_is_mapped(m),
2747 ("vm_page_free_toq: freeing mapped page %p", m));
2749 KASSERT(m->queue == PQ_NONE,
2750 ("vm_page_free_toq: unmanaged page %p is queued", m));
2751 VM_CNT_INC(v_tfree);
2753 if (vm_page_sbusied(m))
2754 panic("vm_page_free: freeing busy page %p", m);
2757 * Unqueue, then remove page. Note that we cannot destroy
2758 * the page here because we do not want to call the pager's
2759 * callback routine until after we've put the page on the
2760 * appropriate free queue.
2766 * If fictitious remove object association and
2767 * return, otherwise delay object association removal.
2769 if ((m->flags & PG_FICTITIOUS) != 0) {
2776 if (m->wire_count != 0)
2777 panic("vm_page_free: freeing wired page %p", m);
2778 if (m->hold_count != 0) {
2779 m->flags &= ~PG_ZERO;
2780 KASSERT((m->flags & PG_UNHOLDFREE) == 0,
2781 ("vm_page_free: freeing PG_UNHOLDFREE page %p", m));
2782 m->flags |= PG_UNHOLDFREE;
2785 * Restore the default memory attribute to the page.
2787 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT)
2788 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT);
2791 * Insert the page into the physical memory allocator's free
2794 mtx_lock(&vm_page_queue_free_mtx);
2795 vm_phys_freecnt_adj(m, 1);
2796 #if VM_NRESERVLEVEL > 0
2797 if (!vm_reserv_free_page(m))
2801 vm_phys_free_pages(m, 0);
2802 vm_page_free_wakeup();
2803 mtx_unlock(&vm_page_queue_free_mtx);
2810 * Mark this page as wired down by yet
2811 * another map, removing it from paging queues
2814 * If the page is fictitious, then its wire count must remain one.
2816 * The page must be locked.
2819 vm_page_wire(vm_page_t m)
2823 * Only bump the wire statistics if the page is not already wired,
2824 * and only unqueue the page if it is on some queue (if it is unmanaged
2825 * it is already off the queues).
2827 vm_page_lock_assert(m, MA_OWNED);
2828 if ((m->flags & PG_FICTITIOUS) != 0) {
2829 KASSERT(m->wire_count == 1,
2830 ("vm_page_wire: fictitious page %p's wire count isn't one",
2834 if (m->wire_count == 0) {
2835 KASSERT((m->oflags & VPO_UNMANAGED) == 0 ||
2836 m->queue == PQ_NONE,
2837 ("vm_page_wire: unmanaged page %p is queued", m));
2839 atomic_add_int(&vm_cnt.v_wire_count, 1);
2842 KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m));
2848 * Release one wiring of the specified page, potentially allowing it to be
2849 * paged out. Returns TRUE if the number of wirings transitions to zero and
2852 * Only managed pages belonging to an object can be paged out. If the number
2853 * of wirings transitions to zero and the page is eligible for page out, then
2854 * the page is added to the specified paging queue (unless PQ_NONE is
2857 * If a page is fictitious, then its wire count must always be one.
2859 * A managed page must be locked.
2862 vm_page_unwire(vm_page_t m, uint8_t queue)
2865 KASSERT(queue < PQ_COUNT || queue == PQ_NONE,
2866 ("vm_page_unwire: invalid queue %u request for page %p",
2868 if ((m->oflags & VPO_UNMANAGED) == 0)
2869 vm_page_assert_locked(m);
2870 if ((m->flags & PG_FICTITIOUS) != 0) {
2871 KASSERT(m->wire_count == 1,
2872 ("vm_page_unwire: fictitious page %p's wire count isn't one", m));
2875 if (m->wire_count > 0) {
2877 if (m->wire_count == 0) {
2878 atomic_subtract_int(&vm_cnt.v_wire_count, 1);
2879 if ((m->oflags & VPO_UNMANAGED) == 0 &&
2880 m->object != NULL && queue != PQ_NONE)
2881 vm_page_enqueue(queue, m);
2886 panic("vm_page_unwire: page %p's wire count is zero", m);
2890 * Move the specified page to the inactive queue.
2892 * Normally, "noreuse" is FALSE, resulting in LRU ordering of the inactive
2893 * queue. However, setting "noreuse" to TRUE will accelerate the specified
2894 * page's reclamation, but it will not unmap the page from any address space.
2895 * This is implemented by inserting the page near the head of the inactive
2896 * queue, using a marker page to guide FIFO insertion ordering.
2898 * The page must be locked.
2901 _vm_page_deactivate(vm_page_t m, boolean_t noreuse)
2903 struct vm_pagequeue *pq;
2906 vm_page_assert_locked(m);
2909 * Ignore if the page is already inactive, unless it is unlikely to be
2912 if ((queue = m->queue) == PQ_INACTIVE && !noreuse)
2914 if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) {
2915 pq = &vm_phys_domain(m)->vmd_pagequeues[PQ_INACTIVE];
2916 /* Avoid multiple acquisitions of the inactive queue lock. */
2917 if (queue == PQ_INACTIVE) {
2918 vm_pagequeue_lock(pq);
2919 vm_page_dequeue_locked(m);
2921 if (queue != PQ_NONE)
2923 vm_pagequeue_lock(pq);
2925 m->queue = PQ_INACTIVE;
2927 TAILQ_INSERT_BEFORE(&vm_phys_domain(m)->vmd_inacthead,
2930 TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
2931 vm_pagequeue_cnt_inc(pq);
2932 vm_pagequeue_unlock(pq);
2937 * Move the specified page to the inactive queue.
2939 * The page must be locked.
2942 vm_page_deactivate(vm_page_t m)
2945 _vm_page_deactivate(m, FALSE);
2949 * Move the specified page to the inactive queue with the expectation
2950 * that it is unlikely to be reused.
2952 * The page must be locked.
2955 vm_page_deactivate_noreuse(vm_page_t m)
2958 _vm_page_deactivate(m, TRUE);
2964 * Put a page in the laundry.
2967 vm_page_launder(vm_page_t m)
2971 vm_page_assert_locked(m);
2972 if ((queue = m->queue) != PQ_LAUNDRY) {
2973 if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) {
2974 if (queue != PQ_NONE)
2976 vm_page_enqueue(PQ_LAUNDRY, m);
2978 KASSERT(queue == PQ_NONE,
2979 ("wired page %p is queued", m));
2984 * vm_page_unswappable
2986 * Put a page in the PQ_UNSWAPPABLE holding queue.
2989 vm_page_unswappable(vm_page_t m)
2992 vm_page_assert_locked(m);
2993 KASSERT(m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0,
2994 ("page %p already unswappable", m));
2995 if (m->queue != PQ_NONE)
2997 vm_page_enqueue(PQ_UNSWAPPABLE, m);
3001 * vm_page_try_to_free()
3003 * Attempt to free the page. If we cannot free it, we do nothing.
3004 * 1 is returned on success, 0 on failure.
3007 vm_page_try_to_free(vm_page_t m)
3010 vm_page_lock_assert(m, MA_OWNED);
3011 if (m->object != NULL)
3012 VM_OBJECT_ASSERT_WLOCKED(m->object);
3013 if (m->dirty || m->hold_count || m->wire_count ||
3014 (m->oflags & VPO_UNMANAGED) != 0 || vm_page_busied(m))
3026 * Apply the specified advice to the given page.
3028 * The object and page must be locked.
3031 vm_page_advise(vm_page_t m, int advice)
3034 vm_page_assert_locked(m);
3035 VM_OBJECT_ASSERT_WLOCKED(m->object);
3036 if (advice == MADV_FREE)
3038 * Mark the page clean. This will allow the page to be freed
3039 * without first paging it out. MADV_FREE pages are often
3040 * quickly reused by malloc(3), so we do not do anything that
3041 * would result in a page fault on a later access.
3044 else if (advice != MADV_DONTNEED) {
3045 if (advice == MADV_WILLNEED)
3046 vm_page_activate(m);
3051 * Clear any references to the page. Otherwise, the page daemon will
3052 * immediately reactivate the page.
3054 vm_page_aflag_clear(m, PGA_REFERENCED);
3056 if (advice != MADV_FREE && m->dirty == 0 && pmap_is_modified(m))
3060 * Place clean pages near the head of the inactive queue rather than
3061 * the tail, thus defeating the queue's LRU operation and ensuring that
3062 * the page will be reused quickly. Dirty pages not already in the
3063 * laundry are moved there.
3066 vm_page_deactivate_noreuse(m);
3072 * Grab a page, waiting until we are waken up due to the page
3073 * changing state. We keep on waiting, if the page continues
3074 * to be in the object. If the page doesn't exist, first allocate it
3075 * and then conditionally zero it.
3077 * This routine may sleep.
3079 * The object must be locked on entry. The lock will, however, be released
3080 * and reacquired if the routine sleeps.
3083 vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags)
3088 VM_OBJECT_ASSERT_WLOCKED(object);
3089 KASSERT((allocflags & VM_ALLOC_SBUSY) == 0 ||
3090 (allocflags & VM_ALLOC_IGN_SBUSY) != 0,
3091 ("vm_page_grab: VM_ALLOC_SBUSY/VM_ALLOC_IGN_SBUSY mismatch"));
3093 if ((m = vm_page_lookup(object, pindex)) != NULL) {
3094 sleep = (allocflags & VM_ALLOC_IGN_SBUSY) != 0 ?
3095 vm_page_xbusied(m) : vm_page_busied(m);
3097 if ((allocflags & VM_ALLOC_NOWAIT) != 0)
3100 * Reference the page before unlocking and
3101 * sleeping so that the page daemon is less
3102 * likely to reclaim it.
3104 vm_page_aflag_set(m, PGA_REFERENCED);
3106 VM_OBJECT_WUNLOCK(object);
3107 vm_page_busy_sleep(m, "pgrbwt", (allocflags &
3108 VM_ALLOC_IGN_SBUSY) != 0);
3109 VM_OBJECT_WLOCK(object);
3112 if ((allocflags & VM_ALLOC_WIRED) != 0) {
3118 (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) == 0)
3120 if ((allocflags & VM_ALLOC_SBUSY) != 0)
3125 m = vm_page_alloc(object, pindex, allocflags);
3127 if ((allocflags & VM_ALLOC_NOWAIT) != 0)
3129 VM_OBJECT_WUNLOCK(object);
3131 VM_OBJECT_WLOCK(object);
3134 if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0)
3140 * Mapping function for valid or dirty bits in a page.
3142 * Inputs are required to range within a page.
3145 vm_page_bits(int base, int size)
3151 base + size <= PAGE_SIZE,
3152 ("vm_page_bits: illegal base/size %d/%d", base, size)
3155 if (size == 0) /* handle degenerate case */
3158 first_bit = base >> DEV_BSHIFT;
3159 last_bit = (base + size - 1) >> DEV_BSHIFT;
3161 return (((vm_page_bits_t)2 << last_bit) -
3162 ((vm_page_bits_t)1 << first_bit));
3166 * vm_page_set_valid_range:
3168 * Sets portions of a page valid. The arguments are expected
3169 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
3170 * of any partial chunks touched by the range. The invalid portion of
3171 * such chunks will be zeroed.
3173 * (base + size) must be less then or equal to PAGE_SIZE.
3176 vm_page_set_valid_range(vm_page_t m, int base, int size)
3180 VM_OBJECT_ASSERT_WLOCKED(m->object);
3181 if (size == 0) /* handle degenerate case */
3185 * If the base is not DEV_BSIZE aligned and the valid
3186 * bit is clear, we have to zero out a portion of the
3189 if ((frag = rounddown2(base, DEV_BSIZE)) != base &&
3190 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0)
3191 pmap_zero_page_area(m, frag, base - frag);
3194 * If the ending offset is not DEV_BSIZE aligned and the
3195 * valid bit is clear, we have to zero out a portion of
3198 endoff = base + size;
3199 if ((frag = rounddown2(endoff, DEV_BSIZE)) != endoff &&
3200 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0)
3201 pmap_zero_page_area(m, endoff,
3202 DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
3205 * Assert that no previously invalid block that is now being validated
3208 KASSERT((~m->valid & vm_page_bits(base, size) & m->dirty) == 0,
3209 ("vm_page_set_valid_range: page %p is dirty", m));
3212 * Set valid bits inclusive of any overlap.
3214 m->valid |= vm_page_bits(base, size);
3218 * Clear the given bits from the specified page's dirty field.
3220 static __inline void
3221 vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits)
3224 #if PAGE_SIZE < 16384
3229 * If the object is locked and the page is neither exclusive busy nor
3230 * write mapped, then the page's dirty field cannot possibly be
3231 * set by a concurrent pmap operation.
3233 VM_OBJECT_ASSERT_WLOCKED(m->object);
3234 if (!vm_page_xbusied(m) && !pmap_page_is_write_mapped(m))
3235 m->dirty &= ~pagebits;
3238 * The pmap layer can call vm_page_dirty() without
3239 * holding a distinguished lock. The combination of
3240 * the object's lock and an atomic operation suffice
3241 * to guarantee consistency of the page dirty field.
3243 * For PAGE_SIZE == 32768 case, compiler already
3244 * properly aligns the dirty field, so no forcible
3245 * alignment is needed. Only require existence of
3246 * atomic_clear_64 when page size is 32768.
3248 addr = (uintptr_t)&m->dirty;
3249 #if PAGE_SIZE == 32768
3250 atomic_clear_64((uint64_t *)addr, pagebits);
3251 #elif PAGE_SIZE == 16384
3252 atomic_clear_32((uint32_t *)addr, pagebits);
3253 #else /* PAGE_SIZE <= 8192 */
3255 * Use a trick to perform a 32-bit atomic on the
3256 * containing aligned word, to not depend on the existence
3257 * of atomic_clear_{8, 16}.
3259 shift = addr & (sizeof(uint32_t) - 1);
3260 #if BYTE_ORDER == BIG_ENDIAN
3261 shift = (sizeof(uint32_t) - sizeof(m->dirty) - shift) * NBBY;
3265 addr &= ~(sizeof(uint32_t) - 1);
3266 atomic_clear_32((uint32_t *)addr, pagebits << shift);
3267 #endif /* PAGE_SIZE */
3272 * vm_page_set_validclean:
3274 * Sets portions of a page valid and clean. The arguments are expected
3275 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
3276 * of any partial chunks touched by the range. The invalid portion of
3277 * such chunks will be zero'd.
3279 * (base + size) must be less then or equal to PAGE_SIZE.
3282 vm_page_set_validclean(vm_page_t m, int base, int size)
3284 vm_page_bits_t oldvalid, pagebits;
3287 VM_OBJECT_ASSERT_WLOCKED(m->object);
3288 if (size == 0) /* handle degenerate case */
3292 * If the base is not DEV_BSIZE aligned and the valid
3293 * bit is clear, we have to zero out a portion of the
3296 if ((frag = rounddown2(base, DEV_BSIZE)) != base &&
3297 (m->valid & ((vm_page_bits_t)1 << (base >> DEV_BSHIFT))) == 0)
3298 pmap_zero_page_area(m, frag, base - frag);
3301 * If the ending offset is not DEV_BSIZE aligned and the
3302 * valid bit is clear, we have to zero out a portion of
3305 endoff = base + size;
3306 if ((frag = rounddown2(endoff, DEV_BSIZE)) != endoff &&
3307 (m->valid & ((vm_page_bits_t)1 << (endoff >> DEV_BSHIFT))) == 0)
3308 pmap_zero_page_area(m, endoff,
3309 DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
3312 * Set valid, clear dirty bits. If validating the entire
3313 * page we can safely clear the pmap modify bit. We also
3314 * use this opportunity to clear the VPO_NOSYNC flag. If a process
3315 * takes a write fault on a MAP_NOSYNC memory area the flag will
3318 * We set valid bits inclusive of any overlap, but we can only
3319 * clear dirty bits for DEV_BSIZE chunks that are fully within
3322 oldvalid = m->valid;
3323 pagebits = vm_page_bits(base, size);
3324 m->valid |= pagebits;
3326 if ((frag = base & (DEV_BSIZE - 1)) != 0) {
3327 frag = DEV_BSIZE - frag;
3333 pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1));
3335 if (base == 0 && size == PAGE_SIZE) {
3337 * The page can only be modified within the pmap if it is
3338 * mapped, and it can only be mapped if it was previously
3341 if (oldvalid == VM_PAGE_BITS_ALL)
3343 * Perform the pmap_clear_modify() first. Otherwise,
3344 * a concurrent pmap operation, such as
3345 * pmap_protect(), could clear a modification in the
3346 * pmap and set the dirty field on the page before
3347 * pmap_clear_modify() had begun and after the dirty
3348 * field was cleared here.
3350 pmap_clear_modify(m);
3352 m->oflags &= ~VPO_NOSYNC;
3353 } else if (oldvalid != VM_PAGE_BITS_ALL)
3354 m->dirty &= ~pagebits;
3356 vm_page_clear_dirty_mask(m, pagebits);
3360 vm_page_clear_dirty(vm_page_t m, int base, int size)
3363 vm_page_clear_dirty_mask(m, vm_page_bits(base, size));
3367 * vm_page_set_invalid:
3369 * Invalidates DEV_BSIZE'd chunks within a page. Both the
3370 * valid and dirty bits for the effected areas are cleared.
3373 vm_page_set_invalid(vm_page_t m, int base, int size)
3375 vm_page_bits_t bits;
3379 VM_OBJECT_ASSERT_WLOCKED(object);
3380 if (object->type == OBJT_VNODE && base == 0 && IDX_TO_OFF(m->pindex) +
3381 size >= object->un_pager.vnp.vnp_size)
3382 bits = VM_PAGE_BITS_ALL;
3384 bits = vm_page_bits(base, size);
3385 if (object->ref_count != 0 && m->valid == VM_PAGE_BITS_ALL &&
3388 KASSERT((bits == 0 && m->valid == VM_PAGE_BITS_ALL) ||
3389 !pmap_page_is_mapped(m),
3390 ("vm_page_set_invalid: page %p is mapped", m));
3396 * vm_page_zero_invalid()
3398 * The kernel assumes that the invalid portions of a page contain
3399 * garbage, but such pages can be mapped into memory by user code.
3400 * When this occurs, we must zero out the non-valid portions of the
3401 * page so user code sees what it expects.
3403 * Pages are most often semi-valid when the end of a file is mapped
3404 * into memory and the file's size is not page aligned.
3407 vm_page_zero_invalid(vm_page_t m, boolean_t setvalid)
3412 VM_OBJECT_ASSERT_WLOCKED(m->object);
3414 * Scan the valid bits looking for invalid sections that
3415 * must be zeroed. Invalid sub-DEV_BSIZE'd areas ( where the
3416 * valid bit may be set ) have already been zeroed by
3417 * vm_page_set_validclean().
3419 for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) {
3420 if (i == (PAGE_SIZE / DEV_BSIZE) ||
3421 (m->valid & ((vm_page_bits_t)1 << i))) {
3423 pmap_zero_page_area(m,
3424 b << DEV_BSHIFT, (i - b) << DEV_BSHIFT);
3431 * setvalid is TRUE when we can safely set the zero'd areas
3432 * as being valid. We can do this if there are no cache consistancy
3433 * issues. e.g. it is ok to do with UFS, but not ok to do with NFS.
3436 m->valid = VM_PAGE_BITS_ALL;
3442 * Is (partial) page valid? Note that the case where size == 0
3443 * will return FALSE in the degenerate case where the page is
3444 * entirely invalid, and TRUE otherwise.
3447 vm_page_is_valid(vm_page_t m, int base, int size)
3449 vm_page_bits_t bits;
3451 VM_OBJECT_ASSERT_LOCKED(m->object);
3452 bits = vm_page_bits(base, size);
3453 return (m->valid != 0 && (m->valid & bits) == bits);
3457 * vm_page_ps_is_valid:
3459 * Returns TRUE if the entire (super)page is valid and FALSE otherwise.
3462 vm_page_ps_is_valid(vm_page_t m)
3466 VM_OBJECT_ASSERT_LOCKED(m->object);
3467 npages = atop(pagesizes[m->psind]);
3470 * The physically contiguous pages that make up a superpage, i.e., a
3471 * page with a page size index ("psind") greater than zero, will
3472 * occupy adjacent entries in vm_page_array[].
3474 for (i = 0; i < npages; i++) {
3475 if (m[i].valid != VM_PAGE_BITS_ALL)
3482 * Set the page's dirty bits if the page is modified.
3485 vm_page_test_dirty(vm_page_t m)
3488 VM_OBJECT_ASSERT_WLOCKED(m->object);
3489 if (m->dirty != VM_PAGE_BITS_ALL && pmap_is_modified(m))
3494 vm_page_lock_KBI(vm_page_t m, const char *file, int line)
3497 mtx_lock_flags_(vm_page_lockptr(m), 0, file, line);
3501 vm_page_unlock_KBI(vm_page_t m, const char *file, int line)
3504 mtx_unlock_flags_(vm_page_lockptr(m), 0, file, line);
3508 vm_page_trylock_KBI(vm_page_t m, const char *file, int line)
3511 return (mtx_trylock_flags_(vm_page_lockptr(m), 0, file, line));
3514 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
3516 vm_page_assert_locked_KBI(vm_page_t m, const char *file, int line)
3519 vm_page_lock_assert_KBI(m, MA_OWNED, file, line);
3523 vm_page_lock_assert_KBI(vm_page_t m, int a, const char *file, int line)
3526 mtx_assert_(vm_page_lockptr(m), a, file, line);
3532 vm_page_object_lock_assert(vm_page_t m)
3536 * Certain of the page's fields may only be modified by the
3537 * holder of the containing object's lock or the exclusive busy.
3538 * holder. Unfortunately, the holder of the write busy is
3539 * not recorded, and thus cannot be checked here.
3541 if (m->object != NULL && !vm_page_xbusied(m))
3542 VM_OBJECT_ASSERT_WLOCKED(m->object);
3546 vm_page_assert_pga_writeable(vm_page_t m, uint8_t bits)
3549 if ((bits & PGA_WRITEABLE) == 0)
3553 * The PGA_WRITEABLE flag can only be set if the page is
3554 * managed, is exclusively busied or the object is locked.
3555 * Currently, this flag is only set by pmap_enter().
3557 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3558 ("PGA_WRITEABLE on unmanaged page"));
3559 if (!vm_page_xbusied(m))
3560 VM_OBJECT_ASSERT_LOCKED(m->object);
3564 #include "opt_ddb.h"
3566 #include <sys/kernel.h>
3568 #include <ddb/ddb.h>
3570 DB_SHOW_COMMAND(page, vm_page_print_page_info)
3573 db_printf("vm_cnt.v_free_count: %d\n", vm_cnt.v_free_count);
3574 db_printf("vm_cnt.v_inactive_count: %d\n", vm_cnt.v_inactive_count);
3575 db_printf("vm_cnt.v_active_count: %d\n", vm_cnt.v_active_count);
3576 db_printf("vm_cnt.v_laundry_count: %d\n", vm_cnt.v_laundry_count);
3577 db_printf("vm_cnt.v_wire_count: %d\n", vm_cnt.v_wire_count);
3578 db_printf("vm_cnt.v_free_reserved: %d\n", vm_cnt.v_free_reserved);
3579 db_printf("vm_cnt.v_free_min: %d\n", vm_cnt.v_free_min);
3580 db_printf("vm_cnt.v_free_target: %d\n", vm_cnt.v_free_target);
3581 db_printf("vm_cnt.v_inactive_target: %d\n", vm_cnt.v_inactive_target);
3584 DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info)
3588 db_printf("pq_free %d\n", vm_cnt.v_free_count);
3589 for (dom = 0; dom < vm_ndomains; dom++) {
3591 "dom %d page_cnt %d free %d pq_act %d pq_inact %d pq_laund %d pq_unsw %d\n",
3593 vm_dom[dom].vmd_page_count,
3594 vm_dom[dom].vmd_free_count,
3595 vm_dom[dom].vmd_pagequeues[PQ_ACTIVE].pq_cnt,
3596 vm_dom[dom].vmd_pagequeues[PQ_INACTIVE].pq_cnt,
3597 vm_dom[dom].vmd_pagequeues[PQ_LAUNDRY].pq_cnt,
3598 vm_dom[dom].vmd_pagequeues[PQ_UNSWAPPABLE].pq_cnt);
3602 DB_SHOW_COMMAND(pginfo, vm_page_print_pginfo)
3608 db_printf("show pginfo addr\n");
3612 phys = strchr(modif, 'p') != NULL;
3614 m = PHYS_TO_VM_PAGE(addr);
3616 m = (vm_page_t)addr;
3618 "page %p obj %p pidx 0x%jx phys 0x%jx q %d hold %d wire %d\n"
3619 " af 0x%x of 0x%x f 0x%x act %d busy %x valid 0x%x dirty 0x%x\n",
3620 m, m->object, (uintmax_t)m->pindex, (uintmax_t)m->phys_addr,
3621 m->queue, m->hold_count, m->wire_count, m->aflags, m->oflags,
3622 m->flags, m->act_count, m->busy_lock, m->valid, m->dirty);