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