160727d8bSWarner Losh /*- 2796df753SPedro F. Giffuni * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU) 351369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1991, 1993 5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 6df8bae1dSRodney W. Grimes * 7df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 8df8bae1dSRodney W. Grimes * The Mach Operating System project at Carnegie-Mellon University. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 19df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 20df8bae1dSRodney W. Grimes * without specific prior written permission. 21df8bae1dSRodney W. Grimes * 22df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32df8bae1dSRodney W. Grimes * SUCH DAMAGE. 33df8bae1dSRodney W. Grimes * 343c4dd356SDavid Greenman * from: @(#)vm_object.c 8.5 (Berkeley) 3/22/94 35df8bae1dSRodney W. Grimes * 36df8bae1dSRodney W. Grimes * 37df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 38df8bae1dSRodney W. Grimes * All rights reserved. 39df8bae1dSRodney W. Grimes * 40df8bae1dSRodney W. Grimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 41df8bae1dSRodney W. Grimes * 42df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and 43df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright 44df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the 45df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions 46df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 47df8bae1dSRodney W. Grimes * 48df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 49df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 50df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 51df8bae1dSRodney W. Grimes * 52df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to 53df8bae1dSRodney W. Grimes * 54df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 55df8bae1dSRodney W. Grimes * School of Computer Science 56df8bae1dSRodney W. Grimes * Carnegie Mellon University 57df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890 58df8bae1dSRodney W. Grimes * 59df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 60df8bae1dSRodney W. Grimes * rights to redistribute these changes. 61df8bae1dSRodney W. Grimes */ 62df8bae1dSRodney W. Grimes 63df8bae1dSRodney W. Grimes /* 64df8bae1dSRodney W. Grimes * Virtual memory object module. 65df8bae1dSRodney W. Grimes */ 66df8bae1dSRodney W. Grimes 67874651b1SDavid E. O'Brien #include <sys/cdefs.h> 68874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 69874651b1SDavid E. O'Brien 70f8a47341SAlan Cox #include "opt_vm.h" 71f8a47341SAlan Cox 72df8bae1dSRodney W. Grimes #include <sys/param.h> 73df8bae1dSRodney W. Grimes #include <sys/systm.h> 743f289c3fSJeff Roberson #include <sys/cpuset.h> 75fb919e4dSMark Murray #include <sys/lock.h> 76867a482dSJohn Dyson #include <sys/mman.h> 77cf2819ccSJohn Dyson #include <sys/mount.h> 78b9b7a4beSMatthew Dillon #include <sys/kernel.h> 79f425ab8eSKonstantin Belousov #include <sys/pctrie.h> 80b9b7a4beSMatthew Dillon #include <sys/sysctl.h> 811b367556SJason Evans #include <sys/mutex.h> 82fb919e4dSMark Murray #include <sys/proc.h> /* for curproc, pageproc */ 83cf27e0d1SJeff Roberson #include <sys/refcount.h> 84fb919e4dSMark Murray #include <sys/socket.h> 853364c323SKonstantin Belousov #include <sys/resourcevar.h> 86205be21dSJeff Roberson #include <sys/refcount.h> 8789f6b863SAttilio Rao #include <sys/rwlock.h> 88ff87ae35SJohn Baldwin #include <sys/user.h> 89fb919e4dSMark Murray #include <sys/vnode.h> 90fb919e4dSMark Murray #include <sys/vmmeter.h> 911005a129SJohn Baldwin #include <sys/sx.h> 92df8bae1dSRodney W. Grimes 93df8bae1dSRodney W. Grimes #include <vm/vm.h> 94efeaf95aSDavid Greenman #include <vm/vm_param.h> 95efeaf95aSDavid Greenman #include <vm/pmap.h> 96efeaf95aSDavid Greenman #include <vm/vm_map.h> 97efeaf95aSDavid Greenman #include <vm/vm_object.h> 98df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 9926f9a767SRodney W. Grimes #include <vm/vm_pageout.h> 1000d94caffSDavid Greenman #include <vm/vm_pager.h> 101e2068d0bSJeff Roberson #include <vm/vm_phys.h> 102e2068d0bSJeff Roberson #include <vm/vm_pagequeue.h> 10305f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h> 104a1f6d91cSDavid Greenman #include <vm/vm_kern.h> 105efeaf95aSDavid Greenman #include <vm/vm_extern.h> 106774d251dSAttilio Rao #include <vm/vm_radix.h> 107f8a47341SAlan Cox #include <vm/vm_reserv.h> 108670d17b5SJeff Roberson #include <vm/uma.h> 10926f9a767SRodney W. Grimes 110c53f7aceSDag-Erling Smørgrav static int old_msync; 111c53f7aceSDag-Erling Smørgrav SYSCTL_INT(_vm, OID_AUTO, old_msync, CTLFLAG_RW, &old_msync, 0, 112c53f7aceSDag-Erling Smørgrav "Use old (insecure) msync behavior"); 113c53f7aceSDag-Erling Smørgrav 114757216f3SKonstantin Belousov static int vm_object_page_collect_flush(vm_object_t object, vm_page_t p, 11567d0e293SJeff Roberson int pagerflags, int flags, boolean_t *allclean, 116126d6082SKonstantin Belousov boolean_t *eio); 1173280870dSKonstantin Belousov static boolean_t vm_object_page_remove_write(vm_page_t p, int flags, 11867d0e293SJeff Roberson boolean_t *allclean); 119b9b7a4beSMatthew Dillon static void vm_object_qcollapse(vm_object_t object); 12002dd8331SAlan Cox static void vm_object_vndeallocate(vm_object_t object); 12151b867e5SJeff Roberson static void vm_object_backing_remove(vm_object_t object); 122f6b04d2bSDavid Greenman 123df8bae1dSRodney W. Grimes /* 124df8bae1dSRodney W. Grimes * Virtual memory objects maintain the actual data 125df8bae1dSRodney W. Grimes * associated with allocated virtual memory. A given 126df8bae1dSRodney W. Grimes * page of memory exists within exactly one object. 127df8bae1dSRodney W. Grimes * 128df8bae1dSRodney W. Grimes * An object is only deallocated when all "references" 129df8bae1dSRodney W. Grimes * are given up. Only one "reference" to a given 130df8bae1dSRodney W. Grimes * region of an object should be writeable. 131df8bae1dSRodney W. Grimes * 132df8bae1dSRodney W. Grimes * Associated with each object is a list of all resident 133df8bae1dSRodney W. Grimes * memory pages belonging to that object; this list is 134df8bae1dSRodney W. Grimes * maintained by the "vm_page" module, and locked by the object's 135df8bae1dSRodney W. Grimes * lock. 136df8bae1dSRodney W. Grimes * 137df8bae1dSRodney W. Grimes * Each object also records a "pager" routine which is 138df8bae1dSRodney W. Grimes * used to retrieve (and store) pages to the proper backing 139df8bae1dSRodney W. Grimes * storage. In addition, objects may be backed by other 140df8bae1dSRodney W. Grimes * objects from which they were virtual-copied. 141df8bae1dSRodney W. Grimes * 142df8bae1dSRodney W. Grimes * The only items within the object structure which are 143df8bae1dSRodney W. Grimes * modified after time of creation are: 144df8bae1dSRodney W. Grimes * reference count locked by object's lock 145df8bae1dSRodney W. Grimes * pager routine locked by object's lock 146df8bae1dSRodney W. Grimes * 147df8bae1dSRodney W. Grimes */ 148df8bae1dSRodney W. Grimes 14928f8db14SBruce Evans struct object_q vm_object_list; 150a5698387SAlan Cox struct mtx vm_object_list_mtx; /* lock for object list and count */ 151cccf11b8SAlan Cox 152cccf11b8SAlan Cox struct vm_object kernel_object_store; 153df8bae1dSRodney W. Grimes 1546472ac3dSEd Schouten static SYSCTL_NODE(_vm_stats, OID_AUTO, object, CTLFLAG_RD, 0, 1556472ac3dSEd Schouten "VM object stats"); 156604c2bbcSAlan Cox 15711542376SAlan Cox static counter_u64_t object_collapses = EARLY_COUNTER; 15811542376SAlan Cox SYSCTL_COUNTER_U64(_vm_stats_object, OID_AUTO, collapses, CTLFLAG_RD, 15911542376SAlan Cox &object_collapses, 16011542376SAlan Cox "VM object collapses"); 161604c2bbcSAlan Cox 16211542376SAlan Cox static counter_u64_t object_bypasses = EARLY_COUNTER; 16311542376SAlan Cox SYSCTL_COUNTER_U64(_vm_stats_object, OID_AUTO, bypasses, CTLFLAG_RD, 16411542376SAlan Cox &object_bypasses, 16511542376SAlan Cox "VM object bypasses"); 16611542376SAlan Cox 16711542376SAlan Cox static void 16811542376SAlan Cox counter_startup(void) 16911542376SAlan Cox { 17011542376SAlan Cox 17111542376SAlan Cox object_collapses = counter_u64_alloc(M_WAITOK); 17211542376SAlan Cox object_bypasses = counter_u64_alloc(M_WAITOK); 17311542376SAlan Cox } 17411542376SAlan Cox SYSINIT(object_counters, SI_SUB_CPU, SI_ORDER_ANY, counter_startup, NULL); 175dad740e9SAlan Cox 176670d17b5SJeff Roberson static uma_zone_t obj_zone; 1778355f576SJeff Roberson 178b23f72e9SBrian Feldman static int vm_object_zinit(void *mem, int size, int flags); 1798355f576SJeff Roberson 1808355f576SJeff Roberson #ifdef INVARIANTS 1818355f576SJeff Roberson static void vm_object_zdtor(void *mem, int size, void *arg); 1828355f576SJeff Roberson 1838355f576SJeff Roberson static void 1848355f576SJeff Roberson vm_object_zdtor(void *mem, int size, void *arg) 1858355f576SJeff Roberson { 1868355f576SJeff Roberson vm_object_t object; 1878355f576SJeff Roberson 1888355f576SJeff Roberson object = (vm_object_t)mem; 189e735691bSJohn Baldwin KASSERT(object->ref_count == 0, 190e735691bSJohn Baldwin ("object %p ref_count = %d", object, object->ref_count)); 19143186e53SAlan Cox KASSERT(TAILQ_EMPTY(&object->memq), 192198da1b2SAttilio Rao ("object %p has resident pages in its memq", object)); 193774d251dSAttilio Rao KASSERT(vm_radix_is_empty(&object->rtree), 194774d251dSAttilio Rao ("object %p has resident pages in its trie", object)); 195f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 196f8a47341SAlan Cox KASSERT(LIST_EMPTY(&object->rvq), 197f8a47341SAlan Cox ("object %p has reservations", 198f8a47341SAlan Cox object)); 199f8a47341SAlan Cox #endif 20011b57401SHans Petter Selasky KASSERT(REFCOUNT_COUNT(object->paging_in_progress) == 0, 2018355f576SJeff Roberson ("object %p paging_in_progress = %d", 20211b57401SHans Petter Selasky object, REFCOUNT_COUNT(object->paging_in_progress))); 203205be21dSJeff Roberson KASSERT(object->busy == 0, 204205be21dSJeff Roberson ("object %p busy = %d", 205205be21dSJeff Roberson object, object->busy)); 2068355f576SJeff Roberson KASSERT(object->resident_page_count == 0, 2078355f576SJeff Roberson ("object %p resident_page_count = %d", 2088355f576SJeff Roberson object, object->resident_page_count)); 2098355f576SJeff Roberson KASSERT(object->shadow_count == 0, 2108355f576SJeff Roberson ("object %p shadow_count = %d", 2118355f576SJeff Roberson object, object->shadow_count)); 212e735691bSJohn Baldwin KASSERT(object->type == OBJT_DEAD, 213e735691bSJohn Baldwin ("object %p has non-dead type %d", 214e735691bSJohn Baldwin object, object->type)); 2158355f576SJeff Roberson } 2168355f576SJeff Roberson #endif 2178355f576SJeff Roberson 218b23f72e9SBrian Feldman static int 219b23f72e9SBrian Feldman vm_object_zinit(void *mem, int size, int flags) 2208355f576SJeff Roberson { 2218355f576SJeff Roberson vm_object_t object; 2228355f576SJeff Roberson 2238355f576SJeff Roberson object = (vm_object_t)mem; 224777a36c5SAlan Cox rw_init_flags(&object->lock, "vm object", RW_DUPOK | RW_NEW); 2258355f576SJeff Roberson 2268355f576SJeff Roberson /* These are true for any object that has been freed */ 227e735691bSJohn Baldwin object->type = OBJT_DEAD; 228cd1241fbSKonstantin Belousov vm_radix_init(&object->rtree); 22951df5321SJeff Roberson refcount_init(&object->ref_count, 0); 230cf27e0d1SJeff Roberson refcount_init(&object->paging_in_progress, 0); 231205be21dSJeff Roberson refcount_init(&object->busy, 0); 2328355f576SJeff Roberson object->resident_page_count = 0; 2338355f576SJeff Roberson object->shadow_count = 0; 234f425ab8eSKonstantin Belousov object->flags = OBJ_DEAD; 235e735691bSJohn Baldwin 236e735691bSJohn Baldwin mtx_lock(&vm_object_list_mtx); 237e735691bSJohn Baldwin TAILQ_INSERT_TAIL(&vm_object_list, object, object_list); 238e735691bSJohn Baldwin mtx_unlock(&vm_object_list_mtx); 239b23f72e9SBrian Feldman return (0); 2408355f576SJeff Roberson } 241df8bae1dSRodney W. Grimes 242a4915c21SAttilio Rao static void 24363967687SJeff Roberson _vm_object_allocate(objtype_t type, vm_pindex_t size, u_short flags, 244*67388836SKonstantin Belousov vm_object_t object, void *handle) 245df8bae1dSRodney W. Grimes { 2460cddd8f0SMatthew Dillon 247df8bae1dSRodney W. Grimes TAILQ_INIT(&object->memq); 2481c500307SAlan Cox LIST_INIT(&object->shadow_head); 249a1f6d91cSDavid Greenman 25024a1cce3SDavid Greenman object->type = type; 251f425ab8eSKonstantin Belousov if (type == OBJT_SWAP) 252f425ab8eSKonstantin Belousov pctrie_init(&object->un_pager.swp.swp_blks); 253f425ab8eSKonstantin Belousov 254f425ab8eSKonstantin Belousov /* 255f425ab8eSKonstantin Belousov * Ensure that swap_pager_swapoff() iteration over object_list 256f425ab8eSKonstantin Belousov * sees up to date type and pctrie head if it observed 257f425ab8eSKonstantin Belousov * non-dead object. 258f425ab8eSKonstantin Belousov */ 259f425ab8eSKonstantin Belousov atomic_thread_fence_rel(); 260f425ab8eSKonstantin Belousov 26163967687SJeff Roberson object->pg_color = 0; 26263967687SJeff Roberson object->flags = flags; 263df8bae1dSRodney W. Grimes object->size = size; 2644c29d2deSMark Johnston object->domain.dr_policy = NULL; 265b881da26SAlan Cox object->generation = 1; 26667d0e293SJeff Roberson object->cleangeneration = 1; 26751df5321SJeff Roberson refcount_init(&object->ref_count, 1); 2683153e878SAlan Cox object->memattr = VM_MEMATTR_DEFAULT; 269ef694c1aSEdward Tomasz Napierala object->cred = NULL; 2703364c323SKonstantin Belousov object->charge = 0; 271*67388836SKonstantin Belousov object->handle = handle; 27224a1cce3SDavid Greenman object->backing_object = NULL; 273a316d390SJohn Dyson object->backing_object_offset = (vm_ooffset_t) 0; 274f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 275f8a47341SAlan Cox LIST_INIT(&object->rvq); 276f8a47341SAlan Cox #endif 2771bdbd705SKonstantin Belousov umtx_shm_object_init(object); 278df8bae1dSRodney W. Grimes } 279df8bae1dSRodney W. Grimes 280df8bae1dSRodney W. Grimes /* 28126f9a767SRodney W. Grimes * vm_object_init: 28226f9a767SRodney W. Grimes * 28326f9a767SRodney W. Grimes * Initialize the VM objects module. 28426f9a767SRodney W. Grimes */ 28526f9a767SRodney W. Grimes void 2861b40f8c0SMatthew Dillon vm_object_init(void) 28726f9a767SRodney W. Grimes { 28826f9a767SRodney W. Grimes TAILQ_INIT(&vm_object_list); 2896008862bSJohn Baldwin mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF); 2900217125fSDavid Greenman 29189f6b863SAttilio Rao rw_init(&kernel_object->lock, "kernel vm object"); 292d1780e8dSKonstantin Belousov _vm_object_allocate(OBJT_PHYS, atop(VM_MAX_KERNEL_ADDRESS - 293*67388836SKonstantin Belousov VM_MIN_KERNEL_ADDRESS), OBJ_UNMANAGED, kernel_object, NULL); 294f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 295f8a47341SAlan Cox kernel_object->flags |= OBJ_COLORED; 296f8a47341SAlan Cox kernel_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS); 297f8a47341SAlan Cox #endif 29826f9a767SRodney W. Grimes 2998dbca793STor Egge /* 3008dbca793STor Egge * The lock portion of struct vm_object must be type stable due 3018dbca793STor Egge * to vm_pageout_fallback_object_lock locking a vm object 3028dbca793STor Egge * without holding any references to it. 3038dbca793STor Egge */ 3048355f576SJeff Roberson obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL, 3058355f576SJeff Roberson #ifdef INVARIANTS 3068355f576SJeff Roberson vm_object_zdtor, 3078355f576SJeff Roberson #else 3088355f576SJeff Roberson NULL, 3098355f576SJeff Roberson #endif 3105df87b21SJeff Roberson vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 311774d251dSAttilio Rao 312cd1241fbSKonstantin Belousov vm_radix_zinit(); 31399448ed1SJohn Dyson } 31499448ed1SJohn Dyson 31599448ed1SJohn Dyson void 3161b40f8c0SMatthew Dillon vm_object_clear_flag(vm_object_t object, u_short bits) 3171b40f8c0SMatthew Dillon { 3185440b5a9SAlan Cox 31989f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 320b06805adSJake Burkholder object->flags &= ~bits; 3211b40f8c0SMatthew Dillon } 3221b40f8c0SMatthew Dillon 3233153e878SAlan Cox /* 3243153e878SAlan Cox * Sets the default memory attribute for the specified object. Pages 3253153e878SAlan Cox * that are allocated to this object are by default assigned this memory 3263153e878SAlan Cox * attribute. 3273153e878SAlan Cox * 3283153e878SAlan Cox * Presently, this function must be called before any pages are allocated 3293153e878SAlan Cox * to the object. In the future, this requirement may be relaxed for 3303153e878SAlan Cox * "default" and "swap" objects. 3313153e878SAlan Cox */ 3323153e878SAlan Cox int 3333153e878SAlan Cox vm_object_set_memattr(vm_object_t object, vm_memattr_t memattr) 3343153e878SAlan Cox { 3353153e878SAlan Cox 33689f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 3373153e878SAlan Cox switch (object->type) { 3383153e878SAlan Cox case OBJT_DEFAULT: 3393153e878SAlan Cox case OBJT_DEVICE: 34096b0b92aSAlan Cox case OBJT_MGTDEVICE: 3413153e878SAlan Cox case OBJT_PHYS: 34201381811SJohn Baldwin case OBJT_SG: 3433153e878SAlan Cox case OBJT_SWAP: 3443153e878SAlan Cox case OBJT_VNODE: 3453153e878SAlan Cox if (!TAILQ_EMPTY(&object->memq)) 3463153e878SAlan Cox return (KERN_FAILURE); 3473153e878SAlan Cox break; 3483153e878SAlan Cox case OBJT_DEAD: 3493153e878SAlan Cox return (KERN_INVALID_ARGUMENT); 35096b0b92aSAlan Cox default: 35196b0b92aSAlan Cox panic("vm_object_set_memattr: object %p is of undefined type", 35296b0b92aSAlan Cox object); 3533153e878SAlan Cox } 3543153e878SAlan Cox object->memattr = memattr; 3553153e878SAlan Cox return (KERN_SUCCESS); 3563153e878SAlan Cox } 3573153e878SAlan Cox 3581b40f8c0SMatthew Dillon void 3591b40f8c0SMatthew Dillon vm_object_pip_add(vm_object_t object, short i) 3601b40f8c0SMatthew Dillon { 361f279b88dSAlan Cox 362cf27e0d1SJeff Roberson refcount_acquiren(&object->paging_in_progress, i); 3631b40f8c0SMatthew Dillon } 3641b40f8c0SMatthew Dillon 3651b40f8c0SMatthew Dillon void 3661b40f8c0SMatthew Dillon vm_object_pip_wakeup(vm_object_t object) 3671b40f8c0SMatthew Dillon { 368f279b88dSAlan Cox 369cf27e0d1SJeff Roberson refcount_release(&object->paging_in_progress); 3701b40f8c0SMatthew Dillon } 3711b40f8c0SMatthew Dillon 3721b40f8c0SMatthew Dillon void 3731b40f8c0SMatthew Dillon vm_object_pip_wakeupn(vm_object_t object, short i) 3741b40f8c0SMatthew Dillon { 375d647a0edSAlan Cox 376cf27e0d1SJeff Roberson refcount_releasen(&object->paging_in_progress, i); 3771b40f8c0SMatthew Dillon } 3781b40f8c0SMatthew Dillon 3791b40f8c0SMatthew Dillon void 3801b40f8c0SMatthew Dillon vm_object_pip_wait(vm_object_t object, char *waitid) 3811b40f8c0SMatthew Dillon { 3821ca58953SAlan Cox 38389f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 384cf27e0d1SJeff Roberson 38511b57401SHans Petter Selasky while (REFCOUNT_COUNT(object->paging_in_progress) > 0) { 386cf27e0d1SJeff Roberson VM_OBJECT_WUNLOCK(object); 387cf27e0d1SJeff Roberson refcount_wait(&object->paging_in_progress, waitid, PVM); 388cf27e0d1SJeff Roberson VM_OBJECT_WLOCK(object); 3891ca58953SAlan Cox } 3901b40f8c0SMatthew Dillon } 3911b40f8c0SMatthew Dillon 392cf27e0d1SJeff Roberson void 393cf27e0d1SJeff Roberson vm_object_pip_wait_unlocked(vm_object_t object, char *waitid) 394cf27e0d1SJeff Roberson { 395cf27e0d1SJeff Roberson 396cf27e0d1SJeff Roberson VM_OBJECT_ASSERT_UNLOCKED(object); 397cf27e0d1SJeff Roberson 39811b57401SHans Petter Selasky while (REFCOUNT_COUNT(object->paging_in_progress) > 0) 399cf27e0d1SJeff Roberson refcount_wait(&object->paging_in_progress, waitid, PVM); 400cf27e0d1SJeff Roberson } 401cf27e0d1SJeff Roberson 40226f9a767SRodney W. Grimes /* 40326f9a767SRodney W. Grimes * vm_object_allocate: 40426f9a767SRodney W. Grimes * 40526f9a767SRodney W. Grimes * Returns a new object with the given size. 40626f9a767SRodney W. Grimes */ 40726f9a767SRodney W. Grimes vm_object_t 4086395da54SIan Dowse vm_object_allocate(objtype_t type, vm_pindex_t size) 40926f9a767SRodney W. Grimes { 41090688d13SAlan Cox vm_object_t object; 41163967687SJeff Roberson u_short flags; 41263967687SJeff Roberson 41363967687SJeff Roberson switch (type) { 41463967687SJeff Roberson case OBJT_DEAD: 41563967687SJeff Roberson panic("vm_object_allocate: can't create OBJT_DEAD"); 41663967687SJeff Roberson case OBJT_DEFAULT: 41763967687SJeff Roberson case OBJT_SWAP: 41863967687SJeff Roberson flags = OBJ_COLORED; 41963967687SJeff Roberson break; 42063967687SJeff Roberson case OBJT_DEVICE: 42163967687SJeff Roberson case OBJT_SG: 42263967687SJeff Roberson flags = OBJ_FICTITIOUS | OBJ_UNMANAGED; 42363967687SJeff Roberson break; 42463967687SJeff Roberson case OBJT_MGTDEVICE: 42563967687SJeff Roberson flags = OBJ_FICTITIOUS; 42663967687SJeff Roberson break; 42763967687SJeff Roberson case OBJT_PHYS: 42863967687SJeff Roberson flags = OBJ_UNMANAGED; 42963967687SJeff Roberson break; 43063967687SJeff Roberson case OBJT_VNODE: 43163967687SJeff Roberson flags = 0; 43263967687SJeff Roberson break; 43363967687SJeff Roberson default: 43463967687SJeff Roberson panic("vm_object_allocate: type %d is undefined", type); 43563967687SJeff Roberson } 43663967687SJeff Roberson object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK); 437*67388836SKonstantin Belousov _vm_object_allocate(type, size, flags, object, NULL); 43863967687SJeff Roberson 43963967687SJeff Roberson return (object); 44063967687SJeff Roberson } 44163967687SJeff Roberson 44263967687SJeff Roberson /* 44363967687SJeff Roberson * vm_object_allocate_anon: 44463967687SJeff Roberson * 44563967687SJeff Roberson * Returns a new default object of the given size and marked as 44663967687SJeff Roberson * anonymous memory for special split/collapse handling. Color 44763967687SJeff Roberson * to be initialized by the caller. 44863967687SJeff Roberson */ 44963967687SJeff Roberson vm_object_t 450*67388836SKonstantin Belousov vm_object_allocate_anon(vm_pindex_t size, vm_object_t backing_object, 451*67388836SKonstantin Belousov struct ucred *cred, vm_size_t charge) 45263967687SJeff Roberson { 453*67388836SKonstantin Belousov vm_object_t handle, object; 45490688d13SAlan Cox 455*67388836SKonstantin Belousov if (backing_object == NULL) 456*67388836SKonstantin Belousov handle = NULL; 457*67388836SKonstantin Belousov else if ((backing_object->flags & OBJ_ANON) != 0) 458*67388836SKonstantin Belousov handle = backing_object->handle; 459*67388836SKonstantin Belousov else 460*67388836SKonstantin Belousov handle = backing_object; 461*67388836SKonstantin Belousov object = uma_zalloc(obj_zone, M_WAITOK); 46263967687SJeff Roberson _vm_object_allocate(OBJT_DEFAULT, size, OBJ_ANON | OBJ_ONEMAPPING, 463*67388836SKonstantin Belousov object, handle); 464*67388836SKonstantin Belousov object->cred = cred; 465*67388836SKonstantin Belousov object->charge = cred != NULL ? charge : 0; 46690688d13SAlan Cox return (object); 46726f9a767SRodney W. Grimes } 46826f9a767SRodney W. Grimes 46926f9a767SRodney W. Grimes 47026f9a767SRodney W. Grimes /* 471df8bae1dSRodney W. Grimes * vm_object_reference: 472df8bae1dSRodney W. Grimes * 47315347817SAlan Cox * Gets another reference to the given object. Note: OBJ_DEAD 47415347817SAlan Cox * objects can be referenced during final cleaning. 475df8bae1dSRodney W. Grimes */ 4766476c0d2SJohn Dyson void 4771b40f8c0SMatthew Dillon vm_object_reference(vm_object_t object) 478df8bae1dSRodney W. Grimes { 479a67d5408SJeff Roberson struct vnode *vp; 480a67d5408SJeff Roberson u_int old; 481a67d5408SJeff Roberson 482df8bae1dSRodney W. Grimes if (object == NULL) 483df8bae1dSRodney W. Grimes return; 484a67d5408SJeff Roberson 485a67d5408SJeff Roberson /* 486a67d5408SJeff Roberson * Many places assume exclusive access to objects with a single 487a67d5408SJeff Roberson * ref. vm_object_collapse() in particular will directly mainpulate 488a67d5408SJeff Roberson * references for objects in this state. vnode objects only need 489a67d5408SJeff Roberson * the lock for the first ref to reference the vnode. 490a67d5408SJeff Roberson */ 491a67d5408SJeff Roberson if (!refcount_acquire_if_gt(&object->ref_count, 492a67d5408SJeff Roberson object->type == OBJT_VNODE ? 0 : 1)) { 49351df5321SJeff Roberson VM_OBJECT_RLOCK(object); 494a67d5408SJeff Roberson old = refcount_acquire(&object->ref_count); 495a67d5408SJeff Roberson if (object->type == OBJT_VNODE && old == 0) { 496a67d5408SJeff Roberson vp = object->handle; 497a67d5408SJeff Roberson vref(vp); 498a67d5408SJeff Roberson } 49951df5321SJeff Roberson VM_OBJECT_RUNLOCK(object); 50095e5e988SJohn Dyson } 501a67d5408SJeff Roberson } 50295e5e988SJohn Dyson 50323955314SAlfred Perlstein /* 504b921a12bSAlan Cox * vm_object_reference_locked: 505b921a12bSAlan Cox * 506b921a12bSAlan Cox * Gets another reference to the given object. 507b921a12bSAlan Cox * 508b921a12bSAlan Cox * The object must be locked. 509b921a12bSAlan Cox */ 510b921a12bSAlan Cox void 511b921a12bSAlan Cox vm_object_reference_locked(vm_object_t object) 512b921a12bSAlan Cox { 513b921a12bSAlan Cox struct vnode *vp; 514a67d5408SJeff Roberson u_int old; 515b921a12bSAlan Cox 51651df5321SJeff Roberson VM_OBJECT_ASSERT_LOCKED(object); 517a67d5408SJeff Roberson old = refcount_acquire(&object->ref_count); 518a67d5408SJeff Roberson if (object->type == OBJT_VNODE && old == 0) { 519b921a12bSAlan Cox vp = object->handle; 520b921a12bSAlan Cox vref(vp); 521b921a12bSAlan Cox } 522b921a12bSAlan Cox } 523b921a12bSAlan Cox 524b921a12bSAlan Cox /* 5259d5abbddSJens Schweikhardt * Handle deallocating an object of type OBJT_VNODE. 52623955314SAlfred Perlstein */ 52702dd8331SAlan Cox static void 5281b40f8c0SMatthew Dillon vm_object_vndeallocate(vm_object_t object) 52995e5e988SJohn Dyson { 53095e5e988SJohn Dyson struct vnode *vp = (struct vnode *) object->handle; 53126c4e983SJeff Roberson bool last; 532219cbf59SEivind Eklund 5335526d2d9SEivind Eklund KASSERT(object->type == OBJT_VNODE, 5345526d2d9SEivind Eklund ("vm_object_vndeallocate: not a vnode object")); 535219cbf59SEivind Eklund KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp")); 53695e5e988SJohn Dyson 53726c4e983SJeff Roberson /* Object lock to protect handle lookup. */ 53826c4e983SJeff Roberson last = refcount_release(&object->ref_count); 53926c4e983SJeff Roberson VM_OBJECT_RUNLOCK(object); 54026c4e983SJeff Roberson 54126c4e983SJeff Roberson if (!last) 54226c4e983SJeff Roberson return; 54326c4e983SJeff Roberson 544a67d5408SJeff Roberson if (!umtx_shm_vnobj_persistent) 5451bdbd705SKonstantin Belousov umtx_shm_object_terminated(object); 5461bdbd705SKonstantin Belousov 54703fa5b34SKonstantin Belousov /* vrele may need the vnode lock. */ 54847221757SJohn Dyson vrele(vp); 54986769ac0SKonstantin Belousov } 550df8bae1dSRodney W. Grimes 551df8bae1dSRodney W. Grimes /* 552df8bae1dSRodney W. Grimes * vm_object_deallocate: 553df8bae1dSRodney W. Grimes * 554df8bae1dSRodney W. Grimes * Release a reference to the specified object, 555df8bae1dSRodney W. Grimes * gained either through a vm_object_allocate 556df8bae1dSRodney W. Grimes * or a vm_object_reference call. When all references 557df8bae1dSRodney W. Grimes * are gone, storage associated with this object 558df8bae1dSRodney W. Grimes * may be relinquished. 559df8bae1dSRodney W. Grimes * 560df8bae1dSRodney W. Grimes * No object may be locked. 561df8bae1dSRodney W. Grimes */ 56226f9a767SRodney W. Grimes void 5631b40f8c0SMatthew Dillon vm_object_deallocate(vm_object_t object) 564df8bae1dSRodney W. Grimes { 56532362449SKonstantin Belousov vm_object_t robject, temp; 56626c4e983SJeff Roberson bool released; 567df8bae1dSRodney W. Grimes 568df8bae1dSRodney W. Grimes while (object != NULL) { 56951df5321SJeff Roberson /* 57051df5321SJeff Roberson * If the reference count goes to 0 we start calling 57151df5321SJeff Roberson * vm_object_terminate() on the object chain. A ref count 57251df5321SJeff Roberson * of 1 may be a special case depending on the shadow count 57351df5321SJeff Roberson * being 0 or 1. These cases require a write lock on the 57451df5321SJeff Roberson * object. 57551df5321SJeff Roberson */ 57663967687SJeff Roberson if ((object->flags & OBJ_ANON) == 0) 57763967687SJeff Roberson released = refcount_release_if_gt(&object->ref_count, 1); 57863967687SJeff Roberson else 57951df5321SJeff Roberson released = refcount_release_if_gt(&object->ref_count, 2); 58051df5321SJeff Roberson if (released) 58151df5321SJeff Roberson return; 58251df5321SJeff Roberson 583a67d5408SJeff Roberson if (object->type == OBJT_VNODE) { 58426c4e983SJeff Roberson VM_OBJECT_RLOCK(object); 58526c4e983SJeff Roberson if (object->type == OBJT_VNODE) { 586a67d5408SJeff Roberson vm_object_vndeallocate(object); 587a67d5408SJeff Roberson return; 588a67d5408SJeff Roberson } 58926c4e983SJeff Roberson VM_OBJECT_RUNLOCK(object); 59026c4e983SJeff Roberson } 59126c4e983SJeff Roberson 59226c4e983SJeff Roberson VM_OBJECT_WLOCK(object); 59326c4e983SJeff Roberson KASSERT(object->ref_count > 0, 59426c4e983SJeff Roberson ("vm_object_deallocate: object deallocated too many times: %d", 59526c4e983SJeff Roberson object->type)); 59626c4e983SJeff Roberson 59726c4e983SJeff Roberson if (refcount_release(&object->ref_count)) 59826c4e983SJeff Roberson goto doterm; 5998125b1e6SAlfred Perlstein if (object->ref_count > 1) { 60089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 60123b186d3SAlan Cox return; 6028125b1e6SAlfred Perlstein } else if (object->ref_count == 1) { 6034c8e0452SAlan Cox if (object->shadow_count == 0 && 60463967687SJeff Roberson (object->flags & OBJ_ANON) != 0) { 6058125b1e6SAlfred Perlstein vm_object_set_flag(object, OBJ_ONEMAPPING); 60632362449SKonstantin Belousov } else if (object->shadow_count == 1) { 60732362449SKonstantin Belousov KASSERT((object->flags & OBJ_ANON) != 0, 60832362449SKonstantin Belousov ("obj %p with shadow_count > 0 is not anon", 60932362449SKonstantin Belousov object)); 6101c500307SAlan Cox robject = LIST_FIRST(&object->shadow_head); 6115526d2d9SEivind Eklund KASSERT(robject != NULL, 61232362449SKonstantin Belousov ("vm_object_deallocate: ref_count: %d, " 61332362449SKonstantin Belousov "shadow_count: %d", object->ref_count, 6145526d2d9SEivind Eklund object->shadow_count)); 6154bace8e7SKonstantin Belousov KASSERT((robject->flags & OBJ_TMPFS_NODE) == 0, 6164bace8e7SKonstantin Belousov ("shadowed tmpfs v_object %p", object)); 61789f6b863SAttilio Rao if (!VM_OBJECT_TRYWLOCK(robject)) { 618b72b0115SAlan Cox /* 619b72b0115SAlan Cox * Avoid a potential deadlock. 620b72b0115SAlan Cox */ 62151df5321SJeff Roberson refcount_acquire(&object->ref_count); 62289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 623a7d86121SAlan Cox /* 624a7d86121SAlan Cox * More likely than not the thread 625a7d86121SAlan Cox * holding robject's lock has lower 626a7d86121SAlan Cox * priority than the current thread. 627a7d86121SAlan Cox * Let the lower priority thread run. 628a7d86121SAlan Cox */ 6298db5fc58SJohn Baldwin pause("vmo_de", 1); 630b72b0115SAlan Cox continue; 631b72b0115SAlan Cox } 632d936694fSAlan Cox /* 633d936694fSAlan Cox * Collapse object into its shadow unless its 634d936694fSAlan Cox * shadow is dead. In that case, object will 635d936694fSAlan Cox * be deallocated by the thread that is 636d936694fSAlan Cox * deallocating its shadow. 637d936694fSAlan Cox */ 63863967687SJeff Roberson if ((robject->flags & 63932362449SKonstantin Belousov (OBJ_DEAD | OBJ_ANON)) == OBJ_ANON) { 640a1f6d91cSDavid Greenman 64151df5321SJeff Roberson refcount_acquire(&robject->ref_count); 642138449dcSAlan Cox retry: 64311b57401SHans Petter Selasky if (REFCOUNT_COUNT(robject->paging_in_progress) > 0) { 64489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 645138449dcSAlan Cox vm_object_pip_wait(robject, 646138449dcSAlan Cox "objde1"); 6472e9f4a69SAlan Cox temp = robject->backing_object; 6482e9f4a69SAlan Cox if (object == temp) { 64989f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 650138449dcSAlan Cox goto retry; 6512e9f4a69SAlan Cox } 65211b57401SHans Petter Selasky } else if (REFCOUNT_COUNT(object->paging_in_progress) > 0) { 65389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(robject); 654cf27e0d1SJeff Roberson VM_OBJECT_WUNLOCK(object); 655cf27e0d1SJeff Roberson refcount_wait( 656cf27e0d1SJeff Roberson &object->paging_in_progress, 657cf27e0d1SJeff Roberson "objde2", PVM); 65889f6b863SAttilio Rao VM_OBJECT_WLOCK(robject); 6592e9f4a69SAlan Cox temp = robject->backing_object; 6602e9f4a69SAlan Cox if (object == temp) { 66189f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 662138449dcSAlan Cox goto retry; 663a1f6d91cSDavid Greenman } 6642e9f4a69SAlan Cox } else 66589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 6662e9f4a69SAlan Cox 66795e5e988SJohn Dyson if (robject->ref_count == 1) { 668a67d5408SJeff Roberson refcount_release(&robject->ref_count); 669ba8da839SDavid Greenman object = robject; 67095e5e988SJohn Dyson goto doterm; 67195e5e988SJohn Dyson } 67295e5e988SJohn Dyson object = robject; 67395e5e988SJohn Dyson vm_object_collapse(object); 67489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 675ba8da839SDavid Greenman continue; 676a1f6d91cSDavid Greenman } 67789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(robject); 67895e5e988SJohn Dyson } 67989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 68023b186d3SAlan Cox return; 68195e5e988SJohn Dyson } 68295e5e988SJohn Dyson doterm: 6831bdbd705SKonstantin Belousov umtx_shm_object_terminated(object); 68424a1cce3SDavid Greenman temp = object->backing_object; 685c9917419SAlan Cox if (temp != NULL) { 6864bace8e7SKonstantin Belousov KASSERT((object->flags & OBJ_TMPFS_NODE) == 0, 6874bace8e7SKonstantin Belousov ("shadowed tmpfs v_object 2 %p", object)); 68851b867e5SJeff Roberson vm_object_backing_remove(object); 689de5f6a77SJohn Dyson } 690245df27cSMatthew Dillon /* 691245df27cSMatthew Dillon * Don't double-terminate, we could be in a termination 692245df27cSMatthew Dillon * recursion due to the terminate having to sync data 693245df27cSMatthew Dillon * to disk. 694245df27cSMatthew Dillon */ 695783a68aaSKonstantin Belousov if ((object->flags & OBJ_DEAD) == 0) { 696783a68aaSKonstantin Belousov vm_object_set_flag(object, OBJ_DEAD); 697df8bae1dSRodney W. Grimes vm_object_terminate(object); 698783a68aaSKonstantin Belousov } else 69989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 700df8bae1dSRodney W. Grimes object = temp; 701df8bae1dSRodney W. Grimes } 702df8bae1dSRodney W. Grimes } 703df8bae1dSRodney W. Grimes 704df8bae1dSRodney W. Grimes /* 7052ac78f0eSStephan Uphoff * vm_object_destroy removes the object from the global object list 7062ac78f0eSStephan Uphoff * and frees the space for the object. 7072ac78f0eSStephan Uphoff */ 7082ac78f0eSStephan Uphoff void 7092ac78f0eSStephan Uphoff vm_object_destroy(vm_object_t object) 7102ac78f0eSStephan Uphoff { 7112ac78f0eSStephan Uphoff 7122ac78f0eSStephan Uphoff /* 7133364c323SKonstantin Belousov * Release the allocation charge. 7143364c323SKonstantin Belousov */ 715ef694c1aSEdward Tomasz Napierala if (object->cred != NULL) { 716ef694c1aSEdward Tomasz Napierala swap_release_by_cred(object->charge, object->cred); 7173364c323SKonstantin Belousov object->charge = 0; 718ef694c1aSEdward Tomasz Napierala crfree(object->cred); 719ef694c1aSEdward Tomasz Napierala object->cred = NULL; 7203364c323SKonstantin Belousov } 7213364c323SKonstantin Belousov 7223364c323SKonstantin Belousov /* 7232ac78f0eSStephan Uphoff * Free the space for the object. 7242ac78f0eSStephan Uphoff */ 7252ac78f0eSStephan Uphoff uma_zfree(obj_zone, object); 7262ac78f0eSStephan Uphoff } 7272ac78f0eSStephan Uphoff 72851b867e5SJeff Roberson static void 72951b867e5SJeff Roberson vm_object_backing_remove_locked(vm_object_t object) 73051b867e5SJeff Roberson { 73151b867e5SJeff Roberson vm_object_t backing_object; 73251b867e5SJeff Roberson 73351b867e5SJeff Roberson backing_object = object->backing_object; 73451b867e5SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 73551b867e5SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(backing_object); 73651b867e5SJeff Roberson 73751b867e5SJeff Roberson if ((object->flags & OBJ_SHADOWLIST) != 0) { 73851b867e5SJeff Roberson LIST_REMOVE(object, shadow_list); 73951b867e5SJeff Roberson backing_object->shadow_count--; 74051b867e5SJeff Roberson object->flags &= ~OBJ_SHADOWLIST; 74151b867e5SJeff Roberson } 74251b867e5SJeff Roberson object->backing_object = NULL; 74351b867e5SJeff Roberson } 74451b867e5SJeff Roberson 74551b867e5SJeff Roberson static void 74651b867e5SJeff Roberson vm_object_backing_remove(vm_object_t object) 74751b867e5SJeff Roberson { 74851b867e5SJeff Roberson vm_object_t backing_object; 74951b867e5SJeff Roberson 75051b867e5SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 75151b867e5SJeff Roberson 75251b867e5SJeff Roberson if ((object->flags & OBJ_SHADOWLIST) != 0) { 75351b867e5SJeff Roberson backing_object = object->backing_object; 75451b867e5SJeff Roberson VM_OBJECT_WLOCK(backing_object); 75551b867e5SJeff Roberson vm_object_backing_remove_locked(object); 75651b867e5SJeff Roberson VM_OBJECT_WUNLOCK(backing_object); 75751b867e5SJeff Roberson } else 75851b867e5SJeff Roberson object->backing_object = NULL; 75951b867e5SJeff Roberson } 76051b867e5SJeff Roberson 76151b867e5SJeff Roberson static void 76251b867e5SJeff Roberson vm_object_backing_insert_locked(vm_object_t object, vm_object_t backing_object) 76351b867e5SJeff Roberson { 76451b867e5SJeff Roberson 76551b867e5SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 76651b867e5SJeff Roberson 76751b867e5SJeff Roberson if ((backing_object->flags & OBJ_ANON) != 0) { 76851b867e5SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(backing_object); 76951b867e5SJeff Roberson LIST_INSERT_HEAD(&backing_object->shadow_head, object, 77051b867e5SJeff Roberson shadow_list); 77151b867e5SJeff Roberson backing_object->shadow_count++; 77251b867e5SJeff Roberson object->flags |= OBJ_SHADOWLIST; 77351b867e5SJeff Roberson } 77451b867e5SJeff Roberson object->backing_object = backing_object; 77551b867e5SJeff Roberson } 77651b867e5SJeff Roberson 77751b867e5SJeff Roberson static void 77851b867e5SJeff Roberson vm_object_backing_insert(vm_object_t object, vm_object_t backing_object) 77951b867e5SJeff Roberson { 78051b867e5SJeff Roberson 78151b867e5SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 78251b867e5SJeff Roberson 78351b867e5SJeff Roberson if ((backing_object->flags & OBJ_ANON) != 0) { 78451b867e5SJeff Roberson VM_OBJECT_WLOCK(backing_object); 78551b867e5SJeff Roberson vm_object_backing_insert_locked(object, backing_object); 78651b867e5SJeff Roberson VM_OBJECT_WUNLOCK(backing_object); 78751b867e5SJeff Roberson } else 78851b867e5SJeff Roberson object->backing_object = backing_object; 78951b867e5SJeff Roberson } 79051b867e5SJeff Roberson 79151b867e5SJeff Roberson 7922ac78f0eSStephan Uphoff /* 7937bbdb843SRuslan Bukin * vm_object_terminate_pages removes any remaining pageable pages 7947bbdb843SRuslan Bukin * from the object and resets the object to an empty state. 7957bbdb843SRuslan Bukin */ 7967bbdb843SRuslan Bukin static void 7977bbdb843SRuslan Bukin vm_object_terminate_pages(vm_object_t object) 7987bbdb843SRuslan Bukin { 7997bbdb843SRuslan Bukin vm_page_t p, p_next; 8007bbdb843SRuslan Bukin 8017bbdb843SRuslan Bukin VM_OBJECT_ASSERT_WLOCKED(object); 8027bbdb843SRuslan Bukin 8037bbdb843SRuslan Bukin /* 8047bbdb843SRuslan Bukin * Free any remaining pageable pages. This also removes them from the 8057bbdb843SRuslan Bukin * paging queues. However, don't free wired pages, just remove them 8067bbdb843SRuslan Bukin * from the object. Rather than incrementally removing each page from 8077bbdb843SRuslan Bukin * the object, the page and object are reset to any empty state. 8087bbdb843SRuslan Bukin */ 8097bbdb843SRuslan Bukin TAILQ_FOREACH_SAFE(p, &object->memq, listq, p_next) { 8107bbdb843SRuslan Bukin vm_page_assert_unbusied(p); 811fee2a2faSMark Johnston KASSERT(p->object == object && 812fee2a2faSMark Johnston (p->ref_count & VPRC_OBJREF) != 0, 813fee2a2faSMark Johnston ("vm_object_terminate_pages: page %p is inconsistent", p)); 814fee2a2faSMark Johnston 8157bbdb843SRuslan Bukin p->object = NULL; 816fee2a2faSMark Johnston if (vm_page_drop(p, VPRC_OBJREF) == VPRC_OBJREF) { 8175cd29d0fSMark Johnston VM_CNT_INC(v_pfree); 8185cd29d0fSMark Johnston vm_page_free(p); 8194074d642SAlan Cox } 820fee2a2faSMark Johnston } 8212fcd1ff6SKonstantin Belousov 8227bbdb843SRuslan Bukin /* 8237bbdb843SRuslan Bukin * If the object contained any pages, then reset it to an empty state. 8247bbdb843SRuslan Bukin * None of the object's fields, including "resident_page_count", were 8257bbdb843SRuslan Bukin * modified by the preceding loop. 8267bbdb843SRuslan Bukin */ 8277bbdb843SRuslan Bukin if (object->resident_page_count != 0) { 8287bbdb843SRuslan Bukin vm_radix_reclaim_allnodes(&object->rtree); 8297bbdb843SRuslan Bukin TAILQ_INIT(&object->memq); 8307bbdb843SRuslan Bukin object->resident_page_count = 0; 8317bbdb843SRuslan Bukin if (object->type == OBJT_VNODE) 8327bbdb843SRuslan Bukin vdrop(object->handle); 8337bbdb843SRuslan Bukin } 8347bbdb843SRuslan Bukin } 8357bbdb843SRuslan Bukin 8367bbdb843SRuslan Bukin /* 837df8bae1dSRodney W. Grimes * vm_object_terminate actually destroys the specified object, freeing 838df8bae1dSRodney W. Grimes * up all previously used resources. 839df8bae1dSRodney W. Grimes * 840df8bae1dSRodney W. Grimes * The object must be locked. 8411c7c3c6aSMatthew Dillon * This routine may block. 842df8bae1dSRodney W. Grimes */ 84395e5e988SJohn Dyson void 8441b40f8c0SMatthew Dillon vm_object_terminate(vm_object_t object) 845df8bae1dSRodney W. Grimes { 84689f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 847783a68aaSKonstantin Belousov KASSERT((object->flags & OBJ_DEAD) != 0, 848783a68aaSKonstantin Belousov ("terminating non-dead obj %p", object)); 849bef608bdSJohn Dyson 850cf27e0d1SJeff Roberson /* 851cf27e0d1SJeff Roberson * wait for the pageout daemon to be done with the object 852cf27e0d1SJeff Roberson */ 853cf27e0d1SJeff Roberson vm_object_pip_wait(object, "objtrm"); 854cf27e0d1SJeff Roberson 85511b57401SHans Petter Selasky KASSERT(!REFCOUNT_COUNT(object->paging_in_progress), 856cf27e0d1SJeff Roberson ("vm_object_terminate: pageout in progress")); 857cf27e0d1SJeff Roberson 858971dd342SAlfred Perlstein KASSERT(object->ref_count == 0, 859971dd342SAlfred Perlstein ("vm_object_terminate: object with references, ref_count=%d", 860971dd342SAlfred Perlstein object->ref_count)); 861996c772fSJohn Dyson 8627bbdb843SRuslan Bukin if ((object->flags & OBJ_PG_DTOR) == 0) 8637bbdb843SRuslan Bukin vm_object_terminate_pages(object); 864bef608bdSJohn Dyson 865f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 866f8a47341SAlan Cox if (__predict_false(!LIST_EMPTY(&object->rvq))) 867f8a47341SAlan Cox vm_reserv_break_all(object); 868f8a47341SAlan Cox #endif 8697bfda801SAlan Cox 870e735691bSJohn Baldwin KASSERT(object->cred == NULL || object->type == OBJT_DEFAULT || 871e735691bSJohn Baldwin object->type == OBJT_SWAP, 872e735691bSJohn Baldwin ("%s: non-swap obj %p has cred", __func__, object)); 873e735691bSJohn Baldwin 8742d8acc0fSJohn Dyson /* 8759fcfb650SDavid Greenman * Let the pager know object is dead. 8769fcfb650SDavid Greenman */ 8779fcfb650SDavid Greenman vm_pager_deallocate(object); 87889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 8799fcfb650SDavid Greenman 8802ac78f0eSStephan Uphoff vm_object_destroy(object); 88147221757SJohn Dyson } 882df8bae1dSRodney W. Grimes 883edf93b25SAlan Cox /* 884edf93b25SAlan Cox * Make the page read-only so that we can clear the object flags. However, if 885edf93b25SAlan Cox * this is a nosync mmap then the object is likely to stay dirty so do not 886edf93b25SAlan Cox * mess with the page and do not clear the object flags. Returns TRUE if the 887edf93b25SAlan Cox * page should be flushed, and FALSE otherwise. 888edf93b25SAlan Cox */ 8893280870dSKonstantin Belousov static boolean_t 89067d0e293SJeff Roberson vm_object_page_remove_write(vm_page_t p, int flags, boolean_t *allclean) 8913280870dSKonstantin Belousov { 8923280870dSKonstantin Belousov 893fff5403fSJeff Roberson vm_page_assert_busied(p); 894fff5403fSJeff Roberson 8953280870dSKonstantin Belousov /* 8963280870dSKonstantin Belousov * If we have been asked to skip nosync pages and this is a 8973280870dSKonstantin Belousov * nosync page, skip it. Note that the object flags were not 8983280870dSKonstantin Belousov * cleared in this case so we do not have to set them. 8993280870dSKonstantin Belousov */ 900fff5403fSJeff Roberson if ((flags & OBJPC_NOSYNC) != 0 && (p->aflags & PGA_NOSYNC) != 0) { 90167d0e293SJeff Roberson *allclean = FALSE; 9023280870dSKonstantin Belousov return (FALSE); 9033280870dSKonstantin Belousov } else { 9043280870dSKonstantin Belousov pmap_remove_write(p); 9053280870dSKonstantin Belousov return (p->dirty != 0); 9063280870dSKonstantin Belousov } 9073280870dSKonstantin Belousov } 9083280870dSKonstantin Belousov 909df8bae1dSRodney W. Grimes /* 910df8bae1dSRodney W. Grimes * vm_object_page_clean 911df8bae1dSRodney W. Grimes * 9124f79d873SMatthew Dillon * Clean all dirty pages in the specified range of object. Leaves page 9134f79d873SMatthew Dillon * on whatever queue it is currently on. If NOSYNC is set then do not 914fff5403fSJeff Roberson * write out pages with PGA_NOSYNC set (originally comes from MAP_NOSYNC), 9154f79d873SMatthew Dillon * leaving the object dirty. 91626f9a767SRodney W. Grimes * 91743b7990eSMatthew Dillon * When stuffing pages asynchronously, allow clustering. XXX we need a 91843b7990eSMatthew Dillon * synchronous clustering mode implementation. 91943b7990eSMatthew Dillon * 92026f9a767SRodney W. Grimes * Odd semantics: if start == end, we clean everything. 92126f9a767SRodney W. Grimes * 92226f9a767SRodney W. Grimes * The object must be locked. 923126d6082SKonstantin Belousov * 924126d6082SKonstantin Belousov * Returns FALSE if some page from the range was not written, as 925126d6082SKonstantin Belousov * reported by the pager, and TRUE otherwise. 92626f9a767SRodney W. Grimes */ 927126d6082SKonstantin Belousov boolean_t 92817f3095dSAlan Cox vm_object_page_clean(vm_object_t object, vm_ooffset_t start, vm_ooffset_t end, 929e239bb97SKonstantin Belousov int flags) 930f6b04d2bSDavid Greenman { 931e239bb97SKonstantin Belousov vm_page_t np, p; 93217f3095dSAlan Cox vm_pindex_t pi, tend, tstart; 933126d6082SKonstantin Belousov int curgeneration, n, pagerflags; 93467d0e293SJeff Roberson boolean_t eio, res, allclean; 935f6b04d2bSDavid Greenman 93689f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 937e5f299ffSKonstantin Belousov 93867d0e293SJeff Roberson if (object->type != OBJT_VNODE || !vm_object_mightbedirty(object) || 939e239bb97SKonstantin Belousov object->resident_page_count == 0) 940126d6082SKonstantin Belousov return (TRUE); 941f6b04d2bSDavid Greenman 942e239bb97SKonstantin Belousov pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) != 0 ? 943e239bb97SKonstantin Belousov VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK; 944e239bb97SKonstantin Belousov pagerflags |= (flags & OBJPC_INVAL) != 0 ? VM_PAGER_PUT_INVAL : 0; 945e239bb97SKonstantin Belousov 94617f3095dSAlan Cox tstart = OFF_TO_IDX(start); 94717f3095dSAlan Cox tend = (end == 0) ? object->size : OFF_TO_IDX(end + PAGE_MASK); 94867d0e293SJeff Roberson allclean = tstart == 0 && tend >= object->size; 949126d6082SKonstantin Belousov res = TRUE; 950f6b04d2bSDavid Greenman 951bd7e5f99SJohn Dyson rescan: 9522d8acc0fSJohn Dyson curgeneration = object->generation; 9532d8acc0fSJohn Dyson 95417f3095dSAlan Cox for (p = vm_page_find_least(object, tstart); p != NULL; p = np) { 955bd7e5f99SJohn Dyson pi = p->pindex; 956e239bb97SKonstantin Belousov if (pi >= tend) 957e239bb97SKonstantin Belousov break; 958e239bb97SKonstantin Belousov np = TAILQ_NEXT(p, listq); 9590012f373SJeff Roberson if (vm_page_none_valid(p)) 960aef922f5SJohn Dyson continue; 96163e97555SJeff Roberson if (vm_page_busy_acquire(p, VM_ALLOC_WAITFAIL) == 0) { 96267d0e293SJeff Roberson if (object->generation != curgeneration && 96367d0e293SJeff Roberson (flags & OBJPC_SYNC) != 0) 964e239bb97SKonstantin Belousov goto rescan; 965780636b7SKonstantin Belousov np = vm_page_find_least(object, pi); 966780636b7SKonstantin Belousov continue; 967f6b04d2bSDavid Greenman } 96867d0e293SJeff Roberson if (!vm_object_page_remove_write(p, flags, &allclean)) { 96963e97555SJeff Roberson vm_page_xunbusy(p); 970bd7e5f99SJohn Dyson continue; 97163e97555SJeff Roberson } 972e239bb97SKonstantin Belousov 9733280870dSKonstantin Belousov n = vm_object_page_collect_flush(object, p, pagerflags, 97467d0e293SJeff Roberson flags, &allclean, &eio); 975126d6082SKonstantin Belousov if (eio) { 976126d6082SKonstantin Belousov res = FALSE; 97767d0e293SJeff Roberson allclean = FALSE; 978126d6082SKonstantin Belousov } 97967d0e293SJeff Roberson if (object->generation != curgeneration && 98067d0e293SJeff Roberson (flags & OBJPC_SYNC) != 0) 981b9b7a4beSMatthew Dillon goto rescan; 982031ec8c1SKonstantin Belousov 983031ec8c1SKonstantin Belousov /* 984031ec8c1SKonstantin Belousov * If the VOP_PUTPAGES() did a truncated write, so 985031ec8c1SKonstantin Belousov * that even the first page of the run is not fully 986031ec8c1SKonstantin Belousov * written, vm_pageout_flush() returns 0 as the run 987031ec8c1SKonstantin Belousov * length. Since the condition that caused truncated 988031ec8c1SKonstantin Belousov * write may be permanent, e.g. exhausted free space, 989031ec8c1SKonstantin Belousov * accepting n == 0 would cause an infinite loop. 990031ec8c1SKonstantin Belousov * 991031ec8c1SKonstantin Belousov * Forwarding the iterator leaves the unwritten page 992031ec8c1SKonstantin Belousov * behind, but there is not much we can do there if 993031ec8c1SKonstantin Belousov * filesystem refuses to write it. 994031ec8c1SKonstantin Belousov */ 995126d6082SKonstantin Belousov if (n == 0) { 996031ec8c1SKonstantin Belousov n = 1; 99767d0e293SJeff Roberson allclean = FALSE; 998126d6082SKonstantin Belousov } 999e239bb97SKonstantin Belousov np = vm_page_find_least(object, pi + n); 1000b9b7a4beSMatthew Dillon } 1001b9b7a4beSMatthew Dillon #if 0 1002e239bb97SKonstantin Belousov VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC) ? MNT_WAIT : 0); 1003b9b7a4beSMatthew Dillon #endif 1004b9b7a4beSMatthew Dillon 100567d0e293SJeff Roberson if (allclean) 100667d0e293SJeff Roberson object->cleangeneration = curgeneration; 1007126d6082SKonstantin Belousov return (res); 1008b9b7a4beSMatthew Dillon } 1009b9b7a4beSMatthew Dillon 1010b9b7a4beSMatthew Dillon static int 10113280870dSKonstantin Belousov vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int pagerflags, 101267d0e293SJeff Roberson int flags, boolean_t *allclean, boolean_t *eio) 1013b9b7a4beSMatthew Dillon { 10143157c503SKonstantin Belousov vm_page_t ma[vm_pageout_page_count], p_first, tp; 10153157c503SKonstantin Belousov int count, i, mreq, runlen; 1016b9b7a4beSMatthew Dillon 10177bec141bSKip Macy vm_page_lock_assert(p, MA_NOTOWNED); 101863e97555SJeff Roberson vm_page_assert_xbusied(p); 101989f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 10203157c503SKonstantin Belousov 10213157c503SKonstantin Belousov count = 1; 10223157c503SKonstantin Belousov mreq = 0; 10233157c503SKonstantin Belousov 10243157c503SKonstantin Belousov for (tp = p; count < vm_pageout_page_count; count++) { 10253157c503SKonstantin Belousov tp = vm_page_next(tp); 102663e97555SJeff Roberson if (tp == NULL || vm_page_tryxbusy(tp) == 0) 1027bd7e5f99SJohn Dyson break; 102867d0e293SJeff Roberson if (!vm_object_page_remove_write(tp, flags, allclean)) { 102963e97555SJeff Roberson vm_page_xunbusy(tp); 1030bd7e5f99SJohn Dyson break; 1031bd7e5f99SJohn Dyson } 103263e97555SJeff Roberson } 1033aef922f5SJohn Dyson 10343157c503SKonstantin Belousov for (p_first = p; count < vm_pageout_page_count; count++) { 10353157c503SKonstantin Belousov tp = vm_page_prev(p_first); 103663e97555SJeff Roberson if (tp == NULL || vm_page_tryxbusy(tp) == 0) 1037bd7e5f99SJohn Dyson break; 103867d0e293SJeff Roberson if (!vm_object_page_remove_write(tp, flags, allclean)) { 103963e97555SJeff Roberson vm_page_xunbusy(tp); 1040bd7e5f99SJohn Dyson break; 104163e97555SJeff Roberson } 10423157c503SKonstantin Belousov p_first = tp; 10433157c503SKonstantin Belousov mreq++; 1044bd7e5f99SJohn Dyson } 1045bd7e5f99SJohn Dyson 10463157c503SKonstantin Belousov for (tp = p_first, i = 0; i < count; tp = TAILQ_NEXT(tp, listq), i++) 10473157c503SKonstantin Belousov ma[i] = tp; 1048cf2819ccSJohn Dyson 1049126d6082SKonstantin Belousov vm_pageout_flush(ma, count, pagerflags, mreq, &runlen, eio); 10501e8a675cSKonstantin Belousov return (runlen); 105126f9a767SRodney W. Grimes } 1052df8bae1dSRodney W. Grimes 10531efb74fbSJohn Dyson /* 1054950f8459SAlan Cox * Note that there is absolutely no sense in writing out 1055950f8459SAlan Cox * anonymous objects, so we track down the vnode object 1056950f8459SAlan Cox * to write out. 1057950f8459SAlan Cox * We invalidate (remove) all pages from the address space 1058950f8459SAlan Cox * for semantic correctness. 1059950f8459SAlan Cox * 10606bbee8e2SAlan Cox * If the backing object is a device object with unmanaged pages, then any 10616bbee8e2SAlan Cox * mappings to the specified range of pages must be removed before this 10626bbee8e2SAlan Cox * function is called. 10636bbee8e2SAlan Cox * 1064950f8459SAlan Cox * Note: certain anonymous maps, such as MAP_NOSYNC maps, 1065950f8459SAlan Cox * may start out with a NULL object. 1066950f8459SAlan Cox */ 1067126d6082SKonstantin Belousov boolean_t 1068950f8459SAlan Cox vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size, 1069950f8459SAlan Cox boolean_t syncio, boolean_t invalidate) 1070950f8459SAlan Cox { 1071950f8459SAlan Cox vm_object_t backing_object; 1072950f8459SAlan Cox struct vnode *vp; 10733b582b4eSTor Egge struct mount *mp; 1074126d6082SKonstantin Belousov int error, flags, fsync_after; 1075126d6082SKonstantin Belousov boolean_t res; 1076950f8459SAlan Cox 1077950f8459SAlan Cox if (object == NULL) 1078126d6082SKonstantin Belousov return (TRUE); 1079126d6082SKonstantin Belousov res = TRUE; 1080126d6082SKonstantin Belousov error = 0; 108189f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1082950f8459SAlan Cox while ((backing_object = object->backing_object) != NULL) { 108389f6b863SAttilio Rao VM_OBJECT_WLOCK(backing_object); 108456e0670fSAlan Cox offset += object->backing_object_offset; 108589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1086950f8459SAlan Cox object = backing_object; 1087950f8459SAlan Cox if (object->size < OFF_TO_IDX(offset + size)) 1088950f8459SAlan Cox size = IDX_TO_OFF(object->size) - offset; 1089950f8459SAlan Cox } 1090950f8459SAlan Cox /* 1091950f8459SAlan Cox * Flush pages if writing is allowed, invalidate them 1092950f8459SAlan Cox * if invalidation requested. Pages undergoing I/O 1093950f8459SAlan Cox * will be ignored by vm_object_page_remove(). 1094950f8459SAlan Cox * 1095950f8459SAlan Cox * We cannot lock the vnode and then wait for paging 1096950f8459SAlan Cox * to complete without deadlocking against vm_fault. 1097950f8459SAlan Cox * Instead we simply call vm_object_page_remove() and 1098950f8459SAlan Cox * allow it to block internally on a page-by-page 1099950f8459SAlan Cox * basis when it encounters pages undergoing async 1100950f8459SAlan Cox * I/O. 1101950f8459SAlan Cox */ 1102950f8459SAlan Cox if (object->type == OBJT_VNODE && 110367d0e293SJeff Roberson vm_object_mightbedirty(object) != 0 && 11045bf94937SKonstantin Belousov ((vp = object->handle)->v_vflag & VV_NOSYNC) == 0) { 110589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 11063b582b4eSTor Egge (void) vn_start_write(vp, &mp, V_WAIT); 1107cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 110875ff604aSKonstantin Belousov if (syncio && !invalidate && offset == 0 && 1109d1780e8dSKonstantin Belousov atop(size) == object->size) { 111075ff604aSKonstantin Belousov /* 111175ff604aSKonstantin Belousov * If syncing the whole mapping of the file, 111275ff604aSKonstantin Belousov * it is faster to schedule all the writes in 111375ff604aSKonstantin Belousov * async mode, also allowing the clustering, 111475ff604aSKonstantin Belousov * and then wait for i/o to complete. 111575ff604aSKonstantin Belousov */ 111675ff604aSKonstantin Belousov flags = 0; 111775ff604aSKonstantin Belousov fsync_after = TRUE; 111875ff604aSKonstantin Belousov } else { 1119950f8459SAlan Cox flags = (syncio || invalidate) ? OBJPC_SYNC : 0; 112075ff604aSKonstantin Belousov flags |= invalidate ? (OBJPC_SYNC | OBJPC_INVAL) : 0; 112175ff604aSKonstantin Belousov fsync_after = FALSE; 112275ff604aSKonstantin Belousov } 112389f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1124126d6082SKonstantin Belousov res = vm_object_page_clean(object, offset, offset + size, 1125126d6082SKonstantin Belousov flags); 112689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 112775ff604aSKonstantin Belousov if (fsync_after) 1128126d6082SKonstantin Belousov error = VOP_FSYNC(vp, MNT_WAIT, curthread); 112922db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 11303b582b4eSTor Egge vn_finished_write(mp); 1131126d6082SKonstantin Belousov if (error != 0) 1132126d6082SKonstantin Belousov res = FALSE; 113389f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1134950f8459SAlan Cox } 1135950f8459SAlan Cox if ((object->type == OBJT_VNODE || 1136950f8459SAlan Cox object->type == OBJT_DEVICE) && invalidate) { 11376bbee8e2SAlan Cox if (object->type == OBJT_DEVICE) 11386bbee8e2SAlan Cox /* 11396bbee8e2SAlan Cox * The option OBJPR_NOTMAPPED must be passed here 11406bbee8e2SAlan Cox * because vm_object_page_remove() cannot remove 11416bbee8e2SAlan Cox * unmanaged mappings. 11426bbee8e2SAlan Cox */ 11436bbee8e2SAlan Cox flags = OBJPR_NOTMAPPED; 11446bbee8e2SAlan Cox else if (old_msync) 11456195b24aSKonstantin Belousov flags = 0; 11466bbee8e2SAlan Cox else 11476195b24aSKonstantin Belousov flags = OBJPR_CLEANONLY; 11486bbee8e2SAlan Cox vm_object_page_remove(object, OFF_TO_IDX(offset), 11496bbee8e2SAlan Cox OFF_TO_IDX(offset + size + PAGE_MASK), flags); 1150950f8459SAlan Cox } 115189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1152126d6082SKonstantin Belousov return (res); 1153950f8459SAlan Cox } 1154950f8459SAlan Cox 1155950f8459SAlan Cox /* 1156aa3650eaSMark Johnston * Determine whether the given advice can be applied to the object. Advice is 1157aa3650eaSMark Johnston * not applied to unmanaged pages since they never belong to page queues, and 1158aa3650eaSMark Johnston * since MADV_FREE is destructive, it can apply only to anonymous pages that 1159aa3650eaSMark Johnston * have been mapped at most once. 1160aa3650eaSMark Johnston */ 1161aa3650eaSMark Johnston static bool 1162aa3650eaSMark Johnston vm_object_advice_applies(vm_object_t object, int advice) 1163aa3650eaSMark Johnston { 1164aa3650eaSMark Johnston 1165aa3650eaSMark Johnston if ((object->flags & OBJ_UNMANAGED) != 0) 1166aa3650eaSMark Johnston return (false); 1167aa3650eaSMark Johnston if (advice != MADV_FREE) 1168aa3650eaSMark Johnston return (true); 116963967687SJeff Roberson return ((object->flags & (OBJ_ONEMAPPING | OBJ_ANON)) == 117063967687SJeff Roberson (OBJ_ONEMAPPING | OBJ_ANON)); 1171aa3650eaSMark Johnston } 1172aa3650eaSMark Johnston 1173aa3650eaSMark Johnston static void 1174aa3650eaSMark Johnston vm_object_madvise_freespace(vm_object_t object, int advice, vm_pindex_t pindex, 1175aa3650eaSMark Johnston vm_size_t size) 1176aa3650eaSMark Johnston { 1177aa3650eaSMark Johnston 1178aa3650eaSMark Johnston if (advice == MADV_FREE && object->type == OBJT_SWAP) 1179aa3650eaSMark Johnston swap_pager_freespace(object, pindex, size); 1180aa3650eaSMark Johnston } 1181aa3650eaSMark Johnston 1182aa3650eaSMark Johnston /* 1183867a482dSJohn Dyson * vm_object_madvise: 1184867a482dSJohn Dyson * 1185867a482dSJohn Dyson * Implements the madvise function at the object/page level. 11861c7c3c6aSMatthew Dillon * 1187193b9358SAlan Cox * MADV_WILLNEED (any object) 1188193b9358SAlan Cox * 1189193b9358SAlan Cox * Activate the specified pages if they are resident. 1190193b9358SAlan Cox * 1191193b9358SAlan Cox * MADV_DONTNEED (any object) 1192193b9358SAlan Cox * 1193193b9358SAlan Cox * Deactivate the specified pages if they are resident. 1194193b9358SAlan Cox * 1195193b9358SAlan Cox * MADV_FREE (OBJT_DEFAULT/OBJT_SWAP objects, 1196193b9358SAlan Cox * OBJ_ONEMAPPING only) 1197193b9358SAlan Cox * 1198193b9358SAlan Cox * Deactivate and clean the specified pages if they are 1199193b9358SAlan Cox * resident. This permits the process to reuse the pages 1200193b9358SAlan Cox * without faulting or the kernel to reclaim the pages 1201193b9358SAlan Cox * without I/O. 1202867a482dSJohn Dyson */ 1203867a482dSJohn Dyson void 120492a59946SJohn Baldwin vm_object_madvise(vm_object_t object, vm_pindex_t pindex, vm_pindex_t end, 1205c2655a40SMark Johnston int advice) 1206867a482dSJohn Dyson { 120792a59946SJohn Baldwin vm_pindex_t tpindex; 120834567de7SAlan Cox vm_object_t backing_object, tobject; 1209aa3650eaSMark Johnston vm_page_t m, tm; 1210867a482dSJohn Dyson 1211867a482dSJohn Dyson if (object == NULL) 1212867a482dSJohn Dyson return; 1213c2655a40SMark Johnston 12146e20a165SJohn Dyson relookup: 1215aa3650eaSMark Johnston VM_OBJECT_WLOCK(object); 1216aa3650eaSMark Johnston if (!vm_object_advice_applies(object, advice)) { 1217aa3650eaSMark Johnston VM_OBJECT_WUNLOCK(object); 1218aa3650eaSMark Johnston return; 12196e20a165SJohn Dyson } 1220aa3650eaSMark Johnston for (m = vm_page_find_least(object, pindex); pindex < end; pindex++) { 1221aa3650eaSMark Johnston tobject = object; 1222c2655a40SMark Johnston 12231ce137beSMatthew Dillon /* 1224aa3650eaSMark Johnston * If the next page isn't resident in the top-level object, we 1225aa3650eaSMark Johnston * need to search the shadow chain. When applying MADV_FREE, we 1226aa3650eaSMark Johnston * take care to release any swap space used to store 1227aa3650eaSMark Johnston * non-resident pages. 1228aa3650eaSMark Johnston */ 1229aa3650eaSMark Johnston if (m == NULL || pindex < m->pindex) { 1230aa3650eaSMark Johnston /* 1231aa3650eaSMark Johnston * Optimize a common case: if the top-level object has 1232aa3650eaSMark Johnston * no backing object, we can skip over the non-resident 1233aa3650eaSMark Johnston * range in constant time. 12341ce137beSMatthew Dillon */ 1235c2655a40SMark Johnston if (object->backing_object == NULL) { 1236c2655a40SMark Johnston tpindex = (m != NULL && m->pindex < end) ? 1237c2655a40SMark Johnston m->pindex : end; 1238aa3650eaSMark Johnston vm_object_madvise_freespace(object, advice, 1239aa3650eaSMark Johnston pindex, tpindex - pindex); 1240c2655a40SMark Johnston if ((pindex = tpindex) == end) 1241c2655a40SMark Johnston break; 1242aa3650eaSMark Johnston goto next_page; 1243aa3650eaSMark Johnston } 1244aa3650eaSMark Johnston 1245aa3650eaSMark Johnston tpindex = pindex; 1246aa3650eaSMark Johnston do { 1247aa3650eaSMark Johnston vm_object_madvise_freespace(tobject, advice, 1248aa3650eaSMark Johnston tpindex, 1); 12491ce137beSMatthew Dillon /* 1250aa3650eaSMark Johnston * Prepare to search the next object in the 1251aa3650eaSMark Johnston * chain. 12521ce137beSMatthew Dillon */ 125334567de7SAlan Cox backing_object = tobject->backing_object; 125434567de7SAlan Cox if (backing_object == NULL) 1255aa3650eaSMark Johnston goto next_pindex; 125689f6b863SAttilio Rao VM_OBJECT_WLOCK(backing_object); 1257aa3650eaSMark Johnston tpindex += 1258aa3650eaSMark Johnston OFF_TO_IDX(tobject->backing_object_offset); 12599b98b796SAlan Cox if (tobject != object) 126089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(tobject); 126134567de7SAlan Cox tobject = backing_object; 1262aa3650eaSMark Johnston if (!vm_object_advice_applies(tobject, advice)) 1263aa3650eaSMark Johnston goto next_pindex; 1264aa3650eaSMark Johnston } while ((tm = vm_page_lookup(tobject, tpindex)) == 1265aa3650eaSMark Johnston NULL); 1266aa3650eaSMark Johnston } else { 1267aa3650eaSMark Johnston next_page: 1268aa3650eaSMark Johnston tm = m; 1269aa3650eaSMark Johnston m = TAILQ_NEXT(m, listq); 1270c2655a40SMark Johnston } 1271c2655a40SMark Johnston 1272867a482dSJohn Dyson /* 12730012f373SJeff Roberson * If the page is not in a normal state, skip it. The page 12740012f373SJeff Roberson * can not be invalidated while the object lock is held. 1275867a482dSJohn Dyson */ 12760012f373SJeff Roberson if (!vm_page_all_valid(tm) || vm_page_wired(tm)) 1277aa3650eaSMark Johnston goto next_pindex; 1278aa3650eaSMark Johnston KASSERT((tm->flags & PG_FICTITIOUS) == 0, 1279aa3650eaSMark Johnston ("vm_object_madvise: page %p is fictitious", tm)); 1280aa3650eaSMark Johnston KASSERT((tm->oflags & VPO_UNMANAGED) == 0, 1281aa3650eaSMark Johnston ("vm_object_madvise: page %p is not managed", tm)); 128263e97555SJeff Roberson if (vm_page_tryxbusy(tm) == 0) { 1283aa3650eaSMark Johnston if (object != tobject) 1284aa3650eaSMark Johnston VM_OBJECT_WUNLOCK(object); 1285c2655a40SMark Johnston if (advice == MADV_WILLNEED) { 1286b11b56b5SAlan Cox /* 1287b11b56b5SAlan Cox * Reference the page before unlocking and 1288b11b56b5SAlan Cox * sleeping so that the page daemon is less 1289b11b56b5SAlan Cox * likely to reclaim it. 1290b11b56b5SAlan Cox */ 1291aa3650eaSMark Johnston vm_page_aflag_set(tm, PGA_REFERENCED); 1292567e51e1SAlan Cox } 1293aa3650eaSMark Johnston vm_page_busy_sleep(tm, "madvpo", false); 12946e20a165SJohn Dyson goto relookup; 129534567de7SAlan Cox } 1296fee2a2faSMark Johnston vm_page_lock(tm); 1297aa3650eaSMark Johnston vm_page_advise(tm, advice); 1298aa3650eaSMark Johnston vm_page_unlock(tm); 129963e97555SJeff Roberson vm_page_xunbusy(tm); 1300aa3650eaSMark Johnston vm_object_madvise_freespace(tobject, advice, tm->pindex, 1); 1301aa3650eaSMark Johnston next_pindex: 13029b98b796SAlan Cox if (tobject != object) 130389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(tobject); 1304867a482dSJohn Dyson } 130589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1306867a482dSJohn Dyson } 1307867a482dSJohn Dyson 1308867a482dSJohn Dyson /* 1309df8bae1dSRodney W. Grimes * vm_object_shadow: 1310df8bae1dSRodney W. Grimes * 1311df8bae1dSRodney W. Grimes * Create a new object which is backed by the 1312df8bae1dSRodney W. Grimes * specified existing object range. The source 1313df8bae1dSRodney W. Grimes * object reference is deallocated. 1314df8bae1dSRodney W. Grimes * 1315df8bae1dSRodney W. Grimes * The new object and offset into that object 1316df8bae1dSRodney W. Grimes * are returned in the source parameters. 1317df8bae1dSRodney W. Grimes */ 131826f9a767SRodney W. Grimes void 1319*67388836SKonstantin Belousov vm_object_shadow(vm_object_t *object, vm_ooffset_t *offset, vm_size_t length, 1320*67388836SKonstantin Belousov struct ucred *cred, bool shared) 1321df8bae1dSRodney W. Grimes { 1322d031cff1SMatthew Dillon vm_object_t source; 1323d031cff1SMatthew Dillon vm_object_t result; 1324df8bae1dSRodney W. Grimes 1325df8bae1dSRodney W. Grimes source = *object; 1326df8bae1dSRodney W. Grimes 1327df8bae1dSRodney W. Grimes /* 13289a2f6362SAlan Cox * Don't create the new object if the old object isn't shared. 132963967687SJeff Roberson * 133063967687SJeff Roberson * If we hold the only reference we can guarantee that it won't 133163967687SJeff Roberson * increase while we have the map locked. Otherwise the race is 133263967687SJeff Roberson * harmless and we will end up with an extra shadow object that 133363967687SJeff Roberson * will be collapsed later. 13349a2f6362SAlan Cox */ 133563967687SJeff Roberson if (source != NULL && source->ref_count == 1 && 133632362449SKonstantin Belousov (source->flags & OBJ_ANON) != 0) 13379a2f6362SAlan Cox return; 13389a2f6362SAlan Cox 13399a2f6362SAlan Cox /* 1340570a2f4aSAlan Cox * Allocate a new object with the given length. 1341df8bae1dSRodney W. Grimes */ 1342*67388836SKonstantin Belousov result = vm_object_allocate_anon(atop(length), source, cred, length); 1343df8bae1dSRodney W. Grimes 1344df8bae1dSRodney W. Grimes /* 134551b867e5SJeff Roberson * Store the offset into the source object, and fix up the offset into 134651b867e5SJeff Roberson * the new object. 134751b867e5SJeff Roberson */ 134851b867e5SJeff Roberson result->backing_object_offset = *offset; 134951b867e5SJeff Roberson 1350*67388836SKonstantin Belousov if (shared || source != NULL) { 1351*67388836SKonstantin Belousov VM_OBJECT_WLOCK(result); 1352*67388836SKonstantin Belousov 135351b867e5SJeff Roberson /* 1354*67388836SKonstantin Belousov * The new object shadows the source object, adding a 1355*67388836SKonstantin Belousov * reference to it. Our caller changes his reference 1356*67388836SKonstantin Belousov * to point to the new object, removing a reference to 1357*67388836SKonstantin Belousov * the source object. Net result: no change of 1358*67388836SKonstantin Belousov * reference count, unless the caller needs to add one 1359*67388836SKonstantin Belousov * more reference due to forking a shared map entry. 1360*67388836SKonstantin Belousov */ 1361*67388836SKonstantin Belousov if (shared) { 1362*67388836SKonstantin Belousov vm_object_reference_locked(result); 1363*67388836SKonstantin Belousov vm_object_clear_flag(result, OBJ_ONEMAPPING); 1364*67388836SKonstantin Belousov } 1365*67388836SKonstantin Belousov 1366*67388836SKonstantin Belousov /* 1367*67388836SKonstantin Belousov * Try to optimize the result object's page color when 1368*67388836SKonstantin Belousov * shadowing in order to maintain page coloring 1369*67388836SKonstantin Belousov * consistency in the combined shadowed object. 1370df8bae1dSRodney W. Grimes */ 1371570a2f4aSAlan Cox if (source != NULL) { 137251b867e5SJeff Roberson vm_object_backing_insert(result, source); 13733f289c3fSJeff Roberson result->domain = source->domain; 1374f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 13757b54b1a9SAlan Cox result->flags |= source->flags & OBJ_COLORED; 1376*67388836SKonstantin Belousov result->pg_color = (source->pg_color + 1377*67388836SKonstantin Belousov OFF_TO_IDX(*offset)) & ((1 << (VM_NFREEORDER - 1378*67388836SKonstantin Belousov 1)) - 1); 1379f8a47341SAlan Cox #endif 1380*67388836SKonstantin Belousov } 138151b867e5SJeff Roberson VM_OBJECT_WUNLOCK(result); 1382de5f6a77SJohn Dyson } 1383df8bae1dSRodney W. Grimes 1384df8bae1dSRodney W. Grimes /* 1385df8bae1dSRodney W. Grimes * Return the new things 1386df8bae1dSRodney W. Grimes */ 1387df8bae1dSRodney W. Grimes *offset = 0; 1388df8bae1dSRodney W. Grimes *object = result; 1389df8bae1dSRodney W. Grimes } 1390df8bae1dSRodney W. Grimes 1391c5aaa06dSAlan Cox /* 1392c5aaa06dSAlan Cox * vm_object_split: 1393c5aaa06dSAlan Cox * 1394c5aaa06dSAlan Cox * Split the pages in a map entry into a new object. This affords 1395c5aaa06dSAlan Cox * easier removal of unused pages, and keeps object inheritance from 1396c5aaa06dSAlan Cox * being a negative impact on memory usage. 1397c5aaa06dSAlan Cox */ 1398c5aaa06dSAlan Cox void 1399c5aaa06dSAlan Cox vm_object_split(vm_map_entry_t entry) 1400c5aaa06dSAlan Cox { 140173000556SAlan Cox vm_page_t m, m_next; 1402c5aaa06dSAlan Cox vm_object_t orig_object, new_object, source; 140373000556SAlan Cox vm_pindex_t idx, offidxstart; 140473000556SAlan Cox vm_size_t size; 1405c5aaa06dSAlan Cox 1406c5aaa06dSAlan Cox orig_object = entry->object.vm_object; 140763967687SJeff Roberson if ((orig_object->flags & OBJ_ANON) == 0) 1408c5aaa06dSAlan Cox return; 1409c5aaa06dSAlan Cox if (orig_object->ref_count <= 1) 1410c5aaa06dSAlan Cox return; 141189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(orig_object); 1412c5aaa06dSAlan Cox 14134da9f125SAlan Cox offidxstart = OFF_TO_IDX(entry->offset); 141495442adfSAlan Cox size = atop(entry->end - entry->start); 1415c5aaa06dSAlan Cox 14164da9f125SAlan Cox /* 14174da9f125SAlan Cox * If swap_pager_copy() is later called, it will convert new_object 14184da9f125SAlan Cox * into a swap object. 14194da9f125SAlan Cox */ 1420*67388836SKonstantin Belousov new_object = vm_object_allocate_anon(size, orig_object, 1421*67388836SKonstantin Belousov orig_object->cred, ptoa(size)); 1422c5aaa06dSAlan Cox 1423c5474b8fSAlan Cox /* 1424c5474b8fSAlan Cox * At this point, the new object is still private, so the order in 1425c5474b8fSAlan Cox * which the original and new objects are locked does not matter. 1426c5474b8fSAlan Cox */ 142789f6b863SAttilio Rao VM_OBJECT_WLOCK(new_object); 142889f6b863SAttilio Rao VM_OBJECT_WLOCK(orig_object); 14293f289c3fSJeff Roberson new_object->domain = orig_object->domain; 1430c5aaa06dSAlan Cox source = orig_object->backing_object; 1431c5aaa06dSAlan Cox if (source != NULL) { 143251b867e5SJeff Roberson if ((source->flags & (OBJ_ANON | OBJ_DEAD)) != 0) { 143389f6b863SAttilio Rao VM_OBJECT_WLOCK(source); 143419c244d0SAlan Cox if ((source->flags & OBJ_DEAD) != 0) { 143589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(source); 143689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(orig_object); 143789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(new_object); 1438*67388836SKonstantin Belousov new_object->cred = NULL; 143919c244d0SAlan Cox vm_object_deallocate(new_object); 144089f6b863SAttilio Rao VM_OBJECT_WLOCK(orig_object); 144119c244d0SAlan Cox return; 144219c244d0SAlan Cox } 144351b867e5SJeff Roberson vm_object_backing_insert_locked(new_object, source); 1444b921a12bSAlan Cox vm_object_reference_locked(source); /* for new_object */ 1445c5aaa06dSAlan Cox vm_object_clear_flag(source, OBJ_ONEMAPPING); 144689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(source); 144751b867e5SJeff Roberson } else { 144851b867e5SJeff Roberson vm_object_backing_insert(new_object, source); 144951b867e5SJeff Roberson vm_object_reference(source); 145051b867e5SJeff Roberson } 1451c5aaa06dSAlan Cox new_object->backing_object_offset = 14524da9f125SAlan Cox orig_object->backing_object_offset + entry->offset; 1453c5aaa06dSAlan Cox } 1454ef694c1aSEdward Tomasz Napierala if (orig_object->cred != NULL) { 1455ef694c1aSEdward Tomasz Napierala crhold(orig_object->cred); 14563364c323SKonstantin Belousov KASSERT(orig_object->charge >= ptoa(size), 14573364c323SKonstantin Belousov ("orig_object->charge < 0")); 14583364c323SKonstantin Belousov orig_object->charge -= ptoa(size); 14593364c323SKonstantin Belousov } 1460c5aaa06dSAlan Cox retry: 1461b382c10aSKonstantin Belousov m = vm_page_find_least(orig_object, offidxstart); 146273000556SAlan Cox for (; m != NULL && (idx = m->pindex - offidxstart) < size; 146373000556SAlan Cox m = m_next) { 146473000556SAlan Cox m_next = TAILQ_NEXT(m, listq); 1465c5aaa06dSAlan Cox 1466c5aaa06dSAlan Cox /* 1467c5aaa06dSAlan Cox * We must wait for pending I/O to complete before we can 1468c5aaa06dSAlan Cox * rename the page. 1469c5aaa06dSAlan Cox * 1470c5aaa06dSAlan Cox * We do not have to VM_PROT_NONE the page as mappings should 1471c5aaa06dSAlan Cox * not be changed by this operation. 1472c5aaa06dSAlan Cox */ 147363e97555SJeff Roberson if (vm_page_tryxbusy(m) == 0) { 147489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(new_object); 14754cdea4a8SJeff Roberson vm_page_sleep_if_busy(m, "spltwt"); 147689f6b863SAttilio Rao VM_OBJECT_WLOCK(new_object); 1477c5aaa06dSAlan Cox goto retry; 1478de33beddSAlan Cox } 1479e946b949SAttilio Rao 14803453bca8SAlan Cox /* vm_page_rename() will dirty the page. */ 1481e946b949SAttilio Rao if (vm_page_rename(m, new_object, idx)) { 148263e97555SJeff Roberson vm_page_xunbusy(m); 1483e946b949SAttilio Rao VM_OBJECT_WUNLOCK(new_object); 1484e946b949SAttilio Rao VM_OBJECT_WUNLOCK(orig_object); 14858d6fbbb8SJeff Roberson vm_radix_wait(); 1486e946b949SAttilio Rao VM_OBJECT_WLOCK(orig_object); 1487e946b949SAttilio Rao VM_OBJECT_WLOCK(new_object); 1488e946b949SAttilio Rao goto retry; 1489e946b949SAttilio Rao } 149063e97555SJeff Roberson 1491b5f359b7SAlan Cox #if VM_NRESERVLEVEL > 0 1492b5f359b7SAlan Cox /* 1493b5f359b7SAlan Cox * If some of the reservation's allocated pages remain with 1494b5f359b7SAlan Cox * the original object, then transferring the reservation to 1495b5f359b7SAlan Cox * the new object is neither particularly beneficial nor 1496b5f359b7SAlan Cox * particularly harmful as compared to leaving the reservation 1497b5f359b7SAlan Cox * with the original object. If, however, all of the 1498b5f359b7SAlan Cox * reservation's allocated pages are transferred to the new 1499b5f359b7SAlan Cox * object, then transferring the reservation is typically 1500b5f359b7SAlan Cox * beneficial. Determining which of these two cases applies 1501b5f359b7SAlan Cox * would be more costly than unconditionally renaming the 1502b5f359b7SAlan Cox * reservation. 1503b5f359b7SAlan Cox */ 1504b5f359b7SAlan Cox vm_reserv_rename(m, new_object, orig_object, offidxstart); 1505b5f359b7SAlan Cox #endif 15068da1c098SJeff Roberson if (orig_object->type != OBJT_SWAP) 15078da1c098SJeff Roberson vm_page_xunbusy(m); 1508c5aaa06dSAlan Cox } 1509d7a013c3SAlan Cox if (orig_object->type == OBJT_SWAP) { 1510c5aaa06dSAlan Cox /* 1511c7c8dd7eSAlan Cox * swap_pager_copy() can sleep, in which case the orig_object's 1512c7c8dd7eSAlan Cox * and new_object's locks are released and reacquired. 1513c5aaa06dSAlan Cox */ 1514c5aaa06dSAlan Cox swap_pager_copy(orig_object, new_object, offidxstart, 0); 1515dfd55c0cSAttilio Rao TAILQ_FOREACH(m, &new_object->memq, listq) 1516c7aebda8SAttilio Rao vm_page_xunbusy(m); 1517c5aaa06dSAlan Cox } 151889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(orig_object); 151989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(new_object); 1520c5aaa06dSAlan Cox entry->object.vm_object = new_object; 1521c5aaa06dSAlan Cox entry->offset = 0LL; 1522c5aaa06dSAlan Cox vm_object_deallocate(orig_object); 152389f6b863SAttilio Rao VM_OBJECT_WLOCK(new_object); 1524c5aaa06dSAlan Cox } 1525c5aaa06dSAlan Cox 15262ad1a3f7SMatthew Dillon #define OBSC_COLLAPSE_NOWAIT 0x0002 15272ad1a3f7SMatthew Dillon #define OBSC_COLLAPSE_WAIT 0x0004 15282ad1a3f7SMatthew Dillon 152999a1570aSKonstantin Belousov static vm_page_t 15304cc8daf7SConrad Meyer vm_object_collapse_scan_wait(vm_object_t object, vm_page_t p, vm_page_t next, 153199a1570aSKonstantin Belousov int op) 153299a1570aSKonstantin Belousov { 153399a1570aSKonstantin Belousov vm_object_t backing_object; 153499a1570aSKonstantin Belousov 153599a1570aSKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(object); 153699a1570aSKonstantin Belousov backing_object = object->backing_object; 153799a1570aSKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(backing_object); 153899a1570aSKonstantin Belousov 153999a1570aSKonstantin Belousov KASSERT(p == NULL || p->object == object || p->object == backing_object, 154099a1570aSKonstantin Belousov ("invalid ownership %p %p %p", p, object, backing_object)); 154199a1570aSKonstantin Belousov if ((op & OBSC_COLLAPSE_NOWAIT) != 0) 154299a1570aSKonstantin Belousov return (next); 15438d6fbbb8SJeff Roberson /* The page is only NULL when rename fails. */ 15444cdea4a8SJeff Roberson if (p == NULL) { 15456a14746cSRyan Libby VM_OBJECT_WUNLOCK(object); 15466a14746cSRyan Libby VM_OBJECT_WUNLOCK(backing_object); 15478d6fbbb8SJeff Roberson vm_radix_wait(); 15484cdea4a8SJeff Roberson } else { 15494cdea4a8SJeff Roberson if (p->object == object) 15504cdea4a8SJeff Roberson VM_OBJECT_WUNLOCK(backing_object); 155199a1570aSKonstantin Belousov else 15524cdea4a8SJeff Roberson VM_OBJECT_WUNLOCK(object); 15535975e53dSKonstantin Belousov vm_page_busy_sleep(p, "vmocol", false); 15544cdea4a8SJeff Roberson } 155599a1570aSKonstantin Belousov VM_OBJECT_WLOCK(object); 155699a1570aSKonstantin Belousov VM_OBJECT_WLOCK(backing_object); 155799a1570aSKonstantin Belousov return (TAILQ_FIRST(&backing_object->memq)); 155899a1570aSKonstantin Belousov } 155999a1570aSKonstantin Belousov 156099a1570aSKonstantin Belousov static bool 15614cc8daf7SConrad Meyer vm_object_scan_all_shadowed(vm_object_t object) 15624cc8daf7SConrad Meyer { 15634cc8daf7SConrad Meyer vm_object_t backing_object; 15644cc8daf7SConrad Meyer vm_page_t p, pp; 156577d6fd97SKonstantin Belousov vm_pindex_t backing_offset_index, new_pindex, pi, ps; 15664cc8daf7SConrad Meyer 15674cc8daf7SConrad Meyer VM_OBJECT_ASSERT_WLOCKED(object); 15684cc8daf7SConrad Meyer VM_OBJECT_ASSERT_WLOCKED(object->backing_object); 15694cc8daf7SConrad Meyer 15704cc8daf7SConrad Meyer backing_object = object->backing_object; 15714cc8daf7SConrad Meyer 157263967687SJeff Roberson if ((backing_object->flags & OBJ_ANON) == 0) 15734cc8daf7SConrad Meyer return (false); 15744cc8daf7SConrad Meyer 157577d6fd97SKonstantin Belousov pi = backing_offset_index = OFF_TO_IDX(object->backing_object_offset); 157677d6fd97SKonstantin Belousov p = vm_page_find_least(backing_object, pi); 157777d6fd97SKonstantin Belousov ps = swap_pager_find_least(backing_object, pi); 15784cc8daf7SConrad Meyer 15794cc8daf7SConrad Meyer /* 158077d6fd97SKonstantin Belousov * Only check pages inside the parent object's range and 158177d6fd97SKonstantin Belousov * inside the parent object's mapping of the backing object. 15824cc8daf7SConrad Meyer */ 158377d6fd97SKonstantin Belousov for (;; pi++) { 158477d6fd97SKonstantin Belousov if (p != NULL && p->pindex < pi) 158577d6fd97SKonstantin Belousov p = TAILQ_NEXT(p, listq); 158677d6fd97SKonstantin Belousov if (ps < pi) 158777d6fd97SKonstantin Belousov ps = swap_pager_find_least(backing_object, pi); 158877d6fd97SKonstantin Belousov if (p == NULL && ps >= backing_object->size) 158977d6fd97SKonstantin Belousov break; 159077d6fd97SKonstantin Belousov else if (p == NULL) 159177d6fd97SKonstantin Belousov pi = ps; 159277d6fd97SKonstantin Belousov else 159377d6fd97SKonstantin Belousov pi = MIN(p->pindex, ps); 159477d6fd97SKonstantin Belousov 159577d6fd97SKonstantin Belousov new_pindex = pi - backing_offset_index; 159677d6fd97SKonstantin Belousov if (new_pindex >= object->size) 159777d6fd97SKonstantin Belousov break; 15984cc8daf7SConrad Meyer 15994cc8daf7SConrad Meyer /* 16004cc8daf7SConrad Meyer * See if the parent has the page or if the parent's object 16014cc8daf7SConrad Meyer * pager has the page. If the parent has the page but the page 16024cc8daf7SConrad Meyer * is not valid, the parent's object pager must have the page. 16034cc8daf7SConrad Meyer * 16044cc8daf7SConrad Meyer * If this fails, the parent does not completely shadow the 16054cc8daf7SConrad Meyer * object and we might as well give up now. 16064cc8daf7SConrad Meyer */ 16074cc8daf7SConrad Meyer pp = vm_page_lookup(object, new_pindex); 16080012f373SJeff Roberson /* 16090012f373SJeff Roberson * The valid check here is stable due to object lock being 16100012f373SJeff Roberson * required to clear valid and initiate paging. 16110012f373SJeff Roberson */ 16120012f373SJeff Roberson if ((pp == NULL || vm_page_none_valid(pp)) && 16134cc8daf7SConrad Meyer !vm_pager_has_page(object, new_pindex, NULL, NULL)) 16144cc8daf7SConrad Meyer return (false); 16154cc8daf7SConrad Meyer } 16164cc8daf7SConrad Meyer return (true); 16174cc8daf7SConrad Meyer } 16184cc8daf7SConrad Meyer 16194cc8daf7SConrad Meyer static bool 16204cc8daf7SConrad Meyer vm_object_collapse_scan(vm_object_t object, int op) 16212ad1a3f7SMatthew Dillon { 16222ad1a3f7SMatthew Dillon vm_object_t backing_object; 162399a1570aSKonstantin Belousov vm_page_t next, p, pp; 162499a1570aSKonstantin Belousov vm_pindex_t backing_offset_index, new_pindex; 16252ad1a3f7SMatthew Dillon 162689f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 162789f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object->backing_object); 16282ad1a3f7SMatthew Dillon 16292ad1a3f7SMatthew Dillon backing_object = object->backing_object; 16302ad1a3f7SMatthew Dillon backing_offset_index = OFF_TO_IDX(object->backing_object_offset); 16312ad1a3f7SMatthew Dillon 16322ad1a3f7SMatthew Dillon /* 16332ad1a3f7SMatthew Dillon * Initial conditions 16342ad1a3f7SMatthew Dillon */ 16354cc8daf7SConrad Meyer if ((op & OBSC_COLLAPSE_WAIT) != 0) 16362ad1a3f7SMatthew Dillon vm_object_set_flag(backing_object, OBJ_DEAD); 16372ad1a3f7SMatthew Dillon 16382ad1a3f7SMatthew Dillon /* 16392ad1a3f7SMatthew Dillon * Our scan 16402ad1a3f7SMatthew Dillon */ 16414cc8daf7SConrad Meyer for (p = TAILQ_FIRST(&backing_object->memq); p != NULL; p = next) { 164299a1570aSKonstantin Belousov next = TAILQ_NEXT(p, listq); 164399a1570aSKonstantin Belousov new_pindex = p->pindex - backing_offset_index; 16442ad1a3f7SMatthew Dillon 16452ad1a3f7SMatthew Dillon /* 16462ad1a3f7SMatthew Dillon * Check for busy page 16472ad1a3f7SMatthew Dillon */ 164863e97555SJeff Roberson if (vm_page_tryxbusy(p) == 0) { 16494cc8daf7SConrad Meyer next = vm_object_collapse_scan_wait(object, p, next, op); 16502ad1a3f7SMatthew Dillon continue; 16512ad1a3f7SMatthew Dillon } 16522ad1a3f7SMatthew Dillon 165399a1570aSKonstantin Belousov KASSERT(p->object == backing_object, 16544cc8daf7SConrad Meyer ("vm_object_collapse_scan: object mismatch")); 16552ad1a3f7SMatthew Dillon 165699a1570aSKonstantin Belousov if (p->pindex < backing_offset_index || 165799a1570aSKonstantin Belousov new_pindex >= object->size) { 1658e946b949SAttilio Rao if (backing_object->type == OBJT_SWAP) 16594cc8daf7SConrad Meyer swap_pager_freespace(backing_object, p->pindex, 16604cc8daf7SConrad Meyer 1); 1661e946b949SAttilio Rao 1662f6d89838SAlan Cox KASSERT(!pmap_page_is_mapped(p), 1663f6d89838SAlan Cox ("freeing mapped page %p", p)); 16640fd977b3SMark Johnston if (vm_page_remove(p)) 16652ad1a3f7SMatthew Dillon vm_page_free(p); 16668da1c098SJeff Roberson else 16678da1c098SJeff Roberson vm_page_xunbusy(p); 16682ad1a3f7SMatthew Dillon continue; 16692ad1a3f7SMatthew Dillon } 16702ad1a3f7SMatthew Dillon 16712ad1a3f7SMatthew Dillon pp = vm_page_lookup(object, new_pindex); 167263e97555SJeff Roberson if (pp != NULL && vm_page_tryxbusy(pp) == 0) { 167363e97555SJeff Roberson vm_page_xunbusy(p); 1674e18cc7bfSMax Laier /* 16754cc8daf7SConrad Meyer * The page in the parent is busy and possibly not 16764cc8daf7SConrad Meyer * (yet) valid. Until its state is finalized by the 16774cc8daf7SConrad Meyer * busy bit owner, we can't tell whether it shadows the 16784cc8daf7SConrad Meyer * original page. Therefore, we must either skip it 16794cc8daf7SConrad Meyer * and the original (backing_object) page or wait for 16804cc8daf7SConrad Meyer * its state to be finalized. 1681e18cc7bfSMax Laier * 16824cc8daf7SConrad Meyer * This is due to a race with vm_fault() where we must 16834cc8daf7SConrad Meyer * unbusy the original (backing_obj) page before we can 16844cc8daf7SConrad Meyer * (re)lock the parent. Hence we can get here. 1685e18cc7bfSMax Laier */ 16864cc8daf7SConrad Meyer next = vm_object_collapse_scan_wait(object, pp, next, 16874cc8daf7SConrad Meyer op); 1688e18cc7bfSMax Laier continue; 1689e18cc7bfSMax Laier } 169099a1570aSKonstantin Belousov 16910012f373SJeff Roberson KASSERT(pp == NULL || !vm_page_none_valid(pp), 169299a1570aSKonstantin Belousov ("unbusy invalid page %p", pp)); 169399a1570aSKonstantin Belousov 16944cc8daf7SConrad Meyer if (pp != NULL || vm_pager_has_page(object, new_pindex, NULL, 16954cc8daf7SConrad Meyer NULL)) { 169699a1570aSKonstantin Belousov /* 16974cc8daf7SConrad Meyer * The page already exists in the parent OR swap exists 16984cc8daf7SConrad Meyer * for this location in the parent. Leave the parent's 16994cc8daf7SConrad Meyer * page alone. Destroy the original page from the 17004cc8daf7SConrad Meyer * backing object. 170199a1570aSKonstantin Belousov */ 1702e946b949SAttilio Rao if (backing_object->type == OBJT_SWAP) 17034cc8daf7SConrad Meyer swap_pager_freespace(backing_object, p->pindex, 17044cc8daf7SConrad Meyer 1); 1705f6d89838SAlan Cox KASSERT(!pmap_page_is_mapped(p), 1706f6d89838SAlan Cox ("freeing mapped page %p", p)); 17070fd977b3SMark Johnston if (vm_page_remove(p)) 17082ad1a3f7SMatthew Dillon vm_page_free(p); 17098da1c098SJeff Roberson else 17108da1c098SJeff Roberson vm_page_xunbusy(p); 171163e97555SJeff Roberson if (pp != NULL) 171263e97555SJeff Roberson vm_page_xunbusy(pp); 17132ad1a3f7SMatthew Dillon continue; 17142ad1a3f7SMatthew Dillon } 17152ad1a3f7SMatthew Dillon 1716e946b949SAttilio Rao /* 17174cc8daf7SConrad Meyer * Page does not exist in parent, rename the page from the 17184cc8daf7SConrad Meyer * backing object to the main object. 1719e946b949SAttilio Rao * 17204cc8daf7SConrad Meyer * If the page was mapped to a process, it can remain mapped 17213453bca8SAlan Cox * through the rename. vm_page_rename() will dirty the page. 1722e946b949SAttilio Rao */ 1723e946b949SAttilio Rao if (vm_page_rename(p, object, new_pindex)) { 172463e97555SJeff Roberson vm_page_xunbusy(p); 172563e97555SJeff Roberson if (pp != NULL) 172663e97555SJeff Roberson vm_page_xunbusy(pp); 17274cc8daf7SConrad Meyer next = vm_object_collapse_scan_wait(object, NULL, next, 17284cc8daf7SConrad Meyer op); 1729e946b949SAttilio Rao continue; 1730e946b949SAttilio Rao } 173114a5dc17SAttilio Rao 173214a5dc17SAttilio Rao /* Use the old pindex to free the right page. */ 1733e946b949SAttilio Rao if (backing_object->type == OBJT_SWAP) 173414a5dc17SAttilio Rao swap_pager_freespace(backing_object, 173514a5dc17SAttilio Rao new_pindex + backing_offset_index, 1); 1736e946b949SAttilio Rao 1737f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 1738f8a47341SAlan Cox /* 1739f8a47341SAlan Cox * Rename the reservation. 1740f8a47341SAlan Cox */ 1741f8a47341SAlan Cox vm_reserv_rename(p, object, backing_object, 1742f8a47341SAlan Cox backing_offset_index); 1743f8a47341SAlan Cox #endif 17448da1c098SJeff Roberson vm_page_xunbusy(p); 17452ad1a3f7SMatthew Dillon } 174699a1570aSKonstantin Belousov return (true); 17472ad1a3f7SMatthew Dillon } 17482ad1a3f7SMatthew Dillon 1749df8bae1dSRodney W. Grimes 1750df8bae1dSRodney W. Grimes /* 17512fe6e4d7SDavid Greenman * this version of collapse allows the operation to occur earlier and 17522fe6e4d7SDavid Greenman * when paging_in_progress is true for an object... This is not a complete 17532fe6e4d7SDavid Greenman * operation, but should plug 99.9% of the rest of the leaks. 17542fe6e4d7SDavid Greenman */ 17552fe6e4d7SDavid Greenman static void 17561b40f8c0SMatthew Dillon vm_object_qcollapse(vm_object_t object) 17572fe6e4d7SDavid Greenman { 17582ad1a3f7SMatthew Dillon vm_object_t backing_object = object->backing_object; 17592fe6e4d7SDavid Greenman 176089f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 176189f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(backing_object); 17621b40f8c0SMatthew Dillon 17632fe6e4d7SDavid Greenman if (backing_object->ref_count != 1) 17642fe6e4d7SDavid Greenman return; 17652fe6e4d7SDavid Greenman 17664cc8daf7SConrad Meyer vm_object_collapse_scan(object, OBSC_COLLAPSE_NOWAIT); 17672fe6e4d7SDavid Greenman } 17682fe6e4d7SDavid Greenman 1769df8bae1dSRodney W. Grimes /* 1770df8bae1dSRodney W. Grimes * vm_object_collapse: 1771df8bae1dSRodney W. Grimes * 1772df8bae1dSRodney W. Grimes * Collapse an object with the object backing it. 1773df8bae1dSRodney W. Grimes * Pages in the backing object are moved into the 1774df8bae1dSRodney W. Grimes * parent, and the backing object is deallocated. 1775df8bae1dSRodney W. Grimes */ 177626f9a767SRodney W. Grimes void 17771b40f8c0SMatthew Dillon vm_object_collapse(vm_object_t object) 1778df8bae1dSRodney W. Grimes { 177998f139daSKonstantin Belousov vm_object_t backing_object, new_backing_object; 178098f139daSKonstantin Belousov 178189f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 178223955314SAlfred Perlstein 1783df8bae1dSRodney W. Grimes while (TRUE) { 1784df8bae1dSRodney W. Grimes /* 1785df8bae1dSRodney W. Grimes * Verify that the conditions are right for collapse: 1786df8bae1dSRodney W. Grimes * 17872ad1a3f7SMatthew Dillon * The object exists and the backing object exists. 1788df8bae1dSRodney W. Grimes */ 178924a1cce3SDavid Greenman if ((backing_object = object->backing_object) == NULL) 17902ad1a3f7SMatthew Dillon break; 1791df8bae1dSRodney W. Grimes 1792f919ebdeSDavid Greenman /* 1793f919ebdeSDavid Greenman * we check the backing object first, because it is most likely 179424a1cce3SDavid Greenman * not collapsable. 1795f919ebdeSDavid Greenman */ 179663967687SJeff Roberson if ((backing_object->flags & OBJ_ANON) == 0) 179763967687SJeff Roberson break; 179889f6b863SAttilio Rao VM_OBJECT_WLOCK(backing_object); 179932362449SKonstantin Belousov if ((backing_object->flags & OBJ_DEAD) != 0 || 180032362449SKonstantin Belousov (object->flags & (OBJ_DEAD | OBJ_ANON)) != OBJ_ANON) { 180189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 18022ad1a3f7SMatthew Dillon break; 180324a1cce3SDavid Greenman } 18049b4814bbSDavid Greenman 180511b57401SHans Petter Selasky if (REFCOUNT_COUNT(object->paging_in_progress) > 0 || 180611b57401SHans Petter Selasky REFCOUNT_COUNT(backing_object->paging_in_progress) > 0) { 1807b9921222SDavid Greenman vm_object_qcollapse(object); 180889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 18092ad1a3f7SMatthew Dillon break; 1810df8bae1dSRodney W. Grimes } 181198f139daSKonstantin Belousov 181226f9a767SRodney W. Grimes /* 18130d94caffSDavid Greenman * We know that we can either collapse the backing object (if 18142ad1a3f7SMatthew Dillon * the parent is the only reference to it) or (perhaps) have 18152ad1a3f7SMatthew Dillon * the parent bypass the object if the parent happens to shadow 18162ad1a3f7SMatthew Dillon * all the resident pages in the entire backing object. 18172ad1a3f7SMatthew Dillon * 18182ad1a3f7SMatthew Dillon * This is ignoring pager-backed pages such as swap pages. 18194cc8daf7SConrad Meyer * vm_object_collapse_scan fails the shadowing test in this 18202ad1a3f7SMatthew Dillon * case. 1821df8bae1dSRodney W. Grimes */ 1822df8bae1dSRodney W. Grimes if (backing_object->ref_count == 1) { 1823aa9bc3b1SKonstantin Belousov vm_object_pip_add(object, 1); 1824aa9bc3b1SKonstantin Belousov vm_object_pip_add(backing_object, 1); 1825aa9bc3b1SKonstantin Belousov 1826df8bae1dSRodney W. Grimes /* 18272ad1a3f7SMatthew Dillon * If there is exactly one reference to the backing 18282ad1a3f7SMatthew Dillon * object, we can collapse it into the parent. 1829df8bae1dSRodney W. Grimes */ 18304cc8daf7SConrad Meyer vm_object_collapse_scan(object, OBSC_COLLAPSE_WAIT); 1831df8bae1dSRodney W. Grimes 1832f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 1833f8a47341SAlan Cox /* 1834f8a47341SAlan Cox * Break any reservations from backing_object. 1835f8a47341SAlan Cox */ 1836f8a47341SAlan Cox if (__predict_false(!LIST_EMPTY(&backing_object->rvq))) 1837f8a47341SAlan Cox vm_reserv_break_all(backing_object); 1838f8a47341SAlan Cox #endif 1839f8a47341SAlan Cox 1840df8bae1dSRodney W. Grimes /* 1841df8bae1dSRodney W. Grimes * Move the pager from backing_object to object. 1842df8bae1dSRodney W. Grimes */ 18436be36525SAlan Cox if (backing_object->type == OBJT_SWAP) { 184424a1cce3SDavid Greenman /* 1845c7c8dd7eSAlan Cox * swap_pager_copy() can sleep, in which case 1846c7c8dd7eSAlan Cox * the backing_object's and object's locks are 1847c7c8dd7eSAlan Cox * released and reacquired. 1848571a1e92SAttilio Rao * Since swap_pager_copy() is being asked to 1849571a1e92SAttilio Rao * destroy the source, it will change the 1850571a1e92SAttilio Rao * backing_object's type to OBJT_DEFAULT. 185124a1cce3SDavid Greenman */ 18521c7c3c6aSMatthew Dillon swap_pager_copy( 18531c7c3c6aSMatthew Dillon backing_object, 18541c7c3c6aSMatthew Dillon object, 18551c7c3c6aSMatthew Dillon OFF_TO_IDX(object->backing_object_offset), TRUE); 1856c0503609SDavid Greenman } 1857df8bae1dSRodney W. Grimes /* 1858df8bae1dSRodney W. Grimes * Object now shadows whatever backing_object did. 18592ad1a3f7SMatthew Dillon * Note that the reference to 18602ad1a3f7SMatthew Dillon * backing_object->backing_object moves from within 18612ad1a3f7SMatthew Dillon * backing_object to within object. 1862df8bae1dSRodney W. Grimes */ 186351b867e5SJeff Roberson vm_object_backing_remove_locked(object); 186451b867e5SJeff Roberson new_backing_object = backing_object->backing_object; 186551b867e5SJeff Roberson if (new_backing_object != NULL) { 186651b867e5SJeff Roberson VM_OBJECT_WLOCK(new_backing_object); 186751b867e5SJeff Roberson vm_object_backing_remove_locked(backing_object); 186851b867e5SJeff Roberson vm_object_backing_insert_locked(object, 186951b867e5SJeff Roberson new_backing_object); 187051b867e5SJeff Roberson VM_OBJECT_WUNLOCK(new_backing_object); 1871de5f6a77SJohn Dyson } 18722ad1a3f7SMatthew Dillon object->backing_object_offset += 18732ad1a3f7SMatthew Dillon backing_object->backing_object_offset; 18742ad1a3f7SMatthew Dillon 1875df8bae1dSRodney W. Grimes /* 1876df8bae1dSRodney W. Grimes * Discard backing_object. 1877df8bae1dSRodney W. Grimes * 18780d94caffSDavid Greenman * Since the backing object has no pages, no pager left, 18790d94caffSDavid Greenman * and no object references within it, all that is 18800d94caffSDavid Greenman * necessary is to dispose of it. 1881df8bae1dSRodney W. Grimes */ 18829b4d473aSKonstantin Belousov KASSERT(backing_object->ref_count == 1, ( 18839b4d473aSKonstantin Belousov "backing_object %p was somehow re-referenced during collapse!", 18849b4d473aSKonstantin Belousov backing_object)); 1885aa9bc3b1SKonstantin Belousov vm_object_pip_wakeup(backing_object); 1886e735691bSJohn Baldwin backing_object->type = OBJT_DEAD; 1887a67d5408SJeff Roberson refcount_release(&backing_object->ref_count); 188889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 18899b4d473aSKonstantin Belousov vm_object_destroy(backing_object); 1890df8bae1dSRodney W. Grimes 1891aa9bc3b1SKonstantin Belousov vm_object_pip_wakeup(object); 189211542376SAlan Cox counter_u64_add(object_collapses, 1); 18930d94caffSDavid Greenman } else { 1894df8bae1dSRodney W. Grimes /* 18952ad1a3f7SMatthew Dillon * If we do not entirely shadow the backing object, 18962ad1a3f7SMatthew Dillon * there is nothing we can do so we give up. 1897df8bae1dSRodney W. Grimes */ 1898df59a0feSJeff Roberson if (object->resident_page_count != object->size && 18994cc8daf7SConrad Meyer !vm_object_scan_all_shadowed(object)) { 190089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 19012ad1a3f7SMatthew Dillon break; 190224a1cce3SDavid Greenman } 1903df8bae1dSRodney W. Grimes 1904df8bae1dSRodney W. Grimes /* 19050d94caffSDavid Greenman * Make the parent shadow the next object in the 19060d94caffSDavid Greenman * chain. Deallocating backing_object will not remove 19070d94caffSDavid Greenman * it, since its reference count is at least 2. 1908df8bae1dSRodney W. Grimes */ 190951b867e5SJeff Roberson vm_object_backing_remove_locked(object); 191095e5e988SJohn Dyson 191195e5e988SJohn Dyson new_backing_object = backing_object->backing_object; 191251b867e5SJeff Roberson if (new_backing_object != NULL) { 191351b867e5SJeff Roberson vm_object_backing_insert(object, 191451b867e5SJeff Roberson new_backing_object); 191551b867e5SJeff Roberson vm_object_reference(new_backing_object); 191695e5e988SJohn Dyson object->backing_object_offset += 191795e5e988SJohn Dyson backing_object->backing_object_offset; 1918de5f6a77SJohn Dyson } 1919df8bae1dSRodney W. Grimes 1920df8bae1dSRodney W. Grimes /* 19210d94caffSDavid Greenman * Drop the reference count on backing_object. Since 192222ec553fSAlan Cox * its ref_count was at least 2, it will not vanish. 1923df8bae1dSRodney W. Grimes */ 192451df5321SJeff Roberson refcount_release(&backing_object->ref_count); 192589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 192611542376SAlan Cox counter_u64_add(object_bypasses, 1); 1927df8bae1dSRodney W. Grimes } 1928df8bae1dSRodney W. Grimes 1929df8bae1dSRodney W. Grimes /* 1930df8bae1dSRodney W. Grimes * Try again with this object's new backing object. 1931df8bae1dSRodney W. Grimes */ 1932df8bae1dSRodney W. Grimes } 1933df8bae1dSRodney W. Grimes } 1934df8bae1dSRodney W. Grimes 1935df8bae1dSRodney W. Grimes /* 1936bff99f0dSAlan Cox * vm_object_page_remove: 1937df8bae1dSRodney W. Grimes * 193868855966SAlan Cox * For the given object, either frees or invalidates each of the 19396bbee8e2SAlan Cox * specified pages. In general, a page is freed. However, if a page is 19406bbee8e2SAlan Cox * wired for any reason other than the existence of a managed, wired 19416bbee8e2SAlan Cox * mapping, then it may be invalidated but not removed from the object. 19426bbee8e2SAlan Cox * Pages are specified by the given range ["start", "end") and the option 19436bbee8e2SAlan Cox * OBJPR_CLEANONLY. As a special case, if "end" is zero, then the range 19446bbee8e2SAlan Cox * extends from "start" to the end of the object. If the option 19456bbee8e2SAlan Cox * OBJPR_CLEANONLY is specified, then only the non-dirty pages within the 19466bbee8e2SAlan Cox * specified range are affected. If the option OBJPR_NOTMAPPED is 19476bbee8e2SAlan Cox * specified, then the pages within the specified range must have no 19486bbee8e2SAlan Cox * mappings. Otherwise, if this option is not specified, any mappings to 19496bbee8e2SAlan Cox * the specified pages are removed before the pages are freed or 19506bbee8e2SAlan Cox * invalidated. 195168855966SAlan Cox * 19526bbee8e2SAlan Cox * In general, this operation should only be performed on objects that 19536bbee8e2SAlan Cox * contain managed pages. There are, however, two exceptions. First, it 19546bbee8e2SAlan Cox * is performed on the kernel and kmem objects by vm_map_entry_delete(). 19556bbee8e2SAlan Cox * Second, it is used by msync(..., MS_INVALIDATE) to invalidate device- 19566bbee8e2SAlan Cox * backed pages. In both of these cases, the option OBJPR_CLEANONLY must 19576bbee8e2SAlan Cox * not be specified and the option OBJPR_NOTMAPPED must be specified. 1958df8bae1dSRodney W. Grimes * 1959df8bae1dSRodney W. Grimes * The object must be locked. 1960df8bae1dSRodney W. Grimes */ 196126f9a767SRodney W. Grimes void 1962ecde4b32SAlan Cox vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end, 19636bbee8e2SAlan Cox int options) 1964df8bae1dSRodney W. Grimes { 1965d031cff1SMatthew Dillon vm_page_t p, next; 1966df8bae1dSRodney W. Grimes 196789f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 196828634820SAlan Cox KASSERT((object->flags & OBJ_UNMANAGED) == 0 || 19696bbee8e2SAlan Cox (options & (OBJPR_CLEANONLY | OBJPR_NOTMAPPED)) == OBJPR_NOTMAPPED, 19706bbee8e2SAlan Cox ("vm_object_page_remove: illegal options for object %p", object)); 1971ecde4b32SAlan Cox if (object->resident_page_count == 0) 19727667839aSAlan Cox return; 1973d474eaaaSDoug Rabson vm_object_pip_add(object, 1); 197426f9a767SRodney W. Grimes again: 1975b382c10aSKonstantin Belousov p = vm_page_find_least(object, start); 19762965a453SKip Macy 197775741c04SAlan Cox /* 19786bbee8e2SAlan Cox * Here, the variable "p" is either (1) the page with the least pindex 19796bbee8e2SAlan Cox * greater than or equal to the parameter "start" or (2) NULL. 198075741c04SAlan Cox */ 19816bbee8e2SAlan Cox for (; p != NULL && (p->pindex < end || end == 0); p = next) { 1982b18bfc3dSJohn Dyson next = TAILQ_NEXT(p, listq); 198375741c04SAlan Cox 198459677d3cSAlan Cox /* 19856bbee8e2SAlan Cox * If the page is wired for any reason besides the existence 19866bbee8e2SAlan Cox * of managed, wired mappings, then it cannot be freed. For 19876bbee8e2SAlan Cox * example, fictitious pages, which represent device memory, 19886bbee8e2SAlan Cox * are inherently wired and cannot be freed. They can, 19896bbee8e2SAlan Cox * however, be invalidated if the option OBJPR_CLEANONLY is 19906bbee8e2SAlan Cox * not specified. 199159677d3cSAlan Cox */ 199263e97555SJeff Roberson if (vm_page_tryxbusy(p) == 0) { 19934cdea4a8SJeff Roberson vm_page_sleep_if_busy(p, "vmopar"); 1994fee2a2faSMark Johnston goto again; 1995fee2a2faSMark Johnston } 1996d842aa51SMark Johnston if (vm_page_wired(p)) { 1997fee2a2faSMark Johnston wired: 1998cf060942SAlan Cox if ((options & OBJPR_NOTMAPPED) == 0 && 1999cf060942SAlan Cox object->ref_count != 0) 20004fec79beSAlan Cox pmap_remove_all(p); 20016bbee8e2SAlan Cox if ((options & OBJPR_CLEANONLY) == 0) { 20020012f373SJeff Roberson vm_page_invalid(p); 2003a28042d1SAlan Cox vm_page_undirty(p); 2004a28042d1SAlan Cox } 200563e97555SJeff Roberson vm_page_xunbusy(p); 200693c5d3a4SKonstantin Belousov continue; 20070d94caffSDavid Greenman } 200868855966SAlan Cox KASSERT((p->flags & PG_FICTITIOUS) == 0, 200968855966SAlan Cox ("vm_object_page_remove: page %p is fictitious", p)); 20100012f373SJeff Roberson if ((options & OBJPR_CLEANONLY) != 0 && 20110012f373SJeff Roberson !vm_page_none_valid(p)) { 2012cf060942SAlan Cox if ((options & OBJPR_NOTMAPPED) == 0 && 2013fee2a2faSMark Johnston object->ref_count != 0 && 2014fee2a2faSMark Johnston !vm_page_try_remove_write(p)) 2015fee2a2faSMark Johnston goto wired; 201663e97555SJeff Roberson if (p->dirty != 0) { 201763e97555SJeff Roberson vm_page_xunbusy(p); 201893c5d3a4SKonstantin Belousov continue; 20192965a453SKip Macy } 202063e97555SJeff Roberson } 2021fee2a2faSMark Johnston if ((options & OBJPR_NOTMAPPED) == 0 && 2022fee2a2faSMark Johnston object->ref_count != 0 && !vm_page_try_remove_all(p)) 2023fee2a2faSMark Johnston goto wired; 20245cd29d0fSMark Johnston vm_page_free(p); 20252965a453SKip Macy } 2026f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 2027c0503609SDavid Greenman } 2028df8bae1dSRodney W. Grimes 2029df8bae1dSRodney W. Grimes /* 20303138cd36SMark Johnston * vm_object_page_noreuse: 2031936c09acSJohn Baldwin * 20323138cd36SMark Johnston * For the given object, attempt to move the specified pages to 20333138cd36SMark Johnston * the head of the inactive queue. This bypasses regular LRU 20343138cd36SMark Johnston * operation and allows the pages to be reused quickly under memory 20353138cd36SMark Johnston * pressure. If a page is wired for any reason, then it will not 20363138cd36SMark Johnston * be queued. Pages are specified by the range ["start", "end"). 20373138cd36SMark Johnston * As a special case, if "end" is zero, then the range extends from 20383138cd36SMark Johnston * "start" to the end of the object. 2039936c09acSJohn Baldwin * 2040936c09acSJohn Baldwin * This operation should only be performed on objects that 204128634820SAlan Cox * contain non-fictitious, managed pages. 2042936c09acSJohn Baldwin * 2043936c09acSJohn Baldwin * The object must be locked. 2044936c09acSJohn Baldwin */ 2045936c09acSJohn Baldwin void 20463138cd36SMark Johnston vm_object_page_noreuse(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 2047936c09acSJohn Baldwin { 204893c5d3a4SKonstantin Belousov struct mtx *mtx; 2049936c09acSJohn Baldwin vm_page_t p, next; 2050936c09acSJohn Baldwin 205152d1addaSAlan Cox VM_OBJECT_ASSERT_LOCKED(object); 205228634820SAlan Cox KASSERT((object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0, 20533138cd36SMark Johnston ("vm_object_page_noreuse: illegal object %p", object)); 2054936c09acSJohn Baldwin if (object->resident_page_count == 0) 2055936c09acSJohn Baldwin return; 2056936c09acSJohn Baldwin p = vm_page_find_least(object, start); 2057936c09acSJohn Baldwin 2058936c09acSJohn Baldwin /* 2059936c09acSJohn Baldwin * Here, the variable "p" is either (1) the page with the least pindex 2060936c09acSJohn Baldwin * greater than or equal to the parameter "start" or (2) NULL. 2061936c09acSJohn Baldwin */ 2062936c09acSJohn Baldwin mtx = NULL; 2063936c09acSJohn Baldwin for (; p != NULL && (p->pindex < end || end == 0); p = next) { 2064936c09acSJohn Baldwin next = TAILQ_NEXT(p, listq); 206593c5d3a4SKonstantin Belousov vm_page_change_lock(p, &mtx); 20663138cd36SMark Johnston vm_page_deactivate_noreuse(p); 2067936c09acSJohn Baldwin } 2068936c09acSJohn Baldwin if (mtx != NULL) 2069936c09acSJohn Baldwin mtx_unlock(mtx); 2070936c09acSJohn Baldwin } 2071936c09acSJohn Baldwin 2072936c09acSJohn Baldwin /* 2073387aabc5SAlan Cox * Populate the specified range of the object with valid pages. Returns 2074387aabc5SAlan Cox * TRUE if the range is successfully populated and FALSE otherwise. 2075387aabc5SAlan Cox * 2076387aabc5SAlan Cox * Note: This function should be optimized to pass a larger array of 2077387aabc5SAlan Cox * pages to vm_pager_get_pages() before it is applied to a non- 2078387aabc5SAlan Cox * OBJT_DEVICE object. 2079387aabc5SAlan Cox * 2080387aabc5SAlan Cox * The object must be locked. 2081387aabc5SAlan Cox */ 2082387aabc5SAlan Cox boolean_t 2083387aabc5SAlan Cox vm_object_populate(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 2084387aabc5SAlan Cox { 2085093c7f39SGleb Smirnoff vm_page_t m; 2086387aabc5SAlan Cox vm_pindex_t pindex; 2087387aabc5SAlan Cox int rv; 2088387aabc5SAlan Cox 208989f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 2090387aabc5SAlan Cox for (pindex = start; pindex < end; pindex++) { 2091c7575748SJeff Roberson rv = vm_page_grab_valid(&m, object, pindex, VM_ALLOC_NORMAL); 2092c7575748SJeff Roberson if (rv != VM_PAGER_OK) 2093387aabc5SAlan Cox break; 2094c7575748SJeff Roberson 2095387aabc5SAlan Cox /* 2096387aabc5SAlan Cox * Keep "m" busy because a subsequent iteration may unlock 2097387aabc5SAlan Cox * the object. 2098387aabc5SAlan Cox */ 2099387aabc5SAlan Cox } 2100387aabc5SAlan Cox if (pindex > start) { 2101387aabc5SAlan Cox m = vm_page_lookup(object, start); 2102387aabc5SAlan Cox while (m != NULL && m->pindex < pindex) { 2103c7aebda8SAttilio Rao vm_page_xunbusy(m); 2104387aabc5SAlan Cox m = TAILQ_NEXT(m, listq); 2105387aabc5SAlan Cox } 2106387aabc5SAlan Cox } 2107387aabc5SAlan Cox return (pindex == end); 2108387aabc5SAlan Cox } 2109387aabc5SAlan Cox 2110387aabc5SAlan Cox /* 2111df8bae1dSRodney W. Grimes * Routine: vm_object_coalesce 2112df8bae1dSRodney W. Grimes * Function: Coalesces two objects backing up adjoining 2113df8bae1dSRodney W. Grimes * regions of memory into a single object. 2114df8bae1dSRodney W. Grimes * 2115df8bae1dSRodney W. Grimes * returns TRUE if objects were combined. 2116df8bae1dSRodney W. Grimes * 2117df8bae1dSRodney W. Grimes * NOTE: Only works at the moment if the second object is NULL - 2118df8bae1dSRodney W. Grimes * if it's not, which object do we lock first? 2119df8bae1dSRodney W. Grimes * 2120df8bae1dSRodney W. Grimes * Parameters: 2121df8bae1dSRodney W. Grimes * prev_object First object to coalesce 2122df8bae1dSRodney W. Grimes * prev_offset Offset into prev_object 2123df8bae1dSRodney W. Grimes * prev_size Size of reference to prev_object 212457a21abaSAlan Cox * next_size Size of reference to the second object 21253364c323SKonstantin Belousov * reserved Indicator that extension region has 21263364c323SKonstantin Belousov * swap accounted for 2127df8bae1dSRodney W. Grimes * 2128df8bae1dSRodney W. Grimes * Conditions: 2129df8bae1dSRodney W. Grimes * The object must *not* be locked. 2130df8bae1dSRodney W. Grimes */ 21310d94caffSDavid Greenman boolean_t 213257a21abaSAlan Cox vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset, 21333364c323SKonstantin Belousov vm_size_t prev_size, vm_size_t next_size, boolean_t reserved) 2134df8bae1dSRodney W. Grimes { 2135ea41812fSAlan Cox vm_pindex_t next_pindex; 2136df8bae1dSRodney W. Grimes 213700e1854aSAlan Cox if (prev_object == NULL) 2138df8bae1dSRodney W. Grimes return (TRUE); 213963967687SJeff Roberson if ((prev_object->flags & OBJ_ANON) == 0) 214030dcfc09SJohn Dyson return (FALSE); 214130dcfc09SJohn Dyson 214263967687SJeff Roberson VM_OBJECT_WLOCK(prev_object); 2143df8bae1dSRodney W. Grimes /* 2144df8bae1dSRodney W. Grimes * Try to collapse the object first 2145df8bae1dSRodney W. Grimes */ 2146df8bae1dSRodney W. Grimes vm_object_collapse(prev_object); 2147df8bae1dSRodney W. Grimes 2148df8bae1dSRodney W. Grimes /* 21490d94caffSDavid Greenman * Can't coalesce if: . more than one reference . paged out . shadows 21500d94caffSDavid Greenman * another object . has a copy elsewhere (any of which mean that the 21510d94caffSDavid Greenman * pages not mapped to prev_entry may be in use anyway) 2152df8bae1dSRodney W. Grimes */ 21538cc7e047SJohn Dyson if (prev_object->backing_object != NULL) { 215489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(prev_object); 2155df8bae1dSRodney W. Grimes return (FALSE); 2156df8bae1dSRodney W. Grimes } 2157a316d390SJohn Dyson 2158a316d390SJohn Dyson prev_size >>= PAGE_SHIFT; 2159a316d390SJohn Dyson next_size >>= PAGE_SHIFT; 216057a21abaSAlan Cox next_pindex = OFF_TO_IDX(prev_offset) + prev_size; 21618cc7e047SJohn Dyson 21620e48e068SMark Johnston if (prev_object->ref_count > 1 && 21630e48e068SMark Johnston prev_object->size != next_pindex && 21640e48e068SMark Johnston (prev_object->flags & OBJ_ONEMAPPING) == 0) { 216589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(prev_object); 21668cc7e047SJohn Dyson return (FALSE); 21678cc7e047SJohn Dyson } 21688cc7e047SJohn Dyson 2169df8bae1dSRodney W. Grimes /* 21703364c323SKonstantin Belousov * Account for the charge. 21713364c323SKonstantin Belousov */ 2172ef694c1aSEdward Tomasz Napierala if (prev_object->cred != NULL) { 21733364c323SKonstantin Belousov 21743364c323SKonstantin Belousov /* 21753364c323SKonstantin Belousov * If prev_object was charged, then this mapping, 2176763df3ecSPedro F. Giffuni * although not charged now, may become writable 2177ef694c1aSEdward Tomasz Napierala * later. Non-NULL cred in the object would prevent 21783364c323SKonstantin Belousov * swap reservation during enabling of the write 21793364c323SKonstantin Belousov * access, so reserve swap now. Failed reservation 21803364c323SKonstantin Belousov * cause allocation of the separate object for the map 21813364c323SKonstantin Belousov * entry, and swap reservation for this entry is 21823364c323SKonstantin Belousov * managed in appropriate time. 21833364c323SKonstantin Belousov */ 2184ef694c1aSEdward Tomasz Napierala if (!reserved && !swap_reserve_by_cred(ptoa(next_size), 2185ef694c1aSEdward Tomasz Napierala prev_object->cred)) { 21869f790a17SKonstantin Belousov VM_OBJECT_WUNLOCK(prev_object); 21873364c323SKonstantin Belousov return (FALSE); 21883364c323SKonstantin Belousov } 21893364c323SKonstantin Belousov prev_object->charge += ptoa(next_size); 21903364c323SKonstantin Belousov } 21913364c323SKonstantin Belousov 21923364c323SKonstantin Belousov /* 21930d94caffSDavid Greenman * Remove any pages that may still be in the object from a previous 21940d94caffSDavid Greenman * deallocation. 2195df8bae1dSRodney W. Grimes */ 2196ea41812fSAlan Cox if (next_pindex < prev_object->size) { 21976bbee8e2SAlan Cox vm_object_page_remove(prev_object, next_pindex, next_pindex + 21986bbee8e2SAlan Cox next_size, 0); 2199ea41812fSAlan Cox if (prev_object->type == OBJT_SWAP) 2200ea41812fSAlan Cox swap_pager_freespace(prev_object, 2201ea41812fSAlan Cox next_pindex, next_size); 22023364c323SKonstantin Belousov #if 0 2203ef694c1aSEdward Tomasz Napierala if (prev_object->cred != NULL) { 22043364c323SKonstantin Belousov KASSERT(prev_object->charge >= 22053364c323SKonstantin Belousov ptoa(prev_object->size - next_pindex), 22063364c323SKonstantin Belousov ("object %p overcharged 1 %jx %jx", prev_object, 22073364c323SKonstantin Belousov (uintmax_t)next_pindex, (uintmax_t)next_size)); 22083364c323SKonstantin Belousov prev_object->charge -= ptoa(prev_object->size - 22093364c323SKonstantin Belousov next_pindex); 22103364c323SKonstantin Belousov } 22113364c323SKonstantin Belousov #endif 2212ea41812fSAlan Cox } 2213df8bae1dSRodney W. Grimes 2214df8bae1dSRodney W. Grimes /* 2215df8bae1dSRodney W. Grimes * Extend the object if necessary. 2216df8bae1dSRodney W. Grimes */ 2217ea41812fSAlan Cox if (next_pindex + next_size > prev_object->size) 2218ea41812fSAlan Cox prev_object->size = next_pindex + next_size; 2219df8bae1dSRodney W. Grimes 222089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(prev_object); 2221df8bae1dSRodney W. Grimes return (TRUE); 2222df8bae1dSRodney W. Grimes } 2223df8bae1dSRodney W. Grimes 22247a5a6352SMatthew Dillon void 22257a5a6352SMatthew Dillon vm_object_set_writeable_dirty(vm_object_t object) 22267a5a6352SMatthew Dillon { 22277a5a6352SMatthew Dillon 222867d0e293SJeff Roberson VM_OBJECT_ASSERT_LOCKED(object); 222967d0e293SJeff Roberson 223067d0e293SJeff Roberson /* Only set for vnodes & tmpfs */ 223167d0e293SJeff Roberson if (object->type != OBJT_VNODE && 223267d0e293SJeff Roberson (object->flags & OBJ_TMPFS_NODE) == 0) 22333280870dSKonstantin Belousov return; 223467d0e293SJeff Roberson atomic_add_int(&object->generation, 1); 22357a5a6352SMatthew Dillon } 22367a5a6352SMatthew Dillon 223703462509SAlan Cox /* 223803462509SAlan Cox * vm_object_unwire: 223903462509SAlan Cox * 224003462509SAlan Cox * For each page offset within the specified range of the given object, 224103462509SAlan Cox * find the highest-level page in the shadow chain and unwire it. A page 224203462509SAlan Cox * must exist at every page offset, and the highest-level page must be 224303462509SAlan Cox * wired. 224403462509SAlan Cox */ 224503462509SAlan Cox void 224603462509SAlan Cox vm_object_unwire(vm_object_t object, vm_ooffset_t offset, vm_size_t length, 224703462509SAlan Cox uint8_t queue) 224803462509SAlan Cox { 224920e4afbfSKonstantin Belousov vm_object_t tobject, t1object; 225003462509SAlan Cox vm_page_t m, tm; 225103462509SAlan Cox vm_pindex_t end_pindex, pindex, tpindex; 225203462509SAlan Cox int depth, locked_depth; 225303462509SAlan Cox 225403462509SAlan Cox KASSERT((offset & PAGE_MASK) == 0, 225503462509SAlan Cox ("vm_object_unwire: offset is not page aligned")); 225603462509SAlan Cox KASSERT((length & PAGE_MASK) == 0, 225703462509SAlan Cox ("vm_object_unwire: length is not a multiple of PAGE_SIZE")); 225803462509SAlan Cox /* The wired count of a fictitious page never changes. */ 225903462509SAlan Cox if ((object->flags & OBJ_FICTITIOUS) != 0) 226003462509SAlan Cox return; 226103462509SAlan Cox pindex = OFF_TO_IDX(offset); 226203462509SAlan Cox end_pindex = pindex + atop(length); 226320e4afbfSKonstantin Belousov again: 226403462509SAlan Cox locked_depth = 1; 226503462509SAlan Cox VM_OBJECT_RLOCK(object); 226603462509SAlan Cox m = vm_page_find_least(object, pindex); 226703462509SAlan Cox while (pindex < end_pindex) { 226803462509SAlan Cox if (m == NULL || pindex < m->pindex) { 226903462509SAlan Cox /* 227003462509SAlan Cox * The first object in the shadow chain doesn't 227103462509SAlan Cox * contain a page at the current index. Therefore, 227203462509SAlan Cox * the page must exist in a backing object. 227303462509SAlan Cox */ 227403462509SAlan Cox tobject = object; 227503462509SAlan Cox tpindex = pindex; 227603462509SAlan Cox depth = 0; 227703462509SAlan Cox do { 227803462509SAlan Cox tpindex += 227903462509SAlan Cox OFF_TO_IDX(tobject->backing_object_offset); 228003462509SAlan Cox tobject = tobject->backing_object; 228103462509SAlan Cox KASSERT(tobject != NULL, 228203462509SAlan Cox ("vm_object_unwire: missing page")); 228303462509SAlan Cox if ((tobject->flags & OBJ_FICTITIOUS) != 0) 228403462509SAlan Cox goto next_page; 228503462509SAlan Cox depth++; 228603462509SAlan Cox if (depth == locked_depth) { 228703462509SAlan Cox locked_depth++; 228803462509SAlan Cox VM_OBJECT_RLOCK(tobject); 228903462509SAlan Cox } 229003462509SAlan Cox } while ((tm = vm_page_lookup(tobject, tpindex)) == 229103462509SAlan Cox NULL); 229203462509SAlan Cox } else { 229303462509SAlan Cox tm = m; 229403462509SAlan Cox m = TAILQ_NEXT(m, listq); 229503462509SAlan Cox } 229663e97555SJeff Roberson if (vm_page_trysbusy(tm) == 0) { 229787e93ea6SMark Johnston for (tobject = object; locked_depth >= 1; 229820e4afbfSKonstantin Belousov locked_depth--) { 229920e4afbfSKonstantin Belousov t1object = tobject->backing_object; 230087e93ea6SMark Johnston if (tm->object != tobject) 230120e4afbfSKonstantin Belousov VM_OBJECT_RUNLOCK(tobject); 230220e4afbfSKonstantin Belousov tobject = t1object; 230320e4afbfSKonstantin Belousov } 230420e4afbfSKonstantin Belousov vm_page_busy_sleep(tm, "unwbo", true); 230520e4afbfSKonstantin Belousov goto again; 230620e4afbfSKonstantin Belousov } 230703462509SAlan Cox vm_page_unwire(tm, queue); 230863e97555SJeff Roberson vm_page_sunbusy(tm); 230903462509SAlan Cox next_page: 231003462509SAlan Cox pindex++; 231103462509SAlan Cox } 231203462509SAlan Cox /* Release the accumulated object locks. */ 231320e4afbfSKonstantin Belousov for (tobject = object; locked_depth >= 1; locked_depth--) { 231420e4afbfSKonstantin Belousov t1object = tobject->backing_object; 231520e4afbfSKonstantin Belousov VM_OBJECT_RUNLOCK(tobject); 231620e4afbfSKonstantin Belousov tobject = t1object; 231703462509SAlan Cox } 231803462509SAlan Cox } 231903462509SAlan Cox 23200951bd36SEric van Gyzen /* 23210951bd36SEric van Gyzen * Return the vnode for the given object, or NULL if none exists. 23220951bd36SEric van Gyzen * For tmpfs objects, the function may return NULL if there is 23230951bd36SEric van Gyzen * no vnode allocated at the time of the call. 23240951bd36SEric van Gyzen */ 232563e4c6cdSEric van Gyzen struct vnode * 232663e4c6cdSEric van Gyzen vm_object_vnode(vm_object_t object) 232763e4c6cdSEric van Gyzen { 23280951bd36SEric van Gyzen struct vnode *vp; 232963e4c6cdSEric van Gyzen 233063e4c6cdSEric van Gyzen VM_OBJECT_ASSERT_LOCKED(object); 23310951bd36SEric van Gyzen if (object->type == OBJT_VNODE) { 23320951bd36SEric van Gyzen vp = object->handle; 23330951bd36SEric van Gyzen KASSERT(vp != NULL, ("%s: OBJT_VNODE has no vnode", __func__)); 23340951bd36SEric van Gyzen } else if (object->type == OBJT_SWAP && 23350951bd36SEric van Gyzen (object->flags & OBJ_TMPFS) != 0) { 23360951bd36SEric van Gyzen vp = object->un_pager.swp.swp_tmpfs; 23370951bd36SEric van Gyzen KASSERT(vp != NULL, ("%s: OBJT_TMPFS has no vnode", __func__)); 23380951bd36SEric van Gyzen } else { 23390951bd36SEric van Gyzen vp = NULL; 23400951bd36SEric van Gyzen } 23410951bd36SEric van Gyzen return (vp); 234263e4c6cdSEric van Gyzen } 234363e4c6cdSEric van Gyzen 2344205be21dSJeff Roberson 2345205be21dSJeff Roberson /* 2346205be21dSJeff Roberson * Busy the vm object. This prevents new pages belonging to the object from 2347205be21dSJeff Roberson * becoming busy. Existing pages persist as busy. Callers are responsible 2348205be21dSJeff Roberson * for checking page state before proceeding. 2349205be21dSJeff Roberson */ 2350205be21dSJeff Roberson void 2351205be21dSJeff Roberson vm_object_busy(vm_object_t obj) 2352205be21dSJeff Roberson { 2353205be21dSJeff Roberson 2354205be21dSJeff Roberson VM_OBJECT_ASSERT_LOCKED(obj); 2355205be21dSJeff Roberson 2356205be21dSJeff Roberson refcount_acquire(&obj->busy); 2357205be21dSJeff Roberson /* The fence is required to order loads of page busy. */ 2358205be21dSJeff Roberson atomic_thread_fence_acq_rel(); 2359205be21dSJeff Roberson } 2360205be21dSJeff Roberson 2361205be21dSJeff Roberson void 2362205be21dSJeff Roberson vm_object_unbusy(vm_object_t obj) 2363205be21dSJeff Roberson { 2364205be21dSJeff Roberson 2365205be21dSJeff Roberson 2366205be21dSJeff Roberson refcount_release(&obj->busy); 2367205be21dSJeff Roberson } 2368205be21dSJeff Roberson 2369205be21dSJeff Roberson void 2370205be21dSJeff Roberson vm_object_busy_wait(vm_object_t obj, const char *wmesg) 2371205be21dSJeff Roberson { 2372205be21dSJeff Roberson 2373205be21dSJeff Roberson VM_OBJECT_ASSERT_UNLOCKED(obj); 2374205be21dSJeff Roberson 2375205be21dSJeff Roberson if (obj->busy) 2376205be21dSJeff Roberson refcount_sleep(&obj->busy, wmesg, PVM); 2377205be21dSJeff Roberson } 2378205be21dSJeff Roberson 23795e38e3f5SEric van Gyzen /* 23805e38e3f5SEric van Gyzen * Return the kvme type of the given object. 23815e38e3f5SEric van Gyzen * If vpp is not NULL, set it to the object's vm_object_vnode() or NULL. 23825e38e3f5SEric van Gyzen */ 23835e38e3f5SEric van Gyzen int 23845e38e3f5SEric van Gyzen vm_object_kvme_type(vm_object_t object, struct vnode **vpp) 23855e38e3f5SEric van Gyzen { 23865e38e3f5SEric van Gyzen 23875e38e3f5SEric van Gyzen VM_OBJECT_ASSERT_LOCKED(object); 23885e38e3f5SEric van Gyzen if (vpp != NULL) 23895e38e3f5SEric van Gyzen *vpp = vm_object_vnode(object); 23905e38e3f5SEric van Gyzen switch (object->type) { 23915e38e3f5SEric van Gyzen case OBJT_DEFAULT: 23925e38e3f5SEric van Gyzen return (KVME_TYPE_DEFAULT); 23935e38e3f5SEric van Gyzen case OBJT_VNODE: 23945e38e3f5SEric van Gyzen return (KVME_TYPE_VNODE); 23955e38e3f5SEric van Gyzen case OBJT_SWAP: 23965e38e3f5SEric van Gyzen if ((object->flags & OBJ_TMPFS_NODE) != 0) 23975e38e3f5SEric van Gyzen return (KVME_TYPE_VNODE); 23985e38e3f5SEric van Gyzen return (KVME_TYPE_SWAP); 23995e38e3f5SEric van Gyzen case OBJT_DEVICE: 24005e38e3f5SEric van Gyzen return (KVME_TYPE_DEVICE); 24015e38e3f5SEric van Gyzen case OBJT_PHYS: 24025e38e3f5SEric van Gyzen return (KVME_TYPE_PHYS); 24035e38e3f5SEric van Gyzen case OBJT_DEAD: 24045e38e3f5SEric van Gyzen return (KVME_TYPE_DEAD); 24055e38e3f5SEric van Gyzen case OBJT_SG: 24065e38e3f5SEric van Gyzen return (KVME_TYPE_SG); 24075e38e3f5SEric van Gyzen case OBJT_MGTDEVICE: 24085e38e3f5SEric van Gyzen return (KVME_TYPE_MGTDEVICE); 24095e38e3f5SEric van Gyzen default: 24105e38e3f5SEric van Gyzen return (KVME_TYPE_UNKNOWN); 24115e38e3f5SEric van Gyzen } 24125e38e3f5SEric van Gyzen } 24135e38e3f5SEric van Gyzen 2414ff87ae35SJohn Baldwin static int 2415ff87ae35SJohn Baldwin sysctl_vm_object_list(SYSCTL_HANDLER_ARGS) 2416ff87ae35SJohn Baldwin { 24170ecee546SKonstantin Belousov struct kinfo_vmobject *kvo; 2418ff87ae35SJohn Baldwin char *fullpath, *freepath; 2419ff87ae35SJohn Baldwin struct vnode *vp; 2420ff87ae35SJohn Baldwin struct vattr va; 2421ff87ae35SJohn Baldwin vm_object_t obj; 2422ff87ae35SJohn Baldwin vm_page_t m; 2423ff87ae35SJohn Baldwin int count, error; 2424ff87ae35SJohn Baldwin 2425ff87ae35SJohn Baldwin if (req->oldptr == NULL) { 2426ff87ae35SJohn Baldwin /* 2427ff87ae35SJohn Baldwin * If an old buffer has not been provided, generate an 2428ff87ae35SJohn Baldwin * estimate of the space needed for a subsequent call. 2429ff87ae35SJohn Baldwin */ 2430ff87ae35SJohn Baldwin mtx_lock(&vm_object_list_mtx); 2431ff87ae35SJohn Baldwin count = 0; 2432ff87ae35SJohn Baldwin TAILQ_FOREACH(obj, &vm_object_list, object_list) { 2433ff87ae35SJohn Baldwin if (obj->type == OBJT_DEAD) 2434ff87ae35SJohn Baldwin continue; 2435ff87ae35SJohn Baldwin count++; 2436ff87ae35SJohn Baldwin } 2437ff87ae35SJohn Baldwin mtx_unlock(&vm_object_list_mtx); 2438ff87ae35SJohn Baldwin return (SYSCTL_OUT(req, NULL, sizeof(struct kinfo_vmobject) * 2439ff87ae35SJohn Baldwin count * 11 / 10)); 2440ff87ae35SJohn Baldwin } 2441ff87ae35SJohn Baldwin 24420ecee546SKonstantin Belousov kvo = malloc(sizeof(*kvo), M_TEMP, M_WAITOK); 2443ff87ae35SJohn Baldwin error = 0; 2444ff87ae35SJohn Baldwin 2445ff87ae35SJohn Baldwin /* 2446ff87ae35SJohn Baldwin * VM objects are type stable and are never removed from the 2447ff87ae35SJohn Baldwin * list once added. This allows us to safely read obj->object_list 2448ff87ae35SJohn Baldwin * after reacquiring the VM object lock. 2449ff87ae35SJohn Baldwin */ 2450ff87ae35SJohn Baldwin mtx_lock(&vm_object_list_mtx); 2451ff87ae35SJohn Baldwin TAILQ_FOREACH(obj, &vm_object_list, object_list) { 2452ff87ae35SJohn Baldwin if (obj->type == OBJT_DEAD) 2453ff87ae35SJohn Baldwin continue; 2454ff87ae35SJohn Baldwin VM_OBJECT_RLOCK(obj); 2455ff87ae35SJohn Baldwin if (obj->type == OBJT_DEAD) { 2456ff87ae35SJohn Baldwin VM_OBJECT_RUNLOCK(obj); 2457ff87ae35SJohn Baldwin continue; 2458ff87ae35SJohn Baldwin } 2459ff87ae35SJohn Baldwin mtx_unlock(&vm_object_list_mtx); 24600ecee546SKonstantin Belousov kvo->kvo_size = ptoa(obj->size); 24610ecee546SKonstantin Belousov kvo->kvo_resident = obj->resident_page_count; 24620ecee546SKonstantin Belousov kvo->kvo_ref_count = obj->ref_count; 24630ecee546SKonstantin Belousov kvo->kvo_shadow_count = obj->shadow_count; 24640ecee546SKonstantin Belousov kvo->kvo_memattr = obj->memattr; 24650ecee546SKonstantin Belousov kvo->kvo_active = 0; 24660ecee546SKonstantin Belousov kvo->kvo_inactive = 0; 2467ff87ae35SJohn Baldwin TAILQ_FOREACH(m, &obj->memq, listq) { 2468ff87ae35SJohn Baldwin /* 2469ff87ae35SJohn Baldwin * A page may belong to the object but be 2470ff87ae35SJohn Baldwin * dequeued and set to PQ_NONE while the 2471ff87ae35SJohn Baldwin * object lock is not held. This makes the 2472ff87ae35SJohn Baldwin * reads of m->queue below racy, and we do not 2473ff87ae35SJohn Baldwin * count pages set to PQ_NONE. However, this 2474ff87ae35SJohn Baldwin * sysctl is only meant to give an 2475ff87ae35SJohn Baldwin * approximation of the system anyway. 2476ff87ae35SJohn Baldwin */ 2477e8bcf696SMark Johnston if (m->queue == PQ_ACTIVE) 24780ecee546SKonstantin Belousov kvo->kvo_active++; 2479e8bcf696SMark Johnston else if (m->queue == PQ_INACTIVE) 24800ecee546SKonstantin Belousov kvo->kvo_inactive++; 2481ff87ae35SJohn Baldwin } 2482ff87ae35SJohn Baldwin 24830ecee546SKonstantin Belousov kvo->kvo_vn_fileid = 0; 24840ecee546SKonstantin Belousov kvo->kvo_vn_fsid = 0; 24850ecee546SKonstantin Belousov kvo->kvo_vn_fsid_freebsd11 = 0; 2486ff87ae35SJohn Baldwin freepath = NULL; 2487ff87ae35SJohn Baldwin fullpath = ""; 24885e38e3f5SEric van Gyzen kvo->kvo_type = vm_object_kvme_type(obj, &vp); 24895e38e3f5SEric van Gyzen if (vp != NULL) 2490ff87ae35SJohn Baldwin vref(vp); 2491ff87ae35SJohn Baldwin VM_OBJECT_RUNLOCK(obj); 2492ff87ae35SJohn Baldwin if (vp != NULL) { 2493ff87ae35SJohn Baldwin vn_fullpath(curthread, vp, &fullpath, &freepath); 2494ff87ae35SJohn Baldwin vn_lock(vp, LK_SHARED | LK_RETRY); 2495ff87ae35SJohn Baldwin if (VOP_GETATTR(vp, &va, curthread->td_ucred) == 0) { 24960ecee546SKonstantin Belousov kvo->kvo_vn_fileid = va.va_fileid; 24970ecee546SKonstantin Belousov kvo->kvo_vn_fsid = va.va_fsid; 24980ecee546SKonstantin Belousov kvo->kvo_vn_fsid_freebsd11 = va.va_fsid; 249969921123SKonstantin Belousov /* truncate */ 2500ff87ae35SJohn Baldwin } 2501ff87ae35SJohn Baldwin vput(vp); 2502ff87ae35SJohn Baldwin } 2503ff87ae35SJohn Baldwin 25040ecee546SKonstantin Belousov strlcpy(kvo->kvo_path, fullpath, sizeof(kvo->kvo_path)); 2505ff87ae35SJohn Baldwin if (freepath != NULL) 2506ff87ae35SJohn Baldwin free(freepath, M_TEMP); 2507ff87ae35SJohn Baldwin 2508ff87ae35SJohn Baldwin /* Pack record size down */ 25090ecee546SKonstantin Belousov kvo->kvo_structsize = offsetof(struct kinfo_vmobject, kvo_path) 25100ecee546SKonstantin Belousov + strlen(kvo->kvo_path) + 1; 25110ecee546SKonstantin Belousov kvo->kvo_structsize = roundup(kvo->kvo_structsize, 2512ff87ae35SJohn Baldwin sizeof(uint64_t)); 25130ecee546SKonstantin Belousov error = SYSCTL_OUT(req, kvo, kvo->kvo_structsize); 2514ff87ae35SJohn Baldwin mtx_lock(&vm_object_list_mtx); 2515ff87ae35SJohn Baldwin if (error) 2516ff87ae35SJohn Baldwin break; 2517ff87ae35SJohn Baldwin } 2518ff87ae35SJohn Baldwin mtx_unlock(&vm_object_list_mtx); 25190ecee546SKonstantin Belousov free(kvo, M_TEMP); 2520ff87ae35SJohn Baldwin return (error); 2521ff87ae35SJohn Baldwin } 2522ff87ae35SJohn Baldwin SYSCTL_PROC(_vm, OID_AUTO, objects, CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_SKIP | 2523ff87ae35SJohn Baldwin CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_object_list, "S,kinfo_vmobject", 2524ff87ae35SJohn Baldwin "List of VM objects"); 2525ff87ae35SJohn Baldwin 2526c7c34a24SBruce Evans #include "opt_ddb.h" 2527c3cb3e12SDavid Greenman #ifdef DDB 2528c7c34a24SBruce Evans #include <sys/kernel.h> 2529c7c34a24SBruce Evans 2530ce9edcf5SPoul-Henning Kamp #include <sys/cons.h> 2531c7c34a24SBruce Evans 2532c7c34a24SBruce Evans #include <ddb/ddb.h> 2533c7c34a24SBruce Evans 2534cac597e4SBruce Evans static int 25351b40f8c0SMatthew Dillon _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry) 2536a1f6d91cSDavid Greenman { 2537a1f6d91cSDavid Greenman vm_map_t tmpm; 2538a1f6d91cSDavid Greenman vm_map_entry_t tmpe; 2539a1f6d91cSDavid Greenman vm_object_t obj; 2540a1f6d91cSDavid Greenman 2541a1f6d91cSDavid Greenman if (map == 0) 2542a1f6d91cSDavid Greenman return 0; 2543a1f6d91cSDavid Greenman 2544a1f6d91cSDavid Greenman if (entry == 0) { 25452288078cSDoug Moore VM_MAP_ENTRY_FOREACH(tmpe, map) { 2546a1f6d91cSDavid Greenman if (_vm_object_in_map(map, object, tmpe)) { 2547a1f6d91cSDavid Greenman return 1; 2548a1f6d91cSDavid Greenman } 2549a1f6d91cSDavid Greenman } 25509fdfe602SMatthew Dillon } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 25519fdfe602SMatthew Dillon tmpm = entry->object.sub_map; 25522288078cSDoug Moore VM_MAP_ENTRY_FOREACH(tmpe, tmpm) { 2553a1f6d91cSDavid Greenman if (_vm_object_in_map(tmpm, object, tmpe)) { 2554a1f6d91cSDavid Greenman return 1; 2555a1f6d91cSDavid Greenman } 2556a1f6d91cSDavid Greenman } 25578aef1712SMatthew Dillon } else if ((obj = entry->object.vm_object) != NULL) { 255824a1cce3SDavid Greenman for (; obj; obj = obj->backing_object) 2559a1f6d91cSDavid Greenman if (obj == object) { 2560a1f6d91cSDavid Greenman return 1; 2561a1f6d91cSDavid Greenman } 2562a1f6d91cSDavid Greenman } 2563a1f6d91cSDavid Greenman return 0; 2564a1f6d91cSDavid Greenman } 2565a1f6d91cSDavid Greenman 2566cac597e4SBruce Evans static int 25671b40f8c0SMatthew Dillon vm_object_in_map(vm_object_t object) 2568a1f6d91cSDavid Greenman { 2569a1f6d91cSDavid Greenman struct proc *p; 25701005a129SJohn Baldwin 257160517fd1SJohn Baldwin /* sx_slock(&allproc_lock); */ 2572f67af5c9SXin LI FOREACH_PROC_IN_SYSTEM(p) { 2573a1f6d91cSDavid Greenman if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */) 2574a1f6d91cSDavid Greenman continue; 2575553629ebSJake Burkholder if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) { 257660517fd1SJohn Baldwin /* sx_sunlock(&allproc_lock); */ 2577a1f6d91cSDavid Greenman return 1; 2578a1f6d91cSDavid Greenman } 2579553629ebSJake Burkholder } 258060517fd1SJohn Baldwin /* sx_sunlock(&allproc_lock); */ 2581a1f6d91cSDavid Greenman if (_vm_object_in_map(kernel_map, object, 0)) 2582a1f6d91cSDavid Greenman return 1; 2583a1f6d91cSDavid Greenman return 0; 2584a1f6d91cSDavid Greenman } 2585a1f6d91cSDavid Greenman 2586c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check) 2587f708ef1bSPoul-Henning Kamp { 2588a1f6d91cSDavid Greenman vm_object_t object; 2589a1f6d91cSDavid Greenman 2590a1f6d91cSDavid Greenman /* 2591a1f6d91cSDavid Greenman * make sure that internal objs are in a map somewhere 2592a1f6d91cSDavid Greenman * and none have zero ref counts. 2593a1f6d91cSDavid Greenman */ 2594cc64b484SAlfred Perlstein TAILQ_FOREACH(object, &vm_object_list, object_list) { 259532362449SKonstantin Belousov if ((object->flags & OBJ_ANON) != 0) { 2596a1f6d91cSDavid Greenman if (object->ref_count == 0) { 25973efc015bSPeter Wemm db_printf("vmochk: internal obj has zero ref count: %ld\n", 25983efc015bSPeter Wemm (long)object->size); 2599a1f6d91cSDavid Greenman } 2600a1f6d91cSDavid Greenman if (!vm_object_in_map(object)) { 2601fc62ef1fSBruce Evans db_printf( 2602fc62ef1fSBruce Evans "vmochk: internal obj is not in a map: " 2603fc62ef1fSBruce Evans "ref: %d, size: %lu: 0x%lx, backing_object: %p\n", 2604fc62ef1fSBruce Evans object->ref_count, (u_long)object->size, 2605fc62ef1fSBruce Evans (u_long)object->size, 2606fc62ef1fSBruce Evans (void *)object->backing_object); 2607a1f6d91cSDavid Greenman } 2608a1f6d91cSDavid Greenman } 2609a1f6d91cSDavid Greenman } 2610a1f6d91cSDavid Greenman } 2611a1f6d91cSDavid Greenman 261226f9a767SRodney W. Grimes /* 2613df8bae1dSRodney W. Grimes * vm_object_print: [ debug ] 2614df8bae1dSRodney W. Grimes */ 2615c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static) 2616df8bae1dSRodney W. Grimes { 2617c7c34a24SBruce Evans /* XXX convert args. */ 2618c7c34a24SBruce Evans vm_object_t object = (vm_object_t)addr; 2619c7c34a24SBruce Evans boolean_t full = have_addr; 2620c7c34a24SBruce Evans 2621d031cff1SMatthew Dillon vm_page_t p; 2622df8bae1dSRodney W. Grimes 2623c7c34a24SBruce Evans /* XXX count is an (unused) arg. Avoid shadowing it. */ 2624c7c34a24SBruce Evans #define count was_count 2625c7c34a24SBruce Evans 2626d031cff1SMatthew Dillon int count; 2627df8bae1dSRodney W. Grimes 2628df8bae1dSRodney W. Grimes if (object == NULL) 2629df8bae1dSRodney W. Grimes return; 2630df8bae1dSRodney W. Grimes 2631eb95adefSBruce Evans db_iprintf( 2632ef694c1aSEdward Tomasz Napierala "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x ruid %d charge %jx\n", 2633e47cd172SMaxime Henrion object, (int)object->type, (uintmax_t)object->size, 26343364c323SKonstantin Belousov object->resident_page_count, object->ref_count, object->flags, 2635ef694c1aSEdward Tomasz Napierala object->cred ? object->cred->cr_ruid : -1, (uintmax_t)object->charge); 2636e47cd172SMaxime Henrion db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n", 26371c7c3c6aSMatthew Dillon object->shadow_count, 2638eb95adefSBruce Evans object->backing_object ? object->backing_object->ref_count : 0, 2639e47cd172SMaxime Henrion object->backing_object, (uintmax_t)object->backing_object_offset); 2640df8bae1dSRodney W. Grimes 2641df8bae1dSRodney W. Grimes if (!full) 2642df8bae1dSRodney W. Grimes return; 2643df8bae1dSRodney W. Grimes 2644c7c34a24SBruce Evans db_indent += 2; 2645df8bae1dSRodney W. Grimes count = 0; 2646fc2ffbe6SPoul-Henning Kamp TAILQ_FOREACH(p, &object->memq, listq) { 2647df8bae1dSRodney W. Grimes if (count == 0) 2648c7c34a24SBruce Evans db_iprintf("memory:="); 2649df8bae1dSRodney W. Grimes else if (count == 6) { 2650c7c34a24SBruce Evans db_printf("\n"); 2651c7c34a24SBruce Evans db_iprintf(" ..."); 2652df8bae1dSRodney W. Grimes count = 0; 2653df8bae1dSRodney W. Grimes } else 2654c7c34a24SBruce Evans db_printf(","); 2655df8bae1dSRodney W. Grimes count++; 2656df8bae1dSRodney W. Grimes 2657e47cd172SMaxime Henrion db_printf("(off=0x%jx,page=0x%jx)", 2658e47cd172SMaxime Henrion (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p)); 2659df8bae1dSRodney W. Grimes } 2660df8bae1dSRodney W. Grimes if (count != 0) 2661c7c34a24SBruce Evans db_printf("\n"); 2662c7c34a24SBruce Evans db_indent -= 2; 2663df8bae1dSRodney W. Grimes } 26645070c7f8SJohn Dyson 2665c7c34a24SBruce Evans /* XXX. */ 2666c7c34a24SBruce Evans #undef count 2667c7c34a24SBruce Evans 2668c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */ 26695070c7f8SJohn Dyson void 26701b40f8c0SMatthew Dillon vm_object_print( 26711b40f8c0SMatthew Dillon /* db_expr_t */ long addr, 26721b40f8c0SMatthew Dillon boolean_t have_addr, 26731b40f8c0SMatthew Dillon /* db_expr_t */ long count, 26741b40f8c0SMatthew Dillon char *modif) 2675c7c34a24SBruce Evans { 2676c7c34a24SBruce Evans vm_object_print_static(addr, have_addr, count, modif); 2677c7c34a24SBruce Evans } 2678c7c34a24SBruce Evans 2679c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages) 26805070c7f8SJohn Dyson { 26815070c7f8SJohn Dyson vm_object_t object; 2682bb2ac86fSKonstantin Belousov vm_pindex_t fidx; 2683bb2ac86fSKonstantin Belousov vm_paddr_t pa; 2684bb2ac86fSKonstantin Belousov vm_page_t m, prev_m; 2685bb2ac86fSKonstantin Belousov int rcount, nl, c; 2686cc64b484SAlfred Perlstein 2687bb2ac86fSKonstantin Belousov nl = 0; 2688cc64b484SAlfred Perlstein TAILQ_FOREACH(object, &vm_object_list, object_list) { 2689fc62ef1fSBruce Evans db_printf("new object: %p\n", (void *)object); 26905070c7f8SJohn Dyson if (nl > 18) { 26915070c7f8SJohn Dyson c = cngetc(); 26925070c7f8SJohn Dyson if (c != ' ') 26935070c7f8SJohn Dyson return; 26945070c7f8SJohn Dyson nl = 0; 26955070c7f8SJohn Dyson } 26965070c7f8SJohn Dyson nl++; 26975070c7f8SJohn Dyson rcount = 0; 26985070c7f8SJohn Dyson fidx = 0; 2699bb2ac86fSKonstantin Belousov pa = -1; 2700bb2ac86fSKonstantin Belousov TAILQ_FOREACH(m, &object->memq, listq) { 2701bb2ac86fSKonstantin Belousov if (m->pindex > 128) 2702bb2ac86fSKonstantin Belousov break; 2703bb2ac86fSKonstantin Belousov if ((prev_m = TAILQ_PREV(m, pglist, listq)) != NULL && 2704bb2ac86fSKonstantin Belousov prev_m->pindex + 1 != m->pindex) { 27055070c7f8SJohn Dyson if (rcount) { 27063efc015bSPeter Wemm db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 27073efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 27085070c7f8SJohn Dyson if (nl > 18) { 27095070c7f8SJohn Dyson c = cngetc(); 27105070c7f8SJohn Dyson if (c != ' ') 27115070c7f8SJohn Dyson return; 27125070c7f8SJohn Dyson nl = 0; 27135070c7f8SJohn Dyson } 27145070c7f8SJohn Dyson nl++; 27155070c7f8SJohn Dyson rcount = 0; 27165070c7f8SJohn Dyson } 27175070c7f8SJohn Dyson } 27185070c7f8SJohn Dyson if (rcount && 27195070c7f8SJohn Dyson (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) { 27205070c7f8SJohn Dyson ++rcount; 27215070c7f8SJohn Dyson continue; 27225070c7f8SJohn Dyson } 27235070c7f8SJohn Dyson if (rcount) { 27242446e4f0SAlan Cox db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 27253efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 27265070c7f8SJohn Dyson if (nl > 18) { 27275070c7f8SJohn Dyson c = cngetc(); 27285070c7f8SJohn Dyson if (c != ' ') 27295070c7f8SJohn Dyson return; 27305070c7f8SJohn Dyson nl = 0; 27315070c7f8SJohn Dyson } 27325070c7f8SJohn Dyson nl++; 27335070c7f8SJohn Dyson } 2734bb2ac86fSKonstantin Belousov fidx = m->pindex; 27355070c7f8SJohn Dyson pa = VM_PAGE_TO_PHYS(m); 27365070c7f8SJohn Dyson rcount = 1; 27375070c7f8SJohn Dyson } 27385070c7f8SJohn Dyson if (rcount) { 27393efc015bSPeter Wemm db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 27403efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 27415070c7f8SJohn Dyson if (nl > 18) { 27425070c7f8SJohn Dyson c = cngetc(); 27435070c7f8SJohn Dyson if (c != ' ') 27445070c7f8SJohn Dyson return; 27455070c7f8SJohn Dyson nl = 0; 27465070c7f8SJohn Dyson } 27475070c7f8SJohn Dyson nl++; 27485070c7f8SJohn Dyson } 27495070c7f8SJohn Dyson } 27505070c7f8SJohn Dyson } 2751c3cb3e12SDavid Greenman #endif /* DDB */ 2752