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