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