FreeBSD virtual memory subsystem code
vm_page.c
Go to the documentation of this file.
1/*-
2 * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU)
3 *
4 * Copyright (c) 1991 Regents of the University of California.
5 * All rights reserved.
6 * Copyright (c) 1998 Matthew Dillon. All Rights Reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * The Mach Operating System project at Carnegie-Mellon University.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
22 *
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
33 * SUCH DAMAGE.
34 *
35 * from: @(#)vm_page.c 7.4 (Berkeley) 5/7/91
36 */
37
38/*-
39 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40 * All rights reserved.
41 *
42 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
43 *
44 * Permission to use, copy, modify and distribute this software and
45 * its documentation is hereby granted, provided that both the copyright
46 * notice and this permission notice appear in all copies of the
47 * software, derivative works or modified versions, and any portions
48 * thereof, and that both notices appear in supporting documentation.
49 *
50 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
53 *
54 * Carnegie Mellon requests users of this software to return to
55 *
56 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
57 * School of Computer Science
58 * Carnegie Mellon University
59 * Pittsburgh PA 15213-3890
60 *
61 * any improvements or extensions that they make and grant Carnegie the
62 * rights to redistribute these changes.
63 */
64
65/*
66 * Resident memory management module.
67 */
68
69#include <sys/cdefs.h>
70__FBSDID("$FreeBSD$");
71
72#include "opt_vm.h"
73
74#include <sys/param.h>
75#include <sys/systm.h>
76#include <sys/counter.h>
77#include <sys/domainset.h>
78#include <sys/kernel.h>
79#include <sys/limits.h>
80#include <sys/linker.h>
81#include <sys/lock.h>
82#include <sys/malloc.h>
83#include <sys/mman.h>
84#include <sys/msgbuf.h>
85#include <sys/mutex.h>
86#include <sys/proc.h>
87#include <sys/rwlock.h>
88#include <sys/sleepqueue.h>
89#include <sys/sbuf.h>
90#include <sys/sched.h>
91#include <sys/smp.h>
92#include <sys/sysctl.h>
93#include <sys/vmmeter.h>
94#include <sys/vnode.h>
95
96#include <vm/vm.h>
97#include <vm/pmap.h>
98#include <vm/vm_param.h>
99#include <vm/vm_domainset.h>
100#include <vm/vm_kern.h>
101#include <vm/vm_map.h>
102#include <vm/vm_object.h>
103#include <vm/vm_page.h>
104#include <vm/vm_pageout.h>
105#include <vm/vm_phys.h>
106#include <vm/vm_pagequeue.h>
107#include <vm/vm_pager.h>
108#include <vm/vm_radix.h>
109#include <vm/vm_reserv.h>
110#include <vm/vm_extern.h>
111#include <vm/vm_dumpset.h>
112#include <vm/uma.h>
113#include <vm/uma_int.h>
114
115#include <machine/md_var.h>
116
117struct vm_domain vm_dom[MAXMEMDOM];
118
119DPCPU_DEFINE_STATIC(struct vm_batchqueue, pqbatch[MAXMEMDOM][PQ_COUNT]);
120
121struct mtx_padalign __exclusive_cache_line pa_lock[PA_LOCK_COUNT];
122
123struct mtx_padalign __exclusive_cache_line vm_domainset_lock;
124/* The following fields are protected by the domainset lock. */
125domainset_t __exclusive_cache_line vm_min_domains;
126domainset_t __exclusive_cache_line vm_severe_domains;
127static int vm_min_waiters;
130
131static SYSCTL_NODE(_vm_stats, OID_AUTO, page, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
132 "VM page statistics");
133
134static COUNTER_U64_DEFINE_EARLY(pqstate_commit_retries);
135SYSCTL_COUNTER_U64(_vm_stats_page, OID_AUTO, pqstate_commit_retries,
136 CTLFLAG_RD, &pqstate_commit_retries,
137 "Number of failed per-page atomic queue state updates");
138
139static COUNTER_U64_DEFINE_EARLY(queue_ops);
140SYSCTL_COUNTER_U64(_vm_stats_page, OID_AUTO, queue_ops,
141 CTLFLAG_RD, &queue_ops,
142 "Number of batched queue operations");
143
144static COUNTER_U64_DEFINE_EARLY(queue_nops);
145SYSCTL_COUNTER_U64(_vm_stats_page, OID_AUTO, queue_nops,
146 CTLFLAG_RD, &queue_nops,
147 "Number of batched queue operations with no effects");
148
149/*
150 * bogus page -- for I/O to/from partially complete buffers,
151 * or for paging into sparsely invalid regions.
152 */
153vm_page_t bogus_page;
154
158
159struct bitset *vm_page_dump;
161
162static TAILQ_HEAD(, vm_page) blacklist_head;
163static int sysctl_vm_page_blacklist(SYSCTL_HANDLER_ARGS);
164SYSCTL_PROC(_vm, OID_AUTO, page_blacklist, CTLTYPE_STRING | CTLFLAG_RD |
165 CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_page_blacklist, "A", "Blacklist pages");
166
167static uma_zone_t fakepg_zone;
168
169static void vm_page_alloc_check(vm_page_t m);
170static bool _vm_page_busy_sleep(vm_object_t obj, vm_page_t m,
171 vm_pindex_t pindex, const char *wmesg, int allocflags, bool locked);
172static void vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits);
173static void vm_page_enqueue(vm_page_t m, uint8_t queue);
174static bool vm_page_free_prep(vm_page_t m);
175static void vm_page_free_toq(vm_page_t m);
176static void vm_page_init(void *dummy);
177static int vm_page_insert_after(vm_page_t m, vm_object_t object,
178 vm_pindex_t pindex, vm_page_t mpred);
179static void vm_page_insert_radixdone(vm_page_t m, vm_object_t object,
180 vm_page_t mpred);
181static void vm_page_mvqueue(vm_page_t m, const uint8_t queue,
182 const uint16_t nflag);
183static int vm_page_reclaim_run(int req_class, int domain, u_long npages,
184 vm_page_t m_run, vm_paddr_t high);
185static void vm_page_release_toq(vm_page_t m, uint8_t nqueue, bool noreuse);
186static int vm_domain_alloc_fail(struct vm_domain *vmd, vm_object_t object,
187 int req);
188static int vm_page_zone_import(void *arg, void **store, int cnt, int domain,
189 int flags);
190static void vm_page_zone_release(void *arg, void **store, int cnt);
191
192SYSINIT(vm_page, SI_SUB_VM, SI_ORDER_SECOND, vm_page_init, NULL);
193
194static void
195vm_page_init(void *dummy)
196{
197
198 fakepg_zone = uma_zcreate("fakepg", sizeof(struct vm_page), NULL, NULL,
199 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
201}
202
203/*
204 * The cache page zone is initialized later since we need to be able to allocate
205 * pages before UMA is fully initialized.
206 */
207static void
208vm_page_init_cache_zones(void *dummy __unused)
209{
210 struct vm_domain *vmd;
211 struct vm_pgcache *pgcache;
212 int cache, domain, maxcache, pool;
213
214 maxcache = 0;
215 TUNABLE_INT_FETCH("vm.pgcache_zone_max_pcpu", &maxcache);
216 maxcache *= 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,
227
228 /*
229 * Limit each pool's zone to 0.1% of the pages in the
230 * domain.
231 */
232 cache = maxcache != 0 ? maxcache :
233 vmd->vmd_page_count / 1000;
234 uma_zone_set_maxcache(pgcache->zone, cache);
235 }
236 }
237}
238SYSINIT(vm_page2, SI_SUB_VM_CONF, SI_ORDER_ANY, vm_page_init_cache_zones, NULL);
239
240/* Make sure that u_long is at least 64 bits when PAGE_SIZE is 32K. */
241#if PAGE_SIZE == 32768
242#ifdef CTASSERT
243CTASSERT(sizeof(u_long) >= 8);
244#endif
245#endif
246
247/*
248 * vm_set_page_size:
249 *
250 * Sets the page size, perhaps based upon the memory
251 * size. Must be called before any use of page-size
252 * dependent functions.
253 */
254void
256{
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");
261}
262
263/*
264 * vm_page_blacklist_next:
265 *
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.
271 */
272static vm_paddr_t
273vm_page_blacklist_next(char **list, char *end)
274{
275 vm_paddr_t bad;
276 char *cp, *pos;
277
278 if (list == NULL || *list == NULL)
279 return (0);
280 if (**list =='\0') {
281 *list = NULL;
282 return (0);
283 }
284
285 /*
286 * If there's no end pointer then the buffer is coming from
287 * the kenv and we know it's null-terminated.
288 */
289 if (end == NULL)
290 end = *list + strlen(*list);
291
292 /* Ensure that strtoq() won't walk off the end */
293 if (*end != '\0') {
294 if (*end == '\n' || *end == ' ' || *end == ',')
295 *end = '\0';
296 else {
297 printf("Blacklist not terminated, skipping\n");
298 *list = NULL;
299 return (0);
300 }
301 }
302
303 for (pos = *list; *pos != '\0'; pos = cp) {
304 bad = strtoq(pos, &cp, 0);
305 if (*cp == '\0' || *cp == ' ' || *cp == ',' || *cp == '\n') {
306 if (bad == 0) {
307 if (++cp < end)
308 continue;
309 else
310 break;
311 }
312 } else
313 break;
314 if (*cp == '\0' || ++cp >= end)
315 *list = NULL;
316 else
317 *list = cp;
318 return (trunc_page(bad));
319 }
320 printf("Garbage in RAM blacklist, skipping\n");
321 *list = NULL;
322 return (0);
323}
324
325bool
326vm_page_blacklist_add(vm_paddr_t pa, bool verbose)
327{
328 struct vm_domain *vmd;
329 vm_page_t m;
330 int ret;
331
333 if (m == NULL)
334 return (true); /* page does not exist, no failure */
335
336 vmd = vm_pagequeue_domain(m);
338 ret = vm_phys_unfree_page(m);
340 if (ret != 0) {
341 vm_domain_freecnt_inc(vmd, -1);
342 TAILQ_INSERT_TAIL(&blacklist_head, m, listq);
343 if (verbose)
344 printf("Skipping page with pa 0x%jx\n", (uintmax_t)pa);
345 }
346 return (ret);
347}
348
349/*
350 * vm_page_blacklist_check:
351 *
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.
355 */
356static void
357vm_page_blacklist_check(char *list, char *end)
358{
359 vm_paddr_t pa;
360 char *next;
361
362 next = list;
363 while (next != NULL) {
364 if ((pa = vm_page_blacklist_next(&next, end)) == 0)
365 continue;
366 vm_page_blacklist_add(pa, bootverbose);
367 }
368}
369
370/*
371 * vm_page_blacklist_load:
372 *
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
375 * of the same name.
376 */
377static void
378vm_page_blacklist_load(char **list, char **end)
379{
380 void *mod;
381 u_char *ptr;
382 u_int len;
383
384 mod = NULL;
385 ptr = NULL;
386
387 mod = preload_search_by_type("ram_blacklist");
388 if (mod != NULL) {
389 ptr = preload_fetch_addr(mod);
390 len = preload_fetch_size(mod);
391 }
392 *list = ptr;
393 if (ptr != NULL)
394 *end = ptr + len;
395 else
396 *end = NULL;
397 return;
398}
399
400static int
401sysctl_vm_page_blacklist(SYSCTL_HANDLER_ARGS)
402{
403 vm_page_t m;
404 struct sbuf sbuf;
405 int error, first;
406
407 first = 1;
408 error = sysctl_wire_old_buffer(req, 0);
409 if (error != 0)
410 return (error);
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);
415 first = 0;
416 }
417 error = sbuf_finish(&sbuf);
418 sbuf_delete(&sbuf);
419 return (error);
420}
421
422/*
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.
426 */
427void
428vm_page_init_marker(vm_page_t marker, int queue, uint16_t aflags)
429{
430
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;
436}
437
438static void
440{
441 struct vm_domain *vmd;
442 struct vm_pagequeue *pq;
443 int i;
444
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 **,
455 "vm unswappable pagequeue";
456 vmd->vmd_domain = domain;
457 vmd->vmd_page_count = 0;
458 vmd->vmd_free_count = 0;
459 vmd->vmd_segs = 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);
466 pq->pq_pdpages = 0;
467 vm_page_init_marker(&vmd->vmd_markers[i], i, 0);
468 }
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);
472
473 /*
474 * inacthead is used to provide FIFO ordering for LRU-bypassing
475 * insertions.
476 */
478 TAILQ_INSERT_HEAD(&vmd->vmd_pagequeues[PQ_INACTIVE].pq_pl,
479 &vmd->vmd_inacthead, plinks.q);
480
481 /*
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.
486 */
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);
493}
494
495/*
496 * Initialize a physical page in preparation for adding it to the free
497 * lists.
498 */
499void
500vm_page_init_page(vm_page_t m, vm_paddr_t pa, int segind)
501{
502
503 m->object = NULL;
504 m->ref_count = 0;
505 m->busy_lock = VPB_FREED;
506 m->flags = m->a.flags = 0;
507 m->phys_addr = pa;
508 m->a.queue = PQ_NONE;
509 m->psind = 0;
510 m->segind = segind;
511 m->order = VM_NFREEORDER;
512 m->pool = VM_FREEPOOL_DEFAULT;
513 m->valid = m->dirty = 0;
515}
516
517#ifndef PMAP_HAS_PAGE_ARRAY
518static vm_paddr_t
519vm_page_array_alloc(vm_offset_t *vaddr, vm_paddr_t end, vm_paddr_t page_range)
520{
521 vm_paddr_t new_end;
522
523 /*
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.
527 */
528 *vaddr += PAGE_SIZE;
529
530 /*
531 * Allocate physical memory for the page structures, and map it.
532 */
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,
536 vm_page_array_size = page_range;
537
538 return (new_end);
539}
540#endif
541
542/*
543 * vm_page_startup:
544 *
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
548 * page queues.
549 */
550vm_offset_t
551vm_page_startup(vm_offset_t vaddr)
552{
553 struct vm_phys_seg *seg;
554 struct vm_domain *vmd;
555 vm_page_t m;
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;
560 u_long pagecount;
561#if MINIDUMP_PAGE_TRACKING
562 u_long vm_page_dump_size;
563#endif
564 int biggestone, i, segind;
565#ifdef WITNESS
566 vm_offset_t mapped;
567 int witness_size;
568#endif
569#if defined(__i386__) && defined(VM_PHYSSEG_DENSE)
570 long ii;
571#endif
572
573 vaddr = round_page(vaddr);
574
576 biggestone = vm_phys_avail_largest();
577 end = phys_avail[biggestone+1];
578
579 /*
580 * Initialize the page and queue locks.
581 */
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++)
587
588 new_end = end;
589#ifdef WITNESS
590 witness_size = round_page(witness_startup_count());
591 new_end -= witness_size;
592 mapped = pmap_map(&vaddr, new_end, new_end + witness_size,
594 bzero((void *)mapped, witness_size);
595 witness_startup((void *)mapped);
596#endif
597
598#if MINIDUMP_PAGE_TRACKING
599 /*
600 * Allocate a bitmap to indicate that a random physical page
601 * needs to be included in a minidump.
602 *
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().
605 *
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.
609 */
610 last_pa = 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];
617 }
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#else
624 (void)last_pa;
625#endif
626#if defined(__aarch64__) || defined(__amd64__) || \
627 defined(__riscv) || defined(__powerpc64__)
628 /*
629 * Include the UMA bootstrap pages, witness pages and vm_page_dump
630 * in a crash dump. When pmap_map() uses the direct map, they are
631 * not automatically included.
632 */
633 for (pa = new_end; pa < end; pa += PAGE_SIZE)
634 dump_add_page(pa);
635#endif
636 phys_avail[biggestone + 1] = new_end;
637#ifdef __amd64__
638 /*
639 * Request that the physical pages underlying the message buffer be
640 * included in a crash dump. Since the message buffer is accessed
641 * through the direct map, they are not automatically included.
642 */
643 pa = DMAP_TO_PHYS((vm_offset_t)msgbufp->msg_ptr);
644 last_pa = pa + round_page(msgbufsize);
645 while (pa < last_pa) {
646 dump_add_page(pa);
647 pa += PAGE_SIZE;
648 }
649#endif
650 /*
651 * Compute the number of pages of memory that will be available for
652 * use, taking into account the overhead of a page structure per page.
653 * In other words, solve
654 * "available physical memory" - round_page(page_range *
655 * sizeof(struct vm_page)) = page_range * PAGE_SIZE
656 * for page_range.
657 */
658 low_avail = phys_avail[0];
659 high_avail = phys_avail[1];
660 for (i = 0; i < vm_phys_nsegs; i++) {
661 if (vm_phys_segs[i].start < low_avail)
662 low_avail = vm_phys_segs[i].start;
663 if (vm_phys_segs[i].end > high_avail)
664 high_avail = vm_phys_segs[i].end;
665 }
666 /* Skip the first chunk. It is already accounted for. */
667 for (i = 2; phys_avail[i + 1] != 0; i += 2) {
668 if (phys_avail[i] < low_avail)
669 low_avail = phys_avail[i];
670 if (phys_avail[i + 1] > high_avail)
671 high_avail = phys_avail[i + 1];
672 }
673 first_page = low_avail / PAGE_SIZE;
674#ifdef VM_PHYSSEG_SPARSE
675 size = 0;
676 for (i = 0; i < vm_phys_nsegs; i++)
677 size += vm_phys_segs[i].end - vm_phys_segs[i].start;
678 for (i = 0; phys_avail[i + 1] != 0; i += 2)
679 size += phys_avail[i + 1] - phys_avail[i];
680#elif defined(VM_PHYSSEG_DENSE)
681 size = high_avail - low_avail;
682#else
683#error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
684#endif
685
686#ifdef PMAP_HAS_PAGE_ARRAY
687 pmap_page_array_startup(size / PAGE_SIZE);
688 biggestone = vm_phys_avail_largest();
689 end = new_end = phys_avail[biggestone + 1];
690#else
691#ifdef VM_PHYSSEG_DENSE
692 /*
693 * In the VM_PHYSSEG_DENSE case, the number of pages can account for
694 * the overhead of a page structure per page only if vm_page_array is
695 * allocated from the last physical memory chunk. Otherwise, we must
696 * allocate page structures representing the physical memory
697 * underlying vm_page_array, even though they will not be used.
698 */
699 if (new_end != high_avail)
700 page_range = size / PAGE_SIZE;
701 else
702#endif
703 {
704 page_range = size / (PAGE_SIZE + sizeof(struct vm_page));
705
706 /*
707 * If the partial bytes remaining are large enough for
708 * a page (PAGE_SIZE) without a corresponding
709 * 'struct vm_page', then new_end will contain an
710 * extra page after subtracting the length of the VM
711 * page array. Compensate by subtracting an extra
712 * page from new_end.
713 */
714 if (size % (PAGE_SIZE + sizeof(struct vm_page)) >= PAGE_SIZE) {
715 if (new_end == high_avail)
716 high_avail -= PAGE_SIZE;
717 new_end -= PAGE_SIZE;
718 }
719 }
720 end = new_end;
721 new_end = vm_page_array_alloc(&vaddr, end, page_range);
722#endif
723
724#if VM_NRESERVLEVEL > 0
725 /*
726 * Allocate physical memory for the reservation management system's
727 * data structures, and map it.
728 */
729 new_end = vm_reserv_startup(&vaddr, new_end);
730#endif
731#if defined(__aarch64__) || defined(__amd64__) || \
732 defined(__riscv) || defined(__powerpc64__)
733 /*
734 * Include vm_page_array and vm_reserv_array in a crash dump.
735 */
736 for (pa = new_end; pa < end; pa += PAGE_SIZE)
737 dump_add_page(pa);
738#endif
739 phys_avail[biggestone + 1] = new_end;
740
741 /*
742 * Add physical memory segments corresponding to the available
743 * physical pages.
744 */
745 for (i = 0; phys_avail[i + 1] != 0; i += 2)
746 if (vm_phys_avail_size(i) != 0)
748
749 /*
750 * Initialize the physical memory allocator.
751 */
752 vm_phys_init();
753
754 /*
755 * Initialize the page structures and add every available page to the
756 * physical memory allocator's free lists.
757 */
758#if defined(__i386__) && defined(VM_PHYSSEG_DENSE)
759 for (ii = 0; ii < vm_page_array_size; ii++) {
760 m = &vm_page_array[ii];
761 vm_page_init_page(m, (first_page + ii) << PAGE_SHIFT, 0);
762 m->flags = PG_FICTITIOUS;
763 }
764#endif
765 vm_cnt.v_page_count = 0;
766 for (segind = 0; segind < vm_phys_nsegs; segind++) {
767 seg = &vm_phys_segs[segind];
768 for (m = seg->first_page, pa = seg->start; pa < seg->end;
769 m++, pa += PAGE_SIZE)
771
772 /*
773 * Add the segment's pages that are covered by one of
774 * phys_avail's ranges to the free lists.
775 */
776 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
777 if (seg->end <= phys_avail[i] ||
778 seg->start >= phys_avail[i + 1])
779 continue;
780
781 startp = MAX(seg->start, phys_avail[i]);
782 endp = MIN(seg->end, phys_avail[i + 1]);
783 pagecount = (u_long)atop(endp - startp);
784 if (pagecount == 0)
785 continue;
786
787 m = seg->first_page + atop(startp - seg->start);
788 vmd = VM_DOMAIN(seg->domain);
790 vm_phys_enqueue_contig(m, pagecount);
792 vm_domain_freecnt_inc(vmd, pagecount);
793 vm_cnt.v_page_count += (u_int)pagecount;
794 vmd->vmd_page_count += (u_int)pagecount;
795 vmd->vmd_segs |= 1UL << segind;
796 }
797 }
798
799 /*
800 * Remove blacklisted pages from the physical memory allocator.
801 */
802 TAILQ_INIT(&blacklist_head);
803 vm_page_blacklist_load(&list, &listend);
804 vm_page_blacklist_check(list, listend);
805
806 list = kern_getenv("vm.blacklist");
807 vm_page_blacklist_check(list, NULL);
808
809 freeenv(list);
810#if VM_NRESERVLEVEL > 0
811 /*
812 * Initialize the reservation management system.
813 */
814 vm_reserv_init();
815#endif
816
817 return (vaddr);
818}
819
820void
822{
823
825}
826
827/*
828 * vm_page_trybusy
829 *
830 * Helper routine for grab functions to trylock busy.
831 *
832 * Returns true on success and false on failure.
833 */
834static bool
835vm_page_trybusy(vm_page_t m, int allocflags)
836{
837
838 if ((allocflags & (VM_ALLOC_SBUSY | VM_ALLOC_IGN_SBUSY)) != 0)
839 return (vm_page_trysbusy(m));
840 else
841 return (vm_page_tryxbusy(m));
842}
843
844/*
845 * vm_page_tryacquire
846 *
847 * Helper routine for grab functions to trylock busy and wire.
848 *
849 * Returns true on success and false on failure.
850 */
851static inline bool
852vm_page_tryacquire(vm_page_t m, int allocflags)
853{
854 bool locked;
855
856 locked = vm_page_trybusy(m, allocflags);
857 if (locked && (allocflags & VM_ALLOC_WIRED) != 0)
858 vm_page_wire(m);
859 return (locked);
860}
861
862/*
863 * vm_page_busy_acquire:
864 *
865 * Acquire the busy lock as described by VM_ALLOC_* flags. Will loop
866 * and drop the object lock if necessary.
867 */
868bool
869vm_page_busy_acquire(vm_page_t m, int allocflags)
870{
871 vm_object_t obj;
872 bool locked;
873
874 /*
875 * The page-specific object must be cached because page
876 * identity can change during the sleep, causing the
877 * re-lock of a different object.
878 * It is assumed that a reference to the object is already
879 * held by the callers.
880 */
881 obj = atomic_load_ptr(&m->object);
882 for (;;) {
883 if (vm_page_tryacquire(m, allocflags))
884 return (true);
885 if ((allocflags & VM_ALLOC_NOWAIT) != 0)
886 return (false);
887 if (obj != NULL)
888 locked = VM_OBJECT_WOWNED(obj);
889 else
890 locked = false;
891 MPASS(locked || vm_page_wired(m));
892 if (_vm_page_busy_sleep(obj, m, m->pindex, "vmpba", allocflags,
893 locked) && locked)
894 VM_OBJECT_WLOCK(obj);
895 if ((allocflags & VM_ALLOC_WAITFAIL) != 0)
896 return (false);
897 KASSERT(m->object == obj || m->object == NULL,
898 ("vm_page_busy_acquire: page %p does not belong to %p",
899 m, obj));
900 }
901}
902
903/*
904 * vm_page_busy_downgrade:
905 *
906 * Downgrade an exclusive busy page into a single shared busy page.
907 */
908void
910{
911 u_int x;
912
914
915 x = vm_page_busy_fetch(m);
916 for (;;) {
917 if (atomic_fcmpset_rel_int(&m->busy_lock,
918 &x, VPB_SHARERS_WORD(1)))
919 break;
920 }
921 if ((x & VPB_BIT_WAITERS) != 0)
922 wakeup(m);
923}
924
925/*
926 *
927 * vm_page_busy_tryupgrade:
928 *
929 * Attempt to upgrade a single shared busy into an exclusive busy.
930 */
931int
933{
934 u_int ce, x;
935
937
938 x = vm_page_busy_fetch(m);
940 for (;;) {
941 if (VPB_SHARERS(x) > 1)
942 return (0);
943 KASSERT((x & ~VPB_BIT_WAITERS) == VPB_SHARERS_WORD(1),
944 ("vm_page_busy_tryupgrade: invalid lock state"));
945 if (!atomic_fcmpset_acq_int(&m->busy_lock, &x,
946 ce | (x & VPB_BIT_WAITERS)))
947 continue;
948 return (1);
949 }
950}
951
952/*
953 * vm_page_sbusied:
954 *
955 * Return a positive value if the page is shared busied, 0 otherwise.
956 */
957int
958vm_page_sbusied(vm_page_t m)
959{
960 u_int x;
961
962 x = vm_page_busy_fetch(m);
963 return ((x & VPB_BIT_SHARED) != 0 && x != VPB_UNBUSIED);
964}
965
966/*
967 * vm_page_sunbusy:
968 *
969 * Shared unbusy a page.
970 */
971void
972vm_page_sunbusy(vm_page_t m)
973{
974 u_int x;
975
977
978 x = vm_page_busy_fetch(m);
979 for (;;) {
980 KASSERT(x != VPB_FREED,
981 ("vm_page_sunbusy: Unlocking freed page."));
982 if (VPB_SHARERS(x) > 1) {
983 if (atomic_fcmpset_int(&m->busy_lock, &x,
984 x - VPB_ONE_SHARER))
985 break;
986 continue;
987 }
988 KASSERT((x & ~VPB_BIT_WAITERS) == VPB_SHARERS_WORD(1),
989 ("vm_page_sunbusy: invalid lock state"));
990 if (!atomic_fcmpset_rel_int(&m->busy_lock, &x, VPB_UNBUSIED))
991 continue;
992 if ((x & VPB_BIT_WAITERS) == 0)
993 break;
994 wakeup(m);
995 break;
996 }
997}
998
999/*
1000 * vm_page_busy_sleep:
1001 *
1002 * Sleep if the page is busy, using the page pointer as wchan.
1003 * This is used to implement the hard-path of the busying mechanism.
1004 *
1005 * If VM_ALLOC_IGN_SBUSY is specified in allocflags, the function
1006 * will not sleep if the page is shared-busy.
1007 *
1008 * The object lock must be held on entry.
1009 *
1010 * Returns true if it slept and dropped the object lock, or false
1011 * if there was no sleep and the lock is still held.
1012 */
1013bool
1014vm_page_busy_sleep(vm_page_t m, const char *wmesg, int allocflags)
1015{
1016 vm_object_t obj;
1017
1018 obj = m->object;
1020
1021 return (_vm_page_busy_sleep(obj, m, m->pindex, wmesg, allocflags,
1022 true));
1023}
1024
1025/*
1026 * vm_page_busy_sleep_unlocked:
1027 *
1028 * Sleep if the page is busy, using the page pointer as wchan.
1029 * This is used to implement the hard-path of busying mechanism.
1030 *
1031 * If VM_ALLOC_IGN_SBUSY is specified in allocflags, the function
1032 * will not sleep if the page is shared-busy.
1033 *
1034 * The object lock must not be held on entry. The operation will
1035 * return if the page changes identity.
1036 */
1037void
1038vm_page_busy_sleep_unlocked(vm_object_t obj, vm_page_t m, vm_pindex_t pindex,
1039 const char *wmesg, int allocflags)
1040{
1042
1043 (void)_vm_page_busy_sleep(obj, m, pindex, wmesg, allocflags, false);
1044}
1045
1046/*
1047 * _vm_page_busy_sleep:
1048 *
1049 * Internal busy sleep function. Verifies the page identity and
1050 * lockstate against parameters. Returns true if it sleeps and
1051 * false otherwise.
1052 *
1053 * allocflags uses VM_ALLOC_* flags to specify the lock required.
1054 *
1055 * If locked is true the lock will be dropped for any true returns
1056 * and held for any false returns.
1057 */
1058static bool
1059_vm_page_busy_sleep(vm_object_t obj, vm_page_t m, vm_pindex_t pindex,
1060 const char *wmesg, int allocflags, bool locked)
1061{
1062 bool xsleep;
1063 u_int x;
1064
1065 /*
1066 * If the object is busy we must wait for that to drain to zero
1067 * before trying the page again.
1068 */
1069 if (obj != NULL && vm_object_busied(obj)) {
1070 if (locked)
1071 VM_OBJECT_DROP(obj);
1072 vm_object_busy_wait(obj, wmesg);
1073 return (true);
1074 }
1075
1076 if (!vm_page_busied(m))
1077 return (false);
1078
1079 xsleep = (allocflags & (VM_ALLOC_SBUSY | VM_ALLOC_IGN_SBUSY)) != 0;
1080 sleepq_lock(m);
1081 x = vm_page_busy_fetch(m);
1082 do {
1083 /*
1084 * If the page changes objects or becomes unlocked we can
1085 * simply return.
1086 */
1087 if (x == VPB_UNBUSIED ||
1088 (xsleep && (x & VPB_BIT_SHARED) != 0) ||
1089 m->object != obj || m->pindex != pindex) {
1090 sleepq_release(m);
1091 return (false);
1092 }
1093 if ((x & VPB_BIT_WAITERS) != 0)
1094 break;
1095 } while (!atomic_fcmpset_int(&m->busy_lock, &x, x | VPB_BIT_WAITERS));
1096 if (locked)
1097 VM_OBJECT_DROP(obj);
1098 DROP_GIANT();
1099 sleepq_add(m, NULL, wmesg, 0, 0);
1100 sleepq_wait(m, PVM);
1101 PICKUP_GIANT();
1102 return (true);
1103}
1104
1105/*
1106 * vm_page_trysbusy:
1107 *
1108 * Try to shared busy a page.
1109 * If the operation succeeds 1 is returned otherwise 0.
1110 * The operation never sleeps.
1111 */
1112int
1114{
1115 vm_object_t obj;
1116 u_int x;
1117
1118 obj = m->object;
1119 x = vm_page_busy_fetch(m);
1120 for (;;) {
1121 if ((x & VPB_BIT_SHARED) == 0)
1122 return (0);
1123 /*
1124 * Reduce the window for transient busies that will trigger
1125 * false negatives in vm_page_ps_test().
1126 */
1127 if (obj != NULL && vm_object_busied(obj))
1128 return (0);
1129 if (atomic_fcmpset_acq_int(&m->busy_lock, &x,
1130 x + VPB_ONE_SHARER))
1131 break;
1132 }
1133
1134 /* Refetch the object now that we're guaranteed that it is stable. */
1135 obj = m->object;
1136 if (obj != NULL && vm_object_busied(obj)) {
1137 vm_page_sunbusy(m);
1138 return (0);
1139 }
1140 return (1);
1141}
1142
1143/*
1144 * vm_page_tryxbusy:
1145 *
1146 * Try to exclusive busy a page.
1147 * If the operation succeeds 1 is returned otherwise 0.
1148 * The operation never sleeps.
1149 */
1150int
1152{
1153 vm_object_t obj;
1154
1155 if (atomic_cmpset_acq_int(&m->busy_lock, VPB_UNBUSIED,
1157 return (0);
1158
1159 obj = m->object;
1160 if (obj != NULL && vm_object_busied(obj)) {
1161 vm_page_xunbusy(m);
1162 return (0);
1163 }
1164 return (1);
1165}
1166
1167static void
1169{
1170 atomic_store_rel_int(&m->busy_lock, VPB_UNBUSIED);
1171 /* Wake the waiter. */
1172 wakeup(m);
1173}
1174
1175/*
1176 * vm_page_xunbusy_hard:
1177 *
1178 * Called when unbusy has failed because there is a waiter.
1179 */
1180void
1182{
1185}
1186
1187void
1189{
1192}
1193
1194static void
1196{
1197 u_int x;
1198
1199 atomic_thread_fence_rel();
1200 x = atomic_swap_int(&m->busy_lock, VPB_FREED);
1201 if ((x & VPB_BIT_WAITERS) != 0)
1202 wakeup(m);
1203}
1204
1205/*
1206 * vm_page_unhold_pages:
1207 *
1208 * Unhold each of the pages that is referenced by the given array.
1209 */
1210void
1211vm_page_unhold_pages(vm_page_t *ma, int count)
1212{
1213
1214 for (; count != 0; count--) {
1216 ma++;
1217 }
1218}
1219
1220vm_page_t
1221PHYS_TO_VM_PAGE(vm_paddr_t pa)
1222{
1223 vm_page_t m;
1224
1225#ifdef VM_PHYSSEG_SPARSE
1227 if (m == NULL)
1229 return (m);
1230#elif defined(VM_PHYSSEG_DENSE)
1231 long pi;
1232
1233 pi = atop(pa);
1234 if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
1235 m = &vm_page_array[pi - first_page];
1236 return (m);
1237 }
1238 return (vm_phys_fictitious_to_vm_page(pa));
1239#else
1240#error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
1241#endif
1242}
1243
1244/*
1245 * vm_page_getfake:
1246 *
1247 * Create a fictitious page with the specified physical address and
1248 * memory attribute. The memory attribute is the only the machine-
1249 * dependent aspect of a fictitious page that must be initialized.
1250 */
1251vm_page_t
1252vm_page_getfake(vm_paddr_t paddr, vm_memattr_t memattr)
1253{
1254 vm_page_t m;
1255
1256 m = uma_zalloc(fakepg_zone, M_WAITOK | M_ZERO);
1257 vm_page_initfake(m, paddr, memattr);
1258 return (m);
1259}
1260
1261void
1262vm_page_initfake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
1263{
1264
1265 if ((m->flags & PG_FICTITIOUS) != 0) {
1266 /*
1267 * The page's memattr might have changed since the
1268 * previous initialization. Update the pmap to the
1269 * new memattr.
1270 */
1271 goto memattr;
1272 }
1273 m->phys_addr = paddr;
1274 m->a.queue = PQ_NONE;
1275 /* Fictitious pages don't use "segind". */
1276 m->flags = PG_FICTITIOUS;
1277 /* Fictitious pages don't use "order" or "pool". */
1278 m->oflags = VPO_UNMANAGED;
1279 m->busy_lock = VPB_CURTHREAD_EXCLUSIVE;
1280 /* Fictitious pages are unevictable. */
1281 m->ref_count = 1;
1282 pmap_page_init(m);
1283memattr:
1284 pmap_page_set_memattr(m, memattr);
1285}
1286
1287/*
1288 * vm_page_putfake:
1289 *
1290 * Release a fictitious page.
1291 */
1292void
1294{
1295
1296 KASSERT((m->oflags & VPO_UNMANAGED) != 0, ("managed %p", m));
1297 KASSERT((m->flags & PG_FICTITIOUS) != 0,
1298 ("vm_page_putfake: bad page %p", m));
1301 uma_zfree(fakepg_zone, m);
1302}
1303
1304/*
1305 * vm_page_updatefake:
1306 *
1307 * Update the given fictitious page to the specified physical address and
1308 * memory attribute.
1309 */
1310void
1311vm_page_updatefake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
1312{
1313
1314 KASSERT((m->flags & PG_FICTITIOUS) != 0,
1315 ("vm_page_updatefake: bad page %p", m));
1316 m->phys_addr = paddr;
1317 pmap_page_set_memattr(m, memattr);
1318}
1319
1320/*
1321 * vm_page_free:
1322 *
1323 * Free a page.
1324 */
1325void
1326vm_page_free(vm_page_t m)
1327{
1328
1329 m->flags &= ~PG_ZERO;
1331}
1332
1333/*
1334 * vm_page_free_zero:
1335 *
1336 * Free a page to the zerod-pages queue
1337 */
1338void
1340{
1341
1342 m->flags |= PG_ZERO;
1344}
1345
1346/*
1347 * Unbusy and handle the page queueing for a page from a getpages request that
1348 * was optionally read ahead or behind.
1349 */
1350void
1352{
1353
1354 /* We shouldn't put invalid pages on queues. */
1355 KASSERT(!vm_page_none_valid(m), ("%s: %p is invalid", __func__, m));
1356
1357 /*
1358 * Since the page is not the actually needed one, whether it should
1359 * be activated or deactivated is not obvious. Empirical results
1360 * have shown that deactivating the page is usually the best choice,
1361 * unless the page is wanted by another thread.
1362 */
1363 if ((vm_page_busy_fetch(m) & VPB_BIT_WAITERS) != 0)
1365 else
1368}
1369
1370/*
1371 * Destroy the identity of an invalid page and free it if possible.
1372 * This is intended to be used when reading a page from backing store fails.
1373 */
1374void
1376{
1377
1378 KASSERT(vm_page_none_valid(m), ("page %p is valid", m));
1379 KASSERT(!pmap_page_is_mapped(m), ("page %p is mapped", m));
1380 KASSERT(m->object != NULL, ("page %p has no object", m));
1381 VM_OBJECT_ASSERT_WLOCKED(m->object);
1382
1383 /*
1384 * We may be attempting to free the page as part of the handling for an
1385 * I/O error, in which case the page was xbusied by a different thread.
1386 */
1388
1389 /*
1390 * If someone has wired this page while the object lock
1391 * was not held, then the thread that unwires is responsible
1392 * for freeing the page. Otherwise just free the page now.
1393 * The wire count of this unmapped page cannot change while
1394 * we have the page xbusy and the page's object wlocked.
1395 */
1396 if (vm_page_remove(m))
1397 vm_page_free(m);
1398}
1399
1400/*
1401 * vm_page_dirty_KBI: [ internal use only ]
1402 *
1403 * Set all bits in the page's dirty field.
1404 *
1405 * The object containing the specified page must be locked if the
1406 * call is made from the machine-independent layer.
1407 *
1408 * See vm_page_clear_dirty_mask().
1409 *
1410 * This function should only be called by vm_page_dirty().
1411 */
1412void
1414{
1415
1416 /* Refer to this operation by its public name. */
1417 KASSERT(vm_page_all_valid(m), ("vm_page_dirty: page is invalid!"));
1418 m->dirty = VM_PAGE_BITS_ALL;
1419}
1420
1421/*
1422 * vm_page_insert: [ internal use only ]
1423 *
1424 * Inserts the given mem entry into the object and object list.
1425 *
1426 * The object must be locked.
1427 */
1428int
1429vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex)
1430{
1431 vm_page_t mpred;
1432
1435 return (vm_page_insert_after(m, object, pindex, mpred));
1436}
1437
1438/*
1439 * vm_page_insert_after:
1440 *
1441 * Inserts the page "m" into the specified object at offset "pindex".
1442 *
1443 * The page "mpred" must immediately precede the offset "pindex" within
1444 * the specified object.
1445 *
1446 * The object must be locked.
1447 */
1448static int
1449vm_page_insert_after(vm_page_t m, vm_object_t object, vm_pindex_t pindex,
1450 vm_page_t mpred)
1451{
1452 vm_page_t msucc;
1453
1455 KASSERT(m->object == NULL,
1456 ("vm_page_insert_after: page already inserted"));
1457 if (mpred != NULL) {
1458 KASSERT(mpred->object == object,
1459 ("vm_page_insert_after: object doesn't contain mpred"));
1460 KASSERT(mpred->pindex < pindex,
1461 ("vm_page_insert_after: mpred doesn't precede pindex"));
1462 msucc = TAILQ_NEXT(mpred, listq);
1463 } else
1464 msucc = TAILQ_FIRST(&object->memq);
1465 if (msucc != NULL)
1466 KASSERT(msucc->pindex > pindex,
1467 ("vm_page_insert_after: msucc doesn't succeed pindex"));
1468
1469 /*
1470 * Record the object/offset pair in this page.
1471 */
1472 m->object = object;
1473 m->pindex = pindex;
1474 m->ref_count |= VPRC_OBJREF;
1475
1476 /*
1477 * Now link into the object's ordered list of backed pages.
1478 */
1479 if (vm_radix_insert(&object->rtree, m)) {
1480 m->object = NULL;
1481 m->pindex = 0;
1482 m->ref_count &= ~VPRC_OBJREF;
1483 return (1);
1484 }
1485 vm_page_insert_radixdone(m, object, mpred);
1486 return (0);
1487}
1488
1489/*
1490 * vm_page_insert_radixdone:
1491 *
1492 * Complete page "m" insertion into the specified object after the
1493 * radix trie hooking.
1494 *
1495 * The page "mpred" must precede the offset "m->pindex" within the
1496 * specified object.
1497 *
1498 * The object must be locked.
1499 */
1500static void
1501vm_page_insert_radixdone(vm_page_t m, vm_object_t object, vm_page_t mpred)
1502{
1503
1505 KASSERT(object != NULL && m->object == object,
1506 ("vm_page_insert_radixdone: page %p has inconsistent object", m));
1507 KASSERT((m->ref_count & VPRC_OBJREF) != 0,
1508 ("vm_page_insert_radixdone: page %p is missing object ref", m));
1509 if (mpred != NULL) {
1510 KASSERT(mpred->object == object,
1511 ("vm_page_insert_radixdone: object doesn't contain mpred"));
1512 KASSERT(mpred->pindex < m->pindex,
1513 ("vm_page_insert_radixdone: mpred doesn't precede pindex"));
1514 }
1515
1516 if (mpred != NULL)
1517 TAILQ_INSERT_AFTER(&object->memq, mpred, m, listq);
1518 else
1519 TAILQ_INSERT_HEAD(&object->memq, m, listq);
1520
1521 /*
1522 * Show that the object has one more resident page.
1523 */
1524 object->resident_page_count++;
1525
1526 /*
1527 * Hold the vnode until the last page is released.
1528 */
1530 vhold(object->handle);
1531
1532 /*
1533 * Since we are inserting a new and possibly dirty page,
1534 * update the object's generation count.
1535 */
1536 if (pmap_page_is_write_mapped(m))
1538}
1539
1540/*
1541 * Do the work to remove a page from its object. The caller is responsible for
1542 * updating the page's fields to reflect this removal.
1543 */
1544static void
1546{
1548 vm_page_t mrem;
1549
1551 object = m->object;
1553 KASSERT((m->ref_count & VPRC_OBJREF) != 0,
1554 ("page %p is missing its object ref", m));
1555
1556 /* Deferred free of swap space. */
1557 if ((m->a.flags & PGA_SWAP_FREE) != 0)
1559
1560 m->object = NULL;
1561 mrem = vm_radix_remove(&object->rtree, m->pindex);
1562 KASSERT(mrem == m, ("removed page %p, expected page %p", mrem, m));
1563
1564 /*
1565 * Now remove from the object's list of backed pages.
1566 */
1567 TAILQ_REMOVE(&object->memq, m, listq);
1568
1569 /*
1570 * And show that the object has one fewer resident page.
1571 */
1572 object->resident_page_count--;
1573
1574 /*
1575 * The vnode may now be recycled.
1576 */
1578 vdrop(object->handle);
1579}
1580
1581/*
1582 * vm_page_remove:
1583 *
1584 * Removes the specified page from its containing object, but does not
1585 * invalidate any backing storage. Returns true if the object's reference
1586 * was the last reference to the page, and false otherwise.
1587 *
1588 * The object must be locked and the page must be exclusively busied.
1589 * The exclusive busy will be released on return. If this is not the
1590 * final ref and the caller does not hold a wire reference it may not
1591 * continue to access the page.
1592 */
1593bool
1594vm_page_remove(vm_page_t m)
1595{
1596 bool dropped;
1597
1598 dropped = vm_page_remove_xbusy(m);
1599 vm_page_xunbusy(m);
1600
1601 return (dropped);
1602}
1603
1604/*
1605 * vm_page_remove_xbusy
1606 *
1607 * Removes the page but leaves the xbusy held. Returns true if this
1608 * removed the final ref and false otherwise.
1609 */
1610bool
1612{
1613
1615 return (vm_page_drop(m, VPRC_OBJREF) == VPRC_OBJREF);
1616}
1617
1618/*
1619 * vm_page_lookup:
1620 *
1621 * Returns the page associated with the object/offset
1622 * pair specified; if none is found, NULL is returned.
1623 *
1624 * The object must be locked.
1625 */
1626vm_page_t
1628{
1629
1631 return (vm_radix_lookup(&object->rtree, pindex));
1632}
1633
1634/*
1635 * vm_page_lookup_unlocked:
1636 *
1637 * Returns the page associated with the object/offset pair specified;
1638 * if none is found, NULL is returned. The page may be no longer be
1639 * present in the object at the time that this function returns. Only
1640 * useful for opportunistic checks such as inmem().
1641 */
1642vm_page_t
1644{
1645
1647}
1648
1649/*
1650 * vm_page_relookup:
1651 *
1652 * Returns a page that must already have been busied by
1653 * the caller. Used for bogus page replacement.
1654 */
1655vm_page_t
1657{
1658 vm_page_t m;
1659
1661 KASSERT(m != NULL && (vm_page_busied(m) || vm_page_wired(m)) &&
1662 m->object == object && m->pindex == pindex,
1663 ("vm_page_relookup: Invalid page %p", m));
1664 return (m);
1665}
1666
1667/*
1668 * This should only be used by lockless functions for releasing transient
1669 * incorrect acquires. The page may have been freed after we acquired a
1670 * busy lock. In this case busy_lock == VPB_FREED and we have nothing
1671 * further to do.
1672 */
1673static void
1675{
1676 u_int x;
1677
1678 x = vm_page_busy_fetch(m);
1679 for (;;) {
1680 if (x == VPB_FREED)
1681 break;
1682 if ((x & VPB_BIT_SHARED) != 0 && VPB_SHARERS(x) > 1) {
1683 if (atomic_fcmpset_int(&m->busy_lock, &x,
1684 x - VPB_ONE_SHARER))
1685 break;
1686 continue;
1687 }
1688 KASSERT((x & VPB_BIT_SHARED) != 0 ||
1690 ("vm_page_busy_release: %p xbusy not owned.", m));
1691 if (!atomic_fcmpset_rel_int(&m->busy_lock, &x, VPB_UNBUSIED))
1692 continue;
1693 if ((x & VPB_BIT_WAITERS) != 0)
1694 wakeup(m);
1695 break;
1696 }
1697}
1698
1699/*
1700 * vm_page_find_least:
1701 *
1702 * Returns the page associated with the object with least pindex
1703 * greater than or equal to the parameter pindex, or NULL.
1704 *
1705 * The object must be locked.
1706 */
1707vm_page_t
1709{
1710 vm_page_t m;
1711
1713 if ((m = TAILQ_FIRST(&object->memq)) != NULL && m->pindex < pindex)
1715 return (m);
1716}
1717
1718/*
1719 * Returns the given page's successor (by pindex) within the object if it is
1720 * resident; if none is found, NULL is returned.
1721 *
1722 * The object must be locked.
1723 */
1724vm_page_t
1725vm_page_next(vm_page_t m)
1726{
1727 vm_page_t next;
1728
1729 VM_OBJECT_ASSERT_LOCKED(m->object);
1730 if ((next = TAILQ_NEXT(m, listq)) != NULL) {
1731 MPASS(next->object == m->object);
1732 if (next->pindex != m->pindex + 1)
1733 next = NULL;
1734 }
1735 return (next);
1736}
1737
1738/*
1739 * Returns the given page's predecessor (by pindex) within the object if it is
1740 * resident; if none is found, NULL is returned.
1741 *
1742 * The object must be locked.
1743 */
1744vm_page_t
1745vm_page_prev(vm_page_t m)
1746{
1747 vm_page_t prev;
1748
1749 VM_OBJECT_ASSERT_LOCKED(m->object);
1750 if ((prev = TAILQ_PREV(m, pglist, listq)) != NULL) {
1751 MPASS(prev->object == m->object);
1752 if (prev->pindex != m->pindex - 1)
1753 prev = NULL;
1754 }
1755 return (prev);
1756}
1757
1758/*
1759 * Uses the page mnew as a replacement for an existing page at index
1760 * pindex which must be already present in the object.
1761 *
1762 * Both pages must be exclusively busied on enter. The old page is
1763 * unbusied on exit.
1764 *
1765 * A return value of true means mold is now free. If this is not the
1766 * final ref and the caller does not hold a wire reference it may not
1767 * continue to access the page.
1768 */
1769static bool
1770vm_page_replace_hold(vm_page_t mnew, vm_object_t object, vm_pindex_t pindex,
1771 vm_page_t mold)
1772{
1773 vm_page_t mret;
1774 bool dropped;
1775
1778 KASSERT(mnew->object == NULL && (mnew->ref_count & VPRC_OBJREF) == 0,
1779 ("vm_page_replace: page %p already in object", mnew));
1780
1781 /*
1782 * This function mostly follows vm_page_insert() and
1783 * vm_page_remove() without the radix, object count and vnode
1784 * dance. Double check such functions for more comments.
1785 */
1786
1787 mnew->object = object;
1788 mnew->pindex = pindex;
1789 atomic_set_int(&mnew->ref_count, VPRC_OBJREF);
1790 mret = vm_radix_replace(&object->rtree, mnew);
1791 KASSERT(mret == mold,
1792 ("invalid page replacement, mold=%p, mret=%p", mold, mret));
1793 KASSERT((mold->oflags & VPO_UNMANAGED) ==
1794 (mnew->oflags & VPO_UNMANAGED),
1795 ("vm_page_replace: mismatched VPO_UNMANAGED"));
1796
1797 /* Keep the resident page list in sorted order. */
1798 TAILQ_INSERT_AFTER(&object->memq, mold, mnew, listq);
1799 TAILQ_REMOVE(&object->memq, mold, listq);
1800 mold->object = NULL;
1801
1802 /*
1803 * The object's resident_page_count does not change because we have
1804 * swapped one page for another, but the generation count should
1805 * change if the page is dirty.
1806 */
1807 if (pmap_page_is_write_mapped(mnew))
1809 dropped = vm_page_drop(mold, VPRC_OBJREF) == VPRC_OBJREF;
1810 vm_page_xunbusy(mold);
1811
1812 return (dropped);
1813}
1814
1815void
1816vm_page_replace(vm_page_t mnew, vm_object_t object, vm_pindex_t pindex,
1817 vm_page_t mold)
1818{
1819
1821
1822 if (vm_page_replace_hold(mnew, object, pindex, mold))
1823 vm_page_free(mold);
1824}
1825
1826/*
1827 * vm_page_rename:
1828 *
1829 * Move the given memory entry from its
1830 * current object to the specified target object/offset.
1831 *
1832 * Note: swap associated with the page must be invalidated by the move. We
1833 * have to do this for several reasons: (1) we aren't freeing the
1834 * page, (2) we are dirtying the page, (3) the VM system is probably
1835 * moving the page from object A to B, and will then later move
1836 * the backing store from A to B and we can't have a conflict.
1837 *
1838 * Note: we *always* dirty the page. It is necessary both for the
1839 * fact that we moved it, and because we may be invalidating
1840 * swap.
1841 *
1842 * The objects must be locked.
1843 */
1844int
1845vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex)
1846{
1847 vm_page_t mpred;
1848 vm_pindex_t opidx;
1849
1850 VM_OBJECT_ASSERT_WLOCKED(new_object);
1851
1852 KASSERT(m->ref_count != 0, ("vm_page_rename: page %p has no refs", m));
1853 mpred = vm_radix_lookup_le(&new_object->rtree, new_pindex);
1854 KASSERT(mpred == NULL || mpred->pindex != new_pindex,
1855 ("vm_page_rename: pindex already renamed"));
1856
1857 /*
1858 * Create a custom version of vm_page_insert() which does not depend
1859 * by m_prev and can cheat on the implementation aspects of the
1860 * function.
1861 */
1862 opidx = m->pindex;
1863 m->pindex = new_pindex;
1864 if (vm_radix_insert(&new_object->rtree, m)) {
1865 m->pindex = opidx;
1866 return (1);
1867 }
1868
1869 /*
1870 * The operation cannot fail anymore. The removal must happen before
1871 * the listq iterator is tainted.
1872 */
1873 m->pindex = opidx;
1875
1876 /* Return back to the new pindex to complete vm_page_insert(). */
1877 m->pindex = new_pindex;
1878 m->object = new_object;
1879
1880 vm_page_insert_radixdone(m, new_object, mpred);
1881 vm_page_dirty(m);
1882 return (0);
1883}
1884
1885/*
1886 * vm_page_alloc:
1887 *
1888 * Allocate and return a page that is associated with the specified
1889 * object and offset pair. By default, this page is exclusive busied.
1890 *
1891 * The caller must always specify an allocation class.
1892 *
1893 * allocation classes:
1894 * VM_ALLOC_NORMAL normal process request
1895 * VM_ALLOC_SYSTEM system *really* needs a page
1896 * VM_ALLOC_INTERRUPT interrupt time request
1897 *
1898 * optional allocation flags:
1899 * VM_ALLOC_COUNT(number) the number of additional pages that the caller
1900 * intends to allocate
1901 * VM_ALLOC_NOBUSY do not exclusive busy the page
1902 * VM_ALLOC_NODUMP do not include the page in a kernel core dump
1903 * VM_ALLOC_SBUSY shared busy the allocated page
1904 * VM_ALLOC_WIRED wire the allocated page
1905 * VM_ALLOC_ZERO prefer a zeroed page
1906 */
1907vm_page_t
1908vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req)
1909{
1910
1911 return (vm_page_alloc_after(object, pindex, req,
1913}
1914
1915vm_page_t
1916vm_page_alloc_domain(vm_object_t object, vm_pindex_t pindex, int domain,
1917 int req)
1918{
1919
1920 return (vm_page_alloc_domain_after(object, pindex, domain, req,
1922}
1923
1924/*
1925 * Allocate a page in the specified object with the given page index. To
1926 * optimize insertion of the page into the object, the caller must also specifiy
1927 * the resident page in the object with largest index smaller than the given
1928 * page index, or NULL if no such page exists.
1929 */
1930vm_page_t
1932 int req, vm_page_t mpred)
1933{
1934 struct vm_domainset_iter di;
1935 vm_page_t m;
1936 int domain;
1937
1938 vm_domainset_iter_page_init(&di, object, pindex, &domain, &req);
1939 do {
1940 m = vm_page_alloc_domain_after(object, pindex, domain, req,
1941 mpred);
1942 if (m != NULL)
1943 break;
1944 } while (vm_domainset_iter_page(&di, object, &domain) == 0);
1945
1946 return (m);
1947}
1948
1949/*
1950 * Returns true if the number of free pages exceeds the minimum
1951 * for the request class and false otherwise.
1952 */
1953static int
1954_vm_domain_allocate(struct vm_domain *vmd, int req_class, int npages)
1955{
1956 u_int limit, old, new;
1957
1958 if (req_class == VM_ALLOC_INTERRUPT)
1959 limit = 0;
1960 else if (req_class == VM_ALLOC_SYSTEM)
1961 limit = vmd->vmd_interrupt_free_min;
1962 else
1963 limit = vmd->vmd_free_reserved;
1964
1965 /*
1966 * Attempt to reserve the pages. Fail if we're below the limit.
1967 */
1968 limit += npages;
1969 old = vmd->vmd_free_count;
1970 do {
1971 if (old < limit)
1972 return (0);
1973 new = old - npages;
1974 } while (atomic_fcmpset_int(&vmd->vmd_free_count, &old, new) == 0);
1975
1976 /* Wake the page daemon if we've crossed the threshold. */
1977 if (vm_paging_needed(vmd, new) && !vm_paging_needed(vmd, old))
1979
1980 /* Only update bitsets on transitions. */
1981 if ((old >= vmd->vmd_free_min && new < vmd->vmd_free_min) ||
1982 (old >= vmd->vmd_free_severe && new < vmd->vmd_free_severe))
1983 vm_domain_set(vmd);
1984
1985 return (1);
1986}
1987
1988int
1989vm_domain_allocate(struct vm_domain *vmd, int req, int npages)
1990{
1991 int req_class;
1992
1993 /*
1994 * The page daemon is allowed to dig deeper into the free page list.
1995 */
1996 req_class = req & VM_ALLOC_CLASS_MASK;
1997 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
1998 req_class = VM_ALLOC_SYSTEM;
1999 return (_vm_domain_allocate(vmd, req_class, npages));
2000}
2001
2002vm_page_t
2003vm_page_alloc_domain_after(vm_object_t object, vm_pindex_t pindex, int domain,
2004 int req, vm_page_t mpred)
2005{
2006 struct vm_domain *vmd;
2007 vm_page_t m;
2008 int flags;
2009
2010#define VPA_FLAGS (VM_ALLOC_CLASS_MASK | VM_ALLOC_WAITFAIL | \
2011 VM_ALLOC_NOWAIT | VM_ALLOC_NOBUSY | \
2012 VM_ALLOC_SBUSY | VM_ALLOC_WIRED | \
2013 VM_ALLOC_NODUMP | VM_ALLOC_ZERO | VM_ALLOC_COUNT_MASK)
2014 KASSERT((req & ~VPA_FLAGS) == 0,
2015 ("invalid request %#x", req));
2016 KASSERT(((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) !=
2018 ("invalid request %#x", req));
2019 KASSERT(mpred == NULL || mpred->pindex < pindex,
2020 ("mpred %p doesn't precede pindex 0x%jx", mpred,
2021 (uintmax_t)pindex));
2023
2024 flags = 0;
2025 m = NULL;
2026again:
2027#if VM_NRESERVLEVEL > 0
2028 /*
2029 * Can we allocate the page from a reservation?
2030 */
2031 if (vm_object_reserv(object) &&
2032 (m = vm_reserv_alloc_page(object, pindex, domain, req, mpred)) !=
2033 NULL) {
2034 goto found;
2035 }
2036#endif
2037 vmd = VM_DOMAIN(domain);
2038 if (vmd->vmd_pgcache[VM_FREEPOOL_DEFAULT].zone != NULL) {
2039 m = uma_zalloc(vmd->vmd_pgcache[VM_FREEPOOL_DEFAULT].zone,
2040 M_NOWAIT | M_NOVM);
2041 if (m != NULL) {
2042 flags |= PG_PCPU_CACHE;
2043 goto found;
2044 }
2045 }
2046 if (vm_domain_allocate(vmd, req, 1)) {
2047 /*
2048 * If not, allocate it from the free page queues.
2049 */
2051 m = vm_phys_alloc_pages(domain, VM_FREEPOOL_DEFAULT, 0);
2053 if (m == NULL) {
2054 vm_domain_freecnt_inc(vmd, 1);
2055#if VM_NRESERVLEVEL > 0
2056 if (vm_reserv_reclaim_inactive(domain))
2057 goto again;
2058#endif
2059 }
2060 }
2061 if (m == NULL) {
2062 /*
2063 * Not allocatable, give up.
2064 */
2065 if (vm_domain_alloc_fail(vmd, object, req))
2066 goto again;
2067 return (NULL);
2068 }
2069
2070 /*
2071 * At this point we had better have found a good page.
2072 */
2073found:
2074 vm_page_dequeue(m);
2076
2077 /*
2078 * Initialize the page. Only the PG_ZERO flag is inherited.
2079 */
2080 flags |= m->flags & PG_ZERO;
2081 if ((req & VM_ALLOC_NODUMP) != 0)
2082 flags |= PG_NODUMP;
2083 m->flags = flags;
2084 m->a.flags = 0;
2085 m->oflags = (object->flags & OBJ_UNMANAGED) != 0 ? VPO_UNMANAGED : 0;
2086 if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) == 0)
2087 m->busy_lock = VPB_CURTHREAD_EXCLUSIVE;
2088 else if ((req & VM_ALLOC_SBUSY) != 0)
2089 m->busy_lock = VPB_SHARERS_WORD(1);
2090 else
2091 m->busy_lock = VPB_UNBUSIED;
2092 if (req & VM_ALLOC_WIRED) {
2093 vm_wire_add(1);
2094 m->ref_count = 1;
2095 }
2096 m->a.act_count = 0;
2097
2098 if (vm_page_insert_after(m, object, pindex, mpred)) {
2099 if (req & VM_ALLOC_WIRED) {
2100 vm_wire_sub(1);
2101 m->ref_count = 0;
2102 }
2103 KASSERT(m->object == NULL, ("page %p has object", m));
2104 m->oflags = VPO_UNMANAGED;
2105 m->busy_lock = VPB_UNBUSIED;
2106 /* Don't change PG_ZERO. */
2108 if (req & VM_ALLOC_WAITFAIL) {
2109 VM_OBJECT_WUNLOCK(object);
2110 vm_radix_wait();
2111 VM_OBJECT_WLOCK(object);
2112 }
2113 return (NULL);
2114 }
2115
2116 /* Ignore device objects; the pager sets "memattr" for them. */
2117 if (object->memattr != VM_MEMATTR_DEFAULT &&
2118 (object->flags & OBJ_FICTITIOUS) == 0)
2119 pmap_page_set_memattr(m, object->memattr);
2120
2121 return (m);
2122}
2123
2124/*
2125 * vm_page_alloc_contig:
2126 *
2127 * Allocate a contiguous set of physical pages of the given size "npages"
2128 * from the free lists. All of the physical pages must be at or above
2129 * the given physical address "low" and below the given physical address
2130 * "high". The given value "alignment" determines the alignment of the
2131 * first physical page in the set. If the given value "boundary" is
2132 * non-zero, then the set of physical pages cannot cross any physical
2133 * address boundary that is a multiple of that value. Both "alignment"
2134 * and "boundary" must be a power of two.
2135 *
2136 * If the specified memory attribute, "memattr", is VM_MEMATTR_DEFAULT,
2137 * then the memory attribute setting for the physical pages is configured
2138 * to the object's memory attribute setting. Otherwise, the memory
2139 * attribute setting for the physical pages is configured to "memattr",
2140 * overriding the object's memory attribute setting. However, if the
2141 * object's memory attribute setting is not VM_MEMATTR_DEFAULT, then the
2142 * memory attribute setting for the physical pages cannot be configured
2143 * to VM_MEMATTR_DEFAULT.
2144 *
2145 * The specified object may not contain fictitious pages.
2146 *
2147 * The caller must always specify an allocation class.
2148 *
2149 * allocation classes:
2150 * VM_ALLOC_NORMAL normal process request
2151 * VM_ALLOC_SYSTEM system *really* needs a page
2152 * VM_ALLOC_INTERRUPT interrupt time request
2153 *
2154 * optional allocation flags:
2155 * VM_ALLOC_NOBUSY do not exclusive busy the page
2156 * VM_ALLOC_NODUMP do not include the page in a kernel core dump
2157 * VM_ALLOC_SBUSY shared busy the allocated page
2158 * VM_ALLOC_WIRED wire the allocated page
2159 * VM_ALLOC_ZERO prefer a zeroed page
2160 */
2161vm_page_t
2162vm_page_alloc_contig(vm_object_t object, vm_pindex_t pindex, int req,
2163 u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
2164 vm_paddr_t boundary, vm_memattr_t memattr)
2165{
2166 struct vm_domainset_iter di;
2167 vm_page_t m;
2168 int domain;
2169
2170 vm_domainset_iter_page_init(&di, object, pindex, &domain, &req);
2171 do {
2172 m = vm_page_alloc_contig_domain(object, pindex, domain, req,
2173 npages, low, high, alignment, boundary, memattr);
2174 if (m != NULL)
2175 break;
2176 } while (vm_domainset_iter_page(&di, object, &domain) == 0);
2177
2178 return (m);
2179}
2180
2181static vm_page_t
2182vm_page_find_contig_domain(int domain, int req, u_long npages, vm_paddr_t low,
2183 vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
2184{
2185 struct vm_domain *vmd;
2186 vm_page_t m_ret;
2187
2188 /*
2189 * Can we allocate the pages without the number of free pages falling
2190 * below the lower bound for the allocation class?
2191 */
2192 vmd = VM_DOMAIN(domain);
2193 if (!vm_domain_allocate(vmd, req, npages))
2194 return (NULL);
2195 /*
2196 * Try to allocate the pages from the free page queues.
2197 */
2199 m_ret = vm_phys_alloc_contig(domain, npages, low, high,
2200 alignment, boundary);
2202 if (m_ret != NULL)
2203 return (m_ret);
2204#if VM_NRESERVLEVEL > 0
2205 /*
2206 * Try to break a reservation to allocate the pages.
2207 */
2208 if ((req & VM_ALLOC_NORECLAIM) == 0) {
2209 m_ret = vm_reserv_reclaim_contig(domain, npages, low,
2210 high, alignment, boundary);
2211 if (m_ret != NULL)
2212 return (m_ret);
2213 }
2214#endif
2215 vm_domain_freecnt_inc(vmd, npages);
2216 return (NULL);
2217}
2218
2219vm_page_t
2220vm_page_alloc_contig_domain(vm_object_t object, vm_pindex_t pindex, int domain,
2221 int req, u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
2222 vm_paddr_t boundary, vm_memattr_t memattr)
2223{
2224 vm_page_t m, m_ret, mpred;
2225 u_int busy_lock, flags, oflags;
2226
2227#define VPAC_FLAGS (VPA_FLAGS | VM_ALLOC_NORECLAIM)
2228 KASSERT((req & ~VPAC_FLAGS) == 0,
2229 ("invalid request %#x", req));
2230 KASSERT(((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) !=
2232 ("invalid request %#x", req));
2233 KASSERT((req & (VM_ALLOC_WAITOK | VM_ALLOC_NORECLAIM)) !=
2235 ("invalid request %#x", req));
2237 KASSERT((object->flags & OBJ_FICTITIOUS) == 0,
2238 ("vm_page_alloc_contig: object %p has fictitious pages",
2239 object));
2240 KASSERT(npages > 0, ("vm_page_alloc_contig: npages is zero"));
2241
2242 mpred = vm_radix_lookup_le(&object->rtree, pindex);
2243 KASSERT(mpred == NULL || mpred->pindex != pindex,
2244 ("vm_page_alloc_contig: pindex already allocated"));
2245 for (;;) {
2246#if VM_NRESERVLEVEL > 0
2247 /*
2248 * Can we allocate the pages from a reservation?
2249 */
2250 if (vm_object_reserv(object) &&
2251 (m_ret = vm_reserv_alloc_contig(object, pindex, domain, req,
2252 mpred, npages, low, high, alignment, boundary)) != NULL) {
2253 break;
2254 }
2255#endif
2256 if ((m_ret = vm_page_find_contig_domain(domain, req, npages,
2257 low, high, alignment, boundary)) != NULL)
2258 break;
2259 if (!vm_domain_alloc_fail(VM_DOMAIN(domain), object, req))
2260 return (NULL);
2261 }
2262 for (m = m_ret; m < &m_ret[npages]; m++) {
2263 vm_page_dequeue(m);
2265 }
2266
2267 /*
2268 * Initialize the pages. Only the PG_ZERO flag is inherited.
2269 */
2270 flags = PG_ZERO;
2271 if ((req & VM_ALLOC_NODUMP) != 0)
2272 flags |= PG_NODUMP;
2273 oflags = (object->flags & OBJ_UNMANAGED) != 0 ? VPO_UNMANAGED : 0;
2274 if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) == 0)
2275 busy_lock = VPB_CURTHREAD_EXCLUSIVE;
2276 else if ((req & VM_ALLOC_SBUSY) != 0)
2277 busy_lock = VPB_SHARERS_WORD(1);
2278 else
2279 busy_lock = VPB_UNBUSIED;
2280 if ((req & VM_ALLOC_WIRED) != 0)
2281 vm_wire_add(npages);
2282 if (object->memattr != VM_MEMATTR_DEFAULT &&
2283 memattr == VM_MEMATTR_DEFAULT)
2284 memattr = object->memattr;
2285 for (m = m_ret; m < &m_ret[npages]; m++) {
2286 m->a.flags = 0;
2287 m->flags = (m->flags | PG_NODUMP) & flags;
2288 m->busy_lock = busy_lock;
2289 if ((req & VM_ALLOC_WIRED) != 0)
2290 m->ref_count = 1;
2291 m->a.act_count = 0;
2292 m->oflags = oflags;
2293 if (vm_page_insert_after(m, object, pindex, mpred)) {
2294 if ((req & VM_ALLOC_WIRED) != 0)
2295 vm_wire_sub(npages);
2296 KASSERT(m->object == NULL,
2297 ("page %p has object", m));
2298 mpred = m;
2299 for (m = m_ret; m < &m_ret[npages]; m++) {
2300 if (m <= mpred &&
2301 (req & VM_ALLOC_WIRED) != 0)
2302 m->ref_count = 0;
2303 m->oflags = VPO_UNMANAGED;
2304 m->busy_lock = VPB_UNBUSIED;
2305 /* Don't change PG_ZERO. */
2307 }
2308 if (req & VM_ALLOC_WAITFAIL) {
2309 VM_OBJECT_WUNLOCK(object);
2310 vm_radix_wait();
2311 VM_OBJECT_WLOCK(object);
2312 }
2313 return (NULL);
2314 }
2315 mpred = m;
2316 if (memattr != VM_MEMATTR_DEFAULT)
2317 pmap_page_set_memattr(m, memattr);
2318 pindex++;
2319 }
2320 return (m_ret);
2321}
2322
2323/*
2324 * Allocate a physical page that is not intended to be inserted into a VM
2325 * object. If the "freelist" parameter is not equal to VM_NFREELIST, then only
2326 * pages from the specified vm_phys freelist will be returned.
2327 */
2328static __always_inline vm_page_t
2329_vm_page_alloc_noobj_domain(int domain, const int freelist, int req)
2330{
2331 struct vm_domain *vmd;
2332 vm_page_t m;
2333 int flags;
2334
2335#define VPAN_FLAGS (VM_ALLOC_CLASS_MASK | VM_ALLOC_WAITFAIL | \
2336 VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | \
2337 VM_ALLOC_NOBUSY | VM_ALLOC_WIRED | \
2338 VM_ALLOC_NODUMP | VM_ALLOC_ZERO | VM_ALLOC_COUNT_MASK)
2339 KASSERT((req & ~VPAN_FLAGS) == 0,
2340 ("invalid request %#x", req));
2341
2342 flags = (req & VM_ALLOC_NODUMP) != 0 ? PG_NODUMP : 0;
2343 vmd = VM_DOMAIN(domain);
2344again:
2345 if (freelist == VM_NFREELIST &&
2346 vmd->vmd_pgcache[VM_FREEPOOL_DIRECT].zone != NULL) {
2347 m = uma_zalloc(vmd->vmd_pgcache[VM_FREEPOOL_DIRECT].zone,
2348 M_NOWAIT | M_NOVM);
2349 if (m != NULL) {
2350 flags |= PG_PCPU_CACHE;
2351 goto found;
2352 }
2353 }
2354
2355 if (vm_domain_allocate(vmd, req, 1)) {
2357 if (freelist == VM_NFREELIST)
2358 m = vm_phys_alloc_pages(domain, VM_FREEPOOL_DIRECT, 0);
2359 else
2360 m = vm_phys_alloc_freelist_pages(domain, freelist,
2361 VM_FREEPOOL_DIRECT, 0);
2363 if (m == NULL) {
2364 vm_domain_freecnt_inc(vmd, 1);
2365#if VM_NRESERVLEVEL > 0
2366 if (freelist == VM_NFREELIST &&
2367 vm_reserv_reclaim_inactive(domain))
2368 goto again;
2369#endif
2370 }
2371 }
2372 if (m == NULL) {
2373 if (vm_domain_alloc_fail(vmd, NULL, req))
2374 goto again;
2375 return (NULL);
2376 }
2377
2378found:
2379 vm_page_dequeue(m);
2381
2382 /*
2383 * Consumers should not rely on a useful default pindex value.
2384 */
2385 m->pindex = 0xdeadc0dedeadc0de;
2386 m->flags = (m->flags & PG_ZERO) | flags;
2387 m->a.flags = 0;
2388 m->oflags = VPO_UNMANAGED;
2389 m->busy_lock = VPB_UNBUSIED;
2390 if ((req & VM_ALLOC_WIRED) != 0) {
2391 vm_wire_add(1);
2392 m->ref_count = 1;
2393 }
2394
2395 if ((req & VM_ALLOC_ZERO) != 0 && (m->flags & PG_ZERO) == 0)
2396 pmap_zero_page(m);
2397
2398 return (m);
2399}
2400
2401vm_page_t
2402vm_page_alloc_freelist(int freelist, int req)
2403{
2404 struct vm_domainset_iter di;
2405 vm_page_t m;
2406 int domain;
2407
2408 vm_domainset_iter_page_init(&di, NULL, 0, &domain, &req);
2409 do {
2410 m = vm_page_alloc_freelist_domain(domain, freelist, req);
2411 if (m != NULL)
2412 break;
2413 } while (vm_domainset_iter_page(&di, NULL, &domain) == 0);
2414
2415 return (m);
2416}
2417
2418vm_page_t
2419vm_page_alloc_freelist_domain(int domain, int freelist, int req)
2420{
2421 KASSERT(freelist >= 0 && freelist < VM_NFREELIST,
2422 ("%s: invalid freelist %d", __func__, freelist));
2423
2424 return (_vm_page_alloc_noobj_domain(domain, freelist, req));
2425}
2426
2427vm_page_t
2429{
2430 struct vm_domainset_iter di;
2431 vm_page_t m;
2432 int domain;
2433
2434 vm_domainset_iter_page_init(&di, NULL, 0, &domain, &req);
2435 do {
2436 m = vm_page_alloc_noobj_domain(domain, req);
2437 if (m != NULL)
2438 break;
2439 } while (vm_domainset_iter_page(&di, NULL, &domain) == 0);
2440
2441 return (m);
2442}
2443
2444vm_page_t
2445vm_page_alloc_noobj_domain(int domain, int req)
2446{
2447 return (_vm_page_alloc_noobj_domain(domain, VM_NFREELIST, req));
2448}
2449
2450vm_page_t
2451vm_page_alloc_noobj_contig(int req, u_long npages, vm_paddr_t low,
2452 vm_paddr_t high, u_long alignment, vm_paddr_t boundary,
2453 vm_memattr_t memattr)
2454{
2455 struct vm_domainset_iter di;
2456 vm_page_t m;
2457 int domain;
2458
2459 vm_domainset_iter_page_init(&di, NULL, 0, &domain, &req);
2460 do {
2461 m = vm_page_alloc_noobj_contig_domain(domain, req, npages, low,
2462 high, alignment, boundary, memattr);
2463 if (m != NULL)
2464 break;
2465 } while (vm_domainset_iter_page(&di, NULL, &domain) == 0);
2466
2467 return (m);
2468}
2469
2470vm_page_t
2471vm_page_alloc_noobj_contig_domain(int domain, int req, u_long npages,
2472 vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary,
2473 vm_memattr_t memattr)
2474{
2475 vm_page_t m, m_ret;
2476 u_int flags;
2477
2478#define VPANC_FLAGS (VPAN_FLAGS | VM_ALLOC_NORECLAIM)
2479 KASSERT((req & ~VPANC_FLAGS) == 0,
2480 ("invalid request %#x", req));
2481 KASSERT((req & (VM_ALLOC_WAITOK | VM_ALLOC_NORECLAIM)) !=
2483 ("invalid request %#x", req));
2484 KASSERT(((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) !=
2486 ("invalid request %#x", req));
2487 KASSERT(npages > 0, ("vm_page_alloc_contig: npages is zero"));
2488
2489 while ((m_ret = vm_page_find_contig_domain(domain, req, npages,
2490 low, high, alignment, boundary)) == NULL) {
2491 if (!vm_domain_alloc_fail(VM_DOMAIN(domain), NULL, req))
2492 return (NULL);
2493 }
2494
2495 /*
2496 * Initialize the pages. Only the PG_ZERO flag is inherited.
2497 */
2498 flags = PG_ZERO;
2499 if ((req & VM_ALLOC_NODUMP) != 0)
2500 flags |= PG_NODUMP;
2501 if ((req & VM_ALLOC_WIRED) != 0)
2502 vm_wire_add(npages);
2503 for (m = m_ret; m < &m_ret[npages]; m++) {
2504 vm_page_dequeue(m);
2506
2507 /*
2508 * Consumers should not rely on a useful default pindex value.
2509 */
2510 m->pindex = 0xdeadc0dedeadc0de;
2511 m->a.flags = 0;
2512 m->flags = (m->flags | PG_NODUMP) & flags;
2513 m->busy_lock = VPB_UNBUSIED;
2514 if ((req & VM_ALLOC_WIRED) != 0)
2515 m->ref_count = 1;
2516 m->a.act_count = 0;
2517 m->oflags = VPO_UNMANAGED;
2518
2519 /*
2520 * Zero the page before updating any mappings since the page is
2521 * not yet shared with any devices which might require the
2522 * non-default memory attribute. pmap_page_set_memattr()
2523 * flushes data caches before returning.
2524 */
2525 if ((req & VM_ALLOC_ZERO) != 0 && (m->flags & PG_ZERO) == 0)
2526 pmap_zero_page(m);
2527 if (memattr != VM_MEMATTR_DEFAULT)
2528 pmap_page_set_memattr(m, memattr);
2529 }
2530 return (m_ret);
2531}
2532
2533/*
2534 * Check a page that has been freshly dequeued from a freelist.
2535 */
2536static void
2538{
2539
2540 KASSERT(m->object == NULL, ("page %p has object", m));
2541 KASSERT(m->a.queue == PQ_NONE &&
2542 (m->a.flags & PGA_QUEUE_STATE_MASK) == 0,
2543 ("page %p has unexpected queue %d, flags %#x",
2544 m, m->a.queue, (m->a.flags & PGA_QUEUE_STATE_MASK)));
2545 KASSERT(m->ref_count == 0, ("page %p has references", m));
2546 KASSERT(vm_page_busy_freed(m), ("page %p is not freed", m));
2547 KASSERT(m->dirty == 0, ("page %p is dirty", m));
2548 KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT,
2549 ("page %p has unexpected memattr %d",
2550 m, pmap_page_get_memattr(m)));
2551 KASSERT(m->valid == 0, ("free page %p is valid", m));
2552 pmap_vm_page_alloc_check(m);
2553}
2554
2555static int
2556vm_page_zone_import(void *arg, void **store, int cnt, int domain, int flags)
2557{
2558 struct vm_domain *vmd;
2559 struct vm_pgcache *pgcache;
2560 int i;
2561
2562 pgcache = arg;
2563 vmd = VM_DOMAIN(pgcache->domain);
2564
2565 /*
2566 * The page daemon should avoid creating extra memory pressure since its
2567 * main purpose is to replenish the store of free pages.
2568 */
2569 if (vmd->vmd_severeset || curproc == pageproc ||
2571 return (0);
2572 domain = vmd->vmd_domain;
2574 i = vm_phys_alloc_npages(domain, pgcache->pool, cnt,
2575 (vm_page_t *)store);
2577 if (cnt != i)
2578 vm_domain_freecnt_inc(vmd, cnt - i);
2579
2580 return (i);
2581}
2582
2583static void
2584vm_page_zone_release(void *arg, void **store, int cnt)
2585{
2586 struct vm_domain *vmd;
2587 struct vm_pgcache *pgcache;
2588 vm_page_t m;
2589 int i;
2590
2591 pgcache = arg;
2592 vmd = VM_DOMAIN(pgcache->domain);
2594 for (i = 0; i < cnt; i++) {
2595 m = (vm_page_t)store[i];
2596 vm_phys_free_pages(m, 0);
2597 }
2599 vm_domain_freecnt_inc(vmd, cnt);
2600}
2601
2602#define VPSC_ANY 0 /* No restrictions. */
2603#define VPSC_NORESERV 1 /* Skip reservations; implies VPSC_NOSUPER. */
2604#define VPSC_NOSUPER 2 /* Skip superpages. */
2605
2606/*
2607 * vm_page_scan_contig:
2608 *
2609 * Scan vm_page_array[] between the specified entries "m_start" and
2610 * "m_end" for a run of contiguous physical pages that satisfy the
2611 * specified conditions, and return the lowest page in the run. The
2612 * specified "alignment" determines the alignment of the lowest physical
2613 * page in the run. If the specified "boundary" is non-zero, then the
2614 * run of physical pages cannot span a physical address that is a
2615 * multiple of "boundary".
2616 *
2617 * "m_end" is never dereferenced, so it need not point to a vm_page
2618 * structure within vm_page_array[].
2619 *
2620 * "npages" must be greater than zero. "m_start" and "m_end" must not
2621 * span a hole (or discontiguity) in the physical address space. Both
2622 * "alignment" and "boundary" must be a power of two.
2623 */
2624vm_page_t
2625vm_page_scan_contig(u_long npages, vm_page_t m_start, vm_page_t m_end,
2626 u_long alignment, vm_paddr_t boundary, int options)
2627{
2628 vm_object_t object;
2629 vm_paddr_t pa;
2630 vm_page_t m, m_run;
2631#if VM_NRESERVLEVEL > 0
2632 int level;
2633#endif
2634 int m_inc, order, run_ext, run_len;
2635
2636 KASSERT(npages > 0, ("npages is 0"));
2637 KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
2638 KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
2639 m_run = NULL;
2640 run_len = 0;
2641 for (m = m_start; m < m_end && run_len < npages; m += m_inc) {
2642 KASSERT((m->flags & PG_MARKER) == 0,
2643 ("page %p is PG_MARKER", m));
2644 KASSERT((m->flags & PG_FICTITIOUS) == 0 || m->ref_count >= 1,
2645 ("fictitious page %p has invalid ref count", m));
2646
2647 /*
2648 * If the current page would be the start of a run, check its
2649 * physical address against the end, alignment, and boundary
2650 * conditions. If it doesn't satisfy these conditions, either
2651 * terminate the scan or advance to the next page that
2652 * satisfies the failed condition.
2653 */
2654 if (run_len == 0) {
2655 KASSERT(m_run == NULL, ("m_run != NULL"));
2656 if (m + npages > m_end)
2657 break;
2658 pa = VM_PAGE_TO_PHYS(m);
2659 if (!vm_addr_align_ok(pa, alignment)) {
2660 m_inc = atop(roundup2(pa, alignment) - pa);
2661 continue;
2662 }
2663 if (!vm_addr_bound_ok(pa, ptoa(npages), boundary)) {
2664 m_inc = atop(roundup2(pa, boundary) - pa);
2665 continue;
2666 }
2667 } else
2668 KASSERT(m_run != NULL, ("m_run == NULL"));
2669
2670retry:
2671 m_inc = 1;
2672 if (vm_page_wired(m))
2673 run_ext = 0;
2674#if VM_NRESERVLEVEL > 0
2675 else if ((level = vm_reserv_level(m)) >= 0 &&
2676 (options & VPSC_NORESERV) != 0) {
2677 run_ext = 0;
2678 /* Advance to the end of the reservation. */
2679 pa = VM_PAGE_TO_PHYS(m);
2680 m_inc = atop(roundup2(pa + 1, vm_reserv_size(level)) -
2681 pa);
2682 }
2683#endif
2684 else if ((object = atomic_load_ptr(&m->object)) != NULL) {
2685 /*
2686 * The page is considered eligible for relocation if
2687 * and only if it could be laundered or reclaimed by
2688 * the page daemon.
2689 */
2690 VM_OBJECT_RLOCK(object);
2691 if (object != m->object) {
2692 VM_OBJECT_RUNLOCK(object);
2693 goto retry;
2694 }
2695 /* Don't care: PG_NODUMP, PG_ZERO. */
2696 if (object->type != OBJT_DEFAULT &&
2697 (object->flags & OBJ_SWAP) == 0 &&
2698 object->type != OBJT_VNODE) {
2699 run_ext = 0;
2700#if VM_NRESERVLEVEL > 0
2701 } else if ((options & VPSC_NOSUPER) != 0 &&
2702 (level = vm_reserv_level_iffullpop(m)) >= 0) {
2703 run_ext = 0;
2704 /* Advance to the end of the superpage. */
2705 pa = VM_PAGE_TO_PHYS(m);
2706 m_inc = atop(roundup2(pa + 1,
2707 vm_reserv_size(level)) - pa);
2708#endif
2709 } else if (object->memattr == VM_MEMATTR_DEFAULT &&
2710 vm_page_queue(m) != PQ_NONE && !vm_page_busied(m)) {
2711 /*
2712 * The page is allocated but eligible for
2713 * relocation. Extend the current run by one
2714 * page.
2715 */
2716 KASSERT(pmap_page_get_memattr(m) ==
2717 VM_MEMATTR_DEFAULT,
2718 ("page %p has an unexpected memattr", m));
2719 KASSERT((m->oflags & (VPO_SWAPINPROG |
2721 ("page %p has unexpected oflags", m));
2722 /* Don't care: PGA_NOSYNC. */
2723 run_ext = 1;
2724 } else
2725 run_ext = 0;
2726 VM_OBJECT_RUNLOCK(object);
2727#if VM_NRESERVLEVEL > 0
2728 } else if (level >= 0) {
2729 /*
2730 * The page is reserved but not yet allocated. In
2731 * other words, it is still free. Extend the current
2732 * run by one page.
2733 */
2734 run_ext = 1;
2735#endif
2736 } else if ((order = m->order) < VM_NFREEORDER) {
2737 /*
2738 * The page is enqueued in the physical memory
2739 * allocator's free page queues. Moreover, it is the
2740 * first page in a power-of-two-sized run of
2741 * contiguous free pages. Add these pages to the end
2742 * of the current run, and jump ahead.
2743 */
2744 run_ext = 1 << order;
2745 m_inc = 1 << order;
2746 } else {
2747 /*
2748 * Skip the page for one of the following reasons: (1)
2749 * It is enqueued in the physical memory allocator's
2750 * free page queues. However, it is not the first
2751 * page in a run of contiguous free pages. (This case
2752 * rarely occurs because the scan is performed in
2753 * ascending order.) (2) It is not reserved, and it is
2754 * transitioning from free to allocated. (Conversely,
2755 * the transition from allocated to free for managed
2756 * pages is blocked by the page busy lock.) (3) It is
2757 * allocated but not contained by an object and not
2758 * wired, e.g., allocated by Xen's balloon driver.
2759 */
2760 run_ext = 0;
2761 }
2762
2763 /*
2764 * Extend or reset the current run of pages.
2765 */
2766 if (run_ext > 0) {
2767 if (run_len == 0)
2768 m_run = m;
2769 run_len += run_ext;
2770 } else {
2771 if (run_len > 0) {
2772 m_run = NULL;
2773 run_len = 0;
2774 }
2775 }
2776 }
2777 if (run_len >= npages)
2778 return (m_run);
2779 return (NULL);
2780}
2781
2782/*
2783 * vm_page_reclaim_run:
2784 *
2785 * Try to relocate each of the allocated virtual pages within the
2786 * specified run of physical pages to a new physical address. Free the
2787 * physical pages underlying the relocated virtual pages. A virtual page
2788 * is relocatable if and only if it could be laundered or reclaimed by
2789 * the page daemon. Whenever possible, a virtual page is relocated to a
2790 * physical address above "high".
2791 *
2792 * Returns 0 if every physical page within the run was already free or
2793 * just freed by a successful relocation. Otherwise, returns a non-zero
2794 * value indicating why the last attempt to relocate a virtual page was
2795 * unsuccessful.
2796 *
2797 * "req_class" must be an allocation class.
2798 */
2799static int
2800vm_page_reclaim_run(int req_class, int domain, u_long npages, vm_page_t m_run,
2801 vm_paddr_t high)
2802{
2803 struct vm_domain *vmd;
2804 struct spglist free;
2805 vm_object_t object;
2806 vm_paddr_t pa;
2807 vm_page_t m, m_end, m_new;
2808 int error, order, req;
2809
2810 KASSERT((req_class & VM_ALLOC_CLASS_MASK) == req_class,
2811 ("req_class is not an allocation class"));
2812 SLIST_INIT(&free);
2813 error = 0;
2814 m = m_run;
2815 m_end = m_run + npages;
2816 for (; error == 0 && m < m_end; m++) {
2817 KASSERT((m->flags & (PG_FICTITIOUS | PG_MARKER)) == 0,
2818 ("page %p is PG_FICTITIOUS or PG_MARKER", m));
2819
2820 /*
2821 * Racily check for wirings. Races are handled once the object
2822 * lock is held and the page is unmapped.
2823 */
2824 if (vm_page_wired(m))
2825 error = EBUSY;
2826 else if ((object = atomic_load_ptr(&m->object)) != NULL) {
2827 /*
2828 * The page is relocated if and only if it could be
2829 * laundered or reclaimed by the page daemon.
2830 */
2831 VM_OBJECT_WLOCK(object);
2832 /* Don't care: PG_NODUMP, PG_ZERO. */
2833 if (m->object != object ||
2834 (object->type != OBJT_DEFAULT &&
2835 (object->flags & OBJ_SWAP) == 0 &&
2836 object->type != OBJT_VNODE))
2837 error = EINVAL;
2838 else if (object->memattr != VM_MEMATTR_DEFAULT)
2839 error = EINVAL;
2840 else if (vm_page_queue(m) != PQ_NONE &&
2841 vm_page_tryxbusy(m) != 0) {
2842 if (vm_page_wired(m)) {
2843 vm_page_xunbusy(m);
2844 error = EBUSY;
2845 goto unlock;
2846 }
2847 KASSERT(pmap_page_get_memattr(m) ==
2848 VM_MEMATTR_DEFAULT,
2849 ("page %p has an unexpected memattr", m));
2850 KASSERT(m->oflags == 0,
2851 ("page %p has unexpected oflags", m));
2852 /* Don't care: PGA_NOSYNC. */
2853 if (!vm_page_none_valid(m)) {
2854 /*
2855 * First, try to allocate a new page
2856 * that is above "high". Failing
2857 * that, try to allocate a new page
2858 * that is below "m_run". Allocate
2859 * the new page between the end of
2860 * "m_run" and "high" only as a last
2861 * resort.
2862 */
2863 req = req_class;
2864 if ((m->flags & PG_NODUMP) != 0)
2865 req |= VM_ALLOC_NODUMP;
2866 if (trunc_page(high) !=
2867 ~(vm_paddr_t)PAGE_MASK) {
2868 m_new =
2870 req, 1, round_page(high),
2871 ~(vm_paddr_t)0, PAGE_SIZE,
2872 0, VM_MEMATTR_DEFAULT);
2873 } else
2874 m_new = NULL;
2875 if (m_new == NULL) {
2876 pa = VM_PAGE_TO_PHYS(m_run);
2877 m_new =
2879 req, 1, 0, pa - 1,
2880 PAGE_SIZE, 0,
2881 VM_MEMATTR_DEFAULT);
2882 }
2883 if (m_new == NULL) {
2884 pa += ptoa(npages);
2885 m_new =
2887 req, 1, pa, high, PAGE_SIZE,
2888 0, VM_MEMATTR_DEFAULT);
2889 }
2890 if (m_new == NULL) {
2891 vm_page_xunbusy(m);
2892 error = ENOMEM;
2893 goto unlock;
2894 }
2895
2896 /*
2897 * Unmap the page and check for new
2898 * wirings that may have been acquired
2899 * through a pmap lookup.
2900 */
2901 if (object->ref_count != 0 &&
2903 vm_page_xunbusy(m);
2904 vm_page_free(m_new);
2905 error = EBUSY;
2906 goto unlock;
2907 }
2908
2909 /*
2910 * Replace "m" with the new page. For
2911 * vm_page_replace(), "m" must be busy
2912 * and dequeued. Finally, change "m"
2913 * as if vm_page_free() was called.
2914 */
2915 m_new->a.flags = m->a.flags &
2916 ~PGA_QUEUE_STATE_MASK;
2917 KASSERT(m_new->oflags == VPO_UNMANAGED,
2918 ("page %p is managed", m_new));
2919 m_new->oflags = 0;
2920 pmap_copy_page(m, m_new);
2921 m_new->valid = m->valid;
2922 m_new->dirty = m->dirty;
2923 m->flags &= ~PG_ZERO;
2924 vm_page_dequeue(m);
2925 if (vm_page_replace_hold(m_new, object,
2926 m->pindex, m) &&
2928 SLIST_INSERT_HEAD(&free, m,
2929 plinks.s.ss);
2930
2931 /*
2932 * The new page must be deactivated
2933 * before the object is unlocked.
2934 */
2935 vm_page_deactivate(m_new);
2936 } else {
2937 m->flags &= ~PG_ZERO;
2938 vm_page_dequeue(m);
2939 if (vm_page_free_prep(m))
2940 SLIST_INSERT_HEAD(&free, m,
2941 plinks.s.ss);
2942 KASSERT(m->dirty == 0,
2943 ("page %p is dirty", m));
2944 }
2945 } else
2946 error = EBUSY;
2947unlock:
2948 VM_OBJECT_WUNLOCK(object);
2949 } else {
2950 MPASS(vm_page_domain(m) == domain);
2951 vmd = VM_DOMAIN(domain);
2953 order = m->order;
2954 if (order < VM_NFREEORDER) {
2955 /*
2956 * The page is enqueued in the physical memory
2957 * allocator's free page queues. Moreover, it
2958 * is the first page in a power-of-two-sized
2959 * run of contiguous free pages. Jump ahead
2960 * to the last page within that run, and
2961 * continue from there.
2962 */
2963 m += (1 << order) - 1;
2964 }
2965#if VM_NRESERVLEVEL > 0
2966 else if (vm_reserv_is_page_free(m))
2967 order = 0;
2968#endif
2970 if (order == VM_NFREEORDER)
2971 error = EINVAL;
2972 }
2973 }
2974 if ((m = SLIST_FIRST(&free)) != NULL) {
2975 int cnt;
2976
2977 vmd = VM_DOMAIN(domain);
2978 cnt = 0;
2980 do {
2981 MPASS(vm_page_domain(m) == domain);
2982 SLIST_REMOVE_HEAD(&free, plinks.s.ss);
2983 vm_phys_free_pages(m, 0);
2984 cnt++;
2985 } while ((m = SLIST_FIRST(&free)) != NULL);
2987 vm_domain_freecnt_inc(vmd, cnt);
2988 }
2989 return (error);
2990}
2991
2992#define NRUNS 16
2993
2994CTASSERT(powerof2(NRUNS));
2995
2996#define RUN_INDEX(count) ((count) & (NRUNS - 1))
2997
2998#define MIN_RECLAIM 8
2999
3000/*
3001 * vm_page_reclaim_contig:
3002 *
3003 * Reclaim allocated, contiguous physical memory satisfying the specified
3004 * conditions by relocating the virtual pages using that physical memory.
3005 * Returns true if reclamation is successful and false otherwise. Since
3006 * relocation requires the allocation of physical pages, reclamation may
3007 * fail due to a shortage of free pages. When reclamation fails, callers
3008 * are expected to perform vm_wait() before retrying a failed allocation
3009 * operation, e.g., vm_page_alloc_contig().
3010 *
3011 * The caller must always specify an allocation class through "req".
3012 *
3013 * allocation classes:
3014 * VM_ALLOC_NORMAL normal process request
3015 * VM_ALLOC_SYSTEM system *really* needs a page
3016 * VM_ALLOC_INTERRUPT interrupt time request
3017 *
3018 * The optional allocation flags are ignored.
3019 *
3020 * "npages" must be greater than zero. Both "alignment" and "boundary"
3021 * must be a power of two.
3022 */
3023bool
3024vm_page_reclaim_contig_domain(int domain, int req, u_long npages,
3025 vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
3026{
3027 struct vm_domain *vmd;
3028 vm_paddr_t curr_low;
3029 vm_page_t m_run, m_runs[NRUNS];
3030 u_long count, minalign, reclaimed;
3031 int error, i, options, req_class;
3032
3033 KASSERT(npages > 0, ("npages is 0"));
3034 KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
3035 KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
3036
3037 /*
3038 * The caller will attempt an allocation after some runs have been
3039 * reclaimed and added to the vm_phys buddy lists. Due to limitations
3040 * of vm_phys_alloc_contig(), round up the requested length to the next
3041 * power of two or maximum chunk size, and ensure that each run is
3042 * suitably aligned.
3043 */
3044 minalign = 1ul << imin(flsl(npages - 1), VM_NFREEORDER - 1);
3045 npages = roundup2(npages, minalign);
3046 if (alignment < ptoa(minalign))
3047 alignment = ptoa(minalign);
3048
3049 /*
3050 * The page daemon is allowed to dig deeper into the free page list.
3051 */
3052 req_class = req & VM_ALLOC_CLASS_MASK;
3053 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
3054 req_class = VM_ALLOC_SYSTEM;
3055
3056 /*
3057 * Return if the number of free pages cannot satisfy the requested
3058 * allocation.
3059 */
3060 vmd = VM_DOMAIN(domain);
3061 count = vmd->vmd_free_count;
3062 if (count < npages + vmd->vmd_free_reserved || (count < npages +
3063 vmd->vmd_interrupt_free_min && req_class == VM_ALLOC_SYSTEM) ||
3064 (count < npages && req_class == VM_ALLOC_INTERRUPT))
3065 return (false);
3066
3067 /*
3068 * Scan up to three times, relaxing the restrictions ("options") on
3069 * the reclamation of reservations and superpages each time.
3070 */
3071 for (options = VPSC_NORESERV;;) {
3072 /*
3073 * Find the highest runs that satisfy the given constraints
3074 * and restrictions, and record them in "m_runs".
3075 */
3076 curr_low = low;
3077 count = 0;
3078 for (;;) {
3079 m_run = vm_phys_scan_contig(domain, npages, curr_low,
3080 high, alignment, boundary, options);
3081 if (m_run == NULL)
3082 break;
3083 curr_low = VM_PAGE_TO_PHYS(m_run) + ptoa(npages);
3084 m_runs[RUN_INDEX(count)] = m_run;
3085 count++;
3086 }
3087
3088 /*
3089 * Reclaim the highest runs in LIFO (descending) order until
3090 * the number of reclaimed pages, "reclaimed", is at least
3091 * MIN_RECLAIM. Reset "reclaimed" each time because each
3092 * reclamation is idempotent, and runs will (likely) recur
3093 * from one scan to the next as restrictions are relaxed.
3094 */
3095 reclaimed = 0;
3096 for (i = 0; count > 0 && i < NRUNS; i++) {
3097 count--;
3098 m_run = m_runs[RUN_INDEX(count)];
3099 error = vm_page_reclaim_run(req_class, domain, npages,
3100 m_run, high);
3101 if (error == 0) {
3102 reclaimed += npages;
3103 if (reclaimed >= MIN_RECLAIM)
3104 return (true);
3105 }
3106 }
3107
3108 /*
3109 * Either relax the restrictions on the next scan or return if
3110 * the last scan had no restrictions.
3111 */
3112 if (options == VPSC_NORESERV)
3113 options = VPSC_NOSUPER;
3114 else if (options == VPSC_NOSUPER)
3115 options = VPSC_ANY;
3116 else if (options == VPSC_ANY)
3117 return (reclaimed != 0);
3118 }
3119}
3120
3121bool
3122vm_page_reclaim_contig(int req, u_long npages, vm_paddr_t low, vm_paddr_t high,
3123 u_long alignment, vm_paddr_t boundary)
3124{
3125 struct vm_domainset_iter di;
3126 int domain;
3127 bool ret;
3128
3129 vm_domainset_iter_page_init(&di, NULL, 0, &domain, &req);
3130 do {
3131 ret = vm_page_reclaim_contig_domain(domain, req, npages, low,
3132 high, alignment, boundary);
3133 if (ret)
3134 break;
3135 } while (vm_domainset_iter_page(&di, NULL, &domain) == 0);
3136
3137 return (ret);
3138}
3139
3140/*
3141 * Set the domain in the appropriate page level domainset.
3142 */
3143void
3145{
3146
3147 mtx_lock(&vm_domainset_lock);
3148 if (!vmd->vmd_minset && vm_paging_min(vmd)) {
3149 vmd->vmd_minset = 1;
3150 DOMAINSET_SET(vmd->vmd_domain, &vm_min_domains);
3151 }
3152 if (!vmd->vmd_severeset && vm_paging_severe(vmd)) {
3153 vmd->vmd_severeset = 1;
3154 DOMAINSET_SET(vmd->vmd_domain, &vm_severe_domains);
3155 }
3156 mtx_unlock(&vm_domainset_lock);
3157}
3158
3159/*
3160 * Clear the domain from the appropriate page level domainset.
3161 */
3162void
3164{
3165
3166 mtx_lock(&vm_domainset_lock);
3167 if (vmd->vmd_minset && !vm_paging_min(vmd)) {
3168 vmd->vmd_minset = 0;
3169 DOMAINSET_CLR(vmd->vmd_domain, &vm_min_domains);
3170 if (vm_min_waiters != 0) {
3171 vm_min_waiters = 0;
3172 wakeup(&vm_min_domains);
3173 }
3174 }
3175 if (vmd->vmd_severeset && !vm_paging_severe(vmd)) {
3176 vmd->vmd_severeset = 0;
3177 DOMAINSET_CLR(vmd->vmd_domain, &vm_severe_domains);
3178 if (vm_severe_waiters != 0) {
3180 wakeup(&vm_severe_domains);
3181 }
3182 }
3183
3184 /*
3185 * If pageout daemon needs pages, then tell it that there are
3186 * some free.
3187 */
3188 if (vmd->vmd_pageout_pages_needed &&
3189 vmd->vmd_free_count >= vmd->vmd_pageout_free_min) {
3190 wakeup(&vmd->vmd_pageout_pages_needed);
3191 vmd->vmd_pageout_pages_needed = 0;
3192 }
3193
3194 /* See comments in vm_wait_doms(). */
3195 if (vm_pageproc_waiters) {
3197 wakeup(&vm_pageproc_waiters);
3198 }
3199 mtx_unlock(&vm_domainset_lock);
3200}
3201
3202/*
3203 * Wait for free pages to exceed the min threshold globally.
3204 */
3205void
3207{
3208
3209 mtx_lock(&vm_domainset_lock);
3210 while (vm_page_count_min()) {
3212 msleep(&vm_min_domains, &vm_domainset_lock, PVM, "vmwait", 0);
3213 }
3214 mtx_unlock(&vm_domainset_lock);
3215}
3216
3217/*
3218 * Wait for free pages to exceed the severe threshold globally.
3219 */
3220void
3222{
3223
3224 mtx_lock(&vm_domainset_lock);
3225 while (vm_page_count_severe()) {
3227 msleep(&vm_severe_domains, &vm_domainset_lock, PVM,
3228 "vmwait", 0);
3229 }
3230 mtx_unlock(&vm_domainset_lock);
3231}
3232
3233u_int
3235{
3236
3238}
3239
3240int
3241vm_wait_doms(const domainset_t *wdoms, int mflags)
3242{
3243 int error;
3244
3245 error = 0;
3246
3247 /*
3248 * We use racey wakeup synchronization to avoid expensive global
3249 * locking for the pageproc when sleeping with a non-specific vm_wait.
3250 * To handle this, we only sleep for one tick in this instance. It
3251 * is expected that most allocations for the pageproc will come from
3252 * kmem or vm_page_grab* which will use the more specific and
3253 * race-free vm_wait_domain().
3254 */
3255 if (curproc == pageproc) {
3256 mtx_lock(&vm_domainset_lock);
3258 error = msleep(&vm_pageproc_waiters, &vm_domainset_lock,
3259 PVM | PDROP | mflags, "pageprocwait", 1);
3260 } else {
3261 /*
3262 * XXX Ideally we would wait only until the allocation could
3263 * be satisfied. This condition can cause new allocators to
3264 * consume all freed pages while old allocators wait.
3265 */
3266 mtx_lock(&vm_domainset_lock);
3267 if (vm_page_count_min_set(wdoms)) {
3269 error = msleep(&vm_min_domains, &vm_domainset_lock,
3270 PVM | PDROP | mflags, "vmwait", 0);
3271 } else
3272 mtx_unlock(&vm_domainset_lock);
3273 }
3274 return (error);
3275}
3276
3277/*
3278 * vm_wait_domain:
3279 *
3280 * Sleep until free pages are available for allocation.
3281 * - Called in various places after failed memory allocations.
3282 */
3283void
3285{
3286 struct vm_domain *vmd;
3287 domainset_t wdom;
3288
3289 vmd = VM_DOMAIN(domain);
3291
3292 if (curproc == pageproc) {
3293 mtx_lock(&vm_domainset_lock);
3294 if (vmd->vmd_free_count < vmd->vmd_pageout_free_min) {
3295 vmd->vmd_pageout_pages_needed = 1;
3296 msleep(&vmd->vmd_pageout_pages_needed,
3297 &vm_domainset_lock, PDROP | PSWP, "VMWait", 0);
3298 } else
3299 mtx_unlock(&vm_domainset_lock);
3300 } else {
3301 if (pageproc == NULL)
3302 panic("vm_wait in early boot");
3303 DOMAINSET_ZERO(&wdom);
3304 DOMAINSET_SET(vmd->vmd_domain, &wdom);
3305 vm_wait_doms(&wdom, 0);
3306 }
3307}
3308
3309static int
3311{
3312 struct domainset *d;
3313
3314 d = NULL;
3315
3316 /*
3317 * Carefully fetch pointers only once: the struct domainset
3318 * itself is ummutable but the pointer might change.
3319 */
3320 if (obj != NULL)
3321 d = obj->domain.dr_policy;
3322 if (d == NULL)
3323 d = curthread->td_domain.dr_policy;
3324
3325 return (vm_wait_doms(&d->ds_mask, mflags));
3326}
3327
3328/*
3329 * vm_wait:
3330 *
3331 * Sleep until free pages are available for allocation in the
3332 * affinity domains of the obj. If obj is NULL, the domain set
3333 * for the calling thread is used.
3334 * Called in various places after failed memory allocations.
3335 */
3336void
3338{
3339 (void)vm_wait_flags(obj, 0);
3340}
3341
3342int
3344{
3345 return (vm_wait_flags(obj, PCATCH));
3346}
3347
3348/*
3349 * vm_domain_alloc_fail:
3350 *
3351 * Called when a page allocation function fails. Informs the
3352 * pagedaemon and performs the requested wait. Requires the
3353 * domain_free and object lock on entry. Returns with the
3354 * object lock held and free lock released. Returns an error when
3355 * retry is necessary.
3356 *
3357 */
3358static int
3359vm_domain_alloc_fail(struct vm_domain *vmd, vm_object_t object, int req)
3360{
3361
3363
3364 atomic_add_int(&vmd->vmd_pageout_deficit,
3365 max((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1));
3366 if (req & (VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL)) {
3367 if (object != NULL)
3368 VM_OBJECT_WUNLOCK(object);
3370 if (object != NULL)
3371 VM_OBJECT_WLOCK(object);
3372 if (req & VM_ALLOC_WAITOK)
3373 return (EAGAIN);
3374 }
3375
3376 return (0);
3377}
3378
3379/*
3380 * vm_waitpfault:
3381 *
3382 * Sleep until free pages are available for allocation.
3383 * - Called only in vm_fault so that processes page faulting
3384 * can be easily tracked.
3385 * - Sleeps at a lower priority than vm_wait() so that vm_wait()ing
3386 * processes will be able to grab memory first. Do not change
3387 * this balance without careful testing first.
3388 */
3389void
3390vm_waitpfault(struct domainset *dset, int timo)
3391{
3392
3393 /*
3394 * XXX Ideally we would wait only until the allocation could
3395 * be satisfied. This condition can cause new allocators to
3396 * consume all freed pages while old allocators wait.
3397 */
3398 mtx_lock(&vm_domainset_lock);
3399 if (vm_page_count_min_set(&dset->ds_mask)) {
3401 msleep(&vm_min_domains, &vm_domainset_lock, PUSER | PDROP,
3402 "pfault", timo);
3403 } else
3404 mtx_unlock(&vm_domainset_lock);
3405}
3406
3407static struct vm_pagequeue *
3408_vm_page_pagequeue(vm_page_t m, uint8_t queue)
3409{
3410
3411 return (&vm_pagequeue_domain(m)->vmd_pagequeues[queue]);
3412}
3413
3414#ifdef INVARIANTS
3415static struct vm_pagequeue *
3416vm_page_pagequeue(vm_page_t m)
3417{
3418
3419 return (_vm_page_pagequeue(m, vm_page_astate_load(m).queue));
3420}
3421#endif
3422
3423static __always_inline bool
3425{
3426 vm_page_astate_t tmp;
3427
3428 tmp = *old;
3429 do {
3430 if (__predict_true(vm_page_astate_fcmpset(m, old, new)))
3431 return (true);
3432 counter_u64_add(pqstate_commit_retries, 1);
3433 } while (old->_bits == tmp._bits);
3434
3435 return (false);
3436}
3437
3438/*
3439 * Do the work of committing a queue state update that moves the page out of
3440 * its current queue.
3441 */
3442static bool
3445{
3446 vm_page_t next;
3447
3449 KASSERT(vm_page_pagequeue(m) == pq,
3450 ("%s: queue %p does not match page %p", __func__, pq, m));
3451 KASSERT(old->queue != PQ_NONE && new.queue != old->queue,
3452 ("%s: invalid queue indices %d %d",
3453 __func__, old->queue, new.queue));
3454
3455 /*
3456 * Once the queue index of the page changes there is nothing
3457 * synchronizing with further updates to the page's physical
3458 * queue state. Therefore we must speculatively remove the page
3459 * from the queue now and be prepared to roll back if the queue
3460 * state update fails. If the page is not physically enqueued then
3461 * we just update its queue index.
3462 */
3463 if ((old->flags & PGA_ENQUEUED) != 0) {
3464 new.flags &= ~PGA_ENQUEUED;
3465 next = TAILQ_NEXT(m, plinks.q);
3466 TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
3468 if (!vm_page_pqstate_fcmpset(m, old, new)) {
3469 if (next == NULL)
3470 TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
3471 else
3472 TAILQ_INSERT_BEFORE(next, m, plinks.q);
3474 return (false);
3475 } else {
3476 return (true);
3477 }
3478 } else {
3479 return (vm_page_pqstate_fcmpset(m, old, new));
3480 }
3481}
3482
3483static bool
3485 vm_page_astate_t new)
3486{
3487 struct vm_pagequeue *pq;
3489 bool ret;
3490
3491 pq = _vm_page_pagequeue(m, old->queue);
3492
3493 /*
3494 * The queue field and PGA_ENQUEUED flag are stable only so long as the
3495 * corresponding page queue lock is held.
3496 */
3498 as = vm_page_astate_load(m);
3499 if (__predict_false(as._bits != old->_bits)) {
3500 *old = as;
3501 ret = false;
3502 } else {
3503 ret = _vm_page_pqstate_commit_dequeue(pq, m, old, new);
3504 }
3506 return (ret);
3507}
3508
3509/*
3510 * Commit a queue state update that enqueues or requeues a page.
3511 */
3512static bool
3515{
3516 struct vm_domain *vmd;
3517
3519 KASSERT(old->queue != PQ_NONE && new.queue == old->queue,
3520 ("%s: invalid queue indices %d %d",
3521 __func__, old->queue, new.queue));
3522
3523 new.flags |= PGA_ENQUEUED;
3524 if (!vm_page_pqstate_fcmpset(m, old, new))
3525 return (false);
3526
3527 if ((old->flags & PGA_ENQUEUED) != 0)
3528 TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
3529 else
3531
3532 /*
3533 * Give PGA_REQUEUE_HEAD precedence over PGA_REQUEUE. In particular, if
3534 * both flags are set in close succession, only PGA_REQUEUE_HEAD will be
3535 * applied, even if it was set first.
3536 */
3537 if ((old->flags & PGA_REQUEUE_HEAD) != 0) {
3538 vmd = vm_pagequeue_domain(m);
3539 KASSERT(pq == &vmd->vmd_pagequeues[PQ_INACTIVE],
3540 ("%s: invalid page queue for page %p", __func__, m));
3541 TAILQ_INSERT_BEFORE(&vmd->vmd_inacthead, m, plinks.q);
3542 } else {
3543 TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
3544 }
3545 return (true);
3546}
3547
3548/*
3549 * Commit a queue state update that encodes a request for a deferred queue
3550 * operation.
3551 */
3552static bool
3554 vm_page_astate_t new)
3555{
3556
3557 KASSERT(old->queue == new.queue || new.queue != PQ_NONE,
3558 ("%s: invalid state, queue %d flags %x",
3559 __func__, new.queue, new.flags));
3560
3561 if (old->_bits != new._bits &&
3562 !vm_page_pqstate_fcmpset(m, old, new))
3563 return (false);
3564 vm_page_pqbatch_submit(m, new.queue);
3565 return (true);
3566}
3567
3568/*
3569 * A generic queue state update function. This handles more cases than the
3570 * specialized functions above.
3571 */
3572bool
3574{
3575
3576 if (old->_bits == new._bits)
3577 return (true);
3578
3579 if (old->queue != PQ_NONE && new.queue != old->queue) {
3580 if (!vm_page_pqstate_commit_dequeue(m, old, new))
3581 return (false);
3582 if (new.queue != PQ_NONE)
3583 vm_page_pqbatch_submit(m, new.queue);
3584 } else {
3585 if (!vm_page_pqstate_fcmpset(m, old, new))
3586 return (false);
3587 if (new.queue != PQ_NONE &&
3588 ((new.flags & ~old->flags) & PGA_QUEUE_OP_MASK) != 0)
3589 vm_page_pqbatch_submit(m, new.queue);
3590 }
3591 return (true);
3592}
3593
3594/*
3595 * Apply deferred queue state updates to a page.
3596 */
3597static inline void
3598vm_pqbatch_process_page(struct vm_pagequeue *pq, vm_page_t m, uint8_t queue)
3599{
3600 vm_page_astate_t new, old;
3601
3602 CRITICAL_ASSERT(curthread);
3604 KASSERT(queue < PQ_COUNT,
3605 ("%s: invalid queue index %d", __func__, queue));
3606 KASSERT(pq == _vm_page_pagequeue(m, queue),
3607 ("%s: page %p does not belong to queue %p", __func__, m, pq));
3608
3609 for (old = vm_page_astate_load(m);;) {
3610 if (__predict_false(old.queue != queue ||
3611 (old.flags & PGA_QUEUE_OP_MASK) == 0)) {
3612 counter_u64_add(queue_nops, 1);
3613 break;
3614 }
3615 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3616 ("%s: page %p is unmanaged", __func__, m));
3617
3618 new = old;
3619 if ((old.flags & PGA_DEQUEUE) != 0) {
3620 new.flags &= ~PGA_QUEUE_OP_MASK;
3621 new.queue = PQ_NONE;
3622 if (__predict_true(_vm_page_pqstate_commit_dequeue(pq,
3623 m, &old, new))) {
3624 counter_u64_add(queue_ops, 1);
3625 break;
3626 }
3627 } else {
3628 new.flags &= ~(PGA_REQUEUE | PGA_REQUEUE_HEAD);
3629 if (__predict_true(_vm_page_pqstate_commit_requeue(pq,
3630 m, &old, new))) {
3631 counter_u64_add(queue_ops, 1);
3632 break;
3633 }
3634 }
3635 }
3636}
3637
3638static void
3640 uint8_t queue)
3641{
3642 int i;
3643
3644 for (i = 0; i < bq->bq_cnt; i++)
3645 vm_pqbatch_process_page(pq, bq->bq_pa[i], queue);
3647}
3648
3649/*
3650 * vm_page_pqbatch_submit: [ internal use only ]
3651 *
3652 * Enqueue a page in the specified page queue's batched work queue.
3653 * The caller must have encoded the requested operation in the page
3654 * structure's a.flags field.
3655 */
3656void
3657vm_page_pqbatch_submit(vm_page_t m, uint8_t queue)
3658{
3659 struct vm_batchqueue *bq;
3660 struct vm_pagequeue *pq;
3661 int domain;
3662
3663 KASSERT(queue < PQ_COUNT, ("invalid queue %d", queue));
3664
3665 domain = vm_page_domain(m);
3666 critical_enter();
3667 bq = DPCPU_PTR(pqbatch[domain][queue]);
3668 if (vm_batchqueue_insert(bq, m)) {
3669 critical_exit();
3670 return;
3671 }
3672 critical_exit();
3673
3674 pq = &VM_DOMAIN(domain)->vmd_pagequeues[queue];
3676 critical_enter();
3677 bq = DPCPU_PTR(pqbatch[domain][queue]);
3678 vm_pqbatch_process(pq, bq, queue);
3679 vm_pqbatch_process_page(pq, m, queue);
3681 critical_exit();
3682}
3683
3684/*
3685 * vm_page_pqbatch_drain: [ internal use only ]
3686 *
3687 * Force all per-CPU page queue batch queues to be drained. This is
3688 * intended for use in severe memory shortages, to ensure that pages
3689 * do not remain stuck in the batch queues.
3690 */
3691void
3693{
3694 struct thread *td;
3695 struct vm_domain *vmd;
3696 struct vm_pagequeue *pq;
3697 int cpu, domain, queue;
3698
3699 td = curthread;
3700 CPU_FOREACH(cpu) {
3701 thread_lock(td);
3702 sched_bind(td, cpu);
3703 thread_unlock(td);
3704
3705 for (domain = 0; domain < vm_ndomains; domain++) {
3706 vmd = VM_DOMAIN(domain);
3707 for (queue = 0; queue < PQ_COUNT; queue++) {
3708 pq = &vmd->vmd_pagequeues[queue];
3710 critical_enter();
3712 DPCPU_PTR(pqbatch[domain][queue]), queue);
3713 critical_exit();
3715 }
3716 }
3717 }
3718 thread_lock(td);
3719 sched_unbind(td);
3720 thread_unlock(td);
3721}
3722
3723/*
3724 * vm_page_dequeue_deferred: [ internal use only ]
3725 *
3726 * Request removal of the given page from its current page
3727 * queue. Physical removal from the queue may be deferred
3728 * indefinitely.
3729 */
3730void
3732{
3733 vm_page_astate_t new, old;
3734
3735 old = vm_page_astate_load(m);
3736 do {
3737 if (old.queue == PQ_NONE) {
3738 KASSERT((old.flags & PGA_QUEUE_STATE_MASK) == 0,
3739 ("%s: page %p has unexpected queue state",
3740 __func__, m));
3741 break;
3742 }
3743 new = old;
3744 new.flags |= PGA_DEQUEUE;
3745 } while (!vm_page_pqstate_commit_request(m, &old, new));
3746}
3747
3748/*
3749 * vm_page_dequeue:
3750 *
3751 * Remove the page from whichever page queue it's in, if any, before
3752 * returning.
3753 */
3754void
3756{
3757 vm_page_astate_t new, old;
3758
3759 old = vm_page_astate_load(m);
3760 do {
3761 if (old.queue == PQ_NONE) {
3762 KASSERT((old.flags & PGA_QUEUE_STATE_MASK) == 0,
3763 ("%s: page %p has unexpected queue state",
3764 __func__, m));
3765 break;
3766 }
3767 new = old;
3768 new.flags &= ~PGA_QUEUE_OP_MASK;
3769 new.queue = PQ_NONE;
3770 } while (!vm_page_pqstate_commit_dequeue(m, &old, new));
3771
3772}
3773
3774/*
3775 * Schedule the given page for insertion into the specified page queue.
3776 * Physical insertion of the page may be deferred indefinitely.
3777 */
3778static void
3779vm_page_enqueue(vm_page_t m, uint8_t queue)
3780{
3781
3782 KASSERT(m->a.queue == PQ_NONE &&
3783 (m->a.flags & PGA_QUEUE_STATE_MASK) == 0,
3784 ("%s: page %p is already enqueued", __func__, m));
3785 KASSERT(m->ref_count > 0,
3786 ("%s: page %p does not carry any references", __func__, m));
3787
3788 m->a.queue = queue;
3789 if ((m->a.flags & PGA_REQUEUE) == 0)
3791 vm_page_pqbatch_submit(m, queue);
3792}
3793
3794/*
3795 * vm_page_free_prep:
3796 *
3797 * Prepares the given page to be put on the free list,
3798 * disassociating it from any VM object. The caller may return
3799 * the page to the free list only if this function returns true.
3800 *
3801 * The object, if it exists, must be locked, and then the page must
3802 * be xbusy. Otherwise the page must be not busied. A managed
3803 * page must be unmapped.
3804 */
3805static bool
3807{
3808
3809 /*
3810 * Synchronize with threads that have dropped a reference to this
3811 * page.
3812 */
3813 atomic_thread_fence_acq();
3814
3815#if defined(DIAGNOSTIC) && defined(PHYS_TO_DMAP)
3816 if (PMAP_HAS_DMAP && (m->flags & PG_ZERO) != 0) {
3817 uint64_t *p;
3818 int i;
3819 p = (uint64_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m));
3820 for (i = 0; i < PAGE_SIZE / sizeof(uint64_t); i++, p++)
3821 KASSERT(*p == 0, ("vm_page_free_prep %p PG_ZERO %d %jx",
3822 m, i, (uintmax_t)*p));
3823 }
3824#endif
3825 if ((m->oflags & VPO_UNMANAGED) == 0) {
3826 KASSERT(!pmap_page_is_mapped(m),
3827 ("vm_page_free_prep: freeing mapped page %p", m));
3828 KASSERT((m->a.flags & (PGA_EXECUTABLE | PGA_WRITEABLE)) == 0,
3829 ("vm_page_free_prep: mapping flags set in page %p", m));
3830 } else {
3831 KASSERT(m->a.queue == PQ_NONE,
3832 ("vm_page_free_prep: unmanaged page %p is queued", m));
3833 }
3834 VM_CNT_INC(v_tfree);
3835
3836 if (m->object != NULL) {
3837 KASSERT(((m->oflags & VPO_UNMANAGED) != 0) ==
3838 ((m->object->flags & OBJ_UNMANAGED) != 0),
3839 ("vm_page_free_prep: managed flag mismatch for page %p",
3840 m));
3842
3843 /*
3844 * The object reference can be released without an atomic
3845 * operation.
3846 */
3847 KASSERT((m->flags & PG_FICTITIOUS) != 0 ||
3848 m->ref_count == VPRC_OBJREF,
3849 ("vm_page_free_prep: page %p has unexpected ref_count %u",
3850 m, m->ref_count));
3852 m->ref_count -= VPRC_OBJREF;
3853 } else
3855
3857
3858 /*
3859 * If fictitious remove object association and
3860 * return.
3861 */
3862 if ((m->flags & PG_FICTITIOUS) != 0) {
3863 KASSERT(m->ref_count == 1,
3864 ("fictitious page %p is referenced", m));
3865 KASSERT(m->a.queue == PQ_NONE,
3866 ("fictitious page %p is queued", m));
3867 return (false);
3868 }
3869
3870 /*
3871 * Pages need not be dequeued before they are returned to the physical
3872 * memory allocator, but they must at least be marked for a deferred
3873 * dequeue.
3874 */
3875 if ((m->oflags & VPO_UNMANAGED) == 0)
3877
3878 m->valid = 0;
3879 vm_page_undirty(m);
3880
3881 if (m->ref_count != 0)
3882 panic("vm_page_free_prep: page %p has references", m);
3883
3884 /*
3885 * Restore the default memory attribute to the page.
3886 */
3887 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT)
3888 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT);
3889
3890#if VM_NRESERVLEVEL > 0
3891 /*
3892 * Determine whether the page belongs to a reservation. If the page was
3893 * allocated from a per-CPU cache, it cannot belong to a reservation, so
3894 * as an optimization, we avoid the check in that case.
3895 */
3896 if ((m->flags & PG_PCPU_CACHE) == 0 && vm_reserv_free_page(m))
3897 return (false);
3898#endif
3899
3900 return (true);
3901}
3902
3903/*
3904 * vm_page_free_toq:
3905 *
3906 * Returns the given page to the free list, disassociating it
3907 * from any VM object.
3908 *
3909 * The object must be locked. The page must be exclusively busied if it
3910 * belongs to an object.
3911 */
3912static void
3914{
3915 struct vm_domain *vmd;
3916 uma_zone_t zone;
3917
3918 if (!vm_page_free_prep(m))
3919 return;
3920
3921 vmd = vm_pagequeue_domain(m);
3922 zone = vmd->vmd_pgcache[m->pool].zone;
3923 if ((m->flags & PG_PCPU_CACHE) != 0 && zone != NULL) {
3924 uma_zfree(zone, m);
3925 return;
3926 }
3928 vm_phys_free_pages(m, 0);
3930 vm_domain_freecnt_inc(vmd, 1);
3931}
3932
3933/*
3934 * vm_page_free_pages_toq:
3935 *
3936 * Returns a list of pages to the free list, disassociating it
3937 * from any VM object. In other words, this is equivalent to
3938 * calling vm_page_free_toq() for each page of a list of VM objects.
3939 */
3940void
3941vm_page_free_pages_toq(struct spglist *free, bool update_wire_count)
3942{
3943 vm_page_t m;
3944 int count;
3945
3946 if (SLIST_EMPTY(free))
3947 return;
3948
3949 count = 0;
3950 while ((m = SLIST_FIRST(free)) != NULL) {
3951 count++;
3952 SLIST_REMOVE_HEAD(free, plinks.s.ss);
3954 }
3955
3956 if (update_wire_count)
3957 vm_wire_sub(count);
3958}
3959
3960/*
3961 * Mark this page as wired down. For managed pages, this prevents reclamation
3962 * by the page daemon, or when the containing object, if any, is destroyed.
3963 */
3964void
3965vm_page_wire(vm_page_t m)
3966{
3967 u_int old;
3968
3969#ifdef INVARIANTS
3970 if (m->object != NULL && !vm_page_busied(m) &&
3971 !vm_object_busied(m->object))
3972 VM_OBJECT_ASSERT_LOCKED(m->object);
3973#endif
3974 KASSERT((m->flags & PG_FICTITIOUS) == 0 ||
3975 VPRC_WIRE_COUNT(m->ref_count) >= 1,
3976 ("vm_page_wire: fictitious page %p has zero wirings", m));
3977
3978 old = atomic_fetchadd_int(&m->ref_count, 1);
3979 KASSERT(VPRC_WIRE_COUNT(old) != VPRC_WIRE_COUNT_MAX,
3980 ("vm_page_wire: counter overflow for page %p", m));
3981 if (VPRC_WIRE_COUNT(old) == 0) {
3982 if ((m->oflags & VPO_UNMANAGED) == 0)
3984 vm_wire_add(1);
3985 }
3986}
3987
3988/*
3989 * Attempt to wire a mapped page following a pmap lookup of that page.
3990 * This may fail if a thread is concurrently tearing down mappings of the page.
3991 * The transient failure is acceptable because it translates to the
3992 * failure of the caller pmap_extract_and_hold(), which should be then
3993 * followed by the vm_fault() fallback, see e.g. vm_fault_quick_hold_pages().
3994 */
3995bool
3997{
3998 u_int old;
3999
4000 old = m->ref_count;
4001 do {
4002 KASSERT(old > 0,
4003 ("vm_page_wire_mapped: wiring unreferenced page %p", m));
4004 if ((old & VPRC_BLOCKED) != 0)
4005 return (false);
4006 } while (!atomic_fcmpset_int(&m->ref_count, &old, old + 1));
4007
4008 if (VPRC_WIRE_COUNT(old) == 0) {
4009 if ((m->oflags & VPO_UNMANAGED) == 0)
4011 vm_wire_add(1);
4012 }
4013 return (true);
4014}
4015
4016/*
4017 * Release a wiring reference to a managed page. If the page still belongs to
4018 * an object, update its position in the page queues to reflect the reference.
4019 * If the wiring was the last reference to the page, free the page.
4020 */
4021static void
4022vm_page_unwire_managed(vm_page_t m, uint8_t nqueue, bool noreuse)
4023{
4024 u_int old;
4025
4026 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4027 ("%s: page %p is unmanaged", __func__, m));
4028
4029 /*
4030 * Update LRU state before releasing the wiring reference.
4031 * Use a release store when updating the reference count to
4032 * synchronize with vm_page_free_prep().
4033 */
4034 old = m->ref_count;
4035 do {
4036 KASSERT(VPRC_WIRE_COUNT(old) > 0,
4037 ("vm_page_unwire: wire count underflow for page %p", m));
4038
4039 if (old > VPRC_OBJREF + 1) {
4040 /*
4041 * The page has at least one other wiring reference. An
4042 * earlier iteration of this loop may have called
4043 * vm_page_release_toq() and cleared PGA_DEQUEUE, so
4044 * re-set it if necessary.
4045 */
4046 if ((vm_page_astate_load(m).flags & PGA_DEQUEUE) == 0)
4048 } else if (old == VPRC_OBJREF + 1) {
4049 /*
4050 * This is the last wiring. Clear PGA_DEQUEUE and
4051 * update the page's queue state to reflect the
4052 * reference. If the page does not belong to an object
4053 * (i.e., the VPRC_OBJREF bit is clear), we only need to
4054 * clear leftover queue state.
4055 */
4056 vm_page_release_toq(m, nqueue, noreuse);
4057 } else if (old == 1) {
4059 }
4060 } while (!atomic_fcmpset_rel_int(&m->ref_count, &old, old - 1));
4061
4062 if (VPRC_WIRE_COUNT(old) == 1) {
4063 vm_wire_sub(1);
4064 if (old == 1)
4065 vm_page_free(m);
4066 }
4067}
4068
4069/*
4070 * Release one wiring of the specified page, potentially allowing it to be
4071 * paged out.
4072 *
4073 * Only managed pages belonging to an object can be paged out. If the number
4074 * of wirings transitions to zero and the page is eligible for page out, then
4075 * the page is added to the specified paging queue. If the released wiring
4076 * represented the last reference to the page, the page is freed.
4077 */
4078void
4079vm_page_unwire(vm_page_t m, uint8_t nqueue)
4080{
4081
4082 KASSERT(nqueue < PQ_COUNT,
4083 ("vm_page_unwire: invalid queue %u request for page %p",
4084 nqueue, m));
4085
4086 if ((m->oflags & VPO_UNMANAGED) != 0) {
4087 if (vm_page_unwire_noq(m) && m->ref_count == 0)
4088 vm_page_free(m);
4089 return;
4090 }
4091 vm_page_unwire_managed(m, nqueue, false);
4092}
4093
4094/*
4095 * Unwire a page without (re-)inserting it into a page queue. It is up
4096 * to the caller to enqueue, requeue, or free the page as appropriate.
4097 * In most cases involving managed pages, vm_page_unwire() should be used
4098 * instead.
4099 */
4100bool
4102{
4103 u_int old;
4104
4105 old = vm_page_drop(m, 1);
4106 KASSERT(VPRC_WIRE_COUNT(old) != 0,
4107 ("%s: counter underflow for page %p", __func__, m));
4108 KASSERT((m->flags & PG_FICTITIOUS) == 0 || VPRC_WIRE_COUNT(old) > 1,
4109 ("%s: missing ref on fictitious page %p", __func__, m));
4110
4111 if (VPRC_WIRE_COUNT(old) > 1)
4112 return (false);
4113 if ((m->oflags & VPO_UNMANAGED) == 0)
4115 vm_wire_sub(1);
4116 return (true);
4117}
4118
4119/*
4120 * Ensure that the page ends up in the specified page queue. If the page is
4121 * active or being moved to the active queue, ensure that its act_count is
4122 * at least ACT_INIT but do not otherwise mess with it.
4123 */
4124static __always_inline void
4125vm_page_mvqueue(vm_page_t m, const uint8_t nqueue, const uint16_t nflag)
4126{
4127 vm_page_astate_t old, new;
4128
4129 KASSERT(m->ref_count > 0,
4130 ("%s: page %p does not carry any references", __func__, m));
4131 KASSERT(nflag == PGA_REQUEUE || nflag == PGA_REQUEUE_HEAD,
4132 ("%s: invalid flags %x", __func__, nflag));
4133
4134 if ((m->oflags & VPO_UNMANAGED) != 0 || vm_page_wired(m))
4135 return;
4136
4137 old = vm_page_astate_load(m);
4138 do {
4139 if ((old.flags & PGA_DEQUEUE) != 0)
4140 break;
4141 new = old;
4142 new.flags &= ~PGA_QUEUE_OP_MASK;
4143 if (nqueue == PQ_ACTIVE)
4144 new.act_count = max(old.act_count, ACT_INIT);
4145 if (old.queue == nqueue) {
4146 if (nqueue != PQ_ACTIVE)
4147 new.flags |= nflag;
4148 } else {
4149 new.flags |= nflag;
4150 new.queue = nqueue;
4151 }
4152 } while (!vm_page_pqstate_commit(m, &old, new));
4153}
4154
4155/*
4156 * Put the specified page on the active list (if appropriate).
4157 */
4158void
4160{
4161
4163}
4164
4165/*
4166 * Move the specified page to the tail of the inactive queue, or requeue
4167 * the page if it is already in the inactive queue.
4168 */
4169void
4171{
4172
4174}
4175
4176void
4178{
4179
4181}
4182
4183/*
4184 * Put a page in the laundry, or requeue it if it is already there.
4185 */
4186void
4188{
4189
4191}
4192
4193/*
4194 * Put a page in the PQ_UNSWAPPABLE holding queue.
4195 */
4196void
4198{
4199
4200 KASSERT(!vm_page_wired(m) && (m->oflags & VPO_UNMANAGED) == 0,
4201 ("page %p already unswappable", m));
4202
4203 vm_page_dequeue(m);
4205}
4206
4207/*
4208 * Release a page back to the page queues in preparation for unwiring.
4209 */
4210static void
4211vm_page_release_toq(vm_page_t m, uint8_t nqueue, const bool noreuse)
4212{
4213 vm_page_astate_t old, new;
4214 uint16_t nflag;
4215
4216 /*
4217 * Use a check of the valid bits to determine whether we should
4218 * accelerate reclamation of the page. The object lock might not be
4219 * held here, in which case the check is racy. At worst we will either
4220 * accelerate reclamation of a valid page and violate LRU, or
4221 * unnecessarily defer reclamation of an invalid page.
4222 *
4223 * If we were asked to not cache the page, place it near the head of the
4224 * inactive queue so that is reclaimed sooner.
4225 */
4226 if (noreuse || m->valid == 0) {
4227 nqueue = PQ_INACTIVE;
4228 nflag = PGA_REQUEUE_HEAD;
4229 } else {
4230 nflag = PGA_REQUEUE;
4231 }
4232
4233 old = vm_page_astate_load(m);
4234 do {
4235 new = old;
4236
4237 /*
4238 * If the page is already in the active queue and we are not
4239 * trying to accelerate reclamation, simply mark it as
4240 * referenced and avoid any queue operations.
4241 */
4242 new.flags &= ~PGA_QUEUE_OP_MASK;
4243 if (nflag != PGA_REQUEUE_HEAD && old.queue == PQ_ACTIVE)
4244 new.flags |= PGA_REFERENCED;
4245 else {
4246 new.flags |= nflag;
4247 new.queue = nqueue;
4248 }
4249 } while (!vm_page_pqstate_commit(m, &old, new));
4250}
4251
4252/*
4253 * Unwire a page and either attempt to free it or re-add it to the page queues.
4254 */
4255void
4256vm_page_release(vm_page_t m, int flags)
4257{
4258 vm_object_t object;
4259
4260 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4261 ("vm_page_release: page %p is unmanaged", m));
4262
4263 if ((flags & VPR_TRYFREE) != 0) {
4264 for (;;) {
4265 object = atomic_load_ptr(&m->object);
4266 if (object == NULL)
4267 break;
4268 /* Depends on type-stability. */
4269 if (vm_page_busied(m) || !VM_OBJECT_TRYWLOCK(object))
4270 break;
4271 if (object == m->object) {
4272 vm_page_release_locked(m, flags);
4273 VM_OBJECT_WUNLOCK(object);
4274 return;
4275 }
4276 VM_OBJECT_WUNLOCK(object);
4277 }
4278 }
4279 vm_page_unwire_managed(m, PQ_INACTIVE, flags != 0);
4280}
4281
4282/* See vm_page_release(). */
4283void
4284vm_page_release_locked(vm_page_t m, int flags)
4285{
4286
4287 VM_OBJECT_ASSERT_WLOCKED(m->object);
4288 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4289 ("vm_page_release_locked: page %p is unmanaged", m));
4290
4291 if (vm_page_unwire_noq(m)) {
4292 if ((flags & VPR_TRYFREE) != 0 &&
4293 (m->object->ref_count == 0 || !pmap_page_is_mapped(m)) &&
4294 m->dirty == 0 && vm_page_tryxbusy(m)) {
4295 /*
4296 * An unlocked lookup may have wired the page before the
4297 * busy lock was acquired, in which case the page must
4298 * not be freed.
4299 */
4300 if (__predict_true(!vm_page_wired(m))) {
4301 vm_page_free(m);
4302 return;
4303 }
4304 vm_page_xunbusy(m);
4305 } else {
4306 vm_page_release_toq(m, PQ_INACTIVE, flags != 0);
4307 }
4308 }
4309}
4310
4311static bool
4312vm_page_try_blocked_op(vm_page_t m, void (*op)(vm_page_t))
4313{
4314 u_int old;
4315
4316 KASSERT(m->object != NULL && (m->oflags & VPO_UNMANAGED) == 0,
4317 ("vm_page_try_blocked_op: page %p has no object", m));
4318 KASSERT(vm_page_busied(m),
4319 ("vm_page_try_blocked_op: page %p is not busy", m));
4320 VM_OBJECT_ASSERT_LOCKED(m->object);
4321
4322 old = m->ref_count;
4323 do {
4324 KASSERT(old != 0,
4325 ("vm_page_try_blocked_op: page %p has no references", m));
4326 if (VPRC_WIRE_COUNT(old) != 0)
4327 return (false);
4328 } while (!atomic_fcmpset_int(&m->ref_count, &old, old | VPRC_BLOCKED));
4329
4330 (op)(m);
4331
4332 /*
4333 * If the object is read-locked, new wirings may be created via an
4334 * object lookup.
4335 */
4336 old = vm_page_drop(m, VPRC_BLOCKED);
4337 KASSERT(!VM_OBJECT_WOWNED(m->object) ||
4338 old == (VPRC_BLOCKED | VPRC_OBJREF),
4339 ("vm_page_try_blocked_op: unexpected refcount value %u for %p",
4340 old, m));
4341 return (true);
4342}
4343
4344/*
4345 * Atomically check for wirings and remove all mappings of the page.
4346 */
4347bool
4349{
4350
4352}
4353
4354/*
4355 * Atomically check for wirings and remove all writeable mappings of the page.
4356 */
4357bool
4359{
4360
4362}
4363
4364/*
4365 * vm_page_advise
4366 *
4367 * Apply the specified advice to the given page.
4368 */
4369void
4370vm_page_advise(vm_page_t m, int advice)
4371{
4372
4373 VM_OBJECT_ASSERT_WLOCKED(m->object);
4375
4376 if (advice == MADV_FREE)
4377 /*
4378 * Mark the page clean. This will allow the page to be freed
4379 * without first paging it out. MADV_FREE pages are often
4380 * quickly reused by malloc(3), so we do not do anything that
4381 * would result in a page fault on a later access.
4382 */
4383 vm_page_undirty(m);
4384 else if (advice != MADV_DONTNEED) {
4385 if (advice == MADV_WILLNEED)
4387 return;
4388 }
4389
4390 if (advice != MADV_FREE && m->dirty == 0 && pmap_is_modified(m))
4391 vm_page_dirty(m);
4392
4393 /*
4394 * Clear any references to the page. Otherwise, the page daemon will
4395 * immediately reactivate the page.
4396 */
4398
4399 /*
4400 * Place clean pages near the head of the inactive queue rather than
4401 * the tail, thus defeating the queue's LRU operation and ensuring that
4402 * the page will be reused quickly. Dirty pages not already in the
4403 * laundry are moved there.
4404 */
4405 if (m->dirty == 0)
4407 else if (!vm_page_in_laundry(m))
4408 vm_page_launder(m);
4409}
4410
4411/*
4412 * vm_page_grab_release
4413 *
4414 * Helper routine for grab functions to release busy on return.
4415 */
4416static inline void
4417vm_page_grab_release(vm_page_t m, int allocflags)
4418{
4419
4420 if ((allocflags & VM_ALLOC_NOBUSY) != 0) {
4421 if ((allocflags & VM_ALLOC_IGN_SBUSY) != 0)
4422 vm_page_sunbusy(m);
4423 else
4424 vm_page_xunbusy(m);
4425 }
4426}
4427
4428/*
4429 * vm_page_grab_sleep
4430 *
4431 * Sleep for busy according to VM_ALLOC_ parameters. Returns true
4432 * if the caller should retry and false otherwise.
4433 *
4434 * If the object is locked on entry the object will be unlocked with
4435 * false returns and still locked but possibly having been dropped
4436 * with true returns.
4437 */
4438static bool
4439vm_page_grab_sleep(vm_object_t object, vm_page_t m, vm_pindex_t pindex,
4440 const char *wmesg, int allocflags, bool locked)
4441{
4442
4443 if ((allocflags & VM_ALLOC_NOWAIT) != 0)
4444 return (false);
4445
4446 /*
4447 * Reference the page before unlocking and sleeping so that
4448 * the page daemon is less likely to reclaim it.
4449 */
4450 if (locked && (allocflags & VM_ALLOC_NOCREAT) == 0)
4452
4453 if (_vm_page_busy_sleep(object, m, pindex, wmesg, allocflags, locked) &&
4454 locked)
4455 VM_OBJECT_WLOCK(object);
4456 if ((allocflags & VM_ALLOC_WAITFAIL) != 0)
4457 return (false);
4458
4459 return (true);
4460}
4461
4462/*
4463 * Assert that the grab flags are valid.
4464 */
4465static inline void
4466vm_page_grab_check(int allocflags)
4467{
4468
4469 KASSERT((allocflags & VM_ALLOC_NOBUSY) == 0 ||
4470 (allocflags & VM_ALLOC_WIRED) != 0,
4471 ("vm_page_grab*: the pages must be busied or wired"));
4472
4473 KASSERT((allocflags & VM_ALLOC_SBUSY) == 0 ||
4474 (allocflags & VM_ALLOC_IGN_SBUSY) != 0,
4475 ("vm_page_grab*: VM_ALLOC_SBUSY/VM_ALLOC_IGN_SBUSY mismatch"));
4476}
4477
4478/*
4479 * Calculate the page allocation flags for grab.
4480 */
4481static inline int
4482vm_page_grab_pflags(int allocflags)
4483{
4484 int pflags;
4485
4486 pflags = allocflags &
4489 if ((allocflags & VM_ALLOC_NOWAIT) == 0)
4490 pflags |= VM_ALLOC_WAITFAIL;
4491 if ((allocflags & VM_ALLOC_IGN_SBUSY) != 0)
4492 pflags |= VM_ALLOC_SBUSY;
4493
4494 return (pflags);
4495}
4496
4497/*
4498 * Grab a page, waiting until we are waken up due to the page
4499 * changing state. We keep on waiting, if the page continues
4500 * to be in the object. If the page doesn't exist, first allocate it
4501 * and then conditionally zero it.
4502 *
4503 * This routine may sleep.
4504 *
4505 * The object must be locked on entry. The lock will, however, be released
4506 * and reacquired if the routine sleeps.
4507 */
4508vm_page_t
4509vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags)
4510{
4511 vm_page_t m;
4512
4514 vm_page_grab_check(allocflags);
4515
4516retrylookup:
4517 if ((m = vm_page_lookup(object, pindex)) != NULL) {
4518 if (!vm_page_tryacquire(m, allocflags)) {
4519 if (vm_page_grab_sleep(object, m, pindex, "pgrbwt",
4520 allocflags, true))
4521 goto retrylookup;
4522 return (NULL);
4523 }
4524 goto out;
4525 }
4526 if ((allocflags & VM_ALLOC_NOCREAT) != 0)
4527 return (NULL);
4528 m = vm_page_alloc(object, pindex, vm_page_grab_pflags(allocflags));
4529 if (m == NULL) {
4530 if ((allocflags & (VM_ALLOC_NOWAIT | VM_ALLOC_WAITFAIL)) != 0)
4531 return (NULL);
4532 goto retrylookup;
4533 }
4534 if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0)
4535 pmap_zero_page(m);
4536
4537out:
4538 vm_page_grab_release(m, allocflags);
4539
4540 return (m);
4541}
4542
4543/*
4544 * Locklessly attempt to acquire a page given a (object, pindex) tuple
4545 * and an optional previous page to avoid the radix lookup. The resulting
4546 * page will be validated against the identity tuple and busied or wired
4547 * as requested. A NULL *mp return guarantees that the page was not in
4548 * radix at the time of the call but callers must perform higher level
4549 * synchronization or retry the operation under a lock if they require
4550 * an atomic answer. This is the only lock free validation routine,
4551 * other routines can depend on the resulting page state.
4552 *
4553 * The return value indicates whether the operation failed due to caller
4554 * flags. The return is tri-state with mp:
4555 *
4556 * (true, *mp != NULL) - The operation was successful.
4557 * (true, *mp == NULL) - The page was not found in tree.
4558 * (false, *mp == NULL) - WAITFAIL or NOWAIT prevented acquisition.
4559 */
4560static bool
4561vm_page_acquire_unlocked(vm_object_t object, vm_pindex_t pindex,
4562 vm_page_t prev, vm_page_t *mp, int allocflags)
4563{
4564 vm_page_t m;
4565
4566 vm_page_grab_check(allocflags);
4567 MPASS(prev == NULL || vm_page_busied(prev) || vm_page_wired(prev));
4568
4569 *mp = NULL;
4570 for (;;) {
4571 /*
4572 * We may see a false NULL here because the previous page
4573 * has been removed or just inserted and the list is loaded
4574 * without barriers. Switch to radix to verify.
4575 */
4576 if (prev == NULL || (m = TAILQ_NEXT(prev, listq)) == NULL ||
4577 QMD_IS_TRASHED(m) || m->pindex != pindex ||
4578 atomic_load_ptr(&m->object) != object) {
4579 prev = NULL;
4580 /*
4581 * This guarantees the result is instantaneously
4582 * correct.
4583 */
4584 m = vm_radix_lookup_unlocked(&object->rtree, pindex);
4585 }
4586 if (m == NULL)
4587 return (true);
4588 if (vm_page_trybusy(m, allocflags)) {
4589 if (m->object == object && m->pindex == pindex)
4590 break;
4591 /* relookup. */
4593 cpu_spinwait();
4594 continue;
4595 }
4596 if (!vm_page_grab_sleep(object, m, pindex, "pgnslp",
4597 allocflags, false))
4598 return (false);
4599 }
4600 if ((allocflags & VM_ALLOC_WIRED) != 0)
4601 vm_page_wire(m);
4602 vm_page_grab_release(m, allocflags);
4603 *mp = m;
4604 return (true);
4605}
4606
4607/*
4608 * Try to locklessly grab a page and fall back to the object lock if NOCREAT
4609 * is not set.
4610 */
4611vm_page_t
4612vm_page_grab_unlocked(vm_object_t object, vm_pindex_t pindex, int allocflags)
4613{
4614 vm_page_t m;
4615
4616 vm_page_grab_check(allocflags);
4617
4618 if (!vm_page_acquire_unlocked(object, pindex, NULL, &m, allocflags))
4619 return (NULL);
4620 if (m != NULL)
4621 return (m);
4622
4623 /*
4624 * The radix lockless lookup should never return a false negative
4625 * errors. If the user specifies NOCREAT they are guaranteed there
4626 * was no page present at the instant of the call. A NOCREAT caller
4627 * must handle create races gracefully.
4628 */
4629 if ((allocflags & VM_ALLOC_NOCREAT) != 0)
4630 return (NULL);
4631
4632 VM_OBJECT_WLOCK(object);
4633 m = vm_page_grab(object, pindex, allocflags);
4634 VM_OBJECT_WUNLOCK(object);
4635
4636 return (m);
4637}
4638
4639/*
4640 * Grab a page and make it valid, paging in if necessary. Pages missing from
4641 * their pager are zero filled and validated. If a VM_ALLOC_COUNT is supplied
4642 * and the page is not valid as many as VM_INITIAL_PAGEIN pages can be brought
4643 * in simultaneously. Additional pages will be left on a paging queue but
4644 * will neither be wired nor busy regardless of allocflags.
4645 */
4646int
4647vm_page_grab_valid(vm_page_t *mp, vm_object_t object, vm_pindex_t pindex, int allocflags)
4648{
4649 vm_page_t m;
4650 vm_page_t ma[VM_INITIAL_PAGEIN];
4651 int after, i, pflags, rv;
4652
4653 KASSERT((allocflags & VM_ALLOC_SBUSY) == 0 ||
4654 (allocflags & VM_ALLOC_IGN_SBUSY) != 0,
4655 ("vm_page_grab_valid: VM_ALLOC_SBUSY/VM_ALLOC_IGN_SBUSY mismatch"));
4656 KASSERT((allocflags &
4658 ("vm_page_grab_valid: Invalid flags 0x%X", allocflags));
4660 pflags = allocflags & ~(VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY |
4662 pflags |= VM_ALLOC_WAITFAIL;
4663
4664retrylookup:
4665 if ((m = vm_page_lookup(object, pindex)) != NULL) {
4666 /*
4667 * If the page is fully valid it can only become invalid
4668 * with the object lock held. If it is not valid it can
4669 * become valid with the busy lock held. Therefore, we
4670 * may unnecessarily lock the exclusive busy here if we
4671 * race with I/O completion not using the object lock.
4672 * However, we will not end up with an invalid page and a
4673 * shared lock.
4674 */
4675 if (!vm_page_trybusy(m,
4676 vm_page_all_valid(m) ? allocflags : 0)) {
4677 (void)vm_page_grab_sleep(object, m, pindex, "pgrbwt",
4678 allocflags, true);
4679 goto retrylookup;
4680 }
4681 if (vm_page_all_valid(m))
4682 goto out;
4683 if ((allocflags & VM_ALLOC_NOCREAT) != 0) {
4685 *mp = NULL;
4686 return (VM_PAGER_FAIL);
4687 }
4688 } else if ((allocflags & VM_ALLOC_NOCREAT) != 0) {
4689 *mp = NULL;
4690 return (VM_PAGER_FAIL);
4691 } else if ((m = vm_page_alloc(object, pindex, pflags)) == NULL) {
4692 goto retrylookup;
4693 }
4694
4696 if (vm_pager_has_page(object, pindex, NULL, &after)) {
4697 after = MIN(after, VM_INITIAL_PAGEIN);
4698 after = MIN(after, allocflags >> VM_ALLOC_COUNT_SHIFT);
4699 after = MAX(after, 1);
4700 ma[0] = m;
4701 for (i = 1; i < after; i++) {
4702 if ((ma[i] = vm_page_next(ma[i - 1])) != NULL) {
4703 if (ma[i]->valid || !vm_page_tryxbusy(ma[i]))
4704 break;
4705 } else {
4706 ma[i] = vm_page_alloc(object, m->pindex + i,
4708 if (ma[i] == NULL)
4709 break;
4710 }
4711 }
4712 after = i;
4713 vm_object_pip_add(object, after);
4714 VM_OBJECT_WUNLOCK(object);
4715 rv = vm_pager_get_pages(object, ma, after, NULL, NULL);
4716 VM_OBJECT_WLOCK(object);
4717 vm_object_pip_wakeupn(object, after);
4718 /* Pager may have replaced a page. */
4719 m = ma[0];
4720 if (rv != VM_PAGER_OK) {
4721 for (i = 0; i < after; i++) {
4722 if (!vm_page_wired(ma[i]))
4723 vm_page_free(ma[i]);
4724 else
4725 vm_page_xunbusy(ma[i]);
4726 }
4727 *mp = NULL;
4728 return (rv);
4729 }
4730 for (i = 1; i < after; i++)
4732 MPASS(vm_page_all_valid(m));
4733 } else {
4734 vm_page_zero_invalid(m, TRUE);
4735 }
4736out:
4737 if ((allocflags & VM_ALLOC_WIRED) != 0)
4738 vm_page_wire(m);
4739 if ((allocflags & VM_ALLOC_SBUSY) != 0 && vm_page_xbusied(m))
4741 else if ((allocflags & VM_ALLOC_NOBUSY) != 0)
4743 *mp = m;
4744 return (VM_PAGER_OK);
4745}
4746
4747/*
4748 * Locklessly grab a valid page. If the page is not valid or not yet
4749 * allocated this will fall back to the object lock method.
4750 */
4751int
4753 vm_pindex_t pindex, int allocflags)
4754{
4755 vm_page_t m;
4756 int flags;
4757 int error;
4758
4759 KASSERT((allocflags & VM_ALLOC_SBUSY) == 0 ||
4760 (allocflags & VM_ALLOC_IGN_SBUSY) != 0,
4761 ("vm_page_grab_valid_unlocked: VM_ALLOC_SBUSY/VM_ALLOC_IGN_SBUSY "
4762 "mismatch"));
4763 KASSERT((allocflags &
4765 ("vm_page_grab_valid_unlocked: Invalid flags 0x%X", allocflags));
4766
4767 /*
4768 * Attempt a lockless lookup and busy. We need at least an sbusy
4769 * before we can inspect the valid field and return a wired page.
4770 */
4771 flags = allocflags & ~(VM_ALLOC_NOBUSY | VM_ALLOC_WIRED);
4772 if (!vm_page_acquire_unlocked(object, pindex, NULL, mp, flags))
4773 return (VM_PAGER_FAIL);
4774 if ((m = *mp) != NULL) {
4775 if (vm_page_all_valid(m)) {
4776 if ((allocflags & VM_ALLOC_WIRED) != 0)
4777 vm_page_wire(m);
4778 vm_page_grab_release(m, allocflags);
4779 return (VM_PAGER_OK);
4780 }
4782 }
4783 if ((allocflags & VM_ALLOC_NOCREAT) != 0) {
4784 *mp = NULL;
4785 return (VM_PAGER_FAIL);
4786 }
4787 VM_OBJECT_WLOCK(object);
4788 error = vm_page_grab_valid(mp, object, pindex, allocflags);
4789 VM_OBJECT_WUNLOCK(object);
4790
4791 return (error);
4792}
4793
4794/*
4795 * Return the specified range of pages from the given object. For each
4796 * page offset within the range, if a page already exists within the object
4797 * at that offset and it is busy, then wait for it to change state. If,
4798 * instead, the page doesn't exist, then allocate it.
4799 *
4800 * The caller must always specify an allocation class.
4801 *
4802 * allocation classes:
4803 * VM_ALLOC_NORMAL normal process request
4804 * VM_ALLOC_SYSTEM system *really* needs the pages
4805 *
4806 * The caller must always specify that the pages are to be busied and/or
4807 * wired.
4808 *
4809 * optional allocation flags:
4810 * VM_ALLOC_IGN_SBUSY do not sleep on soft busy pages
4811 * VM_ALLOC_NOBUSY do not exclusive busy the page
4812 * VM_ALLOC_NOWAIT do not sleep
4813 * VM_ALLOC_SBUSY set page to sbusy state
4814 * VM_ALLOC_WIRED wire the pages
4815 * VM_ALLOC_ZERO zero and validate any invalid pages
4816 *
4817 * If VM_ALLOC_NOWAIT is not specified, this routine may sleep. Otherwise, it
4818 * may return a partial prefix of the requested range.
4819 */
4820int
4821vm_page_grab_pages(vm_object_t object, vm_pindex_t pindex, int allocflags,
4822 vm_page_t *ma, int count)
4823{
4824 vm_page_t m, mpred;
4825 int pflags;
4826 int i;
4827
4829 KASSERT(((u_int)allocflags >> VM_ALLOC_COUNT_SHIFT) == 0,
4830 ("vm_page_grap_pages: VM_ALLOC_COUNT() is not allowed"));
4831 KASSERT(count > 0,
4832 ("vm_page_grab_pages: invalid page count %d", count));
4833 vm_page_grab_check(allocflags);
4834
4835 pflags = vm_page_grab_pflags(allocflags);
4836 i = 0;
4837retrylookup:
4838 m = vm_radix_lookup_le(&object->rtree, pindex + i);
4839 if (m == NULL || m->pindex != pindex + i) {
4840 mpred = m;
4841 m = NULL;
4842 } else
4843 mpred = TAILQ_PREV(m, pglist, listq);
4844 for (; i < count; i++) {
4845 if (m != NULL) {
4846 if (!vm_page_tryacquire(m, allocflags)) {
4847 if (vm_page_grab_sleep(object, m, pindex + i,
4848 "grbmaw", allocflags, true))
4849 goto retrylookup;
4850 break;
4851 }
4852 } else {
4853 if ((allocflags & VM_ALLOC_NOCREAT) != 0)
4854 break;
4855 m = vm_page_alloc_after(object, pindex + i,
4856 pflags | VM_ALLOC_COUNT(count - i), mpred);
4857 if (m == NULL) {
4858 if ((allocflags & (VM_ALLOC_NOWAIT |
4859 VM_ALLOC_WAITFAIL)) != 0)
4860 break;
4861 goto retrylookup;
4862 }
4863 }
4864 if (vm_page_none_valid(m) &&
4865 (allocflags & VM_ALLOC_ZERO) != 0) {
4866 if ((m->flags & PG_ZERO) == 0)
4867 pmap_zero_page(m);
4868 vm_page_valid(m);
4869 }
4870 vm_page_grab_release(m, allocflags);
4871 ma[i] = mpred = m;
4872 m = vm_page_next(m);
4873 }
4874 return (i);
4875}
4876
4877/*
4878 * Unlocked variant of vm_page_grab_pages(). This accepts the same flags
4879 * and will fall back to the locked variant to handle allocation.
4880 */
4881int
4882vm_page_grab_pages_unlocked(vm_object_t object, vm_pindex_t pindex,
4883 int allocflags, vm_page_t *ma, int count)
4884{
4885 vm_page_t m, pred;
4886 int flags;
4887 int i;
4888
4889 KASSERT(count > 0,
4890 ("vm_page_grab_pages_unlocked: invalid page count %d", count));
4891 vm_page_grab_check(allocflags);
4892
4893 /*
4894 * Modify flags for lockless acquire to hold the page until we
4895 * set it valid if necessary.
4896 */
4897 flags = allocflags & ~VM_ALLOC_NOBUSY;
4898 pred = NULL;
4899 for (i = 0; i < count; i++, pindex++) {
4900 if (!vm_page_acquire_unlocked(object, pindex, pred, &m, flags))
4901 return (i);
4902 if (m == NULL)
4903 break;
4904 if ((flags & VM_ALLOC_ZERO) != 0 && vm_page_none_valid(m)) {
4905 if ((m->flags & PG_ZERO) == 0)
4906 pmap_zero_page(m);
4907 vm_page_valid(m);
4908 }
4909 /* m will still be wired or busy according to flags. */
4910 vm_page_grab_release(m, allocflags);
4911 pred = ma[i] = m;
4912 }
4913 if (i == count || (allocflags & VM_ALLOC_NOCREAT) != 0)
4914 return (i);
4915 count -= i;
4916 VM_OBJECT_WLOCK(object);
4917 i += vm_page_grab_pages(object, pindex, allocflags, &ma[i], count);
4918 VM_OBJECT_WUNLOCK(object);
4919
4920 return (i);
4921}
4922
4923/*
4924 * Mapping function for valid or dirty bits in a page.
4925 *
4926 * Inputs are required to range within a page.
4927 */
4928vm_page_bits_t
4929vm_page_bits(int base, int size)
4930{
4931 int first_bit;
4932 int last_bit;
4933
4934 KASSERT(
4935 base + size <= PAGE_SIZE,
4936 ("vm_page_bits: illegal base/size %d/%d", base, size)
4937 );
4938
4939 if (size == 0) /* handle degenerate case */
4940 return (0);
4941
4942 first_bit = base >> DEV_BSHIFT;
4943 last_bit = (base + size - 1) >> DEV_BSHIFT;
4944
4945 return (((vm_page_bits_t)2 << last_bit) -
4946 ((vm_page_bits_t)1 << first_bit));
4947}
4948
4949void
4950vm_page_bits_set(vm_page_t m, vm_page_bits_t *bits, vm_page_bits_t set)
4951{
4952
4953#if PAGE_SIZE == 32768
4954 atomic_set_64((uint64_t *)bits, set);
4955#elif PAGE_SIZE == 16384
4956 atomic_set_32((uint32_t *)bits, set);
4957#elif (PAGE_SIZE == 8192) && defined(atomic_set_16)
4958 atomic_set_16((uint16_t *)bits, set);
4959#elif (PAGE_SIZE == 4096) && defined(atomic_set_8)
4960 atomic_set_8((uint8_t *)bits, set);
4961#else /* PAGE_SIZE <= 8192 */
4962 uintptr_t addr;
4963 int shift;
4964
4965 addr = (uintptr_t)bits;
4966 /*
4967 * Use a trick to perform a 32-bit atomic on the
4968 * containing aligned word, to not depend on the existence
4969 * of atomic_{set, clear}_{8, 16}.
4970 */
4971 shift = addr & (sizeof(uint32_t) - 1);
4972#if BYTE_ORDER == BIG_ENDIAN
4973 shift = (sizeof(uint32_t) - sizeof(vm_page_bits_t) - shift) * NBBY;
4974#else
4975 shift *= NBBY;
4976#endif
4977 addr &= ~(sizeof(uint32_t) - 1);
4978 atomic_set_32((uint32_t *)addr, set << shift);
4979#endif /* PAGE_SIZE */
4980}
4981
4982static inline void
4983vm_page_bits_clear(vm_page_t m, vm_page_bits_t *bits, vm_page_bits_t clear)
4984{
4985
4986#if PAGE_SIZE == 32768
4987 atomic_clear_64((uint64_t *)bits, clear);
4988#elif PAGE_SIZE == 16384
4989 atomic_clear_32((uint32_t *)bits, clear);
4990#elif (PAGE_SIZE == 8192) && defined(atomic_clear_16)
4991 atomic_clear_16((uint16_t *)bits, clear);
4992#elif (PAGE_SIZE == 4096) && defined(atomic_clear_8)
4993 atomic_clear_8((uint8_t *)bits, clear);
4994#else /* PAGE_SIZE <= 8192 */
4995 uintptr_t addr;
4996 int shift;
4997
4998 addr = (uintptr_t)bits;
4999 /*
5000 * Use a trick to perform a 32-bit atomic on the
5001 * containing aligned word, to not depend on the existence
5002 * of atomic_{set, clear}_{8, 16}.
5003 */
5004 shift = addr & (sizeof(uint32_t) - 1);
5005#if BYTE_ORDER == BIG_ENDIAN
5006 shift = (sizeof(uint32_t) - sizeof(vm_page_bits_t) - shift) * NBBY;
5007#else
5008 shift *= NBBY;
5009#endif
5010 addr &= ~(sizeof(uint32_t) - 1);
5011 atomic_clear_32((uint32_t *)addr, clear << shift);
5012#endif /* PAGE_SIZE */
5013}
5014
5015static inline vm_page_bits_t
5016vm_page_bits_swap(vm_page_t m, vm_page_bits_t *bits, vm_page_bits_t newbits)
5017{
5018#if PAGE_SIZE == 32768
5019 uint64_t old;
5020
5021 old = *bits;
5022 while (atomic_fcmpset_64(bits, &old, newbits) == 0);
5023 return (old);
5024#elif PAGE_SIZE == 16384
5025 uint32_t old;
5026
5027 old = *bits;
5028 while (atomic_fcmpset_32(bits, &old, newbits) == 0);
5029 return (old);
5030#elif (PAGE_SIZE == 8192) && defined(atomic_fcmpset_16)
5031 uint16_t old;
5032
5033 old = *bits;
5034 while (atomic_fcmpset_16(bits, &old, newbits) == 0);
5035 return (old);
5036#elif (PAGE_SIZE == 4096) && defined(atomic_fcmpset_8)
5037 uint8_t old;
5038
5039 old = *bits;
5040 while (atomic_fcmpset_8(bits, &old, newbits) == 0);
5041 return (old);
5042#else /* PAGE_SIZE <= 4096*/
5043 uintptr_t addr;
5044 uint32_t old, new, mask;
5045 int shift;
5046
5047 addr = (uintptr_t)bits;
5048 /*
5049 * Use a trick to perform a 32-bit atomic on the
5050 * containing aligned word, to not depend on the existence
5051 * of atomic_{set, swap, clear}_{8, 16}.
5052 */
5053 shift = addr & (sizeof(uint32_t) - 1);
5054#if BYTE_ORDER == BIG_ENDIAN
5055 shift = (sizeof(uint32_t) - sizeof(vm_page_bits_t) - shift) * NBBY;
5056#else
5057 shift *= NBBY;
5058#endif
5059 addr &= ~(sizeof(uint32_t) - 1);
5060 mask = VM_PAGE_BITS_ALL << shift;
5061
5062 old = *bits;
5063 do {
5064 new = old & ~mask;
5065 new |= newbits << shift;
5066 } while (atomic_fcmpset_32((uint32_t *)addr, &old, new) == 0);
5067 return (old >> shift);
5068#endif /* PAGE_SIZE */
5069}
5070
5071/*
5072 * vm_page_set_valid_range:
5073 *
5074 * Sets portions of a page valid. The arguments are expected
5075 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
5076 * of any partial chunks touched by the range. The invalid portion of
5077 * such chunks will be zeroed.
5078 *
5079 * (base + size) must be less then or equal to PAGE_SIZE.
5080 */
5081void
5082vm_page_set_valid_range(vm_page_t m, int base, int size)
5083{
5084 int endoff, frag;
5085 vm_page_bits_t pagebits;
5086
5088 if (size == 0) /* handle degenerate case */
5089 return;
5090
5091 /*
5092 * If the base is not DEV_BSIZE aligned and the valid
5093 * bit is clear, we have to zero out a portion of the
5094 * first block.
5095 */
5096 if ((frag = rounddown2(base, DEV_BSIZE)) != base &&
5097 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0)
5098 pmap_zero_page_area(m, frag, base - frag);
5099
5100 /*
5101 * If the ending offset is not DEV_BSIZE aligned and the
5102 * valid bit is clear, we have to zero out a portion of
5103 * the last block.
5104 */
5105 endoff = base + size;
5106 if ((frag = rounddown2(endoff, DEV_BSIZE)) != endoff &&
5107 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0)
5108 pmap_zero_page_area(m, endoff,
5109 DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
5110
5111 /*
5112 * Assert that no previously invalid block that is now being validated
5113 * is already dirty.
5114 */
5115 KASSERT((~m->valid & vm_page_bits(base, size) & m->dirty) == 0,
5116 ("vm_page_set_valid_range: page %p is dirty", m));
5117
5118 /*
5119 * Set valid bits inclusive of any overlap.
5120 */
5121 pagebits = vm_page_bits(base, size);
5122 if (vm_page_xbusied(m))
5123 m->valid |= pagebits;
5124 else
5125 vm_page_bits_set(m, &m->valid, pagebits);
5126}
5127
5128/*
5129 * Set the page dirty bits and free the invalid swap space if
5130 * present. Returns the previous dirty bits.
5131 */
5132vm_page_bits_t
5134{
5135 vm_page_bits_t old;
5136
5138
5139 if (vm_page_xbusied(m) && !pmap_page_is_write_mapped(m)) {
5140 old = m->dirty;
5141 m->dirty = VM_PAGE_BITS_ALL;
5142 } else
5143 old = vm_page_bits_swap(m, &m->dirty, VM_PAGE_BITS_ALL);
5144 if (old == 0 && (m->a.flags & PGA_SWAP_SPACE) != 0)
5146
5147 return (old);
5148}
5149
5150/*
5151 * Clear the given bits from the specified page's dirty field.
5152 */
5153static __inline void
5154vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits)
5155{
5156
5158
5159 /*
5160 * If the page is xbusied and not write mapped we are the
5161 * only thread that can modify dirty bits. Otherwise, The pmap
5162 * layer can call vm_page_dirty() without holding a distinguished
5163 * lock. The combination of page busy and atomic operations
5164 * suffice to guarantee consistency of the page dirty field.
5165 */
5166 if (vm_page_xbusied(m) && !pmap_page_is_write_mapped(m))
5167 m->dirty &= ~pagebits;
5168 else
5169 vm_page_bits_clear(m, &m->dirty, pagebits);
5170}
5171
5172/*
5173 * vm_page_set_validclean:
5174 *
5175 * Sets portions of a page valid and clean. The arguments are expected
5176 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
5177 * of any partial chunks touched by the range. The invalid portion of
5178 * such chunks will be zero'd.
5179 *
5180 * (base + size) must be less then or equal to PAGE_SIZE.
5181 */
5182void
5183vm_page_set_validclean(vm_page_t m, int base, int size)
5184{
5185 vm_page_bits_t oldvalid, pagebits;
5186 int endoff, frag;
5187
5189 if (size == 0) /* handle degenerate case */
5190 return;
5191
5192 /*
5193 * If the base is not DEV_BSIZE aligned and the valid
5194 * bit is clear, we have to zero out a portion of the
5195 * first block.
5196 */
5197 if ((frag = rounddown2(base, DEV_BSIZE)) != base &&
5198 (m->valid & ((vm_page_bits_t)1 << (base >> DEV_BSHIFT))) == 0)
5199 pmap_zero_page_area(m, frag, base - frag);
5200
5201 /*
5202 * If the ending offset is not DEV_BSIZE aligned and the
5203 * valid bit is clear, we have to zero out a portion of
5204 * the last block.
5205 */
5206 endoff = base + size;
5207 if ((frag = rounddown2(endoff, DEV_BSIZE)) != endoff &&
5208 (m->valid & ((vm_page_bits_t)1 << (endoff >> DEV_BSHIFT))) == 0)
5209 pmap_zero_page_area(m, endoff,
5210 DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
5211
5212 /*
5213 * Set valid, clear dirty bits. If validating the entire
5214 * page we can safely clear the pmap modify bit. We also
5215 * use this opportunity to clear the PGA_NOSYNC flag. If a process
5216 * takes a write fault on a MAP_NOSYNC memory area the flag will
5217 * be set again.
5218 *
5219 * We set valid bits inclusive of any overlap, but we can only
5220 * clear dirty bits for DEV_BSIZE chunks that are fully within
5221 * the range.
5222 */
5223 oldvalid = m->valid;
5224 pagebits = vm_page_bits(base, size);
5225 if (vm_page_xbusied(m))
5226 m->valid |= pagebits;
5227 else
5228 vm_page_bits_set(m, &m->valid, pagebits);
5229#if 0 /* NOT YET */
5230 if ((frag = base & (DEV_BSIZE - 1)) != 0) {
5231 frag = DEV_BSIZE - frag;
5232 base += frag;
5233 size -= frag;
5234 if (size < 0)
5235 size = 0;
5236 }
5237 pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1));
5238#endif
5239 if (base == 0 && size == PAGE_SIZE) {
5240 /*
5241 * The page can only be modified within the pmap if it is
5242 * mapped, and it can only be mapped if it was previously
5243 * fully valid.
5244 */
5245 if (oldvalid == VM_PAGE_BITS_ALL)
5246 /*
5247 * Perform the pmap_clear_modify() first. Otherwise,
5248 * a concurrent pmap operation, such as
5249 * pmap_protect(), could clear a modification in the
5250 * pmap and set the dirty field on the page before
5251 * pmap_clear_modify() had begun and after the dirty
5252 * field was cleared here.
5253 */
5255 m->dirty = 0;
5257 } else if (oldvalid != VM_PAGE_BITS_ALL && vm_page_xbusied(m))
5258 m->dirty &= ~pagebits;
5259 else
5260 vm_page_clear_dirty_mask(m, pagebits);
5261}
5262
5263void
5264vm_page_clear_dirty(vm_page_t m, int base, int size)
5265{
5266
5267 vm_page_clear_dirty_mask(m, vm_page_bits(base, size));
5268}
5269
5270/*
5271 * vm_page_set_invalid:
5272 *
5273 * Invalidates DEV_BSIZE'd chunks within a page. Both the
5274 * valid and dirty bits for the effected areas are cleared.
5275 */
5276void
5277vm_page_set_invalid(vm_page_t m, int base, int size)
5278{
5279 vm_page_bits_t bits;
5280 vm_object_t object;
5281
5282 /*
5283 * The object lock is required so that pages can't be mapped
5284 * read-only while we're in the process of invalidating them.
5285 */
5286 object = m->object;
5289
5290 if (object->type == OBJT_VNODE && base == 0 && IDX_TO_OFF(m->pindex) +
5291 size >= object->un_pager.vnp.vnp_size)
5292 bits = VM_PAGE_BITS_ALL;
5293 else
5294 bits = vm_page_bits(base, size);
5295 if (object->ref_count != 0 && vm_page_all_valid(m) && bits != 0)
5296 pmap_remove_all(m);
5297 KASSERT((bits == 0 && vm_page_all_valid(m)) ||
5298 !pmap_page_is_mapped(m),
5299 ("vm_page_set_invalid: page %p is mapped", m));
5300 if (vm_page_xbusied(m)) {
5301 m->valid &= ~bits;
5302 m->dirty &= ~bits;
5303 } else {
5304 vm_page_bits_clear(m, &m->valid, bits);
5305 vm_page_bits_clear(m, &m->dirty, bits);
5306 }
5307}
5308
5309/*
5310 * vm_page_invalid:
5311 *
5312 * Invalidates the entire page. The page must be busy, unmapped, and
5313 * the enclosing object must be locked. The object locks protects
5314 * against concurrent read-only pmap enter which is done without
5315 * busy.
5316 */
5317void
5319{
5320
5322 VM_OBJECT_ASSERT_WLOCKED(m->object);
5323 MPASS(!pmap_page_is_mapped(m));
5324
5325 if (vm_page_xbusied(m))
5326 m->valid = 0;
5327 else
5328 vm_page_bits_clear(m, &m->valid, VM_PAGE_BITS_ALL);
5329}
5330
5331/*
5332 * vm_page_zero_invalid()
5333 *
5334 * The kernel assumes that the invalid portions of a page contain
5335 * garbage, but such pages can be mapped into memory by user code.
5336 * When this occurs, we must zero out the non-valid portions of the
5337 * page so user code sees what it expects.
5338 *
5339 * Pages are most often semi-valid when the end of a file is mapped
5340 * into memory and the file's size is not page aligned.
5341 */
5342void
5343vm_page_zero_invalid(vm_page_t m, boolean_t setvalid)
5344{
5345 int b;
5346 int i;
5347
5348 /*
5349 * Scan the valid bits looking for invalid sections that
5350 * must be zeroed. Invalid sub-DEV_BSIZE'd areas ( where the
5351 * valid bit may be set ) have already been zeroed by
5352 * vm_page_set_validclean().
5353 */
5354 for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) {
5355 if (i == (PAGE_SIZE / DEV_BSIZE) ||
5356 (m->valid & ((vm_page_bits_t)1 << i))) {
5357 if (i > b) {
5359 b << DEV_BSHIFT, (i - b) << DEV_BSHIFT);
5360 }
5361 b = i + 1;
5362 }
5363 }
5364
5365 /*
5366 * setvalid is TRUE when we can safely set the zero'd areas
5367 * as being valid. We can do this if there are no cache consistancy
5368 * issues. e.g. it is ok to do with UFS, but not ok to do with NFS.
5369 */
5370 if (setvalid)
5371 vm_page_valid(m);
5372}
5373
5374/*
5375 * vm_page_is_valid:
5376 *
5377 * Is (partial) page valid? Note that the case where size == 0
5378 * will return FALSE in the degenerate case where the page is
5379 * entirely invalid, and TRUE otherwise.
5380 *
5381 * Some callers envoke this routine without the busy lock held and
5382 * handle races via higher level locks. Typical callers should
5383 * hold a busy lock to prevent invalidation.
5384 */
5385int
5386vm_page_is_valid(vm_page_t m, int base, int size)
5387{
5388 vm_page_bits_t bits;
5389
5390 bits = vm_page_bits(base, size);
5391 return (m->valid != 0 && (m->valid & bits) == bits);
5392}
5393
5394/*
5395 * Returns true if all of the specified predicates are true for the entire
5396 * (super)page and false otherwise.
5397 */
5398bool
5399vm_page_ps_test(vm_page_t m, int flags, vm_page_t skip_m)
5400{
5401 vm_object_t object;
5402 int i, npages;
5403
5404 object = m->object;
5405 if (skip_m != NULL && skip_m->object != object)
5406 return (false);
5408 npages = atop(pagesizes[m->psind]);
5409
5410 /*
5411 * The physically contiguous pages that make up a superpage, i.e., a
5412 * page with a page size index ("psind") greater than zero, will
5413 * occupy adjacent entries in vm_page_array[].
5414 */
5415 for (i = 0; i < npages; i++) {
5416 /* Always test object consistency, including "skip_m". */
5417 if (m[i].object != object)
5418 return (false);
5419 if (&m[i] == skip_m)
5420 continue;
5421 if ((flags & PS_NONE_BUSY) != 0 && vm_page_busied(&m[i]))
5422 return (false);
5423 if ((flags & PS_ALL_DIRTY) != 0) {
5424 /*
5425 * Calling vm_page_test_dirty() or pmap_is_modified()
5426 * might stop this case from spuriously returning
5427 * "false". However, that would require a write lock
5428 * on the object containing "m[i]".
5429 */
5430 if (m[i].dirty != VM_PAGE_BITS_ALL)
5431 return (false);
5432 }
5433 if ((flags & PS_ALL_VALID) != 0 &&
5434 m[i].valid != VM_PAGE_BITS_ALL)
5435 return (false);
5436 }
5437 return (true);
5438}
5439
5440/*
5441 * Set the page's dirty bits if the page is modified.
5442 */
5443void
5445{
5446
5448 if (m->dirty != VM_PAGE_BITS_ALL && pmap_is_modified(m))
5449 vm_page_dirty(m);
5450}
5451
5452void
5453vm_page_valid(vm_page_t m)
5454{
5455
5457 if (vm_page_xbusied(m))
5458 m->valid = VM_PAGE_BITS_ALL;
5459 else
5460 vm_page_bits_set(m, &m->valid, VM_PAGE_BITS_ALL);
5461}
5462
5463void
5464vm_page_lock_KBI(vm_page_t m, const char *file, int line)
5465{
5466
5467 mtx_lock_flags_(vm_page_lockptr(m), 0, file, line);
5468}
5469
5470void
5471vm_page_unlock_KBI(vm_page_t m, const char *file, int line)
5472{
5473
5474 mtx_unlock_flags_(vm_page_lockptr(m), 0, file, line);
5475}
5476
5477int
5478vm_page_trylock_KBI(vm_page_t m, const char *file, int line)
5479{
5480
5481 return (mtx_trylock_flags_(vm_page_lockptr(m), 0, file, line));
5482}
5483
5484#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
5485void
5486vm_page_assert_locked_KBI(vm_page_t m, const char *file, int line)
5487{
5488
5489 vm_page_lock_assert_KBI(m, MA_OWNED, file, line);
5490}
5491
5492void
5493vm_page_lock_assert_KBI(vm_page_t m, int a, const char *file, int line)
5494{
5495
5496 mtx_assert_(vm_page_lockptr(m), a, file, line);
5497}
5498#endif
5499
5500#ifdef INVARIANTS
5501void
5502vm_page_object_busy_assert(vm_page_t m)
5503{
5504
5505 /*
5506 * Certain of the page's fields may only be modified by the
5507 * holder of a page or object busy.
5508 */
5509 if (m->object != NULL && !vm_page_busied(m))
5510 VM_OBJECT_ASSERT_BUSY(m->object);
5511}
5512
5513void
5514vm_page_assert_pga_writeable(vm_page_t m, uint16_t bits)
5515{
5516
5517 if ((bits & PGA_WRITEABLE) == 0)
5518 return;
5519
5520 /*
5521 * The PGA_WRITEABLE flag can only be set if the page is
5522 * managed, is exclusively busied or the object is locked.
5523 * Currently, this flag is only set by pmap_enter().
5524 */
5525 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5526 ("PGA_WRITEABLE on unmanaged page"));
5527 if (!vm_page_xbusied(m))
5528 VM_OBJECT_ASSERT_BUSY(m->object);
5529}
5530#endif
5531
5532#include "opt_ddb.h"
5533#ifdef DDB
5534#include <sys/kernel.h>
5535
5536#include <ddb/ddb.h>
5537
5538DB_SHOW_COMMAND(page, vm_page_print_page_info)
5539{
5540
5541 db_printf("vm_cnt.v_free_count: %d\n", vm_free_count());
5542 db_printf("vm_cnt.v_inactive_count: %d\n", vm_inactive_count());
5543 db_printf("vm_cnt.v_active_count: %d\n", vm_active_count());
5544 db_printf("vm_cnt.v_laundry_count: %d\n", vm_laundry_count());
5545 db_printf("vm_cnt.v_wire_count: %d\n", vm_wire_count());
5546 db_printf("vm_cnt.v_free_reserved: %d\n", vm_cnt.v_free_reserved);
5547 db_printf("vm_cnt.v_free_min: %d\n", vm_cnt.v_free_min);
5548 db_printf("vm_cnt.v_free_target: %d\n", vm_cnt.v_free_target);
5549 db_printf("vm_cnt.v_inactive_target: %d\n", vm_cnt.v_inactive_target);
5550}
5551
5552DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info)
5553{
5554 int dom;
5555
5556 db_printf("pq_free %d\n", vm_free_count());
5557 for (dom = 0; dom < vm_ndomains; dom++) {
5558 db_printf(
5559 "dom %d page_cnt %d free %d pq_act %d pq_inact %d pq_laund %d pq_unsw %d\n",
5560 dom,
5562 vm_dom[dom].vmd_free_count,
5567 }
5568}
5569
5570DB_SHOW_COMMAND(pginfo, vm_page_print_pginfo)
5571{
5572 vm_page_t m;
5573 boolean_t phys, virt;
5574
5575 if (!have_addr) {
5576 db_printf("show pginfo addr\n");
5577 return;
5578 }
5579
5580 phys = strchr(modif, 'p') != NULL;
5581 virt = strchr(modif, 'v') != NULL;
5582 if (virt)
5583 m = PHYS_TO_VM_PAGE(pmap_kextract(addr));
5584 else if (phys)
5585 m = PHYS_TO_VM_PAGE(addr);
5586 else
5587 m = (vm_page_t)addr;
5588 db_printf(
5589 "page %p obj %p pidx 0x%jx phys 0x%jx q %d ref 0x%x\n"
5590 " af 0x%x of 0x%x f 0x%x act %d busy %x valid 0x%x dirty 0x%x\n",
5591 m, m->object, (uintmax_t)m->pindex, (uintmax_t)m->phys_addr,
5592 m->a.queue, m->ref_count, m->a.flags, m->oflags,
5593 m->flags, m->a.act_count, m->busy_lock, m->valid, m->dirty);
5594}
5595#endif /* DDB */
struct vm_phys_seg vm_phys_segs[]
Definition: vm_phys.c:84
int vm_phys_nsegs
Definition: vm_phys.c:85
SYSCTL_PROC(_vm_memguard, OID_AUTO, desc, CTLTYPE_STRING|CTLFLAG_RW|CTLFLAG_MPSAFE, 0, 0, memguard_sysctl_desc, "A", "Short description of memory type to monitor")
void pmap_zero_page(vm_page_t)
void pmap_zero_page_area(vm_page_t, int off, int size)
void pmap_remove_all(vm_page_t m)
void pmap_remove_write(vm_page_t m)
void pmap_copy_page(vm_page_t, vm_page_t)
boolean_t pmap_is_modified(vm_page_t m)
void pmap_clear_modify(vm_page_t m)
vm_offset_t pmap_map(vm_offset_t *, vm_paddr_t, vm_paddr_t, int)
void pmap_page_init(vm_page_t m)
vm_page_t bq_pa[VM_BATCHQUEUE_SIZE]
Definition: vm_pagequeue.h:82
u_int vmd_free_min
Definition: vm_pagequeue.h:287
u_int vmd_page_count
Definition: vm_pagequeue.h:249
u_int vmd_domain
Definition: vm_pagequeue.h:248
u_int vmd_free_severe
Definition: vm_pagequeue.h:292
struct vm_domain::vm_pgcache vmd_pgcache[VM_NFREEPOOL]
struct vm_page vmd_clock[2]
Definition: vm_pagequeue.h:270
u_int vmd_free_reserved
Definition: vm_pagequeue.h:285
struct vm_page vmd_markers[PQ_COUNT]
Definition: vm_pagequeue.h:268
bool vmd_minset
Definition: vm_pagequeue.h:274
struct mtx_padalign vmd_free_mtx
Definition: vm_pagequeue.h:239
int vmd_pageout_pages_needed
Definition: vm_pagequeue.h:273
u_int vmd_pageout_free_min
Definition: vm_pagequeue.h:289
char vmd_name[sizeof(__XSTRING(MAXMEMDOM))]
Definition: vm_pagequeue.h:296
boolean_t vmd_oom
Definition: vm_pagequeue.h:257
bool vmd_severeset
Definition: vm_pagequeue.h:275
long vmd_segs
Definition: vm_pagequeue.h:250
struct vm_page vmd_inacthead
Definition: vm_pagequeue.h:269
u_int vmd_pageout_deficit
Definition: vm_pagequeue.h:252
u_int vmd_interrupt_free_min
Definition: vm_pagequeue.h:291
struct vm_pagequeue vmd_pagequeues[PQ_COUNT]
Definition: vm_pagequeue.h:238
struct mtx_padalign vmd_pageout_mtx
Definition: vm_pagequeue.h:240
objtype_t type
Definition: vm_object.h:114
off_t vnp_size
Definition: vm_object.h:132
struct domainset_ref domain
Definition: vm_object.h:108
struct vm_radix rtree
Definition: vm_object.h:106
volatile u_int ref_count
Definition: vm_object.h:111
struct vm_object::@0::@1 vnp
void * handle
Definition: vm_object.h:124
vm_memattr_t memattr
Definition: vm_object.h:113
u_short flags
Definition: vm_object.h:115
int resident_page_count
Definition: vm_object.h:119
union vm_object::@0 un_pager
struct pglist memq
Definition: vm_object.h:105
vm_pindex_t pindex
Definition: vm_page.h:242
vm_object_t object
Definition: vm_page.h:241
int8_t segind
Definition: vm_page.h:253
const char *const pq_name
Definition: vm_pagequeue.h:73
struct pglist pq_pl
Definition: vm_pagequeue.h:71
uint64_t pq_pdpages
Definition: vm_pagequeue.h:74
struct mtx pq_mutex
Definition: vm_pagequeue.h:70
uma_zone_t zone
Definition: vm_pagequeue.h:6
vm_paddr_t end
Definition: _vm_phys.h:58
vm_page_t first_page
Definition: _vm_phys.h:59
int domain
Definition: _vm_phys.h:66
vm_paddr_t start
Definition: _vm_phys.h:57
static __inline void uma_zfree(uma_zone_t zone, void *item)
Definition: uma.h:373
#define UMA_ZONE_VM
Definition: uma.h:243
#define UMA_ALIGN_PTR
Definition: uma.h:268
static __inline void * uma_zalloc(uma_zone_t zone, int flags)
Definition: uma.h:332
void uma_zone_set_maxcache(uma_zone_t zone, int nitems)
Definition: uma_core.c:4868
uma_zone_t uma_zcache_create(const char *name, int size, uma_ctor ctor, uma_dtor dtor, uma_init zinit, uma_fini zfini, uma_import zimport, uma_release zrelease, void *arg, int flags)
Definition: uma_core.c:3325
uma_zone_t uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor, uma_init uminit, uma_fini fini, int align, uint32_t flags)
Definition: uma_core.c:3251
#define UMA_ZONE_NOFREE
Definition: uma.h:241
uint16_t flags
Definition: vm_page.h:218
uint32_t _bits
Definition: vm_page.h:222
uint8_t act_count
Definition: vm_page.h:220
uint8_t queue
Definition: vm_page.h:219
#define VM_PROT_WRITE
Definition: vm.h:80
@ OBJT_DEFAULT
Definition: vm.h:92
@ OBJT_VNODE
Definition: vm.h:94
#define VM_PROT_READ
Definition: vm.h:79
int vm_ndomains
Definition: vm_phys.c:81
int vm_domainset_iter_page(struct vm_domainset_iter *di, struct vm_object *obj, int *domain)
Definition: vm_domainset.c:324
void vm_domainset_iter_page_init(struct vm_domainset_iter *di, struct vm_object *obj, vm_pindex_t pindex, int *domain, int *flags)
Definition: vm_domainset.c:332
vm_paddr_t dump_avail[PHYS_AVAIL_COUNT]
#define dump_add_page(pa)
Definition: vm_dumpset.h:41
static bool vm_addr_bound_ok(vm_paddr_t pa, vm_paddr_t size, vm_paddr_t boundary)
Definition: vm_extern.h:155
u_int vm_active_count(void)
Definition: vm_meter.c:446
u_int vm_laundry_count(void)
Definition: vm_meter.c:460
u_int vm_inactive_count(void)
Definition: vm_meter.c:453
static bool vm_addr_align_ok(vm_paddr_t pa, u_long alignment)
Definition: vm_extern.h:143
struct vmmeter __read_mostly vm_cnt
Definition: vm_meter.c:61
u_int vm_free_count(void)
Definition: vm_meter.c:420
void vm_object_pip_wakeupn(vm_object_t object, short i)
Definition: vm_object.c:359
void vm_object_pip_add(vm_object_t object, short i)
Definition: vm_object.c:344
void vm_object_busy_wait(vm_object_t obj, const char *wmesg)
Definition: vm_object.c:2514
#define VM_OBJECT_ASSERT_UNLOCKED(object)
Definition: vm_object.h:254
void vm_object_set_writeable_dirty(vm_object_t)
Definition: vm_pager.c:582
static bool vm_object_busied(vm_object_t object)
Definition: vm_object.h:347
#define VM_OBJECT_RLOCK(object)
Definition: vm_object.h:258
#define OBJ_UNMANAGED
Definition: vm_object.h:197
#define VM_OBJECT_ASSERT_BUSY(object)
Definition: vm_object.h:352
#define OBJ_FICTITIOUS
Definition: vm_object.h:196
#define VM_OBJECT_RUNLOCK(object)
Definition: vm_object.h:260
#define IDX_TO_OFF(idx)
Definition: vm_object.h:220
#define VM_OBJECT_TRYWLOCK(object)
Definition: vm_object.h:266
#define VM_OBJECT_ASSERT_LOCKED(object)
Definition: vm_object.h:248
#define VM_OBJECT_WLOCK(object)
Definition: vm_object.h:270
#define VM_OBJECT_WOWNED(object)
Definition: vm_object.h:272
#define OBJ_SWAP
Definition: vm_object.h:205
#define VM_OBJECT_WUNLOCK(object)
Definition: vm_object.h:274
#define VM_OBJECT_ASSERT_WLOCKED(object)
Definition: vm_object.h:252
static __inline bool vm_object_reserv(vm_object_t object)
Definition: vm_object.h:325
#define VM_OBJECT_DROP(object)
Definition: vm_object.h:276
static bool vm_page_pqstate_commit_request(vm_page_t m, vm_page_astate_t *old, vm_page_astate_t new)
Definition: vm_page.c:3553
void vm_page_xunbusy_hard(vm_page_t m)
Definition: vm_page.c:1181
int vm_page_trylock_KBI(vm_page_t m, const char *file, int line)
Definition: vm_page.c:5478
static void vm_page_insert_radixdone(vm_page_t m, vm_object_t object, vm_page_t mpred)
Definition: vm_page.c:1501
static int vm_severe_waiters
Definition: vm_page.c:128
vm_page_t vm_page_alloc_noobj_contig_domain(int domain, int req, u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary, vm_memattr_t memattr)
Definition: vm_page.c:2471
int vm_page_grab_pages(vm_object_t object, vm_pindex_t pindex, int allocflags, vm_page_t *ma, int count)
Definition: vm_page.c:4821
vm_page_t vm_page_getfake(vm_paddr_t paddr, vm_memattr_t memattr)
Definition: vm_page.c:1252
long vm_page_dump_pages
Definition: vm_page.c:160
#define VPAC_FLAGS
static __always_inline bool vm_page_pqstate_fcmpset(vm_page_t m, vm_page_astate_t *old, vm_page_astate_t new)
Definition: vm_page.c:3424
vm_page_t PHYS_TO_VM_PAGE(vm_paddr_t pa)
Definition: vm_page.c:1221
static void vm_page_grab_release(vm_page_t m, int allocflags)
Definition: vm_page.c:4417
void vm_page_activate(vm_page_t m)
Definition: vm_page.c:4159
static vm_paddr_t vm_page_blacklist_next(char **list, char *end)
Definition: vm_page.c:273
SYSCTL_COUNTER_U64(_vm_stats_page, OID_AUTO, pqstate_commit_retries, CTLFLAG_RD, &pqstate_commit_retries, "Number of failed per-page atomic queue state updates")
void vm_page_initfake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
Definition: vm_page.c:1262
int vm_page_grab_pages_unlocked(vm_object_t object, vm_pindex_t pindex, int allocflags, vm_page_t *ma, int count)
Definition: vm_page.c:4882
void vm_page_deactivate(vm_page_t m)
Definition: vm_page.c:4170
void vm_page_init_marker(vm_page_t marker, int queue, uint16_t aflags)
Definition: vm_page.c:428
DPCPU_DEFINE_STATIC(struct vm_batchqueue, pqbatch[MAXMEMDOM][PQ_COUNT])
struct mtx_padalign __exclusive_cache_line pa_lock[PA_LOCK_COUNT]
Definition: vm_page.c:121
static void vm_page_blacklist_load(char **list, char **end)
Definition: vm_page.c:378
void vm_page_advise(vm_page_t m, int advice)
Definition: vm_page.c:4370
#define VPSC_ANY
Definition: vm_page.c:2602
static void vm_page_bits_clear(vm_page_t m, vm_page_bits_t *bits, vm_page_bits_t clear)
Definition: vm_page.c:4983
void vm_page_xunbusy_hard_unchecked(vm_page_t m)
Definition: vm_page.c:1188
void vm_page_zero_invalid(vm_page_t m, boolean_t setvalid)
Definition: vm_page.c:5343
static __always_inline vm_page_t _vm_page_alloc_noobj_domain(int domain, const int freelist, int req)
Definition: vm_page.c:2329
void vm_page_unlock_KBI(vm_page_t m, const char *file, int line)
Definition: vm_page.c:5471
static void vm_page_zone_release(void *arg, void **store, int cnt)
Definition: vm_page.c:2584
vm_page_t vm_page_alloc_noobj(int req)
Definition: vm_page.c:2428
static void vm_page_unwire_managed(vm_page_t m, uint8_t nqueue, bool noreuse)
Definition: vm_page.c:4022
void vm_page_reference(vm_page_t m)
Definition: vm_page.c:821
bool vm_page_blacklist_add(vm_paddr_t pa, bool verbose)
Definition: vm_page.c:326
vm_page_t vm_page_alloc_contig(vm_object_t object, vm_pindex_t pindex, int req, u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary, vm_memattr_t memattr)
Definition: vm_page.c:2162
static void vm_page_domain_init(int domain)
Definition: vm_page.c:439
vm_page_t vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req)
Definition: vm_page.c:1908
void vm_page_launder(vm_page_t m)
Definition: vm_page.c:4187
void vm_page_unhold_pages(vm_page_t *ma, int count)
Definition: vm_page.c:1211
void vm_domain_set(struct vm_domain *vmd)
Definition: vm_page.c:3144
void vm_page_release_locked(vm_page_t m, int flags)
Definition: vm_page.c:4284
void vm_set_page_size(void)
Definition: vm_page.c:255
int vm_page_grab_valid_unlocked(vm_page_t *mp, vm_object_t object, vm_pindex_t pindex, int allocflags)
Definition: vm_page.c:4752
#define RUN_INDEX(count)
Definition: vm_page.c:2996
static vm_paddr_t vm_page_array_alloc(vm_offset_t *vaddr, vm_paddr_t end, vm_paddr_t page_range)
Definition: vm_page.c:519
static void vm_page_blacklist_check(char *list, char *end)
Definition: vm_page.c:357
void vm_page_wire(vm_page_t m)
Definition: vm_page.c:3965
void vm_wait_severe(void)
Definition: vm_page.c:3221
int vm_page_is_valid(vm_page_t m, int base, int size)
Definition: vm_page.c:5386
vm_page_t vm_page_alloc_domain(vm_object_t object, vm_pindex_t pindex, int domain, int req)
Definition: vm_page.c:1916
vm_page_t vm_page_alloc_freelist(int freelist, int req)
Definition: vm_page.c:2402
bool vm_page_pqstate_commit(vm_page_t m, vm_page_astate_t *old, vm_page_astate_t new)
Definition: vm_page.c:3573
vm_page_t vm_page_lookup(vm_object_t object, vm_pindex_t pindex)
Definition: vm_page.c:1627
int vm_wait_intr(vm_object_t obj)
Definition: vm_page.c:3343
void vm_page_replace(vm_page_t mnew, vm_object_t object, vm_pindex_t pindex, vm_page_t mold)
Definition: vm_page.c:1816
bool vm_page_busy_acquire(vm_page_t m, int allocflags)
Definition: vm_page.c:869
void vm_page_unswappable(vm_page_t m)
Definition: vm_page.c:4197
vm_page_t vm_page_array
Definition: vm_page.c:155
void vm_page_bits_set(vm_page_t m, vm_page_bits_t *bits, vm_page_bits_t set)
Definition: vm_page.c:4950
void vm_page_putfake(vm_page_t m)
Definition: vm_page.c:1293
vm_page_t vm_page_prev(vm_page_t m)
Definition: vm_page.c:1745
vm_page_t vm_page_alloc_noobj_contig(int req, u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary, vm_memattr_t memattr)
Definition: vm_page.c:2451
static bool _vm_page_pqstate_commit_dequeue(struct vm_pagequeue *pq, vm_page_t m, vm_page_astate_t *old, vm_page_astate_t new)
Definition: vm_page.c:3443
domainset_t __exclusive_cache_line vm_min_domains
Definition: vm_page.c:125
long vm_page_array_size
Definition: vm_page.c:156
int vm_page_tryxbusy(vm_page_t m)
Definition: vm_page.c:1151
CTASSERT(powerof2(NRUNS))
static void vm_page_object_remove(vm_page_t m)
Definition: vm_page.c:1545
struct bitset * vm_page_dump
Definition: vm_page.c:159
static int vm_wait_flags(vm_object_t obj, int mflags)
Definition: vm_page.c:3310
void vm_waitpfault(struct domainset *dset, int timo)
Definition: vm_page.c:3390
vm_page_t vm_page_scan_contig(u_long npages, vm_page_t m_start, vm_page_t m_end, u_long alignment, vm_paddr_t boundary, int options)
Definition: vm_page.c:2625
static void vm_page_init_cache_zones(void *dummy __unused)
Definition: vm_page.c:208
bool vm_page_remove(vm_page_t m)
Definition: vm_page.c:1594
static void vm_page_enqueue(vm_page_t m, uint8_t queue)
Definition: vm_page.c:3779
static int vm_min_waiters
Definition: vm_page.c:127
#define MIN_RECLAIM
Definition: vm_page.c:2998
static void vm_page_grab_check(int allocflags)
Definition: vm_page.c:4466
void vm_page_readahead_finish(vm_page_t m)
Definition: vm_page.c:1351
void vm_page_busy_sleep_unlocked(vm_object_t obj, vm_page_t m, vm_pindex_t pindex, const char *wmesg, int allocflags)
Definition: vm_page.c:1038
vm_page_t vm_page_find_least(vm_object_t object, vm_pindex_t pindex)
Definition: vm_page.c:1708
vm_page_t vm_page_relookup(vm_object_t object, vm_pindex_t pindex)
Definition: vm_page.c:1656
#define NRUNS
Definition: vm_page.c:2992
vm_page_t bogus_page
Definition: vm_page.c:153
static TAILQ_HEAD(SYSINIT(vm_page)
Definition: vm_page.c:162
static void vm_pqbatch_process(struct vm_pagequeue *pq, struct vm_batchqueue *bq, uint8_t queue)
Definition: vm_page.c:3639
void vm_page_free_zero(vm_page_t m)
Definition: vm_page.c:1339
#define VPA_FLAGS
void vm_page_pqbatch_submit(vm_page_t m, uint8_t queue)
Definition: vm_page.c:3657
static COUNTER_U64_DEFINE_EARLY(pqstate_commit_retries)
int vm_page_sbusied(vm_page_t m)
Definition: vm_page.c:958
void vm_page_valid(vm_page_t m)
Definition: vm_page.c:5453
static int vm_page_grab_pflags(int allocflags)
Definition: vm_page.c:4482
vm_page_t vm_page_next(vm_page_t m)
Definition: vm_page.c:1725
void vm_wait_min(void)
Definition: vm_page.c:3206
static void vm_page_free_toq(vm_page_t m)
Definition: vm_page.c:3913
void vm_page_dirty_KBI(vm_page_t m)
Definition: vm_page.c:1413
#define VPAN_FLAGS
u_int vm_wait_count(void)
Definition: vm_page.c:3234
vm_page_t vm_page_alloc_after(vm_object_t object, vm_pindex_t pindex, int req, vm_page_t mpred)
Definition: vm_page.c:1931
int vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex)
Definition: vm_page.c:1429
void vm_page_pqbatch_drain(void)
Definition: vm_page.c:3692
__FBSDID("$FreeBSD$")
SYSINIT(vm_page2, SI_SUB_VM_CONF, SI_ORDER_ANY, vm_page_init_cache_zones, NULL)
static vm_page_t vm_page_find_contig_domain(int domain, int req, u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
Definition: vm_page.c:2182
static int _vm_domain_allocate(struct vm_domain *vmd, int req_class, int npages)
Definition: vm_page.c:1954
void vm_page_set_valid_range(vm_page_t m, int base, int size)
Definition: vm_page.c:5082
bool vm_page_wire_mapped(vm_page_t m)
Definition: vm_page.c:3996
void vm_domain_clear(struct vm_domain *vmd)
Definition: vm_page.c:3163
static bool vm_page_tryacquire(vm_page_t m, int allocflags)
Definition: vm_page.c:852
bool vm_page_ps_test(vm_page_t m, int flags, vm_page_t skip_m)
Definition: vm_page.c:5399
vm_page_bits_t vm_page_set_dirty(vm_page_t m)
Definition: vm_page.c:5133
static bool vm_page_pqstate_commit_dequeue(vm_page_t m, vm_page_astate_t *old, vm_page_astate_t new)
Definition: vm_page.c:3484
#define VPANC_FLAGS
static vm_page_bits_t vm_page_bits_swap(vm_page_t m, vm_page_bits_t *bits, vm_page_bits_t newbits)
Definition: vm_page.c:5016
vm_page_t vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags)
Definition: vm_page.c:4509
void vm_page_free_pages_toq(struct spglist *free, bool update_wire_count)
Definition: vm_page.c:3941
static void vm_pqbatch_process_page(struct vm_pagequeue *pq, vm_page_t m, uint8_t queue)
Definition: vm_page.c:3598
static int vm_page_zone_import(void *arg, void **store, int cnt, int domain, int flags)
Definition: vm_page.c:2556
vm_page_t vm_page_alloc_freelist_domain(int domain, int freelist, int req)
Definition: vm_page.c:2419
bool vm_page_try_remove_all(vm_page_t m)
Definition: vm_page.c:4348
void vm_page_lock_KBI(vm_page_t m, const char *file, int line)
Definition: vm_page.c:5464
int vm_page_busy_tryupgrade(vm_page_t m)
Definition: vm_page.c:932
static void vm_page_release_toq(vm_page_t m, uint8_t nqueue, const bool noreuse)
Definition: vm_page.c:4211
static bool vm_page_grab_sleep(vm_object_t object, vm_page_t m, vm_pindex_t pindex, const char *wmesg, int allocflags, bool locked)
Definition: vm_page.c:4439
vm_page_bits_t vm_page_bits(int base, int size)
Definition: vm_page.c:4929
int vm_page_grab_valid(vm_page_t *mp, vm_object_t object, vm_pindex_t pindex, int allocflags)
Definition: vm_page.c:4647
static struct vm_pagequeue * _vm_page_pagequeue(vm_page_t m, uint8_t queue)
Definition: vm_page.c:3408
static int vm_page_reclaim_run(int req_class, int domain, u_long npages, vm_page_t m_run, vm_paddr_t high)
Definition: vm_page.c:2800
static bool _vm_page_pqstate_commit_requeue(struct vm_pagequeue *pq, vm_page_t m, vm_page_astate_t *old, vm_page_astate_t new)
Definition: vm_page.c:3513
struct mtx_padalign __exclusive_cache_line vm_domainset_lock
Definition: vm_page.c:123
void vm_page_set_invalid(vm_page_t m, int base, int size)
Definition: vm_page.c:5277
static int vm_domain_alloc_fail(struct vm_domain *vmd, vm_object_t object, int req)
Definition: vm_page.c:3359
static int vm_page_insert_after(vm_page_t m, vm_object_t object, vm_pindex_t pindex, vm_page_t mpred)
Definition: vm_page.c:1449
int vm_domain_allocate(struct vm_domain *vmd, int req, int npages)
Definition: vm_page.c:1989
static int sysctl_vm_page_blacklist(SYSCTL_HANDLER_ARGS)
Definition: vm_page.c:401
vm_page_t vm_page_alloc_contig_domain(vm_object_t object, vm_pindex_t pindex, int domain, int req, u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary, vm_memattr_t memattr)
Definition: vm_page.c:2220
void vm_page_set_validclean(vm_page_t m, int base, int size)
Definition: vm_page.c:5183
bool vm_page_reclaim_contig_domain(int domain, int req, u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
Definition: vm_page.c:3024
void vm_page_invalid(vm_page_t m)
Definition: vm_page.c:5318
int vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex)
Definition: vm_page.c:1845
static bool vm_page_try_blocked_op(vm_page_t m, void(*op)(vm_page_t))
Definition: vm_page.c:4312
bool vm_page_unwire_noq(vm_page_t m)
Definition: vm_page.c:4101
void vm_page_init_page(vm_page_t m, vm_paddr_t pa, int segind)
Definition: vm_page.c:500
vm_page_t vm_page_lookup_unlocked(vm_object_t object, vm_pindex_t pindex)
Definition: vm_page.c:1643
struct vm_domain vm_dom[MAXMEMDOM]
Definition: vm_page.c:117
void vm_page_sunbusy(vm_page_t m)
Definition: vm_page.c:972
static __always_inline void vm_page_mvqueue(vm_page_t m, const uint8_t nqueue, const uint16_t nflag)
Definition: vm_page.c:4125
void vm_wait_domain(int domain)
Definition: vm_page.c:3284
static __inline void vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits)
Definition: vm_page.c:5154
bool vm_page_busy_sleep(vm_page_t m, const char *wmesg, int allocflags)
Definition: vm_page.c:1014
void vm_wait(vm_object_t obj)
Definition: vm_page.c:3337
void vm_page_updatefake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
Definition: vm_page.c:1311
static void vm_page_busy_free(vm_page_t m)
Definition: vm_page.c:1195
static bool vm_page_replace_hold(vm_page_t mnew, vm_object_t object, vm_pindex_t pindex, vm_page_t mold)
Definition: vm_page.c:1770
#define VPSC_NOSUPER
Definition: vm_page.c:2604
void vm_page_busy_downgrade(vm_page_t m)
Definition: vm_page.c:909
static bool vm_page_free_prep(vm_page_t m)
Definition: vm_page.c:3806
void vm_page_release(vm_page_t m, int flags)
Definition: vm_page.c:4256
static bool _vm_page_busy_sleep(vm_object_t obj, vm_page_t m, vm_pindex_t pindex, const char *wmesg, int allocflags, bool locked)
Definition: vm_page.c:1059
void vm_page_test_dirty(vm_page_t m)
Definition: vm_page.c:5444
vm_offset_t vm_page_startup(vm_offset_t vaddr)
Definition: vm_page.c:551
void vm_page_unwire(vm_page_t m, uint8_t nqueue)
Definition: vm_page.c:4079
static SYSCTL_NODE(_vm_stats, OID_AUTO, page, CTLFLAG_RD|CTLFLAG_MPSAFE, 0, "VM page statistics")
void vm_page_free_invalid(vm_page_t m)
Definition: vm_page.c:1375
static void vm_page_alloc_check(vm_page_t m)
Definition: vm_page.c:2537
#define VPSC_NORESERV
Definition: vm_page.c:2603
void vm_page_dequeue(vm_page_t m)
Definition: vm_page.c:3755
bool vm_page_try_remove_write(vm_page_t m)
Definition: vm_page.c:4358
int vm_page_trysbusy(vm_page_t m)
Definition: vm_page.c:1113
static void vm_page_xunbusy_hard_tail(vm_page_t m)
Definition: vm_page.c:1168
static bool vm_page_acquire_unlocked(vm_object_t object, vm_pindex_t pindex, vm_page_t prev, vm_page_t *mp, int allocflags)
Definition: vm_page.c:4561
static int vm_pageproc_waiters
Definition: vm_page.c:129
void vm_page_free(vm_page_t m)
Definition: vm_page.c:1326
static void vm_page_busy_release(vm_page_t m)
Definition: vm_page.c:1674
vm_page_t vm_page_alloc_noobj_domain(int domain, int req)
Definition: vm_page.c:2445
static bool vm_page_trybusy(vm_page_t m, int allocflags)
Definition: vm_page.c:835
bool vm_page_remove_xbusy(vm_page_t m)
Definition: vm_page.c:1611
vm_page_t vm_page_grab_unlocked(vm_object_t object, vm_pindex_t pindex, int allocflags)
Definition: vm_page.c:4612
void vm_page_deactivate_noreuse(vm_page_t m)
Definition: vm_page.c:4177
void vm_page_clear_dirty(vm_page_t m, int base, int size)
Definition: vm_page.c:5264
bool vm_page_reclaim_contig(int req, u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
Definition: vm_page.c:3122
int vm_wait_doms(const domainset_t *wdoms, int mflags)
Definition: vm_page.c:3241
long first_page
Definition: vm_page.c:157
domainset_t __exclusive_cache_line vm_severe_domains
Definition: vm_page.c:126
void vm_page_dequeue_deferred(vm_page_t m)
Definition: vm_page.c:3731
vm_page_t vm_page_alloc_domain_after(vm_object_t object, vm_pindex_t pindex, int domain, int req, vm_page_t mpred)
Definition: vm_page.c:2003
static void vm_page_aflag_set(vm_page_t m, uint16_t bits)
Definition: vm_page.h:858
#define PQ_ACTIVE
Definition: vm_page.h:333
#define PG_ZERO
Definition: vm_page.h:461
#define vm_page_assert_xbusied(m)
Definition: vm_page.h:745
static bool vm_page_in_laundry(vm_page_t m)
Definition: vm_page.h:945
#define PQ_NONE
Definition: vm_page.h:331
static bool vm_page_all_valid(vm_page_t m)
Definition: vm_page.h:990
#define PGA_SWAP_SPACE
Definition: vm_page.h:446
#define vm_page_xunbusy_unchecked(m)
Definition: vm_page.h:769
#define VM_ALLOC_COUNT(count)
Definition: vm_page.h:555
#define vm_page_assert_xbusied_unchecked(m)
Definition: vm_page.h:740
#define vm_page_busy_freed(m)
Definition: vm_page.h:760
#define VPO_UNMANAGED
Definition: vm_page.h:296
#define VPRC_BLOCKED
Definition: vm_page.h:275
#define PQ_COUNT
Definition: vm_page.h:336
#define vm_page_busy_fetch(m)
Definition: vm_page.h:721
#define VM_ALLOC_NOCREAT
Definition: vm_page.h:546
#define vm_page_assert_busied(m)
Definition: vm_page.h:723
#define VM_ALLOC_INTERRUPT
Definition: vm_page.h:536
#define VPO_SWAPINPROG
Definition: vm_page.h:297
#define VM_ALLOC_WAITOK
Definition: vm_page.h:539
#define PGA_REFERENCED
Definition: vm_page.h:438
#define VPB_UNBUSIED
Definition: vm_page.h:326
#define VPB_BIT_SHARED
Definition: vm_page.h:306
#define VM_PAGE_OBJECT_BUSY_ASSERT(m)
Definition: vm_page.h:796
#define PGA_REQUEUE_HEAD
Definition: vm_page.h:443
#define VPB_BIT_WAITERS
Definition: vm_page.h:308
#define VPB_SHARERS(x)
Definition: vm_page.h:313
#define PQ_INACTIVE
Definition: vm_page.h:332
static bool vm_page_none_valid(vm_page_t m)
Definition: vm_page.h:997
#define vm_page_lockptr(m)
Definition: vm_page.h:377
#define PS_NONE_BUSY
Definition: vm_page.h:596
static bool vm_page_astate_fcmpset(vm_page_t m, vm_page_astate_t *old, vm_page_astate_t new)
Definition: vm_page.h:823
static __inline void vm_page_undirty(vm_page_t m)
Definition: vm_page.h:902
#define vm_page_busied(m)
Definition: vm_page.h:754
#define VM_ALLOC_IGN_SBUSY
Definition: vm_page.h:548
static u_int vm_page_drop(vm_page_t m, u_int val)
Definition: vm_page.h:959
#define VM_ALLOC_SYSTEM
Definition: vm_page.h:537
#define PS_ALL_VALID
Definition: vm_page.h:595
#define vm_page_assert_unbusied(m)
Definition: vm_page.h:733
#define VPB_CURTHREAD_EXCLUSIVE
Definition: vm_page.h:323
#define VM_ALLOC_NORECLAIM
Definition: vm_page.h:543
static uint8_t vm_page_queue(vm_page_t m)
Definition: vm_page.h:924
#define PGA_SWAP_FREE
Definition: vm_page.h:445
#define VPR_TRYFREE
Definition: vm_page.h:672
#define PS_ALL_DIRTY
Definition: vm_page.h:594
#define PGA_EXECUTABLE
Definition: vm_page.h:439
#define vm_page_assert_sbusied(m)
Definition: vm_page.h:728
#define VM_ALLOC_SBUSY
Definition: vm_page.h:550
#define VM_ALLOC_NODUMP
Definition: vm_page.h:549
#define VM_ALLOC_NORMAL
Definition: vm_page.h:535
#define PG_PCPU_CACHE
Definition: vm_page.h:459
#define PG_MARKER
Definition: vm_page.h:462
#define VPRC_OBJREF
Definition: vm_page.h:276
#define PQ_LAUNDRY
Definition: vm_page.h:334
#define PGA_DEQUEUE
Definition: vm_page.h:441
#define PQ_UNSWAPPABLE
Definition: vm_page.h:335
#define VPB_SHARERS_WORD(x)
Definition: vm_page.h:315
#define VPB_FREED
Definition: vm_page.h:329
#define VM_ALLOC_CLASS_MASK
Definition: vm_page.h:538
#define PGA_WRITEABLE
Definition: vm_page.h:437
static vm_page_astate_t vm_page_astate_load(vm_page_t m)
Definition: vm_page.h:811
#define VM_ALLOC_WAITFAIL
Definition: vm_page.h:540
static int vm_page_domain(vm_page_t m)
Definition: vm_page.h:1004
#define ACT_INIT
Definition: vm_page.h:470
#define VPRC_WIRE_COUNT_MAX
Definition: vm_page.h:278
#define PGA_ENQUEUED
Definition: vm_page.h:440
#define VM_ALLOC_WIRED
Definition: vm_page.h:541
static void vm_page_aflag_clear(vm_page_t m, uint16_t bits)
Definition: vm_page.h:840
#define vm_page_xbusy_claim(m)
Definition: vm_page.h:798
#define VPRC_WIRE_COUNT(c)
Definition: vm_page.h:277
#define vm_page_xunbusy(m)
Definition: vm_page.h:764
#define VPO_SWAPSLEEP
Definition: vm_page.h:295
#define VPB_ONE_SHARER
Definition: vm_page.h:316
#define PGA_QUEUE_STATE_MASK
Definition: vm_page.h:449
#define VM_ALLOC_ZERO
Definition: vm_page.h:542
#define PG_NODUMP
Definition: vm_page.h:463
#define PGA_REQUEUE
Definition: vm_page.h:442
#define PGA_NOSYNC
Definition: vm_page.h:444
static __inline void vm_page_dirty(vm_page_t m)
Definition: vm_page.h:885
#define VM_ALLOC_COUNT_SHIFT
Definition: vm_page.h:553
static bool vm_page_wired(vm_page_t m)
Definition: vm_page.h:983
#define VM_ALLOC_NOBUSY
Definition: vm_page.h:545
#define VM_ALLOC_NOWAIT
Definition: vm_page.h:551
#define PG_FICTITIOUS
Definition: vm_page.h:460
#define VM_PAGE_TO_PHYS(entry)
Definition: vm_page.h:506
#define PGA_QUEUE_OP_MASK
Definition: vm_page.h:448
#define vm_page_xbusied(m)
Definition: vm_page.h:757
void pagedaemon_wakeup(int domain)
Definition: vm_pageout.c:2407
struct proc * pageproc
Definition: vm_pageout.c:134
static int vm_paging_needed(struct vm_domain *vmd, u_int free_count)
Definition: vm_pagequeue.h:410
#define vm_domain_free_unlock(d)
Definition: vm_pagequeue.h:320
static bool vm_batchqueue_insert(struct vm_batchqueue *bq, vm_page_t m)
Definition: vm_pagequeue.h:360
#define vm_pagequeue_assert_locked(pq)
Definition: vm_pagequeue.h:304
static struct vm_domain * vm_pagequeue_domain(vm_page_t m)
Definition: vm_pagequeue.h:389
#define vm_domain_free_lock(d)
Definition: vm_pagequeue.h:314
static void vm_domain_freecnt_inc(struct vm_domain *vmd, int adj)
Definition: vm_pagequeue.h:450
static int vm_paging_severe(struct vm_domain *vmd)
Definition: vm_pagequeue.h:430
int pq_cnt
Definition: vm_pagequeue.h:2
#define vm_pagequeue_cnt_dec(pq)
Definition: vm_pagequeue.h:342
u_int vmd_page_count
Definition: vm_pagequeue.h:11
u_int vmd_free_reserved
Definition: vm_pagequeue.h:47
#define vm_pagequeue_unlock(pq)
Definition: vm_pagequeue.h:308
static void vm_batchqueue_init(struct vm_batchqueue *bq)
Definition: vm_pagequeue.h:353
#define vm_pagequeue_lock(pq)
Definition: vm_pagequeue.h:305
u_int vmd_free_min
Definition: vm_pagequeue.h:49
u_int vmd_free_severe
Definition: vm_pagequeue.h:54
static int vm_paging_min(struct vm_domain *vmd)
Definition: vm_pagequeue.h:420
struct vm_pagequeue vmd_pagequeues[PQ_COUNT]
Definition: vm_pagequeue.h:0
#define vm_domain_free_assert_unlocked(n)
Definition: vm_pagequeue.h:312
#define vm_pagequeue_cnt_inc(pq)
Definition: vm_pagequeue.h:341
#define VM_DOMAIN(n)
Definition: vm_pagequeue.h:301
int vm_pager_get_pages(vm_object_t object, vm_page_t *m, int count, int *rbehind, int *rahead)
Definition: vm_pager.c:319
#define VM_PAGER_FAIL
Definition: vm_pager.h:112
static __inline void vm_pager_page_unswapped(vm_page_t m)
Definition: vm_pager.h:197
#define VM_PAGER_OK
Definition: vm_pager.h:110
static __inline boolean_t vm_pager_has_page(vm_object_t object, vm_pindex_t offset, int *before, int *after)
Definition: vm_pager.h:163
#define PA_LOCK_COUNT
Definition: vm_param.h:123
void vm_phys_early_startup(void)
Definition: vm_phys.c:1716
vm_page_t vm_phys_fictitious_to_vm_page(vm_paddr_t pa)
Definition: vm_phys.c:909
vm_page_t vm_phys_alloc_contig(int domain, u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
Definition: vm_phys.c:1364
vm_page_t vm_phys_scan_contig(int domain, u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary, int options)
Definition: vm_phys.c:1249
int vm_phys_alloc_npages(int domain, int pool, int npages, vm_page_t ma[])
Definition: vm_phys.c:740
vm_paddr_t vm_phys_avail_size(int i)
Definition: vm_phys.c:1564
void vm_phys_enqueue_contig(vm_page_t m, u_long npages)
Definition: vm_phys.c:1161
void vm_phys_add_seg(vm_paddr_t start, vm_paddr_t end)
Definition: vm_phys.c:454
void vm_phys_free_pages(vm_page_t m, int order)
Definition: vm_phys.c:1097
vm_page_t vm_phys_alloc_freelist_pages(int domain, int freelist, int pool, int order)
Definition: vm_phys.c:835
boolean_t vm_phys_unfree_page(vm_page_t m)
Definition: vm_phys.c:1297
vm_page_t vm_phys_alloc_pages(int domain, int pool, int order)
Definition: vm_phys.c:814
int vm_phys_avail_largest(void)
Definition: vm_phys.c:1544
void vm_phys_init(void)
Definition: vm_phys.c:489
vm_page_t vm_phys_paddr_to_vm_page(vm_paddr_t pa)
Definition: vm_phys.c:895
vm_paddr_t phys_avail[]
int vm_radix_insert(struct vm_radix *rtree, vm_page_t page)
Definition: vm_radix.c:386
void vm_radix_wait(void)
Definition: vm_radix.c:881
vm_page_t vm_radix_lookup_ge(struct vm_radix *rtree, vm_pindex_t index)
Definition: vm_radix.c:534
vm_page_t vm_radix_replace(struct vm_radix *rtree, vm_page_t newpage)
Definition: vm_radix.c:842
vm_page_t vm_radix_remove(struct vm_radix *rtree, vm_pindex_t index)
Definition: vm_radix.c:761
vm_page_t vm_radix_lookup_unlocked(struct vm_radix *rtree, vm_pindex_t index)
Definition: vm_radix.c:519
vm_page_t vm_radix_lookup(struct vm_radix *rtree, vm_pindex_t index)
Definition: vm_radix.c:507
vm_page_t vm_radix_lookup_le(struct vm_radix *rtree, vm_pindex_t index)
Definition: vm_radix.c:646