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