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