160727d8bSWarner Losh /*- 2796df753SPedro F. Giffuni * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU) 351369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1991, 1993 5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 6df8bae1dSRodney W. Grimes * 7df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 8df8bae1dSRodney W. Grimes * The Mach Operating System project at Carnegie-Mellon University. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 19df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 20df8bae1dSRodney W. Grimes * without specific prior written permission. 21df8bae1dSRodney W. Grimes * 22df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32df8bae1dSRodney W. Grimes * SUCH DAMAGE. 33df8bae1dSRodney W. Grimes * 343c4dd356SDavid Greenman * from: @(#)vm_object.c 8.5 (Berkeley) 3/22/94 35df8bae1dSRodney W. Grimes * 36df8bae1dSRodney W. Grimes * 37df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 38df8bae1dSRodney W. Grimes * All rights reserved. 39df8bae1dSRodney W. Grimes * 40df8bae1dSRodney W. Grimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 41df8bae1dSRodney W. Grimes * 42df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and 43df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright 44df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the 45df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions 46df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 47df8bae1dSRodney W. Grimes * 48df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 49df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 50df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 51df8bae1dSRodney W. Grimes * 52df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to 53df8bae1dSRodney W. Grimes * 54df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 55df8bae1dSRodney W. Grimes * School of Computer Science 56df8bae1dSRodney W. Grimes * Carnegie Mellon University 57df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890 58df8bae1dSRodney W. Grimes * 59df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 60df8bae1dSRodney W. Grimes * rights to redistribute these changes. 61df8bae1dSRodney W. Grimes */ 62df8bae1dSRodney W. Grimes 63df8bae1dSRodney W. Grimes /* 64df8bae1dSRodney W. Grimes * Virtual memory object module. 65df8bae1dSRodney W. Grimes */ 66df8bae1dSRodney W. Grimes 67874651b1SDavid E. O'Brien #include <sys/cdefs.h> 68874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 69874651b1SDavid E. O'Brien 70f8a47341SAlan Cox #include "opt_vm.h" 71f8a47341SAlan Cox 72df8bae1dSRodney W. Grimes #include <sys/param.h> 73df8bae1dSRodney W. Grimes #include <sys/systm.h> 74c99d0c58SMark Johnston #include <sys/blockcount.h> 753f289c3fSJeff Roberson #include <sys/cpuset.h> 76ecfbddf0SKonstantin Belousov #include <sys/limits.h> 77fb919e4dSMark Murray #include <sys/lock.h> 78867a482dSJohn Dyson #include <sys/mman.h> 79cf2819ccSJohn Dyson #include <sys/mount.h> 80b9b7a4beSMatthew Dillon #include <sys/kernel.h> 81f425ab8eSKonstantin Belousov #include <sys/pctrie.h> 82b9b7a4beSMatthew Dillon #include <sys/sysctl.h> 831b367556SJason Evans #include <sys/mutex.h> 84fb919e4dSMark Murray #include <sys/proc.h> /* for curproc, pageproc */ 85cf27e0d1SJeff Roberson #include <sys/refcount.h> 86fb919e4dSMark Murray #include <sys/socket.h> 873364c323SKonstantin Belousov #include <sys/resourcevar.h> 88205be21dSJeff Roberson #include <sys/refcount.h> 8989f6b863SAttilio Rao #include <sys/rwlock.h> 90ff87ae35SJohn Baldwin #include <sys/user.h> 91fb919e4dSMark Murray #include <sys/vnode.h> 92fb919e4dSMark Murray #include <sys/vmmeter.h> 931005a129SJohn Baldwin #include <sys/sx.h> 94df8bae1dSRodney W. Grimes 95df8bae1dSRodney W. Grimes #include <vm/vm.h> 96efeaf95aSDavid Greenman #include <vm/vm_param.h> 97efeaf95aSDavid Greenman #include <vm/pmap.h> 98efeaf95aSDavid Greenman #include <vm/vm_map.h> 99efeaf95aSDavid Greenman #include <vm/vm_object.h> 100df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 10126f9a767SRodney W. Grimes #include <vm/vm_pageout.h> 1020d94caffSDavid Greenman #include <vm/vm_pager.h> 103e2068d0bSJeff Roberson #include <vm/vm_phys.h> 104e2068d0bSJeff Roberson #include <vm/vm_pagequeue.h> 10505f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h> 106a1f6d91cSDavid Greenman #include <vm/vm_kern.h> 107efeaf95aSDavid Greenman #include <vm/vm_extern.h> 108774d251dSAttilio Rao #include <vm/vm_radix.h> 109f8a47341SAlan Cox #include <vm/vm_reserv.h> 110670d17b5SJeff Roberson #include <vm/uma.h> 11126f9a767SRodney W. Grimes 112c53f7aceSDag-Erling Smørgrav static int old_msync; 113c53f7aceSDag-Erling Smørgrav SYSCTL_INT(_vm, OID_AUTO, old_msync, CTLFLAG_RW, &old_msync, 0, 114c53f7aceSDag-Erling Smørgrav "Use old (insecure) msync behavior"); 115c53f7aceSDag-Erling Smørgrav 116757216f3SKonstantin Belousov static int vm_object_page_collect_flush(vm_object_t object, vm_page_t p, 11767d0e293SJeff Roberson int pagerflags, int flags, boolean_t *allclean, 118126d6082SKonstantin Belousov boolean_t *eio); 1193280870dSKonstantin Belousov static boolean_t vm_object_page_remove_write(vm_page_t p, int flags, 12067d0e293SJeff Roberson boolean_t *allclean); 12151b867e5SJeff Roberson static void vm_object_backing_remove(vm_object_t object); 122f6b04d2bSDavid Greenman 123df8bae1dSRodney W. Grimes /* 124df8bae1dSRodney W. Grimes * Virtual memory objects maintain the actual data 125df8bae1dSRodney W. Grimes * associated with allocated virtual memory. A given 126df8bae1dSRodney W. Grimes * page of memory exists within exactly one object. 127df8bae1dSRodney W. Grimes * 128df8bae1dSRodney W. Grimes * An object is only deallocated when all "references" 129df8bae1dSRodney W. Grimes * are given up. Only one "reference" to a given 130df8bae1dSRodney W. Grimes * region of an object should be writeable. 131df8bae1dSRodney W. Grimes * 132df8bae1dSRodney W. Grimes * Associated with each object is a list of all resident 133df8bae1dSRodney W. Grimes * memory pages belonging to that object; this list is 134df8bae1dSRodney W. Grimes * maintained by the "vm_page" module, and locked by the object's 135df8bae1dSRodney W. Grimes * lock. 136df8bae1dSRodney W. Grimes * 137df8bae1dSRodney W. Grimes * Each object also records a "pager" routine which is 138df8bae1dSRodney W. Grimes * used to retrieve (and store) pages to the proper backing 139df8bae1dSRodney W. Grimes * storage. In addition, objects may be backed by other 140df8bae1dSRodney W. Grimes * objects from which they were virtual-copied. 141df8bae1dSRodney W. Grimes * 142df8bae1dSRodney W. Grimes * The only items within the object structure which are 143df8bae1dSRodney W. Grimes * modified after time of creation are: 144df8bae1dSRodney W. Grimes * reference count locked by object's lock 145df8bae1dSRodney W. Grimes * pager routine locked by object's lock 146df8bae1dSRodney W. Grimes * 147df8bae1dSRodney W. Grimes */ 148df8bae1dSRodney W. Grimes 14928f8db14SBruce Evans struct object_q vm_object_list; 150a5698387SAlan Cox struct mtx vm_object_list_mtx; /* lock for object list and count */ 151cccf11b8SAlan Cox 152cccf11b8SAlan Cox struct vm_object kernel_object_store; 153df8bae1dSRodney W. Grimes 1547029da5cSPawel Biernacki static SYSCTL_NODE(_vm_stats, OID_AUTO, object, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 1556472ac3dSEd Schouten "VM object stats"); 156604c2bbcSAlan Cox 157d869a17eSMark Johnston static COUNTER_U64_DEFINE_EARLY(object_collapses); 15811542376SAlan Cox SYSCTL_COUNTER_U64(_vm_stats_object, OID_AUTO, collapses, CTLFLAG_RD, 15911542376SAlan Cox &object_collapses, 16011542376SAlan Cox "VM object collapses"); 161604c2bbcSAlan Cox 162d869a17eSMark Johnston static COUNTER_U64_DEFINE_EARLY(object_bypasses); 16311542376SAlan Cox SYSCTL_COUNTER_U64(_vm_stats_object, OID_AUTO, bypasses, CTLFLAG_RD, 16411542376SAlan Cox &object_bypasses, 16511542376SAlan Cox "VM object bypasses"); 16611542376SAlan Cox 167d869a17eSMark Johnston static COUNTER_U64_DEFINE_EARLY(object_collapse_waits); 16898087a06SJeff Roberson SYSCTL_COUNTER_U64(_vm_stats_object, OID_AUTO, collapse_waits, CTLFLAG_RD, 16998087a06SJeff Roberson &object_collapse_waits, 17098087a06SJeff Roberson "Number of sleeps for collapse"); 17198087a06SJeff Roberson 172670d17b5SJeff Roberson static uma_zone_t obj_zone; 1738355f576SJeff Roberson 174b23f72e9SBrian Feldman static int vm_object_zinit(void *mem, int size, int flags); 1758355f576SJeff Roberson 1768355f576SJeff Roberson #ifdef INVARIANTS 1778355f576SJeff Roberson static void vm_object_zdtor(void *mem, int size, void *arg); 1788355f576SJeff Roberson 1798355f576SJeff Roberson static void 1808355f576SJeff Roberson vm_object_zdtor(void *mem, int size, void *arg) 1818355f576SJeff Roberson { 1828355f576SJeff Roberson vm_object_t object; 1838355f576SJeff Roberson 1848355f576SJeff Roberson object = (vm_object_t)mem; 185e735691bSJohn Baldwin KASSERT(object->ref_count == 0, 186e735691bSJohn Baldwin ("object %p ref_count = %d", object, object->ref_count)); 18743186e53SAlan Cox KASSERT(TAILQ_EMPTY(&object->memq), 188198da1b2SAttilio Rao ("object %p has resident pages in its memq", object)); 189774d251dSAttilio Rao KASSERT(vm_radix_is_empty(&object->rtree), 190774d251dSAttilio Rao ("object %p has resident pages in its trie", object)); 191f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 192f8a47341SAlan Cox KASSERT(LIST_EMPTY(&object->rvq), 193f8a47341SAlan Cox ("object %p has reservations", 194f8a47341SAlan Cox object)); 195f8a47341SAlan Cox #endif 196c99d0c58SMark Johnston KASSERT(!vm_object_busied(object), 197c99d0c58SMark Johnston ("object %p busy = %d", object, blockcount_read(&object->busy))); 1988355f576SJeff Roberson KASSERT(object->resident_page_count == 0, 1998355f576SJeff Roberson ("object %p resident_page_count = %d", 2008355f576SJeff Roberson object, object->resident_page_count)); 2018355f576SJeff Roberson KASSERT(object->shadow_count == 0, 2028355f576SJeff Roberson ("object %p shadow_count = %d", 2038355f576SJeff Roberson object, object->shadow_count)); 204e735691bSJohn Baldwin KASSERT(object->type == OBJT_DEAD, 205e735691bSJohn Baldwin ("object %p has non-dead type %d", 206e735691bSJohn Baldwin object, object->type)); 2078355f576SJeff Roberson } 2088355f576SJeff Roberson #endif 2098355f576SJeff Roberson 210b23f72e9SBrian Feldman static int 211b23f72e9SBrian Feldman vm_object_zinit(void *mem, int size, int flags) 2128355f576SJeff Roberson { 2138355f576SJeff Roberson vm_object_t object; 2148355f576SJeff Roberson 2158355f576SJeff Roberson object = (vm_object_t)mem; 216777a36c5SAlan Cox rw_init_flags(&object->lock, "vm object", RW_DUPOK | RW_NEW); 2178355f576SJeff Roberson 2188355f576SJeff Roberson /* These are true for any object that has been freed */ 219e735691bSJohn Baldwin object->type = OBJT_DEAD; 220cd1241fbSKonstantin Belousov vm_radix_init(&object->rtree); 22151df5321SJeff Roberson refcount_init(&object->ref_count, 0); 222c99d0c58SMark Johnston blockcount_init(&object->paging_in_progress); 223c99d0c58SMark Johnston blockcount_init(&object->busy); 2248355f576SJeff Roberson object->resident_page_count = 0; 2258355f576SJeff Roberson object->shadow_count = 0; 226f425ab8eSKonstantin Belousov object->flags = OBJ_DEAD; 227e735691bSJohn Baldwin 228e735691bSJohn Baldwin mtx_lock(&vm_object_list_mtx); 229e735691bSJohn Baldwin TAILQ_INSERT_TAIL(&vm_object_list, object, object_list); 230e735691bSJohn Baldwin mtx_unlock(&vm_object_list_mtx); 231b23f72e9SBrian Feldman return (0); 2328355f576SJeff Roberson } 233df8bae1dSRodney W. Grimes 234a4915c21SAttilio Rao static void 23563967687SJeff Roberson _vm_object_allocate(objtype_t type, vm_pindex_t size, u_short flags, 23667388836SKonstantin Belousov vm_object_t object, void *handle) 237df8bae1dSRodney W. Grimes { 2380cddd8f0SMatthew Dillon 239df8bae1dSRodney W. Grimes TAILQ_INIT(&object->memq); 2401c500307SAlan Cox LIST_INIT(&object->shadow_head); 241a1f6d91cSDavid Greenman 24224a1cce3SDavid Greenman object->type = type; 2434b8365d7SKonstantin Belousov object->flags = flags; 2444b8365d7SKonstantin Belousov if ((flags & OBJ_SWAP) != 0) 245f425ab8eSKonstantin Belousov pctrie_init(&object->un_pager.swp.swp_blks); 246f425ab8eSKonstantin Belousov 247f425ab8eSKonstantin Belousov /* 248f425ab8eSKonstantin Belousov * Ensure that swap_pager_swapoff() iteration over object_list 249f425ab8eSKonstantin Belousov * sees up to date type and pctrie head if it observed 250f425ab8eSKonstantin Belousov * non-dead object. 251f425ab8eSKonstantin Belousov */ 252f425ab8eSKonstantin Belousov atomic_thread_fence_rel(); 253f425ab8eSKonstantin Belousov 25463967687SJeff Roberson object->pg_color = 0; 255df8bae1dSRodney W. Grimes object->size = size; 2564c29d2deSMark Johnston object->domain.dr_policy = NULL; 257b881da26SAlan Cox object->generation = 1; 25867d0e293SJeff Roberson object->cleangeneration = 1; 25951df5321SJeff Roberson refcount_init(&object->ref_count, 1); 2603153e878SAlan Cox object->memattr = VM_MEMATTR_DEFAULT; 261ef694c1aSEdward Tomasz Napierala object->cred = NULL; 2623364c323SKonstantin Belousov object->charge = 0; 26367388836SKonstantin Belousov object->handle = handle; 26424a1cce3SDavid Greenman object->backing_object = NULL; 265a316d390SJohn Dyson object->backing_object_offset = (vm_ooffset_t) 0; 266f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 267f8a47341SAlan Cox LIST_INIT(&object->rvq); 268f8a47341SAlan Cox #endif 2691bdbd705SKonstantin Belousov umtx_shm_object_init(object); 270df8bae1dSRodney W. Grimes } 271df8bae1dSRodney W. Grimes 272df8bae1dSRodney W. Grimes /* 27326f9a767SRodney W. Grimes * vm_object_init: 27426f9a767SRodney W. Grimes * 27526f9a767SRodney W. Grimes * Initialize the VM objects module. 27626f9a767SRodney W. Grimes */ 27726f9a767SRodney W. Grimes void 2781b40f8c0SMatthew Dillon vm_object_init(void) 27926f9a767SRodney W. Grimes { 28026f9a767SRodney W. Grimes TAILQ_INIT(&vm_object_list); 2816008862bSJohn Baldwin mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF); 2820217125fSDavid Greenman 28389f6b863SAttilio Rao rw_init(&kernel_object->lock, "kernel vm object"); 284d1780e8dSKonstantin Belousov _vm_object_allocate(OBJT_PHYS, atop(VM_MAX_KERNEL_ADDRESS - 28567388836SKonstantin Belousov VM_MIN_KERNEL_ADDRESS), OBJ_UNMANAGED, kernel_object, NULL); 286f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 287f8a47341SAlan Cox kernel_object->flags |= OBJ_COLORED; 288f8a47341SAlan Cox kernel_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS); 289f8a47341SAlan Cox #endif 290a720b31cSKonstantin Belousov kernel_object->un_pager.phys.ops = &default_phys_pg_ops; 29126f9a767SRodney W. Grimes 2928dbca793STor Egge /* 2938dbca793STor Egge * The lock portion of struct vm_object must be type stable due 2948dbca793STor Egge * to vm_pageout_fallback_object_lock locking a vm object 2958dbca793STor Egge * without holding any references to it. 296ffae7ea9SKonstantin Belousov * 297ffae7ea9SKonstantin Belousov * paging_in_progress is valid always. Lockless references to 298ffae7ea9SKonstantin Belousov * the objects may acquire pip and then check OBJ_DEAD. 2998dbca793STor Egge */ 3008355f576SJeff Roberson obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL, 3018355f576SJeff Roberson #ifdef INVARIANTS 3028355f576SJeff Roberson vm_object_zdtor, 3038355f576SJeff Roberson #else 3048355f576SJeff Roberson NULL, 3058355f576SJeff Roberson #endif 3065df87b21SJeff Roberson vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 307774d251dSAttilio Rao 308cd1241fbSKonstantin Belousov vm_radix_zinit(); 30999448ed1SJohn Dyson } 31099448ed1SJohn Dyson 31199448ed1SJohn Dyson void 3121b40f8c0SMatthew Dillon vm_object_clear_flag(vm_object_t object, u_short bits) 3131b40f8c0SMatthew Dillon { 3145440b5a9SAlan Cox 31589f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 316b06805adSJake Burkholder object->flags &= ~bits; 3171b40f8c0SMatthew Dillon } 3181b40f8c0SMatthew Dillon 3193153e878SAlan Cox /* 3203153e878SAlan Cox * Sets the default memory attribute for the specified object. Pages 3213153e878SAlan Cox * that are allocated to this object are by default assigned this memory 3223153e878SAlan Cox * attribute. 3233153e878SAlan Cox * 3243153e878SAlan Cox * Presently, this function must be called before any pages are allocated 3253153e878SAlan Cox * to the object. In the future, this requirement may be relaxed for 3263153e878SAlan Cox * "default" and "swap" objects. 3273153e878SAlan Cox */ 3283153e878SAlan Cox int 3293153e878SAlan Cox vm_object_set_memattr(vm_object_t object, vm_memattr_t memattr) 3303153e878SAlan Cox { 3313153e878SAlan Cox 33289f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 333*3e7a11caSKonstantin Belousov 334*3e7a11caSKonstantin Belousov if (object->type == OBJT_DEAD) 335*3e7a11caSKonstantin Belousov return (KERN_INVALID_ARGUMENT); 3363153e878SAlan Cox if (!TAILQ_EMPTY(&object->memq)) 3373153e878SAlan Cox return (KERN_FAILURE); 338*3e7a11caSKonstantin Belousov 3393153e878SAlan Cox object->memattr = memattr; 3403153e878SAlan Cox return (KERN_SUCCESS); 3413153e878SAlan Cox } 3423153e878SAlan Cox 3431b40f8c0SMatthew Dillon void 3441b40f8c0SMatthew Dillon vm_object_pip_add(vm_object_t object, short i) 3451b40f8c0SMatthew Dillon { 346f279b88dSAlan Cox 347c99d0c58SMark Johnston if (i > 0) 348c99d0c58SMark Johnston blockcount_acquire(&object->paging_in_progress, i); 3491b40f8c0SMatthew Dillon } 3501b40f8c0SMatthew Dillon 3511b40f8c0SMatthew Dillon void 3521b40f8c0SMatthew Dillon vm_object_pip_wakeup(vm_object_t object) 3531b40f8c0SMatthew Dillon { 354f279b88dSAlan Cox 355c99d0c58SMark Johnston vm_object_pip_wakeupn(object, 1); 3561b40f8c0SMatthew Dillon } 3571b40f8c0SMatthew Dillon 3581b40f8c0SMatthew Dillon void 3591b40f8c0SMatthew Dillon vm_object_pip_wakeupn(vm_object_t object, short i) 3601b40f8c0SMatthew Dillon { 361d647a0edSAlan Cox 362c99d0c58SMark Johnston if (i > 0) 363c99d0c58SMark Johnston blockcount_release(&object->paging_in_progress, i); 3641b40f8c0SMatthew Dillon } 3651b40f8c0SMatthew Dillon 36698087a06SJeff Roberson /* 367c99d0c58SMark Johnston * Atomically drop the object lock and wait for pip to drain. This protects 368c99d0c58SMark Johnston * from sleep/wakeup races due to identity changes. The lock is not re-acquired 369c99d0c58SMark Johnston * on return. 37098087a06SJeff Roberson */ 37198087a06SJeff Roberson static void 372eaa17d42SRyan Libby vm_object_pip_sleep(vm_object_t object, const char *waitid) 37398087a06SJeff Roberson { 37498087a06SJeff Roberson 375c99d0c58SMark Johnston (void)blockcount_sleep(&object->paging_in_progress, &object->lock, 376c99d0c58SMark Johnston waitid, PVM | PDROP); 37798087a06SJeff Roberson } 37898087a06SJeff Roberson 3791b40f8c0SMatthew Dillon void 380eaa17d42SRyan Libby vm_object_pip_wait(vm_object_t object, const char *waitid) 3811b40f8c0SMatthew Dillon { 3821ca58953SAlan Cox 38389f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 384cf27e0d1SJeff Roberson 385c99d0c58SMark Johnston blockcount_wait(&object->paging_in_progress, &object->lock, waitid, 386c99d0c58SMark Johnston PVM); 3871b40f8c0SMatthew Dillon } 3881b40f8c0SMatthew Dillon 389cf27e0d1SJeff Roberson void 390eaa17d42SRyan Libby vm_object_pip_wait_unlocked(vm_object_t object, const char *waitid) 391cf27e0d1SJeff Roberson { 392cf27e0d1SJeff Roberson 393cf27e0d1SJeff Roberson VM_OBJECT_ASSERT_UNLOCKED(object); 394cf27e0d1SJeff Roberson 395c99d0c58SMark Johnston blockcount_wait(&object->paging_in_progress, NULL, waitid, PVM); 396cf27e0d1SJeff Roberson } 397cf27e0d1SJeff Roberson 39826f9a767SRodney W. Grimes /* 39926f9a767SRodney W. Grimes * vm_object_allocate: 40026f9a767SRodney W. Grimes * 40126f9a767SRodney W. Grimes * Returns a new object with the given size. 40226f9a767SRodney W. Grimes */ 40326f9a767SRodney W. Grimes vm_object_t 4046395da54SIan Dowse vm_object_allocate(objtype_t type, vm_pindex_t size) 40526f9a767SRodney W. Grimes { 40690688d13SAlan Cox vm_object_t object; 40763967687SJeff Roberson u_short flags; 40863967687SJeff Roberson 40963967687SJeff Roberson switch (type) { 41063967687SJeff Roberson case OBJT_DEAD: 41163967687SJeff Roberson panic("vm_object_allocate: can't create OBJT_DEAD"); 41263967687SJeff Roberson case OBJT_DEFAULT: 41363967687SJeff Roberson flags = OBJ_COLORED; 41463967687SJeff Roberson break; 4154b8365d7SKonstantin Belousov case OBJT_SWAP: 4164b8365d7SKonstantin Belousov case OBJT_SWAP_TMPFS: 4174b8365d7SKonstantin Belousov flags = OBJ_COLORED | OBJ_SWAP; 4184b8365d7SKonstantin Belousov break; 41963967687SJeff Roberson case OBJT_DEVICE: 42063967687SJeff Roberson case OBJT_SG: 42163967687SJeff Roberson flags = OBJ_FICTITIOUS | OBJ_UNMANAGED; 42263967687SJeff Roberson break; 42363967687SJeff Roberson case OBJT_MGTDEVICE: 42463967687SJeff Roberson flags = OBJ_FICTITIOUS; 42563967687SJeff Roberson break; 42663967687SJeff Roberson case OBJT_PHYS: 42763967687SJeff Roberson flags = OBJ_UNMANAGED; 42863967687SJeff Roberson break; 42963967687SJeff Roberson case OBJT_VNODE: 43063967687SJeff Roberson flags = 0; 43163967687SJeff Roberson break; 43263967687SJeff Roberson default: 43363967687SJeff Roberson panic("vm_object_allocate: type %d is undefined", type); 43463967687SJeff Roberson } 43563967687SJeff Roberson object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK); 43667388836SKonstantin Belousov _vm_object_allocate(type, size, flags, object, NULL); 43763967687SJeff Roberson 43863967687SJeff Roberson return (object); 43963967687SJeff Roberson } 44063967687SJeff Roberson 44163967687SJeff Roberson /* 44263967687SJeff Roberson * vm_object_allocate_anon: 44363967687SJeff Roberson * 44463967687SJeff Roberson * Returns a new default object of the given size and marked as 44563967687SJeff Roberson * anonymous memory for special split/collapse handling. Color 44663967687SJeff Roberson * to be initialized by the caller. 44763967687SJeff Roberson */ 44863967687SJeff Roberson vm_object_t 44967388836SKonstantin Belousov vm_object_allocate_anon(vm_pindex_t size, vm_object_t backing_object, 45067388836SKonstantin Belousov struct ucred *cred, vm_size_t charge) 45163967687SJeff Roberson { 45267388836SKonstantin Belousov vm_object_t handle, object; 45390688d13SAlan Cox 45467388836SKonstantin Belousov if (backing_object == NULL) 45567388836SKonstantin Belousov handle = NULL; 45667388836SKonstantin Belousov else if ((backing_object->flags & OBJ_ANON) != 0) 45767388836SKonstantin Belousov handle = backing_object->handle; 45867388836SKonstantin Belousov else 45967388836SKonstantin Belousov handle = backing_object; 46067388836SKonstantin Belousov object = uma_zalloc(obj_zone, M_WAITOK); 46163967687SJeff Roberson _vm_object_allocate(OBJT_DEFAULT, size, OBJ_ANON | OBJ_ONEMAPPING, 46267388836SKonstantin Belousov object, handle); 46367388836SKonstantin Belousov object->cred = cred; 46467388836SKonstantin Belousov object->charge = cred != NULL ? charge : 0; 46590688d13SAlan Cox return (object); 46626f9a767SRodney W. Grimes } 46726f9a767SRodney W. Grimes 46898087a06SJeff Roberson static void 46998087a06SJeff Roberson vm_object_reference_vnode(vm_object_t object) 470df8bae1dSRodney W. Grimes { 471a67d5408SJeff Roberson u_int old; 472a67d5408SJeff Roberson 473a67d5408SJeff Roberson /* 47498087a06SJeff Roberson * vnode objects need the lock for the first reference 47598087a06SJeff Roberson * to serialize with vnode_object_deallocate(). 476a67d5408SJeff Roberson */ 47798087a06SJeff Roberson if (!refcount_acquire_if_gt(&object->ref_count, 0)) { 47851df5321SJeff Roberson VM_OBJECT_RLOCK(object); 479a67d5408SJeff Roberson old = refcount_acquire(&object->ref_count); 4801a0c234eSJeff Roberson if (object->type == OBJT_VNODE && old == 0) 4811a0c234eSJeff Roberson vref(object->handle); 48251df5321SJeff Roberson VM_OBJECT_RUNLOCK(object); 48395e5e988SJohn Dyson } 484a67d5408SJeff Roberson } 48595e5e988SJohn Dyson 48623955314SAlfred Perlstein /* 48798087a06SJeff Roberson * vm_object_reference: 48898087a06SJeff Roberson * 48998087a06SJeff Roberson * Acquires a reference to the given object. 49098087a06SJeff Roberson */ 49198087a06SJeff Roberson void 49298087a06SJeff Roberson vm_object_reference(vm_object_t object) 49398087a06SJeff Roberson { 49498087a06SJeff Roberson 49598087a06SJeff Roberson if (object == NULL) 49698087a06SJeff Roberson return; 49798087a06SJeff Roberson 49898087a06SJeff Roberson if (object->type == OBJT_VNODE) 49998087a06SJeff Roberson vm_object_reference_vnode(object); 50098087a06SJeff Roberson else 50198087a06SJeff Roberson refcount_acquire(&object->ref_count); 50298087a06SJeff Roberson KASSERT((object->flags & OBJ_DEAD) == 0, 50398087a06SJeff Roberson ("vm_object_reference: Referenced dead object.")); 50498087a06SJeff Roberson } 50598087a06SJeff Roberson 50698087a06SJeff Roberson /* 507b921a12bSAlan Cox * vm_object_reference_locked: 508b921a12bSAlan Cox * 509b921a12bSAlan Cox * Gets another reference to the given object. 510b921a12bSAlan Cox * 511b921a12bSAlan Cox * The object must be locked. 512b921a12bSAlan Cox */ 513b921a12bSAlan Cox void 514b921a12bSAlan Cox vm_object_reference_locked(vm_object_t object) 515b921a12bSAlan Cox { 516a67d5408SJeff Roberson u_int old; 517b921a12bSAlan Cox 51851df5321SJeff Roberson VM_OBJECT_ASSERT_LOCKED(object); 519a67d5408SJeff Roberson old = refcount_acquire(&object->ref_count); 5201a0c234eSJeff Roberson if (object->type == OBJT_VNODE && old == 0) 5211a0c234eSJeff Roberson vref(object->handle); 52298087a06SJeff Roberson KASSERT((object->flags & OBJ_DEAD) == 0, 52398087a06SJeff Roberson ("vm_object_reference: Referenced dead object.")); 524b921a12bSAlan Cox } 525b921a12bSAlan Cox 526b921a12bSAlan Cox /* 5279d5abbddSJens Schweikhardt * Handle deallocating an object of type OBJT_VNODE. 52823955314SAlfred Perlstein */ 52902dd8331SAlan Cox static void 53098087a06SJeff Roberson vm_object_deallocate_vnode(vm_object_t object) 53195e5e988SJohn Dyson { 53295e5e988SJohn Dyson struct vnode *vp = (struct vnode *) object->handle; 53326c4e983SJeff Roberson bool last; 534219cbf59SEivind Eklund 5355526d2d9SEivind Eklund KASSERT(object->type == OBJT_VNODE, 53698087a06SJeff Roberson ("vm_object_deallocate_vnode: not a vnode object")); 53798087a06SJeff Roberson KASSERT(vp != NULL, ("vm_object_deallocate_vnode: missing vp")); 53895e5e988SJohn Dyson 53926c4e983SJeff Roberson /* Object lock to protect handle lookup. */ 54026c4e983SJeff Roberson last = refcount_release(&object->ref_count); 54126c4e983SJeff Roberson VM_OBJECT_RUNLOCK(object); 54226c4e983SJeff Roberson 54326c4e983SJeff Roberson if (!last) 54426c4e983SJeff Roberson return; 54526c4e983SJeff Roberson 546a67d5408SJeff Roberson if (!umtx_shm_vnobj_persistent) 5471bdbd705SKonstantin Belousov umtx_shm_object_terminated(object); 5481bdbd705SKonstantin Belousov 54903fa5b34SKonstantin Belousov /* vrele may need the vnode lock. */ 55047221757SJohn Dyson vrele(vp); 55186769ac0SKonstantin Belousov } 552df8bae1dSRodney W. Grimes 55398087a06SJeff Roberson /* 55498087a06SJeff Roberson * We dropped a reference on an object and discovered that it had a 55598087a06SJeff Roberson * single remaining shadow. This is a sibling of the reference we 55698087a06SJeff Roberson * dropped. Attempt to collapse the sibling and backing object. 55798087a06SJeff Roberson */ 55898087a06SJeff Roberson static vm_object_t 55998087a06SJeff Roberson vm_object_deallocate_anon(vm_object_t backing_object) 56098087a06SJeff Roberson { 56198087a06SJeff Roberson vm_object_t object; 56298087a06SJeff Roberson 56398087a06SJeff Roberson /* Fetch the final shadow. */ 56498087a06SJeff Roberson object = LIST_FIRST(&backing_object->shadow_head); 56598087a06SJeff Roberson KASSERT(object != NULL && backing_object->shadow_count == 1, 56698087a06SJeff Roberson ("vm_object_anon_deallocate: ref_count: %d, shadow_count: %d", 56798087a06SJeff Roberson backing_object->ref_count, backing_object->shadow_count)); 5684b8365d7SKonstantin Belousov KASSERT((object->flags & OBJ_ANON) != 0, 56998087a06SJeff Roberson ("invalid shadow object %p", object)); 57098087a06SJeff Roberson 57198087a06SJeff Roberson if (!VM_OBJECT_TRYWLOCK(object)) { 57298087a06SJeff Roberson /* 57398087a06SJeff Roberson * Prevent object from disappearing since we do not have a 57498087a06SJeff Roberson * reference. 57598087a06SJeff Roberson */ 57698087a06SJeff Roberson vm_object_pip_add(object, 1); 57798087a06SJeff Roberson VM_OBJECT_WUNLOCK(backing_object); 57898087a06SJeff Roberson VM_OBJECT_WLOCK(object); 57998087a06SJeff Roberson vm_object_pip_wakeup(object); 58098087a06SJeff Roberson } else 58198087a06SJeff Roberson VM_OBJECT_WUNLOCK(backing_object); 58298087a06SJeff Roberson 58398087a06SJeff Roberson /* 58498087a06SJeff Roberson * Check for a collapse/terminate race with the last reference holder. 58598087a06SJeff Roberson */ 58698087a06SJeff Roberson if ((object->flags & (OBJ_DEAD | OBJ_COLLAPSING)) != 0 || 58798087a06SJeff Roberson !refcount_acquire_if_not_zero(&object->ref_count)) { 58898087a06SJeff Roberson VM_OBJECT_WUNLOCK(object); 58998087a06SJeff Roberson return (NULL); 59098087a06SJeff Roberson } 59198087a06SJeff Roberson backing_object = object->backing_object; 59298087a06SJeff Roberson if (backing_object != NULL && (backing_object->flags & OBJ_ANON) != 0) 59398087a06SJeff Roberson vm_object_collapse(object); 59498087a06SJeff Roberson VM_OBJECT_WUNLOCK(object); 59598087a06SJeff Roberson 59698087a06SJeff Roberson return (object); 59798087a06SJeff Roberson } 59898087a06SJeff Roberson 599df8bae1dSRodney W. Grimes /* 600df8bae1dSRodney W. Grimes * vm_object_deallocate: 601df8bae1dSRodney W. Grimes * 602df8bae1dSRodney W. Grimes * Release a reference to the specified object, 603df8bae1dSRodney W. Grimes * gained either through a vm_object_allocate 604df8bae1dSRodney W. Grimes * or a vm_object_reference call. When all references 605df8bae1dSRodney W. Grimes * are gone, storage associated with this object 606df8bae1dSRodney W. Grimes * may be relinquished. 607df8bae1dSRodney W. Grimes * 608df8bae1dSRodney W. Grimes * No object may be locked. 609df8bae1dSRodney W. Grimes */ 61026f9a767SRodney W. Grimes void 6111b40f8c0SMatthew Dillon vm_object_deallocate(vm_object_t object) 612df8bae1dSRodney W. Grimes { 61398087a06SJeff Roberson vm_object_t temp; 61426c4e983SJeff Roberson bool released; 615df8bae1dSRodney W. Grimes 616df8bae1dSRodney W. Grimes while (object != NULL) { 61751df5321SJeff Roberson /* 61851df5321SJeff Roberson * If the reference count goes to 0 we start calling 61951df5321SJeff Roberson * vm_object_terminate() on the object chain. A ref count 62051df5321SJeff Roberson * of 1 may be a special case depending on the shadow count 62151df5321SJeff Roberson * being 0 or 1. These cases require a write lock on the 62251df5321SJeff Roberson * object. 62351df5321SJeff Roberson */ 62463967687SJeff Roberson if ((object->flags & OBJ_ANON) == 0) 62563967687SJeff Roberson released = refcount_release_if_gt(&object->ref_count, 1); 62663967687SJeff Roberson else 62751df5321SJeff Roberson released = refcount_release_if_gt(&object->ref_count, 2); 62851df5321SJeff Roberson if (released) 62951df5321SJeff Roberson return; 63051df5321SJeff Roberson 631a67d5408SJeff Roberson if (object->type == OBJT_VNODE) { 63226c4e983SJeff Roberson VM_OBJECT_RLOCK(object); 63326c4e983SJeff Roberson if (object->type == OBJT_VNODE) { 63498087a06SJeff Roberson vm_object_deallocate_vnode(object); 635a67d5408SJeff Roberson return; 636a67d5408SJeff Roberson } 63726c4e983SJeff Roberson VM_OBJECT_RUNLOCK(object); 63826c4e983SJeff Roberson } 63926c4e983SJeff Roberson 64026c4e983SJeff Roberson VM_OBJECT_WLOCK(object); 64126c4e983SJeff Roberson KASSERT(object->ref_count > 0, 64226c4e983SJeff Roberson ("vm_object_deallocate: object deallocated too many times: %d", 64326c4e983SJeff Roberson object->type)); 64426c4e983SJeff Roberson 645b72b0115SAlan Cox /* 64698087a06SJeff Roberson * If this is not the final reference to an anonymous 64798087a06SJeff Roberson * object we may need to collapse the shadow chain. 648b72b0115SAlan Cox */ 64998087a06SJeff Roberson if (!refcount_release(&object->ref_count)) { 65098087a06SJeff Roberson if (object->ref_count > 1 || 65198087a06SJeff Roberson object->shadow_count == 0) { 65298087a06SJeff Roberson if ((object->flags & OBJ_ANON) != 0 && 65398087a06SJeff Roberson object->ref_count == 1) 65498087a06SJeff Roberson vm_object_set_flag(object, 65598087a06SJeff Roberson OBJ_ONEMAPPING); 65689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 65723b186d3SAlan Cox return; 65895e5e988SJohn Dyson } 65998087a06SJeff Roberson 66098087a06SJeff Roberson /* Handle collapsing last ref on anonymous objects. */ 66198087a06SJeff Roberson object = vm_object_deallocate_anon(object); 66298087a06SJeff Roberson continue; 66398087a06SJeff Roberson } 66498087a06SJeff Roberson 66598087a06SJeff Roberson /* 66698087a06SJeff Roberson * Handle the final reference to an object. We restart 66798087a06SJeff Roberson * the loop with the backing object to avoid recursion. 66898087a06SJeff Roberson */ 6691bdbd705SKonstantin Belousov umtx_shm_object_terminated(object); 67024a1cce3SDavid Greenman temp = object->backing_object; 671c9917419SAlan Cox if (temp != NULL) { 6724b8365d7SKonstantin Belousov KASSERT(object->type != OBJT_SWAP_TMPFS, 6734bace8e7SKonstantin Belousov ("shadowed tmpfs v_object 2 %p", object)); 67451b867e5SJeff Roberson vm_object_backing_remove(object); 675de5f6a77SJohn Dyson } 67698087a06SJeff Roberson 67798087a06SJeff Roberson KASSERT((object->flags & OBJ_DEAD) == 0, 67898087a06SJeff Roberson ("vm_object_deallocate: Terminating dead object.")); 679783a68aaSKonstantin Belousov vm_object_set_flag(object, OBJ_DEAD); 680df8bae1dSRodney W. Grimes vm_object_terminate(object); 681df8bae1dSRodney W. Grimes object = temp; 682df8bae1dSRodney W. Grimes } 683df8bae1dSRodney W. Grimes } 684df8bae1dSRodney W. Grimes 685df8bae1dSRodney W. Grimes /* 6862ac78f0eSStephan Uphoff * vm_object_destroy removes the object from the global object list 6872ac78f0eSStephan Uphoff * and frees the space for the object. 6882ac78f0eSStephan Uphoff */ 6892ac78f0eSStephan Uphoff void 6902ac78f0eSStephan Uphoff vm_object_destroy(vm_object_t object) 6912ac78f0eSStephan Uphoff { 6922ac78f0eSStephan Uphoff 6932ac78f0eSStephan Uphoff /* 6943364c323SKonstantin Belousov * Release the allocation charge. 6953364c323SKonstantin Belousov */ 696ef694c1aSEdward Tomasz Napierala if (object->cred != NULL) { 697ef694c1aSEdward Tomasz Napierala swap_release_by_cred(object->charge, object->cred); 6983364c323SKonstantin Belousov object->charge = 0; 699ef694c1aSEdward Tomasz Napierala crfree(object->cred); 700ef694c1aSEdward Tomasz Napierala object->cred = NULL; 7013364c323SKonstantin Belousov } 7023364c323SKonstantin Belousov 7033364c323SKonstantin Belousov /* 7042ac78f0eSStephan Uphoff * Free the space for the object. 7052ac78f0eSStephan Uphoff */ 7062ac78f0eSStephan Uphoff uma_zfree(obj_zone, object); 7072ac78f0eSStephan Uphoff } 7082ac78f0eSStephan Uphoff 70951b867e5SJeff Roberson static void 71051b867e5SJeff Roberson vm_object_backing_remove_locked(vm_object_t object) 71151b867e5SJeff Roberson { 71251b867e5SJeff Roberson vm_object_t backing_object; 71351b867e5SJeff Roberson 71451b867e5SJeff Roberson backing_object = object->backing_object; 71551b867e5SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 71651b867e5SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(backing_object); 71751b867e5SJeff Roberson 71898087a06SJeff Roberson KASSERT((object->flags & OBJ_COLLAPSING) == 0, 71998087a06SJeff Roberson ("vm_object_backing_remove: Removing collapsing object.")); 72098087a06SJeff Roberson 72151b867e5SJeff Roberson if ((object->flags & OBJ_SHADOWLIST) != 0) { 72251b867e5SJeff Roberson LIST_REMOVE(object, shadow_list); 72351b867e5SJeff Roberson backing_object->shadow_count--; 72451b867e5SJeff Roberson object->flags &= ~OBJ_SHADOWLIST; 72551b867e5SJeff Roberson } 72651b867e5SJeff Roberson object->backing_object = NULL; 72751b867e5SJeff Roberson } 72851b867e5SJeff Roberson 72951b867e5SJeff Roberson static void 73051b867e5SJeff Roberson vm_object_backing_remove(vm_object_t object) 73151b867e5SJeff Roberson { 73251b867e5SJeff Roberson vm_object_t backing_object; 73351b867e5SJeff Roberson 73451b867e5SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 73551b867e5SJeff Roberson 73651b867e5SJeff Roberson if ((object->flags & OBJ_SHADOWLIST) != 0) { 73751b867e5SJeff Roberson backing_object = object->backing_object; 73851b867e5SJeff Roberson VM_OBJECT_WLOCK(backing_object); 73951b867e5SJeff Roberson vm_object_backing_remove_locked(object); 74051b867e5SJeff Roberson VM_OBJECT_WUNLOCK(backing_object); 74151b867e5SJeff Roberson } else 74251b867e5SJeff Roberson object->backing_object = NULL; 74351b867e5SJeff Roberson } 74451b867e5SJeff Roberson 74551b867e5SJeff Roberson static void 74651b867e5SJeff Roberson vm_object_backing_insert_locked(vm_object_t object, vm_object_t backing_object) 74751b867e5SJeff Roberson { 74851b867e5SJeff Roberson 74951b867e5SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 75051b867e5SJeff Roberson 75151b867e5SJeff Roberson if ((backing_object->flags & OBJ_ANON) != 0) { 75251b867e5SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(backing_object); 75351b867e5SJeff Roberson LIST_INSERT_HEAD(&backing_object->shadow_head, object, 75451b867e5SJeff Roberson shadow_list); 75551b867e5SJeff Roberson backing_object->shadow_count++; 75651b867e5SJeff Roberson object->flags |= OBJ_SHADOWLIST; 75751b867e5SJeff Roberson } 75851b867e5SJeff Roberson object->backing_object = backing_object; 75951b867e5SJeff Roberson } 76051b867e5SJeff Roberson 76151b867e5SJeff Roberson static void 76251b867e5SJeff Roberson vm_object_backing_insert(vm_object_t object, vm_object_t backing_object) 76351b867e5SJeff Roberson { 76451b867e5SJeff Roberson 76551b867e5SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 76651b867e5SJeff Roberson 76751b867e5SJeff Roberson if ((backing_object->flags & OBJ_ANON) != 0) { 76851b867e5SJeff Roberson VM_OBJECT_WLOCK(backing_object); 76951b867e5SJeff Roberson vm_object_backing_insert_locked(object, backing_object); 77051b867e5SJeff Roberson VM_OBJECT_WUNLOCK(backing_object); 77151b867e5SJeff Roberson } else 77251b867e5SJeff Roberson object->backing_object = backing_object; 77351b867e5SJeff Roberson } 77451b867e5SJeff Roberson 77598087a06SJeff Roberson /* 77698087a06SJeff Roberson * Insert an object into a backing_object's shadow list with an additional 77798087a06SJeff Roberson * reference to the backing_object added. 77898087a06SJeff Roberson */ 77998087a06SJeff Roberson static void 78098087a06SJeff Roberson vm_object_backing_insert_ref(vm_object_t object, vm_object_t backing_object) 78198087a06SJeff Roberson { 78298087a06SJeff Roberson 78398087a06SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 78498087a06SJeff Roberson 78598087a06SJeff Roberson if ((backing_object->flags & OBJ_ANON) != 0) { 78698087a06SJeff Roberson VM_OBJECT_WLOCK(backing_object); 78798087a06SJeff Roberson KASSERT((backing_object->flags & OBJ_DEAD) == 0, 78898087a06SJeff Roberson ("shadowing dead anonymous object")); 78998087a06SJeff Roberson vm_object_reference_locked(backing_object); 79098087a06SJeff Roberson vm_object_backing_insert_locked(object, backing_object); 79198087a06SJeff Roberson vm_object_clear_flag(backing_object, OBJ_ONEMAPPING); 79298087a06SJeff Roberson VM_OBJECT_WUNLOCK(backing_object); 79398087a06SJeff Roberson } else { 79498087a06SJeff Roberson vm_object_reference(backing_object); 79598087a06SJeff Roberson object->backing_object = backing_object; 79698087a06SJeff Roberson } 79798087a06SJeff Roberson } 79898087a06SJeff Roberson 79998087a06SJeff Roberson /* 80098087a06SJeff Roberson * Transfer a backing reference from backing_object to object. 80198087a06SJeff Roberson */ 80298087a06SJeff Roberson static void 80398087a06SJeff Roberson vm_object_backing_transfer(vm_object_t object, vm_object_t backing_object) 80498087a06SJeff Roberson { 80598087a06SJeff Roberson vm_object_t new_backing_object; 80698087a06SJeff Roberson 80798087a06SJeff Roberson /* 80898087a06SJeff Roberson * Note that the reference to backing_object->backing_object 80998087a06SJeff Roberson * moves from within backing_object to within object. 81098087a06SJeff Roberson */ 81198087a06SJeff Roberson vm_object_backing_remove_locked(object); 81298087a06SJeff Roberson new_backing_object = backing_object->backing_object; 81398087a06SJeff Roberson if (new_backing_object == NULL) 81498087a06SJeff Roberson return; 81598087a06SJeff Roberson if ((new_backing_object->flags & OBJ_ANON) != 0) { 81698087a06SJeff Roberson VM_OBJECT_WLOCK(new_backing_object); 81798087a06SJeff Roberson vm_object_backing_remove_locked(backing_object); 81898087a06SJeff Roberson vm_object_backing_insert_locked(object, new_backing_object); 81998087a06SJeff Roberson VM_OBJECT_WUNLOCK(new_backing_object); 82098087a06SJeff Roberson } else { 82198087a06SJeff Roberson object->backing_object = new_backing_object; 82298087a06SJeff Roberson backing_object->backing_object = NULL; 82398087a06SJeff Roberson } 82498087a06SJeff Roberson } 82598087a06SJeff Roberson 82698087a06SJeff Roberson /* 82798087a06SJeff Roberson * Wait for a concurrent collapse to settle. 82898087a06SJeff Roberson */ 82998087a06SJeff Roberson static void 83098087a06SJeff Roberson vm_object_collapse_wait(vm_object_t object) 83198087a06SJeff Roberson { 83298087a06SJeff Roberson 83398087a06SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 83498087a06SJeff Roberson 83598087a06SJeff Roberson while ((object->flags & OBJ_COLLAPSING) != 0) { 83698087a06SJeff Roberson vm_object_pip_wait(object, "vmcolwait"); 83798087a06SJeff Roberson counter_u64_add(object_collapse_waits, 1); 83898087a06SJeff Roberson } 83998087a06SJeff Roberson } 84098087a06SJeff Roberson 84198087a06SJeff Roberson /* 84298087a06SJeff Roberson * Waits for a backing object to clear a pending collapse and returns 84398087a06SJeff Roberson * it locked if it is an ANON object. 84498087a06SJeff Roberson */ 84598087a06SJeff Roberson static vm_object_t 84698087a06SJeff Roberson vm_object_backing_collapse_wait(vm_object_t object) 84798087a06SJeff Roberson { 84898087a06SJeff Roberson vm_object_t backing_object; 84998087a06SJeff Roberson 85098087a06SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 85198087a06SJeff Roberson 85298087a06SJeff Roberson for (;;) { 85398087a06SJeff Roberson backing_object = object->backing_object; 85498087a06SJeff Roberson if (backing_object == NULL || 85598087a06SJeff Roberson (backing_object->flags & OBJ_ANON) == 0) 85698087a06SJeff Roberson return (NULL); 85798087a06SJeff Roberson VM_OBJECT_WLOCK(backing_object); 85898087a06SJeff Roberson if ((backing_object->flags & (OBJ_DEAD | OBJ_COLLAPSING)) == 0) 85998087a06SJeff Roberson break; 860cdd02f43SMark Johnston VM_OBJECT_WUNLOCK(object); 86198087a06SJeff Roberson vm_object_pip_sleep(backing_object, "vmbckwait"); 86298087a06SJeff Roberson counter_u64_add(object_collapse_waits, 1); 86398087a06SJeff Roberson VM_OBJECT_WLOCK(object); 86498087a06SJeff Roberson } 86598087a06SJeff Roberson return (backing_object); 86698087a06SJeff Roberson } 86751b867e5SJeff Roberson 8682ac78f0eSStephan Uphoff /* 8697bbdb843SRuslan Bukin * vm_object_terminate_pages removes any remaining pageable pages 8707bbdb843SRuslan Bukin * from the object and resets the object to an empty state. 8717bbdb843SRuslan Bukin */ 8727bbdb843SRuslan Bukin static void 8737bbdb843SRuslan Bukin vm_object_terminate_pages(vm_object_t object) 8747bbdb843SRuslan Bukin { 8757bbdb843SRuslan Bukin vm_page_t p, p_next; 8767bbdb843SRuslan Bukin 8777bbdb843SRuslan Bukin VM_OBJECT_ASSERT_WLOCKED(object); 8787bbdb843SRuslan Bukin 8797bbdb843SRuslan Bukin /* 8807bbdb843SRuslan Bukin * Free any remaining pageable pages. This also removes them from the 8817bbdb843SRuslan Bukin * paging queues. However, don't free wired pages, just remove them 8827bbdb843SRuslan Bukin * from the object. Rather than incrementally removing each page from 8837bbdb843SRuslan Bukin * the object, the page and object are reset to any empty state. 8847bbdb843SRuslan Bukin */ 8857bbdb843SRuslan Bukin TAILQ_FOREACH_SAFE(p, &object->memq, listq, p_next) { 8867bbdb843SRuslan Bukin vm_page_assert_unbusied(p); 887fee2a2faSMark Johnston KASSERT(p->object == object && 888fee2a2faSMark Johnston (p->ref_count & VPRC_OBJREF) != 0, 889fee2a2faSMark Johnston ("vm_object_terminate_pages: page %p is inconsistent", p)); 890fee2a2faSMark Johnston 8917bbdb843SRuslan Bukin p->object = NULL; 892fee2a2faSMark Johnston if (vm_page_drop(p, VPRC_OBJREF) == VPRC_OBJREF) { 8935cd29d0fSMark Johnston VM_CNT_INC(v_pfree); 8945cd29d0fSMark Johnston vm_page_free(p); 8954074d642SAlan Cox } 896fee2a2faSMark Johnston } 8972fcd1ff6SKonstantin Belousov 8987bbdb843SRuslan Bukin /* 8997bbdb843SRuslan Bukin * If the object contained any pages, then reset it to an empty state. 9007bbdb843SRuslan Bukin * None of the object's fields, including "resident_page_count", were 9017bbdb843SRuslan Bukin * modified by the preceding loop. 9027bbdb843SRuslan Bukin */ 9037bbdb843SRuslan Bukin if (object->resident_page_count != 0) { 9047bbdb843SRuslan Bukin vm_radix_reclaim_allnodes(&object->rtree); 9057bbdb843SRuslan Bukin TAILQ_INIT(&object->memq); 9067bbdb843SRuslan Bukin object->resident_page_count = 0; 9077bbdb843SRuslan Bukin if (object->type == OBJT_VNODE) 9087bbdb843SRuslan Bukin vdrop(object->handle); 9097bbdb843SRuslan Bukin } 9107bbdb843SRuslan Bukin } 9117bbdb843SRuslan Bukin 9127bbdb843SRuslan Bukin /* 913df8bae1dSRodney W. Grimes * vm_object_terminate actually destroys the specified object, freeing 914df8bae1dSRodney W. Grimes * up all previously used resources. 915df8bae1dSRodney W. Grimes * 916df8bae1dSRodney W. Grimes * The object must be locked. 9171c7c3c6aSMatthew Dillon * This routine may block. 918df8bae1dSRodney W. Grimes */ 91995e5e988SJohn Dyson void 9201b40f8c0SMatthew Dillon vm_object_terminate(vm_object_t object) 921df8bae1dSRodney W. Grimes { 92298087a06SJeff Roberson 92389f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 924783a68aaSKonstantin Belousov KASSERT((object->flags & OBJ_DEAD) != 0, 925783a68aaSKonstantin Belousov ("terminating non-dead obj %p", object)); 92698087a06SJeff Roberson KASSERT((object->flags & OBJ_COLLAPSING) == 0, 92798087a06SJeff Roberson ("terminating collapsing obj %p", object)); 92898087a06SJeff Roberson KASSERT(object->backing_object == NULL, 92998087a06SJeff Roberson ("terminating shadow obj %p", object)); 930bef608bdSJohn Dyson 931cf27e0d1SJeff Roberson /* 932ffae7ea9SKonstantin Belousov * Wait for the pageout daemon and other current users to be 933ffae7ea9SKonstantin Belousov * done with the object. Note that new paging_in_progress 934ffae7ea9SKonstantin Belousov * users can come after this wait, but they must check 935ffae7ea9SKonstantin Belousov * OBJ_DEAD flag set (without unlocking the object), and avoid 936ffae7ea9SKonstantin Belousov * the object being terminated. 937cf27e0d1SJeff Roberson */ 938cf27e0d1SJeff Roberson vm_object_pip_wait(object, "objtrm"); 939cf27e0d1SJeff Roberson 940971dd342SAlfred Perlstein KASSERT(object->ref_count == 0, 941971dd342SAlfred Perlstein ("vm_object_terminate: object with references, ref_count=%d", 942971dd342SAlfred Perlstein object->ref_count)); 943996c772fSJohn Dyson 9447bbdb843SRuslan Bukin if ((object->flags & OBJ_PG_DTOR) == 0) 9457bbdb843SRuslan Bukin vm_object_terminate_pages(object); 946bef608bdSJohn Dyson 947f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 948f8a47341SAlan Cox if (__predict_false(!LIST_EMPTY(&object->rvq))) 949f8a47341SAlan Cox vm_reserv_break_all(object); 950f8a47341SAlan Cox #endif 9517bfda801SAlan Cox 952e735691bSJohn Baldwin KASSERT(object->cred == NULL || object->type == OBJT_DEFAULT || 9534b8365d7SKonstantin Belousov object->type == OBJT_SWAP || object->type == OBJT_SWAP_TMPFS, 954e735691bSJohn Baldwin ("%s: non-swap obj %p has cred", __func__, object)); 955e735691bSJohn Baldwin 9562d8acc0fSJohn Dyson /* 9579fcfb650SDavid Greenman * Let the pager know object is dead. 9589fcfb650SDavid Greenman */ 9599fcfb650SDavid Greenman vm_pager_deallocate(object); 96089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 9619fcfb650SDavid Greenman 9622ac78f0eSStephan Uphoff vm_object_destroy(object); 96347221757SJohn Dyson } 964df8bae1dSRodney W. Grimes 965edf93b25SAlan Cox /* 966edf93b25SAlan Cox * Make the page read-only so that we can clear the object flags. However, if 967edf93b25SAlan Cox * this is a nosync mmap then the object is likely to stay dirty so do not 968edf93b25SAlan Cox * mess with the page and do not clear the object flags. Returns TRUE if the 969edf93b25SAlan Cox * page should be flushed, and FALSE otherwise. 970edf93b25SAlan Cox */ 9713280870dSKonstantin Belousov static boolean_t 97267d0e293SJeff Roberson vm_object_page_remove_write(vm_page_t p, int flags, boolean_t *allclean) 9733280870dSKonstantin Belousov { 9743280870dSKonstantin Belousov 975fff5403fSJeff Roberson vm_page_assert_busied(p); 976fff5403fSJeff Roberson 9773280870dSKonstantin Belousov /* 9783280870dSKonstantin Belousov * If we have been asked to skip nosync pages and this is a 9793280870dSKonstantin Belousov * nosync page, skip it. Note that the object flags were not 9803280870dSKonstantin Belousov * cleared in this case so we do not have to set them. 9813280870dSKonstantin Belousov */ 9825cff1f4dSMark Johnston if ((flags & OBJPC_NOSYNC) != 0 && (p->a.flags & PGA_NOSYNC) != 0) { 98367d0e293SJeff Roberson *allclean = FALSE; 9843280870dSKonstantin Belousov return (FALSE); 9853280870dSKonstantin Belousov } else { 9863280870dSKonstantin Belousov pmap_remove_write(p); 9873280870dSKonstantin Belousov return (p->dirty != 0); 9883280870dSKonstantin Belousov } 9893280870dSKonstantin Belousov } 9903280870dSKonstantin Belousov 991df8bae1dSRodney W. Grimes /* 992df8bae1dSRodney W. Grimes * vm_object_page_clean 993df8bae1dSRodney W. Grimes * 9944f79d873SMatthew Dillon * Clean all dirty pages in the specified range of object. Leaves page 9954f79d873SMatthew Dillon * on whatever queue it is currently on. If NOSYNC is set then do not 996fff5403fSJeff Roberson * write out pages with PGA_NOSYNC set (originally comes from MAP_NOSYNC), 9974f79d873SMatthew Dillon * leaving the object dirty. 99826f9a767SRodney W. Grimes * 9998d34a3bfSKonstantin Belousov * For swap objects backing tmpfs regular files, do not flush anything, 10008d34a3bfSKonstantin Belousov * but remove write protection on the mapped pages to update mtime through 10018d34a3bfSKonstantin Belousov * mmaped writes. 10028d34a3bfSKonstantin Belousov * 100343b7990eSMatthew Dillon * When stuffing pages asynchronously, allow clustering. XXX we need a 100443b7990eSMatthew Dillon * synchronous clustering mode implementation. 100543b7990eSMatthew Dillon * 100626f9a767SRodney W. Grimes * Odd semantics: if start == end, we clean everything. 100726f9a767SRodney W. Grimes * 100826f9a767SRodney W. Grimes * The object must be locked. 1009126d6082SKonstantin Belousov * 1010126d6082SKonstantin Belousov * Returns FALSE if some page from the range was not written, as 1011126d6082SKonstantin Belousov * reported by the pager, and TRUE otherwise. 101226f9a767SRodney W. Grimes */ 1013126d6082SKonstantin Belousov boolean_t 101417f3095dSAlan Cox vm_object_page_clean(vm_object_t object, vm_ooffset_t start, vm_ooffset_t end, 1015e239bb97SKonstantin Belousov int flags) 1016f6b04d2bSDavid Greenman { 1017e239bb97SKonstantin Belousov vm_page_t np, p; 101817f3095dSAlan Cox vm_pindex_t pi, tend, tstart; 1019126d6082SKonstantin Belousov int curgeneration, n, pagerflags; 102067d0e293SJeff Roberson boolean_t eio, res, allclean; 1021f6b04d2bSDavid Greenman 102289f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 1023e5f299ffSKonstantin Belousov 10248d34a3bfSKonstantin Belousov if (!vm_object_mightbedirty(object) || object->resident_page_count == 0) 1025126d6082SKonstantin Belousov return (TRUE); 1026f6b04d2bSDavid Greenman 1027e239bb97SKonstantin Belousov pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) != 0 ? 1028e239bb97SKonstantin Belousov VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK; 1029e239bb97SKonstantin Belousov pagerflags |= (flags & OBJPC_INVAL) != 0 ? VM_PAGER_PUT_INVAL : 0; 1030e239bb97SKonstantin Belousov 103117f3095dSAlan Cox tstart = OFF_TO_IDX(start); 103217f3095dSAlan Cox tend = (end == 0) ? object->size : OFF_TO_IDX(end + PAGE_MASK); 103367d0e293SJeff Roberson allclean = tstart == 0 && tend >= object->size; 1034126d6082SKonstantin Belousov res = TRUE; 1035f6b04d2bSDavid Greenman 1036bd7e5f99SJohn Dyson rescan: 10372d8acc0fSJohn Dyson curgeneration = object->generation; 10382d8acc0fSJohn Dyson 103917f3095dSAlan Cox for (p = vm_page_find_least(object, tstart); p != NULL; p = np) { 1040bd7e5f99SJohn Dyson pi = p->pindex; 1041e239bb97SKonstantin Belousov if (pi >= tend) 1042e239bb97SKonstantin Belousov break; 1043e239bb97SKonstantin Belousov np = TAILQ_NEXT(p, listq); 10440012f373SJeff Roberson if (vm_page_none_valid(p)) 1045aef922f5SJohn Dyson continue; 104663e97555SJeff Roberson if (vm_page_busy_acquire(p, VM_ALLOC_WAITFAIL) == 0) { 104767d0e293SJeff Roberson if (object->generation != curgeneration && 104867d0e293SJeff Roberson (flags & OBJPC_SYNC) != 0) 1049e239bb97SKonstantin Belousov goto rescan; 1050780636b7SKonstantin Belousov np = vm_page_find_least(object, pi); 1051780636b7SKonstantin Belousov continue; 1052f6b04d2bSDavid Greenman } 105367d0e293SJeff Roberson if (!vm_object_page_remove_write(p, flags, &allclean)) { 105463e97555SJeff Roberson vm_page_xunbusy(p); 1055bd7e5f99SJohn Dyson continue; 105663e97555SJeff Roberson } 10578d34a3bfSKonstantin Belousov if (object->type == OBJT_VNODE) { 10583280870dSKonstantin Belousov n = vm_object_page_collect_flush(object, p, pagerflags, 105967d0e293SJeff Roberson flags, &allclean, &eio); 1060126d6082SKonstantin Belousov if (eio) { 1061126d6082SKonstantin Belousov res = FALSE; 106267d0e293SJeff Roberson allclean = FALSE; 1063126d6082SKonstantin Belousov } 106467d0e293SJeff Roberson if (object->generation != curgeneration && 106567d0e293SJeff Roberson (flags & OBJPC_SYNC) != 0) 1066b9b7a4beSMatthew Dillon goto rescan; 1067031ec8c1SKonstantin Belousov 1068031ec8c1SKonstantin Belousov /* 1069031ec8c1SKonstantin Belousov * If the VOP_PUTPAGES() did a truncated write, so 1070031ec8c1SKonstantin Belousov * that even the first page of the run is not fully 1071031ec8c1SKonstantin Belousov * written, vm_pageout_flush() returns 0 as the run 1072031ec8c1SKonstantin Belousov * length. Since the condition that caused truncated 1073031ec8c1SKonstantin Belousov * write may be permanent, e.g. exhausted free space, 1074031ec8c1SKonstantin Belousov * accepting n == 0 would cause an infinite loop. 1075031ec8c1SKonstantin Belousov * 1076031ec8c1SKonstantin Belousov * Forwarding the iterator leaves the unwritten page 1077031ec8c1SKonstantin Belousov * behind, but there is not much we can do there if 1078031ec8c1SKonstantin Belousov * filesystem refuses to write it. 1079031ec8c1SKonstantin Belousov */ 1080126d6082SKonstantin Belousov if (n == 0) { 1081031ec8c1SKonstantin Belousov n = 1; 108267d0e293SJeff Roberson allclean = FALSE; 1083126d6082SKonstantin Belousov } 10848d34a3bfSKonstantin Belousov } else { 10858d34a3bfSKonstantin Belousov n = 1; 10868d34a3bfSKonstantin Belousov vm_page_xunbusy(p); 10878d34a3bfSKonstantin Belousov } 1088e239bb97SKonstantin Belousov np = vm_page_find_least(object, pi + n); 1089b9b7a4beSMatthew Dillon } 1090b9b7a4beSMatthew Dillon #if 0 1091e239bb97SKonstantin Belousov VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC) ? MNT_WAIT : 0); 1092b9b7a4beSMatthew Dillon #endif 1093b9b7a4beSMatthew Dillon 10948d34a3bfSKonstantin Belousov /* 10958d34a3bfSKonstantin Belousov * Leave updating cleangeneration for tmpfs objects to tmpfs 10968d34a3bfSKonstantin Belousov * scan. It needs to update mtime, which happens for other 10978d34a3bfSKonstantin Belousov * filesystems during page writeouts. 10988d34a3bfSKonstantin Belousov */ 10998d34a3bfSKonstantin Belousov if (allclean && object->type == OBJT_VNODE) 110067d0e293SJeff Roberson object->cleangeneration = curgeneration; 1101126d6082SKonstantin Belousov return (res); 1102b9b7a4beSMatthew Dillon } 1103b9b7a4beSMatthew Dillon 1104b9b7a4beSMatthew Dillon static int 11053280870dSKonstantin Belousov vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int pagerflags, 110667d0e293SJeff Roberson int flags, boolean_t *allclean, boolean_t *eio) 1107b9b7a4beSMatthew Dillon { 11083157c503SKonstantin Belousov vm_page_t ma[vm_pageout_page_count], p_first, tp; 11093157c503SKonstantin Belousov int count, i, mreq, runlen; 1110b9b7a4beSMatthew Dillon 11117bec141bSKip Macy vm_page_lock_assert(p, MA_NOTOWNED); 111263e97555SJeff Roberson vm_page_assert_xbusied(p); 111389f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 11143157c503SKonstantin Belousov 11153157c503SKonstantin Belousov count = 1; 11163157c503SKonstantin Belousov mreq = 0; 11173157c503SKonstantin Belousov 11183157c503SKonstantin Belousov for (tp = p; count < vm_pageout_page_count; count++) { 11193157c503SKonstantin Belousov tp = vm_page_next(tp); 112063e97555SJeff Roberson if (tp == NULL || vm_page_tryxbusy(tp) == 0) 1121bd7e5f99SJohn Dyson break; 112267d0e293SJeff Roberson if (!vm_object_page_remove_write(tp, flags, allclean)) { 112363e97555SJeff Roberson vm_page_xunbusy(tp); 1124bd7e5f99SJohn Dyson break; 1125bd7e5f99SJohn Dyson } 112663e97555SJeff Roberson } 1127aef922f5SJohn Dyson 11283157c503SKonstantin Belousov for (p_first = p; count < vm_pageout_page_count; count++) { 11293157c503SKonstantin Belousov tp = vm_page_prev(p_first); 113063e97555SJeff Roberson if (tp == NULL || vm_page_tryxbusy(tp) == 0) 1131bd7e5f99SJohn Dyson break; 113267d0e293SJeff Roberson if (!vm_object_page_remove_write(tp, flags, allclean)) { 113363e97555SJeff Roberson vm_page_xunbusy(tp); 1134bd7e5f99SJohn Dyson break; 113563e97555SJeff Roberson } 11363157c503SKonstantin Belousov p_first = tp; 11373157c503SKonstantin Belousov mreq++; 1138bd7e5f99SJohn Dyson } 1139bd7e5f99SJohn Dyson 11403157c503SKonstantin Belousov for (tp = p_first, i = 0; i < count; tp = TAILQ_NEXT(tp, listq), i++) 11413157c503SKonstantin Belousov ma[i] = tp; 1142cf2819ccSJohn Dyson 1143126d6082SKonstantin Belousov vm_pageout_flush(ma, count, pagerflags, mreq, &runlen, eio); 11441e8a675cSKonstantin Belousov return (runlen); 114526f9a767SRodney W. Grimes } 1146df8bae1dSRodney W. Grimes 11471efb74fbSJohn Dyson /* 1148950f8459SAlan Cox * Note that there is absolutely no sense in writing out 1149950f8459SAlan Cox * anonymous objects, so we track down the vnode object 1150950f8459SAlan Cox * to write out. 1151950f8459SAlan Cox * We invalidate (remove) all pages from the address space 1152950f8459SAlan Cox * for semantic correctness. 1153950f8459SAlan Cox * 11546bbee8e2SAlan Cox * If the backing object is a device object with unmanaged pages, then any 11556bbee8e2SAlan Cox * mappings to the specified range of pages must be removed before this 11566bbee8e2SAlan Cox * function is called. 11576bbee8e2SAlan Cox * 1158950f8459SAlan Cox * Note: certain anonymous maps, such as MAP_NOSYNC maps, 1159950f8459SAlan Cox * may start out with a NULL object. 1160950f8459SAlan Cox */ 1161126d6082SKonstantin Belousov boolean_t 1162950f8459SAlan Cox vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size, 1163950f8459SAlan Cox boolean_t syncio, boolean_t invalidate) 1164950f8459SAlan Cox { 1165950f8459SAlan Cox vm_object_t backing_object; 1166950f8459SAlan Cox struct vnode *vp; 11673b582b4eSTor Egge struct mount *mp; 1168126d6082SKonstantin Belousov int error, flags, fsync_after; 1169126d6082SKonstantin Belousov boolean_t res; 1170950f8459SAlan Cox 1171950f8459SAlan Cox if (object == NULL) 1172126d6082SKonstantin Belousov return (TRUE); 1173126d6082SKonstantin Belousov res = TRUE; 1174126d6082SKonstantin Belousov error = 0; 117589f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1176950f8459SAlan Cox while ((backing_object = object->backing_object) != NULL) { 117789f6b863SAttilio Rao VM_OBJECT_WLOCK(backing_object); 117856e0670fSAlan Cox offset += object->backing_object_offset; 117989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1180950f8459SAlan Cox object = backing_object; 1181950f8459SAlan Cox if (object->size < OFF_TO_IDX(offset + size)) 1182950f8459SAlan Cox size = IDX_TO_OFF(object->size) - offset; 1183950f8459SAlan Cox } 1184950f8459SAlan Cox /* 1185950f8459SAlan Cox * Flush pages if writing is allowed, invalidate them 1186950f8459SAlan Cox * if invalidation requested. Pages undergoing I/O 1187950f8459SAlan Cox * will be ignored by vm_object_page_remove(). 1188950f8459SAlan Cox * 1189950f8459SAlan Cox * We cannot lock the vnode and then wait for paging 1190950f8459SAlan Cox * to complete without deadlocking against vm_fault. 1191950f8459SAlan Cox * Instead we simply call vm_object_page_remove() and 1192950f8459SAlan Cox * allow it to block internally on a page-by-page 1193950f8459SAlan Cox * basis when it encounters pages undergoing async 1194950f8459SAlan Cox * I/O. 1195950f8459SAlan Cox */ 1196950f8459SAlan Cox if (object->type == OBJT_VNODE && 119767d0e293SJeff Roberson vm_object_mightbedirty(object) != 0 && 11985bf94937SKonstantin Belousov ((vp = object->handle)->v_vflag & VV_NOSYNC) == 0) { 119989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 12003b582b4eSTor Egge (void) vn_start_write(vp, &mp, V_WAIT); 1201cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 120275ff604aSKonstantin Belousov if (syncio && !invalidate && offset == 0 && 1203d1780e8dSKonstantin Belousov atop(size) == object->size) { 120475ff604aSKonstantin Belousov /* 120575ff604aSKonstantin Belousov * If syncing the whole mapping of the file, 120675ff604aSKonstantin Belousov * it is faster to schedule all the writes in 120775ff604aSKonstantin Belousov * async mode, also allowing the clustering, 120875ff604aSKonstantin Belousov * and then wait for i/o to complete. 120975ff604aSKonstantin Belousov */ 121075ff604aSKonstantin Belousov flags = 0; 121175ff604aSKonstantin Belousov fsync_after = TRUE; 121275ff604aSKonstantin Belousov } else { 1213950f8459SAlan Cox flags = (syncio || invalidate) ? OBJPC_SYNC : 0; 121475ff604aSKonstantin Belousov flags |= invalidate ? (OBJPC_SYNC | OBJPC_INVAL) : 0; 121575ff604aSKonstantin Belousov fsync_after = FALSE; 121675ff604aSKonstantin Belousov } 121789f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1218126d6082SKonstantin Belousov res = vm_object_page_clean(object, offset, offset + size, 1219126d6082SKonstantin Belousov flags); 122089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 122175ff604aSKonstantin Belousov if (fsync_after) 1222126d6082SKonstantin Belousov error = VOP_FSYNC(vp, MNT_WAIT, curthread); 1223b249ce48SMateusz Guzik VOP_UNLOCK(vp); 12243b582b4eSTor Egge vn_finished_write(mp); 1225126d6082SKonstantin Belousov if (error != 0) 1226126d6082SKonstantin Belousov res = FALSE; 122789f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1228950f8459SAlan Cox } 1229950f8459SAlan Cox if ((object->type == OBJT_VNODE || 1230950f8459SAlan Cox object->type == OBJT_DEVICE) && invalidate) { 12316bbee8e2SAlan Cox if (object->type == OBJT_DEVICE) 12326bbee8e2SAlan Cox /* 12336bbee8e2SAlan Cox * The option OBJPR_NOTMAPPED must be passed here 12346bbee8e2SAlan Cox * because vm_object_page_remove() cannot remove 12356bbee8e2SAlan Cox * unmanaged mappings. 12366bbee8e2SAlan Cox */ 12376bbee8e2SAlan Cox flags = OBJPR_NOTMAPPED; 12386bbee8e2SAlan Cox else if (old_msync) 12396195b24aSKonstantin Belousov flags = 0; 12406bbee8e2SAlan Cox else 12416195b24aSKonstantin Belousov flags = OBJPR_CLEANONLY; 12426bbee8e2SAlan Cox vm_object_page_remove(object, OFF_TO_IDX(offset), 12436bbee8e2SAlan Cox OFF_TO_IDX(offset + size + PAGE_MASK), flags); 1244950f8459SAlan Cox } 124589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1246126d6082SKonstantin Belousov return (res); 1247950f8459SAlan Cox } 1248950f8459SAlan Cox 1249950f8459SAlan Cox /* 1250aa3650eaSMark Johnston * Determine whether the given advice can be applied to the object. Advice is 1251aa3650eaSMark Johnston * not applied to unmanaged pages since they never belong to page queues, and 1252aa3650eaSMark Johnston * since MADV_FREE is destructive, it can apply only to anonymous pages that 1253aa3650eaSMark Johnston * have been mapped at most once. 1254aa3650eaSMark Johnston */ 1255aa3650eaSMark Johnston static bool 1256aa3650eaSMark Johnston vm_object_advice_applies(vm_object_t object, int advice) 1257aa3650eaSMark Johnston { 1258aa3650eaSMark Johnston 1259aa3650eaSMark Johnston if ((object->flags & OBJ_UNMANAGED) != 0) 1260aa3650eaSMark Johnston return (false); 1261aa3650eaSMark Johnston if (advice != MADV_FREE) 1262aa3650eaSMark Johnston return (true); 126363967687SJeff Roberson return ((object->flags & (OBJ_ONEMAPPING | OBJ_ANON)) == 126463967687SJeff Roberson (OBJ_ONEMAPPING | OBJ_ANON)); 1265aa3650eaSMark Johnston } 1266aa3650eaSMark Johnston 1267aa3650eaSMark Johnston static void 1268aa3650eaSMark Johnston vm_object_madvise_freespace(vm_object_t object, int advice, vm_pindex_t pindex, 1269aa3650eaSMark Johnston vm_size_t size) 1270aa3650eaSMark Johnston { 1271aa3650eaSMark Johnston 12721390a5cbSKonstantin Belousov if (advice == MADV_FREE) 12731390a5cbSKonstantin Belousov vm_pager_freespace(object, pindex, size); 1274aa3650eaSMark Johnston } 1275aa3650eaSMark Johnston 1276aa3650eaSMark Johnston /* 1277867a482dSJohn Dyson * vm_object_madvise: 1278867a482dSJohn Dyson * 1279867a482dSJohn Dyson * Implements the madvise function at the object/page level. 12801c7c3c6aSMatthew Dillon * 1281193b9358SAlan Cox * MADV_WILLNEED (any object) 1282193b9358SAlan Cox * 1283193b9358SAlan Cox * Activate the specified pages if they are resident. 1284193b9358SAlan Cox * 1285193b9358SAlan Cox * MADV_DONTNEED (any object) 1286193b9358SAlan Cox * 1287193b9358SAlan Cox * Deactivate the specified pages if they are resident. 1288193b9358SAlan Cox * 1289193b9358SAlan Cox * MADV_FREE (OBJT_DEFAULT/OBJT_SWAP objects, 1290193b9358SAlan Cox * OBJ_ONEMAPPING only) 1291193b9358SAlan Cox * 1292193b9358SAlan Cox * Deactivate and clean the specified pages if they are 1293193b9358SAlan Cox * resident. This permits the process to reuse the pages 1294193b9358SAlan Cox * without faulting or the kernel to reclaim the pages 1295193b9358SAlan Cox * without I/O. 1296867a482dSJohn Dyson */ 1297867a482dSJohn Dyson void 129892a59946SJohn Baldwin vm_object_madvise(vm_object_t object, vm_pindex_t pindex, vm_pindex_t end, 1299c2655a40SMark Johnston int advice) 1300867a482dSJohn Dyson { 130192a59946SJohn Baldwin vm_pindex_t tpindex; 130234567de7SAlan Cox vm_object_t backing_object, tobject; 1303aa3650eaSMark Johnston vm_page_t m, tm; 1304867a482dSJohn Dyson 1305867a482dSJohn Dyson if (object == NULL) 1306867a482dSJohn Dyson return; 1307c2655a40SMark Johnston 13086e20a165SJohn Dyson relookup: 1309aa3650eaSMark Johnston VM_OBJECT_WLOCK(object); 1310aa3650eaSMark Johnston if (!vm_object_advice_applies(object, advice)) { 1311aa3650eaSMark Johnston VM_OBJECT_WUNLOCK(object); 1312aa3650eaSMark Johnston return; 13136e20a165SJohn Dyson } 1314aa3650eaSMark Johnston for (m = vm_page_find_least(object, pindex); pindex < end; pindex++) { 1315aa3650eaSMark Johnston tobject = object; 1316c2655a40SMark Johnston 13171ce137beSMatthew Dillon /* 1318aa3650eaSMark Johnston * If the next page isn't resident in the top-level object, we 1319aa3650eaSMark Johnston * need to search the shadow chain. When applying MADV_FREE, we 1320aa3650eaSMark Johnston * take care to release any swap space used to store 1321aa3650eaSMark Johnston * non-resident pages. 1322aa3650eaSMark Johnston */ 1323aa3650eaSMark Johnston if (m == NULL || pindex < m->pindex) { 1324aa3650eaSMark Johnston /* 1325aa3650eaSMark Johnston * Optimize a common case: if the top-level object has 1326aa3650eaSMark Johnston * no backing object, we can skip over the non-resident 1327aa3650eaSMark Johnston * range in constant time. 13281ce137beSMatthew Dillon */ 1329c2655a40SMark Johnston if (object->backing_object == NULL) { 1330c2655a40SMark Johnston tpindex = (m != NULL && m->pindex < end) ? 1331c2655a40SMark Johnston m->pindex : end; 1332aa3650eaSMark Johnston vm_object_madvise_freespace(object, advice, 1333aa3650eaSMark Johnston pindex, tpindex - pindex); 1334c2655a40SMark Johnston if ((pindex = tpindex) == end) 1335c2655a40SMark Johnston break; 1336aa3650eaSMark Johnston goto next_page; 1337aa3650eaSMark Johnston } 1338aa3650eaSMark Johnston 1339aa3650eaSMark Johnston tpindex = pindex; 1340aa3650eaSMark Johnston do { 1341aa3650eaSMark Johnston vm_object_madvise_freespace(tobject, advice, 1342aa3650eaSMark Johnston tpindex, 1); 13431ce137beSMatthew Dillon /* 1344aa3650eaSMark Johnston * Prepare to search the next object in the 1345aa3650eaSMark Johnston * chain. 13461ce137beSMatthew Dillon */ 134734567de7SAlan Cox backing_object = tobject->backing_object; 134834567de7SAlan Cox if (backing_object == NULL) 1349aa3650eaSMark Johnston goto next_pindex; 135089f6b863SAttilio Rao VM_OBJECT_WLOCK(backing_object); 1351aa3650eaSMark Johnston tpindex += 1352aa3650eaSMark Johnston OFF_TO_IDX(tobject->backing_object_offset); 13539b98b796SAlan Cox if (tobject != object) 135489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(tobject); 135534567de7SAlan Cox tobject = backing_object; 1356aa3650eaSMark Johnston if (!vm_object_advice_applies(tobject, advice)) 1357aa3650eaSMark Johnston goto next_pindex; 1358aa3650eaSMark Johnston } while ((tm = vm_page_lookup(tobject, tpindex)) == 1359aa3650eaSMark Johnston NULL); 1360aa3650eaSMark Johnston } else { 1361aa3650eaSMark Johnston next_page: 1362aa3650eaSMark Johnston tm = m; 1363aa3650eaSMark Johnston m = TAILQ_NEXT(m, listq); 1364c2655a40SMark Johnston } 1365c2655a40SMark Johnston 1366867a482dSJohn Dyson /* 13670012f373SJeff Roberson * If the page is not in a normal state, skip it. The page 13680012f373SJeff Roberson * can not be invalidated while the object lock is held. 1369867a482dSJohn Dyson */ 13700012f373SJeff Roberson if (!vm_page_all_valid(tm) || vm_page_wired(tm)) 1371aa3650eaSMark Johnston goto next_pindex; 1372aa3650eaSMark Johnston KASSERT((tm->flags & PG_FICTITIOUS) == 0, 1373aa3650eaSMark Johnston ("vm_object_madvise: page %p is fictitious", tm)); 1374aa3650eaSMark Johnston KASSERT((tm->oflags & VPO_UNMANAGED) == 0, 1375aa3650eaSMark Johnston ("vm_object_madvise: page %p is not managed", tm)); 137663e97555SJeff Roberson if (vm_page_tryxbusy(tm) == 0) { 1377aa3650eaSMark Johnston if (object != tobject) 1378aa3650eaSMark Johnston VM_OBJECT_WUNLOCK(object); 1379c2655a40SMark Johnston if (advice == MADV_WILLNEED) { 1380b11b56b5SAlan Cox /* 1381b11b56b5SAlan Cox * Reference the page before unlocking and 1382b11b56b5SAlan Cox * sleeping so that the page daemon is less 1383b11b56b5SAlan Cox * likely to reclaim it. 1384b11b56b5SAlan Cox */ 1385aa3650eaSMark Johnston vm_page_aflag_set(tm, PGA_REFERENCED); 1386567e51e1SAlan Cox } 1387aa3650eaSMark Johnston vm_page_busy_sleep(tm, "madvpo", false); 13886e20a165SJohn Dyson goto relookup; 138934567de7SAlan Cox } 1390aa3650eaSMark Johnston vm_page_advise(tm, advice); 139163e97555SJeff Roberson vm_page_xunbusy(tm); 1392aa3650eaSMark Johnston vm_object_madvise_freespace(tobject, advice, tm->pindex, 1); 1393aa3650eaSMark Johnston next_pindex: 13949b98b796SAlan Cox if (tobject != object) 139589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(tobject); 1396867a482dSJohn Dyson } 139789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1398867a482dSJohn Dyson } 1399867a482dSJohn Dyson 1400867a482dSJohn Dyson /* 1401df8bae1dSRodney W. Grimes * vm_object_shadow: 1402df8bae1dSRodney W. Grimes * 1403df8bae1dSRodney W. Grimes * Create a new object which is backed by the 1404df8bae1dSRodney W. Grimes * specified existing object range. The source 1405df8bae1dSRodney W. Grimes * object reference is deallocated. 1406df8bae1dSRodney W. Grimes * 1407df8bae1dSRodney W. Grimes * The new object and offset into that object 1408df8bae1dSRodney W. Grimes * are returned in the source parameters. 1409df8bae1dSRodney W. Grimes */ 141026f9a767SRodney W. Grimes void 141167388836SKonstantin Belousov vm_object_shadow(vm_object_t *object, vm_ooffset_t *offset, vm_size_t length, 141267388836SKonstantin Belousov struct ucred *cred, bool shared) 1413df8bae1dSRodney W. Grimes { 1414d031cff1SMatthew Dillon vm_object_t source; 1415d031cff1SMatthew Dillon vm_object_t result; 1416df8bae1dSRodney W. Grimes 1417df8bae1dSRodney W. Grimes source = *object; 1418df8bae1dSRodney W. Grimes 1419df8bae1dSRodney W. Grimes /* 14209a2f6362SAlan Cox * Don't create the new object if the old object isn't shared. 142163967687SJeff Roberson * 142263967687SJeff Roberson * If we hold the only reference we can guarantee that it won't 142363967687SJeff Roberson * increase while we have the map locked. Otherwise the race is 142463967687SJeff Roberson * harmless and we will end up with an extra shadow object that 142563967687SJeff Roberson * will be collapsed later. 14269a2f6362SAlan Cox */ 142763967687SJeff Roberson if (source != NULL && source->ref_count == 1 && 142832362449SKonstantin Belousov (source->flags & OBJ_ANON) != 0) 14299a2f6362SAlan Cox return; 14309a2f6362SAlan Cox 14319a2f6362SAlan Cox /* 1432570a2f4aSAlan Cox * Allocate a new object with the given length. 1433df8bae1dSRodney W. Grimes */ 143467388836SKonstantin Belousov result = vm_object_allocate_anon(atop(length), source, cred, length); 1435df8bae1dSRodney W. Grimes 1436df8bae1dSRodney W. Grimes /* 143751b867e5SJeff Roberson * Store the offset into the source object, and fix up the offset into 143851b867e5SJeff Roberson * the new object. 143951b867e5SJeff Roberson */ 144051b867e5SJeff Roberson result->backing_object_offset = *offset; 144151b867e5SJeff Roberson 144267388836SKonstantin Belousov if (shared || source != NULL) { 144367388836SKonstantin Belousov VM_OBJECT_WLOCK(result); 144467388836SKonstantin Belousov 144551b867e5SJeff Roberson /* 144667388836SKonstantin Belousov * The new object shadows the source object, adding a 144767388836SKonstantin Belousov * reference to it. Our caller changes his reference 144867388836SKonstantin Belousov * to point to the new object, removing a reference to 144967388836SKonstantin Belousov * the source object. Net result: no change of 145067388836SKonstantin Belousov * reference count, unless the caller needs to add one 145167388836SKonstantin Belousov * more reference due to forking a shared map entry. 145267388836SKonstantin Belousov */ 145367388836SKonstantin Belousov if (shared) { 145467388836SKonstantin Belousov vm_object_reference_locked(result); 145567388836SKonstantin Belousov vm_object_clear_flag(result, OBJ_ONEMAPPING); 145667388836SKonstantin Belousov } 145767388836SKonstantin Belousov 145867388836SKonstantin Belousov /* 145967388836SKonstantin Belousov * Try to optimize the result object's page color when 146067388836SKonstantin Belousov * shadowing in order to maintain page coloring 146167388836SKonstantin Belousov * consistency in the combined shadowed object. 1462df8bae1dSRodney W. Grimes */ 1463570a2f4aSAlan Cox if (source != NULL) { 146451b867e5SJeff Roberson vm_object_backing_insert(result, source); 14653f289c3fSJeff Roberson result->domain = source->domain; 1466f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 14677b54b1a9SAlan Cox result->flags |= source->flags & OBJ_COLORED; 146867388836SKonstantin Belousov result->pg_color = (source->pg_color + 146967388836SKonstantin Belousov OFF_TO_IDX(*offset)) & ((1 << (VM_NFREEORDER - 147067388836SKonstantin Belousov 1)) - 1); 1471f8a47341SAlan Cox #endif 147267388836SKonstantin Belousov } 147351b867e5SJeff Roberson VM_OBJECT_WUNLOCK(result); 1474de5f6a77SJohn Dyson } 1475df8bae1dSRodney W. Grimes 1476df8bae1dSRodney W. Grimes /* 1477df8bae1dSRodney W. Grimes * Return the new things 1478df8bae1dSRodney W. Grimes */ 1479df8bae1dSRodney W. Grimes *offset = 0; 1480df8bae1dSRodney W. Grimes *object = result; 1481df8bae1dSRodney W. Grimes } 1482df8bae1dSRodney W. Grimes 1483c5aaa06dSAlan Cox /* 1484c5aaa06dSAlan Cox * vm_object_split: 1485c5aaa06dSAlan Cox * 1486c5aaa06dSAlan Cox * Split the pages in a map entry into a new object. This affords 1487c5aaa06dSAlan Cox * easier removal of unused pages, and keeps object inheritance from 1488c5aaa06dSAlan Cox * being a negative impact on memory usage. 1489c5aaa06dSAlan Cox */ 1490c5aaa06dSAlan Cox void 1491c5aaa06dSAlan Cox vm_object_split(vm_map_entry_t entry) 1492c5aaa06dSAlan Cox { 1493aec9e7d8SMark Johnston vm_page_t m, m_busy, m_next; 149498087a06SJeff Roberson vm_object_t orig_object, new_object, backing_object; 149573000556SAlan Cox vm_pindex_t idx, offidxstart; 149673000556SAlan Cox vm_size_t size; 1497c5aaa06dSAlan Cox 1498c5aaa06dSAlan Cox orig_object = entry->object.vm_object; 149998087a06SJeff Roberson KASSERT((orig_object->flags & OBJ_ONEMAPPING) != 0, 150098087a06SJeff Roberson ("vm_object_split: Splitting object with multiple mappings.")); 150163967687SJeff Roberson if ((orig_object->flags & OBJ_ANON) == 0) 1502c5aaa06dSAlan Cox return; 1503c5aaa06dSAlan Cox if (orig_object->ref_count <= 1) 1504c5aaa06dSAlan Cox return; 150589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(orig_object); 1506c5aaa06dSAlan Cox 15074da9f125SAlan Cox offidxstart = OFF_TO_IDX(entry->offset); 150895442adfSAlan Cox size = atop(entry->end - entry->start); 1509c5aaa06dSAlan Cox 15104da9f125SAlan Cox /* 15114da9f125SAlan Cox * If swap_pager_copy() is later called, it will convert new_object 15124da9f125SAlan Cox * into a swap object. 15134da9f125SAlan Cox */ 151467388836SKonstantin Belousov new_object = vm_object_allocate_anon(size, orig_object, 151567388836SKonstantin Belousov orig_object->cred, ptoa(size)); 1516c5aaa06dSAlan Cox 1517c5474b8fSAlan Cox /* 151898087a06SJeff Roberson * We must wait for the orig_object to complete any in-progress 151998087a06SJeff Roberson * collapse so that the swap blocks are stable below. The 152098087a06SJeff Roberson * additional reference on backing_object by new object will 152198087a06SJeff Roberson * prevent further collapse operations until split completes. 152298087a06SJeff Roberson */ 152398087a06SJeff Roberson VM_OBJECT_WLOCK(orig_object); 152498087a06SJeff Roberson vm_object_collapse_wait(orig_object); 152598087a06SJeff Roberson 152698087a06SJeff Roberson /* 1527c5474b8fSAlan Cox * At this point, the new object is still private, so the order in 1528c5474b8fSAlan Cox * which the original and new objects are locked does not matter. 1529c5474b8fSAlan Cox */ 153089f6b863SAttilio Rao VM_OBJECT_WLOCK(new_object); 15313f289c3fSJeff Roberson new_object->domain = orig_object->domain; 153298087a06SJeff Roberson backing_object = orig_object->backing_object; 153398087a06SJeff Roberson if (backing_object != NULL) { 153498087a06SJeff Roberson vm_object_backing_insert_ref(new_object, backing_object); 1535c5aaa06dSAlan Cox new_object->backing_object_offset = 15364da9f125SAlan Cox orig_object->backing_object_offset + entry->offset; 1537c5aaa06dSAlan Cox } 1538ef694c1aSEdward Tomasz Napierala if (orig_object->cred != NULL) { 1539ef694c1aSEdward Tomasz Napierala crhold(orig_object->cred); 15403364c323SKonstantin Belousov KASSERT(orig_object->charge >= ptoa(size), 15413364c323SKonstantin Belousov ("orig_object->charge < 0")); 15423364c323SKonstantin Belousov orig_object->charge -= ptoa(size); 15433364c323SKonstantin Belousov } 154498087a06SJeff Roberson 154598087a06SJeff Roberson /* 154698087a06SJeff Roberson * Mark the split operation so that swap_pager_getpages() knows 154798087a06SJeff Roberson * that the object is in transition. 154898087a06SJeff Roberson */ 154998087a06SJeff Roberson vm_object_set_flag(orig_object, OBJ_SPLIT); 1550aec9e7d8SMark Johnston m_busy = NULL; 1551aec9e7d8SMark Johnston #ifdef INVARIANTS 1552aec9e7d8SMark Johnston idx = 0; 1553aec9e7d8SMark Johnston #endif 1554c5aaa06dSAlan Cox retry: 1555b382c10aSKonstantin Belousov m = vm_page_find_least(orig_object, offidxstart); 1556aec9e7d8SMark Johnston KASSERT(m == NULL || idx <= m->pindex - offidxstart, 1557aec9e7d8SMark Johnston ("%s: object %p was repopulated", __func__, orig_object)); 155873000556SAlan Cox for (; m != NULL && (idx = m->pindex - offidxstart) < size; 155973000556SAlan Cox m = m_next) { 156073000556SAlan Cox m_next = TAILQ_NEXT(m, listq); 1561c5aaa06dSAlan Cox 1562c5aaa06dSAlan Cox /* 1563c5aaa06dSAlan Cox * We must wait for pending I/O to complete before we can 1564c5aaa06dSAlan Cox * rename the page. 1565c5aaa06dSAlan Cox * 1566c5aaa06dSAlan Cox * We do not have to VM_PROT_NONE the page as mappings should 1567c5aaa06dSAlan Cox * not be changed by this operation. 1568c5aaa06dSAlan Cox */ 156963e97555SJeff Roberson if (vm_page_tryxbusy(m) == 0) { 157089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(new_object); 15714cdea4a8SJeff Roberson vm_page_sleep_if_busy(m, "spltwt"); 157289f6b863SAttilio Rao VM_OBJECT_WLOCK(new_object); 1573c5aaa06dSAlan Cox goto retry; 1574de33beddSAlan Cox } 1575e946b949SAttilio Rao 15764bf95d00SJeff Roberson /* 15774bf95d00SJeff Roberson * The page was left invalid. Likely placed there by 15784bf95d00SJeff Roberson * an incomplete fault. Just remove and ignore. 15794bf95d00SJeff Roberson */ 15804bf95d00SJeff Roberson if (vm_page_none_valid(m)) { 15814bf95d00SJeff Roberson if (vm_page_remove(m)) 15824bf95d00SJeff Roberson vm_page_free(m); 15834bf95d00SJeff Roberson continue; 15844bf95d00SJeff Roberson } 15854bf95d00SJeff Roberson 15863453bca8SAlan Cox /* vm_page_rename() will dirty the page. */ 1587e946b949SAttilio Rao if (vm_page_rename(m, new_object, idx)) { 158863e97555SJeff Roberson vm_page_xunbusy(m); 1589e946b949SAttilio Rao VM_OBJECT_WUNLOCK(new_object); 1590e946b949SAttilio Rao VM_OBJECT_WUNLOCK(orig_object); 15918d6fbbb8SJeff Roberson vm_radix_wait(); 1592e946b949SAttilio Rao VM_OBJECT_WLOCK(orig_object); 1593e946b949SAttilio Rao VM_OBJECT_WLOCK(new_object); 1594e946b949SAttilio Rao goto retry; 1595e946b949SAttilio Rao } 159663e97555SJeff Roberson 1597b5f359b7SAlan Cox #if VM_NRESERVLEVEL > 0 1598b5f359b7SAlan Cox /* 1599b5f359b7SAlan Cox * If some of the reservation's allocated pages remain with 1600b5f359b7SAlan Cox * the original object, then transferring the reservation to 1601b5f359b7SAlan Cox * the new object is neither particularly beneficial nor 1602b5f359b7SAlan Cox * particularly harmful as compared to leaving the reservation 1603b5f359b7SAlan Cox * with the original object. If, however, all of the 1604b5f359b7SAlan Cox * reservation's allocated pages are transferred to the new 1605b5f359b7SAlan Cox * object, then transferring the reservation is typically 1606b5f359b7SAlan Cox * beneficial. Determining which of these two cases applies 1607b5f359b7SAlan Cox * would be more costly than unconditionally renaming the 1608b5f359b7SAlan Cox * reservation. 1609b5f359b7SAlan Cox */ 1610b5f359b7SAlan Cox vm_reserv_rename(m, new_object, orig_object, offidxstart); 1611b5f359b7SAlan Cox #endif 1612aec9e7d8SMark Johnston 1613aec9e7d8SMark Johnston /* 1614aec9e7d8SMark Johnston * orig_object's type may change while sleeping, so keep track 1615aec9e7d8SMark Johnston * of the beginning of the busied range. 1616aec9e7d8SMark Johnston */ 16178da1c098SJeff Roberson if (orig_object->type != OBJT_SWAP) 16188da1c098SJeff Roberson vm_page_xunbusy(m); 1619aec9e7d8SMark Johnston else if (m_busy == NULL) 1620aec9e7d8SMark Johnston m_busy = m; 1621c5aaa06dSAlan Cox } 16224b8365d7SKonstantin Belousov if ((orig_object->flags & OBJ_SWAP) != 0) { 1623c5aaa06dSAlan Cox /* 1624c7c8dd7eSAlan Cox * swap_pager_copy() can sleep, in which case the orig_object's 1625c7c8dd7eSAlan Cox * and new_object's locks are released and reacquired. 1626c5aaa06dSAlan Cox */ 1627c5aaa06dSAlan Cox swap_pager_copy(orig_object, new_object, offidxstart, 0); 1628aec9e7d8SMark Johnston if (m_busy != NULL) 1629aec9e7d8SMark Johnston TAILQ_FOREACH_FROM(m_busy, &new_object->memq, listq) 1630aec9e7d8SMark Johnston vm_page_xunbusy(m_busy); 1631c5aaa06dSAlan Cox } 163298087a06SJeff Roberson vm_object_clear_flag(orig_object, OBJ_SPLIT); 163389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(orig_object); 163489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(new_object); 1635c5aaa06dSAlan Cox entry->object.vm_object = new_object; 1636c5aaa06dSAlan Cox entry->offset = 0LL; 1637c5aaa06dSAlan Cox vm_object_deallocate(orig_object); 163889f6b863SAttilio Rao VM_OBJECT_WLOCK(new_object); 1639c5aaa06dSAlan Cox } 1640c5aaa06dSAlan Cox 164199a1570aSKonstantin Belousov static vm_page_t 164298087a06SJeff Roberson vm_object_collapse_scan_wait(vm_object_t object, vm_page_t p) 164399a1570aSKonstantin Belousov { 164499a1570aSKonstantin Belousov vm_object_t backing_object; 164599a1570aSKonstantin Belousov 164699a1570aSKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(object); 164799a1570aSKonstantin Belousov backing_object = object->backing_object; 164899a1570aSKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(backing_object); 164999a1570aSKonstantin Belousov 165099a1570aSKonstantin Belousov KASSERT(p == NULL || p->object == object || p->object == backing_object, 165199a1570aSKonstantin Belousov ("invalid ownership %p %p %p", p, object, backing_object)); 16528d6fbbb8SJeff Roberson /* The page is only NULL when rename fails. */ 16534cdea4a8SJeff Roberson if (p == NULL) { 16546a14746cSRyan Libby VM_OBJECT_WUNLOCK(object); 16556a14746cSRyan Libby VM_OBJECT_WUNLOCK(backing_object); 16568d6fbbb8SJeff Roberson vm_radix_wait(); 16574cdea4a8SJeff Roberson } else { 16584cdea4a8SJeff Roberson if (p->object == object) 16594cdea4a8SJeff Roberson VM_OBJECT_WUNLOCK(backing_object); 166099a1570aSKonstantin Belousov else 16614cdea4a8SJeff Roberson VM_OBJECT_WUNLOCK(object); 16625975e53dSKonstantin Belousov vm_page_busy_sleep(p, "vmocol", false); 16634cdea4a8SJeff Roberson } 166499a1570aSKonstantin Belousov VM_OBJECT_WLOCK(object); 166599a1570aSKonstantin Belousov VM_OBJECT_WLOCK(backing_object); 166699a1570aSKonstantin Belousov return (TAILQ_FIRST(&backing_object->memq)); 166799a1570aSKonstantin Belousov } 166899a1570aSKonstantin Belousov 166999a1570aSKonstantin Belousov static bool 16704cc8daf7SConrad Meyer vm_object_scan_all_shadowed(vm_object_t object) 16714cc8daf7SConrad Meyer { 16724cc8daf7SConrad Meyer vm_object_t backing_object; 16734cc8daf7SConrad Meyer vm_page_t p, pp; 167477d6fd97SKonstantin Belousov vm_pindex_t backing_offset_index, new_pindex, pi, ps; 16754cc8daf7SConrad Meyer 16764cc8daf7SConrad Meyer VM_OBJECT_ASSERT_WLOCKED(object); 16774cc8daf7SConrad Meyer VM_OBJECT_ASSERT_WLOCKED(object->backing_object); 16784cc8daf7SConrad Meyer 16794cc8daf7SConrad Meyer backing_object = object->backing_object; 16804cc8daf7SConrad Meyer 168163967687SJeff Roberson if ((backing_object->flags & OBJ_ANON) == 0) 16824cc8daf7SConrad Meyer return (false); 16834cc8daf7SConrad Meyer 168477d6fd97SKonstantin Belousov pi = backing_offset_index = OFF_TO_IDX(object->backing_object_offset); 168577d6fd97SKonstantin Belousov p = vm_page_find_least(backing_object, pi); 168677d6fd97SKonstantin Belousov ps = swap_pager_find_least(backing_object, pi); 16874cc8daf7SConrad Meyer 16884cc8daf7SConrad Meyer /* 168977d6fd97SKonstantin Belousov * Only check pages inside the parent object's range and 169077d6fd97SKonstantin Belousov * inside the parent object's mapping of the backing object. 16914cc8daf7SConrad Meyer */ 169277d6fd97SKonstantin Belousov for (;; pi++) { 169377d6fd97SKonstantin Belousov if (p != NULL && p->pindex < pi) 169477d6fd97SKonstantin Belousov p = TAILQ_NEXT(p, listq); 169577d6fd97SKonstantin Belousov if (ps < pi) 169677d6fd97SKonstantin Belousov ps = swap_pager_find_least(backing_object, pi); 169777d6fd97SKonstantin Belousov if (p == NULL && ps >= backing_object->size) 169877d6fd97SKonstantin Belousov break; 169977d6fd97SKonstantin Belousov else if (p == NULL) 170077d6fd97SKonstantin Belousov pi = ps; 170177d6fd97SKonstantin Belousov else 170277d6fd97SKonstantin Belousov pi = MIN(p->pindex, ps); 170377d6fd97SKonstantin Belousov 170477d6fd97SKonstantin Belousov new_pindex = pi - backing_offset_index; 170577d6fd97SKonstantin Belousov if (new_pindex >= object->size) 170677d6fd97SKonstantin Belousov break; 17074cc8daf7SConrad Meyer 1708cd0047f3SKonstantin Belousov if (p != NULL) { 17094cc8daf7SConrad Meyer /* 1710cd0047f3SKonstantin Belousov * If the backing object page is busy a 1711cd0047f3SKonstantin Belousov * grandparent or older page may still be 1712cd0047f3SKonstantin Belousov * undergoing CoW. It is not safe to collapse 1713cd0047f3SKonstantin Belousov * the backing object until it is quiesced. 171458447749SJeff Roberson */ 1715cd0047f3SKonstantin Belousov if (vm_page_tryxbusy(p) == 0) 171658447749SJeff Roberson return (false); 171758447749SJeff Roberson 171858447749SJeff Roberson /* 1719cd0047f3SKonstantin Belousov * We raced with the fault handler that left 1720cd0047f3SKonstantin Belousov * newly allocated invalid page on the object 1721cd0047f3SKonstantin Belousov * queue and retried. 1722cd0047f3SKonstantin Belousov */ 1723cd0047f3SKonstantin Belousov if (!vm_page_all_valid(p)) 1724cd0047f3SKonstantin Belousov goto unbusy_ret; 1725cd0047f3SKonstantin Belousov } 1726cd0047f3SKonstantin Belousov 1727cd0047f3SKonstantin Belousov /* 17284cc8daf7SConrad Meyer * See if the parent has the page or if the parent's object 17294cc8daf7SConrad Meyer * pager has the page. If the parent has the page but the page 17304cc8daf7SConrad Meyer * is not valid, the parent's object pager must have the page. 17314cc8daf7SConrad Meyer * 17324cc8daf7SConrad Meyer * If this fails, the parent does not completely shadow the 17334cc8daf7SConrad Meyer * object and we might as well give up now. 17344cc8daf7SConrad Meyer */ 17354cc8daf7SConrad Meyer pp = vm_page_lookup(object, new_pindex); 1736cd0047f3SKonstantin Belousov 17370012f373SJeff Roberson /* 1738cd0047f3SKonstantin Belousov * The valid check here is stable due to object lock 1739cd0047f3SKonstantin Belousov * being required to clear valid and initiate paging. 1740cd0047f3SKonstantin Belousov * Busy of p disallows fault handler to validate pp. 17410012f373SJeff Roberson */ 17420012f373SJeff Roberson if ((pp == NULL || vm_page_none_valid(pp)) && 17434cc8daf7SConrad Meyer !vm_pager_has_page(object, new_pindex, NULL, NULL)) 1744cd0047f3SKonstantin Belousov goto unbusy_ret; 1745cd0047f3SKonstantin Belousov if (p != NULL) 1746cd0047f3SKonstantin Belousov vm_page_xunbusy(p); 17474cc8daf7SConrad Meyer } 17484cc8daf7SConrad Meyer return (true); 1749cd0047f3SKonstantin Belousov 1750cd0047f3SKonstantin Belousov unbusy_ret: 1751cd0047f3SKonstantin Belousov if (p != NULL) 1752cd0047f3SKonstantin Belousov vm_page_xunbusy(p); 1753cd0047f3SKonstantin Belousov return (false); 17544cc8daf7SConrad Meyer } 17554cc8daf7SConrad Meyer 175698087a06SJeff Roberson static void 175798087a06SJeff Roberson vm_object_collapse_scan(vm_object_t object) 17582ad1a3f7SMatthew Dillon { 17592ad1a3f7SMatthew Dillon vm_object_t backing_object; 176099a1570aSKonstantin Belousov vm_page_t next, p, pp; 176199a1570aSKonstantin Belousov vm_pindex_t backing_offset_index, new_pindex; 17622ad1a3f7SMatthew Dillon 176389f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 176489f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object->backing_object); 17652ad1a3f7SMatthew Dillon 17662ad1a3f7SMatthew Dillon backing_object = object->backing_object; 17672ad1a3f7SMatthew Dillon backing_offset_index = OFF_TO_IDX(object->backing_object_offset); 17682ad1a3f7SMatthew Dillon 17692ad1a3f7SMatthew Dillon /* 17702ad1a3f7SMatthew Dillon * Our scan 17712ad1a3f7SMatthew Dillon */ 17724cc8daf7SConrad Meyer for (p = TAILQ_FIRST(&backing_object->memq); p != NULL; p = next) { 177399a1570aSKonstantin Belousov next = TAILQ_NEXT(p, listq); 177499a1570aSKonstantin Belousov new_pindex = p->pindex - backing_offset_index; 17752ad1a3f7SMatthew Dillon 17762ad1a3f7SMatthew Dillon /* 17772ad1a3f7SMatthew Dillon * Check for busy page 17782ad1a3f7SMatthew Dillon */ 177963e97555SJeff Roberson if (vm_page_tryxbusy(p) == 0) { 178098087a06SJeff Roberson next = vm_object_collapse_scan_wait(object, p); 17812ad1a3f7SMatthew Dillon continue; 17822ad1a3f7SMatthew Dillon } 17832ad1a3f7SMatthew Dillon 178498087a06SJeff Roberson KASSERT(object->backing_object == backing_object, 178598087a06SJeff Roberson ("vm_object_collapse_scan: backing object mismatch %p != %p", 178698087a06SJeff Roberson object->backing_object, backing_object)); 178799a1570aSKonstantin Belousov KASSERT(p->object == backing_object, 178898087a06SJeff Roberson ("vm_object_collapse_scan: object mismatch %p != %p", 178998087a06SJeff Roberson p->object, backing_object)); 17902ad1a3f7SMatthew Dillon 179199a1570aSKonstantin Belousov if (p->pindex < backing_offset_index || 179299a1570aSKonstantin Belousov new_pindex >= object->size) { 17931390a5cbSKonstantin Belousov vm_pager_freespace(backing_object, p->pindex, 1); 1794e946b949SAttilio Rao 1795f6d89838SAlan Cox KASSERT(!pmap_page_is_mapped(p), 1796f6d89838SAlan Cox ("freeing mapped page %p", p)); 17970fd977b3SMark Johnston if (vm_page_remove(p)) 17982ad1a3f7SMatthew Dillon vm_page_free(p); 17992ad1a3f7SMatthew Dillon continue; 18002ad1a3f7SMatthew Dillon } 18012ad1a3f7SMatthew Dillon 1802cd0047f3SKonstantin Belousov if (!vm_page_all_valid(p)) { 1803cd0047f3SKonstantin Belousov KASSERT(!pmap_page_is_mapped(p), 1804cd0047f3SKonstantin Belousov ("freeing mapped page %p", p)); 1805cd0047f3SKonstantin Belousov if (vm_page_remove(p)) 1806cd0047f3SKonstantin Belousov vm_page_free(p); 1807cd0047f3SKonstantin Belousov continue; 1808cd0047f3SKonstantin Belousov } 1809cd0047f3SKonstantin Belousov 18102ad1a3f7SMatthew Dillon pp = vm_page_lookup(object, new_pindex); 181163e97555SJeff Roberson if (pp != NULL && vm_page_tryxbusy(pp) == 0) { 181263e97555SJeff Roberson vm_page_xunbusy(p); 1813e18cc7bfSMax Laier /* 18144cc8daf7SConrad Meyer * The page in the parent is busy and possibly not 18154cc8daf7SConrad Meyer * (yet) valid. Until its state is finalized by the 18164cc8daf7SConrad Meyer * busy bit owner, we can't tell whether it shadows the 181798087a06SJeff Roberson * original page. 1818e18cc7bfSMax Laier */ 181998087a06SJeff Roberson next = vm_object_collapse_scan_wait(object, pp); 1820e18cc7bfSMax Laier continue; 1821e18cc7bfSMax Laier } 182299a1570aSKonstantin Belousov 18234bf95d00SJeff Roberson if (pp != NULL && vm_page_none_valid(pp)) { 18244bf95d00SJeff Roberson /* 18254bf95d00SJeff Roberson * The page was invalid in the parent. Likely placed 18264bf95d00SJeff Roberson * there by an incomplete fault. Just remove and 18274bf95d00SJeff Roberson * ignore. p can replace it. 18284bf95d00SJeff Roberson */ 18294bf95d00SJeff Roberson if (vm_page_remove(pp)) 18304bf95d00SJeff Roberson vm_page_free(pp); 18314bf95d00SJeff Roberson pp = NULL; 18324bf95d00SJeff Roberson } 183399a1570aSKonstantin Belousov 18344cc8daf7SConrad Meyer if (pp != NULL || vm_pager_has_page(object, new_pindex, NULL, 18354cc8daf7SConrad Meyer NULL)) { 183699a1570aSKonstantin Belousov /* 18374cc8daf7SConrad Meyer * The page already exists in the parent OR swap exists 18384cc8daf7SConrad Meyer * for this location in the parent. Leave the parent's 18394cc8daf7SConrad Meyer * page alone. Destroy the original page from the 18404cc8daf7SConrad Meyer * backing object. 184199a1570aSKonstantin Belousov */ 18421390a5cbSKonstantin Belousov vm_pager_freespace(backing_object, p->pindex, 1); 1843f6d89838SAlan Cox KASSERT(!pmap_page_is_mapped(p), 1844f6d89838SAlan Cox ("freeing mapped page %p", p)); 18450fd977b3SMark Johnston if (vm_page_remove(p)) 18462ad1a3f7SMatthew Dillon vm_page_free(p); 184763e97555SJeff Roberson if (pp != NULL) 184863e97555SJeff Roberson vm_page_xunbusy(pp); 18492ad1a3f7SMatthew Dillon continue; 18502ad1a3f7SMatthew Dillon } 18512ad1a3f7SMatthew Dillon 1852e946b949SAttilio Rao /* 18534cc8daf7SConrad Meyer * Page does not exist in parent, rename the page from the 18544cc8daf7SConrad Meyer * backing object to the main object. 1855e946b949SAttilio Rao * 18564cc8daf7SConrad Meyer * If the page was mapped to a process, it can remain mapped 18573453bca8SAlan Cox * through the rename. vm_page_rename() will dirty the page. 1858e946b949SAttilio Rao */ 1859e946b949SAttilio Rao if (vm_page_rename(p, object, new_pindex)) { 186063e97555SJeff Roberson vm_page_xunbusy(p); 186198087a06SJeff Roberson next = vm_object_collapse_scan_wait(object, NULL); 1862e946b949SAttilio Rao continue; 1863e946b949SAttilio Rao } 186414a5dc17SAttilio Rao 186514a5dc17SAttilio Rao /* Use the old pindex to free the right page. */ 18661390a5cbSKonstantin Belousov vm_pager_freespace(backing_object, new_pindex + 18671390a5cbSKonstantin Belousov backing_offset_index, 1); 1868e946b949SAttilio Rao 1869f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 1870f8a47341SAlan Cox /* 1871f8a47341SAlan Cox * Rename the reservation. 1872f8a47341SAlan Cox */ 1873f8a47341SAlan Cox vm_reserv_rename(p, object, backing_object, 1874f8a47341SAlan Cox backing_offset_index); 1875f8a47341SAlan Cox #endif 18768da1c098SJeff Roberson vm_page_xunbusy(p); 18772ad1a3f7SMatthew Dillon } 18782fe6e4d7SDavid Greenman return; 18792fe6e4d7SDavid Greenman } 18802fe6e4d7SDavid Greenman 1881df8bae1dSRodney W. Grimes /* 1882df8bae1dSRodney W. Grimes * vm_object_collapse: 1883df8bae1dSRodney W. Grimes * 1884df8bae1dSRodney W. Grimes * Collapse an object with the object backing it. 1885df8bae1dSRodney W. Grimes * Pages in the backing object are moved into the 1886df8bae1dSRodney W. Grimes * parent, and the backing object is deallocated. 1887df8bae1dSRodney W. Grimes */ 188826f9a767SRodney W. Grimes void 18891b40f8c0SMatthew Dillon vm_object_collapse(vm_object_t object) 1890df8bae1dSRodney W. Grimes { 189198f139daSKonstantin Belousov vm_object_t backing_object, new_backing_object; 189298f139daSKonstantin Belousov 189389f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 189423955314SAlfred Perlstein 1895df8bae1dSRodney W. Grimes while (TRUE) { 189698087a06SJeff Roberson KASSERT((object->flags & (OBJ_DEAD | OBJ_ANON)) == OBJ_ANON, 189798087a06SJeff Roberson ("collapsing invalid object")); 189898087a06SJeff Roberson 1899df8bae1dSRodney W. Grimes /* 190098087a06SJeff Roberson * Wait for the backing_object to finish any pending 190198087a06SJeff Roberson * collapse so that the caller sees the shortest possible 190298087a06SJeff Roberson * shadow chain. 1903df8bae1dSRodney W. Grimes */ 190498087a06SJeff Roberson backing_object = vm_object_backing_collapse_wait(object); 190598087a06SJeff Roberson if (backing_object == NULL) 190698087a06SJeff Roberson return; 190798087a06SJeff Roberson 190898087a06SJeff Roberson KASSERT(object->ref_count > 0 && 190998087a06SJeff Roberson object->ref_count > object->shadow_count, 191098087a06SJeff Roberson ("collapse with invalid ref %d or shadow %d count.", 191198087a06SJeff Roberson object->ref_count, object->shadow_count)); 191298087a06SJeff Roberson KASSERT((backing_object->flags & 191398087a06SJeff Roberson (OBJ_COLLAPSING | OBJ_DEAD)) == 0, 191498087a06SJeff Roberson ("vm_object_collapse: Backing object already collapsing.")); 191598087a06SJeff Roberson KASSERT((object->flags & (OBJ_COLLAPSING | OBJ_DEAD)) == 0, 191698087a06SJeff Roberson ("vm_object_collapse: object is already collapsing.")); 1917df8bae1dSRodney W. Grimes 1918f919ebdeSDavid Greenman /* 191998087a06SJeff Roberson * We know that we can either collapse the backing object if 192098087a06SJeff Roberson * the parent is the only reference to it, or (perhaps) have 19212ad1a3f7SMatthew Dillon * the parent bypass the object if the parent happens to shadow 19222ad1a3f7SMatthew Dillon * all the resident pages in the entire backing object. 1923df8bae1dSRodney W. Grimes */ 1924df8bae1dSRodney W. Grimes if (backing_object->ref_count == 1) { 192598087a06SJeff Roberson KASSERT(backing_object->shadow_count == 1, 192698087a06SJeff Roberson ("vm_object_collapse: shadow_count: %d", 192798087a06SJeff Roberson backing_object->shadow_count)); 1928aa9bc3b1SKonstantin Belousov vm_object_pip_add(object, 1); 192998087a06SJeff Roberson vm_object_set_flag(object, OBJ_COLLAPSING); 1930aa9bc3b1SKonstantin Belousov vm_object_pip_add(backing_object, 1); 193198087a06SJeff Roberson vm_object_set_flag(backing_object, OBJ_DEAD); 1932aa9bc3b1SKonstantin Belousov 1933df8bae1dSRodney W. Grimes /* 19342ad1a3f7SMatthew Dillon * If there is exactly one reference to the backing 19352ad1a3f7SMatthew Dillon * object, we can collapse it into the parent. 1936df8bae1dSRodney W. Grimes */ 193798087a06SJeff Roberson vm_object_collapse_scan(object); 1938df8bae1dSRodney W. Grimes 1939f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 1940f8a47341SAlan Cox /* 1941f8a47341SAlan Cox * Break any reservations from backing_object. 1942f8a47341SAlan Cox */ 1943f8a47341SAlan Cox if (__predict_false(!LIST_EMPTY(&backing_object->rvq))) 1944f8a47341SAlan Cox vm_reserv_break_all(backing_object); 1945f8a47341SAlan Cox #endif 1946f8a47341SAlan Cox 1947df8bae1dSRodney W. Grimes /* 1948df8bae1dSRodney W. Grimes * Move the pager from backing_object to object. 1949df8bae1dSRodney W. Grimes */ 19504b8365d7SKonstantin Belousov if ((backing_object->flags & OBJ_SWAP) != 0) { 195124a1cce3SDavid Greenman /* 1952c7c8dd7eSAlan Cox * swap_pager_copy() can sleep, in which case 1953c7c8dd7eSAlan Cox * the backing_object's and object's locks are 1954c7c8dd7eSAlan Cox * released and reacquired. 1955571a1e92SAttilio Rao * Since swap_pager_copy() is being asked to 195698087a06SJeff Roberson * destroy backing_object, it will change the 195798087a06SJeff Roberson * type to OBJT_DEFAULT. 195824a1cce3SDavid Greenman */ 19591c7c3c6aSMatthew Dillon swap_pager_copy( 19601c7c3c6aSMatthew Dillon backing_object, 19611c7c3c6aSMatthew Dillon object, 19621c7c3c6aSMatthew Dillon OFF_TO_IDX(object->backing_object_offset), TRUE); 1963c0503609SDavid Greenman } 196498087a06SJeff Roberson 1965df8bae1dSRodney W. Grimes /* 1966df8bae1dSRodney W. Grimes * Object now shadows whatever backing_object did. 1967df8bae1dSRodney W. Grimes */ 196898087a06SJeff Roberson vm_object_clear_flag(object, OBJ_COLLAPSING); 196998087a06SJeff Roberson vm_object_backing_transfer(object, backing_object); 19702ad1a3f7SMatthew Dillon object->backing_object_offset += 19712ad1a3f7SMatthew Dillon backing_object->backing_object_offset; 197298087a06SJeff Roberson VM_OBJECT_WUNLOCK(object); 197398087a06SJeff Roberson vm_object_pip_wakeup(object); 19742ad1a3f7SMatthew Dillon 1975df8bae1dSRodney W. Grimes /* 1976df8bae1dSRodney W. Grimes * Discard backing_object. 1977df8bae1dSRodney W. Grimes * 19780d94caffSDavid Greenman * Since the backing object has no pages, no pager left, 19790d94caffSDavid Greenman * and no object references within it, all that is 19800d94caffSDavid Greenman * necessary is to dispose of it. 1981df8bae1dSRodney W. Grimes */ 19829b4d473aSKonstantin Belousov KASSERT(backing_object->ref_count == 1, ( 19839b4d473aSKonstantin Belousov "backing_object %p was somehow re-referenced during collapse!", 19849b4d473aSKonstantin Belousov backing_object)); 1985aa9bc3b1SKonstantin Belousov vm_object_pip_wakeup(backing_object); 198698087a06SJeff Roberson (void)refcount_release(&backing_object->ref_count); 198798087a06SJeff Roberson vm_object_terminate(backing_object); 198811542376SAlan Cox counter_u64_add(object_collapses, 1); 198998087a06SJeff Roberson VM_OBJECT_WLOCK(object); 19900d94caffSDavid Greenman } else { 1991df8bae1dSRodney W. Grimes /* 19922ad1a3f7SMatthew Dillon * If we do not entirely shadow the backing object, 19932ad1a3f7SMatthew Dillon * there is nothing we can do so we give up. 199498087a06SJeff Roberson * 199598087a06SJeff Roberson * The object lock and backing_object lock must not 199698087a06SJeff Roberson * be dropped during this sequence. 1997df8bae1dSRodney W. Grimes */ 199858447749SJeff Roberson if (!vm_object_scan_all_shadowed(object)) { 199989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 20002ad1a3f7SMatthew Dillon break; 200124a1cce3SDavid Greenman } 2002df8bae1dSRodney W. Grimes 2003df8bae1dSRodney W. Grimes /* 20040d94caffSDavid Greenman * Make the parent shadow the next object in the 20050d94caffSDavid Greenman * chain. Deallocating backing_object will not remove 20060d94caffSDavid Greenman * it, since its reference count is at least 2. 2007df8bae1dSRodney W. Grimes */ 200851b867e5SJeff Roberson vm_object_backing_remove_locked(object); 200995e5e988SJohn Dyson new_backing_object = backing_object->backing_object; 201051b867e5SJeff Roberson if (new_backing_object != NULL) { 201198087a06SJeff Roberson vm_object_backing_insert_ref(object, 201251b867e5SJeff Roberson new_backing_object); 201395e5e988SJohn Dyson object->backing_object_offset += 201495e5e988SJohn Dyson backing_object->backing_object_offset; 2015de5f6a77SJohn Dyson } 2016df8bae1dSRodney W. Grimes 2017df8bae1dSRodney W. Grimes /* 20180d94caffSDavid Greenman * Drop the reference count on backing_object. Since 201922ec553fSAlan Cox * its ref_count was at least 2, it will not vanish. 2020df8bae1dSRodney W. Grimes */ 202198087a06SJeff Roberson (void)refcount_release(&backing_object->ref_count); 202298087a06SJeff Roberson KASSERT(backing_object->ref_count >= 1, ( 202398087a06SJeff Roberson "backing_object %p was somehow dereferenced during collapse!", 202498087a06SJeff Roberson backing_object)); 202589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 202611542376SAlan Cox counter_u64_add(object_bypasses, 1); 2027df8bae1dSRodney W. Grimes } 2028df8bae1dSRodney W. Grimes 2029df8bae1dSRodney W. Grimes /* 2030df8bae1dSRodney W. Grimes * Try again with this object's new backing object. 2031df8bae1dSRodney W. Grimes */ 2032df8bae1dSRodney W. Grimes } 2033df8bae1dSRodney W. Grimes } 2034df8bae1dSRodney W. Grimes 2035df8bae1dSRodney W. Grimes /* 2036bff99f0dSAlan Cox * vm_object_page_remove: 2037df8bae1dSRodney W. Grimes * 203868855966SAlan Cox * For the given object, either frees or invalidates each of the 20396bbee8e2SAlan Cox * specified pages. In general, a page is freed. However, if a page is 20406bbee8e2SAlan Cox * wired for any reason other than the existence of a managed, wired 20416bbee8e2SAlan Cox * mapping, then it may be invalidated but not removed from the object. 20426bbee8e2SAlan Cox * Pages are specified by the given range ["start", "end") and the option 20436bbee8e2SAlan Cox * OBJPR_CLEANONLY. As a special case, if "end" is zero, then the range 20446bbee8e2SAlan Cox * extends from "start" to the end of the object. If the option 20456bbee8e2SAlan Cox * OBJPR_CLEANONLY is specified, then only the non-dirty pages within the 20466bbee8e2SAlan Cox * specified range are affected. If the option OBJPR_NOTMAPPED is 20476bbee8e2SAlan Cox * specified, then the pages within the specified range must have no 20486bbee8e2SAlan Cox * mappings. Otherwise, if this option is not specified, any mappings to 20496bbee8e2SAlan Cox * the specified pages are removed before the pages are freed or 20506bbee8e2SAlan Cox * invalidated. 205168855966SAlan Cox * 20526bbee8e2SAlan Cox * In general, this operation should only be performed on objects that 20536bbee8e2SAlan Cox * contain managed pages. There are, however, two exceptions. First, it 20546bbee8e2SAlan Cox * is performed on the kernel and kmem objects by vm_map_entry_delete(). 20556bbee8e2SAlan Cox * Second, it is used by msync(..., MS_INVALIDATE) to invalidate device- 20566bbee8e2SAlan Cox * backed pages. In both of these cases, the option OBJPR_CLEANONLY must 20576bbee8e2SAlan Cox * not be specified and the option OBJPR_NOTMAPPED must be specified. 2058df8bae1dSRodney W. Grimes * 2059df8bae1dSRodney W. Grimes * The object must be locked. 2060df8bae1dSRodney W. Grimes */ 206126f9a767SRodney W. Grimes void 2062ecde4b32SAlan Cox vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end, 20636bbee8e2SAlan Cox int options) 2064df8bae1dSRodney W. Grimes { 2065d031cff1SMatthew Dillon vm_page_t p, next; 2066df8bae1dSRodney W. Grimes 206789f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 206828634820SAlan Cox KASSERT((object->flags & OBJ_UNMANAGED) == 0 || 20696bbee8e2SAlan Cox (options & (OBJPR_CLEANONLY | OBJPR_NOTMAPPED)) == OBJPR_NOTMAPPED, 20706bbee8e2SAlan Cox ("vm_object_page_remove: illegal options for object %p", object)); 2071ecde4b32SAlan Cox if (object->resident_page_count == 0) 20727667839aSAlan Cox return; 2073d474eaaaSDoug Rabson vm_object_pip_add(object, 1); 207426f9a767SRodney W. Grimes again: 2075b382c10aSKonstantin Belousov p = vm_page_find_least(object, start); 20762965a453SKip Macy 207775741c04SAlan Cox /* 20786bbee8e2SAlan Cox * Here, the variable "p" is either (1) the page with the least pindex 20796bbee8e2SAlan Cox * greater than or equal to the parameter "start" or (2) NULL. 208075741c04SAlan Cox */ 20816bbee8e2SAlan Cox for (; p != NULL && (p->pindex < end || end == 0); p = next) { 2082b18bfc3dSJohn Dyson next = TAILQ_NEXT(p, listq); 208375741c04SAlan Cox 208459677d3cSAlan Cox /* 20856bbee8e2SAlan Cox * If the page is wired for any reason besides the existence 20866bbee8e2SAlan Cox * of managed, wired mappings, then it cannot be freed. For 20876bbee8e2SAlan Cox * example, fictitious pages, which represent device memory, 20886bbee8e2SAlan Cox * are inherently wired and cannot be freed. They can, 20896bbee8e2SAlan Cox * however, be invalidated if the option OBJPR_CLEANONLY is 20906bbee8e2SAlan Cox * not specified. 209159677d3cSAlan Cox */ 209263e97555SJeff Roberson if (vm_page_tryxbusy(p) == 0) { 20934cdea4a8SJeff Roberson vm_page_sleep_if_busy(p, "vmopar"); 2094fee2a2faSMark Johnston goto again; 2095fee2a2faSMark Johnston } 2096d842aa51SMark Johnston if (vm_page_wired(p)) { 2097fee2a2faSMark Johnston wired: 2098cf060942SAlan Cox if ((options & OBJPR_NOTMAPPED) == 0 && 2099cf060942SAlan Cox object->ref_count != 0) 21004fec79beSAlan Cox pmap_remove_all(p); 21016bbee8e2SAlan Cox if ((options & OBJPR_CLEANONLY) == 0) { 21020012f373SJeff Roberson vm_page_invalid(p); 2103a28042d1SAlan Cox vm_page_undirty(p); 2104a28042d1SAlan Cox } 210563e97555SJeff Roberson vm_page_xunbusy(p); 210693c5d3a4SKonstantin Belousov continue; 21070d94caffSDavid Greenman } 210868855966SAlan Cox KASSERT((p->flags & PG_FICTITIOUS) == 0, 210968855966SAlan Cox ("vm_object_page_remove: page %p is fictitious", p)); 21100012f373SJeff Roberson if ((options & OBJPR_CLEANONLY) != 0 && 21110012f373SJeff Roberson !vm_page_none_valid(p)) { 2112cf060942SAlan Cox if ((options & OBJPR_NOTMAPPED) == 0 && 2113fee2a2faSMark Johnston object->ref_count != 0 && 2114fee2a2faSMark Johnston !vm_page_try_remove_write(p)) 2115fee2a2faSMark Johnston goto wired; 211663e97555SJeff Roberson if (p->dirty != 0) { 211763e97555SJeff Roberson vm_page_xunbusy(p); 211893c5d3a4SKonstantin Belousov continue; 21192965a453SKip Macy } 212063e97555SJeff Roberson } 2121fee2a2faSMark Johnston if ((options & OBJPR_NOTMAPPED) == 0 && 2122fee2a2faSMark Johnston object->ref_count != 0 && !vm_page_try_remove_all(p)) 2123fee2a2faSMark Johnston goto wired; 21245cd29d0fSMark Johnston vm_page_free(p); 21252965a453SKip Macy } 2126f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 212784242cf6SMark Johnston 21281390a5cbSKonstantin Belousov vm_pager_freespace(object, start, (end == 0 ? object->size : end) - 21291390a5cbSKonstantin Belousov start); 2130c0503609SDavid Greenman } 2131df8bae1dSRodney W. Grimes 2132df8bae1dSRodney W. Grimes /* 21333138cd36SMark Johnston * vm_object_page_noreuse: 2134936c09acSJohn Baldwin * 21353138cd36SMark Johnston * For the given object, attempt to move the specified pages to 21363138cd36SMark Johnston * the head of the inactive queue. This bypasses regular LRU 21373138cd36SMark Johnston * operation and allows the pages to be reused quickly under memory 21383138cd36SMark Johnston * pressure. If a page is wired for any reason, then it will not 21393138cd36SMark Johnston * be queued. Pages are specified by the range ["start", "end"). 21403138cd36SMark Johnston * As a special case, if "end" is zero, then the range extends from 21413138cd36SMark Johnston * "start" to the end of the object. 2142936c09acSJohn Baldwin * 2143936c09acSJohn Baldwin * This operation should only be performed on objects that 214428634820SAlan Cox * contain non-fictitious, managed pages. 2145936c09acSJohn Baldwin * 2146936c09acSJohn Baldwin * The object must be locked. 2147936c09acSJohn Baldwin */ 2148936c09acSJohn Baldwin void 21493138cd36SMark Johnston vm_object_page_noreuse(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 2150936c09acSJohn Baldwin { 2151936c09acSJohn Baldwin vm_page_t p, next; 2152936c09acSJohn Baldwin 215352d1addaSAlan Cox VM_OBJECT_ASSERT_LOCKED(object); 215428634820SAlan Cox KASSERT((object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0, 21553138cd36SMark Johnston ("vm_object_page_noreuse: illegal object %p", object)); 2156936c09acSJohn Baldwin if (object->resident_page_count == 0) 2157936c09acSJohn Baldwin return; 2158936c09acSJohn Baldwin p = vm_page_find_least(object, start); 2159936c09acSJohn Baldwin 2160936c09acSJohn Baldwin /* 2161936c09acSJohn Baldwin * Here, the variable "p" is either (1) the page with the least pindex 2162936c09acSJohn Baldwin * greater than or equal to the parameter "start" or (2) NULL. 2163936c09acSJohn Baldwin */ 2164936c09acSJohn Baldwin for (; p != NULL && (p->pindex < end || end == 0); p = next) { 2165936c09acSJohn Baldwin next = TAILQ_NEXT(p, listq); 21663138cd36SMark Johnston vm_page_deactivate_noreuse(p); 2167936c09acSJohn Baldwin } 2168936c09acSJohn Baldwin } 2169936c09acSJohn Baldwin 2170936c09acSJohn Baldwin /* 2171387aabc5SAlan Cox * Populate the specified range of the object with valid pages. Returns 2172387aabc5SAlan Cox * TRUE if the range is successfully populated and FALSE otherwise. 2173387aabc5SAlan Cox * 2174387aabc5SAlan Cox * Note: This function should be optimized to pass a larger array of 2175387aabc5SAlan Cox * pages to vm_pager_get_pages() before it is applied to a non- 2176387aabc5SAlan Cox * OBJT_DEVICE object. 2177387aabc5SAlan Cox * 2178387aabc5SAlan Cox * The object must be locked. 2179387aabc5SAlan Cox */ 2180387aabc5SAlan Cox boolean_t 2181387aabc5SAlan Cox vm_object_populate(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 2182387aabc5SAlan Cox { 2183093c7f39SGleb Smirnoff vm_page_t m; 2184387aabc5SAlan Cox vm_pindex_t pindex; 2185387aabc5SAlan Cox int rv; 2186387aabc5SAlan Cox 218789f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 2188387aabc5SAlan Cox for (pindex = start; pindex < end; pindex++) { 2189c7575748SJeff Roberson rv = vm_page_grab_valid(&m, object, pindex, VM_ALLOC_NORMAL); 2190c7575748SJeff Roberson if (rv != VM_PAGER_OK) 2191387aabc5SAlan Cox break; 2192c7575748SJeff Roberson 2193387aabc5SAlan Cox /* 2194387aabc5SAlan Cox * Keep "m" busy because a subsequent iteration may unlock 2195387aabc5SAlan Cox * the object. 2196387aabc5SAlan Cox */ 2197387aabc5SAlan Cox } 2198387aabc5SAlan Cox if (pindex > start) { 2199387aabc5SAlan Cox m = vm_page_lookup(object, start); 2200387aabc5SAlan Cox while (m != NULL && m->pindex < pindex) { 2201c7aebda8SAttilio Rao vm_page_xunbusy(m); 2202387aabc5SAlan Cox m = TAILQ_NEXT(m, listq); 2203387aabc5SAlan Cox } 2204387aabc5SAlan Cox } 2205387aabc5SAlan Cox return (pindex == end); 2206387aabc5SAlan Cox } 2207387aabc5SAlan Cox 2208387aabc5SAlan Cox /* 2209df8bae1dSRodney W. Grimes * Routine: vm_object_coalesce 2210df8bae1dSRodney W. Grimes * Function: Coalesces two objects backing up adjoining 2211df8bae1dSRodney W. Grimes * regions of memory into a single object. 2212df8bae1dSRodney W. Grimes * 2213df8bae1dSRodney W. Grimes * returns TRUE if objects were combined. 2214df8bae1dSRodney W. Grimes * 2215df8bae1dSRodney W. Grimes * NOTE: Only works at the moment if the second object is NULL - 2216df8bae1dSRodney W. Grimes * if it's not, which object do we lock first? 2217df8bae1dSRodney W. Grimes * 2218df8bae1dSRodney W. Grimes * Parameters: 2219df8bae1dSRodney W. Grimes * prev_object First object to coalesce 2220df8bae1dSRodney W. Grimes * prev_offset Offset into prev_object 2221df8bae1dSRodney W. Grimes * prev_size Size of reference to prev_object 222257a21abaSAlan Cox * next_size Size of reference to the second object 22233364c323SKonstantin Belousov * reserved Indicator that extension region has 22243364c323SKonstantin Belousov * swap accounted for 2225df8bae1dSRodney W. Grimes * 2226df8bae1dSRodney W. Grimes * Conditions: 2227df8bae1dSRodney W. Grimes * The object must *not* be locked. 2228df8bae1dSRodney W. Grimes */ 22290d94caffSDavid Greenman boolean_t 223057a21abaSAlan Cox vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset, 22313364c323SKonstantin Belousov vm_size_t prev_size, vm_size_t next_size, boolean_t reserved) 2232df8bae1dSRodney W. Grimes { 2233ea41812fSAlan Cox vm_pindex_t next_pindex; 2234df8bae1dSRodney W. Grimes 223500e1854aSAlan Cox if (prev_object == NULL) 2236df8bae1dSRodney W. Grimes return (TRUE); 223763967687SJeff Roberson if ((prev_object->flags & OBJ_ANON) == 0) 223830dcfc09SJohn Dyson return (FALSE); 223930dcfc09SJohn Dyson 224063967687SJeff Roberson VM_OBJECT_WLOCK(prev_object); 2241df8bae1dSRodney W. Grimes /* 224298087a06SJeff Roberson * Try to collapse the object first. 2243df8bae1dSRodney W. Grimes */ 2244df8bae1dSRodney W. Grimes vm_object_collapse(prev_object); 2245df8bae1dSRodney W. Grimes 2246df8bae1dSRodney W. Grimes /* 22470d94caffSDavid Greenman * Can't coalesce if: . more than one reference . paged out . shadows 22480d94caffSDavid Greenman * another object . has a copy elsewhere (any of which mean that the 22490d94caffSDavid Greenman * pages not mapped to prev_entry may be in use anyway) 2250df8bae1dSRodney W. Grimes */ 22518cc7e047SJohn Dyson if (prev_object->backing_object != NULL) { 225289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(prev_object); 2253df8bae1dSRodney W. Grimes return (FALSE); 2254df8bae1dSRodney W. Grimes } 2255a316d390SJohn Dyson 2256a316d390SJohn Dyson prev_size >>= PAGE_SHIFT; 2257a316d390SJohn Dyson next_size >>= PAGE_SHIFT; 225857a21abaSAlan Cox next_pindex = OFF_TO_IDX(prev_offset) + prev_size; 22598cc7e047SJohn Dyson 22600e48e068SMark Johnston if (prev_object->ref_count > 1 && 22610e48e068SMark Johnston prev_object->size != next_pindex && 22620e48e068SMark Johnston (prev_object->flags & OBJ_ONEMAPPING) == 0) { 226389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(prev_object); 22648cc7e047SJohn Dyson return (FALSE); 22658cc7e047SJohn Dyson } 22668cc7e047SJohn Dyson 2267df8bae1dSRodney W. Grimes /* 22683364c323SKonstantin Belousov * Account for the charge. 22693364c323SKonstantin Belousov */ 2270ef694c1aSEdward Tomasz Napierala if (prev_object->cred != NULL) { 22713364c323SKonstantin Belousov /* 22723364c323SKonstantin Belousov * If prev_object was charged, then this mapping, 2273763df3ecSPedro F. Giffuni * although not charged now, may become writable 2274ef694c1aSEdward Tomasz Napierala * later. Non-NULL cred in the object would prevent 22753364c323SKonstantin Belousov * swap reservation during enabling of the write 22763364c323SKonstantin Belousov * access, so reserve swap now. Failed reservation 22773364c323SKonstantin Belousov * cause allocation of the separate object for the map 22783364c323SKonstantin Belousov * entry, and swap reservation for this entry is 22793364c323SKonstantin Belousov * managed in appropriate time. 22803364c323SKonstantin Belousov */ 2281ef694c1aSEdward Tomasz Napierala if (!reserved && !swap_reserve_by_cred(ptoa(next_size), 2282ef694c1aSEdward Tomasz Napierala prev_object->cred)) { 22839f790a17SKonstantin Belousov VM_OBJECT_WUNLOCK(prev_object); 22843364c323SKonstantin Belousov return (FALSE); 22853364c323SKonstantin Belousov } 22863364c323SKonstantin Belousov prev_object->charge += ptoa(next_size); 22873364c323SKonstantin Belousov } 22883364c323SKonstantin Belousov 22893364c323SKonstantin Belousov /* 22900d94caffSDavid Greenman * Remove any pages that may still be in the object from a previous 22910d94caffSDavid Greenman * deallocation. 2292df8bae1dSRodney W. Grimes */ 2293ea41812fSAlan Cox if (next_pindex < prev_object->size) { 22946bbee8e2SAlan Cox vm_object_page_remove(prev_object, next_pindex, next_pindex + 22956bbee8e2SAlan Cox next_size, 0); 22963364c323SKonstantin Belousov #if 0 2297ef694c1aSEdward Tomasz Napierala if (prev_object->cred != NULL) { 22983364c323SKonstantin Belousov KASSERT(prev_object->charge >= 22993364c323SKonstantin Belousov ptoa(prev_object->size - next_pindex), 23003364c323SKonstantin Belousov ("object %p overcharged 1 %jx %jx", prev_object, 23013364c323SKonstantin Belousov (uintmax_t)next_pindex, (uintmax_t)next_size)); 23023364c323SKonstantin Belousov prev_object->charge -= ptoa(prev_object->size - 23033364c323SKonstantin Belousov next_pindex); 23043364c323SKonstantin Belousov } 23053364c323SKonstantin Belousov #endif 2306ea41812fSAlan Cox } 2307df8bae1dSRodney W. Grimes 2308df8bae1dSRodney W. Grimes /* 2309df8bae1dSRodney W. Grimes * Extend the object if necessary. 2310df8bae1dSRodney W. Grimes */ 2311ea41812fSAlan Cox if (next_pindex + next_size > prev_object->size) 2312ea41812fSAlan Cox prev_object->size = next_pindex + next_size; 2313df8bae1dSRodney W. Grimes 231489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(prev_object); 2315df8bae1dSRodney W. Grimes return (TRUE); 2316df8bae1dSRodney W. Grimes } 2317df8bae1dSRodney W. Grimes 23187a5a6352SMatthew Dillon void 2319180bcaa4SKonstantin Belousov vm_object_set_writeable_dirty_(vm_object_t object) 23207a5a6352SMatthew Dillon { 232167d0e293SJeff Roberson atomic_add_int(&object->generation, 1); 23227a5a6352SMatthew Dillon } 23237a5a6352SMatthew Dillon 2324c23c555bSKonstantin Belousov bool 2325c23c555bSKonstantin Belousov vm_object_mightbedirty_(vm_object_t object) 2326c23c555bSKonstantin Belousov { 2327c23c555bSKonstantin Belousov return (object->generation != object->cleangeneration); 2328c23c555bSKonstantin Belousov } 2329c23c555bSKonstantin Belousov 233003462509SAlan Cox /* 233103462509SAlan Cox * vm_object_unwire: 233203462509SAlan Cox * 233303462509SAlan Cox * For each page offset within the specified range of the given object, 233403462509SAlan Cox * find the highest-level page in the shadow chain and unwire it. A page 233503462509SAlan Cox * must exist at every page offset, and the highest-level page must be 233603462509SAlan Cox * wired. 233703462509SAlan Cox */ 233803462509SAlan Cox void 233903462509SAlan Cox vm_object_unwire(vm_object_t object, vm_ooffset_t offset, vm_size_t length, 234003462509SAlan Cox uint8_t queue) 234103462509SAlan Cox { 234220e4afbfSKonstantin Belousov vm_object_t tobject, t1object; 234303462509SAlan Cox vm_page_t m, tm; 234403462509SAlan Cox vm_pindex_t end_pindex, pindex, tpindex; 234503462509SAlan Cox int depth, locked_depth; 234603462509SAlan Cox 234703462509SAlan Cox KASSERT((offset & PAGE_MASK) == 0, 234803462509SAlan Cox ("vm_object_unwire: offset is not page aligned")); 234903462509SAlan Cox KASSERT((length & PAGE_MASK) == 0, 235003462509SAlan Cox ("vm_object_unwire: length is not a multiple of PAGE_SIZE")); 235103462509SAlan Cox /* The wired count of a fictitious page never changes. */ 235203462509SAlan Cox if ((object->flags & OBJ_FICTITIOUS) != 0) 235303462509SAlan Cox return; 235403462509SAlan Cox pindex = OFF_TO_IDX(offset); 235503462509SAlan Cox end_pindex = pindex + atop(length); 235620e4afbfSKonstantin Belousov again: 235703462509SAlan Cox locked_depth = 1; 235803462509SAlan Cox VM_OBJECT_RLOCK(object); 235903462509SAlan Cox m = vm_page_find_least(object, pindex); 236003462509SAlan Cox while (pindex < end_pindex) { 236103462509SAlan Cox if (m == NULL || pindex < m->pindex) { 236203462509SAlan Cox /* 236303462509SAlan Cox * The first object in the shadow chain doesn't 236403462509SAlan Cox * contain a page at the current index. Therefore, 236503462509SAlan Cox * the page must exist in a backing object. 236603462509SAlan Cox */ 236703462509SAlan Cox tobject = object; 236803462509SAlan Cox tpindex = pindex; 236903462509SAlan Cox depth = 0; 237003462509SAlan Cox do { 237103462509SAlan Cox tpindex += 237203462509SAlan Cox OFF_TO_IDX(tobject->backing_object_offset); 237303462509SAlan Cox tobject = tobject->backing_object; 237403462509SAlan Cox KASSERT(tobject != NULL, 237503462509SAlan Cox ("vm_object_unwire: missing page")); 237603462509SAlan Cox if ((tobject->flags & OBJ_FICTITIOUS) != 0) 237703462509SAlan Cox goto next_page; 237803462509SAlan Cox depth++; 237903462509SAlan Cox if (depth == locked_depth) { 238003462509SAlan Cox locked_depth++; 238103462509SAlan Cox VM_OBJECT_RLOCK(tobject); 238203462509SAlan Cox } 238303462509SAlan Cox } while ((tm = vm_page_lookup(tobject, tpindex)) == 238403462509SAlan Cox NULL); 238503462509SAlan Cox } else { 238603462509SAlan Cox tm = m; 238703462509SAlan Cox m = TAILQ_NEXT(m, listq); 238803462509SAlan Cox } 238963e97555SJeff Roberson if (vm_page_trysbusy(tm) == 0) { 239087e93ea6SMark Johnston for (tobject = object; locked_depth >= 1; 239120e4afbfSKonstantin Belousov locked_depth--) { 239220e4afbfSKonstantin Belousov t1object = tobject->backing_object; 239387e93ea6SMark Johnston if (tm->object != tobject) 239420e4afbfSKonstantin Belousov VM_OBJECT_RUNLOCK(tobject); 239520e4afbfSKonstantin Belousov tobject = t1object; 239620e4afbfSKonstantin Belousov } 239720e4afbfSKonstantin Belousov vm_page_busy_sleep(tm, "unwbo", true); 239820e4afbfSKonstantin Belousov goto again; 239920e4afbfSKonstantin Belousov } 240003462509SAlan Cox vm_page_unwire(tm, queue); 240163e97555SJeff Roberson vm_page_sunbusy(tm); 240203462509SAlan Cox next_page: 240303462509SAlan Cox pindex++; 240403462509SAlan Cox } 240503462509SAlan Cox /* Release the accumulated object locks. */ 240620e4afbfSKonstantin Belousov for (tobject = object; locked_depth >= 1; locked_depth--) { 240720e4afbfSKonstantin Belousov t1object = tobject->backing_object; 240820e4afbfSKonstantin Belousov VM_OBJECT_RUNLOCK(tobject); 240920e4afbfSKonstantin Belousov tobject = t1object; 241003462509SAlan Cox } 241103462509SAlan Cox } 241203462509SAlan Cox 24130951bd36SEric van Gyzen /* 24140951bd36SEric van Gyzen * Return the vnode for the given object, or NULL if none exists. 24150951bd36SEric van Gyzen * For tmpfs objects, the function may return NULL if there is 24160951bd36SEric van Gyzen * no vnode allocated at the time of the call. 24170951bd36SEric van Gyzen */ 241863e4c6cdSEric van Gyzen struct vnode * 241963e4c6cdSEric van Gyzen vm_object_vnode(vm_object_t object) 242063e4c6cdSEric van Gyzen { 24210951bd36SEric van Gyzen struct vnode *vp; 242263e4c6cdSEric van Gyzen 242363e4c6cdSEric van Gyzen VM_OBJECT_ASSERT_LOCKED(object); 2424a7c198a2SKonstantin Belousov vm_pager_getvp(object, &vp, NULL); 24250951bd36SEric van Gyzen return (vp); 242663e4c6cdSEric van Gyzen } 242763e4c6cdSEric van Gyzen 2428205be21dSJeff Roberson /* 2429205be21dSJeff Roberson * Busy the vm object. This prevents new pages belonging to the object from 2430205be21dSJeff Roberson * becoming busy. Existing pages persist as busy. Callers are responsible 2431205be21dSJeff Roberson * for checking page state before proceeding. 2432205be21dSJeff Roberson */ 2433205be21dSJeff Roberson void 2434205be21dSJeff Roberson vm_object_busy(vm_object_t obj) 2435205be21dSJeff Roberson { 2436205be21dSJeff Roberson 2437205be21dSJeff Roberson VM_OBJECT_ASSERT_LOCKED(obj); 2438205be21dSJeff Roberson 2439c99d0c58SMark Johnston blockcount_acquire(&obj->busy, 1); 2440205be21dSJeff Roberson /* The fence is required to order loads of page busy. */ 2441205be21dSJeff Roberson atomic_thread_fence_acq_rel(); 2442205be21dSJeff Roberson } 2443205be21dSJeff Roberson 2444205be21dSJeff Roberson void 2445205be21dSJeff Roberson vm_object_unbusy(vm_object_t obj) 2446205be21dSJeff Roberson { 2447205be21dSJeff Roberson 2448c99d0c58SMark Johnston blockcount_release(&obj->busy, 1); 2449205be21dSJeff Roberson } 2450205be21dSJeff Roberson 2451205be21dSJeff Roberson void 2452205be21dSJeff Roberson vm_object_busy_wait(vm_object_t obj, const char *wmesg) 2453205be21dSJeff Roberson { 2454205be21dSJeff Roberson 2455205be21dSJeff Roberson VM_OBJECT_ASSERT_UNLOCKED(obj); 2456205be21dSJeff Roberson 2457c99d0c58SMark Johnston (void)blockcount_sleep(&obj->busy, NULL, wmesg, PVM); 2458205be21dSJeff Roberson } 2459205be21dSJeff Roberson 2460ff87ae35SJohn Baldwin static int 2461ff87ae35SJohn Baldwin sysctl_vm_object_list(SYSCTL_HANDLER_ARGS) 2462ff87ae35SJohn Baldwin { 24630ecee546SKonstantin Belousov struct kinfo_vmobject *kvo; 2464ff87ae35SJohn Baldwin char *fullpath, *freepath; 2465ff87ae35SJohn Baldwin struct vnode *vp; 2466ff87ae35SJohn Baldwin struct vattr va; 2467ff87ae35SJohn Baldwin vm_object_t obj; 2468ff87ae35SJohn Baldwin vm_page_t m; 2469ecfbddf0SKonstantin Belousov u_long sp; 2470ff87ae35SJohn Baldwin int count, error; 2471ff87ae35SJohn Baldwin 2472ff87ae35SJohn Baldwin if (req->oldptr == NULL) { 2473ff87ae35SJohn Baldwin /* 2474ff87ae35SJohn Baldwin * If an old buffer has not been provided, generate an 2475ff87ae35SJohn Baldwin * estimate of the space needed for a subsequent call. 2476ff87ae35SJohn Baldwin */ 2477ff87ae35SJohn Baldwin mtx_lock(&vm_object_list_mtx); 2478ff87ae35SJohn Baldwin count = 0; 2479ff87ae35SJohn Baldwin TAILQ_FOREACH(obj, &vm_object_list, object_list) { 2480ff87ae35SJohn Baldwin if (obj->type == OBJT_DEAD) 2481ff87ae35SJohn Baldwin continue; 2482ff87ae35SJohn Baldwin count++; 2483ff87ae35SJohn Baldwin } 2484ff87ae35SJohn Baldwin mtx_unlock(&vm_object_list_mtx); 2485ff87ae35SJohn Baldwin return (SYSCTL_OUT(req, NULL, sizeof(struct kinfo_vmobject) * 2486ff87ae35SJohn Baldwin count * 11 / 10)); 2487ff87ae35SJohn Baldwin } 2488ff87ae35SJohn Baldwin 24890ecee546SKonstantin Belousov kvo = malloc(sizeof(*kvo), M_TEMP, M_WAITOK); 2490ff87ae35SJohn Baldwin error = 0; 2491ff87ae35SJohn Baldwin 2492ff87ae35SJohn Baldwin /* 2493ff87ae35SJohn Baldwin * VM objects are type stable and are never removed from the 2494ff87ae35SJohn Baldwin * list once added. This allows us to safely read obj->object_list 2495ff87ae35SJohn Baldwin * after reacquiring the VM object lock. 2496ff87ae35SJohn Baldwin */ 2497ff87ae35SJohn Baldwin mtx_lock(&vm_object_list_mtx); 2498ff87ae35SJohn Baldwin TAILQ_FOREACH(obj, &vm_object_list, object_list) { 2499ff87ae35SJohn Baldwin if (obj->type == OBJT_DEAD) 2500ff87ae35SJohn Baldwin continue; 2501ff87ae35SJohn Baldwin VM_OBJECT_RLOCK(obj); 2502ff87ae35SJohn Baldwin if (obj->type == OBJT_DEAD) { 2503ff87ae35SJohn Baldwin VM_OBJECT_RUNLOCK(obj); 2504ff87ae35SJohn Baldwin continue; 2505ff87ae35SJohn Baldwin } 2506ff87ae35SJohn Baldwin mtx_unlock(&vm_object_list_mtx); 25070ecee546SKonstantin Belousov kvo->kvo_size = ptoa(obj->size); 25080ecee546SKonstantin Belousov kvo->kvo_resident = obj->resident_page_count; 25090ecee546SKonstantin Belousov kvo->kvo_ref_count = obj->ref_count; 25100ecee546SKonstantin Belousov kvo->kvo_shadow_count = obj->shadow_count; 25110ecee546SKonstantin Belousov kvo->kvo_memattr = obj->memattr; 25120ecee546SKonstantin Belousov kvo->kvo_active = 0; 25130ecee546SKonstantin Belousov kvo->kvo_inactive = 0; 2514ff87ae35SJohn Baldwin TAILQ_FOREACH(m, &obj->memq, listq) { 2515ff87ae35SJohn Baldwin /* 2516ff87ae35SJohn Baldwin * A page may belong to the object but be 2517ff87ae35SJohn Baldwin * dequeued and set to PQ_NONE while the 2518ff87ae35SJohn Baldwin * object lock is not held. This makes the 2519ff87ae35SJohn Baldwin * reads of m->queue below racy, and we do not 2520ff87ae35SJohn Baldwin * count pages set to PQ_NONE. However, this 2521ff87ae35SJohn Baldwin * sysctl is only meant to give an 2522ff87ae35SJohn Baldwin * approximation of the system anyway. 2523ff87ae35SJohn Baldwin */ 25245cff1f4dSMark Johnston if (m->a.queue == PQ_ACTIVE) 25250ecee546SKonstantin Belousov kvo->kvo_active++; 25265cff1f4dSMark Johnston else if (m->a.queue == PQ_INACTIVE) 25270ecee546SKonstantin Belousov kvo->kvo_inactive++; 2528ff87ae35SJohn Baldwin } 2529ff87ae35SJohn Baldwin 25300ecee546SKonstantin Belousov kvo->kvo_vn_fileid = 0; 25310ecee546SKonstantin Belousov kvo->kvo_vn_fsid = 0; 25320ecee546SKonstantin Belousov kvo->kvo_vn_fsid_freebsd11 = 0; 2533ff87ae35SJohn Baldwin freepath = NULL; 2534ff87ae35SJohn Baldwin fullpath = ""; 25355e38e3f5SEric van Gyzen kvo->kvo_type = vm_object_kvme_type(obj, &vp); 2536ecfbddf0SKonstantin Belousov if (vp != NULL) { 2537ff87ae35SJohn Baldwin vref(vp); 2538ecfbddf0SKonstantin Belousov } else if ((obj->flags & OBJ_ANON) != 0) { 2539ecfbddf0SKonstantin Belousov MPASS(kvo->kvo_type == KVME_TYPE_DEFAULT || 2540ecfbddf0SKonstantin Belousov kvo->kvo_type == KVME_TYPE_SWAP); 2541ecfbddf0SKonstantin Belousov kvo->kvo_me = (uintptr_t)obj; 2542ecfbddf0SKonstantin Belousov /* tmpfs objs are reported as vnodes */ 2543ecfbddf0SKonstantin Belousov kvo->kvo_backing_obj = (uintptr_t)obj->backing_object; 2544ecfbddf0SKonstantin Belousov sp = swap_pager_swapped_pages(obj); 2545ecfbddf0SKonstantin Belousov kvo->kvo_swapped = sp > UINT32_MAX ? UINT32_MAX : sp; 2546ecfbddf0SKonstantin Belousov } 2547ff87ae35SJohn Baldwin VM_OBJECT_RUNLOCK(obj); 2548ff87ae35SJohn Baldwin if (vp != NULL) { 2549feabaaf9SMateusz Guzik vn_fullpath(vp, &fullpath, &freepath); 2550ff87ae35SJohn Baldwin vn_lock(vp, LK_SHARED | LK_RETRY); 2551ff87ae35SJohn Baldwin if (VOP_GETATTR(vp, &va, curthread->td_ucred) == 0) { 25520ecee546SKonstantin Belousov kvo->kvo_vn_fileid = va.va_fileid; 25530ecee546SKonstantin Belousov kvo->kvo_vn_fsid = va.va_fsid; 25540ecee546SKonstantin Belousov kvo->kvo_vn_fsid_freebsd11 = va.va_fsid; 255569921123SKonstantin Belousov /* truncate */ 2556ff87ae35SJohn Baldwin } 2557ff87ae35SJohn Baldwin vput(vp); 2558ff87ae35SJohn Baldwin } 2559ff87ae35SJohn Baldwin 25600ecee546SKonstantin Belousov strlcpy(kvo->kvo_path, fullpath, sizeof(kvo->kvo_path)); 2561ff87ae35SJohn Baldwin if (freepath != NULL) 2562ff87ae35SJohn Baldwin free(freepath, M_TEMP); 2563ff87ae35SJohn Baldwin 2564ff87ae35SJohn Baldwin /* Pack record size down */ 25650ecee546SKonstantin Belousov kvo->kvo_structsize = offsetof(struct kinfo_vmobject, kvo_path) 25660ecee546SKonstantin Belousov + strlen(kvo->kvo_path) + 1; 25670ecee546SKonstantin Belousov kvo->kvo_structsize = roundup(kvo->kvo_structsize, 2568ff87ae35SJohn Baldwin sizeof(uint64_t)); 25690ecee546SKonstantin Belousov error = SYSCTL_OUT(req, kvo, kvo->kvo_structsize); 2570ff87ae35SJohn Baldwin mtx_lock(&vm_object_list_mtx); 2571ff87ae35SJohn Baldwin if (error) 2572ff87ae35SJohn Baldwin break; 2573ff87ae35SJohn Baldwin } 2574ff87ae35SJohn Baldwin mtx_unlock(&vm_object_list_mtx); 25750ecee546SKonstantin Belousov free(kvo, M_TEMP); 2576ff87ae35SJohn Baldwin return (error); 2577ff87ae35SJohn Baldwin } 2578ff87ae35SJohn Baldwin SYSCTL_PROC(_vm, OID_AUTO, objects, CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_SKIP | 2579ff87ae35SJohn Baldwin CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_object_list, "S,kinfo_vmobject", 2580ff87ae35SJohn Baldwin "List of VM objects"); 2581ff87ae35SJohn Baldwin 2582c7c34a24SBruce Evans #include "opt_ddb.h" 2583c3cb3e12SDavid Greenman #ifdef DDB 2584c7c34a24SBruce Evans #include <sys/kernel.h> 2585c7c34a24SBruce Evans 2586ce9edcf5SPoul-Henning Kamp #include <sys/cons.h> 2587c7c34a24SBruce Evans 2588c7c34a24SBruce Evans #include <ddb/ddb.h> 2589c7c34a24SBruce Evans 2590cac597e4SBruce Evans static int 25911b40f8c0SMatthew Dillon _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry) 2592a1f6d91cSDavid Greenman { 2593a1f6d91cSDavid Greenman vm_map_t tmpm; 2594a1f6d91cSDavid Greenman vm_map_entry_t tmpe; 2595a1f6d91cSDavid Greenman vm_object_t obj; 2596a1f6d91cSDavid Greenman 2597a1f6d91cSDavid Greenman if (map == 0) 2598a1f6d91cSDavid Greenman return 0; 2599a1f6d91cSDavid Greenman 2600a1f6d91cSDavid Greenman if (entry == 0) { 26012288078cSDoug Moore VM_MAP_ENTRY_FOREACH(tmpe, map) { 2602a1f6d91cSDavid Greenman if (_vm_object_in_map(map, object, tmpe)) { 2603a1f6d91cSDavid Greenman return 1; 2604a1f6d91cSDavid Greenman } 2605a1f6d91cSDavid Greenman } 26069fdfe602SMatthew Dillon } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 26079fdfe602SMatthew Dillon tmpm = entry->object.sub_map; 26082288078cSDoug Moore VM_MAP_ENTRY_FOREACH(tmpe, tmpm) { 2609a1f6d91cSDavid Greenman if (_vm_object_in_map(tmpm, object, tmpe)) { 2610a1f6d91cSDavid Greenman return 1; 2611a1f6d91cSDavid Greenman } 2612a1f6d91cSDavid Greenman } 26138aef1712SMatthew Dillon } else if ((obj = entry->object.vm_object) != NULL) { 261424a1cce3SDavid Greenman for (; obj; obj = obj->backing_object) 2615a1f6d91cSDavid Greenman if (obj == object) { 2616a1f6d91cSDavid Greenman return 1; 2617a1f6d91cSDavid Greenman } 2618a1f6d91cSDavid Greenman } 2619a1f6d91cSDavid Greenman return 0; 2620a1f6d91cSDavid Greenman } 2621a1f6d91cSDavid Greenman 2622cac597e4SBruce Evans static int 26231b40f8c0SMatthew Dillon vm_object_in_map(vm_object_t object) 2624a1f6d91cSDavid Greenman { 2625a1f6d91cSDavid Greenman struct proc *p; 26261005a129SJohn Baldwin 262760517fd1SJohn Baldwin /* sx_slock(&allproc_lock); */ 2628f67af5c9SXin LI FOREACH_PROC_IN_SYSTEM(p) { 2629a1f6d91cSDavid Greenman if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */) 2630a1f6d91cSDavid Greenman continue; 2631553629ebSJake Burkholder if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) { 263260517fd1SJohn Baldwin /* sx_sunlock(&allproc_lock); */ 2633a1f6d91cSDavid Greenman return 1; 2634a1f6d91cSDavid Greenman } 2635553629ebSJake Burkholder } 263660517fd1SJohn Baldwin /* sx_sunlock(&allproc_lock); */ 2637a1f6d91cSDavid Greenman if (_vm_object_in_map(kernel_map, object, 0)) 2638a1f6d91cSDavid Greenman return 1; 2639a1f6d91cSDavid Greenman return 0; 2640a1f6d91cSDavid Greenman } 2641a1f6d91cSDavid Greenman 2642c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check) 2643f708ef1bSPoul-Henning Kamp { 2644a1f6d91cSDavid Greenman vm_object_t object; 2645a1f6d91cSDavid Greenman 2646a1f6d91cSDavid Greenman /* 2647a1f6d91cSDavid Greenman * make sure that internal objs are in a map somewhere 2648a1f6d91cSDavid Greenman * and none have zero ref counts. 2649a1f6d91cSDavid Greenman */ 2650cc64b484SAlfred Perlstein TAILQ_FOREACH(object, &vm_object_list, object_list) { 265132362449SKonstantin Belousov if ((object->flags & OBJ_ANON) != 0) { 2652a1f6d91cSDavid Greenman if (object->ref_count == 0) { 26533efc015bSPeter Wemm db_printf("vmochk: internal obj has zero ref count: %ld\n", 26543efc015bSPeter Wemm (long)object->size); 2655a1f6d91cSDavid Greenman } 2656a1f6d91cSDavid Greenman if (!vm_object_in_map(object)) { 2657fc62ef1fSBruce Evans db_printf( 2658fc62ef1fSBruce Evans "vmochk: internal obj is not in a map: " 2659fc62ef1fSBruce Evans "ref: %d, size: %lu: 0x%lx, backing_object: %p\n", 2660fc62ef1fSBruce Evans object->ref_count, (u_long)object->size, 2661fc62ef1fSBruce Evans (u_long)object->size, 2662fc62ef1fSBruce Evans (void *)object->backing_object); 2663a1f6d91cSDavid Greenman } 2664a1f6d91cSDavid Greenman } 26658cc8c586SEric van Gyzen if (db_pager_quit) 26668cc8c586SEric van Gyzen return; 2667a1f6d91cSDavid Greenman } 2668a1f6d91cSDavid Greenman } 2669a1f6d91cSDavid Greenman 267026f9a767SRodney W. Grimes /* 2671df8bae1dSRodney W. Grimes * vm_object_print: [ debug ] 2672df8bae1dSRodney W. Grimes */ 2673c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static) 2674df8bae1dSRodney W. Grimes { 2675c7c34a24SBruce Evans /* XXX convert args. */ 2676c7c34a24SBruce Evans vm_object_t object = (vm_object_t)addr; 2677c7c34a24SBruce Evans boolean_t full = have_addr; 2678c7c34a24SBruce Evans 2679d031cff1SMatthew Dillon vm_page_t p; 2680df8bae1dSRodney W. Grimes 2681c7c34a24SBruce Evans /* XXX count is an (unused) arg. Avoid shadowing it. */ 2682c7c34a24SBruce Evans #define count was_count 2683c7c34a24SBruce Evans 2684d031cff1SMatthew Dillon int count; 2685df8bae1dSRodney W. Grimes 2686df8bae1dSRodney W. Grimes if (object == NULL) 2687df8bae1dSRodney W. Grimes return; 2688df8bae1dSRodney W. Grimes 2689eb95adefSBruce Evans db_iprintf( 2690ef694c1aSEdward Tomasz Napierala "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x ruid %d charge %jx\n", 2691e47cd172SMaxime Henrion object, (int)object->type, (uintmax_t)object->size, 26923364c323SKonstantin Belousov object->resident_page_count, object->ref_count, object->flags, 2693ef694c1aSEdward Tomasz Napierala object->cred ? object->cred->cr_ruid : -1, (uintmax_t)object->charge); 2694e47cd172SMaxime Henrion db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n", 26951c7c3c6aSMatthew Dillon object->shadow_count, 2696eb95adefSBruce Evans object->backing_object ? object->backing_object->ref_count : 0, 2697e47cd172SMaxime Henrion object->backing_object, (uintmax_t)object->backing_object_offset); 2698df8bae1dSRodney W. Grimes 2699df8bae1dSRodney W. Grimes if (!full) 2700df8bae1dSRodney W. Grimes return; 2701df8bae1dSRodney W. Grimes 2702c7c34a24SBruce Evans db_indent += 2; 2703df8bae1dSRodney W. Grimes count = 0; 2704fc2ffbe6SPoul-Henning Kamp TAILQ_FOREACH(p, &object->memq, listq) { 2705df8bae1dSRodney W. Grimes if (count == 0) 2706c7c34a24SBruce Evans db_iprintf("memory:="); 2707df8bae1dSRodney W. Grimes else if (count == 6) { 2708c7c34a24SBruce Evans db_printf("\n"); 2709c7c34a24SBruce Evans db_iprintf(" ..."); 2710df8bae1dSRodney W. Grimes count = 0; 2711df8bae1dSRodney W. Grimes } else 2712c7c34a24SBruce Evans db_printf(","); 2713df8bae1dSRodney W. Grimes count++; 2714df8bae1dSRodney W. Grimes 2715e47cd172SMaxime Henrion db_printf("(off=0x%jx,page=0x%jx)", 2716e47cd172SMaxime Henrion (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p)); 27178cc8c586SEric van Gyzen 27188cc8c586SEric van Gyzen if (db_pager_quit) 27198cc8c586SEric van Gyzen break; 2720df8bae1dSRodney W. Grimes } 2721df8bae1dSRodney W. Grimes if (count != 0) 2722c7c34a24SBruce Evans db_printf("\n"); 2723c7c34a24SBruce Evans db_indent -= 2; 2724df8bae1dSRodney W. Grimes } 27255070c7f8SJohn Dyson 2726c7c34a24SBruce Evans /* XXX. */ 2727c7c34a24SBruce Evans #undef count 2728c7c34a24SBruce Evans 2729c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */ 27305070c7f8SJohn Dyson void 27311b40f8c0SMatthew Dillon vm_object_print( 27321b40f8c0SMatthew Dillon /* db_expr_t */ long addr, 27331b40f8c0SMatthew Dillon boolean_t have_addr, 27341b40f8c0SMatthew Dillon /* db_expr_t */ long count, 27351b40f8c0SMatthew Dillon char *modif) 2736c7c34a24SBruce Evans { 2737c7c34a24SBruce Evans vm_object_print_static(addr, have_addr, count, modif); 2738c7c34a24SBruce Evans } 2739c7c34a24SBruce Evans 2740c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages) 27415070c7f8SJohn Dyson { 27425070c7f8SJohn Dyson vm_object_t object; 2743bb2ac86fSKonstantin Belousov vm_pindex_t fidx; 2744bb2ac86fSKonstantin Belousov vm_paddr_t pa; 2745bb2ac86fSKonstantin Belousov vm_page_t m, prev_m; 2746bb2ac86fSKonstantin Belousov int rcount, nl, c; 2747cc64b484SAlfred Perlstein 2748bb2ac86fSKonstantin Belousov nl = 0; 2749cc64b484SAlfred Perlstein TAILQ_FOREACH(object, &vm_object_list, object_list) { 2750fc62ef1fSBruce Evans db_printf("new object: %p\n", (void *)object); 27515070c7f8SJohn Dyson if (nl > 18) { 27525070c7f8SJohn Dyson c = cngetc(); 27535070c7f8SJohn Dyson if (c != ' ') 27545070c7f8SJohn Dyson return; 27555070c7f8SJohn Dyson nl = 0; 27565070c7f8SJohn Dyson } 27575070c7f8SJohn Dyson nl++; 27585070c7f8SJohn Dyson rcount = 0; 27595070c7f8SJohn Dyson fidx = 0; 2760bb2ac86fSKonstantin Belousov pa = -1; 2761bb2ac86fSKonstantin Belousov TAILQ_FOREACH(m, &object->memq, listq) { 2762bb2ac86fSKonstantin Belousov if (m->pindex > 128) 2763bb2ac86fSKonstantin Belousov break; 2764bb2ac86fSKonstantin Belousov if ((prev_m = TAILQ_PREV(m, pglist, listq)) != NULL && 2765bb2ac86fSKonstantin Belousov prev_m->pindex + 1 != m->pindex) { 27665070c7f8SJohn Dyson if (rcount) { 27673efc015bSPeter Wemm db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 27683efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 27695070c7f8SJohn Dyson if (nl > 18) { 27705070c7f8SJohn Dyson c = cngetc(); 27715070c7f8SJohn Dyson if (c != ' ') 27725070c7f8SJohn Dyson return; 27735070c7f8SJohn Dyson nl = 0; 27745070c7f8SJohn Dyson } 27755070c7f8SJohn Dyson nl++; 27765070c7f8SJohn Dyson rcount = 0; 27775070c7f8SJohn Dyson } 27785070c7f8SJohn Dyson } 27795070c7f8SJohn Dyson if (rcount && 27805070c7f8SJohn Dyson (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) { 27815070c7f8SJohn Dyson ++rcount; 27825070c7f8SJohn Dyson continue; 27835070c7f8SJohn Dyson } 27845070c7f8SJohn Dyson if (rcount) { 27852446e4f0SAlan Cox db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 27863efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 27875070c7f8SJohn Dyson if (nl > 18) { 27885070c7f8SJohn Dyson c = cngetc(); 27895070c7f8SJohn Dyson if (c != ' ') 27905070c7f8SJohn Dyson return; 27915070c7f8SJohn Dyson nl = 0; 27925070c7f8SJohn Dyson } 27935070c7f8SJohn Dyson nl++; 27945070c7f8SJohn Dyson } 2795bb2ac86fSKonstantin Belousov fidx = m->pindex; 27965070c7f8SJohn Dyson pa = VM_PAGE_TO_PHYS(m); 27975070c7f8SJohn Dyson rcount = 1; 27985070c7f8SJohn Dyson } 27995070c7f8SJohn Dyson if (rcount) { 28003efc015bSPeter Wemm db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 28013efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 28025070c7f8SJohn Dyson if (nl > 18) { 28035070c7f8SJohn Dyson c = cngetc(); 28045070c7f8SJohn Dyson if (c != ' ') 28055070c7f8SJohn Dyson return; 28065070c7f8SJohn Dyson nl = 0; 28075070c7f8SJohn Dyson } 28085070c7f8SJohn Dyson nl++; 28095070c7f8SJohn Dyson } 28105070c7f8SJohn Dyson } 28115070c7f8SJohn Dyson } 2812c3cb3e12SDavid Greenman #endif /* DDB */ 2813