vm_page.c revision 119373
1130389Sle/* 2190507Slulf * Copyright (c) 1991 Regents of the University of California. 3130389Sle * All rights reserved. 4130389Sle * 5130389Sle * This code is derived from software contributed to Berkeley by 6130389Sle * The Mach Operating System project at Carnegie-Mellon University. 7130389Sle * 8130389Sle * Redistribution and use in source and binary forms, with or without 9130389Sle * modification, are permitted provided that the following conditions 10130389Sle * are met: 11130389Sle * 1. Redistributions of source code must retain the above copyright 12130389Sle * notice, this list of conditions and the following disclaimer. 13130389Sle * 2. Redistributions in binary form must reproduce the above copyright 14130389Sle * notice, this list of conditions and the following disclaimer in the 15130389Sle * documentation and/or other materials provided with the distribution. 16130389Sle * 3. All advertising materials mentioning features or use of this software 17130389Sle * must display the following acknowledgement: 18130389Sle * This product includes software developed by the University of 19130389Sle * California, Berkeley and its contributors. 20130389Sle * 4. Neither the name of the University nor the names of its contributors 21130389Sle * may be used to endorse or promote products derived from this software 22130389Sle * without specific prior written permission. 23130389Sle * 24130389Sle * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25130389Sle * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26130389Sle * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27130389Sle * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28130389Sle * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29130389Sle * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30130389Sle * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31130389Sle * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32130389Sle * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33130389Sle * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34130389Sle * SUCH DAMAGE. 35130389Sle * 36130389Sle * from: @(#)vm_page.c 7.4 (Berkeley) 5/7/91 37130389Sle */ 38138112Sle 39138112Sle/* 40138112Sle * Copyright (c) 1987, 1990 Carnegie-Mellon University. 41138112Sle * All rights reserved. 42138112Sle * 43138112Sle * Authors: Avadis Tevanian, Jr., Michael Wayne Young 44190507Slulf * 45190507Slulf * Permission to use, copy, modify and distribute this software and 46138112Sle * its documentation is hereby granted, provided that both the copyright 47190507Slulf * notice and this permission notice appear in all copies of the 48138112Sle * software, derivative works or modified versions, and any portions 49138112Sle * thereof, and that both notices appear in supporting documentation. 50138112Sle * 51138112Sle * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 52138112Sle * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 53138112Sle * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 54138112Sle * 55138112Sle * Carnegie Mellon requests users of this software to return to 56138112Sle * 57138112Sle * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 58138112Sle * School of Computer Science 59138112Sle * Carnegie Mellon University 60138112Sle * Pittsburgh PA 15213-3890 61138112Sle * 62138112Sle * any improvements or extensions that they make and grant Carnegie the 63138112Sle * rights to redistribute these changes. 64138112Sle */ 65138112Sle 66138112Sle/* 67138112Sle * GENERAL RULES ON VM_PAGE MANIPULATION 68138112Sle * 69138112Sle * - a pageq mutex is required when adding or removing a page from a 70138112Sle * page queue (vm_page_queue[]), regardless of other mutexes or the 71138112Sle * busy state of a page. 72138112Sle * 73138112Sle * - a hash chain mutex is required when associating or disassociating 74138112Sle * a page from the VM PAGE CACHE hash table (vm_page_buckets), 75190507Slulf * regardless of other mutexes or the busy state of a page. 76190507Slulf * 77190507Slulf * - either a hash chain mutex OR a busied page is required in order 78190507Slulf * to modify the page flags. A hash chain mutex must be obtained in 79190507Slulf * order to busy a page. A page's flags cannot be modified by a 80190507Slulf * hash chain mutex if the page is marked busy. 81190507Slulf * 82190507Slulf * - The object memq mutex is held when inserting or removing 83190507Slulf * pages from an object (vm_page_insert() or vm_page_remove()). This 84138112Sle * is different from the object's main mutex. 85190507Slulf * 86190507Slulf * Generally speaking, you have to be aware of side effects when running 87190507Slulf * vm_page ops. A vm_page_lookup() will return with the hash chain 88190507Slulf * locked, whether it was able to lookup the page or not. vm_page_free(), 89190507Slulf * vm_page_cache(), vm_page_activate(), and a number of other routines 90190507Slulf * will release the hash chain mutex for you. Intermediate manipulation 91190507Slulf * routines such as vm_page_flag_set() expect the hash chain to be held 92138112Sle * on entry and the hash chain will remain held on return. 93138112Sle * 94138112Sle * pageq scanning can only occur with the pageq in question locked. 95190507Slulf * We have a known bottleneck with the active queue, but the cache 96138112Sle * and free queues are actually arrays already. 97138112Sle */ 98138112Sle 99138112Sle/* 100190507Slulf * Resident memory management module. 101190507Slulf */ 102138112Sle 103138112Sle#include <sys/cdefs.h> 104138112Sle__FBSDID("$FreeBSD: head/sys/vm/vm_page.c 119373 2003-08-23 20:29:29Z alc $"); 105190507Slulf 106138112Sle#include <sys/param.h> 107138112Sle#include <sys/systm.h> 108138112Sle#include <sys/lock.h> 109138112Sle#include <sys/malloc.h> 110190507Slulf#include <sys/mutex.h> 111190507Slulf#include <sys/proc.h> 112138112Sle#include <sys/vmmeter.h> 113138112Sle#include <sys/vnode.h> 114138112Sle 115138112Sle#include <vm/vm.h> 116138112Sle#include <vm/vm_param.h> 117138112Sle#include <vm/vm_kern.h> 118138112Sle#include <vm/vm_object.h> 119138112Sle#include <vm/vm_page.h> 120190507Slulf#include <vm/vm_pageout.h> 121130389Sle#include <vm/vm_pager.h> 122130389Sle#include <vm/vm_extern.h> 123130389Sle#include <vm/uma.h> 124130389Sle#include <vm/uma_int.h> 125130389Sle 126130389Sle/* 127130389Sle * Associated with page of user-allocatable memory is a 128130389Sle * page structure. 129130389Sle */ 130130389Sle 131130389Slestruct mtx vm_page_queue_mtx; 132138112Slestruct mtx vm_page_queue_free_mtx; 133130389Sle 134130389Slevm_page_t vm_page_array = 0; 135190507Slulfint vm_page_array_size = 0; 136130389Slelong first_page = 0; 137190507Slulfint vm_page_zero_count = 0; 138130389Sle 139130389Sle/* 140130389Sle * vm_set_page_size: 141130389Sle * 142130389Sle * Sets the page size, perhaps based upon the memory 143130389Sle * size. Must be called before any use of page-size 144130389Sle * dependent functions. 145130389Sle */ 146135162Slevoid 147135162Slevm_set_page_size(void) 148190507Slulf{ 149135162Sle if (cnt.v_page_size == 0) 150138112Sle cnt.v_page_size = PAGE_SIZE; 151130389Sle if (((cnt.v_page_size - 1) & cnt.v_page_size) != 0) 152130389Sle panic("vm_set_page_size: page size not a power of two"); 153130389Sle} 154130389Sle 155130389Sle/* 156130389Sle * vm_page_startup: 157130389Sle * 158130389Sle * Initializes the resident memory module. 159130389Sle * 160130389Sle * Allocates memory for the page cells, and 161130389Sle * for the object/offset-to-page hash table headers. 162130389Sle * Each page cell is initialized and placed on the free list. 163130389Sle */ 164130389Slevm_offset_t 165130389Slevm_page_startup(vm_offset_t starta, vm_offset_t enda, vm_offset_t vaddr) 166130389Sle{ 167130389Sle vm_offset_t mapped; 168130389Sle vm_size_t npages; 169130389Sle vm_paddr_t page_range; 170130389Sle vm_paddr_t new_end; 171130389Sle int i; 172130389Sle vm_paddr_t pa; 173130389Sle int nblocks; 174130389Sle vm_paddr_t last_pa; 175130389Sle 176130389Sle /* the biggest memory array is the second group of pages */ 177190507Slulf vm_paddr_t end; 178130389Sle vm_paddr_t biggestsize; 179130389Sle int biggestone; 180190507Slulf 181190507Slulf vm_paddr_t total; 182190507Slulf vm_size_t bootpages; 183190507Slulf 184190507Slulf total = 0; 185190507Slulf biggestsize = 0; 186190507Slulf biggestone = 0; 187190507Slulf nblocks = 0; 188190507Slulf vaddr = round_page(vaddr); 189190507Slulf 190130389Sle for (i = 0; phys_avail[i + 1]; i += 2) { 191130389Sle phys_avail[i] = round_page(phys_avail[i]); 192130389Sle phys_avail[i + 1] = trunc_page(phys_avail[i + 1]); 193130389Sle } 194190507Slulf 195130389Sle for (i = 0; phys_avail[i + 1]; i += 2) { 196130389Sle vm_paddr_t size = phys_avail[i + 1] - phys_avail[i]; 197130389Sle 198130389Sle if (size > biggestsize) { 199130389Sle biggestone = i; 200130389Sle biggestsize = size; 201130389Sle } 202130389Sle ++nblocks; 203130389Sle total += size; 204130389Sle } 205130389Sle 206130389Sle end = phys_avail[biggestone+1]; 207130389Sle 208130389Sle /* 209130389Sle * Initialize the locks. 210130389Sle */ 211130389Sle mtx_init(&vm_page_queue_mtx, "vm page queue mutex", NULL, MTX_DEF); 212130389Sle mtx_init(&vm_page_queue_free_mtx, "vm page queue free mutex", NULL, 213130389Sle MTX_SPIN); 214130389Sle 215130389Sle /* 216130389Sle * Initialize the queue headers for the free queue, the active queue 217130389Sle * and the inactive queue. 218130389Sle */ 219130389Sle vm_pageq_init(); 220130389Sle 221130389Sle /* 222130389Sle * Allocate memory for use when boot strapping the kernel memory 223190507Slulf * allocator. 224190507Slulf */ 225138112Sle bootpages = UMA_BOOT_PAGES * UMA_SLAB_SIZE; 226190507Slulf new_end = end - bootpages; 227190507Slulf new_end = trunc_page(new_end); 228130389Sle mapped = pmap_map(&vaddr, new_end, end, 229130389Sle VM_PROT_READ | VM_PROT_WRITE); 230130389Sle bzero((caddr_t) mapped, end - new_end); 231190507Slulf uma_startup((caddr_t)mapped); 232130389Sle 233130389Sle /* 234130389Sle * Compute the number of pages of memory that will be available for 235130389Sle * use (taking into account the overhead of a page structure per 236135434Sle * page). 237135434Sle */ 238135434Sle first_page = phys_avail[0] / PAGE_SIZE; 239135434Sle page_range = phys_avail[(nblocks - 1) * 2 + 1] / PAGE_SIZE - first_page; 240135434Sle npages = (total - (page_range * sizeof(struct vm_page)) - 241130389Sle (end - new_end)) / PAGE_SIZE; 242135434Sle end = new_end; 243138112Sle 244135434Sle /* 245135434Sle * Initialize the mem entry structures now, and put them in the free 246190507Slulf * queue. 247190507Slulf */ 248190507Slulf new_end = trunc_page(end - page_range * sizeof(struct vm_page)); 249190507Slulf mapped = pmap_map(&vaddr, new_end, end, 250190507Slulf VM_PROT_READ | VM_PROT_WRITE); 251135434Sle vm_page_array = (vm_page_t) mapped; 252135434Sle phys_avail[biggestone + 1] = new_end; 253190507Slulf 254135434Sle /* 255130389Sle * Clear all of the page structures 256190507Slulf */ 257130389Sle bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page)); 258130389Sle vm_page_array_size = page_range; 259130389Sle 260130389Sle /* 261130389Sle * Construct the free queue(s) in descending order (by physical 262130389Sle * address) so that the first 16MB of physical memory is allocated 263190507Slulf * last rather than first. On large-memory machines, this avoids 264130389Sle * the exhaustion of low physical memory before isa_dmainit has run. 265130389Sle */ 266130389Sle cnt.v_page_count = 0; 267130389Sle cnt.v_free_count = 0; 268130389Sle for (i = 0; phys_avail[i + 1] && npages > 0; i += 2) { 269130389Sle pa = phys_avail[i]; 270130389Sle last_pa = phys_avail[i + 1]; 271130389Sle while (pa < last_pa && npages-- > 0) { 272130389Sle vm_pageq_add_new_page(pa); 273130389Sle pa += PAGE_SIZE; 274130389Sle } 275130389Sle } 276190507Slulf return (vaddr); 277130389Sle} 278130389Sle 279130389Slevoid 280130389Slevm_page_flag_set(vm_page_t m, unsigned short bits) 281190507Slulf{ 282190507Slulf 283190507Slulf mtx_assert(&vm_page_queue_mtx, MA_OWNED); 284190507Slulf m->flags |= bits; 285190507Slulf} 286130389Sle 287190507Slulfvoid 288190507Slulfvm_page_flag_clear(vm_page_t m, unsigned short bits) 289190507Slulf{ 290190507Slulf 291190507Slulf mtx_assert(&vm_page_queue_mtx, MA_OWNED); 292190507Slulf m->flags &= ~bits; 293190507Slulf} 294190507Slulf 295190507Slulfvoid 296190507Slulfvm_page_busy(vm_page_t m) 297190507Slulf{ 298190507Slulf KASSERT((m->flags & PG_BUSY) == 0, 299190507Slulf ("vm_page_busy: page already busy!!!")); 300190507Slulf vm_page_flag_set(m, PG_BUSY); 301190507Slulf} 302190507Slulf 303190507Slulf/* 304190507Slulf * vm_page_flash: 305190507Slulf * 306190507Slulf * wakeup anyone waiting for the page. 307190507Slulf */ 308190507Slulfvoid 309190507Slulfvm_page_flash(vm_page_t m) 310190507Slulf{ 311190507Slulf if (m->flags & PG_WANTED) { 312190507Slulf vm_page_flag_clear(m, PG_WANTED); 313190507Slulf wakeup(m); 314190507Slulf } 315190507Slulf} 316190507Slulf 317190507Slulf/* 318190507Slulf * vm_page_wakeup: 319190507Slulf * 320190507Slulf * clear the PG_BUSY flag and wakeup anyone waiting for the 321190507Slulf * page. 322190507Slulf * 323190507Slulf */ 324190507Slulfvoid 325190507Slulfvm_page_wakeup(vm_page_t m) 326190507Slulf{ 327190507Slulf KASSERT(m->flags & PG_BUSY, ("vm_page_wakeup: page not busy!!!")); 328190507Slulf vm_page_flag_clear(m, PG_BUSY); 329190507Slulf vm_page_flash(m); 330190507Slulf} 331190507Slulf 332190507Slulfvoid 333190507Slulfvm_page_io_start(vm_page_t m) 334190507Slulf{ 335190507Slulf 336190507Slulf mtx_assert(&vm_page_queue_mtx, MA_OWNED); 337190507Slulf m->busy++; 338190507Slulf} 339190507Slulf 340190507Slulfvoid 341190507Slulfvm_page_io_finish(vm_page_t m) 342190507Slulf{ 343190507Slulf 344190507Slulf mtx_assert(&vm_page_queue_mtx, MA_OWNED); 345190507Slulf m->busy--; 346190507Slulf if (m->busy == 0) 347190507Slulf vm_page_flash(m); 348190507Slulf} 349190507Slulf 350190507Slulf/* 351190507Slulf * Keep page from being freed by the page daemon 352190507Slulf * much of the same effect as wiring, except much lower 353190507Slulf * overhead and should be used only for *very* temporary 354190507Slulf * holding ("wiring"). 355190507Slulf */ 356190507Slulfvoid 357190507Slulfvm_page_hold(vm_page_t mem) 358190507Slulf{ 359190507Slulf 360190507Slulf mtx_assert(&vm_page_queue_mtx, MA_OWNED); 361190507Slulf mem->hold_count++; 362190507Slulf} 363190507Slulf 364190507Slulfvoid 365190507Slulfvm_page_unhold(vm_page_t mem) 366190507Slulf{ 367190507Slulf 368190507Slulf mtx_assert(&vm_page_queue_mtx, MA_OWNED); 369190507Slulf --mem->hold_count; 370190507Slulf KASSERT(mem->hold_count >= 0, ("vm_page_unhold: hold count < 0!!!")); 371190507Slulf if (mem->hold_count == 0 && mem->queue == PQ_HOLD) 372190507Slulf vm_page_free_toq(mem); 373130389Sle} 374130389Sle 375130389Sle/* 376130389Sle * vm_page_copy: 377130389Sle * 378140591Sle * Copy one page to another 379130389Sle */ 380130389Slevoid 381130389Slevm_page_copy(vm_page_t src_m, vm_page_t dest_m) 382130389Sle{ 383140591Sle pmap_copy_page(src_m, dest_m); 384140591Sle dest_m->valid = VM_PAGE_BITS_ALL; 385130389Sle} 386130389Sle 387190507Slulf/* 388130389Sle * vm_page_free: 389130389Sle * 390190507Slulf * Free a page 391130389Sle * 392130389Sle * The clearing of PG_ZERO is a temporary safety until the code can be 393190507Slulf * reviewed to determine that PG_ZERO is being properly cleared on 394190507Slulf * write faults or maps. PG_ZERO was previously cleared in 395190507Slulf * vm_page_alloc(). 396190507Slulf */ 397190507Slulfvoid 398190507Slulfvm_page_free(vm_page_t m) 399190507Slulf{ 400130389Sle vm_page_flag_clear(m, PG_ZERO); 401130389Sle vm_page_free_toq(m); 402140591Sle vm_page_zero_idle_wakeup(); 403184292Slulf} 404184292Slulf 405140591Sle/* 406130389Sle * vm_page_free_zero: 407130389Sle * 408130389Sle * Free a page to the zerod-pages queue 409130389Sle */ 410130389Slevoid 411130389Slevm_page_free_zero(vm_page_t m) 412130389Sle{ 413130389Sle vm_page_flag_set(m, PG_ZERO); 414130389Sle vm_page_free_toq(m); 415190507Slulf} 416130389Sle 417140591Sle/* 418130389Sle * vm_page_sleep_if_busy: 419130389Sle * 420130389Sle * Sleep and release the page queues lock if PG_BUSY is set or, 421140591Sle * if also_m_busy is TRUE, busy is non-zero. Returns TRUE if the 422140591Sle * thread slept and the page queues lock was released. 423130389Sle * Otherwise, retains the page queues lock and returns FALSE. 424130389Sle */ 425130389Sleint 426130389Slevm_page_sleep_if_busy(vm_page_t m, int also_m_busy, const char *msg) 427130389Sle{ 428130389Sle int is_object_locked; 429130389Sle 430130389Sle mtx_assert(&vm_page_queue_mtx, MA_OWNED); 431130389Sle if ((m->flags & PG_BUSY) || (also_m_busy && m->busy)) { 432130389Sle vm_page_flag_set(m, PG_WANTED | PG_REFERENCED); 433130389Sle /* 434130389Sle * Remove mtx_owned() after vm_object locking is finished. 435130389Sle */ 436130389Sle if ((is_object_locked = m->object != NULL && 437130389Sle mtx_owned(&m->object->mtx))) 438130389Sle mtx_unlock(&m->object->mtx); 439130389Sle msleep(m, &vm_page_queue_mtx, PDROP | PVM, msg, 0); 440190507Slulf if (is_object_locked) 441190507Slulf mtx_lock(&m->object->mtx); 442190507Slulf return (TRUE); 443130389Sle } 444130389Sle return (FALSE); 445130389Sle} 446130389Sle 447130389Sle/* 448130389Sle * vm_page_dirty: 449190507Slulf * 450190507Slulf * make page all dirty 451190507Slulf */ 452190507Slulfvoid 453190507Slulfvm_page_dirty(vm_page_t m) 454190507Slulf{ 455190507Slulf KASSERT(m->queue - m->pc != PQ_CACHE, 456190507Slulf ("vm_page_dirty: page in cache!")); 457190507Slulf KASSERT(m->queue - m->pc != PQ_FREE, 458140591Sle ("vm_page_dirty: page is free!")); 459184292Slulf m->dirty = VM_PAGE_BITS_ALL; 460140591Sle} 461140591Sle 462130389Sle/* 463130389Sle * vm_page_splay: 464130389Sle * 465130389Sle * Implements Sleator and Tarjan's top-down splay algorithm. Returns 466130389Sle * the vm_page containing the given pindex. If, however, that 467130389Sle * pindex is not found in the vm_object, returns a vm_page that is 468130389Sle * adjacent to the pindex, coming before or after it. 469130389Sle */ 470130389Slevm_page_t 471130389Slevm_page_splay(vm_pindex_t pindex, vm_page_t root) 472130389Sle{ 473130389Sle struct vm_page dummy; 474134014Sle vm_page_t lefttreemax, righttreemin, y; 475157292Sle 476157292Sle if (root == NULL) 477157292Sle return (root); 478157292Sle lefttreemax = righttreemin = &dummy; 479157292Sle for (;; root = y) { 480157292Sle if (pindex < root->pindex) { 481130389Sle if ((y = root->left) == NULL) 482130389Sle break; 483130389Sle if (pindex < y->pindex) { 484130389Sle /* Rotate right. */ 485130389Sle root->left = y->right; 486134014Sle y->right = root; 487134014Sle root = y; 488134014Sle if ((y = root->left) == NULL) 489134014Sle break; 490134014Sle } 491134014Sle /* Link into the new root's right tree. */ 492130389Sle righttreemin->left = root; 493130389Sle righttreemin = root; 494130389Sle } else if (pindex > root->pindex) { 495130389Sle if ((y = root->right) == NULL) 496130389Sle break; 497130389Sle if (pindex > y->pindex) { 498130389Sle /* Rotate left. */ 499130389Sle root->right = y->left; 500130389Sle y->left = root; 501130389Sle root = y; 502130389Sle if ((y = root->right) == NULL) 503130389Sle break; 504130389Sle } 505130389Sle /* Link into the new root's left tree. */ 506130389Sle lefttreemax->right = root; 507130389Sle lefttreemax = root; 508130389Sle } else 509130389Sle break; 510130389Sle } 511130389Sle /* Assemble the new root. */ 512130389Sle lefttreemax->right = root->left; 513130389Sle righttreemin->left = root->right; 514130389Sle root->left = dummy.right; 515130389Sle root->right = dummy.left; 516130389Sle return (root); 517130389Sle} 518130389Sle 519130389Sle/* 520130389Sle * vm_page_insert: [ internal use only ] 521130389Sle * 522130389Sle * Inserts the given mem entry into the object and object list. 523130389Sle * 524130389Sle * The pagetables are not updated but will presumably fault the page 525130389Sle * in if necessary, or if a kernel page the caller will at some point 526130389Sle * enter the page into the kernel's pmap. We are not allowed to block 527130389Sle * here so we *can't* do this anyway. 528130389Sle * 529130389Sle * The object and page must be locked, and must be splhigh. 530130389Sle * This routine may not block. 531130389Sle */ 532130389Slevoid 533130389Slevm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex) 534130389Sle{ 535 vm_page_t root; 536 537 if (!VM_OBJECT_LOCKED(object)) 538 GIANT_REQUIRED; 539 if (m->object != NULL) 540 panic("vm_page_insert: already inserted"); 541 542 /* 543 * Record the object/offset pair in this page 544 */ 545 m->object = object; 546 m->pindex = pindex; 547 548 /* 549 * Now link into the object's ordered list of backed pages. 550 */ 551 root = object->root; 552 if (root == NULL) { 553 m->left = NULL; 554 m->right = NULL; 555 TAILQ_INSERT_TAIL(&object->memq, m, listq); 556 } else { 557 root = vm_page_splay(pindex, root); 558 if (pindex < root->pindex) { 559 m->left = root->left; 560 m->right = root; 561 root->left = NULL; 562 TAILQ_INSERT_BEFORE(root, m, listq); 563 } else { 564 m->right = root->right; 565 m->left = root; 566 root->right = NULL; 567 TAILQ_INSERT_AFTER(&object->memq, root, m, listq); 568 } 569 } 570 object->root = m; 571 object->generation++; 572 573 /* 574 * show that the object has one more resident page. 575 */ 576 object->resident_page_count++; 577 578 /* 579 * Since we are inserting a new and possibly dirty page, 580 * update the object's OBJ_WRITEABLE and OBJ_MIGHTBEDIRTY flags. 581 */ 582 if (m->flags & PG_WRITEABLE) 583 vm_object_set_writeable_dirty(object); 584} 585 586/* 587 * vm_page_remove: 588 * NOTE: used by device pager as well -wfj 589 * 590 * Removes the given mem entry from the object/offset-page 591 * table and the object page list, but do not invalidate/terminate 592 * the backing store. 593 * 594 * The object and page must be locked, and at splhigh. 595 * The underlying pmap entry (if any) is NOT removed here. 596 * This routine may not block. 597 */ 598void 599vm_page_remove(vm_page_t m) 600{ 601 vm_object_t object; 602 vm_page_t root; 603 604 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 605 if (m->object == NULL) 606 return; 607 if (!VM_OBJECT_LOCKED(m->object)) 608 GIANT_REQUIRED; 609 if ((m->flags & PG_BUSY) == 0) { 610 panic("vm_page_remove: page not busy"); 611 } 612 613 /* 614 * Basically destroy the page. 615 */ 616 vm_page_wakeup(m); 617 618 object = m->object; 619 620 /* 621 * Now remove from the object's list of backed pages. 622 */ 623 if (m != object->root) 624 vm_page_splay(m->pindex, object->root); 625 if (m->left == NULL) 626 root = m->right; 627 else { 628 root = vm_page_splay(m->pindex, m->left); 629 root->right = m->right; 630 } 631 object->root = root; 632 TAILQ_REMOVE(&object->memq, m, listq); 633 634 /* 635 * And show that the object has one fewer resident page. 636 */ 637 object->resident_page_count--; 638 object->generation++; 639 640 m->object = NULL; 641} 642 643/* 644 * vm_page_lookup: 645 * 646 * Returns the page associated with the object/offset 647 * pair specified; if none is found, NULL is returned. 648 * 649 * The object must be locked. 650 * This routine may not block. 651 * This is a critical path routine 652 */ 653vm_page_t 654vm_page_lookup(vm_object_t object, vm_pindex_t pindex) 655{ 656 vm_page_t m; 657 658 if (!VM_OBJECT_LOCKED(object)) 659 GIANT_REQUIRED; 660 m = vm_page_splay(pindex, object->root); 661 if ((object->root = m) != NULL && m->pindex != pindex) 662 m = NULL; 663 return (m); 664} 665 666/* 667 * vm_page_rename: 668 * 669 * Move the given memory entry from its 670 * current object to the specified target object/offset. 671 * 672 * The object must be locked. 673 * This routine may not block. 674 * 675 * Note: this routine will raise itself to splvm(), the caller need not. 676 * 677 * Note: swap associated with the page must be invalidated by the move. We 678 * have to do this for several reasons: (1) we aren't freeing the 679 * page, (2) we are dirtying the page, (3) the VM system is probably 680 * moving the page from object A to B, and will then later move 681 * the backing store from A to B and we can't have a conflict. 682 * 683 * Note: we *always* dirty the page. It is necessary both for the 684 * fact that we moved it, and because we may be invalidating 685 * swap. If the page is on the cache, we have to deactivate it 686 * or vm_page_dirty() will panic. Dirty pages are not allowed 687 * on the cache. 688 */ 689void 690vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex) 691{ 692 int s; 693 694 s = splvm(); 695 vm_page_remove(m); 696 vm_page_insert(m, new_object, new_pindex); 697 if (m->queue - m->pc == PQ_CACHE) 698 vm_page_deactivate(m); 699 vm_page_dirty(m); 700 splx(s); 701} 702 703/* 704 * vm_page_select_cache: 705 * 706 * Find a page on the cache queue with color optimization. As pages 707 * might be found, but not applicable, they are deactivated. This 708 * keeps us from using potentially busy cached pages. 709 * 710 * This routine must be called at splvm(). 711 * This routine may not block. 712 */ 713static vm_page_t 714vm_page_select_cache(int color) 715{ 716 vm_page_t m; 717 718 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 719 while (TRUE) { 720 m = vm_pageq_find(PQ_CACHE, color, FALSE); 721 if (m && ((m->flags & (PG_BUSY|PG_UNMANAGED)) || m->busy || 722 m->hold_count || m->wire_count || 723 (!VM_OBJECT_TRYLOCK(m->object) && 724 !VM_OBJECT_LOCKED(m->object)))) { 725 vm_page_deactivate(m); 726 continue; 727 } 728 return m; 729 } 730} 731 732/* 733 * vm_page_alloc: 734 * 735 * Allocate and return a memory cell associated 736 * with this VM object/offset pair. 737 * 738 * page_req classes: 739 * VM_ALLOC_NORMAL normal process request 740 * VM_ALLOC_SYSTEM system *really* needs a page 741 * VM_ALLOC_INTERRUPT interrupt time request 742 * VM_ALLOC_ZERO zero page 743 * 744 * This routine may not block. 745 * 746 * Additional special handling is required when called from an 747 * interrupt (VM_ALLOC_INTERRUPT). We are not allowed to mess with 748 * the page cache in this case. 749 */ 750vm_page_t 751vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req) 752{ 753 vm_object_t m_object; 754 vm_page_t m = NULL; 755 int color, flags, page_req, s; 756 757 page_req = req & VM_ALLOC_CLASS_MASK; 758 759 if ((req & VM_ALLOC_NOOBJ) == 0) { 760 KASSERT(object != NULL, 761 ("vm_page_alloc: NULL object.")); 762 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 763 KASSERT(!vm_page_lookup(object, pindex), 764 ("vm_page_alloc: page already allocated")); 765 color = (pindex + object->pg_color) & PQ_L2_MASK; 766 } else 767 color = pindex & PQ_L2_MASK; 768 769 /* 770 * The pager is allowed to eat deeper into the free page list. 771 */ 772 if ((curproc == pageproc) && (page_req != VM_ALLOC_INTERRUPT)) { 773 page_req = VM_ALLOC_SYSTEM; 774 }; 775 776 s = splvm(); 777loop: 778 mtx_lock_spin(&vm_page_queue_free_mtx); 779 if (cnt.v_free_count > cnt.v_free_reserved || 780 (page_req == VM_ALLOC_SYSTEM && 781 cnt.v_cache_count == 0 && 782 cnt.v_free_count > cnt.v_interrupt_free_min) || 783 (page_req == VM_ALLOC_INTERRUPT && cnt.v_free_count > 0)) { 784 /* 785 * Allocate from the free queue if the number of free pages 786 * exceeds the minimum for the request class. 787 */ 788 m = vm_pageq_find(PQ_FREE, color, (req & VM_ALLOC_ZERO) != 0); 789 } else if (page_req != VM_ALLOC_INTERRUPT) { 790 mtx_unlock_spin(&vm_page_queue_free_mtx); 791 /* 792 * Allocatable from cache (non-interrupt only). On success, 793 * we must free the page and try again, thus ensuring that 794 * cnt.v_*_free_min counters are replenished. 795 */ 796 vm_page_lock_queues(); 797 if ((m = vm_page_select_cache(color)) == NULL) { 798 vm_page_unlock_queues(); 799 splx(s); 800#if defined(DIAGNOSTIC) 801 if (cnt.v_cache_count > 0) 802 printf("vm_page_alloc(NORMAL): missing pages on cache queue: %d\n", cnt.v_cache_count); 803#endif 804 atomic_add_int(&vm_pageout_deficit, 1); 805 pagedaemon_wakeup(); 806 return (NULL); 807 } 808 KASSERT(m->dirty == 0, ("Found dirty cache page %p", m)); 809 m_object = m->object; 810 VM_OBJECT_LOCK_ASSERT(m_object, MA_OWNED); 811 vm_page_busy(m); 812 pmap_remove_all(m); 813 vm_page_free(m); 814 vm_page_unlock_queues(); 815 if (m_object != object) 816 VM_OBJECT_UNLOCK(m_object); 817 goto loop; 818 } else { 819 /* 820 * Not allocatable from cache from interrupt, give up. 821 */ 822 mtx_unlock_spin(&vm_page_queue_free_mtx); 823 splx(s); 824 atomic_add_int(&vm_pageout_deficit, 1); 825 pagedaemon_wakeup(); 826 return (NULL); 827 } 828 829 /* 830 * At this point we had better have found a good page. 831 */ 832 833 KASSERT( 834 m != NULL, 835 ("vm_page_alloc(): missing page on free queue\n") 836 ); 837 838 /* 839 * Remove from free queue 840 */ 841 842 vm_pageq_remove_nowakeup(m); 843 844 /* 845 * Initialize structure. Only the PG_ZERO flag is inherited. 846 */ 847 flags = PG_BUSY; 848 if (m->flags & PG_ZERO) { 849 vm_page_zero_count--; 850 if (req & VM_ALLOC_ZERO) 851 flags = PG_ZERO | PG_BUSY; 852 } 853 m->flags = flags; 854 if (req & VM_ALLOC_WIRED) { 855 atomic_add_int(&cnt.v_wire_count, 1); 856 m->wire_count = 1; 857 } else 858 m->wire_count = 0; 859 m->hold_count = 0; 860 m->act_count = 0; 861 m->busy = 0; 862 m->valid = 0; 863 KASSERT(m->dirty == 0, ("vm_page_alloc: free/cache page %p was dirty", m)); 864 mtx_unlock_spin(&vm_page_queue_free_mtx); 865 866 /* 867 * vm_page_insert() is safe prior to the splx(). Note also that 868 * inserting a page here does not insert it into the pmap (which 869 * could cause us to block allocating memory). We cannot block 870 * anywhere. 871 */ 872 if ((req & VM_ALLOC_NOOBJ) == 0) 873 vm_page_insert(m, object, pindex); 874 875 /* 876 * Don't wakeup too often - wakeup the pageout daemon when 877 * we would be nearly out of memory. 878 */ 879 if (vm_paging_needed()) 880 pagedaemon_wakeup(); 881 882 splx(s); 883 return (m); 884} 885 886/* 887 * vm_wait: (also see VM_WAIT macro) 888 * 889 * Block until free pages are available for allocation 890 * - Called in various places before memory allocations. 891 */ 892void 893vm_wait(void) 894{ 895 int s; 896 897 s = splvm(); 898 vm_page_lock_queues(); 899 if (curproc == pageproc) { 900 vm_pageout_pages_needed = 1; 901 msleep(&vm_pageout_pages_needed, &vm_page_queue_mtx, 902 PDROP | PSWP, "VMWait", 0); 903 } else { 904 if (!vm_pages_needed) { 905 vm_pages_needed = 1; 906 wakeup(&vm_pages_needed); 907 } 908 msleep(&cnt.v_free_count, &vm_page_queue_mtx, PDROP | PVM, 909 "vmwait", 0); 910 } 911 splx(s); 912} 913 914/* 915 * vm_waitpfault: (also see VM_WAITPFAULT macro) 916 * 917 * Block until free pages are available for allocation 918 * - Called only in vm_fault so that processes page faulting 919 * can be easily tracked. 920 * - Sleeps at a lower priority than vm_wait() so that vm_wait()ing 921 * processes will be able to grab memory first. Do not change 922 * this balance without careful testing first. 923 */ 924void 925vm_waitpfault(void) 926{ 927 int s; 928 929 s = splvm(); 930 vm_page_lock_queues(); 931 if (!vm_pages_needed) { 932 vm_pages_needed = 1; 933 wakeup(&vm_pages_needed); 934 } 935 msleep(&cnt.v_free_count, &vm_page_queue_mtx, PDROP | PUSER, 936 "pfault", 0); 937 splx(s); 938} 939 940/* 941 * vm_page_activate: 942 * 943 * Put the specified page on the active list (if appropriate). 944 * Ensure that act_count is at least ACT_INIT but do not otherwise 945 * mess with it. 946 * 947 * The page queues must be locked. 948 * This routine may not block. 949 */ 950void 951vm_page_activate(vm_page_t m) 952{ 953 int s; 954 955 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 956 s = splvm(); 957 if (m->queue != PQ_ACTIVE) { 958 if ((m->queue - m->pc) == PQ_CACHE) 959 cnt.v_reactivated++; 960 vm_pageq_remove(m); 961 if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) { 962 if (m->act_count < ACT_INIT) 963 m->act_count = ACT_INIT; 964 vm_pageq_enqueue(PQ_ACTIVE, m); 965 } 966 } else { 967 if (m->act_count < ACT_INIT) 968 m->act_count = ACT_INIT; 969 } 970 splx(s); 971} 972 973/* 974 * vm_page_free_wakeup: 975 * 976 * Helper routine for vm_page_free_toq() and vm_page_cache(). This 977 * routine is called when a page has been added to the cache or free 978 * queues. 979 * 980 * This routine may not block. 981 * This routine must be called at splvm() 982 */ 983static __inline void 984vm_page_free_wakeup(void) 985{ 986 987 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 988 /* 989 * if pageout daemon needs pages, then tell it that there are 990 * some free. 991 */ 992 if (vm_pageout_pages_needed && 993 cnt.v_cache_count + cnt.v_free_count >= cnt.v_pageout_free_min) { 994 wakeup(&vm_pageout_pages_needed); 995 vm_pageout_pages_needed = 0; 996 } 997 /* 998 * wakeup processes that are waiting on memory if we hit a 999 * high water mark. And wakeup scheduler process if we have 1000 * lots of memory. this process will swapin processes. 1001 */ 1002 if (vm_pages_needed && !vm_page_count_min()) { 1003 vm_pages_needed = 0; 1004 wakeup(&cnt.v_free_count); 1005 } 1006} 1007 1008/* 1009 * vm_page_free_toq: 1010 * 1011 * Returns the given page to the PQ_FREE list, 1012 * disassociating it with any VM object. 1013 * 1014 * Object and page must be locked prior to entry. 1015 * This routine may not block. 1016 */ 1017 1018void 1019vm_page_free_toq(vm_page_t m) 1020{ 1021 int s; 1022 struct vpgqueues *pq; 1023 vm_object_t object = m->object; 1024 1025 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1026 s = splvm(); 1027 cnt.v_tfree++; 1028 1029 if (m->busy || ((m->queue - m->pc) == PQ_FREE)) { 1030 printf( 1031 "vm_page_free: pindex(%lu), busy(%d), PG_BUSY(%d), hold(%d)\n", 1032 (u_long)m->pindex, m->busy, (m->flags & PG_BUSY) ? 1 : 0, 1033 m->hold_count); 1034 if ((m->queue - m->pc) == PQ_FREE) 1035 panic("vm_page_free: freeing free page"); 1036 else 1037 panic("vm_page_free: freeing busy page"); 1038 } 1039 1040 /* 1041 * unqueue, then remove page. Note that we cannot destroy 1042 * the page here because we do not want to call the pager's 1043 * callback routine until after we've put the page on the 1044 * appropriate free queue. 1045 */ 1046 vm_pageq_remove_nowakeup(m); 1047 vm_page_remove(m); 1048 1049 /* 1050 * If fictitious remove object association and 1051 * return, otherwise delay object association removal. 1052 */ 1053 if ((m->flags & PG_FICTITIOUS) != 0) { 1054 splx(s); 1055 return; 1056 } 1057 1058 m->valid = 0; 1059 vm_page_undirty(m); 1060 1061 if (m->wire_count != 0) { 1062 if (m->wire_count > 1) { 1063 panic("vm_page_free: invalid wire count (%d), pindex: 0x%lx", 1064 m->wire_count, (long)m->pindex); 1065 } 1066 panic("vm_page_free: freeing wired page\n"); 1067 } 1068 1069 /* 1070 * If we've exhausted the object's resident pages we want to free 1071 * it up. 1072 */ 1073 if (object && 1074 (object->type == OBJT_VNODE) && 1075 ((object->flags & OBJ_DEAD) == 0) 1076 ) { 1077 struct vnode *vp = (struct vnode *)object->handle; 1078 1079 if (vp) { 1080 VI_LOCK(vp); 1081 if (VSHOULDFREE(vp)) 1082 vfree(vp); 1083 VI_UNLOCK(vp); 1084 } 1085 } 1086 1087 /* 1088 * Clear the UNMANAGED flag when freeing an unmanaged page. 1089 */ 1090 if (m->flags & PG_UNMANAGED) { 1091 m->flags &= ~PG_UNMANAGED; 1092 } 1093 1094 if (m->hold_count != 0) { 1095 m->flags &= ~PG_ZERO; 1096 m->queue = PQ_HOLD; 1097 } else 1098 m->queue = PQ_FREE + m->pc; 1099 pq = &vm_page_queues[m->queue]; 1100 mtx_lock_spin(&vm_page_queue_free_mtx); 1101 pq->lcnt++; 1102 ++(*pq->cnt); 1103 1104 /* 1105 * Put zero'd pages on the end ( where we look for zero'd pages 1106 * first ) and non-zerod pages at the head. 1107 */ 1108 if (m->flags & PG_ZERO) { 1109 TAILQ_INSERT_TAIL(&pq->pl, m, pageq); 1110 ++vm_page_zero_count; 1111 } else { 1112 TAILQ_INSERT_HEAD(&pq->pl, m, pageq); 1113 } 1114 mtx_unlock_spin(&vm_page_queue_free_mtx); 1115 vm_page_free_wakeup(); 1116 splx(s); 1117} 1118 1119/* 1120 * vm_page_unmanage: 1121 * 1122 * Prevent PV management from being done on the page. The page is 1123 * removed from the paging queues as if it were wired, and as a 1124 * consequence of no longer being managed the pageout daemon will not 1125 * touch it (since there is no way to locate the pte mappings for the 1126 * page). madvise() calls that mess with the pmap will also no longer 1127 * operate on the page. 1128 * 1129 * Beyond that the page is still reasonably 'normal'. Freeing the page 1130 * will clear the flag. 1131 * 1132 * This routine is used by OBJT_PHYS objects - objects using unswappable 1133 * physical memory as backing store rather then swap-backed memory and 1134 * will eventually be extended to support 4MB unmanaged physical 1135 * mappings. 1136 */ 1137void 1138vm_page_unmanage(vm_page_t m) 1139{ 1140 int s; 1141 1142 s = splvm(); 1143 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1144 if ((m->flags & PG_UNMANAGED) == 0) { 1145 if (m->wire_count == 0) 1146 vm_pageq_remove(m); 1147 } 1148 vm_page_flag_set(m, PG_UNMANAGED); 1149 splx(s); 1150} 1151 1152/* 1153 * vm_page_wire: 1154 * 1155 * Mark this page as wired down by yet 1156 * another map, removing it from paging queues 1157 * as necessary. 1158 * 1159 * The page queues must be locked. 1160 * This routine may not block. 1161 */ 1162void 1163vm_page_wire(vm_page_t m) 1164{ 1165 int s; 1166 1167 /* 1168 * Only bump the wire statistics if the page is not already wired, 1169 * and only unqueue the page if it is on some queue (if it is unmanaged 1170 * it is already off the queues). 1171 */ 1172 s = splvm(); 1173 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1174 if (m->wire_count == 0) { 1175 if ((m->flags & PG_UNMANAGED) == 0) 1176 vm_pageq_remove(m); 1177 atomic_add_int(&cnt.v_wire_count, 1); 1178 } 1179 m->wire_count++; 1180 KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m)); 1181 splx(s); 1182} 1183 1184/* 1185 * vm_page_unwire: 1186 * 1187 * Release one wiring of this page, potentially 1188 * enabling it to be paged again. 1189 * 1190 * Many pages placed on the inactive queue should actually go 1191 * into the cache, but it is difficult to figure out which. What 1192 * we do instead, if the inactive target is well met, is to put 1193 * clean pages at the head of the inactive queue instead of the tail. 1194 * This will cause them to be moved to the cache more quickly and 1195 * if not actively re-referenced, freed more quickly. If we just 1196 * stick these pages at the end of the inactive queue, heavy filesystem 1197 * meta-data accesses can cause an unnecessary paging load on memory bound 1198 * processes. This optimization causes one-time-use metadata to be 1199 * reused more quickly. 1200 * 1201 * BUT, if we are in a low-memory situation we have no choice but to 1202 * put clean pages on the cache queue. 1203 * 1204 * A number of routines use vm_page_unwire() to guarantee that the page 1205 * will go into either the inactive or active queues, and will NEVER 1206 * be placed in the cache - for example, just after dirtying a page. 1207 * dirty pages in the cache are not allowed. 1208 * 1209 * The page queues must be locked. 1210 * This routine may not block. 1211 */ 1212void 1213vm_page_unwire(vm_page_t m, int activate) 1214{ 1215 int s; 1216 1217 s = splvm(); 1218 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1219 if (m->wire_count > 0) { 1220 m->wire_count--; 1221 if (m->wire_count == 0) { 1222 atomic_subtract_int(&cnt.v_wire_count, 1); 1223 if (m->flags & PG_UNMANAGED) { 1224 ; 1225 } else if (activate) 1226 vm_pageq_enqueue(PQ_ACTIVE, m); 1227 else { 1228 vm_page_flag_clear(m, PG_WINATCFLS); 1229 vm_pageq_enqueue(PQ_INACTIVE, m); 1230 } 1231 } 1232 } else { 1233 panic("vm_page_unwire: invalid wire count: %d\n", m->wire_count); 1234 } 1235 splx(s); 1236} 1237 1238 1239/* 1240 * Move the specified page to the inactive queue. If the page has 1241 * any associated swap, the swap is deallocated. 1242 * 1243 * Normally athead is 0 resulting in LRU operation. athead is set 1244 * to 1 if we want this page to be 'as if it were placed in the cache', 1245 * except without unmapping it from the process address space. 1246 * 1247 * This routine may not block. 1248 */ 1249static __inline void 1250_vm_page_deactivate(vm_page_t m, int athead) 1251{ 1252 int s; 1253 1254 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1255 /* 1256 * Ignore if already inactive. 1257 */ 1258 if (m->queue == PQ_INACTIVE) 1259 return; 1260 1261 s = splvm(); 1262 if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) { 1263 if ((m->queue - m->pc) == PQ_CACHE) 1264 cnt.v_reactivated++; 1265 vm_page_flag_clear(m, PG_WINATCFLS); 1266 vm_pageq_remove(m); 1267 if (athead) 1268 TAILQ_INSERT_HEAD(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 1269 else 1270 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 1271 m->queue = PQ_INACTIVE; 1272 vm_page_queues[PQ_INACTIVE].lcnt++; 1273 cnt.v_inactive_count++; 1274 } 1275 splx(s); 1276} 1277 1278void 1279vm_page_deactivate(vm_page_t m) 1280{ 1281 _vm_page_deactivate(m, 0); 1282} 1283 1284/* 1285 * vm_page_try_to_cache: 1286 * 1287 * Returns 0 on failure, 1 on success 1288 */ 1289int 1290vm_page_try_to_cache(vm_page_t m) 1291{ 1292 1293 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1294 if (m->dirty || m->hold_count || m->busy || m->wire_count || 1295 (m->flags & (PG_BUSY|PG_UNMANAGED))) { 1296 return (0); 1297 } 1298 vm_page_test_dirty(m); 1299 if (m->dirty) 1300 return (0); 1301 vm_page_cache(m); 1302 return (1); 1303} 1304 1305/* 1306 * vm_page_try_to_free() 1307 * 1308 * Attempt to free the page. If we cannot free it, we do nothing. 1309 * 1 is returned on success, 0 on failure. 1310 */ 1311int 1312vm_page_try_to_free(vm_page_t m) 1313{ 1314 1315 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1316 if (m->object != NULL) 1317 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1318 if (m->dirty || m->hold_count || m->busy || m->wire_count || 1319 (m->flags & (PG_BUSY|PG_UNMANAGED))) { 1320 return (0); 1321 } 1322 vm_page_test_dirty(m); 1323 if (m->dirty) 1324 return (0); 1325 vm_page_busy(m); 1326 pmap_remove_all(m); 1327 vm_page_free(m); 1328 return (1); 1329} 1330 1331/* 1332 * vm_page_cache 1333 * 1334 * Put the specified page onto the page cache queue (if appropriate). 1335 * 1336 * This routine may not block. 1337 */ 1338void 1339vm_page_cache(vm_page_t m) 1340{ 1341 int s; 1342 1343 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1344 if ((m->flags & (PG_BUSY|PG_UNMANAGED)) || m->busy || 1345 m->hold_count || m->wire_count) { 1346 printf("vm_page_cache: attempting to cache busy page\n"); 1347 return; 1348 } 1349 if ((m->queue - m->pc) == PQ_CACHE) 1350 return; 1351 1352 /* 1353 * Remove all pmaps and indicate that the page is not 1354 * writeable or mapped. 1355 */ 1356 pmap_remove_all(m); 1357 if (m->dirty != 0) { 1358 panic("vm_page_cache: caching a dirty page, pindex: %ld", 1359 (long)m->pindex); 1360 } 1361 s = splvm(); 1362 vm_pageq_remove_nowakeup(m); 1363 vm_pageq_enqueue(PQ_CACHE + m->pc, m); 1364 vm_page_free_wakeup(); 1365 splx(s); 1366} 1367 1368/* 1369 * vm_page_dontneed 1370 * 1371 * Cache, deactivate, or do nothing as appropriate. This routine 1372 * is typically used by madvise() MADV_DONTNEED. 1373 * 1374 * Generally speaking we want to move the page into the cache so 1375 * it gets reused quickly. However, this can result in a silly syndrome 1376 * due to the page recycling too quickly. Small objects will not be 1377 * fully cached. On the otherhand, if we move the page to the inactive 1378 * queue we wind up with a problem whereby very large objects 1379 * unnecessarily blow away our inactive and cache queues. 1380 * 1381 * The solution is to move the pages based on a fixed weighting. We 1382 * either leave them alone, deactivate them, or move them to the cache, 1383 * where moving them to the cache has the highest weighting. 1384 * By forcing some pages into other queues we eventually force the 1385 * system to balance the queues, potentially recovering other unrelated 1386 * space from active. The idea is to not force this to happen too 1387 * often. 1388 */ 1389void 1390vm_page_dontneed(vm_page_t m) 1391{ 1392 static int dnweight; 1393 int dnw; 1394 int head; 1395 1396 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1397 dnw = ++dnweight; 1398 1399 /* 1400 * occassionally leave the page alone 1401 */ 1402 if ((dnw & 0x01F0) == 0 || 1403 m->queue == PQ_INACTIVE || 1404 m->queue - m->pc == PQ_CACHE 1405 ) { 1406 if (m->act_count >= ACT_INIT) 1407 --m->act_count; 1408 return; 1409 } 1410 1411 if (m->dirty == 0) 1412 vm_page_test_dirty(m); 1413 1414 if (m->dirty || (dnw & 0x0070) == 0) { 1415 /* 1416 * Deactivate the page 3 times out of 32. 1417 */ 1418 head = 0; 1419 } else { 1420 /* 1421 * Cache the page 28 times out of every 32. Note that 1422 * the page is deactivated instead of cached, but placed 1423 * at the head of the queue instead of the tail. 1424 */ 1425 head = 1; 1426 } 1427 _vm_page_deactivate(m, head); 1428} 1429 1430/* 1431 * Grab a page, waiting until we are waken up due to the page 1432 * changing state. We keep on waiting, if the page continues 1433 * to be in the object. If the page doesn't exist, allocate it. 1434 * 1435 * This routine may block. 1436 */ 1437vm_page_t 1438vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags) 1439{ 1440 vm_page_t m; 1441 int s, generation; 1442 1443 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1444retrylookup: 1445 if ((m = vm_page_lookup(object, pindex)) != NULL) { 1446 vm_page_lock_queues(); 1447 if (m->busy || (m->flags & PG_BUSY)) { 1448 generation = object->generation; 1449 1450 s = splvm(); 1451 while ((object->generation == generation) && 1452 (m->busy || (m->flags & PG_BUSY))) { 1453 vm_page_flag_set(m, PG_WANTED | PG_REFERENCED); 1454 VM_OBJECT_UNLOCK(object); 1455 msleep(m, &vm_page_queue_mtx, PDROP | PVM, "pgrbwt", 0); 1456 VM_OBJECT_LOCK(object); 1457 if ((allocflags & VM_ALLOC_RETRY) == 0) { 1458 splx(s); 1459 return NULL; 1460 } 1461 vm_page_lock_queues(); 1462 } 1463 vm_page_unlock_queues(); 1464 splx(s); 1465 goto retrylookup; 1466 } else { 1467 if (allocflags & VM_ALLOC_WIRED) 1468 vm_page_wire(m); 1469 vm_page_busy(m); 1470 vm_page_unlock_queues(); 1471 return m; 1472 } 1473 } 1474 1475 m = vm_page_alloc(object, pindex, allocflags & ~VM_ALLOC_RETRY); 1476 if (m == NULL) { 1477 VM_OBJECT_UNLOCK(object); 1478 VM_WAIT; 1479 VM_OBJECT_LOCK(object); 1480 if ((allocflags & VM_ALLOC_RETRY) == 0) 1481 return NULL; 1482 goto retrylookup; 1483 } 1484 1485 return m; 1486} 1487 1488/* 1489 * Mapping function for valid bits or for dirty bits in 1490 * a page. May not block. 1491 * 1492 * Inputs are required to range within a page. 1493 */ 1494__inline int 1495vm_page_bits(int base, int size) 1496{ 1497 int first_bit; 1498 int last_bit; 1499 1500 KASSERT( 1501 base + size <= PAGE_SIZE, 1502 ("vm_page_bits: illegal base/size %d/%d", base, size) 1503 ); 1504 1505 if (size == 0) /* handle degenerate case */ 1506 return (0); 1507 1508 first_bit = base >> DEV_BSHIFT; 1509 last_bit = (base + size - 1) >> DEV_BSHIFT; 1510 1511 return ((2 << last_bit) - (1 << first_bit)); 1512} 1513 1514/* 1515 * vm_page_set_validclean: 1516 * 1517 * Sets portions of a page valid and clean. The arguments are expected 1518 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive 1519 * of any partial chunks touched by the range. The invalid portion of 1520 * such chunks will be zero'd. 1521 * 1522 * This routine may not block. 1523 * 1524 * (base + size) must be less then or equal to PAGE_SIZE. 1525 */ 1526void 1527vm_page_set_validclean(vm_page_t m, int base, int size) 1528{ 1529 int pagebits; 1530 int frag; 1531 int endoff; 1532 1533 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1534 if (size == 0) /* handle degenerate case */ 1535 return; 1536 1537 /* 1538 * If the base is not DEV_BSIZE aligned and the valid 1539 * bit is clear, we have to zero out a portion of the 1540 * first block. 1541 */ 1542 if ((frag = base & ~(DEV_BSIZE - 1)) != base && 1543 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0) 1544 pmap_zero_page_area(m, frag, base - frag); 1545 1546 /* 1547 * If the ending offset is not DEV_BSIZE aligned and the 1548 * valid bit is clear, we have to zero out a portion of 1549 * the last block. 1550 */ 1551 endoff = base + size; 1552 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff && 1553 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0) 1554 pmap_zero_page_area(m, endoff, 1555 DEV_BSIZE - (endoff & (DEV_BSIZE - 1))); 1556 1557 /* 1558 * Set valid, clear dirty bits. If validating the entire 1559 * page we can safely clear the pmap modify bit. We also 1560 * use this opportunity to clear the PG_NOSYNC flag. If a process 1561 * takes a write fault on a MAP_NOSYNC memory area the flag will 1562 * be set again. 1563 * 1564 * We set valid bits inclusive of any overlap, but we can only 1565 * clear dirty bits for DEV_BSIZE chunks that are fully within 1566 * the range. 1567 */ 1568 pagebits = vm_page_bits(base, size); 1569 m->valid |= pagebits; 1570#if 0 /* NOT YET */ 1571 if ((frag = base & (DEV_BSIZE - 1)) != 0) { 1572 frag = DEV_BSIZE - frag; 1573 base += frag; 1574 size -= frag; 1575 if (size < 0) 1576 size = 0; 1577 } 1578 pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1)); 1579#endif 1580 m->dirty &= ~pagebits; 1581 if (base == 0 && size == PAGE_SIZE) { 1582 pmap_clear_modify(m); 1583 vm_page_flag_clear(m, PG_NOSYNC); 1584 } 1585} 1586 1587#if 0 1588 1589void 1590vm_page_set_dirty(vm_page_t m, int base, int size) 1591{ 1592 m->dirty |= vm_page_bits(base, size); 1593} 1594 1595#endif 1596 1597void 1598vm_page_clear_dirty(vm_page_t m, int base, int size) 1599{ 1600 1601 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1602 m->dirty &= ~vm_page_bits(base, size); 1603} 1604 1605/* 1606 * vm_page_set_invalid: 1607 * 1608 * Invalidates DEV_BSIZE'd chunks within a page. Both the 1609 * valid and dirty bits for the effected areas are cleared. 1610 * 1611 * May not block. 1612 */ 1613void 1614vm_page_set_invalid(vm_page_t m, int base, int size) 1615{ 1616 int bits; 1617 1618 bits = vm_page_bits(base, size); 1619 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1620 m->valid &= ~bits; 1621 m->dirty &= ~bits; 1622 m->object->generation++; 1623} 1624 1625/* 1626 * vm_page_zero_invalid() 1627 * 1628 * The kernel assumes that the invalid portions of a page contain 1629 * garbage, but such pages can be mapped into memory by user code. 1630 * When this occurs, we must zero out the non-valid portions of the 1631 * page so user code sees what it expects. 1632 * 1633 * Pages are most often semi-valid when the end of a file is mapped 1634 * into memory and the file's size is not page aligned. 1635 */ 1636void 1637vm_page_zero_invalid(vm_page_t m, boolean_t setvalid) 1638{ 1639 int b; 1640 int i; 1641 1642 /* 1643 * Scan the valid bits looking for invalid sections that 1644 * must be zerod. Invalid sub-DEV_BSIZE'd areas ( where the 1645 * valid bit may be set ) have already been zerod by 1646 * vm_page_set_validclean(). 1647 */ 1648 for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) { 1649 if (i == (PAGE_SIZE / DEV_BSIZE) || 1650 (m->valid & (1 << i)) 1651 ) { 1652 if (i > b) { 1653 pmap_zero_page_area(m, 1654 b << DEV_BSHIFT, (i - b) << DEV_BSHIFT); 1655 } 1656 b = i + 1; 1657 } 1658 } 1659 1660 /* 1661 * setvalid is TRUE when we can safely set the zero'd areas 1662 * as being valid. We can do this if there are no cache consistancy 1663 * issues. e.g. it is ok to do with UFS, but not ok to do with NFS. 1664 */ 1665 if (setvalid) 1666 m->valid = VM_PAGE_BITS_ALL; 1667} 1668 1669/* 1670 * vm_page_is_valid: 1671 * 1672 * Is (partial) page valid? Note that the case where size == 0 1673 * will return FALSE in the degenerate case where the page is 1674 * entirely invalid, and TRUE otherwise. 1675 * 1676 * May not block. 1677 */ 1678int 1679vm_page_is_valid(vm_page_t m, int base, int size) 1680{ 1681 int bits = vm_page_bits(base, size); 1682 1683 if (m->valid && ((m->valid & bits) == bits)) 1684 return 1; 1685 else 1686 return 0; 1687} 1688 1689/* 1690 * update dirty bits from pmap/mmu. May not block. 1691 */ 1692void 1693vm_page_test_dirty(vm_page_t m) 1694{ 1695 if ((m->dirty != VM_PAGE_BITS_ALL) && pmap_is_modified(m)) { 1696 vm_page_dirty(m); 1697 } 1698} 1699 1700int so_zerocp_fullpage = 0; 1701 1702void 1703vm_page_cowfault(vm_page_t m) 1704{ 1705 vm_page_t mnew; 1706 vm_object_t object; 1707 vm_pindex_t pindex; 1708 1709 object = m->object; 1710 pindex = m->pindex; 1711 vm_page_busy(m); 1712 1713 retry_alloc: 1714 vm_page_remove(m); 1715 /* 1716 * An interrupt allocation is requested because the page 1717 * queues lock is held. 1718 */ 1719 mnew = vm_page_alloc(object, pindex, VM_ALLOC_INTERRUPT); 1720 if (mnew == NULL) { 1721 vm_page_insert(m, object, pindex); 1722 vm_page_unlock_queues(); 1723 VM_OBJECT_UNLOCK(object); 1724 VM_WAIT; 1725 VM_OBJECT_LOCK(object); 1726 vm_page_lock_queues(); 1727 goto retry_alloc; 1728 } 1729 1730 if (m->cow == 0) { 1731 /* 1732 * check to see if we raced with an xmit complete when 1733 * waiting to allocate a page. If so, put things back 1734 * the way they were 1735 */ 1736 vm_page_busy(mnew); 1737 vm_page_free(mnew); 1738 vm_page_insert(m, object, pindex); 1739 } else { /* clear COW & copy page */ 1740 if (so_zerocp_fullpage) { 1741 mnew->valid = VM_PAGE_BITS_ALL; 1742 } else { 1743 vm_page_copy(m, mnew); 1744 } 1745 vm_page_dirty(mnew); 1746 vm_page_flag_clear(mnew, PG_BUSY); 1747 } 1748} 1749 1750void 1751vm_page_cowclear(vm_page_t m) 1752{ 1753 1754 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1755 if (m->cow) { 1756 m->cow--; 1757 /* 1758 * let vm_fault add back write permission lazily 1759 */ 1760 } 1761 /* 1762 * sf_buf_free() will free the page, so we needn't do it here 1763 */ 1764} 1765 1766void 1767vm_page_cowsetup(vm_page_t m) 1768{ 1769 1770 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1771 m->cow++; 1772 pmap_page_protect(m, VM_PROT_READ); 1773} 1774 1775#include "opt_ddb.h" 1776#ifdef DDB 1777#include <sys/kernel.h> 1778 1779#include <ddb/ddb.h> 1780 1781DB_SHOW_COMMAND(page, vm_page_print_page_info) 1782{ 1783 db_printf("cnt.v_free_count: %d\n", cnt.v_free_count); 1784 db_printf("cnt.v_cache_count: %d\n", cnt.v_cache_count); 1785 db_printf("cnt.v_inactive_count: %d\n", cnt.v_inactive_count); 1786 db_printf("cnt.v_active_count: %d\n", cnt.v_active_count); 1787 db_printf("cnt.v_wire_count: %d\n", cnt.v_wire_count); 1788 db_printf("cnt.v_free_reserved: %d\n", cnt.v_free_reserved); 1789 db_printf("cnt.v_free_min: %d\n", cnt.v_free_min); 1790 db_printf("cnt.v_free_target: %d\n", cnt.v_free_target); 1791 db_printf("cnt.v_cache_min: %d\n", cnt.v_cache_min); 1792 db_printf("cnt.v_inactive_target: %d\n", cnt.v_inactive_target); 1793} 1794 1795DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info) 1796{ 1797 int i; 1798 db_printf("PQ_FREE:"); 1799 for (i = 0; i < PQ_L2_SIZE; i++) { 1800 db_printf(" %d", vm_page_queues[PQ_FREE + i].lcnt); 1801 } 1802 db_printf("\n"); 1803 1804 db_printf("PQ_CACHE:"); 1805 for (i = 0; i < PQ_L2_SIZE; i++) { 1806 db_printf(" %d", vm_page_queues[PQ_CACHE + i].lcnt); 1807 } 1808 db_printf("\n"); 1809 1810 db_printf("PQ_ACTIVE: %d, PQ_INACTIVE: %d\n", 1811 vm_page_queues[PQ_ACTIVE].lcnt, 1812 vm_page_queues[PQ_INACTIVE].lcnt); 1813} 1814#endif /* DDB */ 1815