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