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