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