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