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