160727d8bSWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 6df8bae1dSRodney W. Grimes * The Mach Operating System project at Carnegie-Mellon University. 7df8bae1dSRodney W. Grimes * 8df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 9df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 10df8bae1dSRodney W. Grimes * are met: 11df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 12df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 13df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 15df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 16df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 17df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 18df8bae1dSRodney W. Grimes * without specific prior written permission. 19df8bae1dSRodney W. Grimes * 20df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30df8bae1dSRodney W. Grimes * SUCH DAMAGE. 31df8bae1dSRodney W. Grimes * 323c4dd356SDavid Greenman * from: @(#)vm_object.c 8.5 (Berkeley) 3/22/94 33df8bae1dSRodney W. Grimes * 34df8bae1dSRodney W. Grimes * 35df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 36df8bae1dSRodney W. Grimes * All rights reserved. 37df8bae1dSRodney W. Grimes * 38df8bae1dSRodney W. Grimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 39df8bae1dSRodney W. Grimes * 40df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and 41df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright 42df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the 43df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions 44df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 45df8bae1dSRodney W. Grimes * 46df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 47df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 48df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 49df8bae1dSRodney W. Grimes * 50df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to 51df8bae1dSRodney W. Grimes * 52df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 53df8bae1dSRodney W. Grimes * School of Computer Science 54df8bae1dSRodney W. Grimes * Carnegie Mellon University 55df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890 56df8bae1dSRodney W. Grimes * 57df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 58df8bae1dSRodney W. Grimes * rights to redistribute these changes. 59df8bae1dSRodney W. Grimes */ 60df8bae1dSRodney W. Grimes 61df8bae1dSRodney W. Grimes /* 62df8bae1dSRodney W. Grimes * Virtual memory object module. 63df8bae1dSRodney W. Grimes */ 64df8bae1dSRodney W. Grimes 65874651b1SDavid E. O'Brien #include <sys/cdefs.h> 66874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 67874651b1SDavid E. O'Brien 68f8a47341SAlan Cox #include "opt_vm.h" 69f8a47341SAlan Cox 70df8bae1dSRodney W. Grimes #include <sys/param.h> 71df8bae1dSRodney W. Grimes #include <sys/systm.h> 72fb919e4dSMark Murray #include <sys/lock.h> 73867a482dSJohn Dyson #include <sys/mman.h> 74cf2819ccSJohn Dyson #include <sys/mount.h> 75b9b7a4beSMatthew Dillon #include <sys/kernel.h> 76b9b7a4beSMatthew Dillon #include <sys/sysctl.h> 771b367556SJason Evans #include <sys/mutex.h> 78fb919e4dSMark Murray #include <sys/proc.h> /* for curproc, pageproc */ 79fb919e4dSMark Murray #include <sys/socket.h> 803364c323SKonstantin Belousov #include <sys/resourcevar.h> 81*89f6b863SAttilio Rao #include <sys/rwlock.h> 82fb919e4dSMark Murray #include <sys/vnode.h> 83fb919e4dSMark Murray #include <sys/vmmeter.h> 841005a129SJohn Baldwin #include <sys/sx.h> 85df8bae1dSRodney W. Grimes 86df8bae1dSRodney W. Grimes #include <vm/vm.h> 87efeaf95aSDavid Greenman #include <vm/vm_param.h> 88efeaf95aSDavid Greenman #include <vm/pmap.h> 89efeaf95aSDavid Greenman #include <vm/vm_map.h> 90efeaf95aSDavid Greenman #include <vm/vm_object.h> 91df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 9226f9a767SRodney W. Grimes #include <vm/vm_pageout.h> 930d94caffSDavid Greenman #include <vm/vm_pager.h> 9405f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h> 95a1f6d91cSDavid Greenman #include <vm/vm_kern.h> 96efeaf95aSDavid Greenman #include <vm/vm_extern.h> 97f8a47341SAlan Cox #include <vm/vm_reserv.h> 98670d17b5SJeff Roberson #include <vm/uma.h> 9926f9a767SRodney W. Grimes 100c53f7aceSDag-Erling Smørgrav static int old_msync; 101c53f7aceSDag-Erling Smørgrav SYSCTL_INT(_vm, OID_AUTO, old_msync, CTLFLAG_RW, &old_msync, 0, 102c53f7aceSDag-Erling Smørgrav "Use old (insecure) msync behavior"); 103c53f7aceSDag-Erling Smørgrav 104757216f3SKonstantin Belousov static int vm_object_page_collect_flush(vm_object_t object, vm_page_t p, 105126d6082SKonstantin Belousov int pagerflags, int flags, boolean_t *clearobjflags, 106126d6082SKonstantin Belousov boolean_t *eio); 1073280870dSKonstantin Belousov static boolean_t vm_object_page_remove_write(vm_page_t p, int flags, 108126d6082SKonstantin Belousov boolean_t *clearobjflags); 109b9b7a4beSMatthew Dillon static void vm_object_qcollapse(vm_object_t object); 11002dd8331SAlan Cox static void vm_object_vndeallocate(vm_object_t object); 111f6b04d2bSDavid Greenman 112df8bae1dSRodney W. Grimes /* 113df8bae1dSRodney W. Grimes * Virtual memory objects maintain the actual data 114df8bae1dSRodney W. Grimes * associated with allocated virtual memory. A given 115df8bae1dSRodney W. Grimes * page of memory exists within exactly one object. 116df8bae1dSRodney W. Grimes * 117df8bae1dSRodney W. Grimes * An object is only deallocated when all "references" 118df8bae1dSRodney W. Grimes * are given up. Only one "reference" to a given 119df8bae1dSRodney W. Grimes * region of an object should be writeable. 120df8bae1dSRodney W. Grimes * 121df8bae1dSRodney W. Grimes * Associated with each object is a list of all resident 122df8bae1dSRodney W. Grimes * memory pages belonging to that object; this list is 123df8bae1dSRodney W. Grimes * maintained by the "vm_page" module, and locked by the object's 124df8bae1dSRodney W. Grimes * lock. 125df8bae1dSRodney W. Grimes * 126df8bae1dSRodney W. Grimes * Each object also records a "pager" routine which is 127df8bae1dSRodney W. Grimes * used to retrieve (and store) pages to the proper backing 128df8bae1dSRodney W. Grimes * storage. In addition, objects may be backed by other 129df8bae1dSRodney W. Grimes * objects from which they were virtual-copied. 130df8bae1dSRodney W. Grimes * 131df8bae1dSRodney W. Grimes * The only items within the object structure which are 132df8bae1dSRodney W. Grimes * modified after time of creation are: 133df8bae1dSRodney W. Grimes * reference count locked by object's lock 134df8bae1dSRodney W. Grimes * pager routine locked by object's lock 135df8bae1dSRodney W. Grimes * 136df8bae1dSRodney W. Grimes */ 137df8bae1dSRodney W. Grimes 13828f8db14SBruce Evans struct object_q vm_object_list; 139a5698387SAlan Cox struct mtx vm_object_list_mtx; /* lock for object list and count */ 140cccf11b8SAlan Cox 141cccf11b8SAlan Cox struct vm_object kernel_object_store; 142cccf11b8SAlan Cox struct vm_object kmem_object_store; 143df8bae1dSRodney W. Grimes 1446472ac3dSEd Schouten static SYSCTL_NODE(_vm_stats, OID_AUTO, object, CTLFLAG_RD, 0, 1456472ac3dSEd Schouten "VM object stats"); 146604c2bbcSAlan Cox 147f708ef1bSPoul-Henning Kamp static long object_collapses; 148604c2bbcSAlan Cox SYSCTL_LONG(_vm_stats_object, OID_AUTO, collapses, CTLFLAG_RD, 149604c2bbcSAlan Cox &object_collapses, 0, "VM object collapses"); 150604c2bbcSAlan Cox 151f708ef1bSPoul-Henning Kamp static long object_bypasses; 152604c2bbcSAlan Cox SYSCTL_LONG(_vm_stats_object, OID_AUTO, bypasses, CTLFLAG_RD, 153604c2bbcSAlan Cox &object_bypasses, 0, "VM object bypasses"); 154dad740e9SAlan Cox 155670d17b5SJeff Roberson static uma_zone_t obj_zone; 1568355f576SJeff Roberson 157b23f72e9SBrian Feldman static int vm_object_zinit(void *mem, int size, int flags); 1588355f576SJeff Roberson 1598355f576SJeff Roberson #ifdef INVARIANTS 1608355f576SJeff Roberson static void vm_object_zdtor(void *mem, int size, void *arg); 1618355f576SJeff Roberson 1628355f576SJeff Roberson static void 1638355f576SJeff Roberson vm_object_zdtor(void *mem, int size, void *arg) 1648355f576SJeff Roberson { 1658355f576SJeff Roberson vm_object_t object; 1668355f576SJeff Roberson 1678355f576SJeff Roberson object = (vm_object_t)mem; 16843186e53SAlan Cox KASSERT(TAILQ_EMPTY(&object->memq), 169198da1b2SAttilio Rao ("object %p has resident pages in its memq", object)); 170198da1b2SAttilio Rao KASSERT(object->root == NULL, 171198da1b2SAttilio Rao ("object %p has resident pages in its tree", object)); 172f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 173f8a47341SAlan Cox KASSERT(LIST_EMPTY(&object->rvq), 174f8a47341SAlan Cox ("object %p has reservations", 175f8a47341SAlan Cox object)); 176f8a47341SAlan Cox #endif 177c9341161SAttilio Rao KASSERT(vm_object_cache_is_empty(object), 1787bfda801SAlan Cox ("object %p has cached pages", 1797bfda801SAlan Cox object)); 1808355f576SJeff Roberson KASSERT(object->paging_in_progress == 0, 1818355f576SJeff Roberson ("object %p paging_in_progress = %d", 1828355f576SJeff Roberson object, object->paging_in_progress)); 1838355f576SJeff Roberson KASSERT(object->resident_page_count == 0, 1848355f576SJeff Roberson ("object %p resident_page_count = %d", 1858355f576SJeff Roberson object, object->resident_page_count)); 1868355f576SJeff Roberson KASSERT(object->shadow_count == 0, 1878355f576SJeff Roberson ("object %p shadow_count = %d", 1888355f576SJeff Roberson object, object->shadow_count)); 1898355f576SJeff Roberson } 1908355f576SJeff Roberson #endif 1918355f576SJeff Roberson 192b23f72e9SBrian Feldman static int 193b23f72e9SBrian Feldman vm_object_zinit(void *mem, int size, int flags) 1948355f576SJeff Roberson { 1958355f576SJeff Roberson vm_object_t object; 1968355f576SJeff Roberson 1978355f576SJeff Roberson object = (vm_object_t)mem; 198*89f6b863SAttilio Rao bzero(&object->lock, sizeof(object->lock)); 199*89f6b863SAttilio Rao rw_init_flags(&object->lock, "vm object", RW_DUPOK); 2008355f576SJeff Roberson 2018355f576SJeff Roberson /* These are true for any object that has been freed */ 202198da1b2SAttilio Rao object->root = NULL; 2038355f576SJeff Roberson object->paging_in_progress = 0; 2048355f576SJeff Roberson object->resident_page_count = 0; 2058355f576SJeff Roberson object->shadow_count = 0; 206198da1b2SAttilio Rao object->cache = NULL; 207b23f72e9SBrian Feldman return (0); 2088355f576SJeff Roberson } 209df8bae1dSRodney W. Grimes 210a4915c21SAttilio Rao static void 2116395da54SIan Dowse _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object) 212df8bae1dSRodney W. Grimes { 2130cddd8f0SMatthew Dillon 214df8bae1dSRodney W. Grimes TAILQ_INIT(&object->memq); 2151c500307SAlan Cox LIST_INIT(&object->shadow_head); 216a1f6d91cSDavid Greenman 21724a1cce3SDavid Greenman object->type = type; 21828634820SAlan Cox switch (type) { 21928634820SAlan Cox case OBJT_DEAD: 22028634820SAlan Cox panic("_vm_object_allocate: can't create OBJT_DEAD"); 22128634820SAlan Cox case OBJT_DEFAULT: 22228634820SAlan Cox case OBJT_SWAP: 22328634820SAlan Cox object->flags = OBJ_ONEMAPPING; 22428634820SAlan Cox break; 22528634820SAlan Cox case OBJT_DEVICE: 22628634820SAlan Cox case OBJT_SG: 22728634820SAlan Cox object->flags = OBJ_FICTITIOUS | OBJ_UNMANAGED; 22828634820SAlan Cox break; 22928634820SAlan Cox case OBJT_MGTDEVICE: 23028634820SAlan Cox object->flags = OBJ_FICTITIOUS; 23128634820SAlan Cox break; 23228634820SAlan Cox case OBJT_PHYS: 23328634820SAlan Cox object->flags = OBJ_UNMANAGED; 23428634820SAlan Cox break; 23528634820SAlan Cox case OBJT_VNODE: 23628634820SAlan Cox object->flags = 0; 23728634820SAlan Cox break; 23828634820SAlan Cox default: 23928634820SAlan Cox panic("_vm_object_allocate: type %d is undefined", type); 24028634820SAlan Cox } 241df8bae1dSRodney W. Grimes object->size = size; 242b881da26SAlan Cox object->generation = 1; 243a1f6d91cSDavid Greenman object->ref_count = 1; 2443153e878SAlan Cox object->memattr = VM_MEMATTR_DEFAULT; 245ef694c1aSEdward Tomasz Napierala object->cred = NULL; 2463364c323SKonstantin Belousov object->charge = 0; 24724a1cce3SDavid Greenman object->handle = NULL; 24824a1cce3SDavid Greenman object->backing_object = NULL; 249a316d390SJohn Dyson object->backing_object_offset = (vm_ooffset_t) 0; 250f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 251f8a47341SAlan Cox LIST_INIT(&object->rvq); 252f8a47341SAlan Cox #endif 253a1f6d91cSDavid Greenman 254a5698387SAlan Cox mtx_lock(&vm_object_list_mtx); 25560517fd1SJohn Baldwin TAILQ_INSERT_TAIL(&vm_object_list, object, object_list); 256a5698387SAlan Cox mtx_unlock(&vm_object_list_mtx); 257df8bae1dSRodney W. Grimes } 258df8bae1dSRodney W. Grimes 259df8bae1dSRodney W. Grimes /* 26026f9a767SRodney W. Grimes * vm_object_init: 26126f9a767SRodney W. Grimes * 26226f9a767SRodney W. Grimes * Initialize the VM objects module. 26326f9a767SRodney W. Grimes */ 26426f9a767SRodney W. Grimes void 2651b40f8c0SMatthew Dillon vm_object_init(void) 26626f9a767SRodney W. Grimes { 26726f9a767SRodney W. Grimes TAILQ_INIT(&vm_object_list); 2686008862bSJohn Baldwin mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF); 2690217125fSDavid Greenman 270*89f6b863SAttilio Rao rw_init(&kernel_object->lock, "kernel vm object"); 2719f5c801bSAlan Cox _vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS), 27226f9a767SRodney W. Grimes kernel_object); 273f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 274f8a47341SAlan Cox kernel_object->flags |= OBJ_COLORED; 275f8a47341SAlan Cox kernel_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS); 276f8a47341SAlan Cox #endif 27726f9a767SRodney W. Grimes 278*89f6b863SAttilio Rao rw_init(&kmem_object->lock, "kmem vm object"); 2799f5c801bSAlan Cox _vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS), 28026f9a767SRodney W. Grimes kmem_object); 281f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 282f8a47341SAlan Cox kmem_object->flags |= OBJ_COLORED; 283f8a47341SAlan Cox kmem_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS); 284f8a47341SAlan Cox #endif 285ed6a7863SAlan Cox 2868dbca793STor Egge /* 2878dbca793STor Egge * The lock portion of struct vm_object must be type stable due 2888dbca793STor Egge * to vm_pageout_fallback_object_lock locking a vm object 2898dbca793STor Egge * without holding any references to it. 2908dbca793STor Egge */ 2918355f576SJeff Roberson obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL, 2928355f576SJeff Roberson #ifdef INVARIANTS 2938355f576SJeff Roberson vm_object_zdtor, 2948355f576SJeff Roberson #else 2958355f576SJeff Roberson NULL, 2968355f576SJeff Roberson #endif 297f3c625e4SJeff Roberson vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM|UMA_ZONE_NOFREE); 29899448ed1SJohn Dyson } 29999448ed1SJohn Dyson 30099448ed1SJohn Dyson void 3011b40f8c0SMatthew Dillon vm_object_clear_flag(vm_object_t object, u_short bits) 3021b40f8c0SMatthew Dillon { 3035440b5a9SAlan Cox 304*89f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 305b06805adSJake Burkholder object->flags &= ~bits; 3061b40f8c0SMatthew Dillon } 3071b40f8c0SMatthew Dillon 3083153e878SAlan Cox /* 3093153e878SAlan Cox * Sets the default memory attribute for the specified object. Pages 3103153e878SAlan Cox * that are allocated to this object are by default assigned this memory 3113153e878SAlan Cox * attribute. 3123153e878SAlan Cox * 3133153e878SAlan Cox * Presently, this function must be called before any pages are allocated 3143153e878SAlan Cox * to the object. In the future, this requirement may be relaxed for 3153153e878SAlan Cox * "default" and "swap" objects. 3163153e878SAlan Cox */ 3173153e878SAlan Cox int 3183153e878SAlan Cox vm_object_set_memattr(vm_object_t object, vm_memattr_t memattr) 3193153e878SAlan Cox { 3203153e878SAlan Cox 321*89f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 3223153e878SAlan Cox switch (object->type) { 3233153e878SAlan Cox case OBJT_DEFAULT: 3243153e878SAlan Cox case OBJT_DEVICE: 32596b0b92aSAlan Cox case OBJT_MGTDEVICE: 3263153e878SAlan Cox case OBJT_PHYS: 32701381811SJohn Baldwin case OBJT_SG: 3283153e878SAlan Cox case OBJT_SWAP: 3293153e878SAlan Cox case OBJT_VNODE: 3303153e878SAlan Cox if (!TAILQ_EMPTY(&object->memq)) 3313153e878SAlan Cox return (KERN_FAILURE); 3323153e878SAlan Cox break; 3333153e878SAlan Cox case OBJT_DEAD: 3343153e878SAlan Cox return (KERN_INVALID_ARGUMENT); 33596b0b92aSAlan Cox default: 33696b0b92aSAlan Cox panic("vm_object_set_memattr: object %p is of undefined type", 33796b0b92aSAlan Cox object); 3383153e878SAlan Cox } 3393153e878SAlan Cox object->memattr = memattr; 3403153e878SAlan Cox return (KERN_SUCCESS); 3413153e878SAlan Cox } 3423153e878SAlan Cox 3431b40f8c0SMatthew Dillon void 3441b40f8c0SMatthew Dillon vm_object_pip_add(vm_object_t object, short i) 3451b40f8c0SMatthew Dillon { 346f279b88dSAlan Cox 347*89f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 348b06805adSJake Burkholder object->paging_in_progress += i; 3491b40f8c0SMatthew Dillon } 3501b40f8c0SMatthew Dillon 3511b40f8c0SMatthew Dillon void 3521b40f8c0SMatthew Dillon vm_object_pip_subtract(vm_object_t object, short i) 3531b40f8c0SMatthew Dillon { 354d647a0edSAlan Cox 355*89f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 356b06805adSJake Burkholder object->paging_in_progress -= i; 3571b40f8c0SMatthew Dillon } 3581b40f8c0SMatthew Dillon 3591b40f8c0SMatthew Dillon void 3601b40f8c0SMatthew Dillon vm_object_pip_wakeup(vm_object_t object) 3611b40f8c0SMatthew Dillon { 362f279b88dSAlan Cox 363*89f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 364b06805adSJake Burkholder object->paging_in_progress--; 3651b40f8c0SMatthew Dillon if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) { 3661b40f8c0SMatthew Dillon vm_object_clear_flag(object, OBJ_PIPWNT); 3671b40f8c0SMatthew Dillon wakeup(object); 3681b40f8c0SMatthew Dillon } 3691b40f8c0SMatthew Dillon } 3701b40f8c0SMatthew Dillon 3711b40f8c0SMatthew Dillon void 3721b40f8c0SMatthew Dillon vm_object_pip_wakeupn(vm_object_t object, short i) 3731b40f8c0SMatthew Dillon { 374d647a0edSAlan Cox 375*89f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 3761b40f8c0SMatthew Dillon if (i) 377b06805adSJake Burkholder object->paging_in_progress -= i; 3781b40f8c0SMatthew Dillon if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) { 3791b40f8c0SMatthew Dillon vm_object_clear_flag(object, OBJ_PIPWNT); 3801b40f8c0SMatthew Dillon wakeup(object); 3811b40f8c0SMatthew Dillon } 3821b40f8c0SMatthew Dillon } 3831b40f8c0SMatthew Dillon 3841b40f8c0SMatthew Dillon void 3851b40f8c0SMatthew Dillon vm_object_pip_wait(vm_object_t object, char *waitid) 3861b40f8c0SMatthew Dillon { 3871ca58953SAlan Cox 388*89f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 3891ca58953SAlan Cox while (object->paging_in_progress) { 3901ca58953SAlan Cox object->flags |= OBJ_PIPWNT; 3910dde287bSAttilio Rao VM_OBJECT_SLEEP(object, object, PVM, waitid, 0); 3921ca58953SAlan Cox } 3931b40f8c0SMatthew Dillon } 3941b40f8c0SMatthew Dillon 39526f9a767SRodney W. Grimes /* 39626f9a767SRodney W. Grimes * vm_object_allocate: 39726f9a767SRodney W. Grimes * 39826f9a767SRodney W. Grimes * Returns a new object with the given size. 39926f9a767SRodney W. Grimes */ 40026f9a767SRodney W. Grimes vm_object_t 4016395da54SIan Dowse vm_object_allocate(objtype_t type, vm_pindex_t size) 40226f9a767SRodney W. Grimes { 40390688d13SAlan Cox vm_object_t object; 40490688d13SAlan Cox 40590688d13SAlan Cox object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK); 40690688d13SAlan Cox _vm_object_allocate(type, size, object); 40790688d13SAlan Cox return (object); 40826f9a767SRodney W. Grimes } 40926f9a767SRodney W. Grimes 41026f9a767SRodney W. Grimes 41126f9a767SRodney W. Grimes /* 412df8bae1dSRodney W. Grimes * vm_object_reference: 413df8bae1dSRodney W. Grimes * 41415347817SAlan Cox * Gets another reference to the given object. Note: OBJ_DEAD 41515347817SAlan Cox * objects can be referenced during final cleaning. 416df8bae1dSRodney W. Grimes */ 4176476c0d2SJohn Dyson void 4181b40f8c0SMatthew Dillon vm_object_reference(vm_object_t object) 419df8bae1dSRodney W. Grimes { 420df8bae1dSRodney W. Grimes if (object == NULL) 421df8bae1dSRodney W. Grimes return; 422*89f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 42352481a9aSJeff Roberson vm_object_reference_locked(object); 424*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 42595e5e988SJohn Dyson } 42695e5e988SJohn Dyson 42723955314SAlfred Perlstein /* 428b921a12bSAlan Cox * vm_object_reference_locked: 429b921a12bSAlan Cox * 430b921a12bSAlan Cox * Gets another reference to the given object. 431b921a12bSAlan Cox * 432b921a12bSAlan Cox * The object must be locked. 433b921a12bSAlan Cox */ 434b921a12bSAlan Cox void 435b921a12bSAlan Cox vm_object_reference_locked(vm_object_t object) 436b921a12bSAlan Cox { 437b921a12bSAlan Cox struct vnode *vp; 438b921a12bSAlan Cox 439*89f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 440b921a12bSAlan Cox object->ref_count++; 441b921a12bSAlan Cox if (object->type == OBJT_VNODE) { 442b921a12bSAlan Cox vp = object->handle; 443b921a12bSAlan Cox vref(vp); 444b921a12bSAlan Cox } 445b921a12bSAlan Cox } 446b921a12bSAlan Cox 447b921a12bSAlan Cox /* 4489d5abbddSJens Schweikhardt * Handle deallocating an object of type OBJT_VNODE. 44923955314SAlfred Perlstein */ 45002dd8331SAlan Cox static void 4511b40f8c0SMatthew Dillon vm_object_vndeallocate(vm_object_t object) 45295e5e988SJohn Dyson { 45395e5e988SJohn Dyson struct vnode *vp = (struct vnode *) object->handle; 454219cbf59SEivind Eklund 455*89f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 4565526d2d9SEivind Eklund KASSERT(object->type == OBJT_VNODE, 4575526d2d9SEivind Eklund ("vm_object_vndeallocate: not a vnode object")); 458219cbf59SEivind Eklund KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp")); 459219cbf59SEivind Eklund #ifdef INVARIANTS 46095e5e988SJohn Dyson if (object->ref_count == 0) { 46195e5e988SJohn Dyson vprint("vm_object_vndeallocate", vp); 46295e5e988SJohn Dyson panic("vm_object_vndeallocate: bad object reference count"); 46395e5e988SJohn Dyson } 46495e5e988SJohn Dyson #endif 46595e5e988SJohn Dyson 46603fa5b34SKonstantin Belousov if (object->ref_count > 1) { 46795e5e988SJohn Dyson object->ref_count--; 468*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 46903fa5b34SKonstantin Belousov /* vrele may need the vnode lock. */ 47047221757SJohn Dyson vrele(vp); 47103fa5b34SKonstantin Belousov } else { 47286769ac0SKonstantin Belousov vhold(vp); 473*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 47403fa5b34SKonstantin Belousov vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 47586769ac0SKonstantin Belousov vdrop(vp); 476*89f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 47703fa5b34SKonstantin Belousov object->ref_count--; 47886769ac0SKonstantin Belousov if (object->type == OBJT_DEAD) { 479*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 48086769ac0SKonstantin Belousov VOP_UNLOCK(vp, 0); 48186769ac0SKonstantin Belousov } else { 48203fa5b34SKonstantin Belousov if (object->ref_count == 0) 483877d24acSKonstantin Belousov VOP_UNSET_TEXT(vp); 484*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 48503fa5b34SKonstantin Belousov vput(vp); 48603fa5b34SKonstantin Belousov } 487df8bae1dSRodney W. Grimes } 48886769ac0SKonstantin Belousov } 489df8bae1dSRodney W. Grimes 490df8bae1dSRodney W. Grimes /* 491df8bae1dSRodney W. Grimes * vm_object_deallocate: 492df8bae1dSRodney W. Grimes * 493df8bae1dSRodney W. Grimes * Release a reference to the specified object, 494df8bae1dSRodney W. Grimes * gained either through a vm_object_allocate 495df8bae1dSRodney W. Grimes * or a vm_object_reference call. When all references 496df8bae1dSRodney W. Grimes * are gone, storage associated with this object 497df8bae1dSRodney W. Grimes * may be relinquished. 498df8bae1dSRodney W. Grimes * 499df8bae1dSRodney W. Grimes * No object may be locked. 500df8bae1dSRodney W. Grimes */ 50126f9a767SRodney W. Grimes void 5021b40f8c0SMatthew Dillon vm_object_deallocate(vm_object_t object) 503df8bae1dSRodney W. Grimes { 504df8bae1dSRodney W. Grimes vm_object_t temp; 505df8bae1dSRodney W. Grimes 506df8bae1dSRodney W. Grimes while (object != NULL) { 507*89f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 5083b68228cSAlan Cox if (object->type == OBJT_VNODE) { 50995e5e988SJohn Dyson vm_object_vndeallocate(object); 51023b186d3SAlan Cox return; 5115050aa86SKonstantin Belousov } 51295e5e988SJohn Dyson 5138125b1e6SAlfred Perlstein KASSERT(object->ref_count != 0, 5148125b1e6SAlfred Perlstein ("vm_object_deallocate: object deallocated too many times: %d", object->type)); 5152be70f79SJohn Dyson 5162be70f79SJohn Dyson /* 5178125b1e6SAlfred Perlstein * If the reference count goes to 0 we start calling 5188125b1e6SAlfred Perlstein * vm_object_terminate() on the object chain. 5198125b1e6SAlfred Perlstein * A ref count of 1 may be a special case depending on the 5208125b1e6SAlfred Perlstein * shadow count being 0 or 1. 5212be70f79SJohn Dyson */ 522c0877f10SJohn Dyson object->ref_count--; 5238125b1e6SAlfred Perlstein if (object->ref_count > 1) { 524*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 52523b186d3SAlan Cox return; 5268125b1e6SAlfred Perlstein } else if (object->ref_count == 1) { 5274c8e0452SAlan Cox if (object->shadow_count == 0 && 5284c8e0452SAlan Cox object->handle == NULL && 5294c8e0452SAlan Cox (object->type == OBJT_DEFAULT || 5304c8e0452SAlan Cox object->type == OBJT_SWAP)) { 5318125b1e6SAlfred Perlstein vm_object_set_flag(object, OBJ_ONEMAPPING); 5328125b1e6SAlfred Perlstein } else if ((object->shadow_count == 1) && 5338125b1e6SAlfred Perlstein (object->handle == NULL) && 53424a1cce3SDavid Greenman (object->type == OBJT_DEFAULT || 53524a1cce3SDavid Greenman object->type == OBJT_SWAP)) { 536a1f6d91cSDavid Greenman vm_object_t robject; 53795e5e988SJohn Dyson 5381c500307SAlan Cox robject = LIST_FIRST(&object->shadow_head); 5395526d2d9SEivind Eklund KASSERT(robject != NULL, 540219cbf59SEivind Eklund ("vm_object_deallocate: ref_count: %d, shadow_count: %d", 5415526d2d9SEivind Eklund object->ref_count, 5425526d2d9SEivind Eklund object->shadow_count)); 543*89f6b863SAttilio Rao if (!VM_OBJECT_TRYWLOCK(robject)) { 544b72b0115SAlan Cox /* 545b72b0115SAlan Cox * Avoid a potential deadlock. 546b72b0115SAlan Cox */ 547b72b0115SAlan Cox object->ref_count++; 548*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 549a7d86121SAlan Cox /* 550a7d86121SAlan Cox * More likely than not the thread 551a7d86121SAlan Cox * holding robject's lock has lower 552a7d86121SAlan Cox * priority than the current thread. 553a7d86121SAlan Cox * Let the lower priority thread run. 554a7d86121SAlan Cox */ 5558db5fc58SJohn Baldwin pause("vmo_de", 1); 556b72b0115SAlan Cox continue; 557b72b0115SAlan Cox } 558d936694fSAlan Cox /* 559d936694fSAlan Cox * Collapse object into its shadow unless its 560d936694fSAlan Cox * shadow is dead. In that case, object will 561d936694fSAlan Cox * be deallocated by the thread that is 562d936694fSAlan Cox * deallocating its shadow. 563d936694fSAlan Cox */ 564d936694fSAlan Cox if ((robject->flags & OBJ_DEAD) == 0 && 565d936694fSAlan Cox (robject->handle == NULL) && 56624a1cce3SDavid Greenman (robject->type == OBJT_DEFAULT || 56724a1cce3SDavid Greenman robject->type == OBJT_SWAP)) { 568a1f6d91cSDavid Greenman 56995e5e988SJohn Dyson robject->ref_count++; 570138449dcSAlan Cox retry: 571138449dcSAlan Cox if (robject->paging_in_progress) { 572*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 573138449dcSAlan Cox vm_object_pip_wait(robject, 574138449dcSAlan Cox "objde1"); 5752e9f4a69SAlan Cox temp = robject->backing_object; 5762e9f4a69SAlan Cox if (object == temp) { 577*89f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 578138449dcSAlan Cox goto retry; 5792e9f4a69SAlan Cox } 580138449dcSAlan Cox } else if (object->paging_in_progress) { 581*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(robject); 582138449dcSAlan Cox object->flags |= OBJ_PIPWNT; 5830dde287bSAttilio Rao VM_OBJECT_SLEEP(object, object, 584138449dcSAlan Cox PDROP | PVM, "objde2", 0); 585*89f6b863SAttilio Rao VM_OBJECT_WLOCK(robject); 5862e9f4a69SAlan Cox temp = robject->backing_object; 5872e9f4a69SAlan Cox if (object == temp) { 588*89f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 589138449dcSAlan Cox goto retry; 590a1f6d91cSDavid Greenman } 5912e9f4a69SAlan Cox } else 592*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 5932e9f4a69SAlan Cox 59495e5e988SJohn Dyson if (robject->ref_count == 1) { 59595e5e988SJohn Dyson robject->ref_count--; 596ba8da839SDavid Greenman object = robject; 59795e5e988SJohn Dyson goto doterm; 59895e5e988SJohn Dyson } 59995e5e988SJohn Dyson object = robject; 60095e5e988SJohn Dyson vm_object_collapse(object); 601*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 602ba8da839SDavid Greenman continue; 603a1f6d91cSDavid Greenman } 604*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(robject); 60595e5e988SJohn Dyson } 606*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 60723b186d3SAlan Cox return; 60895e5e988SJohn Dyson } 60995e5e988SJohn Dyson doterm: 61024a1cce3SDavid Greenman temp = object->backing_object; 611c9917419SAlan Cox if (temp != NULL) { 612*89f6b863SAttilio Rao VM_OBJECT_WLOCK(temp); 6131c500307SAlan Cox LIST_REMOVE(object, shadow_list); 61495e5e988SJohn Dyson temp->shadow_count--; 615*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(temp); 61695461b45SJohn Dyson object->backing_object = NULL; 617de5f6a77SJohn Dyson } 618245df27cSMatthew Dillon /* 619245df27cSMatthew Dillon * Don't double-terminate, we could be in a termination 620245df27cSMatthew Dillon * recursion due to the terminate having to sync data 621245df27cSMatthew Dillon * to disk. 622245df27cSMatthew Dillon */ 623245df27cSMatthew Dillon if ((object->flags & OBJ_DEAD) == 0) 624df8bae1dSRodney W. Grimes vm_object_terminate(object); 625c829b9d0SAlan Cox else 626*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 627df8bae1dSRodney W. Grimes object = temp; 628df8bae1dSRodney W. Grimes } 629df8bae1dSRodney W. Grimes } 630df8bae1dSRodney W. Grimes 631df8bae1dSRodney W. Grimes /* 6322ac78f0eSStephan Uphoff * vm_object_destroy removes the object from the global object list 6332ac78f0eSStephan Uphoff * and frees the space for the object. 6342ac78f0eSStephan Uphoff */ 6352ac78f0eSStephan Uphoff void 6362ac78f0eSStephan Uphoff vm_object_destroy(vm_object_t object) 6372ac78f0eSStephan Uphoff { 6382ac78f0eSStephan Uphoff 6392ac78f0eSStephan Uphoff /* 6402ac78f0eSStephan Uphoff * Remove the object from the global object list. 6412ac78f0eSStephan Uphoff */ 6422ac78f0eSStephan Uphoff mtx_lock(&vm_object_list_mtx); 6432ac78f0eSStephan Uphoff TAILQ_REMOVE(&vm_object_list, object, object_list); 6442ac78f0eSStephan Uphoff mtx_unlock(&vm_object_list_mtx); 6452ac78f0eSStephan Uphoff 6462ac78f0eSStephan Uphoff /* 6473364c323SKonstantin Belousov * Release the allocation charge. 6483364c323SKonstantin Belousov */ 649ef694c1aSEdward Tomasz Napierala if (object->cred != NULL) { 6503364c323SKonstantin Belousov KASSERT(object->type == OBJT_DEFAULT || 6513364c323SKonstantin Belousov object->type == OBJT_SWAP, 652ef694c1aSEdward Tomasz Napierala ("vm_object_terminate: non-swap obj %p has cred", 6533364c323SKonstantin Belousov object)); 654ef694c1aSEdward Tomasz Napierala swap_release_by_cred(object->charge, object->cred); 6553364c323SKonstantin Belousov object->charge = 0; 656ef694c1aSEdward Tomasz Napierala crfree(object->cred); 657ef694c1aSEdward Tomasz Napierala object->cred = NULL; 6583364c323SKonstantin Belousov } 6593364c323SKonstantin Belousov 6603364c323SKonstantin Belousov /* 6612ac78f0eSStephan Uphoff * Free the space for the object. 6622ac78f0eSStephan Uphoff */ 6632ac78f0eSStephan Uphoff uma_zfree(obj_zone, object); 6642ac78f0eSStephan Uphoff } 6652ac78f0eSStephan Uphoff 6662ac78f0eSStephan Uphoff /* 667df8bae1dSRodney W. Grimes * vm_object_terminate actually destroys the specified object, freeing 668df8bae1dSRodney W. Grimes * up all previously used resources. 669df8bae1dSRodney W. Grimes * 670df8bae1dSRodney W. Grimes * The object must be locked. 6711c7c3c6aSMatthew Dillon * This routine may block. 672df8bae1dSRodney W. Grimes */ 67395e5e988SJohn Dyson void 6741b40f8c0SMatthew Dillon vm_object_terminate(vm_object_t object) 675df8bae1dSRodney W. Grimes { 67617ea6f00SAlan Cox vm_page_t p, p_next; 677df8bae1dSRodney W. Grimes 678*89f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 6790cddd8f0SMatthew Dillon 68095e5e988SJohn Dyson /* 68195e5e988SJohn Dyson * Make sure no one uses us. 68295e5e988SJohn Dyson */ 683069e9bc1SDoug Rabson vm_object_set_flag(object, OBJ_DEAD); 6843c631446SJohn Dyson 685df8bae1dSRodney W. Grimes /* 686f6b04d2bSDavid Greenman * wait for the pageout daemon to be done with the object 687df8bae1dSRodney W. Grimes */ 68866095752SJohn Dyson vm_object_pip_wait(object, "objtrm"); 689df8bae1dSRodney W. Grimes 6905526d2d9SEivind Eklund KASSERT(!object->paging_in_progress, 6915526d2d9SEivind Eklund ("vm_object_terminate: pageout in progress")); 69226f9a767SRodney W. Grimes 69326f9a767SRodney W. Grimes /* 6940d94caffSDavid Greenman * Clean and free the pages, as appropriate. All references to the 6950d94caffSDavid Greenman * object are gone, so we don't need to lock it. 69626f9a767SRodney W. Grimes */ 69724a1cce3SDavid Greenman if (object->type == OBJT_VNODE) { 698f7dd7b63SAlan Cox struct vnode *vp = (struct vnode *)object->handle; 69995e5e988SJohn Dyson 70095e5e988SJohn Dyson /* 70195e5e988SJohn Dyson * Clean pages and flush buffers. 70295e5e988SJohn Dyson */ 7038f9110f6SJohn Dyson vm_object_page_clean(object, 0, 0, OBJPC_SYNC); 704*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 70595e5e988SJohn Dyson 7060d7935fdSAttilio Rao vinvalbuf(vp, V_SAVE, 0, 0); 707f7dd7b63SAlan Cox 708*89f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 709bef608bdSJohn Dyson } 710bef608bdSJohn Dyson 711971dd342SAlfred Perlstein KASSERT(object->ref_count == 0, 712971dd342SAlfred Perlstein ("vm_object_terminate: object with references, ref_count=%d", 713971dd342SAlfred Perlstein object->ref_count)); 714996c772fSJohn Dyson 7150d94caffSDavid Greenman /* 71617ea6f00SAlan Cox * Free any remaining pageable pages. This also removes them from the 71717ea6f00SAlan Cox * paging queues. However, don't free wired pages, just remove them 71817ea6f00SAlan Cox * from the object. Rather than incrementally removing each page from 71917ea6f00SAlan Cox * the object, the page and object are reset to any empty state. 720df8bae1dSRodney W. Grimes */ 72117ea6f00SAlan Cox TAILQ_FOREACH_SAFE(p, &object->memq, listq, p_next) { 7229af80719SAlan Cox KASSERT(!p->busy && (p->oflags & VPO_BUSY) == 0, 72317ea6f00SAlan Cox ("vm_object_terminate: freeing busy page %p", p)); 7242965a453SKip Macy vm_page_lock(p); 72517ea6f00SAlan Cox /* 72617ea6f00SAlan Cox * Optimize the page's removal from the object by resetting 72717ea6f00SAlan Cox * its "object" field. Specifically, if the page is not 72817ea6f00SAlan Cox * wired, then the effect of this assignment is that 72917ea6f00SAlan Cox * vm_page_free()'s call to vm_page_remove() will return 73017ea6f00SAlan Cox * immediately without modifying the page or the object. 73117ea6f00SAlan Cox */ 73217ea6f00SAlan Cox p->object = NULL; 7330b10ba98SDavid Greenman if (p->wire_count == 0) { 734df8bae1dSRodney W. Grimes vm_page_free(p); 73570721880SAlan Cox PCPU_INC(cnt.v_pfree); 73617ea6f00SAlan Cox } 7372965a453SKip Macy vm_page_unlock(p); 7382965a453SKip Macy } 73917ea6f00SAlan Cox /* 74017ea6f00SAlan Cox * If the object contained any pages, then reset it to an empty state. 74117ea6f00SAlan Cox * None of the object's fields, including "resident_page_count", were 74217ea6f00SAlan Cox * modified by the preceding loop. 74317ea6f00SAlan Cox */ 74417ea6f00SAlan Cox if (object->resident_page_count != 0) { 74517ea6f00SAlan Cox object->root = NULL; 74617ea6f00SAlan Cox TAILQ_INIT(&object->memq); 74717ea6f00SAlan Cox object->resident_page_count = 0; 74817ea6f00SAlan Cox if (object->type == OBJT_VNODE) 74917ea6f00SAlan Cox vdrop(object->handle); 75017ea6f00SAlan Cox } 751bef608bdSJohn Dyson 752f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 753f8a47341SAlan Cox if (__predict_false(!LIST_EMPTY(&object->rvq))) 754f8a47341SAlan Cox vm_reserv_break_all(object); 755f8a47341SAlan Cox #endif 756c9341161SAttilio Rao if (__predict_false(!vm_object_cache_is_empty(object))) 757c9444914SAlan Cox vm_page_cache_free(object, 0, 0); 7587bfda801SAlan Cox 7592d8acc0fSJohn Dyson /* 7609fcfb650SDavid Greenman * Let the pager know object is dead. 7619fcfb650SDavid Greenman */ 7629fcfb650SDavid Greenman vm_pager_deallocate(object); 763*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 7649fcfb650SDavid Greenman 7652ac78f0eSStephan Uphoff vm_object_destroy(object); 76647221757SJohn Dyson } 767df8bae1dSRodney W. Grimes 768edf93b25SAlan Cox /* 769edf93b25SAlan Cox * Make the page read-only so that we can clear the object flags. However, if 770edf93b25SAlan Cox * this is a nosync mmap then the object is likely to stay dirty so do not 771edf93b25SAlan Cox * mess with the page and do not clear the object flags. Returns TRUE if the 772edf93b25SAlan Cox * page should be flushed, and FALSE otherwise. 773edf93b25SAlan Cox */ 7743280870dSKonstantin Belousov static boolean_t 775126d6082SKonstantin Belousov vm_object_page_remove_write(vm_page_t p, int flags, boolean_t *clearobjflags) 7763280870dSKonstantin Belousov { 7773280870dSKonstantin Belousov 7783280870dSKonstantin Belousov /* 7793280870dSKonstantin Belousov * If we have been asked to skip nosync pages and this is a 7803280870dSKonstantin Belousov * nosync page, skip it. Note that the object flags were not 7813280870dSKonstantin Belousov * cleared in this case so we do not have to set them. 7823280870dSKonstantin Belousov */ 7833280870dSKonstantin Belousov if ((flags & OBJPC_NOSYNC) != 0 && (p->oflags & VPO_NOSYNC) != 0) { 784126d6082SKonstantin Belousov *clearobjflags = FALSE; 7853280870dSKonstantin Belousov return (FALSE); 7863280870dSKonstantin Belousov } else { 7873280870dSKonstantin Belousov pmap_remove_write(p); 7883280870dSKonstantin Belousov return (p->dirty != 0); 7893280870dSKonstantin Belousov } 7903280870dSKonstantin Belousov } 7913280870dSKonstantin Belousov 792df8bae1dSRodney W. Grimes /* 793df8bae1dSRodney W. Grimes * vm_object_page_clean 794df8bae1dSRodney W. Grimes * 7954f79d873SMatthew Dillon * Clean all dirty pages in the specified range of object. Leaves page 7964f79d873SMatthew Dillon * on whatever queue it is currently on. If NOSYNC is set then do not 797b146f9e5SAlan Cox * write out pages with VPO_NOSYNC set (originally comes from MAP_NOSYNC), 7984f79d873SMatthew Dillon * leaving the object dirty. 79926f9a767SRodney W. Grimes * 80043b7990eSMatthew Dillon * When stuffing pages asynchronously, allow clustering. XXX we need a 80143b7990eSMatthew Dillon * synchronous clustering mode implementation. 80243b7990eSMatthew Dillon * 80326f9a767SRodney W. Grimes * Odd semantics: if start == end, we clean everything. 80426f9a767SRodney W. Grimes * 80526f9a767SRodney W. Grimes * The object must be locked. 806126d6082SKonstantin Belousov * 807126d6082SKonstantin Belousov * Returns FALSE if some page from the range was not written, as 808126d6082SKonstantin Belousov * reported by the pager, and TRUE otherwise. 80926f9a767SRodney W. Grimes */ 810126d6082SKonstantin Belousov boolean_t 81117f3095dSAlan Cox vm_object_page_clean(vm_object_t object, vm_ooffset_t start, vm_ooffset_t end, 812e239bb97SKonstantin Belousov int flags) 813f6b04d2bSDavid Greenman { 814e239bb97SKonstantin Belousov vm_page_t np, p; 81517f3095dSAlan Cox vm_pindex_t pi, tend, tstart; 816126d6082SKonstantin Belousov int curgeneration, n, pagerflags; 817126d6082SKonstantin Belousov boolean_t clearobjflags, eio, res; 818f6b04d2bSDavid Greenman 819*89f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 820757216f3SKonstantin Belousov KASSERT(object->type == OBJT_VNODE, ("Not a vnode object")); 821e239bb97SKonstantin Belousov if ((object->flags & OBJ_MIGHTBEDIRTY) == 0 || 822e239bb97SKonstantin Belousov object->resident_page_count == 0) 823126d6082SKonstantin Belousov return (TRUE); 824f6b04d2bSDavid Greenman 825e239bb97SKonstantin Belousov pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) != 0 ? 826e239bb97SKonstantin Belousov VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK; 827e239bb97SKonstantin Belousov pagerflags |= (flags & OBJPC_INVAL) != 0 ? VM_PAGER_PUT_INVAL : 0; 828e239bb97SKonstantin Belousov 82917f3095dSAlan Cox tstart = OFF_TO_IDX(start); 83017f3095dSAlan Cox tend = (end == 0) ? object->size : OFF_TO_IDX(end + PAGE_MASK); 83117f3095dSAlan Cox clearobjflags = tstart == 0 && tend >= object->size; 832126d6082SKonstantin Belousov res = TRUE; 833f6b04d2bSDavid Greenman 834bd7e5f99SJohn Dyson rescan: 8352d8acc0fSJohn Dyson curgeneration = object->generation; 8362d8acc0fSJohn Dyson 83717f3095dSAlan Cox for (p = vm_page_find_least(object, tstart); p != NULL; p = np) { 838bd7e5f99SJohn Dyson pi = p->pindex; 839e239bb97SKonstantin Belousov if (pi >= tend) 840e239bb97SKonstantin Belousov break; 841e239bb97SKonstantin Belousov np = TAILQ_NEXT(p, listq); 842e239bb97SKonstantin Belousov if (p->valid == 0) 843aef922f5SJohn Dyson continue; 844780636b7SKonstantin Belousov if (vm_page_sleep_if_busy(p, TRUE, "vpcwai")) { 845e65919f9SKonstantin Belousov if (object->generation != curgeneration) { 846e65919f9SKonstantin Belousov if ((flags & OBJPC_SYNC) != 0) 847e239bb97SKonstantin Belousov goto rescan; 848e65919f9SKonstantin Belousov else 849126d6082SKonstantin Belousov clearobjflags = FALSE; 850e65919f9SKonstantin Belousov } 851780636b7SKonstantin Belousov np = vm_page_find_least(object, pi); 852780636b7SKonstantin Belousov continue; 853f6b04d2bSDavid Greenman } 8543280870dSKonstantin Belousov if (!vm_object_page_remove_write(p, flags, &clearobjflags)) 855bd7e5f99SJohn Dyson continue; 856e239bb97SKonstantin Belousov 8573280870dSKonstantin Belousov n = vm_object_page_collect_flush(object, p, pagerflags, 858126d6082SKonstantin Belousov flags, &clearobjflags, &eio); 859126d6082SKonstantin Belousov if (eio) { 860126d6082SKonstantin Belousov res = FALSE; 861126d6082SKonstantin Belousov clearobjflags = FALSE; 862126d6082SKonstantin Belousov } 863e65919f9SKonstantin Belousov if (object->generation != curgeneration) { 864e65919f9SKonstantin Belousov if ((flags & OBJPC_SYNC) != 0) 865b9b7a4beSMatthew Dillon goto rescan; 866e65919f9SKonstantin Belousov else 867126d6082SKonstantin Belousov clearobjflags = FALSE; 868e65919f9SKonstantin Belousov } 869031ec8c1SKonstantin Belousov 870031ec8c1SKonstantin Belousov /* 871031ec8c1SKonstantin Belousov * If the VOP_PUTPAGES() did a truncated write, so 872031ec8c1SKonstantin Belousov * that even the first page of the run is not fully 873031ec8c1SKonstantin Belousov * written, vm_pageout_flush() returns 0 as the run 874031ec8c1SKonstantin Belousov * length. Since the condition that caused truncated 875031ec8c1SKonstantin Belousov * write may be permanent, e.g. exhausted free space, 876031ec8c1SKonstantin Belousov * accepting n == 0 would cause an infinite loop. 877031ec8c1SKonstantin Belousov * 878031ec8c1SKonstantin Belousov * Forwarding the iterator leaves the unwritten page 879031ec8c1SKonstantin Belousov * behind, but there is not much we can do there if 880031ec8c1SKonstantin Belousov * filesystem refuses to write it. 881031ec8c1SKonstantin Belousov */ 882126d6082SKonstantin Belousov if (n == 0) { 883031ec8c1SKonstantin Belousov n = 1; 884126d6082SKonstantin Belousov clearobjflags = FALSE; 885126d6082SKonstantin Belousov } 886e239bb97SKonstantin Belousov np = vm_page_find_least(object, pi + n); 887b9b7a4beSMatthew Dillon } 888b9b7a4beSMatthew Dillon #if 0 889e239bb97SKonstantin Belousov VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC) ? MNT_WAIT : 0); 890b9b7a4beSMatthew Dillon #endif 891b9b7a4beSMatthew Dillon 892edf93b25SAlan Cox if (clearobjflags) 8933280870dSKonstantin Belousov vm_object_clear_flag(object, OBJ_MIGHTBEDIRTY); 894126d6082SKonstantin Belousov return (res); 895b9b7a4beSMatthew Dillon } 896b9b7a4beSMatthew Dillon 897b9b7a4beSMatthew Dillon static int 8983280870dSKonstantin Belousov vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int pagerflags, 899126d6082SKonstantin Belousov int flags, boolean_t *clearobjflags, boolean_t *eio) 900b9b7a4beSMatthew Dillon { 9013157c503SKonstantin Belousov vm_page_t ma[vm_pageout_page_count], p_first, tp; 9023157c503SKonstantin Belousov int count, i, mreq, runlen; 903b9b7a4beSMatthew Dillon 9047bec141bSKip Macy vm_page_lock_assert(p, MA_NOTOWNED); 905*89f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 9063157c503SKonstantin Belousov 9073157c503SKonstantin Belousov count = 1; 9083157c503SKonstantin Belousov mreq = 0; 9093157c503SKonstantin Belousov 9103157c503SKonstantin Belousov for (tp = p; count < vm_pageout_page_count; count++) { 9113157c503SKonstantin Belousov tp = vm_page_next(tp); 912e239bb97SKonstantin Belousov if (tp == NULL || tp->busy != 0 || (tp->oflags & VPO_BUSY) != 0) 913bd7e5f99SJohn Dyson break; 9143280870dSKonstantin Belousov if (!vm_object_page_remove_write(tp, flags, clearobjflags)) 915bd7e5f99SJohn Dyson break; 916bd7e5f99SJohn Dyson } 917aef922f5SJohn Dyson 9183157c503SKonstantin Belousov for (p_first = p; count < vm_pageout_page_count; count++) { 9193157c503SKonstantin Belousov tp = vm_page_prev(p_first); 920e239bb97SKonstantin Belousov if (tp == NULL || tp->busy != 0 || (tp->oflags & VPO_BUSY) != 0) 921bd7e5f99SJohn Dyson break; 9223280870dSKonstantin Belousov if (!vm_object_page_remove_write(tp, flags, clearobjflags)) 923bd7e5f99SJohn Dyson break; 9243157c503SKonstantin Belousov p_first = tp; 9253157c503SKonstantin Belousov mreq++; 926bd7e5f99SJohn Dyson } 927bd7e5f99SJohn Dyson 9283157c503SKonstantin Belousov for (tp = p_first, i = 0; i < count; tp = TAILQ_NEXT(tp, listq), i++) 9293157c503SKonstantin Belousov ma[i] = tp; 930cf2819ccSJohn Dyson 931126d6082SKonstantin Belousov vm_pageout_flush(ma, count, pagerflags, mreq, &runlen, eio); 9321e8a675cSKonstantin Belousov return (runlen); 93326f9a767SRodney W. Grimes } 934df8bae1dSRodney W. Grimes 9351efb74fbSJohn Dyson /* 936950f8459SAlan Cox * Note that there is absolutely no sense in writing out 937950f8459SAlan Cox * anonymous objects, so we track down the vnode object 938950f8459SAlan Cox * to write out. 939950f8459SAlan Cox * We invalidate (remove) all pages from the address space 940950f8459SAlan Cox * for semantic correctness. 941950f8459SAlan Cox * 9426bbee8e2SAlan Cox * If the backing object is a device object with unmanaged pages, then any 9436bbee8e2SAlan Cox * mappings to the specified range of pages must be removed before this 9446bbee8e2SAlan Cox * function is called. 9456bbee8e2SAlan Cox * 946950f8459SAlan Cox * Note: certain anonymous maps, such as MAP_NOSYNC maps, 947950f8459SAlan Cox * may start out with a NULL object. 948950f8459SAlan Cox */ 949126d6082SKonstantin Belousov boolean_t 950950f8459SAlan Cox vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size, 951950f8459SAlan Cox boolean_t syncio, boolean_t invalidate) 952950f8459SAlan Cox { 953950f8459SAlan Cox vm_object_t backing_object; 954950f8459SAlan Cox struct vnode *vp; 9553b582b4eSTor Egge struct mount *mp; 956126d6082SKonstantin Belousov int error, flags, fsync_after; 957126d6082SKonstantin Belousov boolean_t res; 958950f8459SAlan Cox 959950f8459SAlan Cox if (object == NULL) 960126d6082SKonstantin Belousov return (TRUE); 961126d6082SKonstantin Belousov res = TRUE; 962126d6082SKonstantin Belousov error = 0; 963*89f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 964950f8459SAlan Cox while ((backing_object = object->backing_object) != NULL) { 965*89f6b863SAttilio Rao VM_OBJECT_WLOCK(backing_object); 96656e0670fSAlan Cox offset += object->backing_object_offset; 967*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 968950f8459SAlan Cox object = backing_object; 969950f8459SAlan Cox if (object->size < OFF_TO_IDX(offset + size)) 970950f8459SAlan Cox size = IDX_TO_OFF(object->size) - offset; 971950f8459SAlan Cox } 972950f8459SAlan Cox /* 973950f8459SAlan Cox * Flush pages if writing is allowed, invalidate them 974950f8459SAlan Cox * if invalidation requested. Pages undergoing I/O 975950f8459SAlan Cox * will be ignored by vm_object_page_remove(). 976950f8459SAlan Cox * 977950f8459SAlan Cox * We cannot lock the vnode and then wait for paging 978950f8459SAlan Cox * to complete without deadlocking against vm_fault. 979950f8459SAlan Cox * Instead we simply call vm_object_page_remove() and 980950f8459SAlan Cox * allow it to block internally on a page-by-page 981950f8459SAlan Cox * basis when it encounters pages undergoing async 982950f8459SAlan Cox * I/O. 983950f8459SAlan Cox */ 984950f8459SAlan Cox if (object->type == OBJT_VNODE && 985950f8459SAlan Cox (object->flags & OBJ_MIGHTBEDIRTY) != 0) { 986950f8459SAlan Cox vp = object->handle; 987*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 9883b582b4eSTor Egge (void) vn_start_write(vp, &mp, V_WAIT); 989cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 99075ff604aSKonstantin Belousov if (syncio && !invalidate && offset == 0 && 99175ff604aSKonstantin Belousov OFF_TO_IDX(size) == object->size) { 99275ff604aSKonstantin Belousov /* 99375ff604aSKonstantin Belousov * If syncing the whole mapping of the file, 99475ff604aSKonstantin Belousov * it is faster to schedule all the writes in 99575ff604aSKonstantin Belousov * async mode, also allowing the clustering, 99675ff604aSKonstantin Belousov * and then wait for i/o to complete. 99775ff604aSKonstantin Belousov */ 99875ff604aSKonstantin Belousov flags = 0; 99975ff604aSKonstantin Belousov fsync_after = TRUE; 100075ff604aSKonstantin Belousov } else { 1001950f8459SAlan Cox flags = (syncio || invalidate) ? OBJPC_SYNC : 0; 100275ff604aSKonstantin Belousov flags |= invalidate ? (OBJPC_SYNC | OBJPC_INVAL) : 0; 100375ff604aSKonstantin Belousov fsync_after = FALSE; 100475ff604aSKonstantin Belousov } 1005*89f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1006126d6082SKonstantin Belousov res = vm_object_page_clean(object, offset, offset + size, 1007126d6082SKonstantin Belousov flags); 1008*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 100975ff604aSKonstantin Belousov if (fsync_after) 1010126d6082SKonstantin Belousov error = VOP_FSYNC(vp, MNT_WAIT, curthread); 101122db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 10123b582b4eSTor Egge vn_finished_write(mp); 1013126d6082SKonstantin Belousov if (error != 0) 1014126d6082SKonstantin Belousov res = FALSE; 1015*89f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1016950f8459SAlan Cox } 1017950f8459SAlan Cox if ((object->type == OBJT_VNODE || 1018950f8459SAlan Cox object->type == OBJT_DEVICE) && invalidate) { 10196bbee8e2SAlan Cox if (object->type == OBJT_DEVICE) 10206bbee8e2SAlan Cox /* 10216bbee8e2SAlan Cox * The option OBJPR_NOTMAPPED must be passed here 10226bbee8e2SAlan Cox * because vm_object_page_remove() cannot remove 10236bbee8e2SAlan Cox * unmanaged mappings. 10246bbee8e2SAlan Cox */ 10256bbee8e2SAlan Cox flags = OBJPR_NOTMAPPED; 10266bbee8e2SAlan Cox else if (old_msync) 10276bbee8e2SAlan Cox flags = 0; 10286bbee8e2SAlan Cox else 10296bbee8e2SAlan Cox flags = OBJPR_CLEANONLY; 10306bbee8e2SAlan Cox vm_object_page_remove(object, OFF_TO_IDX(offset), 10316bbee8e2SAlan Cox OFF_TO_IDX(offset + size + PAGE_MASK), flags); 1032950f8459SAlan Cox } 1033*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1034126d6082SKonstantin Belousov return (res); 1035950f8459SAlan Cox } 1036950f8459SAlan Cox 1037950f8459SAlan Cox /* 1038867a482dSJohn Dyson * vm_object_madvise: 1039867a482dSJohn Dyson * 1040867a482dSJohn Dyson * Implements the madvise function at the object/page level. 10411c7c3c6aSMatthew Dillon * 1042193b9358SAlan Cox * MADV_WILLNEED (any object) 1043193b9358SAlan Cox * 1044193b9358SAlan Cox * Activate the specified pages if they are resident. 1045193b9358SAlan Cox * 1046193b9358SAlan Cox * MADV_DONTNEED (any object) 1047193b9358SAlan Cox * 1048193b9358SAlan Cox * Deactivate the specified pages if they are resident. 1049193b9358SAlan Cox * 1050193b9358SAlan Cox * MADV_FREE (OBJT_DEFAULT/OBJT_SWAP objects, 1051193b9358SAlan Cox * OBJ_ONEMAPPING only) 1052193b9358SAlan Cox * 1053193b9358SAlan Cox * Deactivate and clean the specified pages if they are 1054193b9358SAlan Cox * resident. This permits the process to reuse the pages 1055193b9358SAlan Cox * without faulting or the kernel to reclaim the pages 1056193b9358SAlan Cox * without I/O. 1057867a482dSJohn Dyson */ 1058867a482dSJohn Dyson void 105992a59946SJohn Baldwin vm_object_madvise(vm_object_t object, vm_pindex_t pindex, vm_pindex_t end, 106092a59946SJohn Baldwin int advise) 1061867a482dSJohn Dyson { 106292a59946SJohn Baldwin vm_pindex_t tpindex; 106334567de7SAlan Cox vm_object_t backing_object, tobject; 1064867a482dSJohn Dyson vm_page_t m; 1065867a482dSJohn Dyson 1066867a482dSJohn Dyson if (object == NULL) 1067867a482dSJohn Dyson return; 1068*89f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 10691c7c3c6aSMatthew Dillon /* 10701c7c3c6aSMatthew Dillon * Locate and adjust resident pages 10711c7c3c6aSMatthew Dillon */ 10721c7c3c6aSMatthew Dillon for (; pindex < end; pindex += 1) { 10736e20a165SJohn Dyson relookup: 10746e20a165SJohn Dyson tobject = object; 10756e20a165SJohn Dyson tpindex = pindex; 10766e20a165SJohn Dyson shadowlookup: 107758b4e6ccSAlan Cox /* 107858b4e6ccSAlan Cox * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages 107958b4e6ccSAlan Cox * and those pages must be OBJ_ONEMAPPING. 108058b4e6ccSAlan Cox */ 108158b4e6ccSAlan Cox if (advise == MADV_FREE) { 108258b4e6ccSAlan Cox if ((tobject->type != OBJT_DEFAULT && 108358b4e6ccSAlan Cox tobject->type != OBJT_SWAP) || 108458b4e6ccSAlan Cox (tobject->flags & OBJ_ONEMAPPING) == 0) { 108534567de7SAlan Cox goto unlock_tobject; 10866e20a165SJohn Dyson } 108728634820SAlan Cox } else if ((tobject->flags & OBJ_UNMANAGED) != 0) 10886a2a3d73SAlan Cox goto unlock_tobject; 10891c7c3c6aSMatthew Dillon m = vm_page_lookup(tobject, tpindex); 10907bfda801SAlan Cox if (m == NULL && advise == MADV_WILLNEED) { 10917bfda801SAlan Cox /* 10927bfda801SAlan Cox * If the page is cached, reactivate it. 10937bfda801SAlan Cox */ 1094f3a2ed4bSAlan Cox m = vm_page_alloc(tobject, tpindex, VM_ALLOC_IFCACHED | 1095f3a2ed4bSAlan Cox VM_ALLOC_NOBUSY); 10967bfda801SAlan Cox } 10971c7c3c6aSMatthew Dillon if (m == NULL) { 10981ce137beSMatthew Dillon /* 10991ce137beSMatthew Dillon * There may be swap even if there is no backing page 11001ce137beSMatthew Dillon */ 11011ce137beSMatthew Dillon if (advise == MADV_FREE && tobject->type == OBJT_SWAP) 11021ce137beSMatthew Dillon swap_pager_freespace(tobject, tpindex, 1); 11031ce137beSMatthew Dillon /* 11041ce137beSMatthew Dillon * next object 11051ce137beSMatthew Dillon */ 110634567de7SAlan Cox backing_object = tobject->backing_object; 110734567de7SAlan Cox if (backing_object == NULL) 110834567de7SAlan Cox goto unlock_tobject; 1109*89f6b863SAttilio Rao VM_OBJECT_WLOCK(backing_object); 111056e0670fSAlan Cox tpindex += OFF_TO_IDX(tobject->backing_object_offset); 11119b98b796SAlan Cox if (tobject != object) 1112*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(tobject); 111334567de7SAlan Cox tobject = backing_object; 11146e20a165SJohn Dyson goto shadowlookup; 11156a2a3d73SAlan Cox } else if (m->valid != VM_PAGE_BITS_ALL) 11166a2a3d73SAlan Cox goto unlock_tobject; 1117867a482dSJohn Dyson /* 11186a2a3d73SAlan Cox * If the page is not in a normal state, skip it. 1119867a482dSJohn Dyson */ 11202965a453SKip Macy vm_page_lock(m); 11216a2a3d73SAlan Cox if (m->hold_count != 0 || m->wire_count != 0) { 11222965a453SKip Macy vm_page_unlock(m); 112334567de7SAlan Cox goto unlock_tobject; 11246e20a165SJohn Dyson } 1125d98d0ce2SKonstantin Belousov KASSERT((m->flags & PG_FICTITIOUS) == 0, 1126d98d0ce2SKonstantin Belousov ("vm_object_madvise: page %p is fictitious", m)); 1127d98d0ce2SKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) == 0, 1128567e51e1SAlan Cox ("vm_object_madvise: page %p is not managed", m)); 11299af80719SAlan Cox if ((m->oflags & VPO_BUSY) || m->busy) { 1130567e51e1SAlan Cox if (advise == MADV_WILLNEED) { 1131b11b56b5SAlan Cox /* 1132b11b56b5SAlan Cox * Reference the page before unlocking and 1133b11b56b5SAlan Cox * sleeping so that the page daemon is less 1134b11b56b5SAlan Cox * likely to reclaim it. 1135b11b56b5SAlan Cox */ 11363407fefeSKonstantin Belousov vm_page_aflag_set(m, PGA_REFERENCED); 1137567e51e1SAlan Cox } 11382965a453SKip Macy vm_page_unlock(m); 11399b98b796SAlan Cox if (object != tobject) 1140*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 11415786be7cSAlan Cox m->oflags |= VPO_WANTED; 11420dde287bSAttilio Rao VM_OBJECT_SLEEP(tobject, m, PDROP | PVM, "madvpo", 0); 1143*89f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 11446e20a165SJohn Dyson goto relookup; 114534567de7SAlan Cox } 1146867a482dSJohn Dyson if (advise == MADV_WILLNEED) { 1147867a482dSJohn Dyson vm_page_activate(m); 11486e20a165SJohn Dyson } else if (advise == MADV_DONTNEED) { 1149479112dfSMatthew Dillon vm_page_dontneed(m); 11500a47b48bSJohn Dyson } else if (advise == MADV_FREE) { 11511c7c3c6aSMatthew Dillon /* 11522aaeadf8SMatthew Dillon * Mark the page clean. This will allow the page 11532aaeadf8SMatthew Dillon * to be freed up by the system. However, such pages 11542aaeadf8SMatthew Dillon * are often reused quickly by malloc()/free() 11552aaeadf8SMatthew Dillon * so we do not do anything that would cause 11562aaeadf8SMatthew Dillon * a page fault if we can help it. 11572aaeadf8SMatthew Dillon * 11582aaeadf8SMatthew Dillon * Specifically, we do not try to actually free 11592aaeadf8SMatthew Dillon * the page now nor do we try to put it in the 11602aaeadf8SMatthew Dillon * cache (which would cause a page fault on reuse). 116141c67e12SMatthew Dillon * 116241c67e12SMatthew Dillon * But we do make the page is freeable as we 116341c67e12SMatthew Dillon * can without actually taking the step of unmapping 116441c67e12SMatthew Dillon * it. 11651c7c3c6aSMatthew Dillon */ 11660385347cSPeter Wemm pmap_clear_modify(m); 11676e20a165SJohn Dyson m->dirty = 0; 116841c67e12SMatthew Dillon m->act_count = 0; 1169479112dfSMatthew Dillon vm_page_dontneed(m); 1170867a482dSJohn Dyson } 11712965a453SKip Macy vm_page_unlock(m); 11722999e9faSAlan Cox if (advise == MADV_FREE && tobject->type == OBJT_SWAP) 11732999e9faSAlan Cox swap_pager_freespace(tobject, tpindex, 1); 117434567de7SAlan Cox unlock_tobject: 11759b98b796SAlan Cox if (tobject != object) 1176*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(tobject); 1177867a482dSJohn Dyson } 1178*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1179867a482dSJohn Dyson } 1180867a482dSJohn Dyson 1181867a482dSJohn Dyson /* 1182df8bae1dSRodney W. Grimes * vm_object_shadow: 1183df8bae1dSRodney W. Grimes * 1184df8bae1dSRodney W. Grimes * Create a new object which is backed by the 1185df8bae1dSRodney W. Grimes * specified existing object range. The source 1186df8bae1dSRodney W. Grimes * object reference is deallocated. 1187df8bae1dSRodney W. Grimes * 1188df8bae1dSRodney W. Grimes * The new object and offset into that object 1189df8bae1dSRodney W. Grimes * are returned in the source parameters. 1190df8bae1dSRodney W. Grimes */ 119126f9a767SRodney W. Grimes void 11921b40f8c0SMatthew Dillon vm_object_shadow( 11931b40f8c0SMatthew Dillon vm_object_t *object, /* IN/OUT */ 11941b40f8c0SMatthew Dillon vm_ooffset_t *offset, /* IN/OUT */ 11951b40f8c0SMatthew Dillon vm_size_t length) 1196df8bae1dSRodney W. Grimes { 1197d031cff1SMatthew Dillon vm_object_t source; 1198d031cff1SMatthew Dillon vm_object_t result; 1199df8bae1dSRodney W. Grimes 1200df8bae1dSRodney W. Grimes source = *object; 1201df8bae1dSRodney W. Grimes 1202df8bae1dSRodney W. Grimes /* 12039a2f6362SAlan Cox * Don't create the new object if the old object isn't shared. 12049a2f6362SAlan Cox */ 1205570a2f4aSAlan Cox if (source != NULL) { 1206*89f6b863SAttilio Rao VM_OBJECT_WLOCK(source); 1207570a2f4aSAlan Cox if (source->ref_count == 1 && 12089a2f6362SAlan Cox source->handle == NULL && 12099a2f6362SAlan Cox (source->type == OBJT_DEFAULT || 12109917e010SAlan Cox source->type == OBJT_SWAP)) { 1211*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(source); 12129a2f6362SAlan Cox return; 12139917e010SAlan Cox } 1214*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(source); 1215570a2f4aSAlan Cox } 12169a2f6362SAlan Cox 12179a2f6362SAlan Cox /* 1218570a2f4aSAlan Cox * Allocate a new object with the given length. 1219df8bae1dSRodney W. Grimes */ 12200cc74f14SAlan Cox result = vm_object_allocate(OBJT_DEFAULT, atop(length)); 1221df8bae1dSRodney W. Grimes 1222df8bae1dSRodney W. Grimes /* 12230d94caffSDavid Greenman * The new object shadows the source object, adding a reference to it. 12240d94caffSDavid Greenman * Our caller changes his reference to point to the new object, 12250d94caffSDavid Greenman * removing a reference to the source object. Net result: no change 12260d94caffSDavid Greenman * of reference count. 12279b09fe24SMatthew Dillon * 12289b09fe24SMatthew Dillon * Try to optimize the result object's page color when shadowing 1229956f3135SPhilippe Charnier * in order to maintain page coloring consistency in the combined 12309b09fe24SMatthew Dillon * shadowed object. 1231df8bae1dSRodney W. Grimes */ 123224a1cce3SDavid Greenman result->backing_object = source; 12339174ca7bSTor Egge /* 12349174ca7bSTor Egge * Store the offset into the source object, and fix up the offset into 12359174ca7bSTor Egge * the new object. 12369174ca7bSTor Egge */ 12379174ca7bSTor Egge result->backing_object_offset = *offset; 1238570a2f4aSAlan Cox if (source != NULL) { 1239*89f6b863SAttilio Rao VM_OBJECT_WLOCK(source); 12401c500307SAlan Cox LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list); 1241eaf13dd7SJohn Dyson source->shadow_count++; 1242f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 12437b54b1a9SAlan Cox result->flags |= source->flags & OBJ_COLORED; 1244f8a47341SAlan Cox result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) & 1245f8a47341SAlan Cox ((1 << (VM_NFREEORDER - 1)) - 1); 1246f8a47341SAlan Cox #endif 1247*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(source); 1248de5f6a77SJohn Dyson } 1249df8bae1dSRodney W. Grimes 1250df8bae1dSRodney W. Grimes 1251df8bae1dSRodney W. Grimes /* 1252df8bae1dSRodney W. Grimes * Return the new things 1253df8bae1dSRodney W. Grimes */ 1254df8bae1dSRodney W. Grimes *offset = 0; 1255df8bae1dSRodney W. Grimes *object = result; 1256df8bae1dSRodney W. Grimes } 1257df8bae1dSRodney W. Grimes 1258c5aaa06dSAlan Cox /* 1259c5aaa06dSAlan Cox * vm_object_split: 1260c5aaa06dSAlan Cox * 1261c5aaa06dSAlan Cox * Split the pages in a map entry into a new object. This affords 1262c5aaa06dSAlan Cox * easier removal of unused pages, and keeps object inheritance from 1263c5aaa06dSAlan Cox * being a negative impact on memory usage. 1264c5aaa06dSAlan Cox */ 1265c5aaa06dSAlan Cox void 1266c5aaa06dSAlan Cox vm_object_split(vm_map_entry_t entry) 1267c5aaa06dSAlan Cox { 126873000556SAlan Cox vm_page_t m, m_next; 1269c5aaa06dSAlan Cox vm_object_t orig_object, new_object, source; 127073000556SAlan Cox vm_pindex_t idx, offidxstart; 127173000556SAlan Cox vm_size_t size; 1272c5aaa06dSAlan Cox 1273c5aaa06dSAlan Cox orig_object = entry->object.vm_object; 1274c5aaa06dSAlan Cox if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP) 1275c5aaa06dSAlan Cox return; 1276c5aaa06dSAlan Cox if (orig_object->ref_count <= 1) 1277c5aaa06dSAlan Cox return; 1278*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(orig_object); 1279c5aaa06dSAlan Cox 12804da9f125SAlan Cox offidxstart = OFF_TO_IDX(entry->offset); 128195442adfSAlan Cox size = atop(entry->end - entry->start); 1282c5aaa06dSAlan Cox 12834da9f125SAlan Cox /* 12844da9f125SAlan Cox * If swap_pager_copy() is later called, it will convert new_object 12854da9f125SAlan Cox * into a swap object. 12864da9f125SAlan Cox */ 12874da9f125SAlan Cox new_object = vm_object_allocate(OBJT_DEFAULT, size); 1288c5aaa06dSAlan Cox 1289c5474b8fSAlan Cox /* 1290c5474b8fSAlan Cox * At this point, the new object is still private, so the order in 1291c5474b8fSAlan Cox * which the original and new objects are locked does not matter. 1292c5474b8fSAlan Cox */ 1293*89f6b863SAttilio Rao VM_OBJECT_WLOCK(new_object); 1294*89f6b863SAttilio Rao VM_OBJECT_WLOCK(orig_object); 1295c5aaa06dSAlan Cox source = orig_object->backing_object; 1296c5aaa06dSAlan Cox if (source != NULL) { 1297*89f6b863SAttilio Rao VM_OBJECT_WLOCK(source); 129819c244d0SAlan Cox if ((source->flags & OBJ_DEAD) != 0) { 1299*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(source); 1300*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(orig_object); 1301*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(new_object); 130219c244d0SAlan Cox vm_object_deallocate(new_object); 1303*89f6b863SAttilio Rao VM_OBJECT_WLOCK(orig_object); 130419c244d0SAlan Cox return; 130519c244d0SAlan Cox } 13061c500307SAlan Cox LIST_INSERT_HEAD(&source->shadow_head, 1307c5aaa06dSAlan Cox new_object, shadow_list); 13088e3a76fbSAlan Cox source->shadow_count++; 1309b921a12bSAlan Cox vm_object_reference_locked(source); /* for new_object */ 1310c5aaa06dSAlan Cox vm_object_clear_flag(source, OBJ_ONEMAPPING); 1311*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(source); 1312c5aaa06dSAlan Cox new_object->backing_object_offset = 13134da9f125SAlan Cox orig_object->backing_object_offset + entry->offset; 1314c5aaa06dSAlan Cox new_object->backing_object = source; 1315c5aaa06dSAlan Cox } 1316ef694c1aSEdward Tomasz Napierala if (orig_object->cred != NULL) { 1317ef694c1aSEdward Tomasz Napierala new_object->cred = orig_object->cred; 1318ef694c1aSEdward Tomasz Napierala crhold(orig_object->cred); 13193364c323SKonstantin Belousov new_object->charge = ptoa(size); 13203364c323SKonstantin Belousov KASSERT(orig_object->charge >= ptoa(size), 13213364c323SKonstantin Belousov ("orig_object->charge < 0")); 13223364c323SKonstantin Belousov orig_object->charge -= ptoa(size); 13233364c323SKonstantin Belousov } 1324c5aaa06dSAlan Cox retry: 1325b382c10aSKonstantin Belousov m = vm_page_find_least(orig_object, offidxstart); 132673000556SAlan Cox for (; m != NULL && (idx = m->pindex - offidxstart) < size; 132773000556SAlan Cox m = m_next) { 132873000556SAlan Cox m_next = TAILQ_NEXT(m, listq); 1329c5aaa06dSAlan Cox 1330c5aaa06dSAlan Cox /* 1331c5aaa06dSAlan Cox * We must wait for pending I/O to complete before we can 1332c5aaa06dSAlan Cox * rename the page. 1333c5aaa06dSAlan Cox * 1334c5aaa06dSAlan Cox * We do not have to VM_PROT_NONE the page as mappings should 1335c5aaa06dSAlan Cox * not be changed by this operation. 1336c5aaa06dSAlan Cox */ 13379af80719SAlan Cox if ((m->oflags & VPO_BUSY) || m->busy) { 1338*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(new_object); 13395786be7cSAlan Cox m->oflags |= VPO_WANTED; 13400dde287bSAttilio Rao VM_OBJECT_SLEEP(orig_object, m, PVM, "spltwt", 0); 1341*89f6b863SAttilio Rao VM_OBJECT_WLOCK(new_object); 1342c5aaa06dSAlan Cox goto retry; 1343de33beddSAlan Cox } 1344b5f359b7SAlan Cox #if VM_NRESERVLEVEL > 0 1345b5f359b7SAlan Cox /* 1346b5f359b7SAlan Cox * If some of the reservation's allocated pages remain with 1347b5f359b7SAlan Cox * the original object, then transferring the reservation to 1348b5f359b7SAlan Cox * the new object is neither particularly beneficial nor 1349b5f359b7SAlan Cox * particularly harmful as compared to leaving the reservation 1350b5f359b7SAlan Cox * with the original object. If, however, all of the 1351b5f359b7SAlan Cox * reservation's allocated pages are transferred to the new 1352b5f359b7SAlan Cox * object, then transferring the reservation is typically 1353b5f359b7SAlan Cox * beneficial. Determining which of these two cases applies 1354b5f359b7SAlan Cox * would be more costly than unconditionally renaming the 1355b5f359b7SAlan Cox * reservation. 1356b5f359b7SAlan Cox */ 1357b5f359b7SAlan Cox vm_reserv_rename(m, new_object, orig_object, offidxstart); 1358b5f359b7SAlan Cox #endif 13592965a453SKip Macy vm_page_lock(m); 1360c5aaa06dSAlan Cox vm_page_rename(m, new_object, idx); 13612965a453SKip Macy vm_page_unlock(m); 1362c5aaa06dSAlan Cox /* page automatically made dirty by rename and cache handled */ 1363c5aaa06dSAlan Cox vm_page_busy(m); 1364c5aaa06dSAlan Cox } 1365d7a013c3SAlan Cox if (orig_object->type == OBJT_SWAP) { 1366c5aaa06dSAlan Cox /* 1367c7c8dd7eSAlan Cox * swap_pager_copy() can sleep, in which case the orig_object's 1368c7c8dd7eSAlan Cox * and new_object's locks are released and reacquired. 1369c5aaa06dSAlan Cox */ 1370c5aaa06dSAlan Cox swap_pager_copy(orig_object, new_object, offidxstart, 0); 13717bfda801SAlan Cox 13727bfda801SAlan Cox /* 13737bfda801SAlan Cox * Transfer any cached pages from orig_object to new_object. 1374571a1e92SAttilio Rao * If swap_pager_copy() found swapped out pages within the 1375571a1e92SAttilio Rao * specified range of orig_object, then it changed 1376571a1e92SAttilio Rao * new_object's type to OBJT_SWAP when it transferred those 1377571a1e92SAttilio Rao * pages to new_object. Otherwise, new_object's type 1378571a1e92SAttilio Rao * should still be OBJT_DEFAULT and orig_object should not 1379571a1e92SAttilio Rao * contain any cached pages within the specified range. 13807bfda801SAlan Cox */ 1381c9341161SAttilio Rao if (__predict_false(!vm_object_cache_is_empty(orig_object))) 13827bfda801SAlan Cox vm_page_cache_transfer(orig_object, offidxstart, 13837bfda801SAlan Cox new_object); 1384c5aaa06dSAlan Cox } 1385*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(orig_object); 1386c7118ed6SAlan Cox TAILQ_FOREACH(m, &new_object->memq, listq) 1387c5aaa06dSAlan Cox vm_page_wakeup(m); 1388*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(new_object); 1389c5aaa06dSAlan Cox entry->object.vm_object = new_object; 1390c5aaa06dSAlan Cox entry->offset = 0LL; 1391c5aaa06dSAlan Cox vm_object_deallocate(orig_object); 1392*89f6b863SAttilio Rao VM_OBJECT_WLOCK(new_object); 1393c5aaa06dSAlan Cox } 1394c5aaa06dSAlan Cox 13952ad1a3f7SMatthew Dillon #define OBSC_TEST_ALL_SHADOWED 0x0001 13962ad1a3f7SMatthew Dillon #define OBSC_COLLAPSE_NOWAIT 0x0002 13972ad1a3f7SMatthew Dillon #define OBSC_COLLAPSE_WAIT 0x0004 13982ad1a3f7SMatthew Dillon 1399b4ae4780SPoul-Henning Kamp static int 14002ad1a3f7SMatthew Dillon vm_object_backing_scan(vm_object_t object, int op) 14012ad1a3f7SMatthew Dillon { 14022ad1a3f7SMatthew Dillon int r = 1; 14032ad1a3f7SMatthew Dillon vm_page_t p; 14042ad1a3f7SMatthew Dillon vm_object_t backing_object; 14052ad1a3f7SMatthew Dillon vm_pindex_t backing_offset_index; 14062ad1a3f7SMatthew Dillon 1407*89f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 1408*89f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object->backing_object); 14092ad1a3f7SMatthew Dillon 14102ad1a3f7SMatthew Dillon backing_object = object->backing_object; 14112ad1a3f7SMatthew Dillon backing_offset_index = OFF_TO_IDX(object->backing_object_offset); 14122ad1a3f7SMatthew Dillon 14132ad1a3f7SMatthew Dillon /* 14142ad1a3f7SMatthew Dillon * Initial conditions 14152ad1a3f7SMatthew Dillon */ 14162ad1a3f7SMatthew Dillon if (op & OBSC_TEST_ALL_SHADOWED) { 14172ad1a3f7SMatthew Dillon /* 14187bfda801SAlan Cox * We do not want to have to test for the existence of cache 14197bfda801SAlan Cox * or swap pages in the backing object. XXX but with the 14202ad1a3f7SMatthew Dillon * new swapper this would be pretty easy to do. 14212ad1a3f7SMatthew Dillon * 14222ad1a3f7SMatthew Dillon * XXX what about anonymous MAP_SHARED memory that hasn't 14232ad1a3f7SMatthew Dillon * been ZFOD faulted yet? If we do not test for this, the 14242ad1a3f7SMatthew Dillon * shadow test may succeed! XXX 14252ad1a3f7SMatthew Dillon */ 14262ad1a3f7SMatthew Dillon if (backing_object->type != OBJT_DEFAULT) { 14272ad1a3f7SMatthew Dillon return (0); 14282ad1a3f7SMatthew Dillon } 14292ad1a3f7SMatthew Dillon } 14302ad1a3f7SMatthew Dillon if (op & OBSC_COLLAPSE_WAIT) { 14312ad1a3f7SMatthew Dillon vm_object_set_flag(backing_object, OBJ_DEAD); 14322ad1a3f7SMatthew Dillon } 14332ad1a3f7SMatthew Dillon 14342ad1a3f7SMatthew Dillon /* 14352ad1a3f7SMatthew Dillon * Our scan 14362ad1a3f7SMatthew Dillon */ 14372ad1a3f7SMatthew Dillon p = TAILQ_FIRST(&backing_object->memq); 14382ad1a3f7SMatthew Dillon while (p) { 14392ad1a3f7SMatthew Dillon vm_page_t next = TAILQ_NEXT(p, listq); 14402ad1a3f7SMatthew Dillon vm_pindex_t new_pindex = p->pindex - backing_offset_index; 14412ad1a3f7SMatthew Dillon 14422ad1a3f7SMatthew Dillon if (op & OBSC_TEST_ALL_SHADOWED) { 14432ad1a3f7SMatthew Dillon vm_page_t pp; 14442ad1a3f7SMatthew Dillon 14452ad1a3f7SMatthew Dillon /* 14462ad1a3f7SMatthew Dillon * Ignore pages outside the parent object's range 14472ad1a3f7SMatthew Dillon * and outside the parent object's mapping of the 14482ad1a3f7SMatthew Dillon * backing object. 14492ad1a3f7SMatthew Dillon * 14502ad1a3f7SMatthew Dillon * note that we do not busy the backing object's 14512ad1a3f7SMatthew Dillon * page. 14522ad1a3f7SMatthew Dillon */ 14532ad1a3f7SMatthew Dillon if ( 14542ad1a3f7SMatthew Dillon p->pindex < backing_offset_index || 14552ad1a3f7SMatthew Dillon new_pindex >= object->size 14562ad1a3f7SMatthew Dillon ) { 14572ad1a3f7SMatthew Dillon p = next; 14582ad1a3f7SMatthew Dillon continue; 14592ad1a3f7SMatthew Dillon } 14602ad1a3f7SMatthew Dillon 14612ad1a3f7SMatthew Dillon /* 14622ad1a3f7SMatthew Dillon * See if the parent has the page or if the parent's 14632ad1a3f7SMatthew Dillon * object pager has the page. If the parent has the 14642ad1a3f7SMatthew Dillon * page but the page is not valid, the parent's 14652ad1a3f7SMatthew Dillon * object pager must have the page. 14662ad1a3f7SMatthew Dillon * 14672ad1a3f7SMatthew Dillon * If this fails, the parent does not completely shadow 14682ad1a3f7SMatthew Dillon * the object and we might as well give up now. 14692ad1a3f7SMatthew Dillon */ 14702ad1a3f7SMatthew Dillon 14712ad1a3f7SMatthew Dillon pp = vm_page_lookup(object, new_pindex); 14722ad1a3f7SMatthew Dillon if ( 14732ad1a3f7SMatthew Dillon (pp == NULL || pp->valid == 0) && 14742ad1a3f7SMatthew Dillon !vm_pager_has_page(object, new_pindex, NULL, NULL) 14752ad1a3f7SMatthew Dillon ) { 14762ad1a3f7SMatthew Dillon r = 0; 14772ad1a3f7SMatthew Dillon break; 14782ad1a3f7SMatthew Dillon } 14792ad1a3f7SMatthew Dillon } 14802ad1a3f7SMatthew Dillon 14812ad1a3f7SMatthew Dillon /* 14822ad1a3f7SMatthew Dillon * Check for busy page 14832ad1a3f7SMatthew Dillon */ 14842ad1a3f7SMatthew Dillon if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) { 14852ad1a3f7SMatthew Dillon vm_page_t pp; 14862ad1a3f7SMatthew Dillon 14872ad1a3f7SMatthew Dillon if (op & OBSC_COLLAPSE_NOWAIT) { 14889af80719SAlan Cox if ((p->oflags & VPO_BUSY) || 14892ad1a3f7SMatthew Dillon !p->valid || 149000f9e8b4SAlan Cox p->busy) { 14912ad1a3f7SMatthew Dillon p = next; 14922ad1a3f7SMatthew Dillon continue; 14932ad1a3f7SMatthew Dillon } 14942ad1a3f7SMatthew Dillon } else if (op & OBSC_COLLAPSE_WAIT) { 14959af80719SAlan Cox if ((p->oflags & VPO_BUSY) || p->busy) { 1496*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 14975786be7cSAlan Cox p->oflags |= VPO_WANTED; 14980dde287bSAttilio Rao VM_OBJECT_SLEEP(backing_object, p, 14997ca33ad1SAlan Cox PDROP | PVM, "vmocol", 0); 1500*89f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1501*89f6b863SAttilio Rao VM_OBJECT_WLOCK(backing_object); 15022ad1a3f7SMatthew Dillon /* 15032ad1a3f7SMatthew Dillon * If we slept, anything could have 15042ad1a3f7SMatthew Dillon * happened. Since the object is 15052ad1a3f7SMatthew Dillon * marked dead, the backing offset 15062ad1a3f7SMatthew Dillon * should not have changed so we 15072ad1a3f7SMatthew Dillon * just restart our scan. 15082ad1a3f7SMatthew Dillon */ 15092ad1a3f7SMatthew Dillon p = TAILQ_FIRST(&backing_object->memq); 15102ad1a3f7SMatthew Dillon continue; 15112ad1a3f7SMatthew Dillon } 15122ad1a3f7SMatthew Dillon } 15132ad1a3f7SMatthew Dillon 15142ad1a3f7SMatthew Dillon KASSERT( 15152ad1a3f7SMatthew Dillon p->object == backing_object, 15168e99783bSAlan Cox ("vm_object_backing_scan: object mismatch") 15172ad1a3f7SMatthew Dillon ); 15182ad1a3f7SMatthew Dillon 15192ad1a3f7SMatthew Dillon /* 15202ad1a3f7SMatthew Dillon * Destroy any associated swap 15212ad1a3f7SMatthew Dillon */ 15222ad1a3f7SMatthew Dillon if (backing_object->type == OBJT_SWAP) { 15232ad1a3f7SMatthew Dillon swap_pager_freespace( 15242ad1a3f7SMatthew Dillon backing_object, 15252ad1a3f7SMatthew Dillon p->pindex, 15262ad1a3f7SMatthew Dillon 1 15272ad1a3f7SMatthew Dillon ); 15282ad1a3f7SMatthew Dillon } 15292ad1a3f7SMatthew Dillon 15302ad1a3f7SMatthew Dillon if ( 15312ad1a3f7SMatthew Dillon p->pindex < backing_offset_index || 15322ad1a3f7SMatthew Dillon new_pindex >= object->size 15332ad1a3f7SMatthew Dillon ) { 15342ad1a3f7SMatthew Dillon /* 15352ad1a3f7SMatthew Dillon * Page is out of the parent object's range, we 15362ad1a3f7SMatthew Dillon * can simply destroy it. 15372ad1a3f7SMatthew Dillon */ 15382965a453SKip Macy vm_page_lock(p); 1539f6d89838SAlan Cox KASSERT(!pmap_page_is_mapped(p), 1540f6d89838SAlan Cox ("freeing mapped page %p", p)); 1541f6d89838SAlan Cox if (p->wire_count == 0) 15422ad1a3f7SMatthew Dillon vm_page_free(p); 1543f6d89838SAlan Cox else 1544f6d89838SAlan Cox vm_page_remove(p); 15452965a453SKip Macy vm_page_unlock(p); 15462ad1a3f7SMatthew Dillon p = next; 15472ad1a3f7SMatthew Dillon continue; 15482ad1a3f7SMatthew Dillon } 15492ad1a3f7SMatthew Dillon 15502ad1a3f7SMatthew Dillon pp = vm_page_lookup(object, new_pindex); 15512ad1a3f7SMatthew Dillon if ( 1552e18cc7bfSMax Laier (op & OBSC_COLLAPSE_NOWAIT) != 0 && 1553e18cc7bfSMax Laier (pp != NULL && pp->valid == 0) 1554e18cc7bfSMax Laier ) { 1555e18cc7bfSMax Laier /* 1556e18cc7bfSMax Laier * The page in the parent is not (yet) valid. 1557e18cc7bfSMax Laier * We don't know anything about the state of 1558e18cc7bfSMax Laier * the original page. It might be mapped, 1559e18cc7bfSMax Laier * so we must avoid the next if here. 1560e18cc7bfSMax Laier * 1561e18cc7bfSMax Laier * This is due to a race in vm_fault() where 1562e18cc7bfSMax Laier * we must unbusy the original (backing_obj) 1563e18cc7bfSMax Laier * page before we can (re)lock the parent. 1564e18cc7bfSMax Laier * Hence we can get here. 1565e18cc7bfSMax Laier */ 1566e18cc7bfSMax Laier p = next; 1567e18cc7bfSMax Laier continue; 1568e18cc7bfSMax Laier } 1569e18cc7bfSMax Laier if ( 15702ad1a3f7SMatthew Dillon pp != NULL || 15712ad1a3f7SMatthew Dillon vm_pager_has_page(object, new_pindex, NULL, NULL) 15722ad1a3f7SMatthew Dillon ) { 15732ad1a3f7SMatthew Dillon /* 15742ad1a3f7SMatthew Dillon * page already exists in parent OR swap exists 15752ad1a3f7SMatthew Dillon * for this location in the parent. Destroy 15762ad1a3f7SMatthew Dillon * the original page from the backing object. 15772ad1a3f7SMatthew Dillon * 15782ad1a3f7SMatthew Dillon * Leave the parent's page alone 15792ad1a3f7SMatthew Dillon */ 15802965a453SKip Macy vm_page_lock(p); 1581f6d89838SAlan Cox KASSERT(!pmap_page_is_mapped(p), 1582f6d89838SAlan Cox ("freeing mapped page %p", p)); 1583f6d89838SAlan Cox if (p->wire_count == 0) 15842ad1a3f7SMatthew Dillon vm_page_free(p); 1585f6d89838SAlan Cox else 1586f6d89838SAlan Cox vm_page_remove(p); 15872965a453SKip Macy vm_page_unlock(p); 15882ad1a3f7SMatthew Dillon p = next; 15892ad1a3f7SMatthew Dillon continue; 15902ad1a3f7SMatthew Dillon } 15912ad1a3f7SMatthew Dillon 1592f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 1593f8a47341SAlan Cox /* 1594f8a47341SAlan Cox * Rename the reservation. 1595f8a47341SAlan Cox */ 1596f8a47341SAlan Cox vm_reserv_rename(p, object, backing_object, 1597f8a47341SAlan Cox backing_offset_index); 1598f8a47341SAlan Cox #endif 1599f8a47341SAlan Cox 16002ad1a3f7SMatthew Dillon /* 16012ad1a3f7SMatthew Dillon * Page does not exist in parent, rename the 16022ad1a3f7SMatthew Dillon * page from the backing object to the main object. 1603d1bf5d56SMatthew Dillon * 1604d1bf5d56SMatthew Dillon * If the page was mapped to a process, it can remain 1605d1bf5d56SMatthew Dillon * mapped through the rename. 16062ad1a3f7SMatthew Dillon */ 16072965a453SKip Macy vm_page_lock(p); 16082ad1a3f7SMatthew Dillon vm_page_rename(p, object, new_pindex); 16092965a453SKip Macy vm_page_unlock(p); 16102ad1a3f7SMatthew Dillon /* page automatically made dirty by rename */ 16112ad1a3f7SMatthew Dillon } 16122ad1a3f7SMatthew Dillon p = next; 16132ad1a3f7SMatthew Dillon } 16142ad1a3f7SMatthew Dillon return (r); 16152ad1a3f7SMatthew Dillon } 16162ad1a3f7SMatthew Dillon 1617df8bae1dSRodney W. Grimes 1618df8bae1dSRodney W. Grimes /* 16192fe6e4d7SDavid Greenman * this version of collapse allows the operation to occur earlier and 16202fe6e4d7SDavid Greenman * when paging_in_progress is true for an object... This is not a complete 16212fe6e4d7SDavid Greenman * operation, but should plug 99.9% of the rest of the leaks. 16222fe6e4d7SDavid Greenman */ 16232fe6e4d7SDavid Greenman static void 16241b40f8c0SMatthew Dillon vm_object_qcollapse(vm_object_t object) 16252fe6e4d7SDavid Greenman { 16262ad1a3f7SMatthew Dillon vm_object_t backing_object = object->backing_object; 16272fe6e4d7SDavid Greenman 1628*89f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 1629*89f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(backing_object); 16301b40f8c0SMatthew Dillon 16312fe6e4d7SDavid Greenman if (backing_object->ref_count != 1) 16322fe6e4d7SDavid Greenman return; 16332fe6e4d7SDavid Greenman 16342ad1a3f7SMatthew Dillon vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT); 16352fe6e4d7SDavid Greenman } 16362fe6e4d7SDavid Greenman 1637df8bae1dSRodney W. Grimes /* 1638df8bae1dSRodney W. Grimes * vm_object_collapse: 1639df8bae1dSRodney W. Grimes * 1640df8bae1dSRodney W. Grimes * Collapse an object with the object backing it. 1641df8bae1dSRodney W. Grimes * Pages in the backing object are moved into the 1642df8bae1dSRodney W. Grimes * parent, and the backing object is deallocated. 1643df8bae1dSRodney W. Grimes */ 164426f9a767SRodney W. Grimes void 16451b40f8c0SMatthew Dillon vm_object_collapse(vm_object_t object) 1646df8bae1dSRodney W. Grimes { 1647*89f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 164823955314SAlfred Perlstein 1649df8bae1dSRodney W. Grimes while (TRUE) { 16502ad1a3f7SMatthew Dillon vm_object_t backing_object; 16512ad1a3f7SMatthew Dillon 1652df8bae1dSRodney W. Grimes /* 1653df8bae1dSRodney W. Grimes * Verify that the conditions are right for collapse: 1654df8bae1dSRodney W. Grimes * 16552ad1a3f7SMatthew Dillon * The object exists and the backing object exists. 1656df8bae1dSRodney W. Grimes */ 165724a1cce3SDavid Greenman if ((backing_object = object->backing_object) == NULL) 16582ad1a3f7SMatthew Dillon break; 1659df8bae1dSRodney W. Grimes 1660f919ebdeSDavid Greenman /* 1661f919ebdeSDavid Greenman * we check the backing object first, because it is most likely 166224a1cce3SDavid Greenman * not collapsable. 1663f919ebdeSDavid Greenman */ 1664*89f6b863SAttilio Rao VM_OBJECT_WLOCK(backing_object); 166524a1cce3SDavid Greenman if (backing_object->handle != NULL || 166624a1cce3SDavid Greenman (backing_object->type != OBJT_DEFAULT && 166724a1cce3SDavid Greenman backing_object->type != OBJT_SWAP) || 1668f919ebdeSDavid Greenman (backing_object->flags & OBJ_DEAD) || 166924a1cce3SDavid Greenman object->handle != NULL || 167024a1cce3SDavid Greenman (object->type != OBJT_DEFAULT && 167124a1cce3SDavid Greenman object->type != OBJT_SWAP) || 167224a1cce3SDavid Greenman (object->flags & OBJ_DEAD)) { 1673*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 16742ad1a3f7SMatthew Dillon break; 167524a1cce3SDavid Greenman } 16769b4814bbSDavid Greenman 16772ad1a3f7SMatthew Dillon if ( 16782ad1a3f7SMatthew Dillon object->paging_in_progress != 0 || 16792ad1a3f7SMatthew Dillon backing_object->paging_in_progress != 0 16802ad1a3f7SMatthew Dillon ) { 1681b9921222SDavid Greenman vm_object_qcollapse(object); 1682*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 16832ad1a3f7SMatthew Dillon break; 1684df8bae1dSRodney W. Grimes } 168526f9a767SRodney W. Grimes /* 16860d94caffSDavid Greenman * We know that we can either collapse the backing object (if 16872ad1a3f7SMatthew Dillon * the parent is the only reference to it) or (perhaps) have 16882ad1a3f7SMatthew Dillon * the parent bypass the object if the parent happens to shadow 16892ad1a3f7SMatthew Dillon * all the resident pages in the entire backing object. 16902ad1a3f7SMatthew Dillon * 16912ad1a3f7SMatthew Dillon * This is ignoring pager-backed pages such as swap pages. 16922ad1a3f7SMatthew Dillon * vm_object_backing_scan fails the shadowing test in this 16932ad1a3f7SMatthew Dillon * case. 1694df8bae1dSRodney W. Grimes */ 1695df8bae1dSRodney W. Grimes if (backing_object->ref_count == 1) { 1696df8bae1dSRodney W. Grimes /* 16972ad1a3f7SMatthew Dillon * If there is exactly one reference to the backing 16982ad1a3f7SMatthew Dillon * object, we can collapse it into the parent. 1699df8bae1dSRodney W. Grimes */ 17002ad1a3f7SMatthew Dillon vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT); 1701df8bae1dSRodney W. Grimes 1702f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 1703f8a47341SAlan Cox /* 1704f8a47341SAlan Cox * Break any reservations from backing_object. 1705f8a47341SAlan Cox */ 1706f8a47341SAlan Cox if (__predict_false(!LIST_EMPTY(&backing_object->rvq))) 1707f8a47341SAlan Cox vm_reserv_break_all(backing_object); 1708f8a47341SAlan Cox #endif 1709f8a47341SAlan Cox 1710df8bae1dSRodney W. Grimes /* 1711df8bae1dSRodney W. Grimes * Move the pager from backing_object to object. 1712df8bae1dSRodney W. Grimes */ 17136be36525SAlan Cox if (backing_object->type == OBJT_SWAP) { 171424a1cce3SDavid Greenman /* 1715c7c8dd7eSAlan Cox * swap_pager_copy() can sleep, in which case 1716c7c8dd7eSAlan Cox * the backing_object's and object's locks are 1717c7c8dd7eSAlan Cox * released and reacquired. 1718571a1e92SAttilio Rao * Since swap_pager_copy() is being asked to 1719571a1e92SAttilio Rao * destroy the source, it will change the 1720571a1e92SAttilio Rao * backing_object's type to OBJT_DEFAULT. 172124a1cce3SDavid Greenman */ 17221c7c3c6aSMatthew Dillon swap_pager_copy( 17231c7c3c6aSMatthew Dillon backing_object, 17241c7c3c6aSMatthew Dillon object, 17251c7c3c6aSMatthew Dillon OFF_TO_IDX(object->backing_object_offset), TRUE); 17267bfda801SAlan Cox 17277bfda801SAlan Cox /* 17287bfda801SAlan Cox * Free any cached pages from backing_object. 17297bfda801SAlan Cox */ 1730c9341161SAttilio Rao if (__predict_false( 1731c9341161SAttilio Rao !vm_object_cache_is_empty(backing_object))) 1732c9444914SAlan Cox vm_page_cache_free(backing_object, 0, 0); 1733c0503609SDavid Greenman } 1734df8bae1dSRodney W. Grimes /* 1735df8bae1dSRodney W. Grimes * Object now shadows whatever backing_object did. 17362ad1a3f7SMatthew Dillon * Note that the reference to 17372ad1a3f7SMatthew Dillon * backing_object->backing_object moves from within 17382ad1a3f7SMatthew Dillon * backing_object to within object. 1739df8bae1dSRodney W. Grimes */ 17401c500307SAlan Cox LIST_REMOVE(object, shadow_list); 17414f7c7f6eSAlan Cox backing_object->shadow_count--; 1742de5f6a77SJohn Dyson if (backing_object->backing_object) { 1743*89f6b863SAttilio Rao VM_OBJECT_WLOCK(backing_object->backing_object); 17441c500307SAlan Cox LIST_REMOVE(backing_object, shadow_list); 174543186e53SAlan Cox LIST_INSERT_HEAD( 174643186e53SAlan Cox &backing_object->backing_object->shadow_head, 174743186e53SAlan Cox object, shadow_list); 174843186e53SAlan Cox /* 174943186e53SAlan Cox * The shadow_count has not changed. 175043186e53SAlan Cox */ 1751*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object->backing_object); 1752de5f6a77SJohn Dyson } 175324a1cce3SDavid Greenman object->backing_object = backing_object->backing_object; 17542ad1a3f7SMatthew Dillon object->backing_object_offset += 17552ad1a3f7SMatthew Dillon backing_object->backing_object_offset; 17562ad1a3f7SMatthew Dillon 1757df8bae1dSRodney W. Grimes /* 1758df8bae1dSRodney W. Grimes * Discard backing_object. 1759df8bae1dSRodney W. Grimes * 17600d94caffSDavid Greenman * Since the backing object has no pages, no pager left, 17610d94caffSDavid Greenman * and no object references within it, all that is 17620d94caffSDavid Greenman * necessary is to dispose of it. 1763df8bae1dSRodney W. Grimes */ 17649b4d473aSKonstantin Belousov KASSERT(backing_object->ref_count == 1, ( 17659b4d473aSKonstantin Belousov "backing_object %p was somehow re-referenced during collapse!", 17669b4d473aSKonstantin Belousov backing_object)); 1767*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 17689b4d473aSKonstantin Belousov vm_object_destroy(backing_object); 1769df8bae1dSRodney W. Grimes 1770df8bae1dSRodney W. Grimes object_collapses++; 17710d94caffSDavid Greenman } else { 177295e5e988SJohn Dyson vm_object_t new_backing_object; 1773df8bae1dSRodney W. Grimes 1774df8bae1dSRodney W. Grimes /* 17752ad1a3f7SMatthew Dillon * If we do not entirely shadow the backing object, 17762ad1a3f7SMatthew Dillon * there is nothing we can do so we give up. 1777df8bae1dSRodney W. Grimes */ 1778df59a0feSJeff Roberson if (object->resident_page_count != object->size && 1779df59a0feSJeff Roberson vm_object_backing_scan(object, 1780df59a0feSJeff Roberson OBSC_TEST_ALL_SHADOWED) == 0) { 1781*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 17822ad1a3f7SMatthew Dillon break; 178324a1cce3SDavid Greenman } 1784df8bae1dSRodney W. Grimes 1785df8bae1dSRodney W. Grimes /* 17860d94caffSDavid Greenman * Make the parent shadow the next object in the 17870d94caffSDavid Greenman * chain. Deallocating backing_object will not remove 17880d94caffSDavid Greenman * it, since its reference count is at least 2. 1789df8bae1dSRodney W. Grimes */ 17901c500307SAlan Cox LIST_REMOVE(object, shadow_list); 1791eaf13dd7SJohn Dyson backing_object->shadow_count--; 179295e5e988SJohn Dyson 179395e5e988SJohn Dyson new_backing_object = backing_object->backing_object; 17948aef1712SMatthew Dillon if ((object->backing_object = new_backing_object) != NULL) { 1795*89f6b863SAttilio Rao VM_OBJECT_WLOCK(new_backing_object); 17961c500307SAlan Cox LIST_INSERT_HEAD( 17972ad1a3f7SMatthew Dillon &new_backing_object->shadow_head, 17982ad1a3f7SMatthew Dillon object, 17992ad1a3f7SMatthew Dillon shadow_list 18002ad1a3f7SMatthew Dillon ); 1801eaf13dd7SJohn Dyson new_backing_object->shadow_count++; 1802b921a12bSAlan Cox vm_object_reference_locked(new_backing_object); 1803*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(new_backing_object); 180495e5e988SJohn Dyson object->backing_object_offset += 180595e5e988SJohn Dyson backing_object->backing_object_offset; 1806de5f6a77SJohn Dyson } 1807df8bae1dSRodney W. Grimes 1808df8bae1dSRodney W. Grimes /* 18090d94caffSDavid Greenman * Drop the reference count on backing_object. Since 181022ec553fSAlan Cox * its ref_count was at least 2, it will not vanish. 1811df8bae1dSRodney W. Grimes */ 181222ec553fSAlan Cox backing_object->ref_count--; 1813*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 1814df8bae1dSRodney W. Grimes object_bypasses++; 1815df8bae1dSRodney W. Grimes } 1816df8bae1dSRodney W. Grimes 1817df8bae1dSRodney W. Grimes /* 1818df8bae1dSRodney W. Grimes * Try again with this object's new backing object. 1819df8bae1dSRodney W. Grimes */ 1820df8bae1dSRodney W. Grimes } 1821df8bae1dSRodney W. Grimes } 1822df8bae1dSRodney W. Grimes 1823df8bae1dSRodney W. Grimes /* 1824bff99f0dSAlan Cox * vm_object_page_remove: 1825df8bae1dSRodney W. Grimes * 182668855966SAlan Cox * For the given object, either frees or invalidates each of the 18276bbee8e2SAlan Cox * specified pages. In general, a page is freed. However, if a page is 18286bbee8e2SAlan Cox * wired for any reason other than the existence of a managed, wired 18296bbee8e2SAlan Cox * mapping, then it may be invalidated but not removed from the object. 18306bbee8e2SAlan Cox * Pages are specified by the given range ["start", "end") and the option 18316bbee8e2SAlan Cox * OBJPR_CLEANONLY. As a special case, if "end" is zero, then the range 18326bbee8e2SAlan Cox * extends from "start" to the end of the object. If the option 18336bbee8e2SAlan Cox * OBJPR_CLEANONLY is specified, then only the non-dirty pages within the 18346bbee8e2SAlan Cox * specified range are affected. If the option OBJPR_NOTMAPPED is 18356bbee8e2SAlan Cox * specified, then the pages within the specified range must have no 18366bbee8e2SAlan Cox * mappings. Otherwise, if this option is not specified, any mappings to 18376bbee8e2SAlan Cox * the specified pages are removed before the pages are freed or 18386bbee8e2SAlan Cox * invalidated. 183968855966SAlan Cox * 18406bbee8e2SAlan Cox * In general, this operation should only be performed on objects that 18416bbee8e2SAlan Cox * contain managed pages. There are, however, two exceptions. First, it 18426bbee8e2SAlan Cox * is performed on the kernel and kmem objects by vm_map_entry_delete(). 18436bbee8e2SAlan Cox * Second, it is used by msync(..., MS_INVALIDATE) to invalidate device- 18446bbee8e2SAlan Cox * backed pages. In both of these cases, the option OBJPR_CLEANONLY must 18456bbee8e2SAlan Cox * not be specified and the option OBJPR_NOTMAPPED must be specified. 1846df8bae1dSRodney W. Grimes * 1847df8bae1dSRodney W. Grimes * The object must be locked. 1848df8bae1dSRodney W. Grimes */ 184926f9a767SRodney W. Grimes void 1850ecde4b32SAlan Cox vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end, 18516bbee8e2SAlan Cox int options) 1852df8bae1dSRodney W. Grimes { 1853d031cff1SMatthew Dillon vm_page_t p, next; 185459677d3cSAlan Cox int wirings; 1855df8bae1dSRodney W. Grimes 1856*89f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 185728634820SAlan Cox KASSERT((object->flags & OBJ_UNMANAGED) == 0 || 18586bbee8e2SAlan Cox (options & (OBJPR_CLEANONLY | OBJPR_NOTMAPPED)) == OBJPR_NOTMAPPED, 18596bbee8e2SAlan Cox ("vm_object_page_remove: illegal options for object %p", object)); 1860ecde4b32SAlan Cox if (object->resident_page_count == 0) 186125732691SAlan Cox goto skipmemq; 1862d474eaaaSDoug Rabson vm_object_pip_add(object, 1); 186326f9a767SRodney W. Grimes again: 1864b382c10aSKonstantin Belousov p = vm_page_find_least(object, start); 18652965a453SKip Macy 186675741c04SAlan Cox /* 18676bbee8e2SAlan Cox * Here, the variable "p" is either (1) the page with the least pindex 18686bbee8e2SAlan Cox * greater than or equal to the parameter "start" or (2) NULL. 186975741c04SAlan Cox */ 18706bbee8e2SAlan Cox for (; p != NULL && (p->pindex < end || end == 0); p = next) { 1871b18bfc3dSJohn Dyson next = TAILQ_NEXT(p, listq); 187275741c04SAlan Cox 187359677d3cSAlan Cox /* 18746bbee8e2SAlan Cox * If the page is wired for any reason besides the existence 18756bbee8e2SAlan Cox * of managed, wired mappings, then it cannot be freed. For 18766bbee8e2SAlan Cox * example, fictitious pages, which represent device memory, 18776bbee8e2SAlan Cox * are inherently wired and cannot be freed. They can, 18786bbee8e2SAlan Cox * however, be invalidated if the option OBJPR_CLEANONLY is 18796bbee8e2SAlan Cox * not specified. 188059677d3cSAlan Cox */ 18812965a453SKip Macy vm_page_lock(p); 188259677d3cSAlan Cox if ((wirings = p->wire_count) != 0 && 188359677d3cSAlan Cox (wirings = pmap_page_wired_mappings(p)) != p->wire_count) { 18846bbee8e2SAlan Cox if ((options & OBJPR_NOTMAPPED) == 0) { 18854fec79beSAlan Cox pmap_remove_all(p); 18866bbee8e2SAlan Cox /* Account for removal of wired mappings. */ 18876bbee8e2SAlan Cox if (wirings != 0) 188859677d3cSAlan Cox p->wire_count -= wirings; 18896bbee8e2SAlan Cox } 18906bbee8e2SAlan Cox if ((options & OBJPR_CLEANONLY) == 0) { 1891bd7e5f99SJohn Dyson p->valid = 0; 1892a28042d1SAlan Cox vm_page_undirty(p); 1893a28042d1SAlan Cox } 18942965a453SKip Macy vm_page_unlock(p); 18950d94caffSDavid Greenman continue; 18960d94caffSDavid Greenman } 189732585dd6SAlan Cox if (vm_page_sleep_if_busy(p, TRUE, "vmopar")) 189826f9a767SRodney W. Grimes goto again; 189968855966SAlan Cox KASSERT((p->flags & PG_FICTITIOUS) == 0, 190068855966SAlan Cox ("vm_object_page_remove: page %p is fictitious", p)); 19016bbee8e2SAlan Cox if ((options & OBJPR_CLEANONLY) != 0 && p->valid != 0) { 19026bbee8e2SAlan Cox if ((options & OBJPR_NOTMAPPED) == 0) 190378985e42SAlan Cox pmap_remove_write(p); 19042965a453SKip Macy if (p->dirty) { 19052965a453SKip Macy vm_page_unlock(p); 19067c1f6cedSDavid Greenman continue; 19077c1f6cedSDavid Greenman } 19082965a453SKip Macy } 19096bbee8e2SAlan Cox if ((options & OBJPR_NOTMAPPED) == 0) { 19104fec79beSAlan Cox pmap_remove_all(p); 19116bbee8e2SAlan Cox /* Account for removal of wired mappings. */ 19125f9c767bSKonstantin Belousov if (wirings != 0) { 19135f9c767bSKonstantin Belousov KASSERT(p->wire_count == wirings, 19145f9c767bSKonstantin Belousov ("inconsistent wire count %d %d %p", 19155f9c767bSKonstantin Belousov p->wire_count, wirings, p)); 19165f9c767bSKonstantin Belousov p->wire_count = 0; 19175f9c767bSKonstantin Belousov atomic_subtract_int(&cnt.v_wire_count, 1); 19185f9c767bSKonstantin Belousov } 19196bbee8e2SAlan Cox } 1920df8bae1dSRodney W. Grimes vm_page_free(p); 19212965a453SKip Macy vm_page_unlock(p); 19222965a453SKip Macy } 1923f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 192425732691SAlan Cox skipmemq: 1925c9341161SAttilio Rao if (__predict_false(!vm_object_cache_is_empty(object))) 1926c9444914SAlan Cox vm_page_cache_free(object, start, end); 1927c0503609SDavid Greenman } 1928df8bae1dSRodney W. Grimes 1929df8bae1dSRodney W. Grimes /* 1930936c09acSJohn Baldwin * vm_object_page_cache: 1931936c09acSJohn Baldwin * 1932936c09acSJohn Baldwin * For the given object, attempt to move the specified clean 1933936c09acSJohn Baldwin * pages to the cache queue. If a page is wired for any reason, 1934936c09acSJohn Baldwin * then it will not be changed. Pages are specified by the given 1935936c09acSJohn Baldwin * range ["start", "end"). As a special case, if "end" is zero, 1936936c09acSJohn Baldwin * then the range extends from "start" to the end of the object. 1937936c09acSJohn Baldwin * Any mappings to the specified pages are removed before the 1938936c09acSJohn Baldwin * pages are moved to the cache queue. 1939936c09acSJohn Baldwin * 1940936c09acSJohn Baldwin * This operation should only be performed on objects that 194128634820SAlan Cox * contain non-fictitious, managed pages. 1942936c09acSJohn Baldwin * 1943936c09acSJohn Baldwin * The object must be locked. 1944936c09acSJohn Baldwin */ 1945936c09acSJohn Baldwin void 1946936c09acSJohn Baldwin vm_object_page_cache(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 1947936c09acSJohn Baldwin { 1948936c09acSJohn Baldwin struct mtx *mtx, *new_mtx; 1949936c09acSJohn Baldwin vm_page_t p, next; 1950936c09acSJohn Baldwin 1951*89f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 195228634820SAlan Cox KASSERT((object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0, 1953936c09acSJohn Baldwin ("vm_object_page_cache: illegal object %p", object)); 1954936c09acSJohn Baldwin if (object->resident_page_count == 0) 1955936c09acSJohn Baldwin return; 1956936c09acSJohn Baldwin p = vm_page_find_least(object, start); 1957936c09acSJohn Baldwin 1958936c09acSJohn Baldwin /* 1959936c09acSJohn Baldwin * Here, the variable "p" is either (1) the page with the least pindex 1960936c09acSJohn Baldwin * greater than or equal to the parameter "start" or (2) NULL. 1961936c09acSJohn Baldwin */ 1962936c09acSJohn Baldwin mtx = NULL; 1963936c09acSJohn Baldwin for (; p != NULL && (p->pindex < end || end == 0); p = next) { 1964936c09acSJohn Baldwin next = TAILQ_NEXT(p, listq); 1965936c09acSJohn Baldwin 1966936c09acSJohn Baldwin /* 1967936c09acSJohn Baldwin * Avoid releasing and reacquiring the same page lock. 1968936c09acSJohn Baldwin */ 1969936c09acSJohn Baldwin new_mtx = vm_page_lockptr(p); 1970936c09acSJohn Baldwin if (mtx != new_mtx) { 1971936c09acSJohn Baldwin if (mtx != NULL) 1972936c09acSJohn Baldwin mtx_unlock(mtx); 1973936c09acSJohn Baldwin mtx = new_mtx; 1974936c09acSJohn Baldwin mtx_lock(mtx); 1975936c09acSJohn Baldwin } 1976936c09acSJohn Baldwin vm_page_try_to_cache(p); 1977936c09acSJohn Baldwin } 1978936c09acSJohn Baldwin if (mtx != NULL) 1979936c09acSJohn Baldwin mtx_unlock(mtx); 1980936c09acSJohn Baldwin } 1981936c09acSJohn Baldwin 1982936c09acSJohn Baldwin /* 1983387aabc5SAlan Cox * Populate the specified range of the object with valid pages. Returns 1984387aabc5SAlan Cox * TRUE if the range is successfully populated and FALSE otherwise. 1985387aabc5SAlan Cox * 1986387aabc5SAlan Cox * Note: This function should be optimized to pass a larger array of 1987387aabc5SAlan Cox * pages to vm_pager_get_pages() before it is applied to a non- 1988387aabc5SAlan Cox * OBJT_DEVICE object. 1989387aabc5SAlan Cox * 1990387aabc5SAlan Cox * The object must be locked. 1991387aabc5SAlan Cox */ 1992387aabc5SAlan Cox boolean_t 1993387aabc5SAlan Cox vm_object_populate(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 1994387aabc5SAlan Cox { 1995387aabc5SAlan Cox vm_page_t m, ma[1]; 1996387aabc5SAlan Cox vm_pindex_t pindex; 1997387aabc5SAlan Cox int rv; 1998387aabc5SAlan Cox 1999*89f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 2000387aabc5SAlan Cox for (pindex = start; pindex < end; pindex++) { 2001387aabc5SAlan Cox m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL | 2002387aabc5SAlan Cox VM_ALLOC_RETRY); 2003387aabc5SAlan Cox if (m->valid != VM_PAGE_BITS_ALL) { 2004387aabc5SAlan Cox ma[0] = m; 2005387aabc5SAlan Cox rv = vm_pager_get_pages(object, ma, 1, 0); 2006387aabc5SAlan Cox m = vm_page_lookup(object, pindex); 2007387aabc5SAlan Cox if (m == NULL) 2008387aabc5SAlan Cox break; 2009387aabc5SAlan Cox if (rv != VM_PAGER_OK) { 20102965a453SKip Macy vm_page_lock(m); 2011387aabc5SAlan Cox vm_page_free(m); 20122965a453SKip Macy vm_page_unlock(m); 2013387aabc5SAlan Cox break; 2014387aabc5SAlan Cox } 2015387aabc5SAlan Cox } 2016387aabc5SAlan Cox /* 2017387aabc5SAlan Cox * Keep "m" busy because a subsequent iteration may unlock 2018387aabc5SAlan Cox * the object. 2019387aabc5SAlan Cox */ 2020387aabc5SAlan Cox } 2021387aabc5SAlan Cox if (pindex > start) { 2022387aabc5SAlan Cox m = vm_page_lookup(object, start); 2023387aabc5SAlan Cox while (m != NULL && m->pindex < pindex) { 2024387aabc5SAlan Cox vm_page_wakeup(m); 2025387aabc5SAlan Cox m = TAILQ_NEXT(m, listq); 2026387aabc5SAlan Cox } 2027387aabc5SAlan Cox } 2028387aabc5SAlan Cox return (pindex == end); 2029387aabc5SAlan Cox } 2030387aabc5SAlan Cox 2031387aabc5SAlan Cox /* 2032df8bae1dSRodney W. Grimes * Routine: vm_object_coalesce 2033df8bae1dSRodney W. Grimes * Function: Coalesces two objects backing up adjoining 2034df8bae1dSRodney W. Grimes * regions of memory into a single object. 2035df8bae1dSRodney W. Grimes * 2036df8bae1dSRodney W. Grimes * returns TRUE if objects were combined. 2037df8bae1dSRodney W. Grimes * 2038df8bae1dSRodney W. Grimes * NOTE: Only works at the moment if the second object is NULL - 2039df8bae1dSRodney W. Grimes * if it's not, which object do we lock first? 2040df8bae1dSRodney W. Grimes * 2041df8bae1dSRodney W. Grimes * Parameters: 2042df8bae1dSRodney W. Grimes * prev_object First object to coalesce 2043df8bae1dSRodney W. Grimes * prev_offset Offset into prev_object 2044df8bae1dSRodney W. Grimes * prev_size Size of reference to prev_object 204557a21abaSAlan Cox * next_size Size of reference to the second object 20463364c323SKonstantin Belousov * reserved Indicator that extension region has 20473364c323SKonstantin Belousov * swap accounted for 2048df8bae1dSRodney W. Grimes * 2049df8bae1dSRodney W. Grimes * Conditions: 2050df8bae1dSRodney W. Grimes * The object must *not* be locked. 2051df8bae1dSRodney W. Grimes */ 20520d94caffSDavid Greenman boolean_t 205357a21abaSAlan Cox vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset, 20543364c323SKonstantin Belousov vm_size_t prev_size, vm_size_t next_size, boolean_t reserved) 2055df8bae1dSRodney W. Grimes { 2056ea41812fSAlan Cox vm_pindex_t next_pindex; 2057df8bae1dSRodney W. Grimes 205800e1854aSAlan Cox if (prev_object == NULL) 2059df8bae1dSRodney W. Grimes return (TRUE); 2060*89f6b863SAttilio Rao VM_OBJECT_WLOCK(prev_object); 20614112823fSMatthew Dillon if (prev_object->type != OBJT_DEFAULT && 20624112823fSMatthew Dillon prev_object->type != OBJT_SWAP) { 2063*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(prev_object); 206430dcfc09SJohn Dyson return (FALSE); 206530dcfc09SJohn Dyson } 206630dcfc09SJohn Dyson 2067df8bae1dSRodney W. Grimes /* 2068df8bae1dSRodney W. Grimes * Try to collapse the object first 2069df8bae1dSRodney W. Grimes */ 2070df8bae1dSRodney W. Grimes vm_object_collapse(prev_object); 2071df8bae1dSRodney W. Grimes 2072df8bae1dSRodney W. Grimes /* 20730d94caffSDavid Greenman * Can't coalesce if: . more than one reference . paged out . shadows 20740d94caffSDavid Greenman * another object . has a copy elsewhere (any of which mean that the 20750d94caffSDavid Greenman * pages not mapped to prev_entry may be in use anyway) 2076df8bae1dSRodney W. Grimes */ 20778cc7e047SJohn Dyson if (prev_object->backing_object != NULL) { 2078*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(prev_object); 2079df8bae1dSRodney W. Grimes return (FALSE); 2080df8bae1dSRodney W. Grimes } 2081a316d390SJohn Dyson 2082a316d390SJohn Dyson prev_size >>= PAGE_SHIFT; 2083a316d390SJohn Dyson next_size >>= PAGE_SHIFT; 208457a21abaSAlan Cox next_pindex = OFF_TO_IDX(prev_offset) + prev_size; 20858cc7e047SJohn Dyson 20868cc7e047SJohn Dyson if ((prev_object->ref_count > 1) && 2087ea41812fSAlan Cox (prev_object->size != next_pindex)) { 2088*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(prev_object); 20898cc7e047SJohn Dyson return (FALSE); 20908cc7e047SJohn Dyson } 20918cc7e047SJohn Dyson 2092df8bae1dSRodney W. Grimes /* 20933364c323SKonstantin Belousov * Account for the charge. 20943364c323SKonstantin Belousov */ 2095ef694c1aSEdward Tomasz Napierala if (prev_object->cred != NULL) { 20963364c323SKonstantin Belousov 20973364c323SKonstantin Belousov /* 20983364c323SKonstantin Belousov * If prev_object was charged, then this mapping, 20993364c323SKonstantin Belousov * althought not charged now, may become writable 2100ef694c1aSEdward Tomasz Napierala * later. Non-NULL cred in the object would prevent 21013364c323SKonstantin Belousov * swap reservation during enabling of the write 21023364c323SKonstantin Belousov * access, so reserve swap now. Failed reservation 21033364c323SKonstantin Belousov * cause allocation of the separate object for the map 21043364c323SKonstantin Belousov * entry, and swap reservation for this entry is 21053364c323SKonstantin Belousov * managed in appropriate time. 21063364c323SKonstantin Belousov */ 2107ef694c1aSEdward Tomasz Napierala if (!reserved && !swap_reserve_by_cred(ptoa(next_size), 2108ef694c1aSEdward Tomasz Napierala prev_object->cred)) { 21093364c323SKonstantin Belousov return (FALSE); 21103364c323SKonstantin Belousov } 21113364c323SKonstantin Belousov prev_object->charge += ptoa(next_size); 21123364c323SKonstantin Belousov } 21133364c323SKonstantin Belousov 21143364c323SKonstantin Belousov /* 21150d94caffSDavid Greenman * Remove any pages that may still be in the object from a previous 21160d94caffSDavid Greenman * deallocation. 2117df8bae1dSRodney W. Grimes */ 2118ea41812fSAlan Cox if (next_pindex < prev_object->size) { 21196bbee8e2SAlan Cox vm_object_page_remove(prev_object, next_pindex, next_pindex + 21206bbee8e2SAlan Cox next_size, 0); 2121ea41812fSAlan Cox if (prev_object->type == OBJT_SWAP) 2122ea41812fSAlan Cox swap_pager_freespace(prev_object, 2123ea41812fSAlan Cox next_pindex, next_size); 21243364c323SKonstantin Belousov #if 0 2125ef694c1aSEdward Tomasz Napierala if (prev_object->cred != NULL) { 21263364c323SKonstantin Belousov KASSERT(prev_object->charge >= 21273364c323SKonstantin Belousov ptoa(prev_object->size - next_pindex), 21283364c323SKonstantin Belousov ("object %p overcharged 1 %jx %jx", prev_object, 21293364c323SKonstantin Belousov (uintmax_t)next_pindex, (uintmax_t)next_size)); 21303364c323SKonstantin Belousov prev_object->charge -= ptoa(prev_object->size - 21313364c323SKonstantin Belousov next_pindex); 21323364c323SKonstantin Belousov } 21333364c323SKonstantin Belousov #endif 2134ea41812fSAlan Cox } 2135df8bae1dSRodney W. Grimes 2136df8bae1dSRodney W. Grimes /* 2137df8bae1dSRodney W. Grimes * Extend the object if necessary. 2138df8bae1dSRodney W. Grimes */ 2139ea41812fSAlan Cox if (next_pindex + next_size > prev_object->size) 2140ea41812fSAlan Cox prev_object->size = next_pindex + next_size; 2141df8bae1dSRodney W. Grimes 2142*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(prev_object); 2143df8bae1dSRodney W. Grimes return (TRUE); 2144df8bae1dSRodney W. Grimes } 2145df8bae1dSRodney W. Grimes 21467a5a6352SMatthew Dillon void 21477a5a6352SMatthew Dillon vm_object_set_writeable_dirty(vm_object_t object) 21487a5a6352SMatthew Dillon { 21497a5a6352SMatthew Dillon 2150*89f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 21513280870dSKonstantin Belousov if (object->type != OBJT_VNODE) 21523280870dSKonstantin Belousov return; 21533280870dSKonstantin Belousov object->generation++; 21543280870dSKonstantin Belousov if ((object->flags & OBJ_MIGHTBEDIRTY) != 0) 2155ee39666aSJeff Roberson return; 2156af51d7bfSAlan Cox vm_object_set_flag(object, OBJ_MIGHTBEDIRTY); 21577a5a6352SMatthew Dillon } 21587a5a6352SMatthew Dillon 2159c7c34a24SBruce Evans #include "opt_ddb.h" 2160c3cb3e12SDavid Greenman #ifdef DDB 2161c7c34a24SBruce Evans #include <sys/kernel.h> 2162c7c34a24SBruce Evans 2163ce9edcf5SPoul-Henning Kamp #include <sys/cons.h> 2164c7c34a24SBruce Evans 2165c7c34a24SBruce Evans #include <ddb/ddb.h> 2166c7c34a24SBruce Evans 2167cac597e4SBruce Evans static int 21681b40f8c0SMatthew Dillon _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry) 2169a1f6d91cSDavid Greenman { 2170a1f6d91cSDavid Greenman vm_map_t tmpm; 2171a1f6d91cSDavid Greenman vm_map_entry_t tmpe; 2172a1f6d91cSDavid Greenman vm_object_t obj; 2173a1f6d91cSDavid Greenman int entcount; 2174a1f6d91cSDavid Greenman 2175a1f6d91cSDavid Greenman if (map == 0) 2176a1f6d91cSDavid Greenman return 0; 2177a1f6d91cSDavid Greenman 2178a1f6d91cSDavid Greenman if (entry == 0) { 2179a1f6d91cSDavid Greenman tmpe = map->header.next; 2180a1f6d91cSDavid Greenman entcount = map->nentries; 2181a1f6d91cSDavid Greenman while (entcount-- && (tmpe != &map->header)) { 2182a1f6d91cSDavid Greenman if (_vm_object_in_map(map, object, tmpe)) { 2183a1f6d91cSDavid Greenman return 1; 2184a1f6d91cSDavid Greenman } 2185a1f6d91cSDavid Greenman tmpe = tmpe->next; 2186a1f6d91cSDavid Greenman } 21879fdfe602SMatthew Dillon } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 21889fdfe602SMatthew Dillon tmpm = entry->object.sub_map; 2189a1f6d91cSDavid Greenman tmpe = tmpm->header.next; 2190a1f6d91cSDavid Greenman entcount = tmpm->nentries; 2191a1f6d91cSDavid Greenman while (entcount-- && tmpe != &tmpm->header) { 2192a1f6d91cSDavid Greenman if (_vm_object_in_map(tmpm, object, tmpe)) { 2193a1f6d91cSDavid Greenman return 1; 2194a1f6d91cSDavid Greenman } 2195a1f6d91cSDavid Greenman tmpe = tmpe->next; 2196a1f6d91cSDavid Greenman } 21978aef1712SMatthew Dillon } else if ((obj = entry->object.vm_object) != NULL) { 219824a1cce3SDavid Greenman for (; obj; obj = obj->backing_object) 2199a1f6d91cSDavid Greenman if (obj == object) { 2200a1f6d91cSDavid Greenman return 1; 2201a1f6d91cSDavid Greenman } 2202a1f6d91cSDavid Greenman } 2203a1f6d91cSDavid Greenman return 0; 2204a1f6d91cSDavid Greenman } 2205a1f6d91cSDavid Greenman 2206cac597e4SBruce Evans static int 22071b40f8c0SMatthew Dillon vm_object_in_map(vm_object_t object) 2208a1f6d91cSDavid Greenman { 2209a1f6d91cSDavid Greenman struct proc *p; 22101005a129SJohn Baldwin 221160517fd1SJohn Baldwin /* sx_slock(&allproc_lock); */ 2212f67af5c9SXin LI FOREACH_PROC_IN_SYSTEM(p) { 2213a1f6d91cSDavid Greenman if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */) 2214a1f6d91cSDavid Greenman continue; 2215553629ebSJake Burkholder if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) { 221660517fd1SJohn Baldwin /* sx_sunlock(&allproc_lock); */ 2217a1f6d91cSDavid Greenman return 1; 2218a1f6d91cSDavid Greenman } 2219553629ebSJake Burkholder } 222060517fd1SJohn Baldwin /* sx_sunlock(&allproc_lock); */ 2221a1f6d91cSDavid Greenman if (_vm_object_in_map(kernel_map, object, 0)) 2222a1f6d91cSDavid Greenman return 1; 2223a1f6d91cSDavid Greenman if (_vm_object_in_map(kmem_map, object, 0)) 2224a1f6d91cSDavid Greenman return 1; 2225a1f6d91cSDavid Greenman if (_vm_object_in_map(pager_map, object, 0)) 2226a1f6d91cSDavid Greenman return 1; 2227a1f6d91cSDavid Greenman if (_vm_object_in_map(buffer_map, object, 0)) 2228a1f6d91cSDavid Greenman return 1; 2229a1f6d91cSDavid Greenman return 0; 2230a1f6d91cSDavid Greenman } 2231a1f6d91cSDavid Greenman 2232c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check) 2233f708ef1bSPoul-Henning Kamp { 2234a1f6d91cSDavid Greenman vm_object_t object; 2235a1f6d91cSDavid Greenman 2236a1f6d91cSDavid Greenman /* 2237a1f6d91cSDavid Greenman * make sure that internal objs are in a map somewhere 2238a1f6d91cSDavid Greenman * and none have zero ref counts. 2239a1f6d91cSDavid Greenman */ 2240cc64b484SAlfred Perlstein TAILQ_FOREACH(object, &vm_object_list, object_list) { 224124a1cce3SDavid Greenman if (object->handle == NULL && 224224a1cce3SDavid Greenman (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) { 2243a1f6d91cSDavid Greenman if (object->ref_count == 0) { 22443efc015bSPeter Wemm db_printf("vmochk: internal obj has zero ref count: %ld\n", 22453efc015bSPeter Wemm (long)object->size); 2246a1f6d91cSDavid Greenman } 2247a1f6d91cSDavid Greenman if (!vm_object_in_map(object)) { 2248fc62ef1fSBruce Evans db_printf( 2249fc62ef1fSBruce Evans "vmochk: internal obj is not in a map: " 2250fc62ef1fSBruce Evans "ref: %d, size: %lu: 0x%lx, backing_object: %p\n", 2251fc62ef1fSBruce Evans object->ref_count, (u_long)object->size, 2252fc62ef1fSBruce Evans (u_long)object->size, 2253fc62ef1fSBruce Evans (void *)object->backing_object); 2254a1f6d91cSDavid Greenman } 2255a1f6d91cSDavid Greenman } 2256a1f6d91cSDavid Greenman } 2257a1f6d91cSDavid Greenman } 2258a1f6d91cSDavid Greenman 225926f9a767SRodney W. Grimes /* 2260df8bae1dSRodney W. Grimes * vm_object_print: [ debug ] 2261df8bae1dSRodney W. Grimes */ 2262c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static) 2263df8bae1dSRodney W. Grimes { 2264c7c34a24SBruce Evans /* XXX convert args. */ 2265c7c34a24SBruce Evans vm_object_t object = (vm_object_t)addr; 2266c7c34a24SBruce Evans boolean_t full = have_addr; 2267c7c34a24SBruce Evans 2268d031cff1SMatthew Dillon vm_page_t p; 2269df8bae1dSRodney W. Grimes 2270c7c34a24SBruce Evans /* XXX count is an (unused) arg. Avoid shadowing it. */ 2271c7c34a24SBruce Evans #define count was_count 2272c7c34a24SBruce Evans 2273d031cff1SMatthew Dillon int count; 2274df8bae1dSRodney W. Grimes 2275df8bae1dSRodney W. Grimes if (object == NULL) 2276df8bae1dSRodney W. Grimes return; 2277df8bae1dSRodney W. Grimes 2278eb95adefSBruce Evans db_iprintf( 2279ef694c1aSEdward Tomasz Napierala "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x ruid %d charge %jx\n", 2280e47cd172SMaxime Henrion object, (int)object->type, (uintmax_t)object->size, 22813364c323SKonstantin Belousov object->resident_page_count, object->ref_count, object->flags, 2282ef694c1aSEdward Tomasz Napierala object->cred ? object->cred->cr_ruid : -1, (uintmax_t)object->charge); 2283e47cd172SMaxime Henrion db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n", 22841c7c3c6aSMatthew Dillon object->shadow_count, 2285eb95adefSBruce Evans object->backing_object ? object->backing_object->ref_count : 0, 2286e47cd172SMaxime Henrion object->backing_object, (uintmax_t)object->backing_object_offset); 2287df8bae1dSRodney W. Grimes 2288df8bae1dSRodney W. Grimes if (!full) 2289df8bae1dSRodney W. Grimes return; 2290df8bae1dSRodney W. Grimes 2291c7c34a24SBruce Evans db_indent += 2; 2292df8bae1dSRodney W. Grimes count = 0; 2293fc2ffbe6SPoul-Henning Kamp TAILQ_FOREACH(p, &object->memq, listq) { 2294df8bae1dSRodney W. Grimes if (count == 0) 2295c7c34a24SBruce Evans db_iprintf("memory:="); 2296df8bae1dSRodney W. Grimes else if (count == 6) { 2297c7c34a24SBruce Evans db_printf("\n"); 2298c7c34a24SBruce Evans db_iprintf(" ..."); 2299df8bae1dSRodney W. Grimes count = 0; 2300df8bae1dSRodney W. Grimes } else 2301c7c34a24SBruce Evans db_printf(","); 2302df8bae1dSRodney W. Grimes count++; 2303df8bae1dSRodney W. Grimes 2304e47cd172SMaxime Henrion db_printf("(off=0x%jx,page=0x%jx)", 2305e47cd172SMaxime Henrion (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p)); 2306df8bae1dSRodney W. Grimes } 2307df8bae1dSRodney W. Grimes if (count != 0) 2308c7c34a24SBruce Evans db_printf("\n"); 2309c7c34a24SBruce Evans db_indent -= 2; 2310df8bae1dSRodney W. Grimes } 23115070c7f8SJohn Dyson 2312c7c34a24SBruce Evans /* XXX. */ 2313c7c34a24SBruce Evans #undef count 2314c7c34a24SBruce Evans 2315c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */ 23165070c7f8SJohn Dyson void 23171b40f8c0SMatthew Dillon vm_object_print( 23181b40f8c0SMatthew Dillon /* db_expr_t */ long addr, 23191b40f8c0SMatthew Dillon boolean_t have_addr, 23201b40f8c0SMatthew Dillon /* db_expr_t */ long count, 23211b40f8c0SMatthew Dillon char *modif) 2322c7c34a24SBruce Evans { 2323c7c34a24SBruce Evans vm_object_print_static(addr, have_addr, count, modif); 2324c7c34a24SBruce Evans } 2325c7c34a24SBruce Evans 2326c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages) 23275070c7f8SJohn Dyson { 23285070c7f8SJohn Dyson vm_object_t object; 2329bb2ac86fSKonstantin Belousov vm_pindex_t fidx; 2330bb2ac86fSKonstantin Belousov vm_paddr_t pa; 2331bb2ac86fSKonstantin Belousov vm_page_t m, prev_m; 2332bb2ac86fSKonstantin Belousov int rcount, nl, c; 2333cc64b484SAlfred Perlstein 2334bb2ac86fSKonstantin Belousov nl = 0; 2335cc64b484SAlfred Perlstein TAILQ_FOREACH(object, &vm_object_list, object_list) { 2336fc62ef1fSBruce Evans db_printf("new object: %p\n", (void *)object); 23375070c7f8SJohn Dyson if (nl > 18) { 23385070c7f8SJohn Dyson c = cngetc(); 23395070c7f8SJohn Dyson if (c != ' ') 23405070c7f8SJohn Dyson return; 23415070c7f8SJohn Dyson nl = 0; 23425070c7f8SJohn Dyson } 23435070c7f8SJohn Dyson nl++; 23445070c7f8SJohn Dyson rcount = 0; 23455070c7f8SJohn Dyson fidx = 0; 2346bb2ac86fSKonstantin Belousov pa = -1; 2347bb2ac86fSKonstantin Belousov TAILQ_FOREACH(m, &object->memq, listq) { 2348bb2ac86fSKonstantin Belousov if (m->pindex > 128) 2349bb2ac86fSKonstantin Belousov break; 2350bb2ac86fSKonstantin Belousov if ((prev_m = TAILQ_PREV(m, pglist, listq)) != NULL && 2351bb2ac86fSKonstantin Belousov prev_m->pindex + 1 != m->pindex) { 23525070c7f8SJohn Dyson if (rcount) { 23533efc015bSPeter Wemm db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 23543efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 23555070c7f8SJohn Dyson if (nl > 18) { 23565070c7f8SJohn Dyson c = cngetc(); 23575070c7f8SJohn Dyson if (c != ' ') 23585070c7f8SJohn Dyson return; 23595070c7f8SJohn Dyson nl = 0; 23605070c7f8SJohn Dyson } 23615070c7f8SJohn Dyson nl++; 23625070c7f8SJohn Dyson rcount = 0; 23635070c7f8SJohn Dyson } 23645070c7f8SJohn Dyson } 23655070c7f8SJohn Dyson if (rcount && 23665070c7f8SJohn Dyson (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) { 23675070c7f8SJohn Dyson ++rcount; 23685070c7f8SJohn Dyson continue; 23695070c7f8SJohn Dyson } 23705070c7f8SJohn Dyson if (rcount) { 23712446e4f0SAlan Cox db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 23723efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 23735070c7f8SJohn Dyson if (nl > 18) { 23745070c7f8SJohn Dyson c = cngetc(); 23755070c7f8SJohn Dyson if (c != ' ') 23765070c7f8SJohn Dyson return; 23775070c7f8SJohn Dyson nl = 0; 23785070c7f8SJohn Dyson } 23795070c7f8SJohn Dyson nl++; 23805070c7f8SJohn Dyson } 2381bb2ac86fSKonstantin Belousov fidx = m->pindex; 23825070c7f8SJohn Dyson pa = VM_PAGE_TO_PHYS(m); 23835070c7f8SJohn Dyson rcount = 1; 23845070c7f8SJohn Dyson } 23855070c7f8SJohn Dyson if (rcount) { 23863efc015bSPeter Wemm db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 23873efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 23885070c7f8SJohn Dyson if (nl > 18) { 23895070c7f8SJohn Dyson c = cngetc(); 23905070c7f8SJohn Dyson if (c != ' ') 23915070c7f8SJohn Dyson return; 23925070c7f8SJohn Dyson nl = 0; 23935070c7f8SJohn Dyson } 23945070c7f8SJohn Dyson nl++; 23955070c7f8SJohn Dyson } 23965070c7f8SJohn Dyson } 23975070c7f8SJohn Dyson } 2398c3cb3e12SDavid Greenman #endif /* DDB */ 2399