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