vm_page.c revision 217171
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 217171 2011-01-08 22:45:22Z 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] __aligned(CACHE_LINE_SIZE); 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 page_req = req & VM_ALLOC_CLASS_MASK; 1218 KASSERT(curthread->td_intr_nesting_level == 0 || 1219 page_req == VM_ALLOC_INTERRUPT, 1220 ("vm_page_alloc(NORMAL|SYSTEM) in interrupt context")); 1221 1222 if ((req & VM_ALLOC_NOOBJ) == 0) { 1223 KASSERT(object != NULL, 1224 ("vm_page_alloc: NULL object.")); 1225 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1226 } 1227 1228 /* 1229 * The pager is allowed to eat deeper into the free page list. 1230 */ 1231 if ((curproc == pageproc) && (page_req != VM_ALLOC_INTERRUPT)) { 1232 page_req = VM_ALLOC_SYSTEM; 1233 }; 1234 1235 mtx_lock(&vm_page_queue_free_mtx); 1236 if (cnt.v_free_count + cnt.v_cache_count > cnt.v_free_reserved || 1237 (page_req == VM_ALLOC_SYSTEM && 1238 cnt.v_free_count + cnt.v_cache_count > cnt.v_interrupt_free_min) || 1239 (page_req == VM_ALLOC_INTERRUPT && 1240 cnt.v_free_count + cnt.v_cache_count > 0)) { 1241 /* 1242 * Allocate from the free queue if the number of free pages 1243 * exceeds the minimum for the request class. 1244 */ 1245 if (object != NULL && 1246 (m = vm_page_cache_lookup(object, pindex)) != NULL) { 1247 if ((req & VM_ALLOC_IFNOTCACHED) != 0) { 1248 mtx_unlock(&vm_page_queue_free_mtx); 1249 return (NULL); 1250 } 1251 if (vm_phys_unfree_page(m)) 1252 vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m, 0); 1253#if VM_NRESERVLEVEL > 0 1254 else if (!vm_reserv_reactivate_page(m)) 1255#else 1256 else 1257#endif 1258 panic("vm_page_alloc: cache page %p is missing" 1259 " from the free queue", m); 1260 } else if ((req & VM_ALLOC_IFCACHED) != 0) { 1261 mtx_unlock(&vm_page_queue_free_mtx); 1262 return (NULL); 1263#if VM_NRESERVLEVEL > 0 1264 } else if (object == NULL || object->type == OBJT_DEVICE || 1265 object->type == OBJT_SG || 1266 (object->flags & OBJ_COLORED) == 0 || 1267 (m = vm_reserv_alloc_page(object, pindex)) == NULL) { 1268#else 1269 } else { 1270#endif 1271 m = vm_phys_alloc_pages(object != NULL ? 1272 VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT, 0); 1273#if VM_NRESERVLEVEL > 0 1274 if (m == NULL && vm_reserv_reclaim_inactive()) { 1275 m = vm_phys_alloc_pages(object != NULL ? 1276 VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT, 1277 0); 1278 } 1279#endif 1280 } 1281 } else { 1282 /* 1283 * Not allocatable, give up. 1284 */ 1285 mtx_unlock(&vm_page_queue_free_mtx); 1286 atomic_add_int(&vm_pageout_deficit, 1287 MAX((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1)); 1288 pagedaemon_wakeup(); 1289 return (NULL); 1290 } 1291 1292 /* 1293 * At this point we had better have found a good page. 1294 */ 1295 1296 KASSERT(m != NULL, ("vm_page_alloc: missing page")); 1297 KASSERT(m->queue == PQ_NONE, 1298 ("vm_page_alloc: page %p has unexpected queue %d", m, m->queue)); 1299 KASSERT(m->wire_count == 0, ("vm_page_alloc: page %p is wired", m)); 1300 KASSERT(m->hold_count == 0, ("vm_page_alloc: page %p is held", m)); 1301 KASSERT(m->busy == 0, ("vm_page_alloc: page %p is busy", m)); 1302 KASSERT(m->dirty == 0, ("vm_page_alloc: page %p is dirty", m)); 1303 KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT, 1304 ("vm_page_alloc: page %p has unexpected memattr %d", m, 1305 pmap_page_get_memattr(m))); 1306 if ((m->flags & PG_CACHED) != 0) { 1307 KASSERT(m->valid != 0, 1308 ("vm_page_alloc: cached page %p is invalid", m)); 1309 if (m->object == object && m->pindex == pindex) 1310 cnt.v_reactivated++; 1311 else 1312 m->valid = 0; 1313 m_object = m->object; 1314 vm_page_cache_remove(m); 1315 if (m_object->type == OBJT_VNODE && m_object->cache == NULL) 1316 vp = m_object->handle; 1317 } else { 1318 KASSERT(VM_PAGE_IS_FREE(m), 1319 ("vm_page_alloc: page %p is not free", m)); 1320 KASSERT(m->valid == 0, 1321 ("vm_page_alloc: free page %p is valid", m)); 1322 cnt.v_free_count--; 1323 } 1324 1325 /* 1326 * Initialize structure. Only the PG_ZERO flag is inherited. 1327 */ 1328 flags = 0; 1329 if (m->flags & PG_ZERO) { 1330 vm_page_zero_count--; 1331 if (req & VM_ALLOC_ZERO) 1332 flags = PG_ZERO; 1333 } 1334 if (object == NULL || object->type == OBJT_PHYS) 1335 flags |= PG_UNMANAGED; 1336 m->flags = flags; 1337 if (req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ)) 1338 m->oflags = 0; 1339 else 1340 m->oflags = VPO_BUSY; 1341 if (req & VM_ALLOC_WIRED) { 1342 atomic_add_int(&cnt.v_wire_count, 1); 1343 m->wire_count = 1; 1344 } 1345 m->act_count = 0; 1346 mtx_unlock(&vm_page_queue_free_mtx); 1347 1348 if (object != NULL) { 1349 /* Ignore device objects; the pager sets "memattr" for them. */ 1350 if (object->memattr != VM_MEMATTR_DEFAULT && 1351 object->type != OBJT_DEVICE && object->type != OBJT_SG) 1352 pmap_page_set_memattr(m, object->memattr); 1353 vm_page_insert(m, object, pindex); 1354 } else 1355 m->pindex = pindex; 1356 1357 /* 1358 * The following call to vdrop() must come after the above call 1359 * to vm_page_insert() in case both affect the same object and 1360 * vnode. Otherwise, the affected vnode's hold count could 1361 * temporarily become zero. 1362 */ 1363 if (vp != NULL) 1364 vdrop(vp); 1365 1366 /* 1367 * Don't wakeup too often - wakeup the pageout daemon when 1368 * we would be nearly out of memory. 1369 */ 1370 if (vm_paging_needed()) 1371 pagedaemon_wakeup(); 1372 1373 return (m); 1374} 1375 1376/* 1377 * Initialize a page that has been freshly dequeued from a freelist. 1378 * The caller has to drop the vnode returned, if it is not NULL. 1379 * 1380 * To be called with vm_page_queue_free_mtx held. 1381 */ 1382struct vnode * 1383vm_page_alloc_init(vm_page_t m) 1384{ 1385 struct vnode *drop; 1386 vm_object_t m_object; 1387 1388 KASSERT(m->queue == PQ_NONE, 1389 ("vm_page_alloc_init: page %p has unexpected queue %d", 1390 m, m->queue)); 1391 KASSERT(m->wire_count == 0, 1392 ("vm_page_alloc_init: page %p is wired", m)); 1393 KASSERT(m->hold_count == 0, 1394 ("vm_page_alloc_init: page %p is held", m)); 1395 KASSERT(m->busy == 0, 1396 ("vm_page_alloc_init: page %p is busy", m)); 1397 KASSERT(m->dirty == 0, 1398 ("vm_page_alloc_init: page %p is dirty", m)); 1399 KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT, 1400 ("vm_page_alloc_init: page %p has unexpected memattr %d", 1401 m, pmap_page_get_memattr(m))); 1402 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 1403 drop = NULL; 1404 if ((m->flags & PG_CACHED) != 0) { 1405 m->valid = 0; 1406 m_object = m->object; 1407 vm_page_cache_remove(m); 1408 if (m_object->type == OBJT_VNODE && 1409 m_object->cache == NULL) 1410 drop = m_object->handle; 1411 } else { 1412 KASSERT(VM_PAGE_IS_FREE(m), 1413 ("vm_page_alloc_init: page %p is not free", m)); 1414 KASSERT(m->valid == 0, 1415 ("vm_page_alloc_init: free page %p is valid", m)); 1416 cnt.v_free_count--; 1417 } 1418 if (m->flags & PG_ZERO) 1419 vm_page_zero_count--; 1420 /* Don't clear the PG_ZERO flag; we'll need it later. */ 1421 m->flags = PG_UNMANAGED | (m->flags & PG_ZERO); 1422 m->oflags = 0; 1423 /* Unmanaged pages don't use "act_count". */ 1424 return (drop); 1425} 1426 1427/* 1428 * vm_page_alloc_freelist: 1429 * 1430 * Allocate a page from the specified freelist. 1431 * Only the ALLOC_CLASS values in req are honored, other request flags 1432 * are ignored. 1433 */ 1434vm_page_t 1435vm_page_alloc_freelist(int flind, int req) 1436{ 1437 struct vnode *drop; 1438 vm_page_t m; 1439 int page_req; 1440 1441 m = NULL; 1442 page_req = req & VM_ALLOC_CLASS_MASK; 1443 mtx_lock(&vm_page_queue_free_mtx); 1444 /* 1445 * Do not allocate reserved pages unless the req has asked for it. 1446 */ 1447 if (cnt.v_free_count + cnt.v_cache_count > cnt.v_free_reserved || 1448 (page_req == VM_ALLOC_SYSTEM && 1449 cnt.v_free_count + cnt.v_cache_count > cnt.v_interrupt_free_min) || 1450 (page_req == VM_ALLOC_INTERRUPT && 1451 cnt.v_free_count + cnt.v_cache_count > 0)) { 1452 m = vm_phys_alloc_freelist_pages(flind, VM_FREEPOOL_DIRECT, 0); 1453 } 1454 if (m == NULL) { 1455 mtx_unlock(&vm_page_queue_free_mtx); 1456 return (NULL); 1457 } 1458 drop = vm_page_alloc_init(m); 1459 mtx_unlock(&vm_page_queue_free_mtx); 1460 if (drop) 1461 vdrop(drop); 1462 return (m); 1463} 1464 1465/* 1466 * vm_wait: (also see VM_WAIT macro) 1467 * 1468 * Block until free pages are available for allocation 1469 * - Called in various places before memory allocations. 1470 */ 1471void 1472vm_wait(void) 1473{ 1474 1475 mtx_lock(&vm_page_queue_free_mtx); 1476 if (curproc == pageproc) { 1477 vm_pageout_pages_needed = 1; 1478 msleep(&vm_pageout_pages_needed, &vm_page_queue_free_mtx, 1479 PDROP | PSWP, "VMWait", 0); 1480 } else { 1481 if (!vm_pages_needed) { 1482 vm_pages_needed = 1; 1483 wakeup(&vm_pages_needed); 1484 } 1485 msleep(&cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PVM, 1486 "vmwait", 0); 1487 } 1488} 1489 1490/* 1491 * vm_waitpfault: (also see VM_WAITPFAULT macro) 1492 * 1493 * Block until free pages are available for allocation 1494 * - Called only in vm_fault so that processes page faulting 1495 * can be easily tracked. 1496 * - Sleeps at a lower priority than vm_wait() so that vm_wait()ing 1497 * processes will be able to grab memory first. Do not change 1498 * this balance without careful testing first. 1499 */ 1500void 1501vm_waitpfault(void) 1502{ 1503 1504 mtx_lock(&vm_page_queue_free_mtx); 1505 if (!vm_pages_needed) { 1506 vm_pages_needed = 1; 1507 wakeup(&vm_pages_needed); 1508 } 1509 msleep(&cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PUSER, 1510 "pfault", 0); 1511} 1512 1513/* 1514 * vm_page_requeue: 1515 * 1516 * Move the given page to the tail of its present page queue. 1517 * 1518 * The page queues must be locked. 1519 */ 1520void 1521vm_page_requeue(vm_page_t m) 1522{ 1523 struct vpgqueues *vpq; 1524 int queue; 1525 1526 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1527 queue = m->queue; 1528 KASSERT(queue != PQ_NONE, 1529 ("vm_page_requeue: page %p is not queued", m)); 1530 vpq = &vm_page_queues[queue]; 1531 TAILQ_REMOVE(&vpq->pl, m, pageq); 1532 TAILQ_INSERT_TAIL(&vpq->pl, m, pageq); 1533} 1534 1535/* 1536 * vm_page_queue_remove: 1537 * 1538 * Remove the given page from the specified queue. 1539 * 1540 * The page and page queues must be locked. 1541 */ 1542static __inline void 1543vm_page_queue_remove(int queue, vm_page_t m) 1544{ 1545 struct vpgqueues *pq; 1546 1547 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1548 vm_page_lock_assert(m, MA_OWNED); 1549 pq = &vm_page_queues[queue]; 1550 TAILQ_REMOVE(&pq->pl, m, pageq); 1551 (*pq->cnt)--; 1552} 1553 1554/* 1555 * vm_pageq_remove: 1556 * 1557 * Remove a page from its queue. 1558 * 1559 * The given page must be locked. 1560 * This routine may not block. 1561 */ 1562void 1563vm_pageq_remove(vm_page_t m) 1564{ 1565 int queue; 1566 1567 vm_page_lock_assert(m, MA_OWNED); 1568 if ((queue = m->queue) != PQ_NONE) { 1569 vm_page_lock_queues(); 1570 m->queue = PQ_NONE; 1571 vm_page_queue_remove(queue, m); 1572 vm_page_unlock_queues(); 1573 } 1574} 1575 1576/* 1577 * vm_page_enqueue: 1578 * 1579 * Add the given page to the specified queue. 1580 * 1581 * The page queues must be locked. 1582 */ 1583static void 1584vm_page_enqueue(int queue, vm_page_t m) 1585{ 1586 struct vpgqueues *vpq; 1587 1588 vpq = &vm_page_queues[queue]; 1589 m->queue = queue; 1590 TAILQ_INSERT_TAIL(&vpq->pl, m, pageq); 1591 ++*vpq->cnt; 1592} 1593 1594/* 1595 * vm_page_activate: 1596 * 1597 * Put the specified page on the active list (if appropriate). 1598 * Ensure that act_count is at least ACT_INIT but do not otherwise 1599 * mess with it. 1600 * 1601 * The page must be locked. 1602 * This routine may not block. 1603 */ 1604void 1605vm_page_activate(vm_page_t m) 1606{ 1607 int queue; 1608 1609 vm_page_lock_assert(m, MA_OWNED); 1610 if ((queue = m->queue) != PQ_ACTIVE) { 1611 if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) { 1612 if (m->act_count < ACT_INIT) 1613 m->act_count = ACT_INIT; 1614 vm_page_lock_queues(); 1615 if (queue != PQ_NONE) 1616 vm_page_queue_remove(queue, m); 1617 vm_page_enqueue(PQ_ACTIVE, m); 1618 vm_page_unlock_queues(); 1619 } else 1620 KASSERT(queue == PQ_NONE, 1621 ("vm_page_activate: wired page %p is queued", m)); 1622 } else { 1623 if (m->act_count < ACT_INIT) 1624 m->act_count = ACT_INIT; 1625 } 1626} 1627 1628/* 1629 * vm_page_free_wakeup: 1630 * 1631 * Helper routine for vm_page_free_toq() and vm_page_cache(). This 1632 * routine is called when a page has been added to the cache or free 1633 * queues. 1634 * 1635 * The page queues must be locked. 1636 * This routine may not block. 1637 */ 1638static inline void 1639vm_page_free_wakeup(void) 1640{ 1641 1642 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 1643 /* 1644 * if pageout daemon needs pages, then tell it that there are 1645 * some free. 1646 */ 1647 if (vm_pageout_pages_needed && 1648 cnt.v_cache_count + cnt.v_free_count >= cnt.v_pageout_free_min) { 1649 wakeup(&vm_pageout_pages_needed); 1650 vm_pageout_pages_needed = 0; 1651 } 1652 /* 1653 * wakeup processes that are waiting on memory if we hit a 1654 * high water mark. And wakeup scheduler process if we have 1655 * lots of memory. this process will swapin processes. 1656 */ 1657 if (vm_pages_needed && !vm_page_count_min()) { 1658 vm_pages_needed = 0; 1659 wakeup(&cnt.v_free_count); 1660 } 1661} 1662 1663/* 1664 * vm_page_free_toq: 1665 * 1666 * Returns the given page to the free list, 1667 * disassociating it with any VM object. 1668 * 1669 * Object and page must be locked prior to entry. 1670 * This routine may not block. 1671 */ 1672 1673void 1674vm_page_free_toq(vm_page_t m) 1675{ 1676 1677 if ((m->flags & PG_UNMANAGED) == 0) { 1678 vm_page_lock_assert(m, MA_OWNED); 1679 KASSERT(!pmap_page_is_mapped(m), 1680 ("vm_page_free_toq: freeing mapped page %p", m)); 1681 } 1682 PCPU_INC(cnt.v_tfree); 1683 1684 if (VM_PAGE_IS_FREE(m)) 1685 panic("vm_page_free: freeing free page %p", m); 1686 else if (m->busy != 0) 1687 panic("vm_page_free: freeing busy page %p", m); 1688 1689 /* 1690 * unqueue, then remove page. Note that we cannot destroy 1691 * the page here because we do not want to call the pager's 1692 * callback routine until after we've put the page on the 1693 * appropriate free queue. 1694 */ 1695 if ((m->flags & PG_UNMANAGED) == 0) 1696 vm_pageq_remove(m); 1697 vm_page_remove(m); 1698 1699 /* 1700 * If fictitious remove object association and 1701 * return, otherwise delay object association removal. 1702 */ 1703 if ((m->flags & PG_FICTITIOUS) != 0) { 1704 return; 1705 } 1706 1707 m->valid = 0; 1708 vm_page_undirty(m); 1709 1710 if (m->wire_count != 0) 1711 panic("vm_page_free: freeing wired page %p", m); 1712 if (m->hold_count != 0) { 1713 m->flags &= ~PG_ZERO; 1714 vm_page_lock_queues(); 1715 vm_page_enqueue(PQ_HOLD, m); 1716 vm_page_unlock_queues(); 1717 } else { 1718 /* 1719 * Restore the default memory attribute to the page. 1720 */ 1721 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT) 1722 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT); 1723 1724 /* 1725 * Insert the page into the physical memory allocator's 1726 * cache/free page queues. 1727 */ 1728 mtx_lock(&vm_page_queue_free_mtx); 1729 m->flags |= PG_FREE; 1730 cnt.v_free_count++; 1731#if VM_NRESERVLEVEL > 0 1732 if (!vm_reserv_free_page(m)) 1733#else 1734 if (TRUE) 1735#endif 1736 vm_phys_free_pages(m, 0); 1737 if ((m->flags & PG_ZERO) != 0) 1738 ++vm_page_zero_count; 1739 else 1740 vm_page_zero_idle_wakeup(); 1741 vm_page_free_wakeup(); 1742 mtx_unlock(&vm_page_queue_free_mtx); 1743 } 1744} 1745 1746/* 1747 * vm_page_wire: 1748 * 1749 * Mark this page as wired down by yet 1750 * another map, removing it from paging queues 1751 * as necessary. 1752 * 1753 * If the page is fictitious, then its wire count must remain one. 1754 * 1755 * The page must be locked. 1756 * This routine may not block. 1757 */ 1758void 1759vm_page_wire(vm_page_t m) 1760{ 1761 1762 /* 1763 * Only bump the wire statistics if the page is not already wired, 1764 * and only unqueue the page if it is on some queue (if it is unmanaged 1765 * it is already off the queues). 1766 */ 1767 vm_page_lock_assert(m, MA_OWNED); 1768 if ((m->flags & PG_FICTITIOUS) != 0) { 1769 KASSERT(m->wire_count == 1, 1770 ("vm_page_wire: fictitious page %p's wire count isn't one", 1771 m)); 1772 return; 1773 } 1774 if (m->wire_count == 0) { 1775 if ((m->flags & PG_UNMANAGED) == 0) 1776 vm_pageq_remove(m); 1777 atomic_add_int(&cnt.v_wire_count, 1); 1778 } 1779 m->wire_count++; 1780 KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m)); 1781} 1782 1783/* 1784 * vm_page_unwire: 1785 * 1786 * Release one wiring of the specified page, potentially enabling it to be 1787 * paged again. If paging is enabled, then the value of the parameter 1788 * "activate" determines to which queue the page is added. If "activate" is 1789 * non-zero, then the page is added to the active queue. Otherwise, it is 1790 * added to the inactive queue. 1791 * 1792 * However, unless the page belongs to an object, it is not enqueued because 1793 * it cannot be paged out. 1794 * 1795 * If a page is fictitious, then its wire count must alway be one. 1796 * 1797 * A managed page must be locked. 1798 */ 1799void 1800vm_page_unwire(vm_page_t m, int activate) 1801{ 1802 1803 if ((m->flags & PG_UNMANAGED) == 0) 1804 vm_page_lock_assert(m, MA_OWNED); 1805 if ((m->flags & PG_FICTITIOUS) != 0) { 1806 KASSERT(m->wire_count == 1, 1807 ("vm_page_unwire: fictitious page %p's wire count isn't one", m)); 1808 return; 1809 } 1810 if (m->wire_count > 0) { 1811 m->wire_count--; 1812 if (m->wire_count == 0) { 1813 atomic_subtract_int(&cnt.v_wire_count, 1); 1814 if ((m->flags & PG_UNMANAGED) != 0 || 1815 m->object == NULL) 1816 return; 1817 vm_page_lock_queues(); 1818 if (activate) 1819 vm_page_enqueue(PQ_ACTIVE, m); 1820 else { 1821 vm_page_flag_clear(m, PG_WINATCFLS); 1822 vm_page_enqueue(PQ_INACTIVE, m); 1823 } 1824 vm_page_unlock_queues(); 1825 } 1826 } else 1827 panic("vm_page_unwire: page %p's wire count is zero", m); 1828} 1829 1830/* 1831 * Move the specified page to the inactive queue. 1832 * 1833 * Many pages placed on the inactive queue should actually go 1834 * into the cache, but it is difficult to figure out which. What 1835 * we do instead, if the inactive target is well met, is to put 1836 * clean pages at the head of the inactive queue instead of the tail. 1837 * This will cause them to be moved to the cache more quickly and 1838 * if not actively re-referenced, reclaimed more quickly. If we just 1839 * stick these pages at the end of the inactive queue, heavy filesystem 1840 * meta-data accesses can cause an unnecessary paging load on memory bound 1841 * processes. This optimization causes one-time-use metadata to be 1842 * reused more quickly. 1843 * 1844 * Normally athead is 0 resulting in LRU operation. athead is set 1845 * to 1 if we want this page to be 'as if it were placed in the cache', 1846 * except without unmapping it from the process address space. 1847 * 1848 * This routine may not block. 1849 */ 1850static inline void 1851_vm_page_deactivate(vm_page_t m, int athead) 1852{ 1853 int queue; 1854 1855 vm_page_lock_assert(m, MA_OWNED); 1856 1857 /* 1858 * Ignore if already inactive. 1859 */ 1860 if ((queue = m->queue) == PQ_INACTIVE) 1861 return; 1862 if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) { 1863 vm_page_lock_queues(); 1864 vm_page_flag_clear(m, PG_WINATCFLS); 1865 if (queue != PQ_NONE) 1866 vm_page_queue_remove(queue, m); 1867 if (athead) 1868 TAILQ_INSERT_HEAD(&vm_page_queues[PQ_INACTIVE].pl, m, 1869 pageq); 1870 else 1871 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, 1872 pageq); 1873 m->queue = PQ_INACTIVE; 1874 cnt.v_inactive_count++; 1875 vm_page_unlock_queues(); 1876 } 1877} 1878 1879/* 1880 * Move the specified page to the inactive queue. 1881 * 1882 * The page must be locked. 1883 */ 1884void 1885vm_page_deactivate(vm_page_t m) 1886{ 1887 1888 _vm_page_deactivate(m, 0); 1889} 1890 1891/* 1892 * vm_page_try_to_cache: 1893 * 1894 * Returns 0 on failure, 1 on success 1895 */ 1896int 1897vm_page_try_to_cache(vm_page_t m) 1898{ 1899 1900 vm_page_lock_assert(m, MA_OWNED); 1901 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1902 if (m->dirty || m->hold_count || m->busy || m->wire_count || 1903 (m->oflags & VPO_BUSY) || (m->flags & PG_UNMANAGED)) 1904 return (0); 1905 pmap_remove_all(m); 1906 if (m->dirty) 1907 return (0); 1908 vm_page_cache(m); 1909 return (1); 1910} 1911 1912/* 1913 * vm_page_try_to_free() 1914 * 1915 * Attempt to free the page. If we cannot free it, we do nothing. 1916 * 1 is returned on success, 0 on failure. 1917 */ 1918int 1919vm_page_try_to_free(vm_page_t m) 1920{ 1921 1922 vm_page_lock_assert(m, MA_OWNED); 1923 if (m->object != NULL) 1924 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1925 if (m->dirty || m->hold_count || m->busy || m->wire_count || 1926 (m->oflags & VPO_BUSY) || (m->flags & PG_UNMANAGED)) 1927 return (0); 1928 pmap_remove_all(m); 1929 if (m->dirty) 1930 return (0); 1931 vm_page_free(m); 1932 return (1); 1933} 1934 1935/* 1936 * vm_page_cache 1937 * 1938 * Put the specified page onto the page cache queue (if appropriate). 1939 * 1940 * This routine may not block. 1941 */ 1942void 1943vm_page_cache(vm_page_t m) 1944{ 1945 vm_object_t object; 1946 vm_page_t root; 1947 1948 vm_page_lock_assert(m, MA_OWNED); 1949 object = m->object; 1950 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1951 if ((m->flags & PG_UNMANAGED) || (m->oflags & VPO_BUSY) || m->busy || 1952 m->hold_count || m->wire_count) 1953 panic("vm_page_cache: attempting to cache busy page"); 1954 pmap_remove_all(m); 1955 if (m->dirty != 0) 1956 panic("vm_page_cache: page %p is dirty", m); 1957 if (m->valid == 0 || object->type == OBJT_DEFAULT || 1958 (object->type == OBJT_SWAP && 1959 !vm_pager_has_page(object, m->pindex, NULL, NULL))) { 1960 /* 1961 * Hypothesis: A cache-elgible page belonging to a 1962 * default object or swap object but without a backing 1963 * store must be zero filled. 1964 */ 1965 vm_page_free(m); 1966 return; 1967 } 1968 KASSERT((m->flags & PG_CACHED) == 0, 1969 ("vm_page_cache: page %p is already cached", m)); 1970 PCPU_INC(cnt.v_tcached); 1971 1972 /* 1973 * Remove the page from the paging queues. 1974 */ 1975 vm_pageq_remove(m); 1976 1977 /* 1978 * Remove the page from the object's collection of resident 1979 * pages. 1980 */ 1981 if (m != object->root) 1982 vm_page_splay(m->pindex, object->root); 1983 if (m->left == NULL) 1984 root = m->right; 1985 else { 1986 root = vm_page_splay(m->pindex, m->left); 1987 root->right = m->right; 1988 } 1989 object->root = root; 1990 TAILQ_REMOVE(&object->memq, m, listq); 1991 object->resident_page_count--; 1992 1993 /* 1994 * Restore the default memory attribute to the page. 1995 */ 1996 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT) 1997 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT); 1998 1999 /* 2000 * Insert the page into the object's collection of cached pages 2001 * and the physical memory allocator's cache/free page queues. 2002 */ 2003 m->flags &= ~PG_ZERO; 2004 mtx_lock(&vm_page_queue_free_mtx); 2005 m->flags |= PG_CACHED; 2006 cnt.v_cache_count++; 2007 root = object->cache; 2008 if (root == NULL) { 2009 m->left = NULL; 2010 m->right = NULL; 2011 } else { 2012 root = vm_page_splay(m->pindex, root); 2013 if (m->pindex < root->pindex) { 2014 m->left = root->left; 2015 m->right = root; 2016 root->left = NULL; 2017 } else if (__predict_false(m->pindex == root->pindex)) 2018 panic("vm_page_cache: offset already cached"); 2019 else { 2020 m->right = root->right; 2021 m->left = root; 2022 root->right = NULL; 2023 } 2024 } 2025 object->cache = m; 2026#if VM_NRESERVLEVEL > 0 2027 if (!vm_reserv_free_page(m)) { 2028#else 2029 if (TRUE) { 2030#endif 2031 vm_phys_set_pool(VM_FREEPOOL_CACHE, m, 0); 2032 vm_phys_free_pages(m, 0); 2033 } 2034 vm_page_free_wakeup(); 2035 mtx_unlock(&vm_page_queue_free_mtx); 2036 2037 /* 2038 * Increment the vnode's hold count if this is the object's only 2039 * cached page. Decrement the vnode's hold count if this was 2040 * the object's only resident page. 2041 */ 2042 if (object->type == OBJT_VNODE) { 2043 if (root == NULL && object->resident_page_count != 0) 2044 vhold(object->handle); 2045 else if (root != NULL && object->resident_page_count == 0) 2046 vdrop(object->handle); 2047 } 2048} 2049 2050/* 2051 * vm_page_dontneed 2052 * 2053 * Cache, deactivate, or do nothing as appropriate. This routine 2054 * is typically used by madvise() MADV_DONTNEED. 2055 * 2056 * Generally speaking we want to move the page into the cache so 2057 * it gets reused quickly. However, this can result in a silly syndrome 2058 * due to the page recycling too quickly. Small objects will not be 2059 * fully cached. On the otherhand, if we move the page to the inactive 2060 * queue we wind up with a problem whereby very large objects 2061 * unnecessarily blow away our inactive and cache queues. 2062 * 2063 * The solution is to move the pages based on a fixed weighting. We 2064 * either leave them alone, deactivate them, or move them to the cache, 2065 * where moving them to the cache has the highest weighting. 2066 * By forcing some pages into other queues we eventually force the 2067 * system to balance the queues, potentially recovering other unrelated 2068 * space from active. The idea is to not force this to happen too 2069 * often. 2070 */ 2071void 2072vm_page_dontneed(vm_page_t m) 2073{ 2074 int dnw; 2075 int head; 2076 2077 vm_page_lock_assert(m, MA_OWNED); 2078 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 2079 dnw = PCPU_GET(dnweight); 2080 PCPU_INC(dnweight); 2081 2082 /* 2083 * Occasionally leave the page alone. 2084 */ 2085 if ((dnw & 0x01F0) == 0 || m->queue == PQ_INACTIVE) { 2086 if (m->act_count >= ACT_INIT) 2087 --m->act_count; 2088 return; 2089 } 2090 2091 /* 2092 * Clear any references to the page. Otherwise, the page daemon will 2093 * immediately reactivate the page. 2094 * 2095 * Perform the pmap_clear_reference() first. Otherwise, a concurrent 2096 * pmap operation, such as pmap_remove(), could clear a reference in 2097 * the pmap and set PG_REFERENCED on the page before the 2098 * pmap_clear_reference() had completed. Consequently, the page would 2099 * appear referenced based upon an old reference that occurred before 2100 * this function ran. 2101 */ 2102 pmap_clear_reference(m); 2103 vm_page_lock_queues(); 2104 vm_page_flag_clear(m, PG_REFERENCED); 2105 vm_page_unlock_queues(); 2106 2107 if (m->dirty == 0 && pmap_is_modified(m)) 2108 vm_page_dirty(m); 2109 2110 if (m->dirty || (dnw & 0x0070) == 0) { 2111 /* 2112 * Deactivate the page 3 times out of 32. 2113 */ 2114 head = 0; 2115 } else { 2116 /* 2117 * Cache the page 28 times out of every 32. Note that 2118 * the page is deactivated instead of cached, but placed 2119 * at the head of the queue instead of the tail. 2120 */ 2121 head = 1; 2122 } 2123 _vm_page_deactivate(m, head); 2124} 2125 2126/* 2127 * Grab a page, waiting until we are waken up due to the page 2128 * changing state. We keep on waiting, if the page continues 2129 * to be in the object. If the page doesn't exist, first allocate it 2130 * and then conditionally zero it. 2131 * 2132 * The caller must always specify the VM_ALLOC_RETRY flag. This is intended 2133 * to facilitate its eventual removal. 2134 * 2135 * This routine may block. 2136 */ 2137vm_page_t 2138vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags) 2139{ 2140 vm_page_t m; 2141 2142 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 2143 KASSERT((allocflags & VM_ALLOC_RETRY) != 0, 2144 ("vm_page_grab: VM_ALLOC_RETRY is required")); 2145retrylookup: 2146 if ((m = vm_page_lookup(object, pindex)) != NULL) { 2147 if ((m->oflags & VPO_BUSY) != 0 || 2148 ((allocflags & VM_ALLOC_IGN_SBUSY) == 0 && m->busy != 0)) { 2149 /* 2150 * Reference the page before unlocking and 2151 * sleeping so that the page daemon is less 2152 * likely to reclaim it. 2153 */ 2154 vm_page_lock_queues(); 2155 vm_page_flag_set(m, PG_REFERENCED); 2156 vm_page_sleep(m, "pgrbwt"); 2157 goto retrylookup; 2158 } else { 2159 if ((allocflags & VM_ALLOC_WIRED) != 0) { 2160 vm_page_lock(m); 2161 vm_page_wire(m); 2162 vm_page_unlock(m); 2163 } 2164 if ((allocflags & VM_ALLOC_NOBUSY) == 0) 2165 vm_page_busy(m); 2166 return (m); 2167 } 2168 } 2169 m = vm_page_alloc(object, pindex, allocflags & ~(VM_ALLOC_RETRY | 2170 VM_ALLOC_IGN_SBUSY)); 2171 if (m == NULL) { 2172 VM_OBJECT_UNLOCK(object); 2173 VM_WAIT; 2174 VM_OBJECT_LOCK(object); 2175 goto retrylookup; 2176 } else if (m->valid != 0) 2177 return (m); 2178 if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0) 2179 pmap_zero_page(m); 2180 return (m); 2181} 2182 2183/* 2184 * Mapping function for valid bits or for dirty bits in 2185 * a page. May not block. 2186 * 2187 * Inputs are required to range within a page. 2188 */ 2189int 2190vm_page_bits(int base, int size) 2191{ 2192 int first_bit; 2193 int last_bit; 2194 2195 KASSERT( 2196 base + size <= PAGE_SIZE, 2197 ("vm_page_bits: illegal base/size %d/%d", base, size) 2198 ); 2199 2200 if (size == 0) /* handle degenerate case */ 2201 return (0); 2202 2203 first_bit = base >> DEV_BSHIFT; 2204 last_bit = (base + size - 1) >> DEV_BSHIFT; 2205 2206 return ((2 << last_bit) - (1 << first_bit)); 2207} 2208 2209/* 2210 * vm_page_set_valid: 2211 * 2212 * Sets portions of a page valid. The arguments are expected 2213 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive 2214 * of any partial chunks touched by the range. The invalid portion of 2215 * such chunks will be zeroed. 2216 * 2217 * (base + size) must be less then or equal to PAGE_SIZE. 2218 */ 2219void 2220vm_page_set_valid(vm_page_t m, int base, int size) 2221{ 2222 int endoff, frag; 2223 2224 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 2225 if (size == 0) /* handle degenerate case */ 2226 return; 2227 2228 /* 2229 * If the base is not DEV_BSIZE aligned and the valid 2230 * bit is clear, we have to zero out a portion of the 2231 * first block. 2232 */ 2233 if ((frag = base & ~(DEV_BSIZE - 1)) != base && 2234 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0) 2235 pmap_zero_page_area(m, frag, base - frag); 2236 2237 /* 2238 * If the ending offset is not DEV_BSIZE aligned and the 2239 * valid bit is clear, we have to zero out a portion of 2240 * the last block. 2241 */ 2242 endoff = base + size; 2243 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff && 2244 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0) 2245 pmap_zero_page_area(m, endoff, 2246 DEV_BSIZE - (endoff & (DEV_BSIZE - 1))); 2247 2248 /* 2249 * Assert that no previously invalid block that is now being validated 2250 * is already dirty. 2251 */ 2252 KASSERT((~m->valid & vm_page_bits(base, size) & m->dirty) == 0, 2253 ("vm_page_set_valid: page %p is dirty", m)); 2254 2255 /* 2256 * Set valid bits inclusive of any overlap. 2257 */ 2258 m->valid |= vm_page_bits(base, size); 2259} 2260 2261/* 2262 * Clear the given bits from the specified page's dirty field. 2263 */ 2264static __inline void 2265vm_page_clear_dirty_mask(vm_page_t m, int pagebits) 2266{ 2267 2268 /* 2269 * If the object is locked and the page is neither VPO_BUSY nor 2270 * PG_WRITEABLE, then the page's dirty field cannot possibly be 2271 * modified by a concurrent pmap operation. 2272 */ 2273 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 2274 if ((m->oflags & VPO_BUSY) == 0 && (m->flags & PG_WRITEABLE) == 0) 2275 m->dirty &= ~pagebits; 2276 else { 2277 vm_page_lock_queues(); 2278 m->dirty &= ~pagebits; 2279 vm_page_unlock_queues(); 2280 } 2281} 2282 2283/* 2284 * vm_page_set_validclean: 2285 * 2286 * Sets portions of a page valid and clean. The arguments are expected 2287 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive 2288 * of any partial chunks touched by the range. The invalid portion of 2289 * such chunks will be zero'd. 2290 * 2291 * This routine may not block. 2292 * 2293 * (base + size) must be less then or equal to PAGE_SIZE. 2294 */ 2295void 2296vm_page_set_validclean(vm_page_t m, int base, int size) 2297{ 2298 u_long oldvalid; 2299 int endoff, frag, pagebits; 2300 2301 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 2302 if (size == 0) /* handle degenerate case */ 2303 return; 2304 2305 /* 2306 * If the base is not DEV_BSIZE aligned and the valid 2307 * bit is clear, we have to zero out a portion of the 2308 * first block. 2309 */ 2310 if ((frag = base & ~(DEV_BSIZE - 1)) != base && 2311 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0) 2312 pmap_zero_page_area(m, frag, base - frag); 2313 2314 /* 2315 * If the ending offset is not DEV_BSIZE aligned and the 2316 * valid bit is clear, we have to zero out a portion of 2317 * the last block. 2318 */ 2319 endoff = base + size; 2320 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff && 2321 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0) 2322 pmap_zero_page_area(m, endoff, 2323 DEV_BSIZE - (endoff & (DEV_BSIZE - 1))); 2324 2325 /* 2326 * Set valid, clear dirty bits. If validating the entire 2327 * page we can safely clear the pmap modify bit. We also 2328 * use this opportunity to clear the VPO_NOSYNC flag. If a process 2329 * takes a write fault on a MAP_NOSYNC memory area the flag will 2330 * be set again. 2331 * 2332 * We set valid bits inclusive of any overlap, but we can only 2333 * clear dirty bits for DEV_BSIZE chunks that are fully within 2334 * the range. 2335 */ 2336 oldvalid = m->valid; 2337 pagebits = vm_page_bits(base, size); 2338 m->valid |= pagebits; 2339#if 0 /* NOT YET */ 2340 if ((frag = base & (DEV_BSIZE - 1)) != 0) { 2341 frag = DEV_BSIZE - frag; 2342 base += frag; 2343 size -= frag; 2344 if (size < 0) 2345 size = 0; 2346 } 2347 pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1)); 2348#endif 2349 if (base == 0 && size == PAGE_SIZE) { 2350 /* 2351 * The page can only be modified within the pmap if it is 2352 * mapped, and it can only be mapped if it was previously 2353 * fully valid. 2354 */ 2355 if (oldvalid == VM_PAGE_BITS_ALL) 2356 /* 2357 * Perform the pmap_clear_modify() first. Otherwise, 2358 * a concurrent pmap operation, such as 2359 * pmap_protect(), could clear a modification in the 2360 * pmap and set the dirty field on the page before 2361 * pmap_clear_modify() had begun and after the dirty 2362 * field was cleared here. 2363 */ 2364 pmap_clear_modify(m); 2365 m->dirty = 0; 2366 m->oflags &= ~VPO_NOSYNC; 2367 } else if (oldvalid != VM_PAGE_BITS_ALL) 2368 m->dirty &= ~pagebits; 2369 else 2370 vm_page_clear_dirty_mask(m, pagebits); 2371} 2372 2373void 2374vm_page_clear_dirty(vm_page_t m, int base, int size) 2375{ 2376 2377 vm_page_clear_dirty_mask(m, vm_page_bits(base, size)); 2378} 2379 2380/* 2381 * vm_page_set_invalid: 2382 * 2383 * Invalidates DEV_BSIZE'd chunks within a page. Both the 2384 * valid and dirty bits for the effected areas are cleared. 2385 * 2386 * May not block. 2387 */ 2388void 2389vm_page_set_invalid(vm_page_t m, int base, int size) 2390{ 2391 int bits; 2392 2393 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 2394 KASSERT((m->oflags & VPO_BUSY) == 0, 2395 ("vm_page_set_invalid: page %p is busy", m)); 2396 bits = vm_page_bits(base, size); 2397 if (m->valid == VM_PAGE_BITS_ALL && bits != 0) 2398 pmap_remove_all(m); 2399 KASSERT(!pmap_page_is_mapped(m), 2400 ("vm_page_set_invalid: page %p is mapped", m)); 2401 m->valid &= ~bits; 2402 m->dirty &= ~bits; 2403} 2404 2405/* 2406 * vm_page_zero_invalid() 2407 * 2408 * The kernel assumes that the invalid portions of a page contain 2409 * garbage, but such pages can be mapped into memory by user code. 2410 * When this occurs, we must zero out the non-valid portions of the 2411 * page so user code sees what it expects. 2412 * 2413 * Pages are most often semi-valid when the end of a file is mapped 2414 * into memory and the file's size is not page aligned. 2415 */ 2416void 2417vm_page_zero_invalid(vm_page_t m, boolean_t setvalid) 2418{ 2419 int b; 2420 int i; 2421 2422 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 2423 /* 2424 * Scan the valid bits looking for invalid sections that 2425 * must be zerod. Invalid sub-DEV_BSIZE'd areas ( where the 2426 * valid bit may be set ) have already been zerod by 2427 * vm_page_set_validclean(). 2428 */ 2429 for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) { 2430 if (i == (PAGE_SIZE / DEV_BSIZE) || 2431 (m->valid & (1 << i)) 2432 ) { 2433 if (i > b) { 2434 pmap_zero_page_area(m, 2435 b << DEV_BSHIFT, (i - b) << DEV_BSHIFT); 2436 } 2437 b = i + 1; 2438 } 2439 } 2440 2441 /* 2442 * setvalid is TRUE when we can safely set the zero'd areas 2443 * as being valid. We can do this if there are no cache consistancy 2444 * issues. e.g. it is ok to do with UFS, but not ok to do with NFS. 2445 */ 2446 if (setvalid) 2447 m->valid = VM_PAGE_BITS_ALL; 2448} 2449 2450/* 2451 * vm_page_is_valid: 2452 * 2453 * Is (partial) page valid? Note that the case where size == 0 2454 * will return FALSE in the degenerate case where the page is 2455 * entirely invalid, and TRUE otherwise. 2456 * 2457 * May not block. 2458 */ 2459int 2460vm_page_is_valid(vm_page_t m, int base, int size) 2461{ 2462 int bits = vm_page_bits(base, size); 2463 2464 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 2465 if (m->valid && ((m->valid & bits) == bits)) 2466 return 1; 2467 else 2468 return 0; 2469} 2470 2471/* 2472 * update dirty bits from pmap/mmu. May not block. 2473 */ 2474void 2475vm_page_test_dirty(vm_page_t m) 2476{ 2477 2478 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 2479 if (m->dirty != VM_PAGE_BITS_ALL && pmap_is_modified(m)) 2480 vm_page_dirty(m); 2481} 2482 2483int so_zerocp_fullpage = 0; 2484 2485/* 2486 * Replace the given page with a copy. The copied page assumes 2487 * the portion of the given page's "wire_count" that is not the 2488 * responsibility of this copy-on-write mechanism. 2489 * 2490 * The object containing the given page must have a non-zero 2491 * paging-in-progress count and be locked. 2492 */ 2493void 2494vm_page_cowfault(vm_page_t m) 2495{ 2496 vm_page_t mnew; 2497 vm_object_t object; 2498 vm_pindex_t pindex; 2499 2500 mtx_assert(&vm_page_queue_mtx, MA_NOTOWNED); 2501 vm_page_lock_assert(m, MA_OWNED); 2502 object = m->object; 2503 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 2504 KASSERT(object->paging_in_progress != 0, 2505 ("vm_page_cowfault: object %p's paging-in-progress count is zero.", 2506 object)); 2507 pindex = m->pindex; 2508 2509 retry_alloc: 2510 pmap_remove_all(m); 2511 vm_page_remove(m); 2512 mnew = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL | VM_ALLOC_NOBUSY); 2513 if (mnew == NULL) { 2514 vm_page_insert(m, object, pindex); 2515 vm_page_unlock(m); 2516 VM_OBJECT_UNLOCK(object); 2517 VM_WAIT; 2518 VM_OBJECT_LOCK(object); 2519 if (m == vm_page_lookup(object, pindex)) { 2520 vm_page_lock(m); 2521 goto retry_alloc; 2522 } else { 2523 /* 2524 * Page disappeared during the wait. 2525 */ 2526 return; 2527 } 2528 } 2529 2530 if (m->cow == 0) { 2531 /* 2532 * check to see if we raced with an xmit complete when 2533 * waiting to allocate a page. If so, put things back 2534 * the way they were 2535 */ 2536 vm_page_unlock(m); 2537 vm_page_lock(mnew); 2538 vm_page_free(mnew); 2539 vm_page_unlock(mnew); 2540 vm_page_insert(m, object, pindex); 2541 } else { /* clear COW & copy page */ 2542 if (!so_zerocp_fullpage) 2543 pmap_copy_page(m, mnew); 2544 mnew->valid = VM_PAGE_BITS_ALL; 2545 vm_page_dirty(mnew); 2546 mnew->wire_count = m->wire_count - m->cow; 2547 m->wire_count = m->cow; 2548 vm_page_unlock(m); 2549 } 2550} 2551 2552void 2553vm_page_cowclear(vm_page_t m) 2554{ 2555 2556 vm_page_lock_assert(m, MA_OWNED); 2557 if (m->cow) { 2558 m->cow--; 2559 /* 2560 * let vm_fault add back write permission lazily 2561 */ 2562 } 2563 /* 2564 * sf_buf_free() will free the page, so we needn't do it here 2565 */ 2566} 2567 2568int 2569vm_page_cowsetup(vm_page_t m) 2570{ 2571 2572 vm_page_lock_assert(m, MA_OWNED); 2573 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0 || 2574 m->cow == USHRT_MAX - 1 || !VM_OBJECT_TRYLOCK(m->object)) 2575 return (EBUSY); 2576 m->cow++; 2577 pmap_remove_write(m); 2578 VM_OBJECT_UNLOCK(m->object); 2579 return (0); 2580} 2581 2582#include "opt_ddb.h" 2583#ifdef DDB 2584#include <sys/kernel.h> 2585 2586#include <ddb/ddb.h> 2587 2588DB_SHOW_COMMAND(page, vm_page_print_page_info) 2589{ 2590 db_printf("cnt.v_free_count: %d\n", cnt.v_free_count); 2591 db_printf("cnt.v_cache_count: %d\n", cnt.v_cache_count); 2592 db_printf("cnt.v_inactive_count: %d\n", cnt.v_inactive_count); 2593 db_printf("cnt.v_active_count: %d\n", cnt.v_active_count); 2594 db_printf("cnt.v_wire_count: %d\n", cnt.v_wire_count); 2595 db_printf("cnt.v_free_reserved: %d\n", cnt.v_free_reserved); 2596 db_printf("cnt.v_free_min: %d\n", cnt.v_free_min); 2597 db_printf("cnt.v_free_target: %d\n", cnt.v_free_target); 2598 db_printf("cnt.v_cache_min: %d\n", cnt.v_cache_min); 2599 db_printf("cnt.v_inactive_target: %d\n", cnt.v_inactive_target); 2600} 2601 2602DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info) 2603{ 2604 2605 db_printf("PQ_FREE:"); 2606 db_printf(" %d", cnt.v_free_count); 2607 db_printf("\n"); 2608 2609 db_printf("PQ_CACHE:"); 2610 db_printf(" %d", cnt.v_cache_count); 2611 db_printf("\n"); 2612 2613 db_printf("PQ_ACTIVE: %d, PQ_INACTIVE: %d\n", 2614 *vm_page_queues[PQ_ACTIVE].cnt, 2615 *vm_page_queues[PQ_INACTIVE].cnt); 2616} 2617#endif /* DDB */ 2618