xref: /freebsd/sys/vm/vm_object.c (revision 2fcd1ff68fd7184baee480f39f7d08fb1d73be22)
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.
16fbbd9655SWarner Losh  * 3. 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>
76f425ab8eSKonstantin Belousov #include <sys/pctrie.h>
77b9b7a4beSMatthew Dillon #include <sys/sysctl.h>
781b367556SJason Evans #include <sys/mutex.h>
79fb919e4dSMark Murray #include <sys/proc.h>		/* for curproc, pageproc */
80fb919e4dSMark Murray #include <sys/socket.h>
813364c323SKonstantin Belousov #include <sys/resourcevar.h>
8289f6b863SAttilio Rao #include <sys/rwlock.h>
83ff87ae35SJohn Baldwin #include <sys/user.h>
84fb919e4dSMark Murray #include <sys/vnode.h>
85fb919e4dSMark Murray #include <sys/vmmeter.h>
861005a129SJohn Baldwin #include <sys/sx.h>
87df8bae1dSRodney W. Grimes 
88df8bae1dSRodney W. Grimes #include <vm/vm.h>
89efeaf95aSDavid Greenman #include <vm/vm_param.h>
90efeaf95aSDavid Greenman #include <vm/pmap.h>
91efeaf95aSDavid Greenman #include <vm/vm_map.h>
92efeaf95aSDavid Greenman #include <vm/vm_object.h>
93df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
9426f9a767SRodney W. Grimes #include <vm/vm_pageout.h>
950d94caffSDavid Greenman #include <vm/vm_pager.h>
9605f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h>
97a1f6d91cSDavid Greenman #include <vm/vm_kern.h>
98efeaf95aSDavid Greenman #include <vm/vm_extern.h>
99774d251dSAttilio Rao #include <vm/vm_radix.h>
100f8a47341SAlan Cox #include <vm/vm_reserv.h>
101670d17b5SJeff Roberson #include <vm/uma.h>
10226f9a767SRodney W. Grimes 
103c53f7aceSDag-Erling Smørgrav static int old_msync;
104c53f7aceSDag-Erling Smørgrav SYSCTL_INT(_vm, OID_AUTO, old_msync, CTLFLAG_RW, &old_msync, 0,
105c53f7aceSDag-Erling Smørgrav     "Use old (insecure) msync behavior");
106c53f7aceSDag-Erling Smørgrav 
107757216f3SKonstantin Belousov static int	vm_object_page_collect_flush(vm_object_t object, vm_page_t p,
108126d6082SKonstantin Belousov 		    int pagerflags, int flags, boolean_t *clearobjflags,
109126d6082SKonstantin Belousov 		    boolean_t *eio);
1103280870dSKonstantin Belousov static boolean_t vm_object_page_remove_write(vm_page_t p, int flags,
111126d6082SKonstantin Belousov 		    boolean_t *clearobjflags);
112b9b7a4beSMatthew Dillon static void	vm_object_qcollapse(vm_object_t object);
11302dd8331SAlan Cox static void	vm_object_vndeallocate(vm_object_t object);
114f6b04d2bSDavid Greenman 
115df8bae1dSRodney W. Grimes /*
116df8bae1dSRodney W. Grimes  *	Virtual memory objects maintain the actual data
117df8bae1dSRodney W. Grimes  *	associated with allocated virtual memory.  A given
118df8bae1dSRodney W. Grimes  *	page of memory exists within exactly one object.
119df8bae1dSRodney W. Grimes  *
120df8bae1dSRodney W. Grimes  *	An object is only deallocated when all "references"
121df8bae1dSRodney W. Grimes  *	are given up.  Only one "reference" to a given
122df8bae1dSRodney W. Grimes  *	region of an object should be writeable.
123df8bae1dSRodney W. Grimes  *
124df8bae1dSRodney W. Grimes  *	Associated with each object is a list of all resident
125df8bae1dSRodney W. Grimes  *	memory pages belonging to that object; this list is
126df8bae1dSRodney W. Grimes  *	maintained by the "vm_page" module, and locked by the object's
127df8bae1dSRodney W. Grimes  *	lock.
128df8bae1dSRodney W. Grimes  *
129df8bae1dSRodney W. Grimes  *	Each object also records a "pager" routine which is
130df8bae1dSRodney W. Grimes  *	used to retrieve (and store) pages to the proper backing
131df8bae1dSRodney W. Grimes  *	storage.  In addition, objects may be backed by other
132df8bae1dSRodney W. Grimes  *	objects from which they were virtual-copied.
133df8bae1dSRodney W. Grimes  *
134df8bae1dSRodney W. Grimes  *	The only items within the object structure which are
135df8bae1dSRodney W. Grimes  *	modified after time of creation are:
136df8bae1dSRodney W. Grimes  *		reference count		locked by object's lock
137df8bae1dSRodney W. Grimes  *		pager routine		locked by object's lock
138df8bae1dSRodney W. Grimes  *
139df8bae1dSRodney W. Grimes  */
140df8bae1dSRodney W. Grimes 
14128f8db14SBruce Evans struct object_q vm_object_list;
142a5698387SAlan Cox struct mtx vm_object_list_mtx;	/* lock for object list and count */
143cccf11b8SAlan Cox 
144cccf11b8SAlan Cox struct vm_object kernel_object_store;
145cccf11b8SAlan Cox struct vm_object kmem_object_store;
146df8bae1dSRodney W. Grimes 
1476472ac3dSEd Schouten static SYSCTL_NODE(_vm_stats, OID_AUTO, object, CTLFLAG_RD, 0,
1486472ac3dSEd Schouten     "VM object stats");
149604c2bbcSAlan Cox 
150f708ef1bSPoul-Henning Kamp static long object_collapses;
151604c2bbcSAlan Cox SYSCTL_LONG(_vm_stats_object, OID_AUTO, collapses, CTLFLAG_RD,
152604c2bbcSAlan Cox     &object_collapses, 0, "VM object collapses");
153604c2bbcSAlan Cox 
154f708ef1bSPoul-Henning Kamp static long object_bypasses;
155604c2bbcSAlan Cox SYSCTL_LONG(_vm_stats_object, OID_AUTO, bypasses, CTLFLAG_RD,
156604c2bbcSAlan Cox     &object_bypasses, 0, "VM object bypasses");
157dad740e9SAlan Cox 
158670d17b5SJeff Roberson static uma_zone_t obj_zone;
1598355f576SJeff Roberson 
160b23f72e9SBrian Feldman static int vm_object_zinit(void *mem, int size, int flags);
1618355f576SJeff Roberson 
1628355f576SJeff Roberson #ifdef INVARIANTS
1638355f576SJeff Roberson static void vm_object_zdtor(void *mem, int size, void *arg);
1648355f576SJeff Roberson 
1658355f576SJeff Roberson static void
1668355f576SJeff Roberson vm_object_zdtor(void *mem, int size, void *arg)
1678355f576SJeff Roberson {
1688355f576SJeff Roberson 	vm_object_t object;
1698355f576SJeff Roberson 
1708355f576SJeff Roberson 	object = (vm_object_t)mem;
171e735691bSJohn Baldwin 	KASSERT(object->ref_count == 0,
172e735691bSJohn Baldwin 	    ("object %p ref_count = %d", object, object->ref_count));
17343186e53SAlan Cox 	KASSERT(TAILQ_EMPTY(&object->memq),
174198da1b2SAttilio Rao 	    ("object %p has resident pages in its memq", object));
175774d251dSAttilio Rao 	KASSERT(vm_radix_is_empty(&object->rtree),
176774d251dSAttilio Rao 	    ("object %p has resident pages in its trie", object));
177f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
178f8a47341SAlan Cox 	KASSERT(LIST_EMPTY(&object->rvq),
179f8a47341SAlan Cox 	    ("object %p has reservations",
180f8a47341SAlan Cox 	    object));
181f8a47341SAlan Cox #endif
1828355f576SJeff Roberson 	KASSERT(object->paging_in_progress == 0,
1838355f576SJeff Roberson 	    ("object %p paging_in_progress = %d",
1848355f576SJeff Roberson 	    object, object->paging_in_progress));
1858355f576SJeff Roberson 	KASSERT(object->resident_page_count == 0,
1868355f576SJeff Roberson 	    ("object %p resident_page_count = %d",
1878355f576SJeff Roberson 	    object, object->resident_page_count));
1888355f576SJeff Roberson 	KASSERT(object->shadow_count == 0,
1898355f576SJeff Roberson 	    ("object %p shadow_count = %d",
1908355f576SJeff Roberson 	    object, object->shadow_count));
191e735691bSJohn Baldwin 	KASSERT(object->type == OBJT_DEAD,
192e735691bSJohn Baldwin 	    ("object %p has non-dead type %d",
193e735691bSJohn Baldwin 	    object, object->type));
1948355f576SJeff Roberson }
1958355f576SJeff Roberson #endif
1968355f576SJeff Roberson 
197b23f72e9SBrian Feldman static int
198b23f72e9SBrian Feldman vm_object_zinit(void *mem, int size, int flags)
1998355f576SJeff Roberson {
2008355f576SJeff Roberson 	vm_object_t object;
2018355f576SJeff Roberson 
2028355f576SJeff Roberson 	object = (vm_object_t)mem;
203777a36c5SAlan Cox 	rw_init_flags(&object->lock, "vm object", RW_DUPOK | RW_NEW);
2048355f576SJeff Roberson 
2058355f576SJeff Roberson 	/* These are true for any object that has been freed */
206e735691bSJohn Baldwin 	object->type = OBJT_DEAD;
207e735691bSJohn Baldwin 	object->ref_count = 0;
208cd1241fbSKonstantin Belousov 	vm_radix_init(&object->rtree);
2098355f576SJeff Roberson 	object->paging_in_progress = 0;
2108355f576SJeff Roberson 	object->resident_page_count = 0;
2118355f576SJeff Roberson 	object->shadow_count = 0;
212f425ab8eSKonstantin Belousov 	object->flags = OBJ_DEAD;
213e735691bSJohn Baldwin 
214e735691bSJohn Baldwin 	mtx_lock(&vm_object_list_mtx);
215e735691bSJohn Baldwin 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
216e735691bSJohn Baldwin 	mtx_unlock(&vm_object_list_mtx);
217b23f72e9SBrian Feldman 	return (0);
2188355f576SJeff Roberson }
219df8bae1dSRodney W. Grimes 
220a4915c21SAttilio Rao static void
2216395da54SIan Dowse _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
222df8bae1dSRodney W. Grimes {
2230cddd8f0SMatthew Dillon 
224df8bae1dSRodney W. Grimes 	TAILQ_INIT(&object->memq);
2251c500307SAlan Cox 	LIST_INIT(&object->shadow_head);
226a1f6d91cSDavid Greenman 
22724a1cce3SDavid Greenman 	object->type = type;
228f425ab8eSKonstantin Belousov 	if (type == OBJT_SWAP)
229f425ab8eSKonstantin Belousov 		pctrie_init(&object->un_pager.swp.swp_blks);
230f425ab8eSKonstantin Belousov 
231f425ab8eSKonstantin Belousov 	/*
232f425ab8eSKonstantin Belousov 	 * Ensure that swap_pager_swapoff() iteration over object_list
233f425ab8eSKonstantin Belousov 	 * sees up to date type and pctrie head if it observed
234f425ab8eSKonstantin Belousov 	 * non-dead object.
235f425ab8eSKonstantin Belousov 	 */
236f425ab8eSKonstantin Belousov 	atomic_thread_fence_rel();
237f425ab8eSKonstantin Belousov 
23828634820SAlan Cox 	switch (type) {
23928634820SAlan Cox 	case OBJT_DEAD:
24028634820SAlan Cox 		panic("_vm_object_allocate: can't create OBJT_DEAD");
24128634820SAlan Cox 	case OBJT_DEFAULT:
24228634820SAlan Cox 	case OBJT_SWAP:
24328634820SAlan Cox 		object->flags = OBJ_ONEMAPPING;
24428634820SAlan Cox 		break;
24528634820SAlan Cox 	case OBJT_DEVICE:
24628634820SAlan Cox 	case OBJT_SG:
24728634820SAlan Cox 		object->flags = OBJ_FICTITIOUS | OBJ_UNMANAGED;
24828634820SAlan Cox 		break;
24928634820SAlan Cox 	case OBJT_MGTDEVICE:
25028634820SAlan Cox 		object->flags = OBJ_FICTITIOUS;
25128634820SAlan Cox 		break;
25228634820SAlan Cox 	case OBJT_PHYS:
25328634820SAlan Cox 		object->flags = OBJ_UNMANAGED;
25428634820SAlan Cox 		break;
25528634820SAlan Cox 	case OBJT_VNODE:
25628634820SAlan Cox 		object->flags = 0;
25728634820SAlan Cox 		break;
25828634820SAlan Cox 	default:
25928634820SAlan Cox 		panic("_vm_object_allocate: type %d is undefined", type);
26028634820SAlan Cox 	}
261df8bae1dSRodney W. Grimes 	object->size = size;
262b881da26SAlan Cox 	object->generation = 1;
263a1f6d91cSDavid Greenman 	object->ref_count = 1;
2643153e878SAlan Cox 	object->memattr = VM_MEMATTR_DEFAULT;
265ef694c1aSEdward Tomasz Napierala 	object->cred = NULL;
2663364c323SKonstantin Belousov 	object->charge = 0;
26724a1cce3SDavid Greenman 	object->handle = NULL;
26824a1cce3SDavid Greenman 	object->backing_object = NULL;
269a316d390SJohn Dyson 	object->backing_object_offset = (vm_ooffset_t) 0;
270f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
271f8a47341SAlan Cox 	LIST_INIT(&object->rvq);
272f8a47341SAlan Cox #endif
2731bdbd705SKonstantin Belousov 	umtx_shm_object_init(object);
274df8bae1dSRodney W. Grimes }
275df8bae1dSRodney W. Grimes 
276df8bae1dSRodney W. Grimes /*
27726f9a767SRodney W. Grimes  *	vm_object_init:
27826f9a767SRodney W. Grimes  *
27926f9a767SRodney W. Grimes  *	Initialize the VM objects module.
28026f9a767SRodney W. Grimes  */
28126f9a767SRodney W. Grimes void
2821b40f8c0SMatthew Dillon vm_object_init(void)
28326f9a767SRodney W. Grimes {
28426f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_list);
2856008862bSJohn Baldwin 	mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF);
2860217125fSDavid Greenman 
28789f6b863SAttilio Rao 	rw_init(&kernel_object->lock, "kernel vm object");
288d1780e8dSKonstantin Belousov 	_vm_object_allocate(OBJT_PHYS, atop(VM_MAX_KERNEL_ADDRESS -
289d1780e8dSKonstantin Belousov 	    VM_MIN_KERNEL_ADDRESS), kernel_object);
290f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
291f8a47341SAlan Cox 	kernel_object->flags |= OBJ_COLORED;
292f8a47341SAlan Cox 	kernel_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS);
293f8a47341SAlan Cox #endif
29426f9a767SRodney W. Grimes 
29589f6b863SAttilio Rao 	rw_init(&kmem_object->lock, "kmem vm object");
296d1780e8dSKonstantin Belousov 	_vm_object_allocate(OBJT_PHYS, atop(VM_MAX_KERNEL_ADDRESS -
297d1780e8dSKonstantin Belousov 	    VM_MIN_KERNEL_ADDRESS), kmem_object);
298f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
299f8a47341SAlan Cox 	kmem_object->flags |= OBJ_COLORED;
300f8a47341SAlan Cox 	kmem_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS);
301f8a47341SAlan Cox #endif
302ed6a7863SAlan Cox 
3038dbca793STor Egge 	/*
3048dbca793STor Egge 	 * The lock portion of struct vm_object must be type stable due
3058dbca793STor Egge 	 * to vm_pageout_fallback_object_lock locking a vm object
3068dbca793STor Egge 	 * without holding any references to it.
3078dbca793STor Egge 	 */
3088355f576SJeff Roberson 	obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL,
3098355f576SJeff Roberson #ifdef INVARIANTS
3108355f576SJeff Roberson 	    vm_object_zdtor,
3118355f576SJeff Roberson #else
3128355f576SJeff Roberson 	    NULL,
3138355f576SJeff Roberson #endif
3145df87b21SJeff Roberson 	    vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
315774d251dSAttilio Rao 
316cd1241fbSKonstantin Belousov 	vm_radix_zinit();
31799448ed1SJohn Dyson }
31899448ed1SJohn Dyson 
31999448ed1SJohn Dyson void
3201b40f8c0SMatthew Dillon vm_object_clear_flag(vm_object_t object, u_short bits)
3211b40f8c0SMatthew Dillon {
3225440b5a9SAlan Cox 
32389f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
324b06805adSJake Burkholder 	object->flags &= ~bits;
3251b40f8c0SMatthew Dillon }
3261b40f8c0SMatthew Dillon 
3273153e878SAlan Cox /*
3283153e878SAlan Cox  *	Sets the default memory attribute for the specified object.  Pages
3293153e878SAlan Cox  *	that are allocated to this object are by default assigned this memory
3303153e878SAlan Cox  *	attribute.
3313153e878SAlan Cox  *
3323153e878SAlan Cox  *	Presently, this function must be called before any pages are allocated
3333153e878SAlan Cox  *	to the object.  In the future, this requirement may be relaxed for
3343153e878SAlan Cox  *	"default" and "swap" objects.
3353153e878SAlan Cox  */
3363153e878SAlan Cox int
3373153e878SAlan Cox vm_object_set_memattr(vm_object_t object, vm_memattr_t memattr)
3383153e878SAlan Cox {
3393153e878SAlan Cox 
34089f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
3413153e878SAlan Cox 	switch (object->type) {
3423153e878SAlan Cox 	case OBJT_DEFAULT:
3433153e878SAlan Cox 	case OBJT_DEVICE:
34496b0b92aSAlan Cox 	case OBJT_MGTDEVICE:
3453153e878SAlan Cox 	case OBJT_PHYS:
34601381811SJohn Baldwin 	case OBJT_SG:
3473153e878SAlan Cox 	case OBJT_SWAP:
3483153e878SAlan Cox 	case OBJT_VNODE:
3493153e878SAlan Cox 		if (!TAILQ_EMPTY(&object->memq))
3503153e878SAlan Cox 			return (KERN_FAILURE);
3513153e878SAlan Cox 		break;
3523153e878SAlan Cox 	case OBJT_DEAD:
3533153e878SAlan Cox 		return (KERN_INVALID_ARGUMENT);
35496b0b92aSAlan Cox 	default:
35596b0b92aSAlan Cox 		panic("vm_object_set_memattr: object %p is of undefined type",
35696b0b92aSAlan Cox 		    object);
3573153e878SAlan Cox 	}
3583153e878SAlan Cox 	object->memattr = memattr;
3593153e878SAlan Cox 	return (KERN_SUCCESS);
3603153e878SAlan Cox }
3613153e878SAlan Cox 
3621b40f8c0SMatthew Dillon void
3631b40f8c0SMatthew Dillon vm_object_pip_add(vm_object_t object, short i)
3641b40f8c0SMatthew Dillon {
365f279b88dSAlan Cox 
36689f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
367b06805adSJake Burkholder 	object->paging_in_progress += i;
3681b40f8c0SMatthew Dillon }
3691b40f8c0SMatthew Dillon 
3701b40f8c0SMatthew Dillon void
3711b40f8c0SMatthew Dillon vm_object_pip_subtract(vm_object_t object, short i)
3721b40f8c0SMatthew Dillon {
373d647a0edSAlan Cox 
37489f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
375b06805adSJake Burkholder 	object->paging_in_progress -= i;
3761b40f8c0SMatthew Dillon }
3771b40f8c0SMatthew Dillon 
3781b40f8c0SMatthew Dillon void
3791b40f8c0SMatthew Dillon vm_object_pip_wakeup(vm_object_t object)
3801b40f8c0SMatthew Dillon {
381f279b88dSAlan Cox 
38289f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
383b06805adSJake Burkholder 	object->paging_in_progress--;
3841b40f8c0SMatthew Dillon 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
3851b40f8c0SMatthew Dillon 		vm_object_clear_flag(object, OBJ_PIPWNT);
3861b40f8c0SMatthew Dillon 		wakeup(object);
3871b40f8c0SMatthew Dillon 	}
3881b40f8c0SMatthew Dillon }
3891b40f8c0SMatthew Dillon 
3901b40f8c0SMatthew Dillon void
3911b40f8c0SMatthew Dillon vm_object_pip_wakeupn(vm_object_t object, short i)
3921b40f8c0SMatthew Dillon {
393d647a0edSAlan Cox 
39489f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
3951b40f8c0SMatthew Dillon 	if (i)
396b06805adSJake Burkholder 		object->paging_in_progress -= i;
3971b40f8c0SMatthew Dillon 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
3981b40f8c0SMatthew Dillon 		vm_object_clear_flag(object, OBJ_PIPWNT);
3991b40f8c0SMatthew Dillon 		wakeup(object);
4001b40f8c0SMatthew Dillon 	}
4011b40f8c0SMatthew Dillon }
4021b40f8c0SMatthew Dillon 
4031b40f8c0SMatthew Dillon void
4041b40f8c0SMatthew Dillon vm_object_pip_wait(vm_object_t object, char *waitid)
4051b40f8c0SMatthew Dillon {
4061ca58953SAlan Cox 
40789f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
4081ca58953SAlan Cox 	while (object->paging_in_progress) {
4091ca58953SAlan Cox 		object->flags |= OBJ_PIPWNT;
4100dde287bSAttilio Rao 		VM_OBJECT_SLEEP(object, object, PVM, waitid, 0);
4111ca58953SAlan Cox 	}
4121b40f8c0SMatthew Dillon }
4131b40f8c0SMatthew Dillon 
41426f9a767SRodney W. Grimes /*
41526f9a767SRodney W. Grimes  *	vm_object_allocate:
41626f9a767SRodney W. Grimes  *
41726f9a767SRodney W. Grimes  *	Returns a new object with the given size.
41826f9a767SRodney W. Grimes  */
41926f9a767SRodney W. Grimes vm_object_t
4206395da54SIan Dowse vm_object_allocate(objtype_t type, vm_pindex_t size)
42126f9a767SRodney W. Grimes {
42290688d13SAlan Cox 	vm_object_t object;
42390688d13SAlan Cox 
42490688d13SAlan Cox 	object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK);
42590688d13SAlan Cox 	_vm_object_allocate(type, size, object);
42690688d13SAlan Cox 	return (object);
42726f9a767SRodney W. Grimes }
42826f9a767SRodney W. Grimes 
42926f9a767SRodney W. Grimes 
43026f9a767SRodney W. Grimes /*
431df8bae1dSRodney W. Grimes  *	vm_object_reference:
432df8bae1dSRodney W. Grimes  *
43315347817SAlan Cox  *	Gets another reference to the given object.  Note: OBJ_DEAD
43415347817SAlan Cox  *	objects can be referenced during final cleaning.
435df8bae1dSRodney W. Grimes  */
4366476c0d2SJohn Dyson void
4371b40f8c0SMatthew Dillon vm_object_reference(vm_object_t object)
438df8bae1dSRodney W. Grimes {
439df8bae1dSRodney W. Grimes 	if (object == NULL)
440df8bae1dSRodney W. Grimes 		return;
44189f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
44252481a9aSJeff Roberson 	vm_object_reference_locked(object);
44389f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
44495e5e988SJohn Dyson }
44595e5e988SJohn Dyson 
44623955314SAlfred Perlstein /*
447b921a12bSAlan Cox  *	vm_object_reference_locked:
448b921a12bSAlan Cox  *
449b921a12bSAlan Cox  *	Gets another reference to the given object.
450b921a12bSAlan Cox  *
451b921a12bSAlan Cox  *	The object must be locked.
452b921a12bSAlan Cox  */
453b921a12bSAlan Cox void
454b921a12bSAlan Cox vm_object_reference_locked(vm_object_t object)
455b921a12bSAlan Cox {
456b921a12bSAlan Cox 	struct vnode *vp;
457b921a12bSAlan Cox 
45889f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
459b921a12bSAlan Cox 	object->ref_count++;
460b921a12bSAlan Cox 	if (object->type == OBJT_VNODE) {
461b921a12bSAlan Cox 		vp = object->handle;
462b921a12bSAlan Cox 		vref(vp);
463b921a12bSAlan Cox 	}
464b921a12bSAlan Cox }
465b921a12bSAlan Cox 
466b921a12bSAlan Cox /*
4679d5abbddSJens Schweikhardt  * Handle deallocating an object of type OBJT_VNODE.
46823955314SAlfred Perlstein  */
46902dd8331SAlan Cox static void
4701b40f8c0SMatthew Dillon vm_object_vndeallocate(vm_object_t object)
47195e5e988SJohn Dyson {
47295e5e988SJohn Dyson 	struct vnode *vp = (struct vnode *) object->handle;
473219cbf59SEivind Eklund 
47489f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
4755526d2d9SEivind Eklund 	KASSERT(object->type == OBJT_VNODE,
4765526d2d9SEivind Eklund 	    ("vm_object_vndeallocate: not a vnode object"));
477219cbf59SEivind Eklund 	KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
478219cbf59SEivind Eklund #ifdef INVARIANTS
47995e5e988SJohn Dyson 	if (object->ref_count == 0) {
480411455a8SEdward Tomasz Napierala 		vn_printf(vp, "vm_object_vndeallocate ");
48195e5e988SJohn Dyson 		panic("vm_object_vndeallocate: bad object reference count");
48295e5e988SJohn Dyson 	}
48395e5e988SJohn Dyson #endif
48495e5e988SJohn Dyson 
4852a339d9eSKonstantin Belousov 	if (!umtx_shm_vnobj_persistent && object->ref_count == 1)
4861bdbd705SKonstantin Belousov 		umtx_shm_object_terminated(object);
4871bdbd705SKonstantin Belousov 
48830d57414SKonstantin Belousov 	/*
48930d57414SKonstantin Belousov 	 * The test for text of vp vnode does not need a bypass to
49030d57414SKonstantin Belousov 	 * reach right VV_TEXT there, since it is obtained from
49130d57414SKonstantin Belousov 	 * object->handle.
49230d57414SKonstantin Belousov 	 */
49330d57414SKonstantin Belousov 	if (object->ref_count > 1 || (vp->v_vflag & VV_TEXT) == 0) {
49495e5e988SJohn Dyson 		object->ref_count--;
49589f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
49603fa5b34SKonstantin Belousov 		/* vrele may need the vnode lock. */
49747221757SJohn Dyson 		vrele(vp);
49803fa5b34SKonstantin Belousov 	} else {
49986769ac0SKonstantin Belousov 		vhold(vp);
50089f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
50103fa5b34SKonstantin Belousov 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
50286769ac0SKonstantin Belousov 		vdrop(vp);
50389f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
50403fa5b34SKonstantin Belousov 		object->ref_count--;
50586769ac0SKonstantin Belousov 		if (object->type == OBJT_DEAD) {
50689f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
50786769ac0SKonstantin Belousov 			VOP_UNLOCK(vp, 0);
50886769ac0SKonstantin Belousov 		} else {
50903fa5b34SKonstantin Belousov 			if (object->ref_count == 0)
510877d24acSKonstantin Belousov 				VOP_UNSET_TEXT(vp);
51189f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
51203fa5b34SKonstantin Belousov 			vput(vp);
51303fa5b34SKonstantin Belousov 		}
514df8bae1dSRodney W. Grimes 	}
51586769ac0SKonstantin Belousov }
516df8bae1dSRodney W. Grimes 
517df8bae1dSRodney W. Grimes /*
518df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
519df8bae1dSRodney W. Grimes  *
520df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
521df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
522df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
523df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
524df8bae1dSRodney W. Grimes  *	may be relinquished.
525df8bae1dSRodney W. Grimes  *
526df8bae1dSRodney W. Grimes  *	No object may be locked.
527df8bae1dSRodney W. Grimes  */
52826f9a767SRodney W. Grimes void
5291b40f8c0SMatthew Dillon vm_object_deallocate(vm_object_t object)
530df8bae1dSRodney W. Grimes {
531df8bae1dSRodney W. Grimes 	vm_object_t temp;
5326f2af3fcSKonstantin Belousov 	struct vnode *vp;
533df8bae1dSRodney W. Grimes 
534df8bae1dSRodney W. Grimes 	while (object != NULL) {
53589f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
5363b68228cSAlan Cox 		if (object->type == OBJT_VNODE) {
53795e5e988SJohn Dyson 			vm_object_vndeallocate(object);
53823b186d3SAlan Cox 			return;
5395050aa86SKonstantin Belousov 		}
54095e5e988SJohn Dyson 
5418125b1e6SAlfred Perlstein 		KASSERT(object->ref_count != 0,
5428125b1e6SAlfred Perlstein 			("vm_object_deallocate: object deallocated too many times: %d", object->type));
5432be70f79SJohn Dyson 
5442be70f79SJohn Dyson 		/*
5458125b1e6SAlfred Perlstein 		 * If the reference count goes to 0 we start calling
5468125b1e6SAlfred Perlstein 		 * vm_object_terminate() on the object chain.
5478125b1e6SAlfred Perlstein 		 * A ref count of 1 may be a special case depending on the
5488125b1e6SAlfred Perlstein 		 * shadow count being 0 or 1.
5492be70f79SJohn Dyson 		 */
550c0877f10SJohn Dyson 		object->ref_count--;
5518125b1e6SAlfred Perlstein 		if (object->ref_count > 1) {
55289f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
55323b186d3SAlan Cox 			return;
5548125b1e6SAlfred Perlstein 		} else if (object->ref_count == 1) {
5556f2af3fcSKonstantin Belousov 			if (object->type == OBJT_SWAP &&
5566f2af3fcSKonstantin Belousov 			    (object->flags & OBJ_TMPFS) != 0) {
5576f2af3fcSKonstantin Belousov 				vp = object->un_pager.swp.swp_tmpfs;
5586f2af3fcSKonstantin Belousov 				vhold(vp);
5596f2af3fcSKonstantin Belousov 				VM_OBJECT_WUNLOCK(object);
5606f2af3fcSKonstantin Belousov 				vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
5616f2af3fcSKonstantin Belousov 				VM_OBJECT_WLOCK(object);
5627560005cSKonstantin Belousov 				if (object->type == OBJT_DEAD ||
5637560005cSKonstantin Belousov 				    object->ref_count != 1) {
5646f2af3fcSKonstantin Belousov 					VM_OBJECT_WUNLOCK(object);
5656f2af3fcSKonstantin Belousov 					VOP_UNLOCK(vp, 0);
5662309fa9bSKonstantin Belousov 					vdrop(vp);
5676f2af3fcSKonstantin Belousov 					return;
5687560005cSKonstantin Belousov 				}
5697560005cSKonstantin Belousov 				if ((object->flags & OBJ_TMPFS) != 0)
5706f2af3fcSKonstantin Belousov 					VOP_UNSET_TEXT(vp);
5716f2af3fcSKonstantin Belousov 				VOP_UNLOCK(vp, 0);
5722309fa9bSKonstantin Belousov 				vdrop(vp);
5736f2af3fcSKonstantin Belousov 			}
5744c8e0452SAlan Cox 			if (object->shadow_count == 0 &&
5754c8e0452SAlan Cox 			    object->handle == NULL &&
5764c8e0452SAlan Cox 			    (object->type == OBJT_DEFAULT ||
5776f2af3fcSKonstantin Belousov 			    (object->type == OBJT_SWAP &&
578f08f7dcaSKonstantin Belousov 			    (object->flags & OBJ_TMPFS_NODE) == 0))) {
5798125b1e6SAlfred Perlstein 				vm_object_set_flag(object, OBJ_ONEMAPPING);
5808125b1e6SAlfred Perlstein 			} else if ((object->shadow_count == 1) &&
5818125b1e6SAlfred Perlstein 			    (object->handle == NULL) &&
58224a1cce3SDavid Greenman 			    (object->type == OBJT_DEFAULT ||
58324a1cce3SDavid Greenman 			     object->type == OBJT_SWAP)) {
584a1f6d91cSDavid Greenman 				vm_object_t robject;
58595e5e988SJohn Dyson 
5861c500307SAlan Cox 				robject = LIST_FIRST(&object->shadow_head);
5875526d2d9SEivind Eklund 				KASSERT(robject != NULL,
588219cbf59SEivind Eklund 				    ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
5895526d2d9SEivind Eklund 					 object->ref_count,
5905526d2d9SEivind Eklund 					 object->shadow_count));
5914bace8e7SKonstantin Belousov 				KASSERT((robject->flags & OBJ_TMPFS_NODE) == 0,
5924bace8e7SKonstantin Belousov 				    ("shadowed tmpfs v_object %p", object));
59389f6b863SAttilio Rao 				if (!VM_OBJECT_TRYWLOCK(robject)) {
594b72b0115SAlan Cox 					/*
595b72b0115SAlan Cox 					 * Avoid a potential deadlock.
596b72b0115SAlan Cox 					 */
597b72b0115SAlan Cox 					object->ref_count++;
59889f6b863SAttilio Rao 					VM_OBJECT_WUNLOCK(object);
599a7d86121SAlan Cox 					/*
600a7d86121SAlan Cox 					 * More likely than not the thread
601a7d86121SAlan Cox 					 * holding robject's lock has lower
602a7d86121SAlan Cox 					 * priority than the current thread.
603a7d86121SAlan Cox 					 * Let the lower priority thread run.
604a7d86121SAlan Cox 					 */
6058db5fc58SJohn Baldwin 					pause("vmo_de", 1);
606b72b0115SAlan Cox 					continue;
607b72b0115SAlan Cox 				}
608d936694fSAlan Cox 				/*
609d936694fSAlan Cox 				 * Collapse object into its shadow unless its
610d936694fSAlan Cox 				 * shadow is dead.  In that case, object will
611d936694fSAlan Cox 				 * be deallocated by the thread that is
612d936694fSAlan Cox 				 * deallocating its shadow.
613d936694fSAlan Cox 				 */
614d936694fSAlan Cox 				if ((robject->flags & OBJ_DEAD) == 0 &&
615d936694fSAlan Cox 				    (robject->handle == NULL) &&
61624a1cce3SDavid Greenman 				    (robject->type == OBJT_DEFAULT ||
61724a1cce3SDavid Greenman 				     robject->type == OBJT_SWAP)) {
618a1f6d91cSDavid Greenman 
61995e5e988SJohn Dyson 					robject->ref_count++;
620138449dcSAlan Cox retry:
621138449dcSAlan Cox 					if (robject->paging_in_progress) {
62289f6b863SAttilio Rao 						VM_OBJECT_WUNLOCK(object);
623138449dcSAlan Cox 						vm_object_pip_wait(robject,
624138449dcSAlan Cox 						    "objde1");
6252e9f4a69SAlan Cox 						temp = robject->backing_object;
6262e9f4a69SAlan Cox 						if (object == temp) {
62789f6b863SAttilio Rao 							VM_OBJECT_WLOCK(object);
628138449dcSAlan Cox 							goto retry;
6292e9f4a69SAlan Cox 						}
630138449dcSAlan Cox 					} else if (object->paging_in_progress) {
63189f6b863SAttilio Rao 						VM_OBJECT_WUNLOCK(robject);
632138449dcSAlan Cox 						object->flags |= OBJ_PIPWNT;
6330dde287bSAttilio Rao 						VM_OBJECT_SLEEP(object, object,
634138449dcSAlan Cox 						    PDROP | PVM, "objde2", 0);
63589f6b863SAttilio Rao 						VM_OBJECT_WLOCK(robject);
6362e9f4a69SAlan Cox 						temp = robject->backing_object;
6372e9f4a69SAlan Cox 						if (object == temp) {
63889f6b863SAttilio Rao 							VM_OBJECT_WLOCK(object);
639138449dcSAlan Cox 							goto retry;
640a1f6d91cSDavid Greenman 						}
6412e9f4a69SAlan Cox 					} else
64289f6b863SAttilio Rao 						VM_OBJECT_WUNLOCK(object);
6432e9f4a69SAlan Cox 
64495e5e988SJohn Dyson 					if (robject->ref_count == 1) {
64595e5e988SJohn Dyson 						robject->ref_count--;
646ba8da839SDavid Greenman 						object = robject;
64795e5e988SJohn Dyson 						goto doterm;
64895e5e988SJohn Dyson 					}
64995e5e988SJohn Dyson 					object = robject;
65095e5e988SJohn Dyson 					vm_object_collapse(object);
65189f6b863SAttilio Rao 					VM_OBJECT_WUNLOCK(object);
652ba8da839SDavid Greenman 					continue;
653a1f6d91cSDavid Greenman 				}
65489f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(robject);
65595e5e988SJohn Dyson 			}
65689f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
65723b186d3SAlan Cox 			return;
65895e5e988SJohn Dyson 		}
65995e5e988SJohn Dyson doterm:
6601bdbd705SKonstantin Belousov 		umtx_shm_object_terminated(object);
66124a1cce3SDavid Greenman 		temp = object->backing_object;
662c9917419SAlan Cox 		if (temp != NULL) {
6634bace8e7SKonstantin Belousov 			KASSERT((object->flags & OBJ_TMPFS_NODE) == 0,
6644bace8e7SKonstantin Belousov 			    ("shadowed tmpfs v_object 2 %p", object));
66589f6b863SAttilio Rao 			VM_OBJECT_WLOCK(temp);
6661c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
66795e5e988SJohn Dyson 			temp->shadow_count--;
66889f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(temp);
66995461b45SJohn Dyson 			object->backing_object = NULL;
670de5f6a77SJohn Dyson 		}
671245df27cSMatthew Dillon 		/*
672245df27cSMatthew Dillon 		 * Don't double-terminate, we could be in a termination
673245df27cSMatthew Dillon 		 * recursion due to the terminate having to sync data
674245df27cSMatthew Dillon 		 * to disk.
675245df27cSMatthew Dillon 		 */
676245df27cSMatthew Dillon 		if ((object->flags & OBJ_DEAD) == 0)
677df8bae1dSRodney W. Grimes 			vm_object_terminate(object);
678c829b9d0SAlan Cox 		else
67989f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
680df8bae1dSRodney W. Grimes 		object = temp;
681df8bae1dSRodney W. Grimes 	}
682df8bae1dSRodney W. Grimes }
683df8bae1dSRodney W. Grimes 
684df8bae1dSRodney W. Grimes /*
6852ac78f0eSStephan Uphoff  *	vm_object_destroy removes the object from the global object list
6862ac78f0eSStephan Uphoff  *      and frees the space for the object.
6872ac78f0eSStephan Uphoff  */
6882ac78f0eSStephan Uphoff void
6892ac78f0eSStephan Uphoff vm_object_destroy(vm_object_t object)
6902ac78f0eSStephan Uphoff {
6912ac78f0eSStephan Uphoff 
6922ac78f0eSStephan Uphoff 	/*
6933364c323SKonstantin Belousov 	 * Release the allocation charge.
6943364c323SKonstantin Belousov 	 */
695ef694c1aSEdward Tomasz Napierala 	if (object->cred != NULL) {
696ef694c1aSEdward Tomasz Napierala 		swap_release_by_cred(object->charge, object->cred);
6973364c323SKonstantin Belousov 		object->charge = 0;
698ef694c1aSEdward Tomasz Napierala 		crfree(object->cred);
699ef694c1aSEdward Tomasz Napierala 		object->cred = NULL;
7003364c323SKonstantin Belousov 	}
7013364c323SKonstantin Belousov 
7023364c323SKonstantin Belousov 	/*
7032ac78f0eSStephan Uphoff 	 * Free the space for the object.
7042ac78f0eSStephan Uphoff 	 */
7052ac78f0eSStephan Uphoff 	uma_zfree(obj_zone, object);
7062ac78f0eSStephan Uphoff }
7072ac78f0eSStephan Uphoff 
7082ac78f0eSStephan Uphoff /*
7097bbdb843SRuslan Bukin  *	vm_object_terminate_pages removes any remaining pageable pages
7107bbdb843SRuslan Bukin  *	from the object and resets the object to an empty state.
7117bbdb843SRuslan Bukin  */
7127bbdb843SRuslan Bukin static void
7137bbdb843SRuslan Bukin vm_object_terminate_pages(vm_object_t object)
7147bbdb843SRuslan Bukin {
7157bbdb843SRuslan Bukin 	vm_page_t p, p_next;
716*2fcd1ff6SKonstantin Belousov 	struct mtx *mtx, *mtx1;
717*2fcd1ff6SKonstantin Belousov 	struct vm_pagequeue *pq, *pq1;
7187bbdb843SRuslan Bukin 
7197bbdb843SRuslan Bukin 	VM_OBJECT_ASSERT_WLOCKED(object);
7207bbdb843SRuslan Bukin 
721*2fcd1ff6SKonstantin Belousov 	mtx = NULL;
722*2fcd1ff6SKonstantin Belousov 	pq = NULL;
723*2fcd1ff6SKonstantin Belousov 
7247bbdb843SRuslan Bukin 	/*
7257bbdb843SRuslan Bukin 	 * Free any remaining pageable pages.  This also removes them from the
7267bbdb843SRuslan Bukin 	 * paging queues.  However, don't free wired pages, just remove them
7277bbdb843SRuslan Bukin 	 * from the object.  Rather than incrementally removing each page from
7287bbdb843SRuslan Bukin 	 * the object, the page and object are reset to any empty state.
7297bbdb843SRuslan Bukin 	 */
7307bbdb843SRuslan Bukin 	TAILQ_FOREACH_SAFE(p, &object->memq, listq, p_next) {
7317bbdb843SRuslan Bukin 		vm_page_assert_unbusied(p);
732*2fcd1ff6SKonstantin Belousov 		if ((object->flags & OBJ_UNMANAGED) == 0) {
7337bbdb843SRuslan Bukin 			/*
734*2fcd1ff6SKonstantin Belousov 			 * vm_page_free_prep() only needs the page
735*2fcd1ff6SKonstantin Belousov 			 * lock for managed pages.
7367bbdb843SRuslan Bukin 			 */
737*2fcd1ff6SKonstantin Belousov 			mtx1 = vm_page_lockptr(p);
738*2fcd1ff6SKonstantin Belousov 			if (mtx1 != mtx) {
739*2fcd1ff6SKonstantin Belousov 				if (mtx != NULL)
740*2fcd1ff6SKonstantin Belousov 					mtx_unlock(mtx);
741*2fcd1ff6SKonstantin Belousov 				if (pq != NULL) {
742*2fcd1ff6SKonstantin Belousov 					vm_pagequeue_unlock(pq);
743*2fcd1ff6SKonstantin Belousov 					pq = NULL;
744*2fcd1ff6SKonstantin Belousov 				}
745*2fcd1ff6SKonstantin Belousov 				mtx = mtx1;
746*2fcd1ff6SKonstantin Belousov 				mtx_lock(mtx);
747*2fcd1ff6SKonstantin Belousov 			}
748*2fcd1ff6SKonstantin Belousov 		}
7497bbdb843SRuslan Bukin 		p->object = NULL;
750*2fcd1ff6SKonstantin Belousov 		if (p->wire_count != 0)
751*2fcd1ff6SKonstantin Belousov 			goto unlist;
7527bbdb843SRuslan Bukin 		VM_CNT_INC(v_pfree);
753*2fcd1ff6SKonstantin Belousov 		p->flags &= ~PG_ZERO;
754*2fcd1ff6SKonstantin Belousov 		if (p->queue != PQ_NONE) {
755*2fcd1ff6SKonstantin Belousov 			KASSERT(p->queue < PQ_COUNT, ("vm_object_terminate: "
756*2fcd1ff6SKonstantin Belousov 			    "page %p is not queued", p));
757*2fcd1ff6SKonstantin Belousov 			pq1 = vm_page_pagequeue(p);
758*2fcd1ff6SKonstantin Belousov 			if (pq != pq1) {
759*2fcd1ff6SKonstantin Belousov 				if (pq != NULL)
760*2fcd1ff6SKonstantin Belousov 					vm_pagequeue_unlock(pq);
761*2fcd1ff6SKonstantin Belousov 				pq = pq1;
762*2fcd1ff6SKonstantin Belousov 				vm_pagequeue_lock(pq);
7637bbdb843SRuslan Bukin 			}
7647bbdb843SRuslan Bukin 		}
765*2fcd1ff6SKonstantin Belousov 		if (vm_page_free_prep(p, true))
766*2fcd1ff6SKonstantin Belousov 			continue;
767*2fcd1ff6SKonstantin Belousov unlist:
768*2fcd1ff6SKonstantin Belousov 		TAILQ_REMOVE(&object->memq, p, listq);
769*2fcd1ff6SKonstantin Belousov 	}
770*2fcd1ff6SKonstantin Belousov 	if (pq != NULL)
771*2fcd1ff6SKonstantin Belousov 		vm_pagequeue_unlock(pq);
772*2fcd1ff6SKonstantin Belousov 	if (mtx != NULL)
773*2fcd1ff6SKonstantin Belousov 		mtx_unlock(mtx);
774*2fcd1ff6SKonstantin Belousov 
775*2fcd1ff6SKonstantin Belousov 	vm_page_free_phys_pglist(&object->memq);
776*2fcd1ff6SKonstantin Belousov 
7777bbdb843SRuslan Bukin 	/*
7787bbdb843SRuslan Bukin 	 * If the object contained any pages, then reset it to an empty state.
7797bbdb843SRuslan Bukin 	 * None of the object's fields, including "resident_page_count", were
7807bbdb843SRuslan Bukin 	 * modified by the preceding loop.
7817bbdb843SRuslan Bukin 	 */
7827bbdb843SRuslan Bukin 	if (object->resident_page_count != 0) {
7837bbdb843SRuslan Bukin 		vm_radix_reclaim_allnodes(&object->rtree);
7847bbdb843SRuslan Bukin 		TAILQ_INIT(&object->memq);
7857bbdb843SRuslan Bukin 		object->resident_page_count = 0;
7867bbdb843SRuslan Bukin 		if (object->type == OBJT_VNODE)
7877bbdb843SRuslan Bukin 			vdrop(object->handle);
7887bbdb843SRuslan Bukin 	}
7897bbdb843SRuslan Bukin }
7907bbdb843SRuslan Bukin 
7917bbdb843SRuslan Bukin /*
792df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
793df8bae1dSRodney W. Grimes  *	up all previously used resources.
794df8bae1dSRodney W. Grimes  *
795df8bae1dSRodney W. Grimes  *	The object must be locked.
7961c7c3c6aSMatthew Dillon  *	This routine may block.
797df8bae1dSRodney W. Grimes  */
79895e5e988SJohn Dyson void
7991b40f8c0SMatthew Dillon vm_object_terminate(vm_object_t object)
800df8bae1dSRodney W. Grimes {
801df8bae1dSRodney W. Grimes 
80289f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
8030cddd8f0SMatthew Dillon 
80495e5e988SJohn Dyson 	/*
80595e5e988SJohn Dyson 	 * Make sure no one uses us.
80695e5e988SJohn Dyson 	 */
807069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_DEAD);
8083c631446SJohn Dyson 
809df8bae1dSRodney W. Grimes 	/*
810f6b04d2bSDavid Greenman 	 * wait for the pageout daemon to be done with the object
811df8bae1dSRodney W. Grimes 	 */
81266095752SJohn Dyson 	vm_object_pip_wait(object, "objtrm");
813df8bae1dSRodney W. Grimes 
8145526d2d9SEivind Eklund 	KASSERT(!object->paging_in_progress,
8155526d2d9SEivind Eklund 		("vm_object_terminate: pageout in progress"));
81626f9a767SRodney W. Grimes 
81726f9a767SRodney W. Grimes 	/*
8180d94caffSDavid Greenman 	 * Clean and free the pages, as appropriate. All references to the
8190d94caffSDavid Greenman 	 * object are gone, so we don't need to lock it.
82026f9a767SRodney W. Grimes 	 */
82124a1cce3SDavid Greenman 	if (object->type == OBJT_VNODE) {
822f7dd7b63SAlan Cox 		struct vnode *vp = (struct vnode *)object->handle;
82395e5e988SJohn Dyson 
82495e5e988SJohn Dyson 		/*
82595e5e988SJohn Dyson 		 * Clean pages and flush buffers.
82695e5e988SJohn Dyson 		 */
8278f9110f6SJohn Dyson 		vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
82889f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
82995e5e988SJohn Dyson 
8300d7935fdSAttilio Rao 		vinvalbuf(vp, V_SAVE, 0, 0);
831f7dd7b63SAlan Cox 
83219efd8a5SKonstantin Belousov 		BO_LOCK(&vp->v_bufobj);
83319efd8a5SKonstantin Belousov 		vp->v_bufobj.bo_flag |= BO_DEAD;
83419efd8a5SKonstantin Belousov 		BO_UNLOCK(&vp->v_bufobj);
83519efd8a5SKonstantin Belousov 
83689f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
837bef608bdSJohn Dyson 	}
838bef608bdSJohn Dyson 
839971dd342SAlfred Perlstein 	KASSERT(object->ref_count == 0,
840971dd342SAlfred Perlstein 		("vm_object_terminate: object with references, ref_count=%d",
841971dd342SAlfred Perlstein 		object->ref_count));
842996c772fSJohn Dyson 
8437bbdb843SRuslan Bukin 	if ((object->flags & OBJ_PG_DTOR) == 0)
8447bbdb843SRuslan Bukin 		vm_object_terminate_pages(object);
845bef608bdSJohn Dyson 
846f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
847f8a47341SAlan Cox 	if (__predict_false(!LIST_EMPTY(&object->rvq)))
848f8a47341SAlan Cox 		vm_reserv_break_all(object);
849f8a47341SAlan Cox #endif
8507bfda801SAlan Cox 
851e735691bSJohn Baldwin 	KASSERT(object->cred == NULL || object->type == OBJT_DEFAULT ||
852e735691bSJohn Baldwin 	    object->type == OBJT_SWAP,
853e735691bSJohn Baldwin 	    ("%s: non-swap obj %p has cred", __func__, object));
854e735691bSJohn Baldwin 
8552d8acc0fSJohn Dyson 	/*
8569fcfb650SDavid Greenman 	 * Let the pager know object is dead.
8579fcfb650SDavid Greenman 	 */
8589fcfb650SDavid Greenman 	vm_pager_deallocate(object);
85989f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
8609fcfb650SDavid Greenman 
8612ac78f0eSStephan Uphoff 	vm_object_destroy(object);
86247221757SJohn Dyson }
863df8bae1dSRodney W. Grimes 
864edf93b25SAlan Cox /*
865edf93b25SAlan Cox  * Make the page read-only so that we can clear the object flags.  However, if
866edf93b25SAlan Cox  * this is a nosync mmap then the object is likely to stay dirty so do not
867edf93b25SAlan Cox  * mess with the page and do not clear the object flags.  Returns TRUE if the
868edf93b25SAlan Cox  * page should be flushed, and FALSE otherwise.
869edf93b25SAlan Cox  */
8703280870dSKonstantin Belousov static boolean_t
871126d6082SKonstantin Belousov vm_object_page_remove_write(vm_page_t p, int flags, boolean_t *clearobjflags)
8723280870dSKonstantin Belousov {
8733280870dSKonstantin Belousov 
8743280870dSKonstantin Belousov 	/*
8753280870dSKonstantin Belousov 	 * If we have been asked to skip nosync pages and this is a
8763280870dSKonstantin Belousov 	 * nosync page, skip it.  Note that the object flags were not
8773280870dSKonstantin Belousov 	 * cleared in this case so we do not have to set them.
8783280870dSKonstantin Belousov 	 */
8793280870dSKonstantin Belousov 	if ((flags & OBJPC_NOSYNC) != 0 && (p->oflags & VPO_NOSYNC) != 0) {
880126d6082SKonstantin Belousov 		*clearobjflags = FALSE;
8813280870dSKonstantin Belousov 		return (FALSE);
8823280870dSKonstantin Belousov 	} else {
8833280870dSKonstantin Belousov 		pmap_remove_write(p);
8843280870dSKonstantin Belousov 		return (p->dirty != 0);
8853280870dSKonstantin Belousov 	}
8863280870dSKonstantin Belousov }
8873280870dSKonstantin Belousov 
888df8bae1dSRodney W. Grimes /*
889df8bae1dSRodney W. Grimes  *	vm_object_page_clean
890df8bae1dSRodney W. Grimes  *
8914f79d873SMatthew Dillon  *	Clean all dirty pages in the specified range of object.  Leaves page
8924f79d873SMatthew Dillon  * 	on whatever queue it is currently on.   If NOSYNC is set then do not
893b146f9e5SAlan Cox  *	write out pages with VPO_NOSYNC set (originally comes from MAP_NOSYNC),
8944f79d873SMatthew Dillon  *	leaving the object dirty.
89526f9a767SRodney W. Grimes  *
89643b7990eSMatthew Dillon  *	When stuffing pages asynchronously, allow clustering.  XXX we need a
89743b7990eSMatthew Dillon  *	synchronous clustering mode implementation.
89843b7990eSMatthew Dillon  *
89926f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
90026f9a767SRodney W. Grimes  *
90126f9a767SRodney W. Grimes  *	The object must be locked.
902126d6082SKonstantin Belousov  *
903126d6082SKonstantin Belousov  *	Returns FALSE if some page from the range was not written, as
904126d6082SKonstantin Belousov  *	reported by the pager, and TRUE otherwise.
90526f9a767SRodney W. Grimes  */
906126d6082SKonstantin Belousov boolean_t
90717f3095dSAlan Cox vm_object_page_clean(vm_object_t object, vm_ooffset_t start, vm_ooffset_t end,
908e239bb97SKonstantin Belousov     int flags)
909f6b04d2bSDavid Greenman {
910e239bb97SKonstantin Belousov 	vm_page_t np, p;
91117f3095dSAlan Cox 	vm_pindex_t pi, tend, tstart;
912126d6082SKonstantin Belousov 	int curgeneration, n, pagerflags;
913126d6082SKonstantin Belousov 	boolean_t clearobjflags, eio, res;
914f6b04d2bSDavid Greenman 
91589f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
916e5f299ffSKonstantin Belousov 
917e5f299ffSKonstantin Belousov 	/*
918e5f299ffSKonstantin Belousov 	 * The OBJ_MIGHTBEDIRTY flag is only set for OBJT_VNODE
919e5f299ffSKonstantin Belousov 	 * objects.  The check below prevents the function from
920e5f299ffSKonstantin Belousov 	 * operating on non-vnode objects.
921e5f299ffSKonstantin Belousov 	 */
922e239bb97SKonstantin Belousov 	if ((object->flags & OBJ_MIGHTBEDIRTY) == 0 ||
923e239bb97SKonstantin Belousov 	    object->resident_page_count == 0)
924126d6082SKonstantin Belousov 		return (TRUE);
925f6b04d2bSDavid Greenman 
926e239bb97SKonstantin Belousov 	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) != 0 ?
927e239bb97SKonstantin Belousov 	    VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
928e239bb97SKonstantin Belousov 	pagerflags |= (flags & OBJPC_INVAL) != 0 ? VM_PAGER_PUT_INVAL : 0;
929e239bb97SKonstantin Belousov 
93017f3095dSAlan Cox 	tstart = OFF_TO_IDX(start);
93117f3095dSAlan Cox 	tend = (end == 0) ? object->size : OFF_TO_IDX(end + PAGE_MASK);
93217f3095dSAlan Cox 	clearobjflags = tstart == 0 && tend >= object->size;
933126d6082SKonstantin Belousov 	res = TRUE;
934f6b04d2bSDavid Greenman 
935bd7e5f99SJohn Dyson rescan:
9362d8acc0fSJohn Dyson 	curgeneration = object->generation;
9372d8acc0fSJohn Dyson 
93817f3095dSAlan Cox 	for (p = vm_page_find_least(object, tstart); p != NULL; p = np) {
939bd7e5f99SJohn Dyson 		pi = p->pindex;
940e239bb97SKonstantin Belousov 		if (pi >= tend)
941e239bb97SKonstantin Belousov 			break;
942e239bb97SKonstantin Belousov 		np = TAILQ_NEXT(p, listq);
943e239bb97SKonstantin Belousov 		if (p->valid == 0)
944aef922f5SJohn Dyson 			continue;
945c7aebda8SAttilio Rao 		if (vm_page_sleep_if_busy(p, "vpcwai")) {
946e65919f9SKonstantin Belousov 			if (object->generation != curgeneration) {
947e65919f9SKonstantin Belousov 				if ((flags & OBJPC_SYNC) != 0)
948e239bb97SKonstantin Belousov 					goto rescan;
949e65919f9SKonstantin Belousov 				else
950126d6082SKonstantin Belousov 					clearobjflags = FALSE;
951e65919f9SKonstantin Belousov 			}
952780636b7SKonstantin Belousov 			np = vm_page_find_least(object, pi);
953780636b7SKonstantin Belousov 			continue;
954f6b04d2bSDavid Greenman 		}
9553280870dSKonstantin Belousov 		if (!vm_object_page_remove_write(p, flags, &clearobjflags))
956bd7e5f99SJohn Dyson 			continue;
957e239bb97SKonstantin Belousov 
9583280870dSKonstantin Belousov 		n = vm_object_page_collect_flush(object, p, pagerflags,
959126d6082SKonstantin Belousov 		    flags, &clearobjflags, &eio);
960126d6082SKonstantin Belousov 		if (eio) {
961126d6082SKonstantin Belousov 			res = FALSE;
962126d6082SKonstantin Belousov 			clearobjflags = FALSE;
963126d6082SKonstantin Belousov 		}
964e65919f9SKonstantin Belousov 		if (object->generation != curgeneration) {
965e65919f9SKonstantin Belousov 			if ((flags & OBJPC_SYNC) != 0)
966b9b7a4beSMatthew Dillon 				goto rescan;
967e65919f9SKonstantin Belousov 			else
968126d6082SKonstantin Belousov 				clearobjflags = FALSE;
969e65919f9SKonstantin Belousov 		}
970031ec8c1SKonstantin Belousov 
971031ec8c1SKonstantin Belousov 		/*
972031ec8c1SKonstantin Belousov 		 * If the VOP_PUTPAGES() did a truncated write, so
973031ec8c1SKonstantin Belousov 		 * that even the first page of the run is not fully
974031ec8c1SKonstantin Belousov 		 * written, vm_pageout_flush() returns 0 as the run
975031ec8c1SKonstantin Belousov 		 * length.  Since the condition that caused truncated
976031ec8c1SKonstantin Belousov 		 * write may be permanent, e.g. exhausted free space,
977031ec8c1SKonstantin Belousov 		 * accepting n == 0 would cause an infinite loop.
978031ec8c1SKonstantin Belousov 		 *
979031ec8c1SKonstantin Belousov 		 * Forwarding the iterator leaves the unwritten page
980031ec8c1SKonstantin Belousov 		 * behind, but there is not much we can do there if
981031ec8c1SKonstantin Belousov 		 * filesystem refuses to write it.
982031ec8c1SKonstantin Belousov 		 */
983126d6082SKonstantin Belousov 		if (n == 0) {
984031ec8c1SKonstantin Belousov 			n = 1;
985126d6082SKonstantin Belousov 			clearobjflags = FALSE;
986126d6082SKonstantin Belousov 		}
987e239bb97SKonstantin Belousov 		np = vm_page_find_least(object, pi + n);
988b9b7a4beSMatthew Dillon 	}
989b9b7a4beSMatthew Dillon #if 0
990e239bb97SKonstantin Belousov 	VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC) ? MNT_WAIT : 0);
991b9b7a4beSMatthew Dillon #endif
992b9b7a4beSMatthew Dillon 
993edf93b25SAlan Cox 	if (clearobjflags)
9943280870dSKonstantin Belousov 		vm_object_clear_flag(object, OBJ_MIGHTBEDIRTY);
995126d6082SKonstantin Belousov 	return (res);
996b9b7a4beSMatthew Dillon }
997b9b7a4beSMatthew Dillon 
998b9b7a4beSMatthew Dillon static int
9993280870dSKonstantin Belousov vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int pagerflags,
1000126d6082SKonstantin Belousov     int flags, boolean_t *clearobjflags, boolean_t *eio)
1001b9b7a4beSMatthew Dillon {
10023157c503SKonstantin Belousov 	vm_page_t ma[vm_pageout_page_count], p_first, tp;
10033157c503SKonstantin Belousov 	int count, i, mreq, runlen;
1004b9b7a4beSMatthew Dillon 
10057bec141bSKip Macy 	vm_page_lock_assert(p, MA_NOTOWNED);
100689f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
10073157c503SKonstantin Belousov 
10083157c503SKonstantin Belousov 	count = 1;
10093157c503SKonstantin Belousov 	mreq = 0;
10103157c503SKonstantin Belousov 
10113157c503SKonstantin Belousov 	for (tp = p; count < vm_pageout_page_count; count++) {
10123157c503SKonstantin Belousov 		tp = vm_page_next(tp);
1013c7aebda8SAttilio Rao 		if (tp == NULL || vm_page_busied(tp))
1014bd7e5f99SJohn Dyson 			break;
10153280870dSKonstantin Belousov 		if (!vm_object_page_remove_write(tp, flags, clearobjflags))
1016bd7e5f99SJohn Dyson 			break;
1017bd7e5f99SJohn Dyson 	}
1018aef922f5SJohn Dyson 
10193157c503SKonstantin Belousov 	for (p_first = p; count < vm_pageout_page_count; count++) {
10203157c503SKonstantin Belousov 		tp = vm_page_prev(p_first);
1021c7aebda8SAttilio Rao 		if (tp == NULL || vm_page_busied(tp))
1022bd7e5f99SJohn Dyson 			break;
10233280870dSKonstantin Belousov 		if (!vm_object_page_remove_write(tp, flags, clearobjflags))
1024bd7e5f99SJohn Dyson 			break;
10253157c503SKonstantin Belousov 		p_first = tp;
10263157c503SKonstantin Belousov 		mreq++;
1027bd7e5f99SJohn Dyson 	}
1028bd7e5f99SJohn Dyson 
10293157c503SKonstantin Belousov 	for (tp = p_first, i = 0; i < count; tp = TAILQ_NEXT(tp, listq), i++)
10303157c503SKonstantin Belousov 		ma[i] = tp;
1031cf2819ccSJohn Dyson 
1032126d6082SKonstantin Belousov 	vm_pageout_flush(ma, count, pagerflags, mreq, &runlen, eio);
10331e8a675cSKonstantin Belousov 	return (runlen);
103426f9a767SRodney W. Grimes }
1035df8bae1dSRodney W. Grimes 
10361efb74fbSJohn Dyson /*
1037950f8459SAlan Cox  * Note that there is absolutely no sense in writing out
1038950f8459SAlan Cox  * anonymous objects, so we track down the vnode object
1039950f8459SAlan Cox  * to write out.
1040950f8459SAlan Cox  * We invalidate (remove) all pages from the address space
1041950f8459SAlan Cox  * for semantic correctness.
1042950f8459SAlan Cox  *
10436bbee8e2SAlan Cox  * If the backing object is a device object with unmanaged pages, then any
10446bbee8e2SAlan Cox  * mappings to the specified range of pages must be removed before this
10456bbee8e2SAlan Cox  * function is called.
10466bbee8e2SAlan Cox  *
1047950f8459SAlan Cox  * Note: certain anonymous maps, such as MAP_NOSYNC maps,
1048950f8459SAlan Cox  * may start out with a NULL object.
1049950f8459SAlan Cox  */
1050126d6082SKonstantin Belousov boolean_t
1051950f8459SAlan Cox vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size,
1052950f8459SAlan Cox     boolean_t syncio, boolean_t invalidate)
1053950f8459SAlan Cox {
1054950f8459SAlan Cox 	vm_object_t backing_object;
1055950f8459SAlan Cox 	struct vnode *vp;
10563b582b4eSTor Egge 	struct mount *mp;
1057126d6082SKonstantin Belousov 	int error, flags, fsync_after;
1058126d6082SKonstantin Belousov 	boolean_t res;
1059950f8459SAlan Cox 
1060950f8459SAlan Cox 	if (object == NULL)
1061126d6082SKonstantin Belousov 		return (TRUE);
1062126d6082SKonstantin Belousov 	res = TRUE;
1063126d6082SKonstantin Belousov 	error = 0;
106489f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
1065950f8459SAlan Cox 	while ((backing_object = object->backing_object) != NULL) {
106689f6b863SAttilio Rao 		VM_OBJECT_WLOCK(backing_object);
106756e0670fSAlan Cox 		offset += object->backing_object_offset;
106889f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
1069950f8459SAlan Cox 		object = backing_object;
1070950f8459SAlan Cox 		if (object->size < OFF_TO_IDX(offset + size))
1071950f8459SAlan Cox 			size = IDX_TO_OFF(object->size) - offset;
1072950f8459SAlan Cox 	}
1073950f8459SAlan Cox 	/*
1074950f8459SAlan Cox 	 * Flush pages if writing is allowed, invalidate them
1075950f8459SAlan Cox 	 * if invalidation requested.  Pages undergoing I/O
1076950f8459SAlan Cox 	 * will be ignored by vm_object_page_remove().
1077950f8459SAlan Cox 	 *
1078950f8459SAlan Cox 	 * We cannot lock the vnode and then wait for paging
1079950f8459SAlan Cox 	 * to complete without deadlocking against vm_fault.
1080950f8459SAlan Cox 	 * Instead we simply call vm_object_page_remove() and
1081950f8459SAlan Cox 	 * allow it to block internally on a page-by-page
1082950f8459SAlan Cox 	 * basis when it encounters pages undergoing async
1083950f8459SAlan Cox 	 * I/O.
1084950f8459SAlan Cox 	 */
1085950f8459SAlan Cox 	if (object->type == OBJT_VNODE &&
1086950f8459SAlan Cox 	    (object->flags & OBJ_MIGHTBEDIRTY) != 0) {
1087950f8459SAlan Cox 		vp = object->handle;
108889f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
10893b582b4eSTor Egge 		(void) vn_start_write(vp, &mp, V_WAIT);
1090cb05b60aSAttilio Rao 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
109175ff604aSKonstantin Belousov 		if (syncio && !invalidate && offset == 0 &&
1092d1780e8dSKonstantin Belousov 		    atop(size) == object->size) {
109375ff604aSKonstantin Belousov 			/*
109475ff604aSKonstantin Belousov 			 * If syncing the whole mapping of the file,
109575ff604aSKonstantin Belousov 			 * it is faster to schedule all the writes in
109675ff604aSKonstantin Belousov 			 * async mode, also allowing the clustering,
109775ff604aSKonstantin Belousov 			 * and then wait for i/o to complete.
109875ff604aSKonstantin Belousov 			 */
109975ff604aSKonstantin Belousov 			flags = 0;
110075ff604aSKonstantin Belousov 			fsync_after = TRUE;
110175ff604aSKonstantin Belousov 		} else {
1102950f8459SAlan Cox 			flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
110375ff604aSKonstantin Belousov 			flags |= invalidate ? (OBJPC_SYNC | OBJPC_INVAL) : 0;
110475ff604aSKonstantin Belousov 			fsync_after = FALSE;
110575ff604aSKonstantin Belousov 		}
110689f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
1107126d6082SKonstantin Belousov 		res = vm_object_page_clean(object, offset, offset + size,
1108126d6082SKonstantin Belousov 		    flags);
110989f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
111075ff604aSKonstantin Belousov 		if (fsync_after)
1111126d6082SKonstantin Belousov 			error = VOP_FSYNC(vp, MNT_WAIT, curthread);
111222db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
11133b582b4eSTor Egge 		vn_finished_write(mp);
1114126d6082SKonstantin Belousov 		if (error != 0)
1115126d6082SKonstantin Belousov 			res = FALSE;
111689f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
1117950f8459SAlan Cox 	}
1118950f8459SAlan Cox 	if ((object->type == OBJT_VNODE ||
1119950f8459SAlan Cox 	     object->type == OBJT_DEVICE) && invalidate) {
11206bbee8e2SAlan Cox 		if (object->type == OBJT_DEVICE)
11216bbee8e2SAlan Cox 			/*
11226bbee8e2SAlan Cox 			 * The option OBJPR_NOTMAPPED must be passed here
11236bbee8e2SAlan Cox 			 * because vm_object_page_remove() cannot remove
11246bbee8e2SAlan Cox 			 * unmanaged mappings.
11256bbee8e2SAlan Cox 			 */
11266bbee8e2SAlan Cox 			flags = OBJPR_NOTMAPPED;
11276bbee8e2SAlan Cox 		else if (old_msync)
11286195b24aSKonstantin Belousov 			flags = 0;
11296bbee8e2SAlan Cox 		else
11306195b24aSKonstantin Belousov 			flags = OBJPR_CLEANONLY;
11316bbee8e2SAlan Cox 		vm_object_page_remove(object, OFF_TO_IDX(offset),
11326bbee8e2SAlan Cox 		    OFF_TO_IDX(offset + size + PAGE_MASK), flags);
1133950f8459SAlan Cox 	}
113489f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
1135126d6082SKonstantin Belousov 	return (res);
1136950f8459SAlan Cox }
1137950f8459SAlan Cox 
1138950f8459SAlan Cox /*
1139aa3650eaSMark Johnston  * Determine whether the given advice can be applied to the object.  Advice is
1140aa3650eaSMark Johnston  * not applied to unmanaged pages since they never belong to page queues, and
1141aa3650eaSMark Johnston  * since MADV_FREE is destructive, it can apply only to anonymous pages that
1142aa3650eaSMark Johnston  * have been mapped at most once.
1143aa3650eaSMark Johnston  */
1144aa3650eaSMark Johnston static bool
1145aa3650eaSMark Johnston vm_object_advice_applies(vm_object_t object, int advice)
1146aa3650eaSMark Johnston {
1147aa3650eaSMark Johnston 
1148aa3650eaSMark Johnston 	if ((object->flags & OBJ_UNMANAGED) != 0)
1149aa3650eaSMark Johnston 		return (false);
1150aa3650eaSMark Johnston 	if (advice != MADV_FREE)
1151aa3650eaSMark Johnston 		return (true);
1152aa3650eaSMark Johnston 	return ((object->type == OBJT_DEFAULT || object->type == OBJT_SWAP) &&
1153aa3650eaSMark Johnston 	    (object->flags & OBJ_ONEMAPPING) != 0);
1154aa3650eaSMark Johnston }
1155aa3650eaSMark Johnston 
1156aa3650eaSMark Johnston static void
1157aa3650eaSMark Johnston vm_object_madvise_freespace(vm_object_t object, int advice, vm_pindex_t pindex,
1158aa3650eaSMark Johnston     vm_size_t size)
1159aa3650eaSMark Johnston {
1160aa3650eaSMark Johnston 
1161aa3650eaSMark Johnston 	if (advice == MADV_FREE && object->type == OBJT_SWAP)
1162aa3650eaSMark Johnston 		swap_pager_freespace(object, pindex, size);
1163aa3650eaSMark Johnston }
1164aa3650eaSMark Johnston 
1165aa3650eaSMark Johnston /*
1166867a482dSJohn Dyson  *	vm_object_madvise:
1167867a482dSJohn Dyson  *
1168867a482dSJohn Dyson  *	Implements the madvise function at the object/page level.
11691c7c3c6aSMatthew Dillon  *
1170193b9358SAlan Cox  *	MADV_WILLNEED	(any object)
1171193b9358SAlan Cox  *
1172193b9358SAlan Cox  *	    Activate the specified pages if they are resident.
1173193b9358SAlan Cox  *
1174193b9358SAlan Cox  *	MADV_DONTNEED	(any object)
1175193b9358SAlan Cox  *
1176193b9358SAlan Cox  *	    Deactivate the specified pages if they are resident.
1177193b9358SAlan Cox  *
1178193b9358SAlan Cox  *	MADV_FREE	(OBJT_DEFAULT/OBJT_SWAP objects,
1179193b9358SAlan Cox  *			 OBJ_ONEMAPPING only)
1180193b9358SAlan Cox  *
1181193b9358SAlan Cox  *	    Deactivate and clean the specified pages if they are
1182193b9358SAlan Cox  *	    resident.  This permits the process to reuse the pages
1183193b9358SAlan Cox  *	    without faulting or the kernel to reclaim the pages
1184193b9358SAlan Cox  *	    without I/O.
1185867a482dSJohn Dyson  */
1186867a482dSJohn Dyson void
118792a59946SJohn Baldwin vm_object_madvise(vm_object_t object, vm_pindex_t pindex, vm_pindex_t end,
1188c2655a40SMark Johnston     int advice)
1189867a482dSJohn Dyson {
119092a59946SJohn Baldwin 	vm_pindex_t tpindex;
119134567de7SAlan Cox 	vm_object_t backing_object, tobject;
1192aa3650eaSMark Johnston 	vm_page_t m, tm;
1193867a482dSJohn Dyson 
1194867a482dSJohn Dyson 	if (object == NULL)
1195867a482dSJohn Dyson 		return;
1196c2655a40SMark Johnston 
11976e20a165SJohn Dyson relookup:
1198aa3650eaSMark Johnston 	VM_OBJECT_WLOCK(object);
1199aa3650eaSMark Johnston 	if (!vm_object_advice_applies(object, advice)) {
1200aa3650eaSMark Johnston 		VM_OBJECT_WUNLOCK(object);
1201aa3650eaSMark Johnston 		return;
12026e20a165SJohn Dyson 	}
1203aa3650eaSMark Johnston 	for (m = vm_page_find_least(object, pindex); pindex < end; pindex++) {
1204aa3650eaSMark Johnston 		tobject = object;
1205c2655a40SMark Johnston 
12061ce137beSMatthew Dillon 		/*
1207aa3650eaSMark Johnston 		 * If the next page isn't resident in the top-level object, we
1208aa3650eaSMark Johnston 		 * need to search the shadow chain.  When applying MADV_FREE, we
1209aa3650eaSMark Johnston 		 * take care to release any swap space used to store
1210aa3650eaSMark Johnston 		 * non-resident pages.
1211aa3650eaSMark Johnston 		 */
1212aa3650eaSMark Johnston 		if (m == NULL || pindex < m->pindex) {
1213aa3650eaSMark Johnston 			/*
1214aa3650eaSMark Johnston 			 * Optimize a common case: if the top-level object has
1215aa3650eaSMark Johnston 			 * no backing object, we can skip over the non-resident
1216aa3650eaSMark Johnston 			 * range in constant time.
12171ce137beSMatthew Dillon 			 */
1218c2655a40SMark Johnston 			if (object->backing_object == NULL) {
1219c2655a40SMark Johnston 				tpindex = (m != NULL && m->pindex < end) ?
1220c2655a40SMark Johnston 				    m->pindex : end;
1221aa3650eaSMark Johnston 				vm_object_madvise_freespace(object, advice,
1222aa3650eaSMark Johnston 				    pindex, tpindex - pindex);
1223c2655a40SMark Johnston 				if ((pindex = tpindex) == end)
1224c2655a40SMark Johnston 					break;
1225aa3650eaSMark Johnston 				goto next_page;
1226aa3650eaSMark Johnston 			}
1227aa3650eaSMark Johnston 
1228aa3650eaSMark Johnston 			tpindex = pindex;
1229aa3650eaSMark Johnston 			do {
1230aa3650eaSMark Johnston 				vm_object_madvise_freespace(tobject, advice,
1231aa3650eaSMark Johnston 				    tpindex, 1);
12321ce137beSMatthew Dillon 				/*
1233aa3650eaSMark Johnston 				 * Prepare to search the next object in the
1234aa3650eaSMark Johnston 				 * chain.
12351ce137beSMatthew Dillon 				 */
123634567de7SAlan Cox 				backing_object = tobject->backing_object;
123734567de7SAlan Cox 				if (backing_object == NULL)
1238aa3650eaSMark Johnston 					goto next_pindex;
123989f6b863SAttilio Rao 				VM_OBJECT_WLOCK(backing_object);
1240aa3650eaSMark Johnston 				tpindex +=
1241aa3650eaSMark Johnston 				    OFF_TO_IDX(tobject->backing_object_offset);
12429b98b796SAlan Cox 				if (tobject != object)
124389f6b863SAttilio Rao 					VM_OBJECT_WUNLOCK(tobject);
124434567de7SAlan Cox 				tobject = backing_object;
1245aa3650eaSMark Johnston 				if (!vm_object_advice_applies(tobject, advice))
1246aa3650eaSMark Johnston 					goto next_pindex;
1247aa3650eaSMark Johnston 			} while ((tm = vm_page_lookup(tobject, tpindex)) ==
1248aa3650eaSMark Johnston 			    NULL);
1249aa3650eaSMark Johnston 		} else {
1250aa3650eaSMark Johnston next_page:
1251aa3650eaSMark Johnston 			tm = m;
1252aa3650eaSMark Johnston 			m = TAILQ_NEXT(m, listq);
1253c2655a40SMark Johnston 		}
1254c2655a40SMark Johnston 
1255867a482dSJohn Dyson 		/*
12566a2a3d73SAlan Cox 		 * If the page is not in a normal state, skip it.
1257867a482dSJohn Dyson 		 */
1258aa3650eaSMark Johnston 		if (tm->valid != VM_PAGE_BITS_ALL)
1259aa3650eaSMark Johnston 			goto next_pindex;
1260aa3650eaSMark Johnston 		vm_page_lock(tm);
1261aa3650eaSMark Johnston 		if (tm->hold_count != 0 || tm->wire_count != 0) {
1262aa3650eaSMark Johnston 			vm_page_unlock(tm);
1263aa3650eaSMark Johnston 			goto next_pindex;
12646e20a165SJohn Dyson 		}
1265aa3650eaSMark Johnston 		KASSERT((tm->flags & PG_FICTITIOUS) == 0,
1266aa3650eaSMark Johnston 		    ("vm_object_madvise: page %p is fictitious", tm));
1267aa3650eaSMark Johnston 		KASSERT((tm->oflags & VPO_UNMANAGED) == 0,
1268aa3650eaSMark Johnston 		    ("vm_object_madvise: page %p is not managed", tm));
1269aa3650eaSMark Johnston 		if (vm_page_busied(tm)) {
1270aa3650eaSMark Johnston 			if (object != tobject)
1271aa3650eaSMark Johnston 				VM_OBJECT_WUNLOCK(tobject);
1272aa3650eaSMark Johnston 			VM_OBJECT_WUNLOCK(object);
1273c2655a40SMark Johnston 			if (advice == MADV_WILLNEED) {
1274b11b56b5SAlan Cox 				/*
1275b11b56b5SAlan Cox 				 * Reference the page before unlocking and
1276b11b56b5SAlan Cox 				 * sleeping so that the page daemon is less
1277b11b56b5SAlan Cox 				 * likely to reclaim it.
1278b11b56b5SAlan Cox 				 */
1279aa3650eaSMark Johnston 				vm_page_aflag_set(tm, PGA_REFERENCED);
1280567e51e1SAlan Cox 			}
1281aa3650eaSMark Johnston 			vm_page_busy_sleep(tm, "madvpo", false);
12826e20a165SJohn Dyson   			goto relookup;
128334567de7SAlan Cox 		}
1284aa3650eaSMark Johnston 		vm_page_advise(tm, advice);
1285aa3650eaSMark Johnston 		vm_page_unlock(tm);
1286aa3650eaSMark Johnston 		vm_object_madvise_freespace(tobject, advice, tm->pindex, 1);
1287aa3650eaSMark Johnston next_pindex:
12889b98b796SAlan Cox 		if (tobject != object)
128989f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(tobject);
1290867a482dSJohn Dyson 	}
129189f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
1292867a482dSJohn Dyson }
1293867a482dSJohn Dyson 
1294867a482dSJohn Dyson /*
1295df8bae1dSRodney W. Grimes  *	vm_object_shadow:
1296df8bae1dSRodney W. Grimes  *
1297df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
1298df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
1299df8bae1dSRodney W. Grimes  *	object reference is deallocated.
1300df8bae1dSRodney W. Grimes  *
1301df8bae1dSRodney W. Grimes  *	The new object and offset into that object
1302df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
1303df8bae1dSRodney W. Grimes  */
130426f9a767SRodney W. Grimes void
13051b40f8c0SMatthew Dillon vm_object_shadow(
13061b40f8c0SMatthew Dillon 	vm_object_t *object,	/* IN/OUT */
13071b40f8c0SMatthew Dillon 	vm_ooffset_t *offset,	/* IN/OUT */
13081b40f8c0SMatthew Dillon 	vm_size_t length)
1309df8bae1dSRodney W. Grimes {
1310d031cff1SMatthew Dillon 	vm_object_t source;
1311d031cff1SMatthew Dillon 	vm_object_t result;
1312df8bae1dSRodney W. Grimes 
1313df8bae1dSRodney W. Grimes 	source = *object;
1314df8bae1dSRodney W. Grimes 
1315df8bae1dSRodney W. Grimes 	/*
13169a2f6362SAlan Cox 	 * Don't create the new object if the old object isn't shared.
13179a2f6362SAlan Cox 	 */
1318570a2f4aSAlan Cox 	if (source != NULL) {
131989f6b863SAttilio Rao 		VM_OBJECT_WLOCK(source);
1320570a2f4aSAlan Cox 		if (source->ref_count == 1 &&
13219a2f6362SAlan Cox 		    source->handle == NULL &&
13229a2f6362SAlan Cox 		    (source->type == OBJT_DEFAULT ||
13239917e010SAlan Cox 		     source->type == OBJT_SWAP)) {
132489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(source);
13259a2f6362SAlan Cox 			return;
13269917e010SAlan Cox 		}
132789f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(source);
1328570a2f4aSAlan Cox 	}
13299a2f6362SAlan Cox 
13309a2f6362SAlan Cox 	/*
1331570a2f4aSAlan Cox 	 * Allocate a new object with the given length.
1332df8bae1dSRodney W. Grimes 	 */
13330cc74f14SAlan Cox 	result = vm_object_allocate(OBJT_DEFAULT, atop(length));
1334df8bae1dSRodney W. Grimes 
1335df8bae1dSRodney W. Grimes 	/*
13360d94caffSDavid Greenman 	 * The new object shadows the source object, adding a reference to it.
13370d94caffSDavid Greenman 	 * Our caller changes his reference to point to the new object,
13380d94caffSDavid Greenman 	 * removing a reference to the source object.  Net result: no change
13390d94caffSDavid Greenman 	 * of reference count.
13409b09fe24SMatthew Dillon 	 *
13419b09fe24SMatthew Dillon 	 * Try to optimize the result object's page color when shadowing
1342956f3135SPhilippe Charnier 	 * in order to maintain page coloring consistency in the combined
13439b09fe24SMatthew Dillon 	 * shadowed object.
1344df8bae1dSRodney W. Grimes 	 */
134524a1cce3SDavid Greenman 	result->backing_object = source;
13469174ca7bSTor Egge 	/*
13479174ca7bSTor Egge 	 * Store the offset into the source object, and fix up the offset into
13489174ca7bSTor Egge 	 * the new object.
13499174ca7bSTor Egge 	 */
13509174ca7bSTor Egge 	result->backing_object_offset = *offset;
1351570a2f4aSAlan Cox 	if (source != NULL) {
135289f6b863SAttilio Rao 		VM_OBJECT_WLOCK(source);
13531c500307SAlan Cox 		LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1354eaf13dd7SJohn Dyson 		source->shadow_count++;
1355f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
13567b54b1a9SAlan Cox 		result->flags |= source->flags & OBJ_COLORED;
1357f8a47341SAlan Cox 		result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) &
1358f8a47341SAlan Cox 		    ((1 << (VM_NFREEORDER - 1)) - 1);
1359f8a47341SAlan Cox #endif
136089f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(source);
1361de5f6a77SJohn Dyson 	}
1362df8bae1dSRodney W. Grimes 
1363df8bae1dSRodney W. Grimes 
1364df8bae1dSRodney W. Grimes 	/*
1365df8bae1dSRodney W. Grimes 	 * Return the new things
1366df8bae1dSRodney W. Grimes 	 */
1367df8bae1dSRodney W. Grimes 	*offset = 0;
1368df8bae1dSRodney W. Grimes 	*object = result;
1369df8bae1dSRodney W. Grimes }
1370df8bae1dSRodney W. Grimes 
1371c5aaa06dSAlan Cox /*
1372c5aaa06dSAlan Cox  *	vm_object_split:
1373c5aaa06dSAlan Cox  *
1374c5aaa06dSAlan Cox  * Split the pages in a map entry into a new object.  This affords
1375c5aaa06dSAlan Cox  * easier removal of unused pages, and keeps object inheritance from
1376c5aaa06dSAlan Cox  * being a negative impact on memory usage.
1377c5aaa06dSAlan Cox  */
1378c5aaa06dSAlan Cox void
1379c5aaa06dSAlan Cox vm_object_split(vm_map_entry_t entry)
1380c5aaa06dSAlan Cox {
138173000556SAlan Cox 	vm_page_t m, m_next;
1382c5aaa06dSAlan Cox 	vm_object_t orig_object, new_object, source;
138373000556SAlan Cox 	vm_pindex_t idx, offidxstart;
138473000556SAlan Cox 	vm_size_t size;
1385c5aaa06dSAlan Cox 
1386c5aaa06dSAlan Cox 	orig_object = entry->object.vm_object;
1387c5aaa06dSAlan Cox 	if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1388c5aaa06dSAlan Cox 		return;
1389c5aaa06dSAlan Cox 	if (orig_object->ref_count <= 1)
1390c5aaa06dSAlan Cox 		return;
139189f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(orig_object);
1392c5aaa06dSAlan Cox 
13934da9f125SAlan Cox 	offidxstart = OFF_TO_IDX(entry->offset);
139495442adfSAlan Cox 	size = atop(entry->end - entry->start);
1395c5aaa06dSAlan Cox 
13964da9f125SAlan Cox 	/*
13974da9f125SAlan Cox 	 * If swap_pager_copy() is later called, it will convert new_object
13984da9f125SAlan Cox 	 * into a swap object.
13994da9f125SAlan Cox 	 */
14004da9f125SAlan Cox 	new_object = vm_object_allocate(OBJT_DEFAULT, size);
1401c5aaa06dSAlan Cox 
1402c5474b8fSAlan Cox 	/*
1403c5474b8fSAlan Cox 	 * At this point, the new object is still private, so the order in
1404c5474b8fSAlan Cox 	 * which the original and new objects are locked does not matter.
1405c5474b8fSAlan Cox 	 */
140689f6b863SAttilio Rao 	VM_OBJECT_WLOCK(new_object);
140789f6b863SAttilio Rao 	VM_OBJECT_WLOCK(orig_object);
1408c5aaa06dSAlan Cox 	source = orig_object->backing_object;
1409c5aaa06dSAlan Cox 	if (source != NULL) {
141089f6b863SAttilio Rao 		VM_OBJECT_WLOCK(source);
141119c244d0SAlan Cox 		if ((source->flags & OBJ_DEAD) != 0) {
141289f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(source);
141389f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(orig_object);
141489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(new_object);
141519c244d0SAlan Cox 			vm_object_deallocate(new_object);
141689f6b863SAttilio Rao 			VM_OBJECT_WLOCK(orig_object);
141719c244d0SAlan Cox 			return;
141819c244d0SAlan Cox 		}
14191c500307SAlan Cox 		LIST_INSERT_HEAD(&source->shadow_head,
1420c5aaa06dSAlan Cox 				  new_object, shadow_list);
14218e3a76fbSAlan Cox 		source->shadow_count++;
1422b921a12bSAlan Cox 		vm_object_reference_locked(source);	/* for new_object */
1423c5aaa06dSAlan Cox 		vm_object_clear_flag(source, OBJ_ONEMAPPING);
142489f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(source);
1425c5aaa06dSAlan Cox 		new_object->backing_object_offset =
14264da9f125SAlan Cox 			orig_object->backing_object_offset + entry->offset;
1427c5aaa06dSAlan Cox 		new_object->backing_object = source;
1428c5aaa06dSAlan Cox 	}
1429ef694c1aSEdward Tomasz Napierala 	if (orig_object->cred != NULL) {
1430ef694c1aSEdward Tomasz Napierala 		new_object->cred = orig_object->cred;
1431ef694c1aSEdward Tomasz Napierala 		crhold(orig_object->cred);
14323364c323SKonstantin Belousov 		new_object->charge = ptoa(size);
14333364c323SKonstantin Belousov 		KASSERT(orig_object->charge >= ptoa(size),
14343364c323SKonstantin Belousov 		    ("orig_object->charge < 0"));
14353364c323SKonstantin Belousov 		orig_object->charge -= ptoa(size);
14363364c323SKonstantin Belousov 	}
1437c5aaa06dSAlan Cox retry:
1438b382c10aSKonstantin Belousov 	m = vm_page_find_least(orig_object, offidxstart);
143973000556SAlan Cox 	for (; m != NULL && (idx = m->pindex - offidxstart) < size;
144073000556SAlan Cox 	    m = m_next) {
144173000556SAlan Cox 		m_next = TAILQ_NEXT(m, listq);
1442c5aaa06dSAlan Cox 
1443c5aaa06dSAlan Cox 		/*
1444c5aaa06dSAlan Cox 		 * We must wait for pending I/O to complete before we can
1445c5aaa06dSAlan Cox 		 * rename the page.
1446c5aaa06dSAlan Cox 		 *
1447c5aaa06dSAlan Cox 		 * We do not have to VM_PROT_NONE the page as mappings should
1448c5aaa06dSAlan Cox 		 * not be changed by this operation.
1449c5aaa06dSAlan Cox 		 */
1450c7aebda8SAttilio Rao 		if (vm_page_busied(m)) {
145189f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(new_object);
1452c7aebda8SAttilio Rao 			vm_page_lock(m);
1453c7aebda8SAttilio Rao 			VM_OBJECT_WUNLOCK(orig_object);
14545975e53dSKonstantin Belousov 			vm_page_busy_sleep(m, "spltwt", false);
1455c7aebda8SAttilio Rao 			VM_OBJECT_WLOCK(orig_object);
145689f6b863SAttilio Rao 			VM_OBJECT_WLOCK(new_object);
1457c5aaa06dSAlan Cox 			goto retry;
1458de33beddSAlan Cox 		}
1459e946b949SAttilio Rao 
14603453bca8SAlan Cox 		/* vm_page_rename() will dirty the page. */
1461e946b949SAttilio Rao 		if (vm_page_rename(m, new_object, idx)) {
1462e946b949SAttilio Rao 			VM_OBJECT_WUNLOCK(new_object);
1463e946b949SAttilio Rao 			VM_OBJECT_WUNLOCK(orig_object);
1464e946b949SAttilio Rao 			VM_WAIT;
1465e946b949SAttilio Rao 			VM_OBJECT_WLOCK(orig_object);
1466e946b949SAttilio Rao 			VM_OBJECT_WLOCK(new_object);
1467e946b949SAttilio Rao 			goto retry;
1468e946b949SAttilio Rao 		}
1469b5f359b7SAlan Cox #if VM_NRESERVLEVEL > 0
1470b5f359b7SAlan Cox 		/*
1471b5f359b7SAlan Cox 		 * If some of the reservation's allocated pages remain with
1472b5f359b7SAlan Cox 		 * the original object, then transferring the reservation to
1473b5f359b7SAlan Cox 		 * the new object is neither particularly beneficial nor
1474b5f359b7SAlan Cox 		 * particularly harmful as compared to leaving the reservation
1475b5f359b7SAlan Cox 		 * with the original object.  If, however, all of the
1476b5f359b7SAlan Cox 		 * reservation's allocated pages are transferred to the new
1477b5f359b7SAlan Cox 		 * object, then transferring the reservation is typically
1478b5f359b7SAlan Cox 		 * beneficial.  Determining which of these two cases applies
1479b5f359b7SAlan Cox 		 * would be more costly than unconditionally renaming the
1480b5f359b7SAlan Cox 		 * reservation.
1481b5f359b7SAlan Cox 		 */
1482b5f359b7SAlan Cox 		vm_reserv_rename(m, new_object, orig_object, offidxstart);
1483b5f359b7SAlan Cox #endif
1484dfd55c0cSAttilio Rao 		if (orig_object->type == OBJT_SWAP)
1485c7aebda8SAttilio Rao 			vm_page_xbusy(m);
1486c5aaa06dSAlan Cox 	}
1487d7a013c3SAlan Cox 	if (orig_object->type == OBJT_SWAP) {
1488c5aaa06dSAlan Cox 		/*
1489c7c8dd7eSAlan Cox 		 * swap_pager_copy() can sleep, in which case the orig_object's
1490c7c8dd7eSAlan Cox 		 * and new_object's locks are released and reacquired.
1491c5aaa06dSAlan Cox 		 */
1492c5aaa06dSAlan Cox 		swap_pager_copy(orig_object, new_object, offidxstart, 0);
1493dfd55c0cSAttilio Rao 		TAILQ_FOREACH(m, &new_object->memq, listq)
1494c7aebda8SAttilio Rao 			vm_page_xunbusy(m);
1495c5aaa06dSAlan Cox 	}
149689f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(orig_object);
149789f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(new_object);
1498c5aaa06dSAlan Cox 	entry->object.vm_object = new_object;
1499c5aaa06dSAlan Cox 	entry->offset = 0LL;
1500c5aaa06dSAlan Cox 	vm_object_deallocate(orig_object);
150189f6b863SAttilio Rao 	VM_OBJECT_WLOCK(new_object);
1502c5aaa06dSAlan Cox }
1503c5aaa06dSAlan Cox 
15042ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_NOWAIT	0x0002
15052ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_WAIT	0x0004
15062ad1a3f7SMatthew Dillon 
150799a1570aSKonstantin Belousov static vm_page_t
15084cc8daf7SConrad Meyer vm_object_collapse_scan_wait(vm_object_t object, vm_page_t p, vm_page_t next,
150999a1570aSKonstantin Belousov     int op)
151099a1570aSKonstantin Belousov {
151199a1570aSKonstantin Belousov 	vm_object_t backing_object;
151299a1570aSKonstantin Belousov 
151399a1570aSKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED(object);
151499a1570aSKonstantin Belousov 	backing_object = object->backing_object;
151599a1570aSKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED(backing_object);
151699a1570aSKonstantin Belousov 
151799a1570aSKonstantin Belousov 	KASSERT(p == NULL || vm_page_busied(p), ("unbusy page %p", p));
151899a1570aSKonstantin Belousov 	KASSERT(p == NULL || p->object == object || p->object == backing_object,
151999a1570aSKonstantin Belousov 	    ("invalid ownership %p %p %p", p, object, backing_object));
152099a1570aSKonstantin Belousov 	if ((op & OBSC_COLLAPSE_NOWAIT) != 0)
152199a1570aSKonstantin Belousov 		return (next);
152299a1570aSKonstantin Belousov 	if (p != NULL)
152399a1570aSKonstantin Belousov 		vm_page_lock(p);
152499a1570aSKonstantin Belousov 	VM_OBJECT_WUNLOCK(object);
152599a1570aSKonstantin Belousov 	VM_OBJECT_WUNLOCK(backing_object);
152699a1570aSKonstantin Belousov 	if (p == NULL)
152799a1570aSKonstantin Belousov 		VM_WAIT;
152899a1570aSKonstantin Belousov 	else
15295975e53dSKonstantin Belousov 		vm_page_busy_sleep(p, "vmocol", false);
153099a1570aSKonstantin Belousov 	VM_OBJECT_WLOCK(object);
153199a1570aSKonstantin Belousov 	VM_OBJECT_WLOCK(backing_object);
153299a1570aSKonstantin Belousov 	return (TAILQ_FIRST(&backing_object->memq));
153399a1570aSKonstantin Belousov }
153499a1570aSKonstantin Belousov 
153599a1570aSKonstantin Belousov static bool
15364cc8daf7SConrad Meyer vm_object_scan_all_shadowed(vm_object_t object)
15374cc8daf7SConrad Meyer {
15384cc8daf7SConrad Meyer 	vm_object_t backing_object;
15394cc8daf7SConrad Meyer 	vm_page_t p, pp;
154077d6fd97SKonstantin Belousov 	vm_pindex_t backing_offset_index, new_pindex, pi, ps;
15414cc8daf7SConrad Meyer 
15424cc8daf7SConrad Meyer 	VM_OBJECT_ASSERT_WLOCKED(object);
15434cc8daf7SConrad Meyer 	VM_OBJECT_ASSERT_WLOCKED(object->backing_object);
15444cc8daf7SConrad Meyer 
15454cc8daf7SConrad Meyer 	backing_object = object->backing_object;
15464cc8daf7SConrad Meyer 
154777d6fd97SKonstantin Belousov 	if (backing_object->type != OBJT_DEFAULT &&
154877d6fd97SKonstantin Belousov 	    backing_object->type != OBJT_SWAP)
15494cc8daf7SConrad Meyer 		return (false);
15504cc8daf7SConrad Meyer 
155177d6fd97SKonstantin Belousov 	pi = backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
155277d6fd97SKonstantin Belousov 	p = vm_page_find_least(backing_object, pi);
155377d6fd97SKonstantin Belousov 	ps = swap_pager_find_least(backing_object, pi);
15544cc8daf7SConrad Meyer 
15554cc8daf7SConrad Meyer 	/*
155677d6fd97SKonstantin Belousov 	 * Only check pages inside the parent object's range and
155777d6fd97SKonstantin Belousov 	 * inside the parent object's mapping of the backing object.
15584cc8daf7SConrad Meyer 	 */
155977d6fd97SKonstantin Belousov 	for (;; pi++) {
156077d6fd97SKonstantin Belousov 		if (p != NULL && p->pindex < pi)
156177d6fd97SKonstantin Belousov 			p = TAILQ_NEXT(p, listq);
156277d6fd97SKonstantin Belousov 		if (ps < pi)
156377d6fd97SKonstantin Belousov 			ps = swap_pager_find_least(backing_object, pi);
156477d6fd97SKonstantin Belousov 		if (p == NULL && ps >= backing_object->size)
156577d6fd97SKonstantin Belousov 			break;
156677d6fd97SKonstantin Belousov 		else if (p == NULL)
156777d6fd97SKonstantin Belousov 			pi = ps;
156877d6fd97SKonstantin Belousov 		else
156977d6fd97SKonstantin Belousov 			pi = MIN(p->pindex, ps);
157077d6fd97SKonstantin Belousov 
157177d6fd97SKonstantin Belousov 		new_pindex = pi - backing_offset_index;
157277d6fd97SKonstantin Belousov 		if (new_pindex >= object->size)
157377d6fd97SKonstantin Belousov 			break;
15744cc8daf7SConrad Meyer 
15754cc8daf7SConrad Meyer 		/*
15764cc8daf7SConrad Meyer 		 * See if the parent has the page or if the parent's object
15774cc8daf7SConrad Meyer 		 * pager has the page.  If the parent has the page but the page
15784cc8daf7SConrad Meyer 		 * is not valid, the parent's object pager must have the page.
15794cc8daf7SConrad Meyer 		 *
15804cc8daf7SConrad Meyer 		 * If this fails, the parent does not completely shadow the
15814cc8daf7SConrad Meyer 		 * object and we might as well give up now.
15824cc8daf7SConrad Meyer 		 */
15834cc8daf7SConrad Meyer 		pp = vm_page_lookup(object, new_pindex);
15844cc8daf7SConrad Meyer 		if ((pp == NULL || pp->valid == 0) &&
15854cc8daf7SConrad Meyer 		    !vm_pager_has_page(object, new_pindex, NULL, NULL))
15864cc8daf7SConrad Meyer 			return (false);
15874cc8daf7SConrad Meyer 	}
15884cc8daf7SConrad Meyer 	return (true);
15894cc8daf7SConrad Meyer }
15904cc8daf7SConrad Meyer 
15914cc8daf7SConrad Meyer static bool
15924cc8daf7SConrad Meyer vm_object_collapse_scan(vm_object_t object, int op)
15932ad1a3f7SMatthew Dillon {
15942ad1a3f7SMatthew Dillon 	vm_object_t backing_object;
159599a1570aSKonstantin Belousov 	vm_page_t next, p, pp;
159699a1570aSKonstantin Belousov 	vm_pindex_t backing_offset_index, new_pindex;
15972ad1a3f7SMatthew Dillon 
159889f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
159989f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object->backing_object);
16002ad1a3f7SMatthew Dillon 
16012ad1a3f7SMatthew Dillon 	backing_object = object->backing_object;
16022ad1a3f7SMatthew Dillon 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
16032ad1a3f7SMatthew Dillon 
16042ad1a3f7SMatthew Dillon 	/*
16052ad1a3f7SMatthew Dillon 	 * Initial conditions
16062ad1a3f7SMatthew Dillon 	 */
16074cc8daf7SConrad Meyer 	if ((op & OBSC_COLLAPSE_WAIT) != 0)
16082ad1a3f7SMatthew Dillon 		vm_object_set_flag(backing_object, OBJ_DEAD);
16092ad1a3f7SMatthew Dillon 
16102ad1a3f7SMatthew Dillon 	/*
16112ad1a3f7SMatthew Dillon 	 * Our scan
16122ad1a3f7SMatthew Dillon 	 */
16134cc8daf7SConrad Meyer 	for (p = TAILQ_FIRST(&backing_object->memq); p != NULL; p = next) {
161499a1570aSKonstantin Belousov 		next = TAILQ_NEXT(p, listq);
161599a1570aSKonstantin Belousov 		new_pindex = p->pindex - backing_offset_index;
16162ad1a3f7SMatthew Dillon 
16172ad1a3f7SMatthew Dillon 		/*
16182ad1a3f7SMatthew Dillon 		 * Check for busy page
16192ad1a3f7SMatthew Dillon 		 */
1620c7aebda8SAttilio Rao 		if (vm_page_busied(p)) {
16214cc8daf7SConrad Meyer 			next = vm_object_collapse_scan_wait(object, p, next, op);
16222ad1a3f7SMatthew Dillon 			continue;
16232ad1a3f7SMatthew Dillon 		}
16242ad1a3f7SMatthew Dillon 
162599a1570aSKonstantin Belousov 		KASSERT(p->object == backing_object,
16264cc8daf7SConrad Meyer 		    ("vm_object_collapse_scan: object mismatch"));
16272ad1a3f7SMatthew Dillon 
162899a1570aSKonstantin Belousov 		if (p->pindex < backing_offset_index ||
162999a1570aSKonstantin Belousov 		    new_pindex >= object->size) {
1630e946b949SAttilio Rao 			if (backing_object->type == OBJT_SWAP)
16314cc8daf7SConrad Meyer 				swap_pager_freespace(backing_object, p->pindex,
16324cc8daf7SConrad Meyer 				    1);
1633e946b949SAttilio Rao 
16342ad1a3f7SMatthew Dillon 			/*
16354cc8daf7SConrad Meyer 			 * Page is out of the parent object's range, we can
16364cc8daf7SConrad Meyer 			 * simply destroy it.
16372ad1a3f7SMatthew Dillon 			 */
16382965a453SKip Macy 			vm_page_lock(p);
1639f6d89838SAlan Cox 			KASSERT(!pmap_page_is_mapped(p),
1640f6d89838SAlan Cox 			    ("freeing mapped page %p", p));
1641f6d89838SAlan Cox 			if (p->wire_count == 0)
16422ad1a3f7SMatthew Dillon 				vm_page_free(p);
1643f6d89838SAlan Cox 			else
1644f6d89838SAlan Cox 				vm_page_remove(p);
16452965a453SKip Macy 			vm_page_unlock(p);
16462ad1a3f7SMatthew Dillon 			continue;
16472ad1a3f7SMatthew Dillon 		}
16482ad1a3f7SMatthew Dillon 
16492ad1a3f7SMatthew Dillon 		pp = vm_page_lookup(object, new_pindex);
165099a1570aSKonstantin Belousov 		if (pp != NULL && vm_page_busied(pp)) {
1651e18cc7bfSMax Laier 			/*
16524cc8daf7SConrad Meyer 			 * The page in the parent is busy and possibly not
16534cc8daf7SConrad Meyer 			 * (yet) valid.  Until its state is finalized by the
16544cc8daf7SConrad Meyer 			 * busy bit owner, we can't tell whether it shadows the
16554cc8daf7SConrad Meyer 			 * original page.  Therefore, we must either skip it
16564cc8daf7SConrad Meyer 			 * and the original (backing_object) page or wait for
16574cc8daf7SConrad Meyer 			 * its state to be finalized.
1658e18cc7bfSMax Laier 			 *
16594cc8daf7SConrad Meyer 			 * This is due to a race with vm_fault() where we must
16604cc8daf7SConrad Meyer 			 * unbusy the original (backing_obj) page before we can
16614cc8daf7SConrad Meyer 			 * (re)lock the parent.  Hence we can get here.
1662e18cc7bfSMax Laier 			 */
16634cc8daf7SConrad Meyer 			next = vm_object_collapse_scan_wait(object, pp, next,
16644cc8daf7SConrad Meyer 			    op);
1665e18cc7bfSMax Laier 			continue;
1666e18cc7bfSMax Laier 		}
166799a1570aSKonstantin Belousov 
166899a1570aSKonstantin Belousov 		KASSERT(pp == NULL || pp->valid != 0,
166999a1570aSKonstantin Belousov 		    ("unbusy invalid page %p", pp));
167099a1570aSKonstantin Belousov 
16714cc8daf7SConrad Meyer 		if (pp != NULL || vm_pager_has_page(object, new_pindex, NULL,
16724cc8daf7SConrad Meyer 			NULL)) {
167399a1570aSKonstantin Belousov 			/*
16744cc8daf7SConrad Meyer 			 * The page already exists in the parent OR swap exists
16754cc8daf7SConrad Meyer 			 * for this location in the parent.  Leave the parent's
16764cc8daf7SConrad Meyer 			 * page alone.  Destroy the original page from the
16774cc8daf7SConrad Meyer 			 * backing object.
167899a1570aSKonstantin Belousov 			 */
1679e946b949SAttilio Rao 			if (backing_object->type == OBJT_SWAP)
16804cc8daf7SConrad Meyer 				swap_pager_freespace(backing_object, p->pindex,
16814cc8daf7SConrad Meyer 				    1);
16822965a453SKip Macy 			vm_page_lock(p);
1683f6d89838SAlan Cox 			KASSERT(!pmap_page_is_mapped(p),
1684f6d89838SAlan Cox 			    ("freeing mapped page %p", p));
1685f6d89838SAlan Cox 			if (p->wire_count == 0)
16862ad1a3f7SMatthew Dillon 				vm_page_free(p);
1687f6d89838SAlan Cox 			else
1688f6d89838SAlan Cox 				vm_page_remove(p);
16892965a453SKip Macy 			vm_page_unlock(p);
16902ad1a3f7SMatthew Dillon 			continue;
16912ad1a3f7SMatthew Dillon 		}
16922ad1a3f7SMatthew Dillon 
1693e946b949SAttilio Rao 		/*
16944cc8daf7SConrad Meyer 		 * Page does not exist in parent, rename the page from the
16954cc8daf7SConrad Meyer 		 * backing object to the main object.
1696e946b949SAttilio Rao 		 *
16974cc8daf7SConrad Meyer 		 * If the page was mapped to a process, it can remain mapped
16983453bca8SAlan Cox 		 * through the rename.  vm_page_rename() will dirty the page.
1699e946b949SAttilio Rao 		 */
1700e946b949SAttilio Rao 		if (vm_page_rename(p, object, new_pindex)) {
17014cc8daf7SConrad Meyer 			next = vm_object_collapse_scan_wait(object, NULL, next,
17024cc8daf7SConrad Meyer 			    op);
1703e946b949SAttilio Rao 			continue;
1704e946b949SAttilio Rao 		}
170514a5dc17SAttilio Rao 
170614a5dc17SAttilio Rao 		/* Use the old pindex to free the right page. */
1707e946b949SAttilio Rao 		if (backing_object->type == OBJT_SWAP)
170814a5dc17SAttilio Rao 			swap_pager_freespace(backing_object,
170914a5dc17SAttilio Rao 			    new_pindex + backing_offset_index, 1);
1710e946b949SAttilio Rao 
1711f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
1712f8a47341SAlan Cox 		/*
1713f8a47341SAlan Cox 		 * Rename the reservation.
1714f8a47341SAlan Cox 		 */
1715f8a47341SAlan Cox 		vm_reserv_rename(p, object, backing_object,
1716f8a47341SAlan Cox 		    backing_offset_index);
1717f8a47341SAlan Cox #endif
17182ad1a3f7SMatthew Dillon 	}
171999a1570aSKonstantin Belousov 	return (true);
17202ad1a3f7SMatthew Dillon }
17212ad1a3f7SMatthew Dillon 
1722df8bae1dSRodney W. Grimes 
1723df8bae1dSRodney W. Grimes /*
17242fe6e4d7SDavid Greenman  * this version of collapse allows the operation to occur earlier and
17252fe6e4d7SDavid Greenman  * when paging_in_progress is true for an object...  This is not a complete
17262fe6e4d7SDavid Greenman  * operation, but should plug 99.9% of the rest of the leaks.
17272fe6e4d7SDavid Greenman  */
17282fe6e4d7SDavid Greenman static void
17291b40f8c0SMatthew Dillon vm_object_qcollapse(vm_object_t object)
17302fe6e4d7SDavid Greenman {
17312ad1a3f7SMatthew Dillon 	vm_object_t backing_object = object->backing_object;
17322fe6e4d7SDavid Greenman 
173389f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
173489f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(backing_object);
17351b40f8c0SMatthew Dillon 
17362fe6e4d7SDavid Greenman 	if (backing_object->ref_count != 1)
17372fe6e4d7SDavid Greenman 		return;
17382fe6e4d7SDavid Greenman 
17394cc8daf7SConrad Meyer 	vm_object_collapse_scan(object, OBSC_COLLAPSE_NOWAIT);
17402fe6e4d7SDavid Greenman }
17412fe6e4d7SDavid Greenman 
1742df8bae1dSRodney W. Grimes /*
1743df8bae1dSRodney W. Grimes  *	vm_object_collapse:
1744df8bae1dSRodney W. Grimes  *
1745df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
1746df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
1747df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
1748df8bae1dSRodney W. Grimes  */
174926f9a767SRodney W. Grimes void
17501b40f8c0SMatthew Dillon vm_object_collapse(vm_object_t object)
1751df8bae1dSRodney W. Grimes {
175298f139daSKonstantin Belousov 	vm_object_t backing_object, new_backing_object;
175398f139daSKonstantin Belousov 
175489f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
175523955314SAlfred Perlstein 
1756df8bae1dSRodney W. Grimes 	while (TRUE) {
1757df8bae1dSRodney W. Grimes 		/*
1758df8bae1dSRodney W. Grimes 		 * Verify that the conditions are right for collapse:
1759df8bae1dSRodney W. Grimes 		 *
17602ad1a3f7SMatthew Dillon 		 * The object exists and the backing object exists.
1761df8bae1dSRodney W. Grimes 		 */
176224a1cce3SDavid Greenman 		if ((backing_object = object->backing_object) == NULL)
17632ad1a3f7SMatthew Dillon 			break;
1764df8bae1dSRodney W. Grimes 
1765f919ebdeSDavid Greenman 		/*
1766f919ebdeSDavid Greenman 		 * we check the backing object first, because it is most likely
176724a1cce3SDavid Greenman 		 * not collapsable.
1768f919ebdeSDavid Greenman 		 */
176989f6b863SAttilio Rao 		VM_OBJECT_WLOCK(backing_object);
177024a1cce3SDavid Greenman 		if (backing_object->handle != NULL ||
177124a1cce3SDavid Greenman 		    (backing_object->type != OBJT_DEFAULT &&
177224a1cce3SDavid Greenman 		     backing_object->type != OBJT_SWAP) ||
1773f919ebdeSDavid Greenman 		    (backing_object->flags & OBJ_DEAD) ||
177424a1cce3SDavid Greenman 		    object->handle != NULL ||
177524a1cce3SDavid Greenman 		    (object->type != OBJT_DEFAULT &&
177624a1cce3SDavid Greenman 		     object->type != OBJT_SWAP) ||
177724a1cce3SDavid Greenman 		    (object->flags & OBJ_DEAD)) {
177889f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(backing_object);
17792ad1a3f7SMatthew Dillon 			break;
178024a1cce3SDavid Greenman 		}
17819b4814bbSDavid Greenman 
178298f139daSKonstantin Belousov 		if (object->paging_in_progress != 0 ||
178398f139daSKonstantin Belousov 		    backing_object->paging_in_progress != 0) {
1784b9921222SDavid Greenman 			vm_object_qcollapse(object);
178589f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(backing_object);
17862ad1a3f7SMatthew Dillon 			break;
1787df8bae1dSRodney W. Grimes 		}
178898f139daSKonstantin Belousov 
178926f9a767SRodney W. Grimes 		/*
17900d94caffSDavid Greenman 		 * We know that we can either collapse the backing object (if
17912ad1a3f7SMatthew Dillon 		 * the parent is the only reference to it) or (perhaps) have
17922ad1a3f7SMatthew Dillon 		 * the parent bypass the object if the parent happens to shadow
17932ad1a3f7SMatthew Dillon 		 * all the resident pages in the entire backing object.
17942ad1a3f7SMatthew Dillon 		 *
17952ad1a3f7SMatthew Dillon 		 * This is ignoring pager-backed pages such as swap pages.
17964cc8daf7SConrad Meyer 		 * vm_object_collapse_scan fails the shadowing test in this
17972ad1a3f7SMatthew Dillon 		 * case.
1798df8bae1dSRodney W. Grimes 		 */
1799df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
1800aa9bc3b1SKonstantin Belousov 			vm_object_pip_add(object, 1);
1801aa9bc3b1SKonstantin Belousov 			vm_object_pip_add(backing_object, 1);
1802aa9bc3b1SKonstantin Belousov 
1803df8bae1dSRodney W. Grimes 			/*
18042ad1a3f7SMatthew Dillon 			 * If there is exactly one reference to the backing
18052ad1a3f7SMatthew Dillon 			 * object, we can collapse it into the parent.
1806df8bae1dSRodney W. Grimes 			 */
18074cc8daf7SConrad Meyer 			vm_object_collapse_scan(object, OBSC_COLLAPSE_WAIT);
1808df8bae1dSRodney W. Grimes 
1809f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
1810f8a47341SAlan Cox 			/*
1811f8a47341SAlan Cox 			 * Break any reservations from backing_object.
1812f8a47341SAlan Cox 			 */
1813f8a47341SAlan Cox 			if (__predict_false(!LIST_EMPTY(&backing_object->rvq)))
1814f8a47341SAlan Cox 				vm_reserv_break_all(backing_object);
1815f8a47341SAlan Cox #endif
1816f8a47341SAlan Cox 
1817df8bae1dSRodney W. Grimes 			/*
1818df8bae1dSRodney W. Grimes 			 * Move the pager from backing_object to object.
1819df8bae1dSRodney W. Grimes 			 */
18206be36525SAlan Cox 			if (backing_object->type == OBJT_SWAP) {
182124a1cce3SDavid Greenman 				/*
1822c7c8dd7eSAlan Cox 				 * swap_pager_copy() can sleep, in which case
1823c7c8dd7eSAlan Cox 				 * the backing_object's and object's locks are
1824c7c8dd7eSAlan Cox 				 * released and reacquired.
1825571a1e92SAttilio Rao 				 * Since swap_pager_copy() is being asked to
1826571a1e92SAttilio Rao 				 * destroy the source, it will change the
1827571a1e92SAttilio Rao 				 * backing_object's type to OBJT_DEFAULT.
182824a1cce3SDavid Greenman 				 */
18291c7c3c6aSMatthew Dillon 				swap_pager_copy(
18301c7c3c6aSMatthew Dillon 				    backing_object,
18311c7c3c6aSMatthew Dillon 				    object,
18321c7c3c6aSMatthew Dillon 				    OFF_TO_IDX(object->backing_object_offset), TRUE);
1833c0503609SDavid Greenman 			}
1834df8bae1dSRodney W. Grimes 			/*
1835df8bae1dSRodney W. Grimes 			 * Object now shadows whatever backing_object did.
18362ad1a3f7SMatthew Dillon 			 * Note that the reference to
18372ad1a3f7SMatthew Dillon 			 * backing_object->backing_object moves from within
18382ad1a3f7SMatthew Dillon 			 * backing_object to within object.
1839df8bae1dSRodney W. Grimes 			 */
18401c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
18414f7c7f6eSAlan Cox 			backing_object->shadow_count--;
1842de5f6a77SJohn Dyson 			if (backing_object->backing_object) {
184389f6b863SAttilio Rao 				VM_OBJECT_WLOCK(backing_object->backing_object);
18441c500307SAlan Cox 				LIST_REMOVE(backing_object, shadow_list);
184543186e53SAlan Cox 				LIST_INSERT_HEAD(
184643186e53SAlan Cox 				    &backing_object->backing_object->shadow_head,
184743186e53SAlan Cox 				    object, shadow_list);
184843186e53SAlan Cox 				/*
184943186e53SAlan Cox 				 * The shadow_count has not changed.
185043186e53SAlan Cox 				 */
185189f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(backing_object->backing_object);
1852de5f6a77SJohn Dyson 			}
185324a1cce3SDavid Greenman 			object->backing_object = backing_object->backing_object;
18542ad1a3f7SMatthew Dillon 			object->backing_object_offset +=
18552ad1a3f7SMatthew Dillon 			    backing_object->backing_object_offset;
18562ad1a3f7SMatthew Dillon 
1857df8bae1dSRodney W. Grimes 			/*
1858df8bae1dSRodney W. Grimes 			 * Discard backing_object.
1859df8bae1dSRodney W. Grimes 			 *
18600d94caffSDavid Greenman 			 * Since the backing object has no pages, no pager left,
18610d94caffSDavid Greenman 			 * and no object references within it, all that is
18620d94caffSDavid Greenman 			 * necessary is to dispose of it.
1863df8bae1dSRodney W. Grimes 			 */
18649b4d473aSKonstantin Belousov 			KASSERT(backing_object->ref_count == 1, (
18659b4d473aSKonstantin Belousov "backing_object %p was somehow re-referenced during collapse!",
18669b4d473aSKonstantin Belousov 			    backing_object));
1867aa9bc3b1SKonstantin Belousov 			vm_object_pip_wakeup(backing_object);
1868e735691bSJohn Baldwin 			backing_object->type = OBJT_DEAD;
1869e735691bSJohn Baldwin 			backing_object->ref_count = 0;
187089f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(backing_object);
18719b4d473aSKonstantin Belousov 			vm_object_destroy(backing_object);
1872df8bae1dSRodney W. Grimes 
1873aa9bc3b1SKonstantin Belousov 			vm_object_pip_wakeup(object);
1874df8bae1dSRodney W. Grimes 			object_collapses++;
18750d94caffSDavid Greenman 		} else {
1876df8bae1dSRodney W. Grimes 			/*
18772ad1a3f7SMatthew Dillon 			 * If we do not entirely shadow the backing object,
18782ad1a3f7SMatthew Dillon 			 * there is nothing we can do so we give up.
1879df8bae1dSRodney W. Grimes 			 */
1880df59a0feSJeff Roberson 			if (object->resident_page_count != object->size &&
18814cc8daf7SConrad Meyer 			    !vm_object_scan_all_shadowed(object)) {
188289f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(backing_object);
18832ad1a3f7SMatthew Dillon 				break;
188424a1cce3SDavid Greenman 			}
1885df8bae1dSRodney W. Grimes 
1886df8bae1dSRodney W. Grimes 			/*
18870d94caffSDavid Greenman 			 * Make the parent shadow the next object in the
18880d94caffSDavid Greenman 			 * chain.  Deallocating backing_object will not remove
18890d94caffSDavid Greenman 			 * it, since its reference count is at least 2.
1890df8bae1dSRodney W. Grimes 			 */
18911c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
1892eaf13dd7SJohn Dyson 			backing_object->shadow_count--;
189395e5e988SJohn Dyson 
189495e5e988SJohn Dyson 			new_backing_object = backing_object->backing_object;
18958aef1712SMatthew Dillon 			if ((object->backing_object = new_backing_object) != NULL) {
189689f6b863SAttilio Rao 				VM_OBJECT_WLOCK(new_backing_object);
18971c500307SAlan Cox 				LIST_INSERT_HEAD(
18982ad1a3f7SMatthew Dillon 				    &new_backing_object->shadow_head,
18992ad1a3f7SMatthew Dillon 				    object,
19002ad1a3f7SMatthew Dillon 				    shadow_list
19012ad1a3f7SMatthew Dillon 				);
1902eaf13dd7SJohn Dyson 				new_backing_object->shadow_count++;
1903b921a12bSAlan Cox 				vm_object_reference_locked(new_backing_object);
190489f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(new_backing_object);
190595e5e988SJohn Dyson 				object->backing_object_offset +=
190695e5e988SJohn Dyson 					backing_object->backing_object_offset;
1907de5f6a77SJohn Dyson 			}
1908df8bae1dSRodney W. Grimes 
1909df8bae1dSRodney W. Grimes 			/*
19100d94caffSDavid Greenman 			 * Drop the reference count on backing_object. Since
191122ec553fSAlan Cox 			 * its ref_count was at least 2, it will not vanish.
1912df8bae1dSRodney W. Grimes 			 */
191322ec553fSAlan Cox 			backing_object->ref_count--;
191489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(backing_object);
1915df8bae1dSRodney W. Grimes 			object_bypasses++;
1916df8bae1dSRodney W. Grimes 		}
1917df8bae1dSRodney W. Grimes 
1918df8bae1dSRodney W. Grimes 		/*
1919df8bae1dSRodney W. Grimes 		 * Try again with this object's new backing object.
1920df8bae1dSRodney W. Grimes 		 */
1921df8bae1dSRodney W. Grimes 	}
1922df8bae1dSRodney W. Grimes }
1923df8bae1dSRodney W. Grimes 
1924df8bae1dSRodney W. Grimes /*
1925bff99f0dSAlan Cox  *	vm_object_page_remove:
1926df8bae1dSRodney W. Grimes  *
192768855966SAlan Cox  *	For the given object, either frees or invalidates each of the
19286bbee8e2SAlan Cox  *	specified pages.  In general, a page is freed.  However, if a page is
19296bbee8e2SAlan Cox  *	wired for any reason other than the existence of a managed, wired
19306bbee8e2SAlan Cox  *	mapping, then it may be invalidated but not removed from the object.
19316bbee8e2SAlan Cox  *	Pages are specified by the given range ["start", "end") and the option
19326bbee8e2SAlan Cox  *	OBJPR_CLEANONLY.  As a special case, if "end" is zero, then the range
19336bbee8e2SAlan Cox  *	extends from "start" to the end of the object.  If the option
19346bbee8e2SAlan Cox  *	OBJPR_CLEANONLY is specified, then only the non-dirty pages within the
19356bbee8e2SAlan Cox  *	specified range are affected.  If the option OBJPR_NOTMAPPED is
19366bbee8e2SAlan Cox  *	specified, then the pages within the specified range must have no
19376bbee8e2SAlan Cox  *	mappings.  Otherwise, if this option is not specified, any mappings to
19386bbee8e2SAlan Cox  *	the specified pages are removed before the pages are freed or
19396bbee8e2SAlan Cox  *	invalidated.
194068855966SAlan Cox  *
19416bbee8e2SAlan Cox  *	In general, this operation should only be performed on objects that
19426bbee8e2SAlan Cox  *	contain managed pages.  There are, however, two exceptions.  First, it
19436bbee8e2SAlan Cox  *	is performed on the kernel and kmem objects by vm_map_entry_delete().
19446bbee8e2SAlan Cox  *	Second, it is used by msync(..., MS_INVALIDATE) to invalidate device-
19456bbee8e2SAlan Cox  *	backed pages.  In both of these cases, the option OBJPR_CLEANONLY must
19466bbee8e2SAlan Cox  *	not be specified and the option OBJPR_NOTMAPPED must be specified.
1947df8bae1dSRodney W. Grimes  *
1948df8bae1dSRodney W. Grimes  *	The object must be locked.
1949df8bae1dSRodney W. Grimes  */
195026f9a767SRodney W. Grimes void
1951ecde4b32SAlan Cox vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
19526bbee8e2SAlan Cox     int options)
1953df8bae1dSRodney W. Grimes {
1954d031cff1SMatthew Dillon 	vm_page_t p, next;
195593c5d3a4SKonstantin Belousov 	struct mtx *mtx;
1956df8bae1dSRodney W. Grimes 
195789f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
195828634820SAlan Cox 	KASSERT((object->flags & OBJ_UNMANAGED) == 0 ||
19596bbee8e2SAlan Cox 	    (options & (OBJPR_CLEANONLY | OBJPR_NOTMAPPED)) == OBJPR_NOTMAPPED,
19606bbee8e2SAlan Cox 	    ("vm_object_page_remove: illegal options for object %p", object));
1961ecde4b32SAlan Cox 	if (object->resident_page_count == 0)
19627667839aSAlan Cox 		return;
1963d474eaaaSDoug Rabson 	vm_object_pip_add(object, 1);
196426f9a767SRodney W. Grimes again:
1965b382c10aSKonstantin Belousov 	p = vm_page_find_least(object, start);
196693c5d3a4SKonstantin Belousov 	mtx = NULL;
19672965a453SKip Macy 
196875741c04SAlan Cox 	/*
19696bbee8e2SAlan Cox 	 * Here, the variable "p" is either (1) the page with the least pindex
19706bbee8e2SAlan Cox 	 * greater than or equal to the parameter "start" or (2) NULL.
197175741c04SAlan Cox 	 */
19726bbee8e2SAlan Cox 	for (; p != NULL && (p->pindex < end || end == 0); p = next) {
1973b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
197475741c04SAlan Cox 
197559677d3cSAlan Cox 		/*
19766bbee8e2SAlan Cox 		 * If the page is wired for any reason besides the existence
19776bbee8e2SAlan Cox 		 * of managed, wired mappings, then it cannot be freed.  For
19786bbee8e2SAlan Cox 		 * example, fictitious pages, which represent device memory,
19796bbee8e2SAlan Cox 		 * are inherently wired and cannot be freed.  They can,
19806bbee8e2SAlan Cox 		 * however, be invalidated if the option OBJPR_CLEANONLY is
19816bbee8e2SAlan Cox 		 * not specified.
198259677d3cSAlan Cox 		 */
198393c5d3a4SKonstantin Belousov 		vm_page_change_lock(p, &mtx);
19843aaea6efSKonstantin Belousov 		if (vm_page_xbusied(p)) {
19853aaea6efSKonstantin Belousov 			VM_OBJECT_WUNLOCK(object);
19865975e53dSKonstantin Belousov 			vm_page_busy_sleep(p, "vmopax", true);
19873aaea6efSKonstantin Belousov 			VM_OBJECT_WLOCK(object);
19883aaea6efSKonstantin Belousov 			goto again;
19893aaea6efSKonstantin Belousov 		}
19906195b24aSKonstantin Belousov 		if (p->wire_count != 0) {
19916195b24aSKonstantin Belousov 			if ((options & OBJPR_NOTMAPPED) == 0)
19924fec79beSAlan Cox 				pmap_remove_all(p);
19936bbee8e2SAlan Cox 			if ((options & OBJPR_CLEANONLY) == 0) {
1994bd7e5f99SJohn Dyson 				p->valid = 0;
1995a28042d1SAlan Cox 				vm_page_undirty(p);
1996a28042d1SAlan Cox 			}
199793c5d3a4SKonstantin Belousov 			continue;
19980d94caffSDavid Greenman 		}
1999c7aebda8SAttilio Rao 		if (vm_page_busied(p)) {
2000c7aebda8SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
20015975e53dSKonstantin Belousov 			vm_page_busy_sleep(p, "vmopar", false);
2002c7aebda8SAttilio Rao 			VM_OBJECT_WLOCK(object);
200326f9a767SRodney W. Grimes 			goto again;
2004c7aebda8SAttilio Rao 		}
200568855966SAlan Cox 		KASSERT((p->flags & PG_FICTITIOUS) == 0,
200668855966SAlan Cox 		    ("vm_object_page_remove: page %p is fictitious", p));
20076bbee8e2SAlan Cox 		if ((options & OBJPR_CLEANONLY) != 0 && p->valid != 0) {
20086bbee8e2SAlan Cox 			if ((options & OBJPR_NOTMAPPED) == 0)
200978985e42SAlan Cox 				pmap_remove_write(p);
2010ebf5d94eSKonstantin Belousov 			if (p->dirty)
201193c5d3a4SKonstantin Belousov 				continue;
20122965a453SKip Macy 		}
20136195b24aSKonstantin Belousov 		if ((options & OBJPR_NOTMAPPED) == 0)
20144fec79beSAlan Cox 			pmap_remove_all(p);
2015df8bae1dSRodney W. Grimes 		vm_page_free(p);
20162965a453SKip Macy 	}
201793c5d3a4SKonstantin Belousov 	if (mtx != NULL)
201893c5d3a4SKonstantin Belousov 		mtx_unlock(mtx);
2019f919ebdeSDavid Greenman 	vm_object_pip_wakeup(object);
2020c0503609SDavid Greenman }
2021df8bae1dSRodney W. Grimes 
2022df8bae1dSRodney W. Grimes /*
20233138cd36SMark Johnston  *	vm_object_page_noreuse:
2024936c09acSJohn Baldwin  *
20253138cd36SMark Johnston  *	For the given object, attempt to move the specified pages to
20263138cd36SMark Johnston  *	the head of the inactive queue.  This bypasses regular LRU
20273138cd36SMark Johnston  *	operation and allows the pages to be reused quickly under memory
20283138cd36SMark Johnston  *	pressure.  If a page is wired for any reason, then it will not
20293138cd36SMark Johnston  *	be queued.  Pages are specified by the range ["start", "end").
20303138cd36SMark Johnston  *	As a special case, if "end" is zero, then the range extends from
20313138cd36SMark Johnston  *	"start" to the end of the object.
2032936c09acSJohn Baldwin  *
2033936c09acSJohn Baldwin  *	This operation should only be performed on objects that
203428634820SAlan Cox  *	contain non-fictitious, managed pages.
2035936c09acSJohn Baldwin  *
2036936c09acSJohn Baldwin  *	The object must be locked.
2037936c09acSJohn Baldwin  */
2038936c09acSJohn Baldwin void
20393138cd36SMark Johnston vm_object_page_noreuse(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
2040936c09acSJohn Baldwin {
204193c5d3a4SKonstantin Belousov 	struct mtx *mtx;
2042936c09acSJohn Baldwin 	vm_page_t p, next;
2043936c09acSJohn Baldwin 
204452d1addaSAlan Cox 	VM_OBJECT_ASSERT_LOCKED(object);
204528634820SAlan Cox 	KASSERT((object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0,
20463138cd36SMark Johnston 	    ("vm_object_page_noreuse: illegal object %p", object));
2047936c09acSJohn Baldwin 	if (object->resident_page_count == 0)
2048936c09acSJohn Baldwin 		return;
2049936c09acSJohn Baldwin 	p = vm_page_find_least(object, start);
2050936c09acSJohn Baldwin 
2051936c09acSJohn Baldwin 	/*
2052936c09acSJohn Baldwin 	 * Here, the variable "p" is either (1) the page with the least pindex
2053936c09acSJohn Baldwin 	 * greater than or equal to the parameter "start" or (2) NULL.
2054936c09acSJohn Baldwin 	 */
2055936c09acSJohn Baldwin 	mtx = NULL;
2056936c09acSJohn Baldwin 	for (; p != NULL && (p->pindex < end || end == 0); p = next) {
2057936c09acSJohn Baldwin 		next = TAILQ_NEXT(p, listq);
205893c5d3a4SKonstantin Belousov 		vm_page_change_lock(p, &mtx);
20593138cd36SMark Johnston 		vm_page_deactivate_noreuse(p);
2060936c09acSJohn Baldwin 	}
2061936c09acSJohn Baldwin 	if (mtx != NULL)
2062936c09acSJohn Baldwin 		mtx_unlock(mtx);
2063936c09acSJohn Baldwin }
2064936c09acSJohn Baldwin 
2065936c09acSJohn Baldwin /*
2066387aabc5SAlan Cox  *	Populate the specified range of the object with valid pages.  Returns
2067387aabc5SAlan Cox  *	TRUE if the range is successfully populated and FALSE otherwise.
2068387aabc5SAlan Cox  *
2069387aabc5SAlan Cox  *	Note: This function should be optimized to pass a larger array of
2070387aabc5SAlan Cox  *	pages to vm_pager_get_pages() before it is applied to a non-
2071387aabc5SAlan Cox  *	OBJT_DEVICE object.
2072387aabc5SAlan Cox  *
2073387aabc5SAlan Cox  *	The object must be locked.
2074387aabc5SAlan Cox  */
2075387aabc5SAlan Cox boolean_t
2076387aabc5SAlan Cox vm_object_populate(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
2077387aabc5SAlan Cox {
2078093c7f39SGleb Smirnoff 	vm_page_t m;
2079387aabc5SAlan Cox 	vm_pindex_t pindex;
2080387aabc5SAlan Cox 	int rv;
2081387aabc5SAlan Cox 
208289f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
2083387aabc5SAlan Cox 	for (pindex = start; pindex < end; pindex++) {
20845944de8eSKonstantin Belousov 		m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL);
2085387aabc5SAlan Cox 		if (m->valid != VM_PAGE_BITS_ALL) {
2086b0cd2017SGleb Smirnoff 			rv = vm_pager_get_pages(object, &m, 1, NULL, NULL);
2087387aabc5SAlan Cox 			if (rv != VM_PAGER_OK) {
20882965a453SKip Macy 				vm_page_lock(m);
2089387aabc5SAlan Cox 				vm_page_free(m);
20902965a453SKip Macy 				vm_page_unlock(m);
2091387aabc5SAlan Cox 				break;
2092387aabc5SAlan Cox 			}
2093387aabc5SAlan Cox 		}
2094387aabc5SAlan Cox 		/*
2095387aabc5SAlan Cox 		 * Keep "m" busy because a subsequent iteration may unlock
2096387aabc5SAlan Cox 		 * the object.
2097387aabc5SAlan Cox 		 */
2098387aabc5SAlan Cox 	}
2099387aabc5SAlan Cox 	if (pindex > start) {
2100387aabc5SAlan Cox 		m = vm_page_lookup(object, start);
2101387aabc5SAlan Cox 		while (m != NULL && m->pindex < pindex) {
2102c7aebda8SAttilio Rao 			vm_page_xunbusy(m);
2103387aabc5SAlan Cox 			m = TAILQ_NEXT(m, listq);
2104387aabc5SAlan Cox 		}
2105387aabc5SAlan Cox 	}
2106387aabc5SAlan Cox 	return (pindex == end);
2107387aabc5SAlan Cox }
2108387aabc5SAlan Cox 
2109387aabc5SAlan Cox /*
2110df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
2111df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
2112df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
2113df8bae1dSRodney W. Grimes  *
2114df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
2115df8bae1dSRodney W. Grimes  *
2116df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
2117df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
2118df8bae1dSRodney W. Grimes  *
2119df8bae1dSRodney W. Grimes  *	Parameters:
2120df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
2121df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
2122df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
212357a21abaSAlan Cox  *		next_size	Size of reference to the second object
21243364c323SKonstantin Belousov  *		reserved	Indicator that extension region has
21253364c323SKonstantin Belousov  *				swap accounted for
2126df8bae1dSRodney W. Grimes  *
2127df8bae1dSRodney W. Grimes  *	Conditions:
2128df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
2129df8bae1dSRodney W. Grimes  */
21300d94caffSDavid Greenman boolean_t
213157a21abaSAlan Cox vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset,
21323364c323SKonstantin Belousov     vm_size_t prev_size, vm_size_t next_size, boolean_t reserved)
2133df8bae1dSRodney W. Grimes {
2134ea41812fSAlan Cox 	vm_pindex_t next_pindex;
2135df8bae1dSRodney W. Grimes 
213600e1854aSAlan Cox 	if (prev_object == NULL)
2137df8bae1dSRodney W. Grimes 		return (TRUE);
213889f6b863SAttilio Rao 	VM_OBJECT_WLOCK(prev_object);
21399ded9474SKonstantin Belousov 	if ((prev_object->type != OBJT_DEFAULT &&
21409ded9474SKonstantin Belousov 	    prev_object->type != OBJT_SWAP) ||
2141f08f7dcaSKonstantin Belousov 	    (prev_object->flags & OBJ_TMPFS_NODE) != 0) {
214289f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(prev_object);
214330dcfc09SJohn Dyson 		return (FALSE);
214430dcfc09SJohn Dyson 	}
214530dcfc09SJohn Dyson 
2146df8bae1dSRodney W. Grimes 	/*
2147df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
2148df8bae1dSRodney W. Grimes 	 */
2149df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
2150df8bae1dSRodney W. Grimes 
2151df8bae1dSRodney W. Grimes 	/*
21520d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
21530d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
21540d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
2155df8bae1dSRodney W. Grimes 	 */
21568cc7e047SJohn Dyson 	if (prev_object->backing_object != NULL) {
215789f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(prev_object);
2158df8bae1dSRodney W. Grimes 		return (FALSE);
2159df8bae1dSRodney W. Grimes 	}
2160a316d390SJohn Dyson 
2161a316d390SJohn Dyson 	prev_size >>= PAGE_SHIFT;
2162a316d390SJohn Dyson 	next_size >>= PAGE_SHIFT;
216357a21abaSAlan Cox 	next_pindex = OFF_TO_IDX(prev_offset) + prev_size;
21648cc7e047SJohn Dyson 
21658cc7e047SJohn Dyson 	if ((prev_object->ref_count > 1) &&
2166ea41812fSAlan Cox 	    (prev_object->size != next_pindex)) {
216789f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(prev_object);
21688cc7e047SJohn Dyson 		return (FALSE);
21698cc7e047SJohn Dyson 	}
21708cc7e047SJohn Dyson 
2171df8bae1dSRodney W. Grimes 	/*
21723364c323SKonstantin Belousov 	 * Account for the charge.
21733364c323SKonstantin Belousov 	 */
2174ef694c1aSEdward Tomasz Napierala 	if (prev_object->cred != NULL) {
21753364c323SKonstantin Belousov 
21763364c323SKonstantin Belousov 		/*
21773364c323SKonstantin Belousov 		 * If prev_object was charged, then this mapping,
2178763df3ecSPedro F. Giffuni 		 * although not charged now, may become writable
2179ef694c1aSEdward Tomasz Napierala 		 * later. Non-NULL cred in the object would prevent
21803364c323SKonstantin Belousov 		 * swap reservation during enabling of the write
21813364c323SKonstantin Belousov 		 * access, so reserve swap now. Failed reservation
21823364c323SKonstantin Belousov 		 * cause allocation of the separate object for the map
21833364c323SKonstantin Belousov 		 * entry, and swap reservation for this entry is
21843364c323SKonstantin Belousov 		 * managed in appropriate time.
21853364c323SKonstantin Belousov 		 */
2186ef694c1aSEdward Tomasz Napierala 		if (!reserved && !swap_reserve_by_cred(ptoa(next_size),
2187ef694c1aSEdward Tomasz Napierala 		    prev_object->cred)) {
21889f790a17SKonstantin Belousov 			VM_OBJECT_WUNLOCK(prev_object);
21893364c323SKonstantin Belousov 			return (FALSE);
21903364c323SKonstantin Belousov 		}
21913364c323SKonstantin Belousov 		prev_object->charge += ptoa(next_size);
21923364c323SKonstantin Belousov 	}
21933364c323SKonstantin Belousov 
21943364c323SKonstantin Belousov 	/*
21950d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
21960d94caffSDavid Greenman 	 * deallocation.
2197df8bae1dSRodney W. Grimes 	 */
2198ea41812fSAlan Cox 	if (next_pindex < prev_object->size) {
21996bbee8e2SAlan Cox 		vm_object_page_remove(prev_object, next_pindex, next_pindex +
22006bbee8e2SAlan Cox 		    next_size, 0);
2201ea41812fSAlan Cox 		if (prev_object->type == OBJT_SWAP)
2202ea41812fSAlan Cox 			swap_pager_freespace(prev_object,
2203ea41812fSAlan Cox 					     next_pindex, next_size);
22043364c323SKonstantin Belousov #if 0
2205ef694c1aSEdward Tomasz Napierala 		if (prev_object->cred != NULL) {
22063364c323SKonstantin Belousov 			KASSERT(prev_object->charge >=
22073364c323SKonstantin Belousov 			    ptoa(prev_object->size - next_pindex),
22083364c323SKonstantin Belousov 			    ("object %p overcharged 1 %jx %jx", prev_object,
22093364c323SKonstantin Belousov 				(uintmax_t)next_pindex, (uintmax_t)next_size));
22103364c323SKonstantin Belousov 			prev_object->charge -= ptoa(prev_object->size -
22113364c323SKonstantin Belousov 			    next_pindex);
22123364c323SKonstantin Belousov 		}
22133364c323SKonstantin Belousov #endif
2214ea41812fSAlan Cox 	}
2215df8bae1dSRodney W. Grimes 
2216df8bae1dSRodney W. Grimes 	/*
2217df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
2218df8bae1dSRodney W. Grimes 	 */
2219ea41812fSAlan Cox 	if (next_pindex + next_size > prev_object->size)
2220ea41812fSAlan Cox 		prev_object->size = next_pindex + next_size;
2221df8bae1dSRodney W. Grimes 
222289f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(prev_object);
2223df8bae1dSRodney W. Grimes 	return (TRUE);
2224df8bae1dSRodney W. Grimes }
2225df8bae1dSRodney W. Grimes 
22267a5a6352SMatthew Dillon void
22277a5a6352SMatthew Dillon vm_object_set_writeable_dirty(vm_object_t object)
22287a5a6352SMatthew Dillon {
22297a5a6352SMatthew Dillon 
223089f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
2231f40cb1c6SKonstantin Belousov 	if (object->type != OBJT_VNODE) {
2232f40cb1c6SKonstantin Belousov 		if ((object->flags & OBJ_TMPFS_NODE) != 0) {
2233f40cb1c6SKonstantin Belousov 			KASSERT(object->type == OBJT_SWAP, ("non-swap tmpfs"));
2234f40cb1c6SKonstantin Belousov 			vm_object_set_flag(object, OBJ_TMPFS_DIRTY);
2235f40cb1c6SKonstantin Belousov 		}
22363280870dSKonstantin Belousov 		return;
2237f40cb1c6SKonstantin Belousov 	}
22383280870dSKonstantin Belousov 	object->generation++;
22393280870dSKonstantin Belousov 	if ((object->flags & OBJ_MIGHTBEDIRTY) != 0)
2240ee39666aSJeff Roberson 		return;
2241af51d7bfSAlan Cox 	vm_object_set_flag(object, OBJ_MIGHTBEDIRTY);
22427a5a6352SMatthew Dillon }
22437a5a6352SMatthew Dillon 
224403462509SAlan Cox /*
224503462509SAlan Cox  *	vm_object_unwire:
224603462509SAlan Cox  *
224703462509SAlan Cox  *	For each page offset within the specified range of the given object,
224803462509SAlan Cox  *	find the highest-level page in the shadow chain and unwire it.  A page
224903462509SAlan Cox  *	must exist at every page offset, and the highest-level page must be
225003462509SAlan Cox  *	wired.
225103462509SAlan Cox  */
225203462509SAlan Cox void
225303462509SAlan Cox vm_object_unwire(vm_object_t object, vm_ooffset_t offset, vm_size_t length,
225403462509SAlan Cox     uint8_t queue)
225503462509SAlan Cox {
225603462509SAlan Cox 	vm_object_t tobject;
225703462509SAlan Cox 	vm_page_t m, tm;
225803462509SAlan Cox 	vm_pindex_t end_pindex, pindex, tpindex;
225903462509SAlan Cox 	int depth, locked_depth;
226003462509SAlan Cox 
226103462509SAlan Cox 	KASSERT((offset & PAGE_MASK) == 0,
226203462509SAlan Cox 	    ("vm_object_unwire: offset is not page aligned"));
226303462509SAlan Cox 	KASSERT((length & PAGE_MASK) == 0,
226403462509SAlan Cox 	    ("vm_object_unwire: length is not a multiple of PAGE_SIZE"));
226503462509SAlan Cox 	/* The wired count of a fictitious page never changes. */
226603462509SAlan Cox 	if ((object->flags & OBJ_FICTITIOUS) != 0)
226703462509SAlan Cox 		return;
226803462509SAlan Cox 	pindex = OFF_TO_IDX(offset);
226903462509SAlan Cox 	end_pindex = pindex + atop(length);
227003462509SAlan Cox 	locked_depth = 1;
227103462509SAlan Cox 	VM_OBJECT_RLOCK(object);
227203462509SAlan Cox 	m = vm_page_find_least(object, pindex);
227303462509SAlan Cox 	while (pindex < end_pindex) {
227403462509SAlan Cox 		if (m == NULL || pindex < m->pindex) {
227503462509SAlan Cox 			/*
227603462509SAlan Cox 			 * The first object in the shadow chain doesn't
227703462509SAlan Cox 			 * contain a page at the current index.  Therefore,
227803462509SAlan Cox 			 * the page must exist in a backing object.
227903462509SAlan Cox 			 */
228003462509SAlan Cox 			tobject = object;
228103462509SAlan Cox 			tpindex = pindex;
228203462509SAlan Cox 			depth = 0;
228303462509SAlan Cox 			do {
228403462509SAlan Cox 				tpindex +=
228503462509SAlan Cox 				    OFF_TO_IDX(tobject->backing_object_offset);
228603462509SAlan Cox 				tobject = tobject->backing_object;
228703462509SAlan Cox 				KASSERT(tobject != NULL,
228803462509SAlan Cox 				    ("vm_object_unwire: missing page"));
228903462509SAlan Cox 				if ((tobject->flags & OBJ_FICTITIOUS) != 0)
229003462509SAlan Cox 					goto next_page;
229103462509SAlan Cox 				depth++;
229203462509SAlan Cox 				if (depth == locked_depth) {
229303462509SAlan Cox 					locked_depth++;
229403462509SAlan Cox 					VM_OBJECT_RLOCK(tobject);
229503462509SAlan Cox 				}
229603462509SAlan Cox 			} while ((tm = vm_page_lookup(tobject, tpindex)) ==
229703462509SAlan Cox 			    NULL);
229803462509SAlan Cox 		} else {
229903462509SAlan Cox 			tm = m;
230003462509SAlan Cox 			m = TAILQ_NEXT(m, listq);
230103462509SAlan Cox 		}
230203462509SAlan Cox 		vm_page_lock(tm);
230303462509SAlan Cox 		vm_page_unwire(tm, queue);
230403462509SAlan Cox 		vm_page_unlock(tm);
230503462509SAlan Cox next_page:
230603462509SAlan Cox 		pindex++;
230703462509SAlan Cox 	}
230803462509SAlan Cox 	/* Release the accumulated object locks. */
230903462509SAlan Cox 	for (depth = 0; depth < locked_depth; depth++) {
231003462509SAlan Cox 		tobject = object->backing_object;
231103462509SAlan Cox 		VM_OBJECT_RUNLOCK(object);
231203462509SAlan Cox 		object = tobject;
231303462509SAlan Cox 	}
231403462509SAlan Cox }
231503462509SAlan Cox 
231663e4c6cdSEric van Gyzen struct vnode *
231763e4c6cdSEric van Gyzen vm_object_vnode(vm_object_t object)
231863e4c6cdSEric van Gyzen {
231963e4c6cdSEric van Gyzen 
232063e4c6cdSEric van Gyzen 	VM_OBJECT_ASSERT_LOCKED(object);
232163e4c6cdSEric van Gyzen 	if (object->type == OBJT_VNODE)
232263e4c6cdSEric van Gyzen 		return (object->handle);
232363e4c6cdSEric van Gyzen 	if (object->type == OBJT_SWAP && (object->flags & OBJ_TMPFS) != 0)
232463e4c6cdSEric van Gyzen 		return (object->un_pager.swp.swp_tmpfs);
232563e4c6cdSEric van Gyzen 	return (NULL);
232663e4c6cdSEric van Gyzen }
232763e4c6cdSEric van Gyzen 
2328ff87ae35SJohn Baldwin static int
2329ff87ae35SJohn Baldwin sysctl_vm_object_list(SYSCTL_HANDLER_ARGS)
2330ff87ae35SJohn Baldwin {
23310ecee546SKonstantin Belousov 	struct kinfo_vmobject *kvo;
2332ff87ae35SJohn Baldwin 	char *fullpath, *freepath;
2333ff87ae35SJohn Baldwin 	struct vnode *vp;
2334ff87ae35SJohn Baldwin 	struct vattr va;
2335ff87ae35SJohn Baldwin 	vm_object_t obj;
2336ff87ae35SJohn Baldwin 	vm_page_t m;
2337ff87ae35SJohn Baldwin 	int count, error;
2338ff87ae35SJohn Baldwin 
2339ff87ae35SJohn Baldwin 	if (req->oldptr == NULL) {
2340ff87ae35SJohn Baldwin 		/*
2341ff87ae35SJohn Baldwin 		 * If an old buffer has not been provided, generate an
2342ff87ae35SJohn Baldwin 		 * estimate of the space needed for a subsequent call.
2343ff87ae35SJohn Baldwin 		 */
2344ff87ae35SJohn Baldwin 		mtx_lock(&vm_object_list_mtx);
2345ff87ae35SJohn Baldwin 		count = 0;
2346ff87ae35SJohn Baldwin 		TAILQ_FOREACH(obj, &vm_object_list, object_list) {
2347ff87ae35SJohn Baldwin 			if (obj->type == OBJT_DEAD)
2348ff87ae35SJohn Baldwin 				continue;
2349ff87ae35SJohn Baldwin 			count++;
2350ff87ae35SJohn Baldwin 		}
2351ff87ae35SJohn Baldwin 		mtx_unlock(&vm_object_list_mtx);
2352ff87ae35SJohn Baldwin 		return (SYSCTL_OUT(req, NULL, sizeof(struct kinfo_vmobject) *
2353ff87ae35SJohn Baldwin 		    count * 11 / 10));
2354ff87ae35SJohn Baldwin 	}
2355ff87ae35SJohn Baldwin 
23560ecee546SKonstantin Belousov 	kvo = malloc(sizeof(*kvo), M_TEMP, M_WAITOK);
2357ff87ae35SJohn Baldwin 	error = 0;
2358ff87ae35SJohn Baldwin 
2359ff87ae35SJohn Baldwin 	/*
2360ff87ae35SJohn Baldwin 	 * VM objects are type stable and are never removed from the
2361ff87ae35SJohn Baldwin 	 * list once added.  This allows us to safely read obj->object_list
2362ff87ae35SJohn Baldwin 	 * after reacquiring the VM object lock.
2363ff87ae35SJohn Baldwin 	 */
2364ff87ae35SJohn Baldwin 	mtx_lock(&vm_object_list_mtx);
2365ff87ae35SJohn Baldwin 	TAILQ_FOREACH(obj, &vm_object_list, object_list) {
2366ff87ae35SJohn Baldwin 		if (obj->type == OBJT_DEAD)
2367ff87ae35SJohn Baldwin 			continue;
2368ff87ae35SJohn Baldwin 		VM_OBJECT_RLOCK(obj);
2369ff87ae35SJohn Baldwin 		if (obj->type == OBJT_DEAD) {
2370ff87ae35SJohn Baldwin 			VM_OBJECT_RUNLOCK(obj);
2371ff87ae35SJohn Baldwin 			continue;
2372ff87ae35SJohn Baldwin 		}
2373ff87ae35SJohn Baldwin 		mtx_unlock(&vm_object_list_mtx);
23740ecee546SKonstantin Belousov 		kvo->kvo_size = ptoa(obj->size);
23750ecee546SKonstantin Belousov 		kvo->kvo_resident = obj->resident_page_count;
23760ecee546SKonstantin Belousov 		kvo->kvo_ref_count = obj->ref_count;
23770ecee546SKonstantin Belousov 		kvo->kvo_shadow_count = obj->shadow_count;
23780ecee546SKonstantin Belousov 		kvo->kvo_memattr = obj->memattr;
23790ecee546SKonstantin Belousov 		kvo->kvo_active = 0;
23800ecee546SKonstantin Belousov 		kvo->kvo_inactive = 0;
2381ff87ae35SJohn Baldwin 		TAILQ_FOREACH(m, &obj->memq, listq) {
2382ff87ae35SJohn Baldwin 			/*
2383ff87ae35SJohn Baldwin 			 * A page may belong to the object but be
2384ff87ae35SJohn Baldwin 			 * dequeued and set to PQ_NONE while the
2385ff87ae35SJohn Baldwin 			 * object lock is not held.  This makes the
2386ff87ae35SJohn Baldwin 			 * reads of m->queue below racy, and we do not
2387ff87ae35SJohn Baldwin 			 * count pages set to PQ_NONE.  However, this
2388ff87ae35SJohn Baldwin 			 * sysctl is only meant to give an
2389ff87ae35SJohn Baldwin 			 * approximation of the system anyway.
2390ff87ae35SJohn Baldwin 			 */
2391ebcddc72SAlan Cox 			if (vm_page_active(m))
23920ecee546SKonstantin Belousov 				kvo->kvo_active++;
2393ebcddc72SAlan Cox 			else if (vm_page_inactive(m))
23940ecee546SKonstantin Belousov 				kvo->kvo_inactive++;
2395ff87ae35SJohn Baldwin 		}
2396ff87ae35SJohn Baldwin 
23970ecee546SKonstantin Belousov 		kvo->kvo_vn_fileid = 0;
23980ecee546SKonstantin Belousov 		kvo->kvo_vn_fsid = 0;
23990ecee546SKonstantin Belousov 		kvo->kvo_vn_fsid_freebsd11 = 0;
2400ff87ae35SJohn Baldwin 		freepath = NULL;
2401ff87ae35SJohn Baldwin 		fullpath = "";
2402ff87ae35SJohn Baldwin 		vp = NULL;
2403ff87ae35SJohn Baldwin 		switch (obj->type) {
2404ff87ae35SJohn Baldwin 		case OBJT_DEFAULT:
24050ecee546SKonstantin Belousov 			kvo->kvo_type = KVME_TYPE_DEFAULT;
2406ff87ae35SJohn Baldwin 			break;
2407ff87ae35SJohn Baldwin 		case OBJT_VNODE:
24080ecee546SKonstantin Belousov 			kvo->kvo_type = KVME_TYPE_VNODE;
2409ff87ae35SJohn Baldwin 			vp = obj->handle;
2410ff87ae35SJohn Baldwin 			vref(vp);
2411ff87ae35SJohn Baldwin 			break;
2412ff87ae35SJohn Baldwin 		case OBJT_SWAP:
24130ecee546SKonstantin Belousov 			kvo->kvo_type = KVME_TYPE_SWAP;
2414ff87ae35SJohn Baldwin 			break;
2415ff87ae35SJohn Baldwin 		case OBJT_DEVICE:
24160ecee546SKonstantin Belousov 			kvo->kvo_type = KVME_TYPE_DEVICE;
2417ff87ae35SJohn Baldwin 			break;
2418ff87ae35SJohn Baldwin 		case OBJT_PHYS:
24190ecee546SKonstantin Belousov 			kvo->kvo_type = KVME_TYPE_PHYS;
2420ff87ae35SJohn Baldwin 			break;
2421ff87ae35SJohn Baldwin 		case OBJT_DEAD:
24220ecee546SKonstantin Belousov 			kvo->kvo_type = KVME_TYPE_DEAD;
2423ff87ae35SJohn Baldwin 			break;
2424ff87ae35SJohn Baldwin 		case OBJT_SG:
24250ecee546SKonstantin Belousov 			kvo->kvo_type = KVME_TYPE_SG;
2426ff87ae35SJohn Baldwin 			break;
2427ff87ae35SJohn Baldwin 		case OBJT_MGTDEVICE:
24280ecee546SKonstantin Belousov 			kvo->kvo_type = KVME_TYPE_MGTDEVICE;
2429ff87ae35SJohn Baldwin 			break;
2430ff87ae35SJohn Baldwin 		default:
24310ecee546SKonstantin Belousov 			kvo->kvo_type = KVME_TYPE_UNKNOWN;
2432ff87ae35SJohn Baldwin 			break;
2433ff87ae35SJohn Baldwin 		}
2434ff87ae35SJohn Baldwin 		VM_OBJECT_RUNLOCK(obj);
2435ff87ae35SJohn Baldwin 		if (vp != NULL) {
2436ff87ae35SJohn Baldwin 			vn_fullpath(curthread, vp, &fullpath, &freepath);
2437ff87ae35SJohn Baldwin 			vn_lock(vp, LK_SHARED | LK_RETRY);
2438ff87ae35SJohn Baldwin 			if (VOP_GETATTR(vp, &va, curthread->td_ucred) == 0) {
24390ecee546SKonstantin Belousov 				kvo->kvo_vn_fileid = va.va_fileid;
24400ecee546SKonstantin Belousov 				kvo->kvo_vn_fsid = va.va_fsid;
24410ecee546SKonstantin Belousov 				kvo->kvo_vn_fsid_freebsd11 = va.va_fsid;
244269921123SKonstantin Belousov 								/* truncate */
2443ff87ae35SJohn Baldwin 			}
2444ff87ae35SJohn Baldwin 			vput(vp);
2445ff87ae35SJohn Baldwin 		}
2446ff87ae35SJohn Baldwin 
24470ecee546SKonstantin Belousov 		strlcpy(kvo->kvo_path, fullpath, sizeof(kvo->kvo_path));
2448ff87ae35SJohn Baldwin 		if (freepath != NULL)
2449ff87ae35SJohn Baldwin 			free(freepath, M_TEMP);
2450ff87ae35SJohn Baldwin 
2451ff87ae35SJohn Baldwin 		/* Pack record size down */
24520ecee546SKonstantin Belousov 		kvo->kvo_structsize = offsetof(struct kinfo_vmobject, kvo_path)
24530ecee546SKonstantin Belousov 		    + strlen(kvo->kvo_path) + 1;
24540ecee546SKonstantin Belousov 		kvo->kvo_structsize = roundup(kvo->kvo_structsize,
2455ff87ae35SJohn Baldwin 		    sizeof(uint64_t));
24560ecee546SKonstantin Belousov 		error = SYSCTL_OUT(req, kvo, kvo->kvo_structsize);
2457ff87ae35SJohn Baldwin 		mtx_lock(&vm_object_list_mtx);
2458ff87ae35SJohn Baldwin 		if (error)
2459ff87ae35SJohn Baldwin 			break;
2460ff87ae35SJohn Baldwin 	}
2461ff87ae35SJohn Baldwin 	mtx_unlock(&vm_object_list_mtx);
24620ecee546SKonstantin Belousov 	free(kvo, M_TEMP);
2463ff87ae35SJohn Baldwin 	return (error);
2464ff87ae35SJohn Baldwin }
2465ff87ae35SJohn Baldwin SYSCTL_PROC(_vm, OID_AUTO, objects, CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_SKIP |
2466ff87ae35SJohn Baldwin     CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_object_list, "S,kinfo_vmobject",
2467ff87ae35SJohn Baldwin     "List of VM objects");
2468ff87ae35SJohn Baldwin 
2469c7c34a24SBruce Evans #include "opt_ddb.h"
2470c3cb3e12SDavid Greenman #ifdef DDB
2471c7c34a24SBruce Evans #include <sys/kernel.h>
2472c7c34a24SBruce Evans 
2473ce9edcf5SPoul-Henning Kamp #include <sys/cons.h>
2474c7c34a24SBruce Evans 
2475c7c34a24SBruce Evans #include <ddb/ddb.h>
2476c7c34a24SBruce Evans 
2477cac597e4SBruce Evans static int
24781b40f8c0SMatthew Dillon _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
2479a1f6d91cSDavid Greenman {
2480a1f6d91cSDavid Greenman 	vm_map_t tmpm;
2481a1f6d91cSDavid Greenman 	vm_map_entry_t tmpe;
2482a1f6d91cSDavid Greenman 	vm_object_t obj;
2483a1f6d91cSDavid Greenman 	int entcount;
2484a1f6d91cSDavid Greenman 
2485a1f6d91cSDavid Greenman 	if (map == 0)
2486a1f6d91cSDavid Greenman 		return 0;
2487a1f6d91cSDavid Greenman 
2488a1f6d91cSDavid Greenman 	if (entry == 0) {
2489a1f6d91cSDavid Greenman 		tmpe = map->header.next;
2490a1f6d91cSDavid Greenman 		entcount = map->nentries;
2491a1f6d91cSDavid Greenman 		while (entcount-- && (tmpe != &map->header)) {
2492a1f6d91cSDavid Greenman 			if (_vm_object_in_map(map, object, tmpe)) {
2493a1f6d91cSDavid Greenman 				return 1;
2494a1f6d91cSDavid Greenman 			}
2495a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
2496a1f6d91cSDavid Greenman 		}
24979fdfe602SMatthew Dillon 	} else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
24989fdfe602SMatthew Dillon 		tmpm = entry->object.sub_map;
2499a1f6d91cSDavid Greenman 		tmpe = tmpm->header.next;
2500a1f6d91cSDavid Greenman 		entcount = tmpm->nentries;
2501a1f6d91cSDavid Greenman 		while (entcount-- && tmpe != &tmpm->header) {
2502a1f6d91cSDavid Greenman 			if (_vm_object_in_map(tmpm, object, tmpe)) {
2503a1f6d91cSDavid Greenman 				return 1;
2504a1f6d91cSDavid Greenman 			}
2505a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
2506a1f6d91cSDavid Greenman 		}
25078aef1712SMatthew Dillon 	} else if ((obj = entry->object.vm_object) != NULL) {
250824a1cce3SDavid Greenman 		for (; obj; obj = obj->backing_object)
2509a1f6d91cSDavid Greenman 			if (obj == object) {
2510a1f6d91cSDavid Greenman 				return 1;
2511a1f6d91cSDavid Greenman 			}
2512a1f6d91cSDavid Greenman 	}
2513a1f6d91cSDavid Greenman 	return 0;
2514a1f6d91cSDavid Greenman }
2515a1f6d91cSDavid Greenman 
2516cac597e4SBruce Evans static int
25171b40f8c0SMatthew Dillon vm_object_in_map(vm_object_t object)
2518a1f6d91cSDavid Greenman {
2519a1f6d91cSDavid Greenman 	struct proc *p;
25201005a129SJohn Baldwin 
252160517fd1SJohn Baldwin 	/* sx_slock(&allproc_lock); */
2522f67af5c9SXin LI 	FOREACH_PROC_IN_SYSTEM(p) {
2523a1f6d91cSDavid Greenman 		if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
2524a1f6d91cSDavid Greenman 			continue;
2525553629ebSJake Burkholder 		if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
252660517fd1SJohn Baldwin 			/* sx_sunlock(&allproc_lock); */
2527a1f6d91cSDavid Greenman 			return 1;
2528a1f6d91cSDavid Greenman 		}
2529553629ebSJake Burkholder 	}
253060517fd1SJohn Baldwin 	/* sx_sunlock(&allproc_lock); */
2531a1f6d91cSDavid Greenman 	if (_vm_object_in_map(kernel_map, object, 0))
2532a1f6d91cSDavid Greenman 		return 1;
2533a1f6d91cSDavid Greenman 	return 0;
2534a1f6d91cSDavid Greenman }
2535a1f6d91cSDavid Greenman 
2536c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check)
2537f708ef1bSPoul-Henning Kamp {
2538a1f6d91cSDavid Greenman 	vm_object_t object;
2539a1f6d91cSDavid Greenman 
2540a1f6d91cSDavid Greenman 	/*
2541a1f6d91cSDavid Greenman 	 * make sure that internal objs are in a map somewhere
2542a1f6d91cSDavid Greenman 	 * and none have zero ref counts.
2543a1f6d91cSDavid Greenman 	 */
2544cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
254524a1cce3SDavid Greenman 		if (object->handle == NULL &&
254624a1cce3SDavid Greenman 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2547a1f6d91cSDavid Greenman 			if (object->ref_count == 0) {
25483efc015bSPeter Wemm 				db_printf("vmochk: internal obj has zero ref count: %ld\n",
25493efc015bSPeter Wemm 					(long)object->size);
2550a1f6d91cSDavid Greenman 			}
2551a1f6d91cSDavid Greenman 			if (!vm_object_in_map(object)) {
2552fc62ef1fSBruce Evans 				db_printf(
2553fc62ef1fSBruce Evans 			"vmochk: internal obj is not in a map: "
2554fc62ef1fSBruce Evans 			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2555fc62ef1fSBruce Evans 				    object->ref_count, (u_long)object->size,
2556fc62ef1fSBruce Evans 				    (u_long)object->size,
2557fc62ef1fSBruce Evans 				    (void *)object->backing_object);
2558a1f6d91cSDavid Greenman 			}
2559a1f6d91cSDavid Greenman 		}
2560a1f6d91cSDavid Greenman 	}
2561a1f6d91cSDavid Greenman }
2562a1f6d91cSDavid Greenman 
256326f9a767SRodney W. Grimes /*
2564df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
2565df8bae1dSRodney W. Grimes  */
2566c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static)
2567df8bae1dSRodney W. Grimes {
2568c7c34a24SBruce Evans 	/* XXX convert args. */
2569c7c34a24SBruce Evans 	vm_object_t object = (vm_object_t)addr;
2570c7c34a24SBruce Evans 	boolean_t full = have_addr;
2571c7c34a24SBruce Evans 
2572d031cff1SMatthew Dillon 	vm_page_t p;
2573df8bae1dSRodney W. Grimes 
2574c7c34a24SBruce Evans 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
2575c7c34a24SBruce Evans #define	count	was_count
2576c7c34a24SBruce Evans 
2577d031cff1SMatthew Dillon 	int count;
2578df8bae1dSRodney W. Grimes 
2579df8bae1dSRodney W. Grimes 	if (object == NULL)
2580df8bae1dSRodney W. Grimes 		return;
2581df8bae1dSRodney W. Grimes 
2582eb95adefSBruce Evans 	db_iprintf(
2583ef694c1aSEdward Tomasz Napierala 	    "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x ruid %d charge %jx\n",
2584e47cd172SMaxime Henrion 	    object, (int)object->type, (uintmax_t)object->size,
25853364c323SKonstantin Belousov 	    object->resident_page_count, object->ref_count, object->flags,
2586ef694c1aSEdward Tomasz Napierala 	    object->cred ? object->cred->cr_ruid : -1, (uintmax_t)object->charge);
2587e47cd172SMaxime Henrion 	db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n",
25881c7c3c6aSMatthew Dillon 	    object->shadow_count,
2589eb95adefSBruce Evans 	    object->backing_object ? object->backing_object->ref_count : 0,
2590e47cd172SMaxime Henrion 	    object->backing_object, (uintmax_t)object->backing_object_offset);
2591df8bae1dSRodney W. Grimes 
2592df8bae1dSRodney W. Grimes 	if (!full)
2593df8bae1dSRodney W. Grimes 		return;
2594df8bae1dSRodney W. Grimes 
2595c7c34a24SBruce Evans 	db_indent += 2;
2596df8bae1dSRodney W. Grimes 	count = 0;
2597fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(p, &object->memq, listq) {
2598df8bae1dSRodney W. Grimes 		if (count == 0)
2599c7c34a24SBruce Evans 			db_iprintf("memory:=");
2600df8bae1dSRodney W. Grimes 		else if (count == 6) {
2601c7c34a24SBruce Evans 			db_printf("\n");
2602c7c34a24SBruce Evans 			db_iprintf(" ...");
2603df8bae1dSRodney W. Grimes 			count = 0;
2604df8bae1dSRodney W. Grimes 		} else
2605c7c34a24SBruce Evans 			db_printf(",");
2606df8bae1dSRodney W. Grimes 		count++;
2607df8bae1dSRodney W. Grimes 
2608e47cd172SMaxime Henrion 		db_printf("(off=0x%jx,page=0x%jx)",
2609e47cd172SMaxime Henrion 		    (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p));
2610df8bae1dSRodney W. Grimes 	}
2611df8bae1dSRodney W. Grimes 	if (count != 0)
2612c7c34a24SBruce Evans 		db_printf("\n");
2613c7c34a24SBruce Evans 	db_indent -= 2;
2614df8bae1dSRodney W. Grimes }
26155070c7f8SJohn Dyson 
2616c7c34a24SBruce Evans /* XXX. */
2617c7c34a24SBruce Evans #undef count
2618c7c34a24SBruce Evans 
2619c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */
26205070c7f8SJohn Dyson void
26211b40f8c0SMatthew Dillon vm_object_print(
26221b40f8c0SMatthew Dillon         /* db_expr_t */ long addr,
26231b40f8c0SMatthew Dillon 	boolean_t have_addr,
26241b40f8c0SMatthew Dillon 	/* db_expr_t */ long count,
26251b40f8c0SMatthew Dillon 	char *modif)
2626c7c34a24SBruce Evans {
2627c7c34a24SBruce Evans 	vm_object_print_static(addr, have_addr, count, modif);
2628c7c34a24SBruce Evans }
2629c7c34a24SBruce Evans 
2630c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
26315070c7f8SJohn Dyson {
26325070c7f8SJohn Dyson 	vm_object_t object;
2633bb2ac86fSKonstantin Belousov 	vm_pindex_t fidx;
2634bb2ac86fSKonstantin Belousov 	vm_paddr_t pa;
2635bb2ac86fSKonstantin Belousov 	vm_page_t m, prev_m;
2636bb2ac86fSKonstantin Belousov 	int rcount, nl, c;
2637cc64b484SAlfred Perlstein 
2638bb2ac86fSKonstantin Belousov 	nl = 0;
2639cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
2640fc62ef1fSBruce Evans 		db_printf("new object: %p\n", (void *)object);
26415070c7f8SJohn Dyson 		if (nl > 18) {
26425070c7f8SJohn Dyson 			c = cngetc();
26435070c7f8SJohn Dyson 			if (c != ' ')
26445070c7f8SJohn Dyson 				return;
26455070c7f8SJohn Dyson 			nl = 0;
26465070c7f8SJohn Dyson 		}
26475070c7f8SJohn Dyson 		nl++;
26485070c7f8SJohn Dyson 		rcount = 0;
26495070c7f8SJohn Dyson 		fidx = 0;
2650bb2ac86fSKonstantin Belousov 		pa = -1;
2651bb2ac86fSKonstantin Belousov 		TAILQ_FOREACH(m, &object->memq, listq) {
2652bb2ac86fSKonstantin Belousov 			if (m->pindex > 128)
2653bb2ac86fSKonstantin Belousov 				break;
2654bb2ac86fSKonstantin Belousov 			if ((prev_m = TAILQ_PREV(m, pglist, listq)) != NULL &&
2655bb2ac86fSKonstantin Belousov 			    prev_m->pindex + 1 != m->pindex) {
26565070c7f8SJohn Dyson 				if (rcount) {
26573efc015bSPeter Wemm 					db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
26583efc015bSPeter Wemm 						(long)fidx, rcount, (long)pa);
26595070c7f8SJohn Dyson 					if (nl > 18) {
26605070c7f8SJohn Dyson 						c = cngetc();
26615070c7f8SJohn Dyson 						if (c != ' ')
26625070c7f8SJohn Dyson 							return;
26635070c7f8SJohn Dyson 						nl = 0;
26645070c7f8SJohn Dyson 					}
26655070c7f8SJohn Dyson 					nl++;
26665070c7f8SJohn Dyson 					rcount = 0;
26675070c7f8SJohn Dyson 				}
26685070c7f8SJohn Dyson 			}
26695070c7f8SJohn Dyson 			if (rcount &&
26705070c7f8SJohn Dyson 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
26715070c7f8SJohn Dyson 				++rcount;
26725070c7f8SJohn Dyson 				continue;
26735070c7f8SJohn Dyson 			}
26745070c7f8SJohn Dyson 			if (rcount) {
26752446e4f0SAlan Cox 				db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
26763efc015bSPeter Wemm 					(long)fidx, rcount, (long)pa);
26775070c7f8SJohn Dyson 				if (nl > 18) {
26785070c7f8SJohn Dyson 					c = cngetc();
26795070c7f8SJohn Dyson 					if (c != ' ')
26805070c7f8SJohn Dyson 						return;
26815070c7f8SJohn Dyson 					nl = 0;
26825070c7f8SJohn Dyson 				}
26835070c7f8SJohn Dyson 				nl++;
26845070c7f8SJohn Dyson 			}
2685bb2ac86fSKonstantin Belousov 			fidx = m->pindex;
26865070c7f8SJohn Dyson 			pa = VM_PAGE_TO_PHYS(m);
26875070c7f8SJohn Dyson 			rcount = 1;
26885070c7f8SJohn Dyson 		}
26895070c7f8SJohn Dyson 		if (rcount) {
26903efc015bSPeter Wemm 			db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
26913efc015bSPeter Wemm 				(long)fidx, rcount, (long)pa);
26925070c7f8SJohn Dyson 			if (nl > 18) {
26935070c7f8SJohn Dyson 				c = cngetc();
26945070c7f8SJohn Dyson 				if (c != ' ')
26955070c7f8SJohn Dyson 					return;
26965070c7f8SJohn Dyson 				nl = 0;
26975070c7f8SJohn Dyson 			}
26985070c7f8SJohn Dyson 			nl++;
26995070c7f8SJohn Dyson 		}
27005070c7f8SJohn Dyson 	}
27015070c7f8SJohn Dyson }
2702c3cb3e12SDavid Greenman #endif /* DDB */
2703