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 * 34df8bae1dSRodney W. Grimes * 35df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 36df8bae1dSRodney W. Grimes * All rights reserved. 37df8bae1dSRodney W. Grimes * 38df8bae1dSRodney W. Grimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 39df8bae1dSRodney W. Grimes * 40df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and 41df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright 42df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the 43df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions 44df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 45df8bae1dSRodney W. Grimes * 46df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 47df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 48df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 49df8bae1dSRodney W. Grimes * 50df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to 51df8bae1dSRodney W. Grimes * 52df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 53df8bae1dSRodney W. Grimes * School of Computer Science 54df8bae1dSRodney W. Grimes * Carnegie Mellon University 55df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890 56df8bae1dSRodney W. Grimes * 57df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 58df8bae1dSRodney W. Grimes * rights to redistribute these changes. 59df8bae1dSRodney W. Grimes */ 60df8bae1dSRodney W. Grimes 61df8bae1dSRodney W. Grimes /* 62df8bae1dSRodney W. Grimes * Virtual memory object module. 63df8bae1dSRodney W. Grimes */ 64df8bae1dSRodney W. Grimes 65f8a47341SAlan Cox #include "opt_vm.h" 66f8a47341SAlan Cox 67df8bae1dSRodney W. Grimes #include <sys/systm.h> 68c99d0c58SMark Johnston #include <sys/blockcount.h> 693f289c3fSJeff Roberson #include <sys/cpuset.h> 70*38f5f2a4SKonstantin Belousov #include <sys/jail.h> 71ecfbddf0SKonstantin Belousov #include <sys/limits.h> 72fb919e4dSMark Murray #include <sys/lock.h> 73867a482dSJohn Dyson #include <sys/mman.h> 74cf2819ccSJohn Dyson #include <sys/mount.h> 75b9b7a4beSMatthew Dillon #include <sys/kernel.h> 761b367556SJason Evans #include <sys/mutex.h> 7769748e62SKonstantin Belousov #include <sys/pctrie.h> 7869748e62SKonstantin Belousov #include <sys/proc.h> 79cf27e0d1SJeff Roberson #include <sys/refcount.h> 8069748e62SKonstantin Belousov #include <sys/sx.h> 8169748e62SKonstantin Belousov #include <sys/sysctl.h> 823364c323SKonstantin Belousov #include <sys/resourcevar.h> 83205be21dSJeff Roberson #include <sys/refcount.h> 8489f6b863SAttilio Rao #include <sys/rwlock.h> 85ff87ae35SJohn Baldwin #include <sys/user.h> 86fb919e4dSMark Murray #include <sys/vnode.h> 87fb919e4dSMark Murray #include <sys/vmmeter.h> 88df8bae1dSRodney W. Grimes 89df8bae1dSRodney W. Grimes #include <vm/vm.h> 90efeaf95aSDavid Greenman #include <vm/vm_param.h> 91efeaf95aSDavid Greenman #include <vm/pmap.h> 92efeaf95aSDavid Greenman #include <vm/vm_map.h> 93efeaf95aSDavid Greenman #include <vm/vm_object.h> 94df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 9526f9a767SRodney W. Grimes #include <vm/vm_pageout.h> 960d94caffSDavid Greenman #include <vm/vm_pager.h> 97e2068d0bSJeff Roberson #include <vm/vm_phys.h> 98e2068d0bSJeff Roberson #include <vm/vm_pagequeue.h> 9905f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h> 100a1f6d91cSDavid Greenman #include <vm/vm_kern.h> 101efeaf95aSDavid Greenman #include <vm/vm_extern.h> 102774d251dSAttilio Rao #include <vm/vm_radix.h> 103f8a47341SAlan Cox #include <vm/vm_reserv.h> 104670d17b5SJeff Roberson #include <vm/uma.h> 10526f9a767SRodney W. Grimes 106c53f7aceSDag-Erling Smørgrav static int old_msync; 107c53f7aceSDag-Erling Smørgrav SYSCTL_INT(_vm, OID_AUTO, old_msync, CTLFLAG_RW, &old_msync, 0, 108c53f7aceSDag-Erling Smørgrav "Use old (insecure) msync behavior"); 109c53f7aceSDag-Erling Smørgrav 110757216f3SKonstantin Belousov static int vm_object_page_collect_flush(vm_object_t object, vm_page_t p, 11167d0e293SJeff Roberson int pagerflags, int flags, boolean_t *allclean, 112126d6082SKonstantin Belousov boolean_t *eio); 1133280870dSKonstantin Belousov static boolean_t vm_object_page_remove_write(vm_page_t p, int flags, 11467d0e293SJeff Roberson boolean_t *allclean); 11551b867e5SJeff Roberson static void vm_object_backing_remove(vm_object_t object); 116f6b04d2bSDavid Greenman 117df8bae1dSRodney W. Grimes /* 118df8bae1dSRodney W. Grimes * Virtual memory objects maintain the actual data 119df8bae1dSRodney W. Grimes * associated with allocated virtual memory. A given 120df8bae1dSRodney W. Grimes * page of memory exists within exactly one object. 121df8bae1dSRodney W. Grimes * 122df8bae1dSRodney W. Grimes * An object is only deallocated when all "references" 123df8bae1dSRodney W. Grimes * are given up. Only one "reference" to a given 124df8bae1dSRodney W. Grimes * region of an object should be writeable. 125df8bae1dSRodney W. Grimes * 126df8bae1dSRodney W. Grimes * Associated with each object is a list of all resident 127df8bae1dSRodney W. Grimes * memory pages belonging to that object; this list is 128df8bae1dSRodney W. Grimes * maintained by the "vm_page" module, and locked by the object's 129df8bae1dSRodney W. Grimes * lock. 130df8bae1dSRodney W. Grimes * 131df8bae1dSRodney W. Grimes * Each object also records a "pager" routine which is 132df8bae1dSRodney W. Grimes * used to retrieve (and store) pages to the proper backing 133df8bae1dSRodney W. Grimes * storage. In addition, objects may be backed by other 134df8bae1dSRodney W. Grimes * objects from which they were virtual-copied. 135df8bae1dSRodney W. Grimes * 136df8bae1dSRodney W. Grimes * The only items within the object structure which are 137df8bae1dSRodney W. Grimes * modified after time of creation are: 138df8bae1dSRodney W. Grimes * reference count locked by object's lock 139df8bae1dSRodney W. Grimes * pager routine locked by object's lock 140df8bae1dSRodney W. Grimes * 141df8bae1dSRodney W. Grimes */ 142df8bae1dSRodney W. Grimes 14328f8db14SBruce Evans struct object_q vm_object_list; 144a5698387SAlan Cox struct mtx vm_object_list_mtx; /* lock for object list and count */ 145cccf11b8SAlan Cox 146cccf11b8SAlan Cox struct vm_object kernel_object_store; 147df8bae1dSRodney W. Grimes 1487029da5cSPawel Biernacki static SYSCTL_NODE(_vm_stats, OID_AUTO, object, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 1496472ac3dSEd Schouten "VM object stats"); 150604c2bbcSAlan Cox 151d869a17eSMark Johnston static COUNTER_U64_DEFINE_EARLY(object_collapses); 15211542376SAlan Cox SYSCTL_COUNTER_U64(_vm_stats_object, OID_AUTO, collapses, CTLFLAG_RD, 15311542376SAlan Cox &object_collapses, 15411542376SAlan Cox "VM object collapses"); 155604c2bbcSAlan Cox 156d869a17eSMark Johnston static COUNTER_U64_DEFINE_EARLY(object_bypasses); 15711542376SAlan Cox SYSCTL_COUNTER_U64(_vm_stats_object, OID_AUTO, bypasses, CTLFLAG_RD, 15811542376SAlan Cox &object_bypasses, 15911542376SAlan Cox "VM object bypasses"); 16011542376SAlan Cox 161d869a17eSMark Johnston static COUNTER_U64_DEFINE_EARLY(object_collapse_waits); 16298087a06SJeff Roberson SYSCTL_COUNTER_U64(_vm_stats_object, OID_AUTO, collapse_waits, CTLFLAG_RD, 16398087a06SJeff Roberson &object_collapse_waits, 16498087a06SJeff Roberson "Number of sleeps for collapse"); 16598087a06SJeff Roberson 166670d17b5SJeff Roberson static uma_zone_t obj_zone; 1678355f576SJeff Roberson 168b23f72e9SBrian Feldman static int vm_object_zinit(void *mem, int size, int flags); 1698355f576SJeff Roberson 1708355f576SJeff Roberson #ifdef INVARIANTS 1718355f576SJeff Roberson static void vm_object_zdtor(void *mem, int size, void *arg); 1728355f576SJeff Roberson 1738355f576SJeff Roberson static void 1748355f576SJeff Roberson vm_object_zdtor(void *mem, int size, void *arg) 1758355f576SJeff Roberson { 1768355f576SJeff Roberson vm_object_t object; 1778355f576SJeff Roberson 1788355f576SJeff Roberson object = (vm_object_t)mem; 179e735691bSJohn Baldwin KASSERT(object->ref_count == 0, 180e735691bSJohn Baldwin ("object %p ref_count = %d", object, object->ref_count)); 18143186e53SAlan Cox KASSERT(TAILQ_EMPTY(&object->memq), 182198da1b2SAttilio Rao ("object %p has resident pages in its memq", object)); 183774d251dSAttilio Rao KASSERT(vm_radix_is_empty(&object->rtree), 184774d251dSAttilio Rao ("object %p has resident pages in its trie", object)); 185f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 186f8a47341SAlan Cox KASSERT(LIST_EMPTY(&object->rvq), 187f8a47341SAlan Cox ("object %p has reservations", 188f8a47341SAlan Cox object)); 189f8a47341SAlan Cox #endif 190c99d0c58SMark Johnston KASSERT(!vm_object_busied(object), 191c99d0c58SMark Johnston ("object %p busy = %d", object, blockcount_read(&object->busy))); 1928355f576SJeff Roberson KASSERT(object->resident_page_count == 0, 1938355f576SJeff Roberson ("object %p resident_page_count = %d", 1948355f576SJeff Roberson object, object->resident_page_count)); 1954cf9f5d8SKonstantin Belousov KASSERT(atomic_load_int(&object->shadow_count) == 0, 1968355f576SJeff Roberson ("object %p shadow_count = %d", 1974cf9f5d8SKonstantin Belousov object, atomic_load_int(&object->shadow_count))); 198e735691bSJohn Baldwin KASSERT(object->type == OBJT_DEAD, 199e735691bSJohn Baldwin ("object %p has non-dead type %d", 200e735691bSJohn Baldwin object, object->type)); 201e1979b45SMark Johnston KASSERT(object->charge == 0 && object->cred == NULL, 202e1979b45SMark Johnston ("object %p has non-zero charge %ju (%p)", 203e1979b45SMark Johnston object, (uintmax_t)object->charge, object->cred)); 2048355f576SJeff Roberson } 2058355f576SJeff Roberson #endif 2068355f576SJeff Roberson 207b23f72e9SBrian Feldman static int 208b23f72e9SBrian Feldman vm_object_zinit(void *mem, int size, int flags) 2098355f576SJeff Roberson { 2108355f576SJeff Roberson vm_object_t object; 2118355f576SJeff Roberson 2128355f576SJeff Roberson object = (vm_object_t)mem; 213777a36c5SAlan Cox rw_init_flags(&object->lock, "vmobject", RW_DUPOK | RW_NEW); 2148355f576SJeff Roberson 2158355f576SJeff Roberson /* These are true for any object that has been freed */ 216e735691bSJohn Baldwin object->type = OBJT_DEAD; 217cd1241fbSKonstantin Belousov vm_radix_init(&object->rtree); 21851df5321SJeff Roberson refcount_init(&object->ref_count, 0); 219c99d0c58SMark Johnston blockcount_init(&object->paging_in_progress); 220c99d0c58SMark Johnston blockcount_init(&object->busy); 2218355f576SJeff Roberson object->resident_page_count = 0; 2224cf9f5d8SKonstantin Belousov atomic_store_int(&object->shadow_count, 0); 223f425ab8eSKonstantin Belousov object->flags = OBJ_DEAD; 224e735691bSJohn Baldwin 225e735691bSJohn Baldwin mtx_lock(&vm_object_list_mtx); 226e735691bSJohn Baldwin TAILQ_INSERT_TAIL(&vm_object_list, object, object_list); 227e735691bSJohn Baldwin mtx_unlock(&vm_object_list_mtx); 228b23f72e9SBrian Feldman return (0); 2298355f576SJeff Roberson } 230df8bae1dSRodney W. Grimes 231a4915c21SAttilio Rao static void 23263967687SJeff Roberson _vm_object_allocate(objtype_t type, vm_pindex_t size, u_short flags, 23367388836SKonstantin Belousov vm_object_t object, void *handle) 234df8bae1dSRodney W. Grimes { 2350cddd8f0SMatthew Dillon 236df8bae1dSRodney W. Grimes TAILQ_INIT(&object->memq); 2371c500307SAlan Cox LIST_INIT(&object->shadow_head); 238a1f6d91cSDavid Greenman 23924a1cce3SDavid Greenman object->type = type; 2404b8365d7SKonstantin Belousov object->flags = flags; 2415d32157dSMark Johnston if ((flags & OBJ_SWAP) != 0) { 242f425ab8eSKonstantin Belousov pctrie_init(&object->un_pager.swp.swp_blks); 2435d32157dSMark Johnston object->un_pager.swp.writemappings = 0; 2445d32157dSMark Johnston } 245f425ab8eSKonstantin Belousov 246f425ab8eSKonstantin Belousov /* 247f425ab8eSKonstantin Belousov * Ensure that swap_pager_swapoff() iteration over object_list 248f425ab8eSKonstantin Belousov * sees up to date type and pctrie head if it observed 249f425ab8eSKonstantin Belousov * non-dead object. 250f425ab8eSKonstantin Belousov */ 251f425ab8eSKonstantin Belousov atomic_thread_fence_rel(); 252f425ab8eSKonstantin Belousov 25363967687SJeff Roberson object->pg_color = 0; 254df8bae1dSRodney W. Grimes object->size = size; 2554c29d2deSMark Johnston object->domain.dr_policy = NULL; 256b881da26SAlan Cox object->generation = 1; 25767d0e293SJeff Roberson object->cleangeneration = 1; 25851df5321SJeff Roberson refcount_init(&object->ref_count, 1); 2593153e878SAlan Cox object->memattr = VM_MEMATTR_DEFAULT; 260ef694c1aSEdward Tomasz Napierala object->cred = NULL; 2613364c323SKonstantin Belousov object->charge = 0; 26267388836SKonstantin Belousov object->handle = handle; 26324a1cce3SDavid Greenman object->backing_object = NULL; 264a316d390SJohn Dyson object->backing_object_offset = (vm_ooffset_t) 0; 265f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 266f8a47341SAlan Cox LIST_INIT(&object->rvq); 267f8a47341SAlan Cox #endif 2681bdbd705SKonstantin Belousov umtx_shm_object_init(object); 269df8bae1dSRodney W. Grimes } 270df8bae1dSRodney W. Grimes 271df8bae1dSRodney W. Grimes /* 27226f9a767SRodney W. Grimes * vm_object_init: 27326f9a767SRodney W. Grimes * 27426f9a767SRodney W. Grimes * Initialize the VM objects module. 27526f9a767SRodney W. Grimes */ 27626f9a767SRodney W. Grimes void 2771b40f8c0SMatthew Dillon vm_object_init(void) 27826f9a767SRodney W. Grimes { 27926f9a767SRodney W. Grimes TAILQ_INIT(&vm_object_list); 2806008862bSJohn Baldwin mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF); 2810217125fSDavid Greenman 28289f6b863SAttilio Rao rw_init(&kernel_object->lock, "kernel vm object"); 2833e04ae43SDoug Moore vm_radix_init(&kernel_object->rtree); 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); 3333e7a11caSKonstantin Belousov 3343e7a11caSKonstantin Belousov if (object->type == OBJT_DEAD) 3353e7a11caSKonstantin Belousov return (KERN_INVALID_ARGUMENT); 3363153e878SAlan Cox if (!TAILQ_EMPTY(&object->memq)) 3373153e878SAlan Cox return (KERN_FAILURE); 3383e7a11caSKonstantin 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"); 4124b8365d7SKonstantin Belousov case OBJT_SWAP: 4134b8365d7SKonstantin Belousov flags = OBJ_COLORED | OBJ_SWAP; 4144b8365d7SKonstantin Belousov break; 41563967687SJeff Roberson case OBJT_DEVICE: 41663967687SJeff Roberson case OBJT_SG: 41763967687SJeff Roberson flags = OBJ_FICTITIOUS | OBJ_UNMANAGED; 41863967687SJeff Roberson break; 41963967687SJeff Roberson case OBJT_MGTDEVICE: 42063967687SJeff Roberson flags = OBJ_FICTITIOUS; 42163967687SJeff Roberson break; 42263967687SJeff Roberson case OBJT_PHYS: 42363967687SJeff Roberson flags = OBJ_UNMANAGED; 42463967687SJeff Roberson break; 42563967687SJeff Roberson case OBJT_VNODE: 42663967687SJeff Roberson flags = 0; 42763967687SJeff Roberson break; 42863967687SJeff Roberson default: 429b730fd30SKonstantin Belousov panic("vm_object_allocate: type %d is undefined or dynamic", 430b730fd30SKonstantin Belousov type); 43163967687SJeff Roberson } 43263967687SJeff Roberson object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK); 43367388836SKonstantin Belousov _vm_object_allocate(type, size, flags, object, NULL); 43463967687SJeff Roberson 43563967687SJeff Roberson return (object); 43663967687SJeff Roberson } 43763967687SJeff Roberson 438b730fd30SKonstantin Belousov vm_object_t 439b730fd30SKonstantin Belousov vm_object_allocate_dyn(objtype_t dyntype, vm_pindex_t size, u_short flags) 440b730fd30SKonstantin Belousov { 441b730fd30SKonstantin Belousov vm_object_t object; 442b730fd30SKonstantin Belousov 443b730fd30SKonstantin Belousov MPASS(dyntype >= OBJT_FIRST_DYN /* && dyntype < nitems(pagertab) */); 444b730fd30SKonstantin Belousov object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK); 445b730fd30SKonstantin Belousov _vm_object_allocate(dyntype, size, flags, object, NULL); 446b730fd30SKonstantin Belousov 447b730fd30SKonstantin Belousov return (object); 448b730fd30SKonstantin Belousov } 449b730fd30SKonstantin Belousov 45063967687SJeff Roberson /* 45163967687SJeff Roberson * vm_object_allocate_anon: 45263967687SJeff Roberson * 45363967687SJeff Roberson * Returns a new default object of the given size and marked as 45463967687SJeff Roberson * anonymous memory for special split/collapse handling. Color 45563967687SJeff Roberson * to be initialized by the caller. 45663967687SJeff Roberson */ 45763967687SJeff Roberson vm_object_t 45867388836SKonstantin Belousov vm_object_allocate_anon(vm_pindex_t size, vm_object_t backing_object, 45967388836SKonstantin Belousov struct ucred *cred, vm_size_t charge) 46063967687SJeff Roberson { 46167388836SKonstantin Belousov vm_object_t handle, object; 46290688d13SAlan Cox 46367388836SKonstantin Belousov if (backing_object == NULL) 46467388836SKonstantin Belousov handle = NULL; 46567388836SKonstantin Belousov else if ((backing_object->flags & OBJ_ANON) != 0) 46667388836SKonstantin Belousov handle = backing_object->handle; 46767388836SKonstantin Belousov else 46867388836SKonstantin Belousov handle = backing_object; 46967388836SKonstantin Belousov object = uma_zalloc(obj_zone, M_WAITOK); 4705d32157dSMark Johnston _vm_object_allocate(OBJT_SWAP, size, 4715d32157dSMark Johnston OBJ_ANON | OBJ_ONEMAPPING | OBJ_SWAP, object, handle); 47267388836SKonstantin Belousov object->cred = cred; 47367388836SKonstantin Belousov object->charge = cred != NULL ? charge : 0; 47490688d13SAlan Cox return (object); 47526f9a767SRodney W. Grimes } 47626f9a767SRodney W. Grimes 47798087a06SJeff Roberson static void 47898087a06SJeff Roberson vm_object_reference_vnode(vm_object_t object) 479df8bae1dSRodney W. Grimes { 480a67d5408SJeff Roberson u_int old; 481a67d5408SJeff Roberson 482a67d5408SJeff Roberson /* 48398087a06SJeff Roberson * vnode objects need the lock for the first reference 48498087a06SJeff Roberson * to serialize with vnode_object_deallocate(). 485a67d5408SJeff Roberson */ 48698087a06SJeff Roberson if (!refcount_acquire_if_gt(&object->ref_count, 0)) { 48751df5321SJeff Roberson VM_OBJECT_RLOCK(object); 488a67d5408SJeff Roberson old = refcount_acquire(&object->ref_count); 4891a0c234eSJeff Roberson if (object->type == OBJT_VNODE && old == 0) 4901a0c234eSJeff Roberson vref(object->handle); 49151df5321SJeff Roberson VM_OBJECT_RUNLOCK(object); 49295e5e988SJohn Dyson } 493a67d5408SJeff Roberson } 49495e5e988SJohn Dyson 49523955314SAlfred Perlstein /* 49698087a06SJeff Roberson * vm_object_reference: 49798087a06SJeff Roberson * 49898087a06SJeff Roberson * Acquires a reference to the given object. 49998087a06SJeff Roberson */ 50098087a06SJeff Roberson void 50198087a06SJeff Roberson vm_object_reference(vm_object_t object) 50298087a06SJeff Roberson { 50398087a06SJeff Roberson 50498087a06SJeff Roberson if (object == NULL) 50598087a06SJeff Roberson return; 50698087a06SJeff Roberson 50798087a06SJeff Roberson if (object->type == OBJT_VNODE) 50898087a06SJeff Roberson vm_object_reference_vnode(object); 50998087a06SJeff Roberson else 51098087a06SJeff Roberson refcount_acquire(&object->ref_count); 51198087a06SJeff Roberson KASSERT((object->flags & OBJ_DEAD) == 0, 51298087a06SJeff Roberson ("vm_object_reference: Referenced dead object.")); 51398087a06SJeff Roberson } 51498087a06SJeff Roberson 51598087a06SJeff Roberson /* 516b921a12bSAlan Cox * vm_object_reference_locked: 517b921a12bSAlan Cox * 518b921a12bSAlan Cox * Gets another reference to the given object. 519b921a12bSAlan Cox * 520b921a12bSAlan Cox * The object must be locked. 521b921a12bSAlan Cox */ 522b921a12bSAlan Cox void 523b921a12bSAlan Cox vm_object_reference_locked(vm_object_t object) 524b921a12bSAlan Cox { 525a67d5408SJeff Roberson u_int old; 526b921a12bSAlan Cox 52751df5321SJeff Roberson VM_OBJECT_ASSERT_LOCKED(object); 528a67d5408SJeff Roberson old = refcount_acquire(&object->ref_count); 5291a0c234eSJeff Roberson if (object->type == OBJT_VNODE && old == 0) 5301a0c234eSJeff Roberson vref(object->handle); 53198087a06SJeff Roberson KASSERT((object->flags & OBJ_DEAD) == 0, 53298087a06SJeff Roberson ("vm_object_reference: Referenced dead object.")); 533b921a12bSAlan Cox } 534b921a12bSAlan Cox 535b921a12bSAlan Cox /* 5369d5abbddSJens Schweikhardt * Handle deallocating an object of type OBJT_VNODE. 53723955314SAlfred Perlstein */ 53802dd8331SAlan Cox static void 53998087a06SJeff Roberson vm_object_deallocate_vnode(vm_object_t object) 54095e5e988SJohn Dyson { 54195e5e988SJohn Dyson struct vnode *vp = (struct vnode *) object->handle; 54226c4e983SJeff Roberson bool last; 543219cbf59SEivind Eklund 5445526d2d9SEivind Eklund KASSERT(object->type == OBJT_VNODE, 54598087a06SJeff Roberson ("vm_object_deallocate_vnode: not a vnode object")); 54698087a06SJeff Roberson KASSERT(vp != NULL, ("vm_object_deallocate_vnode: missing vp")); 54795e5e988SJohn Dyson 54826c4e983SJeff Roberson /* Object lock to protect handle lookup. */ 54926c4e983SJeff Roberson last = refcount_release(&object->ref_count); 55026c4e983SJeff Roberson VM_OBJECT_RUNLOCK(object); 55126c4e983SJeff Roberson 55226c4e983SJeff Roberson if (!last) 55326c4e983SJeff Roberson return; 55426c4e983SJeff Roberson 555a67d5408SJeff Roberson if (!umtx_shm_vnobj_persistent) 5561bdbd705SKonstantin Belousov umtx_shm_object_terminated(object); 5571bdbd705SKonstantin Belousov 55803fa5b34SKonstantin Belousov /* vrele may need the vnode lock. */ 55947221757SJohn Dyson vrele(vp); 56086769ac0SKonstantin Belousov } 561df8bae1dSRodney W. Grimes 56298087a06SJeff Roberson /* 56398087a06SJeff Roberson * We dropped a reference on an object and discovered that it had a 56498087a06SJeff Roberson * single remaining shadow. This is a sibling of the reference we 56598087a06SJeff Roberson * dropped. Attempt to collapse the sibling and backing object. 56698087a06SJeff Roberson */ 56798087a06SJeff Roberson static vm_object_t 56898087a06SJeff Roberson vm_object_deallocate_anon(vm_object_t backing_object) 56998087a06SJeff Roberson { 57098087a06SJeff Roberson vm_object_t object; 57198087a06SJeff Roberson 57298087a06SJeff Roberson /* Fetch the final shadow. */ 57398087a06SJeff Roberson object = LIST_FIRST(&backing_object->shadow_head); 5744cf9f5d8SKonstantin Belousov KASSERT(object != NULL && 5754cf9f5d8SKonstantin Belousov atomic_load_int(&backing_object->shadow_count) == 1, 57698087a06SJeff Roberson ("vm_object_anon_deallocate: ref_count: %d, shadow_count: %d", 5774cf9f5d8SKonstantin Belousov backing_object->ref_count, 5784cf9f5d8SKonstantin Belousov atomic_load_int(&backing_object->shadow_count))); 5794b8365d7SKonstantin Belousov KASSERT((object->flags & OBJ_ANON) != 0, 58098087a06SJeff Roberson ("invalid shadow object %p", object)); 58198087a06SJeff Roberson 58298087a06SJeff Roberson if (!VM_OBJECT_TRYWLOCK(object)) { 58398087a06SJeff Roberson /* 58498087a06SJeff Roberson * Prevent object from disappearing since we do not have a 58598087a06SJeff Roberson * reference. 58698087a06SJeff Roberson */ 58798087a06SJeff Roberson vm_object_pip_add(object, 1); 58898087a06SJeff Roberson VM_OBJECT_WUNLOCK(backing_object); 58998087a06SJeff Roberson VM_OBJECT_WLOCK(object); 59098087a06SJeff Roberson vm_object_pip_wakeup(object); 59198087a06SJeff Roberson } else 59298087a06SJeff Roberson VM_OBJECT_WUNLOCK(backing_object); 59398087a06SJeff Roberson 59498087a06SJeff Roberson /* 59598087a06SJeff Roberson * Check for a collapse/terminate race with the last reference holder. 59698087a06SJeff Roberson */ 59798087a06SJeff Roberson if ((object->flags & (OBJ_DEAD | OBJ_COLLAPSING)) != 0 || 59898087a06SJeff Roberson !refcount_acquire_if_not_zero(&object->ref_count)) { 59998087a06SJeff Roberson VM_OBJECT_WUNLOCK(object); 60098087a06SJeff Roberson return (NULL); 60198087a06SJeff Roberson } 60298087a06SJeff Roberson backing_object = object->backing_object; 60398087a06SJeff Roberson if (backing_object != NULL && (backing_object->flags & OBJ_ANON) != 0) 60498087a06SJeff Roberson vm_object_collapse(object); 60598087a06SJeff Roberson VM_OBJECT_WUNLOCK(object); 60698087a06SJeff Roberson 60798087a06SJeff Roberson return (object); 60898087a06SJeff Roberson } 60998087a06SJeff Roberson 610df8bae1dSRodney W. Grimes /* 611df8bae1dSRodney W. Grimes * vm_object_deallocate: 612df8bae1dSRodney W. Grimes * 613df8bae1dSRodney W. Grimes * Release a reference to the specified object, 614df8bae1dSRodney W. Grimes * gained either through a vm_object_allocate 615df8bae1dSRodney W. Grimes * or a vm_object_reference call. When all references 616df8bae1dSRodney W. Grimes * are gone, storage associated with this object 617df8bae1dSRodney W. Grimes * may be relinquished. 618df8bae1dSRodney W. Grimes * 619df8bae1dSRodney W. Grimes * No object may be locked. 620df8bae1dSRodney W. Grimes */ 62126f9a767SRodney W. Grimes void 6221b40f8c0SMatthew Dillon vm_object_deallocate(vm_object_t object) 623df8bae1dSRodney W. Grimes { 62498087a06SJeff Roberson vm_object_t temp; 62526c4e983SJeff Roberson bool released; 626df8bae1dSRodney W. Grimes 627df8bae1dSRodney W. Grimes while (object != NULL) { 62851df5321SJeff Roberson /* 62951df5321SJeff Roberson * If the reference count goes to 0 we start calling 63051df5321SJeff Roberson * vm_object_terminate() on the object chain. A ref count 63151df5321SJeff Roberson * of 1 may be a special case depending on the shadow count 63251df5321SJeff Roberson * being 0 or 1. These cases require a write lock on the 63351df5321SJeff Roberson * object. 63451df5321SJeff Roberson */ 63563967687SJeff Roberson if ((object->flags & OBJ_ANON) == 0) 63663967687SJeff Roberson released = refcount_release_if_gt(&object->ref_count, 1); 63763967687SJeff Roberson else 63851df5321SJeff Roberson released = refcount_release_if_gt(&object->ref_count, 2); 63951df5321SJeff Roberson if (released) 64051df5321SJeff Roberson return; 64151df5321SJeff Roberson 642a67d5408SJeff Roberson if (object->type == OBJT_VNODE) { 64326c4e983SJeff Roberson VM_OBJECT_RLOCK(object); 64426c4e983SJeff Roberson if (object->type == OBJT_VNODE) { 64598087a06SJeff Roberson vm_object_deallocate_vnode(object); 646a67d5408SJeff Roberson return; 647a67d5408SJeff Roberson } 64826c4e983SJeff Roberson VM_OBJECT_RUNLOCK(object); 64926c4e983SJeff Roberson } 65026c4e983SJeff Roberson 65126c4e983SJeff Roberson VM_OBJECT_WLOCK(object); 65226c4e983SJeff Roberson KASSERT(object->ref_count > 0, 65326c4e983SJeff Roberson ("vm_object_deallocate: object deallocated too many times: %d", 65426c4e983SJeff Roberson object->type)); 65526c4e983SJeff Roberson 656b72b0115SAlan Cox /* 65798087a06SJeff Roberson * If this is not the final reference to an anonymous 65898087a06SJeff Roberson * object we may need to collapse the shadow chain. 659b72b0115SAlan Cox */ 66098087a06SJeff Roberson if (!refcount_release(&object->ref_count)) { 66198087a06SJeff Roberson if (object->ref_count > 1 || 6624cf9f5d8SKonstantin Belousov atomic_load_int(&object->shadow_count) == 0) { 66398087a06SJeff Roberson if ((object->flags & OBJ_ANON) != 0 && 66498087a06SJeff Roberson object->ref_count == 1) 66598087a06SJeff Roberson vm_object_set_flag(object, 66698087a06SJeff Roberson OBJ_ONEMAPPING); 66789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 66823b186d3SAlan Cox return; 66995e5e988SJohn Dyson } 67098087a06SJeff Roberson 67198087a06SJeff Roberson /* Handle collapsing last ref on anonymous objects. */ 67298087a06SJeff Roberson object = vm_object_deallocate_anon(object); 67398087a06SJeff Roberson continue; 67498087a06SJeff Roberson } 67598087a06SJeff Roberson 67698087a06SJeff Roberson /* 67798087a06SJeff Roberson * Handle the final reference to an object. We restart 67898087a06SJeff Roberson * the loop with the backing object to avoid recursion. 67998087a06SJeff Roberson */ 6801bdbd705SKonstantin Belousov umtx_shm_object_terminated(object); 68124a1cce3SDavid Greenman temp = object->backing_object; 682c9917419SAlan Cox if (temp != NULL) { 6830cb2610eSMark Johnston KASSERT(object->type == OBJT_SWAP, 6844bace8e7SKonstantin Belousov ("shadowed tmpfs v_object 2 %p", object)); 68551b867e5SJeff Roberson vm_object_backing_remove(object); 686de5f6a77SJohn Dyson } 68798087a06SJeff Roberson 68898087a06SJeff Roberson KASSERT((object->flags & OBJ_DEAD) == 0, 68998087a06SJeff Roberson ("vm_object_deallocate: Terminating dead object.")); 690783a68aaSKonstantin Belousov vm_object_set_flag(object, OBJ_DEAD); 691df8bae1dSRodney W. Grimes vm_object_terminate(object); 692df8bae1dSRodney W. Grimes object = temp; 693df8bae1dSRodney W. Grimes } 694df8bae1dSRodney W. Grimes } 695df8bae1dSRodney W. Grimes 6962ac78f0eSStephan Uphoff void 6972ac78f0eSStephan Uphoff vm_object_destroy(vm_object_t object) 6982ac78f0eSStephan Uphoff { 6992ac78f0eSStephan Uphoff uma_zfree(obj_zone, object); 7002ac78f0eSStephan Uphoff } 7012ac78f0eSStephan Uphoff 70251b867e5SJeff Roberson static void 7034cf9f5d8SKonstantin Belousov vm_object_sub_shadow(vm_object_t object) 7044cf9f5d8SKonstantin Belousov { 7054cf9f5d8SKonstantin Belousov KASSERT(object->shadow_count >= 1, 7064cf9f5d8SKonstantin Belousov ("object %p sub_shadow count zero", object)); 7074cf9f5d8SKonstantin Belousov atomic_subtract_int(&object->shadow_count, 1); 7084cf9f5d8SKonstantin Belousov } 7094cf9f5d8SKonstantin Belousov 7104cf9f5d8SKonstantin Belousov static void 71151b867e5SJeff Roberson vm_object_backing_remove_locked(vm_object_t object) 71251b867e5SJeff Roberson { 71351b867e5SJeff Roberson vm_object_t backing_object; 71451b867e5SJeff Roberson 71551b867e5SJeff Roberson backing_object = object->backing_object; 71651b867e5SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 71751b867e5SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(backing_object); 71851b867e5SJeff Roberson 71998087a06SJeff Roberson KASSERT((object->flags & OBJ_COLLAPSING) == 0, 72098087a06SJeff Roberson ("vm_object_backing_remove: Removing collapsing object.")); 72198087a06SJeff Roberson 7224cf9f5d8SKonstantin Belousov vm_object_sub_shadow(backing_object); 72351b867e5SJeff Roberson if ((object->flags & OBJ_SHADOWLIST) != 0) { 72451b867e5SJeff Roberson LIST_REMOVE(object, shadow_list); 725630f633fSMark Johnston vm_object_clear_flag(object, OBJ_SHADOWLIST); 72651b867e5SJeff Roberson } 72751b867e5SJeff Roberson object->backing_object = NULL; 72851b867e5SJeff Roberson } 72951b867e5SJeff Roberson 73051b867e5SJeff Roberson static void 73151b867e5SJeff Roberson vm_object_backing_remove(vm_object_t object) 73251b867e5SJeff Roberson { 73351b867e5SJeff Roberson vm_object_t backing_object; 73451b867e5SJeff Roberson 73551b867e5SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 73651b867e5SJeff Roberson 73751b867e5SJeff Roberson backing_object = object->backing_object; 7384cf9f5d8SKonstantin Belousov if ((object->flags & OBJ_SHADOWLIST) != 0) { 73951b867e5SJeff Roberson VM_OBJECT_WLOCK(backing_object); 74051b867e5SJeff Roberson vm_object_backing_remove_locked(object); 74151b867e5SJeff Roberson VM_OBJECT_WUNLOCK(backing_object); 7424cf9f5d8SKonstantin Belousov } else { 74351b867e5SJeff Roberson object->backing_object = NULL; 7444cf9f5d8SKonstantin Belousov vm_object_sub_shadow(backing_object); 7454cf9f5d8SKonstantin Belousov } 74651b867e5SJeff Roberson } 74751b867e5SJeff Roberson 74851b867e5SJeff Roberson static void 74951b867e5SJeff Roberson vm_object_backing_insert_locked(vm_object_t object, vm_object_t backing_object) 75051b867e5SJeff Roberson { 75151b867e5SJeff Roberson 75251b867e5SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 75351b867e5SJeff Roberson 7544cf9f5d8SKonstantin Belousov atomic_add_int(&backing_object->shadow_count, 1); 75551b867e5SJeff Roberson if ((backing_object->flags & OBJ_ANON) != 0) { 75651b867e5SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(backing_object); 75751b867e5SJeff Roberson LIST_INSERT_HEAD(&backing_object->shadow_head, object, 75851b867e5SJeff Roberson shadow_list); 759630f633fSMark Johnston vm_object_set_flag(object, OBJ_SHADOWLIST); 76051b867e5SJeff Roberson } 76151b867e5SJeff Roberson object->backing_object = backing_object; 76251b867e5SJeff Roberson } 76351b867e5SJeff Roberson 76451b867e5SJeff Roberson static void 76551b867e5SJeff Roberson vm_object_backing_insert(vm_object_t object, vm_object_t backing_object) 76651b867e5SJeff Roberson { 76751b867e5SJeff Roberson 76851b867e5SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 76951b867e5SJeff Roberson 77051b867e5SJeff Roberson if ((backing_object->flags & OBJ_ANON) != 0) { 77151b867e5SJeff Roberson VM_OBJECT_WLOCK(backing_object); 77251b867e5SJeff Roberson vm_object_backing_insert_locked(object, backing_object); 77351b867e5SJeff Roberson VM_OBJECT_WUNLOCK(backing_object); 7744cf9f5d8SKonstantin Belousov } else { 77551b867e5SJeff Roberson object->backing_object = backing_object; 7764cf9f5d8SKonstantin Belousov atomic_add_int(&backing_object->shadow_count, 1); 7774cf9f5d8SKonstantin Belousov } 77851b867e5SJeff Roberson } 77951b867e5SJeff Roberson 78098087a06SJeff Roberson /* 78198087a06SJeff Roberson * Insert an object into a backing_object's shadow list with an additional 78298087a06SJeff Roberson * reference to the backing_object added. 78398087a06SJeff Roberson */ 78498087a06SJeff Roberson static void 78598087a06SJeff Roberson vm_object_backing_insert_ref(vm_object_t object, vm_object_t backing_object) 78698087a06SJeff Roberson { 78798087a06SJeff Roberson 78898087a06SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 78998087a06SJeff Roberson 79098087a06SJeff Roberson if ((backing_object->flags & OBJ_ANON) != 0) { 79198087a06SJeff Roberson VM_OBJECT_WLOCK(backing_object); 79298087a06SJeff Roberson KASSERT((backing_object->flags & OBJ_DEAD) == 0, 79398087a06SJeff Roberson ("shadowing dead anonymous object")); 79498087a06SJeff Roberson vm_object_reference_locked(backing_object); 79598087a06SJeff Roberson vm_object_backing_insert_locked(object, backing_object); 79698087a06SJeff Roberson vm_object_clear_flag(backing_object, OBJ_ONEMAPPING); 79798087a06SJeff Roberson VM_OBJECT_WUNLOCK(backing_object); 79898087a06SJeff Roberson } else { 79998087a06SJeff Roberson vm_object_reference(backing_object); 8004cf9f5d8SKonstantin Belousov atomic_add_int(&backing_object->shadow_count, 1); 80198087a06SJeff Roberson object->backing_object = backing_object; 80298087a06SJeff Roberson } 80398087a06SJeff Roberson } 80498087a06SJeff Roberson 80598087a06SJeff Roberson /* 80698087a06SJeff Roberson * Transfer a backing reference from backing_object to object. 80798087a06SJeff Roberson */ 80898087a06SJeff Roberson static void 80998087a06SJeff Roberson vm_object_backing_transfer(vm_object_t object, vm_object_t backing_object) 81098087a06SJeff Roberson { 81198087a06SJeff Roberson vm_object_t new_backing_object; 81298087a06SJeff Roberson 81398087a06SJeff Roberson /* 81498087a06SJeff Roberson * Note that the reference to backing_object->backing_object 81598087a06SJeff Roberson * moves from within backing_object to within object. 81698087a06SJeff Roberson */ 81798087a06SJeff Roberson vm_object_backing_remove_locked(object); 81898087a06SJeff Roberson new_backing_object = backing_object->backing_object; 81998087a06SJeff Roberson if (new_backing_object == NULL) 82098087a06SJeff Roberson return; 82198087a06SJeff Roberson if ((new_backing_object->flags & OBJ_ANON) != 0) { 82298087a06SJeff Roberson VM_OBJECT_WLOCK(new_backing_object); 82398087a06SJeff Roberson vm_object_backing_remove_locked(backing_object); 82498087a06SJeff Roberson vm_object_backing_insert_locked(object, new_backing_object); 82598087a06SJeff Roberson VM_OBJECT_WUNLOCK(new_backing_object); 82698087a06SJeff Roberson } else { 8274cf9f5d8SKonstantin Belousov /* 8284cf9f5d8SKonstantin Belousov * shadow_count for new_backing_object is left 8294cf9f5d8SKonstantin Belousov * unchanged, its reference provided by backing_object 8304cf9f5d8SKonstantin Belousov * is replaced by object. 8314cf9f5d8SKonstantin Belousov */ 83298087a06SJeff Roberson object->backing_object = new_backing_object; 83398087a06SJeff Roberson backing_object->backing_object = NULL; 83498087a06SJeff Roberson } 83598087a06SJeff Roberson } 83698087a06SJeff Roberson 83798087a06SJeff Roberson /* 83898087a06SJeff Roberson * Wait for a concurrent collapse to settle. 83998087a06SJeff Roberson */ 84098087a06SJeff Roberson static void 84198087a06SJeff Roberson vm_object_collapse_wait(vm_object_t object) 84298087a06SJeff Roberson { 84398087a06SJeff Roberson 84498087a06SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 84598087a06SJeff Roberson 84698087a06SJeff Roberson while ((object->flags & OBJ_COLLAPSING) != 0) { 84798087a06SJeff Roberson vm_object_pip_wait(object, "vmcolwait"); 84898087a06SJeff Roberson counter_u64_add(object_collapse_waits, 1); 84998087a06SJeff Roberson } 85098087a06SJeff Roberson } 85198087a06SJeff Roberson 85298087a06SJeff Roberson /* 85398087a06SJeff Roberson * Waits for a backing object to clear a pending collapse and returns 85498087a06SJeff Roberson * it locked if it is an ANON object. 85598087a06SJeff Roberson */ 85698087a06SJeff Roberson static vm_object_t 85798087a06SJeff Roberson vm_object_backing_collapse_wait(vm_object_t object) 85898087a06SJeff Roberson { 85998087a06SJeff Roberson vm_object_t backing_object; 86098087a06SJeff Roberson 86198087a06SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 86298087a06SJeff Roberson 86398087a06SJeff Roberson for (;;) { 86498087a06SJeff Roberson backing_object = object->backing_object; 86598087a06SJeff Roberson if (backing_object == NULL || 86698087a06SJeff Roberson (backing_object->flags & OBJ_ANON) == 0) 86798087a06SJeff Roberson return (NULL); 86898087a06SJeff Roberson VM_OBJECT_WLOCK(backing_object); 86998087a06SJeff Roberson if ((backing_object->flags & (OBJ_DEAD | OBJ_COLLAPSING)) == 0) 87098087a06SJeff Roberson break; 871cdd02f43SMark Johnston VM_OBJECT_WUNLOCK(object); 87298087a06SJeff Roberson vm_object_pip_sleep(backing_object, "vmbckwait"); 87398087a06SJeff Roberson counter_u64_add(object_collapse_waits, 1); 87498087a06SJeff Roberson VM_OBJECT_WLOCK(object); 87598087a06SJeff Roberson } 87698087a06SJeff Roberson return (backing_object); 87798087a06SJeff Roberson } 87851b867e5SJeff Roberson 8792ac78f0eSStephan Uphoff /* 8807bbdb843SRuslan Bukin * vm_object_terminate_pages removes any remaining pageable pages 8817bbdb843SRuslan Bukin * from the object and resets the object to an empty state. 8827bbdb843SRuslan Bukin */ 8837bbdb843SRuslan Bukin static void 8847bbdb843SRuslan Bukin vm_object_terminate_pages(vm_object_t object) 8857bbdb843SRuslan Bukin { 8867bbdb843SRuslan Bukin vm_page_t p, p_next; 8877bbdb843SRuslan Bukin 8887bbdb843SRuslan Bukin VM_OBJECT_ASSERT_WLOCKED(object); 8897bbdb843SRuslan Bukin 8907bbdb843SRuslan Bukin /* 8917bbdb843SRuslan Bukin * Free any remaining pageable pages. This also removes them from the 8927bbdb843SRuslan Bukin * paging queues. However, don't free wired pages, just remove them 8937bbdb843SRuslan Bukin * from the object. Rather than incrementally removing each page from 8947bbdb843SRuslan Bukin * the object, the page and object are reset to any empty state. 8957bbdb843SRuslan Bukin */ 8967bbdb843SRuslan Bukin TAILQ_FOREACH_SAFE(p, &object->memq, listq, p_next) { 8977bbdb843SRuslan Bukin vm_page_assert_unbusied(p); 898fee2a2faSMark Johnston KASSERT(p->object == object && 899fee2a2faSMark Johnston (p->ref_count & VPRC_OBJREF) != 0, 900fee2a2faSMark Johnston ("vm_object_terminate_pages: page %p is inconsistent", p)); 901fee2a2faSMark Johnston 9027bbdb843SRuslan Bukin p->object = NULL; 903fee2a2faSMark Johnston if (vm_page_drop(p, VPRC_OBJREF) == VPRC_OBJREF) { 9045cd29d0fSMark Johnston VM_CNT_INC(v_pfree); 9055cd29d0fSMark Johnston vm_page_free(p); 9064074d642SAlan Cox } 907fee2a2faSMark Johnston } 9082fcd1ff6SKonstantin Belousov 9097bbdb843SRuslan Bukin /* 9107bbdb843SRuslan Bukin * If the object contained any pages, then reset it to an empty state. 9117bbdb843SRuslan Bukin * None of the object's fields, including "resident_page_count", were 9127bbdb843SRuslan Bukin * modified by the preceding loop. 9137bbdb843SRuslan Bukin */ 9147bbdb843SRuslan Bukin if (object->resident_page_count != 0) { 9157bbdb843SRuslan Bukin vm_radix_reclaim_allnodes(&object->rtree); 9167bbdb843SRuslan Bukin TAILQ_INIT(&object->memq); 9177bbdb843SRuslan Bukin object->resident_page_count = 0; 9187bbdb843SRuslan Bukin if (object->type == OBJT_VNODE) 9197bbdb843SRuslan Bukin vdrop(object->handle); 9207bbdb843SRuslan Bukin } 9217bbdb843SRuslan Bukin } 9227bbdb843SRuslan Bukin 9237bbdb843SRuslan Bukin /* 924df8bae1dSRodney W. Grimes * vm_object_terminate actually destroys the specified object, freeing 925df8bae1dSRodney W. Grimes * up all previously used resources. 926df8bae1dSRodney W. Grimes * 927df8bae1dSRodney W. Grimes * The object must be locked. 9281c7c3c6aSMatthew Dillon * This routine may block. 929df8bae1dSRodney W. Grimes */ 93095e5e988SJohn Dyson void 9311b40f8c0SMatthew Dillon vm_object_terminate(vm_object_t object) 932df8bae1dSRodney W. Grimes { 93398087a06SJeff Roberson 93489f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 935783a68aaSKonstantin Belousov KASSERT((object->flags & OBJ_DEAD) != 0, 936783a68aaSKonstantin Belousov ("terminating non-dead obj %p", object)); 93798087a06SJeff Roberson KASSERT((object->flags & OBJ_COLLAPSING) == 0, 93898087a06SJeff Roberson ("terminating collapsing obj %p", object)); 93998087a06SJeff Roberson KASSERT(object->backing_object == NULL, 94098087a06SJeff Roberson ("terminating shadow obj %p", object)); 941bef608bdSJohn Dyson 942cf27e0d1SJeff Roberson /* 943ffae7ea9SKonstantin Belousov * Wait for the pageout daemon and other current users to be 944ffae7ea9SKonstantin Belousov * done with the object. Note that new paging_in_progress 945ffae7ea9SKonstantin Belousov * users can come after this wait, but they must check 946ffae7ea9SKonstantin Belousov * OBJ_DEAD flag set (without unlocking the object), and avoid 947ffae7ea9SKonstantin Belousov * the object being terminated. 948cf27e0d1SJeff Roberson */ 949cf27e0d1SJeff Roberson vm_object_pip_wait(object, "objtrm"); 950cf27e0d1SJeff Roberson 951971dd342SAlfred Perlstein KASSERT(object->ref_count == 0, 952971dd342SAlfred Perlstein ("vm_object_terminate: object with references, ref_count=%d", 953971dd342SAlfred Perlstein object->ref_count)); 954996c772fSJohn Dyson 9557bbdb843SRuslan Bukin if ((object->flags & OBJ_PG_DTOR) == 0) 9567bbdb843SRuslan Bukin vm_object_terminate_pages(object); 957bef608bdSJohn Dyson 958f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 959f8a47341SAlan Cox if (__predict_false(!LIST_EMPTY(&object->rvq))) 960f8a47341SAlan Cox vm_reserv_break_all(object); 961f8a47341SAlan Cox #endif 9627bfda801SAlan Cox 9630cb2610eSMark Johnston KASSERT(object->cred == NULL || (object->flags & OBJ_SWAP) != 0, 964e735691bSJohn Baldwin ("%s: non-swap obj %p has cred", __func__, object)); 965e735691bSJohn Baldwin 9662d8acc0fSJohn Dyson /* 9679fcfb650SDavid Greenman * Let the pager know object is dead. 9689fcfb650SDavid Greenman */ 9699fcfb650SDavid Greenman vm_pager_deallocate(object); 97089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 9719fcfb650SDavid Greenman 9722ac78f0eSStephan Uphoff vm_object_destroy(object); 97347221757SJohn Dyson } 974df8bae1dSRodney W. Grimes 975edf93b25SAlan Cox /* 976edf93b25SAlan Cox * Make the page read-only so that we can clear the object flags. However, if 977edf93b25SAlan Cox * this is a nosync mmap then the object is likely to stay dirty so do not 978edf93b25SAlan Cox * mess with the page and do not clear the object flags. Returns TRUE if the 979edf93b25SAlan Cox * page should be flushed, and FALSE otherwise. 980edf93b25SAlan Cox */ 9813280870dSKonstantin Belousov static boolean_t 98267d0e293SJeff Roberson vm_object_page_remove_write(vm_page_t p, int flags, boolean_t *allclean) 9833280870dSKonstantin Belousov { 9843280870dSKonstantin Belousov 985fff5403fSJeff Roberson vm_page_assert_busied(p); 986fff5403fSJeff Roberson 9873280870dSKonstantin Belousov /* 9883280870dSKonstantin Belousov * If we have been asked to skip nosync pages and this is a 9893280870dSKonstantin Belousov * nosync page, skip it. Note that the object flags were not 9903280870dSKonstantin Belousov * cleared in this case so we do not have to set them. 9913280870dSKonstantin Belousov */ 9925cff1f4dSMark Johnston if ((flags & OBJPC_NOSYNC) != 0 && (p->a.flags & PGA_NOSYNC) != 0) { 99367d0e293SJeff Roberson *allclean = FALSE; 9943280870dSKonstantin Belousov return (FALSE); 9953280870dSKonstantin Belousov } else { 9963280870dSKonstantin Belousov pmap_remove_write(p); 9973280870dSKonstantin Belousov return (p->dirty != 0); 9983280870dSKonstantin Belousov } 9993280870dSKonstantin Belousov } 10003280870dSKonstantin Belousov 1001df8bae1dSRodney W. Grimes /* 1002df8bae1dSRodney W. Grimes * vm_object_page_clean 1003df8bae1dSRodney W. Grimes * 10044f79d873SMatthew Dillon * Clean all dirty pages in the specified range of object. Leaves page 10054f79d873SMatthew Dillon * on whatever queue it is currently on. If NOSYNC is set then do not 1006fff5403fSJeff Roberson * write out pages with PGA_NOSYNC set (originally comes from MAP_NOSYNC), 10074f79d873SMatthew Dillon * leaving the object dirty. 100826f9a767SRodney W. Grimes * 10098d34a3bfSKonstantin Belousov * For swap objects backing tmpfs regular files, do not flush anything, 10108d34a3bfSKonstantin Belousov * but remove write protection on the mapped pages to update mtime through 10118d34a3bfSKonstantin Belousov * mmaped writes. 10128d34a3bfSKonstantin Belousov * 101343b7990eSMatthew Dillon * When stuffing pages asynchronously, allow clustering. XXX we need a 101443b7990eSMatthew Dillon * synchronous clustering mode implementation. 101543b7990eSMatthew Dillon * 101626f9a767SRodney W. Grimes * Odd semantics: if start == end, we clean everything. 101726f9a767SRodney W. Grimes * 101826f9a767SRodney W. Grimes * The object must be locked. 1019126d6082SKonstantin Belousov * 1020126d6082SKonstantin Belousov * Returns FALSE if some page from the range was not written, as 1021126d6082SKonstantin Belousov * reported by the pager, and TRUE otherwise. 102226f9a767SRodney W. Grimes */ 1023126d6082SKonstantin Belousov boolean_t 102417f3095dSAlan Cox vm_object_page_clean(vm_object_t object, vm_ooffset_t start, vm_ooffset_t end, 1025e239bb97SKonstantin Belousov int flags) 1026f6b04d2bSDavid Greenman { 1027e239bb97SKonstantin Belousov vm_page_t np, p; 102817f3095dSAlan Cox vm_pindex_t pi, tend, tstart; 1029126d6082SKonstantin Belousov int curgeneration, n, pagerflags; 103067d0e293SJeff Roberson boolean_t eio, res, allclean; 1031f6b04d2bSDavid Greenman 103289f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 1033e5f299ffSKonstantin Belousov 10348d34a3bfSKonstantin Belousov if (!vm_object_mightbedirty(object) || object->resident_page_count == 0) 1035126d6082SKonstantin Belousov return (TRUE); 1036f6b04d2bSDavid Greenman 1037e239bb97SKonstantin Belousov pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) != 0 ? 1038e239bb97SKonstantin Belousov VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK; 1039e239bb97SKonstantin Belousov pagerflags |= (flags & OBJPC_INVAL) != 0 ? VM_PAGER_PUT_INVAL : 0; 1040e239bb97SKonstantin Belousov 104117f3095dSAlan Cox tstart = OFF_TO_IDX(start); 104217f3095dSAlan Cox tend = (end == 0) ? object->size : OFF_TO_IDX(end + PAGE_MASK); 104367d0e293SJeff Roberson allclean = tstart == 0 && tend >= object->size; 1044126d6082SKonstantin Belousov res = TRUE; 1045f6b04d2bSDavid Greenman 1046bd7e5f99SJohn Dyson rescan: 10472d8acc0fSJohn Dyson curgeneration = object->generation; 10482d8acc0fSJohn Dyson 104917f3095dSAlan Cox for (p = vm_page_find_least(object, tstart); p != NULL; p = np) { 1050bd7e5f99SJohn Dyson pi = p->pindex; 1051e239bb97SKonstantin Belousov if (pi >= tend) 1052e239bb97SKonstantin Belousov break; 1053e239bb97SKonstantin Belousov np = TAILQ_NEXT(p, listq); 10540012f373SJeff Roberson if (vm_page_none_valid(p)) 1055aef922f5SJohn Dyson continue; 105663e97555SJeff Roberson if (vm_page_busy_acquire(p, VM_ALLOC_WAITFAIL) == 0) { 105767d0e293SJeff Roberson if (object->generation != curgeneration && 105867d0e293SJeff Roberson (flags & OBJPC_SYNC) != 0) 1059e239bb97SKonstantin Belousov goto rescan; 1060780636b7SKonstantin Belousov np = vm_page_find_least(object, pi); 1061780636b7SKonstantin Belousov continue; 1062f6b04d2bSDavid Greenman } 106367d0e293SJeff Roberson if (!vm_object_page_remove_write(p, flags, &allclean)) { 106463e97555SJeff Roberson vm_page_xunbusy(p); 1065bd7e5f99SJohn Dyson continue; 106663e97555SJeff Roberson } 10678d34a3bfSKonstantin Belousov if (object->type == OBJT_VNODE) { 10683280870dSKonstantin Belousov n = vm_object_page_collect_flush(object, p, pagerflags, 106967d0e293SJeff Roberson flags, &allclean, &eio); 1070126d6082SKonstantin Belousov if (eio) { 1071126d6082SKonstantin Belousov res = FALSE; 107267d0e293SJeff Roberson allclean = FALSE; 1073126d6082SKonstantin Belousov } 107467d0e293SJeff Roberson if (object->generation != curgeneration && 107567d0e293SJeff Roberson (flags & OBJPC_SYNC) != 0) 1076b9b7a4beSMatthew Dillon goto rescan; 1077031ec8c1SKonstantin Belousov 1078031ec8c1SKonstantin Belousov /* 1079031ec8c1SKonstantin Belousov * If the VOP_PUTPAGES() did a truncated write, so 1080031ec8c1SKonstantin Belousov * that even the first page of the run is not fully 1081031ec8c1SKonstantin Belousov * written, vm_pageout_flush() returns 0 as the run 1082031ec8c1SKonstantin Belousov * length. Since the condition that caused truncated 1083031ec8c1SKonstantin Belousov * write may be permanent, e.g. exhausted free space, 1084031ec8c1SKonstantin Belousov * accepting n == 0 would cause an infinite loop. 1085031ec8c1SKonstantin Belousov * 1086031ec8c1SKonstantin Belousov * Forwarding the iterator leaves the unwritten page 1087031ec8c1SKonstantin Belousov * behind, but there is not much we can do there if 1088031ec8c1SKonstantin Belousov * filesystem refuses to write it. 1089031ec8c1SKonstantin Belousov */ 1090126d6082SKonstantin Belousov if (n == 0) { 1091031ec8c1SKonstantin Belousov n = 1; 109267d0e293SJeff Roberson allclean = FALSE; 1093126d6082SKonstantin Belousov } 10948d34a3bfSKonstantin Belousov } else { 10958d34a3bfSKonstantin Belousov n = 1; 10968d34a3bfSKonstantin Belousov vm_page_xunbusy(p); 10978d34a3bfSKonstantin Belousov } 1098e239bb97SKonstantin Belousov np = vm_page_find_least(object, pi + n); 1099b9b7a4beSMatthew Dillon } 1100b9b7a4beSMatthew Dillon #if 0 1101e239bb97SKonstantin Belousov VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC) ? MNT_WAIT : 0); 1102b9b7a4beSMatthew Dillon #endif 1103b9b7a4beSMatthew Dillon 11048d34a3bfSKonstantin Belousov /* 11058d34a3bfSKonstantin Belousov * Leave updating cleangeneration for tmpfs objects to tmpfs 11068d34a3bfSKonstantin Belousov * scan. It needs to update mtime, which happens for other 11078d34a3bfSKonstantin Belousov * filesystems during page writeouts. 11088d34a3bfSKonstantin Belousov */ 11098d34a3bfSKonstantin Belousov if (allclean && object->type == OBJT_VNODE) 111067d0e293SJeff Roberson object->cleangeneration = curgeneration; 1111126d6082SKonstantin Belousov return (res); 1112b9b7a4beSMatthew Dillon } 1113b9b7a4beSMatthew Dillon 1114b9b7a4beSMatthew Dillon static int 11153280870dSKonstantin Belousov vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int pagerflags, 111667d0e293SJeff Roberson int flags, boolean_t *allclean, boolean_t *eio) 1117b9b7a4beSMatthew Dillon { 11183157c503SKonstantin Belousov vm_page_t ma[vm_pageout_page_count], p_first, tp; 11193157c503SKonstantin Belousov int count, i, mreq, runlen; 1120b9b7a4beSMatthew Dillon 11217bec141bSKip Macy vm_page_lock_assert(p, MA_NOTOWNED); 112263e97555SJeff Roberson vm_page_assert_xbusied(p); 112389f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 11243157c503SKonstantin Belousov 11253157c503SKonstantin Belousov count = 1; 11263157c503SKonstantin Belousov mreq = 0; 11273157c503SKonstantin Belousov 11283157c503SKonstantin Belousov for (tp = p; count < vm_pageout_page_count; count++) { 11293157c503SKonstantin Belousov tp = vm_page_next(tp); 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; 1135bd7e5f99SJohn Dyson } 113663e97555SJeff Roberson } 1137aef922f5SJohn Dyson 11383157c503SKonstantin Belousov for (p_first = p; count < vm_pageout_page_count; count++) { 11393157c503SKonstantin Belousov tp = vm_page_prev(p_first); 114063e97555SJeff Roberson if (tp == NULL || vm_page_tryxbusy(tp) == 0) 1141bd7e5f99SJohn Dyson break; 114267d0e293SJeff Roberson if (!vm_object_page_remove_write(tp, flags, allclean)) { 114363e97555SJeff Roberson vm_page_xunbusy(tp); 1144bd7e5f99SJohn Dyson break; 114563e97555SJeff Roberson } 11463157c503SKonstantin Belousov p_first = tp; 11473157c503SKonstantin Belousov mreq++; 1148bd7e5f99SJohn Dyson } 1149bd7e5f99SJohn Dyson 11503157c503SKonstantin Belousov for (tp = p_first, i = 0; i < count; tp = TAILQ_NEXT(tp, listq), i++) 11513157c503SKonstantin Belousov ma[i] = tp; 1152cf2819ccSJohn Dyson 1153126d6082SKonstantin Belousov vm_pageout_flush(ma, count, pagerflags, mreq, &runlen, eio); 11541e8a675cSKonstantin Belousov return (runlen); 115526f9a767SRodney W. Grimes } 1156df8bae1dSRodney W. Grimes 11571efb74fbSJohn Dyson /* 1158950f8459SAlan Cox * Note that there is absolutely no sense in writing out 1159950f8459SAlan Cox * anonymous objects, so we track down the vnode object 1160950f8459SAlan Cox * to write out. 1161950f8459SAlan Cox * We invalidate (remove) all pages from the address space 1162950f8459SAlan Cox * for semantic correctness. 1163950f8459SAlan Cox * 11646bbee8e2SAlan Cox * If the backing object is a device object with unmanaged pages, then any 11656bbee8e2SAlan Cox * mappings to the specified range of pages must be removed before this 11666bbee8e2SAlan Cox * function is called. 11676bbee8e2SAlan Cox * 1168950f8459SAlan Cox * Note: certain anonymous maps, such as MAP_NOSYNC maps, 1169950f8459SAlan Cox * may start out with a NULL object. 1170950f8459SAlan Cox */ 1171126d6082SKonstantin Belousov boolean_t 1172950f8459SAlan Cox vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size, 1173950f8459SAlan Cox boolean_t syncio, boolean_t invalidate) 1174950f8459SAlan Cox { 1175950f8459SAlan Cox vm_object_t backing_object; 1176950f8459SAlan Cox struct vnode *vp; 11773b582b4eSTor Egge struct mount *mp; 1178126d6082SKonstantin Belousov int error, flags, fsync_after; 1179126d6082SKonstantin Belousov boolean_t res; 1180950f8459SAlan Cox 1181950f8459SAlan Cox if (object == NULL) 1182126d6082SKonstantin Belousov return (TRUE); 1183126d6082SKonstantin Belousov res = TRUE; 1184126d6082SKonstantin Belousov error = 0; 118589f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1186950f8459SAlan Cox while ((backing_object = object->backing_object) != NULL) { 118789f6b863SAttilio Rao VM_OBJECT_WLOCK(backing_object); 118856e0670fSAlan Cox offset += object->backing_object_offset; 118989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1190950f8459SAlan Cox object = backing_object; 1191950f8459SAlan Cox if (object->size < OFF_TO_IDX(offset + size)) 1192950f8459SAlan Cox size = IDX_TO_OFF(object->size) - offset; 1193950f8459SAlan Cox } 1194950f8459SAlan Cox /* 1195950f8459SAlan Cox * Flush pages if writing is allowed, invalidate them 1196950f8459SAlan Cox * if invalidation requested. Pages undergoing I/O 1197950f8459SAlan Cox * will be ignored by vm_object_page_remove(). 1198950f8459SAlan Cox * 1199950f8459SAlan Cox * We cannot lock the vnode and then wait for paging 1200950f8459SAlan Cox * to complete without deadlocking against vm_fault. 1201950f8459SAlan Cox * Instead we simply call vm_object_page_remove() and 1202950f8459SAlan Cox * allow it to block internally on a page-by-page 1203950f8459SAlan Cox * basis when it encounters pages undergoing async 1204950f8459SAlan Cox * I/O. 1205950f8459SAlan Cox */ 1206950f8459SAlan Cox if (object->type == OBJT_VNODE && 120767d0e293SJeff Roberson vm_object_mightbedirty(object) != 0 && 12085bf94937SKonstantin Belousov ((vp = object->handle)->v_vflag & VV_NOSYNC) == 0) { 120989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 12103b582b4eSTor Egge (void)vn_start_write(vp, &mp, V_WAIT); 1211cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 121275ff604aSKonstantin Belousov if (syncio && !invalidate && offset == 0 && 1213d1780e8dSKonstantin Belousov atop(size) == object->size) { 121475ff604aSKonstantin Belousov /* 121575ff604aSKonstantin Belousov * If syncing the whole mapping of the file, 121675ff604aSKonstantin Belousov * it is faster to schedule all the writes in 121775ff604aSKonstantin Belousov * async mode, also allowing the clustering, 121875ff604aSKonstantin Belousov * and then wait for i/o to complete. 121975ff604aSKonstantin Belousov */ 122075ff604aSKonstantin Belousov flags = 0; 122175ff604aSKonstantin Belousov fsync_after = TRUE; 122275ff604aSKonstantin Belousov } else { 1223950f8459SAlan Cox flags = (syncio || invalidate) ? OBJPC_SYNC : 0; 122475ff604aSKonstantin Belousov flags |= invalidate ? (OBJPC_SYNC | OBJPC_INVAL) : 0; 122575ff604aSKonstantin Belousov fsync_after = FALSE; 122675ff604aSKonstantin Belousov } 122789f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1228126d6082SKonstantin Belousov res = vm_object_page_clean(object, offset, offset + size, 1229126d6082SKonstantin Belousov flags); 123089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 12316189672eSKonstantin Belousov if (fsync_after) { 12326189672eSKonstantin Belousov for (;;) { 1233126d6082SKonstantin Belousov error = VOP_FSYNC(vp, MNT_WAIT, curthread); 12346189672eSKonstantin Belousov if (error != ERELOOKUP) 12356189672eSKonstantin Belousov break; 12366189672eSKonstantin Belousov 12376189672eSKonstantin Belousov /* 12386189672eSKonstantin Belousov * Allow SU/bufdaemon to handle more 12396189672eSKonstantin Belousov * dependencies in the meantime. 12406189672eSKonstantin Belousov */ 12416189672eSKonstantin Belousov VOP_UNLOCK(vp); 12426189672eSKonstantin Belousov vn_finished_write(mp); 12436189672eSKonstantin Belousov 12446189672eSKonstantin Belousov (void)vn_start_write(vp, &mp, V_WAIT); 12456189672eSKonstantin Belousov vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 12466189672eSKonstantin Belousov } 12476189672eSKonstantin Belousov } 1248b249ce48SMateusz Guzik VOP_UNLOCK(vp); 12493b582b4eSTor Egge vn_finished_write(mp); 1250126d6082SKonstantin Belousov if (error != 0) 1251126d6082SKonstantin Belousov res = FALSE; 125289f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1253950f8459SAlan Cox } 1254950f8459SAlan Cox if ((object->type == OBJT_VNODE || 1255950f8459SAlan Cox object->type == OBJT_DEVICE) && invalidate) { 12566bbee8e2SAlan Cox if (object->type == OBJT_DEVICE) 12576bbee8e2SAlan Cox /* 12586bbee8e2SAlan Cox * The option OBJPR_NOTMAPPED must be passed here 12596bbee8e2SAlan Cox * because vm_object_page_remove() cannot remove 12606bbee8e2SAlan Cox * unmanaged mappings. 12616bbee8e2SAlan Cox */ 12626bbee8e2SAlan Cox flags = OBJPR_NOTMAPPED; 12636bbee8e2SAlan Cox else if (old_msync) 12646195b24aSKonstantin Belousov flags = 0; 12656bbee8e2SAlan Cox else 12666195b24aSKonstantin Belousov flags = OBJPR_CLEANONLY; 12676bbee8e2SAlan Cox vm_object_page_remove(object, OFF_TO_IDX(offset), 12686bbee8e2SAlan Cox OFF_TO_IDX(offset + size + PAGE_MASK), flags); 1269950f8459SAlan Cox } 127089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1271126d6082SKonstantin Belousov return (res); 1272950f8459SAlan Cox } 1273950f8459SAlan Cox 1274950f8459SAlan Cox /* 1275aa3650eaSMark Johnston * Determine whether the given advice can be applied to the object. Advice is 1276aa3650eaSMark Johnston * not applied to unmanaged pages since they never belong to page queues, and 1277aa3650eaSMark Johnston * since MADV_FREE is destructive, it can apply only to anonymous pages that 1278aa3650eaSMark Johnston * have been mapped at most once. 1279aa3650eaSMark Johnston */ 1280aa3650eaSMark Johnston static bool 1281aa3650eaSMark Johnston vm_object_advice_applies(vm_object_t object, int advice) 1282aa3650eaSMark Johnston { 1283aa3650eaSMark Johnston 1284aa3650eaSMark Johnston if ((object->flags & OBJ_UNMANAGED) != 0) 1285aa3650eaSMark Johnston return (false); 1286aa3650eaSMark Johnston if (advice != MADV_FREE) 1287aa3650eaSMark Johnston return (true); 128863967687SJeff Roberson return ((object->flags & (OBJ_ONEMAPPING | OBJ_ANON)) == 128963967687SJeff Roberson (OBJ_ONEMAPPING | OBJ_ANON)); 1290aa3650eaSMark Johnston } 1291aa3650eaSMark Johnston 1292aa3650eaSMark Johnston static void 1293aa3650eaSMark Johnston vm_object_madvise_freespace(vm_object_t object, int advice, vm_pindex_t pindex, 1294aa3650eaSMark Johnston vm_size_t size) 1295aa3650eaSMark Johnston { 1296aa3650eaSMark Johnston 12971390a5cbSKonstantin Belousov if (advice == MADV_FREE) 12981390a5cbSKonstantin Belousov vm_pager_freespace(object, pindex, size); 1299aa3650eaSMark Johnston } 1300aa3650eaSMark Johnston 1301aa3650eaSMark Johnston /* 1302867a482dSJohn Dyson * vm_object_madvise: 1303867a482dSJohn Dyson * 1304867a482dSJohn Dyson * Implements the madvise function at the object/page level. 13051c7c3c6aSMatthew Dillon * 1306193b9358SAlan Cox * MADV_WILLNEED (any object) 1307193b9358SAlan Cox * 1308193b9358SAlan Cox * Activate the specified pages if they are resident. 1309193b9358SAlan Cox * 1310193b9358SAlan Cox * MADV_DONTNEED (any object) 1311193b9358SAlan Cox * 1312193b9358SAlan Cox * Deactivate the specified pages if they are resident. 1313193b9358SAlan Cox * 13140cb2610eSMark Johnston * MADV_FREE (OBJT_SWAP objects, OBJ_ONEMAPPING only) 1315193b9358SAlan Cox * 1316193b9358SAlan Cox * Deactivate and clean the specified pages if they are 1317193b9358SAlan Cox * resident. This permits the process to reuse the pages 1318193b9358SAlan Cox * without faulting or the kernel to reclaim the pages 1319193b9358SAlan Cox * without I/O. 1320867a482dSJohn Dyson */ 1321867a482dSJohn Dyson void 132292a59946SJohn Baldwin vm_object_madvise(vm_object_t object, vm_pindex_t pindex, vm_pindex_t end, 1323c2655a40SMark Johnston int advice) 1324867a482dSJohn Dyson { 132592a59946SJohn Baldwin vm_pindex_t tpindex; 132634567de7SAlan Cox vm_object_t backing_object, tobject; 1327aa3650eaSMark Johnston vm_page_t m, tm; 1328867a482dSJohn Dyson 1329867a482dSJohn Dyson if (object == NULL) 1330867a482dSJohn Dyson return; 1331c2655a40SMark Johnston 13326e20a165SJohn Dyson relookup: 1333aa3650eaSMark Johnston VM_OBJECT_WLOCK(object); 1334aa3650eaSMark Johnston if (!vm_object_advice_applies(object, advice)) { 1335aa3650eaSMark Johnston VM_OBJECT_WUNLOCK(object); 1336aa3650eaSMark Johnston return; 13376e20a165SJohn Dyson } 1338aa3650eaSMark Johnston for (m = vm_page_find_least(object, pindex); pindex < end; pindex++) { 1339aa3650eaSMark Johnston tobject = object; 1340c2655a40SMark Johnston 13411ce137beSMatthew Dillon /* 1342aa3650eaSMark Johnston * If the next page isn't resident in the top-level object, we 1343aa3650eaSMark Johnston * need to search the shadow chain. When applying MADV_FREE, we 1344aa3650eaSMark Johnston * take care to release any swap space used to store 1345aa3650eaSMark Johnston * non-resident pages. 1346aa3650eaSMark Johnston */ 1347aa3650eaSMark Johnston if (m == NULL || pindex < m->pindex) { 1348aa3650eaSMark Johnston /* 1349aa3650eaSMark Johnston * Optimize a common case: if the top-level object has 1350aa3650eaSMark Johnston * no backing object, we can skip over the non-resident 1351aa3650eaSMark Johnston * range in constant time. 13521ce137beSMatthew Dillon */ 1353c2655a40SMark Johnston if (object->backing_object == NULL) { 1354c2655a40SMark Johnston tpindex = (m != NULL && m->pindex < end) ? 1355c2655a40SMark Johnston m->pindex : end; 1356aa3650eaSMark Johnston vm_object_madvise_freespace(object, advice, 1357aa3650eaSMark Johnston pindex, tpindex - pindex); 1358c2655a40SMark Johnston if ((pindex = tpindex) == end) 1359c2655a40SMark Johnston break; 1360aa3650eaSMark Johnston goto next_page; 1361aa3650eaSMark Johnston } 1362aa3650eaSMark Johnston 1363aa3650eaSMark Johnston tpindex = pindex; 1364aa3650eaSMark Johnston do { 1365aa3650eaSMark Johnston vm_object_madvise_freespace(tobject, advice, 1366aa3650eaSMark Johnston tpindex, 1); 13671ce137beSMatthew Dillon /* 1368aa3650eaSMark Johnston * Prepare to search the next object in the 1369aa3650eaSMark Johnston * chain. 13701ce137beSMatthew Dillon */ 137134567de7SAlan Cox backing_object = tobject->backing_object; 137234567de7SAlan Cox if (backing_object == NULL) 1373aa3650eaSMark Johnston goto next_pindex; 137489f6b863SAttilio Rao VM_OBJECT_WLOCK(backing_object); 1375aa3650eaSMark Johnston tpindex += 1376aa3650eaSMark Johnston OFF_TO_IDX(tobject->backing_object_offset); 13779b98b796SAlan Cox if (tobject != object) 137889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(tobject); 137934567de7SAlan Cox tobject = backing_object; 1380aa3650eaSMark Johnston if (!vm_object_advice_applies(tobject, advice)) 1381aa3650eaSMark Johnston goto next_pindex; 1382aa3650eaSMark Johnston } while ((tm = vm_page_lookup(tobject, tpindex)) == 1383aa3650eaSMark Johnston NULL); 1384aa3650eaSMark Johnston } else { 1385aa3650eaSMark Johnston next_page: 1386aa3650eaSMark Johnston tm = m; 1387aa3650eaSMark Johnston m = TAILQ_NEXT(m, listq); 1388c2655a40SMark Johnston } 1389c2655a40SMark Johnston 1390867a482dSJohn Dyson /* 13910012f373SJeff Roberson * If the page is not in a normal state, skip it. The page 13920012f373SJeff Roberson * can not be invalidated while the object lock is held. 1393867a482dSJohn Dyson */ 13940012f373SJeff Roberson if (!vm_page_all_valid(tm) || vm_page_wired(tm)) 1395aa3650eaSMark Johnston goto next_pindex; 1396aa3650eaSMark Johnston KASSERT((tm->flags & PG_FICTITIOUS) == 0, 1397aa3650eaSMark Johnston ("vm_object_madvise: page %p is fictitious", tm)); 1398aa3650eaSMark Johnston KASSERT((tm->oflags & VPO_UNMANAGED) == 0, 1399aa3650eaSMark Johnston ("vm_object_madvise: page %p is not managed", tm)); 140063e97555SJeff Roberson if (vm_page_tryxbusy(tm) == 0) { 1401aa3650eaSMark Johnston if (object != tobject) 1402aa3650eaSMark Johnston VM_OBJECT_WUNLOCK(object); 1403c2655a40SMark Johnston if (advice == MADV_WILLNEED) { 1404b11b56b5SAlan Cox /* 1405b11b56b5SAlan Cox * Reference the page before unlocking and 1406b11b56b5SAlan Cox * sleeping so that the page daemon is less 1407b11b56b5SAlan Cox * likely to reclaim it. 1408b11b56b5SAlan Cox */ 1409aa3650eaSMark Johnston vm_page_aflag_set(tm, PGA_REFERENCED); 1410567e51e1SAlan Cox } 141187b64663SMark Johnston if (!vm_page_busy_sleep(tm, "madvpo", 0)) 141287b64663SMark Johnston VM_OBJECT_WUNLOCK(tobject); 14136e20a165SJohn Dyson goto relookup; 141434567de7SAlan Cox } 1415aa3650eaSMark Johnston vm_page_advise(tm, advice); 141663e97555SJeff Roberson vm_page_xunbusy(tm); 1417aa3650eaSMark Johnston vm_object_madvise_freespace(tobject, advice, tm->pindex, 1); 1418aa3650eaSMark Johnston next_pindex: 14199b98b796SAlan Cox if (tobject != object) 142089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(tobject); 1421867a482dSJohn Dyson } 142289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1423867a482dSJohn Dyson } 1424867a482dSJohn Dyson 1425867a482dSJohn Dyson /* 1426df8bae1dSRodney W. Grimes * vm_object_shadow: 1427df8bae1dSRodney W. Grimes * 1428df8bae1dSRodney W. Grimes * Create a new object which is backed by the 1429df8bae1dSRodney W. Grimes * specified existing object range. The source 1430df8bae1dSRodney W. Grimes * object reference is deallocated. 1431df8bae1dSRodney W. Grimes * 1432df8bae1dSRodney W. Grimes * The new object and offset into that object 1433df8bae1dSRodney W. Grimes * are returned in the source parameters. 1434df8bae1dSRodney W. Grimes */ 143526f9a767SRodney W. Grimes void 143667388836SKonstantin Belousov vm_object_shadow(vm_object_t *object, vm_ooffset_t *offset, vm_size_t length, 143767388836SKonstantin Belousov struct ucred *cred, bool shared) 1438df8bae1dSRodney W. Grimes { 1439d031cff1SMatthew Dillon vm_object_t source; 1440d031cff1SMatthew Dillon vm_object_t result; 1441df8bae1dSRodney W. Grimes 1442df8bae1dSRodney W. Grimes source = *object; 1443df8bae1dSRodney W. Grimes 1444df8bae1dSRodney W. Grimes /* 14459a2f6362SAlan Cox * Don't create the new object if the old object isn't shared. 144663967687SJeff Roberson * 144763967687SJeff Roberson * If we hold the only reference we can guarantee that it won't 144863967687SJeff Roberson * increase while we have the map locked. Otherwise the race is 144963967687SJeff Roberson * harmless and we will end up with an extra shadow object that 145063967687SJeff Roberson * will be collapsed later. 14519a2f6362SAlan Cox */ 145263967687SJeff Roberson if (source != NULL && source->ref_count == 1 && 145332362449SKonstantin Belousov (source->flags & OBJ_ANON) != 0) 14549a2f6362SAlan Cox return; 14559a2f6362SAlan Cox 14569a2f6362SAlan Cox /* 1457570a2f4aSAlan Cox * Allocate a new object with the given length. 1458df8bae1dSRodney W. Grimes */ 145967388836SKonstantin Belousov result = vm_object_allocate_anon(atop(length), source, cred, length); 1460df8bae1dSRodney W. Grimes 1461df8bae1dSRodney W. Grimes /* 146251b867e5SJeff Roberson * Store the offset into the source object, and fix up the offset into 146351b867e5SJeff Roberson * the new object. 146451b867e5SJeff Roberson */ 146551b867e5SJeff Roberson result->backing_object_offset = *offset; 146651b867e5SJeff Roberson 146767388836SKonstantin Belousov if (shared || source != NULL) { 146867388836SKonstantin Belousov VM_OBJECT_WLOCK(result); 146967388836SKonstantin Belousov 147051b867e5SJeff Roberson /* 147167388836SKonstantin Belousov * The new object shadows the source object, adding a 147267388836SKonstantin Belousov * reference to it. Our caller changes his reference 147367388836SKonstantin Belousov * to point to the new object, removing a reference to 147467388836SKonstantin Belousov * the source object. Net result: no change of 147567388836SKonstantin Belousov * reference count, unless the caller needs to add one 147667388836SKonstantin Belousov * more reference due to forking a shared map entry. 147767388836SKonstantin Belousov */ 147867388836SKonstantin Belousov if (shared) { 147967388836SKonstantin Belousov vm_object_reference_locked(result); 148067388836SKonstantin Belousov vm_object_clear_flag(result, OBJ_ONEMAPPING); 148167388836SKonstantin Belousov } 148267388836SKonstantin Belousov 148367388836SKonstantin Belousov /* 148467388836SKonstantin Belousov * Try to optimize the result object's page color when 148567388836SKonstantin Belousov * shadowing in order to maintain page coloring 148667388836SKonstantin Belousov * consistency in the combined shadowed object. 1487df8bae1dSRodney W. Grimes */ 1488570a2f4aSAlan Cox if (source != NULL) { 148951b867e5SJeff Roberson vm_object_backing_insert(result, source); 14903f289c3fSJeff Roberson result->domain = source->domain; 1491f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 1492630f633fSMark Johnston vm_object_set_flag(result, 1493630f633fSMark Johnston (source->flags & OBJ_COLORED)); 149467388836SKonstantin Belousov result->pg_color = (source->pg_color + 149567388836SKonstantin Belousov OFF_TO_IDX(*offset)) & ((1 << (VM_NFREEORDER - 149667388836SKonstantin Belousov 1)) - 1); 1497f8a47341SAlan Cox #endif 149867388836SKonstantin Belousov } 149951b867e5SJeff Roberson VM_OBJECT_WUNLOCK(result); 1500de5f6a77SJohn Dyson } 1501df8bae1dSRodney W. Grimes 1502df8bae1dSRodney W. Grimes /* 1503df8bae1dSRodney W. Grimes * Return the new things 1504df8bae1dSRodney W. Grimes */ 1505df8bae1dSRodney W. Grimes *offset = 0; 1506df8bae1dSRodney W. Grimes *object = result; 1507df8bae1dSRodney W. Grimes } 1508df8bae1dSRodney W. Grimes 1509c5aaa06dSAlan Cox /* 1510c5aaa06dSAlan Cox * vm_object_split: 1511c5aaa06dSAlan Cox * 1512c5aaa06dSAlan Cox * Split the pages in a map entry into a new object. This affords 1513c5aaa06dSAlan Cox * easier removal of unused pages, and keeps object inheritance from 1514c5aaa06dSAlan Cox * being a negative impact on memory usage. 1515c5aaa06dSAlan Cox */ 1516c5aaa06dSAlan Cox void 1517c5aaa06dSAlan Cox vm_object_split(vm_map_entry_t entry) 1518c5aaa06dSAlan Cox { 1519fff19e0eSMark Johnston vm_page_t m, m_next; 152098087a06SJeff Roberson vm_object_t orig_object, new_object, backing_object; 152173000556SAlan Cox vm_pindex_t idx, offidxstart; 152273000556SAlan Cox vm_size_t size; 1523c5aaa06dSAlan Cox 1524c5aaa06dSAlan Cox orig_object = entry->object.vm_object; 152598087a06SJeff Roberson KASSERT((orig_object->flags & OBJ_ONEMAPPING) != 0, 152698087a06SJeff Roberson ("vm_object_split: Splitting object with multiple mappings.")); 152763967687SJeff Roberson if ((orig_object->flags & OBJ_ANON) == 0) 1528c5aaa06dSAlan Cox return; 1529c5aaa06dSAlan Cox if (orig_object->ref_count <= 1) 1530c5aaa06dSAlan Cox return; 153189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(orig_object); 1532c5aaa06dSAlan Cox 15334da9f125SAlan Cox offidxstart = OFF_TO_IDX(entry->offset); 153495442adfSAlan Cox size = atop(entry->end - entry->start); 1535c5aaa06dSAlan Cox 153667388836SKonstantin Belousov new_object = vm_object_allocate_anon(size, orig_object, 153767388836SKonstantin Belousov orig_object->cred, ptoa(size)); 1538c5aaa06dSAlan Cox 1539c5474b8fSAlan Cox /* 154098087a06SJeff Roberson * We must wait for the orig_object to complete any in-progress 154198087a06SJeff Roberson * collapse so that the swap blocks are stable below. The 154298087a06SJeff Roberson * additional reference on backing_object by new object will 154398087a06SJeff Roberson * prevent further collapse operations until split completes. 154498087a06SJeff Roberson */ 154598087a06SJeff Roberson VM_OBJECT_WLOCK(orig_object); 154698087a06SJeff Roberson vm_object_collapse_wait(orig_object); 154798087a06SJeff Roberson 154898087a06SJeff Roberson /* 1549c5474b8fSAlan Cox * At this point, the new object is still private, so the order in 1550c5474b8fSAlan Cox * which the original and new objects are locked does not matter. 1551c5474b8fSAlan Cox */ 155289f6b863SAttilio Rao VM_OBJECT_WLOCK(new_object); 15533f289c3fSJeff Roberson new_object->domain = orig_object->domain; 155498087a06SJeff Roberson backing_object = orig_object->backing_object; 155598087a06SJeff Roberson if (backing_object != NULL) { 155698087a06SJeff Roberson vm_object_backing_insert_ref(new_object, backing_object); 1557c5aaa06dSAlan Cox new_object->backing_object_offset = 15584da9f125SAlan Cox orig_object->backing_object_offset + entry->offset; 1559c5aaa06dSAlan Cox } 1560ef694c1aSEdward Tomasz Napierala if (orig_object->cred != NULL) { 1561ef694c1aSEdward Tomasz Napierala crhold(orig_object->cred); 15623364c323SKonstantin Belousov KASSERT(orig_object->charge >= ptoa(size), 15633364c323SKonstantin Belousov ("orig_object->charge < 0")); 15643364c323SKonstantin Belousov orig_object->charge -= ptoa(size); 15653364c323SKonstantin Belousov } 156698087a06SJeff Roberson 156798087a06SJeff Roberson /* 156898087a06SJeff Roberson * Mark the split operation so that swap_pager_getpages() knows 156998087a06SJeff Roberson * that the object is in transition. 157098087a06SJeff Roberson */ 157198087a06SJeff Roberson vm_object_set_flag(orig_object, OBJ_SPLIT); 1572aec9e7d8SMark Johnston #ifdef INVARIANTS 1573aec9e7d8SMark Johnston idx = 0; 1574aec9e7d8SMark Johnston #endif 1575c5aaa06dSAlan Cox retry: 1576b382c10aSKonstantin Belousov m = vm_page_find_least(orig_object, offidxstart); 1577aec9e7d8SMark Johnston KASSERT(m == NULL || idx <= m->pindex - offidxstart, 1578aec9e7d8SMark Johnston ("%s: object %p was repopulated", __func__, orig_object)); 157973000556SAlan Cox for (; m != NULL && (idx = m->pindex - offidxstart) < size; 158073000556SAlan Cox m = m_next) { 158173000556SAlan Cox m_next = TAILQ_NEXT(m, listq); 1582c5aaa06dSAlan Cox 1583c5aaa06dSAlan Cox /* 1584c5aaa06dSAlan Cox * We must wait for pending I/O to complete before we can 1585c5aaa06dSAlan Cox * rename the page. 1586c5aaa06dSAlan Cox * 1587c5aaa06dSAlan Cox * We do not have to VM_PROT_NONE the page as mappings should 1588c5aaa06dSAlan Cox * not be changed by this operation. 1589c5aaa06dSAlan Cox */ 159063e97555SJeff Roberson if (vm_page_tryxbusy(m) == 0) { 159189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(new_object); 159287b64663SMark Johnston if (vm_page_busy_sleep(m, "spltwt", 0)) 159387b64663SMark Johnston VM_OBJECT_WLOCK(orig_object); 159489f6b863SAttilio Rao VM_OBJECT_WLOCK(new_object); 1595c5aaa06dSAlan Cox goto retry; 1596de33beddSAlan Cox } 1597e946b949SAttilio Rao 15984bf95d00SJeff Roberson /* 15994bf95d00SJeff Roberson * The page was left invalid. Likely placed there by 16004bf95d00SJeff Roberson * an incomplete fault. Just remove and ignore. 16014bf95d00SJeff Roberson */ 16024bf95d00SJeff Roberson if (vm_page_none_valid(m)) { 16034bf95d00SJeff Roberson if (vm_page_remove(m)) 16044bf95d00SJeff Roberson vm_page_free(m); 16054bf95d00SJeff Roberson continue; 16064bf95d00SJeff Roberson } 16074bf95d00SJeff Roberson 16083453bca8SAlan Cox /* vm_page_rename() will dirty the page. */ 1609e946b949SAttilio Rao if (vm_page_rename(m, new_object, idx)) { 161063e97555SJeff Roberson vm_page_xunbusy(m); 1611e946b949SAttilio Rao VM_OBJECT_WUNLOCK(new_object); 1612e946b949SAttilio Rao VM_OBJECT_WUNLOCK(orig_object); 16138d6fbbb8SJeff Roberson vm_radix_wait(); 1614e946b949SAttilio Rao VM_OBJECT_WLOCK(orig_object); 1615e946b949SAttilio Rao VM_OBJECT_WLOCK(new_object); 1616e946b949SAttilio Rao goto retry; 1617e946b949SAttilio Rao } 161863e97555SJeff Roberson 1619b5f359b7SAlan Cox #if VM_NRESERVLEVEL > 0 1620b5f359b7SAlan Cox /* 1621b5f359b7SAlan Cox * If some of the reservation's allocated pages remain with 1622b5f359b7SAlan Cox * the original object, then transferring the reservation to 1623b5f359b7SAlan Cox * the new object is neither particularly beneficial nor 1624b5f359b7SAlan Cox * particularly harmful as compared to leaving the reservation 1625b5f359b7SAlan Cox * with the original object. If, however, all of the 1626b5f359b7SAlan Cox * reservation's allocated pages are transferred to the new 1627b5f359b7SAlan Cox * object, then transferring the reservation is typically 1628b5f359b7SAlan Cox * beneficial. Determining which of these two cases applies 1629b5f359b7SAlan Cox * would be more costly than unconditionally renaming the 1630b5f359b7SAlan Cox * reservation. 1631b5f359b7SAlan Cox */ 1632b5f359b7SAlan Cox vm_reserv_rename(m, new_object, orig_object, offidxstart); 1633b5f359b7SAlan Cox #endif 1634c5aaa06dSAlan Cox } 1635fff19e0eSMark Johnston 1636c5aaa06dSAlan Cox /* 1637c7c8dd7eSAlan Cox * swap_pager_copy() can sleep, in which case the orig_object's 1638c7c8dd7eSAlan Cox * and new_object's locks are released and reacquired. 1639c5aaa06dSAlan Cox */ 1640c5aaa06dSAlan Cox swap_pager_copy(orig_object, new_object, offidxstart, 0); 1641fff19e0eSMark Johnston 1642fff19e0eSMark Johnston TAILQ_FOREACH(m, &new_object->memq, listq) 1643fff19e0eSMark Johnston vm_page_xunbusy(m); 1644fff19e0eSMark Johnston 164598087a06SJeff Roberson vm_object_clear_flag(orig_object, OBJ_SPLIT); 164689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(orig_object); 164789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(new_object); 1648c5aaa06dSAlan Cox entry->object.vm_object = new_object; 1649c5aaa06dSAlan Cox entry->offset = 0LL; 1650c5aaa06dSAlan Cox vm_object_deallocate(orig_object); 165189f6b863SAttilio Rao VM_OBJECT_WLOCK(new_object); 1652c5aaa06dSAlan Cox } 1653c5aaa06dSAlan Cox 165499a1570aSKonstantin Belousov static vm_page_t 165598087a06SJeff Roberson vm_object_collapse_scan_wait(vm_object_t object, vm_page_t p) 165699a1570aSKonstantin Belousov { 165799a1570aSKonstantin Belousov vm_object_t backing_object; 165899a1570aSKonstantin Belousov 165999a1570aSKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(object); 166099a1570aSKonstantin Belousov backing_object = object->backing_object; 166199a1570aSKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(backing_object); 166299a1570aSKonstantin Belousov 166399a1570aSKonstantin Belousov KASSERT(p == NULL || p->object == object || p->object == backing_object, 166499a1570aSKonstantin Belousov ("invalid ownership %p %p %p", p, object, backing_object)); 16658d6fbbb8SJeff Roberson /* The page is only NULL when rename fails. */ 16664cdea4a8SJeff Roberson if (p == NULL) { 16676a14746cSRyan Libby VM_OBJECT_WUNLOCK(object); 16686a14746cSRyan Libby VM_OBJECT_WUNLOCK(backing_object); 16698d6fbbb8SJeff Roberson vm_radix_wait(); 167099a1570aSKonstantin Belousov VM_OBJECT_WLOCK(object); 167187b64663SMark Johnston } else if (p->object == object) { 167287b64663SMark Johnston VM_OBJECT_WUNLOCK(backing_object); 167387b64663SMark Johnston if (vm_page_busy_sleep(p, "vmocol", 0)) 167487b64663SMark Johnston VM_OBJECT_WLOCK(object); 167587b64663SMark Johnston } else { 167687b64663SMark Johnston VM_OBJECT_WUNLOCK(object); 167787b64663SMark Johnston if (!vm_page_busy_sleep(p, "vmocol", 0)) 167887b64663SMark Johnston VM_OBJECT_WUNLOCK(backing_object); 167987b64663SMark Johnston VM_OBJECT_WLOCK(object); 168087b64663SMark Johnston } 168199a1570aSKonstantin Belousov VM_OBJECT_WLOCK(backing_object); 168299a1570aSKonstantin Belousov return (TAILQ_FIRST(&backing_object->memq)); 168399a1570aSKonstantin Belousov } 168499a1570aSKonstantin Belousov 168599a1570aSKonstantin Belousov static bool 16864cc8daf7SConrad Meyer vm_object_scan_all_shadowed(vm_object_t object) 16874cc8daf7SConrad Meyer { 16884cc8daf7SConrad Meyer vm_object_t backing_object; 16894cc8daf7SConrad Meyer vm_page_t p, pp; 169077d6fd97SKonstantin Belousov vm_pindex_t backing_offset_index, new_pindex, pi, ps; 16914cc8daf7SConrad Meyer 16924cc8daf7SConrad Meyer VM_OBJECT_ASSERT_WLOCKED(object); 16934cc8daf7SConrad Meyer VM_OBJECT_ASSERT_WLOCKED(object->backing_object); 16944cc8daf7SConrad Meyer 16954cc8daf7SConrad Meyer backing_object = object->backing_object; 16964cc8daf7SConrad Meyer 169763967687SJeff Roberson if ((backing_object->flags & OBJ_ANON) == 0) 16984cc8daf7SConrad Meyer return (false); 16994cc8daf7SConrad Meyer 170077d6fd97SKonstantin Belousov pi = backing_offset_index = OFF_TO_IDX(object->backing_object_offset); 170177d6fd97SKonstantin Belousov p = vm_page_find_least(backing_object, pi); 170277d6fd97SKonstantin Belousov ps = swap_pager_find_least(backing_object, pi); 17034cc8daf7SConrad Meyer 17044cc8daf7SConrad Meyer /* 170577d6fd97SKonstantin Belousov * Only check pages inside the parent object's range and 170677d6fd97SKonstantin Belousov * inside the parent object's mapping of the backing object. 17074cc8daf7SConrad Meyer */ 170877d6fd97SKonstantin Belousov for (;; pi++) { 170977d6fd97SKonstantin Belousov if (p != NULL && p->pindex < pi) 171077d6fd97SKonstantin Belousov p = TAILQ_NEXT(p, listq); 171177d6fd97SKonstantin Belousov if (ps < pi) 171277d6fd97SKonstantin Belousov ps = swap_pager_find_least(backing_object, pi); 171377d6fd97SKonstantin Belousov if (p == NULL && ps >= backing_object->size) 171477d6fd97SKonstantin Belousov break; 171577d6fd97SKonstantin Belousov else if (p == NULL) 171677d6fd97SKonstantin Belousov pi = ps; 171777d6fd97SKonstantin Belousov else 171877d6fd97SKonstantin Belousov pi = MIN(p->pindex, ps); 171977d6fd97SKonstantin Belousov 172077d6fd97SKonstantin Belousov new_pindex = pi - backing_offset_index; 172177d6fd97SKonstantin Belousov if (new_pindex >= object->size) 172277d6fd97SKonstantin Belousov break; 17234cc8daf7SConrad Meyer 1724cd0047f3SKonstantin Belousov if (p != NULL) { 17254cc8daf7SConrad Meyer /* 1726cd0047f3SKonstantin Belousov * If the backing object page is busy a 1727cd0047f3SKonstantin Belousov * grandparent or older page may still be 1728cd0047f3SKonstantin Belousov * undergoing CoW. It is not safe to collapse 1729cd0047f3SKonstantin Belousov * the backing object until it is quiesced. 173058447749SJeff Roberson */ 1731cd0047f3SKonstantin Belousov if (vm_page_tryxbusy(p) == 0) 173258447749SJeff Roberson return (false); 173358447749SJeff Roberson 173458447749SJeff Roberson /* 1735cd0047f3SKonstantin Belousov * We raced with the fault handler that left 1736cd0047f3SKonstantin Belousov * newly allocated invalid page on the object 1737cd0047f3SKonstantin Belousov * queue and retried. 1738cd0047f3SKonstantin Belousov */ 1739cd0047f3SKonstantin Belousov if (!vm_page_all_valid(p)) 1740cd0047f3SKonstantin Belousov goto unbusy_ret; 1741cd0047f3SKonstantin Belousov } 1742cd0047f3SKonstantin Belousov 1743cd0047f3SKonstantin Belousov /* 17444cc8daf7SConrad Meyer * See if the parent has the page or if the parent's object 17454cc8daf7SConrad Meyer * pager has the page. If the parent has the page but the page 17464cc8daf7SConrad Meyer * is not valid, the parent's object pager must have the page. 17474cc8daf7SConrad Meyer * 17484cc8daf7SConrad Meyer * If this fails, the parent does not completely shadow the 17494cc8daf7SConrad Meyer * object and we might as well give up now. 17504cc8daf7SConrad Meyer */ 17514cc8daf7SConrad Meyer pp = vm_page_lookup(object, new_pindex); 1752cd0047f3SKonstantin Belousov 17530012f373SJeff Roberson /* 1754cd0047f3SKonstantin Belousov * The valid check here is stable due to object lock 1755cd0047f3SKonstantin Belousov * being required to clear valid and initiate paging. 1756cd0047f3SKonstantin Belousov * Busy of p disallows fault handler to validate pp. 17570012f373SJeff Roberson */ 17580012f373SJeff Roberson if ((pp == NULL || vm_page_none_valid(pp)) && 17594cc8daf7SConrad Meyer !vm_pager_has_page(object, new_pindex, NULL, NULL)) 1760cd0047f3SKonstantin Belousov goto unbusy_ret; 1761cd0047f3SKonstantin Belousov if (p != NULL) 1762cd0047f3SKonstantin Belousov vm_page_xunbusy(p); 17634cc8daf7SConrad Meyer } 17644cc8daf7SConrad Meyer return (true); 1765cd0047f3SKonstantin Belousov 1766cd0047f3SKonstantin Belousov unbusy_ret: 1767cd0047f3SKonstantin Belousov if (p != NULL) 1768cd0047f3SKonstantin Belousov vm_page_xunbusy(p); 1769cd0047f3SKonstantin Belousov return (false); 17704cc8daf7SConrad Meyer } 17714cc8daf7SConrad Meyer 177298087a06SJeff Roberson static void 177398087a06SJeff Roberson vm_object_collapse_scan(vm_object_t object) 17742ad1a3f7SMatthew Dillon { 17752ad1a3f7SMatthew Dillon vm_object_t backing_object; 177699a1570aSKonstantin Belousov vm_page_t next, p, pp; 177799a1570aSKonstantin Belousov vm_pindex_t backing_offset_index, new_pindex; 17782ad1a3f7SMatthew Dillon 177989f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 178089f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object->backing_object); 17812ad1a3f7SMatthew Dillon 17822ad1a3f7SMatthew Dillon backing_object = object->backing_object; 17832ad1a3f7SMatthew Dillon backing_offset_index = OFF_TO_IDX(object->backing_object_offset); 17842ad1a3f7SMatthew Dillon 17852ad1a3f7SMatthew Dillon /* 17862ad1a3f7SMatthew Dillon * Our scan 17872ad1a3f7SMatthew Dillon */ 17884cc8daf7SConrad Meyer for (p = TAILQ_FIRST(&backing_object->memq); p != NULL; p = next) { 178999a1570aSKonstantin Belousov next = TAILQ_NEXT(p, listq); 179099a1570aSKonstantin Belousov new_pindex = p->pindex - backing_offset_index; 17912ad1a3f7SMatthew Dillon 17922ad1a3f7SMatthew Dillon /* 17932ad1a3f7SMatthew Dillon * Check for busy page 17942ad1a3f7SMatthew Dillon */ 179563e97555SJeff Roberson if (vm_page_tryxbusy(p) == 0) { 179698087a06SJeff Roberson next = vm_object_collapse_scan_wait(object, p); 17972ad1a3f7SMatthew Dillon continue; 17982ad1a3f7SMatthew Dillon } 17992ad1a3f7SMatthew Dillon 180098087a06SJeff Roberson KASSERT(object->backing_object == backing_object, 180198087a06SJeff Roberson ("vm_object_collapse_scan: backing object mismatch %p != %p", 180298087a06SJeff Roberson object->backing_object, backing_object)); 180399a1570aSKonstantin Belousov KASSERT(p->object == backing_object, 180498087a06SJeff Roberson ("vm_object_collapse_scan: object mismatch %p != %p", 180598087a06SJeff Roberson p->object, backing_object)); 18062ad1a3f7SMatthew Dillon 180799a1570aSKonstantin Belousov if (p->pindex < backing_offset_index || 180899a1570aSKonstantin Belousov new_pindex >= object->size) { 18091390a5cbSKonstantin Belousov vm_pager_freespace(backing_object, p->pindex, 1); 1810e946b949SAttilio Rao 1811f6d89838SAlan Cox KASSERT(!pmap_page_is_mapped(p), 1812f6d89838SAlan Cox ("freeing mapped page %p", p)); 18130fd977b3SMark Johnston if (vm_page_remove(p)) 18142ad1a3f7SMatthew Dillon vm_page_free(p); 18152ad1a3f7SMatthew Dillon continue; 18162ad1a3f7SMatthew Dillon } 18172ad1a3f7SMatthew Dillon 1818cd0047f3SKonstantin Belousov if (!vm_page_all_valid(p)) { 1819cd0047f3SKonstantin Belousov KASSERT(!pmap_page_is_mapped(p), 1820cd0047f3SKonstantin Belousov ("freeing mapped page %p", p)); 1821cd0047f3SKonstantin Belousov if (vm_page_remove(p)) 1822cd0047f3SKonstantin Belousov vm_page_free(p); 1823cd0047f3SKonstantin Belousov continue; 1824cd0047f3SKonstantin Belousov } 1825cd0047f3SKonstantin Belousov 18262ad1a3f7SMatthew Dillon pp = vm_page_lookup(object, new_pindex); 182763e97555SJeff Roberson if (pp != NULL && vm_page_tryxbusy(pp) == 0) { 182863e97555SJeff Roberson vm_page_xunbusy(p); 1829e18cc7bfSMax Laier /* 18304cc8daf7SConrad Meyer * The page in the parent is busy and possibly not 18314cc8daf7SConrad Meyer * (yet) valid. Until its state is finalized by the 18324cc8daf7SConrad Meyer * busy bit owner, we can't tell whether it shadows the 183398087a06SJeff Roberson * original page. 1834e18cc7bfSMax Laier */ 183598087a06SJeff Roberson next = vm_object_collapse_scan_wait(object, pp); 1836e18cc7bfSMax Laier continue; 1837e18cc7bfSMax Laier } 183899a1570aSKonstantin Belousov 18394bf95d00SJeff Roberson if (pp != NULL && vm_page_none_valid(pp)) { 18404bf95d00SJeff Roberson /* 18414bf95d00SJeff Roberson * The page was invalid in the parent. Likely placed 18424bf95d00SJeff Roberson * there by an incomplete fault. Just remove and 18434bf95d00SJeff Roberson * ignore. p can replace it. 18444bf95d00SJeff Roberson */ 18454bf95d00SJeff Roberson if (vm_page_remove(pp)) 18464bf95d00SJeff Roberson vm_page_free(pp); 18474bf95d00SJeff Roberson pp = NULL; 18484bf95d00SJeff Roberson } 184999a1570aSKonstantin Belousov 18504cc8daf7SConrad Meyer if (pp != NULL || vm_pager_has_page(object, new_pindex, NULL, 18514cc8daf7SConrad Meyer NULL)) { 185299a1570aSKonstantin Belousov /* 18534cc8daf7SConrad Meyer * The page already exists in the parent OR swap exists 18544cc8daf7SConrad Meyer * for this location in the parent. Leave the parent's 18554cc8daf7SConrad Meyer * page alone. Destroy the original page from the 18564cc8daf7SConrad Meyer * backing object. 185799a1570aSKonstantin Belousov */ 18581390a5cbSKonstantin Belousov vm_pager_freespace(backing_object, p->pindex, 1); 1859f6d89838SAlan Cox KASSERT(!pmap_page_is_mapped(p), 1860f6d89838SAlan Cox ("freeing mapped page %p", p)); 18610fd977b3SMark Johnston if (vm_page_remove(p)) 18622ad1a3f7SMatthew Dillon vm_page_free(p); 186363e97555SJeff Roberson if (pp != NULL) 186463e97555SJeff Roberson vm_page_xunbusy(pp); 18652ad1a3f7SMatthew Dillon continue; 18662ad1a3f7SMatthew Dillon } 18672ad1a3f7SMatthew Dillon 1868e946b949SAttilio Rao /* 18694cc8daf7SConrad Meyer * Page does not exist in parent, rename the page from the 18704cc8daf7SConrad Meyer * backing object to the main object. 1871e946b949SAttilio Rao * 18724cc8daf7SConrad Meyer * If the page was mapped to a process, it can remain mapped 18733453bca8SAlan Cox * through the rename. vm_page_rename() will dirty the page. 1874e946b949SAttilio Rao */ 1875e946b949SAttilio Rao if (vm_page_rename(p, object, new_pindex)) { 187663e97555SJeff Roberson vm_page_xunbusy(p); 187798087a06SJeff Roberson next = vm_object_collapse_scan_wait(object, NULL); 1878e946b949SAttilio Rao continue; 1879e946b949SAttilio Rao } 188014a5dc17SAttilio Rao 188114a5dc17SAttilio Rao /* Use the old pindex to free the right page. */ 18821390a5cbSKonstantin Belousov vm_pager_freespace(backing_object, new_pindex + 18831390a5cbSKonstantin Belousov backing_offset_index, 1); 1884e946b949SAttilio Rao 1885f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 1886f8a47341SAlan Cox /* 1887f8a47341SAlan Cox * Rename the reservation. 1888f8a47341SAlan Cox */ 1889f8a47341SAlan Cox vm_reserv_rename(p, object, backing_object, 1890f8a47341SAlan Cox backing_offset_index); 1891f8a47341SAlan Cox #endif 18928da1c098SJeff Roberson vm_page_xunbusy(p); 18932ad1a3f7SMatthew Dillon } 18942fe6e4d7SDavid Greenman return; 18952fe6e4d7SDavid Greenman } 18962fe6e4d7SDavid Greenman 1897df8bae1dSRodney W. Grimes /* 1898df8bae1dSRodney W. Grimes * vm_object_collapse: 1899df8bae1dSRodney W. Grimes * 1900df8bae1dSRodney W. Grimes * Collapse an object with the object backing it. 1901df8bae1dSRodney W. Grimes * Pages in the backing object are moved into the 1902df8bae1dSRodney W. Grimes * parent, and the backing object is deallocated. 1903df8bae1dSRodney W. Grimes */ 190426f9a767SRodney W. Grimes void 19051b40f8c0SMatthew Dillon vm_object_collapse(vm_object_t object) 1906df8bae1dSRodney W. Grimes { 190798f139daSKonstantin Belousov vm_object_t backing_object, new_backing_object; 190898f139daSKonstantin Belousov 190989f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 191023955314SAlfred Perlstein 1911df8bae1dSRodney W. Grimes while (TRUE) { 191298087a06SJeff Roberson KASSERT((object->flags & (OBJ_DEAD | OBJ_ANON)) == OBJ_ANON, 191398087a06SJeff Roberson ("collapsing invalid object")); 191498087a06SJeff Roberson 1915df8bae1dSRodney W. Grimes /* 191698087a06SJeff Roberson * Wait for the backing_object to finish any pending 191798087a06SJeff Roberson * collapse so that the caller sees the shortest possible 191898087a06SJeff Roberson * shadow chain. 1919df8bae1dSRodney W. Grimes */ 192098087a06SJeff Roberson backing_object = vm_object_backing_collapse_wait(object); 192198087a06SJeff Roberson if (backing_object == NULL) 192298087a06SJeff Roberson return; 192398087a06SJeff Roberson 192498087a06SJeff Roberson KASSERT(object->ref_count > 0 && 19254cf9f5d8SKonstantin Belousov object->ref_count > atomic_load_int(&object->shadow_count), 192698087a06SJeff Roberson ("collapse with invalid ref %d or shadow %d count.", 19274cf9f5d8SKonstantin Belousov object->ref_count, atomic_load_int(&object->shadow_count))); 192898087a06SJeff Roberson KASSERT((backing_object->flags & 192998087a06SJeff Roberson (OBJ_COLLAPSING | OBJ_DEAD)) == 0, 193098087a06SJeff Roberson ("vm_object_collapse: Backing object already collapsing.")); 193198087a06SJeff Roberson KASSERT((object->flags & (OBJ_COLLAPSING | OBJ_DEAD)) == 0, 193298087a06SJeff Roberson ("vm_object_collapse: object is already collapsing.")); 1933df8bae1dSRodney W. Grimes 1934f919ebdeSDavid Greenman /* 193598087a06SJeff Roberson * We know that we can either collapse the backing object if 193698087a06SJeff Roberson * the parent is the only reference to it, or (perhaps) have 19372ad1a3f7SMatthew Dillon * the parent bypass the object if the parent happens to shadow 19382ad1a3f7SMatthew Dillon * all the resident pages in the entire backing object. 1939df8bae1dSRodney W. Grimes */ 1940df8bae1dSRodney W. Grimes if (backing_object->ref_count == 1) { 19414cf9f5d8SKonstantin Belousov KASSERT(atomic_load_int(&backing_object->shadow_count) 19424cf9f5d8SKonstantin Belousov == 1, 194398087a06SJeff Roberson ("vm_object_collapse: shadow_count: %d", 19444cf9f5d8SKonstantin Belousov atomic_load_int(&backing_object->shadow_count))); 1945aa9bc3b1SKonstantin Belousov vm_object_pip_add(object, 1); 194698087a06SJeff Roberson vm_object_set_flag(object, OBJ_COLLAPSING); 1947aa9bc3b1SKonstantin Belousov vm_object_pip_add(backing_object, 1); 194898087a06SJeff Roberson vm_object_set_flag(backing_object, OBJ_DEAD); 1949aa9bc3b1SKonstantin Belousov 1950df8bae1dSRodney W. Grimes /* 19512ad1a3f7SMatthew Dillon * If there is exactly one reference to the backing 19522ad1a3f7SMatthew Dillon * object, we can collapse it into the parent. 1953df8bae1dSRodney W. Grimes */ 195498087a06SJeff Roberson vm_object_collapse_scan(object); 1955df8bae1dSRodney W. Grimes 1956f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 1957f8a47341SAlan Cox /* 1958f8a47341SAlan Cox * Break any reservations from backing_object. 1959f8a47341SAlan Cox */ 1960f8a47341SAlan Cox if (__predict_false(!LIST_EMPTY(&backing_object->rvq))) 1961f8a47341SAlan Cox vm_reserv_break_all(backing_object); 1962f8a47341SAlan Cox #endif 1963f8a47341SAlan Cox 1964df8bae1dSRodney W. Grimes /* 1965df8bae1dSRodney W. Grimes * Move the pager from backing_object to object. 1966fff19e0eSMark Johnston * 1967fff19e0eSMark Johnston * swap_pager_copy() can sleep, in which case the 1968fff19e0eSMark Johnston * backing_object's and object's locks are released and 1969fff19e0eSMark Johnston * reacquired. 1970df8bae1dSRodney W. Grimes */ 1971fff19e0eSMark Johnston swap_pager_copy(backing_object, object, 19721c7c3c6aSMatthew Dillon OFF_TO_IDX(object->backing_object_offset), TRUE); 197398087a06SJeff Roberson 1974df8bae1dSRodney W. Grimes /* 1975df8bae1dSRodney W. Grimes * Object now shadows whatever backing_object did. 1976df8bae1dSRodney W. Grimes */ 197798087a06SJeff Roberson vm_object_clear_flag(object, OBJ_COLLAPSING); 197898087a06SJeff Roberson vm_object_backing_transfer(object, backing_object); 19792ad1a3f7SMatthew Dillon object->backing_object_offset += 19802ad1a3f7SMatthew Dillon backing_object->backing_object_offset; 198198087a06SJeff Roberson VM_OBJECT_WUNLOCK(object); 198298087a06SJeff Roberson vm_object_pip_wakeup(object); 19832ad1a3f7SMatthew Dillon 1984df8bae1dSRodney W. Grimes /* 1985df8bae1dSRodney W. Grimes * Discard backing_object. 1986df8bae1dSRodney W. Grimes * 19870d94caffSDavid Greenman * Since the backing object has no pages, no pager left, 19880d94caffSDavid Greenman * and no object references within it, all that is 19890d94caffSDavid Greenman * necessary is to dispose of it. 1990df8bae1dSRodney W. Grimes */ 19919b4d473aSKonstantin Belousov KASSERT(backing_object->ref_count == 1, ( 19929b4d473aSKonstantin Belousov "backing_object %p was somehow re-referenced during collapse!", 19939b4d473aSKonstantin Belousov backing_object)); 1994aa9bc3b1SKonstantin Belousov vm_object_pip_wakeup(backing_object); 199598087a06SJeff Roberson (void)refcount_release(&backing_object->ref_count); 19966f3e9bacSPawel Jakub Dawidek umtx_shm_object_terminated(backing_object); 199798087a06SJeff Roberson vm_object_terminate(backing_object); 199811542376SAlan Cox counter_u64_add(object_collapses, 1); 199998087a06SJeff Roberson VM_OBJECT_WLOCK(object); 20000d94caffSDavid Greenman } else { 2001df8bae1dSRodney W. Grimes /* 20022ad1a3f7SMatthew Dillon * If we do not entirely shadow the backing object, 20032ad1a3f7SMatthew Dillon * there is nothing we can do so we give up. 200498087a06SJeff Roberson * 200598087a06SJeff Roberson * The object lock and backing_object lock must not 200698087a06SJeff Roberson * be dropped during this sequence. 2007df8bae1dSRodney W. Grimes */ 200858447749SJeff Roberson if (!vm_object_scan_all_shadowed(object)) { 200989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 20102ad1a3f7SMatthew Dillon break; 201124a1cce3SDavid Greenman } 2012df8bae1dSRodney W. Grimes 2013df8bae1dSRodney W. Grimes /* 20140d94caffSDavid Greenman * Make the parent shadow the next object in the 20150d94caffSDavid Greenman * chain. Deallocating backing_object will not remove 20160d94caffSDavid Greenman * it, since its reference count is at least 2. 2017df8bae1dSRodney W. Grimes */ 201851b867e5SJeff Roberson vm_object_backing_remove_locked(object); 201995e5e988SJohn Dyson new_backing_object = backing_object->backing_object; 202051b867e5SJeff Roberson if (new_backing_object != NULL) { 202198087a06SJeff Roberson vm_object_backing_insert_ref(object, 202251b867e5SJeff Roberson new_backing_object); 202395e5e988SJohn Dyson object->backing_object_offset += 202495e5e988SJohn Dyson backing_object->backing_object_offset; 2025de5f6a77SJohn Dyson } 2026df8bae1dSRodney W. Grimes 2027df8bae1dSRodney W. Grimes /* 20280d94caffSDavid Greenman * Drop the reference count on backing_object. Since 202922ec553fSAlan Cox * its ref_count was at least 2, it will not vanish. 2030df8bae1dSRodney W. Grimes */ 203198087a06SJeff Roberson (void)refcount_release(&backing_object->ref_count); 203298087a06SJeff Roberson KASSERT(backing_object->ref_count >= 1, ( 203398087a06SJeff Roberson "backing_object %p was somehow dereferenced during collapse!", 203498087a06SJeff Roberson backing_object)); 203589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 203611542376SAlan Cox counter_u64_add(object_bypasses, 1); 2037df8bae1dSRodney W. Grimes } 2038df8bae1dSRodney W. Grimes 2039df8bae1dSRodney W. Grimes /* 2040df8bae1dSRodney W. Grimes * Try again with this object's new backing object. 2041df8bae1dSRodney W. Grimes */ 2042df8bae1dSRodney W. Grimes } 2043df8bae1dSRodney W. Grimes } 2044df8bae1dSRodney W. Grimes 2045df8bae1dSRodney W. Grimes /* 2046bff99f0dSAlan Cox * vm_object_page_remove: 2047df8bae1dSRodney W. Grimes * 204868855966SAlan Cox * For the given object, either frees or invalidates each of the 20496bbee8e2SAlan Cox * specified pages. In general, a page is freed. However, if a page is 20506bbee8e2SAlan Cox * wired for any reason other than the existence of a managed, wired 20516bbee8e2SAlan Cox * mapping, then it may be invalidated but not removed from the object. 20526bbee8e2SAlan Cox * Pages are specified by the given range ["start", "end") and the option 20536bbee8e2SAlan Cox * OBJPR_CLEANONLY. As a special case, if "end" is zero, then the range 20546bbee8e2SAlan Cox * extends from "start" to the end of the object. If the option 20556bbee8e2SAlan Cox * OBJPR_CLEANONLY is specified, then only the non-dirty pages within the 20566bbee8e2SAlan Cox * specified range are affected. If the option OBJPR_NOTMAPPED is 20576bbee8e2SAlan Cox * specified, then the pages within the specified range must have no 20586bbee8e2SAlan Cox * mappings. Otherwise, if this option is not specified, any mappings to 20596bbee8e2SAlan Cox * the specified pages are removed before the pages are freed or 20606bbee8e2SAlan Cox * invalidated. 206168855966SAlan Cox * 20626bbee8e2SAlan Cox * In general, this operation should only be performed on objects that 20636bbee8e2SAlan Cox * contain managed pages. There are, however, two exceptions. First, it 20646bbee8e2SAlan Cox * is performed on the kernel and kmem objects by vm_map_entry_delete(). 20656bbee8e2SAlan Cox * Second, it is used by msync(..., MS_INVALIDATE) to invalidate device- 20666bbee8e2SAlan Cox * backed pages. In both of these cases, the option OBJPR_CLEANONLY must 20676bbee8e2SAlan Cox * not be specified and the option OBJPR_NOTMAPPED must be specified. 2068df8bae1dSRodney W. Grimes * 2069df8bae1dSRodney W. Grimes * The object must be locked. 2070df8bae1dSRodney W. Grimes */ 207126f9a767SRodney W. Grimes void 2072ecde4b32SAlan Cox vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end, 20736bbee8e2SAlan Cox int options) 2074df8bae1dSRodney W. Grimes { 2075d031cff1SMatthew Dillon vm_page_t p, next; 2076df8bae1dSRodney W. Grimes 207789f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 207828634820SAlan Cox KASSERT((object->flags & OBJ_UNMANAGED) == 0 || 20796bbee8e2SAlan Cox (options & (OBJPR_CLEANONLY | OBJPR_NOTMAPPED)) == OBJPR_NOTMAPPED, 20806bbee8e2SAlan Cox ("vm_object_page_remove: illegal options for object %p", object)); 2081ecde4b32SAlan Cox if (object->resident_page_count == 0) 20827667839aSAlan Cox return; 2083d474eaaaSDoug Rabson vm_object_pip_add(object, 1); 208426f9a767SRodney W. Grimes again: 2085b382c10aSKonstantin Belousov p = vm_page_find_least(object, start); 20862965a453SKip Macy 208775741c04SAlan Cox /* 20886bbee8e2SAlan Cox * Here, the variable "p" is either (1) the page with the least pindex 20896bbee8e2SAlan Cox * greater than or equal to the parameter "start" or (2) NULL. 209075741c04SAlan Cox */ 20916bbee8e2SAlan Cox for (; p != NULL && (p->pindex < end || end == 0); p = next) { 2092b18bfc3dSJohn Dyson next = TAILQ_NEXT(p, listq); 209375741c04SAlan Cox 209459677d3cSAlan Cox /* 2095d28af1abSMark Johnston * Skip invalid pages if asked to do so. Try to avoid acquiring 2096d28af1abSMark Johnston * the busy lock, as some consumers rely on this to avoid 2097d28af1abSMark Johnston * deadlocks. 2098d28af1abSMark Johnston * 2099d28af1abSMark Johnston * A thread may concurrently transition the page from invalid to 2100d28af1abSMark Johnston * valid using only the busy lock, so the result of this check 2101d28af1abSMark Johnston * is immediately stale. It is up to consumers to handle this, 2102d28af1abSMark Johnston * for instance by ensuring that all invalid->valid transitions 2103d28af1abSMark Johnston * happen with a mutex held, as may be possible for a 2104d28af1abSMark Johnston * filesystem. 2105d28af1abSMark Johnston */ 2106d28af1abSMark Johnston if ((options & OBJPR_VALIDONLY) != 0 && vm_page_none_valid(p)) 2107d28af1abSMark Johnston continue; 2108d28af1abSMark Johnston 2109d28af1abSMark Johnston /* 21106bbee8e2SAlan Cox * If the page is wired for any reason besides the existence 21116bbee8e2SAlan Cox * of managed, wired mappings, then it cannot be freed. For 21126bbee8e2SAlan Cox * example, fictitious pages, which represent device memory, 21136bbee8e2SAlan Cox * are inherently wired and cannot be freed. They can, 21146bbee8e2SAlan Cox * however, be invalidated if the option OBJPR_CLEANONLY is 21156bbee8e2SAlan Cox * not specified. 211659677d3cSAlan Cox */ 211763e97555SJeff Roberson if (vm_page_tryxbusy(p) == 0) { 211887b64663SMark Johnston if (vm_page_busy_sleep(p, "vmopar", 0)) 211987b64663SMark Johnston VM_OBJECT_WLOCK(object); 2120fee2a2faSMark Johnston goto again; 2121fee2a2faSMark Johnston } 2122d28af1abSMark Johnston if ((options & OBJPR_VALIDONLY) != 0 && vm_page_none_valid(p)) { 2123d28af1abSMark Johnston vm_page_xunbusy(p); 2124d28af1abSMark Johnston continue; 2125d28af1abSMark Johnston } 2126d842aa51SMark Johnston if (vm_page_wired(p)) { 2127fee2a2faSMark Johnston wired: 2128cf060942SAlan Cox if ((options & OBJPR_NOTMAPPED) == 0 && 2129cf060942SAlan Cox object->ref_count != 0) 21304fec79beSAlan Cox pmap_remove_all(p); 21316bbee8e2SAlan Cox if ((options & OBJPR_CLEANONLY) == 0) { 21320012f373SJeff Roberson vm_page_invalid(p); 2133a28042d1SAlan Cox vm_page_undirty(p); 2134a28042d1SAlan Cox } 213563e97555SJeff Roberson vm_page_xunbusy(p); 213693c5d3a4SKonstantin Belousov continue; 21370d94caffSDavid Greenman } 213868855966SAlan Cox KASSERT((p->flags & PG_FICTITIOUS) == 0, 213968855966SAlan Cox ("vm_object_page_remove: page %p is fictitious", p)); 21400012f373SJeff Roberson if ((options & OBJPR_CLEANONLY) != 0 && 21410012f373SJeff Roberson !vm_page_none_valid(p)) { 2142cf060942SAlan Cox if ((options & OBJPR_NOTMAPPED) == 0 && 2143fee2a2faSMark Johnston object->ref_count != 0 && 2144fee2a2faSMark Johnston !vm_page_try_remove_write(p)) 2145fee2a2faSMark Johnston goto wired; 214663e97555SJeff Roberson if (p->dirty != 0) { 214763e97555SJeff Roberson vm_page_xunbusy(p); 214893c5d3a4SKonstantin Belousov continue; 21492965a453SKip Macy } 215063e97555SJeff Roberson } 2151fee2a2faSMark Johnston if ((options & OBJPR_NOTMAPPED) == 0 && 2152fee2a2faSMark Johnston object->ref_count != 0 && !vm_page_try_remove_all(p)) 2153fee2a2faSMark Johnston goto wired; 21545cd29d0fSMark Johnston vm_page_free(p); 21552965a453SKip Macy } 2156f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 215784242cf6SMark Johnston 21581390a5cbSKonstantin Belousov vm_pager_freespace(object, start, (end == 0 ? object->size : end) - 21591390a5cbSKonstantin Belousov start); 2160c0503609SDavid Greenman } 2161df8bae1dSRodney W. Grimes 2162df8bae1dSRodney W. Grimes /* 21633138cd36SMark Johnston * vm_object_page_noreuse: 2164936c09acSJohn Baldwin * 21653138cd36SMark Johnston * For the given object, attempt to move the specified pages to 21663138cd36SMark Johnston * the head of the inactive queue. This bypasses regular LRU 21673138cd36SMark Johnston * operation and allows the pages to be reused quickly under memory 21683138cd36SMark Johnston * pressure. If a page is wired for any reason, then it will not 21693138cd36SMark Johnston * be queued. Pages are specified by the range ["start", "end"). 21703138cd36SMark Johnston * As a special case, if "end" is zero, then the range extends from 21713138cd36SMark Johnston * "start" to the end of the object. 2172936c09acSJohn Baldwin * 2173936c09acSJohn Baldwin * This operation should only be performed on objects that 217428634820SAlan Cox * contain non-fictitious, managed pages. 2175936c09acSJohn Baldwin * 2176936c09acSJohn Baldwin * The object must be locked. 2177936c09acSJohn Baldwin */ 2178936c09acSJohn Baldwin void 21793138cd36SMark Johnston vm_object_page_noreuse(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 2180936c09acSJohn Baldwin { 2181936c09acSJohn Baldwin vm_page_t p, next; 2182936c09acSJohn Baldwin 218352d1addaSAlan Cox VM_OBJECT_ASSERT_LOCKED(object); 218428634820SAlan Cox KASSERT((object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0, 21853138cd36SMark Johnston ("vm_object_page_noreuse: illegal object %p", object)); 2186936c09acSJohn Baldwin if (object->resident_page_count == 0) 2187936c09acSJohn Baldwin return; 2188936c09acSJohn Baldwin p = vm_page_find_least(object, start); 2189936c09acSJohn Baldwin 2190936c09acSJohn Baldwin /* 2191936c09acSJohn Baldwin * Here, the variable "p" is either (1) the page with the least pindex 2192936c09acSJohn Baldwin * greater than or equal to the parameter "start" or (2) NULL. 2193936c09acSJohn Baldwin */ 2194936c09acSJohn Baldwin for (; p != NULL && (p->pindex < end || end == 0); p = next) { 2195936c09acSJohn Baldwin next = TAILQ_NEXT(p, listq); 21963138cd36SMark Johnston vm_page_deactivate_noreuse(p); 2197936c09acSJohn Baldwin } 2198936c09acSJohn Baldwin } 2199936c09acSJohn Baldwin 2200936c09acSJohn Baldwin /* 2201387aabc5SAlan Cox * Populate the specified range of the object with valid pages. Returns 2202387aabc5SAlan Cox * TRUE if the range is successfully populated and FALSE otherwise. 2203387aabc5SAlan Cox * 2204387aabc5SAlan Cox * Note: This function should be optimized to pass a larger array of 2205387aabc5SAlan Cox * pages to vm_pager_get_pages() before it is applied to a non- 2206387aabc5SAlan Cox * OBJT_DEVICE object. 2207387aabc5SAlan Cox * 2208387aabc5SAlan Cox * The object must be locked. 2209387aabc5SAlan Cox */ 2210387aabc5SAlan Cox boolean_t 2211387aabc5SAlan Cox vm_object_populate(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 2212387aabc5SAlan Cox { 2213093c7f39SGleb Smirnoff vm_page_t m; 2214387aabc5SAlan Cox vm_pindex_t pindex; 2215387aabc5SAlan Cox int rv; 2216387aabc5SAlan Cox 221789f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 2218387aabc5SAlan Cox for (pindex = start; pindex < end; pindex++) { 2219c7575748SJeff Roberson rv = vm_page_grab_valid(&m, object, pindex, VM_ALLOC_NORMAL); 2220c7575748SJeff Roberson if (rv != VM_PAGER_OK) 2221387aabc5SAlan Cox break; 2222c7575748SJeff Roberson 2223387aabc5SAlan Cox /* 2224387aabc5SAlan Cox * Keep "m" busy because a subsequent iteration may unlock 2225387aabc5SAlan Cox * the object. 2226387aabc5SAlan Cox */ 2227387aabc5SAlan Cox } 2228387aabc5SAlan Cox if (pindex > start) { 2229387aabc5SAlan Cox m = vm_page_lookup(object, start); 2230387aabc5SAlan Cox while (m != NULL && m->pindex < pindex) { 2231c7aebda8SAttilio Rao vm_page_xunbusy(m); 2232387aabc5SAlan Cox m = TAILQ_NEXT(m, listq); 2233387aabc5SAlan Cox } 2234387aabc5SAlan Cox } 2235387aabc5SAlan Cox return (pindex == end); 2236387aabc5SAlan Cox } 2237387aabc5SAlan Cox 2238387aabc5SAlan Cox /* 2239df8bae1dSRodney W. Grimes * Routine: vm_object_coalesce 2240df8bae1dSRodney W. Grimes * Function: Coalesces two objects backing up adjoining 2241df8bae1dSRodney W. Grimes * regions of memory into a single object. 2242df8bae1dSRodney W. Grimes * 2243df8bae1dSRodney W. Grimes * returns TRUE if objects were combined. 2244df8bae1dSRodney W. Grimes * 2245df8bae1dSRodney W. Grimes * NOTE: Only works at the moment if the second object is NULL - 2246df8bae1dSRodney W. Grimes * if it's not, which object do we lock first? 2247df8bae1dSRodney W. Grimes * 2248df8bae1dSRodney W. Grimes * Parameters: 2249df8bae1dSRodney W. Grimes * prev_object First object to coalesce 2250df8bae1dSRodney W. Grimes * prev_offset Offset into prev_object 2251df8bae1dSRodney W. Grimes * prev_size Size of reference to prev_object 225257a21abaSAlan Cox * next_size Size of reference to the second object 22533364c323SKonstantin Belousov * reserved Indicator that extension region has 22543364c323SKonstantin Belousov * swap accounted for 2255df8bae1dSRodney W. Grimes * 2256df8bae1dSRodney W. Grimes * Conditions: 2257df8bae1dSRodney W. Grimes * The object must *not* be locked. 2258df8bae1dSRodney W. Grimes */ 22590d94caffSDavid Greenman boolean_t 226057a21abaSAlan Cox vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset, 22613364c323SKonstantin Belousov vm_size_t prev_size, vm_size_t next_size, boolean_t reserved) 2262df8bae1dSRodney W. Grimes { 2263ea41812fSAlan Cox vm_pindex_t next_pindex; 2264df8bae1dSRodney W. Grimes 226500e1854aSAlan Cox if (prev_object == NULL) 2266df8bae1dSRodney W. Grimes return (TRUE); 226763967687SJeff Roberson if ((prev_object->flags & OBJ_ANON) == 0) 226830dcfc09SJohn Dyson return (FALSE); 226930dcfc09SJohn Dyson 227063967687SJeff Roberson VM_OBJECT_WLOCK(prev_object); 2271df8bae1dSRodney W. Grimes /* 227298087a06SJeff Roberson * Try to collapse the object first. 2273df8bae1dSRodney W. Grimes */ 2274df8bae1dSRodney W. Grimes vm_object_collapse(prev_object); 2275df8bae1dSRodney W. Grimes 2276df8bae1dSRodney W. Grimes /* 22770d94caffSDavid Greenman * Can't coalesce if: . more than one reference . paged out . shadows 22780d94caffSDavid Greenman * another object . has a copy elsewhere (any of which mean that the 22790d94caffSDavid Greenman * pages not mapped to prev_entry may be in use anyway) 2280df8bae1dSRodney W. Grimes */ 22818cc7e047SJohn Dyson if (prev_object->backing_object != NULL) { 228289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(prev_object); 2283df8bae1dSRodney W. Grimes return (FALSE); 2284df8bae1dSRodney W. Grimes } 2285a316d390SJohn Dyson 2286a316d390SJohn Dyson prev_size >>= PAGE_SHIFT; 2287a316d390SJohn Dyson next_size >>= PAGE_SHIFT; 228857a21abaSAlan Cox next_pindex = OFF_TO_IDX(prev_offset) + prev_size; 22898cc7e047SJohn Dyson 22900e48e068SMark Johnston if (prev_object->ref_count > 1 && 22910e48e068SMark Johnston prev_object->size != next_pindex && 22920e48e068SMark Johnston (prev_object->flags & OBJ_ONEMAPPING) == 0) { 229389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(prev_object); 22948cc7e047SJohn Dyson return (FALSE); 22958cc7e047SJohn Dyson } 22968cc7e047SJohn Dyson 2297df8bae1dSRodney W. Grimes /* 22983364c323SKonstantin Belousov * Account for the charge. 22993364c323SKonstantin Belousov */ 2300ef694c1aSEdward Tomasz Napierala if (prev_object->cred != NULL) { 23013364c323SKonstantin Belousov /* 23023364c323SKonstantin Belousov * If prev_object was charged, then this mapping, 2303763df3ecSPedro F. Giffuni * although not charged now, may become writable 2304ef694c1aSEdward Tomasz Napierala * later. Non-NULL cred in the object would prevent 23053364c323SKonstantin Belousov * swap reservation during enabling of the write 23063364c323SKonstantin Belousov * access, so reserve swap now. Failed reservation 23073364c323SKonstantin Belousov * cause allocation of the separate object for the map 23083364c323SKonstantin Belousov * entry, and swap reservation for this entry is 23093364c323SKonstantin Belousov * managed in appropriate time. 23103364c323SKonstantin Belousov */ 2311ef694c1aSEdward Tomasz Napierala if (!reserved && !swap_reserve_by_cred(ptoa(next_size), 2312ef694c1aSEdward Tomasz Napierala prev_object->cred)) { 23139f790a17SKonstantin Belousov VM_OBJECT_WUNLOCK(prev_object); 23143364c323SKonstantin Belousov return (FALSE); 23153364c323SKonstantin Belousov } 23163364c323SKonstantin Belousov prev_object->charge += ptoa(next_size); 23173364c323SKonstantin Belousov } 23183364c323SKonstantin Belousov 23193364c323SKonstantin Belousov /* 23200d94caffSDavid Greenman * Remove any pages that may still be in the object from a previous 23210d94caffSDavid Greenman * deallocation. 2322df8bae1dSRodney W. Grimes */ 2323ea41812fSAlan Cox if (next_pindex < prev_object->size) { 23246bbee8e2SAlan Cox vm_object_page_remove(prev_object, next_pindex, next_pindex + 23256bbee8e2SAlan Cox next_size, 0); 23263364c323SKonstantin Belousov #if 0 2327ef694c1aSEdward Tomasz Napierala if (prev_object->cred != NULL) { 23283364c323SKonstantin Belousov KASSERT(prev_object->charge >= 23293364c323SKonstantin Belousov ptoa(prev_object->size - next_pindex), 23303364c323SKonstantin Belousov ("object %p overcharged 1 %jx %jx", prev_object, 23313364c323SKonstantin Belousov (uintmax_t)next_pindex, (uintmax_t)next_size)); 23323364c323SKonstantin Belousov prev_object->charge -= ptoa(prev_object->size - 23333364c323SKonstantin Belousov next_pindex); 23343364c323SKonstantin Belousov } 23353364c323SKonstantin Belousov #endif 2336ea41812fSAlan Cox } 2337df8bae1dSRodney W. Grimes 2338df8bae1dSRodney W. Grimes /* 2339df8bae1dSRodney W. Grimes * Extend the object if necessary. 2340df8bae1dSRodney W. Grimes */ 2341ea41812fSAlan Cox if (next_pindex + next_size > prev_object->size) 2342ea41812fSAlan Cox prev_object->size = next_pindex + next_size; 2343df8bae1dSRodney W. Grimes 234489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(prev_object); 2345df8bae1dSRodney W. Grimes return (TRUE); 2346df8bae1dSRodney W. Grimes } 2347df8bae1dSRodney W. Grimes 23487a5a6352SMatthew Dillon void 2349180bcaa4SKonstantin Belousov vm_object_set_writeable_dirty_(vm_object_t object) 23507a5a6352SMatthew Dillon { 235167d0e293SJeff Roberson atomic_add_int(&object->generation, 1); 23527a5a6352SMatthew Dillon } 23537a5a6352SMatthew Dillon 2354c23c555bSKonstantin Belousov bool 2355c23c555bSKonstantin Belousov vm_object_mightbedirty_(vm_object_t object) 2356c23c555bSKonstantin Belousov { 2357c23c555bSKonstantin Belousov return (object->generation != object->cleangeneration); 2358c23c555bSKonstantin Belousov } 2359c23c555bSKonstantin Belousov 236003462509SAlan Cox /* 236103462509SAlan Cox * vm_object_unwire: 236203462509SAlan Cox * 236303462509SAlan Cox * For each page offset within the specified range of the given object, 236403462509SAlan Cox * find the highest-level page in the shadow chain and unwire it. A page 236503462509SAlan Cox * must exist at every page offset, and the highest-level page must be 236603462509SAlan Cox * wired. 236703462509SAlan Cox */ 236803462509SAlan Cox void 236903462509SAlan Cox vm_object_unwire(vm_object_t object, vm_ooffset_t offset, vm_size_t length, 237003462509SAlan Cox uint8_t queue) 237103462509SAlan Cox { 237220e4afbfSKonstantin Belousov vm_object_t tobject, t1object; 237303462509SAlan Cox vm_page_t m, tm; 237403462509SAlan Cox vm_pindex_t end_pindex, pindex, tpindex; 237503462509SAlan Cox int depth, locked_depth; 237603462509SAlan Cox 237703462509SAlan Cox KASSERT((offset & PAGE_MASK) == 0, 237803462509SAlan Cox ("vm_object_unwire: offset is not page aligned")); 237903462509SAlan Cox KASSERT((length & PAGE_MASK) == 0, 238003462509SAlan Cox ("vm_object_unwire: length is not a multiple of PAGE_SIZE")); 238103462509SAlan Cox /* The wired count of a fictitious page never changes. */ 238203462509SAlan Cox if ((object->flags & OBJ_FICTITIOUS) != 0) 238303462509SAlan Cox return; 238403462509SAlan Cox pindex = OFF_TO_IDX(offset); 238503462509SAlan Cox end_pindex = pindex + atop(length); 238620e4afbfSKonstantin Belousov again: 238703462509SAlan Cox locked_depth = 1; 238803462509SAlan Cox VM_OBJECT_RLOCK(object); 238903462509SAlan Cox m = vm_page_find_least(object, pindex); 239003462509SAlan Cox while (pindex < end_pindex) { 239103462509SAlan Cox if (m == NULL || pindex < m->pindex) { 239203462509SAlan Cox /* 239303462509SAlan Cox * The first object in the shadow chain doesn't 239403462509SAlan Cox * contain a page at the current index. Therefore, 239503462509SAlan Cox * the page must exist in a backing object. 239603462509SAlan Cox */ 239703462509SAlan Cox tobject = object; 239803462509SAlan Cox tpindex = pindex; 239903462509SAlan Cox depth = 0; 240003462509SAlan Cox do { 240103462509SAlan Cox tpindex += 240203462509SAlan Cox OFF_TO_IDX(tobject->backing_object_offset); 240303462509SAlan Cox tobject = tobject->backing_object; 240403462509SAlan Cox KASSERT(tobject != NULL, 240503462509SAlan Cox ("vm_object_unwire: missing page")); 240603462509SAlan Cox if ((tobject->flags & OBJ_FICTITIOUS) != 0) 240703462509SAlan Cox goto next_page; 240803462509SAlan Cox depth++; 240903462509SAlan Cox if (depth == locked_depth) { 241003462509SAlan Cox locked_depth++; 241103462509SAlan Cox VM_OBJECT_RLOCK(tobject); 241203462509SAlan Cox } 241303462509SAlan Cox } while ((tm = vm_page_lookup(tobject, tpindex)) == 241403462509SAlan Cox NULL); 241503462509SAlan Cox } else { 241603462509SAlan Cox tm = m; 241703462509SAlan Cox m = TAILQ_NEXT(m, listq); 241803462509SAlan Cox } 241963e97555SJeff Roberson if (vm_page_trysbusy(tm) == 0) { 242087e93ea6SMark Johnston for (tobject = object; locked_depth >= 1; 242120e4afbfSKonstantin Belousov locked_depth--) { 242220e4afbfSKonstantin Belousov t1object = tobject->backing_object; 242387e93ea6SMark Johnston if (tm->object != tobject) 242420e4afbfSKonstantin Belousov VM_OBJECT_RUNLOCK(tobject); 242520e4afbfSKonstantin Belousov tobject = t1object; 242620e4afbfSKonstantin Belousov } 242787b64663SMark Johnston tobject = tm->object; 242887b64663SMark Johnston if (!vm_page_busy_sleep(tm, "unwbo", 242987b64663SMark Johnston VM_ALLOC_IGN_SBUSY)) 243087b64663SMark Johnston VM_OBJECT_RUNLOCK(tobject); 243120e4afbfSKonstantin Belousov goto again; 243220e4afbfSKonstantin Belousov } 243303462509SAlan Cox vm_page_unwire(tm, queue); 243463e97555SJeff Roberson vm_page_sunbusy(tm); 243503462509SAlan Cox next_page: 243603462509SAlan Cox pindex++; 243703462509SAlan Cox } 243803462509SAlan Cox /* Release the accumulated object locks. */ 243920e4afbfSKonstantin Belousov for (tobject = object; locked_depth >= 1; locked_depth--) { 244020e4afbfSKonstantin Belousov t1object = tobject->backing_object; 244120e4afbfSKonstantin Belousov VM_OBJECT_RUNLOCK(tobject); 244220e4afbfSKonstantin Belousov tobject = t1object; 244303462509SAlan Cox } 244403462509SAlan Cox } 244503462509SAlan Cox 24460951bd36SEric van Gyzen /* 24470951bd36SEric van Gyzen * Return the vnode for the given object, or NULL if none exists. 24480951bd36SEric van Gyzen * For tmpfs objects, the function may return NULL if there is 24490951bd36SEric van Gyzen * no vnode allocated at the time of the call. 24500951bd36SEric van Gyzen */ 245163e4c6cdSEric van Gyzen struct vnode * 245263e4c6cdSEric van Gyzen vm_object_vnode(vm_object_t object) 245363e4c6cdSEric van Gyzen { 24540951bd36SEric van Gyzen struct vnode *vp; 245563e4c6cdSEric van Gyzen 245663e4c6cdSEric van Gyzen VM_OBJECT_ASSERT_LOCKED(object); 2457a7c198a2SKonstantin Belousov vm_pager_getvp(object, &vp, NULL); 24580951bd36SEric van Gyzen return (vp); 245963e4c6cdSEric van Gyzen } 246063e4c6cdSEric van Gyzen 2461205be21dSJeff Roberson /* 2462205be21dSJeff Roberson * Busy the vm object. This prevents new pages belonging to the object from 2463205be21dSJeff Roberson * becoming busy. Existing pages persist as busy. Callers are responsible 2464205be21dSJeff Roberson * for checking page state before proceeding. 2465205be21dSJeff Roberson */ 2466205be21dSJeff Roberson void 2467205be21dSJeff Roberson vm_object_busy(vm_object_t obj) 2468205be21dSJeff Roberson { 2469205be21dSJeff Roberson 2470205be21dSJeff Roberson VM_OBJECT_ASSERT_LOCKED(obj); 2471205be21dSJeff Roberson 2472c99d0c58SMark Johnston blockcount_acquire(&obj->busy, 1); 2473205be21dSJeff Roberson /* The fence is required to order loads of page busy. */ 2474205be21dSJeff Roberson atomic_thread_fence_acq_rel(); 2475205be21dSJeff Roberson } 2476205be21dSJeff Roberson 2477205be21dSJeff Roberson void 2478205be21dSJeff Roberson vm_object_unbusy(vm_object_t obj) 2479205be21dSJeff Roberson { 2480205be21dSJeff Roberson 2481c99d0c58SMark Johnston blockcount_release(&obj->busy, 1); 2482205be21dSJeff Roberson } 2483205be21dSJeff Roberson 2484205be21dSJeff Roberson void 2485205be21dSJeff Roberson vm_object_busy_wait(vm_object_t obj, const char *wmesg) 2486205be21dSJeff Roberson { 2487205be21dSJeff Roberson 2488205be21dSJeff Roberson VM_OBJECT_ASSERT_UNLOCKED(obj); 2489205be21dSJeff Roberson 2490c99d0c58SMark Johnston (void)blockcount_sleep(&obj->busy, NULL, wmesg, PVM); 2491205be21dSJeff Roberson } 2492205be21dSJeff Roberson 2493cd37afd8SRick Macklem /* 2494cd37afd8SRick Macklem * This function aims to determine if the object is mapped, 2495cd37afd8SRick Macklem * specifically, if it is referenced by a vm_map_entry. Because 2496cd37afd8SRick Macklem * objects occasionally acquire transient references that do not 2497cd37afd8SRick Macklem * represent a mapping, the method used here is inexact. However, it 2498cd37afd8SRick Macklem * has very low overhead and is good enough for the advisory 2499cd37afd8SRick Macklem * vm.vmtotal sysctl. 2500cd37afd8SRick Macklem */ 2501cd37afd8SRick Macklem bool 2502cd37afd8SRick Macklem vm_object_is_active(vm_object_t obj) 2503cd37afd8SRick Macklem { 2504cd37afd8SRick Macklem 25054cf9f5d8SKonstantin Belousov return (obj->ref_count > atomic_load_int(&obj->shadow_count)); 2506cd37afd8SRick Macklem } 2507cd37afd8SRick Macklem 2508ff87ae35SJohn Baldwin static int 250942812cccSKonstantin Belousov vm_object_list_handler(struct sysctl_req *req, bool swap_only) 2510ff87ae35SJohn Baldwin { 25110ecee546SKonstantin Belousov struct kinfo_vmobject *kvo; 2512ff87ae35SJohn Baldwin char *fullpath, *freepath; 2513ff87ae35SJohn Baldwin struct vnode *vp; 2514ff87ae35SJohn Baldwin struct vattr va; 2515ff87ae35SJohn Baldwin vm_object_t obj; 2516ff87ae35SJohn Baldwin vm_page_t m; 2517ecfbddf0SKonstantin Belousov u_long sp; 2518ff87ae35SJohn Baldwin int count, error; 2519*38f5f2a4SKonstantin Belousov bool want_path; 2520ff87ae35SJohn Baldwin 2521ff87ae35SJohn Baldwin if (req->oldptr == NULL) { 2522ff87ae35SJohn Baldwin /* 2523ff87ae35SJohn Baldwin * If an old buffer has not been provided, generate an 2524ff87ae35SJohn Baldwin * estimate of the space needed for a subsequent call. 2525ff87ae35SJohn Baldwin */ 2526ff87ae35SJohn Baldwin mtx_lock(&vm_object_list_mtx); 2527ff87ae35SJohn Baldwin count = 0; 2528ff87ae35SJohn Baldwin TAILQ_FOREACH(obj, &vm_object_list, object_list) { 2529ff87ae35SJohn Baldwin if (obj->type == OBJT_DEAD) 2530ff87ae35SJohn Baldwin continue; 2531ff87ae35SJohn Baldwin count++; 2532ff87ae35SJohn Baldwin } 2533ff87ae35SJohn Baldwin mtx_unlock(&vm_object_list_mtx); 2534ff87ae35SJohn Baldwin return (SYSCTL_OUT(req, NULL, sizeof(struct kinfo_vmobject) * 2535ff87ae35SJohn Baldwin count * 11 / 10)); 2536ff87ae35SJohn Baldwin } 2537ff87ae35SJohn Baldwin 2538*38f5f2a4SKonstantin Belousov want_path = !(swap_only || jailed(curthread->td_ucred)); 2539b050ee6cSMark Johnston kvo = malloc(sizeof(*kvo), M_TEMP, M_WAITOK | M_ZERO); 2540ff87ae35SJohn Baldwin error = 0; 2541ff87ae35SJohn Baldwin 2542ff87ae35SJohn Baldwin /* 2543ff87ae35SJohn Baldwin * VM objects are type stable and are never removed from the 2544ff87ae35SJohn Baldwin * list once added. This allows us to safely read obj->object_list 2545ff87ae35SJohn Baldwin * after reacquiring the VM object lock. 2546ff87ae35SJohn Baldwin */ 2547ff87ae35SJohn Baldwin mtx_lock(&vm_object_list_mtx); 2548ff87ae35SJohn Baldwin TAILQ_FOREACH(obj, &vm_object_list, object_list) { 254942812cccSKonstantin Belousov if (obj->type == OBJT_DEAD || 255042812cccSKonstantin Belousov (swap_only && (obj->flags & (OBJ_ANON | OBJ_SWAP)) == 0)) 2551ff87ae35SJohn Baldwin continue; 2552ff87ae35SJohn Baldwin VM_OBJECT_RLOCK(obj); 255342812cccSKonstantin Belousov if (obj->type == OBJT_DEAD || 255442812cccSKonstantin Belousov (swap_only && (obj->flags & (OBJ_ANON | OBJ_SWAP)) == 0)) { 2555ff87ae35SJohn Baldwin VM_OBJECT_RUNLOCK(obj); 2556ff87ae35SJohn Baldwin continue; 2557ff87ae35SJohn Baldwin } 2558ff87ae35SJohn Baldwin mtx_unlock(&vm_object_list_mtx); 25590ecee546SKonstantin Belousov kvo->kvo_size = ptoa(obj->size); 25600ecee546SKonstantin Belousov kvo->kvo_resident = obj->resident_page_count; 25610ecee546SKonstantin Belousov kvo->kvo_ref_count = obj->ref_count; 25624cf9f5d8SKonstantin Belousov kvo->kvo_shadow_count = atomic_load_int(&obj->shadow_count); 25630ecee546SKonstantin Belousov kvo->kvo_memattr = obj->memattr; 25640ecee546SKonstantin Belousov kvo->kvo_active = 0; 25650ecee546SKonstantin Belousov kvo->kvo_inactive = 0; 25667738118eSKonstantin Belousov if (!swap_only) { 2567ff87ae35SJohn Baldwin TAILQ_FOREACH(m, &obj->memq, listq) { 2568ff87ae35SJohn Baldwin /* 2569ff87ae35SJohn Baldwin * A page may belong to the object but be 2570ff87ae35SJohn Baldwin * dequeued and set to PQ_NONE while the 2571ff87ae35SJohn Baldwin * object lock is not held. This makes the 2572ff87ae35SJohn Baldwin * reads of m->queue below racy, and we do not 2573ff87ae35SJohn Baldwin * count pages set to PQ_NONE. However, this 2574ff87ae35SJohn Baldwin * sysctl is only meant to give an 2575ff87ae35SJohn Baldwin * approximation of the system anyway. 2576ff87ae35SJohn Baldwin */ 25775cff1f4dSMark Johnston if (m->a.queue == PQ_ACTIVE) 25780ecee546SKonstantin Belousov kvo->kvo_active++; 25795cff1f4dSMark Johnston else if (m->a.queue == PQ_INACTIVE) 25800ecee546SKonstantin Belousov kvo->kvo_inactive++; 2581ff87ae35SJohn Baldwin } 25827738118eSKonstantin Belousov } 2583ff87ae35SJohn Baldwin 25840ecee546SKonstantin Belousov kvo->kvo_vn_fileid = 0; 25850ecee546SKonstantin Belousov kvo->kvo_vn_fsid = 0; 25860ecee546SKonstantin Belousov kvo->kvo_vn_fsid_freebsd11 = 0; 2587ff87ae35SJohn Baldwin freepath = NULL; 2588ff87ae35SJohn Baldwin fullpath = ""; 25897738118eSKonstantin Belousov vp = NULL; 2590*38f5f2a4SKonstantin Belousov kvo->kvo_type = vm_object_kvme_type(obj, want_path ? &vp : 2591*38f5f2a4SKonstantin Belousov NULL); 2592ecfbddf0SKonstantin Belousov if (vp != NULL) { 2593ff87ae35SJohn Baldwin vref(vp); 2594ecfbddf0SKonstantin Belousov } else if ((obj->flags & OBJ_ANON) != 0) { 25957f3c78fbSMark Johnston MPASS(kvo->kvo_type == KVME_TYPE_SWAP); 2596ecfbddf0SKonstantin Belousov kvo->kvo_me = (uintptr_t)obj; 2597ecfbddf0SKonstantin Belousov /* tmpfs objs are reported as vnodes */ 2598ecfbddf0SKonstantin Belousov kvo->kvo_backing_obj = (uintptr_t)obj->backing_object; 2599ecfbddf0SKonstantin Belousov sp = swap_pager_swapped_pages(obj); 2600ecfbddf0SKonstantin Belousov kvo->kvo_swapped = sp > UINT32_MAX ? UINT32_MAX : sp; 2601ecfbddf0SKonstantin Belousov } 2602ff87ae35SJohn Baldwin VM_OBJECT_RUNLOCK(obj); 2603ff87ae35SJohn Baldwin if (vp != NULL) { 2604feabaaf9SMateusz Guzik vn_fullpath(vp, &fullpath, &freepath); 2605ff87ae35SJohn Baldwin vn_lock(vp, LK_SHARED | LK_RETRY); 2606ff87ae35SJohn Baldwin if (VOP_GETATTR(vp, &va, curthread->td_ucred) == 0) { 26070ecee546SKonstantin Belousov kvo->kvo_vn_fileid = va.va_fileid; 26080ecee546SKonstantin Belousov kvo->kvo_vn_fsid = va.va_fsid; 26090ecee546SKonstantin Belousov kvo->kvo_vn_fsid_freebsd11 = va.va_fsid; 261069921123SKonstantin Belousov /* truncate */ 2611ff87ae35SJohn Baldwin } 2612ff87ae35SJohn Baldwin vput(vp); 2613ff87ae35SJohn Baldwin } 2614ff87ae35SJohn Baldwin 26150ecee546SKonstantin Belousov strlcpy(kvo->kvo_path, fullpath, sizeof(kvo->kvo_path)); 2616ff87ae35SJohn Baldwin free(freepath, M_TEMP); 2617ff87ae35SJohn Baldwin 2618ff87ae35SJohn Baldwin /* Pack record size down */ 26190ecee546SKonstantin Belousov kvo->kvo_structsize = offsetof(struct kinfo_vmobject, kvo_path) 26200ecee546SKonstantin Belousov + strlen(kvo->kvo_path) + 1; 26210ecee546SKonstantin Belousov kvo->kvo_structsize = roundup(kvo->kvo_structsize, 2622ff87ae35SJohn Baldwin sizeof(uint64_t)); 26230ecee546SKonstantin Belousov error = SYSCTL_OUT(req, kvo, kvo->kvo_structsize); 2624350fc36bSKonstantin Belousov maybe_yield(); 2625ff87ae35SJohn Baldwin mtx_lock(&vm_object_list_mtx); 2626ff87ae35SJohn Baldwin if (error) 2627ff87ae35SJohn Baldwin break; 2628ff87ae35SJohn Baldwin } 2629ff87ae35SJohn Baldwin mtx_unlock(&vm_object_list_mtx); 26300ecee546SKonstantin Belousov free(kvo, M_TEMP); 2631ff87ae35SJohn Baldwin return (error); 2632ff87ae35SJohn Baldwin } 26331b610624SKonstantin Belousov 26341b610624SKonstantin Belousov static int 26351b610624SKonstantin Belousov sysctl_vm_object_list(SYSCTL_HANDLER_ARGS) 26361b610624SKonstantin Belousov { 263742812cccSKonstantin Belousov return (vm_object_list_handler(req, false)); 26381b610624SKonstantin Belousov } 26391b610624SKonstantin Belousov 2640ff87ae35SJohn Baldwin SYSCTL_PROC(_vm, OID_AUTO, objects, CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_SKIP | 2641ff87ae35SJohn Baldwin CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_object_list, "S,kinfo_vmobject", 2642ff87ae35SJohn Baldwin "List of VM objects"); 2643ff87ae35SJohn Baldwin 264442812cccSKonstantin Belousov static int 264542812cccSKonstantin Belousov sysctl_vm_object_list_swap(SYSCTL_HANDLER_ARGS) 264642812cccSKonstantin Belousov { 264742812cccSKonstantin Belousov return (vm_object_list_handler(req, true)); 264842812cccSKonstantin Belousov } 264942812cccSKonstantin Belousov 265042812cccSKonstantin Belousov /* 265142812cccSKonstantin Belousov * This sysctl returns list of the anonymous or swap objects. Intent 265242812cccSKonstantin Belousov * is to provide stripped optimized list useful to analyze swap use. 265342812cccSKonstantin Belousov * Since technically non-swap (default) objects participate in the 265442812cccSKonstantin Belousov * shadow chains, and are converted to swap type as needed by swap 265542812cccSKonstantin Belousov * pager, we must report them. 265642812cccSKonstantin Belousov */ 265742812cccSKonstantin Belousov SYSCTL_PROC(_vm, OID_AUTO, swap_objects, 265842812cccSKonstantin Belousov CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL, 0, 265942812cccSKonstantin Belousov sysctl_vm_object_list_swap, "S,kinfo_vmobject", 266042812cccSKonstantin Belousov "List of swap VM objects"); 266142812cccSKonstantin Belousov 2662c7c34a24SBruce Evans #include "opt_ddb.h" 2663c3cb3e12SDavid Greenman #ifdef DDB 2664c7c34a24SBruce Evans #include <sys/kernel.h> 2665c7c34a24SBruce Evans 2666ce9edcf5SPoul-Henning Kamp #include <sys/cons.h> 2667c7c34a24SBruce Evans 2668c7c34a24SBruce Evans #include <ddb/ddb.h> 2669c7c34a24SBruce Evans 2670cac597e4SBruce Evans static int 26711b40f8c0SMatthew Dillon _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry) 2672a1f6d91cSDavid Greenman { 2673a1f6d91cSDavid Greenman vm_map_t tmpm; 2674a1f6d91cSDavid Greenman vm_map_entry_t tmpe; 2675a1f6d91cSDavid Greenman vm_object_t obj; 2676a1f6d91cSDavid Greenman 2677a1f6d91cSDavid Greenman if (map == 0) 2678a1f6d91cSDavid Greenman return 0; 2679a1f6d91cSDavid Greenman 2680a1f6d91cSDavid Greenman if (entry == 0) { 26812288078cSDoug Moore VM_MAP_ENTRY_FOREACH(tmpe, map) { 2682a1f6d91cSDavid Greenman if (_vm_object_in_map(map, object, tmpe)) { 2683a1f6d91cSDavid Greenman return 1; 2684a1f6d91cSDavid Greenman } 2685a1f6d91cSDavid Greenman } 26869fdfe602SMatthew Dillon } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 26879fdfe602SMatthew Dillon tmpm = entry->object.sub_map; 26882288078cSDoug Moore VM_MAP_ENTRY_FOREACH(tmpe, tmpm) { 2689a1f6d91cSDavid Greenman if (_vm_object_in_map(tmpm, object, tmpe)) { 2690a1f6d91cSDavid Greenman return 1; 2691a1f6d91cSDavid Greenman } 2692a1f6d91cSDavid Greenman } 26938aef1712SMatthew Dillon } else if ((obj = entry->object.vm_object) != NULL) { 269424a1cce3SDavid Greenman for (; obj; obj = obj->backing_object) 2695a1f6d91cSDavid Greenman if (obj == object) { 2696a1f6d91cSDavid Greenman return 1; 2697a1f6d91cSDavid Greenman } 2698a1f6d91cSDavid Greenman } 2699a1f6d91cSDavid Greenman return 0; 2700a1f6d91cSDavid Greenman } 2701a1f6d91cSDavid Greenman 2702cac597e4SBruce Evans static int 27031b40f8c0SMatthew Dillon vm_object_in_map(vm_object_t object) 2704a1f6d91cSDavid Greenman { 2705a1f6d91cSDavid Greenman struct proc *p; 27061005a129SJohn Baldwin 270760517fd1SJohn Baldwin /* sx_slock(&allproc_lock); */ 2708f67af5c9SXin LI FOREACH_PROC_IN_SYSTEM(p) { 2709a1f6d91cSDavid Greenman if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */) 2710a1f6d91cSDavid Greenman continue; 2711553629ebSJake Burkholder if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) { 271260517fd1SJohn Baldwin /* sx_sunlock(&allproc_lock); */ 2713a1f6d91cSDavid Greenman return 1; 2714a1f6d91cSDavid Greenman } 2715553629ebSJake Burkholder } 271660517fd1SJohn Baldwin /* sx_sunlock(&allproc_lock); */ 2717a1f6d91cSDavid Greenman if (_vm_object_in_map(kernel_map, object, 0)) 2718a1f6d91cSDavid Greenman return 1; 2719a1f6d91cSDavid Greenman return 0; 2720a1f6d91cSDavid Greenman } 2721a1f6d91cSDavid Greenman 2722c84c5e00SMitchell Horne DB_SHOW_COMMAND_FLAGS(vmochk, vm_object_check, DB_CMD_MEMSAFE) 2723f708ef1bSPoul-Henning Kamp { 2724a1f6d91cSDavid Greenman vm_object_t object; 2725a1f6d91cSDavid Greenman 2726a1f6d91cSDavid Greenman /* 2727a1f6d91cSDavid Greenman * make sure that internal objs are in a map somewhere 2728a1f6d91cSDavid Greenman * and none have zero ref counts. 2729a1f6d91cSDavid Greenman */ 2730cc64b484SAlfred Perlstein TAILQ_FOREACH(object, &vm_object_list, object_list) { 273132362449SKonstantin Belousov if ((object->flags & OBJ_ANON) != 0) { 2732a1f6d91cSDavid Greenman if (object->ref_count == 0) { 27333efc015bSPeter Wemm db_printf("vmochk: internal obj has zero ref count: %ld\n", 27343efc015bSPeter Wemm (long)object->size); 2735a1f6d91cSDavid Greenman } 2736a1f6d91cSDavid Greenman if (!vm_object_in_map(object)) { 2737fc62ef1fSBruce Evans db_printf( 2738fc62ef1fSBruce Evans "vmochk: internal obj is not in a map: " 2739fc62ef1fSBruce Evans "ref: %d, size: %lu: 0x%lx, backing_object: %p\n", 2740fc62ef1fSBruce Evans object->ref_count, (u_long)object->size, 2741fc62ef1fSBruce Evans (u_long)object->size, 2742fc62ef1fSBruce Evans (void *)object->backing_object); 2743a1f6d91cSDavid Greenman } 2744a1f6d91cSDavid Greenman } 27458cc8c586SEric van Gyzen if (db_pager_quit) 27468cc8c586SEric van Gyzen return; 2747a1f6d91cSDavid Greenman } 2748a1f6d91cSDavid Greenman } 2749a1f6d91cSDavid Greenman 275026f9a767SRodney W. Grimes /* 2751df8bae1dSRodney W. Grimes * vm_object_print: [ debug ] 2752df8bae1dSRodney W. Grimes */ 2753c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static) 2754df8bae1dSRodney W. Grimes { 2755c7c34a24SBruce Evans /* XXX convert args. */ 2756c7c34a24SBruce Evans vm_object_t object = (vm_object_t)addr; 2757c7c34a24SBruce Evans boolean_t full = have_addr; 2758c7c34a24SBruce Evans 2759d031cff1SMatthew Dillon vm_page_t p; 2760df8bae1dSRodney W. Grimes 2761c7c34a24SBruce Evans /* XXX count is an (unused) arg. Avoid shadowing it. */ 2762c7c34a24SBruce Evans #define count was_count 2763c7c34a24SBruce Evans 2764d031cff1SMatthew Dillon int count; 2765df8bae1dSRodney W. Grimes 2766df8bae1dSRodney W. Grimes if (object == NULL) 2767df8bae1dSRodney W. Grimes return; 2768df8bae1dSRodney W. Grimes 2769eb95adefSBruce Evans db_iprintf( 2770ef694c1aSEdward Tomasz Napierala "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x ruid %d charge %jx\n", 2771e47cd172SMaxime Henrion object, (int)object->type, (uintmax_t)object->size, 27723364c323SKonstantin Belousov object->resident_page_count, object->ref_count, object->flags, 2773ef694c1aSEdward Tomasz Napierala object->cred ? object->cred->cr_ruid : -1, (uintmax_t)object->charge); 2774e47cd172SMaxime Henrion db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n", 27754cf9f5d8SKonstantin Belousov atomic_load_int(&object->shadow_count), 2776eb95adefSBruce Evans object->backing_object ? object->backing_object->ref_count : 0, 2777e47cd172SMaxime Henrion object->backing_object, (uintmax_t)object->backing_object_offset); 2778df8bae1dSRodney W. Grimes 2779df8bae1dSRodney W. Grimes if (!full) 2780df8bae1dSRodney W. Grimes return; 2781df8bae1dSRodney W. Grimes 2782c7c34a24SBruce Evans db_indent += 2; 2783df8bae1dSRodney W. Grimes count = 0; 2784fc2ffbe6SPoul-Henning Kamp TAILQ_FOREACH(p, &object->memq, listq) { 2785df8bae1dSRodney W. Grimes if (count == 0) 2786c7c34a24SBruce Evans db_iprintf("memory:="); 2787df8bae1dSRodney W. Grimes else if (count == 6) { 2788c7c34a24SBruce Evans db_printf("\n"); 2789c7c34a24SBruce Evans db_iprintf(" ..."); 2790df8bae1dSRodney W. Grimes count = 0; 2791df8bae1dSRodney W. Grimes } else 2792c7c34a24SBruce Evans db_printf(","); 2793df8bae1dSRodney W. Grimes count++; 2794df8bae1dSRodney W. Grimes 2795e47cd172SMaxime Henrion db_printf("(off=0x%jx,page=0x%jx)", 2796e47cd172SMaxime Henrion (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p)); 27978cc8c586SEric van Gyzen 27988cc8c586SEric van Gyzen if (db_pager_quit) 27998cc8c586SEric van Gyzen break; 2800df8bae1dSRodney W. Grimes } 2801df8bae1dSRodney W. Grimes if (count != 0) 2802c7c34a24SBruce Evans db_printf("\n"); 2803c7c34a24SBruce Evans db_indent -= 2; 2804df8bae1dSRodney W. Grimes } 28055070c7f8SJohn Dyson 2806c7c34a24SBruce Evans /* XXX. */ 2807c7c34a24SBruce Evans #undef count 2808c7c34a24SBruce Evans 2809c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */ 28105070c7f8SJohn Dyson void 28111b40f8c0SMatthew Dillon vm_object_print( 28121b40f8c0SMatthew Dillon /* db_expr_t */ long addr, 28131b40f8c0SMatthew Dillon boolean_t have_addr, 28141b40f8c0SMatthew Dillon /* db_expr_t */ long count, 28151b40f8c0SMatthew Dillon char *modif) 2816c7c34a24SBruce Evans { 2817c7c34a24SBruce Evans vm_object_print_static(addr, have_addr, count, modif); 2818c7c34a24SBruce Evans } 2819c7c34a24SBruce Evans 2820c84c5e00SMitchell Horne DB_SHOW_COMMAND_FLAGS(vmopag, vm_object_print_pages, DB_CMD_MEMSAFE) 28215070c7f8SJohn Dyson { 28225070c7f8SJohn Dyson vm_object_t object; 2823bb2ac86fSKonstantin Belousov vm_pindex_t fidx; 2824bb2ac86fSKonstantin Belousov vm_paddr_t pa; 2825bb2ac86fSKonstantin Belousov vm_page_t m, prev_m; 282618048b6eSStephen J. Kiernan int rcount; 2827cc64b484SAlfred Perlstein 2828cc64b484SAlfred Perlstein TAILQ_FOREACH(object, &vm_object_list, object_list) { 2829fc62ef1fSBruce Evans db_printf("new object: %p\n", (void *)object); 283018048b6eSStephen J. Kiernan if (db_pager_quit) 28315070c7f8SJohn Dyson return; 283218048b6eSStephen J. Kiernan 28335070c7f8SJohn Dyson rcount = 0; 28345070c7f8SJohn Dyson fidx = 0; 2835bb2ac86fSKonstantin Belousov pa = -1; 2836bb2ac86fSKonstantin Belousov TAILQ_FOREACH(m, &object->memq, listq) { 2837bb2ac86fSKonstantin Belousov if ((prev_m = TAILQ_PREV(m, pglist, listq)) != NULL && 2838bb2ac86fSKonstantin Belousov prev_m->pindex + 1 != m->pindex) { 28395070c7f8SJohn Dyson if (rcount) { 28403efc015bSPeter Wemm db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 28413efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 284218048b6eSStephen J. Kiernan if (db_pager_quit) 28435070c7f8SJohn Dyson return; 28445070c7f8SJohn Dyson rcount = 0; 28455070c7f8SJohn Dyson } 28465070c7f8SJohn Dyson } 28475070c7f8SJohn Dyson if (rcount && 28485070c7f8SJohn Dyson (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) { 28495070c7f8SJohn Dyson ++rcount; 28505070c7f8SJohn Dyson continue; 28515070c7f8SJohn Dyson } 28525070c7f8SJohn Dyson if (rcount) { 28532446e4f0SAlan Cox db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 28543efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 285518048b6eSStephen J. Kiernan if (db_pager_quit) 28565070c7f8SJohn Dyson return; 28575070c7f8SJohn Dyson } 2858bb2ac86fSKonstantin Belousov fidx = m->pindex; 28595070c7f8SJohn Dyson pa = VM_PAGE_TO_PHYS(m); 28605070c7f8SJohn Dyson rcount = 1; 28615070c7f8SJohn Dyson } 28625070c7f8SJohn Dyson if (rcount) { 28633efc015bSPeter Wemm db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 28643efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 286518048b6eSStephen J. Kiernan if (db_pager_quit) 28665070c7f8SJohn Dyson return; 28675070c7f8SJohn Dyson } 28685070c7f8SJohn Dyson } 28695070c7f8SJohn Dyson } 2870c3cb3e12SDavid Greenman #endif /* DDB */ 2871