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, 24467388836SKonstantin 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; 27167388836SKonstantin 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 - 29367388836SKonstantin 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); 43767388836SKonstantin 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 45067388836SKonstantin Belousov vm_object_allocate_anon(vm_pindex_t size, vm_object_t backing_object, 45167388836SKonstantin Belousov struct ucred *cred, vm_size_t charge) 45263967687SJeff Roberson { 45367388836SKonstantin Belousov vm_object_t handle, object; 45490688d13SAlan Cox 45567388836SKonstantin Belousov if (backing_object == NULL) 45667388836SKonstantin Belousov handle = NULL; 45767388836SKonstantin Belousov else if ((backing_object->flags & OBJ_ANON) != 0) 45867388836SKonstantin Belousov handle = backing_object->handle; 45967388836SKonstantin Belousov else 46067388836SKonstantin Belousov handle = backing_object; 46167388836SKonstantin Belousov object = uma_zalloc(obj_zone, M_WAITOK); 46263967687SJeff Roberson _vm_object_allocate(OBJT_DEFAULT, size, OBJ_ANON | OBJ_ONEMAPPING, 46367388836SKonstantin Belousov object, handle); 46467388836SKonstantin Belousov object->cred = cred; 46567388836SKonstantin 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 */ 9005cff1f4dSMark Johnston if ((flags & OBJPC_NOSYNC) != 0 && (p->a.flags & 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 131967388836SKonstantin Belousov vm_object_shadow(vm_object_t *object, vm_ooffset_t *offset, vm_size_t length, 132067388836SKonstantin 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 */ 134267388836SKonstantin 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 135067388836SKonstantin Belousov if (shared || source != NULL) { 135167388836SKonstantin Belousov VM_OBJECT_WLOCK(result); 135267388836SKonstantin Belousov 135351b867e5SJeff Roberson /* 135467388836SKonstantin Belousov * The new object shadows the source object, adding a 135567388836SKonstantin Belousov * reference to it. Our caller changes his reference 135667388836SKonstantin Belousov * to point to the new object, removing a reference to 135767388836SKonstantin Belousov * the source object. Net result: no change of 135867388836SKonstantin Belousov * reference count, unless the caller needs to add one 135967388836SKonstantin Belousov * more reference due to forking a shared map entry. 136067388836SKonstantin Belousov */ 136167388836SKonstantin Belousov if (shared) { 136267388836SKonstantin Belousov vm_object_reference_locked(result); 136367388836SKonstantin Belousov vm_object_clear_flag(result, OBJ_ONEMAPPING); 136467388836SKonstantin Belousov } 136567388836SKonstantin Belousov 136667388836SKonstantin Belousov /* 136767388836SKonstantin Belousov * Try to optimize the result object's page color when 136867388836SKonstantin Belousov * shadowing in order to maintain page coloring 136967388836SKonstantin 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; 137667388836SKonstantin Belousov result->pg_color = (source->pg_color + 137767388836SKonstantin Belousov OFF_TO_IDX(*offset)) & ((1 << (VM_NFREEORDER - 137867388836SKonstantin Belousov 1)) - 1); 1379f8a47341SAlan Cox #endif 138067388836SKonstantin 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 */ 142067388836SKonstantin Belousov new_object = vm_object_allocate_anon(size, orig_object, 142167388836SKonstantin 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); 143867388836SKonstantin 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 1480*4bf95d00SJeff Roberson /* 1481*4bf95d00SJeff Roberson * The page was left invalid. Likely placed there by 1482*4bf95d00SJeff Roberson * an incomplete fault. Just remove and ignore. 1483*4bf95d00SJeff Roberson */ 1484*4bf95d00SJeff Roberson if (vm_page_none_valid(m)) { 1485*4bf95d00SJeff Roberson if (vm_page_remove(m)) 1486*4bf95d00SJeff Roberson vm_page_free(m); 1487*4bf95d00SJeff Roberson else 1488*4bf95d00SJeff Roberson vm_page_xunbusy(m); 1489*4bf95d00SJeff Roberson continue; 1490*4bf95d00SJeff Roberson } 1491*4bf95d00SJeff Roberson 14923453bca8SAlan Cox /* vm_page_rename() will dirty the page. */ 1493e946b949SAttilio Rao if (vm_page_rename(m, new_object, idx)) { 149463e97555SJeff Roberson vm_page_xunbusy(m); 1495e946b949SAttilio Rao VM_OBJECT_WUNLOCK(new_object); 1496e946b949SAttilio Rao VM_OBJECT_WUNLOCK(orig_object); 14978d6fbbb8SJeff Roberson vm_radix_wait(); 1498e946b949SAttilio Rao VM_OBJECT_WLOCK(orig_object); 1499e946b949SAttilio Rao VM_OBJECT_WLOCK(new_object); 1500e946b949SAttilio Rao goto retry; 1501e946b949SAttilio Rao } 150263e97555SJeff Roberson 1503b5f359b7SAlan Cox #if VM_NRESERVLEVEL > 0 1504b5f359b7SAlan Cox /* 1505b5f359b7SAlan Cox * If some of the reservation's allocated pages remain with 1506b5f359b7SAlan Cox * the original object, then transferring the reservation to 1507b5f359b7SAlan Cox * the new object is neither particularly beneficial nor 1508b5f359b7SAlan Cox * particularly harmful as compared to leaving the reservation 1509b5f359b7SAlan Cox * with the original object. If, however, all of the 1510b5f359b7SAlan Cox * reservation's allocated pages are transferred to the new 1511b5f359b7SAlan Cox * object, then transferring the reservation is typically 1512b5f359b7SAlan Cox * beneficial. Determining which of these two cases applies 1513b5f359b7SAlan Cox * would be more costly than unconditionally renaming the 1514b5f359b7SAlan Cox * reservation. 1515b5f359b7SAlan Cox */ 1516b5f359b7SAlan Cox vm_reserv_rename(m, new_object, orig_object, offidxstart); 1517b5f359b7SAlan Cox #endif 15188da1c098SJeff Roberson if (orig_object->type != OBJT_SWAP) 15198da1c098SJeff Roberson vm_page_xunbusy(m); 1520c5aaa06dSAlan Cox } 1521d7a013c3SAlan Cox if (orig_object->type == OBJT_SWAP) { 1522c5aaa06dSAlan Cox /* 1523c7c8dd7eSAlan Cox * swap_pager_copy() can sleep, in which case the orig_object's 1524c7c8dd7eSAlan Cox * and new_object's locks are released and reacquired. 1525c5aaa06dSAlan Cox */ 1526c5aaa06dSAlan Cox swap_pager_copy(orig_object, new_object, offidxstart, 0); 1527dfd55c0cSAttilio Rao TAILQ_FOREACH(m, &new_object->memq, listq) 1528c7aebda8SAttilio Rao vm_page_xunbusy(m); 1529c5aaa06dSAlan Cox } 153089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(orig_object); 153189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(new_object); 1532c5aaa06dSAlan Cox entry->object.vm_object = new_object; 1533c5aaa06dSAlan Cox entry->offset = 0LL; 1534c5aaa06dSAlan Cox vm_object_deallocate(orig_object); 153589f6b863SAttilio Rao VM_OBJECT_WLOCK(new_object); 1536c5aaa06dSAlan Cox } 1537c5aaa06dSAlan Cox 15382ad1a3f7SMatthew Dillon #define OBSC_COLLAPSE_NOWAIT 0x0002 15392ad1a3f7SMatthew Dillon #define OBSC_COLLAPSE_WAIT 0x0004 15402ad1a3f7SMatthew Dillon 154199a1570aSKonstantin Belousov static vm_page_t 15424cc8daf7SConrad Meyer vm_object_collapse_scan_wait(vm_object_t object, vm_page_t p, vm_page_t next, 154399a1570aSKonstantin Belousov int op) 154499a1570aSKonstantin Belousov { 154599a1570aSKonstantin Belousov vm_object_t backing_object; 154699a1570aSKonstantin Belousov 154799a1570aSKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(object); 154899a1570aSKonstantin Belousov backing_object = object->backing_object; 154999a1570aSKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(backing_object); 155099a1570aSKonstantin Belousov 155199a1570aSKonstantin Belousov KASSERT(p == NULL || p->object == object || p->object == backing_object, 155299a1570aSKonstantin Belousov ("invalid ownership %p %p %p", p, object, backing_object)); 155399a1570aSKonstantin Belousov if ((op & OBSC_COLLAPSE_NOWAIT) != 0) 155499a1570aSKonstantin Belousov return (next); 15558d6fbbb8SJeff Roberson /* The page is only NULL when rename fails. */ 15564cdea4a8SJeff Roberson if (p == NULL) { 15576a14746cSRyan Libby VM_OBJECT_WUNLOCK(object); 15586a14746cSRyan Libby VM_OBJECT_WUNLOCK(backing_object); 15598d6fbbb8SJeff Roberson vm_radix_wait(); 15604cdea4a8SJeff Roberson } else { 15614cdea4a8SJeff Roberson if (p->object == object) 15624cdea4a8SJeff Roberson VM_OBJECT_WUNLOCK(backing_object); 156399a1570aSKonstantin Belousov else 15644cdea4a8SJeff Roberson VM_OBJECT_WUNLOCK(object); 15655975e53dSKonstantin Belousov vm_page_busy_sleep(p, "vmocol", false); 15664cdea4a8SJeff Roberson } 156799a1570aSKonstantin Belousov VM_OBJECT_WLOCK(object); 156899a1570aSKonstantin Belousov VM_OBJECT_WLOCK(backing_object); 156999a1570aSKonstantin Belousov return (TAILQ_FIRST(&backing_object->memq)); 157099a1570aSKonstantin Belousov } 157199a1570aSKonstantin Belousov 157299a1570aSKonstantin Belousov static bool 15734cc8daf7SConrad Meyer vm_object_scan_all_shadowed(vm_object_t object) 15744cc8daf7SConrad Meyer { 15754cc8daf7SConrad Meyer vm_object_t backing_object; 15764cc8daf7SConrad Meyer vm_page_t p, pp; 157777d6fd97SKonstantin Belousov vm_pindex_t backing_offset_index, new_pindex, pi, ps; 15784cc8daf7SConrad Meyer 15794cc8daf7SConrad Meyer VM_OBJECT_ASSERT_WLOCKED(object); 15804cc8daf7SConrad Meyer VM_OBJECT_ASSERT_WLOCKED(object->backing_object); 15814cc8daf7SConrad Meyer 15824cc8daf7SConrad Meyer backing_object = object->backing_object; 15834cc8daf7SConrad Meyer 158463967687SJeff Roberson if ((backing_object->flags & OBJ_ANON) == 0) 15854cc8daf7SConrad Meyer return (false); 15864cc8daf7SConrad Meyer 158777d6fd97SKonstantin Belousov pi = backing_offset_index = OFF_TO_IDX(object->backing_object_offset); 158877d6fd97SKonstantin Belousov p = vm_page_find_least(backing_object, pi); 158977d6fd97SKonstantin Belousov ps = swap_pager_find_least(backing_object, pi); 15904cc8daf7SConrad Meyer 15914cc8daf7SConrad Meyer /* 159277d6fd97SKonstantin Belousov * Only check pages inside the parent object's range and 159377d6fd97SKonstantin Belousov * inside the parent object's mapping of the backing object. 15944cc8daf7SConrad Meyer */ 159577d6fd97SKonstantin Belousov for (;; pi++) { 159677d6fd97SKonstantin Belousov if (p != NULL && p->pindex < pi) 159777d6fd97SKonstantin Belousov p = TAILQ_NEXT(p, listq); 159877d6fd97SKonstantin Belousov if (ps < pi) 159977d6fd97SKonstantin Belousov ps = swap_pager_find_least(backing_object, pi); 160077d6fd97SKonstantin Belousov if (p == NULL && ps >= backing_object->size) 160177d6fd97SKonstantin Belousov break; 160277d6fd97SKonstantin Belousov else if (p == NULL) 160377d6fd97SKonstantin Belousov pi = ps; 160477d6fd97SKonstantin Belousov else 160577d6fd97SKonstantin Belousov pi = MIN(p->pindex, ps); 160677d6fd97SKonstantin Belousov 160777d6fd97SKonstantin Belousov new_pindex = pi - backing_offset_index; 160877d6fd97SKonstantin Belousov if (new_pindex >= object->size) 160977d6fd97SKonstantin Belousov break; 16104cc8daf7SConrad Meyer 16114cc8daf7SConrad Meyer /* 16124cc8daf7SConrad Meyer * See if the parent has the page or if the parent's object 16134cc8daf7SConrad Meyer * pager has the page. If the parent has the page but the page 16144cc8daf7SConrad Meyer * is not valid, the parent's object pager must have the page. 16154cc8daf7SConrad Meyer * 16164cc8daf7SConrad Meyer * If this fails, the parent does not completely shadow the 16174cc8daf7SConrad Meyer * object and we might as well give up now. 16184cc8daf7SConrad Meyer */ 16194cc8daf7SConrad Meyer pp = vm_page_lookup(object, new_pindex); 16200012f373SJeff Roberson /* 16210012f373SJeff Roberson * The valid check here is stable due to object lock being 16220012f373SJeff Roberson * required to clear valid and initiate paging. 16230012f373SJeff Roberson */ 16240012f373SJeff Roberson if ((pp == NULL || vm_page_none_valid(pp)) && 16254cc8daf7SConrad Meyer !vm_pager_has_page(object, new_pindex, NULL, NULL)) 16264cc8daf7SConrad Meyer return (false); 16274cc8daf7SConrad Meyer } 16284cc8daf7SConrad Meyer return (true); 16294cc8daf7SConrad Meyer } 16304cc8daf7SConrad Meyer 16314cc8daf7SConrad Meyer static bool 16324cc8daf7SConrad Meyer vm_object_collapse_scan(vm_object_t object, int op) 16332ad1a3f7SMatthew Dillon { 16342ad1a3f7SMatthew Dillon vm_object_t backing_object; 163599a1570aSKonstantin Belousov vm_page_t next, p, pp; 163699a1570aSKonstantin Belousov vm_pindex_t backing_offset_index, new_pindex; 16372ad1a3f7SMatthew Dillon 163889f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 163989f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object->backing_object); 16402ad1a3f7SMatthew Dillon 16412ad1a3f7SMatthew Dillon backing_object = object->backing_object; 16422ad1a3f7SMatthew Dillon backing_offset_index = OFF_TO_IDX(object->backing_object_offset); 16432ad1a3f7SMatthew Dillon 16442ad1a3f7SMatthew Dillon /* 16452ad1a3f7SMatthew Dillon * Initial conditions 16462ad1a3f7SMatthew Dillon */ 16474cc8daf7SConrad Meyer if ((op & OBSC_COLLAPSE_WAIT) != 0) 16482ad1a3f7SMatthew Dillon vm_object_set_flag(backing_object, OBJ_DEAD); 16492ad1a3f7SMatthew Dillon 16502ad1a3f7SMatthew Dillon /* 16512ad1a3f7SMatthew Dillon * Our scan 16522ad1a3f7SMatthew Dillon */ 16534cc8daf7SConrad Meyer for (p = TAILQ_FIRST(&backing_object->memq); p != NULL; p = next) { 165499a1570aSKonstantin Belousov next = TAILQ_NEXT(p, listq); 165599a1570aSKonstantin Belousov new_pindex = p->pindex - backing_offset_index; 16562ad1a3f7SMatthew Dillon 16572ad1a3f7SMatthew Dillon /* 16582ad1a3f7SMatthew Dillon * Check for busy page 16592ad1a3f7SMatthew Dillon */ 166063e97555SJeff Roberson if (vm_page_tryxbusy(p) == 0) { 16614cc8daf7SConrad Meyer next = vm_object_collapse_scan_wait(object, p, next, op); 16622ad1a3f7SMatthew Dillon continue; 16632ad1a3f7SMatthew Dillon } 16642ad1a3f7SMatthew Dillon 166599a1570aSKonstantin Belousov KASSERT(p->object == backing_object, 16664cc8daf7SConrad Meyer ("vm_object_collapse_scan: object mismatch")); 16672ad1a3f7SMatthew Dillon 166899a1570aSKonstantin Belousov if (p->pindex < backing_offset_index || 166999a1570aSKonstantin Belousov new_pindex >= object->size) { 1670e946b949SAttilio Rao if (backing_object->type == OBJT_SWAP) 16714cc8daf7SConrad Meyer swap_pager_freespace(backing_object, p->pindex, 16724cc8daf7SConrad Meyer 1); 1673e946b949SAttilio Rao 1674f6d89838SAlan Cox KASSERT(!pmap_page_is_mapped(p), 1675f6d89838SAlan Cox ("freeing mapped page %p", p)); 16760fd977b3SMark Johnston if (vm_page_remove(p)) 16772ad1a3f7SMatthew Dillon vm_page_free(p); 16788da1c098SJeff Roberson else 16798da1c098SJeff Roberson vm_page_xunbusy(p); 16802ad1a3f7SMatthew Dillon continue; 16812ad1a3f7SMatthew Dillon } 16822ad1a3f7SMatthew Dillon 16832ad1a3f7SMatthew Dillon pp = vm_page_lookup(object, new_pindex); 168463e97555SJeff Roberson if (pp != NULL && vm_page_tryxbusy(pp) == 0) { 168563e97555SJeff Roberson vm_page_xunbusy(p); 1686e18cc7bfSMax Laier /* 16874cc8daf7SConrad Meyer * The page in the parent is busy and possibly not 16884cc8daf7SConrad Meyer * (yet) valid. Until its state is finalized by the 16894cc8daf7SConrad Meyer * busy bit owner, we can't tell whether it shadows the 16904cc8daf7SConrad Meyer * original page. Therefore, we must either skip it 16914cc8daf7SConrad Meyer * and the original (backing_object) page or wait for 16924cc8daf7SConrad Meyer * its state to be finalized. 1693e18cc7bfSMax Laier * 16944cc8daf7SConrad Meyer * This is due to a race with vm_fault() where we must 16954cc8daf7SConrad Meyer * unbusy the original (backing_obj) page before we can 16964cc8daf7SConrad Meyer * (re)lock the parent. Hence we can get here. 1697e18cc7bfSMax Laier */ 16984cc8daf7SConrad Meyer next = vm_object_collapse_scan_wait(object, pp, next, 16994cc8daf7SConrad Meyer op); 1700e18cc7bfSMax Laier continue; 1701e18cc7bfSMax Laier } 170299a1570aSKonstantin Belousov 1703*4bf95d00SJeff Roberson if (pp != NULL && vm_page_none_valid(pp)) { 1704*4bf95d00SJeff Roberson /* 1705*4bf95d00SJeff Roberson * The page was invalid in the parent. Likely placed 1706*4bf95d00SJeff Roberson * there by an incomplete fault. Just remove and 1707*4bf95d00SJeff Roberson * ignore. p can replace it. 1708*4bf95d00SJeff Roberson */ 1709*4bf95d00SJeff Roberson if (vm_page_remove(pp)) 1710*4bf95d00SJeff Roberson vm_page_free(pp); 1711*4bf95d00SJeff Roberson else 1712*4bf95d00SJeff Roberson vm_page_xunbusy(pp); 1713*4bf95d00SJeff Roberson pp = NULL; 1714*4bf95d00SJeff Roberson } 171599a1570aSKonstantin Belousov 17164cc8daf7SConrad Meyer if (pp != NULL || vm_pager_has_page(object, new_pindex, NULL, 17174cc8daf7SConrad Meyer NULL)) { 171899a1570aSKonstantin Belousov /* 17194cc8daf7SConrad Meyer * The page already exists in the parent OR swap exists 17204cc8daf7SConrad Meyer * for this location in the parent. Leave the parent's 17214cc8daf7SConrad Meyer * page alone. Destroy the original page from the 17224cc8daf7SConrad Meyer * backing object. 172399a1570aSKonstantin Belousov */ 1724e946b949SAttilio Rao if (backing_object->type == OBJT_SWAP) 17254cc8daf7SConrad Meyer swap_pager_freespace(backing_object, p->pindex, 17264cc8daf7SConrad Meyer 1); 1727f6d89838SAlan Cox KASSERT(!pmap_page_is_mapped(p), 1728f6d89838SAlan Cox ("freeing mapped page %p", p)); 17290fd977b3SMark Johnston if (vm_page_remove(p)) 17302ad1a3f7SMatthew Dillon vm_page_free(p); 17318da1c098SJeff Roberson else 17328da1c098SJeff Roberson vm_page_xunbusy(p); 173363e97555SJeff Roberson if (pp != NULL) 173463e97555SJeff Roberson vm_page_xunbusy(pp); 17352ad1a3f7SMatthew Dillon continue; 17362ad1a3f7SMatthew Dillon } 17372ad1a3f7SMatthew Dillon 1738e946b949SAttilio Rao /* 17394cc8daf7SConrad Meyer * Page does not exist in parent, rename the page from the 17404cc8daf7SConrad Meyer * backing object to the main object. 1741e946b949SAttilio Rao * 17424cc8daf7SConrad Meyer * If the page was mapped to a process, it can remain mapped 17433453bca8SAlan Cox * through the rename. vm_page_rename() will dirty the page. 1744e946b949SAttilio Rao */ 1745e946b949SAttilio Rao if (vm_page_rename(p, object, new_pindex)) { 174663e97555SJeff Roberson vm_page_xunbusy(p); 174763e97555SJeff Roberson if (pp != NULL) 174863e97555SJeff Roberson vm_page_xunbusy(pp); 17494cc8daf7SConrad Meyer next = vm_object_collapse_scan_wait(object, NULL, next, 17504cc8daf7SConrad Meyer op); 1751e946b949SAttilio Rao continue; 1752e946b949SAttilio Rao } 175314a5dc17SAttilio Rao 175414a5dc17SAttilio Rao /* Use the old pindex to free the right page. */ 1755e946b949SAttilio Rao if (backing_object->type == OBJT_SWAP) 175614a5dc17SAttilio Rao swap_pager_freespace(backing_object, 175714a5dc17SAttilio Rao new_pindex + backing_offset_index, 1); 1758e946b949SAttilio Rao 1759f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 1760f8a47341SAlan Cox /* 1761f8a47341SAlan Cox * Rename the reservation. 1762f8a47341SAlan Cox */ 1763f8a47341SAlan Cox vm_reserv_rename(p, object, backing_object, 1764f8a47341SAlan Cox backing_offset_index); 1765f8a47341SAlan Cox #endif 17668da1c098SJeff Roberson vm_page_xunbusy(p); 17672ad1a3f7SMatthew Dillon } 176899a1570aSKonstantin Belousov return (true); 17692ad1a3f7SMatthew Dillon } 17702ad1a3f7SMatthew Dillon 1771df8bae1dSRodney W. Grimes 1772df8bae1dSRodney W. Grimes /* 17732fe6e4d7SDavid Greenman * this version of collapse allows the operation to occur earlier and 17742fe6e4d7SDavid Greenman * when paging_in_progress is true for an object... This is not a complete 17752fe6e4d7SDavid Greenman * operation, but should plug 99.9% of the rest of the leaks. 17762fe6e4d7SDavid Greenman */ 17772fe6e4d7SDavid Greenman static void 17781b40f8c0SMatthew Dillon vm_object_qcollapse(vm_object_t object) 17792fe6e4d7SDavid Greenman { 17802ad1a3f7SMatthew Dillon vm_object_t backing_object = object->backing_object; 17812fe6e4d7SDavid Greenman 178289f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 178389f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(backing_object); 17841b40f8c0SMatthew Dillon 17852fe6e4d7SDavid Greenman if (backing_object->ref_count != 1) 17862fe6e4d7SDavid Greenman return; 17872fe6e4d7SDavid Greenman 17884cc8daf7SConrad Meyer vm_object_collapse_scan(object, OBSC_COLLAPSE_NOWAIT); 17892fe6e4d7SDavid Greenman } 17902fe6e4d7SDavid Greenman 1791df8bae1dSRodney W. Grimes /* 1792df8bae1dSRodney W. Grimes * vm_object_collapse: 1793df8bae1dSRodney W. Grimes * 1794df8bae1dSRodney W. Grimes * Collapse an object with the object backing it. 1795df8bae1dSRodney W. Grimes * Pages in the backing object are moved into the 1796df8bae1dSRodney W. Grimes * parent, and the backing object is deallocated. 1797df8bae1dSRodney W. Grimes */ 179826f9a767SRodney W. Grimes void 17991b40f8c0SMatthew Dillon vm_object_collapse(vm_object_t object) 1800df8bae1dSRodney W. Grimes { 180198f139daSKonstantin Belousov vm_object_t backing_object, new_backing_object; 180298f139daSKonstantin Belousov 180389f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 180423955314SAlfred Perlstein 1805df8bae1dSRodney W. Grimes while (TRUE) { 1806df8bae1dSRodney W. Grimes /* 1807df8bae1dSRodney W. Grimes * Verify that the conditions are right for collapse: 1808df8bae1dSRodney W. Grimes * 18092ad1a3f7SMatthew Dillon * The object exists and the backing object exists. 1810df8bae1dSRodney W. Grimes */ 181124a1cce3SDavid Greenman if ((backing_object = object->backing_object) == NULL) 18122ad1a3f7SMatthew Dillon break; 1813df8bae1dSRodney W. Grimes 1814f919ebdeSDavid Greenman /* 1815f919ebdeSDavid Greenman * we check the backing object first, because it is most likely 181624a1cce3SDavid Greenman * not collapsable. 1817f919ebdeSDavid Greenman */ 181863967687SJeff Roberson if ((backing_object->flags & OBJ_ANON) == 0) 181963967687SJeff Roberson break; 182089f6b863SAttilio Rao VM_OBJECT_WLOCK(backing_object); 182132362449SKonstantin Belousov if ((backing_object->flags & OBJ_DEAD) != 0 || 182232362449SKonstantin Belousov (object->flags & (OBJ_DEAD | OBJ_ANON)) != OBJ_ANON) { 182389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 18242ad1a3f7SMatthew Dillon break; 182524a1cce3SDavid Greenman } 18269b4814bbSDavid Greenman 182711b57401SHans Petter Selasky if (REFCOUNT_COUNT(object->paging_in_progress) > 0 || 182811b57401SHans Petter Selasky REFCOUNT_COUNT(backing_object->paging_in_progress) > 0) { 1829b9921222SDavid Greenman vm_object_qcollapse(object); 183089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 18312ad1a3f7SMatthew Dillon break; 1832df8bae1dSRodney W. Grimes } 183398f139daSKonstantin Belousov 183426f9a767SRodney W. Grimes /* 18350d94caffSDavid Greenman * We know that we can either collapse the backing object (if 18362ad1a3f7SMatthew Dillon * the parent is the only reference to it) or (perhaps) have 18372ad1a3f7SMatthew Dillon * the parent bypass the object if the parent happens to shadow 18382ad1a3f7SMatthew Dillon * all the resident pages in the entire backing object. 18392ad1a3f7SMatthew Dillon * 18402ad1a3f7SMatthew Dillon * This is ignoring pager-backed pages such as swap pages. 18414cc8daf7SConrad Meyer * vm_object_collapse_scan fails the shadowing test in this 18422ad1a3f7SMatthew Dillon * case. 1843df8bae1dSRodney W. Grimes */ 1844df8bae1dSRodney W. Grimes if (backing_object->ref_count == 1) { 1845aa9bc3b1SKonstantin Belousov vm_object_pip_add(object, 1); 1846aa9bc3b1SKonstantin Belousov vm_object_pip_add(backing_object, 1); 1847aa9bc3b1SKonstantin Belousov 1848df8bae1dSRodney W. Grimes /* 18492ad1a3f7SMatthew Dillon * If there is exactly one reference to the backing 18502ad1a3f7SMatthew Dillon * object, we can collapse it into the parent. 1851df8bae1dSRodney W. Grimes */ 18524cc8daf7SConrad Meyer vm_object_collapse_scan(object, OBSC_COLLAPSE_WAIT); 1853df8bae1dSRodney W. Grimes 1854f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 1855f8a47341SAlan Cox /* 1856f8a47341SAlan Cox * Break any reservations from backing_object. 1857f8a47341SAlan Cox */ 1858f8a47341SAlan Cox if (__predict_false(!LIST_EMPTY(&backing_object->rvq))) 1859f8a47341SAlan Cox vm_reserv_break_all(backing_object); 1860f8a47341SAlan Cox #endif 1861f8a47341SAlan Cox 1862df8bae1dSRodney W. Grimes /* 1863df8bae1dSRodney W. Grimes * Move the pager from backing_object to object. 1864df8bae1dSRodney W. Grimes */ 18656be36525SAlan Cox if (backing_object->type == OBJT_SWAP) { 186624a1cce3SDavid Greenman /* 1867c7c8dd7eSAlan Cox * swap_pager_copy() can sleep, in which case 1868c7c8dd7eSAlan Cox * the backing_object's and object's locks are 1869c7c8dd7eSAlan Cox * released and reacquired. 1870571a1e92SAttilio Rao * Since swap_pager_copy() is being asked to 1871571a1e92SAttilio Rao * destroy the source, it will change the 1872571a1e92SAttilio Rao * backing_object's type to OBJT_DEFAULT. 187324a1cce3SDavid Greenman */ 18741c7c3c6aSMatthew Dillon swap_pager_copy( 18751c7c3c6aSMatthew Dillon backing_object, 18761c7c3c6aSMatthew Dillon object, 18771c7c3c6aSMatthew Dillon OFF_TO_IDX(object->backing_object_offset), TRUE); 1878c0503609SDavid Greenman } 1879df8bae1dSRodney W. Grimes /* 1880df8bae1dSRodney W. Grimes * Object now shadows whatever backing_object did. 18812ad1a3f7SMatthew Dillon * Note that the reference to 18822ad1a3f7SMatthew Dillon * backing_object->backing_object moves from within 18832ad1a3f7SMatthew Dillon * backing_object to within object. 1884df8bae1dSRodney W. Grimes */ 188551b867e5SJeff Roberson vm_object_backing_remove_locked(object); 188651b867e5SJeff Roberson new_backing_object = backing_object->backing_object; 188751b867e5SJeff Roberson if (new_backing_object != NULL) { 188851b867e5SJeff Roberson VM_OBJECT_WLOCK(new_backing_object); 188951b867e5SJeff Roberson vm_object_backing_remove_locked(backing_object); 189051b867e5SJeff Roberson vm_object_backing_insert_locked(object, 189151b867e5SJeff Roberson new_backing_object); 189251b867e5SJeff Roberson VM_OBJECT_WUNLOCK(new_backing_object); 1893de5f6a77SJohn Dyson } 18942ad1a3f7SMatthew Dillon object->backing_object_offset += 18952ad1a3f7SMatthew Dillon backing_object->backing_object_offset; 18962ad1a3f7SMatthew Dillon 1897df8bae1dSRodney W. Grimes /* 1898df8bae1dSRodney W. Grimes * Discard backing_object. 1899df8bae1dSRodney W. Grimes * 19000d94caffSDavid Greenman * Since the backing object has no pages, no pager left, 19010d94caffSDavid Greenman * and no object references within it, all that is 19020d94caffSDavid Greenman * necessary is to dispose of it. 1903df8bae1dSRodney W. Grimes */ 19049b4d473aSKonstantin Belousov KASSERT(backing_object->ref_count == 1, ( 19059b4d473aSKonstantin Belousov "backing_object %p was somehow re-referenced during collapse!", 19069b4d473aSKonstantin Belousov backing_object)); 1907aa9bc3b1SKonstantin Belousov vm_object_pip_wakeup(backing_object); 1908e735691bSJohn Baldwin backing_object->type = OBJT_DEAD; 1909a67d5408SJeff Roberson refcount_release(&backing_object->ref_count); 191089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 19119b4d473aSKonstantin Belousov vm_object_destroy(backing_object); 1912df8bae1dSRodney W. Grimes 1913aa9bc3b1SKonstantin Belousov vm_object_pip_wakeup(object); 191411542376SAlan Cox counter_u64_add(object_collapses, 1); 19150d94caffSDavid Greenman } else { 1916df8bae1dSRodney W. Grimes /* 19172ad1a3f7SMatthew Dillon * If we do not entirely shadow the backing object, 19182ad1a3f7SMatthew Dillon * there is nothing we can do so we give up. 1919df8bae1dSRodney W. Grimes */ 1920df59a0feSJeff Roberson if (object->resident_page_count != object->size && 19214cc8daf7SConrad Meyer !vm_object_scan_all_shadowed(object)) { 192289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 19232ad1a3f7SMatthew Dillon break; 192424a1cce3SDavid Greenman } 1925df8bae1dSRodney W. Grimes 1926df8bae1dSRodney W. Grimes /* 19270d94caffSDavid Greenman * Make the parent shadow the next object in the 19280d94caffSDavid Greenman * chain. Deallocating backing_object will not remove 19290d94caffSDavid Greenman * it, since its reference count is at least 2. 1930df8bae1dSRodney W. Grimes */ 193151b867e5SJeff Roberson vm_object_backing_remove_locked(object); 193295e5e988SJohn Dyson 193395e5e988SJohn Dyson new_backing_object = backing_object->backing_object; 193451b867e5SJeff Roberson if (new_backing_object != NULL) { 193551b867e5SJeff Roberson vm_object_backing_insert(object, 193651b867e5SJeff Roberson new_backing_object); 193751b867e5SJeff Roberson vm_object_reference(new_backing_object); 193895e5e988SJohn Dyson object->backing_object_offset += 193995e5e988SJohn Dyson backing_object->backing_object_offset; 1940de5f6a77SJohn Dyson } 1941df8bae1dSRodney W. Grimes 1942df8bae1dSRodney W. Grimes /* 19430d94caffSDavid Greenman * Drop the reference count on backing_object. Since 194422ec553fSAlan Cox * its ref_count was at least 2, it will not vanish. 1945df8bae1dSRodney W. Grimes */ 194651df5321SJeff Roberson refcount_release(&backing_object->ref_count); 194789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 194811542376SAlan Cox counter_u64_add(object_bypasses, 1); 1949df8bae1dSRodney W. Grimes } 1950df8bae1dSRodney W. Grimes 1951df8bae1dSRodney W. Grimes /* 1952df8bae1dSRodney W. Grimes * Try again with this object's new backing object. 1953df8bae1dSRodney W. Grimes */ 1954df8bae1dSRodney W. Grimes } 1955df8bae1dSRodney W. Grimes } 1956df8bae1dSRodney W. Grimes 1957df8bae1dSRodney W. Grimes /* 1958bff99f0dSAlan Cox * vm_object_page_remove: 1959df8bae1dSRodney W. Grimes * 196068855966SAlan Cox * For the given object, either frees or invalidates each of the 19616bbee8e2SAlan Cox * specified pages. In general, a page is freed. However, if a page is 19626bbee8e2SAlan Cox * wired for any reason other than the existence of a managed, wired 19636bbee8e2SAlan Cox * mapping, then it may be invalidated but not removed from the object. 19646bbee8e2SAlan Cox * Pages are specified by the given range ["start", "end") and the option 19656bbee8e2SAlan Cox * OBJPR_CLEANONLY. As a special case, if "end" is zero, then the range 19666bbee8e2SAlan Cox * extends from "start" to the end of the object. If the option 19676bbee8e2SAlan Cox * OBJPR_CLEANONLY is specified, then only the non-dirty pages within the 19686bbee8e2SAlan Cox * specified range are affected. If the option OBJPR_NOTMAPPED is 19696bbee8e2SAlan Cox * specified, then the pages within the specified range must have no 19706bbee8e2SAlan Cox * mappings. Otherwise, if this option is not specified, any mappings to 19716bbee8e2SAlan Cox * the specified pages are removed before the pages are freed or 19726bbee8e2SAlan Cox * invalidated. 197368855966SAlan Cox * 19746bbee8e2SAlan Cox * In general, this operation should only be performed on objects that 19756bbee8e2SAlan Cox * contain managed pages. There are, however, two exceptions. First, it 19766bbee8e2SAlan Cox * is performed on the kernel and kmem objects by vm_map_entry_delete(). 19776bbee8e2SAlan Cox * Second, it is used by msync(..., MS_INVALIDATE) to invalidate device- 19786bbee8e2SAlan Cox * backed pages. In both of these cases, the option OBJPR_CLEANONLY must 19796bbee8e2SAlan Cox * not be specified and the option OBJPR_NOTMAPPED must be specified. 1980df8bae1dSRodney W. Grimes * 1981df8bae1dSRodney W. Grimes * The object must be locked. 1982df8bae1dSRodney W. Grimes */ 198326f9a767SRodney W. Grimes void 1984ecde4b32SAlan Cox vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end, 19856bbee8e2SAlan Cox int options) 1986df8bae1dSRodney W. Grimes { 1987d031cff1SMatthew Dillon vm_page_t p, next; 1988df8bae1dSRodney W. Grimes 198989f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 199028634820SAlan Cox KASSERT((object->flags & OBJ_UNMANAGED) == 0 || 19916bbee8e2SAlan Cox (options & (OBJPR_CLEANONLY | OBJPR_NOTMAPPED)) == OBJPR_NOTMAPPED, 19926bbee8e2SAlan Cox ("vm_object_page_remove: illegal options for object %p", object)); 1993ecde4b32SAlan Cox if (object->resident_page_count == 0) 19947667839aSAlan Cox return; 1995d474eaaaSDoug Rabson vm_object_pip_add(object, 1); 199626f9a767SRodney W. Grimes again: 1997b382c10aSKonstantin Belousov p = vm_page_find_least(object, start); 19982965a453SKip Macy 199975741c04SAlan Cox /* 20006bbee8e2SAlan Cox * Here, the variable "p" is either (1) the page with the least pindex 20016bbee8e2SAlan Cox * greater than or equal to the parameter "start" or (2) NULL. 200275741c04SAlan Cox */ 20036bbee8e2SAlan Cox for (; p != NULL && (p->pindex < end || end == 0); p = next) { 2004b18bfc3dSJohn Dyson next = TAILQ_NEXT(p, listq); 200575741c04SAlan Cox 200659677d3cSAlan Cox /* 20076bbee8e2SAlan Cox * If the page is wired for any reason besides the existence 20086bbee8e2SAlan Cox * of managed, wired mappings, then it cannot be freed. For 20096bbee8e2SAlan Cox * example, fictitious pages, which represent device memory, 20106bbee8e2SAlan Cox * are inherently wired and cannot be freed. They can, 20116bbee8e2SAlan Cox * however, be invalidated if the option OBJPR_CLEANONLY is 20126bbee8e2SAlan Cox * not specified. 201359677d3cSAlan Cox */ 201463e97555SJeff Roberson if (vm_page_tryxbusy(p) == 0) { 20154cdea4a8SJeff Roberson vm_page_sleep_if_busy(p, "vmopar"); 2016fee2a2faSMark Johnston goto again; 2017fee2a2faSMark Johnston } 2018d842aa51SMark Johnston if (vm_page_wired(p)) { 2019fee2a2faSMark Johnston wired: 2020cf060942SAlan Cox if ((options & OBJPR_NOTMAPPED) == 0 && 2021cf060942SAlan Cox object->ref_count != 0) 20224fec79beSAlan Cox pmap_remove_all(p); 20236bbee8e2SAlan Cox if ((options & OBJPR_CLEANONLY) == 0) { 20240012f373SJeff Roberson vm_page_invalid(p); 2025a28042d1SAlan Cox vm_page_undirty(p); 2026a28042d1SAlan Cox } 202763e97555SJeff Roberson vm_page_xunbusy(p); 202893c5d3a4SKonstantin Belousov continue; 20290d94caffSDavid Greenman } 203068855966SAlan Cox KASSERT((p->flags & PG_FICTITIOUS) == 0, 203168855966SAlan Cox ("vm_object_page_remove: page %p is fictitious", p)); 20320012f373SJeff Roberson if ((options & OBJPR_CLEANONLY) != 0 && 20330012f373SJeff Roberson !vm_page_none_valid(p)) { 2034cf060942SAlan Cox if ((options & OBJPR_NOTMAPPED) == 0 && 2035fee2a2faSMark Johnston object->ref_count != 0 && 2036fee2a2faSMark Johnston !vm_page_try_remove_write(p)) 2037fee2a2faSMark Johnston goto wired; 203863e97555SJeff Roberson if (p->dirty != 0) { 203963e97555SJeff Roberson vm_page_xunbusy(p); 204093c5d3a4SKonstantin Belousov continue; 20412965a453SKip Macy } 204263e97555SJeff Roberson } 2043fee2a2faSMark Johnston if ((options & OBJPR_NOTMAPPED) == 0 && 2044fee2a2faSMark Johnston object->ref_count != 0 && !vm_page_try_remove_all(p)) 2045fee2a2faSMark Johnston goto wired; 20465cd29d0fSMark Johnston vm_page_free(p); 20472965a453SKip Macy } 2048f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 2049c0503609SDavid Greenman } 2050df8bae1dSRodney W. Grimes 2051df8bae1dSRodney W. Grimes /* 20523138cd36SMark Johnston * vm_object_page_noreuse: 2053936c09acSJohn Baldwin * 20543138cd36SMark Johnston * For the given object, attempt to move the specified pages to 20553138cd36SMark Johnston * the head of the inactive queue. This bypasses regular LRU 20563138cd36SMark Johnston * operation and allows the pages to be reused quickly under memory 20573138cd36SMark Johnston * pressure. If a page is wired for any reason, then it will not 20583138cd36SMark Johnston * be queued. Pages are specified by the range ["start", "end"). 20593138cd36SMark Johnston * As a special case, if "end" is zero, then the range extends from 20603138cd36SMark Johnston * "start" to the end of the object. 2061936c09acSJohn Baldwin * 2062936c09acSJohn Baldwin * This operation should only be performed on objects that 206328634820SAlan Cox * contain non-fictitious, managed pages. 2064936c09acSJohn Baldwin * 2065936c09acSJohn Baldwin * The object must be locked. 2066936c09acSJohn Baldwin */ 2067936c09acSJohn Baldwin void 20683138cd36SMark Johnston vm_object_page_noreuse(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 2069936c09acSJohn Baldwin { 207093c5d3a4SKonstantin Belousov struct mtx *mtx; 2071936c09acSJohn Baldwin vm_page_t p, next; 2072936c09acSJohn Baldwin 207352d1addaSAlan Cox VM_OBJECT_ASSERT_LOCKED(object); 207428634820SAlan Cox KASSERT((object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0, 20753138cd36SMark Johnston ("vm_object_page_noreuse: illegal object %p", object)); 2076936c09acSJohn Baldwin if (object->resident_page_count == 0) 2077936c09acSJohn Baldwin return; 2078936c09acSJohn Baldwin p = vm_page_find_least(object, start); 2079936c09acSJohn Baldwin 2080936c09acSJohn Baldwin /* 2081936c09acSJohn Baldwin * Here, the variable "p" is either (1) the page with the least pindex 2082936c09acSJohn Baldwin * greater than or equal to the parameter "start" or (2) NULL. 2083936c09acSJohn Baldwin */ 2084936c09acSJohn Baldwin mtx = NULL; 2085936c09acSJohn Baldwin for (; p != NULL && (p->pindex < end || end == 0); p = next) { 2086936c09acSJohn Baldwin next = TAILQ_NEXT(p, listq); 208793c5d3a4SKonstantin Belousov vm_page_change_lock(p, &mtx); 20883138cd36SMark Johnston vm_page_deactivate_noreuse(p); 2089936c09acSJohn Baldwin } 2090936c09acSJohn Baldwin if (mtx != NULL) 2091936c09acSJohn Baldwin mtx_unlock(mtx); 2092936c09acSJohn Baldwin } 2093936c09acSJohn Baldwin 2094936c09acSJohn Baldwin /* 2095387aabc5SAlan Cox * Populate the specified range of the object with valid pages. Returns 2096387aabc5SAlan Cox * TRUE if the range is successfully populated and FALSE otherwise. 2097387aabc5SAlan Cox * 2098387aabc5SAlan Cox * Note: This function should be optimized to pass a larger array of 2099387aabc5SAlan Cox * pages to vm_pager_get_pages() before it is applied to a non- 2100387aabc5SAlan Cox * OBJT_DEVICE object. 2101387aabc5SAlan Cox * 2102387aabc5SAlan Cox * The object must be locked. 2103387aabc5SAlan Cox */ 2104387aabc5SAlan Cox boolean_t 2105387aabc5SAlan Cox vm_object_populate(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 2106387aabc5SAlan Cox { 2107093c7f39SGleb Smirnoff vm_page_t m; 2108387aabc5SAlan Cox vm_pindex_t pindex; 2109387aabc5SAlan Cox int rv; 2110387aabc5SAlan Cox 211189f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 2112387aabc5SAlan Cox for (pindex = start; pindex < end; pindex++) { 2113c7575748SJeff Roberson rv = vm_page_grab_valid(&m, object, pindex, VM_ALLOC_NORMAL); 2114c7575748SJeff Roberson if (rv != VM_PAGER_OK) 2115387aabc5SAlan Cox break; 2116c7575748SJeff Roberson 2117387aabc5SAlan Cox /* 2118387aabc5SAlan Cox * Keep "m" busy because a subsequent iteration may unlock 2119387aabc5SAlan Cox * the object. 2120387aabc5SAlan Cox */ 2121387aabc5SAlan Cox } 2122387aabc5SAlan Cox if (pindex > start) { 2123387aabc5SAlan Cox m = vm_page_lookup(object, start); 2124387aabc5SAlan Cox while (m != NULL && m->pindex < pindex) { 2125c7aebda8SAttilio Rao vm_page_xunbusy(m); 2126387aabc5SAlan Cox m = TAILQ_NEXT(m, listq); 2127387aabc5SAlan Cox } 2128387aabc5SAlan Cox } 2129387aabc5SAlan Cox return (pindex == end); 2130387aabc5SAlan Cox } 2131387aabc5SAlan Cox 2132387aabc5SAlan Cox /* 2133df8bae1dSRodney W. Grimes * Routine: vm_object_coalesce 2134df8bae1dSRodney W. Grimes * Function: Coalesces two objects backing up adjoining 2135df8bae1dSRodney W. Grimes * regions of memory into a single object. 2136df8bae1dSRodney W. Grimes * 2137df8bae1dSRodney W. Grimes * returns TRUE if objects were combined. 2138df8bae1dSRodney W. Grimes * 2139df8bae1dSRodney W. Grimes * NOTE: Only works at the moment if the second object is NULL - 2140df8bae1dSRodney W. Grimes * if it's not, which object do we lock first? 2141df8bae1dSRodney W. Grimes * 2142df8bae1dSRodney W. Grimes * Parameters: 2143df8bae1dSRodney W. Grimes * prev_object First object to coalesce 2144df8bae1dSRodney W. Grimes * prev_offset Offset into prev_object 2145df8bae1dSRodney W. Grimes * prev_size Size of reference to prev_object 214657a21abaSAlan Cox * next_size Size of reference to the second object 21473364c323SKonstantin Belousov * reserved Indicator that extension region has 21483364c323SKonstantin Belousov * swap accounted for 2149df8bae1dSRodney W. Grimes * 2150df8bae1dSRodney W. Grimes * Conditions: 2151df8bae1dSRodney W. Grimes * The object must *not* be locked. 2152df8bae1dSRodney W. Grimes */ 21530d94caffSDavid Greenman boolean_t 215457a21abaSAlan Cox vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset, 21553364c323SKonstantin Belousov vm_size_t prev_size, vm_size_t next_size, boolean_t reserved) 2156df8bae1dSRodney W. Grimes { 2157ea41812fSAlan Cox vm_pindex_t next_pindex; 2158df8bae1dSRodney W. Grimes 215900e1854aSAlan Cox if (prev_object == NULL) 2160df8bae1dSRodney W. Grimes return (TRUE); 216163967687SJeff Roberson if ((prev_object->flags & OBJ_ANON) == 0) 216230dcfc09SJohn Dyson return (FALSE); 216330dcfc09SJohn Dyson 216463967687SJeff Roberson VM_OBJECT_WLOCK(prev_object); 2165df8bae1dSRodney W. Grimes /* 2166df8bae1dSRodney W. Grimes * Try to collapse the object first 2167df8bae1dSRodney W. Grimes */ 2168df8bae1dSRodney W. Grimes vm_object_collapse(prev_object); 2169df8bae1dSRodney W. Grimes 2170df8bae1dSRodney W. Grimes /* 21710d94caffSDavid Greenman * Can't coalesce if: . more than one reference . paged out . shadows 21720d94caffSDavid Greenman * another object . has a copy elsewhere (any of which mean that the 21730d94caffSDavid Greenman * pages not mapped to prev_entry may be in use anyway) 2174df8bae1dSRodney W. Grimes */ 21758cc7e047SJohn Dyson if (prev_object->backing_object != NULL) { 217689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(prev_object); 2177df8bae1dSRodney W. Grimes return (FALSE); 2178df8bae1dSRodney W. Grimes } 2179a316d390SJohn Dyson 2180a316d390SJohn Dyson prev_size >>= PAGE_SHIFT; 2181a316d390SJohn Dyson next_size >>= PAGE_SHIFT; 218257a21abaSAlan Cox next_pindex = OFF_TO_IDX(prev_offset) + prev_size; 21838cc7e047SJohn Dyson 21840e48e068SMark Johnston if (prev_object->ref_count > 1 && 21850e48e068SMark Johnston prev_object->size != next_pindex && 21860e48e068SMark Johnston (prev_object->flags & OBJ_ONEMAPPING) == 0) { 218789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(prev_object); 21888cc7e047SJohn Dyson return (FALSE); 21898cc7e047SJohn Dyson } 21908cc7e047SJohn Dyson 2191df8bae1dSRodney W. Grimes /* 21923364c323SKonstantin Belousov * Account for the charge. 21933364c323SKonstantin Belousov */ 2194ef694c1aSEdward Tomasz Napierala if (prev_object->cred != NULL) { 21953364c323SKonstantin Belousov 21963364c323SKonstantin Belousov /* 21973364c323SKonstantin Belousov * If prev_object was charged, then this mapping, 2198763df3ecSPedro F. Giffuni * although not charged now, may become writable 2199ef694c1aSEdward Tomasz Napierala * later. Non-NULL cred in the object would prevent 22003364c323SKonstantin Belousov * swap reservation during enabling of the write 22013364c323SKonstantin Belousov * access, so reserve swap now. Failed reservation 22023364c323SKonstantin Belousov * cause allocation of the separate object for the map 22033364c323SKonstantin Belousov * entry, and swap reservation for this entry is 22043364c323SKonstantin Belousov * managed in appropriate time. 22053364c323SKonstantin Belousov */ 2206ef694c1aSEdward Tomasz Napierala if (!reserved && !swap_reserve_by_cred(ptoa(next_size), 2207ef694c1aSEdward Tomasz Napierala prev_object->cred)) { 22089f790a17SKonstantin Belousov VM_OBJECT_WUNLOCK(prev_object); 22093364c323SKonstantin Belousov return (FALSE); 22103364c323SKonstantin Belousov } 22113364c323SKonstantin Belousov prev_object->charge += ptoa(next_size); 22123364c323SKonstantin Belousov } 22133364c323SKonstantin Belousov 22143364c323SKonstantin Belousov /* 22150d94caffSDavid Greenman * Remove any pages that may still be in the object from a previous 22160d94caffSDavid Greenman * deallocation. 2217df8bae1dSRodney W. Grimes */ 2218ea41812fSAlan Cox if (next_pindex < prev_object->size) { 22196bbee8e2SAlan Cox vm_object_page_remove(prev_object, next_pindex, next_pindex + 22206bbee8e2SAlan Cox next_size, 0); 2221ea41812fSAlan Cox if (prev_object->type == OBJT_SWAP) 2222ea41812fSAlan Cox swap_pager_freespace(prev_object, 2223ea41812fSAlan Cox next_pindex, next_size); 22243364c323SKonstantin Belousov #if 0 2225ef694c1aSEdward Tomasz Napierala if (prev_object->cred != NULL) { 22263364c323SKonstantin Belousov KASSERT(prev_object->charge >= 22273364c323SKonstantin Belousov ptoa(prev_object->size - next_pindex), 22283364c323SKonstantin Belousov ("object %p overcharged 1 %jx %jx", prev_object, 22293364c323SKonstantin Belousov (uintmax_t)next_pindex, (uintmax_t)next_size)); 22303364c323SKonstantin Belousov prev_object->charge -= ptoa(prev_object->size - 22313364c323SKonstantin Belousov next_pindex); 22323364c323SKonstantin Belousov } 22333364c323SKonstantin Belousov #endif 2234ea41812fSAlan Cox } 2235df8bae1dSRodney W. Grimes 2236df8bae1dSRodney W. Grimes /* 2237df8bae1dSRodney W. Grimes * Extend the object if necessary. 2238df8bae1dSRodney W. Grimes */ 2239ea41812fSAlan Cox if (next_pindex + next_size > prev_object->size) 2240ea41812fSAlan Cox prev_object->size = next_pindex + next_size; 2241df8bae1dSRodney W. Grimes 224289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(prev_object); 2243df8bae1dSRodney W. Grimes return (TRUE); 2244df8bae1dSRodney W. Grimes } 2245df8bae1dSRodney W. Grimes 22467a5a6352SMatthew Dillon void 22477a5a6352SMatthew Dillon vm_object_set_writeable_dirty(vm_object_t object) 22487a5a6352SMatthew Dillon { 22497a5a6352SMatthew Dillon 225067d0e293SJeff Roberson /* Only set for vnodes & tmpfs */ 225167d0e293SJeff Roberson if (object->type != OBJT_VNODE && 225267d0e293SJeff Roberson (object->flags & OBJ_TMPFS_NODE) == 0) 22533280870dSKonstantin Belousov return; 225467d0e293SJeff Roberson atomic_add_int(&object->generation, 1); 22557a5a6352SMatthew Dillon } 22567a5a6352SMatthew Dillon 225703462509SAlan Cox /* 225803462509SAlan Cox * vm_object_unwire: 225903462509SAlan Cox * 226003462509SAlan Cox * For each page offset within the specified range of the given object, 226103462509SAlan Cox * find the highest-level page in the shadow chain and unwire it. A page 226203462509SAlan Cox * must exist at every page offset, and the highest-level page must be 226303462509SAlan Cox * wired. 226403462509SAlan Cox */ 226503462509SAlan Cox void 226603462509SAlan Cox vm_object_unwire(vm_object_t object, vm_ooffset_t offset, vm_size_t length, 226703462509SAlan Cox uint8_t queue) 226803462509SAlan Cox { 226920e4afbfSKonstantin Belousov vm_object_t tobject, t1object; 227003462509SAlan Cox vm_page_t m, tm; 227103462509SAlan Cox vm_pindex_t end_pindex, pindex, tpindex; 227203462509SAlan Cox int depth, locked_depth; 227303462509SAlan Cox 227403462509SAlan Cox KASSERT((offset & PAGE_MASK) == 0, 227503462509SAlan Cox ("vm_object_unwire: offset is not page aligned")); 227603462509SAlan Cox KASSERT((length & PAGE_MASK) == 0, 227703462509SAlan Cox ("vm_object_unwire: length is not a multiple of PAGE_SIZE")); 227803462509SAlan Cox /* The wired count of a fictitious page never changes. */ 227903462509SAlan Cox if ((object->flags & OBJ_FICTITIOUS) != 0) 228003462509SAlan Cox return; 228103462509SAlan Cox pindex = OFF_TO_IDX(offset); 228203462509SAlan Cox end_pindex = pindex + atop(length); 228320e4afbfSKonstantin Belousov again: 228403462509SAlan Cox locked_depth = 1; 228503462509SAlan Cox VM_OBJECT_RLOCK(object); 228603462509SAlan Cox m = vm_page_find_least(object, pindex); 228703462509SAlan Cox while (pindex < end_pindex) { 228803462509SAlan Cox if (m == NULL || pindex < m->pindex) { 228903462509SAlan Cox /* 229003462509SAlan Cox * The first object in the shadow chain doesn't 229103462509SAlan Cox * contain a page at the current index. Therefore, 229203462509SAlan Cox * the page must exist in a backing object. 229303462509SAlan Cox */ 229403462509SAlan Cox tobject = object; 229503462509SAlan Cox tpindex = pindex; 229603462509SAlan Cox depth = 0; 229703462509SAlan Cox do { 229803462509SAlan Cox tpindex += 229903462509SAlan Cox OFF_TO_IDX(tobject->backing_object_offset); 230003462509SAlan Cox tobject = tobject->backing_object; 230103462509SAlan Cox KASSERT(tobject != NULL, 230203462509SAlan Cox ("vm_object_unwire: missing page")); 230303462509SAlan Cox if ((tobject->flags & OBJ_FICTITIOUS) != 0) 230403462509SAlan Cox goto next_page; 230503462509SAlan Cox depth++; 230603462509SAlan Cox if (depth == locked_depth) { 230703462509SAlan Cox locked_depth++; 230803462509SAlan Cox VM_OBJECT_RLOCK(tobject); 230903462509SAlan Cox } 231003462509SAlan Cox } while ((tm = vm_page_lookup(tobject, tpindex)) == 231103462509SAlan Cox NULL); 231203462509SAlan Cox } else { 231303462509SAlan Cox tm = m; 231403462509SAlan Cox m = TAILQ_NEXT(m, listq); 231503462509SAlan Cox } 231663e97555SJeff Roberson if (vm_page_trysbusy(tm) == 0) { 231787e93ea6SMark Johnston for (tobject = object; locked_depth >= 1; 231820e4afbfSKonstantin Belousov locked_depth--) { 231920e4afbfSKonstantin Belousov t1object = tobject->backing_object; 232087e93ea6SMark Johnston if (tm->object != tobject) 232120e4afbfSKonstantin Belousov VM_OBJECT_RUNLOCK(tobject); 232220e4afbfSKonstantin Belousov tobject = t1object; 232320e4afbfSKonstantin Belousov } 232420e4afbfSKonstantin Belousov vm_page_busy_sleep(tm, "unwbo", true); 232520e4afbfSKonstantin Belousov goto again; 232620e4afbfSKonstantin Belousov } 232703462509SAlan Cox vm_page_unwire(tm, queue); 232863e97555SJeff Roberson vm_page_sunbusy(tm); 232903462509SAlan Cox next_page: 233003462509SAlan Cox pindex++; 233103462509SAlan Cox } 233203462509SAlan Cox /* Release the accumulated object locks. */ 233320e4afbfSKonstantin Belousov for (tobject = object; locked_depth >= 1; locked_depth--) { 233420e4afbfSKonstantin Belousov t1object = tobject->backing_object; 233520e4afbfSKonstantin Belousov VM_OBJECT_RUNLOCK(tobject); 233620e4afbfSKonstantin Belousov tobject = t1object; 233703462509SAlan Cox } 233803462509SAlan Cox } 233903462509SAlan Cox 23400951bd36SEric van Gyzen /* 23410951bd36SEric van Gyzen * Return the vnode for the given object, or NULL if none exists. 23420951bd36SEric van Gyzen * For tmpfs objects, the function may return NULL if there is 23430951bd36SEric van Gyzen * no vnode allocated at the time of the call. 23440951bd36SEric van Gyzen */ 234563e4c6cdSEric van Gyzen struct vnode * 234663e4c6cdSEric van Gyzen vm_object_vnode(vm_object_t object) 234763e4c6cdSEric van Gyzen { 23480951bd36SEric van Gyzen struct vnode *vp; 234963e4c6cdSEric van Gyzen 235063e4c6cdSEric van Gyzen VM_OBJECT_ASSERT_LOCKED(object); 23510951bd36SEric van Gyzen if (object->type == OBJT_VNODE) { 23520951bd36SEric van Gyzen vp = object->handle; 23530951bd36SEric van Gyzen KASSERT(vp != NULL, ("%s: OBJT_VNODE has no vnode", __func__)); 23540951bd36SEric van Gyzen } else if (object->type == OBJT_SWAP && 23550951bd36SEric van Gyzen (object->flags & OBJ_TMPFS) != 0) { 23560951bd36SEric van Gyzen vp = object->un_pager.swp.swp_tmpfs; 23570951bd36SEric van Gyzen KASSERT(vp != NULL, ("%s: OBJT_TMPFS has no vnode", __func__)); 23580951bd36SEric van Gyzen } else { 23590951bd36SEric van Gyzen vp = NULL; 23600951bd36SEric van Gyzen } 23610951bd36SEric van Gyzen return (vp); 236263e4c6cdSEric van Gyzen } 236363e4c6cdSEric van Gyzen 2364205be21dSJeff Roberson 2365205be21dSJeff Roberson /* 2366205be21dSJeff Roberson * Busy the vm object. This prevents new pages belonging to the object from 2367205be21dSJeff Roberson * becoming busy. Existing pages persist as busy. Callers are responsible 2368205be21dSJeff Roberson * for checking page state before proceeding. 2369205be21dSJeff Roberson */ 2370205be21dSJeff Roberson void 2371205be21dSJeff Roberson vm_object_busy(vm_object_t obj) 2372205be21dSJeff Roberson { 2373205be21dSJeff Roberson 2374205be21dSJeff Roberson VM_OBJECT_ASSERT_LOCKED(obj); 2375205be21dSJeff Roberson 2376205be21dSJeff Roberson refcount_acquire(&obj->busy); 2377205be21dSJeff Roberson /* The fence is required to order loads of page busy. */ 2378205be21dSJeff Roberson atomic_thread_fence_acq_rel(); 2379205be21dSJeff Roberson } 2380205be21dSJeff Roberson 2381205be21dSJeff Roberson void 2382205be21dSJeff Roberson vm_object_unbusy(vm_object_t obj) 2383205be21dSJeff Roberson { 2384205be21dSJeff Roberson 2385205be21dSJeff Roberson 2386205be21dSJeff Roberson refcount_release(&obj->busy); 2387205be21dSJeff Roberson } 2388205be21dSJeff Roberson 2389205be21dSJeff Roberson void 2390205be21dSJeff Roberson vm_object_busy_wait(vm_object_t obj, const char *wmesg) 2391205be21dSJeff Roberson { 2392205be21dSJeff Roberson 2393205be21dSJeff Roberson VM_OBJECT_ASSERT_UNLOCKED(obj); 2394205be21dSJeff Roberson 2395205be21dSJeff Roberson if (obj->busy) 2396205be21dSJeff Roberson refcount_sleep(&obj->busy, wmesg, PVM); 2397205be21dSJeff Roberson } 2398205be21dSJeff Roberson 23995e38e3f5SEric van Gyzen /* 24005e38e3f5SEric van Gyzen * Return the kvme type of the given object. 24015e38e3f5SEric van Gyzen * If vpp is not NULL, set it to the object's vm_object_vnode() or NULL. 24025e38e3f5SEric van Gyzen */ 24035e38e3f5SEric van Gyzen int 24045e38e3f5SEric van Gyzen vm_object_kvme_type(vm_object_t object, struct vnode **vpp) 24055e38e3f5SEric van Gyzen { 24065e38e3f5SEric van Gyzen 24075e38e3f5SEric van Gyzen VM_OBJECT_ASSERT_LOCKED(object); 24085e38e3f5SEric van Gyzen if (vpp != NULL) 24095e38e3f5SEric van Gyzen *vpp = vm_object_vnode(object); 24105e38e3f5SEric van Gyzen switch (object->type) { 24115e38e3f5SEric van Gyzen case OBJT_DEFAULT: 24125e38e3f5SEric van Gyzen return (KVME_TYPE_DEFAULT); 24135e38e3f5SEric van Gyzen case OBJT_VNODE: 24145e38e3f5SEric van Gyzen return (KVME_TYPE_VNODE); 24155e38e3f5SEric van Gyzen case OBJT_SWAP: 24165e38e3f5SEric van Gyzen if ((object->flags & OBJ_TMPFS_NODE) != 0) 24175e38e3f5SEric van Gyzen return (KVME_TYPE_VNODE); 24185e38e3f5SEric van Gyzen return (KVME_TYPE_SWAP); 24195e38e3f5SEric van Gyzen case OBJT_DEVICE: 24205e38e3f5SEric van Gyzen return (KVME_TYPE_DEVICE); 24215e38e3f5SEric van Gyzen case OBJT_PHYS: 24225e38e3f5SEric van Gyzen return (KVME_TYPE_PHYS); 24235e38e3f5SEric van Gyzen case OBJT_DEAD: 24245e38e3f5SEric van Gyzen return (KVME_TYPE_DEAD); 24255e38e3f5SEric van Gyzen case OBJT_SG: 24265e38e3f5SEric van Gyzen return (KVME_TYPE_SG); 24275e38e3f5SEric van Gyzen case OBJT_MGTDEVICE: 24285e38e3f5SEric van Gyzen return (KVME_TYPE_MGTDEVICE); 24295e38e3f5SEric van Gyzen default: 24305e38e3f5SEric van Gyzen return (KVME_TYPE_UNKNOWN); 24315e38e3f5SEric van Gyzen } 24325e38e3f5SEric van Gyzen } 24335e38e3f5SEric van Gyzen 2434ff87ae35SJohn Baldwin static int 2435ff87ae35SJohn Baldwin sysctl_vm_object_list(SYSCTL_HANDLER_ARGS) 2436ff87ae35SJohn Baldwin { 24370ecee546SKonstantin Belousov struct kinfo_vmobject *kvo; 2438ff87ae35SJohn Baldwin char *fullpath, *freepath; 2439ff87ae35SJohn Baldwin struct vnode *vp; 2440ff87ae35SJohn Baldwin struct vattr va; 2441ff87ae35SJohn Baldwin vm_object_t obj; 2442ff87ae35SJohn Baldwin vm_page_t m; 2443ff87ae35SJohn Baldwin int count, error; 2444ff87ae35SJohn Baldwin 2445ff87ae35SJohn Baldwin if (req->oldptr == NULL) { 2446ff87ae35SJohn Baldwin /* 2447ff87ae35SJohn Baldwin * If an old buffer has not been provided, generate an 2448ff87ae35SJohn Baldwin * estimate of the space needed for a subsequent call. 2449ff87ae35SJohn Baldwin */ 2450ff87ae35SJohn Baldwin mtx_lock(&vm_object_list_mtx); 2451ff87ae35SJohn Baldwin count = 0; 2452ff87ae35SJohn Baldwin TAILQ_FOREACH(obj, &vm_object_list, object_list) { 2453ff87ae35SJohn Baldwin if (obj->type == OBJT_DEAD) 2454ff87ae35SJohn Baldwin continue; 2455ff87ae35SJohn Baldwin count++; 2456ff87ae35SJohn Baldwin } 2457ff87ae35SJohn Baldwin mtx_unlock(&vm_object_list_mtx); 2458ff87ae35SJohn Baldwin return (SYSCTL_OUT(req, NULL, sizeof(struct kinfo_vmobject) * 2459ff87ae35SJohn Baldwin count * 11 / 10)); 2460ff87ae35SJohn Baldwin } 2461ff87ae35SJohn Baldwin 24620ecee546SKonstantin Belousov kvo = malloc(sizeof(*kvo), M_TEMP, M_WAITOK); 2463ff87ae35SJohn Baldwin error = 0; 2464ff87ae35SJohn Baldwin 2465ff87ae35SJohn Baldwin /* 2466ff87ae35SJohn Baldwin * VM objects are type stable and are never removed from the 2467ff87ae35SJohn Baldwin * list once added. This allows us to safely read obj->object_list 2468ff87ae35SJohn Baldwin * after reacquiring the VM object lock. 2469ff87ae35SJohn Baldwin */ 2470ff87ae35SJohn Baldwin mtx_lock(&vm_object_list_mtx); 2471ff87ae35SJohn Baldwin TAILQ_FOREACH(obj, &vm_object_list, object_list) { 2472ff87ae35SJohn Baldwin if (obj->type == OBJT_DEAD) 2473ff87ae35SJohn Baldwin continue; 2474ff87ae35SJohn Baldwin VM_OBJECT_RLOCK(obj); 2475ff87ae35SJohn Baldwin if (obj->type == OBJT_DEAD) { 2476ff87ae35SJohn Baldwin VM_OBJECT_RUNLOCK(obj); 2477ff87ae35SJohn Baldwin continue; 2478ff87ae35SJohn Baldwin } 2479ff87ae35SJohn Baldwin mtx_unlock(&vm_object_list_mtx); 24800ecee546SKonstantin Belousov kvo->kvo_size = ptoa(obj->size); 24810ecee546SKonstantin Belousov kvo->kvo_resident = obj->resident_page_count; 24820ecee546SKonstantin Belousov kvo->kvo_ref_count = obj->ref_count; 24830ecee546SKonstantin Belousov kvo->kvo_shadow_count = obj->shadow_count; 24840ecee546SKonstantin Belousov kvo->kvo_memattr = obj->memattr; 24850ecee546SKonstantin Belousov kvo->kvo_active = 0; 24860ecee546SKonstantin Belousov kvo->kvo_inactive = 0; 2487ff87ae35SJohn Baldwin TAILQ_FOREACH(m, &obj->memq, listq) { 2488ff87ae35SJohn Baldwin /* 2489ff87ae35SJohn Baldwin * A page may belong to the object but be 2490ff87ae35SJohn Baldwin * dequeued and set to PQ_NONE while the 2491ff87ae35SJohn Baldwin * object lock is not held. This makes the 2492ff87ae35SJohn Baldwin * reads of m->queue below racy, and we do not 2493ff87ae35SJohn Baldwin * count pages set to PQ_NONE. However, this 2494ff87ae35SJohn Baldwin * sysctl is only meant to give an 2495ff87ae35SJohn Baldwin * approximation of the system anyway. 2496ff87ae35SJohn Baldwin */ 24975cff1f4dSMark Johnston if (m->a.queue == PQ_ACTIVE) 24980ecee546SKonstantin Belousov kvo->kvo_active++; 24995cff1f4dSMark Johnston else if (m->a.queue == PQ_INACTIVE) 25000ecee546SKonstantin Belousov kvo->kvo_inactive++; 2501ff87ae35SJohn Baldwin } 2502ff87ae35SJohn Baldwin 25030ecee546SKonstantin Belousov kvo->kvo_vn_fileid = 0; 25040ecee546SKonstantin Belousov kvo->kvo_vn_fsid = 0; 25050ecee546SKonstantin Belousov kvo->kvo_vn_fsid_freebsd11 = 0; 2506ff87ae35SJohn Baldwin freepath = NULL; 2507ff87ae35SJohn Baldwin fullpath = ""; 25085e38e3f5SEric van Gyzen kvo->kvo_type = vm_object_kvme_type(obj, &vp); 25095e38e3f5SEric van Gyzen if (vp != NULL) 2510ff87ae35SJohn Baldwin vref(vp); 2511ff87ae35SJohn Baldwin VM_OBJECT_RUNLOCK(obj); 2512ff87ae35SJohn Baldwin if (vp != NULL) { 2513ff87ae35SJohn Baldwin vn_fullpath(curthread, vp, &fullpath, &freepath); 2514ff87ae35SJohn Baldwin vn_lock(vp, LK_SHARED | LK_RETRY); 2515ff87ae35SJohn Baldwin if (VOP_GETATTR(vp, &va, curthread->td_ucred) == 0) { 25160ecee546SKonstantin Belousov kvo->kvo_vn_fileid = va.va_fileid; 25170ecee546SKonstantin Belousov kvo->kvo_vn_fsid = va.va_fsid; 25180ecee546SKonstantin Belousov kvo->kvo_vn_fsid_freebsd11 = va.va_fsid; 251969921123SKonstantin Belousov /* truncate */ 2520ff87ae35SJohn Baldwin } 2521ff87ae35SJohn Baldwin vput(vp); 2522ff87ae35SJohn Baldwin } 2523ff87ae35SJohn Baldwin 25240ecee546SKonstantin Belousov strlcpy(kvo->kvo_path, fullpath, sizeof(kvo->kvo_path)); 2525ff87ae35SJohn Baldwin if (freepath != NULL) 2526ff87ae35SJohn Baldwin free(freepath, M_TEMP); 2527ff87ae35SJohn Baldwin 2528ff87ae35SJohn Baldwin /* Pack record size down */ 25290ecee546SKonstantin Belousov kvo->kvo_structsize = offsetof(struct kinfo_vmobject, kvo_path) 25300ecee546SKonstantin Belousov + strlen(kvo->kvo_path) + 1; 25310ecee546SKonstantin Belousov kvo->kvo_structsize = roundup(kvo->kvo_structsize, 2532ff87ae35SJohn Baldwin sizeof(uint64_t)); 25330ecee546SKonstantin Belousov error = SYSCTL_OUT(req, kvo, kvo->kvo_structsize); 2534ff87ae35SJohn Baldwin mtx_lock(&vm_object_list_mtx); 2535ff87ae35SJohn Baldwin if (error) 2536ff87ae35SJohn Baldwin break; 2537ff87ae35SJohn Baldwin } 2538ff87ae35SJohn Baldwin mtx_unlock(&vm_object_list_mtx); 25390ecee546SKonstantin Belousov free(kvo, M_TEMP); 2540ff87ae35SJohn Baldwin return (error); 2541ff87ae35SJohn Baldwin } 2542ff87ae35SJohn Baldwin SYSCTL_PROC(_vm, OID_AUTO, objects, CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_SKIP | 2543ff87ae35SJohn Baldwin CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_object_list, "S,kinfo_vmobject", 2544ff87ae35SJohn Baldwin "List of VM objects"); 2545ff87ae35SJohn Baldwin 2546c7c34a24SBruce Evans #include "opt_ddb.h" 2547c3cb3e12SDavid Greenman #ifdef DDB 2548c7c34a24SBruce Evans #include <sys/kernel.h> 2549c7c34a24SBruce Evans 2550ce9edcf5SPoul-Henning Kamp #include <sys/cons.h> 2551c7c34a24SBruce Evans 2552c7c34a24SBruce Evans #include <ddb/ddb.h> 2553c7c34a24SBruce Evans 2554cac597e4SBruce Evans static int 25551b40f8c0SMatthew Dillon _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry) 2556a1f6d91cSDavid Greenman { 2557a1f6d91cSDavid Greenman vm_map_t tmpm; 2558a1f6d91cSDavid Greenman vm_map_entry_t tmpe; 2559a1f6d91cSDavid Greenman vm_object_t obj; 2560a1f6d91cSDavid Greenman 2561a1f6d91cSDavid Greenman if (map == 0) 2562a1f6d91cSDavid Greenman return 0; 2563a1f6d91cSDavid Greenman 2564a1f6d91cSDavid Greenman if (entry == 0) { 25652288078cSDoug Moore VM_MAP_ENTRY_FOREACH(tmpe, map) { 2566a1f6d91cSDavid Greenman if (_vm_object_in_map(map, object, tmpe)) { 2567a1f6d91cSDavid Greenman return 1; 2568a1f6d91cSDavid Greenman } 2569a1f6d91cSDavid Greenman } 25709fdfe602SMatthew Dillon } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 25719fdfe602SMatthew Dillon tmpm = entry->object.sub_map; 25722288078cSDoug Moore VM_MAP_ENTRY_FOREACH(tmpe, tmpm) { 2573a1f6d91cSDavid Greenman if (_vm_object_in_map(tmpm, object, tmpe)) { 2574a1f6d91cSDavid Greenman return 1; 2575a1f6d91cSDavid Greenman } 2576a1f6d91cSDavid Greenman } 25778aef1712SMatthew Dillon } else if ((obj = entry->object.vm_object) != NULL) { 257824a1cce3SDavid Greenman for (; obj; obj = obj->backing_object) 2579a1f6d91cSDavid Greenman if (obj == object) { 2580a1f6d91cSDavid Greenman return 1; 2581a1f6d91cSDavid Greenman } 2582a1f6d91cSDavid Greenman } 2583a1f6d91cSDavid Greenman return 0; 2584a1f6d91cSDavid Greenman } 2585a1f6d91cSDavid Greenman 2586cac597e4SBruce Evans static int 25871b40f8c0SMatthew Dillon vm_object_in_map(vm_object_t object) 2588a1f6d91cSDavid Greenman { 2589a1f6d91cSDavid Greenman struct proc *p; 25901005a129SJohn Baldwin 259160517fd1SJohn Baldwin /* sx_slock(&allproc_lock); */ 2592f67af5c9SXin LI FOREACH_PROC_IN_SYSTEM(p) { 2593a1f6d91cSDavid Greenman if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */) 2594a1f6d91cSDavid Greenman continue; 2595553629ebSJake Burkholder if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) { 259660517fd1SJohn Baldwin /* sx_sunlock(&allproc_lock); */ 2597a1f6d91cSDavid Greenman return 1; 2598a1f6d91cSDavid Greenman } 2599553629ebSJake Burkholder } 260060517fd1SJohn Baldwin /* sx_sunlock(&allproc_lock); */ 2601a1f6d91cSDavid Greenman if (_vm_object_in_map(kernel_map, object, 0)) 2602a1f6d91cSDavid Greenman return 1; 2603a1f6d91cSDavid Greenman return 0; 2604a1f6d91cSDavid Greenman } 2605a1f6d91cSDavid Greenman 2606c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check) 2607f708ef1bSPoul-Henning Kamp { 2608a1f6d91cSDavid Greenman vm_object_t object; 2609a1f6d91cSDavid Greenman 2610a1f6d91cSDavid Greenman /* 2611a1f6d91cSDavid Greenman * make sure that internal objs are in a map somewhere 2612a1f6d91cSDavid Greenman * and none have zero ref counts. 2613a1f6d91cSDavid Greenman */ 2614cc64b484SAlfred Perlstein TAILQ_FOREACH(object, &vm_object_list, object_list) { 261532362449SKonstantin Belousov if ((object->flags & OBJ_ANON) != 0) { 2616a1f6d91cSDavid Greenman if (object->ref_count == 0) { 26173efc015bSPeter Wemm db_printf("vmochk: internal obj has zero ref count: %ld\n", 26183efc015bSPeter Wemm (long)object->size); 2619a1f6d91cSDavid Greenman } 2620a1f6d91cSDavid Greenman if (!vm_object_in_map(object)) { 2621fc62ef1fSBruce Evans db_printf( 2622fc62ef1fSBruce Evans "vmochk: internal obj is not in a map: " 2623fc62ef1fSBruce Evans "ref: %d, size: %lu: 0x%lx, backing_object: %p\n", 2624fc62ef1fSBruce Evans object->ref_count, (u_long)object->size, 2625fc62ef1fSBruce Evans (u_long)object->size, 2626fc62ef1fSBruce Evans (void *)object->backing_object); 2627a1f6d91cSDavid Greenman } 2628a1f6d91cSDavid Greenman } 2629a1f6d91cSDavid Greenman } 2630a1f6d91cSDavid Greenman } 2631a1f6d91cSDavid Greenman 263226f9a767SRodney W. Grimes /* 2633df8bae1dSRodney W. Grimes * vm_object_print: [ debug ] 2634df8bae1dSRodney W. Grimes */ 2635c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static) 2636df8bae1dSRodney W. Grimes { 2637c7c34a24SBruce Evans /* XXX convert args. */ 2638c7c34a24SBruce Evans vm_object_t object = (vm_object_t)addr; 2639c7c34a24SBruce Evans boolean_t full = have_addr; 2640c7c34a24SBruce Evans 2641d031cff1SMatthew Dillon vm_page_t p; 2642df8bae1dSRodney W. Grimes 2643c7c34a24SBruce Evans /* XXX count is an (unused) arg. Avoid shadowing it. */ 2644c7c34a24SBruce Evans #define count was_count 2645c7c34a24SBruce Evans 2646d031cff1SMatthew Dillon int count; 2647df8bae1dSRodney W. Grimes 2648df8bae1dSRodney W. Grimes if (object == NULL) 2649df8bae1dSRodney W. Grimes return; 2650df8bae1dSRodney W. Grimes 2651eb95adefSBruce Evans db_iprintf( 2652ef694c1aSEdward Tomasz Napierala "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x ruid %d charge %jx\n", 2653e47cd172SMaxime Henrion object, (int)object->type, (uintmax_t)object->size, 26543364c323SKonstantin Belousov object->resident_page_count, object->ref_count, object->flags, 2655ef694c1aSEdward Tomasz Napierala object->cred ? object->cred->cr_ruid : -1, (uintmax_t)object->charge); 2656e47cd172SMaxime Henrion db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n", 26571c7c3c6aSMatthew Dillon object->shadow_count, 2658eb95adefSBruce Evans object->backing_object ? object->backing_object->ref_count : 0, 2659e47cd172SMaxime Henrion object->backing_object, (uintmax_t)object->backing_object_offset); 2660df8bae1dSRodney W. Grimes 2661df8bae1dSRodney W. Grimes if (!full) 2662df8bae1dSRodney W. Grimes return; 2663df8bae1dSRodney W. Grimes 2664c7c34a24SBruce Evans db_indent += 2; 2665df8bae1dSRodney W. Grimes count = 0; 2666fc2ffbe6SPoul-Henning Kamp TAILQ_FOREACH(p, &object->memq, listq) { 2667df8bae1dSRodney W. Grimes if (count == 0) 2668c7c34a24SBruce Evans db_iprintf("memory:="); 2669df8bae1dSRodney W. Grimes else if (count == 6) { 2670c7c34a24SBruce Evans db_printf("\n"); 2671c7c34a24SBruce Evans db_iprintf(" ..."); 2672df8bae1dSRodney W. Grimes count = 0; 2673df8bae1dSRodney W. Grimes } else 2674c7c34a24SBruce Evans db_printf(","); 2675df8bae1dSRodney W. Grimes count++; 2676df8bae1dSRodney W. Grimes 2677e47cd172SMaxime Henrion db_printf("(off=0x%jx,page=0x%jx)", 2678e47cd172SMaxime Henrion (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p)); 2679df8bae1dSRodney W. Grimes } 2680df8bae1dSRodney W. Grimes if (count != 0) 2681c7c34a24SBruce Evans db_printf("\n"); 2682c7c34a24SBruce Evans db_indent -= 2; 2683df8bae1dSRodney W. Grimes } 26845070c7f8SJohn Dyson 2685c7c34a24SBruce Evans /* XXX. */ 2686c7c34a24SBruce Evans #undef count 2687c7c34a24SBruce Evans 2688c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */ 26895070c7f8SJohn Dyson void 26901b40f8c0SMatthew Dillon vm_object_print( 26911b40f8c0SMatthew Dillon /* db_expr_t */ long addr, 26921b40f8c0SMatthew Dillon boolean_t have_addr, 26931b40f8c0SMatthew Dillon /* db_expr_t */ long count, 26941b40f8c0SMatthew Dillon char *modif) 2695c7c34a24SBruce Evans { 2696c7c34a24SBruce Evans vm_object_print_static(addr, have_addr, count, modif); 2697c7c34a24SBruce Evans } 2698c7c34a24SBruce Evans 2699c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages) 27005070c7f8SJohn Dyson { 27015070c7f8SJohn Dyson vm_object_t object; 2702bb2ac86fSKonstantin Belousov vm_pindex_t fidx; 2703bb2ac86fSKonstantin Belousov vm_paddr_t pa; 2704bb2ac86fSKonstantin Belousov vm_page_t m, prev_m; 2705bb2ac86fSKonstantin Belousov int rcount, nl, c; 2706cc64b484SAlfred Perlstein 2707bb2ac86fSKonstantin Belousov nl = 0; 2708cc64b484SAlfred Perlstein TAILQ_FOREACH(object, &vm_object_list, object_list) { 2709fc62ef1fSBruce Evans db_printf("new object: %p\n", (void *)object); 27105070c7f8SJohn Dyson if (nl > 18) { 27115070c7f8SJohn Dyson c = cngetc(); 27125070c7f8SJohn Dyson if (c != ' ') 27135070c7f8SJohn Dyson return; 27145070c7f8SJohn Dyson nl = 0; 27155070c7f8SJohn Dyson } 27165070c7f8SJohn Dyson nl++; 27175070c7f8SJohn Dyson rcount = 0; 27185070c7f8SJohn Dyson fidx = 0; 2719bb2ac86fSKonstantin Belousov pa = -1; 2720bb2ac86fSKonstantin Belousov TAILQ_FOREACH(m, &object->memq, listq) { 2721bb2ac86fSKonstantin Belousov if (m->pindex > 128) 2722bb2ac86fSKonstantin Belousov break; 2723bb2ac86fSKonstantin Belousov if ((prev_m = TAILQ_PREV(m, pglist, listq)) != NULL && 2724bb2ac86fSKonstantin Belousov prev_m->pindex + 1 != m->pindex) { 27255070c7f8SJohn Dyson if (rcount) { 27263efc015bSPeter Wemm db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 27273efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 27285070c7f8SJohn Dyson if (nl > 18) { 27295070c7f8SJohn Dyson c = cngetc(); 27305070c7f8SJohn Dyson if (c != ' ') 27315070c7f8SJohn Dyson return; 27325070c7f8SJohn Dyson nl = 0; 27335070c7f8SJohn Dyson } 27345070c7f8SJohn Dyson nl++; 27355070c7f8SJohn Dyson rcount = 0; 27365070c7f8SJohn Dyson } 27375070c7f8SJohn Dyson } 27385070c7f8SJohn Dyson if (rcount && 27395070c7f8SJohn Dyson (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) { 27405070c7f8SJohn Dyson ++rcount; 27415070c7f8SJohn Dyson continue; 27425070c7f8SJohn Dyson } 27435070c7f8SJohn Dyson if (rcount) { 27442446e4f0SAlan Cox db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 27453efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 27465070c7f8SJohn Dyson if (nl > 18) { 27475070c7f8SJohn Dyson c = cngetc(); 27485070c7f8SJohn Dyson if (c != ' ') 27495070c7f8SJohn Dyson return; 27505070c7f8SJohn Dyson nl = 0; 27515070c7f8SJohn Dyson } 27525070c7f8SJohn Dyson nl++; 27535070c7f8SJohn Dyson } 2754bb2ac86fSKonstantin Belousov fidx = m->pindex; 27555070c7f8SJohn Dyson pa = VM_PAGE_TO_PHYS(m); 27565070c7f8SJohn Dyson rcount = 1; 27575070c7f8SJohn Dyson } 27585070c7f8SJohn Dyson if (rcount) { 27593efc015bSPeter Wemm db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 27603efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 27615070c7f8SJohn Dyson if (nl > 18) { 27625070c7f8SJohn Dyson c = cngetc(); 27635070c7f8SJohn Dyson if (c != ' ') 27645070c7f8SJohn Dyson return; 27655070c7f8SJohn Dyson nl = 0; 27665070c7f8SJohn Dyson } 27675070c7f8SJohn Dyson nl++; 27685070c7f8SJohn Dyson } 27695070c7f8SJohn Dyson } 27705070c7f8SJohn Dyson } 2771c3cb3e12SDavid Greenman #endif /* DDB */ 2772