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 { 471df8bae1dSRodney W. Grimes if (object == NULL) 472df8bae1dSRodney W. Grimes return; 47351df5321SJeff Roberson VM_OBJECT_RLOCK(object); 47452481a9aSJeff Roberson vm_object_reference_locked(object); 47551df5321SJeff Roberson VM_OBJECT_RUNLOCK(object); 47695e5e988SJohn Dyson } 47795e5e988SJohn Dyson 47823955314SAlfred Perlstein /* 479b921a12bSAlan Cox * vm_object_reference_locked: 480b921a12bSAlan Cox * 481b921a12bSAlan Cox * Gets another reference to the given object. 482b921a12bSAlan Cox * 483b921a12bSAlan Cox * The object must be locked. 484b921a12bSAlan Cox */ 485b921a12bSAlan Cox void 486b921a12bSAlan Cox vm_object_reference_locked(vm_object_t object) 487b921a12bSAlan Cox { 488b921a12bSAlan Cox struct vnode *vp; 489b921a12bSAlan Cox 49051df5321SJeff Roberson VM_OBJECT_ASSERT_LOCKED(object); 49151df5321SJeff Roberson refcount_acquire(&object->ref_count); 492b921a12bSAlan Cox if (object->type == OBJT_VNODE) { 493b921a12bSAlan Cox vp = object->handle; 494b921a12bSAlan Cox vref(vp); 495b921a12bSAlan Cox } 496b921a12bSAlan Cox } 497b921a12bSAlan Cox 498b921a12bSAlan Cox /* 4999d5abbddSJens Schweikhardt * Handle deallocating an object of type OBJT_VNODE. 50023955314SAlfred Perlstein */ 50102dd8331SAlan Cox static void 5021b40f8c0SMatthew Dillon vm_object_vndeallocate(vm_object_t object) 50395e5e988SJohn Dyson { 50495e5e988SJohn Dyson struct vnode *vp = (struct vnode *) object->handle; 505219cbf59SEivind Eklund 5065526d2d9SEivind Eklund KASSERT(object->type == OBJT_VNODE, 5075526d2d9SEivind Eklund ("vm_object_vndeallocate: not a vnode object")); 508219cbf59SEivind Eklund KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp")); 50995e5e988SJohn Dyson 51051df5321SJeff Roberson if (refcount_release(&object->ref_count) && 51151df5321SJeff Roberson !umtx_shm_vnobj_persistent) 5121bdbd705SKonstantin Belousov umtx_shm_object_terminated(object); 5131bdbd705SKonstantin Belousov 51451df5321SJeff Roberson VM_OBJECT_RUNLOCK(object); 51503fa5b34SKonstantin Belousov /* vrele may need the vnode lock. */ 51647221757SJohn Dyson vrele(vp); 51786769ac0SKonstantin Belousov } 518df8bae1dSRodney W. Grimes 519df8bae1dSRodney W. Grimes /* 520df8bae1dSRodney W. Grimes * vm_object_deallocate: 521df8bae1dSRodney W. Grimes * 522df8bae1dSRodney W. Grimes * Release a reference to the specified object, 523df8bae1dSRodney W. Grimes * gained either through a vm_object_allocate 524df8bae1dSRodney W. Grimes * or a vm_object_reference call. When all references 525df8bae1dSRodney W. Grimes * are gone, storage associated with this object 526df8bae1dSRodney W. Grimes * may be relinquished. 527df8bae1dSRodney W. Grimes * 528df8bae1dSRodney W. Grimes * No object may be locked. 529df8bae1dSRodney W. Grimes */ 53026f9a767SRodney W. Grimes void 5311b40f8c0SMatthew Dillon vm_object_deallocate(vm_object_t object) 532df8bae1dSRodney W. Grimes { 53332362449SKonstantin Belousov vm_object_t robject, temp; 53451df5321SJeff Roberson bool released; 535df8bae1dSRodney W. Grimes 536df8bae1dSRodney W. Grimes while (object != NULL) { 53751df5321SJeff Roberson VM_OBJECT_RLOCK(object); 5383b68228cSAlan Cox if (object->type == OBJT_VNODE) { 53995e5e988SJohn Dyson vm_object_vndeallocate(object); 54023b186d3SAlan Cox return; 5415050aa86SKonstantin Belousov } 54295e5e988SJohn Dyson 54351df5321SJeff Roberson /* 54451df5321SJeff Roberson * If the reference count goes to 0 we start calling 54551df5321SJeff Roberson * vm_object_terminate() on the object chain. A ref count 54651df5321SJeff Roberson * of 1 may be a special case depending on the shadow count 54751df5321SJeff Roberson * being 0 or 1. These cases require a write lock on the 54851df5321SJeff Roberson * object. 54951df5321SJeff Roberson */ 55063967687SJeff Roberson if ((object->flags & OBJ_ANON) == 0) 55163967687SJeff Roberson released = refcount_release_if_gt(&object->ref_count, 1); 55263967687SJeff Roberson else 55351df5321SJeff Roberson released = refcount_release_if_gt(&object->ref_count, 2); 55451df5321SJeff Roberson VM_OBJECT_RUNLOCK(object); 55551df5321SJeff Roberson if (released) 55651df5321SJeff Roberson return; 55751df5321SJeff Roberson 55851df5321SJeff Roberson VM_OBJECT_WLOCK(object); 5598125b1e6SAlfred Perlstein KASSERT(object->ref_count != 0, 5608125b1e6SAlfred Perlstein ("vm_object_deallocate: object deallocated too many times: %d", object->type)); 5612be70f79SJohn Dyson 56251df5321SJeff Roberson refcount_release(&object->ref_count); 5638125b1e6SAlfred Perlstein if (object->ref_count > 1) { 56489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 56523b186d3SAlan Cox return; 5668125b1e6SAlfred Perlstein } else if (object->ref_count == 1) { 5674c8e0452SAlan Cox if (object->shadow_count == 0 && 56863967687SJeff Roberson (object->flags & OBJ_ANON) != 0) { 5698125b1e6SAlfred Perlstein vm_object_set_flag(object, OBJ_ONEMAPPING); 57032362449SKonstantin Belousov } else if (object->shadow_count == 1) { 57132362449SKonstantin Belousov KASSERT((object->flags & OBJ_ANON) != 0, 57232362449SKonstantin Belousov ("obj %p with shadow_count > 0 is not anon", 57332362449SKonstantin Belousov object)); 5741c500307SAlan Cox robject = LIST_FIRST(&object->shadow_head); 5755526d2d9SEivind Eklund KASSERT(robject != NULL, 57632362449SKonstantin Belousov ("vm_object_deallocate: ref_count: %d, " 57732362449SKonstantin Belousov "shadow_count: %d", object->ref_count, 5785526d2d9SEivind Eklund object->shadow_count)); 5794bace8e7SKonstantin Belousov KASSERT((robject->flags & OBJ_TMPFS_NODE) == 0, 5804bace8e7SKonstantin Belousov ("shadowed tmpfs v_object %p", object)); 58189f6b863SAttilio Rao if (!VM_OBJECT_TRYWLOCK(robject)) { 582b72b0115SAlan Cox /* 583b72b0115SAlan Cox * Avoid a potential deadlock. 584b72b0115SAlan Cox */ 58551df5321SJeff Roberson refcount_acquire(&object->ref_count); 58689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 587a7d86121SAlan Cox /* 588a7d86121SAlan Cox * More likely than not the thread 589a7d86121SAlan Cox * holding robject's lock has lower 590a7d86121SAlan Cox * priority than the current thread. 591a7d86121SAlan Cox * Let the lower priority thread run. 592a7d86121SAlan Cox */ 5938db5fc58SJohn Baldwin pause("vmo_de", 1); 594b72b0115SAlan Cox continue; 595b72b0115SAlan Cox } 596d936694fSAlan Cox /* 597d936694fSAlan Cox * Collapse object into its shadow unless its 598d936694fSAlan Cox * shadow is dead. In that case, object will 599d936694fSAlan Cox * be deallocated by the thread that is 600d936694fSAlan Cox * deallocating its shadow. 601d936694fSAlan Cox */ 60263967687SJeff Roberson if ((robject->flags & 60332362449SKonstantin Belousov (OBJ_DEAD | OBJ_ANON)) == OBJ_ANON) { 604a1f6d91cSDavid Greenman 60551df5321SJeff Roberson refcount_acquire(&robject->ref_count); 606138449dcSAlan Cox retry: 60711b57401SHans Petter Selasky if (REFCOUNT_COUNT(robject->paging_in_progress) > 0) { 60889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 609138449dcSAlan Cox vm_object_pip_wait(robject, 610138449dcSAlan Cox "objde1"); 6112e9f4a69SAlan Cox temp = robject->backing_object; 6122e9f4a69SAlan Cox if (object == temp) { 61389f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 614138449dcSAlan Cox goto retry; 6152e9f4a69SAlan Cox } 61611b57401SHans Petter Selasky } else if (REFCOUNT_COUNT(object->paging_in_progress) > 0) { 61789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(robject); 618cf27e0d1SJeff Roberson VM_OBJECT_WUNLOCK(object); 619cf27e0d1SJeff Roberson refcount_wait( 620cf27e0d1SJeff Roberson &object->paging_in_progress, 621cf27e0d1SJeff Roberson "objde2", PVM); 62289f6b863SAttilio Rao VM_OBJECT_WLOCK(robject); 6232e9f4a69SAlan Cox temp = robject->backing_object; 6242e9f4a69SAlan Cox if (object == temp) { 62589f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 626138449dcSAlan Cox goto retry; 627a1f6d91cSDavid Greenman } 6282e9f4a69SAlan Cox } else 62989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 6302e9f4a69SAlan Cox 63195e5e988SJohn Dyson if (robject->ref_count == 1) { 63295e5e988SJohn Dyson robject->ref_count--; 633ba8da839SDavid Greenman object = robject; 63495e5e988SJohn Dyson goto doterm; 63595e5e988SJohn Dyson } 63695e5e988SJohn Dyson object = robject; 63795e5e988SJohn Dyson vm_object_collapse(object); 63889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 639ba8da839SDavid Greenman continue; 640a1f6d91cSDavid Greenman } 64189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(robject); 64295e5e988SJohn Dyson } 64389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 64423b186d3SAlan Cox return; 64595e5e988SJohn Dyson } 64695e5e988SJohn Dyson doterm: 6471bdbd705SKonstantin Belousov umtx_shm_object_terminated(object); 64824a1cce3SDavid Greenman temp = object->backing_object; 649c9917419SAlan Cox if (temp != NULL) { 6504bace8e7SKonstantin Belousov KASSERT((object->flags & OBJ_TMPFS_NODE) == 0, 6514bace8e7SKonstantin Belousov ("shadowed tmpfs v_object 2 %p", object)); 65251b867e5SJeff Roberson vm_object_backing_remove(object); 653de5f6a77SJohn Dyson } 654245df27cSMatthew Dillon /* 655245df27cSMatthew Dillon * Don't double-terminate, we could be in a termination 656245df27cSMatthew Dillon * recursion due to the terminate having to sync data 657245df27cSMatthew Dillon * to disk. 658245df27cSMatthew Dillon */ 659783a68aaSKonstantin Belousov if ((object->flags & OBJ_DEAD) == 0) { 660783a68aaSKonstantin Belousov vm_object_set_flag(object, OBJ_DEAD); 661df8bae1dSRodney W. Grimes vm_object_terminate(object); 662783a68aaSKonstantin Belousov } else 66389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 664df8bae1dSRodney W. Grimes object = temp; 665df8bae1dSRodney W. Grimes } 666df8bae1dSRodney W. Grimes } 667df8bae1dSRodney W. Grimes 668df8bae1dSRodney W. Grimes /* 6692ac78f0eSStephan Uphoff * vm_object_destroy removes the object from the global object list 6702ac78f0eSStephan Uphoff * and frees the space for the object. 6712ac78f0eSStephan Uphoff */ 6722ac78f0eSStephan Uphoff void 6732ac78f0eSStephan Uphoff vm_object_destroy(vm_object_t object) 6742ac78f0eSStephan Uphoff { 6752ac78f0eSStephan Uphoff 6762ac78f0eSStephan Uphoff /* 6773364c323SKonstantin Belousov * Release the allocation charge. 6783364c323SKonstantin Belousov */ 679ef694c1aSEdward Tomasz Napierala if (object->cred != NULL) { 680ef694c1aSEdward Tomasz Napierala swap_release_by_cred(object->charge, object->cred); 6813364c323SKonstantin Belousov object->charge = 0; 682ef694c1aSEdward Tomasz Napierala crfree(object->cred); 683ef694c1aSEdward Tomasz Napierala object->cred = NULL; 6843364c323SKonstantin Belousov } 6853364c323SKonstantin Belousov 6863364c323SKonstantin Belousov /* 6872ac78f0eSStephan Uphoff * Free the space for the object. 6882ac78f0eSStephan Uphoff */ 6892ac78f0eSStephan Uphoff uma_zfree(obj_zone, object); 6902ac78f0eSStephan Uphoff } 6912ac78f0eSStephan Uphoff 69251b867e5SJeff Roberson static void 69351b867e5SJeff Roberson vm_object_backing_remove_locked(vm_object_t object) 69451b867e5SJeff Roberson { 69551b867e5SJeff Roberson vm_object_t backing_object; 69651b867e5SJeff Roberson 69751b867e5SJeff Roberson backing_object = object->backing_object; 69851b867e5SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 69951b867e5SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(backing_object); 70051b867e5SJeff Roberson 70151b867e5SJeff Roberson if ((object->flags & OBJ_SHADOWLIST) != 0) { 70251b867e5SJeff Roberson LIST_REMOVE(object, shadow_list); 70351b867e5SJeff Roberson backing_object->shadow_count--; 70451b867e5SJeff Roberson object->flags &= ~OBJ_SHADOWLIST; 70551b867e5SJeff Roberson } 70651b867e5SJeff Roberson object->backing_object = NULL; 70751b867e5SJeff Roberson } 70851b867e5SJeff Roberson 70951b867e5SJeff Roberson static void 71051b867e5SJeff Roberson vm_object_backing_remove(vm_object_t object) 71151b867e5SJeff Roberson { 71251b867e5SJeff Roberson vm_object_t backing_object; 71351b867e5SJeff Roberson 71451b867e5SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 71551b867e5SJeff Roberson 71651b867e5SJeff Roberson if ((object->flags & OBJ_SHADOWLIST) != 0) { 71751b867e5SJeff Roberson backing_object = object->backing_object; 71851b867e5SJeff Roberson VM_OBJECT_WLOCK(backing_object); 71951b867e5SJeff Roberson vm_object_backing_remove_locked(object); 72051b867e5SJeff Roberson VM_OBJECT_WUNLOCK(backing_object); 72151b867e5SJeff Roberson } else 72251b867e5SJeff Roberson object->backing_object = NULL; 72351b867e5SJeff Roberson } 72451b867e5SJeff Roberson 72551b867e5SJeff Roberson static void 72651b867e5SJeff Roberson vm_object_backing_insert_locked(vm_object_t object, vm_object_t backing_object) 72751b867e5SJeff Roberson { 72851b867e5SJeff Roberson 72951b867e5SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 73051b867e5SJeff Roberson 73151b867e5SJeff Roberson if ((backing_object->flags & OBJ_ANON) != 0) { 73251b867e5SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(backing_object); 73351b867e5SJeff Roberson LIST_INSERT_HEAD(&backing_object->shadow_head, object, 73451b867e5SJeff Roberson shadow_list); 73551b867e5SJeff Roberson backing_object->shadow_count++; 73651b867e5SJeff Roberson object->flags |= OBJ_SHADOWLIST; 73751b867e5SJeff Roberson } 73851b867e5SJeff Roberson object->backing_object = backing_object; 73951b867e5SJeff Roberson } 74051b867e5SJeff Roberson 74151b867e5SJeff Roberson static void 74251b867e5SJeff Roberson vm_object_backing_insert(vm_object_t object, vm_object_t backing_object) 74351b867e5SJeff Roberson { 74451b867e5SJeff Roberson 74551b867e5SJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 74651b867e5SJeff Roberson 74751b867e5SJeff Roberson if ((backing_object->flags & OBJ_ANON) != 0) { 74851b867e5SJeff Roberson VM_OBJECT_WLOCK(backing_object); 74951b867e5SJeff Roberson vm_object_backing_insert_locked(object, backing_object); 75051b867e5SJeff Roberson VM_OBJECT_WUNLOCK(backing_object); 75151b867e5SJeff Roberson } else 75251b867e5SJeff Roberson object->backing_object = backing_object; 75351b867e5SJeff Roberson } 75451b867e5SJeff Roberson 75551b867e5SJeff Roberson 7562ac78f0eSStephan Uphoff /* 7577bbdb843SRuslan Bukin * vm_object_terminate_pages removes any remaining pageable pages 7587bbdb843SRuslan Bukin * from the object and resets the object to an empty state. 7597bbdb843SRuslan Bukin */ 7607bbdb843SRuslan Bukin static void 7617bbdb843SRuslan Bukin vm_object_terminate_pages(vm_object_t object) 7627bbdb843SRuslan Bukin { 7637bbdb843SRuslan Bukin vm_page_t p, p_next; 7647bbdb843SRuslan Bukin 7657bbdb843SRuslan Bukin VM_OBJECT_ASSERT_WLOCKED(object); 7667bbdb843SRuslan Bukin 7677bbdb843SRuslan Bukin /* 7687bbdb843SRuslan Bukin * Free any remaining pageable pages. This also removes them from the 7697bbdb843SRuslan Bukin * paging queues. However, don't free wired pages, just remove them 7707bbdb843SRuslan Bukin * from the object. Rather than incrementally removing each page from 7717bbdb843SRuslan Bukin * the object, the page and object are reset to any empty state. 7727bbdb843SRuslan Bukin */ 7737bbdb843SRuslan Bukin TAILQ_FOREACH_SAFE(p, &object->memq, listq, p_next) { 7747bbdb843SRuslan Bukin vm_page_assert_unbusied(p); 775fee2a2faSMark Johnston KASSERT(p->object == object && 776fee2a2faSMark Johnston (p->ref_count & VPRC_OBJREF) != 0, 777fee2a2faSMark Johnston ("vm_object_terminate_pages: page %p is inconsistent", p)); 778fee2a2faSMark Johnston 7797bbdb843SRuslan Bukin p->object = NULL; 780fee2a2faSMark Johnston if (vm_page_drop(p, VPRC_OBJREF) == VPRC_OBJREF) { 7815cd29d0fSMark Johnston VM_CNT_INC(v_pfree); 7825cd29d0fSMark Johnston vm_page_free(p); 7834074d642SAlan Cox } 784fee2a2faSMark Johnston } 7852fcd1ff6SKonstantin Belousov 7867bbdb843SRuslan Bukin /* 7877bbdb843SRuslan Bukin * If the object contained any pages, then reset it to an empty state. 7887bbdb843SRuslan Bukin * None of the object's fields, including "resident_page_count", were 7897bbdb843SRuslan Bukin * modified by the preceding loop. 7907bbdb843SRuslan Bukin */ 7917bbdb843SRuslan Bukin if (object->resident_page_count != 0) { 7927bbdb843SRuslan Bukin vm_radix_reclaim_allnodes(&object->rtree); 7937bbdb843SRuslan Bukin TAILQ_INIT(&object->memq); 7947bbdb843SRuslan Bukin object->resident_page_count = 0; 7957bbdb843SRuslan Bukin if (object->type == OBJT_VNODE) 7967bbdb843SRuslan Bukin vdrop(object->handle); 7977bbdb843SRuslan Bukin } 7987bbdb843SRuslan Bukin } 7997bbdb843SRuslan Bukin 8007bbdb843SRuslan Bukin /* 801df8bae1dSRodney W. Grimes * vm_object_terminate actually destroys the specified object, freeing 802df8bae1dSRodney W. Grimes * up all previously used resources. 803df8bae1dSRodney W. Grimes * 804df8bae1dSRodney W. Grimes * The object must be locked. 8051c7c3c6aSMatthew Dillon * This routine may block. 806df8bae1dSRodney W. Grimes */ 80795e5e988SJohn Dyson void 8081b40f8c0SMatthew Dillon vm_object_terminate(vm_object_t object) 809df8bae1dSRodney W. Grimes { 81089f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 811783a68aaSKonstantin Belousov KASSERT((object->flags & OBJ_DEAD) != 0, 812783a68aaSKonstantin Belousov ("terminating non-dead obj %p", object)); 813bef608bdSJohn Dyson 814cf27e0d1SJeff Roberson /* 815cf27e0d1SJeff Roberson * wait for the pageout daemon to be done with the object 816cf27e0d1SJeff Roberson */ 817cf27e0d1SJeff Roberson vm_object_pip_wait(object, "objtrm"); 818cf27e0d1SJeff Roberson 81911b57401SHans Petter Selasky KASSERT(!REFCOUNT_COUNT(object->paging_in_progress), 820cf27e0d1SJeff Roberson ("vm_object_terminate: pageout in progress")); 821cf27e0d1SJeff Roberson 822971dd342SAlfred Perlstein KASSERT(object->ref_count == 0, 823971dd342SAlfred Perlstein ("vm_object_terminate: object with references, ref_count=%d", 824971dd342SAlfred Perlstein object->ref_count)); 825996c772fSJohn Dyson 8267bbdb843SRuslan Bukin if ((object->flags & OBJ_PG_DTOR) == 0) 8277bbdb843SRuslan Bukin vm_object_terminate_pages(object); 828bef608bdSJohn Dyson 829f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 830f8a47341SAlan Cox if (__predict_false(!LIST_EMPTY(&object->rvq))) 831f8a47341SAlan Cox vm_reserv_break_all(object); 832f8a47341SAlan Cox #endif 8337bfda801SAlan Cox 834e735691bSJohn Baldwin KASSERT(object->cred == NULL || object->type == OBJT_DEFAULT || 835e735691bSJohn Baldwin object->type == OBJT_SWAP, 836e735691bSJohn Baldwin ("%s: non-swap obj %p has cred", __func__, object)); 837e735691bSJohn Baldwin 8382d8acc0fSJohn Dyson /* 8399fcfb650SDavid Greenman * Let the pager know object is dead. 8409fcfb650SDavid Greenman */ 8419fcfb650SDavid Greenman vm_pager_deallocate(object); 84289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 8439fcfb650SDavid Greenman 8442ac78f0eSStephan Uphoff vm_object_destroy(object); 84547221757SJohn Dyson } 846df8bae1dSRodney W. Grimes 847edf93b25SAlan Cox /* 848edf93b25SAlan Cox * Make the page read-only so that we can clear the object flags. However, if 849edf93b25SAlan Cox * this is a nosync mmap then the object is likely to stay dirty so do not 850edf93b25SAlan Cox * mess with the page and do not clear the object flags. Returns TRUE if the 851edf93b25SAlan Cox * page should be flushed, and FALSE otherwise. 852edf93b25SAlan Cox */ 8533280870dSKonstantin Belousov static boolean_t 85467d0e293SJeff Roberson vm_object_page_remove_write(vm_page_t p, int flags, boolean_t *allclean) 8553280870dSKonstantin Belousov { 8563280870dSKonstantin Belousov 857fff5403fSJeff Roberson vm_page_assert_busied(p); 858fff5403fSJeff Roberson 8593280870dSKonstantin Belousov /* 8603280870dSKonstantin Belousov * If we have been asked to skip nosync pages and this is a 8613280870dSKonstantin Belousov * nosync page, skip it. Note that the object flags were not 8623280870dSKonstantin Belousov * cleared in this case so we do not have to set them. 8633280870dSKonstantin Belousov */ 864fff5403fSJeff Roberson if ((flags & OBJPC_NOSYNC) != 0 && (p->aflags & PGA_NOSYNC) != 0) { 86567d0e293SJeff Roberson *allclean = FALSE; 8663280870dSKonstantin Belousov return (FALSE); 8673280870dSKonstantin Belousov } else { 8683280870dSKonstantin Belousov pmap_remove_write(p); 8693280870dSKonstantin Belousov return (p->dirty != 0); 8703280870dSKonstantin Belousov } 8713280870dSKonstantin Belousov } 8723280870dSKonstantin Belousov 873df8bae1dSRodney W. Grimes /* 874df8bae1dSRodney W. Grimes * vm_object_page_clean 875df8bae1dSRodney W. Grimes * 8764f79d873SMatthew Dillon * Clean all dirty pages in the specified range of object. Leaves page 8774f79d873SMatthew Dillon * on whatever queue it is currently on. If NOSYNC is set then do not 878fff5403fSJeff Roberson * write out pages with PGA_NOSYNC set (originally comes from MAP_NOSYNC), 8794f79d873SMatthew Dillon * leaving the object dirty. 88026f9a767SRodney W. Grimes * 88143b7990eSMatthew Dillon * When stuffing pages asynchronously, allow clustering. XXX we need a 88243b7990eSMatthew Dillon * synchronous clustering mode implementation. 88343b7990eSMatthew Dillon * 88426f9a767SRodney W. Grimes * Odd semantics: if start == end, we clean everything. 88526f9a767SRodney W. Grimes * 88626f9a767SRodney W. Grimes * The object must be locked. 887126d6082SKonstantin Belousov * 888126d6082SKonstantin Belousov * Returns FALSE if some page from the range was not written, as 889126d6082SKonstantin Belousov * reported by the pager, and TRUE otherwise. 89026f9a767SRodney W. Grimes */ 891126d6082SKonstantin Belousov boolean_t 89217f3095dSAlan Cox vm_object_page_clean(vm_object_t object, vm_ooffset_t start, vm_ooffset_t end, 893e239bb97SKonstantin Belousov int flags) 894f6b04d2bSDavid Greenman { 895e239bb97SKonstantin Belousov vm_page_t np, p; 89617f3095dSAlan Cox vm_pindex_t pi, tend, tstart; 897126d6082SKonstantin Belousov int curgeneration, n, pagerflags; 89867d0e293SJeff Roberson boolean_t eio, res, allclean; 899f6b04d2bSDavid Greenman 90089f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 901e5f299ffSKonstantin Belousov 90267d0e293SJeff Roberson if (object->type != OBJT_VNODE || !vm_object_mightbedirty(object) || 903e239bb97SKonstantin Belousov object->resident_page_count == 0) 904126d6082SKonstantin Belousov return (TRUE); 905f6b04d2bSDavid Greenman 906e239bb97SKonstantin Belousov pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) != 0 ? 907e239bb97SKonstantin Belousov VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK; 908e239bb97SKonstantin Belousov pagerflags |= (flags & OBJPC_INVAL) != 0 ? VM_PAGER_PUT_INVAL : 0; 909e239bb97SKonstantin Belousov 91017f3095dSAlan Cox tstart = OFF_TO_IDX(start); 91117f3095dSAlan Cox tend = (end == 0) ? object->size : OFF_TO_IDX(end + PAGE_MASK); 91267d0e293SJeff Roberson allclean = tstart == 0 && tend >= object->size; 913126d6082SKonstantin Belousov res = TRUE; 914f6b04d2bSDavid Greenman 915bd7e5f99SJohn Dyson rescan: 9162d8acc0fSJohn Dyson curgeneration = object->generation; 9172d8acc0fSJohn Dyson 91817f3095dSAlan Cox for (p = vm_page_find_least(object, tstart); p != NULL; p = np) { 919bd7e5f99SJohn Dyson pi = p->pindex; 920e239bb97SKonstantin Belousov if (pi >= tend) 921e239bb97SKonstantin Belousov break; 922e239bb97SKonstantin Belousov np = TAILQ_NEXT(p, listq); 9230012f373SJeff Roberson if (vm_page_none_valid(p)) 924aef922f5SJohn Dyson continue; 92563e97555SJeff Roberson if (vm_page_busy_acquire(p, VM_ALLOC_WAITFAIL) == 0) { 92667d0e293SJeff Roberson if (object->generation != curgeneration && 92767d0e293SJeff Roberson (flags & OBJPC_SYNC) != 0) 928e239bb97SKonstantin Belousov goto rescan; 929780636b7SKonstantin Belousov np = vm_page_find_least(object, pi); 930780636b7SKonstantin Belousov continue; 931f6b04d2bSDavid Greenman } 93267d0e293SJeff Roberson if (!vm_object_page_remove_write(p, flags, &allclean)) { 93363e97555SJeff Roberson vm_page_xunbusy(p); 934bd7e5f99SJohn Dyson continue; 93563e97555SJeff Roberson } 936e239bb97SKonstantin Belousov 9373280870dSKonstantin Belousov n = vm_object_page_collect_flush(object, p, pagerflags, 93867d0e293SJeff Roberson flags, &allclean, &eio); 939126d6082SKonstantin Belousov if (eio) { 940126d6082SKonstantin Belousov res = FALSE; 94167d0e293SJeff Roberson allclean = FALSE; 942126d6082SKonstantin Belousov } 94367d0e293SJeff Roberson if (object->generation != curgeneration && 94467d0e293SJeff Roberson (flags & OBJPC_SYNC) != 0) 945b9b7a4beSMatthew Dillon goto rescan; 946031ec8c1SKonstantin Belousov 947031ec8c1SKonstantin Belousov /* 948031ec8c1SKonstantin Belousov * If the VOP_PUTPAGES() did a truncated write, so 949031ec8c1SKonstantin Belousov * that even the first page of the run is not fully 950031ec8c1SKonstantin Belousov * written, vm_pageout_flush() returns 0 as the run 951031ec8c1SKonstantin Belousov * length. Since the condition that caused truncated 952031ec8c1SKonstantin Belousov * write may be permanent, e.g. exhausted free space, 953031ec8c1SKonstantin Belousov * accepting n == 0 would cause an infinite loop. 954031ec8c1SKonstantin Belousov * 955031ec8c1SKonstantin Belousov * Forwarding the iterator leaves the unwritten page 956031ec8c1SKonstantin Belousov * behind, but there is not much we can do there if 957031ec8c1SKonstantin Belousov * filesystem refuses to write it. 958031ec8c1SKonstantin Belousov */ 959126d6082SKonstantin Belousov if (n == 0) { 960031ec8c1SKonstantin Belousov n = 1; 96167d0e293SJeff Roberson allclean = FALSE; 962126d6082SKonstantin Belousov } 963e239bb97SKonstantin Belousov np = vm_page_find_least(object, pi + n); 964b9b7a4beSMatthew Dillon } 965b9b7a4beSMatthew Dillon #if 0 966e239bb97SKonstantin Belousov VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC) ? MNT_WAIT : 0); 967b9b7a4beSMatthew Dillon #endif 968b9b7a4beSMatthew Dillon 96967d0e293SJeff Roberson if (allclean) 97067d0e293SJeff Roberson object->cleangeneration = curgeneration; 971126d6082SKonstantin Belousov return (res); 972b9b7a4beSMatthew Dillon } 973b9b7a4beSMatthew Dillon 974b9b7a4beSMatthew Dillon static int 9753280870dSKonstantin Belousov vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int pagerflags, 97667d0e293SJeff Roberson int flags, boolean_t *allclean, boolean_t *eio) 977b9b7a4beSMatthew Dillon { 9783157c503SKonstantin Belousov vm_page_t ma[vm_pageout_page_count], p_first, tp; 9793157c503SKonstantin Belousov int count, i, mreq, runlen; 980b9b7a4beSMatthew Dillon 9817bec141bSKip Macy vm_page_lock_assert(p, MA_NOTOWNED); 98263e97555SJeff Roberson vm_page_assert_xbusied(p); 98389f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 9843157c503SKonstantin Belousov 9853157c503SKonstantin Belousov count = 1; 9863157c503SKonstantin Belousov mreq = 0; 9873157c503SKonstantin Belousov 9883157c503SKonstantin Belousov for (tp = p; count < vm_pageout_page_count; count++) { 9893157c503SKonstantin Belousov tp = vm_page_next(tp); 99063e97555SJeff Roberson if (tp == NULL || vm_page_tryxbusy(tp) == 0) 991bd7e5f99SJohn Dyson break; 99267d0e293SJeff Roberson if (!vm_object_page_remove_write(tp, flags, allclean)) { 99363e97555SJeff Roberson vm_page_xunbusy(tp); 994bd7e5f99SJohn Dyson break; 995bd7e5f99SJohn Dyson } 99663e97555SJeff Roberson } 997aef922f5SJohn Dyson 9983157c503SKonstantin Belousov for (p_first = p; count < vm_pageout_page_count; count++) { 9993157c503SKonstantin Belousov tp = vm_page_prev(p_first); 100063e97555SJeff Roberson if (tp == NULL || vm_page_tryxbusy(tp) == 0) 1001bd7e5f99SJohn Dyson break; 100267d0e293SJeff Roberson if (!vm_object_page_remove_write(tp, flags, allclean)) { 100363e97555SJeff Roberson vm_page_xunbusy(tp); 1004bd7e5f99SJohn Dyson break; 100563e97555SJeff Roberson } 10063157c503SKonstantin Belousov p_first = tp; 10073157c503SKonstantin Belousov mreq++; 1008bd7e5f99SJohn Dyson } 1009bd7e5f99SJohn Dyson 10103157c503SKonstantin Belousov for (tp = p_first, i = 0; i < count; tp = TAILQ_NEXT(tp, listq), i++) 10113157c503SKonstantin Belousov ma[i] = tp; 1012cf2819ccSJohn Dyson 1013126d6082SKonstantin Belousov vm_pageout_flush(ma, count, pagerflags, mreq, &runlen, eio); 10141e8a675cSKonstantin Belousov return (runlen); 101526f9a767SRodney W. Grimes } 1016df8bae1dSRodney W. Grimes 10171efb74fbSJohn Dyson /* 1018950f8459SAlan Cox * Note that there is absolutely no sense in writing out 1019950f8459SAlan Cox * anonymous objects, so we track down the vnode object 1020950f8459SAlan Cox * to write out. 1021950f8459SAlan Cox * We invalidate (remove) all pages from the address space 1022950f8459SAlan Cox * for semantic correctness. 1023950f8459SAlan Cox * 10246bbee8e2SAlan Cox * If the backing object is a device object with unmanaged pages, then any 10256bbee8e2SAlan Cox * mappings to the specified range of pages must be removed before this 10266bbee8e2SAlan Cox * function is called. 10276bbee8e2SAlan Cox * 1028950f8459SAlan Cox * Note: certain anonymous maps, such as MAP_NOSYNC maps, 1029950f8459SAlan Cox * may start out with a NULL object. 1030950f8459SAlan Cox */ 1031126d6082SKonstantin Belousov boolean_t 1032950f8459SAlan Cox vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size, 1033950f8459SAlan Cox boolean_t syncio, boolean_t invalidate) 1034950f8459SAlan Cox { 1035950f8459SAlan Cox vm_object_t backing_object; 1036950f8459SAlan Cox struct vnode *vp; 10373b582b4eSTor Egge struct mount *mp; 1038126d6082SKonstantin Belousov int error, flags, fsync_after; 1039126d6082SKonstantin Belousov boolean_t res; 1040950f8459SAlan Cox 1041950f8459SAlan Cox if (object == NULL) 1042126d6082SKonstantin Belousov return (TRUE); 1043126d6082SKonstantin Belousov res = TRUE; 1044126d6082SKonstantin Belousov error = 0; 104589f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1046950f8459SAlan Cox while ((backing_object = object->backing_object) != NULL) { 104789f6b863SAttilio Rao VM_OBJECT_WLOCK(backing_object); 104856e0670fSAlan Cox offset += object->backing_object_offset; 104989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1050950f8459SAlan Cox object = backing_object; 1051950f8459SAlan Cox if (object->size < OFF_TO_IDX(offset + size)) 1052950f8459SAlan Cox size = IDX_TO_OFF(object->size) - offset; 1053950f8459SAlan Cox } 1054950f8459SAlan Cox /* 1055950f8459SAlan Cox * Flush pages if writing is allowed, invalidate them 1056950f8459SAlan Cox * if invalidation requested. Pages undergoing I/O 1057950f8459SAlan Cox * will be ignored by vm_object_page_remove(). 1058950f8459SAlan Cox * 1059950f8459SAlan Cox * We cannot lock the vnode and then wait for paging 1060950f8459SAlan Cox * to complete without deadlocking against vm_fault. 1061950f8459SAlan Cox * Instead we simply call vm_object_page_remove() and 1062950f8459SAlan Cox * allow it to block internally on a page-by-page 1063950f8459SAlan Cox * basis when it encounters pages undergoing async 1064950f8459SAlan Cox * I/O. 1065950f8459SAlan Cox */ 1066950f8459SAlan Cox if (object->type == OBJT_VNODE && 106767d0e293SJeff Roberson vm_object_mightbedirty(object) != 0 && 10685bf94937SKonstantin Belousov ((vp = object->handle)->v_vflag & VV_NOSYNC) == 0) { 106989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 10703b582b4eSTor Egge (void) vn_start_write(vp, &mp, V_WAIT); 1071cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 107275ff604aSKonstantin Belousov if (syncio && !invalidate && offset == 0 && 1073d1780e8dSKonstantin Belousov atop(size) == object->size) { 107475ff604aSKonstantin Belousov /* 107575ff604aSKonstantin Belousov * If syncing the whole mapping of the file, 107675ff604aSKonstantin Belousov * it is faster to schedule all the writes in 107775ff604aSKonstantin Belousov * async mode, also allowing the clustering, 107875ff604aSKonstantin Belousov * and then wait for i/o to complete. 107975ff604aSKonstantin Belousov */ 108075ff604aSKonstantin Belousov flags = 0; 108175ff604aSKonstantin Belousov fsync_after = TRUE; 108275ff604aSKonstantin Belousov } else { 1083950f8459SAlan Cox flags = (syncio || invalidate) ? OBJPC_SYNC : 0; 108475ff604aSKonstantin Belousov flags |= invalidate ? (OBJPC_SYNC | OBJPC_INVAL) : 0; 108575ff604aSKonstantin Belousov fsync_after = FALSE; 108675ff604aSKonstantin Belousov } 108789f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1088126d6082SKonstantin Belousov res = vm_object_page_clean(object, offset, offset + size, 1089126d6082SKonstantin Belousov flags); 109089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 109175ff604aSKonstantin Belousov if (fsync_after) 1092126d6082SKonstantin Belousov error = VOP_FSYNC(vp, MNT_WAIT, curthread); 109322db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 10943b582b4eSTor Egge vn_finished_write(mp); 1095126d6082SKonstantin Belousov if (error != 0) 1096126d6082SKonstantin Belousov res = FALSE; 109789f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1098950f8459SAlan Cox } 1099950f8459SAlan Cox if ((object->type == OBJT_VNODE || 1100950f8459SAlan Cox object->type == OBJT_DEVICE) && invalidate) { 11016bbee8e2SAlan Cox if (object->type == OBJT_DEVICE) 11026bbee8e2SAlan Cox /* 11036bbee8e2SAlan Cox * The option OBJPR_NOTMAPPED must be passed here 11046bbee8e2SAlan Cox * because vm_object_page_remove() cannot remove 11056bbee8e2SAlan Cox * unmanaged mappings. 11066bbee8e2SAlan Cox */ 11076bbee8e2SAlan Cox flags = OBJPR_NOTMAPPED; 11086bbee8e2SAlan Cox else if (old_msync) 11096195b24aSKonstantin Belousov flags = 0; 11106bbee8e2SAlan Cox else 11116195b24aSKonstantin Belousov flags = OBJPR_CLEANONLY; 11126bbee8e2SAlan Cox vm_object_page_remove(object, OFF_TO_IDX(offset), 11136bbee8e2SAlan Cox OFF_TO_IDX(offset + size + PAGE_MASK), flags); 1114950f8459SAlan Cox } 111589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1116126d6082SKonstantin Belousov return (res); 1117950f8459SAlan Cox } 1118950f8459SAlan Cox 1119950f8459SAlan Cox /* 1120aa3650eaSMark Johnston * Determine whether the given advice can be applied to the object. Advice is 1121aa3650eaSMark Johnston * not applied to unmanaged pages since they never belong to page queues, and 1122aa3650eaSMark Johnston * since MADV_FREE is destructive, it can apply only to anonymous pages that 1123aa3650eaSMark Johnston * have been mapped at most once. 1124aa3650eaSMark Johnston */ 1125aa3650eaSMark Johnston static bool 1126aa3650eaSMark Johnston vm_object_advice_applies(vm_object_t object, int advice) 1127aa3650eaSMark Johnston { 1128aa3650eaSMark Johnston 1129aa3650eaSMark Johnston if ((object->flags & OBJ_UNMANAGED) != 0) 1130aa3650eaSMark Johnston return (false); 1131aa3650eaSMark Johnston if (advice != MADV_FREE) 1132aa3650eaSMark Johnston return (true); 113363967687SJeff Roberson return ((object->flags & (OBJ_ONEMAPPING | OBJ_ANON)) == 113463967687SJeff Roberson (OBJ_ONEMAPPING | OBJ_ANON)); 1135aa3650eaSMark Johnston } 1136aa3650eaSMark Johnston 1137aa3650eaSMark Johnston static void 1138aa3650eaSMark Johnston vm_object_madvise_freespace(vm_object_t object, int advice, vm_pindex_t pindex, 1139aa3650eaSMark Johnston vm_size_t size) 1140aa3650eaSMark Johnston { 1141aa3650eaSMark Johnston 1142aa3650eaSMark Johnston if (advice == MADV_FREE && object->type == OBJT_SWAP) 1143aa3650eaSMark Johnston swap_pager_freespace(object, pindex, size); 1144aa3650eaSMark Johnston } 1145aa3650eaSMark Johnston 1146aa3650eaSMark Johnston /* 1147867a482dSJohn Dyson * vm_object_madvise: 1148867a482dSJohn Dyson * 1149867a482dSJohn Dyson * Implements the madvise function at the object/page level. 11501c7c3c6aSMatthew Dillon * 1151193b9358SAlan Cox * MADV_WILLNEED (any object) 1152193b9358SAlan Cox * 1153193b9358SAlan Cox * Activate the specified pages if they are resident. 1154193b9358SAlan Cox * 1155193b9358SAlan Cox * MADV_DONTNEED (any object) 1156193b9358SAlan Cox * 1157193b9358SAlan Cox * Deactivate the specified pages if they are resident. 1158193b9358SAlan Cox * 1159193b9358SAlan Cox * MADV_FREE (OBJT_DEFAULT/OBJT_SWAP objects, 1160193b9358SAlan Cox * OBJ_ONEMAPPING only) 1161193b9358SAlan Cox * 1162193b9358SAlan Cox * Deactivate and clean the specified pages if they are 1163193b9358SAlan Cox * resident. This permits the process to reuse the pages 1164193b9358SAlan Cox * without faulting or the kernel to reclaim the pages 1165193b9358SAlan Cox * without I/O. 1166867a482dSJohn Dyson */ 1167867a482dSJohn Dyson void 116892a59946SJohn Baldwin vm_object_madvise(vm_object_t object, vm_pindex_t pindex, vm_pindex_t end, 1169c2655a40SMark Johnston int advice) 1170867a482dSJohn Dyson { 117192a59946SJohn Baldwin vm_pindex_t tpindex; 117234567de7SAlan Cox vm_object_t backing_object, tobject; 1173aa3650eaSMark Johnston vm_page_t m, tm; 1174867a482dSJohn Dyson 1175867a482dSJohn Dyson if (object == NULL) 1176867a482dSJohn Dyson return; 1177c2655a40SMark Johnston 11786e20a165SJohn Dyson relookup: 1179aa3650eaSMark Johnston VM_OBJECT_WLOCK(object); 1180aa3650eaSMark Johnston if (!vm_object_advice_applies(object, advice)) { 1181aa3650eaSMark Johnston VM_OBJECT_WUNLOCK(object); 1182aa3650eaSMark Johnston return; 11836e20a165SJohn Dyson } 1184aa3650eaSMark Johnston for (m = vm_page_find_least(object, pindex); pindex < end; pindex++) { 1185aa3650eaSMark Johnston tobject = object; 1186c2655a40SMark Johnston 11871ce137beSMatthew Dillon /* 1188aa3650eaSMark Johnston * If the next page isn't resident in the top-level object, we 1189aa3650eaSMark Johnston * need to search the shadow chain. When applying MADV_FREE, we 1190aa3650eaSMark Johnston * take care to release any swap space used to store 1191aa3650eaSMark Johnston * non-resident pages. 1192aa3650eaSMark Johnston */ 1193aa3650eaSMark Johnston if (m == NULL || pindex < m->pindex) { 1194aa3650eaSMark Johnston /* 1195aa3650eaSMark Johnston * Optimize a common case: if the top-level object has 1196aa3650eaSMark Johnston * no backing object, we can skip over the non-resident 1197aa3650eaSMark Johnston * range in constant time. 11981ce137beSMatthew Dillon */ 1199c2655a40SMark Johnston if (object->backing_object == NULL) { 1200c2655a40SMark Johnston tpindex = (m != NULL && m->pindex < end) ? 1201c2655a40SMark Johnston m->pindex : end; 1202aa3650eaSMark Johnston vm_object_madvise_freespace(object, advice, 1203aa3650eaSMark Johnston pindex, tpindex - pindex); 1204c2655a40SMark Johnston if ((pindex = tpindex) == end) 1205c2655a40SMark Johnston break; 1206aa3650eaSMark Johnston goto next_page; 1207aa3650eaSMark Johnston } 1208aa3650eaSMark Johnston 1209aa3650eaSMark Johnston tpindex = pindex; 1210aa3650eaSMark Johnston do { 1211aa3650eaSMark Johnston vm_object_madvise_freespace(tobject, advice, 1212aa3650eaSMark Johnston tpindex, 1); 12131ce137beSMatthew Dillon /* 1214aa3650eaSMark Johnston * Prepare to search the next object in the 1215aa3650eaSMark Johnston * chain. 12161ce137beSMatthew Dillon */ 121734567de7SAlan Cox backing_object = tobject->backing_object; 121834567de7SAlan Cox if (backing_object == NULL) 1219aa3650eaSMark Johnston goto next_pindex; 122089f6b863SAttilio Rao VM_OBJECT_WLOCK(backing_object); 1221aa3650eaSMark Johnston tpindex += 1222aa3650eaSMark Johnston OFF_TO_IDX(tobject->backing_object_offset); 12239b98b796SAlan Cox if (tobject != object) 122489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(tobject); 122534567de7SAlan Cox tobject = backing_object; 1226aa3650eaSMark Johnston if (!vm_object_advice_applies(tobject, advice)) 1227aa3650eaSMark Johnston goto next_pindex; 1228aa3650eaSMark Johnston } while ((tm = vm_page_lookup(tobject, tpindex)) == 1229aa3650eaSMark Johnston NULL); 1230aa3650eaSMark Johnston } else { 1231aa3650eaSMark Johnston next_page: 1232aa3650eaSMark Johnston tm = m; 1233aa3650eaSMark Johnston m = TAILQ_NEXT(m, listq); 1234c2655a40SMark Johnston } 1235c2655a40SMark Johnston 1236867a482dSJohn Dyson /* 12370012f373SJeff Roberson * If the page is not in a normal state, skip it. The page 12380012f373SJeff Roberson * can not be invalidated while the object lock is held. 1239867a482dSJohn Dyson */ 12400012f373SJeff Roberson if (!vm_page_all_valid(tm) || vm_page_wired(tm)) 1241aa3650eaSMark Johnston goto next_pindex; 1242aa3650eaSMark Johnston KASSERT((tm->flags & PG_FICTITIOUS) == 0, 1243aa3650eaSMark Johnston ("vm_object_madvise: page %p is fictitious", tm)); 1244aa3650eaSMark Johnston KASSERT((tm->oflags & VPO_UNMANAGED) == 0, 1245aa3650eaSMark Johnston ("vm_object_madvise: page %p is not managed", tm)); 124663e97555SJeff Roberson if (vm_page_tryxbusy(tm) == 0) { 1247aa3650eaSMark Johnston if (object != tobject) 1248aa3650eaSMark Johnston VM_OBJECT_WUNLOCK(object); 1249c2655a40SMark Johnston if (advice == MADV_WILLNEED) { 1250b11b56b5SAlan Cox /* 1251b11b56b5SAlan Cox * Reference the page before unlocking and 1252b11b56b5SAlan Cox * sleeping so that the page daemon is less 1253b11b56b5SAlan Cox * likely to reclaim it. 1254b11b56b5SAlan Cox */ 1255aa3650eaSMark Johnston vm_page_aflag_set(tm, PGA_REFERENCED); 1256567e51e1SAlan Cox } 1257aa3650eaSMark Johnston vm_page_busy_sleep(tm, "madvpo", false); 12586e20a165SJohn Dyson goto relookup; 125934567de7SAlan Cox } 1260fee2a2faSMark Johnston vm_page_lock(tm); 1261aa3650eaSMark Johnston vm_page_advise(tm, advice); 1262aa3650eaSMark Johnston vm_page_unlock(tm); 126363e97555SJeff Roberson vm_page_xunbusy(tm); 1264aa3650eaSMark Johnston vm_object_madvise_freespace(tobject, advice, tm->pindex, 1); 1265aa3650eaSMark Johnston next_pindex: 12669b98b796SAlan Cox if (tobject != object) 126789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(tobject); 1268867a482dSJohn Dyson } 126989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1270867a482dSJohn Dyson } 1271867a482dSJohn Dyson 1272867a482dSJohn Dyson /* 1273df8bae1dSRodney W. Grimes * vm_object_shadow: 1274df8bae1dSRodney W. Grimes * 1275df8bae1dSRodney W. Grimes * Create a new object which is backed by the 1276df8bae1dSRodney W. Grimes * specified existing object range. The source 1277df8bae1dSRodney W. Grimes * object reference is deallocated. 1278df8bae1dSRodney W. Grimes * 1279df8bae1dSRodney W. Grimes * The new object and offset into that object 1280df8bae1dSRodney W. Grimes * are returned in the source parameters. 1281df8bae1dSRodney W. Grimes */ 128226f9a767SRodney W. Grimes void 12831b40f8c0SMatthew Dillon vm_object_shadow( 12841b40f8c0SMatthew Dillon vm_object_t *object, /* IN/OUT */ 12851b40f8c0SMatthew Dillon vm_ooffset_t *offset, /* IN/OUT */ 12861b40f8c0SMatthew Dillon vm_size_t length) 1287df8bae1dSRodney W. Grimes { 1288d031cff1SMatthew Dillon vm_object_t source; 1289d031cff1SMatthew Dillon vm_object_t result; 1290df8bae1dSRodney W. Grimes 1291df8bae1dSRodney W. Grimes source = *object; 1292df8bae1dSRodney W. Grimes 1293df8bae1dSRodney W. Grimes /* 12949a2f6362SAlan Cox * Don't create the new object if the old object isn't shared. 129563967687SJeff Roberson * 129663967687SJeff Roberson * If we hold the only reference we can guarantee that it won't 129763967687SJeff Roberson * increase while we have the map locked. Otherwise the race is 129863967687SJeff Roberson * harmless and we will end up with an extra shadow object that 129963967687SJeff Roberson * will be collapsed later. 13009a2f6362SAlan Cox */ 130163967687SJeff Roberson if (source != NULL && source->ref_count == 1 && 130232362449SKonstantin Belousov (source->flags & OBJ_ANON) != 0) 13039a2f6362SAlan Cox return; 13049a2f6362SAlan Cox 13059a2f6362SAlan Cox /* 1306570a2f4aSAlan Cox * Allocate a new object with the given length. 1307df8bae1dSRodney W. Grimes */ 130863967687SJeff Roberson result = vm_object_allocate_anon(atop(length)); 1309df8bae1dSRodney W. Grimes 1310df8bae1dSRodney W. Grimes /* 131151b867e5SJeff Roberson * Store the offset into the source object, and fix up the offset into 131251b867e5SJeff Roberson * the new object. 131351b867e5SJeff Roberson */ 131451b867e5SJeff Roberson result->backing_object_offset = *offset; 131551b867e5SJeff Roberson 131651b867e5SJeff Roberson /* 13170d94caffSDavid Greenman * The new object shadows the source object, adding a reference to it. 13180d94caffSDavid Greenman * Our caller changes his reference to point to the new object, 13190d94caffSDavid Greenman * removing a reference to the source object. Net result: no change 13200d94caffSDavid Greenman * of reference count. 13219b09fe24SMatthew Dillon * 13229b09fe24SMatthew Dillon * Try to optimize the result object's page color when shadowing 1323956f3135SPhilippe Charnier * in order to maintain page coloring consistency in the combined 13249b09fe24SMatthew Dillon * shadowed object. 1325df8bae1dSRodney W. Grimes */ 1326570a2f4aSAlan Cox if (source != NULL) { 132751b867e5SJeff Roberson VM_OBJECT_WLOCK(result); 132851b867e5SJeff Roberson vm_object_backing_insert(result, source); 13293f289c3fSJeff Roberson result->domain = source->domain; 1330f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 13317b54b1a9SAlan Cox result->flags |= source->flags & OBJ_COLORED; 1332f8a47341SAlan Cox result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) & 1333f8a47341SAlan Cox ((1 << (VM_NFREEORDER - 1)) - 1); 1334f8a47341SAlan Cox #endif 133551b867e5SJeff Roberson VM_OBJECT_WUNLOCK(result); 1336de5f6a77SJohn Dyson } 1337df8bae1dSRodney W. Grimes 1338df8bae1dSRodney W. Grimes /* 1339df8bae1dSRodney W. Grimes * Return the new things 1340df8bae1dSRodney W. Grimes */ 1341df8bae1dSRodney W. Grimes *offset = 0; 1342df8bae1dSRodney W. Grimes *object = result; 1343df8bae1dSRodney W. Grimes } 1344df8bae1dSRodney W. Grimes 1345c5aaa06dSAlan Cox /* 1346c5aaa06dSAlan Cox * vm_object_split: 1347c5aaa06dSAlan Cox * 1348c5aaa06dSAlan Cox * Split the pages in a map entry into a new object. This affords 1349c5aaa06dSAlan Cox * easier removal of unused pages, and keeps object inheritance from 1350c5aaa06dSAlan Cox * being a negative impact on memory usage. 1351c5aaa06dSAlan Cox */ 1352c5aaa06dSAlan Cox void 1353c5aaa06dSAlan Cox vm_object_split(vm_map_entry_t entry) 1354c5aaa06dSAlan Cox { 135573000556SAlan Cox vm_page_t m, m_next; 1356c5aaa06dSAlan Cox vm_object_t orig_object, new_object, source; 135773000556SAlan Cox vm_pindex_t idx, offidxstart; 135873000556SAlan Cox vm_size_t size; 1359c5aaa06dSAlan Cox 1360c5aaa06dSAlan Cox orig_object = entry->object.vm_object; 136163967687SJeff Roberson if ((orig_object->flags & OBJ_ANON) == 0) 1362c5aaa06dSAlan Cox return; 1363c5aaa06dSAlan Cox if (orig_object->ref_count <= 1) 1364c5aaa06dSAlan Cox return; 136589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(orig_object); 1366c5aaa06dSAlan Cox 13674da9f125SAlan Cox offidxstart = OFF_TO_IDX(entry->offset); 136895442adfSAlan Cox size = atop(entry->end - entry->start); 1369c5aaa06dSAlan Cox 13704da9f125SAlan Cox /* 13714da9f125SAlan Cox * If swap_pager_copy() is later called, it will convert new_object 13724da9f125SAlan Cox * into a swap object. 13734da9f125SAlan Cox */ 137463967687SJeff Roberson new_object = vm_object_allocate_anon(size); 1375c5aaa06dSAlan Cox 1376c5474b8fSAlan Cox /* 1377c5474b8fSAlan Cox * At this point, the new object is still private, so the order in 1378c5474b8fSAlan Cox * which the original and new objects are locked does not matter. 1379c5474b8fSAlan Cox */ 138089f6b863SAttilio Rao VM_OBJECT_WLOCK(new_object); 138189f6b863SAttilio Rao VM_OBJECT_WLOCK(orig_object); 13823f289c3fSJeff Roberson new_object->domain = orig_object->domain; 1383c5aaa06dSAlan Cox source = orig_object->backing_object; 1384c5aaa06dSAlan Cox if (source != NULL) { 138551b867e5SJeff Roberson if ((source->flags & (OBJ_ANON | OBJ_DEAD)) != 0) { 138689f6b863SAttilio Rao VM_OBJECT_WLOCK(source); 138719c244d0SAlan Cox if ((source->flags & OBJ_DEAD) != 0) { 138889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(source); 138989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(orig_object); 139089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(new_object); 139119c244d0SAlan Cox vm_object_deallocate(new_object); 139289f6b863SAttilio Rao VM_OBJECT_WLOCK(orig_object); 139319c244d0SAlan Cox return; 139419c244d0SAlan Cox } 139551b867e5SJeff Roberson vm_object_backing_insert_locked(new_object, source); 1396b921a12bSAlan Cox vm_object_reference_locked(source); /* for new_object */ 1397c5aaa06dSAlan Cox vm_object_clear_flag(source, OBJ_ONEMAPPING); 139889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(source); 139951b867e5SJeff Roberson } else { 140051b867e5SJeff Roberson vm_object_backing_insert(new_object, source); 140151b867e5SJeff Roberson vm_object_reference(source); 140251b867e5SJeff Roberson } 1403c5aaa06dSAlan Cox new_object->backing_object_offset = 14044da9f125SAlan Cox orig_object->backing_object_offset + entry->offset; 1405c5aaa06dSAlan Cox } 1406ef694c1aSEdward Tomasz Napierala if (orig_object->cred != NULL) { 1407ef694c1aSEdward Tomasz Napierala new_object->cred = orig_object->cred; 1408ef694c1aSEdward Tomasz Napierala crhold(orig_object->cred); 14093364c323SKonstantin Belousov new_object->charge = ptoa(size); 14103364c323SKonstantin Belousov KASSERT(orig_object->charge >= ptoa(size), 14113364c323SKonstantin Belousov ("orig_object->charge < 0")); 14123364c323SKonstantin Belousov orig_object->charge -= ptoa(size); 14133364c323SKonstantin Belousov } 1414c5aaa06dSAlan Cox retry: 1415b382c10aSKonstantin Belousov m = vm_page_find_least(orig_object, offidxstart); 141673000556SAlan Cox for (; m != NULL && (idx = m->pindex - offidxstart) < size; 141773000556SAlan Cox m = m_next) { 141873000556SAlan Cox m_next = TAILQ_NEXT(m, listq); 1419c5aaa06dSAlan Cox 1420c5aaa06dSAlan Cox /* 1421c5aaa06dSAlan Cox * We must wait for pending I/O to complete before we can 1422c5aaa06dSAlan Cox * rename the page. 1423c5aaa06dSAlan Cox * 1424c5aaa06dSAlan Cox * We do not have to VM_PROT_NONE the page as mappings should 1425c5aaa06dSAlan Cox * not be changed by this operation. 1426c5aaa06dSAlan Cox */ 142763e97555SJeff Roberson if (vm_page_tryxbusy(m) == 0) { 142889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(new_object); 14294cdea4a8SJeff Roberson vm_page_sleep_if_busy(m, "spltwt"); 143089f6b863SAttilio Rao VM_OBJECT_WLOCK(new_object); 1431c5aaa06dSAlan Cox goto retry; 1432de33beddSAlan Cox } 1433e946b949SAttilio Rao 14343453bca8SAlan Cox /* vm_page_rename() will dirty the page. */ 1435e946b949SAttilio Rao if (vm_page_rename(m, new_object, idx)) { 143663e97555SJeff Roberson vm_page_xunbusy(m); 1437e946b949SAttilio Rao VM_OBJECT_WUNLOCK(new_object); 1438e946b949SAttilio Rao VM_OBJECT_WUNLOCK(orig_object); 14398d6fbbb8SJeff Roberson vm_radix_wait(); 1440e946b949SAttilio Rao VM_OBJECT_WLOCK(orig_object); 1441e946b949SAttilio Rao VM_OBJECT_WLOCK(new_object); 1442e946b949SAttilio Rao goto retry; 1443e946b949SAttilio Rao } 144463e97555SJeff Roberson 1445b5f359b7SAlan Cox #if VM_NRESERVLEVEL > 0 1446b5f359b7SAlan Cox /* 1447b5f359b7SAlan Cox * If some of the reservation's allocated pages remain with 1448b5f359b7SAlan Cox * the original object, then transferring the reservation to 1449b5f359b7SAlan Cox * the new object is neither particularly beneficial nor 1450b5f359b7SAlan Cox * particularly harmful as compared to leaving the reservation 1451b5f359b7SAlan Cox * with the original object. If, however, all of the 1452b5f359b7SAlan Cox * reservation's allocated pages are transferred to the new 1453b5f359b7SAlan Cox * object, then transferring the reservation is typically 1454b5f359b7SAlan Cox * beneficial. Determining which of these two cases applies 1455b5f359b7SAlan Cox * would be more costly than unconditionally renaming the 1456b5f359b7SAlan Cox * reservation. 1457b5f359b7SAlan Cox */ 1458b5f359b7SAlan Cox vm_reserv_rename(m, new_object, orig_object, offidxstart); 1459b5f359b7SAlan Cox #endif 14608da1c098SJeff Roberson if (orig_object->type != OBJT_SWAP) 14618da1c098SJeff Roberson vm_page_xunbusy(m); 1462c5aaa06dSAlan Cox } 1463d7a013c3SAlan Cox if (orig_object->type == OBJT_SWAP) { 1464c5aaa06dSAlan Cox /* 1465c7c8dd7eSAlan Cox * swap_pager_copy() can sleep, in which case the orig_object's 1466c7c8dd7eSAlan Cox * and new_object's locks are released and reacquired. 1467c5aaa06dSAlan Cox */ 1468c5aaa06dSAlan Cox swap_pager_copy(orig_object, new_object, offidxstart, 0); 1469dfd55c0cSAttilio Rao TAILQ_FOREACH(m, &new_object->memq, listq) 1470c7aebda8SAttilio Rao vm_page_xunbusy(m); 1471c5aaa06dSAlan Cox } 147289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(orig_object); 147389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(new_object); 1474c5aaa06dSAlan Cox entry->object.vm_object = new_object; 1475c5aaa06dSAlan Cox entry->offset = 0LL; 1476c5aaa06dSAlan Cox vm_object_deallocate(orig_object); 147789f6b863SAttilio Rao VM_OBJECT_WLOCK(new_object); 1478c5aaa06dSAlan Cox } 1479c5aaa06dSAlan Cox 14802ad1a3f7SMatthew Dillon #define OBSC_COLLAPSE_NOWAIT 0x0002 14812ad1a3f7SMatthew Dillon #define OBSC_COLLAPSE_WAIT 0x0004 14822ad1a3f7SMatthew Dillon 148399a1570aSKonstantin Belousov static vm_page_t 14844cc8daf7SConrad Meyer vm_object_collapse_scan_wait(vm_object_t object, vm_page_t p, vm_page_t next, 148599a1570aSKonstantin Belousov int op) 148699a1570aSKonstantin Belousov { 148799a1570aSKonstantin Belousov vm_object_t backing_object; 148899a1570aSKonstantin Belousov 148999a1570aSKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(object); 149099a1570aSKonstantin Belousov backing_object = object->backing_object; 149199a1570aSKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(backing_object); 149299a1570aSKonstantin Belousov 149399a1570aSKonstantin Belousov KASSERT(p == NULL || p->object == object || p->object == backing_object, 149499a1570aSKonstantin Belousov ("invalid ownership %p %p %p", p, object, backing_object)); 149599a1570aSKonstantin Belousov if ((op & OBSC_COLLAPSE_NOWAIT) != 0) 149699a1570aSKonstantin Belousov return (next); 14978d6fbbb8SJeff Roberson /* The page is only NULL when rename fails. */ 14984cdea4a8SJeff Roberson if (p == NULL) { 1499*6a14746cSRyan Libby VM_OBJECT_WUNLOCK(object); 1500*6a14746cSRyan Libby VM_OBJECT_WUNLOCK(backing_object); 15018d6fbbb8SJeff Roberson vm_radix_wait(); 15024cdea4a8SJeff Roberson } else { 15034cdea4a8SJeff Roberson if (p->object == object) 15044cdea4a8SJeff Roberson VM_OBJECT_WUNLOCK(backing_object); 150599a1570aSKonstantin Belousov else 15064cdea4a8SJeff Roberson VM_OBJECT_WUNLOCK(object); 15075975e53dSKonstantin Belousov vm_page_busy_sleep(p, "vmocol", false); 15084cdea4a8SJeff Roberson } 150999a1570aSKonstantin Belousov VM_OBJECT_WLOCK(object); 151099a1570aSKonstantin Belousov VM_OBJECT_WLOCK(backing_object); 151199a1570aSKonstantin Belousov return (TAILQ_FIRST(&backing_object->memq)); 151299a1570aSKonstantin Belousov } 151399a1570aSKonstantin Belousov 151499a1570aSKonstantin Belousov static bool 15154cc8daf7SConrad Meyer vm_object_scan_all_shadowed(vm_object_t object) 15164cc8daf7SConrad Meyer { 15174cc8daf7SConrad Meyer vm_object_t backing_object; 15184cc8daf7SConrad Meyer vm_page_t p, pp; 151977d6fd97SKonstantin Belousov vm_pindex_t backing_offset_index, new_pindex, pi, ps; 15204cc8daf7SConrad Meyer 15214cc8daf7SConrad Meyer VM_OBJECT_ASSERT_WLOCKED(object); 15224cc8daf7SConrad Meyer VM_OBJECT_ASSERT_WLOCKED(object->backing_object); 15234cc8daf7SConrad Meyer 15244cc8daf7SConrad Meyer backing_object = object->backing_object; 15254cc8daf7SConrad Meyer 152663967687SJeff Roberson if ((backing_object->flags & OBJ_ANON) == 0) 15274cc8daf7SConrad Meyer return (false); 15284cc8daf7SConrad Meyer 152977d6fd97SKonstantin Belousov pi = backing_offset_index = OFF_TO_IDX(object->backing_object_offset); 153077d6fd97SKonstantin Belousov p = vm_page_find_least(backing_object, pi); 153177d6fd97SKonstantin Belousov ps = swap_pager_find_least(backing_object, pi); 15324cc8daf7SConrad Meyer 15334cc8daf7SConrad Meyer /* 153477d6fd97SKonstantin Belousov * Only check pages inside the parent object's range and 153577d6fd97SKonstantin Belousov * inside the parent object's mapping of the backing object. 15364cc8daf7SConrad Meyer */ 153777d6fd97SKonstantin Belousov for (;; pi++) { 153877d6fd97SKonstantin Belousov if (p != NULL && p->pindex < pi) 153977d6fd97SKonstantin Belousov p = TAILQ_NEXT(p, listq); 154077d6fd97SKonstantin Belousov if (ps < pi) 154177d6fd97SKonstantin Belousov ps = swap_pager_find_least(backing_object, pi); 154277d6fd97SKonstantin Belousov if (p == NULL && ps >= backing_object->size) 154377d6fd97SKonstantin Belousov break; 154477d6fd97SKonstantin Belousov else if (p == NULL) 154577d6fd97SKonstantin Belousov pi = ps; 154677d6fd97SKonstantin Belousov else 154777d6fd97SKonstantin Belousov pi = MIN(p->pindex, ps); 154877d6fd97SKonstantin Belousov 154977d6fd97SKonstantin Belousov new_pindex = pi - backing_offset_index; 155077d6fd97SKonstantin Belousov if (new_pindex >= object->size) 155177d6fd97SKonstantin Belousov break; 15524cc8daf7SConrad Meyer 15534cc8daf7SConrad Meyer /* 15544cc8daf7SConrad Meyer * See if the parent has the page or if the parent's object 15554cc8daf7SConrad Meyer * pager has the page. If the parent has the page but the page 15564cc8daf7SConrad Meyer * is not valid, the parent's object pager must have the page. 15574cc8daf7SConrad Meyer * 15584cc8daf7SConrad Meyer * If this fails, the parent does not completely shadow the 15594cc8daf7SConrad Meyer * object and we might as well give up now. 15604cc8daf7SConrad Meyer */ 15614cc8daf7SConrad Meyer pp = vm_page_lookup(object, new_pindex); 15620012f373SJeff Roberson /* 15630012f373SJeff Roberson * The valid check here is stable due to object lock being 15640012f373SJeff Roberson * required to clear valid and initiate paging. 15650012f373SJeff Roberson */ 15660012f373SJeff Roberson if ((pp == NULL || vm_page_none_valid(pp)) && 15674cc8daf7SConrad Meyer !vm_pager_has_page(object, new_pindex, NULL, NULL)) 15684cc8daf7SConrad Meyer return (false); 15694cc8daf7SConrad Meyer } 15704cc8daf7SConrad Meyer return (true); 15714cc8daf7SConrad Meyer } 15724cc8daf7SConrad Meyer 15734cc8daf7SConrad Meyer static bool 15744cc8daf7SConrad Meyer vm_object_collapse_scan(vm_object_t object, int op) 15752ad1a3f7SMatthew Dillon { 15762ad1a3f7SMatthew Dillon vm_object_t backing_object; 157799a1570aSKonstantin Belousov vm_page_t next, p, pp; 157899a1570aSKonstantin Belousov vm_pindex_t backing_offset_index, new_pindex; 15792ad1a3f7SMatthew Dillon 158089f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 158189f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object->backing_object); 15822ad1a3f7SMatthew Dillon 15832ad1a3f7SMatthew Dillon backing_object = object->backing_object; 15842ad1a3f7SMatthew Dillon backing_offset_index = OFF_TO_IDX(object->backing_object_offset); 15852ad1a3f7SMatthew Dillon 15862ad1a3f7SMatthew Dillon /* 15872ad1a3f7SMatthew Dillon * Initial conditions 15882ad1a3f7SMatthew Dillon */ 15894cc8daf7SConrad Meyer if ((op & OBSC_COLLAPSE_WAIT) != 0) 15902ad1a3f7SMatthew Dillon vm_object_set_flag(backing_object, OBJ_DEAD); 15912ad1a3f7SMatthew Dillon 15922ad1a3f7SMatthew Dillon /* 15932ad1a3f7SMatthew Dillon * Our scan 15942ad1a3f7SMatthew Dillon */ 15954cc8daf7SConrad Meyer for (p = TAILQ_FIRST(&backing_object->memq); p != NULL; p = next) { 159699a1570aSKonstantin Belousov next = TAILQ_NEXT(p, listq); 159799a1570aSKonstantin Belousov new_pindex = p->pindex - backing_offset_index; 15982ad1a3f7SMatthew Dillon 15992ad1a3f7SMatthew Dillon /* 16002ad1a3f7SMatthew Dillon * Check for busy page 16012ad1a3f7SMatthew Dillon */ 160263e97555SJeff Roberson if (vm_page_tryxbusy(p) == 0) { 16034cc8daf7SConrad Meyer next = vm_object_collapse_scan_wait(object, p, next, op); 16042ad1a3f7SMatthew Dillon continue; 16052ad1a3f7SMatthew Dillon } 16062ad1a3f7SMatthew Dillon 160799a1570aSKonstantin Belousov KASSERT(p->object == backing_object, 16084cc8daf7SConrad Meyer ("vm_object_collapse_scan: object mismatch")); 16092ad1a3f7SMatthew Dillon 161099a1570aSKonstantin Belousov if (p->pindex < backing_offset_index || 161199a1570aSKonstantin Belousov new_pindex >= object->size) { 1612e946b949SAttilio Rao if (backing_object->type == OBJT_SWAP) 16134cc8daf7SConrad Meyer swap_pager_freespace(backing_object, p->pindex, 16144cc8daf7SConrad Meyer 1); 1615e946b949SAttilio Rao 1616f6d89838SAlan Cox KASSERT(!pmap_page_is_mapped(p), 1617f6d89838SAlan Cox ("freeing mapped page %p", p)); 16180fd977b3SMark Johnston if (vm_page_remove(p)) 16192ad1a3f7SMatthew Dillon vm_page_free(p); 16208da1c098SJeff Roberson else 16218da1c098SJeff Roberson vm_page_xunbusy(p); 16222ad1a3f7SMatthew Dillon continue; 16232ad1a3f7SMatthew Dillon } 16242ad1a3f7SMatthew Dillon 16252ad1a3f7SMatthew Dillon pp = vm_page_lookup(object, new_pindex); 162663e97555SJeff Roberson if (pp != NULL && vm_page_tryxbusy(pp) == 0) { 162763e97555SJeff Roberson vm_page_xunbusy(p); 1628e18cc7bfSMax Laier /* 16294cc8daf7SConrad Meyer * The page in the parent is busy and possibly not 16304cc8daf7SConrad Meyer * (yet) valid. Until its state is finalized by the 16314cc8daf7SConrad Meyer * busy bit owner, we can't tell whether it shadows the 16324cc8daf7SConrad Meyer * original page. Therefore, we must either skip it 16334cc8daf7SConrad Meyer * and the original (backing_object) page or wait for 16344cc8daf7SConrad Meyer * its state to be finalized. 1635e18cc7bfSMax Laier * 16364cc8daf7SConrad Meyer * This is due to a race with vm_fault() where we must 16374cc8daf7SConrad Meyer * unbusy the original (backing_obj) page before we can 16384cc8daf7SConrad Meyer * (re)lock the parent. Hence we can get here. 1639e18cc7bfSMax Laier */ 16404cc8daf7SConrad Meyer next = vm_object_collapse_scan_wait(object, pp, next, 16414cc8daf7SConrad Meyer op); 1642e18cc7bfSMax Laier continue; 1643e18cc7bfSMax Laier } 164499a1570aSKonstantin Belousov 16450012f373SJeff Roberson KASSERT(pp == NULL || !vm_page_none_valid(pp), 164699a1570aSKonstantin Belousov ("unbusy invalid page %p", pp)); 164799a1570aSKonstantin Belousov 16484cc8daf7SConrad Meyer if (pp != NULL || vm_pager_has_page(object, new_pindex, NULL, 16494cc8daf7SConrad Meyer NULL)) { 165099a1570aSKonstantin Belousov /* 16514cc8daf7SConrad Meyer * The page already exists in the parent OR swap exists 16524cc8daf7SConrad Meyer * for this location in the parent. Leave the parent's 16534cc8daf7SConrad Meyer * page alone. Destroy the original page from the 16544cc8daf7SConrad Meyer * backing object. 165599a1570aSKonstantin Belousov */ 1656e946b949SAttilio Rao if (backing_object->type == OBJT_SWAP) 16574cc8daf7SConrad Meyer swap_pager_freespace(backing_object, p->pindex, 16584cc8daf7SConrad Meyer 1); 1659f6d89838SAlan Cox KASSERT(!pmap_page_is_mapped(p), 1660f6d89838SAlan Cox ("freeing mapped page %p", p)); 16610fd977b3SMark Johnston if (vm_page_remove(p)) 16622ad1a3f7SMatthew Dillon vm_page_free(p); 16638da1c098SJeff Roberson else 16648da1c098SJeff Roberson vm_page_xunbusy(p); 166563e97555SJeff Roberson if (pp != NULL) 166663e97555SJeff Roberson vm_page_xunbusy(pp); 16672ad1a3f7SMatthew Dillon continue; 16682ad1a3f7SMatthew Dillon } 16692ad1a3f7SMatthew Dillon 1670e946b949SAttilio Rao /* 16714cc8daf7SConrad Meyer * Page does not exist in parent, rename the page from the 16724cc8daf7SConrad Meyer * backing object to the main object. 1673e946b949SAttilio Rao * 16744cc8daf7SConrad Meyer * If the page was mapped to a process, it can remain mapped 16753453bca8SAlan Cox * through the rename. vm_page_rename() will dirty the page. 1676e946b949SAttilio Rao */ 1677e946b949SAttilio Rao if (vm_page_rename(p, object, new_pindex)) { 167863e97555SJeff Roberson vm_page_xunbusy(p); 167963e97555SJeff Roberson if (pp != NULL) 168063e97555SJeff Roberson vm_page_xunbusy(pp); 16814cc8daf7SConrad Meyer next = vm_object_collapse_scan_wait(object, NULL, next, 16824cc8daf7SConrad Meyer op); 1683e946b949SAttilio Rao continue; 1684e946b949SAttilio Rao } 168514a5dc17SAttilio Rao 168614a5dc17SAttilio Rao /* Use the old pindex to free the right page. */ 1687e946b949SAttilio Rao if (backing_object->type == OBJT_SWAP) 168814a5dc17SAttilio Rao swap_pager_freespace(backing_object, 168914a5dc17SAttilio Rao new_pindex + backing_offset_index, 1); 1690e946b949SAttilio Rao 1691f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 1692f8a47341SAlan Cox /* 1693f8a47341SAlan Cox * Rename the reservation. 1694f8a47341SAlan Cox */ 1695f8a47341SAlan Cox vm_reserv_rename(p, object, backing_object, 1696f8a47341SAlan Cox backing_offset_index); 1697f8a47341SAlan Cox #endif 16988da1c098SJeff Roberson vm_page_xunbusy(p); 16992ad1a3f7SMatthew Dillon } 170099a1570aSKonstantin Belousov return (true); 17012ad1a3f7SMatthew Dillon } 17022ad1a3f7SMatthew Dillon 1703df8bae1dSRodney W. Grimes 1704df8bae1dSRodney W. Grimes /* 17052fe6e4d7SDavid Greenman * this version of collapse allows the operation to occur earlier and 17062fe6e4d7SDavid Greenman * when paging_in_progress is true for an object... This is not a complete 17072fe6e4d7SDavid Greenman * operation, but should plug 99.9% of the rest of the leaks. 17082fe6e4d7SDavid Greenman */ 17092fe6e4d7SDavid Greenman static void 17101b40f8c0SMatthew Dillon vm_object_qcollapse(vm_object_t object) 17112fe6e4d7SDavid Greenman { 17122ad1a3f7SMatthew Dillon vm_object_t backing_object = object->backing_object; 17132fe6e4d7SDavid Greenman 171489f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 171589f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(backing_object); 17161b40f8c0SMatthew Dillon 17172fe6e4d7SDavid Greenman if (backing_object->ref_count != 1) 17182fe6e4d7SDavid Greenman return; 17192fe6e4d7SDavid Greenman 17204cc8daf7SConrad Meyer vm_object_collapse_scan(object, OBSC_COLLAPSE_NOWAIT); 17212fe6e4d7SDavid Greenman } 17222fe6e4d7SDavid Greenman 1723df8bae1dSRodney W. Grimes /* 1724df8bae1dSRodney W. Grimes * vm_object_collapse: 1725df8bae1dSRodney W. Grimes * 1726df8bae1dSRodney W. Grimes * Collapse an object with the object backing it. 1727df8bae1dSRodney W. Grimes * Pages in the backing object are moved into the 1728df8bae1dSRodney W. Grimes * parent, and the backing object is deallocated. 1729df8bae1dSRodney W. Grimes */ 173026f9a767SRodney W. Grimes void 17311b40f8c0SMatthew Dillon vm_object_collapse(vm_object_t object) 1732df8bae1dSRodney W. Grimes { 173398f139daSKonstantin Belousov vm_object_t backing_object, new_backing_object; 173498f139daSKonstantin Belousov 173589f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 173623955314SAlfred Perlstein 1737df8bae1dSRodney W. Grimes while (TRUE) { 1738df8bae1dSRodney W. Grimes /* 1739df8bae1dSRodney W. Grimes * Verify that the conditions are right for collapse: 1740df8bae1dSRodney W. Grimes * 17412ad1a3f7SMatthew Dillon * The object exists and the backing object exists. 1742df8bae1dSRodney W. Grimes */ 174324a1cce3SDavid Greenman if ((backing_object = object->backing_object) == NULL) 17442ad1a3f7SMatthew Dillon break; 1745df8bae1dSRodney W. Grimes 1746f919ebdeSDavid Greenman /* 1747f919ebdeSDavid Greenman * we check the backing object first, because it is most likely 174824a1cce3SDavid Greenman * not collapsable. 1749f919ebdeSDavid Greenman */ 175063967687SJeff Roberson if ((backing_object->flags & OBJ_ANON) == 0) 175163967687SJeff Roberson break; 175289f6b863SAttilio Rao VM_OBJECT_WLOCK(backing_object); 175332362449SKonstantin Belousov if ((backing_object->flags & OBJ_DEAD) != 0 || 175432362449SKonstantin Belousov (object->flags & (OBJ_DEAD | OBJ_ANON)) != OBJ_ANON) { 175589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 17562ad1a3f7SMatthew Dillon break; 175724a1cce3SDavid Greenman } 17589b4814bbSDavid Greenman 175911b57401SHans Petter Selasky if (REFCOUNT_COUNT(object->paging_in_progress) > 0 || 176011b57401SHans Petter Selasky REFCOUNT_COUNT(backing_object->paging_in_progress) > 0) { 1761b9921222SDavid Greenman vm_object_qcollapse(object); 176289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 17632ad1a3f7SMatthew Dillon break; 1764df8bae1dSRodney W. Grimes } 176598f139daSKonstantin Belousov 176626f9a767SRodney W. Grimes /* 17670d94caffSDavid Greenman * We know that we can either collapse the backing object (if 17682ad1a3f7SMatthew Dillon * the parent is the only reference to it) or (perhaps) have 17692ad1a3f7SMatthew Dillon * the parent bypass the object if the parent happens to shadow 17702ad1a3f7SMatthew Dillon * all the resident pages in the entire backing object. 17712ad1a3f7SMatthew Dillon * 17722ad1a3f7SMatthew Dillon * This is ignoring pager-backed pages such as swap pages. 17734cc8daf7SConrad Meyer * vm_object_collapse_scan fails the shadowing test in this 17742ad1a3f7SMatthew Dillon * case. 1775df8bae1dSRodney W. Grimes */ 1776df8bae1dSRodney W. Grimes if (backing_object->ref_count == 1) { 1777aa9bc3b1SKonstantin Belousov vm_object_pip_add(object, 1); 1778aa9bc3b1SKonstantin Belousov vm_object_pip_add(backing_object, 1); 1779aa9bc3b1SKonstantin Belousov 1780df8bae1dSRodney W. Grimes /* 17812ad1a3f7SMatthew Dillon * If there is exactly one reference to the backing 17822ad1a3f7SMatthew Dillon * object, we can collapse it into the parent. 1783df8bae1dSRodney W. Grimes */ 17844cc8daf7SConrad Meyer vm_object_collapse_scan(object, OBSC_COLLAPSE_WAIT); 1785df8bae1dSRodney W. Grimes 1786f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 1787f8a47341SAlan Cox /* 1788f8a47341SAlan Cox * Break any reservations from backing_object. 1789f8a47341SAlan Cox */ 1790f8a47341SAlan Cox if (__predict_false(!LIST_EMPTY(&backing_object->rvq))) 1791f8a47341SAlan Cox vm_reserv_break_all(backing_object); 1792f8a47341SAlan Cox #endif 1793f8a47341SAlan Cox 1794df8bae1dSRodney W. Grimes /* 1795df8bae1dSRodney W. Grimes * Move the pager from backing_object to object. 1796df8bae1dSRodney W. Grimes */ 17976be36525SAlan Cox if (backing_object->type == OBJT_SWAP) { 179824a1cce3SDavid Greenman /* 1799c7c8dd7eSAlan Cox * swap_pager_copy() can sleep, in which case 1800c7c8dd7eSAlan Cox * the backing_object's and object's locks are 1801c7c8dd7eSAlan Cox * released and reacquired. 1802571a1e92SAttilio Rao * Since swap_pager_copy() is being asked to 1803571a1e92SAttilio Rao * destroy the source, it will change the 1804571a1e92SAttilio Rao * backing_object's type to OBJT_DEFAULT. 180524a1cce3SDavid Greenman */ 18061c7c3c6aSMatthew Dillon swap_pager_copy( 18071c7c3c6aSMatthew Dillon backing_object, 18081c7c3c6aSMatthew Dillon object, 18091c7c3c6aSMatthew Dillon OFF_TO_IDX(object->backing_object_offset), TRUE); 1810c0503609SDavid Greenman } 1811df8bae1dSRodney W. Grimes /* 1812df8bae1dSRodney W. Grimes * Object now shadows whatever backing_object did. 18132ad1a3f7SMatthew Dillon * Note that the reference to 18142ad1a3f7SMatthew Dillon * backing_object->backing_object moves from within 18152ad1a3f7SMatthew Dillon * backing_object to within object. 1816df8bae1dSRodney W. Grimes */ 181751b867e5SJeff Roberson vm_object_backing_remove_locked(object); 181851b867e5SJeff Roberson new_backing_object = backing_object->backing_object; 181951b867e5SJeff Roberson if (new_backing_object != NULL) { 182051b867e5SJeff Roberson VM_OBJECT_WLOCK(new_backing_object); 182151b867e5SJeff Roberson vm_object_backing_remove_locked(backing_object); 182251b867e5SJeff Roberson vm_object_backing_insert_locked(object, 182351b867e5SJeff Roberson new_backing_object); 182451b867e5SJeff Roberson VM_OBJECT_WUNLOCK(new_backing_object); 1825de5f6a77SJohn Dyson } 18262ad1a3f7SMatthew Dillon object->backing_object_offset += 18272ad1a3f7SMatthew Dillon backing_object->backing_object_offset; 18282ad1a3f7SMatthew Dillon 1829df8bae1dSRodney W. Grimes /* 1830df8bae1dSRodney W. Grimes * Discard backing_object. 1831df8bae1dSRodney W. Grimes * 18320d94caffSDavid Greenman * Since the backing object has no pages, no pager left, 18330d94caffSDavid Greenman * and no object references within it, all that is 18340d94caffSDavid Greenman * necessary is to dispose of it. 1835df8bae1dSRodney W. Grimes */ 18369b4d473aSKonstantin Belousov KASSERT(backing_object->ref_count == 1, ( 18379b4d473aSKonstantin Belousov "backing_object %p was somehow re-referenced during collapse!", 18389b4d473aSKonstantin Belousov backing_object)); 1839aa9bc3b1SKonstantin Belousov vm_object_pip_wakeup(backing_object); 1840e735691bSJohn Baldwin backing_object->type = OBJT_DEAD; 1841e735691bSJohn Baldwin backing_object->ref_count = 0; 184289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 18439b4d473aSKonstantin Belousov vm_object_destroy(backing_object); 1844df8bae1dSRodney W. Grimes 1845aa9bc3b1SKonstantin Belousov vm_object_pip_wakeup(object); 184611542376SAlan Cox counter_u64_add(object_collapses, 1); 18470d94caffSDavid Greenman } else { 1848df8bae1dSRodney W. Grimes /* 18492ad1a3f7SMatthew Dillon * If we do not entirely shadow the backing object, 18502ad1a3f7SMatthew Dillon * there is nothing we can do so we give up. 1851df8bae1dSRodney W. Grimes */ 1852df59a0feSJeff Roberson if (object->resident_page_count != object->size && 18534cc8daf7SConrad Meyer !vm_object_scan_all_shadowed(object)) { 185489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 18552ad1a3f7SMatthew Dillon break; 185624a1cce3SDavid Greenman } 1857df8bae1dSRodney W. Grimes 1858df8bae1dSRodney W. Grimes /* 18590d94caffSDavid Greenman * Make the parent shadow the next object in the 18600d94caffSDavid Greenman * chain. Deallocating backing_object will not remove 18610d94caffSDavid Greenman * it, since its reference count is at least 2. 1862df8bae1dSRodney W. Grimes */ 186351b867e5SJeff Roberson vm_object_backing_remove_locked(object); 186495e5e988SJohn Dyson 186595e5e988SJohn Dyson new_backing_object = backing_object->backing_object; 186651b867e5SJeff Roberson if (new_backing_object != NULL) { 186751b867e5SJeff Roberson vm_object_backing_insert(object, 186851b867e5SJeff Roberson new_backing_object); 186951b867e5SJeff Roberson vm_object_reference(new_backing_object); 187095e5e988SJohn Dyson object->backing_object_offset += 187195e5e988SJohn Dyson backing_object->backing_object_offset; 1872de5f6a77SJohn Dyson } 1873df8bae1dSRodney W. Grimes 1874df8bae1dSRodney W. Grimes /* 18750d94caffSDavid Greenman * Drop the reference count on backing_object. Since 187622ec553fSAlan Cox * its ref_count was at least 2, it will not vanish. 1877df8bae1dSRodney W. Grimes */ 187851df5321SJeff Roberson refcount_release(&backing_object->ref_count); 187989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 188011542376SAlan Cox counter_u64_add(object_bypasses, 1); 1881df8bae1dSRodney W. Grimes } 1882df8bae1dSRodney W. Grimes 1883df8bae1dSRodney W. Grimes /* 1884df8bae1dSRodney W. Grimes * Try again with this object's new backing object. 1885df8bae1dSRodney W. Grimes */ 1886df8bae1dSRodney W. Grimes } 1887df8bae1dSRodney W. Grimes } 1888df8bae1dSRodney W. Grimes 1889df8bae1dSRodney W. Grimes /* 1890bff99f0dSAlan Cox * vm_object_page_remove: 1891df8bae1dSRodney W. Grimes * 189268855966SAlan Cox * For the given object, either frees or invalidates each of the 18936bbee8e2SAlan Cox * specified pages. In general, a page is freed. However, if a page is 18946bbee8e2SAlan Cox * wired for any reason other than the existence of a managed, wired 18956bbee8e2SAlan Cox * mapping, then it may be invalidated but not removed from the object. 18966bbee8e2SAlan Cox * Pages are specified by the given range ["start", "end") and the option 18976bbee8e2SAlan Cox * OBJPR_CLEANONLY. As a special case, if "end" is zero, then the range 18986bbee8e2SAlan Cox * extends from "start" to the end of the object. If the option 18996bbee8e2SAlan Cox * OBJPR_CLEANONLY is specified, then only the non-dirty pages within the 19006bbee8e2SAlan Cox * specified range are affected. If the option OBJPR_NOTMAPPED is 19016bbee8e2SAlan Cox * specified, then the pages within the specified range must have no 19026bbee8e2SAlan Cox * mappings. Otherwise, if this option is not specified, any mappings to 19036bbee8e2SAlan Cox * the specified pages are removed before the pages are freed or 19046bbee8e2SAlan Cox * invalidated. 190568855966SAlan Cox * 19066bbee8e2SAlan Cox * In general, this operation should only be performed on objects that 19076bbee8e2SAlan Cox * contain managed pages. There are, however, two exceptions. First, it 19086bbee8e2SAlan Cox * is performed on the kernel and kmem objects by vm_map_entry_delete(). 19096bbee8e2SAlan Cox * Second, it is used by msync(..., MS_INVALIDATE) to invalidate device- 19106bbee8e2SAlan Cox * backed pages. In both of these cases, the option OBJPR_CLEANONLY must 19116bbee8e2SAlan Cox * not be specified and the option OBJPR_NOTMAPPED must be specified. 1912df8bae1dSRodney W. Grimes * 1913df8bae1dSRodney W. Grimes * The object must be locked. 1914df8bae1dSRodney W. Grimes */ 191526f9a767SRodney W. Grimes void 1916ecde4b32SAlan Cox vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end, 19176bbee8e2SAlan Cox int options) 1918df8bae1dSRodney W. Grimes { 1919d031cff1SMatthew Dillon vm_page_t p, next; 1920df8bae1dSRodney W. Grimes 192189f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 192228634820SAlan Cox KASSERT((object->flags & OBJ_UNMANAGED) == 0 || 19236bbee8e2SAlan Cox (options & (OBJPR_CLEANONLY | OBJPR_NOTMAPPED)) == OBJPR_NOTMAPPED, 19246bbee8e2SAlan Cox ("vm_object_page_remove: illegal options for object %p", object)); 1925ecde4b32SAlan Cox if (object->resident_page_count == 0) 19267667839aSAlan Cox return; 1927d474eaaaSDoug Rabson vm_object_pip_add(object, 1); 192826f9a767SRodney W. Grimes again: 1929b382c10aSKonstantin Belousov p = vm_page_find_least(object, start); 19302965a453SKip Macy 193175741c04SAlan Cox /* 19326bbee8e2SAlan Cox * Here, the variable "p" is either (1) the page with the least pindex 19336bbee8e2SAlan Cox * greater than or equal to the parameter "start" or (2) NULL. 193475741c04SAlan Cox */ 19356bbee8e2SAlan Cox for (; p != NULL && (p->pindex < end || end == 0); p = next) { 1936b18bfc3dSJohn Dyson next = TAILQ_NEXT(p, listq); 193775741c04SAlan Cox 193859677d3cSAlan Cox /* 19396bbee8e2SAlan Cox * If the page is wired for any reason besides the existence 19406bbee8e2SAlan Cox * of managed, wired mappings, then it cannot be freed. For 19416bbee8e2SAlan Cox * example, fictitious pages, which represent device memory, 19426bbee8e2SAlan Cox * are inherently wired and cannot be freed. They can, 19436bbee8e2SAlan Cox * however, be invalidated if the option OBJPR_CLEANONLY is 19446bbee8e2SAlan Cox * not specified. 194559677d3cSAlan Cox */ 194663e97555SJeff Roberson if (vm_page_tryxbusy(p) == 0) { 19474cdea4a8SJeff Roberson vm_page_sleep_if_busy(p, "vmopar"); 1948fee2a2faSMark Johnston goto again; 1949fee2a2faSMark Johnston } 1950d842aa51SMark Johnston if (vm_page_wired(p)) { 1951fee2a2faSMark Johnston wired: 1952cf060942SAlan Cox if ((options & OBJPR_NOTMAPPED) == 0 && 1953cf060942SAlan Cox object->ref_count != 0) 19544fec79beSAlan Cox pmap_remove_all(p); 19556bbee8e2SAlan Cox if ((options & OBJPR_CLEANONLY) == 0) { 19560012f373SJeff Roberson vm_page_invalid(p); 1957a28042d1SAlan Cox vm_page_undirty(p); 1958a28042d1SAlan Cox } 195963e97555SJeff Roberson vm_page_xunbusy(p); 196093c5d3a4SKonstantin Belousov continue; 19610d94caffSDavid Greenman } 196268855966SAlan Cox KASSERT((p->flags & PG_FICTITIOUS) == 0, 196368855966SAlan Cox ("vm_object_page_remove: page %p is fictitious", p)); 19640012f373SJeff Roberson if ((options & OBJPR_CLEANONLY) != 0 && 19650012f373SJeff Roberson !vm_page_none_valid(p)) { 1966cf060942SAlan Cox if ((options & OBJPR_NOTMAPPED) == 0 && 1967fee2a2faSMark Johnston object->ref_count != 0 && 1968fee2a2faSMark Johnston !vm_page_try_remove_write(p)) 1969fee2a2faSMark Johnston goto wired; 197063e97555SJeff Roberson if (p->dirty != 0) { 197163e97555SJeff Roberson vm_page_xunbusy(p); 197293c5d3a4SKonstantin Belousov continue; 19732965a453SKip Macy } 197463e97555SJeff Roberson } 1975fee2a2faSMark Johnston if ((options & OBJPR_NOTMAPPED) == 0 && 1976fee2a2faSMark Johnston object->ref_count != 0 && !vm_page_try_remove_all(p)) 1977fee2a2faSMark Johnston goto wired; 19785cd29d0fSMark Johnston vm_page_free(p); 19792965a453SKip Macy } 1980f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 1981c0503609SDavid Greenman } 1982df8bae1dSRodney W. Grimes 1983df8bae1dSRodney W. Grimes /* 19843138cd36SMark Johnston * vm_object_page_noreuse: 1985936c09acSJohn Baldwin * 19863138cd36SMark Johnston * For the given object, attempt to move the specified pages to 19873138cd36SMark Johnston * the head of the inactive queue. This bypasses regular LRU 19883138cd36SMark Johnston * operation and allows the pages to be reused quickly under memory 19893138cd36SMark Johnston * pressure. If a page is wired for any reason, then it will not 19903138cd36SMark Johnston * be queued. Pages are specified by the range ["start", "end"). 19913138cd36SMark Johnston * As a special case, if "end" is zero, then the range extends from 19923138cd36SMark Johnston * "start" to the end of the object. 1993936c09acSJohn Baldwin * 1994936c09acSJohn Baldwin * This operation should only be performed on objects that 199528634820SAlan Cox * contain non-fictitious, managed pages. 1996936c09acSJohn Baldwin * 1997936c09acSJohn Baldwin * The object must be locked. 1998936c09acSJohn Baldwin */ 1999936c09acSJohn Baldwin void 20003138cd36SMark Johnston vm_object_page_noreuse(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 2001936c09acSJohn Baldwin { 200293c5d3a4SKonstantin Belousov struct mtx *mtx; 2003936c09acSJohn Baldwin vm_page_t p, next; 2004936c09acSJohn Baldwin 200552d1addaSAlan Cox VM_OBJECT_ASSERT_LOCKED(object); 200628634820SAlan Cox KASSERT((object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0, 20073138cd36SMark Johnston ("vm_object_page_noreuse: illegal object %p", object)); 2008936c09acSJohn Baldwin if (object->resident_page_count == 0) 2009936c09acSJohn Baldwin return; 2010936c09acSJohn Baldwin p = vm_page_find_least(object, start); 2011936c09acSJohn Baldwin 2012936c09acSJohn Baldwin /* 2013936c09acSJohn Baldwin * Here, the variable "p" is either (1) the page with the least pindex 2014936c09acSJohn Baldwin * greater than or equal to the parameter "start" or (2) NULL. 2015936c09acSJohn Baldwin */ 2016936c09acSJohn Baldwin mtx = NULL; 2017936c09acSJohn Baldwin for (; p != NULL && (p->pindex < end || end == 0); p = next) { 2018936c09acSJohn Baldwin next = TAILQ_NEXT(p, listq); 201993c5d3a4SKonstantin Belousov vm_page_change_lock(p, &mtx); 20203138cd36SMark Johnston vm_page_deactivate_noreuse(p); 2021936c09acSJohn Baldwin } 2022936c09acSJohn Baldwin if (mtx != NULL) 2023936c09acSJohn Baldwin mtx_unlock(mtx); 2024936c09acSJohn Baldwin } 2025936c09acSJohn Baldwin 2026936c09acSJohn Baldwin /* 2027387aabc5SAlan Cox * Populate the specified range of the object with valid pages. Returns 2028387aabc5SAlan Cox * TRUE if the range is successfully populated and FALSE otherwise. 2029387aabc5SAlan Cox * 2030387aabc5SAlan Cox * Note: This function should be optimized to pass a larger array of 2031387aabc5SAlan Cox * pages to vm_pager_get_pages() before it is applied to a non- 2032387aabc5SAlan Cox * OBJT_DEVICE object. 2033387aabc5SAlan Cox * 2034387aabc5SAlan Cox * The object must be locked. 2035387aabc5SAlan Cox */ 2036387aabc5SAlan Cox boolean_t 2037387aabc5SAlan Cox vm_object_populate(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 2038387aabc5SAlan Cox { 2039093c7f39SGleb Smirnoff vm_page_t m; 2040387aabc5SAlan Cox vm_pindex_t pindex; 2041387aabc5SAlan Cox int rv; 2042387aabc5SAlan Cox 204389f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 2044387aabc5SAlan Cox for (pindex = start; pindex < end; pindex++) { 2045c7575748SJeff Roberson rv = vm_page_grab_valid(&m, object, pindex, VM_ALLOC_NORMAL); 2046c7575748SJeff Roberson if (rv != VM_PAGER_OK) 2047387aabc5SAlan Cox break; 2048c7575748SJeff Roberson 2049387aabc5SAlan Cox /* 2050387aabc5SAlan Cox * Keep "m" busy because a subsequent iteration may unlock 2051387aabc5SAlan Cox * the object. 2052387aabc5SAlan Cox */ 2053387aabc5SAlan Cox } 2054387aabc5SAlan Cox if (pindex > start) { 2055387aabc5SAlan Cox m = vm_page_lookup(object, start); 2056387aabc5SAlan Cox while (m != NULL && m->pindex < pindex) { 2057c7aebda8SAttilio Rao vm_page_xunbusy(m); 2058387aabc5SAlan Cox m = TAILQ_NEXT(m, listq); 2059387aabc5SAlan Cox } 2060387aabc5SAlan Cox } 2061387aabc5SAlan Cox return (pindex == end); 2062387aabc5SAlan Cox } 2063387aabc5SAlan Cox 2064387aabc5SAlan Cox /* 2065df8bae1dSRodney W. Grimes * Routine: vm_object_coalesce 2066df8bae1dSRodney W. Grimes * Function: Coalesces two objects backing up adjoining 2067df8bae1dSRodney W. Grimes * regions of memory into a single object. 2068df8bae1dSRodney W. Grimes * 2069df8bae1dSRodney W. Grimes * returns TRUE if objects were combined. 2070df8bae1dSRodney W. Grimes * 2071df8bae1dSRodney W. Grimes * NOTE: Only works at the moment if the second object is NULL - 2072df8bae1dSRodney W. Grimes * if it's not, which object do we lock first? 2073df8bae1dSRodney W. Grimes * 2074df8bae1dSRodney W. Grimes * Parameters: 2075df8bae1dSRodney W. Grimes * prev_object First object to coalesce 2076df8bae1dSRodney W. Grimes * prev_offset Offset into prev_object 2077df8bae1dSRodney W. Grimes * prev_size Size of reference to prev_object 207857a21abaSAlan Cox * next_size Size of reference to the second object 20793364c323SKonstantin Belousov * reserved Indicator that extension region has 20803364c323SKonstantin Belousov * swap accounted for 2081df8bae1dSRodney W. Grimes * 2082df8bae1dSRodney W. Grimes * Conditions: 2083df8bae1dSRodney W. Grimes * The object must *not* be locked. 2084df8bae1dSRodney W. Grimes */ 20850d94caffSDavid Greenman boolean_t 208657a21abaSAlan Cox vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset, 20873364c323SKonstantin Belousov vm_size_t prev_size, vm_size_t next_size, boolean_t reserved) 2088df8bae1dSRodney W. Grimes { 2089ea41812fSAlan Cox vm_pindex_t next_pindex; 2090df8bae1dSRodney W. Grimes 209100e1854aSAlan Cox if (prev_object == NULL) 2092df8bae1dSRodney W. Grimes return (TRUE); 209363967687SJeff Roberson if ((prev_object->flags & OBJ_ANON) == 0) 209430dcfc09SJohn Dyson return (FALSE); 209530dcfc09SJohn Dyson 209663967687SJeff Roberson VM_OBJECT_WLOCK(prev_object); 2097df8bae1dSRodney W. Grimes /* 2098df8bae1dSRodney W. Grimes * Try to collapse the object first 2099df8bae1dSRodney W. Grimes */ 2100df8bae1dSRodney W. Grimes vm_object_collapse(prev_object); 2101df8bae1dSRodney W. Grimes 2102df8bae1dSRodney W. Grimes /* 21030d94caffSDavid Greenman * Can't coalesce if: . more than one reference . paged out . shadows 21040d94caffSDavid Greenman * another object . has a copy elsewhere (any of which mean that the 21050d94caffSDavid Greenman * pages not mapped to prev_entry may be in use anyway) 2106df8bae1dSRodney W. Grimes */ 21078cc7e047SJohn Dyson if (prev_object->backing_object != NULL) { 210889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(prev_object); 2109df8bae1dSRodney W. Grimes return (FALSE); 2110df8bae1dSRodney W. Grimes } 2111a316d390SJohn Dyson 2112a316d390SJohn Dyson prev_size >>= PAGE_SHIFT; 2113a316d390SJohn Dyson next_size >>= PAGE_SHIFT; 211457a21abaSAlan Cox next_pindex = OFF_TO_IDX(prev_offset) + prev_size; 21158cc7e047SJohn Dyson 21160e48e068SMark Johnston if (prev_object->ref_count > 1 && 21170e48e068SMark Johnston prev_object->size != next_pindex && 21180e48e068SMark Johnston (prev_object->flags & OBJ_ONEMAPPING) == 0) { 211989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(prev_object); 21208cc7e047SJohn Dyson return (FALSE); 21218cc7e047SJohn Dyson } 21228cc7e047SJohn Dyson 2123df8bae1dSRodney W. Grimes /* 21243364c323SKonstantin Belousov * Account for the charge. 21253364c323SKonstantin Belousov */ 2126ef694c1aSEdward Tomasz Napierala if (prev_object->cred != NULL) { 21273364c323SKonstantin Belousov 21283364c323SKonstantin Belousov /* 21293364c323SKonstantin Belousov * If prev_object was charged, then this mapping, 2130763df3ecSPedro F. Giffuni * although not charged now, may become writable 2131ef694c1aSEdward Tomasz Napierala * later. Non-NULL cred in the object would prevent 21323364c323SKonstantin Belousov * swap reservation during enabling of the write 21333364c323SKonstantin Belousov * access, so reserve swap now. Failed reservation 21343364c323SKonstantin Belousov * cause allocation of the separate object for the map 21353364c323SKonstantin Belousov * entry, and swap reservation for this entry is 21363364c323SKonstantin Belousov * managed in appropriate time. 21373364c323SKonstantin Belousov */ 2138ef694c1aSEdward Tomasz Napierala if (!reserved && !swap_reserve_by_cred(ptoa(next_size), 2139ef694c1aSEdward Tomasz Napierala prev_object->cred)) { 21409f790a17SKonstantin Belousov VM_OBJECT_WUNLOCK(prev_object); 21413364c323SKonstantin Belousov return (FALSE); 21423364c323SKonstantin Belousov } 21433364c323SKonstantin Belousov prev_object->charge += ptoa(next_size); 21443364c323SKonstantin Belousov } 21453364c323SKonstantin Belousov 21463364c323SKonstantin Belousov /* 21470d94caffSDavid Greenman * Remove any pages that may still be in the object from a previous 21480d94caffSDavid Greenman * deallocation. 2149df8bae1dSRodney W. Grimes */ 2150ea41812fSAlan Cox if (next_pindex < prev_object->size) { 21516bbee8e2SAlan Cox vm_object_page_remove(prev_object, next_pindex, next_pindex + 21526bbee8e2SAlan Cox next_size, 0); 2153ea41812fSAlan Cox if (prev_object->type == OBJT_SWAP) 2154ea41812fSAlan Cox swap_pager_freespace(prev_object, 2155ea41812fSAlan Cox next_pindex, next_size); 21563364c323SKonstantin Belousov #if 0 2157ef694c1aSEdward Tomasz Napierala if (prev_object->cred != NULL) { 21583364c323SKonstantin Belousov KASSERT(prev_object->charge >= 21593364c323SKonstantin Belousov ptoa(prev_object->size - next_pindex), 21603364c323SKonstantin Belousov ("object %p overcharged 1 %jx %jx", prev_object, 21613364c323SKonstantin Belousov (uintmax_t)next_pindex, (uintmax_t)next_size)); 21623364c323SKonstantin Belousov prev_object->charge -= ptoa(prev_object->size - 21633364c323SKonstantin Belousov next_pindex); 21643364c323SKonstantin Belousov } 21653364c323SKonstantin Belousov #endif 2166ea41812fSAlan Cox } 2167df8bae1dSRodney W. Grimes 2168df8bae1dSRodney W. Grimes /* 2169df8bae1dSRodney W. Grimes * Extend the object if necessary. 2170df8bae1dSRodney W. Grimes */ 2171ea41812fSAlan Cox if (next_pindex + next_size > prev_object->size) 2172ea41812fSAlan Cox prev_object->size = next_pindex + next_size; 2173df8bae1dSRodney W. Grimes 217489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(prev_object); 2175df8bae1dSRodney W. Grimes return (TRUE); 2176df8bae1dSRodney W. Grimes } 2177df8bae1dSRodney W. Grimes 21787a5a6352SMatthew Dillon void 21797a5a6352SMatthew Dillon vm_object_set_writeable_dirty(vm_object_t object) 21807a5a6352SMatthew Dillon { 21817a5a6352SMatthew Dillon 218267d0e293SJeff Roberson VM_OBJECT_ASSERT_LOCKED(object); 218367d0e293SJeff Roberson 218467d0e293SJeff Roberson /* Only set for vnodes & tmpfs */ 218567d0e293SJeff Roberson if (object->type != OBJT_VNODE && 218667d0e293SJeff Roberson (object->flags & OBJ_TMPFS_NODE) == 0) 21873280870dSKonstantin Belousov return; 218867d0e293SJeff Roberson atomic_add_int(&object->generation, 1); 21897a5a6352SMatthew Dillon } 21907a5a6352SMatthew Dillon 219103462509SAlan Cox /* 219203462509SAlan Cox * vm_object_unwire: 219303462509SAlan Cox * 219403462509SAlan Cox * For each page offset within the specified range of the given object, 219503462509SAlan Cox * find the highest-level page in the shadow chain and unwire it. A page 219603462509SAlan Cox * must exist at every page offset, and the highest-level page must be 219703462509SAlan Cox * wired. 219803462509SAlan Cox */ 219903462509SAlan Cox void 220003462509SAlan Cox vm_object_unwire(vm_object_t object, vm_ooffset_t offset, vm_size_t length, 220103462509SAlan Cox uint8_t queue) 220203462509SAlan Cox { 220320e4afbfSKonstantin Belousov vm_object_t tobject, t1object; 220403462509SAlan Cox vm_page_t m, tm; 220503462509SAlan Cox vm_pindex_t end_pindex, pindex, tpindex; 220603462509SAlan Cox int depth, locked_depth; 220703462509SAlan Cox 220803462509SAlan Cox KASSERT((offset & PAGE_MASK) == 0, 220903462509SAlan Cox ("vm_object_unwire: offset is not page aligned")); 221003462509SAlan Cox KASSERT((length & PAGE_MASK) == 0, 221103462509SAlan Cox ("vm_object_unwire: length is not a multiple of PAGE_SIZE")); 221203462509SAlan Cox /* The wired count of a fictitious page never changes. */ 221303462509SAlan Cox if ((object->flags & OBJ_FICTITIOUS) != 0) 221403462509SAlan Cox return; 221503462509SAlan Cox pindex = OFF_TO_IDX(offset); 221603462509SAlan Cox end_pindex = pindex + atop(length); 221720e4afbfSKonstantin Belousov again: 221803462509SAlan Cox locked_depth = 1; 221903462509SAlan Cox VM_OBJECT_RLOCK(object); 222003462509SAlan Cox m = vm_page_find_least(object, pindex); 222103462509SAlan Cox while (pindex < end_pindex) { 222203462509SAlan Cox if (m == NULL || pindex < m->pindex) { 222303462509SAlan Cox /* 222403462509SAlan Cox * The first object in the shadow chain doesn't 222503462509SAlan Cox * contain a page at the current index. Therefore, 222603462509SAlan Cox * the page must exist in a backing object. 222703462509SAlan Cox */ 222803462509SAlan Cox tobject = object; 222903462509SAlan Cox tpindex = pindex; 223003462509SAlan Cox depth = 0; 223103462509SAlan Cox do { 223203462509SAlan Cox tpindex += 223303462509SAlan Cox OFF_TO_IDX(tobject->backing_object_offset); 223403462509SAlan Cox tobject = tobject->backing_object; 223503462509SAlan Cox KASSERT(tobject != NULL, 223603462509SAlan Cox ("vm_object_unwire: missing page")); 223703462509SAlan Cox if ((tobject->flags & OBJ_FICTITIOUS) != 0) 223803462509SAlan Cox goto next_page; 223903462509SAlan Cox depth++; 224003462509SAlan Cox if (depth == locked_depth) { 224103462509SAlan Cox locked_depth++; 224203462509SAlan Cox VM_OBJECT_RLOCK(tobject); 224303462509SAlan Cox } 224403462509SAlan Cox } while ((tm = vm_page_lookup(tobject, tpindex)) == 224503462509SAlan Cox NULL); 224603462509SAlan Cox } else { 224703462509SAlan Cox tm = m; 224803462509SAlan Cox m = TAILQ_NEXT(m, listq); 224903462509SAlan Cox } 225063e97555SJeff Roberson if (vm_page_trysbusy(tm) == 0) { 225187e93ea6SMark Johnston for (tobject = object; locked_depth >= 1; 225220e4afbfSKonstantin Belousov locked_depth--) { 225320e4afbfSKonstantin Belousov t1object = tobject->backing_object; 225487e93ea6SMark Johnston if (tm->object != tobject) 225520e4afbfSKonstantin Belousov VM_OBJECT_RUNLOCK(tobject); 225620e4afbfSKonstantin Belousov tobject = t1object; 225720e4afbfSKonstantin Belousov } 225820e4afbfSKonstantin Belousov vm_page_busy_sleep(tm, "unwbo", true); 225920e4afbfSKonstantin Belousov goto again; 226020e4afbfSKonstantin Belousov } 226103462509SAlan Cox vm_page_unwire(tm, queue); 226263e97555SJeff Roberson vm_page_sunbusy(tm); 226303462509SAlan Cox next_page: 226403462509SAlan Cox pindex++; 226503462509SAlan Cox } 226603462509SAlan Cox /* Release the accumulated object locks. */ 226720e4afbfSKonstantin Belousov for (tobject = object; locked_depth >= 1; locked_depth--) { 226820e4afbfSKonstantin Belousov t1object = tobject->backing_object; 226920e4afbfSKonstantin Belousov VM_OBJECT_RUNLOCK(tobject); 227020e4afbfSKonstantin Belousov tobject = t1object; 227103462509SAlan Cox } 227203462509SAlan Cox } 227303462509SAlan Cox 22740951bd36SEric van Gyzen /* 22750951bd36SEric van Gyzen * Return the vnode for the given object, or NULL if none exists. 22760951bd36SEric van Gyzen * For tmpfs objects, the function may return NULL if there is 22770951bd36SEric van Gyzen * no vnode allocated at the time of the call. 22780951bd36SEric van Gyzen */ 227963e4c6cdSEric van Gyzen struct vnode * 228063e4c6cdSEric van Gyzen vm_object_vnode(vm_object_t object) 228163e4c6cdSEric van Gyzen { 22820951bd36SEric van Gyzen struct vnode *vp; 228363e4c6cdSEric van Gyzen 228463e4c6cdSEric van Gyzen VM_OBJECT_ASSERT_LOCKED(object); 22850951bd36SEric van Gyzen if (object->type == OBJT_VNODE) { 22860951bd36SEric van Gyzen vp = object->handle; 22870951bd36SEric van Gyzen KASSERT(vp != NULL, ("%s: OBJT_VNODE has no vnode", __func__)); 22880951bd36SEric van Gyzen } else if (object->type == OBJT_SWAP && 22890951bd36SEric van Gyzen (object->flags & OBJ_TMPFS) != 0) { 22900951bd36SEric van Gyzen vp = object->un_pager.swp.swp_tmpfs; 22910951bd36SEric van Gyzen KASSERT(vp != NULL, ("%s: OBJT_TMPFS has no vnode", __func__)); 22920951bd36SEric van Gyzen } else { 22930951bd36SEric van Gyzen vp = NULL; 22940951bd36SEric van Gyzen } 22950951bd36SEric van Gyzen return (vp); 229663e4c6cdSEric van Gyzen } 229763e4c6cdSEric van Gyzen 2298205be21dSJeff Roberson 2299205be21dSJeff Roberson /* 2300205be21dSJeff Roberson * Busy the vm object. This prevents new pages belonging to the object from 2301205be21dSJeff Roberson * becoming busy. Existing pages persist as busy. Callers are responsible 2302205be21dSJeff Roberson * for checking page state before proceeding. 2303205be21dSJeff Roberson */ 2304205be21dSJeff Roberson void 2305205be21dSJeff Roberson vm_object_busy(vm_object_t obj) 2306205be21dSJeff Roberson { 2307205be21dSJeff Roberson 2308205be21dSJeff Roberson VM_OBJECT_ASSERT_LOCKED(obj); 2309205be21dSJeff Roberson 2310205be21dSJeff Roberson refcount_acquire(&obj->busy); 2311205be21dSJeff Roberson /* The fence is required to order loads of page busy. */ 2312205be21dSJeff Roberson atomic_thread_fence_acq_rel(); 2313205be21dSJeff Roberson } 2314205be21dSJeff Roberson 2315205be21dSJeff Roberson void 2316205be21dSJeff Roberson vm_object_unbusy(vm_object_t obj) 2317205be21dSJeff Roberson { 2318205be21dSJeff Roberson 2319205be21dSJeff Roberson 2320205be21dSJeff Roberson refcount_release(&obj->busy); 2321205be21dSJeff Roberson } 2322205be21dSJeff Roberson 2323205be21dSJeff Roberson void 2324205be21dSJeff Roberson vm_object_busy_wait(vm_object_t obj, const char *wmesg) 2325205be21dSJeff Roberson { 2326205be21dSJeff Roberson 2327205be21dSJeff Roberson VM_OBJECT_ASSERT_UNLOCKED(obj); 2328205be21dSJeff Roberson 2329205be21dSJeff Roberson if (obj->busy) 2330205be21dSJeff Roberson refcount_sleep(&obj->busy, wmesg, PVM); 2331205be21dSJeff Roberson } 2332205be21dSJeff Roberson 23335e38e3f5SEric van Gyzen /* 23345e38e3f5SEric van Gyzen * Return the kvme type of the given object. 23355e38e3f5SEric van Gyzen * If vpp is not NULL, set it to the object's vm_object_vnode() or NULL. 23365e38e3f5SEric van Gyzen */ 23375e38e3f5SEric van Gyzen int 23385e38e3f5SEric van Gyzen vm_object_kvme_type(vm_object_t object, struct vnode **vpp) 23395e38e3f5SEric van Gyzen { 23405e38e3f5SEric van Gyzen 23415e38e3f5SEric van Gyzen VM_OBJECT_ASSERT_LOCKED(object); 23425e38e3f5SEric van Gyzen if (vpp != NULL) 23435e38e3f5SEric van Gyzen *vpp = vm_object_vnode(object); 23445e38e3f5SEric van Gyzen switch (object->type) { 23455e38e3f5SEric van Gyzen case OBJT_DEFAULT: 23465e38e3f5SEric van Gyzen return (KVME_TYPE_DEFAULT); 23475e38e3f5SEric van Gyzen case OBJT_VNODE: 23485e38e3f5SEric van Gyzen return (KVME_TYPE_VNODE); 23495e38e3f5SEric van Gyzen case OBJT_SWAP: 23505e38e3f5SEric van Gyzen if ((object->flags & OBJ_TMPFS_NODE) != 0) 23515e38e3f5SEric van Gyzen return (KVME_TYPE_VNODE); 23525e38e3f5SEric van Gyzen return (KVME_TYPE_SWAP); 23535e38e3f5SEric van Gyzen case OBJT_DEVICE: 23545e38e3f5SEric van Gyzen return (KVME_TYPE_DEVICE); 23555e38e3f5SEric van Gyzen case OBJT_PHYS: 23565e38e3f5SEric van Gyzen return (KVME_TYPE_PHYS); 23575e38e3f5SEric van Gyzen case OBJT_DEAD: 23585e38e3f5SEric van Gyzen return (KVME_TYPE_DEAD); 23595e38e3f5SEric van Gyzen case OBJT_SG: 23605e38e3f5SEric van Gyzen return (KVME_TYPE_SG); 23615e38e3f5SEric van Gyzen case OBJT_MGTDEVICE: 23625e38e3f5SEric van Gyzen return (KVME_TYPE_MGTDEVICE); 23635e38e3f5SEric van Gyzen default: 23645e38e3f5SEric van Gyzen return (KVME_TYPE_UNKNOWN); 23655e38e3f5SEric van Gyzen } 23665e38e3f5SEric van Gyzen } 23675e38e3f5SEric van Gyzen 2368ff87ae35SJohn Baldwin static int 2369ff87ae35SJohn Baldwin sysctl_vm_object_list(SYSCTL_HANDLER_ARGS) 2370ff87ae35SJohn Baldwin { 23710ecee546SKonstantin Belousov struct kinfo_vmobject *kvo; 2372ff87ae35SJohn Baldwin char *fullpath, *freepath; 2373ff87ae35SJohn Baldwin struct vnode *vp; 2374ff87ae35SJohn Baldwin struct vattr va; 2375ff87ae35SJohn Baldwin vm_object_t obj; 2376ff87ae35SJohn Baldwin vm_page_t m; 2377ff87ae35SJohn Baldwin int count, error; 2378ff87ae35SJohn Baldwin 2379ff87ae35SJohn Baldwin if (req->oldptr == NULL) { 2380ff87ae35SJohn Baldwin /* 2381ff87ae35SJohn Baldwin * If an old buffer has not been provided, generate an 2382ff87ae35SJohn Baldwin * estimate of the space needed for a subsequent call. 2383ff87ae35SJohn Baldwin */ 2384ff87ae35SJohn Baldwin mtx_lock(&vm_object_list_mtx); 2385ff87ae35SJohn Baldwin count = 0; 2386ff87ae35SJohn Baldwin TAILQ_FOREACH(obj, &vm_object_list, object_list) { 2387ff87ae35SJohn Baldwin if (obj->type == OBJT_DEAD) 2388ff87ae35SJohn Baldwin continue; 2389ff87ae35SJohn Baldwin count++; 2390ff87ae35SJohn Baldwin } 2391ff87ae35SJohn Baldwin mtx_unlock(&vm_object_list_mtx); 2392ff87ae35SJohn Baldwin return (SYSCTL_OUT(req, NULL, sizeof(struct kinfo_vmobject) * 2393ff87ae35SJohn Baldwin count * 11 / 10)); 2394ff87ae35SJohn Baldwin } 2395ff87ae35SJohn Baldwin 23960ecee546SKonstantin Belousov kvo = malloc(sizeof(*kvo), M_TEMP, M_WAITOK); 2397ff87ae35SJohn Baldwin error = 0; 2398ff87ae35SJohn Baldwin 2399ff87ae35SJohn Baldwin /* 2400ff87ae35SJohn Baldwin * VM objects are type stable and are never removed from the 2401ff87ae35SJohn Baldwin * list once added. This allows us to safely read obj->object_list 2402ff87ae35SJohn Baldwin * after reacquiring the VM object lock. 2403ff87ae35SJohn Baldwin */ 2404ff87ae35SJohn Baldwin mtx_lock(&vm_object_list_mtx); 2405ff87ae35SJohn Baldwin TAILQ_FOREACH(obj, &vm_object_list, object_list) { 2406ff87ae35SJohn Baldwin if (obj->type == OBJT_DEAD) 2407ff87ae35SJohn Baldwin continue; 2408ff87ae35SJohn Baldwin VM_OBJECT_RLOCK(obj); 2409ff87ae35SJohn Baldwin if (obj->type == OBJT_DEAD) { 2410ff87ae35SJohn Baldwin VM_OBJECT_RUNLOCK(obj); 2411ff87ae35SJohn Baldwin continue; 2412ff87ae35SJohn Baldwin } 2413ff87ae35SJohn Baldwin mtx_unlock(&vm_object_list_mtx); 24140ecee546SKonstantin Belousov kvo->kvo_size = ptoa(obj->size); 24150ecee546SKonstantin Belousov kvo->kvo_resident = obj->resident_page_count; 24160ecee546SKonstantin Belousov kvo->kvo_ref_count = obj->ref_count; 24170ecee546SKonstantin Belousov kvo->kvo_shadow_count = obj->shadow_count; 24180ecee546SKonstantin Belousov kvo->kvo_memattr = obj->memattr; 24190ecee546SKonstantin Belousov kvo->kvo_active = 0; 24200ecee546SKonstantin Belousov kvo->kvo_inactive = 0; 2421ff87ae35SJohn Baldwin TAILQ_FOREACH(m, &obj->memq, listq) { 2422ff87ae35SJohn Baldwin /* 2423ff87ae35SJohn Baldwin * A page may belong to the object but be 2424ff87ae35SJohn Baldwin * dequeued and set to PQ_NONE while the 2425ff87ae35SJohn Baldwin * object lock is not held. This makes the 2426ff87ae35SJohn Baldwin * reads of m->queue below racy, and we do not 2427ff87ae35SJohn Baldwin * count pages set to PQ_NONE. However, this 2428ff87ae35SJohn Baldwin * sysctl is only meant to give an 2429ff87ae35SJohn Baldwin * approximation of the system anyway. 2430ff87ae35SJohn Baldwin */ 2431e8bcf696SMark Johnston if (m->queue == PQ_ACTIVE) 24320ecee546SKonstantin Belousov kvo->kvo_active++; 2433e8bcf696SMark Johnston else if (m->queue == PQ_INACTIVE) 24340ecee546SKonstantin Belousov kvo->kvo_inactive++; 2435ff87ae35SJohn Baldwin } 2436ff87ae35SJohn Baldwin 24370ecee546SKonstantin Belousov kvo->kvo_vn_fileid = 0; 24380ecee546SKonstantin Belousov kvo->kvo_vn_fsid = 0; 24390ecee546SKonstantin Belousov kvo->kvo_vn_fsid_freebsd11 = 0; 2440ff87ae35SJohn Baldwin freepath = NULL; 2441ff87ae35SJohn Baldwin fullpath = ""; 24425e38e3f5SEric van Gyzen kvo->kvo_type = vm_object_kvme_type(obj, &vp); 24435e38e3f5SEric van Gyzen if (vp != NULL) 2444ff87ae35SJohn Baldwin vref(vp); 2445ff87ae35SJohn Baldwin VM_OBJECT_RUNLOCK(obj); 2446ff87ae35SJohn Baldwin if (vp != NULL) { 2447ff87ae35SJohn Baldwin vn_fullpath(curthread, vp, &fullpath, &freepath); 2448ff87ae35SJohn Baldwin vn_lock(vp, LK_SHARED | LK_RETRY); 2449ff87ae35SJohn Baldwin if (VOP_GETATTR(vp, &va, curthread->td_ucred) == 0) { 24500ecee546SKonstantin Belousov kvo->kvo_vn_fileid = va.va_fileid; 24510ecee546SKonstantin Belousov kvo->kvo_vn_fsid = va.va_fsid; 24520ecee546SKonstantin Belousov kvo->kvo_vn_fsid_freebsd11 = va.va_fsid; 245369921123SKonstantin Belousov /* truncate */ 2454ff87ae35SJohn Baldwin } 2455ff87ae35SJohn Baldwin vput(vp); 2456ff87ae35SJohn Baldwin } 2457ff87ae35SJohn Baldwin 24580ecee546SKonstantin Belousov strlcpy(kvo->kvo_path, fullpath, sizeof(kvo->kvo_path)); 2459ff87ae35SJohn Baldwin if (freepath != NULL) 2460ff87ae35SJohn Baldwin free(freepath, M_TEMP); 2461ff87ae35SJohn Baldwin 2462ff87ae35SJohn Baldwin /* Pack record size down */ 24630ecee546SKonstantin Belousov kvo->kvo_structsize = offsetof(struct kinfo_vmobject, kvo_path) 24640ecee546SKonstantin Belousov + strlen(kvo->kvo_path) + 1; 24650ecee546SKonstantin Belousov kvo->kvo_structsize = roundup(kvo->kvo_structsize, 2466ff87ae35SJohn Baldwin sizeof(uint64_t)); 24670ecee546SKonstantin Belousov error = SYSCTL_OUT(req, kvo, kvo->kvo_structsize); 2468ff87ae35SJohn Baldwin mtx_lock(&vm_object_list_mtx); 2469ff87ae35SJohn Baldwin if (error) 2470ff87ae35SJohn Baldwin break; 2471ff87ae35SJohn Baldwin } 2472ff87ae35SJohn Baldwin mtx_unlock(&vm_object_list_mtx); 24730ecee546SKonstantin Belousov free(kvo, M_TEMP); 2474ff87ae35SJohn Baldwin return (error); 2475ff87ae35SJohn Baldwin } 2476ff87ae35SJohn Baldwin SYSCTL_PROC(_vm, OID_AUTO, objects, CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_SKIP | 2477ff87ae35SJohn Baldwin CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_object_list, "S,kinfo_vmobject", 2478ff87ae35SJohn Baldwin "List of VM objects"); 2479ff87ae35SJohn Baldwin 2480c7c34a24SBruce Evans #include "opt_ddb.h" 2481c3cb3e12SDavid Greenman #ifdef DDB 2482c7c34a24SBruce Evans #include <sys/kernel.h> 2483c7c34a24SBruce Evans 2484ce9edcf5SPoul-Henning Kamp #include <sys/cons.h> 2485c7c34a24SBruce Evans 2486c7c34a24SBruce Evans #include <ddb/ddb.h> 2487c7c34a24SBruce Evans 2488cac597e4SBruce Evans static int 24891b40f8c0SMatthew Dillon _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry) 2490a1f6d91cSDavid Greenman { 2491a1f6d91cSDavid Greenman vm_map_t tmpm; 2492a1f6d91cSDavid Greenman vm_map_entry_t tmpe; 2493a1f6d91cSDavid Greenman vm_object_t obj; 2494a1f6d91cSDavid Greenman 2495a1f6d91cSDavid Greenman if (map == 0) 2496a1f6d91cSDavid Greenman return 0; 2497a1f6d91cSDavid Greenman 2498a1f6d91cSDavid Greenman if (entry == 0) { 24992288078cSDoug Moore VM_MAP_ENTRY_FOREACH(tmpe, map) { 2500a1f6d91cSDavid Greenman if (_vm_object_in_map(map, object, tmpe)) { 2501a1f6d91cSDavid Greenman return 1; 2502a1f6d91cSDavid Greenman } 2503a1f6d91cSDavid Greenman } 25049fdfe602SMatthew Dillon } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 25059fdfe602SMatthew Dillon tmpm = entry->object.sub_map; 25062288078cSDoug Moore VM_MAP_ENTRY_FOREACH(tmpe, tmpm) { 2507a1f6d91cSDavid Greenman if (_vm_object_in_map(tmpm, object, tmpe)) { 2508a1f6d91cSDavid Greenman return 1; 2509a1f6d91cSDavid Greenman } 2510a1f6d91cSDavid Greenman } 25118aef1712SMatthew Dillon } else if ((obj = entry->object.vm_object) != NULL) { 251224a1cce3SDavid Greenman for (; obj; obj = obj->backing_object) 2513a1f6d91cSDavid Greenman if (obj == object) { 2514a1f6d91cSDavid Greenman return 1; 2515a1f6d91cSDavid Greenman } 2516a1f6d91cSDavid Greenman } 2517a1f6d91cSDavid Greenman return 0; 2518a1f6d91cSDavid Greenman } 2519a1f6d91cSDavid Greenman 2520cac597e4SBruce Evans static int 25211b40f8c0SMatthew Dillon vm_object_in_map(vm_object_t object) 2522a1f6d91cSDavid Greenman { 2523a1f6d91cSDavid Greenman struct proc *p; 25241005a129SJohn Baldwin 252560517fd1SJohn Baldwin /* sx_slock(&allproc_lock); */ 2526f67af5c9SXin LI FOREACH_PROC_IN_SYSTEM(p) { 2527a1f6d91cSDavid Greenman if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */) 2528a1f6d91cSDavid Greenman continue; 2529553629ebSJake Burkholder if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) { 253060517fd1SJohn Baldwin /* sx_sunlock(&allproc_lock); */ 2531a1f6d91cSDavid Greenman return 1; 2532a1f6d91cSDavid Greenman } 2533553629ebSJake Burkholder } 253460517fd1SJohn Baldwin /* sx_sunlock(&allproc_lock); */ 2535a1f6d91cSDavid Greenman if (_vm_object_in_map(kernel_map, object, 0)) 2536a1f6d91cSDavid Greenman return 1; 2537a1f6d91cSDavid Greenman return 0; 2538a1f6d91cSDavid Greenman } 2539a1f6d91cSDavid Greenman 2540c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check) 2541f708ef1bSPoul-Henning Kamp { 2542a1f6d91cSDavid Greenman vm_object_t object; 2543a1f6d91cSDavid Greenman 2544a1f6d91cSDavid Greenman /* 2545a1f6d91cSDavid Greenman * make sure that internal objs are in a map somewhere 2546a1f6d91cSDavid Greenman * and none have zero ref counts. 2547a1f6d91cSDavid Greenman */ 2548cc64b484SAlfred Perlstein TAILQ_FOREACH(object, &vm_object_list, object_list) { 254932362449SKonstantin Belousov if ((object->flags & OBJ_ANON) != 0) { 2550a1f6d91cSDavid Greenman if (object->ref_count == 0) { 25513efc015bSPeter Wemm db_printf("vmochk: internal obj has zero ref count: %ld\n", 25523efc015bSPeter Wemm (long)object->size); 2553a1f6d91cSDavid Greenman } 2554a1f6d91cSDavid Greenman if (!vm_object_in_map(object)) { 2555fc62ef1fSBruce Evans db_printf( 2556fc62ef1fSBruce Evans "vmochk: internal obj is not in a map: " 2557fc62ef1fSBruce Evans "ref: %d, size: %lu: 0x%lx, backing_object: %p\n", 2558fc62ef1fSBruce Evans object->ref_count, (u_long)object->size, 2559fc62ef1fSBruce Evans (u_long)object->size, 2560fc62ef1fSBruce Evans (void *)object->backing_object); 2561a1f6d91cSDavid Greenman } 2562a1f6d91cSDavid Greenman } 2563a1f6d91cSDavid Greenman } 2564a1f6d91cSDavid Greenman } 2565a1f6d91cSDavid Greenman 256626f9a767SRodney W. Grimes /* 2567df8bae1dSRodney W. Grimes * vm_object_print: [ debug ] 2568df8bae1dSRodney W. Grimes */ 2569c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static) 2570df8bae1dSRodney W. Grimes { 2571c7c34a24SBruce Evans /* XXX convert args. */ 2572c7c34a24SBruce Evans vm_object_t object = (vm_object_t)addr; 2573c7c34a24SBruce Evans boolean_t full = have_addr; 2574c7c34a24SBruce Evans 2575d031cff1SMatthew Dillon vm_page_t p; 2576df8bae1dSRodney W. Grimes 2577c7c34a24SBruce Evans /* XXX count is an (unused) arg. Avoid shadowing it. */ 2578c7c34a24SBruce Evans #define count was_count 2579c7c34a24SBruce Evans 2580d031cff1SMatthew Dillon int count; 2581df8bae1dSRodney W. Grimes 2582df8bae1dSRodney W. Grimes if (object == NULL) 2583df8bae1dSRodney W. Grimes return; 2584df8bae1dSRodney W. Grimes 2585eb95adefSBruce Evans db_iprintf( 2586ef694c1aSEdward Tomasz Napierala "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x ruid %d charge %jx\n", 2587e47cd172SMaxime Henrion object, (int)object->type, (uintmax_t)object->size, 25883364c323SKonstantin Belousov object->resident_page_count, object->ref_count, object->flags, 2589ef694c1aSEdward Tomasz Napierala object->cred ? object->cred->cr_ruid : -1, (uintmax_t)object->charge); 2590e47cd172SMaxime Henrion db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n", 25911c7c3c6aSMatthew Dillon object->shadow_count, 2592eb95adefSBruce Evans object->backing_object ? object->backing_object->ref_count : 0, 2593e47cd172SMaxime Henrion object->backing_object, (uintmax_t)object->backing_object_offset); 2594df8bae1dSRodney W. Grimes 2595df8bae1dSRodney W. Grimes if (!full) 2596df8bae1dSRodney W. Grimes return; 2597df8bae1dSRodney W. Grimes 2598c7c34a24SBruce Evans db_indent += 2; 2599df8bae1dSRodney W. Grimes count = 0; 2600fc2ffbe6SPoul-Henning Kamp TAILQ_FOREACH(p, &object->memq, listq) { 2601df8bae1dSRodney W. Grimes if (count == 0) 2602c7c34a24SBruce Evans db_iprintf("memory:="); 2603df8bae1dSRodney W. Grimes else if (count == 6) { 2604c7c34a24SBruce Evans db_printf("\n"); 2605c7c34a24SBruce Evans db_iprintf(" ..."); 2606df8bae1dSRodney W. Grimes count = 0; 2607df8bae1dSRodney W. Grimes } else 2608c7c34a24SBruce Evans db_printf(","); 2609df8bae1dSRodney W. Grimes count++; 2610df8bae1dSRodney W. Grimes 2611e47cd172SMaxime Henrion db_printf("(off=0x%jx,page=0x%jx)", 2612e47cd172SMaxime Henrion (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p)); 2613df8bae1dSRodney W. Grimes } 2614df8bae1dSRodney W. Grimes if (count != 0) 2615c7c34a24SBruce Evans db_printf("\n"); 2616c7c34a24SBruce Evans db_indent -= 2; 2617df8bae1dSRodney W. Grimes } 26185070c7f8SJohn Dyson 2619c7c34a24SBruce Evans /* XXX. */ 2620c7c34a24SBruce Evans #undef count 2621c7c34a24SBruce Evans 2622c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */ 26235070c7f8SJohn Dyson void 26241b40f8c0SMatthew Dillon vm_object_print( 26251b40f8c0SMatthew Dillon /* db_expr_t */ long addr, 26261b40f8c0SMatthew Dillon boolean_t have_addr, 26271b40f8c0SMatthew Dillon /* db_expr_t */ long count, 26281b40f8c0SMatthew Dillon char *modif) 2629c7c34a24SBruce Evans { 2630c7c34a24SBruce Evans vm_object_print_static(addr, have_addr, count, modif); 2631c7c34a24SBruce Evans } 2632c7c34a24SBruce Evans 2633c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages) 26345070c7f8SJohn Dyson { 26355070c7f8SJohn Dyson vm_object_t object; 2636bb2ac86fSKonstantin Belousov vm_pindex_t fidx; 2637bb2ac86fSKonstantin Belousov vm_paddr_t pa; 2638bb2ac86fSKonstantin Belousov vm_page_t m, prev_m; 2639bb2ac86fSKonstantin Belousov int rcount, nl, c; 2640cc64b484SAlfred Perlstein 2641bb2ac86fSKonstantin Belousov nl = 0; 2642cc64b484SAlfred Perlstein TAILQ_FOREACH(object, &vm_object_list, object_list) { 2643fc62ef1fSBruce Evans db_printf("new object: %p\n", (void *)object); 26445070c7f8SJohn Dyson if (nl > 18) { 26455070c7f8SJohn Dyson c = cngetc(); 26465070c7f8SJohn Dyson if (c != ' ') 26475070c7f8SJohn Dyson return; 26485070c7f8SJohn Dyson nl = 0; 26495070c7f8SJohn Dyson } 26505070c7f8SJohn Dyson nl++; 26515070c7f8SJohn Dyson rcount = 0; 26525070c7f8SJohn Dyson fidx = 0; 2653bb2ac86fSKonstantin Belousov pa = -1; 2654bb2ac86fSKonstantin Belousov TAILQ_FOREACH(m, &object->memq, listq) { 2655bb2ac86fSKonstantin Belousov if (m->pindex > 128) 2656bb2ac86fSKonstantin Belousov break; 2657bb2ac86fSKonstantin Belousov if ((prev_m = TAILQ_PREV(m, pglist, listq)) != NULL && 2658bb2ac86fSKonstantin Belousov prev_m->pindex + 1 != m->pindex) { 26595070c7f8SJohn Dyson if (rcount) { 26603efc015bSPeter Wemm db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 26613efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 26625070c7f8SJohn Dyson if (nl > 18) { 26635070c7f8SJohn Dyson c = cngetc(); 26645070c7f8SJohn Dyson if (c != ' ') 26655070c7f8SJohn Dyson return; 26665070c7f8SJohn Dyson nl = 0; 26675070c7f8SJohn Dyson } 26685070c7f8SJohn Dyson nl++; 26695070c7f8SJohn Dyson rcount = 0; 26705070c7f8SJohn Dyson } 26715070c7f8SJohn Dyson } 26725070c7f8SJohn Dyson if (rcount && 26735070c7f8SJohn Dyson (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) { 26745070c7f8SJohn Dyson ++rcount; 26755070c7f8SJohn Dyson continue; 26765070c7f8SJohn Dyson } 26775070c7f8SJohn Dyson if (rcount) { 26782446e4f0SAlan Cox db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 26793efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 26805070c7f8SJohn Dyson if (nl > 18) { 26815070c7f8SJohn Dyson c = cngetc(); 26825070c7f8SJohn Dyson if (c != ' ') 26835070c7f8SJohn Dyson return; 26845070c7f8SJohn Dyson nl = 0; 26855070c7f8SJohn Dyson } 26865070c7f8SJohn Dyson nl++; 26875070c7f8SJohn Dyson } 2688bb2ac86fSKonstantin Belousov fidx = m->pindex; 26895070c7f8SJohn Dyson pa = VM_PAGE_TO_PHYS(m); 26905070c7f8SJohn Dyson rcount = 1; 26915070c7f8SJohn Dyson } 26925070c7f8SJohn Dyson if (rcount) { 26933efc015bSPeter Wemm db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 26943efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 26955070c7f8SJohn Dyson if (nl > 18) { 26965070c7f8SJohn Dyson c = cngetc(); 26975070c7f8SJohn Dyson if (c != ' ') 26985070c7f8SJohn Dyson return; 26995070c7f8SJohn Dyson nl = 0; 27005070c7f8SJohn Dyson } 27015070c7f8SJohn Dyson nl++; 27025070c7f8SJohn Dyson } 27035070c7f8SJohn Dyson } 27045070c7f8SJohn Dyson } 2705c3cb3e12SDavid Greenman #endif /* DDB */ 2706