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