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