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