vm_page.c revision 217478
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 217478 2011-01-16 18:01:39Z 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 mtx_unlock(&vm_page_queue_free_mtx); 1342 m->act_count = 0; 1343 1344 if (object != NULL) { 1345 /* Ignore device objects; the pager sets "memattr" for them. */ 1346 if (object->memattr != VM_MEMATTR_DEFAULT && 1347 object->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 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1607 if ((queue = m->queue) != PQ_ACTIVE) { 1608 if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) { 1609 if (m->act_count < ACT_INIT) 1610 m->act_count = ACT_INIT; 1611 vm_page_lock_queues(); 1612 if (queue != PQ_NONE) 1613 vm_page_queue_remove(queue, m); 1614 vm_page_enqueue(PQ_ACTIVE, m); 1615 vm_page_unlock_queues(); 1616 } else 1617 KASSERT(queue == PQ_NONE, 1618 ("vm_page_activate: wired page %p is queued", m)); 1619 } else { 1620 if (m->act_count < ACT_INIT) 1621 m->act_count = ACT_INIT; 1622 } 1623} 1624 1625/* 1626 * vm_page_free_wakeup: 1627 * 1628 * Helper routine for vm_page_free_toq() and vm_page_cache(). This 1629 * routine is called when a page has been added to the cache or free 1630 * queues. 1631 * 1632 * The page queues must be locked. 1633 * This routine may not block. 1634 */ 1635static inline void 1636vm_page_free_wakeup(void) 1637{ 1638 1639 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 1640 /* 1641 * if pageout daemon needs pages, then tell it that there are 1642 * some free. 1643 */ 1644 if (vm_pageout_pages_needed && 1645 cnt.v_cache_count + cnt.v_free_count >= cnt.v_pageout_free_min) { 1646 wakeup(&vm_pageout_pages_needed); 1647 vm_pageout_pages_needed = 0; 1648 } 1649 /* 1650 * wakeup processes that are waiting on memory if we hit a 1651 * high water mark. And wakeup scheduler process if we have 1652 * lots of memory. this process will swapin processes. 1653 */ 1654 if (vm_pages_needed && !vm_page_count_min()) { 1655 vm_pages_needed = 0; 1656 wakeup(&cnt.v_free_count); 1657 } 1658} 1659 1660/* 1661 * vm_page_free_toq: 1662 * 1663 * Returns the given page to the free list, 1664 * disassociating it with any VM object. 1665 * 1666 * Object and page must be locked prior to entry. 1667 * This routine may not block. 1668 */ 1669 1670void 1671vm_page_free_toq(vm_page_t m) 1672{ 1673 1674 if ((m->flags & PG_UNMANAGED) == 0) { 1675 vm_page_lock_assert(m, MA_OWNED); 1676 KASSERT(!pmap_page_is_mapped(m), 1677 ("vm_page_free_toq: freeing mapped page %p", m)); 1678 } 1679 PCPU_INC(cnt.v_tfree); 1680 1681 if (VM_PAGE_IS_FREE(m)) 1682 panic("vm_page_free: freeing free page %p", m); 1683 else if (m->busy != 0) 1684 panic("vm_page_free: freeing busy page %p", m); 1685 1686 /* 1687 * unqueue, then remove page. Note that we cannot destroy 1688 * the page here because we do not want to call the pager's 1689 * callback routine until after we've put the page on the 1690 * appropriate free queue. 1691 */ 1692 if ((m->flags & PG_UNMANAGED) == 0) 1693 vm_pageq_remove(m); 1694 vm_page_remove(m); 1695 1696 /* 1697 * If fictitious remove object association and 1698 * return, otherwise delay object association removal. 1699 */ 1700 if ((m->flags & PG_FICTITIOUS) != 0) { 1701 return; 1702 } 1703 1704 m->valid = 0; 1705 vm_page_undirty(m); 1706 1707 if (m->wire_count != 0) 1708 panic("vm_page_free: freeing wired page %p", m); 1709 if (m->hold_count != 0) { 1710 m->flags &= ~PG_ZERO; 1711 vm_page_lock_queues(); 1712 vm_page_enqueue(PQ_HOLD, m); 1713 vm_page_unlock_queues(); 1714 } else { 1715 /* 1716 * Restore the default memory attribute to the page. 1717 */ 1718 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT) 1719 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT); 1720 1721 /* 1722 * Insert the page into the physical memory allocator's 1723 * cache/free page queues. 1724 */ 1725 mtx_lock(&vm_page_queue_free_mtx); 1726 m->flags |= PG_FREE; 1727 cnt.v_free_count++; 1728#if VM_NRESERVLEVEL > 0 1729 if (!vm_reserv_free_page(m)) 1730#else 1731 if (TRUE) 1732#endif 1733 vm_phys_free_pages(m, 0); 1734 if ((m->flags & PG_ZERO) != 0) 1735 ++vm_page_zero_count; 1736 else 1737 vm_page_zero_idle_wakeup(); 1738 vm_page_free_wakeup(); 1739 mtx_unlock(&vm_page_queue_free_mtx); 1740 } 1741} 1742 1743/* 1744 * vm_page_wire: 1745 * 1746 * Mark this page as wired down by yet 1747 * another map, removing it from paging queues 1748 * as necessary. 1749 * 1750 * If the page is fictitious, then its wire count must remain one. 1751 * 1752 * The page must be locked. 1753 * This routine may not block. 1754 */ 1755void 1756vm_page_wire(vm_page_t m) 1757{ 1758 1759 /* 1760 * Only bump the wire statistics if the page is not already wired, 1761 * and only unqueue the page if it is on some queue (if it is unmanaged 1762 * it is already off the queues). 1763 */ 1764 vm_page_lock_assert(m, MA_OWNED); 1765 if ((m->flags & PG_FICTITIOUS) != 0) { 1766 KASSERT(m->wire_count == 1, 1767 ("vm_page_wire: fictitious page %p's wire count isn't one", 1768 m)); 1769 return; 1770 } 1771 if (m->wire_count == 0) { 1772 if ((m->flags & PG_UNMANAGED) == 0) 1773 vm_pageq_remove(m); 1774 atomic_add_int(&cnt.v_wire_count, 1); 1775 } 1776 m->wire_count++; 1777 KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m)); 1778} 1779 1780/* 1781 * vm_page_unwire: 1782 * 1783 * Release one wiring of the specified page, potentially enabling it to be 1784 * paged again. If paging is enabled, then the value of the parameter 1785 * "activate" determines to which queue the page is added. If "activate" is 1786 * non-zero, then the page is added to the active queue. Otherwise, it is 1787 * added to the inactive queue. 1788 * 1789 * However, unless the page belongs to an object, it is not enqueued because 1790 * it cannot be paged out. 1791 * 1792 * If a page is fictitious, then its wire count must alway be one. 1793 * 1794 * A managed page must be locked. 1795 */ 1796void 1797vm_page_unwire(vm_page_t m, int activate) 1798{ 1799 1800 if ((m->flags & PG_UNMANAGED) == 0) 1801 vm_page_lock_assert(m, MA_OWNED); 1802 if ((m->flags & PG_FICTITIOUS) != 0) { 1803 KASSERT(m->wire_count == 1, 1804 ("vm_page_unwire: fictitious page %p's wire count isn't one", m)); 1805 return; 1806 } 1807 if (m->wire_count > 0) { 1808 m->wire_count--; 1809 if (m->wire_count == 0) { 1810 atomic_subtract_int(&cnt.v_wire_count, 1); 1811 if ((m->flags & PG_UNMANAGED) != 0 || 1812 m->object == NULL) 1813 return; 1814 vm_page_lock_queues(); 1815 if (activate) 1816 vm_page_enqueue(PQ_ACTIVE, m); 1817 else { 1818 vm_page_flag_clear(m, PG_WINATCFLS); 1819 vm_page_enqueue(PQ_INACTIVE, m); 1820 } 1821 vm_page_unlock_queues(); 1822 } 1823 } else 1824 panic("vm_page_unwire: page %p's wire count is zero", m); 1825} 1826 1827/* 1828 * Move the specified page to the inactive queue. 1829 * 1830 * Many pages placed on the inactive queue should actually go 1831 * into the cache, but it is difficult to figure out which. What 1832 * we do instead, if the inactive target is well met, is to put 1833 * clean pages at the head of the inactive queue instead of the tail. 1834 * This will cause them to be moved to the cache more quickly and 1835 * if not actively re-referenced, reclaimed more quickly. If we just 1836 * stick these pages at the end of the inactive queue, heavy filesystem 1837 * meta-data accesses can cause an unnecessary paging load on memory bound 1838 * processes. This optimization causes one-time-use metadata to be 1839 * reused more quickly. 1840 * 1841 * Normally athead is 0 resulting in LRU operation. athead is set 1842 * to 1 if we want this page to be 'as if it were placed in the cache', 1843 * except without unmapping it from the process address space. 1844 * 1845 * This routine may not block. 1846 */ 1847static inline void 1848_vm_page_deactivate(vm_page_t m, int athead) 1849{ 1850 int queue; 1851 1852 vm_page_lock_assert(m, MA_OWNED); 1853 1854 /* 1855 * Ignore if already inactive. 1856 */ 1857 if ((queue = m->queue) == PQ_INACTIVE) 1858 return; 1859 if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) { 1860 vm_page_lock_queues(); 1861 vm_page_flag_clear(m, PG_WINATCFLS); 1862 if (queue != PQ_NONE) 1863 vm_page_queue_remove(queue, m); 1864 if (athead) 1865 TAILQ_INSERT_HEAD(&vm_page_queues[PQ_INACTIVE].pl, m, 1866 pageq); 1867 else 1868 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, 1869 pageq); 1870 m->queue = PQ_INACTIVE; 1871 cnt.v_inactive_count++; 1872 vm_page_unlock_queues(); 1873 } 1874} 1875 1876/* 1877 * Move the specified page to the inactive queue. 1878 * 1879 * The page must be locked. 1880 */ 1881void 1882vm_page_deactivate(vm_page_t m) 1883{ 1884 1885 _vm_page_deactivate(m, 0); 1886} 1887 1888/* 1889 * vm_page_try_to_cache: 1890 * 1891 * Returns 0 on failure, 1 on success 1892 */ 1893int 1894vm_page_try_to_cache(vm_page_t m) 1895{ 1896 1897 vm_page_lock_assert(m, MA_OWNED); 1898 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1899 if (m->dirty || m->hold_count || m->busy || m->wire_count || 1900 (m->oflags & VPO_BUSY) || (m->flags & PG_UNMANAGED)) 1901 return (0); 1902 pmap_remove_all(m); 1903 if (m->dirty) 1904 return (0); 1905 vm_page_cache(m); 1906 return (1); 1907} 1908 1909/* 1910 * vm_page_try_to_free() 1911 * 1912 * Attempt to free the page. If we cannot free it, we do nothing. 1913 * 1 is returned on success, 0 on failure. 1914 */ 1915int 1916vm_page_try_to_free(vm_page_t m) 1917{ 1918 1919 vm_page_lock_assert(m, MA_OWNED); 1920 if (m->object != NULL) 1921 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1922 if (m->dirty || m->hold_count || m->busy || m->wire_count || 1923 (m->oflags & VPO_BUSY) || (m->flags & PG_UNMANAGED)) 1924 return (0); 1925 pmap_remove_all(m); 1926 if (m->dirty) 1927 return (0); 1928 vm_page_free(m); 1929 return (1); 1930} 1931 1932/* 1933 * vm_page_cache 1934 * 1935 * Put the specified page onto the page cache queue (if appropriate). 1936 * 1937 * This routine may not block. 1938 */ 1939void 1940vm_page_cache(vm_page_t m) 1941{ 1942 vm_object_t object; 1943 vm_page_t root; 1944 1945 vm_page_lock_assert(m, MA_OWNED); 1946 object = m->object; 1947 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1948 if ((m->flags & PG_UNMANAGED) || (m->oflags & VPO_BUSY) || m->busy || 1949 m->hold_count || m->wire_count) 1950 panic("vm_page_cache: attempting to cache busy page"); 1951 pmap_remove_all(m); 1952 if (m->dirty != 0) 1953 panic("vm_page_cache: page %p is dirty", m); 1954 if (m->valid == 0 || object->type == OBJT_DEFAULT || 1955 (object->type == OBJT_SWAP && 1956 !vm_pager_has_page(object, m->pindex, NULL, NULL))) { 1957 /* 1958 * Hypothesis: A cache-elgible page belonging to a 1959 * default object or swap object but without a backing 1960 * store must be zero filled. 1961 */ 1962 vm_page_free(m); 1963 return; 1964 } 1965 KASSERT((m->flags & PG_CACHED) == 0, 1966 ("vm_page_cache: page %p is already cached", m)); 1967 PCPU_INC(cnt.v_tcached); 1968 1969 /* 1970 * Remove the page from the paging queues. 1971 */ 1972 vm_pageq_remove(m); 1973 1974 /* 1975 * Remove the page from the object's collection of resident 1976 * pages. 1977 */ 1978 if (m != object->root) 1979 vm_page_splay(m->pindex, object->root); 1980 if (m->left == NULL) 1981 root = m->right; 1982 else { 1983 root = vm_page_splay(m->pindex, m->left); 1984 root->right = m->right; 1985 } 1986 object->root = root; 1987 TAILQ_REMOVE(&object->memq, m, listq); 1988 object->resident_page_count--; 1989 1990 /* 1991 * Restore the default memory attribute to the page. 1992 */ 1993 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT) 1994 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT); 1995 1996 /* 1997 * Insert the page into the object's collection of cached pages 1998 * and the physical memory allocator's cache/free page queues. 1999 */ 2000 m->flags &= ~PG_ZERO; 2001 mtx_lock(&vm_page_queue_free_mtx); 2002 m->flags |= PG_CACHED; 2003 cnt.v_cache_count++; 2004 root = object->cache; 2005 if (root == NULL) { 2006 m->left = NULL; 2007 m->right = NULL; 2008 } else { 2009 root = vm_page_splay(m->pindex, root); 2010 if (m->pindex < root->pindex) { 2011 m->left = root->left; 2012 m->right = root; 2013 root->left = NULL; 2014 } else if (__predict_false(m->pindex == root->pindex)) 2015 panic("vm_page_cache: offset already cached"); 2016 else { 2017 m->right = root->right; 2018 m->left = root; 2019 root->right = NULL; 2020 } 2021 } 2022 object->cache = m; 2023#if VM_NRESERVLEVEL > 0 2024 if (!vm_reserv_free_page(m)) { 2025#else 2026 if (TRUE) { 2027#endif 2028 vm_phys_set_pool(VM_FREEPOOL_CACHE, m, 0); 2029 vm_phys_free_pages(m, 0); 2030 } 2031 vm_page_free_wakeup(); 2032 mtx_unlock(&vm_page_queue_free_mtx); 2033 2034 /* 2035 * Increment the vnode's hold count if this is the object's only 2036 * cached page. Decrement the vnode's hold count if this was 2037 * the object's only resident page. 2038 */ 2039 if (object->type == OBJT_VNODE) { 2040 if (root == NULL && object->resident_page_count != 0) 2041 vhold(object->handle); 2042 else if (root != NULL && object->resident_page_count == 0) 2043 vdrop(object->handle); 2044 } 2045} 2046 2047/* 2048 * vm_page_dontneed 2049 * 2050 * Cache, deactivate, or do nothing as appropriate. This routine 2051 * is typically used by madvise() MADV_DONTNEED. 2052 * 2053 * Generally speaking we want to move the page into the cache so 2054 * it gets reused quickly. However, this can result in a silly syndrome 2055 * due to the page recycling too quickly. Small objects will not be 2056 * fully cached. On the otherhand, if we move the page to the inactive 2057 * queue we wind up with a problem whereby very large objects 2058 * unnecessarily blow away our inactive and cache queues. 2059 * 2060 * The solution is to move the pages based on a fixed weighting. We 2061 * either leave them alone, deactivate them, or move them to the cache, 2062 * where moving them to the cache has the highest weighting. 2063 * By forcing some pages into other queues we eventually force the 2064 * system to balance the queues, potentially recovering other unrelated 2065 * space from active. The idea is to not force this to happen too 2066 * often. 2067 */ 2068void 2069vm_page_dontneed(vm_page_t m) 2070{ 2071 int dnw; 2072 int head; 2073 2074 vm_page_lock_assert(m, MA_OWNED); 2075 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 2076 dnw = PCPU_GET(dnweight); 2077 PCPU_INC(dnweight); 2078 2079 /* 2080 * Occasionally leave the page alone. 2081 */ 2082 if ((dnw & 0x01F0) == 0 || m->queue == PQ_INACTIVE) { 2083 if (m->act_count >= ACT_INIT) 2084 --m->act_count; 2085 return; 2086 } 2087 2088 /* 2089 * Clear any references to the page. Otherwise, the page daemon will 2090 * immediately reactivate the page. 2091 * 2092 * Perform the pmap_clear_reference() first. Otherwise, a concurrent 2093 * pmap operation, such as pmap_remove(), could clear a reference in 2094 * the pmap and set PG_REFERENCED on the page before the 2095 * pmap_clear_reference() had completed. Consequently, the page would 2096 * appear referenced based upon an old reference that occurred before 2097 * this function ran. 2098 */ 2099 pmap_clear_reference(m); 2100 vm_page_lock_queues(); 2101 vm_page_flag_clear(m, PG_REFERENCED); 2102 vm_page_unlock_queues(); 2103 2104 if (m->dirty == 0 && pmap_is_modified(m)) 2105 vm_page_dirty(m); 2106 2107 if (m->dirty || (dnw & 0x0070) == 0) { 2108 /* 2109 * Deactivate the page 3 times out of 32. 2110 */ 2111 head = 0; 2112 } else { 2113 /* 2114 * Cache the page 28 times out of every 32. Note that 2115 * the page is deactivated instead of cached, but placed 2116 * at the head of the queue instead of the tail. 2117 */ 2118 head = 1; 2119 } 2120 _vm_page_deactivate(m, head); 2121} 2122 2123/* 2124 * Grab a page, waiting until we are waken up due to the page 2125 * changing state. We keep on waiting, if the page continues 2126 * to be in the object. If the page doesn't exist, first allocate it 2127 * and then conditionally zero it. 2128 * 2129 * The caller must always specify the VM_ALLOC_RETRY flag. This is intended 2130 * to facilitate its eventual removal. 2131 * 2132 * This routine may block. 2133 */ 2134vm_page_t 2135vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags) 2136{ 2137 vm_page_t m; 2138 2139 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 2140 KASSERT((allocflags & VM_ALLOC_RETRY) != 0, 2141 ("vm_page_grab: VM_ALLOC_RETRY is required")); 2142retrylookup: 2143 if ((m = vm_page_lookup(object, pindex)) != NULL) { 2144 if ((m->oflags & VPO_BUSY) != 0 || 2145 ((allocflags & VM_ALLOC_IGN_SBUSY) == 0 && m->busy != 0)) { 2146 /* 2147 * Reference the page before unlocking and 2148 * sleeping so that the page daemon is less 2149 * likely to reclaim it. 2150 */ 2151 vm_page_lock_queues(); 2152 vm_page_flag_set(m, PG_REFERENCED); 2153 vm_page_sleep(m, "pgrbwt"); 2154 goto retrylookup; 2155 } else { 2156 if ((allocflags & VM_ALLOC_WIRED) != 0) { 2157 vm_page_lock(m); 2158 vm_page_wire(m); 2159 vm_page_unlock(m); 2160 } 2161 if ((allocflags & VM_ALLOC_NOBUSY) == 0) 2162 vm_page_busy(m); 2163 return (m); 2164 } 2165 } 2166 m = vm_page_alloc(object, pindex, allocflags & ~(VM_ALLOC_RETRY | 2167 VM_ALLOC_IGN_SBUSY)); 2168 if (m == NULL) { 2169 VM_OBJECT_UNLOCK(object); 2170 VM_WAIT; 2171 VM_OBJECT_LOCK(object); 2172 goto retrylookup; 2173 } else if (m->valid != 0) 2174 return (m); 2175 if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0) 2176 pmap_zero_page(m); 2177 return (m); 2178} 2179 2180/* 2181 * Mapping function for valid bits or for dirty bits in 2182 * a page. May not block. 2183 * 2184 * Inputs are required to range within a page. 2185 */ 2186int 2187vm_page_bits(int base, int size) 2188{ 2189 int first_bit; 2190 int last_bit; 2191 2192 KASSERT( 2193 base + size <= PAGE_SIZE, 2194 ("vm_page_bits: illegal base/size %d/%d", base, size) 2195 ); 2196 2197 if (size == 0) /* handle degenerate case */ 2198 return (0); 2199 2200 first_bit = base >> DEV_BSHIFT; 2201 last_bit = (base + size - 1) >> DEV_BSHIFT; 2202 2203 return ((2 << last_bit) - (1 << first_bit)); 2204} 2205 2206/* 2207 * vm_page_set_valid: 2208 * 2209 * Sets portions of a page valid. The arguments are expected 2210 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive 2211 * of any partial chunks touched by the range. The invalid portion of 2212 * such chunks will be zeroed. 2213 * 2214 * (base + size) must be less then or equal to PAGE_SIZE. 2215 */ 2216void 2217vm_page_set_valid(vm_page_t m, int base, int size) 2218{ 2219 int endoff, frag; 2220 2221 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 2222 if (size == 0) /* handle degenerate case */ 2223 return; 2224 2225 /* 2226 * If the base is not DEV_BSIZE aligned and the valid 2227 * bit is clear, we have to zero out a portion of the 2228 * first block. 2229 */ 2230 if ((frag = base & ~(DEV_BSIZE - 1)) != base && 2231 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0) 2232 pmap_zero_page_area(m, frag, base - frag); 2233 2234 /* 2235 * If the ending offset is not DEV_BSIZE aligned and the 2236 * valid bit is clear, we have to zero out a portion of 2237 * the last block. 2238 */ 2239 endoff = base + size; 2240 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff && 2241 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0) 2242 pmap_zero_page_area(m, endoff, 2243 DEV_BSIZE - (endoff & (DEV_BSIZE - 1))); 2244 2245 /* 2246 * Assert that no previously invalid block that is now being validated 2247 * is already dirty. 2248 */ 2249 KASSERT((~m->valid & vm_page_bits(base, size) & m->dirty) == 0, 2250 ("vm_page_set_valid: page %p is dirty", m)); 2251 2252 /* 2253 * Set valid bits inclusive of any overlap. 2254 */ 2255 m->valid |= vm_page_bits(base, size); 2256} 2257 2258/* 2259 * Clear the given bits from the specified page's dirty field. 2260 */ 2261static __inline void 2262vm_page_clear_dirty_mask(vm_page_t m, int pagebits) 2263{ 2264 2265 /* 2266 * If the object is locked and the page is neither VPO_BUSY nor 2267 * PG_WRITEABLE, then the page's dirty field cannot possibly be 2268 * modified by a concurrent pmap operation. 2269 */ 2270 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 2271 if ((m->oflags & VPO_BUSY) == 0 && (m->flags & PG_WRITEABLE) == 0) 2272 m->dirty &= ~pagebits; 2273 else { 2274 vm_page_lock_queues(); 2275 m->dirty &= ~pagebits; 2276 vm_page_unlock_queues(); 2277 } 2278} 2279 2280/* 2281 * vm_page_set_validclean: 2282 * 2283 * Sets portions of a page valid and clean. The arguments are expected 2284 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive 2285 * of any partial chunks touched by the range. The invalid portion of 2286 * such chunks will be zero'd. 2287 * 2288 * This routine may not block. 2289 * 2290 * (base + size) must be less then or equal to PAGE_SIZE. 2291 */ 2292void 2293vm_page_set_validclean(vm_page_t m, int base, int size) 2294{ 2295 u_long oldvalid; 2296 int endoff, frag, pagebits; 2297 2298 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 2299 if (size == 0) /* handle degenerate case */ 2300 return; 2301 2302 /* 2303 * If the base is not DEV_BSIZE aligned and the valid 2304 * bit is clear, we have to zero out a portion of the 2305 * first block. 2306 */ 2307 if ((frag = base & ~(DEV_BSIZE - 1)) != base && 2308 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0) 2309 pmap_zero_page_area(m, frag, base - frag); 2310 2311 /* 2312 * If the ending offset is not DEV_BSIZE aligned and the 2313 * valid bit is clear, we have to zero out a portion of 2314 * the last block. 2315 */ 2316 endoff = base + size; 2317 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff && 2318 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0) 2319 pmap_zero_page_area(m, endoff, 2320 DEV_BSIZE - (endoff & (DEV_BSIZE - 1))); 2321 2322 /* 2323 * Set valid, clear dirty bits. If validating the entire 2324 * page we can safely clear the pmap modify bit. We also 2325 * use this opportunity to clear the VPO_NOSYNC flag. If a process 2326 * takes a write fault on a MAP_NOSYNC memory area the flag will 2327 * be set again. 2328 * 2329 * We set valid bits inclusive of any overlap, but we can only 2330 * clear dirty bits for DEV_BSIZE chunks that are fully within 2331 * the range. 2332 */ 2333 oldvalid = m->valid; 2334 pagebits = vm_page_bits(base, size); 2335 m->valid |= pagebits; 2336#if 0 /* NOT YET */ 2337 if ((frag = base & (DEV_BSIZE - 1)) != 0) { 2338 frag = DEV_BSIZE - frag; 2339 base += frag; 2340 size -= frag; 2341 if (size < 0) 2342 size = 0; 2343 } 2344 pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1)); 2345#endif 2346 if (base == 0 && size == PAGE_SIZE) { 2347 /* 2348 * The page can only be modified within the pmap if it is 2349 * mapped, and it can only be mapped if it was previously 2350 * fully valid. 2351 */ 2352 if (oldvalid == VM_PAGE_BITS_ALL) 2353 /* 2354 * Perform the pmap_clear_modify() first. Otherwise, 2355 * a concurrent pmap operation, such as 2356 * pmap_protect(), could clear a modification in the 2357 * pmap and set the dirty field on the page before 2358 * pmap_clear_modify() had begun and after the dirty 2359 * field was cleared here. 2360 */ 2361 pmap_clear_modify(m); 2362 m->dirty = 0; 2363 m->oflags &= ~VPO_NOSYNC; 2364 } else if (oldvalid != VM_PAGE_BITS_ALL) 2365 m->dirty &= ~pagebits; 2366 else 2367 vm_page_clear_dirty_mask(m, pagebits); 2368} 2369 2370void 2371vm_page_clear_dirty(vm_page_t m, int base, int size) 2372{ 2373 2374 vm_page_clear_dirty_mask(m, vm_page_bits(base, size)); 2375} 2376 2377/* 2378 * vm_page_set_invalid: 2379 * 2380 * Invalidates DEV_BSIZE'd chunks within a page. Both the 2381 * valid and dirty bits for the effected areas are cleared. 2382 * 2383 * May not block. 2384 */ 2385void 2386vm_page_set_invalid(vm_page_t m, int base, int size) 2387{ 2388 int bits; 2389 2390 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 2391 KASSERT((m->oflags & VPO_BUSY) == 0, 2392 ("vm_page_set_invalid: page %p is busy", m)); 2393 bits = vm_page_bits(base, size); 2394 if (m->valid == VM_PAGE_BITS_ALL && bits != 0) 2395 pmap_remove_all(m); 2396 KASSERT(!pmap_page_is_mapped(m), 2397 ("vm_page_set_invalid: page %p is mapped", m)); 2398 m->valid &= ~bits; 2399 m->dirty &= ~bits; 2400} 2401 2402/* 2403 * vm_page_zero_invalid() 2404 * 2405 * The kernel assumes that the invalid portions of a page contain 2406 * garbage, but such pages can be mapped into memory by user code. 2407 * When this occurs, we must zero out the non-valid portions of the 2408 * page so user code sees what it expects. 2409 * 2410 * Pages are most often semi-valid when the end of a file is mapped 2411 * into memory and the file's size is not page aligned. 2412 */ 2413void 2414vm_page_zero_invalid(vm_page_t m, boolean_t setvalid) 2415{ 2416 int b; 2417 int i; 2418 2419 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 2420 /* 2421 * Scan the valid bits looking for invalid sections that 2422 * must be zerod. Invalid sub-DEV_BSIZE'd areas ( where the 2423 * valid bit may be set ) have already been zerod by 2424 * vm_page_set_validclean(). 2425 */ 2426 for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) { 2427 if (i == (PAGE_SIZE / DEV_BSIZE) || 2428 (m->valid & (1 << i)) 2429 ) { 2430 if (i > b) { 2431 pmap_zero_page_area(m, 2432 b << DEV_BSHIFT, (i - b) << DEV_BSHIFT); 2433 } 2434 b = i + 1; 2435 } 2436 } 2437 2438 /* 2439 * setvalid is TRUE when we can safely set the zero'd areas 2440 * as being valid. We can do this if there are no cache consistancy 2441 * issues. e.g. it is ok to do with UFS, but not ok to do with NFS. 2442 */ 2443 if (setvalid) 2444 m->valid = VM_PAGE_BITS_ALL; 2445} 2446 2447/* 2448 * vm_page_is_valid: 2449 * 2450 * Is (partial) page valid? Note that the case where size == 0 2451 * will return FALSE in the degenerate case where the page is 2452 * entirely invalid, and TRUE otherwise. 2453 * 2454 * May not block. 2455 */ 2456int 2457vm_page_is_valid(vm_page_t m, int base, int size) 2458{ 2459 int bits = vm_page_bits(base, size); 2460 2461 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 2462 if (m->valid && ((m->valid & bits) == bits)) 2463 return 1; 2464 else 2465 return 0; 2466} 2467 2468/* 2469 * update dirty bits from pmap/mmu. May not block. 2470 */ 2471void 2472vm_page_test_dirty(vm_page_t m) 2473{ 2474 2475 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 2476 if (m->dirty != VM_PAGE_BITS_ALL && pmap_is_modified(m)) 2477 vm_page_dirty(m); 2478} 2479 2480int so_zerocp_fullpage = 0; 2481 2482/* 2483 * Replace the given page with a copy. The copied page assumes 2484 * the portion of the given page's "wire_count" that is not the 2485 * responsibility of this copy-on-write mechanism. 2486 * 2487 * The object containing the given page must have a non-zero 2488 * paging-in-progress count and be locked. 2489 */ 2490void 2491vm_page_cowfault(vm_page_t m) 2492{ 2493 vm_page_t mnew; 2494 vm_object_t object; 2495 vm_pindex_t pindex; 2496 2497 mtx_assert(&vm_page_queue_mtx, MA_NOTOWNED); 2498 vm_page_lock_assert(m, MA_OWNED); 2499 object = m->object; 2500 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 2501 KASSERT(object->paging_in_progress != 0, 2502 ("vm_page_cowfault: object %p's paging-in-progress count is zero.", 2503 object)); 2504 pindex = m->pindex; 2505 2506 retry_alloc: 2507 pmap_remove_all(m); 2508 vm_page_remove(m); 2509 mnew = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL | VM_ALLOC_NOBUSY); 2510 if (mnew == NULL) { 2511 vm_page_insert(m, object, pindex); 2512 vm_page_unlock(m); 2513 VM_OBJECT_UNLOCK(object); 2514 VM_WAIT; 2515 VM_OBJECT_LOCK(object); 2516 if (m == vm_page_lookup(object, pindex)) { 2517 vm_page_lock(m); 2518 goto retry_alloc; 2519 } else { 2520 /* 2521 * Page disappeared during the wait. 2522 */ 2523 return; 2524 } 2525 } 2526 2527 if (m->cow == 0) { 2528 /* 2529 * check to see if we raced with an xmit complete when 2530 * waiting to allocate a page. If so, put things back 2531 * the way they were 2532 */ 2533 vm_page_unlock(m); 2534 vm_page_lock(mnew); 2535 vm_page_free(mnew); 2536 vm_page_unlock(mnew); 2537 vm_page_insert(m, object, pindex); 2538 } else { /* clear COW & copy page */ 2539 if (!so_zerocp_fullpage) 2540 pmap_copy_page(m, mnew); 2541 mnew->valid = VM_PAGE_BITS_ALL; 2542 vm_page_dirty(mnew); 2543 mnew->wire_count = m->wire_count - m->cow; 2544 m->wire_count = m->cow; 2545 vm_page_unlock(m); 2546 } 2547} 2548 2549void 2550vm_page_cowclear(vm_page_t m) 2551{ 2552 2553 vm_page_lock_assert(m, MA_OWNED); 2554 if (m->cow) { 2555 m->cow--; 2556 /* 2557 * let vm_fault add back write permission lazily 2558 */ 2559 } 2560 /* 2561 * sf_buf_free() will free the page, so we needn't do it here 2562 */ 2563} 2564 2565int 2566vm_page_cowsetup(vm_page_t m) 2567{ 2568 2569 vm_page_lock_assert(m, MA_OWNED); 2570 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0 || 2571 m->cow == USHRT_MAX - 1 || !VM_OBJECT_TRYLOCK(m->object)) 2572 return (EBUSY); 2573 m->cow++; 2574 pmap_remove_write(m); 2575 VM_OBJECT_UNLOCK(m->object); 2576 return (0); 2577} 2578 2579#include "opt_ddb.h" 2580#ifdef DDB 2581#include <sys/kernel.h> 2582 2583#include <ddb/ddb.h> 2584 2585DB_SHOW_COMMAND(page, vm_page_print_page_info) 2586{ 2587 db_printf("cnt.v_free_count: %d\n", cnt.v_free_count); 2588 db_printf("cnt.v_cache_count: %d\n", cnt.v_cache_count); 2589 db_printf("cnt.v_inactive_count: %d\n", cnt.v_inactive_count); 2590 db_printf("cnt.v_active_count: %d\n", cnt.v_active_count); 2591 db_printf("cnt.v_wire_count: %d\n", cnt.v_wire_count); 2592 db_printf("cnt.v_free_reserved: %d\n", cnt.v_free_reserved); 2593 db_printf("cnt.v_free_min: %d\n", cnt.v_free_min); 2594 db_printf("cnt.v_free_target: %d\n", cnt.v_free_target); 2595 db_printf("cnt.v_cache_min: %d\n", cnt.v_cache_min); 2596 db_printf("cnt.v_inactive_target: %d\n", cnt.v_inactive_target); 2597} 2598 2599DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info) 2600{ 2601 2602 db_printf("PQ_FREE:"); 2603 db_printf(" %d", cnt.v_free_count); 2604 db_printf("\n"); 2605 2606 db_printf("PQ_CACHE:"); 2607 db_printf(" %d", cnt.v_cache_count); 2608 db_printf("\n"); 2609 2610 db_printf("PQ_ACTIVE: %d, PQ_INACTIVE: %d\n", 2611 *vm_page_queues[PQ_ACTIVE].cnt, 2612 *vm_page_queues[PQ_INACTIVE].cnt); 2613} 2614#endif /* DDB */ 2615