2 * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU)
4 * Copyright (c) 1991 Regents of the University of California.
6 * Copyright (c) 1998 Matthew Dillon. All Rights Reserved.
8 * This code is derived from software contributed to Berkeley by
9 * The Mach Operating System project at Carnegie-Mellon University.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * from: @(#)vm_page.c 7.4 (Berkeley) 5/7/91
39 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40 * All rights reserved.
42 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
44 * Permission to use, copy, modify and distribute this software and
45 * its documentation is hereby granted, provided that both the copyright
46 * notice and this permission notice appear in all copies of the
47 * software, derivative works or modified versions, and any portions
48 * thereof, and that both notices appear in supporting documentation.
50 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
54 * Carnegie Mellon requests users of this software to return to
56 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
57 * School of Computer Science
58 * Carnegie Mellon University
59 * Pittsburgh PA 15213-3890
61 * any improvements or extensions that they make and grant Carnegie the
62 * rights to redistribute these changes.
66 * Resident memory management module.
69 #include <sys/cdefs.h>
70 __FBSDID("$FreeBSD$");
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/counter.h>
77 #include <sys/domainset.h>
78 #include <sys/kernel.h>
79 #include <sys/limits.h>
80 #include <sys/linker.h>
82 #include <sys/malloc.h>
84 #include <sys/msgbuf.h>
85 #include <sys/mutex.h>
87 #include <sys/rwlock.h>
88 #include <sys/sleepqueue.h>
90 #include <sys/sched.h>
92 #include <sys/sysctl.h>
93 #include <sys/vmmeter.h>
94 #include <sys/vnode.h>
98 #include <vm/vm_param.h>
99 #include <vm/vm_domainset.h>
100 #include <vm/vm_kern.h>
101 #include <vm/vm_map.h>
102 #include <vm/vm_object.h>
103 #include <vm/vm_page.h>
104 #include <vm/vm_pageout.h>
105 #include <vm/vm_phys.h>
106 #include <vm/vm_pagequeue.h>
107 #include <vm/vm_pager.h>
108 #include <vm/vm_radix.h>
109 #include <vm/vm_reserv.h>
110 #include <vm/vm_extern.h>
112 #include <vm/uma_int.h>
114 #include <machine/md_var.h>
116 extern int uma_startup_count(int);
117 extern void uma_startup(void *, int);
118 extern int vmem_startup_count(void);
120 struct vm_domain vm_dom[MAXMEMDOM];
122 DPCPU_DEFINE_STATIC(struct vm_batchqueue, pqbatch[MAXMEMDOM][PQ_COUNT]);
124 struct mtx_padalign __exclusive_cache_line pa_lock[PA_LOCK_COUNT];
126 struct mtx_padalign __exclusive_cache_line vm_domainset_lock;
127 /* The following fields are protected by the domainset lock. */
128 domainset_t __exclusive_cache_line vm_min_domains;
129 domainset_t __exclusive_cache_line vm_severe_domains;
130 static int vm_min_waiters;
131 static int vm_severe_waiters;
132 static int vm_pageproc_waiters;
134 static SYSCTL_NODE(_vm_stats, OID_AUTO, page, CTLFLAG_RD, 0,
135 "VM page statistics");
137 static counter_u64_t queue_ops = EARLY_COUNTER;
138 SYSCTL_COUNTER_U64(_vm_stats_page, OID_AUTO, queue_ops,
139 CTLFLAG_RD, &queue_ops,
140 "Number of batched queue operations");
142 static counter_u64_t queue_nops = EARLY_COUNTER;
143 SYSCTL_COUNTER_U64(_vm_stats_page, OID_AUTO, queue_nops,
144 CTLFLAG_RD, &queue_nops,
145 "Number of batched queue operations with no effects");
148 counter_startup(void)
151 queue_ops = counter_u64_alloc(M_WAITOK);
152 queue_nops = counter_u64_alloc(M_WAITOK);
154 SYSINIT(page_counters, SI_SUB_CPU, SI_ORDER_ANY, counter_startup, NULL);
157 * bogus page -- for I/O to/from partially complete buffers,
158 * or for paging into sparsely invalid regions.
160 vm_page_t bogus_page;
162 vm_page_t vm_page_array;
163 long vm_page_array_size;
166 static int boot_pages;
167 SYSCTL_INT(_vm, OID_AUTO, boot_pages, CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
169 "number of pages allocated for bootstrapping the VM system");
171 static int pa_tryrelock_restart;
172 SYSCTL_INT(_vm, OID_AUTO, tryrelock_restart, CTLFLAG_RD,
173 &pa_tryrelock_restart, 0, "Number of tryrelock restarts");
175 static TAILQ_HEAD(, vm_page) blacklist_head;
176 static int sysctl_vm_page_blacklist(SYSCTL_HANDLER_ARGS);
177 SYSCTL_PROC(_vm, OID_AUTO, page_blacklist, CTLTYPE_STRING | CTLFLAG_RD |
178 CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_page_blacklist, "A", "Blacklist pages");
180 static uma_zone_t fakepg_zone;
182 static void vm_page_alloc_check(vm_page_t m);
183 static void _vm_page_busy_sleep(vm_object_t obj, vm_page_t m,
184 const char *wmesg, bool nonshared, bool locked);
185 static void vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits);
186 static void vm_page_dequeue_complete(vm_page_t m);
187 static void vm_page_enqueue(vm_page_t m, uint8_t queue);
188 static void vm_page_init(void *dummy);
189 static int vm_page_insert_after(vm_page_t m, vm_object_t object,
190 vm_pindex_t pindex, vm_page_t mpred);
191 static void vm_page_insert_radixdone(vm_page_t m, vm_object_t object,
193 static void vm_page_mvqueue(vm_page_t m, uint8_t queue);
194 static int vm_page_reclaim_run(int req_class, int domain, u_long npages,
195 vm_page_t m_run, vm_paddr_t high);
196 static int vm_domain_alloc_fail(struct vm_domain *vmd, vm_object_t object,
198 static int vm_page_zone_import(void *arg, void **store, int cnt, int domain,
200 static void vm_page_zone_release(void *arg, void **store, int cnt);
202 SYSINIT(vm_page, SI_SUB_VM, SI_ORDER_SECOND, vm_page_init, NULL);
205 vm_page_init(void *dummy)
208 fakepg_zone = uma_zcreate("fakepg", sizeof(struct vm_page), NULL, NULL,
209 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE | UMA_ZONE_VM);
210 bogus_page = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ |
211 VM_ALLOC_NORMAL | VM_ALLOC_WIRED);
215 * The cache page zone is initialized later since we need to be able to allocate
216 * pages before UMA is fully initialized.
219 vm_page_init_cache_zones(void *dummy __unused)
221 struct vm_domain *vmd;
222 struct vm_pgcache *pgcache;
225 for (domain = 0; domain < vm_ndomains; domain++) {
226 vmd = VM_DOMAIN(domain);
229 * Don't allow the page caches to take up more than .25% of
232 if (vmd->vmd_page_count / 400 < 256 * mp_ncpus * VM_NFREEPOOL)
234 for (pool = 0; pool < VM_NFREEPOOL; pool++) {
235 pgcache = &vmd->vmd_pgcache[pool];
236 pgcache->domain = domain;
237 pgcache->pool = pool;
238 pgcache->zone = uma_zcache_create("vm pgcache",
239 sizeof(struct vm_page), NULL, NULL, NULL, NULL,
240 vm_page_zone_import, vm_page_zone_release, pgcache,
241 UMA_ZONE_MAXBUCKET | UMA_ZONE_VM);
242 (void)uma_zone_set_maxcache(pgcache->zone, 0);
246 SYSINIT(vm_page2, SI_SUB_VM_CONF, SI_ORDER_ANY, vm_page_init_cache_zones, NULL);
248 /* Make sure that u_long is at least 64 bits when PAGE_SIZE is 32K. */
249 #if PAGE_SIZE == 32768
251 CTASSERT(sizeof(u_long) >= 8);
256 * Try to acquire a physical address lock while a pmap is locked. If we
257 * fail to trylock we unlock and lock the pmap directly and cache the
258 * locked pa in *locked. The caller should then restart their loop in case
259 * the virtual to physical mapping has changed.
262 vm_page_pa_tryrelock(pmap_t pmap, vm_paddr_t pa, vm_paddr_t *locked)
269 PA_LOCK_ASSERT(lockpa, MA_OWNED);
270 if (PA_LOCKPTR(pa) == PA_LOCKPTR(lockpa))
277 atomic_add_int(&pa_tryrelock_restart, 1);
286 * Sets the page size, perhaps based upon the memory
287 * size. Must be called before any use of page-size
288 * dependent functions.
291 vm_set_page_size(void)
293 if (vm_cnt.v_page_size == 0)
294 vm_cnt.v_page_size = PAGE_SIZE;
295 if (((vm_cnt.v_page_size - 1) & vm_cnt.v_page_size) != 0)
296 panic("vm_set_page_size: page size not a power of two");
300 * vm_page_blacklist_next:
302 * Find the next entry in the provided string of blacklist
303 * addresses. Entries are separated by space, comma, or newline.
304 * If an invalid integer is encountered then the rest of the
305 * string is skipped. Updates the list pointer to the next
306 * character, or NULL if the string is exhausted or invalid.
309 vm_page_blacklist_next(char **list, char *end)
314 if (list == NULL || *list == NULL)
322 * If there's no end pointer then the buffer is coming from
323 * the kenv and we know it's null-terminated.
326 end = *list + strlen(*list);
328 /* Ensure that strtoq() won't walk off the end */
330 if (*end == '\n' || *end == ' ' || *end == ',')
333 printf("Blacklist not terminated, skipping\n");
339 for (pos = *list; *pos != '\0'; pos = cp) {
340 bad = strtoq(pos, &cp, 0);
341 if (*cp == '\0' || *cp == ' ' || *cp == ',' || *cp == '\n') {
350 if (*cp == '\0' || ++cp >= end)
354 return (trunc_page(bad));
356 printf("Garbage in RAM blacklist, skipping\n");
362 vm_page_blacklist_add(vm_paddr_t pa, bool verbose)
364 struct vm_domain *vmd;
368 m = vm_phys_paddr_to_vm_page(pa);
370 return (true); /* page does not exist, no failure */
372 vmd = vm_pagequeue_domain(m);
373 vm_domain_free_lock(vmd);
374 ret = vm_phys_unfree_page(m);
375 vm_domain_free_unlock(vmd);
377 vm_domain_freecnt_inc(vmd, -1);
378 TAILQ_INSERT_TAIL(&blacklist_head, m, listq);
380 printf("Skipping page with pa 0x%jx\n", (uintmax_t)pa);
386 * vm_page_blacklist_check:
388 * Iterate through the provided string of blacklist addresses, pulling
389 * each entry out of the physical allocator free list and putting it
390 * onto a list for reporting via the vm.page_blacklist sysctl.
393 vm_page_blacklist_check(char *list, char *end)
399 while (next != NULL) {
400 if ((pa = vm_page_blacklist_next(&next, end)) == 0)
402 vm_page_blacklist_add(pa, bootverbose);
407 * vm_page_blacklist_load:
409 * Search for a special module named "ram_blacklist". It'll be a
410 * plain text file provided by the user via the loader directive
414 vm_page_blacklist_load(char **list, char **end)
423 mod = preload_search_by_type("ram_blacklist");
425 ptr = preload_fetch_addr(mod);
426 len = preload_fetch_size(mod);
437 sysctl_vm_page_blacklist(SYSCTL_HANDLER_ARGS)
444 error = sysctl_wire_old_buffer(req, 0);
447 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
448 TAILQ_FOREACH(m, &blacklist_head, listq) {
449 sbuf_printf(&sbuf, "%s%#jx", first ? "" : ",",
450 (uintmax_t)m->phys_addr);
453 error = sbuf_finish(&sbuf);
459 * Initialize a dummy page for use in scans of the specified paging queue.
460 * In principle, this function only needs to set the flag PG_MARKER.
461 * Nonetheless, it write busies the page as a safety precaution.
464 vm_page_init_marker(vm_page_t marker, int queue, uint8_t aflags)
467 bzero(marker, sizeof(*marker));
468 marker->flags = PG_MARKER;
469 marker->aflags = aflags;
470 marker->busy_lock = VPB_SINGLE_EXCLUSIVER;
471 marker->queue = queue;
475 vm_page_domain_init(int domain)
477 struct vm_domain *vmd;
478 struct vm_pagequeue *pq;
481 vmd = VM_DOMAIN(domain);
482 bzero(vmd, sizeof(*vmd));
483 *__DECONST(char **, &vmd->vmd_pagequeues[PQ_INACTIVE].pq_name) =
484 "vm inactive pagequeue";
485 *__DECONST(char **, &vmd->vmd_pagequeues[PQ_ACTIVE].pq_name) =
486 "vm active pagequeue";
487 *__DECONST(char **, &vmd->vmd_pagequeues[PQ_LAUNDRY].pq_name) =
488 "vm laundry pagequeue";
489 *__DECONST(char **, &vmd->vmd_pagequeues[PQ_UNSWAPPABLE].pq_name) =
490 "vm unswappable pagequeue";
491 vmd->vmd_domain = domain;
492 vmd->vmd_page_count = 0;
493 vmd->vmd_free_count = 0;
495 vmd->vmd_oom = FALSE;
496 for (i = 0; i < PQ_COUNT; i++) {
497 pq = &vmd->vmd_pagequeues[i];
498 TAILQ_INIT(&pq->pq_pl);
499 mtx_init(&pq->pq_mutex, pq->pq_name, "vm pagequeue",
500 MTX_DEF | MTX_DUPOK);
502 vm_page_init_marker(&vmd->vmd_markers[i], i, 0);
504 mtx_init(&vmd->vmd_free_mtx, "vm page free queue", NULL, MTX_DEF);
505 mtx_init(&vmd->vmd_pageout_mtx, "vm pageout lock", NULL, MTX_DEF);
506 snprintf(vmd->vmd_name, sizeof(vmd->vmd_name), "%d", domain);
509 * inacthead is used to provide FIFO ordering for LRU-bypassing
512 vm_page_init_marker(&vmd->vmd_inacthead, PQ_INACTIVE, PGA_ENQUEUED);
513 TAILQ_INSERT_HEAD(&vmd->vmd_pagequeues[PQ_INACTIVE].pq_pl,
514 &vmd->vmd_inacthead, plinks.q);
517 * The clock pages are used to implement active queue scanning without
518 * requeues. Scans start at clock[0], which is advanced after the scan
519 * ends. When the two clock hands meet, they are reset and scanning
520 * resumes from the head of the queue.
522 vm_page_init_marker(&vmd->vmd_clock[0], PQ_ACTIVE, PGA_ENQUEUED);
523 vm_page_init_marker(&vmd->vmd_clock[1], PQ_ACTIVE, PGA_ENQUEUED);
524 TAILQ_INSERT_HEAD(&vmd->vmd_pagequeues[PQ_ACTIVE].pq_pl,
525 &vmd->vmd_clock[0], plinks.q);
526 TAILQ_INSERT_TAIL(&vmd->vmd_pagequeues[PQ_ACTIVE].pq_pl,
527 &vmd->vmd_clock[1], plinks.q);
531 * Initialize a physical page in preparation for adding it to the free
535 vm_page_init_page(vm_page_t m, vm_paddr_t pa, int segind)
540 m->busy_lock = VPB_UNBUSIED;
541 m->flags = m->aflags = 0;
546 m->order = VM_NFREEORDER;
547 m->pool = VM_FREEPOOL_DEFAULT;
548 m->valid = m->dirty = 0;
552 #ifndef PMAP_HAS_PAGE_ARRAY
554 vm_page_array_alloc(vm_offset_t *vaddr, vm_paddr_t end, vm_paddr_t page_range)
559 * Reserve an unmapped guard page to trap access to vm_page_array[-1].
560 * However, because this page is allocated from KVM, out-of-bounds
561 * accesses using the direct map will not be trapped.
566 * Allocate physical memory for the page structures, and map it.
568 new_end = trunc_page(end - page_range * sizeof(struct vm_page));
569 vm_page_array = (vm_page_t)pmap_map(vaddr, new_end, end,
570 VM_PROT_READ | VM_PROT_WRITE);
571 vm_page_array_size = page_range;
580 * Initializes the resident memory module. Allocates physical memory for
581 * bootstrapping UMA and some data structures that are used to manage
582 * physical pages. Initializes these structures, and populates the free
586 vm_page_startup(vm_offset_t vaddr)
588 struct vm_phys_seg *seg;
590 char *list, *listend;
592 vm_paddr_t end, high_avail, low_avail, new_end, page_range, size;
593 vm_paddr_t last_pa, pa;
595 int biggestone, i, segind;
599 #if defined(__i386__) && defined(VM_PHYSSEG_DENSE)
603 vaddr = round_page(vaddr);
605 vm_phys_early_startup();
606 biggestone = vm_phys_avail_largest();
607 end = phys_avail[biggestone+1];
610 * Initialize the page and queue locks.
612 mtx_init(&vm_domainset_lock, "vm domainset lock", NULL, MTX_DEF);
613 for (i = 0; i < PA_LOCK_COUNT; i++)
614 mtx_init(&pa_lock[i], "vm page", NULL, MTX_DEF);
615 for (i = 0; i < vm_ndomains; i++)
616 vm_page_domain_init(i);
619 * Allocate memory for use when boot strapping the kernel memory
620 * allocator. Tell UMA how many zones we are going to create
621 * before going fully functional. UMA will add its zones.
623 * VM startup zones: vmem, vmem_btag, VM OBJECT, RADIX NODE, MAP,
624 * KMAP ENTRY, MAP ENTRY, VMSPACE.
626 boot_pages = uma_startup_count(8);
628 #ifndef UMA_MD_SMALL_ALLOC
629 /* vmem_startup() calls uma_prealloc(). */
630 boot_pages += vmem_startup_count();
631 /* vm_map_startup() calls uma_prealloc(). */
632 boot_pages += howmany(MAX_KMAP,
633 UMA_SLAB_SPACE / sizeof(struct vm_map));
636 * Before going fully functional kmem_init() does allocation
637 * from "KMAP ENTRY" and vmem_create() does allocation from "vmem".
642 * CTFLAG_RDTUN doesn't work during the early boot process, so we must
643 * manually fetch the value.
645 TUNABLE_INT_FETCH("vm.boot_pages", &boot_pages);
646 new_end = end - (boot_pages * UMA_SLAB_SIZE);
647 new_end = trunc_page(new_end);
648 mapped = pmap_map(&vaddr, new_end, end,
649 VM_PROT_READ | VM_PROT_WRITE);
650 bzero((void *)mapped, end - new_end);
651 uma_startup((void *)mapped, boot_pages);
654 witness_size = round_page(witness_startup_count());
655 new_end -= witness_size;
656 mapped = pmap_map(&vaddr, new_end, new_end + witness_size,
657 VM_PROT_READ | VM_PROT_WRITE);
658 bzero((void *)mapped, witness_size);
659 witness_startup((void *)mapped);
662 #if defined(__aarch64__) || defined(__amd64__) || defined(__arm__) || \
663 defined(__i386__) || defined(__mips__) || defined(__riscv) || \
664 defined(__powerpc64__)
666 * Allocate a bitmap to indicate that a random physical page
667 * needs to be included in a minidump.
669 * The amd64 port needs this to indicate which direct map pages
670 * need to be dumped, via calls to dump_add_page()/dump_drop_page().
672 * However, i386 still needs this workspace internally within the
673 * minidump code. In theory, they are not needed on i386, but are
674 * included should the sf_buf code decide to use them.
677 for (i = 0; dump_avail[i + 1] != 0; i += 2)
678 if (dump_avail[i + 1] > last_pa)
679 last_pa = dump_avail[i + 1];
680 page_range = last_pa / PAGE_SIZE;
681 vm_page_dump_size = round_page(roundup2(page_range, NBBY) / NBBY);
682 new_end -= vm_page_dump_size;
683 vm_page_dump = (void *)(uintptr_t)pmap_map(&vaddr, new_end,
684 new_end + vm_page_dump_size, VM_PROT_READ | VM_PROT_WRITE);
685 bzero((void *)vm_page_dump, vm_page_dump_size);
689 #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) || \
690 defined(__riscv) || defined(__powerpc64__)
692 * Include the UMA bootstrap pages, witness pages and vm_page_dump
693 * in a crash dump. When pmap_map() uses the direct map, they are
694 * not automatically included.
696 for (pa = new_end; pa < end; pa += PAGE_SIZE)
699 phys_avail[biggestone + 1] = new_end;
702 * Request that the physical pages underlying the message buffer be
703 * included in a crash dump. Since the message buffer is accessed
704 * through the direct map, they are not automatically included.
706 pa = DMAP_TO_PHYS((vm_offset_t)msgbufp->msg_ptr);
707 last_pa = pa + round_page(msgbufsize);
708 while (pa < last_pa) {
714 * Compute the number of pages of memory that will be available for
715 * use, taking into account the overhead of a page structure per page.
716 * In other words, solve
717 * "available physical memory" - round_page(page_range *
718 * sizeof(struct vm_page)) = page_range * PAGE_SIZE
721 low_avail = phys_avail[0];
722 high_avail = phys_avail[1];
723 for (i = 0; i < vm_phys_nsegs; i++) {
724 if (vm_phys_segs[i].start < low_avail)
725 low_avail = vm_phys_segs[i].start;
726 if (vm_phys_segs[i].end > high_avail)
727 high_avail = vm_phys_segs[i].end;
729 /* Skip the first chunk. It is already accounted for. */
730 for (i = 2; phys_avail[i + 1] != 0; i += 2) {
731 if (phys_avail[i] < low_avail)
732 low_avail = phys_avail[i];
733 if (phys_avail[i + 1] > high_avail)
734 high_avail = phys_avail[i + 1];
736 first_page = low_avail / PAGE_SIZE;
737 #ifdef VM_PHYSSEG_SPARSE
739 for (i = 0; i < vm_phys_nsegs; i++)
740 size += vm_phys_segs[i].end - vm_phys_segs[i].start;
741 for (i = 0; phys_avail[i + 1] != 0; i += 2)
742 size += phys_avail[i + 1] - phys_avail[i];
743 #elif defined(VM_PHYSSEG_DENSE)
744 size = high_avail - low_avail;
746 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
749 #ifdef PMAP_HAS_PAGE_ARRAY
750 pmap_page_array_startup(size / PAGE_SIZE);
751 biggestone = vm_phys_avail_largest();
752 end = new_end = phys_avail[biggestone + 1];
754 #ifdef VM_PHYSSEG_DENSE
756 * In the VM_PHYSSEG_DENSE case, the number of pages can account for
757 * the overhead of a page structure per page only if vm_page_array is
758 * allocated from the last physical memory chunk. Otherwise, we must
759 * allocate page structures representing the physical memory
760 * underlying vm_page_array, even though they will not be used.
762 if (new_end != high_avail)
763 page_range = size / PAGE_SIZE;
767 page_range = size / (PAGE_SIZE + sizeof(struct vm_page));
770 * If the partial bytes remaining are large enough for
771 * a page (PAGE_SIZE) without a corresponding
772 * 'struct vm_page', then new_end will contain an
773 * extra page after subtracting the length of the VM
774 * page array. Compensate by subtracting an extra
777 if (size % (PAGE_SIZE + sizeof(struct vm_page)) >= PAGE_SIZE) {
778 if (new_end == high_avail)
779 high_avail -= PAGE_SIZE;
780 new_end -= PAGE_SIZE;
784 new_end = vm_page_array_alloc(&vaddr, end, page_range);
787 #if VM_NRESERVLEVEL > 0
789 * Allocate physical memory for the reservation management system's
790 * data structures, and map it.
792 new_end = vm_reserv_startup(&vaddr, new_end);
794 #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) || \
795 defined(__riscv) || defined(__powerpc64__)
797 * Include vm_page_array and vm_reserv_array in a crash dump.
799 for (pa = new_end; pa < end; pa += PAGE_SIZE)
802 phys_avail[biggestone + 1] = new_end;
805 * Add physical memory segments corresponding to the available
808 for (i = 0; phys_avail[i + 1] != 0; i += 2)
809 if (vm_phys_avail_size(i) != 0)
810 vm_phys_add_seg(phys_avail[i], phys_avail[i + 1]);
813 * Initialize the physical memory allocator.
818 * Initialize the page structures and add every available page to the
819 * physical memory allocator's free lists.
821 #if defined(__i386__) && defined(VM_PHYSSEG_DENSE)
822 for (ii = 0; ii < vm_page_array_size; ii++) {
823 m = &vm_page_array[ii];
824 vm_page_init_page(m, (first_page + ii) << PAGE_SHIFT, 0);
825 m->flags = PG_FICTITIOUS;
828 vm_cnt.v_page_count = 0;
829 for (segind = 0; segind < vm_phys_nsegs; segind++) {
830 seg = &vm_phys_segs[segind];
831 for (m = seg->first_page, pa = seg->start; pa < seg->end;
832 m++, pa += PAGE_SIZE)
833 vm_page_init_page(m, pa, segind);
836 * Add the segment to the free lists only if it is covered by
837 * one of the ranges in phys_avail. Because we've added the
838 * ranges to the vm_phys_segs array, we can assume that each
839 * segment is either entirely contained in one of the ranges,
840 * or doesn't overlap any of them.
842 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
843 struct vm_domain *vmd;
845 if (seg->start < phys_avail[i] ||
846 seg->end > phys_avail[i + 1])
850 pagecount = (u_long)atop(seg->end - seg->start);
852 vmd = VM_DOMAIN(seg->domain);
853 vm_domain_free_lock(vmd);
854 vm_phys_enqueue_contig(m, pagecount);
855 vm_domain_free_unlock(vmd);
856 vm_domain_freecnt_inc(vmd, pagecount);
857 vm_cnt.v_page_count += (u_int)pagecount;
859 vmd = VM_DOMAIN(seg->domain);
860 vmd->vmd_page_count += (u_int)pagecount;
861 vmd->vmd_segs |= 1UL << m->segind;
867 * Remove blacklisted pages from the physical memory allocator.
869 TAILQ_INIT(&blacklist_head);
870 vm_page_blacklist_load(&list, &listend);
871 vm_page_blacklist_check(list, listend);
873 list = kern_getenv("vm.blacklist");
874 vm_page_blacklist_check(list, NULL);
877 #if VM_NRESERVLEVEL > 0
879 * Initialize the reservation management system.
888 vm_page_reference(vm_page_t m)
891 vm_page_aflag_set(m, PGA_REFERENCED);
895 * vm_page_busy_acquire:
897 * Acquire the busy lock as described by VM_ALLOC_* flags. Will loop
898 * and drop the object lock if necessary.
901 vm_page_busy_acquire(vm_page_t m, int allocflags)
907 * The page-specific object must be cached because page
908 * identity can change during the sleep, causing the
909 * re-lock of a different object.
910 * It is assumed that a reference to the object is already
911 * held by the callers.
915 if ((allocflags & VM_ALLOC_SBUSY) == 0) {
916 if (vm_page_tryxbusy(m))
919 if (vm_page_trysbusy(m))
922 if ((allocflags & VM_ALLOC_NOWAIT) != 0)
925 locked = VM_OBJECT_WOWNED(obj);
928 MPASS(locked || vm_page_wired(m));
929 _vm_page_busy_sleep(obj, m, "vmpba",
930 (allocflags & VM_ALLOC_SBUSY) != 0, locked);
932 VM_OBJECT_WLOCK(obj);
933 MPASS(m->object == obj || m->object == NULL);
934 if ((allocflags & VM_ALLOC_WAITFAIL) != 0)
940 * vm_page_busy_downgrade:
942 * Downgrade an exclusive busy page into a single shared busy page.
945 vm_page_busy_downgrade(vm_page_t m)
949 vm_page_assert_xbusied(m);
953 if (atomic_fcmpset_rel_int(&m->busy_lock,
954 &x, VPB_SHARERS_WORD(1)))
957 if ((x & VPB_BIT_WAITERS) != 0)
963 * vm_page_busy_tryupgrade:
965 * Attempt to upgrade a single shared busy into an exclusive busy.
968 vm_page_busy_tryupgrade(vm_page_t m)
972 vm_page_assert_sbusied(m);
976 if (VPB_SHARERS(x) > 1)
978 KASSERT((x & ~VPB_BIT_WAITERS) == VPB_SHARERS_WORD(1),
979 ("vm_page_busy_tryupgrade: invalid lock state"));
980 if (!atomic_fcmpset_acq_int(&m->busy_lock, &x,
981 VPB_SINGLE_EXCLUSIVER | (x & VPB_BIT_WAITERS)))
990 * Return a positive value if the page is shared busied, 0 otherwise.
993 vm_page_sbusied(vm_page_t m)
998 return ((x & VPB_BIT_SHARED) != 0 && x != VPB_UNBUSIED);
1004 * Shared unbusy a page.
1007 vm_page_sunbusy(vm_page_t m)
1011 vm_page_assert_sbusied(m);
1015 if (VPB_SHARERS(x) > 1) {
1016 if (atomic_fcmpset_int(&m->busy_lock, &x,
1017 x - VPB_ONE_SHARER))
1021 KASSERT((x & ~VPB_BIT_WAITERS) == VPB_SHARERS_WORD(1),
1022 ("vm_page_sunbusy: invalid lock state"));
1023 if (!atomic_fcmpset_rel_int(&m->busy_lock, &x, VPB_UNBUSIED))
1025 if ((x & VPB_BIT_WAITERS) == 0)
1033 * vm_page_busy_sleep:
1035 * Sleep if the page is busy, using the page pointer as wchan.
1036 * This is used to implement the hard-path of busying mechanism.
1038 * If nonshared is true, sleep only if the page is xbusy.
1040 * The object lock must be held on entry and will be released on exit.
1043 vm_page_busy_sleep(vm_page_t m, const char *wmesg, bool nonshared)
1048 VM_OBJECT_ASSERT_LOCKED(obj);
1049 vm_page_lock_assert(m, MA_NOTOWNED);
1051 _vm_page_busy_sleep(obj, m, wmesg, nonshared, true);
1055 _vm_page_busy_sleep(vm_object_t obj, vm_page_t m, const char *wmesg,
1056 bool nonshared, bool locked)
1061 * If the object is busy we must wait for that to drain to zero
1062 * before trying the page again.
1064 if (obj != NULL && vm_object_busied(obj)) {
1066 VM_OBJECT_DROP(obj);
1067 vm_object_busy_wait(obj, wmesg);
1072 if (x == VPB_UNBUSIED || (nonshared && (x & VPB_BIT_SHARED) != 0) ||
1073 ((x & VPB_BIT_WAITERS) == 0 &&
1074 !atomic_cmpset_int(&m->busy_lock, x, x | VPB_BIT_WAITERS))) {
1076 VM_OBJECT_DROP(obj);
1081 VM_OBJECT_DROP(obj);
1082 sleepq_add(m, NULL, wmesg, 0, 0);
1083 sleepq_wait(m, PVM);
1089 * Try to shared busy a page.
1090 * If the operation succeeds 1 is returned otherwise 0.
1091 * The operation never sleeps.
1094 vm_page_trysbusy(vm_page_t m)
1102 if ((x & VPB_BIT_SHARED) == 0)
1105 * Reduce the window for transient busies that will trigger
1106 * false negatives in vm_page_ps_test().
1108 if (obj != NULL && vm_object_busied(obj))
1110 if (atomic_fcmpset_acq_int(&m->busy_lock, &x,
1111 x + VPB_ONE_SHARER))
1115 /* Refetch the object now that we're guaranteed that it is stable. */
1117 if (obj != NULL && vm_object_busied(obj)) {
1127 * Try to exclusive busy a page.
1128 * If the operation succeeds 1 is returned otherwise 0.
1129 * The operation never sleeps.
1132 vm_page_tryxbusy(vm_page_t m)
1136 if (atomic_cmpset_acq_int(&(m)->busy_lock, VPB_UNBUSIED,
1137 VPB_SINGLE_EXCLUSIVER) == 0)
1141 if (obj != NULL && vm_object_busied(obj)) {
1149 * vm_page_xunbusy_hard:
1151 * Called when unbusy has failed because there is a waiter.
1154 vm_page_xunbusy_hard(vm_page_t m)
1157 vm_page_assert_xbusied(m);
1162 atomic_store_rel_int(&m->busy_lock, VPB_UNBUSIED);
1167 * Avoid releasing and reacquiring the same page lock.
1170 vm_page_change_lock(vm_page_t m, struct mtx **mtx)
1174 mtx1 = vm_page_lockptr(m);
1184 * vm_page_unhold_pages:
1186 * Unhold each of the pages that is referenced by the given array.
1189 vm_page_unhold_pages(vm_page_t *ma, int count)
1192 for (; count != 0; count--) {
1193 vm_page_unwire(*ma, PQ_ACTIVE);
1199 PHYS_TO_VM_PAGE(vm_paddr_t pa)
1203 #ifdef VM_PHYSSEG_SPARSE
1204 m = vm_phys_paddr_to_vm_page(pa);
1206 m = vm_phys_fictitious_to_vm_page(pa);
1208 #elif defined(VM_PHYSSEG_DENSE)
1212 if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
1213 m = &vm_page_array[pi - first_page];
1216 return (vm_phys_fictitious_to_vm_page(pa));
1218 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
1225 * Create a fictitious page with the specified physical address and
1226 * memory attribute. The memory attribute is the only the machine-
1227 * dependent aspect of a fictitious page that must be initialized.
1230 vm_page_getfake(vm_paddr_t paddr, vm_memattr_t memattr)
1234 m = uma_zalloc(fakepg_zone, M_WAITOK | M_ZERO);
1235 vm_page_initfake(m, paddr, memattr);
1240 vm_page_initfake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
1243 if ((m->flags & PG_FICTITIOUS) != 0) {
1245 * The page's memattr might have changed since the
1246 * previous initialization. Update the pmap to the
1251 m->phys_addr = paddr;
1253 /* Fictitious pages don't use "segind". */
1254 m->flags = PG_FICTITIOUS;
1255 /* Fictitious pages don't use "order" or "pool". */
1256 m->oflags = VPO_UNMANAGED;
1257 m->busy_lock = VPB_SINGLE_EXCLUSIVER;
1258 /* Fictitious pages are unevictable. */
1262 pmap_page_set_memattr(m, memattr);
1268 * Release a fictitious page.
1271 vm_page_putfake(vm_page_t m)
1274 KASSERT((m->oflags & VPO_UNMANAGED) != 0, ("managed %p", m));
1275 KASSERT((m->flags & PG_FICTITIOUS) != 0,
1276 ("vm_page_putfake: bad page %p", m));
1277 if (vm_page_xbusied(m))
1279 uma_zfree(fakepg_zone, m);
1283 * vm_page_updatefake:
1285 * Update the given fictitious page to the specified physical address and
1289 vm_page_updatefake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
1292 KASSERT((m->flags & PG_FICTITIOUS) != 0,
1293 ("vm_page_updatefake: bad page %p", m));
1294 m->phys_addr = paddr;
1295 pmap_page_set_memattr(m, memattr);
1304 vm_page_free(vm_page_t m)
1307 m->flags &= ~PG_ZERO;
1308 vm_page_free_toq(m);
1312 * vm_page_free_zero:
1314 * Free a page to the zerod-pages queue
1317 vm_page_free_zero(vm_page_t m)
1320 m->flags |= PG_ZERO;
1321 vm_page_free_toq(m);
1325 * Unbusy and handle the page queueing for a page from a getpages request that
1326 * was optionally read ahead or behind.
1329 vm_page_readahead_finish(vm_page_t m)
1332 /* We shouldn't put invalid pages on queues. */
1333 KASSERT(!vm_page_none_valid(m), ("%s: %p is invalid", __func__, m));
1336 * Since the page is not the actually needed one, whether it should
1337 * be activated or deactivated is not obvious. Empirical results
1338 * have shown that deactivating the page is usually the best choice,
1339 * unless the page is wanted by another thread.
1342 if ((m->busy_lock & VPB_BIT_WAITERS) != 0)
1343 vm_page_activate(m);
1345 vm_page_deactivate(m);
1351 * vm_page_sleep_if_busy:
1353 * Sleep and release the object lock if the page is busied.
1354 * Returns TRUE if the thread slept.
1356 * The given page must be unlocked and object containing it must
1360 vm_page_sleep_if_busy(vm_page_t m, const char *msg)
1364 vm_page_lock_assert(m, MA_NOTOWNED);
1365 VM_OBJECT_ASSERT_WLOCKED(m->object);
1368 * The page-specific object must be cached because page
1369 * identity can change during the sleep, causing the
1370 * re-lock of a different object.
1371 * It is assumed that a reference to the object is already
1372 * held by the callers.
1375 if (vm_page_busied(m) || (obj != NULL && obj->busy)) {
1376 vm_page_busy_sleep(m, msg, false);
1377 VM_OBJECT_WLOCK(obj);
1384 * vm_page_sleep_if_xbusy:
1386 * Sleep and release the object lock if the page is xbusied.
1387 * Returns TRUE if the thread slept.
1389 * The given page must be unlocked and object containing it must
1393 vm_page_sleep_if_xbusy(vm_page_t m, const char *msg)
1397 vm_page_lock_assert(m, MA_NOTOWNED);
1398 VM_OBJECT_ASSERT_WLOCKED(m->object);
1401 * The page-specific object must be cached because page
1402 * identity can change during the sleep, causing the
1403 * re-lock of a different object.
1404 * It is assumed that a reference to the object is already
1405 * held by the callers.
1408 if (vm_page_xbusied(m) || (obj != NULL && obj->busy)) {
1409 vm_page_busy_sleep(m, msg, true);
1410 VM_OBJECT_WLOCK(obj);
1417 * vm_page_dirty_KBI: [ internal use only ]
1419 * Set all bits in the page's dirty field.
1421 * The object containing the specified page must be locked if the
1422 * call is made from the machine-independent layer.
1424 * See vm_page_clear_dirty_mask().
1426 * This function should only be called by vm_page_dirty().
1429 vm_page_dirty_KBI(vm_page_t m)
1432 /* Refer to this operation by its public name. */
1433 KASSERT(vm_page_all_valid(m), ("vm_page_dirty: page is invalid!"));
1434 m->dirty = VM_PAGE_BITS_ALL;
1438 * vm_page_insert: [ internal use only ]
1440 * Inserts the given mem entry into the object and object list.
1442 * The object must be locked.
1445 vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex)
1449 VM_OBJECT_ASSERT_WLOCKED(object);
1450 mpred = vm_radix_lookup_le(&object->rtree, pindex);
1451 return (vm_page_insert_after(m, object, pindex, mpred));
1455 * vm_page_insert_after:
1457 * Inserts the page "m" into the specified object at offset "pindex".
1459 * The page "mpred" must immediately precede the offset "pindex" within
1460 * the specified object.
1462 * The object must be locked.
1465 vm_page_insert_after(vm_page_t m, vm_object_t object, vm_pindex_t pindex,
1470 VM_OBJECT_ASSERT_WLOCKED(object);
1471 KASSERT(m->object == NULL,
1472 ("vm_page_insert_after: page already inserted"));
1473 if (mpred != NULL) {
1474 KASSERT(mpred->object == object,
1475 ("vm_page_insert_after: object doesn't contain mpred"));
1476 KASSERT(mpred->pindex < pindex,
1477 ("vm_page_insert_after: mpred doesn't precede pindex"));
1478 msucc = TAILQ_NEXT(mpred, listq);
1480 msucc = TAILQ_FIRST(&object->memq);
1482 KASSERT(msucc->pindex > pindex,
1483 ("vm_page_insert_after: msucc doesn't succeed pindex"));
1486 * Record the object/offset pair in this page.
1490 m->ref_count |= VPRC_OBJREF;
1493 * Now link into the object's ordered list of backed pages.
1495 if (vm_radix_insert(&object->rtree, m)) {
1498 m->ref_count &= ~VPRC_OBJREF;
1501 vm_page_insert_radixdone(m, object, mpred);
1506 * vm_page_insert_radixdone:
1508 * Complete page "m" insertion into the specified object after the
1509 * radix trie hooking.
1511 * The page "mpred" must precede the offset "m->pindex" within the
1514 * The object must be locked.
1517 vm_page_insert_radixdone(vm_page_t m, vm_object_t object, vm_page_t mpred)
1520 VM_OBJECT_ASSERT_WLOCKED(object);
1521 KASSERT(object != NULL && m->object == object,
1522 ("vm_page_insert_radixdone: page %p has inconsistent object", m));
1523 KASSERT((m->ref_count & VPRC_OBJREF) != 0,
1524 ("vm_page_insert_radixdone: page %p is missing object ref", m));
1525 if (mpred != NULL) {
1526 KASSERT(mpred->object == object,
1527 ("vm_page_insert_radixdone: object doesn't contain mpred"));
1528 KASSERT(mpred->pindex < m->pindex,
1529 ("vm_page_insert_radixdone: mpred doesn't precede pindex"));
1533 TAILQ_INSERT_AFTER(&object->memq, mpred, m, listq);
1535 TAILQ_INSERT_HEAD(&object->memq, m, listq);
1538 * Show that the object has one more resident page.
1540 object->resident_page_count++;
1543 * Hold the vnode until the last page is released.
1545 if (object->resident_page_count == 1 && object->type == OBJT_VNODE)
1546 vhold(object->handle);
1549 * Since we are inserting a new and possibly dirty page,
1550 * update the object's OBJ_MIGHTBEDIRTY flag.
1552 if (pmap_page_is_write_mapped(m))
1553 vm_object_set_writeable_dirty(object);
1557 * Do the work to remove a page from its object. The caller is responsible for
1558 * updating the page's fields to reflect this removal.
1561 vm_page_object_remove(vm_page_t m)
1567 VM_OBJECT_ASSERT_WLOCKED(object);
1568 KASSERT((m->ref_count & VPRC_OBJREF) != 0,
1569 ("page %p is missing its object ref", m));
1571 mrem = vm_radix_remove(&object->rtree, m->pindex);
1572 KASSERT(mrem == m, ("removed page %p, expected page %p", mrem, m));
1575 * Now remove from the object's list of backed pages.
1577 TAILQ_REMOVE(&object->memq, m, listq);
1580 * And show that the object has one fewer resident page.
1582 object->resident_page_count--;
1585 * The vnode may now be recycled.
1587 if (object->resident_page_count == 0 && object->type == OBJT_VNODE)
1588 vdrop(object->handle);
1594 * Removes the specified page from its containing object, but does not
1595 * invalidate any backing storage. Returns true if the object's reference
1596 * was the last reference to the page, and false otherwise.
1598 * The object must be locked.
1601 vm_page_remove(vm_page_t m)
1604 vm_page_object_remove(m);
1606 return (vm_page_drop(m, VPRC_OBJREF) == VPRC_OBJREF);
1612 * Returns the page associated with the object/offset
1613 * pair specified; if none is found, NULL is returned.
1615 * The object must be locked.
1618 vm_page_lookup(vm_object_t object, vm_pindex_t pindex)
1621 VM_OBJECT_ASSERT_LOCKED(object);
1622 return (vm_radix_lookup(&object->rtree, pindex));
1626 * vm_page_find_least:
1628 * Returns the page associated with the object with least pindex
1629 * greater than or equal to the parameter pindex, or NULL.
1631 * The object must be locked.
1634 vm_page_find_least(vm_object_t object, vm_pindex_t pindex)
1638 VM_OBJECT_ASSERT_LOCKED(object);
1639 if ((m = TAILQ_FIRST(&object->memq)) != NULL && m->pindex < pindex)
1640 m = vm_radix_lookup_ge(&object->rtree, pindex);
1645 * Returns the given page's successor (by pindex) within the object if it is
1646 * resident; if none is found, NULL is returned.
1648 * The object must be locked.
1651 vm_page_next(vm_page_t m)
1655 VM_OBJECT_ASSERT_LOCKED(m->object);
1656 if ((next = TAILQ_NEXT(m, listq)) != NULL) {
1657 MPASS(next->object == m->object);
1658 if (next->pindex != m->pindex + 1)
1665 * Returns the given page's predecessor (by pindex) within the object if it is
1666 * resident; if none is found, NULL is returned.
1668 * The object must be locked.
1671 vm_page_prev(vm_page_t m)
1675 VM_OBJECT_ASSERT_LOCKED(m->object);
1676 if ((prev = TAILQ_PREV(m, pglist, listq)) != NULL) {
1677 MPASS(prev->object == m->object);
1678 if (prev->pindex != m->pindex - 1)
1685 * Uses the page mnew as a replacement for an existing page at index
1686 * pindex which must be already present in the object.
1689 vm_page_replace(vm_page_t mnew, vm_object_t object, vm_pindex_t pindex)
1693 VM_OBJECT_ASSERT_WLOCKED(object);
1694 KASSERT(mnew->object == NULL && (mnew->ref_count & VPRC_OBJREF) == 0,
1695 ("vm_page_replace: page %p already in object", mnew));
1698 * This function mostly follows vm_page_insert() and
1699 * vm_page_remove() without the radix, object count and vnode
1700 * dance. Double check such functions for more comments.
1703 mnew->object = object;
1704 mnew->pindex = pindex;
1705 atomic_set_int(&mnew->ref_count, VPRC_OBJREF);
1706 mold = vm_radix_replace(&object->rtree, mnew);
1707 KASSERT(mold->queue == PQ_NONE,
1708 ("vm_page_replace: old page %p is on a paging queue", mold));
1710 /* Keep the resident page list in sorted order. */
1711 TAILQ_INSERT_AFTER(&object->memq, mold, mnew, listq);
1712 TAILQ_REMOVE(&object->memq, mold, listq);
1714 mold->object = NULL;
1715 atomic_clear_int(&mold->ref_count, VPRC_OBJREF);
1716 vm_page_xunbusy(mold);
1719 * The object's resident_page_count does not change because we have
1720 * swapped one page for another, but OBJ_MIGHTBEDIRTY.
1722 if (pmap_page_is_write_mapped(mnew))
1723 vm_object_set_writeable_dirty(object);
1730 * Move the given memory entry from its
1731 * current object to the specified target object/offset.
1733 * Note: swap associated with the page must be invalidated by the move. We
1734 * have to do this for several reasons: (1) we aren't freeing the
1735 * page, (2) we are dirtying the page, (3) the VM system is probably
1736 * moving the page from object A to B, and will then later move
1737 * the backing store from A to B and we can't have a conflict.
1739 * Note: we *always* dirty the page. It is necessary both for the
1740 * fact that we moved it, and because we may be invalidating
1743 * The objects must be locked.
1746 vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex)
1751 VM_OBJECT_ASSERT_WLOCKED(new_object);
1753 KASSERT(m->ref_count != 0, ("vm_page_rename: page %p has no refs", m));
1754 mpred = vm_radix_lookup_le(&new_object->rtree, new_pindex);
1755 KASSERT(mpred == NULL || mpred->pindex != new_pindex,
1756 ("vm_page_rename: pindex already renamed"));
1759 * Create a custom version of vm_page_insert() which does not depend
1760 * by m_prev and can cheat on the implementation aspects of the
1764 m->pindex = new_pindex;
1765 if (vm_radix_insert(&new_object->rtree, m)) {
1771 * The operation cannot fail anymore. The removal must happen before
1772 * the listq iterator is tainted.
1775 vm_page_object_remove(m);
1777 /* Return back to the new pindex to complete vm_page_insert(). */
1778 m->pindex = new_pindex;
1779 m->object = new_object;
1781 vm_page_insert_radixdone(m, new_object, mpred);
1789 * Allocate and return a page that is associated with the specified
1790 * object and offset pair. By default, this page is exclusive busied.
1792 * The caller must always specify an allocation class.
1794 * allocation classes:
1795 * VM_ALLOC_NORMAL normal process request
1796 * VM_ALLOC_SYSTEM system *really* needs a page
1797 * VM_ALLOC_INTERRUPT interrupt time request
1799 * optional allocation flags:
1800 * VM_ALLOC_COUNT(number) the number of additional pages that the caller
1801 * intends to allocate
1802 * VM_ALLOC_NOBUSY do not exclusive busy the page
1803 * VM_ALLOC_NODUMP do not include the page in a kernel core dump
1804 * VM_ALLOC_NOOBJ page is not associated with an object and
1805 * should not be exclusive busy
1806 * VM_ALLOC_SBUSY shared busy the allocated page
1807 * VM_ALLOC_WIRED wire the allocated page
1808 * VM_ALLOC_ZERO prefer a zeroed page
1811 vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req)
1814 return (vm_page_alloc_after(object, pindex, req, object != NULL ?
1815 vm_radix_lookup_le(&object->rtree, pindex) : NULL));
1819 vm_page_alloc_domain(vm_object_t object, vm_pindex_t pindex, int domain,
1823 return (vm_page_alloc_domain_after(object, pindex, domain, req,
1824 object != NULL ? vm_radix_lookup_le(&object->rtree, pindex) :
1829 * Allocate a page in the specified object with the given page index. To
1830 * optimize insertion of the page into the object, the caller must also specifiy
1831 * the resident page in the object with largest index smaller than the given
1832 * page index, or NULL if no such page exists.
1835 vm_page_alloc_after(vm_object_t object, vm_pindex_t pindex,
1836 int req, vm_page_t mpred)
1838 struct vm_domainset_iter di;
1842 vm_domainset_iter_page_init(&di, object, pindex, &domain, &req);
1844 m = vm_page_alloc_domain_after(object, pindex, domain, req,
1848 } while (vm_domainset_iter_page(&di, object, &domain) == 0);
1854 * Returns true if the number of free pages exceeds the minimum
1855 * for the request class and false otherwise.
1858 vm_domain_allocate(struct vm_domain *vmd, int req, int npages)
1860 u_int limit, old, new;
1862 req = req & VM_ALLOC_CLASS_MASK;
1865 * The page daemon is allowed to dig deeper into the free page list.
1867 if (curproc == pageproc && req != VM_ALLOC_INTERRUPT)
1868 req = VM_ALLOC_SYSTEM;
1869 if (req == VM_ALLOC_INTERRUPT)
1871 else if (req == VM_ALLOC_SYSTEM)
1872 limit = vmd->vmd_interrupt_free_min;
1874 limit = vmd->vmd_free_reserved;
1877 * Attempt to reserve the pages. Fail if we're below the limit.
1880 old = vmd->vmd_free_count;
1885 } while (atomic_fcmpset_int(&vmd->vmd_free_count, &old, new) == 0);
1887 /* Wake the page daemon if we've crossed the threshold. */
1888 if (vm_paging_needed(vmd, new) && !vm_paging_needed(vmd, old))
1889 pagedaemon_wakeup(vmd->vmd_domain);
1891 /* Only update bitsets on transitions. */
1892 if ((old >= vmd->vmd_free_min && new < vmd->vmd_free_min) ||
1893 (old >= vmd->vmd_free_severe && new < vmd->vmd_free_severe))
1900 vm_page_alloc_domain_after(vm_object_t object, vm_pindex_t pindex, int domain,
1901 int req, vm_page_t mpred)
1903 struct vm_domain *vmd;
1907 KASSERT((object != NULL) == ((req & VM_ALLOC_NOOBJ) == 0) &&
1908 (object != NULL || (req & VM_ALLOC_SBUSY) == 0) &&
1909 ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) !=
1910 (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)),
1911 ("inconsistent object(%p)/req(%x)", object, req));
1912 KASSERT(object == NULL || (req & VM_ALLOC_WAITOK) == 0,
1913 ("Can't sleep and retry object insertion."));
1914 KASSERT(mpred == NULL || mpred->pindex < pindex,
1915 ("mpred %p doesn't precede pindex 0x%jx", mpred,
1916 (uintmax_t)pindex));
1918 VM_OBJECT_ASSERT_WLOCKED(object);
1922 pool = object != NULL ? VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT;
1924 #if VM_NRESERVLEVEL > 0
1926 * Can we allocate the page from a reservation?
1928 if (vm_object_reserv(object) &&
1929 (m = vm_reserv_alloc_page(object, pindex, domain, req, mpred)) !=
1931 domain = vm_phys_domain(m);
1932 vmd = VM_DOMAIN(domain);
1936 vmd = VM_DOMAIN(domain);
1937 if (vmd->vmd_pgcache[pool].zone != NULL) {
1938 m = uma_zalloc(vmd->vmd_pgcache[pool].zone, M_NOWAIT);
1940 flags |= PG_PCPU_CACHE;
1944 if (vm_domain_allocate(vmd, req, 1)) {
1946 * If not, allocate it from the free page queues.
1948 vm_domain_free_lock(vmd);
1949 m = vm_phys_alloc_pages(domain, pool, 0);
1950 vm_domain_free_unlock(vmd);
1952 vm_domain_freecnt_inc(vmd, 1);
1953 #if VM_NRESERVLEVEL > 0
1954 if (vm_reserv_reclaim_inactive(domain))
1961 * Not allocatable, give up.
1963 if (vm_domain_alloc_fail(vmd, object, req))
1969 * At this point we had better have found a good page.
1973 vm_page_alloc_check(m);
1976 * Initialize the page. Only the PG_ZERO flag is inherited.
1978 if ((req & VM_ALLOC_ZERO) != 0)
1979 flags |= (m->flags & PG_ZERO);
1980 if ((req & VM_ALLOC_NODUMP) != 0)
1984 m->oflags = object == NULL || (object->flags & OBJ_UNMANAGED) != 0 ?
1986 m->busy_lock = VPB_UNBUSIED;
1987 if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ | VM_ALLOC_SBUSY)) == 0)
1988 m->busy_lock = VPB_SINGLE_EXCLUSIVER;
1989 if ((req & VM_ALLOC_SBUSY) != 0)
1990 m->busy_lock = VPB_SHARERS_WORD(1);
1991 if (req & VM_ALLOC_WIRED) {
1993 * The page lock is not required for wiring a page until that
1994 * page is inserted into the object.
2001 if (object != NULL) {
2002 if (vm_page_insert_after(m, object, pindex, mpred)) {
2003 if (req & VM_ALLOC_WIRED) {
2007 KASSERT(m->object == NULL, ("page %p has object", m));
2008 m->oflags = VPO_UNMANAGED;
2009 m->busy_lock = VPB_UNBUSIED;
2010 /* Don't change PG_ZERO. */
2011 vm_page_free_toq(m);
2012 if (req & VM_ALLOC_WAITFAIL) {
2013 VM_OBJECT_WUNLOCK(object);
2015 VM_OBJECT_WLOCK(object);
2020 /* Ignore device objects; the pager sets "memattr" for them. */
2021 if (object->memattr != VM_MEMATTR_DEFAULT &&
2022 (object->flags & OBJ_FICTITIOUS) == 0)
2023 pmap_page_set_memattr(m, object->memattr);
2031 * vm_page_alloc_contig:
2033 * Allocate a contiguous set of physical pages of the given size "npages"
2034 * from the free lists. All of the physical pages must be at or above
2035 * the given physical address "low" and below the given physical address
2036 * "high". The given value "alignment" determines the alignment of the
2037 * first physical page in the set. If the given value "boundary" is
2038 * non-zero, then the set of physical pages cannot cross any physical
2039 * address boundary that is a multiple of that value. Both "alignment"
2040 * and "boundary" must be a power of two.
2042 * If the specified memory attribute, "memattr", is VM_MEMATTR_DEFAULT,
2043 * then the memory attribute setting for the physical pages is configured
2044 * to the object's memory attribute setting. Otherwise, the memory
2045 * attribute setting for the physical pages is configured to "memattr",
2046 * overriding the object's memory attribute setting. However, if the
2047 * object's memory attribute setting is not VM_MEMATTR_DEFAULT, then the
2048 * memory attribute setting for the physical pages cannot be configured
2049 * to VM_MEMATTR_DEFAULT.
2051 * The specified object may not contain fictitious pages.
2053 * The caller must always specify an allocation class.
2055 * allocation classes:
2056 * VM_ALLOC_NORMAL normal process request
2057 * VM_ALLOC_SYSTEM system *really* needs a page
2058 * VM_ALLOC_INTERRUPT interrupt time request
2060 * optional allocation flags:
2061 * VM_ALLOC_NOBUSY do not exclusive busy the page
2062 * VM_ALLOC_NODUMP do not include the page in a kernel core dump
2063 * VM_ALLOC_NOOBJ page is not associated with an object and
2064 * should not be exclusive busy
2065 * VM_ALLOC_SBUSY shared busy the allocated page
2066 * VM_ALLOC_WIRED wire the allocated page
2067 * VM_ALLOC_ZERO prefer a zeroed page
2070 vm_page_alloc_contig(vm_object_t object, vm_pindex_t pindex, int req,
2071 u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
2072 vm_paddr_t boundary, vm_memattr_t memattr)
2074 struct vm_domainset_iter di;
2078 vm_domainset_iter_page_init(&di, object, pindex, &domain, &req);
2080 m = vm_page_alloc_contig_domain(object, pindex, domain, req,
2081 npages, low, high, alignment, boundary, memattr);
2084 } while (vm_domainset_iter_page(&di, object, &domain) == 0);
2090 vm_page_alloc_contig_domain(vm_object_t object, vm_pindex_t pindex, int domain,
2091 int req, u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
2092 vm_paddr_t boundary, vm_memattr_t memattr)
2094 struct vm_domain *vmd;
2095 vm_page_t m, m_ret, mpred;
2096 u_int busy_lock, flags, oflags;
2098 mpred = NULL; /* XXX: pacify gcc */
2099 KASSERT((object != NULL) == ((req & VM_ALLOC_NOOBJ) == 0) &&
2100 (object != NULL || (req & VM_ALLOC_SBUSY) == 0) &&
2101 ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) !=
2102 (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)),
2103 ("vm_page_alloc_contig: inconsistent object(%p)/req(%x)", object,
2105 KASSERT(object == NULL || (req & VM_ALLOC_WAITOK) == 0,
2106 ("Can't sleep and retry object insertion."));
2107 if (object != NULL) {
2108 VM_OBJECT_ASSERT_WLOCKED(object);
2109 KASSERT((object->flags & OBJ_FICTITIOUS) == 0,
2110 ("vm_page_alloc_contig: object %p has fictitious pages",
2113 KASSERT(npages > 0, ("vm_page_alloc_contig: npages is zero"));
2115 if (object != NULL) {
2116 mpred = vm_radix_lookup_le(&object->rtree, pindex);
2117 KASSERT(mpred == NULL || mpred->pindex != pindex,
2118 ("vm_page_alloc_contig: pindex already allocated"));
2122 * Can we allocate the pages without the number of free pages falling
2123 * below the lower bound for the allocation class?
2127 #if VM_NRESERVLEVEL > 0
2129 * Can we allocate the pages from a reservation?
2131 if (vm_object_reserv(object) &&
2132 (m_ret = vm_reserv_alloc_contig(object, pindex, domain, req,
2133 mpred, npages, low, high, alignment, boundary)) != NULL) {
2134 domain = vm_phys_domain(m_ret);
2135 vmd = VM_DOMAIN(domain);
2139 vmd = VM_DOMAIN(domain);
2140 if (vm_domain_allocate(vmd, req, npages)) {
2142 * allocate them from the free page queues.
2144 vm_domain_free_lock(vmd);
2145 m_ret = vm_phys_alloc_contig(domain, npages, low, high,
2146 alignment, boundary);
2147 vm_domain_free_unlock(vmd);
2148 if (m_ret == NULL) {
2149 vm_domain_freecnt_inc(vmd, npages);
2150 #if VM_NRESERVLEVEL > 0
2151 if (vm_reserv_reclaim_contig(domain, npages, low,
2152 high, alignment, boundary))
2157 if (m_ret == NULL) {
2158 if (vm_domain_alloc_fail(vmd, object, req))
2162 #if VM_NRESERVLEVEL > 0
2165 for (m = m_ret; m < &m_ret[npages]; m++) {
2167 vm_page_alloc_check(m);
2171 * Initialize the pages. Only the PG_ZERO flag is inherited.
2174 if ((req & VM_ALLOC_ZERO) != 0)
2176 if ((req & VM_ALLOC_NODUMP) != 0)
2178 oflags = object == NULL || (object->flags & OBJ_UNMANAGED) != 0 ?
2180 busy_lock = VPB_UNBUSIED;
2181 if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ | VM_ALLOC_SBUSY)) == 0)
2182 busy_lock = VPB_SINGLE_EXCLUSIVER;
2183 if ((req & VM_ALLOC_SBUSY) != 0)
2184 busy_lock = VPB_SHARERS_WORD(1);
2185 if ((req & VM_ALLOC_WIRED) != 0)
2186 vm_wire_add(npages);
2187 if (object != NULL) {
2188 if (object->memattr != VM_MEMATTR_DEFAULT &&
2189 memattr == VM_MEMATTR_DEFAULT)
2190 memattr = object->memattr;
2192 for (m = m_ret; m < &m_ret[npages]; m++) {
2194 m->flags = (m->flags | PG_NODUMP) & flags;
2195 m->busy_lock = busy_lock;
2196 if ((req & VM_ALLOC_WIRED) != 0)
2200 if (object != NULL) {
2201 if (vm_page_insert_after(m, object, pindex, mpred)) {
2202 if ((req & VM_ALLOC_WIRED) != 0)
2203 vm_wire_sub(npages);
2204 KASSERT(m->object == NULL,
2205 ("page %p has object", m));
2207 for (m = m_ret; m < &m_ret[npages]; m++) {
2209 (req & VM_ALLOC_WIRED) != 0)
2211 m->oflags = VPO_UNMANAGED;
2212 m->busy_lock = VPB_UNBUSIED;
2213 /* Don't change PG_ZERO. */
2214 vm_page_free_toq(m);
2216 if (req & VM_ALLOC_WAITFAIL) {
2217 VM_OBJECT_WUNLOCK(object);
2219 VM_OBJECT_WLOCK(object);
2226 if (memattr != VM_MEMATTR_DEFAULT)
2227 pmap_page_set_memattr(m, memattr);
2234 * Check a page that has been freshly dequeued from a freelist.
2237 vm_page_alloc_check(vm_page_t m)
2240 KASSERT(m->object == NULL, ("page %p has object", m));
2241 KASSERT(m->queue == PQ_NONE && (m->aflags & PGA_QUEUE_STATE_MASK) == 0,
2242 ("page %p has unexpected queue %d, flags %#x",
2243 m, m->queue, (m->aflags & PGA_QUEUE_STATE_MASK)));
2244 KASSERT(m->ref_count == 0, ("page %p has references", m));
2245 KASSERT(!vm_page_busied(m), ("page %p is busy", m));
2246 KASSERT(m->dirty == 0, ("page %p is dirty", m));
2247 KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT,
2248 ("page %p has unexpected memattr %d",
2249 m, pmap_page_get_memattr(m)));
2250 KASSERT(m->valid == 0, ("free page %p is valid", m));
2254 * vm_page_alloc_freelist:
2256 * Allocate a physical page from the specified free page list.
2258 * The caller must always specify an allocation class.
2260 * allocation classes:
2261 * VM_ALLOC_NORMAL normal process request
2262 * VM_ALLOC_SYSTEM system *really* needs a page
2263 * VM_ALLOC_INTERRUPT interrupt time request
2265 * optional allocation flags:
2266 * VM_ALLOC_COUNT(number) the number of additional pages that the caller
2267 * intends to allocate
2268 * VM_ALLOC_WIRED wire the allocated page
2269 * VM_ALLOC_ZERO prefer a zeroed page
2272 vm_page_alloc_freelist(int freelist, int req)
2274 struct vm_domainset_iter di;
2278 vm_domainset_iter_page_init(&di, NULL, 0, &domain, &req);
2280 m = vm_page_alloc_freelist_domain(domain, freelist, req);
2283 } while (vm_domainset_iter_page(&di, NULL, &domain) == 0);
2289 vm_page_alloc_freelist_domain(int domain, int freelist, int req)
2291 struct vm_domain *vmd;
2296 vmd = VM_DOMAIN(domain);
2298 if (vm_domain_allocate(vmd, req, 1)) {
2299 vm_domain_free_lock(vmd);
2300 m = vm_phys_alloc_freelist_pages(domain, freelist,
2301 VM_FREEPOOL_DIRECT, 0);
2302 vm_domain_free_unlock(vmd);
2304 vm_domain_freecnt_inc(vmd, 1);
2307 if (vm_domain_alloc_fail(vmd, NULL, req))
2312 vm_page_alloc_check(m);
2315 * Initialize the page. Only the PG_ZERO flag is inherited.
2319 if ((req & VM_ALLOC_ZERO) != 0)
2322 if ((req & VM_ALLOC_WIRED) != 0) {
2324 * The page lock is not required for wiring a page that does
2325 * not belong to an object.
2330 /* Unmanaged pages don't use "act_count". */
2331 m->oflags = VPO_UNMANAGED;
2336 vm_page_zone_import(void *arg, void **store, int cnt, int domain, int flags)
2338 struct vm_domain *vmd;
2339 struct vm_pgcache *pgcache;
2343 vmd = VM_DOMAIN(pgcache->domain);
2344 /* Only import if we can bring in a full bucket. */
2345 if (cnt == 1 || !vm_domain_allocate(vmd, VM_ALLOC_NORMAL, cnt))
2347 domain = vmd->vmd_domain;
2348 vm_domain_free_lock(vmd);
2349 i = vm_phys_alloc_npages(domain, pgcache->pool, cnt,
2350 (vm_page_t *)store);
2351 vm_domain_free_unlock(vmd);
2353 vm_domain_freecnt_inc(vmd, cnt - i);
2359 vm_page_zone_release(void *arg, void **store, int cnt)
2361 struct vm_domain *vmd;
2362 struct vm_pgcache *pgcache;
2367 vmd = VM_DOMAIN(pgcache->domain);
2368 vm_domain_free_lock(vmd);
2369 for (i = 0; i < cnt; i++) {
2370 m = (vm_page_t)store[i];
2371 vm_phys_free_pages(m, 0);
2373 vm_domain_free_unlock(vmd);
2374 vm_domain_freecnt_inc(vmd, cnt);
2377 #define VPSC_ANY 0 /* No restrictions. */
2378 #define VPSC_NORESERV 1 /* Skip reservations; implies VPSC_NOSUPER. */
2379 #define VPSC_NOSUPER 2 /* Skip superpages. */
2382 * vm_page_scan_contig:
2384 * Scan vm_page_array[] between the specified entries "m_start" and
2385 * "m_end" for a run of contiguous physical pages that satisfy the
2386 * specified conditions, and return the lowest page in the run. The
2387 * specified "alignment" determines the alignment of the lowest physical
2388 * page in the run. If the specified "boundary" is non-zero, then the
2389 * run of physical pages cannot span a physical address that is a
2390 * multiple of "boundary".
2392 * "m_end" is never dereferenced, so it need not point to a vm_page
2393 * structure within vm_page_array[].
2395 * "npages" must be greater than zero. "m_start" and "m_end" must not
2396 * span a hole (or discontiguity) in the physical address space. Both
2397 * "alignment" and "boundary" must be a power of two.
2400 vm_page_scan_contig(u_long npages, vm_page_t m_start, vm_page_t m_end,
2401 u_long alignment, vm_paddr_t boundary, int options)
2407 #if VM_NRESERVLEVEL > 0
2410 int m_inc, order, run_ext, run_len;
2412 KASSERT(npages > 0, ("npages is 0"));
2413 KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
2414 KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
2418 for (m = m_start; m < m_end && run_len < npages; m += m_inc) {
2419 KASSERT((m->flags & PG_MARKER) == 0,
2420 ("page %p is PG_MARKER", m));
2421 KASSERT((m->flags & PG_FICTITIOUS) == 0 || m->ref_count >= 1,
2422 ("fictitious page %p has invalid ref count", m));
2425 * If the current page would be the start of a run, check its
2426 * physical address against the end, alignment, and boundary
2427 * conditions. If it doesn't satisfy these conditions, either
2428 * terminate the scan or advance to the next page that
2429 * satisfies the failed condition.
2432 KASSERT(m_run == NULL, ("m_run != NULL"));
2433 if (m + npages > m_end)
2435 pa = VM_PAGE_TO_PHYS(m);
2436 if ((pa & (alignment - 1)) != 0) {
2437 m_inc = atop(roundup2(pa, alignment) - pa);
2440 if (rounddown2(pa ^ (pa + ptoa(npages) - 1),
2442 m_inc = atop(roundup2(pa, boundary) - pa);
2446 KASSERT(m_run != NULL, ("m_run == NULL"));
2448 vm_page_change_lock(m, &m_mtx);
2451 if (vm_page_wired(m))
2453 #if VM_NRESERVLEVEL > 0
2454 else if ((level = vm_reserv_level(m)) >= 0 &&
2455 (options & VPSC_NORESERV) != 0) {
2457 /* Advance to the end of the reservation. */
2458 pa = VM_PAGE_TO_PHYS(m);
2459 m_inc = atop(roundup2(pa + 1, vm_reserv_size(level)) -
2463 else if ((object = m->object) != NULL) {
2465 * The page is considered eligible for relocation if
2466 * and only if it could be laundered or reclaimed by
2469 if (!VM_OBJECT_TRYRLOCK(object)) {
2471 VM_OBJECT_RLOCK(object);
2473 if (m->object != object) {
2475 * The page may have been freed.
2477 VM_OBJECT_RUNLOCK(object);
2481 /* Don't care: PG_NODUMP, PG_ZERO. */
2482 if (object->type != OBJT_DEFAULT &&
2483 object->type != OBJT_SWAP &&
2484 object->type != OBJT_VNODE) {
2486 #if VM_NRESERVLEVEL > 0
2487 } else if ((options & VPSC_NOSUPER) != 0 &&
2488 (level = vm_reserv_level_iffullpop(m)) >= 0) {
2490 /* Advance to the end of the superpage. */
2491 pa = VM_PAGE_TO_PHYS(m);
2492 m_inc = atop(roundup2(pa + 1,
2493 vm_reserv_size(level)) - pa);
2495 } else if (object->memattr == VM_MEMATTR_DEFAULT &&
2496 vm_page_queue(m) != PQ_NONE && !vm_page_busied(m) &&
2497 !vm_page_wired(m)) {
2499 * The page is allocated but eligible for
2500 * relocation. Extend the current run by one
2503 KASSERT(pmap_page_get_memattr(m) ==
2505 ("page %p has an unexpected memattr", m));
2506 KASSERT((m->oflags & (VPO_SWAPINPROG |
2507 VPO_SWAPSLEEP | VPO_UNMANAGED)) == 0,
2508 ("page %p has unexpected oflags", m));
2509 /* Don't care: PGA_NOSYNC. */
2513 VM_OBJECT_RUNLOCK(object);
2514 #if VM_NRESERVLEVEL > 0
2515 } else if (level >= 0) {
2517 * The page is reserved but not yet allocated. In
2518 * other words, it is still free. Extend the current
2523 } else if ((order = m->order) < VM_NFREEORDER) {
2525 * The page is enqueued in the physical memory
2526 * allocator's free page queues. Moreover, it is the
2527 * first page in a power-of-two-sized run of
2528 * contiguous free pages. Add these pages to the end
2529 * of the current run, and jump ahead.
2531 run_ext = 1 << order;
2535 * Skip the page for one of the following reasons: (1)
2536 * It is enqueued in the physical memory allocator's
2537 * free page queues. However, it is not the first
2538 * page in a run of contiguous free pages. (This case
2539 * rarely occurs because the scan is performed in
2540 * ascending order.) (2) It is not reserved, and it is
2541 * transitioning from free to allocated. (Conversely,
2542 * the transition from allocated to free for managed
2543 * pages is blocked by the page lock.) (3) It is
2544 * allocated but not contained by an object and not
2545 * wired, e.g., allocated by Xen's balloon driver.
2551 * Extend or reset the current run of pages.
2566 if (run_len >= npages)
2572 * vm_page_reclaim_run:
2574 * Try to relocate each of the allocated virtual pages within the
2575 * specified run of physical pages to a new physical address. Free the
2576 * physical pages underlying the relocated virtual pages. A virtual page
2577 * is relocatable if and only if it could be laundered or reclaimed by
2578 * the page daemon. Whenever possible, a virtual page is relocated to a
2579 * physical address above "high".
2581 * Returns 0 if every physical page within the run was already free or
2582 * just freed by a successful relocation. Otherwise, returns a non-zero
2583 * value indicating why the last attempt to relocate a virtual page was
2586 * "req_class" must be an allocation class.
2589 vm_page_reclaim_run(int req_class, int domain, u_long npages, vm_page_t m_run,
2592 struct vm_domain *vmd;
2594 struct spglist free;
2597 vm_page_t m, m_end, m_new;
2598 int error, order, req;
2600 KASSERT((req_class & VM_ALLOC_CLASS_MASK) == req_class,
2601 ("req_class is not an allocation class"));
2605 m_end = m_run + npages;
2607 for (; error == 0 && m < m_end; m++) {
2608 KASSERT((m->flags & (PG_FICTITIOUS | PG_MARKER)) == 0,
2609 ("page %p is PG_FICTITIOUS or PG_MARKER", m));
2612 * Avoid releasing and reacquiring the same page lock.
2614 vm_page_change_lock(m, &m_mtx);
2617 * Racily check for wirings. Races are handled below.
2619 if (vm_page_wired(m))
2621 else if ((object = m->object) != NULL) {
2623 * The page is relocated if and only if it could be
2624 * laundered or reclaimed by the page daemon.
2626 if (!VM_OBJECT_TRYWLOCK(object)) {
2628 VM_OBJECT_WLOCK(object);
2630 if (m->object != object) {
2632 * The page may have been freed.
2634 VM_OBJECT_WUNLOCK(object);
2638 /* Don't care: PG_NODUMP, PG_ZERO. */
2639 if (object->type != OBJT_DEFAULT &&
2640 object->type != OBJT_SWAP &&
2641 object->type != OBJT_VNODE)
2643 else if (object->memattr != VM_MEMATTR_DEFAULT)
2645 else if (vm_page_queue(m) != PQ_NONE &&
2646 vm_page_tryxbusy(m) != 0) {
2647 if (vm_page_wired(m)) {
2652 KASSERT(pmap_page_get_memattr(m) ==
2654 ("page %p has an unexpected memattr", m));
2655 KASSERT((m->oflags & (VPO_SWAPINPROG |
2656 VPO_SWAPSLEEP | VPO_UNMANAGED)) == 0,
2657 ("page %p has unexpected oflags", m));
2658 /* Don't care: PGA_NOSYNC. */
2659 if (!vm_page_none_valid(m)) {
2661 * First, try to allocate a new page
2662 * that is above "high". Failing
2663 * that, try to allocate a new page
2664 * that is below "m_run". Allocate
2665 * the new page between the end of
2666 * "m_run" and "high" only as a last
2669 req = req_class | VM_ALLOC_NOOBJ;
2670 if ((m->flags & PG_NODUMP) != 0)
2671 req |= VM_ALLOC_NODUMP;
2672 if (trunc_page(high) !=
2673 ~(vm_paddr_t)PAGE_MASK) {
2674 m_new = vm_page_alloc_contig(
2679 VM_MEMATTR_DEFAULT);
2682 if (m_new == NULL) {
2683 pa = VM_PAGE_TO_PHYS(m_run);
2684 m_new = vm_page_alloc_contig(
2686 0, pa - 1, PAGE_SIZE, 0,
2687 VM_MEMATTR_DEFAULT);
2689 if (m_new == NULL) {
2691 m_new = vm_page_alloc_contig(
2693 pa, high, PAGE_SIZE, 0,
2694 VM_MEMATTR_DEFAULT);
2696 if (m_new == NULL) {
2703 * Unmap the page and check for new
2704 * wirings that may have been acquired
2705 * through a pmap lookup.
2707 if (object->ref_count != 0 &&
2708 !vm_page_try_remove_all(m)) {
2709 vm_page_free(m_new);
2715 * Replace "m" with the new page. For
2716 * vm_page_replace(), "m" must be busy
2717 * and dequeued. Finally, change "m"
2718 * as if vm_page_free() was called.
2720 m_new->aflags = m->aflags &
2721 ~PGA_QUEUE_STATE_MASK;
2722 KASSERT(m_new->oflags == VPO_UNMANAGED,
2723 ("page %p is managed", m_new));
2724 pmap_copy_page(m, m_new);
2725 m_new->valid = m->valid;
2726 m_new->dirty = m->dirty;
2727 m->flags &= ~PG_ZERO;
2729 vm_page_replace_checked(m_new, object,
2731 if (vm_page_free_prep(m))
2732 SLIST_INSERT_HEAD(&free, m,
2736 * The new page must be deactivated
2737 * before the object is unlocked.
2739 vm_page_change_lock(m_new, &m_mtx);
2740 vm_page_deactivate(m_new);
2742 m->flags &= ~PG_ZERO;
2744 if (vm_page_free_prep(m))
2745 SLIST_INSERT_HEAD(&free, m,
2747 KASSERT(m->dirty == 0,
2748 ("page %p is dirty", m));
2753 VM_OBJECT_WUNLOCK(object);
2755 MPASS(vm_phys_domain(m) == domain);
2756 vmd = VM_DOMAIN(domain);
2757 vm_domain_free_lock(vmd);
2759 if (order < VM_NFREEORDER) {
2761 * The page is enqueued in the physical memory
2762 * allocator's free page queues. Moreover, it
2763 * is the first page in a power-of-two-sized
2764 * run of contiguous free pages. Jump ahead
2765 * to the last page within that run, and
2766 * continue from there.
2768 m += (1 << order) - 1;
2770 #if VM_NRESERVLEVEL > 0
2771 else if (vm_reserv_is_page_free(m))
2774 vm_domain_free_unlock(vmd);
2775 if (order == VM_NFREEORDER)
2781 if ((m = SLIST_FIRST(&free)) != NULL) {
2784 vmd = VM_DOMAIN(domain);
2786 vm_domain_free_lock(vmd);
2788 MPASS(vm_phys_domain(m) == domain);
2789 SLIST_REMOVE_HEAD(&free, plinks.s.ss);
2790 vm_phys_free_pages(m, 0);
2792 } while ((m = SLIST_FIRST(&free)) != NULL);
2793 vm_domain_free_unlock(vmd);
2794 vm_domain_freecnt_inc(vmd, cnt);
2801 CTASSERT(powerof2(NRUNS));
2803 #define RUN_INDEX(count) ((count) & (NRUNS - 1))
2805 #define MIN_RECLAIM 8
2808 * vm_page_reclaim_contig:
2810 * Reclaim allocated, contiguous physical memory satisfying the specified
2811 * conditions by relocating the virtual pages using that physical memory.
2812 * Returns true if reclamation is successful and false otherwise. Since
2813 * relocation requires the allocation of physical pages, reclamation may
2814 * fail due to a shortage of free pages. When reclamation fails, callers
2815 * are expected to perform vm_wait() before retrying a failed allocation
2816 * operation, e.g., vm_page_alloc_contig().
2818 * The caller must always specify an allocation class through "req".
2820 * allocation classes:
2821 * VM_ALLOC_NORMAL normal process request
2822 * VM_ALLOC_SYSTEM system *really* needs a page
2823 * VM_ALLOC_INTERRUPT interrupt time request
2825 * The optional allocation flags are ignored.
2827 * "npages" must be greater than zero. Both "alignment" and "boundary"
2828 * must be a power of two.
2831 vm_page_reclaim_contig_domain(int domain, int req, u_long npages,
2832 vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
2834 struct vm_domain *vmd;
2835 vm_paddr_t curr_low;
2836 vm_page_t m_run, m_runs[NRUNS];
2837 u_long count, reclaimed;
2838 int error, i, options, req_class;
2840 KASSERT(npages > 0, ("npages is 0"));
2841 KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
2842 KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
2843 req_class = req & VM_ALLOC_CLASS_MASK;
2846 * The page daemon is allowed to dig deeper into the free page list.
2848 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
2849 req_class = VM_ALLOC_SYSTEM;
2852 * Return if the number of free pages cannot satisfy the requested
2855 vmd = VM_DOMAIN(domain);
2856 count = vmd->vmd_free_count;
2857 if (count < npages + vmd->vmd_free_reserved || (count < npages +
2858 vmd->vmd_interrupt_free_min && req_class == VM_ALLOC_SYSTEM) ||
2859 (count < npages && req_class == VM_ALLOC_INTERRUPT))
2863 * Scan up to three times, relaxing the restrictions ("options") on
2864 * the reclamation of reservations and superpages each time.
2866 for (options = VPSC_NORESERV;;) {
2868 * Find the highest runs that satisfy the given constraints
2869 * and restrictions, and record them in "m_runs".
2874 m_run = vm_phys_scan_contig(domain, npages, curr_low,
2875 high, alignment, boundary, options);
2878 curr_low = VM_PAGE_TO_PHYS(m_run) + ptoa(npages);
2879 m_runs[RUN_INDEX(count)] = m_run;
2884 * Reclaim the highest runs in LIFO (descending) order until
2885 * the number of reclaimed pages, "reclaimed", is at least
2886 * MIN_RECLAIM. Reset "reclaimed" each time because each
2887 * reclamation is idempotent, and runs will (likely) recur
2888 * from one scan to the next as restrictions are relaxed.
2891 for (i = 0; count > 0 && i < NRUNS; i++) {
2893 m_run = m_runs[RUN_INDEX(count)];
2894 error = vm_page_reclaim_run(req_class, domain, npages,
2897 reclaimed += npages;
2898 if (reclaimed >= MIN_RECLAIM)
2904 * Either relax the restrictions on the next scan or return if
2905 * the last scan had no restrictions.
2907 if (options == VPSC_NORESERV)
2908 options = VPSC_NOSUPER;
2909 else if (options == VPSC_NOSUPER)
2911 else if (options == VPSC_ANY)
2912 return (reclaimed != 0);
2917 vm_page_reclaim_contig(int req, u_long npages, vm_paddr_t low, vm_paddr_t high,
2918 u_long alignment, vm_paddr_t boundary)
2920 struct vm_domainset_iter di;
2924 vm_domainset_iter_page_init(&di, NULL, 0, &domain, &req);
2926 ret = vm_page_reclaim_contig_domain(domain, req, npages, low,
2927 high, alignment, boundary);
2930 } while (vm_domainset_iter_page(&di, NULL, &domain) == 0);
2936 * Set the domain in the appropriate page level domainset.
2939 vm_domain_set(struct vm_domain *vmd)
2942 mtx_lock(&vm_domainset_lock);
2943 if (!vmd->vmd_minset && vm_paging_min(vmd)) {
2944 vmd->vmd_minset = 1;
2945 DOMAINSET_SET(vmd->vmd_domain, &vm_min_domains);
2947 if (!vmd->vmd_severeset && vm_paging_severe(vmd)) {
2948 vmd->vmd_severeset = 1;
2949 DOMAINSET_SET(vmd->vmd_domain, &vm_severe_domains);
2951 mtx_unlock(&vm_domainset_lock);
2955 * Clear the domain from the appropriate page level domainset.
2958 vm_domain_clear(struct vm_domain *vmd)
2961 mtx_lock(&vm_domainset_lock);
2962 if (vmd->vmd_minset && !vm_paging_min(vmd)) {
2963 vmd->vmd_minset = 0;
2964 DOMAINSET_CLR(vmd->vmd_domain, &vm_min_domains);
2965 if (vm_min_waiters != 0) {
2967 wakeup(&vm_min_domains);
2970 if (vmd->vmd_severeset && !vm_paging_severe(vmd)) {
2971 vmd->vmd_severeset = 0;
2972 DOMAINSET_CLR(vmd->vmd_domain, &vm_severe_domains);
2973 if (vm_severe_waiters != 0) {
2974 vm_severe_waiters = 0;
2975 wakeup(&vm_severe_domains);
2980 * If pageout daemon needs pages, then tell it that there are
2983 if (vmd->vmd_pageout_pages_needed &&
2984 vmd->vmd_free_count >= vmd->vmd_pageout_free_min) {
2985 wakeup(&vmd->vmd_pageout_pages_needed);
2986 vmd->vmd_pageout_pages_needed = 0;
2989 /* See comments in vm_wait_doms(). */
2990 if (vm_pageproc_waiters) {
2991 vm_pageproc_waiters = 0;
2992 wakeup(&vm_pageproc_waiters);
2994 mtx_unlock(&vm_domainset_lock);
2998 * Wait for free pages to exceed the min threshold globally.
3004 mtx_lock(&vm_domainset_lock);
3005 while (vm_page_count_min()) {
3007 msleep(&vm_min_domains, &vm_domainset_lock, PVM, "vmwait", 0);
3009 mtx_unlock(&vm_domainset_lock);
3013 * Wait for free pages to exceed the severe threshold globally.
3016 vm_wait_severe(void)
3019 mtx_lock(&vm_domainset_lock);
3020 while (vm_page_count_severe()) {
3021 vm_severe_waiters++;
3022 msleep(&vm_severe_domains, &vm_domainset_lock, PVM,
3025 mtx_unlock(&vm_domainset_lock);
3032 return (vm_severe_waiters + vm_min_waiters + vm_pageproc_waiters);
3036 vm_wait_doms(const domainset_t *wdoms)
3040 * We use racey wakeup synchronization to avoid expensive global
3041 * locking for the pageproc when sleeping with a non-specific vm_wait.
3042 * To handle this, we only sleep for one tick in this instance. It
3043 * is expected that most allocations for the pageproc will come from
3044 * kmem or vm_page_grab* which will use the more specific and
3045 * race-free vm_wait_domain().
3047 if (curproc == pageproc) {
3048 mtx_lock(&vm_domainset_lock);
3049 vm_pageproc_waiters++;
3050 msleep(&vm_pageproc_waiters, &vm_domainset_lock, PVM | PDROP,
3054 * XXX Ideally we would wait only until the allocation could
3055 * be satisfied. This condition can cause new allocators to
3056 * consume all freed pages while old allocators wait.
3058 mtx_lock(&vm_domainset_lock);
3059 if (vm_page_count_min_set(wdoms)) {
3061 msleep(&vm_min_domains, &vm_domainset_lock,
3062 PVM | PDROP, "vmwait", 0);
3064 mtx_unlock(&vm_domainset_lock);
3071 * Sleep until free pages are available for allocation.
3072 * - Called in various places after failed memory allocations.
3075 vm_wait_domain(int domain)
3077 struct vm_domain *vmd;
3080 vmd = VM_DOMAIN(domain);
3081 vm_domain_free_assert_unlocked(vmd);
3083 if (curproc == pageproc) {
3084 mtx_lock(&vm_domainset_lock);
3085 if (vmd->vmd_free_count < vmd->vmd_pageout_free_min) {
3086 vmd->vmd_pageout_pages_needed = 1;
3087 msleep(&vmd->vmd_pageout_pages_needed,
3088 &vm_domainset_lock, PDROP | PSWP, "VMWait", 0);
3090 mtx_unlock(&vm_domainset_lock);
3092 if (pageproc == NULL)
3093 panic("vm_wait in early boot");
3094 DOMAINSET_ZERO(&wdom);
3095 DOMAINSET_SET(vmd->vmd_domain, &wdom);
3096 vm_wait_doms(&wdom);
3103 * Sleep until free pages are available for allocation in the
3104 * affinity domains of the obj. If obj is NULL, the domain set
3105 * for the calling thread is used.
3106 * Called in various places after failed memory allocations.
3109 vm_wait(vm_object_t obj)
3111 struct domainset *d;
3116 * Carefully fetch pointers only once: the struct domainset
3117 * itself is ummutable but the pointer might change.
3120 d = obj->domain.dr_policy;
3122 d = curthread->td_domain.dr_policy;
3124 vm_wait_doms(&d->ds_mask);
3128 * vm_domain_alloc_fail:
3130 * Called when a page allocation function fails. Informs the
3131 * pagedaemon and performs the requested wait. Requires the
3132 * domain_free and object lock on entry. Returns with the
3133 * object lock held and free lock released. Returns an error when
3134 * retry is necessary.
3138 vm_domain_alloc_fail(struct vm_domain *vmd, vm_object_t object, int req)
3141 vm_domain_free_assert_unlocked(vmd);
3143 atomic_add_int(&vmd->vmd_pageout_deficit,
3144 max((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1));
3145 if (req & (VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL)) {
3147 VM_OBJECT_WUNLOCK(object);
3148 vm_wait_domain(vmd->vmd_domain);
3150 VM_OBJECT_WLOCK(object);
3151 if (req & VM_ALLOC_WAITOK)
3161 * Sleep until free pages are available for allocation.
3162 * - Called only in vm_fault so that processes page faulting
3163 * can be easily tracked.
3164 * - Sleeps at a lower priority than vm_wait() so that vm_wait()ing
3165 * processes will be able to grab memory first. Do not change
3166 * this balance without careful testing first.
3169 vm_waitpfault(struct domainset *dset, int timo)
3173 * XXX Ideally we would wait only until the allocation could
3174 * be satisfied. This condition can cause new allocators to
3175 * consume all freed pages while old allocators wait.
3177 mtx_lock(&vm_domainset_lock);
3178 if (vm_page_count_min_set(&dset->ds_mask)) {
3180 msleep(&vm_min_domains, &vm_domainset_lock, PUSER | PDROP,
3183 mtx_unlock(&vm_domainset_lock);
3186 static struct vm_pagequeue *
3187 vm_page_pagequeue(vm_page_t m)
3192 if ((queue = atomic_load_8(&m->queue)) == PQ_NONE)
3194 return (&vm_pagequeue_domain(m)->vmd_pagequeues[queue]);
3198 vm_pqbatch_process_page(struct vm_pagequeue *pq, vm_page_t m)
3200 struct vm_domain *vmd;
3203 CRITICAL_ASSERT(curthread);
3204 vm_pagequeue_assert_locked(pq);
3207 * The page daemon is allowed to set m->queue = PQ_NONE without
3208 * the page queue lock held. In this case it is about to free the page,
3209 * which must not have any queue state.
3211 qflags = atomic_load_8(&m->aflags);
3212 KASSERT(pq == vm_page_pagequeue(m) ||
3213 (qflags & PGA_QUEUE_STATE_MASK) == 0,
3214 ("page %p doesn't belong to queue %p but has aflags %#x",
3217 if ((qflags & PGA_DEQUEUE) != 0) {
3218 if (__predict_true((qflags & PGA_ENQUEUED) != 0))
3219 vm_pagequeue_remove(pq, m);
3220 vm_page_dequeue_complete(m);
3221 counter_u64_add(queue_ops, 1);
3222 } else if ((qflags & (PGA_REQUEUE | PGA_REQUEUE_HEAD)) != 0) {
3223 if ((qflags & PGA_ENQUEUED) != 0)
3224 TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
3226 vm_pagequeue_cnt_inc(pq);
3227 vm_page_aflag_set(m, PGA_ENQUEUED);
3231 * Give PGA_REQUEUE_HEAD precedence over PGA_REQUEUE.
3232 * In particular, if both flags are set in close succession,
3233 * only PGA_REQUEUE_HEAD will be applied, even if it was set
3236 if ((qflags & PGA_REQUEUE_HEAD) != 0) {
3237 KASSERT(m->queue == PQ_INACTIVE,
3238 ("head enqueue not supported for page %p", m));
3239 vmd = vm_pagequeue_domain(m);
3240 TAILQ_INSERT_BEFORE(&vmd->vmd_inacthead, m, plinks.q);
3242 TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
3244 vm_page_aflag_clear(m, qflags & (PGA_REQUEUE |
3246 counter_u64_add(queue_ops, 1);
3248 counter_u64_add(queue_nops, 1);
3253 vm_pqbatch_process(struct vm_pagequeue *pq, struct vm_batchqueue *bq,
3259 for (i = 0; i < bq->bq_cnt; i++) {
3261 if (__predict_false(m->queue != queue))
3263 vm_pqbatch_process_page(pq, m);
3265 vm_batchqueue_init(bq);
3269 * vm_page_pqbatch_submit: [ internal use only ]
3271 * Enqueue a page in the specified page queue's batched work queue.
3272 * The caller must have encoded the requested operation in the page
3273 * structure's aflags field.
3276 vm_page_pqbatch_submit(vm_page_t m, uint8_t queue)
3278 struct vm_batchqueue *bq;
3279 struct vm_pagequeue *pq;
3282 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3283 ("page %p is unmanaged", m));
3284 KASSERT(mtx_owned(vm_page_lockptr(m)) || m->object == NULL,
3285 ("missing synchronization for page %p", m));
3286 KASSERT(queue < PQ_COUNT, ("invalid queue %d", queue));
3288 domain = vm_phys_domain(m);
3289 pq = &vm_pagequeue_domain(m)->vmd_pagequeues[queue];
3292 bq = DPCPU_PTR(pqbatch[domain][queue]);
3293 if (vm_batchqueue_insert(bq, m)) {
3298 vm_pagequeue_lock(pq);
3300 bq = DPCPU_PTR(pqbatch[domain][queue]);
3301 vm_pqbatch_process(pq, bq, queue);
3304 * The page may have been logically dequeued before we acquired the
3305 * page queue lock. In this case, since we either hold the page lock
3306 * or the page is being freed, a different thread cannot be concurrently
3307 * enqueuing the page.
3309 if (__predict_true(m->queue == queue))
3310 vm_pqbatch_process_page(pq, m);
3312 KASSERT(m->queue == PQ_NONE,
3313 ("invalid queue transition for page %p", m));
3314 KASSERT((m->aflags & PGA_ENQUEUED) == 0,
3315 ("page %p is enqueued with invalid queue index", m));
3317 vm_pagequeue_unlock(pq);
3322 * vm_page_pqbatch_drain: [ internal use only ]
3324 * Force all per-CPU page queue batch queues to be drained. This is
3325 * intended for use in severe memory shortages, to ensure that pages
3326 * do not remain stuck in the batch queues.
3329 vm_page_pqbatch_drain(void)
3332 struct vm_domain *vmd;
3333 struct vm_pagequeue *pq;
3334 int cpu, domain, queue;
3339 sched_bind(td, cpu);
3342 for (domain = 0; domain < vm_ndomains; domain++) {
3343 vmd = VM_DOMAIN(domain);
3344 for (queue = 0; queue < PQ_COUNT; queue++) {
3345 pq = &vmd->vmd_pagequeues[queue];
3346 vm_pagequeue_lock(pq);
3348 vm_pqbatch_process(pq,
3349 DPCPU_PTR(pqbatch[domain][queue]), queue);
3351 vm_pagequeue_unlock(pq);
3361 * Complete the logical removal of a page from a page queue. We must be
3362 * careful to synchronize with the page daemon, which may be concurrently
3363 * examining the page with only the page lock held. The page must not be
3364 * in a state where it appears to be logically enqueued.
3367 vm_page_dequeue_complete(vm_page_t m)
3371 atomic_thread_fence_rel();
3372 vm_page_aflag_clear(m, PGA_QUEUE_STATE_MASK);
3376 * vm_page_dequeue_deferred: [ internal use only ]
3378 * Request removal of the given page from its current page
3379 * queue. Physical removal from the queue may be deferred
3382 * The page must be locked.
3385 vm_page_dequeue_deferred(vm_page_t m)
3389 vm_page_assert_locked(m);
3391 if ((queue = vm_page_queue(m)) == PQ_NONE)
3395 * Set PGA_DEQUEUE if it is not already set to handle a concurrent call
3396 * to vm_page_dequeue_deferred_free(). In particular, avoid modifying
3397 * the page's queue state once vm_page_dequeue_deferred_free() has been
3398 * called. In the event of a race, two batch queue entries for the page
3399 * will be created, but the second will have no effect.
3401 if (vm_page_pqstate_cmpset(m, queue, queue, PGA_DEQUEUE, PGA_DEQUEUE))
3402 vm_page_pqbatch_submit(m, queue);
3406 * A variant of vm_page_dequeue_deferred() that does not assert the page
3407 * lock and is only to be called from vm_page_free_prep(). Because the
3408 * page is being freed, we can assume that nothing other than the page
3409 * daemon is scheduling queue operations on this page, so we get for
3410 * free the mutual exclusion that is otherwise provided by the page lock.
3411 * To handle races, the page daemon must take care to atomically check
3412 * for PGA_DEQUEUE when updating queue state.
3415 vm_page_dequeue_deferred_free(vm_page_t m)
3419 KASSERT(m->ref_count == 0, ("page %p has references", m));
3422 if ((m->aflags & PGA_DEQUEUE) != 0)
3424 atomic_thread_fence_acq();
3425 if ((queue = atomic_load_8(&m->queue)) == PQ_NONE)
3427 if (vm_page_pqstate_cmpset(m, queue, queue, PGA_DEQUEUE,
3429 vm_page_pqbatch_submit(m, queue);
3438 * Remove the page from whichever page queue it's in, if any.
3439 * The page must either be locked or unallocated. This constraint
3440 * ensures that the queue state of the page will remain consistent
3441 * after this function returns.
3444 vm_page_dequeue(vm_page_t m)
3446 struct vm_pagequeue *pq, *pq1;
3449 KASSERT(mtx_owned(vm_page_lockptr(m)) || m->object == NULL,
3450 ("page %p is allocated and unlocked", m));
3452 for (pq = vm_page_pagequeue(m);; pq = pq1) {
3455 * A thread may be concurrently executing
3456 * vm_page_dequeue_complete(). Ensure that all queue
3457 * state is cleared before we return.
3459 aflags = atomic_load_8(&m->aflags);
3460 if ((aflags & PGA_QUEUE_STATE_MASK) == 0)
3462 KASSERT((aflags & PGA_DEQUEUE) != 0,
3463 ("page %p has unexpected queue state flags %#x",
3467 * Busy wait until the thread updating queue state is
3468 * finished. Such a thread must be executing in a
3472 pq1 = vm_page_pagequeue(m);
3475 vm_pagequeue_lock(pq);
3476 if ((pq1 = vm_page_pagequeue(m)) == pq)
3478 vm_pagequeue_unlock(pq);
3480 KASSERT(pq == vm_page_pagequeue(m),
3481 ("%s: page %p migrated directly between queues", __func__, m));
3482 KASSERT((m->aflags & PGA_DEQUEUE) != 0 ||
3483 mtx_owned(vm_page_lockptr(m)),
3484 ("%s: queued unlocked page %p", __func__, m));
3486 if ((m->aflags & PGA_ENQUEUED) != 0)
3487 vm_pagequeue_remove(pq, m);
3488 vm_page_dequeue_complete(m);
3489 vm_pagequeue_unlock(pq);
3493 * Schedule the given page for insertion into the specified page queue.
3494 * Physical insertion of the page may be deferred indefinitely.
3497 vm_page_enqueue(vm_page_t m, uint8_t queue)
3500 vm_page_assert_locked(m);
3501 KASSERT(m->queue == PQ_NONE && (m->aflags & PGA_QUEUE_STATE_MASK) == 0,
3502 ("%s: page %p is already enqueued", __func__, m));
3505 if ((m->aflags & PGA_REQUEUE) == 0)
3506 vm_page_aflag_set(m, PGA_REQUEUE);
3507 vm_page_pqbatch_submit(m, queue);
3511 * vm_page_requeue: [ internal use only ]
3513 * Schedule a requeue of the given page.
3515 * The page must be locked.
3518 vm_page_requeue(vm_page_t m)
3521 vm_page_assert_locked(m);
3522 KASSERT(vm_page_queue(m) != PQ_NONE,
3523 ("%s: page %p is not logically enqueued", __func__, m));
3525 if ((m->aflags & PGA_REQUEUE) == 0)
3526 vm_page_aflag_set(m, PGA_REQUEUE);
3527 vm_page_pqbatch_submit(m, atomic_load_8(&m->queue));
3531 * vm_page_swapqueue: [ internal use only ]
3533 * Move the page from one queue to another, or to the tail of its
3534 * current queue, in the face of a possible concurrent call to
3535 * vm_page_dequeue_deferred_free().
3538 vm_page_swapqueue(vm_page_t m, uint8_t oldq, uint8_t newq)
3540 struct vm_pagequeue *pq;
3544 KASSERT(oldq < PQ_COUNT && newq < PQ_COUNT && oldq != newq,
3545 ("vm_page_swapqueue: invalid queues (%d, %d)", oldq, newq));
3546 vm_page_assert_locked(m);
3548 pq = &vm_pagequeue_domain(m)->vmd_pagequeues[oldq];
3549 vm_pagequeue_lock(pq);
3552 * The physical queue state might change at any point before the page
3553 * queue lock is acquired, so we must verify that we hold the correct
3554 * lock before proceeding.
3556 if (__predict_false(m->queue != oldq)) {
3557 vm_pagequeue_unlock(pq);
3562 * Once the queue index of the page changes, there is nothing
3563 * synchronizing with further updates to the physical queue state.
3564 * Therefore we must remove the page from the queue now in anticipation
3565 * of a successful commit, and be prepared to roll back.
3567 if (__predict_true((m->aflags & PGA_ENQUEUED) != 0)) {
3568 next = TAILQ_NEXT(m, plinks.q);
3569 TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
3570 vm_page_aflag_clear(m, PGA_ENQUEUED);
3577 * Atomically update the queue field and set PGA_REQUEUE while
3578 * ensuring that PGA_DEQUEUE has not been set.
3580 if (__predict_false(!vm_page_pqstate_cmpset(m, oldq, newq, PGA_DEQUEUE,
3583 vm_page_aflag_set(m, PGA_ENQUEUED);
3585 TAILQ_INSERT_BEFORE(next, m, plinks.q);
3587 TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
3589 vm_pagequeue_unlock(pq);
3592 vm_pagequeue_cnt_dec(pq);
3593 vm_pagequeue_unlock(pq);
3594 vm_page_pqbatch_submit(m, newq);
3598 * vm_page_free_prep:
3600 * Prepares the given page to be put on the free list,
3601 * disassociating it from any VM object. The caller may return
3602 * the page to the free list only if this function returns true.
3604 * The object must be locked. The page must be locked if it is
3608 vm_page_free_prep(vm_page_t m)
3612 * Synchronize with threads that have dropped a reference to this
3615 atomic_thread_fence_acq();
3617 #if defined(DIAGNOSTIC) && defined(PHYS_TO_DMAP)
3618 if (PMAP_HAS_DMAP && (m->flags & PG_ZERO) != 0) {
3621 p = (uint64_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m));
3622 for (i = 0; i < PAGE_SIZE / sizeof(uint64_t); i++, p++)
3623 KASSERT(*p == 0, ("vm_page_free_prep %p PG_ZERO %d %jx",
3624 m, i, (uintmax_t)*p));
3627 if ((m->oflags & VPO_UNMANAGED) == 0) {
3628 KASSERT(!pmap_page_is_mapped(m),
3629 ("vm_page_free_prep: freeing mapped page %p", m));
3630 KASSERT((m->aflags & (PGA_EXECUTABLE | PGA_WRITEABLE)) == 0,
3631 ("vm_page_free_prep: mapping flags set in page %p", m));
3633 KASSERT(m->queue == PQ_NONE,
3634 ("vm_page_free_prep: unmanaged page %p is queued", m));
3636 VM_CNT_INC(v_tfree);
3638 if (vm_page_sbusied(m))
3639 panic("vm_page_free_prep: freeing shared busy page %p", m);
3641 if (m->object != NULL) {
3642 vm_page_object_remove(m);
3645 * The object reference can be released without an atomic
3648 KASSERT((m->flags & PG_FICTITIOUS) != 0 ||
3649 m->ref_count == VPRC_OBJREF,
3650 ("vm_page_free_prep: page %p has unexpected ref_count %u",
3653 m->ref_count -= VPRC_OBJREF;
3656 if (vm_page_xbusied(m))
3660 * If fictitious remove object association and
3663 if ((m->flags & PG_FICTITIOUS) != 0) {
3664 KASSERT(m->ref_count == 1,
3665 ("fictitious page %p is referenced", m));
3666 KASSERT(m->queue == PQ_NONE,
3667 ("fictitious page %p is queued", m));
3672 * Pages need not be dequeued before they are returned to the physical
3673 * memory allocator, but they must at least be marked for a deferred
3676 if ((m->oflags & VPO_UNMANAGED) == 0)
3677 vm_page_dequeue_deferred_free(m);
3682 if (m->ref_count != 0)
3683 panic("vm_page_free_prep: page %p has references", m);
3686 * Restore the default memory attribute to the page.
3688 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT)
3689 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT);
3691 #if VM_NRESERVLEVEL > 0
3693 * Determine whether the page belongs to a reservation. If the page was
3694 * allocated from a per-CPU cache, it cannot belong to a reservation, so
3695 * as an optimization, we avoid the check in that case.
3697 if ((m->flags & PG_PCPU_CACHE) == 0 && vm_reserv_free_page(m))
3707 * Returns the given page to the free list, disassociating it
3708 * from any VM object.
3710 * The object must be locked. The page must be locked if it is
3714 vm_page_free_toq(vm_page_t m)
3716 struct vm_domain *vmd;
3719 if (!vm_page_free_prep(m))
3722 vmd = vm_pagequeue_domain(m);
3723 zone = vmd->vmd_pgcache[m->pool].zone;
3724 if ((m->flags & PG_PCPU_CACHE) != 0 && zone != NULL) {
3728 vm_domain_free_lock(vmd);
3729 vm_phys_free_pages(m, 0);
3730 vm_domain_free_unlock(vmd);
3731 vm_domain_freecnt_inc(vmd, 1);
3735 * vm_page_free_pages_toq:
3737 * Returns a list of pages to the free list, disassociating it
3738 * from any VM object. In other words, this is equivalent to
3739 * calling vm_page_free_toq() for each page of a list of VM objects.
3741 * The objects must be locked. The pages must be locked if it is
3745 vm_page_free_pages_toq(struct spglist *free, bool update_wire_count)
3750 if (SLIST_EMPTY(free))
3754 while ((m = SLIST_FIRST(free)) != NULL) {
3756 SLIST_REMOVE_HEAD(free, plinks.s.ss);
3757 vm_page_free_toq(m);
3760 if (update_wire_count)
3765 * Mark this page as wired down, preventing reclamation by the page daemon
3766 * or when the containing object is destroyed.
3769 vm_page_wire(vm_page_t m)
3773 KASSERT(m->object != NULL,
3774 ("vm_page_wire: page %p does not belong to an object", m));
3775 if (!vm_page_busied(m))
3776 VM_OBJECT_ASSERT_LOCKED(m->object);
3777 KASSERT((m->flags & PG_FICTITIOUS) == 0 ||
3778 VPRC_WIRE_COUNT(m->ref_count) >= 1,
3779 ("vm_page_wire: fictitious page %p has zero wirings", m));
3781 old = atomic_fetchadd_int(&m->ref_count, 1);
3782 KASSERT(VPRC_WIRE_COUNT(old) != VPRC_WIRE_COUNT_MAX,
3783 ("vm_page_wire: counter overflow for page %p", m));
3784 if (VPRC_WIRE_COUNT(old) == 0)
3789 * Attempt to wire a mapped page following a pmap lookup of that page.
3790 * This may fail if a thread is concurrently tearing down mappings of the page.
3793 vm_page_wire_mapped(vm_page_t m)
3800 ("vm_page_wire_mapped: wiring unreferenced page %p", m));
3801 if ((old & VPRC_BLOCKED) != 0)
3803 } while (!atomic_fcmpset_int(&m->ref_count, &old, old + 1));
3805 if (VPRC_WIRE_COUNT(old) == 0)
3811 * Release one wiring of the specified page, potentially allowing it to be
3814 * Only managed pages belonging to an object can be paged out. If the number
3815 * of wirings transitions to zero and the page is eligible for page out, then
3816 * the page is added to the specified paging queue. If the released wiring
3817 * represented the last reference to the page, the page is freed.
3819 * A managed page must be locked.
3822 vm_page_unwire(vm_page_t m, uint8_t queue)
3827 KASSERT(queue < PQ_COUNT,
3828 ("vm_page_unwire: invalid queue %u request for page %p", queue, m));
3830 if ((m->oflags & VPO_UNMANAGED) != 0) {
3831 if (vm_page_unwire_noq(m) && m->ref_count == 0)
3837 * Update LRU state before releasing the wiring reference.
3838 * We only need to do this once since we hold the page lock.
3839 * Use a release store when updating the reference count to
3840 * synchronize with vm_page_free_prep().
3845 KASSERT(VPRC_WIRE_COUNT(old) > 0,
3846 ("vm_page_unwire: wire count underflow for page %p", m));
3847 if (!locked && VPRC_WIRE_COUNT(old) == 1) {
3850 if (queue == PQ_ACTIVE && vm_page_queue(m) == PQ_ACTIVE)
3851 vm_page_reference(m);
3853 vm_page_mvqueue(m, queue);
3855 } while (!atomic_fcmpset_rel_int(&m->ref_count, &old, old - 1));
3858 * Release the lock only after the wiring is released, to ensure that
3859 * the page daemon does not encounter and dequeue the page while it is
3865 if (VPRC_WIRE_COUNT(old) == 1) {
3873 * Unwire a page without (re-)inserting it into a page queue. It is up
3874 * to the caller to enqueue, requeue, or free the page as appropriate.
3875 * In most cases involving managed pages, vm_page_unwire() should be used
3879 vm_page_unwire_noq(vm_page_t m)
3883 old = vm_page_drop(m, 1);
3884 KASSERT(VPRC_WIRE_COUNT(old) != 0,
3885 ("vm_page_unref: counter underflow for page %p", m));
3886 KASSERT((m->flags & PG_FICTITIOUS) == 0 || VPRC_WIRE_COUNT(old) > 1,
3887 ("vm_page_unref: missing ref on fictitious page %p", m));
3889 if (VPRC_WIRE_COUNT(old) > 1)
3896 * Ensure that the page is in the specified page queue. If the page is
3897 * active or being moved to the active queue, ensure that its act_count is
3898 * at least ACT_INIT but do not otherwise mess with it. Otherwise, ensure that
3899 * the page is at the tail of its page queue.
3901 * The page may be wired. The caller should release its wiring reference
3902 * before releasing the page lock, otherwise the page daemon may immediately
3905 * A managed page must be locked.
3907 static __always_inline void
3908 vm_page_mvqueue(vm_page_t m, const uint8_t nqueue)
3911 vm_page_assert_locked(m);
3912 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3913 ("vm_page_mvqueue: page %p is unmanaged", m));
3915 if (vm_page_queue(m) != nqueue) {
3917 vm_page_enqueue(m, nqueue);
3918 } else if (nqueue != PQ_ACTIVE) {
3922 if (nqueue == PQ_ACTIVE && m->act_count < ACT_INIT)
3923 m->act_count = ACT_INIT;
3927 * Put the specified page on the active list (if appropriate).
3930 vm_page_activate(vm_page_t m)
3933 if ((m->oflags & VPO_UNMANAGED) != 0 || vm_page_wired(m))
3935 vm_page_mvqueue(m, PQ_ACTIVE);
3939 * Move the specified page to the tail of the inactive queue, or requeue
3940 * the page if it is already in the inactive queue.
3943 vm_page_deactivate(vm_page_t m)
3946 if ((m->oflags & VPO_UNMANAGED) != 0 || vm_page_wired(m))
3948 vm_page_mvqueue(m, PQ_INACTIVE);
3952 * Move the specified page close to the head of the inactive queue,
3953 * bypassing LRU. A marker page is used to maintain FIFO ordering.
3954 * As with regular enqueues, we use a per-CPU batch queue to reduce
3955 * contention on the page queue lock.
3958 _vm_page_deactivate_noreuse(vm_page_t m)
3961 vm_page_assert_locked(m);
3963 if (!vm_page_inactive(m)) {
3965 m->queue = PQ_INACTIVE;
3967 if ((m->aflags & PGA_REQUEUE_HEAD) == 0)
3968 vm_page_aflag_set(m, PGA_REQUEUE_HEAD);
3969 vm_page_pqbatch_submit(m, PQ_INACTIVE);
3973 vm_page_deactivate_noreuse(vm_page_t m)
3976 KASSERT(m->object != NULL,
3977 ("vm_page_deactivate_noreuse: page %p has no object", m));
3979 if ((m->oflags & VPO_UNMANAGED) == 0 && !vm_page_wired(m))
3980 _vm_page_deactivate_noreuse(m);
3984 * Put a page in the laundry, or requeue it if it is already there.
3987 vm_page_launder(vm_page_t m)
3990 if ((m->oflags & VPO_UNMANAGED) != 0 || vm_page_wired(m))
3992 vm_page_mvqueue(m, PQ_LAUNDRY);
3996 * Put a page in the PQ_UNSWAPPABLE holding queue.
3999 vm_page_unswappable(vm_page_t m)
4002 vm_page_assert_locked(m);
4003 KASSERT(!vm_page_wired(m) && (m->oflags & VPO_UNMANAGED) == 0,
4004 ("page %p already unswappable", m));
4007 vm_page_enqueue(m, PQ_UNSWAPPABLE);
4011 vm_page_release_toq(vm_page_t m, int flags)
4014 vm_page_assert_locked(m);
4017 * Use a check of the valid bits to determine whether we should
4018 * accelerate reclamation of the page. The object lock might not be
4019 * held here, in which case the check is racy. At worst we will either
4020 * accelerate reclamation of a valid page and violate LRU, or
4021 * unnecessarily defer reclamation of an invalid page.
4023 * If we were asked to not cache the page, place it near the head of the
4024 * inactive queue so that is reclaimed sooner.
4026 if ((flags & (VPR_TRYFREE | VPR_NOREUSE)) != 0 || m->valid == 0)
4027 _vm_page_deactivate_noreuse(m);
4028 else if (vm_page_active(m))
4029 vm_page_reference(m);
4031 vm_page_mvqueue(m, PQ_INACTIVE);
4035 * Unwire a page and either attempt to free it or re-add it to the page queues.
4038 vm_page_release(vm_page_t m, int flags)
4044 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4045 ("vm_page_release: page %p is unmanaged", m));
4047 if ((flags & VPR_TRYFREE) != 0) {
4049 object = (vm_object_t)atomic_load_ptr(&m->object);
4052 /* Depends on type-stability. */
4053 if (vm_page_busied(m) || !VM_OBJECT_TRYWLOCK(object)) {
4057 if (object == m->object)
4059 VM_OBJECT_WUNLOCK(object);
4061 if (__predict_true(object != NULL)) {
4062 vm_page_release_locked(m, flags);
4063 VM_OBJECT_WUNLOCK(object);
4069 * Update LRU state before releasing the wiring reference.
4070 * Use a release store when updating the reference count to
4071 * synchronize with vm_page_free_prep().
4076 KASSERT(VPRC_WIRE_COUNT(old) > 0,
4077 ("vm_page_unwire: wire count underflow for page %p", m));
4078 if (!locked && VPRC_WIRE_COUNT(old) == 1) {
4081 vm_page_release_toq(m, flags);
4083 } while (!atomic_fcmpset_rel_int(&m->ref_count, &old, old - 1));
4086 * Release the lock only after the wiring is released, to ensure that
4087 * the page daemon does not encounter and dequeue the page while it is
4093 if (VPRC_WIRE_COUNT(old) == 1) {
4100 /* See vm_page_release(). */
4102 vm_page_release_locked(vm_page_t m, int flags)
4105 VM_OBJECT_ASSERT_WLOCKED(m->object);
4106 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4107 ("vm_page_release_locked: page %p is unmanaged", m));
4109 if (vm_page_unwire_noq(m)) {
4110 if ((flags & VPR_TRYFREE) != 0 &&
4111 (m->object->ref_count == 0 || !pmap_page_is_mapped(m)) &&
4112 m->dirty == 0 && !vm_page_busied(m)) {
4116 vm_page_release_toq(m, flags);
4123 vm_page_try_blocked_op(vm_page_t m, void (*op)(vm_page_t))
4127 KASSERT(m->object != NULL && (m->oflags & VPO_UNMANAGED) == 0,
4128 ("vm_page_try_blocked_op: page %p has no object", m));
4129 KASSERT(vm_page_busied(m),
4130 ("vm_page_try_blocked_op: page %p is not busy", m));
4131 VM_OBJECT_ASSERT_LOCKED(m->object);
4136 ("vm_page_try_blocked_op: page %p has no references", m));
4137 if (VPRC_WIRE_COUNT(old) != 0)
4139 } while (!atomic_fcmpset_int(&m->ref_count, &old, old | VPRC_BLOCKED));
4144 * If the object is read-locked, new wirings may be created via an
4147 old = vm_page_drop(m, VPRC_BLOCKED);
4148 KASSERT(!VM_OBJECT_WOWNED(m->object) ||
4149 old == (VPRC_BLOCKED | VPRC_OBJREF),
4150 ("vm_page_try_blocked_op: unexpected refcount value %u for %p",
4156 * Atomically check for wirings and remove all mappings of the page.
4159 vm_page_try_remove_all(vm_page_t m)
4162 return (vm_page_try_blocked_op(m, pmap_remove_all));
4166 * Atomically check for wirings and remove all writeable mappings of the page.
4169 vm_page_try_remove_write(vm_page_t m)
4172 return (vm_page_try_blocked_op(m, pmap_remove_write));
4178 * Apply the specified advice to the given page.
4180 * The object and page must be locked.
4183 vm_page_advise(vm_page_t m, int advice)
4186 vm_page_assert_locked(m);
4187 VM_OBJECT_ASSERT_WLOCKED(m->object);
4188 if (advice == MADV_FREE)
4190 * Mark the page clean. This will allow the page to be freed
4191 * without first paging it out. MADV_FREE pages are often
4192 * quickly reused by malloc(3), so we do not do anything that
4193 * would result in a page fault on a later access.
4196 else if (advice != MADV_DONTNEED) {
4197 if (advice == MADV_WILLNEED)
4198 vm_page_activate(m);
4203 * Clear any references to the page. Otherwise, the page daemon will
4204 * immediately reactivate the page.
4206 vm_page_aflag_clear(m, PGA_REFERENCED);
4208 if (advice != MADV_FREE && m->dirty == 0 && pmap_is_modified(m))
4212 * Place clean pages near the head of the inactive queue rather than
4213 * the tail, thus defeating the queue's LRU operation and ensuring that
4214 * the page will be reused quickly. Dirty pages not already in the
4215 * laundry are moved there.
4218 vm_page_deactivate_noreuse(m);
4219 else if (!vm_page_in_laundry(m))
4224 * Grab a page, waiting until we are waken up due to the page
4225 * changing state. We keep on waiting, if the page continues
4226 * to be in the object. If the page doesn't exist, first allocate it
4227 * and then conditionally zero it.
4229 * This routine may sleep.
4231 * The object must be locked on entry. The lock will, however, be released
4232 * and reacquired if the routine sleeps.
4235 vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags)
4241 VM_OBJECT_ASSERT_WLOCKED(object);
4242 KASSERT((allocflags & VM_ALLOC_SBUSY) == 0 ||
4243 (allocflags & VM_ALLOC_IGN_SBUSY) != 0,
4244 ("vm_page_grab: VM_ALLOC_SBUSY/VM_ALLOC_IGN_SBUSY mismatch"));
4245 pflags = allocflags &
4246 ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL |
4248 if ((allocflags & VM_ALLOC_NOWAIT) == 0)
4249 pflags |= VM_ALLOC_WAITFAIL;
4250 if ((allocflags & VM_ALLOC_IGN_SBUSY) != 0)
4251 pflags |= VM_ALLOC_SBUSY;
4253 if ((m = vm_page_lookup(object, pindex)) != NULL) {
4254 if ((allocflags & (VM_ALLOC_IGN_SBUSY | VM_ALLOC_SBUSY)) != 0)
4255 sleep = !vm_page_trysbusy(m);
4257 sleep = !vm_page_tryxbusy(m);
4259 if ((allocflags & VM_ALLOC_NOWAIT) != 0)
4262 * Reference the page before unlocking and
4263 * sleeping so that the page daemon is less
4264 * likely to reclaim it.
4266 if ((allocflags & VM_ALLOC_NOCREAT) == 0)
4267 vm_page_aflag_set(m, PGA_REFERENCED);
4268 vm_page_busy_sleep(m, "pgrbwt", (allocflags &
4269 VM_ALLOC_IGN_SBUSY) != 0);
4270 VM_OBJECT_WLOCK(object);
4271 if ((allocflags & VM_ALLOC_WAITFAIL) != 0)
4275 if ((allocflags & VM_ALLOC_WIRED) != 0)
4280 if ((allocflags & VM_ALLOC_NOCREAT) != 0)
4282 m = vm_page_alloc(object, pindex, pflags);
4284 if ((allocflags & VM_ALLOC_NOWAIT) != 0)
4288 if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0)
4292 if ((allocflags & VM_ALLOC_NOBUSY) != 0) {
4293 if ((allocflags & VM_ALLOC_IGN_SBUSY) != 0)
4302 * Grab a page and make it valid, paging in if necessary. Pages missing from
4303 * their pager are zero filled and validated.
4306 vm_page_grab_valid(vm_page_t *mp, vm_object_t object, vm_pindex_t pindex, int allocflags)
4313 KASSERT((allocflags & VM_ALLOC_SBUSY) == 0 ||
4314 (allocflags & VM_ALLOC_IGN_SBUSY) != 0,
4315 ("vm_page_grab_valid: VM_ALLOC_SBUSY/VM_ALLOC_IGN_SBUSY mismatch"));
4316 KASSERT((allocflags &
4317 (VM_ALLOC_NOWAIT | VM_ALLOC_WAITFAIL | VM_ALLOC_ZERO)) == 0,
4318 ("vm_page_grab_valid: Invalid flags 0x%X", allocflags));
4319 VM_OBJECT_ASSERT_WLOCKED(object);
4320 pflags = allocflags & ~(VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY);
4321 pflags |= VM_ALLOC_WAITFAIL;
4325 if ((m = vm_page_lookup(object, pindex)) != NULL) {
4327 * If the page is fully valid it can only become invalid
4328 * with the object lock held. If it is not valid it can
4329 * become valid with the busy lock held. Therefore, we
4330 * may unnecessarily lock the exclusive busy here if we
4331 * race with I/O completion not using the object lock.
4332 * However, we will not end up with an invalid page and a
4335 if (!vm_page_all_valid(m) ||
4336 (allocflags & (VM_ALLOC_IGN_SBUSY | VM_ALLOC_SBUSY)) == 0) {
4337 sleep = !vm_page_tryxbusy(m);
4340 sleep = !vm_page_trysbusy(m);
4343 * Reference the page before unlocking and
4344 * sleeping so that the page daemon is less
4345 * likely to reclaim it.
4347 if ((allocflags & VM_ALLOC_NOCREAT) == 0)
4348 vm_page_aflag_set(m, PGA_REFERENCED);
4349 vm_page_busy_sleep(m, "pgrbwt", (allocflags &
4350 VM_ALLOC_IGN_SBUSY) != 0);
4351 VM_OBJECT_WLOCK(object);
4354 if ((allocflags & VM_ALLOC_NOCREAT) != 0 &&
4355 !vm_page_all_valid(m)) {
4361 return (VM_PAGER_FAIL);
4363 if ((allocflags & VM_ALLOC_WIRED) != 0)
4365 if (vm_page_all_valid(m))
4367 } else if ((allocflags & VM_ALLOC_NOCREAT) != 0) {
4369 return (VM_PAGER_FAIL);
4370 } else if ((m = vm_page_alloc(object, pindex, pflags)) != NULL) {
4376 vm_page_assert_xbusied(m);
4378 if (vm_pager_has_page(object, pindex, NULL, NULL)) {
4379 rv = vm_pager_get_pages(object, &m, 1, NULL, NULL);
4380 if (rv != VM_PAGER_OK) {
4381 if (allocflags & VM_ALLOC_WIRED)
4382 vm_page_unwire_noq(m);
4387 MPASS(vm_page_all_valid(m));
4389 vm_page_zero_invalid(m, TRUE);
4392 if ((allocflags & VM_ALLOC_NOBUSY) != 0) {
4398 if ((allocflags & VM_ALLOC_SBUSY) != 0 && xbusy)
4399 vm_page_busy_downgrade(m);
4401 return (VM_PAGER_OK);
4405 * Return the specified range of pages from the given object. For each
4406 * page offset within the range, if a page already exists within the object
4407 * at that offset and it is busy, then wait for it to change state. If,
4408 * instead, the page doesn't exist, then allocate it.
4410 * The caller must always specify an allocation class.
4412 * allocation classes:
4413 * VM_ALLOC_NORMAL normal process request
4414 * VM_ALLOC_SYSTEM system *really* needs the pages
4416 * The caller must always specify that the pages are to be busied and/or
4419 * optional allocation flags:
4420 * VM_ALLOC_IGN_SBUSY do not sleep on soft busy pages
4421 * VM_ALLOC_NOBUSY do not exclusive busy the page
4422 * VM_ALLOC_NOWAIT do not sleep
4423 * VM_ALLOC_SBUSY set page to sbusy state
4424 * VM_ALLOC_WIRED wire the pages
4425 * VM_ALLOC_ZERO zero and validate any invalid pages
4427 * If VM_ALLOC_NOWAIT is not specified, this routine may sleep. Otherwise, it
4428 * may return a partial prefix of the requested range.
4431 vm_page_grab_pages(vm_object_t object, vm_pindex_t pindex, int allocflags,
4432 vm_page_t *ma, int count)
4439 VM_OBJECT_ASSERT_WLOCKED(object);
4440 KASSERT(((u_int)allocflags >> VM_ALLOC_COUNT_SHIFT) == 0,
4441 ("vm_page_grap_pages: VM_ALLOC_COUNT() is not allowed"));
4442 KASSERT((allocflags & VM_ALLOC_NOBUSY) == 0 ||
4443 (allocflags & VM_ALLOC_WIRED) != 0,
4444 ("vm_page_grab_pages: the pages must be busied or wired"));
4445 KASSERT((allocflags & VM_ALLOC_SBUSY) == 0 ||
4446 (allocflags & VM_ALLOC_IGN_SBUSY) != 0,
4447 ("vm_page_grab_pages: VM_ALLOC_SBUSY/IGN_SBUSY mismatch"));
4450 pflags = allocflags &
4451 ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL |
4453 if ((allocflags & VM_ALLOC_NOWAIT) == 0)
4454 pflags |= VM_ALLOC_WAITFAIL;
4455 if ((allocflags & VM_ALLOC_IGN_SBUSY) != 0)
4456 pflags |= VM_ALLOC_SBUSY;
4459 m = vm_radix_lookup_le(&object->rtree, pindex + i);
4460 if (m == NULL || m->pindex != pindex + i) {
4464 mpred = TAILQ_PREV(m, pglist, listq);
4465 for (; i < count; i++) {
4468 (VM_ALLOC_SBUSY | VM_ALLOC_IGN_SBUSY)) != 0)
4469 sleep = !vm_page_trysbusy(m);
4471 sleep = !vm_page_tryxbusy(m);
4473 if ((allocflags & VM_ALLOC_NOWAIT) != 0)
4476 * Reference the page before unlocking and
4477 * sleeping so that the page daemon is less
4478 * likely to reclaim it.
4480 if ((allocflags & VM_ALLOC_NOCREAT) == 0)
4481 vm_page_aflag_set(m, PGA_REFERENCED);
4482 vm_page_busy_sleep(m, "grbmaw", (allocflags &
4483 VM_ALLOC_IGN_SBUSY) != 0);
4484 VM_OBJECT_WLOCK(object);
4487 if ((allocflags & VM_ALLOC_WIRED) != 0)
4490 if ((allocflags & VM_ALLOC_NOCREAT) != 0)
4492 m = vm_page_alloc_after(object, pindex + i,
4493 pflags | VM_ALLOC_COUNT(count - i), mpred);
4495 if ((allocflags & VM_ALLOC_NOWAIT) != 0)
4500 if (vm_page_none_valid(m) &&
4501 (allocflags & VM_ALLOC_ZERO) != 0) {
4502 if ((m->flags & PG_ZERO) == 0)
4506 if ((allocflags & VM_ALLOC_NOBUSY) != 0) {
4507 if ((allocflags & VM_ALLOC_IGN_SBUSY) != 0)
4513 m = vm_page_next(m);
4519 * Mapping function for valid or dirty bits in a page.
4521 * Inputs are required to range within a page.
4524 vm_page_bits(int base, int size)
4530 base + size <= PAGE_SIZE,
4531 ("vm_page_bits: illegal base/size %d/%d", base, size)
4534 if (size == 0) /* handle degenerate case */
4537 first_bit = base >> DEV_BSHIFT;
4538 last_bit = (base + size - 1) >> DEV_BSHIFT;
4540 return (((vm_page_bits_t)2 << last_bit) -
4541 ((vm_page_bits_t)1 << first_bit));
4545 vm_page_bits_set(vm_page_t m, vm_page_bits_t *bits, vm_page_bits_t set)
4548 #if PAGE_SIZE == 32768
4549 atomic_set_64((uint64_t *)bits, set);
4550 #elif PAGE_SIZE == 16384
4551 atomic_set_32((uint32_t *)bits, set);
4552 #elif (PAGE_SIZE == 8192) && defined(atomic_set_16)
4553 atomic_set_16((uint16_t *)bits, set);
4554 #elif (PAGE_SIZE == 4096) && defined(atomic_set_8)
4555 atomic_set_8((uint8_t *)bits, set);
4556 #else /* PAGE_SIZE <= 8192 */
4560 addr = (uintptr_t)bits;
4562 * Use a trick to perform a 32-bit atomic on the
4563 * containing aligned word, to not depend on the existence
4564 * of atomic_{set, clear}_{8, 16}.
4566 shift = addr & (sizeof(uint32_t) - 1);
4567 #if BYTE_ORDER == BIG_ENDIAN
4568 shift = (sizeof(uint32_t) - sizeof(vm_page_bits_t) - shift) * NBBY;
4572 addr &= ~(sizeof(uint32_t) - 1);
4573 atomic_set_32((uint32_t *)addr, set << shift);
4574 #endif /* PAGE_SIZE */
4578 vm_page_bits_clear(vm_page_t m, vm_page_bits_t *bits, vm_page_bits_t clear)
4581 #if PAGE_SIZE == 32768
4582 atomic_clear_64((uint64_t *)bits, clear);
4583 #elif PAGE_SIZE == 16384
4584 atomic_clear_32((uint32_t *)bits, clear);
4585 #elif (PAGE_SIZE == 8192) && defined(atomic_clear_16)
4586 atomic_clear_16((uint16_t *)bits, clear);
4587 #elif (PAGE_SIZE == 4096) && defined(atomic_clear_8)
4588 atomic_clear_8((uint8_t *)bits, clear);
4589 #else /* PAGE_SIZE <= 8192 */
4593 addr = (uintptr_t)bits;
4595 * Use a trick to perform a 32-bit atomic on the
4596 * containing aligned word, to not depend on the existence
4597 * of atomic_{set, clear}_{8, 16}.
4599 shift = addr & (sizeof(uint32_t) - 1);
4600 #if BYTE_ORDER == BIG_ENDIAN
4601 shift = (sizeof(uint32_t) - sizeof(vm_page_bits_t) - shift) * NBBY;
4605 addr &= ~(sizeof(uint32_t) - 1);
4606 atomic_clear_32((uint32_t *)addr, clear << shift);
4607 #endif /* PAGE_SIZE */
4611 * vm_page_set_valid_range:
4613 * Sets portions of a page valid. The arguments are expected
4614 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
4615 * of any partial chunks touched by the range. The invalid portion of
4616 * such chunks will be zeroed.
4618 * (base + size) must be less then or equal to PAGE_SIZE.
4621 vm_page_set_valid_range(vm_page_t m, int base, int size)
4624 vm_page_bits_t pagebits;
4626 vm_page_assert_busied(m);
4627 if (size == 0) /* handle degenerate case */
4631 * If the base is not DEV_BSIZE aligned and the valid
4632 * bit is clear, we have to zero out a portion of the
4635 if ((frag = rounddown2(base, DEV_BSIZE)) != base &&
4636 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0)
4637 pmap_zero_page_area(m, frag, base - frag);
4640 * If the ending offset is not DEV_BSIZE aligned and the
4641 * valid bit is clear, we have to zero out a portion of
4644 endoff = base + size;
4645 if ((frag = rounddown2(endoff, DEV_BSIZE)) != endoff &&
4646 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0)
4647 pmap_zero_page_area(m, endoff,
4648 DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
4651 * Assert that no previously invalid block that is now being validated
4654 KASSERT((~m->valid & vm_page_bits(base, size) & m->dirty) == 0,
4655 ("vm_page_set_valid_range: page %p is dirty", m));
4658 * Set valid bits inclusive of any overlap.
4660 pagebits = vm_page_bits(base, size);
4661 if (vm_page_xbusied(m))
4662 m->valid |= pagebits;
4664 vm_page_bits_set(m, &m->valid, pagebits);
4668 * Clear the given bits from the specified page's dirty field.
4670 static __inline void
4671 vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits)
4674 vm_page_assert_busied(m);
4677 * If the page is xbusied and not write mapped we are the
4678 * only thread that can modify dirty bits. Otherwise, The pmap
4679 * layer can call vm_page_dirty() without holding a distinguished
4680 * lock. The combination of page busy and atomic operations
4681 * suffice to guarantee consistency of the page dirty field.
4683 if (vm_page_xbusied(m) && !pmap_page_is_write_mapped(m))
4684 m->dirty &= ~pagebits;
4686 vm_page_bits_clear(m, &m->dirty, pagebits);
4690 * vm_page_set_validclean:
4692 * Sets portions of a page valid and clean. The arguments are expected
4693 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
4694 * of any partial chunks touched by the range. The invalid portion of
4695 * such chunks will be zero'd.
4697 * (base + size) must be less then or equal to PAGE_SIZE.
4700 vm_page_set_validclean(vm_page_t m, int base, int size)
4702 vm_page_bits_t oldvalid, pagebits;
4705 vm_page_assert_busied(m);
4706 if (size == 0) /* handle degenerate case */
4710 * If the base is not DEV_BSIZE aligned and the valid
4711 * bit is clear, we have to zero out a portion of the
4714 if ((frag = rounddown2(base, DEV_BSIZE)) != base &&
4715 (m->valid & ((vm_page_bits_t)1 << (base >> DEV_BSHIFT))) == 0)
4716 pmap_zero_page_area(m, frag, base - frag);
4719 * If the ending offset is not DEV_BSIZE aligned and the
4720 * valid bit is clear, we have to zero out a portion of
4723 endoff = base + size;
4724 if ((frag = rounddown2(endoff, DEV_BSIZE)) != endoff &&
4725 (m->valid & ((vm_page_bits_t)1 << (endoff >> DEV_BSHIFT))) == 0)
4726 pmap_zero_page_area(m, endoff,
4727 DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
4730 * Set valid, clear dirty bits. If validating the entire
4731 * page we can safely clear the pmap modify bit. We also
4732 * use this opportunity to clear the PGA_NOSYNC flag. If a process
4733 * takes a write fault on a MAP_NOSYNC memory area the flag will
4736 * We set valid bits inclusive of any overlap, but we can only
4737 * clear dirty bits for DEV_BSIZE chunks that are fully within
4740 oldvalid = m->valid;
4741 pagebits = vm_page_bits(base, size);
4742 if (vm_page_xbusied(m))
4743 m->valid |= pagebits;
4745 vm_page_bits_set(m, &m->valid, pagebits);
4747 if ((frag = base & (DEV_BSIZE - 1)) != 0) {
4748 frag = DEV_BSIZE - frag;
4754 pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1));
4756 if (base == 0 && size == PAGE_SIZE) {
4758 * The page can only be modified within the pmap if it is
4759 * mapped, and it can only be mapped if it was previously
4762 if (oldvalid == VM_PAGE_BITS_ALL)
4764 * Perform the pmap_clear_modify() first. Otherwise,
4765 * a concurrent pmap operation, such as
4766 * pmap_protect(), could clear a modification in the
4767 * pmap and set the dirty field on the page before
4768 * pmap_clear_modify() had begun and after the dirty
4769 * field was cleared here.
4771 pmap_clear_modify(m);
4773 vm_page_aflag_clear(m, PGA_NOSYNC);
4774 } else if (oldvalid != VM_PAGE_BITS_ALL && vm_page_xbusied(m))
4775 m->dirty &= ~pagebits;
4777 vm_page_clear_dirty_mask(m, pagebits);
4781 vm_page_clear_dirty(vm_page_t m, int base, int size)
4784 vm_page_clear_dirty_mask(m, vm_page_bits(base, size));
4788 * vm_page_set_invalid:
4790 * Invalidates DEV_BSIZE'd chunks within a page. Both the
4791 * valid and dirty bits for the effected areas are cleared.
4794 vm_page_set_invalid(vm_page_t m, int base, int size)
4796 vm_page_bits_t bits;
4800 * The object lock is required so that pages can't be mapped
4801 * read-only while we're in the process of invalidating them.
4804 VM_OBJECT_ASSERT_WLOCKED(object);
4805 vm_page_assert_busied(m);
4807 if (object->type == OBJT_VNODE && base == 0 && IDX_TO_OFF(m->pindex) +
4808 size >= object->un_pager.vnp.vnp_size)
4809 bits = VM_PAGE_BITS_ALL;
4811 bits = vm_page_bits(base, size);
4812 if (object->ref_count != 0 && vm_page_all_valid(m) && bits != 0)
4814 KASSERT((bits == 0 && vm_page_all_valid(m)) ||
4815 !pmap_page_is_mapped(m),
4816 ("vm_page_set_invalid: page %p is mapped", m));
4817 if (vm_page_xbusied(m)) {
4821 vm_page_bits_clear(m, &m->valid, bits);
4822 vm_page_bits_clear(m, &m->dirty, bits);
4829 * Invalidates the entire page. The page must be busy, unmapped, and
4830 * the enclosing object must be locked. The object locks protects
4831 * against concurrent read-only pmap enter which is done without
4835 vm_page_invalid(vm_page_t m)
4838 vm_page_assert_busied(m);
4839 VM_OBJECT_ASSERT_LOCKED(m->object);
4840 MPASS(!pmap_page_is_mapped(m));
4842 if (vm_page_xbusied(m))
4845 vm_page_bits_clear(m, &m->valid, VM_PAGE_BITS_ALL);
4849 * vm_page_zero_invalid()
4851 * The kernel assumes that the invalid portions of a page contain
4852 * garbage, but such pages can be mapped into memory by user code.
4853 * When this occurs, we must zero out the non-valid portions of the
4854 * page so user code sees what it expects.
4856 * Pages are most often semi-valid when the end of a file is mapped
4857 * into memory and the file's size is not page aligned.
4860 vm_page_zero_invalid(vm_page_t m, boolean_t setvalid)
4866 * Scan the valid bits looking for invalid sections that
4867 * must be zeroed. Invalid sub-DEV_BSIZE'd areas ( where the
4868 * valid bit may be set ) have already been zeroed by
4869 * vm_page_set_validclean().
4871 for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) {
4872 if (i == (PAGE_SIZE / DEV_BSIZE) ||
4873 (m->valid & ((vm_page_bits_t)1 << i))) {
4875 pmap_zero_page_area(m,
4876 b << DEV_BSHIFT, (i - b) << DEV_BSHIFT);
4883 * setvalid is TRUE when we can safely set the zero'd areas
4884 * as being valid. We can do this if there are no cache consistancy
4885 * issues. e.g. it is ok to do with UFS, but not ok to do with NFS.
4894 * Is (partial) page valid? Note that the case where size == 0
4895 * will return FALSE in the degenerate case where the page is
4896 * entirely invalid, and TRUE otherwise.
4898 * Some callers envoke this routine without the busy lock held and
4899 * handle races via higher level locks. Typical callers should
4900 * hold a busy lock to prevent invalidation.
4903 vm_page_is_valid(vm_page_t m, int base, int size)
4905 vm_page_bits_t bits;
4907 bits = vm_page_bits(base, size);
4908 return (m->valid != 0 && (m->valid & bits) == bits);
4912 * Returns true if all of the specified predicates are true for the entire
4913 * (super)page and false otherwise.
4916 vm_page_ps_test(vm_page_t m, int flags, vm_page_t skip_m)
4922 if (skip_m != NULL && skip_m->object != object)
4924 VM_OBJECT_ASSERT_LOCKED(object);
4925 npages = atop(pagesizes[m->psind]);
4928 * The physically contiguous pages that make up a superpage, i.e., a
4929 * page with a page size index ("psind") greater than zero, will
4930 * occupy adjacent entries in vm_page_array[].
4932 for (i = 0; i < npages; i++) {
4933 /* Always test object consistency, including "skip_m". */
4934 if (m[i].object != object)
4936 if (&m[i] == skip_m)
4938 if ((flags & PS_NONE_BUSY) != 0 && vm_page_busied(&m[i]))
4940 if ((flags & PS_ALL_DIRTY) != 0) {
4942 * Calling vm_page_test_dirty() or pmap_is_modified()
4943 * might stop this case from spuriously returning
4944 * "false". However, that would require a write lock
4945 * on the object containing "m[i]".
4947 if (m[i].dirty != VM_PAGE_BITS_ALL)
4950 if ((flags & PS_ALL_VALID) != 0 &&
4951 m[i].valid != VM_PAGE_BITS_ALL)
4958 * Set the page's dirty bits if the page is modified.
4961 vm_page_test_dirty(vm_page_t m)
4964 vm_page_assert_busied(m);
4965 if (m->dirty != VM_PAGE_BITS_ALL && pmap_is_modified(m))
4970 vm_page_valid(vm_page_t m)
4973 vm_page_assert_busied(m);
4974 if (vm_page_xbusied(m))
4975 m->valid = VM_PAGE_BITS_ALL;
4977 vm_page_bits_set(m, &m->valid, VM_PAGE_BITS_ALL);
4981 vm_page_lock_KBI(vm_page_t m, const char *file, int line)
4984 mtx_lock_flags_(vm_page_lockptr(m), 0, file, line);
4988 vm_page_unlock_KBI(vm_page_t m, const char *file, int line)
4991 mtx_unlock_flags_(vm_page_lockptr(m), 0, file, line);
4995 vm_page_trylock_KBI(vm_page_t m, const char *file, int line)
4998 return (mtx_trylock_flags_(vm_page_lockptr(m), 0, file, line));
5001 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
5003 vm_page_assert_locked_KBI(vm_page_t m, const char *file, int line)
5006 vm_page_lock_assert_KBI(m, MA_OWNED, file, line);
5010 vm_page_lock_assert_KBI(vm_page_t m, int a, const char *file, int line)
5013 mtx_assert_(vm_page_lockptr(m), a, file, line);
5019 vm_page_object_busy_assert(vm_page_t m)
5023 * Certain of the page's fields may only be modified by the
5024 * holder of a page or object busy.
5026 if (m->object != NULL && !vm_page_busied(m))
5027 VM_OBJECT_ASSERT_BUSY(m->object);
5031 vm_page_assert_pga_writeable(vm_page_t m, uint8_t bits)
5034 if ((bits & PGA_WRITEABLE) == 0)
5038 * The PGA_WRITEABLE flag can only be set if the page is
5039 * managed, is exclusively busied or the object is locked.
5040 * Currently, this flag is only set by pmap_enter().
5042 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5043 ("PGA_WRITEABLE on unmanaged page"));
5044 if (!vm_page_xbusied(m))
5045 VM_OBJECT_ASSERT_BUSY(m->object);
5049 #include "opt_ddb.h"
5051 #include <sys/kernel.h>
5053 #include <ddb/ddb.h>
5055 DB_SHOW_COMMAND(page, vm_page_print_page_info)
5058 db_printf("vm_cnt.v_free_count: %d\n", vm_free_count());
5059 db_printf("vm_cnt.v_inactive_count: %d\n", vm_inactive_count());
5060 db_printf("vm_cnt.v_active_count: %d\n", vm_active_count());
5061 db_printf("vm_cnt.v_laundry_count: %d\n", vm_laundry_count());
5062 db_printf("vm_cnt.v_wire_count: %d\n", vm_wire_count());
5063 db_printf("vm_cnt.v_free_reserved: %d\n", vm_cnt.v_free_reserved);
5064 db_printf("vm_cnt.v_free_min: %d\n", vm_cnt.v_free_min);
5065 db_printf("vm_cnt.v_free_target: %d\n", vm_cnt.v_free_target);
5066 db_printf("vm_cnt.v_inactive_target: %d\n", vm_cnt.v_inactive_target);
5069 DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info)
5073 db_printf("pq_free %d\n", vm_free_count());
5074 for (dom = 0; dom < vm_ndomains; dom++) {
5076 "dom %d page_cnt %d free %d pq_act %d pq_inact %d pq_laund %d pq_unsw %d\n",
5078 vm_dom[dom].vmd_page_count,
5079 vm_dom[dom].vmd_free_count,
5080 vm_dom[dom].vmd_pagequeues[PQ_ACTIVE].pq_cnt,
5081 vm_dom[dom].vmd_pagequeues[PQ_INACTIVE].pq_cnt,
5082 vm_dom[dom].vmd_pagequeues[PQ_LAUNDRY].pq_cnt,
5083 vm_dom[dom].vmd_pagequeues[PQ_UNSWAPPABLE].pq_cnt);
5087 DB_SHOW_COMMAND(pginfo, vm_page_print_pginfo)
5090 boolean_t phys, virt;
5093 db_printf("show pginfo addr\n");
5097 phys = strchr(modif, 'p') != NULL;
5098 virt = strchr(modif, 'v') != NULL;
5100 m = PHYS_TO_VM_PAGE(pmap_kextract(addr));
5102 m = PHYS_TO_VM_PAGE(addr);
5104 m = (vm_page_t)addr;
5106 "page %p obj %p pidx 0x%jx phys 0x%jx q %d ref %u\n"
5107 " af 0x%x of 0x%x f 0x%x act %d busy %x valid 0x%x dirty 0x%x\n",
5108 m, m->object, (uintmax_t)m->pindex, (uintmax_t)m->phys_addr,
5109 m->queue, m->ref_count, m->aflags, m->oflags,
5110 m->flags, m->act_count, m->busy_lock, m->valid, m->dirty);