1 /*- 2 * Copyright (c) 1991 Regents of the University of California. 3 * All rights reserved. 4 * Copyright (c) 1998 Matthew Dillon. All Rights Reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * The Mach Operating System project at Carnegie-Mellon University. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * from: @(#)vm_page.c 7.4 (Berkeley) 5/7/91 34 */ 35 36 /*- 37 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 38 * All rights reserved. 39 * 40 * Authors: Avadis Tevanian, Jr., Michael Wayne Young 41 * 42 * Permission to use, copy, modify and distribute this software and 43 * its documentation is hereby granted, provided that both the copyright 44 * notice and this permission notice appear in all copies of the 45 * software, derivative works or modified versions, and any portions 46 * thereof, and that both notices appear in supporting documentation. 47 * 48 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 49 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 50 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 51 * 52 * Carnegie Mellon requests users of this software to return to 53 * 54 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 55 * School of Computer Science 56 * Carnegie Mellon University 57 * Pittsburgh PA 15213-3890 58 * 59 * any improvements or extensions that they make and grant Carnegie the 60 * rights to redistribute these changes. 61 */ 62 63 /* 64 * GENERAL RULES ON VM_PAGE MANIPULATION 65 * 66 * - A page queue lock is required when adding or removing a page from a 67 * page queue regardless of other locks or the busy state of a page. 68 * 69 * * In general, no thread besides the page daemon can acquire or 70 * hold more than one page queue lock at a time. 71 * 72 * * The page daemon can acquire and hold any pair of page queue 73 * locks in any order. 74 * 75 * - The object lock is required when inserting or removing 76 * pages from an object (vm_page_insert() or vm_page_remove()). 77 * 78 */ 79 80 /* 81 * Resident memory management module. 82 */ 83 84 #include <sys/cdefs.h> 85 __FBSDID("$FreeBSD$"); 86 87 #include "opt_vm.h" 88 89 #include <sys/param.h> 90 #include <sys/systm.h> 91 #include <sys/lock.h> 92 #include <sys/kernel.h> 93 #include <sys/limits.h> 94 #include <sys/malloc.h> 95 #include <sys/mman.h> 96 #include <sys/msgbuf.h> 97 #include <sys/mutex.h> 98 #include <sys/proc.h> 99 #include <sys/rwlock.h> 100 #include <sys/sysctl.h> 101 #include <sys/vmmeter.h> 102 #include <sys/vnode.h> 103 104 #include <vm/vm.h> 105 #include <vm/pmap.h> 106 #include <vm/vm_param.h> 107 #include <vm/vm_kern.h> 108 #include <vm/vm_object.h> 109 #include <vm/vm_page.h> 110 #include <vm/vm_pageout.h> 111 #include <vm/vm_pager.h> 112 #include <vm/vm_phys.h> 113 #include <vm/vm_radix.h> 114 #include <vm/vm_reserv.h> 115 #include <vm/vm_extern.h> 116 #include <vm/uma.h> 117 #include <vm/uma_int.h> 118 119 #include <machine/md_var.h> 120 121 /* 122 * Associated with page of user-allocatable memory is a 123 * page structure. 124 */ 125 126 struct vm_domain vm_dom[MAXMEMDOM]; 127 struct mtx_padalign vm_page_queue_free_mtx; 128 129 struct mtx_padalign pa_lock[PA_LOCK_COUNT]; 130 131 vm_page_t vm_page_array; 132 long vm_page_array_size; 133 long first_page; 134 int vm_page_zero_count; 135 136 static int boot_pages = UMA_BOOT_PAGES; 137 TUNABLE_INT("vm.boot_pages", &boot_pages); 138 SYSCTL_INT(_vm, OID_AUTO, boot_pages, CTLFLAG_RD, &boot_pages, 0, 139 "number of pages allocated for bootstrapping the VM system"); 140 141 static int pa_tryrelock_restart; 142 SYSCTL_INT(_vm, OID_AUTO, tryrelock_restart, CTLFLAG_RD, 143 &pa_tryrelock_restart, 0, "Number of tryrelock restarts"); 144 145 static uma_zone_t fakepg_zone; 146 147 static struct vnode *vm_page_alloc_init(vm_page_t m); 148 static void vm_page_cache_turn_free(vm_page_t m); 149 static void vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits); 150 static void vm_page_enqueue(int queue, vm_page_t m); 151 static void vm_page_init_fakepg(void *dummy); 152 static int vm_page_insert_after(vm_page_t m, vm_object_t object, 153 vm_pindex_t pindex, vm_page_t mpred); 154 static void vm_page_insert_radixdone(vm_page_t m, vm_object_t object, 155 vm_page_t mpred); 156 157 SYSINIT(vm_page, SI_SUB_VM, SI_ORDER_SECOND, vm_page_init_fakepg, NULL); 158 159 static void 160 vm_page_init_fakepg(void *dummy) 161 { 162 163 fakepg_zone = uma_zcreate("fakepg", sizeof(struct vm_page), NULL, NULL, 164 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE | UMA_ZONE_VM); 165 } 166 167 /* Make sure that u_long is at least 64 bits when PAGE_SIZE is 32K. */ 168 #if PAGE_SIZE == 32768 169 #ifdef CTASSERT 170 CTASSERT(sizeof(u_long) >= 8); 171 #endif 172 #endif 173 174 /* 175 * Try to acquire a physical address lock while a pmap is locked. If we 176 * fail to trylock we unlock and lock the pmap directly and cache the 177 * locked pa in *locked. The caller should then restart their loop in case 178 * the virtual to physical mapping has changed. 179 */ 180 int 181 vm_page_pa_tryrelock(pmap_t pmap, vm_paddr_t pa, vm_paddr_t *locked) 182 { 183 vm_paddr_t lockpa; 184 185 lockpa = *locked; 186 *locked = pa; 187 if (lockpa) { 188 PA_LOCK_ASSERT(lockpa, MA_OWNED); 189 if (PA_LOCKPTR(pa) == PA_LOCKPTR(lockpa)) 190 return (0); 191 PA_UNLOCK(lockpa); 192 } 193 if (PA_TRYLOCK(pa)) 194 return (0); 195 PMAP_UNLOCK(pmap); 196 atomic_add_int(&pa_tryrelock_restart, 1); 197 PA_LOCK(pa); 198 PMAP_LOCK(pmap); 199 return (EAGAIN); 200 } 201 202 /* 203 * vm_set_page_size: 204 * 205 * Sets the page size, perhaps based upon the memory 206 * size. Must be called before any use of page-size 207 * dependent functions. 208 */ 209 void 210 vm_set_page_size(void) 211 { 212 if (cnt.v_page_size == 0) 213 cnt.v_page_size = PAGE_SIZE; 214 if (((cnt.v_page_size - 1) & cnt.v_page_size) != 0) 215 panic("vm_set_page_size: page size not a power of two"); 216 } 217 218 /* 219 * vm_page_blacklist_lookup: 220 * 221 * See if a physical address in this page has been listed 222 * in the blacklist tunable. Entries in the tunable are 223 * separated by spaces or commas. If an invalid integer is 224 * encountered then the rest of the string is skipped. 225 */ 226 static int 227 vm_page_blacklist_lookup(char *list, vm_paddr_t pa) 228 { 229 vm_paddr_t bad; 230 char *cp, *pos; 231 232 for (pos = list; *pos != '\0'; pos = cp) { 233 bad = strtoq(pos, &cp, 0); 234 if (*cp != '\0') { 235 if (*cp == ' ' || *cp == ',') { 236 cp++; 237 if (cp == pos) 238 continue; 239 } else 240 break; 241 } 242 if (pa == trunc_page(bad)) 243 return (1); 244 } 245 return (0); 246 } 247 248 static void 249 vm_page_domain_init(struct vm_domain *vmd) 250 { 251 struct vm_pagequeue *pq; 252 int i; 253 254 *__DECONST(char **, &vmd->vmd_pagequeues[PQ_INACTIVE].pq_name) = 255 "vm inactive pagequeue"; 256 *__DECONST(int **, &vmd->vmd_pagequeues[PQ_INACTIVE].pq_vcnt) = 257 &cnt.v_inactive_count; 258 *__DECONST(char **, &vmd->vmd_pagequeues[PQ_ACTIVE].pq_name) = 259 "vm active pagequeue"; 260 *__DECONST(int **, &vmd->vmd_pagequeues[PQ_ACTIVE].pq_vcnt) = 261 &cnt.v_active_count; 262 vmd->vmd_page_count = 0; 263 vmd->vmd_free_count = 0; 264 vmd->vmd_segs = 0; 265 vmd->vmd_oom = FALSE; 266 vmd->vmd_pass = 0; 267 for (i = 0; i < PQ_COUNT; i++) { 268 pq = &vmd->vmd_pagequeues[i]; 269 TAILQ_INIT(&pq->pq_pl); 270 mtx_init(&pq->pq_mutex, pq->pq_name, "vm pagequeue", 271 MTX_DEF | MTX_DUPOK); 272 } 273 } 274 275 /* 276 * vm_page_startup: 277 * 278 * Initializes the resident memory module. 279 * 280 * Allocates memory for the page cells, and 281 * for the object/offset-to-page hash table headers. 282 * Each page cell is initialized and placed on the free list. 283 */ 284 vm_offset_t 285 vm_page_startup(vm_offset_t vaddr) 286 { 287 vm_offset_t mapped; 288 vm_paddr_t page_range; 289 vm_paddr_t new_end; 290 int i; 291 vm_paddr_t pa; 292 vm_paddr_t last_pa; 293 char *list; 294 295 /* the biggest memory array is the second group of pages */ 296 vm_paddr_t end; 297 vm_paddr_t biggestsize; 298 vm_paddr_t low_water, high_water; 299 int biggestone; 300 301 biggestsize = 0; 302 biggestone = 0; 303 vaddr = round_page(vaddr); 304 305 for (i = 0; phys_avail[i + 1]; i += 2) { 306 phys_avail[i] = round_page(phys_avail[i]); 307 phys_avail[i + 1] = trunc_page(phys_avail[i + 1]); 308 } 309 310 low_water = phys_avail[0]; 311 high_water = phys_avail[1]; 312 313 for (i = 0; phys_avail[i + 1]; i += 2) { 314 vm_paddr_t size = phys_avail[i + 1] - phys_avail[i]; 315 316 if (size > biggestsize) { 317 biggestone = i; 318 biggestsize = size; 319 } 320 if (phys_avail[i] < low_water) 321 low_water = phys_avail[i]; 322 if (phys_avail[i + 1] > high_water) 323 high_water = phys_avail[i + 1]; 324 } 325 326 #ifdef XEN 327 low_water = 0; 328 #endif 329 330 end = phys_avail[biggestone+1]; 331 332 /* 333 * Initialize the page and queue locks. 334 */ 335 mtx_init(&vm_page_queue_free_mtx, "vm page free queue", NULL, MTX_DEF); 336 for (i = 0; i < PA_LOCK_COUNT; i++) 337 mtx_init(&pa_lock[i], "vm page", NULL, MTX_DEF); 338 for (i = 0; i < vm_ndomains; i++) 339 vm_page_domain_init(&vm_dom[i]); 340 341 /* 342 * Allocate memory for use when boot strapping the kernel memory 343 * allocator. 344 */ 345 new_end = end - (boot_pages * UMA_SLAB_SIZE); 346 new_end = trunc_page(new_end); 347 mapped = pmap_map(&vaddr, new_end, end, 348 VM_PROT_READ | VM_PROT_WRITE); 349 bzero((void *)mapped, end - new_end); 350 uma_startup((void *)mapped, boot_pages); 351 352 #if defined(__amd64__) || defined(__i386__) || defined(__arm__) || \ 353 defined(__mips__) 354 /* 355 * Allocate a bitmap to indicate that a random physical page 356 * needs to be included in a minidump. 357 * 358 * The amd64 port needs this to indicate which direct map pages 359 * need to be dumped, via calls to dump_add_page()/dump_drop_page(). 360 * 361 * However, i386 still needs this workspace internally within the 362 * minidump code. In theory, they are not needed on i386, but are 363 * included should the sf_buf code decide to use them. 364 */ 365 last_pa = 0; 366 for (i = 0; dump_avail[i + 1] != 0; i += 2) 367 if (dump_avail[i + 1] > last_pa) 368 last_pa = dump_avail[i + 1]; 369 page_range = last_pa / PAGE_SIZE; 370 vm_page_dump_size = round_page(roundup2(page_range, NBBY) / NBBY); 371 new_end -= vm_page_dump_size; 372 vm_page_dump = (void *)(uintptr_t)pmap_map(&vaddr, new_end, 373 new_end + vm_page_dump_size, VM_PROT_READ | VM_PROT_WRITE); 374 bzero((void *)vm_page_dump, vm_page_dump_size); 375 #endif 376 #ifdef __amd64__ 377 /* 378 * Request that the physical pages underlying the message buffer be 379 * included in a crash dump. Since the message buffer is accessed 380 * through the direct map, they are not automatically included. 381 */ 382 pa = DMAP_TO_PHYS((vm_offset_t)msgbufp->msg_ptr); 383 last_pa = pa + round_page(msgbufsize); 384 while (pa < last_pa) { 385 dump_add_page(pa); 386 pa += PAGE_SIZE; 387 } 388 #endif 389 /* 390 * Compute the number of pages of memory that will be available for 391 * use (taking into account the overhead of a page structure per 392 * page). 393 */ 394 first_page = low_water / PAGE_SIZE; 395 #ifdef VM_PHYSSEG_SPARSE 396 page_range = 0; 397 for (i = 0; phys_avail[i + 1] != 0; i += 2) 398 page_range += atop(phys_avail[i + 1] - phys_avail[i]); 399 #elif defined(VM_PHYSSEG_DENSE) 400 page_range = high_water / PAGE_SIZE - first_page; 401 #else 402 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined." 403 #endif 404 end = new_end; 405 406 /* 407 * Reserve an unmapped guard page to trap access to vm_page_array[-1]. 408 */ 409 vaddr += PAGE_SIZE; 410 411 /* 412 * Initialize the mem entry structures now, and put them in the free 413 * queue. 414 */ 415 new_end = trunc_page(end - page_range * sizeof(struct vm_page)); 416 mapped = pmap_map(&vaddr, new_end, end, 417 VM_PROT_READ | VM_PROT_WRITE); 418 vm_page_array = (vm_page_t) mapped; 419 #if VM_NRESERVLEVEL > 0 420 /* 421 * Allocate memory for the reservation management system's data 422 * structures. 423 */ 424 new_end = vm_reserv_startup(&vaddr, new_end, high_water); 425 #endif 426 #if defined(__amd64__) || defined(__mips__) 427 /* 428 * pmap_map on amd64 and mips can come out of the direct-map, not kvm 429 * like i386, so the pages must be tracked for a crashdump to include 430 * this data. This includes the vm_page_array and the early UMA 431 * bootstrap pages. 432 */ 433 for (pa = new_end; pa < phys_avail[biggestone + 1]; pa += PAGE_SIZE) 434 dump_add_page(pa); 435 #endif 436 phys_avail[biggestone + 1] = new_end; 437 438 /* 439 * Clear all of the page structures 440 */ 441 bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page)); 442 for (i = 0; i < page_range; i++) 443 vm_page_array[i].order = VM_NFREEORDER; 444 vm_page_array_size = page_range; 445 446 /* 447 * Initialize the physical memory allocator. 448 */ 449 vm_phys_init(); 450 451 /* 452 * Add every available physical page that is not blacklisted to 453 * the free lists. 454 */ 455 cnt.v_page_count = 0; 456 cnt.v_free_count = 0; 457 list = getenv("vm.blacklist"); 458 for (i = 0; phys_avail[i + 1] != 0; i += 2) { 459 pa = phys_avail[i]; 460 last_pa = phys_avail[i + 1]; 461 while (pa < last_pa) { 462 if (list != NULL && 463 vm_page_blacklist_lookup(list, pa)) 464 printf("Skipping page with pa 0x%jx\n", 465 (uintmax_t)pa); 466 else 467 vm_phys_add_page(pa); 468 pa += PAGE_SIZE; 469 } 470 } 471 freeenv(list); 472 #if VM_NRESERVLEVEL > 0 473 /* 474 * Initialize the reservation management system. 475 */ 476 vm_reserv_init(); 477 #endif 478 return (vaddr); 479 } 480 481 void 482 vm_page_reference(vm_page_t m) 483 { 484 485 vm_page_aflag_set(m, PGA_REFERENCED); 486 } 487 488 /* 489 * vm_page_busy_downgrade: 490 * 491 * Downgrade an exclusive busy page into a single shared busy page. 492 */ 493 void 494 vm_page_busy_downgrade(vm_page_t m) 495 { 496 u_int x; 497 498 vm_page_assert_xbusied(m); 499 500 for (;;) { 501 x = m->busy_lock; 502 x &= VPB_BIT_WAITERS; 503 if (atomic_cmpset_rel_int(&m->busy_lock, 504 VPB_SINGLE_EXCLUSIVER | x, VPB_SHARERS_WORD(1) | x)) 505 break; 506 } 507 } 508 509 /* 510 * vm_page_sbusied: 511 * 512 * Return a positive value if the page is shared busied, 0 otherwise. 513 */ 514 int 515 vm_page_sbusied(vm_page_t m) 516 { 517 u_int x; 518 519 x = m->busy_lock; 520 return ((x & VPB_BIT_SHARED) != 0 && x != VPB_UNBUSIED); 521 } 522 523 /* 524 * vm_page_sunbusy: 525 * 526 * Shared unbusy a page. 527 */ 528 void 529 vm_page_sunbusy(vm_page_t m) 530 { 531 u_int x; 532 533 vm_page_assert_sbusied(m); 534 535 for (;;) { 536 x = m->busy_lock; 537 if (VPB_SHARERS(x) > 1) { 538 if (atomic_cmpset_int(&m->busy_lock, x, 539 x - VPB_ONE_SHARER)) 540 break; 541 continue; 542 } 543 if ((x & VPB_BIT_WAITERS) == 0) { 544 KASSERT(x == VPB_SHARERS_WORD(1), 545 ("vm_page_sunbusy: invalid lock state")); 546 if (atomic_cmpset_int(&m->busy_lock, 547 VPB_SHARERS_WORD(1), VPB_UNBUSIED)) 548 break; 549 continue; 550 } 551 KASSERT(x == (VPB_SHARERS_WORD(1) | VPB_BIT_WAITERS), 552 ("vm_page_sunbusy: invalid lock state for waiters")); 553 554 vm_page_lock(m); 555 if (!atomic_cmpset_int(&m->busy_lock, x, VPB_UNBUSIED)) { 556 vm_page_unlock(m); 557 continue; 558 } 559 wakeup(m); 560 vm_page_unlock(m); 561 break; 562 } 563 } 564 565 /* 566 * vm_page_busy_sleep: 567 * 568 * Sleep and release the page lock, using the page pointer as wchan. 569 * This is used to implement the hard-path of busying mechanism. 570 * 571 * The given page must be locked. 572 */ 573 void 574 vm_page_busy_sleep(vm_page_t m, const char *wmesg) 575 { 576 u_int x; 577 578 vm_page_lock_assert(m, MA_OWNED); 579 580 x = m->busy_lock; 581 if (x == VPB_UNBUSIED) { 582 vm_page_unlock(m); 583 return; 584 } 585 if ((x & VPB_BIT_WAITERS) == 0 && 586 !atomic_cmpset_int(&m->busy_lock, x, x | VPB_BIT_WAITERS)) { 587 vm_page_unlock(m); 588 return; 589 } 590 msleep(m, vm_page_lockptr(m), PVM | PDROP, wmesg, 0); 591 } 592 593 /* 594 * vm_page_trysbusy: 595 * 596 * Try to shared busy a page. 597 * If the operation succeeds 1 is returned otherwise 0. 598 * The operation never sleeps. 599 */ 600 int 601 vm_page_trysbusy(vm_page_t m) 602 { 603 u_int x; 604 605 x = m->busy_lock; 606 return ((x & VPB_BIT_SHARED) != 0 && 607 atomic_cmpset_acq_int(&m->busy_lock, x, x + VPB_ONE_SHARER)); 608 } 609 610 /* 611 * vm_page_xunbusy_hard: 612 * 613 * Called after the first try the exclusive unbusy of a page failed. 614 * It is assumed that the waiters bit is on. 615 */ 616 void 617 vm_page_xunbusy_hard(vm_page_t m) 618 { 619 620 vm_page_assert_xbusied(m); 621 622 vm_page_lock(m); 623 atomic_store_rel_int(&m->busy_lock, VPB_UNBUSIED); 624 wakeup(m); 625 vm_page_unlock(m); 626 } 627 628 /* 629 * vm_page_flash: 630 * 631 * Wakeup anyone waiting for the page. 632 * The ownership bits do not change. 633 * 634 * The given page must be locked. 635 */ 636 void 637 vm_page_flash(vm_page_t m) 638 { 639 u_int x; 640 641 vm_page_lock_assert(m, MA_OWNED); 642 643 for (;;) { 644 x = m->busy_lock; 645 if ((x & VPB_BIT_WAITERS) == 0) 646 return; 647 if (atomic_cmpset_int(&m->busy_lock, x, 648 x & (~VPB_BIT_WAITERS))) 649 break; 650 } 651 wakeup(m); 652 } 653 654 /* 655 * Keep page from being freed by the page daemon 656 * much of the same effect as wiring, except much lower 657 * overhead and should be used only for *very* temporary 658 * holding ("wiring"). 659 */ 660 void 661 vm_page_hold(vm_page_t mem) 662 { 663 664 vm_page_lock_assert(mem, MA_OWNED); 665 mem->hold_count++; 666 } 667 668 void 669 vm_page_unhold(vm_page_t mem) 670 { 671 672 vm_page_lock_assert(mem, MA_OWNED); 673 --mem->hold_count; 674 KASSERT(mem->hold_count >= 0, ("vm_page_unhold: hold count < 0!!!")); 675 if (mem->hold_count == 0 && (mem->flags & PG_UNHOLDFREE) != 0) 676 vm_page_free_toq(mem); 677 } 678 679 /* 680 * vm_page_unhold_pages: 681 * 682 * Unhold each of the pages that is referenced by the given array. 683 */ 684 void 685 vm_page_unhold_pages(vm_page_t *ma, int count) 686 { 687 struct mtx *mtx, *new_mtx; 688 689 mtx = NULL; 690 for (; count != 0; count--) { 691 /* 692 * Avoid releasing and reacquiring the same page lock. 693 */ 694 new_mtx = vm_page_lockptr(*ma); 695 if (mtx != new_mtx) { 696 if (mtx != NULL) 697 mtx_unlock(mtx); 698 mtx = new_mtx; 699 mtx_lock(mtx); 700 } 701 vm_page_unhold(*ma); 702 ma++; 703 } 704 if (mtx != NULL) 705 mtx_unlock(mtx); 706 } 707 708 vm_page_t 709 PHYS_TO_VM_PAGE(vm_paddr_t pa) 710 { 711 vm_page_t m; 712 713 #ifdef VM_PHYSSEG_SPARSE 714 m = vm_phys_paddr_to_vm_page(pa); 715 if (m == NULL) 716 m = vm_phys_fictitious_to_vm_page(pa); 717 return (m); 718 #elif defined(VM_PHYSSEG_DENSE) 719 long pi; 720 721 pi = atop(pa); 722 if (pi >= first_page && (pi - first_page) < vm_page_array_size) { 723 m = &vm_page_array[pi - first_page]; 724 return (m); 725 } 726 return (vm_phys_fictitious_to_vm_page(pa)); 727 #else 728 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined." 729 #endif 730 } 731 732 /* 733 * vm_page_getfake: 734 * 735 * Create a fictitious page with the specified physical address and 736 * memory attribute. The memory attribute is the only the machine- 737 * dependent aspect of a fictitious page that must be initialized. 738 */ 739 vm_page_t 740 vm_page_getfake(vm_paddr_t paddr, vm_memattr_t memattr) 741 { 742 vm_page_t m; 743 744 m = uma_zalloc(fakepg_zone, M_WAITOK | M_ZERO); 745 vm_page_initfake(m, paddr, memattr); 746 return (m); 747 } 748 749 void 750 vm_page_initfake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr) 751 { 752 753 if ((m->flags & PG_FICTITIOUS) != 0) { 754 /* 755 * The page's memattr might have changed since the 756 * previous initialization. Update the pmap to the 757 * new memattr. 758 */ 759 goto memattr; 760 } 761 m->phys_addr = paddr; 762 m->queue = PQ_NONE; 763 /* Fictitious pages don't use "segind". */ 764 m->flags = PG_FICTITIOUS; 765 /* Fictitious pages don't use "order" or "pool". */ 766 m->oflags = VPO_UNMANAGED; 767 m->busy_lock = VPB_SINGLE_EXCLUSIVER; 768 m->wire_count = 1; 769 pmap_page_init(m); 770 memattr: 771 pmap_page_set_memattr(m, memattr); 772 } 773 774 /* 775 * vm_page_putfake: 776 * 777 * Release a fictitious page. 778 */ 779 void 780 vm_page_putfake(vm_page_t m) 781 { 782 783 KASSERT((m->oflags & VPO_UNMANAGED) != 0, ("managed %p", m)); 784 KASSERT((m->flags & PG_FICTITIOUS) != 0, 785 ("vm_page_putfake: bad page %p", m)); 786 uma_zfree(fakepg_zone, m); 787 } 788 789 /* 790 * vm_page_updatefake: 791 * 792 * Update the given fictitious page to the specified physical address and 793 * memory attribute. 794 */ 795 void 796 vm_page_updatefake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr) 797 { 798 799 KASSERT((m->flags & PG_FICTITIOUS) != 0, 800 ("vm_page_updatefake: bad page %p", m)); 801 m->phys_addr = paddr; 802 pmap_page_set_memattr(m, memattr); 803 } 804 805 /* 806 * vm_page_free: 807 * 808 * Free a page. 809 */ 810 void 811 vm_page_free(vm_page_t m) 812 { 813 814 m->flags &= ~PG_ZERO; 815 vm_page_free_toq(m); 816 } 817 818 /* 819 * vm_page_free_zero: 820 * 821 * Free a page to the zerod-pages queue 822 */ 823 void 824 vm_page_free_zero(vm_page_t m) 825 { 826 827 m->flags |= PG_ZERO; 828 vm_page_free_toq(m); 829 } 830 831 /* 832 * Unbusy and handle the page queueing for a page from the VOP_GETPAGES() 833 * array which is not the request page. 834 */ 835 void 836 vm_page_readahead_finish(vm_page_t m) 837 { 838 839 if (m->valid != 0) { 840 /* 841 * Since the page is not the requested page, whether 842 * it should be activated or deactivated is not 843 * obvious. Empirical results have shown that 844 * deactivating the page is usually the best choice, 845 * unless the page is wanted by another thread. 846 */ 847 vm_page_lock(m); 848 if ((m->busy_lock & VPB_BIT_WAITERS) != 0) 849 vm_page_activate(m); 850 else 851 vm_page_deactivate(m); 852 vm_page_unlock(m); 853 vm_page_xunbusy(m); 854 } else { 855 /* 856 * Free the completely invalid page. Such page state 857 * occurs due to the short read operation which did 858 * not covered our page at all, or in case when a read 859 * error happens. 860 */ 861 vm_page_lock(m); 862 vm_page_free(m); 863 vm_page_unlock(m); 864 } 865 } 866 867 /* 868 * vm_page_sleep_if_busy: 869 * 870 * Sleep and release the page queues lock if the page is busied. 871 * Returns TRUE if the thread slept. 872 * 873 * The given page must be unlocked and object containing it must 874 * be locked. 875 */ 876 int 877 vm_page_sleep_if_busy(vm_page_t m, const char *msg) 878 { 879 vm_object_t obj; 880 881 vm_page_lock_assert(m, MA_NOTOWNED); 882 VM_OBJECT_ASSERT_WLOCKED(m->object); 883 884 if (vm_page_busied(m)) { 885 /* 886 * The page-specific object must be cached because page 887 * identity can change during the sleep, causing the 888 * re-lock of a different object. 889 * It is assumed that a reference to the object is already 890 * held by the callers. 891 */ 892 obj = m->object; 893 vm_page_lock(m); 894 VM_OBJECT_WUNLOCK(obj); 895 vm_page_busy_sleep(m, msg); 896 VM_OBJECT_WLOCK(obj); 897 return (TRUE); 898 } 899 return (FALSE); 900 } 901 902 /* 903 * vm_page_dirty_KBI: [ internal use only ] 904 * 905 * Set all bits in the page's dirty field. 906 * 907 * The object containing the specified page must be locked if the 908 * call is made from the machine-independent layer. 909 * 910 * See vm_page_clear_dirty_mask(). 911 * 912 * This function should only be called by vm_page_dirty(). 913 */ 914 void 915 vm_page_dirty_KBI(vm_page_t m) 916 { 917 918 /* These assertions refer to this operation by its public name. */ 919 KASSERT((m->flags & PG_CACHED) == 0, 920 ("vm_page_dirty: page in cache!")); 921 KASSERT(!VM_PAGE_IS_FREE(m), 922 ("vm_page_dirty: page is free!")); 923 KASSERT(m->valid == VM_PAGE_BITS_ALL, 924 ("vm_page_dirty: page is invalid!")); 925 m->dirty = VM_PAGE_BITS_ALL; 926 } 927 928 /* 929 * vm_page_insert: [ internal use only ] 930 * 931 * Inserts the given mem entry into the object and object list. 932 * 933 * The object must be locked. 934 */ 935 int 936 vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex) 937 { 938 vm_page_t mpred; 939 940 VM_OBJECT_ASSERT_WLOCKED(object); 941 mpred = vm_radix_lookup_le(&object->rtree, pindex); 942 return (vm_page_insert_after(m, object, pindex, mpred)); 943 } 944 945 /* 946 * vm_page_insert_after: 947 * 948 * Inserts the page "m" into the specified object at offset "pindex". 949 * 950 * The page "mpred" must immediately precede the offset "pindex" within 951 * the specified object. 952 * 953 * The object must be locked. 954 */ 955 static int 956 vm_page_insert_after(vm_page_t m, vm_object_t object, vm_pindex_t pindex, 957 vm_page_t mpred) 958 { 959 vm_pindex_t sidx; 960 vm_object_t sobj; 961 vm_page_t msucc; 962 963 VM_OBJECT_ASSERT_WLOCKED(object); 964 KASSERT(m->object == NULL, 965 ("vm_page_insert_after: page already inserted")); 966 if (mpred != NULL) { 967 KASSERT(mpred->object == object || 968 (mpred->flags & PG_SLAB) != 0, 969 ("vm_page_insert_after: object doesn't contain mpred")); 970 KASSERT(mpred->pindex < pindex, 971 ("vm_page_insert_after: mpred doesn't precede pindex")); 972 msucc = TAILQ_NEXT(mpred, listq); 973 } else 974 msucc = TAILQ_FIRST(&object->memq); 975 if (msucc != NULL) 976 KASSERT(msucc->pindex > pindex, 977 ("vm_page_insert_after: msucc doesn't succeed pindex")); 978 979 /* 980 * Record the object/offset pair in this page 981 */ 982 sobj = m->object; 983 sidx = m->pindex; 984 m->object = object; 985 m->pindex = pindex; 986 987 /* 988 * Now link into the object's ordered list of backed pages. 989 */ 990 if (vm_radix_insert(&object->rtree, m)) { 991 m->object = sobj; 992 m->pindex = sidx; 993 return (1); 994 } 995 vm_page_insert_radixdone(m, object, mpred); 996 return (0); 997 } 998 999 /* 1000 * vm_page_insert_radixdone: 1001 * 1002 * Complete page "m" insertion into the specified object after the 1003 * radix trie hooking. 1004 * 1005 * The page "mpred" must precede the offset "m->pindex" within the 1006 * specified object. 1007 * 1008 * The object must be locked. 1009 */ 1010 static void 1011 vm_page_insert_radixdone(vm_page_t m, vm_object_t object, vm_page_t mpred) 1012 { 1013 1014 VM_OBJECT_ASSERT_WLOCKED(object); 1015 KASSERT(object != NULL && m->object == object, 1016 ("vm_page_insert_radixdone: page %p has inconsistent object", m)); 1017 if (mpred != NULL) { 1018 KASSERT(mpred->object == object || 1019 (mpred->flags & PG_SLAB) != 0, 1020 ("vm_page_insert_after: object doesn't contain mpred")); 1021 KASSERT(mpred->pindex < m->pindex, 1022 ("vm_page_insert_after: mpred doesn't precede pindex")); 1023 } 1024 1025 if (mpred != NULL) 1026 TAILQ_INSERT_AFTER(&object->memq, mpred, m, listq); 1027 else 1028 TAILQ_INSERT_HEAD(&object->memq, m, listq); 1029 1030 /* 1031 * Show that the object has one more resident page. 1032 */ 1033 object->resident_page_count++; 1034 1035 /* 1036 * Hold the vnode until the last page is released. 1037 */ 1038 if (object->resident_page_count == 1 && object->type == OBJT_VNODE) 1039 vhold(object->handle); 1040 1041 /* 1042 * Since we are inserting a new and possibly dirty page, 1043 * update the object's OBJ_MIGHTBEDIRTY flag. 1044 */ 1045 if (pmap_page_is_write_mapped(m)) 1046 vm_object_set_writeable_dirty(object); 1047 } 1048 1049 /* 1050 * vm_page_remove: 1051 * 1052 * Removes the given mem entry from the object/offset-page 1053 * table and the object page list, but do not invalidate/terminate 1054 * the backing store. 1055 * 1056 * The object must be locked. The page must be locked if it is managed. 1057 */ 1058 void 1059 vm_page_remove(vm_page_t m) 1060 { 1061 vm_object_t object; 1062 boolean_t lockacq; 1063 1064 if ((m->oflags & VPO_UNMANAGED) == 0) 1065 vm_page_lock_assert(m, MA_OWNED); 1066 if ((object = m->object) == NULL) 1067 return; 1068 VM_OBJECT_ASSERT_WLOCKED(object); 1069 if (vm_page_xbusied(m)) { 1070 lockacq = FALSE; 1071 if ((m->oflags & VPO_UNMANAGED) != 0 && 1072 !mtx_owned(vm_page_lockptr(m))) { 1073 lockacq = TRUE; 1074 vm_page_lock(m); 1075 } 1076 vm_page_flash(m); 1077 atomic_store_rel_int(&m->busy_lock, VPB_UNBUSIED); 1078 if (lockacq) 1079 vm_page_unlock(m); 1080 } 1081 1082 /* 1083 * Now remove from the object's list of backed pages. 1084 */ 1085 vm_radix_remove(&object->rtree, m->pindex); 1086 TAILQ_REMOVE(&object->memq, m, listq); 1087 1088 /* 1089 * And show that the object has one fewer resident page. 1090 */ 1091 object->resident_page_count--; 1092 1093 /* 1094 * The vnode may now be recycled. 1095 */ 1096 if (object->resident_page_count == 0 && object->type == OBJT_VNODE) 1097 vdrop(object->handle); 1098 1099 m->object = NULL; 1100 } 1101 1102 /* 1103 * vm_page_lookup: 1104 * 1105 * Returns the page associated with the object/offset 1106 * pair specified; if none is found, NULL is returned. 1107 * 1108 * The object must be locked. 1109 */ 1110 vm_page_t 1111 vm_page_lookup(vm_object_t object, vm_pindex_t pindex) 1112 { 1113 1114 VM_OBJECT_ASSERT_LOCKED(object); 1115 return (vm_radix_lookup(&object->rtree, pindex)); 1116 } 1117 1118 /* 1119 * vm_page_find_least: 1120 * 1121 * Returns the page associated with the object with least pindex 1122 * greater than or equal to the parameter pindex, or NULL. 1123 * 1124 * The object must be locked. 1125 */ 1126 vm_page_t 1127 vm_page_find_least(vm_object_t object, vm_pindex_t pindex) 1128 { 1129 vm_page_t m; 1130 1131 VM_OBJECT_ASSERT_LOCKED(object); 1132 if ((m = TAILQ_FIRST(&object->memq)) != NULL && m->pindex < pindex) 1133 m = vm_radix_lookup_ge(&object->rtree, pindex); 1134 return (m); 1135 } 1136 1137 /* 1138 * Returns the given page's successor (by pindex) within the object if it is 1139 * resident; if none is found, NULL is returned. 1140 * 1141 * The object must be locked. 1142 */ 1143 vm_page_t 1144 vm_page_next(vm_page_t m) 1145 { 1146 vm_page_t next; 1147 1148 VM_OBJECT_ASSERT_WLOCKED(m->object); 1149 if ((next = TAILQ_NEXT(m, listq)) != NULL && 1150 next->pindex != m->pindex + 1) 1151 next = NULL; 1152 return (next); 1153 } 1154 1155 /* 1156 * Returns the given page's predecessor (by pindex) within the object if it is 1157 * resident; if none is found, NULL is returned. 1158 * 1159 * The object must be locked. 1160 */ 1161 vm_page_t 1162 vm_page_prev(vm_page_t m) 1163 { 1164 vm_page_t prev; 1165 1166 VM_OBJECT_ASSERT_WLOCKED(m->object); 1167 if ((prev = TAILQ_PREV(m, pglist, listq)) != NULL && 1168 prev->pindex != m->pindex - 1) 1169 prev = NULL; 1170 return (prev); 1171 } 1172 1173 /* 1174 * Uses the page mnew as a replacement for an existing page at index 1175 * pindex which must be already present in the object. 1176 * 1177 * The existing page must not be on a paging queue. 1178 */ 1179 vm_page_t 1180 vm_page_replace(vm_page_t mnew, vm_object_t object, vm_pindex_t pindex) 1181 { 1182 vm_page_t mold, mpred; 1183 1184 VM_OBJECT_ASSERT_WLOCKED(object); 1185 1186 /* 1187 * This function mostly follows vm_page_insert() and 1188 * vm_page_remove() without the radix, object count and vnode 1189 * dance. Double check such functions for more comments. 1190 */ 1191 mpred = vm_radix_lookup(&object->rtree, pindex); 1192 KASSERT(mpred != NULL, 1193 ("vm_page_replace: replacing page not present with pindex")); 1194 mpred = TAILQ_PREV(mpred, respgs, listq); 1195 if (mpred != NULL) 1196 KASSERT(mpred->pindex < pindex, 1197 ("vm_page_insert_after: mpred doesn't precede pindex")); 1198 1199 mnew->object = object; 1200 mnew->pindex = pindex; 1201 mold = vm_radix_replace(&object->rtree, mnew, pindex); 1202 KASSERT(mold->queue == PQ_NONE, 1203 ("vm_page_replace: mold is on a paging queue")); 1204 1205 /* Detach the old page from the resident tailq. */ 1206 TAILQ_REMOVE(&object->memq, mold, listq); 1207 1208 mold->object = NULL; 1209 vm_page_xunbusy(mold); 1210 1211 /* Insert the new page in the resident tailq. */ 1212 if (mpred != NULL) 1213 TAILQ_INSERT_AFTER(&object->memq, mpred, mnew, listq); 1214 else 1215 TAILQ_INSERT_HEAD(&object->memq, mnew, listq); 1216 if (pmap_page_is_write_mapped(mnew)) 1217 vm_object_set_writeable_dirty(object); 1218 return (mold); 1219 } 1220 1221 /* 1222 * vm_page_rename: 1223 * 1224 * Move the given memory entry from its 1225 * current object to the specified target object/offset. 1226 * 1227 * Note: swap associated with the page must be invalidated by the move. We 1228 * have to do this for several reasons: (1) we aren't freeing the 1229 * page, (2) we are dirtying the page, (3) the VM system is probably 1230 * moving the page from object A to B, and will then later move 1231 * the backing store from A to B and we can't have a conflict. 1232 * 1233 * Note: we *always* dirty the page. It is necessary both for the 1234 * fact that we moved it, and because we may be invalidating 1235 * swap. If the page is on the cache, we have to deactivate it 1236 * or vm_page_dirty() will panic. Dirty pages are not allowed 1237 * on the cache. 1238 * 1239 * The objects must be locked. 1240 */ 1241 int 1242 vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex) 1243 { 1244 vm_page_t mpred; 1245 vm_pindex_t opidx; 1246 1247 VM_OBJECT_ASSERT_WLOCKED(new_object); 1248 1249 mpred = vm_radix_lookup_le(&new_object->rtree, new_pindex); 1250 KASSERT(mpred == NULL || mpred->pindex != new_pindex, 1251 ("vm_page_rename: pindex already renamed")); 1252 1253 /* 1254 * Create a custom version of vm_page_insert() which does not depend 1255 * by m_prev and can cheat on the implementation aspects of the 1256 * function. 1257 */ 1258 opidx = m->pindex; 1259 m->pindex = new_pindex; 1260 if (vm_radix_insert(&new_object->rtree, m)) { 1261 m->pindex = opidx; 1262 return (1); 1263 } 1264 1265 /* 1266 * The operation cannot fail anymore. The removal must happen before 1267 * the listq iterator is tainted. 1268 */ 1269 m->pindex = opidx; 1270 vm_page_lock(m); 1271 vm_page_remove(m); 1272 1273 /* Return back to the new pindex to complete vm_page_insert(). */ 1274 m->pindex = new_pindex; 1275 m->object = new_object; 1276 vm_page_unlock(m); 1277 vm_page_insert_radixdone(m, new_object, mpred); 1278 vm_page_dirty(m); 1279 return (0); 1280 } 1281 1282 /* 1283 * Convert all of the given object's cached pages that have a 1284 * pindex within the given range into free pages. If the value 1285 * zero is given for "end", then the range's upper bound is 1286 * infinity. If the given object is backed by a vnode and it 1287 * transitions from having one or more cached pages to none, the 1288 * vnode's hold count is reduced. 1289 */ 1290 void 1291 vm_page_cache_free(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 1292 { 1293 vm_page_t m; 1294 boolean_t empty; 1295 1296 mtx_lock(&vm_page_queue_free_mtx); 1297 if (__predict_false(vm_radix_is_empty(&object->cache))) { 1298 mtx_unlock(&vm_page_queue_free_mtx); 1299 return; 1300 } 1301 while ((m = vm_radix_lookup_ge(&object->cache, start)) != NULL) { 1302 if (end != 0 && m->pindex >= end) 1303 break; 1304 vm_radix_remove(&object->cache, m->pindex); 1305 vm_page_cache_turn_free(m); 1306 } 1307 empty = vm_radix_is_empty(&object->cache); 1308 mtx_unlock(&vm_page_queue_free_mtx); 1309 if (object->type == OBJT_VNODE && empty) 1310 vdrop(object->handle); 1311 } 1312 1313 /* 1314 * Returns the cached page that is associated with the given 1315 * object and offset. If, however, none exists, returns NULL. 1316 * 1317 * The free page queue must be locked. 1318 */ 1319 static inline vm_page_t 1320 vm_page_cache_lookup(vm_object_t object, vm_pindex_t pindex) 1321 { 1322 1323 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 1324 return (vm_radix_lookup(&object->cache, pindex)); 1325 } 1326 1327 /* 1328 * Remove the given cached page from its containing object's 1329 * collection of cached pages. 1330 * 1331 * The free page queue must be locked. 1332 */ 1333 static void 1334 vm_page_cache_remove(vm_page_t m) 1335 { 1336 1337 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 1338 KASSERT((m->flags & PG_CACHED) != 0, 1339 ("vm_page_cache_remove: page %p is not cached", m)); 1340 vm_radix_remove(&m->object->cache, m->pindex); 1341 m->object = NULL; 1342 cnt.v_cache_count--; 1343 } 1344 1345 /* 1346 * Transfer all of the cached pages with offset greater than or 1347 * equal to 'offidxstart' from the original object's cache to the 1348 * new object's cache. However, any cached pages with offset 1349 * greater than or equal to the new object's size are kept in the 1350 * original object. Initially, the new object's cache must be 1351 * empty. Offset 'offidxstart' in the original object must 1352 * correspond to offset zero in the new object. 1353 * 1354 * The new object must be locked. 1355 */ 1356 void 1357 vm_page_cache_transfer(vm_object_t orig_object, vm_pindex_t offidxstart, 1358 vm_object_t new_object) 1359 { 1360 vm_page_t m; 1361 1362 /* 1363 * Insertion into an object's collection of cached pages 1364 * requires the object to be locked. In contrast, removal does 1365 * not. 1366 */ 1367 VM_OBJECT_ASSERT_WLOCKED(new_object); 1368 KASSERT(vm_radix_is_empty(&new_object->cache), 1369 ("vm_page_cache_transfer: object %p has cached pages", 1370 new_object)); 1371 mtx_lock(&vm_page_queue_free_mtx); 1372 while ((m = vm_radix_lookup_ge(&orig_object->cache, 1373 offidxstart)) != NULL) { 1374 /* 1375 * Transfer all of the pages with offset greater than or 1376 * equal to 'offidxstart' from the original object's 1377 * cache to the new object's cache. 1378 */ 1379 if ((m->pindex - offidxstart) >= new_object->size) 1380 break; 1381 vm_radix_remove(&orig_object->cache, m->pindex); 1382 /* Update the page's object and offset. */ 1383 m->object = new_object; 1384 m->pindex -= offidxstart; 1385 if (vm_radix_insert(&new_object->cache, m)) 1386 vm_page_cache_turn_free(m); 1387 } 1388 mtx_unlock(&vm_page_queue_free_mtx); 1389 } 1390 1391 /* 1392 * Returns TRUE if a cached page is associated with the given object and 1393 * offset, and FALSE otherwise. 1394 * 1395 * The object must be locked. 1396 */ 1397 boolean_t 1398 vm_page_is_cached(vm_object_t object, vm_pindex_t pindex) 1399 { 1400 vm_page_t m; 1401 1402 /* 1403 * Insertion into an object's collection of cached pages requires the 1404 * object to be locked. Therefore, if the object is locked and the 1405 * object's collection is empty, there is no need to acquire the free 1406 * page queues lock in order to prove that the specified page doesn't 1407 * exist. 1408 */ 1409 VM_OBJECT_ASSERT_WLOCKED(object); 1410 if (__predict_true(vm_object_cache_is_empty(object))) 1411 return (FALSE); 1412 mtx_lock(&vm_page_queue_free_mtx); 1413 m = vm_page_cache_lookup(object, pindex); 1414 mtx_unlock(&vm_page_queue_free_mtx); 1415 return (m != NULL); 1416 } 1417 1418 /* 1419 * vm_page_alloc: 1420 * 1421 * Allocate and return a page that is associated with the specified 1422 * object and offset pair. By default, this page is exclusive busied. 1423 * 1424 * The caller must always specify an allocation class. 1425 * 1426 * allocation classes: 1427 * VM_ALLOC_NORMAL normal process request 1428 * VM_ALLOC_SYSTEM system *really* needs a page 1429 * VM_ALLOC_INTERRUPT interrupt time request 1430 * 1431 * optional allocation flags: 1432 * VM_ALLOC_COUNT(number) the number of additional pages that the caller 1433 * intends to allocate 1434 * VM_ALLOC_IFCACHED return page only if it is cached 1435 * VM_ALLOC_IFNOTCACHED return NULL, do not reactivate if the page 1436 * is cached 1437 * VM_ALLOC_NOBUSY do not exclusive busy the page 1438 * VM_ALLOC_NODUMP do not include the page in a kernel core dump 1439 * VM_ALLOC_NOOBJ page is not associated with an object and 1440 * should not be exclusive busy 1441 * VM_ALLOC_SBUSY shared busy the allocated page 1442 * VM_ALLOC_WIRED wire the allocated page 1443 * VM_ALLOC_ZERO prefer a zeroed page 1444 * 1445 * This routine may not sleep. 1446 */ 1447 vm_page_t 1448 vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req) 1449 { 1450 struct vnode *vp = NULL; 1451 vm_object_t m_object; 1452 vm_page_t m, mpred; 1453 int flags, req_class; 1454 1455 mpred = 0; /* XXX: pacify gcc */ 1456 KASSERT((object != NULL) == ((req & VM_ALLOC_NOOBJ) == 0) && 1457 (object != NULL || (req & VM_ALLOC_SBUSY) == 0) && 1458 ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) != 1459 (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)), 1460 ("vm_page_alloc: inconsistent object(%p)/req(%x)", (void *)object, 1461 req)); 1462 if (object != NULL) 1463 VM_OBJECT_ASSERT_WLOCKED(object); 1464 1465 req_class = req & VM_ALLOC_CLASS_MASK; 1466 1467 /* 1468 * The page daemon is allowed to dig deeper into the free page list. 1469 */ 1470 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT) 1471 req_class = VM_ALLOC_SYSTEM; 1472 1473 if (object != NULL) { 1474 mpred = vm_radix_lookup_le(&object->rtree, pindex); 1475 KASSERT(mpred == NULL || mpred->pindex != pindex, 1476 ("vm_page_alloc: pindex already allocated")); 1477 } 1478 1479 /* 1480 * The page allocation request can came from consumers which already 1481 * hold the free page queue mutex, like vm_page_insert() in 1482 * vm_page_cache(). 1483 */ 1484 mtx_lock_flags(&vm_page_queue_free_mtx, MTX_RECURSE); 1485 if (cnt.v_free_count + cnt.v_cache_count > cnt.v_free_reserved || 1486 (req_class == VM_ALLOC_SYSTEM && 1487 cnt.v_free_count + cnt.v_cache_count > cnt.v_interrupt_free_min) || 1488 (req_class == VM_ALLOC_INTERRUPT && 1489 cnt.v_free_count + cnt.v_cache_count > 0)) { 1490 /* 1491 * Allocate from the free queue if the number of free pages 1492 * exceeds the minimum for the request class. 1493 */ 1494 if (object != NULL && 1495 (m = vm_page_cache_lookup(object, pindex)) != NULL) { 1496 if ((req & VM_ALLOC_IFNOTCACHED) != 0) { 1497 mtx_unlock(&vm_page_queue_free_mtx); 1498 return (NULL); 1499 } 1500 if (vm_phys_unfree_page(m)) 1501 vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m, 0); 1502 #if VM_NRESERVLEVEL > 0 1503 else if (!vm_reserv_reactivate_page(m)) 1504 #else 1505 else 1506 #endif 1507 panic("vm_page_alloc: cache page %p is missing" 1508 " from the free queue", m); 1509 } else if ((req & VM_ALLOC_IFCACHED) != 0) { 1510 mtx_unlock(&vm_page_queue_free_mtx); 1511 return (NULL); 1512 #if VM_NRESERVLEVEL > 0 1513 } else if (object == NULL || (object->flags & (OBJ_COLORED | 1514 OBJ_FICTITIOUS)) != OBJ_COLORED || (m = 1515 vm_reserv_alloc_page(object, pindex, mpred)) == NULL) { 1516 #else 1517 } else { 1518 #endif 1519 m = vm_phys_alloc_pages(object != NULL ? 1520 VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT, 0); 1521 #if VM_NRESERVLEVEL > 0 1522 if (m == NULL && vm_reserv_reclaim_inactive()) { 1523 m = vm_phys_alloc_pages(object != NULL ? 1524 VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT, 1525 0); 1526 } 1527 #endif 1528 } 1529 } else { 1530 /* 1531 * Not allocatable, give up. 1532 */ 1533 mtx_unlock(&vm_page_queue_free_mtx); 1534 atomic_add_int(&vm_pageout_deficit, 1535 max((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1)); 1536 pagedaemon_wakeup(); 1537 return (NULL); 1538 } 1539 1540 /* 1541 * At this point we had better have found a good page. 1542 */ 1543 KASSERT(m != NULL, ("vm_page_alloc: missing page")); 1544 KASSERT(m->queue == PQ_NONE, 1545 ("vm_page_alloc: page %p has unexpected queue %d", m, m->queue)); 1546 KASSERT(m->wire_count == 0, ("vm_page_alloc: page %p is wired", m)); 1547 KASSERT(m->hold_count == 0, ("vm_page_alloc: page %p is held", m)); 1548 KASSERT(!vm_page_sbusied(m), 1549 ("vm_page_alloc: page %p is busy", m)); 1550 KASSERT(m->dirty == 0, ("vm_page_alloc: page %p is dirty", m)); 1551 KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT, 1552 ("vm_page_alloc: page %p has unexpected memattr %d", m, 1553 pmap_page_get_memattr(m))); 1554 if ((m->flags & PG_CACHED) != 0) { 1555 KASSERT((m->flags & PG_ZERO) == 0, 1556 ("vm_page_alloc: cached page %p is PG_ZERO", m)); 1557 KASSERT(m->valid != 0, 1558 ("vm_page_alloc: cached page %p is invalid", m)); 1559 if (m->object == object && m->pindex == pindex) 1560 cnt.v_reactivated++; 1561 else 1562 m->valid = 0; 1563 m_object = m->object; 1564 vm_page_cache_remove(m); 1565 if (m_object->type == OBJT_VNODE && 1566 vm_object_cache_is_empty(m_object)) 1567 vp = m_object->handle; 1568 } else { 1569 KASSERT(VM_PAGE_IS_FREE(m), 1570 ("vm_page_alloc: page %p is not free", m)); 1571 KASSERT(m->valid == 0, 1572 ("vm_page_alloc: free page %p is valid", m)); 1573 vm_phys_freecnt_adj(m, -1); 1574 } 1575 1576 /* 1577 * Only the PG_ZERO flag is inherited. The PG_CACHED or PG_FREE flag 1578 * must be cleared before the free page queues lock is released. 1579 */ 1580 flags = 0; 1581 if (m->flags & PG_ZERO) { 1582 vm_page_zero_count--; 1583 if (req & VM_ALLOC_ZERO) 1584 flags = PG_ZERO; 1585 } 1586 if (req & VM_ALLOC_NODUMP) 1587 flags |= PG_NODUMP; 1588 m->flags = flags; 1589 mtx_unlock(&vm_page_queue_free_mtx); 1590 m->aflags = 0; 1591 m->oflags = object == NULL || (object->flags & OBJ_UNMANAGED) != 0 ? 1592 VPO_UNMANAGED : 0; 1593 m->busy_lock = VPB_UNBUSIED; 1594 if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ | VM_ALLOC_SBUSY)) == 0) 1595 m->busy_lock = VPB_SINGLE_EXCLUSIVER; 1596 if ((req & VM_ALLOC_SBUSY) != 0) 1597 m->busy_lock = VPB_SHARERS_WORD(1); 1598 if (req & VM_ALLOC_WIRED) { 1599 /* 1600 * The page lock is not required for wiring a page until that 1601 * page is inserted into the object. 1602 */ 1603 atomic_add_int(&cnt.v_wire_count, 1); 1604 m->wire_count = 1; 1605 } 1606 m->act_count = 0; 1607 1608 if (object != NULL) { 1609 if (vm_page_insert_after(m, object, pindex, mpred)) { 1610 /* See the comment below about hold count. */ 1611 if (vp != NULL) 1612 vdrop(vp); 1613 pagedaemon_wakeup(); 1614 if (req & VM_ALLOC_WIRED) { 1615 atomic_subtract_int(&cnt.v_wire_count, 1); 1616 m->wire_count = 0; 1617 } 1618 m->object = NULL; 1619 vm_page_free(m); 1620 return (NULL); 1621 } 1622 1623 /* Ignore device objects; the pager sets "memattr" for them. */ 1624 if (object->memattr != VM_MEMATTR_DEFAULT && 1625 (object->flags & OBJ_FICTITIOUS) == 0) 1626 pmap_page_set_memattr(m, object->memattr); 1627 } else 1628 m->pindex = pindex; 1629 1630 /* 1631 * The following call to vdrop() must come after the above call 1632 * to vm_page_insert() in case both affect the same object and 1633 * vnode. Otherwise, the affected vnode's hold count could 1634 * temporarily become zero. 1635 */ 1636 if (vp != NULL) 1637 vdrop(vp); 1638 1639 /* 1640 * Don't wakeup too often - wakeup the pageout daemon when 1641 * we would be nearly out of memory. 1642 */ 1643 if (vm_paging_needed()) 1644 pagedaemon_wakeup(); 1645 1646 return (m); 1647 } 1648 1649 static void 1650 vm_page_alloc_contig_vdrop(struct spglist *lst) 1651 { 1652 1653 while (!SLIST_EMPTY(lst)) { 1654 vdrop((struct vnode *)SLIST_FIRST(lst)-> plinks.s.pv); 1655 SLIST_REMOVE_HEAD(lst, plinks.s.ss); 1656 } 1657 } 1658 1659 /* 1660 * vm_page_alloc_contig: 1661 * 1662 * Allocate a contiguous set of physical pages of the given size "npages" 1663 * from the free lists. All of the physical pages must be at or above 1664 * the given physical address "low" and below the given physical address 1665 * "high". The given value "alignment" determines the alignment of the 1666 * first physical page in the set. If the given value "boundary" is 1667 * non-zero, then the set of physical pages cannot cross any physical 1668 * address boundary that is a multiple of that value. Both "alignment" 1669 * and "boundary" must be a power of two. 1670 * 1671 * If the specified memory attribute, "memattr", is VM_MEMATTR_DEFAULT, 1672 * then the memory attribute setting for the physical pages is configured 1673 * to the object's memory attribute setting. Otherwise, the memory 1674 * attribute setting for the physical pages is configured to "memattr", 1675 * overriding the object's memory attribute setting. However, if the 1676 * object's memory attribute setting is not VM_MEMATTR_DEFAULT, then the 1677 * memory attribute setting for the physical pages cannot be configured 1678 * to VM_MEMATTR_DEFAULT. 1679 * 1680 * The caller must always specify an allocation class. 1681 * 1682 * allocation classes: 1683 * VM_ALLOC_NORMAL normal process request 1684 * VM_ALLOC_SYSTEM system *really* needs a page 1685 * VM_ALLOC_INTERRUPT interrupt time request 1686 * 1687 * optional allocation flags: 1688 * VM_ALLOC_NOBUSY do not exclusive busy the page 1689 * VM_ALLOC_NOOBJ page is not associated with an object and 1690 * should not be exclusive busy 1691 * VM_ALLOC_SBUSY shared busy the allocated page 1692 * VM_ALLOC_WIRED wire the allocated page 1693 * VM_ALLOC_ZERO prefer a zeroed page 1694 * 1695 * This routine may not sleep. 1696 */ 1697 vm_page_t 1698 vm_page_alloc_contig(vm_object_t object, vm_pindex_t pindex, int req, 1699 u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment, 1700 vm_paddr_t boundary, vm_memattr_t memattr) 1701 { 1702 struct vnode *drop; 1703 struct spglist deferred_vdrop_list; 1704 vm_page_t m, m_tmp, m_ret; 1705 u_int flags, oflags; 1706 int req_class; 1707 1708 KASSERT((object != NULL) == ((req & VM_ALLOC_NOOBJ) == 0) && 1709 (object != NULL || (req & VM_ALLOC_SBUSY) == 0) && 1710 ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) != 1711 (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)), 1712 ("vm_page_alloc: inconsistent object(%p)/req(%x)", (void *)object, 1713 req)); 1714 if (object != NULL) { 1715 VM_OBJECT_ASSERT_WLOCKED(object); 1716 KASSERT(object->type == OBJT_PHYS, 1717 ("vm_page_alloc_contig: object %p isn't OBJT_PHYS", 1718 object)); 1719 } 1720 KASSERT(npages > 0, ("vm_page_alloc_contig: npages is zero")); 1721 req_class = req & VM_ALLOC_CLASS_MASK; 1722 1723 /* 1724 * The page daemon is allowed to dig deeper into the free page list. 1725 */ 1726 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT) 1727 req_class = VM_ALLOC_SYSTEM; 1728 1729 SLIST_INIT(&deferred_vdrop_list); 1730 mtx_lock(&vm_page_queue_free_mtx); 1731 if (cnt.v_free_count + cnt.v_cache_count >= npages + 1732 cnt.v_free_reserved || (req_class == VM_ALLOC_SYSTEM && 1733 cnt.v_free_count + cnt.v_cache_count >= npages + 1734 cnt.v_interrupt_free_min) || (req_class == VM_ALLOC_INTERRUPT && 1735 cnt.v_free_count + cnt.v_cache_count >= npages)) { 1736 #if VM_NRESERVLEVEL > 0 1737 retry: 1738 if (object == NULL || (object->flags & OBJ_COLORED) == 0 || 1739 (m_ret = vm_reserv_alloc_contig(object, pindex, npages, 1740 low, high, alignment, boundary)) == NULL) 1741 #endif 1742 m_ret = vm_phys_alloc_contig(npages, low, high, 1743 alignment, boundary); 1744 } else { 1745 mtx_unlock(&vm_page_queue_free_mtx); 1746 atomic_add_int(&vm_pageout_deficit, npages); 1747 pagedaemon_wakeup(); 1748 return (NULL); 1749 } 1750 if (m_ret != NULL) 1751 for (m = m_ret; m < &m_ret[npages]; m++) { 1752 drop = vm_page_alloc_init(m); 1753 if (drop != NULL) { 1754 /* 1755 * Enqueue the vnode for deferred vdrop(). 1756 */ 1757 m->plinks.s.pv = drop; 1758 SLIST_INSERT_HEAD(&deferred_vdrop_list, m, 1759 plinks.s.ss); 1760 } 1761 } 1762 else { 1763 #if VM_NRESERVLEVEL > 0 1764 if (vm_reserv_reclaim_contig(npages, low, high, alignment, 1765 boundary)) 1766 goto retry; 1767 #endif 1768 } 1769 mtx_unlock(&vm_page_queue_free_mtx); 1770 if (m_ret == NULL) 1771 return (NULL); 1772 1773 /* 1774 * Initialize the pages. Only the PG_ZERO flag is inherited. 1775 */ 1776 flags = 0; 1777 if ((req & VM_ALLOC_ZERO) != 0) 1778 flags = PG_ZERO; 1779 if ((req & VM_ALLOC_NODUMP) != 0) 1780 flags |= PG_NODUMP; 1781 if ((req & VM_ALLOC_WIRED) != 0) 1782 atomic_add_int(&cnt.v_wire_count, npages); 1783 oflags = VPO_UNMANAGED; 1784 if (object != NULL) { 1785 if (object->memattr != VM_MEMATTR_DEFAULT && 1786 memattr == VM_MEMATTR_DEFAULT) 1787 memattr = object->memattr; 1788 } 1789 for (m = m_ret; m < &m_ret[npages]; m++) { 1790 m->aflags = 0; 1791 m->flags = (m->flags | PG_NODUMP) & flags; 1792 m->busy_lock = VPB_UNBUSIED; 1793 if (object != NULL) { 1794 if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) == 0) 1795 m->busy_lock = VPB_SINGLE_EXCLUSIVER; 1796 if ((req & VM_ALLOC_SBUSY) != 0) 1797 m->busy_lock = VPB_SHARERS_WORD(1); 1798 } 1799 if ((req & VM_ALLOC_WIRED) != 0) 1800 m->wire_count = 1; 1801 /* Unmanaged pages don't use "act_count". */ 1802 m->oflags = oflags; 1803 if (object != NULL) { 1804 if (vm_page_insert(m, object, pindex)) { 1805 vm_page_alloc_contig_vdrop( 1806 &deferred_vdrop_list); 1807 if (vm_paging_needed()) 1808 pagedaemon_wakeup(); 1809 if ((req & VM_ALLOC_WIRED) != 0) 1810 atomic_subtract_int(&cnt.v_wire_count, 1811 npages); 1812 for (m_tmp = m, m = m_ret; 1813 m < &m_ret[npages]; m++) { 1814 if ((req & VM_ALLOC_WIRED) != 0) 1815 m->wire_count = 0; 1816 if (m >= m_tmp) 1817 m->object = NULL; 1818 vm_page_free(m); 1819 } 1820 return (NULL); 1821 } 1822 } else 1823 m->pindex = pindex; 1824 if (memattr != VM_MEMATTR_DEFAULT) 1825 pmap_page_set_memattr(m, memattr); 1826 pindex++; 1827 } 1828 vm_page_alloc_contig_vdrop(&deferred_vdrop_list); 1829 if (vm_paging_needed()) 1830 pagedaemon_wakeup(); 1831 return (m_ret); 1832 } 1833 1834 /* 1835 * Initialize a page that has been freshly dequeued from a freelist. 1836 * The caller has to drop the vnode returned, if it is not NULL. 1837 * 1838 * This function may only be used to initialize unmanaged pages. 1839 * 1840 * To be called with vm_page_queue_free_mtx held. 1841 */ 1842 static struct vnode * 1843 vm_page_alloc_init(vm_page_t m) 1844 { 1845 struct vnode *drop; 1846 vm_object_t m_object; 1847 1848 KASSERT(m->queue == PQ_NONE, 1849 ("vm_page_alloc_init: page %p has unexpected queue %d", 1850 m, m->queue)); 1851 KASSERT(m->wire_count == 0, 1852 ("vm_page_alloc_init: page %p is wired", m)); 1853 KASSERT(m->hold_count == 0, 1854 ("vm_page_alloc_init: page %p is held", m)); 1855 KASSERT(!vm_page_sbusied(m), 1856 ("vm_page_alloc_init: page %p is busy", m)); 1857 KASSERT(m->dirty == 0, 1858 ("vm_page_alloc_init: page %p is dirty", m)); 1859 KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT, 1860 ("vm_page_alloc_init: page %p has unexpected memattr %d", 1861 m, pmap_page_get_memattr(m))); 1862 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 1863 drop = NULL; 1864 if ((m->flags & PG_CACHED) != 0) { 1865 KASSERT((m->flags & PG_ZERO) == 0, 1866 ("vm_page_alloc_init: cached page %p is PG_ZERO", m)); 1867 m->valid = 0; 1868 m_object = m->object; 1869 vm_page_cache_remove(m); 1870 if (m_object->type == OBJT_VNODE && 1871 vm_object_cache_is_empty(m_object)) 1872 drop = m_object->handle; 1873 } else { 1874 KASSERT(VM_PAGE_IS_FREE(m), 1875 ("vm_page_alloc_init: page %p is not free", m)); 1876 KASSERT(m->valid == 0, 1877 ("vm_page_alloc_init: free page %p is valid", m)); 1878 vm_phys_freecnt_adj(m, -1); 1879 if ((m->flags & PG_ZERO) != 0) 1880 vm_page_zero_count--; 1881 } 1882 /* Don't clear the PG_ZERO flag; we'll need it later. */ 1883 m->flags &= PG_ZERO; 1884 return (drop); 1885 } 1886 1887 /* 1888 * vm_page_alloc_freelist: 1889 * 1890 * Allocate a physical page from the specified free page list. 1891 * 1892 * The caller must always specify an allocation class. 1893 * 1894 * allocation classes: 1895 * VM_ALLOC_NORMAL normal process request 1896 * VM_ALLOC_SYSTEM system *really* needs a page 1897 * VM_ALLOC_INTERRUPT interrupt time request 1898 * 1899 * optional allocation flags: 1900 * VM_ALLOC_COUNT(number) the number of additional pages that the caller 1901 * intends to allocate 1902 * VM_ALLOC_WIRED wire the allocated page 1903 * VM_ALLOC_ZERO prefer a zeroed page 1904 * 1905 * This routine may not sleep. 1906 */ 1907 vm_page_t 1908 vm_page_alloc_freelist(int flind, int req) 1909 { 1910 struct vnode *drop; 1911 vm_page_t m; 1912 u_int flags; 1913 int req_class; 1914 1915 req_class = req & VM_ALLOC_CLASS_MASK; 1916 1917 /* 1918 * The page daemon is allowed to dig deeper into the free page list. 1919 */ 1920 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT) 1921 req_class = VM_ALLOC_SYSTEM; 1922 1923 /* 1924 * Do not allocate reserved pages unless the req has asked for it. 1925 */ 1926 mtx_lock_flags(&vm_page_queue_free_mtx, MTX_RECURSE); 1927 if (cnt.v_free_count + cnt.v_cache_count > cnt.v_free_reserved || 1928 (req_class == VM_ALLOC_SYSTEM && 1929 cnt.v_free_count + cnt.v_cache_count > cnt.v_interrupt_free_min) || 1930 (req_class == VM_ALLOC_INTERRUPT && 1931 cnt.v_free_count + cnt.v_cache_count > 0)) 1932 m = vm_phys_alloc_freelist_pages(flind, VM_FREEPOOL_DIRECT, 0); 1933 else { 1934 mtx_unlock(&vm_page_queue_free_mtx); 1935 atomic_add_int(&vm_pageout_deficit, 1936 max((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1)); 1937 pagedaemon_wakeup(); 1938 return (NULL); 1939 } 1940 if (m == NULL) { 1941 mtx_unlock(&vm_page_queue_free_mtx); 1942 return (NULL); 1943 } 1944 drop = vm_page_alloc_init(m); 1945 mtx_unlock(&vm_page_queue_free_mtx); 1946 1947 /* 1948 * Initialize the page. Only the PG_ZERO flag is inherited. 1949 */ 1950 m->aflags = 0; 1951 flags = 0; 1952 if ((req & VM_ALLOC_ZERO) != 0) 1953 flags = PG_ZERO; 1954 m->flags &= flags; 1955 if ((req & VM_ALLOC_WIRED) != 0) { 1956 /* 1957 * The page lock is not required for wiring a page that does 1958 * not belong to an object. 1959 */ 1960 atomic_add_int(&cnt.v_wire_count, 1); 1961 m->wire_count = 1; 1962 } 1963 /* Unmanaged pages don't use "act_count". */ 1964 m->oflags = VPO_UNMANAGED; 1965 if (drop != NULL) 1966 vdrop(drop); 1967 if (vm_paging_needed()) 1968 pagedaemon_wakeup(); 1969 return (m); 1970 } 1971 1972 /* 1973 * vm_wait: (also see VM_WAIT macro) 1974 * 1975 * Sleep until free pages are available for allocation. 1976 * - Called in various places before memory allocations. 1977 */ 1978 void 1979 vm_wait(void) 1980 { 1981 1982 mtx_lock(&vm_page_queue_free_mtx); 1983 if (curproc == pageproc) { 1984 vm_pageout_pages_needed = 1; 1985 msleep(&vm_pageout_pages_needed, &vm_page_queue_free_mtx, 1986 PDROP | PSWP, "VMWait", 0); 1987 } else { 1988 if (!vm_pages_needed) { 1989 vm_pages_needed = 1; 1990 wakeup(&vm_pages_needed); 1991 } 1992 msleep(&cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PVM, 1993 "vmwait", 0); 1994 } 1995 } 1996 1997 /* 1998 * vm_waitpfault: (also see VM_WAITPFAULT macro) 1999 * 2000 * Sleep until free pages are available for allocation. 2001 * - Called only in vm_fault so that processes page faulting 2002 * can be easily tracked. 2003 * - Sleeps at a lower priority than vm_wait() so that vm_wait()ing 2004 * processes will be able to grab memory first. Do not change 2005 * this balance without careful testing first. 2006 */ 2007 void 2008 vm_waitpfault(void) 2009 { 2010 2011 mtx_lock(&vm_page_queue_free_mtx); 2012 if (!vm_pages_needed) { 2013 vm_pages_needed = 1; 2014 wakeup(&vm_pages_needed); 2015 } 2016 msleep(&cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PUSER, 2017 "pfault", 0); 2018 } 2019 2020 struct vm_pagequeue * 2021 vm_page_pagequeue(vm_page_t m) 2022 { 2023 2024 return (&vm_phys_domain(m)->vmd_pagequeues[m->queue]); 2025 } 2026 2027 /* 2028 * vm_page_dequeue: 2029 * 2030 * Remove the given page from its current page queue. 2031 * 2032 * The page must be locked. 2033 */ 2034 void 2035 vm_page_dequeue(vm_page_t m) 2036 { 2037 struct vm_pagequeue *pq; 2038 2039 vm_page_lock_assert(m, MA_OWNED); 2040 KASSERT(m->queue != PQ_NONE, 2041 ("vm_page_dequeue: page %p is not queued", m)); 2042 pq = vm_page_pagequeue(m); 2043 vm_pagequeue_lock(pq); 2044 m->queue = PQ_NONE; 2045 TAILQ_REMOVE(&pq->pq_pl, m, plinks.q); 2046 vm_pagequeue_cnt_dec(pq); 2047 vm_pagequeue_unlock(pq); 2048 } 2049 2050 /* 2051 * vm_page_dequeue_locked: 2052 * 2053 * Remove the given page from its current page queue. 2054 * 2055 * The page and page queue must be locked. 2056 */ 2057 void 2058 vm_page_dequeue_locked(vm_page_t m) 2059 { 2060 struct vm_pagequeue *pq; 2061 2062 vm_page_lock_assert(m, MA_OWNED); 2063 pq = vm_page_pagequeue(m); 2064 vm_pagequeue_assert_locked(pq); 2065 m->queue = PQ_NONE; 2066 TAILQ_REMOVE(&pq->pq_pl, m, plinks.q); 2067 vm_pagequeue_cnt_dec(pq); 2068 } 2069 2070 /* 2071 * vm_page_enqueue: 2072 * 2073 * Add the given page to the specified page queue. 2074 * 2075 * The page must be locked. 2076 */ 2077 static void 2078 vm_page_enqueue(int queue, vm_page_t m) 2079 { 2080 struct vm_pagequeue *pq; 2081 2082 vm_page_lock_assert(m, MA_OWNED); 2083 pq = &vm_phys_domain(m)->vmd_pagequeues[queue]; 2084 vm_pagequeue_lock(pq); 2085 m->queue = queue; 2086 TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q); 2087 vm_pagequeue_cnt_inc(pq); 2088 vm_pagequeue_unlock(pq); 2089 } 2090 2091 /* 2092 * vm_page_requeue: 2093 * 2094 * Move the given page to the tail of its current page queue. 2095 * 2096 * The page must be locked. 2097 */ 2098 void 2099 vm_page_requeue(vm_page_t m) 2100 { 2101 struct vm_pagequeue *pq; 2102 2103 vm_page_lock_assert(m, MA_OWNED); 2104 KASSERT(m->queue != PQ_NONE, 2105 ("vm_page_requeue: page %p is not queued", m)); 2106 pq = vm_page_pagequeue(m); 2107 vm_pagequeue_lock(pq); 2108 TAILQ_REMOVE(&pq->pq_pl, m, plinks.q); 2109 TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q); 2110 vm_pagequeue_unlock(pq); 2111 } 2112 2113 /* 2114 * vm_page_requeue_locked: 2115 * 2116 * Move the given page to the tail of its current page queue. 2117 * 2118 * The page queue must be locked. 2119 */ 2120 void 2121 vm_page_requeue_locked(vm_page_t m) 2122 { 2123 struct vm_pagequeue *pq; 2124 2125 KASSERT(m->queue != PQ_NONE, 2126 ("vm_page_requeue_locked: page %p is not queued", m)); 2127 pq = vm_page_pagequeue(m); 2128 vm_pagequeue_assert_locked(pq); 2129 TAILQ_REMOVE(&pq->pq_pl, m, plinks.q); 2130 TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q); 2131 } 2132 2133 /* 2134 * vm_page_activate: 2135 * 2136 * Put the specified page on the active list (if appropriate). 2137 * Ensure that act_count is at least ACT_INIT but do not otherwise 2138 * mess with it. 2139 * 2140 * The page must be locked. 2141 */ 2142 void 2143 vm_page_activate(vm_page_t m) 2144 { 2145 int queue; 2146 2147 vm_page_lock_assert(m, MA_OWNED); 2148 if ((queue = m->queue) != PQ_ACTIVE) { 2149 if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) { 2150 if (m->act_count < ACT_INIT) 2151 m->act_count = ACT_INIT; 2152 if (queue != PQ_NONE) 2153 vm_page_dequeue(m); 2154 vm_page_enqueue(PQ_ACTIVE, m); 2155 } else 2156 KASSERT(queue == PQ_NONE, 2157 ("vm_page_activate: wired page %p is queued", m)); 2158 } else { 2159 if (m->act_count < ACT_INIT) 2160 m->act_count = ACT_INIT; 2161 } 2162 } 2163 2164 /* 2165 * vm_page_free_wakeup: 2166 * 2167 * Helper routine for vm_page_free_toq() and vm_page_cache(). This 2168 * routine is called when a page has been added to the cache or free 2169 * queues. 2170 * 2171 * The page queues must be locked. 2172 */ 2173 static inline void 2174 vm_page_free_wakeup(void) 2175 { 2176 2177 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 2178 /* 2179 * if pageout daemon needs pages, then tell it that there are 2180 * some free. 2181 */ 2182 if (vm_pageout_pages_needed && 2183 cnt.v_cache_count + cnt.v_free_count >= cnt.v_pageout_free_min) { 2184 wakeup(&vm_pageout_pages_needed); 2185 vm_pageout_pages_needed = 0; 2186 } 2187 /* 2188 * wakeup processes that are waiting on memory if we hit a 2189 * high water mark. And wakeup scheduler process if we have 2190 * lots of memory. this process will swapin processes. 2191 */ 2192 if (vm_pages_needed && !vm_page_count_min()) { 2193 vm_pages_needed = 0; 2194 wakeup(&cnt.v_free_count); 2195 } 2196 } 2197 2198 /* 2199 * Turn a cached page into a free page, by changing its attributes. 2200 * Keep the statistics up-to-date. 2201 * 2202 * The free page queue must be locked. 2203 */ 2204 static void 2205 vm_page_cache_turn_free(vm_page_t m) 2206 { 2207 2208 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 2209 2210 m->object = NULL; 2211 m->valid = 0; 2212 /* Clear PG_CACHED and set PG_FREE. */ 2213 m->flags ^= PG_CACHED | PG_FREE; 2214 KASSERT((m->flags & (PG_CACHED | PG_FREE)) == PG_FREE, 2215 ("vm_page_cache_free: page %p has inconsistent flags", m)); 2216 cnt.v_cache_count--; 2217 vm_phys_freecnt_adj(m, 1); 2218 } 2219 2220 /* 2221 * vm_page_free_toq: 2222 * 2223 * Returns the given page to the free list, 2224 * disassociating it with any VM object. 2225 * 2226 * The object must be locked. The page must be locked if it is managed. 2227 */ 2228 void 2229 vm_page_free_toq(vm_page_t m) 2230 { 2231 2232 if ((m->oflags & VPO_UNMANAGED) == 0) { 2233 vm_page_lock_assert(m, MA_OWNED); 2234 KASSERT(!pmap_page_is_mapped(m), 2235 ("vm_page_free_toq: freeing mapped page %p", m)); 2236 } else 2237 KASSERT(m->queue == PQ_NONE, 2238 ("vm_page_free_toq: unmanaged page %p is queued", m)); 2239 PCPU_INC(cnt.v_tfree); 2240 2241 if (VM_PAGE_IS_FREE(m)) 2242 panic("vm_page_free: freeing free page %p", m); 2243 else if (vm_page_sbusied(m)) 2244 panic("vm_page_free: freeing busy page %p", m); 2245 2246 /* 2247 * Unqueue, then remove page. Note that we cannot destroy 2248 * the page here because we do not want to call the pager's 2249 * callback routine until after we've put the page on the 2250 * appropriate free queue. 2251 */ 2252 vm_page_remque(m); 2253 vm_page_remove(m); 2254 2255 /* 2256 * If fictitious remove object association and 2257 * return, otherwise delay object association removal. 2258 */ 2259 if ((m->flags & PG_FICTITIOUS) != 0) { 2260 return; 2261 } 2262 2263 m->valid = 0; 2264 vm_page_undirty(m); 2265 2266 if (m->wire_count != 0) 2267 panic("vm_page_free: freeing wired page %p", m); 2268 if (m->hold_count != 0) { 2269 m->flags &= ~PG_ZERO; 2270 KASSERT((m->flags & PG_UNHOLDFREE) == 0, 2271 ("vm_page_free: freeing PG_UNHOLDFREE page %p", m)); 2272 m->flags |= PG_UNHOLDFREE; 2273 } else { 2274 /* 2275 * Restore the default memory attribute to the page. 2276 */ 2277 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT) 2278 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT); 2279 2280 /* 2281 * Insert the page into the physical memory allocator's 2282 * cache/free page queues. 2283 */ 2284 mtx_lock(&vm_page_queue_free_mtx); 2285 m->flags |= PG_FREE; 2286 vm_phys_freecnt_adj(m, 1); 2287 #if VM_NRESERVLEVEL > 0 2288 if (!vm_reserv_free_page(m)) 2289 #else 2290 if (TRUE) 2291 #endif 2292 vm_phys_free_pages(m, 0); 2293 if ((m->flags & PG_ZERO) != 0) 2294 ++vm_page_zero_count; 2295 else 2296 vm_page_zero_idle_wakeup(); 2297 vm_page_free_wakeup(); 2298 mtx_unlock(&vm_page_queue_free_mtx); 2299 } 2300 } 2301 2302 /* 2303 * vm_page_wire: 2304 * 2305 * Mark this page as wired down by yet 2306 * another map, removing it from paging queues 2307 * as necessary. 2308 * 2309 * If the page is fictitious, then its wire count must remain one. 2310 * 2311 * The page must be locked. 2312 */ 2313 void 2314 vm_page_wire(vm_page_t m) 2315 { 2316 2317 /* 2318 * Only bump the wire statistics if the page is not already wired, 2319 * and only unqueue the page if it is on some queue (if it is unmanaged 2320 * it is already off the queues). 2321 */ 2322 vm_page_lock_assert(m, MA_OWNED); 2323 if ((m->flags & PG_FICTITIOUS) != 0) { 2324 KASSERT(m->wire_count == 1, 2325 ("vm_page_wire: fictitious page %p's wire count isn't one", 2326 m)); 2327 return; 2328 } 2329 if (m->wire_count == 0) { 2330 KASSERT((m->oflags & VPO_UNMANAGED) == 0 || 2331 m->queue == PQ_NONE, 2332 ("vm_page_wire: unmanaged page %p is queued", m)); 2333 vm_page_remque(m); 2334 atomic_add_int(&cnt.v_wire_count, 1); 2335 } 2336 m->wire_count++; 2337 KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m)); 2338 } 2339 2340 /* 2341 * vm_page_unwire: 2342 * 2343 * Release one wiring of the specified page, potentially enabling it to be 2344 * paged again. If paging is enabled, then the value of the parameter 2345 * "activate" determines to which queue the page is added. If "activate" is 2346 * non-zero, then the page is added to the active queue. Otherwise, it is 2347 * added to the inactive queue. 2348 * 2349 * However, unless the page belongs to an object, it is not enqueued because 2350 * it cannot be paged out. 2351 * 2352 * If a page is fictitious, then its wire count must always be one. 2353 * 2354 * A managed page must be locked. 2355 */ 2356 void 2357 vm_page_unwire(vm_page_t m, int activate) 2358 { 2359 2360 if ((m->oflags & VPO_UNMANAGED) == 0) 2361 vm_page_lock_assert(m, MA_OWNED); 2362 if ((m->flags & PG_FICTITIOUS) != 0) { 2363 KASSERT(m->wire_count == 1, 2364 ("vm_page_unwire: fictitious page %p's wire count isn't one", m)); 2365 return; 2366 } 2367 if (m->wire_count > 0) { 2368 m->wire_count--; 2369 if (m->wire_count == 0) { 2370 atomic_subtract_int(&cnt.v_wire_count, 1); 2371 if ((m->oflags & VPO_UNMANAGED) != 0 || 2372 m->object == NULL) 2373 return; 2374 if (!activate) 2375 m->flags &= ~PG_WINATCFLS; 2376 vm_page_enqueue(activate ? PQ_ACTIVE : PQ_INACTIVE, m); 2377 } 2378 } else 2379 panic("vm_page_unwire: page %p's wire count is zero", m); 2380 } 2381 2382 /* 2383 * Move the specified page to the inactive queue. 2384 * 2385 * Many pages placed on the inactive queue should actually go 2386 * into the cache, but it is difficult to figure out which. What 2387 * we do instead, if the inactive target is well met, is to put 2388 * clean pages at the head of the inactive queue instead of the tail. 2389 * This will cause them to be moved to the cache more quickly and 2390 * if not actively re-referenced, reclaimed more quickly. If we just 2391 * stick these pages at the end of the inactive queue, heavy filesystem 2392 * meta-data accesses can cause an unnecessary paging load on memory bound 2393 * processes. This optimization causes one-time-use metadata to be 2394 * reused more quickly. 2395 * 2396 * Normally athead is 0 resulting in LRU operation. athead is set 2397 * to 1 if we want this page to be 'as if it were placed in the cache', 2398 * except without unmapping it from the process address space. 2399 * 2400 * The page must be locked. 2401 */ 2402 static inline void 2403 _vm_page_deactivate(vm_page_t m, int athead) 2404 { 2405 struct vm_pagequeue *pq; 2406 int queue; 2407 2408 vm_page_lock_assert(m, MA_OWNED); 2409 2410 /* 2411 * Ignore if already inactive. 2412 */ 2413 if ((queue = m->queue) == PQ_INACTIVE) 2414 return; 2415 if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) { 2416 if (queue != PQ_NONE) 2417 vm_page_dequeue(m); 2418 m->flags &= ~PG_WINATCFLS; 2419 pq = &vm_phys_domain(m)->vmd_pagequeues[PQ_INACTIVE]; 2420 vm_pagequeue_lock(pq); 2421 m->queue = PQ_INACTIVE; 2422 if (athead) 2423 TAILQ_INSERT_HEAD(&pq->pq_pl, m, plinks.q); 2424 else 2425 TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q); 2426 vm_pagequeue_cnt_inc(pq); 2427 vm_pagequeue_unlock(pq); 2428 } 2429 } 2430 2431 /* 2432 * Move the specified page to the inactive queue. 2433 * 2434 * The page must be locked. 2435 */ 2436 void 2437 vm_page_deactivate(vm_page_t m) 2438 { 2439 2440 _vm_page_deactivate(m, 0); 2441 } 2442 2443 /* 2444 * vm_page_try_to_cache: 2445 * 2446 * Returns 0 on failure, 1 on success 2447 */ 2448 int 2449 vm_page_try_to_cache(vm_page_t m) 2450 { 2451 2452 vm_page_lock_assert(m, MA_OWNED); 2453 VM_OBJECT_ASSERT_WLOCKED(m->object); 2454 if (m->dirty || m->hold_count || m->wire_count || 2455 (m->oflags & VPO_UNMANAGED) != 0 || vm_page_busied(m)) 2456 return (0); 2457 pmap_remove_all(m); 2458 if (m->dirty) 2459 return (0); 2460 vm_page_cache(m); 2461 return (1); 2462 } 2463 2464 /* 2465 * vm_page_try_to_free() 2466 * 2467 * Attempt to free the page. If we cannot free it, we do nothing. 2468 * 1 is returned on success, 0 on failure. 2469 */ 2470 int 2471 vm_page_try_to_free(vm_page_t m) 2472 { 2473 2474 vm_page_lock_assert(m, MA_OWNED); 2475 if (m->object != NULL) 2476 VM_OBJECT_ASSERT_WLOCKED(m->object); 2477 if (m->dirty || m->hold_count || m->wire_count || 2478 (m->oflags & VPO_UNMANAGED) != 0 || vm_page_busied(m)) 2479 return (0); 2480 pmap_remove_all(m); 2481 if (m->dirty) 2482 return (0); 2483 vm_page_free(m); 2484 return (1); 2485 } 2486 2487 /* 2488 * vm_page_cache 2489 * 2490 * Put the specified page onto the page cache queue (if appropriate). 2491 * 2492 * The object and page must be locked. 2493 */ 2494 void 2495 vm_page_cache(vm_page_t m) 2496 { 2497 vm_object_t object; 2498 boolean_t cache_was_empty; 2499 2500 vm_page_lock_assert(m, MA_OWNED); 2501 object = m->object; 2502 VM_OBJECT_ASSERT_WLOCKED(object); 2503 if (vm_page_busied(m) || (m->oflags & VPO_UNMANAGED) || 2504 m->hold_count || m->wire_count) 2505 panic("vm_page_cache: attempting to cache busy page"); 2506 KASSERT(!pmap_page_is_mapped(m), 2507 ("vm_page_cache: page %p is mapped", m)); 2508 KASSERT(m->dirty == 0, ("vm_page_cache: page %p is dirty", m)); 2509 if (m->valid == 0 || object->type == OBJT_DEFAULT || 2510 (object->type == OBJT_SWAP && 2511 !vm_pager_has_page(object, m->pindex, NULL, NULL))) { 2512 /* 2513 * Hypothesis: A cache-elgible page belonging to a 2514 * default object or swap object but without a backing 2515 * store must be zero filled. 2516 */ 2517 vm_page_free(m); 2518 return; 2519 } 2520 KASSERT((m->flags & PG_CACHED) == 0, 2521 ("vm_page_cache: page %p is already cached", m)); 2522 2523 /* 2524 * Remove the page from the paging queues. 2525 */ 2526 vm_page_remque(m); 2527 2528 /* 2529 * Remove the page from the object's collection of resident 2530 * pages. 2531 */ 2532 vm_radix_remove(&object->rtree, m->pindex); 2533 TAILQ_REMOVE(&object->memq, m, listq); 2534 object->resident_page_count--; 2535 2536 /* 2537 * Restore the default memory attribute to the page. 2538 */ 2539 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT) 2540 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT); 2541 2542 /* 2543 * Insert the page into the object's collection of cached pages 2544 * and the physical memory allocator's cache/free page queues. 2545 */ 2546 m->flags &= ~PG_ZERO; 2547 mtx_lock(&vm_page_queue_free_mtx); 2548 cache_was_empty = vm_radix_is_empty(&object->cache); 2549 if (vm_radix_insert(&object->cache, m)) { 2550 mtx_unlock(&vm_page_queue_free_mtx); 2551 if (object->resident_page_count == 0) 2552 vdrop(object->handle); 2553 m->object = NULL; 2554 vm_page_free(m); 2555 return; 2556 } 2557 2558 /* 2559 * The above call to vm_radix_insert() could reclaim the one pre- 2560 * existing cached page from this object, resulting in a call to 2561 * vdrop(). 2562 */ 2563 if (!cache_was_empty) 2564 cache_was_empty = vm_radix_is_singleton(&object->cache); 2565 2566 m->flags |= PG_CACHED; 2567 cnt.v_cache_count++; 2568 PCPU_INC(cnt.v_tcached); 2569 #if VM_NRESERVLEVEL > 0 2570 if (!vm_reserv_free_page(m)) { 2571 #else 2572 if (TRUE) { 2573 #endif 2574 vm_phys_set_pool(VM_FREEPOOL_CACHE, m, 0); 2575 vm_phys_free_pages(m, 0); 2576 } 2577 vm_page_free_wakeup(); 2578 mtx_unlock(&vm_page_queue_free_mtx); 2579 2580 /* 2581 * Increment the vnode's hold count if this is the object's only 2582 * cached page. Decrement the vnode's hold count if this was 2583 * the object's only resident page. 2584 */ 2585 if (object->type == OBJT_VNODE) { 2586 if (cache_was_empty && object->resident_page_count != 0) 2587 vhold(object->handle); 2588 else if (!cache_was_empty && object->resident_page_count == 0) 2589 vdrop(object->handle); 2590 } 2591 } 2592 2593 /* 2594 * vm_page_advise 2595 * 2596 * Cache, deactivate, or do nothing as appropriate. This routine 2597 * is used by madvise(). 2598 * 2599 * Generally speaking we want to move the page into the cache so 2600 * it gets reused quickly. However, this can result in a silly syndrome 2601 * due to the page recycling too quickly. Small objects will not be 2602 * fully cached. On the other hand, if we move the page to the inactive 2603 * queue we wind up with a problem whereby very large objects 2604 * unnecessarily blow away our inactive and cache queues. 2605 * 2606 * The solution is to move the pages based on a fixed weighting. We 2607 * either leave them alone, deactivate them, or move them to the cache, 2608 * where moving them to the cache has the highest weighting. 2609 * By forcing some pages into other queues we eventually force the 2610 * system to balance the queues, potentially recovering other unrelated 2611 * space from active. The idea is to not force this to happen too 2612 * often. 2613 * 2614 * The object and page must be locked. 2615 */ 2616 void 2617 vm_page_advise(vm_page_t m, int advice) 2618 { 2619 int dnw, head; 2620 2621 vm_page_assert_locked(m); 2622 VM_OBJECT_ASSERT_WLOCKED(m->object); 2623 if (advice == MADV_FREE) { 2624 /* 2625 * Mark the page clean. This will allow the page to be freed 2626 * up by the system. However, such pages are often reused 2627 * quickly by malloc() so we do not do anything that would 2628 * cause a page fault if we can help it. 2629 * 2630 * Specifically, we do not try to actually free the page now 2631 * nor do we try to put it in the cache (which would cause a 2632 * page fault on reuse). 2633 * 2634 * But we do make the page is freeable as we can without 2635 * actually taking the step of unmapping it. 2636 */ 2637 m->dirty = 0; 2638 m->act_count = 0; 2639 } else if (advice != MADV_DONTNEED) 2640 return; 2641 dnw = PCPU_GET(dnweight); 2642 PCPU_INC(dnweight); 2643 2644 /* 2645 * Occasionally leave the page alone. 2646 */ 2647 if ((dnw & 0x01F0) == 0 || m->queue == PQ_INACTIVE) { 2648 if (m->act_count >= ACT_INIT) 2649 --m->act_count; 2650 return; 2651 } 2652 2653 /* 2654 * Clear any references to the page. Otherwise, the page daemon will 2655 * immediately reactivate the page. 2656 */ 2657 vm_page_aflag_clear(m, PGA_REFERENCED); 2658 2659 if (advice != MADV_FREE && m->dirty == 0 && pmap_is_modified(m)) 2660 vm_page_dirty(m); 2661 2662 if (m->dirty || (dnw & 0x0070) == 0) { 2663 /* 2664 * Deactivate the page 3 times out of 32. 2665 */ 2666 head = 0; 2667 } else { 2668 /* 2669 * Cache the page 28 times out of every 32. Note that 2670 * the page is deactivated instead of cached, but placed 2671 * at the head of the queue instead of the tail. 2672 */ 2673 head = 1; 2674 } 2675 _vm_page_deactivate(m, head); 2676 } 2677 2678 /* 2679 * Grab a page, waiting until we are waken up due to the page 2680 * changing state. We keep on waiting, if the page continues 2681 * to be in the object. If the page doesn't exist, first allocate it 2682 * and then conditionally zero it. 2683 * 2684 * This routine may sleep. 2685 * 2686 * The object must be locked on entry. The lock will, however, be released 2687 * and reacquired if the routine sleeps. 2688 */ 2689 vm_page_t 2690 vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags) 2691 { 2692 vm_page_t m; 2693 int sleep; 2694 2695 VM_OBJECT_ASSERT_WLOCKED(object); 2696 KASSERT((allocflags & VM_ALLOC_SBUSY) == 0 || 2697 (allocflags & VM_ALLOC_IGN_SBUSY) != 0, 2698 ("vm_page_grab: VM_ALLOC_SBUSY/VM_ALLOC_IGN_SBUSY mismatch")); 2699 retrylookup: 2700 if ((m = vm_page_lookup(object, pindex)) != NULL) { 2701 sleep = (allocflags & VM_ALLOC_IGN_SBUSY) != 0 ? 2702 vm_page_xbusied(m) : vm_page_busied(m); 2703 if (sleep) { 2704 /* 2705 * Reference the page before unlocking and 2706 * sleeping so that the page daemon is less 2707 * likely to reclaim it. 2708 */ 2709 vm_page_aflag_set(m, PGA_REFERENCED); 2710 vm_page_lock(m); 2711 VM_OBJECT_WUNLOCK(object); 2712 vm_page_busy_sleep(m, "pgrbwt"); 2713 VM_OBJECT_WLOCK(object); 2714 goto retrylookup; 2715 } else { 2716 if ((allocflags & VM_ALLOC_WIRED) != 0) { 2717 vm_page_lock(m); 2718 vm_page_wire(m); 2719 vm_page_unlock(m); 2720 } 2721 if ((allocflags & 2722 (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) == 0) 2723 vm_page_xbusy(m); 2724 if ((allocflags & VM_ALLOC_SBUSY) != 0) 2725 vm_page_sbusy(m); 2726 return (m); 2727 } 2728 } 2729 m = vm_page_alloc(object, pindex, allocflags & ~VM_ALLOC_IGN_SBUSY); 2730 if (m == NULL) { 2731 VM_OBJECT_WUNLOCK(object); 2732 VM_WAIT; 2733 VM_OBJECT_WLOCK(object); 2734 goto retrylookup; 2735 } else if (m->valid != 0) 2736 return (m); 2737 if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0) 2738 pmap_zero_page(m); 2739 return (m); 2740 } 2741 2742 /* 2743 * Mapping function for valid or dirty bits in a page. 2744 * 2745 * Inputs are required to range within a page. 2746 */ 2747 vm_page_bits_t 2748 vm_page_bits(int base, int size) 2749 { 2750 int first_bit; 2751 int last_bit; 2752 2753 KASSERT( 2754 base + size <= PAGE_SIZE, 2755 ("vm_page_bits: illegal base/size %d/%d", base, size) 2756 ); 2757 2758 if (size == 0) /* handle degenerate case */ 2759 return (0); 2760 2761 first_bit = base >> DEV_BSHIFT; 2762 last_bit = (base + size - 1) >> DEV_BSHIFT; 2763 2764 return (((vm_page_bits_t)2 << last_bit) - 2765 ((vm_page_bits_t)1 << first_bit)); 2766 } 2767 2768 /* 2769 * vm_page_set_valid_range: 2770 * 2771 * Sets portions of a page valid. The arguments are expected 2772 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive 2773 * of any partial chunks touched by the range. The invalid portion of 2774 * such chunks will be zeroed. 2775 * 2776 * (base + size) must be less then or equal to PAGE_SIZE. 2777 */ 2778 void 2779 vm_page_set_valid_range(vm_page_t m, int base, int size) 2780 { 2781 int endoff, frag; 2782 2783 VM_OBJECT_ASSERT_WLOCKED(m->object); 2784 if (size == 0) /* handle degenerate case */ 2785 return; 2786 2787 /* 2788 * If the base is not DEV_BSIZE aligned and the valid 2789 * bit is clear, we have to zero out a portion of the 2790 * first block. 2791 */ 2792 if ((frag = base & ~(DEV_BSIZE - 1)) != base && 2793 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0) 2794 pmap_zero_page_area(m, frag, base - frag); 2795 2796 /* 2797 * If the ending offset is not DEV_BSIZE aligned and the 2798 * valid bit is clear, we have to zero out a portion of 2799 * the last block. 2800 */ 2801 endoff = base + size; 2802 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff && 2803 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0) 2804 pmap_zero_page_area(m, endoff, 2805 DEV_BSIZE - (endoff & (DEV_BSIZE - 1))); 2806 2807 /* 2808 * Assert that no previously invalid block that is now being validated 2809 * is already dirty. 2810 */ 2811 KASSERT((~m->valid & vm_page_bits(base, size) & m->dirty) == 0, 2812 ("vm_page_set_valid_range: page %p is dirty", m)); 2813 2814 /* 2815 * Set valid bits inclusive of any overlap. 2816 */ 2817 m->valid |= vm_page_bits(base, size); 2818 } 2819 2820 /* 2821 * Clear the given bits from the specified page's dirty field. 2822 */ 2823 static __inline void 2824 vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits) 2825 { 2826 uintptr_t addr; 2827 #if PAGE_SIZE < 16384 2828 int shift; 2829 #endif 2830 2831 /* 2832 * If the object is locked and the page is neither exclusive busy nor 2833 * write mapped, then the page's dirty field cannot possibly be 2834 * set by a concurrent pmap operation. 2835 */ 2836 VM_OBJECT_ASSERT_WLOCKED(m->object); 2837 if (!vm_page_xbusied(m) && !pmap_page_is_write_mapped(m)) 2838 m->dirty &= ~pagebits; 2839 else { 2840 /* 2841 * The pmap layer can call vm_page_dirty() without 2842 * holding a distinguished lock. The combination of 2843 * the object's lock and an atomic operation suffice 2844 * to guarantee consistency of the page dirty field. 2845 * 2846 * For PAGE_SIZE == 32768 case, compiler already 2847 * properly aligns the dirty field, so no forcible 2848 * alignment is needed. Only require existence of 2849 * atomic_clear_64 when page size is 32768. 2850 */ 2851 addr = (uintptr_t)&m->dirty; 2852 #if PAGE_SIZE == 32768 2853 atomic_clear_64((uint64_t *)addr, pagebits); 2854 #elif PAGE_SIZE == 16384 2855 atomic_clear_32((uint32_t *)addr, pagebits); 2856 #else /* PAGE_SIZE <= 8192 */ 2857 /* 2858 * Use a trick to perform a 32-bit atomic on the 2859 * containing aligned word, to not depend on the existence 2860 * of atomic_clear_{8, 16}. 2861 */ 2862 shift = addr & (sizeof(uint32_t) - 1); 2863 #if BYTE_ORDER == BIG_ENDIAN 2864 shift = (sizeof(uint32_t) - sizeof(m->dirty) - shift) * NBBY; 2865 #else 2866 shift *= NBBY; 2867 #endif 2868 addr &= ~(sizeof(uint32_t) - 1); 2869 atomic_clear_32((uint32_t *)addr, pagebits << shift); 2870 #endif /* PAGE_SIZE */ 2871 } 2872 } 2873 2874 /* 2875 * vm_page_set_validclean: 2876 * 2877 * Sets portions of a page valid and clean. The arguments are expected 2878 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive 2879 * of any partial chunks touched by the range. The invalid portion of 2880 * such chunks will be zero'd. 2881 * 2882 * (base + size) must be less then or equal to PAGE_SIZE. 2883 */ 2884 void 2885 vm_page_set_validclean(vm_page_t m, int base, int size) 2886 { 2887 vm_page_bits_t oldvalid, pagebits; 2888 int endoff, frag; 2889 2890 VM_OBJECT_ASSERT_WLOCKED(m->object); 2891 if (size == 0) /* handle degenerate case */ 2892 return; 2893 2894 /* 2895 * If the base is not DEV_BSIZE aligned and the valid 2896 * bit is clear, we have to zero out a portion of the 2897 * first block. 2898 */ 2899 if ((frag = base & ~(DEV_BSIZE - 1)) != base && 2900 (m->valid & ((vm_page_bits_t)1 << (base >> DEV_BSHIFT))) == 0) 2901 pmap_zero_page_area(m, frag, base - frag); 2902 2903 /* 2904 * If the ending offset is not DEV_BSIZE aligned and the 2905 * valid bit is clear, we have to zero out a portion of 2906 * the last block. 2907 */ 2908 endoff = base + size; 2909 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff && 2910 (m->valid & ((vm_page_bits_t)1 << (endoff >> DEV_BSHIFT))) == 0) 2911 pmap_zero_page_area(m, endoff, 2912 DEV_BSIZE - (endoff & (DEV_BSIZE - 1))); 2913 2914 /* 2915 * Set valid, clear dirty bits. If validating the entire 2916 * page we can safely clear the pmap modify bit. We also 2917 * use this opportunity to clear the VPO_NOSYNC flag. If a process 2918 * takes a write fault on a MAP_NOSYNC memory area the flag will 2919 * be set again. 2920 * 2921 * We set valid bits inclusive of any overlap, but we can only 2922 * clear dirty bits for DEV_BSIZE chunks that are fully within 2923 * the range. 2924 */ 2925 oldvalid = m->valid; 2926 pagebits = vm_page_bits(base, size); 2927 m->valid |= pagebits; 2928 #if 0 /* NOT YET */ 2929 if ((frag = base & (DEV_BSIZE - 1)) != 0) { 2930 frag = DEV_BSIZE - frag; 2931 base += frag; 2932 size -= frag; 2933 if (size < 0) 2934 size = 0; 2935 } 2936 pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1)); 2937 #endif 2938 if (base == 0 && size == PAGE_SIZE) { 2939 /* 2940 * The page can only be modified within the pmap if it is 2941 * mapped, and it can only be mapped if it was previously 2942 * fully valid. 2943 */ 2944 if (oldvalid == VM_PAGE_BITS_ALL) 2945 /* 2946 * Perform the pmap_clear_modify() first. Otherwise, 2947 * a concurrent pmap operation, such as 2948 * pmap_protect(), could clear a modification in the 2949 * pmap and set the dirty field on the page before 2950 * pmap_clear_modify() had begun and after the dirty 2951 * field was cleared here. 2952 */ 2953 pmap_clear_modify(m); 2954 m->dirty = 0; 2955 m->oflags &= ~VPO_NOSYNC; 2956 } else if (oldvalid != VM_PAGE_BITS_ALL) 2957 m->dirty &= ~pagebits; 2958 else 2959 vm_page_clear_dirty_mask(m, pagebits); 2960 } 2961 2962 void 2963 vm_page_clear_dirty(vm_page_t m, int base, int size) 2964 { 2965 2966 vm_page_clear_dirty_mask(m, vm_page_bits(base, size)); 2967 } 2968 2969 /* 2970 * vm_page_set_invalid: 2971 * 2972 * Invalidates DEV_BSIZE'd chunks within a page. Both the 2973 * valid and dirty bits for the effected areas are cleared. 2974 */ 2975 void 2976 vm_page_set_invalid(vm_page_t m, int base, int size) 2977 { 2978 vm_page_bits_t bits; 2979 2980 VM_OBJECT_ASSERT_WLOCKED(m->object); 2981 bits = vm_page_bits(base, size); 2982 if (m->valid == VM_PAGE_BITS_ALL && bits != 0) 2983 pmap_remove_all(m); 2984 KASSERT(!pmap_page_is_mapped(m), 2985 ("vm_page_set_invalid: page %p is mapped", m)); 2986 m->valid &= ~bits; 2987 m->dirty &= ~bits; 2988 } 2989 2990 /* 2991 * vm_page_zero_invalid() 2992 * 2993 * The kernel assumes that the invalid portions of a page contain 2994 * garbage, but such pages can be mapped into memory by user code. 2995 * When this occurs, we must zero out the non-valid portions of the 2996 * page so user code sees what it expects. 2997 * 2998 * Pages are most often semi-valid when the end of a file is mapped 2999 * into memory and the file's size is not page aligned. 3000 */ 3001 void 3002 vm_page_zero_invalid(vm_page_t m, boolean_t setvalid) 3003 { 3004 int b; 3005 int i; 3006 3007 VM_OBJECT_ASSERT_WLOCKED(m->object); 3008 /* 3009 * Scan the valid bits looking for invalid sections that 3010 * must be zerod. Invalid sub-DEV_BSIZE'd areas ( where the 3011 * valid bit may be set ) have already been zerod by 3012 * vm_page_set_validclean(). 3013 */ 3014 for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) { 3015 if (i == (PAGE_SIZE / DEV_BSIZE) || 3016 (m->valid & ((vm_page_bits_t)1 << i))) { 3017 if (i > b) { 3018 pmap_zero_page_area(m, 3019 b << DEV_BSHIFT, (i - b) << DEV_BSHIFT); 3020 } 3021 b = i + 1; 3022 } 3023 } 3024 3025 /* 3026 * setvalid is TRUE when we can safely set the zero'd areas 3027 * as being valid. We can do this if there are no cache consistancy 3028 * issues. e.g. it is ok to do with UFS, but not ok to do with NFS. 3029 */ 3030 if (setvalid) 3031 m->valid = VM_PAGE_BITS_ALL; 3032 } 3033 3034 /* 3035 * vm_page_is_valid: 3036 * 3037 * Is (partial) page valid? Note that the case where size == 0 3038 * will return FALSE in the degenerate case where the page is 3039 * entirely invalid, and TRUE otherwise. 3040 */ 3041 int 3042 vm_page_is_valid(vm_page_t m, int base, int size) 3043 { 3044 vm_page_bits_t bits; 3045 3046 VM_OBJECT_ASSERT_LOCKED(m->object); 3047 bits = vm_page_bits(base, size); 3048 return (m->valid != 0 && (m->valid & bits) == bits); 3049 } 3050 3051 /* 3052 * Set the page's dirty bits if the page is modified. 3053 */ 3054 void 3055 vm_page_test_dirty(vm_page_t m) 3056 { 3057 3058 VM_OBJECT_ASSERT_WLOCKED(m->object); 3059 if (m->dirty != VM_PAGE_BITS_ALL && pmap_is_modified(m)) 3060 vm_page_dirty(m); 3061 } 3062 3063 void 3064 vm_page_lock_KBI(vm_page_t m, const char *file, int line) 3065 { 3066 3067 mtx_lock_flags_(vm_page_lockptr(m), 0, file, line); 3068 } 3069 3070 void 3071 vm_page_unlock_KBI(vm_page_t m, const char *file, int line) 3072 { 3073 3074 mtx_unlock_flags_(vm_page_lockptr(m), 0, file, line); 3075 } 3076 3077 int 3078 vm_page_trylock_KBI(vm_page_t m, const char *file, int line) 3079 { 3080 3081 return (mtx_trylock_flags_(vm_page_lockptr(m), 0, file, line)); 3082 } 3083 3084 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT) 3085 void 3086 vm_page_assert_locked_KBI(vm_page_t m, const char *file, int line) 3087 { 3088 3089 vm_page_lock_assert_KBI(m, MA_OWNED, file, line); 3090 } 3091 3092 void 3093 vm_page_lock_assert_KBI(vm_page_t m, int a, const char *file, int line) 3094 { 3095 3096 mtx_assert_(vm_page_lockptr(m), a, file, line); 3097 } 3098 #endif 3099 3100 int so_zerocp_fullpage = 0; 3101 3102 /* 3103 * Replace the given page with a copy. The copied page assumes 3104 * the portion of the given page's "wire_count" that is not the 3105 * responsibility of this copy-on-write mechanism. 3106 * 3107 * The object containing the given page must have a non-zero 3108 * paging-in-progress count and be locked. 3109 */ 3110 void 3111 vm_page_cowfault(vm_page_t m) 3112 { 3113 vm_page_t mnew; 3114 vm_object_t object; 3115 vm_pindex_t pindex; 3116 3117 vm_page_lock_assert(m, MA_OWNED); 3118 object = m->object; 3119 VM_OBJECT_ASSERT_WLOCKED(object); 3120 KASSERT(object->paging_in_progress != 0, 3121 ("vm_page_cowfault: object %p's paging-in-progress count is zero.", 3122 object)); 3123 pindex = m->pindex; 3124 3125 retry_alloc: 3126 mnew = vm_page_alloc(NULL, pindex, VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ); 3127 if (mnew == NULL) { 3128 vm_page_unlock(m); 3129 VM_OBJECT_WUNLOCK(object); 3130 VM_WAIT; 3131 VM_OBJECT_WLOCK(object); 3132 if (m == vm_page_lookup(object, pindex)) { 3133 vm_page_lock(m); 3134 goto retry_alloc; 3135 } else { 3136 /* 3137 * Page disappeared during the wait. 3138 */ 3139 return; 3140 } 3141 } 3142 3143 if (m->cow == 0) { 3144 /* 3145 * check to see if we raced with an xmit complete when 3146 * waiting to allocate a page. If so, put things back 3147 * the way they were 3148 */ 3149 vm_page_unlock(m); 3150 vm_page_lock(mnew); 3151 vm_page_free(mnew); 3152 vm_page_unlock(mnew); 3153 } else { /* clear COW & copy page */ 3154 pmap_remove_all(m); 3155 mnew->object = object; 3156 if (object->memattr != VM_MEMATTR_DEFAULT && 3157 (object->flags & OBJ_FICTITIOUS) == 0) 3158 pmap_page_set_memattr(mnew, object->memattr); 3159 if (vm_page_replace(mnew, object, pindex) != m) 3160 panic("vm_page_cowfault: invalid page replacement"); 3161 if (!so_zerocp_fullpage) 3162 pmap_copy_page(m, mnew); 3163 mnew->valid = VM_PAGE_BITS_ALL; 3164 vm_page_dirty(mnew); 3165 mnew->wire_count = m->wire_count - m->cow; 3166 m->wire_count = m->cow; 3167 vm_page_unlock(m); 3168 } 3169 } 3170 3171 void 3172 vm_page_cowclear(vm_page_t m) 3173 { 3174 3175 vm_page_lock_assert(m, MA_OWNED); 3176 if (m->cow) { 3177 m->cow--; 3178 /* 3179 * let vm_fault add back write permission lazily 3180 */ 3181 } 3182 /* 3183 * sf_buf_free() will free the page, so we needn't do it here 3184 */ 3185 } 3186 3187 int 3188 vm_page_cowsetup(vm_page_t m) 3189 { 3190 3191 vm_page_lock_assert(m, MA_OWNED); 3192 if ((m->flags & PG_FICTITIOUS) != 0 || 3193 (m->oflags & VPO_UNMANAGED) != 0 || 3194 m->cow == USHRT_MAX - 1 || !VM_OBJECT_TRYWLOCK(m->object)) 3195 return (EBUSY); 3196 m->cow++; 3197 pmap_remove_write(m); 3198 VM_OBJECT_WUNLOCK(m->object); 3199 return (0); 3200 } 3201 3202 #ifdef INVARIANTS 3203 void 3204 vm_page_object_lock_assert(vm_page_t m) 3205 { 3206 3207 /* 3208 * Certain of the page's fields may only be modified by the 3209 * holder of the containing object's lock or the exclusive busy. 3210 * holder. Unfortunately, the holder of the write busy is 3211 * not recorded, and thus cannot be checked here. 3212 */ 3213 if (m->object != NULL && !vm_page_xbusied(m)) 3214 VM_OBJECT_ASSERT_WLOCKED(m->object); 3215 } 3216 #endif 3217 3218 #include "opt_ddb.h" 3219 #ifdef DDB 3220 #include <sys/kernel.h> 3221 3222 #include <ddb/ddb.h> 3223 3224 DB_SHOW_COMMAND(page, vm_page_print_page_info) 3225 { 3226 db_printf("cnt.v_free_count: %d\n", cnt.v_free_count); 3227 db_printf("cnt.v_cache_count: %d\n", cnt.v_cache_count); 3228 db_printf("cnt.v_inactive_count: %d\n", cnt.v_inactive_count); 3229 db_printf("cnt.v_active_count: %d\n", cnt.v_active_count); 3230 db_printf("cnt.v_wire_count: %d\n", cnt.v_wire_count); 3231 db_printf("cnt.v_free_reserved: %d\n", cnt.v_free_reserved); 3232 db_printf("cnt.v_free_min: %d\n", cnt.v_free_min); 3233 db_printf("cnt.v_free_target: %d\n", cnt.v_free_target); 3234 db_printf("cnt.v_cache_min: %d\n", cnt.v_cache_min); 3235 db_printf("cnt.v_inactive_target: %d\n", cnt.v_inactive_target); 3236 } 3237 3238 DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info) 3239 { 3240 int dom; 3241 3242 db_printf("pq_free %d pq_cache %d\n", 3243 cnt.v_free_count, cnt.v_cache_count); 3244 for (dom = 0; dom < vm_ndomains; dom++) { 3245 db_printf( 3246 "dom %d page_cnt %d free %d pq_act %d pq_inact %d pass %d\n", 3247 dom, 3248 vm_dom[dom].vmd_page_count, 3249 vm_dom[dom].vmd_free_count, 3250 vm_dom[dom].vmd_pagequeues[PQ_ACTIVE].pq_cnt, 3251 vm_dom[dom].vmd_pagequeues[PQ_INACTIVE].pq_cnt, 3252 vm_dom[dom].vmd_pass); 3253 } 3254 } 3255 3256 DB_SHOW_COMMAND(pginfo, vm_page_print_pginfo) 3257 { 3258 vm_page_t m; 3259 boolean_t phys; 3260 3261 if (!have_addr) { 3262 db_printf("show pginfo addr\n"); 3263 return; 3264 } 3265 3266 phys = strchr(modif, 'p') != NULL; 3267 if (phys) 3268 m = PHYS_TO_VM_PAGE(addr); 3269 else 3270 m = (vm_page_t)addr; 3271 db_printf( 3272 "page %p obj %p pidx 0x%jx phys 0x%jx q %d hold %d wire %d\n" 3273 " af 0x%x of 0x%x f 0x%x act %d busy %x valid 0x%x dirty 0x%x\n", 3274 m, m->object, (uintmax_t)m->pindex, (uintmax_t)m->phys_addr, 3275 m->queue, m->hold_count, m->wire_count, m->aflags, m->oflags, 3276 m->flags, m->act_count, m->busy_lock, m->valid, m->dirty); 3277 } 3278 #endif /* DDB */ 3279