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