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