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