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