1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 6df8bae1dSRodney W. Grimes * The Mach Operating System project at Carnegie-Mellon University. 7df8bae1dSRodney W. Grimes * 8df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 9df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 10df8bae1dSRodney W. Grimes * are met: 11df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 12df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 13df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 15df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 16df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 175929bcfaSPhilippe Charnier * must display the following acknowledgement: 18df8bae1dSRodney W. Grimes * This product includes software developed by the University of 19df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 20df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 21df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 22df8bae1dSRodney W. Grimes * without specific prior written permission. 23df8bae1dSRodney W. Grimes * 24df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34df8bae1dSRodney W. Grimes * SUCH DAMAGE. 35df8bae1dSRodney W. Grimes * 363c4dd356SDavid Greenman * from: @(#)vm_object.c 8.5 (Berkeley) 3/22/94 37df8bae1dSRodney W. Grimes * 38df8bae1dSRodney W. Grimes * 39df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 40df8bae1dSRodney W. Grimes * All rights reserved. 41df8bae1dSRodney W. Grimes * 42df8bae1dSRodney W. Grimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 43df8bae1dSRodney W. Grimes * 44df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and 45df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright 46df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the 47df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions 48df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 49df8bae1dSRodney W. Grimes * 50df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 51df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 52df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 53df8bae1dSRodney W. Grimes * 54df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to 55df8bae1dSRodney W. Grimes * 56df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 57df8bae1dSRodney W. Grimes * School of Computer Science 58df8bae1dSRodney W. Grimes * Carnegie Mellon University 59df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890 60df8bae1dSRodney W. Grimes * 61df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 62df8bae1dSRodney W. Grimes * rights to redistribute these changes. 633c4dd356SDavid Greenman * 64c3aac50fSPeter Wemm * $FreeBSD$ 65df8bae1dSRodney W. Grimes */ 66df8bae1dSRodney W. Grimes 67df8bae1dSRodney W. Grimes /* 68df8bae1dSRodney W. Grimes * Virtual memory object module. 69df8bae1dSRodney W. Grimes */ 70df8bae1dSRodney W. Grimes 71df8bae1dSRodney W. Grimes #include <sys/param.h> 72df8bae1dSRodney W. Grimes #include <sys/systm.h> 73fb919e4dSMark Murray #include <sys/lock.h> 74867a482dSJohn Dyson #include <sys/mman.h> 75cf2819ccSJohn Dyson #include <sys/mount.h> 76b9b7a4beSMatthew Dillon #include <sys/kernel.h> 77b9b7a4beSMatthew Dillon #include <sys/sysctl.h> 781b367556SJason Evans #include <sys/mutex.h> 79fb919e4dSMark Murray #include <sys/proc.h> /* for curproc, pageproc */ 80fb919e4dSMark Murray #include <sys/socket.h> 81e47cd172SMaxime Henrion #include <sys/stdint.h> 82fb919e4dSMark Murray #include <sys/vnode.h> 83fb919e4dSMark Murray #include <sys/vmmeter.h> 841005a129SJohn Baldwin #include <sys/sx.h> 85df8bae1dSRodney W. Grimes 86df8bae1dSRodney W. Grimes #include <vm/vm.h> 87efeaf95aSDavid Greenman #include <vm/vm_param.h> 88efeaf95aSDavid Greenman #include <vm/pmap.h> 89efeaf95aSDavid Greenman #include <vm/vm_map.h> 90efeaf95aSDavid Greenman #include <vm/vm_object.h> 91df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 9226f9a767SRodney W. Grimes #include <vm/vm_pageout.h> 930d94caffSDavid Greenman #include <vm/vm_pager.h> 9405f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h> 95a1f6d91cSDavid Greenman #include <vm/vm_kern.h> 96efeaf95aSDavid Greenman #include <vm/vm_extern.h> 97670d17b5SJeff Roberson #include <vm/uma.h> 9826f9a767SRodney W. Grimes 99b9b7a4beSMatthew Dillon #define EASY_SCAN_FACTOR 8 100b9b7a4beSMatthew Dillon 101b9b7a4beSMatthew Dillon #define MSYNC_FLUSH_HARDSEQ 0x01 102b9b7a4beSMatthew Dillon #define MSYNC_FLUSH_SOFTSEQ 0x02 103b9b7a4beSMatthew Dillon 104b9b7a4beSMatthew Dillon /* 105b9b7a4beSMatthew Dillon * msync / VM object flushing optimizations 106b9b7a4beSMatthew Dillon */ 107b9b7a4beSMatthew Dillon static int msync_flush_flags = MSYNC_FLUSH_HARDSEQ | MSYNC_FLUSH_SOFTSEQ; 108b9b7a4beSMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, msync_flush_flags, 109b9b7a4beSMatthew Dillon CTLFLAG_RW, &msync_flush_flags, 0, ""); 110b9b7a4beSMatthew Dillon 111b9b7a4beSMatthew Dillon static void vm_object_qcollapse(vm_object_t object); 112b9b7a4beSMatthew Dillon static int vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags); 113f6b04d2bSDavid Greenman 114df8bae1dSRodney W. Grimes /* 115df8bae1dSRodney W. Grimes * Virtual memory objects maintain the actual data 116df8bae1dSRodney W. Grimes * associated with allocated virtual memory. A given 117df8bae1dSRodney W. Grimes * page of memory exists within exactly one object. 118df8bae1dSRodney W. Grimes * 119df8bae1dSRodney W. Grimes * An object is only deallocated when all "references" 120df8bae1dSRodney W. Grimes * are given up. Only one "reference" to a given 121df8bae1dSRodney W. Grimes * region of an object should be writeable. 122df8bae1dSRodney W. Grimes * 123df8bae1dSRodney W. Grimes * Associated with each object is a list of all resident 124df8bae1dSRodney W. Grimes * memory pages belonging to that object; this list is 125df8bae1dSRodney W. Grimes * maintained by the "vm_page" module, and locked by the object's 126df8bae1dSRodney W. Grimes * lock. 127df8bae1dSRodney W. Grimes * 128df8bae1dSRodney W. Grimes * Each object also records a "pager" routine which is 129df8bae1dSRodney W. Grimes * used to retrieve (and store) pages to the proper backing 130df8bae1dSRodney W. Grimes * storage. In addition, objects may be backed by other 131df8bae1dSRodney W. Grimes * objects from which they were virtual-copied. 132df8bae1dSRodney W. Grimes * 133df8bae1dSRodney W. Grimes * The only items within the object structure which are 134df8bae1dSRodney W. Grimes * modified after time of creation are: 135df8bae1dSRodney W. Grimes * reference count locked by object's lock 136df8bae1dSRodney W. Grimes * pager routine locked by object's lock 137df8bae1dSRodney W. Grimes * 138df8bae1dSRodney W. Grimes */ 139df8bae1dSRodney W. Grimes 14028f8db14SBruce Evans struct object_q vm_object_list; 141a5698387SAlan Cox struct mtx vm_object_list_mtx; /* lock for object list and count */ 14228f8db14SBruce Evans vm_object_t kernel_object; 14328f8db14SBruce Evans vm_object_t kmem_object; 144f708ef1bSPoul-Henning Kamp static struct vm_object kernel_object_store; 145f708ef1bSPoul-Henning Kamp static struct vm_object kmem_object_store; 146aef922f5SJohn Dyson extern int vm_pageout_page_count; 147df8bae1dSRodney W. Grimes 148f708ef1bSPoul-Henning Kamp static long object_collapses; 149f708ef1bSPoul-Henning Kamp static long object_bypasses; 150dcc5840eSAlan Cox static int next_index; 151670d17b5SJeff Roberson static uma_zone_t obj_zone; 15299448ed1SJohn Dyson #define VM_OBJECTS_INIT 256 1538355f576SJeff Roberson 1548355f576SJeff Roberson static void vm_object_zinit(void *mem, int size); 1558355f576SJeff Roberson 1568355f576SJeff Roberson #ifdef INVARIANTS 1578355f576SJeff Roberson static void vm_object_zdtor(void *mem, int size, void *arg); 1588355f576SJeff Roberson 1598355f576SJeff Roberson static void 1608355f576SJeff Roberson vm_object_zdtor(void *mem, int size, void *arg) 1618355f576SJeff Roberson { 1628355f576SJeff Roberson vm_object_t object; 1638355f576SJeff Roberson 1648355f576SJeff Roberson object = (vm_object_t)mem; 1658355f576SJeff Roberson KASSERT(object->paging_in_progress == 0, 1668355f576SJeff Roberson ("object %p paging_in_progress = %d", 1678355f576SJeff Roberson object, object->paging_in_progress)); 1688355f576SJeff Roberson KASSERT(object->resident_page_count == 0, 1698355f576SJeff Roberson ("object %p resident_page_count = %d", 1708355f576SJeff Roberson object, object->resident_page_count)); 1718355f576SJeff Roberson KASSERT(object->shadow_count == 0, 1728355f576SJeff Roberson ("object %p shadow_count = %d", 1738355f576SJeff Roberson object, object->shadow_count)); 1748355f576SJeff Roberson } 1758355f576SJeff Roberson #endif 1768355f576SJeff Roberson 1778355f576SJeff Roberson static void 1788355f576SJeff Roberson vm_object_zinit(void *mem, int size) 1798355f576SJeff Roberson { 1808355f576SJeff Roberson vm_object_t object; 1818355f576SJeff Roberson 1828355f576SJeff Roberson object = (vm_object_t)mem; 1838355f576SJeff Roberson 1848355f576SJeff Roberson /* These are true for any object that has been freed */ 1858355f576SJeff Roberson object->paging_in_progress = 0; 1868355f576SJeff Roberson object->resident_page_count = 0; 1878355f576SJeff Roberson object->shadow_count = 0; 1888355f576SJeff Roberson } 189df8bae1dSRodney W. Grimes 1903075778bSJohn Dyson void 1916395da54SIan Dowse _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object) 192df8bae1dSRodney W. Grimes { 19379660d83SAlan Cox static int object_hash_rand; 19479660d83SAlan Cox int exp, incr; 1950cddd8f0SMatthew Dillon 196df8bae1dSRodney W. Grimes TAILQ_INIT(&object->memq); 19724a1cce3SDavid Greenman TAILQ_INIT(&object->shadow_head); 198a1f6d91cSDavid Greenman 199b86ec922SMatthew Dillon object->root = NULL; 20024a1cce3SDavid Greenman object->type = type; 201df8bae1dSRodney W. Grimes object->size = size; 202a1f6d91cSDavid Greenman object->ref_count = 1; 20324a1cce3SDavid Greenman object->flags = 0; 20460517fd1SJohn Baldwin if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP)) 20560517fd1SJohn Baldwin vm_object_set_flag(object, OBJ_ONEMAPPING); 20699448ed1SJohn Dyson if (size > (PQ_L2_SIZE / 3 + PQ_PRIME1)) 20799448ed1SJohn Dyson incr = PQ_L2_SIZE / 3 + PQ_PRIME1; 20899448ed1SJohn Dyson else 20999448ed1SJohn Dyson incr = size; 210dcc5840eSAlan Cox do 211dcc5840eSAlan Cox object->pg_color = next_index; 212dcc5840eSAlan Cox while (!atomic_cmpset_int(&next_index, object->pg_color, 21379660d83SAlan Cox (object->pg_color + incr) & PQ_L2_MASK)); 21424a1cce3SDavid Greenman object->handle = NULL; 21524a1cce3SDavid Greenman object->backing_object = NULL; 216a316d390SJohn Dyson object->backing_object_offset = (vm_ooffset_t) 0; 2171c7c3c6aSMatthew Dillon /* 2181c7c3c6aSMatthew Dillon * Try to generate a number that will spread objects out in the 2191c7c3c6aSMatthew Dillon * hash table. We 'wipe' new objects across the hash in 128 page 2201c7c3c6aSMatthew Dillon * increments plus 1 more to offset it a little more by the time 2211c7c3c6aSMatthew Dillon * it wraps around. 2221c7c3c6aSMatthew Dillon */ 22379660d83SAlan Cox do { 22479660d83SAlan Cox exp = object_hash_rand; 22579660d83SAlan Cox object->hash_rand = exp - 129; 22679660d83SAlan Cox } while (!atomic_cmpset_int(&object_hash_rand, exp, object->hash_rand)); 227a1f6d91cSDavid Greenman 22879660d83SAlan Cox object->generation++; /* atomicity needed? XXX */ 22960517fd1SJohn Baldwin 230a5698387SAlan Cox mtx_lock(&vm_object_list_mtx); 23160517fd1SJohn Baldwin TAILQ_INSERT_TAIL(&vm_object_list, object, object_list); 232a5698387SAlan Cox mtx_unlock(&vm_object_list_mtx); 233df8bae1dSRodney W. Grimes } 234df8bae1dSRodney W. Grimes 235df8bae1dSRodney W. Grimes /* 23626f9a767SRodney W. Grimes * vm_object_init: 23726f9a767SRodney W. Grimes * 23826f9a767SRodney W. Grimes * Initialize the VM objects module. 23926f9a767SRodney W. Grimes */ 24026f9a767SRodney W. Grimes void 2411b40f8c0SMatthew Dillon vm_object_init(void) 24226f9a767SRodney W. Grimes { 24326f9a767SRodney W. Grimes TAILQ_INIT(&vm_object_list); 2446008862bSJohn Baldwin mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF); 2450217125fSDavid Greenman 24626f9a767SRodney W. Grimes kernel_object = &kernel_object_store; 247a316d390SJohn Dyson _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS), 24826f9a767SRodney W. Grimes kernel_object); 24926f9a767SRodney W. Grimes 25026f9a767SRodney W. Grimes kmem_object = &kmem_object_store; 251a316d390SJohn Dyson _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS), 25226f9a767SRodney W. Grimes kmem_object); 2538355f576SJeff Roberson obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL, 2548355f576SJeff Roberson #ifdef INVARIANTS 2558355f576SJeff Roberson vm_object_zdtor, 2568355f576SJeff Roberson #else 2578355f576SJeff Roberson NULL, 2588355f576SJeff Roberson #endif 2598355f576SJeff Roberson vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 2608355f576SJeff Roberson uma_prealloc(obj_zone, VM_OBJECTS_INIT); 26199448ed1SJohn Dyson } 26299448ed1SJohn Dyson 26399448ed1SJohn Dyson void 2641b40f8c0SMatthew Dillon vm_object_init2(void) 26560517fd1SJohn Baldwin { 26626f9a767SRodney W. Grimes } 26726f9a767SRodney W. Grimes 2681b40f8c0SMatthew Dillon void 2691b40f8c0SMatthew Dillon vm_object_set_flag(vm_object_t object, u_short bits) 2701b40f8c0SMatthew Dillon { 271b06805adSJake Burkholder object->flags |= bits; 2721b40f8c0SMatthew Dillon } 2731b40f8c0SMatthew Dillon 2741b40f8c0SMatthew Dillon void 2751b40f8c0SMatthew Dillon vm_object_clear_flag(vm_object_t object, u_short bits) 2761b40f8c0SMatthew Dillon { 2771b40f8c0SMatthew Dillon GIANT_REQUIRED; 278b06805adSJake Burkholder object->flags &= ~bits; 2791b40f8c0SMatthew Dillon } 2801b40f8c0SMatthew Dillon 2811b40f8c0SMatthew Dillon void 2821b40f8c0SMatthew Dillon vm_object_pip_add(vm_object_t object, short i) 2831b40f8c0SMatthew Dillon { 2841b40f8c0SMatthew Dillon GIANT_REQUIRED; 285b06805adSJake Burkholder object->paging_in_progress += i; 2861b40f8c0SMatthew Dillon } 2871b40f8c0SMatthew Dillon 2881b40f8c0SMatthew Dillon void 2891b40f8c0SMatthew Dillon vm_object_pip_subtract(vm_object_t object, short i) 2901b40f8c0SMatthew Dillon { 2911b40f8c0SMatthew Dillon GIANT_REQUIRED; 292b06805adSJake Burkholder object->paging_in_progress -= i; 2931b40f8c0SMatthew Dillon } 2941b40f8c0SMatthew Dillon 2951b40f8c0SMatthew Dillon void 2961b40f8c0SMatthew Dillon vm_object_pip_wakeup(vm_object_t object) 2971b40f8c0SMatthew Dillon { 2981b40f8c0SMatthew Dillon GIANT_REQUIRED; 299b06805adSJake Burkholder object->paging_in_progress--; 3001b40f8c0SMatthew Dillon if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) { 3011b40f8c0SMatthew Dillon vm_object_clear_flag(object, OBJ_PIPWNT); 3021b40f8c0SMatthew Dillon wakeup(object); 3031b40f8c0SMatthew Dillon } 3041b40f8c0SMatthew Dillon } 3051b40f8c0SMatthew Dillon 3061b40f8c0SMatthew Dillon void 3071b40f8c0SMatthew Dillon vm_object_pip_wakeupn(vm_object_t object, short i) 3081b40f8c0SMatthew Dillon { 3091b40f8c0SMatthew Dillon GIANT_REQUIRED; 3101b40f8c0SMatthew Dillon if (i) 311b06805adSJake Burkholder object->paging_in_progress -= i; 3121b40f8c0SMatthew Dillon if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) { 3131b40f8c0SMatthew Dillon vm_object_clear_flag(object, OBJ_PIPWNT); 3141b40f8c0SMatthew Dillon wakeup(object); 3151b40f8c0SMatthew Dillon } 3161b40f8c0SMatthew Dillon } 3171b40f8c0SMatthew Dillon 3181b40f8c0SMatthew Dillon void 3191b40f8c0SMatthew Dillon vm_object_pip_sleep(vm_object_t object, char *waitid) 3201b40f8c0SMatthew Dillon { 3211b40f8c0SMatthew Dillon GIANT_REQUIRED; 3221b40f8c0SMatthew Dillon if (object->paging_in_progress) { 3231b40f8c0SMatthew Dillon int s = splvm(); 3241b40f8c0SMatthew Dillon if (object->paging_in_progress) { 3251b40f8c0SMatthew Dillon vm_object_set_flag(object, OBJ_PIPWNT); 3261b40f8c0SMatthew Dillon tsleep(object, PVM, waitid, 0); 3271b40f8c0SMatthew Dillon } 3281b40f8c0SMatthew Dillon splx(s); 3291b40f8c0SMatthew Dillon } 3301b40f8c0SMatthew Dillon } 3311b40f8c0SMatthew Dillon 3321b40f8c0SMatthew Dillon void 3331b40f8c0SMatthew Dillon vm_object_pip_wait(vm_object_t object, char *waitid) 3341b40f8c0SMatthew Dillon { 3351b40f8c0SMatthew Dillon GIANT_REQUIRED; 3361b40f8c0SMatthew Dillon while (object->paging_in_progress) 3371b40f8c0SMatthew Dillon vm_object_pip_sleep(object, waitid); 3381b40f8c0SMatthew Dillon } 3391b40f8c0SMatthew Dillon 34026f9a767SRodney W. Grimes /* 34198cb733cSKenneth D. Merry * vm_object_allocate_wait 34298cb733cSKenneth D. Merry * 34398cb733cSKenneth D. Merry * Return a new object with the given size, and give the user the 34498cb733cSKenneth D. Merry * option of waiting for it to complete or failing if the needed 34598cb733cSKenneth D. Merry * memory isn't available. 34698cb733cSKenneth D. Merry */ 34798cb733cSKenneth D. Merry vm_object_t 34898cb733cSKenneth D. Merry vm_object_allocate_wait(objtype_t type, vm_pindex_t size, int flags) 34998cb733cSKenneth D. Merry { 35098cb733cSKenneth D. Merry vm_object_t result; 35198cb733cSKenneth D. Merry 35298cb733cSKenneth D. Merry result = (vm_object_t) uma_zalloc(obj_zone, flags); 35398cb733cSKenneth D. Merry 35498cb733cSKenneth D. Merry if (result != NULL) 35598cb733cSKenneth D. Merry _vm_object_allocate(type, size, result); 35698cb733cSKenneth D. Merry 35798cb733cSKenneth D. Merry return (result); 35898cb733cSKenneth D. Merry } 35998cb733cSKenneth D. Merry 36098cb733cSKenneth D. Merry /* 36126f9a767SRodney W. Grimes * vm_object_allocate: 36226f9a767SRodney W. Grimes * 36326f9a767SRodney W. Grimes * Returns a new object with the given size. 36426f9a767SRodney W. Grimes */ 36526f9a767SRodney W. Grimes vm_object_t 3666395da54SIan Dowse vm_object_allocate(objtype_t type, vm_pindex_t size) 36726f9a767SRodney W. Grimes { 36898cb733cSKenneth D. Merry return(vm_object_allocate_wait(type, size, M_WAITOK)); 36926f9a767SRodney W. Grimes } 37026f9a767SRodney W. Grimes 37126f9a767SRodney W. Grimes 37226f9a767SRodney W. Grimes /* 373df8bae1dSRodney W. Grimes * vm_object_reference: 374df8bae1dSRodney W. Grimes * 375df8bae1dSRodney W. Grimes * Gets another reference to the given object. 376df8bae1dSRodney W. Grimes */ 3776476c0d2SJohn Dyson void 3781b40f8c0SMatthew Dillon vm_object_reference(vm_object_t object) 379df8bae1dSRodney W. Grimes { 380df8bae1dSRodney W. Grimes if (object == NULL) 381df8bae1dSRodney W. Grimes return; 38295e5e988SJohn Dyson 38315c176c1SAlan Cox vm_object_lock(object); 384245df27cSMatthew Dillon #if 0 385245df27cSMatthew Dillon /* object can be re-referenced during final cleaning */ 3865526d2d9SEivind Eklund KASSERT(!(object->flags & OBJ_DEAD), 3875526d2d9SEivind Eklund ("vm_object_reference: attempting to reference dead obj")); 388245df27cSMatthew Dillon #endif 38995e5e988SJohn Dyson 390df8bae1dSRodney W. Grimes object->ref_count++; 39147221757SJohn Dyson if (object->type == OBJT_VNODE) { 3923ef3e7c4SJeff Roberson while (vget((struct vnode *) object->handle, LK_RETRY, curthread)) { 39347221757SJohn Dyson printf("vm_object_reference: delay in getting object\n"); 39447221757SJohn Dyson } 39547221757SJohn Dyson } 39615c176c1SAlan Cox vm_object_unlock(object); 39795e5e988SJohn Dyson } 39895e5e988SJohn Dyson 39923955314SAlfred Perlstein /* 40023955314SAlfred Perlstein * handle deallocating a object of type OBJT_VNODE 40123955314SAlfred Perlstein */ 402bf27292bSJohn Dyson void 4031b40f8c0SMatthew Dillon vm_object_vndeallocate(vm_object_t object) 40495e5e988SJohn Dyson { 40595e5e988SJohn Dyson struct vnode *vp = (struct vnode *) object->handle; 406219cbf59SEivind Eklund 4070cddd8f0SMatthew Dillon GIANT_REQUIRED; 4085526d2d9SEivind Eklund KASSERT(object->type == OBJT_VNODE, 4095526d2d9SEivind Eklund ("vm_object_vndeallocate: not a vnode object")); 410219cbf59SEivind Eklund KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp")); 411219cbf59SEivind Eklund #ifdef INVARIANTS 41295e5e988SJohn Dyson if (object->ref_count == 0) { 41395e5e988SJohn Dyson vprint("vm_object_vndeallocate", vp); 41495e5e988SJohn Dyson panic("vm_object_vndeallocate: bad object reference count"); 41595e5e988SJohn Dyson } 41695e5e988SJohn Dyson #endif 41795e5e988SJohn Dyson 41895e5e988SJohn Dyson object->ref_count--; 41947221757SJohn Dyson if (object->ref_count == 0) { 420e6e370a7SJeff Roberson mp_fixme("Unlocked vflag access."); 421e6e370a7SJeff Roberson vp->v_vflag &= ~VV_TEXT; 422c0b6bbb8SAlan Cox #ifdef ENABLE_VFS_IOOPT 423069e9bc1SDoug Rabson vm_object_clear_flag(object, OBJ_OPT); 424c0b6bbb8SAlan Cox #endif 4252be70f79SJohn Dyson } 42623955314SAlfred Perlstein /* 42723955314SAlfred Perlstein * vrele may need a vop lock 42823955314SAlfred Perlstein */ 42947221757SJohn Dyson vrele(vp); 430df8bae1dSRodney W. Grimes } 431df8bae1dSRodney W. Grimes 432df8bae1dSRodney W. Grimes /* 433df8bae1dSRodney W. Grimes * vm_object_deallocate: 434df8bae1dSRodney W. Grimes * 435df8bae1dSRodney W. Grimes * Release a reference to the specified object, 436df8bae1dSRodney W. Grimes * gained either through a vm_object_allocate 437df8bae1dSRodney W. Grimes * or a vm_object_reference call. When all references 438df8bae1dSRodney W. Grimes * are gone, storage associated with this object 439df8bae1dSRodney W. Grimes * may be relinquished. 440df8bae1dSRodney W. Grimes * 441df8bae1dSRodney W. Grimes * No object may be locked. 442df8bae1dSRodney W. Grimes */ 44326f9a767SRodney W. Grimes void 4441b40f8c0SMatthew Dillon vm_object_deallocate(vm_object_t object) 445df8bae1dSRodney W. Grimes { 446df8bae1dSRodney W. Grimes vm_object_t temp; 447df8bae1dSRodney W. Grimes 44847c3ccc4SAlan Cox mtx_lock(&Giant); 449df8bae1dSRodney W. Grimes while (object != NULL) { 450df8bae1dSRodney W. Grimes 45195e5e988SJohn Dyson if (object->type == OBJT_VNODE) { 45295e5e988SJohn Dyson vm_object_vndeallocate(object); 45347c3ccc4SAlan Cox mtx_unlock(&Giant); 45495e5e988SJohn Dyson return; 45595e5e988SJohn Dyson } 45695e5e988SJohn Dyson 4578125b1e6SAlfred Perlstein KASSERT(object->ref_count != 0, 4588125b1e6SAlfred Perlstein ("vm_object_deallocate: object deallocated too many times: %d", object->type)); 4592be70f79SJohn Dyson 4602be70f79SJohn Dyson /* 4618125b1e6SAlfred Perlstein * If the reference count goes to 0 we start calling 4628125b1e6SAlfred Perlstein * vm_object_terminate() on the object chain. 4638125b1e6SAlfred Perlstein * A ref count of 1 may be a special case depending on the 4648125b1e6SAlfred Perlstein * shadow count being 0 or 1. 4652be70f79SJohn Dyson */ 466c0877f10SJohn Dyson object->ref_count--; 4678125b1e6SAlfred Perlstein if (object->ref_count > 1) { 46847c3ccc4SAlan Cox mtx_unlock(&Giant); 469c0877f10SJohn Dyson return; 4708125b1e6SAlfred Perlstein } else if (object->ref_count == 1) { 4718125b1e6SAlfred Perlstein if (object->shadow_count == 0) { 4728125b1e6SAlfred Perlstein vm_object_set_flag(object, OBJ_ONEMAPPING); 4738125b1e6SAlfred Perlstein } else if ((object->shadow_count == 1) && 4748125b1e6SAlfred Perlstein (object->handle == NULL) && 47524a1cce3SDavid Greenman (object->type == OBJT_DEFAULT || 47624a1cce3SDavid Greenman object->type == OBJT_SWAP)) { 477a1f6d91cSDavid Greenman vm_object_t robject; 47895e5e988SJohn Dyson 479b18bfc3dSJohn Dyson robject = TAILQ_FIRST(&object->shadow_head); 4805526d2d9SEivind Eklund KASSERT(robject != NULL, 481219cbf59SEivind Eklund ("vm_object_deallocate: ref_count: %d, shadow_count: %d", 4825526d2d9SEivind Eklund object->ref_count, 4835526d2d9SEivind Eklund object->shadow_count)); 48495e5e988SJohn Dyson if ((robject->handle == NULL) && 48524a1cce3SDavid Greenman (robject->type == OBJT_DEFAULT || 48624a1cce3SDavid Greenman robject->type == OBJT_SWAP)) { 487a1f6d91cSDavid Greenman 48895e5e988SJohn Dyson robject->ref_count++; 48995e5e988SJohn Dyson 4901c7c3c6aSMatthew Dillon while ( 4911c7c3c6aSMatthew Dillon robject->paging_in_progress || 4921c7c3c6aSMatthew Dillon object->paging_in_progress 4931c7c3c6aSMatthew Dillon ) { 49466095752SJohn Dyson vm_object_pip_sleep(robject, "objde1"); 49566095752SJohn Dyson vm_object_pip_sleep(object, "objde2"); 496a1f6d91cSDavid Greenman } 497a1f6d91cSDavid Greenman 49895e5e988SJohn Dyson if (robject->ref_count == 1) { 49995e5e988SJohn Dyson robject->ref_count--; 500ba8da839SDavid Greenman object = robject; 50195e5e988SJohn Dyson goto doterm; 50295e5e988SJohn Dyson } 50395e5e988SJohn Dyson 50495e5e988SJohn Dyson object = robject; 50595e5e988SJohn Dyson vm_object_collapse(object); 506ba8da839SDavid Greenman continue; 507a1f6d91cSDavid Greenman } 50895e5e988SJohn Dyson } 50947c3ccc4SAlan Cox mtx_unlock(&Giant); 510a1f6d91cSDavid Greenman return; 51195e5e988SJohn Dyson } 51295e5e988SJohn Dyson doterm: 51324a1cce3SDavid Greenman temp = object->backing_object; 514de5f6a77SJohn Dyson if (temp) { 51524a1cce3SDavid Greenman TAILQ_REMOVE(&temp->shadow_head, object, shadow_list); 51695e5e988SJohn Dyson temp->shadow_count--; 517c0b6bbb8SAlan Cox #ifdef ENABLE_VFS_IOOPT 5182d8acc0fSJohn Dyson if (temp->ref_count == 0) 519069e9bc1SDoug Rabson vm_object_clear_flag(temp, OBJ_OPT); 520c0b6bbb8SAlan Cox #endif 521eaf13dd7SJohn Dyson temp->generation++; 52295461b45SJohn Dyson object->backing_object = NULL; 523de5f6a77SJohn Dyson } 524245df27cSMatthew Dillon /* 525245df27cSMatthew Dillon * Don't double-terminate, we could be in a termination 526245df27cSMatthew Dillon * recursion due to the terminate having to sync data 527245df27cSMatthew Dillon * to disk. 528245df27cSMatthew Dillon */ 529245df27cSMatthew Dillon if ((object->flags & OBJ_DEAD) == 0) 530df8bae1dSRodney W. Grimes vm_object_terminate(object); 531df8bae1dSRodney W. Grimes object = temp; 532df8bae1dSRodney W. Grimes } 53347c3ccc4SAlan Cox mtx_unlock(&Giant); 534df8bae1dSRodney W. Grimes } 535df8bae1dSRodney W. Grimes 536df8bae1dSRodney W. Grimes /* 537df8bae1dSRodney W. Grimes * vm_object_terminate actually destroys the specified object, freeing 538df8bae1dSRodney W. Grimes * up all previously used resources. 539df8bae1dSRodney W. Grimes * 540df8bae1dSRodney W. Grimes * The object must be locked. 5411c7c3c6aSMatthew Dillon * This routine may block. 542df8bae1dSRodney W. Grimes */ 54395e5e988SJohn Dyson void 5441b40f8c0SMatthew Dillon vm_object_terminate(vm_object_t object) 545df8bae1dSRodney W. Grimes { 546d031cff1SMatthew Dillon vm_page_t p; 547e4b7635dSDavid Greenman int s; 548df8bae1dSRodney W. Grimes 5490cddd8f0SMatthew Dillon GIANT_REQUIRED; 5500cddd8f0SMatthew Dillon 55195e5e988SJohn Dyson /* 55295e5e988SJohn Dyson * Make sure no one uses us. 55395e5e988SJohn Dyson */ 554069e9bc1SDoug Rabson vm_object_set_flag(object, OBJ_DEAD); 5553c631446SJohn Dyson 556df8bae1dSRodney W. Grimes /* 557f6b04d2bSDavid Greenman * wait for the pageout daemon to be done with the object 558df8bae1dSRodney W. Grimes */ 55966095752SJohn Dyson vm_object_pip_wait(object, "objtrm"); 560df8bae1dSRodney W. Grimes 5615526d2d9SEivind Eklund KASSERT(!object->paging_in_progress, 5625526d2d9SEivind Eklund ("vm_object_terminate: pageout in progress")); 56326f9a767SRodney W. Grimes 56426f9a767SRodney W. Grimes /* 5650d94caffSDavid Greenman * Clean and free the pages, as appropriate. All references to the 5660d94caffSDavid Greenman * object are gone, so we don't need to lock it. 56726f9a767SRodney W. Grimes */ 56824a1cce3SDavid Greenman if (object->type == OBJT_VNODE) { 56995e5e988SJohn Dyson struct vnode *vp; 57095e5e988SJohn Dyson 571c0b6bbb8SAlan Cox #ifdef ENABLE_VFS_IOOPT 57295e5e988SJohn Dyson /* 57395e5e988SJohn Dyson * Freeze optimized copies. 57495e5e988SJohn Dyson */ 57595e5e988SJohn Dyson vm_freeze_copyopts(object, 0, object->size); 576c0b6bbb8SAlan Cox #endif 57795e5e988SJohn Dyson /* 57895e5e988SJohn Dyson * Clean pages and flush buffers. 57995e5e988SJohn Dyson */ 5808f9110f6SJohn Dyson vm_object_page_clean(object, 0, 0, OBJPC_SYNC); 58195e5e988SJohn Dyson 58295e5e988SJohn Dyson vp = (struct vnode *) object->handle; 583f6b04d2bSDavid Greenman vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0); 584bef608bdSJohn Dyson } 585bef608bdSJohn Dyson 586971dd342SAlfred Perlstein KASSERT(object->ref_count == 0, 587971dd342SAlfred Perlstein ("vm_object_terminate: object with references, ref_count=%d", 588971dd342SAlfred Perlstein object->ref_count)); 589996c772fSJohn Dyson 5900d94caffSDavid Greenman /* 591356863ebSDavid Greenman * Now free any remaining pages. For internal objects, this also 592356863ebSDavid Greenman * removes them from paging queues. Don't free wired pages, just 593356863ebSDavid Greenman * remove them from the object. 594df8bae1dSRodney W. Grimes */ 595e4b7635dSDavid Greenman s = splvm(); 59656030358SAlan Cox vm_page_lock_queues(); 597b18bfc3dSJohn Dyson while ((p = TAILQ_FIRST(&object->memq)) != NULL) { 598971dd342SAlfred Perlstein KASSERT(!p->busy && (p->flags & PG_BUSY) == 0, 599971dd342SAlfred Perlstein ("vm_object_terminate: freeing busy page %p " 600971dd342SAlfred Perlstein "p->busy = %d, p->flags %x\n", p, p->busy, p->flags)); 6010b10ba98SDavid Greenman if (p->wire_count == 0) { 602e69763a3SDoug Rabson vm_page_busy(p); 603df8bae1dSRodney W. Grimes vm_page_free(p); 604df8bae1dSRodney W. Grimes cnt.v_pfree++; 6050b10ba98SDavid Greenman } else { 606356863ebSDavid Greenman vm_page_busy(p); 6070b10ba98SDavid Greenman vm_page_remove(p); 6080b10ba98SDavid Greenman } 609df8bae1dSRodney W. Grimes } 61056030358SAlan Cox vm_page_unlock_queues(); 611e4b7635dSDavid Greenman splx(s); 612bef608bdSJohn Dyson 6132d8acc0fSJohn Dyson /* 6149fcfb650SDavid Greenman * Let the pager know object is dead. 6159fcfb650SDavid Greenman */ 6169fcfb650SDavid Greenman vm_pager_deallocate(object); 6179fcfb650SDavid Greenman 6189fcfb650SDavid Greenman /* 6190b10ba98SDavid Greenman * Remove the object from the global object list. 6202d8acc0fSJohn Dyson */ 6219ed346baSBosko Milekic mtx_lock(&vm_object_list_mtx); 622df8bae1dSRodney W. Grimes TAILQ_REMOVE(&vm_object_list, object, object_list); 6239ed346baSBosko Milekic mtx_unlock(&vm_object_list_mtx); 6240b10ba98SDavid Greenman 6250b10ba98SDavid Greenman wakeup(object); 6260b10ba98SDavid Greenman 627df8bae1dSRodney W. Grimes /* 628df8bae1dSRodney W. Grimes * Free the space for the object. 629df8bae1dSRodney W. Grimes */ 630670d17b5SJeff Roberson uma_zfree(obj_zone, object); 63147221757SJohn Dyson } 632df8bae1dSRodney W. Grimes 633df8bae1dSRodney W. Grimes /* 634df8bae1dSRodney W. Grimes * vm_object_page_clean 635df8bae1dSRodney W. Grimes * 6364f79d873SMatthew Dillon * Clean all dirty pages in the specified range of object. Leaves page 6374f79d873SMatthew Dillon * on whatever queue it is currently on. If NOSYNC is set then do not 6384f79d873SMatthew Dillon * write out pages with PG_NOSYNC set (originally comes from MAP_NOSYNC), 6394f79d873SMatthew Dillon * leaving the object dirty. 64026f9a767SRodney W. Grimes * 64126f9a767SRodney W. Grimes * Odd semantics: if start == end, we clean everything. 64226f9a767SRodney W. Grimes * 64326f9a767SRodney W. Grimes * The object must be locked. 64426f9a767SRodney W. Grimes */ 645f6b04d2bSDavid Greenman void 6461b40f8c0SMatthew Dillon vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end, int flags) 647f6b04d2bSDavid Greenman { 648b9b7a4beSMatthew Dillon vm_page_t p, np; 6496395da54SIan Dowse vm_pindex_t tstart, tend; 650bd7e5f99SJohn Dyson vm_pindex_t pi; 65124a1cce3SDavid Greenman struct vnode *vp; 6524f79d873SMatthew Dillon int clearobjflags; 6538f9110f6SJohn Dyson int pagerflags; 6542d8acc0fSJohn Dyson int curgeneration; 655f6b04d2bSDavid Greenman 6560cddd8f0SMatthew Dillon GIANT_REQUIRED; 6570cddd8f0SMatthew Dillon 658aef922f5SJohn Dyson if (object->type != OBJT_VNODE || 659aef922f5SJohn Dyson (object->flags & OBJ_MIGHTBEDIRTY) == 0) 660f6b04d2bSDavid Greenman return; 661f6b04d2bSDavid Greenman 6628f9110f6SJohn Dyson pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : 0; 6638f9110f6SJohn Dyson pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0; 6648f9110f6SJohn Dyson 66524a1cce3SDavid Greenman vp = object->handle; 66624a1cce3SDavid Greenman 667069e9bc1SDoug Rabson vm_object_set_flag(object, OBJ_CLEANING); 66824a1cce3SDavid Greenman 669f6b04d2bSDavid Greenman tstart = start; 670f6b04d2bSDavid Greenman if (end == 0) { 671f6b04d2bSDavid Greenman tend = object->size; 672f6b04d2bSDavid Greenman } else { 673f6b04d2bSDavid Greenman tend = end; 674f6b04d2bSDavid Greenman } 675eaf13dd7SJohn Dyson 6764f79d873SMatthew Dillon /* 677b9b7a4beSMatthew Dillon * If the caller is smart and only msync()s a range he knows is 678b9b7a4beSMatthew Dillon * dirty, we may be able to avoid an object scan. This results in 679b9b7a4beSMatthew Dillon * a phenominal improvement in performance. We cannot do this 680b9b7a4beSMatthew Dillon * as a matter of course because the object may be huge - e.g. 681b9b7a4beSMatthew Dillon * the size might be in the gigabytes or terrabytes. 682b9b7a4beSMatthew Dillon */ 683b9b7a4beSMatthew Dillon if (msync_flush_flags & MSYNC_FLUSH_HARDSEQ) { 684300b96acSIan Dowse vm_pindex_t tscan; 685b9b7a4beSMatthew Dillon int scanlimit; 686b9b7a4beSMatthew Dillon int scanreset; 687b9b7a4beSMatthew Dillon 688b9b7a4beSMatthew Dillon scanreset = object->resident_page_count / EASY_SCAN_FACTOR; 689b9b7a4beSMatthew Dillon if (scanreset < 16) 690b9b7a4beSMatthew Dillon scanreset = 16; 691b9b7a4beSMatthew Dillon 692b9b7a4beSMatthew Dillon scanlimit = scanreset; 693b9b7a4beSMatthew Dillon tscan = tstart; 694b9b7a4beSMatthew Dillon while (tscan < tend) { 695b9b7a4beSMatthew Dillon curgeneration = object->generation; 696b9b7a4beSMatthew Dillon p = vm_page_lookup(object, tscan); 697b9b7a4beSMatthew Dillon if (p == NULL || p->valid == 0 || 698b9b7a4beSMatthew Dillon (p->queue - p->pc) == PQ_CACHE) { 699b9b7a4beSMatthew Dillon if (--scanlimit == 0) 700b9b7a4beSMatthew Dillon break; 701b9b7a4beSMatthew Dillon ++tscan; 702b9b7a4beSMatthew Dillon continue; 703b9b7a4beSMatthew Dillon } 704b9b7a4beSMatthew Dillon vm_page_test_dirty(p); 705b9b7a4beSMatthew Dillon if ((p->dirty & p->valid) == 0) { 706b9b7a4beSMatthew Dillon if (--scanlimit == 0) 707b9b7a4beSMatthew Dillon break; 708b9b7a4beSMatthew Dillon ++tscan; 709b9b7a4beSMatthew Dillon continue; 710b9b7a4beSMatthew Dillon } 711b9b7a4beSMatthew Dillon /* 712b9b7a4beSMatthew Dillon * If we have been asked to skip nosync pages and 713b9b7a4beSMatthew Dillon * this is a nosync page, we can't continue. 714b9b7a4beSMatthew Dillon */ 715b9b7a4beSMatthew Dillon if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) { 716b9b7a4beSMatthew Dillon if (--scanlimit == 0) 717b9b7a4beSMatthew Dillon break; 718b9b7a4beSMatthew Dillon ++tscan; 719b9b7a4beSMatthew Dillon continue; 720b9b7a4beSMatthew Dillon } 721b9b7a4beSMatthew Dillon scanlimit = scanreset; 722b9b7a4beSMatthew Dillon 723b9b7a4beSMatthew Dillon /* 724b9b7a4beSMatthew Dillon * This returns 0 if it was unable to busy the first 725b9b7a4beSMatthew Dillon * page (i.e. had to sleep). 726b9b7a4beSMatthew Dillon */ 727b9b7a4beSMatthew Dillon tscan += vm_object_page_collect_flush(object, p, curgeneration, pagerflags); 728b9b7a4beSMatthew Dillon } 729b9b7a4beSMatthew Dillon 730b9b7a4beSMatthew Dillon /* 731b9b7a4beSMatthew Dillon * If everything was dirty and we flushed it successfully, 732b9b7a4beSMatthew Dillon * and the requested range is not the entire object, we 733b9b7a4beSMatthew Dillon * don't have to mess with CLEANCHK or MIGHTBEDIRTY and can 734b9b7a4beSMatthew Dillon * return immediately. 735b9b7a4beSMatthew Dillon */ 736b9b7a4beSMatthew Dillon if (tscan >= tend && (tstart || tend < object->size)) { 737b9b7a4beSMatthew Dillon vm_object_clear_flag(object, OBJ_CLEANING); 738b9b7a4beSMatthew Dillon return; 739b9b7a4beSMatthew Dillon } 740b9b7a4beSMatthew Dillon } 741b9b7a4beSMatthew Dillon 742b9b7a4beSMatthew Dillon /* 7434f79d873SMatthew Dillon * Generally set CLEANCHK interlock and make the page read-only so 7444f79d873SMatthew Dillon * we can then clear the object flags. 7454f79d873SMatthew Dillon * 7464f79d873SMatthew Dillon * However, if this is a nosync mmap then the object is likely to 7474f79d873SMatthew Dillon * stay dirty so do not mess with the page and do not clear the 7484f79d873SMatthew Dillon * object flags. 7494f79d873SMatthew Dillon */ 7504f79d873SMatthew Dillon clearobjflags = 1; 7514f79d873SMatthew Dillon 752fc2ffbe6SPoul-Henning Kamp TAILQ_FOREACH(p, &object->memq, listq) { 753e69763a3SDoug Rabson vm_page_flag_set(p, PG_CLEANCHK); 7544f79d873SMatthew Dillon if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) 7554f79d873SMatthew Dillon clearobjflags = 0; 7564f79d873SMatthew Dillon else 757eaf13dd7SJohn Dyson vm_page_protect(p, VM_PROT_READ); 758eaf13dd7SJohn Dyson } 759eaf13dd7SJohn Dyson 7604f79d873SMatthew Dillon if (clearobjflags && (tstart == 0) && (tend == object->size)) { 761245df27cSMatthew Dillon struct vnode *vp; 762245df27cSMatthew Dillon 763069e9bc1SDoug Rabson vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY); 764245df27cSMatthew Dillon if (object->type == OBJT_VNODE && 765245df27cSMatthew Dillon (vp = (struct vnode *)object->handle) != NULL) { 766e6e370a7SJeff Roberson VI_LOCK(vp); 767e6e370a7SJeff Roberson if (vp->v_iflag & VI_OBJDIRTY) 768e6e370a7SJeff Roberson vp->v_iflag &= ~VI_OBJDIRTY; 769e6e370a7SJeff Roberson VI_UNLOCK(vp); 770245df27cSMatthew Dillon } 771ec4f9fb0SDavid Greenman } 772f6b04d2bSDavid Greenman 773bd7e5f99SJohn Dyson rescan: 7742d8acc0fSJohn Dyson curgeneration = object->generation; 7752d8acc0fSJohn Dyson 776b18bfc3dSJohn Dyson for (p = TAILQ_FIRST(&object->memq); p; p = np) { 777b9b7a4beSMatthew Dillon int n; 778b9b7a4beSMatthew Dillon 779b18bfc3dSJohn Dyson np = TAILQ_NEXT(p, listq); 780bd7e5f99SJohn Dyson 781b9b7a4beSMatthew Dillon again: 782bd7e5f99SJohn Dyson pi = p->pindex; 783bd7e5f99SJohn Dyson if (((p->flags & PG_CLEANCHK) == 0) || 784bd7e5f99SJohn Dyson (pi < tstart) || (pi >= tend) || 7855070c7f8SJohn Dyson (p->valid == 0) || 7865070c7f8SJohn Dyson ((p->queue - p->pc) == PQ_CACHE)) { 787e69763a3SDoug Rabson vm_page_flag_clear(p, PG_CLEANCHK); 788aef922f5SJohn Dyson continue; 789f6b04d2bSDavid Greenman } 790f6b04d2bSDavid Greenman 791bd7e5f99SJohn Dyson vm_page_test_dirty(p); 792bd7e5f99SJohn Dyson if ((p->dirty & p->valid) == 0) { 793e69763a3SDoug Rabson vm_page_flag_clear(p, PG_CLEANCHK); 794bd7e5f99SJohn Dyson continue; 795bd7e5f99SJohn Dyson } 796ec4f9fb0SDavid Greenman 7974f79d873SMatthew Dillon /* 7984f79d873SMatthew Dillon * If we have been asked to skip nosync pages and this is a 7994f79d873SMatthew Dillon * nosync page, skip it. Note that the object flags were 8004f79d873SMatthew Dillon * not cleared in this case so we do not have to set them. 8014f79d873SMatthew Dillon */ 8024f79d873SMatthew Dillon if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) { 8034f79d873SMatthew Dillon vm_page_flag_clear(p, PG_CLEANCHK); 8044f79d873SMatthew Dillon continue; 8054f79d873SMatthew Dillon } 8064f79d873SMatthew Dillon 807b9b7a4beSMatthew Dillon n = vm_object_page_collect_flush(object, p, 808b9b7a4beSMatthew Dillon curgeneration, pagerflags); 809b9b7a4beSMatthew Dillon if (n == 0) 810b9b7a4beSMatthew Dillon goto rescan; 811b9b7a4beSMatthew Dillon 812b9b7a4beSMatthew Dillon if (object->generation != curgeneration) 813b9b7a4beSMatthew Dillon goto rescan; 814b9b7a4beSMatthew Dillon 815b9b7a4beSMatthew Dillon /* 816b9b7a4beSMatthew Dillon * Try to optimize the next page. If we can't we pick up 817b9b7a4beSMatthew Dillon * our (random) scan where we left off. 818b9b7a4beSMatthew Dillon */ 819b9b7a4beSMatthew Dillon if (msync_flush_flags & MSYNC_FLUSH_SOFTSEQ) { 820b9b7a4beSMatthew Dillon if ((p = vm_page_lookup(object, pi + n)) != NULL) 821b9b7a4beSMatthew Dillon goto again; 822b9b7a4beSMatthew Dillon } 823b9b7a4beSMatthew Dillon } 824b9b7a4beSMatthew Dillon 825b9b7a4beSMatthew Dillon #if 0 826b9b7a4beSMatthew Dillon VOP_FSYNC(vp, NULL, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc); 827b9b7a4beSMatthew Dillon #endif 828b9b7a4beSMatthew Dillon 829b9b7a4beSMatthew Dillon vm_object_clear_flag(object, OBJ_CLEANING); 830b9b7a4beSMatthew Dillon return; 831b9b7a4beSMatthew Dillon } 832b9b7a4beSMatthew Dillon 833b9b7a4beSMatthew Dillon static int 834b9b7a4beSMatthew Dillon vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags) 835b9b7a4beSMatthew Dillon { 836b9b7a4beSMatthew Dillon int runlen; 837b9b7a4beSMatthew Dillon int s; 838b9b7a4beSMatthew Dillon int maxf; 839b9b7a4beSMatthew Dillon int chkb; 840b9b7a4beSMatthew Dillon int maxb; 841b9b7a4beSMatthew Dillon int i; 842b9b7a4beSMatthew Dillon vm_pindex_t pi; 843b9b7a4beSMatthew Dillon vm_page_t maf[vm_pageout_page_count]; 844b9b7a4beSMatthew Dillon vm_page_t mab[vm_pageout_page_count]; 845b9b7a4beSMatthew Dillon vm_page_t ma[vm_pageout_page_count]; 846b9b7a4beSMatthew Dillon 847b18bfc3dSJohn Dyson s = splvm(); 848b9b7a4beSMatthew Dillon pi = p->pindex; 8491c7c3c6aSMatthew Dillon while (vm_page_sleep_busy(p, TRUE, "vpcwai")) { 8502d8acc0fSJohn Dyson if (object->generation != curgeneration) { 851f6b04d2bSDavid Greenman splx(s); 852b9b7a4beSMatthew Dillon return(0); 853f6b04d2bSDavid Greenman } 8542d8acc0fSJohn Dyson } 85555df3298SAlan Cox vm_page_lock_queues(); 856bd7e5f99SJohn Dyson maxf = 0; 857bd7e5f99SJohn Dyson for(i = 1; i < vm_pageout_page_count; i++) { 858b9b7a4beSMatthew Dillon vm_page_t tp; 859b9b7a4beSMatthew Dillon 8608aef1712SMatthew Dillon if ((tp = vm_page_lookup(object, pi + i)) != NULL) { 861bd7e5f99SJohn Dyson if ((tp->flags & PG_BUSY) || 862ffc82b0aSJohn Dyson (tp->flags & PG_CLEANCHK) == 0 || 863ffc82b0aSJohn Dyson (tp->busy != 0)) 864bd7e5f99SJohn Dyson break; 8655070c7f8SJohn Dyson if((tp->queue - tp->pc) == PQ_CACHE) { 866e69763a3SDoug Rabson vm_page_flag_clear(tp, PG_CLEANCHK); 8673077a9c2SJohn Dyson break; 8683077a9c2SJohn Dyson } 869bd7e5f99SJohn Dyson vm_page_test_dirty(tp); 870bd7e5f99SJohn Dyson if ((tp->dirty & tp->valid) == 0) { 871e69763a3SDoug Rabson vm_page_flag_clear(tp, PG_CLEANCHK); 872bd7e5f99SJohn Dyson break; 873bd7e5f99SJohn Dyson } 874bd7e5f99SJohn Dyson maf[ i - 1 ] = tp; 875bd7e5f99SJohn Dyson maxf++; 876bd7e5f99SJohn Dyson continue; 877bd7e5f99SJohn Dyson } 878bd7e5f99SJohn Dyson break; 879bd7e5f99SJohn Dyson } 880aef922f5SJohn Dyson 881bd7e5f99SJohn Dyson maxb = 0; 882bd7e5f99SJohn Dyson chkb = vm_pageout_page_count - maxf; 883bd7e5f99SJohn Dyson if (chkb) { 884bd7e5f99SJohn Dyson for(i = 1; i < chkb;i++) { 885b9b7a4beSMatthew Dillon vm_page_t tp; 886b9b7a4beSMatthew Dillon 8878aef1712SMatthew Dillon if ((tp = vm_page_lookup(object, pi - i)) != NULL) { 888bd7e5f99SJohn Dyson if ((tp->flags & PG_BUSY) || 889ffc82b0aSJohn Dyson (tp->flags & PG_CLEANCHK) == 0 || 890ffc82b0aSJohn Dyson (tp->busy != 0)) 891bd7e5f99SJohn Dyson break; 8925070c7f8SJohn Dyson if ((tp->queue - tp->pc) == PQ_CACHE) { 893e69763a3SDoug Rabson vm_page_flag_clear(tp, PG_CLEANCHK); 8943077a9c2SJohn Dyson break; 8953077a9c2SJohn Dyson } 896bd7e5f99SJohn Dyson vm_page_test_dirty(tp); 897bd7e5f99SJohn Dyson if ((tp->dirty & tp->valid) == 0) { 898e69763a3SDoug Rabson vm_page_flag_clear(tp, PG_CLEANCHK); 899bd7e5f99SJohn Dyson break; 900bd7e5f99SJohn Dyson } 901bd7e5f99SJohn Dyson mab[ i - 1 ] = tp; 902bd7e5f99SJohn Dyson maxb++; 903bd7e5f99SJohn Dyson continue; 904bd7e5f99SJohn Dyson } 905bd7e5f99SJohn Dyson break; 906bd7e5f99SJohn Dyson } 907bd7e5f99SJohn Dyson } 908bd7e5f99SJohn Dyson 909bd7e5f99SJohn Dyson for(i = 0; i < maxb; i++) { 910bd7e5f99SJohn Dyson int index = (maxb - i) - 1; 911bd7e5f99SJohn Dyson ma[index] = mab[i]; 912e69763a3SDoug Rabson vm_page_flag_clear(ma[index], PG_CLEANCHK); 913bd7e5f99SJohn Dyson } 914e69763a3SDoug Rabson vm_page_flag_clear(p, PG_CLEANCHK); 915bd7e5f99SJohn Dyson ma[maxb] = p; 916bd7e5f99SJohn Dyson for(i = 0; i < maxf; i++) { 917bd7e5f99SJohn Dyson int index = (maxb + i) + 1; 918bd7e5f99SJohn Dyson ma[index] = maf[i]; 919e69763a3SDoug Rabson vm_page_flag_clear(ma[index], PG_CLEANCHK); 920f6b04d2bSDavid Greenman } 921bd7e5f99SJohn Dyson runlen = maxb + maxf + 1; 922cf2819ccSJohn Dyson 923f35329acSJohn Dyson splx(s); 9248f9110f6SJohn Dyson vm_pageout_flush(ma, runlen, pagerflags); 925cf2819ccSJohn Dyson for (i = 0; i < runlen; i++) { 926cf2819ccSJohn Dyson if (ma[i]->valid & ma[i]->dirty) { 927cf2819ccSJohn Dyson vm_page_protect(ma[i], VM_PROT_READ); 928e69763a3SDoug Rabson vm_page_flag_set(ma[i], PG_CLEANCHK); 929aef922f5SJohn Dyson 930b9b7a4beSMatthew Dillon /* 931b9b7a4beSMatthew Dillon * maxf will end up being the actual number of pages 932b9b7a4beSMatthew Dillon * we wrote out contiguously, non-inclusive of the 933b9b7a4beSMatthew Dillon * first page. We do not count look-behind pages. 934b9b7a4beSMatthew Dillon */ 935b9b7a4beSMatthew Dillon if (i >= maxb + 1 && (maxf > i - maxb - 1)) 936b9b7a4beSMatthew Dillon maxf = i - maxb - 1; 937b9b7a4beSMatthew Dillon } 938b9b7a4beSMatthew Dillon } 93955df3298SAlan Cox vm_page_unlock_queues(); 940b9b7a4beSMatthew Dillon return(maxf + 1); 94126f9a767SRodney W. Grimes } 942df8bae1dSRodney W. Grimes 94372353893SAlan Cox #ifdef ENABLE_VFS_IOOPT 944df8bae1dSRodney W. Grimes /* 9458e3ad7c9SMatthew Dillon * Same as vm_object_pmap_copy, except range checking really 9461efb74fbSJohn Dyson * works, and is meant for small sections of an object. 9478e3ad7c9SMatthew Dillon * 9488e3ad7c9SMatthew Dillon * This code protects resident pages by making them read-only 9498e3ad7c9SMatthew Dillon * and is typically called on a fork or split when a page 9508e3ad7c9SMatthew Dillon * is converted to copy-on-write. 9518e3ad7c9SMatthew Dillon * 9528e3ad7c9SMatthew Dillon * NOTE: If the page is already at VM_PROT_NONE, calling 9538e3ad7c9SMatthew Dillon * vm_page_protect will have no effect. 9541efb74fbSJohn Dyson */ 9551efb74fbSJohn Dyson void 9561b40f8c0SMatthew Dillon vm_object_pmap_copy_1(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 9571efb74fbSJohn Dyson { 9581efb74fbSJohn Dyson vm_pindex_t idx; 959d031cff1SMatthew Dillon vm_page_t p; 9601efb74fbSJohn Dyson 9610cddd8f0SMatthew Dillon GIANT_REQUIRED; 9620cddd8f0SMatthew Dillon 9631efb74fbSJohn Dyson if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0) 9641efb74fbSJohn Dyson return; 9651efb74fbSJohn Dyson 9661efb74fbSJohn Dyson for (idx = start; idx < end; idx++) { 9671efb74fbSJohn Dyson p = vm_page_lookup(object, idx); 9681efb74fbSJohn Dyson if (p == NULL) 9691efb74fbSJohn Dyson continue; 9701efb74fbSJohn Dyson vm_page_protect(p, VM_PROT_READ); 9711efb74fbSJohn Dyson } 9721efb74fbSJohn Dyson } 97372353893SAlan Cox #endif 9741efb74fbSJohn Dyson 9751efb74fbSJohn Dyson /* 976df8bae1dSRodney W. Grimes * vm_object_pmap_remove: 977df8bae1dSRodney W. Grimes * 978df8bae1dSRodney W. Grimes * Removes all physical pages in the specified 979df8bae1dSRodney W. Grimes * object range from all physical maps. 980df8bae1dSRodney W. Grimes * 981df8bae1dSRodney W. Grimes * The object must *not* be locked. 982df8bae1dSRodney W. Grimes */ 98326f9a767SRodney W. Grimes void 9841b40f8c0SMatthew Dillon vm_object_pmap_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 985df8bae1dSRodney W. Grimes { 986d031cff1SMatthew Dillon vm_page_t p; 987d031cff1SMatthew Dillon 9880cddd8f0SMatthew Dillon GIANT_REQUIRED; 989df8bae1dSRodney W. Grimes if (object == NULL) 990df8bae1dSRodney W. Grimes return; 991cc64b484SAlfred Perlstein TAILQ_FOREACH(p, &object->memq, listq) { 992bd7e5f99SJohn Dyson if (p->pindex >= start && p->pindex < end) 993f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 99426f9a767SRodney W. Grimes } 9956e20a165SJohn Dyson if ((start == 0) && (object->size == end)) 996069e9bc1SDoug Rabson vm_object_clear_flag(object, OBJ_WRITEABLE); 99726f9a767SRodney W. Grimes } 998df8bae1dSRodney W. Grimes 999df8bae1dSRodney W. Grimes /* 1000867a482dSJohn Dyson * vm_object_madvise: 1001867a482dSJohn Dyson * 1002867a482dSJohn Dyson * Implements the madvise function at the object/page level. 10031c7c3c6aSMatthew Dillon * 1004193b9358SAlan Cox * MADV_WILLNEED (any object) 1005193b9358SAlan Cox * 1006193b9358SAlan Cox * Activate the specified pages if they are resident. 1007193b9358SAlan Cox * 1008193b9358SAlan Cox * MADV_DONTNEED (any object) 1009193b9358SAlan Cox * 1010193b9358SAlan Cox * Deactivate the specified pages if they are resident. 1011193b9358SAlan Cox * 1012193b9358SAlan Cox * MADV_FREE (OBJT_DEFAULT/OBJT_SWAP objects, 1013193b9358SAlan Cox * OBJ_ONEMAPPING only) 1014193b9358SAlan Cox * 1015193b9358SAlan Cox * Deactivate and clean the specified pages if they are 1016193b9358SAlan Cox * resident. This permits the process to reuse the pages 1017193b9358SAlan Cox * without faulting or the kernel to reclaim the pages 1018193b9358SAlan Cox * without I/O. 1019867a482dSJohn Dyson */ 1020867a482dSJohn Dyson void 10211b40f8c0SMatthew Dillon vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise) 1022867a482dSJohn Dyson { 10236e20a165SJohn Dyson vm_pindex_t end, tpindex; 10246e20a165SJohn Dyson vm_object_t tobject; 1025867a482dSJohn Dyson vm_page_t m; 1026867a482dSJohn Dyson 1027867a482dSJohn Dyson if (object == NULL) 1028867a482dSJohn Dyson return; 1029867a482dSJohn Dyson 103015c176c1SAlan Cox vm_object_lock(object); 1031094f6d26SAlan Cox 1032867a482dSJohn Dyson end = pindex + count; 1033867a482dSJohn Dyson 10341c7c3c6aSMatthew Dillon /* 10351c7c3c6aSMatthew Dillon * Locate and adjust resident pages 10361c7c3c6aSMatthew Dillon */ 10371c7c3c6aSMatthew Dillon for (; pindex < end; pindex += 1) { 10386e20a165SJohn Dyson relookup: 10396e20a165SJohn Dyson tobject = object; 10406e20a165SJohn Dyson tpindex = pindex; 10416e20a165SJohn Dyson shadowlookup: 104258b4e6ccSAlan Cox /* 104358b4e6ccSAlan Cox * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages 104458b4e6ccSAlan Cox * and those pages must be OBJ_ONEMAPPING. 104558b4e6ccSAlan Cox */ 104658b4e6ccSAlan Cox if (advise == MADV_FREE) { 104758b4e6ccSAlan Cox if ((tobject->type != OBJT_DEFAULT && 104858b4e6ccSAlan Cox tobject->type != OBJT_SWAP) || 104958b4e6ccSAlan Cox (tobject->flags & OBJ_ONEMAPPING) == 0) { 10506e20a165SJohn Dyson continue; 10516e20a165SJohn Dyson } 105258b4e6ccSAlan Cox } 10531c7c3c6aSMatthew Dillon 10541c7c3c6aSMatthew Dillon m = vm_page_lookup(tobject, tpindex); 10551c7c3c6aSMatthew Dillon 10561c7c3c6aSMatthew Dillon if (m == NULL) { 10571ce137beSMatthew Dillon /* 10581ce137beSMatthew Dillon * There may be swap even if there is no backing page 10591ce137beSMatthew Dillon */ 10601ce137beSMatthew Dillon if (advise == MADV_FREE && tobject->type == OBJT_SWAP) 10611ce137beSMatthew Dillon swap_pager_freespace(tobject, tpindex, 1); 10621ce137beSMatthew Dillon 10631ce137beSMatthew Dillon /* 10641ce137beSMatthew Dillon * next object 10651ce137beSMatthew Dillon */ 10666e20a165SJohn Dyson tobject = tobject->backing_object; 10671c7c3c6aSMatthew Dillon if (tobject == NULL) 10681c7c3c6aSMatthew Dillon continue; 10696e20a165SJohn Dyson tpindex += OFF_TO_IDX(tobject->backing_object_offset); 10706e20a165SJohn Dyson goto shadowlookup; 10716e20a165SJohn Dyson } 1072867a482dSJohn Dyson 1073867a482dSJohn Dyson /* 1074867a482dSJohn Dyson * If the page is busy or not in a normal active state, 10758b03c8edSMatthew Dillon * we skip it. If the page is not managed there are no 10768b03c8edSMatthew Dillon * page queues to mess with. Things can break if we mess 10778b03c8edSMatthew Dillon * with pages in any of the below states. 1078867a482dSJohn Dyson */ 107932585dd6SAlan Cox vm_page_lock_queues(); 108032585dd6SAlan Cox if (m->hold_count || 10811c7c3c6aSMatthew Dillon m->wire_count || 10828b03c8edSMatthew Dillon (m->flags & PG_UNMANAGED) || 108332585dd6SAlan Cox m->valid != VM_PAGE_BITS_ALL) { 108432585dd6SAlan Cox vm_page_unlock_queues(); 1085867a482dSJohn Dyson continue; 10866e20a165SJohn Dyson } 108732585dd6SAlan Cox if (vm_page_sleep_if_busy(m, TRUE, "madvpo")) 10886e20a165SJohn Dyson goto relookup; 1089867a482dSJohn Dyson if (advise == MADV_WILLNEED) { 1090867a482dSJohn Dyson vm_page_activate(m); 10916e20a165SJohn Dyson } else if (advise == MADV_DONTNEED) { 1092479112dfSMatthew Dillon vm_page_dontneed(m); 10930a47b48bSJohn Dyson } else if (advise == MADV_FREE) { 10941c7c3c6aSMatthew Dillon /* 10952aaeadf8SMatthew Dillon * Mark the page clean. This will allow the page 10962aaeadf8SMatthew Dillon * to be freed up by the system. However, such pages 10972aaeadf8SMatthew Dillon * are often reused quickly by malloc()/free() 10982aaeadf8SMatthew Dillon * so we do not do anything that would cause 10992aaeadf8SMatthew Dillon * a page fault if we can help it. 11002aaeadf8SMatthew Dillon * 11012aaeadf8SMatthew Dillon * Specifically, we do not try to actually free 11022aaeadf8SMatthew Dillon * the page now nor do we try to put it in the 11032aaeadf8SMatthew Dillon * cache (which would cause a page fault on reuse). 110441c67e12SMatthew Dillon * 110541c67e12SMatthew Dillon * But we do make the page is freeable as we 110641c67e12SMatthew Dillon * can without actually taking the step of unmapping 110741c67e12SMatthew Dillon * it. 11081c7c3c6aSMatthew Dillon */ 11090385347cSPeter Wemm pmap_clear_modify(m); 11106e20a165SJohn Dyson m->dirty = 0; 111141c67e12SMatthew Dillon m->act_count = 0; 1112479112dfSMatthew Dillon vm_page_dontneed(m); 1113867a482dSJohn Dyson } 11142999e9faSAlan Cox vm_page_unlock_queues(); 11152999e9faSAlan Cox if (advise == MADV_FREE && tobject->type == OBJT_SWAP) 11162999e9faSAlan Cox swap_pager_freespace(tobject, tpindex, 1); 1117867a482dSJohn Dyson } 111815c176c1SAlan Cox vm_object_unlock(object); 1119867a482dSJohn Dyson } 1120867a482dSJohn Dyson 1121867a482dSJohn Dyson /* 1122df8bae1dSRodney W. Grimes * vm_object_shadow: 1123df8bae1dSRodney W. Grimes * 1124df8bae1dSRodney W. Grimes * Create a new object which is backed by the 1125df8bae1dSRodney W. Grimes * specified existing object range. The source 1126df8bae1dSRodney W. Grimes * object reference is deallocated. 1127df8bae1dSRodney W. Grimes * 1128df8bae1dSRodney W. Grimes * The new object and offset into that object 1129df8bae1dSRodney W. Grimes * are returned in the source parameters. 1130df8bae1dSRodney W. Grimes */ 113126f9a767SRodney W. Grimes void 11321b40f8c0SMatthew Dillon vm_object_shadow( 11331b40f8c0SMatthew Dillon vm_object_t *object, /* IN/OUT */ 11341b40f8c0SMatthew Dillon vm_ooffset_t *offset, /* IN/OUT */ 11351b40f8c0SMatthew Dillon vm_size_t length) 1136df8bae1dSRodney W. Grimes { 1137d031cff1SMatthew Dillon vm_object_t source; 1138d031cff1SMatthew Dillon vm_object_t result; 1139df8bae1dSRodney W. Grimes 1140df8bae1dSRodney W. Grimes source = *object; 1141df8bae1dSRodney W. Grimes 114215c176c1SAlan Cox vm_object_lock(source); 1143df8bae1dSRodney W. Grimes /* 11449a2f6362SAlan Cox * Don't create the new object if the old object isn't shared. 11459a2f6362SAlan Cox */ 1146c7997d57SAlan Cox if (source != NULL && 1147c7997d57SAlan Cox source->ref_count == 1 && 11489a2f6362SAlan Cox source->handle == NULL && 11499a2f6362SAlan Cox (source->type == OBJT_DEFAULT || 11509917e010SAlan Cox source->type == OBJT_SWAP)) { 115115c176c1SAlan Cox vm_object_unlock(source); 11529a2f6362SAlan Cox return; 11539917e010SAlan Cox } 11549a2f6362SAlan Cox 11559a2f6362SAlan Cox /* 1156df8bae1dSRodney W. Grimes * Allocate a new object with the given length 1157df8bae1dSRodney W. Grimes */ 1158971dd342SAlfred Perlstein result = vm_object_allocate(OBJT_DEFAULT, length); 1159971dd342SAlfred Perlstein KASSERT(result != NULL, ("vm_object_shadow: no object for shadowing")); 1160df8bae1dSRodney W. Grimes 1161df8bae1dSRodney W. Grimes /* 11620d94caffSDavid Greenman * The new object shadows the source object, adding a reference to it. 11630d94caffSDavid Greenman * Our caller changes his reference to point to the new object, 11640d94caffSDavid Greenman * removing a reference to the source object. Net result: no change 11650d94caffSDavid Greenman * of reference count. 11669b09fe24SMatthew Dillon * 11679b09fe24SMatthew Dillon * Try to optimize the result object's page color when shadowing 1168956f3135SPhilippe Charnier * in order to maintain page coloring consistency in the combined 11699b09fe24SMatthew Dillon * shadowed object. 1170df8bae1dSRodney W. Grimes */ 117124a1cce3SDavid Greenman result->backing_object = source; 1172de5f6a77SJohn Dyson if (source) { 1173de5f6a77SJohn Dyson TAILQ_INSERT_TAIL(&source->shadow_head, result, shadow_list); 1174eaf13dd7SJohn Dyson source->shadow_count++; 1175eaf13dd7SJohn Dyson source->generation++; 1176dcc5840eSAlan Cox if (length < source->size) 1177dcc5840eSAlan Cox length = source->size; 1178dcc5840eSAlan Cox if (length > PQ_L2_SIZE / 3 + PQ_PRIME1 || 1179dcc5840eSAlan Cox source->generation > 1) 1180dcc5840eSAlan Cox length = PQ_L2_SIZE / 3 + PQ_PRIME1; 1181dcc5840eSAlan Cox result->pg_color = (source->pg_color + 1182dcc5840eSAlan Cox length * source->generation) & PQ_L2_MASK; 1183dcc5840eSAlan Cox next_index = (result->pg_color + PQ_L2_SIZE / 3 + PQ_PRIME1) & 1184dcc5840eSAlan Cox PQ_L2_MASK; 1185de5f6a77SJohn Dyson } 1186df8bae1dSRodney W. Grimes 1187df8bae1dSRodney W. Grimes /* 11880d94caffSDavid Greenman * Store the offset into the source object, and fix up the offset into 11890d94caffSDavid Greenman * the new object. 1190df8bae1dSRodney W. Grimes */ 119124a1cce3SDavid Greenman result->backing_object_offset = *offset; 1192df8bae1dSRodney W. Grimes 1193df8bae1dSRodney W. Grimes /* 1194df8bae1dSRodney W. Grimes * Return the new things 1195df8bae1dSRodney W. Grimes */ 1196df8bae1dSRodney W. Grimes *offset = 0; 1197df8bae1dSRodney W. Grimes *object = result; 11989917e010SAlan Cox 119915c176c1SAlan Cox vm_object_unlock(source); 1200df8bae1dSRodney W. Grimes } 1201df8bae1dSRodney W. Grimes 1202c5aaa06dSAlan Cox /* 1203c5aaa06dSAlan Cox * vm_object_split: 1204c5aaa06dSAlan Cox * 1205c5aaa06dSAlan Cox * Split the pages in a map entry into a new object. This affords 1206c5aaa06dSAlan Cox * easier removal of unused pages, and keeps object inheritance from 1207c5aaa06dSAlan Cox * being a negative impact on memory usage. 1208c5aaa06dSAlan Cox */ 1209c5aaa06dSAlan Cox void 1210c5aaa06dSAlan Cox vm_object_split(vm_map_entry_t entry) 1211c5aaa06dSAlan Cox { 1212c5aaa06dSAlan Cox vm_page_t m; 1213c5aaa06dSAlan Cox vm_object_t orig_object, new_object, source; 1214c5aaa06dSAlan Cox vm_offset_t s, e; 121523f09d50SIan Dowse vm_pindex_t offidxstart, offidxend; 121623f09d50SIan Dowse vm_size_t idx, size; 1217c5aaa06dSAlan Cox vm_ooffset_t offset; 1218c5aaa06dSAlan Cox 1219c5aaa06dSAlan Cox GIANT_REQUIRED; 1220c5aaa06dSAlan Cox 1221c5aaa06dSAlan Cox orig_object = entry->object.vm_object; 1222c5aaa06dSAlan Cox if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP) 1223c5aaa06dSAlan Cox return; 1224c5aaa06dSAlan Cox if (orig_object->ref_count <= 1) 1225c5aaa06dSAlan Cox return; 1226c5aaa06dSAlan Cox 1227c5aaa06dSAlan Cox offset = entry->offset; 1228c5aaa06dSAlan Cox s = entry->start; 1229c5aaa06dSAlan Cox e = entry->end; 1230c5aaa06dSAlan Cox 1231c5aaa06dSAlan Cox offidxstart = OFF_TO_IDX(offset); 1232c5aaa06dSAlan Cox offidxend = offidxstart + OFF_TO_IDX(e - s); 1233c5aaa06dSAlan Cox size = offidxend - offidxstart; 1234c5aaa06dSAlan Cox 1235c5aaa06dSAlan Cox new_object = vm_pager_allocate(orig_object->type, 1236c5aaa06dSAlan Cox NULL, IDX_TO_OFF(size), VM_PROT_ALL, 0LL); 1237c5aaa06dSAlan Cox if (new_object == NULL) 1238c5aaa06dSAlan Cox return; 1239c5aaa06dSAlan Cox 1240c5aaa06dSAlan Cox source = orig_object->backing_object; 1241c5aaa06dSAlan Cox if (source != NULL) { 1242c5aaa06dSAlan Cox vm_object_reference(source); /* Referenced by new_object */ 1243c5aaa06dSAlan Cox TAILQ_INSERT_TAIL(&source->shadow_head, 1244c5aaa06dSAlan Cox new_object, shadow_list); 1245c5aaa06dSAlan Cox vm_object_clear_flag(source, OBJ_ONEMAPPING); 1246c5aaa06dSAlan Cox new_object->backing_object_offset = 124756030358SAlan Cox orig_object->backing_object_offset + offset; 1248c5aaa06dSAlan Cox new_object->backing_object = source; 1249c5aaa06dSAlan Cox source->shadow_count++; 1250c5aaa06dSAlan Cox source->generation++; 1251c5aaa06dSAlan Cox } 1252c5aaa06dSAlan Cox for (idx = 0; idx < size; idx++) { 1253c5aaa06dSAlan Cox retry: 1254c5aaa06dSAlan Cox m = vm_page_lookup(orig_object, offidxstart + idx); 1255c5aaa06dSAlan Cox if (m == NULL) 1256c5aaa06dSAlan Cox continue; 1257c5aaa06dSAlan Cox 1258c5aaa06dSAlan Cox /* 1259c5aaa06dSAlan Cox * We must wait for pending I/O to complete before we can 1260c5aaa06dSAlan Cox * rename the page. 1261c5aaa06dSAlan Cox * 1262c5aaa06dSAlan Cox * We do not have to VM_PROT_NONE the page as mappings should 1263c5aaa06dSAlan Cox * not be changed by this operation. 1264c5aaa06dSAlan Cox */ 126500f9e8b4SAlan Cox vm_page_lock_queues(); 126600f9e8b4SAlan Cox if (vm_page_sleep_if_busy(m, TRUE, "spltwt")) 1267c5aaa06dSAlan Cox goto retry; 1268c5aaa06dSAlan Cox 1269c5aaa06dSAlan Cox vm_page_busy(m); 127000f9e8b4SAlan Cox vm_page_unlock_queues(); 1271c5aaa06dSAlan Cox vm_page_rename(m, new_object, idx); 1272c5aaa06dSAlan Cox /* page automatically made dirty by rename and cache handled */ 1273c5aaa06dSAlan Cox vm_page_busy(m); 1274c5aaa06dSAlan Cox } 1275c5aaa06dSAlan Cox if (orig_object->type == OBJT_SWAP) { 1276c5aaa06dSAlan Cox vm_object_pip_add(orig_object, 1); 1277c5aaa06dSAlan Cox /* 1278c5aaa06dSAlan Cox * copy orig_object pages into new_object 1279c5aaa06dSAlan Cox * and destroy unneeded pages in 1280c5aaa06dSAlan Cox * shadow object. 1281c5aaa06dSAlan Cox */ 1282c5aaa06dSAlan Cox swap_pager_copy(orig_object, new_object, offidxstart, 0); 1283c5aaa06dSAlan Cox vm_object_pip_wakeup(orig_object); 1284c5aaa06dSAlan Cox } 1285c7118ed6SAlan Cox TAILQ_FOREACH(m, &new_object->memq, listq) 1286c5aaa06dSAlan Cox vm_page_wakeup(m); 1287c5aaa06dSAlan Cox entry->object.vm_object = new_object; 1288c5aaa06dSAlan Cox entry->offset = 0LL; 1289c5aaa06dSAlan Cox vm_object_deallocate(orig_object); 1290c5aaa06dSAlan Cox } 1291c5aaa06dSAlan Cox 12922ad1a3f7SMatthew Dillon #define OBSC_TEST_ALL_SHADOWED 0x0001 12932ad1a3f7SMatthew Dillon #define OBSC_COLLAPSE_NOWAIT 0x0002 12942ad1a3f7SMatthew Dillon #define OBSC_COLLAPSE_WAIT 0x0004 12952ad1a3f7SMatthew Dillon 12962ad1a3f7SMatthew Dillon static __inline int 12972ad1a3f7SMatthew Dillon vm_object_backing_scan(vm_object_t object, int op) 12982ad1a3f7SMatthew Dillon { 12992ad1a3f7SMatthew Dillon int s; 13002ad1a3f7SMatthew Dillon int r = 1; 13012ad1a3f7SMatthew Dillon vm_page_t p; 13022ad1a3f7SMatthew Dillon vm_object_t backing_object; 13032ad1a3f7SMatthew Dillon vm_pindex_t backing_offset_index; 13042ad1a3f7SMatthew Dillon 13052ad1a3f7SMatthew Dillon s = splvm(); 13060cddd8f0SMatthew Dillon GIANT_REQUIRED; 13072ad1a3f7SMatthew Dillon 13082ad1a3f7SMatthew Dillon backing_object = object->backing_object; 13092ad1a3f7SMatthew Dillon backing_offset_index = OFF_TO_IDX(object->backing_object_offset); 13102ad1a3f7SMatthew Dillon 13112ad1a3f7SMatthew Dillon /* 13122ad1a3f7SMatthew Dillon * Initial conditions 13132ad1a3f7SMatthew Dillon */ 13142ad1a3f7SMatthew Dillon if (op & OBSC_TEST_ALL_SHADOWED) { 13152ad1a3f7SMatthew Dillon /* 1316956f3135SPhilippe Charnier * We do not want to have to test for the existence of 13172ad1a3f7SMatthew Dillon * swap pages in the backing object. XXX but with the 13182ad1a3f7SMatthew Dillon * new swapper this would be pretty easy to do. 13192ad1a3f7SMatthew Dillon * 13202ad1a3f7SMatthew Dillon * XXX what about anonymous MAP_SHARED memory that hasn't 13212ad1a3f7SMatthew Dillon * been ZFOD faulted yet? If we do not test for this, the 13222ad1a3f7SMatthew Dillon * shadow test may succeed! XXX 13232ad1a3f7SMatthew Dillon */ 13242ad1a3f7SMatthew Dillon if (backing_object->type != OBJT_DEFAULT) { 13252ad1a3f7SMatthew Dillon splx(s); 13262ad1a3f7SMatthew Dillon return (0); 13272ad1a3f7SMatthew Dillon } 13282ad1a3f7SMatthew Dillon } 13292ad1a3f7SMatthew Dillon if (op & OBSC_COLLAPSE_WAIT) { 13302ad1a3f7SMatthew Dillon vm_object_set_flag(backing_object, OBJ_DEAD); 13312ad1a3f7SMatthew Dillon } 13322ad1a3f7SMatthew Dillon 13332ad1a3f7SMatthew Dillon /* 13342ad1a3f7SMatthew Dillon * Our scan 13352ad1a3f7SMatthew Dillon */ 13362ad1a3f7SMatthew Dillon p = TAILQ_FIRST(&backing_object->memq); 13372ad1a3f7SMatthew Dillon while (p) { 13382ad1a3f7SMatthew Dillon vm_page_t next = TAILQ_NEXT(p, listq); 13392ad1a3f7SMatthew Dillon vm_pindex_t new_pindex = p->pindex - backing_offset_index; 13402ad1a3f7SMatthew Dillon 13412ad1a3f7SMatthew Dillon if (op & OBSC_TEST_ALL_SHADOWED) { 13422ad1a3f7SMatthew Dillon vm_page_t pp; 13432ad1a3f7SMatthew Dillon 13442ad1a3f7SMatthew Dillon /* 13452ad1a3f7SMatthew Dillon * Ignore pages outside the parent object's range 13462ad1a3f7SMatthew Dillon * and outside the parent object's mapping of the 13472ad1a3f7SMatthew Dillon * backing object. 13482ad1a3f7SMatthew Dillon * 13492ad1a3f7SMatthew Dillon * note that we do not busy the backing object's 13502ad1a3f7SMatthew Dillon * page. 13512ad1a3f7SMatthew Dillon */ 13522ad1a3f7SMatthew Dillon if ( 13532ad1a3f7SMatthew Dillon p->pindex < backing_offset_index || 13542ad1a3f7SMatthew Dillon new_pindex >= object->size 13552ad1a3f7SMatthew Dillon ) { 13562ad1a3f7SMatthew Dillon p = next; 13572ad1a3f7SMatthew Dillon continue; 13582ad1a3f7SMatthew Dillon } 13592ad1a3f7SMatthew Dillon 13602ad1a3f7SMatthew Dillon /* 13612ad1a3f7SMatthew Dillon * See if the parent has the page or if the parent's 13622ad1a3f7SMatthew Dillon * object pager has the page. If the parent has the 13632ad1a3f7SMatthew Dillon * page but the page is not valid, the parent's 13642ad1a3f7SMatthew Dillon * object pager must have the page. 13652ad1a3f7SMatthew Dillon * 13662ad1a3f7SMatthew Dillon * If this fails, the parent does not completely shadow 13672ad1a3f7SMatthew Dillon * the object and we might as well give up now. 13682ad1a3f7SMatthew Dillon */ 13692ad1a3f7SMatthew Dillon 13702ad1a3f7SMatthew Dillon pp = vm_page_lookup(object, new_pindex); 13712ad1a3f7SMatthew Dillon if ( 13722ad1a3f7SMatthew Dillon (pp == NULL || pp->valid == 0) && 13732ad1a3f7SMatthew Dillon !vm_pager_has_page(object, new_pindex, NULL, NULL) 13742ad1a3f7SMatthew Dillon ) { 13752ad1a3f7SMatthew Dillon r = 0; 13762ad1a3f7SMatthew Dillon break; 13772ad1a3f7SMatthew Dillon } 13782ad1a3f7SMatthew Dillon } 13792ad1a3f7SMatthew Dillon 13802ad1a3f7SMatthew Dillon /* 13812ad1a3f7SMatthew Dillon * Check for busy page 13822ad1a3f7SMatthew Dillon */ 13832ad1a3f7SMatthew Dillon if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) { 13842ad1a3f7SMatthew Dillon vm_page_t pp; 13852ad1a3f7SMatthew Dillon 138600f9e8b4SAlan Cox vm_page_lock_queues(); 13872ad1a3f7SMatthew Dillon if (op & OBSC_COLLAPSE_NOWAIT) { 138800f9e8b4SAlan Cox if ((p->flags & PG_BUSY) || 13892ad1a3f7SMatthew Dillon !p->valid || 13902ad1a3f7SMatthew Dillon p->hold_count || 13912ad1a3f7SMatthew Dillon p->wire_count || 139200f9e8b4SAlan Cox p->busy) { 139300f9e8b4SAlan Cox vm_page_unlock_queues(); 13942ad1a3f7SMatthew Dillon p = next; 13952ad1a3f7SMatthew Dillon continue; 13962ad1a3f7SMatthew Dillon } 13972ad1a3f7SMatthew Dillon } else if (op & OBSC_COLLAPSE_WAIT) { 139800f9e8b4SAlan Cox if (vm_page_sleep_if_busy(p, TRUE, "vmocol")) { 13992ad1a3f7SMatthew Dillon /* 14002ad1a3f7SMatthew Dillon * If we slept, anything could have 14012ad1a3f7SMatthew Dillon * happened. Since the object is 14022ad1a3f7SMatthew Dillon * marked dead, the backing offset 14032ad1a3f7SMatthew Dillon * should not have changed so we 14042ad1a3f7SMatthew Dillon * just restart our scan. 14052ad1a3f7SMatthew Dillon */ 14062ad1a3f7SMatthew Dillon p = TAILQ_FIRST(&backing_object->memq); 14072ad1a3f7SMatthew Dillon continue; 14082ad1a3f7SMatthew Dillon } 14092ad1a3f7SMatthew Dillon } 14102ad1a3f7SMatthew Dillon 14112ad1a3f7SMatthew Dillon /* 14122ad1a3f7SMatthew Dillon * Busy the page 14132ad1a3f7SMatthew Dillon */ 14142ad1a3f7SMatthew Dillon vm_page_busy(p); 141500f9e8b4SAlan Cox vm_page_unlock_queues(); 14162ad1a3f7SMatthew Dillon 14172ad1a3f7SMatthew Dillon KASSERT( 14182ad1a3f7SMatthew Dillon p->object == backing_object, 14192ad1a3f7SMatthew Dillon ("vm_object_qcollapse(): object mismatch") 14202ad1a3f7SMatthew Dillon ); 14212ad1a3f7SMatthew Dillon 14222ad1a3f7SMatthew Dillon /* 14232ad1a3f7SMatthew Dillon * Destroy any associated swap 14242ad1a3f7SMatthew Dillon */ 14252ad1a3f7SMatthew Dillon if (backing_object->type == OBJT_SWAP) { 14262ad1a3f7SMatthew Dillon swap_pager_freespace( 14272ad1a3f7SMatthew Dillon backing_object, 14282ad1a3f7SMatthew Dillon p->pindex, 14292ad1a3f7SMatthew Dillon 1 14302ad1a3f7SMatthew Dillon ); 14312ad1a3f7SMatthew Dillon } 14322ad1a3f7SMatthew Dillon 14332ad1a3f7SMatthew Dillon if ( 14342ad1a3f7SMatthew Dillon p->pindex < backing_offset_index || 14352ad1a3f7SMatthew Dillon new_pindex >= object->size 14362ad1a3f7SMatthew Dillon ) { 14372ad1a3f7SMatthew Dillon /* 14382ad1a3f7SMatthew Dillon * Page is out of the parent object's range, we 14392ad1a3f7SMatthew Dillon * can simply destroy it. 14402ad1a3f7SMatthew Dillon */ 14416a684ecfSAlan Cox vm_page_lock_queues(); 14422ad1a3f7SMatthew Dillon vm_page_protect(p, VM_PROT_NONE); 14432ad1a3f7SMatthew Dillon vm_page_free(p); 14446a684ecfSAlan Cox vm_page_unlock_queues(); 14452ad1a3f7SMatthew Dillon p = next; 14462ad1a3f7SMatthew Dillon continue; 14472ad1a3f7SMatthew Dillon } 14482ad1a3f7SMatthew Dillon 14492ad1a3f7SMatthew Dillon pp = vm_page_lookup(object, new_pindex); 14502ad1a3f7SMatthew Dillon if ( 14512ad1a3f7SMatthew Dillon pp != NULL || 14522ad1a3f7SMatthew Dillon vm_pager_has_page(object, new_pindex, NULL, NULL) 14532ad1a3f7SMatthew Dillon ) { 14542ad1a3f7SMatthew Dillon /* 14552ad1a3f7SMatthew Dillon * page already exists in parent OR swap exists 14562ad1a3f7SMatthew Dillon * for this location in the parent. Destroy 14572ad1a3f7SMatthew Dillon * the original page from the backing object. 14582ad1a3f7SMatthew Dillon * 14592ad1a3f7SMatthew Dillon * Leave the parent's page alone 14602ad1a3f7SMatthew Dillon */ 14616a684ecfSAlan Cox vm_page_lock_queues(); 14622ad1a3f7SMatthew Dillon vm_page_protect(p, VM_PROT_NONE); 14632ad1a3f7SMatthew Dillon vm_page_free(p); 14646a684ecfSAlan Cox vm_page_unlock_queues(); 14652ad1a3f7SMatthew Dillon p = next; 14662ad1a3f7SMatthew Dillon continue; 14672ad1a3f7SMatthew Dillon } 14682ad1a3f7SMatthew Dillon 14692ad1a3f7SMatthew Dillon /* 14702ad1a3f7SMatthew Dillon * Page does not exist in parent, rename the 14712ad1a3f7SMatthew Dillon * page from the backing object to the main object. 1472d1bf5d56SMatthew Dillon * 1473d1bf5d56SMatthew Dillon * If the page was mapped to a process, it can remain 1474d1bf5d56SMatthew Dillon * mapped through the rename. 14752ad1a3f7SMatthew Dillon */ 14762ad1a3f7SMatthew Dillon vm_page_rename(p, object, new_pindex); 14772ad1a3f7SMatthew Dillon /* page automatically made dirty by rename */ 14782ad1a3f7SMatthew Dillon } 14792ad1a3f7SMatthew Dillon p = next; 14802ad1a3f7SMatthew Dillon } 14812ad1a3f7SMatthew Dillon splx(s); 14822ad1a3f7SMatthew Dillon return (r); 14832ad1a3f7SMatthew Dillon } 14842ad1a3f7SMatthew Dillon 1485df8bae1dSRodney W. Grimes 1486df8bae1dSRodney W. Grimes /* 14872fe6e4d7SDavid Greenman * this version of collapse allows the operation to occur earlier and 14882fe6e4d7SDavid Greenman * when paging_in_progress is true for an object... This is not a complete 14892fe6e4d7SDavid Greenman * operation, but should plug 99.9% of the rest of the leaks. 14902fe6e4d7SDavid Greenman */ 14912fe6e4d7SDavid Greenman static void 14921b40f8c0SMatthew Dillon vm_object_qcollapse(vm_object_t object) 14932fe6e4d7SDavid Greenman { 14942ad1a3f7SMatthew Dillon vm_object_t backing_object = object->backing_object; 14952fe6e4d7SDavid Greenman 14961b40f8c0SMatthew Dillon GIANT_REQUIRED; 14971b40f8c0SMatthew Dillon 14982fe6e4d7SDavid Greenman if (backing_object->ref_count != 1) 14992fe6e4d7SDavid Greenman return; 15002fe6e4d7SDavid Greenman 1501010cf3b9SDavid Greenman backing_object->ref_count += 2; 1502010cf3b9SDavid Greenman 15032ad1a3f7SMatthew Dillon vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT); 15041c7c3c6aSMatthew Dillon 1505010cf3b9SDavid Greenman backing_object->ref_count -= 2; 15062fe6e4d7SDavid Greenman } 15072fe6e4d7SDavid Greenman 1508df8bae1dSRodney W. Grimes /* 1509df8bae1dSRodney W. Grimes * vm_object_collapse: 1510df8bae1dSRodney W. Grimes * 1511df8bae1dSRodney W. Grimes * Collapse an object with the object backing it. 1512df8bae1dSRodney W. Grimes * Pages in the backing object are moved into the 1513df8bae1dSRodney W. Grimes * parent, and the backing object is deallocated. 1514df8bae1dSRodney W. Grimes */ 151526f9a767SRodney W. Grimes void 15161b40f8c0SMatthew Dillon vm_object_collapse(vm_object_t object) 1517df8bae1dSRodney W. Grimes { 15180cddd8f0SMatthew Dillon GIANT_REQUIRED; 151923955314SAlfred Perlstein 1520df8bae1dSRodney W. Grimes while (TRUE) { 15212ad1a3f7SMatthew Dillon vm_object_t backing_object; 15222ad1a3f7SMatthew Dillon 1523df8bae1dSRodney W. Grimes /* 1524df8bae1dSRodney W. Grimes * Verify that the conditions are right for collapse: 1525df8bae1dSRodney W. Grimes * 15262ad1a3f7SMatthew Dillon * The object exists and the backing object exists. 1527df8bae1dSRodney W. Grimes */ 15282fe6e4d7SDavid Greenman if (object == NULL) 15292ad1a3f7SMatthew Dillon break; 1530df8bae1dSRodney W. Grimes 153124a1cce3SDavid Greenman if ((backing_object = object->backing_object) == NULL) 15322ad1a3f7SMatthew Dillon break; 1533df8bae1dSRodney W. Grimes 1534f919ebdeSDavid Greenman /* 1535f919ebdeSDavid Greenman * we check the backing object first, because it is most likely 153624a1cce3SDavid Greenman * not collapsable. 1537f919ebdeSDavid Greenman */ 153824a1cce3SDavid Greenman if (backing_object->handle != NULL || 153924a1cce3SDavid Greenman (backing_object->type != OBJT_DEFAULT && 154024a1cce3SDavid Greenman backing_object->type != OBJT_SWAP) || 1541f919ebdeSDavid Greenman (backing_object->flags & OBJ_DEAD) || 154224a1cce3SDavid Greenman object->handle != NULL || 154324a1cce3SDavid Greenman (object->type != OBJT_DEFAULT && 154424a1cce3SDavid Greenman object->type != OBJT_SWAP) || 154524a1cce3SDavid Greenman (object->flags & OBJ_DEAD)) { 15462ad1a3f7SMatthew Dillon break; 154724a1cce3SDavid Greenman } 15489b4814bbSDavid Greenman 15492ad1a3f7SMatthew Dillon if ( 15502ad1a3f7SMatthew Dillon object->paging_in_progress != 0 || 15512ad1a3f7SMatthew Dillon backing_object->paging_in_progress != 0 15522ad1a3f7SMatthew Dillon ) { 1553b9921222SDavid Greenman vm_object_qcollapse(object); 15542ad1a3f7SMatthew Dillon break; 1555df8bae1dSRodney W. Grimes } 1556f919ebdeSDavid Greenman 155726f9a767SRodney W. Grimes /* 15580d94caffSDavid Greenman * We know that we can either collapse the backing object (if 15592ad1a3f7SMatthew Dillon * the parent is the only reference to it) or (perhaps) have 15602ad1a3f7SMatthew Dillon * the parent bypass the object if the parent happens to shadow 15612ad1a3f7SMatthew Dillon * all the resident pages in the entire backing object. 15622ad1a3f7SMatthew Dillon * 15632ad1a3f7SMatthew Dillon * This is ignoring pager-backed pages such as swap pages. 15642ad1a3f7SMatthew Dillon * vm_object_backing_scan fails the shadowing test in this 15652ad1a3f7SMatthew Dillon * case. 1566df8bae1dSRodney W. Grimes */ 1567df8bae1dSRodney W. Grimes if (backing_object->ref_count == 1) { 1568df8bae1dSRodney W. Grimes /* 15692ad1a3f7SMatthew Dillon * If there is exactly one reference to the backing 15702ad1a3f7SMatthew Dillon * object, we can collapse it into the parent. 1571df8bae1dSRodney W. Grimes */ 15722ad1a3f7SMatthew Dillon vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT); 1573df8bae1dSRodney W. Grimes 1574df8bae1dSRodney W. Grimes /* 1575df8bae1dSRodney W. Grimes * Move the pager from backing_object to object. 1576df8bae1dSRodney W. Grimes */ 157724a1cce3SDavid Greenman if (backing_object->type == OBJT_SWAP) { 1578d474eaaaSDoug Rabson vm_object_pip_add(backing_object, 1); 157924a1cce3SDavid Greenman 158024a1cce3SDavid Greenman /* 15811c7c3c6aSMatthew Dillon * scrap the paging_offset junk and do a 15821c7c3c6aSMatthew Dillon * discrete copy. This also removes major 15831c7c3c6aSMatthew Dillon * assumptions about how the swap-pager 15841c7c3c6aSMatthew Dillon * works from where it doesn't belong. The 15851c7c3c6aSMatthew Dillon * new swapper is able to optimize the 15861c7c3c6aSMatthew Dillon * destroy-source case. 158724a1cce3SDavid Greenman */ 15881c7c3c6aSMatthew Dillon vm_object_pip_add(object, 1); 15891c7c3c6aSMatthew Dillon swap_pager_copy( 15901c7c3c6aSMatthew Dillon backing_object, 15911c7c3c6aSMatthew Dillon object, 15921c7c3c6aSMatthew Dillon OFF_TO_IDX(object->backing_object_offset), TRUE); 1593f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 1594c0503609SDavid Greenman 1595f919ebdeSDavid Greenman vm_object_pip_wakeup(backing_object); 1596c0503609SDavid Greenman } 1597df8bae1dSRodney W. Grimes /* 1598df8bae1dSRodney W. Grimes * Object now shadows whatever backing_object did. 15992ad1a3f7SMatthew Dillon * Note that the reference to 16002ad1a3f7SMatthew Dillon * backing_object->backing_object moves from within 16012ad1a3f7SMatthew Dillon * backing_object to within object. 1602df8bae1dSRodney W. Grimes */ 16032ad1a3f7SMatthew Dillon TAILQ_REMOVE( 16042ad1a3f7SMatthew Dillon &object->backing_object->shadow_head, 16052ad1a3f7SMatthew Dillon object, 16062ad1a3f7SMatthew Dillon shadow_list 16072ad1a3f7SMatthew Dillon ); 1608eaf13dd7SJohn Dyson object->backing_object->shadow_count--; 1609eaf13dd7SJohn Dyson object->backing_object->generation++; 1610de5f6a77SJohn Dyson if (backing_object->backing_object) { 16112ad1a3f7SMatthew Dillon TAILQ_REMOVE( 16122ad1a3f7SMatthew Dillon &backing_object->backing_object->shadow_head, 16132ad1a3f7SMatthew Dillon backing_object, 16142ad1a3f7SMatthew Dillon shadow_list 16152ad1a3f7SMatthew Dillon ); 1616eaf13dd7SJohn Dyson backing_object->backing_object->shadow_count--; 1617eaf13dd7SJohn Dyson backing_object->backing_object->generation++; 1618de5f6a77SJohn Dyson } 161924a1cce3SDavid Greenman object->backing_object = backing_object->backing_object; 1620de5f6a77SJohn Dyson if (object->backing_object) { 16212ad1a3f7SMatthew Dillon TAILQ_INSERT_TAIL( 16222ad1a3f7SMatthew Dillon &object->backing_object->shadow_head, 16232ad1a3f7SMatthew Dillon object, 16242ad1a3f7SMatthew Dillon shadow_list 16252ad1a3f7SMatthew Dillon ); 1626eaf13dd7SJohn Dyson object->backing_object->shadow_count++; 1627eaf13dd7SJohn Dyson object->backing_object->generation++; 1628de5f6a77SJohn Dyson } 16292fe6e4d7SDavid Greenman 16302ad1a3f7SMatthew Dillon object->backing_object_offset += 16312ad1a3f7SMatthew Dillon backing_object->backing_object_offset; 16322ad1a3f7SMatthew Dillon 1633df8bae1dSRodney W. Grimes /* 1634df8bae1dSRodney W. Grimes * Discard backing_object. 1635df8bae1dSRodney W. Grimes * 16360d94caffSDavid Greenman * Since the backing object has no pages, no pager left, 16370d94caffSDavid Greenman * and no object references within it, all that is 16380d94caffSDavid Greenman * necessary is to dispose of it. 1639df8bae1dSRodney W. Grimes */ 1640245df27cSMatthew Dillon KASSERT(backing_object->ref_count == 1, ("backing_object %p was somehow re-referenced during collapse!", backing_object)); 1641245df27cSMatthew Dillon KASSERT(TAILQ_FIRST(&backing_object->memq) == NULL, ("backing_object %p somehow has left over pages during collapse!", backing_object)); 1642df8bae1dSRodney W. Grimes 1643a5698387SAlan Cox mtx_lock(&vm_object_list_mtx); 16442ad1a3f7SMatthew Dillon TAILQ_REMOVE( 16452ad1a3f7SMatthew Dillon &vm_object_list, 16462ad1a3f7SMatthew Dillon backing_object, 16472ad1a3f7SMatthew Dillon object_list 16482ad1a3f7SMatthew Dillon ); 1649a5698387SAlan Cox mtx_unlock(&vm_object_list_mtx); 1650df8bae1dSRodney W. Grimes 1651670d17b5SJeff Roberson uma_zfree(obj_zone, backing_object); 1652df8bae1dSRodney W. Grimes 1653df8bae1dSRodney W. Grimes object_collapses++; 16540d94caffSDavid Greenman } else { 165595e5e988SJohn Dyson vm_object_t new_backing_object; 1656df8bae1dSRodney W. Grimes 1657df8bae1dSRodney W. Grimes /* 16582ad1a3f7SMatthew Dillon * If we do not entirely shadow the backing object, 16592ad1a3f7SMatthew Dillon * there is nothing we can do so we give up. 1660df8bae1dSRodney W. Grimes */ 16612ad1a3f7SMatthew Dillon if (vm_object_backing_scan(object, OBSC_TEST_ALL_SHADOWED) == 0) { 16622ad1a3f7SMatthew Dillon break; 166324a1cce3SDavid Greenman } 1664df8bae1dSRodney W. Grimes 1665df8bae1dSRodney W. Grimes /* 16660d94caffSDavid Greenman * Make the parent shadow the next object in the 16670d94caffSDavid Greenman * chain. Deallocating backing_object will not remove 16680d94caffSDavid Greenman * it, since its reference count is at least 2. 1669df8bae1dSRodney W. Grimes */ 16702ad1a3f7SMatthew Dillon TAILQ_REMOVE( 16712ad1a3f7SMatthew Dillon &backing_object->shadow_head, 16722ad1a3f7SMatthew Dillon object, 16732ad1a3f7SMatthew Dillon shadow_list 16742ad1a3f7SMatthew Dillon ); 1675eaf13dd7SJohn Dyson backing_object->shadow_count--; 1676eaf13dd7SJohn Dyson backing_object->generation++; 167795e5e988SJohn Dyson 167895e5e988SJohn Dyson new_backing_object = backing_object->backing_object; 16798aef1712SMatthew Dillon if ((object->backing_object = new_backing_object) != NULL) { 168095e5e988SJohn Dyson vm_object_reference(new_backing_object); 16812ad1a3f7SMatthew Dillon TAILQ_INSERT_TAIL( 16822ad1a3f7SMatthew Dillon &new_backing_object->shadow_head, 16832ad1a3f7SMatthew Dillon object, 16842ad1a3f7SMatthew Dillon shadow_list 16852ad1a3f7SMatthew Dillon ); 1686eaf13dd7SJohn Dyson new_backing_object->shadow_count++; 1687eaf13dd7SJohn Dyson new_backing_object->generation++; 168895e5e988SJohn Dyson object->backing_object_offset += 168995e5e988SJohn Dyson backing_object->backing_object_offset; 1690de5f6a77SJohn Dyson } 1691df8bae1dSRodney W. Grimes 1692df8bae1dSRodney W. Grimes /* 16930d94caffSDavid Greenman * Drop the reference count on backing_object. Since 16940d94caffSDavid Greenman * its ref_count was at least 2, it will not vanish; 1695eaf13dd7SJohn Dyson * so we don't need to call vm_object_deallocate, but 1696eaf13dd7SJohn Dyson * we do anyway. 1697df8bae1dSRodney W. Grimes */ 169895e5e988SJohn Dyson vm_object_deallocate(backing_object); 1699df8bae1dSRodney W. Grimes object_bypasses++; 1700df8bae1dSRodney W. Grimes } 1701df8bae1dSRodney W. Grimes 1702df8bae1dSRodney W. Grimes /* 1703df8bae1dSRodney W. Grimes * Try again with this object's new backing object. 1704df8bae1dSRodney W. Grimes */ 1705df8bae1dSRodney W. Grimes } 1706df8bae1dSRodney W. Grimes } 1707df8bae1dSRodney W. Grimes 1708df8bae1dSRodney W. Grimes /* 1709df8bae1dSRodney W. Grimes * vm_object_page_remove: [internal] 1710df8bae1dSRodney W. Grimes * 1711df8bae1dSRodney W. Grimes * Removes all physical pages in the specified 1712df8bae1dSRodney W. Grimes * object range from the object's list of pages. 1713df8bae1dSRodney W. Grimes * 1714df8bae1dSRodney W. Grimes * The object must be locked. 1715df8bae1dSRodney W. Grimes */ 171626f9a767SRodney W. Grimes void 17171b40f8c0SMatthew Dillon vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end, boolean_t clean_only) 1718df8bae1dSRodney W. Grimes { 1719d031cff1SMatthew Dillon vm_page_t p, next; 17206395da54SIan Dowse vm_pindex_t size; 1721f5ef029eSPoul-Henning Kamp int all; 1722df8bae1dSRodney W. Grimes 17239917e010SAlan Cox if (object == NULL) 1724df8bae1dSRodney W. Grimes return; 1725df8bae1dSRodney W. Grimes 17269917e010SAlan Cox mtx_lock(&Giant); 17279917e010SAlan Cox if (object->resident_page_count == 0) { 17289917e010SAlan Cox mtx_unlock(&Giant); 17299917e010SAlan Cox return; 17309917e010SAlan Cox } 173195e5e988SJohn Dyson all = ((end == 0) && (start == 0)); 173295e5e988SJohn Dyson 17338b03c8edSMatthew Dillon /* 17348b03c8edSMatthew Dillon * Since physically-backed objects do not use managed pages, we can't 17358b03c8edSMatthew Dillon * remove pages from the object (we must instead remove the page 17368b03c8edSMatthew Dillon * references, and then destroy the object). 17378b03c8edSMatthew Dillon */ 17388b03c8edSMatthew Dillon KASSERT(object->type != OBJT_PHYS, ("attempt to remove pages from a physical object")); 17398b03c8edSMatthew Dillon 1740d474eaaaSDoug Rabson vm_object_pip_add(object, 1); 174126f9a767SRodney W. Grimes again: 174232585dd6SAlan Cox vm_page_lock_queues(); 174326f9a767SRodney W. Grimes size = end - start; 1744a1a54e9fSAlan Cox if (all || size > object->resident_page_count / 4) { 1745b18bfc3dSJohn Dyson for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) { 1746b18bfc3dSJohn Dyson next = TAILQ_NEXT(p, listq); 174795e5e988SJohn Dyson if (all || ((start <= p->pindex) && (p->pindex < end))) { 1748bd7e5f99SJohn Dyson if (p->wire_count != 0) { 1749bd7e5f99SJohn Dyson vm_page_protect(p, VM_PROT_NONE); 1750ce65e68cSDavid Greenman if (!clean_only) 1751bd7e5f99SJohn Dyson p->valid = 0; 17520d94caffSDavid Greenman continue; 17530d94caffSDavid Greenman } 17540891ef4cSJohn Dyson 1755b18bfc3dSJohn Dyson /* 1756b18bfc3dSJohn Dyson * The busy flags are only cleared at 1757b18bfc3dSJohn Dyson * interrupt -- minimize the spl transitions 1758b18bfc3dSJohn Dyson */ 175932585dd6SAlan Cox if (vm_page_sleep_if_busy(p, TRUE, "vmopar")) 176026f9a767SRodney W. Grimes goto again; 17610891ef4cSJohn Dyson 17628f9110f6SJohn Dyson if (clean_only && p->valid) { 17637c1f6cedSDavid Greenman vm_page_test_dirty(p); 17647c1f6cedSDavid Greenman if (p->valid & p->dirty) 17657c1f6cedSDavid Greenman continue; 17667c1f6cedSDavid Greenman } 1767e69763a3SDoug Rabson vm_page_busy(p); 1768f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 1769df8bae1dSRodney W. Grimes vm_page_free(p); 177026f9a767SRodney W. Grimes } 177126f9a767SRodney W. Grimes } 177226f9a767SRodney W. Grimes } else { 177326f9a767SRodney W. Grimes while (size > 0) { 177432585dd6SAlan Cox if ((p = vm_page_lookup(object, start)) != NULL) { 1775bd7e5f99SJohn Dyson if (p->wire_count != 0) { 1776bd7e5f99SJohn Dyson vm_page_protect(p, VM_PROT_NONE); 1777ce65e68cSDavid Greenman if (!clean_only) 1778ce65e68cSDavid Greenman p->valid = 0; 1779bd7e5f99SJohn Dyson start += 1; 1780bd7e5f99SJohn Dyson size -= 1; 1781bd7e5f99SJohn Dyson continue; 17820d94caffSDavid Greenman } 1783eaf13dd7SJohn Dyson 1784b18bfc3dSJohn Dyson /* 1785b18bfc3dSJohn Dyson * The busy flags are only cleared at 1786b18bfc3dSJohn Dyson * interrupt -- minimize the spl transitions 1787b18bfc3dSJohn Dyson */ 178832585dd6SAlan Cox if (vm_page_sleep_if_busy(p, TRUE, "vmopar")) 178926f9a767SRodney W. Grimes goto again; 1790eaf13dd7SJohn Dyson 17918f9110f6SJohn Dyson if (clean_only && p->valid) { 17927c1f6cedSDavid Greenman vm_page_test_dirty(p); 1793bd7e5f99SJohn Dyson if (p->valid & p->dirty) { 1794bd7e5f99SJohn Dyson start += 1; 1795bd7e5f99SJohn Dyson size -= 1; 17967c1f6cedSDavid Greenman continue; 17977c1f6cedSDavid Greenman } 1798bd7e5f99SJohn Dyson } 1799e69763a3SDoug Rabson vm_page_busy(p); 1800f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 180126f9a767SRodney W. Grimes vm_page_free(p); 180226f9a767SRodney W. Grimes } 1803a316d390SJohn Dyson start += 1; 1804a316d390SJohn Dyson size -= 1; 1805df8bae1dSRodney W. Grimes } 1806df8bae1dSRodney W. Grimes } 180732585dd6SAlan Cox vm_page_unlock_queues(); 1808f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 18099917e010SAlan Cox mtx_unlock(&Giant); 1810c0503609SDavid Greenman } 1811df8bae1dSRodney W. Grimes 1812df8bae1dSRodney W. Grimes /* 1813df8bae1dSRodney W. Grimes * Routine: vm_object_coalesce 1814df8bae1dSRodney W. Grimes * Function: Coalesces two objects backing up adjoining 1815df8bae1dSRodney W. Grimes * regions of memory into a single object. 1816df8bae1dSRodney W. Grimes * 1817df8bae1dSRodney W. Grimes * returns TRUE if objects were combined. 1818df8bae1dSRodney W. Grimes * 1819df8bae1dSRodney W. Grimes * NOTE: Only works at the moment if the second object is NULL - 1820df8bae1dSRodney W. Grimes * if it's not, which object do we lock first? 1821df8bae1dSRodney W. Grimes * 1822df8bae1dSRodney W. Grimes * Parameters: 1823df8bae1dSRodney W. Grimes * prev_object First object to coalesce 1824df8bae1dSRodney W. Grimes * prev_offset Offset into prev_object 1825df8bae1dSRodney W. Grimes * next_object Second object into coalesce 1826df8bae1dSRodney W. Grimes * next_offset Offset into next_object 1827df8bae1dSRodney W. Grimes * 1828df8bae1dSRodney W. Grimes * prev_size Size of reference to prev_object 1829df8bae1dSRodney W. Grimes * next_size Size of reference to next_object 1830df8bae1dSRodney W. Grimes * 1831df8bae1dSRodney W. Grimes * Conditions: 1832df8bae1dSRodney W. Grimes * The object must *not* be locked. 1833df8bae1dSRodney W. Grimes */ 18340d94caffSDavid Greenman boolean_t 183500e1854aSAlan Cox vm_object_coalesce(vm_object_t prev_object, vm_pindex_t prev_pindex, 183600e1854aSAlan Cox vm_size_t prev_size, vm_size_t next_size) 1837df8bae1dSRodney W. Grimes { 1838ea41812fSAlan Cox vm_pindex_t next_pindex; 1839df8bae1dSRodney W. Grimes 184000e1854aSAlan Cox if (prev_object == NULL) 1841df8bae1dSRodney W. Grimes return (TRUE); 184215c176c1SAlan Cox vm_object_lock(prev_object); 18434112823fSMatthew Dillon if (prev_object->type != OBJT_DEFAULT && 18444112823fSMatthew Dillon prev_object->type != OBJT_SWAP) { 184515c176c1SAlan Cox vm_object_unlock(prev_object); 184630dcfc09SJohn Dyson return (FALSE); 184730dcfc09SJohn Dyson } 184830dcfc09SJohn Dyson 1849df8bae1dSRodney W. Grimes /* 1850df8bae1dSRodney W. Grimes * Try to collapse the object first 1851df8bae1dSRodney W. Grimes */ 1852df8bae1dSRodney W. Grimes vm_object_collapse(prev_object); 1853df8bae1dSRodney W. Grimes 1854df8bae1dSRodney W. Grimes /* 18550d94caffSDavid Greenman * Can't coalesce if: . more than one reference . paged out . shadows 18560d94caffSDavid Greenman * another object . has a copy elsewhere (any of which mean that the 18570d94caffSDavid Greenman * pages not mapped to prev_entry may be in use anyway) 1858df8bae1dSRodney W. Grimes */ 18598cc7e047SJohn Dyson if (prev_object->backing_object != NULL) { 186015c176c1SAlan Cox vm_object_unlock(prev_object); 1861df8bae1dSRodney W. Grimes return (FALSE); 1862df8bae1dSRodney W. Grimes } 1863a316d390SJohn Dyson 1864a316d390SJohn Dyson prev_size >>= PAGE_SHIFT; 1865a316d390SJohn Dyson next_size >>= PAGE_SHIFT; 1866ea41812fSAlan Cox next_pindex = prev_pindex + prev_size; 18678cc7e047SJohn Dyson 18688cc7e047SJohn Dyson if ((prev_object->ref_count > 1) && 1869ea41812fSAlan Cox (prev_object->size != next_pindex)) { 187015c176c1SAlan Cox vm_object_unlock(prev_object); 18718cc7e047SJohn Dyson return (FALSE); 18728cc7e047SJohn Dyson } 18738cc7e047SJohn Dyson 1874df8bae1dSRodney W. Grimes /* 18750d94caffSDavid Greenman * Remove any pages that may still be in the object from a previous 18760d94caffSDavid Greenman * deallocation. 1877df8bae1dSRodney W. Grimes */ 1878ea41812fSAlan Cox if (next_pindex < prev_object->size) { 1879df8bae1dSRodney W. Grimes vm_object_page_remove(prev_object, 1880ea41812fSAlan Cox next_pindex, 1881ea41812fSAlan Cox next_pindex + next_size, FALSE); 1882ea41812fSAlan Cox if (prev_object->type == OBJT_SWAP) 1883ea41812fSAlan Cox swap_pager_freespace(prev_object, 1884ea41812fSAlan Cox next_pindex, next_size); 1885ea41812fSAlan Cox } 1886df8bae1dSRodney W. Grimes 1887df8bae1dSRodney W. Grimes /* 1888df8bae1dSRodney W. Grimes * Extend the object if necessary. 1889df8bae1dSRodney W. Grimes */ 1890ea41812fSAlan Cox if (next_pindex + next_size > prev_object->size) 1891ea41812fSAlan Cox prev_object->size = next_pindex + next_size; 1892df8bae1dSRodney W. Grimes 189315c176c1SAlan Cox vm_object_unlock(prev_object); 1894df8bae1dSRodney W. Grimes return (TRUE); 1895df8bae1dSRodney W. Grimes } 1896df8bae1dSRodney W. Grimes 18977a5a6352SMatthew Dillon void 18987a5a6352SMatthew Dillon vm_object_set_writeable_dirty(vm_object_t object) 18997a5a6352SMatthew Dillon { 19007a5a6352SMatthew Dillon struct vnode *vp; 19017a5a6352SMatthew Dillon 19027a5a6352SMatthew Dillon vm_object_set_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY); 19037a5a6352SMatthew Dillon if (object->type == OBJT_VNODE && 19047a5a6352SMatthew Dillon (vp = (struct vnode *)object->handle) != NULL) { 1905e6e370a7SJeff Roberson VI_LOCK(vp); 1906e6e370a7SJeff Roberson if ((vp->v_iflag & VI_OBJDIRTY) == 0) 1907e6e370a7SJeff Roberson vp->v_iflag |= VI_OBJDIRTY; 1908e6e370a7SJeff Roberson VI_UNLOCK(vp); 19097a5a6352SMatthew Dillon } 19107a5a6352SMatthew Dillon } 19117a5a6352SMatthew Dillon 1912c0b6bbb8SAlan Cox #ifdef ENABLE_VFS_IOOPT 1913e86256c1SAlan Cox /* 1914c0b6bbb8SAlan Cox * Experimental support for zero-copy I/O 1915c0b6bbb8SAlan Cox * 1916e86256c1SAlan Cox * Performs the copy_on_write operations necessary to allow the virtual copies 1917e86256c1SAlan Cox * into user space to work. This has to be called for write(2) system calls 1918e86256c1SAlan Cox * from other processes, file unlinking, and file size shrinkage. 1919e86256c1SAlan Cox */ 1920e86256c1SAlan Cox void 1921e86256c1SAlan Cox vm_freeze_copyopts(vm_object_t object, vm_pindex_t froma, vm_pindex_t toa) 1922e86256c1SAlan Cox { 1923e86256c1SAlan Cox int rv; 1924e86256c1SAlan Cox vm_object_t robject; 1925e86256c1SAlan Cox vm_pindex_t idx; 1926e86256c1SAlan Cox 1927e86256c1SAlan Cox GIANT_REQUIRED; 1928e86256c1SAlan Cox if ((object == NULL) || 1929e86256c1SAlan Cox ((object->flags & OBJ_OPT) == 0)) 1930e86256c1SAlan Cox return; 1931e86256c1SAlan Cox 1932e86256c1SAlan Cox if (object->shadow_count > object->ref_count) 1933e86256c1SAlan Cox panic("vm_freeze_copyopts: sc > rc"); 1934e86256c1SAlan Cox 1935e86256c1SAlan Cox while ((robject = TAILQ_FIRST(&object->shadow_head)) != NULL) { 1936e86256c1SAlan Cox vm_pindex_t bo_pindex; 1937e86256c1SAlan Cox vm_page_t m_in, m_out; 1938e86256c1SAlan Cox 1939e86256c1SAlan Cox bo_pindex = OFF_TO_IDX(robject->backing_object_offset); 1940e86256c1SAlan Cox 1941e86256c1SAlan Cox vm_object_reference(robject); 1942e86256c1SAlan Cox 1943e86256c1SAlan Cox vm_object_pip_wait(robject, "objfrz"); 1944e86256c1SAlan Cox 1945e86256c1SAlan Cox if (robject->ref_count == 1) { 1946e86256c1SAlan Cox vm_object_deallocate(robject); 1947e86256c1SAlan Cox continue; 1948e86256c1SAlan Cox } 1949e86256c1SAlan Cox 1950e86256c1SAlan Cox vm_object_pip_add(robject, 1); 1951e86256c1SAlan Cox 1952e86256c1SAlan Cox for (idx = 0; idx < robject->size; idx++) { 1953e86256c1SAlan Cox 1954e86256c1SAlan Cox m_out = vm_page_grab(robject, idx, 1955e86256c1SAlan Cox VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 1956e86256c1SAlan Cox 1957e86256c1SAlan Cox if (m_out->valid == 0) { 1958e86256c1SAlan Cox m_in = vm_page_grab(object, bo_pindex + idx, 1959e86256c1SAlan Cox VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 1960e86256c1SAlan Cox if (m_in->valid == 0) { 1961e86256c1SAlan Cox rv = vm_pager_get_pages(object, &m_in, 1, 0); 1962e86256c1SAlan Cox if (rv != VM_PAGER_OK) { 1963e86256c1SAlan Cox printf("vm_freeze_copyopts: cannot read page from file: %lx\n", (long)m_in->pindex); 1964e86256c1SAlan Cox continue; 1965e86256c1SAlan Cox } 196691bb74a8SAlan Cox vm_page_lock_queues(); 1967e86256c1SAlan Cox vm_page_deactivate(m_in); 196891bb74a8SAlan Cox vm_page_unlock_queues(); 1969e86256c1SAlan Cox } 1970e86256c1SAlan Cox 1971e86256c1SAlan Cox vm_page_protect(m_in, VM_PROT_NONE); 1972e86256c1SAlan Cox pmap_copy_page(m_in, m_out); 1973e86256c1SAlan Cox m_out->valid = m_in->valid; 1974e86256c1SAlan Cox vm_page_dirty(m_out); 197599cb3c4cSAlan Cox vm_page_lock_queues(); 1976e86256c1SAlan Cox vm_page_activate(m_out); 197799cb3c4cSAlan Cox vm_page_unlock_queues(); 1978e86256c1SAlan Cox vm_page_wakeup(m_in); 1979e86256c1SAlan Cox } 1980e86256c1SAlan Cox vm_page_wakeup(m_out); 1981e86256c1SAlan Cox } 1982e86256c1SAlan Cox 1983e86256c1SAlan Cox object->shadow_count--; 1984e86256c1SAlan Cox object->ref_count--; 1985e86256c1SAlan Cox TAILQ_REMOVE(&object->shadow_head, robject, shadow_list); 1986e86256c1SAlan Cox robject->backing_object = NULL; 1987e86256c1SAlan Cox robject->backing_object_offset = 0; 1988e86256c1SAlan Cox 1989e86256c1SAlan Cox vm_object_pip_wakeup(robject); 1990e86256c1SAlan Cox vm_object_deallocate(robject); 1991e86256c1SAlan Cox } 1992e86256c1SAlan Cox 1993e86256c1SAlan Cox vm_object_clear_flag(object, OBJ_OPT); 1994e86256c1SAlan Cox } 1995c0b6bbb8SAlan Cox #endif 1996e86256c1SAlan Cox 1997c7c34a24SBruce Evans #include "opt_ddb.h" 1998c3cb3e12SDavid Greenman #ifdef DDB 1999c7c34a24SBruce Evans #include <sys/kernel.h> 2000c7c34a24SBruce Evans 2001ce9edcf5SPoul-Henning Kamp #include <sys/cons.h> 2002c7c34a24SBruce Evans 2003c7c34a24SBruce Evans #include <ddb/ddb.h> 2004c7c34a24SBruce Evans 2005cac597e4SBruce Evans static int 20061b40f8c0SMatthew Dillon _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry) 2007a1f6d91cSDavid Greenman { 2008a1f6d91cSDavid Greenman vm_map_t tmpm; 2009a1f6d91cSDavid Greenman vm_map_entry_t tmpe; 2010a1f6d91cSDavid Greenman vm_object_t obj; 2011a1f6d91cSDavid Greenman int entcount; 2012a1f6d91cSDavid Greenman 2013a1f6d91cSDavid Greenman if (map == 0) 2014a1f6d91cSDavid Greenman return 0; 2015a1f6d91cSDavid Greenman 2016a1f6d91cSDavid Greenman if (entry == 0) { 2017a1f6d91cSDavid Greenman tmpe = map->header.next; 2018a1f6d91cSDavid Greenman entcount = map->nentries; 2019a1f6d91cSDavid Greenman while (entcount-- && (tmpe != &map->header)) { 2020a1f6d91cSDavid Greenman if (_vm_object_in_map(map, object, tmpe)) { 2021a1f6d91cSDavid Greenman return 1; 2022a1f6d91cSDavid Greenman } 2023a1f6d91cSDavid Greenman tmpe = tmpe->next; 2024a1f6d91cSDavid Greenman } 20259fdfe602SMatthew Dillon } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 20269fdfe602SMatthew Dillon tmpm = entry->object.sub_map; 2027a1f6d91cSDavid Greenman tmpe = tmpm->header.next; 2028a1f6d91cSDavid Greenman entcount = tmpm->nentries; 2029a1f6d91cSDavid Greenman while (entcount-- && tmpe != &tmpm->header) { 2030a1f6d91cSDavid Greenman if (_vm_object_in_map(tmpm, object, tmpe)) { 2031a1f6d91cSDavid Greenman return 1; 2032a1f6d91cSDavid Greenman } 2033a1f6d91cSDavid Greenman tmpe = tmpe->next; 2034a1f6d91cSDavid Greenman } 20358aef1712SMatthew Dillon } else if ((obj = entry->object.vm_object) != NULL) { 203624a1cce3SDavid Greenman for (; obj; obj = obj->backing_object) 2037a1f6d91cSDavid Greenman if (obj == object) { 2038a1f6d91cSDavid Greenman return 1; 2039a1f6d91cSDavid Greenman } 2040a1f6d91cSDavid Greenman } 2041a1f6d91cSDavid Greenman return 0; 2042a1f6d91cSDavid Greenman } 2043a1f6d91cSDavid Greenman 2044cac597e4SBruce Evans static int 20451b40f8c0SMatthew Dillon vm_object_in_map(vm_object_t object) 2046a1f6d91cSDavid Greenman { 2047a1f6d91cSDavid Greenman struct proc *p; 20481005a129SJohn Baldwin 204960517fd1SJohn Baldwin /* sx_slock(&allproc_lock); */ 2050fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(p, &allproc, p_list) { 2051a1f6d91cSDavid Greenman if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */) 2052a1f6d91cSDavid Greenman continue; 2053553629ebSJake Burkholder if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) { 205460517fd1SJohn Baldwin /* sx_sunlock(&allproc_lock); */ 2055a1f6d91cSDavid Greenman return 1; 2056a1f6d91cSDavid Greenman } 2057553629ebSJake Burkholder } 205860517fd1SJohn Baldwin /* sx_sunlock(&allproc_lock); */ 2059a1f6d91cSDavid Greenman if (_vm_object_in_map(kernel_map, object, 0)) 2060a1f6d91cSDavid Greenman return 1; 2061a1f6d91cSDavid Greenman if (_vm_object_in_map(kmem_map, object, 0)) 2062a1f6d91cSDavid Greenman return 1; 2063a1f6d91cSDavid Greenman if (_vm_object_in_map(pager_map, object, 0)) 2064a1f6d91cSDavid Greenman return 1; 2065a1f6d91cSDavid Greenman if (_vm_object_in_map(buffer_map, object, 0)) 2066a1f6d91cSDavid Greenman return 1; 2067a1f6d91cSDavid Greenman return 0; 2068a1f6d91cSDavid Greenman } 2069a1f6d91cSDavid Greenman 2070c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check) 2071f708ef1bSPoul-Henning Kamp { 2072a1f6d91cSDavid Greenman vm_object_t object; 2073a1f6d91cSDavid Greenman 2074a1f6d91cSDavid Greenman /* 2075a1f6d91cSDavid Greenman * make sure that internal objs are in a map somewhere 2076a1f6d91cSDavid Greenman * and none have zero ref counts. 2077a1f6d91cSDavid Greenman */ 2078cc64b484SAlfred Perlstein TAILQ_FOREACH(object, &vm_object_list, object_list) { 207924a1cce3SDavid Greenman if (object->handle == NULL && 208024a1cce3SDavid Greenman (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) { 2081a1f6d91cSDavid Greenman if (object->ref_count == 0) { 20823efc015bSPeter Wemm db_printf("vmochk: internal obj has zero ref count: %ld\n", 20833efc015bSPeter Wemm (long)object->size); 2084a1f6d91cSDavid Greenman } 2085a1f6d91cSDavid Greenman if (!vm_object_in_map(object)) { 2086fc62ef1fSBruce Evans db_printf( 2087fc62ef1fSBruce Evans "vmochk: internal obj is not in a map: " 2088fc62ef1fSBruce Evans "ref: %d, size: %lu: 0x%lx, backing_object: %p\n", 2089fc62ef1fSBruce Evans object->ref_count, (u_long)object->size, 2090fc62ef1fSBruce Evans (u_long)object->size, 2091fc62ef1fSBruce Evans (void *)object->backing_object); 2092a1f6d91cSDavid Greenman } 2093a1f6d91cSDavid Greenman } 2094a1f6d91cSDavid Greenman } 2095a1f6d91cSDavid Greenman } 2096a1f6d91cSDavid Greenman 209726f9a767SRodney W. Grimes /* 2098df8bae1dSRodney W. Grimes * vm_object_print: [ debug ] 2099df8bae1dSRodney W. Grimes */ 2100c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static) 2101df8bae1dSRodney W. Grimes { 2102c7c34a24SBruce Evans /* XXX convert args. */ 2103c7c34a24SBruce Evans vm_object_t object = (vm_object_t)addr; 2104c7c34a24SBruce Evans boolean_t full = have_addr; 2105c7c34a24SBruce Evans 2106d031cff1SMatthew Dillon vm_page_t p; 2107df8bae1dSRodney W. Grimes 2108c7c34a24SBruce Evans /* XXX count is an (unused) arg. Avoid shadowing it. */ 2109c7c34a24SBruce Evans #define count was_count 2110c7c34a24SBruce Evans 2111d031cff1SMatthew Dillon int count; 2112df8bae1dSRodney W. Grimes 2113df8bae1dSRodney W. Grimes if (object == NULL) 2114df8bae1dSRodney W. Grimes return; 2115df8bae1dSRodney W. Grimes 2116eb95adefSBruce Evans db_iprintf( 2117e47cd172SMaxime Henrion "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x\n", 2118e47cd172SMaxime Henrion object, (int)object->type, (uintmax_t)object->size, 2119eb95adefSBruce Evans object->resident_page_count, object->ref_count, object->flags); 2120e47cd172SMaxime Henrion db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n", 21211c7c3c6aSMatthew Dillon object->shadow_count, 2122eb95adefSBruce Evans object->backing_object ? object->backing_object->ref_count : 0, 2123e47cd172SMaxime Henrion object->backing_object, (uintmax_t)object->backing_object_offset); 2124df8bae1dSRodney W. Grimes 2125df8bae1dSRodney W. Grimes if (!full) 2126df8bae1dSRodney W. Grimes return; 2127df8bae1dSRodney W. Grimes 2128c7c34a24SBruce Evans db_indent += 2; 2129df8bae1dSRodney W. Grimes count = 0; 2130fc2ffbe6SPoul-Henning Kamp TAILQ_FOREACH(p, &object->memq, listq) { 2131df8bae1dSRodney W. Grimes if (count == 0) 2132c7c34a24SBruce Evans db_iprintf("memory:="); 2133df8bae1dSRodney W. Grimes else if (count == 6) { 2134c7c34a24SBruce Evans db_printf("\n"); 2135c7c34a24SBruce Evans db_iprintf(" ..."); 2136df8bae1dSRodney W. Grimes count = 0; 2137df8bae1dSRodney W. Grimes } else 2138c7c34a24SBruce Evans db_printf(","); 2139df8bae1dSRodney W. Grimes count++; 2140df8bae1dSRodney W. Grimes 2141e47cd172SMaxime Henrion db_printf("(off=0x%jx,page=0x%jx)", 2142e47cd172SMaxime Henrion (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p)); 2143df8bae1dSRodney W. Grimes } 2144df8bae1dSRodney W. Grimes if (count != 0) 2145c7c34a24SBruce Evans db_printf("\n"); 2146c7c34a24SBruce Evans db_indent -= 2; 2147df8bae1dSRodney W. Grimes } 21485070c7f8SJohn Dyson 2149c7c34a24SBruce Evans /* XXX. */ 2150c7c34a24SBruce Evans #undef count 2151c7c34a24SBruce Evans 2152c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */ 21535070c7f8SJohn Dyson void 21541b40f8c0SMatthew Dillon vm_object_print( 21551b40f8c0SMatthew Dillon /* db_expr_t */ long addr, 21561b40f8c0SMatthew Dillon boolean_t have_addr, 21571b40f8c0SMatthew Dillon /* db_expr_t */ long count, 21581b40f8c0SMatthew Dillon char *modif) 2159c7c34a24SBruce Evans { 2160c7c34a24SBruce Evans vm_object_print_static(addr, have_addr, count, modif); 2161c7c34a24SBruce Evans } 2162c7c34a24SBruce Evans 2163c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages) 21645070c7f8SJohn Dyson { 21655070c7f8SJohn Dyson vm_object_t object; 21665070c7f8SJohn Dyson int nl = 0; 21675070c7f8SJohn Dyson int c; 2168cc64b484SAlfred Perlstein 2169cc64b484SAlfred Perlstein TAILQ_FOREACH(object, &vm_object_list, object_list) { 21705070c7f8SJohn Dyson vm_pindex_t idx, fidx; 21715070c7f8SJohn Dyson vm_pindex_t osize; 21725070c7f8SJohn Dyson vm_offset_t pa = -1, padiff; 21735070c7f8SJohn Dyson int rcount; 21745070c7f8SJohn Dyson vm_page_t m; 21755070c7f8SJohn Dyson 2176fc62ef1fSBruce Evans db_printf("new object: %p\n", (void *)object); 21775070c7f8SJohn Dyson if (nl > 18) { 21785070c7f8SJohn Dyson c = cngetc(); 21795070c7f8SJohn Dyson if (c != ' ') 21805070c7f8SJohn Dyson return; 21815070c7f8SJohn Dyson nl = 0; 21825070c7f8SJohn Dyson } 21835070c7f8SJohn Dyson nl++; 21845070c7f8SJohn Dyson rcount = 0; 21855070c7f8SJohn Dyson fidx = 0; 21865070c7f8SJohn Dyson osize = object->size; 21875070c7f8SJohn Dyson if (osize > 128) 21885070c7f8SJohn Dyson osize = 128; 21895070c7f8SJohn Dyson for (idx = 0; idx < osize; idx++) { 21905070c7f8SJohn Dyson m = vm_page_lookup(object, idx); 21915070c7f8SJohn Dyson if (m == NULL) { 21925070c7f8SJohn Dyson if (rcount) { 21933efc015bSPeter Wemm db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 21943efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 21955070c7f8SJohn Dyson if (nl > 18) { 21965070c7f8SJohn Dyson c = cngetc(); 21975070c7f8SJohn Dyson if (c != ' ') 21985070c7f8SJohn Dyson return; 21995070c7f8SJohn Dyson nl = 0; 22005070c7f8SJohn Dyson } 22015070c7f8SJohn Dyson nl++; 22025070c7f8SJohn Dyson rcount = 0; 22035070c7f8SJohn Dyson } 22045070c7f8SJohn Dyson continue; 22055070c7f8SJohn Dyson } 22065070c7f8SJohn Dyson 22075070c7f8SJohn Dyson 22085070c7f8SJohn Dyson if (rcount && 22095070c7f8SJohn Dyson (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) { 22105070c7f8SJohn Dyson ++rcount; 22115070c7f8SJohn Dyson continue; 22125070c7f8SJohn Dyson } 22135070c7f8SJohn Dyson if (rcount) { 22145070c7f8SJohn Dyson padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m); 22155070c7f8SJohn Dyson padiff >>= PAGE_SHIFT; 22165070c7f8SJohn Dyson padiff &= PQ_L2_MASK; 22175070c7f8SJohn Dyson if (padiff == 0) { 22185070c7f8SJohn Dyson pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE; 22195070c7f8SJohn Dyson ++rcount; 22205070c7f8SJohn Dyson continue; 22215070c7f8SJohn Dyson } 22223efc015bSPeter Wemm db_printf(" index(%ld)run(%d)pa(0x%lx)", 22233efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 22243efc015bSPeter Wemm db_printf("pd(%ld)\n", (long)padiff); 22255070c7f8SJohn Dyson if (nl > 18) { 22265070c7f8SJohn Dyson c = cngetc(); 22275070c7f8SJohn Dyson if (c != ' ') 22285070c7f8SJohn Dyson return; 22295070c7f8SJohn Dyson nl = 0; 22305070c7f8SJohn Dyson } 22315070c7f8SJohn Dyson nl++; 22325070c7f8SJohn Dyson } 22335070c7f8SJohn Dyson fidx = idx; 22345070c7f8SJohn Dyson pa = VM_PAGE_TO_PHYS(m); 22355070c7f8SJohn Dyson rcount = 1; 22365070c7f8SJohn Dyson } 22375070c7f8SJohn Dyson if (rcount) { 22383efc015bSPeter Wemm db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 22393efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 22405070c7f8SJohn Dyson if (nl > 18) { 22415070c7f8SJohn Dyson c = cngetc(); 22425070c7f8SJohn Dyson if (c != ' ') 22435070c7f8SJohn Dyson return; 22445070c7f8SJohn Dyson nl = 0; 22455070c7f8SJohn Dyson } 22465070c7f8SJohn Dyson nl++; 22475070c7f8SJohn Dyson } 22485070c7f8SJohn Dyson } 22495070c7f8SJohn Dyson } 2250c3cb3e12SDavid Greenman #endif /* DDB */ 2251