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> 81fb919e4dSMark Murray #include <sys/vnode.h> 82fb919e4dSMark Murray #include <sys/vmmeter.h> 831005a129SJohn Baldwin #include <sys/sx.h> 84df8bae1dSRodney W. Grimes 85df8bae1dSRodney W. Grimes #include <vm/vm.h> 86efeaf95aSDavid Greenman #include <vm/vm_param.h> 87efeaf95aSDavid Greenman #include <vm/pmap.h> 88efeaf95aSDavid Greenman #include <vm/vm_map.h> 89efeaf95aSDavid Greenman #include <vm/vm_object.h> 90df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 9126f9a767SRodney W. Grimes #include <vm/vm_pageout.h> 920d94caffSDavid Greenman #include <vm/vm_pager.h> 9305f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h> 94a1f6d91cSDavid Greenman #include <vm/vm_kern.h> 95efeaf95aSDavid Greenman #include <vm/vm_extern.h> 96f8a47341SAlan Cox #include <vm/vm_reserv.h> 97670d17b5SJeff Roberson #include <vm/uma.h> 9826f9a767SRodney W. Grimes 99c53f7aceSDag-Erling Smørgrav static int old_msync; 100c53f7aceSDag-Erling Smørgrav SYSCTL_INT(_vm, OID_AUTO, old_msync, CTLFLAG_RW, &old_msync, 0, 101c53f7aceSDag-Erling Smørgrav "Use old (insecure) msync behavior"); 102c53f7aceSDag-Erling Smørgrav 103757216f3SKonstantin Belousov static int vm_object_page_collect_flush(vm_object_t object, vm_page_t p, 1043280870dSKonstantin Belousov int pagerflags, int flags, int *clearobjflags); 1053280870dSKonstantin Belousov static boolean_t vm_object_page_remove_write(vm_page_t p, int flags, 1063280870dSKonstantin Belousov int *clearobjflags); 107b9b7a4beSMatthew Dillon static void vm_object_qcollapse(vm_object_t object); 10802dd8331SAlan Cox static void vm_object_vndeallocate(vm_object_t object); 109f6b04d2bSDavid Greenman 110df8bae1dSRodney W. Grimes /* 111df8bae1dSRodney W. Grimes * Virtual memory objects maintain the actual data 112df8bae1dSRodney W. Grimes * associated with allocated virtual memory. A given 113df8bae1dSRodney W. Grimes * page of memory exists within exactly one object. 114df8bae1dSRodney W. Grimes * 115df8bae1dSRodney W. Grimes * An object is only deallocated when all "references" 116df8bae1dSRodney W. Grimes * are given up. Only one "reference" to a given 117df8bae1dSRodney W. Grimes * region of an object should be writeable. 118df8bae1dSRodney W. Grimes * 119df8bae1dSRodney W. Grimes * Associated with each object is a list of all resident 120df8bae1dSRodney W. Grimes * memory pages belonging to that object; this list is 121df8bae1dSRodney W. Grimes * maintained by the "vm_page" module, and locked by the object's 122df8bae1dSRodney W. Grimes * lock. 123df8bae1dSRodney W. Grimes * 124df8bae1dSRodney W. Grimes * Each object also records a "pager" routine which is 125df8bae1dSRodney W. Grimes * used to retrieve (and store) pages to the proper backing 126df8bae1dSRodney W. Grimes * storage. In addition, objects may be backed by other 127df8bae1dSRodney W. Grimes * objects from which they were virtual-copied. 128df8bae1dSRodney W. Grimes * 129df8bae1dSRodney W. Grimes * The only items within the object structure which are 130df8bae1dSRodney W. Grimes * modified after time of creation are: 131df8bae1dSRodney W. Grimes * reference count locked by object's lock 132df8bae1dSRodney W. Grimes * pager routine locked by object's lock 133df8bae1dSRodney W. Grimes * 134df8bae1dSRodney W. Grimes */ 135df8bae1dSRodney W. Grimes 13628f8db14SBruce Evans struct object_q vm_object_list; 137a5698387SAlan Cox struct mtx vm_object_list_mtx; /* lock for object list and count */ 138cccf11b8SAlan Cox 139cccf11b8SAlan Cox struct vm_object kernel_object_store; 140cccf11b8SAlan Cox struct vm_object kmem_object_store; 141df8bae1dSRodney W. Grimes 142*6472ac3dSEd Schouten static SYSCTL_NODE(_vm_stats, OID_AUTO, object, CTLFLAG_RD, 0, 143*6472ac3dSEd Schouten "VM object stats"); 144604c2bbcSAlan Cox 145f708ef1bSPoul-Henning Kamp static long object_collapses; 146604c2bbcSAlan Cox SYSCTL_LONG(_vm_stats_object, OID_AUTO, collapses, CTLFLAG_RD, 147604c2bbcSAlan Cox &object_collapses, 0, "VM object collapses"); 148604c2bbcSAlan Cox 149f708ef1bSPoul-Henning Kamp static long object_bypasses; 150604c2bbcSAlan Cox SYSCTL_LONG(_vm_stats_object, OID_AUTO, bypasses, CTLFLAG_RD, 151604c2bbcSAlan Cox &object_bypasses, 0, "VM object bypasses"); 152dad740e9SAlan Cox 153670d17b5SJeff Roberson static uma_zone_t obj_zone; 1548355f576SJeff Roberson 155b23f72e9SBrian Feldman static int vm_object_zinit(void *mem, int size, int flags); 1568355f576SJeff Roberson 1578355f576SJeff Roberson #ifdef INVARIANTS 1588355f576SJeff Roberson static void vm_object_zdtor(void *mem, int size, void *arg); 1598355f576SJeff Roberson 1608355f576SJeff Roberson static void 1618355f576SJeff Roberson vm_object_zdtor(void *mem, int size, void *arg) 1628355f576SJeff Roberson { 1638355f576SJeff Roberson vm_object_t object; 1648355f576SJeff Roberson 1658355f576SJeff Roberson object = (vm_object_t)mem; 16643186e53SAlan Cox KASSERT(TAILQ_EMPTY(&object->memq), 16743186e53SAlan Cox ("object %p has resident pages", 16843186e53SAlan Cox object)); 169f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 170f8a47341SAlan Cox KASSERT(LIST_EMPTY(&object->rvq), 171f8a47341SAlan Cox ("object %p has reservations", 172f8a47341SAlan Cox object)); 173f8a47341SAlan Cox #endif 1747bfda801SAlan Cox KASSERT(object->cache == NULL, 1757bfda801SAlan Cox ("object %p has cached pages", 1767bfda801SAlan Cox object)); 1778355f576SJeff Roberson KASSERT(object->paging_in_progress == 0, 1788355f576SJeff Roberson ("object %p paging_in_progress = %d", 1798355f576SJeff Roberson object, object->paging_in_progress)); 1808355f576SJeff Roberson KASSERT(object->resident_page_count == 0, 1818355f576SJeff Roberson ("object %p resident_page_count = %d", 1828355f576SJeff Roberson object, object->resident_page_count)); 1838355f576SJeff Roberson KASSERT(object->shadow_count == 0, 1848355f576SJeff Roberson ("object %p shadow_count = %d", 1858355f576SJeff Roberson object, object->shadow_count)); 1868355f576SJeff Roberson } 1878355f576SJeff Roberson #endif 1888355f576SJeff Roberson 189b23f72e9SBrian Feldman static int 190b23f72e9SBrian Feldman vm_object_zinit(void *mem, int size, int flags) 1918355f576SJeff Roberson { 1928355f576SJeff Roberson vm_object_t object; 1938355f576SJeff Roberson 1948355f576SJeff Roberson object = (vm_object_t)mem; 195e3a9e1b2SAlan Cox bzero(&object->mtx, sizeof(object->mtx)); 1965285558aSAlan Cox VM_OBJECT_LOCK_INIT(object, "standard object"); 1978355f576SJeff Roberson 1988355f576SJeff Roberson /* These are true for any object that has been freed */ 1998355f576SJeff Roberson object->paging_in_progress = 0; 2008355f576SJeff Roberson object->resident_page_count = 0; 2018355f576SJeff Roberson object->shadow_count = 0; 202b23f72e9SBrian Feldman return (0); 2038355f576SJeff Roberson } 204df8bae1dSRodney W. Grimes 2053075778bSJohn Dyson void 2066395da54SIan Dowse _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object) 207df8bae1dSRodney W. Grimes { 2080cddd8f0SMatthew Dillon 209df8bae1dSRodney W. Grimes TAILQ_INIT(&object->memq); 2101c500307SAlan Cox LIST_INIT(&object->shadow_head); 211a1f6d91cSDavid Greenman 212b86ec922SMatthew Dillon object->root = NULL; 21324a1cce3SDavid Greenman object->type = type; 214df8bae1dSRodney W. Grimes object->size = size; 215b881da26SAlan Cox object->generation = 1; 216a1f6d91cSDavid Greenman object->ref_count = 1; 2173153e878SAlan Cox object->memattr = VM_MEMATTR_DEFAULT; 21824a1cce3SDavid Greenman object->flags = 0; 219ef694c1aSEdward Tomasz Napierala object->cred = NULL; 2203364c323SKonstantin Belousov object->charge = 0; 22160517fd1SJohn Baldwin if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP)) 2223471677cSAlan Cox object->flags = OBJ_ONEMAPPING; 2232446e4f0SAlan Cox object->pg_color = 0; 22424a1cce3SDavid Greenman object->handle = NULL; 22524a1cce3SDavid Greenman object->backing_object = NULL; 226a316d390SJohn Dyson object->backing_object_offset = (vm_ooffset_t) 0; 227f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 228f8a47341SAlan Cox LIST_INIT(&object->rvq); 229f8a47341SAlan Cox #endif 2307bfda801SAlan Cox object->cache = NULL; 231a1f6d91cSDavid Greenman 232a5698387SAlan Cox mtx_lock(&vm_object_list_mtx); 23360517fd1SJohn Baldwin TAILQ_INSERT_TAIL(&vm_object_list, object, object_list); 234a5698387SAlan Cox mtx_unlock(&vm_object_list_mtx); 235df8bae1dSRodney W. Grimes } 236df8bae1dSRodney W. Grimes 237df8bae1dSRodney W. Grimes /* 23826f9a767SRodney W. Grimes * vm_object_init: 23926f9a767SRodney W. Grimes * 24026f9a767SRodney W. Grimes * Initialize the VM objects module. 24126f9a767SRodney W. Grimes */ 24226f9a767SRodney W. Grimes void 2431b40f8c0SMatthew Dillon vm_object_init(void) 24426f9a767SRodney W. Grimes { 24526f9a767SRodney W. Grimes TAILQ_INIT(&vm_object_list); 2466008862bSJohn Baldwin mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF); 2470217125fSDavid Greenman 248c6c9025bSKonstantin Belousov VM_OBJECT_LOCK_INIT(kernel_object, "kernel object"); 2499f5c801bSAlan Cox _vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS), 25026f9a767SRodney W. Grimes kernel_object); 251f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 252f8a47341SAlan Cox kernel_object->flags |= OBJ_COLORED; 253f8a47341SAlan Cox kernel_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS); 254f8a47341SAlan Cox #endif 25526f9a767SRodney W. Grimes 256c6c9025bSKonstantin Belousov VM_OBJECT_LOCK_INIT(kmem_object, "kmem object"); 2579f5c801bSAlan Cox _vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS), 25826f9a767SRodney W. Grimes kmem_object); 259f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 260f8a47341SAlan Cox kmem_object->flags |= OBJ_COLORED; 261f8a47341SAlan Cox kmem_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS); 262f8a47341SAlan Cox #endif 263ed6a7863SAlan Cox 2648dbca793STor Egge /* 2658dbca793STor Egge * The lock portion of struct vm_object must be type stable due 2668dbca793STor Egge * to vm_pageout_fallback_object_lock locking a vm object 2678dbca793STor Egge * without holding any references to it. 2688dbca793STor Egge */ 2698355f576SJeff Roberson obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL, 2708355f576SJeff Roberson #ifdef INVARIANTS 2718355f576SJeff Roberson vm_object_zdtor, 2728355f576SJeff Roberson #else 2738355f576SJeff Roberson NULL, 2748355f576SJeff Roberson #endif 275f3c625e4SJeff Roberson vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM|UMA_ZONE_NOFREE); 27699448ed1SJohn Dyson } 27799448ed1SJohn Dyson 27899448ed1SJohn Dyson void 2791b40f8c0SMatthew Dillon vm_object_clear_flag(vm_object_t object, u_short bits) 2801b40f8c0SMatthew Dillon { 2815440b5a9SAlan Cox 282d647a0edSAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 283b06805adSJake Burkholder object->flags &= ~bits; 2841b40f8c0SMatthew Dillon } 2851b40f8c0SMatthew Dillon 2863153e878SAlan Cox /* 2873153e878SAlan Cox * Sets the default memory attribute for the specified object. Pages 2883153e878SAlan Cox * that are allocated to this object are by default assigned this memory 2893153e878SAlan Cox * attribute. 2903153e878SAlan Cox * 2913153e878SAlan Cox * Presently, this function must be called before any pages are allocated 2923153e878SAlan Cox * to the object. In the future, this requirement may be relaxed for 2933153e878SAlan Cox * "default" and "swap" objects. 2943153e878SAlan Cox */ 2953153e878SAlan Cox int 2963153e878SAlan Cox vm_object_set_memattr(vm_object_t object, vm_memattr_t memattr) 2973153e878SAlan Cox { 2983153e878SAlan Cox 2993153e878SAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 3003153e878SAlan Cox switch (object->type) { 3013153e878SAlan Cox case OBJT_DEFAULT: 3023153e878SAlan Cox case OBJT_DEVICE: 3033153e878SAlan Cox case OBJT_PHYS: 30401381811SJohn Baldwin case OBJT_SG: 3053153e878SAlan Cox case OBJT_SWAP: 3063153e878SAlan Cox case OBJT_VNODE: 3073153e878SAlan Cox if (!TAILQ_EMPTY(&object->memq)) 3083153e878SAlan Cox return (KERN_FAILURE); 3093153e878SAlan Cox break; 3103153e878SAlan Cox case OBJT_DEAD: 3113153e878SAlan Cox return (KERN_INVALID_ARGUMENT); 3123153e878SAlan Cox } 3133153e878SAlan Cox object->memattr = memattr; 3143153e878SAlan Cox return (KERN_SUCCESS); 3153153e878SAlan Cox } 3163153e878SAlan Cox 3171b40f8c0SMatthew Dillon void 3181b40f8c0SMatthew Dillon vm_object_pip_add(vm_object_t object, short i) 3191b40f8c0SMatthew Dillon { 320f279b88dSAlan Cox 321d647a0edSAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 322b06805adSJake Burkholder object->paging_in_progress += i; 3231b40f8c0SMatthew Dillon } 3241b40f8c0SMatthew Dillon 3251b40f8c0SMatthew Dillon void 3261b40f8c0SMatthew Dillon vm_object_pip_subtract(vm_object_t object, short i) 3271b40f8c0SMatthew Dillon { 328d647a0edSAlan Cox 3290fa05eaeSAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 330b06805adSJake Burkholder object->paging_in_progress -= i; 3311b40f8c0SMatthew Dillon } 3321b40f8c0SMatthew Dillon 3331b40f8c0SMatthew Dillon void 3341b40f8c0SMatthew Dillon vm_object_pip_wakeup(vm_object_t object) 3351b40f8c0SMatthew Dillon { 336f279b88dSAlan Cox 337d647a0edSAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 338b06805adSJake Burkholder object->paging_in_progress--; 3391b40f8c0SMatthew Dillon if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) { 3401b40f8c0SMatthew Dillon vm_object_clear_flag(object, OBJ_PIPWNT); 3411b40f8c0SMatthew Dillon wakeup(object); 3421b40f8c0SMatthew Dillon } 3431b40f8c0SMatthew Dillon } 3441b40f8c0SMatthew Dillon 3451b40f8c0SMatthew Dillon void 3461b40f8c0SMatthew Dillon vm_object_pip_wakeupn(vm_object_t object, short i) 3471b40f8c0SMatthew Dillon { 348d647a0edSAlan Cox 3490d420ad3SAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 3501b40f8c0SMatthew Dillon if (i) 351b06805adSJake Burkholder object->paging_in_progress -= i; 3521b40f8c0SMatthew Dillon if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) { 3531b40f8c0SMatthew Dillon vm_object_clear_flag(object, OBJ_PIPWNT); 3541b40f8c0SMatthew Dillon wakeup(object); 3551b40f8c0SMatthew Dillon } 3561b40f8c0SMatthew Dillon } 3571b40f8c0SMatthew Dillon 3581b40f8c0SMatthew Dillon void 3591b40f8c0SMatthew Dillon vm_object_pip_wait(vm_object_t object, char *waitid) 3601b40f8c0SMatthew Dillon { 3611ca58953SAlan Cox 3621ca58953SAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 3631ca58953SAlan Cox while (object->paging_in_progress) { 3641ca58953SAlan Cox object->flags |= OBJ_PIPWNT; 3651ca58953SAlan Cox msleep(object, VM_OBJECT_MTX(object), PVM, waitid, 0); 3661ca58953SAlan Cox } 3671b40f8c0SMatthew Dillon } 3681b40f8c0SMatthew Dillon 36926f9a767SRodney W. Grimes /* 37026f9a767SRodney W. Grimes * vm_object_allocate: 37126f9a767SRodney W. Grimes * 37226f9a767SRodney W. Grimes * Returns a new object with the given size. 37326f9a767SRodney W. Grimes */ 37426f9a767SRodney W. Grimes vm_object_t 3756395da54SIan Dowse vm_object_allocate(objtype_t type, vm_pindex_t size) 37626f9a767SRodney W. Grimes { 37790688d13SAlan Cox vm_object_t object; 37890688d13SAlan Cox 37990688d13SAlan Cox object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK); 38090688d13SAlan Cox _vm_object_allocate(type, size, object); 38190688d13SAlan Cox return (object); 38226f9a767SRodney W. Grimes } 38326f9a767SRodney W. Grimes 38426f9a767SRodney W. Grimes 38526f9a767SRodney W. Grimes /* 386df8bae1dSRodney W. Grimes * vm_object_reference: 387df8bae1dSRodney W. Grimes * 38815347817SAlan Cox * Gets another reference to the given object. Note: OBJ_DEAD 38915347817SAlan Cox * objects can be referenced during final cleaning. 390df8bae1dSRodney W. Grimes */ 3916476c0d2SJohn Dyson void 3921b40f8c0SMatthew Dillon vm_object_reference(vm_object_t object) 393df8bae1dSRodney W. Grimes { 394df8bae1dSRodney W. Grimes if (object == NULL) 395df8bae1dSRodney W. Grimes return; 39615347817SAlan Cox VM_OBJECT_LOCK(object); 39752481a9aSJeff Roberson vm_object_reference_locked(object); 39882f9defeSAlan Cox VM_OBJECT_UNLOCK(object); 39995e5e988SJohn Dyson } 40095e5e988SJohn Dyson 40123955314SAlfred Perlstein /* 402b921a12bSAlan Cox * vm_object_reference_locked: 403b921a12bSAlan Cox * 404b921a12bSAlan Cox * Gets another reference to the given object. 405b921a12bSAlan Cox * 406b921a12bSAlan Cox * The object must be locked. 407b921a12bSAlan Cox */ 408b921a12bSAlan Cox void 409b921a12bSAlan Cox vm_object_reference_locked(vm_object_t object) 410b921a12bSAlan Cox { 411b921a12bSAlan Cox struct vnode *vp; 412b921a12bSAlan Cox 413b921a12bSAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 414b921a12bSAlan Cox object->ref_count++; 415b921a12bSAlan Cox if (object->type == OBJT_VNODE) { 416b921a12bSAlan Cox vp = object->handle; 417b921a12bSAlan Cox vref(vp); 418b921a12bSAlan Cox } 419b921a12bSAlan Cox } 420b921a12bSAlan Cox 421b921a12bSAlan Cox /* 4229d5abbddSJens Schweikhardt * Handle deallocating an object of type OBJT_VNODE. 42323955314SAlfred Perlstein */ 42402dd8331SAlan Cox static void 4251b40f8c0SMatthew Dillon vm_object_vndeallocate(vm_object_t object) 42695e5e988SJohn Dyson { 42795e5e988SJohn Dyson struct vnode *vp = (struct vnode *) object->handle; 428219cbf59SEivind Eklund 429ae51ff11SJeff Roberson VFS_ASSERT_GIANT(vp->v_mount); 430ad682c48SAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 4315526d2d9SEivind Eklund KASSERT(object->type == OBJT_VNODE, 4325526d2d9SEivind Eklund ("vm_object_vndeallocate: not a vnode object")); 433219cbf59SEivind Eklund KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp")); 434219cbf59SEivind Eklund #ifdef INVARIANTS 43595e5e988SJohn Dyson if (object->ref_count == 0) { 43695e5e988SJohn Dyson vprint("vm_object_vndeallocate", vp); 43795e5e988SJohn Dyson panic("vm_object_vndeallocate: bad object reference count"); 43895e5e988SJohn Dyson } 43995e5e988SJohn Dyson #endif 44095e5e988SJohn Dyson 44103fa5b34SKonstantin Belousov if (object->ref_count > 1) { 44295e5e988SJohn Dyson object->ref_count--; 443ad682c48SAlan Cox VM_OBJECT_UNLOCK(object); 44403fa5b34SKonstantin Belousov /* vrele may need the vnode lock. */ 44547221757SJohn Dyson vrele(vp); 44603fa5b34SKonstantin Belousov } else { 44786769ac0SKonstantin Belousov vhold(vp); 44803fa5b34SKonstantin Belousov VM_OBJECT_UNLOCK(object); 44903fa5b34SKonstantin Belousov vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 45086769ac0SKonstantin Belousov vdrop(vp); 45103fa5b34SKonstantin Belousov VM_OBJECT_LOCK(object); 45203fa5b34SKonstantin Belousov object->ref_count--; 45386769ac0SKonstantin Belousov if (object->type == OBJT_DEAD) { 45486769ac0SKonstantin Belousov VM_OBJECT_UNLOCK(object); 45586769ac0SKonstantin Belousov VOP_UNLOCK(vp, 0); 45686769ac0SKonstantin Belousov } else { 45703fa5b34SKonstantin Belousov if (object->ref_count == 0) 45803fa5b34SKonstantin Belousov vp->v_vflag &= ~VV_TEXT; 45903fa5b34SKonstantin Belousov VM_OBJECT_UNLOCK(object); 46003fa5b34SKonstantin Belousov vput(vp); 46103fa5b34SKonstantin Belousov } 462df8bae1dSRodney W. Grimes } 46386769ac0SKonstantin Belousov } 464df8bae1dSRodney W. Grimes 465df8bae1dSRodney W. Grimes /* 466df8bae1dSRodney W. Grimes * vm_object_deallocate: 467df8bae1dSRodney W. Grimes * 468df8bae1dSRodney W. Grimes * Release a reference to the specified object, 469df8bae1dSRodney W. Grimes * gained either through a vm_object_allocate 470df8bae1dSRodney W. Grimes * or a vm_object_reference call. When all references 471df8bae1dSRodney W. Grimes * are gone, storage associated with this object 472df8bae1dSRodney W. Grimes * may be relinquished. 473df8bae1dSRodney W. Grimes * 474df8bae1dSRodney W. Grimes * No object may be locked. 475df8bae1dSRodney W. Grimes */ 47626f9a767SRodney W. Grimes void 4771b40f8c0SMatthew Dillon vm_object_deallocate(vm_object_t object) 478df8bae1dSRodney W. Grimes { 479df8bae1dSRodney W. Grimes vm_object_t temp; 480df8bae1dSRodney W. Grimes 481df8bae1dSRodney W. Grimes while (object != NULL) { 482ae51ff11SJeff Roberson int vfslocked; 483ca95b514SJohn Baldwin 484ae51ff11SJeff Roberson vfslocked = 0; 485ca95b514SJohn Baldwin restart: 486ad682c48SAlan Cox VM_OBJECT_LOCK(object); 4873b68228cSAlan Cox if (object->type == OBJT_VNODE) { 488ca95b514SJohn Baldwin struct vnode *vp = (struct vnode *) object->handle; 489ca95b514SJohn Baldwin 490ca95b514SJohn Baldwin /* 491ca95b514SJohn Baldwin * Conditionally acquire Giant for a vnode-backed 492ca95b514SJohn Baldwin * object. We have to be careful since the type of 493ca95b514SJohn Baldwin * a vnode object can change while the object is 494ca95b514SJohn Baldwin * unlocked. 495ca95b514SJohn Baldwin */ 496ca95b514SJohn Baldwin if (VFS_NEEDSGIANT(vp->v_mount) && !vfslocked) { 497ca95b514SJohn Baldwin vfslocked = 1; 498ca95b514SJohn Baldwin if (!mtx_trylock(&Giant)) { 499ca95b514SJohn Baldwin VM_OBJECT_UNLOCK(object); 500ca95b514SJohn Baldwin mtx_lock(&Giant); 501ca95b514SJohn Baldwin goto restart; 502ca95b514SJohn Baldwin } 503ca95b514SJohn Baldwin } 50495e5e988SJohn Dyson vm_object_vndeallocate(object); 505ae51ff11SJeff Roberson VFS_UNLOCK_GIANT(vfslocked); 50623b186d3SAlan Cox return; 507ca95b514SJohn Baldwin } else 508ca95b514SJohn Baldwin /* 509ca95b514SJohn Baldwin * This is to handle the case that the object 510ca95b514SJohn Baldwin * changed type while we dropped its lock to 511ca95b514SJohn Baldwin * obtain Giant. 512ca95b514SJohn Baldwin */ 513ca95b514SJohn Baldwin VFS_UNLOCK_GIANT(vfslocked); 51495e5e988SJohn Dyson 5158125b1e6SAlfred Perlstein KASSERT(object->ref_count != 0, 5168125b1e6SAlfred Perlstein ("vm_object_deallocate: object deallocated too many times: %d", object->type)); 5172be70f79SJohn Dyson 5182be70f79SJohn Dyson /* 5198125b1e6SAlfred Perlstein * If the reference count goes to 0 we start calling 5208125b1e6SAlfred Perlstein * vm_object_terminate() on the object chain. 5218125b1e6SAlfred Perlstein * A ref count of 1 may be a special case depending on the 5228125b1e6SAlfred Perlstein * shadow count being 0 or 1. 5232be70f79SJohn Dyson */ 524c0877f10SJohn Dyson object->ref_count--; 5258125b1e6SAlfred Perlstein if (object->ref_count > 1) { 5263b68228cSAlan Cox VM_OBJECT_UNLOCK(object); 52723b186d3SAlan Cox return; 5288125b1e6SAlfred Perlstein } else if (object->ref_count == 1) { 5294c8e0452SAlan Cox if (object->shadow_count == 0 && 5304c8e0452SAlan Cox object->handle == NULL && 5314c8e0452SAlan Cox (object->type == OBJT_DEFAULT || 5324c8e0452SAlan Cox object->type == OBJT_SWAP)) { 5338125b1e6SAlfred Perlstein vm_object_set_flag(object, OBJ_ONEMAPPING); 5348125b1e6SAlfred Perlstein } else if ((object->shadow_count == 1) && 5358125b1e6SAlfred Perlstein (object->handle == NULL) && 53624a1cce3SDavid Greenman (object->type == OBJT_DEFAULT || 53724a1cce3SDavid Greenman object->type == OBJT_SWAP)) { 538a1f6d91cSDavid Greenman vm_object_t robject; 53995e5e988SJohn Dyson 5401c500307SAlan Cox robject = LIST_FIRST(&object->shadow_head); 5415526d2d9SEivind Eklund KASSERT(robject != NULL, 542219cbf59SEivind Eklund ("vm_object_deallocate: ref_count: %d, shadow_count: %d", 5435526d2d9SEivind Eklund object->ref_count, 5445526d2d9SEivind Eklund object->shadow_count)); 545b72b0115SAlan Cox if (!VM_OBJECT_TRYLOCK(robject)) { 546b72b0115SAlan Cox /* 547b72b0115SAlan Cox * Avoid a potential deadlock. 548b72b0115SAlan Cox */ 549b72b0115SAlan Cox object->ref_count++; 550b72b0115SAlan Cox VM_OBJECT_UNLOCK(object); 551a7d86121SAlan Cox /* 552a7d86121SAlan Cox * More likely than not the thread 553a7d86121SAlan Cox * holding robject's lock has lower 554a7d86121SAlan Cox * priority than the current thread. 555a7d86121SAlan Cox * Let the lower priority thread run. 556a7d86121SAlan Cox */ 5578db5fc58SJohn Baldwin pause("vmo_de", 1); 558b72b0115SAlan Cox continue; 559b72b0115SAlan Cox } 560d936694fSAlan Cox /* 561d936694fSAlan Cox * Collapse object into its shadow unless its 562d936694fSAlan Cox * shadow is dead. In that case, object will 563d936694fSAlan Cox * be deallocated by the thread that is 564d936694fSAlan Cox * deallocating its shadow. 565d936694fSAlan Cox */ 566d936694fSAlan Cox if ((robject->flags & OBJ_DEAD) == 0 && 567d936694fSAlan Cox (robject->handle == NULL) && 56824a1cce3SDavid Greenman (robject->type == OBJT_DEFAULT || 56924a1cce3SDavid Greenman robject->type == OBJT_SWAP)) { 570a1f6d91cSDavid Greenman 57195e5e988SJohn Dyson robject->ref_count++; 572138449dcSAlan Cox retry: 573138449dcSAlan Cox if (robject->paging_in_progress) { 574138449dcSAlan Cox VM_OBJECT_UNLOCK(object); 575138449dcSAlan Cox vm_object_pip_wait(robject, 576138449dcSAlan Cox "objde1"); 5772e9f4a69SAlan Cox temp = robject->backing_object; 5782e9f4a69SAlan Cox if (object == temp) { 579138449dcSAlan Cox VM_OBJECT_LOCK(object); 580138449dcSAlan Cox goto retry; 5812e9f4a69SAlan Cox } 582138449dcSAlan Cox } else if (object->paging_in_progress) { 583138449dcSAlan Cox VM_OBJECT_UNLOCK(robject); 584138449dcSAlan Cox object->flags |= OBJ_PIPWNT; 585138449dcSAlan Cox msleep(object, 586138449dcSAlan Cox VM_OBJECT_MTX(object), 587138449dcSAlan Cox PDROP | PVM, "objde2", 0); 588138449dcSAlan Cox VM_OBJECT_LOCK(robject); 5892e9f4a69SAlan Cox temp = robject->backing_object; 5902e9f4a69SAlan Cox if (object == temp) { 591138449dcSAlan Cox VM_OBJECT_LOCK(object); 592138449dcSAlan Cox goto retry; 593a1f6d91cSDavid Greenman } 5942e9f4a69SAlan Cox } else 5953b68228cSAlan Cox VM_OBJECT_UNLOCK(object); 5962e9f4a69SAlan Cox 59795e5e988SJohn Dyson if (robject->ref_count == 1) { 59895e5e988SJohn Dyson robject->ref_count--; 599ba8da839SDavid Greenman object = robject; 60095e5e988SJohn Dyson goto doterm; 60195e5e988SJohn Dyson } 60295e5e988SJohn Dyson object = robject; 60395e5e988SJohn Dyson vm_object_collapse(object); 604d7fc2210SAlan Cox VM_OBJECT_UNLOCK(object); 605ba8da839SDavid Greenman continue; 606a1f6d91cSDavid Greenman } 607b72b0115SAlan Cox VM_OBJECT_UNLOCK(robject); 60895e5e988SJohn Dyson } 6093b68228cSAlan Cox VM_OBJECT_UNLOCK(object); 61023b186d3SAlan Cox return; 61195e5e988SJohn Dyson } 61295e5e988SJohn Dyson doterm: 61324a1cce3SDavid Greenman temp = object->backing_object; 614c9917419SAlan Cox if (temp != NULL) { 615c9917419SAlan Cox VM_OBJECT_LOCK(temp); 6161c500307SAlan Cox LIST_REMOVE(object, shadow_list); 61795e5e988SJohn Dyson temp->shadow_count--; 618c9917419SAlan Cox VM_OBJECT_UNLOCK(temp); 61995461b45SJohn Dyson object->backing_object = NULL; 620de5f6a77SJohn Dyson } 621245df27cSMatthew Dillon /* 622245df27cSMatthew Dillon * Don't double-terminate, we could be in a termination 623245df27cSMatthew Dillon * recursion due to the terminate having to sync data 624245df27cSMatthew Dillon * to disk. 625245df27cSMatthew Dillon */ 626245df27cSMatthew Dillon if ((object->flags & OBJ_DEAD) == 0) 627df8bae1dSRodney W. Grimes vm_object_terminate(object); 628c829b9d0SAlan Cox else 629c829b9d0SAlan Cox VM_OBJECT_UNLOCK(object); 630df8bae1dSRodney W. Grimes object = temp; 631df8bae1dSRodney W. Grimes } 632df8bae1dSRodney W. Grimes } 633df8bae1dSRodney W. Grimes 634df8bae1dSRodney W. Grimes /* 6352ac78f0eSStephan Uphoff * vm_object_destroy removes the object from the global object list 6362ac78f0eSStephan Uphoff * and frees the space for the object. 6372ac78f0eSStephan Uphoff */ 6382ac78f0eSStephan Uphoff void 6392ac78f0eSStephan Uphoff vm_object_destroy(vm_object_t object) 6402ac78f0eSStephan Uphoff { 6412ac78f0eSStephan Uphoff 6422ac78f0eSStephan Uphoff /* 6432ac78f0eSStephan Uphoff * Remove the object from the global object list. 6442ac78f0eSStephan Uphoff */ 6452ac78f0eSStephan Uphoff mtx_lock(&vm_object_list_mtx); 6462ac78f0eSStephan Uphoff TAILQ_REMOVE(&vm_object_list, object, object_list); 6472ac78f0eSStephan Uphoff mtx_unlock(&vm_object_list_mtx); 6482ac78f0eSStephan Uphoff 6492ac78f0eSStephan Uphoff /* 6503364c323SKonstantin Belousov * Release the allocation charge. 6513364c323SKonstantin Belousov */ 652ef694c1aSEdward Tomasz Napierala if (object->cred != NULL) { 6533364c323SKonstantin Belousov KASSERT(object->type == OBJT_DEFAULT || 6543364c323SKonstantin Belousov object->type == OBJT_SWAP, 655ef694c1aSEdward Tomasz Napierala ("vm_object_terminate: non-swap obj %p has cred", 6563364c323SKonstantin Belousov object)); 657ef694c1aSEdward Tomasz Napierala swap_release_by_cred(object->charge, object->cred); 6583364c323SKonstantin Belousov object->charge = 0; 659ef694c1aSEdward Tomasz Napierala crfree(object->cred); 660ef694c1aSEdward Tomasz Napierala object->cred = NULL; 6613364c323SKonstantin Belousov } 6623364c323SKonstantin Belousov 6633364c323SKonstantin Belousov /* 6642ac78f0eSStephan Uphoff * Free the space for the object. 6652ac78f0eSStephan Uphoff */ 6662ac78f0eSStephan Uphoff uma_zfree(obj_zone, object); 6672ac78f0eSStephan Uphoff } 6682ac78f0eSStephan Uphoff 6692ac78f0eSStephan Uphoff /* 670df8bae1dSRodney W. Grimes * vm_object_terminate actually destroys the specified object, freeing 671df8bae1dSRodney W. Grimes * up all previously used resources. 672df8bae1dSRodney W. Grimes * 673df8bae1dSRodney W. Grimes * The object must be locked. 6741c7c3c6aSMatthew Dillon * This routine may block. 675df8bae1dSRodney W. Grimes */ 67695e5e988SJohn Dyson void 6771b40f8c0SMatthew Dillon vm_object_terminate(vm_object_t object) 678df8bae1dSRodney W. Grimes { 67917ea6f00SAlan Cox vm_page_t p, p_next; 680df8bae1dSRodney W. Grimes 681c829b9d0SAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 6820cddd8f0SMatthew Dillon 68395e5e988SJohn Dyson /* 68495e5e988SJohn Dyson * Make sure no one uses us. 68595e5e988SJohn Dyson */ 686069e9bc1SDoug Rabson vm_object_set_flag(object, OBJ_DEAD); 6873c631446SJohn Dyson 688df8bae1dSRodney W. Grimes /* 689f6b04d2bSDavid Greenman * wait for the pageout daemon to be done with the object 690df8bae1dSRodney W. Grimes */ 69166095752SJohn Dyson vm_object_pip_wait(object, "objtrm"); 692df8bae1dSRodney W. Grimes 6935526d2d9SEivind Eklund KASSERT(!object->paging_in_progress, 6945526d2d9SEivind Eklund ("vm_object_terminate: pageout in progress")); 69526f9a767SRodney W. Grimes 69626f9a767SRodney W. Grimes /* 6970d94caffSDavid Greenman * Clean and free the pages, as appropriate. All references to the 6980d94caffSDavid Greenman * object are gone, so we don't need to lock it. 69926f9a767SRodney W. Grimes */ 70024a1cce3SDavid Greenman if (object->type == OBJT_VNODE) { 701f7dd7b63SAlan Cox struct vnode *vp = (struct vnode *)object->handle; 70295e5e988SJohn Dyson 70395e5e988SJohn Dyson /* 70495e5e988SJohn Dyson * Clean pages and flush buffers. 70595e5e988SJohn Dyson */ 7068f9110f6SJohn Dyson vm_object_page_clean(object, 0, 0, OBJPC_SYNC); 707b6e48e03SAlan Cox VM_OBJECT_UNLOCK(object); 70895e5e988SJohn Dyson 7090d7935fdSAttilio Rao vinvalbuf(vp, V_SAVE, 0, 0); 710f7dd7b63SAlan Cox 711f7dd7b63SAlan Cox VM_OBJECT_LOCK(object); 712bef608bdSJohn Dyson } 713bef608bdSJohn Dyson 714971dd342SAlfred Perlstein KASSERT(object->ref_count == 0, 715971dd342SAlfred Perlstein ("vm_object_terminate: object with references, ref_count=%d", 716971dd342SAlfred Perlstein object->ref_count)); 717996c772fSJohn Dyson 7180d94caffSDavid Greenman /* 71917ea6f00SAlan Cox * Free any remaining pageable pages. This also removes them from the 72017ea6f00SAlan Cox * paging queues. However, don't free wired pages, just remove them 72117ea6f00SAlan Cox * from the object. Rather than incrementally removing each page from 72217ea6f00SAlan Cox * the object, the page and object are reset to any empty state. 723df8bae1dSRodney W. Grimes */ 72417ea6f00SAlan Cox TAILQ_FOREACH_SAFE(p, &object->memq, listq, p_next) { 7259af80719SAlan Cox KASSERT(!p->busy && (p->oflags & VPO_BUSY) == 0, 72617ea6f00SAlan Cox ("vm_object_terminate: freeing busy page %p", p)); 7272965a453SKip Macy vm_page_lock(p); 72817ea6f00SAlan Cox /* 72917ea6f00SAlan Cox * Optimize the page's removal from the object by resetting 73017ea6f00SAlan Cox * its "object" field. Specifically, if the page is not 73117ea6f00SAlan Cox * wired, then the effect of this assignment is that 73217ea6f00SAlan Cox * vm_page_free()'s call to vm_page_remove() will return 73317ea6f00SAlan Cox * immediately without modifying the page or the object. 73417ea6f00SAlan Cox */ 73517ea6f00SAlan Cox p->object = NULL; 7360b10ba98SDavid Greenman if (p->wire_count == 0) { 737df8bae1dSRodney W. Grimes vm_page_free(p); 73870721880SAlan Cox PCPU_INC(cnt.v_pfree); 73917ea6f00SAlan Cox } 7402965a453SKip Macy vm_page_unlock(p); 7412965a453SKip Macy } 74217ea6f00SAlan Cox /* 74317ea6f00SAlan Cox * If the object contained any pages, then reset it to an empty state. 74417ea6f00SAlan Cox * None of the object's fields, including "resident_page_count", were 74517ea6f00SAlan Cox * modified by the preceding loop. 74617ea6f00SAlan Cox */ 74717ea6f00SAlan Cox if (object->resident_page_count != 0) { 74817ea6f00SAlan Cox object->root = NULL; 74917ea6f00SAlan Cox TAILQ_INIT(&object->memq); 75017ea6f00SAlan Cox object->resident_page_count = 0; 75117ea6f00SAlan Cox if (object->type == OBJT_VNODE) 75217ea6f00SAlan Cox vdrop(object->handle); 75317ea6f00SAlan Cox } 754bef608bdSJohn Dyson 755f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 756f8a47341SAlan Cox if (__predict_false(!LIST_EMPTY(&object->rvq))) 757f8a47341SAlan Cox vm_reserv_break_all(object); 758f8a47341SAlan Cox #endif 7597bfda801SAlan Cox if (__predict_false(object->cache != NULL)) 760c9444914SAlan Cox vm_page_cache_free(object, 0, 0); 7617bfda801SAlan Cox 7622d8acc0fSJohn Dyson /* 7639fcfb650SDavid Greenman * Let the pager know object is dead. 7649fcfb650SDavid Greenman */ 7659fcfb650SDavid Greenman vm_pager_deallocate(object); 766658ad5ffSAlan Cox VM_OBJECT_UNLOCK(object); 7679fcfb650SDavid Greenman 7682ac78f0eSStephan Uphoff vm_object_destroy(object); 76947221757SJohn Dyson } 770df8bae1dSRodney W. Grimes 771edf93b25SAlan Cox /* 772edf93b25SAlan Cox * Make the page read-only so that we can clear the object flags. However, if 773edf93b25SAlan Cox * this is a nosync mmap then the object is likely to stay dirty so do not 774edf93b25SAlan Cox * mess with the page and do not clear the object flags. Returns TRUE if the 775edf93b25SAlan Cox * page should be flushed, and FALSE otherwise. 776edf93b25SAlan Cox */ 7773280870dSKonstantin Belousov static boolean_t 7783280870dSKonstantin Belousov vm_object_page_remove_write(vm_page_t p, int flags, int *clearobjflags) 7793280870dSKonstantin Belousov { 7803280870dSKonstantin Belousov 7813280870dSKonstantin Belousov /* 7823280870dSKonstantin Belousov * If we have been asked to skip nosync pages and this is a 7833280870dSKonstantin Belousov * nosync page, skip it. Note that the object flags were not 7843280870dSKonstantin Belousov * cleared in this case so we do not have to set them. 7853280870dSKonstantin Belousov */ 7863280870dSKonstantin Belousov if ((flags & OBJPC_NOSYNC) != 0 && (p->oflags & VPO_NOSYNC) != 0) { 7873280870dSKonstantin Belousov *clearobjflags = 0; 7883280870dSKonstantin Belousov return (FALSE); 7893280870dSKonstantin Belousov } else { 7903280870dSKonstantin Belousov pmap_remove_write(p); 7913280870dSKonstantin Belousov return (p->dirty != 0); 7923280870dSKonstantin Belousov } 7933280870dSKonstantin Belousov } 7943280870dSKonstantin Belousov 795df8bae1dSRodney W. Grimes /* 796df8bae1dSRodney W. Grimes * vm_object_page_clean 797df8bae1dSRodney W. Grimes * 7984f79d873SMatthew Dillon * Clean all dirty pages in the specified range of object. Leaves page 7994f79d873SMatthew Dillon * on whatever queue it is currently on. If NOSYNC is set then do not 800b146f9e5SAlan Cox * write out pages with VPO_NOSYNC set (originally comes from MAP_NOSYNC), 8014f79d873SMatthew Dillon * leaving the object dirty. 80226f9a767SRodney W. Grimes * 80343b7990eSMatthew Dillon * When stuffing pages asynchronously, allow clustering. XXX we need a 80443b7990eSMatthew Dillon * synchronous clustering mode implementation. 80543b7990eSMatthew Dillon * 80626f9a767SRodney W. Grimes * Odd semantics: if start == end, we clean everything. 80726f9a767SRodney W. Grimes * 80826f9a767SRodney W. Grimes * The object must be locked. 80926f9a767SRodney W. Grimes */ 810f6b04d2bSDavid Greenman void 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; 816e239bb97SKonstantin Belousov int clearobjflags, curgeneration, n, pagerflags; 817f6b04d2bSDavid Greenman 8187bec141bSKip Macy mtx_assert(&vm_page_queue_mtx, MA_NOTOWNED); 819b6e48e03SAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 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) 823f6b04d2bSDavid Greenman return; 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; 832f6b04d2bSDavid Greenman 833bd7e5f99SJohn Dyson rescan: 8342d8acc0fSJohn Dyson curgeneration = object->generation; 8352d8acc0fSJohn Dyson 83617f3095dSAlan Cox for (p = vm_page_find_least(object, tstart); p != NULL; p = np) { 837bd7e5f99SJohn Dyson pi = p->pindex; 838e239bb97SKonstantin Belousov if (pi >= tend) 839e239bb97SKonstantin Belousov break; 840e239bb97SKonstantin Belousov np = TAILQ_NEXT(p, listq); 841e239bb97SKonstantin Belousov if (p->valid == 0) 842aef922f5SJohn Dyson continue; 843780636b7SKonstantin Belousov if (vm_page_sleep_if_busy(p, TRUE, "vpcwai")) { 844e239bb97SKonstantin Belousov if (object->generation != curgeneration) 845e239bb97SKonstantin Belousov goto rescan; 846780636b7SKonstantin Belousov np = vm_page_find_least(object, pi); 847780636b7SKonstantin Belousov continue; 848f6b04d2bSDavid Greenman } 8493280870dSKonstantin Belousov if (!vm_object_page_remove_write(p, flags, &clearobjflags)) 850bd7e5f99SJohn Dyson continue; 851e239bb97SKonstantin Belousov 8523280870dSKonstantin Belousov n = vm_object_page_collect_flush(object, p, pagerflags, 8533280870dSKonstantin Belousov flags, &clearobjflags); 8547bec141bSKip Macy if (object->generation != curgeneration) 855b9b7a4beSMatthew Dillon goto rescan; 856031ec8c1SKonstantin Belousov 857031ec8c1SKonstantin Belousov /* 858031ec8c1SKonstantin Belousov * If the VOP_PUTPAGES() did a truncated write, so 859031ec8c1SKonstantin Belousov * that even the first page of the run is not fully 860031ec8c1SKonstantin Belousov * written, vm_pageout_flush() returns 0 as the run 861031ec8c1SKonstantin Belousov * length. Since the condition that caused truncated 862031ec8c1SKonstantin Belousov * write may be permanent, e.g. exhausted free space, 863031ec8c1SKonstantin Belousov * accepting n == 0 would cause an infinite loop. 864031ec8c1SKonstantin Belousov * 865031ec8c1SKonstantin Belousov * Forwarding the iterator leaves the unwritten page 866031ec8c1SKonstantin Belousov * behind, but there is not much we can do there if 867031ec8c1SKonstantin Belousov * filesystem refuses to write it. 868031ec8c1SKonstantin Belousov */ 869031ec8c1SKonstantin Belousov if (n == 0) 870031ec8c1SKonstantin Belousov n = 1; 871e239bb97SKonstantin Belousov np = vm_page_find_least(object, pi + n); 872b9b7a4beSMatthew Dillon } 873b9b7a4beSMatthew Dillon #if 0 874e239bb97SKonstantin Belousov VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC) ? MNT_WAIT : 0); 875b9b7a4beSMatthew Dillon #endif 876b9b7a4beSMatthew Dillon 877edf93b25SAlan Cox if (clearobjflags) 8783280870dSKonstantin Belousov vm_object_clear_flag(object, OBJ_MIGHTBEDIRTY); 879b9b7a4beSMatthew Dillon } 880b9b7a4beSMatthew Dillon 881b9b7a4beSMatthew Dillon static int 8823280870dSKonstantin Belousov vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int pagerflags, 8833280870dSKonstantin Belousov int flags, int *clearobjflags) 884b9b7a4beSMatthew Dillon { 8853157c503SKonstantin Belousov vm_page_t ma[vm_pageout_page_count], p_first, tp; 8863157c503SKonstantin Belousov int count, i, mreq, runlen; 887b9b7a4beSMatthew Dillon 8887bec141bSKip Macy mtx_assert(&vm_page_queue_mtx, MA_NOTOWNED); 8897bec141bSKip Macy vm_page_lock_assert(p, MA_NOTOWNED); 8907bec141bSKip Macy VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 8913157c503SKonstantin Belousov 8923157c503SKonstantin Belousov count = 1; 8933157c503SKonstantin Belousov mreq = 0; 8943157c503SKonstantin Belousov 8953157c503SKonstantin Belousov for (tp = p; count < vm_pageout_page_count; count++) { 8963157c503SKonstantin Belousov tp = vm_page_next(tp); 897e239bb97SKonstantin Belousov if (tp == NULL || tp->busy != 0 || (tp->oflags & VPO_BUSY) != 0) 898bd7e5f99SJohn Dyson break; 8993280870dSKonstantin Belousov if (!vm_object_page_remove_write(tp, flags, clearobjflags)) 900bd7e5f99SJohn Dyson break; 901bd7e5f99SJohn Dyson } 902aef922f5SJohn Dyson 9033157c503SKonstantin Belousov for (p_first = p; count < vm_pageout_page_count; count++) { 9043157c503SKonstantin Belousov tp = vm_page_prev(p_first); 905e239bb97SKonstantin Belousov if (tp == NULL || tp->busy != 0 || (tp->oflags & VPO_BUSY) != 0) 906bd7e5f99SJohn Dyson break; 9073280870dSKonstantin Belousov if (!vm_object_page_remove_write(tp, flags, clearobjflags)) 908bd7e5f99SJohn Dyson break; 9093157c503SKonstantin Belousov p_first = tp; 9103157c503SKonstantin Belousov mreq++; 911bd7e5f99SJohn Dyson } 912bd7e5f99SJohn Dyson 9133157c503SKonstantin Belousov for (tp = p_first, i = 0; i < count; tp = TAILQ_NEXT(tp, listq), i++) 9143157c503SKonstantin Belousov ma[i] = tp; 915cf2819ccSJohn Dyson 9163157c503SKonstantin Belousov vm_pageout_flush(ma, count, pagerflags, mreq, &runlen); 9171e8a675cSKonstantin Belousov return (runlen); 91826f9a767SRodney W. Grimes } 919df8bae1dSRodney W. Grimes 9201efb74fbSJohn Dyson /* 921950f8459SAlan Cox * Note that there is absolutely no sense in writing out 922950f8459SAlan Cox * anonymous objects, so we track down the vnode object 923950f8459SAlan Cox * to write out. 924950f8459SAlan Cox * We invalidate (remove) all pages from the address space 925950f8459SAlan Cox * for semantic correctness. 926950f8459SAlan Cox * 9276bbee8e2SAlan Cox * If the backing object is a device object with unmanaged pages, then any 9286bbee8e2SAlan Cox * mappings to the specified range of pages must be removed before this 9296bbee8e2SAlan Cox * function is called. 9306bbee8e2SAlan Cox * 931950f8459SAlan Cox * Note: certain anonymous maps, such as MAP_NOSYNC maps, 932950f8459SAlan Cox * may start out with a NULL object. 933950f8459SAlan Cox */ 934950f8459SAlan Cox void 935950f8459SAlan Cox vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size, 936950f8459SAlan Cox boolean_t syncio, boolean_t invalidate) 937950f8459SAlan Cox { 938950f8459SAlan Cox vm_object_t backing_object; 939950f8459SAlan Cox struct vnode *vp; 9403b582b4eSTor Egge struct mount *mp; 941950f8459SAlan Cox int flags; 942950f8459SAlan Cox 943950f8459SAlan Cox if (object == NULL) 944950f8459SAlan Cox return; 945950f8459SAlan Cox VM_OBJECT_LOCK(object); 946950f8459SAlan Cox while ((backing_object = object->backing_object) != NULL) { 947950f8459SAlan Cox VM_OBJECT_LOCK(backing_object); 94856e0670fSAlan Cox offset += object->backing_object_offset; 949950f8459SAlan Cox VM_OBJECT_UNLOCK(object); 950950f8459SAlan Cox object = backing_object; 951950f8459SAlan Cox if (object->size < OFF_TO_IDX(offset + size)) 952950f8459SAlan Cox size = IDX_TO_OFF(object->size) - offset; 953950f8459SAlan Cox } 954950f8459SAlan Cox /* 955950f8459SAlan Cox * Flush pages if writing is allowed, invalidate them 956950f8459SAlan Cox * if invalidation requested. Pages undergoing I/O 957950f8459SAlan Cox * will be ignored by vm_object_page_remove(). 958950f8459SAlan Cox * 959950f8459SAlan Cox * We cannot lock the vnode and then wait for paging 960950f8459SAlan Cox * to complete without deadlocking against vm_fault. 961950f8459SAlan Cox * Instead we simply call vm_object_page_remove() and 962950f8459SAlan Cox * allow it to block internally on a page-by-page 963950f8459SAlan Cox * basis when it encounters pages undergoing async 964950f8459SAlan Cox * I/O. 965950f8459SAlan Cox */ 966950f8459SAlan Cox if (object->type == OBJT_VNODE && 967950f8459SAlan Cox (object->flags & OBJ_MIGHTBEDIRTY) != 0) { 968ae51ff11SJeff Roberson int vfslocked; 969950f8459SAlan Cox vp = object->handle; 970950f8459SAlan Cox VM_OBJECT_UNLOCK(object); 9713b582b4eSTor Egge (void) vn_start_write(vp, &mp, V_WAIT); 972ae51ff11SJeff Roberson vfslocked = VFS_LOCK_GIANT(vp->v_mount); 973cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 974950f8459SAlan Cox flags = (syncio || invalidate) ? OBJPC_SYNC : 0; 975950f8459SAlan Cox flags |= invalidate ? OBJPC_INVAL : 0; 976950f8459SAlan Cox VM_OBJECT_LOCK(object); 97717f3095dSAlan Cox vm_object_page_clean(object, offset, offset + size, flags); 978950f8459SAlan Cox VM_OBJECT_UNLOCK(object); 97922db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 980ae51ff11SJeff Roberson VFS_UNLOCK_GIANT(vfslocked); 9813b582b4eSTor Egge vn_finished_write(mp); 982950f8459SAlan Cox VM_OBJECT_LOCK(object); 983950f8459SAlan Cox } 984950f8459SAlan Cox if ((object->type == OBJT_VNODE || 985950f8459SAlan Cox object->type == OBJT_DEVICE) && invalidate) { 9866bbee8e2SAlan Cox if (object->type == OBJT_DEVICE) 9876bbee8e2SAlan Cox /* 9886bbee8e2SAlan Cox * The option OBJPR_NOTMAPPED must be passed here 9896bbee8e2SAlan Cox * because vm_object_page_remove() cannot remove 9906bbee8e2SAlan Cox * unmanaged mappings. 9916bbee8e2SAlan Cox */ 9926bbee8e2SAlan Cox flags = OBJPR_NOTMAPPED; 9936bbee8e2SAlan Cox else if (old_msync) 9946bbee8e2SAlan Cox flags = 0; 9956bbee8e2SAlan Cox else 9966bbee8e2SAlan Cox flags = OBJPR_CLEANONLY; 9976bbee8e2SAlan Cox vm_object_page_remove(object, OFF_TO_IDX(offset), 9986bbee8e2SAlan Cox OFF_TO_IDX(offset + size + PAGE_MASK), flags); 999950f8459SAlan Cox } 1000950f8459SAlan Cox VM_OBJECT_UNLOCK(object); 1001950f8459SAlan Cox } 1002950f8459SAlan Cox 1003950f8459SAlan Cox /* 1004867a482dSJohn Dyson * vm_object_madvise: 1005867a482dSJohn Dyson * 1006867a482dSJohn Dyson * Implements the madvise function at the object/page level. 10071c7c3c6aSMatthew Dillon * 1008193b9358SAlan Cox * MADV_WILLNEED (any object) 1009193b9358SAlan Cox * 1010193b9358SAlan Cox * Activate the specified pages if they are resident. 1011193b9358SAlan Cox * 1012193b9358SAlan Cox * MADV_DONTNEED (any object) 1013193b9358SAlan Cox * 1014193b9358SAlan Cox * Deactivate the specified pages if they are resident. 1015193b9358SAlan Cox * 1016193b9358SAlan Cox * MADV_FREE (OBJT_DEFAULT/OBJT_SWAP objects, 1017193b9358SAlan Cox * OBJ_ONEMAPPING only) 1018193b9358SAlan Cox * 1019193b9358SAlan Cox * Deactivate and clean the specified pages if they are 1020193b9358SAlan Cox * resident. This permits the process to reuse the pages 1021193b9358SAlan Cox * without faulting or the kernel to reclaim the pages 1022193b9358SAlan Cox * without I/O. 1023867a482dSJohn Dyson */ 1024867a482dSJohn Dyson void 10251b40f8c0SMatthew Dillon vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise) 1026867a482dSJohn Dyson { 10276e20a165SJohn Dyson vm_pindex_t end, tpindex; 102834567de7SAlan Cox vm_object_t backing_object, tobject; 1029867a482dSJohn Dyson vm_page_t m; 1030867a482dSJohn Dyson 1031867a482dSJohn Dyson if (object == NULL) 1032867a482dSJohn Dyson return; 10339b98b796SAlan Cox VM_OBJECT_LOCK(object); 1034867a482dSJohn Dyson end = pindex + count; 10351c7c3c6aSMatthew Dillon /* 10361c7c3c6aSMatthew Dillon * Locate and adjust resident pages 10371c7c3c6aSMatthew Dillon */ 10381c7c3c6aSMatthew Dillon for (; pindex < end; pindex += 1) { 10396e20a165SJohn Dyson relookup: 10406e20a165SJohn Dyson tobject = object; 10416e20a165SJohn Dyson tpindex = pindex; 10426e20a165SJohn Dyson shadowlookup: 104358b4e6ccSAlan Cox /* 104458b4e6ccSAlan Cox * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages 104558b4e6ccSAlan Cox * and those pages must be OBJ_ONEMAPPING. 104658b4e6ccSAlan Cox */ 104758b4e6ccSAlan Cox if (advise == MADV_FREE) { 104858b4e6ccSAlan Cox if ((tobject->type != OBJT_DEFAULT && 104958b4e6ccSAlan Cox tobject->type != OBJT_SWAP) || 105058b4e6ccSAlan Cox (tobject->flags & OBJ_ONEMAPPING) == 0) { 105134567de7SAlan Cox goto unlock_tobject; 10526e20a165SJohn Dyson } 10536a2a3d73SAlan Cox } else if (tobject->type == OBJT_PHYS) 10546a2a3d73SAlan Cox goto unlock_tobject; 10551c7c3c6aSMatthew Dillon m = vm_page_lookup(tobject, tpindex); 10567bfda801SAlan Cox if (m == NULL && advise == MADV_WILLNEED) { 10577bfda801SAlan Cox /* 10587bfda801SAlan Cox * If the page is cached, reactivate it. 10597bfda801SAlan Cox */ 1060f3a2ed4bSAlan Cox m = vm_page_alloc(tobject, tpindex, VM_ALLOC_IFCACHED | 1061f3a2ed4bSAlan Cox VM_ALLOC_NOBUSY); 10627bfda801SAlan Cox } 10631c7c3c6aSMatthew Dillon if (m == NULL) { 10641ce137beSMatthew Dillon /* 10651ce137beSMatthew Dillon * There may be swap even if there is no backing page 10661ce137beSMatthew Dillon */ 10671ce137beSMatthew Dillon if (advise == MADV_FREE && tobject->type == OBJT_SWAP) 10681ce137beSMatthew Dillon swap_pager_freespace(tobject, tpindex, 1); 10691ce137beSMatthew Dillon /* 10701ce137beSMatthew Dillon * next object 10711ce137beSMatthew Dillon */ 107234567de7SAlan Cox backing_object = tobject->backing_object; 107334567de7SAlan Cox if (backing_object == NULL) 107434567de7SAlan Cox goto unlock_tobject; 107534567de7SAlan Cox VM_OBJECT_LOCK(backing_object); 107656e0670fSAlan Cox tpindex += OFF_TO_IDX(tobject->backing_object_offset); 10779b98b796SAlan Cox if (tobject != object) 107834567de7SAlan Cox VM_OBJECT_UNLOCK(tobject); 107934567de7SAlan Cox tobject = backing_object; 10806e20a165SJohn Dyson goto shadowlookup; 10816a2a3d73SAlan Cox } else if (m->valid != VM_PAGE_BITS_ALL) 10826a2a3d73SAlan Cox goto unlock_tobject; 1083867a482dSJohn Dyson /* 10846a2a3d73SAlan Cox * If the page is not in a normal state, skip it. 1085867a482dSJohn Dyson */ 10862965a453SKip Macy vm_page_lock(m); 10876a2a3d73SAlan Cox if (m->hold_count != 0 || m->wire_count != 0) { 10882965a453SKip Macy vm_page_unlock(m); 108934567de7SAlan Cox goto unlock_tobject; 10906e20a165SJohn Dyson } 1091d98d0ce2SKonstantin Belousov KASSERT((m->flags & PG_FICTITIOUS) == 0, 1092d98d0ce2SKonstantin Belousov ("vm_object_madvise: page %p is fictitious", m)); 1093d98d0ce2SKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) == 0, 1094567e51e1SAlan Cox ("vm_object_madvise: page %p is not managed", m)); 10959af80719SAlan Cox if ((m->oflags & VPO_BUSY) || m->busy) { 1096567e51e1SAlan Cox if (advise == MADV_WILLNEED) { 1097b11b56b5SAlan Cox /* 1098b11b56b5SAlan Cox * Reference the page before unlocking and 1099b11b56b5SAlan Cox * sleeping so that the page daemon is less 1100b11b56b5SAlan Cox * likely to reclaim it. 1101b11b56b5SAlan Cox */ 11023407fefeSKonstantin Belousov vm_page_aflag_set(m, PGA_REFERENCED); 1103567e51e1SAlan Cox } 11042965a453SKip Macy vm_page_unlock(m); 11059b98b796SAlan Cox if (object != tobject) 11069b98b796SAlan Cox VM_OBJECT_UNLOCK(object); 11075786be7cSAlan Cox m->oflags |= VPO_WANTED; 1108b11b56b5SAlan Cox msleep(m, VM_OBJECT_MTX(tobject), PDROP | PVM, "madvpo", 1109b11b56b5SAlan Cox 0); 11109b98b796SAlan Cox VM_OBJECT_LOCK(object); 11116e20a165SJohn Dyson goto relookup; 111234567de7SAlan Cox } 1113867a482dSJohn Dyson if (advise == MADV_WILLNEED) { 1114867a482dSJohn Dyson vm_page_activate(m); 11156e20a165SJohn Dyson } else if (advise == MADV_DONTNEED) { 1116479112dfSMatthew Dillon vm_page_dontneed(m); 11170a47b48bSJohn Dyson } else if (advise == MADV_FREE) { 11181c7c3c6aSMatthew Dillon /* 11192aaeadf8SMatthew Dillon * Mark the page clean. This will allow the page 11202aaeadf8SMatthew Dillon * to be freed up by the system. However, such pages 11212aaeadf8SMatthew Dillon * are often reused quickly by malloc()/free() 11222aaeadf8SMatthew Dillon * so we do not do anything that would cause 11232aaeadf8SMatthew Dillon * a page fault if we can help it. 11242aaeadf8SMatthew Dillon * 11252aaeadf8SMatthew Dillon * Specifically, we do not try to actually free 11262aaeadf8SMatthew Dillon * the page now nor do we try to put it in the 11272aaeadf8SMatthew Dillon * cache (which would cause a page fault on reuse). 112841c67e12SMatthew Dillon * 112941c67e12SMatthew Dillon * But we do make the page is freeable as we 113041c67e12SMatthew Dillon * can without actually taking the step of unmapping 113141c67e12SMatthew Dillon * it. 11321c7c3c6aSMatthew Dillon */ 11330385347cSPeter Wemm pmap_clear_modify(m); 11346e20a165SJohn Dyson m->dirty = 0; 113541c67e12SMatthew Dillon m->act_count = 0; 1136479112dfSMatthew Dillon vm_page_dontneed(m); 1137867a482dSJohn Dyson } 11382965a453SKip Macy vm_page_unlock(m); 11392999e9faSAlan Cox if (advise == MADV_FREE && tobject->type == OBJT_SWAP) 11402999e9faSAlan Cox swap_pager_freespace(tobject, tpindex, 1); 114134567de7SAlan Cox unlock_tobject: 11429b98b796SAlan Cox if (tobject != object) 114334567de7SAlan Cox VM_OBJECT_UNLOCK(tobject); 1144867a482dSJohn Dyson } 11459b98b796SAlan Cox VM_OBJECT_UNLOCK(object); 1146867a482dSJohn Dyson } 1147867a482dSJohn Dyson 1148867a482dSJohn Dyson /* 1149df8bae1dSRodney W. Grimes * vm_object_shadow: 1150df8bae1dSRodney W. Grimes * 1151df8bae1dSRodney W. Grimes * Create a new object which is backed by the 1152df8bae1dSRodney W. Grimes * specified existing object range. The source 1153df8bae1dSRodney W. Grimes * object reference is deallocated. 1154df8bae1dSRodney W. Grimes * 1155df8bae1dSRodney W. Grimes * The new object and offset into that object 1156df8bae1dSRodney W. Grimes * are returned in the source parameters. 1157df8bae1dSRodney W. Grimes */ 115826f9a767SRodney W. Grimes void 11591b40f8c0SMatthew Dillon vm_object_shadow( 11601b40f8c0SMatthew Dillon vm_object_t *object, /* IN/OUT */ 11611b40f8c0SMatthew Dillon vm_ooffset_t *offset, /* IN/OUT */ 11621b40f8c0SMatthew Dillon vm_size_t length) 1163df8bae1dSRodney W. Grimes { 1164d031cff1SMatthew Dillon vm_object_t source; 1165d031cff1SMatthew Dillon vm_object_t result; 1166df8bae1dSRodney W. Grimes 1167df8bae1dSRodney W. Grimes source = *object; 1168df8bae1dSRodney W. Grimes 1169df8bae1dSRodney W. Grimes /* 11709a2f6362SAlan Cox * Don't create the new object if the old object isn't shared. 11719a2f6362SAlan Cox */ 1172570a2f4aSAlan Cox if (source != NULL) { 1173570a2f4aSAlan Cox VM_OBJECT_LOCK(source); 1174570a2f4aSAlan Cox if (source->ref_count == 1 && 11759a2f6362SAlan Cox source->handle == NULL && 11769a2f6362SAlan Cox (source->type == OBJT_DEFAULT || 11779917e010SAlan Cox source->type == OBJT_SWAP)) { 1178570a2f4aSAlan Cox VM_OBJECT_UNLOCK(source); 11799a2f6362SAlan Cox return; 11809917e010SAlan Cox } 1181570a2f4aSAlan Cox VM_OBJECT_UNLOCK(source); 1182570a2f4aSAlan Cox } 11839a2f6362SAlan Cox 11849a2f6362SAlan Cox /* 1185570a2f4aSAlan Cox * Allocate a new object with the given length. 1186df8bae1dSRodney W. Grimes */ 11870cc74f14SAlan Cox result = vm_object_allocate(OBJT_DEFAULT, atop(length)); 1188df8bae1dSRodney W. Grimes 1189df8bae1dSRodney W. Grimes /* 11900d94caffSDavid Greenman * The new object shadows the source object, adding a reference to it. 11910d94caffSDavid Greenman * Our caller changes his reference to point to the new object, 11920d94caffSDavid Greenman * removing a reference to the source object. Net result: no change 11930d94caffSDavid Greenman * of reference count. 11949b09fe24SMatthew Dillon * 11959b09fe24SMatthew Dillon * Try to optimize the result object's page color when shadowing 1196956f3135SPhilippe Charnier * in order to maintain page coloring consistency in the combined 11979b09fe24SMatthew Dillon * shadowed object. 1198df8bae1dSRodney W. Grimes */ 119924a1cce3SDavid Greenman result->backing_object = source; 12009174ca7bSTor Egge /* 12019174ca7bSTor Egge * Store the offset into the source object, and fix up the offset into 12029174ca7bSTor Egge * the new object. 12039174ca7bSTor Egge */ 12049174ca7bSTor Egge result->backing_object_offset = *offset; 1205570a2f4aSAlan Cox if (source != NULL) { 1206570a2f4aSAlan Cox VM_OBJECT_LOCK(source); 12071c500307SAlan Cox LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list); 1208eaf13dd7SJohn Dyson source->shadow_count++; 1209f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 12107b54b1a9SAlan Cox result->flags |= source->flags & OBJ_COLORED; 1211f8a47341SAlan Cox result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) & 1212f8a47341SAlan Cox ((1 << (VM_NFREEORDER - 1)) - 1); 1213f8a47341SAlan Cox #endif 1214570a2f4aSAlan Cox VM_OBJECT_UNLOCK(source); 1215de5f6a77SJohn Dyson } 1216df8bae1dSRodney W. Grimes 1217df8bae1dSRodney W. Grimes 1218df8bae1dSRodney W. Grimes /* 1219df8bae1dSRodney W. Grimes * Return the new things 1220df8bae1dSRodney W. Grimes */ 1221df8bae1dSRodney W. Grimes *offset = 0; 1222df8bae1dSRodney W. Grimes *object = result; 1223df8bae1dSRodney W. Grimes } 1224df8bae1dSRodney W. Grimes 1225c5aaa06dSAlan Cox /* 1226c5aaa06dSAlan Cox * vm_object_split: 1227c5aaa06dSAlan Cox * 1228c5aaa06dSAlan Cox * Split the pages in a map entry into a new object. This affords 1229c5aaa06dSAlan Cox * easier removal of unused pages, and keeps object inheritance from 1230c5aaa06dSAlan Cox * being a negative impact on memory usage. 1231c5aaa06dSAlan Cox */ 1232c5aaa06dSAlan Cox void 1233c5aaa06dSAlan Cox vm_object_split(vm_map_entry_t entry) 1234c5aaa06dSAlan Cox { 123573000556SAlan Cox vm_page_t m, m_next; 1236c5aaa06dSAlan Cox vm_object_t orig_object, new_object, source; 123773000556SAlan Cox vm_pindex_t idx, offidxstart; 123873000556SAlan Cox vm_size_t size; 1239c5aaa06dSAlan Cox 1240c5aaa06dSAlan Cox orig_object = entry->object.vm_object; 1241c5aaa06dSAlan Cox if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP) 1242c5aaa06dSAlan Cox return; 1243c5aaa06dSAlan Cox if (orig_object->ref_count <= 1) 1244c5aaa06dSAlan Cox return; 12454da9f125SAlan Cox VM_OBJECT_UNLOCK(orig_object); 1246c5aaa06dSAlan Cox 12474da9f125SAlan Cox offidxstart = OFF_TO_IDX(entry->offset); 124895442adfSAlan Cox size = atop(entry->end - entry->start); 1249c5aaa06dSAlan Cox 12504da9f125SAlan Cox /* 12514da9f125SAlan Cox * If swap_pager_copy() is later called, it will convert new_object 12524da9f125SAlan Cox * into a swap object. 12534da9f125SAlan Cox */ 12544da9f125SAlan Cox new_object = vm_object_allocate(OBJT_DEFAULT, size); 1255c5aaa06dSAlan Cox 1256c5474b8fSAlan Cox /* 1257c5474b8fSAlan Cox * At this point, the new object is still private, so the order in 1258c5474b8fSAlan Cox * which the original and new objects are locked does not matter. 1259c5474b8fSAlan Cox */ 126063f6cefcSAlan Cox VM_OBJECT_LOCK(new_object); 126163f6cefcSAlan Cox VM_OBJECT_LOCK(orig_object); 1262c5aaa06dSAlan Cox source = orig_object->backing_object; 1263c5aaa06dSAlan Cox if (source != NULL) { 12648e3a76fbSAlan Cox VM_OBJECT_LOCK(source); 126519c244d0SAlan Cox if ((source->flags & OBJ_DEAD) != 0) { 126619c244d0SAlan Cox VM_OBJECT_UNLOCK(source); 126719c244d0SAlan Cox VM_OBJECT_UNLOCK(orig_object); 126819c244d0SAlan Cox VM_OBJECT_UNLOCK(new_object); 126919c244d0SAlan Cox vm_object_deallocate(new_object); 127019c244d0SAlan Cox VM_OBJECT_LOCK(orig_object); 127119c244d0SAlan Cox return; 127219c244d0SAlan Cox } 12731c500307SAlan Cox LIST_INSERT_HEAD(&source->shadow_head, 1274c5aaa06dSAlan Cox new_object, shadow_list); 12758e3a76fbSAlan Cox source->shadow_count++; 1276b921a12bSAlan Cox vm_object_reference_locked(source); /* for new_object */ 1277c5aaa06dSAlan Cox vm_object_clear_flag(source, OBJ_ONEMAPPING); 1278e2479b4fSAlan Cox VM_OBJECT_UNLOCK(source); 1279c5aaa06dSAlan Cox new_object->backing_object_offset = 12804da9f125SAlan Cox orig_object->backing_object_offset + entry->offset; 1281c5aaa06dSAlan Cox new_object->backing_object = source; 1282c5aaa06dSAlan Cox } 1283ef694c1aSEdward Tomasz Napierala if (orig_object->cred != NULL) { 1284ef694c1aSEdward Tomasz Napierala new_object->cred = orig_object->cred; 1285ef694c1aSEdward Tomasz Napierala crhold(orig_object->cred); 12863364c323SKonstantin Belousov new_object->charge = ptoa(size); 12873364c323SKonstantin Belousov KASSERT(orig_object->charge >= ptoa(size), 12883364c323SKonstantin Belousov ("orig_object->charge < 0")); 12893364c323SKonstantin Belousov orig_object->charge -= ptoa(size); 12903364c323SKonstantin Belousov } 1291c5aaa06dSAlan Cox retry: 1292b382c10aSKonstantin Belousov m = vm_page_find_least(orig_object, offidxstart); 129373000556SAlan Cox for (; m != NULL && (idx = m->pindex - offidxstart) < size; 129473000556SAlan Cox m = m_next) { 129573000556SAlan Cox m_next = TAILQ_NEXT(m, listq); 1296c5aaa06dSAlan Cox 1297c5aaa06dSAlan Cox /* 1298c5aaa06dSAlan Cox * We must wait for pending I/O to complete before we can 1299c5aaa06dSAlan Cox * rename the page. 1300c5aaa06dSAlan Cox * 1301c5aaa06dSAlan Cox * We do not have to VM_PROT_NONE the page as mappings should 1302c5aaa06dSAlan Cox * not be changed by this operation. 1303c5aaa06dSAlan Cox */ 13049af80719SAlan Cox if ((m->oflags & VPO_BUSY) || m->busy) { 1305de33beddSAlan Cox VM_OBJECT_UNLOCK(new_object); 13065786be7cSAlan Cox m->oflags |= VPO_WANTED; 1307c5474b8fSAlan Cox msleep(m, VM_OBJECT_MTX(orig_object), PVM, "spltwt", 0); 1308de33beddSAlan Cox VM_OBJECT_LOCK(new_object); 1309c5aaa06dSAlan Cox goto retry; 1310de33beddSAlan Cox } 13112965a453SKip Macy vm_page_lock(m); 1312c5aaa06dSAlan Cox vm_page_rename(m, new_object, idx); 13132965a453SKip Macy vm_page_unlock(m); 1314c5aaa06dSAlan Cox /* page automatically made dirty by rename and cache handled */ 1315c5aaa06dSAlan Cox vm_page_busy(m); 1316c5aaa06dSAlan Cox } 1317d7a013c3SAlan Cox if (orig_object->type == OBJT_SWAP) { 1318c5aaa06dSAlan Cox /* 1319c7c8dd7eSAlan Cox * swap_pager_copy() can sleep, in which case the orig_object's 1320c7c8dd7eSAlan Cox * and new_object's locks are released and reacquired. 1321c5aaa06dSAlan Cox */ 1322c5aaa06dSAlan Cox swap_pager_copy(orig_object, new_object, offidxstart, 0); 13237bfda801SAlan Cox 13247bfda801SAlan Cox /* 13257bfda801SAlan Cox * Transfer any cached pages from orig_object to new_object. 13267bfda801SAlan Cox */ 13277bfda801SAlan Cox if (__predict_false(orig_object->cache != NULL)) 13287bfda801SAlan Cox vm_page_cache_transfer(orig_object, offidxstart, 13297bfda801SAlan Cox new_object); 1330c5aaa06dSAlan Cox } 1331d7a013c3SAlan Cox VM_OBJECT_UNLOCK(orig_object); 1332c7118ed6SAlan Cox TAILQ_FOREACH(m, &new_object->memq, listq) 1333c5aaa06dSAlan Cox vm_page_wakeup(m); 1334c7c8dd7eSAlan Cox VM_OBJECT_UNLOCK(new_object); 1335c5aaa06dSAlan Cox entry->object.vm_object = new_object; 1336c5aaa06dSAlan Cox entry->offset = 0LL; 1337c5aaa06dSAlan Cox vm_object_deallocate(orig_object); 13384da9f125SAlan Cox VM_OBJECT_LOCK(new_object); 1339c5aaa06dSAlan Cox } 1340c5aaa06dSAlan Cox 13412ad1a3f7SMatthew Dillon #define OBSC_TEST_ALL_SHADOWED 0x0001 13422ad1a3f7SMatthew Dillon #define OBSC_COLLAPSE_NOWAIT 0x0002 13432ad1a3f7SMatthew Dillon #define OBSC_COLLAPSE_WAIT 0x0004 13442ad1a3f7SMatthew Dillon 1345b4ae4780SPoul-Henning Kamp static int 13462ad1a3f7SMatthew Dillon vm_object_backing_scan(vm_object_t object, int op) 13472ad1a3f7SMatthew Dillon { 13482ad1a3f7SMatthew Dillon int r = 1; 13492ad1a3f7SMatthew Dillon vm_page_t p; 13502ad1a3f7SMatthew Dillon vm_object_t backing_object; 13512ad1a3f7SMatthew Dillon vm_pindex_t backing_offset_index; 13522ad1a3f7SMatthew Dillon 13537ca33ad1SAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 13547ca33ad1SAlan Cox VM_OBJECT_LOCK_ASSERT(object->backing_object, MA_OWNED); 13552ad1a3f7SMatthew Dillon 13562ad1a3f7SMatthew Dillon backing_object = object->backing_object; 13572ad1a3f7SMatthew Dillon backing_offset_index = OFF_TO_IDX(object->backing_object_offset); 13582ad1a3f7SMatthew Dillon 13592ad1a3f7SMatthew Dillon /* 13602ad1a3f7SMatthew Dillon * Initial conditions 13612ad1a3f7SMatthew Dillon */ 13622ad1a3f7SMatthew Dillon if (op & OBSC_TEST_ALL_SHADOWED) { 13632ad1a3f7SMatthew Dillon /* 13647bfda801SAlan Cox * We do not want to have to test for the existence of cache 13657bfda801SAlan Cox * or swap pages in the backing object. XXX but with the 13662ad1a3f7SMatthew Dillon * new swapper this would be pretty easy to do. 13672ad1a3f7SMatthew Dillon * 13682ad1a3f7SMatthew Dillon * XXX what about anonymous MAP_SHARED memory that hasn't 13692ad1a3f7SMatthew Dillon * been ZFOD faulted yet? If we do not test for this, the 13702ad1a3f7SMatthew Dillon * shadow test may succeed! XXX 13712ad1a3f7SMatthew Dillon */ 13722ad1a3f7SMatthew Dillon if (backing_object->type != OBJT_DEFAULT) { 13732ad1a3f7SMatthew Dillon return (0); 13742ad1a3f7SMatthew Dillon } 13752ad1a3f7SMatthew Dillon } 13762ad1a3f7SMatthew Dillon if (op & OBSC_COLLAPSE_WAIT) { 13772ad1a3f7SMatthew Dillon vm_object_set_flag(backing_object, OBJ_DEAD); 13782ad1a3f7SMatthew Dillon } 13792ad1a3f7SMatthew Dillon 13802ad1a3f7SMatthew Dillon /* 13812ad1a3f7SMatthew Dillon * Our scan 13822ad1a3f7SMatthew Dillon */ 13832ad1a3f7SMatthew Dillon p = TAILQ_FIRST(&backing_object->memq); 13842ad1a3f7SMatthew Dillon while (p) { 13852ad1a3f7SMatthew Dillon vm_page_t next = TAILQ_NEXT(p, listq); 13862ad1a3f7SMatthew Dillon vm_pindex_t new_pindex = p->pindex - backing_offset_index; 13872ad1a3f7SMatthew Dillon 13882ad1a3f7SMatthew Dillon if (op & OBSC_TEST_ALL_SHADOWED) { 13892ad1a3f7SMatthew Dillon vm_page_t pp; 13902ad1a3f7SMatthew Dillon 13912ad1a3f7SMatthew Dillon /* 13922ad1a3f7SMatthew Dillon * Ignore pages outside the parent object's range 13932ad1a3f7SMatthew Dillon * and outside the parent object's mapping of the 13942ad1a3f7SMatthew Dillon * backing object. 13952ad1a3f7SMatthew Dillon * 13962ad1a3f7SMatthew Dillon * note that we do not busy the backing object's 13972ad1a3f7SMatthew Dillon * page. 13982ad1a3f7SMatthew Dillon */ 13992ad1a3f7SMatthew Dillon if ( 14002ad1a3f7SMatthew Dillon p->pindex < backing_offset_index || 14012ad1a3f7SMatthew Dillon new_pindex >= object->size 14022ad1a3f7SMatthew Dillon ) { 14032ad1a3f7SMatthew Dillon p = next; 14042ad1a3f7SMatthew Dillon continue; 14052ad1a3f7SMatthew Dillon } 14062ad1a3f7SMatthew Dillon 14072ad1a3f7SMatthew Dillon /* 14082ad1a3f7SMatthew Dillon * See if the parent has the page or if the parent's 14092ad1a3f7SMatthew Dillon * object pager has the page. If the parent has the 14102ad1a3f7SMatthew Dillon * page but the page is not valid, the parent's 14112ad1a3f7SMatthew Dillon * object pager must have the page. 14122ad1a3f7SMatthew Dillon * 14132ad1a3f7SMatthew Dillon * If this fails, the parent does not completely shadow 14142ad1a3f7SMatthew Dillon * the object and we might as well give up now. 14152ad1a3f7SMatthew Dillon */ 14162ad1a3f7SMatthew Dillon 14172ad1a3f7SMatthew Dillon pp = vm_page_lookup(object, new_pindex); 14182ad1a3f7SMatthew Dillon if ( 14192ad1a3f7SMatthew Dillon (pp == NULL || pp->valid == 0) && 14202ad1a3f7SMatthew Dillon !vm_pager_has_page(object, new_pindex, NULL, NULL) 14212ad1a3f7SMatthew Dillon ) { 14222ad1a3f7SMatthew Dillon r = 0; 14232ad1a3f7SMatthew Dillon break; 14242ad1a3f7SMatthew Dillon } 14252ad1a3f7SMatthew Dillon } 14262ad1a3f7SMatthew Dillon 14272ad1a3f7SMatthew Dillon /* 14282ad1a3f7SMatthew Dillon * Check for busy page 14292ad1a3f7SMatthew Dillon */ 14302ad1a3f7SMatthew Dillon if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) { 14312ad1a3f7SMatthew Dillon vm_page_t pp; 14322ad1a3f7SMatthew Dillon 14332ad1a3f7SMatthew Dillon if (op & OBSC_COLLAPSE_NOWAIT) { 14349af80719SAlan Cox if ((p->oflags & VPO_BUSY) || 14352ad1a3f7SMatthew Dillon !p->valid || 143600f9e8b4SAlan Cox p->busy) { 14372ad1a3f7SMatthew Dillon p = next; 14382ad1a3f7SMatthew Dillon continue; 14392ad1a3f7SMatthew Dillon } 14402ad1a3f7SMatthew Dillon } else if (op & OBSC_COLLAPSE_WAIT) { 14419af80719SAlan Cox if ((p->oflags & VPO_BUSY) || p->busy) { 14427ca33ad1SAlan Cox VM_OBJECT_UNLOCK(object); 14435786be7cSAlan Cox p->oflags |= VPO_WANTED; 144491449ce9SAlan Cox msleep(p, VM_OBJECT_MTX(backing_object), 14457ca33ad1SAlan Cox PDROP | PVM, "vmocol", 0); 14467ca33ad1SAlan Cox VM_OBJECT_LOCK(object); 14477ca33ad1SAlan Cox VM_OBJECT_LOCK(backing_object); 14482ad1a3f7SMatthew Dillon /* 14492ad1a3f7SMatthew Dillon * If we slept, anything could have 14502ad1a3f7SMatthew Dillon * happened. Since the object is 14512ad1a3f7SMatthew Dillon * marked dead, the backing offset 14522ad1a3f7SMatthew Dillon * should not have changed so we 14532ad1a3f7SMatthew Dillon * just restart our scan. 14542ad1a3f7SMatthew Dillon */ 14552ad1a3f7SMatthew Dillon p = TAILQ_FIRST(&backing_object->memq); 14562ad1a3f7SMatthew Dillon continue; 14572ad1a3f7SMatthew Dillon } 14582ad1a3f7SMatthew Dillon } 14592ad1a3f7SMatthew Dillon 14602ad1a3f7SMatthew Dillon KASSERT( 14612ad1a3f7SMatthew Dillon p->object == backing_object, 14628e99783bSAlan Cox ("vm_object_backing_scan: object mismatch") 14632ad1a3f7SMatthew Dillon ); 14642ad1a3f7SMatthew Dillon 14652ad1a3f7SMatthew Dillon /* 14662ad1a3f7SMatthew Dillon * Destroy any associated swap 14672ad1a3f7SMatthew Dillon */ 14682ad1a3f7SMatthew Dillon if (backing_object->type == OBJT_SWAP) { 14692ad1a3f7SMatthew Dillon swap_pager_freespace( 14702ad1a3f7SMatthew Dillon backing_object, 14712ad1a3f7SMatthew Dillon p->pindex, 14722ad1a3f7SMatthew Dillon 1 14732ad1a3f7SMatthew Dillon ); 14742ad1a3f7SMatthew Dillon } 14752ad1a3f7SMatthew Dillon 14762ad1a3f7SMatthew Dillon if ( 14772ad1a3f7SMatthew Dillon p->pindex < backing_offset_index || 14782ad1a3f7SMatthew Dillon new_pindex >= object->size 14792ad1a3f7SMatthew Dillon ) { 14802ad1a3f7SMatthew Dillon /* 14812ad1a3f7SMatthew Dillon * Page is out of the parent object's range, we 14822ad1a3f7SMatthew Dillon * can simply destroy it. 14832ad1a3f7SMatthew Dillon */ 14842965a453SKip Macy vm_page_lock(p); 1485f6d89838SAlan Cox KASSERT(!pmap_page_is_mapped(p), 1486f6d89838SAlan Cox ("freeing mapped page %p", p)); 1487f6d89838SAlan Cox if (p->wire_count == 0) 14882ad1a3f7SMatthew Dillon vm_page_free(p); 1489f6d89838SAlan Cox else 1490f6d89838SAlan Cox vm_page_remove(p); 14912965a453SKip Macy vm_page_unlock(p); 14922ad1a3f7SMatthew Dillon p = next; 14932ad1a3f7SMatthew Dillon continue; 14942ad1a3f7SMatthew Dillon } 14952ad1a3f7SMatthew Dillon 14962ad1a3f7SMatthew Dillon pp = vm_page_lookup(object, new_pindex); 14972ad1a3f7SMatthew Dillon if ( 1498e18cc7bfSMax Laier (op & OBSC_COLLAPSE_NOWAIT) != 0 && 1499e18cc7bfSMax Laier (pp != NULL && pp->valid == 0) 1500e18cc7bfSMax Laier ) { 1501e18cc7bfSMax Laier /* 1502e18cc7bfSMax Laier * The page in the parent is not (yet) valid. 1503e18cc7bfSMax Laier * We don't know anything about the state of 1504e18cc7bfSMax Laier * the original page. It might be mapped, 1505e18cc7bfSMax Laier * so we must avoid the next if here. 1506e18cc7bfSMax Laier * 1507e18cc7bfSMax Laier * This is due to a race in vm_fault() where 1508e18cc7bfSMax Laier * we must unbusy the original (backing_obj) 1509e18cc7bfSMax Laier * page before we can (re)lock the parent. 1510e18cc7bfSMax Laier * Hence we can get here. 1511e18cc7bfSMax Laier */ 1512e18cc7bfSMax Laier p = next; 1513e18cc7bfSMax Laier continue; 1514e18cc7bfSMax Laier } 1515e18cc7bfSMax Laier if ( 15162ad1a3f7SMatthew Dillon pp != NULL || 15172ad1a3f7SMatthew Dillon vm_pager_has_page(object, new_pindex, NULL, NULL) 15182ad1a3f7SMatthew Dillon ) { 15192ad1a3f7SMatthew Dillon /* 15202ad1a3f7SMatthew Dillon * page already exists in parent OR swap exists 15212ad1a3f7SMatthew Dillon * for this location in the parent. Destroy 15222ad1a3f7SMatthew Dillon * the original page from the backing object. 15232ad1a3f7SMatthew Dillon * 15242ad1a3f7SMatthew Dillon * Leave the parent's page alone 15252ad1a3f7SMatthew Dillon */ 15262965a453SKip Macy vm_page_lock(p); 1527f6d89838SAlan Cox KASSERT(!pmap_page_is_mapped(p), 1528f6d89838SAlan Cox ("freeing mapped page %p", p)); 1529f6d89838SAlan Cox if (p->wire_count == 0) 15302ad1a3f7SMatthew Dillon vm_page_free(p); 1531f6d89838SAlan Cox else 1532f6d89838SAlan Cox vm_page_remove(p); 15332965a453SKip Macy vm_page_unlock(p); 15342ad1a3f7SMatthew Dillon p = next; 15352ad1a3f7SMatthew Dillon continue; 15362ad1a3f7SMatthew Dillon } 15372ad1a3f7SMatthew Dillon 1538f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 1539f8a47341SAlan Cox /* 1540f8a47341SAlan Cox * Rename the reservation. 1541f8a47341SAlan Cox */ 1542f8a47341SAlan Cox vm_reserv_rename(p, object, backing_object, 1543f8a47341SAlan Cox backing_offset_index); 1544f8a47341SAlan Cox #endif 1545f8a47341SAlan Cox 15462ad1a3f7SMatthew Dillon /* 15472ad1a3f7SMatthew Dillon * Page does not exist in parent, rename the 15482ad1a3f7SMatthew Dillon * page from the backing object to the main object. 1549d1bf5d56SMatthew Dillon * 1550d1bf5d56SMatthew Dillon * If the page was mapped to a process, it can remain 1551d1bf5d56SMatthew Dillon * mapped through the rename. 15522ad1a3f7SMatthew Dillon */ 15532965a453SKip Macy vm_page_lock(p); 15542ad1a3f7SMatthew Dillon vm_page_rename(p, object, new_pindex); 15552965a453SKip Macy vm_page_unlock(p); 15562ad1a3f7SMatthew Dillon /* page automatically made dirty by rename */ 15572ad1a3f7SMatthew Dillon } 15582ad1a3f7SMatthew Dillon p = next; 15592ad1a3f7SMatthew Dillon } 15602ad1a3f7SMatthew Dillon return (r); 15612ad1a3f7SMatthew Dillon } 15622ad1a3f7SMatthew Dillon 1563df8bae1dSRodney W. Grimes 1564df8bae1dSRodney W. Grimes /* 15652fe6e4d7SDavid Greenman * this version of collapse allows the operation to occur earlier and 15662fe6e4d7SDavid Greenman * when paging_in_progress is true for an object... This is not a complete 15672fe6e4d7SDavid Greenman * operation, but should plug 99.9% of the rest of the leaks. 15682fe6e4d7SDavid Greenman */ 15692fe6e4d7SDavid Greenman static void 15701b40f8c0SMatthew Dillon vm_object_qcollapse(vm_object_t object) 15712fe6e4d7SDavid Greenman { 15722ad1a3f7SMatthew Dillon vm_object_t backing_object = object->backing_object; 15732fe6e4d7SDavid Greenman 157406ecade7SAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 157506ecade7SAlan Cox VM_OBJECT_LOCK_ASSERT(backing_object, MA_OWNED); 15761b40f8c0SMatthew Dillon 15772fe6e4d7SDavid Greenman if (backing_object->ref_count != 1) 15782fe6e4d7SDavid Greenman return; 15792fe6e4d7SDavid Greenman 15802ad1a3f7SMatthew Dillon vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT); 15812fe6e4d7SDavid Greenman } 15822fe6e4d7SDavid Greenman 1583df8bae1dSRodney W. Grimes /* 1584df8bae1dSRodney W. Grimes * vm_object_collapse: 1585df8bae1dSRodney W. Grimes * 1586df8bae1dSRodney W. Grimes * Collapse an object with the object backing it. 1587df8bae1dSRodney W. Grimes * Pages in the backing object are moved into the 1588df8bae1dSRodney W. Grimes * parent, and the backing object is deallocated. 1589df8bae1dSRodney W. Grimes */ 159026f9a767SRodney W. Grimes void 15911b40f8c0SMatthew Dillon vm_object_collapse(vm_object_t object) 1592df8bae1dSRodney W. Grimes { 1593d7fc2210SAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 159423955314SAlfred Perlstein 1595df8bae1dSRodney W. Grimes while (TRUE) { 15962ad1a3f7SMatthew Dillon vm_object_t backing_object; 15972ad1a3f7SMatthew Dillon 1598df8bae1dSRodney W. Grimes /* 1599df8bae1dSRodney W. Grimes * Verify that the conditions are right for collapse: 1600df8bae1dSRodney W. Grimes * 16012ad1a3f7SMatthew Dillon * The object exists and the backing object exists. 1602df8bae1dSRodney W. Grimes */ 160324a1cce3SDavid Greenman if ((backing_object = object->backing_object) == NULL) 16042ad1a3f7SMatthew Dillon break; 1605df8bae1dSRodney W. Grimes 1606f919ebdeSDavid Greenman /* 1607f919ebdeSDavid Greenman * we check the backing object first, because it is most likely 160824a1cce3SDavid Greenman * not collapsable. 1609f919ebdeSDavid Greenman */ 161040b808a8SAlan Cox VM_OBJECT_LOCK(backing_object); 161124a1cce3SDavid Greenman if (backing_object->handle != NULL || 161224a1cce3SDavid Greenman (backing_object->type != OBJT_DEFAULT && 161324a1cce3SDavid Greenman backing_object->type != OBJT_SWAP) || 1614f919ebdeSDavid Greenman (backing_object->flags & OBJ_DEAD) || 161524a1cce3SDavid Greenman object->handle != NULL || 161624a1cce3SDavid Greenman (object->type != OBJT_DEFAULT && 161724a1cce3SDavid Greenman object->type != OBJT_SWAP) || 161824a1cce3SDavid Greenman (object->flags & OBJ_DEAD)) { 161940b808a8SAlan Cox VM_OBJECT_UNLOCK(backing_object); 16202ad1a3f7SMatthew Dillon break; 162124a1cce3SDavid Greenman } 16229b4814bbSDavid Greenman 16232ad1a3f7SMatthew Dillon if ( 16242ad1a3f7SMatthew Dillon object->paging_in_progress != 0 || 16252ad1a3f7SMatthew Dillon backing_object->paging_in_progress != 0 16262ad1a3f7SMatthew Dillon ) { 1627b9921222SDavid Greenman vm_object_qcollapse(object); 162840b808a8SAlan Cox VM_OBJECT_UNLOCK(backing_object); 16292ad1a3f7SMatthew Dillon break; 1630df8bae1dSRodney W. Grimes } 163126f9a767SRodney W. Grimes /* 16320d94caffSDavid Greenman * We know that we can either collapse the backing object (if 16332ad1a3f7SMatthew Dillon * the parent is the only reference to it) or (perhaps) have 16342ad1a3f7SMatthew Dillon * the parent bypass the object if the parent happens to shadow 16352ad1a3f7SMatthew Dillon * all the resident pages in the entire backing object. 16362ad1a3f7SMatthew Dillon * 16372ad1a3f7SMatthew Dillon * This is ignoring pager-backed pages such as swap pages. 16382ad1a3f7SMatthew Dillon * vm_object_backing_scan fails the shadowing test in this 16392ad1a3f7SMatthew Dillon * case. 1640df8bae1dSRodney W. Grimes */ 1641df8bae1dSRodney W. Grimes if (backing_object->ref_count == 1) { 1642df8bae1dSRodney W. Grimes /* 16432ad1a3f7SMatthew Dillon * If there is exactly one reference to the backing 16442ad1a3f7SMatthew Dillon * object, we can collapse it into the parent. 1645df8bae1dSRodney W. Grimes */ 16462ad1a3f7SMatthew Dillon vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT); 1647df8bae1dSRodney W. Grimes 1648f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 1649f8a47341SAlan Cox /* 1650f8a47341SAlan Cox * Break any reservations from backing_object. 1651f8a47341SAlan Cox */ 1652f8a47341SAlan Cox if (__predict_false(!LIST_EMPTY(&backing_object->rvq))) 1653f8a47341SAlan Cox vm_reserv_break_all(backing_object); 1654f8a47341SAlan Cox #endif 1655f8a47341SAlan Cox 1656df8bae1dSRodney W. Grimes /* 1657df8bae1dSRodney W. Grimes * Move the pager from backing_object to object. 1658df8bae1dSRodney W. Grimes */ 16596be36525SAlan Cox if (backing_object->type == OBJT_SWAP) { 166024a1cce3SDavid Greenman /* 1661c7c8dd7eSAlan Cox * swap_pager_copy() can sleep, in which case 1662c7c8dd7eSAlan Cox * the backing_object's and object's locks are 1663c7c8dd7eSAlan Cox * released and reacquired. 166424a1cce3SDavid Greenman */ 16651c7c3c6aSMatthew Dillon swap_pager_copy( 16661c7c3c6aSMatthew Dillon backing_object, 16671c7c3c6aSMatthew Dillon object, 16681c7c3c6aSMatthew Dillon OFF_TO_IDX(object->backing_object_offset), TRUE); 16697bfda801SAlan Cox 16707bfda801SAlan Cox /* 16717bfda801SAlan Cox * Free any cached pages from backing_object. 16727bfda801SAlan Cox */ 16737bfda801SAlan Cox if (__predict_false(backing_object->cache != NULL)) 1674c9444914SAlan Cox vm_page_cache_free(backing_object, 0, 0); 1675c0503609SDavid Greenman } 1676df8bae1dSRodney W. Grimes /* 1677df8bae1dSRodney W. Grimes * Object now shadows whatever backing_object did. 16782ad1a3f7SMatthew Dillon * Note that the reference to 16792ad1a3f7SMatthew Dillon * backing_object->backing_object moves from within 16802ad1a3f7SMatthew Dillon * backing_object to within object. 1681df8bae1dSRodney W. Grimes */ 16821c500307SAlan Cox LIST_REMOVE(object, shadow_list); 16834f7c7f6eSAlan Cox backing_object->shadow_count--; 1684de5f6a77SJohn Dyson if (backing_object->backing_object) { 16856be36525SAlan Cox VM_OBJECT_LOCK(backing_object->backing_object); 16861c500307SAlan Cox LIST_REMOVE(backing_object, shadow_list); 168743186e53SAlan Cox LIST_INSERT_HEAD( 168843186e53SAlan Cox &backing_object->backing_object->shadow_head, 168943186e53SAlan Cox object, shadow_list); 169043186e53SAlan Cox /* 169143186e53SAlan Cox * The shadow_count has not changed. 169243186e53SAlan Cox */ 16936be36525SAlan Cox VM_OBJECT_UNLOCK(backing_object->backing_object); 1694de5f6a77SJohn Dyson } 169524a1cce3SDavid Greenman object->backing_object = backing_object->backing_object; 16962ad1a3f7SMatthew Dillon object->backing_object_offset += 16972ad1a3f7SMatthew Dillon backing_object->backing_object_offset; 16982ad1a3f7SMatthew Dillon 1699df8bae1dSRodney W. Grimes /* 1700df8bae1dSRodney W. Grimes * Discard backing_object. 1701df8bae1dSRodney W. Grimes * 17020d94caffSDavid Greenman * Since the backing object has no pages, no pager left, 17030d94caffSDavid Greenman * and no object references within it, all that is 17040d94caffSDavid Greenman * necessary is to dispose of it. 1705df8bae1dSRodney W. Grimes */ 17069b4d473aSKonstantin Belousov KASSERT(backing_object->ref_count == 1, ( 17079b4d473aSKonstantin Belousov "backing_object %p was somehow re-referenced during collapse!", 17089b4d473aSKonstantin Belousov backing_object)); 17096be36525SAlan Cox VM_OBJECT_UNLOCK(backing_object); 17109b4d473aSKonstantin Belousov vm_object_destroy(backing_object); 1711df8bae1dSRodney W. Grimes 1712df8bae1dSRodney W. Grimes object_collapses++; 17130d94caffSDavid Greenman } else { 171495e5e988SJohn Dyson vm_object_t new_backing_object; 1715df8bae1dSRodney W. Grimes 1716df8bae1dSRodney W. Grimes /* 17172ad1a3f7SMatthew Dillon * If we do not entirely shadow the backing object, 17182ad1a3f7SMatthew Dillon * there is nothing we can do so we give up. 1719df8bae1dSRodney W. Grimes */ 1720df59a0feSJeff Roberson if (object->resident_page_count != object->size && 1721df59a0feSJeff Roberson vm_object_backing_scan(object, 1722df59a0feSJeff Roberson OBSC_TEST_ALL_SHADOWED) == 0) { 172340b808a8SAlan Cox VM_OBJECT_UNLOCK(backing_object); 17242ad1a3f7SMatthew Dillon break; 172524a1cce3SDavid Greenman } 1726df8bae1dSRodney W. Grimes 1727df8bae1dSRodney W. Grimes /* 17280d94caffSDavid Greenman * Make the parent shadow the next object in the 17290d94caffSDavid Greenman * chain. Deallocating backing_object will not remove 17300d94caffSDavid Greenman * it, since its reference count is at least 2. 1731df8bae1dSRodney W. Grimes */ 17321c500307SAlan Cox LIST_REMOVE(object, shadow_list); 1733eaf13dd7SJohn Dyson backing_object->shadow_count--; 173495e5e988SJohn Dyson 173595e5e988SJohn Dyson new_backing_object = backing_object->backing_object; 17368aef1712SMatthew Dillon if ((object->backing_object = new_backing_object) != NULL) { 17376be36525SAlan Cox VM_OBJECT_LOCK(new_backing_object); 17381c500307SAlan Cox LIST_INSERT_HEAD( 17392ad1a3f7SMatthew Dillon &new_backing_object->shadow_head, 17402ad1a3f7SMatthew Dillon object, 17412ad1a3f7SMatthew Dillon shadow_list 17422ad1a3f7SMatthew Dillon ); 1743eaf13dd7SJohn Dyson new_backing_object->shadow_count++; 1744b921a12bSAlan Cox vm_object_reference_locked(new_backing_object); 17456be36525SAlan Cox VM_OBJECT_UNLOCK(new_backing_object); 174695e5e988SJohn Dyson object->backing_object_offset += 174795e5e988SJohn Dyson backing_object->backing_object_offset; 1748de5f6a77SJohn Dyson } 1749df8bae1dSRodney W. Grimes 1750df8bae1dSRodney W. Grimes /* 17510d94caffSDavid Greenman * Drop the reference count on backing_object. Since 175222ec553fSAlan Cox * its ref_count was at least 2, it will not vanish. 1753df8bae1dSRodney W. Grimes */ 175422ec553fSAlan Cox backing_object->ref_count--; 175522ec553fSAlan Cox VM_OBJECT_UNLOCK(backing_object); 1756df8bae1dSRodney W. Grimes object_bypasses++; 1757df8bae1dSRodney W. Grimes } 1758df8bae1dSRodney W. Grimes 1759df8bae1dSRodney W. Grimes /* 1760df8bae1dSRodney W. Grimes * Try again with this object's new backing object. 1761df8bae1dSRodney W. Grimes */ 1762df8bae1dSRodney W. Grimes } 1763df8bae1dSRodney W. Grimes } 1764df8bae1dSRodney W. Grimes 1765df8bae1dSRodney W. Grimes /* 1766bff99f0dSAlan Cox * vm_object_page_remove: 1767df8bae1dSRodney W. Grimes * 176868855966SAlan Cox * For the given object, either frees or invalidates each of the 17696bbee8e2SAlan Cox * specified pages. In general, a page is freed. However, if a page is 17706bbee8e2SAlan Cox * wired for any reason other than the existence of a managed, wired 17716bbee8e2SAlan Cox * mapping, then it may be invalidated but not removed from the object. 17726bbee8e2SAlan Cox * Pages are specified by the given range ["start", "end") and the option 17736bbee8e2SAlan Cox * OBJPR_CLEANONLY. As a special case, if "end" is zero, then the range 17746bbee8e2SAlan Cox * extends from "start" to the end of the object. If the option 17756bbee8e2SAlan Cox * OBJPR_CLEANONLY is specified, then only the non-dirty pages within the 17766bbee8e2SAlan Cox * specified range are affected. If the option OBJPR_NOTMAPPED is 17776bbee8e2SAlan Cox * specified, then the pages within the specified range must have no 17786bbee8e2SAlan Cox * mappings. Otherwise, if this option is not specified, any mappings to 17796bbee8e2SAlan Cox * the specified pages are removed before the pages are freed or 17806bbee8e2SAlan Cox * invalidated. 178168855966SAlan Cox * 17826bbee8e2SAlan Cox * In general, this operation should only be performed on objects that 17836bbee8e2SAlan Cox * contain managed pages. There are, however, two exceptions. First, it 17846bbee8e2SAlan Cox * is performed on the kernel and kmem objects by vm_map_entry_delete(). 17856bbee8e2SAlan Cox * Second, it is used by msync(..., MS_INVALIDATE) to invalidate device- 17866bbee8e2SAlan Cox * backed pages. In both of these cases, the option OBJPR_CLEANONLY must 17876bbee8e2SAlan Cox * not be specified and the option OBJPR_NOTMAPPED must be specified. 1788df8bae1dSRodney W. Grimes * 1789df8bae1dSRodney W. Grimes * The object must be locked. 1790df8bae1dSRodney W. Grimes */ 179126f9a767SRodney W. Grimes void 1792ecde4b32SAlan Cox vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end, 17936bbee8e2SAlan Cox int options) 1794df8bae1dSRodney W. Grimes { 1795d031cff1SMatthew Dillon vm_page_t p, next; 179659677d3cSAlan Cox int wirings; 1797df8bae1dSRodney W. Grimes 1798ecde4b32SAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 17996bbee8e2SAlan Cox KASSERT((object->type != OBJT_DEVICE && object->type != OBJT_PHYS) || 18006bbee8e2SAlan Cox (options & (OBJPR_CLEANONLY | OBJPR_NOTMAPPED)) == OBJPR_NOTMAPPED, 18016bbee8e2SAlan Cox ("vm_object_page_remove: illegal options for object %p", object)); 1802ecde4b32SAlan Cox if (object->resident_page_count == 0) 180325732691SAlan Cox goto skipmemq; 1804d474eaaaSDoug Rabson vm_object_pip_add(object, 1); 180526f9a767SRodney W. Grimes again: 1806b382c10aSKonstantin Belousov p = vm_page_find_least(object, start); 18072965a453SKip Macy 180875741c04SAlan Cox /* 18096bbee8e2SAlan Cox * Here, the variable "p" is either (1) the page with the least pindex 18106bbee8e2SAlan Cox * greater than or equal to the parameter "start" or (2) NULL. 181175741c04SAlan Cox */ 18126bbee8e2SAlan Cox for (; p != NULL && (p->pindex < end || end == 0); p = next) { 1813b18bfc3dSJohn Dyson next = TAILQ_NEXT(p, listq); 181475741c04SAlan Cox 181559677d3cSAlan Cox /* 18166bbee8e2SAlan Cox * If the page is wired for any reason besides the existence 18176bbee8e2SAlan Cox * of managed, wired mappings, then it cannot be freed. For 18186bbee8e2SAlan Cox * example, fictitious pages, which represent device memory, 18196bbee8e2SAlan Cox * are inherently wired and cannot be freed. They can, 18206bbee8e2SAlan Cox * however, be invalidated if the option OBJPR_CLEANONLY is 18216bbee8e2SAlan Cox * not specified. 182259677d3cSAlan Cox */ 18232965a453SKip Macy vm_page_lock(p); 182459677d3cSAlan Cox if ((wirings = p->wire_count) != 0 && 182559677d3cSAlan Cox (wirings = pmap_page_wired_mappings(p)) != p->wire_count) { 18266bbee8e2SAlan Cox if ((options & OBJPR_NOTMAPPED) == 0) { 18274fec79beSAlan Cox pmap_remove_all(p); 18286bbee8e2SAlan Cox /* Account for removal of wired mappings. */ 18296bbee8e2SAlan Cox if (wirings != 0) 183059677d3cSAlan Cox p->wire_count -= wirings; 18316bbee8e2SAlan Cox } 18326bbee8e2SAlan Cox if ((options & OBJPR_CLEANONLY) == 0) { 1833bd7e5f99SJohn Dyson p->valid = 0; 1834a28042d1SAlan Cox vm_page_undirty(p); 1835a28042d1SAlan Cox } 18362965a453SKip Macy vm_page_unlock(p); 18370d94caffSDavid Greenman continue; 18380d94caffSDavid Greenman } 183932585dd6SAlan Cox if (vm_page_sleep_if_busy(p, TRUE, "vmopar")) 184026f9a767SRodney W. Grimes goto again; 184168855966SAlan Cox KASSERT((p->flags & PG_FICTITIOUS) == 0, 184268855966SAlan Cox ("vm_object_page_remove: page %p is fictitious", p)); 18436bbee8e2SAlan Cox if ((options & OBJPR_CLEANONLY) != 0 && p->valid != 0) { 18446bbee8e2SAlan Cox if ((options & OBJPR_NOTMAPPED) == 0) 184578985e42SAlan Cox pmap_remove_write(p); 18462965a453SKip Macy if (p->dirty) { 18472965a453SKip Macy vm_page_unlock(p); 18487c1f6cedSDavid Greenman continue; 18497c1f6cedSDavid Greenman } 18502965a453SKip Macy } 18516bbee8e2SAlan Cox if ((options & OBJPR_NOTMAPPED) == 0) { 18524fec79beSAlan Cox pmap_remove_all(p); 18536bbee8e2SAlan Cox /* Account for removal of wired mappings. */ 185459677d3cSAlan Cox if (wirings != 0) 185559677d3cSAlan Cox p->wire_count -= wirings; 18566bbee8e2SAlan Cox } 1857df8bae1dSRodney W. Grimes vm_page_free(p); 18582965a453SKip Macy vm_page_unlock(p); 18592965a453SKip Macy } 1860f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 186125732691SAlan Cox skipmemq: 1862c9444914SAlan Cox if (__predict_false(object->cache != NULL)) 1863c9444914SAlan Cox vm_page_cache_free(object, start, end); 1864c0503609SDavid Greenman } 1865df8bae1dSRodney W. Grimes 1866df8bae1dSRodney W. Grimes /* 1867936c09acSJohn Baldwin * vm_object_page_cache: 1868936c09acSJohn Baldwin * 1869936c09acSJohn Baldwin * For the given object, attempt to move the specified clean 1870936c09acSJohn Baldwin * pages to the cache queue. If a page is wired for any reason, 1871936c09acSJohn Baldwin * then it will not be changed. Pages are specified by the given 1872936c09acSJohn Baldwin * range ["start", "end"). As a special case, if "end" is zero, 1873936c09acSJohn Baldwin * then the range extends from "start" to the end of the object. 1874936c09acSJohn Baldwin * Any mappings to the specified pages are removed before the 1875936c09acSJohn Baldwin * pages are moved to the cache queue. 1876936c09acSJohn Baldwin * 1877936c09acSJohn Baldwin * This operation should only be performed on objects that 1878936c09acSJohn Baldwin * contain managed pages. 1879936c09acSJohn Baldwin * 1880936c09acSJohn Baldwin * The object must be locked. 1881936c09acSJohn Baldwin */ 1882936c09acSJohn Baldwin void 1883936c09acSJohn Baldwin vm_object_page_cache(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 1884936c09acSJohn Baldwin { 1885936c09acSJohn Baldwin struct mtx *mtx, *new_mtx; 1886936c09acSJohn Baldwin vm_page_t p, next; 1887936c09acSJohn Baldwin 1888936c09acSJohn Baldwin VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1889936c09acSJohn Baldwin KASSERT((object->type != OBJT_DEVICE && object->type != OBJT_SG && 1890936c09acSJohn Baldwin object->type != OBJT_PHYS), 1891936c09acSJohn Baldwin ("vm_object_page_cache: illegal object %p", object)); 1892936c09acSJohn Baldwin if (object->resident_page_count == 0) 1893936c09acSJohn Baldwin return; 1894936c09acSJohn Baldwin p = vm_page_find_least(object, start); 1895936c09acSJohn Baldwin 1896936c09acSJohn Baldwin /* 1897936c09acSJohn Baldwin * Here, the variable "p" is either (1) the page with the least pindex 1898936c09acSJohn Baldwin * greater than or equal to the parameter "start" or (2) NULL. 1899936c09acSJohn Baldwin */ 1900936c09acSJohn Baldwin mtx = NULL; 1901936c09acSJohn Baldwin for (; p != NULL && (p->pindex < end || end == 0); p = next) { 1902936c09acSJohn Baldwin next = TAILQ_NEXT(p, listq); 1903936c09acSJohn Baldwin 1904936c09acSJohn Baldwin /* 1905936c09acSJohn Baldwin * Avoid releasing and reacquiring the same page lock. 1906936c09acSJohn Baldwin */ 1907936c09acSJohn Baldwin new_mtx = vm_page_lockptr(p); 1908936c09acSJohn Baldwin if (mtx != new_mtx) { 1909936c09acSJohn Baldwin if (mtx != NULL) 1910936c09acSJohn Baldwin mtx_unlock(mtx); 1911936c09acSJohn Baldwin mtx = new_mtx; 1912936c09acSJohn Baldwin mtx_lock(mtx); 1913936c09acSJohn Baldwin } 1914936c09acSJohn Baldwin vm_page_try_to_cache(p); 1915936c09acSJohn Baldwin } 1916936c09acSJohn Baldwin if (mtx != NULL) 1917936c09acSJohn Baldwin mtx_unlock(mtx); 1918936c09acSJohn Baldwin } 1919936c09acSJohn Baldwin 1920936c09acSJohn Baldwin /* 1921387aabc5SAlan Cox * Populate the specified range of the object with valid pages. Returns 1922387aabc5SAlan Cox * TRUE if the range is successfully populated and FALSE otherwise. 1923387aabc5SAlan Cox * 1924387aabc5SAlan Cox * Note: This function should be optimized to pass a larger array of 1925387aabc5SAlan Cox * pages to vm_pager_get_pages() before it is applied to a non- 1926387aabc5SAlan Cox * OBJT_DEVICE object. 1927387aabc5SAlan Cox * 1928387aabc5SAlan Cox * The object must be locked. 1929387aabc5SAlan Cox */ 1930387aabc5SAlan Cox boolean_t 1931387aabc5SAlan Cox vm_object_populate(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 1932387aabc5SAlan Cox { 1933387aabc5SAlan Cox vm_page_t m, ma[1]; 1934387aabc5SAlan Cox vm_pindex_t pindex; 1935387aabc5SAlan Cox int rv; 1936387aabc5SAlan Cox 1937387aabc5SAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1938387aabc5SAlan Cox for (pindex = start; pindex < end; pindex++) { 1939387aabc5SAlan Cox m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL | 1940387aabc5SAlan Cox VM_ALLOC_RETRY); 1941387aabc5SAlan Cox if (m->valid != VM_PAGE_BITS_ALL) { 1942387aabc5SAlan Cox ma[0] = m; 1943387aabc5SAlan Cox rv = vm_pager_get_pages(object, ma, 1, 0); 1944387aabc5SAlan Cox m = vm_page_lookup(object, pindex); 1945387aabc5SAlan Cox if (m == NULL) 1946387aabc5SAlan Cox break; 1947387aabc5SAlan Cox if (rv != VM_PAGER_OK) { 19482965a453SKip Macy vm_page_lock(m); 1949387aabc5SAlan Cox vm_page_free(m); 19502965a453SKip Macy vm_page_unlock(m); 1951387aabc5SAlan Cox break; 1952387aabc5SAlan Cox } 1953387aabc5SAlan Cox } 1954387aabc5SAlan Cox /* 1955387aabc5SAlan Cox * Keep "m" busy because a subsequent iteration may unlock 1956387aabc5SAlan Cox * the object. 1957387aabc5SAlan Cox */ 1958387aabc5SAlan Cox } 1959387aabc5SAlan Cox if (pindex > start) { 1960387aabc5SAlan Cox m = vm_page_lookup(object, start); 1961387aabc5SAlan Cox while (m != NULL && m->pindex < pindex) { 1962387aabc5SAlan Cox vm_page_wakeup(m); 1963387aabc5SAlan Cox m = TAILQ_NEXT(m, listq); 1964387aabc5SAlan Cox } 1965387aabc5SAlan Cox } 1966387aabc5SAlan Cox return (pindex == end); 1967387aabc5SAlan Cox } 1968387aabc5SAlan Cox 1969387aabc5SAlan Cox /* 1970df8bae1dSRodney W. Grimes * Routine: vm_object_coalesce 1971df8bae1dSRodney W. Grimes * Function: Coalesces two objects backing up adjoining 1972df8bae1dSRodney W. Grimes * regions of memory into a single object. 1973df8bae1dSRodney W. Grimes * 1974df8bae1dSRodney W. Grimes * returns TRUE if objects were combined. 1975df8bae1dSRodney W. Grimes * 1976df8bae1dSRodney W. Grimes * NOTE: Only works at the moment if the second object is NULL - 1977df8bae1dSRodney W. Grimes * if it's not, which object do we lock first? 1978df8bae1dSRodney W. Grimes * 1979df8bae1dSRodney W. Grimes * Parameters: 1980df8bae1dSRodney W. Grimes * prev_object First object to coalesce 1981df8bae1dSRodney W. Grimes * prev_offset Offset into prev_object 1982df8bae1dSRodney W. Grimes * prev_size Size of reference to prev_object 198357a21abaSAlan Cox * next_size Size of reference to the second object 19843364c323SKonstantin Belousov * reserved Indicator that extension region has 19853364c323SKonstantin Belousov * swap accounted for 1986df8bae1dSRodney W. Grimes * 1987df8bae1dSRodney W. Grimes * Conditions: 1988df8bae1dSRodney W. Grimes * The object must *not* be locked. 1989df8bae1dSRodney W. Grimes */ 19900d94caffSDavid Greenman boolean_t 199157a21abaSAlan Cox vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset, 19923364c323SKonstantin Belousov vm_size_t prev_size, vm_size_t next_size, boolean_t reserved) 1993df8bae1dSRodney W. Grimes { 1994ea41812fSAlan Cox vm_pindex_t next_pindex; 1995df8bae1dSRodney W. Grimes 199600e1854aSAlan Cox if (prev_object == NULL) 1997df8bae1dSRodney W. Grimes return (TRUE); 1998bdbfbaafSAlan Cox VM_OBJECT_LOCK(prev_object); 19994112823fSMatthew Dillon if (prev_object->type != OBJT_DEFAULT && 20004112823fSMatthew Dillon prev_object->type != OBJT_SWAP) { 2001bdbfbaafSAlan Cox VM_OBJECT_UNLOCK(prev_object); 200230dcfc09SJohn Dyson return (FALSE); 200330dcfc09SJohn Dyson } 200430dcfc09SJohn Dyson 2005df8bae1dSRodney W. Grimes /* 2006df8bae1dSRodney W. Grimes * Try to collapse the object first 2007df8bae1dSRodney W. Grimes */ 2008df8bae1dSRodney W. Grimes vm_object_collapse(prev_object); 2009df8bae1dSRodney W. Grimes 2010df8bae1dSRodney W. Grimes /* 20110d94caffSDavid Greenman * Can't coalesce if: . more than one reference . paged out . shadows 20120d94caffSDavid Greenman * another object . has a copy elsewhere (any of which mean that the 20130d94caffSDavid Greenman * pages not mapped to prev_entry may be in use anyway) 2014df8bae1dSRodney W. Grimes */ 20158cc7e047SJohn Dyson if (prev_object->backing_object != NULL) { 2016bdbfbaafSAlan Cox VM_OBJECT_UNLOCK(prev_object); 2017df8bae1dSRodney W. Grimes return (FALSE); 2018df8bae1dSRodney W. Grimes } 2019a316d390SJohn Dyson 2020a316d390SJohn Dyson prev_size >>= PAGE_SHIFT; 2021a316d390SJohn Dyson next_size >>= PAGE_SHIFT; 202257a21abaSAlan Cox next_pindex = OFF_TO_IDX(prev_offset) + prev_size; 20238cc7e047SJohn Dyson 20248cc7e047SJohn Dyson if ((prev_object->ref_count > 1) && 2025ea41812fSAlan Cox (prev_object->size != next_pindex)) { 2026bdbfbaafSAlan Cox VM_OBJECT_UNLOCK(prev_object); 20278cc7e047SJohn Dyson return (FALSE); 20288cc7e047SJohn Dyson } 20298cc7e047SJohn Dyson 2030df8bae1dSRodney W. Grimes /* 20313364c323SKonstantin Belousov * Account for the charge. 20323364c323SKonstantin Belousov */ 2033ef694c1aSEdward Tomasz Napierala if (prev_object->cred != NULL) { 20343364c323SKonstantin Belousov 20353364c323SKonstantin Belousov /* 20363364c323SKonstantin Belousov * If prev_object was charged, then this mapping, 20373364c323SKonstantin Belousov * althought not charged now, may become writable 2038ef694c1aSEdward Tomasz Napierala * later. Non-NULL cred in the object would prevent 20393364c323SKonstantin Belousov * swap reservation during enabling of the write 20403364c323SKonstantin Belousov * access, so reserve swap now. Failed reservation 20413364c323SKonstantin Belousov * cause allocation of the separate object for the map 20423364c323SKonstantin Belousov * entry, and swap reservation for this entry is 20433364c323SKonstantin Belousov * managed in appropriate time. 20443364c323SKonstantin Belousov */ 2045ef694c1aSEdward Tomasz Napierala if (!reserved && !swap_reserve_by_cred(ptoa(next_size), 2046ef694c1aSEdward Tomasz Napierala prev_object->cred)) { 20473364c323SKonstantin Belousov return (FALSE); 20483364c323SKonstantin Belousov } 20493364c323SKonstantin Belousov prev_object->charge += ptoa(next_size); 20503364c323SKonstantin Belousov } 20513364c323SKonstantin Belousov 20523364c323SKonstantin Belousov /* 20530d94caffSDavid Greenman * Remove any pages that may still be in the object from a previous 20540d94caffSDavid Greenman * deallocation. 2055df8bae1dSRodney W. Grimes */ 2056ea41812fSAlan Cox if (next_pindex < prev_object->size) { 20576bbee8e2SAlan Cox vm_object_page_remove(prev_object, next_pindex, next_pindex + 20586bbee8e2SAlan Cox next_size, 0); 2059ea41812fSAlan Cox if (prev_object->type == OBJT_SWAP) 2060ea41812fSAlan Cox swap_pager_freespace(prev_object, 2061ea41812fSAlan Cox next_pindex, next_size); 20623364c323SKonstantin Belousov #if 0 2063ef694c1aSEdward Tomasz Napierala if (prev_object->cred != NULL) { 20643364c323SKonstantin Belousov KASSERT(prev_object->charge >= 20653364c323SKonstantin Belousov ptoa(prev_object->size - next_pindex), 20663364c323SKonstantin Belousov ("object %p overcharged 1 %jx %jx", prev_object, 20673364c323SKonstantin Belousov (uintmax_t)next_pindex, (uintmax_t)next_size)); 20683364c323SKonstantin Belousov prev_object->charge -= ptoa(prev_object->size - 20693364c323SKonstantin Belousov next_pindex); 20703364c323SKonstantin Belousov } 20713364c323SKonstantin Belousov #endif 2072ea41812fSAlan Cox } 2073df8bae1dSRodney W. Grimes 2074df8bae1dSRodney W. Grimes /* 2075df8bae1dSRodney W. Grimes * Extend the object if necessary. 2076df8bae1dSRodney W. Grimes */ 2077ea41812fSAlan Cox if (next_pindex + next_size > prev_object->size) 2078ea41812fSAlan Cox prev_object->size = next_pindex + next_size; 2079df8bae1dSRodney W. Grimes 2080bdbfbaafSAlan Cox VM_OBJECT_UNLOCK(prev_object); 2081df8bae1dSRodney W. Grimes return (TRUE); 2082df8bae1dSRodney W. Grimes } 2083df8bae1dSRodney W. Grimes 20847a5a6352SMatthew Dillon void 20857a5a6352SMatthew Dillon vm_object_set_writeable_dirty(vm_object_t object) 20867a5a6352SMatthew Dillon { 20877a5a6352SMatthew Dillon 2088de33beddSAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 20893280870dSKonstantin Belousov if (object->type != OBJT_VNODE) 20903280870dSKonstantin Belousov return; 20913280870dSKonstantin Belousov object->generation++; 20923280870dSKonstantin Belousov if ((object->flags & OBJ_MIGHTBEDIRTY) != 0) 2093ee39666aSJeff Roberson return; 2094af51d7bfSAlan Cox vm_object_set_flag(object, OBJ_MIGHTBEDIRTY); 20957a5a6352SMatthew Dillon } 20967a5a6352SMatthew Dillon 2097c7c34a24SBruce Evans #include "opt_ddb.h" 2098c3cb3e12SDavid Greenman #ifdef DDB 2099c7c34a24SBruce Evans #include <sys/kernel.h> 2100c7c34a24SBruce Evans 2101ce9edcf5SPoul-Henning Kamp #include <sys/cons.h> 2102c7c34a24SBruce Evans 2103c7c34a24SBruce Evans #include <ddb/ddb.h> 2104c7c34a24SBruce Evans 2105cac597e4SBruce Evans static int 21061b40f8c0SMatthew Dillon _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry) 2107a1f6d91cSDavid Greenman { 2108a1f6d91cSDavid Greenman vm_map_t tmpm; 2109a1f6d91cSDavid Greenman vm_map_entry_t tmpe; 2110a1f6d91cSDavid Greenman vm_object_t obj; 2111a1f6d91cSDavid Greenman int entcount; 2112a1f6d91cSDavid Greenman 2113a1f6d91cSDavid Greenman if (map == 0) 2114a1f6d91cSDavid Greenman return 0; 2115a1f6d91cSDavid Greenman 2116a1f6d91cSDavid Greenman if (entry == 0) { 2117a1f6d91cSDavid Greenman tmpe = map->header.next; 2118a1f6d91cSDavid Greenman entcount = map->nentries; 2119a1f6d91cSDavid Greenman while (entcount-- && (tmpe != &map->header)) { 2120a1f6d91cSDavid Greenman if (_vm_object_in_map(map, object, tmpe)) { 2121a1f6d91cSDavid Greenman return 1; 2122a1f6d91cSDavid Greenman } 2123a1f6d91cSDavid Greenman tmpe = tmpe->next; 2124a1f6d91cSDavid Greenman } 21259fdfe602SMatthew Dillon } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 21269fdfe602SMatthew Dillon tmpm = entry->object.sub_map; 2127a1f6d91cSDavid Greenman tmpe = tmpm->header.next; 2128a1f6d91cSDavid Greenman entcount = tmpm->nentries; 2129a1f6d91cSDavid Greenman while (entcount-- && tmpe != &tmpm->header) { 2130a1f6d91cSDavid Greenman if (_vm_object_in_map(tmpm, object, tmpe)) { 2131a1f6d91cSDavid Greenman return 1; 2132a1f6d91cSDavid Greenman } 2133a1f6d91cSDavid Greenman tmpe = tmpe->next; 2134a1f6d91cSDavid Greenman } 21358aef1712SMatthew Dillon } else if ((obj = entry->object.vm_object) != NULL) { 213624a1cce3SDavid Greenman for (; obj; obj = obj->backing_object) 2137a1f6d91cSDavid Greenman if (obj == object) { 2138a1f6d91cSDavid Greenman return 1; 2139a1f6d91cSDavid Greenman } 2140a1f6d91cSDavid Greenman } 2141a1f6d91cSDavid Greenman return 0; 2142a1f6d91cSDavid Greenman } 2143a1f6d91cSDavid Greenman 2144cac597e4SBruce Evans static int 21451b40f8c0SMatthew Dillon vm_object_in_map(vm_object_t object) 2146a1f6d91cSDavid Greenman { 2147a1f6d91cSDavid Greenman struct proc *p; 21481005a129SJohn Baldwin 214960517fd1SJohn Baldwin /* sx_slock(&allproc_lock); */ 2150f67af5c9SXin LI FOREACH_PROC_IN_SYSTEM(p) { 2151a1f6d91cSDavid Greenman if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */) 2152a1f6d91cSDavid Greenman continue; 2153553629ebSJake Burkholder if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) { 215460517fd1SJohn Baldwin /* sx_sunlock(&allproc_lock); */ 2155a1f6d91cSDavid Greenman return 1; 2156a1f6d91cSDavid Greenman } 2157553629ebSJake Burkholder } 215860517fd1SJohn Baldwin /* sx_sunlock(&allproc_lock); */ 2159a1f6d91cSDavid Greenman if (_vm_object_in_map(kernel_map, object, 0)) 2160a1f6d91cSDavid Greenman return 1; 2161a1f6d91cSDavid Greenman if (_vm_object_in_map(kmem_map, object, 0)) 2162a1f6d91cSDavid Greenman return 1; 2163a1f6d91cSDavid Greenman if (_vm_object_in_map(pager_map, object, 0)) 2164a1f6d91cSDavid Greenman return 1; 2165a1f6d91cSDavid Greenman if (_vm_object_in_map(buffer_map, object, 0)) 2166a1f6d91cSDavid Greenman return 1; 2167a1f6d91cSDavid Greenman return 0; 2168a1f6d91cSDavid Greenman } 2169a1f6d91cSDavid Greenman 2170c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check) 2171f708ef1bSPoul-Henning Kamp { 2172a1f6d91cSDavid Greenman vm_object_t object; 2173a1f6d91cSDavid Greenman 2174a1f6d91cSDavid Greenman /* 2175a1f6d91cSDavid Greenman * make sure that internal objs are in a map somewhere 2176a1f6d91cSDavid Greenman * and none have zero ref counts. 2177a1f6d91cSDavid Greenman */ 2178cc64b484SAlfred Perlstein TAILQ_FOREACH(object, &vm_object_list, object_list) { 217924a1cce3SDavid Greenman if (object->handle == NULL && 218024a1cce3SDavid Greenman (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) { 2181a1f6d91cSDavid Greenman if (object->ref_count == 0) { 21823efc015bSPeter Wemm db_printf("vmochk: internal obj has zero ref count: %ld\n", 21833efc015bSPeter Wemm (long)object->size); 2184a1f6d91cSDavid Greenman } 2185a1f6d91cSDavid Greenman if (!vm_object_in_map(object)) { 2186fc62ef1fSBruce Evans db_printf( 2187fc62ef1fSBruce Evans "vmochk: internal obj is not in a map: " 2188fc62ef1fSBruce Evans "ref: %d, size: %lu: 0x%lx, backing_object: %p\n", 2189fc62ef1fSBruce Evans object->ref_count, (u_long)object->size, 2190fc62ef1fSBruce Evans (u_long)object->size, 2191fc62ef1fSBruce Evans (void *)object->backing_object); 2192a1f6d91cSDavid Greenman } 2193a1f6d91cSDavid Greenman } 2194a1f6d91cSDavid Greenman } 2195a1f6d91cSDavid Greenman } 2196a1f6d91cSDavid Greenman 219726f9a767SRodney W. Grimes /* 2198df8bae1dSRodney W. Grimes * vm_object_print: [ debug ] 2199df8bae1dSRodney W. Grimes */ 2200c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static) 2201df8bae1dSRodney W. Grimes { 2202c7c34a24SBruce Evans /* XXX convert args. */ 2203c7c34a24SBruce Evans vm_object_t object = (vm_object_t)addr; 2204c7c34a24SBruce Evans boolean_t full = have_addr; 2205c7c34a24SBruce Evans 2206d031cff1SMatthew Dillon vm_page_t p; 2207df8bae1dSRodney W. Grimes 2208c7c34a24SBruce Evans /* XXX count is an (unused) arg. Avoid shadowing it. */ 2209c7c34a24SBruce Evans #define count was_count 2210c7c34a24SBruce Evans 2211d031cff1SMatthew Dillon int count; 2212df8bae1dSRodney W. Grimes 2213df8bae1dSRodney W. Grimes if (object == NULL) 2214df8bae1dSRodney W. Grimes return; 2215df8bae1dSRodney W. Grimes 2216eb95adefSBruce Evans db_iprintf( 2217ef694c1aSEdward Tomasz Napierala "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x ruid %d charge %jx\n", 2218e47cd172SMaxime Henrion object, (int)object->type, (uintmax_t)object->size, 22193364c323SKonstantin Belousov object->resident_page_count, object->ref_count, object->flags, 2220ef694c1aSEdward Tomasz Napierala object->cred ? object->cred->cr_ruid : -1, (uintmax_t)object->charge); 2221e47cd172SMaxime Henrion db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n", 22221c7c3c6aSMatthew Dillon object->shadow_count, 2223eb95adefSBruce Evans object->backing_object ? object->backing_object->ref_count : 0, 2224e47cd172SMaxime Henrion object->backing_object, (uintmax_t)object->backing_object_offset); 2225df8bae1dSRodney W. Grimes 2226df8bae1dSRodney W. Grimes if (!full) 2227df8bae1dSRodney W. Grimes return; 2228df8bae1dSRodney W. Grimes 2229c7c34a24SBruce Evans db_indent += 2; 2230df8bae1dSRodney W. Grimes count = 0; 2231fc2ffbe6SPoul-Henning Kamp TAILQ_FOREACH(p, &object->memq, listq) { 2232df8bae1dSRodney W. Grimes if (count == 0) 2233c7c34a24SBruce Evans db_iprintf("memory:="); 2234df8bae1dSRodney W. Grimes else if (count == 6) { 2235c7c34a24SBruce Evans db_printf("\n"); 2236c7c34a24SBruce Evans db_iprintf(" ..."); 2237df8bae1dSRodney W. Grimes count = 0; 2238df8bae1dSRodney W. Grimes } else 2239c7c34a24SBruce Evans db_printf(","); 2240df8bae1dSRodney W. Grimes count++; 2241df8bae1dSRodney W. Grimes 2242e47cd172SMaxime Henrion db_printf("(off=0x%jx,page=0x%jx)", 2243e47cd172SMaxime Henrion (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p)); 2244df8bae1dSRodney W. Grimes } 2245df8bae1dSRodney W. Grimes if (count != 0) 2246c7c34a24SBruce Evans db_printf("\n"); 2247c7c34a24SBruce Evans db_indent -= 2; 2248df8bae1dSRodney W. Grimes } 22495070c7f8SJohn Dyson 2250c7c34a24SBruce Evans /* XXX. */ 2251c7c34a24SBruce Evans #undef count 2252c7c34a24SBruce Evans 2253c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */ 22545070c7f8SJohn Dyson void 22551b40f8c0SMatthew Dillon vm_object_print( 22561b40f8c0SMatthew Dillon /* db_expr_t */ long addr, 22571b40f8c0SMatthew Dillon boolean_t have_addr, 22581b40f8c0SMatthew Dillon /* db_expr_t */ long count, 22591b40f8c0SMatthew Dillon char *modif) 2260c7c34a24SBruce Evans { 2261c7c34a24SBruce Evans vm_object_print_static(addr, have_addr, count, modif); 2262c7c34a24SBruce Evans } 2263c7c34a24SBruce Evans 2264c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages) 22655070c7f8SJohn Dyson { 22665070c7f8SJohn Dyson vm_object_t object; 2267bb2ac86fSKonstantin Belousov vm_pindex_t fidx; 2268bb2ac86fSKonstantin Belousov vm_paddr_t pa; 2269bb2ac86fSKonstantin Belousov vm_page_t m, prev_m; 2270bb2ac86fSKonstantin Belousov int rcount, nl, c; 2271cc64b484SAlfred Perlstein 2272bb2ac86fSKonstantin Belousov nl = 0; 2273cc64b484SAlfred Perlstein TAILQ_FOREACH(object, &vm_object_list, object_list) { 2274fc62ef1fSBruce Evans db_printf("new object: %p\n", (void *)object); 22755070c7f8SJohn Dyson if (nl > 18) { 22765070c7f8SJohn Dyson c = cngetc(); 22775070c7f8SJohn Dyson if (c != ' ') 22785070c7f8SJohn Dyson return; 22795070c7f8SJohn Dyson nl = 0; 22805070c7f8SJohn Dyson } 22815070c7f8SJohn Dyson nl++; 22825070c7f8SJohn Dyson rcount = 0; 22835070c7f8SJohn Dyson fidx = 0; 2284bb2ac86fSKonstantin Belousov pa = -1; 2285bb2ac86fSKonstantin Belousov TAILQ_FOREACH(m, &object->memq, listq) { 2286bb2ac86fSKonstantin Belousov if (m->pindex > 128) 2287bb2ac86fSKonstantin Belousov break; 2288bb2ac86fSKonstantin Belousov if ((prev_m = TAILQ_PREV(m, pglist, listq)) != NULL && 2289bb2ac86fSKonstantin Belousov prev_m->pindex + 1 != m->pindex) { 22905070c7f8SJohn Dyson if (rcount) { 22913efc015bSPeter Wemm db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 22923efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 22935070c7f8SJohn Dyson if (nl > 18) { 22945070c7f8SJohn Dyson c = cngetc(); 22955070c7f8SJohn Dyson if (c != ' ') 22965070c7f8SJohn Dyson return; 22975070c7f8SJohn Dyson nl = 0; 22985070c7f8SJohn Dyson } 22995070c7f8SJohn Dyson nl++; 23005070c7f8SJohn Dyson rcount = 0; 23015070c7f8SJohn Dyson } 23025070c7f8SJohn Dyson } 23035070c7f8SJohn Dyson if (rcount && 23045070c7f8SJohn Dyson (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) { 23055070c7f8SJohn Dyson ++rcount; 23065070c7f8SJohn Dyson continue; 23075070c7f8SJohn Dyson } 23085070c7f8SJohn Dyson if (rcount) { 23092446e4f0SAlan Cox db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 23103efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 23115070c7f8SJohn Dyson if (nl > 18) { 23125070c7f8SJohn Dyson c = cngetc(); 23135070c7f8SJohn Dyson if (c != ' ') 23145070c7f8SJohn Dyson return; 23155070c7f8SJohn Dyson nl = 0; 23165070c7f8SJohn Dyson } 23175070c7f8SJohn Dyson nl++; 23185070c7f8SJohn Dyson } 2319bb2ac86fSKonstantin Belousov fidx = m->pindex; 23205070c7f8SJohn Dyson pa = VM_PAGE_TO_PHYS(m); 23215070c7f8SJohn Dyson rcount = 1; 23225070c7f8SJohn Dyson } 23235070c7f8SJohn Dyson if (rcount) { 23243efc015bSPeter Wemm db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 23253efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 23265070c7f8SJohn Dyson if (nl > 18) { 23275070c7f8SJohn Dyson c = cngetc(); 23285070c7f8SJohn Dyson if (c != ' ') 23295070c7f8SJohn Dyson return; 23305070c7f8SJohn Dyson nl = 0; 23315070c7f8SJohn Dyson } 23325070c7f8SJohn Dyson nl++; 23335070c7f8SJohn Dyson } 23345070c7f8SJohn Dyson } 23355070c7f8SJohn Dyson } 2336c3cb3e12SDavid Greenman #endif /* DDB */ 2337