xref: /freebsd/sys/vm/vm_object.c (revision 1bdbd705993eab79189dff87b69a9cff5c69b17e)
160727d8bSWarner Losh /*-
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  * 4. Neither the name of the University nor the names of its contributors
17df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
18df8bae1dSRodney W. Grimes  *    without specific prior written permission.
19df8bae1dSRodney W. Grimes  *
20df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
31df8bae1dSRodney W. Grimes  *
323c4dd356SDavid Greenman  *	from: @(#)vm_object.c	8.5 (Berkeley) 3/22/94
33df8bae1dSRodney W. Grimes  *
34df8bae1dSRodney W. Grimes  *
35df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36df8bae1dSRodney W. Grimes  * All rights reserved.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
39df8bae1dSRodney W. Grimes  *
40df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
41df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
42df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
43df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
44df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
45df8bae1dSRodney W. Grimes  *
46df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49df8bae1dSRodney W. Grimes  *
50df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
51df8bae1dSRodney W. Grimes  *
52df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
53df8bae1dSRodney W. Grimes  *  School of Computer Science
54df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
55df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
56df8bae1dSRodney W. Grimes  *
57df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
58df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
59df8bae1dSRodney W. Grimes  */
60df8bae1dSRodney W. Grimes 
61df8bae1dSRodney W. Grimes /*
62df8bae1dSRodney W. Grimes  *	Virtual memory object module.
63df8bae1dSRodney W. Grimes  */
64df8bae1dSRodney W. Grimes 
65874651b1SDavid E. O'Brien #include <sys/cdefs.h>
66874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
67874651b1SDavid E. O'Brien 
68f8a47341SAlan Cox #include "opt_vm.h"
69f8a47341SAlan Cox 
70df8bae1dSRodney W. Grimes #include <sys/param.h>
71df8bae1dSRodney W. Grimes #include <sys/systm.h>
72fb919e4dSMark Murray #include <sys/lock.h>
73867a482dSJohn Dyson #include <sys/mman.h>
74cf2819ccSJohn Dyson #include <sys/mount.h>
75b9b7a4beSMatthew Dillon #include <sys/kernel.h>
76b9b7a4beSMatthew Dillon #include <sys/sysctl.h>
771b367556SJason Evans #include <sys/mutex.h>
78fb919e4dSMark Murray #include <sys/proc.h>		/* for curproc, pageproc */
79fb919e4dSMark Murray #include <sys/socket.h>
803364c323SKonstantin Belousov #include <sys/resourcevar.h>
8189f6b863SAttilio Rao #include <sys/rwlock.h>
82ff87ae35SJohn Baldwin #include <sys/user.h>
83fb919e4dSMark Murray #include <sys/vnode.h>
84fb919e4dSMark Murray #include <sys/vmmeter.h>
851005a129SJohn Baldwin #include <sys/sx.h>
86df8bae1dSRodney W. Grimes 
87df8bae1dSRodney W. Grimes #include <vm/vm.h>
88efeaf95aSDavid Greenman #include <vm/vm_param.h>
89efeaf95aSDavid Greenman #include <vm/pmap.h>
90efeaf95aSDavid Greenman #include <vm/vm_map.h>
91efeaf95aSDavid Greenman #include <vm/vm_object.h>
92df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
9326f9a767SRodney W. Grimes #include <vm/vm_pageout.h>
940d94caffSDavid Greenman #include <vm/vm_pager.h>
9505f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h>
96a1f6d91cSDavid Greenman #include <vm/vm_kern.h>
97efeaf95aSDavid Greenman #include <vm/vm_extern.h>
98774d251dSAttilio Rao #include <vm/vm_radix.h>
99f8a47341SAlan Cox #include <vm/vm_reserv.h>
100670d17b5SJeff Roberson #include <vm/uma.h>
10126f9a767SRodney W. Grimes 
102c53f7aceSDag-Erling Smørgrav static int old_msync;
103c53f7aceSDag-Erling Smørgrav SYSCTL_INT(_vm, OID_AUTO, old_msync, CTLFLAG_RW, &old_msync, 0,
104c53f7aceSDag-Erling Smørgrav     "Use old (insecure) msync behavior");
105c53f7aceSDag-Erling Smørgrav 
106757216f3SKonstantin Belousov static int	vm_object_page_collect_flush(vm_object_t object, vm_page_t p,
107126d6082SKonstantin Belousov 		    int pagerflags, int flags, boolean_t *clearobjflags,
108126d6082SKonstantin Belousov 		    boolean_t *eio);
1093280870dSKonstantin Belousov static boolean_t vm_object_page_remove_write(vm_page_t p, int flags,
110126d6082SKonstantin Belousov 		    boolean_t *clearobjflags);
111b9b7a4beSMatthew Dillon static void	vm_object_qcollapse(vm_object_t object);
11202dd8331SAlan Cox static void	vm_object_vndeallocate(vm_object_t object);
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 */
142cccf11b8SAlan Cox 
143cccf11b8SAlan Cox struct vm_object kernel_object_store;
144cccf11b8SAlan Cox struct vm_object kmem_object_store;
145df8bae1dSRodney W. Grimes 
1466472ac3dSEd Schouten static SYSCTL_NODE(_vm_stats, OID_AUTO, object, CTLFLAG_RD, 0,
1476472ac3dSEd Schouten     "VM object stats");
148604c2bbcSAlan Cox 
149f708ef1bSPoul-Henning Kamp static long object_collapses;
150604c2bbcSAlan Cox SYSCTL_LONG(_vm_stats_object, OID_AUTO, collapses, CTLFLAG_RD,
151604c2bbcSAlan Cox     &object_collapses, 0, "VM object collapses");
152604c2bbcSAlan Cox 
153f708ef1bSPoul-Henning Kamp static long object_bypasses;
154604c2bbcSAlan Cox SYSCTL_LONG(_vm_stats_object, OID_AUTO, bypasses, CTLFLAG_RD,
155604c2bbcSAlan Cox     &object_bypasses, 0, "VM object bypasses");
156dad740e9SAlan Cox 
157670d17b5SJeff Roberson static uma_zone_t obj_zone;
1588355f576SJeff Roberson 
159b23f72e9SBrian Feldman static int vm_object_zinit(void *mem, int size, int flags);
1608355f576SJeff Roberson 
1618355f576SJeff Roberson #ifdef INVARIANTS
1628355f576SJeff Roberson static void vm_object_zdtor(void *mem, int size, void *arg);
1638355f576SJeff Roberson 
1648355f576SJeff Roberson static void
1658355f576SJeff Roberson vm_object_zdtor(void *mem, int size, void *arg)
1668355f576SJeff Roberson {
1678355f576SJeff Roberson 	vm_object_t object;
1688355f576SJeff Roberson 
1698355f576SJeff Roberson 	object = (vm_object_t)mem;
170e735691bSJohn Baldwin 	KASSERT(object->ref_count == 0,
171e735691bSJohn Baldwin 	    ("object %p ref_count = %d", object, object->ref_count));
17243186e53SAlan Cox 	KASSERT(TAILQ_EMPTY(&object->memq),
173198da1b2SAttilio Rao 	    ("object %p has resident pages in its memq", object));
174774d251dSAttilio Rao 	KASSERT(vm_radix_is_empty(&object->rtree),
175774d251dSAttilio Rao 	    ("object %p has resident pages in its trie", object));
176f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
177f8a47341SAlan Cox 	KASSERT(LIST_EMPTY(&object->rvq),
178f8a47341SAlan Cox 	    ("object %p has reservations",
179f8a47341SAlan Cox 	    object));
180f8a47341SAlan Cox #endif
181c9341161SAttilio Rao 	KASSERT(vm_object_cache_is_empty(object),
1827bfda801SAlan Cox 	    ("object %p has cached pages",
1837bfda801SAlan Cox 	    object));
1848355f576SJeff Roberson 	KASSERT(object->paging_in_progress == 0,
1858355f576SJeff Roberson 	    ("object %p paging_in_progress = %d",
1868355f576SJeff Roberson 	    object, object->paging_in_progress));
1878355f576SJeff Roberson 	KASSERT(object->resident_page_count == 0,
1888355f576SJeff Roberson 	    ("object %p resident_page_count = %d",
1898355f576SJeff Roberson 	    object, object->resident_page_count));
1908355f576SJeff Roberson 	KASSERT(object->shadow_count == 0,
1918355f576SJeff Roberson 	    ("object %p shadow_count = %d",
1928355f576SJeff Roberson 	    object, object->shadow_count));
193e735691bSJohn Baldwin 	KASSERT(object->type == OBJT_DEAD,
194e735691bSJohn Baldwin 	    ("object %p has non-dead type %d",
195e735691bSJohn Baldwin 	    object, object->type));
1968355f576SJeff Roberson }
1978355f576SJeff Roberson #endif
1988355f576SJeff Roberson 
199b23f72e9SBrian Feldman static int
200b23f72e9SBrian Feldman vm_object_zinit(void *mem, int size, int flags)
2018355f576SJeff Roberson {
2028355f576SJeff Roberson 	vm_object_t object;
2038355f576SJeff Roberson 
2048355f576SJeff Roberson 	object = (vm_object_t)mem;
205777a36c5SAlan Cox 	rw_init_flags(&object->lock, "vm object", RW_DUPOK | RW_NEW);
2068355f576SJeff Roberson 
2078355f576SJeff Roberson 	/* These are true for any object that has been freed */
208e735691bSJohn Baldwin 	object->type = OBJT_DEAD;
209e735691bSJohn Baldwin 	object->ref_count = 0;
210774d251dSAttilio Rao 	object->rtree.rt_root = 0;
211e946b949SAttilio Rao 	object->rtree.rt_flags = 0;
2128355f576SJeff Roberson 	object->paging_in_progress = 0;
2138355f576SJeff Roberson 	object->resident_page_count = 0;
2148355f576SJeff Roberson 	object->shadow_count = 0;
215774d251dSAttilio Rao 	object->cache.rt_root = 0;
216e946b949SAttilio Rao 	object->cache.rt_flags = 0;
217e735691bSJohn Baldwin 
218e735691bSJohn Baldwin 	mtx_lock(&vm_object_list_mtx);
219e735691bSJohn Baldwin 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
220e735691bSJohn Baldwin 	mtx_unlock(&vm_object_list_mtx);
221b23f72e9SBrian Feldman 	return (0);
2228355f576SJeff Roberson }
223df8bae1dSRodney W. Grimes 
224a4915c21SAttilio Rao static void
2256395da54SIan Dowse _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
226df8bae1dSRodney W. Grimes {
2270cddd8f0SMatthew Dillon 
228df8bae1dSRodney W. Grimes 	TAILQ_INIT(&object->memq);
2291c500307SAlan Cox 	LIST_INIT(&object->shadow_head);
230a1f6d91cSDavid Greenman 
23124a1cce3SDavid Greenman 	object->type = type;
23228634820SAlan Cox 	switch (type) {
23328634820SAlan Cox 	case OBJT_DEAD:
23428634820SAlan Cox 		panic("_vm_object_allocate: can't create OBJT_DEAD");
23528634820SAlan Cox 	case OBJT_DEFAULT:
23628634820SAlan Cox 	case OBJT_SWAP:
23728634820SAlan Cox 		object->flags = OBJ_ONEMAPPING;
23828634820SAlan Cox 		break;
23928634820SAlan Cox 	case OBJT_DEVICE:
24028634820SAlan Cox 	case OBJT_SG:
24128634820SAlan Cox 		object->flags = OBJ_FICTITIOUS | OBJ_UNMANAGED;
24228634820SAlan Cox 		break;
24328634820SAlan Cox 	case OBJT_MGTDEVICE:
24428634820SAlan Cox 		object->flags = OBJ_FICTITIOUS;
24528634820SAlan Cox 		break;
24628634820SAlan Cox 	case OBJT_PHYS:
24728634820SAlan Cox 		object->flags = OBJ_UNMANAGED;
24828634820SAlan Cox 		break;
24928634820SAlan Cox 	case OBJT_VNODE:
25028634820SAlan Cox 		object->flags = 0;
25128634820SAlan Cox 		break;
25228634820SAlan Cox 	default:
25328634820SAlan Cox 		panic("_vm_object_allocate: type %d is undefined", type);
25428634820SAlan Cox 	}
255df8bae1dSRodney W. Grimes 	object->size = size;
256b881da26SAlan Cox 	object->generation = 1;
257a1f6d91cSDavid Greenman 	object->ref_count = 1;
2583153e878SAlan Cox 	object->memattr = VM_MEMATTR_DEFAULT;
259ef694c1aSEdward Tomasz Napierala 	object->cred = NULL;
2603364c323SKonstantin Belousov 	object->charge = 0;
26124a1cce3SDavid Greenman 	object->handle = NULL;
26224a1cce3SDavid Greenman 	object->backing_object = NULL;
263a316d390SJohn Dyson 	object->backing_object_offset = (vm_ooffset_t) 0;
264f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
265f8a47341SAlan Cox 	LIST_INIT(&object->rvq);
266f8a47341SAlan Cox #endif
267*1bdbd705SKonstantin Belousov 	umtx_shm_object_init(object);
268df8bae1dSRodney W. Grimes }
269df8bae1dSRodney W. Grimes 
270df8bae1dSRodney W. Grimes /*
27126f9a767SRodney W. Grimes  *	vm_object_init:
27226f9a767SRodney W. Grimes  *
27326f9a767SRodney W. Grimes  *	Initialize the VM objects module.
27426f9a767SRodney W. Grimes  */
27526f9a767SRodney W. Grimes void
2761b40f8c0SMatthew Dillon vm_object_init(void)
27726f9a767SRodney W. Grimes {
27826f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_list);
2796008862bSJohn Baldwin 	mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF);
2800217125fSDavid Greenman 
28189f6b863SAttilio Rao 	rw_init(&kernel_object->lock, "kernel vm object");
2829f5c801bSAlan Cox 	_vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
28326f9a767SRodney W. Grimes 	    kernel_object);
284f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
285f8a47341SAlan Cox 	kernel_object->flags |= OBJ_COLORED;
286f8a47341SAlan Cox 	kernel_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS);
287f8a47341SAlan Cox #endif
28826f9a767SRodney W. Grimes 
28989f6b863SAttilio Rao 	rw_init(&kmem_object->lock, "kmem vm object");
2909f5c801bSAlan Cox 	_vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
29126f9a767SRodney W. Grimes 	    kmem_object);
292f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
293f8a47341SAlan Cox 	kmem_object->flags |= OBJ_COLORED;
294f8a47341SAlan Cox 	kmem_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS);
295f8a47341SAlan Cox #endif
296ed6a7863SAlan Cox 
2978dbca793STor Egge 	/*
2988dbca793STor Egge 	 * The lock portion of struct vm_object must be type stable due
2998dbca793STor Egge 	 * to vm_pageout_fallback_object_lock locking a vm object
3008dbca793STor Egge 	 * without holding any references to it.
3018dbca793STor Egge 	 */
3028355f576SJeff Roberson 	obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL,
3038355f576SJeff Roberson #ifdef INVARIANTS
3048355f576SJeff Roberson 	    vm_object_zdtor,
3058355f576SJeff Roberson #else
3068355f576SJeff Roberson 	    NULL,
3078355f576SJeff Roberson #endif
3085df87b21SJeff Roberson 	    vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
309774d251dSAttilio Rao 
310774d251dSAttilio Rao 	vm_radix_init();
31199448ed1SJohn Dyson }
31299448ed1SJohn Dyson 
31399448ed1SJohn Dyson void
3141b40f8c0SMatthew Dillon vm_object_clear_flag(vm_object_t object, u_short bits)
3151b40f8c0SMatthew Dillon {
3165440b5a9SAlan Cox 
31789f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
318b06805adSJake Burkholder 	object->flags &= ~bits;
3191b40f8c0SMatthew Dillon }
3201b40f8c0SMatthew Dillon 
3213153e878SAlan Cox /*
3223153e878SAlan Cox  *	Sets the default memory attribute for the specified object.  Pages
3233153e878SAlan Cox  *	that are allocated to this object are by default assigned this memory
3243153e878SAlan Cox  *	attribute.
3253153e878SAlan Cox  *
3263153e878SAlan Cox  *	Presently, this function must be called before any pages are allocated
3273153e878SAlan Cox  *	to the object.  In the future, this requirement may be relaxed for
3283153e878SAlan Cox  *	"default" and "swap" objects.
3293153e878SAlan Cox  */
3303153e878SAlan Cox int
3313153e878SAlan Cox vm_object_set_memattr(vm_object_t object, vm_memattr_t memattr)
3323153e878SAlan Cox {
3333153e878SAlan Cox 
33489f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
3353153e878SAlan Cox 	switch (object->type) {
3363153e878SAlan Cox 	case OBJT_DEFAULT:
3373153e878SAlan Cox 	case OBJT_DEVICE:
33896b0b92aSAlan Cox 	case OBJT_MGTDEVICE:
3393153e878SAlan Cox 	case OBJT_PHYS:
34001381811SJohn Baldwin 	case OBJT_SG:
3413153e878SAlan Cox 	case OBJT_SWAP:
3423153e878SAlan Cox 	case OBJT_VNODE:
3433153e878SAlan Cox 		if (!TAILQ_EMPTY(&object->memq))
3443153e878SAlan Cox 			return (KERN_FAILURE);
3453153e878SAlan Cox 		break;
3463153e878SAlan Cox 	case OBJT_DEAD:
3473153e878SAlan Cox 		return (KERN_INVALID_ARGUMENT);
34896b0b92aSAlan Cox 	default:
34996b0b92aSAlan Cox 		panic("vm_object_set_memattr: object %p is of undefined type",
35096b0b92aSAlan Cox 		    object);
3513153e878SAlan Cox 	}
3523153e878SAlan Cox 	object->memattr = memattr;
3533153e878SAlan Cox 	return (KERN_SUCCESS);
3543153e878SAlan Cox }
3553153e878SAlan Cox 
3561b40f8c0SMatthew Dillon void
3571b40f8c0SMatthew Dillon vm_object_pip_add(vm_object_t object, short i)
3581b40f8c0SMatthew Dillon {
359f279b88dSAlan Cox 
36089f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
361b06805adSJake Burkholder 	object->paging_in_progress += i;
3621b40f8c0SMatthew Dillon }
3631b40f8c0SMatthew Dillon 
3641b40f8c0SMatthew Dillon void
3651b40f8c0SMatthew Dillon vm_object_pip_subtract(vm_object_t object, short i)
3661b40f8c0SMatthew Dillon {
367d647a0edSAlan Cox 
36889f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
369b06805adSJake Burkholder 	object->paging_in_progress -= i;
3701b40f8c0SMatthew Dillon }
3711b40f8c0SMatthew Dillon 
3721b40f8c0SMatthew Dillon void
3731b40f8c0SMatthew Dillon vm_object_pip_wakeup(vm_object_t object)
3741b40f8c0SMatthew Dillon {
375f279b88dSAlan Cox 
37689f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
377b06805adSJake Burkholder 	object->paging_in_progress--;
3781b40f8c0SMatthew Dillon 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
3791b40f8c0SMatthew Dillon 		vm_object_clear_flag(object, OBJ_PIPWNT);
3801b40f8c0SMatthew Dillon 		wakeup(object);
3811b40f8c0SMatthew Dillon 	}
3821b40f8c0SMatthew Dillon }
3831b40f8c0SMatthew Dillon 
3841b40f8c0SMatthew Dillon void
3851b40f8c0SMatthew Dillon vm_object_pip_wakeupn(vm_object_t object, short i)
3861b40f8c0SMatthew Dillon {
387d647a0edSAlan Cox 
38889f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
3891b40f8c0SMatthew Dillon 	if (i)
390b06805adSJake Burkholder 		object->paging_in_progress -= i;
3911b40f8c0SMatthew Dillon 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
3921b40f8c0SMatthew Dillon 		vm_object_clear_flag(object, OBJ_PIPWNT);
3931b40f8c0SMatthew Dillon 		wakeup(object);
3941b40f8c0SMatthew Dillon 	}
3951b40f8c0SMatthew Dillon }
3961b40f8c0SMatthew Dillon 
3971b40f8c0SMatthew Dillon void
3981b40f8c0SMatthew Dillon vm_object_pip_wait(vm_object_t object, char *waitid)
3991b40f8c0SMatthew Dillon {
4001ca58953SAlan Cox 
40189f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
4021ca58953SAlan Cox 	while (object->paging_in_progress) {
4031ca58953SAlan Cox 		object->flags |= OBJ_PIPWNT;
4040dde287bSAttilio Rao 		VM_OBJECT_SLEEP(object, object, PVM, waitid, 0);
4051ca58953SAlan Cox 	}
4061b40f8c0SMatthew Dillon }
4071b40f8c0SMatthew Dillon 
40826f9a767SRodney W. Grimes /*
40926f9a767SRodney W. Grimes  *	vm_object_allocate:
41026f9a767SRodney W. Grimes  *
41126f9a767SRodney W. Grimes  *	Returns a new object with the given size.
41226f9a767SRodney W. Grimes  */
41326f9a767SRodney W. Grimes vm_object_t
4146395da54SIan Dowse vm_object_allocate(objtype_t type, vm_pindex_t size)
41526f9a767SRodney W. Grimes {
41690688d13SAlan Cox 	vm_object_t object;
41790688d13SAlan Cox 
41890688d13SAlan Cox 	object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK);
41990688d13SAlan Cox 	_vm_object_allocate(type, size, object);
42090688d13SAlan Cox 	return (object);
42126f9a767SRodney W. Grimes }
42226f9a767SRodney W. Grimes 
42326f9a767SRodney W. Grimes 
42426f9a767SRodney W. Grimes /*
425df8bae1dSRodney W. Grimes  *	vm_object_reference:
426df8bae1dSRodney W. Grimes  *
42715347817SAlan Cox  *	Gets another reference to the given object.  Note: OBJ_DEAD
42815347817SAlan Cox  *	objects can be referenced during final cleaning.
429df8bae1dSRodney W. Grimes  */
4306476c0d2SJohn Dyson void
4311b40f8c0SMatthew Dillon vm_object_reference(vm_object_t object)
432df8bae1dSRodney W. Grimes {
433df8bae1dSRodney W. Grimes 	if (object == NULL)
434df8bae1dSRodney W. Grimes 		return;
43589f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
43652481a9aSJeff Roberson 	vm_object_reference_locked(object);
43789f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
43895e5e988SJohn Dyson }
43995e5e988SJohn Dyson 
44023955314SAlfred Perlstein /*
441b921a12bSAlan Cox  *	vm_object_reference_locked:
442b921a12bSAlan Cox  *
443b921a12bSAlan Cox  *	Gets another reference to the given object.
444b921a12bSAlan Cox  *
445b921a12bSAlan Cox  *	The object must be locked.
446b921a12bSAlan Cox  */
447b921a12bSAlan Cox void
448b921a12bSAlan Cox vm_object_reference_locked(vm_object_t object)
449b921a12bSAlan Cox {
450b921a12bSAlan Cox 	struct vnode *vp;
451b921a12bSAlan Cox 
45289f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
453b921a12bSAlan Cox 	object->ref_count++;
454b921a12bSAlan Cox 	if (object->type == OBJT_VNODE) {
455b921a12bSAlan Cox 		vp = object->handle;
456b921a12bSAlan Cox 		vref(vp);
457b921a12bSAlan Cox 	}
458b921a12bSAlan Cox }
459b921a12bSAlan Cox 
460b921a12bSAlan Cox /*
4619d5abbddSJens Schweikhardt  * Handle deallocating an object of type OBJT_VNODE.
46223955314SAlfred Perlstein  */
46302dd8331SAlan Cox static void
4641b40f8c0SMatthew Dillon vm_object_vndeallocate(vm_object_t object)
46595e5e988SJohn Dyson {
46695e5e988SJohn Dyson 	struct vnode *vp = (struct vnode *) object->handle;
467219cbf59SEivind Eklund 
46889f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
4695526d2d9SEivind Eklund 	KASSERT(object->type == OBJT_VNODE,
4705526d2d9SEivind Eklund 	    ("vm_object_vndeallocate: not a vnode object"));
471219cbf59SEivind Eklund 	KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
472219cbf59SEivind Eklund #ifdef INVARIANTS
47395e5e988SJohn Dyson 	if (object->ref_count == 0) {
47495e5e988SJohn Dyson 		vprint("vm_object_vndeallocate", vp);
47595e5e988SJohn Dyson 		panic("vm_object_vndeallocate: bad object reference count");
47695e5e988SJohn Dyson 	}
47795e5e988SJohn Dyson #endif
47895e5e988SJohn Dyson 
479*1bdbd705SKonstantin Belousov 	if (object->ref_count == 1)
480*1bdbd705SKonstantin Belousov 		umtx_shm_object_terminated(object);
481*1bdbd705SKonstantin Belousov 
48230d57414SKonstantin Belousov 	/*
48330d57414SKonstantin Belousov 	 * The test for text of vp vnode does not need a bypass to
48430d57414SKonstantin Belousov 	 * reach right VV_TEXT there, since it is obtained from
48530d57414SKonstantin Belousov 	 * object->handle.
48630d57414SKonstantin Belousov 	 */
48730d57414SKonstantin Belousov 	if (object->ref_count > 1 || (vp->v_vflag & VV_TEXT) == 0) {
48895e5e988SJohn Dyson 		object->ref_count--;
48989f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
49003fa5b34SKonstantin Belousov 		/* vrele may need the vnode lock. */
49147221757SJohn Dyson 		vrele(vp);
49203fa5b34SKonstantin Belousov 	} else {
49386769ac0SKonstantin Belousov 		vhold(vp);
49489f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
49503fa5b34SKonstantin Belousov 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
49686769ac0SKonstantin Belousov 		vdrop(vp);
49789f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
49803fa5b34SKonstantin Belousov 		object->ref_count--;
49986769ac0SKonstantin Belousov 		if (object->type == OBJT_DEAD) {
50089f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
50186769ac0SKonstantin Belousov 			VOP_UNLOCK(vp, 0);
50286769ac0SKonstantin Belousov 		} else {
50303fa5b34SKonstantin Belousov 			if (object->ref_count == 0)
504877d24acSKonstantin Belousov 				VOP_UNSET_TEXT(vp);
50589f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
50603fa5b34SKonstantin Belousov 			vput(vp);
50703fa5b34SKonstantin Belousov 		}
508df8bae1dSRodney W. Grimes 	}
50986769ac0SKonstantin Belousov }
510df8bae1dSRodney W. Grimes 
511df8bae1dSRodney W. Grimes /*
512df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
513df8bae1dSRodney W. Grimes  *
514df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
515df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
516df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
517df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
518df8bae1dSRodney W. Grimes  *	may be relinquished.
519df8bae1dSRodney W. Grimes  *
520df8bae1dSRodney W. Grimes  *	No object may be locked.
521df8bae1dSRodney W. Grimes  */
52226f9a767SRodney W. Grimes void
5231b40f8c0SMatthew Dillon vm_object_deallocate(vm_object_t object)
524df8bae1dSRodney W. Grimes {
525df8bae1dSRodney W. Grimes 	vm_object_t temp;
5266f2af3fcSKonstantin Belousov 	struct vnode *vp;
527df8bae1dSRodney W. Grimes 
528df8bae1dSRodney W. Grimes 	while (object != NULL) {
52989f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
5303b68228cSAlan Cox 		if (object->type == OBJT_VNODE) {
53195e5e988SJohn Dyson 			vm_object_vndeallocate(object);
53223b186d3SAlan Cox 			return;
5335050aa86SKonstantin Belousov 		}
53495e5e988SJohn Dyson 
5358125b1e6SAlfred Perlstein 		KASSERT(object->ref_count != 0,
5368125b1e6SAlfred Perlstein 			("vm_object_deallocate: object deallocated too many times: %d", object->type));
5372be70f79SJohn Dyson 
5382be70f79SJohn Dyson 		/*
5398125b1e6SAlfred Perlstein 		 * If the reference count goes to 0 we start calling
5408125b1e6SAlfred Perlstein 		 * vm_object_terminate() on the object chain.
5418125b1e6SAlfred Perlstein 		 * A ref count of 1 may be a special case depending on the
5428125b1e6SAlfred Perlstein 		 * shadow count being 0 or 1.
5432be70f79SJohn Dyson 		 */
544c0877f10SJohn Dyson 		object->ref_count--;
5458125b1e6SAlfred Perlstein 		if (object->ref_count > 1) {
54689f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
54723b186d3SAlan Cox 			return;
5488125b1e6SAlfred Perlstein 		} else if (object->ref_count == 1) {
5496f2af3fcSKonstantin Belousov 			if (object->type == OBJT_SWAP &&
5506f2af3fcSKonstantin Belousov 			    (object->flags & OBJ_TMPFS) != 0) {
5516f2af3fcSKonstantin Belousov 				vp = object->un_pager.swp.swp_tmpfs;
5526f2af3fcSKonstantin Belousov 				vhold(vp);
5536f2af3fcSKonstantin Belousov 				VM_OBJECT_WUNLOCK(object);
5546f2af3fcSKonstantin Belousov 				vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
5556f2af3fcSKonstantin Belousov 				VM_OBJECT_WLOCK(object);
5567560005cSKonstantin Belousov 				if (object->type == OBJT_DEAD ||
5577560005cSKonstantin Belousov 				    object->ref_count != 1) {
5586f2af3fcSKonstantin Belousov 					VM_OBJECT_WUNLOCK(object);
5596f2af3fcSKonstantin Belousov 					VOP_UNLOCK(vp, 0);
5602309fa9bSKonstantin Belousov 					vdrop(vp);
5616f2af3fcSKonstantin Belousov 					return;
5627560005cSKonstantin Belousov 				}
5637560005cSKonstantin Belousov 				if ((object->flags & OBJ_TMPFS) != 0)
5646f2af3fcSKonstantin Belousov 					VOP_UNSET_TEXT(vp);
5656f2af3fcSKonstantin Belousov 				VOP_UNLOCK(vp, 0);
5662309fa9bSKonstantin Belousov 				vdrop(vp);
5676f2af3fcSKonstantin Belousov 			}
5684c8e0452SAlan Cox 			if (object->shadow_count == 0 &&
5694c8e0452SAlan Cox 			    object->handle == NULL &&
5704c8e0452SAlan Cox 			    (object->type == OBJT_DEFAULT ||
5716f2af3fcSKonstantin Belousov 			    (object->type == OBJT_SWAP &&
572f08f7dcaSKonstantin Belousov 			    (object->flags & OBJ_TMPFS_NODE) == 0))) {
5738125b1e6SAlfred Perlstein 				vm_object_set_flag(object, OBJ_ONEMAPPING);
5748125b1e6SAlfred Perlstein 			} else if ((object->shadow_count == 1) &&
5758125b1e6SAlfred Perlstein 			    (object->handle == NULL) &&
57624a1cce3SDavid Greenman 			    (object->type == OBJT_DEFAULT ||
57724a1cce3SDavid Greenman 			     object->type == OBJT_SWAP)) {
578a1f6d91cSDavid Greenman 				vm_object_t robject;
57995e5e988SJohn Dyson 
5801c500307SAlan Cox 				robject = LIST_FIRST(&object->shadow_head);
5815526d2d9SEivind Eklund 				KASSERT(robject != NULL,
582219cbf59SEivind Eklund 				    ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
5835526d2d9SEivind Eklund 					 object->ref_count,
5845526d2d9SEivind Eklund 					 object->shadow_count));
5854bace8e7SKonstantin Belousov 				KASSERT((robject->flags & OBJ_TMPFS_NODE) == 0,
5864bace8e7SKonstantin Belousov 				    ("shadowed tmpfs v_object %p", object));
58789f6b863SAttilio Rao 				if (!VM_OBJECT_TRYWLOCK(robject)) {
588b72b0115SAlan Cox 					/*
589b72b0115SAlan Cox 					 * Avoid a potential deadlock.
590b72b0115SAlan Cox 					 */
591b72b0115SAlan Cox 					object->ref_count++;
59289f6b863SAttilio Rao 					VM_OBJECT_WUNLOCK(object);
593a7d86121SAlan Cox 					/*
594a7d86121SAlan Cox 					 * More likely than not the thread
595a7d86121SAlan Cox 					 * holding robject's lock has lower
596a7d86121SAlan Cox 					 * priority than the current thread.
597a7d86121SAlan Cox 					 * Let the lower priority thread run.
598a7d86121SAlan Cox 					 */
5998db5fc58SJohn Baldwin 					pause("vmo_de", 1);
600b72b0115SAlan Cox 					continue;
601b72b0115SAlan Cox 				}
602d936694fSAlan Cox 				/*
603d936694fSAlan Cox 				 * Collapse object into its shadow unless its
604d936694fSAlan Cox 				 * shadow is dead.  In that case, object will
605d936694fSAlan Cox 				 * be deallocated by the thread that is
606d936694fSAlan Cox 				 * deallocating its shadow.
607d936694fSAlan Cox 				 */
608d936694fSAlan Cox 				if ((robject->flags & OBJ_DEAD) == 0 &&
609d936694fSAlan Cox 				    (robject->handle == NULL) &&
61024a1cce3SDavid Greenman 				    (robject->type == OBJT_DEFAULT ||
61124a1cce3SDavid Greenman 				     robject->type == OBJT_SWAP)) {
612a1f6d91cSDavid Greenman 
61395e5e988SJohn Dyson 					robject->ref_count++;
614138449dcSAlan Cox retry:
615138449dcSAlan Cox 					if (robject->paging_in_progress) {
61689f6b863SAttilio Rao 						VM_OBJECT_WUNLOCK(object);
617138449dcSAlan Cox 						vm_object_pip_wait(robject,
618138449dcSAlan Cox 						    "objde1");
6192e9f4a69SAlan Cox 						temp = robject->backing_object;
6202e9f4a69SAlan Cox 						if (object == temp) {
62189f6b863SAttilio Rao 							VM_OBJECT_WLOCK(object);
622138449dcSAlan Cox 							goto retry;
6232e9f4a69SAlan Cox 						}
624138449dcSAlan Cox 					} else if (object->paging_in_progress) {
62589f6b863SAttilio Rao 						VM_OBJECT_WUNLOCK(robject);
626138449dcSAlan Cox 						object->flags |= OBJ_PIPWNT;
6270dde287bSAttilio Rao 						VM_OBJECT_SLEEP(object, object,
628138449dcSAlan Cox 						    PDROP | PVM, "objde2", 0);
62989f6b863SAttilio Rao 						VM_OBJECT_WLOCK(robject);
6302e9f4a69SAlan Cox 						temp = robject->backing_object;
6312e9f4a69SAlan Cox 						if (object == temp) {
63289f6b863SAttilio Rao 							VM_OBJECT_WLOCK(object);
633138449dcSAlan Cox 							goto retry;
634a1f6d91cSDavid Greenman 						}
6352e9f4a69SAlan Cox 					} else
63689f6b863SAttilio Rao 						VM_OBJECT_WUNLOCK(object);
6372e9f4a69SAlan Cox 
63895e5e988SJohn Dyson 					if (robject->ref_count == 1) {
63995e5e988SJohn Dyson 						robject->ref_count--;
640ba8da839SDavid Greenman 						object = robject;
64195e5e988SJohn Dyson 						goto doterm;
64295e5e988SJohn Dyson 					}
64395e5e988SJohn Dyson 					object = robject;
64495e5e988SJohn Dyson 					vm_object_collapse(object);
64589f6b863SAttilio Rao 					VM_OBJECT_WUNLOCK(object);
646ba8da839SDavid Greenman 					continue;
647a1f6d91cSDavid Greenman 				}
64889f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(robject);
64995e5e988SJohn Dyson 			}
65089f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
65123b186d3SAlan Cox 			return;
65295e5e988SJohn Dyson 		}
65395e5e988SJohn Dyson doterm:
654*1bdbd705SKonstantin Belousov 		umtx_shm_object_terminated(object);
65524a1cce3SDavid Greenman 		temp = object->backing_object;
656c9917419SAlan Cox 		if (temp != NULL) {
6574bace8e7SKonstantin Belousov 			KASSERT((object->flags & OBJ_TMPFS_NODE) == 0,
6584bace8e7SKonstantin Belousov 			    ("shadowed tmpfs v_object 2 %p", object));
65989f6b863SAttilio Rao 			VM_OBJECT_WLOCK(temp);
6601c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
66195e5e988SJohn Dyson 			temp->shadow_count--;
66289f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(temp);
66395461b45SJohn Dyson 			object->backing_object = NULL;
664de5f6a77SJohn Dyson 		}
665245df27cSMatthew Dillon 		/*
666245df27cSMatthew Dillon 		 * Don't double-terminate, we could be in a termination
667245df27cSMatthew Dillon 		 * recursion due to the terminate having to sync data
668245df27cSMatthew Dillon 		 * to disk.
669245df27cSMatthew Dillon 		 */
670245df27cSMatthew Dillon 		if ((object->flags & OBJ_DEAD) == 0)
671df8bae1dSRodney W. Grimes 			vm_object_terminate(object);
672c829b9d0SAlan Cox 		else
67389f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
674df8bae1dSRodney W. Grimes 		object = temp;
675df8bae1dSRodney W. Grimes 	}
676df8bae1dSRodney W. Grimes }
677df8bae1dSRodney W. Grimes 
678df8bae1dSRodney W. Grimes /*
6792ac78f0eSStephan Uphoff  *	vm_object_destroy removes the object from the global object list
6802ac78f0eSStephan Uphoff  *      and frees the space for the object.
6812ac78f0eSStephan Uphoff  */
6822ac78f0eSStephan Uphoff void
6832ac78f0eSStephan Uphoff vm_object_destroy(vm_object_t object)
6842ac78f0eSStephan Uphoff {
6852ac78f0eSStephan Uphoff 
6862ac78f0eSStephan Uphoff 	/*
6873364c323SKonstantin Belousov 	 * Release the allocation charge.
6883364c323SKonstantin Belousov 	 */
689ef694c1aSEdward Tomasz Napierala 	if (object->cred != NULL) {
690ef694c1aSEdward Tomasz Napierala 		swap_release_by_cred(object->charge, object->cred);
6913364c323SKonstantin Belousov 		object->charge = 0;
692ef694c1aSEdward Tomasz Napierala 		crfree(object->cred);
693ef694c1aSEdward Tomasz Napierala 		object->cred = NULL;
6943364c323SKonstantin Belousov 	}
6953364c323SKonstantin Belousov 
6963364c323SKonstantin Belousov 	/*
6972ac78f0eSStephan Uphoff 	 * Free the space for the object.
6982ac78f0eSStephan Uphoff 	 */
6992ac78f0eSStephan Uphoff 	uma_zfree(obj_zone, object);
7002ac78f0eSStephan Uphoff }
7012ac78f0eSStephan Uphoff 
7022ac78f0eSStephan Uphoff /*
703df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
704df8bae1dSRodney W. Grimes  *	up all previously used resources.
705df8bae1dSRodney W. Grimes  *
706df8bae1dSRodney W. Grimes  *	The object must be locked.
7071c7c3c6aSMatthew Dillon  *	This routine may block.
708df8bae1dSRodney W. Grimes  */
70995e5e988SJohn Dyson void
7101b40f8c0SMatthew Dillon vm_object_terminate(vm_object_t object)
711df8bae1dSRodney W. Grimes {
71217ea6f00SAlan Cox 	vm_page_t p, p_next;
713df8bae1dSRodney W. Grimes 
71489f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
7150cddd8f0SMatthew Dillon 
71695e5e988SJohn Dyson 	/*
71795e5e988SJohn Dyson 	 * Make sure no one uses us.
71895e5e988SJohn Dyson 	 */
719069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_DEAD);
7203c631446SJohn Dyson 
721df8bae1dSRodney W. Grimes 	/*
722f6b04d2bSDavid Greenman 	 * wait for the pageout daemon to be done with the object
723df8bae1dSRodney W. Grimes 	 */
72466095752SJohn Dyson 	vm_object_pip_wait(object, "objtrm");
725df8bae1dSRodney W. Grimes 
7265526d2d9SEivind Eklund 	KASSERT(!object->paging_in_progress,
7275526d2d9SEivind Eklund 		("vm_object_terminate: pageout in progress"));
72826f9a767SRodney W. Grimes 
72926f9a767SRodney W. Grimes 	/*
7300d94caffSDavid Greenman 	 * Clean and free the pages, as appropriate. All references to the
7310d94caffSDavid Greenman 	 * object are gone, so we don't need to lock it.
73226f9a767SRodney W. Grimes 	 */
73324a1cce3SDavid Greenman 	if (object->type == OBJT_VNODE) {
734f7dd7b63SAlan Cox 		struct vnode *vp = (struct vnode *)object->handle;
73595e5e988SJohn Dyson 
73695e5e988SJohn Dyson 		/*
73795e5e988SJohn Dyson 		 * Clean pages and flush buffers.
73895e5e988SJohn Dyson 		 */
7398f9110f6SJohn Dyson 		vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
74089f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
74195e5e988SJohn Dyson 
7420d7935fdSAttilio Rao 		vinvalbuf(vp, V_SAVE, 0, 0);
743f7dd7b63SAlan Cox 
74489f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
745bef608bdSJohn Dyson 	}
746bef608bdSJohn Dyson 
747971dd342SAlfred Perlstein 	KASSERT(object->ref_count == 0,
748971dd342SAlfred Perlstein 		("vm_object_terminate: object with references, ref_count=%d",
749971dd342SAlfred Perlstein 		object->ref_count));
750996c772fSJohn Dyson 
7510d94caffSDavid Greenman 	/*
75217ea6f00SAlan Cox 	 * Free any remaining pageable pages.  This also removes them from the
75317ea6f00SAlan Cox 	 * paging queues.  However, don't free wired pages, just remove them
75417ea6f00SAlan Cox 	 * from the object.  Rather than incrementally removing each page from
75517ea6f00SAlan Cox 	 * the object, the page and object are reset to any empty state.
756df8bae1dSRodney W. Grimes 	 */
75717ea6f00SAlan Cox 	TAILQ_FOREACH_SAFE(p, &object->memq, listq, p_next) {
758c7aebda8SAttilio Rao 		vm_page_assert_unbusied(p);
7592965a453SKip Macy 		vm_page_lock(p);
76017ea6f00SAlan Cox 		/*
76117ea6f00SAlan Cox 		 * Optimize the page's removal from the object by resetting
76217ea6f00SAlan Cox 		 * its "object" field.  Specifically, if the page is not
76317ea6f00SAlan Cox 		 * wired, then the effect of this assignment is that
76417ea6f00SAlan Cox 		 * vm_page_free()'s call to vm_page_remove() will return
76517ea6f00SAlan Cox 		 * immediately without modifying the page or the object.
76617ea6f00SAlan Cox 		 */
76717ea6f00SAlan Cox 		p->object = NULL;
7680b10ba98SDavid Greenman 		if (p->wire_count == 0) {
769df8bae1dSRodney W. Grimes 			vm_page_free(p);
77070721880SAlan Cox 			PCPU_INC(cnt.v_pfree);
77117ea6f00SAlan Cox 		}
7722965a453SKip Macy 		vm_page_unlock(p);
7732965a453SKip Macy 	}
77417ea6f00SAlan Cox 	/*
77517ea6f00SAlan Cox 	 * If the object contained any pages, then reset it to an empty state.
77617ea6f00SAlan Cox 	 * None of the object's fields, including "resident_page_count", were
77717ea6f00SAlan Cox 	 * modified by the preceding loop.
77817ea6f00SAlan Cox 	 */
77917ea6f00SAlan Cox 	if (object->resident_page_count != 0) {
780774d251dSAttilio Rao 		vm_radix_reclaim_allnodes(&object->rtree);
78117ea6f00SAlan Cox 		TAILQ_INIT(&object->memq);
78217ea6f00SAlan Cox 		object->resident_page_count = 0;
78317ea6f00SAlan Cox 		if (object->type == OBJT_VNODE)
78417ea6f00SAlan Cox 			vdrop(object->handle);
78517ea6f00SAlan Cox 	}
786bef608bdSJohn Dyson 
787f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
788f8a47341SAlan Cox 	if (__predict_false(!LIST_EMPTY(&object->rvq)))
789f8a47341SAlan Cox 		vm_reserv_break_all(object);
790f8a47341SAlan Cox #endif
791c9341161SAttilio Rao 	if (__predict_false(!vm_object_cache_is_empty(object)))
792c9444914SAlan Cox 		vm_page_cache_free(object, 0, 0);
7937bfda801SAlan Cox 
794e735691bSJohn Baldwin 	KASSERT(object->cred == NULL || object->type == OBJT_DEFAULT ||
795e735691bSJohn Baldwin 	    object->type == OBJT_SWAP,
796e735691bSJohn Baldwin 	    ("%s: non-swap obj %p has cred", __func__, object));
797e735691bSJohn Baldwin 
7982d8acc0fSJohn Dyson 	/*
7999fcfb650SDavid Greenman 	 * Let the pager know object is dead.
8009fcfb650SDavid Greenman 	 */
8019fcfb650SDavid Greenman 	vm_pager_deallocate(object);
80289f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
8039fcfb650SDavid Greenman 
8042ac78f0eSStephan Uphoff 	vm_object_destroy(object);
80547221757SJohn Dyson }
806df8bae1dSRodney W. Grimes 
807edf93b25SAlan Cox /*
808edf93b25SAlan Cox  * Make the page read-only so that we can clear the object flags.  However, if
809edf93b25SAlan Cox  * this is a nosync mmap then the object is likely to stay dirty so do not
810edf93b25SAlan Cox  * mess with the page and do not clear the object flags.  Returns TRUE if the
811edf93b25SAlan Cox  * page should be flushed, and FALSE otherwise.
812edf93b25SAlan Cox  */
8133280870dSKonstantin Belousov static boolean_t
814126d6082SKonstantin Belousov vm_object_page_remove_write(vm_page_t p, int flags, boolean_t *clearobjflags)
8153280870dSKonstantin Belousov {
8163280870dSKonstantin Belousov 
8173280870dSKonstantin Belousov 	/*
8183280870dSKonstantin Belousov 	 * If we have been asked to skip nosync pages and this is a
8193280870dSKonstantin Belousov 	 * nosync page, skip it.  Note that the object flags were not
8203280870dSKonstantin Belousov 	 * cleared in this case so we do not have to set them.
8213280870dSKonstantin Belousov 	 */
8223280870dSKonstantin Belousov 	if ((flags & OBJPC_NOSYNC) != 0 && (p->oflags & VPO_NOSYNC) != 0) {
823126d6082SKonstantin Belousov 		*clearobjflags = FALSE;
8243280870dSKonstantin Belousov 		return (FALSE);
8253280870dSKonstantin Belousov 	} else {
8263280870dSKonstantin Belousov 		pmap_remove_write(p);
8273280870dSKonstantin Belousov 		return (p->dirty != 0);
8283280870dSKonstantin Belousov 	}
8293280870dSKonstantin Belousov }
8303280870dSKonstantin Belousov 
831df8bae1dSRodney W. Grimes /*
832df8bae1dSRodney W. Grimes  *	vm_object_page_clean
833df8bae1dSRodney W. Grimes  *
8344f79d873SMatthew Dillon  *	Clean all dirty pages in the specified range of object.  Leaves page
8354f79d873SMatthew Dillon  * 	on whatever queue it is currently on.   If NOSYNC is set then do not
836b146f9e5SAlan Cox  *	write out pages with VPO_NOSYNC set (originally comes from MAP_NOSYNC),
8374f79d873SMatthew Dillon  *	leaving the object dirty.
83826f9a767SRodney W. Grimes  *
83943b7990eSMatthew Dillon  *	When stuffing pages asynchronously, allow clustering.  XXX we need a
84043b7990eSMatthew Dillon  *	synchronous clustering mode implementation.
84143b7990eSMatthew Dillon  *
84226f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
84326f9a767SRodney W. Grimes  *
84426f9a767SRodney W. Grimes  *	The object must be locked.
845126d6082SKonstantin Belousov  *
846126d6082SKonstantin Belousov  *	Returns FALSE if some page from the range was not written, as
847126d6082SKonstantin Belousov  *	reported by the pager, and TRUE otherwise.
84826f9a767SRodney W. Grimes  */
849126d6082SKonstantin Belousov boolean_t
85017f3095dSAlan Cox vm_object_page_clean(vm_object_t object, vm_ooffset_t start, vm_ooffset_t end,
851e239bb97SKonstantin Belousov     int flags)
852f6b04d2bSDavid Greenman {
853e239bb97SKonstantin Belousov 	vm_page_t np, p;
85417f3095dSAlan Cox 	vm_pindex_t pi, tend, tstart;
855126d6082SKonstantin Belousov 	int curgeneration, n, pagerflags;
856126d6082SKonstantin Belousov 	boolean_t clearobjflags, eio, res;
857f6b04d2bSDavid Greenman 
85889f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
859e5f299ffSKonstantin Belousov 
860e5f299ffSKonstantin Belousov 	/*
861e5f299ffSKonstantin Belousov 	 * The OBJ_MIGHTBEDIRTY flag is only set for OBJT_VNODE
862e5f299ffSKonstantin Belousov 	 * objects.  The check below prevents the function from
863e5f299ffSKonstantin Belousov 	 * operating on non-vnode objects.
864e5f299ffSKonstantin Belousov 	 */
865e239bb97SKonstantin Belousov 	if ((object->flags & OBJ_MIGHTBEDIRTY) == 0 ||
866e239bb97SKonstantin Belousov 	    object->resident_page_count == 0)
867126d6082SKonstantin Belousov 		return (TRUE);
868f6b04d2bSDavid Greenman 
869e239bb97SKonstantin Belousov 	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) != 0 ?
870e239bb97SKonstantin Belousov 	    VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
871e239bb97SKonstantin Belousov 	pagerflags |= (flags & OBJPC_INVAL) != 0 ? VM_PAGER_PUT_INVAL : 0;
872e239bb97SKonstantin Belousov 
87317f3095dSAlan Cox 	tstart = OFF_TO_IDX(start);
87417f3095dSAlan Cox 	tend = (end == 0) ? object->size : OFF_TO_IDX(end + PAGE_MASK);
87517f3095dSAlan Cox 	clearobjflags = tstart == 0 && tend >= object->size;
876126d6082SKonstantin Belousov 	res = TRUE;
877f6b04d2bSDavid Greenman 
878bd7e5f99SJohn Dyson rescan:
8792d8acc0fSJohn Dyson 	curgeneration = object->generation;
8802d8acc0fSJohn Dyson 
88117f3095dSAlan Cox 	for (p = vm_page_find_least(object, tstart); p != NULL; p = np) {
882bd7e5f99SJohn Dyson 		pi = p->pindex;
883e239bb97SKonstantin Belousov 		if (pi >= tend)
884e239bb97SKonstantin Belousov 			break;
885e239bb97SKonstantin Belousov 		np = TAILQ_NEXT(p, listq);
886e239bb97SKonstantin Belousov 		if (p->valid == 0)
887aef922f5SJohn Dyson 			continue;
888c7aebda8SAttilio Rao 		if (vm_page_sleep_if_busy(p, "vpcwai")) {
889e65919f9SKonstantin Belousov 			if (object->generation != curgeneration) {
890e65919f9SKonstantin Belousov 				if ((flags & OBJPC_SYNC) != 0)
891e239bb97SKonstantin Belousov 					goto rescan;
892e65919f9SKonstantin Belousov 				else
893126d6082SKonstantin Belousov 					clearobjflags = FALSE;
894e65919f9SKonstantin Belousov 			}
895780636b7SKonstantin Belousov 			np = vm_page_find_least(object, pi);
896780636b7SKonstantin Belousov 			continue;
897f6b04d2bSDavid Greenman 		}
8983280870dSKonstantin Belousov 		if (!vm_object_page_remove_write(p, flags, &clearobjflags))
899bd7e5f99SJohn Dyson 			continue;
900e239bb97SKonstantin Belousov 
9013280870dSKonstantin Belousov 		n = vm_object_page_collect_flush(object, p, pagerflags,
902126d6082SKonstantin Belousov 		    flags, &clearobjflags, &eio);
903126d6082SKonstantin Belousov 		if (eio) {
904126d6082SKonstantin Belousov 			res = FALSE;
905126d6082SKonstantin Belousov 			clearobjflags = FALSE;
906126d6082SKonstantin Belousov 		}
907e65919f9SKonstantin Belousov 		if (object->generation != curgeneration) {
908e65919f9SKonstantin Belousov 			if ((flags & OBJPC_SYNC) != 0)
909b9b7a4beSMatthew Dillon 				goto rescan;
910e65919f9SKonstantin Belousov 			else
911126d6082SKonstantin Belousov 				clearobjflags = FALSE;
912e65919f9SKonstantin Belousov 		}
913031ec8c1SKonstantin Belousov 
914031ec8c1SKonstantin Belousov 		/*
915031ec8c1SKonstantin Belousov 		 * If the VOP_PUTPAGES() did a truncated write, so
916031ec8c1SKonstantin Belousov 		 * that even the first page of the run is not fully
917031ec8c1SKonstantin Belousov 		 * written, vm_pageout_flush() returns 0 as the run
918031ec8c1SKonstantin Belousov 		 * length.  Since the condition that caused truncated
919031ec8c1SKonstantin Belousov 		 * write may be permanent, e.g. exhausted free space,
920031ec8c1SKonstantin Belousov 		 * accepting n == 0 would cause an infinite loop.
921031ec8c1SKonstantin Belousov 		 *
922031ec8c1SKonstantin Belousov 		 * Forwarding the iterator leaves the unwritten page
923031ec8c1SKonstantin Belousov 		 * behind, but there is not much we can do there if
924031ec8c1SKonstantin Belousov 		 * filesystem refuses to write it.
925031ec8c1SKonstantin Belousov 		 */
926126d6082SKonstantin Belousov 		if (n == 0) {
927031ec8c1SKonstantin Belousov 			n = 1;
928126d6082SKonstantin Belousov 			clearobjflags = FALSE;
929126d6082SKonstantin Belousov 		}
930e239bb97SKonstantin Belousov 		np = vm_page_find_least(object, pi + n);
931b9b7a4beSMatthew Dillon 	}
932b9b7a4beSMatthew Dillon #if 0
933e239bb97SKonstantin Belousov 	VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC) ? MNT_WAIT : 0);
934b9b7a4beSMatthew Dillon #endif
935b9b7a4beSMatthew Dillon 
936edf93b25SAlan Cox 	if (clearobjflags)
9373280870dSKonstantin Belousov 		vm_object_clear_flag(object, OBJ_MIGHTBEDIRTY);
938126d6082SKonstantin Belousov 	return (res);
939b9b7a4beSMatthew Dillon }
940b9b7a4beSMatthew Dillon 
941b9b7a4beSMatthew Dillon static int
9423280870dSKonstantin Belousov vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int pagerflags,
943126d6082SKonstantin Belousov     int flags, boolean_t *clearobjflags, boolean_t *eio)
944b9b7a4beSMatthew Dillon {
9453157c503SKonstantin Belousov 	vm_page_t ma[vm_pageout_page_count], p_first, tp;
9463157c503SKonstantin Belousov 	int count, i, mreq, runlen;
947b9b7a4beSMatthew Dillon 
9487bec141bSKip Macy 	vm_page_lock_assert(p, MA_NOTOWNED);
94989f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
9503157c503SKonstantin Belousov 
9513157c503SKonstantin Belousov 	count = 1;
9523157c503SKonstantin Belousov 	mreq = 0;
9533157c503SKonstantin Belousov 
9543157c503SKonstantin Belousov 	for (tp = p; count < vm_pageout_page_count; count++) {
9553157c503SKonstantin Belousov 		tp = vm_page_next(tp);
956c7aebda8SAttilio Rao 		if (tp == NULL || vm_page_busied(tp))
957bd7e5f99SJohn Dyson 			break;
9583280870dSKonstantin Belousov 		if (!vm_object_page_remove_write(tp, flags, clearobjflags))
959bd7e5f99SJohn Dyson 			break;
960bd7e5f99SJohn Dyson 	}
961aef922f5SJohn Dyson 
9623157c503SKonstantin Belousov 	for (p_first = p; count < vm_pageout_page_count; count++) {
9633157c503SKonstantin Belousov 		tp = vm_page_prev(p_first);
964c7aebda8SAttilio Rao 		if (tp == NULL || vm_page_busied(tp))
965bd7e5f99SJohn Dyson 			break;
9663280870dSKonstantin Belousov 		if (!vm_object_page_remove_write(tp, flags, clearobjflags))
967bd7e5f99SJohn Dyson 			break;
9683157c503SKonstantin Belousov 		p_first = tp;
9693157c503SKonstantin Belousov 		mreq++;
970bd7e5f99SJohn Dyson 	}
971bd7e5f99SJohn Dyson 
9723157c503SKonstantin Belousov 	for (tp = p_first, i = 0; i < count; tp = TAILQ_NEXT(tp, listq), i++)
9733157c503SKonstantin Belousov 		ma[i] = tp;
974cf2819ccSJohn Dyson 
975126d6082SKonstantin Belousov 	vm_pageout_flush(ma, count, pagerflags, mreq, &runlen, eio);
9761e8a675cSKonstantin Belousov 	return (runlen);
97726f9a767SRodney W. Grimes }
978df8bae1dSRodney W. Grimes 
9791efb74fbSJohn Dyson /*
980950f8459SAlan Cox  * Note that there is absolutely no sense in writing out
981950f8459SAlan Cox  * anonymous objects, so we track down the vnode object
982950f8459SAlan Cox  * to write out.
983950f8459SAlan Cox  * We invalidate (remove) all pages from the address space
984950f8459SAlan Cox  * for semantic correctness.
985950f8459SAlan Cox  *
9866bbee8e2SAlan Cox  * If the backing object is a device object with unmanaged pages, then any
9876bbee8e2SAlan Cox  * mappings to the specified range of pages must be removed before this
9886bbee8e2SAlan Cox  * function is called.
9896bbee8e2SAlan Cox  *
990950f8459SAlan Cox  * Note: certain anonymous maps, such as MAP_NOSYNC maps,
991950f8459SAlan Cox  * may start out with a NULL object.
992950f8459SAlan Cox  */
993126d6082SKonstantin Belousov boolean_t
994950f8459SAlan Cox vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size,
995950f8459SAlan Cox     boolean_t syncio, boolean_t invalidate)
996950f8459SAlan Cox {
997950f8459SAlan Cox 	vm_object_t backing_object;
998950f8459SAlan Cox 	struct vnode *vp;
9993b582b4eSTor Egge 	struct mount *mp;
1000126d6082SKonstantin Belousov 	int error, flags, fsync_after;
1001126d6082SKonstantin Belousov 	boolean_t res;
1002950f8459SAlan Cox 
1003950f8459SAlan Cox 	if (object == NULL)
1004126d6082SKonstantin Belousov 		return (TRUE);
1005126d6082SKonstantin Belousov 	res = TRUE;
1006126d6082SKonstantin Belousov 	error = 0;
100789f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
1008950f8459SAlan Cox 	while ((backing_object = object->backing_object) != NULL) {
100989f6b863SAttilio Rao 		VM_OBJECT_WLOCK(backing_object);
101056e0670fSAlan Cox 		offset += object->backing_object_offset;
101189f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
1012950f8459SAlan Cox 		object = backing_object;
1013950f8459SAlan Cox 		if (object->size < OFF_TO_IDX(offset + size))
1014950f8459SAlan Cox 			size = IDX_TO_OFF(object->size) - offset;
1015950f8459SAlan Cox 	}
1016950f8459SAlan Cox 	/*
1017950f8459SAlan Cox 	 * Flush pages if writing is allowed, invalidate them
1018950f8459SAlan Cox 	 * if invalidation requested.  Pages undergoing I/O
1019950f8459SAlan Cox 	 * will be ignored by vm_object_page_remove().
1020950f8459SAlan Cox 	 *
1021950f8459SAlan Cox 	 * We cannot lock the vnode and then wait for paging
1022950f8459SAlan Cox 	 * to complete without deadlocking against vm_fault.
1023950f8459SAlan Cox 	 * Instead we simply call vm_object_page_remove() and
1024950f8459SAlan Cox 	 * allow it to block internally on a page-by-page
1025950f8459SAlan Cox 	 * basis when it encounters pages undergoing async
1026950f8459SAlan Cox 	 * I/O.
1027950f8459SAlan Cox 	 */
1028950f8459SAlan Cox 	if (object->type == OBJT_VNODE &&
1029950f8459SAlan Cox 	    (object->flags & OBJ_MIGHTBEDIRTY) != 0) {
1030950f8459SAlan Cox 		vp = object->handle;
103189f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
10323b582b4eSTor Egge 		(void) vn_start_write(vp, &mp, V_WAIT);
1033cb05b60aSAttilio Rao 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
103475ff604aSKonstantin Belousov 		if (syncio && !invalidate && offset == 0 &&
103575ff604aSKonstantin Belousov 		    OFF_TO_IDX(size) == object->size) {
103675ff604aSKonstantin Belousov 			/*
103775ff604aSKonstantin Belousov 			 * If syncing the whole mapping of the file,
103875ff604aSKonstantin Belousov 			 * it is faster to schedule all the writes in
103975ff604aSKonstantin Belousov 			 * async mode, also allowing the clustering,
104075ff604aSKonstantin Belousov 			 * and then wait for i/o to complete.
104175ff604aSKonstantin Belousov 			 */
104275ff604aSKonstantin Belousov 			flags = 0;
104375ff604aSKonstantin Belousov 			fsync_after = TRUE;
104475ff604aSKonstantin Belousov 		} else {
1045950f8459SAlan Cox 			flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
104675ff604aSKonstantin Belousov 			flags |= invalidate ? (OBJPC_SYNC | OBJPC_INVAL) : 0;
104775ff604aSKonstantin Belousov 			fsync_after = FALSE;
104875ff604aSKonstantin Belousov 		}
104989f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
1050126d6082SKonstantin Belousov 		res = vm_object_page_clean(object, offset, offset + size,
1051126d6082SKonstantin Belousov 		    flags);
105289f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
105375ff604aSKonstantin Belousov 		if (fsync_after)
1054126d6082SKonstantin Belousov 			error = VOP_FSYNC(vp, MNT_WAIT, curthread);
105522db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
10563b582b4eSTor Egge 		vn_finished_write(mp);
1057126d6082SKonstantin Belousov 		if (error != 0)
1058126d6082SKonstantin Belousov 			res = FALSE;
105989f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
1060950f8459SAlan Cox 	}
1061950f8459SAlan Cox 	if ((object->type == OBJT_VNODE ||
1062950f8459SAlan Cox 	     object->type == OBJT_DEVICE) && invalidate) {
10636bbee8e2SAlan Cox 		if (object->type == OBJT_DEVICE)
10646bbee8e2SAlan Cox 			/*
10656bbee8e2SAlan Cox 			 * The option OBJPR_NOTMAPPED must be passed here
10666bbee8e2SAlan Cox 			 * because vm_object_page_remove() cannot remove
10676bbee8e2SAlan Cox 			 * unmanaged mappings.
10686bbee8e2SAlan Cox 			 */
10696bbee8e2SAlan Cox 			flags = OBJPR_NOTMAPPED;
10706bbee8e2SAlan Cox 		else if (old_msync)
10716195b24aSKonstantin Belousov 			flags = 0;
10726bbee8e2SAlan Cox 		else
10736195b24aSKonstantin Belousov 			flags = OBJPR_CLEANONLY;
10746bbee8e2SAlan Cox 		vm_object_page_remove(object, OFF_TO_IDX(offset),
10756bbee8e2SAlan Cox 		    OFF_TO_IDX(offset + size + PAGE_MASK), flags);
1076950f8459SAlan Cox 	}
107789f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
1078126d6082SKonstantin Belousov 	return (res);
1079950f8459SAlan Cox }
1080950f8459SAlan Cox 
1081950f8459SAlan Cox /*
1082867a482dSJohn Dyson  *	vm_object_madvise:
1083867a482dSJohn Dyson  *
1084867a482dSJohn Dyson  *	Implements the madvise function at the object/page level.
10851c7c3c6aSMatthew Dillon  *
1086193b9358SAlan Cox  *	MADV_WILLNEED	(any object)
1087193b9358SAlan Cox  *
1088193b9358SAlan Cox  *	    Activate the specified pages if they are resident.
1089193b9358SAlan Cox  *
1090193b9358SAlan Cox  *	MADV_DONTNEED	(any object)
1091193b9358SAlan Cox  *
1092193b9358SAlan Cox  *	    Deactivate the specified pages if they are resident.
1093193b9358SAlan Cox  *
1094193b9358SAlan Cox  *	MADV_FREE	(OBJT_DEFAULT/OBJT_SWAP objects,
1095193b9358SAlan Cox  *			 OBJ_ONEMAPPING only)
1096193b9358SAlan Cox  *
1097193b9358SAlan Cox  *	    Deactivate and clean the specified pages if they are
1098193b9358SAlan Cox  *	    resident.  This permits the process to reuse the pages
1099193b9358SAlan Cox  *	    without faulting or the kernel to reclaim the pages
1100193b9358SAlan Cox  *	    without I/O.
1101867a482dSJohn Dyson  */
1102867a482dSJohn Dyson void
110392a59946SJohn Baldwin vm_object_madvise(vm_object_t object, vm_pindex_t pindex, vm_pindex_t end,
110492a59946SJohn Baldwin     int advise)
1105867a482dSJohn Dyson {
110692a59946SJohn Baldwin 	vm_pindex_t tpindex;
110734567de7SAlan Cox 	vm_object_t backing_object, tobject;
1108867a482dSJohn Dyson 	vm_page_t m;
1109867a482dSJohn Dyson 
1110867a482dSJohn Dyson 	if (object == NULL)
1111867a482dSJohn Dyson 		return;
111289f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
11131c7c3c6aSMatthew Dillon 	/*
11141c7c3c6aSMatthew Dillon 	 * Locate and adjust resident pages
11151c7c3c6aSMatthew Dillon 	 */
11161c7c3c6aSMatthew Dillon 	for (; pindex < end; pindex += 1) {
11176e20a165SJohn Dyson relookup:
11186e20a165SJohn Dyson 		tobject = object;
11196e20a165SJohn Dyson 		tpindex = pindex;
11206e20a165SJohn Dyson shadowlookup:
112158b4e6ccSAlan Cox 		/*
112258b4e6ccSAlan Cox 		 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
112358b4e6ccSAlan Cox 		 * and those pages must be OBJ_ONEMAPPING.
112458b4e6ccSAlan Cox 		 */
112558b4e6ccSAlan Cox 		if (advise == MADV_FREE) {
112658b4e6ccSAlan Cox 			if ((tobject->type != OBJT_DEFAULT &&
112758b4e6ccSAlan Cox 			     tobject->type != OBJT_SWAP) ||
112858b4e6ccSAlan Cox 			    (tobject->flags & OBJ_ONEMAPPING) == 0) {
112934567de7SAlan Cox 				goto unlock_tobject;
11306e20a165SJohn Dyson 			}
113128634820SAlan Cox 		} else if ((tobject->flags & OBJ_UNMANAGED) != 0)
11326a2a3d73SAlan Cox 			goto unlock_tobject;
11331c7c3c6aSMatthew Dillon 		m = vm_page_lookup(tobject, tpindex);
11347bfda801SAlan Cox 		if (m == NULL && advise == MADV_WILLNEED) {
11357bfda801SAlan Cox 			/*
11367bfda801SAlan Cox 			 * If the page is cached, reactivate it.
11377bfda801SAlan Cox 			 */
1138f3a2ed4bSAlan Cox 			m = vm_page_alloc(tobject, tpindex, VM_ALLOC_IFCACHED |
1139f3a2ed4bSAlan Cox 			    VM_ALLOC_NOBUSY);
11407bfda801SAlan Cox 		}
11411c7c3c6aSMatthew Dillon 		if (m == NULL) {
11421ce137beSMatthew Dillon 			/*
11431ce137beSMatthew Dillon 			 * There may be swap even if there is no backing page
11441ce137beSMatthew Dillon 			 */
11451ce137beSMatthew Dillon 			if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
11461ce137beSMatthew Dillon 				swap_pager_freespace(tobject, tpindex, 1);
11471ce137beSMatthew Dillon 			/*
11481ce137beSMatthew Dillon 			 * next object
11491ce137beSMatthew Dillon 			 */
115034567de7SAlan Cox 			backing_object = tobject->backing_object;
115134567de7SAlan Cox 			if (backing_object == NULL)
115234567de7SAlan Cox 				goto unlock_tobject;
115389f6b863SAttilio Rao 			VM_OBJECT_WLOCK(backing_object);
115456e0670fSAlan Cox 			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
11559b98b796SAlan Cox 			if (tobject != object)
115689f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(tobject);
115734567de7SAlan Cox 			tobject = backing_object;
11586e20a165SJohn Dyson 			goto shadowlookup;
11596a2a3d73SAlan Cox 		} else if (m->valid != VM_PAGE_BITS_ALL)
11606a2a3d73SAlan Cox 			goto unlock_tobject;
1161867a482dSJohn Dyson 		/*
11626a2a3d73SAlan Cox 		 * If the page is not in a normal state, skip it.
1163867a482dSJohn Dyson 		 */
11642965a453SKip Macy 		vm_page_lock(m);
11656a2a3d73SAlan Cox 		if (m->hold_count != 0 || m->wire_count != 0) {
11662965a453SKip Macy 			vm_page_unlock(m);
116734567de7SAlan Cox 			goto unlock_tobject;
11686e20a165SJohn Dyson 		}
1169d98d0ce2SKonstantin Belousov 		KASSERT((m->flags & PG_FICTITIOUS) == 0,
1170d98d0ce2SKonstantin Belousov 		    ("vm_object_madvise: page %p is fictitious", m));
1171d98d0ce2SKonstantin Belousov 		KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1172567e51e1SAlan Cox 		    ("vm_object_madvise: page %p is not managed", m));
1173c7aebda8SAttilio Rao 		if (vm_page_busied(m)) {
1174567e51e1SAlan Cox 			if (advise == MADV_WILLNEED) {
1175b11b56b5SAlan Cox 				/*
1176b11b56b5SAlan Cox 				 * Reference the page before unlocking and
1177b11b56b5SAlan Cox 				 * sleeping so that the page daemon is less
1178b11b56b5SAlan Cox 				 * likely to reclaim it.
1179b11b56b5SAlan Cox 				 */
11803407fefeSKonstantin Belousov 				vm_page_aflag_set(m, PGA_REFERENCED);
1181567e51e1SAlan Cox 			}
11829b98b796SAlan Cox 			if (object != tobject)
118389f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(object);
1184c7aebda8SAttilio Rao 			VM_OBJECT_WUNLOCK(tobject);
1185c7aebda8SAttilio Rao 			vm_page_busy_sleep(m, "madvpo");
118689f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
11876e20a165SJohn Dyson   			goto relookup;
118834567de7SAlan Cox 		}
1189867a482dSJohn Dyson 		if (advise == MADV_WILLNEED) {
1190867a482dSJohn Dyson 			vm_page_activate(m);
11912051980fSAlan Cox 		} else {
11922051980fSAlan Cox 			vm_page_advise(m, advise);
1193867a482dSJohn Dyson 		}
11942965a453SKip Macy 		vm_page_unlock(m);
11952999e9faSAlan Cox 		if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
11962999e9faSAlan Cox 			swap_pager_freespace(tobject, tpindex, 1);
119734567de7SAlan Cox unlock_tobject:
11989b98b796SAlan Cox 		if (tobject != object)
119989f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(tobject);
1200867a482dSJohn Dyson 	}
120189f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
1202867a482dSJohn Dyson }
1203867a482dSJohn Dyson 
1204867a482dSJohn Dyson /*
1205df8bae1dSRodney W. Grimes  *	vm_object_shadow:
1206df8bae1dSRodney W. Grimes  *
1207df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
1208df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
1209df8bae1dSRodney W. Grimes  *	object reference is deallocated.
1210df8bae1dSRodney W. Grimes  *
1211df8bae1dSRodney W. Grimes  *	The new object and offset into that object
1212df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
1213df8bae1dSRodney W. Grimes  */
121426f9a767SRodney W. Grimes void
12151b40f8c0SMatthew Dillon vm_object_shadow(
12161b40f8c0SMatthew Dillon 	vm_object_t *object,	/* IN/OUT */
12171b40f8c0SMatthew Dillon 	vm_ooffset_t *offset,	/* IN/OUT */
12181b40f8c0SMatthew Dillon 	vm_size_t length)
1219df8bae1dSRodney W. Grimes {
1220d031cff1SMatthew Dillon 	vm_object_t source;
1221d031cff1SMatthew Dillon 	vm_object_t result;
1222df8bae1dSRodney W. Grimes 
1223df8bae1dSRodney W. Grimes 	source = *object;
1224df8bae1dSRodney W. Grimes 
1225df8bae1dSRodney W. Grimes 	/*
12269a2f6362SAlan Cox 	 * Don't create the new object if the old object isn't shared.
12279a2f6362SAlan Cox 	 */
1228570a2f4aSAlan Cox 	if (source != NULL) {
122989f6b863SAttilio Rao 		VM_OBJECT_WLOCK(source);
1230570a2f4aSAlan Cox 		if (source->ref_count == 1 &&
12319a2f6362SAlan Cox 		    source->handle == NULL &&
12329a2f6362SAlan Cox 		    (source->type == OBJT_DEFAULT ||
12339917e010SAlan Cox 		     source->type == OBJT_SWAP)) {
123489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(source);
12359a2f6362SAlan Cox 			return;
12369917e010SAlan Cox 		}
123789f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(source);
1238570a2f4aSAlan Cox 	}
12399a2f6362SAlan Cox 
12409a2f6362SAlan Cox 	/*
1241570a2f4aSAlan Cox 	 * Allocate a new object with the given length.
1242df8bae1dSRodney W. Grimes 	 */
12430cc74f14SAlan Cox 	result = vm_object_allocate(OBJT_DEFAULT, atop(length));
1244df8bae1dSRodney W. Grimes 
1245df8bae1dSRodney W. Grimes 	/*
12460d94caffSDavid Greenman 	 * The new object shadows the source object, adding a reference to it.
12470d94caffSDavid Greenman 	 * Our caller changes his reference to point to the new object,
12480d94caffSDavid Greenman 	 * removing a reference to the source object.  Net result: no change
12490d94caffSDavid Greenman 	 * of reference count.
12509b09fe24SMatthew Dillon 	 *
12519b09fe24SMatthew Dillon 	 * Try to optimize the result object's page color when shadowing
1252956f3135SPhilippe Charnier 	 * in order to maintain page coloring consistency in the combined
12539b09fe24SMatthew Dillon 	 * shadowed object.
1254df8bae1dSRodney W. Grimes 	 */
125524a1cce3SDavid Greenman 	result->backing_object = source;
12569174ca7bSTor Egge 	/*
12579174ca7bSTor Egge 	 * Store the offset into the source object, and fix up the offset into
12589174ca7bSTor Egge 	 * the new object.
12599174ca7bSTor Egge 	 */
12609174ca7bSTor Egge 	result->backing_object_offset = *offset;
1261570a2f4aSAlan Cox 	if (source != NULL) {
126289f6b863SAttilio Rao 		VM_OBJECT_WLOCK(source);
12631c500307SAlan Cox 		LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1264eaf13dd7SJohn Dyson 		source->shadow_count++;
1265f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
12667b54b1a9SAlan Cox 		result->flags |= source->flags & OBJ_COLORED;
1267f8a47341SAlan Cox 		result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) &
1268f8a47341SAlan Cox 		    ((1 << (VM_NFREEORDER - 1)) - 1);
1269f8a47341SAlan Cox #endif
127089f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(source);
1271de5f6a77SJohn Dyson 	}
1272df8bae1dSRodney W. Grimes 
1273df8bae1dSRodney W. Grimes 
1274df8bae1dSRodney W. Grimes 	/*
1275df8bae1dSRodney W. Grimes 	 * Return the new things
1276df8bae1dSRodney W. Grimes 	 */
1277df8bae1dSRodney W. Grimes 	*offset = 0;
1278df8bae1dSRodney W. Grimes 	*object = result;
1279df8bae1dSRodney W. Grimes }
1280df8bae1dSRodney W. Grimes 
1281c5aaa06dSAlan Cox /*
1282c5aaa06dSAlan Cox  *	vm_object_split:
1283c5aaa06dSAlan Cox  *
1284c5aaa06dSAlan Cox  * Split the pages in a map entry into a new object.  This affords
1285c5aaa06dSAlan Cox  * easier removal of unused pages, and keeps object inheritance from
1286c5aaa06dSAlan Cox  * being a negative impact on memory usage.
1287c5aaa06dSAlan Cox  */
1288c5aaa06dSAlan Cox void
1289c5aaa06dSAlan Cox vm_object_split(vm_map_entry_t entry)
1290c5aaa06dSAlan Cox {
129173000556SAlan Cox 	vm_page_t m, m_next;
1292c5aaa06dSAlan Cox 	vm_object_t orig_object, new_object, source;
129373000556SAlan Cox 	vm_pindex_t idx, offidxstart;
129473000556SAlan Cox 	vm_size_t size;
1295c5aaa06dSAlan Cox 
1296c5aaa06dSAlan Cox 	orig_object = entry->object.vm_object;
1297c5aaa06dSAlan Cox 	if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1298c5aaa06dSAlan Cox 		return;
1299c5aaa06dSAlan Cox 	if (orig_object->ref_count <= 1)
1300c5aaa06dSAlan Cox 		return;
130189f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(orig_object);
1302c5aaa06dSAlan Cox 
13034da9f125SAlan Cox 	offidxstart = OFF_TO_IDX(entry->offset);
130495442adfSAlan Cox 	size = atop(entry->end - entry->start);
1305c5aaa06dSAlan Cox 
13064da9f125SAlan Cox 	/*
13074da9f125SAlan Cox 	 * If swap_pager_copy() is later called, it will convert new_object
13084da9f125SAlan Cox 	 * into a swap object.
13094da9f125SAlan Cox 	 */
13104da9f125SAlan Cox 	new_object = vm_object_allocate(OBJT_DEFAULT, size);
1311c5aaa06dSAlan Cox 
1312c5474b8fSAlan Cox 	/*
1313c5474b8fSAlan Cox 	 * At this point, the new object is still private, so the order in
1314c5474b8fSAlan Cox 	 * which the original and new objects are locked does not matter.
1315c5474b8fSAlan Cox 	 */
131689f6b863SAttilio Rao 	VM_OBJECT_WLOCK(new_object);
131789f6b863SAttilio Rao 	VM_OBJECT_WLOCK(orig_object);
1318c5aaa06dSAlan Cox 	source = orig_object->backing_object;
1319c5aaa06dSAlan Cox 	if (source != NULL) {
132089f6b863SAttilio Rao 		VM_OBJECT_WLOCK(source);
132119c244d0SAlan Cox 		if ((source->flags & OBJ_DEAD) != 0) {
132289f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(source);
132389f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(orig_object);
132489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(new_object);
132519c244d0SAlan Cox 			vm_object_deallocate(new_object);
132689f6b863SAttilio Rao 			VM_OBJECT_WLOCK(orig_object);
132719c244d0SAlan Cox 			return;
132819c244d0SAlan Cox 		}
13291c500307SAlan Cox 		LIST_INSERT_HEAD(&source->shadow_head,
1330c5aaa06dSAlan Cox 				  new_object, shadow_list);
13318e3a76fbSAlan Cox 		source->shadow_count++;
1332b921a12bSAlan Cox 		vm_object_reference_locked(source);	/* for new_object */
1333c5aaa06dSAlan Cox 		vm_object_clear_flag(source, OBJ_ONEMAPPING);
133489f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(source);
1335c5aaa06dSAlan Cox 		new_object->backing_object_offset =
13364da9f125SAlan Cox 			orig_object->backing_object_offset + entry->offset;
1337c5aaa06dSAlan Cox 		new_object->backing_object = source;
1338c5aaa06dSAlan Cox 	}
1339ef694c1aSEdward Tomasz Napierala 	if (orig_object->cred != NULL) {
1340ef694c1aSEdward Tomasz Napierala 		new_object->cred = orig_object->cred;
1341ef694c1aSEdward Tomasz Napierala 		crhold(orig_object->cred);
13423364c323SKonstantin Belousov 		new_object->charge = ptoa(size);
13433364c323SKonstantin Belousov 		KASSERT(orig_object->charge >= ptoa(size),
13443364c323SKonstantin Belousov 		    ("orig_object->charge < 0"));
13453364c323SKonstantin Belousov 		orig_object->charge -= ptoa(size);
13463364c323SKonstantin Belousov 	}
1347c5aaa06dSAlan Cox retry:
1348b382c10aSKonstantin Belousov 	m = vm_page_find_least(orig_object, offidxstart);
134973000556SAlan Cox 	for (; m != NULL && (idx = m->pindex - offidxstart) < size;
135073000556SAlan Cox 	    m = m_next) {
135173000556SAlan Cox 		m_next = TAILQ_NEXT(m, listq);
1352c5aaa06dSAlan Cox 
1353c5aaa06dSAlan Cox 		/*
1354c5aaa06dSAlan Cox 		 * We must wait for pending I/O to complete before we can
1355c5aaa06dSAlan Cox 		 * rename the page.
1356c5aaa06dSAlan Cox 		 *
1357c5aaa06dSAlan Cox 		 * We do not have to VM_PROT_NONE the page as mappings should
1358c5aaa06dSAlan Cox 		 * not be changed by this operation.
1359c5aaa06dSAlan Cox 		 */
1360c7aebda8SAttilio Rao 		if (vm_page_busied(m)) {
136189f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(new_object);
1362c7aebda8SAttilio Rao 			vm_page_lock(m);
1363c7aebda8SAttilio Rao 			VM_OBJECT_WUNLOCK(orig_object);
1364c7aebda8SAttilio Rao 			vm_page_busy_sleep(m, "spltwt");
1365c7aebda8SAttilio Rao 			VM_OBJECT_WLOCK(orig_object);
136689f6b863SAttilio Rao 			VM_OBJECT_WLOCK(new_object);
1367c5aaa06dSAlan Cox 			goto retry;
1368de33beddSAlan Cox 		}
1369e946b949SAttilio Rao 
1370e946b949SAttilio Rao 		/* vm_page_rename() will handle dirty and cache. */
1371e946b949SAttilio Rao 		if (vm_page_rename(m, new_object, idx)) {
1372e946b949SAttilio Rao 			VM_OBJECT_WUNLOCK(new_object);
1373e946b949SAttilio Rao 			VM_OBJECT_WUNLOCK(orig_object);
1374e946b949SAttilio Rao 			VM_WAIT;
1375e946b949SAttilio Rao 			VM_OBJECT_WLOCK(orig_object);
1376e946b949SAttilio Rao 			VM_OBJECT_WLOCK(new_object);
1377e946b949SAttilio Rao 			goto retry;
1378e946b949SAttilio Rao 		}
1379b5f359b7SAlan Cox #if VM_NRESERVLEVEL > 0
1380b5f359b7SAlan Cox 		/*
1381b5f359b7SAlan Cox 		 * If some of the reservation's allocated pages remain with
1382b5f359b7SAlan Cox 		 * the original object, then transferring the reservation to
1383b5f359b7SAlan Cox 		 * the new object is neither particularly beneficial nor
1384b5f359b7SAlan Cox 		 * particularly harmful as compared to leaving the reservation
1385b5f359b7SAlan Cox 		 * with the original object.  If, however, all of the
1386b5f359b7SAlan Cox 		 * reservation's allocated pages are transferred to the new
1387b5f359b7SAlan Cox 		 * object, then transferring the reservation is typically
1388b5f359b7SAlan Cox 		 * beneficial.  Determining which of these two cases applies
1389b5f359b7SAlan Cox 		 * would be more costly than unconditionally renaming the
1390b5f359b7SAlan Cox 		 * reservation.
1391b5f359b7SAlan Cox 		 */
1392b5f359b7SAlan Cox 		vm_reserv_rename(m, new_object, orig_object, offidxstart);
1393b5f359b7SAlan Cox #endif
1394dfd55c0cSAttilio Rao 		if (orig_object->type == OBJT_SWAP)
1395c7aebda8SAttilio Rao 			vm_page_xbusy(m);
1396c5aaa06dSAlan Cox 	}
1397d7a013c3SAlan Cox 	if (orig_object->type == OBJT_SWAP) {
1398c5aaa06dSAlan Cox 		/*
1399c7c8dd7eSAlan Cox 		 * swap_pager_copy() can sleep, in which case the orig_object's
1400c7c8dd7eSAlan Cox 		 * and new_object's locks are released and reacquired.
1401c5aaa06dSAlan Cox 		 */
1402c5aaa06dSAlan Cox 		swap_pager_copy(orig_object, new_object, offidxstart, 0);
1403dfd55c0cSAttilio Rao 		TAILQ_FOREACH(m, &new_object->memq, listq)
1404c7aebda8SAttilio Rao 			vm_page_xunbusy(m);
14057bfda801SAlan Cox 
14067bfda801SAlan Cox 		/*
14077bfda801SAlan Cox 		 * Transfer any cached pages from orig_object to new_object.
1408571a1e92SAttilio Rao 		 * If swap_pager_copy() found swapped out pages within the
1409571a1e92SAttilio Rao 		 * specified range of orig_object, then it changed
1410571a1e92SAttilio Rao 		 * new_object's type to OBJT_SWAP when it transferred those
1411571a1e92SAttilio Rao 		 * pages to new_object.  Otherwise, new_object's type
1412571a1e92SAttilio Rao 		 * should still be OBJT_DEFAULT and orig_object should not
1413571a1e92SAttilio Rao 		 * contain any cached pages within the specified range.
14147bfda801SAlan Cox 		 */
1415c9341161SAttilio Rao 		if (__predict_false(!vm_object_cache_is_empty(orig_object)))
14167bfda801SAlan Cox 			vm_page_cache_transfer(orig_object, offidxstart,
14177bfda801SAlan Cox 			    new_object);
1418c5aaa06dSAlan Cox 	}
141989f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(orig_object);
142089f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(new_object);
1421c5aaa06dSAlan Cox 	entry->object.vm_object = new_object;
1422c5aaa06dSAlan Cox 	entry->offset = 0LL;
1423c5aaa06dSAlan Cox 	vm_object_deallocate(orig_object);
142489f6b863SAttilio Rao 	VM_OBJECT_WLOCK(new_object);
1425c5aaa06dSAlan Cox }
1426c5aaa06dSAlan Cox 
14272ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_NOWAIT	0x0002
14282ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_WAIT	0x0004
14292ad1a3f7SMatthew Dillon 
143099a1570aSKonstantin Belousov static vm_page_t
14314cc8daf7SConrad Meyer vm_object_collapse_scan_wait(vm_object_t object, vm_page_t p, vm_page_t next,
143299a1570aSKonstantin Belousov     int op)
143399a1570aSKonstantin Belousov {
143499a1570aSKonstantin Belousov 	vm_object_t backing_object;
143599a1570aSKonstantin Belousov 
143699a1570aSKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED(object);
143799a1570aSKonstantin Belousov 	backing_object = object->backing_object;
143899a1570aSKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED(backing_object);
143999a1570aSKonstantin Belousov 
144099a1570aSKonstantin Belousov 	KASSERT(p == NULL || vm_page_busied(p), ("unbusy page %p", p));
144199a1570aSKonstantin Belousov 	KASSERT(p == NULL || p->object == object || p->object == backing_object,
144299a1570aSKonstantin Belousov 	    ("invalid ownership %p %p %p", p, object, backing_object));
144399a1570aSKonstantin Belousov 	if ((op & OBSC_COLLAPSE_NOWAIT) != 0)
144499a1570aSKonstantin Belousov 		return (next);
144599a1570aSKonstantin Belousov 	if (p != NULL)
144699a1570aSKonstantin Belousov 		vm_page_lock(p);
144799a1570aSKonstantin Belousov 	VM_OBJECT_WUNLOCK(object);
144899a1570aSKonstantin Belousov 	VM_OBJECT_WUNLOCK(backing_object);
144999a1570aSKonstantin Belousov 	if (p == NULL)
145099a1570aSKonstantin Belousov 		VM_WAIT;
145199a1570aSKonstantin Belousov 	else
145299a1570aSKonstantin Belousov 		vm_page_busy_sleep(p, "vmocol");
145399a1570aSKonstantin Belousov 	VM_OBJECT_WLOCK(object);
145499a1570aSKonstantin Belousov 	VM_OBJECT_WLOCK(backing_object);
145599a1570aSKonstantin Belousov 	return (TAILQ_FIRST(&backing_object->memq));
145699a1570aSKonstantin Belousov }
145799a1570aSKonstantin Belousov 
145899a1570aSKonstantin Belousov static bool
14594cc8daf7SConrad Meyer vm_object_scan_all_shadowed(vm_object_t object)
14604cc8daf7SConrad Meyer {
14614cc8daf7SConrad Meyer 	vm_object_t backing_object;
14624cc8daf7SConrad Meyer 	vm_page_t p, pp;
14634cc8daf7SConrad Meyer 	vm_pindex_t backing_offset_index, new_pindex;
14644cc8daf7SConrad Meyer 
14654cc8daf7SConrad Meyer 	VM_OBJECT_ASSERT_WLOCKED(object);
14664cc8daf7SConrad Meyer 	VM_OBJECT_ASSERT_WLOCKED(object->backing_object);
14674cc8daf7SConrad Meyer 
14684cc8daf7SConrad Meyer 	backing_object = object->backing_object;
14694cc8daf7SConrad Meyer 
14704cc8daf7SConrad Meyer 	/*
14714cc8daf7SConrad Meyer 	 * Initial conditions:
14724cc8daf7SConrad Meyer 	 *
14734cc8daf7SConrad Meyer 	 * We do not want to have to test for the existence of cache or swap
14744cc8daf7SConrad Meyer 	 * pages in the backing object.  XXX but with the new swapper this
14754cc8daf7SConrad Meyer 	 * would be pretty easy to do.
14764cc8daf7SConrad Meyer 	 */
14774cc8daf7SConrad Meyer 	if (backing_object->type != OBJT_DEFAULT)
14784cc8daf7SConrad Meyer 		return (false);
14794cc8daf7SConrad Meyer 
14804cc8daf7SConrad Meyer 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
14814cc8daf7SConrad Meyer 
14824cc8daf7SConrad Meyer 	for (p = TAILQ_FIRST(&backing_object->memq); p != NULL;
14834cc8daf7SConrad Meyer 	    p = TAILQ_NEXT(p, listq)) {
14844cc8daf7SConrad Meyer 		new_pindex = p->pindex - backing_offset_index;
14854cc8daf7SConrad Meyer 
14864cc8daf7SConrad Meyer 		/*
14874cc8daf7SConrad Meyer 		 * Ignore pages outside the parent object's range and outside
14884cc8daf7SConrad Meyer 		 * the parent object's mapping of the backing object.
14894cc8daf7SConrad Meyer 		 */
14904cc8daf7SConrad Meyer 		if (p->pindex < backing_offset_index ||
14914cc8daf7SConrad Meyer 		    new_pindex >= object->size)
14924cc8daf7SConrad Meyer 			continue;
14934cc8daf7SConrad Meyer 
14944cc8daf7SConrad Meyer 		/*
14954cc8daf7SConrad Meyer 		 * See if the parent has the page or if the parent's object
14964cc8daf7SConrad Meyer 		 * pager has the page.  If the parent has the page but the page
14974cc8daf7SConrad Meyer 		 * is not valid, the parent's object pager must have the page.
14984cc8daf7SConrad Meyer 		 *
14994cc8daf7SConrad Meyer 		 * If this fails, the parent does not completely shadow the
15004cc8daf7SConrad Meyer 		 * object and we might as well give up now.
15014cc8daf7SConrad Meyer 		 */
15024cc8daf7SConrad Meyer 		pp = vm_page_lookup(object, new_pindex);
15034cc8daf7SConrad Meyer 		if ((pp == NULL || pp->valid == 0) &&
15044cc8daf7SConrad Meyer 		    !vm_pager_has_page(object, new_pindex, NULL, NULL))
15054cc8daf7SConrad Meyer 			return (false);
15064cc8daf7SConrad Meyer 	}
15074cc8daf7SConrad Meyer 	return (true);
15084cc8daf7SConrad Meyer }
15094cc8daf7SConrad Meyer 
15104cc8daf7SConrad Meyer static bool
15114cc8daf7SConrad Meyer vm_object_collapse_scan(vm_object_t object, int op)
15122ad1a3f7SMatthew Dillon {
15132ad1a3f7SMatthew Dillon 	vm_object_t backing_object;
151499a1570aSKonstantin Belousov 	vm_page_t next, p, pp;
151599a1570aSKonstantin Belousov 	vm_pindex_t backing_offset_index, new_pindex;
15162ad1a3f7SMatthew Dillon 
151789f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
151889f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object->backing_object);
15192ad1a3f7SMatthew Dillon 
15202ad1a3f7SMatthew Dillon 	backing_object = object->backing_object;
15212ad1a3f7SMatthew Dillon 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
15222ad1a3f7SMatthew Dillon 
15232ad1a3f7SMatthew Dillon 	/*
15242ad1a3f7SMatthew Dillon 	 * Initial conditions
15252ad1a3f7SMatthew Dillon 	 */
15264cc8daf7SConrad Meyer 	if ((op & OBSC_COLLAPSE_WAIT) != 0)
15272ad1a3f7SMatthew Dillon 		vm_object_set_flag(backing_object, OBJ_DEAD);
15282ad1a3f7SMatthew Dillon 
15292ad1a3f7SMatthew Dillon 	/*
15302ad1a3f7SMatthew Dillon 	 * Our scan
15312ad1a3f7SMatthew Dillon 	 */
15324cc8daf7SConrad Meyer 	for (p = TAILQ_FIRST(&backing_object->memq); p != NULL; p = next) {
153399a1570aSKonstantin Belousov 		next = TAILQ_NEXT(p, listq);
153499a1570aSKonstantin Belousov 		new_pindex = p->pindex - backing_offset_index;
15352ad1a3f7SMatthew Dillon 
15362ad1a3f7SMatthew Dillon 		/*
15372ad1a3f7SMatthew Dillon 		 * Check for busy page
15382ad1a3f7SMatthew Dillon 		 */
1539c7aebda8SAttilio Rao 		if (vm_page_busied(p)) {
15404cc8daf7SConrad Meyer 			next = vm_object_collapse_scan_wait(object, p, next, op);
15412ad1a3f7SMatthew Dillon 			continue;
15422ad1a3f7SMatthew Dillon 		}
15432ad1a3f7SMatthew Dillon 
154499a1570aSKonstantin Belousov 		KASSERT(p->object == backing_object,
15454cc8daf7SConrad Meyer 		    ("vm_object_collapse_scan: object mismatch"));
15462ad1a3f7SMatthew Dillon 
154799a1570aSKonstantin Belousov 		if (p->pindex < backing_offset_index ||
154899a1570aSKonstantin Belousov 		    new_pindex >= object->size) {
1549e946b949SAttilio Rao 			if (backing_object->type == OBJT_SWAP)
15504cc8daf7SConrad Meyer 				swap_pager_freespace(backing_object, p->pindex,
15514cc8daf7SConrad Meyer 				    1);
1552e946b949SAttilio Rao 
15532ad1a3f7SMatthew Dillon 			/*
15544cc8daf7SConrad Meyer 			 * Page is out of the parent object's range, we can
15554cc8daf7SConrad Meyer 			 * simply destroy it.
15562ad1a3f7SMatthew Dillon 			 */
15572965a453SKip Macy 			vm_page_lock(p);
1558f6d89838SAlan Cox 			KASSERT(!pmap_page_is_mapped(p),
1559f6d89838SAlan Cox 			    ("freeing mapped page %p", p));
1560f6d89838SAlan Cox 			if (p->wire_count == 0)
15612ad1a3f7SMatthew Dillon 				vm_page_free(p);
1562f6d89838SAlan Cox 			else
1563f6d89838SAlan Cox 				vm_page_remove(p);
15642965a453SKip Macy 			vm_page_unlock(p);
15652ad1a3f7SMatthew Dillon 			continue;
15662ad1a3f7SMatthew Dillon 		}
15672ad1a3f7SMatthew Dillon 
15682ad1a3f7SMatthew Dillon 		pp = vm_page_lookup(object, new_pindex);
156999a1570aSKonstantin Belousov 		if (pp != NULL && vm_page_busied(pp)) {
1570e18cc7bfSMax Laier 			/*
15714cc8daf7SConrad Meyer 			 * The page in the parent is busy and possibly not
15724cc8daf7SConrad Meyer 			 * (yet) valid.  Until its state is finalized by the
15734cc8daf7SConrad Meyer 			 * busy bit owner, we can't tell whether it shadows the
15744cc8daf7SConrad Meyer 			 * original page.  Therefore, we must either skip it
15754cc8daf7SConrad Meyer 			 * and the original (backing_object) page or wait for
15764cc8daf7SConrad Meyer 			 * its state to be finalized.
1577e18cc7bfSMax Laier 			 *
15784cc8daf7SConrad Meyer 			 * This is due to a race with vm_fault() where we must
15794cc8daf7SConrad Meyer 			 * unbusy the original (backing_obj) page before we can
15804cc8daf7SConrad Meyer 			 * (re)lock the parent.  Hence we can get here.
1581e18cc7bfSMax Laier 			 */
15824cc8daf7SConrad Meyer 			next = vm_object_collapse_scan_wait(object, pp, next,
15834cc8daf7SConrad Meyer 			    op);
1584e18cc7bfSMax Laier 			continue;
1585e18cc7bfSMax Laier 		}
158699a1570aSKonstantin Belousov 
158799a1570aSKonstantin Belousov 		KASSERT(pp == NULL || pp->valid != 0,
158899a1570aSKonstantin Belousov 		    ("unbusy invalid page %p", pp));
158999a1570aSKonstantin Belousov 
15904cc8daf7SConrad Meyer 		if (pp != NULL || vm_pager_has_page(object, new_pindex, NULL,
15914cc8daf7SConrad Meyer 			NULL)) {
159299a1570aSKonstantin Belousov 			/*
15934cc8daf7SConrad Meyer 			 * The page already exists in the parent OR swap exists
15944cc8daf7SConrad Meyer 			 * for this location in the parent.  Leave the parent's
15954cc8daf7SConrad Meyer 			 * page alone.  Destroy the original page from the
15964cc8daf7SConrad Meyer 			 * backing object.
159799a1570aSKonstantin Belousov 			 */
1598e946b949SAttilio Rao 			if (backing_object->type == OBJT_SWAP)
15994cc8daf7SConrad Meyer 				swap_pager_freespace(backing_object, p->pindex,
16004cc8daf7SConrad Meyer 				    1);
16012965a453SKip Macy 			vm_page_lock(p);
1602f6d89838SAlan Cox 			KASSERT(!pmap_page_is_mapped(p),
1603f6d89838SAlan Cox 			    ("freeing mapped page %p", p));
1604f6d89838SAlan Cox 			if (p->wire_count == 0)
16052ad1a3f7SMatthew Dillon 				vm_page_free(p);
1606f6d89838SAlan Cox 			else
1607f6d89838SAlan Cox 				vm_page_remove(p);
16082965a453SKip Macy 			vm_page_unlock(p);
16092ad1a3f7SMatthew Dillon 			continue;
16102ad1a3f7SMatthew Dillon 		}
16112ad1a3f7SMatthew Dillon 
1612e946b949SAttilio Rao 		/*
16134cc8daf7SConrad Meyer 		 * Page does not exist in parent, rename the page from the
16144cc8daf7SConrad Meyer 		 * backing object to the main object.
1615e946b949SAttilio Rao 		 *
16164cc8daf7SConrad Meyer 		 * If the page was mapped to a process, it can remain mapped
16174cc8daf7SConrad Meyer 		 * through the rename.  vm_page_rename() will handle dirty and
16184cc8daf7SConrad Meyer 		 * cache.
1619e946b949SAttilio Rao 		 */
1620e946b949SAttilio Rao 		if (vm_page_rename(p, object, new_pindex)) {
16214cc8daf7SConrad Meyer 			next = vm_object_collapse_scan_wait(object, NULL, next,
16224cc8daf7SConrad Meyer 			    op);
1623e946b949SAttilio Rao 			continue;
1624e946b949SAttilio Rao 		}
162514a5dc17SAttilio Rao 
162614a5dc17SAttilio Rao 		/* Use the old pindex to free the right page. */
1627e946b949SAttilio Rao 		if (backing_object->type == OBJT_SWAP)
162814a5dc17SAttilio Rao 			swap_pager_freespace(backing_object,
162914a5dc17SAttilio Rao 			    new_pindex + backing_offset_index, 1);
1630e946b949SAttilio Rao 
1631f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
1632f8a47341SAlan Cox 		/*
1633f8a47341SAlan Cox 		 * Rename the reservation.
1634f8a47341SAlan Cox 		 */
1635f8a47341SAlan Cox 		vm_reserv_rename(p, object, backing_object,
1636f8a47341SAlan Cox 		    backing_offset_index);
1637f8a47341SAlan Cox #endif
16382ad1a3f7SMatthew Dillon 	}
163999a1570aSKonstantin Belousov 	return (true);
16402ad1a3f7SMatthew Dillon }
16412ad1a3f7SMatthew Dillon 
1642df8bae1dSRodney W. Grimes 
1643df8bae1dSRodney W. Grimes /*
16442fe6e4d7SDavid Greenman  * this version of collapse allows the operation to occur earlier and
16452fe6e4d7SDavid Greenman  * when paging_in_progress is true for an object...  This is not a complete
16462fe6e4d7SDavid Greenman  * operation, but should plug 99.9% of the rest of the leaks.
16472fe6e4d7SDavid Greenman  */
16482fe6e4d7SDavid Greenman static void
16491b40f8c0SMatthew Dillon vm_object_qcollapse(vm_object_t object)
16502fe6e4d7SDavid Greenman {
16512ad1a3f7SMatthew Dillon 	vm_object_t backing_object = object->backing_object;
16522fe6e4d7SDavid Greenman 
165389f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
165489f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(backing_object);
16551b40f8c0SMatthew Dillon 
16562fe6e4d7SDavid Greenman 	if (backing_object->ref_count != 1)
16572fe6e4d7SDavid Greenman 		return;
16582fe6e4d7SDavid Greenman 
16594cc8daf7SConrad Meyer 	vm_object_collapse_scan(object, OBSC_COLLAPSE_NOWAIT);
16602fe6e4d7SDavid Greenman }
16612fe6e4d7SDavid Greenman 
1662df8bae1dSRodney W. Grimes /*
1663df8bae1dSRodney W. Grimes  *	vm_object_collapse:
1664df8bae1dSRodney W. Grimes  *
1665df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
1666df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
1667df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
1668df8bae1dSRodney W. Grimes  */
166926f9a767SRodney W. Grimes void
16701b40f8c0SMatthew Dillon vm_object_collapse(vm_object_t object)
1671df8bae1dSRodney W. Grimes {
167289f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
167323955314SAlfred Perlstein 
1674df8bae1dSRodney W. Grimes 	while (TRUE) {
16752ad1a3f7SMatthew Dillon 		vm_object_t backing_object;
16762ad1a3f7SMatthew Dillon 
1677df8bae1dSRodney W. Grimes 		/*
1678df8bae1dSRodney W. Grimes 		 * Verify that the conditions are right for collapse:
1679df8bae1dSRodney W. Grimes 		 *
16802ad1a3f7SMatthew Dillon 		 * The object exists and the backing object exists.
1681df8bae1dSRodney W. Grimes 		 */
168224a1cce3SDavid Greenman 		if ((backing_object = object->backing_object) == NULL)
16832ad1a3f7SMatthew Dillon 			break;
1684df8bae1dSRodney W. Grimes 
1685f919ebdeSDavid Greenman 		/*
1686f919ebdeSDavid Greenman 		 * we check the backing object first, because it is most likely
168724a1cce3SDavid Greenman 		 * not collapsable.
1688f919ebdeSDavid Greenman 		 */
168989f6b863SAttilio Rao 		VM_OBJECT_WLOCK(backing_object);
169024a1cce3SDavid Greenman 		if (backing_object->handle != NULL ||
169124a1cce3SDavid Greenman 		    (backing_object->type != OBJT_DEFAULT &&
169224a1cce3SDavid Greenman 		     backing_object->type != OBJT_SWAP) ||
1693f919ebdeSDavid Greenman 		    (backing_object->flags & OBJ_DEAD) ||
169424a1cce3SDavid Greenman 		    object->handle != NULL ||
169524a1cce3SDavid Greenman 		    (object->type != OBJT_DEFAULT &&
169624a1cce3SDavid Greenman 		     object->type != OBJT_SWAP) ||
169724a1cce3SDavid Greenman 		    (object->flags & OBJ_DEAD)) {
169889f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(backing_object);
16992ad1a3f7SMatthew Dillon 			break;
170024a1cce3SDavid Greenman 		}
17019b4814bbSDavid Greenman 
17022ad1a3f7SMatthew Dillon 		if (
17032ad1a3f7SMatthew Dillon 		    object->paging_in_progress != 0 ||
17042ad1a3f7SMatthew Dillon 		    backing_object->paging_in_progress != 0
17052ad1a3f7SMatthew Dillon 		) {
1706b9921222SDavid Greenman 			vm_object_qcollapse(object);
170789f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(backing_object);
17082ad1a3f7SMatthew Dillon 			break;
1709df8bae1dSRodney W. Grimes 		}
171026f9a767SRodney W. Grimes 		/*
17110d94caffSDavid Greenman 		 * We know that we can either collapse the backing object (if
17122ad1a3f7SMatthew Dillon 		 * the parent is the only reference to it) or (perhaps) have
17132ad1a3f7SMatthew Dillon 		 * the parent bypass the object if the parent happens to shadow
17142ad1a3f7SMatthew Dillon 		 * all the resident pages in the entire backing object.
17152ad1a3f7SMatthew Dillon 		 *
17162ad1a3f7SMatthew Dillon 		 * This is ignoring pager-backed pages such as swap pages.
17174cc8daf7SConrad Meyer 		 * vm_object_collapse_scan fails the shadowing test in this
17182ad1a3f7SMatthew Dillon 		 * case.
1719df8bae1dSRodney W. Grimes 		 */
1720df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
1721df8bae1dSRodney W. Grimes 			/*
17222ad1a3f7SMatthew Dillon 			 * If there is exactly one reference to the backing
17232ad1a3f7SMatthew Dillon 			 * object, we can collapse it into the parent.
1724df8bae1dSRodney W. Grimes 			 */
17254cc8daf7SConrad Meyer 			vm_object_collapse_scan(object, OBSC_COLLAPSE_WAIT);
1726df8bae1dSRodney W. Grimes 
1727f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
1728f8a47341SAlan Cox 			/*
1729f8a47341SAlan Cox 			 * Break any reservations from backing_object.
1730f8a47341SAlan Cox 			 */
1731f8a47341SAlan Cox 			if (__predict_false(!LIST_EMPTY(&backing_object->rvq)))
1732f8a47341SAlan Cox 				vm_reserv_break_all(backing_object);
1733f8a47341SAlan Cox #endif
1734f8a47341SAlan Cox 
1735df8bae1dSRodney W. Grimes 			/*
1736df8bae1dSRodney W. Grimes 			 * Move the pager from backing_object to object.
1737df8bae1dSRodney W. Grimes 			 */
17386be36525SAlan Cox 			if (backing_object->type == OBJT_SWAP) {
173924a1cce3SDavid Greenman 				/*
1740c7c8dd7eSAlan Cox 				 * swap_pager_copy() can sleep, in which case
1741c7c8dd7eSAlan Cox 				 * the backing_object's and object's locks are
1742c7c8dd7eSAlan Cox 				 * released and reacquired.
1743571a1e92SAttilio Rao 				 * Since swap_pager_copy() is being asked to
1744571a1e92SAttilio Rao 				 * destroy the source, it will change the
1745571a1e92SAttilio Rao 				 * backing_object's type to OBJT_DEFAULT.
174624a1cce3SDavid Greenman 				 */
17471c7c3c6aSMatthew Dillon 				swap_pager_copy(
17481c7c3c6aSMatthew Dillon 				    backing_object,
17491c7c3c6aSMatthew Dillon 				    object,
17501c7c3c6aSMatthew Dillon 				    OFF_TO_IDX(object->backing_object_offset), TRUE);
17517bfda801SAlan Cox 
17527bfda801SAlan Cox 				/*
17537bfda801SAlan Cox 				 * Free any cached pages from backing_object.
17547bfda801SAlan Cox 				 */
1755c9341161SAttilio Rao 				if (__predict_false(
1756c9341161SAttilio Rao 				    !vm_object_cache_is_empty(backing_object)))
1757c9444914SAlan Cox 					vm_page_cache_free(backing_object, 0, 0);
1758c0503609SDavid Greenman 			}
1759df8bae1dSRodney W. Grimes 			/*
1760df8bae1dSRodney W. Grimes 			 * Object now shadows whatever backing_object did.
17612ad1a3f7SMatthew Dillon 			 * Note that the reference to
17622ad1a3f7SMatthew Dillon 			 * backing_object->backing_object moves from within
17632ad1a3f7SMatthew Dillon 			 * backing_object to within object.
1764df8bae1dSRodney W. Grimes 			 */
17651c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
17664f7c7f6eSAlan Cox 			backing_object->shadow_count--;
1767de5f6a77SJohn Dyson 			if (backing_object->backing_object) {
176889f6b863SAttilio Rao 				VM_OBJECT_WLOCK(backing_object->backing_object);
17691c500307SAlan Cox 				LIST_REMOVE(backing_object, shadow_list);
177043186e53SAlan Cox 				LIST_INSERT_HEAD(
177143186e53SAlan Cox 				    &backing_object->backing_object->shadow_head,
177243186e53SAlan Cox 				    object, shadow_list);
177343186e53SAlan Cox 				/*
177443186e53SAlan Cox 				 * The shadow_count has not changed.
177543186e53SAlan Cox 				 */
177689f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(backing_object->backing_object);
1777de5f6a77SJohn Dyson 			}
177824a1cce3SDavid Greenman 			object->backing_object = backing_object->backing_object;
17792ad1a3f7SMatthew Dillon 			object->backing_object_offset +=
17802ad1a3f7SMatthew Dillon 			    backing_object->backing_object_offset;
17812ad1a3f7SMatthew Dillon 
1782df8bae1dSRodney W. Grimes 			/*
1783df8bae1dSRodney W. Grimes 			 * Discard backing_object.
1784df8bae1dSRodney W. Grimes 			 *
17850d94caffSDavid Greenman 			 * Since the backing object has no pages, no pager left,
17860d94caffSDavid Greenman 			 * and no object references within it, all that is
17870d94caffSDavid Greenman 			 * necessary is to dispose of it.
1788df8bae1dSRodney W. Grimes 			 */
17899b4d473aSKonstantin Belousov 			KASSERT(backing_object->ref_count == 1, (
17909b4d473aSKonstantin Belousov "backing_object %p was somehow re-referenced during collapse!",
17919b4d473aSKonstantin Belousov 			    backing_object));
1792e735691bSJohn Baldwin 			backing_object->type = OBJT_DEAD;
1793e735691bSJohn Baldwin 			backing_object->ref_count = 0;
179489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(backing_object);
17959b4d473aSKonstantin Belousov 			vm_object_destroy(backing_object);
1796df8bae1dSRodney W. Grimes 
1797df8bae1dSRodney W. Grimes 			object_collapses++;
17980d94caffSDavid Greenman 		} else {
179995e5e988SJohn Dyson 			vm_object_t new_backing_object;
1800df8bae1dSRodney W. Grimes 
1801df8bae1dSRodney W. Grimes 			/*
18022ad1a3f7SMatthew Dillon 			 * If we do not entirely shadow the backing object,
18032ad1a3f7SMatthew Dillon 			 * there is nothing we can do so we give up.
1804df8bae1dSRodney W. Grimes 			 */
1805df59a0feSJeff Roberson 			if (object->resident_page_count != object->size &&
18064cc8daf7SConrad Meyer 			    !vm_object_scan_all_shadowed(object)) {
180789f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(backing_object);
18082ad1a3f7SMatthew Dillon 				break;
180924a1cce3SDavid Greenman 			}
1810df8bae1dSRodney W. Grimes 
1811df8bae1dSRodney W. Grimes 			/*
18120d94caffSDavid Greenman 			 * Make the parent shadow the next object in the
18130d94caffSDavid Greenman 			 * chain.  Deallocating backing_object will not remove
18140d94caffSDavid Greenman 			 * it, since its reference count is at least 2.
1815df8bae1dSRodney W. Grimes 			 */
18161c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
1817eaf13dd7SJohn Dyson 			backing_object->shadow_count--;
181895e5e988SJohn Dyson 
181995e5e988SJohn Dyson 			new_backing_object = backing_object->backing_object;
18208aef1712SMatthew Dillon 			if ((object->backing_object = new_backing_object) != NULL) {
182189f6b863SAttilio Rao 				VM_OBJECT_WLOCK(new_backing_object);
18221c500307SAlan Cox 				LIST_INSERT_HEAD(
18232ad1a3f7SMatthew Dillon 				    &new_backing_object->shadow_head,
18242ad1a3f7SMatthew Dillon 				    object,
18252ad1a3f7SMatthew Dillon 				    shadow_list
18262ad1a3f7SMatthew Dillon 				);
1827eaf13dd7SJohn Dyson 				new_backing_object->shadow_count++;
1828b921a12bSAlan Cox 				vm_object_reference_locked(new_backing_object);
182989f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(new_backing_object);
183095e5e988SJohn Dyson 				object->backing_object_offset +=
183195e5e988SJohn Dyson 					backing_object->backing_object_offset;
1832de5f6a77SJohn Dyson 			}
1833df8bae1dSRodney W. Grimes 
1834df8bae1dSRodney W. Grimes 			/*
18350d94caffSDavid Greenman 			 * Drop the reference count on backing_object. Since
183622ec553fSAlan Cox 			 * its ref_count was at least 2, it will not vanish.
1837df8bae1dSRodney W. Grimes 			 */
183822ec553fSAlan Cox 			backing_object->ref_count--;
183989f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(backing_object);
1840df8bae1dSRodney W. Grimes 			object_bypasses++;
1841df8bae1dSRodney W. Grimes 		}
1842df8bae1dSRodney W. Grimes 
1843df8bae1dSRodney W. Grimes 		/*
1844df8bae1dSRodney W. Grimes 		 * Try again with this object's new backing object.
1845df8bae1dSRodney W. Grimes 		 */
1846df8bae1dSRodney W. Grimes 	}
1847df8bae1dSRodney W. Grimes }
1848df8bae1dSRodney W. Grimes 
1849df8bae1dSRodney W. Grimes /*
1850bff99f0dSAlan Cox  *	vm_object_page_remove:
1851df8bae1dSRodney W. Grimes  *
185268855966SAlan Cox  *	For the given object, either frees or invalidates each of the
18536bbee8e2SAlan Cox  *	specified pages.  In general, a page is freed.  However, if a page is
18546bbee8e2SAlan Cox  *	wired for any reason other than the existence of a managed, wired
18556bbee8e2SAlan Cox  *	mapping, then it may be invalidated but not removed from the object.
18566bbee8e2SAlan Cox  *	Pages are specified by the given range ["start", "end") and the option
18576bbee8e2SAlan Cox  *	OBJPR_CLEANONLY.  As a special case, if "end" is zero, then the range
18586bbee8e2SAlan Cox  *	extends from "start" to the end of the object.  If the option
18596bbee8e2SAlan Cox  *	OBJPR_CLEANONLY is specified, then only the non-dirty pages within the
18606bbee8e2SAlan Cox  *	specified range are affected.  If the option OBJPR_NOTMAPPED is
18616bbee8e2SAlan Cox  *	specified, then the pages within the specified range must have no
18626bbee8e2SAlan Cox  *	mappings.  Otherwise, if this option is not specified, any mappings to
18636bbee8e2SAlan Cox  *	the specified pages are removed before the pages are freed or
18646bbee8e2SAlan Cox  *	invalidated.
186568855966SAlan Cox  *
18666bbee8e2SAlan Cox  *	In general, this operation should only be performed on objects that
18676bbee8e2SAlan Cox  *	contain managed pages.  There are, however, two exceptions.  First, it
18686bbee8e2SAlan Cox  *	is performed on the kernel and kmem objects by vm_map_entry_delete().
18696bbee8e2SAlan Cox  *	Second, it is used by msync(..., MS_INVALIDATE) to invalidate device-
18706bbee8e2SAlan Cox  *	backed pages.  In both of these cases, the option OBJPR_CLEANONLY must
18716bbee8e2SAlan Cox  *	not be specified and the option OBJPR_NOTMAPPED must be specified.
1872df8bae1dSRodney W. Grimes  *
1873df8bae1dSRodney W. Grimes  *	The object must be locked.
1874df8bae1dSRodney W. Grimes  */
187526f9a767SRodney W. Grimes void
1876ecde4b32SAlan Cox vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
18776bbee8e2SAlan Cox     int options)
1878df8bae1dSRodney W. Grimes {
1879d031cff1SMatthew Dillon 	vm_page_t p, next;
1880df8bae1dSRodney W. Grimes 
188189f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
188228634820SAlan Cox 	KASSERT((object->flags & OBJ_UNMANAGED) == 0 ||
18836bbee8e2SAlan Cox 	    (options & (OBJPR_CLEANONLY | OBJPR_NOTMAPPED)) == OBJPR_NOTMAPPED,
18846bbee8e2SAlan Cox 	    ("vm_object_page_remove: illegal options for object %p", object));
1885ecde4b32SAlan Cox 	if (object->resident_page_count == 0)
188625732691SAlan Cox 		goto skipmemq;
1887d474eaaaSDoug Rabson 	vm_object_pip_add(object, 1);
188826f9a767SRodney W. Grimes again:
1889b382c10aSKonstantin Belousov 	p = vm_page_find_least(object, start);
18902965a453SKip Macy 
189175741c04SAlan Cox 	/*
18926bbee8e2SAlan Cox 	 * Here, the variable "p" is either (1) the page with the least pindex
18936bbee8e2SAlan Cox 	 * greater than or equal to the parameter "start" or (2) NULL.
189475741c04SAlan Cox 	 */
18956bbee8e2SAlan Cox 	for (; p != NULL && (p->pindex < end || end == 0); p = next) {
1896b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
189775741c04SAlan Cox 
189859677d3cSAlan Cox 		/*
18996bbee8e2SAlan Cox 		 * If the page is wired for any reason besides the existence
19006bbee8e2SAlan Cox 		 * of managed, wired mappings, then it cannot be freed.  For
19016bbee8e2SAlan Cox 		 * example, fictitious pages, which represent device memory,
19026bbee8e2SAlan Cox 		 * are inherently wired and cannot be freed.  They can,
19036bbee8e2SAlan Cox 		 * however, be invalidated if the option OBJPR_CLEANONLY is
19046bbee8e2SAlan Cox 		 * not specified.
190559677d3cSAlan Cox 		 */
19062965a453SKip Macy 		vm_page_lock(p);
19073aaea6efSKonstantin Belousov 		if (vm_page_xbusied(p)) {
19083aaea6efSKonstantin Belousov 			VM_OBJECT_WUNLOCK(object);
19093aaea6efSKonstantin Belousov 			vm_page_busy_sleep(p, "vmopax");
19103aaea6efSKonstantin Belousov 			VM_OBJECT_WLOCK(object);
19113aaea6efSKonstantin Belousov 			goto again;
19123aaea6efSKonstantin Belousov 		}
19136195b24aSKonstantin Belousov 		if (p->wire_count != 0) {
19146195b24aSKonstantin Belousov 			if ((options & OBJPR_NOTMAPPED) == 0)
19154fec79beSAlan Cox 				pmap_remove_all(p);
19166bbee8e2SAlan Cox 			if ((options & OBJPR_CLEANONLY) == 0) {
1917bd7e5f99SJohn Dyson 				p->valid = 0;
1918a28042d1SAlan Cox 				vm_page_undirty(p);
1919a28042d1SAlan Cox 			}
1920ebf5d94eSKonstantin Belousov 			goto next;
19210d94caffSDavid Greenman 		}
1922c7aebda8SAttilio Rao 		if (vm_page_busied(p)) {
1923c7aebda8SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
1924c7aebda8SAttilio Rao 			vm_page_busy_sleep(p, "vmopar");
1925c7aebda8SAttilio Rao 			VM_OBJECT_WLOCK(object);
192626f9a767SRodney W. Grimes 			goto again;
1927c7aebda8SAttilio Rao 		}
192868855966SAlan Cox 		KASSERT((p->flags & PG_FICTITIOUS) == 0,
192968855966SAlan Cox 		    ("vm_object_page_remove: page %p is fictitious", p));
19306bbee8e2SAlan Cox 		if ((options & OBJPR_CLEANONLY) != 0 && p->valid != 0) {
19316bbee8e2SAlan Cox 			if ((options & OBJPR_NOTMAPPED) == 0)
193278985e42SAlan Cox 				pmap_remove_write(p);
1933ebf5d94eSKonstantin Belousov 			if (p->dirty)
1934ebf5d94eSKonstantin Belousov 				goto next;
19352965a453SKip Macy 		}
19366195b24aSKonstantin Belousov 		if ((options & OBJPR_NOTMAPPED) == 0)
19374fec79beSAlan Cox 			pmap_remove_all(p);
1938df8bae1dSRodney W. Grimes 		vm_page_free(p);
1939ebf5d94eSKonstantin Belousov next:
19402965a453SKip Macy 		vm_page_unlock(p);
19412965a453SKip Macy 	}
1942f919ebdeSDavid Greenman 	vm_object_pip_wakeup(object);
194325732691SAlan Cox skipmemq:
1944c9341161SAttilio Rao 	if (__predict_false(!vm_object_cache_is_empty(object)))
1945c9444914SAlan Cox 		vm_page_cache_free(object, start, end);
1946c0503609SDavid Greenman }
1947df8bae1dSRodney W. Grimes 
1948df8bae1dSRodney W. Grimes /*
19493138cd36SMark Johnston  *	vm_object_page_noreuse:
1950936c09acSJohn Baldwin  *
19513138cd36SMark Johnston  *	For the given object, attempt to move the specified pages to
19523138cd36SMark Johnston  *	the head of the inactive queue.  This bypasses regular LRU
19533138cd36SMark Johnston  *	operation and allows the pages to be reused quickly under memory
19543138cd36SMark Johnston  *	pressure.  If a page is wired for any reason, then it will not
19553138cd36SMark Johnston  *	be queued.  Pages are specified by the range ["start", "end").
19563138cd36SMark Johnston  *	As a special case, if "end" is zero, then the range extends from
19573138cd36SMark Johnston  *	"start" to the end of the object.
1958936c09acSJohn Baldwin  *
1959936c09acSJohn Baldwin  *	This operation should only be performed on objects that
196028634820SAlan Cox  *	contain non-fictitious, managed pages.
1961936c09acSJohn Baldwin  *
1962936c09acSJohn Baldwin  *	The object must be locked.
1963936c09acSJohn Baldwin  */
1964936c09acSJohn Baldwin void
19653138cd36SMark Johnston vm_object_page_noreuse(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1966936c09acSJohn Baldwin {
1967936c09acSJohn Baldwin 	struct mtx *mtx, *new_mtx;
1968936c09acSJohn Baldwin 	vm_page_t p, next;
1969936c09acSJohn Baldwin 
197089f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
197128634820SAlan Cox 	KASSERT((object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0,
19723138cd36SMark Johnston 	    ("vm_object_page_noreuse: illegal object %p", object));
1973936c09acSJohn Baldwin 	if (object->resident_page_count == 0)
1974936c09acSJohn Baldwin 		return;
1975936c09acSJohn Baldwin 	p = vm_page_find_least(object, start);
1976936c09acSJohn Baldwin 
1977936c09acSJohn Baldwin 	/*
1978936c09acSJohn Baldwin 	 * Here, the variable "p" is either (1) the page with the least pindex
1979936c09acSJohn Baldwin 	 * greater than or equal to the parameter "start" or (2) NULL.
1980936c09acSJohn Baldwin 	 */
1981936c09acSJohn Baldwin 	mtx = NULL;
1982936c09acSJohn Baldwin 	for (; p != NULL && (p->pindex < end || end == 0); p = next) {
1983936c09acSJohn Baldwin 		next = TAILQ_NEXT(p, listq);
1984936c09acSJohn Baldwin 
1985936c09acSJohn Baldwin 		/*
1986936c09acSJohn Baldwin 		 * Avoid releasing and reacquiring the same page lock.
1987936c09acSJohn Baldwin 		 */
1988936c09acSJohn Baldwin 		new_mtx = vm_page_lockptr(p);
1989936c09acSJohn Baldwin 		if (mtx != new_mtx) {
1990936c09acSJohn Baldwin 			if (mtx != NULL)
1991936c09acSJohn Baldwin 				mtx_unlock(mtx);
1992936c09acSJohn Baldwin 			mtx = new_mtx;
1993936c09acSJohn Baldwin 			mtx_lock(mtx);
1994936c09acSJohn Baldwin 		}
19953138cd36SMark Johnston 		vm_page_deactivate_noreuse(p);
1996936c09acSJohn Baldwin 	}
1997936c09acSJohn Baldwin 	if (mtx != NULL)
1998936c09acSJohn Baldwin 		mtx_unlock(mtx);
1999936c09acSJohn Baldwin }
2000936c09acSJohn Baldwin 
2001936c09acSJohn Baldwin /*
2002387aabc5SAlan Cox  *	Populate the specified range of the object with valid pages.  Returns
2003387aabc5SAlan Cox  *	TRUE if the range is successfully populated and FALSE otherwise.
2004387aabc5SAlan Cox  *
2005387aabc5SAlan Cox  *	Note: This function should be optimized to pass a larger array of
2006387aabc5SAlan Cox  *	pages to vm_pager_get_pages() before it is applied to a non-
2007387aabc5SAlan Cox  *	OBJT_DEVICE object.
2008387aabc5SAlan Cox  *
2009387aabc5SAlan Cox  *	The object must be locked.
2010387aabc5SAlan Cox  */
2011387aabc5SAlan Cox boolean_t
2012387aabc5SAlan Cox vm_object_populate(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
2013387aabc5SAlan Cox {
2014093c7f39SGleb Smirnoff 	vm_page_t m;
2015387aabc5SAlan Cox 	vm_pindex_t pindex;
2016387aabc5SAlan Cox 	int rv;
2017387aabc5SAlan Cox 
201889f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
2019387aabc5SAlan Cox 	for (pindex = start; pindex < end; pindex++) {
20205944de8eSKonstantin Belousov 		m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL);
2021387aabc5SAlan Cox 		if (m->valid != VM_PAGE_BITS_ALL) {
2022b0cd2017SGleb Smirnoff 			rv = vm_pager_get_pages(object, &m, 1, NULL, NULL);
2023387aabc5SAlan Cox 			if (rv != VM_PAGER_OK) {
20242965a453SKip Macy 				vm_page_lock(m);
2025387aabc5SAlan Cox 				vm_page_free(m);
20262965a453SKip Macy 				vm_page_unlock(m);
2027387aabc5SAlan Cox 				break;
2028387aabc5SAlan Cox 			}
2029387aabc5SAlan Cox 		}
2030387aabc5SAlan Cox 		/*
2031387aabc5SAlan Cox 		 * Keep "m" busy because a subsequent iteration may unlock
2032387aabc5SAlan Cox 		 * the object.
2033387aabc5SAlan Cox 		 */
2034387aabc5SAlan Cox 	}
2035387aabc5SAlan Cox 	if (pindex > start) {
2036387aabc5SAlan Cox 		m = vm_page_lookup(object, start);
2037387aabc5SAlan Cox 		while (m != NULL && m->pindex < pindex) {
2038c7aebda8SAttilio Rao 			vm_page_xunbusy(m);
2039387aabc5SAlan Cox 			m = TAILQ_NEXT(m, listq);
2040387aabc5SAlan Cox 		}
2041387aabc5SAlan Cox 	}
2042387aabc5SAlan Cox 	return (pindex == end);
2043387aabc5SAlan Cox }
2044387aabc5SAlan Cox 
2045387aabc5SAlan Cox /*
2046df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
2047df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
2048df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
2049df8bae1dSRodney W. Grimes  *
2050df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
2051df8bae1dSRodney W. Grimes  *
2052df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
2053df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
2054df8bae1dSRodney W. Grimes  *
2055df8bae1dSRodney W. Grimes  *	Parameters:
2056df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
2057df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
2058df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
205957a21abaSAlan Cox  *		next_size	Size of reference to the second object
20603364c323SKonstantin Belousov  *		reserved	Indicator that extension region has
20613364c323SKonstantin Belousov  *				swap accounted for
2062df8bae1dSRodney W. Grimes  *
2063df8bae1dSRodney W. Grimes  *	Conditions:
2064df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
2065df8bae1dSRodney W. Grimes  */
20660d94caffSDavid Greenman boolean_t
206757a21abaSAlan Cox vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset,
20683364c323SKonstantin Belousov     vm_size_t prev_size, vm_size_t next_size, boolean_t reserved)
2069df8bae1dSRodney W. Grimes {
2070ea41812fSAlan Cox 	vm_pindex_t next_pindex;
2071df8bae1dSRodney W. Grimes 
207200e1854aSAlan Cox 	if (prev_object == NULL)
2073df8bae1dSRodney W. Grimes 		return (TRUE);
207489f6b863SAttilio Rao 	VM_OBJECT_WLOCK(prev_object);
20759ded9474SKonstantin Belousov 	if ((prev_object->type != OBJT_DEFAULT &&
20769ded9474SKonstantin Belousov 	    prev_object->type != OBJT_SWAP) ||
2077f08f7dcaSKonstantin Belousov 	    (prev_object->flags & OBJ_TMPFS_NODE) != 0) {
207889f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(prev_object);
207930dcfc09SJohn Dyson 		return (FALSE);
208030dcfc09SJohn Dyson 	}
208130dcfc09SJohn Dyson 
2082df8bae1dSRodney W. Grimes 	/*
2083df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
2084df8bae1dSRodney W. Grimes 	 */
2085df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
2086df8bae1dSRodney W. Grimes 
2087df8bae1dSRodney W. Grimes 	/*
20880d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
20890d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
20900d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
2091df8bae1dSRodney W. Grimes 	 */
20928cc7e047SJohn Dyson 	if (prev_object->backing_object != NULL) {
209389f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(prev_object);
2094df8bae1dSRodney W. Grimes 		return (FALSE);
2095df8bae1dSRodney W. Grimes 	}
2096a316d390SJohn Dyson 
2097a316d390SJohn Dyson 	prev_size >>= PAGE_SHIFT;
2098a316d390SJohn Dyson 	next_size >>= PAGE_SHIFT;
209957a21abaSAlan Cox 	next_pindex = OFF_TO_IDX(prev_offset) + prev_size;
21008cc7e047SJohn Dyson 
21018cc7e047SJohn Dyson 	if ((prev_object->ref_count > 1) &&
2102ea41812fSAlan Cox 	    (prev_object->size != next_pindex)) {
210389f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(prev_object);
21048cc7e047SJohn Dyson 		return (FALSE);
21058cc7e047SJohn Dyson 	}
21068cc7e047SJohn Dyson 
2107df8bae1dSRodney W. Grimes 	/*
21083364c323SKonstantin Belousov 	 * Account for the charge.
21093364c323SKonstantin Belousov 	 */
2110ef694c1aSEdward Tomasz Napierala 	if (prev_object->cred != NULL) {
21113364c323SKonstantin Belousov 
21123364c323SKonstantin Belousov 		/*
21133364c323SKonstantin Belousov 		 * If prev_object was charged, then this mapping,
21143364c323SKonstantin Belousov 		 * althought not charged now, may become writable
2115ef694c1aSEdward Tomasz Napierala 		 * later. Non-NULL cred in the object would prevent
21163364c323SKonstantin Belousov 		 * swap reservation during enabling of the write
21173364c323SKonstantin Belousov 		 * access, so reserve swap now. Failed reservation
21183364c323SKonstantin Belousov 		 * cause allocation of the separate object for the map
21193364c323SKonstantin Belousov 		 * entry, and swap reservation for this entry is
21203364c323SKonstantin Belousov 		 * managed in appropriate time.
21213364c323SKonstantin Belousov 		 */
2122ef694c1aSEdward Tomasz Napierala 		if (!reserved && !swap_reserve_by_cred(ptoa(next_size),
2123ef694c1aSEdward Tomasz Napierala 		    prev_object->cred)) {
21243364c323SKonstantin Belousov 			return (FALSE);
21253364c323SKonstantin Belousov 		}
21263364c323SKonstantin Belousov 		prev_object->charge += ptoa(next_size);
21273364c323SKonstantin Belousov 	}
21283364c323SKonstantin Belousov 
21293364c323SKonstantin Belousov 	/*
21300d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
21310d94caffSDavid Greenman 	 * deallocation.
2132df8bae1dSRodney W. Grimes 	 */
2133ea41812fSAlan Cox 	if (next_pindex < prev_object->size) {
21346bbee8e2SAlan Cox 		vm_object_page_remove(prev_object, next_pindex, next_pindex +
21356bbee8e2SAlan Cox 		    next_size, 0);
2136ea41812fSAlan Cox 		if (prev_object->type == OBJT_SWAP)
2137ea41812fSAlan Cox 			swap_pager_freespace(prev_object,
2138ea41812fSAlan Cox 					     next_pindex, next_size);
21393364c323SKonstantin Belousov #if 0
2140ef694c1aSEdward Tomasz Napierala 		if (prev_object->cred != NULL) {
21413364c323SKonstantin Belousov 			KASSERT(prev_object->charge >=
21423364c323SKonstantin Belousov 			    ptoa(prev_object->size - next_pindex),
21433364c323SKonstantin Belousov 			    ("object %p overcharged 1 %jx %jx", prev_object,
21443364c323SKonstantin Belousov 				(uintmax_t)next_pindex, (uintmax_t)next_size));
21453364c323SKonstantin Belousov 			prev_object->charge -= ptoa(prev_object->size -
21463364c323SKonstantin Belousov 			    next_pindex);
21473364c323SKonstantin Belousov 		}
21483364c323SKonstantin Belousov #endif
2149ea41812fSAlan Cox 	}
2150df8bae1dSRodney W. Grimes 
2151df8bae1dSRodney W. Grimes 	/*
2152df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
2153df8bae1dSRodney W. Grimes 	 */
2154ea41812fSAlan Cox 	if (next_pindex + next_size > prev_object->size)
2155ea41812fSAlan Cox 		prev_object->size = next_pindex + next_size;
2156df8bae1dSRodney W. Grimes 
215789f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(prev_object);
2158df8bae1dSRodney W. Grimes 	return (TRUE);
2159df8bae1dSRodney W. Grimes }
2160df8bae1dSRodney W. Grimes 
21617a5a6352SMatthew Dillon void
21627a5a6352SMatthew Dillon vm_object_set_writeable_dirty(vm_object_t object)
21637a5a6352SMatthew Dillon {
21647a5a6352SMatthew Dillon 
216589f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
2166f40cb1c6SKonstantin Belousov 	if (object->type != OBJT_VNODE) {
2167f40cb1c6SKonstantin Belousov 		if ((object->flags & OBJ_TMPFS_NODE) != 0) {
2168f40cb1c6SKonstantin Belousov 			KASSERT(object->type == OBJT_SWAP, ("non-swap tmpfs"));
2169f40cb1c6SKonstantin Belousov 			vm_object_set_flag(object, OBJ_TMPFS_DIRTY);
2170f40cb1c6SKonstantin Belousov 		}
21713280870dSKonstantin Belousov 		return;
2172f40cb1c6SKonstantin Belousov 	}
21733280870dSKonstantin Belousov 	object->generation++;
21743280870dSKonstantin Belousov 	if ((object->flags & OBJ_MIGHTBEDIRTY) != 0)
2175ee39666aSJeff Roberson 		return;
2176af51d7bfSAlan Cox 	vm_object_set_flag(object, OBJ_MIGHTBEDIRTY);
21777a5a6352SMatthew Dillon }
21787a5a6352SMatthew Dillon 
217903462509SAlan Cox /*
218003462509SAlan Cox  *	vm_object_unwire:
218103462509SAlan Cox  *
218203462509SAlan Cox  *	For each page offset within the specified range of the given object,
218303462509SAlan Cox  *	find the highest-level page in the shadow chain and unwire it.  A page
218403462509SAlan Cox  *	must exist at every page offset, and the highest-level page must be
218503462509SAlan Cox  *	wired.
218603462509SAlan Cox  */
218703462509SAlan Cox void
218803462509SAlan Cox vm_object_unwire(vm_object_t object, vm_ooffset_t offset, vm_size_t length,
218903462509SAlan Cox     uint8_t queue)
219003462509SAlan Cox {
219103462509SAlan Cox 	vm_object_t tobject;
219203462509SAlan Cox 	vm_page_t m, tm;
219303462509SAlan Cox 	vm_pindex_t end_pindex, pindex, tpindex;
219403462509SAlan Cox 	int depth, locked_depth;
219503462509SAlan Cox 
219603462509SAlan Cox 	KASSERT((offset & PAGE_MASK) == 0,
219703462509SAlan Cox 	    ("vm_object_unwire: offset is not page aligned"));
219803462509SAlan Cox 	KASSERT((length & PAGE_MASK) == 0,
219903462509SAlan Cox 	    ("vm_object_unwire: length is not a multiple of PAGE_SIZE"));
220003462509SAlan Cox 	/* The wired count of a fictitious page never changes. */
220103462509SAlan Cox 	if ((object->flags & OBJ_FICTITIOUS) != 0)
220203462509SAlan Cox 		return;
220303462509SAlan Cox 	pindex = OFF_TO_IDX(offset);
220403462509SAlan Cox 	end_pindex = pindex + atop(length);
220503462509SAlan Cox 	locked_depth = 1;
220603462509SAlan Cox 	VM_OBJECT_RLOCK(object);
220703462509SAlan Cox 	m = vm_page_find_least(object, pindex);
220803462509SAlan Cox 	while (pindex < end_pindex) {
220903462509SAlan Cox 		if (m == NULL || pindex < m->pindex) {
221003462509SAlan Cox 			/*
221103462509SAlan Cox 			 * The first object in the shadow chain doesn't
221203462509SAlan Cox 			 * contain a page at the current index.  Therefore,
221303462509SAlan Cox 			 * the page must exist in a backing object.
221403462509SAlan Cox 			 */
221503462509SAlan Cox 			tobject = object;
221603462509SAlan Cox 			tpindex = pindex;
221703462509SAlan Cox 			depth = 0;
221803462509SAlan Cox 			do {
221903462509SAlan Cox 				tpindex +=
222003462509SAlan Cox 				    OFF_TO_IDX(tobject->backing_object_offset);
222103462509SAlan Cox 				tobject = tobject->backing_object;
222203462509SAlan Cox 				KASSERT(tobject != NULL,
222303462509SAlan Cox 				    ("vm_object_unwire: missing page"));
222403462509SAlan Cox 				if ((tobject->flags & OBJ_FICTITIOUS) != 0)
222503462509SAlan Cox 					goto next_page;
222603462509SAlan Cox 				depth++;
222703462509SAlan Cox 				if (depth == locked_depth) {
222803462509SAlan Cox 					locked_depth++;
222903462509SAlan Cox 					VM_OBJECT_RLOCK(tobject);
223003462509SAlan Cox 				}
223103462509SAlan Cox 			} while ((tm = vm_page_lookup(tobject, tpindex)) ==
223203462509SAlan Cox 			    NULL);
223303462509SAlan Cox 		} else {
223403462509SAlan Cox 			tm = m;
223503462509SAlan Cox 			m = TAILQ_NEXT(m, listq);
223603462509SAlan Cox 		}
223703462509SAlan Cox 		vm_page_lock(tm);
223803462509SAlan Cox 		vm_page_unwire(tm, queue);
223903462509SAlan Cox 		vm_page_unlock(tm);
224003462509SAlan Cox next_page:
224103462509SAlan Cox 		pindex++;
224203462509SAlan Cox 	}
224303462509SAlan Cox 	/* Release the accumulated object locks. */
224403462509SAlan Cox 	for (depth = 0; depth < locked_depth; depth++) {
224503462509SAlan Cox 		tobject = object->backing_object;
224603462509SAlan Cox 		VM_OBJECT_RUNLOCK(object);
224703462509SAlan Cox 		object = tobject;
224803462509SAlan Cox 	}
224903462509SAlan Cox }
225003462509SAlan Cox 
225163e4c6cdSEric van Gyzen struct vnode *
225263e4c6cdSEric van Gyzen vm_object_vnode(vm_object_t object)
225363e4c6cdSEric van Gyzen {
225463e4c6cdSEric van Gyzen 
225563e4c6cdSEric van Gyzen 	VM_OBJECT_ASSERT_LOCKED(object);
225663e4c6cdSEric van Gyzen 	if (object->type == OBJT_VNODE)
225763e4c6cdSEric van Gyzen 		return (object->handle);
225863e4c6cdSEric van Gyzen 	if (object->type == OBJT_SWAP && (object->flags & OBJ_TMPFS) != 0)
225963e4c6cdSEric van Gyzen 		return (object->un_pager.swp.swp_tmpfs);
226063e4c6cdSEric van Gyzen 	return (NULL);
226163e4c6cdSEric van Gyzen }
226263e4c6cdSEric van Gyzen 
2263ff87ae35SJohn Baldwin static int
2264ff87ae35SJohn Baldwin sysctl_vm_object_list(SYSCTL_HANDLER_ARGS)
2265ff87ae35SJohn Baldwin {
2266ff87ae35SJohn Baldwin 	struct kinfo_vmobject kvo;
2267ff87ae35SJohn Baldwin 	char *fullpath, *freepath;
2268ff87ae35SJohn Baldwin 	struct vnode *vp;
2269ff87ae35SJohn Baldwin 	struct vattr va;
2270ff87ae35SJohn Baldwin 	vm_object_t obj;
2271ff87ae35SJohn Baldwin 	vm_page_t m;
2272ff87ae35SJohn Baldwin 	int count, error;
2273ff87ae35SJohn Baldwin 
2274ff87ae35SJohn Baldwin 	if (req->oldptr == NULL) {
2275ff87ae35SJohn Baldwin 		/*
2276ff87ae35SJohn Baldwin 		 * If an old buffer has not been provided, generate an
2277ff87ae35SJohn Baldwin 		 * estimate of the space needed for a subsequent call.
2278ff87ae35SJohn Baldwin 		 */
2279ff87ae35SJohn Baldwin 		mtx_lock(&vm_object_list_mtx);
2280ff87ae35SJohn Baldwin 		count = 0;
2281ff87ae35SJohn Baldwin 		TAILQ_FOREACH(obj, &vm_object_list, object_list) {
2282ff87ae35SJohn Baldwin 			if (obj->type == OBJT_DEAD)
2283ff87ae35SJohn Baldwin 				continue;
2284ff87ae35SJohn Baldwin 			count++;
2285ff87ae35SJohn Baldwin 		}
2286ff87ae35SJohn Baldwin 		mtx_unlock(&vm_object_list_mtx);
2287ff87ae35SJohn Baldwin 		return (SYSCTL_OUT(req, NULL, sizeof(struct kinfo_vmobject) *
2288ff87ae35SJohn Baldwin 		    count * 11 / 10));
2289ff87ae35SJohn Baldwin 	}
2290ff87ae35SJohn Baldwin 
2291ff87ae35SJohn Baldwin 	error = 0;
2292ff87ae35SJohn Baldwin 
2293ff87ae35SJohn Baldwin 	/*
2294ff87ae35SJohn Baldwin 	 * VM objects are type stable and are never removed from the
2295ff87ae35SJohn Baldwin 	 * list once added.  This allows us to safely read obj->object_list
2296ff87ae35SJohn Baldwin 	 * after reacquiring the VM object lock.
2297ff87ae35SJohn Baldwin 	 */
2298ff87ae35SJohn Baldwin 	mtx_lock(&vm_object_list_mtx);
2299ff87ae35SJohn Baldwin 	TAILQ_FOREACH(obj, &vm_object_list, object_list) {
2300ff87ae35SJohn Baldwin 		if (obj->type == OBJT_DEAD)
2301ff87ae35SJohn Baldwin 			continue;
2302ff87ae35SJohn Baldwin 		VM_OBJECT_RLOCK(obj);
2303ff87ae35SJohn Baldwin 		if (obj->type == OBJT_DEAD) {
2304ff87ae35SJohn Baldwin 			VM_OBJECT_RUNLOCK(obj);
2305ff87ae35SJohn Baldwin 			continue;
2306ff87ae35SJohn Baldwin 		}
2307ff87ae35SJohn Baldwin 		mtx_unlock(&vm_object_list_mtx);
2308ff87ae35SJohn Baldwin 		kvo.kvo_size = ptoa(obj->size);
2309ff87ae35SJohn Baldwin 		kvo.kvo_resident = obj->resident_page_count;
2310ff87ae35SJohn Baldwin 		kvo.kvo_ref_count = obj->ref_count;
2311ff87ae35SJohn Baldwin 		kvo.kvo_shadow_count = obj->shadow_count;
2312ff87ae35SJohn Baldwin 		kvo.kvo_memattr = obj->memattr;
2313ff87ae35SJohn Baldwin 		kvo.kvo_active = 0;
2314ff87ae35SJohn Baldwin 		kvo.kvo_inactive = 0;
2315ff87ae35SJohn Baldwin 		TAILQ_FOREACH(m, &obj->memq, listq) {
2316ff87ae35SJohn Baldwin 			/*
2317ff87ae35SJohn Baldwin 			 * A page may belong to the object but be
2318ff87ae35SJohn Baldwin 			 * dequeued and set to PQ_NONE while the
2319ff87ae35SJohn Baldwin 			 * object lock is not held.  This makes the
2320ff87ae35SJohn Baldwin 			 * reads of m->queue below racy, and we do not
2321ff87ae35SJohn Baldwin 			 * count pages set to PQ_NONE.  However, this
2322ff87ae35SJohn Baldwin 			 * sysctl is only meant to give an
2323ff87ae35SJohn Baldwin 			 * approximation of the system anyway.
2324ff87ae35SJohn Baldwin 			 */
2325ff87ae35SJohn Baldwin 			if (m->queue == PQ_ACTIVE)
2326ff87ae35SJohn Baldwin 				kvo.kvo_active++;
2327ff87ae35SJohn Baldwin 			else if (m->queue == PQ_INACTIVE)
2328ff87ae35SJohn Baldwin 				kvo.kvo_inactive++;
2329ff87ae35SJohn Baldwin 		}
2330ff87ae35SJohn Baldwin 
2331ff87ae35SJohn Baldwin 		kvo.kvo_vn_fileid = 0;
2332ff87ae35SJohn Baldwin 		kvo.kvo_vn_fsid = 0;
2333ff87ae35SJohn Baldwin 		freepath = NULL;
2334ff87ae35SJohn Baldwin 		fullpath = "";
2335ff87ae35SJohn Baldwin 		vp = NULL;
2336ff87ae35SJohn Baldwin 		switch (obj->type) {
2337ff87ae35SJohn Baldwin 		case OBJT_DEFAULT:
2338ff87ae35SJohn Baldwin 			kvo.kvo_type = KVME_TYPE_DEFAULT;
2339ff87ae35SJohn Baldwin 			break;
2340ff87ae35SJohn Baldwin 		case OBJT_VNODE:
2341ff87ae35SJohn Baldwin 			kvo.kvo_type = KVME_TYPE_VNODE;
2342ff87ae35SJohn Baldwin 			vp = obj->handle;
2343ff87ae35SJohn Baldwin 			vref(vp);
2344ff87ae35SJohn Baldwin 			break;
2345ff87ae35SJohn Baldwin 		case OBJT_SWAP:
2346ff87ae35SJohn Baldwin 			kvo.kvo_type = KVME_TYPE_SWAP;
2347ff87ae35SJohn Baldwin 			break;
2348ff87ae35SJohn Baldwin 		case OBJT_DEVICE:
2349ff87ae35SJohn Baldwin 			kvo.kvo_type = KVME_TYPE_DEVICE;
2350ff87ae35SJohn Baldwin 			break;
2351ff87ae35SJohn Baldwin 		case OBJT_PHYS:
2352ff87ae35SJohn Baldwin 			kvo.kvo_type = KVME_TYPE_PHYS;
2353ff87ae35SJohn Baldwin 			break;
2354ff87ae35SJohn Baldwin 		case OBJT_DEAD:
2355ff87ae35SJohn Baldwin 			kvo.kvo_type = KVME_TYPE_DEAD;
2356ff87ae35SJohn Baldwin 			break;
2357ff87ae35SJohn Baldwin 		case OBJT_SG:
2358ff87ae35SJohn Baldwin 			kvo.kvo_type = KVME_TYPE_SG;
2359ff87ae35SJohn Baldwin 			break;
2360ff87ae35SJohn Baldwin 		case OBJT_MGTDEVICE:
2361ff87ae35SJohn Baldwin 			kvo.kvo_type = KVME_TYPE_MGTDEVICE;
2362ff87ae35SJohn Baldwin 			break;
2363ff87ae35SJohn Baldwin 		default:
2364ff87ae35SJohn Baldwin 			kvo.kvo_type = KVME_TYPE_UNKNOWN;
2365ff87ae35SJohn Baldwin 			break;
2366ff87ae35SJohn Baldwin 		}
2367ff87ae35SJohn Baldwin 		VM_OBJECT_RUNLOCK(obj);
2368ff87ae35SJohn Baldwin 		if (vp != NULL) {
2369ff87ae35SJohn Baldwin 			vn_fullpath(curthread, vp, &fullpath, &freepath);
2370ff87ae35SJohn Baldwin 			vn_lock(vp, LK_SHARED | LK_RETRY);
2371ff87ae35SJohn Baldwin 			if (VOP_GETATTR(vp, &va, curthread->td_ucred) == 0) {
2372ff87ae35SJohn Baldwin 				kvo.kvo_vn_fileid = va.va_fileid;
2373ff87ae35SJohn Baldwin 				kvo.kvo_vn_fsid = va.va_fsid;
2374ff87ae35SJohn Baldwin 			}
2375ff87ae35SJohn Baldwin 			vput(vp);
2376ff87ae35SJohn Baldwin 		}
2377ff87ae35SJohn Baldwin 
2378ff87ae35SJohn Baldwin 		strlcpy(kvo.kvo_path, fullpath, sizeof(kvo.kvo_path));
2379ff87ae35SJohn Baldwin 		if (freepath != NULL)
2380ff87ae35SJohn Baldwin 			free(freepath, M_TEMP);
2381ff87ae35SJohn Baldwin 
2382ff87ae35SJohn Baldwin 		/* Pack record size down */
2383ff87ae35SJohn Baldwin 		kvo.kvo_structsize = offsetof(struct kinfo_vmobject, kvo_path) +
2384ff87ae35SJohn Baldwin 		    strlen(kvo.kvo_path) + 1;
2385ff87ae35SJohn Baldwin 		kvo.kvo_structsize = roundup(kvo.kvo_structsize,
2386ff87ae35SJohn Baldwin 		    sizeof(uint64_t));
2387ff87ae35SJohn Baldwin 		error = SYSCTL_OUT(req, &kvo, kvo.kvo_structsize);
2388ff87ae35SJohn Baldwin 		mtx_lock(&vm_object_list_mtx);
2389ff87ae35SJohn Baldwin 		if (error)
2390ff87ae35SJohn Baldwin 			break;
2391ff87ae35SJohn Baldwin 	}
2392ff87ae35SJohn Baldwin 	mtx_unlock(&vm_object_list_mtx);
2393ff87ae35SJohn Baldwin 	return (error);
2394ff87ae35SJohn Baldwin }
2395ff87ae35SJohn Baldwin SYSCTL_PROC(_vm, OID_AUTO, objects, CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_SKIP |
2396ff87ae35SJohn Baldwin     CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_object_list, "S,kinfo_vmobject",
2397ff87ae35SJohn Baldwin     "List of VM objects");
2398ff87ae35SJohn Baldwin 
2399c7c34a24SBruce Evans #include "opt_ddb.h"
2400c3cb3e12SDavid Greenman #ifdef DDB
2401c7c34a24SBruce Evans #include <sys/kernel.h>
2402c7c34a24SBruce Evans 
2403ce9edcf5SPoul-Henning Kamp #include <sys/cons.h>
2404c7c34a24SBruce Evans 
2405c7c34a24SBruce Evans #include <ddb/ddb.h>
2406c7c34a24SBruce Evans 
2407cac597e4SBruce Evans static int
24081b40f8c0SMatthew Dillon _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
2409a1f6d91cSDavid Greenman {
2410a1f6d91cSDavid Greenman 	vm_map_t tmpm;
2411a1f6d91cSDavid Greenman 	vm_map_entry_t tmpe;
2412a1f6d91cSDavid Greenman 	vm_object_t obj;
2413a1f6d91cSDavid Greenman 	int entcount;
2414a1f6d91cSDavid Greenman 
2415a1f6d91cSDavid Greenman 	if (map == 0)
2416a1f6d91cSDavid Greenman 		return 0;
2417a1f6d91cSDavid Greenman 
2418a1f6d91cSDavid Greenman 	if (entry == 0) {
2419a1f6d91cSDavid Greenman 		tmpe = map->header.next;
2420a1f6d91cSDavid Greenman 		entcount = map->nentries;
2421a1f6d91cSDavid Greenman 		while (entcount-- && (tmpe != &map->header)) {
2422a1f6d91cSDavid Greenman 			if (_vm_object_in_map(map, object, tmpe)) {
2423a1f6d91cSDavid Greenman 				return 1;
2424a1f6d91cSDavid Greenman 			}
2425a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
2426a1f6d91cSDavid Greenman 		}
24279fdfe602SMatthew Dillon 	} else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
24289fdfe602SMatthew Dillon 		tmpm = entry->object.sub_map;
2429a1f6d91cSDavid Greenman 		tmpe = tmpm->header.next;
2430a1f6d91cSDavid Greenman 		entcount = tmpm->nentries;
2431a1f6d91cSDavid Greenman 		while (entcount-- && tmpe != &tmpm->header) {
2432a1f6d91cSDavid Greenman 			if (_vm_object_in_map(tmpm, object, tmpe)) {
2433a1f6d91cSDavid Greenman 				return 1;
2434a1f6d91cSDavid Greenman 			}
2435a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
2436a1f6d91cSDavid Greenman 		}
24378aef1712SMatthew Dillon 	} else if ((obj = entry->object.vm_object) != NULL) {
243824a1cce3SDavid Greenman 		for (; obj; obj = obj->backing_object)
2439a1f6d91cSDavid Greenman 			if (obj == object) {
2440a1f6d91cSDavid Greenman 				return 1;
2441a1f6d91cSDavid Greenman 			}
2442a1f6d91cSDavid Greenman 	}
2443a1f6d91cSDavid Greenman 	return 0;
2444a1f6d91cSDavid Greenman }
2445a1f6d91cSDavid Greenman 
2446cac597e4SBruce Evans static int
24471b40f8c0SMatthew Dillon vm_object_in_map(vm_object_t object)
2448a1f6d91cSDavid Greenman {
2449a1f6d91cSDavid Greenman 	struct proc *p;
24501005a129SJohn Baldwin 
245160517fd1SJohn Baldwin 	/* sx_slock(&allproc_lock); */
2452f67af5c9SXin LI 	FOREACH_PROC_IN_SYSTEM(p) {
2453a1f6d91cSDavid Greenman 		if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
2454a1f6d91cSDavid Greenman 			continue;
2455553629ebSJake Burkholder 		if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
245660517fd1SJohn Baldwin 			/* sx_sunlock(&allproc_lock); */
2457a1f6d91cSDavid Greenman 			return 1;
2458a1f6d91cSDavid Greenman 		}
2459553629ebSJake Burkholder 	}
246060517fd1SJohn Baldwin 	/* sx_sunlock(&allproc_lock); */
2461a1f6d91cSDavid Greenman 	if (_vm_object_in_map(kernel_map, object, 0))
2462a1f6d91cSDavid Greenman 		return 1;
2463a1f6d91cSDavid Greenman 	return 0;
2464a1f6d91cSDavid Greenman }
2465a1f6d91cSDavid Greenman 
2466c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check)
2467f708ef1bSPoul-Henning Kamp {
2468a1f6d91cSDavid Greenman 	vm_object_t object;
2469a1f6d91cSDavid Greenman 
2470a1f6d91cSDavid Greenman 	/*
2471a1f6d91cSDavid Greenman 	 * make sure that internal objs are in a map somewhere
2472a1f6d91cSDavid Greenman 	 * and none have zero ref counts.
2473a1f6d91cSDavid Greenman 	 */
2474cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
247524a1cce3SDavid Greenman 		if (object->handle == NULL &&
247624a1cce3SDavid Greenman 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2477a1f6d91cSDavid Greenman 			if (object->ref_count == 0) {
24783efc015bSPeter Wemm 				db_printf("vmochk: internal obj has zero ref count: %ld\n",
24793efc015bSPeter Wemm 					(long)object->size);
2480a1f6d91cSDavid Greenman 			}
2481a1f6d91cSDavid Greenman 			if (!vm_object_in_map(object)) {
2482fc62ef1fSBruce Evans 				db_printf(
2483fc62ef1fSBruce Evans 			"vmochk: internal obj is not in a map: "
2484fc62ef1fSBruce Evans 			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2485fc62ef1fSBruce Evans 				    object->ref_count, (u_long)object->size,
2486fc62ef1fSBruce Evans 				    (u_long)object->size,
2487fc62ef1fSBruce Evans 				    (void *)object->backing_object);
2488a1f6d91cSDavid Greenman 			}
2489a1f6d91cSDavid Greenman 		}
2490a1f6d91cSDavid Greenman 	}
2491a1f6d91cSDavid Greenman }
2492a1f6d91cSDavid Greenman 
249326f9a767SRodney W. Grimes /*
2494df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
2495df8bae1dSRodney W. Grimes  */
2496c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static)
2497df8bae1dSRodney W. Grimes {
2498c7c34a24SBruce Evans 	/* XXX convert args. */
2499c7c34a24SBruce Evans 	vm_object_t object = (vm_object_t)addr;
2500c7c34a24SBruce Evans 	boolean_t full = have_addr;
2501c7c34a24SBruce Evans 
2502d031cff1SMatthew Dillon 	vm_page_t p;
2503df8bae1dSRodney W. Grimes 
2504c7c34a24SBruce Evans 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
2505c7c34a24SBruce Evans #define	count	was_count
2506c7c34a24SBruce Evans 
2507d031cff1SMatthew Dillon 	int count;
2508df8bae1dSRodney W. Grimes 
2509df8bae1dSRodney W. Grimes 	if (object == NULL)
2510df8bae1dSRodney W. Grimes 		return;
2511df8bae1dSRodney W. Grimes 
2512eb95adefSBruce Evans 	db_iprintf(
2513ef694c1aSEdward Tomasz Napierala 	    "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x ruid %d charge %jx\n",
2514e47cd172SMaxime Henrion 	    object, (int)object->type, (uintmax_t)object->size,
25153364c323SKonstantin Belousov 	    object->resident_page_count, object->ref_count, object->flags,
2516ef694c1aSEdward Tomasz Napierala 	    object->cred ? object->cred->cr_ruid : -1, (uintmax_t)object->charge);
2517e47cd172SMaxime Henrion 	db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n",
25181c7c3c6aSMatthew Dillon 	    object->shadow_count,
2519eb95adefSBruce Evans 	    object->backing_object ? object->backing_object->ref_count : 0,
2520e47cd172SMaxime Henrion 	    object->backing_object, (uintmax_t)object->backing_object_offset);
2521df8bae1dSRodney W. Grimes 
2522df8bae1dSRodney W. Grimes 	if (!full)
2523df8bae1dSRodney W. Grimes 		return;
2524df8bae1dSRodney W. Grimes 
2525c7c34a24SBruce Evans 	db_indent += 2;
2526df8bae1dSRodney W. Grimes 	count = 0;
2527fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(p, &object->memq, listq) {
2528df8bae1dSRodney W. Grimes 		if (count == 0)
2529c7c34a24SBruce Evans 			db_iprintf("memory:=");
2530df8bae1dSRodney W. Grimes 		else if (count == 6) {
2531c7c34a24SBruce Evans 			db_printf("\n");
2532c7c34a24SBruce Evans 			db_iprintf(" ...");
2533df8bae1dSRodney W. Grimes 			count = 0;
2534df8bae1dSRodney W. Grimes 		} else
2535c7c34a24SBruce Evans 			db_printf(",");
2536df8bae1dSRodney W. Grimes 		count++;
2537df8bae1dSRodney W. Grimes 
2538e47cd172SMaxime Henrion 		db_printf("(off=0x%jx,page=0x%jx)",
2539e47cd172SMaxime Henrion 		    (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p));
2540df8bae1dSRodney W. Grimes 	}
2541df8bae1dSRodney W. Grimes 	if (count != 0)
2542c7c34a24SBruce Evans 		db_printf("\n");
2543c7c34a24SBruce Evans 	db_indent -= 2;
2544df8bae1dSRodney W. Grimes }
25455070c7f8SJohn Dyson 
2546c7c34a24SBruce Evans /* XXX. */
2547c7c34a24SBruce Evans #undef count
2548c7c34a24SBruce Evans 
2549c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */
25505070c7f8SJohn Dyson void
25511b40f8c0SMatthew Dillon vm_object_print(
25521b40f8c0SMatthew Dillon         /* db_expr_t */ long addr,
25531b40f8c0SMatthew Dillon 	boolean_t have_addr,
25541b40f8c0SMatthew Dillon 	/* db_expr_t */ long count,
25551b40f8c0SMatthew Dillon 	char *modif)
2556c7c34a24SBruce Evans {
2557c7c34a24SBruce Evans 	vm_object_print_static(addr, have_addr, count, modif);
2558c7c34a24SBruce Evans }
2559c7c34a24SBruce Evans 
2560c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
25615070c7f8SJohn Dyson {
25625070c7f8SJohn Dyson 	vm_object_t object;
2563bb2ac86fSKonstantin Belousov 	vm_pindex_t fidx;
2564bb2ac86fSKonstantin Belousov 	vm_paddr_t pa;
2565bb2ac86fSKonstantin Belousov 	vm_page_t m, prev_m;
2566bb2ac86fSKonstantin Belousov 	int rcount, nl, c;
2567cc64b484SAlfred Perlstein 
2568bb2ac86fSKonstantin Belousov 	nl = 0;
2569cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
2570fc62ef1fSBruce Evans 		db_printf("new object: %p\n", (void *)object);
25715070c7f8SJohn Dyson 		if (nl > 18) {
25725070c7f8SJohn Dyson 			c = cngetc();
25735070c7f8SJohn Dyson 			if (c != ' ')
25745070c7f8SJohn Dyson 				return;
25755070c7f8SJohn Dyson 			nl = 0;
25765070c7f8SJohn Dyson 		}
25775070c7f8SJohn Dyson 		nl++;
25785070c7f8SJohn Dyson 		rcount = 0;
25795070c7f8SJohn Dyson 		fidx = 0;
2580bb2ac86fSKonstantin Belousov 		pa = -1;
2581bb2ac86fSKonstantin Belousov 		TAILQ_FOREACH(m, &object->memq, listq) {
2582bb2ac86fSKonstantin Belousov 			if (m->pindex > 128)
2583bb2ac86fSKonstantin Belousov 				break;
2584bb2ac86fSKonstantin Belousov 			if ((prev_m = TAILQ_PREV(m, pglist, listq)) != NULL &&
2585bb2ac86fSKonstantin Belousov 			    prev_m->pindex + 1 != m->pindex) {
25865070c7f8SJohn Dyson 				if (rcount) {
25873efc015bSPeter Wemm 					db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
25883efc015bSPeter Wemm 						(long)fidx, rcount, (long)pa);
25895070c7f8SJohn Dyson 					if (nl > 18) {
25905070c7f8SJohn Dyson 						c = cngetc();
25915070c7f8SJohn Dyson 						if (c != ' ')
25925070c7f8SJohn Dyson 							return;
25935070c7f8SJohn Dyson 						nl = 0;
25945070c7f8SJohn Dyson 					}
25955070c7f8SJohn Dyson 					nl++;
25965070c7f8SJohn Dyson 					rcount = 0;
25975070c7f8SJohn Dyson 				}
25985070c7f8SJohn Dyson 			}
25995070c7f8SJohn Dyson 			if (rcount &&
26005070c7f8SJohn Dyson 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
26015070c7f8SJohn Dyson 				++rcount;
26025070c7f8SJohn Dyson 				continue;
26035070c7f8SJohn Dyson 			}
26045070c7f8SJohn Dyson 			if (rcount) {
26052446e4f0SAlan Cox 				db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
26063efc015bSPeter Wemm 					(long)fidx, rcount, (long)pa);
26075070c7f8SJohn Dyson 				if (nl > 18) {
26085070c7f8SJohn Dyson 					c = cngetc();
26095070c7f8SJohn Dyson 					if (c != ' ')
26105070c7f8SJohn Dyson 						return;
26115070c7f8SJohn Dyson 					nl = 0;
26125070c7f8SJohn Dyson 				}
26135070c7f8SJohn Dyson 				nl++;
26145070c7f8SJohn Dyson 			}
2615bb2ac86fSKonstantin Belousov 			fidx = m->pindex;
26165070c7f8SJohn Dyson 			pa = VM_PAGE_TO_PHYS(m);
26175070c7f8SJohn Dyson 			rcount = 1;
26185070c7f8SJohn Dyson 		}
26195070c7f8SJohn Dyson 		if (rcount) {
26203efc015bSPeter Wemm 			db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
26213efc015bSPeter Wemm 				(long)fidx, rcount, (long)pa);
26225070c7f8SJohn Dyson 			if (nl > 18) {
26235070c7f8SJohn Dyson 				c = cngetc();
26245070c7f8SJohn Dyson 				if (c != ' ')
26255070c7f8SJohn Dyson 					return;
26265070c7f8SJohn Dyson 				nl = 0;
26275070c7f8SJohn Dyson 			}
26285070c7f8SJohn Dyson 			nl++;
26295070c7f8SJohn Dyson 		}
26305070c7f8SJohn Dyson 	}
26315070c7f8SJohn Dyson }
2632c3cb3e12SDavid Greenman #endif /* DDB */
2633