vm_page.c revision 169667
1154941Sjhb/*- 2154941Sjhb * Copyright (c) 1991 Regents of the University of California. 3154941Sjhb * All rights reserved. 4154941Sjhb * 5154941Sjhb * This code is derived from software contributed to Berkeley by 6154941Sjhb * The Mach Operating System project at Carnegie-Mellon University. 7154941Sjhb * 8154941Sjhb * Redistribution and use in source and binary forms, with or without 9154941Sjhb * modification, are permitted provided that the following conditions 10154941Sjhb * are met: 11154941Sjhb * 1. Redistributions of source code must retain the above copyright 12154941Sjhb * notice, this list of conditions and the following disclaimer. 13154941Sjhb * 2. Redistributions in binary form must reproduce the above copyright 14154941Sjhb * notice, this list of conditions and the following disclaimer in the 15154941Sjhb * documentation and/or other materials provided with the distribution. 16154941Sjhb * 4. Neither the name of the University nor the names of its contributors 17154941Sjhb * may be used to endorse or promote products derived from this software 18154941Sjhb * without specific prior written permission. 19154941Sjhb * 20154941Sjhb * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21154941Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22154941Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23154941Sjhb * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24154941Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25154941Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26154941Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27154941Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28154941Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29154941Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30154941Sjhb * SUCH DAMAGE. 31154941Sjhb * 32154941Sjhb * from: @(#)vm_page.c 7.4 (Berkeley) 5/7/91 33154941Sjhb */ 34154941Sjhb 35233628Sfabient/*- 36167801Sjhb * Copyright (c) 1987, 1990 Carnegie-Mellon University. 37154941Sjhb * All rights reserved. 38154941Sjhb * 39244582Sattilio * Authors: Avadis Tevanian, Jr., Michael Wayne Young 40154941Sjhb * 41177912Sjeff * Permission to use, copy, modify and distribute this software and 42154941Sjhb * its documentation is hereby granted, provided that both the copyright 43154941Sjhb * notice and this permission notice appear in all copies of the 44154941Sjhb * software, derivative works or modified versions, and any portions 45154941Sjhb * thereof, and that both notices appear in supporting documentation. 46274092Sjhb * 47177912Sjeff * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 48154941Sjhb * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 49154941Sjhb * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 50171516Sattilio * 51154941Sjhb * Carnegie Mellon requests users of this software to return to 52154941Sjhb * 53167801Sjhb * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 54167801Sjhb * School of Computer Science 55167801Sjhb * Carnegie Mellon University 56167801Sjhb * Pittsburgh PA 15213-3890 57233628Sfabient * 58233628Sfabient * any improvements or extensions that they make and grant Carnegie the 59233628Sfabient * rights to redistribute these changes. 60233628Sfabient */ 61233628Sfabient 62242515Sattilio/* 63242515Sattilio * GENERAL RULES ON VM_PAGE MANIPULATION 64242515Sattilio * 65242515Sattilio * - a pageq mutex is required when adding or removing a page from a 66242515Sattilio * page queue (vm_page_queue[]), regardless of other mutexes or the 67242515Sattilio * busy state of a page. 68177912Sjeff * 69177912Sjeff * - a hash chain mutex is required when associating or disassociating 70177912Sjeff * a page from the VM PAGE CACHE hash table (vm_page_buckets), 71227309Sed * regardless of other mutexes or the busy state of a page. 72227309Sed * 73177912Sjeff * - either a hash chain mutex OR a busied page is required in order 74177912Sjeff * to modify the page flags. A hash chain mutex must be obtained in 75177912Sjeff * order to busy a page. A page's flags cannot be modified by a 76177912Sjeff * hash chain mutex if the page is marked busy. 77154941Sjhb * 78154941Sjhb * - The object memq mutex is held when inserting or removing 79154941Sjhb * pages from an object (vm_page_insert() or vm_page_remove()). This 80227588Spjd * is different from the object's main mutex. 81154941Sjhb * 82227588Spjd * Generally speaking, you have to be aware of side effects when running 83255745Sdavide * vm_page ops. A vm_page_lookup() will return with the hash chain 84192853Ssson * locked, whether it was able to lookup the page or not. vm_page_free(), 85227588Spjd * vm_page_cache(), vm_page_activate(), and a number of other routines 86192853Ssson * will release the hash chain mutex for you. Intermediate manipulation 87255745Sdavide * routines such as vm_page_flag_set() expect the hash chain to be held 88154941Sjhb * on entry and the hash chain will remain held on return. 89154941Sjhb * 90167365Sjhb * pageq scanning can only occur with the pageq in question locked. 91167365Sjhb * We have a known bottleneck with the active queue, but the cache 92173733Sattilio * and free queues are actually arrays already. 93154941Sjhb */ 94167365Sjhb 95154941Sjhb/* 96167368Sjhb * Resident memory management module. 97167368Sjhb */ 98192853Ssson 99192853Ssson#include <sys/cdefs.h> 100192853Ssson__FBSDID("$FreeBSD: head/sys/vm/vm_page.c 169667 2007-05-18 07:10:50Z jeff $"); 101154941Sjhb 102154941Sjhb#include <sys/param.h> 103157826Sjhb#include <sys/systm.h> 104157826Sjhb#include <sys/lock.h> 105157826Sjhb#include <sys/kernel.h> 106157826Sjhb#include <sys/malloc.h> 107157826Sjhb#include <sys/mutex.h> 108154941Sjhb#include <sys/proc.h> 109154941Sjhb#include <sys/sysctl.h> 110154941Sjhb#include <sys/vmmeter.h> 111157826Sjhb#include <sys/vnode.h> 112171052Sattilio 113171052Sattilio#include <vm/vm.h> 114171052Sattilio#include <vm/vm_param.h> 115171052Sattilio#include <vm/vm_kern.h> 116171052Sattilio#include <vm/vm_object.h> 117171052Sattilio#include <vm/vm_page.h> 118171052Sattilio#include <vm/vm_pageout.h> 119171052Sattilio#include <vm/vm_pager.h> 120171052Sattilio#include <vm/vm_extern.h> 121171052Sattilio#include <vm/uma.h> 122171052Sattilio#include <vm/uma_int.h> 123157826Sjhb 124157826Sjhb#include <machine/md_var.h> 125157826Sjhb 126157826Sjhb/* 127157826Sjhb * Associated with page of user-allocatable memory is a 128157826Sjhb * page structure. 129154941Sjhb */ 130242515Sattilio 131154941Sjhbstruct mtx vm_page_queue_mtx; 132154941Sjhbstruct mtx vm_page_queue_free_mtx; 133154941Sjhb 134227588Spjdvm_page_t vm_page_array = 0; 135173733Sattilioint vm_page_array_size = 0; 136173733Sattiliolong first_page = 0; 137227588Spjdint vm_page_zero_count = 0; 138173733Sattilio 139173733Sattiliostatic int boot_pages = UMA_BOOT_PAGES; 140173733SattilioTUNABLE_INT("vm.boot_pages", &boot_pages); 141255745SdavideSYSCTL_INT(_vm, OID_AUTO, boot_pages, CTLFLAG_RD, &boot_pages, 0, 142167368Sjhb "number of pages allocated for bootstrapping the VM system"); 143167368Sjhb 144167368Sjhb/* 145167368Sjhb * vm_set_page_size: 146167368Sjhb * 147255788Sdavide * Sets the page size, perhaps based upon the memory 148255788Sdavide * size. Must be called before any use of page-size 149167368Sjhb * dependent functions. 150167368Sjhb */ 151167368Sjhbvoid 152255745Sdavidevm_set_page_size(void) 153167368Sjhb{ 154167368Sjhb if (VMCNT_GET(page_size) == 0) 155167368Sjhb VMCNT_SET(page_size, PAGE_SIZE); 156167368Sjhb if (((VMCNT_GET(page_size) - 1) & VMCNT_GET(page_size)) != 0) 157167368Sjhb panic("vm_set_page_size: page size not a power of two"); 158167368Sjhb} 159167368Sjhb 160167368Sjhb/* 161255788Sdavide * vm_page_blacklist_lookup: 162167368Sjhb * 163167368Sjhb * See if a physical address in this page has been listed 164255788Sdavide * in the blacklist tunable. Entries in the tunable are 165167368Sjhb * separated by spaces or commas. If an invalid integer is 166167368Sjhb * encountered then the rest of the string is skipped. 167167368Sjhb */ 168192853Sssonstatic int 169192853Sssonvm_page_blacklist_lookup(char *list, vm_paddr_t pa) 170227588Spjd{ 171192853Ssson vm_paddr_t bad; 172227588Spjd char *cp, *pos; 173192853Ssson 174192853Ssson for (pos = list; *pos != '\0'; pos = cp) { 175192853Ssson bad = strtoq(pos, &cp, 0); 176192853Ssson if (*cp != '\0') { 177192853Ssson if (*cp == ' ' || *cp == ',') { 178192853Ssson cp++; 179192853Ssson if (cp == pos) 180192853Ssson continue; 181167368Sjhb } else 182242515Sattilio break; 183154941Sjhb } 184242515Sattilio if (pa == trunc_page(bad)) 185171052Sattilio return (1); 186154941Sjhb } 187242515Sattilio return (0); 188242515Sattilio} 189171052Sattilio 190275751Sdchagin/* 191196334Sattilio * vm_page_startup: 192196334Sattilio * 193196334Sattilio * Initializes the resident memory module. 194171052Sattilio * 195193307Sattilio * Allocates memory for the page cells, and 196171052Sattilio * for the object/offset-to-page hash table headers. 197171052Sattilio * Each page cell is initialized and placed on the free list. 198171052Sattilio */ 199171052Sattiliovm_offset_t 200171052Sattiliovm_page_startup(vm_offset_t vaddr) 201171052Sattilio{ 202193307Sattilio vm_offset_t mapped; 203193307Sattilio vm_size_t npages; 204171052Sattilio vm_paddr_t page_range; 205171052Sattilio vm_paddr_t new_end; 206275751Sdchagin int i; 207275751Sdchagin vm_paddr_t pa; 208171052Sattilio int nblocks; 209252212Sjhb vm_paddr_t last_pa; 210154941Sjhb char *list; 211171052Sattilio 212154941Sjhb /* the biggest memory array is the second group of pages */ 213154941Sjhb vm_paddr_t end; 214154941Sjhb vm_paddr_t biggestsize; 215242515Sattilio vm_paddr_t low_water, high_water; 216154941Sjhb int biggestone; 217242515Sattilio 218154941Sjhb vm_paddr_t total; 219242515Sattilio 220242515Sattilio total = 0; 221205626Sbz biggestsize = 0; 222205626Sbz biggestone = 0; 223169394Sjhb nblocks = 0; 224167787Sjhb vaddr = round_page(vaddr); 225154941Sjhb 226154941Sjhb for (i = 0; phys_avail[i + 1]; i += 2) { 227154941Sjhb phys_avail[i] = round_page(phys_avail[i]); 228154941Sjhb phys_avail[i + 1] = trunc_page(phys_avail[i + 1]); 229154941Sjhb } 230154941Sjhb 231154941Sjhb low_water = phys_avail[0]; 232242515Sattilio high_water = phys_avail[1]; 233154941Sjhb 234154941Sjhb for (i = 0; phys_avail[i + 1]; i += 2) { 235185778Skmacy vm_paddr_t size = phys_avail[i + 1] - phys_avail[i]; 236185778Skmacy 237185778Skmacy if (size > biggestsize) { 238185778Skmacy biggestone = i; 239185778Skmacy biggestsize = size; 240242515Sattilio } 241242515Sattilio if (phys_avail[i] < low_water) 242185778Skmacy low_water = phys_avail[i]; 243185778Skmacy if (phys_avail[i + 1] > high_water) 244167024Srwatson high_water = phys_avail[i + 1]; 245242515Sattilio ++nblocks; 246167024Srwatson total += size; 247167024Srwatson } 248242515Sattilio 249167024Srwatson end = phys_avail[biggestone+1]; 250167024Srwatson 251154941Sjhb /* 252242515Sattilio * Initialize the locks. 253154941Sjhb */ 254242515Sattilio mtx_init(&vm_page_queue_mtx, "vm page queue mutex", NULL, MTX_DEF | 255154941Sjhb MTX_RECURSE); 256228424Savg mtx_init(&vm_page_queue_free_mtx, "vm page queue free mutex", NULL, 257228424Savg MTX_DEF); 258242515Sattilio 259242515Sattilio /* 260242515Sattilio * Initialize the queue headers for the free queue, the active queue 261244582Sattilio * and the inactive queue. 262240424Sattilio */ 263240424Sattilio vm_pageq_init(); 264169394Sjhb 265169394Sjhb /* 266167787Sjhb * Allocate memory for use when boot strapping the kernel memory 267182914Sjhb * allocator. 268154941Sjhb */ 269171052Sattilio new_end = end - (boot_pages * UMA_SLAB_SIZE); 270167787Sjhb new_end = trunc_page(new_end); 271286166Smarkj mapped = pmap_map(&vaddr, new_end, end, 272154941Sjhb VM_PROT_READ | VM_PROT_WRITE); 273154941Sjhb bzero((void *)mapped, end - new_end); 274177843Sattilio uma_startup((void *)mapped, boot_pages); 275242515Sattilio 276177843Sattilio#if defined(__amd64__) || defined(__i386__) 277242515Sattilio /* 278177843Sattilio * Allocate a bitmap to indicate that a random physical page 279177843Sattilio * needs to be included in a minidump. 280228424Savg * 281228424Savg * The amd64 port needs this to indicate which direct map pages 282228424Savg * need to be dumped, via calls to dump_add_page()/dump_drop_page(). 283242515Sattilio * 284242515Sattilio * However, i386 still needs this workspace internally within the 285244582Sattilio * minidump code. In theory, they are not needed on i386, but are 286240424Sattilio * included should the sf_buf code decide to use them. 287240424Sattilio */ 288177843Sattilio page_range = phys_avail[(nblocks - 1) * 2 + 1] / PAGE_SIZE; 289177843Sattilio vm_page_dump_size = round_page(roundup2(page_range, NBBY) / NBBY); 290177843Sattilio new_end -= vm_page_dump_size; 291193307Sattilio vm_page_dump = (void *)(uintptr_t)pmap_map(&vaddr, new_end, 292193307Sattilio new_end + vm_page_dump_size, VM_PROT_READ | VM_PROT_WRITE); 293177843Sattilio bzero((void *)vm_page_dump, vm_page_dump_size); 294177843Sattilio#endif 295177843Sattilio /* 296177843Sattilio * Compute the number of pages of memory that will be available for 297177843Sattilio * use (taking into account the overhead of a page structure per 298177843Sattilio * page). 299177843Sattilio */ 300177843Sattilio first_page = low_water / PAGE_SIZE; 301177843Sattilio#ifdef VM_PHYSSEG_SPARSE 302177843Sattilio page_range = 0; 303284297Savg for (i = 0; phys_avail[i + 1] != 0; i += 2) 304285704Smarkj page_range += atop(phys_avail[i + 1] - phys_avail[i]); 305285704Smarkj#elif defined(VM_PHYSSEG_DENSE) 306286166Smarkj page_range = high_water / PAGE_SIZE - first_page; 307177843Sattilio#else 308177843Sattilio#error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined." 309177843Sattilio#endif 310177843Sattilio npages = (total - (page_range * sizeof(struct vm_page)) - 311154941Sjhb (end - new_end)) / PAGE_SIZE; 312242515Sattilio end = new_end; 313154941Sjhb 314242515Sattilio /* 315154941Sjhb * Reserve an unmapped guard page to trap access to vm_page_array[-1]. 316228424Savg */ 317228424Savg vaddr += PAGE_SIZE; 318242515Sattilio 319242515Sattilio /* 320242515Sattilio * Initialize the mem entry structures now, and put them in the free 321169394Sjhb * queue. 322169394Sjhb */ 323242515Sattilio new_end = trunc_page(end - page_range * sizeof(struct vm_page)); 324167787Sjhb mapped = pmap_map(&vaddr, new_end, end, 325171052Sattilio VM_PROT_READ | VM_PROT_WRITE); 326171052Sattilio vm_page_array = (vm_page_t) mapped; 327154941Sjhb#ifdef __amd64__ 328286166Smarkj /* 329154941Sjhb * pmap_map on amd64 comes out of the direct-map, not kvm like i386, 330286166Smarkj * so the pages must be tracked for a crashdump to include this data. 331176017Sjeff * This includes the vm_page_array and the early UMA bootstrap pages. 332176017Sjeff */ 333176017Sjeff for (pa = new_end; pa < phys_avail[biggestone + 1]; pa += PAGE_SIZE) 334176017Sjeff dump_add_page(pa); 335176017Sjeff#endif 336176017Sjeff phys_avail[biggestone + 1] = new_end; 337176017Sjeff 338176017Sjeff /* 339176017Sjeff * Clear all of the page structures 340176017Sjeff */ 341176017Sjeff bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page)); 342154941Sjhb vm_page_array_size = page_range; 343154941Sjhb 344242515Sattilio /* 345154941Sjhb * This assertion tests the hypothesis that npages and total are 346242515Sattilio * redundant. XXX 347170295Sjeff */ 348167801Sjhb page_range = 0; 349157846Sjhb for (i = 0; phys_avail[i + 1] != 0; i += 2) 350177912Sjeff page_range += atop(phys_avail[i + 1] - phys_avail[i]); 351177912Sjeff KASSERT(page_range == npages, 352157851Swkoszek ("vm_page_startup: inconsistent page counts")); 353189846Sjeff 354167307Sjhb /* 355167054Skmacy * Construct the free queue(s) in descending order (by physical 356189846Sjeff * address) so that the first 16MB of physical memory is allocated 357176017Sjeff * last rather than first. On large-memory machines, this avoids 358192853Ssson * the exhaustion of low physical memory before isa_dma_init has run. 359284297Savg */ 360192853Ssson VMCNT_SET(page_count, 0); 361192853Ssson VMCNT_SET(free_count, 0); 362192853Ssson list = getenv("vm.blacklist"); 363284297Savg for (i = 0; phys_avail[i + 1] != 0; i += 2) { 364192853Ssson pa = phys_avail[i]; 365154941Sjhb last_pa = phys_avail[i + 1]; 366228424Savg while (pa < last_pa) { 367228424Savg if (list != NULL && 368228424Savg vm_page_blacklist_lookup(list, pa)) 369242515Sattilio printf("Skipping page with pa 0x%jx\n", 370242515Sattilio (uintmax_t)pa); 371244582Sattilio else 372240424Sattilio vm_pageq_add_new_page(pa); 373240424Sattilio pa += PAGE_SIZE; 374169394Sjhb } 375169394Sjhb } 376157826Sjhb freeenv(list); 377251323Sjhb return (vaddr); 378167787Sjhb} 379182914Sjhb 380154941Sjhbvoid 381284297Savgvm_page_flag_set(vm_page_t m, unsigned short bits) 382285664Smarkj{ 383284297Savg 384284297Savg mtx_assert(&vm_page_queue_mtx, MA_OWNED); 385154941Sjhb m->flags |= bits; 386154941Sjhb} 387154941Sjhb 388154941Sjhbvoid 389154941Sjhbvm_page_flag_clear(vm_page_t m, unsigned short bits) 390154941Sjhb{ 391154941Sjhb 392154941Sjhb mtx_assert(&vm_page_queue_mtx, MA_OWNED); 393154941Sjhb m->flags &= ~bits; 394154941Sjhb} 395154941Sjhb 396176017Sjeffvoid 397176017Sjeffvm_page_busy(vm_page_t m) 398154941Sjhb{ 399154941Sjhb 400176017Sjeff VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 401176017Sjeff KASSERT((m->oflags & VPO_BUSY) == 0, 402154941Sjhb ("vm_page_busy: page already busy!!!")); 403176017Sjeff m->oflags |= VPO_BUSY; 404176017Sjeff} 405167787Sjhb 406154941Sjhb/* 407154941Sjhb * vm_page_flash: 408176017Sjeff * 409176017Sjeff * wakeup anyone waiting for the page. 410154941Sjhb */ 411154941Sjhbvoid 412154941Sjhbvm_page_flash(vm_page_t m) 413154941Sjhb{ 414285706Smarkj 415285706Smarkj VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 416285706Smarkj if (m->oflags & VPO_WANTED) { 417233628Sfabient m->oflags &= ~VPO_WANTED; 418233628Sfabient wakeup(m); 419233628Sfabient } 420174629Sjeff} 421174629Sjeff 422154941Sjhb/* 423173960Sattilio * vm_page_wakeup: 424154941Sjhb * 425173960Sattilio * clear the VPO_BUSY flag and wakeup anyone waiting for the 426173960Sattilio * page. 427173960Sattilio * 428173960Sattilio */ 429176017Sjeffvoid 430176017Sjeffvm_page_wakeup(vm_page_t m) 431176017Sjeff{ 432176017Sjeff 433176017Sjeff VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 434176017Sjeff KASSERT(m->oflags & VPO_BUSY, ("vm_page_wakeup: page not busy!!!")); 435176017Sjeff m->oflags &= ~VPO_BUSY; 436274092Sjhb vm_page_flash(m); 437274092Sjhb} 438274092Sjhb 439176017Sjeffvoid 440192853Sssonvm_page_io_start(vm_page_t m) 441176017Sjeff{ 442192853Ssson 443192853Ssson VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 444192853Ssson m->busy++; 445192853Ssson} 446274092Sjhb 447274092Sjhbvoid 448176017Sjeffvm_page_io_finish(vm_page_t m) 449176017Sjeff{ 450177912Sjeff 451177912Sjeff VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 452274092Sjhb m->busy--; 453274092Sjhb if (m->busy == 0) 454274092Sjhb vm_page_flash(m); 455177912Sjeff} 456177912Sjeff 457177912Sjeff/* 458177912Sjeff * Keep page from being freed by the page daemon 459177912Sjeff * much of the same effect as wiring, except much lower 460177912Sjeff * overhead and should be used only for *very* temporary 461259509Sattilio * holding ("wiring"). 462259509Sattilio */ 463259509Sattiliovoid 464274092Sjhbvm_page_hold(vm_page_t mem) 465274092Sjhb{ 466177912Sjeff 467177912Sjeff mtx_assert(&vm_page_queue_mtx, MA_OWNED); 468173960Sattilio mem->hold_count++; 469173960Sattilio} 470173960Sattilio 471173960Sattiliovoid 472154941Sjhbvm_page_unhold(vm_page_t mem) 473176017Sjeff{ 474176017Sjeff 475176017Sjeff mtx_assert(&vm_page_queue_mtx, MA_OWNED); 476154941Sjhb --mem->hold_count; 477170295Sjeff KASSERT(mem->hold_count >= 0, ("vm_page_unhold: hold count < 0!!!")); 478154941Sjhb if (mem->hold_count == 0 && VM_PAGE_INQUEUE2(mem, PQ_HOLD)) 479154941Sjhb vm_page_free_toq(mem); 480154941Sjhb} 481176017Sjeff 482154941Sjhb/* 483176017Sjeff * vm_page_free: 484176017Sjeff * 485170295Sjeff * Free a page. 486154941Sjhb */ 487154941Sjhbvoid 488154941Sjhbvm_page_free(vm_page_t m) 489173960Sattilio{ 490154941Sjhb 491193035Sjhb m->flags &= ~PG_ZERO; 492193035Sjhb vm_page_free_toq(m); 493193035Sjhb} 494193035Sjhb 495193035Sjhb/* 496173960Sattilio * vm_page_free_zero: 497176017Sjeff * 498176017Sjeff * Free a page to the zerod-pages queue 499176017Sjeff */ 500176017Sjeffvoid 501176017Sjeffvm_page_free_zero(vm_page_t m) 502176017Sjeff{ 503173960Sattilio 504173960Sattilio m->flags |= PG_ZERO; 505173960Sattilio vm_page_free_toq(m); 506173960Sattilio} 507176017Sjeff 508154941Sjhb/* 509176017Sjeff * vm_page_sleep: 510176017Sjeff * 511176017Sjeff * Sleep and release the page queues lock. 512176017Sjeff * 513176017Sjeff * The object containing the given page must be locked. 514176017Sjeff */ 515176017Sjeffvoid 516176017Sjeffvm_page_sleep(vm_page_t m, const char *msg) 517176017Sjeff{ 518176017Sjeff 519176017Sjeff VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 520170295Sjeff if (!mtx_owned(&vm_page_queue_mtx)) 521157826Sjhb vm_page_lock_queues(); 522157826Sjhb vm_page_flag_set(m, PG_REFERENCED); 523167787Sjhb vm_page_unlock_queues(); 524157826Sjhb 525157826Sjhb /* 526154941Sjhb * It's possible that while we sleep, the page will get 527154941Sjhb * unbusied and freed. If we are holding the object 528154941Sjhb * lock, we will assume we hold a reference to the object 529154941Sjhb * such that even if m->object changes, we can re-lock 530154941Sjhb * it. 531154941Sjhb */ 532167787Sjhb m->oflags |= VPO_WANTED; 533154941Sjhb msleep(m, VM_OBJECT_MTX(m->object), PVM, msg, 0); 534154941Sjhb} 535192853Ssson 536285664Smarkj/* 537192853Ssson * vm_page_dirty: 538170295Sjeff * 539192853Ssson * make page all dirty 540285664Smarkj */ 541192853Sssonvoid 542192853Sssonvm_page_dirty(vm_page_t m) 543167787Sjhb{ 544154941Sjhb KASSERT(VM_PAGE_GETKNOWNQUEUE1(m) != PQ_CACHE, 545154941Sjhb ("vm_page_dirty: page in cache!")); 546154941Sjhb KASSERT(VM_PAGE_GETKNOWNQUEUE1(m) != PQ_FREE, 547284297Savg ("vm_page_dirty: page is free!")); 548285664Smarkj m->dirty = VM_PAGE_BITS_ALL; 549284297Savg} 550285703Smarkj 551284297Savg/* 552284297Savg * vm_page_splay: 553154941Sjhb * 554284297Savg * Implements Sleator and Tarjan's top-down splay algorithm. Returns 555284297Savg * the vm_page containing the given pindex. If, however, that 556285703Smarkj * pindex is not found in the vm_object, returns a vm_page that is 557284297Savg * adjacent to the pindex, coming before or after it. 558284297Savg */ 559284297Savgvm_page_t 560154941Sjhbvm_page_splay(vm_pindex_t pindex, vm_page_t root) 561154941Sjhb{ 562154941Sjhb struct vm_page dummy; 563154941Sjhb vm_page_t lefttreemax, righttreemin, y; 564154941Sjhb 565285704Smarkj if (root == NULL) 566285704Smarkj return (root); 567167787Sjhb lefttreemax = righttreemin = &dummy; 568167787Sjhb for (;; root = y) { 569286166Smarkj if (pindex < root->pindex) { 570176017Sjeff if ((y = root->left) == NULL) 571154941Sjhb break; 572154941Sjhb if (pindex < y->pindex) { 573177843Sattilio /* Rotate right. */ 574242515Sattilio root->left = y->right; 575177843Sattilio y->right = root; 576242515Sattilio root = y; 577177843Sattilio if ((y = root->left) == NULL) 578177843Sattilio break; 579228424Savg } 580228424Savg /* Link into the new root's right tree. */ 581228424Savg righttreemin->left = root; 582242515Sattilio righttreemin = root; 583242515Sattilio } else if (pindex > root->pindex) { 584244582Sattilio if ((y = root->right) == NULL) 585240424Sattilio break; 586240424Sattilio if (pindex > y->pindex) { 587240424Sattilio /* Rotate left. */ 588177843Sattilio root->right = y->left; 589177843Sattilio y->left = root; 590177843Sattilio root = y; 591177843Sattilio if ((y = root->right) == NULL) 592177843Sattilio break; 593177843Sattilio } 594177843Sattilio /* Link into the new root's left tree. */ 595177843Sattilio lefttreemax->right = root; 596177843Sattilio lefttreemax = root; 597177843Sattilio } else 598285704Smarkj break; 599285704Smarkj } 600286166Smarkj /* Assemble the new root. */ 601177843Sattilio lefttreemax->right = root->left; 602177843Sattilio righttreemin->left = root->right; 603177843Sattilio root->left = dummy.right; 604177843Sattilio root->right = dummy.left; 605177843Sattilio return (root); 606177843Sattilio} 607177843Sattilio 608177843Sattilio/* 609177843Sattilio * vm_page_insert: [ internal use only ] 610154941Sjhb * 611242515Sattilio * Inserts the given mem entry into the object and object list. 612154941Sjhb * 613242515Sattilio * The pagetables are not updated but will presumably fault the page 614154941Sjhb * in if necessary, or if a kernel page the caller will at some point 615176017Sjeff * enter the page into the kernel's pmap. We are not allowed to block 616154941Sjhb * here so we *can't* do this anyway. 617228424Savg * 618228424Savg * The object and page must be locked. 619228424Savg * This routine may not block. 620242515Sattilio */ 621242515Sattiliovoid 622169394Sjhbvm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex) 623169394Sjhb{ 624242515Sattilio vm_page_t root; 625167787Sjhb 626167787Sjhb VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 627154941Sjhb if (m->object != NULL) 628154941Sjhb panic("vm_page_insert: page already inserted"); 629154941Sjhb 630154941Sjhb /* 631154941Sjhb * Record the object/offset pair in this page 632154941Sjhb */ 633154941Sjhb m->object = object; 634154941Sjhb m->pindex = pindex; 635154941Sjhb 636154941Sjhb /* 637197643Sattilio * Now link into the object's ordered list of backed pages. 638154941Sjhb */ 639167787Sjhb root = object->root; 640154941Sjhb if (root == NULL) { 641154941Sjhb m->left = NULL; 642154941Sjhb m->right = NULL; 643154941Sjhb TAILQ_INSERT_TAIL(&object->memq, m, listq); 644154941Sjhb } else { 645154941Sjhb root = vm_page_splay(pindex, root); 646154941Sjhb if (pindex < root->pindex) { 647167307Sjhb m->left = root->left; 648154941Sjhb m->right = root; 649154941Sjhb root->left = NULL; 650154941Sjhb TAILQ_INSERT_BEFORE(root, m, listq); 651154941Sjhb } else if (pindex == root->pindex) 652176017Sjeff panic("vm_page_insert: offset already allocated"); 653176017Sjeff else { 654176017Sjeff m->right = root->right; 655197643Sattilio m->left = root; 656197643Sattilio root->right = NULL; 657167787Sjhb TAILQ_INSERT_AFTER(&object->memq, root, m, listq); 658154941Sjhb } 659154941Sjhb } 660154941Sjhb object->root = m; 661154941Sjhb object->generation++; 662154941Sjhb 663154941Sjhb /* 664154941Sjhb * show that the object has one more resident page. 665176017Sjeff */ 666176017Sjeff object->resident_page_count++; 667154941Sjhb /* 668170295Sjeff * Hold the vnode until the last page is released. 669176017Sjeff */ 670176017Sjeff if (object->resident_page_count == 1 && object->type == OBJT_VNODE) 671154941Sjhb vhold((struct vnode *)object->handle); 672154941Sjhb 673154941Sjhb /* 674154941Sjhb * Since we are inserting a new and possibly dirty page, 675154941Sjhb * update the object's OBJ_MIGHTBEDIRTY flag. 676154941Sjhb */ 677154941Sjhb if (m->flags & PG_WRITEABLE) 678154941Sjhb vm_object_set_writeable_dirty(object); 679154941Sjhb} 680154941Sjhb 681154941Sjhb/* 682154941Sjhb * vm_page_remove: 683154941Sjhb * NOTE: used by device pager as well -wfj 684154941Sjhb * 685154941Sjhb * Removes the given mem entry from the object/offset-page 686154941Sjhb * table and the object page list, but do not invalidate/terminate 687154941Sjhb * the backing store. 688176017Sjeff * 689176017Sjeff * The object and page must be locked. 690176017Sjeff * The underlying pmap entry (if any) is NOT removed here. 691176017Sjeff * This routine may not block. 692176017Sjeff */ 693176017Sjeffvoid 694197643Sattiliovm_page_remove(vm_page_t m) 695176017Sjeff{ 696170295Sjeff vm_object_t object; 697154941Sjhb vm_page_t root; 698154941Sjhb 699167787Sjhb if ((object = m->object) == NULL) 700154941Sjhb return; 701154941Sjhb VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 702154941Sjhb if (m->oflags & VPO_BUSY) { 703154941Sjhb m->oflags &= ~VPO_BUSY; 704154941Sjhb vm_page_flash(m); 705154941Sjhb } 706154941Sjhb mtx_assert(&vm_page_queue_mtx, MA_OWNED); 707154941Sjhb 708154941Sjhb /* 709154941Sjhb * Now remove from the object's list of backed pages. 710167787Sjhb */ 711157846Sjhb if (m != object->root) 712176017Sjeff vm_page_splay(m->pindex, object->root); 713154941Sjhb if (m->left == NULL) 714170295Sjeff root = m->right; 715154941Sjhb else { 716154941Sjhb root = vm_page_splay(m->pindex, m->left); 717285704Smarkj root->right = m->right; 718286166Smarkj } 719252212Sjhb object->root = root; 720154941Sjhb TAILQ_REMOVE(&object->memq, m, listq); 721154941Sjhb 722154941Sjhb /* 723154941Sjhb * And show that the object has one fewer resident page. 724154941Sjhb */ 725154941Sjhb object->resident_page_count--; 726154941Sjhb object->generation++; 727154941Sjhb /* 728242515Sattilio * The vnode may now be recycled. 729242515Sattilio */ 730154941Sjhb if (object->resident_page_count == 0 && object->type == OBJT_VNODE) 731242515Sattilio vdrop((struct vnode *)object->handle); 732170295Sjeff 733167801Sjhb m->object = NULL; 734157846Sjhb} 735176017Sjeff 736176017Sjeff/* 737157851Swkoszek * vm_page_lookup: 738189846Sjeff * 739189846Sjeff * Returns the page associated with the object/offset 740171516Sattilio * pair specified; if none is found, NULL is returned. 741171516Sattilio * 742189846Sjeff * The object must be locked. 743192853Ssson * This routine may not block. 744284297Savg * This is a critical path routine 745192853Ssson */ 746192853Sssonvm_page_t 747192853Sssonvm_page_lookup(vm_object_t object, vm_pindex_t pindex) 748284297Savg{ 749192853Ssson vm_page_t m; 750154941Sjhb 751228424Savg VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 752228424Savg if ((m = object->root) != NULL && m->pindex != pindex) { 753228424Savg m = vm_page_splay(pindex, m); 754242515Sattilio if ((object->root = m)->pindex != pindex) 755242515Sattilio m = NULL; 756171052Sattilio } 757193307Sattilio return (m); 758171052Sattilio} 759171052Sattilio 760171052Sattilio/* 761171052Sattilio * vm_page_rename: 762171052Sattilio * 763171052Sattilio * Move the given memory entry from its 764171052Sattilio * current object to the specified target object/offset. 765171052Sattilio * 766167787Sjhb * The object must be locked. 767154941Sjhb * This routine may not block. 768167787Sjhb * 769154941Sjhb * Note: swap associated with the page must be invalidated by the move. We 770284297Savg * have to do this for several reasons: (1) we aren't freeing the 771285664Smarkj * page, (2) we are dirtying the page, (3) the VM system is probably 772284297Savg * moving the page from object A to B, and will then later move 773284297Savg * the backing store from A to B and we can't have a conflict. 774301157Smjg * 775301157Smjg * Note: we *always* dirty the page. It is necessary both for the 776301157Smjg * fact that we moved it, and because we may be invalidating 777192853Ssson * swap. If the page is on the cache, we have to deactivate it 778192853Ssson * or vm_page_dirty() will panic. Dirty pages are not allowed 779192853Ssson * on the cache. 780233628Sfabient */ 781233628Sfabientvoid 782233628Sfabientvm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex) 783174629Sjeff{ 784174629Sjeff 785173960Sattilio vm_page_remove(m); 786173960Sattilio vm_page_insert(m, new_object, new_pindex); 787173960Sattilio if (VM_PAGE_INQUEUE1(m, PQ_CACHE)) 788173960Sattilio vm_page_deactivate(m); 789173960Sattilio vm_page_dirty(m); 790173960Sattilio} 791173960Sattilio 792173960Sattilio/* 793173960Sattilio * vm_page_select_cache: 794173960Sattilio * 795173960Sattilio * Move a page of the given color from the cache queue to the free 796173960Sattilio * queue. As pages might be found, but are not applicable, they are 797274092Sjhb * deactivated. 798274092Sjhb * 799274092Sjhb * This routine may not block. 800173960Sattilio */ 801192853Sssonvm_page_t 802173960Sattiliovm_page_select_cache(int color) 803192853Ssson{ 804192853Ssson vm_object_t object; 805192853Ssson vm_page_t m; 806192853Ssson boolean_t was_trylocked; 807274092Sjhb 808274092Sjhb mtx_assert(&vm_page_queue_mtx, MA_OWNED); 809173960Sattilio while ((m = vm_pageq_find(PQ_CACHE, color, FALSE)) != NULL) { 810173960Sattilio KASSERT(m->dirty == 0, ("Found dirty cache page %p", m)); 811177912Sjeff KASSERT(!pmap_page_is_mapped(m), 812177912Sjeff ("Found mapped cache page %p", m)); 813176017Sjeff KASSERT((m->flags & PG_UNMANAGED) == 0, 814176017Sjeff ("Found unmanaged cache page %p", m)); 815176017Sjeff KASSERT(m->wire_count == 0, ("Found wired cache page %p", m)); 816176017Sjeff if (m->hold_count == 0 && (object = m->object, 817176017Sjeff (was_trylocked = VM_OBJECT_TRYLOCK(object)) || 818176017Sjeff VM_OBJECT_LOCKED(object))) { 819176017Sjeff KASSERT((m->oflags & VPO_BUSY) == 0 && m->busy == 0, 820274092Sjhb ("Found busy cache page %p", m)); 821274092Sjhb vm_page_free(m); 822274092Sjhb if (was_trylocked) 823177912Sjeff VM_OBJECT_UNLOCK(object); 824176017Sjeff break; 825176017Sjeff } 826176017Sjeff vm_page_deactivate(m); 827176017Sjeff } 828274092Sjhb return (m); 829274092Sjhb} 830192853Ssson 831192853Ssson/* 832192853Ssson * vm_page_alloc: 833177912Sjeff * 834176017Sjeff * Allocate and return a memory cell associated 835176017Sjeff * with this VM object/offset pair. 836173960Sattilio * 837170295Sjeff * page_req classes: 838154941Sjhb * VM_ALLOC_NORMAL normal process request 839154941Sjhb * VM_ALLOC_SYSTEM system *really* needs a page 840173960Sattilio * VM_ALLOC_INTERRUPT interrupt time request 841154941Sjhb * VM_ALLOC_ZERO zero page 842193035Sjhb * 843193035Sjhb * This routine may not block. 844193035Sjhb * 845193035Sjhb * Additional special handling is required when called from an 846193035Sjhb * interrupt (VM_ALLOC_INTERRUPT). We are not allowed to mess with 847173960Sattilio * the page cache in this case. 848173960Sattilio */ 849173960Sattiliovm_page_t 850173960Sattiliovm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req) 851173960Sattilio{ 852173960Sattilio vm_page_t m = NULL; 853173960Sattilio int color, flags, page_req; 854173960Sattilio 855173960Sattilio page_req = req & VM_ALLOC_CLASS_MASK; 856173960Sattilio KASSERT(curthread->td_intr_nesting_level == 0 || 857179334Sattilio page_req == VM_ALLOC_INTERRUPT, 858179334Sattilio ("vm_page_alloc(NORMAL|SYSTEM) in interrupt context")); 859179334Sattilio 860179334Sattilio if ((req & VM_ALLOC_NOOBJ) == 0) { 861179334Sattilio KASSERT(object != NULL, 862154941Sjhb ("vm_page_alloc: NULL object.")); 863176017Sjeff VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 864176017Sjeff color = (pindex + object->pg_color) & PQ_COLORMASK; 865176017Sjeff } else 866176017Sjeff color = pindex & PQ_COLORMASK; 867176017Sjeff 868176017Sjeff /* 869176017Sjeff * The pager is allowed to eat deeper into the free page list. 870176017Sjeff */ 871154941Sjhb if ((curproc == pageproc) && (page_req != VM_ALLOC_INTERRUPT)) { 872154941Sjhb page_req = VM_ALLOC_SYSTEM; 873170295Sjeff }; 874154941Sjhb 875154941Sjhbloop: 876154941Sjhb mtx_lock(&vm_page_queue_free_mtx); 877154941Sjhb if (VMCNT_GET(free_count) > VMCNT_GET(free_reserved) || 878154941Sjhb (page_req == VM_ALLOC_SYSTEM && 879154941Sjhb VMCNT_GET(cache_count) == 0 && 880154941Sjhb VMCNT_GET(free_count) > VMCNT_GET(interrupt_free_min)) || 881157826Sjhb (page_req == VM_ALLOC_INTERRUPT && VMCNT_GET(free_count) > 0)) { 882157826Sjhb /* 883157826Sjhb * Allocate from the free queue if the number of free pages 884170295Sjeff * exceeds the minimum for the request class. 885157826Sjhb */ 886157826Sjhb m = vm_pageq_find(PQ_FREE, color, (req & VM_ALLOC_ZERO) != 0); 887167787Sjhb } else if (page_req != VM_ALLOC_INTERRUPT) { 888157826Sjhb mtx_unlock(&vm_page_queue_free_mtx); 889157826Sjhb /* 890154941Sjhb * Allocatable from cache (non-interrupt only). On success, 891157846Sjhb * we must free the page and try again, thus ensuring that 892154941Sjhb * cnt.v_*_free_min counters are replenished. 893154941Sjhb */ 894154941Sjhb vm_page_lock_queues(); 895167787Sjhb if ((m = vm_page_select_cache(color)) == NULL) { 896154941Sjhb KASSERT(VMCNT_GET(cache_count) == 0, 897154941Sjhb ("vm_page_alloc: cache queue is missing %d pages", 898192853Ssson VMCNT_GET(cache_count))); 899285664Smarkj vm_page_unlock_queues(); 900192853Ssson atomic_add_int(&vm_pageout_deficit, 1); 901170295Sjeff pagedaemon_wakeup(); 902192853Ssson 903285664Smarkj if (page_req != VM_ALLOC_SYSTEM) 904192853Ssson return (NULL); 905192853Ssson 906167787Sjhb mtx_lock(&vm_page_queue_free_mtx); 907154941Sjhb if (VMCNT_GET(free_count) <= 908154941Sjhb VMCNT_GET(interrupt_free_min)) { 909176017Sjeff mtx_unlock(&vm_page_queue_free_mtx); 910176017Sjeff return (NULL); 911176017Sjeff } 912154941Sjhb m = vm_pageq_find(PQ_FREE, color, (req & VM_ALLOC_ZERO) != 0); 913192853Ssson } else { 914285664Smarkj vm_page_unlock_queues(); 915192853Ssson goto loop; 916285703Smarkj } 917284297Savg } else { 918284297Savg /* 919192853Ssson * Not allocatable from cache from interrupt, give up. 920284297Savg */ 921192853Ssson mtx_unlock(&vm_page_queue_free_mtx); 922285703Smarkj atomic_add_int(&vm_pageout_deficit, 1); 923284297Savg pagedaemon_wakeup(); 924284297Savg return (NULL); 925192853Ssson } 926285704Smarkj 927285704Smarkj /* 928154941Sjhb * At this point we had better have found a good page. 929154941Sjhb */ 930154941Sjhb 931154941Sjhb KASSERT( 932154941Sjhb m != NULL, 933154941Sjhb ("vm_page_alloc(): missing page on free queue") 934154941Sjhb ); 935154941Sjhb 936242515Sattilio /* 937242515Sattilio * Remove from free queue 938154941Sjhb */ 939242515Sattilio vm_pageq_remove_nowakeup(m); 940154941Sjhb 941154941Sjhb /* 942154941Sjhb * Initialize structure. Only the PG_ZERO flag is inherited. 943154941Sjhb */ 944228424Savg flags = 0; 945228424Savg if (m->flags & PG_ZERO) { 946228424Savg vm_page_zero_count--; 947242515Sattilio if (req & VM_ALLOC_ZERO) 948242515Sattilio flags = PG_ZERO; 949171052Sattilio } 950176017Sjeff if (object != NULL && object->type == OBJT_PHYS) 951171052Sattilio flags |= PG_UNMANAGED; 952171052Sattilio m->flags = flags; 953171052Sattilio if (req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ)) 954171052Sattilio m->oflags = 0; 955171052Sattilio else 956154941Sjhb m->oflags = VPO_BUSY; 957154941Sjhb if (req & VM_ALLOC_WIRED) { 958154941Sjhb VMCNT_ADD(wire_count, 1); 959167787Sjhb m->wire_count = 1; 960154941Sjhb } else 961154941Sjhb m->wire_count = 0; 962170295Sjeff m->hold_count = 0; 963167787Sjhb m->act_count = 0; 964154941Sjhb m->busy = 0; 965154941Sjhb m->valid = 0; 966154941Sjhb KASSERT(m->dirty == 0, ("vm_page_alloc: free/cache page %p was dirty", m)); 967154941Sjhb mtx_unlock(&vm_page_queue_free_mtx); 968154941Sjhb 969154941Sjhb if ((req & VM_ALLOC_NOOBJ) == 0) 970154941Sjhb vm_page_insert(m, object, pindex); 971154941Sjhb else 972154941Sjhb m->pindex = pindex; 973154941Sjhb 974154941Sjhb /* 975154941Sjhb * Don't wakeup too often - wakeup the pageout daemon when 976154941Sjhb * we would be nearly out of memory. 977154941Sjhb */ 978154941Sjhb if (vm_paging_needed()) 979154941Sjhb pagedaemon_wakeup(); 980154941Sjhb 981154941Sjhb return (m); 982157846Sjhb} 983176076Sjeff 984176076Sjeff/* 985176076Sjeff * vm_wait: (also see VM_WAIT macro) 986176076Sjeff * 987154941Sjhb * Block until free pages are available for allocation 988157846Sjhb * - Called in various places before memory allocations. 989157846Sjhb */ 990167787Sjhbvoid 991154941Sjhbvm_wait(void) 992154941Sjhb{ 993154941Sjhb 994154941Sjhb mtx_lock(&vm_page_queue_free_mtx); 995154941Sjhb if (curproc == pageproc) { 996170295Sjeff vm_pageout_pages_needed = 1; 997154941Sjhb msleep(&vm_pageout_pages_needed, &vm_page_queue_free_mtx, 998154941Sjhb PDROP | PSWP, "VMWait", 0); 999157882Sjhb } else { 1000157882Sjhb if (!vm_pages_needed) { 1001157882Sjhb vm_pages_needed = 1; 1002157882Sjhb wakeup(&vm_pages_needed); 1003157882Sjhb } 1004157882Sjhb msleep(VMCNT_PTR(free_count), &vm_page_queue_free_mtx, PDROP | 1005242515Sattilio PVM, "vmwait", 0); 1006157882Sjhb } 1007242515Sattilio} 1008176017Sjeff 1009170295Sjeff/* 1010157882Sjhb * vm_waitpfault: (also see VM_WAITPFAULT macro) 1011157882Sjhb * 1012228424Savg * Block until free pages are available for allocation 1013228424Savg * - Called only in vm_fault so that processes page faulting 1014228424Savg * can be easily tracked. 1015242515Sattilio * - Sleeps at a lower priority than vm_wait() so that vm_wait()ing 1016242515Sattilio * processes will be able to grab memory first. Do not change 1017169394Sjhb * this balance without careful testing first. 1018169394Sjhb */ 1019242515Sattiliovoid 1020157882Sjhbvm_waitpfault(void) 1021157882Sjhb{ 1022157882Sjhb 1023157882Sjhb mtx_lock(&vm_page_queue_free_mtx); 1024157882Sjhb if (!vm_pages_needed) { 1025157882Sjhb vm_pages_needed = 1; 1026157882Sjhb wakeup(&vm_pages_needed); 1027157882Sjhb } 1028157882Sjhb msleep(VMCNT_PTR(free_count), &vm_page_queue_free_mtx, PDROP | PUSER, 1029176017Sjeff "pfault", 0); 1030176017Sjeff} 1031176017Sjeff 1032176017Sjeff/* 1033176017Sjeff * vm_page_activate: 1034176017Sjeff * 1035176017Sjeff * Put the specified page on the active list (if appropriate). 1036176017Sjeff * Ensure that act_count is at least ACT_INIT but do not otherwise 1037176017Sjeff * mess with it. 1038176017Sjeff * 1039176017Sjeff * The page queues must be locked. 1040157882Sjhb * This routine may not block. 1041176017Sjeff */ 1042176017Sjeffvoid 1043176017Sjeffvm_page_activate(vm_page_t m) 1044176017Sjeff{ 1045176017Sjeff 1046176017Sjeff mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1047176017Sjeff if (VM_PAGE_GETKNOWNQUEUE2(m) != PQ_ACTIVE) { 1048176017Sjeff if (VM_PAGE_INQUEUE1(m, PQ_CACHE)) 1049176017Sjeff VMCNT_ADD(reactivated, 1); 1050176017Sjeff vm_pageq_remove(m); 1051176017Sjeff if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) { 1052176017Sjeff if (m->act_count < ACT_INIT) 1053176017Sjeff m->act_count = ACT_INIT; 1054176017Sjeff vm_pageq_enqueue(PQ_ACTIVE, m); 1055176017Sjeff } 1056176017Sjeff } else { 1057176017Sjeff if (m->act_count < ACT_INIT) 1058176017Sjeff m->act_count = ACT_INIT; 1059176017Sjeff } 1060176017Sjeff} 1061176017Sjeff 1062176017Sjeff/* 1063176017Sjeff * vm_page_free_wakeup: 1064176017Sjeff * 1065170295Sjeff * Helper routine for vm_page_free_toq() and vm_page_cache(). This 1066176017Sjeff * routine is called when a page has been added to the cache or free 1067167787Sjhb * queues. 1068176017Sjeff * 1069176017Sjeff * The page queues must be locked. 1070167787Sjhb * This routine may not block. 1071157882Sjhb */ 1072285703Smarkjstatic inline void 1073176017Sjeffvm_page_free_wakeup(void) 1074157882Sjhb{ 1075157882Sjhb 1076157882Sjhb mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 1077157882Sjhb /* 1078157882Sjhb * if pageout daemon needs pages, then tell it that there are 1079157882Sjhb * some free. 1080157882Sjhb */ 1081242515Sattilio if (vm_pageout_pages_needed && 1082157882Sjhb VMCNT_GET(cache_count) + VMCNT_GET(free_count) >= 1083242515Sattilio VMCNT_GET(pageout_free_min)) { 1084157882Sjhb wakeup(&vm_pageout_pages_needed); 1085157882Sjhb vm_pageout_pages_needed = 0; 1086176017Sjeff } 1087157882Sjhb /* 1088228424Savg * wakeup processes that are waiting on memory if we hit a 1089228424Savg * high water mark. And wakeup scheduler process if we have 1090228424Savg * lots of memory. this process will swapin processes. 1091242515Sattilio */ 1092242515Sattilio if (vm_pages_needed && !vm_page_count_min()) { 1093169394Sjhb vm_pages_needed = 0; 1094169394Sjhb wakeup(VMCNT_PTR(free_count)); 1095242515Sattilio } 1096171052Sattilio} 1097171052Sattilio 1098171052Sattilio/* 1099171052Sattilio * vm_page_free_toq: 1100157882Sjhb * 1101167787Sjhb * Returns the given page to the PQ_FREE list, 1102157882Sjhb * disassociating it with any VM object. 1103157882Sjhb * 1104157882Sjhb * Object and page must be locked prior to entry. 1105157882Sjhb * This routine may not block. 1106176017Sjeff */ 1107157882Sjhb 1108157882Sjhbvoid 1109157882Sjhbvm_page_free_toq(vm_page_t m) 1110157882Sjhb{ 1111157882Sjhb struct vpgqueues *pq; 1112157882Sjhb 1113157882Sjhb if (VM_PAGE_GETQUEUE(m) != PQ_NONE) 1114157882Sjhb mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1115157882Sjhb KASSERT(!pmap_page_is_mapped(m), 1116170295Sjeff ("vm_page_free_toq: freeing mapped page %p", m)); 1117176017Sjeff VMCNT_ADD(tfree, 1); 1118176017Sjeff 1119176017Sjeff if (m->busy || VM_PAGE_INQUEUE1(m, PQ_FREE)) { 1120176017Sjeff printf( 1121157882Sjhb "vm_page_free: pindex(%lu), busy(%d), VPO_BUSY(%d), hold(%d)\n", 1122157882Sjhb (u_long)m->pindex, m->busy, (m->oflags & VPO_BUSY) ? 1 : 0, 1123176017Sjeff m->hold_count); 1124176017Sjeff if (VM_PAGE_INQUEUE1(m, PQ_FREE)) 1125157882Sjhb panic("vm_page_free: freeing free page"); 1126167787Sjhb else 1127157882Sjhb panic("vm_page_free: freeing busy page"); 1128176017Sjeff } 1129176017Sjeff 1130176017Sjeff /* 1131176017Sjeff * unqueue, then remove page. Note that we cannot destroy 1132176017Sjeff * the page here because we do not want to call the pager's 1133176017Sjeff * callback routine until after we've put the page on the 1134176017Sjeff * appropriate free queue. 1135176017Sjeff */ 1136157882Sjhb vm_pageq_remove_nowakeup(m); 1137157882Sjhb vm_page_remove(m); 1138176017Sjeff 1139157882Sjhb /* 1140170295Sjeff * If fictitious remove object association and 1141157882Sjhb * return, otherwise delay object association removal. 1142176017Sjeff */ 1143167787Sjhb if ((m->flags & PG_FICTITIOUS) != 0) { 1144285703Smarkj return; 1145157882Sjhb } 1146157882Sjhb 1147154941Sjhb m->valid = 0; 1148155162Sscottl vm_page_undirty(m); 1149242515Sattilio 1150154941Sjhb if (m->wire_count != 0) { 1151154941Sjhb if (m->wire_count > 1) { 1152154941Sjhb panic("vm_page_free: invalid wire count (%d), pindex: 0x%lx", 1153154941Sjhb m->wire_count, (long)m->pindex); 1154154941Sjhb } 1155154941Sjhb panic("vm_page_free: freeing wired page"); 1156154941Sjhb } 1157154941Sjhb if (m->hold_count != 0) { 1158242515Sattilio m->flags &= ~PG_ZERO; 1159154941Sjhb vm_pageq_enqueue(PQ_HOLD, m); 1160242515Sattilio return; 1161154941Sjhb } 1162154941Sjhb VM_PAGE_SETQUEUE1(m, PQ_FREE); 1163154941Sjhb mtx_lock(&vm_page_queue_free_mtx); 1164242515Sattilio pq = &vm_page_queues[VM_PAGE_GETQUEUE(m)]; 1165242515Sattilio pq->lcnt++; 1166242515Sattilio ++(*pq->cnt); 1167154941Sjhb 1168154941Sjhb /* 1169171052Sattilio * Put zero'd pages on the end ( where we look for zero'd pages 1170171052Sattilio * first ) and non-zerod pages at the head. 1171154941Sjhb */ 1172251323Sjhb if (m->flags & PG_ZERO) { 1173251323Sjhb TAILQ_INSERT_TAIL(&pq->pl, m, pageq); 1174154941Sjhb ++vm_page_zero_count; 1175167787Sjhb } else { 1176154941Sjhb TAILQ_INSERT_HEAD(&pq->pl, m, pageq); 1177154941Sjhb vm_page_zero_idle_wakeup(); 1178154941Sjhb } 1179154941Sjhb vm_page_free_wakeup(); 1180154941Sjhb mtx_unlock(&vm_page_queue_free_mtx); 1181154941Sjhb} 1182155061Sscottl 1183251323Sjhb/* 1184157826Sjhb * vm_page_wire: 1185154941Sjhb * 1186251323Sjhb * Mark this page as wired down by yet 1187154941Sjhb * another map, removing it from paging queues 1188171052Sattilio * as necessary. 1189251323Sjhb * 1190171052Sattilio * The page queues must be locked. 1191171052Sattilio * This routine may not block. 1192171052Sattilio */ 1193171052Sattiliovoid 1194171052Sattiliovm_page_wire(vm_page_t m) 1195171052Sattilio{ 1196171052Sattilio 1197171052Sattilio /* 1198171052Sattilio * Only bump the wire statistics if the page is not already wired, 1199154941Sjhb * and only unqueue the page if it is on some queue (if it is unmanaged 1200154941Sjhb * it is already off the queues). 1201154941Sjhb */ 1202171052Sattilio mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1203171052Sattilio if (m->flags & PG_FICTITIOUS) 1204157826Sjhb return; 1205154941Sjhb if (m->wire_count == 0) { 1206167787Sjhb if ((m->flags & PG_UNMANAGED) == 0) 1207171052Sattilio vm_pageq_remove(m); 1208171052Sattilio VMCNT_ADD(wire_count, 1); 1209171052Sattilio } 1210171052Sattilio m->wire_count++; 1211171052Sattilio KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m)); 1212171052Sattilio} 1213171052Sattilio 1214154941Sjhb/* 1215154941Sjhb * vm_page_unwire: 1216154941Sjhb * 1217167787Sjhb * Release one wiring of this page, potentially 1218154941Sjhb * enabling it to be paged again. 1219154941Sjhb * 1220154941Sjhb * Many pages placed on the inactive queue should actually go 1221154941Sjhb * into the cache, but it is difficult to figure out which. What 1222154941Sjhb * we do instead, if the inactive target is well met, is to put 1223157826Sjhb * clean pages at the head of the inactive queue instead of the tail. 1224154941Sjhb * This will cause them to be moved to the cache more quickly and 1225167787Sjhb * if not actively re-referenced, freed more quickly. If we just 1226154941Sjhb * stick these pages at the end of the inactive queue, heavy filesystem 1227154941Sjhb * meta-data accesses can cause an unnecessary paging load on memory bound 1228154941Sjhb * processes. This optimization causes one-time-use metadata to be 1229154941Sjhb * reused more quickly. 1230154941Sjhb * 1231154941Sjhb * BUT, if we are in a low-memory situation we have no choice but to 1232154941Sjhb * put clean pages on the cache queue. 1233154941Sjhb * 1234154941Sjhb * A number of routines use vm_page_unwire() to guarantee that the page 1235154941Sjhb * will go into either the inactive or active queues, and will NEVER 1236154941Sjhb * be placed in the cache - for example, just after dirtying a page. 1237227588Spjd * dirty pages in the cache are not allowed. 1238154941Sjhb * 1239227588Spjd * The page queues must be locked. 1240154941Sjhb * This routine may not block. 1241154941Sjhb */ 1242227588Spjdvoid 1243154941Sjhbvm_page_unwire(vm_page_t m, int activate) 1244154941Sjhb{ 1245154941Sjhb 1246154941Sjhb mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1247169394Sjhb if (m->flags & PG_FICTITIOUS) 1248169394Sjhb return; 1249169394Sjhb if (m->wire_count > 0) { 1250169394Sjhb m->wire_count--; 1251167504Sjhb if (m->wire_count == 0) { 1252167504Sjhb VMCNT_DEC(wire_count, 1); 1253154941Sjhb if (m->flags & PG_UNMANAGED) { 1254157826Sjhb ; 1255154941Sjhb } else if (activate) 1256173600Sjulian vm_pageq_enqueue(PQ_ACTIVE, m); 1257171052Sattilio else { 1258171052Sattilio vm_page_flag_clear(m, PG_WINATCFLS); 1259154941Sjhb vm_pageq_enqueue(PQ_INACTIVE, m); 1260154941Sjhb } 1261154941Sjhb } 1262154941Sjhb } else { 1263154941Sjhb panic("vm_page_unwire: invalid wire count: %d", m->wire_count); 1264154941Sjhb } 1265154941Sjhb} 1266154941Sjhb 1267154941Sjhb 1268154941Sjhb/* 1269167492Sjhb * Move the specified page to the inactive queue. If the page has 1270154941Sjhb * any associated swap, the swap is deallocated. 1271154941Sjhb * 1272154941Sjhb * Normally athead is 0 resulting in LRU operation. athead is set 1273154941Sjhb * to 1 if we want this page to be 'as if it were placed in the cache', 1274154941Sjhb * except without unmapping it from the process address space. 1275154941Sjhb * 1276154941Sjhb * This routine may not block. 1277154941Sjhb */ 1278static inline void 1279_vm_page_deactivate(vm_page_t m, int athead) 1280{ 1281 1282 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1283 1284 /* 1285 * Ignore if already inactive. 1286 */ 1287 if (VM_PAGE_INQUEUE2(m, PQ_INACTIVE)) 1288 return; 1289 if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) { 1290 if (VM_PAGE_INQUEUE1(m, PQ_CACHE)) 1291 VMCNT_ADD(reactivated, 1); 1292 vm_page_flag_clear(m, PG_WINATCFLS); 1293 vm_pageq_remove(m); 1294 if (athead) 1295 TAILQ_INSERT_HEAD(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 1296 else 1297 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 1298 VM_PAGE_SETQUEUE2(m, PQ_INACTIVE); 1299 vm_page_queues[PQ_INACTIVE].lcnt++; 1300 VMCNT_ADD(inactive_count, 1); 1301 } 1302} 1303 1304void 1305vm_page_deactivate(vm_page_t m) 1306{ 1307 _vm_page_deactivate(m, 0); 1308} 1309 1310/* 1311 * vm_page_try_to_cache: 1312 * 1313 * Returns 0 on failure, 1 on success 1314 */ 1315int 1316vm_page_try_to_cache(vm_page_t m) 1317{ 1318 1319 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1320 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1321 if (m->dirty || m->hold_count || m->busy || m->wire_count || 1322 (m->oflags & VPO_BUSY) || (m->flags & PG_UNMANAGED)) { 1323 return (0); 1324 } 1325 pmap_remove_all(m); 1326 if (m->dirty) 1327 return (0); 1328 vm_page_cache(m); 1329 return (1); 1330} 1331 1332/* 1333 * vm_page_try_to_free() 1334 * 1335 * Attempt to free the page. If we cannot free it, we do nothing. 1336 * 1 is returned on success, 0 on failure. 1337 */ 1338int 1339vm_page_try_to_free(vm_page_t m) 1340{ 1341 1342 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1343 if (m->object != NULL) 1344 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1345 if (m->dirty || m->hold_count || m->busy || m->wire_count || 1346 (m->oflags & VPO_BUSY) || (m->flags & PG_UNMANAGED)) { 1347 return (0); 1348 } 1349 pmap_remove_all(m); 1350 if (m->dirty) 1351 return (0); 1352 vm_page_free(m); 1353 return (1); 1354} 1355 1356/* 1357 * vm_page_cache 1358 * 1359 * Put the specified page onto the page cache queue (if appropriate). 1360 * 1361 * This routine may not block. 1362 */ 1363void 1364vm_page_cache(vm_page_t m) 1365{ 1366 1367 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1368 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1369 if ((m->flags & PG_UNMANAGED) || (m->oflags & VPO_BUSY) || m->busy || 1370 m->hold_count || m->wire_count) { 1371 printf("vm_page_cache: attempting to cache busy page\n"); 1372 return; 1373 } 1374 if (VM_PAGE_INQUEUE1(m, PQ_CACHE)) 1375 return; 1376 1377 /* 1378 * Remove all pmaps and indicate that the page is not 1379 * writeable or mapped. 1380 */ 1381 pmap_remove_all(m); 1382 if (m->dirty != 0) { 1383 panic("vm_page_cache: caching a dirty page, pindex: %ld", 1384 (long)m->pindex); 1385 } 1386 vm_pageq_remove_nowakeup(m); 1387 vm_pageq_enqueue(PQ_CACHE + m->pc, m); 1388 mtx_lock(&vm_page_queue_free_mtx); 1389 vm_page_free_wakeup(); 1390 mtx_unlock(&vm_page_queue_free_mtx); 1391} 1392 1393/* 1394 * vm_page_dontneed 1395 * 1396 * Cache, deactivate, or do nothing as appropriate. This routine 1397 * is typically used by madvise() MADV_DONTNEED. 1398 * 1399 * Generally speaking we want to move the page into the cache so 1400 * it gets reused quickly. However, this can result in a silly syndrome 1401 * due to the page recycling too quickly. Small objects will not be 1402 * fully cached. On the otherhand, if we move the page to the inactive 1403 * queue we wind up with a problem whereby very large objects 1404 * unnecessarily blow away our inactive and cache queues. 1405 * 1406 * The solution is to move the pages based on a fixed weighting. We 1407 * either leave them alone, deactivate them, or move them to the cache, 1408 * where moving them to the cache has the highest weighting. 1409 * By forcing some pages into other queues we eventually force the 1410 * system to balance the queues, potentially recovering other unrelated 1411 * space from active. The idea is to not force this to happen too 1412 * often. 1413 */ 1414void 1415vm_page_dontneed(vm_page_t m) 1416{ 1417 static int dnweight; 1418 int dnw; 1419 int head; 1420 1421 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1422 dnw = ++dnweight; 1423 1424 /* 1425 * occassionally leave the page alone 1426 */ 1427 if ((dnw & 0x01F0) == 0 || 1428 VM_PAGE_INQUEUE2(m, PQ_INACTIVE) || 1429 VM_PAGE_INQUEUE1(m, PQ_CACHE) 1430 ) { 1431 if (m->act_count >= ACT_INIT) 1432 --m->act_count; 1433 return; 1434 } 1435 1436 if (m->dirty == 0 && pmap_is_modified(m)) 1437 vm_page_dirty(m); 1438 1439 if (m->dirty || (dnw & 0x0070) == 0) { 1440 /* 1441 * Deactivate the page 3 times out of 32. 1442 */ 1443 head = 0; 1444 } else { 1445 /* 1446 * Cache the page 28 times out of every 32. Note that 1447 * the page is deactivated instead of cached, but placed 1448 * at the head of the queue instead of the tail. 1449 */ 1450 head = 1; 1451 } 1452 _vm_page_deactivate(m, head); 1453} 1454 1455/* 1456 * Grab a page, waiting until we are waken up due to the page 1457 * changing state. We keep on waiting, if the page continues 1458 * to be in the object. If the page doesn't exist, first allocate it 1459 * and then conditionally zero it. 1460 * 1461 * This routine may block. 1462 */ 1463vm_page_t 1464vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags) 1465{ 1466 vm_page_t m; 1467 1468 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1469retrylookup: 1470 if ((m = vm_page_lookup(object, pindex)) != NULL) { 1471 if (vm_page_sleep_if_busy(m, TRUE, "pgrbwt")) { 1472 if ((allocflags & VM_ALLOC_RETRY) == 0) 1473 return (NULL); 1474 goto retrylookup; 1475 } else { 1476 if ((allocflags & VM_ALLOC_WIRED) != 0) { 1477 vm_page_lock_queues(); 1478 vm_page_wire(m); 1479 vm_page_unlock_queues(); 1480 } 1481 if ((allocflags & VM_ALLOC_NOBUSY) == 0) 1482 vm_page_busy(m); 1483 return (m); 1484 } 1485 } 1486 m = vm_page_alloc(object, pindex, allocflags & ~VM_ALLOC_RETRY); 1487 if (m == NULL) { 1488 VM_OBJECT_UNLOCK(object); 1489 VM_WAIT; 1490 VM_OBJECT_LOCK(object); 1491 if ((allocflags & VM_ALLOC_RETRY) == 0) 1492 return (NULL); 1493 goto retrylookup; 1494 } 1495 if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0) 1496 pmap_zero_page(m); 1497 return (m); 1498} 1499 1500/* 1501 * Mapping function for valid bits or for dirty bits in 1502 * a page. May not block. 1503 * 1504 * Inputs are required to range within a page. 1505 */ 1506inline int 1507vm_page_bits(int base, int size) 1508{ 1509 int first_bit; 1510 int last_bit; 1511 1512 KASSERT( 1513 base + size <= PAGE_SIZE, 1514 ("vm_page_bits: illegal base/size %d/%d", base, size) 1515 ); 1516 1517 if (size == 0) /* handle degenerate case */ 1518 return (0); 1519 1520 first_bit = base >> DEV_BSHIFT; 1521 last_bit = (base + size - 1) >> DEV_BSHIFT; 1522 1523 return ((2 << last_bit) - (1 << first_bit)); 1524} 1525 1526/* 1527 * vm_page_set_validclean: 1528 * 1529 * Sets portions of a page valid and clean. The arguments are expected 1530 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive 1531 * of any partial chunks touched by the range. The invalid portion of 1532 * such chunks will be zero'd. 1533 * 1534 * This routine may not block. 1535 * 1536 * (base + size) must be less then or equal to PAGE_SIZE. 1537 */ 1538void 1539vm_page_set_validclean(vm_page_t m, int base, int size) 1540{ 1541 int pagebits; 1542 int frag; 1543 int endoff; 1544 1545 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1546 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1547 if (size == 0) /* handle degenerate case */ 1548 return; 1549 1550 /* 1551 * If the base is not DEV_BSIZE aligned and the valid 1552 * bit is clear, we have to zero out a portion of the 1553 * first block. 1554 */ 1555 if ((frag = base & ~(DEV_BSIZE - 1)) != base && 1556 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0) 1557 pmap_zero_page_area(m, frag, base - frag); 1558 1559 /* 1560 * If the ending offset is not DEV_BSIZE aligned and the 1561 * valid bit is clear, we have to zero out a portion of 1562 * the last block. 1563 */ 1564 endoff = base + size; 1565 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff && 1566 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0) 1567 pmap_zero_page_area(m, endoff, 1568 DEV_BSIZE - (endoff & (DEV_BSIZE - 1))); 1569 1570 /* 1571 * Set valid, clear dirty bits. If validating the entire 1572 * page we can safely clear the pmap modify bit. We also 1573 * use this opportunity to clear the VPO_NOSYNC flag. If a process 1574 * takes a write fault on a MAP_NOSYNC memory area the flag will 1575 * be set again. 1576 * 1577 * We set valid bits inclusive of any overlap, but we can only 1578 * clear dirty bits for DEV_BSIZE chunks that are fully within 1579 * the range. 1580 */ 1581 pagebits = vm_page_bits(base, size); 1582 m->valid |= pagebits; 1583#if 0 /* NOT YET */ 1584 if ((frag = base & (DEV_BSIZE - 1)) != 0) { 1585 frag = DEV_BSIZE - frag; 1586 base += frag; 1587 size -= frag; 1588 if (size < 0) 1589 size = 0; 1590 } 1591 pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1)); 1592#endif 1593 m->dirty &= ~pagebits; 1594 if (base == 0 && size == PAGE_SIZE) { 1595 pmap_clear_modify(m); 1596 m->oflags &= ~VPO_NOSYNC; 1597 } 1598} 1599 1600void 1601vm_page_clear_dirty(vm_page_t m, int base, int size) 1602{ 1603 1604 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1605 m->dirty &= ~vm_page_bits(base, size); 1606} 1607 1608/* 1609 * vm_page_set_invalid: 1610 * 1611 * Invalidates DEV_BSIZE'd chunks within a page. Both the 1612 * valid and dirty bits for the effected areas are cleared. 1613 * 1614 * May not block. 1615 */ 1616void 1617vm_page_set_invalid(vm_page_t m, int base, int size) 1618{ 1619 int bits; 1620 1621 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1622 bits = vm_page_bits(base, size); 1623 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1624 if (m->valid == VM_PAGE_BITS_ALL && bits != 0) 1625 pmap_remove_all(m); 1626 m->valid &= ~bits; 1627 m->dirty &= ~bits; 1628 m->object->generation++; 1629} 1630 1631/* 1632 * vm_page_zero_invalid() 1633 * 1634 * The kernel assumes that the invalid portions of a page contain 1635 * garbage, but such pages can be mapped into memory by user code. 1636 * When this occurs, we must zero out the non-valid portions of the 1637 * page so user code sees what it expects. 1638 * 1639 * Pages are most often semi-valid when the end of a file is mapped 1640 * into memory and the file's size is not page aligned. 1641 */ 1642void 1643vm_page_zero_invalid(vm_page_t m, boolean_t setvalid) 1644{ 1645 int b; 1646 int i; 1647 1648 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1649 /* 1650 * Scan the valid bits looking for invalid sections that 1651 * must be zerod. Invalid sub-DEV_BSIZE'd areas ( where the 1652 * valid bit may be set ) have already been zerod by 1653 * vm_page_set_validclean(). 1654 */ 1655 for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) { 1656 if (i == (PAGE_SIZE / DEV_BSIZE) || 1657 (m->valid & (1 << i)) 1658 ) { 1659 if (i > b) { 1660 pmap_zero_page_area(m, 1661 b << DEV_BSHIFT, (i - b) << DEV_BSHIFT); 1662 } 1663 b = i + 1; 1664 } 1665 } 1666 1667 /* 1668 * setvalid is TRUE when we can safely set the zero'd areas 1669 * as being valid. We can do this if there are no cache consistancy 1670 * issues. e.g. it is ok to do with UFS, but not ok to do with NFS. 1671 */ 1672 if (setvalid) 1673 m->valid = VM_PAGE_BITS_ALL; 1674} 1675 1676/* 1677 * vm_page_is_valid: 1678 * 1679 * Is (partial) page valid? Note that the case where size == 0 1680 * will return FALSE in the degenerate case where the page is 1681 * entirely invalid, and TRUE otherwise. 1682 * 1683 * May not block. 1684 */ 1685int 1686vm_page_is_valid(vm_page_t m, int base, int size) 1687{ 1688 int bits = vm_page_bits(base, size); 1689 1690 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1691 if (m->valid && ((m->valid & bits) == bits)) 1692 return 1; 1693 else 1694 return 0; 1695} 1696 1697/* 1698 * update dirty bits from pmap/mmu. May not block. 1699 */ 1700void 1701vm_page_test_dirty(vm_page_t m) 1702{ 1703 if ((m->dirty != VM_PAGE_BITS_ALL) && pmap_is_modified(m)) { 1704 vm_page_dirty(m); 1705 } 1706} 1707 1708int so_zerocp_fullpage = 0; 1709 1710void 1711vm_page_cowfault(vm_page_t m) 1712{ 1713 vm_page_t mnew; 1714 vm_object_t object; 1715 vm_pindex_t pindex; 1716 1717 object = m->object; 1718 pindex = m->pindex; 1719 1720 retry_alloc: 1721 pmap_remove_all(m); 1722 vm_page_remove(m); 1723 mnew = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL | VM_ALLOC_NOBUSY); 1724 if (mnew == NULL) { 1725 vm_page_insert(m, object, pindex); 1726 vm_page_unlock_queues(); 1727 VM_OBJECT_UNLOCK(object); 1728 VM_WAIT; 1729 VM_OBJECT_LOCK(object); 1730 vm_page_lock_queues(); 1731 goto retry_alloc; 1732 } 1733 1734 if (m->cow == 0) { 1735 /* 1736 * check to see if we raced with an xmit complete when 1737 * waiting to allocate a page. If so, put things back 1738 * the way they were 1739 */ 1740 vm_page_free(mnew); 1741 vm_page_insert(m, object, pindex); 1742 } else { /* clear COW & copy page */ 1743 if (!so_zerocp_fullpage) 1744 pmap_copy_page(m, mnew); 1745 mnew->valid = VM_PAGE_BITS_ALL; 1746 vm_page_dirty(mnew); 1747 mnew->wire_count = m->wire_count - m->cow; 1748 m->wire_count = m->cow; 1749 } 1750} 1751 1752void 1753vm_page_cowclear(vm_page_t m) 1754{ 1755 1756 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1757 if (m->cow) { 1758 m->cow--; 1759 /* 1760 * let vm_fault add back write permission lazily 1761 */ 1762 } 1763 /* 1764 * sf_buf_free() will free the page, so we needn't do it here 1765 */ 1766} 1767 1768void 1769vm_page_cowsetup(vm_page_t m) 1770{ 1771 1772 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1773 m->cow++; 1774 pmap_remove_write(m); 1775} 1776 1777#include "opt_ddb.h" 1778#ifdef DDB 1779#include <sys/kernel.h> 1780 1781#include <ddb/ddb.h> 1782 1783DB_SHOW_COMMAND(page, vm_page_print_page_info) 1784{ 1785 db_printf("cnt.v_free_count: %d\n", VMCNT_GET(free_count)); 1786 db_printf("cnt.v_cache_count: %d\n", VMCNT_GET(cache_count)); 1787 db_printf("cnt.v_inactive_count: %d\n", VMCNT_GET(inactive_count)); 1788 db_printf("cnt.v_active_count: %d\n", VMCNT_GET(active_count)); 1789 db_printf("cnt.v_wire_count: %d\n", VMCNT_GET(wire_count)); 1790 db_printf("cnt.v_free_reserved: %d\n", VMCNT_GET(free_reserved)); 1791 db_printf("cnt.v_free_min: %d\n", VMCNT_GET(free_min)); 1792 db_printf("cnt.v_free_target: %d\n", VMCNT_GET(free_target)); 1793 db_printf("cnt.v_cache_min: %d\n", VMCNT_GET(cache_min)); 1794 db_printf("cnt.v_inactive_target: %d\n", VMCNT_GET(inactive_target)); 1795} 1796 1797DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info) 1798{ 1799 int i; 1800 db_printf("PQ_FREE:"); 1801 for (i = 0; i < PQ_NUMCOLORS; i++) { 1802 db_printf(" %d", vm_page_queues[PQ_FREE + i].lcnt); 1803 } 1804 db_printf("\n"); 1805 1806 db_printf("PQ_CACHE:"); 1807 for (i = 0; i < PQ_NUMCOLORS; i++) { 1808 db_printf(" %d", vm_page_queues[PQ_CACHE + i].lcnt); 1809 } 1810 db_printf("\n"); 1811 1812 db_printf("PQ_ACTIVE: %d, PQ_INACTIVE: %d\n", 1813 vm_page_queues[PQ_ACTIVE].lcnt, 1814 vm_page_queues[PQ_INACTIVE].lcnt); 1815} 1816#endif /* DDB */ 1817