1 /*- 2 * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU) 3 * 4 * Copyright (c) 1991 Regents of the University of California. 5 * All rights reserved. 6 * Copyright (c) 1998 Matthew Dillon. All Rights Reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * The Mach Operating System project at Carnegie-Mellon University. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * from: @(#)vm_page.c 7.4 (Berkeley) 5/7/91 36 */ 37 38 /*- 39 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 40 * All rights reserved. 41 * 42 * Authors: Avadis Tevanian, Jr., Michael Wayne Young 43 * 44 * Permission to use, copy, modify and distribute this software and 45 * its documentation is hereby granted, provided that both the copyright 46 * notice and this permission notice appear in all copies of the 47 * software, derivative works or modified versions, and any portions 48 * thereof, and that both notices appear in supporting documentation. 49 * 50 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 51 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 52 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 53 * 54 * Carnegie Mellon requests users of this software to return to 55 * 56 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 57 * School of Computer Science 58 * Carnegie Mellon University 59 * Pittsburgh PA 15213-3890 60 * 61 * any improvements or extensions that they make and grant Carnegie the 62 * rights to redistribute these changes. 63 */ 64 65 /* 66 * GENERAL RULES ON VM_PAGE MANIPULATION 67 * 68 * - A page queue lock is required when adding or removing a page from a 69 * page queue regardless of other locks or the busy state of a page. 70 * 71 * * In general, no thread besides the page daemon can acquire or 72 * hold more than one page queue lock at a time. 73 * 74 * * The page daemon can acquire and hold any pair of page queue 75 * locks in any order. 76 * 77 * - The object lock is required when inserting or removing 78 * pages from an object (vm_page_insert() or vm_page_remove()). 79 * 80 */ 81 82 /* 83 * Resident memory management module. 84 */ 85 86 #include <sys/cdefs.h> 87 __FBSDID("$FreeBSD$"); 88 89 #include "opt_vm.h" 90 91 #include <sys/param.h> 92 #include <sys/systm.h> 93 #include <sys/lock.h> 94 #include <sys/domainset.h> 95 #include <sys/kernel.h> 96 #include <sys/limits.h> 97 #include <sys/linker.h> 98 #include <sys/malloc.h> 99 #include <sys/mman.h> 100 #include <sys/msgbuf.h> 101 #include <sys/mutex.h> 102 #include <sys/proc.h> 103 #include <sys/rwlock.h> 104 #include <sys/sbuf.h> 105 #include <sys/sched.h> 106 #include <sys/smp.h> 107 #include <sys/sysctl.h> 108 #include <sys/vmmeter.h> 109 #include <sys/vnode.h> 110 111 #include <vm/vm.h> 112 #include <vm/pmap.h> 113 #include <vm/vm_param.h> 114 #include <vm/vm_domainset.h> 115 #include <vm/vm_kern.h> 116 #include <vm/vm_map.h> 117 #include <vm/vm_object.h> 118 #include <vm/vm_page.h> 119 #include <vm/vm_pageout.h> 120 #include <vm/vm_phys.h> 121 #include <vm/vm_pagequeue.h> 122 #include <vm/vm_pager.h> 123 #include <vm/vm_radix.h> 124 #include <vm/vm_reserv.h> 125 #include <vm/vm_extern.h> 126 #include <vm/uma.h> 127 #include <vm/uma_int.h> 128 129 #include <machine/md_var.h> 130 131 extern int uma_startup_count(int); 132 extern void uma_startup(void *, int); 133 extern int vmem_startup_count(void); 134 135 struct vm_domain vm_dom[MAXMEMDOM]; 136 137 static DPCPU_DEFINE(struct vm_batchqueue, pqbatch[MAXMEMDOM][PQ_COUNT]); 138 139 struct mtx_padalign __exclusive_cache_line pa_lock[PA_LOCK_COUNT]; 140 141 struct mtx_padalign __exclusive_cache_line vm_domainset_lock; 142 /* The following fields are protected by the domainset lock. */ 143 domainset_t __exclusive_cache_line vm_min_domains; 144 domainset_t __exclusive_cache_line vm_severe_domains; 145 static int vm_min_waiters; 146 static int vm_severe_waiters; 147 static int vm_pageproc_waiters; 148 149 /* 150 * bogus page -- for I/O to/from partially complete buffers, 151 * or for paging into sparsely invalid regions. 152 */ 153 vm_page_t bogus_page; 154 155 vm_page_t vm_page_array; 156 long vm_page_array_size; 157 long first_page; 158 159 static int boot_pages; 160 SYSCTL_INT(_vm, OID_AUTO, boot_pages, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, 161 &boot_pages, 0, 162 "number of pages allocated for bootstrapping the VM system"); 163 164 static int pa_tryrelock_restart; 165 SYSCTL_INT(_vm, OID_AUTO, tryrelock_restart, CTLFLAG_RD, 166 &pa_tryrelock_restart, 0, "Number of tryrelock restarts"); 167 168 static TAILQ_HEAD(, vm_page) blacklist_head; 169 static int sysctl_vm_page_blacklist(SYSCTL_HANDLER_ARGS); 170 SYSCTL_PROC(_vm, OID_AUTO, page_blacklist, CTLTYPE_STRING | CTLFLAG_RD | 171 CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_page_blacklist, "A", "Blacklist pages"); 172 173 static uma_zone_t fakepg_zone; 174 175 static void vm_page_alloc_check(vm_page_t m); 176 static void vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits); 177 static void vm_page_dequeue_complete(vm_page_t m); 178 static void vm_page_enqueue(vm_page_t m, uint8_t queue); 179 static void vm_page_init(void *dummy); 180 static int vm_page_insert_after(vm_page_t m, vm_object_t object, 181 vm_pindex_t pindex, vm_page_t mpred); 182 static void vm_page_insert_radixdone(vm_page_t m, vm_object_t object, 183 vm_page_t mpred); 184 static int vm_page_reclaim_run(int req_class, int domain, u_long npages, 185 vm_page_t m_run, vm_paddr_t high); 186 static int vm_domain_alloc_fail(struct vm_domain *vmd, vm_object_t object, 187 int req); 188 static int vm_page_import(void *arg, void **store, int cnt, int domain, 189 int flags); 190 static void vm_page_release(void *arg, void **store, int cnt); 191 192 SYSINIT(vm_page, SI_SUB_VM, SI_ORDER_SECOND, vm_page_init, NULL); 193 194 static void 195 vm_page_init(void *dummy) 196 { 197 198 fakepg_zone = uma_zcreate("fakepg", sizeof(struct vm_page), NULL, NULL, 199 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE | UMA_ZONE_VM); 200 bogus_page = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ | 201 VM_ALLOC_NORMAL | VM_ALLOC_WIRED); 202 } 203 204 /* 205 * The cache page zone is initialized later since we need to be able to allocate 206 * pages before UMA is fully initialized. 207 */ 208 static void 209 vm_page_init_cache_zones(void *dummy __unused) 210 { 211 struct vm_domain *vmd; 212 int i; 213 214 for (i = 0; i < vm_ndomains; i++) { 215 vmd = VM_DOMAIN(i); 216 /* 217 * Don't allow the page cache to take up more than .25% of 218 * memory. 219 */ 220 if (vmd->vmd_page_count / 400 < 256 * mp_ncpus) 221 continue; 222 vmd->vmd_pgcache = uma_zcache_create("vm pgcache", 223 sizeof(struct vm_page), NULL, NULL, NULL, NULL, 224 vm_page_import, vm_page_release, vmd, 225 UMA_ZONE_NOBUCKETCACHE | UMA_ZONE_MAXBUCKET | UMA_ZONE_VM); 226 } 227 } 228 SYSINIT(vm_page2, SI_SUB_VM_CONF, SI_ORDER_ANY, vm_page_init_cache_zones, NULL); 229 230 /* Make sure that u_long is at least 64 bits when PAGE_SIZE is 32K. */ 231 #if PAGE_SIZE == 32768 232 #ifdef CTASSERT 233 CTASSERT(sizeof(u_long) >= 8); 234 #endif 235 #endif 236 237 /* 238 * Try to acquire a physical address lock while a pmap is locked. If we 239 * fail to trylock we unlock and lock the pmap directly and cache the 240 * locked pa in *locked. The caller should then restart their loop in case 241 * the virtual to physical mapping has changed. 242 */ 243 int 244 vm_page_pa_tryrelock(pmap_t pmap, vm_paddr_t pa, vm_paddr_t *locked) 245 { 246 vm_paddr_t lockpa; 247 248 lockpa = *locked; 249 *locked = pa; 250 if (lockpa) { 251 PA_LOCK_ASSERT(lockpa, MA_OWNED); 252 if (PA_LOCKPTR(pa) == PA_LOCKPTR(lockpa)) 253 return (0); 254 PA_UNLOCK(lockpa); 255 } 256 if (PA_TRYLOCK(pa)) 257 return (0); 258 PMAP_UNLOCK(pmap); 259 atomic_add_int(&pa_tryrelock_restart, 1); 260 PA_LOCK(pa); 261 PMAP_LOCK(pmap); 262 return (EAGAIN); 263 } 264 265 /* 266 * vm_set_page_size: 267 * 268 * Sets the page size, perhaps based upon the memory 269 * size. Must be called before any use of page-size 270 * dependent functions. 271 */ 272 void 273 vm_set_page_size(void) 274 { 275 if (vm_cnt.v_page_size == 0) 276 vm_cnt.v_page_size = PAGE_SIZE; 277 if (((vm_cnt.v_page_size - 1) & vm_cnt.v_page_size) != 0) 278 panic("vm_set_page_size: page size not a power of two"); 279 } 280 281 /* 282 * vm_page_blacklist_next: 283 * 284 * Find the next entry in the provided string of blacklist 285 * addresses. Entries are separated by space, comma, or newline. 286 * If an invalid integer is encountered then the rest of the 287 * string is skipped. Updates the list pointer to the next 288 * character, or NULL if the string is exhausted or invalid. 289 */ 290 static vm_paddr_t 291 vm_page_blacklist_next(char **list, char *end) 292 { 293 vm_paddr_t bad; 294 char *cp, *pos; 295 296 if (list == NULL || *list == NULL) 297 return (0); 298 if (**list =='\0') { 299 *list = NULL; 300 return (0); 301 } 302 303 /* 304 * If there's no end pointer then the buffer is coming from 305 * the kenv and we know it's null-terminated. 306 */ 307 if (end == NULL) 308 end = *list + strlen(*list); 309 310 /* Ensure that strtoq() won't walk off the end */ 311 if (*end != '\0') { 312 if (*end == '\n' || *end == ' ' || *end == ',') 313 *end = '\0'; 314 else { 315 printf("Blacklist not terminated, skipping\n"); 316 *list = NULL; 317 return (0); 318 } 319 } 320 321 for (pos = *list; *pos != '\0'; pos = cp) { 322 bad = strtoq(pos, &cp, 0); 323 if (*cp == '\0' || *cp == ' ' || *cp == ',' || *cp == '\n') { 324 if (bad == 0) { 325 if (++cp < end) 326 continue; 327 else 328 break; 329 } 330 } else 331 break; 332 if (*cp == '\0' || ++cp >= end) 333 *list = NULL; 334 else 335 *list = cp; 336 return (trunc_page(bad)); 337 } 338 printf("Garbage in RAM blacklist, skipping\n"); 339 *list = NULL; 340 return (0); 341 } 342 343 bool 344 vm_page_blacklist_add(vm_paddr_t pa, bool verbose) 345 { 346 struct vm_domain *vmd; 347 vm_page_t m; 348 int ret; 349 350 m = vm_phys_paddr_to_vm_page(pa); 351 if (m == NULL) 352 return (true); /* page does not exist, no failure */ 353 354 vmd = vm_pagequeue_domain(m); 355 vm_domain_free_lock(vmd); 356 ret = vm_phys_unfree_page(m); 357 vm_domain_free_unlock(vmd); 358 if (ret) { 359 TAILQ_INSERT_TAIL(&blacklist_head, m, listq); 360 if (verbose) 361 printf("Skipping page with pa 0x%jx\n", (uintmax_t)pa); 362 } 363 return (ret); 364 } 365 366 /* 367 * vm_page_blacklist_check: 368 * 369 * Iterate through the provided string of blacklist addresses, pulling 370 * each entry out of the physical allocator free list and putting it 371 * onto a list for reporting via the vm.page_blacklist sysctl. 372 */ 373 static void 374 vm_page_blacklist_check(char *list, char *end) 375 { 376 vm_paddr_t pa; 377 char *next; 378 379 next = list; 380 while (next != NULL) { 381 if ((pa = vm_page_blacklist_next(&next, end)) == 0) 382 continue; 383 vm_page_blacklist_add(pa, bootverbose); 384 } 385 } 386 387 /* 388 * vm_page_blacklist_load: 389 * 390 * Search for a special module named "ram_blacklist". It'll be a 391 * plain text file provided by the user via the loader directive 392 * of the same name. 393 */ 394 static void 395 vm_page_blacklist_load(char **list, char **end) 396 { 397 void *mod; 398 u_char *ptr; 399 u_int len; 400 401 mod = NULL; 402 ptr = NULL; 403 404 mod = preload_search_by_type("ram_blacklist"); 405 if (mod != NULL) { 406 ptr = preload_fetch_addr(mod); 407 len = preload_fetch_size(mod); 408 } 409 *list = ptr; 410 if (ptr != NULL) 411 *end = ptr + len; 412 else 413 *end = NULL; 414 return; 415 } 416 417 static int 418 sysctl_vm_page_blacklist(SYSCTL_HANDLER_ARGS) 419 { 420 vm_page_t m; 421 struct sbuf sbuf; 422 int error, first; 423 424 first = 1; 425 error = sysctl_wire_old_buffer(req, 0); 426 if (error != 0) 427 return (error); 428 sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 429 TAILQ_FOREACH(m, &blacklist_head, listq) { 430 sbuf_printf(&sbuf, "%s%#jx", first ? "" : ",", 431 (uintmax_t)m->phys_addr); 432 first = 0; 433 } 434 error = sbuf_finish(&sbuf); 435 sbuf_delete(&sbuf); 436 return (error); 437 } 438 439 /* 440 * Initialize a dummy page for use in scans of the specified paging queue. 441 * In principle, this function only needs to set the flag PG_MARKER. 442 * Nonetheless, it write busies and initializes the hold count to one as 443 * safety precautions. 444 */ 445 static void 446 vm_page_init_marker(vm_page_t marker, int queue, uint8_t aflags) 447 { 448 449 bzero(marker, sizeof(*marker)); 450 marker->flags = PG_MARKER; 451 marker->aflags = aflags; 452 marker->busy_lock = VPB_SINGLE_EXCLUSIVER; 453 marker->queue = queue; 454 marker->hold_count = 1; 455 } 456 457 static void 458 vm_page_domain_init(int domain) 459 { 460 struct vm_domain *vmd; 461 struct vm_pagequeue *pq; 462 int i; 463 464 vmd = VM_DOMAIN(domain); 465 bzero(vmd, sizeof(*vmd)); 466 *__DECONST(char **, &vmd->vmd_pagequeues[PQ_INACTIVE].pq_name) = 467 "vm inactive pagequeue"; 468 *__DECONST(char **, &vmd->vmd_pagequeues[PQ_ACTIVE].pq_name) = 469 "vm active pagequeue"; 470 *__DECONST(char **, &vmd->vmd_pagequeues[PQ_LAUNDRY].pq_name) = 471 "vm laundry pagequeue"; 472 *__DECONST(char **, &vmd->vmd_pagequeues[PQ_UNSWAPPABLE].pq_name) = 473 "vm unswappable pagequeue"; 474 vmd->vmd_domain = domain; 475 vmd->vmd_page_count = 0; 476 vmd->vmd_free_count = 0; 477 vmd->vmd_segs = 0; 478 vmd->vmd_oom = FALSE; 479 for (i = 0; i < PQ_COUNT; i++) { 480 pq = &vmd->vmd_pagequeues[i]; 481 TAILQ_INIT(&pq->pq_pl); 482 mtx_init(&pq->pq_mutex, pq->pq_name, "vm pagequeue", 483 MTX_DEF | MTX_DUPOK); 484 vm_page_init_marker(&vmd->vmd_markers[i], i, 0); 485 } 486 mtx_init(&vmd->vmd_free_mtx, "vm page free queue", NULL, MTX_DEF); 487 mtx_init(&vmd->vmd_pageout_mtx, "vm pageout lock", NULL, MTX_DEF); 488 snprintf(vmd->vmd_name, sizeof(vmd->vmd_name), "%d", domain); 489 490 /* 491 * inacthead is used to provide FIFO ordering for LRU-bypassing 492 * insertions. 493 */ 494 vm_page_init_marker(&vmd->vmd_inacthead, PQ_INACTIVE, PGA_ENQUEUED); 495 TAILQ_INSERT_HEAD(&vmd->vmd_pagequeues[PQ_INACTIVE].pq_pl, 496 &vmd->vmd_inacthead, plinks.q); 497 498 /* 499 * The clock pages are used to implement active queue scanning without 500 * requeues. Scans start at clock[0], which is advanced after the scan 501 * ends. When the two clock hands meet, they are reset and scanning 502 * resumes from the head of the queue. 503 */ 504 vm_page_init_marker(&vmd->vmd_clock[0], PQ_ACTIVE, PGA_ENQUEUED); 505 vm_page_init_marker(&vmd->vmd_clock[1], PQ_ACTIVE, PGA_ENQUEUED); 506 TAILQ_INSERT_HEAD(&vmd->vmd_pagequeues[PQ_ACTIVE].pq_pl, 507 &vmd->vmd_clock[0], plinks.q); 508 TAILQ_INSERT_TAIL(&vmd->vmd_pagequeues[PQ_ACTIVE].pq_pl, 509 &vmd->vmd_clock[1], plinks.q); 510 } 511 512 /* 513 * Initialize a physical page in preparation for adding it to the free 514 * lists. 515 */ 516 static void 517 vm_page_init_page(vm_page_t m, vm_paddr_t pa, int segind) 518 { 519 520 m->object = NULL; 521 m->wire_count = 0; 522 m->busy_lock = VPB_UNBUSIED; 523 m->hold_count = 0; 524 m->flags = 0; 525 m->phys_addr = pa; 526 m->queue = PQ_NONE; 527 m->psind = 0; 528 m->segind = segind; 529 m->order = VM_NFREEORDER; 530 m->pool = VM_FREEPOOL_DEFAULT; 531 m->valid = m->dirty = 0; 532 pmap_page_init(m); 533 } 534 535 /* 536 * vm_page_startup: 537 * 538 * Initializes the resident memory module. Allocates physical memory for 539 * bootstrapping UMA and some data structures that are used to manage 540 * physical pages. Initializes these structures, and populates the free 541 * page queues. 542 */ 543 vm_offset_t 544 vm_page_startup(vm_offset_t vaddr) 545 { 546 struct vm_phys_seg *seg; 547 vm_page_t m; 548 char *list, *listend; 549 vm_offset_t mapped; 550 vm_paddr_t end, high_avail, low_avail, new_end, page_range, size; 551 vm_paddr_t biggestsize, last_pa, pa; 552 u_long pagecount; 553 int biggestone, i, segind; 554 555 biggestsize = 0; 556 biggestone = 0; 557 vaddr = round_page(vaddr); 558 559 for (i = 0; phys_avail[i + 1]; i += 2) { 560 phys_avail[i] = round_page(phys_avail[i]); 561 phys_avail[i + 1] = trunc_page(phys_avail[i + 1]); 562 } 563 for (i = 0; phys_avail[i + 1]; i += 2) { 564 size = phys_avail[i + 1] - phys_avail[i]; 565 if (size > biggestsize) { 566 biggestone = i; 567 biggestsize = size; 568 } 569 } 570 571 end = phys_avail[biggestone+1]; 572 573 /* 574 * Initialize the page and queue locks. 575 */ 576 mtx_init(&vm_domainset_lock, "vm domainset lock", NULL, MTX_DEF); 577 for (i = 0; i < PA_LOCK_COUNT; i++) 578 mtx_init(&pa_lock[i], "vm page", NULL, MTX_DEF); 579 for (i = 0; i < vm_ndomains; i++) 580 vm_page_domain_init(i); 581 582 /* 583 * Allocate memory for use when boot strapping the kernel memory 584 * allocator. Tell UMA how many zones we are going to create 585 * before going fully functional. UMA will add its zones. 586 * 587 * VM startup zones: vmem, vmem_btag, VM OBJECT, RADIX NODE, MAP, 588 * KMAP ENTRY, MAP ENTRY, VMSPACE. 589 */ 590 boot_pages = uma_startup_count(8); 591 592 #ifndef UMA_MD_SMALL_ALLOC 593 /* vmem_startup() calls uma_prealloc(). */ 594 boot_pages += vmem_startup_count(); 595 /* vm_map_startup() calls uma_prealloc(). */ 596 boot_pages += howmany(MAX_KMAP, 597 UMA_SLAB_SPACE / sizeof(struct vm_map)); 598 599 /* 600 * Before going fully functional kmem_init() does allocation 601 * from "KMAP ENTRY" and vmem_create() does allocation from "vmem". 602 */ 603 boot_pages += 2; 604 #endif 605 /* 606 * CTFLAG_RDTUN doesn't work during the early boot process, so we must 607 * manually fetch the value. 608 */ 609 TUNABLE_INT_FETCH("vm.boot_pages", &boot_pages); 610 new_end = end - (boot_pages * UMA_SLAB_SIZE); 611 new_end = trunc_page(new_end); 612 mapped = pmap_map(&vaddr, new_end, end, 613 VM_PROT_READ | VM_PROT_WRITE); 614 bzero((void *)mapped, end - new_end); 615 uma_startup((void *)mapped, boot_pages); 616 617 #ifdef WITNESS 618 end = new_end; 619 new_end = end - round_page(witness_startup_count()); 620 mapped = pmap_map(&vaddr, new_end, end, 621 VM_PROT_READ | VM_PROT_WRITE); 622 bzero((void *)mapped, end - new_end); 623 witness_startup((void *)mapped); 624 #endif 625 626 #if defined(__aarch64__) || defined(__amd64__) || defined(__arm__) || \ 627 defined(__i386__) || defined(__mips__) 628 /* 629 * Allocate a bitmap to indicate that a random physical page 630 * needs to be included in a minidump. 631 * 632 * The amd64 port needs this to indicate which direct map pages 633 * need to be dumped, via calls to dump_add_page()/dump_drop_page(). 634 * 635 * However, i386 still needs this workspace internally within the 636 * minidump code. In theory, they are not needed on i386, but are 637 * included should the sf_buf code decide to use them. 638 */ 639 last_pa = 0; 640 for (i = 0; dump_avail[i + 1] != 0; i += 2) 641 if (dump_avail[i + 1] > last_pa) 642 last_pa = dump_avail[i + 1]; 643 page_range = last_pa / PAGE_SIZE; 644 vm_page_dump_size = round_page(roundup2(page_range, NBBY) / NBBY); 645 new_end -= vm_page_dump_size; 646 vm_page_dump = (void *)(uintptr_t)pmap_map(&vaddr, new_end, 647 new_end + vm_page_dump_size, VM_PROT_READ | VM_PROT_WRITE); 648 bzero((void *)vm_page_dump, vm_page_dump_size); 649 #else 650 (void)last_pa; 651 #endif 652 #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) 653 /* 654 * Include the UMA bootstrap pages and vm_page_dump in a crash dump. 655 * When pmap_map() uses the direct map, they are not automatically 656 * included. 657 */ 658 for (pa = new_end; pa < end; pa += PAGE_SIZE) 659 dump_add_page(pa); 660 #endif 661 phys_avail[biggestone + 1] = new_end; 662 #ifdef __amd64__ 663 /* 664 * Request that the physical pages underlying the message buffer be 665 * included in a crash dump. Since the message buffer is accessed 666 * through the direct map, they are not automatically included. 667 */ 668 pa = DMAP_TO_PHYS((vm_offset_t)msgbufp->msg_ptr); 669 last_pa = pa + round_page(msgbufsize); 670 while (pa < last_pa) { 671 dump_add_page(pa); 672 pa += PAGE_SIZE; 673 } 674 #endif 675 /* 676 * Compute the number of pages of memory that will be available for 677 * use, taking into account the overhead of a page structure per page. 678 * In other words, solve 679 * "available physical memory" - round_page(page_range * 680 * sizeof(struct vm_page)) = page_range * PAGE_SIZE 681 * for page_range. 682 */ 683 low_avail = phys_avail[0]; 684 high_avail = phys_avail[1]; 685 for (i = 0; i < vm_phys_nsegs; i++) { 686 if (vm_phys_segs[i].start < low_avail) 687 low_avail = vm_phys_segs[i].start; 688 if (vm_phys_segs[i].end > high_avail) 689 high_avail = vm_phys_segs[i].end; 690 } 691 /* Skip the first chunk. It is already accounted for. */ 692 for (i = 2; phys_avail[i + 1] != 0; i += 2) { 693 if (phys_avail[i] < low_avail) 694 low_avail = phys_avail[i]; 695 if (phys_avail[i + 1] > high_avail) 696 high_avail = phys_avail[i + 1]; 697 } 698 first_page = low_avail / PAGE_SIZE; 699 #ifdef VM_PHYSSEG_SPARSE 700 size = 0; 701 for (i = 0; i < vm_phys_nsegs; i++) 702 size += vm_phys_segs[i].end - vm_phys_segs[i].start; 703 for (i = 0; phys_avail[i + 1] != 0; i += 2) 704 size += phys_avail[i + 1] - phys_avail[i]; 705 #elif defined(VM_PHYSSEG_DENSE) 706 size = high_avail - low_avail; 707 #else 708 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined." 709 #endif 710 711 #ifdef VM_PHYSSEG_DENSE 712 /* 713 * In the VM_PHYSSEG_DENSE case, the number of pages can account for 714 * the overhead of a page structure per page only if vm_page_array is 715 * allocated from the last physical memory chunk. Otherwise, we must 716 * allocate page structures representing the physical memory 717 * underlying vm_page_array, even though they will not be used. 718 */ 719 if (new_end != high_avail) 720 page_range = size / PAGE_SIZE; 721 else 722 #endif 723 { 724 page_range = size / (PAGE_SIZE + sizeof(struct vm_page)); 725 726 /* 727 * If the partial bytes remaining are large enough for 728 * a page (PAGE_SIZE) without a corresponding 729 * 'struct vm_page', then new_end will contain an 730 * extra page after subtracting the length of the VM 731 * page array. Compensate by subtracting an extra 732 * page from new_end. 733 */ 734 if (size % (PAGE_SIZE + sizeof(struct vm_page)) >= PAGE_SIZE) { 735 if (new_end == high_avail) 736 high_avail -= PAGE_SIZE; 737 new_end -= PAGE_SIZE; 738 } 739 } 740 end = new_end; 741 742 /* 743 * Reserve an unmapped guard page to trap access to vm_page_array[-1]. 744 * However, because this page is allocated from KVM, out-of-bounds 745 * accesses using the direct map will not be trapped. 746 */ 747 vaddr += PAGE_SIZE; 748 749 /* 750 * Allocate physical memory for the page structures, and map it. 751 */ 752 new_end = trunc_page(end - page_range * sizeof(struct vm_page)); 753 mapped = pmap_map(&vaddr, new_end, end, 754 VM_PROT_READ | VM_PROT_WRITE); 755 vm_page_array = (vm_page_t)mapped; 756 vm_page_array_size = page_range; 757 758 #if VM_NRESERVLEVEL > 0 759 /* 760 * Allocate physical memory for the reservation management system's 761 * data structures, and map it. 762 */ 763 if (high_avail == end) 764 high_avail = new_end; 765 new_end = vm_reserv_startup(&vaddr, new_end, high_avail); 766 #endif 767 #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) 768 /* 769 * Include vm_page_array and vm_reserv_array in a crash dump. 770 */ 771 for (pa = new_end; pa < end; pa += PAGE_SIZE) 772 dump_add_page(pa); 773 #endif 774 phys_avail[biggestone + 1] = new_end; 775 776 /* 777 * Add physical memory segments corresponding to the available 778 * physical pages. 779 */ 780 for (i = 0; phys_avail[i + 1] != 0; i += 2) 781 vm_phys_add_seg(phys_avail[i], phys_avail[i + 1]); 782 783 /* 784 * Initialize the physical memory allocator. 785 */ 786 vm_phys_init(); 787 788 /* 789 * Initialize the page structures and add every available page to the 790 * physical memory allocator's free lists. 791 */ 792 vm_cnt.v_page_count = 0; 793 for (segind = 0; segind < vm_phys_nsegs; segind++) { 794 seg = &vm_phys_segs[segind]; 795 for (m = seg->first_page, pa = seg->start; pa < seg->end; 796 m++, pa += PAGE_SIZE) 797 vm_page_init_page(m, pa, segind); 798 799 /* 800 * Add the segment to the free lists only if it is covered by 801 * one of the ranges in phys_avail. Because we've added the 802 * ranges to the vm_phys_segs array, we can assume that each 803 * segment is either entirely contained in one of the ranges, 804 * or doesn't overlap any of them. 805 */ 806 for (i = 0; phys_avail[i + 1] != 0; i += 2) { 807 struct vm_domain *vmd; 808 809 if (seg->start < phys_avail[i] || 810 seg->end > phys_avail[i + 1]) 811 continue; 812 813 m = seg->first_page; 814 pagecount = (u_long)atop(seg->end - seg->start); 815 816 vmd = VM_DOMAIN(seg->domain); 817 vm_domain_free_lock(vmd); 818 vm_phys_free_contig(m, pagecount); 819 vm_domain_free_unlock(vmd); 820 vm_domain_freecnt_inc(vmd, pagecount); 821 vm_cnt.v_page_count += (u_int)pagecount; 822 823 vmd = VM_DOMAIN(seg->domain); 824 vmd->vmd_page_count += (u_int)pagecount; 825 vmd->vmd_segs |= 1UL << m->segind; 826 break; 827 } 828 } 829 830 /* 831 * Remove blacklisted pages from the physical memory allocator. 832 */ 833 TAILQ_INIT(&blacklist_head); 834 vm_page_blacklist_load(&list, &listend); 835 vm_page_blacklist_check(list, listend); 836 837 list = kern_getenv("vm.blacklist"); 838 vm_page_blacklist_check(list, NULL); 839 840 freeenv(list); 841 #if VM_NRESERVLEVEL > 0 842 /* 843 * Initialize the reservation management system. 844 */ 845 vm_reserv_init(); 846 #endif 847 /* 848 * Set an initial domain policy for thread0 so that allocations 849 * can work. 850 */ 851 domainset_zero(); 852 853 return (vaddr); 854 } 855 856 void 857 vm_page_reference(vm_page_t m) 858 { 859 860 vm_page_aflag_set(m, PGA_REFERENCED); 861 } 862 863 /* 864 * vm_page_busy_downgrade: 865 * 866 * Downgrade an exclusive busy page into a single shared busy page. 867 */ 868 void 869 vm_page_busy_downgrade(vm_page_t m) 870 { 871 u_int x; 872 bool locked; 873 874 vm_page_assert_xbusied(m); 875 locked = mtx_owned(vm_page_lockptr(m)); 876 877 for (;;) { 878 x = m->busy_lock; 879 x &= VPB_BIT_WAITERS; 880 if (x != 0 && !locked) 881 vm_page_lock(m); 882 if (atomic_cmpset_rel_int(&m->busy_lock, 883 VPB_SINGLE_EXCLUSIVER | x, VPB_SHARERS_WORD(1))) 884 break; 885 if (x != 0 && !locked) 886 vm_page_unlock(m); 887 } 888 if (x != 0) { 889 wakeup(m); 890 if (!locked) 891 vm_page_unlock(m); 892 } 893 } 894 895 /* 896 * vm_page_sbusied: 897 * 898 * Return a positive value if the page is shared busied, 0 otherwise. 899 */ 900 int 901 vm_page_sbusied(vm_page_t m) 902 { 903 u_int x; 904 905 x = m->busy_lock; 906 return ((x & VPB_BIT_SHARED) != 0 && x != VPB_UNBUSIED); 907 } 908 909 /* 910 * vm_page_sunbusy: 911 * 912 * Shared unbusy a page. 913 */ 914 void 915 vm_page_sunbusy(vm_page_t m) 916 { 917 u_int x; 918 919 vm_page_lock_assert(m, MA_NOTOWNED); 920 vm_page_assert_sbusied(m); 921 922 for (;;) { 923 x = m->busy_lock; 924 if (VPB_SHARERS(x) > 1) { 925 if (atomic_cmpset_int(&m->busy_lock, x, 926 x - VPB_ONE_SHARER)) 927 break; 928 continue; 929 } 930 if ((x & VPB_BIT_WAITERS) == 0) { 931 KASSERT(x == VPB_SHARERS_WORD(1), 932 ("vm_page_sunbusy: invalid lock state")); 933 if (atomic_cmpset_int(&m->busy_lock, 934 VPB_SHARERS_WORD(1), VPB_UNBUSIED)) 935 break; 936 continue; 937 } 938 KASSERT(x == (VPB_SHARERS_WORD(1) | VPB_BIT_WAITERS), 939 ("vm_page_sunbusy: invalid lock state for waiters")); 940 941 vm_page_lock(m); 942 if (!atomic_cmpset_int(&m->busy_lock, x, VPB_UNBUSIED)) { 943 vm_page_unlock(m); 944 continue; 945 } 946 wakeup(m); 947 vm_page_unlock(m); 948 break; 949 } 950 } 951 952 /* 953 * vm_page_busy_sleep: 954 * 955 * Sleep and release the page lock, using the page pointer as wchan. 956 * This is used to implement the hard-path of busying mechanism. 957 * 958 * The given page must be locked. 959 * 960 * If nonshared is true, sleep only if the page is xbusy. 961 */ 962 void 963 vm_page_busy_sleep(vm_page_t m, const char *wmesg, bool nonshared) 964 { 965 u_int x; 966 967 vm_page_assert_locked(m); 968 969 x = m->busy_lock; 970 if (x == VPB_UNBUSIED || (nonshared && (x & VPB_BIT_SHARED) != 0) || 971 ((x & VPB_BIT_WAITERS) == 0 && 972 !atomic_cmpset_int(&m->busy_lock, x, x | VPB_BIT_WAITERS))) { 973 vm_page_unlock(m); 974 return; 975 } 976 msleep(m, vm_page_lockptr(m), PVM | PDROP, wmesg, 0); 977 } 978 979 /* 980 * vm_page_trysbusy: 981 * 982 * Try to shared busy a page. 983 * If the operation succeeds 1 is returned otherwise 0. 984 * The operation never sleeps. 985 */ 986 int 987 vm_page_trysbusy(vm_page_t m) 988 { 989 u_int x; 990 991 for (;;) { 992 x = m->busy_lock; 993 if ((x & VPB_BIT_SHARED) == 0) 994 return (0); 995 if (atomic_cmpset_acq_int(&m->busy_lock, x, x + VPB_ONE_SHARER)) 996 return (1); 997 } 998 } 999 1000 static void 1001 vm_page_xunbusy_locked(vm_page_t m) 1002 { 1003 1004 vm_page_assert_xbusied(m); 1005 vm_page_assert_locked(m); 1006 1007 atomic_store_rel_int(&m->busy_lock, VPB_UNBUSIED); 1008 /* There is a waiter, do wakeup() instead of vm_page_flash(). */ 1009 wakeup(m); 1010 } 1011 1012 void 1013 vm_page_xunbusy_maybelocked(vm_page_t m) 1014 { 1015 bool lockacq; 1016 1017 vm_page_assert_xbusied(m); 1018 1019 /* 1020 * Fast path for unbusy. If it succeeds, we know that there 1021 * are no waiters, so we do not need a wakeup. 1022 */ 1023 if (atomic_cmpset_rel_int(&m->busy_lock, VPB_SINGLE_EXCLUSIVER, 1024 VPB_UNBUSIED)) 1025 return; 1026 1027 lockacq = !mtx_owned(vm_page_lockptr(m)); 1028 if (lockacq) 1029 vm_page_lock(m); 1030 vm_page_xunbusy_locked(m); 1031 if (lockacq) 1032 vm_page_unlock(m); 1033 } 1034 1035 /* 1036 * vm_page_xunbusy_hard: 1037 * 1038 * Called after the first try the exclusive unbusy of a page failed. 1039 * It is assumed that the waiters bit is on. 1040 */ 1041 void 1042 vm_page_xunbusy_hard(vm_page_t m) 1043 { 1044 1045 vm_page_assert_xbusied(m); 1046 1047 vm_page_lock(m); 1048 vm_page_xunbusy_locked(m); 1049 vm_page_unlock(m); 1050 } 1051 1052 /* 1053 * vm_page_flash: 1054 * 1055 * Wakeup anyone waiting for the page. 1056 * The ownership bits do not change. 1057 * 1058 * The given page must be locked. 1059 */ 1060 void 1061 vm_page_flash(vm_page_t m) 1062 { 1063 u_int x; 1064 1065 vm_page_lock_assert(m, MA_OWNED); 1066 1067 for (;;) { 1068 x = m->busy_lock; 1069 if ((x & VPB_BIT_WAITERS) == 0) 1070 return; 1071 if (atomic_cmpset_int(&m->busy_lock, x, 1072 x & (~VPB_BIT_WAITERS))) 1073 break; 1074 } 1075 wakeup(m); 1076 } 1077 1078 /* 1079 * Avoid releasing and reacquiring the same page lock. 1080 */ 1081 void 1082 vm_page_change_lock(vm_page_t m, struct mtx **mtx) 1083 { 1084 struct mtx *mtx1; 1085 1086 mtx1 = vm_page_lockptr(m); 1087 if (*mtx == mtx1) 1088 return; 1089 if (*mtx != NULL) 1090 mtx_unlock(*mtx); 1091 *mtx = mtx1; 1092 mtx_lock(mtx1); 1093 } 1094 1095 /* 1096 * Keep page from being freed by the page daemon 1097 * much of the same effect as wiring, except much lower 1098 * overhead and should be used only for *very* temporary 1099 * holding ("wiring"). 1100 */ 1101 void 1102 vm_page_hold(vm_page_t mem) 1103 { 1104 1105 vm_page_lock_assert(mem, MA_OWNED); 1106 mem->hold_count++; 1107 } 1108 1109 void 1110 vm_page_unhold(vm_page_t mem) 1111 { 1112 1113 vm_page_lock_assert(mem, MA_OWNED); 1114 KASSERT(mem->hold_count >= 1, ("vm_page_unhold: hold count < 0!!!")); 1115 --mem->hold_count; 1116 if (mem->hold_count == 0 && (mem->flags & PG_UNHOLDFREE) != 0) 1117 vm_page_free_toq(mem); 1118 } 1119 1120 /* 1121 * vm_page_unhold_pages: 1122 * 1123 * Unhold each of the pages that is referenced by the given array. 1124 */ 1125 void 1126 vm_page_unhold_pages(vm_page_t *ma, int count) 1127 { 1128 struct mtx *mtx; 1129 1130 mtx = NULL; 1131 for (; count != 0; count--) { 1132 vm_page_change_lock(*ma, &mtx); 1133 vm_page_unhold(*ma); 1134 ma++; 1135 } 1136 if (mtx != NULL) 1137 mtx_unlock(mtx); 1138 } 1139 1140 vm_page_t 1141 PHYS_TO_VM_PAGE(vm_paddr_t pa) 1142 { 1143 vm_page_t m; 1144 1145 #ifdef VM_PHYSSEG_SPARSE 1146 m = vm_phys_paddr_to_vm_page(pa); 1147 if (m == NULL) 1148 m = vm_phys_fictitious_to_vm_page(pa); 1149 return (m); 1150 #elif defined(VM_PHYSSEG_DENSE) 1151 long pi; 1152 1153 pi = atop(pa); 1154 if (pi >= first_page && (pi - first_page) < vm_page_array_size) { 1155 m = &vm_page_array[pi - first_page]; 1156 return (m); 1157 } 1158 return (vm_phys_fictitious_to_vm_page(pa)); 1159 #else 1160 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined." 1161 #endif 1162 } 1163 1164 /* 1165 * vm_page_getfake: 1166 * 1167 * Create a fictitious page with the specified physical address and 1168 * memory attribute. The memory attribute is the only the machine- 1169 * dependent aspect of a fictitious page that must be initialized. 1170 */ 1171 vm_page_t 1172 vm_page_getfake(vm_paddr_t paddr, vm_memattr_t memattr) 1173 { 1174 vm_page_t m; 1175 1176 m = uma_zalloc(fakepg_zone, M_WAITOK | M_ZERO); 1177 vm_page_initfake(m, paddr, memattr); 1178 return (m); 1179 } 1180 1181 void 1182 vm_page_initfake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr) 1183 { 1184 1185 if ((m->flags & PG_FICTITIOUS) != 0) { 1186 /* 1187 * The page's memattr might have changed since the 1188 * previous initialization. Update the pmap to the 1189 * new memattr. 1190 */ 1191 goto memattr; 1192 } 1193 m->phys_addr = paddr; 1194 m->queue = PQ_NONE; 1195 /* Fictitious pages don't use "segind". */ 1196 m->flags = PG_FICTITIOUS; 1197 /* Fictitious pages don't use "order" or "pool". */ 1198 m->oflags = VPO_UNMANAGED; 1199 m->busy_lock = VPB_SINGLE_EXCLUSIVER; 1200 m->wire_count = 1; 1201 pmap_page_init(m); 1202 memattr: 1203 pmap_page_set_memattr(m, memattr); 1204 } 1205 1206 /* 1207 * vm_page_putfake: 1208 * 1209 * Release a fictitious page. 1210 */ 1211 void 1212 vm_page_putfake(vm_page_t m) 1213 { 1214 1215 KASSERT((m->oflags & VPO_UNMANAGED) != 0, ("managed %p", m)); 1216 KASSERT((m->flags & PG_FICTITIOUS) != 0, 1217 ("vm_page_putfake: bad page %p", m)); 1218 uma_zfree(fakepg_zone, m); 1219 } 1220 1221 /* 1222 * vm_page_updatefake: 1223 * 1224 * Update the given fictitious page to the specified physical address and 1225 * memory attribute. 1226 */ 1227 void 1228 vm_page_updatefake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr) 1229 { 1230 1231 KASSERT((m->flags & PG_FICTITIOUS) != 0, 1232 ("vm_page_updatefake: bad page %p", m)); 1233 m->phys_addr = paddr; 1234 pmap_page_set_memattr(m, memattr); 1235 } 1236 1237 /* 1238 * vm_page_free: 1239 * 1240 * Free a page. 1241 */ 1242 void 1243 vm_page_free(vm_page_t m) 1244 { 1245 1246 m->flags &= ~PG_ZERO; 1247 vm_page_free_toq(m); 1248 } 1249 1250 /* 1251 * vm_page_free_zero: 1252 * 1253 * Free a page to the zerod-pages queue 1254 */ 1255 void 1256 vm_page_free_zero(vm_page_t m) 1257 { 1258 1259 m->flags |= PG_ZERO; 1260 vm_page_free_toq(m); 1261 } 1262 1263 /* 1264 * Unbusy and handle the page queueing for a page from a getpages request that 1265 * was optionally read ahead or behind. 1266 */ 1267 void 1268 vm_page_readahead_finish(vm_page_t m) 1269 { 1270 1271 /* We shouldn't put invalid pages on queues. */ 1272 KASSERT(m->valid != 0, ("%s: %p is invalid", __func__, m)); 1273 1274 /* 1275 * Since the page is not the actually needed one, whether it should 1276 * be activated or deactivated is not obvious. Empirical results 1277 * have shown that deactivating the page is usually the best choice, 1278 * unless the page is wanted by another thread. 1279 */ 1280 vm_page_lock(m); 1281 if ((m->busy_lock & VPB_BIT_WAITERS) != 0) 1282 vm_page_activate(m); 1283 else 1284 vm_page_deactivate(m); 1285 vm_page_unlock(m); 1286 vm_page_xunbusy(m); 1287 } 1288 1289 /* 1290 * vm_page_sleep_if_busy: 1291 * 1292 * Sleep and release the page queues lock if the page is busied. 1293 * Returns TRUE if the thread slept. 1294 * 1295 * The given page must be unlocked and object containing it must 1296 * be locked. 1297 */ 1298 int 1299 vm_page_sleep_if_busy(vm_page_t m, const char *msg) 1300 { 1301 vm_object_t obj; 1302 1303 vm_page_lock_assert(m, MA_NOTOWNED); 1304 VM_OBJECT_ASSERT_WLOCKED(m->object); 1305 1306 if (vm_page_busied(m)) { 1307 /* 1308 * The page-specific object must be cached because page 1309 * identity can change during the sleep, causing the 1310 * re-lock of a different object. 1311 * It is assumed that a reference to the object is already 1312 * held by the callers. 1313 */ 1314 obj = m->object; 1315 vm_page_lock(m); 1316 VM_OBJECT_WUNLOCK(obj); 1317 vm_page_busy_sleep(m, msg, false); 1318 VM_OBJECT_WLOCK(obj); 1319 return (TRUE); 1320 } 1321 return (FALSE); 1322 } 1323 1324 /* 1325 * vm_page_dirty_KBI: [ internal use only ] 1326 * 1327 * Set all bits in the page's dirty field. 1328 * 1329 * The object containing the specified page must be locked if the 1330 * call is made from the machine-independent layer. 1331 * 1332 * See vm_page_clear_dirty_mask(). 1333 * 1334 * This function should only be called by vm_page_dirty(). 1335 */ 1336 void 1337 vm_page_dirty_KBI(vm_page_t m) 1338 { 1339 1340 /* Refer to this operation by its public name. */ 1341 KASSERT(m->valid == VM_PAGE_BITS_ALL, 1342 ("vm_page_dirty: page is invalid!")); 1343 m->dirty = VM_PAGE_BITS_ALL; 1344 } 1345 1346 /* 1347 * vm_page_insert: [ internal use only ] 1348 * 1349 * Inserts the given mem entry into the object and object list. 1350 * 1351 * The object must be locked. 1352 */ 1353 int 1354 vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex) 1355 { 1356 vm_page_t mpred; 1357 1358 VM_OBJECT_ASSERT_WLOCKED(object); 1359 mpred = vm_radix_lookup_le(&object->rtree, pindex); 1360 return (vm_page_insert_after(m, object, pindex, mpred)); 1361 } 1362 1363 /* 1364 * vm_page_insert_after: 1365 * 1366 * Inserts the page "m" into the specified object at offset "pindex". 1367 * 1368 * The page "mpred" must immediately precede the offset "pindex" within 1369 * the specified object. 1370 * 1371 * The object must be locked. 1372 */ 1373 static int 1374 vm_page_insert_after(vm_page_t m, vm_object_t object, vm_pindex_t pindex, 1375 vm_page_t mpred) 1376 { 1377 vm_page_t msucc; 1378 1379 VM_OBJECT_ASSERT_WLOCKED(object); 1380 KASSERT(m->object == NULL, 1381 ("vm_page_insert_after: page already inserted")); 1382 if (mpred != NULL) { 1383 KASSERT(mpred->object == object, 1384 ("vm_page_insert_after: object doesn't contain mpred")); 1385 KASSERT(mpred->pindex < pindex, 1386 ("vm_page_insert_after: mpred doesn't precede pindex")); 1387 msucc = TAILQ_NEXT(mpred, listq); 1388 } else 1389 msucc = TAILQ_FIRST(&object->memq); 1390 if (msucc != NULL) 1391 KASSERT(msucc->pindex > pindex, 1392 ("vm_page_insert_after: msucc doesn't succeed pindex")); 1393 1394 /* 1395 * Record the object/offset pair in this page 1396 */ 1397 m->object = object; 1398 m->pindex = pindex; 1399 1400 /* 1401 * Now link into the object's ordered list of backed pages. 1402 */ 1403 if (vm_radix_insert(&object->rtree, m)) { 1404 m->object = NULL; 1405 m->pindex = 0; 1406 return (1); 1407 } 1408 vm_page_insert_radixdone(m, object, mpred); 1409 return (0); 1410 } 1411 1412 /* 1413 * vm_page_insert_radixdone: 1414 * 1415 * Complete page "m" insertion into the specified object after the 1416 * radix trie hooking. 1417 * 1418 * The page "mpred" must precede the offset "m->pindex" within the 1419 * specified object. 1420 * 1421 * The object must be locked. 1422 */ 1423 static void 1424 vm_page_insert_radixdone(vm_page_t m, vm_object_t object, vm_page_t mpred) 1425 { 1426 1427 VM_OBJECT_ASSERT_WLOCKED(object); 1428 KASSERT(object != NULL && m->object == object, 1429 ("vm_page_insert_radixdone: page %p has inconsistent object", m)); 1430 if (mpred != NULL) { 1431 KASSERT(mpred->object == object, 1432 ("vm_page_insert_after: object doesn't contain mpred")); 1433 KASSERT(mpred->pindex < m->pindex, 1434 ("vm_page_insert_after: mpred doesn't precede pindex")); 1435 } 1436 1437 if (mpred != NULL) 1438 TAILQ_INSERT_AFTER(&object->memq, mpred, m, listq); 1439 else 1440 TAILQ_INSERT_HEAD(&object->memq, m, listq); 1441 1442 /* 1443 * Show that the object has one more resident page. 1444 */ 1445 object->resident_page_count++; 1446 1447 /* 1448 * Hold the vnode until the last page is released. 1449 */ 1450 if (object->resident_page_count == 1 && object->type == OBJT_VNODE) 1451 vhold(object->handle); 1452 1453 /* 1454 * Since we are inserting a new and possibly dirty page, 1455 * update the object's OBJ_MIGHTBEDIRTY flag. 1456 */ 1457 if (pmap_page_is_write_mapped(m)) 1458 vm_object_set_writeable_dirty(object); 1459 } 1460 1461 /* 1462 * vm_page_remove: 1463 * 1464 * Removes the specified page from its containing object, but does not 1465 * invalidate any backing storage. 1466 * 1467 * The object must be locked. The page must be locked if it is managed. 1468 */ 1469 void 1470 vm_page_remove(vm_page_t m) 1471 { 1472 vm_object_t object; 1473 vm_page_t mrem; 1474 1475 if ((m->oflags & VPO_UNMANAGED) == 0) 1476 vm_page_assert_locked(m); 1477 if ((object = m->object) == NULL) 1478 return; 1479 VM_OBJECT_ASSERT_WLOCKED(object); 1480 if (vm_page_xbusied(m)) 1481 vm_page_xunbusy_maybelocked(m); 1482 mrem = vm_radix_remove(&object->rtree, m->pindex); 1483 KASSERT(mrem == m, ("removed page %p, expected page %p", mrem, m)); 1484 1485 /* 1486 * Now remove from the object's list of backed pages. 1487 */ 1488 TAILQ_REMOVE(&object->memq, m, listq); 1489 1490 /* 1491 * And show that the object has one fewer resident page. 1492 */ 1493 object->resident_page_count--; 1494 1495 /* 1496 * The vnode may now be recycled. 1497 */ 1498 if (object->resident_page_count == 0 && object->type == OBJT_VNODE) 1499 vdrop(object->handle); 1500 1501 m->object = NULL; 1502 } 1503 1504 /* 1505 * vm_page_lookup: 1506 * 1507 * Returns the page associated with the object/offset 1508 * pair specified; if none is found, NULL is returned. 1509 * 1510 * The object must be locked. 1511 */ 1512 vm_page_t 1513 vm_page_lookup(vm_object_t object, vm_pindex_t pindex) 1514 { 1515 1516 VM_OBJECT_ASSERT_LOCKED(object); 1517 return (vm_radix_lookup(&object->rtree, pindex)); 1518 } 1519 1520 /* 1521 * vm_page_find_least: 1522 * 1523 * Returns the page associated with the object with least pindex 1524 * greater than or equal to the parameter pindex, or NULL. 1525 * 1526 * The object must be locked. 1527 */ 1528 vm_page_t 1529 vm_page_find_least(vm_object_t object, vm_pindex_t pindex) 1530 { 1531 vm_page_t m; 1532 1533 VM_OBJECT_ASSERT_LOCKED(object); 1534 if ((m = TAILQ_FIRST(&object->memq)) != NULL && m->pindex < pindex) 1535 m = vm_radix_lookup_ge(&object->rtree, pindex); 1536 return (m); 1537 } 1538 1539 /* 1540 * Returns the given page's successor (by pindex) within the object if it is 1541 * resident; if none is found, NULL is returned. 1542 * 1543 * The object must be locked. 1544 */ 1545 vm_page_t 1546 vm_page_next(vm_page_t m) 1547 { 1548 vm_page_t next; 1549 1550 VM_OBJECT_ASSERT_LOCKED(m->object); 1551 if ((next = TAILQ_NEXT(m, listq)) != NULL) { 1552 MPASS(next->object == m->object); 1553 if (next->pindex != m->pindex + 1) 1554 next = NULL; 1555 } 1556 return (next); 1557 } 1558 1559 /* 1560 * Returns the given page's predecessor (by pindex) within the object if it is 1561 * resident; if none is found, NULL is returned. 1562 * 1563 * The object must be locked. 1564 */ 1565 vm_page_t 1566 vm_page_prev(vm_page_t m) 1567 { 1568 vm_page_t prev; 1569 1570 VM_OBJECT_ASSERT_LOCKED(m->object); 1571 if ((prev = TAILQ_PREV(m, pglist, listq)) != NULL) { 1572 MPASS(prev->object == m->object); 1573 if (prev->pindex != m->pindex - 1) 1574 prev = NULL; 1575 } 1576 return (prev); 1577 } 1578 1579 /* 1580 * Uses the page mnew as a replacement for an existing page at index 1581 * pindex which must be already present in the object. 1582 * 1583 * The existing page must not be on a paging queue. 1584 */ 1585 vm_page_t 1586 vm_page_replace(vm_page_t mnew, vm_object_t object, vm_pindex_t pindex) 1587 { 1588 vm_page_t mold; 1589 1590 VM_OBJECT_ASSERT_WLOCKED(object); 1591 KASSERT(mnew->object == NULL, 1592 ("vm_page_replace: page %p already in object", mnew)); 1593 KASSERT(mnew->queue == PQ_NONE, 1594 ("vm_page_replace: new page %p is on a paging queue", mnew)); 1595 1596 /* 1597 * This function mostly follows vm_page_insert() and 1598 * vm_page_remove() without the radix, object count and vnode 1599 * dance. Double check such functions for more comments. 1600 */ 1601 1602 mnew->object = object; 1603 mnew->pindex = pindex; 1604 mold = vm_radix_replace(&object->rtree, mnew); 1605 KASSERT(mold->queue == PQ_NONE, 1606 ("vm_page_replace: old page %p is on a paging queue", mold)); 1607 1608 /* Keep the resident page list in sorted order. */ 1609 TAILQ_INSERT_AFTER(&object->memq, mold, mnew, listq); 1610 TAILQ_REMOVE(&object->memq, mold, listq); 1611 1612 mold->object = NULL; 1613 vm_page_xunbusy_maybelocked(mold); 1614 1615 /* 1616 * The object's resident_page_count does not change because we have 1617 * swapped one page for another, but OBJ_MIGHTBEDIRTY. 1618 */ 1619 if (pmap_page_is_write_mapped(mnew)) 1620 vm_object_set_writeable_dirty(object); 1621 return (mold); 1622 } 1623 1624 /* 1625 * vm_page_rename: 1626 * 1627 * Move the given memory entry from its 1628 * current object to the specified target object/offset. 1629 * 1630 * Note: swap associated with the page must be invalidated by the move. We 1631 * have to do this for several reasons: (1) we aren't freeing the 1632 * page, (2) we are dirtying the page, (3) the VM system is probably 1633 * moving the page from object A to B, and will then later move 1634 * the backing store from A to B and we can't have a conflict. 1635 * 1636 * Note: we *always* dirty the page. It is necessary both for the 1637 * fact that we moved it, and because we may be invalidating 1638 * swap. 1639 * 1640 * The objects must be locked. 1641 */ 1642 int 1643 vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex) 1644 { 1645 vm_page_t mpred; 1646 vm_pindex_t opidx; 1647 1648 VM_OBJECT_ASSERT_WLOCKED(new_object); 1649 1650 mpred = vm_radix_lookup_le(&new_object->rtree, new_pindex); 1651 KASSERT(mpred == NULL || mpred->pindex != new_pindex, 1652 ("vm_page_rename: pindex already renamed")); 1653 1654 /* 1655 * Create a custom version of vm_page_insert() which does not depend 1656 * by m_prev and can cheat on the implementation aspects of the 1657 * function. 1658 */ 1659 opidx = m->pindex; 1660 m->pindex = new_pindex; 1661 if (vm_radix_insert(&new_object->rtree, m)) { 1662 m->pindex = opidx; 1663 return (1); 1664 } 1665 1666 /* 1667 * The operation cannot fail anymore. The removal must happen before 1668 * the listq iterator is tainted. 1669 */ 1670 m->pindex = opidx; 1671 vm_page_lock(m); 1672 vm_page_remove(m); 1673 1674 /* Return back to the new pindex to complete vm_page_insert(). */ 1675 m->pindex = new_pindex; 1676 m->object = new_object; 1677 vm_page_unlock(m); 1678 vm_page_insert_radixdone(m, new_object, mpred); 1679 vm_page_dirty(m); 1680 return (0); 1681 } 1682 1683 /* 1684 * vm_page_alloc: 1685 * 1686 * Allocate and return a page that is associated with the specified 1687 * object and offset pair. By default, this page is exclusive busied. 1688 * 1689 * The caller must always specify an allocation class. 1690 * 1691 * allocation classes: 1692 * VM_ALLOC_NORMAL normal process request 1693 * VM_ALLOC_SYSTEM system *really* needs a page 1694 * VM_ALLOC_INTERRUPT interrupt time request 1695 * 1696 * optional allocation flags: 1697 * VM_ALLOC_COUNT(number) the number of additional pages that the caller 1698 * intends to allocate 1699 * VM_ALLOC_NOBUSY do not exclusive busy the page 1700 * VM_ALLOC_NODUMP do not include the page in a kernel core dump 1701 * VM_ALLOC_NOOBJ page is not associated with an object and 1702 * should not be exclusive busy 1703 * VM_ALLOC_SBUSY shared busy the allocated page 1704 * VM_ALLOC_WIRED wire the allocated page 1705 * VM_ALLOC_ZERO prefer a zeroed page 1706 */ 1707 vm_page_t 1708 vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req) 1709 { 1710 1711 return (vm_page_alloc_after(object, pindex, req, object != NULL ? 1712 vm_radix_lookup_le(&object->rtree, pindex) : NULL)); 1713 } 1714 1715 vm_page_t 1716 vm_page_alloc_domain(vm_object_t object, vm_pindex_t pindex, int domain, 1717 int req) 1718 { 1719 1720 return (vm_page_alloc_domain_after(object, pindex, domain, req, 1721 object != NULL ? vm_radix_lookup_le(&object->rtree, pindex) : 1722 NULL)); 1723 } 1724 1725 /* 1726 * Allocate a page in the specified object with the given page index. To 1727 * optimize insertion of the page into the object, the caller must also specifiy 1728 * the resident page in the object with largest index smaller than the given 1729 * page index, or NULL if no such page exists. 1730 */ 1731 vm_page_t 1732 vm_page_alloc_after(vm_object_t object, vm_pindex_t pindex, 1733 int req, vm_page_t mpred) 1734 { 1735 struct vm_domainset_iter di; 1736 vm_page_t m; 1737 int domain; 1738 1739 vm_domainset_iter_page_init(&di, object, pindex, &domain, &req); 1740 do { 1741 m = vm_page_alloc_domain_after(object, pindex, domain, req, 1742 mpred); 1743 if (m != NULL) 1744 break; 1745 } while (vm_domainset_iter_page(&di, &domain, &req) == 0); 1746 1747 return (m); 1748 } 1749 1750 /* 1751 * Returns true if the number of free pages exceeds the minimum 1752 * for the request class and false otherwise. 1753 */ 1754 int 1755 vm_domain_allocate(struct vm_domain *vmd, int req, int npages) 1756 { 1757 u_int limit, old, new; 1758 1759 req = req & VM_ALLOC_CLASS_MASK; 1760 1761 /* 1762 * The page daemon is allowed to dig deeper into the free page list. 1763 */ 1764 if (curproc == pageproc && req != VM_ALLOC_INTERRUPT) 1765 req = VM_ALLOC_SYSTEM; 1766 if (req == VM_ALLOC_INTERRUPT) 1767 limit = 0; 1768 else if (req == VM_ALLOC_SYSTEM) 1769 limit = vmd->vmd_interrupt_free_min; 1770 else 1771 limit = vmd->vmd_free_reserved; 1772 1773 /* 1774 * Attempt to reserve the pages. Fail if we're below the limit. 1775 */ 1776 limit += npages; 1777 old = vmd->vmd_free_count; 1778 do { 1779 if (old < limit) 1780 return (0); 1781 new = old - npages; 1782 } while (atomic_fcmpset_int(&vmd->vmd_free_count, &old, new) == 0); 1783 1784 /* Wake the page daemon if we've crossed the threshold. */ 1785 if (vm_paging_needed(vmd, new) && !vm_paging_needed(vmd, old)) 1786 pagedaemon_wakeup(vmd->vmd_domain); 1787 1788 /* Only update bitsets on transitions. */ 1789 if ((old >= vmd->vmd_free_min && new < vmd->vmd_free_min) || 1790 (old >= vmd->vmd_free_severe && new < vmd->vmd_free_severe)) 1791 vm_domain_set(vmd); 1792 1793 return (1); 1794 } 1795 1796 vm_page_t 1797 vm_page_alloc_domain_after(vm_object_t object, vm_pindex_t pindex, int domain, 1798 int req, vm_page_t mpred) 1799 { 1800 struct vm_domain *vmd; 1801 vm_page_t m; 1802 int flags; 1803 1804 KASSERT((object != NULL) == ((req & VM_ALLOC_NOOBJ) == 0) && 1805 (object != NULL || (req & VM_ALLOC_SBUSY) == 0) && 1806 ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) != 1807 (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)), 1808 ("inconsistent object(%p)/req(%x)", object, req)); 1809 KASSERT(object == NULL || (req & VM_ALLOC_WAITOK) == 0, 1810 ("Can't sleep and retry object insertion.")); 1811 KASSERT(mpred == NULL || mpred->pindex < pindex, 1812 ("mpred %p doesn't precede pindex 0x%jx", mpred, 1813 (uintmax_t)pindex)); 1814 if (object != NULL) 1815 VM_OBJECT_ASSERT_WLOCKED(object); 1816 1817 again: 1818 m = NULL; 1819 #if VM_NRESERVLEVEL > 0 1820 /* 1821 * Can we allocate the page from a reservation? 1822 */ 1823 if (vm_object_reserv(object) && 1824 ((m = vm_reserv_extend(req, object, pindex, domain, mpred)) != NULL || 1825 (m = vm_reserv_alloc_page(req, object, pindex, domain, mpred)) != NULL)) { 1826 domain = vm_phys_domain(m); 1827 vmd = VM_DOMAIN(domain); 1828 goto found; 1829 } 1830 #endif 1831 vmd = VM_DOMAIN(domain); 1832 if (object != NULL && vmd->vmd_pgcache != NULL) { 1833 m = uma_zalloc(vmd->vmd_pgcache, M_NOWAIT); 1834 if (m != NULL) 1835 goto found; 1836 } 1837 if (vm_domain_allocate(vmd, req, 1)) { 1838 /* 1839 * If not, allocate it from the free page queues. 1840 */ 1841 vm_domain_free_lock(vmd); 1842 m = vm_phys_alloc_pages(domain, object != NULL ? 1843 VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT, 0); 1844 vm_domain_free_unlock(vmd); 1845 if (m == NULL) { 1846 vm_domain_freecnt_inc(vmd, 1); 1847 #if VM_NRESERVLEVEL > 0 1848 if (vm_reserv_reclaim_inactive(domain)) 1849 goto again; 1850 #endif 1851 } 1852 } 1853 if (m == NULL) { 1854 /* 1855 * Not allocatable, give up. 1856 */ 1857 if (vm_domain_alloc_fail(vmd, object, req)) 1858 goto again; 1859 return (NULL); 1860 } 1861 1862 /* 1863 * At this point we had better have found a good page. 1864 */ 1865 KASSERT(m != NULL, ("missing page")); 1866 1867 found: 1868 vm_page_dequeue(m); 1869 vm_page_alloc_check(m); 1870 1871 /* 1872 * Initialize the page. Only the PG_ZERO flag is inherited. 1873 */ 1874 flags = 0; 1875 if ((req & VM_ALLOC_ZERO) != 0) 1876 flags = PG_ZERO; 1877 flags &= m->flags; 1878 if ((req & VM_ALLOC_NODUMP) != 0) 1879 flags |= PG_NODUMP; 1880 m->flags = flags; 1881 m->aflags = 0; 1882 m->oflags = object == NULL || (object->flags & OBJ_UNMANAGED) != 0 ? 1883 VPO_UNMANAGED : 0; 1884 m->busy_lock = VPB_UNBUSIED; 1885 if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ | VM_ALLOC_SBUSY)) == 0) 1886 m->busy_lock = VPB_SINGLE_EXCLUSIVER; 1887 if ((req & VM_ALLOC_SBUSY) != 0) 1888 m->busy_lock = VPB_SHARERS_WORD(1); 1889 if (req & VM_ALLOC_WIRED) { 1890 /* 1891 * The page lock is not required for wiring a page until that 1892 * page is inserted into the object. 1893 */ 1894 vm_wire_add(1); 1895 m->wire_count = 1; 1896 } 1897 m->act_count = 0; 1898 1899 if (object != NULL) { 1900 if (vm_page_insert_after(m, object, pindex, mpred)) { 1901 if (req & VM_ALLOC_WIRED) { 1902 vm_wire_sub(1); 1903 m->wire_count = 0; 1904 } 1905 KASSERT(m->object == NULL, ("page %p has object", m)); 1906 m->oflags = VPO_UNMANAGED; 1907 m->busy_lock = VPB_UNBUSIED; 1908 /* Don't change PG_ZERO. */ 1909 vm_page_free_toq(m); 1910 if (req & VM_ALLOC_WAITFAIL) { 1911 VM_OBJECT_WUNLOCK(object); 1912 vm_radix_wait(); 1913 VM_OBJECT_WLOCK(object); 1914 } 1915 return (NULL); 1916 } 1917 1918 /* Ignore device objects; the pager sets "memattr" for them. */ 1919 if (object->memattr != VM_MEMATTR_DEFAULT && 1920 (object->flags & OBJ_FICTITIOUS) == 0) 1921 pmap_page_set_memattr(m, object->memattr); 1922 } else 1923 m->pindex = pindex; 1924 1925 return (m); 1926 } 1927 1928 /* 1929 * vm_page_alloc_contig: 1930 * 1931 * Allocate a contiguous set of physical pages of the given size "npages" 1932 * from the free lists. All of the physical pages must be at or above 1933 * the given physical address "low" and below the given physical address 1934 * "high". The given value "alignment" determines the alignment of the 1935 * first physical page in the set. If the given value "boundary" is 1936 * non-zero, then the set of physical pages cannot cross any physical 1937 * address boundary that is a multiple of that value. Both "alignment" 1938 * and "boundary" must be a power of two. 1939 * 1940 * If the specified memory attribute, "memattr", is VM_MEMATTR_DEFAULT, 1941 * then the memory attribute setting for the physical pages is configured 1942 * to the object's memory attribute setting. Otherwise, the memory 1943 * attribute setting for the physical pages is configured to "memattr", 1944 * overriding the object's memory attribute setting. However, if the 1945 * object's memory attribute setting is not VM_MEMATTR_DEFAULT, then the 1946 * memory attribute setting for the physical pages cannot be configured 1947 * to VM_MEMATTR_DEFAULT. 1948 * 1949 * The specified object may not contain fictitious pages. 1950 * 1951 * The caller must always specify an allocation class. 1952 * 1953 * allocation classes: 1954 * VM_ALLOC_NORMAL normal process request 1955 * VM_ALLOC_SYSTEM system *really* needs a page 1956 * VM_ALLOC_INTERRUPT interrupt time request 1957 * 1958 * optional allocation flags: 1959 * VM_ALLOC_NOBUSY do not exclusive busy the page 1960 * VM_ALLOC_NODUMP do not include the page in a kernel core dump 1961 * VM_ALLOC_NOOBJ page is not associated with an object and 1962 * should not be exclusive busy 1963 * VM_ALLOC_SBUSY shared busy the allocated page 1964 * VM_ALLOC_WIRED wire the allocated page 1965 * VM_ALLOC_ZERO prefer a zeroed page 1966 */ 1967 vm_page_t 1968 vm_page_alloc_contig(vm_object_t object, vm_pindex_t pindex, int req, 1969 u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment, 1970 vm_paddr_t boundary, vm_memattr_t memattr) 1971 { 1972 struct vm_domainset_iter di; 1973 vm_page_t m; 1974 int domain; 1975 1976 vm_domainset_iter_page_init(&di, object, pindex, &domain, &req); 1977 do { 1978 m = vm_page_alloc_contig_domain(object, pindex, domain, req, 1979 npages, low, high, alignment, boundary, memattr); 1980 if (m != NULL) 1981 break; 1982 } while (vm_domainset_iter_page(&di, &domain, &req) == 0); 1983 1984 return (m); 1985 } 1986 1987 vm_page_t 1988 vm_page_alloc_contig_domain(vm_object_t object, vm_pindex_t pindex, int domain, 1989 int req, u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment, 1990 vm_paddr_t boundary, vm_memattr_t memattr) 1991 { 1992 struct vm_domain *vmd; 1993 vm_page_t m, m_ret, mpred; 1994 u_int busy_lock, flags, oflags; 1995 1996 mpred = NULL; /* XXX: pacify gcc */ 1997 KASSERT((object != NULL) == ((req & VM_ALLOC_NOOBJ) == 0) && 1998 (object != NULL || (req & VM_ALLOC_SBUSY) == 0) && 1999 ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) != 2000 (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)), 2001 ("vm_page_alloc_contig: inconsistent object(%p)/req(%x)", object, 2002 req)); 2003 KASSERT(object == NULL || (req & VM_ALLOC_WAITOK) == 0, 2004 ("Can't sleep and retry object insertion.")); 2005 if (object != NULL) { 2006 VM_OBJECT_ASSERT_WLOCKED(object); 2007 KASSERT((object->flags & OBJ_FICTITIOUS) == 0, 2008 ("vm_page_alloc_contig: object %p has fictitious pages", 2009 object)); 2010 } 2011 KASSERT(npages > 0, ("vm_page_alloc_contig: npages is zero")); 2012 2013 if (object != NULL) { 2014 mpred = vm_radix_lookup_le(&object->rtree, pindex); 2015 KASSERT(mpred == NULL || mpred->pindex != pindex, 2016 ("vm_page_alloc_contig: pindex already allocated")); 2017 } 2018 2019 /* 2020 * Can we allocate the pages without the number of free pages falling 2021 * below the lower bound for the allocation class? 2022 */ 2023 again: 2024 #if VM_NRESERVLEVEL > 0 2025 /* 2026 * Can we allocate the pages from a reservation? 2027 */ 2028 if (vm_object_reserv(object) && 2029 ((m_ret = vm_reserv_extend_contig(req, object, pindex, domain, 2030 npages, low, high, alignment, boundary, mpred)) != NULL || 2031 (m_ret = vm_reserv_alloc_contig(req, object, pindex, domain, 2032 npages, low, high, alignment, boundary, mpred)) != NULL)) { 2033 domain = vm_phys_domain(m_ret); 2034 vmd = VM_DOMAIN(domain); 2035 goto found; 2036 } 2037 #endif 2038 m_ret = NULL; 2039 vmd = VM_DOMAIN(domain); 2040 if (vm_domain_allocate(vmd, req, npages)) { 2041 /* 2042 * allocate them from the free page queues. 2043 */ 2044 vm_domain_free_lock(vmd); 2045 m_ret = vm_phys_alloc_contig(domain, npages, low, high, 2046 alignment, boundary); 2047 vm_domain_free_unlock(vmd); 2048 if (m_ret == NULL) { 2049 vm_domain_freecnt_inc(vmd, npages); 2050 #if VM_NRESERVLEVEL > 0 2051 if (vm_reserv_reclaim_contig(domain, npages, low, 2052 high, alignment, boundary)) 2053 goto again; 2054 #endif 2055 } 2056 } 2057 if (m_ret == NULL) { 2058 if (vm_domain_alloc_fail(vmd, object, req)) 2059 goto again; 2060 return (NULL); 2061 } 2062 #if VM_NRESERVLEVEL > 0 2063 found: 2064 #endif 2065 for (m = m_ret; m < &m_ret[npages]; m++) { 2066 vm_page_dequeue(m); 2067 vm_page_alloc_check(m); 2068 } 2069 2070 /* 2071 * Initialize the pages. Only the PG_ZERO flag is inherited. 2072 */ 2073 flags = 0; 2074 if ((req & VM_ALLOC_ZERO) != 0) 2075 flags = PG_ZERO; 2076 if ((req & VM_ALLOC_NODUMP) != 0) 2077 flags |= PG_NODUMP; 2078 oflags = object == NULL || (object->flags & OBJ_UNMANAGED) != 0 ? 2079 VPO_UNMANAGED : 0; 2080 busy_lock = VPB_UNBUSIED; 2081 if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ | VM_ALLOC_SBUSY)) == 0) 2082 busy_lock = VPB_SINGLE_EXCLUSIVER; 2083 if ((req & VM_ALLOC_SBUSY) != 0) 2084 busy_lock = VPB_SHARERS_WORD(1); 2085 if ((req & VM_ALLOC_WIRED) != 0) 2086 vm_wire_add(npages); 2087 if (object != NULL) { 2088 if (object->memattr != VM_MEMATTR_DEFAULT && 2089 memattr == VM_MEMATTR_DEFAULT) 2090 memattr = object->memattr; 2091 } 2092 for (m = m_ret; m < &m_ret[npages]; m++) { 2093 m->aflags = 0; 2094 m->flags = (m->flags | PG_NODUMP) & flags; 2095 m->busy_lock = busy_lock; 2096 if ((req & VM_ALLOC_WIRED) != 0) 2097 m->wire_count = 1; 2098 m->act_count = 0; 2099 m->oflags = oflags; 2100 if (object != NULL) { 2101 if (vm_page_insert_after(m, object, pindex, mpred)) { 2102 if ((req & VM_ALLOC_WIRED) != 0) 2103 vm_wire_sub(npages); 2104 KASSERT(m->object == NULL, 2105 ("page %p has object", m)); 2106 mpred = m; 2107 for (m = m_ret; m < &m_ret[npages]; m++) { 2108 if (m <= mpred && 2109 (req & VM_ALLOC_WIRED) != 0) 2110 m->wire_count = 0; 2111 m->oflags = VPO_UNMANAGED; 2112 m->busy_lock = VPB_UNBUSIED; 2113 /* Don't change PG_ZERO. */ 2114 vm_page_free_toq(m); 2115 } 2116 if (req & VM_ALLOC_WAITFAIL) { 2117 VM_OBJECT_WUNLOCK(object); 2118 vm_radix_wait(); 2119 VM_OBJECT_WLOCK(object); 2120 } 2121 return (NULL); 2122 } 2123 mpred = m; 2124 } else 2125 m->pindex = pindex; 2126 if (memattr != VM_MEMATTR_DEFAULT) 2127 pmap_page_set_memattr(m, memattr); 2128 pindex++; 2129 } 2130 return (m_ret); 2131 } 2132 2133 /* 2134 * Check a page that has been freshly dequeued from a freelist. 2135 */ 2136 static void 2137 vm_page_alloc_check(vm_page_t m) 2138 { 2139 2140 KASSERT(m->object == NULL, ("page %p has object", m)); 2141 KASSERT(m->queue == PQ_NONE, 2142 ("page %p has unexpected queue %d", m, m->queue)); 2143 KASSERT(!vm_page_held(m), ("page %p is held", m)); 2144 KASSERT(!vm_page_busied(m), ("page %p is busy", m)); 2145 KASSERT(m->dirty == 0, ("page %p is dirty", m)); 2146 KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT, 2147 ("page %p has unexpected memattr %d", 2148 m, pmap_page_get_memattr(m))); 2149 KASSERT(m->valid == 0, ("free page %p is valid", m)); 2150 } 2151 2152 /* 2153 * vm_page_alloc_freelist: 2154 * 2155 * Allocate a physical page from the specified free page list. 2156 * 2157 * The caller must always specify an allocation class. 2158 * 2159 * allocation classes: 2160 * VM_ALLOC_NORMAL normal process request 2161 * VM_ALLOC_SYSTEM system *really* needs a page 2162 * VM_ALLOC_INTERRUPT interrupt time request 2163 * 2164 * optional allocation flags: 2165 * VM_ALLOC_COUNT(number) the number of additional pages that the caller 2166 * intends to allocate 2167 * VM_ALLOC_WIRED wire the allocated page 2168 * VM_ALLOC_ZERO prefer a zeroed page 2169 */ 2170 vm_page_t 2171 vm_page_alloc_freelist(int freelist, int req) 2172 { 2173 struct vm_domainset_iter di; 2174 vm_page_t m; 2175 int domain; 2176 2177 vm_domainset_iter_page_init(&di, NULL, 0, &domain, &req); 2178 do { 2179 m = vm_page_alloc_freelist_domain(domain, freelist, req); 2180 if (m != NULL) 2181 break; 2182 } while (vm_domainset_iter_page(&di, &domain, &req) == 0); 2183 2184 return (m); 2185 } 2186 2187 vm_page_t 2188 vm_page_alloc_freelist_domain(int domain, int freelist, int req) 2189 { 2190 struct vm_domain *vmd; 2191 vm_page_t m; 2192 u_int flags; 2193 2194 m = NULL; 2195 vmd = VM_DOMAIN(domain); 2196 again: 2197 if (vm_domain_allocate(vmd, req, 1)) { 2198 vm_domain_free_lock(vmd); 2199 m = vm_phys_alloc_freelist_pages(domain, freelist, 2200 VM_FREEPOOL_DIRECT, 0); 2201 vm_domain_free_unlock(vmd); 2202 if (m == NULL) 2203 vm_domain_freecnt_inc(vmd, 1); 2204 } 2205 if (m == NULL) { 2206 if (vm_domain_alloc_fail(vmd, NULL, req)) 2207 goto again; 2208 return (NULL); 2209 } 2210 vm_page_dequeue(m); 2211 vm_page_alloc_check(m); 2212 2213 /* 2214 * Initialize the page. Only the PG_ZERO flag is inherited. 2215 */ 2216 m->aflags = 0; 2217 flags = 0; 2218 if ((req & VM_ALLOC_ZERO) != 0) 2219 flags = PG_ZERO; 2220 m->flags &= flags; 2221 if ((req & VM_ALLOC_WIRED) != 0) { 2222 /* 2223 * The page lock is not required for wiring a page that does 2224 * not belong to an object. 2225 */ 2226 vm_wire_add(1); 2227 m->wire_count = 1; 2228 } 2229 /* Unmanaged pages don't use "act_count". */ 2230 m->oflags = VPO_UNMANAGED; 2231 return (m); 2232 } 2233 2234 static int 2235 vm_page_import(void *arg, void **store, int cnt, int domain, int flags) 2236 { 2237 struct vm_domain *vmd; 2238 int i; 2239 2240 vmd = arg; 2241 /* Only import if we can bring in a full bucket. */ 2242 if (cnt == 1 || !vm_domain_allocate(vmd, VM_ALLOC_NORMAL, cnt)) 2243 return (0); 2244 domain = vmd->vmd_domain; 2245 vm_domain_free_lock(vmd); 2246 i = vm_phys_alloc_npages(domain, VM_FREEPOOL_DEFAULT, cnt, 2247 (vm_page_t *)store); 2248 vm_domain_free_unlock(vmd); 2249 if (cnt != i) 2250 vm_domain_freecnt_inc(vmd, cnt - i); 2251 2252 return (i); 2253 } 2254 2255 static void 2256 vm_page_release(void *arg, void **store, int cnt) 2257 { 2258 struct vm_domain *vmd; 2259 vm_page_t m; 2260 int i; 2261 2262 vmd = arg; 2263 vm_domain_free_lock(vmd); 2264 for (i = 0; i < cnt; i++) { 2265 m = (vm_page_t)store[i]; 2266 vm_phys_free_pages(m, 0); 2267 } 2268 vm_domain_free_unlock(vmd); 2269 vm_domain_freecnt_inc(vmd, cnt); 2270 } 2271 2272 #define VPSC_ANY 0 /* No restrictions. */ 2273 #define VPSC_NORESERV 1 /* Skip reservations; implies VPSC_NOSUPER. */ 2274 #define VPSC_NOSUPER 2 /* Skip superpages. */ 2275 2276 /* 2277 * vm_page_scan_contig: 2278 * 2279 * Scan vm_page_array[] between the specified entries "m_start" and 2280 * "m_end" for a run of contiguous physical pages that satisfy the 2281 * specified conditions, and return the lowest page in the run. The 2282 * specified "alignment" determines the alignment of the lowest physical 2283 * page in the run. If the specified "boundary" is non-zero, then the 2284 * run of physical pages cannot span a physical address that is a 2285 * multiple of "boundary". 2286 * 2287 * "m_end" is never dereferenced, so it need not point to a vm_page 2288 * structure within vm_page_array[]. 2289 * 2290 * "npages" must be greater than zero. "m_start" and "m_end" must not 2291 * span a hole (or discontiguity) in the physical address space. Both 2292 * "alignment" and "boundary" must be a power of two. 2293 */ 2294 vm_page_t 2295 vm_page_scan_contig(u_long npages, vm_page_t m_start, vm_page_t m_end, 2296 u_long alignment, vm_paddr_t boundary, int options) 2297 { 2298 struct mtx *m_mtx; 2299 vm_object_t object; 2300 vm_paddr_t pa; 2301 vm_page_t m, m_run; 2302 #if VM_NRESERVLEVEL > 0 2303 int level; 2304 #endif 2305 int m_inc, order, run_ext, run_len; 2306 2307 KASSERT(npages > 0, ("npages is 0")); 2308 KASSERT(powerof2(alignment), ("alignment is not a power of 2")); 2309 KASSERT(powerof2(boundary), ("boundary is not a power of 2")); 2310 m_run = NULL; 2311 run_len = 0; 2312 m_mtx = NULL; 2313 for (m = m_start; m < m_end && run_len < npages; m += m_inc) { 2314 KASSERT((m->flags & PG_MARKER) == 0, 2315 ("page %p is PG_MARKER", m)); 2316 KASSERT((m->flags & PG_FICTITIOUS) == 0 || m->wire_count == 1, 2317 ("fictitious page %p has invalid wire count", m)); 2318 2319 /* 2320 * If the current page would be the start of a run, check its 2321 * physical address against the end, alignment, and boundary 2322 * conditions. If it doesn't satisfy these conditions, either 2323 * terminate the scan or advance to the next page that 2324 * satisfies the failed condition. 2325 */ 2326 if (run_len == 0) { 2327 KASSERT(m_run == NULL, ("m_run != NULL")); 2328 if (m + npages > m_end) 2329 break; 2330 pa = VM_PAGE_TO_PHYS(m); 2331 if ((pa & (alignment - 1)) != 0) { 2332 m_inc = atop(roundup2(pa, alignment) - pa); 2333 continue; 2334 } 2335 if (rounddown2(pa ^ (pa + ptoa(npages) - 1), 2336 boundary) != 0) { 2337 m_inc = atop(roundup2(pa, boundary) - pa); 2338 continue; 2339 } 2340 } else 2341 KASSERT(m_run != NULL, ("m_run == NULL")); 2342 2343 vm_page_change_lock(m, &m_mtx); 2344 m_inc = 1; 2345 retry: 2346 if (vm_page_held(m)) 2347 run_ext = 0; 2348 #if VM_NRESERVLEVEL > 0 2349 else if ((level = vm_reserv_level(m)) >= 0 && 2350 (options & VPSC_NORESERV) != 0) { 2351 run_ext = 0; 2352 /* Advance to the end of the reservation. */ 2353 pa = VM_PAGE_TO_PHYS(m); 2354 m_inc = atop(roundup2(pa + 1, vm_reserv_size(level)) - 2355 pa); 2356 } 2357 #endif 2358 else if ((object = m->object) != NULL) { 2359 /* 2360 * The page is considered eligible for relocation if 2361 * and only if it could be laundered or reclaimed by 2362 * the page daemon. 2363 */ 2364 if (!VM_OBJECT_TRYRLOCK(object)) { 2365 mtx_unlock(m_mtx); 2366 VM_OBJECT_RLOCK(object); 2367 mtx_lock(m_mtx); 2368 if (m->object != object) { 2369 /* 2370 * The page may have been freed. 2371 */ 2372 VM_OBJECT_RUNLOCK(object); 2373 goto retry; 2374 } else if (vm_page_held(m)) { 2375 run_ext = 0; 2376 goto unlock; 2377 } 2378 } 2379 KASSERT((m->flags & PG_UNHOLDFREE) == 0, 2380 ("page %p is PG_UNHOLDFREE", m)); 2381 /* Don't care: PG_NODUMP, PG_ZERO. */ 2382 if (object->type != OBJT_DEFAULT && 2383 object->type != OBJT_SWAP && 2384 object->type != OBJT_VNODE) { 2385 run_ext = 0; 2386 #if VM_NRESERVLEVEL > 0 2387 } else if ((options & VPSC_NOSUPER) != 0 && 2388 (level = vm_reserv_level_iffullpop(m)) >= 0) { 2389 run_ext = 0; 2390 /* Advance to the end of the superpage. */ 2391 pa = VM_PAGE_TO_PHYS(m); 2392 m_inc = atop(roundup2(pa + 1, 2393 vm_reserv_size(level)) - pa); 2394 #endif 2395 } else if (object->memattr == VM_MEMATTR_DEFAULT && 2396 vm_page_queue(m) != PQ_NONE && !vm_page_busied(m)) { 2397 /* 2398 * The page is allocated but eligible for 2399 * relocation. Extend the current run by one 2400 * page. 2401 */ 2402 KASSERT(pmap_page_get_memattr(m) == 2403 VM_MEMATTR_DEFAULT, 2404 ("page %p has an unexpected memattr", m)); 2405 KASSERT((m->oflags & (VPO_SWAPINPROG | 2406 VPO_SWAPSLEEP | VPO_UNMANAGED)) == 0, 2407 ("page %p has unexpected oflags", m)); 2408 /* Don't care: VPO_NOSYNC. */ 2409 run_ext = 1; 2410 } else 2411 run_ext = 0; 2412 unlock: 2413 VM_OBJECT_RUNLOCK(object); 2414 #if VM_NRESERVLEVEL > 0 2415 } else if (level >= 0) { 2416 /* 2417 * The page is reserved but not yet allocated. In 2418 * other words, it is still free. Extend the current 2419 * run by one page. 2420 */ 2421 run_ext = 1; 2422 #endif 2423 } else if ((order = m->order) < VM_NFREEORDER) { 2424 /* 2425 * The page is enqueued in the physical memory 2426 * allocator's free page queues. Moreover, it is the 2427 * first page in a power-of-two-sized run of 2428 * contiguous free pages. Add these pages to the end 2429 * of the current run, and jump ahead. 2430 */ 2431 run_ext = 1 << order; 2432 m_inc = 1 << order; 2433 } else { 2434 /* 2435 * Skip the page for one of the following reasons: (1) 2436 * It is enqueued in the physical memory allocator's 2437 * free page queues. However, it is not the first 2438 * page in a run of contiguous free pages. (This case 2439 * rarely occurs because the scan is performed in 2440 * ascending order.) (2) It is not reserved, and it is 2441 * transitioning from free to allocated. (Conversely, 2442 * the transition from allocated to free for managed 2443 * pages is blocked by the page lock.) (3) It is 2444 * allocated but not contained by an object and not 2445 * wired, e.g., allocated by Xen's balloon driver. 2446 */ 2447 run_ext = 0; 2448 } 2449 2450 /* 2451 * Extend or reset the current run of pages. 2452 */ 2453 if (run_ext > 0) { 2454 if (run_len == 0) 2455 m_run = m; 2456 run_len += run_ext; 2457 } else { 2458 if (run_len > 0) { 2459 m_run = NULL; 2460 run_len = 0; 2461 } 2462 } 2463 } 2464 if (m_mtx != NULL) 2465 mtx_unlock(m_mtx); 2466 if (run_len >= npages) 2467 return (m_run); 2468 return (NULL); 2469 } 2470 2471 /* 2472 * vm_page_reclaim_run: 2473 * 2474 * Try to relocate each of the allocated virtual pages within the 2475 * specified run of physical pages to a new physical address. Free the 2476 * physical pages underlying the relocated virtual pages. A virtual page 2477 * is relocatable if and only if it could be laundered or reclaimed by 2478 * the page daemon. Whenever possible, a virtual page is relocated to a 2479 * physical address above "high". 2480 * 2481 * Returns 0 if every physical page within the run was already free or 2482 * just freed by a successful relocation. Otherwise, returns a non-zero 2483 * value indicating why the last attempt to relocate a virtual page was 2484 * unsuccessful. 2485 * 2486 * "req_class" must be an allocation class. 2487 */ 2488 static int 2489 vm_page_reclaim_run(int req_class, int domain, u_long npages, vm_page_t m_run, 2490 vm_paddr_t high) 2491 { 2492 struct vm_domain *vmd; 2493 struct mtx *m_mtx; 2494 struct spglist free; 2495 vm_object_t object; 2496 vm_paddr_t pa; 2497 vm_page_t m, m_end, m_new; 2498 int error, order, req; 2499 2500 KASSERT((req_class & VM_ALLOC_CLASS_MASK) == req_class, 2501 ("req_class is not an allocation class")); 2502 SLIST_INIT(&free); 2503 error = 0; 2504 m = m_run; 2505 m_end = m_run + npages; 2506 m_mtx = NULL; 2507 for (; error == 0 && m < m_end; m++) { 2508 KASSERT((m->flags & (PG_FICTITIOUS | PG_MARKER)) == 0, 2509 ("page %p is PG_FICTITIOUS or PG_MARKER", m)); 2510 2511 /* 2512 * Avoid releasing and reacquiring the same page lock. 2513 */ 2514 vm_page_change_lock(m, &m_mtx); 2515 retry: 2516 if (vm_page_held(m)) 2517 error = EBUSY; 2518 else if ((object = m->object) != NULL) { 2519 /* 2520 * The page is relocated if and only if it could be 2521 * laundered or reclaimed by the page daemon. 2522 */ 2523 if (!VM_OBJECT_TRYWLOCK(object)) { 2524 mtx_unlock(m_mtx); 2525 VM_OBJECT_WLOCK(object); 2526 mtx_lock(m_mtx); 2527 if (m->object != object) { 2528 /* 2529 * The page may have been freed. 2530 */ 2531 VM_OBJECT_WUNLOCK(object); 2532 goto retry; 2533 } else if (vm_page_held(m)) { 2534 error = EBUSY; 2535 goto unlock; 2536 } 2537 } 2538 KASSERT((m->flags & PG_UNHOLDFREE) == 0, 2539 ("page %p is PG_UNHOLDFREE", m)); 2540 /* Don't care: PG_NODUMP, PG_ZERO. */ 2541 if (object->type != OBJT_DEFAULT && 2542 object->type != OBJT_SWAP && 2543 object->type != OBJT_VNODE) 2544 error = EINVAL; 2545 else if (object->memattr != VM_MEMATTR_DEFAULT) 2546 error = EINVAL; 2547 else if (vm_page_queue(m) != PQ_NONE && 2548 !vm_page_busied(m)) { 2549 KASSERT(pmap_page_get_memattr(m) == 2550 VM_MEMATTR_DEFAULT, 2551 ("page %p has an unexpected memattr", m)); 2552 KASSERT((m->oflags & (VPO_SWAPINPROG | 2553 VPO_SWAPSLEEP | VPO_UNMANAGED)) == 0, 2554 ("page %p has unexpected oflags", m)); 2555 /* Don't care: VPO_NOSYNC. */ 2556 if (m->valid != 0) { 2557 /* 2558 * First, try to allocate a new page 2559 * that is above "high". Failing 2560 * that, try to allocate a new page 2561 * that is below "m_run". Allocate 2562 * the new page between the end of 2563 * "m_run" and "high" only as a last 2564 * resort. 2565 */ 2566 req = req_class | VM_ALLOC_NOOBJ; 2567 if ((m->flags & PG_NODUMP) != 0) 2568 req |= VM_ALLOC_NODUMP; 2569 if (trunc_page(high) != 2570 ~(vm_paddr_t)PAGE_MASK) { 2571 m_new = vm_page_alloc_contig( 2572 NULL, 0, req, 1, 2573 round_page(high), 2574 ~(vm_paddr_t)0, 2575 PAGE_SIZE, 0, 2576 VM_MEMATTR_DEFAULT); 2577 } else 2578 m_new = NULL; 2579 if (m_new == NULL) { 2580 pa = VM_PAGE_TO_PHYS(m_run); 2581 m_new = vm_page_alloc_contig( 2582 NULL, 0, req, 1, 2583 0, pa - 1, PAGE_SIZE, 0, 2584 VM_MEMATTR_DEFAULT); 2585 } 2586 if (m_new == NULL) { 2587 pa += ptoa(npages); 2588 m_new = vm_page_alloc_contig( 2589 NULL, 0, req, 1, 2590 pa, high, PAGE_SIZE, 0, 2591 VM_MEMATTR_DEFAULT); 2592 } 2593 if (m_new == NULL) { 2594 error = ENOMEM; 2595 goto unlock; 2596 } 2597 KASSERT(m_new->wire_count == 0, 2598 ("page %p is wired", m_new)); 2599 2600 /* 2601 * Replace "m" with the new page. For 2602 * vm_page_replace(), "m" must be busy 2603 * and dequeued. Finally, change "m" 2604 * as if vm_page_free() was called. 2605 */ 2606 if (object->ref_count != 0) 2607 pmap_remove_all(m); 2608 m_new->aflags = m->aflags & 2609 ~PGA_QUEUE_STATE_MASK; 2610 KASSERT(m_new->oflags == VPO_UNMANAGED, 2611 ("page %p is managed", m_new)); 2612 m_new->oflags = m->oflags & VPO_NOSYNC; 2613 pmap_copy_page(m, m_new); 2614 m_new->valid = m->valid; 2615 m_new->dirty = m->dirty; 2616 m->flags &= ~PG_ZERO; 2617 vm_page_xbusy(m); 2618 vm_page_remque(m); 2619 vm_page_replace_checked(m_new, object, 2620 m->pindex, m); 2621 if (vm_page_free_prep(m)) 2622 SLIST_INSERT_HEAD(&free, m, 2623 plinks.s.ss); 2624 2625 /* 2626 * The new page must be deactivated 2627 * before the object is unlocked. 2628 */ 2629 vm_page_change_lock(m_new, &m_mtx); 2630 vm_page_deactivate(m_new); 2631 } else { 2632 m->flags &= ~PG_ZERO; 2633 vm_page_remque(m); 2634 vm_page_remove(m); 2635 if (vm_page_free_prep(m)) 2636 SLIST_INSERT_HEAD(&free, m, 2637 plinks.s.ss); 2638 KASSERT(m->dirty == 0, 2639 ("page %p is dirty", m)); 2640 } 2641 } else 2642 error = EBUSY; 2643 unlock: 2644 VM_OBJECT_WUNLOCK(object); 2645 } else { 2646 MPASS(vm_phys_domain(m) == domain); 2647 vmd = VM_DOMAIN(domain); 2648 vm_domain_free_lock(vmd); 2649 order = m->order; 2650 if (order < VM_NFREEORDER) { 2651 /* 2652 * The page is enqueued in the physical memory 2653 * allocator's free page queues. Moreover, it 2654 * is the first page in a power-of-two-sized 2655 * run of contiguous free pages. Jump ahead 2656 * to the last page within that run, and 2657 * continue from there. 2658 */ 2659 m += (1 << order) - 1; 2660 } 2661 #if VM_NRESERVLEVEL > 0 2662 else if (vm_reserv_is_page_free(m)) 2663 order = 0; 2664 #endif 2665 vm_domain_free_unlock(vmd); 2666 if (order == VM_NFREEORDER) 2667 error = EINVAL; 2668 } 2669 } 2670 if (m_mtx != NULL) 2671 mtx_unlock(m_mtx); 2672 if ((m = SLIST_FIRST(&free)) != NULL) { 2673 int cnt; 2674 2675 vmd = VM_DOMAIN(domain); 2676 cnt = 0; 2677 vm_domain_free_lock(vmd); 2678 do { 2679 MPASS(vm_phys_domain(m) == domain); 2680 SLIST_REMOVE_HEAD(&free, plinks.s.ss); 2681 vm_phys_free_pages(m, 0); 2682 cnt++; 2683 } while ((m = SLIST_FIRST(&free)) != NULL); 2684 vm_domain_free_unlock(vmd); 2685 vm_domain_freecnt_inc(vmd, cnt); 2686 } 2687 return (error); 2688 } 2689 2690 #define NRUNS 16 2691 2692 CTASSERT(powerof2(NRUNS)); 2693 2694 #define RUN_INDEX(count) ((count) & (NRUNS - 1)) 2695 2696 #define MIN_RECLAIM 8 2697 2698 /* 2699 * vm_page_reclaim_contig: 2700 * 2701 * Reclaim allocated, contiguous physical memory satisfying the specified 2702 * conditions by relocating the virtual pages using that physical memory. 2703 * Returns true if reclamation is successful and false otherwise. Since 2704 * relocation requires the allocation of physical pages, reclamation may 2705 * fail due to a shortage of free pages. When reclamation fails, callers 2706 * are expected to perform vm_wait() before retrying a failed allocation 2707 * operation, e.g., vm_page_alloc_contig(). 2708 * 2709 * The caller must always specify an allocation class through "req". 2710 * 2711 * allocation classes: 2712 * VM_ALLOC_NORMAL normal process request 2713 * VM_ALLOC_SYSTEM system *really* needs a page 2714 * VM_ALLOC_INTERRUPT interrupt time request 2715 * 2716 * The optional allocation flags are ignored. 2717 * 2718 * "npages" must be greater than zero. Both "alignment" and "boundary" 2719 * must be a power of two. 2720 */ 2721 bool 2722 vm_page_reclaim_contig_domain(int domain, int req, u_long npages, 2723 vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary) 2724 { 2725 struct vm_domain *vmd; 2726 vm_paddr_t curr_low; 2727 vm_page_t m_run, m_runs[NRUNS]; 2728 u_long count, reclaimed; 2729 int error, i, options, req_class; 2730 2731 KASSERT(npages > 0, ("npages is 0")); 2732 KASSERT(powerof2(alignment), ("alignment is not a power of 2")); 2733 KASSERT(powerof2(boundary), ("boundary is not a power of 2")); 2734 req_class = req & VM_ALLOC_CLASS_MASK; 2735 2736 /* 2737 * The page daemon is allowed to dig deeper into the free page list. 2738 */ 2739 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT) 2740 req_class = VM_ALLOC_SYSTEM; 2741 2742 /* 2743 * Return if the number of free pages cannot satisfy the requested 2744 * allocation. 2745 */ 2746 vmd = VM_DOMAIN(domain); 2747 count = vmd->vmd_free_count; 2748 if (count < npages + vmd->vmd_free_reserved || (count < npages + 2749 vmd->vmd_interrupt_free_min && req_class == VM_ALLOC_SYSTEM) || 2750 (count < npages && req_class == VM_ALLOC_INTERRUPT)) 2751 return (false); 2752 2753 /* 2754 * Scan up to three times, relaxing the restrictions ("options") on 2755 * the reclamation of reservations and superpages each time. 2756 */ 2757 for (options = VPSC_NORESERV;;) { 2758 /* 2759 * Find the highest runs that satisfy the given constraints 2760 * and restrictions, and record them in "m_runs". 2761 */ 2762 curr_low = low; 2763 count = 0; 2764 for (;;) { 2765 m_run = vm_phys_scan_contig(domain, npages, curr_low, 2766 high, alignment, boundary, options); 2767 if (m_run == NULL) 2768 break; 2769 curr_low = VM_PAGE_TO_PHYS(m_run) + ptoa(npages); 2770 m_runs[RUN_INDEX(count)] = m_run; 2771 count++; 2772 } 2773 2774 /* 2775 * Reclaim the highest runs in LIFO (descending) order until 2776 * the number of reclaimed pages, "reclaimed", is at least 2777 * MIN_RECLAIM. Reset "reclaimed" each time because each 2778 * reclamation is idempotent, and runs will (likely) recur 2779 * from one scan to the next as restrictions are relaxed. 2780 */ 2781 reclaimed = 0; 2782 for (i = 0; count > 0 && i < NRUNS; i++) { 2783 count--; 2784 m_run = m_runs[RUN_INDEX(count)]; 2785 error = vm_page_reclaim_run(req_class, domain, npages, 2786 m_run, high); 2787 if (error == 0) { 2788 reclaimed += npages; 2789 if (reclaimed >= MIN_RECLAIM) 2790 return (true); 2791 } 2792 } 2793 2794 /* 2795 * Either relax the restrictions on the next scan or return if 2796 * the last scan had no restrictions. 2797 */ 2798 if (options == VPSC_NORESERV) 2799 options = VPSC_NOSUPER; 2800 else if (options == VPSC_NOSUPER) 2801 options = VPSC_ANY; 2802 else if (options == VPSC_ANY) 2803 return (reclaimed != 0); 2804 } 2805 } 2806 2807 bool 2808 vm_page_reclaim_contig(int req, u_long npages, vm_paddr_t low, vm_paddr_t high, 2809 u_long alignment, vm_paddr_t boundary) 2810 { 2811 struct vm_domainset_iter di; 2812 int domain; 2813 bool ret; 2814 2815 vm_domainset_iter_page_init(&di, NULL, 0, &domain, &req); 2816 do { 2817 ret = vm_page_reclaim_contig_domain(domain, req, npages, low, 2818 high, alignment, boundary); 2819 if (ret) 2820 break; 2821 } while (vm_domainset_iter_page(&di, &domain, &req) == 0); 2822 2823 return (ret); 2824 } 2825 2826 /* 2827 * Set the domain in the appropriate page level domainset. 2828 */ 2829 void 2830 vm_domain_set(struct vm_domain *vmd) 2831 { 2832 2833 mtx_lock(&vm_domainset_lock); 2834 if (!vmd->vmd_minset && vm_paging_min(vmd)) { 2835 vmd->vmd_minset = 1; 2836 DOMAINSET_SET(vmd->vmd_domain, &vm_min_domains); 2837 } 2838 if (!vmd->vmd_severeset && vm_paging_severe(vmd)) { 2839 vmd->vmd_severeset = 1; 2840 DOMAINSET_SET(vmd->vmd_domain, &vm_severe_domains); 2841 } 2842 mtx_unlock(&vm_domainset_lock); 2843 } 2844 2845 /* 2846 * Clear the domain from the appropriate page level domainset. 2847 */ 2848 void 2849 vm_domain_clear(struct vm_domain *vmd) 2850 { 2851 2852 mtx_lock(&vm_domainset_lock); 2853 if (vmd->vmd_minset && !vm_paging_min(vmd)) { 2854 vmd->vmd_minset = 0; 2855 DOMAINSET_CLR(vmd->vmd_domain, &vm_min_domains); 2856 if (vm_min_waiters != 0) { 2857 vm_min_waiters = 0; 2858 wakeup(&vm_min_domains); 2859 } 2860 } 2861 if (vmd->vmd_severeset && !vm_paging_severe(vmd)) { 2862 vmd->vmd_severeset = 0; 2863 DOMAINSET_CLR(vmd->vmd_domain, &vm_severe_domains); 2864 if (vm_severe_waiters != 0) { 2865 vm_severe_waiters = 0; 2866 wakeup(&vm_severe_domains); 2867 } 2868 } 2869 2870 /* 2871 * If pageout daemon needs pages, then tell it that there are 2872 * some free. 2873 */ 2874 if (vmd->vmd_pageout_pages_needed && 2875 vmd->vmd_free_count >= vmd->vmd_pageout_free_min) { 2876 wakeup(&vmd->vmd_pageout_pages_needed); 2877 vmd->vmd_pageout_pages_needed = 0; 2878 } 2879 2880 /* See comments in vm_wait_doms(). */ 2881 if (vm_pageproc_waiters) { 2882 vm_pageproc_waiters = 0; 2883 wakeup(&vm_pageproc_waiters); 2884 } 2885 mtx_unlock(&vm_domainset_lock); 2886 } 2887 2888 /* 2889 * Wait for free pages to exceed the min threshold globally. 2890 */ 2891 void 2892 vm_wait_min(void) 2893 { 2894 2895 mtx_lock(&vm_domainset_lock); 2896 while (vm_page_count_min()) { 2897 vm_min_waiters++; 2898 msleep(&vm_min_domains, &vm_domainset_lock, PVM, "vmwait", 0); 2899 } 2900 mtx_unlock(&vm_domainset_lock); 2901 } 2902 2903 /* 2904 * Wait for free pages to exceed the severe threshold globally. 2905 */ 2906 void 2907 vm_wait_severe(void) 2908 { 2909 2910 mtx_lock(&vm_domainset_lock); 2911 while (vm_page_count_severe()) { 2912 vm_severe_waiters++; 2913 msleep(&vm_severe_domains, &vm_domainset_lock, PVM, 2914 "vmwait", 0); 2915 } 2916 mtx_unlock(&vm_domainset_lock); 2917 } 2918 2919 u_int 2920 vm_wait_count(void) 2921 { 2922 2923 return (vm_severe_waiters + vm_min_waiters + vm_pageproc_waiters); 2924 } 2925 2926 static void 2927 vm_wait_doms(const domainset_t *wdoms) 2928 { 2929 2930 /* 2931 * We use racey wakeup synchronization to avoid expensive global 2932 * locking for the pageproc when sleeping with a non-specific vm_wait. 2933 * To handle this, we only sleep for one tick in this instance. It 2934 * is expected that most allocations for the pageproc will come from 2935 * kmem or vm_page_grab* which will use the more specific and 2936 * race-free vm_wait_domain(). 2937 */ 2938 if (curproc == pageproc) { 2939 mtx_lock(&vm_domainset_lock); 2940 vm_pageproc_waiters++; 2941 msleep(&vm_pageproc_waiters, &vm_domainset_lock, PVM | PDROP, 2942 "pageprocwait", 1); 2943 } else { 2944 /* 2945 * XXX Ideally we would wait only until the allocation could 2946 * be satisfied. This condition can cause new allocators to 2947 * consume all freed pages while old allocators wait. 2948 */ 2949 mtx_lock(&vm_domainset_lock); 2950 if (DOMAINSET_SUBSET(&vm_min_domains, wdoms)) { 2951 vm_min_waiters++; 2952 msleep(&vm_min_domains, &vm_domainset_lock, PVM, 2953 "vmwait", 0); 2954 } 2955 mtx_unlock(&vm_domainset_lock); 2956 } 2957 } 2958 2959 /* 2960 * vm_wait_domain: 2961 * 2962 * Sleep until free pages are available for allocation. 2963 * - Called in various places after failed memory allocations. 2964 */ 2965 void 2966 vm_wait_domain(int domain) 2967 { 2968 struct vm_domain *vmd; 2969 domainset_t wdom; 2970 2971 vmd = VM_DOMAIN(domain); 2972 vm_domain_free_assert_unlocked(vmd); 2973 2974 if (curproc == pageproc) { 2975 mtx_lock(&vm_domainset_lock); 2976 if (vmd->vmd_free_count < vmd->vmd_pageout_free_min) { 2977 vmd->vmd_pageout_pages_needed = 1; 2978 msleep(&vmd->vmd_pageout_pages_needed, 2979 &vm_domainset_lock, PDROP | PSWP, "VMWait", 0); 2980 } else 2981 mtx_unlock(&vm_domainset_lock); 2982 } else { 2983 if (pageproc == NULL) 2984 panic("vm_wait in early boot"); 2985 DOMAINSET_ZERO(&wdom); 2986 DOMAINSET_SET(vmd->vmd_domain, &wdom); 2987 vm_wait_doms(&wdom); 2988 } 2989 } 2990 2991 /* 2992 * vm_wait: 2993 * 2994 * Sleep until free pages are available for allocation in the 2995 * affinity domains of the obj. If obj is NULL, the domain set 2996 * for the calling thread is used. 2997 * Called in various places after failed memory allocations. 2998 */ 2999 void 3000 vm_wait(vm_object_t obj) 3001 { 3002 struct domainset *d; 3003 3004 d = NULL; 3005 3006 /* 3007 * Carefully fetch pointers only once: the struct domainset 3008 * itself is ummutable but the pointer might change. 3009 */ 3010 if (obj != NULL) 3011 d = obj->domain.dr_policy; 3012 if (d == NULL) 3013 d = curthread->td_domain.dr_policy; 3014 3015 vm_wait_doms(&d->ds_mask); 3016 } 3017 3018 /* 3019 * vm_domain_alloc_fail: 3020 * 3021 * Called when a page allocation function fails. Informs the 3022 * pagedaemon and performs the requested wait. Requires the 3023 * domain_free and object lock on entry. Returns with the 3024 * object lock held and free lock released. Returns an error when 3025 * retry is necessary. 3026 * 3027 */ 3028 static int 3029 vm_domain_alloc_fail(struct vm_domain *vmd, vm_object_t object, int req) 3030 { 3031 3032 vm_domain_free_assert_unlocked(vmd); 3033 3034 atomic_add_int(&vmd->vmd_pageout_deficit, 3035 max((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1)); 3036 if (req & (VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL)) { 3037 if (object != NULL) 3038 VM_OBJECT_WUNLOCK(object); 3039 vm_wait_domain(vmd->vmd_domain); 3040 if (object != NULL) 3041 VM_OBJECT_WLOCK(object); 3042 if (req & VM_ALLOC_WAITOK) 3043 return (EAGAIN); 3044 } 3045 3046 return (0); 3047 } 3048 3049 /* 3050 * vm_waitpfault: 3051 * 3052 * Sleep until free pages are available for allocation. 3053 * - Called only in vm_fault so that processes page faulting 3054 * can be easily tracked. 3055 * - Sleeps at a lower priority than vm_wait() so that vm_wait()ing 3056 * processes will be able to grab memory first. Do not change 3057 * this balance without careful testing first. 3058 */ 3059 void 3060 vm_waitpfault(void) 3061 { 3062 3063 mtx_lock(&vm_domainset_lock); 3064 if (vm_page_count_min()) { 3065 vm_min_waiters++; 3066 msleep(&vm_min_domains, &vm_domainset_lock, PUSER, "pfault", 0); 3067 } 3068 mtx_unlock(&vm_domainset_lock); 3069 } 3070 3071 struct vm_pagequeue * 3072 vm_page_pagequeue(vm_page_t m) 3073 { 3074 3075 return (&vm_pagequeue_domain(m)->vmd_pagequeues[m->queue]); 3076 } 3077 3078 static struct mtx * 3079 vm_page_pagequeue_lockptr(vm_page_t m) 3080 { 3081 uint8_t queue; 3082 3083 if ((queue = m->queue) == PQ_NONE) 3084 return (NULL); 3085 return (&vm_pagequeue_domain(m)->vmd_pagequeues[queue].pq_mutex); 3086 } 3087 3088 static inline void 3089 vm_pqbatch_process_page(struct vm_pagequeue *pq, vm_page_t m) 3090 { 3091 struct vm_domain *vmd; 3092 uint8_t aflags; 3093 3094 vm_pagequeue_assert_locked(pq); 3095 KASSERT(pq == vm_page_pagequeue(m), 3096 ("page %p doesn't belong to %p", m, pq)); 3097 3098 aflags = m->aflags; 3099 if ((aflags & PGA_DEQUEUE) != 0) { 3100 if (__predict_true((aflags & PGA_ENQUEUED) != 0)) { 3101 TAILQ_REMOVE(&pq->pq_pl, m, plinks.q); 3102 vm_pagequeue_cnt_dec(pq); 3103 } 3104 vm_page_dequeue_complete(m); 3105 } else if ((aflags & (PGA_REQUEUE | PGA_REQUEUE_HEAD)) != 0) { 3106 if ((aflags & PGA_ENQUEUED) != 0) 3107 TAILQ_REMOVE(&pq->pq_pl, m, plinks.q); 3108 else { 3109 vm_pagequeue_cnt_inc(pq); 3110 vm_page_aflag_set(m, PGA_ENQUEUED); 3111 } 3112 if ((aflags & PGA_REQUEUE_HEAD) != 0) { 3113 KASSERT(m->queue == PQ_INACTIVE, 3114 ("head enqueue not supported for page %p", m)); 3115 vmd = vm_pagequeue_domain(m); 3116 TAILQ_INSERT_BEFORE(&vmd->vmd_inacthead, m, plinks.q); 3117 } else 3118 TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q); 3119 3120 /* 3121 * PGA_REQUEUE and PGA_REQUEUE_HEAD must be cleared after 3122 * setting PGA_ENQUEUED in order to synchronize with the 3123 * page daemon. 3124 */ 3125 vm_page_aflag_clear(m, PGA_REQUEUE | PGA_REQUEUE_HEAD); 3126 } 3127 } 3128 3129 static void 3130 vm_pqbatch_process(struct vm_pagequeue *pq, struct vm_batchqueue *bq, 3131 uint8_t queue) 3132 { 3133 vm_page_t m; 3134 int i; 3135 3136 for (i = 0; i < bq->bq_cnt; i++) { 3137 m = bq->bq_pa[i]; 3138 if (__predict_false(m->queue != queue)) 3139 continue; 3140 vm_pqbatch_process_page(pq, m); 3141 } 3142 vm_batchqueue_init(bq); 3143 } 3144 3145 static void 3146 vm_pqbatch_submit_page(vm_page_t m, uint8_t queue) 3147 { 3148 struct vm_batchqueue *bq; 3149 struct vm_pagequeue *pq; 3150 int domain; 3151 3152 vm_page_assert_locked(m); 3153 KASSERT(queue < PQ_COUNT, ("invalid queue %d", queue)); 3154 3155 domain = vm_phys_domain(m); 3156 pq = &vm_pagequeue_domain(m)->vmd_pagequeues[queue]; 3157 3158 critical_enter(); 3159 bq = DPCPU_PTR(pqbatch[domain][queue]); 3160 if (vm_batchqueue_insert(bq, m)) { 3161 critical_exit(); 3162 return; 3163 } 3164 if (!vm_pagequeue_trylock(pq)) { 3165 critical_exit(); 3166 vm_pagequeue_lock(pq); 3167 critical_enter(); 3168 bq = DPCPU_PTR(pqbatch[domain][queue]); 3169 } 3170 vm_pqbatch_process(pq, bq, queue); 3171 3172 /* 3173 * The page may have been logically dequeued before we acquired the 3174 * page queue lock. In this case, the page lock prevents the page 3175 * from being logically enqueued elsewhere. 3176 */ 3177 if (__predict_true(m->queue == queue)) 3178 vm_pqbatch_process_page(pq, m); 3179 else { 3180 KASSERT(m->queue == PQ_NONE, 3181 ("invalid queue transition for page %p", m)); 3182 KASSERT((m->aflags & PGA_ENQUEUED) == 0, 3183 ("page %p is enqueued with invalid queue index", m)); 3184 vm_page_aflag_clear(m, PGA_QUEUE_STATE_MASK); 3185 } 3186 vm_pagequeue_unlock(pq); 3187 critical_exit(); 3188 } 3189 3190 /* 3191 * vm_page_drain_pqbatch: [ internal use only ] 3192 * 3193 * Force all per-CPU page queue batch queues to be drained. This is 3194 * intended for use in severe memory shortages, to ensure that pages 3195 * do not remain stuck in the batch queues. 3196 */ 3197 void 3198 vm_page_drain_pqbatch(void) 3199 { 3200 struct thread *td; 3201 struct vm_domain *vmd; 3202 struct vm_pagequeue *pq; 3203 int cpu, domain, queue; 3204 3205 td = curthread; 3206 CPU_FOREACH(cpu) { 3207 thread_lock(td); 3208 sched_bind(td, cpu); 3209 thread_unlock(td); 3210 3211 for (domain = 0; domain < vm_ndomains; domain++) { 3212 vmd = VM_DOMAIN(domain); 3213 for (queue = 0; queue < PQ_COUNT; queue++) { 3214 pq = &vmd->vmd_pagequeues[queue]; 3215 vm_pagequeue_lock(pq); 3216 critical_enter(); 3217 vm_pqbatch_process(pq, 3218 DPCPU_PTR(pqbatch[domain][queue]), queue); 3219 critical_exit(); 3220 vm_pagequeue_unlock(pq); 3221 } 3222 } 3223 } 3224 thread_lock(td); 3225 sched_unbind(td); 3226 thread_unlock(td); 3227 } 3228 3229 /* 3230 * Complete the logical removal of a page from a page queue. We must be 3231 * careful to synchronize with the page daemon, which may be concurrently 3232 * examining the page with only the page lock held. The page must not be 3233 * in a state where it appears to be logically enqueued. 3234 */ 3235 static void 3236 vm_page_dequeue_complete(vm_page_t m) 3237 { 3238 3239 m->queue = PQ_NONE; 3240 atomic_thread_fence_rel(); 3241 vm_page_aflag_clear(m, PGA_QUEUE_STATE_MASK); 3242 } 3243 3244 /* 3245 * vm_page_dequeue_deferred: [ internal use only ] 3246 * 3247 * Request removal of the given page from its current page 3248 * queue. Physical removal from the queue may be deferred 3249 * indefinitely. 3250 * 3251 * The page must be locked. 3252 */ 3253 void 3254 vm_page_dequeue_deferred(vm_page_t m) 3255 { 3256 int queue; 3257 3258 vm_page_assert_locked(m); 3259 3260 queue = m->queue; 3261 if (queue == PQ_NONE) { 3262 KASSERT((m->aflags & PGA_QUEUE_STATE_MASK) == 0, 3263 ("page %p has queue state", m)); 3264 return; 3265 } 3266 if ((m->aflags & PGA_DEQUEUE) == 0) 3267 vm_page_aflag_set(m, PGA_DEQUEUE); 3268 vm_pqbatch_submit_page(m, queue); 3269 } 3270 3271 /* 3272 * vm_page_dequeue_locked: 3273 * 3274 * Remove the page from its page queue, which must be locked. 3275 * If the page lock is not held, there is no guarantee that the 3276 * page will not be enqueued by another thread before this function 3277 * returns. In this case, it is up to the caller to ensure that 3278 * no other threads hold a reference to the page. 3279 * 3280 * The page queue lock must be held. If the page is not already 3281 * logically dequeued, the page lock must be held as well. 3282 */ 3283 void 3284 vm_page_dequeue_locked(vm_page_t m) 3285 { 3286 struct vm_pagequeue *pq; 3287 3288 pq = vm_page_pagequeue(m); 3289 3290 KASSERT(m->queue != PQ_NONE, 3291 ("%s: page %p queue field is PQ_NONE", __func__, m)); 3292 vm_pagequeue_assert_locked(pq); 3293 KASSERT((m->aflags & PGA_DEQUEUE) != 0 || 3294 mtx_owned(vm_page_lockptr(m)), 3295 ("%s: queued unlocked page %p", __func__, m)); 3296 3297 if ((m->aflags & PGA_ENQUEUED) != 0) { 3298 TAILQ_REMOVE(&pq->pq_pl, m, plinks.q); 3299 vm_pagequeue_cnt_dec(pq); 3300 } 3301 vm_page_dequeue_complete(m); 3302 } 3303 3304 /* 3305 * vm_page_dequeue: 3306 * 3307 * Remove the page from whichever page queue it's in, if any. 3308 * If the page lock is not held, there is no guarantee that the 3309 * page will not be enqueued by another thread before this function 3310 * returns. In this case, it is up to the caller to ensure that 3311 * no other threads hold a reference to the page. 3312 */ 3313 void 3314 vm_page_dequeue(vm_page_t m) 3315 { 3316 struct mtx *lock, *lock1; 3317 3318 lock = vm_page_pagequeue_lockptr(m); 3319 for (;;) { 3320 if (lock == NULL) 3321 return; 3322 mtx_lock(lock); 3323 if ((lock1 = vm_page_pagequeue_lockptr(m)) == lock) 3324 break; 3325 mtx_unlock(lock); 3326 lock = lock1; 3327 } 3328 KASSERT(lock == vm_page_pagequeue_lockptr(m), 3329 ("%s: page %p migrated directly between queues", __func__, m)); 3330 vm_page_dequeue_locked(m); 3331 mtx_unlock(lock); 3332 } 3333 3334 /* 3335 * Schedule the given page for insertion into the specified page queue. 3336 * Physical insertion of the page may be deferred indefinitely. 3337 */ 3338 static void 3339 vm_page_enqueue(vm_page_t m, uint8_t queue) 3340 { 3341 3342 vm_page_assert_locked(m); 3343 KASSERT(m->queue == PQ_NONE && (m->aflags & PGA_QUEUE_STATE_MASK) == 0, 3344 ("%s: page %p is already enqueued", __func__, m)); 3345 3346 m->queue = queue; 3347 if ((m->aflags & PGA_REQUEUE) == 0) 3348 vm_page_aflag_set(m, PGA_REQUEUE); 3349 vm_pqbatch_submit_page(m, queue); 3350 } 3351 3352 /* 3353 * vm_page_requeue: [ internal use only ] 3354 * 3355 * Schedule a requeue of the given page. 3356 * 3357 * The page must be locked. 3358 */ 3359 void 3360 vm_page_requeue(vm_page_t m) 3361 { 3362 3363 vm_page_assert_locked(m); 3364 KASSERT(m->queue != PQ_NONE, 3365 ("%s: page %p is not logically enqueued", __func__, m)); 3366 3367 if ((m->aflags & PGA_REQUEUE) == 0) 3368 vm_page_aflag_set(m, PGA_REQUEUE); 3369 vm_pqbatch_submit_page(m, m->queue); 3370 } 3371 3372 /* 3373 * vm_page_activate: 3374 * 3375 * Put the specified page on the active list (if appropriate). 3376 * Ensure that act_count is at least ACT_INIT but do not otherwise 3377 * mess with it. 3378 * 3379 * The page must be locked. 3380 */ 3381 void 3382 vm_page_activate(vm_page_t m) 3383 { 3384 int queue; 3385 3386 vm_page_assert_locked(m); 3387 3388 if ((queue = vm_page_queue(m)) == PQ_ACTIVE || m->wire_count > 0 || 3389 (m->oflags & VPO_UNMANAGED) != 0) { 3390 if (queue == PQ_ACTIVE && m->act_count < ACT_INIT) 3391 m->act_count = ACT_INIT; 3392 return; 3393 } 3394 3395 vm_page_remque(m); 3396 if (m->act_count < ACT_INIT) 3397 m->act_count = ACT_INIT; 3398 vm_page_enqueue(m, PQ_ACTIVE); 3399 } 3400 3401 /* 3402 * vm_page_free_prep: 3403 * 3404 * Prepares the given page to be put on the free list, 3405 * disassociating it from any VM object. The caller may return 3406 * the page to the free list only if this function returns true. 3407 * 3408 * The object must be locked. The page must be locked if it is 3409 * managed. 3410 */ 3411 bool 3412 vm_page_free_prep(vm_page_t m) 3413 { 3414 3415 #if defined(DIAGNOSTIC) && defined(PHYS_TO_DMAP) 3416 if (PMAP_HAS_DMAP && (m->flags & PG_ZERO) != 0) { 3417 uint64_t *p; 3418 int i; 3419 p = (uint64_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m)); 3420 for (i = 0; i < PAGE_SIZE / sizeof(uint64_t); i++, p++) 3421 KASSERT(*p == 0, ("vm_page_free_prep %p PG_ZERO %d %jx", 3422 m, i, (uintmax_t)*p)); 3423 } 3424 #endif 3425 if ((m->oflags & VPO_UNMANAGED) == 0) { 3426 vm_page_lock_assert(m, MA_OWNED); 3427 KASSERT(!pmap_page_is_mapped(m), 3428 ("vm_page_free_prep: freeing mapped page %p", m)); 3429 } else 3430 KASSERT(m->queue == PQ_NONE, 3431 ("vm_page_free_prep: unmanaged page %p is queued", m)); 3432 VM_CNT_INC(v_tfree); 3433 3434 if (vm_page_sbusied(m)) 3435 panic("vm_page_free_prep: freeing busy page %p", m); 3436 3437 vm_page_remove(m); 3438 3439 /* 3440 * If fictitious remove object association and 3441 * return. 3442 */ 3443 if ((m->flags & PG_FICTITIOUS) != 0) { 3444 KASSERT(m->wire_count == 1, 3445 ("fictitious page %p is not wired", m)); 3446 KASSERT(m->queue == PQ_NONE, 3447 ("fictitious page %p is queued", m)); 3448 return (false); 3449 } 3450 3451 /* 3452 * Pages need not be dequeued before they are returned to the physical 3453 * memory allocator, but they must at least be marked for a deferred 3454 * dequeue. 3455 */ 3456 if ((m->oflags & VPO_UNMANAGED) == 0) 3457 vm_page_dequeue_deferred(m); 3458 3459 m->valid = 0; 3460 vm_page_undirty(m); 3461 3462 if (m->wire_count != 0) 3463 panic("vm_page_free_prep: freeing wired page %p", m); 3464 if (m->hold_count != 0) { 3465 m->flags &= ~PG_ZERO; 3466 KASSERT((m->flags & PG_UNHOLDFREE) == 0, 3467 ("vm_page_free_prep: freeing PG_UNHOLDFREE page %p", m)); 3468 m->flags |= PG_UNHOLDFREE; 3469 return (false); 3470 } 3471 3472 /* 3473 * Restore the default memory attribute to the page. 3474 */ 3475 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT) 3476 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT); 3477 3478 #if VM_NRESERVLEVEL > 0 3479 if (vm_reserv_free_page(m)) 3480 return (false); 3481 #endif 3482 3483 return (true); 3484 } 3485 3486 /* 3487 * vm_page_free_toq: 3488 * 3489 * Returns the given page to the free list, disassociating it 3490 * from any VM object. 3491 * 3492 * The object must be locked. The page must be locked if it is 3493 * managed. 3494 */ 3495 void 3496 vm_page_free_toq(vm_page_t m) 3497 { 3498 struct vm_domain *vmd; 3499 3500 if (!vm_page_free_prep(m)) 3501 return; 3502 3503 vmd = vm_pagequeue_domain(m); 3504 if (m->pool == VM_FREEPOOL_DEFAULT && vmd->vmd_pgcache != NULL) { 3505 uma_zfree(vmd->vmd_pgcache, m); 3506 return; 3507 } 3508 vm_domain_free_lock(vmd); 3509 vm_phys_free_pages(m, 0); 3510 vm_domain_free_unlock(vmd); 3511 vm_domain_freecnt_inc(vmd, 1); 3512 } 3513 3514 /* 3515 * vm_page_free_pages_toq: 3516 * 3517 * Returns a list of pages to the free list, disassociating it 3518 * from any VM object. In other words, this is equivalent to 3519 * calling vm_page_free_toq() for each page of a list of VM objects. 3520 * 3521 * The objects must be locked. The pages must be locked if it is 3522 * managed. 3523 */ 3524 void 3525 vm_page_free_pages_toq(struct spglist *free, bool update_wire_count) 3526 { 3527 vm_page_t m; 3528 int count; 3529 3530 if (SLIST_EMPTY(free)) 3531 return; 3532 3533 count = 0; 3534 while ((m = SLIST_FIRST(free)) != NULL) { 3535 count++; 3536 SLIST_REMOVE_HEAD(free, plinks.s.ss); 3537 vm_page_free_toq(m); 3538 } 3539 3540 if (update_wire_count) 3541 vm_wire_sub(count); 3542 } 3543 3544 /* 3545 * vm_page_wire: 3546 * 3547 * Mark this page as wired down. If the page is fictitious, then 3548 * its wire count must remain one. 3549 * 3550 * The page must be locked. 3551 */ 3552 void 3553 vm_page_wire(vm_page_t m) 3554 { 3555 3556 vm_page_assert_locked(m); 3557 if ((m->flags & PG_FICTITIOUS) != 0) { 3558 KASSERT(m->wire_count == 1, 3559 ("vm_page_wire: fictitious page %p's wire count isn't one", 3560 m)); 3561 return; 3562 } 3563 if (m->wire_count == 0) { 3564 KASSERT((m->oflags & VPO_UNMANAGED) == 0 || 3565 m->queue == PQ_NONE, 3566 ("vm_page_wire: unmanaged page %p is queued", m)); 3567 vm_wire_add(1); 3568 } 3569 m->wire_count++; 3570 KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m)); 3571 } 3572 3573 /* 3574 * vm_page_unwire: 3575 * 3576 * Release one wiring of the specified page, potentially allowing it to be 3577 * paged out. Returns TRUE if the number of wirings transitions to zero and 3578 * FALSE otherwise. 3579 * 3580 * Only managed pages belonging to an object can be paged out. If the number 3581 * of wirings transitions to zero and the page is eligible for page out, then 3582 * the page is added to the specified paging queue (unless PQ_NONE is 3583 * specified, in which case the page is dequeued if it belongs to a paging 3584 * queue). 3585 * 3586 * If a page is fictitious, then its wire count must always be one. 3587 * 3588 * A managed page must be locked. 3589 */ 3590 bool 3591 vm_page_unwire(vm_page_t m, uint8_t queue) 3592 { 3593 bool unwired; 3594 3595 KASSERT(queue < PQ_COUNT || queue == PQ_NONE, 3596 ("vm_page_unwire: invalid queue %u request for page %p", 3597 queue, m)); 3598 if ((m->oflags & VPO_UNMANAGED) == 0) 3599 vm_page_assert_locked(m); 3600 3601 unwired = vm_page_unwire_noq(m); 3602 if (!unwired || (m->oflags & VPO_UNMANAGED) != 0 || m->object == NULL) 3603 return (unwired); 3604 3605 if (vm_page_queue(m) == queue) { 3606 if (queue == PQ_ACTIVE) 3607 vm_page_reference(m); 3608 else if (queue != PQ_NONE) 3609 vm_page_requeue(m); 3610 } else { 3611 vm_page_dequeue(m); 3612 if (queue != PQ_NONE) { 3613 vm_page_enqueue(m, queue); 3614 if (queue == PQ_ACTIVE) 3615 /* Initialize act_count. */ 3616 vm_page_activate(m); 3617 } 3618 } 3619 return (unwired); 3620 } 3621 3622 /* 3623 * 3624 * vm_page_unwire_noq: 3625 * 3626 * Unwire a page without (re-)inserting it into a page queue. It is up 3627 * to the caller to enqueue, requeue, or free the page as appropriate. 3628 * In most cases, vm_page_unwire() should be used instead. 3629 */ 3630 bool 3631 vm_page_unwire_noq(vm_page_t m) 3632 { 3633 3634 if ((m->oflags & VPO_UNMANAGED) == 0) 3635 vm_page_assert_locked(m); 3636 if ((m->flags & PG_FICTITIOUS) != 0) { 3637 KASSERT(m->wire_count == 1, 3638 ("vm_page_unwire: fictitious page %p's wire count isn't one", m)); 3639 return (false); 3640 } 3641 if (m->wire_count == 0) 3642 panic("vm_page_unwire: page %p's wire count is zero", m); 3643 m->wire_count--; 3644 if (m->wire_count == 0) { 3645 vm_wire_sub(1); 3646 return (true); 3647 } else 3648 return (false); 3649 } 3650 3651 /* 3652 * Move the specified page to the tail of the inactive queue, or requeue 3653 * the page if it is already in the inactive queue. 3654 * 3655 * The page must be locked. 3656 */ 3657 void 3658 vm_page_deactivate(vm_page_t m) 3659 { 3660 3661 vm_page_assert_locked(m); 3662 3663 if (m->wire_count > 0 || (m->oflags & VPO_UNMANAGED) != 0) 3664 return; 3665 3666 if (!vm_page_inactive(m)) { 3667 vm_page_remque(m); 3668 vm_page_enqueue(m, PQ_INACTIVE); 3669 } else 3670 vm_page_requeue(m); 3671 } 3672 3673 /* 3674 * Move the specified page close to the head of the inactive queue, 3675 * bypassing LRU. A marker page is used to maintain FIFO ordering. 3676 * As with regular enqueues, we use a per-CPU batch queue to reduce 3677 * contention on the page queue lock. 3678 * 3679 * The page must be locked. 3680 */ 3681 void 3682 vm_page_deactivate_noreuse(vm_page_t m) 3683 { 3684 3685 vm_page_assert_locked(m); 3686 3687 if (m->wire_count > 0 || (m->oflags & VPO_UNMANAGED) != 0) 3688 return; 3689 3690 if (!vm_page_inactive(m)) 3691 vm_page_remque(m); 3692 m->queue = PQ_INACTIVE; 3693 if ((m->aflags & PGA_REQUEUE_HEAD) == 0) 3694 vm_page_aflag_set(m, PGA_REQUEUE_HEAD); 3695 vm_pqbatch_submit_page(m, PQ_INACTIVE); 3696 } 3697 3698 /* 3699 * vm_page_launder 3700 * 3701 * Put a page in the laundry, or requeue it if it is already there. 3702 */ 3703 void 3704 vm_page_launder(vm_page_t m) 3705 { 3706 3707 vm_page_assert_locked(m); 3708 if (m->wire_count > 0 || (m->oflags & VPO_UNMANAGED) != 0) 3709 return; 3710 3711 if (vm_page_in_laundry(m)) 3712 vm_page_requeue(m); 3713 else { 3714 vm_page_remque(m); 3715 vm_page_enqueue(m, PQ_LAUNDRY); 3716 } 3717 } 3718 3719 /* 3720 * vm_page_unswappable 3721 * 3722 * Put a page in the PQ_UNSWAPPABLE holding queue. 3723 */ 3724 void 3725 vm_page_unswappable(vm_page_t m) 3726 { 3727 3728 vm_page_assert_locked(m); 3729 KASSERT(m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0, 3730 ("page %p already unswappable", m)); 3731 3732 vm_page_remque(m); 3733 vm_page_enqueue(m, PQ_UNSWAPPABLE); 3734 } 3735 3736 /* 3737 * Attempt to free the page. If it cannot be freed, do nothing. Returns true 3738 * if the page is freed and false otherwise. 3739 * 3740 * The page must be managed. The page and its containing object must be 3741 * locked. 3742 */ 3743 bool 3744 vm_page_try_to_free(vm_page_t m) 3745 { 3746 3747 vm_page_assert_locked(m); 3748 VM_OBJECT_ASSERT_WLOCKED(m->object); 3749 KASSERT((m->oflags & VPO_UNMANAGED) == 0, ("page %p is unmanaged", m)); 3750 if (m->dirty != 0 || vm_page_held(m) || vm_page_busied(m)) 3751 return (false); 3752 if (m->object->ref_count != 0) { 3753 pmap_remove_all(m); 3754 if (m->dirty != 0) 3755 return (false); 3756 } 3757 vm_page_free(m); 3758 return (true); 3759 } 3760 3761 /* 3762 * vm_page_advise 3763 * 3764 * Apply the specified advice to the given page. 3765 * 3766 * The object and page must be locked. 3767 */ 3768 void 3769 vm_page_advise(vm_page_t m, int advice) 3770 { 3771 3772 vm_page_assert_locked(m); 3773 VM_OBJECT_ASSERT_WLOCKED(m->object); 3774 if (advice == MADV_FREE) 3775 /* 3776 * Mark the page clean. This will allow the page to be freed 3777 * without first paging it out. MADV_FREE pages are often 3778 * quickly reused by malloc(3), so we do not do anything that 3779 * would result in a page fault on a later access. 3780 */ 3781 vm_page_undirty(m); 3782 else if (advice != MADV_DONTNEED) { 3783 if (advice == MADV_WILLNEED) 3784 vm_page_activate(m); 3785 return; 3786 } 3787 3788 /* 3789 * Clear any references to the page. Otherwise, the page daemon will 3790 * immediately reactivate the page. 3791 */ 3792 vm_page_aflag_clear(m, PGA_REFERENCED); 3793 3794 if (advice != MADV_FREE && m->dirty == 0 && pmap_is_modified(m)) 3795 vm_page_dirty(m); 3796 3797 /* 3798 * Place clean pages near the head of the inactive queue rather than 3799 * the tail, thus defeating the queue's LRU operation and ensuring that 3800 * the page will be reused quickly. Dirty pages not already in the 3801 * laundry are moved there. 3802 */ 3803 if (m->dirty == 0) 3804 vm_page_deactivate_noreuse(m); 3805 else if (!vm_page_in_laundry(m)) 3806 vm_page_launder(m); 3807 } 3808 3809 /* 3810 * Grab a page, waiting until we are waken up due to the page 3811 * changing state. We keep on waiting, if the page continues 3812 * to be in the object. If the page doesn't exist, first allocate it 3813 * and then conditionally zero it. 3814 * 3815 * This routine may sleep. 3816 * 3817 * The object must be locked on entry. The lock will, however, be released 3818 * and reacquired if the routine sleeps. 3819 */ 3820 vm_page_t 3821 vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags) 3822 { 3823 vm_page_t m; 3824 int sleep; 3825 int pflags; 3826 3827 VM_OBJECT_ASSERT_WLOCKED(object); 3828 KASSERT((allocflags & VM_ALLOC_SBUSY) == 0 || 3829 (allocflags & VM_ALLOC_IGN_SBUSY) != 0, 3830 ("vm_page_grab: VM_ALLOC_SBUSY/VM_ALLOC_IGN_SBUSY mismatch")); 3831 pflags = allocflags & 3832 ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL); 3833 if ((allocflags & VM_ALLOC_NOWAIT) == 0) 3834 pflags |= VM_ALLOC_WAITFAIL; 3835 retrylookup: 3836 if ((m = vm_page_lookup(object, pindex)) != NULL) { 3837 sleep = (allocflags & VM_ALLOC_IGN_SBUSY) != 0 ? 3838 vm_page_xbusied(m) : vm_page_busied(m); 3839 if (sleep) { 3840 if ((allocflags & VM_ALLOC_NOWAIT) != 0) 3841 return (NULL); 3842 /* 3843 * Reference the page before unlocking and 3844 * sleeping so that the page daemon is less 3845 * likely to reclaim it. 3846 */ 3847 vm_page_aflag_set(m, PGA_REFERENCED); 3848 vm_page_lock(m); 3849 VM_OBJECT_WUNLOCK(object); 3850 vm_page_busy_sleep(m, "pgrbwt", (allocflags & 3851 VM_ALLOC_IGN_SBUSY) != 0); 3852 VM_OBJECT_WLOCK(object); 3853 goto retrylookup; 3854 } else { 3855 if ((allocflags & VM_ALLOC_WIRED) != 0) { 3856 vm_page_lock(m); 3857 vm_page_wire(m); 3858 vm_page_unlock(m); 3859 } 3860 if ((allocflags & 3861 (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) == 0) 3862 vm_page_xbusy(m); 3863 if ((allocflags & VM_ALLOC_SBUSY) != 0) 3864 vm_page_sbusy(m); 3865 return (m); 3866 } 3867 } 3868 m = vm_page_alloc(object, pindex, pflags); 3869 if (m == NULL) { 3870 if ((allocflags & VM_ALLOC_NOWAIT) != 0) 3871 return (NULL); 3872 goto retrylookup; 3873 } 3874 if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0) 3875 pmap_zero_page(m); 3876 return (m); 3877 } 3878 3879 /* 3880 * Return the specified range of pages from the given object. For each 3881 * page offset within the range, if a page already exists within the object 3882 * at that offset and it is busy, then wait for it to change state. If, 3883 * instead, the page doesn't exist, then allocate it. 3884 * 3885 * The caller must always specify an allocation class. 3886 * 3887 * allocation classes: 3888 * VM_ALLOC_NORMAL normal process request 3889 * VM_ALLOC_SYSTEM system *really* needs the pages 3890 * 3891 * The caller must always specify that the pages are to be busied and/or 3892 * wired. 3893 * 3894 * optional allocation flags: 3895 * VM_ALLOC_IGN_SBUSY do not sleep on soft busy pages 3896 * VM_ALLOC_NOBUSY do not exclusive busy the page 3897 * VM_ALLOC_NOWAIT do not sleep 3898 * VM_ALLOC_SBUSY set page to sbusy state 3899 * VM_ALLOC_WIRED wire the pages 3900 * VM_ALLOC_ZERO zero and validate any invalid pages 3901 * 3902 * If VM_ALLOC_NOWAIT is not specified, this routine may sleep. Otherwise, it 3903 * may return a partial prefix of the requested range. 3904 */ 3905 int 3906 vm_page_grab_pages(vm_object_t object, vm_pindex_t pindex, int allocflags, 3907 vm_page_t *ma, int count) 3908 { 3909 vm_page_t m, mpred; 3910 int pflags; 3911 int i; 3912 bool sleep; 3913 3914 VM_OBJECT_ASSERT_WLOCKED(object); 3915 KASSERT(((u_int)allocflags >> VM_ALLOC_COUNT_SHIFT) == 0, 3916 ("vm_page_grap_pages: VM_ALLOC_COUNT() is not allowed")); 3917 KASSERT((allocflags & VM_ALLOC_NOBUSY) == 0 || 3918 (allocflags & VM_ALLOC_WIRED) != 0, 3919 ("vm_page_grab_pages: the pages must be busied or wired")); 3920 KASSERT((allocflags & VM_ALLOC_SBUSY) == 0 || 3921 (allocflags & VM_ALLOC_IGN_SBUSY) != 0, 3922 ("vm_page_grab_pages: VM_ALLOC_SBUSY/IGN_SBUSY mismatch")); 3923 if (count == 0) 3924 return (0); 3925 pflags = allocflags & ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | 3926 VM_ALLOC_WAITFAIL | VM_ALLOC_IGN_SBUSY); 3927 if ((allocflags & VM_ALLOC_NOWAIT) == 0) 3928 pflags |= VM_ALLOC_WAITFAIL; 3929 i = 0; 3930 retrylookup: 3931 m = vm_radix_lookup_le(&object->rtree, pindex + i); 3932 if (m == NULL || m->pindex != pindex + i) { 3933 mpred = m; 3934 m = NULL; 3935 } else 3936 mpred = TAILQ_PREV(m, pglist, listq); 3937 for (; i < count; i++) { 3938 if (m != NULL) { 3939 sleep = (allocflags & VM_ALLOC_IGN_SBUSY) != 0 ? 3940 vm_page_xbusied(m) : vm_page_busied(m); 3941 if (sleep) { 3942 if ((allocflags & VM_ALLOC_NOWAIT) != 0) 3943 break; 3944 /* 3945 * Reference the page before unlocking and 3946 * sleeping so that the page daemon is less 3947 * likely to reclaim it. 3948 */ 3949 vm_page_aflag_set(m, PGA_REFERENCED); 3950 vm_page_lock(m); 3951 VM_OBJECT_WUNLOCK(object); 3952 vm_page_busy_sleep(m, "grbmaw", (allocflags & 3953 VM_ALLOC_IGN_SBUSY) != 0); 3954 VM_OBJECT_WLOCK(object); 3955 goto retrylookup; 3956 } 3957 if ((allocflags & VM_ALLOC_WIRED) != 0) { 3958 vm_page_lock(m); 3959 vm_page_wire(m); 3960 vm_page_unlock(m); 3961 } 3962 if ((allocflags & (VM_ALLOC_NOBUSY | 3963 VM_ALLOC_SBUSY)) == 0) 3964 vm_page_xbusy(m); 3965 if ((allocflags & VM_ALLOC_SBUSY) != 0) 3966 vm_page_sbusy(m); 3967 } else { 3968 m = vm_page_alloc_after(object, pindex + i, 3969 pflags | VM_ALLOC_COUNT(count - i), mpred); 3970 if (m == NULL) { 3971 if ((allocflags & VM_ALLOC_NOWAIT) != 0) 3972 break; 3973 goto retrylookup; 3974 } 3975 } 3976 if (m->valid == 0 && (allocflags & VM_ALLOC_ZERO) != 0) { 3977 if ((m->flags & PG_ZERO) == 0) 3978 pmap_zero_page(m); 3979 m->valid = VM_PAGE_BITS_ALL; 3980 } 3981 ma[i] = mpred = m; 3982 m = vm_page_next(m); 3983 } 3984 return (i); 3985 } 3986 3987 /* 3988 * Mapping function for valid or dirty bits in a page. 3989 * 3990 * Inputs are required to range within a page. 3991 */ 3992 vm_page_bits_t 3993 vm_page_bits(int base, int size) 3994 { 3995 int first_bit; 3996 int last_bit; 3997 3998 KASSERT( 3999 base + size <= PAGE_SIZE, 4000 ("vm_page_bits: illegal base/size %d/%d", base, size) 4001 ); 4002 4003 if (size == 0) /* handle degenerate case */ 4004 return (0); 4005 4006 first_bit = base >> DEV_BSHIFT; 4007 last_bit = (base + size - 1) >> DEV_BSHIFT; 4008 4009 return (((vm_page_bits_t)2 << last_bit) - 4010 ((vm_page_bits_t)1 << first_bit)); 4011 } 4012 4013 /* 4014 * vm_page_set_valid_range: 4015 * 4016 * Sets portions of a page valid. The arguments are expected 4017 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive 4018 * of any partial chunks touched by the range. The invalid portion of 4019 * such chunks will be zeroed. 4020 * 4021 * (base + size) must be less then or equal to PAGE_SIZE. 4022 */ 4023 void 4024 vm_page_set_valid_range(vm_page_t m, int base, int size) 4025 { 4026 int endoff, frag; 4027 4028 VM_OBJECT_ASSERT_WLOCKED(m->object); 4029 if (size == 0) /* handle degenerate case */ 4030 return; 4031 4032 /* 4033 * If the base is not DEV_BSIZE aligned and the valid 4034 * bit is clear, we have to zero out a portion of the 4035 * first block. 4036 */ 4037 if ((frag = rounddown2(base, DEV_BSIZE)) != base && 4038 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0) 4039 pmap_zero_page_area(m, frag, base - frag); 4040 4041 /* 4042 * If the ending offset is not DEV_BSIZE aligned and the 4043 * valid bit is clear, we have to zero out a portion of 4044 * the last block. 4045 */ 4046 endoff = base + size; 4047 if ((frag = rounddown2(endoff, DEV_BSIZE)) != endoff && 4048 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0) 4049 pmap_zero_page_area(m, endoff, 4050 DEV_BSIZE - (endoff & (DEV_BSIZE - 1))); 4051 4052 /* 4053 * Assert that no previously invalid block that is now being validated 4054 * is already dirty. 4055 */ 4056 KASSERT((~m->valid & vm_page_bits(base, size) & m->dirty) == 0, 4057 ("vm_page_set_valid_range: page %p is dirty", m)); 4058 4059 /* 4060 * Set valid bits inclusive of any overlap. 4061 */ 4062 m->valid |= vm_page_bits(base, size); 4063 } 4064 4065 /* 4066 * Clear the given bits from the specified page's dirty field. 4067 */ 4068 static __inline void 4069 vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits) 4070 { 4071 uintptr_t addr; 4072 #if PAGE_SIZE < 16384 4073 int shift; 4074 #endif 4075 4076 /* 4077 * If the object is locked and the page is neither exclusive busy nor 4078 * write mapped, then the page's dirty field cannot possibly be 4079 * set by a concurrent pmap operation. 4080 */ 4081 VM_OBJECT_ASSERT_WLOCKED(m->object); 4082 if (!vm_page_xbusied(m) && !pmap_page_is_write_mapped(m)) 4083 m->dirty &= ~pagebits; 4084 else { 4085 /* 4086 * The pmap layer can call vm_page_dirty() without 4087 * holding a distinguished lock. The combination of 4088 * the object's lock and an atomic operation suffice 4089 * to guarantee consistency of the page dirty field. 4090 * 4091 * For PAGE_SIZE == 32768 case, compiler already 4092 * properly aligns the dirty field, so no forcible 4093 * alignment is needed. Only require existence of 4094 * atomic_clear_64 when page size is 32768. 4095 */ 4096 addr = (uintptr_t)&m->dirty; 4097 #if PAGE_SIZE == 32768 4098 atomic_clear_64((uint64_t *)addr, pagebits); 4099 #elif PAGE_SIZE == 16384 4100 atomic_clear_32((uint32_t *)addr, pagebits); 4101 #else /* PAGE_SIZE <= 8192 */ 4102 /* 4103 * Use a trick to perform a 32-bit atomic on the 4104 * containing aligned word, to not depend on the existence 4105 * of atomic_clear_{8, 16}. 4106 */ 4107 shift = addr & (sizeof(uint32_t) - 1); 4108 #if BYTE_ORDER == BIG_ENDIAN 4109 shift = (sizeof(uint32_t) - sizeof(m->dirty) - shift) * NBBY; 4110 #else 4111 shift *= NBBY; 4112 #endif 4113 addr &= ~(sizeof(uint32_t) - 1); 4114 atomic_clear_32((uint32_t *)addr, pagebits << shift); 4115 #endif /* PAGE_SIZE */ 4116 } 4117 } 4118 4119 /* 4120 * vm_page_set_validclean: 4121 * 4122 * Sets portions of a page valid and clean. The arguments are expected 4123 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive 4124 * of any partial chunks touched by the range. The invalid portion of 4125 * such chunks will be zero'd. 4126 * 4127 * (base + size) must be less then or equal to PAGE_SIZE. 4128 */ 4129 void 4130 vm_page_set_validclean(vm_page_t m, int base, int size) 4131 { 4132 vm_page_bits_t oldvalid, pagebits; 4133 int endoff, frag; 4134 4135 VM_OBJECT_ASSERT_WLOCKED(m->object); 4136 if (size == 0) /* handle degenerate case */ 4137 return; 4138 4139 /* 4140 * If the base is not DEV_BSIZE aligned and the valid 4141 * bit is clear, we have to zero out a portion of the 4142 * first block. 4143 */ 4144 if ((frag = rounddown2(base, DEV_BSIZE)) != base && 4145 (m->valid & ((vm_page_bits_t)1 << (base >> DEV_BSHIFT))) == 0) 4146 pmap_zero_page_area(m, frag, base - frag); 4147 4148 /* 4149 * If the ending offset is not DEV_BSIZE aligned and the 4150 * valid bit is clear, we have to zero out a portion of 4151 * the last block. 4152 */ 4153 endoff = base + size; 4154 if ((frag = rounddown2(endoff, DEV_BSIZE)) != endoff && 4155 (m->valid & ((vm_page_bits_t)1 << (endoff >> DEV_BSHIFT))) == 0) 4156 pmap_zero_page_area(m, endoff, 4157 DEV_BSIZE - (endoff & (DEV_BSIZE - 1))); 4158 4159 /* 4160 * Set valid, clear dirty bits. If validating the entire 4161 * page we can safely clear the pmap modify bit. We also 4162 * use this opportunity to clear the VPO_NOSYNC flag. If a process 4163 * takes a write fault on a MAP_NOSYNC memory area the flag will 4164 * be set again. 4165 * 4166 * We set valid bits inclusive of any overlap, but we can only 4167 * clear dirty bits for DEV_BSIZE chunks that are fully within 4168 * the range. 4169 */ 4170 oldvalid = m->valid; 4171 pagebits = vm_page_bits(base, size); 4172 m->valid |= pagebits; 4173 #if 0 /* NOT YET */ 4174 if ((frag = base & (DEV_BSIZE - 1)) != 0) { 4175 frag = DEV_BSIZE - frag; 4176 base += frag; 4177 size -= frag; 4178 if (size < 0) 4179 size = 0; 4180 } 4181 pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1)); 4182 #endif 4183 if (base == 0 && size == PAGE_SIZE) { 4184 /* 4185 * The page can only be modified within the pmap if it is 4186 * mapped, and it can only be mapped if it was previously 4187 * fully valid. 4188 */ 4189 if (oldvalid == VM_PAGE_BITS_ALL) 4190 /* 4191 * Perform the pmap_clear_modify() first. Otherwise, 4192 * a concurrent pmap operation, such as 4193 * pmap_protect(), could clear a modification in the 4194 * pmap and set the dirty field on the page before 4195 * pmap_clear_modify() had begun and after the dirty 4196 * field was cleared here. 4197 */ 4198 pmap_clear_modify(m); 4199 m->dirty = 0; 4200 m->oflags &= ~VPO_NOSYNC; 4201 } else if (oldvalid != VM_PAGE_BITS_ALL) 4202 m->dirty &= ~pagebits; 4203 else 4204 vm_page_clear_dirty_mask(m, pagebits); 4205 } 4206 4207 void 4208 vm_page_clear_dirty(vm_page_t m, int base, int size) 4209 { 4210 4211 vm_page_clear_dirty_mask(m, vm_page_bits(base, size)); 4212 } 4213 4214 /* 4215 * vm_page_set_invalid: 4216 * 4217 * Invalidates DEV_BSIZE'd chunks within a page. Both the 4218 * valid and dirty bits for the effected areas are cleared. 4219 */ 4220 void 4221 vm_page_set_invalid(vm_page_t m, int base, int size) 4222 { 4223 vm_page_bits_t bits; 4224 vm_object_t object; 4225 4226 object = m->object; 4227 VM_OBJECT_ASSERT_WLOCKED(object); 4228 if (object->type == OBJT_VNODE && base == 0 && IDX_TO_OFF(m->pindex) + 4229 size >= object->un_pager.vnp.vnp_size) 4230 bits = VM_PAGE_BITS_ALL; 4231 else 4232 bits = vm_page_bits(base, size); 4233 if (object->ref_count != 0 && m->valid == VM_PAGE_BITS_ALL && 4234 bits != 0) 4235 pmap_remove_all(m); 4236 KASSERT((bits == 0 && m->valid == VM_PAGE_BITS_ALL) || 4237 !pmap_page_is_mapped(m), 4238 ("vm_page_set_invalid: page %p is mapped", m)); 4239 m->valid &= ~bits; 4240 m->dirty &= ~bits; 4241 } 4242 4243 /* 4244 * vm_page_zero_invalid() 4245 * 4246 * The kernel assumes that the invalid portions of a page contain 4247 * garbage, but such pages can be mapped into memory by user code. 4248 * When this occurs, we must zero out the non-valid portions of the 4249 * page so user code sees what it expects. 4250 * 4251 * Pages are most often semi-valid when the end of a file is mapped 4252 * into memory and the file's size is not page aligned. 4253 */ 4254 void 4255 vm_page_zero_invalid(vm_page_t m, boolean_t setvalid) 4256 { 4257 int b; 4258 int i; 4259 4260 VM_OBJECT_ASSERT_WLOCKED(m->object); 4261 /* 4262 * Scan the valid bits looking for invalid sections that 4263 * must be zeroed. Invalid sub-DEV_BSIZE'd areas ( where the 4264 * valid bit may be set ) have already been zeroed by 4265 * vm_page_set_validclean(). 4266 */ 4267 for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) { 4268 if (i == (PAGE_SIZE / DEV_BSIZE) || 4269 (m->valid & ((vm_page_bits_t)1 << i))) { 4270 if (i > b) { 4271 pmap_zero_page_area(m, 4272 b << DEV_BSHIFT, (i - b) << DEV_BSHIFT); 4273 } 4274 b = i + 1; 4275 } 4276 } 4277 4278 /* 4279 * setvalid is TRUE when we can safely set the zero'd areas 4280 * as being valid. We can do this if there are no cache consistancy 4281 * issues. e.g. it is ok to do with UFS, but not ok to do with NFS. 4282 */ 4283 if (setvalid) 4284 m->valid = VM_PAGE_BITS_ALL; 4285 } 4286 4287 /* 4288 * vm_page_is_valid: 4289 * 4290 * Is (partial) page valid? Note that the case where size == 0 4291 * will return FALSE in the degenerate case where the page is 4292 * entirely invalid, and TRUE otherwise. 4293 */ 4294 int 4295 vm_page_is_valid(vm_page_t m, int base, int size) 4296 { 4297 vm_page_bits_t bits; 4298 4299 VM_OBJECT_ASSERT_LOCKED(m->object); 4300 bits = vm_page_bits(base, size); 4301 return (m->valid != 0 && (m->valid & bits) == bits); 4302 } 4303 4304 /* 4305 * Returns true if all of the specified predicates are true for the entire 4306 * (super)page and false otherwise. 4307 */ 4308 bool 4309 vm_page_ps_test(vm_page_t m, int flags, vm_page_t skip_m) 4310 { 4311 vm_object_t object; 4312 int i, npages; 4313 4314 object = m->object; 4315 if (skip_m != NULL && skip_m->object != object) 4316 return (false); 4317 VM_OBJECT_ASSERT_LOCKED(object); 4318 npages = atop(pagesizes[m->psind]); 4319 4320 /* 4321 * The physically contiguous pages that make up a superpage, i.e., a 4322 * page with a page size index ("psind") greater than zero, will 4323 * occupy adjacent entries in vm_page_array[]. 4324 */ 4325 for (i = 0; i < npages; i++) { 4326 /* Always test object consistency, including "skip_m". */ 4327 if (m[i].object != object) 4328 return (false); 4329 if (&m[i] == skip_m) 4330 continue; 4331 if ((flags & PS_NONE_BUSY) != 0 && vm_page_busied(&m[i])) 4332 return (false); 4333 if ((flags & PS_ALL_DIRTY) != 0) { 4334 /* 4335 * Calling vm_page_test_dirty() or pmap_is_modified() 4336 * might stop this case from spuriously returning 4337 * "false". However, that would require a write lock 4338 * on the object containing "m[i]". 4339 */ 4340 if (m[i].dirty != VM_PAGE_BITS_ALL) 4341 return (false); 4342 } 4343 if ((flags & PS_ALL_VALID) != 0 && 4344 m[i].valid != VM_PAGE_BITS_ALL) 4345 return (false); 4346 } 4347 return (true); 4348 } 4349 4350 /* 4351 * Set the page's dirty bits if the page is modified. 4352 */ 4353 void 4354 vm_page_test_dirty(vm_page_t m) 4355 { 4356 4357 VM_OBJECT_ASSERT_WLOCKED(m->object); 4358 if (m->dirty != VM_PAGE_BITS_ALL && pmap_is_modified(m)) 4359 vm_page_dirty(m); 4360 } 4361 4362 void 4363 vm_page_lock_KBI(vm_page_t m, const char *file, int line) 4364 { 4365 4366 mtx_lock_flags_(vm_page_lockptr(m), 0, file, line); 4367 } 4368 4369 void 4370 vm_page_unlock_KBI(vm_page_t m, const char *file, int line) 4371 { 4372 4373 mtx_unlock_flags_(vm_page_lockptr(m), 0, file, line); 4374 } 4375 4376 int 4377 vm_page_trylock_KBI(vm_page_t m, const char *file, int line) 4378 { 4379 4380 return (mtx_trylock_flags_(vm_page_lockptr(m), 0, file, line)); 4381 } 4382 4383 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT) 4384 void 4385 vm_page_assert_locked_KBI(vm_page_t m, const char *file, int line) 4386 { 4387 4388 vm_page_lock_assert_KBI(m, MA_OWNED, file, line); 4389 } 4390 4391 void 4392 vm_page_lock_assert_KBI(vm_page_t m, int a, const char *file, int line) 4393 { 4394 4395 mtx_assert_(vm_page_lockptr(m), a, file, line); 4396 } 4397 #endif 4398 4399 #ifdef INVARIANTS 4400 void 4401 vm_page_object_lock_assert(vm_page_t m) 4402 { 4403 4404 /* 4405 * Certain of the page's fields may only be modified by the 4406 * holder of the containing object's lock or the exclusive busy. 4407 * holder. Unfortunately, the holder of the write busy is 4408 * not recorded, and thus cannot be checked here. 4409 */ 4410 if (m->object != NULL && !vm_page_xbusied(m)) 4411 VM_OBJECT_ASSERT_WLOCKED(m->object); 4412 } 4413 4414 void 4415 vm_page_assert_pga_writeable(vm_page_t m, uint8_t bits) 4416 { 4417 4418 if ((bits & PGA_WRITEABLE) == 0) 4419 return; 4420 4421 /* 4422 * The PGA_WRITEABLE flag can only be set if the page is 4423 * managed, is exclusively busied or the object is locked. 4424 * Currently, this flag is only set by pmap_enter(). 4425 */ 4426 KASSERT((m->oflags & VPO_UNMANAGED) == 0, 4427 ("PGA_WRITEABLE on unmanaged page")); 4428 if (!vm_page_xbusied(m)) 4429 VM_OBJECT_ASSERT_LOCKED(m->object); 4430 } 4431 #endif 4432 4433 #include "opt_ddb.h" 4434 #ifdef DDB 4435 #include <sys/kernel.h> 4436 4437 #include <ddb/ddb.h> 4438 4439 DB_SHOW_COMMAND(page, vm_page_print_page_info) 4440 { 4441 4442 db_printf("vm_cnt.v_free_count: %d\n", vm_free_count()); 4443 db_printf("vm_cnt.v_inactive_count: %d\n", vm_inactive_count()); 4444 db_printf("vm_cnt.v_active_count: %d\n", vm_active_count()); 4445 db_printf("vm_cnt.v_laundry_count: %d\n", vm_laundry_count()); 4446 db_printf("vm_cnt.v_wire_count: %d\n", vm_wire_count()); 4447 db_printf("vm_cnt.v_free_reserved: %d\n", vm_cnt.v_free_reserved); 4448 db_printf("vm_cnt.v_free_min: %d\n", vm_cnt.v_free_min); 4449 db_printf("vm_cnt.v_free_target: %d\n", vm_cnt.v_free_target); 4450 db_printf("vm_cnt.v_inactive_target: %d\n", vm_cnt.v_inactive_target); 4451 } 4452 4453 DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info) 4454 { 4455 int dom; 4456 4457 db_printf("pq_free %d\n", vm_free_count()); 4458 for (dom = 0; dom < vm_ndomains; dom++) { 4459 db_printf( 4460 "dom %d page_cnt %d free %d pq_act %d pq_inact %d pq_laund %d pq_unsw %d\n", 4461 dom, 4462 vm_dom[dom].vmd_page_count, 4463 vm_dom[dom].vmd_free_count, 4464 vm_dom[dom].vmd_pagequeues[PQ_ACTIVE].pq_cnt, 4465 vm_dom[dom].vmd_pagequeues[PQ_INACTIVE].pq_cnt, 4466 vm_dom[dom].vmd_pagequeues[PQ_LAUNDRY].pq_cnt, 4467 vm_dom[dom].vmd_pagequeues[PQ_UNSWAPPABLE].pq_cnt); 4468 } 4469 } 4470 4471 DB_SHOW_COMMAND(pginfo, vm_page_print_pginfo) 4472 { 4473 vm_page_t m; 4474 boolean_t phys; 4475 4476 if (!have_addr) { 4477 db_printf("show pginfo addr\n"); 4478 return; 4479 } 4480 4481 phys = strchr(modif, 'p') != NULL; 4482 if (phys) 4483 m = PHYS_TO_VM_PAGE(addr); 4484 else 4485 m = (vm_page_t)addr; 4486 db_printf( 4487 "page %p obj %p pidx 0x%jx phys 0x%jx q %d hold %d wire %d\n" 4488 " af 0x%x of 0x%x f 0x%x act %d busy %x valid 0x%x dirty 0x%x\n", 4489 m, m->object, (uintmax_t)m->pindex, (uintmax_t)m->phys_addr, 4490 m->queue, m->hold_count, m->wire_count, m->aflags, m->oflags, 4491 m->flags, m->act_count, m->busy_lock, m->valid, m->dirty); 4492 } 4493 #endif /* DDB */ 4494