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