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 TAILQ_HEAD(, vm_page) blacklist_head;
172 static int sysctl_vm_page_blacklist(SYSCTL_HANDLER_ARGS);
173 SYSCTL_PROC(_vm, OID_AUTO, page_blacklist, CTLTYPE_STRING | CTLFLAG_RD |
174 CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_page_blacklist, "A", "Blacklist pages");
176 static uma_zone_t fakepg_zone;
178 static void vm_page_alloc_check(vm_page_t m);
179 static void _vm_page_busy_sleep(vm_object_t obj, vm_page_t m,
180 const char *wmesg, bool nonshared, bool locked);
181 static void vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits);
182 static void vm_page_dequeue_complete(vm_page_t m);
183 static void vm_page_enqueue(vm_page_t m, uint8_t queue);
184 static void vm_page_init(void *dummy);
185 static int vm_page_insert_after(vm_page_t m, vm_object_t object,
186 vm_pindex_t pindex, vm_page_t mpred);
187 static void vm_page_insert_radixdone(vm_page_t m, vm_object_t object,
189 static void vm_page_mvqueue(vm_page_t m, uint8_t queue);
190 static int vm_page_reclaim_run(int req_class, int domain, u_long npages,
191 vm_page_t m_run, vm_paddr_t high);
192 static int vm_domain_alloc_fail(struct vm_domain *vmd, vm_object_t object,
194 static int vm_page_zone_import(void *arg, void **store, int cnt, int domain,
196 static void vm_page_zone_release(void *arg, void **store, int cnt);
198 SYSINIT(vm_page, SI_SUB_VM, SI_ORDER_SECOND, vm_page_init, NULL);
201 vm_page_init(void *dummy)
204 fakepg_zone = uma_zcreate("fakepg", sizeof(struct vm_page), NULL, NULL,
205 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE | UMA_ZONE_VM);
206 bogus_page = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ |
207 VM_ALLOC_NORMAL | VM_ALLOC_WIRED);
211 * The cache page zone is initialized later since we need to be able to allocate
212 * pages before UMA is fully initialized.
215 vm_page_init_cache_zones(void *dummy __unused)
217 struct vm_domain *vmd;
218 struct vm_pgcache *pgcache;
219 int domain, maxcache, pool;
222 TUNABLE_INT_FETCH("vm.pgcache_zone_max", &maxcache);
223 for (domain = 0; domain < vm_ndomains; domain++) {
224 vmd = VM_DOMAIN(domain);
227 * Don't allow the page caches to take up more than .1875% of
228 * memory. A UMA bucket contains at most 256 free pages, and we
229 * have two buckets per CPU per free pool.
231 if (vmd->vmd_page_count / 600 < 2 * 256 * mp_ncpus *
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, maxcache);
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);
258 * Sets the page size, perhaps based upon the memory
259 * size. Must be called before any use of page-size
260 * dependent functions.
263 vm_set_page_size(void)
265 if (vm_cnt.v_page_size == 0)
266 vm_cnt.v_page_size = PAGE_SIZE;
267 if (((vm_cnt.v_page_size - 1) & vm_cnt.v_page_size) != 0)
268 panic("vm_set_page_size: page size not a power of two");
272 * vm_page_blacklist_next:
274 * Find the next entry in the provided string of blacklist
275 * addresses. Entries are separated by space, comma, or newline.
276 * If an invalid integer is encountered then the rest of the
277 * string is skipped. Updates the list pointer to the next
278 * character, or NULL if the string is exhausted or invalid.
281 vm_page_blacklist_next(char **list, char *end)
286 if (list == NULL || *list == NULL)
294 * If there's no end pointer then the buffer is coming from
295 * the kenv and we know it's null-terminated.
298 end = *list + strlen(*list);
300 /* Ensure that strtoq() won't walk off the end */
302 if (*end == '\n' || *end == ' ' || *end == ',')
305 printf("Blacklist not terminated, skipping\n");
311 for (pos = *list; *pos != '\0'; pos = cp) {
312 bad = strtoq(pos, &cp, 0);
313 if (*cp == '\0' || *cp == ' ' || *cp == ',' || *cp == '\n') {
322 if (*cp == '\0' || ++cp >= end)
326 return (trunc_page(bad));
328 printf("Garbage in RAM blacklist, skipping\n");
334 vm_page_blacklist_add(vm_paddr_t pa, bool verbose)
336 struct vm_domain *vmd;
340 m = vm_phys_paddr_to_vm_page(pa);
342 return (true); /* page does not exist, no failure */
344 vmd = vm_pagequeue_domain(m);
345 vm_domain_free_lock(vmd);
346 ret = vm_phys_unfree_page(m);
347 vm_domain_free_unlock(vmd);
349 vm_domain_freecnt_inc(vmd, -1);
350 TAILQ_INSERT_TAIL(&blacklist_head, m, listq);
352 printf("Skipping page with pa 0x%jx\n", (uintmax_t)pa);
358 * vm_page_blacklist_check:
360 * Iterate through the provided string of blacklist addresses, pulling
361 * each entry out of the physical allocator free list and putting it
362 * onto a list for reporting via the vm.page_blacklist sysctl.
365 vm_page_blacklist_check(char *list, char *end)
371 while (next != NULL) {
372 if ((pa = vm_page_blacklist_next(&next, end)) == 0)
374 vm_page_blacklist_add(pa, bootverbose);
379 * vm_page_blacklist_load:
381 * Search for a special module named "ram_blacklist". It'll be a
382 * plain text file provided by the user via the loader directive
386 vm_page_blacklist_load(char **list, char **end)
395 mod = preload_search_by_type("ram_blacklist");
397 ptr = preload_fetch_addr(mod);
398 len = preload_fetch_size(mod);
409 sysctl_vm_page_blacklist(SYSCTL_HANDLER_ARGS)
416 error = sysctl_wire_old_buffer(req, 0);
419 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
420 TAILQ_FOREACH(m, &blacklist_head, listq) {
421 sbuf_printf(&sbuf, "%s%#jx", first ? "" : ",",
422 (uintmax_t)m->phys_addr);
425 error = sbuf_finish(&sbuf);
431 * Initialize a dummy page for use in scans of the specified paging queue.
432 * In principle, this function only needs to set the flag PG_MARKER.
433 * Nonetheless, it write busies the page as a safety precaution.
436 vm_page_init_marker(vm_page_t marker, int queue, uint8_t aflags)
439 bzero(marker, sizeof(*marker));
440 marker->flags = PG_MARKER;
441 marker->aflags = aflags;
442 marker->busy_lock = VPB_SINGLE_EXCLUSIVER;
443 marker->queue = queue;
447 vm_page_domain_init(int domain)
449 struct vm_domain *vmd;
450 struct vm_pagequeue *pq;
453 vmd = VM_DOMAIN(domain);
454 bzero(vmd, sizeof(*vmd));
455 *__DECONST(char **, &vmd->vmd_pagequeues[PQ_INACTIVE].pq_name) =
456 "vm inactive pagequeue";
457 *__DECONST(char **, &vmd->vmd_pagequeues[PQ_ACTIVE].pq_name) =
458 "vm active pagequeue";
459 *__DECONST(char **, &vmd->vmd_pagequeues[PQ_LAUNDRY].pq_name) =
460 "vm laundry pagequeue";
461 *__DECONST(char **, &vmd->vmd_pagequeues[PQ_UNSWAPPABLE].pq_name) =
462 "vm unswappable pagequeue";
463 vmd->vmd_domain = domain;
464 vmd->vmd_page_count = 0;
465 vmd->vmd_free_count = 0;
467 vmd->vmd_oom = FALSE;
468 for (i = 0; i < PQ_COUNT; i++) {
469 pq = &vmd->vmd_pagequeues[i];
470 TAILQ_INIT(&pq->pq_pl);
471 mtx_init(&pq->pq_mutex, pq->pq_name, "vm pagequeue",
472 MTX_DEF | MTX_DUPOK);
474 vm_page_init_marker(&vmd->vmd_markers[i], i, 0);
476 mtx_init(&vmd->vmd_free_mtx, "vm page free queue", NULL, MTX_DEF);
477 mtx_init(&vmd->vmd_pageout_mtx, "vm pageout lock", NULL, MTX_DEF);
478 snprintf(vmd->vmd_name, sizeof(vmd->vmd_name), "%d", domain);
481 * inacthead is used to provide FIFO ordering for LRU-bypassing
484 vm_page_init_marker(&vmd->vmd_inacthead, PQ_INACTIVE, PGA_ENQUEUED);
485 TAILQ_INSERT_HEAD(&vmd->vmd_pagequeues[PQ_INACTIVE].pq_pl,
486 &vmd->vmd_inacthead, plinks.q);
489 * The clock pages are used to implement active queue scanning without
490 * requeues. Scans start at clock[0], which is advanced after the scan
491 * ends. When the two clock hands meet, they are reset and scanning
492 * resumes from the head of the queue.
494 vm_page_init_marker(&vmd->vmd_clock[0], PQ_ACTIVE, PGA_ENQUEUED);
495 vm_page_init_marker(&vmd->vmd_clock[1], PQ_ACTIVE, PGA_ENQUEUED);
496 TAILQ_INSERT_HEAD(&vmd->vmd_pagequeues[PQ_ACTIVE].pq_pl,
497 &vmd->vmd_clock[0], plinks.q);
498 TAILQ_INSERT_TAIL(&vmd->vmd_pagequeues[PQ_ACTIVE].pq_pl,
499 &vmd->vmd_clock[1], plinks.q);
503 * Initialize a physical page in preparation for adding it to the free
507 vm_page_init_page(vm_page_t m, vm_paddr_t pa, int segind)
512 m->busy_lock = VPB_UNBUSIED;
513 m->flags = m->aflags = 0;
518 m->order = VM_NFREEORDER;
519 m->pool = VM_FREEPOOL_DEFAULT;
520 m->valid = m->dirty = 0;
524 #ifndef PMAP_HAS_PAGE_ARRAY
526 vm_page_array_alloc(vm_offset_t *vaddr, vm_paddr_t end, vm_paddr_t page_range)
531 * Reserve an unmapped guard page to trap access to vm_page_array[-1].
532 * However, because this page is allocated from KVM, out-of-bounds
533 * accesses using the direct map will not be trapped.
538 * Allocate physical memory for the page structures, and map it.
540 new_end = trunc_page(end - page_range * sizeof(struct vm_page));
541 vm_page_array = (vm_page_t)pmap_map(vaddr, new_end, end,
542 VM_PROT_READ | VM_PROT_WRITE);
543 vm_page_array_size = page_range;
552 * Initializes the resident memory module. Allocates physical memory for
553 * bootstrapping UMA and some data structures that are used to manage
554 * physical pages. Initializes these structures, and populates the free
558 vm_page_startup(vm_offset_t vaddr)
560 struct vm_phys_seg *seg;
562 char *list, *listend;
564 vm_paddr_t end, high_avail, low_avail, new_end, page_range, size;
565 vm_paddr_t last_pa, pa;
567 int biggestone, i, segind;
571 #if defined(__i386__) && defined(VM_PHYSSEG_DENSE)
575 vaddr = round_page(vaddr);
577 vm_phys_early_startup();
578 biggestone = vm_phys_avail_largest();
579 end = phys_avail[biggestone+1];
582 * Initialize the page and queue locks.
584 mtx_init(&vm_domainset_lock, "vm domainset lock", NULL, MTX_DEF);
585 for (i = 0; i < PA_LOCK_COUNT; i++)
586 mtx_init(&pa_lock[i], "vm page", NULL, MTX_DEF);
587 for (i = 0; i < vm_ndomains; i++)
588 vm_page_domain_init(i);
591 * Allocate memory for use when boot strapping the kernel memory
592 * allocator. Tell UMA how many zones we are going to create
593 * before going fully functional. UMA will add its zones.
595 * VM startup zones: vmem, vmem_btag, VM OBJECT, RADIX NODE, MAP,
596 * KMAP ENTRY, MAP ENTRY, VMSPACE.
598 boot_pages = uma_startup_count(8);
600 #ifndef UMA_MD_SMALL_ALLOC
601 /* vmem_startup() calls uma_prealloc(). */
602 boot_pages += vmem_startup_count();
603 /* vm_map_startup() calls uma_prealloc(). */
604 boot_pages += howmany(MAX_KMAP,
605 UMA_SLAB_SPACE / sizeof(struct vm_map));
608 * Before going fully functional kmem_init() does allocation
609 * from "KMAP ENTRY" and vmem_create() does allocation from "vmem".
614 * CTFLAG_RDTUN doesn't work during the early boot process, so we must
615 * manually fetch the value.
617 TUNABLE_INT_FETCH("vm.boot_pages", &boot_pages);
618 new_end = end - (boot_pages * UMA_SLAB_SIZE);
619 new_end = trunc_page(new_end);
620 mapped = pmap_map(&vaddr, new_end, end,
621 VM_PROT_READ | VM_PROT_WRITE);
622 bzero((void *)mapped, end - new_end);
623 uma_startup((void *)mapped, boot_pages);
626 witness_size = round_page(witness_startup_count());
627 new_end -= witness_size;
628 mapped = pmap_map(&vaddr, new_end, new_end + witness_size,
629 VM_PROT_READ | VM_PROT_WRITE);
630 bzero((void *)mapped, witness_size);
631 witness_startup((void *)mapped);
634 #if defined(__aarch64__) || defined(__amd64__) || defined(__arm__) || \
635 defined(__i386__) || defined(__mips__) || defined(__riscv) || \
636 defined(__powerpc64__)
638 * Allocate a bitmap to indicate that a random physical page
639 * needs to be included in a minidump.
641 * The amd64 port needs this to indicate which direct map pages
642 * need to be dumped, via calls to dump_add_page()/dump_drop_page().
644 * However, i386 still needs this workspace internally within the
645 * minidump code. In theory, they are not needed on i386, but are
646 * included should the sf_buf code decide to use them.
649 for (i = 0; dump_avail[i + 1] != 0; i += 2)
650 if (dump_avail[i + 1] > last_pa)
651 last_pa = dump_avail[i + 1];
652 page_range = last_pa / PAGE_SIZE;
653 vm_page_dump_size = round_page(roundup2(page_range, NBBY) / NBBY);
654 new_end -= vm_page_dump_size;
655 vm_page_dump = (void *)(uintptr_t)pmap_map(&vaddr, new_end,
656 new_end + vm_page_dump_size, VM_PROT_READ | VM_PROT_WRITE);
657 bzero((void *)vm_page_dump, vm_page_dump_size);
661 #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) || \
662 defined(__riscv) || defined(__powerpc64__)
664 * Include the UMA bootstrap pages, witness pages and vm_page_dump
665 * in a crash dump. When pmap_map() uses the direct map, they are
666 * not automatically included.
668 for (pa = new_end; pa < end; pa += PAGE_SIZE)
671 phys_avail[biggestone + 1] = new_end;
674 * Request that the physical pages underlying the message buffer be
675 * included in a crash dump. Since the message buffer is accessed
676 * through the direct map, they are not automatically included.
678 pa = DMAP_TO_PHYS((vm_offset_t)msgbufp->msg_ptr);
679 last_pa = pa + round_page(msgbufsize);
680 while (pa < last_pa) {
686 * Compute the number of pages of memory that will be available for
687 * use, taking into account the overhead of a page structure per page.
688 * In other words, solve
689 * "available physical memory" - round_page(page_range *
690 * sizeof(struct vm_page)) = page_range * PAGE_SIZE
693 low_avail = phys_avail[0];
694 high_avail = phys_avail[1];
695 for (i = 0; i < vm_phys_nsegs; i++) {
696 if (vm_phys_segs[i].start < low_avail)
697 low_avail = vm_phys_segs[i].start;
698 if (vm_phys_segs[i].end > high_avail)
699 high_avail = vm_phys_segs[i].end;
701 /* Skip the first chunk. It is already accounted for. */
702 for (i = 2; phys_avail[i + 1] != 0; i += 2) {
703 if (phys_avail[i] < low_avail)
704 low_avail = phys_avail[i];
705 if (phys_avail[i + 1] > high_avail)
706 high_avail = phys_avail[i + 1];
708 first_page = low_avail / PAGE_SIZE;
709 #ifdef VM_PHYSSEG_SPARSE
711 for (i = 0; i < vm_phys_nsegs; i++)
712 size += vm_phys_segs[i].end - vm_phys_segs[i].start;
713 for (i = 0; phys_avail[i + 1] != 0; i += 2)
714 size += phys_avail[i + 1] - phys_avail[i];
715 #elif defined(VM_PHYSSEG_DENSE)
716 size = high_avail - low_avail;
718 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
721 #ifdef PMAP_HAS_PAGE_ARRAY
722 pmap_page_array_startup(size / PAGE_SIZE);
723 biggestone = vm_phys_avail_largest();
724 end = new_end = phys_avail[biggestone + 1];
726 #ifdef VM_PHYSSEG_DENSE
728 * In the VM_PHYSSEG_DENSE case, the number of pages can account for
729 * the overhead of a page structure per page only if vm_page_array is
730 * allocated from the last physical memory chunk. Otherwise, we must
731 * allocate page structures representing the physical memory
732 * underlying vm_page_array, even though they will not be used.
734 if (new_end != high_avail)
735 page_range = size / PAGE_SIZE;
739 page_range = size / (PAGE_SIZE + sizeof(struct vm_page));
742 * If the partial bytes remaining are large enough for
743 * a page (PAGE_SIZE) without a corresponding
744 * 'struct vm_page', then new_end will contain an
745 * extra page after subtracting the length of the VM
746 * page array. Compensate by subtracting an extra
749 if (size % (PAGE_SIZE + sizeof(struct vm_page)) >= PAGE_SIZE) {
750 if (new_end == high_avail)
751 high_avail -= PAGE_SIZE;
752 new_end -= PAGE_SIZE;
756 new_end = vm_page_array_alloc(&vaddr, end, page_range);
759 #if VM_NRESERVLEVEL > 0
761 * Allocate physical memory for the reservation management system's
762 * data structures, and map it.
764 new_end = vm_reserv_startup(&vaddr, new_end);
766 #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) || \
767 defined(__riscv) || defined(__powerpc64__)
769 * Include vm_page_array and vm_reserv_array in a crash dump.
771 for (pa = new_end; pa < end; pa += PAGE_SIZE)
774 phys_avail[biggestone + 1] = new_end;
777 * Add physical memory segments corresponding to the available
780 for (i = 0; phys_avail[i + 1] != 0; i += 2)
781 if (vm_phys_avail_size(i) != 0)
782 vm_phys_add_seg(phys_avail[i], phys_avail[i + 1]);
785 * Initialize the physical memory allocator.
790 * Initialize the page structures and add every available page to the
791 * physical memory allocator's free lists.
793 #if defined(__i386__) && defined(VM_PHYSSEG_DENSE)
794 for (ii = 0; ii < vm_page_array_size; ii++) {
795 m = &vm_page_array[ii];
796 vm_page_init_page(m, (first_page + ii) << PAGE_SHIFT, 0);
797 m->flags = PG_FICTITIOUS;
800 vm_cnt.v_page_count = 0;
801 for (segind = 0; segind < vm_phys_nsegs; segind++) {
802 seg = &vm_phys_segs[segind];
803 for (m = seg->first_page, pa = seg->start; pa < seg->end;
804 m++, pa += PAGE_SIZE)
805 vm_page_init_page(m, pa, segind);
808 * Add the segment to the free lists only if it is covered by
809 * one of the ranges in phys_avail. Because we've added the
810 * ranges to the vm_phys_segs array, we can assume that each
811 * segment is either entirely contained in one of the ranges,
812 * or doesn't overlap any of them.
814 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
815 struct vm_domain *vmd;
817 if (seg->start < phys_avail[i] ||
818 seg->end > phys_avail[i + 1])
822 pagecount = (u_long)atop(seg->end - seg->start);
824 vmd = VM_DOMAIN(seg->domain);
825 vm_domain_free_lock(vmd);
826 vm_phys_enqueue_contig(m, pagecount);
827 vm_domain_free_unlock(vmd);
828 vm_domain_freecnt_inc(vmd, pagecount);
829 vm_cnt.v_page_count += (u_int)pagecount;
831 vmd = VM_DOMAIN(seg->domain);
832 vmd->vmd_page_count += (u_int)pagecount;
833 vmd->vmd_segs |= 1UL << m->segind;
839 * Remove blacklisted pages from the physical memory allocator.
841 TAILQ_INIT(&blacklist_head);
842 vm_page_blacklist_load(&list, &listend);
843 vm_page_blacklist_check(list, listend);
845 list = kern_getenv("vm.blacklist");
846 vm_page_blacklist_check(list, NULL);
849 #if VM_NRESERVLEVEL > 0
851 * Initialize the reservation management system.
860 vm_page_reference(vm_page_t m)
863 vm_page_aflag_set(m, PGA_REFERENCED);
867 * vm_page_busy_acquire:
869 * Acquire the busy lock as described by VM_ALLOC_* flags. Will loop
870 * and drop the object lock if necessary.
873 vm_page_busy_acquire(vm_page_t m, int allocflags)
879 * The page-specific object must be cached because page
880 * identity can change during the sleep, causing the
881 * re-lock of a different object.
882 * It is assumed that a reference to the object is already
883 * held by the callers.
887 if ((allocflags & VM_ALLOC_SBUSY) == 0) {
888 if (vm_page_tryxbusy(m))
891 if (vm_page_trysbusy(m))
894 if ((allocflags & VM_ALLOC_NOWAIT) != 0)
897 locked = VM_OBJECT_WOWNED(obj);
900 MPASS(locked || vm_page_wired(m));
901 _vm_page_busy_sleep(obj, m, "vmpba",
902 (allocflags & VM_ALLOC_SBUSY) != 0, locked);
904 VM_OBJECT_WLOCK(obj);
905 if ((allocflags & VM_ALLOC_WAITFAIL) != 0)
907 KASSERT(m->object == obj || m->object == NULL,
908 ("vm_page_busy_acquire: page %p does not belong to %p",
914 * vm_page_busy_downgrade:
916 * Downgrade an exclusive busy page into a single shared busy page.
919 vm_page_busy_downgrade(vm_page_t m)
923 vm_page_assert_xbusied(m);
927 if (atomic_fcmpset_rel_int(&m->busy_lock,
928 &x, VPB_SHARERS_WORD(1)))
931 if ((x & VPB_BIT_WAITERS) != 0)
937 * vm_page_busy_tryupgrade:
939 * Attempt to upgrade a single shared busy into an exclusive busy.
942 vm_page_busy_tryupgrade(vm_page_t m)
946 vm_page_assert_sbusied(m);
950 if (VPB_SHARERS(x) > 1)
952 KASSERT((x & ~VPB_BIT_WAITERS) == VPB_SHARERS_WORD(1),
953 ("vm_page_busy_tryupgrade: invalid lock state"));
954 if (!atomic_fcmpset_acq_int(&m->busy_lock, &x,
955 VPB_SINGLE_EXCLUSIVER | (x & VPB_BIT_WAITERS)))
964 * Return a positive value if the page is shared busied, 0 otherwise.
967 vm_page_sbusied(vm_page_t m)
972 return ((x & VPB_BIT_SHARED) != 0 && x != VPB_UNBUSIED);
978 * Shared unbusy a page.
981 vm_page_sunbusy(vm_page_t m)
985 vm_page_assert_sbusied(m);
989 if (VPB_SHARERS(x) > 1) {
990 if (atomic_fcmpset_int(&m->busy_lock, &x,
995 KASSERT((x & ~VPB_BIT_WAITERS) == VPB_SHARERS_WORD(1),
996 ("vm_page_sunbusy: invalid lock state"));
997 if (!atomic_fcmpset_rel_int(&m->busy_lock, &x, VPB_UNBUSIED))
999 if ((x & VPB_BIT_WAITERS) == 0)
1007 * vm_page_busy_sleep:
1009 * Sleep if the page is busy, using the page pointer as wchan.
1010 * This is used to implement the hard-path of busying mechanism.
1012 * If nonshared is true, sleep only if the page is xbusy.
1014 * The object lock must be held on entry and will be released on exit.
1017 vm_page_busy_sleep(vm_page_t m, const char *wmesg, bool nonshared)
1022 VM_OBJECT_ASSERT_LOCKED(obj);
1023 vm_page_lock_assert(m, MA_NOTOWNED);
1025 _vm_page_busy_sleep(obj, m, wmesg, nonshared, true);
1029 _vm_page_busy_sleep(vm_object_t obj, vm_page_t m, const char *wmesg,
1030 bool nonshared, bool locked)
1035 * If the object is busy we must wait for that to drain to zero
1036 * before trying the page again.
1038 if (obj != NULL && vm_object_busied(obj)) {
1040 VM_OBJECT_DROP(obj);
1041 vm_object_busy_wait(obj, wmesg);
1046 if (x == VPB_UNBUSIED || (nonshared && (x & VPB_BIT_SHARED) != 0) ||
1047 ((x & VPB_BIT_WAITERS) == 0 &&
1048 !atomic_cmpset_int(&m->busy_lock, x, x | VPB_BIT_WAITERS))) {
1050 VM_OBJECT_DROP(obj);
1055 VM_OBJECT_DROP(obj);
1056 sleepq_add(m, NULL, wmesg, 0, 0);
1057 sleepq_wait(m, PVM);
1063 * Try to shared busy a page.
1064 * If the operation succeeds 1 is returned otherwise 0.
1065 * The operation never sleeps.
1068 vm_page_trysbusy(vm_page_t m)
1076 if ((x & VPB_BIT_SHARED) == 0)
1079 * Reduce the window for transient busies that will trigger
1080 * false negatives in vm_page_ps_test().
1082 if (obj != NULL && vm_object_busied(obj))
1084 if (atomic_fcmpset_acq_int(&m->busy_lock, &x,
1085 x + VPB_ONE_SHARER))
1089 /* Refetch the object now that we're guaranteed that it is stable. */
1091 if (obj != NULL && vm_object_busied(obj)) {
1101 * Try to exclusive busy a page.
1102 * If the operation succeeds 1 is returned otherwise 0.
1103 * The operation never sleeps.
1106 vm_page_tryxbusy(vm_page_t m)
1110 if (atomic_cmpset_acq_int(&(m)->busy_lock, VPB_UNBUSIED,
1111 VPB_SINGLE_EXCLUSIVER) == 0)
1115 if (obj != NULL && vm_object_busied(obj)) {
1123 * vm_page_xunbusy_hard:
1125 * Called when unbusy has failed because there is a waiter.
1128 vm_page_xunbusy_hard(vm_page_t m)
1131 vm_page_assert_xbusied(m);
1136 atomic_store_rel_int(&m->busy_lock, VPB_UNBUSIED);
1141 * Avoid releasing and reacquiring the same page lock.
1144 vm_page_change_lock(vm_page_t m, struct mtx **mtx)
1148 mtx1 = vm_page_lockptr(m);
1158 * vm_page_unhold_pages:
1160 * Unhold each of the pages that is referenced by the given array.
1163 vm_page_unhold_pages(vm_page_t *ma, int count)
1166 for (; count != 0; count--) {
1167 vm_page_unwire(*ma, PQ_ACTIVE);
1173 PHYS_TO_VM_PAGE(vm_paddr_t pa)
1177 #ifdef VM_PHYSSEG_SPARSE
1178 m = vm_phys_paddr_to_vm_page(pa);
1180 m = vm_phys_fictitious_to_vm_page(pa);
1182 #elif defined(VM_PHYSSEG_DENSE)
1186 if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
1187 m = &vm_page_array[pi - first_page];
1190 return (vm_phys_fictitious_to_vm_page(pa));
1192 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
1199 * Create a fictitious page with the specified physical address and
1200 * memory attribute. The memory attribute is the only the machine-
1201 * dependent aspect of a fictitious page that must be initialized.
1204 vm_page_getfake(vm_paddr_t paddr, vm_memattr_t memattr)
1208 m = uma_zalloc(fakepg_zone, M_WAITOK | M_ZERO);
1209 vm_page_initfake(m, paddr, memattr);
1214 vm_page_initfake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
1217 if ((m->flags & PG_FICTITIOUS) != 0) {
1219 * The page's memattr might have changed since the
1220 * previous initialization. Update the pmap to the
1225 m->phys_addr = paddr;
1227 /* Fictitious pages don't use "segind". */
1228 m->flags = PG_FICTITIOUS;
1229 /* Fictitious pages don't use "order" or "pool". */
1230 m->oflags = VPO_UNMANAGED;
1231 m->busy_lock = VPB_SINGLE_EXCLUSIVER;
1232 /* Fictitious pages are unevictable. */
1236 pmap_page_set_memattr(m, memattr);
1242 * Release a fictitious page.
1245 vm_page_putfake(vm_page_t m)
1248 KASSERT((m->oflags & VPO_UNMANAGED) != 0, ("managed %p", m));
1249 KASSERT((m->flags & PG_FICTITIOUS) != 0,
1250 ("vm_page_putfake: bad page %p", m));
1251 if (vm_page_xbusied(m))
1253 uma_zfree(fakepg_zone, m);
1257 * vm_page_updatefake:
1259 * Update the given fictitious page to the specified physical address and
1263 vm_page_updatefake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
1266 KASSERT((m->flags & PG_FICTITIOUS) != 0,
1267 ("vm_page_updatefake: bad page %p", m));
1268 m->phys_addr = paddr;
1269 pmap_page_set_memattr(m, memattr);
1278 vm_page_free(vm_page_t m)
1281 m->flags &= ~PG_ZERO;
1282 vm_page_free_toq(m);
1286 * vm_page_free_zero:
1288 * Free a page to the zerod-pages queue
1291 vm_page_free_zero(vm_page_t m)
1294 m->flags |= PG_ZERO;
1295 vm_page_free_toq(m);
1299 * Unbusy and handle the page queueing for a page from a getpages request that
1300 * was optionally read ahead or behind.
1303 vm_page_readahead_finish(vm_page_t m)
1306 /* We shouldn't put invalid pages on queues. */
1307 KASSERT(!vm_page_none_valid(m), ("%s: %p is invalid", __func__, m));
1310 * Since the page is not the actually needed one, whether it should
1311 * be activated or deactivated is not obvious. Empirical results
1312 * have shown that deactivating the page is usually the best choice,
1313 * unless the page is wanted by another thread.
1316 if ((m->busy_lock & VPB_BIT_WAITERS) != 0)
1317 vm_page_activate(m);
1319 vm_page_deactivate(m);
1325 * vm_page_sleep_if_busy:
1327 * Sleep and release the object lock if the page is busied.
1328 * Returns TRUE if the thread slept.
1330 * The given page must be unlocked and object containing it must
1334 vm_page_sleep_if_busy(vm_page_t m, const char *msg)
1338 vm_page_lock_assert(m, MA_NOTOWNED);
1339 VM_OBJECT_ASSERT_WLOCKED(m->object);
1342 * The page-specific object must be cached because page
1343 * identity can change during the sleep, causing the
1344 * re-lock of a different object.
1345 * It is assumed that a reference to the object is already
1346 * held by the callers.
1349 if (vm_page_busied(m) || (obj != NULL && obj->busy)) {
1350 vm_page_busy_sleep(m, msg, false);
1351 VM_OBJECT_WLOCK(obj);
1358 * vm_page_sleep_if_xbusy:
1360 * Sleep and release the object lock if the page is xbusied.
1361 * Returns TRUE if the thread slept.
1363 * The given page must be unlocked and object containing it must
1367 vm_page_sleep_if_xbusy(vm_page_t m, const char *msg)
1371 vm_page_lock_assert(m, MA_NOTOWNED);
1372 VM_OBJECT_ASSERT_WLOCKED(m->object);
1375 * The page-specific object must be cached because page
1376 * identity can change during the sleep, causing the
1377 * re-lock of a different object.
1378 * It is assumed that a reference to the object is already
1379 * held by the callers.
1382 if (vm_page_xbusied(m) || (obj != NULL && obj->busy)) {
1383 vm_page_busy_sleep(m, msg, true);
1384 VM_OBJECT_WLOCK(obj);
1391 * vm_page_dirty_KBI: [ internal use only ]
1393 * Set all bits in the page's dirty field.
1395 * The object containing the specified page must be locked if the
1396 * call is made from the machine-independent layer.
1398 * See vm_page_clear_dirty_mask().
1400 * This function should only be called by vm_page_dirty().
1403 vm_page_dirty_KBI(vm_page_t m)
1406 /* Refer to this operation by its public name. */
1407 KASSERT(vm_page_all_valid(m), ("vm_page_dirty: page is invalid!"));
1408 m->dirty = VM_PAGE_BITS_ALL;
1412 * vm_page_insert: [ internal use only ]
1414 * Inserts the given mem entry into the object and object list.
1416 * The object must be locked.
1419 vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex)
1423 VM_OBJECT_ASSERT_WLOCKED(object);
1424 mpred = vm_radix_lookup_le(&object->rtree, pindex);
1425 return (vm_page_insert_after(m, object, pindex, mpred));
1429 * vm_page_insert_after:
1431 * Inserts the page "m" into the specified object at offset "pindex".
1433 * The page "mpred" must immediately precede the offset "pindex" within
1434 * the specified object.
1436 * The object must be locked.
1439 vm_page_insert_after(vm_page_t m, vm_object_t object, vm_pindex_t pindex,
1444 VM_OBJECT_ASSERT_WLOCKED(object);
1445 KASSERT(m->object == NULL,
1446 ("vm_page_insert_after: page already inserted"));
1447 if (mpred != NULL) {
1448 KASSERT(mpred->object == object,
1449 ("vm_page_insert_after: object doesn't contain mpred"));
1450 KASSERT(mpred->pindex < pindex,
1451 ("vm_page_insert_after: mpred doesn't precede pindex"));
1452 msucc = TAILQ_NEXT(mpred, listq);
1454 msucc = TAILQ_FIRST(&object->memq);
1456 KASSERT(msucc->pindex > pindex,
1457 ("vm_page_insert_after: msucc doesn't succeed pindex"));
1460 * Record the object/offset pair in this page.
1464 m->ref_count |= VPRC_OBJREF;
1467 * Now link into the object's ordered list of backed pages.
1469 if (vm_radix_insert(&object->rtree, m)) {
1472 m->ref_count &= ~VPRC_OBJREF;
1475 vm_page_insert_radixdone(m, object, mpred);
1480 * vm_page_insert_radixdone:
1482 * Complete page "m" insertion into the specified object after the
1483 * radix trie hooking.
1485 * The page "mpred" must precede the offset "m->pindex" within the
1488 * The object must be locked.
1491 vm_page_insert_radixdone(vm_page_t m, vm_object_t object, vm_page_t mpred)
1494 VM_OBJECT_ASSERT_WLOCKED(object);
1495 KASSERT(object != NULL && m->object == object,
1496 ("vm_page_insert_radixdone: page %p has inconsistent object", m));
1497 KASSERT((m->ref_count & VPRC_OBJREF) != 0,
1498 ("vm_page_insert_radixdone: page %p is missing object ref", m));
1499 if (mpred != NULL) {
1500 KASSERT(mpred->object == object,
1501 ("vm_page_insert_radixdone: object doesn't contain mpred"));
1502 KASSERT(mpred->pindex < m->pindex,
1503 ("vm_page_insert_radixdone: mpred doesn't precede pindex"));
1507 TAILQ_INSERT_AFTER(&object->memq, mpred, m, listq);
1509 TAILQ_INSERT_HEAD(&object->memq, m, listq);
1512 * Show that the object has one more resident page.
1514 object->resident_page_count++;
1517 * Hold the vnode until the last page is released.
1519 if (object->resident_page_count == 1 && object->type == OBJT_VNODE)
1520 vhold(object->handle);
1523 * Since we are inserting a new and possibly dirty page,
1524 * update the object's generation count.
1526 if (pmap_page_is_write_mapped(m))
1527 vm_object_set_writeable_dirty(object);
1531 * Do the work to remove a page from its object. The caller is responsible for
1532 * updating the page's fields to reflect this removal.
1535 vm_page_object_remove(vm_page_t m)
1541 VM_OBJECT_ASSERT_WLOCKED(object);
1542 KASSERT((m->ref_count & VPRC_OBJREF) != 0,
1543 ("page %p is missing its object ref", m));
1545 mrem = vm_radix_remove(&object->rtree, m->pindex);
1546 KASSERT(mrem == m, ("removed page %p, expected page %p", mrem, m));
1549 * Now remove from the object's list of backed pages.
1551 TAILQ_REMOVE(&object->memq, m, listq);
1554 * And show that the object has one fewer resident page.
1556 object->resident_page_count--;
1559 * The vnode may now be recycled.
1561 if (object->resident_page_count == 0 && object->type == OBJT_VNODE)
1562 vdrop(object->handle);
1568 * Removes the specified page from its containing object, but does not
1569 * invalidate any backing storage. Returns true if the object's reference
1570 * was the last reference to the page, and false otherwise.
1572 * The object must be locked.
1575 vm_page_remove(vm_page_t m)
1578 vm_page_object_remove(m);
1580 return (vm_page_drop(m, VPRC_OBJREF) == VPRC_OBJREF);
1586 * Returns the page associated with the object/offset
1587 * pair specified; if none is found, NULL is returned.
1589 * The object must be locked.
1592 vm_page_lookup(vm_object_t object, vm_pindex_t pindex)
1595 VM_OBJECT_ASSERT_LOCKED(object);
1596 return (vm_radix_lookup(&object->rtree, pindex));
1600 * vm_page_find_least:
1602 * Returns the page associated with the object with least pindex
1603 * greater than or equal to the parameter pindex, or NULL.
1605 * The object must be locked.
1608 vm_page_find_least(vm_object_t object, vm_pindex_t pindex)
1612 VM_OBJECT_ASSERT_LOCKED(object);
1613 if ((m = TAILQ_FIRST(&object->memq)) != NULL && m->pindex < pindex)
1614 m = vm_radix_lookup_ge(&object->rtree, pindex);
1619 * Returns the given page's successor (by pindex) within the object if it is
1620 * resident; if none is found, NULL is returned.
1622 * The object must be locked.
1625 vm_page_next(vm_page_t m)
1629 VM_OBJECT_ASSERT_LOCKED(m->object);
1630 if ((next = TAILQ_NEXT(m, listq)) != NULL) {
1631 MPASS(next->object == m->object);
1632 if (next->pindex != m->pindex + 1)
1639 * Returns the given page's predecessor (by pindex) within the object if it is
1640 * resident; if none is found, NULL is returned.
1642 * The object must be locked.
1645 vm_page_prev(vm_page_t m)
1649 VM_OBJECT_ASSERT_LOCKED(m->object);
1650 if ((prev = TAILQ_PREV(m, pglist, listq)) != NULL) {
1651 MPASS(prev->object == m->object);
1652 if (prev->pindex != m->pindex - 1)
1659 * Uses the page mnew as a replacement for an existing page at index
1660 * pindex which must be already present in the object.
1663 vm_page_replace(vm_page_t mnew, vm_object_t object, vm_pindex_t pindex)
1667 VM_OBJECT_ASSERT_WLOCKED(object);
1668 KASSERT(mnew->object == NULL && (mnew->ref_count & VPRC_OBJREF) == 0,
1669 ("vm_page_replace: page %p already in object", mnew));
1672 * This function mostly follows vm_page_insert() and
1673 * vm_page_remove() without the radix, object count and vnode
1674 * dance. Double check such functions for more comments.
1677 mnew->object = object;
1678 mnew->pindex = pindex;
1679 atomic_set_int(&mnew->ref_count, VPRC_OBJREF);
1680 mold = vm_radix_replace(&object->rtree, mnew);
1681 KASSERT(mold->queue == PQ_NONE,
1682 ("vm_page_replace: old page %p is on a paging queue", mold));
1684 /* Keep the resident page list in sorted order. */
1685 TAILQ_INSERT_AFTER(&object->memq, mold, mnew, listq);
1686 TAILQ_REMOVE(&object->memq, mold, listq);
1688 mold->object = NULL;
1689 atomic_clear_int(&mold->ref_count, VPRC_OBJREF);
1690 vm_page_xunbusy(mold);
1693 * The object's resident_page_count does not change because we have
1694 * swapped one page for another, but the generation count should
1695 * change if the page is dirty.
1697 if (pmap_page_is_write_mapped(mnew))
1698 vm_object_set_writeable_dirty(object);
1705 * Move the given memory entry from its
1706 * current object to the specified target object/offset.
1708 * Note: swap associated with the page must be invalidated by the move. We
1709 * have to do this for several reasons: (1) we aren't freeing the
1710 * page, (2) we are dirtying the page, (3) the VM system is probably
1711 * moving the page from object A to B, and will then later move
1712 * the backing store from A to B and we can't have a conflict.
1714 * Note: we *always* dirty the page. It is necessary both for the
1715 * fact that we moved it, and because we may be invalidating
1718 * The objects must be locked.
1721 vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex)
1726 VM_OBJECT_ASSERT_WLOCKED(new_object);
1728 KASSERT(m->ref_count != 0, ("vm_page_rename: page %p has no refs", m));
1729 mpred = vm_radix_lookup_le(&new_object->rtree, new_pindex);
1730 KASSERT(mpred == NULL || mpred->pindex != new_pindex,
1731 ("vm_page_rename: pindex already renamed"));
1734 * Create a custom version of vm_page_insert() which does not depend
1735 * by m_prev and can cheat on the implementation aspects of the
1739 m->pindex = new_pindex;
1740 if (vm_radix_insert(&new_object->rtree, m)) {
1746 * The operation cannot fail anymore. The removal must happen before
1747 * the listq iterator is tainted.
1750 vm_page_object_remove(m);
1752 /* Return back to the new pindex to complete vm_page_insert(). */
1753 m->pindex = new_pindex;
1754 m->object = new_object;
1756 vm_page_insert_radixdone(m, new_object, mpred);
1764 * Allocate and return a page that is associated with the specified
1765 * object and offset pair. By default, this page is exclusive busied.
1767 * The caller must always specify an allocation class.
1769 * allocation classes:
1770 * VM_ALLOC_NORMAL normal process request
1771 * VM_ALLOC_SYSTEM system *really* needs a page
1772 * VM_ALLOC_INTERRUPT interrupt time request
1774 * optional allocation flags:
1775 * VM_ALLOC_COUNT(number) the number of additional pages that the caller
1776 * intends to allocate
1777 * VM_ALLOC_NOBUSY do not exclusive busy the page
1778 * VM_ALLOC_NODUMP do not include the page in a kernel core dump
1779 * VM_ALLOC_NOOBJ page is not associated with an object and
1780 * should not be exclusive busy
1781 * VM_ALLOC_SBUSY shared busy the allocated page
1782 * VM_ALLOC_WIRED wire the allocated page
1783 * VM_ALLOC_ZERO prefer a zeroed page
1786 vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req)
1789 return (vm_page_alloc_after(object, pindex, req, object != NULL ?
1790 vm_radix_lookup_le(&object->rtree, pindex) : NULL));
1794 vm_page_alloc_domain(vm_object_t object, vm_pindex_t pindex, int domain,
1798 return (vm_page_alloc_domain_after(object, pindex, domain, req,
1799 object != NULL ? vm_radix_lookup_le(&object->rtree, pindex) :
1804 * Allocate a page in the specified object with the given page index. To
1805 * optimize insertion of the page into the object, the caller must also specifiy
1806 * the resident page in the object with largest index smaller than the given
1807 * page index, or NULL if no such page exists.
1810 vm_page_alloc_after(vm_object_t object, vm_pindex_t pindex,
1811 int req, vm_page_t mpred)
1813 struct vm_domainset_iter di;
1817 vm_domainset_iter_page_init(&di, object, pindex, &domain, &req);
1819 m = vm_page_alloc_domain_after(object, pindex, domain, req,
1823 } while (vm_domainset_iter_page(&di, object, &domain) == 0);
1829 * Returns true if the number of free pages exceeds the minimum
1830 * for the request class and false otherwise.
1833 vm_domain_allocate(struct vm_domain *vmd, int req, int npages)
1835 u_int limit, old, new;
1837 req = req & VM_ALLOC_CLASS_MASK;
1840 * The page daemon is allowed to dig deeper into the free page list.
1842 if (curproc == pageproc && req != VM_ALLOC_INTERRUPT)
1843 req = VM_ALLOC_SYSTEM;
1844 if (req == VM_ALLOC_INTERRUPT)
1846 else if (req == VM_ALLOC_SYSTEM)
1847 limit = vmd->vmd_interrupt_free_min;
1849 limit = vmd->vmd_free_reserved;
1852 * Attempt to reserve the pages. Fail if we're below the limit.
1855 old = vmd->vmd_free_count;
1860 } while (atomic_fcmpset_int(&vmd->vmd_free_count, &old, new) == 0);
1862 /* Wake the page daemon if we've crossed the threshold. */
1863 if (vm_paging_needed(vmd, new) && !vm_paging_needed(vmd, old))
1864 pagedaemon_wakeup(vmd->vmd_domain);
1866 /* Only update bitsets on transitions. */
1867 if ((old >= vmd->vmd_free_min && new < vmd->vmd_free_min) ||
1868 (old >= vmd->vmd_free_severe && new < vmd->vmd_free_severe))
1875 vm_page_alloc_domain_after(vm_object_t object, vm_pindex_t pindex, int domain,
1876 int req, vm_page_t mpred)
1878 struct vm_domain *vmd;
1882 KASSERT((object != NULL) == ((req & VM_ALLOC_NOOBJ) == 0) &&
1883 (object != NULL || (req & VM_ALLOC_SBUSY) == 0) &&
1884 ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) !=
1885 (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)),
1886 ("inconsistent object(%p)/req(%x)", object, req));
1887 KASSERT(object == NULL || (req & VM_ALLOC_WAITOK) == 0,
1888 ("Can't sleep and retry object insertion."));
1889 KASSERT(mpred == NULL || mpred->pindex < pindex,
1890 ("mpred %p doesn't precede pindex 0x%jx", mpred,
1891 (uintmax_t)pindex));
1893 VM_OBJECT_ASSERT_WLOCKED(object);
1897 pool = object != NULL ? VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT;
1899 #if VM_NRESERVLEVEL > 0
1901 * Can we allocate the page from a reservation?
1903 if (vm_object_reserv(object) &&
1904 (m = vm_reserv_alloc_page(object, pindex, domain, req, mpred)) !=
1906 domain = vm_phys_domain(m);
1907 vmd = VM_DOMAIN(domain);
1911 vmd = VM_DOMAIN(domain);
1912 if (vmd->vmd_pgcache[pool].zone != NULL) {
1913 m = uma_zalloc(vmd->vmd_pgcache[pool].zone, M_NOWAIT);
1915 flags |= PG_PCPU_CACHE;
1919 if (vm_domain_allocate(vmd, req, 1)) {
1921 * If not, allocate it from the free page queues.
1923 vm_domain_free_lock(vmd);
1924 m = vm_phys_alloc_pages(domain, pool, 0);
1925 vm_domain_free_unlock(vmd);
1927 vm_domain_freecnt_inc(vmd, 1);
1928 #if VM_NRESERVLEVEL > 0
1929 if (vm_reserv_reclaim_inactive(domain))
1936 * Not allocatable, give up.
1938 if (vm_domain_alloc_fail(vmd, object, req))
1944 * At this point we had better have found a good page.
1948 vm_page_alloc_check(m);
1951 * Initialize the page. Only the PG_ZERO flag is inherited.
1953 if ((req & VM_ALLOC_ZERO) != 0)
1954 flags |= (m->flags & PG_ZERO);
1955 if ((req & VM_ALLOC_NODUMP) != 0)
1959 m->oflags = object == NULL || (object->flags & OBJ_UNMANAGED) != 0 ?
1961 m->busy_lock = VPB_UNBUSIED;
1962 if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ | VM_ALLOC_SBUSY)) == 0)
1963 m->busy_lock = VPB_SINGLE_EXCLUSIVER;
1964 if ((req & VM_ALLOC_SBUSY) != 0)
1965 m->busy_lock = VPB_SHARERS_WORD(1);
1966 if (req & VM_ALLOC_WIRED) {
1968 * The page lock is not required for wiring a page until that
1969 * page is inserted into the object.
1976 if (object != NULL) {
1977 if (vm_page_insert_after(m, object, pindex, mpred)) {
1978 if (req & VM_ALLOC_WIRED) {
1982 KASSERT(m->object == NULL, ("page %p has object", m));
1983 m->oflags = VPO_UNMANAGED;
1984 m->busy_lock = VPB_UNBUSIED;
1985 /* Don't change PG_ZERO. */
1986 vm_page_free_toq(m);
1987 if (req & VM_ALLOC_WAITFAIL) {
1988 VM_OBJECT_WUNLOCK(object);
1990 VM_OBJECT_WLOCK(object);
1995 /* Ignore device objects; the pager sets "memattr" for them. */
1996 if (object->memattr != VM_MEMATTR_DEFAULT &&
1997 (object->flags & OBJ_FICTITIOUS) == 0)
1998 pmap_page_set_memattr(m, object->memattr);
2006 * vm_page_alloc_contig:
2008 * Allocate a contiguous set of physical pages of the given size "npages"
2009 * from the free lists. All of the physical pages must be at or above
2010 * the given physical address "low" and below the given physical address
2011 * "high". The given value "alignment" determines the alignment of the
2012 * first physical page in the set. If the given value "boundary" is
2013 * non-zero, then the set of physical pages cannot cross any physical
2014 * address boundary that is a multiple of that value. Both "alignment"
2015 * and "boundary" must be a power of two.
2017 * If the specified memory attribute, "memattr", is VM_MEMATTR_DEFAULT,
2018 * then the memory attribute setting for the physical pages is configured
2019 * to the object's memory attribute setting. Otherwise, the memory
2020 * attribute setting for the physical pages is configured to "memattr",
2021 * overriding the object's memory attribute setting. However, if the
2022 * object's memory attribute setting is not VM_MEMATTR_DEFAULT, then the
2023 * memory attribute setting for the physical pages cannot be configured
2024 * to VM_MEMATTR_DEFAULT.
2026 * The specified object may not contain fictitious pages.
2028 * The caller must always specify an allocation class.
2030 * allocation classes:
2031 * VM_ALLOC_NORMAL normal process request
2032 * VM_ALLOC_SYSTEM system *really* needs a page
2033 * VM_ALLOC_INTERRUPT interrupt time request
2035 * optional allocation flags:
2036 * VM_ALLOC_NOBUSY do not exclusive busy the page
2037 * VM_ALLOC_NODUMP do not include the page in a kernel core dump
2038 * VM_ALLOC_NOOBJ page is not associated with an object and
2039 * should not be exclusive busy
2040 * VM_ALLOC_SBUSY shared busy the allocated page
2041 * VM_ALLOC_WIRED wire the allocated page
2042 * VM_ALLOC_ZERO prefer a zeroed page
2045 vm_page_alloc_contig(vm_object_t object, vm_pindex_t pindex, int req,
2046 u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
2047 vm_paddr_t boundary, vm_memattr_t memattr)
2049 struct vm_domainset_iter di;
2053 vm_domainset_iter_page_init(&di, object, pindex, &domain, &req);
2055 m = vm_page_alloc_contig_domain(object, pindex, domain, req,
2056 npages, low, high, alignment, boundary, memattr);
2059 } while (vm_domainset_iter_page(&di, object, &domain) == 0);
2065 vm_page_alloc_contig_domain(vm_object_t object, vm_pindex_t pindex, int domain,
2066 int req, u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
2067 vm_paddr_t boundary, vm_memattr_t memattr)
2069 struct vm_domain *vmd;
2070 vm_page_t m, m_ret, mpred;
2071 u_int busy_lock, flags, oflags;
2073 mpred = NULL; /* XXX: pacify gcc */
2074 KASSERT((object != NULL) == ((req & VM_ALLOC_NOOBJ) == 0) &&
2075 (object != NULL || (req & VM_ALLOC_SBUSY) == 0) &&
2076 ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) !=
2077 (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)),
2078 ("vm_page_alloc_contig: inconsistent object(%p)/req(%x)", object,
2080 KASSERT(object == NULL || (req & VM_ALLOC_WAITOK) == 0,
2081 ("Can't sleep and retry object insertion."));
2082 if (object != NULL) {
2083 VM_OBJECT_ASSERT_WLOCKED(object);
2084 KASSERT((object->flags & OBJ_FICTITIOUS) == 0,
2085 ("vm_page_alloc_contig: object %p has fictitious pages",
2088 KASSERT(npages > 0, ("vm_page_alloc_contig: npages is zero"));
2090 if (object != NULL) {
2091 mpred = vm_radix_lookup_le(&object->rtree, pindex);
2092 KASSERT(mpred == NULL || mpred->pindex != pindex,
2093 ("vm_page_alloc_contig: pindex already allocated"));
2097 * Can we allocate the pages without the number of free pages falling
2098 * below the lower bound for the allocation class?
2102 #if VM_NRESERVLEVEL > 0
2104 * Can we allocate the pages from a reservation?
2106 if (vm_object_reserv(object) &&
2107 (m_ret = vm_reserv_alloc_contig(object, pindex, domain, req,
2108 mpred, npages, low, high, alignment, boundary)) != NULL) {
2109 domain = vm_phys_domain(m_ret);
2110 vmd = VM_DOMAIN(domain);
2114 vmd = VM_DOMAIN(domain);
2115 if (vm_domain_allocate(vmd, req, npages)) {
2117 * allocate them from the free page queues.
2119 vm_domain_free_lock(vmd);
2120 m_ret = vm_phys_alloc_contig(domain, npages, low, high,
2121 alignment, boundary);
2122 vm_domain_free_unlock(vmd);
2123 if (m_ret == NULL) {
2124 vm_domain_freecnt_inc(vmd, npages);
2125 #if VM_NRESERVLEVEL > 0
2126 if (vm_reserv_reclaim_contig(domain, npages, low,
2127 high, alignment, boundary))
2132 if (m_ret == NULL) {
2133 if (vm_domain_alloc_fail(vmd, object, req))
2137 #if VM_NRESERVLEVEL > 0
2140 for (m = m_ret; m < &m_ret[npages]; m++) {
2142 vm_page_alloc_check(m);
2146 * Initialize the pages. Only the PG_ZERO flag is inherited.
2149 if ((req & VM_ALLOC_ZERO) != 0)
2151 if ((req & VM_ALLOC_NODUMP) != 0)
2153 oflags = object == NULL || (object->flags & OBJ_UNMANAGED) != 0 ?
2155 busy_lock = VPB_UNBUSIED;
2156 if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ | VM_ALLOC_SBUSY)) == 0)
2157 busy_lock = VPB_SINGLE_EXCLUSIVER;
2158 if ((req & VM_ALLOC_SBUSY) != 0)
2159 busy_lock = VPB_SHARERS_WORD(1);
2160 if ((req & VM_ALLOC_WIRED) != 0)
2161 vm_wire_add(npages);
2162 if (object != NULL) {
2163 if (object->memattr != VM_MEMATTR_DEFAULT &&
2164 memattr == VM_MEMATTR_DEFAULT)
2165 memattr = object->memattr;
2167 for (m = m_ret; m < &m_ret[npages]; m++) {
2169 m->flags = (m->flags | PG_NODUMP) & flags;
2170 m->busy_lock = busy_lock;
2171 if ((req & VM_ALLOC_WIRED) != 0)
2175 if (object != NULL) {
2176 if (vm_page_insert_after(m, object, pindex, mpred)) {
2177 if ((req & VM_ALLOC_WIRED) != 0)
2178 vm_wire_sub(npages);
2179 KASSERT(m->object == NULL,
2180 ("page %p has object", m));
2182 for (m = m_ret; m < &m_ret[npages]; m++) {
2184 (req & VM_ALLOC_WIRED) != 0)
2186 m->oflags = VPO_UNMANAGED;
2187 m->busy_lock = VPB_UNBUSIED;
2188 /* Don't change PG_ZERO. */
2189 vm_page_free_toq(m);
2191 if (req & VM_ALLOC_WAITFAIL) {
2192 VM_OBJECT_WUNLOCK(object);
2194 VM_OBJECT_WLOCK(object);
2201 if (memattr != VM_MEMATTR_DEFAULT)
2202 pmap_page_set_memattr(m, memattr);
2209 * Check a page that has been freshly dequeued from a freelist.
2212 vm_page_alloc_check(vm_page_t m)
2215 KASSERT(m->object == NULL, ("page %p has object", m));
2216 KASSERT(m->queue == PQ_NONE && (m->aflags & PGA_QUEUE_STATE_MASK) == 0,
2217 ("page %p has unexpected queue %d, flags %#x",
2218 m, m->queue, (m->aflags & PGA_QUEUE_STATE_MASK)));
2219 KASSERT(m->ref_count == 0, ("page %p has references", m));
2220 KASSERT(!vm_page_busied(m), ("page %p is busy", m));
2221 KASSERT(m->dirty == 0, ("page %p is dirty", m));
2222 KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT,
2223 ("page %p has unexpected memattr %d",
2224 m, pmap_page_get_memattr(m)));
2225 KASSERT(m->valid == 0, ("free page %p is valid", m));
2229 * vm_page_alloc_freelist:
2231 * Allocate a physical page from the specified free page list.
2233 * The caller must always specify an allocation class.
2235 * allocation classes:
2236 * VM_ALLOC_NORMAL normal process request
2237 * VM_ALLOC_SYSTEM system *really* needs a page
2238 * VM_ALLOC_INTERRUPT interrupt time request
2240 * optional allocation flags:
2241 * VM_ALLOC_COUNT(number) the number of additional pages that the caller
2242 * intends to allocate
2243 * VM_ALLOC_WIRED wire the allocated page
2244 * VM_ALLOC_ZERO prefer a zeroed page
2247 vm_page_alloc_freelist(int freelist, int req)
2249 struct vm_domainset_iter di;
2253 vm_domainset_iter_page_init(&di, NULL, 0, &domain, &req);
2255 m = vm_page_alloc_freelist_domain(domain, freelist, req);
2258 } while (vm_domainset_iter_page(&di, NULL, &domain) == 0);
2264 vm_page_alloc_freelist_domain(int domain, int freelist, int req)
2266 struct vm_domain *vmd;
2271 vmd = VM_DOMAIN(domain);
2273 if (vm_domain_allocate(vmd, req, 1)) {
2274 vm_domain_free_lock(vmd);
2275 m = vm_phys_alloc_freelist_pages(domain, freelist,
2276 VM_FREEPOOL_DIRECT, 0);
2277 vm_domain_free_unlock(vmd);
2279 vm_domain_freecnt_inc(vmd, 1);
2282 if (vm_domain_alloc_fail(vmd, NULL, req))
2287 vm_page_alloc_check(m);
2290 * Initialize the page. Only the PG_ZERO flag is inherited.
2294 if ((req & VM_ALLOC_ZERO) != 0)
2297 if ((req & VM_ALLOC_WIRED) != 0) {
2299 * The page lock is not required for wiring a page that does
2300 * not belong to an object.
2305 /* Unmanaged pages don't use "act_count". */
2306 m->oflags = VPO_UNMANAGED;
2311 vm_page_zone_import(void *arg, void **store, int cnt, int domain, int flags)
2313 struct vm_domain *vmd;
2314 struct vm_pgcache *pgcache;
2318 vmd = VM_DOMAIN(pgcache->domain);
2319 /* Only import if we can bring in a full bucket. */
2320 if (cnt == 1 || !vm_domain_allocate(vmd, VM_ALLOC_NORMAL, cnt))
2322 domain = vmd->vmd_domain;
2323 vm_domain_free_lock(vmd);
2324 i = vm_phys_alloc_npages(domain, pgcache->pool, cnt,
2325 (vm_page_t *)store);
2326 vm_domain_free_unlock(vmd);
2328 vm_domain_freecnt_inc(vmd, cnt - i);
2334 vm_page_zone_release(void *arg, void **store, int cnt)
2336 struct vm_domain *vmd;
2337 struct vm_pgcache *pgcache;
2342 vmd = VM_DOMAIN(pgcache->domain);
2343 vm_domain_free_lock(vmd);
2344 for (i = 0; i < cnt; i++) {
2345 m = (vm_page_t)store[i];
2346 vm_phys_free_pages(m, 0);
2348 vm_domain_free_unlock(vmd);
2349 vm_domain_freecnt_inc(vmd, cnt);
2352 #define VPSC_ANY 0 /* No restrictions. */
2353 #define VPSC_NORESERV 1 /* Skip reservations; implies VPSC_NOSUPER. */
2354 #define VPSC_NOSUPER 2 /* Skip superpages. */
2357 * vm_page_scan_contig:
2359 * Scan vm_page_array[] between the specified entries "m_start" and
2360 * "m_end" for a run of contiguous physical pages that satisfy the
2361 * specified conditions, and return the lowest page in the run. The
2362 * specified "alignment" determines the alignment of the lowest physical
2363 * page in the run. If the specified "boundary" is non-zero, then the
2364 * run of physical pages cannot span a physical address that is a
2365 * multiple of "boundary".
2367 * "m_end" is never dereferenced, so it need not point to a vm_page
2368 * structure within vm_page_array[].
2370 * "npages" must be greater than zero. "m_start" and "m_end" must not
2371 * span a hole (or discontiguity) in the physical address space. Both
2372 * "alignment" and "boundary" must be a power of two.
2375 vm_page_scan_contig(u_long npages, vm_page_t m_start, vm_page_t m_end,
2376 u_long alignment, vm_paddr_t boundary, int options)
2382 #if VM_NRESERVLEVEL > 0
2385 int m_inc, order, run_ext, run_len;
2387 KASSERT(npages > 0, ("npages is 0"));
2388 KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
2389 KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
2393 for (m = m_start; m < m_end && run_len < npages; m += m_inc) {
2394 KASSERT((m->flags & PG_MARKER) == 0,
2395 ("page %p is PG_MARKER", m));
2396 KASSERT((m->flags & PG_FICTITIOUS) == 0 || m->ref_count >= 1,
2397 ("fictitious page %p has invalid ref count", m));
2400 * If the current page would be the start of a run, check its
2401 * physical address against the end, alignment, and boundary
2402 * conditions. If it doesn't satisfy these conditions, either
2403 * terminate the scan or advance to the next page that
2404 * satisfies the failed condition.
2407 KASSERT(m_run == NULL, ("m_run != NULL"));
2408 if (m + npages > m_end)
2410 pa = VM_PAGE_TO_PHYS(m);
2411 if ((pa & (alignment - 1)) != 0) {
2412 m_inc = atop(roundup2(pa, alignment) - pa);
2415 if (rounddown2(pa ^ (pa + ptoa(npages) - 1),
2417 m_inc = atop(roundup2(pa, boundary) - pa);
2421 KASSERT(m_run != NULL, ("m_run == NULL"));
2423 vm_page_change_lock(m, &m_mtx);
2426 if (vm_page_wired(m))
2428 #if VM_NRESERVLEVEL > 0
2429 else if ((level = vm_reserv_level(m)) >= 0 &&
2430 (options & VPSC_NORESERV) != 0) {
2432 /* Advance to the end of the reservation. */
2433 pa = VM_PAGE_TO_PHYS(m);
2434 m_inc = atop(roundup2(pa + 1, vm_reserv_size(level)) -
2438 else if ((object = m->object) != NULL) {
2440 * The page is considered eligible for relocation if
2441 * and only if it could be laundered or reclaimed by
2444 if (!VM_OBJECT_TRYRLOCK(object)) {
2446 VM_OBJECT_RLOCK(object);
2448 if (m->object != object) {
2450 * The page may have been freed.
2452 VM_OBJECT_RUNLOCK(object);
2456 /* Don't care: PG_NODUMP, PG_ZERO. */
2457 if (object->type != OBJT_DEFAULT &&
2458 object->type != OBJT_SWAP &&
2459 object->type != OBJT_VNODE) {
2461 #if VM_NRESERVLEVEL > 0
2462 } else if ((options & VPSC_NOSUPER) != 0 &&
2463 (level = vm_reserv_level_iffullpop(m)) >= 0) {
2465 /* Advance to the end of the superpage. */
2466 pa = VM_PAGE_TO_PHYS(m);
2467 m_inc = atop(roundup2(pa + 1,
2468 vm_reserv_size(level)) - pa);
2470 } else if (object->memattr == VM_MEMATTR_DEFAULT &&
2471 vm_page_queue(m) != PQ_NONE && !vm_page_busied(m) &&
2472 !vm_page_wired(m)) {
2474 * The page is allocated but eligible for
2475 * relocation. Extend the current run by one
2478 KASSERT(pmap_page_get_memattr(m) ==
2480 ("page %p has an unexpected memattr", m));
2481 KASSERT((m->oflags & (VPO_SWAPINPROG |
2482 VPO_SWAPSLEEP | VPO_UNMANAGED)) == 0,
2483 ("page %p has unexpected oflags", m));
2484 /* Don't care: PGA_NOSYNC. */
2488 VM_OBJECT_RUNLOCK(object);
2489 #if VM_NRESERVLEVEL > 0
2490 } else if (level >= 0) {
2492 * The page is reserved but not yet allocated. In
2493 * other words, it is still free. Extend the current
2498 } else if ((order = m->order) < VM_NFREEORDER) {
2500 * The page is enqueued in the physical memory
2501 * allocator's free page queues. Moreover, it is the
2502 * first page in a power-of-two-sized run of
2503 * contiguous free pages. Add these pages to the end
2504 * of the current run, and jump ahead.
2506 run_ext = 1 << order;
2510 * Skip the page for one of the following reasons: (1)
2511 * It is enqueued in the physical memory allocator's
2512 * free page queues. However, it is not the first
2513 * page in a run of contiguous free pages. (This case
2514 * rarely occurs because the scan is performed in
2515 * ascending order.) (2) It is not reserved, and it is
2516 * transitioning from free to allocated. (Conversely,
2517 * the transition from allocated to free for managed
2518 * pages is blocked by the page lock.) (3) It is
2519 * allocated but not contained by an object and not
2520 * wired, e.g., allocated by Xen's balloon driver.
2526 * Extend or reset the current run of pages.
2541 if (run_len >= npages)
2547 * vm_page_reclaim_run:
2549 * Try to relocate each of the allocated virtual pages within the
2550 * specified run of physical pages to a new physical address. Free the
2551 * physical pages underlying the relocated virtual pages. A virtual page
2552 * is relocatable if and only if it could be laundered or reclaimed by
2553 * the page daemon. Whenever possible, a virtual page is relocated to a
2554 * physical address above "high".
2556 * Returns 0 if every physical page within the run was already free or
2557 * just freed by a successful relocation. Otherwise, returns a non-zero
2558 * value indicating why the last attempt to relocate a virtual page was
2561 * "req_class" must be an allocation class.
2564 vm_page_reclaim_run(int req_class, int domain, u_long npages, vm_page_t m_run,
2567 struct vm_domain *vmd;
2569 struct spglist free;
2572 vm_page_t m, m_end, m_new;
2573 int error, order, req;
2575 KASSERT((req_class & VM_ALLOC_CLASS_MASK) == req_class,
2576 ("req_class is not an allocation class"));
2580 m_end = m_run + npages;
2582 for (; error == 0 && m < m_end; m++) {
2583 KASSERT((m->flags & (PG_FICTITIOUS | PG_MARKER)) == 0,
2584 ("page %p is PG_FICTITIOUS or PG_MARKER", m));
2587 * Avoid releasing and reacquiring the same page lock.
2589 vm_page_change_lock(m, &m_mtx);
2592 * Racily check for wirings. Races are handled below.
2594 if (vm_page_wired(m))
2596 else if ((object = m->object) != NULL) {
2598 * The page is relocated if and only if it could be
2599 * laundered or reclaimed by the page daemon.
2601 if (!VM_OBJECT_TRYWLOCK(object)) {
2603 VM_OBJECT_WLOCK(object);
2605 if (m->object != object) {
2607 * The page may have been freed.
2609 VM_OBJECT_WUNLOCK(object);
2613 /* Don't care: PG_NODUMP, PG_ZERO. */
2614 if (object->type != OBJT_DEFAULT &&
2615 object->type != OBJT_SWAP &&
2616 object->type != OBJT_VNODE)
2618 else if (object->memattr != VM_MEMATTR_DEFAULT)
2620 else if (vm_page_queue(m) != PQ_NONE &&
2621 vm_page_tryxbusy(m) != 0) {
2622 if (vm_page_wired(m)) {
2627 KASSERT(pmap_page_get_memattr(m) ==
2629 ("page %p has an unexpected memattr", m));
2630 KASSERT((m->oflags & (VPO_SWAPINPROG |
2631 VPO_SWAPSLEEP | VPO_UNMANAGED)) == 0,
2632 ("page %p has unexpected oflags", m));
2633 /* Don't care: PGA_NOSYNC. */
2634 if (!vm_page_none_valid(m)) {
2636 * First, try to allocate a new page
2637 * that is above "high". Failing
2638 * that, try to allocate a new page
2639 * that is below "m_run". Allocate
2640 * the new page between the end of
2641 * "m_run" and "high" only as a last
2644 req = req_class | VM_ALLOC_NOOBJ;
2645 if ((m->flags & PG_NODUMP) != 0)
2646 req |= VM_ALLOC_NODUMP;
2647 if (trunc_page(high) !=
2648 ~(vm_paddr_t)PAGE_MASK) {
2649 m_new = vm_page_alloc_contig(
2654 VM_MEMATTR_DEFAULT);
2657 if (m_new == NULL) {
2658 pa = VM_PAGE_TO_PHYS(m_run);
2659 m_new = vm_page_alloc_contig(
2661 0, pa - 1, PAGE_SIZE, 0,
2662 VM_MEMATTR_DEFAULT);
2664 if (m_new == NULL) {
2666 m_new = vm_page_alloc_contig(
2668 pa, high, PAGE_SIZE, 0,
2669 VM_MEMATTR_DEFAULT);
2671 if (m_new == NULL) {
2678 * Unmap the page and check for new
2679 * wirings that may have been acquired
2680 * through a pmap lookup.
2682 if (object->ref_count != 0 &&
2683 !vm_page_try_remove_all(m)) {
2684 vm_page_free(m_new);
2690 * Replace "m" with the new page. For
2691 * vm_page_replace(), "m" must be busy
2692 * and dequeued. Finally, change "m"
2693 * as if vm_page_free() was called.
2695 m_new->aflags = m->aflags &
2696 ~PGA_QUEUE_STATE_MASK;
2697 KASSERT(m_new->oflags == VPO_UNMANAGED,
2698 ("page %p is managed", m_new));
2699 pmap_copy_page(m, m_new);
2700 m_new->valid = m->valid;
2701 m_new->dirty = m->dirty;
2702 m->flags &= ~PG_ZERO;
2704 vm_page_replace_checked(m_new, object,
2706 if (vm_page_free_prep(m))
2707 SLIST_INSERT_HEAD(&free, m,
2711 * The new page must be deactivated
2712 * before the object is unlocked.
2714 vm_page_change_lock(m_new, &m_mtx);
2715 vm_page_deactivate(m_new);
2717 m->flags &= ~PG_ZERO;
2719 if (vm_page_free_prep(m))
2720 SLIST_INSERT_HEAD(&free, m,
2722 KASSERT(m->dirty == 0,
2723 ("page %p is dirty", m));
2728 VM_OBJECT_WUNLOCK(object);
2730 MPASS(vm_phys_domain(m) == domain);
2731 vmd = VM_DOMAIN(domain);
2732 vm_domain_free_lock(vmd);
2734 if (order < VM_NFREEORDER) {
2736 * The page is enqueued in the physical memory
2737 * allocator's free page queues. Moreover, it
2738 * is the first page in a power-of-two-sized
2739 * run of contiguous free pages. Jump ahead
2740 * to the last page within that run, and
2741 * continue from there.
2743 m += (1 << order) - 1;
2745 #if VM_NRESERVLEVEL > 0
2746 else if (vm_reserv_is_page_free(m))
2749 vm_domain_free_unlock(vmd);
2750 if (order == VM_NFREEORDER)
2756 if ((m = SLIST_FIRST(&free)) != NULL) {
2759 vmd = VM_DOMAIN(domain);
2761 vm_domain_free_lock(vmd);
2763 MPASS(vm_phys_domain(m) == domain);
2764 SLIST_REMOVE_HEAD(&free, plinks.s.ss);
2765 vm_phys_free_pages(m, 0);
2767 } while ((m = SLIST_FIRST(&free)) != NULL);
2768 vm_domain_free_unlock(vmd);
2769 vm_domain_freecnt_inc(vmd, cnt);
2776 CTASSERT(powerof2(NRUNS));
2778 #define RUN_INDEX(count) ((count) & (NRUNS - 1))
2780 #define MIN_RECLAIM 8
2783 * vm_page_reclaim_contig:
2785 * Reclaim allocated, contiguous physical memory satisfying the specified
2786 * conditions by relocating the virtual pages using that physical memory.
2787 * Returns true if reclamation is successful and false otherwise. Since
2788 * relocation requires the allocation of physical pages, reclamation may
2789 * fail due to a shortage of free pages. When reclamation fails, callers
2790 * are expected to perform vm_wait() before retrying a failed allocation
2791 * operation, e.g., vm_page_alloc_contig().
2793 * The caller must always specify an allocation class through "req".
2795 * allocation classes:
2796 * VM_ALLOC_NORMAL normal process request
2797 * VM_ALLOC_SYSTEM system *really* needs a page
2798 * VM_ALLOC_INTERRUPT interrupt time request
2800 * The optional allocation flags are ignored.
2802 * "npages" must be greater than zero. Both "alignment" and "boundary"
2803 * must be a power of two.
2806 vm_page_reclaim_contig_domain(int domain, int req, u_long npages,
2807 vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
2809 struct vm_domain *vmd;
2810 vm_paddr_t curr_low;
2811 vm_page_t m_run, m_runs[NRUNS];
2812 u_long count, reclaimed;
2813 int error, i, options, req_class;
2815 KASSERT(npages > 0, ("npages is 0"));
2816 KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
2817 KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
2818 req_class = req & VM_ALLOC_CLASS_MASK;
2821 * The page daemon is allowed to dig deeper into the free page list.
2823 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
2824 req_class = VM_ALLOC_SYSTEM;
2827 * Return if the number of free pages cannot satisfy the requested
2830 vmd = VM_DOMAIN(domain);
2831 count = vmd->vmd_free_count;
2832 if (count < npages + vmd->vmd_free_reserved || (count < npages +
2833 vmd->vmd_interrupt_free_min && req_class == VM_ALLOC_SYSTEM) ||
2834 (count < npages && req_class == VM_ALLOC_INTERRUPT))
2838 * Scan up to three times, relaxing the restrictions ("options") on
2839 * the reclamation of reservations and superpages each time.
2841 for (options = VPSC_NORESERV;;) {
2843 * Find the highest runs that satisfy the given constraints
2844 * and restrictions, and record them in "m_runs".
2849 m_run = vm_phys_scan_contig(domain, npages, curr_low,
2850 high, alignment, boundary, options);
2853 curr_low = VM_PAGE_TO_PHYS(m_run) + ptoa(npages);
2854 m_runs[RUN_INDEX(count)] = m_run;
2859 * Reclaim the highest runs in LIFO (descending) order until
2860 * the number of reclaimed pages, "reclaimed", is at least
2861 * MIN_RECLAIM. Reset "reclaimed" each time because each
2862 * reclamation is idempotent, and runs will (likely) recur
2863 * from one scan to the next as restrictions are relaxed.
2866 for (i = 0; count > 0 && i < NRUNS; i++) {
2868 m_run = m_runs[RUN_INDEX(count)];
2869 error = vm_page_reclaim_run(req_class, domain, npages,
2872 reclaimed += npages;
2873 if (reclaimed >= MIN_RECLAIM)
2879 * Either relax the restrictions on the next scan or return if
2880 * the last scan had no restrictions.
2882 if (options == VPSC_NORESERV)
2883 options = VPSC_NOSUPER;
2884 else if (options == VPSC_NOSUPER)
2886 else if (options == VPSC_ANY)
2887 return (reclaimed != 0);
2892 vm_page_reclaim_contig(int req, u_long npages, vm_paddr_t low, vm_paddr_t high,
2893 u_long alignment, vm_paddr_t boundary)
2895 struct vm_domainset_iter di;
2899 vm_domainset_iter_page_init(&di, NULL, 0, &domain, &req);
2901 ret = vm_page_reclaim_contig_domain(domain, req, npages, low,
2902 high, alignment, boundary);
2905 } while (vm_domainset_iter_page(&di, NULL, &domain) == 0);
2911 * Set the domain in the appropriate page level domainset.
2914 vm_domain_set(struct vm_domain *vmd)
2917 mtx_lock(&vm_domainset_lock);
2918 if (!vmd->vmd_minset && vm_paging_min(vmd)) {
2919 vmd->vmd_minset = 1;
2920 DOMAINSET_SET(vmd->vmd_domain, &vm_min_domains);
2922 if (!vmd->vmd_severeset && vm_paging_severe(vmd)) {
2923 vmd->vmd_severeset = 1;
2924 DOMAINSET_SET(vmd->vmd_domain, &vm_severe_domains);
2926 mtx_unlock(&vm_domainset_lock);
2930 * Clear the domain from the appropriate page level domainset.
2933 vm_domain_clear(struct vm_domain *vmd)
2936 mtx_lock(&vm_domainset_lock);
2937 if (vmd->vmd_minset && !vm_paging_min(vmd)) {
2938 vmd->vmd_minset = 0;
2939 DOMAINSET_CLR(vmd->vmd_domain, &vm_min_domains);
2940 if (vm_min_waiters != 0) {
2942 wakeup(&vm_min_domains);
2945 if (vmd->vmd_severeset && !vm_paging_severe(vmd)) {
2946 vmd->vmd_severeset = 0;
2947 DOMAINSET_CLR(vmd->vmd_domain, &vm_severe_domains);
2948 if (vm_severe_waiters != 0) {
2949 vm_severe_waiters = 0;
2950 wakeup(&vm_severe_domains);
2955 * If pageout daemon needs pages, then tell it that there are
2958 if (vmd->vmd_pageout_pages_needed &&
2959 vmd->vmd_free_count >= vmd->vmd_pageout_free_min) {
2960 wakeup(&vmd->vmd_pageout_pages_needed);
2961 vmd->vmd_pageout_pages_needed = 0;
2964 /* See comments in vm_wait_doms(). */
2965 if (vm_pageproc_waiters) {
2966 vm_pageproc_waiters = 0;
2967 wakeup(&vm_pageproc_waiters);
2969 mtx_unlock(&vm_domainset_lock);
2973 * Wait for free pages to exceed the min threshold globally.
2979 mtx_lock(&vm_domainset_lock);
2980 while (vm_page_count_min()) {
2982 msleep(&vm_min_domains, &vm_domainset_lock, PVM, "vmwait", 0);
2984 mtx_unlock(&vm_domainset_lock);
2988 * Wait for free pages to exceed the severe threshold globally.
2991 vm_wait_severe(void)
2994 mtx_lock(&vm_domainset_lock);
2995 while (vm_page_count_severe()) {
2996 vm_severe_waiters++;
2997 msleep(&vm_severe_domains, &vm_domainset_lock, PVM,
3000 mtx_unlock(&vm_domainset_lock);
3007 return (vm_severe_waiters + vm_min_waiters + vm_pageproc_waiters);
3011 vm_wait_doms(const domainset_t *wdoms)
3015 * We use racey wakeup synchronization to avoid expensive global
3016 * locking for the pageproc when sleeping with a non-specific vm_wait.
3017 * To handle this, we only sleep for one tick in this instance. It
3018 * is expected that most allocations for the pageproc will come from
3019 * kmem or vm_page_grab* which will use the more specific and
3020 * race-free vm_wait_domain().
3022 if (curproc == pageproc) {
3023 mtx_lock(&vm_domainset_lock);
3024 vm_pageproc_waiters++;
3025 msleep(&vm_pageproc_waiters, &vm_domainset_lock, PVM | PDROP,
3029 * XXX Ideally we would wait only until the allocation could
3030 * be satisfied. This condition can cause new allocators to
3031 * consume all freed pages while old allocators wait.
3033 mtx_lock(&vm_domainset_lock);
3034 if (vm_page_count_min_set(wdoms)) {
3036 msleep(&vm_min_domains, &vm_domainset_lock,
3037 PVM | PDROP, "vmwait", 0);
3039 mtx_unlock(&vm_domainset_lock);
3046 * Sleep until free pages are available for allocation.
3047 * - Called in various places after failed memory allocations.
3050 vm_wait_domain(int domain)
3052 struct vm_domain *vmd;
3055 vmd = VM_DOMAIN(domain);
3056 vm_domain_free_assert_unlocked(vmd);
3058 if (curproc == pageproc) {
3059 mtx_lock(&vm_domainset_lock);
3060 if (vmd->vmd_free_count < vmd->vmd_pageout_free_min) {
3061 vmd->vmd_pageout_pages_needed = 1;
3062 msleep(&vmd->vmd_pageout_pages_needed,
3063 &vm_domainset_lock, PDROP | PSWP, "VMWait", 0);
3065 mtx_unlock(&vm_domainset_lock);
3067 if (pageproc == NULL)
3068 panic("vm_wait in early boot");
3069 DOMAINSET_ZERO(&wdom);
3070 DOMAINSET_SET(vmd->vmd_domain, &wdom);
3071 vm_wait_doms(&wdom);
3078 * Sleep until free pages are available for allocation in the
3079 * affinity domains of the obj. If obj is NULL, the domain set
3080 * for the calling thread is used.
3081 * Called in various places after failed memory allocations.
3084 vm_wait(vm_object_t obj)
3086 struct domainset *d;
3091 * Carefully fetch pointers only once: the struct domainset
3092 * itself is ummutable but the pointer might change.
3095 d = obj->domain.dr_policy;
3097 d = curthread->td_domain.dr_policy;
3099 vm_wait_doms(&d->ds_mask);
3103 * vm_domain_alloc_fail:
3105 * Called when a page allocation function fails. Informs the
3106 * pagedaemon and performs the requested wait. Requires the
3107 * domain_free and object lock on entry. Returns with the
3108 * object lock held and free lock released. Returns an error when
3109 * retry is necessary.
3113 vm_domain_alloc_fail(struct vm_domain *vmd, vm_object_t object, int req)
3116 vm_domain_free_assert_unlocked(vmd);
3118 atomic_add_int(&vmd->vmd_pageout_deficit,
3119 max((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1));
3120 if (req & (VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL)) {
3122 VM_OBJECT_WUNLOCK(object);
3123 vm_wait_domain(vmd->vmd_domain);
3125 VM_OBJECT_WLOCK(object);
3126 if (req & VM_ALLOC_WAITOK)
3136 * Sleep until free pages are available for allocation.
3137 * - Called only in vm_fault so that processes page faulting
3138 * can be easily tracked.
3139 * - Sleeps at a lower priority than vm_wait() so that vm_wait()ing
3140 * processes will be able to grab memory first. Do not change
3141 * this balance without careful testing first.
3144 vm_waitpfault(struct domainset *dset, int timo)
3148 * XXX Ideally we would wait only until the allocation could
3149 * be satisfied. This condition can cause new allocators to
3150 * consume all freed pages while old allocators wait.
3152 mtx_lock(&vm_domainset_lock);
3153 if (vm_page_count_min_set(&dset->ds_mask)) {
3155 msleep(&vm_min_domains, &vm_domainset_lock, PUSER | PDROP,
3158 mtx_unlock(&vm_domainset_lock);
3161 static struct vm_pagequeue *
3162 vm_page_pagequeue(vm_page_t m)
3167 if ((queue = atomic_load_8(&m->queue)) == PQ_NONE)
3169 return (&vm_pagequeue_domain(m)->vmd_pagequeues[queue]);
3173 vm_pqbatch_process_page(struct vm_pagequeue *pq, vm_page_t m)
3175 struct vm_domain *vmd;
3178 CRITICAL_ASSERT(curthread);
3179 vm_pagequeue_assert_locked(pq);
3182 * The page daemon is allowed to set m->queue = PQ_NONE without
3183 * the page queue lock held. In this case it is about to free the page,
3184 * which must not have any queue state.
3186 qflags = atomic_load_8(&m->aflags);
3187 KASSERT(pq == vm_page_pagequeue(m) ||
3188 (qflags & PGA_QUEUE_STATE_MASK) == 0,
3189 ("page %p doesn't belong to queue %p but has aflags %#x",
3192 if ((qflags & PGA_DEQUEUE) != 0) {
3193 if (__predict_true((qflags & PGA_ENQUEUED) != 0))
3194 vm_pagequeue_remove(pq, m);
3195 vm_page_dequeue_complete(m);
3196 counter_u64_add(queue_ops, 1);
3197 } else if ((qflags & (PGA_REQUEUE | PGA_REQUEUE_HEAD)) != 0) {
3198 if ((qflags & PGA_ENQUEUED) != 0)
3199 TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
3201 vm_pagequeue_cnt_inc(pq);
3202 vm_page_aflag_set(m, PGA_ENQUEUED);
3206 * Give PGA_REQUEUE_HEAD precedence over PGA_REQUEUE.
3207 * In particular, if both flags are set in close succession,
3208 * only PGA_REQUEUE_HEAD will be applied, even if it was set
3211 if ((qflags & PGA_REQUEUE_HEAD) != 0) {
3212 KASSERT(m->queue == PQ_INACTIVE,
3213 ("head enqueue not supported for page %p", m));
3214 vmd = vm_pagequeue_domain(m);
3215 TAILQ_INSERT_BEFORE(&vmd->vmd_inacthead, m, plinks.q);
3217 TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
3219 vm_page_aflag_clear(m, qflags & (PGA_REQUEUE |
3221 counter_u64_add(queue_ops, 1);
3223 counter_u64_add(queue_nops, 1);
3228 vm_pqbatch_process(struct vm_pagequeue *pq, struct vm_batchqueue *bq,
3234 for (i = 0; i < bq->bq_cnt; i++) {
3236 if (__predict_false(m->queue != queue))
3238 vm_pqbatch_process_page(pq, m);
3240 vm_batchqueue_init(bq);
3244 * vm_page_pqbatch_submit: [ internal use only ]
3246 * Enqueue a page in the specified page queue's batched work queue.
3247 * The caller must have encoded the requested operation in the page
3248 * structure's aflags field.
3251 vm_page_pqbatch_submit(vm_page_t m, uint8_t queue)
3253 struct vm_batchqueue *bq;
3254 struct vm_pagequeue *pq;
3257 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3258 ("page %p is unmanaged", m));
3259 KASSERT(mtx_owned(vm_page_lockptr(m)) || m->object == NULL,
3260 ("missing synchronization for page %p", m));
3261 KASSERT(queue < PQ_COUNT, ("invalid queue %d", queue));
3263 domain = vm_phys_domain(m);
3264 pq = &vm_pagequeue_domain(m)->vmd_pagequeues[queue];
3267 bq = DPCPU_PTR(pqbatch[domain][queue]);
3268 if (vm_batchqueue_insert(bq, m)) {
3273 vm_pagequeue_lock(pq);
3275 bq = DPCPU_PTR(pqbatch[domain][queue]);
3276 vm_pqbatch_process(pq, bq, queue);
3279 * The page may have been logically dequeued before we acquired the
3280 * page queue lock. In this case, since we either hold the page lock
3281 * or the page is being freed, a different thread cannot be concurrently
3282 * enqueuing the page.
3284 if (__predict_true(m->queue == queue))
3285 vm_pqbatch_process_page(pq, m);
3287 KASSERT(m->queue == PQ_NONE,
3288 ("invalid queue transition for page %p", m));
3289 KASSERT((m->aflags & PGA_ENQUEUED) == 0,
3290 ("page %p is enqueued with invalid queue index", m));
3292 vm_pagequeue_unlock(pq);
3297 * vm_page_pqbatch_drain: [ internal use only ]
3299 * Force all per-CPU page queue batch queues to be drained. This is
3300 * intended for use in severe memory shortages, to ensure that pages
3301 * do not remain stuck in the batch queues.
3304 vm_page_pqbatch_drain(void)
3307 struct vm_domain *vmd;
3308 struct vm_pagequeue *pq;
3309 int cpu, domain, queue;
3314 sched_bind(td, cpu);
3317 for (domain = 0; domain < vm_ndomains; domain++) {
3318 vmd = VM_DOMAIN(domain);
3319 for (queue = 0; queue < PQ_COUNT; queue++) {
3320 pq = &vmd->vmd_pagequeues[queue];
3321 vm_pagequeue_lock(pq);
3323 vm_pqbatch_process(pq,
3324 DPCPU_PTR(pqbatch[domain][queue]), queue);
3326 vm_pagequeue_unlock(pq);
3336 * Complete the logical removal of a page from a page queue. We must be
3337 * careful to synchronize with the page daemon, which may be concurrently
3338 * examining the page with only the page lock held. The page must not be
3339 * in a state where it appears to be logically enqueued.
3342 vm_page_dequeue_complete(vm_page_t m)
3346 atomic_thread_fence_rel();
3347 vm_page_aflag_clear(m, PGA_QUEUE_STATE_MASK);
3351 * vm_page_dequeue_deferred: [ internal use only ]
3353 * Request removal of the given page from its current page
3354 * queue. Physical removal from the queue may be deferred
3357 * The page must be locked.
3360 vm_page_dequeue_deferred(vm_page_t m)
3364 vm_page_assert_locked(m);
3366 if ((queue = vm_page_queue(m)) == PQ_NONE)
3370 * Set PGA_DEQUEUE if it is not already set to handle a concurrent call
3371 * to vm_page_dequeue_deferred_free(). In particular, avoid modifying
3372 * the page's queue state once vm_page_dequeue_deferred_free() has been
3373 * called. In the event of a race, two batch queue entries for the page
3374 * will be created, but the second will have no effect.
3376 if (vm_page_pqstate_cmpset(m, queue, queue, PGA_DEQUEUE, PGA_DEQUEUE))
3377 vm_page_pqbatch_submit(m, queue);
3381 * A variant of vm_page_dequeue_deferred() that does not assert the page
3382 * lock and is only to be called from vm_page_free_prep(). Because the
3383 * page is being freed, we can assume that nothing other than the page
3384 * daemon is scheduling queue operations on this page, so we get for
3385 * free the mutual exclusion that is otherwise provided by the page lock.
3386 * To handle races, the page daemon must take care to atomically check
3387 * for PGA_DEQUEUE when updating queue state.
3390 vm_page_dequeue_deferred_free(vm_page_t m)
3394 KASSERT(m->ref_count == 0, ("page %p has references", m));
3397 if ((m->aflags & PGA_DEQUEUE) != 0)
3399 atomic_thread_fence_acq();
3400 if ((queue = atomic_load_8(&m->queue)) == PQ_NONE)
3402 if (vm_page_pqstate_cmpset(m, queue, queue, PGA_DEQUEUE,
3404 vm_page_pqbatch_submit(m, queue);
3413 * Remove the page from whichever page queue it's in, if any.
3414 * The page must either be locked or unallocated. This constraint
3415 * ensures that the queue state of the page will remain consistent
3416 * after this function returns.
3419 vm_page_dequeue(vm_page_t m)
3421 struct vm_pagequeue *pq, *pq1;
3424 KASSERT(mtx_owned(vm_page_lockptr(m)) || m->ref_count == 0,
3425 ("page %p is allocated and unlocked", m));
3427 for (pq = vm_page_pagequeue(m);; pq = pq1) {
3430 * A thread may be concurrently executing
3431 * vm_page_dequeue_complete(). Ensure that all queue
3432 * state is cleared before we return.
3434 aflags = atomic_load_8(&m->aflags);
3435 if ((aflags & PGA_QUEUE_STATE_MASK) == 0)
3437 KASSERT((aflags & PGA_DEQUEUE) != 0,
3438 ("page %p has unexpected queue state flags %#x",
3442 * Busy wait until the thread updating queue state is
3443 * finished. Such a thread must be executing in a
3447 pq1 = vm_page_pagequeue(m);
3450 vm_pagequeue_lock(pq);
3451 if ((pq1 = vm_page_pagequeue(m)) == pq)
3453 vm_pagequeue_unlock(pq);
3455 KASSERT(pq == vm_page_pagequeue(m),
3456 ("%s: page %p migrated directly between queues", __func__, m));
3457 KASSERT((m->aflags & PGA_DEQUEUE) != 0 ||
3458 mtx_owned(vm_page_lockptr(m)),
3459 ("%s: queued unlocked page %p", __func__, m));
3461 if ((m->aflags & PGA_ENQUEUED) != 0)
3462 vm_pagequeue_remove(pq, m);
3463 vm_page_dequeue_complete(m);
3464 vm_pagequeue_unlock(pq);
3468 * Schedule the given page for insertion into the specified page queue.
3469 * Physical insertion of the page may be deferred indefinitely.
3472 vm_page_enqueue(vm_page_t m, uint8_t queue)
3475 vm_page_assert_locked(m);
3476 KASSERT(m->queue == PQ_NONE && (m->aflags & PGA_QUEUE_STATE_MASK) == 0,
3477 ("%s: page %p is already enqueued", __func__, m));
3478 KASSERT(m->ref_count > 0,
3479 ("%s: page %p does not carry any references", __func__, m));
3482 if ((m->aflags & PGA_REQUEUE) == 0)
3483 vm_page_aflag_set(m, PGA_REQUEUE);
3484 vm_page_pqbatch_submit(m, queue);
3488 * vm_page_requeue: [ internal use only ]
3490 * Schedule a requeue of the given page.
3492 * The page must be locked.
3495 vm_page_requeue(vm_page_t m)
3498 vm_page_assert_locked(m);
3499 KASSERT(vm_page_queue(m) != PQ_NONE,
3500 ("%s: page %p is not logically enqueued", __func__, m));
3501 KASSERT(m->ref_count > 0,
3502 ("%s: page %p does not carry any references", __func__, m));
3504 if ((m->aflags & PGA_REQUEUE) == 0)
3505 vm_page_aflag_set(m, PGA_REQUEUE);
3506 vm_page_pqbatch_submit(m, atomic_load_8(&m->queue));
3510 * vm_page_swapqueue: [ internal use only ]
3512 * Move the page from one queue to another, or to the tail of its
3513 * current queue, in the face of a possible concurrent call to
3514 * vm_page_dequeue_deferred_free().
3517 vm_page_swapqueue(vm_page_t m, uint8_t oldq, uint8_t newq)
3519 struct vm_pagequeue *pq;
3523 KASSERT(oldq < PQ_COUNT && newq < PQ_COUNT && oldq != newq,
3524 ("vm_page_swapqueue: invalid queues (%d, %d)", oldq, newq));
3525 vm_page_assert_locked(m);
3527 pq = &vm_pagequeue_domain(m)->vmd_pagequeues[oldq];
3528 vm_pagequeue_lock(pq);
3531 * The physical queue state might change at any point before the page
3532 * queue lock is acquired, so we must verify that we hold the correct
3533 * lock before proceeding.
3535 if (__predict_false(m->queue != oldq)) {
3536 vm_pagequeue_unlock(pq);
3541 * Once the queue index of the page changes, there is nothing
3542 * synchronizing with further updates to the physical queue state.
3543 * Therefore we must remove the page from the queue now in anticipation
3544 * of a successful commit, and be prepared to roll back.
3546 if (__predict_true((m->aflags & PGA_ENQUEUED) != 0)) {
3547 next = TAILQ_NEXT(m, plinks.q);
3548 TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
3549 vm_page_aflag_clear(m, PGA_ENQUEUED);
3556 * Atomically update the queue field and set PGA_REQUEUE while
3557 * ensuring that PGA_DEQUEUE has not been set.
3559 if (__predict_false(!vm_page_pqstate_cmpset(m, oldq, newq, PGA_DEQUEUE,
3562 vm_page_aflag_set(m, PGA_ENQUEUED);
3564 TAILQ_INSERT_BEFORE(next, m, plinks.q);
3566 TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
3568 vm_pagequeue_unlock(pq);
3571 vm_pagequeue_cnt_dec(pq);
3572 vm_pagequeue_unlock(pq);
3573 vm_page_pqbatch_submit(m, newq);
3577 * vm_page_free_prep:
3579 * Prepares the given page to be put on the free list,
3580 * disassociating it from any VM object. The caller may return
3581 * the page to the free list only if this function returns true.
3583 * The object must be locked. The page must be locked if it is
3587 vm_page_free_prep(vm_page_t m)
3591 * Synchronize with threads that have dropped a reference to this
3594 atomic_thread_fence_acq();
3596 #if defined(DIAGNOSTIC) && defined(PHYS_TO_DMAP)
3597 if (PMAP_HAS_DMAP && (m->flags & PG_ZERO) != 0) {
3600 p = (uint64_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m));
3601 for (i = 0; i < PAGE_SIZE / sizeof(uint64_t); i++, p++)
3602 KASSERT(*p == 0, ("vm_page_free_prep %p PG_ZERO %d %jx",
3603 m, i, (uintmax_t)*p));
3606 if ((m->oflags & VPO_UNMANAGED) == 0) {
3607 KASSERT(!pmap_page_is_mapped(m),
3608 ("vm_page_free_prep: freeing mapped page %p", m));
3609 KASSERT((m->aflags & (PGA_EXECUTABLE | PGA_WRITEABLE)) == 0,
3610 ("vm_page_free_prep: mapping flags set in page %p", m));
3612 KASSERT(m->queue == PQ_NONE,
3613 ("vm_page_free_prep: unmanaged page %p is queued", m));
3615 VM_CNT_INC(v_tfree);
3617 if (vm_page_sbusied(m))
3618 panic("vm_page_free_prep: freeing shared busy page %p", m);
3620 if (m->object != NULL) {
3621 vm_page_object_remove(m);
3624 * The object reference can be released without an atomic
3627 KASSERT((m->flags & PG_FICTITIOUS) != 0 ||
3628 m->ref_count == VPRC_OBJREF,
3629 ("vm_page_free_prep: page %p has unexpected ref_count %u",
3632 m->ref_count -= VPRC_OBJREF;
3635 if (vm_page_xbusied(m))
3639 * If fictitious remove object association and
3642 if ((m->flags & PG_FICTITIOUS) != 0) {
3643 KASSERT(m->ref_count == 1,
3644 ("fictitious page %p is referenced", m));
3645 KASSERT(m->queue == PQ_NONE,
3646 ("fictitious page %p is queued", m));
3651 * Pages need not be dequeued before they are returned to the physical
3652 * memory allocator, but they must at least be marked for a deferred
3655 if ((m->oflags & VPO_UNMANAGED) == 0)
3656 vm_page_dequeue_deferred_free(m);
3661 if (m->ref_count != 0)
3662 panic("vm_page_free_prep: page %p has references", m);
3665 * Restore the default memory attribute to the page.
3667 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT)
3668 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT);
3670 #if VM_NRESERVLEVEL > 0
3672 * Determine whether the page belongs to a reservation. If the page was
3673 * allocated from a per-CPU cache, it cannot belong to a reservation, so
3674 * as an optimization, we avoid the check in that case.
3676 if ((m->flags & PG_PCPU_CACHE) == 0 && vm_reserv_free_page(m))
3686 * Returns the given page to the free list, disassociating it
3687 * from any VM object.
3689 * The object must be locked. The page must be locked if it is
3693 vm_page_free_toq(vm_page_t m)
3695 struct vm_domain *vmd;
3698 if (!vm_page_free_prep(m))
3701 vmd = vm_pagequeue_domain(m);
3702 zone = vmd->vmd_pgcache[m->pool].zone;
3703 if ((m->flags & PG_PCPU_CACHE) != 0 && zone != NULL) {
3707 vm_domain_free_lock(vmd);
3708 vm_phys_free_pages(m, 0);
3709 vm_domain_free_unlock(vmd);
3710 vm_domain_freecnt_inc(vmd, 1);
3714 * vm_page_free_pages_toq:
3716 * Returns a list of pages to the free list, disassociating it
3717 * from any VM object. In other words, this is equivalent to
3718 * calling vm_page_free_toq() for each page of a list of VM objects.
3720 * The objects must be locked. The pages must be locked if it is
3724 vm_page_free_pages_toq(struct spglist *free, bool update_wire_count)
3729 if (SLIST_EMPTY(free))
3733 while ((m = SLIST_FIRST(free)) != NULL) {
3735 SLIST_REMOVE_HEAD(free, plinks.s.ss);
3736 vm_page_free_toq(m);
3739 if (update_wire_count)
3744 * Mark this page as wired down, preventing reclamation by the page daemon
3745 * or when the containing object is destroyed.
3748 vm_page_wire(vm_page_t m)
3752 KASSERT(m->object != NULL,
3753 ("vm_page_wire: page %p does not belong to an object", m));
3754 if (!vm_page_busied(m))
3755 VM_OBJECT_ASSERT_LOCKED(m->object);
3756 KASSERT((m->flags & PG_FICTITIOUS) == 0 ||
3757 VPRC_WIRE_COUNT(m->ref_count) >= 1,
3758 ("vm_page_wire: fictitious page %p has zero wirings", m));
3760 old = atomic_fetchadd_int(&m->ref_count, 1);
3761 KASSERT(VPRC_WIRE_COUNT(old) != VPRC_WIRE_COUNT_MAX,
3762 ("vm_page_wire: counter overflow for page %p", m));
3763 if (VPRC_WIRE_COUNT(old) == 0)
3768 * Attempt to wire a mapped page following a pmap lookup of that page.
3769 * This may fail if a thread is concurrently tearing down mappings of the page.
3770 * The transient failure is acceptable because it translates to the
3771 * failure of the caller pmap_extract_and_hold(), which should be then
3772 * followed by the vm_fault() fallback, see e.g. vm_fault_quick_hold_pages().
3775 vm_page_wire_mapped(vm_page_t m)
3782 ("vm_page_wire_mapped: wiring unreferenced page %p", m));
3783 if ((old & VPRC_BLOCKED) != 0)
3785 } while (!atomic_fcmpset_int(&m->ref_count, &old, old + 1));
3787 if (VPRC_WIRE_COUNT(old) == 0)
3793 * Release one wiring of the specified page, potentially allowing it to be
3796 * Only managed pages belonging to an object can be paged out. If the number
3797 * of wirings transitions to zero and the page is eligible for page out, then
3798 * the page is added to the specified paging queue. If the released wiring
3799 * represented the last reference to the page, the page is freed.
3801 * A managed page must be locked.
3804 vm_page_unwire(vm_page_t m, uint8_t queue)
3809 KASSERT(queue < PQ_COUNT,
3810 ("vm_page_unwire: invalid queue %u request for page %p", queue, m));
3812 if ((m->oflags & VPO_UNMANAGED) != 0) {
3813 if (vm_page_unwire_noq(m) && m->ref_count == 0)
3819 * Update LRU state before releasing the wiring reference.
3820 * We only need to do this once since we hold the page lock.
3821 * Use a release store when updating the reference count to
3822 * synchronize with vm_page_free_prep().
3827 KASSERT(VPRC_WIRE_COUNT(old) > 0,
3828 ("vm_page_unwire: wire count underflow for page %p", m));
3829 if (!locked && VPRC_WIRE_COUNT(old) == 1) {
3832 if (queue == PQ_ACTIVE && vm_page_queue(m) == PQ_ACTIVE)
3833 vm_page_reference(m);
3835 vm_page_mvqueue(m, queue);
3837 } while (!atomic_fcmpset_rel_int(&m->ref_count, &old, old - 1));
3840 * Release the lock only after the wiring is released, to ensure that
3841 * the page daemon does not encounter and dequeue the page while it is
3847 if (VPRC_WIRE_COUNT(old) == 1) {
3855 * Unwire a page without (re-)inserting it into a page queue. It is up
3856 * to the caller to enqueue, requeue, or free the page as appropriate.
3857 * In most cases involving managed pages, vm_page_unwire() should be used
3861 vm_page_unwire_noq(vm_page_t m)
3865 old = vm_page_drop(m, 1);
3866 KASSERT(VPRC_WIRE_COUNT(old) != 0,
3867 ("vm_page_unref: counter underflow for page %p", m));
3868 KASSERT((m->flags & PG_FICTITIOUS) == 0 || VPRC_WIRE_COUNT(old) > 1,
3869 ("vm_page_unref: missing ref on fictitious page %p", m));
3871 if (VPRC_WIRE_COUNT(old) > 1)
3878 * Ensure that the page is in the specified page queue. If the page is
3879 * active or being moved to the active queue, ensure that its act_count is
3880 * at least ACT_INIT but do not otherwise mess with it. Otherwise, ensure that
3881 * the page is at the tail of its page queue.
3883 * The page may be wired. The caller should release its wiring reference
3884 * before releasing the page lock, otherwise the page daemon may immediately
3887 * A managed page must be locked.
3889 static __always_inline void
3890 vm_page_mvqueue(vm_page_t m, const uint8_t nqueue)
3893 vm_page_assert_locked(m);
3894 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3895 ("vm_page_mvqueue: page %p is unmanaged", m));
3896 KASSERT(m->ref_count > 0,
3897 ("%s: page %p does not carry any references", __func__, m));
3899 if (vm_page_queue(m) != nqueue) {
3901 vm_page_enqueue(m, nqueue);
3902 } else if (nqueue != PQ_ACTIVE) {
3906 if (nqueue == PQ_ACTIVE && m->act_count < ACT_INIT)
3907 m->act_count = ACT_INIT;
3911 * Put the specified page on the active list (if appropriate).
3914 vm_page_activate(vm_page_t m)
3917 if ((m->oflags & VPO_UNMANAGED) != 0 || vm_page_wired(m))
3919 vm_page_mvqueue(m, PQ_ACTIVE);
3923 * Move the specified page to the tail of the inactive queue, or requeue
3924 * the page if it is already in the inactive queue.
3927 vm_page_deactivate(vm_page_t m)
3930 if ((m->oflags & VPO_UNMANAGED) != 0 || vm_page_wired(m))
3932 vm_page_mvqueue(m, PQ_INACTIVE);
3936 * Move the specified page close to the head of the inactive queue,
3937 * bypassing LRU. A marker page is used to maintain FIFO ordering.
3938 * As with regular enqueues, we use a per-CPU batch queue to reduce
3939 * contention on the page queue lock.
3942 _vm_page_deactivate_noreuse(vm_page_t m)
3945 vm_page_assert_locked(m);
3947 if (!vm_page_inactive(m)) {
3949 m->queue = PQ_INACTIVE;
3951 if ((m->aflags & PGA_REQUEUE_HEAD) == 0)
3952 vm_page_aflag_set(m, PGA_REQUEUE_HEAD);
3953 vm_page_pqbatch_submit(m, PQ_INACTIVE);
3957 vm_page_deactivate_noreuse(vm_page_t m)
3960 KASSERT(m->object != NULL,
3961 ("vm_page_deactivate_noreuse: page %p has no object", m));
3963 if ((m->oflags & VPO_UNMANAGED) == 0 && !vm_page_wired(m))
3964 _vm_page_deactivate_noreuse(m);
3968 * Put a page in the laundry, or requeue it if it is already there.
3971 vm_page_launder(vm_page_t m)
3974 if ((m->oflags & VPO_UNMANAGED) != 0 || vm_page_wired(m))
3976 vm_page_mvqueue(m, PQ_LAUNDRY);
3980 * Put a page in the PQ_UNSWAPPABLE holding queue.
3983 vm_page_unswappable(vm_page_t m)
3986 vm_page_assert_locked(m);
3987 KASSERT(!vm_page_wired(m) && (m->oflags & VPO_UNMANAGED) == 0,
3988 ("page %p already unswappable", m));
3991 vm_page_enqueue(m, PQ_UNSWAPPABLE);
3995 vm_page_release_toq(vm_page_t m, int flags)
3998 vm_page_assert_locked(m);
4001 * Use a check of the valid bits to determine whether we should
4002 * accelerate reclamation of the page. The object lock might not be
4003 * held here, in which case the check is racy. At worst we will either
4004 * accelerate reclamation of a valid page and violate LRU, or
4005 * unnecessarily defer reclamation of an invalid page.
4007 * If we were asked to not cache the page, place it near the head of the
4008 * inactive queue so that is reclaimed sooner.
4010 if ((flags & (VPR_TRYFREE | VPR_NOREUSE)) != 0 || m->valid == 0)
4011 _vm_page_deactivate_noreuse(m);
4012 else if (vm_page_active(m))
4013 vm_page_reference(m);
4015 vm_page_mvqueue(m, PQ_INACTIVE);
4019 * Unwire a page and either attempt to free it or re-add it to the page queues.
4022 vm_page_release(vm_page_t m, int flags)
4028 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4029 ("vm_page_release: page %p is unmanaged", m));
4031 if ((flags & VPR_TRYFREE) != 0) {
4033 object = (vm_object_t)atomic_load_ptr(&m->object);
4036 /* Depends on type-stability. */
4037 if (vm_page_busied(m) || !VM_OBJECT_TRYWLOCK(object)) {
4041 if (object == m->object)
4043 VM_OBJECT_WUNLOCK(object);
4045 if (__predict_true(object != NULL)) {
4046 vm_page_release_locked(m, flags);
4047 VM_OBJECT_WUNLOCK(object);
4053 * Update LRU state before releasing the wiring reference.
4054 * Use a release store when updating the reference count to
4055 * synchronize with vm_page_free_prep().
4060 KASSERT(VPRC_WIRE_COUNT(old) > 0,
4061 ("vm_page_unwire: wire count underflow for page %p", m));
4062 if (!locked && VPRC_WIRE_COUNT(old) == 1) {
4065 vm_page_release_toq(m, flags);
4067 } while (!atomic_fcmpset_rel_int(&m->ref_count, &old, old - 1));
4070 * Release the lock only after the wiring is released, to ensure that
4071 * the page daemon does not encounter and dequeue the page while it is
4077 if (VPRC_WIRE_COUNT(old) == 1) {
4084 /* See vm_page_release(). */
4086 vm_page_release_locked(vm_page_t m, int flags)
4089 VM_OBJECT_ASSERT_WLOCKED(m->object);
4090 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4091 ("vm_page_release_locked: page %p is unmanaged", m));
4093 if (vm_page_unwire_noq(m)) {
4094 if ((flags & VPR_TRYFREE) != 0 &&
4095 (m->object->ref_count == 0 || !pmap_page_is_mapped(m)) &&
4096 m->dirty == 0 && !vm_page_busied(m)) {
4100 vm_page_release_toq(m, flags);
4107 vm_page_try_blocked_op(vm_page_t m, void (*op)(vm_page_t))
4111 KASSERT(m->object != NULL && (m->oflags & VPO_UNMANAGED) == 0,
4112 ("vm_page_try_blocked_op: page %p has no object", m));
4113 KASSERT(vm_page_busied(m),
4114 ("vm_page_try_blocked_op: page %p is not busy", m));
4115 VM_OBJECT_ASSERT_LOCKED(m->object);
4120 ("vm_page_try_blocked_op: page %p has no references", m));
4121 if (VPRC_WIRE_COUNT(old) != 0)
4123 } while (!atomic_fcmpset_int(&m->ref_count, &old, old | VPRC_BLOCKED));
4128 * If the object is read-locked, new wirings may be created via an
4131 old = vm_page_drop(m, VPRC_BLOCKED);
4132 KASSERT(!VM_OBJECT_WOWNED(m->object) ||
4133 old == (VPRC_BLOCKED | VPRC_OBJREF),
4134 ("vm_page_try_blocked_op: unexpected refcount value %u for %p",
4140 * Atomically check for wirings and remove all mappings of the page.
4143 vm_page_try_remove_all(vm_page_t m)
4146 return (vm_page_try_blocked_op(m, pmap_remove_all));
4150 * Atomically check for wirings and remove all writeable mappings of the page.
4153 vm_page_try_remove_write(vm_page_t m)
4156 return (vm_page_try_blocked_op(m, pmap_remove_write));
4162 * Apply the specified advice to the given page.
4164 * The object and page must be locked.
4167 vm_page_advise(vm_page_t m, int advice)
4170 vm_page_assert_locked(m);
4171 VM_OBJECT_ASSERT_WLOCKED(m->object);
4172 if (advice == MADV_FREE)
4174 * Mark the page clean. This will allow the page to be freed
4175 * without first paging it out. MADV_FREE pages are often
4176 * quickly reused by malloc(3), so we do not do anything that
4177 * would result in a page fault on a later access.
4180 else if (advice != MADV_DONTNEED) {
4181 if (advice == MADV_WILLNEED)
4182 vm_page_activate(m);
4187 * Clear any references to the page. Otherwise, the page daemon will
4188 * immediately reactivate the page.
4190 vm_page_aflag_clear(m, PGA_REFERENCED);
4192 if (advice != MADV_FREE && m->dirty == 0 && pmap_is_modified(m))
4196 * Place clean pages near the head of the inactive queue rather than
4197 * the tail, thus defeating the queue's LRU operation and ensuring that
4198 * the page will be reused quickly. Dirty pages not already in the
4199 * laundry are moved there.
4202 vm_page_deactivate_noreuse(m);
4203 else if (!vm_page_in_laundry(m))
4208 * Grab a page, waiting until we are waken up due to the page
4209 * changing state. We keep on waiting, if the page continues
4210 * to be in the object. If the page doesn't exist, first allocate it
4211 * and then conditionally zero it.
4213 * This routine may sleep.
4215 * The object must be locked on entry. The lock will, however, be released
4216 * and reacquired if the routine sleeps.
4219 vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags)
4225 VM_OBJECT_ASSERT_WLOCKED(object);
4226 KASSERT((allocflags & VM_ALLOC_SBUSY) == 0 ||
4227 (allocflags & VM_ALLOC_IGN_SBUSY) != 0,
4228 ("vm_page_grab: VM_ALLOC_SBUSY/VM_ALLOC_IGN_SBUSY mismatch"));
4229 pflags = allocflags &
4230 ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL |
4232 if ((allocflags & VM_ALLOC_NOWAIT) == 0)
4233 pflags |= VM_ALLOC_WAITFAIL;
4234 if ((allocflags & VM_ALLOC_IGN_SBUSY) != 0)
4235 pflags |= VM_ALLOC_SBUSY;
4237 if ((m = vm_page_lookup(object, pindex)) != NULL) {
4238 if ((allocflags & (VM_ALLOC_IGN_SBUSY | VM_ALLOC_SBUSY)) != 0)
4239 sleep = !vm_page_trysbusy(m);
4241 sleep = !vm_page_tryxbusy(m);
4243 if ((allocflags & VM_ALLOC_NOWAIT) != 0)
4246 * Reference the page before unlocking and
4247 * sleeping so that the page daemon is less
4248 * likely to reclaim it.
4250 if ((allocflags & VM_ALLOC_NOCREAT) == 0)
4251 vm_page_aflag_set(m, PGA_REFERENCED);
4252 vm_page_busy_sleep(m, "pgrbwt", (allocflags &
4253 VM_ALLOC_IGN_SBUSY) != 0);
4254 VM_OBJECT_WLOCK(object);
4255 if ((allocflags & VM_ALLOC_WAITFAIL) != 0)
4259 if ((allocflags & VM_ALLOC_WIRED) != 0)
4264 if ((allocflags & VM_ALLOC_NOCREAT) != 0)
4266 m = vm_page_alloc(object, pindex, pflags);
4268 if ((allocflags & VM_ALLOC_NOWAIT) != 0)
4272 if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0)
4276 if ((allocflags & VM_ALLOC_NOBUSY) != 0) {
4277 if ((allocflags & VM_ALLOC_IGN_SBUSY) != 0)
4286 * Grab a page and make it valid, paging in if necessary. Pages missing from
4287 * their pager are zero filled and validated.
4290 vm_page_grab_valid(vm_page_t *mp, vm_object_t object, vm_pindex_t pindex, int allocflags)
4297 KASSERT((allocflags & VM_ALLOC_SBUSY) == 0 ||
4298 (allocflags & VM_ALLOC_IGN_SBUSY) != 0,
4299 ("vm_page_grab_valid: VM_ALLOC_SBUSY/VM_ALLOC_IGN_SBUSY mismatch"));
4300 KASSERT((allocflags &
4301 (VM_ALLOC_NOWAIT | VM_ALLOC_WAITFAIL | VM_ALLOC_ZERO)) == 0,
4302 ("vm_page_grab_valid: Invalid flags 0x%X", allocflags));
4303 VM_OBJECT_ASSERT_WLOCKED(object);
4304 pflags = allocflags & ~(VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY);
4305 pflags |= VM_ALLOC_WAITFAIL;
4309 if ((m = vm_page_lookup(object, pindex)) != NULL) {
4311 * If the page is fully valid it can only become invalid
4312 * with the object lock held. If it is not valid it can
4313 * become valid with the busy lock held. Therefore, we
4314 * may unnecessarily lock the exclusive busy here if we
4315 * race with I/O completion not using the object lock.
4316 * However, we will not end up with an invalid page and a
4319 if (!vm_page_all_valid(m) ||
4320 (allocflags & (VM_ALLOC_IGN_SBUSY | VM_ALLOC_SBUSY)) == 0) {
4321 sleep = !vm_page_tryxbusy(m);
4324 sleep = !vm_page_trysbusy(m);
4327 * Reference the page before unlocking and
4328 * sleeping so that the page daemon is less
4329 * likely to reclaim it.
4331 if ((allocflags & VM_ALLOC_NOCREAT) == 0)
4332 vm_page_aflag_set(m, PGA_REFERENCED);
4333 vm_page_busy_sleep(m, "pgrbwt", (allocflags &
4334 VM_ALLOC_IGN_SBUSY) != 0);
4335 VM_OBJECT_WLOCK(object);
4338 if ((allocflags & VM_ALLOC_NOCREAT) != 0 &&
4339 !vm_page_all_valid(m)) {
4345 return (VM_PAGER_FAIL);
4347 if ((allocflags & VM_ALLOC_WIRED) != 0)
4349 if (vm_page_all_valid(m))
4351 } else if ((allocflags & VM_ALLOC_NOCREAT) != 0) {
4353 return (VM_PAGER_FAIL);
4354 } else if ((m = vm_page_alloc(object, pindex, pflags)) != NULL) {
4360 vm_page_assert_xbusied(m);
4362 if (vm_pager_has_page(object, pindex, NULL, NULL)) {
4363 rv = vm_pager_get_pages(object, &m, 1, NULL, NULL);
4364 if (rv != VM_PAGER_OK) {
4365 if (allocflags & VM_ALLOC_WIRED)
4366 vm_page_unwire_noq(m);
4371 MPASS(vm_page_all_valid(m));
4373 vm_page_zero_invalid(m, TRUE);
4376 if ((allocflags & VM_ALLOC_NOBUSY) != 0) {
4382 if ((allocflags & VM_ALLOC_SBUSY) != 0 && xbusy)
4383 vm_page_busy_downgrade(m);
4385 return (VM_PAGER_OK);
4389 * Return the specified range of pages from the given object. For each
4390 * page offset within the range, if a page already exists within the object
4391 * at that offset and it is busy, then wait for it to change state. If,
4392 * instead, the page doesn't exist, then allocate it.
4394 * The caller must always specify an allocation class.
4396 * allocation classes:
4397 * VM_ALLOC_NORMAL normal process request
4398 * VM_ALLOC_SYSTEM system *really* needs the pages
4400 * The caller must always specify that the pages are to be busied and/or
4403 * optional allocation flags:
4404 * VM_ALLOC_IGN_SBUSY do not sleep on soft busy pages
4405 * VM_ALLOC_NOBUSY do not exclusive busy the page
4406 * VM_ALLOC_NOWAIT do not sleep
4407 * VM_ALLOC_SBUSY set page to sbusy state
4408 * VM_ALLOC_WIRED wire the pages
4409 * VM_ALLOC_ZERO zero and validate any invalid pages
4411 * If VM_ALLOC_NOWAIT is not specified, this routine may sleep. Otherwise, it
4412 * may return a partial prefix of the requested range.
4415 vm_page_grab_pages(vm_object_t object, vm_pindex_t pindex, int allocflags,
4416 vm_page_t *ma, int count)
4423 VM_OBJECT_ASSERT_WLOCKED(object);
4424 KASSERT(((u_int)allocflags >> VM_ALLOC_COUNT_SHIFT) == 0,
4425 ("vm_page_grap_pages: VM_ALLOC_COUNT() is not allowed"));
4426 KASSERT((allocflags & VM_ALLOC_NOBUSY) == 0 ||
4427 (allocflags & VM_ALLOC_WIRED) != 0,
4428 ("vm_page_grab_pages: the pages must be busied or wired"));
4429 KASSERT((allocflags & VM_ALLOC_SBUSY) == 0 ||
4430 (allocflags & VM_ALLOC_IGN_SBUSY) != 0,
4431 ("vm_page_grab_pages: VM_ALLOC_SBUSY/IGN_SBUSY mismatch"));
4434 pflags = allocflags &
4435 ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL |
4437 if ((allocflags & VM_ALLOC_NOWAIT) == 0)
4438 pflags |= VM_ALLOC_WAITFAIL;
4439 if ((allocflags & VM_ALLOC_IGN_SBUSY) != 0)
4440 pflags |= VM_ALLOC_SBUSY;
4443 m = vm_radix_lookup_le(&object->rtree, pindex + i);
4444 if (m == NULL || m->pindex != pindex + i) {
4448 mpred = TAILQ_PREV(m, pglist, listq);
4449 for (; i < count; i++) {
4452 (VM_ALLOC_SBUSY | VM_ALLOC_IGN_SBUSY)) != 0)
4453 sleep = !vm_page_trysbusy(m);
4455 sleep = !vm_page_tryxbusy(m);
4457 if ((allocflags & VM_ALLOC_NOWAIT) != 0)
4460 * Reference the page before unlocking and
4461 * sleeping so that the page daemon is less
4462 * likely to reclaim it.
4464 if ((allocflags & VM_ALLOC_NOCREAT) == 0)
4465 vm_page_aflag_set(m, PGA_REFERENCED);
4466 vm_page_busy_sleep(m, "grbmaw", (allocflags &
4467 VM_ALLOC_IGN_SBUSY) != 0);
4468 VM_OBJECT_WLOCK(object);
4471 if ((allocflags & VM_ALLOC_WIRED) != 0)
4474 if ((allocflags & VM_ALLOC_NOCREAT) != 0)
4476 m = vm_page_alloc_after(object, pindex + i,
4477 pflags | VM_ALLOC_COUNT(count - i), mpred);
4479 if ((allocflags & VM_ALLOC_NOWAIT) != 0)
4484 if (vm_page_none_valid(m) &&
4485 (allocflags & VM_ALLOC_ZERO) != 0) {
4486 if ((m->flags & PG_ZERO) == 0)
4490 if ((allocflags & VM_ALLOC_NOBUSY) != 0) {
4491 if ((allocflags & VM_ALLOC_IGN_SBUSY) != 0)
4497 m = vm_page_next(m);
4503 * Mapping function for valid or dirty bits in a page.
4505 * Inputs are required to range within a page.
4508 vm_page_bits(int base, int size)
4514 base + size <= PAGE_SIZE,
4515 ("vm_page_bits: illegal base/size %d/%d", base, size)
4518 if (size == 0) /* handle degenerate case */
4521 first_bit = base >> DEV_BSHIFT;
4522 last_bit = (base + size - 1) >> DEV_BSHIFT;
4524 return (((vm_page_bits_t)2 << last_bit) -
4525 ((vm_page_bits_t)1 << first_bit));
4529 vm_page_bits_set(vm_page_t m, vm_page_bits_t *bits, vm_page_bits_t set)
4532 #if PAGE_SIZE == 32768
4533 atomic_set_64((uint64_t *)bits, set);
4534 #elif PAGE_SIZE == 16384
4535 atomic_set_32((uint32_t *)bits, set);
4536 #elif (PAGE_SIZE == 8192) && defined(atomic_set_16)
4537 atomic_set_16((uint16_t *)bits, set);
4538 #elif (PAGE_SIZE == 4096) && defined(atomic_set_8)
4539 atomic_set_8((uint8_t *)bits, set);
4540 #else /* PAGE_SIZE <= 8192 */
4544 addr = (uintptr_t)bits;
4546 * Use a trick to perform a 32-bit atomic on the
4547 * containing aligned word, to not depend on the existence
4548 * of atomic_{set, clear}_{8, 16}.
4550 shift = addr & (sizeof(uint32_t) - 1);
4551 #if BYTE_ORDER == BIG_ENDIAN
4552 shift = (sizeof(uint32_t) - sizeof(vm_page_bits_t) - shift) * NBBY;
4556 addr &= ~(sizeof(uint32_t) - 1);
4557 atomic_set_32((uint32_t *)addr, set << shift);
4558 #endif /* PAGE_SIZE */
4562 vm_page_bits_clear(vm_page_t m, vm_page_bits_t *bits, vm_page_bits_t clear)
4565 #if PAGE_SIZE == 32768
4566 atomic_clear_64((uint64_t *)bits, clear);
4567 #elif PAGE_SIZE == 16384
4568 atomic_clear_32((uint32_t *)bits, clear);
4569 #elif (PAGE_SIZE == 8192) && defined(atomic_clear_16)
4570 atomic_clear_16((uint16_t *)bits, clear);
4571 #elif (PAGE_SIZE == 4096) && defined(atomic_clear_8)
4572 atomic_clear_8((uint8_t *)bits, clear);
4573 #else /* PAGE_SIZE <= 8192 */
4577 addr = (uintptr_t)bits;
4579 * Use a trick to perform a 32-bit atomic on the
4580 * containing aligned word, to not depend on the existence
4581 * of atomic_{set, clear}_{8, 16}.
4583 shift = addr & (sizeof(uint32_t) - 1);
4584 #if BYTE_ORDER == BIG_ENDIAN
4585 shift = (sizeof(uint32_t) - sizeof(vm_page_bits_t) - shift) * NBBY;
4589 addr &= ~(sizeof(uint32_t) - 1);
4590 atomic_clear_32((uint32_t *)addr, clear << shift);
4591 #endif /* PAGE_SIZE */
4595 * vm_page_set_valid_range:
4597 * Sets portions of a page valid. The arguments are expected
4598 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
4599 * of any partial chunks touched by the range. The invalid portion of
4600 * such chunks will be zeroed.
4602 * (base + size) must be less then or equal to PAGE_SIZE.
4605 vm_page_set_valid_range(vm_page_t m, int base, int size)
4608 vm_page_bits_t pagebits;
4610 vm_page_assert_busied(m);
4611 if (size == 0) /* handle degenerate case */
4615 * If the base is not DEV_BSIZE aligned and the valid
4616 * bit is clear, we have to zero out a portion of the
4619 if ((frag = rounddown2(base, DEV_BSIZE)) != base &&
4620 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0)
4621 pmap_zero_page_area(m, frag, base - frag);
4624 * If the ending offset is not DEV_BSIZE aligned and the
4625 * valid bit is clear, we have to zero out a portion of
4628 endoff = base + size;
4629 if ((frag = rounddown2(endoff, DEV_BSIZE)) != endoff &&
4630 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0)
4631 pmap_zero_page_area(m, endoff,
4632 DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
4635 * Assert that no previously invalid block that is now being validated
4638 KASSERT((~m->valid & vm_page_bits(base, size) & m->dirty) == 0,
4639 ("vm_page_set_valid_range: page %p is dirty", m));
4642 * Set valid bits inclusive of any overlap.
4644 pagebits = vm_page_bits(base, size);
4645 if (vm_page_xbusied(m))
4646 m->valid |= pagebits;
4648 vm_page_bits_set(m, &m->valid, pagebits);
4652 * Clear the given bits from the specified page's dirty field.
4654 static __inline void
4655 vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits)
4658 vm_page_assert_busied(m);
4661 * If the page is xbusied and not write mapped we are the
4662 * only thread that can modify dirty bits. Otherwise, The pmap
4663 * layer can call vm_page_dirty() without holding a distinguished
4664 * lock. The combination of page busy and atomic operations
4665 * suffice to guarantee consistency of the page dirty field.
4667 if (vm_page_xbusied(m) && !pmap_page_is_write_mapped(m))
4668 m->dirty &= ~pagebits;
4670 vm_page_bits_clear(m, &m->dirty, pagebits);
4674 * vm_page_set_validclean:
4676 * Sets portions of a page valid and clean. The arguments are expected
4677 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
4678 * of any partial chunks touched by the range. The invalid portion of
4679 * such chunks will be zero'd.
4681 * (base + size) must be less then or equal to PAGE_SIZE.
4684 vm_page_set_validclean(vm_page_t m, int base, int size)
4686 vm_page_bits_t oldvalid, pagebits;
4689 vm_page_assert_busied(m);
4690 if (size == 0) /* handle degenerate case */
4694 * If the base is not DEV_BSIZE aligned and the valid
4695 * bit is clear, we have to zero out a portion of the
4698 if ((frag = rounddown2(base, DEV_BSIZE)) != base &&
4699 (m->valid & ((vm_page_bits_t)1 << (base >> DEV_BSHIFT))) == 0)
4700 pmap_zero_page_area(m, frag, base - frag);
4703 * If the ending offset is not DEV_BSIZE aligned and the
4704 * valid bit is clear, we have to zero out a portion of
4707 endoff = base + size;
4708 if ((frag = rounddown2(endoff, DEV_BSIZE)) != endoff &&
4709 (m->valid & ((vm_page_bits_t)1 << (endoff >> DEV_BSHIFT))) == 0)
4710 pmap_zero_page_area(m, endoff,
4711 DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
4714 * Set valid, clear dirty bits. If validating the entire
4715 * page we can safely clear the pmap modify bit. We also
4716 * use this opportunity to clear the PGA_NOSYNC flag. If a process
4717 * takes a write fault on a MAP_NOSYNC memory area the flag will
4720 * We set valid bits inclusive of any overlap, but we can only
4721 * clear dirty bits for DEV_BSIZE chunks that are fully within
4724 oldvalid = m->valid;
4725 pagebits = vm_page_bits(base, size);
4726 if (vm_page_xbusied(m))
4727 m->valid |= pagebits;
4729 vm_page_bits_set(m, &m->valid, pagebits);
4731 if ((frag = base & (DEV_BSIZE - 1)) != 0) {
4732 frag = DEV_BSIZE - frag;
4738 pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1));
4740 if (base == 0 && size == PAGE_SIZE) {
4742 * The page can only be modified within the pmap if it is
4743 * mapped, and it can only be mapped if it was previously
4746 if (oldvalid == VM_PAGE_BITS_ALL)
4748 * Perform the pmap_clear_modify() first. Otherwise,
4749 * a concurrent pmap operation, such as
4750 * pmap_protect(), could clear a modification in the
4751 * pmap and set the dirty field on the page before
4752 * pmap_clear_modify() had begun and after the dirty
4753 * field was cleared here.
4755 pmap_clear_modify(m);
4757 vm_page_aflag_clear(m, PGA_NOSYNC);
4758 } else if (oldvalid != VM_PAGE_BITS_ALL && vm_page_xbusied(m))
4759 m->dirty &= ~pagebits;
4761 vm_page_clear_dirty_mask(m, pagebits);
4765 vm_page_clear_dirty(vm_page_t m, int base, int size)
4768 vm_page_clear_dirty_mask(m, vm_page_bits(base, size));
4772 * vm_page_set_invalid:
4774 * Invalidates DEV_BSIZE'd chunks within a page. Both the
4775 * valid and dirty bits for the effected areas are cleared.
4778 vm_page_set_invalid(vm_page_t m, int base, int size)
4780 vm_page_bits_t bits;
4784 * The object lock is required so that pages can't be mapped
4785 * read-only while we're in the process of invalidating them.
4788 VM_OBJECT_ASSERT_WLOCKED(object);
4789 vm_page_assert_busied(m);
4791 if (object->type == OBJT_VNODE && base == 0 && IDX_TO_OFF(m->pindex) +
4792 size >= object->un_pager.vnp.vnp_size)
4793 bits = VM_PAGE_BITS_ALL;
4795 bits = vm_page_bits(base, size);
4796 if (object->ref_count != 0 && vm_page_all_valid(m) && bits != 0)
4798 KASSERT((bits == 0 && vm_page_all_valid(m)) ||
4799 !pmap_page_is_mapped(m),
4800 ("vm_page_set_invalid: page %p is mapped", m));
4801 if (vm_page_xbusied(m)) {
4805 vm_page_bits_clear(m, &m->valid, bits);
4806 vm_page_bits_clear(m, &m->dirty, bits);
4813 * Invalidates the entire page. The page must be busy, unmapped, and
4814 * the enclosing object must be locked. The object locks protects
4815 * against concurrent read-only pmap enter which is done without
4819 vm_page_invalid(vm_page_t m)
4822 vm_page_assert_busied(m);
4823 VM_OBJECT_ASSERT_LOCKED(m->object);
4824 MPASS(!pmap_page_is_mapped(m));
4826 if (vm_page_xbusied(m))
4829 vm_page_bits_clear(m, &m->valid, VM_PAGE_BITS_ALL);
4833 * vm_page_zero_invalid()
4835 * The kernel assumes that the invalid portions of a page contain
4836 * garbage, but such pages can be mapped into memory by user code.
4837 * When this occurs, we must zero out the non-valid portions of the
4838 * page so user code sees what it expects.
4840 * Pages are most often semi-valid when the end of a file is mapped
4841 * into memory and the file's size is not page aligned.
4844 vm_page_zero_invalid(vm_page_t m, boolean_t setvalid)
4850 * Scan the valid bits looking for invalid sections that
4851 * must be zeroed. Invalid sub-DEV_BSIZE'd areas ( where the
4852 * valid bit may be set ) have already been zeroed by
4853 * vm_page_set_validclean().
4855 for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) {
4856 if (i == (PAGE_SIZE / DEV_BSIZE) ||
4857 (m->valid & ((vm_page_bits_t)1 << i))) {
4859 pmap_zero_page_area(m,
4860 b << DEV_BSHIFT, (i - b) << DEV_BSHIFT);
4867 * setvalid is TRUE when we can safely set the zero'd areas
4868 * as being valid. We can do this if there are no cache consistancy
4869 * issues. e.g. it is ok to do with UFS, but not ok to do with NFS.
4878 * Is (partial) page valid? Note that the case where size == 0
4879 * will return FALSE in the degenerate case where the page is
4880 * entirely invalid, and TRUE otherwise.
4882 * Some callers envoke this routine without the busy lock held and
4883 * handle races via higher level locks. Typical callers should
4884 * hold a busy lock to prevent invalidation.
4887 vm_page_is_valid(vm_page_t m, int base, int size)
4889 vm_page_bits_t bits;
4891 bits = vm_page_bits(base, size);
4892 return (m->valid != 0 && (m->valid & bits) == bits);
4896 * Returns true if all of the specified predicates are true for the entire
4897 * (super)page and false otherwise.
4900 vm_page_ps_test(vm_page_t m, int flags, vm_page_t skip_m)
4906 if (skip_m != NULL && skip_m->object != object)
4908 VM_OBJECT_ASSERT_LOCKED(object);
4909 npages = atop(pagesizes[m->psind]);
4912 * The physically contiguous pages that make up a superpage, i.e., a
4913 * page with a page size index ("psind") greater than zero, will
4914 * occupy adjacent entries in vm_page_array[].
4916 for (i = 0; i < npages; i++) {
4917 /* Always test object consistency, including "skip_m". */
4918 if (m[i].object != object)
4920 if (&m[i] == skip_m)
4922 if ((flags & PS_NONE_BUSY) != 0 && vm_page_busied(&m[i]))
4924 if ((flags & PS_ALL_DIRTY) != 0) {
4926 * Calling vm_page_test_dirty() or pmap_is_modified()
4927 * might stop this case from spuriously returning
4928 * "false". However, that would require a write lock
4929 * on the object containing "m[i]".
4931 if (m[i].dirty != VM_PAGE_BITS_ALL)
4934 if ((flags & PS_ALL_VALID) != 0 &&
4935 m[i].valid != VM_PAGE_BITS_ALL)
4942 * Set the page's dirty bits if the page is modified.
4945 vm_page_test_dirty(vm_page_t m)
4948 vm_page_assert_busied(m);
4949 if (m->dirty != VM_PAGE_BITS_ALL && pmap_is_modified(m))
4954 vm_page_valid(vm_page_t m)
4957 vm_page_assert_busied(m);
4958 if (vm_page_xbusied(m))
4959 m->valid = VM_PAGE_BITS_ALL;
4961 vm_page_bits_set(m, &m->valid, VM_PAGE_BITS_ALL);
4965 vm_page_lock_KBI(vm_page_t m, const char *file, int line)
4968 mtx_lock_flags_(vm_page_lockptr(m), 0, file, line);
4972 vm_page_unlock_KBI(vm_page_t m, const char *file, int line)
4975 mtx_unlock_flags_(vm_page_lockptr(m), 0, file, line);
4979 vm_page_trylock_KBI(vm_page_t m, const char *file, int line)
4982 return (mtx_trylock_flags_(vm_page_lockptr(m), 0, file, line));
4985 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
4987 vm_page_assert_locked_KBI(vm_page_t m, const char *file, int line)
4990 vm_page_lock_assert_KBI(m, MA_OWNED, file, line);
4994 vm_page_lock_assert_KBI(vm_page_t m, int a, const char *file, int line)
4997 mtx_assert_(vm_page_lockptr(m), a, file, line);
5003 vm_page_object_busy_assert(vm_page_t m)
5007 * Certain of the page's fields may only be modified by the
5008 * holder of a page or object busy.
5010 if (m->object != NULL && !vm_page_busied(m))
5011 VM_OBJECT_ASSERT_BUSY(m->object);
5015 vm_page_assert_pga_writeable(vm_page_t m, uint8_t bits)
5018 if ((bits & PGA_WRITEABLE) == 0)
5022 * The PGA_WRITEABLE flag can only be set if the page is
5023 * managed, is exclusively busied or the object is locked.
5024 * Currently, this flag is only set by pmap_enter().
5026 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5027 ("PGA_WRITEABLE on unmanaged page"));
5028 if (!vm_page_xbusied(m))
5029 VM_OBJECT_ASSERT_BUSY(m->object);
5033 #include "opt_ddb.h"
5035 #include <sys/kernel.h>
5037 #include <ddb/ddb.h>
5039 DB_SHOW_COMMAND(page, vm_page_print_page_info)
5042 db_printf("vm_cnt.v_free_count: %d\n", vm_free_count());
5043 db_printf("vm_cnt.v_inactive_count: %d\n", vm_inactive_count());
5044 db_printf("vm_cnt.v_active_count: %d\n", vm_active_count());
5045 db_printf("vm_cnt.v_laundry_count: %d\n", vm_laundry_count());
5046 db_printf("vm_cnt.v_wire_count: %d\n", vm_wire_count());
5047 db_printf("vm_cnt.v_free_reserved: %d\n", vm_cnt.v_free_reserved);
5048 db_printf("vm_cnt.v_free_min: %d\n", vm_cnt.v_free_min);
5049 db_printf("vm_cnt.v_free_target: %d\n", vm_cnt.v_free_target);
5050 db_printf("vm_cnt.v_inactive_target: %d\n", vm_cnt.v_inactive_target);
5053 DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info)
5057 db_printf("pq_free %d\n", vm_free_count());
5058 for (dom = 0; dom < vm_ndomains; dom++) {
5060 "dom %d page_cnt %d free %d pq_act %d pq_inact %d pq_laund %d pq_unsw %d\n",
5062 vm_dom[dom].vmd_page_count,
5063 vm_dom[dom].vmd_free_count,
5064 vm_dom[dom].vmd_pagequeues[PQ_ACTIVE].pq_cnt,
5065 vm_dom[dom].vmd_pagequeues[PQ_INACTIVE].pq_cnt,
5066 vm_dom[dom].vmd_pagequeues[PQ_LAUNDRY].pq_cnt,
5067 vm_dom[dom].vmd_pagequeues[PQ_UNSWAPPABLE].pq_cnt);
5071 DB_SHOW_COMMAND(pginfo, vm_page_print_pginfo)
5074 boolean_t phys, virt;
5077 db_printf("show pginfo addr\n");
5081 phys = strchr(modif, 'p') != NULL;
5082 virt = strchr(modif, 'v') != NULL;
5084 m = PHYS_TO_VM_PAGE(pmap_kextract(addr));
5086 m = PHYS_TO_VM_PAGE(addr);
5088 m = (vm_page_t)addr;
5090 "page %p obj %p pidx 0x%jx phys 0x%jx q %d ref %u\n"
5091 " af 0x%x of 0x%x f 0x%x act %d busy %x valid 0x%x dirty 0x%x\n",
5092 m, m->object, (uintmax_t)m->pindex, (uintmax_t)m->phys_addr,
5093 m->queue, m->ref_count, m->aflags, m->oflags,
5094 m->flags, m->act_count, m->busy_lock, m->valid, m->dirty);