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