vm_page.c revision 273174
1117845Ssam/*- 2117845Ssam * Copyright (c) 1991 Regents of the University of California. 3117845Ssam * All rights reserved. 4117845Ssam * Copyright (c) 1998 Matthew Dillon. All Rights Reserved. 5117845Ssam * 6117845Ssam * This code is derived from software contributed to Berkeley by 7117845Ssam * The Mach Operating System project at Carnegie-Mellon University. 8117845Ssam * 9117845Ssam * Redistribution and use in source and binary forms, with or without 10117845Ssam * modification, are permitted provided that the following conditions 11117845Ssam * are met: 12117845Ssam * 1. Redistributions of source code must retain the above copyright 13117845Ssam * notice, this list of conditions and the following disclaimer. 14117845Ssam * 2. Redistributions in binary form must reproduce the above copyright 15117845Ssam * notice, this list of conditions and the following disclaimer in the 16117845Ssam * documentation and/or other materials provided with the distribution. 17117845Ssam * 4. Neither the name of the University nor the names of its contributors 18117845Ssam * may be used to endorse or promote products derived from this software 19117845Ssam * without specific prior written permission. 20117845Ssam * 21117845Ssam * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22117845Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23117845Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24117845Ssam * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25117845Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26117845Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27117845Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28117845Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29117845Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30117845Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31117845Ssam * SUCH DAMAGE. 32117845Ssam * 33117845Ssam * from: @(#)vm_page.c 7.4 (Berkeley) 5/7/91 34117845Ssam */ 35117845Ssam 36117845Ssam/*- 37117845Ssam * Copyright (c) 1987, 1990 Carnegie-Mellon University. 38117845Ssam * All rights reserved. 39117845Ssam * 40117845Ssam * Authors: Avadis Tevanian, Jr., Michael Wayne Young 41117845Ssam * 42117845Ssam * Permission to use, copy, modify and distribute this software and 43129879Sphk * its documentation is hereby granted, provided that both the copyright 44117845Ssam * notice and this permission notice appear in all copies of the 45117845Ssam * software, derivative works or modified versions, and any portions 46117845Ssam * thereof, and that both notices appear in supporting documentation. 47117845Ssam * 48117845Ssam * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 49117845Ssam * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 50117845Ssam * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 51117845Ssam * 52117845Ssam * Carnegie Mellon requests users of this software to return to 53117845Ssam * 54117845Ssam * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 55117845Ssam * School of Computer Science 56117845Ssam * Carnegie Mellon University 57117845Ssam * Pittsburgh PA 15213-3890 58117845Ssam * 59117845Ssam * any improvements or extensions that they make and grant Carnegie the 60117845Ssam * rights to redistribute these changes. 61117845Ssam */ 62117845Ssam 63119287Simp/* 64119287Simp * GENERAL RULES ON VM_PAGE MANIPULATION 65117845Ssam * 66117845Ssam * - A page queue lock is required when adding or removing a page from a 67117845Ssam * page queue regardless of other locks or the busy state of a page. 68117845Ssam * 69117845Ssam * * In general, no thread besides the page daemon can acquire or 70117845Ssam * hold more than one page queue lock at a time. 71117845Ssam * 72117845Ssam * * The page daemon can acquire and hold any pair of page queue 73117845Ssam * locks in any order. 74117845Ssam * 75117845Ssam * - The object lock is required when inserting or removing 76117845Ssam * pages from an object (vm_page_insert() or vm_page_remove()). 77117845Ssam * 78117845Ssam */ 79117845Ssam 80117845Ssam/* 81117845Ssam * Resident memory management module. 82117845Ssam */ 83117845Ssam 84117845Ssam#include <sys/cdefs.h> 85117845Ssam__FBSDID("$FreeBSD: head/sys/vm/vm_page.c 273174 2014-10-16 18:04:43Z davide $"); 86117845Ssam 87117845Ssam#include "opt_vm.h" 88117845Ssam 89117845Ssam#include <sys/param.h> 90117845Ssam#include <sys/systm.h> 91117845Ssam#include <sys/lock.h> 92117845Ssam#include <sys/kernel.h> 93117845Ssam#include <sys/limits.h> 94117845Ssam#include <sys/malloc.h> 95117845Ssam#include <sys/mman.h> 96117845Ssam#include <sys/msgbuf.h> 97117845Ssam#include <sys/mutex.h> 98117845Ssam#include <sys/proc.h> 99117845Ssam#include <sys/rwlock.h> 100117845Ssam#include <sys/sysctl.h> 101117845Ssam#include <sys/vmmeter.h> 102117845Ssam#include <sys/vnode.h> 103117845Ssam 104117845Ssam#include <vm/vm.h> 105117845Ssam#include <vm/pmap.h> 106117845Ssam#include <vm/vm_param.h> 107117845Ssam#include <vm/vm_kern.h> 108117845Ssam#include <vm/vm_object.h> 109117845Ssam#include <vm/vm_page.h> 110117845Ssam#include <vm/vm_pageout.h> 111117845Ssam#include <vm/vm_pager.h> 112117845Ssam#include <vm/vm_phys.h> 113117845Ssam#include <vm/vm_radix.h> 114117845Ssam#include <vm/vm_reserv.h> 115117845Ssam#include <vm/vm_extern.h> 116117845Ssam#include <vm/uma.h> 117117845Ssam#include <vm/uma_int.h> 118117845Ssam 119117845Ssam#include <machine/md_var.h> 120117845Ssam 121117845Ssam/* 122117845Ssam * Associated with page of user-allocatable memory is a 123117845Ssam * page structure. 124117845Ssam */ 125117845Ssam 126117845Ssamstruct vm_domain vm_dom[MAXMEMDOM]; 127117845Ssamstruct mtx_padalign vm_page_queue_free_mtx; 128117845Ssam 129117845Ssamstruct mtx_padalign pa_lock[PA_LOCK_COUNT]; 130117845Ssam 131117845Ssamvm_page_t vm_page_array; 132117845Ssamlong vm_page_array_size; 133117845Ssamlong first_page; 134117845Ssamint vm_page_zero_count; 135117845Ssam 136117845Ssamstatic int boot_pages = UMA_BOOT_PAGES; 137117845SsamSYSCTL_INT(_vm, OID_AUTO, boot_pages, CTLFLAG_RDTUN, &boot_pages, 0, 138117845Ssam "number of pages allocated for bootstrapping the VM system"); 139117845Ssam 140117845Ssamstatic int pa_tryrelock_restart; 141117845SsamSYSCTL_INT(_vm, OID_AUTO, tryrelock_restart, CTLFLAG_RD, 142117845Ssam &pa_tryrelock_restart, 0, "Number of tryrelock restarts"); 143117845Ssam 144117845Ssamstatic uma_zone_t fakepg_zone; 145117845Ssam 146117845Ssamstatic struct vnode *vm_page_alloc_init(vm_page_t m); 147117845Ssamstatic void vm_page_cache_turn_free(vm_page_t m); 148117845Ssamstatic void vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits); 149117845Ssamstatic void vm_page_enqueue(uint8_t queue, vm_page_t m); 150117845Ssamstatic void vm_page_init_fakepg(void *dummy); 151117845Ssamstatic int vm_page_insert_after(vm_page_t m, vm_object_t object, 152117845Ssam vm_pindex_t pindex, vm_page_t mpred); 153117845Ssamstatic void vm_page_insert_radixdone(vm_page_t m, vm_object_t object, 154117845Ssam vm_page_t mpred); 155117845Ssam 156117845SsamSYSINIT(vm_page, SI_SUB_VM, SI_ORDER_SECOND, vm_page_init_fakepg, NULL); 157117845Ssam 158117845Ssamstatic void 159117845Ssamvm_page_init_fakepg(void *dummy) 160117845Ssam{ 161117845Ssam 162117845Ssam fakepg_zone = uma_zcreate("fakepg", sizeof(struct vm_page), NULL, NULL, 163117845Ssam NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE | UMA_ZONE_VM); 164117845Ssam} 165117845Ssam 166117845Ssam/* Make sure that u_long is at least 64 bits when PAGE_SIZE is 32K. */ 167117845Ssam#if PAGE_SIZE == 32768 168117845Ssam#ifdef CTASSERT 169117845SsamCTASSERT(sizeof(u_long) >= 8); 170117845Ssam#endif 171117845Ssam#endif 172117845Ssam 173117845Ssam/* 174117845Ssam * Try to acquire a physical address lock while a pmap is locked. If we 175117845Ssam * fail to trylock we unlock and lock the pmap directly and cache the 176117845Ssam * locked pa in *locked. The caller should then restart their loop in case 177117845Ssam * the virtual to physical mapping has changed. 178117845Ssam */ 179117845Ssamint 180117845Ssamvm_page_pa_tryrelock(pmap_t pmap, vm_paddr_t pa, vm_paddr_t *locked) 181117845Ssam{ 182117845Ssam vm_paddr_t lockpa; 183117845Ssam 184117845Ssam lockpa = *locked; 185117845Ssam *locked = pa; 186142890Simp if (lockpa) { 187117845Ssam PA_LOCK_ASSERT(lockpa, MA_OWNED); 188117845Ssam if (PA_LOCKPTR(pa) == PA_LOCKPTR(lockpa)) 189117845Ssam return (0); 190117845Ssam PA_UNLOCK(lockpa); 191117845Ssam } 192117845Ssam if (PA_TRYLOCK(pa)) 193117845Ssam return (0); 194117845Ssam PMAP_UNLOCK(pmap); 195117845Ssam atomic_add_int(&pa_tryrelock_restart, 1); 196117845Ssam PA_LOCK(pa); 197117845Ssam PMAP_LOCK(pmap); 198117845Ssam return (EAGAIN); 199117845Ssam} 200117845Ssam 201117845Ssam/* 202117845Ssam * vm_set_page_size: 203117845Ssam * 204117845Ssam * Sets the page size, perhaps based upon the memory 205117845Ssam * size. Must be called before any use of page-size 206117845Ssam * dependent functions. 207117845Ssam */ 208117845Ssamvoid 209117845Ssamvm_set_page_size(void) 210117845Ssam{ 211117845Ssam if (vm_cnt.v_page_size == 0) 212117845Ssam vm_cnt.v_page_size = PAGE_SIZE; 213117845Ssam if (((vm_cnt.v_page_size - 1) & vm_cnt.v_page_size) != 0) 214117845Ssam panic("vm_set_page_size: page size not a power of two"); 215117845Ssam} 216117845Ssam 217117845Ssam/* 218117845Ssam * vm_page_blacklist_lookup: 219117845Ssam * 220117845Ssam * See if a physical address in this page has been listed 221117845Ssam * in the blacklist tunable. Entries in the tunable are 222117845Ssam * separated by spaces or commas. If an invalid integer is 223117845Ssam * encountered then the rest of the string is skipped. 224117845Ssam */ 225117845Ssamstatic int 226117845Ssamvm_page_blacklist_lookup(char *list, vm_paddr_t pa) 227117845Ssam{ 228117845Ssam vm_paddr_t bad; 229117845Ssam char *cp, *pos; 230117845Ssam 231117845Ssam for (pos = list; *pos != '\0'; pos = cp) { 232117845Ssam bad = strtoq(pos, &cp, 0); 233117845Ssam if (*cp != '\0') { 234117845Ssam if (*cp == ' ' || *cp == ',') { 235117845Ssam cp++; 236117845Ssam if (cp == pos) 237117845Ssam continue; 238117845Ssam } else 239117845Ssam break; 240117845Ssam } 241117845Ssam if (pa == trunc_page(bad)) 242117845Ssam return (1); 243117845Ssam } 244127135Snjl return (0); 245127135Snjl} 246117845Ssam 247117845Ssamstatic void 248117845Ssamvm_page_domain_init(struct vm_domain *vmd) 249117845Ssam{ 250117845Ssam struct vm_pagequeue *pq; 251117845Ssam int i; 252117845Ssam 253117845Ssam *__DECONST(char **, &vmd->vmd_pagequeues[PQ_INACTIVE].pq_name) = 254117845Ssam "vm inactive pagequeue"; 255117845Ssam *__DECONST(int **, &vmd->vmd_pagequeues[PQ_INACTIVE].pq_vcnt) = 256117845Ssam &vm_cnt.v_inactive_count; 257127135Snjl *__DECONST(char **, &vmd->vmd_pagequeues[PQ_ACTIVE].pq_name) = 258127135Snjl "vm active pagequeue"; 259117845Ssam *__DECONST(int **, &vmd->vmd_pagequeues[PQ_ACTIVE].pq_vcnt) = 260117845Ssam &vm_cnt.v_active_count; 261117845Ssam vmd->vmd_page_count = 0; 262117845Ssam vmd->vmd_free_count = 0; 263117845Ssam vmd->vmd_segs = 0; 264117845Ssam vmd->vmd_oom = FALSE; 265117845Ssam vmd->vmd_pass = 0; 266117845Ssam for (i = 0; i < PQ_COUNT; i++) { 267117845Ssam pq = &vmd->vmd_pagequeues[i]; 268117845Ssam TAILQ_INIT(&pq->pq_pl); 269117845Ssam mtx_init(&pq->pq_mutex, pq->pq_name, "vm pagequeue", 270117845Ssam MTX_DEF | MTX_DUPOK); 271117845Ssam } 272117845Ssam} 273117845Ssam 274117845Ssam/* 275117845Ssam * vm_page_startup: 276117845Ssam * 277117845Ssam * Initializes the resident memory module. 278117845Ssam * 279117845Ssam * Allocates memory for the page cells, and 280117845Ssam * for the object/offset-to-page hash table headers. 281117845Ssam * Each page cell is initialized and placed on the free list. 282117845Ssam */ 283117845Ssamvm_offset_t 284117845Ssamvm_page_startup(vm_offset_t vaddr) 285117845Ssam{ 286117845Ssam vm_offset_t mapped; 287117845Ssam vm_paddr_t page_range; 288117845Ssam vm_paddr_t new_end; 289117845Ssam int i; 290117845Ssam vm_paddr_t pa; 291117845Ssam vm_paddr_t last_pa; 292117845Ssam char *list; 293117845Ssam 294117845Ssam /* the biggest memory array is the second group of pages */ 295117845Ssam vm_paddr_t end; 296117845Ssam vm_paddr_t biggestsize; 297117845Ssam vm_paddr_t low_water, high_water; 298117845Ssam int biggestone; 299117845Ssam 300117845Ssam biggestsize = 0; 301117845Ssam biggestone = 0; 302117845Ssam vaddr = round_page(vaddr); 303117845Ssam 304117845Ssam for (i = 0; phys_avail[i + 1]; i += 2) { 305117845Ssam phys_avail[i] = round_page(phys_avail[i]); 306117845Ssam phys_avail[i + 1] = trunc_page(phys_avail[i + 1]); 307117845Ssam } 308117845Ssam 309117845Ssam low_water = phys_avail[0]; 310117845Ssam high_water = phys_avail[1]; 311117845Ssam 312117845Ssam for (i = 0; phys_avail[i + 1]; i += 2) { 313117845Ssam vm_paddr_t size = phys_avail[i + 1] - phys_avail[i]; 314117845Ssam 315117845Ssam if (size > biggestsize) { 316117845Ssam biggestone = i; 317117845Ssam biggestsize = size; 318117845Ssam } 319117845Ssam if (phys_avail[i] < low_water) 320117845Ssam low_water = phys_avail[i]; 321117845Ssam if (phys_avail[i + 1] > high_water) 322117845Ssam high_water = phys_avail[i + 1]; 323117845Ssam } 324117845Ssam 325117845Ssam#ifdef XEN 326117845Ssam low_water = 0; 327117845Ssam#endif 328117845Ssam 329117845Ssam end = phys_avail[biggestone+1]; 330117845Ssam 331117845Ssam /* 332117845Ssam * Initialize the page and queue locks. 333117845Ssam */ 334117845Ssam mtx_init(&vm_page_queue_free_mtx, "vm page free queue", NULL, MTX_DEF); 335117845Ssam for (i = 0; i < PA_LOCK_COUNT; i++) 336117845Ssam mtx_init(&pa_lock[i], "vm page", NULL, MTX_DEF); 337117845Ssam for (i = 0; i < vm_ndomains; i++) 338117845Ssam vm_page_domain_init(&vm_dom[i]); 339117845Ssam 340117845Ssam /* 341117845Ssam * Allocate memory for use when boot strapping the kernel memory 342117845Ssam * allocator. 343117845Ssam */ 344117845Ssam new_end = end - (boot_pages * UMA_SLAB_SIZE); 345117845Ssam new_end = trunc_page(new_end); 346117845Ssam mapped = pmap_map(&vaddr, new_end, end, 347117845Ssam VM_PROT_READ | VM_PROT_WRITE); 348117845Ssam bzero((void *)mapped, end - new_end); 349117845Ssam uma_startup((void *)mapped, boot_pages); 350117845Ssam 351117845Ssam#if defined(__amd64__) || defined(__i386__) || defined(__arm__) || \ 352117845Ssam defined(__mips__) 353117845Ssam /* 354117845Ssam * Allocate a bitmap to indicate that a random physical page 355117845Ssam * needs to be included in a minidump. 356117845Ssam * 357117845Ssam * The amd64 port needs this to indicate which direct map pages 358117845Ssam * need to be dumped, via calls to dump_add_page()/dump_drop_page(). 359117845Ssam * 360117845Ssam * However, i386 still needs this workspace internally within the 361117845Ssam * minidump code. In theory, they are not needed on i386, but are 362117845Ssam * included should the sf_buf code decide to use them. 363117845Ssam */ 364117845Ssam last_pa = 0; 365117845Ssam for (i = 0; dump_avail[i + 1] != 0; i += 2) 366117845Ssam if (dump_avail[i + 1] > last_pa) 367117845Ssam last_pa = dump_avail[i + 1]; 368117845Ssam page_range = last_pa / PAGE_SIZE; 369117845Ssam vm_page_dump_size = round_page(roundup2(page_range, NBBY) / NBBY); 370117845Ssam new_end -= vm_page_dump_size; 371117845Ssam vm_page_dump = (void *)(uintptr_t)pmap_map(&vaddr, new_end, 372117845Ssam new_end + vm_page_dump_size, VM_PROT_READ | VM_PROT_WRITE); 373117845Ssam bzero((void *)vm_page_dump, vm_page_dump_size); 374117845Ssam#endif 375117845Ssam#ifdef __amd64__ 376117845Ssam /* 377117845Ssam * Request that the physical pages underlying the message buffer be 378117845Ssam * included in a crash dump. Since the message buffer is accessed 379117845Ssam * through the direct map, they are not automatically included. 380117845Ssam */ 381117845Ssam pa = DMAP_TO_PHYS((vm_offset_t)msgbufp->msg_ptr); 382117845Ssam last_pa = pa + round_page(msgbufsize); 383117845Ssam while (pa < last_pa) { 384117845Ssam dump_add_page(pa); 385117845Ssam pa += PAGE_SIZE; 386117845Ssam } 387117845Ssam#endif 388117845Ssam /* 389117845Ssam * Compute the number of pages of memory that will be available for 390117845Ssam * use (taking into account the overhead of a page structure per 391117845Ssam * page). 392117845Ssam */ 393117845Ssam first_page = low_water / PAGE_SIZE; 394117845Ssam#ifdef VM_PHYSSEG_SPARSE 395117845Ssam page_range = 0; 396117845Ssam for (i = 0; phys_avail[i + 1] != 0; i += 2) 397117845Ssam page_range += atop(phys_avail[i + 1] - phys_avail[i]); 398117845Ssam#elif defined(VM_PHYSSEG_DENSE) 399117845Ssam page_range = high_water / PAGE_SIZE - first_page; 400117845Ssam#else 401117845Ssam#error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined." 402117845Ssam#endif 403117845Ssam end = new_end; 404117845Ssam 405117845Ssam /* 406117845Ssam * Reserve an unmapped guard page to trap access to vm_page_array[-1]. 407117845Ssam */ 408117845Ssam vaddr += PAGE_SIZE; 409117845Ssam 410117845Ssam /* 411117845Ssam * Initialize the mem entry structures now, and put them in the free 412117845Ssam * queue. 413117845Ssam */ 414117845Ssam new_end = trunc_page(end - page_range * sizeof(struct vm_page)); 415117845Ssam mapped = pmap_map(&vaddr, new_end, end, 416117845Ssam VM_PROT_READ | VM_PROT_WRITE); 417117845Ssam vm_page_array = (vm_page_t) mapped; 418117845Ssam#if VM_NRESERVLEVEL > 0 419117845Ssam /* 420117845Ssam * Allocate memory for the reservation management system's data 421117845Ssam * structures. 422117845Ssam */ 423117845Ssam new_end = vm_reserv_startup(&vaddr, new_end, high_water); 424117845Ssam#endif 425117845Ssam#if defined(__amd64__) || defined(__mips__) 426117845Ssam /* 427117845Ssam * pmap_map on amd64 and mips can come out of the direct-map, not kvm 428117845Ssam * like i386, so the pages must be tracked for a crashdump to include 429117845Ssam * this data. This includes the vm_page_array and the early UMA 430117845Ssam * bootstrap pages. 431117845Ssam */ 432117845Ssam for (pa = new_end; pa < phys_avail[biggestone + 1]; pa += PAGE_SIZE) 433117845Ssam dump_add_page(pa); 434117845Ssam#endif 435117845Ssam phys_avail[biggestone + 1] = new_end; 436117845Ssam 437117845Ssam /* 438117845Ssam * Clear all of the page structures 439117845Ssam */ 440117845Ssam bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page)); 441117845Ssam for (i = 0; i < page_range; i++) 442117845Ssam vm_page_array[i].order = VM_NFREEORDER; 443117845Ssam vm_page_array_size = page_range; 444119137Ssam 445117845Ssam /* 446117845Ssam * Initialize the physical memory allocator. 447117845Ssam */ 448117845Ssam vm_phys_init(); 449117845Ssam 450117845Ssam /* 451117845Ssam * Add every available physical page that is not blacklisted to 452117845Ssam * the free lists. 453117845Ssam */ 454117845Ssam vm_cnt.v_page_count = 0; 455117845Ssam vm_cnt.v_free_count = 0; 456117845Ssam list = kern_getenv("vm.blacklist"); 457117845Ssam for (i = 0; phys_avail[i + 1] != 0; i += 2) { 458117845Ssam pa = phys_avail[i]; 459117845Ssam last_pa = phys_avail[i + 1]; 460117845Ssam while (pa < last_pa) { 461117845Ssam if (list != NULL && 462117845Ssam vm_page_blacklist_lookup(list, pa)) 463117845Ssam printf("Skipping page with pa 0x%jx\n", 464117845Ssam (uintmax_t)pa); 465117845Ssam else 466117845Ssam vm_phys_add_page(pa); 467117845Ssam pa += PAGE_SIZE; 468117845Ssam } 469117845Ssam } 470117845Ssam freeenv(list); 471117845Ssam#if VM_NRESERVLEVEL > 0 472117845Ssam /* 473117845Ssam * Initialize the reservation management system. 474117845Ssam */ 475117845Ssam vm_reserv_init(); 476117845Ssam#endif 477117845Ssam return (vaddr); 478117845Ssam} 479117845Ssam 480117845Ssamvoid 481117845Ssamvm_page_reference(vm_page_t m) 482117845Ssam{ 483117845Ssam 484117845Ssam vm_page_aflag_set(m, PGA_REFERENCED); 485117845Ssam} 486117845Ssam 487117845Ssam/* 488117845Ssam * vm_page_busy_downgrade: 489117845Ssam * 490117845Ssam * Downgrade an exclusive busy page into a single shared busy page. 491117845Ssam */ 492117845Ssamvoid 493117845Ssamvm_page_busy_downgrade(vm_page_t m) 494117845Ssam{ 495117845Ssam u_int x; 496117845Ssam 497117845Ssam vm_page_assert_xbusied(m); 498117845Ssam 499117845Ssam for (;;) { 500117845Ssam x = m->busy_lock; 501117845Ssam x &= VPB_BIT_WAITERS; 502117845Ssam if (atomic_cmpset_rel_int(&m->busy_lock, 503117845Ssam VPB_SINGLE_EXCLUSIVER | x, VPB_SHARERS_WORD(1) | x)) 504117845Ssam break; 505117845Ssam } 506117845Ssam} 507117845Ssam 508117845Ssam/* 509117845Ssam * vm_page_sbusied: 510117845Ssam * 511117845Ssam * Return a positive value if the page is shared busied, 0 otherwise. 512117845Ssam */ 513117845Ssamint 514117845Ssamvm_page_sbusied(vm_page_t m) 515117845Ssam{ 516117845Ssam u_int x; 517117845Ssam 518117845Ssam x = m->busy_lock; 519117845Ssam return ((x & VPB_BIT_SHARED) != 0 && x != VPB_UNBUSIED); 520117845Ssam} 521117845Ssam 522117845Ssam/* 523117845Ssam * vm_page_sunbusy: 524117845Ssam * 525117845Ssam * Shared unbusy a page. 526117845Ssam */ 527117845Ssamvoid 528117845Ssamvm_page_sunbusy(vm_page_t m) 529117845Ssam{ 530117845Ssam u_int x; 531117845Ssam 532117845Ssam vm_page_assert_sbusied(m); 533117845Ssam 534117845Ssam for (;;) { 535117845Ssam x = m->busy_lock; 536117845Ssam if (VPB_SHARERS(x) > 1) { 537117845Ssam if (atomic_cmpset_int(&m->busy_lock, x, 538117845Ssam x - VPB_ONE_SHARER)) 539117845Ssam break; 540117845Ssam continue; 541117845Ssam } 542117845Ssam if ((x & VPB_BIT_WAITERS) == 0) { 543117845Ssam KASSERT(x == VPB_SHARERS_WORD(1), 544117845Ssam ("vm_page_sunbusy: invalid lock state")); 545117845Ssam if (atomic_cmpset_int(&m->busy_lock, 546117845Ssam VPB_SHARERS_WORD(1), VPB_UNBUSIED)) 547117845Ssam break; 548117845Ssam continue; 549117845Ssam } 550117845Ssam KASSERT(x == (VPB_SHARERS_WORD(1) | VPB_BIT_WAITERS), 551117845Ssam ("vm_page_sunbusy: invalid lock state for waiters")); 552117845Ssam 553117845Ssam vm_page_lock(m); 554117845Ssam if (!atomic_cmpset_int(&m->busy_lock, x, VPB_UNBUSIED)) { 555117845Ssam vm_page_unlock(m); 556117845Ssam continue; 557117845Ssam } 558117845Ssam wakeup(m); 559117845Ssam vm_page_unlock(m); 560117845Ssam break; 561117845Ssam } 562117845Ssam} 563117845Ssam 564117845Ssam/* 565117845Ssam * vm_page_busy_sleep: 566117845Ssam * 567117845Ssam * Sleep and release the page lock, using the page pointer as wchan. 568117845Ssam * This is used to implement the hard-path of busying mechanism. 569117845Ssam * 570117845Ssam * The given page must be locked. 571117845Ssam */ 572117845Ssamvoid 573117845Ssamvm_page_busy_sleep(vm_page_t m, const char *wmesg) 574117845Ssam{ 575117845Ssam u_int x; 576117845Ssam 577117845Ssam vm_page_lock_assert(m, MA_OWNED); 578117845Ssam 579117845Ssam x = m->busy_lock; 580117845Ssam if (x == VPB_UNBUSIED) { 581117845Ssam vm_page_unlock(m); 582117845Ssam return; 583117845Ssam } 584117845Ssam if ((x & VPB_BIT_WAITERS) == 0 && 585117845Ssam !atomic_cmpset_int(&m->busy_lock, x, x | VPB_BIT_WAITERS)) { 586117845Ssam vm_page_unlock(m); 587117845Ssam return; 588117845Ssam } 589117845Ssam msleep(m, vm_page_lockptr(m), PVM | PDROP, wmesg, 0); 590117845Ssam} 591117845Ssam 592117845Ssam/* 593117845Ssam * vm_page_trysbusy: 594117845Ssam * 595117845Ssam * Try to shared busy a page. 596117845Ssam * If the operation succeeds 1 is returned otherwise 0. 597117845Ssam * The operation never sleeps. 598117845Ssam */ 599117845Ssamint 600117845Ssamvm_page_trysbusy(vm_page_t m) 601117845Ssam{ 602117845Ssam u_int x; 603117845Ssam 604117845Ssam for (;;) { 605117845Ssam x = m->busy_lock; 606117845Ssam if ((x & VPB_BIT_SHARED) == 0) 607117845Ssam return (0); 608117845Ssam if (atomic_cmpset_acq_int(&m->busy_lock, x, x + VPB_ONE_SHARER)) 609117845Ssam return (1); 610117845Ssam } 611117845Ssam} 612117845Ssam 613117845Ssam/* 614117845Ssam * vm_page_xunbusy_hard: 615117845Ssam * 616117845Ssam * Called after the first try the exclusive unbusy of a page failed. 617117845Ssam * It is assumed that the waiters bit is on. 618117845Ssam */ 619117845Ssamvoid 620117845Ssamvm_page_xunbusy_hard(vm_page_t m) 621117845Ssam{ 622117845Ssam 623117845Ssam vm_page_assert_xbusied(m); 624117845Ssam 625117845Ssam vm_page_lock(m); 626117845Ssam atomic_store_rel_int(&m->busy_lock, VPB_UNBUSIED); 627117845Ssam wakeup(m); 628117845Ssam vm_page_unlock(m); 629117845Ssam} 630117845Ssam 631117845Ssam/* 632117845Ssam * vm_page_flash: 633117845Ssam * 634117845Ssam * Wakeup anyone waiting for the page. 635117845Ssam * The ownership bits do not change. 636117845Ssam * 637117845Ssam * The given page must be locked. 638117845Ssam */ 639117845Ssamvoid 640117845Ssamvm_page_flash(vm_page_t m) 641117845Ssam{ 642117845Ssam u_int x; 643159226Spjd 644159226Spjd vm_page_lock_assert(m, MA_OWNED); 645159226Spjd 646159226Spjd for (;;) { 647159226Spjd x = m->busy_lock; 648159226Spjd if ((x & VPB_BIT_WAITERS) == 0) 649159226Spjd return; 650159226Spjd if (atomic_cmpset_int(&m->busy_lock, x, 651159226Spjd x & (~VPB_BIT_WAITERS))) 652159226Spjd break; 653159226Spjd } 654159226Spjd wakeup(m); 655159226Spjd} 656159226Spjd 657159226Spjd/* 658159226Spjd * Keep page from being freed by the page daemon 659159226Spjd * much of the same effect as wiring, except much lower 660159226Spjd * overhead and should be used only for *very* temporary 661159226Spjd * holding ("wiring"). 662159226Spjd */ 663159226Spjdvoid 664159226Spjdvm_page_hold(vm_page_t mem) 665159226Spjd{ 666159226Spjd 667159226Spjd vm_page_lock_assert(mem, MA_OWNED); 668159226Spjd mem->hold_count++; 669159226Spjd} 670159232Spjd 671159226Spjdvoid 672159226Spjdvm_page_unhold(vm_page_t mem) 673159226Spjd{ 674159226Spjd 675159232Spjd vm_page_lock_assert(mem, MA_OWNED); 676159232Spjd KASSERT(mem->hold_count >= 1, ("vm_page_unhold: hold count < 0!!!")); 677159226Spjd --mem->hold_count; 678159226Spjd if (mem->hold_count == 0 && (mem->flags & PG_UNHOLDFREE) != 0) 679159226Spjd vm_page_free_toq(mem); 680159226Spjd} 681159226Spjd 682159226Spjd/* 683159226Spjd * vm_page_unhold_pages: 684159226Spjd * 685159226Spjd * Unhold each of the pages that is referenced by the given array. 686159232Spjd */ 687159226Spjdvoid 688159226Spjdvm_page_unhold_pages(vm_page_t *ma, int count) 689159226Spjd{ 690159226Spjd struct mtx *mtx, *new_mtx; 691159232Spjd 692159232Spjd mtx = NULL; 693159226Spjd for (; count != 0; count--) { 694159226Spjd /* 695159226Spjd * Avoid releasing and reacquiring the same page lock. 696159226Spjd */ 697159226Spjd new_mtx = vm_page_lockptr(*ma); 698159226Spjd if (mtx != new_mtx) { 699159226Spjd if (mtx != NULL) 700159226Spjd mtx_unlock(mtx); 701159226Spjd mtx = new_mtx; 702159226Spjd mtx_lock(mtx); 703159226Spjd } 704159226Spjd vm_page_unhold(*ma); 705159226Spjd ma++; 706159226Spjd } 707117845Ssam if (mtx != NULL) 708117845Ssam mtx_unlock(mtx); 709117845Ssam} 710117845Ssam 711117845Ssamvm_page_t 712117845SsamPHYS_TO_VM_PAGE(vm_paddr_t pa) 713117845Ssam{ 714117845Ssam vm_page_t m; 715117845Ssam 716117845Ssam#ifdef VM_PHYSSEG_SPARSE 717117845Ssam m = vm_phys_paddr_to_vm_page(pa); 718159226Spjd if (m == NULL) 719117845Ssam m = vm_phys_fictitious_to_vm_page(pa); 720117845Ssam return (m); 721117845Ssam#elif defined(VM_PHYSSEG_DENSE) 722117845Ssam long pi; 723117845Ssam 724117845Ssam pi = atop(pa); 725117845Ssam if (pi >= first_page && (pi - first_page) < vm_page_array_size) { 726117845Ssam m = &vm_page_array[pi - first_page]; 727117845Ssam return (m); 728117845Ssam } 729117845Ssam return (vm_phys_fictitious_to_vm_page(pa)); 730117845Ssam#else 731117845Ssam#error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined." 732117845Ssam#endif 733117845Ssam} 734117845Ssam 735117845Ssam/* 736117845Ssam * vm_page_getfake: 737117845Ssam * 738117845Ssam * Create a fictitious page with the specified physical address and 739117845Ssam * memory attribute. The memory attribute is the only the machine- 740117845Ssam * dependent aspect of a fictitious page that must be initialized. 741117845Ssam */ 742117845Ssamvm_page_t 743117845Ssamvm_page_getfake(vm_paddr_t paddr, vm_memattr_t memattr) 744117845Ssam{ 745117845Ssam vm_page_t m; 746117845Ssam 747117845Ssam m = uma_zalloc(fakepg_zone, M_WAITOK | M_ZERO); 748117845Ssam vm_page_initfake(m, paddr, memattr); 749117845Ssam return (m); 750117845Ssam} 751117845Ssam 752117845Ssamvoid 753117845Ssamvm_page_initfake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr) 754117845Ssam{ 755117845Ssam 756117845Ssam if ((m->flags & PG_FICTITIOUS) != 0) { 757117845Ssam /* 758117845Ssam * The page's memattr might have changed since the 759117845Ssam * previous initialization. Update the pmap to the 760117845Ssam * new memattr. 761117845Ssam */ 762117845Ssam goto memattr; 763117845Ssam } 764117845Ssam m->phys_addr = paddr; 765117845Ssam m->queue = PQ_NONE; 766117845Ssam /* Fictitious pages don't use "segind". */ 767117845Ssam m->flags = PG_FICTITIOUS; 768117845Ssam /* Fictitious pages don't use "order" or "pool". */ 769117845Ssam m->oflags = VPO_UNMANAGED; 770117845Ssam m->busy_lock = VPB_SINGLE_EXCLUSIVER; 771117845Ssam m->wire_count = 1; 772117845Ssam pmap_page_init(m); 773117845Ssammemattr: 774117845Ssam pmap_page_set_memattr(m, memattr); 775117845Ssam} 776117845Ssam 777117845Ssam/* 778117845Ssam * vm_page_putfake: 779117845Ssam * 780117845Ssam * Release a fictitious page. 781117845Ssam */ 782117845Ssamvoid 783117845Ssamvm_page_putfake(vm_page_t m) 784117845Ssam{ 785117845Ssam 786117845Ssam KASSERT((m->oflags & VPO_UNMANAGED) != 0, ("managed %p", m)); 787117845Ssam KASSERT((m->flags & PG_FICTITIOUS) != 0, 788117845Ssam ("vm_page_putfake: bad page %p", m)); 789117845Ssam uma_zfree(fakepg_zone, m); 790117845Ssam} 791117845Ssam 792117845Ssam/* 793117845Ssam * vm_page_updatefake: 794117845Ssam * 795117845Ssam * Update the given fictitious page to the specified physical address and 796117845Ssam * memory attribute. 797117845Ssam */ 798117845Ssamvoid 799117845Ssamvm_page_updatefake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr) 800117845Ssam{ 801117845Ssam 802159226Spjd KASSERT((m->flags & PG_FICTITIOUS) != 0, 803159226Spjd ("vm_page_updatefake: bad page %p", m)); 804117845Ssam m->phys_addr = paddr; 805117845Ssam pmap_page_set_memattr(m, memattr); 806117845Ssam} 807158705Spjd 808158705Spjd/* 809158705Spjd * vm_page_free: 810159233Spjd * 811158705Spjd * Free a page. 812159233Spjd */ 813158705Spjdvoid 814158705Spjdvm_page_free(vm_page_t m) 815159226Spjd{ 816159226Spjd 817117845Ssam m->flags &= ~PG_ZERO; 818117845Ssam vm_page_free_toq(m); 819117845Ssam} 820117845Ssam 821117845Ssam/* 822117845Ssam * vm_page_free_zero: 823117845Ssam * 824117845Ssam * Free a page to the zerod-pages queue 825117845Ssam */ 826117845Ssamvoid 827117845Ssamvm_page_free_zero(vm_page_t m) 828117845Ssam{ 829117845Ssam 830117845Ssam m->flags |= PG_ZERO; 831117845Ssam vm_page_free_toq(m); 832117845Ssam} 833117845Ssam 834117845Ssam/* 835117845Ssam * Unbusy and handle the page queueing for a page from the VOP_GETPAGES() 836117845Ssam * array which is not the request page. 837117845Ssam */ 838117845Ssamvoid 839117845Ssamvm_page_readahead_finish(vm_page_t m) 840117845Ssam{ 841117845Ssam 842117845Ssam if (m->valid != 0) { 843117845Ssam /* 844117845Ssam * Since the page is not the requested page, whether 845117845Ssam * it should be activated or deactivated is not 846117845Ssam * obvious. Empirical results have shown that 847117845Ssam * deactivating the page is usually the best choice, 848117845Ssam * unless the page is wanted by another thread. 849117845Ssam */ 850117845Ssam vm_page_lock(m); 851117845Ssam if ((m->busy_lock & VPB_BIT_WAITERS) != 0) 852117845Ssam vm_page_activate(m); 853117845Ssam else 854117845Ssam vm_page_deactivate(m); 855117845Ssam vm_page_unlock(m); 856117845Ssam vm_page_xunbusy(m); 857117845Ssam } else { 858117845Ssam /* 859117845Ssam * Free the completely invalid page. Such page state 860117845Ssam * occurs due to the short read operation which did 861117845Ssam * not covered our page at all, or in case when a read 862117845Ssam * error happens. 863117845Ssam */ 864117845Ssam vm_page_lock(m); 865117845Ssam vm_page_free(m); 866117845Ssam vm_page_unlock(m); 867117845Ssam } 868117845Ssam} 869117845Ssam 870117845Ssam/* 871117845Ssam * vm_page_sleep_if_busy: 872117845Ssam * 873117845Ssam * Sleep and release the page queues lock if the page is busied. 874117845Ssam * Returns TRUE if the thread slept. 875117845Ssam * 876117845Ssam * The given page must be unlocked and object containing it must 877117845Ssam * be locked. 878117845Ssam */ 879117845Ssamint 880117845Ssamvm_page_sleep_if_busy(vm_page_t m, const char *msg) 881117845Ssam{ 882117845Ssam vm_object_t obj; 883117845Ssam 884117845Ssam vm_page_lock_assert(m, MA_NOTOWNED); 885117845Ssam VM_OBJECT_ASSERT_WLOCKED(m->object); 886117845Ssam 887117845Ssam if (vm_page_busied(m)) { 888117845Ssam /* 889117845Ssam * The page-specific object must be cached because page 890117845Ssam * identity can change during the sleep, causing the 891117845Ssam * re-lock of a different object. 892117845Ssam * It is assumed that a reference to the object is already 893117845Ssam * held by the callers. 894117845Ssam */ 895117845Ssam obj = m->object; 896117845Ssam vm_page_lock(m); 897117845Ssam VM_OBJECT_WUNLOCK(obj); 898117845Ssam vm_page_busy_sleep(m, msg); 899117845Ssam VM_OBJECT_WLOCK(obj); 900117845Ssam return (TRUE); 901117845Ssam } 902117845Ssam return (FALSE); 903117845Ssam} 904117845Ssam 905117845Ssam/* 906117845Ssam * vm_page_dirty_KBI: [ internal use only ] 907117845Ssam * 908117845Ssam * Set all bits in the page's dirty field. 909117845Ssam * 910117845Ssam * The object containing the specified page must be locked if the 911117845Ssam * call is made from the machine-independent layer. 912117845Ssam * 913117845Ssam * See vm_page_clear_dirty_mask(). 914117845Ssam * 915117845Ssam * This function should only be called by vm_page_dirty(). 916117845Ssam */ 917117845Ssamvoid 918117845Ssamvm_page_dirty_KBI(vm_page_t m) 919117845Ssam{ 920117845Ssam 921117845Ssam /* These assertions refer to this operation by its public name. */ 922117845Ssam KASSERT((m->flags & PG_CACHED) == 0, 923117845Ssam ("vm_page_dirty: page in cache!")); 924117845Ssam KASSERT(m->valid == VM_PAGE_BITS_ALL, 925117845Ssam ("vm_page_dirty: page is invalid!")); 926117845Ssam m->dirty = VM_PAGE_BITS_ALL; 927117845Ssam} 928117845Ssam 929117845Ssam/* 930117845Ssam * vm_page_insert: [ internal use only ] 931117845Ssam * 932117845Ssam * Inserts the given mem entry into the object and object list. 933117845Ssam * 934117845Ssam * The object must be locked. 935117845Ssam */ 936117845Ssamint 937117845Ssamvm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex) 938117845Ssam{ 939117845Ssam vm_page_t mpred; 940117845Ssam 941117845Ssam VM_OBJECT_ASSERT_WLOCKED(object); 942117845Ssam mpred = vm_radix_lookup_le(&object->rtree, pindex); 943117845Ssam return (vm_page_insert_after(m, object, pindex, mpred)); 944117845Ssam} 945117845Ssam 946117845Ssam/* 947117845Ssam * vm_page_insert_after: 948117845Ssam * 949117845Ssam * Inserts the page "m" into the specified object at offset "pindex". 950117845Ssam * 951117845Ssam * The page "mpred" must immediately precede the offset "pindex" within 952117845Ssam * the specified object. 953117845Ssam * 954117845Ssam * The object must be locked. 955117845Ssam */ 956117845Ssamstatic int 957117845Ssamvm_page_insert_after(vm_page_t m, vm_object_t object, vm_pindex_t pindex, 958117845Ssam vm_page_t mpred) 959117845Ssam{ 960117845Ssam vm_pindex_t sidx; 961117845Ssam vm_object_t sobj; 962117845Ssam vm_page_t msucc; 963117845Ssam 964117845Ssam VM_OBJECT_ASSERT_WLOCKED(object); 965117845Ssam KASSERT(m->object == NULL, 966117845Ssam ("vm_page_insert_after: page already inserted")); 967117845Ssam if (mpred != NULL) { 968117845Ssam KASSERT(mpred->object == object, 969117845Ssam ("vm_page_insert_after: object doesn't contain mpred")); 970117845Ssam KASSERT(mpred->pindex < pindex, 971117845Ssam ("vm_page_insert_after: mpred doesn't precede pindex")); 972117845Ssam msucc = TAILQ_NEXT(mpred, listq); 973117845Ssam } else 974117845Ssam msucc = TAILQ_FIRST(&object->memq); 975117845Ssam if (msucc != NULL) 976159226Spjd KASSERT(msucc->pindex > pindex, 977159226Spjd ("vm_page_insert_after: msucc doesn't succeed pindex")); 978159226Spjd 979117845Ssam /* 980117845Ssam * Record the object/offset pair in this page 981117845Ssam */ 982117845Ssam sobj = m->object; 983117845Ssam sidx = m->pindex; 984117845Ssam m->object = object; 985117845Ssam m->pindex = pindex; 986117845Ssam 987117845Ssam /* 988117845Ssam * Now link into the object's ordered list of backed pages. 989117845Ssam */ 990117845Ssam if (vm_radix_insert(&object->rtree, m)) { 991117845Ssam m->object = sobj; 992117845Ssam m->pindex = sidx; 993117845Ssam return (1); 994117845Ssam } 995117845Ssam vm_page_insert_radixdone(m, object, mpred); 996117845Ssam return (0); 997117845Ssam} 998117845Ssam 999117845Ssam/* 1000117845Ssam * vm_page_insert_radixdone: 1001117845Ssam * 1002117845Ssam * Complete page "m" insertion into the specified object after the 1003117845Ssam * radix trie hooking. 1004117845Ssam * 1005117845Ssam * The page "mpred" must precede the offset "m->pindex" within the 1006117845Ssam * specified object. 1007117845Ssam * 1008117845Ssam * The object must be locked. 1009117845Ssam */ 1010117845Ssamstatic void 1011117845Ssamvm_page_insert_radixdone(vm_page_t m, vm_object_t object, vm_page_t mpred) 1012117845Ssam{ 1013117845Ssam 1014117845Ssam VM_OBJECT_ASSERT_WLOCKED(object); 1015117845Ssam KASSERT(object != NULL && m->object == object, 1016117845Ssam ("vm_page_insert_radixdone: page %p has inconsistent object", m)); 1017117845Ssam if (mpred != NULL) { 1018117845Ssam KASSERT(mpred->object == object, 1019159242Spjd ("vm_page_insert_after: object doesn't contain mpred")); 1020159242Spjd KASSERT(mpred->pindex < m->pindex, 1021117845Ssam ("vm_page_insert_after: mpred doesn't precede pindex")); 1022117845Ssam } 1023117845Ssam 1024117845Ssam if (mpred != NULL) 1025117845Ssam TAILQ_INSERT_AFTER(&object->memq, mpred, m, listq); 1026117845Ssam else 1027117845Ssam TAILQ_INSERT_HEAD(&object->memq, m, listq); 1028159242Spjd 1029117845Ssam /* 1030117845Ssam * Show that the object has one more resident page. 1031159242Spjd */ 1032159242Spjd object->resident_page_count++; 1033159242Spjd 1034159242Spjd /* 1035159242Spjd * Hold the vnode until the last page is released. 1036117845Ssam */ 1037117845Ssam if (object->resident_page_count == 1 && object->type == OBJT_VNODE) 1038117845Ssam vhold(object->handle); 1039117845Ssam 1040117845Ssam /* 1041117845Ssam * Since we are inserting a new and possibly dirty page, 1042117845Ssam * update the object's OBJ_MIGHTBEDIRTY flag. 1043117845Ssam */ 1044117845Ssam if (pmap_page_is_write_mapped(m)) 1045117845Ssam vm_object_set_writeable_dirty(object); 1046117845Ssam} 1047117845Ssam 1048117845Ssam/* 1049117845Ssam * vm_page_remove: 1050117845Ssam * 1051117845Ssam * Removes the given mem entry from the object/offset-page 1052117845Ssam * table and the object page list, but do not invalidate/terminate 1053159226Spjd * the backing store. 1054159226Spjd * 1055159226Spjd * The object must be locked. The page must be locked if it is managed. 1056159226Spjd */ 1057159226Spjdvoid 1058117845Ssamvm_page_remove(vm_page_t m) 1059117845Ssam{ 1060117845Ssam vm_object_t object; 1061117845Ssam boolean_t lockacq; 1062117845Ssam 1063117845Ssam if ((m->oflags & VPO_UNMANAGED) == 0) 1064117845Ssam vm_page_lock_assert(m, MA_OWNED); 1065117845Ssam if ((object = m->object) == NULL) 1066117845Ssam return; 1067117845Ssam VM_OBJECT_ASSERT_WLOCKED(object); 1068117845Ssam if (vm_page_xbusied(m)) { 1069117845Ssam lockacq = FALSE; 1070117845Ssam if ((m->oflags & VPO_UNMANAGED) != 0 && 1071117845Ssam !mtx_owned(vm_page_lockptr(m))) { 1072117845Ssam lockacq = TRUE; 1073117845Ssam vm_page_lock(m); 1074117845Ssam } 1075117845Ssam vm_page_flash(m); 1076117845Ssam atomic_store_rel_int(&m->busy_lock, VPB_UNBUSIED); 1077117845Ssam if (lockacq) 1078117845Ssam vm_page_unlock(m); 1079117845Ssam } 1080117845Ssam 1081117845Ssam /* 1082117845Ssam * Now remove from the object's list of backed pages. 1083117845Ssam */ 1084117845Ssam vm_radix_remove(&object->rtree, m->pindex); 1085117845Ssam TAILQ_REMOVE(&object->memq, m, listq); 1086117845Ssam 1087117845Ssam /* 1088117845Ssam * And show that the object has one fewer resident page. 1089117845Ssam */ 1090117845Ssam object->resident_page_count--; 1091117845Ssam 1092117845Ssam /* 1093117845Ssam * The vnode may now be recycled. 1094117845Ssam */ 1095117845Ssam if (object->resident_page_count == 0 && object->type == OBJT_VNODE) 1096117845Ssam vdrop(object->handle); 1097117845Ssam 1098117845Ssam m->object = NULL; 1099117845Ssam} 1100117845Ssam 1101117845Ssam/* 1102117845Ssam * vm_page_lookup: 1103117845Ssam * 1104117845Ssam * Returns the page associated with the object/offset 1105117845Ssam * pair specified; if none is found, NULL is returned. 1106117845Ssam * 1107117845Ssam * The object must be locked. 1108117845Ssam */ 1109117845Ssamvm_page_t 1110117845Ssamvm_page_lookup(vm_object_t object, vm_pindex_t pindex) 1111117845Ssam{ 1112117845Ssam 1113117845Ssam VM_OBJECT_ASSERT_LOCKED(object); 1114117845Ssam return (vm_radix_lookup(&object->rtree, pindex)); 1115117845Ssam} 1116117845Ssam 1117117845Ssam/* 1118117845Ssam * vm_page_find_least: 1119117845Ssam * 1120117845Ssam * Returns the page associated with the object with least pindex 1121117845Ssam * greater than or equal to the parameter pindex, or NULL. 1122117845Ssam * 1123117845Ssam * The object must be locked. 1124117845Ssam */ 1125117845Ssamvm_page_t 1126117845Ssamvm_page_find_least(vm_object_t object, vm_pindex_t pindex) 1127117845Ssam{ 1128117845Ssam vm_page_t m; 1129117845Ssam 1130117845Ssam VM_OBJECT_ASSERT_LOCKED(object); 1131117845Ssam if ((m = TAILQ_FIRST(&object->memq)) != NULL && m->pindex < pindex) 1132117845Ssam m = vm_radix_lookup_ge(&object->rtree, pindex); 1133117845Ssam return (m); 1134117845Ssam} 1135117845Ssam 1136117845Ssam/* 1137117845Ssam * Returns the given page's successor (by pindex) within the object if it is 1138117845Ssam * resident; if none is found, NULL is returned. 1139117845Ssam * 1140117845Ssam * The object must be locked. 1141117845Ssam */ 1142117845Ssamvm_page_t 1143117845Ssamvm_page_next(vm_page_t m) 1144117845Ssam{ 1145117845Ssam vm_page_t next; 1146117845Ssam 1147117845Ssam VM_OBJECT_ASSERT_WLOCKED(m->object); 1148117845Ssam if ((next = TAILQ_NEXT(m, listq)) != NULL && 1149117845Ssam next->pindex != m->pindex + 1) 1150117845Ssam next = NULL; 1151117845Ssam return (next); 1152117845Ssam} 1153117845Ssam 1154117845Ssam/* 1155117845Ssam * Returns the given page's predecessor (by pindex) within the object if it is 1156117845Ssam * resident; if none is found, NULL is returned. 1157117845Ssam * 1158117845Ssam * The object must be locked. 1159117845Ssam */ 1160117845Ssamvm_page_t 1161117845Ssamvm_page_prev(vm_page_t m) 1162117845Ssam{ 1163117845Ssam vm_page_t prev; 1164117845Ssam 1165117845Ssam VM_OBJECT_ASSERT_WLOCKED(m->object); 1166117845Ssam if ((prev = TAILQ_PREV(m, pglist, listq)) != NULL && 1167117845Ssam prev->pindex != m->pindex - 1) 1168117845Ssam prev = NULL; 1169117845Ssam return (prev); 1170117845Ssam} 1171117845Ssam 1172117845Ssam/* 1173117845Ssam * Uses the page mnew as a replacement for an existing page at index 1174117845Ssam * pindex which must be already present in the object. 1175117845Ssam * 1176117845Ssam * The existing page must not be on a paging queue. 1177117845Ssam */ 1178117845Ssamvm_page_t 1179117845Ssamvm_page_replace(vm_page_t mnew, vm_object_t object, vm_pindex_t pindex) 1180117845Ssam{ 1181117845Ssam vm_page_t mold, mpred; 1182117845Ssam 1183117845Ssam VM_OBJECT_ASSERT_WLOCKED(object); 1184117845Ssam 1185117845Ssam /* 1186117845Ssam * This function mostly follows vm_page_insert() and 1187117845Ssam * vm_page_remove() without the radix, object count and vnode 1188117845Ssam * dance. Double check such functions for more comments. 1189117845Ssam */ 1190117845Ssam mpred = vm_radix_lookup(&object->rtree, pindex); 1191117845Ssam KASSERT(mpred != NULL, 1192117845Ssam ("vm_page_replace: replacing page not present with pindex")); 1193117845Ssam mpred = TAILQ_PREV(mpred, respgs, listq); 1194117845Ssam if (mpred != NULL) 1195117845Ssam KASSERT(mpred->pindex < pindex, 1196117845Ssam ("vm_page_insert_after: mpred doesn't precede pindex")); 1197117845Ssam 1198117845Ssam mnew->object = object; 1199117845Ssam mnew->pindex = pindex; 1200117845Ssam mold = vm_radix_replace(&object->rtree, mnew); 1201117845Ssam KASSERT(mold->queue == PQ_NONE, 1202117845Ssam ("vm_page_replace: mold is on a paging queue")); 1203117845Ssam 1204117845Ssam /* Detach the old page from the resident tailq. */ 1205117845Ssam TAILQ_REMOVE(&object->memq, mold, listq); 1206117845Ssam 1207117845Ssam mold->object = NULL; 1208117845Ssam vm_page_xunbusy(mold); 1209117845Ssam 1210117845Ssam /* Insert the new page in the resident tailq. */ 1211117845Ssam if (mpred != NULL) 1212117845Ssam TAILQ_INSERT_AFTER(&object->memq, mpred, mnew, listq); 1213117845Ssam else 1214117845Ssam TAILQ_INSERT_HEAD(&object->memq, mnew, listq); 1215117845Ssam if (pmap_page_is_write_mapped(mnew)) 1216117845Ssam vm_object_set_writeable_dirty(object); 1217117845Ssam return (mold); 1218117845Ssam} 1219117845Ssam 1220117845Ssam/* 1221117845Ssam * vm_page_rename: 1222117845Ssam * 1223117845Ssam * Move the given memory entry from its 1224117845Ssam * current object to the specified target object/offset. 1225117845Ssam * 1226117845Ssam * Note: swap associated with the page must be invalidated by the move. We 1227117845Ssam * have to do this for several reasons: (1) we aren't freeing the 1228117845Ssam * page, (2) we are dirtying the page, (3) the VM system is probably 1229117845Ssam * moving the page from object A to B, and will then later move 1230117845Ssam * the backing store from A to B and we can't have a conflict. 1231117845Ssam * 1232117845Ssam * Note: we *always* dirty the page. It is necessary both for the 1233117845Ssam * fact that we moved it, and because we may be invalidating 1234117845Ssam * swap. If the page is on the cache, we have to deactivate it 1235117845Ssam * or vm_page_dirty() will panic. Dirty pages are not allowed 1236117845Ssam * on the cache. 1237117845Ssam * 1238117845Ssam * The objects must be locked. 1239117845Ssam */ 1240117845Ssamint 1241117845Ssamvm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex) 1242117845Ssam{ 1243117845Ssam vm_page_t mpred; 1244117845Ssam vm_pindex_t opidx; 1245117845Ssam 1246117845Ssam VM_OBJECT_ASSERT_WLOCKED(new_object); 1247117845Ssam 1248117845Ssam mpred = vm_radix_lookup_le(&new_object->rtree, new_pindex); 1249117845Ssam KASSERT(mpred == NULL || mpred->pindex != new_pindex, 1250117845Ssam ("vm_page_rename: pindex already renamed")); 1251117845Ssam 1252117845Ssam /* 1253117845Ssam * Create a custom version of vm_page_insert() which does not depend 1254117845Ssam * by m_prev and can cheat on the implementation aspects of the 1255117845Ssam * function. 1256117845Ssam */ 1257117845Ssam opidx = m->pindex; 1258117845Ssam m->pindex = new_pindex; 1259117845Ssam if (vm_radix_insert(&new_object->rtree, m)) { 1260117845Ssam m->pindex = opidx; 1261117845Ssam return (1); 1262117845Ssam } 1263117845Ssam 1264117845Ssam /* 1265117845Ssam * The operation cannot fail anymore. The removal must happen before 1266117845Ssam * the listq iterator is tainted. 1267117845Ssam */ 1268117845Ssam m->pindex = opidx; 1269117845Ssam vm_page_lock(m); 1270117845Ssam vm_page_remove(m); 1271117845Ssam 1272117845Ssam /* Return back to the new pindex to complete vm_page_insert(). */ 1273117845Ssam m->pindex = new_pindex; 1274117845Ssam m->object = new_object; 1275117845Ssam vm_page_unlock(m); 1276118882Ssam vm_page_insert_radixdone(m, new_object, mpred); 1277118882Ssam vm_page_dirty(m); 1278117845Ssam return (0); 1279117845Ssam} 1280117845Ssam 1281117845Ssam/* 1282117845Ssam * Convert all of the given object's cached pages that have a 1283117845Ssam * pindex within the given range into free pages. If the value 1284117845Ssam * zero is given for "end", then the range's upper bound is 1285117845Ssam * infinity. If the given object is backed by a vnode and it 1286117845Ssam * transitions from having one or more cached pages to none, the 1287117845Ssam * vnode's hold count is reduced. 1288117845Ssam */ 1289117845Ssamvoid 1290117845Ssamvm_page_cache_free(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 1291117845Ssam{ 1292117845Ssam vm_page_t m; 1293117845Ssam boolean_t empty; 1294117845Ssam 1295117845Ssam mtx_lock(&vm_page_queue_free_mtx); 1296117845Ssam if (__predict_false(vm_radix_is_empty(&object->cache))) { 1297117845Ssam mtx_unlock(&vm_page_queue_free_mtx); 1298117845Ssam return; 1299117845Ssam } 1300117845Ssam while ((m = vm_radix_lookup_ge(&object->cache, start)) != NULL) { 1301117845Ssam if (end != 0 && m->pindex >= end) 1302117845Ssam break; 1303117845Ssam vm_radix_remove(&object->cache, m->pindex); 1304117845Ssam vm_page_cache_turn_free(m); 1305117845Ssam } 1306117845Ssam empty = vm_radix_is_empty(&object->cache); 1307117845Ssam mtx_unlock(&vm_page_queue_free_mtx); 1308117845Ssam if (object->type == OBJT_VNODE && empty) 1309117845Ssam vdrop(object->handle); 1310117845Ssam} 1311117845Ssam 1312117845Ssam/* 1313117845Ssam * Returns the cached page that is associated with the given 1314117845Ssam * object and offset. If, however, none exists, returns NULL. 1315117845Ssam * 1316117845Ssam * The free page queue must be locked. 1317117845Ssam */ 1318117845Ssamstatic inline vm_page_t 1319117845Ssamvm_page_cache_lookup(vm_object_t object, vm_pindex_t pindex) 1320117845Ssam{ 1321117845Ssam 1322117845Ssam mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 1323117845Ssam return (vm_radix_lookup(&object->cache, pindex)); 1324117845Ssam} 1325117845Ssam 1326117845Ssam/* 1327117845Ssam * Remove the given cached page from its containing object's 1328117845Ssam * collection of cached pages. 1329117845Ssam * 1330117845Ssam * The free page queue must be locked. 1331117845Ssam */ 1332117845Ssamstatic void 1333117845Ssamvm_page_cache_remove(vm_page_t m) 1334117845Ssam{ 1335117845Ssam 1336117845Ssam mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 1337117845Ssam KASSERT((m->flags & PG_CACHED) != 0, 1338117845Ssam ("vm_page_cache_remove: page %p is not cached", m)); 1339117845Ssam vm_radix_remove(&m->object->cache, m->pindex); 1340117845Ssam m->object = NULL; 1341117845Ssam vm_cnt.v_cache_count--; 1342117845Ssam} 1343117845Ssam 1344117845Ssam/* 1345117845Ssam * Transfer all of the cached pages with offset greater than or 1346117845Ssam * equal to 'offidxstart' from the original object's cache to the 1347117845Ssam * new object's cache. However, any cached pages with offset 1348117845Ssam * greater than or equal to the new object's size are kept in the 1349117845Ssam * original object. Initially, the new object's cache must be 1350117845Ssam * empty. Offset 'offidxstart' in the original object must 1351117845Ssam * correspond to offset zero in the new object. 1352117845Ssam * 1353117845Ssam * The new object must be locked. 1354117845Ssam */ 1355117845Ssamvoid 1356117845Ssamvm_page_cache_transfer(vm_object_t orig_object, vm_pindex_t offidxstart, 1357117845Ssam vm_object_t new_object) 1358117845Ssam{ 1359117845Ssam vm_page_t m; 1360117845Ssam 1361117845Ssam /* 1362117845Ssam * Insertion into an object's collection of cached pages 1363117845Ssam * requires the object to be locked. In contrast, removal does 1364117845Ssam * not. 1365117845Ssam */ 1366117845Ssam VM_OBJECT_ASSERT_WLOCKED(new_object); 1367117845Ssam KASSERT(vm_radix_is_empty(&new_object->cache), 1368117845Ssam ("vm_page_cache_transfer: object %p has cached pages", 1369117845Ssam new_object)); 1370117845Ssam mtx_lock(&vm_page_queue_free_mtx); 1371117845Ssam while ((m = vm_radix_lookup_ge(&orig_object->cache, 1372117845Ssam offidxstart)) != NULL) { 1373117845Ssam /* 1374117845Ssam * Transfer all of the pages with offset greater than or 1375117845Ssam * equal to 'offidxstart' from the original object's 1376117845Ssam * cache to the new object's cache. 1377117845Ssam */ 1378117845Ssam if ((m->pindex - offidxstart) >= new_object->size) 1379117845Ssam break; 1380117845Ssam vm_radix_remove(&orig_object->cache, m->pindex); 1381117845Ssam /* Update the page's object and offset. */ 1382117845Ssam m->object = new_object; 1383117845Ssam m->pindex -= offidxstart; 1384117845Ssam if (vm_radix_insert(&new_object->cache, m)) 1385117845Ssam vm_page_cache_turn_free(m); 1386117845Ssam } 1387117845Ssam mtx_unlock(&vm_page_queue_free_mtx); 1388117845Ssam} 1389117845Ssam 1390117845Ssam/* 1391117845Ssam * Returns TRUE if a cached page is associated with the given object and 1392117845Ssam * offset, and FALSE otherwise. 1393117845Ssam * 1394117845Ssam * The object must be locked. 1395117845Ssam */ 1396117845Ssamboolean_t 1397117845Ssamvm_page_is_cached(vm_object_t object, vm_pindex_t pindex) 1398117845Ssam{ 1399117845Ssam vm_page_t m; 1400117845Ssam 1401117845Ssam /* 1402117845Ssam * Insertion into an object's collection of cached pages requires the 1403117845Ssam * object to be locked. Therefore, if the object is locked and the 1404117845Ssam * object's collection is empty, there is no need to acquire the free 1405117845Ssam * page queues lock in order to prove that the specified page doesn't 1406117845Ssam * exist. 1407117845Ssam */ 1408117845Ssam VM_OBJECT_ASSERT_WLOCKED(object); 1409117845Ssam if (__predict_true(vm_object_cache_is_empty(object))) 1410117845Ssam return (FALSE); 1411117845Ssam mtx_lock(&vm_page_queue_free_mtx); 1412117845Ssam m = vm_page_cache_lookup(object, pindex); 1413117845Ssam mtx_unlock(&vm_page_queue_free_mtx); 1414117845Ssam return (m != NULL); 1415117845Ssam} 1416117845Ssam 1417117845Ssam/* 1418117845Ssam * vm_page_alloc: 1419117845Ssam * 1420117845Ssam * Allocate and return a page that is associated with the specified 1421117845Ssam * object and offset pair. By default, this page is exclusive busied. 1422117845Ssam * 1423117845Ssam * The caller must always specify an allocation class. 1424117845Ssam * 1425117845Ssam * allocation classes: 1426117845Ssam * VM_ALLOC_NORMAL normal process request 1427117845Ssam * VM_ALLOC_SYSTEM system *really* needs a page 1428117845Ssam * VM_ALLOC_INTERRUPT interrupt time request 1429117845Ssam * 1430117845Ssam * optional allocation flags: 1431117845Ssam * VM_ALLOC_COUNT(number) the number of additional pages that the caller 1432117845Ssam * intends to allocate 1433117845Ssam * VM_ALLOC_IFCACHED return page only if it is cached 1434117845Ssam * VM_ALLOC_IFNOTCACHED return NULL, do not reactivate if the page 1435117845Ssam * is cached 1436117845Ssam * VM_ALLOC_NOBUSY do not exclusive busy the page 1437117845Ssam * VM_ALLOC_NODUMP do not include the page in a kernel core dump 1438117845Ssam * VM_ALLOC_NOOBJ page is not associated with an object and 1439117845Ssam * should not be exclusive busy 1440117845Ssam * VM_ALLOC_SBUSY shared busy the allocated page 1441117845Ssam * VM_ALLOC_WIRED wire the allocated page 1442117845Ssam * VM_ALLOC_ZERO prefer a zeroed page 1443117845Ssam * 1444117845Ssam * This routine may not sleep. 1445117845Ssam */ 1446117845Ssamvm_page_t 1447117845Ssamvm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req) 1448117845Ssam{ 1449117845Ssam struct vnode *vp = NULL; 1450117845Ssam vm_object_t m_object; 1451117845Ssam vm_page_t m, mpred; 1452117845Ssam int flags, req_class; 1453117845Ssam 1454117845Ssam mpred = 0; /* XXX: pacify gcc */ 1455117845Ssam KASSERT((object != NULL) == ((req & VM_ALLOC_NOOBJ) == 0) && 1456117845Ssam (object != NULL || (req & VM_ALLOC_SBUSY) == 0) && 1457117845Ssam ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) != 1458117845Ssam (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)), 1459117845Ssam ("vm_page_alloc: inconsistent object(%p)/req(%x)", (void *)object, 1460117845Ssam req)); 1461117845Ssam if (object != NULL) 1462117845Ssam VM_OBJECT_ASSERT_WLOCKED(object); 1463117845Ssam 1464117845Ssam req_class = req & VM_ALLOC_CLASS_MASK; 1465117845Ssam 1466117845Ssam /* 1467117845Ssam * The page daemon is allowed to dig deeper into the free page list. 1468117845Ssam */ 1469117845Ssam if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT) 1470117845Ssam req_class = VM_ALLOC_SYSTEM; 1471117845Ssam 1472117845Ssam if (object != NULL) { 1473117845Ssam mpred = vm_radix_lookup_le(&object->rtree, pindex); 1474117845Ssam KASSERT(mpred == NULL || mpred->pindex != pindex, 1475117845Ssam ("vm_page_alloc: pindex already allocated")); 1476117845Ssam } 1477117845Ssam 1478117845Ssam /* 1479117845Ssam * The page allocation request can came from consumers which already 1480117845Ssam * hold the free page queue mutex, like vm_page_insert() in 1481117845Ssam * vm_page_cache(). 1482117845Ssam */ 1483117845Ssam mtx_lock_flags(&vm_page_queue_free_mtx, MTX_RECURSE); 1484117845Ssam if (vm_cnt.v_free_count + vm_cnt.v_cache_count > vm_cnt.v_free_reserved || 1485117845Ssam (req_class == VM_ALLOC_SYSTEM && 1486117845Ssam vm_cnt.v_free_count + vm_cnt.v_cache_count > vm_cnt.v_interrupt_free_min) || 1487117845Ssam (req_class == VM_ALLOC_INTERRUPT && 1488117845Ssam vm_cnt.v_free_count + vm_cnt.v_cache_count > 0)) { 1489117845Ssam /* 1490117845Ssam * Allocate from the free queue if the number of free pages 1491117845Ssam * exceeds the minimum for the request class. 1492117845Ssam */ 1493117845Ssam if (object != NULL && 1494117845Ssam (m = vm_page_cache_lookup(object, pindex)) != NULL) { 1495117845Ssam if ((req & VM_ALLOC_IFNOTCACHED) != 0) { 1496117845Ssam mtx_unlock(&vm_page_queue_free_mtx); 1497117845Ssam return (NULL); 1498117845Ssam } 1499117845Ssam if (vm_phys_unfree_page(m)) 1500117845Ssam vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m, 0); 1501117845Ssam#if VM_NRESERVLEVEL > 0 1502117845Ssam else if (!vm_reserv_reactivate_page(m)) 1503117845Ssam#else 1504117845Ssam else 1505117845Ssam#endif 1506117845Ssam panic("vm_page_alloc: cache page %p is missing" 1507117845Ssam " from the free queue", m); 1508117845Ssam } else if ((req & VM_ALLOC_IFCACHED) != 0) { 1509117845Ssam mtx_unlock(&vm_page_queue_free_mtx); 1510117845Ssam return (NULL); 1511117845Ssam#if VM_NRESERVLEVEL > 0 1512117845Ssam } else if (object == NULL || (object->flags & (OBJ_COLORED | 1513117845Ssam OBJ_FICTITIOUS)) != OBJ_COLORED || (m = 1514117845Ssam vm_reserv_alloc_page(object, pindex, mpred)) == NULL) { 1515117845Ssam#else 1516117845Ssam } else { 1517117845Ssam#endif 1518117845Ssam m = vm_phys_alloc_pages(object != NULL ? 1519117845Ssam VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT, 0); 1520117845Ssam#if VM_NRESERVLEVEL > 0 1521117845Ssam if (m == NULL && vm_reserv_reclaim_inactive()) { 1522117845Ssam m = vm_phys_alloc_pages(object != NULL ? 1523117845Ssam VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT, 1524117845Ssam 0); 1525117845Ssam } 1526117845Ssam#endif 1527117845Ssam } 1528117845Ssam } else { 1529117845Ssam /* 1530117845Ssam * Not allocatable, give up. 1531117845Ssam */ 1532117845Ssam mtx_unlock(&vm_page_queue_free_mtx); 1533117845Ssam atomic_add_int(&vm_pageout_deficit, 1534117845Ssam max((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1)); 1535117845Ssam pagedaemon_wakeup(); 1536117845Ssam return (NULL); 1537117845Ssam } 1538117845Ssam 1539117845Ssam /* 1540117845Ssam * At this point we had better have found a good page. 1541117845Ssam */ 1542117845Ssam KASSERT(m != NULL, ("vm_page_alloc: missing page")); 1543117845Ssam KASSERT(m->queue == PQ_NONE, 1544117845Ssam ("vm_page_alloc: page %p has unexpected queue %d", m, m->queue)); 1545117845Ssam KASSERT(m->wire_count == 0, ("vm_page_alloc: page %p is wired", m)); 1546117845Ssam KASSERT(m->hold_count == 0, ("vm_page_alloc: page %p is held", m)); 1547117845Ssam KASSERT(!vm_page_sbusied(m), 1548117845Ssam ("vm_page_alloc: page %p is busy", m)); 1549117845Ssam KASSERT(m->dirty == 0, ("vm_page_alloc: page %p is dirty", m)); 1550117845Ssam KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT, 1551117845Ssam ("vm_page_alloc: page %p has unexpected memattr %d", m, 1552117845Ssam pmap_page_get_memattr(m))); 1553117845Ssam if ((m->flags & PG_CACHED) != 0) { 1554117845Ssam KASSERT((m->flags & PG_ZERO) == 0, 1555117845Ssam ("vm_page_alloc: cached page %p is PG_ZERO", m)); 1556117845Ssam KASSERT(m->valid != 0, 1557117845Ssam ("vm_page_alloc: cached page %p is invalid", m)); 1558117845Ssam if (m->object == object && m->pindex == pindex) 1559117845Ssam vm_cnt.v_reactivated++; 1560117845Ssam else 1561117845Ssam m->valid = 0; 1562117845Ssam m_object = m->object; 1563159242Spjd vm_page_cache_remove(m); 1564159242Spjd if (m_object->type == OBJT_VNODE && 1565159242Spjd vm_object_cache_is_empty(m_object)) 1566117845Ssam vp = m_object->handle; 1567117845Ssam } else { 1568117845Ssam KASSERT(m->valid == 0, 1569117845Ssam ("vm_page_alloc: free page %p is valid", m)); 1570117845Ssam vm_phys_freecnt_adj(m, -1); 1571117845Ssam if ((m->flags & PG_ZERO) != 0) 1572117845Ssam vm_page_zero_count--; 1573117845Ssam } 1574117845Ssam mtx_unlock(&vm_page_queue_free_mtx); 1575117845Ssam 1576117845Ssam /* 1577117845Ssam * Initialize the page. Only the PG_ZERO flag is inherited. 1578117845Ssam */ 1579117845Ssam flags = 0; 1580117845Ssam if ((req & VM_ALLOC_ZERO) != 0) 1581117845Ssam flags = PG_ZERO; 1582117845Ssam flags &= m->flags; 1583117845Ssam if ((req & VM_ALLOC_NODUMP) != 0) 1584117845Ssam flags |= PG_NODUMP; 1585117845Ssam m->flags = flags; 1586159242Spjd m->aflags = 0; 1587159242Spjd m->oflags = object == NULL || (object->flags & OBJ_UNMANAGED) != 0 ? 1588159242Spjd VPO_UNMANAGED : 0; 1589159242Spjd m->busy_lock = VPB_UNBUSIED; 1590117845Ssam if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ | VM_ALLOC_SBUSY)) == 0) 1591117845Ssam m->busy_lock = VPB_SINGLE_EXCLUSIVER; 1592117845Ssam if ((req & VM_ALLOC_SBUSY) != 0) 1593117845Ssam m->busy_lock = VPB_SHARERS_WORD(1); 1594117845Ssam if (req & VM_ALLOC_WIRED) { 1595117845Ssam /* 1596117845Ssam * The page lock is not required for wiring a page until that 1597117845Ssam * page is inserted into the object. 1598117845Ssam */ 1599117845Ssam atomic_add_int(&vm_cnt.v_wire_count, 1); 1600117845Ssam m->wire_count = 1; 1601117845Ssam } 1602117845Ssam m->act_count = 0; 1603117845Ssam 1604117845Ssam if (object != NULL) { 1605117845Ssam if (vm_page_insert_after(m, object, pindex, mpred)) { 1606117845Ssam /* See the comment below about hold count. */ 1607117845Ssam if (vp != NULL) 1608117845Ssam vdrop(vp); 1609117845Ssam pagedaemon_wakeup(); 1610117845Ssam if (req & VM_ALLOC_WIRED) { 1611117845Ssam atomic_subtract_int(&vm_cnt.v_wire_count, 1); 1612117845Ssam m->wire_count = 0; 1613117845Ssam } 1614117845Ssam m->object = NULL; 1615117845Ssam vm_page_free(m); 1616117845Ssam return (NULL); 1617117845Ssam } 1618117845Ssam 1619117845Ssam /* Ignore device objects; the pager sets "memattr" for them. */ 1620117845Ssam if (object->memattr != VM_MEMATTR_DEFAULT && 1621117845Ssam (object->flags & OBJ_FICTITIOUS) == 0) 1622117845Ssam pmap_page_set_memattr(m, object->memattr); 1623117845Ssam } else 1624117845Ssam m->pindex = pindex; 1625117845Ssam 1626117845Ssam /* 1627117845Ssam * The following call to vdrop() must come after the above call 1628117845Ssam * to vm_page_insert() in case both affect the same object and 1629117845Ssam * vnode. Otherwise, the affected vnode's hold count could 1630117845Ssam * temporarily become zero. 1631117845Ssam */ 1632117845Ssam if (vp != NULL) 1633117845Ssam vdrop(vp); 1634117845Ssam 1635117845Ssam /* 1636117845Ssam * Don't wakeup too often - wakeup the pageout daemon when 1637117845Ssam * we would be nearly out of memory. 1638117845Ssam */ 1639117845Ssam if (vm_paging_needed()) 1640117845Ssam pagedaemon_wakeup(); 1641117845Ssam 1642117845Ssam return (m); 1643117845Ssam} 1644117845Ssam 1645117845Ssamstatic void 1646117845Ssamvm_page_alloc_contig_vdrop(struct spglist *lst) 1647117845Ssam{ 1648117845Ssam 1649117845Ssam while (!SLIST_EMPTY(lst)) { 1650117845Ssam vdrop((struct vnode *)SLIST_FIRST(lst)-> plinks.s.pv); 1651117845Ssam SLIST_REMOVE_HEAD(lst, plinks.s.ss); 1652117845Ssam } 1653117845Ssam} 1654117845Ssam 1655117845Ssam/* 1656117845Ssam * vm_page_alloc_contig: 1657117845Ssam * 1658117845Ssam * Allocate a contiguous set of physical pages of the given size "npages" 1659117845Ssam * from the free lists. All of the physical pages must be at or above 1660117845Ssam * the given physical address "low" and below the given physical address 1661117845Ssam * "high". The given value "alignment" determines the alignment of the 1662117845Ssam * first physical page in the set. If the given value "boundary" is 1663117845Ssam * non-zero, then the set of physical pages cannot cross any physical 1664117845Ssam * address boundary that is a multiple of that value. Both "alignment" 1665117845Ssam * and "boundary" must be a power of two. 1666117845Ssam * 1667117845Ssam * If the specified memory attribute, "memattr", is VM_MEMATTR_DEFAULT, 1668117845Ssam * then the memory attribute setting for the physical pages is configured 1669117845Ssam * to the object's memory attribute setting. Otherwise, the memory 1670117845Ssam * attribute setting for the physical pages is configured to "memattr", 1671117845Ssam * overriding the object's memory attribute setting. However, if the 1672117845Ssam * object's memory attribute setting is not VM_MEMATTR_DEFAULT, then the 1673117845Ssam * memory attribute setting for the physical pages cannot be configured 1674117845Ssam * to VM_MEMATTR_DEFAULT. 1675117845Ssam * 1676117845Ssam * The caller must always specify an allocation class. 1677117845Ssam * 1678117845Ssam * allocation classes: 1679117845Ssam * VM_ALLOC_NORMAL normal process request 1680117845Ssam * VM_ALLOC_SYSTEM system *really* needs a page 1681117845Ssam * VM_ALLOC_INTERRUPT interrupt time request 1682117845Ssam * 1683117845Ssam * optional allocation flags: 1684117845Ssam * VM_ALLOC_NOBUSY do not exclusive busy the page 1685117845Ssam * VM_ALLOC_NOOBJ page is not associated with an object and 1686117845Ssam * should not be exclusive busy 1687117845Ssam * VM_ALLOC_SBUSY shared busy the allocated page 1688117845Ssam * VM_ALLOC_WIRED wire the allocated page 1689117845Ssam * VM_ALLOC_ZERO prefer a zeroed page 1690117845Ssam * 1691117845Ssam * This routine may not sleep. 1692117845Ssam */ 1693117845Ssamvm_page_t 1694117845Ssamvm_page_alloc_contig(vm_object_t object, vm_pindex_t pindex, int req, 1695117845Ssam u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment, 1696117845Ssam vm_paddr_t boundary, vm_memattr_t memattr) 1697117845Ssam{ 1698117845Ssam struct vnode *drop; 1699117845Ssam struct spglist deferred_vdrop_list; 1700117845Ssam vm_page_t m, m_tmp, m_ret; 1701117845Ssam u_int flags; 1702117845Ssam int req_class; 1703117845Ssam 1704117845Ssam KASSERT((object != NULL) == ((req & VM_ALLOC_NOOBJ) == 0) && 1705117845Ssam (object != NULL || (req & VM_ALLOC_SBUSY) == 0) && 1706117845Ssam ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) != 1707117845Ssam (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)), 1708117845Ssam ("vm_page_alloc: inconsistent object(%p)/req(%x)", (void *)object, 1709117845Ssam req)); 1710117845Ssam if (object != NULL) { 1711117845Ssam VM_OBJECT_ASSERT_WLOCKED(object); 1712117845Ssam KASSERT(object->type == OBJT_PHYS, 1713117845Ssam ("vm_page_alloc_contig: object %p isn't OBJT_PHYS", 1714117845Ssam object)); 1715117845Ssam } 1716117845Ssam KASSERT(npages > 0, ("vm_page_alloc_contig: npages is zero")); 1717117845Ssam req_class = req & VM_ALLOC_CLASS_MASK; 1718117845Ssam 1719117845Ssam /* 1720117845Ssam * The page daemon is allowed to dig deeper into the free page list. 1721117845Ssam */ 1722117845Ssam if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT) 1723117845Ssam req_class = VM_ALLOC_SYSTEM; 1724117845Ssam 1725117845Ssam SLIST_INIT(&deferred_vdrop_list); 1726117845Ssam mtx_lock(&vm_page_queue_free_mtx); 1727117845Ssam if (vm_cnt.v_free_count + vm_cnt.v_cache_count >= npages + 1728117845Ssam vm_cnt.v_free_reserved || (req_class == VM_ALLOC_SYSTEM && 1729117845Ssam vm_cnt.v_free_count + vm_cnt.v_cache_count >= npages + 1730117845Ssam vm_cnt.v_interrupt_free_min) || (req_class == VM_ALLOC_INTERRUPT && 1731117845Ssam vm_cnt.v_free_count + vm_cnt.v_cache_count >= npages)) { 1732117845Ssam#if VM_NRESERVLEVEL > 0 1733117845Ssamretry: 1734117845Ssam if (object == NULL || (object->flags & OBJ_COLORED) == 0 || 1735117845Ssam (m_ret = vm_reserv_alloc_contig(object, pindex, npages, 1736117845Ssam low, high, alignment, boundary)) == NULL) 1737117845Ssam#endif 1738117845Ssam m_ret = vm_phys_alloc_contig(npages, low, high, 1739117845Ssam alignment, boundary); 1740117845Ssam } else { 1741117845Ssam mtx_unlock(&vm_page_queue_free_mtx); 1742117845Ssam atomic_add_int(&vm_pageout_deficit, npages); 1743117845Ssam pagedaemon_wakeup(); 1744117845Ssam return (NULL); 1745117845Ssam } 1746117845Ssam if (m_ret != NULL) 1747117845Ssam for (m = m_ret; m < &m_ret[npages]; m++) { 1748117845Ssam drop = vm_page_alloc_init(m); 1749117845Ssam if (drop != NULL) { 1750117845Ssam /* 1751117845Ssam * Enqueue the vnode for deferred vdrop(). 1752117845Ssam */ 1753117845Ssam m->plinks.s.pv = drop; 1754117845Ssam SLIST_INSERT_HEAD(&deferred_vdrop_list, m, 1755117845Ssam plinks.s.ss); 1756117845Ssam } 1757117845Ssam } 1758117845Ssam else { 1759117845Ssam#if VM_NRESERVLEVEL > 0 1760117845Ssam if (vm_reserv_reclaim_contig(npages, low, high, alignment, 1761117845Ssam boundary)) 1762117845Ssam goto retry; 1763117845Ssam#endif 1764117845Ssam } 1765117845Ssam mtx_unlock(&vm_page_queue_free_mtx); 1766117845Ssam if (m_ret == NULL) 1767117845Ssam return (NULL); 1768117845Ssam 1769117845Ssam /* 1770117845Ssam * Initialize the pages. Only the PG_ZERO flag is inherited. 1771117845Ssam */ 1772117845Ssam flags = 0; 1773117845Ssam if ((req & VM_ALLOC_ZERO) != 0) 1774117845Ssam flags = PG_ZERO; 1775117845Ssam if ((req & VM_ALLOC_NODUMP) != 0) 1776117845Ssam flags |= PG_NODUMP; 1777117845Ssam if ((req & VM_ALLOC_WIRED) != 0) 1778117845Ssam atomic_add_int(&vm_cnt.v_wire_count, npages); 1779117845Ssam if (object != NULL) { 1780117845Ssam if (object->memattr != VM_MEMATTR_DEFAULT && 1781117845Ssam memattr == VM_MEMATTR_DEFAULT) 1782117845Ssam memattr = object->memattr; 1783117845Ssam } 1784117845Ssam for (m = m_ret; m < &m_ret[npages]; m++) { 1785117845Ssam m->aflags = 0; 1786117845Ssam m->flags = (m->flags | PG_NODUMP) & flags; 1787117845Ssam m->busy_lock = VPB_UNBUSIED; 1788117845Ssam if (object != NULL) { 1789117845Ssam if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) == 0) 1790117845Ssam m->busy_lock = VPB_SINGLE_EXCLUSIVER; 1791117845Ssam if ((req & VM_ALLOC_SBUSY) != 0) 1792117845Ssam m->busy_lock = VPB_SHARERS_WORD(1); 1793117845Ssam } 1794117845Ssam if ((req & VM_ALLOC_WIRED) != 0) 1795117845Ssam m->wire_count = 1; 1796117845Ssam /* Unmanaged pages don't use "act_count". */ 1797117845Ssam m->oflags = VPO_UNMANAGED; 1798117845Ssam if (object != NULL) { 1799117845Ssam if (vm_page_insert(m, object, pindex)) { 1800117845Ssam vm_page_alloc_contig_vdrop( 1801117845Ssam &deferred_vdrop_list); 1802117845Ssam if (vm_paging_needed()) 1803117845Ssam pagedaemon_wakeup(); 1804117845Ssam if ((req & VM_ALLOC_WIRED) != 0) 1805117845Ssam atomic_subtract_int(&vm_cnt.v_wire_count, 1806117845Ssam npages); 1807117845Ssam for (m_tmp = m, m = m_ret; 1808117845Ssam m < &m_ret[npages]; m++) { 1809117845Ssam if ((req & VM_ALLOC_WIRED) != 0) 1810117845Ssam m->wire_count = 0; 1811117845Ssam if (m >= m_tmp) 1812117845Ssam m->object = NULL; 1813117845Ssam vm_page_free(m); 1814117845Ssam } 1815117845Ssam return (NULL); 1816117845Ssam } 1817117845Ssam } else 1818117845Ssam m->pindex = pindex; 1819117845Ssam if (memattr != VM_MEMATTR_DEFAULT) 1820117845Ssam pmap_page_set_memattr(m, memattr); 1821117845Ssam pindex++; 1822117845Ssam } 1823117845Ssam vm_page_alloc_contig_vdrop(&deferred_vdrop_list); 1824117845Ssam if (vm_paging_needed()) 1825117845Ssam pagedaemon_wakeup(); 1826117845Ssam return (m_ret); 1827117845Ssam} 1828117845Ssam 1829117845Ssam/* 1830117845Ssam * Initialize a page that has been freshly dequeued from a freelist. 1831117845Ssam * The caller has to drop the vnode returned, if it is not NULL. 1832117845Ssam * 1833117845Ssam * This function may only be used to initialize unmanaged pages. 1834117845Ssam * 1835117845Ssam * To be called with vm_page_queue_free_mtx held. 1836117845Ssam */ 1837117845Ssamstatic struct vnode * 1838117845Ssamvm_page_alloc_init(vm_page_t m) 1839117845Ssam{ 1840117845Ssam struct vnode *drop; 1841117845Ssam vm_object_t m_object; 1842117845Ssam 1843117845Ssam KASSERT(m->queue == PQ_NONE, 1844117845Ssam ("vm_page_alloc_init: page %p has unexpected queue %d", 1845117845Ssam m, m->queue)); 1846117845Ssam KASSERT(m->wire_count == 0, 1847117845Ssam ("vm_page_alloc_init: page %p is wired", m)); 1848117845Ssam KASSERT(m->hold_count == 0, 1849117845Ssam ("vm_page_alloc_init: page %p is held", m)); 1850117845Ssam KASSERT(!vm_page_sbusied(m), 1851117845Ssam ("vm_page_alloc_init: page %p is busy", m)); 1852117845Ssam KASSERT(m->dirty == 0, 1853117845Ssam ("vm_page_alloc_init: page %p is dirty", m)); 1854117845Ssam KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT, 1855117845Ssam ("vm_page_alloc_init: page %p has unexpected memattr %d", 1856117845Ssam m, pmap_page_get_memattr(m))); 1857117845Ssam mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 1858117845Ssam drop = NULL; 1859117845Ssam if ((m->flags & PG_CACHED) != 0) { 1860117845Ssam KASSERT((m->flags & PG_ZERO) == 0, 1861117845Ssam ("vm_page_alloc_init: cached page %p is PG_ZERO", m)); 1862117845Ssam m->valid = 0; 1863117845Ssam m_object = m->object; 1864117845Ssam vm_page_cache_remove(m); 1865117845Ssam if (m_object->type == OBJT_VNODE && 1866117845Ssam vm_object_cache_is_empty(m_object)) 1867117845Ssam drop = m_object->handle; 1868117845Ssam } else { 1869117845Ssam KASSERT(m->valid == 0, 1870117845Ssam ("vm_page_alloc_init: free page %p is valid", m)); 1871117845Ssam vm_phys_freecnt_adj(m, -1); 1872117845Ssam if ((m->flags & PG_ZERO) != 0) 1873117845Ssam vm_page_zero_count--; 1874117845Ssam } 1875117845Ssam return (drop); 1876117845Ssam} 1877117845Ssam 1878117845Ssam/* 1879117845Ssam * vm_page_alloc_freelist: 1880117845Ssam * 1881117845Ssam * Allocate a physical page from the specified free page list. 1882117845Ssam * 1883117845Ssam * The caller must always specify an allocation class. 1884117845Ssam * 1885117845Ssam * allocation classes: 1886117845Ssam * VM_ALLOC_NORMAL normal process request 1887117845Ssam * VM_ALLOC_SYSTEM system *really* needs a page 1888117845Ssam * VM_ALLOC_INTERRUPT interrupt time request 1889117845Ssam * 1890117845Ssam * optional allocation flags: 1891117845Ssam * VM_ALLOC_COUNT(number) the number of additional pages that the caller 1892117845Ssam * intends to allocate 1893117845Ssam * VM_ALLOC_WIRED wire the allocated page 1894117845Ssam * VM_ALLOC_ZERO prefer a zeroed page 1895117845Ssam * 1896117845Ssam * This routine may not sleep. 1897117845Ssam */ 1898117845Ssamvm_page_t 1899117845Ssamvm_page_alloc_freelist(int flind, int req) 1900117845Ssam{ 1901117845Ssam struct vnode *drop; 1902117845Ssam vm_page_t m; 1903117845Ssam u_int flags; 1904117845Ssam int req_class; 1905117845Ssam 1906117845Ssam req_class = req & VM_ALLOC_CLASS_MASK; 1907117845Ssam 1908117845Ssam /* 1909117845Ssam * The page daemon is allowed to dig deeper into the free page list. 1910117845Ssam */ 1911117845Ssam if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT) 1912117845Ssam req_class = VM_ALLOC_SYSTEM; 1913117845Ssam 1914117845Ssam /* 1915117845Ssam * Do not allocate reserved pages unless the req has asked for it. 1916117845Ssam */ 1917117845Ssam mtx_lock_flags(&vm_page_queue_free_mtx, MTX_RECURSE); 1918117845Ssam if (vm_cnt.v_free_count + vm_cnt.v_cache_count > vm_cnt.v_free_reserved || 1919117845Ssam (req_class == VM_ALLOC_SYSTEM && 1920117845Ssam vm_cnt.v_free_count + vm_cnt.v_cache_count > vm_cnt.v_interrupt_free_min) || 1921117845Ssam (req_class == VM_ALLOC_INTERRUPT && 1922117845Ssam vm_cnt.v_free_count + vm_cnt.v_cache_count > 0)) 1923117845Ssam m = vm_phys_alloc_freelist_pages(flind, VM_FREEPOOL_DIRECT, 0); 1924117845Ssam else { 1925117845Ssam mtx_unlock(&vm_page_queue_free_mtx); 1926117845Ssam atomic_add_int(&vm_pageout_deficit, 1927117845Ssam max((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1)); 1928117845Ssam pagedaemon_wakeup(); 1929117845Ssam return (NULL); 1930117845Ssam } 1931117845Ssam if (m == NULL) { 1932117845Ssam mtx_unlock(&vm_page_queue_free_mtx); 1933117845Ssam return (NULL); 1934117845Ssam } 1935117845Ssam drop = vm_page_alloc_init(m); 1936117845Ssam mtx_unlock(&vm_page_queue_free_mtx); 1937117845Ssam 1938117845Ssam /* 1939117845Ssam * Initialize the page. Only the PG_ZERO flag is inherited. 1940117845Ssam */ 1941117845Ssam m->aflags = 0; 1942117845Ssam flags = 0; 1943117845Ssam if ((req & VM_ALLOC_ZERO) != 0) 1944117845Ssam flags = PG_ZERO; 1945117845Ssam m->flags &= flags; 1946117845Ssam if ((req & VM_ALLOC_WIRED) != 0) { 1947117845Ssam /* 1948117845Ssam * The page lock is not required for wiring a page that does 1949117845Ssam * not belong to an object. 1950117845Ssam */ 1951117845Ssam atomic_add_int(&vm_cnt.v_wire_count, 1); 1952117845Ssam m->wire_count = 1; 1953117845Ssam } 1954117845Ssam /* Unmanaged pages don't use "act_count". */ 1955117845Ssam m->oflags = VPO_UNMANAGED; 1956117845Ssam if (drop != NULL) 1957117845Ssam vdrop(drop); 1958117845Ssam if (vm_paging_needed()) 1959117845Ssam pagedaemon_wakeup(); 1960117845Ssam return (m); 1961117845Ssam} 1962117845Ssam 1963117845Ssam/* 1964117845Ssam * vm_wait: (also see VM_WAIT macro) 1965117845Ssam * 1966117845Ssam * Sleep until free pages are available for allocation. 1967117845Ssam * - Called in various places before memory allocations. 1968117845Ssam */ 1969117845Ssamvoid 1970117845Ssamvm_wait(void) 1971117845Ssam{ 1972117845Ssam 1973117845Ssam mtx_lock(&vm_page_queue_free_mtx); 1974117845Ssam if (curproc == pageproc) { 1975117845Ssam vm_pageout_pages_needed = 1; 1976117845Ssam msleep(&vm_pageout_pages_needed, &vm_page_queue_free_mtx, 1977117845Ssam PDROP | PSWP, "VMWait", 0); 1978117845Ssam } else { 1979117845Ssam if (!vm_pages_needed) { 1980117845Ssam vm_pages_needed = 1; 1981117845Ssam wakeup(&vm_pages_needed); 1982117845Ssam } 1983117845Ssam msleep(&vm_cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PVM, 1984117845Ssam "vmwait", 0); 1985117845Ssam } 1986117845Ssam} 1987117845Ssam 1988117845Ssam/* 1989117845Ssam * vm_waitpfault: (also see VM_WAITPFAULT macro) 1990117845Ssam * 1991117845Ssam * Sleep until free pages are available for allocation. 1992117845Ssam * - Called only in vm_fault so that processes page faulting 1993117845Ssam * can be easily tracked. 1994117845Ssam * - Sleeps at a lower priority than vm_wait() so that vm_wait()ing 1995117845Ssam * processes will be able to grab memory first. Do not change 1996117845Ssam * this balance without careful testing first. 1997117845Ssam */ 1998117845Ssamvoid 1999117845Ssamvm_waitpfault(void) 2000117845Ssam{ 2001117845Ssam 2002117845Ssam mtx_lock(&vm_page_queue_free_mtx); 2003117845Ssam if (!vm_pages_needed) { 2004117845Ssam vm_pages_needed = 1; 2005117845Ssam wakeup(&vm_pages_needed); 2006117845Ssam } 2007117845Ssam msleep(&vm_cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PUSER, 2008117845Ssam "pfault", 0); 2009117845Ssam} 2010117845Ssam 2011117845Ssamstruct vm_pagequeue * 2012117845Ssamvm_page_pagequeue(vm_page_t m) 2013117845Ssam{ 2014117845Ssam 2015117845Ssam return (&vm_phys_domain(m)->vmd_pagequeues[m->queue]); 2016117845Ssam} 2017117845Ssam 2018117845Ssam/* 2019117845Ssam * vm_page_dequeue: 2020117845Ssam * 2021117845Ssam * Remove the given page from its current page queue. 2022117845Ssam * 2023117845Ssam * The page must be locked. 2024117845Ssam */ 2025117845Ssamvoid 2026117845Ssamvm_page_dequeue(vm_page_t m) 2027117845Ssam{ 2028117845Ssam struct vm_pagequeue *pq; 2029117845Ssam 2030117845Ssam vm_page_assert_locked(m); 2031117845Ssam KASSERT(m->queue < PQ_COUNT, ("vm_page_dequeue: page %p is not queued", 2032117845Ssam m)); 2033117845Ssam pq = vm_page_pagequeue(m); 2034117845Ssam vm_pagequeue_lock(pq); 2035117845Ssam m->queue = PQ_NONE; 2036117845Ssam TAILQ_REMOVE(&pq->pq_pl, m, plinks.q); 2037117845Ssam vm_pagequeue_cnt_dec(pq); 2038117845Ssam vm_pagequeue_unlock(pq); 2039117845Ssam} 2040117845Ssam 2041117845Ssam/* 2042117845Ssam * vm_page_dequeue_locked: 2043117845Ssam * 2044117845Ssam * Remove the given page from its current page queue. 2045117845Ssam * 2046117845Ssam * The page and page queue must be locked. 2047117845Ssam */ 2048117845Ssamvoid 2049117845Ssamvm_page_dequeue_locked(vm_page_t m) 2050117845Ssam{ 2051117845Ssam struct vm_pagequeue *pq; 2052117845Ssam 2053117845Ssam vm_page_lock_assert(m, MA_OWNED); 2054117845Ssam pq = vm_page_pagequeue(m); 2055117845Ssam vm_pagequeue_assert_locked(pq); 2056117845Ssam m->queue = PQ_NONE; 2057117845Ssam TAILQ_REMOVE(&pq->pq_pl, m, plinks.q); 2058117845Ssam vm_pagequeue_cnt_dec(pq); 2059117845Ssam} 2060117845Ssam 2061117845Ssam/* 2062117845Ssam * vm_page_enqueue: 2063117845Ssam * 2064117845Ssam * Add the given page to the specified page queue. 2065117845Ssam * 2066117845Ssam * The page must be locked. 2067117845Ssam */ 2068117845Ssamstatic void 2069117845Ssamvm_page_enqueue(uint8_t queue, vm_page_t m) 2070117845Ssam{ 2071117845Ssam struct vm_pagequeue *pq; 2072118882Ssam 2073117845Ssam vm_page_lock_assert(m, MA_OWNED); 2074117845Ssam KASSERT(queue < PQ_COUNT, 2075117845Ssam ("vm_page_enqueue: invalid queue %u request for page %p", 2076117845Ssam queue, m)); 2077118882Ssam pq = &vm_phys_domain(m)->vmd_pagequeues[queue]; 2078117845Ssam vm_pagequeue_lock(pq); 2079117845Ssam m->queue = queue; 2080117845Ssam TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q); 2081117845Ssam vm_pagequeue_cnt_inc(pq); 2082117845Ssam vm_pagequeue_unlock(pq); 2083117845Ssam} 2084117845Ssam 2085117845Ssam/* 2086117845Ssam * vm_page_requeue: 2087117845Ssam * 2088117845Ssam * Move the given page to the tail of its current page queue. 2089117845Ssam * 2090117845Ssam * The page must be locked. 2091117845Ssam */ 2092117845Ssamvoid 2093117845Ssamvm_page_requeue(vm_page_t m) 2094117845Ssam{ 2095117845Ssam struct vm_pagequeue *pq; 2096117845Ssam 2097117845Ssam vm_page_lock_assert(m, MA_OWNED); 2098117845Ssam KASSERT(m->queue != PQ_NONE, 2099117845Ssam ("vm_page_requeue: page %p is not queued", m)); 2100117845Ssam pq = vm_page_pagequeue(m); 2101117845Ssam vm_pagequeue_lock(pq); 2102117845Ssam TAILQ_REMOVE(&pq->pq_pl, m, plinks.q); 2103117845Ssam TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q); 2104117845Ssam vm_pagequeue_unlock(pq); 2105117845Ssam} 2106117845Ssam 2107117845Ssam/* 2108117845Ssam * vm_page_requeue_locked: 2109117845Ssam * 2110117845Ssam * Move the given page to the tail of its current page queue. 2111117845Ssam * 2112117845Ssam * The page queue must be locked. 2113117845Ssam */ 2114125466Spetervoid 2115117845Ssamvm_page_requeue_locked(vm_page_t m) 2116117845Ssam{ 2117125466Speter struct vm_pagequeue *pq; 2118125466Speter 2119117845Ssam KASSERT(m->queue != PQ_NONE, 2120117845Ssam ("vm_page_requeue_locked: page %p is not queued", m)); 2121117845Ssam pq = vm_page_pagequeue(m); 2122117845Ssam vm_pagequeue_assert_locked(pq); 2123117845Ssam TAILQ_REMOVE(&pq->pq_pl, m, plinks.q); 2124117845Ssam TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q); 2125117845Ssam} 2126117845Ssam 2127117845Ssam/* 2128117845Ssam * vm_page_activate: 2129117845Ssam * 2130117845Ssam * Put the specified page on the active list (if appropriate). 2131117845Ssam * Ensure that act_count is at least ACT_INIT but do not otherwise 2132117845Ssam * mess with it. 2133117845Ssam * 2134117845Ssam * The page must be locked. 2135117845Ssam */ 2136117845Ssamvoid 2137117845Ssamvm_page_activate(vm_page_t m) 2138117845Ssam{ 2139117845Ssam int queue; 2140117845Ssam 2141117845Ssam vm_page_lock_assert(m, MA_OWNED); 2142125466Speter if ((queue = m->queue) != PQ_ACTIVE) { 2143117845Ssam if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) { 2144117845Ssam if (m->act_count < ACT_INIT) 2145117845Ssam m->act_count = ACT_INIT; 2146117845Ssam if (queue != PQ_NONE) 2147117845Ssam vm_page_dequeue(m); 2148117845Ssam vm_page_enqueue(PQ_ACTIVE, m); 2149117845Ssam } else 2150117845Ssam KASSERT(queue == PQ_NONE, 2151117845Ssam ("vm_page_activate: wired page %p is queued", m)); 2152117845Ssam } else { 2153117845Ssam if (m->act_count < ACT_INIT) 2154117845Ssam m->act_count = ACT_INIT; 2155117845Ssam } 2156117845Ssam} 2157117845Ssam 2158117845Ssam/* 2159125466Speter * vm_page_free_wakeup: 2160117845Ssam * 2161117845Ssam * Helper routine for vm_page_free_toq() and vm_page_cache(). This 2162117845Ssam * routine is called when a page has been added to the cache or free 2163117845Ssam * queues. 2164117845Ssam * 2165117845Ssam * The page queues must be locked. 2166117845Ssam */ 2167117845Ssamstatic inline void 2168117845Ssamvm_page_free_wakeup(void) 2169117845Ssam{ 2170117845Ssam 2171117845Ssam mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 2172117845Ssam /* 2173117845Ssam * if pageout daemon needs pages, then tell it that there are 2174117845Ssam * some free. 2175117845Ssam */ 2176117845Ssam if (vm_pageout_pages_needed && 2177117845Ssam vm_cnt.v_cache_count + vm_cnt.v_free_count >= vm_cnt.v_pageout_free_min) { 2178117845Ssam wakeup(&vm_pageout_pages_needed); 2179117845Ssam vm_pageout_pages_needed = 0; 2180117845Ssam } 2181117845Ssam /* 2182117845Ssam * wakeup processes that are waiting on memory if we hit a 2183117845Ssam * high water mark. And wakeup scheduler process if we have 2184117845Ssam * lots of memory. this process will swapin processes. 2185117845Ssam */ 2186117845Ssam if (vm_pages_needed && !vm_page_count_min()) { 2187117845Ssam vm_pages_needed = 0; 2188117845Ssam wakeup(&vm_cnt.v_free_count); 2189117845Ssam } 2190117845Ssam} 2191117845Ssam 2192117845Ssam/* 2193117845Ssam * Turn a cached page into a free page, by changing its attributes. 2194117845Ssam * Keep the statistics up-to-date. 2195117845Ssam * 2196117845Ssam * The free page queue must be locked. 2197117845Ssam */ 2198117845Ssamstatic void 2199117845Ssamvm_page_cache_turn_free(vm_page_t m) 2200117845Ssam{ 2201117845Ssam 2202117845Ssam mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 2203117845Ssam 2204117845Ssam m->object = NULL; 2205117845Ssam m->valid = 0; 2206117845Ssam KASSERT((m->flags & PG_CACHED) != 0, 2207117845Ssam ("vm_page_cache_turn_free: page %p is not cached", m)); 2208117845Ssam m->flags &= ~PG_CACHED; 2209117845Ssam vm_cnt.v_cache_count--; 2210117845Ssam vm_phys_freecnt_adj(m, 1); 2211117845Ssam} 2212117845Ssam 2213117845Ssam/* 2214117845Ssam * vm_page_free_toq: 2215117845Ssam * 2216117845Ssam * Returns the given page to the free list, 2217117845Ssam * disassociating it with any VM object. 2218117845Ssam * 2219117845Ssam * The object must be locked. The page must be locked if it is managed. 2220117845Ssam */ 2221117845Ssamvoid 2222117845Ssamvm_page_free_toq(vm_page_t m) 2223117845Ssam{ 2224117845Ssam 2225117845Ssam if ((m->oflags & VPO_UNMANAGED) == 0) { 2226117845Ssam vm_page_lock_assert(m, MA_OWNED); 2227117845Ssam KASSERT(!pmap_page_is_mapped(m), 2228117845Ssam ("vm_page_free_toq: freeing mapped page %p", m)); 2229117845Ssam } else 2230117845Ssam KASSERT(m->queue == PQ_NONE, 2231117845Ssam ("vm_page_free_toq: unmanaged page %p is queued", m)); 2232117845Ssam PCPU_INC(cnt.v_tfree); 2233117845Ssam 2234117845Ssam if (vm_page_sbusied(m)) 2235117845Ssam panic("vm_page_free: freeing busy page %p", m); 2236117845Ssam 2237117845Ssam /* 2238117845Ssam * Unqueue, then remove page. Note that we cannot destroy 2239117845Ssam * the page here because we do not want to call the pager's 2240117845Ssam * callback routine until after we've put the page on the 2241117845Ssam * appropriate free queue. 2242117845Ssam */ 2243117845Ssam vm_page_remque(m); 2244117845Ssam vm_page_remove(m); 2245117845Ssam 2246117845Ssam /* 2247117845Ssam * If fictitious remove object association and 2248117845Ssam * return, otherwise delay object association removal. 2249117845Ssam */ 2250117845Ssam if ((m->flags & PG_FICTITIOUS) != 0) { 2251117845Ssam return; 2252117845Ssam } 2253117845Ssam 2254117845Ssam m->valid = 0; 2255117845Ssam vm_page_undirty(m); 2256 2257 if (m->wire_count != 0) 2258 panic("vm_page_free: freeing wired page %p", m); 2259 if (m->hold_count != 0) { 2260 m->flags &= ~PG_ZERO; 2261 KASSERT((m->flags & PG_UNHOLDFREE) == 0, 2262 ("vm_page_free: freeing PG_UNHOLDFREE page %p", m)); 2263 m->flags |= PG_UNHOLDFREE; 2264 } else { 2265 /* 2266 * Restore the default memory attribute to the page. 2267 */ 2268 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT) 2269 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT); 2270 2271 /* 2272 * Insert the page into the physical memory allocator's 2273 * cache/free page queues. 2274 */ 2275 mtx_lock(&vm_page_queue_free_mtx); 2276 vm_phys_freecnt_adj(m, 1); 2277#if VM_NRESERVLEVEL > 0 2278 if (!vm_reserv_free_page(m)) 2279#else 2280 if (TRUE) 2281#endif 2282 vm_phys_free_pages(m, 0); 2283 if ((m->flags & PG_ZERO) != 0) 2284 ++vm_page_zero_count; 2285 else 2286 vm_page_zero_idle_wakeup(); 2287 vm_page_free_wakeup(); 2288 mtx_unlock(&vm_page_queue_free_mtx); 2289 } 2290} 2291 2292/* 2293 * vm_page_wire: 2294 * 2295 * Mark this page as wired down by yet 2296 * another map, removing it from paging queues 2297 * as necessary. 2298 * 2299 * If the page is fictitious, then its wire count must remain one. 2300 * 2301 * The page must be locked. 2302 */ 2303void 2304vm_page_wire(vm_page_t m) 2305{ 2306 2307 /* 2308 * Only bump the wire statistics if the page is not already wired, 2309 * and only unqueue the page if it is on some queue (if it is unmanaged 2310 * it is already off the queues). 2311 */ 2312 vm_page_lock_assert(m, MA_OWNED); 2313 if ((m->flags & PG_FICTITIOUS) != 0) { 2314 KASSERT(m->wire_count == 1, 2315 ("vm_page_wire: fictitious page %p's wire count isn't one", 2316 m)); 2317 return; 2318 } 2319 if (m->wire_count == 0) { 2320 KASSERT((m->oflags & VPO_UNMANAGED) == 0 || 2321 m->queue == PQ_NONE, 2322 ("vm_page_wire: unmanaged page %p is queued", m)); 2323 vm_page_remque(m); 2324 atomic_add_int(&vm_cnt.v_wire_count, 1); 2325 } 2326 m->wire_count++; 2327 KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m)); 2328} 2329 2330/* 2331 * vm_page_unwire: 2332 * 2333 * Release one wiring of the specified page, potentially enabling it to be 2334 * paged again. If paging is enabled, then the value of the parameter 2335 * "queue" determines the queue to which the page is added. 2336 * 2337 * However, unless the page belongs to an object, it is not enqueued because 2338 * it cannot be paged out. 2339 * 2340 * If a page is fictitious, then its wire count must always be one. 2341 * 2342 * A managed page must be locked. 2343 */ 2344void 2345vm_page_unwire(vm_page_t m, uint8_t queue) 2346{ 2347 2348 KASSERT(queue < PQ_COUNT, 2349 ("vm_page_unwire: invalid queue %u request for page %p", 2350 queue, m)); 2351 if ((m->oflags & VPO_UNMANAGED) == 0) 2352 vm_page_lock_assert(m, MA_OWNED); 2353 if ((m->flags & PG_FICTITIOUS) != 0) { 2354 KASSERT(m->wire_count == 1, 2355 ("vm_page_unwire: fictitious page %p's wire count isn't one", m)); 2356 return; 2357 } 2358 if (m->wire_count > 0) { 2359 m->wire_count--; 2360 if (m->wire_count == 0) { 2361 atomic_subtract_int(&vm_cnt.v_wire_count, 1); 2362 if ((m->oflags & VPO_UNMANAGED) != 0 || 2363 m->object == NULL) 2364 return; 2365 if (queue == PQ_INACTIVE) 2366 m->flags &= ~PG_WINATCFLS; 2367 vm_page_enqueue(queue, m); 2368 } 2369 } else 2370 panic("vm_page_unwire: page %p's wire count is zero", m); 2371} 2372 2373/* 2374 * Move the specified page to the inactive queue. 2375 * 2376 * Many pages placed on the inactive queue should actually go 2377 * into the cache, but it is difficult to figure out which. What 2378 * we do instead, if the inactive target is well met, is to put 2379 * clean pages at the head of the inactive queue instead of the tail. 2380 * This will cause them to be moved to the cache more quickly and 2381 * if not actively re-referenced, reclaimed more quickly. If we just 2382 * stick these pages at the end of the inactive queue, heavy filesystem 2383 * meta-data accesses can cause an unnecessary paging load on memory bound 2384 * processes. This optimization causes one-time-use metadata to be 2385 * reused more quickly. 2386 * 2387 * Normally athead is 0 resulting in LRU operation. athead is set 2388 * to 1 if we want this page to be 'as if it were placed in the cache', 2389 * except without unmapping it from the process address space. 2390 * 2391 * The page must be locked. 2392 */ 2393static inline void 2394_vm_page_deactivate(vm_page_t m, int athead) 2395{ 2396 struct vm_pagequeue *pq; 2397 int queue; 2398 2399 vm_page_lock_assert(m, MA_OWNED); 2400 2401 /* 2402 * Ignore if already inactive. 2403 */ 2404 if ((queue = m->queue) == PQ_INACTIVE) 2405 return; 2406 if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) { 2407 if (queue != PQ_NONE) 2408 vm_page_dequeue(m); 2409 m->flags &= ~PG_WINATCFLS; 2410 pq = &vm_phys_domain(m)->vmd_pagequeues[PQ_INACTIVE]; 2411 vm_pagequeue_lock(pq); 2412 m->queue = PQ_INACTIVE; 2413 if (athead) 2414 TAILQ_INSERT_HEAD(&pq->pq_pl, m, plinks.q); 2415 else 2416 TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q); 2417 vm_pagequeue_cnt_inc(pq); 2418 vm_pagequeue_unlock(pq); 2419 } 2420} 2421 2422/* 2423 * Move the specified page to the inactive queue. 2424 * 2425 * The page must be locked. 2426 */ 2427void 2428vm_page_deactivate(vm_page_t m) 2429{ 2430 2431 _vm_page_deactivate(m, 0); 2432} 2433 2434/* 2435 * vm_page_try_to_cache: 2436 * 2437 * Returns 0 on failure, 1 on success 2438 */ 2439int 2440vm_page_try_to_cache(vm_page_t m) 2441{ 2442 2443 vm_page_lock_assert(m, MA_OWNED); 2444 VM_OBJECT_ASSERT_WLOCKED(m->object); 2445 if (m->dirty || m->hold_count || m->wire_count || 2446 (m->oflags & VPO_UNMANAGED) != 0 || vm_page_busied(m)) 2447 return (0); 2448 pmap_remove_all(m); 2449 if (m->dirty) 2450 return (0); 2451 vm_page_cache(m); 2452 return (1); 2453} 2454 2455/* 2456 * vm_page_try_to_free() 2457 * 2458 * Attempt to free the page. If we cannot free it, we do nothing. 2459 * 1 is returned on success, 0 on failure. 2460 */ 2461int 2462vm_page_try_to_free(vm_page_t m) 2463{ 2464 2465 vm_page_lock_assert(m, MA_OWNED); 2466 if (m->object != NULL) 2467 VM_OBJECT_ASSERT_WLOCKED(m->object); 2468 if (m->dirty || m->hold_count || m->wire_count || 2469 (m->oflags & VPO_UNMANAGED) != 0 || vm_page_busied(m)) 2470 return (0); 2471 pmap_remove_all(m); 2472 if (m->dirty) 2473 return (0); 2474 vm_page_free(m); 2475 return (1); 2476} 2477 2478/* 2479 * vm_page_cache 2480 * 2481 * Put the specified page onto the page cache queue (if appropriate). 2482 * 2483 * The object and page must be locked. 2484 */ 2485void 2486vm_page_cache(vm_page_t m) 2487{ 2488 vm_object_t object; 2489 boolean_t cache_was_empty; 2490 2491 vm_page_lock_assert(m, MA_OWNED); 2492 object = m->object; 2493 VM_OBJECT_ASSERT_WLOCKED(object); 2494 if (vm_page_busied(m) || (m->oflags & VPO_UNMANAGED) || 2495 m->hold_count || m->wire_count) 2496 panic("vm_page_cache: attempting to cache busy page"); 2497 KASSERT(!pmap_page_is_mapped(m), 2498 ("vm_page_cache: page %p is mapped", m)); 2499 KASSERT(m->dirty == 0, ("vm_page_cache: page %p is dirty", m)); 2500 if (m->valid == 0 || object->type == OBJT_DEFAULT || 2501 (object->type == OBJT_SWAP && 2502 !vm_pager_has_page(object, m->pindex, NULL, NULL))) { 2503 /* 2504 * Hypothesis: A cache-eligible page belonging to a 2505 * default object or swap object but without a backing 2506 * store must be zero filled. 2507 */ 2508 vm_page_free(m); 2509 return; 2510 } 2511 KASSERT((m->flags & PG_CACHED) == 0, 2512 ("vm_page_cache: page %p is already cached", m)); 2513 2514 /* 2515 * Remove the page from the paging queues. 2516 */ 2517 vm_page_remque(m); 2518 2519 /* 2520 * Remove the page from the object's collection of resident 2521 * pages. 2522 */ 2523 vm_radix_remove(&object->rtree, m->pindex); 2524 TAILQ_REMOVE(&object->memq, m, listq); 2525 object->resident_page_count--; 2526 2527 /* 2528 * Restore the default memory attribute to the page. 2529 */ 2530 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT) 2531 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT); 2532 2533 /* 2534 * Insert the page into the object's collection of cached pages 2535 * and the physical memory allocator's cache/free page queues. 2536 */ 2537 m->flags &= ~PG_ZERO; 2538 mtx_lock(&vm_page_queue_free_mtx); 2539 cache_was_empty = vm_radix_is_empty(&object->cache); 2540 if (vm_radix_insert(&object->cache, m)) { 2541 mtx_unlock(&vm_page_queue_free_mtx); 2542 if (object->resident_page_count == 0) 2543 vdrop(object->handle); 2544 m->object = NULL; 2545 vm_page_free(m); 2546 return; 2547 } 2548 2549 /* 2550 * The above call to vm_radix_insert() could reclaim the one pre- 2551 * existing cached page from this object, resulting in a call to 2552 * vdrop(). 2553 */ 2554 if (!cache_was_empty) 2555 cache_was_empty = vm_radix_is_singleton(&object->cache); 2556 2557 m->flags |= PG_CACHED; 2558 vm_cnt.v_cache_count++; 2559 PCPU_INC(cnt.v_tcached); 2560#if VM_NRESERVLEVEL > 0 2561 if (!vm_reserv_free_page(m)) { 2562#else 2563 if (TRUE) { 2564#endif 2565 vm_phys_set_pool(VM_FREEPOOL_CACHE, m, 0); 2566 vm_phys_free_pages(m, 0); 2567 } 2568 vm_page_free_wakeup(); 2569 mtx_unlock(&vm_page_queue_free_mtx); 2570 2571 /* 2572 * Increment the vnode's hold count if this is the object's only 2573 * cached page. Decrement the vnode's hold count if this was 2574 * the object's only resident page. 2575 */ 2576 if (object->type == OBJT_VNODE) { 2577 if (cache_was_empty && object->resident_page_count != 0) 2578 vhold(object->handle); 2579 else if (!cache_was_empty && object->resident_page_count == 0) 2580 vdrop(object->handle); 2581 } 2582} 2583 2584/* 2585 * vm_page_advise 2586 * 2587 * Cache, deactivate, or do nothing as appropriate. This routine 2588 * is used by madvise(). 2589 * 2590 * Generally speaking we want to move the page into the cache so 2591 * it gets reused quickly. However, this can result in a silly syndrome 2592 * due to the page recycling too quickly. Small objects will not be 2593 * fully cached. On the other hand, if we move the page to the inactive 2594 * queue we wind up with a problem whereby very large objects 2595 * unnecessarily blow away our inactive and cache queues. 2596 * 2597 * The solution is to move the pages based on a fixed weighting. We 2598 * either leave them alone, deactivate them, or move them to the cache, 2599 * where moving them to the cache has the highest weighting. 2600 * By forcing some pages into other queues we eventually force the 2601 * system to balance the queues, potentially recovering other unrelated 2602 * space from active. The idea is to not force this to happen too 2603 * often. 2604 * 2605 * The object and page must be locked. 2606 */ 2607void 2608vm_page_advise(vm_page_t m, int advice) 2609{ 2610 int dnw, head; 2611 2612 vm_page_assert_locked(m); 2613 VM_OBJECT_ASSERT_WLOCKED(m->object); 2614 if (advice == MADV_FREE) { 2615 /* 2616 * Mark the page clean. This will allow the page to be freed 2617 * up by the system. However, such pages are often reused 2618 * quickly by malloc() so we do not do anything that would 2619 * cause a page fault if we can help it. 2620 * 2621 * Specifically, we do not try to actually free the page now 2622 * nor do we try to put it in the cache (which would cause a 2623 * page fault on reuse). 2624 * 2625 * But we do make the page is freeable as we can without 2626 * actually taking the step of unmapping it. 2627 */ 2628 m->dirty = 0; 2629 m->act_count = 0; 2630 } else if (advice != MADV_DONTNEED) 2631 return; 2632 dnw = PCPU_GET(dnweight); 2633 PCPU_INC(dnweight); 2634 2635 /* 2636 * Occasionally leave the page alone. 2637 */ 2638 if ((dnw & 0x01F0) == 0 || m->queue == PQ_INACTIVE) { 2639 if (m->act_count >= ACT_INIT) 2640 --m->act_count; 2641 return; 2642 } 2643 2644 /* 2645 * Clear any references to the page. Otherwise, the page daemon will 2646 * immediately reactivate the page. 2647 */ 2648 vm_page_aflag_clear(m, PGA_REFERENCED); 2649 2650 if (advice != MADV_FREE && m->dirty == 0 && pmap_is_modified(m)) 2651 vm_page_dirty(m); 2652 2653 if (m->dirty || (dnw & 0x0070) == 0) { 2654 /* 2655 * Deactivate the page 3 times out of 32. 2656 */ 2657 head = 0; 2658 } else { 2659 /* 2660 * Cache the page 28 times out of every 32. Note that 2661 * the page is deactivated instead of cached, but placed 2662 * at the head of the queue instead of the tail. 2663 */ 2664 head = 1; 2665 } 2666 _vm_page_deactivate(m, head); 2667} 2668 2669/* 2670 * Grab a page, waiting until we are waken up due to the page 2671 * changing state. We keep on waiting, if the page continues 2672 * to be in the object. If the page doesn't exist, first allocate it 2673 * and then conditionally zero it. 2674 * 2675 * This routine may sleep. 2676 * 2677 * The object must be locked on entry. The lock will, however, be released 2678 * and reacquired if the routine sleeps. 2679 */ 2680vm_page_t 2681vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags) 2682{ 2683 vm_page_t m; 2684 int sleep; 2685 2686 VM_OBJECT_ASSERT_WLOCKED(object); 2687 KASSERT((allocflags & VM_ALLOC_SBUSY) == 0 || 2688 (allocflags & VM_ALLOC_IGN_SBUSY) != 0, 2689 ("vm_page_grab: VM_ALLOC_SBUSY/VM_ALLOC_IGN_SBUSY mismatch")); 2690retrylookup: 2691 if ((m = vm_page_lookup(object, pindex)) != NULL) { 2692 sleep = (allocflags & VM_ALLOC_IGN_SBUSY) != 0 ? 2693 vm_page_xbusied(m) : vm_page_busied(m); 2694 if (sleep) { 2695 /* 2696 * Reference the page before unlocking and 2697 * sleeping so that the page daemon is less 2698 * likely to reclaim it. 2699 */ 2700 vm_page_aflag_set(m, PGA_REFERENCED); 2701 vm_page_lock(m); 2702 VM_OBJECT_WUNLOCK(object); 2703 vm_page_busy_sleep(m, "pgrbwt"); 2704 VM_OBJECT_WLOCK(object); 2705 goto retrylookup; 2706 } else { 2707 if ((allocflags & VM_ALLOC_WIRED) != 0) { 2708 vm_page_lock(m); 2709 vm_page_wire(m); 2710 vm_page_unlock(m); 2711 } 2712 if ((allocflags & 2713 (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) == 0) 2714 vm_page_xbusy(m); 2715 if ((allocflags & VM_ALLOC_SBUSY) != 0) 2716 vm_page_sbusy(m); 2717 return (m); 2718 } 2719 } 2720 m = vm_page_alloc(object, pindex, allocflags & ~VM_ALLOC_IGN_SBUSY); 2721 if (m == NULL) { 2722 VM_OBJECT_WUNLOCK(object); 2723 VM_WAIT; 2724 VM_OBJECT_WLOCK(object); 2725 goto retrylookup; 2726 } else if (m->valid != 0) 2727 return (m); 2728 if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0) 2729 pmap_zero_page(m); 2730 return (m); 2731} 2732 2733/* 2734 * Mapping function for valid or dirty bits in a page. 2735 * 2736 * Inputs are required to range within a page. 2737 */ 2738vm_page_bits_t 2739vm_page_bits(int base, int size) 2740{ 2741 int first_bit; 2742 int last_bit; 2743 2744 KASSERT( 2745 base + size <= PAGE_SIZE, 2746 ("vm_page_bits: illegal base/size %d/%d", base, size) 2747 ); 2748 2749 if (size == 0) /* handle degenerate case */ 2750 return (0); 2751 2752 first_bit = base >> DEV_BSHIFT; 2753 last_bit = (base + size - 1) >> DEV_BSHIFT; 2754 2755 return (((vm_page_bits_t)2 << last_bit) - 2756 ((vm_page_bits_t)1 << first_bit)); 2757} 2758 2759/* 2760 * vm_page_set_valid_range: 2761 * 2762 * Sets portions of a page valid. The arguments are expected 2763 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive 2764 * of any partial chunks touched by the range. The invalid portion of 2765 * such chunks will be zeroed. 2766 * 2767 * (base + size) must be less then or equal to PAGE_SIZE. 2768 */ 2769void 2770vm_page_set_valid_range(vm_page_t m, int base, int size) 2771{ 2772 int endoff, frag; 2773 2774 VM_OBJECT_ASSERT_WLOCKED(m->object); 2775 if (size == 0) /* handle degenerate case */ 2776 return; 2777 2778 /* 2779 * If the base is not DEV_BSIZE aligned and the valid 2780 * bit is clear, we have to zero out a portion of the 2781 * first block. 2782 */ 2783 if ((frag = base & ~(DEV_BSIZE - 1)) != base && 2784 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0) 2785 pmap_zero_page_area(m, frag, base - frag); 2786 2787 /* 2788 * If the ending offset is not DEV_BSIZE aligned and the 2789 * valid bit is clear, we have to zero out a portion of 2790 * the last block. 2791 */ 2792 endoff = base + size; 2793 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff && 2794 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0) 2795 pmap_zero_page_area(m, endoff, 2796 DEV_BSIZE - (endoff & (DEV_BSIZE - 1))); 2797 2798 /* 2799 * Assert that no previously invalid block that is now being validated 2800 * is already dirty. 2801 */ 2802 KASSERT((~m->valid & vm_page_bits(base, size) & m->dirty) == 0, 2803 ("vm_page_set_valid_range: page %p is dirty", m)); 2804 2805 /* 2806 * Set valid bits inclusive of any overlap. 2807 */ 2808 m->valid |= vm_page_bits(base, size); 2809} 2810 2811/* 2812 * Clear the given bits from the specified page's dirty field. 2813 */ 2814static __inline void 2815vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits) 2816{ 2817 uintptr_t addr; 2818#if PAGE_SIZE < 16384 2819 int shift; 2820#endif 2821 2822 /* 2823 * If the object is locked and the page is neither exclusive busy nor 2824 * write mapped, then the page's dirty field cannot possibly be 2825 * set by a concurrent pmap operation. 2826 */ 2827 VM_OBJECT_ASSERT_WLOCKED(m->object); 2828 if (!vm_page_xbusied(m) && !pmap_page_is_write_mapped(m)) 2829 m->dirty &= ~pagebits; 2830 else { 2831 /* 2832 * The pmap layer can call vm_page_dirty() without 2833 * holding a distinguished lock. The combination of 2834 * the object's lock and an atomic operation suffice 2835 * to guarantee consistency of the page dirty field. 2836 * 2837 * For PAGE_SIZE == 32768 case, compiler already 2838 * properly aligns the dirty field, so no forcible 2839 * alignment is needed. Only require existence of 2840 * atomic_clear_64 when page size is 32768. 2841 */ 2842 addr = (uintptr_t)&m->dirty; 2843#if PAGE_SIZE == 32768 2844 atomic_clear_64((uint64_t *)addr, pagebits); 2845#elif PAGE_SIZE == 16384 2846 atomic_clear_32((uint32_t *)addr, pagebits); 2847#else /* PAGE_SIZE <= 8192 */ 2848 /* 2849 * Use a trick to perform a 32-bit atomic on the 2850 * containing aligned word, to not depend on the existence 2851 * of atomic_clear_{8, 16}. 2852 */ 2853 shift = addr & (sizeof(uint32_t) - 1); 2854#if BYTE_ORDER == BIG_ENDIAN 2855 shift = (sizeof(uint32_t) - sizeof(m->dirty) - shift) * NBBY; 2856#else 2857 shift *= NBBY; 2858#endif 2859 addr &= ~(sizeof(uint32_t) - 1); 2860 atomic_clear_32((uint32_t *)addr, pagebits << shift); 2861#endif /* PAGE_SIZE */ 2862 } 2863} 2864 2865/* 2866 * vm_page_set_validclean: 2867 * 2868 * Sets portions of a page valid and clean. The arguments are expected 2869 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive 2870 * of any partial chunks touched by the range. The invalid portion of 2871 * such chunks will be zero'd. 2872 * 2873 * (base + size) must be less then or equal to PAGE_SIZE. 2874 */ 2875void 2876vm_page_set_validclean(vm_page_t m, int base, int size) 2877{ 2878 vm_page_bits_t oldvalid, pagebits; 2879 int endoff, frag; 2880 2881 VM_OBJECT_ASSERT_WLOCKED(m->object); 2882 if (size == 0) /* handle degenerate case */ 2883 return; 2884 2885 /* 2886 * If the base is not DEV_BSIZE aligned and the valid 2887 * bit is clear, we have to zero out a portion of the 2888 * first block. 2889 */ 2890 if ((frag = base & ~(DEV_BSIZE - 1)) != base && 2891 (m->valid & ((vm_page_bits_t)1 << (base >> DEV_BSHIFT))) == 0) 2892 pmap_zero_page_area(m, frag, base - frag); 2893 2894 /* 2895 * If the ending offset is not DEV_BSIZE aligned and the 2896 * valid bit is clear, we have to zero out a portion of 2897 * the last block. 2898 */ 2899 endoff = base + size; 2900 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff && 2901 (m->valid & ((vm_page_bits_t)1 << (endoff >> DEV_BSHIFT))) == 0) 2902 pmap_zero_page_area(m, endoff, 2903 DEV_BSIZE - (endoff & (DEV_BSIZE - 1))); 2904 2905 /* 2906 * Set valid, clear dirty bits. If validating the entire 2907 * page we can safely clear the pmap modify bit. We also 2908 * use this opportunity to clear the VPO_NOSYNC flag. If a process 2909 * takes a write fault on a MAP_NOSYNC memory area the flag will 2910 * be set again. 2911 * 2912 * We set valid bits inclusive of any overlap, but we can only 2913 * clear dirty bits for DEV_BSIZE chunks that are fully within 2914 * the range. 2915 */ 2916 oldvalid = m->valid; 2917 pagebits = vm_page_bits(base, size); 2918 m->valid |= pagebits; 2919#if 0 /* NOT YET */ 2920 if ((frag = base & (DEV_BSIZE - 1)) != 0) { 2921 frag = DEV_BSIZE - frag; 2922 base += frag; 2923 size -= frag; 2924 if (size < 0) 2925 size = 0; 2926 } 2927 pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1)); 2928#endif 2929 if (base == 0 && size == PAGE_SIZE) { 2930 /* 2931 * The page can only be modified within the pmap if it is 2932 * mapped, and it can only be mapped if it was previously 2933 * fully valid. 2934 */ 2935 if (oldvalid == VM_PAGE_BITS_ALL) 2936 /* 2937 * Perform the pmap_clear_modify() first. Otherwise, 2938 * a concurrent pmap operation, such as 2939 * pmap_protect(), could clear a modification in the 2940 * pmap and set the dirty field on the page before 2941 * pmap_clear_modify() had begun and after the dirty 2942 * field was cleared here. 2943 */ 2944 pmap_clear_modify(m); 2945 m->dirty = 0; 2946 m->oflags &= ~VPO_NOSYNC; 2947 } else if (oldvalid != VM_PAGE_BITS_ALL) 2948 m->dirty &= ~pagebits; 2949 else 2950 vm_page_clear_dirty_mask(m, pagebits); 2951} 2952 2953void 2954vm_page_clear_dirty(vm_page_t m, int base, int size) 2955{ 2956 2957 vm_page_clear_dirty_mask(m, vm_page_bits(base, size)); 2958} 2959 2960/* 2961 * vm_page_set_invalid: 2962 * 2963 * Invalidates DEV_BSIZE'd chunks within a page. Both the 2964 * valid and dirty bits for the effected areas are cleared. 2965 */ 2966void 2967vm_page_set_invalid(vm_page_t m, int base, int size) 2968{ 2969 vm_page_bits_t bits; 2970 vm_object_t object; 2971 2972 object = m->object; 2973 VM_OBJECT_ASSERT_WLOCKED(object); 2974 if (object->type == OBJT_VNODE && base == 0 && IDX_TO_OFF(m->pindex) + 2975 size >= object->un_pager.vnp.vnp_size) 2976 bits = VM_PAGE_BITS_ALL; 2977 else 2978 bits = vm_page_bits(base, size); 2979 if (m->valid == VM_PAGE_BITS_ALL && bits != 0) 2980 pmap_remove_all(m); 2981 KASSERT((bits == 0 && m->valid == VM_PAGE_BITS_ALL) || 2982 !pmap_page_is_mapped(m), 2983 ("vm_page_set_invalid: page %p is mapped", m)); 2984 m->valid &= ~bits; 2985 m->dirty &= ~bits; 2986} 2987 2988/* 2989 * vm_page_zero_invalid() 2990 * 2991 * The kernel assumes that the invalid portions of a page contain 2992 * garbage, but such pages can be mapped into memory by user code. 2993 * When this occurs, we must zero out the non-valid portions of the 2994 * page so user code sees what it expects. 2995 * 2996 * Pages are most often semi-valid when the end of a file is mapped 2997 * into memory and the file's size is not page aligned. 2998 */ 2999void 3000vm_page_zero_invalid(vm_page_t m, boolean_t setvalid) 3001{ 3002 int b; 3003 int i; 3004 3005 VM_OBJECT_ASSERT_WLOCKED(m->object); 3006 /* 3007 * Scan the valid bits looking for invalid sections that 3008 * must be zerod. Invalid sub-DEV_BSIZE'd areas ( where the 3009 * valid bit may be set ) have already been zerod by 3010 * vm_page_set_validclean(). 3011 */ 3012 for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) { 3013 if (i == (PAGE_SIZE / DEV_BSIZE) || 3014 (m->valid & ((vm_page_bits_t)1 << i))) { 3015 if (i > b) { 3016 pmap_zero_page_area(m, 3017 b << DEV_BSHIFT, (i - b) << DEV_BSHIFT); 3018 } 3019 b = i + 1; 3020 } 3021 } 3022 3023 /* 3024 * setvalid is TRUE when we can safely set the zero'd areas 3025 * as being valid. We can do this if there are no cache consistancy 3026 * issues. e.g. it is ok to do with UFS, but not ok to do with NFS. 3027 */ 3028 if (setvalid) 3029 m->valid = VM_PAGE_BITS_ALL; 3030} 3031 3032/* 3033 * vm_page_is_valid: 3034 * 3035 * Is (partial) page valid? Note that the case where size == 0 3036 * will return FALSE in the degenerate case where the page is 3037 * entirely invalid, and TRUE otherwise. 3038 */ 3039int 3040vm_page_is_valid(vm_page_t m, int base, int size) 3041{ 3042 vm_page_bits_t bits; 3043 3044 VM_OBJECT_ASSERT_LOCKED(m->object); 3045 bits = vm_page_bits(base, size); 3046 return (m->valid != 0 && (m->valid & bits) == bits); 3047} 3048 3049/* 3050 * vm_page_ps_is_valid: 3051 * 3052 * Returns TRUE if the entire (super)page is valid and FALSE otherwise. 3053 */ 3054boolean_t 3055vm_page_ps_is_valid(vm_page_t m) 3056{ 3057 int i, npages; 3058 3059 VM_OBJECT_ASSERT_LOCKED(m->object); 3060 npages = atop(pagesizes[m->psind]); 3061 3062 /* 3063 * The physically contiguous pages that make up a superpage, i.e., a 3064 * page with a page size index ("psind") greater than zero, will 3065 * occupy adjacent entries in vm_page_array[]. 3066 */ 3067 for (i = 0; i < npages; i++) { 3068 if (m[i].valid != VM_PAGE_BITS_ALL) 3069 return (FALSE); 3070 } 3071 return (TRUE); 3072} 3073 3074/* 3075 * Set the page's dirty bits if the page is modified. 3076 */ 3077void 3078vm_page_test_dirty(vm_page_t m) 3079{ 3080 3081 VM_OBJECT_ASSERT_WLOCKED(m->object); 3082 if (m->dirty != VM_PAGE_BITS_ALL && pmap_is_modified(m)) 3083 vm_page_dirty(m); 3084} 3085 3086void 3087vm_page_lock_KBI(vm_page_t m, const char *file, int line) 3088{ 3089 3090 mtx_lock_flags_(vm_page_lockptr(m), 0, file, line); 3091} 3092 3093void 3094vm_page_unlock_KBI(vm_page_t m, const char *file, int line) 3095{ 3096 3097 mtx_unlock_flags_(vm_page_lockptr(m), 0, file, line); 3098} 3099 3100int 3101vm_page_trylock_KBI(vm_page_t m, const char *file, int line) 3102{ 3103 3104 return (mtx_trylock_flags_(vm_page_lockptr(m), 0, file, line)); 3105} 3106 3107#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT) 3108void 3109vm_page_assert_locked_KBI(vm_page_t m, const char *file, int line) 3110{ 3111 3112 vm_page_lock_assert_KBI(m, MA_OWNED, file, line); 3113} 3114 3115void 3116vm_page_lock_assert_KBI(vm_page_t m, int a, const char *file, int line) 3117{ 3118 3119 mtx_assert_(vm_page_lockptr(m), a, file, line); 3120} 3121#endif 3122 3123#ifdef INVARIANTS 3124void 3125vm_page_object_lock_assert(vm_page_t m) 3126{ 3127 3128 /* 3129 * Certain of the page's fields may only be modified by the 3130 * holder of the containing object's lock or the exclusive busy. 3131 * holder. Unfortunately, the holder of the write busy is 3132 * not recorded, and thus cannot be checked here. 3133 */ 3134 if (m->object != NULL && !vm_page_xbusied(m)) 3135 VM_OBJECT_ASSERT_WLOCKED(m->object); 3136} 3137 3138void 3139vm_page_assert_pga_writeable(vm_page_t m, uint8_t bits) 3140{ 3141 3142 if ((bits & PGA_WRITEABLE) == 0) 3143 return; 3144 3145 /* 3146 * The PGA_WRITEABLE flag can only be set if the page is 3147 * managed, is exclusively busied or the object is locked. 3148 * Currently, this flag is only set by pmap_enter(). 3149 */ 3150 KASSERT((m->oflags & VPO_UNMANAGED) == 0, 3151 ("PGA_WRITEABLE on unmanaged page")); 3152 if (!vm_page_xbusied(m)) 3153 VM_OBJECT_ASSERT_LOCKED(m->object); 3154} 3155#endif 3156 3157#include "opt_ddb.h" 3158#ifdef DDB 3159#include <sys/kernel.h> 3160 3161#include <ddb/ddb.h> 3162 3163DB_SHOW_COMMAND(page, vm_page_print_page_info) 3164{ 3165 db_printf("vm_cnt.v_free_count: %d\n", vm_cnt.v_free_count); 3166 db_printf("vm_cnt.v_cache_count: %d\n", vm_cnt.v_cache_count); 3167 db_printf("vm_cnt.v_inactive_count: %d\n", vm_cnt.v_inactive_count); 3168 db_printf("vm_cnt.v_active_count: %d\n", vm_cnt.v_active_count); 3169 db_printf("vm_cnt.v_wire_count: %d\n", vm_cnt.v_wire_count); 3170 db_printf("vm_cnt.v_free_reserved: %d\n", vm_cnt.v_free_reserved); 3171 db_printf("vm_cnt.v_free_min: %d\n", vm_cnt.v_free_min); 3172 db_printf("vm_cnt.v_free_target: %d\n", vm_cnt.v_free_target); 3173 db_printf("vm_cnt.v_cache_min: %d\n", vm_cnt.v_cache_min); 3174 db_printf("vm_cnt.v_inactive_target: %d\n", vm_cnt.v_inactive_target); 3175} 3176 3177DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info) 3178{ 3179 int dom; 3180 3181 db_printf("pq_free %d pq_cache %d\n", 3182 vm_cnt.v_free_count, vm_cnt.v_cache_count); 3183 for (dom = 0; dom < vm_ndomains; dom++) { 3184 db_printf( 3185 "dom %d page_cnt %d free %d pq_act %d pq_inact %d pass %d\n", 3186 dom, 3187 vm_dom[dom].vmd_page_count, 3188 vm_dom[dom].vmd_free_count, 3189 vm_dom[dom].vmd_pagequeues[PQ_ACTIVE].pq_cnt, 3190 vm_dom[dom].vmd_pagequeues[PQ_INACTIVE].pq_cnt, 3191 vm_dom[dom].vmd_pass); 3192 } 3193} 3194 3195DB_SHOW_COMMAND(pginfo, vm_page_print_pginfo) 3196{ 3197 vm_page_t m; 3198 boolean_t phys; 3199 3200 if (!have_addr) { 3201 db_printf("show pginfo addr\n"); 3202 return; 3203 } 3204 3205 phys = strchr(modif, 'p') != NULL; 3206 if (phys) 3207 m = PHYS_TO_VM_PAGE(addr); 3208 else 3209 m = (vm_page_t)addr; 3210 db_printf( 3211 "page %p obj %p pidx 0x%jx phys 0x%jx q %d hold %d wire %d\n" 3212 " af 0x%x of 0x%x f 0x%x act %d busy %x valid 0x%x dirty 0x%x\n", 3213 m, m->object, (uintmax_t)m->pindex, (uintmax_t)m->phys_addr, 3214 m->queue, m->hold_count, m->wire_count, m->aflags, m->oflags, 3215 m->flags, m->act_count, m->busy_lock, m->valid, m->dirty); 3216} 3217#endif /* DDB */ 3218