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