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