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