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, 24463967687SJeff Roberson vm_object_t object) 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; 27124a1cce3SDavid Greenman object->handle = NULL; 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 - 29363967687SJeff Roberson VM_MIN_KERNEL_ADDRESS), OBJ_UNMANAGED, kernel_object); 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); 43763967687SJeff Roberson _vm_object_allocate(type, size, flags, object); 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 45063967687SJeff Roberson vm_object_allocate_anon(vm_pindex_t size) 45163967687SJeff Roberson { 45263967687SJeff Roberson vm_object_t object; 45390688d13SAlan Cox 45490688d13SAlan Cox object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK); 45563967687SJeff Roberson _vm_object_allocate(OBJT_DEFAULT, size, OBJ_ANON | OBJ_ONEMAPPING, 45663967687SJeff Roberson object); 45763967687SJeff Roberson 45890688d13SAlan Cox return (object); 45926f9a767SRodney W. Grimes } 46026f9a767SRodney W. Grimes 46126f9a767SRodney W. Grimes 46226f9a767SRodney W. Grimes /* 463df8bae1dSRodney W. Grimes * vm_object_reference: 464df8bae1dSRodney W. Grimes * 46515347817SAlan Cox * Gets another reference to the given object. Note: OBJ_DEAD 46615347817SAlan Cox * objects can be referenced during final cleaning. 467df8bae1dSRodney W. Grimes */ 4686476c0d2SJohn Dyson void 4691b40f8c0SMatthew Dillon vm_object_reference(vm_object_t object) 470df8bae1dSRodney W. Grimes { 471*a67d5408SJeff Roberson struct vnode *vp; 472*a67d5408SJeff Roberson u_int old; 473*a67d5408SJeff Roberson 474df8bae1dSRodney W. Grimes if (object == NULL) 475df8bae1dSRodney W. Grimes return; 476*a67d5408SJeff Roberson 477*a67d5408SJeff Roberson /* 478*a67d5408SJeff Roberson * Many places assume exclusive access to objects with a single 479*a67d5408SJeff Roberson * ref. vm_object_collapse() in particular will directly mainpulate 480*a67d5408SJeff Roberson * references for objects in this state. vnode objects only need 481*a67d5408SJeff Roberson * the lock for the first ref to reference the vnode. 482*a67d5408SJeff Roberson */ 483*a67d5408SJeff Roberson if (!refcount_acquire_if_gt(&object->ref_count, 484*a67d5408SJeff Roberson object->type == OBJT_VNODE ? 0 : 1)) { 48551df5321SJeff Roberson VM_OBJECT_RLOCK(object); 486*a67d5408SJeff Roberson old = refcount_acquire(&object->ref_count); 487*a67d5408SJeff Roberson if (object->type == OBJT_VNODE && old == 0) { 488*a67d5408SJeff Roberson vp = object->handle; 489*a67d5408SJeff Roberson vref(vp); 490*a67d5408SJeff Roberson } 49151df5321SJeff Roberson VM_OBJECT_RUNLOCK(object); 49295e5e988SJohn Dyson } 493*a67d5408SJeff Roberson } 49495e5e988SJohn Dyson 49523955314SAlfred Perlstein /* 496b921a12bSAlan Cox * vm_object_reference_locked: 497b921a12bSAlan Cox * 498b921a12bSAlan Cox * Gets another reference to the given object. 499b921a12bSAlan Cox * 500b921a12bSAlan Cox * The object must be locked. 501b921a12bSAlan Cox */ 502b921a12bSAlan Cox void 503b921a12bSAlan Cox vm_object_reference_locked(vm_object_t object) 504b921a12bSAlan Cox { 505b921a12bSAlan Cox struct vnode *vp; 506*a67d5408SJeff Roberson u_int old; 507b921a12bSAlan Cox 50851df5321SJeff Roberson VM_OBJECT_ASSERT_LOCKED(object); 509*a67d5408SJeff Roberson old = refcount_acquire(&object->ref_count); 510*a67d5408SJeff Roberson if (object->type == OBJT_VNODE && old == 0) { 511b921a12bSAlan Cox vp = object->handle; 512b921a12bSAlan Cox vref(vp); 513b921a12bSAlan Cox } 514b921a12bSAlan Cox } 515b921a12bSAlan Cox 516b921a12bSAlan Cox /* 5179d5abbddSJens Schweikhardt * Handle deallocating an object of type OBJT_VNODE. 51823955314SAlfred Perlstein */ 51902dd8331SAlan Cox static void 5201b40f8c0SMatthew Dillon vm_object_vndeallocate(vm_object_t object) 52195e5e988SJohn Dyson { 52295e5e988SJohn Dyson struct vnode *vp = (struct vnode *) object->handle; 523219cbf59SEivind Eklund 5245526d2d9SEivind Eklund KASSERT(object->type == OBJT_VNODE, 5255526d2d9SEivind Eklund ("vm_object_vndeallocate: not a vnode object")); 526219cbf59SEivind Eklund KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp")); 52795e5e988SJohn Dyson 528*a67d5408SJeff Roberson if (!umtx_shm_vnobj_persistent) 5291bdbd705SKonstantin Belousov umtx_shm_object_terminated(object); 5301bdbd705SKonstantin Belousov 531*a67d5408SJeff Roberson VM_OBJECT_WUNLOCK(object); 53203fa5b34SKonstantin Belousov /* vrele may need the vnode lock. */ 53347221757SJohn Dyson vrele(vp); 53486769ac0SKonstantin Belousov } 535df8bae1dSRodney W. Grimes 536df8bae1dSRodney W. Grimes /* 537df8bae1dSRodney W. Grimes * vm_object_deallocate: 538df8bae1dSRodney W. Grimes * 539df8bae1dSRodney W. Grimes * Release a reference to the specified object, 540df8bae1dSRodney W. Grimes * gained either through a vm_object_allocate 541df8bae1dSRodney W. Grimes * or a vm_object_reference call. When all references 542df8bae1dSRodney W. Grimes * are gone, storage associated with this object 543df8bae1dSRodney W. Grimes * may be relinquished. 544df8bae1dSRodney W. Grimes * 545df8bae1dSRodney W. Grimes * No object may be locked. 546df8bae1dSRodney W. Grimes */ 54726f9a767SRodney W. Grimes void 5481b40f8c0SMatthew Dillon vm_object_deallocate(vm_object_t object) 549df8bae1dSRodney W. Grimes { 55032362449SKonstantin Belousov vm_object_t robject, temp; 551*a67d5408SJeff Roberson bool last, released; 552df8bae1dSRodney W. Grimes 553df8bae1dSRodney W. Grimes while (object != NULL) { 55451df5321SJeff Roberson /* 55551df5321SJeff Roberson * If the reference count goes to 0 we start calling 55651df5321SJeff Roberson * vm_object_terminate() on the object chain. A ref count 55751df5321SJeff Roberson * of 1 may be a special case depending on the shadow count 55851df5321SJeff Roberson * being 0 or 1. These cases require a write lock on the 55951df5321SJeff Roberson * object. 56051df5321SJeff Roberson */ 56163967687SJeff Roberson if ((object->flags & OBJ_ANON) == 0) 56263967687SJeff Roberson released = refcount_release_if_gt(&object->ref_count, 1); 56363967687SJeff Roberson else 56451df5321SJeff Roberson released = refcount_release_if_gt(&object->ref_count, 2); 56551df5321SJeff Roberson if (released) 56651df5321SJeff Roberson return; 56751df5321SJeff Roberson 56851df5321SJeff Roberson VM_OBJECT_WLOCK(object); 5698125b1e6SAlfred Perlstein KASSERT(object->ref_count != 0, 5708125b1e6SAlfred Perlstein ("vm_object_deallocate: object deallocated too many times: %d", object->type)); 5712be70f79SJohn Dyson 572*a67d5408SJeff Roberson last = refcount_release(&object->ref_count); 573*a67d5408SJeff Roberson if (object->type == OBJT_VNODE) { 574*a67d5408SJeff Roberson if (last) 575*a67d5408SJeff Roberson vm_object_vndeallocate(object); 576*a67d5408SJeff Roberson else 577*a67d5408SJeff Roberson VM_OBJECT_WUNLOCK(object); 578*a67d5408SJeff Roberson return; 579*a67d5408SJeff Roberson } 5808125b1e6SAlfred Perlstein if (object->ref_count > 1) { 58189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 58223b186d3SAlan Cox return; 5838125b1e6SAlfred Perlstein } else if (object->ref_count == 1) { 5844c8e0452SAlan Cox if (object->shadow_count == 0 && 58563967687SJeff Roberson (object->flags & OBJ_ANON) != 0) { 5868125b1e6SAlfred Perlstein vm_object_set_flag(object, OBJ_ONEMAPPING); 58732362449SKonstantin Belousov } else if (object->shadow_count == 1) { 58832362449SKonstantin Belousov KASSERT((object->flags & OBJ_ANON) != 0, 58932362449SKonstantin Belousov ("obj %p with shadow_count > 0 is not anon", 59032362449SKonstantin Belousov object)); 5911c500307SAlan Cox robject = LIST_FIRST(&object->shadow_head); 5925526d2d9SEivind Eklund KASSERT(robject != NULL, 59332362449SKonstantin Belousov ("vm_object_deallocate: ref_count: %d, " 59432362449SKonstantin Belousov "shadow_count: %d", object->ref_count, 5955526d2d9SEivind Eklund object->shadow_count)); 5964bace8e7SKonstantin Belousov KASSERT((robject->flags & OBJ_TMPFS_NODE) == 0, 5974bace8e7SKonstantin Belousov ("shadowed tmpfs v_object %p", object)); 59889f6b863SAttilio Rao if (!VM_OBJECT_TRYWLOCK(robject)) { 599b72b0115SAlan Cox /* 600b72b0115SAlan Cox * Avoid a potential deadlock. 601b72b0115SAlan Cox */ 60251df5321SJeff Roberson refcount_acquire(&object->ref_count); 60389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 604a7d86121SAlan Cox /* 605a7d86121SAlan Cox * More likely than not the thread 606a7d86121SAlan Cox * holding robject's lock has lower 607a7d86121SAlan Cox * priority than the current thread. 608a7d86121SAlan Cox * Let the lower priority thread run. 609a7d86121SAlan Cox */ 6108db5fc58SJohn Baldwin pause("vmo_de", 1); 611b72b0115SAlan Cox continue; 612b72b0115SAlan Cox } 613d936694fSAlan Cox /* 614d936694fSAlan Cox * Collapse object into its shadow unless its 615d936694fSAlan Cox * shadow is dead. In that case, object will 616d936694fSAlan Cox * be deallocated by the thread that is 617d936694fSAlan Cox * deallocating its shadow. 618d936694fSAlan Cox */ 61963967687SJeff Roberson if ((robject->flags & 62032362449SKonstantin Belousov (OBJ_DEAD | OBJ_ANON)) == OBJ_ANON) { 621a1f6d91cSDavid Greenman 62251df5321SJeff Roberson refcount_acquire(&robject->ref_count); 623138449dcSAlan Cox retry: 62411b57401SHans Petter Selasky if (REFCOUNT_COUNT(robject->paging_in_progress) > 0) { 62589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 626138449dcSAlan Cox vm_object_pip_wait(robject, 627138449dcSAlan Cox "objde1"); 6282e9f4a69SAlan Cox temp = robject->backing_object; 6292e9f4a69SAlan Cox if (object == temp) { 63089f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 631138449dcSAlan Cox goto retry; 6322e9f4a69SAlan Cox } 63311b57401SHans Petter Selasky } else if (REFCOUNT_COUNT(object->paging_in_progress) > 0) { 63489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(robject); 635cf27e0d1SJeff Roberson VM_OBJECT_WUNLOCK(object); 636cf27e0d1SJeff Roberson refcount_wait( 637cf27e0d1SJeff Roberson &object->paging_in_progress, 638cf27e0d1SJeff Roberson "objde2", PVM); 63989f6b863SAttilio Rao VM_OBJECT_WLOCK(robject); 6402e9f4a69SAlan Cox temp = robject->backing_object; 6412e9f4a69SAlan Cox if (object == temp) { 64289f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 643138449dcSAlan Cox goto retry; 644a1f6d91cSDavid Greenman } 6452e9f4a69SAlan Cox } else 64689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 6472e9f4a69SAlan Cox 64895e5e988SJohn Dyson if (robject->ref_count == 1) { 649*a67d5408SJeff Roberson refcount_release(&robject->ref_count); 650ba8da839SDavid Greenman object = robject; 65195e5e988SJohn Dyson goto doterm; 65295e5e988SJohn Dyson } 65395e5e988SJohn Dyson object = robject; 65495e5e988SJohn Dyson vm_object_collapse(object); 65589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 656ba8da839SDavid Greenman continue; 657a1f6d91cSDavid Greenman } 65889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(robject); 65995e5e988SJohn Dyson } 66089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 66123b186d3SAlan Cox return; 66295e5e988SJohn Dyson } 66395e5e988SJohn Dyson doterm: 6641bdbd705SKonstantin Belousov umtx_shm_object_terminated(object); 66524a1cce3SDavid Greenman temp = object->backing_object; 666c9917419SAlan Cox if (temp != NULL) { 6674bace8e7SKonstantin Belousov KASSERT((object->flags & OBJ_TMPFS_NODE) == 0, 6684bace8e7SKonstantin Belousov ("shadowed tmpfs v_object 2 %p", object)); 66951b867e5SJeff Roberson vm_object_backing_remove(object); 670de5f6a77SJohn Dyson } 671245df27cSMatthew Dillon /* 672245df27cSMatthew Dillon * Don't double-terminate, we could be in a termination 673245df27cSMatthew Dillon * recursion due to the terminate having to sync data 674245df27cSMatthew Dillon * to disk. 675245df27cSMatthew Dillon */ 676783a68aaSKonstantin Belousov if ((object->flags & OBJ_DEAD) == 0) { 677783a68aaSKonstantin Belousov vm_object_set_flag(object, OBJ_DEAD); 678df8bae1dSRodney W. Grimes vm_object_terminate(object); 679783a68aaSKonstantin Belousov } else 68089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 681df8bae1dSRodney W. Grimes object = temp; 682df8bae1dSRodney W. Grimes } 683df8bae1dSRodney W. Grimes } 684df8bae1dSRodney W. Grimes 685df8bae1dSRodney W. Grimes /* 6862ac78f0eSStephan Uphoff * vm_object_destroy removes the object from the global object list 6872ac78f0eSStephan Uphoff * and frees the space for the object. 6882ac78f0eSStephan Uphoff */ 6892ac78f0eSStephan Uphoff void 6902ac78f0eSStephan Uphoff vm_object_destroy(vm_object_t object) 6912ac78f0eSStephan Uphoff { 6922ac78f0eSStephan Uphoff 6932ac78f0eSStephan Uphoff /* 6943364c323SKonstantin Belousov * Release the allocation charge. 6953364c323SKonstantin Belousov */ 696ef694c1aSEdward Tomasz Napierala if (object->cred != NULL) { 697ef694c1aSEdward Tomasz Napierala swap_release_by_cred(object->charge, object->cred); 6983364c323SKonstantin Belousov object->charge = 0; 699ef694c1aSEdward Tomasz Napierala crfree(object->cred); 700ef694c1aSEdward Tomasz Napierala object->cred = NULL; 7013364c323SKonstantin Belousov } 7023364c323SKonstantin Belousov 7033364c323SKonstantin Belousov /* 7042ac78f0eSStephan Uphoff * Free the space for the object. 7052ac78f0eSStephan Uphoff */ 7062ac78f0eSStephan Uphoff uma_zfree(obj_zone, object); 7072ac78f0eSStephan Uphoff } 7082ac78f0eSStephan Uphoff 70951b867e5SJeff Roberson static void 71051b867e5SJeff Roberson vm_object_backing_remove_locked(vm_object_t object) 71151b867e5SJeff Roberson { 71251b867e5SJeff Roberson vm_object_t backing_object; 71351b867e5SJeff Roberson 71451b867e5SJeff Roberson backing_object = object->backing_object; 71551b867e5SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 71651b867e5SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(backing_object); 71751b867e5SJeff Roberson 71851b867e5SJeff Roberson if ((object->flags & OBJ_SHADOWLIST) != 0) { 71951b867e5SJeff Roberson LIST_REMOVE(object, shadow_list); 72051b867e5SJeff Roberson backing_object->shadow_count--; 72151b867e5SJeff Roberson object->flags &= ~OBJ_SHADOWLIST; 72251b867e5SJeff Roberson } 72351b867e5SJeff Roberson object->backing_object = NULL; 72451b867e5SJeff Roberson } 72551b867e5SJeff Roberson 72651b867e5SJeff Roberson static void 72751b867e5SJeff Roberson vm_object_backing_remove(vm_object_t object) 72851b867e5SJeff Roberson { 72951b867e5SJeff Roberson vm_object_t backing_object; 73051b867e5SJeff Roberson 73151b867e5SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 73251b867e5SJeff Roberson 73351b867e5SJeff Roberson if ((object->flags & OBJ_SHADOWLIST) != 0) { 73451b867e5SJeff Roberson backing_object = object->backing_object; 73551b867e5SJeff Roberson VM_OBJECT_WLOCK(backing_object); 73651b867e5SJeff Roberson vm_object_backing_remove_locked(object); 73751b867e5SJeff Roberson VM_OBJECT_WUNLOCK(backing_object); 73851b867e5SJeff Roberson } else 73951b867e5SJeff Roberson object->backing_object = NULL; 74051b867e5SJeff Roberson } 74151b867e5SJeff Roberson 74251b867e5SJeff Roberson static void 74351b867e5SJeff Roberson vm_object_backing_insert_locked(vm_object_t object, vm_object_t backing_object) 74451b867e5SJeff Roberson { 74551b867e5SJeff Roberson 74651b867e5SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 74751b867e5SJeff Roberson 74851b867e5SJeff Roberson if ((backing_object->flags & OBJ_ANON) != 0) { 74951b867e5SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(backing_object); 75051b867e5SJeff Roberson LIST_INSERT_HEAD(&backing_object->shadow_head, object, 75151b867e5SJeff Roberson shadow_list); 75251b867e5SJeff Roberson backing_object->shadow_count++; 75351b867e5SJeff Roberson object->flags |= OBJ_SHADOWLIST; 75451b867e5SJeff Roberson } 75551b867e5SJeff Roberson object->backing_object = backing_object; 75651b867e5SJeff Roberson } 75751b867e5SJeff Roberson 75851b867e5SJeff Roberson static void 75951b867e5SJeff Roberson vm_object_backing_insert(vm_object_t object, vm_object_t backing_object) 76051b867e5SJeff Roberson { 76151b867e5SJeff Roberson 76251b867e5SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 76351b867e5SJeff Roberson 76451b867e5SJeff Roberson if ((backing_object->flags & OBJ_ANON) != 0) { 76551b867e5SJeff Roberson VM_OBJECT_WLOCK(backing_object); 76651b867e5SJeff Roberson vm_object_backing_insert_locked(object, backing_object); 76751b867e5SJeff Roberson VM_OBJECT_WUNLOCK(backing_object); 76851b867e5SJeff Roberson } else 76951b867e5SJeff Roberson object->backing_object = backing_object; 77051b867e5SJeff Roberson } 77151b867e5SJeff Roberson 77251b867e5SJeff Roberson 7732ac78f0eSStephan Uphoff /* 7747bbdb843SRuslan Bukin * vm_object_terminate_pages removes any remaining pageable pages 7757bbdb843SRuslan Bukin * from the object and resets the object to an empty state. 7767bbdb843SRuslan Bukin */ 7777bbdb843SRuslan Bukin static void 7787bbdb843SRuslan Bukin vm_object_terminate_pages(vm_object_t object) 7797bbdb843SRuslan Bukin { 7807bbdb843SRuslan Bukin vm_page_t p, p_next; 7817bbdb843SRuslan Bukin 7827bbdb843SRuslan Bukin VM_OBJECT_ASSERT_WLOCKED(object); 7837bbdb843SRuslan Bukin 7847bbdb843SRuslan Bukin /* 7857bbdb843SRuslan Bukin * Free any remaining pageable pages. This also removes them from the 7867bbdb843SRuslan Bukin * paging queues. However, don't free wired pages, just remove them 7877bbdb843SRuslan Bukin * from the object. Rather than incrementally removing each page from 7887bbdb843SRuslan Bukin * the object, the page and object are reset to any empty state. 7897bbdb843SRuslan Bukin */ 7907bbdb843SRuslan Bukin TAILQ_FOREACH_SAFE(p, &object->memq, listq, p_next) { 7917bbdb843SRuslan Bukin vm_page_assert_unbusied(p); 792fee2a2faSMark Johnston KASSERT(p->object == object && 793fee2a2faSMark Johnston (p->ref_count & VPRC_OBJREF) != 0, 794fee2a2faSMark Johnston ("vm_object_terminate_pages: page %p is inconsistent", p)); 795fee2a2faSMark Johnston 7967bbdb843SRuslan Bukin p->object = NULL; 797fee2a2faSMark Johnston if (vm_page_drop(p, VPRC_OBJREF) == VPRC_OBJREF) { 7985cd29d0fSMark Johnston VM_CNT_INC(v_pfree); 7995cd29d0fSMark Johnston vm_page_free(p); 8004074d642SAlan Cox } 801fee2a2faSMark Johnston } 8022fcd1ff6SKonstantin Belousov 8037bbdb843SRuslan Bukin /* 8047bbdb843SRuslan Bukin * If the object contained any pages, then reset it to an empty state. 8057bbdb843SRuslan Bukin * None of the object's fields, including "resident_page_count", were 8067bbdb843SRuslan Bukin * modified by the preceding loop. 8077bbdb843SRuslan Bukin */ 8087bbdb843SRuslan Bukin if (object->resident_page_count != 0) { 8097bbdb843SRuslan Bukin vm_radix_reclaim_allnodes(&object->rtree); 8107bbdb843SRuslan Bukin TAILQ_INIT(&object->memq); 8117bbdb843SRuslan Bukin object->resident_page_count = 0; 8127bbdb843SRuslan Bukin if (object->type == OBJT_VNODE) 8137bbdb843SRuslan Bukin vdrop(object->handle); 8147bbdb843SRuslan Bukin } 8157bbdb843SRuslan Bukin } 8167bbdb843SRuslan Bukin 8177bbdb843SRuslan Bukin /* 818df8bae1dSRodney W. Grimes * vm_object_terminate actually destroys the specified object, freeing 819df8bae1dSRodney W. Grimes * up all previously used resources. 820df8bae1dSRodney W. Grimes * 821df8bae1dSRodney W. Grimes * The object must be locked. 8221c7c3c6aSMatthew Dillon * This routine may block. 823df8bae1dSRodney W. Grimes */ 82495e5e988SJohn Dyson void 8251b40f8c0SMatthew Dillon vm_object_terminate(vm_object_t object) 826df8bae1dSRodney W. Grimes { 82789f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 828783a68aaSKonstantin Belousov KASSERT((object->flags & OBJ_DEAD) != 0, 829783a68aaSKonstantin Belousov ("terminating non-dead obj %p", object)); 830bef608bdSJohn Dyson 831cf27e0d1SJeff Roberson /* 832cf27e0d1SJeff Roberson * wait for the pageout daemon to be done with the object 833cf27e0d1SJeff Roberson */ 834cf27e0d1SJeff Roberson vm_object_pip_wait(object, "objtrm"); 835cf27e0d1SJeff Roberson 83611b57401SHans Petter Selasky KASSERT(!REFCOUNT_COUNT(object->paging_in_progress), 837cf27e0d1SJeff Roberson ("vm_object_terminate: pageout in progress")); 838cf27e0d1SJeff Roberson 839971dd342SAlfred Perlstein KASSERT(object->ref_count == 0, 840971dd342SAlfred Perlstein ("vm_object_terminate: object with references, ref_count=%d", 841971dd342SAlfred Perlstein object->ref_count)); 842996c772fSJohn Dyson 8437bbdb843SRuslan Bukin if ((object->flags & OBJ_PG_DTOR) == 0) 8447bbdb843SRuslan Bukin vm_object_terminate_pages(object); 845bef608bdSJohn Dyson 846f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 847f8a47341SAlan Cox if (__predict_false(!LIST_EMPTY(&object->rvq))) 848f8a47341SAlan Cox vm_reserv_break_all(object); 849f8a47341SAlan Cox #endif 8507bfda801SAlan Cox 851e735691bSJohn Baldwin KASSERT(object->cred == NULL || object->type == OBJT_DEFAULT || 852e735691bSJohn Baldwin object->type == OBJT_SWAP, 853e735691bSJohn Baldwin ("%s: non-swap obj %p has cred", __func__, object)); 854e735691bSJohn Baldwin 8552d8acc0fSJohn Dyson /* 8569fcfb650SDavid Greenman * Let the pager know object is dead. 8579fcfb650SDavid Greenman */ 8589fcfb650SDavid Greenman vm_pager_deallocate(object); 85989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 8609fcfb650SDavid Greenman 8612ac78f0eSStephan Uphoff vm_object_destroy(object); 86247221757SJohn Dyson } 863df8bae1dSRodney W. Grimes 864edf93b25SAlan Cox /* 865edf93b25SAlan Cox * Make the page read-only so that we can clear the object flags. However, if 866edf93b25SAlan Cox * this is a nosync mmap then the object is likely to stay dirty so do not 867edf93b25SAlan Cox * mess with the page and do not clear the object flags. Returns TRUE if the 868edf93b25SAlan Cox * page should be flushed, and FALSE otherwise. 869edf93b25SAlan Cox */ 8703280870dSKonstantin Belousov static boolean_t 87167d0e293SJeff Roberson vm_object_page_remove_write(vm_page_t p, int flags, boolean_t *allclean) 8723280870dSKonstantin Belousov { 8733280870dSKonstantin Belousov 874fff5403fSJeff Roberson vm_page_assert_busied(p); 875fff5403fSJeff Roberson 8763280870dSKonstantin Belousov /* 8773280870dSKonstantin Belousov * If we have been asked to skip nosync pages and this is a 8783280870dSKonstantin Belousov * nosync page, skip it. Note that the object flags were not 8793280870dSKonstantin Belousov * cleared in this case so we do not have to set them. 8803280870dSKonstantin Belousov */ 881fff5403fSJeff Roberson if ((flags & OBJPC_NOSYNC) != 0 && (p->aflags & PGA_NOSYNC) != 0) { 88267d0e293SJeff Roberson *allclean = FALSE; 8833280870dSKonstantin Belousov return (FALSE); 8843280870dSKonstantin Belousov } else { 8853280870dSKonstantin Belousov pmap_remove_write(p); 8863280870dSKonstantin Belousov return (p->dirty != 0); 8873280870dSKonstantin Belousov } 8883280870dSKonstantin Belousov } 8893280870dSKonstantin Belousov 890df8bae1dSRodney W. Grimes /* 891df8bae1dSRodney W. Grimes * vm_object_page_clean 892df8bae1dSRodney W. Grimes * 8934f79d873SMatthew Dillon * Clean all dirty pages in the specified range of object. Leaves page 8944f79d873SMatthew Dillon * on whatever queue it is currently on. If NOSYNC is set then do not 895fff5403fSJeff Roberson * write out pages with PGA_NOSYNC set (originally comes from MAP_NOSYNC), 8964f79d873SMatthew Dillon * leaving the object dirty. 89726f9a767SRodney W. Grimes * 89843b7990eSMatthew Dillon * When stuffing pages asynchronously, allow clustering. XXX we need a 89943b7990eSMatthew Dillon * synchronous clustering mode implementation. 90043b7990eSMatthew Dillon * 90126f9a767SRodney W. Grimes * Odd semantics: if start == end, we clean everything. 90226f9a767SRodney W. Grimes * 90326f9a767SRodney W. Grimes * The object must be locked. 904126d6082SKonstantin Belousov * 905126d6082SKonstantin Belousov * Returns FALSE if some page from the range was not written, as 906126d6082SKonstantin Belousov * reported by the pager, and TRUE otherwise. 90726f9a767SRodney W. Grimes */ 908126d6082SKonstantin Belousov boolean_t 90917f3095dSAlan Cox vm_object_page_clean(vm_object_t object, vm_ooffset_t start, vm_ooffset_t end, 910e239bb97SKonstantin Belousov int flags) 911f6b04d2bSDavid Greenman { 912e239bb97SKonstantin Belousov vm_page_t np, p; 91317f3095dSAlan Cox vm_pindex_t pi, tend, tstart; 914126d6082SKonstantin Belousov int curgeneration, n, pagerflags; 91567d0e293SJeff Roberson boolean_t eio, res, allclean; 916f6b04d2bSDavid Greenman 91789f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 918e5f299ffSKonstantin Belousov 91967d0e293SJeff Roberson if (object->type != OBJT_VNODE || !vm_object_mightbedirty(object) || 920e239bb97SKonstantin Belousov object->resident_page_count == 0) 921126d6082SKonstantin Belousov return (TRUE); 922f6b04d2bSDavid Greenman 923e239bb97SKonstantin Belousov pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) != 0 ? 924e239bb97SKonstantin Belousov VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK; 925e239bb97SKonstantin Belousov pagerflags |= (flags & OBJPC_INVAL) != 0 ? VM_PAGER_PUT_INVAL : 0; 926e239bb97SKonstantin Belousov 92717f3095dSAlan Cox tstart = OFF_TO_IDX(start); 92817f3095dSAlan Cox tend = (end == 0) ? object->size : OFF_TO_IDX(end + PAGE_MASK); 92967d0e293SJeff Roberson allclean = tstart == 0 && tend >= object->size; 930126d6082SKonstantin Belousov res = TRUE; 931f6b04d2bSDavid Greenman 932bd7e5f99SJohn Dyson rescan: 9332d8acc0fSJohn Dyson curgeneration = object->generation; 9342d8acc0fSJohn Dyson 93517f3095dSAlan Cox for (p = vm_page_find_least(object, tstart); p != NULL; p = np) { 936bd7e5f99SJohn Dyson pi = p->pindex; 937e239bb97SKonstantin Belousov if (pi >= tend) 938e239bb97SKonstantin Belousov break; 939e239bb97SKonstantin Belousov np = TAILQ_NEXT(p, listq); 9400012f373SJeff Roberson if (vm_page_none_valid(p)) 941aef922f5SJohn Dyson continue; 94263e97555SJeff Roberson if (vm_page_busy_acquire(p, VM_ALLOC_WAITFAIL) == 0) { 94367d0e293SJeff Roberson if (object->generation != curgeneration && 94467d0e293SJeff Roberson (flags & OBJPC_SYNC) != 0) 945e239bb97SKonstantin Belousov goto rescan; 946780636b7SKonstantin Belousov np = vm_page_find_least(object, pi); 947780636b7SKonstantin Belousov continue; 948f6b04d2bSDavid Greenman } 94967d0e293SJeff Roberson if (!vm_object_page_remove_write(p, flags, &allclean)) { 95063e97555SJeff Roberson vm_page_xunbusy(p); 951bd7e5f99SJohn Dyson continue; 95263e97555SJeff Roberson } 953e239bb97SKonstantin Belousov 9543280870dSKonstantin Belousov n = vm_object_page_collect_flush(object, p, pagerflags, 95567d0e293SJeff Roberson flags, &allclean, &eio); 956126d6082SKonstantin Belousov if (eio) { 957126d6082SKonstantin Belousov res = FALSE; 95867d0e293SJeff Roberson allclean = FALSE; 959126d6082SKonstantin Belousov } 96067d0e293SJeff Roberson if (object->generation != curgeneration && 96167d0e293SJeff Roberson (flags & OBJPC_SYNC) != 0) 962b9b7a4beSMatthew Dillon goto rescan; 963031ec8c1SKonstantin Belousov 964031ec8c1SKonstantin Belousov /* 965031ec8c1SKonstantin Belousov * If the VOP_PUTPAGES() did a truncated write, so 966031ec8c1SKonstantin Belousov * that even the first page of the run is not fully 967031ec8c1SKonstantin Belousov * written, vm_pageout_flush() returns 0 as the run 968031ec8c1SKonstantin Belousov * length. Since the condition that caused truncated 969031ec8c1SKonstantin Belousov * write may be permanent, e.g. exhausted free space, 970031ec8c1SKonstantin Belousov * accepting n == 0 would cause an infinite loop. 971031ec8c1SKonstantin Belousov * 972031ec8c1SKonstantin Belousov * Forwarding the iterator leaves the unwritten page 973031ec8c1SKonstantin Belousov * behind, but there is not much we can do there if 974031ec8c1SKonstantin Belousov * filesystem refuses to write it. 975031ec8c1SKonstantin Belousov */ 976126d6082SKonstantin Belousov if (n == 0) { 977031ec8c1SKonstantin Belousov n = 1; 97867d0e293SJeff Roberson allclean = FALSE; 979126d6082SKonstantin Belousov } 980e239bb97SKonstantin Belousov np = vm_page_find_least(object, pi + n); 981b9b7a4beSMatthew Dillon } 982b9b7a4beSMatthew Dillon #if 0 983e239bb97SKonstantin Belousov VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC) ? MNT_WAIT : 0); 984b9b7a4beSMatthew Dillon #endif 985b9b7a4beSMatthew Dillon 98667d0e293SJeff Roberson if (allclean) 98767d0e293SJeff Roberson object->cleangeneration = curgeneration; 988126d6082SKonstantin Belousov return (res); 989b9b7a4beSMatthew Dillon } 990b9b7a4beSMatthew Dillon 991b9b7a4beSMatthew Dillon static int 9923280870dSKonstantin Belousov vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int pagerflags, 99367d0e293SJeff Roberson int flags, boolean_t *allclean, boolean_t *eio) 994b9b7a4beSMatthew Dillon { 9953157c503SKonstantin Belousov vm_page_t ma[vm_pageout_page_count], p_first, tp; 9963157c503SKonstantin Belousov int count, i, mreq, runlen; 997b9b7a4beSMatthew Dillon 9987bec141bSKip Macy vm_page_lock_assert(p, MA_NOTOWNED); 99963e97555SJeff Roberson vm_page_assert_xbusied(p); 100089f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 10013157c503SKonstantin Belousov 10023157c503SKonstantin Belousov count = 1; 10033157c503SKonstantin Belousov mreq = 0; 10043157c503SKonstantin Belousov 10053157c503SKonstantin Belousov for (tp = p; count < vm_pageout_page_count; count++) { 10063157c503SKonstantin Belousov tp = vm_page_next(tp); 100763e97555SJeff Roberson if (tp == NULL || vm_page_tryxbusy(tp) == 0) 1008bd7e5f99SJohn Dyson break; 100967d0e293SJeff Roberson if (!vm_object_page_remove_write(tp, flags, allclean)) { 101063e97555SJeff Roberson vm_page_xunbusy(tp); 1011bd7e5f99SJohn Dyson break; 1012bd7e5f99SJohn Dyson } 101363e97555SJeff Roberson } 1014aef922f5SJohn Dyson 10153157c503SKonstantin Belousov for (p_first = p; count < vm_pageout_page_count; count++) { 10163157c503SKonstantin Belousov tp = vm_page_prev(p_first); 101763e97555SJeff Roberson if (tp == NULL || vm_page_tryxbusy(tp) == 0) 1018bd7e5f99SJohn Dyson break; 101967d0e293SJeff Roberson if (!vm_object_page_remove_write(tp, flags, allclean)) { 102063e97555SJeff Roberson vm_page_xunbusy(tp); 1021bd7e5f99SJohn Dyson break; 102263e97555SJeff Roberson } 10233157c503SKonstantin Belousov p_first = tp; 10243157c503SKonstantin Belousov mreq++; 1025bd7e5f99SJohn Dyson } 1026bd7e5f99SJohn Dyson 10273157c503SKonstantin Belousov for (tp = p_first, i = 0; i < count; tp = TAILQ_NEXT(tp, listq), i++) 10283157c503SKonstantin Belousov ma[i] = tp; 1029cf2819ccSJohn Dyson 1030126d6082SKonstantin Belousov vm_pageout_flush(ma, count, pagerflags, mreq, &runlen, eio); 10311e8a675cSKonstantin Belousov return (runlen); 103226f9a767SRodney W. Grimes } 1033df8bae1dSRodney W. Grimes 10341efb74fbSJohn Dyson /* 1035950f8459SAlan Cox * Note that there is absolutely no sense in writing out 1036950f8459SAlan Cox * anonymous objects, so we track down the vnode object 1037950f8459SAlan Cox * to write out. 1038950f8459SAlan Cox * We invalidate (remove) all pages from the address space 1039950f8459SAlan Cox * for semantic correctness. 1040950f8459SAlan Cox * 10416bbee8e2SAlan Cox * If the backing object is a device object with unmanaged pages, then any 10426bbee8e2SAlan Cox * mappings to the specified range of pages must be removed before this 10436bbee8e2SAlan Cox * function is called. 10446bbee8e2SAlan Cox * 1045950f8459SAlan Cox * Note: certain anonymous maps, such as MAP_NOSYNC maps, 1046950f8459SAlan Cox * may start out with a NULL object. 1047950f8459SAlan Cox */ 1048126d6082SKonstantin Belousov boolean_t 1049950f8459SAlan Cox vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size, 1050950f8459SAlan Cox boolean_t syncio, boolean_t invalidate) 1051950f8459SAlan Cox { 1052950f8459SAlan Cox vm_object_t backing_object; 1053950f8459SAlan Cox struct vnode *vp; 10543b582b4eSTor Egge struct mount *mp; 1055126d6082SKonstantin Belousov int error, flags, fsync_after; 1056126d6082SKonstantin Belousov boolean_t res; 1057950f8459SAlan Cox 1058950f8459SAlan Cox if (object == NULL) 1059126d6082SKonstantin Belousov return (TRUE); 1060126d6082SKonstantin Belousov res = TRUE; 1061126d6082SKonstantin Belousov error = 0; 106289f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1063950f8459SAlan Cox while ((backing_object = object->backing_object) != NULL) { 106489f6b863SAttilio Rao VM_OBJECT_WLOCK(backing_object); 106556e0670fSAlan Cox offset += object->backing_object_offset; 106689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1067950f8459SAlan Cox object = backing_object; 1068950f8459SAlan Cox if (object->size < OFF_TO_IDX(offset + size)) 1069950f8459SAlan Cox size = IDX_TO_OFF(object->size) - offset; 1070950f8459SAlan Cox } 1071950f8459SAlan Cox /* 1072950f8459SAlan Cox * Flush pages if writing is allowed, invalidate them 1073950f8459SAlan Cox * if invalidation requested. Pages undergoing I/O 1074950f8459SAlan Cox * will be ignored by vm_object_page_remove(). 1075950f8459SAlan Cox * 1076950f8459SAlan Cox * We cannot lock the vnode and then wait for paging 1077950f8459SAlan Cox * to complete without deadlocking against vm_fault. 1078950f8459SAlan Cox * Instead we simply call vm_object_page_remove() and 1079950f8459SAlan Cox * allow it to block internally on a page-by-page 1080950f8459SAlan Cox * basis when it encounters pages undergoing async 1081950f8459SAlan Cox * I/O. 1082950f8459SAlan Cox */ 1083950f8459SAlan Cox if (object->type == OBJT_VNODE && 108467d0e293SJeff Roberson vm_object_mightbedirty(object) != 0 && 10855bf94937SKonstantin Belousov ((vp = object->handle)->v_vflag & VV_NOSYNC) == 0) { 108689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 10873b582b4eSTor Egge (void) vn_start_write(vp, &mp, V_WAIT); 1088cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 108975ff604aSKonstantin Belousov if (syncio && !invalidate && offset == 0 && 1090d1780e8dSKonstantin Belousov atop(size) == object->size) { 109175ff604aSKonstantin Belousov /* 109275ff604aSKonstantin Belousov * If syncing the whole mapping of the file, 109375ff604aSKonstantin Belousov * it is faster to schedule all the writes in 109475ff604aSKonstantin Belousov * async mode, also allowing the clustering, 109575ff604aSKonstantin Belousov * and then wait for i/o to complete. 109675ff604aSKonstantin Belousov */ 109775ff604aSKonstantin Belousov flags = 0; 109875ff604aSKonstantin Belousov fsync_after = TRUE; 109975ff604aSKonstantin Belousov } else { 1100950f8459SAlan Cox flags = (syncio || invalidate) ? OBJPC_SYNC : 0; 110175ff604aSKonstantin Belousov flags |= invalidate ? (OBJPC_SYNC | OBJPC_INVAL) : 0; 110275ff604aSKonstantin Belousov fsync_after = FALSE; 110375ff604aSKonstantin Belousov } 110489f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1105126d6082SKonstantin Belousov res = vm_object_page_clean(object, offset, offset + size, 1106126d6082SKonstantin Belousov flags); 110789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 110875ff604aSKonstantin Belousov if (fsync_after) 1109126d6082SKonstantin Belousov error = VOP_FSYNC(vp, MNT_WAIT, curthread); 111022db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 11113b582b4eSTor Egge vn_finished_write(mp); 1112126d6082SKonstantin Belousov if (error != 0) 1113126d6082SKonstantin Belousov res = FALSE; 111489f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1115950f8459SAlan Cox } 1116950f8459SAlan Cox if ((object->type == OBJT_VNODE || 1117950f8459SAlan Cox object->type == OBJT_DEVICE) && invalidate) { 11186bbee8e2SAlan Cox if (object->type == OBJT_DEVICE) 11196bbee8e2SAlan Cox /* 11206bbee8e2SAlan Cox * The option OBJPR_NOTMAPPED must be passed here 11216bbee8e2SAlan Cox * because vm_object_page_remove() cannot remove 11226bbee8e2SAlan Cox * unmanaged mappings. 11236bbee8e2SAlan Cox */ 11246bbee8e2SAlan Cox flags = OBJPR_NOTMAPPED; 11256bbee8e2SAlan Cox else if (old_msync) 11266195b24aSKonstantin Belousov flags = 0; 11276bbee8e2SAlan Cox else 11286195b24aSKonstantin Belousov flags = OBJPR_CLEANONLY; 11296bbee8e2SAlan Cox vm_object_page_remove(object, OFF_TO_IDX(offset), 11306bbee8e2SAlan Cox OFF_TO_IDX(offset + size + PAGE_MASK), flags); 1131950f8459SAlan Cox } 113289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1133126d6082SKonstantin Belousov return (res); 1134950f8459SAlan Cox } 1135950f8459SAlan Cox 1136950f8459SAlan Cox /* 1137aa3650eaSMark Johnston * Determine whether the given advice can be applied to the object. Advice is 1138aa3650eaSMark Johnston * not applied to unmanaged pages since they never belong to page queues, and 1139aa3650eaSMark Johnston * since MADV_FREE is destructive, it can apply only to anonymous pages that 1140aa3650eaSMark Johnston * have been mapped at most once. 1141aa3650eaSMark Johnston */ 1142aa3650eaSMark Johnston static bool 1143aa3650eaSMark Johnston vm_object_advice_applies(vm_object_t object, int advice) 1144aa3650eaSMark Johnston { 1145aa3650eaSMark Johnston 1146aa3650eaSMark Johnston if ((object->flags & OBJ_UNMANAGED) != 0) 1147aa3650eaSMark Johnston return (false); 1148aa3650eaSMark Johnston if (advice != MADV_FREE) 1149aa3650eaSMark Johnston return (true); 115063967687SJeff Roberson return ((object->flags & (OBJ_ONEMAPPING | OBJ_ANON)) == 115163967687SJeff Roberson (OBJ_ONEMAPPING | OBJ_ANON)); 1152aa3650eaSMark Johnston } 1153aa3650eaSMark Johnston 1154aa3650eaSMark Johnston static void 1155aa3650eaSMark Johnston vm_object_madvise_freespace(vm_object_t object, int advice, vm_pindex_t pindex, 1156aa3650eaSMark Johnston vm_size_t size) 1157aa3650eaSMark Johnston { 1158aa3650eaSMark Johnston 1159aa3650eaSMark Johnston if (advice == MADV_FREE && object->type == OBJT_SWAP) 1160aa3650eaSMark Johnston swap_pager_freespace(object, pindex, size); 1161aa3650eaSMark Johnston } 1162aa3650eaSMark Johnston 1163aa3650eaSMark Johnston /* 1164867a482dSJohn Dyson * vm_object_madvise: 1165867a482dSJohn Dyson * 1166867a482dSJohn Dyson * Implements the madvise function at the object/page level. 11671c7c3c6aSMatthew Dillon * 1168193b9358SAlan Cox * MADV_WILLNEED (any object) 1169193b9358SAlan Cox * 1170193b9358SAlan Cox * Activate the specified pages if they are resident. 1171193b9358SAlan Cox * 1172193b9358SAlan Cox * MADV_DONTNEED (any object) 1173193b9358SAlan Cox * 1174193b9358SAlan Cox * Deactivate the specified pages if they are resident. 1175193b9358SAlan Cox * 1176193b9358SAlan Cox * MADV_FREE (OBJT_DEFAULT/OBJT_SWAP objects, 1177193b9358SAlan Cox * OBJ_ONEMAPPING only) 1178193b9358SAlan Cox * 1179193b9358SAlan Cox * Deactivate and clean the specified pages if they are 1180193b9358SAlan Cox * resident. This permits the process to reuse the pages 1181193b9358SAlan Cox * without faulting or the kernel to reclaim the pages 1182193b9358SAlan Cox * without I/O. 1183867a482dSJohn Dyson */ 1184867a482dSJohn Dyson void 118592a59946SJohn Baldwin vm_object_madvise(vm_object_t object, vm_pindex_t pindex, vm_pindex_t end, 1186c2655a40SMark Johnston int advice) 1187867a482dSJohn Dyson { 118892a59946SJohn Baldwin vm_pindex_t tpindex; 118934567de7SAlan Cox vm_object_t backing_object, tobject; 1190aa3650eaSMark Johnston vm_page_t m, tm; 1191867a482dSJohn Dyson 1192867a482dSJohn Dyson if (object == NULL) 1193867a482dSJohn Dyson return; 1194c2655a40SMark Johnston 11956e20a165SJohn Dyson relookup: 1196aa3650eaSMark Johnston VM_OBJECT_WLOCK(object); 1197aa3650eaSMark Johnston if (!vm_object_advice_applies(object, advice)) { 1198aa3650eaSMark Johnston VM_OBJECT_WUNLOCK(object); 1199aa3650eaSMark Johnston return; 12006e20a165SJohn Dyson } 1201aa3650eaSMark Johnston for (m = vm_page_find_least(object, pindex); pindex < end; pindex++) { 1202aa3650eaSMark Johnston tobject = object; 1203c2655a40SMark Johnston 12041ce137beSMatthew Dillon /* 1205aa3650eaSMark Johnston * If the next page isn't resident in the top-level object, we 1206aa3650eaSMark Johnston * need to search the shadow chain. When applying MADV_FREE, we 1207aa3650eaSMark Johnston * take care to release any swap space used to store 1208aa3650eaSMark Johnston * non-resident pages. 1209aa3650eaSMark Johnston */ 1210aa3650eaSMark Johnston if (m == NULL || pindex < m->pindex) { 1211aa3650eaSMark Johnston /* 1212aa3650eaSMark Johnston * Optimize a common case: if the top-level object has 1213aa3650eaSMark Johnston * no backing object, we can skip over the non-resident 1214aa3650eaSMark Johnston * range in constant time. 12151ce137beSMatthew Dillon */ 1216c2655a40SMark Johnston if (object->backing_object == NULL) { 1217c2655a40SMark Johnston tpindex = (m != NULL && m->pindex < end) ? 1218c2655a40SMark Johnston m->pindex : end; 1219aa3650eaSMark Johnston vm_object_madvise_freespace(object, advice, 1220aa3650eaSMark Johnston pindex, tpindex - pindex); 1221c2655a40SMark Johnston if ((pindex = tpindex) == end) 1222c2655a40SMark Johnston break; 1223aa3650eaSMark Johnston goto next_page; 1224aa3650eaSMark Johnston } 1225aa3650eaSMark Johnston 1226aa3650eaSMark Johnston tpindex = pindex; 1227aa3650eaSMark Johnston do { 1228aa3650eaSMark Johnston vm_object_madvise_freespace(tobject, advice, 1229aa3650eaSMark Johnston tpindex, 1); 12301ce137beSMatthew Dillon /* 1231aa3650eaSMark Johnston * Prepare to search the next object in the 1232aa3650eaSMark Johnston * chain. 12331ce137beSMatthew Dillon */ 123434567de7SAlan Cox backing_object = tobject->backing_object; 123534567de7SAlan Cox if (backing_object == NULL) 1236aa3650eaSMark Johnston goto next_pindex; 123789f6b863SAttilio Rao VM_OBJECT_WLOCK(backing_object); 1238aa3650eaSMark Johnston tpindex += 1239aa3650eaSMark Johnston OFF_TO_IDX(tobject->backing_object_offset); 12409b98b796SAlan Cox if (tobject != object) 124189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(tobject); 124234567de7SAlan Cox tobject = backing_object; 1243aa3650eaSMark Johnston if (!vm_object_advice_applies(tobject, advice)) 1244aa3650eaSMark Johnston goto next_pindex; 1245aa3650eaSMark Johnston } while ((tm = vm_page_lookup(tobject, tpindex)) == 1246aa3650eaSMark Johnston NULL); 1247aa3650eaSMark Johnston } else { 1248aa3650eaSMark Johnston next_page: 1249aa3650eaSMark Johnston tm = m; 1250aa3650eaSMark Johnston m = TAILQ_NEXT(m, listq); 1251c2655a40SMark Johnston } 1252c2655a40SMark Johnston 1253867a482dSJohn Dyson /* 12540012f373SJeff Roberson * If the page is not in a normal state, skip it. The page 12550012f373SJeff Roberson * can not be invalidated while the object lock is held. 1256867a482dSJohn Dyson */ 12570012f373SJeff Roberson if (!vm_page_all_valid(tm) || vm_page_wired(tm)) 1258aa3650eaSMark Johnston goto next_pindex; 1259aa3650eaSMark Johnston KASSERT((tm->flags & PG_FICTITIOUS) == 0, 1260aa3650eaSMark Johnston ("vm_object_madvise: page %p is fictitious", tm)); 1261aa3650eaSMark Johnston KASSERT((tm->oflags & VPO_UNMANAGED) == 0, 1262aa3650eaSMark Johnston ("vm_object_madvise: page %p is not managed", tm)); 126363e97555SJeff Roberson if (vm_page_tryxbusy(tm) == 0) { 1264aa3650eaSMark Johnston if (object != tobject) 1265aa3650eaSMark Johnston VM_OBJECT_WUNLOCK(object); 1266c2655a40SMark Johnston if (advice == MADV_WILLNEED) { 1267b11b56b5SAlan Cox /* 1268b11b56b5SAlan Cox * Reference the page before unlocking and 1269b11b56b5SAlan Cox * sleeping so that the page daemon is less 1270b11b56b5SAlan Cox * likely to reclaim it. 1271b11b56b5SAlan Cox */ 1272aa3650eaSMark Johnston vm_page_aflag_set(tm, PGA_REFERENCED); 1273567e51e1SAlan Cox } 1274aa3650eaSMark Johnston vm_page_busy_sleep(tm, "madvpo", false); 12756e20a165SJohn Dyson goto relookup; 127634567de7SAlan Cox } 1277fee2a2faSMark Johnston vm_page_lock(tm); 1278aa3650eaSMark Johnston vm_page_advise(tm, advice); 1279aa3650eaSMark Johnston vm_page_unlock(tm); 128063e97555SJeff Roberson vm_page_xunbusy(tm); 1281aa3650eaSMark Johnston vm_object_madvise_freespace(tobject, advice, tm->pindex, 1); 1282aa3650eaSMark Johnston next_pindex: 12839b98b796SAlan Cox if (tobject != object) 128489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(tobject); 1285867a482dSJohn Dyson } 128689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1287867a482dSJohn Dyson } 1288867a482dSJohn Dyson 1289867a482dSJohn Dyson /* 1290df8bae1dSRodney W. Grimes * vm_object_shadow: 1291df8bae1dSRodney W. Grimes * 1292df8bae1dSRodney W. Grimes * Create a new object which is backed by the 1293df8bae1dSRodney W. Grimes * specified existing object range. The source 1294df8bae1dSRodney W. Grimes * object reference is deallocated. 1295df8bae1dSRodney W. Grimes * 1296df8bae1dSRodney W. Grimes * The new object and offset into that object 1297df8bae1dSRodney W. Grimes * are returned in the source parameters. 1298df8bae1dSRodney W. Grimes */ 129926f9a767SRodney W. Grimes void 13001b40f8c0SMatthew Dillon vm_object_shadow( 13011b40f8c0SMatthew Dillon vm_object_t *object, /* IN/OUT */ 13021b40f8c0SMatthew Dillon vm_ooffset_t *offset, /* IN/OUT */ 13031b40f8c0SMatthew Dillon vm_size_t length) 1304df8bae1dSRodney W. Grimes { 1305d031cff1SMatthew Dillon vm_object_t source; 1306d031cff1SMatthew Dillon vm_object_t result; 1307df8bae1dSRodney W. Grimes 1308df8bae1dSRodney W. Grimes source = *object; 1309df8bae1dSRodney W. Grimes 1310df8bae1dSRodney W. Grimes /* 13119a2f6362SAlan Cox * Don't create the new object if the old object isn't shared. 131263967687SJeff Roberson * 131363967687SJeff Roberson * If we hold the only reference we can guarantee that it won't 131463967687SJeff Roberson * increase while we have the map locked. Otherwise the race is 131563967687SJeff Roberson * harmless and we will end up with an extra shadow object that 131663967687SJeff Roberson * will be collapsed later. 13179a2f6362SAlan Cox */ 131863967687SJeff Roberson if (source != NULL && source->ref_count == 1 && 131932362449SKonstantin Belousov (source->flags & OBJ_ANON) != 0) 13209a2f6362SAlan Cox return; 13219a2f6362SAlan Cox 13229a2f6362SAlan Cox /* 1323570a2f4aSAlan Cox * Allocate a new object with the given length. 1324df8bae1dSRodney W. Grimes */ 132563967687SJeff Roberson result = vm_object_allocate_anon(atop(length)); 1326df8bae1dSRodney W. Grimes 1327df8bae1dSRodney W. Grimes /* 132851b867e5SJeff Roberson * Store the offset into the source object, and fix up the offset into 132951b867e5SJeff Roberson * the new object. 133051b867e5SJeff Roberson */ 133151b867e5SJeff Roberson result->backing_object_offset = *offset; 133251b867e5SJeff Roberson 133351b867e5SJeff Roberson /* 13340d94caffSDavid Greenman * The new object shadows the source object, adding a reference to it. 13350d94caffSDavid Greenman * Our caller changes his reference to point to the new object, 13360d94caffSDavid Greenman * removing a reference to the source object. Net result: no change 13370d94caffSDavid Greenman * of reference count. 13389b09fe24SMatthew Dillon * 13399b09fe24SMatthew Dillon * Try to optimize the result object's page color when shadowing 1340956f3135SPhilippe Charnier * in order to maintain page coloring consistency in the combined 13419b09fe24SMatthew Dillon * shadowed object. 1342df8bae1dSRodney W. Grimes */ 1343570a2f4aSAlan Cox if (source != NULL) { 134451b867e5SJeff Roberson VM_OBJECT_WLOCK(result); 134551b867e5SJeff Roberson vm_object_backing_insert(result, source); 13463f289c3fSJeff Roberson result->domain = source->domain; 1347f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 13487b54b1a9SAlan Cox result->flags |= source->flags & OBJ_COLORED; 1349f8a47341SAlan Cox result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) & 1350f8a47341SAlan Cox ((1 << (VM_NFREEORDER - 1)) - 1); 1351f8a47341SAlan Cox #endif 135251b867e5SJeff Roberson VM_OBJECT_WUNLOCK(result); 1353de5f6a77SJohn Dyson } 1354df8bae1dSRodney W. Grimes 1355df8bae1dSRodney W. Grimes /* 1356df8bae1dSRodney W. Grimes * Return the new things 1357df8bae1dSRodney W. Grimes */ 1358df8bae1dSRodney W. Grimes *offset = 0; 1359df8bae1dSRodney W. Grimes *object = result; 1360df8bae1dSRodney W. Grimes } 1361df8bae1dSRodney W. Grimes 1362c5aaa06dSAlan Cox /* 1363c5aaa06dSAlan Cox * vm_object_split: 1364c5aaa06dSAlan Cox * 1365c5aaa06dSAlan Cox * Split the pages in a map entry into a new object. This affords 1366c5aaa06dSAlan Cox * easier removal of unused pages, and keeps object inheritance from 1367c5aaa06dSAlan Cox * being a negative impact on memory usage. 1368c5aaa06dSAlan Cox */ 1369c5aaa06dSAlan Cox void 1370c5aaa06dSAlan Cox vm_object_split(vm_map_entry_t entry) 1371c5aaa06dSAlan Cox { 137273000556SAlan Cox vm_page_t m, m_next; 1373c5aaa06dSAlan Cox vm_object_t orig_object, new_object, source; 137473000556SAlan Cox vm_pindex_t idx, offidxstart; 137573000556SAlan Cox vm_size_t size; 1376c5aaa06dSAlan Cox 1377c5aaa06dSAlan Cox orig_object = entry->object.vm_object; 137863967687SJeff Roberson if ((orig_object->flags & OBJ_ANON) == 0) 1379c5aaa06dSAlan Cox return; 1380c5aaa06dSAlan Cox if (orig_object->ref_count <= 1) 1381c5aaa06dSAlan Cox return; 138289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(orig_object); 1383c5aaa06dSAlan Cox 13844da9f125SAlan Cox offidxstart = OFF_TO_IDX(entry->offset); 138595442adfSAlan Cox size = atop(entry->end - entry->start); 1386c5aaa06dSAlan Cox 13874da9f125SAlan Cox /* 13884da9f125SAlan Cox * If swap_pager_copy() is later called, it will convert new_object 13894da9f125SAlan Cox * into a swap object. 13904da9f125SAlan Cox */ 139163967687SJeff Roberson new_object = vm_object_allocate_anon(size); 1392c5aaa06dSAlan Cox 1393c5474b8fSAlan Cox /* 1394c5474b8fSAlan Cox * At this point, the new object is still private, so the order in 1395c5474b8fSAlan Cox * which the original and new objects are locked does not matter. 1396c5474b8fSAlan Cox */ 139789f6b863SAttilio Rao VM_OBJECT_WLOCK(new_object); 139889f6b863SAttilio Rao VM_OBJECT_WLOCK(orig_object); 13993f289c3fSJeff Roberson new_object->domain = orig_object->domain; 1400c5aaa06dSAlan Cox source = orig_object->backing_object; 1401c5aaa06dSAlan Cox if (source != NULL) { 140251b867e5SJeff Roberson if ((source->flags & (OBJ_ANON | OBJ_DEAD)) != 0) { 140389f6b863SAttilio Rao VM_OBJECT_WLOCK(source); 140419c244d0SAlan Cox if ((source->flags & OBJ_DEAD) != 0) { 140589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(source); 140689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(orig_object); 140789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(new_object); 140819c244d0SAlan Cox vm_object_deallocate(new_object); 140989f6b863SAttilio Rao VM_OBJECT_WLOCK(orig_object); 141019c244d0SAlan Cox return; 141119c244d0SAlan Cox } 141251b867e5SJeff Roberson vm_object_backing_insert_locked(new_object, source); 1413b921a12bSAlan Cox vm_object_reference_locked(source); /* for new_object */ 1414c5aaa06dSAlan Cox vm_object_clear_flag(source, OBJ_ONEMAPPING); 141589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(source); 141651b867e5SJeff Roberson } else { 141751b867e5SJeff Roberson vm_object_backing_insert(new_object, source); 141851b867e5SJeff Roberson vm_object_reference(source); 141951b867e5SJeff Roberson } 1420c5aaa06dSAlan Cox new_object->backing_object_offset = 14214da9f125SAlan Cox orig_object->backing_object_offset + entry->offset; 1422c5aaa06dSAlan Cox } 1423ef694c1aSEdward Tomasz Napierala if (orig_object->cred != NULL) { 1424ef694c1aSEdward Tomasz Napierala new_object->cred = orig_object->cred; 1425ef694c1aSEdward Tomasz Napierala crhold(orig_object->cred); 14263364c323SKonstantin Belousov new_object->charge = ptoa(size); 14273364c323SKonstantin Belousov KASSERT(orig_object->charge >= ptoa(size), 14283364c323SKonstantin Belousov ("orig_object->charge < 0")); 14293364c323SKonstantin Belousov orig_object->charge -= ptoa(size); 14303364c323SKonstantin Belousov } 1431c5aaa06dSAlan Cox retry: 1432b382c10aSKonstantin Belousov m = vm_page_find_least(orig_object, offidxstart); 143373000556SAlan Cox for (; m != NULL && (idx = m->pindex - offidxstart) < size; 143473000556SAlan Cox m = m_next) { 143573000556SAlan Cox m_next = TAILQ_NEXT(m, listq); 1436c5aaa06dSAlan Cox 1437c5aaa06dSAlan Cox /* 1438c5aaa06dSAlan Cox * We must wait for pending I/O to complete before we can 1439c5aaa06dSAlan Cox * rename the page. 1440c5aaa06dSAlan Cox * 1441c5aaa06dSAlan Cox * We do not have to VM_PROT_NONE the page as mappings should 1442c5aaa06dSAlan Cox * not be changed by this operation. 1443c5aaa06dSAlan Cox */ 144463e97555SJeff Roberson if (vm_page_tryxbusy(m) == 0) { 144589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(new_object); 14464cdea4a8SJeff Roberson vm_page_sleep_if_busy(m, "spltwt"); 144789f6b863SAttilio Rao VM_OBJECT_WLOCK(new_object); 1448c5aaa06dSAlan Cox goto retry; 1449de33beddSAlan Cox } 1450e946b949SAttilio Rao 14513453bca8SAlan Cox /* vm_page_rename() will dirty the page. */ 1452e946b949SAttilio Rao if (vm_page_rename(m, new_object, idx)) { 145363e97555SJeff Roberson vm_page_xunbusy(m); 1454e946b949SAttilio Rao VM_OBJECT_WUNLOCK(new_object); 1455e946b949SAttilio Rao VM_OBJECT_WUNLOCK(orig_object); 14568d6fbbb8SJeff Roberson vm_radix_wait(); 1457e946b949SAttilio Rao VM_OBJECT_WLOCK(orig_object); 1458e946b949SAttilio Rao VM_OBJECT_WLOCK(new_object); 1459e946b949SAttilio Rao goto retry; 1460e946b949SAttilio Rao } 146163e97555SJeff Roberson 1462b5f359b7SAlan Cox #if VM_NRESERVLEVEL > 0 1463b5f359b7SAlan Cox /* 1464b5f359b7SAlan Cox * If some of the reservation's allocated pages remain with 1465b5f359b7SAlan Cox * the original object, then transferring the reservation to 1466b5f359b7SAlan Cox * the new object is neither particularly beneficial nor 1467b5f359b7SAlan Cox * particularly harmful as compared to leaving the reservation 1468b5f359b7SAlan Cox * with the original object. If, however, all of the 1469b5f359b7SAlan Cox * reservation's allocated pages are transferred to the new 1470b5f359b7SAlan Cox * object, then transferring the reservation is typically 1471b5f359b7SAlan Cox * beneficial. Determining which of these two cases applies 1472b5f359b7SAlan Cox * would be more costly than unconditionally renaming the 1473b5f359b7SAlan Cox * reservation. 1474b5f359b7SAlan Cox */ 1475b5f359b7SAlan Cox vm_reserv_rename(m, new_object, orig_object, offidxstart); 1476b5f359b7SAlan Cox #endif 14778da1c098SJeff Roberson if (orig_object->type != OBJT_SWAP) 14788da1c098SJeff Roberson vm_page_xunbusy(m); 1479c5aaa06dSAlan Cox } 1480d7a013c3SAlan Cox if (orig_object->type == OBJT_SWAP) { 1481c5aaa06dSAlan Cox /* 1482c7c8dd7eSAlan Cox * swap_pager_copy() can sleep, in which case the orig_object's 1483c7c8dd7eSAlan Cox * and new_object's locks are released and reacquired. 1484c5aaa06dSAlan Cox */ 1485c5aaa06dSAlan Cox swap_pager_copy(orig_object, new_object, offidxstart, 0); 1486dfd55c0cSAttilio Rao TAILQ_FOREACH(m, &new_object->memq, listq) 1487c7aebda8SAttilio Rao vm_page_xunbusy(m); 1488c5aaa06dSAlan Cox } 148989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(orig_object); 149089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(new_object); 1491c5aaa06dSAlan Cox entry->object.vm_object = new_object; 1492c5aaa06dSAlan Cox entry->offset = 0LL; 1493c5aaa06dSAlan Cox vm_object_deallocate(orig_object); 149489f6b863SAttilio Rao VM_OBJECT_WLOCK(new_object); 1495c5aaa06dSAlan Cox } 1496c5aaa06dSAlan Cox 14972ad1a3f7SMatthew Dillon #define OBSC_COLLAPSE_NOWAIT 0x0002 14982ad1a3f7SMatthew Dillon #define OBSC_COLLAPSE_WAIT 0x0004 14992ad1a3f7SMatthew Dillon 150099a1570aSKonstantin Belousov static vm_page_t 15014cc8daf7SConrad Meyer vm_object_collapse_scan_wait(vm_object_t object, vm_page_t p, vm_page_t next, 150299a1570aSKonstantin Belousov int op) 150399a1570aSKonstantin Belousov { 150499a1570aSKonstantin Belousov vm_object_t backing_object; 150599a1570aSKonstantin Belousov 150699a1570aSKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(object); 150799a1570aSKonstantin Belousov backing_object = object->backing_object; 150899a1570aSKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(backing_object); 150999a1570aSKonstantin Belousov 151099a1570aSKonstantin Belousov KASSERT(p == NULL || p->object == object || p->object == backing_object, 151199a1570aSKonstantin Belousov ("invalid ownership %p %p %p", p, object, backing_object)); 151299a1570aSKonstantin Belousov if ((op & OBSC_COLLAPSE_NOWAIT) != 0) 151399a1570aSKonstantin Belousov return (next); 15148d6fbbb8SJeff Roberson /* The page is only NULL when rename fails. */ 15154cdea4a8SJeff Roberson if (p == NULL) { 15166a14746cSRyan Libby VM_OBJECT_WUNLOCK(object); 15176a14746cSRyan Libby VM_OBJECT_WUNLOCK(backing_object); 15188d6fbbb8SJeff Roberson vm_radix_wait(); 15194cdea4a8SJeff Roberson } else { 15204cdea4a8SJeff Roberson if (p->object == object) 15214cdea4a8SJeff Roberson VM_OBJECT_WUNLOCK(backing_object); 152299a1570aSKonstantin Belousov else 15234cdea4a8SJeff Roberson VM_OBJECT_WUNLOCK(object); 15245975e53dSKonstantin Belousov vm_page_busy_sleep(p, "vmocol", false); 15254cdea4a8SJeff Roberson } 152699a1570aSKonstantin Belousov VM_OBJECT_WLOCK(object); 152799a1570aSKonstantin Belousov VM_OBJECT_WLOCK(backing_object); 152899a1570aSKonstantin Belousov return (TAILQ_FIRST(&backing_object->memq)); 152999a1570aSKonstantin Belousov } 153099a1570aSKonstantin Belousov 153199a1570aSKonstantin Belousov static bool 15324cc8daf7SConrad Meyer vm_object_scan_all_shadowed(vm_object_t object) 15334cc8daf7SConrad Meyer { 15344cc8daf7SConrad Meyer vm_object_t backing_object; 15354cc8daf7SConrad Meyer vm_page_t p, pp; 153677d6fd97SKonstantin Belousov vm_pindex_t backing_offset_index, new_pindex, pi, ps; 15374cc8daf7SConrad Meyer 15384cc8daf7SConrad Meyer VM_OBJECT_ASSERT_WLOCKED(object); 15394cc8daf7SConrad Meyer VM_OBJECT_ASSERT_WLOCKED(object->backing_object); 15404cc8daf7SConrad Meyer 15414cc8daf7SConrad Meyer backing_object = object->backing_object; 15424cc8daf7SConrad Meyer 154363967687SJeff Roberson if ((backing_object->flags & OBJ_ANON) == 0) 15444cc8daf7SConrad Meyer return (false); 15454cc8daf7SConrad Meyer 154677d6fd97SKonstantin Belousov pi = backing_offset_index = OFF_TO_IDX(object->backing_object_offset); 154777d6fd97SKonstantin Belousov p = vm_page_find_least(backing_object, pi); 154877d6fd97SKonstantin Belousov ps = swap_pager_find_least(backing_object, pi); 15494cc8daf7SConrad Meyer 15504cc8daf7SConrad Meyer /* 155177d6fd97SKonstantin Belousov * Only check pages inside the parent object's range and 155277d6fd97SKonstantin Belousov * inside the parent object's mapping of the backing object. 15534cc8daf7SConrad Meyer */ 155477d6fd97SKonstantin Belousov for (;; pi++) { 155577d6fd97SKonstantin Belousov if (p != NULL && p->pindex < pi) 155677d6fd97SKonstantin Belousov p = TAILQ_NEXT(p, listq); 155777d6fd97SKonstantin Belousov if (ps < pi) 155877d6fd97SKonstantin Belousov ps = swap_pager_find_least(backing_object, pi); 155977d6fd97SKonstantin Belousov if (p == NULL && ps >= backing_object->size) 156077d6fd97SKonstantin Belousov break; 156177d6fd97SKonstantin Belousov else if (p == NULL) 156277d6fd97SKonstantin Belousov pi = ps; 156377d6fd97SKonstantin Belousov else 156477d6fd97SKonstantin Belousov pi = MIN(p->pindex, ps); 156577d6fd97SKonstantin Belousov 156677d6fd97SKonstantin Belousov new_pindex = pi - backing_offset_index; 156777d6fd97SKonstantin Belousov if (new_pindex >= object->size) 156877d6fd97SKonstantin Belousov break; 15694cc8daf7SConrad Meyer 15704cc8daf7SConrad Meyer /* 15714cc8daf7SConrad Meyer * See if the parent has the page or if the parent's object 15724cc8daf7SConrad Meyer * pager has the page. If the parent has the page but the page 15734cc8daf7SConrad Meyer * is not valid, the parent's object pager must have the page. 15744cc8daf7SConrad Meyer * 15754cc8daf7SConrad Meyer * If this fails, the parent does not completely shadow the 15764cc8daf7SConrad Meyer * object and we might as well give up now. 15774cc8daf7SConrad Meyer */ 15784cc8daf7SConrad Meyer pp = vm_page_lookup(object, new_pindex); 15790012f373SJeff Roberson /* 15800012f373SJeff Roberson * The valid check here is stable due to object lock being 15810012f373SJeff Roberson * required to clear valid and initiate paging. 15820012f373SJeff Roberson */ 15830012f373SJeff Roberson if ((pp == NULL || vm_page_none_valid(pp)) && 15844cc8daf7SConrad Meyer !vm_pager_has_page(object, new_pindex, NULL, NULL)) 15854cc8daf7SConrad Meyer return (false); 15864cc8daf7SConrad Meyer } 15874cc8daf7SConrad Meyer return (true); 15884cc8daf7SConrad Meyer } 15894cc8daf7SConrad Meyer 15904cc8daf7SConrad Meyer static bool 15914cc8daf7SConrad Meyer vm_object_collapse_scan(vm_object_t object, int op) 15922ad1a3f7SMatthew Dillon { 15932ad1a3f7SMatthew Dillon vm_object_t backing_object; 159499a1570aSKonstantin Belousov vm_page_t next, p, pp; 159599a1570aSKonstantin Belousov vm_pindex_t backing_offset_index, new_pindex; 15962ad1a3f7SMatthew Dillon 159789f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 159889f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object->backing_object); 15992ad1a3f7SMatthew Dillon 16002ad1a3f7SMatthew Dillon backing_object = object->backing_object; 16012ad1a3f7SMatthew Dillon backing_offset_index = OFF_TO_IDX(object->backing_object_offset); 16022ad1a3f7SMatthew Dillon 16032ad1a3f7SMatthew Dillon /* 16042ad1a3f7SMatthew Dillon * Initial conditions 16052ad1a3f7SMatthew Dillon */ 16064cc8daf7SConrad Meyer if ((op & OBSC_COLLAPSE_WAIT) != 0) 16072ad1a3f7SMatthew Dillon vm_object_set_flag(backing_object, OBJ_DEAD); 16082ad1a3f7SMatthew Dillon 16092ad1a3f7SMatthew Dillon /* 16102ad1a3f7SMatthew Dillon * Our scan 16112ad1a3f7SMatthew Dillon */ 16124cc8daf7SConrad Meyer for (p = TAILQ_FIRST(&backing_object->memq); p != NULL; p = next) { 161399a1570aSKonstantin Belousov next = TAILQ_NEXT(p, listq); 161499a1570aSKonstantin Belousov new_pindex = p->pindex - backing_offset_index; 16152ad1a3f7SMatthew Dillon 16162ad1a3f7SMatthew Dillon /* 16172ad1a3f7SMatthew Dillon * Check for busy page 16182ad1a3f7SMatthew Dillon */ 161963e97555SJeff Roberson if (vm_page_tryxbusy(p) == 0) { 16204cc8daf7SConrad Meyer next = vm_object_collapse_scan_wait(object, p, next, op); 16212ad1a3f7SMatthew Dillon continue; 16222ad1a3f7SMatthew Dillon } 16232ad1a3f7SMatthew Dillon 162499a1570aSKonstantin Belousov KASSERT(p->object == backing_object, 16254cc8daf7SConrad Meyer ("vm_object_collapse_scan: object mismatch")); 16262ad1a3f7SMatthew Dillon 162799a1570aSKonstantin Belousov if (p->pindex < backing_offset_index || 162899a1570aSKonstantin Belousov new_pindex >= object->size) { 1629e946b949SAttilio Rao if (backing_object->type == OBJT_SWAP) 16304cc8daf7SConrad Meyer swap_pager_freespace(backing_object, p->pindex, 16314cc8daf7SConrad Meyer 1); 1632e946b949SAttilio Rao 1633f6d89838SAlan Cox KASSERT(!pmap_page_is_mapped(p), 1634f6d89838SAlan Cox ("freeing mapped page %p", p)); 16350fd977b3SMark Johnston if (vm_page_remove(p)) 16362ad1a3f7SMatthew Dillon vm_page_free(p); 16378da1c098SJeff Roberson else 16388da1c098SJeff Roberson vm_page_xunbusy(p); 16392ad1a3f7SMatthew Dillon continue; 16402ad1a3f7SMatthew Dillon } 16412ad1a3f7SMatthew Dillon 16422ad1a3f7SMatthew Dillon pp = vm_page_lookup(object, new_pindex); 164363e97555SJeff Roberson if (pp != NULL && vm_page_tryxbusy(pp) == 0) { 164463e97555SJeff Roberson vm_page_xunbusy(p); 1645e18cc7bfSMax Laier /* 16464cc8daf7SConrad Meyer * The page in the parent is busy and possibly not 16474cc8daf7SConrad Meyer * (yet) valid. Until its state is finalized by the 16484cc8daf7SConrad Meyer * busy bit owner, we can't tell whether it shadows the 16494cc8daf7SConrad Meyer * original page. Therefore, we must either skip it 16504cc8daf7SConrad Meyer * and the original (backing_object) page or wait for 16514cc8daf7SConrad Meyer * its state to be finalized. 1652e18cc7bfSMax Laier * 16534cc8daf7SConrad Meyer * This is due to a race with vm_fault() where we must 16544cc8daf7SConrad Meyer * unbusy the original (backing_obj) page before we can 16554cc8daf7SConrad Meyer * (re)lock the parent. Hence we can get here. 1656e18cc7bfSMax Laier */ 16574cc8daf7SConrad Meyer next = vm_object_collapse_scan_wait(object, pp, next, 16584cc8daf7SConrad Meyer op); 1659e18cc7bfSMax Laier continue; 1660e18cc7bfSMax Laier } 166199a1570aSKonstantin Belousov 16620012f373SJeff Roberson KASSERT(pp == NULL || !vm_page_none_valid(pp), 166399a1570aSKonstantin Belousov ("unbusy invalid page %p", pp)); 166499a1570aSKonstantin Belousov 16654cc8daf7SConrad Meyer if (pp != NULL || vm_pager_has_page(object, new_pindex, NULL, 16664cc8daf7SConrad Meyer NULL)) { 166799a1570aSKonstantin Belousov /* 16684cc8daf7SConrad Meyer * The page already exists in the parent OR swap exists 16694cc8daf7SConrad Meyer * for this location in the parent. Leave the parent's 16704cc8daf7SConrad Meyer * page alone. Destroy the original page from the 16714cc8daf7SConrad Meyer * backing object. 167299a1570aSKonstantin Belousov */ 1673e946b949SAttilio Rao if (backing_object->type == OBJT_SWAP) 16744cc8daf7SConrad Meyer swap_pager_freespace(backing_object, p->pindex, 16754cc8daf7SConrad Meyer 1); 1676f6d89838SAlan Cox KASSERT(!pmap_page_is_mapped(p), 1677f6d89838SAlan Cox ("freeing mapped page %p", p)); 16780fd977b3SMark Johnston if (vm_page_remove(p)) 16792ad1a3f7SMatthew Dillon vm_page_free(p); 16808da1c098SJeff Roberson else 16818da1c098SJeff Roberson vm_page_xunbusy(p); 168263e97555SJeff Roberson if (pp != NULL) 168363e97555SJeff Roberson vm_page_xunbusy(pp); 16842ad1a3f7SMatthew Dillon continue; 16852ad1a3f7SMatthew Dillon } 16862ad1a3f7SMatthew Dillon 1687e946b949SAttilio Rao /* 16884cc8daf7SConrad Meyer * Page does not exist in parent, rename the page from the 16894cc8daf7SConrad Meyer * backing object to the main object. 1690e946b949SAttilio Rao * 16914cc8daf7SConrad Meyer * If the page was mapped to a process, it can remain mapped 16923453bca8SAlan Cox * through the rename. vm_page_rename() will dirty the page. 1693e946b949SAttilio Rao */ 1694e946b949SAttilio Rao if (vm_page_rename(p, object, new_pindex)) { 169563e97555SJeff Roberson vm_page_xunbusy(p); 169663e97555SJeff Roberson if (pp != NULL) 169763e97555SJeff Roberson vm_page_xunbusy(pp); 16984cc8daf7SConrad Meyer next = vm_object_collapse_scan_wait(object, NULL, next, 16994cc8daf7SConrad Meyer op); 1700e946b949SAttilio Rao continue; 1701e946b949SAttilio Rao } 170214a5dc17SAttilio Rao 170314a5dc17SAttilio Rao /* Use the old pindex to free the right page. */ 1704e946b949SAttilio Rao if (backing_object->type == OBJT_SWAP) 170514a5dc17SAttilio Rao swap_pager_freespace(backing_object, 170614a5dc17SAttilio Rao new_pindex + backing_offset_index, 1); 1707e946b949SAttilio Rao 1708f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 1709f8a47341SAlan Cox /* 1710f8a47341SAlan Cox * Rename the reservation. 1711f8a47341SAlan Cox */ 1712f8a47341SAlan Cox vm_reserv_rename(p, object, backing_object, 1713f8a47341SAlan Cox backing_offset_index); 1714f8a47341SAlan Cox #endif 17158da1c098SJeff Roberson vm_page_xunbusy(p); 17162ad1a3f7SMatthew Dillon } 171799a1570aSKonstantin Belousov return (true); 17182ad1a3f7SMatthew Dillon } 17192ad1a3f7SMatthew Dillon 1720df8bae1dSRodney W. Grimes 1721df8bae1dSRodney W. Grimes /* 17222fe6e4d7SDavid Greenman * this version of collapse allows the operation to occur earlier and 17232fe6e4d7SDavid Greenman * when paging_in_progress is true for an object... This is not a complete 17242fe6e4d7SDavid Greenman * operation, but should plug 99.9% of the rest of the leaks. 17252fe6e4d7SDavid Greenman */ 17262fe6e4d7SDavid Greenman static void 17271b40f8c0SMatthew Dillon vm_object_qcollapse(vm_object_t object) 17282fe6e4d7SDavid Greenman { 17292ad1a3f7SMatthew Dillon vm_object_t backing_object = object->backing_object; 17302fe6e4d7SDavid Greenman 173189f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 173289f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(backing_object); 17331b40f8c0SMatthew Dillon 17342fe6e4d7SDavid Greenman if (backing_object->ref_count != 1) 17352fe6e4d7SDavid Greenman return; 17362fe6e4d7SDavid Greenman 17374cc8daf7SConrad Meyer vm_object_collapse_scan(object, OBSC_COLLAPSE_NOWAIT); 17382fe6e4d7SDavid Greenman } 17392fe6e4d7SDavid Greenman 1740df8bae1dSRodney W. Grimes /* 1741df8bae1dSRodney W. Grimes * vm_object_collapse: 1742df8bae1dSRodney W. Grimes * 1743df8bae1dSRodney W. Grimes * Collapse an object with the object backing it. 1744df8bae1dSRodney W. Grimes * Pages in the backing object are moved into the 1745df8bae1dSRodney W. Grimes * parent, and the backing object is deallocated. 1746df8bae1dSRodney W. Grimes */ 174726f9a767SRodney W. Grimes void 17481b40f8c0SMatthew Dillon vm_object_collapse(vm_object_t object) 1749df8bae1dSRodney W. Grimes { 175098f139daSKonstantin Belousov vm_object_t backing_object, new_backing_object; 175198f139daSKonstantin Belousov 175289f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 175323955314SAlfred Perlstein 1754df8bae1dSRodney W. Grimes while (TRUE) { 1755df8bae1dSRodney W. Grimes /* 1756df8bae1dSRodney W. Grimes * Verify that the conditions are right for collapse: 1757df8bae1dSRodney W. Grimes * 17582ad1a3f7SMatthew Dillon * The object exists and the backing object exists. 1759df8bae1dSRodney W. Grimes */ 176024a1cce3SDavid Greenman if ((backing_object = object->backing_object) == NULL) 17612ad1a3f7SMatthew Dillon break; 1762df8bae1dSRodney W. Grimes 1763f919ebdeSDavid Greenman /* 1764f919ebdeSDavid Greenman * we check the backing object first, because it is most likely 176524a1cce3SDavid Greenman * not collapsable. 1766f919ebdeSDavid Greenman */ 176763967687SJeff Roberson if ((backing_object->flags & OBJ_ANON) == 0) 176863967687SJeff Roberson break; 176989f6b863SAttilio Rao VM_OBJECT_WLOCK(backing_object); 177032362449SKonstantin Belousov if ((backing_object->flags & OBJ_DEAD) != 0 || 177132362449SKonstantin Belousov (object->flags & (OBJ_DEAD | OBJ_ANON)) != OBJ_ANON) { 177289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 17732ad1a3f7SMatthew Dillon break; 177424a1cce3SDavid Greenman } 17759b4814bbSDavid Greenman 177611b57401SHans Petter Selasky if (REFCOUNT_COUNT(object->paging_in_progress) > 0 || 177711b57401SHans Petter Selasky REFCOUNT_COUNT(backing_object->paging_in_progress) > 0) { 1778b9921222SDavid Greenman vm_object_qcollapse(object); 177989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 17802ad1a3f7SMatthew Dillon break; 1781df8bae1dSRodney W. Grimes } 178298f139daSKonstantin Belousov 178326f9a767SRodney W. Grimes /* 17840d94caffSDavid Greenman * We know that we can either collapse the backing object (if 17852ad1a3f7SMatthew Dillon * the parent is the only reference to it) or (perhaps) have 17862ad1a3f7SMatthew Dillon * the parent bypass the object if the parent happens to shadow 17872ad1a3f7SMatthew Dillon * all the resident pages in the entire backing object. 17882ad1a3f7SMatthew Dillon * 17892ad1a3f7SMatthew Dillon * This is ignoring pager-backed pages such as swap pages. 17904cc8daf7SConrad Meyer * vm_object_collapse_scan fails the shadowing test in this 17912ad1a3f7SMatthew Dillon * case. 1792df8bae1dSRodney W. Grimes */ 1793df8bae1dSRodney W. Grimes if (backing_object->ref_count == 1) { 1794aa9bc3b1SKonstantin Belousov vm_object_pip_add(object, 1); 1795aa9bc3b1SKonstantin Belousov vm_object_pip_add(backing_object, 1); 1796aa9bc3b1SKonstantin Belousov 1797df8bae1dSRodney W. Grimes /* 17982ad1a3f7SMatthew Dillon * If there is exactly one reference to the backing 17992ad1a3f7SMatthew Dillon * object, we can collapse it into the parent. 1800df8bae1dSRodney W. Grimes */ 18014cc8daf7SConrad Meyer vm_object_collapse_scan(object, OBSC_COLLAPSE_WAIT); 1802df8bae1dSRodney W. Grimes 1803f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 1804f8a47341SAlan Cox /* 1805f8a47341SAlan Cox * Break any reservations from backing_object. 1806f8a47341SAlan Cox */ 1807f8a47341SAlan Cox if (__predict_false(!LIST_EMPTY(&backing_object->rvq))) 1808f8a47341SAlan Cox vm_reserv_break_all(backing_object); 1809f8a47341SAlan Cox #endif 1810f8a47341SAlan Cox 1811df8bae1dSRodney W. Grimes /* 1812df8bae1dSRodney W. Grimes * Move the pager from backing_object to object. 1813df8bae1dSRodney W. Grimes */ 18146be36525SAlan Cox if (backing_object->type == OBJT_SWAP) { 181524a1cce3SDavid Greenman /* 1816c7c8dd7eSAlan Cox * swap_pager_copy() can sleep, in which case 1817c7c8dd7eSAlan Cox * the backing_object's and object's locks are 1818c7c8dd7eSAlan Cox * released and reacquired. 1819571a1e92SAttilio Rao * Since swap_pager_copy() is being asked to 1820571a1e92SAttilio Rao * destroy the source, it will change the 1821571a1e92SAttilio Rao * backing_object's type to OBJT_DEFAULT. 182224a1cce3SDavid Greenman */ 18231c7c3c6aSMatthew Dillon swap_pager_copy( 18241c7c3c6aSMatthew Dillon backing_object, 18251c7c3c6aSMatthew Dillon object, 18261c7c3c6aSMatthew Dillon OFF_TO_IDX(object->backing_object_offset), TRUE); 1827c0503609SDavid Greenman } 1828df8bae1dSRodney W. Grimes /* 1829df8bae1dSRodney W. Grimes * Object now shadows whatever backing_object did. 18302ad1a3f7SMatthew Dillon * Note that the reference to 18312ad1a3f7SMatthew Dillon * backing_object->backing_object moves from within 18322ad1a3f7SMatthew Dillon * backing_object to within object. 1833df8bae1dSRodney W. Grimes */ 183451b867e5SJeff Roberson vm_object_backing_remove_locked(object); 183551b867e5SJeff Roberson new_backing_object = backing_object->backing_object; 183651b867e5SJeff Roberson if (new_backing_object != NULL) { 183751b867e5SJeff Roberson VM_OBJECT_WLOCK(new_backing_object); 183851b867e5SJeff Roberson vm_object_backing_remove_locked(backing_object); 183951b867e5SJeff Roberson vm_object_backing_insert_locked(object, 184051b867e5SJeff Roberson new_backing_object); 184151b867e5SJeff Roberson VM_OBJECT_WUNLOCK(new_backing_object); 1842de5f6a77SJohn Dyson } 18432ad1a3f7SMatthew Dillon object->backing_object_offset += 18442ad1a3f7SMatthew Dillon backing_object->backing_object_offset; 18452ad1a3f7SMatthew Dillon 1846df8bae1dSRodney W. Grimes /* 1847df8bae1dSRodney W. Grimes * Discard backing_object. 1848df8bae1dSRodney W. Grimes * 18490d94caffSDavid Greenman * Since the backing object has no pages, no pager left, 18500d94caffSDavid Greenman * and no object references within it, all that is 18510d94caffSDavid Greenman * necessary is to dispose of it. 1852df8bae1dSRodney W. Grimes */ 18539b4d473aSKonstantin Belousov KASSERT(backing_object->ref_count == 1, ( 18549b4d473aSKonstantin Belousov "backing_object %p was somehow re-referenced during collapse!", 18559b4d473aSKonstantin Belousov backing_object)); 1856aa9bc3b1SKonstantin Belousov vm_object_pip_wakeup(backing_object); 1857e735691bSJohn Baldwin backing_object->type = OBJT_DEAD; 1858*a67d5408SJeff Roberson refcount_release(&backing_object->ref_count); 185989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 18609b4d473aSKonstantin Belousov vm_object_destroy(backing_object); 1861df8bae1dSRodney W. Grimes 1862aa9bc3b1SKonstantin Belousov vm_object_pip_wakeup(object); 186311542376SAlan Cox counter_u64_add(object_collapses, 1); 18640d94caffSDavid Greenman } else { 1865df8bae1dSRodney W. Grimes /* 18662ad1a3f7SMatthew Dillon * If we do not entirely shadow the backing object, 18672ad1a3f7SMatthew Dillon * there is nothing we can do so we give up. 1868df8bae1dSRodney W. Grimes */ 1869df59a0feSJeff Roberson if (object->resident_page_count != object->size && 18704cc8daf7SConrad Meyer !vm_object_scan_all_shadowed(object)) { 187189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 18722ad1a3f7SMatthew Dillon break; 187324a1cce3SDavid Greenman } 1874df8bae1dSRodney W. Grimes 1875df8bae1dSRodney W. Grimes /* 18760d94caffSDavid Greenman * Make the parent shadow the next object in the 18770d94caffSDavid Greenman * chain. Deallocating backing_object will not remove 18780d94caffSDavid Greenman * it, since its reference count is at least 2. 1879df8bae1dSRodney W. Grimes */ 188051b867e5SJeff Roberson vm_object_backing_remove_locked(object); 188195e5e988SJohn Dyson 188295e5e988SJohn Dyson new_backing_object = backing_object->backing_object; 188351b867e5SJeff Roberson if (new_backing_object != NULL) { 188451b867e5SJeff Roberson vm_object_backing_insert(object, 188551b867e5SJeff Roberson new_backing_object); 188651b867e5SJeff Roberson vm_object_reference(new_backing_object); 188795e5e988SJohn Dyson object->backing_object_offset += 188895e5e988SJohn Dyson backing_object->backing_object_offset; 1889de5f6a77SJohn Dyson } 1890df8bae1dSRodney W. Grimes 1891df8bae1dSRodney W. Grimes /* 18920d94caffSDavid Greenman * Drop the reference count on backing_object. Since 189322ec553fSAlan Cox * its ref_count was at least 2, it will not vanish. 1894df8bae1dSRodney W. Grimes */ 189551df5321SJeff Roberson refcount_release(&backing_object->ref_count); 189689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 189711542376SAlan Cox counter_u64_add(object_bypasses, 1); 1898df8bae1dSRodney W. Grimes } 1899df8bae1dSRodney W. Grimes 1900df8bae1dSRodney W. Grimes /* 1901df8bae1dSRodney W. Grimes * Try again with this object's new backing object. 1902df8bae1dSRodney W. Grimes */ 1903df8bae1dSRodney W. Grimes } 1904df8bae1dSRodney W. Grimes } 1905df8bae1dSRodney W. Grimes 1906df8bae1dSRodney W. Grimes /* 1907bff99f0dSAlan Cox * vm_object_page_remove: 1908df8bae1dSRodney W. Grimes * 190968855966SAlan Cox * For the given object, either frees or invalidates each of the 19106bbee8e2SAlan Cox * specified pages. In general, a page is freed. However, if a page is 19116bbee8e2SAlan Cox * wired for any reason other than the existence of a managed, wired 19126bbee8e2SAlan Cox * mapping, then it may be invalidated but not removed from the object. 19136bbee8e2SAlan Cox * Pages are specified by the given range ["start", "end") and the option 19146bbee8e2SAlan Cox * OBJPR_CLEANONLY. As a special case, if "end" is zero, then the range 19156bbee8e2SAlan Cox * extends from "start" to the end of the object. If the option 19166bbee8e2SAlan Cox * OBJPR_CLEANONLY is specified, then only the non-dirty pages within the 19176bbee8e2SAlan Cox * specified range are affected. If the option OBJPR_NOTMAPPED is 19186bbee8e2SAlan Cox * specified, then the pages within the specified range must have no 19196bbee8e2SAlan Cox * mappings. Otherwise, if this option is not specified, any mappings to 19206bbee8e2SAlan Cox * the specified pages are removed before the pages are freed or 19216bbee8e2SAlan Cox * invalidated. 192268855966SAlan Cox * 19236bbee8e2SAlan Cox * In general, this operation should only be performed on objects that 19246bbee8e2SAlan Cox * contain managed pages. There are, however, two exceptions. First, it 19256bbee8e2SAlan Cox * is performed on the kernel and kmem objects by vm_map_entry_delete(). 19266bbee8e2SAlan Cox * Second, it is used by msync(..., MS_INVALIDATE) to invalidate device- 19276bbee8e2SAlan Cox * backed pages. In both of these cases, the option OBJPR_CLEANONLY must 19286bbee8e2SAlan Cox * not be specified and the option OBJPR_NOTMAPPED must be specified. 1929df8bae1dSRodney W. Grimes * 1930df8bae1dSRodney W. Grimes * The object must be locked. 1931df8bae1dSRodney W. Grimes */ 193226f9a767SRodney W. Grimes void 1933ecde4b32SAlan Cox vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end, 19346bbee8e2SAlan Cox int options) 1935df8bae1dSRodney W. Grimes { 1936d031cff1SMatthew Dillon vm_page_t p, next; 1937df8bae1dSRodney W. Grimes 193889f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 193928634820SAlan Cox KASSERT((object->flags & OBJ_UNMANAGED) == 0 || 19406bbee8e2SAlan Cox (options & (OBJPR_CLEANONLY | OBJPR_NOTMAPPED)) == OBJPR_NOTMAPPED, 19416bbee8e2SAlan Cox ("vm_object_page_remove: illegal options for object %p", object)); 1942ecde4b32SAlan Cox if (object->resident_page_count == 0) 19437667839aSAlan Cox return; 1944d474eaaaSDoug Rabson vm_object_pip_add(object, 1); 194526f9a767SRodney W. Grimes again: 1946b382c10aSKonstantin Belousov p = vm_page_find_least(object, start); 19472965a453SKip Macy 194875741c04SAlan Cox /* 19496bbee8e2SAlan Cox * Here, the variable "p" is either (1) the page with the least pindex 19506bbee8e2SAlan Cox * greater than or equal to the parameter "start" or (2) NULL. 195175741c04SAlan Cox */ 19526bbee8e2SAlan Cox for (; p != NULL && (p->pindex < end || end == 0); p = next) { 1953b18bfc3dSJohn Dyson next = TAILQ_NEXT(p, listq); 195475741c04SAlan Cox 195559677d3cSAlan Cox /* 19566bbee8e2SAlan Cox * If the page is wired for any reason besides the existence 19576bbee8e2SAlan Cox * of managed, wired mappings, then it cannot be freed. For 19586bbee8e2SAlan Cox * example, fictitious pages, which represent device memory, 19596bbee8e2SAlan Cox * are inherently wired and cannot be freed. They can, 19606bbee8e2SAlan Cox * however, be invalidated if the option OBJPR_CLEANONLY is 19616bbee8e2SAlan Cox * not specified. 196259677d3cSAlan Cox */ 196363e97555SJeff Roberson if (vm_page_tryxbusy(p) == 0) { 19644cdea4a8SJeff Roberson vm_page_sleep_if_busy(p, "vmopar"); 1965fee2a2faSMark Johnston goto again; 1966fee2a2faSMark Johnston } 1967d842aa51SMark Johnston if (vm_page_wired(p)) { 1968fee2a2faSMark Johnston wired: 1969cf060942SAlan Cox if ((options & OBJPR_NOTMAPPED) == 0 && 1970cf060942SAlan Cox object->ref_count != 0) 19714fec79beSAlan Cox pmap_remove_all(p); 19726bbee8e2SAlan Cox if ((options & OBJPR_CLEANONLY) == 0) { 19730012f373SJeff Roberson vm_page_invalid(p); 1974a28042d1SAlan Cox vm_page_undirty(p); 1975a28042d1SAlan Cox } 197663e97555SJeff Roberson vm_page_xunbusy(p); 197793c5d3a4SKonstantin Belousov continue; 19780d94caffSDavid Greenman } 197968855966SAlan Cox KASSERT((p->flags & PG_FICTITIOUS) == 0, 198068855966SAlan Cox ("vm_object_page_remove: page %p is fictitious", p)); 19810012f373SJeff Roberson if ((options & OBJPR_CLEANONLY) != 0 && 19820012f373SJeff Roberson !vm_page_none_valid(p)) { 1983cf060942SAlan Cox if ((options & OBJPR_NOTMAPPED) == 0 && 1984fee2a2faSMark Johnston object->ref_count != 0 && 1985fee2a2faSMark Johnston !vm_page_try_remove_write(p)) 1986fee2a2faSMark Johnston goto wired; 198763e97555SJeff Roberson if (p->dirty != 0) { 198863e97555SJeff Roberson vm_page_xunbusy(p); 198993c5d3a4SKonstantin Belousov continue; 19902965a453SKip Macy } 199163e97555SJeff Roberson } 1992fee2a2faSMark Johnston if ((options & OBJPR_NOTMAPPED) == 0 && 1993fee2a2faSMark Johnston object->ref_count != 0 && !vm_page_try_remove_all(p)) 1994fee2a2faSMark Johnston goto wired; 19955cd29d0fSMark Johnston vm_page_free(p); 19962965a453SKip Macy } 1997f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 1998c0503609SDavid Greenman } 1999df8bae1dSRodney W. Grimes 2000df8bae1dSRodney W. Grimes /* 20013138cd36SMark Johnston * vm_object_page_noreuse: 2002936c09acSJohn Baldwin * 20033138cd36SMark Johnston * For the given object, attempt to move the specified pages to 20043138cd36SMark Johnston * the head of the inactive queue. This bypasses regular LRU 20053138cd36SMark Johnston * operation and allows the pages to be reused quickly under memory 20063138cd36SMark Johnston * pressure. If a page is wired for any reason, then it will not 20073138cd36SMark Johnston * be queued. Pages are specified by the range ["start", "end"). 20083138cd36SMark Johnston * As a special case, if "end" is zero, then the range extends from 20093138cd36SMark Johnston * "start" to the end of the object. 2010936c09acSJohn Baldwin * 2011936c09acSJohn Baldwin * This operation should only be performed on objects that 201228634820SAlan Cox * contain non-fictitious, managed pages. 2013936c09acSJohn Baldwin * 2014936c09acSJohn Baldwin * The object must be locked. 2015936c09acSJohn Baldwin */ 2016936c09acSJohn Baldwin void 20173138cd36SMark Johnston vm_object_page_noreuse(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 2018936c09acSJohn Baldwin { 201993c5d3a4SKonstantin Belousov struct mtx *mtx; 2020936c09acSJohn Baldwin vm_page_t p, next; 2021936c09acSJohn Baldwin 202252d1addaSAlan Cox VM_OBJECT_ASSERT_LOCKED(object); 202328634820SAlan Cox KASSERT((object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0, 20243138cd36SMark Johnston ("vm_object_page_noreuse: illegal object %p", object)); 2025936c09acSJohn Baldwin if (object->resident_page_count == 0) 2026936c09acSJohn Baldwin return; 2027936c09acSJohn Baldwin p = vm_page_find_least(object, start); 2028936c09acSJohn Baldwin 2029936c09acSJohn Baldwin /* 2030936c09acSJohn Baldwin * Here, the variable "p" is either (1) the page with the least pindex 2031936c09acSJohn Baldwin * greater than or equal to the parameter "start" or (2) NULL. 2032936c09acSJohn Baldwin */ 2033936c09acSJohn Baldwin mtx = NULL; 2034936c09acSJohn Baldwin for (; p != NULL && (p->pindex < end || end == 0); p = next) { 2035936c09acSJohn Baldwin next = TAILQ_NEXT(p, listq); 203693c5d3a4SKonstantin Belousov vm_page_change_lock(p, &mtx); 20373138cd36SMark Johnston vm_page_deactivate_noreuse(p); 2038936c09acSJohn Baldwin } 2039936c09acSJohn Baldwin if (mtx != NULL) 2040936c09acSJohn Baldwin mtx_unlock(mtx); 2041936c09acSJohn Baldwin } 2042936c09acSJohn Baldwin 2043936c09acSJohn Baldwin /* 2044387aabc5SAlan Cox * Populate the specified range of the object with valid pages. Returns 2045387aabc5SAlan Cox * TRUE if the range is successfully populated and FALSE otherwise. 2046387aabc5SAlan Cox * 2047387aabc5SAlan Cox * Note: This function should be optimized to pass a larger array of 2048387aabc5SAlan Cox * pages to vm_pager_get_pages() before it is applied to a non- 2049387aabc5SAlan Cox * OBJT_DEVICE object. 2050387aabc5SAlan Cox * 2051387aabc5SAlan Cox * The object must be locked. 2052387aabc5SAlan Cox */ 2053387aabc5SAlan Cox boolean_t 2054387aabc5SAlan Cox vm_object_populate(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 2055387aabc5SAlan Cox { 2056093c7f39SGleb Smirnoff vm_page_t m; 2057387aabc5SAlan Cox vm_pindex_t pindex; 2058387aabc5SAlan Cox int rv; 2059387aabc5SAlan Cox 206089f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 2061387aabc5SAlan Cox for (pindex = start; pindex < end; pindex++) { 2062c7575748SJeff Roberson rv = vm_page_grab_valid(&m, object, pindex, VM_ALLOC_NORMAL); 2063c7575748SJeff Roberson if (rv != VM_PAGER_OK) 2064387aabc5SAlan Cox break; 2065c7575748SJeff Roberson 2066387aabc5SAlan Cox /* 2067387aabc5SAlan Cox * Keep "m" busy because a subsequent iteration may unlock 2068387aabc5SAlan Cox * the object. 2069387aabc5SAlan Cox */ 2070387aabc5SAlan Cox } 2071387aabc5SAlan Cox if (pindex > start) { 2072387aabc5SAlan Cox m = vm_page_lookup(object, start); 2073387aabc5SAlan Cox while (m != NULL && m->pindex < pindex) { 2074c7aebda8SAttilio Rao vm_page_xunbusy(m); 2075387aabc5SAlan Cox m = TAILQ_NEXT(m, listq); 2076387aabc5SAlan Cox } 2077387aabc5SAlan Cox } 2078387aabc5SAlan Cox return (pindex == end); 2079387aabc5SAlan Cox } 2080387aabc5SAlan Cox 2081387aabc5SAlan Cox /* 2082df8bae1dSRodney W. Grimes * Routine: vm_object_coalesce 2083df8bae1dSRodney W. Grimes * Function: Coalesces two objects backing up adjoining 2084df8bae1dSRodney W. Grimes * regions of memory into a single object. 2085df8bae1dSRodney W. Grimes * 2086df8bae1dSRodney W. Grimes * returns TRUE if objects were combined. 2087df8bae1dSRodney W. Grimes * 2088df8bae1dSRodney W. Grimes * NOTE: Only works at the moment if the second object is NULL - 2089df8bae1dSRodney W. Grimes * if it's not, which object do we lock first? 2090df8bae1dSRodney W. Grimes * 2091df8bae1dSRodney W. Grimes * Parameters: 2092df8bae1dSRodney W. Grimes * prev_object First object to coalesce 2093df8bae1dSRodney W. Grimes * prev_offset Offset into prev_object 2094df8bae1dSRodney W. Grimes * prev_size Size of reference to prev_object 209557a21abaSAlan Cox * next_size Size of reference to the second object 20963364c323SKonstantin Belousov * reserved Indicator that extension region has 20973364c323SKonstantin Belousov * swap accounted for 2098df8bae1dSRodney W. Grimes * 2099df8bae1dSRodney W. Grimes * Conditions: 2100df8bae1dSRodney W. Grimes * The object must *not* be locked. 2101df8bae1dSRodney W. Grimes */ 21020d94caffSDavid Greenman boolean_t 210357a21abaSAlan Cox vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset, 21043364c323SKonstantin Belousov vm_size_t prev_size, vm_size_t next_size, boolean_t reserved) 2105df8bae1dSRodney W. Grimes { 2106ea41812fSAlan Cox vm_pindex_t next_pindex; 2107df8bae1dSRodney W. Grimes 210800e1854aSAlan Cox if (prev_object == NULL) 2109df8bae1dSRodney W. Grimes return (TRUE); 211063967687SJeff Roberson if ((prev_object->flags & OBJ_ANON) == 0) 211130dcfc09SJohn Dyson return (FALSE); 211230dcfc09SJohn Dyson 211363967687SJeff Roberson VM_OBJECT_WLOCK(prev_object); 2114df8bae1dSRodney W. Grimes /* 2115df8bae1dSRodney W. Grimes * Try to collapse the object first 2116df8bae1dSRodney W. Grimes */ 2117df8bae1dSRodney W. Grimes vm_object_collapse(prev_object); 2118df8bae1dSRodney W. Grimes 2119df8bae1dSRodney W. Grimes /* 21200d94caffSDavid Greenman * Can't coalesce if: . more than one reference . paged out . shadows 21210d94caffSDavid Greenman * another object . has a copy elsewhere (any of which mean that the 21220d94caffSDavid Greenman * pages not mapped to prev_entry may be in use anyway) 2123df8bae1dSRodney W. Grimes */ 21248cc7e047SJohn Dyson if (prev_object->backing_object != NULL) { 212589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(prev_object); 2126df8bae1dSRodney W. Grimes return (FALSE); 2127df8bae1dSRodney W. Grimes } 2128a316d390SJohn Dyson 2129a316d390SJohn Dyson prev_size >>= PAGE_SHIFT; 2130a316d390SJohn Dyson next_size >>= PAGE_SHIFT; 213157a21abaSAlan Cox next_pindex = OFF_TO_IDX(prev_offset) + prev_size; 21328cc7e047SJohn Dyson 21330e48e068SMark Johnston if (prev_object->ref_count > 1 && 21340e48e068SMark Johnston prev_object->size != next_pindex && 21350e48e068SMark Johnston (prev_object->flags & OBJ_ONEMAPPING) == 0) { 213689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(prev_object); 21378cc7e047SJohn Dyson return (FALSE); 21388cc7e047SJohn Dyson } 21398cc7e047SJohn Dyson 2140df8bae1dSRodney W. Grimes /* 21413364c323SKonstantin Belousov * Account for the charge. 21423364c323SKonstantin Belousov */ 2143ef694c1aSEdward Tomasz Napierala if (prev_object->cred != NULL) { 21443364c323SKonstantin Belousov 21453364c323SKonstantin Belousov /* 21463364c323SKonstantin Belousov * If prev_object was charged, then this mapping, 2147763df3ecSPedro F. Giffuni * although not charged now, may become writable 2148ef694c1aSEdward Tomasz Napierala * later. Non-NULL cred in the object would prevent 21493364c323SKonstantin Belousov * swap reservation during enabling of the write 21503364c323SKonstantin Belousov * access, so reserve swap now. Failed reservation 21513364c323SKonstantin Belousov * cause allocation of the separate object for the map 21523364c323SKonstantin Belousov * entry, and swap reservation for this entry is 21533364c323SKonstantin Belousov * managed in appropriate time. 21543364c323SKonstantin Belousov */ 2155ef694c1aSEdward Tomasz Napierala if (!reserved && !swap_reserve_by_cred(ptoa(next_size), 2156ef694c1aSEdward Tomasz Napierala prev_object->cred)) { 21579f790a17SKonstantin Belousov VM_OBJECT_WUNLOCK(prev_object); 21583364c323SKonstantin Belousov return (FALSE); 21593364c323SKonstantin Belousov } 21603364c323SKonstantin Belousov prev_object->charge += ptoa(next_size); 21613364c323SKonstantin Belousov } 21623364c323SKonstantin Belousov 21633364c323SKonstantin Belousov /* 21640d94caffSDavid Greenman * Remove any pages that may still be in the object from a previous 21650d94caffSDavid Greenman * deallocation. 2166df8bae1dSRodney W. Grimes */ 2167ea41812fSAlan Cox if (next_pindex < prev_object->size) { 21686bbee8e2SAlan Cox vm_object_page_remove(prev_object, next_pindex, next_pindex + 21696bbee8e2SAlan Cox next_size, 0); 2170ea41812fSAlan Cox if (prev_object->type == OBJT_SWAP) 2171ea41812fSAlan Cox swap_pager_freespace(prev_object, 2172ea41812fSAlan Cox next_pindex, next_size); 21733364c323SKonstantin Belousov #if 0 2174ef694c1aSEdward Tomasz Napierala if (prev_object->cred != NULL) { 21753364c323SKonstantin Belousov KASSERT(prev_object->charge >= 21763364c323SKonstantin Belousov ptoa(prev_object->size - next_pindex), 21773364c323SKonstantin Belousov ("object %p overcharged 1 %jx %jx", prev_object, 21783364c323SKonstantin Belousov (uintmax_t)next_pindex, (uintmax_t)next_size)); 21793364c323SKonstantin Belousov prev_object->charge -= ptoa(prev_object->size - 21803364c323SKonstantin Belousov next_pindex); 21813364c323SKonstantin Belousov } 21823364c323SKonstantin Belousov #endif 2183ea41812fSAlan Cox } 2184df8bae1dSRodney W. Grimes 2185df8bae1dSRodney W. Grimes /* 2186df8bae1dSRodney W. Grimes * Extend the object if necessary. 2187df8bae1dSRodney W. Grimes */ 2188ea41812fSAlan Cox if (next_pindex + next_size > prev_object->size) 2189ea41812fSAlan Cox prev_object->size = next_pindex + next_size; 2190df8bae1dSRodney W. Grimes 219189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(prev_object); 2192df8bae1dSRodney W. Grimes return (TRUE); 2193df8bae1dSRodney W. Grimes } 2194df8bae1dSRodney W. Grimes 21957a5a6352SMatthew Dillon void 21967a5a6352SMatthew Dillon vm_object_set_writeable_dirty(vm_object_t object) 21977a5a6352SMatthew Dillon { 21987a5a6352SMatthew Dillon 219967d0e293SJeff Roberson VM_OBJECT_ASSERT_LOCKED(object); 220067d0e293SJeff Roberson 220167d0e293SJeff Roberson /* Only set for vnodes & tmpfs */ 220267d0e293SJeff Roberson if (object->type != OBJT_VNODE && 220367d0e293SJeff Roberson (object->flags & OBJ_TMPFS_NODE) == 0) 22043280870dSKonstantin Belousov return; 220567d0e293SJeff Roberson atomic_add_int(&object->generation, 1); 22067a5a6352SMatthew Dillon } 22077a5a6352SMatthew Dillon 220803462509SAlan Cox /* 220903462509SAlan Cox * vm_object_unwire: 221003462509SAlan Cox * 221103462509SAlan Cox * For each page offset within the specified range of the given object, 221203462509SAlan Cox * find the highest-level page in the shadow chain and unwire it. A page 221303462509SAlan Cox * must exist at every page offset, and the highest-level page must be 221403462509SAlan Cox * wired. 221503462509SAlan Cox */ 221603462509SAlan Cox void 221703462509SAlan Cox vm_object_unwire(vm_object_t object, vm_ooffset_t offset, vm_size_t length, 221803462509SAlan Cox uint8_t queue) 221903462509SAlan Cox { 222020e4afbfSKonstantin Belousov vm_object_t tobject, t1object; 222103462509SAlan Cox vm_page_t m, tm; 222203462509SAlan Cox vm_pindex_t end_pindex, pindex, tpindex; 222303462509SAlan Cox int depth, locked_depth; 222403462509SAlan Cox 222503462509SAlan Cox KASSERT((offset & PAGE_MASK) == 0, 222603462509SAlan Cox ("vm_object_unwire: offset is not page aligned")); 222703462509SAlan Cox KASSERT((length & PAGE_MASK) == 0, 222803462509SAlan Cox ("vm_object_unwire: length is not a multiple of PAGE_SIZE")); 222903462509SAlan Cox /* The wired count of a fictitious page never changes. */ 223003462509SAlan Cox if ((object->flags & OBJ_FICTITIOUS) != 0) 223103462509SAlan Cox return; 223203462509SAlan Cox pindex = OFF_TO_IDX(offset); 223303462509SAlan Cox end_pindex = pindex + atop(length); 223420e4afbfSKonstantin Belousov again: 223503462509SAlan Cox locked_depth = 1; 223603462509SAlan Cox VM_OBJECT_RLOCK(object); 223703462509SAlan Cox m = vm_page_find_least(object, pindex); 223803462509SAlan Cox while (pindex < end_pindex) { 223903462509SAlan Cox if (m == NULL || pindex < m->pindex) { 224003462509SAlan Cox /* 224103462509SAlan Cox * The first object in the shadow chain doesn't 224203462509SAlan Cox * contain a page at the current index. Therefore, 224303462509SAlan Cox * the page must exist in a backing object. 224403462509SAlan Cox */ 224503462509SAlan Cox tobject = object; 224603462509SAlan Cox tpindex = pindex; 224703462509SAlan Cox depth = 0; 224803462509SAlan Cox do { 224903462509SAlan Cox tpindex += 225003462509SAlan Cox OFF_TO_IDX(tobject->backing_object_offset); 225103462509SAlan Cox tobject = tobject->backing_object; 225203462509SAlan Cox KASSERT(tobject != NULL, 225303462509SAlan Cox ("vm_object_unwire: missing page")); 225403462509SAlan Cox if ((tobject->flags & OBJ_FICTITIOUS) != 0) 225503462509SAlan Cox goto next_page; 225603462509SAlan Cox depth++; 225703462509SAlan Cox if (depth == locked_depth) { 225803462509SAlan Cox locked_depth++; 225903462509SAlan Cox VM_OBJECT_RLOCK(tobject); 226003462509SAlan Cox } 226103462509SAlan Cox } while ((tm = vm_page_lookup(tobject, tpindex)) == 226203462509SAlan Cox NULL); 226303462509SAlan Cox } else { 226403462509SAlan Cox tm = m; 226503462509SAlan Cox m = TAILQ_NEXT(m, listq); 226603462509SAlan Cox } 226763e97555SJeff Roberson if (vm_page_trysbusy(tm) == 0) { 226887e93ea6SMark Johnston for (tobject = object; locked_depth >= 1; 226920e4afbfSKonstantin Belousov locked_depth--) { 227020e4afbfSKonstantin Belousov t1object = tobject->backing_object; 227187e93ea6SMark Johnston if (tm->object != tobject) 227220e4afbfSKonstantin Belousov VM_OBJECT_RUNLOCK(tobject); 227320e4afbfSKonstantin Belousov tobject = t1object; 227420e4afbfSKonstantin Belousov } 227520e4afbfSKonstantin Belousov vm_page_busy_sleep(tm, "unwbo", true); 227620e4afbfSKonstantin Belousov goto again; 227720e4afbfSKonstantin Belousov } 227803462509SAlan Cox vm_page_unwire(tm, queue); 227963e97555SJeff Roberson vm_page_sunbusy(tm); 228003462509SAlan Cox next_page: 228103462509SAlan Cox pindex++; 228203462509SAlan Cox } 228303462509SAlan Cox /* Release the accumulated object locks. */ 228420e4afbfSKonstantin Belousov for (tobject = object; locked_depth >= 1; locked_depth--) { 228520e4afbfSKonstantin Belousov t1object = tobject->backing_object; 228620e4afbfSKonstantin Belousov VM_OBJECT_RUNLOCK(tobject); 228720e4afbfSKonstantin Belousov tobject = t1object; 228803462509SAlan Cox } 228903462509SAlan Cox } 229003462509SAlan Cox 22910951bd36SEric van Gyzen /* 22920951bd36SEric van Gyzen * Return the vnode for the given object, or NULL if none exists. 22930951bd36SEric van Gyzen * For tmpfs objects, the function may return NULL if there is 22940951bd36SEric van Gyzen * no vnode allocated at the time of the call. 22950951bd36SEric van Gyzen */ 229663e4c6cdSEric van Gyzen struct vnode * 229763e4c6cdSEric van Gyzen vm_object_vnode(vm_object_t object) 229863e4c6cdSEric van Gyzen { 22990951bd36SEric van Gyzen struct vnode *vp; 230063e4c6cdSEric van Gyzen 230163e4c6cdSEric van Gyzen VM_OBJECT_ASSERT_LOCKED(object); 23020951bd36SEric van Gyzen if (object->type == OBJT_VNODE) { 23030951bd36SEric van Gyzen vp = object->handle; 23040951bd36SEric van Gyzen KASSERT(vp != NULL, ("%s: OBJT_VNODE has no vnode", __func__)); 23050951bd36SEric van Gyzen } else if (object->type == OBJT_SWAP && 23060951bd36SEric van Gyzen (object->flags & OBJ_TMPFS) != 0) { 23070951bd36SEric van Gyzen vp = object->un_pager.swp.swp_tmpfs; 23080951bd36SEric van Gyzen KASSERT(vp != NULL, ("%s: OBJT_TMPFS has no vnode", __func__)); 23090951bd36SEric van Gyzen } else { 23100951bd36SEric van Gyzen vp = NULL; 23110951bd36SEric van Gyzen } 23120951bd36SEric van Gyzen return (vp); 231363e4c6cdSEric van Gyzen } 231463e4c6cdSEric van Gyzen 2315205be21dSJeff Roberson 2316205be21dSJeff Roberson /* 2317205be21dSJeff Roberson * Busy the vm object. This prevents new pages belonging to the object from 2318205be21dSJeff Roberson * becoming busy. Existing pages persist as busy. Callers are responsible 2319205be21dSJeff Roberson * for checking page state before proceeding. 2320205be21dSJeff Roberson */ 2321205be21dSJeff Roberson void 2322205be21dSJeff Roberson vm_object_busy(vm_object_t obj) 2323205be21dSJeff Roberson { 2324205be21dSJeff Roberson 2325205be21dSJeff Roberson VM_OBJECT_ASSERT_LOCKED(obj); 2326205be21dSJeff Roberson 2327205be21dSJeff Roberson refcount_acquire(&obj->busy); 2328205be21dSJeff Roberson /* The fence is required to order loads of page busy. */ 2329205be21dSJeff Roberson atomic_thread_fence_acq_rel(); 2330205be21dSJeff Roberson } 2331205be21dSJeff Roberson 2332205be21dSJeff Roberson void 2333205be21dSJeff Roberson vm_object_unbusy(vm_object_t obj) 2334205be21dSJeff Roberson { 2335205be21dSJeff Roberson 2336205be21dSJeff Roberson 2337205be21dSJeff Roberson refcount_release(&obj->busy); 2338205be21dSJeff Roberson } 2339205be21dSJeff Roberson 2340205be21dSJeff Roberson void 2341205be21dSJeff Roberson vm_object_busy_wait(vm_object_t obj, const char *wmesg) 2342205be21dSJeff Roberson { 2343205be21dSJeff Roberson 2344205be21dSJeff Roberson VM_OBJECT_ASSERT_UNLOCKED(obj); 2345205be21dSJeff Roberson 2346205be21dSJeff Roberson if (obj->busy) 2347205be21dSJeff Roberson refcount_sleep(&obj->busy, wmesg, PVM); 2348205be21dSJeff Roberson } 2349205be21dSJeff Roberson 23505e38e3f5SEric van Gyzen /* 23515e38e3f5SEric van Gyzen * Return the kvme type of the given object. 23525e38e3f5SEric van Gyzen * If vpp is not NULL, set it to the object's vm_object_vnode() or NULL. 23535e38e3f5SEric van Gyzen */ 23545e38e3f5SEric van Gyzen int 23555e38e3f5SEric van Gyzen vm_object_kvme_type(vm_object_t object, struct vnode **vpp) 23565e38e3f5SEric van Gyzen { 23575e38e3f5SEric van Gyzen 23585e38e3f5SEric van Gyzen VM_OBJECT_ASSERT_LOCKED(object); 23595e38e3f5SEric van Gyzen if (vpp != NULL) 23605e38e3f5SEric van Gyzen *vpp = vm_object_vnode(object); 23615e38e3f5SEric van Gyzen switch (object->type) { 23625e38e3f5SEric van Gyzen case OBJT_DEFAULT: 23635e38e3f5SEric van Gyzen return (KVME_TYPE_DEFAULT); 23645e38e3f5SEric van Gyzen case OBJT_VNODE: 23655e38e3f5SEric van Gyzen return (KVME_TYPE_VNODE); 23665e38e3f5SEric van Gyzen case OBJT_SWAP: 23675e38e3f5SEric van Gyzen if ((object->flags & OBJ_TMPFS_NODE) != 0) 23685e38e3f5SEric van Gyzen return (KVME_TYPE_VNODE); 23695e38e3f5SEric van Gyzen return (KVME_TYPE_SWAP); 23705e38e3f5SEric van Gyzen case OBJT_DEVICE: 23715e38e3f5SEric van Gyzen return (KVME_TYPE_DEVICE); 23725e38e3f5SEric van Gyzen case OBJT_PHYS: 23735e38e3f5SEric van Gyzen return (KVME_TYPE_PHYS); 23745e38e3f5SEric van Gyzen case OBJT_DEAD: 23755e38e3f5SEric van Gyzen return (KVME_TYPE_DEAD); 23765e38e3f5SEric van Gyzen case OBJT_SG: 23775e38e3f5SEric van Gyzen return (KVME_TYPE_SG); 23785e38e3f5SEric van Gyzen case OBJT_MGTDEVICE: 23795e38e3f5SEric van Gyzen return (KVME_TYPE_MGTDEVICE); 23805e38e3f5SEric van Gyzen default: 23815e38e3f5SEric van Gyzen return (KVME_TYPE_UNKNOWN); 23825e38e3f5SEric van Gyzen } 23835e38e3f5SEric van Gyzen } 23845e38e3f5SEric van Gyzen 2385ff87ae35SJohn Baldwin static int 2386ff87ae35SJohn Baldwin sysctl_vm_object_list(SYSCTL_HANDLER_ARGS) 2387ff87ae35SJohn Baldwin { 23880ecee546SKonstantin Belousov struct kinfo_vmobject *kvo; 2389ff87ae35SJohn Baldwin char *fullpath, *freepath; 2390ff87ae35SJohn Baldwin struct vnode *vp; 2391ff87ae35SJohn Baldwin struct vattr va; 2392ff87ae35SJohn Baldwin vm_object_t obj; 2393ff87ae35SJohn Baldwin vm_page_t m; 2394ff87ae35SJohn Baldwin int count, error; 2395ff87ae35SJohn Baldwin 2396ff87ae35SJohn Baldwin if (req->oldptr == NULL) { 2397ff87ae35SJohn Baldwin /* 2398ff87ae35SJohn Baldwin * If an old buffer has not been provided, generate an 2399ff87ae35SJohn Baldwin * estimate of the space needed for a subsequent call. 2400ff87ae35SJohn Baldwin */ 2401ff87ae35SJohn Baldwin mtx_lock(&vm_object_list_mtx); 2402ff87ae35SJohn Baldwin count = 0; 2403ff87ae35SJohn Baldwin TAILQ_FOREACH(obj, &vm_object_list, object_list) { 2404ff87ae35SJohn Baldwin if (obj->type == OBJT_DEAD) 2405ff87ae35SJohn Baldwin continue; 2406ff87ae35SJohn Baldwin count++; 2407ff87ae35SJohn Baldwin } 2408ff87ae35SJohn Baldwin mtx_unlock(&vm_object_list_mtx); 2409ff87ae35SJohn Baldwin return (SYSCTL_OUT(req, NULL, sizeof(struct kinfo_vmobject) * 2410ff87ae35SJohn Baldwin count * 11 / 10)); 2411ff87ae35SJohn Baldwin } 2412ff87ae35SJohn Baldwin 24130ecee546SKonstantin Belousov kvo = malloc(sizeof(*kvo), M_TEMP, M_WAITOK); 2414ff87ae35SJohn Baldwin error = 0; 2415ff87ae35SJohn Baldwin 2416ff87ae35SJohn Baldwin /* 2417ff87ae35SJohn Baldwin * VM objects are type stable and are never removed from the 2418ff87ae35SJohn Baldwin * list once added. This allows us to safely read obj->object_list 2419ff87ae35SJohn Baldwin * after reacquiring the VM object lock. 2420ff87ae35SJohn Baldwin */ 2421ff87ae35SJohn Baldwin mtx_lock(&vm_object_list_mtx); 2422ff87ae35SJohn Baldwin TAILQ_FOREACH(obj, &vm_object_list, object_list) { 2423ff87ae35SJohn Baldwin if (obj->type == OBJT_DEAD) 2424ff87ae35SJohn Baldwin continue; 2425ff87ae35SJohn Baldwin VM_OBJECT_RLOCK(obj); 2426ff87ae35SJohn Baldwin if (obj->type == OBJT_DEAD) { 2427ff87ae35SJohn Baldwin VM_OBJECT_RUNLOCK(obj); 2428ff87ae35SJohn Baldwin continue; 2429ff87ae35SJohn Baldwin } 2430ff87ae35SJohn Baldwin mtx_unlock(&vm_object_list_mtx); 24310ecee546SKonstantin Belousov kvo->kvo_size = ptoa(obj->size); 24320ecee546SKonstantin Belousov kvo->kvo_resident = obj->resident_page_count; 24330ecee546SKonstantin Belousov kvo->kvo_ref_count = obj->ref_count; 24340ecee546SKonstantin Belousov kvo->kvo_shadow_count = obj->shadow_count; 24350ecee546SKonstantin Belousov kvo->kvo_memattr = obj->memattr; 24360ecee546SKonstantin Belousov kvo->kvo_active = 0; 24370ecee546SKonstantin Belousov kvo->kvo_inactive = 0; 2438ff87ae35SJohn Baldwin TAILQ_FOREACH(m, &obj->memq, listq) { 2439ff87ae35SJohn Baldwin /* 2440ff87ae35SJohn Baldwin * A page may belong to the object but be 2441ff87ae35SJohn Baldwin * dequeued and set to PQ_NONE while the 2442ff87ae35SJohn Baldwin * object lock is not held. This makes the 2443ff87ae35SJohn Baldwin * reads of m->queue below racy, and we do not 2444ff87ae35SJohn Baldwin * count pages set to PQ_NONE. However, this 2445ff87ae35SJohn Baldwin * sysctl is only meant to give an 2446ff87ae35SJohn Baldwin * approximation of the system anyway. 2447ff87ae35SJohn Baldwin */ 2448e8bcf696SMark Johnston if (m->queue == PQ_ACTIVE) 24490ecee546SKonstantin Belousov kvo->kvo_active++; 2450e8bcf696SMark Johnston else if (m->queue == PQ_INACTIVE) 24510ecee546SKonstantin Belousov kvo->kvo_inactive++; 2452ff87ae35SJohn Baldwin } 2453ff87ae35SJohn Baldwin 24540ecee546SKonstantin Belousov kvo->kvo_vn_fileid = 0; 24550ecee546SKonstantin Belousov kvo->kvo_vn_fsid = 0; 24560ecee546SKonstantin Belousov kvo->kvo_vn_fsid_freebsd11 = 0; 2457ff87ae35SJohn Baldwin freepath = NULL; 2458ff87ae35SJohn Baldwin fullpath = ""; 24595e38e3f5SEric van Gyzen kvo->kvo_type = vm_object_kvme_type(obj, &vp); 24605e38e3f5SEric van Gyzen if (vp != NULL) 2461ff87ae35SJohn Baldwin vref(vp); 2462ff87ae35SJohn Baldwin VM_OBJECT_RUNLOCK(obj); 2463ff87ae35SJohn Baldwin if (vp != NULL) { 2464ff87ae35SJohn Baldwin vn_fullpath(curthread, vp, &fullpath, &freepath); 2465ff87ae35SJohn Baldwin vn_lock(vp, LK_SHARED | LK_RETRY); 2466ff87ae35SJohn Baldwin if (VOP_GETATTR(vp, &va, curthread->td_ucred) == 0) { 24670ecee546SKonstantin Belousov kvo->kvo_vn_fileid = va.va_fileid; 24680ecee546SKonstantin Belousov kvo->kvo_vn_fsid = va.va_fsid; 24690ecee546SKonstantin Belousov kvo->kvo_vn_fsid_freebsd11 = va.va_fsid; 247069921123SKonstantin Belousov /* truncate */ 2471ff87ae35SJohn Baldwin } 2472ff87ae35SJohn Baldwin vput(vp); 2473ff87ae35SJohn Baldwin } 2474ff87ae35SJohn Baldwin 24750ecee546SKonstantin Belousov strlcpy(kvo->kvo_path, fullpath, sizeof(kvo->kvo_path)); 2476ff87ae35SJohn Baldwin if (freepath != NULL) 2477ff87ae35SJohn Baldwin free(freepath, M_TEMP); 2478ff87ae35SJohn Baldwin 2479ff87ae35SJohn Baldwin /* Pack record size down */ 24800ecee546SKonstantin Belousov kvo->kvo_structsize = offsetof(struct kinfo_vmobject, kvo_path) 24810ecee546SKonstantin Belousov + strlen(kvo->kvo_path) + 1; 24820ecee546SKonstantin Belousov kvo->kvo_structsize = roundup(kvo->kvo_structsize, 2483ff87ae35SJohn Baldwin sizeof(uint64_t)); 24840ecee546SKonstantin Belousov error = SYSCTL_OUT(req, kvo, kvo->kvo_structsize); 2485ff87ae35SJohn Baldwin mtx_lock(&vm_object_list_mtx); 2486ff87ae35SJohn Baldwin if (error) 2487ff87ae35SJohn Baldwin break; 2488ff87ae35SJohn Baldwin } 2489ff87ae35SJohn Baldwin mtx_unlock(&vm_object_list_mtx); 24900ecee546SKonstantin Belousov free(kvo, M_TEMP); 2491ff87ae35SJohn Baldwin return (error); 2492ff87ae35SJohn Baldwin } 2493ff87ae35SJohn Baldwin SYSCTL_PROC(_vm, OID_AUTO, objects, CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_SKIP | 2494ff87ae35SJohn Baldwin CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_object_list, "S,kinfo_vmobject", 2495ff87ae35SJohn Baldwin "List of VM objects"); 2496ff87ae35SJohn Baldwin 2497c7c34a24SBruce Evans #include "opt_ddb.h" 2498c3cb3e12SDavid Greenman #ifdef DDB 2499c7c34a24SBruce Evans #include <sys/kernel.h> 2500c7c34a24SBruce Evans 2501ce9edcf5SPoul-Henning Kamp #include <sys/cons.h> 2502c7c34a24SBruce Evans 2503c7c34a24SBruce Evans #include <ddb/ddb.h> 2504c7c34a24SBruce Evans 2505cac597e4SBruce Evans static int 25061b40f8c0SMatthew Dillon _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry) 2507a1f6d91cSDavid Greenman { 2508a1f6d91cSDavid Greenman vm_map_t tmpm; 2509a1f6d91cSDavid Greenman vm_map_entry_t tmpe; 2510a1f6d91cSDavid Greenman vm_object_t obj; 2511a1f6d91cSDavid Greenman 2512a1f6d91cSDavid Greenman if (map == 0) 2513a1f6d91cSDavid Greenman return 0; 2514a1f6d91cSDavid Greenman 2515a1f6d91cSDavid Greenman if (entry == 0) { 25162288078cSDoug Moore VM_MAP_ENTRY_FOREACH(tmpe, map) { 2517a1f6d91cSDavid Greenman if (_vm_object_in_map(map, object, tmpe)) { 2518a1f6d91cSDavid Greenman return 1; 2519a1f6d91cSDavid Greenman } 2520a1f6d91cSDavid Greenman } 25219fdfe602SMatthew Dillon } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 25229fdfe602SMatthew Dillon tmpm = entry->object.sub_map; 25232288078cSDoug Moore VM_MAP_ENTRY_FOREACH(tmpe, tmpm) { 2524a1f6d91cSDavid Greenman if (_vm_object_in_map(tmpm, object, tmpe)) { 2525a1f6d91cSDavid Greenman return 1; 2526a1f6d91cSDavid Greenman } 2527a1f6d91cSDavid Greenman } 25288aef1712SMatthew Dillon } else if ((obj = entry->object.vm_object) != NULL) { 252924a1cce3SDavid Greenman for (; obj; obj = obj->backing_object) 2530a1f6d91cSDavid Greenman if (obj == object) { 2531a1f6d91cSDavid Greenman return 1; 2532a1f6d91cSDavid Greenman } 2533a1f6d91cSDavid Greenman } 2534a1f6d91cSDavid Greenman return 0; 2535a1f6d91cSDavid Greenman } 2536a1f6d91cSDavid Greenman 2537cac597e4SBruce Evans static int 25381b40f8c0SMatthew Dillon vm_object_in_map(vm_object_t object) 2539a1f6d91cSDavid Greenman { 2540a1f6d91cSDavid Greenman struct proc *p; 25411005a129SJohn Baldwin 254260517fd1SJohn Baldwin /* sx_slock(&allproc_lock); */ 2543f67af5c9SXin LI FOREACH_PROC_IN_SYSTEM(p) { 2544a1f6d91cSDavid Greenman if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */) 2545a1f6d91cSDavid Greenman continue; 2546553629ebSJake Burkholder if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) { 254760517fd1SJohn Baldwin /* sx_sunlock(&allproc_lock); */ 2548a1f6d91cSDavid Greenman return 1; 2549a1f6d91cSDavid Greenman } 2550553629ebSJake Burkholder } 255160517fd1SJohn Baldwin /* sx_sunlock(&allproc_lock); */ 2552a1f6d91cSDavid Greenman if (_vm_object_in_map(kernel_map, object, 0)) 2553a1f6d91cSDavid Greenman return 1; 2554a1f6d91cSDavid Greenman return 0; 2555a1f6d91cSDavid Greenman } 2556a1f6d91cSDavid Greenman 2557c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check) 2558f708ef1bSPoul-Henning Kamp { 2559a1f6d91cSDavid Greenman vm_object_t object; 2560a1f6d91cSDavid Greenman 2561a1f6d91cSDavid Greenman /* 2562a1f6d91cSDavid Greenman * make sure that internal objs are in a map somewhere 2563a1f6d91cSDavid Greenman * and none have zero ref counts. 2564a1f6d91cSDavid Greenman */ 2565cc64b484SAlfred Perlstein TAILQ_FOREACH(object, &vm_object_list, object_list) { 256632362449SKonstantin Belousov if ((object->flags & OBJ_ANON) != 0) { 2567a1f6d91cSDavid Greenman if (object->ref_count == 0) { 25683efc015bSPeter Wemm db_printf("vmochk: internal obj has zero ref count: %ld\n", 25693efc015bSPeter Wemm (long)object->size); 2570a1f6d91cSDavid Greenman } 2571a1f6d91cSDavid Greenman if (!vm_object_in_map(object)) { 2572fc62ef1fSBruce Evans db_printf( 2573fc62ef1fSBruce Evans "vmochk: internal obj is not in a map: " 2574fc62ef1fSBruce Evans "ref: %d, size: %lu: 0x%lx, backing_object: %p\n", 2575fc62ef1fSBruce Evans object->ref_count, (u_long)object->size, 2576fc62ef1fSBruce Evans (u_long)object->size, 2577fc62ef1fSBruce Evans (void *)object->backing_object); 2578a1f6d91cSDavid Greenman } 2579a1f6d91cSDavid Greenman } 2580a1f6d91cSDavid Greenman } 2581a1f6d91cSDavid Greenman } 2582a1f6d91cSDavid Greenman 258326f9a767SRodney W. Grimes /* 2584df8bae1dSRodney W. Grimes * vm_object_print: [ debug ] 2585df8bae1dSRodney W. Grimes */ 2586c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static) 2587df8bae1dSRodney W. Grimes { 2588c7c34a24SBruce Evans /* XXX convert args. */ 2589c7c34a24SBruce Evans vm_object_t object = (vm_object_t)addr; 2590c7c34a24SBruce Evans boolean_t full = have_addr; 2591c7c34a24SBruce Evans 2592d031cff1SMatthew Dillon vm_page_t p; 2593df8bae1dSRodney W. Grimes 2594c7c34a24SBruce Evans /* XXX count is an (unused) arg. Avoid shadowing it. */ 2595c7c34a24SBruce Evans #define count was_count 2596c7c34a24SBruce Evans 2597d031cff1SMatthew Dillon int count; 2598df8bae1dSRodney W. Grimes 2599df8bae1dSRodney W. Grimes if (object == NULL) 2600df8bae1dSRodney W. Grimes return; 2601df8bae1dSRodney W. Grimes 2602eb95adefSBruce Evans db_iprintf( 2603ef694c1aSEdward Tomasz Napierala "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x ruid %d charge %jx\n", 2604e47cd172SMaxime Henrion object, (int)object->type, (uintmax_t)object->size, 26053364c323SKonstantin Belousov object->resident_page_count, object->ref_count, object->flags, 2606ef694c1aSEdward Tomasz Napierala object->cred ? object->cred->cr_ruid : -1, (uintmax_t)object->charge); 2607e47cd172SMaxime Henrion db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n", 26081c7c3c6aSMatthew Dillon object->shadow_count, 2609eb95adefSBruce Evans object->backing_object ? object->backing_object->ref_count : 0, 2610e47cd172SMaxime Henrion object->backing_object, (uintmax_t)object->backing_object_offset); 2611df8bae1dSRodney W. Grimes 2612df8bae1dSRodney W. Grimes if (!full) 2613df8bae1dSRodney W. Grimes return; 2614df8bae1dSRodney W. Grimes 2615c7c34a24SBruce Evans db_indent += 2; 2616df8bae1dSRodney W. Grimes count = 0; 2617fc2ffbe6SPoul-Henning Kamp TAILQ_FOREACH(p, &object->memq, listq) { 2618df8bae1dSRodney W. Grimes if (count == 0) 2619c7c34a24SBruce Evans db_iprintf("memory:="); 2620df8bae1dSRodney W. Grimes else if (count == 6) { 2621c7c34a24SBruce Evans db_printf("\n"); 2622c7c34a24SBruce Evans db_iprintf(" ..."); 2623df8bae1dSRodney W. Grimes count = 0; 2624df8bae1dSRodney W. Grimes } else 2625c7c34a24SBruce Evans db_printf(","); 2626df8bae1dSRodney W. Grimes count++; 2627df8bae1dSRodney W. Grimes 2628e47cd172SMaxime Henrion db_printf("(off=0x%jx,page=0x%jx)", 2629e47cd172SMaxime Henrion (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p)); 2630df8bae1dSRodney W. Grimes } 2631df8bae1dSRodney W. Grimes if (count != 0) 2632c7c34a24SBruce Evans db_printf("\n"); 2633c7c34a24SBruce Evans db_indent -= 2; 2634df8bae1dSRodney W. Grimes } 26355070c7f8SJohn Dyson 2636c7c34a24SBruce Evans /* XXX. */ 2637c7c34a24SBruce Evans #undef count 2638c7c34a24SBruce Evans 2639c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */ 26405070c7f8SJohn Dyson void 26411b40f8c0SMatthew Dillon vm_object_print( 26421b40f8c0SMatthew Dillon /* db_expr_t */ long addr, 26431b40f8c0SMatthew Dillon boolean_t have_addr, 26441b40f8c0SMatthew Dillon /* db_expr_t */ long count, 26451b40f8c0SMatthew Dillon char *modif) 2646c7c34a24SBruce Evans { 2647c7c34a24SBruce Evans vm_object_print_static(addr, have_addr, count, modif); 2648c7c34a24SBruce Evans } 2649c7c34a24SBruce Evans 2650c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages) 26515070c7f8SJohn Dyson { 26525070c7f8SJohn Dyson vm_object_t object; 2653bb2ac86fSKonstantin Belousov vm_pindex_t fidx; 2654bb2ac86fSKonstantin Belousov vm_paddr_t pa; 2655bb2ac86fSKonstantin Belousov vm_page_t m, prev_m; 2656bb2ac86fSKonstantin Belousov int rcount, nl, c; 2657cc64b484SAlfred Perlstein 2658bb2ac86fSKonstantin Belousov nl = 0; 2659cc64b484SAlfred Perlstein TAILQ_FOREACH(object, &vm_object_list, object_list) { 2660fc62ef1fSBruce Evans db_printf("new object: %p\n", (void *)object); 26615070c7f8SJohn Dyson if (nl > 18) { 26625070c7f8SJohn Dyson c = cngetc(); 26635070c7f8SJohn Dyson if (c != ' ') 26645070c7f8SJohn Dyson return; 26655070c7f8SJohn Dyson nl = 0; 26665070c7f8SJohn Dyson } 26675070c7f8SJohn Dyson nl++; 26685070c7f8SJohn Dyson rcount = 0; 26695070c7f8SJohn Dyson fidx = 0; 2670bb2ac86fSKonstantin Belousov pa = -1; 2671bb2ac86fSKonstantin Belousov TAILQ_FOREACH(m, &object->memq, listq) { 2672bb2ac86fSKonstantin Belousov if (m->pindex > 128) 2673bb2ac86fSKonstantin Belousov break; 2674bb2ac86fSKonstantin Belousov if ((prev_m = TAILQ_PREV(m, pglist, listq)) != NULL && 2675bb2ac86fSKonstantin Belousov prev_m->pindex + 1 != m->pindex) { 26765070c7f8SJohn Dyson if (rcount) { 26773efc015bSPeter Wemm db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 26783efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 26795070c7f8SJohn Dyson if (nl > 18) { 26805070c7f8SJohn Dyson c = cngetc(); 26815070c7f8SJohn Dyson if (c != ' ') 26825070c7f8SJohn Dyson return; 26835070c7f8SJohn Dyson nl = 0; 26845070c7f8SJohn Dyson } 26855070c7f8SJohn Dyson nl++; 26865070c7f8SJohn Dyson rcount = 0; 26875070c7f8SJohn Dyson } 26885070c7f8SJohn Dyson } 26895070c7f8SJohn Dyson if (rcount && 26905070c7f8SJohn Dyson (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) { 26915070c7f8SJohn Dyson ++rcount; 26925070c7f8SJohn Dyson continue; 26935070c7f8SJohn Dyson } 26945070c7f8SJohn Dyson if (rcount) { 26952446e4f0SAlan Cox db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 26963efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 26975070c7f8SJohn Dyson if (nl > 18) { 26985070c7f8SJohn Dyson c = cngetc(); 26995070c7f8SJohn Dyson if (c != ' ') 27005070c7f8SJohn Dyson return; 27015070c7f8SJohn Dyson nl = 0; 27025070c7f8SJohn Dyson } 27035070c7f8SJohn Dyson nl++; 27045070c7f8SJohn Dyson } 2705bb2ac86fSKonstantin Belousov fidx = m->pindex; 27065070c7f8SJohn Dyson pa = VM_PAGE_TO_PHYS(m); 27075070c7f8SJohn Dyson rcount = 1; 27085070c7f8SJohn Dyson } 27095070c7f8SJohn Dyson if (rcount) { 27103efc015bSPeter Wemm db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 27113efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 27125070c7f8SJohn Dyson if (nl > 18) { 27135070c7f8SJohn Dyson c = cngetc(); 27145070c7f8SJohn Dyson if (c != ' ') 27155070c7f8SJohn Dyson return; 27165070c7f8SJohn Dyson nl = 0; 27175070c7f8SJohn Dyson } 27185070c7f8SJohn Dyson nl++; 27195070c7f8SJohn Dyson } 27205070c7f8SJohn Dyson } 27215070c7f8SJohn Dyson } 2722c3cb3e12SDavid Greenman #endif /* DDB */ 2723