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
37 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
38 * All rights reserved.
40 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
42 * Permission to use, copy, modify and distribute this software and
43 * its documentation is hereby granted, provided that both the copyright
44 * notice and this permission notice appear in all copies of the
45 * software, derivative works or modified versions, and any portions
46 * thereof, and that both notices appear in supporting documentation.
48 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
50 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
52 * Carnegie Mellon requests users of this software to return to
54 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
55 * School of Computer Science
56 * Carnegie Mellon University
57 * Pittsburgh PA 15213-3890
59 * any improvements or extensions that they make and grant Carnegie the
60 * rights to redistribute these changes.
64 * Resident memory management module.
67 #include <sys/cdefs.h>
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/counter.h>
73 #include <sys/domainset.h>
74 #include <sys/kernel.h>
75 #include <sys/limits.h>
76 #include <sys/linker.h>
78 #include <sys/malloc.h>
80 #include <sys/msgbuf.h>
81 #include <sys/mutex.h>
83 #include <sys/rwlock.h>
84 #include <sys/sleepqueue.h>
86 #include <sys/sched.h>
88 #include <sys/sysctl.h>
89 #include <sys/vmmeter.h>
90 #include <sys/vnode.h>
94 #include <vm/vm_param.h>
95 #include <vm/vm_domainset.h>
96 #include <vm/vm_kern.h>
97 #include <vm/vm_map.h>
98 #include <vm/vm_object.h>
99 #include <vm/vm_page.h>
100 #include <vm/vm_pageout.h>
101 #include <vm/vm_phys.h>
102 #include <vm/vm_pagequeue.h>
103 #include <vm/vm_pager.h>
104 #include <vm/vm_radix.h>
105 #include <vm/vm_reserv.h>
106 #include <vm/vm_extern.h>
107 #include <vm/vm_dumpset.h>
109 #include <vm/uma_int.h>
111 #include <machine/md_var.h>
113 struct vm_domain vm_dom[MAXMEMDOM];
115 DPCPU_DEFINE_STATIC(struct vm_batchqueue, pqbatch[MAXMEMDOM][PQ_COUNT]);
117 struct mtx_padalign __exclusive_cache_line pa_lock[PA_LOCK_COUNT];
119 struct mtx_padalign __exclusive_cache_line vm_domainset_lock;
120 /* The following fields are protected by the domainset lock. */
121 domainset_t __exclusive_cache_line vm_min_domains;
122 domainset_t __exclusive_cache_line vm_severe_domains;
123 static int vm_min_waiters;
124 static int vm_severe_waiters;
125 static int vm_pageproc_waiters;
127 static SYSCTL_NODE(_vm_stats, OID_AUTO, page, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
128 "VM page statistics");
130 static COUNTER_U64_DEFINE_EARLY(pqstate_commit_retries);
131 SYSCTL_COUNTER_U64(_vm_stats_page, OID_AUTO, pqstate_commit_retries,
132 CTLFLAG_RD, &pqstate_commit_retries,
133 "Number of failed per-page atomic queue state updates");
135 static COUNTER_U64_DEFINE_EARLY(queue_ops);
136 SYSCTL_COUNTER_U64(_vm_stats_page, OID_AUTO, queue_ops,
137 CTLFLAG_RD, &queue_ops,
138 "Number of batched queue operations");
140 static COUNTER_U64_DEFINE_EARLY(queue_nops);
141 SYSCTL_COUNTER_U64(_vm_stats_page, OID_AUTO, queue_nops,
142 CTLFLAG_RD, &queue_nops,
143 "Number of batched queue operations with no effects");
146 * bogus page -- for I/O to/from partially complete buffers,
147 * or for paging into sparsely invalid regions.
149 vm_page_t bogus_page;
151 vm_page_t vm_page_array;
152 long vm_page_array_size;
155 struct bitset *vm_page_dump;
156 long vm_page_dump_pages;
158 static TAILQ_HEAD(, vm_page) blacklist_head;
159 static int sysctl_vm_page_blacklist(SYSCTL_HANDLER_ARGS);
160 SYSCTL_PROC(_vm, OID_AUTO, page_blacklist, CTLTYPE_STRING | CTLFLAG_RD |
161 CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_page_blacklist, "A", "Blacklist pages");
163 static uma_zone_t fakepg_zone;
165 static void vm_page_alloc_check(vm_page_t m);
166 static bool _vm_page_busy_sleep(vm_object_t obj, vm_page_t m,
167 vm_pindex_t pindex, const char *wmesg, int allocflags, bool locked);
168 static void vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits);
169 static void vm_page_enqueue(vm_page_t m, uint8_t queue);
170 static bool vm_page_free_prep(vm_page_t m);
171 static void vm_page_free_toq(vm_page_t m);
172 static void vm_page_init(void *dummy);
173 static int vm_page_insert_after(vm_page_t m, vm_object_t object,
174 vm_pindex_t pindex, vm_page_t mpred);
175 static void vm_page_insert_radixdone(vm_page_t m, vm_object_t object,
177 static void vm_page_mvqueue(vm_page_t m, const uint8_t queue,
178 const uint16_t nflag);
179 static int vm_page_reclaim_run(int req_class, int domain, u_long npages,
180 vm_page_t m_run, vm_paddr_t high);
181 static void vm_page_release_toq(vm_page_t m, uint8_t nqueue, bool noreuse);
182 static int vm_domain_alloc_fail(struct vm_domain *vmd, vm_object_t object,
184 static int vm_page_zone_import(void *arg, void **store, int cnt, int domain,
186 static void vm_page_zone_release(void *arg, void **store, int cnt);
188 SYSINIT(vm_page, SI_SUB_VM, SI_ORDER_SECOND, vm_page_init, NULL);
191 vm_page_init(void *dummy)
194 fakepg_zone = uma_zcreate("fakepg", sizeof(struct vm_page), NULL, NULL,
195 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
196 bogus_page = vm_page_alloc_noobj(VM_ALLOC_WIRED);
199 static int pgcache_zone_max_pcpu;
200 SYSCTL_INT(_vm, OID_AUTO, pgcache_zone_max_pcpu,
201 CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &pgcache_zone_max_pcpu, 0,
202 "Per-CPU page cache size");
205 * The cache page zone is initialized later since we need to be able to allocate
206 * pages before UMA is fully initialized.
209 vm_page_init_cache_zones(void *dummy __unused)
211 struct vm_domain *vmd;
212 struct vm_pgcache *pgcache;
213 int cache, domain, maxcache, pool;
215 TUNABLE_INT_FETCH("vm.pgcache_zone_max_pcpu", &pgcache_zone_max_pcpu);
216 maxcache = pgcache_zone_max_pcpu * mp_ncpus;
217 for (domain = 0; domain < vm_ndomains; domain++) {
218 vmd = VM_DOMAIN(domain);
219 for (pool = 0; pool < VM_NFREEPOOL; pool++) {
220 pgcache = &vmd->vmd_pgcache[pool];
221 pgcache->domain = domain;
222 pgcache->pool = pool;
223 pgcache->zone = uma_zcache_create("vm pgcache",
224 PAGE_SIZE, NULL, NULL, NULL, NULL,
225 vm_page_zone_import, vm_page_zone_release, pgcache,
229 * Limit each pool's zone to 0.1% of the pages in the
232 cache = maxcache != 0 ? maxcache :
233 vmd->vmd_page_count / 1000;
234 uma_zone_set_maxcache(pgcache->zone, cache);
238 SYSINIT(vm_page2, SI_SUB_VM_CONF, SI_ORDER_ANY, vm_page_init_cache_zones, NULL);
240 /* Make sure that u_long is at least 64 bits when PAGE_SIZE is 32K. */
241 #if PAGE_SIZE == 32768
243 CTASSERT(sizeof(u_long) >= 8);
250 * Sets the page size, perhaps based upon the memory
251 * size. Must be called before any use of page-size
252 * dependent functions.
255 vm_set_page_size(void)
257 if (vm_cnt.v_page_size == 0)
258 vm_cnt.v_page_size = PAGE_SIZE;
259 if (((vm_cnt.v_page_size - 1) & vm_cnt.v_page_size) != 0)
260 panic("vm_set_page_size: page size not a power of two");
264 * vm_page_blacklist_next:
266 * Find the next entry in the provided string of blacklist
267 * addresses. Entries are separated by space, comma, or newline.
268 * If an invalid integer is encountered then the rest of the
269 * string is skipped. Updates the list pointer to the next
270 * character, or NULL if the string is exhausted or invalid.
273 vm_page_blacklist_next(char **list, char *end)
278 if (list == NULL || *list == NULL)
286 * If there's no end pointer then the buffer is coming from
287 * the kenv and we know it's null-terminated.
290 end = *list + strlen(*list);
292 /* Ensure that strtoq() won't walk off the end */
294 if (*end == '\n' || *end == ' ' || *end == ',')
297 printf("Blacklist not terminated, skipping\n");
303 for (pos = *list; *pos != '\0'; pos = cp) {
304 bad = strtoq(pos, &cp, 0);
305 if (*cp == '\0' || *cp == ' ' || *cp == ',' || *cp == '\n') {
314 if (*cp == '\0' || ++cp >= end)
318 return (trunc_page(bad));
320 printf("Garbage in RAM blacklist, skipping\n");
326 vm_page_blacklist_add(vm_paddr_t pa, bool verbose)
328 struct vm_domain *vmd;
332 m = vm_phys_paddr_to_vm_page(pa);
334 return (true); /* page does not exist, no failure */
336 vmd = vm_pagequeue_domain(m);
337 vm_domain_free_lock(vmd);
338 found = vm_phys_unfree_page(m);
339 vm_domain_free_unlock(vmd);
341 vm_domain_freecnt_inc(vmd, -1);
342 TAILQ_INSERT_TAIL(&blacklist_head, m, listq);
344 printf("Skipping page with pa 0x%jx\n", (uintmax_t)pa);
350 * vm_page_blacklist_check:
352 * Iterate through the provided string of blacklist addresses, pulling
353 * each entry out of the physical allocator free list and putting it
354 * onto a list for reporting via the vm.page_blacklist sysctl.
357 vm_page_blacklist_check(char *list, char *end)
363 while (next != NULL) {
364 if ((pa = vm_page_blacklist_next(&next, end)) == 0)
366 vm_page_blacklist_add(pa, bootverbose);
371 * vm_page_blacklist_load:
373 * Search for a special module named "ram_blacklist". It'll be a
374 * plain text file provided by the user via the loader directive
378 vm_page_blacklist_load(char **list, char **end)
387 mod = preload_search_by_type("ram_blacklist");
389 ptr = preload_fetch_addr(mod);
390 len = preload_fetch_size(mod);
401 sysctl_vm_page_blacklist(SYSCTL_HANDLER_ARGS)
408 error = sysctl_wire_old_buffer(req, 0);
411 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
412 TAILQ_FOREACH(m, &blacklist_head, listq) {
413 sbuf_printf(&sbuf, "%s%#jx", first ? "" : ",",
414 (uintmax_t)m->phys_addr);
417 error = sbuf_finish(&sbuf);
423 * Initialize a dummy page for use in scans of the specified paging queue.
424 * In principle, this function only needs to set the flag PG_MARKER.
425 * Nonetheless, it write busies the page as a safety precaution.
428 vm_page_init_marker(vm_page_t marker, int queue, uint16_t aflags)
431 bzero(marker, sizeof(*marker));
432 marker->flags = PG_MARKER;
433 marker->a.flags = aflags;
434 marker->busy_lock = VPB_CURTHREAD_EXCLUSIVE;
435 marker->a.queue = queue;
439 vm_page_domain_init(int domain)
441 struct vm_domain *vmd;
442 struct vm_pagequeue *pq;
445 vmd = VM_DOMAIN(domain);
446 bzero(vmd, sizeof(*vmd));
447 *__DECONST(const char **, &vmd->vmd_pagequeues[PQ_INACTIVE].pq_name) =
448 "vm inactive pagequeue";
449 *__DECONST(const char **, &vmd->vmd_pagequeues[PQ_ACTIVE].pq_name) =
450 "vm active pagequeue";
451 *__DECONST(const char **, &vmd->vmd_pagequeues[PQ_LAUNDRY].pq_name) =
452 "vm laundry pagequeue";
453 *__DECONST(const char **,
454 &vmd->vmd_pagequeues[PQ_UNSWAPPABLE].pq_name) =
455 "vm unswappable pagequeue";
456 vmd->vmd_domain = domain;
457 vmd->vmd_page_count = 0;
458 vmd->vmd_free_count = 0;
460 vmd->vmd_oom = FALSE;
461 for (i = 0; i < PQ_COUNT; i++) {
462 pq = &vmd->vmd_pagequeues[i];
463 TAILQ_INIT(&pq->pq_pl);
464 mtx_init(&pq->pq_mutex, pq->pq_name, "vm pagequeue",
465 MTX_DEF | MTX_DUPOK);
467 vm_page_init_marker(&vmd->vmd_markers[i], i, 0);
469 mtx_init(&vmd->vmd_free_mtx, "vm page free queue", NULL, MTX_DEF);
470 mtx_init(&vmd->vmd_pageout_mtx, "vm pageout lock", NULL, MTX_DEF);
471 snprintf(vmd->vmd_name, sizeof(vmd->vmd_name), "%d", domain);
474 * inacthead is used to provide FIFO ordering for LRU-bypassing
477 vm_page_init_marker(&vmd->vmd_inacthead, PQ_INACTIVE, PGA_ENQUEUED);
478 TAILQ_INSERT_HEAD(&vmd->vmd_pagequeues[PQ_INACTIVE].pq_pl,
479 &vmd->vmd_inacthead, plinks.q);
482 * The clock pages are used to implement active queue scanning without
483 * requeues. Scans start at clock[0], which is advanced after the scan
484 * ends. When the two clock hands meet, they are reset and scanning
485 * resumes from the head of the queue.
487 vm_page_init_marker(&vmd->vmd_clock[0], PQ_ACTIVE, PGA_ENQUEUED);
488 vm_page_init_marker(&vmd->vmd_clock[1], PQ_ACTIVE, PGA_ENQUEUED);
489 TAILQ_INSERT_HEAD(&vmd->vmd_pagequeues[PQ_ACTIVE].pq_pl,
490 &vmd->vmd_clock[0], plinks.q);
491 TAILQ_INSERT_TAIL(&vmd->vmd_pagequeues[PQ_ACTIVE].pq_pl,
492 &vmd->vmd_clock[1], plinks.q);
496 * Initialize a physical page in preparation for adding it to the free
500 vm_page_init_page(vm_page_t m, vm_paddr_t pa, int segind)
505 m->busy_lock = VPB_FREED;
506 m->flags = m->a.flags = 0;
508 m->a.queue = PQ_NONE;
511 m->order = VM_NFREEORDER;
512 m->pool = VM_FREEPOOL_DEFAULT;
513 m->valid = m->dirty = 0;
517 #ifndef PMAP_HAS_PAGE_ARRAY
519 vm_page_array_alloc(vm_offset_t *vaddr, vm_paddr_t end, vm_paddr_t page_range)
524 * Reserve an unmapped guard page to trap access to vm_page_array[-1].
525 * However, because this page is allocated from KVM, out-of-bounds
526 * accesses using the direct map will not be trapped.
531 * Allocate physical memory for the page structures, and map it.
533 new_end = trunc_page(end - page_range * sizeof(struct vm_page));
534 vm_page_array = (vm_page_t)pmap_map(vaddr, new_end, end,
535 VM_PROT_READ | VM_PROT_WRITE);
536 vm_page_array_size = page_range;
545 * Initializes the resident memory module. Allocates physical memory for
546 * bootstrapping UMA and some data structures that are used to manage
547 * physical pages. Initializes these structures, and populates the free
551 vm_page_startup(vm_offset_t vaddr)
553 struct vm_phys_seg *seg;
554 struct vm_domain *vmd;
556 char *list, *listend;
557 vm_paddr_t end, high_avail, low_avail, new_end, size;
558 vm_paddr_t page_range __unused;
559 vm_paddr_t last_pa, pa, startp, endp;
561 #if MINIDUMP_PAGE_TRACKING
562 u_long vm_page_dump_size;
564 int biggestone, i, segind;
569 #if defined(__i386__) && defined(VM_PHYSSEG_DENSE)
573 vaddr = round_page(vaddr);
575 vm_phys_early_startup();
576 biggestone = vm_phys_avail_largest();
577 end = phys_avail[biggestone+1];
580 * Initialize the page and queue locks.
582 mtx_init(&vm_domainset_lock, "vm domainset lock", NULL, MTX_DEF);
583 for (i = 0; i < PA_LOCK_COUNT; i++)
584 mtx_init(&pa_lock[i], "vm page", NULL, MTX_DEF);
585 for (i = 0; i < vm_ndomains; i++)
586 vm_page_domain_init(i);
590 witness_size = round_page(witness_startup_count());
591 new_end -= witness_size;
592 mapped = pmap_map(&vaddr, new_end, new_end + witness_size,
593 VM_PROT_READ | VM_PROT_WRITE);
594 bzero((void *)mapped, witness_size);
595 witness_startup((void *)mapped);
598 #if MINIDUMP_PAGE_TRACKING
600 * Allocate a bitmap to indicate that a random physical page
601 * needs to be included in a minidump.
603 * The amd64 port needs this to indicate which direct map pages
604 * need to be dumped, via calls to dump_add_page()/dump_drop_page().
606 * However, i386 still needs this workspace internally within the
607 * minidump code. In theory, they are not needed on i386, but are
608 * included should the sf_buf code decide to use them.
611 vm_page_dump_pages = 0;
612 for (i = 0; dump_avail[i + 1] != 0; i += 2) {
613 vm_page_dump_pages += howmany(dump_avail[i + 1], PAGE_SIZE) -
614 dump_avail[i] / PAGE_SIZE;
615 if (dump_avail[i + 1] > last_pa)
616 last_pa = dump_avail[i + 1];
618 vm_page_dump_size = round_page(BITSET_SIZE(vm_page_dump_pages));
619 new_end -= vm_page_dump_size;
620 vm_page_dump = (void *)(uintptr_t)pmap_map(&vaddr, new_end,
621 new_end + vm_page_dump_size, VM_PROT_READ | VM_PROT_WRITE);
622 bzero((void *)vm_page_dump, vm_page_dump_size);
623 #if MINIDUMP_STARTUP_PAGE_TRACKING
625 * Include the UMA bootstrap pages, witness pages and vm_page_dump
626 * in a crash dump. When pmap_map() uses the direct map, they are
627 * not automatically included.
629 for (pa = new_end; pa < end; pa += PAGE_SIZE)
635 phys_avail[biggestone + 1] = new_end;
638 * Request that the physical pages underlying the message buffer be
639 * included in a crash dump. Since the message buffer is accessed
640 * through the direct map, they are not automatically included.
642 pa = DMAP_TO_PHYS((vm_offset_t)msgbufp->msg_ptr);
643 last_pa = pa + round_page(msgbufsize);
644 while (pa < last_pa) {
650 * Compute the number of pages of memory that will be available for
651 * use, taking into account the overhead of a page structure per page.
652 * In other words, solve
653 * "available physical memory" - round_page(page_range *
654 * sizeof(struct vm_page)) = page_range * PAGE_SIZE
657 low_avail = phys_avail[0];
658 high_avail = phys_avail[1];
659 for (i = 0; i < vm_phys_nsegs; i++) {
660 if (vm_phys_segs[i].start < low_avail)
661 low_avail = vm_phys_segs[i].start;
662 if (vm_phys_segs[i].end > high_avail)
663 high_avail = vm_phys_segs[i].end;
665 /* Skip the first chunk. It is already accounted for. */
666 for (i = 2; phys_avail[i + 1] != 0; i += 2) {
667 if (phys_avail[i] < low_avail)
668 low_avail = phys_avail[i];
669 if (phys_avail[i + 1] > high_avail)
670 high_avail = phys_avail[i + 1];
672 first_page = low_avail / PAGE_SIZE;
673 #ifdef VM_PHYSSEG_SPARSE
675 for (i = 0; i < vm_phys_nsegs; i++)
676 size += vm_phys_segs[i].end - vm_phys_segs[i].start;
677 for (i = 0; phys_avail[i + 1] != 0; i += 2)
678 size += phys_avail[i + 1] - phys_avail[i];
679 #elif defined(VM_PHYSSEG_DENSE)
680 size = high_avail - low_avail;
682 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
685 #ifdef PMAP_HAS_PAGE_ARRAY
686 pmap_page_array_startup(size / PAGE_SIZE);
687 biggestone = vm_phys_avail_largest();
688 end = new_end = phys_avail[biggestone + 1];
690 #ifdef VM_PHYSSEG_DENSE
692 * In the VM_PHYSSEG_DENSE case, the number of pages can account for
693 * the overhead of a page structure per page only if vm_page_array is
694 * allocated from the last physical memory chunk. Otherwise, we must
695 * allocate page structures representing the physical memory
696 * underlying vm_page_array, even though they will not be used.
698 if (new_end != high_avail)
699 page_range = size / PAGE_SIZE;
703 page_range = size / (PAGE_SIZE + sizeof(struct vm_page));
706 * If the partial bytes remaining are large enough for
707 * a page (PAGE_SIZE) without a corresponding
708 * 'struct vm_page', then new_end will contain an
709 * extra page after subtracting the length of the VM
710 * page array. Compensate by subtracting an extra
713 if (size % (PAGE_SIZE + sizeof(struct vm_page)) >= PAGE_SIZE) {
714 if (new_end == high_avail)
715 high_avail -= PAGE_SIZE;
716 new_end -= PAGE_SIZE;
720 new_end = vm_page_array_alloc(&vaddr, end, page_range);
723 #if VM_NRESERVLEVEL > 0
725 * Allocate physical memory for the reservation management system's
726 * data structures, and map it.
728 new_end = vm_reserv_startup(&vaddr, new_end);
730 #if MINIDUMP_PAGE_TRACKING && MINIDUMP_STARTUP_PAGE_TRACKING
732 * Include vm_page_array and vm_reserv_array in a crash dump.
734 for (pa = new_end; pa < end; pa += PAGE_SIZE)
737 phys_avail[biggestone + 1] = new_end;
740 * Add physical memory segments corresponding to the available
743 for (i = 0; phys_avail[i + 1] != 0; i += 2)
744 if (vm_phys_avail_size(i) != 0)
745 vm_phys_add_seg(phys_avail[i], phys_avail[i + 1]);
748 * Initialize the physical memory allocator.
753 * Initialize the page structures and add every available page to the
754 * physical memory allocator's free lists.
756 #if defined(__i386__) && defined(VM_PHYSSEG_DENSE)
757 for (ii = 0; ii < vm_page_array_size; ii++) {
758 m = &vm_page_array[ii];
759 vm_page_init_page(m, (first_page + ii) << PAGE_SHIFT, 0);
760 m->flags = PG_FICTITIOUS;
763 vm_cnt.v_page_count = 0;
764 for (segind = 0; segind < vm_phys_nsegs; segind++) {
765 seg = &vm_phys_segs[segind];
766 for (m = seg->first_page, pa = seg->start; pa < seg->end;
767 m++, pa += PAGE_SIZE)
768 vm_page_init_page(m, pa, segind);
771 * Add the segment's pages that are covered by one of
772 * phys_avail's ranges to the free lists.
774 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
775 if (seg->end <= phys_avail[i] ||
776 seg->start >= phys_avail[i + 1])
779 startp = MAX(seg->start, phys_avail[i]);
780 endp = MIN(seg->end, phys_avail[i + 1]);
781 pagecount = (u_long)atop(endp - startp);
785 m = seg->first_page + atop(startp - seg->start);
786 vmd = VM_DOMAIN(seg->domain);
787 vm_domain_free_lock(vmd);
788 vm_phys_enqueue_contig(m, pagecount);
789 vm_domain_free_unlock(vmd);
790 vm_domain_freecnt_inc(vmd, pagecount);
791 vm_cnt.v_page_count += (u_int)pagecount;
792 vmd->vmd_page_count += (u_int)pagecount;
793 vmd->vmd_segs |= 1UL << segind;
798 * Remove blacklisted pages from the physical memory allocator.
800 TAILQ_INIT(&blacklist_head);
801 vm_page_blacklist_load(&list, &listend);
802 vm_page_blacklist_check(list, listend);
804 list = kern_getenv("vm.blacklist");
805 vm_page_blacklist_check(list, NULL);
808 #if VM_NRESERVLEVEL > 0
810 * Initialize the reservation management system.
819 vm_page_reference(vm_page_t m)
822 vm_page_aflag_set(m, PGA_REFERENCED);
828 * Helper routine for grab functions to trylock busy.
830 * Returns true on success and false on failure.
833 vm_page_trybusy(vm_page_t m, int allocflags)
836 if ((allocflags & (VM_ALLOC_SBUSY | VM_ALLOC_IGN_SBUSY)) != 0)
837 return (vm_page_trysbusy(m));
839 return (vm_page_tryxbusy(m));
845 * Helper routine for grab functions to trylock busy and wire.
847 * Returns true on success and false on failure.
850 vm_page_tryacquire(vm_page_t m, int allocflags)
854 locked = vm_page_trybusy(m, allocflags);
855 if (locked && (allocflags & VM_ALLOC_WIRED) != 0)
861 * vm_page_busy_acquire:
863 * Acquire the busy lock as described by VM_ALLOC_* flags. Will loop
864 * and drop the object lock if necessary.
867 vm_page_busy_acquire(vm_page_t m, int allocflags)
873 * The page-specific object must be cached because page
874 * identity can change during the sleep, causing the
875 * re-lock of a different object.
876 * It is assumed that a reference to the object is already
877 * held by the callers.
879 obj = atomic_load_ptr(&m->object);
881 if (vm_page_tryacquire(m, allocflags))
883 if ((allocflags & VM_ALLOC_NOWAIT) != 0)
886 locked = VM_OBJECT_WOWNED(obj);
889 MPASS(locked || vm_page_wired(m));
890 if (_vm_page_busy_sleep(obj, m, m->pindex, "vmpba", allocflags,
892 VM_OBJECT_WLOCK(obj);
893 if ((allocflags & VM_ALLOC_WAITFAIL) != 0)
895 KASSERT(m->object == obj || m->object == NULL,
896 ("vm_page_busy_acquire: page %p does not belong to %p",
902 * vm_page_busy_downgrade:
904 * Downgrade an exclusive busy page into a single shared busy page.
907 vm_page_busy_downgrade(vm_page_t m)
911 vm_page_assert_xbusied(m);
913 x = vm_page_busy_fetch(m);
915 if (atomic_fcmpset_rel_int(&m->busy_lock,
916 &x, VPB_SHARERS_WORD(1)))
919 if ((x & VPB_BIT_WAITERS) != 0)
925 * vm_page_busy_tryupgrade:
927 * Attempt to upgrade a single shared busy into an exclusive busy.
930 vm_page_busy_tryupgrade(vm_page_t m)
934 vm_page_assert_sbusied(m);
936 x = vm_page_busy_fetch(m);
937 ce = VPB_CURTHREAD_EXCLUSIVE;
939 if (VPB_SHARERS(x) > 1)
941 KASSERT((x & ~VPB_BIT_WAITERS) == VPB_SHARERS_WORD(1),
942 ("vm_page_busy_tryupgrade: invalid lock state"));
943 if (!atomic_fcmpset_acq_int(&m->busy_lock, &x,
944 ce | (x & VPB_BIT_WAITERS)))
953 * Return a positive value if the page is shared busied, 0 otherwise.
956 vm_page_sbusied(vm_page_t m)
960 x = vm_page_busy_fetch(m);
961 return ((x & VPB_BIT_SHARED) != 0 && x != VPB_UNBUSIED);
967 * Shared unbusy a page.
970 vm_page_sunbusy(vm_page_t m)
974 vm_page_assert_sbusied(m);
976 x = vm_page_busy_fetch(m);
978 KASSERT(x != VPB_FREED,
979 ("vm_page_sunbusy: Unlocking freed page."));
980 if (VPB_SHARERS(x) > 1) {
981 if (atomic_fcmpset_int(&m->busy_lock, &x,
986 KASSERT((x & ~VPB_BIT_WAITERS) == VPB_SHARERS_WORD(1),
987 ("vm_page_sunbusy: invalid lock state"));
988 if (!atomic_fcmpset_rel_int(&m->busy_lock, &x, VPB_UNBUSIED))
990 if ((x & VPB_BIT_WAITERS) == 0)
998 * vm_page_busy_sleep:
1000 * Sleep if the page is busy, using the page pointer as wchan.
1001 * This is used to implement the hard-path of the busying mechanism.
1003 * If VM_ALLOC_IGN_SBUSY is specified in allocflags, the function
1004 * will not sleep if the page is shared-busy.
1006 * The object lock must be held on entry.
1008 * Returns true if it slept and dropped the object lock, or false
1009 * if there was no sleep and the lock is still held.
1012 vm_page_busy_sleep(vm_page_t m, const char *wmesg, int allocflags)
1017 VM_OBJECT_ASSERT_LOCKED(obj);
1019 return (_vm_page_busy_sleep(obj, m, m->pindex, wmesg, allocflags,
1024 * vm_page_busy_sleep_unlocked:
1026 * Sleep if the page is busy, using the page pointer as wchan.
1027 * This is used to implement the hard-path of busying mechanism.
1029 * If VM_ALLOC_IGN_SBUSY is specified in allocflags, the function
1030 * will not sleep if the page is shared-busy.
1032 * The object lock must not be held on entry. The operation will
1033 * return if the page changes identity.
1036 vm_page_busy_sleep_unlocked(vm_object_t obj, vm_page_t m, vm_pindex_t pindex,
1037 const char *wmesg, int allocflags)
1039 VM_OBJECT_ASSERT_UNLOCKED(obj);
1041 (void)_vm_page_busy_sleep(obj, m, pindex, wmesg, allocflags, false);
1045 * _vm_page_busy_sleep:
1047 * Internal busy sleep function. Verifies the page identity and
1048 * lockstate against parameters. Returns true if it sleeps and
1051 * allocflags uses VM_ALLOC_* flags to specify the lock required.
1053 * If locked is true the lock will be dropped for any true returns
1054 * and held for any false returns.
1057 _vm_page_busy_sleep(vm_object_t obj, vm_page_t m, vm_pindex_t pindex,
1058 const char *wmesg, int allocflags, bool locked)
1064 * If the object is busy we must wait for that to drain to zero
1065 * before trying the page again.
1067 if (obj != NULL && vm_object_busied(obj)) {
1069 VM_OBJECT_DROP(obj);
1070 vm_object_busy_wait(obj, wmesg);
1074 if (!vm_page_busied(m))
1077 xsleep = (allocflags & (VM_ALLOC_SBUSY | VM_ALLOC_IGN_SBUSY)) != 0;
1079 x = vm_page_busy_fetch(m);
1082 * If the page changes objects or becomes unlocked we can
1085 if (x == VPB_UNBUSIED ||
1086 (xsleep && (x & VPB_BIT_SHARED) != 0) ||
1087 m->object != obj || m->pindex != pindex) {
1091 if ((x & VPB_BIT_WAITERS) != 0)
1093 } while (!atomic_fcmpset_int(&m->busy_lock, &x, x | VPB_BIT_WAITERS));
1095 VM_OBJECT_DROP(obj);
1097 sleepq_add(m, NULL, wmesg, 0, 0);
1098 sleepq_wait(m, PVM);
1106 * Try to shared busy a page.
1107 * If the operation succeeds 1 is returned otherwise 0.
1108 * The operation never sleeps.
1111 vm_page_trysbusy(vm_page_t m)
1117 x = vm_page_busy_fetch(m);
1119 if ((x & VPB_BIT_SHARED) == 0)
1122 * Reduce the window for transient busies that will trigger
1123 * false negatives in vm_page_ps_test().
1125 if (obj != NULL && vm_object_busied(obj))
1127 if (atomic_fcmpset_acq_int(&m->busy_lock, &x,
1128 x + VPB_ONE_SHARER))
1132 /* Refetch the object now that we're guaranteed that it is stable. */
1134 if (obj != NULL && vm_object_busied(obj)) {
1144 * Try to exclusive busy a page.
1145 * If the operation succeeds 1 is returned otherwise 0.
1146 * The operation never sleeps.
1149 vm_page_tryxbusy(vm_page_t m)
1153 if (atomic_cmpset_acq_int(&m->busy_lock, VPB_UNBUSIED,
1154 VPB_CURTHREAD_EXCLUSIVE) == 0)
1158 if (obj != NULL && vm_object_busied(obj)) {
1166 vm_page_xunbusy_hard_tail(vm_page_t m)
1168 atomic_store_rel_int(&m->busy_lock, VPB_UNBUSIED);
1169 /* Wake the waiter. */
1174 * vm_page_xunbusy_hard:
1176 * Called when unbusy has failed because there is a waiter.
1179 vm_page_xunbusy_hard(vm_page_t m)
1181 vm_page_assert_xbusied(m);
1182 vm_page_xunbusy_hard_tail(m);
1186 vm_page_xunbusy_hard_unchecked(vm_page_t m)
1188 vm_page_assert_xbusied_unchecked(m);
1189 vm_page_xunbusy_hard_tail(m);
1193 vm_page_busy_free(vm_page_t m)
1197 atomic_thread_fence_rel();
1198 x = atomic_swap_int(&m->busy_lock, VPB_FREED);
1199 if ((x & VPB_BIT_WAITERS) != 0)
1204 * vm_page_unhold_pages:
1206 * Unhold each of the pages that is referenced by the given array.
1209 vm_page_unhold_pages(vm_page_t *ma, int count)
1212 for (; count != 0; count--) {
1213 vm_page_unwire(*ma, PQ_ACTIVE);
1219 PHYS_TO_VM_PAGE(vm_paddr_t pa)
1223 #ifdef VM_PHYSSEG_SPARSE
1224 m = vm_phys_paddr_to_vm_page(pa);
1226 m = vm_phys_fictitious_to_vm_page(pa);
1228 #elif defined(VM_PHYSSEG_DENSE)
1232 if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
1233 m = &vm_page_array[pi - first_page];
1236 return (vm_phys_fictitious_to_vm_page(pa));
1238 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
1245 * Create a fictitious page with the specified physical address and
1246 * memory attribute. The memory attribute is the only the machine-
1247 * dependent aspect of a fictitious page that must be initialized.
1250 vm_page_getfake(vm_paddr_t paddr, vm_memattr_t memattr)
1254 m = uma_zalloc(fakepg_zone, M_WAITOK | M_ZERO);
1255 vm_page_initfake(m, paddr, memattr);
1260 vm_page_initfake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
1263 if ((m->flags & PG_FICTITIOUS) != 0) {
1265 * The page's memattr might have changed since the
1266 * previous initialization. Update the pmap to the
1271 m->phys_addr = paddr;
1272 m->a.queue = PQ_NONE;
1273 /* Fictitious pages don't use "segind". */
1274 m->flags = PG_FICTITIOUS;
1275 /* Fictitious pages don't use "order" or "pool". */
1276 m->oflags = VPO_UNMANAGED;
1277 m->busy_lock = VPB_CURTHREAD_EXCLUSIVE;
1278 /* Fictitious pages are unevictable. */
1282 pmap_page_set_memattr(m, memattr);
1288 * Release a fictitious page.
1291 vm_page_putfake(vm_page_t m)
1294 KASSERT((m->oflags & VPO_UNMANAGED) != 0, ("managed %p", m));
1295 KASSERT((m->flags & PG_FICTITIOUS) != 0,
1296 ("vm_page_putfake: bad page %p", m));
1297 vm_page_assert_xbusied(m);
1298 vm_page_busy_free(m);
1299 uma_zfree(fakepg_zone, m);
1303 * vm_page_updatefake:
1305 * Update the given fictitious page to the specified physical address and
1309 vm_page_updatefake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
1312 KASSERT((m->flags & PG_FICTITIOUS) != 0,
1313 ("vm_page_updatefake: bad page %p", m));
1314 m->phys_addr = paddr;
1315 pmap_page_set_memattr(m, memattr);
1324 vm_page_free(vm_page_t m)
1327 m->flags &= ~PG_ZERO;
1328 vm_page_free_toq(m);
1332 * vm_page_free_zero:
1334 * Free a page to the zerod-pages queue
1337 vm_page_free_zero(vm_page_t m)
1340 m->flags |= PG_ZERO;
1341 vm_page_free_toq(m);
1345 * Unbusy and handle the page queueing for a page from a getpages request that
1346 * was optionally read ahead or behind.
1349 vm_page_readahead_finish(vm_page_t m)
1352 /* We shouldn't put invalid pages on queues. */
1353 KASSERT(!vm_page_none_valid(m), ("%s: %p is invalid", __func__, m));
1356 * Since the page is not the actually needed one, whether it should
1357 * be activated or deactivated is not obvious. Empirical results
1358 * have shown that deactivating the page is usually the best choice,
1359 * unless the page is wanted by another thread.
1361 if ((vm_page_busy_fetch(m) & VPB_BIT_WAITERS) != 0)
1362 vm_page_activate(m);
1364 vm_page_deactivate(m);
1365 vm_page_xunbusy_unchecked(m);
1369 * Destroy the identity of an invalid page and free it if possible.
1370 * This is intended to be used when reading a page from backing store fails.
1373 vm_page_free_invalid(vm_page_t m)
1376 KASSERT(vm_page_none_valid(m), ("page %p is valid", m));
1377 KASSERT(!pmap_page_is_mapped(m), ("page %p is mapped", m));
1378 KASSERT(m->object != NULL, ("page %p has no object", m));
1379 VM_OBJECT_ASSERT_WLOCKED(m->object);
1382 * We may be attempting to free the page as part of the handling for an
1383 * I/O error, in which case the page was xbusied by a different thread.
1385 vm_page_xbusy_claim(m);
1388 * If someone has wired this page while the object lock
1389 * was not held, then the thread that unwires is responsible
1390 * for freeing the page. Otherwise just free the page now.
1391 * The wire count of this unmapped page cannot change while
1392 * we have the page xbusy and the page's object wlocked.
1394 if (vm_page_remove(m))
1399 * vm_page_dirty_KBI: [ internal use only ]
1401 * Set all bits in the page's dirty field.
1403 * The object containing the specified page must be locked if the
1404 * call is made from the machine-independent layer.
1406 * See vm_page_clear_dirty_mask().
1408 * This function should only be called by vm_page_dirty().
1411 vm_page_dirty_KBI(vm_page_t m)
1414 /* Refer to this operation by its public name. */
1415 KASSERT(vm_page_all_valid(m), ("vm_page_dirty: page is invalid!"));
1416 m->dirty = VM_PAGE_BITS_ALL;
1420 * vm_page_insert: [ internal use only ]
1422 * Inserts the given mem entry into the object and object list.
1424 * The object must be locked.
1427 vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex)
1431 VM_OBJECT_ASSERT_WLOCKED(object);
1432 mpred = vm_radix_lookup_le(&object->rtree, pindex);
1433 return (vm_page_insert_after(m, object, pindex, mpred));
1437 * vm_page_insert_after:
1439 * Inserts the page "m" into the specified object at offset "pindex".
1441 * The page "mpred" must immediately precede the offset "pindex" within
1442 * the specified object.
1444 * The object must be locked.
1447 vm_page_insert_after(vm_page_t m, vm_object_t object, vm_pindex_t pindex,
1452 VM_OBJECT_ASSERT_WLOCKED(object);
1453 KASSERT(m->object == NULL,
1454 ("vm_page_insert_after: page already inserted"));
1455 if (mpred != NULL) {
1456 KASSERT(mpred->object == object,
1457 ("vm_page_insert_after: object doesn't contain mpred"));
1458 KASSERT(mpred->pindex < pindex,
1459 ("vm_page_insert_after: mpred doesn't precede pindex"));
1460 msucc = TAILQ_NEXT(mpred, listq);
1462 msucc = TAILQ_FIRST(&object->memq);
1464 KASSERT(msucc->pindex > pindex,
1465 ("vm_page_insert_after: msucc doesn't succeed pindex"));
1468 * Record the object/offset pair in this page.
1472 m->ref_count |= VPRC_OBJREF;
1475 * Now link into the object's ordered list of backed pages.
1477 if (vm_radix_insert(&object->rtree, m)) {
1480 m->ref_count &= ~VPRC_OBJREF;
1483 vm_page_insert_radixdone(m, object, mpred);
1484 vm_pager_page_inserted(object, m);
1489 * vm_page_insert_radixdone:
1491 * Complete page "m" insertion into the specified object after the
1492 * radix trie hooking.
1494 * The page "mpred" must precede the offset "m->pindex" within the
1497 * The object must be locked.
1500 vm_page_insert_radixdone(vm_page_t m, vm_object_t object, vm_page_t mpred)
1503 VM_OBJECT_ASSERT_WLOCKED(object);
1504 KASSERT(object != NULL && m->object == object,
1505 ("vm_page_insert_radixdone: page %p has inconsistent object", m));
1506 KASSERT((m->ref_count & VPRC_OBJREF) != 0,
1507 ("vm_page_insert_radixdone: page %p is missing object ref", m));
1508 if (mpred != NULL) {
1509 KASSERT(mpred->object == object,
1510 ("vm_page_insert_radixdone: object doesn't contain mpred"));
1511 KASSERT(mpred->pindex < m->pindex,
1512 ("vm_page_insert_radixdone: mpred doesn't precede pindex"));
1516 TAILQ_INSERT_AFTER(&object->memq, mpred, m, listq);
1518 TAILQ_INSERT_HEAD(&object->memq, m, listq);
1521 * Show that the object has one more resident page.
1523 object->resident_page_count++;
1526 * Hold the vnode until the last page is released.
1528 if (object->resident_page_count == 1 && object->type == OBJT_VNODE)
1529 vhold(object->handle);
1532 * Since we are inserting a new and possibly dirty page,
1533 * update the object's generation count.
1535 if (pmap_page_is_write_mapped(m))
1536 vm_object_set_writeable_dirty(object);
1540 * Do the work to remove a page from its object. The caller is responsible for
1541 * updating the page's fields to reflect this removal.
1544 vm_page_object_remove(vm_page_t m)
1547 vm_page_t mrem __diagused;
1549 vm_page_assert_xbusied(m);
1551 VM_OBJECT_ASSERT_WLOCKED(object);
1552 KASSERT((m->ref_count & VPRC_OBJREF) != 0,
1553 ("page %p is missing its object ref", m));
1555 /* Deferred free of swap space. */
1556 if ((m->a.flags & PGA_SWAP_FREE) != 0)
1557 vm_pager_page_unswapped(m);
1559 vm_pager_page_removed(object, m);
1562 mrem = vm_radix_remove(&object->rtree, m->pindex);
1563 KASSERT(mrem == m, ("removed page %p, expected page %p", mrem, m));
1566 * Now remove from the object's list of backed pages.
1568 TAILQ_REMOVE(&object->memq, m, listq);
1571 * And show that the object has one fewer resident page.
1573 object->resident_page_count--;
1576 * The vnode may now be recycled.
1578 if (object->resident_page_count == 0 && object->type == OBJT_VNODE)
1579 vdrop(object->handle);
1585 * Removes the specified page from its containing object, but does not
1586 * invalidate any backing storage. Returns true if the object's reference
1587 * was the last reference to the page, and false otherwise.
1589 * The object must be locked and the page must be exclusively busied.
1590 * The exclusive busy will be released on return. If this is not the
1591 * final ref and the caller does not hold a wire reference it may not
1592 * continue to access the page.
1595 vm_page_remove(vm_page_t m)
1599 dropped = vm_page_remove_xbusy(m);
1606 * vm_page_remove_xbusy
1608 * Removes the page but leaves the xbusy held. Returns true if this
1609 * removed the final ref and false otherwise.
1612 vm_page_remove_xbusy(vm_page_t m)
1615 vm_page_object_remove(m);
1616 return (vm_page_drop(m, VPRC_OBJREF) == VPRC_OBJREF);
1622 * Returns the page associated with the object/offset
1623 * pair specified; if none is found, NULL is returned.
1625 * The object must be locked.
1628 vm_page_lookup(vm_object_t object, vm_pindex_t pindex)
1631 VM_OBJECT_ASSERT_LOCKED(object);
1632 return (vm_radix_lookup(&object->rtree, pindex));
1636 * vm_page_lookup_unlocked:
1638 * Returns the page associated with the object/offset pair specified;
1639 * if none is found, NULL is returned. The page may be no longer be
1640 * present in the object at the time that this function returns. Only
1641 * useful for opportunistic checks such as inmem().
1644 vm_page_lookup_unlocked(vm_object_t object, vm_pindex_t pindex)
1647 return (vm_radix_lookup_unlocked(&object->rtree, pindex));
1653 * Returns a page that must already have been busied by
1654 * the caller. Used for bogus page replacement.
1657 vm_page_relookup(vm_object_t object, vm_pindex_t pindex)
1661 m = vm_radix_lookup_unlocked(&object->rtree, pindex);
1662 KASSERT(m != NULL && (vm_page_busied(m) || vm_page_wired(m)) &&
1663 m->object == object && m->pindex == pindex,
1664 ("vm_page_relookup: Invalid page %p", m));
1669 * This should only be used by lockless functions for releasing transient
1670 * incorrect acquires. The page may have been freed after we acquired a
1671 * busy lock. In this case busy_lock == VPB_FREED and we have nothing
1675 vm_page_busy_release(vm_page_t m)
1679 x = vm_page_busy_fetch(m);
1683 if ((x & VPB_BIT_SHARED) != 0 && VPB_SHARERS(x) > 1) {
1684 if (atomic_fcmpset_int(&m->busy_lock, &x,
1685 x - VPB_ONE_SHARER))
1689 KASSERT((x & VPB_BIT_SHARED) != 0 ||
1690 (x & ~VPB_BIT_WAITERS) == VPB_CURTHREAD_EXCLUSIVE,
1691 ("vm_page_busy_release: %p xbusy not owned.", m));
1692 if (!atomic_fcmpset_rel_int(&m->busy_lock, &x, VPB_UNBUSIED))
1694 if ((x & VPB_BIT_WAITERS) != 0)
1701 * vm_page_find_least:
1703 * Returns the page associated with the object with least pindex
1704 * greater than or equal to the parameter pindex, or NULL.
1706 * The object must be locked.
1709 vm_page_find_least(vm_object_t object, vm_pindex_t pindex)
1713 VM_OBJECT_ASSERT_LOCKED(object);
1714 if ((m = TAILQ_FIRST(&object->memq)) != NULL && m->pindex < pindex)
1715 m = vm_radix_lookup_ge(&object->rtree, pindex);
1720 * Returns the given page's successor (by pindex) within the object if it is
1721 * resident; if none is found, NULL is returned.
1723 * The object must be locked.
1726 vm_page_next(vm_page_t m)
1730 VM_OBJECT_ASSERT_LOCKED(m->object);
1731 if ((next = TAILQ_NEXT(m, listq)) != NULL) {
1732 MPASS(next->object == m->object);
1733 if (next->pindex != m->pindex + 1)
1740 * Returns the given page's predecessor (by pindex) within the object if it is
1741 * resident; if none is found, NULL is returned.
1743 * The object must be locked.
1746 vm_page_prev(vm_page_t m)
1750 VM_OBJECT_ASSERT_LOCKED(m->object);
1751 if ((prev = TAILQ_PREV(m, pglist, listq)) != NULL) {
1752 MPASS(prev->object == m->object);
1753 if (prev->pindex != m->pindex - 1)
1760 * Uses the page mnew as a replacement for an existing page at index
1761 * pindex which must be already present in the object.
1763 * Both pages must be exclusively busied on enter. The old page is
1766 * A return value of true means mold is now free. If this is not the
1767 * final ref and the caller does not hold a wire reference it may not
1768 * continue to access the page.
1771 vm_page_replace_hold(vm_page_t mnew, vm_object_t object, vm_pindex_t pindex,
1774 vm_page_t mret __diagused;
1777 VM_OBJECT_ASSERT_WLOCKED(object);
1778 vm_page_assert_xbusied(mold);
1779 KASSERT(mnew->object == NULL && (mnew->ref_count & VPRC_OBJREF) == 0,
1780 ("vm_page_replace: page %p already in object", mnew));
1783 * This function mostly follows vm_page_insert() and
1784 * vm_page_remove() without the radix, object count and vnode
1785 * dance. Double check such functions for more comments.
1788 mnew->object = object;
1789 mnew->pindex = pindex;
1790 atomic_set_int(&mnew->ref_count, VPRC_OBJREF);
1791 mret = vm_radix_replace(&object->rtree, mnew);
1792 KASSERT(mret == mold,
1793 ("invalid page replacement, mold=%p, mret=%p", mold, mret));
1794 KASSERT((mold->oflags & VPO_UNMANAGED) ==
1795 (mnew->oflags & VPO_UNMANAGED),
1796 ("vm_page_replace: mismatched VPO_UNMANAGED"));
1798 /* Keep the resident page list in sorted order. */
1799 TAILQ_INSERT_AFTER(&object->memq, mold, mnew, listq);
1800 TAILQ_REMOVE(&object->memq, mold, listq);
1801 mold->object = NULL;
1804 * The object's resident_page_count does not change because we have
1805 * swapped one page for another, but the generation count should
1806 * change if the page is dirty.
1808 if (pmap_page_is_write_mapped(mnew))
1809 vm_object_set_writeable_dirty(object);
1810 dropped = vm_page_drop(mold, VPRC_OBJREF) == VPRC_OBJREF;
1811 vm_page_xunbusy(mold);
1817 vm_page_replace(vm_page_t mnew, vm_object_t object, vm_pindex_t pindex,
1821 vm_page_assert_xbusied(mnew);
1823 if (vm_page_replace_hold(mnew, object, pindex, mold))
1830 * Move the given memory entry from its
1831 * current object to the specified target object/offset.
1833 * Note: swap associated with the page must be invalidated by the move. We
1834 * have to do this for several reasons: (1) we aren't freeing the
1835 * page, (2) we are dirtying the page, (3) the VM system is probably
1836 * moving the page from object A to B, and will then later move
1837 * the backing store from A to B and we can't have a conflict.
1839 * Note: we *always* dirty the page. It is necessary both for the
1840 * fact that we moved it, and because we may be invalidating
1843 * The objects must be locked.
1846 vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex)
1851 VM_OBJECT_ASSERT_WLOCKED(new_object);
1853 KASSERT(m->ref_count != 0, ("vm_page_rename: page %p has no refs", m));
1854 mpred = vm_radix_lookup_le(&new_object->rtree, new_pindex);
1855 KASSERT(mpred == NULL || mpred->pindex != new_pindex,
1856 ("vm_page_rename: pindex already renamed"));
1859 * Create a custom version of vm_page_insert() which does not depend
1860 * by m_prev and can cheat on the implementation aspects of the
1864 m->pindex = new_pindex;
1865 if (vm_radix_insert(&new_object->rtree, m)) {
1871 * The operation cannot fail anymore. The removal must happen before
1872 * the listq iterator is tainted.
1875 vm_page_object_remove(m);
1877 /* Return back to the new pindex to complete vm_page_insert(). */
1878 m->pindex = new_pindex;
1879 m->object = new_object;
1881 vm_page_insert_radixdone(m, new_object, mpred);
1883 vm_pager_page_inserted(new_object, m);
1890 * Allocate and return a page that is associated with the specified
1891 * object and offset pair. By default, this page is exclusive busied.
1893 * The caller must always specify an allocation class.
1895 * allocation classes:
1896 * VM_ALLOC_NORMAL normal process request
1897 * VM_ALLOC_SYSTEM system *really* needs a page
1898 * VM_ALLOC_INTERRUPT interrupt time request
1900 * optional allocation flags:
1901 * VM_ALLOC_COUNT(number) the number of additional pages that the caller
1902 * intends to allocate
1903 * VM_ALLOC_NOBUSY do not exclusive busy the page
1904 * VM_ALLOC_NODUMP do not include the page in a kernel core dump
1905 * VM_ALLOC_SBUSY shared busy the allocated page
1906 * VM_ALLOC_WIRED wire the allocated page
1907 * VM_ALLOC_ZERO prefer a zeroed page
1910 vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req)
1913 return (vm_page_alloc_after(object, pindex, req,
1914 vm_radix_lookup_le(&object->rtree, pindex)));
1918 vm_page_alloc_domain(vm_object_t object, vm_pindex_t pindex, int domain,
1922 return (vm_page_alloc_domain_after(object, pindex, domain, req,
1923 vm_radix_lookup_le(&object->rtree, pindex)));
1927 * Allocate a page in the specified object with the given page index. To
1928 * optimize insertion of the page into the object, the caller must also specifiy
1929 * the resident page in the object with largest index smaller than the given
1930 * page index, or NULL if no such page exists.
1933 vm_page_alloc_after(vm_object_t object, vm_pindex_t pindex,
1934 int req, vm_page_t mpred)
1936 struct vm_domainset_iter di;
1940 vm_domainset_iter_page_init(&di, object, pindex, &domain, &req);
1942 m = vm_page_alloc_domain_after(object, pindex, domain, req,
1946 } while (vm_domainset_iter_page(&di, object, &domain) == 0);
1952 * Returns true if the number of free pages exceeds the minimum
1953 * for the request class and false otherwise.
1956 _vm_domain_allocate(struct vm_domain *vmd, int req_class, int npages)
1958 u_int limit, old, new;
1960 if (req_class == VM_ALLOC_INTERRUPT)
1962 else if (req_class == VM_ALLOC_SYSTEM)
1963 limit = vmd->vmd_interrupt_free_min;
1965 limit = vmd->vmd_free_reserved;
1968 * Attempt to reserve the pages. Fail if we're below the limit.
1971 old = vmd->vmd_free_count;
1976 } while (atomic_fcmpset_int(&vmd->vmd_free_count, &old, new) == 0);
1978 /* Wake the page daemon if we've crossed the threshold. */
1979 if (vm_paging_needed(vmd, new) && !vm_paging_needed(vmd, old))
1980 pagedaemon_wakeup(vmd->vmd_domain);
1982 /* Only update bitsets on transitions. */
1983 if ((old >= vmd->vmd_free_min && new < vmd->vmd_free_min) ||
1984 (old >= vmd->vmd_free_severe && new < vmd->vmd_free_severe))
1991 vm_domain_allocate(struct vm_domain *vmd, int req, int npages)
1996 * The page daemon is allowed to dig deeper into the free page list.
1998 req_class = req & VM_ALLOC_CLASS_MASK;
1999 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
2000 req_class = VM_ALLOC_SYSTEM;
2001 return (_vm_domain_allocate(vmd, req_class, npages));
2005 vm_page_alloc_domain_after(vm_object_t object, vm_pindex_t pindex, int domain,
2006 int req, vm_page_t mpred)
2008 struct vm_domain *vmd;
2012 #define VPA_FLAGS (VM_ALLOC_CLASS_MASK | VM_ALLOC_WAITFAIL | \
2013 VM_ALLOC_NOWAIT | VM_ALLOC_NOBUSY | \
2014 VM_ALLOC_SBUSY | VM_ALLOC_WIRED | \
2015 VM_ALLOC_NODUMP | VM_ALLOC_ZERO | VM_ALLOC_COUNT_MASK)
2016 KASSERT((req & ~VPA_FLAGS) == 0,
2017 ("invalid request %#x", req));
2018 KASSERT(((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) !=
2019 (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)),
2020 ("invalid request %#x", req));
2021 KASSERT(mpred == NULL || mpred->pindex < pindex,
2022 ("mpred %p doesn't precede pindex 0x%jx", mpred,
2023 (uintmax_t)pindex));
2024 VM_OBJECT_ASSERT_WLOCKED(object);
2028 if (!vm_pager_can_alloc_page(object, pindex))
2031 #if VM_NRESERVLEVEL > 0
2033 * Can we allocate the page from a reservation?
2035 if (vm_object_reserv(object) &&
2036 (m = vm_reserv_alloc_page(object, pindex, domain, req, mpred)) !=
2041 vmd = VM_DOMAIN(domain);
2042 if (vmd->vmd_pgcache[VM_FREEPOOL_DEFAULT].zone != NULL) {
2043 m = uma_zalloc(vmd->vmd_pgcache[VM_FREEPOOL_DEFAULT].zone,
2046 flags |= PG_PCPU_CACHE;
2050 if (vm_domain_allocate(vmd, req, 1)) {
2052 * If not, allocate it from the free page queues.
2054 vm_domain_free_lock(vmd);
2055 m = vm_phys_alloc_pages(domain, VM_FREEPOOL_DEFAULT, 0);
2056 vm_domain_free_unlock(vmd);
2058 vm_domain_freecnt_inc(vmd, 1);
2059 #if VM_NRESERVLEVEL > 0
2060 if (vm_reserv_reclaim_inactive(domain))
2067 * Not allocatable, give up.
2069 if (vm_domain_alloc_fail(vmd, object, req))
2075 * At this point we had better have found a good page.
2079 vm_page_alloc_check(m);
2082 * Initialize the page. Only the PG_ZERO flag is inherited.
2084 flags |= m->flags & PG_ZERO;
2085 if ((req & VM_ALLOC_NODUMP) != 0)
2089 m->oflags = (object->flags & OBJ_UNMANAGED) != 0 ? VPO_UNMANAGED : 0;
2090 if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) == 0)
2091 m->busy_lock = VPB_CURTHREAD_EXCLUSIVE;
2092 else if ((req & VM_ALLOC_SBUSY) != 0)
2093 m->busy_lock = VPB_SHARERS_WORD(1);
2095 m->busy_lock = VPB_UNBUSIED;
2096 if (req & VM_ALLOC_WIRED) {
2102 if (vm_page_insert_after(m, object, pindex, mpred)) {
2103 if (req & VM_ALLOC_WIRED) {
2107 KASSERT(m->object == NULL, ("page %p has object", m));
2108 m->oflags = VPO_UNMANAGED;
2109 m->busy_lock = VPB_UNBUSIED;
2110 /* Don't change PG_ZERO. */
2111 vm_page_free_toq(m);
2112 if (req & VM_ALLOC_WAITFAIL) {
2113 VM_OBJECT_WUNLOCK(object);
2115 VM_OBJECT_WLOCK(object);
2120 /* Ignore device objects; the pager sets "memattr" for them. */
2121 if (object->memattr != VM_MEMATTR_DEFAULT &&
2122 (object->flags & OBJ_FICTITIOUS) == 0)
2123 pmap_page_set_memattr(m, object->memattr);
2129 * vm_page_alloc_contig:
2131 * Allocate a contiguous set of physical pages of the given size "npages"
2132 * from the free lists. All of the physical pages must be at or above
2133 * the given physical address "low" and below the given physical address
2134 * "high". The given value "alignment" determines the alignment of the
2135 * first physical page in the set. If the given value "boundary" is
2136 * non-zero, then the set of physical pages cannot cross any physical
2137 * address boundary that is a multiple of that value. Both "alignment"
2138 * and "boundary" must be a power of two.
2140 * If the specified memory attribute, "memattr", is VM_MEMATTR_DEFAULT,
2141 * then the memory attribute setting for the physical pages is configured
2142 * to the object's memory attribute setting. Otherwise, the memory
2143 * attribute setting for the physical pages is configured to "memattr",
2144 * overriding the object's memory attribute setting. However, if the
2145 * object's memory attribute setting is not VM_MEMATTR_DEFAULT, then the
2146 * memory attribute setting for the physical pages cannot be configured
2147 * to VM_MEMATTR_DEFAULT.
2149 * The specified object may not contain fictitious pages.
2151 * The caller must always specify an allocation class.
2153 * allocation classes:
2154 * VM_ALLOC_NORMAL normal process request
2155 * VM_ALLOC_SYSTEM system *really* needs a page
2156 * VM_ALLOC_INTERRUPT interrupt time request
2158 * optional allocation flags:
2159 * VM_ALLOC_NOBUSY do not exclusive busy the page
2160 * VM_ALLOC_NODUMP do not include the page in a kernel core dump
2161 * VM_ALLOC_SBUSY shared busy the allocated page
2162 * VM_ALLOC_WIRED wire the allocated page
2163 * VM_ALLOC_ZERO prefer a zeroed page
2166 vm_page_alloc_contig(vm_object_t object, vm_pindex_t pindex, int req,
2167 u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
2168 vm_paddr_t boundary, vm_memattr_t memattr)
2170 struct vm_domainset_iter di;
2171 vm_page_t bounds[2];
2178 vm_domainset_iter_page_init(&di, object, pindex, &domain, &req);
2180 m = vm_page_alloc_contig_domain(object, pindex, domain, req,
2181 npages, low, high, alignment, boundary, memattr);
2184 if (start_segind == -1)
2185 start_segind = vm_phys_lookup_segind(low);
2186 if (vm_phys_find_range(bounds, start_segind, domain,
2187 npages, low, high) == -1) {
2188 vm_domainset_iter_ignore(&di, domain);
2190 } while (vm_domainset_iter_page(&di, object, &domain) == 0);
2196 vm_page_find_contig_domain(int domain, int req, u_long npages, vm_paddr_t low,
2197 vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
2199 struct vm_domain *vmd;
2203 * Can we allocate the pages without the number of free pages falling
2204 * below the lower bound for the allocation class?
2206 vmd = VM_DOMAIN(domain);
2207 if (!vm_domain_allocate(vmd, req, npages))
2210 * Try to allocate the pages from the free page queues.
2212 vm_domain_free_lock(vmd);
2213 m_ret = vm_phys_alloc_contig(domain, npages, low, high,
2214 alignment, boundary);
2215 vm_domain_free_unlock(vmd);
2218 #if VM_NRESERVLEVEL > 0
2220 * Try to break a reservation to allocate the pages.
2222 if ((req & VM_ALLOC_NORECLAIM) == 0) {
2223 m_ret = vm_reserv_reclaim_contig(domain, npages, low,
2224 high, alignment, boundary);
2229 vm_domain_freecnt_inc(vmd, npages);
2234 vm_page_alloc_contig_domain(vm_object_t object, vm_pindex_t pindex, int domain,
2235 int req, u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
2236 vm_paddr_t boundary, vm_memattr_t memattr)
2238 vm_page_t m, m_ret, mpred;
2239 u_int busy_lock, flags, oflags;
2241 #define VPAC_FLAGS (VPA_FLAGS | VM_ALLOC_NORECLAIM)
2242 KASSERT((req & ~VPAC_FLAGS) == 0,
2243 ("invalid request %#x", req));
2244 KASSERT(((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) !=
2245 (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)),
2246 ("invalid request %#x", req));
2247 KASSERT((req & (VM_ALLOC_WAITOK | VM_ALLOC_NORECLAIM)) !=
2248 (VM_ALLOC_WAITOK | VM_ALLOC_NORECLAIM),
2249 ("invalid request %#x", req));
2250 VM_OBJECT_ASSERT_WLOCKED(object);
2251 KASSERT((object->flags & OBJ_FICTITIOUS) == 0,
2252 ("vm_page_alloc_contig: object %p has fictitious pages",
2254 KASSERT(npages > 0, ("vm_page_alloc_contig: npages is zero"));
2256 mpred = vm_radix_lookup_le(&object->rtree, pindex);
2257 KASSERT(mpred == NULL || mpred->pindex != pindex,
2258 ("vm_page_alloc_contig: pindex already allocated"));
2260 #if VM_NRESERVLEVEL > 0
2262 * Can we allocate the pages from a reservation?
2264 if (vm_object_reserv(object) &&
2265 (m_ret = vm_reserv_alloc_contig(object, pindex, domain, req,
2266 mpred, npages, low, high, alignment, boundary)) != NULL) {
2270 if ((m_ret = vm_page_find_contig_domain(domain, req, npages,
2271 low, high, alignment, boundary)) != NULL)
2273 if (!vm_domain_alloc_fail(VM_DOMAIN(domain), object, req))
2276 for (m = m_ret; m < &m_ret[npages]; m++) {
2278 vm_page_alloc_check(m);
2282 * Initialize the pages. Only the PG_ZERO flag is inherited.
2285 if ((req & VM_ALLOC_NODUMP) != 0)
2287 oflags = (object->flags & OBJ_UNMANAGED) != 0 ? VPO_UNMANAGED : 0;
2288 if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) == 0)
2289 busy_lock = VPB_CURTHREAD_EXCLUSIVE;
2290 else if ((req & VM_ALLOC_SBUSY) != 0)
2291 busy_lock = VPB_SHARERS_WORD(1);
2293 busy_lock = VPB_UNBUSIED;
2294 if ((req & VM_ALLOC_WIRED) != 0)
2295 vm_wire_add(npages);
2296 if (object->memattr != VM_MEMATTR_DEFAULT &&
2297 memattr == VM_MEMATTR_DEFAULT)
2298 memattr = object->memattr;
2299 for (m = m_ret; m < &m_ret[npages]; m++) {
2301 m->flags = (m->flags | PG_NODUMP) & flags;
2302 m->busy_lock = busy_lock;
2303 if ((req & VM_ALLOC_WIRED) != 0)
2307 if (vm_page_insert_after(m, object, pindex, mpred)) {
2308 if ((req & VM_ALLOC_WIRED) != 0)
2309 vm_wire_sub(npages);
2310 KASSERT(m->object == NULL,
2311 ("page %p has object", m));
2313 for (m = m_ret; m < &m_ret[npages]; m++) {
2315 (req & VM_ALLOC_WIRED) != 0)
2317 m->oflags = VPO_UNMANAGED;
2318 m->busy_lock = VPB_UNBUSIED;
2319 /* Don't change PG_ZERO. */
2320 vm_page_free_toq(m);
2322 if (req & VM_ALLOC_WAITFAIL) {
2323 VM_OBJECT_WUNLOCK(object);
2325 VM_OBJECT_WLOCK(object);
2330 if (memattr != VM_MEMATTR_DEFAULT)
2331 pmap_page_set_memattr(m, memattr);
2338 * Allocate a physical page that is not intended to be inserted into a VM
2339 * object. If the "freelist" parameter is not equal to VM_NFREELIST, then only
2340 * pages from the specified vm_phys freelist will be returned.
2342 static __always_inline vm_page_t
2343 _vm_page_alloc_noobj_domain(int domain, const int freelist, int req)
2345 struct vm_domain *vmd;
2349 #define VPAN_FLAGS (VM_ALLOC_CLASS_MASK | VM_ALLOC_WAITFAIL | \
2350 VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | \
2351 VM_ALLOC_NOBUSY | VM_ALLOC_WIRED | \
2352 VM_ALLOC_NODUMP | VM_ALLOC_ZERO | VM_ALLOC_COUNT_MASK)
2353 KASSERT((req & ~VPAN_FLAGS) == 0,
2354 ("invalid request %#x", req));
2356 flags = (req & VM_ALLOC_NODUMP) != 0 ? PG_NODUMP : 0;
2357 vmd = VM_DOMAIN(domain);
2359 if (freelist == VM_NFREELIST &&
2360 vmd->vmd_pgcache[VM_FREEPOOL_DIRECT].zone != NULL) {
2361 m = uma_zalloc(vmd->vmd_pgcache[VM_FREEPOOL_DIRECT].zone,
2364 flags |= PG_PCPU_CACHE;
2369 if (vm_domain_allocate(vmd, req, 1)) {
2370 vm_domain_free_lock(vmd);
2371 if (freelist == VM_NFREELIST)
2372 m = vm_phys_alloc_pages(domain, VM_FREEPOOL_DIRECT, 0);
2374 m = vm_phys_alloc_freelist_pages(domain, freelist,
2375 VM_FREEPOOL_DIRECT, 0);
2376 vm_domain_free_unlock(vmd);
2378 vm_domain_freecnt_inc(vmd, 1);
2379 #if VM_NRESERVLEVEL > 0
2380 if (freelist == VM_NFREELIST &&
2381 vm_reserv_reclaim_inactive(domain))
2387 if (vm_domain_alloc_fail(vmd, NULL, req))
2394 vm_page_alloc_check(m);
2397 * Consumers should not rely on a useful default pindex value.
2399 m->pindex = 0xdeadc0dedeadc0de;
2400 m->flags = (m->flags & PG_ZERO) | flags;
2402 m->oflags = VPO_UNMANAGED;
2403 m->busy_lock = VPB_UNBUSIED;
2404 if ((req & VM_ALLOC_WIRED) != 0) {
2409 if ((req & VM_ALLOC_ZERO) != 0 && (m->flags & PG_ZERO) == 0)
2416 vm_page_alloc_freelist(int freelist, int req)
2418 struct vm_domainset_iter di;
2422 vm_domainset_iter_page_init(&di, NULL, 0, &domain, &req);
2424 m = vm_page_alloc_freelist_domain(domain, freelist, req);
2427 } while (vm_domainset_iter_page(&di, NULL, &domain) == 0);
2433 vm_page_alloc_freelist_domain(int domain, int freelist, int req)
2435 KASSERT(freelist >= 0 && freelist < VM_NFREELIST,
2436 ("%s: invalid freelist %d", __func__, freelist));
2438 return (_vm_page_alloc_noobj_domain(domain, freelist, req));
2442 vm_page_alloc_noobj(int req)
2444 struct vm_domainset_iter di;
2448 vm_domainset_iter_page_init(&di, NULL, 0, &domain, &req);
2450 m = vm_page_alloc_noobj_domain(domain, req);
2453 } while (vm_domainset_iter_page(&di, NULL, &domain) == 0);
2459 vm_page_alloc_noobj_domain(int domain, int req)
2461 return (_vm_page_alloc_noobj_domain(domain, VM_NFREELIST, req));
2465 vm_page_alloc_noobj_contig(int req, u_long npages, vm_paddr_t low,
2466 vm_paddr_t high, u_long alignment, vm_paddr_t boundary,
2467 vm_memattr_t memattr)
2469 struct vm_domainset_iter di;
2473 vm_domainset_iter_page_init(&di, NULL, 0, &domain, &req);
2475 m = vm_page_alloc_noobj_contig_domain(domain, req, npages, low,
2476 high, alignment, boundary, memattr);
2479 } while (vm_domainset_iter_page(&di, NULL, &domain) == 0);
2485 vm_page_alloc_noobj_contig_domain(int domain, int req, u_long npages,
2486 vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary,
2487 vm_memattr_t memattr)
2492 #define VPANC_FLAGS (VPAN_FLAGS | VM_ALLOC_NORECLAIM)
2493 KASSERT((req & ~VPANC_FLAGS) == 0,
2494 ("invalid request %#x", req));
2495 KASSERT((req & (VM_ALLOC_WAITOK | VM_ALLOC_NORECLAIM)) !=
2496 (VM_ALLOC_WAITOK | VM_ALLOC_NORECLAIM),
2497 ("invalid request %#x", req));
2498 KASSERT(((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) !=
2499 (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)),
2500 ("invalid request %#x", req));
2501 KASSERT(npages > 0, ("vm_page_alloc_contig: npages is zero"));
2503 while ((m_ret = vm_page_find_contig_domain(domain, req, npages,
2504 low, high, alignment, boundary)) == NULL) {
2505 if (!vm_domain_alloc_fail(VM_DOMAIN(domain), NULL, req))
2510 * Initialize the pages. Only the PG_ZERO flag is inherited.
2513 if ((req & VM_ALLOC_NODUMP) != 0)
2515 if ((req & VM_ALLOC_WIRED) != 0)
2516 vm_wire_add(npages);
2517 for (m = m_ret; m < &m_ret[npages]; m++) {
2519 vm_page_alloc_check(m);
2522 * Consumers should not rely on a useful default pindex value.
2524 m->pindex = 0xdeadc0dedeadc0de;
2526 m->flags = (m->flags | PG_NODUMP) & flags;
2527 m->busy_lock = VPB_UNBUSIED;
2528 if ((req & VM_ALLOC_WIRED) != 0)
2531 m->oflags = VPO_UNMANAGED;
2534 * Zero the page before updating any mappings since the page is
2535 * not yet shared with any devices which might require the
2536 * non-default memory attribute. pmap_page_set_memattr()
2537 * flushes data caches before returning.
2539 if ((req & VM_ALLOC_ZERO) != 0 && (m->flags & PG_ZERO) == 0)
2541 if (memattr != VM_MEMATTR_DEFAULT)
2542 pmap_page_set_memattr(m, memattr);
2548 * Check a page that has been freshly dequeued from a freelist.
2551 vm_page_alloc_check(vm_page_t m)
2554 KASSERT(m->object == NULL, ("page %p has object", m));
2555 KASSERT(m->a.queue == PQ_NONE &&
2556 (m->a.flags & PGA_QUEUE_STATE_MASK) == 0,
2557 ("page %p has unexpected queue %d, flags %#x",
2558 m, m->a.queue, (m->a.flags & PGA_QUEUE_STATE_MASK)));
2559 KASSERT(m->ref_count == 0, ("page %p has references", m));
2560 KASSERT(vm_page_busy_freed(m), ("page %p is not freed", m));
2561 KASSERT(m->dirty == 0, ("page %p is dirty", m));
2562 KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT,
2563 ("page %p has unexpected memattr %d",
2564 m, pmap_page_get_memattr(m)));
2565 KASSERT(vm_page_none_valid(m), ("free page %p is valid", m));
2566 pmap_vm_page_alloc_check(m);
2570 vm_page_zone_import(void *arg, void **store, int cnt, int domain, int flags)
2572 struct vm_domain *vmd;
2573 struct vm_pgcache *pgcache;
2577 vmd = VM_DOMAIN(pgcache->domain);
2580 * The page daemon should avoid creating extra memory pressure since its
2581 * main purpose is to replenish the store of free pages.
2583 if (vmd->vmd_severeset || curproc == pageproc ||
2584 !_vm_domain_allocate(vmd, VM_ALLOC_NORMAL, cnt))
2586 domain = vmd->vmd_domain;
2587 vm_domain_free_lock(vmd);
2588 i = vm_phys_alloc_npages(domain, pgcache->pool, cnt,
2589 (vm_page_t *)store);
2590 vm_domain_free_unlock(vmd);
2592 vm_domain_freecnt_inc(vmd, cnt - i);
2598 vm_page_zone_release(void *arg, void **store, int cnt)
2600 struct vm_domain *vmd;
2601 struct vm_pgcache *pgcache;
2606 vmd = VM_DOMAIN(pgcache->domain);
2607 vm_domain_free_lock(vmd);
2608 for (i = 0; i < cnt; i++) {
2609 m = (vm_page_t)store[i];
2610 vm_phys_free_pages(m, 0);
2612 vm_domain_free_unlock(vmd);
2613 vm_domain_freecnt_inc(vmd, cnt);
2616 #define VPSC_ANY 0 /* No restrictions. */
2617 #define VPSC_NORESERV 1 /* Skip reservations; implies VPSC_NOSUPER. */
2618 #define VPSC_NOSUPER 2 /* Skip superpages. */
2621 * vm_page_scan_contig:
2623 * Scan vm_page_array[] between the specified entries "m_start" and
2624 * "m_end" for a run of contiguous physical pages that satisfy the
2625 * specified conditions, and return the lowest page in the run. The
2626 * specified "alignment" determines the alignment of the lowest physical
2627 * page in the run. If the specified "boundary" is non-zero, then the
2628 * run of physical pages cannot span a physical address that is a
2629 * multiple of "boundary".
2631 * "m_end" is never dereferenced, so it need not point to a vm_page
2632 * structure within vm_page_array[].
2634 * "npages" must be greater than zero. "m_start" and "m_end" must not
2635 * span a hole (or discontiguity) in the physical address space. Both
2636 * "alignment" and "boundary" must be a power of two.
2639 vm_page_scan_contig(u_long npages, vm_page_t m_start, vm_page_t m_end,
2640 u_long alignment, vm_paddr_t boundary, int options)
2645 #if VM_NRESERVLEVEL > 0
2648 int m_inc, order, run_ext, run_len;
2650 KASSERT(npages > 0, ("npages is 0"));
2651 KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
2652 KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
2655 for (m = m_start; m < m_end && run_len < npages; m += m_inc) {
2656 KASSERT((m->flags & PG_MARKER) == 0,
2657 ("page %p is PG_MARKER", m));
2658 KASSERT((m->flags & PG_FICTITIOUS) == 0 || m->ref_count >= 1,
2659 ("fictitious page %p has invalid ref count", m));
2662 * If the current page would be the start of a run, check its
2663 * physical address against the end, alignment, and boundary
2664 * conditions. If it doesn't satisfy these conditions, either
2665 * terminate the scan or advance to the next page that
2666 * satisfies the failed condition.
2669 KASSERT(m_run == NULL, ("m_run != NULL"));
2670 if (m + npages > m_end)
2672 pa = VM_PAGE_TO_PHYS(m);
2673 if (!vm_addr_align_ok(pa, alignment)) {
2674 m_inc = atop(roundup2(pa, alignment) - pa);
2677 if (!vm_addr_bound_ok(pa, ptoa(npages), boundary)) {
2678 m_inc = atop(roundup2(pa, boundary) - pa);
2682 KASSERT(m_run != NULL, ("m_run == NULL"));
2686 if (vm_page_wired(m))
2688 #if VM_NRESERVLEVEL > 0
2689 else if ((level = vm_reserv_level(m)) >= 0 &&
2690 (options & VPSC_NORESERV) != 0) {
2692 /* Advance to the end of the reservation. */
2693 pa = VM_PAGE_TO_PHYS(m);
2694 m_inc = atop(roundup2(pa + 1, vm_reserv_size(level)) -
2698 else if ((object = atomic_load_ptr(&m->object)) != NULL) {
2700 * The page is considered eligible for relocation if
2701 * and only if it could be laundered or reclaimed by
2704 VM_OBJECT_RLOCK(object);
2705 if (object != m->object) {
2706 VM_OBJECT_RUNLOCK(object);
2709 /* Don't care: PG_NODUMP, PG_ZERO. */
2710 if ((object->flags & OBJ_SWAP) == 0 &&
2711 object->type != OBJT_VNODE) {
2713 #if VM_NRESERVLEVEL > 0
2714 } else if ((options & VPSC_NOSUPER) != 0 &&
2715 (level = vm_reserv_level_iffullpop(m)) >= 0) {
2717 /* Advance to the end of the superpage. */
2718 pa = VM_PAGE_TO_PHYS(m);
2719 m_inc = atop(roundup2(pa + 1,
2720 vm_reserv_size(level)) - pa);
2722 } else if (object->memattr == VM_MEMATTR_DEFAULT &&
2723 vm_page_queue(m) != PQ_NONE && !vm_page_busied(m)) {
2725 * The page is allocated but eligible for
2726 * relocation. Extend the current run by one
2729 KASSERT(pmap_page_get_memattr(m) ==
2731 ("page %p has an unexpected memattr", m));
2732 KASSERT((m->oflags & (VPO_SWAPINPROG |
2733 VPO_SWAPSLEEP | VPO_UNMANAGED)) == 0,
2734 ("page %p has unexpected oflags", m));
2735 /* Don't care: PGA_NOSYNC. */
2739 VM_OBJECT_RUNLOCK(object);
2740 #if VM_NRESERVLEVEL > 0
2741 } else if (level >= 0) {
2743 * The page is reserved but not yet allocated. In
2744 * other words, it is still free. Extend the current
2749 } else if ((order = m->order) < VM_NFREEORDER) {
2751 * The page is enqueued in the physical memory
2752 * allocator's free page queues. Moreover, it is the
2753 * first page in a power-of-two-sized run of
2754 * contiguous free pages. Add these pages to the end
2755 * of the current run, and jump ahead.
2757 run_ext = 1 << order;
2761 * Skip the page for one of the following reasons: (1)
2762 * It is enqueued in the physical memory allocator's
2763 * free page queues. However, it is not the first
2764 * page in a run of contiguous free pages. (This case
2765 * rarely occurs because the scan is performed in
2766 * ascending order.) (2) It is not reserved, and it is
2767 * transitioning from free to allocated. (Conversely,
2768 * the transition from allocated to free for managed
2769 * pages is blocked by the page busy lock.) (3) It is
2770 * allocated but not contained by an object and not
2771 * wired, e.g., allocated by Xen's balloon driver.
2777 * Extend or reset the current run of pages.
2790 if (run_len >= npages)
2796 * vm_page_reclaim_run:
2798 * Try to relocate each of the allocated virtual pages within the
2799 * specified run of physical pages to a new physical address. Free the
2800 * physical pages underlying the relocated virtual pages. A virtual page
2801 * is relocatable if and only if it could be laundered or reclaimed by
2802 * the page daemon. Whenever possible, a virtual page is relocated to a
2803 * physical address above "high".
2805 * Returns 0 if every physical page within the run was already free or
2806 * just freed by a successful relocation. Otherwise, returns a non-zero
2807 * value indicating why the last attempt to relocate a virtual page was
2810 * "req_class" must be an allocation class.
2813 vm_page_reclaim_run(int req_class, int domain, u_long npages, vm_page_t m_run,
2816 struct vm_domain *vmd;
2817 struct spglist free;
2820 vm_page_t m, m_end, m_new;
2821 int error, order, req;
2823 KASSERT((req_class & VM_ALLOC_CLASS_MASK) == req_class,
2824 ("req_class is not an allocation class"));
2828 m_end = m_run + npages;
2829 for (; error == 0 && m < m_end; m++) {
2830 KASSERT((m->flags & (PG_FICTITIOUS | PG_MARKER)) == 0,
2831 ("page %p is PG_FICTITIOUS or PG_MARKER", m));
2834 * Racily check for wirings. Races are handled once the object
2835 * lock is held and the page is unmapped.
2837 if (vm_page_wired(m))
2839 else if ((object = atomic_load_ptr(&m->object)) != NULL) {
2841 * The page is relocated if and only if it could be
2842 * laundered or reclaimed by the page daemon.
2844 VM_OBJECT_WLOCK(object);
2845 /* Don't care: PG_NODUMP, PG_ZERO. */
2846 if (m->object != object ||
2847 ((object->flags & OBJ_SWAP) == 0 &&
2848 object->type != OBJT_VNODE))
2850 else if (object->memattr != VM_MEMATTR_DEFAULT)
2852 else if (vm_page_queue(m) != PQ_NONE &&
2853 vm_page_tryxbusy(m) != 0) {
2854 if (vm_page_wired(m)) {
2859 KASSERT(pmap_page_get_memattr(m) ==
2861 ("page %p has an unexpected memattr", m));
2862 KASSERT(m->oflags == 0,
2863 ("page %p has unexpected oflags", m));
2864 /* Don't care: PGA_NOSYNC. */
2865 if (!vm_page_none_valid(m)) {
2867 * First, try to allocate a new page
2868 * that is above "high". Failing
2869 * that, try to allocate a new page
2870 * that is below "m_run". Allocate
2871 * the new page between the end of
2872 * "m_run" and "high" only as a last
2876 if ((m->flags & PG_NODUMP) != 0)
2877 req |= VM_ALLOC_NODUMP;
2878 if (trunc_page(high) !=
2879 ~(vm_paddr_t)PAGE_MASK) {
2881 vm_page_alloc_noobj_contig(
2882 req, 1, round_page(high),
2883 ~(vm_paddr_t)0, PAGE_SIZE,
2884 0, VM_MEMATTR_DEFAULT);
2887 if (m_new == NULL) {
2888 pa = VM_PAGE_TO_PHYS(m_run);
2890 vm_page_alloc_noobj_contig(
2893 VM_MEMATTR_DEFAULT);
2895 if (m_new == NULL) {
2898 vm_page_alloc_noobj_contig(
2899 req, 1, pa, high, PAGE_SIZE,
2900 0, VM_MEMATTR_DEFAULT);
2902 if (m_new == NULL) {
2909 * Unmap the page and check for new
2910 * wirings that may have been acquired
2911 * through a pmap lookup.
2913 if (object->ref_count != 0 &&
2914 !vm_page_try_remove_all(m)) {
2916 vm_page_free(m_new);
2922 * Replace "m" with the new page. For
2923 * vm_page_replace(), "m" must be busy
2924 * and dequeued. Finally, change "m"
2925 * as if vm_page_free() was called.
2927 m_new->a.flags = m->a.flags &
2928 ~PGA_QUEUE_STATE_MASK;
2929 KASSERT(m_new->oflags == VPO_UNMANAGED,
2930 ("page %p is managed", m_new));
2932 pmap_copy_page(m, m_new);
2933 m_new->valid = m->valid;
2934 m_new->dirty = m->dirty;
2935 m->flags &= ~PG_ZERO;
2937 if (vm_page_replace_hold(m_new, object,
2939 vm_page_free_prep(m))
2940 SLIST_INSERT_HEAD(&free, m,
2944 * The new page must be deactivated
2945 * before the object is unlocked.
2947 vm_page_deactivate(m_new);
2949 m->flags &= ~PG_ZERO;
2951 if (vm_page_free_prep(m))
2952 SLIST_INSERT_HEAD(&free, m,
2954 KASSERT(m->dirty == 0,
2955 ("page %p is dirty", m));
2960 VM_OBJECT_WUNLOCK(object);
2962 MPASS(vm_page_domain(m) == domain);
2963 vmd = VM_DOMAIN(domain);
2964 vm_domain_free_lock(vmd);
2966 if (order < VM_NFREEORDER) {
2968 * The page is enqueued in the physical memory
2969 * allocator's free page queues. Moreover, it
2970 * is the first page in a power-of-two-sized
2971 * run of contiguous free pages. Jump ahead
2972 * to the last page within that run, and
2973 * continue from there.
2975 m += (1 << order) - 1;
2977 #if VM_NRESERVLEVEL > 0
2978 else if (vm_reserv_is_page_free(m))
2981 vm_domain_free_unlock(vmd);
2982 if (order == VM_NFREEORDER)
2986 if ((m = SLIST_FIRST(&free)) != NULL) {
2989 vmd = VM_DOMAIN(domain);
2991 vm_domain_free_lock(vmd);
2993 MPASS(vm_page_domain(m) == domain);
2994 SLIST_REMOVE_HEAD(&free, plinks.s.ss);
2995 vm_phys_free_pages(m, 0);
2997 } while ((m = SLIST_FIRST(&free)) != NULL);
2998 vm_domain_free_unlock(vmd);
2999 vm_domain_freecnt_inc(vmd, cnt);
3006 #define RUN_INDEX(count, nruns) ((count) % (nruns))
3008 #define MIN_RECLAIM 8
3011 * vm_page_reclaim_contig:
3013 * Reclaim allocated, contiguous physical memory satisfying the specified
3014 * conditions by relocating the virtual pages using that physical memory.
3015 * Returns 0 if reclamation is successful, ERANGE if the specified domain
3016 * can't possibly satisfy the reclamation request, or ENOMEM if not
3017 * currently able to reclaim the requested number of pages. Since
3018 * relocation requires the allocation of physical pages, reclamation may
3019 * fail with ENOMEM due to a shortage of free pages. When reclamation
3020 * fails in this manner, callers are expected to perform vm_wait() before
3021 * retrying a failed allocation operation, e.g., vm_page_alloc_contig().
3023 * The caller must always specify an allocation class through "req".
3025 * allocation classes:
3026 * VM_ALLOC_NORMAL normal process request
3027 * VM_ALLOC_SYSTEM system *really* needs a page
3028 * VM_ALLOC_INTERRUPT interrupt time request
3030 * The optional allocation flags are ignored.
3032 * "npages" must be greater than zero. Both "alignment" and "boundary"
3033 * must be a power of two.
3036 vm_page_reclaim_contig_domain_ext(int domain, int req, u_long npages,
3037 vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary,
3040 struct vm_domain *vmd;
3041 vm_page_t bounds[2], m_run, _m_runs[NRUNS], *m_runs;
3042 u_long count, minalign, reclaimed;
3043 int error, i, min_reclaim, nruns, options, req_class;
3044 int segind, start_segind;
3047 KASSERT(npages > 0, ("npages is 0"));
3048 KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
3049 KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
3054 * If the caller wants to reclaim multiple runs, try to allocate
3055 * space to store the runs. If that fails, fall back to the old
3056 * behavior of just reclaiming MIN_RECLAIM pages.
3058 if (desired_runs > 1)
3059 m_runs = malloc((NRUNS + desired_runs) * sizeof(*m_runs),
3064 if (m_runs == NULL) {
3068 nruns = NRUNS + desired_runs - 1;
3070 min_reclaim = MAX(desired_runs * npages, MIN_RECLAIM);
3073 * The caller will attempt an allocation after some runs have been
3074 * reclaimed and added to the vm_phys buddy lists. Due to limitations
3075 * of vm_phys_alloc_contig(), round up the requested length to the next
3076 * power of two or maximum chunk size, and ensure that each run is
3079 minalign = 1ul << imin(flsl(npages - 1), VM_NFREEORDER - 1);
3080 npages = roundup2(npages, minalign);
3081 if (alignment < ptoa(minalign))
3082 alignment = ptoa(minalign);
3085 * The page daemon is allowed to dig deeper into the free page list.
3087 req_class = req & VM_ALLOC_CLASS_MASK;
3088 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
3089 req_class = VM_ALLOC_SYSTEM;
3091 start_segind = vm_phys_lookup_segind(low);
3094 * Return if the number of free pages cannot satisfy the requested
3097 vmd = VM_DOMAIN(domain);
3098 count = vmd->vmd_free_count;
3099 if (count < npages + vmd->vmd_free_reserved || (count < npages +
3100 vmd->vmd_interrupt_free_min && req_class == VM_ALLOC_SYSTEM) ||
3101 (count < npages && req_class == VM_ALLOC_INTERRUPT))
3105 * Scan up to three times, relaxing the restrictions ("options") on
3106 * the reclamation of reservations and superpages each time.
3108 for (options = VPSC_NORESERV;;) {
3109 bool phys_range_exists = false;
3112 * Find the highest runs that satisfy the given constraints
3113 * and restrictions, and record them in "m_runs".
3116 segind = start_segind;
3117 while ((segind = vm_phys_find_range(bounds, segind, domain,
3118 npages, low, high)) != -1) {
3119 phys_range_exists = true;
3120 while ((m_run = vm_page_scan_contig(npages, bounds[0],
3121 bounds[1], alignment, boundary, options))) {
3122 bounds[0] = m_run + npages;
3123 m_runs[RUN_INDEX(count, nruns)] = m_run;
3129 if (!phys_range_exists) {
3135 * Reclaim the highest runs in LIFO (descending) order until
3136 * the number of reclaimed pages, "reclaimed", is at least
3137 * "min_reclaim". Reset "reclaimed" each time because each
3138 * reclamation is idempotent, and runs will (likely) recur
3139 * from one scan to the next as restrictions are relaxed.
3142 for (i = 0; count > 0 && i < nruns; i++) {
3144 m_run = m_runs[RUN_INDEX(count, nruns)];
3145 error = vm_page_reclaim_run(req_class, domain, npages,
3148 reclaimed += npages;
3149 if (reclaimed >= min_reclaim) {
3157 * Either relax the restrictions on the next scan or return if
3158 * the last scan had no restrictions.
3160 if (options == VPSC_NORESERV)
3161 options = VPSC_NOSUPER;
3162 else if (options == VPSC_NOSUPER)
3164 else if (options == VPSC_ANY) {
3171 if (m_runs != _m_runs)
3172 free(m_runs, M_TEMP);
3177 vm_page_reclaim_contig_domain(int domain, int req, u_long npages,
3178 vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
3180 return (vm_page_reclaim_contig_domain_ext(domain, req, npages, low, high,
3181 alignment, boundary, 1));
3185 vm_page_reclaim_contig(int req, u_long npages, vm_paddr_t low, vm_paddr_t high,
3186 u_long alignment, vm_paddr_t boundary)
3188 struct vm_domainset_iter di;
3189 int domain, ret, status;
3193 vm_domainset_iter_page_init(&di, NULL, 0, &domain, &req);
3195 status = vm_page_reclaim_contig_domain(domain, req, npages, low,
3196 high, alignment, boundary);
3199 else if (status == ERANGE)
3200 vm_domainset_iter_ignore(&di, domain);
3202 KASSERT(status == ENOMEM, ("Unrecognized error %d "
3203 "from vm_page_reclaim_contig_domain()", status));
3206 } while (vm_domainset_iter_page(&di, NULL, &domain) == 0);
3212 * Set the domain in the appropriate page level domainset.
3215 vm_domain_set(struct vm_domain *vmd)
3218 mtx_lock(&vm_domainset_lock);
3219 if (!vmd->vmd_minset && vm_paging_min(vmd)) {
3220 vmd->vmd_minset = 1;
3221 DOMAINSET_SET(vmd->vmd_domain, &vm_min_domains);
3223 if (!vmd->vmd_severeset && vm_paging_severe(vmd)) {
3224 vmd->vmd_severeset = 1;
3225 DOMAINSET_SET(vmd->vmd_domain, &vm_severe_domains);
3227 mtx_unlock(&vm_domainset_lock);
3231 * Clear the domain from the appropriate page level domainset.
3234 vm_domain_clear(struct vm_domain *vmd)
3237 mtx_lock(&vm_domainset_lock);
3238 if (vmd->vmd_minset && !vm_paging_min(vmd)) {
3239 vmd->vmd_minset = 0;
3240 DOMAINSET_CLR(vmd->vmd_domain, &vm_min_domains);
3241 if (vm_min_waiters != 0) {
3243 wakeup(&vm_min_domains);
3246 if (vmd->vmd_severeset && !vm_paging_severe(vmd)) {
3247 vmd->vmd_severeset = 0;
3248 DOMAINSET_CLR(vmd->vmd_domain, &vm_severe_domains);
3249 if (vm_severe_waiters != 0) {
3250 vm_severe_waiters = 0;
3251 wakeup(&vm_severe_domains);
3256 * If pageout daemon needs pages, then tell it that there are
3259 if (vmd->vmd_pageout_pages_needed &&
3260 vmd->vmd_free_count >= vmd->vmd_pageout_free_min) {
3261 wakeup(&vmd->vmd_pageout_pages_needed);
3262 vmd->vmd_pageout_pages_needed = 0;
3265 /* See comments in vm_wait_doms(). */
3266 if (vm_pageproc_waiters) {
3267 vm_pageproc_waiters = 0;
3268 wakeup(&vm_pageproc_waiters);
3270 mtx_unlock(&vm_domainset_lock);
3274 * Wait for free pages to exceed the min threshold globally.
3280 mtx_lock(&vm_domainset_lock);
3281 while (vm_page_count_min()) {
3283 msleep(&vm_min_domains, &vm_domainset_lock, PVM, "vmwait", 0);
3285 mtx_unlock(&vm_domainset_lock);
3289 * Wait for free pages to exceed the severe threshold globally.
3292 vm_wait_severe(void)
3295 mtx_lock(&vm_domainset_lock);
3296 while (vm_page_count_severe()) {
3297 vm_severe_waiters++;
3298 msleep(&vm_severe_domains, &vm_domainset_lock, PVM,
3301 mtx_unlock(&vm_domainset_lock);
3308 return (vm_severe_waiters + vm_min_waiters + vm_pageproc_waiters);
3312 vm_wait_doms(const domainset_t *wdoms, int mflags)
3319 * We use racey wakeup synchronization to avoid expensive global
3320 * locking for the pageproc when sleeping with a non-specific vm_wait.
3321 * To handle this, we only sleep for one tick in this instance. It
3322 * is expected that most allocations for the pageproc will come from
3323 * kmem or vm_page_grab* which will use the more specific and
3324 * race-free vm_wait_domain().
3326 if (curproc == pageproc) {
3327 mtx_lock(&vm_domainset_lock);
3328 vm_pageproc_waiters++;
3329 error = msleep(&vm_pageproc_waiters, &vm_domainset_lock,
3330 PVM | PDROP | mflags, "pageprocwait", 1);
3333 * XXX Ideally we would wait only until the allocation could
3334 * be satisfied. This condition can cause new allocators to
3335 * consume all freed pages while old allocators wait.
3337 mtx_lock(&vm_domainset_lock);
3338 if (vm_page_count_min_set(wdoms)) {
3339 if (pageproc == NULL)
3340 panic("vm_wait in early boot");
3342 error = msleep(&vm_min_domains, &vm_domainset_lock,
3343 PVM | PDROP | mflags, "vmwait", 0);
3345 mtx_unlock(&vm_domainset_lock);
3353 * Sleep until free pages are available for allocation.
3354 * - Called in various places after failed memory allocations.
3357 vm_wait_domain(int domain)
3359 struct vm_domain *vmd;
3362 vmd = VM_DOMAIN(domain);
3363 vm_domain_free_assert_unlocked(vmd);
3365 if (curproc == pageproc) {
3366 mtx_lock(&vm_domainset_lock);
3367 if (vmd->vmd_free_count < vmd->vmd_pageout_free_min) {
3368 vmd->vmd_pageout_pages_needed = 1;
3369 msleep(&vmd->vmd_pageout_pages_needed,
3370 &vm_domainset_lock, PDROP | PSWP, "VMWait", 0);
3372 mtx_unlock(&vm_domainset_lock);
3374 DOMAINSET_ZERO(&wdom);
3375 DOMAINSET_SET(vmd->vmd_domain, &wdom);
3376 vm_wait_doms(&wdom, 0);
3381 vm_wait_flags(vm_object_t obj, int mflags)
3383 struct domainset *d;
3388 * Carefully fetch pointers only once: the struct domainset
3389 * itself is ummutable but the pointer might change.
3392 d = obj->domain.dr_policy;
3394 d = curthread->td_domain.dr_policy;
3396 return (vm_wait_doms(&d->ds_mask, mflags));
3402 * Sleep until free pages are available for allocation in the
3403 * affinity domains of the obj. If obj is NULL, the domain set
3404 * for the calling thread is used.
3405 * Called in various places after failed memory allocations.
3408 vm_wait(vm_object_t obj)
3410 (void)vm_wait_flags(obj, 0);
3414 vm_wait_intr(vm_object_t obj)
3416 return (vm_wait_flags(obj, PCATCH));
3420 * vm_domain_alloc_fail:
3422 * Called when a page allocation function fails. Informs the
3423 * pagedaemon and performs the requested wait. Requires the
3424 * domain_free and object lock on entry. Returns with the
3425 * object lock held and free lock released. Returns an error when
3426 * retry is necessary.
3430 vm_domain_alloc_fail(struct vm_domain *vmd, vm_object_t object, int req)
3433 vm_domain_free_assert_unlocked(vmd);
3435 atomic_add_int(&vmd->vmd_pageout_deficit,
3436 max((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1));
3437 if (req & (VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL)) {
3439 VM_OBJECT_WUNLOCK(object);
3440 vm_wait_domain(vmd->vmd_domain);
3442 VM_OBJECT_WLOCK(object);
3443 if (req & VM_ALLOC_WAITOK)
3453 * Sleep until free pages are available for allocation.
3454 * - Called only in vm_fault so that processes page faulting
3455 * can be easily tracked.
3456 * - Sleeps at a lower priority than vm_wait() so that vm_wait()ing
3457 * processes will be able to grab memory first. Do not change
3458 * this balance without careful testing first.
3461 vm_waitpfault(struct domainset *dset, int timo)
3465 * XXX Ideally we would wait only until the allocation could
3466 * be satisfied. This condition can cause new allocators to
3467 * consume all freed pages while old allocators wait.
3469 mtx_lock(&vm_domainset_lock);
3470 if (vm_page_count_min_set(&dset->ds_mask)) {
3472 msleep(&vm_min_domains, &vm_domainset_lock, PUSER | PDROP,
3475 mtx_unlock(&vm_domainset_lock);
3478 static struct vm_pagequeue *
3479 _vm_page_pagequeue(vm_page_t m, uint8_t queue)
3482 return (&vm_pagequeue_domain(m)->vmd_pagequeues[queue]);
3486 static struct vm_pagequeue *
3487 vm_page_pagequeue(vm_page_t m)
3490 return (_vm_page_pagequeue(m, vm_page_astate_load(m).queue));
3494 static __always_inline bool
3495 vm_page_pqstate_fcmpset(vm_page_t m, vm_page_astate_t *old, vm_page_astate_t new)
3497 vm_page_astate_t tmp;
3501 if (__predict_true(vm_page_astate_fcmpset(m, old, new)))
3503 counter_u64_add(pqstate_commit_retries, 1);
3504 } while (old->_bits == tmp._bits);
3510 * Do the work of committing a queue state update that moves the page out of
3511 * its current queue.
3514 _vm_page_pqstate_commit_dequeue(struct vm_pagequeue *pq, vm_page_t m,
3515 vm_page_astate_t *old, vm_page_astate_t new)
3519 vm_pagequeue_assert_locked(pq);
3520 KASSERT(vm_page_pagequeue(m) == pq,
3521 ("%s: queue %p does not match page %p", __func__, pq, m));
3522 KASSERT(old->queue != PQ_NONE && new.queue != old->queue,
3523 ("%s: invalid queue indices %d %d",
3524 __func__, old->queue, new.queue));
3527 * Once the queue index of the page changes there is nothing
3528 * synchronizing with further updates to the page's physical
3529 * queue state. Therefore we must speculatively remove the page
3530 * from the queue now and be prepared to roll back if the queue
3531 * state update fails. If the page is not physically enqueued then
3532 * we just update its queue index.
3534 if ((old->flags & PGA_ENQUEUED) != 0) {
3535 new.flags &= ~PGA_ENQUEUED;
3536 next = TAILQ_NEXT(m, plinks.q);
3537 TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
3538 vm_pagequeue_cnt_dec(pq);
3539 if (!vm_page_pqstate_fcmpset(m, old, new)) {
3541 TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
3543 TAILQ_INSERT_BEFORE(next, m, plinks.q);
3544 vm_pagequeue_cnt_inc(pq);
3550 return (vm_page_pqstate_fcmpset(m, old, new));
3555 vm_page_pqstate_commit_dequeue(vm_page_t m, vm_page_astate_t *old,
3556 vm_page_astate_t new)
3558 struct vm_pagequeue *pq;
3559 vm_page_astate_t as;
3562 pq = _vm_page_pagequeue(m, old->queue);
3565 * The queue field and PGA_ENQUEUED flag are stable only so long as the
3566 * corresponding page queue lock is held.
3568 vm_pagequeue_lock(pq);
3569 as = vm_page_astate_load(m);
3570 if (__predict_false(as._bits != old->_bits)) {
3574 ret = _vm_page_pqstate_commit_dequeue(pq, m, old, new);
3576 vm_pagequeue_unlock(pq);
3581 * Commit a queue state update that enqueues or requeues a page.
3584 _vm_page_pqstate_commit_requeue(struct vm_pagequeue *pq, vm_page_t m,
3585 vm_page_astate_t *old, vm_page_astate_t new)
3587 struct vm_domain *vmd;
3589 vm_pagequeue_assert_locked(pq);
3590 KASSERT(old->queue != PQ_NONE && new.queue == old->queue,
3591 ("%s: invalid queue indices %d %d",
3592 __func__, old->queue, new.queue));
3594 new.flags |= PGA_ENQUEUED;
3595 if (!vm_page_pqstate_fcmpset(m, old, new))
3598 if ((old->flags & PGA_ENQUEUED) != 0)
3599 TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
3601 vm_pagequeue_cnt_inc(pq);
3604 * Give PGA_REQUEUE_HEAD precedence over PGA_REQUEUE. In particular, if
3605 * both flags are set in close succession, only PGA_REQUEUE_HEAD will be
3606 * applied, even if it was set first.
3608 if ((old->flags & PGA_REQUEUE_HEAD) != 0) {
3609 vmd = vm_pagequeue_domain(m);
3610 KASSERT(pq == &vmd->vmd_pagequeues[PQ_INACTIVE],
3611 ("%s: invalid page queue for page %p", __func__, m));
3612 TAILQ_INSERT_BEFORE(&vmd->vmd_inacthead, m, plinks.q);
3614 TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
3620 * Commit a queue state update that encodes a request for a deferred queue
3624 vm_page_pqstate_commit_request(vm_page_t m, vm_page_astate_t *old,
3625 vm_page_astate_t new)
3628 KASSERT(old->queue == new.queue || new.queue != PQ_NONE,
3629 ("%s: invalid state, queue %d flags %x",
3630 __func__, new.queue, new.flags));
3632 if (old->_bits != new._bits &&
3633 !vm_page_pqstate_fcmpset(m, old, new))
3635 vm_page_pqbatch_submit(m, new.queue);
3640 * A generic queue state update function. This handles more cases than the
3641 * specialized functions above.
3644 vm_page_pqstate_commit(vm_page_t m, vm_page_astate_t *old, vm_page_astate_t new)
3647 if (old->_bits == new._bits)
3650 if (old->queue != PQ_NONE && new.queue != old->queue) {
3651 if (!vm_page_pqstate_commit_dequeue(m, old, new))
3653 if (new.queue != PQ_NONE)
3654 vm_page_pqbatch_submit(m, new.queue);
3656 if (!vm_page_pqstate_fcmpset(m, old, new))
3658 if (new.queue != PQ_NONE &&
3659 ((new.flags & ~old->flags) & PGA_QUEUE_OP_MASK) != 0)
3660 vm_page_pqbatch_submit(m, new.queue);
3666 * Apply deferred queue state updates to a page.
3669 vm_pqbatch_process_page(struct vm_pagequeue *pq, vm_page_t m, uint8_t queue)
3671 vm_page_astate_t new, old;
3673 CRITICAL_ASSERT(curthread);
3674 vm_pagequeue_assert_locked(pq);
3675 KASSERT(queue < PQ_COUNT,
3676 ("%s: invalid queue index %d", __func__, queue));
3677 KASSERT(pq == _vm_page_pagequeue(m, queue),
3678 ("%s: page %p does not belong to queue %p", __func__, m, pq));
3680 for (old = vm_page_astate_load(m);;) {
3681 if (__predict_false(old.queue != queue ||
3682 (old.flags & PGA_QUEUE_OP_MASK) == 0)) {
3683 counter_u64_add(queue_nops, 1);
3686 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3687 ("%s: page %p is unmanaged", __func__, m));
3690 if ((old.flags & PGA_DEQUEUE) != 0) {
3691 new.flags &= ~PGA_QUEUE_OP_MASK;
3692 new.queue = PQ_NONE;
3693 if (__predict_true(_vm_page_pqstate_commit_dequeue(pq,
3695 counter_u64_add(queue_ops, 1);
3699 new.flags &= ~(PGA_REQUEUE | PGA_REQUEUE_HEAD);
3700 if (__predict_true(_vm_page_pqstate_commit_requeue(pq,
3702 counter_u64_add(queue_ops, 1);
3710 vm_pqbatch_process(struct vm_pagequeue *pq, struct vm_batchqueue *bq,
3715 for (i = 0; i < bq->bq_cnt; i++)
3716 vm_pqbatch_process_page(pq, bq->bq_pa[i], queue);
3717 vm_batchqueue_init(bq);
3721 * vm_page_pqbatch_submit: [ internal use only ]
3723 * Enqueue a page in the specified page queue's batched work queue.
3724 * The caller must have encoded the requested operation in the page
3725 * structure's a.flags field.
3728 vm_page_pqbatch_submit(vm_page_t m, uint8_t queue)
3730 struct vm_batchqueue *bq;
3731 struct vm_pagequeue *pq;
3732 int domain, slots_remaining;
3734 KASSERT(queue < PQ_COUNT, ("invalid queue %d", queue));
3736 domain = vm_page_domain(m);
3738 bq = DPCPU_PTR(pqbatch[domain][queue]);
3739 slots_remaining = vm_batchqueue_insert(bq, m);
3740 if (slots_remaining > (VM_BATCHQUEUE_SIZE >> 1)) {
3741 /* keep building the bq */
3744 } else if (slots_remaining > 0 ) {
3745 /* Try to process the bq if we can get the lock */
3746 pq = &VM_DOMAIN(domain)->vmd_pagequeues[queue];
3747 if (vm_pagequeue_trylock(pq)) {
3748 vm_pqbatch_process(pq, bq, queue);
3749 vm_pagequeue_unlock(pq);
3756 /* if we make it here, the bq is full so wait for the lock */
3758 pq = &VM_DOMAIN(domain)->vmd_pagequeues[queue];
3759 vm_pagequeue_lock(pq);
3761 bq = DPCPU_PTR(pqbatch[domain][queue]);
3762 vm_pqbatch_process(pq, bq, queue);
3763 vm_pqbatch_process_page(pq, m, queue);
3764 vm_pagequeue_unlock(pq);
3769 * vm_page_pqbatch_drain: [ internal use only ]
3771 * Force all per-CPU page queue batch queues to be drained. This is
3772 * intended for use in severe memory shortages, to ensure that pages
3773 * do not remain stuck in the batch queues.
3776 vm_page_pqbatch_drain(void)
3779 struct vm_domain *vmd;
3780 struct vm_pagequeue *pq;
3781 int cpu, domain, queue;
3786 sched_bind(td, cpu);
3789 for (domain = 0; domain < vm_ndomains; domain++) {
3790 vmd = VM_DOMAIN(domain);
3791 for (queue = 0; queue < PQ_COUNT; queue++) {
3792 pq = &vmd->vmd_pagequeues[queue];
3793 vm_pagequeue_lock(pq);
3795 vm_pqbatch_process(pq,
3796 DPCPU_PTR(pqbatch[domain][queue]), queue);
3798 vm_pagequeue_unlock(pq);
3808 * vm_page_dequeue_deferred: [ internal use only ]
3810 * Request removal of the given page from its current page
3811 * queue. Physical removal from the queue may be deferred
3815 vm_page_dequeue_deferred(vm_page_t m)
3817 vm_page_astate_t new, old;
3819 old = vm_page_astate_load(m);
3821 if (old.queue == PQ_NONE) {
3822 KASSERT((old.flags & PGA_QUEUE_STATE_MASK) == 0,
3823 ("%s: page %p has unexpected queue state",
3828 new.flags |= PGA_DEQUEUE;
3829 } while (!vm_page_pqstate_commit_request(m, &old, new));
3835 * Remove the page from whichever page queue it's in, if any, before
3839 vm_page_dequeue(vm_page_t m)
3841 vm_page_astate_t new, old;
3843 old = vm_page_astate_load(m);
3845 if (old.queue == PQ_NONE) {
3846 KASSERT((old.flags & PGA_QUEUE_STATE_MASK) == 0,
3847 ("%s: page %p has unexpected queue state",
3852 new.flags &= ~PGA_QUEUE_OP_MASK;
3853 new.queue = PQ_NONE;
3854 } while (!vm_page_pqstate_commit_dequeue(m, &old, new));
3859 * Schedule the given page for insertion into the specified page queue.
3860 * Physical insertion of the page may be deferred indefinitely.
3863 vm_page_enqueue(vm_page_t m, uint8_t queue)
3866 KASSERT(m->a.queue == PQ_NONE &&
3867 (m->a.flags & PGA_QUEUE_STATE_MASK) == 0,
3868 ("%s: page %p is already enqueued", __func__, m));
3869 KASSERT(m->ref_count > 0,
3870 ("%s: page %p does not carry any references", __func__, m));
3873 if ((m->a.flags & PGA_REQUEUE) == 0)
3874 vm_page_aflag_set(m, PGA_REQUEUE);
3875 vm_page_pqbatch_submit(m, queue);
3879 * vm_page_free_prep:
3881 * Prepares the given page to be put on the free list,
3882 * disassociating it from any VM object. The caller may return
3883 * the page to the free list only if this function returns true.
3885 * The object, if it exists, must be locked, and then the page must
3886 * be xbusy. Otherwise the page must be not busied. A managed
3887 * page must be unmapped.
3890 vm_page_free_prep(vm_page_t m)
3894 * Synchronize with threads that have dropped a reference to this
3897 atomic_thread_fence_acq();
3899 #if defined(DIAGNOSTIC) && defined(PHYS_TO_DMAP)
3900 if (PMAP_HAS_DMAP && (m->flags & PG_ZERO) != 0) {
3903 p = (uint64_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m));
3904 for (i = 0; i < PAGE_SIZE / sizeof(uint64_t); i++, p++)
3905 KASSERT(*p == 0, ("vm_page_free_prep %p PG_ZERO %d %jx",
3906 m, i, (uintmax_t)*p));
3909 if ((m->oflags & VPO_UNMANAGED) == 0) {
3910 KASSERT(!pmap_page_is_mapped(m),
3911 ("vm_page_free_prep: freeing mapped page %p", m));
3912 KASSERT((m->a.flags & (PGA_EXECUTABLE | PGA_WRITEABLE)) == 0,
3913 ("vm_page_free_prep: mapping flags set in page %p", m));
3915 KASSERT(m->a.queue == PQ_NONE,
3916 ("vm_page_free_prep: unmanaged page %p is queued", m));
3918 VM_CNT_INC(v_tfree);
3920 if (m->object != NULL) {
3921 KASSERT(((m->oflags & VPO_UNMANAGED) != 0) ==
3922 ((m->object->flags & OBJ_UNMANAGED) != 0),
3923 ("vm_page_free_prep: managed flag mismatch for page %p",
3925 vm_page_assert_xbusied(m);
3928 * The object reference can be released without an atomic
3931 KASSERT((m->flags & PG_FICTITIOUS) != 0 ||
3932 m->ref_count == VPRC_OBJREF,
3933 ("vm_page_free_prep: page %p has unexpected ref_count %u",
3935 vm_page_object_remove(m);
3936 m->ref_count -= VPRC_OBJREF;
3938 vm_page_assert_unbusied(m);
3940 vm_page_busy_free(m);
3943 * If fictitious remove object association and
3946 if ((m->flags & PG_FICTITIOUS) != 0) {
3947 KASSERT(m->ref_count == 1,
3948 ("fictitious page %p is referenced", m));
3949 KASSERT(m->a.queue == PQ_NONE,
3950 ("fictitious page %p is queued", m));
3955 * Pages need not be dequeued before they are returned to the physical
3956 * memory allocator, but they must at least be marked for a deferred
3959 if ((m->oflags & VPO_UNMANAGED) == 0)
3960 vm_page_dequeue_deferred(m);
3965 if (m->ref_count != 0)
3966 panic("vm_page_free_prep: page %p has references", m);
3969 * Restore the default memory attribute to the page.
3971 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT)
3972 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT);
3974 #if VM_NRESERVLEVEL > 0
3976 * Determine whether the page belongs to a reservation. If the page was
3977 * allocated from a per-CPU cache, it cannot belong to a reservation, so
3978 * as an optimization, we avoid the check in that case.
3980 if ((m->flags & PG_PCPU_CACHE) == 0 && vm_reserv_free_page(m))
3990 * Returns the given page to the free list, disassociating it
3991 * from any VM object.
3993 * The object must be locked. The page must be exclusively busied if it
3994 * belongs to an object.
3997 vm_page_free_toq(vm_page_t m)
3999 struct vm_domain *vmd;
4002 if (!vm_page_free_prep(m))
4005 vmd = vm_pagequeue_domain(m);
4006 zone = vmd->vmd_pgcache[m->pool].zone;
4007 if ((m->flags & PG_PCPU_CACHE) != 0 && zone != NULL) {
4011 vm_domain_free_lock(vmd);
4012 vm_phys_free_pages(m, 0);
4013 vm_domain_free_unlock(vmd);
4014 vm_domain_freecnt_inc(vmd, 1);
4018 * vm_page_free_pages_toq:
4020 * Returns a list of pages to the free list, disassociating it
4021 * from any VM object. In other words, this is equivalent to
4022 * calling vm_page_free_toq() for each page of a list of VM objects.
4025 vm_page_free_pages_toq(struct spglist *free, bool update_wire_count)
4030 if (SLIST_EMPTY(free))
4034 while ((m = SLIST_FIRST(free)) != NULL) {
4036 SLIST_REMOVE_HEAD(free, plinks.s.ss);
4037 vm_page_free_toq(m);
4040 if (update_wire_count)
4045 * Mark this page as wired down. For managed pages, this prevents reclamation
4046 * by the page daemon, or when the containing object, if any, is destroyed.
4049 vm_page_wire(vm_page_t m)
4054 if (m->object != NULL && !vm_page_busied(m) &&
4055 !vm_object_busied(m->object))
4056 VM_OBJECT_ASSERT_LOCKED(m->object);
4058 KASSERT((m->flags & PG_FICTITIOUS) == 0 ||
4059 VPRC_WIRE_COUNT(m->ref_count) >= 1,
4060 ("vm_page_wire: fictitious page %p has zero wirings", m));
4062 old = atomic_fetchadd_int(&m->ref_count, 1);
4063 KASSERT(VPRC_WIRE_COUNT(old) != VPRC_WIRE_COUNT_MAX,
4064 ("vm_page_wire: counter overflow for page %p", m));
4065 if (VPRC_WIRE_COUNT(old) == 0) {
4066 if ((m->oflags & VPO_UNMANAGED) == 0)
4067 vm_page_aflag_set(m, PGA_DEQUEUE);
4073 * Attempt to wire a mapped page following a pmap lookup of that page.
4074 * This may fail if a thread is concurrently tearing down mappings of the page.
4075 * The transient failure is acceptable because it translates to the
4076 * failure of the caller pmap_extract_and_hold(), which should be then
4077 * followed by the vm_fault() fallback, see e.g. vm_fault_quick_hold_pages().
4080 vm_page_wire_mapped(vm_page_t m)
4087 ("vm_page_wire_mapped: wiring unreferenced page %p", m));
4088 if ((old & VPRC_BLOCKED) != 0)
4090 } while (!atomic_fcmpset_int(&m->ref_count, &old, old + 1));
4092 if (VPRC_WIRE_COUNT(old) == 0) {
4093 if ((m->oflags & VPO_UNMANAGED) == 0)
4094 vm_page_aflag_set(m, PGA_DEQUEUE);
4101 * Release a wiring reference to a managed page. If the page still belongs to
4102 * an object, update its position in the page queues to reflect the reference.
4103 * If the wiring was the last reference to the page, free the page.
4106 vm_page_unwire_managed(vm_page_t m, uint8_t nqueue, bool noreuse)
4110 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4111 ("%s: page %p is unmanaged", __func__, m));
4114 * Update LRU state before releasing the wiring reference.
4115 * Use a release store when updating the reference count to
4116 * synchronize with vm_page_free_prep().
4120 KASSERT(VPRC_WIRE_COUNT(old) > 0,
4121 ("vm_page_unwire: wire count underflow for page %p", m));
4123 if (old > VPRC_OBJREF + 1) {
4125 * The page has at least one other wiring reference. An
4126 * earlier iteration of this loop may have called
4127 * vm_page_release_toq() and cleared PGA_DEQUEUE, so
4128 * re-set it if necessary.
4130 if ((vm_page_astate_load(m).flags & PGA_DEQUEUE) == 0)
4131 vm_page_aflag_set(m, PGA_DEQUEUE);
4132 } else if (old == VPRC_OBJREF + 1) {
4134 * This is the last wiring. Clear PGA_DEQUEUE and
4135 * update the page's queue state to reflect the
4136 * reference. If the page does not belong to an object
4137 * (i.e., the VPRC_OBJREF bit is clear), we only need to
4138 * clear leftover queue state.
4140 vm_page_release_toq(m, nqueue, noreuse);
4141 } else if (old == 1) {
4142 vm_page_aflag_clear(m, PGA_DEQUEUE);
4144 } while (!atomic_fcmpset_rel_int(&m->ref_count, &old, old - 1));
4146 if (VPRC_WIRE_COUNT(old) == 1) {
4154 * Release one wiring of the specified page, potentially allowing it to be
4157 * Only managed pages belonging to an object can be paged out. If the number
4158 * of wirings transitions to zero and the page is eligible for page out, then
4159 * the page is added to the specified paging queue. If the released wiring
4160 * represented the last reference to the page, the page is freed.
4163 vm_page_unwire(vm_page_t m, uint8_t nqueue)
4166 KASSERT(nqueue < PQ_COUNT,
4167 ("vm_page_unwire: invalid queue %u request for page %p",
4170 if ((m->oflags & VPO_UNMANAGED) != 0) {
4171 if (vm_page_unwire_noq(m) && m->ref_count == 0)
4175 vm_page_unwire_managed(m, nqueue, false);
4179 * Unwire a page without (re-)inserting it into a page queue. It is up
4180 * to the caller to enqueue, requeue, or free the page as appropriate.
4181 * In most cases involving managed pages, vm_page_unwire() should be used
4185 vm_page_unwire_noq(vm_page_t m)
4189 old = vm_page_drop(m, 1);
4190 KASSERT(VPRC_WIRE_COUNT(old) != 0,
4191 ("%s: counter underflow for page %p", __func__, m));
4192 KASSERT((m->flags & PG_FICTITIOUS) == 0 || VPRC_WIRE_COUNT(old) > 1,
4193 ("%s: missing ref on fictitious page %p", __func__, m));
4195 if (VPRC_WIRE_COUNT(old) > 1)
4197 if ((m->oflags & VPO_UNMANAGED) == 0)
4198 vm_page_aflag_clear(m, PGA_DEQUEUE);
4204 * Ensure that the page ends up in the specified page queue. If the page is
4205 * active or being moved to the active queue, ensure that its act_count is
4206 * at least ACT_INIT but do not otherwise mess with it.
4208 static __always_inline void
4209 vm_page_mvqueue(vm_page_t m, const uint8_t nqueue, const uint16_t nflag)
4211 vm_page_astate_t old, new;
4213 KASSERT(m->ref_count > 0,
4214 ("%s: page %p does not carry any references", __func__, m));
4215 KASSERT(nflag == PGA_REQUEUE || nflag == PGA_REQUEUE_HEAD,
4216 ("%s: invalid flags %x", __func__, nflag));
4218 if ((m->oflags & VPO_UNMANAGED) != 0 || vm_page_wired(m))
4221 old = vm_page_astate_load(m);
4223 if ((old.flags & PGA_DEQUEUE) != 0)
4226 new.flags &= ~PGA_QUEUE_OP_MASK;
4227 if (nqueue == PQ_ACTIVE)
4228 new.act_count = max(old.act_count, ACT_INIT);
4229 if (old.queue == nqueue) {
4231 * There is no need to requeue pages already in the
4234 if (nqueue != PQ_ACTIVE ||
4235 (old.flags & PGA_ENQUEUED) == 0)
4241 } while (!vm_page_pqstate_commit(m, &old, new));
4245 * Put the specified page on the active list (if appropriate).
4248 vm_page_activate(vm_page_t m)
4251 vm_page_mvqueue(m, PQ_ACTIVE, PGA_REQUEUE);
4255 * Move the specified page to the tail of the inactive queue, or requeue
4256 * the page if it is already in the inactive queue.
4259 vm_page_deactivate(vm_page_t m)
4262 vm_page_mvqueue(m, PQ_INACTIVE, PGA_REQUEUE);
4266 vm_page_deactivate_noreuse(vm_page_t m)
4269 vm_page_mvqueue(m, PQ_INACTIVE, PGA_REQUEUE_HEAD);
4273 * Put a page in the laundry, or requeue it if it is already there.
4276 vm_page_launder(vm_page_t m)
4279 vm_page_mvqueue(m, PQ_LAUNDRY, PGA_REQUEUE);
4283 * Put a page in the PQ_UNSWAPPABLE holding queue.
4286 vm_page_unswappable(vm_page_t m)
4289 VM_OBJECT_ASSERT_LOCKED(m->object);
4290 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4291 ("page %p already unswappable", m));
4294 vm_page_enqueue(m, PQ_UNSWAPPABLE);
4298 * Release a page back to the page queues in preparation for unwiring.
4301 vm_page_release_toq(vm_page_t m, uint8_t nqueue, const bool noreuse)
4303 vm_page_astate_t old, new;
4307 * Use a check of the valid bits to determine whether we should
4308 * accelerate reclamation of the page. The object lock might not be
4309 * held here, in which case the check is racy. At worst we will either
4310 * accelerate reclamation of a valid page and violate LRU, or
4311 * unnecessarily defer reclamation of an invalid page.
4313 * If we were asked to not cache the page, place it near the head of the
4314 * inactive queue so that is reclaimed sooner.
4316 if (noreuse || vm_page_none_valid(m)) {
4317 nqueue = PQ_INACTIVE;
4318 nflag = PGA_REQUEUE_HEAD;
4320 nflag = PGA_REQUEUE;
4323 old = vm_page_astate_load(m);
4328 * If the page is already in the active queue and we are not
4329 * trying to accelerate reclamation, simply mark it as
4330 * referenced and avoid any queue operations.
4332 new.flags &= ~PGA_QUEUE_OP_MASK;
4333 if (nflag != PGA_REQUEUE_HEAD && old.queue == PQ_ACTIVE &&
4334 (old.flags & PGA_ENQUEUED) != 0)
4335 new.flags |= PGA_REFERENCED;
4340 } while (!vm_page_pqstate_commit(m, &old, new));
4344 * Unwire a page and either attempt to free it or re-add it to the page queues.
4347 vm_page_release(vm_page_t m, int flags)
4351 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4352 ("vm_page_release: page %p is unmanaged", m));
4354 if ((flags & VPR_TRYFREE) != 0) {
4356 object = atomic_load_ptr(&m->object);
4359 /* Depends on type-stability. */
4360 if (vm_page_busied(m) || !VM_OBJECT_TRYWLOCK(object))
4362 if (object == m->object) {
4363 vm_page_release_locked(m, flags);
4364 VM_OBJECT_WUNLOCK(object);
4367 VM_OBJECT_WUNLOCK(object);
4370 vm_page_unwire_managed(m, PQ_INACTIVE, flags != 0);
4373 /* See vm_page_release(). */
4375 vm_page_release_locked(vm_page_t m, int flags)
4378 VM_OBJECT_ASSERT_WLOCKED(m->object);
4379 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4380 ("vm_page_release_locked: page %p is unmanaged", m));
4382 if (vm_page_unwire_noq(m)) {
4383 if ((flags & VPR_TRYFREE) != 0 &&
4384 (m->object->ref_count == 0 || !pmap_page_is_mapped(m)) &&
4385 m->dirty == 0 && vm_page_tryxbusy(m)) {
4387 * An unlocked lookup may have wired the page before the
4388 * busy lock was acquired, in which case the page must
4391 if (__predict_true(!vm_page_wired(m))) {
4397 vm_page_release_toq(m, PQ_INACTIVE, flags != 0);
4403 vm_page_try_blocked_op(vm_page_t m, void (*op)(vm_page_t))
4407 KASSERT(m->object != NULL && (m->oflags & VPO_UNMANAGED) == 0,
4408 ("vm_page_try_blocked_op: page %p has no object", m));
4409 KASSERT(vm_page_busied(m),
4410 ("vm_page_try_blocked_op: page %p is not busy", m));
4411 VM_OBJECT_ASSERT_LOCKED(m->object);
4416 ("vm_page_try_blocked_op: page %p has no references", m));
4417 if (VPRC_WIRE_COUNT(old) != 0)
4419 } while (!atomic_fcmpset_int(&m->ref_count, &old, old | VPRC_BLOCKED));
4424 * If the object is read-locked, new wirings may be created via an
4427 old = vm_page_drop(m, VPRC_BLOCKED);
4428 KASSERT(!VM_OBJECT_WOWNED(m->object) ||
4429 old == (VPRC_BLOCKED | VPRC_OBJREF),
4430 ("vm_page_try_blocked_op: unexpected refcount value %u for %p",
4436 * Atomically check for wirings and remove all mappings of the page.
4439 vm_page_try_remove_all(vm_page_t m)
4442 return (vm_page_try_blocked_op(m, pmap_remove_all));
4446 * Atomically check for wirings and remove all writeable mappings of the page.
4449 vm_page_try_remove_write(vm_page_t m)
4452 return (vm_page_try_blocked_op(m, pmap_remove_write));
4458 * Apply the specified advice to the given page.
4461 vm_page_advise(vm_page_t m, int advice)
4464 VM_OBJECT_ASSERT_WLOCKED(m->object);
4465 vm_page_assert_xbusied(m);
4467 if (advice == MADV_FREE)
4469 * Mark the page clean. This will allow the page to be freed
4470 * without first paging it out. MADV_FREE pages are often
4471 * quickly reused by malloc(3), so we do not do anything that
4472 * would result in a page fault on a later access.
4475 else if (advice != MADV_DONTNEED) {
4476 if (advice == MADV_WILLNEED)
4477 vm_page_activate(m);
4481 if (advice != MADV_FREE && m->dirty == 0 && pmap_is_modified(m))
4485 * Clear any references to the page. Otherwise, the page daemon will
4486 * immediately reactivate the page.
4488 vm_page_aflag_clear(m, PGA_REFERENCED);
4491 * Place clean pages near the head of the inactive queue rather than
4492 * the tail, thus defeating the queue's LRU operation and ensuring that
4493 * the page will be reused quickly. Dirty pages not already in the
4494 * laundry are moved there.
4497 vm_page_deactivate_noreuse(m);
4498 else if (!vm_page_in_laundry(m))
4503 * vm_page_grab_release
4505 * Helper routine for grab functions to release busy on return.
4508 vm_page_grab_release(vm_page_t m, int allocflags)
4511 if ((allocflags & VM_ALLOC_NOBUSY) != 0) {
4512 if ((allocflags & VM_ALLOC_IGN_SBUSY) != 0)
4520 * vm_page_grab_sleep
4522 * Sleep for busy according to VM_ALLOC_ parameters. Returns true
4523 * if the caller should retry and false otherwise.
4525 * If the object is locked on entry the object will be unlocked with
4526 * false returns and still locked but possibly having been dropped
4527 * with true returns.
4530 vm_page_grab_sleep(vm_object_t object, vm_page_t m, vm_pindex_t pindex,
4531 const char *wmesg, int allocflags, bool locked)
4534 if ((allocflags & VM_ALLOC_NOWAIT) != 0)
4538 * Reference the page before unlocking and sleeping so that
4539 * the page daemon is less likely to reclaim it.
4541 if (locked && (allocflags & VM_ALLOC_NOCREAT) == 0)
4542 vm_page_reference(m);
4544 if (_vm_page_busy_sleep(object, m, pindex, wmesg, allocflags, locked) &&
4546 VM_OBJECT_WLOCK(object);
4547 if ((allocflags & VM_ALLOC_WAITFAIL) != 0)
4554 * Assert that the grab flags are valid.
4557 vm_page_grab_check(int allocflags)
4560 KASSERT((allocflags & VM_ALLOC_NOBUSY) == 0 ||
4561 (allocflags & VM_ALLOC_WIRED) != 0,
4562 ("vm_page_grab*: the pages must be busied or wired"));
4564 KASSERT((allocflags & VM_ALLOC_SBUSY) == 0 ||
4565 (allocflags & VM_ALLOC_IGN_SBUSY) != 0,
4566 ("vm_page_grab*: VM_ALLOC_SBUSY/VM_ALLOC_IGN_SBUSY mismatch"));
4570 * Calculate the page allocation flags for grab.
4573 vm_page_grab_pflags(int allocflags)
4577 pflags = allocflags &
4578 ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL |
4579 VM_ALLOC_NOBUSY | VM_ALLOC_IGN_SBUSY);
4580 if ((allocflags & VM_ALLOC_NOWAIT) == 0)
4581 pflags |= VM_ALLOC_WAITFAIL;
4582 if ((allocflags & VM_ALLOC_IGN_SBUSY) != 0)
4583 pflags |= VM_ALLOC_SBUSY;
4589 * Grab a page, waiting until we are waken up due to the page
4590 * changing state. We keep on waiting, if the page continues
4591 * to be in the object. If the page doesn't exist, first allocate it
4592 * and then conditionally zero it.
4594 * This routine may sleep.
4596 * The object must be locked on entry. The lock will, however, be released
4597 * and reacquired if the routine sleeps.
4600 vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags)
4604 VM_OBJECT_ASSERT_WLOCKED(object);
4605 vm_page_grab_check(allocflags);
4608 if ((m = vm_page_lookup(object, pindex)) != NULL) {
4609 if (!vm_page_tryacquire(m, allocflags)) {
4610 if (vm_page_grab_sleep(object, m, pindex, "pgrbwt",
4617 if ((allocflags & VM_ALLOC_NOCREAT) != 0)
4619 m = vm_page_alloc(object, pindex, vm_page_grab_pflags(allocflags));
4621 if ((allocflags & (VM_ALLOC_NOWAIT | VM_ALLOC_WAITFAIL)) != 0)
4625 if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0)
4629 vm_page_grab_release(m, allocflags);
4635 * Locklessly attempt to acquire a page given a (object, pindex) tuple
4636 * and an optional previous page to avoid the radix lookup. The resulting
4637 * page will be validated against the identity tuple and busied or wired
4638 * as requested. A NULL *mp return guarantees that the page was not in
4639 * radix at the time of the call but callers must perform higher level
4640 * synchronization or retry the operation under a lock if they require
4641 * an atomic answer. This is the only lock free validation routine,
4642 * other routines can depend on the resulting page state.
4644 * The return value indicates whether the operation failed due to caller
4645 * flags. The return is tri-state with mp:
4647 * (true, *mp != NULL) - The operation was successful.
4648 * (true, *mp == NULL) - The page was not found in tree.
4649 * (false, *mp == NULL) - WAITFAIL or NOWAIT prevented acquisition.
4652 vm_page_acquire_unlocked(vm_object_t object, vm_pindex_t pindex,
4653 vm_page_t prev, vm_page_t *mp, int allocflags)
4657 vm_page_grab_check(allocflags);
4658 MPASS(prev == NULL || vm_page_busied(prev) || vm_page_wired(prev));
4663 * We may see a false NULL here because the previous page
4664 * has been removed or just inserted and the list is loaded
4665 * without barriers. Switch to radix to verify.
4667 if (prev == NULL || (m = TAILQ_NEXT(prev, listq)) == NULL ||
4668 QMD_IS_TRASHED(m) || m->pindex != pindex ||
4669 atomic_load_ptr(&m->object) != object) {
4672 * This guarantees the result is instantaneously
4675 m = vm_radix_lookup_unlocked(&object->rtree, pindex);
4679 if (vm_page_trybusy(m, allocflags)) {
4680 if (m->object == object && m->pindex == pindex)
4683 vm_page_busy_release(m);
4687 if (!vm_page_grab_sleep(object, m, pindex, "pgnslp",
4691 if ((allocflags & VM_ALLOC_WIRED) != 0)
4693 vm_page_grab_release(m, allocflags);
4699 * Try to locklessly grab a page and fall back to the object lock if NOCREAT
4703 vm_page_grab_unlocked(vm_object_t object, vm_pindex_t pindex, int allocflags)
4707 vm_page_grab_check(allocflags);
4709 if (!vm_page_acquire_unlocked(object, pindex, NULL, &m, allocflags))
4715 * The radix lockless lookup should never return a false negative
4716 * errors. If the user specifies NOCREAT they are guaranteed there
4717 * was no page present at the instant of the call. A NOCREAT caller
4718 * must handle create races gracefully.
4720 if ((allocflags & VM_ALLOC_NOCREAT) != 0)
4723 VM_OBJECT_WLOCK(object);
4724 m = vm_page_grab(object, pindex, allocflags);
4725 VM_OBJECT_WUNLOCK(object);
4731 * Grab a page and make it valid, paging in if necessary. Pages missing from
4732 * their pager are zero filled and validated. If a VM_ALLOC_COUNT is supplied
4733 * and the page is not valid as many as VM_INITIAL_PAGEIN pages can be brought
4734 * in simultaneously. Additional pages will be left on a paging queue but
4735 * will neither be wired nor busy regardless of allocflags.
4738 vm_page_grab_valid(vm_page_t *mp, vm_object_t object, vm_pindex_t pindex, int allocflags)
4741 vm_page_t ma[VM_INITIAL_PAGEIN];
4742 int after, i, pflags, rv;
4744 KASSERT((allocflags & VM_ALLOC_SBUSY) == 0 ||
4745 (allocflags & VM_ALLOC_IGN_SBUSY) != 0,
4746 ("vm_page_grab_valid: VM_ALLOC_SBUSY/VM_ALLOC_IGN_SBUSY mismatch"));
4747 KASSERT((allocflags &
4748 (VM_ALLOC_NOWAIT | VM_ALLOC_WAITFAIL | VM_ALLOC_ZERO)) == 0,
4749 ("vm_page_grab_valid: Invalid flags 0x%X", allocflags));
4750 VM_OBJECT_ASSERT_WLOCKED(object);
4751 pflags = allocflags & ~(VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY |
4752 VM_ALLOC_WIRED | VM_ALLOC_IGN_SBUSY);
4753 pflags |= VM_ALLOC_WAITFAIL;
4756 if ((m = vm_page_lookup(object, pindex)) != NULL) {
4758 * If the page is fully valid it can only become invalid
4759 * with the object lock held. If it is not valid it can
4760 * become valid with the busy lock held. Therefore, we
4761 * may unnecessarily lock the exclusive busy here if we
4762 * race with I/O completion not using the object lock.
4763 * However, we will not end up with an invalid page and a
4766 if (!vm_page_trybusy(m,
4767 vm_page_all_valid(m) ? allocflags : 0)) {
4768 (void)vm_page_grab_sleep(object, m, pindex, "pgrbwt",
4772 if (vm_page_all_valid(m))
4774 if ((allocflags & VM_ALLOC_NOCREAT) != 0) {
4775 vm_page_busy_release(m);
4777 return (VM_PAGER_FAIL);
4779 } else if ((allocflags & VM_ALLOC_NOCREAT) != 0) {
4781 return (VM_PAGER_FAIL);
4782 } else if ((m = vm_page_alloc(object, pindex, pflags)) == NULL) {
4783 if (!vm_pager_can_alloc_page(object, pindex)) {
4785 return (VM_PAGER_AGAIN);
4790 vm_page_assert_xbusied(m);
4791 if (vm_pager_has_page(object, pindex, NULL, &after)) {
4792 after = MIN(after, VM_INITIAL_PAGEIN);
4793 after = MIN(after, allocflags >> VM_ALLOC_COUNT_SHIFT);
4794 after = MAX(after, 1);
4796 for (i = 1; i < after; i++) {
4797 if ((ma[i] = vm_page_next(ma[i - 1])) != NULL) {
4798 if (vm_page_any_valid(ma[i]) ||
4799 !vm_page_tryxbusy(ma[i]))
4802 ma[i] = vm_page_alloc(object, m->pindex + i,
4809 vm_object_pip_add(object, after);
4810 VM_OBJECT_WUNLOCK(object);
4811 rv = vm_pager_get_pages(object, ma, after, NULL, NULL);
4812 VM_OBJECT_WLOCK(object);
4813 vm_object_pip_wakeupn(object, after);
4814 /* Pager may have replaced a page. */
4816 if (rv != VM_PAGER_OK) {
4817 for (i = 0; i < after; i++) {
4818 if (!vm_page_wired(ma[i]))
4819 vm_page_free(ma[i]);
4821 vm_page_xunbusy(ma[i]);
4826 for (i = 1; i < after; i++)
4827 vm_page_readahead_finish(ma[i]);
4828 MPASS(vm_page_all_valid(m));
4830 vm_page_zero_invalid(m, TRUE);
4833 if ((allocflags & VM_ALLOC_WIRED) != 0)
4835 if ((allocflags & VM_ALLOC_SBUSY) != 0 && vm_page_xbusied(m))
4836 vm_page_busy_downgrade(m);
4837 else if ((allocflags & VM_ALLOC_NOBUSY) != 0)
4838 vm_page_busy_release(m);
4840 return (VM_PAGER_OK);
4844 * Locklessly grab a valid page. If the page is not valid or not yet
4845 * allocated this will fall back to the object lock method.
4848 vm_page_grab_valid_unlocked(vm_page_t *mp, vm_object_t object,
4849 vm_pindex_t pindex, int allocflags)
4855 KASSERT((allocflags & VM_ALLOC_SBUSY) == 0 ||
4856 (allocflags & VM_ALLOC_IGN_SBUSY) != 0,
4857 ("vm_page_grab_valid_unlocked: VM_ALLOC_SBUSY/VM_ALLOC_IGN_SBUSY "
4859 KASSERT((allocflags &
4860 (VM_ALLOC_NOWAIT | VM_ALLOC_WAITFAIL | VM_ALLOC_ZERO)) == 0,
4861 ("vm_page_grab_valid_unlocked: Invalid flags 0x%X", allocflags));
4864 * Attempt a lockless lookup and busy. We need at least an sbusy
4865 * before we can inspect the valid field and return a wired page.
4867 flags = allocflags & ~(VM_ALLOC_NOBUSY | VM_ALLOC_WIRED);
4868 if (!vm_page_acquire_unlocked(object, pindex, NULL, mp, flags))
4869 return (VM_PAGER_FAIL);
4870 if ((m = *mp) != NULL) {
4871 if (vm_page_all_valid(m)) {
4872 if ((allocflags & VM_ALLOC_WIRED) != 0)
4874 vm_page_grab_release(m, allocflags);
4875 return (VM_PAGER_OK);
4877 vm_page_busy_release(m);
4879 if ((allocflags & VM_ALLOC_NOCREAT) != 0) {
4881 return (VM_PAGER_FAIL);
4883 VM_OBJECT_WLOCK(object);
4884 error = vm_page_grab_valid(mp, object, pindex, allocflags);
4885 VM_OBJECT_WUNLOCK(object);
4891 * Return the specified range of pages from the given object. For each
4892 * page offset within the range, if a page already exists within the object
4893 * at that offset and it is busy, then wait for it to change state. If,
4894 * instead, the page doesn't exist, then allocate it.
4896 * The caller must always specify an allocation class.
4898 * allocation classes:
4899 * VM_ALLOC_NORMAL normal process request
4900 * VM_ALLOC_SYSTEM system *really* needs the pages
4902 * The caller must always specify that the pages are to be busied and/or
4905 * optional allocation flags:
4906 * VM_ALLOC_IGN_SBUSY do not sleep on soft busy pages
4907 * VM_ALLOC_NOBUSY do not exclusive busy the page
4908 * VM_ALLOC_NOWAIT do not sleep
4909 * VM_ALLOC_SBUSY set page to sbusy state
4910 * VM_ALLOC_WIRED wire the pages
4911 * VM_ALLOC_ZERO zero and validate any invalid pages
4913 * If VM_ALLOC_NOWAIT is not specified, this routine may sleep. Otherwise, it
4914 * may return a partial prefix of the requested range.
4917 vm_page_grab_pages(vm_object_t object, vm_pindex_t pindex, int allocflags,
4918 vm_page_t *ma, int count)
4924 VM_OBJECT_ASSERT_WLOCKED(object);
4925 KASSERT(((u_int)allocflags >> VM_ALLOC_COUNT_SHIFT) == 0,
4926 ("vm_page_grap_pages: VM_ALLOC_COUNT() is not allowed"));
4928 ("vm_page_grab_pages: invalid page count %d", count));
4929 vm_page_grab_check(allocflags);
4931 pflags = vm_page_grab_pflags(allocflags);
4934 m = vm_radix_lookup_le(&object->rtree, pindex + i);
4935 if (m == NULL || m->pindex != pindex + i) {
4939 mpred = TAILQ_PREV(m, pglist, listq);
4940 for (; i < count; i++) {
4942 if (!vm_page_tryacquire(m, allocflags)) {
4943 if (vm_page_grab_sleep(object, m, pindex + i,
4944 "grbmaw", allocflags, true))
4949 if ((allocflags & VM_ALLOC_NOCREAT) != 0)
4951 m = vm_page_alloc_after(object, pindex + i,
4952 pflags | VM_ALLOC_COUNT(count - i), mpred);
4954 if ((allocflags & (VM_ALLOC_NOWAIT |
4955 VM_ALLOC_WAITFAIL)) != 0)
4960 if (vm_page_none_valid(m) &&
4961 (allocflags & VM_ALLOC_ZERO) != 0) {
4962 if ((m->flags & PG_ZERO) == 0)
4966 vm_page_grab_release(m, allocflags);
4968 m = vm_page_next(m);
4974 * Unlocked variant of vm_page_grab_pages(). This accepts the same flags
4975 * and will fall back to the locked variant to handle allocation.
4978 vm_page_grab_pages_unlocked(vm_object_t object, vm_pindex_t pindex,
4979 int allocflags, vm_page_t *ma, int count)
4986 ("vm_page_grab_pages_unlocked: invalid page count %d", count));
4987 vm_page_grab_check(allocflags);
4990 * Modify flags for lockless acquire to hold the page until we
4991 * set it valid if necessary.
4993 flags = allocflags & ~VM_ALLOC_NOBUSY;
4995 for (i = 0; i < count; i++, pindex++) {
4996 if (!vm_page_acquire_unlocked(object, pindex, pred, &m, flags))
5000 if ((flags & VM_ALLOC_ZERO) != 0 && vm_page_none_valid(m)) {
5001 if ((m->flags & PG_ZERO) == 0)
5005 /* m will still be wired or busy according to flags. */
5006 vm_page_grab_release(m, allocflags);
5009 if (i == count || (allocflags & VM_ALLOC_NOCREAT) != 0)
5012 VM_OBJECT_WLOCK(object);
5013 i += vm_page_grab_pages(object, pindex, allocflags, &ma[i], count);
5014 VM_OBJECT_WUNLOCK(object);
5020 * Mapping function for valid or dirty bits in a page.
5022 * Inputs are required to range within a page.
5025 vm_page_bits(int base, int size)
5031 base + size <= PAGE_SIZE,
5032 ("vm_page_bits: illegal base/size %d/%d", base, size)
5035 if (size == 0) /* handle degenerate case */
5038 first_bit = base >> DEV_BSHIFT;
5039 last_bit = (base + size - 1) >> DEV_BSHIFT;
5041 return (((vm_page_bits_t)2 << last_bit) -
5042 ((vm_page_bits_t)1 << first_bit));
5046 vm_page_bits_set(vm_page_t m, vm_page_bits_t *bits, vm_page_bits_t set)
5049 #if PAGE_SIZE == 32768
5050 atomic_set_64((uint64_t *)bits, set);
5051 #elif PAGE_SIZE == 16384
5052 atomic_set_32((uint32_t *)bits, set);
5053 #elif (PAGE_SIZE == 8192) && defined(atomic_set_16)
5054 atomic_set_16((uint16_t *)bits, set);
5055 #elif (PAGE_SIZE == 4096) && defined(atomic_set_8)
5056 atomic_set_8((uint8_t *)bits, set);
5057 #else /* PAGE_SIZE <= 8192 */
5061 addr = (uintptr_t)bits;
5063 * Use a trick to perform a 32-bit atomic on the
5064 * containing aligned word, to not depend on the existence
5065 * of atomic_{set, clear}_{8, 16}.
5067 shift = addr & (sizeof(uint32_t) - 1);
5068 #if BYTE_ORDER == BIG_ENDIAN
5069 shift = (sizeof(uint32_t) - sizeof(vm_page_bits_t) - shift) * NBBY;
5073 addr &= ~(sizeof(uint32_t) - 1);
5074 atomic_set_32((uint32_t *)addr, set << shift);
5075 #endif /* PAGE_SIZE */
5079 vm_page_bits_clear(vm_page_t m, vm_page_bits_t *bits, vm_page_bits_t clear)
5082 #if PAGE_SIZE == 32768
5083 atomic_clear_64((uint64_t *)bits, clear);
5084 #elif PAGE_SIZE == 16384
5085 atomic_clear_32((uint32_t *)bits, clear);
5086 #elif (PAGE_SIZE == 8192) && defined(atomic_clear_16)
5087 atomic_clear_16((uint16_t *)bits, clear);
5088 #elif (PAGE_SIZE == 4096) && defined(atomic_clear_8)
5089 atomic_clear_8((uint8_t *)bits, clear);
5090 #else /* PAGE_SIZE <= 8192 */
5094 addr = (uintptr_t)bits;
5096 * Use a trick to perform a 32-bit atomic on the
5097 * containing aligned word, to not depend on the existence
5098 * of atomic_{set, clear}_{8, 16}.
5100 shift = addr & (sizeof(uint32_t) - 1);
5101 #if BYTE_ORDER == BIG_ENDIAN
5102 shift = (sizeof(uint32_t) - sizeof(vm_page_bits_t) - shift) * NBBY;
5106 addr &= ~(sizeof(uint32_t) - 1);
5107 atomic_clear_32((uint32_t *)addr, clear << shift);
5108 #endif /* PAGE_SIZE */
5111 static inline vm_page_bits_t
5112 vm_page_bits_swap(vm_page_t m, vm_page_bits_t *bits, vm_page_bits_t newbits)
5114 #if PAGE_SIZE == 32768
5118 while (atomic_fcmpset_64(bits, &old, newbits) == 0);
5120 #elif PAGE_SIZE == 16384
5124 while (atomic_fcmpset_32(bits, &old, newbits) == 0);
5126 #elif (PAGE_SIZE == 8192) && defined(atomic_fcmpset_16)
5130 while (atomic_fcmpset_16(bits, &old, newbits) == 0);
5132 #elif (PAGE_SIZE == 4096) && defined(atomic_fcmpset_8)
5136 while (atomic_fcmpset_8(bits, &old, newbits) == 0);
5138 #else /* PAGE_SIZE <= 4096*/
5140 uint32_t old, new, mask;
5143 addr = (uintptr_t)bits;
5145 * Use a trick to perform a 32-bit atomic on the
5146 * containing aligned word, to not depend on the existence
5147 * of atomic_{set, swap, clear}_{8, 16}.
5149 shift = addr & (sizeof(uint32_t) - 1);
5150 #if BYTE_ORDER == BIG_ENDIAN
5151 shift = (sizeof(uint32_t) - sizeof(vm_page_bits_t) - shift) * NBBY;
5155 addr &= ~(sizeof(uint32_t) - 1);
5156 mask = VM_PAGE_BITS_ALL << shift;
5161 new |= newbits << shift;
5162 } while (atomic_fcmpset_32((uint32_t *)addr, &old, new) == 0);
5163 return (old >> shift);
5164 #endif /* PAGE_SIZE */
5168 * vm_page_set_valid_range:
5170 * Sets portions of a page valid. The arguments are expected
5171 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
5172 * of any partial chunks touched by the range. The invalid portion of
5173 * such chunks will be zeroed.
5175 * (base + size) must be less then or equal to PAGE_SIZE.
5178 vm_page_set_valid_range(vm_page_t m, int base, int size)
5181 vm_page_bits_t pagebits;
5183 vm_page_assert_busied(m);
5184 if (size == 0) /* handle degenerate case */
5188 * If the base is not DEV_BSIZE aligned and the valid
5189 * bit is clear, we have to zero out a portion of the
5192 if ((frag = rounddown2(base, DEV_BSIZE)) != base &&
5193 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0)
5194 pmap_zero_page_area(m, frag, base - frag);
5197 * If the ending offset is not DEV_BSIZE aligned and the
5198 * valid bit is clear, we have to zero out a portion of
5201 endoff = base + size;
5202 if ((frag = rounddown2(endoff, DEV_BSIZE)) != endoff &&
5203 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0)
5204 pmap_zero_page_area(m, endoff,
5205 DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
5208 * Assert that no previously invalid block that is now being validated
5211 KASSERT((~m->valid & vm_page_bits(base, size) & m->dirty) == 0,
5212 ("vm_page_set_valid_range: page %p is dirty", m));
5215 * Set valid bits inclusive of any overlap.
5217 pagebits = vm_page_bits(base, size);
5218 if (vm_page_xbusied(m))
5219 m->valid |= pagebits;
5221 vm_page_bits_set(m, &m->valid, pagebits);
5225 * Set the page dirty bits and free the invalid swap space if
5226 * present. Returns the previous dirty bits.
5229 vm_page_set_dirty(vm_page_t m)
5233 VM_PAGE_OBJECT_BUSY_ASSERT(m);
5235 if (vm_page_xbusied(m) && !pmap_page_is_write_mapped(m)) {
5237 m->dirty = VM_PAGE_BITS_ALL;
5239 old = vm_page_bits_swap(m, &m->dirty, VM_PAGE_BITS_ALL);
5240 if (old == 0 && (m->a.flags & PGA_SWAP_SPACE) != 0)
5241 vm_pager_page_unswapped(m);
5247 * Clear the given bits from the specified page's dirty field.
5249 static __inline void
5250 vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits)
5253 vm_page_assert_busied(m);
5256 * If the page is xbusied and not write mapped we are the
5257 * only thread that can modify dirty bits. Otherwise, The pmap
5258 * layer can call vm_page_dirty() without holding a distinguished
5259 * lock. The combination of page busy and atomic operations
5260 * suffice to guarantee consistency of the page dirty field.
5262 if (vm_page_xbusied(m) && !pmap_page_is_write_mapped(m))
5263 m->dirty &= ~pagebits;
5265 vm_page_bits_clear(m, &m->dirty, pagebits);
5269 * vm_page_set_validclean:
5271 * Sets portions of a page valid and clean. The arguments are expected
5272 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
5273 * of any partial chunks touched by the range. The invalid portion of
5274 * such chunks will be zero'd.
5276 * (base + size) must be less then or equal to PAGE_SIZE.
5279 vm_page_set_validclean(vm_page_t m, int base, int size)
5281 vm_page_bits_t oldvalid, pagebits;
5284 vm_page_assert_busied(m);
5285 if (size == 0) /* handle degenerate case */
5289 * If the base is not DEV_BSIZE aligned and the valid
5290 * bit is clear, we have to zero out a portion of the
5293 if ((frag = rounddown2(base, DEV_BSIZE)) != base &&
5294 (m->valid & ((vm_page_bits_t)1 << (base >> DEV_BSHIFT))) == 0)
5295 pmap_zero_page_area(m, frag, base - frag);
5298 * If the ending offset is not DEV_BSIZE aligned and the
5299 * valid bit is clear, we have to zero out a portion of
5302 endoff = base + size;
5303 if ((frag = rounddown2(endoff, DEV_BSIZE)) != endoff &&
5304 (m->valid & ((vm_page_bits_t)1 << (endoff >> DEV_BSHIFT))) == 0)
5305 pmap_zero_page_area(m, endoff,
5306 DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
5309 * Set valid, clear dirty bits. If validating the entire
5310 * page we can safely clear the pmap modify bit. We also
5311 * use this opportunity to clear the PGA_NOSYNC flag. If a process
5312 * takes a write fault on a MAP_NOSYNC memory area the flag will
5315 * We set valid bits inclusive of any overlap, but we can only
5316 * clear dirty bits for DEV_BSIZE chunks that are fully within
5319 oldvalid = m->valid;
5320 pagebits = vm_page_bits(base, size);
5321 if (vm_page_xbusied(m))
5322 m->valid |= pagebits;
5324 vm_page_bits_set(m, &m->valid, pagebits);
5326 if ((frag = base & (DEV_BSIZE - 1)) != 0) {
5327 frag = DEV_BSIZE - frag;
5333 pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1));
5335 if (base == 0 && size == PAGE_SIZE) {
5337 * The page can only be modified within the pmap if it is
5338 * mapped, and it can only be mapped if it was previously
5341 if (oldvalid == VM_PAGE_BITS_ALL)
5343 * Perform the pmap_clear_modify() first. Otherwise,
5344 * a concurrent pmap operation, such as
5345 * pmap_protect(), could clear a modification in the
5346 * pmap and set the dirty field on the page before
5347 * pmap_clear_modify() had begun and after the dirty
5348 * field was cleared here.
5350 pmap_clear_modify(m);
5352 vm_page_aflag_clear(m, PGA_NOSYNC);
5353 } else if (oldvalid != VM_PAGE_BITS_ALL && vm_page_xbusied(m))
5354 m->dirty &= ~pagebits;
5356 vm_page_clear_dirty_mask(m, pagebits);
5360 vm_page_clear_dirty(vm_page_t m, int base, int size)
5363 vm_page_clear_dirty_mask(m, vm_page_bits(base, size));
5367 * vm_page_set_invalid:
5369 * Invalidates DEV_BSIZE'd chunks within a page. Both the
5370 * valid and dirty bits for the effected areas are cleared.
5373 vm_page_set_invalid(vm_page_t m, int base, int size)
5375 vm_page_bits_t bits;
5379 * The object lock is required so that pages can't be mapped
5380 * read-only while we're in the process of invalidating them.
5383 VM_OBJECT_ASSERT_WLOCKED(object);
5384 vm_page_assert_busied(m);
5386 if (object->type == OBJT_VNODE && base == 0 && IDX_TO_OFF(m->pindex) +
5387 size >= object->un_pager.vnp.vnp_size)
5388 bits = VM_PAGE_BITS_ALL;
5390 bits = vm_page_bits(base, size);
5391 if (object->ref_count != 0 && vm_page_all_valid(m) && bits != 0)
5393 KASSERT((bits == 0 && vm_page_all_valid(m)) ||
5394 !pmap_page_is_mapped(m),
5395 ("vm_page_set_invalid: page %p is mapped", m));
5396 if (vm_page_xbusied(m)) {
5400 vm_page_bits_clear(m, &m->valid, bits);
5401 vm_page_bits_clear(m, &m->dirty, bits);
5408 * Invalidates the entire page. The page must be busy, unmapped, and
5409 * the enclosing object must be locked. The object locks protects
5410 * against concurrent read-only pmap enter which is done without
5414 vm_page_invalid(vm_page_t m)
5417 vm_page_assert_busied(m);
5418 VM_OBJECT_ASSERT_WLOCKED(m->object);
5419 MPASS(!pmap_page_is_mapped(m));
5421 if (vm_page_xbusied(m))
5424 vm_page_bits_clear(m, &m->valid, VM_PAGE_BITS_ALL);
5428 * vm_page_zero_invalid()
5430 * The kernel assumes that the invalid portions of a page contain
5431 * garbage, but such pages can be mapped into memory by user code.
5432 * When this occurs, we must zero out the non-valid portions of the
5433 * page so user code sees what it expects.
5435 * Pages are most often semi-valid when the end of a file is mapped
5436 * into memory and the file's size is not page aligned.
5439 vm_page_zero_invalid(vm_page_t m, boolean_t setvalid)
5445 * Scan the valid bits looking for invalid sections that
5446 * must be zeroed. Invalid sub-DEV_BSIZE'd areas ( where the
5447 * valid bit may be set ) have already been zeroed by
5448 * vm_page_set_validclean().
5450 for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) {
5451 if (i == (PAGE_SIZE / DEV_BSIZE) ||
5452 (m->valid & ((vm_page_bits_t)1 << i))) {
5454 pmap_zero_page_area(m,
5455 b << DEV_BSHIFT, (i - b) << DEV_BSHIFT);
5462 * setvalid is TRUE when we can safely set the zero'd areas
5463 * as being valid. We can do this if there are no cache consistency
5464 * issues. e.g. it is ok to do with UFS, but not ok to do with NFS.
5473 * Is (partial) page valid? Note that the case where size == 0
5474 * will return FALSE in the degenerate case where the page is
5475 * entirely invalid, and TRUE otherwise.
5477 * Some callers envoke this routine without the busy lock held and
5478 * handle races via higher level locks. Typical callers should
5479 * hold a busy lock to prevent invalidation.
5482 vm_page_is_valid(vm_page_t m, int base, int size)
5484 vm_page_bits_t bits;
5486 bits = vm_page_bits(base, size);
5487 return (vm_page_any_valid(m) && (m->valid & bits) == bits);
5491 * Returns true if all of the specified predicates are true for the entire
5492 * (super)page and false otherwise.
5495 vm_page_ps_test(vm_page_t m, int flags, vm_page_t skip_m)
5501 if (skip_m != NULL && skip_m->object != object)
5503 VM_OBJECT_ASSERT_LOCKED(object);
5504 npages = atop(pagesizes[m->psind]);
5507 * The physically contiguous pages that make up a superpage, i.e., a
5508 * page with a page size index ("psind") greater than zero, will
5509 * occupy adjacent entries in vm_page_array[].
5511 for (i = 0; i < npages; i++) {
5512 /* Always test object consistency, including "skip_m". */
5513 if (m[i].object != object)
5515 if (&m[i] == skip_m)
5517 if ((flags & PS_NONE_BUSY) != 0 && vm_page_busied(&m[i]))
5519 if ((flags & PS_ALL_DIRTY) != 0) {
5521 * Calling vm_page_test_dirty() or pmap_is_modified()
5522 * might stop this case from spuriously returning
5523 * "false". However, that would require a write lock
5524 * on the object containing "m[i]".
5526 if (m[i].dirty != VM_PAGE_BITS_ALL)
5529 if ((flags & PS_ALL_VALID) != 0 &&
5530 m[i].valid != VM_PAGE_BITS_ALL)
5537 * Set the page's dirty bits if the page is modified.
5540 vm_page_test_dirty(vm_page_t m)
5543 vm_page_assert_busied(m);
5544 if (m->dirty != VM_PAGE_BITS_ALL && pmap_is_modified(m))
5549 vm_page_valid(vm_page_t m)
5552 vm_page_assert_busied(m);
5553 if (vm_page_xbusied(m))
5554 m->valid = VM_PAGE_BITS_ALL;
5556 vm_page_bits_set(m, &m->valid, VM_PAGE_BITS_ALL);
5560 vm_page_lock_KBI(vm_page_t m, const char *file, int line)
5563 mtx_lock_flags_(vm_page_lockptr(m), 0, file, line);
5567 vm_page_unlock_KBI(vm_page_t m, const char *file, int line)
5570 mtx_unlock_flags_(vm_page_lockptr(m), 0, file, line);
5574 vm_page_trylock_KBI(vm_page_t m, const char *file, int line)
5577 return (mtx_trylock_flags_(vm_page_lockptr(m), 0, file, line));
5580 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
5582 vm_page_assert_locked_KBI(vm_page_t m, const char *file, int line)
5585 vm_page_lock_assert_KBI(m, MA_OWNED, file, line);
5589 vm_page_lock_assert_KBI(vm_page_t m, int a, const char *file, int line)
5592 mtx_assert_(vm_page_lockptr(m), a, file, line);
5598 vm_page_object_busy_assert(vm_page_t m)
5602 * Certain of the page's fields may only be modified by the
5603 * holder of a page or object busy.
5605 if (m->object != NULL && !vm_page_busied(m))
5606 VM_OBJECT_ASSERT_BUSY(m->object);
5610 vm_page_assert_pga_writeable(vm_page_t m, uint16_t bits)
5613 if ((bits & PGA_WRITEABLE) == 0)
5617 * The PGA_WRITEABLE flag can only be set if the page is
5618 * managed, is exclusively busied or the object is locked.
5619 * Currently, this flag is only set by pmap_enter().
5621 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5622 ("PGA_WRITEABLE on unmanaged page"));
5623 if (!vm_page_xbusied(m))
5624 VM_OBJECT_ASSERT_BUSY(m->object);
5628 #include "opt_ddb.h"
5630 #include <sys/kernel.h>
5632 #include <ddb/ddb.h>
5634 DB_SHOW_COMMAND_FLAGS(page, vm_page_print_page_info, DB_CMD_MEMSAFE)
5637 db_printf("vm_cnt.v_free_count: %d\n", vm_free_count());
5638 db_printf("vm_cnt.v_inactive_count: %d\n", vm_inactive_count());
5639 db_printf("vm_cnt.v_active_count: %d\n", vm_active_count());
5640 db_printf("vm_cnt.v_laundry_count: %d\n", vm_laundry_count());
5641 db_printf("vm_cnt.v_wire_count: %d\n", vm_wire_count());
5642 db_printf("vm_cnt.v_free_reserved: %d\n", vm_cnt.v_free_reserved);
5643 db_printf("vm_cnt.v_free_min: %d\n", vm_cnt.v_free_min);
5644 db_printf("vm_cnt.v_free_target: %d\n", vm_cnt.v_free_target);
5645 db_printf("vm_cnt.v_inactive_target: %d\n", vm_cnt.v_inactive_target);
5648 DB_SHOW_COMMAND_FLAGS(pageq, vm_page_print_pageq_info, DB_CMD_MEMSAFE)
5652 db_printf("pq_free %d\n", vm_free_count());
5653 for (dom = 0; dom < vm_ndomains; dom++) {
5655 "dom %d page_cnt %d free %d pq_act %d pq_inact %d pq_laund %d pq_unsw %d\n",
5657 vm_dom[dom].vmd_page_count,
5658 vm_dom[dom].vmd_free_count,
5659 vm_dom[dom].vmd_pagequeues[PQ_ACTIVE].pq_cnt,
5660 vm_dom[dom].vmd_pagequeues[PQ_INACTIVE].pq_cnt,
5661 vm_dom[dom].vmd_pagequeues[PQ_LAUNDRY].pq_cnt,
5662 vm_dom[dom].vmd_pagequeues[PQ_UNSWAPPABLE].pq_cnt);
5666 DB_SHOW_COMMAND(pginfo, vm_page_print_pginfo)
5669 boolean_t phys, virt;
5672 db_printf("show pginfo addr\n");
5676 phys = strchr(modif, 'p') != NULL;
5677 virt = strchr(modif, 'v') != NULL;
5679 m = PHYS_TO_VM_PAGE(pmap_kextract(addr));
5681 m = PHYS_TO_VM_PAGE(addr);
5683 m = (vm_page_t)addr;
5685 "page %p obj %p pidx 0x%jx phys 0x%jx q %d ref 0x%x\n"
5686 " af 0x%x of 0x%x f 0x%x act %d busy %x valid 0x%x dirty 0x%x\n",
5687 m, m->object, (uintmax_t)m->pindex, (uintmax_t)m->phys_addr,
5688 m->a.queue, m->ref_count, m->a.flags, m->oflags,
5689 m->flags, m->a.act_count, m->busy_lock, m->valid, m->dirty);