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