xref: /freebsd/sys/vm/vm_object.c (revision 8da1c09853811f6ce71ec3814a6082e43b202c2d)
160727d8bSWarner Losh /*-
2796df753SPedro F. Giffuni  * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU)
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
8df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  *
343c4dd356SDavid Greenman  *	from: @(#)vm_object.c	8.5 (Berkeley) 3/22/94
35df8bae1dSRodney W. Grimes  *
36df8bae1dSRodney W. Grimes  *
37df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
38df8bae1dSRodney W. Grimes  * All rights reserved.
39df8bae1dSRodney W. Grimes  *
40df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
41df8bae1dSRodney W. Grimes  *
42df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
43df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
44df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
45df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
46df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
47df8bae1dSRodney W. Grimes  *
48df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
50df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
51df8bae1dSRodney W. Grimes  *
52df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
53df8bae1dSRodney W. Grimes  *
54df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
55df8bae1dSRodney W. Grimes  *  School of Computer Science
56df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
57df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
58df8bae1dSRodney W. Grimes  *
59df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
60df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
61df8bae1dSRodney W. Grimes  */
62df8bae1dSRodney W. Grimes 
63df8bae1dSRodney W. Grimes /*
64df8bae1dSRodney W. Grimes  *	Virtual memory object module.
65df8bae1dSRodney W. Grimes  */
66df8bae1dSRodney W. Grimes 
67874651b1SDavid E. O'Brien #include <sys/cdefs.h>
68874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
69874651b1SDavid E. O'Brien 
70f8a47341SAlan Cox #include "opt_vm.h"
71f8a47341SAlan Cox 
72df8bae1dSRodney W. Grimes #include <sys/param.h>
73df8bae1dSRodney W. Grimes #include <sys/systm.h>
743f289c3fSJeff Roberson #include <sys/cpuset.h>
75fb919e4dSMark Murray #include <sys/lock.h>
76867a482dSJohn Dyson #include <sys/mman.h>
77cf2819ccSJohn Dyson #include <sys/mount.h>
78b9b7a4beSMatthew Dillon #include <sys/kernel.h>
79f425ab8eSKonstantin Belousov #include <sys/pctrie.h>
80b9b7a4beSMatthew Dillon #include <sys/sysctl.h>
811b367556SJason Evans #include <sys/mutex.h>
82fb919e4dSMark Murray #include <sys/proc.h>		/* for curproc, pageproc */
83cf27e0d1SJeff Roberson #include <sys/refcount.h>
84fb919e4dSMark Murray #include <sys/socket.h>
853364c323SKonstantin Belousov #include <sys/resourcevar.h>
8689f6b863SAttilio Rao #include <sys/rwlock.h>
87ff87ae35SJohn Baldwin #include <sys/user.h>
88fb919e4dSMark Murray #include <sys/vnode.h>
89fb919e4dSMark Murray #include <sys/vmmeter.h>
901005a129SJohn Baldwin #include <sys/sx.h>
91df8bae1dSRodney W. Grimes 
92df8bae1dSRodney W. Grimes #include <vm/vm.h>
93efeaf95aSDavid Greenman #include <vm/vm_param.h>
94efeaf95aSDavid Greenman #include <vm/pmap.h>
95efeaf95aSDavid Greenman #include <vm/vm_map.h>
96efeaf95aSDavid Greenman #include <vm/vm_object.h>
97df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
9826f9a767SRodney W. Grimes #include <vm/vm_pageout.h>
990d94caffSDavid Greenman #include <vm/vm_pager.h>
100e2068d0bSJeff Roberson #include <vm/vm_phys.h>
101e2068d0bSJeff Roberson #include <vm/vm_pagequeue.h>
10205f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h>
103a1f6d91cSDavid Greenman #include <vm/vm_kern.h>
104efeaf95aSDavid Greenman #include <vm/vm_extern.h>
105774d251dSAttilio Rao #include <vm/vm_radix.h>
106f8a47341SAlan Cox #include <vm/vm_reserv.h>
107670d17b5SJeff Roberson #include <vm/uma.h>
10826f9a767SRodney W. Grimes 
109c53f7aceSDag-Erling Smørgrav static int old_msync;
110c53f7aceSDag-Erling Smørgrav SYSCTL_INT(_vm, OID_AUTO, old_msync, CTLFLAG_RW, &old_msync, 0,
111c53f7aceSDag-Erling Smørgrav     "Use old (insecure) msync behavior");
112c53f7aceSDag-Erling Smørgrav 
113757216f3SKonstantin Belousov static int	vm_object_page_collect_flush(vm_object_t object, vm_page_t p,
114126d6082SKonstantin Belousov 		    int pagerflags, int flags, boolean_t *clearobjflags,
115126d6082SKonstantin Belousov 		    boolean_t *eio);
1163280870dSKonstantin Belousov static boolean_t vm_object_page_remove_write(vm_page_t p, int flags,
117126d6082SKonstantin Belousov 		    boolean_t *clearobjflags);
118b9b7a4beSMatthew Dillon static void	vm_object_qcollapse(vm_object_t object);
11902dd8331SAlan Cox static void	vm_object_vndeallocate(vm_object_t object);
120f6b04d2bSDavid Greenman 
121df8bae1dSRodney W. Grimes /*
122df8bae1dSRodney W. Grimes  *	Virtual memory objects maintain the actual data
123df8bae1dSRodney W. Grimes  *	associated with allocated virtual memory.  A given
124df8bae1dSRodney W. Grimes  *	page of memory exists within exactly one object.
125df8bae1dSRodney W. Grimes  *
126df8bae1dSRodney W. Grimes  *	An object is only deallocated when all "references"
127df8bae1dSRodney W. Grimes  *	are given up.  Only one "reference" to a given
128df8bae1dSRodney W. Grimes  *	region of an object should be writeable.
129df8bae1dSRodney W. Grimes  *
130df8bae1dSRodney W. Grimes  *	Associated with each object is a list of all resident
131df8bae1dSRodney W. Grimes  *	memory pages belonging to that object; this list is
132df8bae1dSRodney W. Grimes  *	maintained by the "vm_page" module, and locked by the object's
133df8bae1dSRodney W. Grimes  *	lock.
134df8bae1dSRodney W. Grimes  *
135df8bae1dSRodney W. Grimes  *	Each object also records a "pager" routine which is
136df8bae1dSRodney W. Grimes  *	used to retrieve (and store) pages to the proper backing
137df8bae1dSRodney W. Grimes  *	storage.  In addition, objects may be backed by other
138df8bae1dSRodney W. Grimes  *	objects from which they were virtual-copied.
139df8bae1dSRodney W. Grimes  *
140df8bae1dSRodney W. Grimes  *	The only items within the object structure which are
141df8bae1dSRodney W. Grimes  *	modified after time of creation are:
142df8bae1dSRodney W. Grimes  *		reference count		locked by object's lock
143df8bae1dSRodney W. Grimes  *		pager routine		locked by object's lock
144df8bae1dSRodney W. Grimes  *
145df8bae1dSRodney W. Grimes  */
146df8bae1dSRodney W. Grimes 
14728f8db14SBruce Evans struct object_q vm_object_list;
148a5698387SAlan Cox struct mtx vm_object_list_mtx;	/* lock for object list and count */
149cccf11b8SAlan Cox 
150cccf11b8SAlan Cox struct vm_object kernel_object_store;
151df8bae1dSRodney W. Grimes 
1526472ac3dSEd Schouten static SYSCTL_NODE(_vm_stats, OID_AUTO, object, CTLFLAG_RD, 0,
1536472ac3dSEd Schouten     "VM object stats");
154604c2bbcSAlan Cox 
15511542376SAlan Cox static counter_u64_t object_collapses = EARLY_COUNTER;
15611542376SAlan Cox SYSCTL_COUNTER_U64(_vm_stats_object, OID_AUTO, collapses, CTLFLAG_RD,
15711542376SAlan Cox     &object_collapses,
15811542376SAlan Cox     "VM object collapses");
159604c2bbcSAlan Cox 
16011542376SAlan Cox static counter_u64_t object_bypasses = EARLY_COUNTER;
16111542376SAlan Cox SYSCTL_COUNTER_U64(_vm_stats_object, OID_AUTO, bypasses, CTLFLAG_RD,
16211542376SAlan Cox     &object_bypasses,
16311542376SAlan Cox     "VM object bypasses");
16411542376SAlan Cox 
16511542376SAlan Cox static void
16611542376SAlan Cox counter_startup(void)
16711542376SAlan Cox {
16811542376SAlan Cox 
16911542376SAlan Cox 	object_collapses = counter_u64_alloc(M_WAITOK);
17011542376SAlan Cox 	object_bypasses = counter_u64_alloc(M_WAITOK);
17111542376SAlan Cox }
17211542376SAlan Cox SYSINIT(object_counters, SI_SUB_CPU, SI_ORDER_ANY, counter_startup, NULL);
173dad740e9SAlan Cox 
174670d17b5SJeff Roberson static uma_zone_t obj_zone;
1758355f576SJeff Roberson 
176b23f72e9SBrian Feldman static int vm_object_zinit(void *mem, int size, int flags);
1778355f576SJeff Roberson 
1788355f576SJeff Roberson #ifdef INVARIANTS
1798355f576SJeff Roberson static void vm_object_zdtor(void *mem, int size, void *arg);
1808355f576SJeff Roberson 
1818355f576SJeff Roberson static void
1828355f576SJeff Roberson vm_object_zdtor(void *mem, int size, void *arg)
1838355f576SJeff Roberson {
1848355f576SJeff Roberson 	vm_object_t object;
1858355f576SJeff Roberson 
1868355f576SJeff Roberson 	object = (vm_object_t)mem;
187e735691bSJohn Baldwin 	KASSERT(object->ref_count == 0,
188e735691bSJohn Baldwin 	    ("object %p ref_count = %d", object, object->ref_count));
18943186e53SAlan Cox 	KASSERT(TAILQ_EMPTY(&object->memq),
190198da1b2SAttilio Rao 	    ("object %p has resident pages in its memq", object));
191774d251dSAttilio Rao 	KASSERT(vm_radix_is_empty(&object->rtree),
192774d251dSAttilio Rao 	    ("object %p has resident pages in its trie", object));
193f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
194f8a47341SAlan Cox 	KASSERT(LIST_EMPTY(&object->rvq),
195f8a47341SAlan Cox 	    ("object %p has reservations",
196f8a47341SAlan Cox 	    object));
197f8a47341SAlan Cox #endif
19811b57401SHans Petter Selasky 	KASSERT(REFCOUNT_COUNT(object->paging_in_progress) == 0,
1998355f576SJeff Roberson 	    ("object %p paging_in_progress = %d",
20011b57401SHans Petter Selasky 	    object, REFCOUNT_COUNT(object->paging_in_progress)));
2018355f576SJeff Roberson 	KASSERT(object->resident_page_count == 0,
2028355f576SJeff Roberson 	    ("object %p resident_page_count = %d",
2038355f576SJeff Roberson 	    object, object->resident_page_count));
2048355f576SJeff Roberson 	KASSERT(object->shadow_count == 0,
2058355f576SJeff Roberson 	    ("object %p shadow_count = %d",
2068355f576SJeff Roberson 	    object, object->shadow_count));
207e735691bSJohn Baldwin 	KASSERT(object->type == OBJT_DEAD,
208e735691bSJohn Baldwin 	    ("object %p has non-dead type %d",
209e735691bSJohn Baldwin 	    object, object->type));
2108355f576SJeff Roberson }
2118355f576SJeff Roberson #endif
2128355f576SJeff Roberson 
213b23f72e9SBrian Feldman static int
214b23f72e9SBrian Feldman vm_object_zinit(void *mem, int size, int flags)
2158355f576SJeff Roberson {
2168355f576SJeff Roberson 	vm_object_t object;
2178355f576SJeff Roberson 
2188355f576SJeff Roberson 	object = (vm_object_t)mem;
219777a36c5SAlan Cox 	rw_init_flags(&object->lock, "vm object", RW_DUPOK | RW_NEW);
2208355f576SJeff Roberson 
2218355f576SJeff Roberson 	/* These are true for any object that has been freed */
222e735691bSJohn Baldwin 	object->type = OBJT_DEAD;
223e735691bSJohn Baldwin 	object->ref_count = 0;
224cd1241fbSKonstantin Belousov 	vm_radix_init(&object->rtree);
225cf27e0d1SJeff Roberson 	refcount_init(&object->paging_in_progress, 0);
2268355f576SJeff Roberson 	object->resident_page_count = 0;
2278355f576SJeff Roberson 	object->shadow_count = 0;
228f425ab8eSKonstantin Belousov 	object->flags = OBJ_DEAD;
229e735691bSJohn Baldwin 
230e735691bSJohn Baldwin 	mtx_lock(&vm_object_list_mtx);
231e735691bSJohn Baldwin 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
232e735691bSJohn Baldwin 	mtx_unlock(&vm_object_list_mtx);
233b23f72e9SBrian Feldman 	return (0);
2348355f576SJeff Roberson }
235df8bae1dSRodney W. Grimes 
236a4915c21SAttilio Rao static void
2376395da54SIan Dowse _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
238df8bae1dSRodney W. Grimes {
2390cddd8f0SMatthew Dillon 
240df8bae1dSRodney W. Grimes 	TAILQ_INIT(&object->memq);
2411c500307SAlan Cox 	LIST_INIT(&object->shadow_head);
242a1f6d91cSDavid Greenman 
24324a1cce3SDavid Greenman 	object->type = type;
244f425ab8eSKonstantin Belousov 	if (type == OBJT_SWAP)
245f425ab8eSKonstantin Belousov 		pctrie_init(&object->un_pager.swp.swp_blks);
246f425ab8eSKonstantin Belousov 
247f425ab8eSKonstantin Belousov 	/*
248f425ab8eSKonstantin Belousov 	 * Ensure that swap_pager_swapoff() iteration over object_list
249f425ab8eSKonstantin Belousov 	 * sees up to date type and pctrie head if it observed
250f425ab8eSKonstantin Belousov 	 * non-dead object.
251f425ab8eSKonstantin Belousov 	 */
252f425ab8eSKonstantin Belousov 	atomic_thread_fence_rel();
253f425ab8eSKonstantin Belousov 
25428634820SAlan Cox 	switch (type) {
25528634820SAlan Cox 	case OBJT_DEAD:
25628634820SAlan Cox 		panic("_vm_object_allocate: can't create OBJT_DEAD");
25728634820SAlan Cox 	case OBJT_DEFAULT:
25828634820SAlan Cox 	case OBJT_SWAP:
25928634820SAlan Cox 		object->flags = OBJ_ONEMAPPING;
26028634820SAlan Cox 		break;
26128634820SAlan Cox 	case OBJT_DEVICE:
26228634820SAlan Cox 	case OBJT_SG:
26328634820SAlan Cox 		object->flags = OBJ_FICTITIOUS | OBJ_UNMANAGED;
26428634820SAlan Cox 		break;
26528634820SAlan Cox 	case OBJT_MGTDEVICE:
26628634820SAlan Cox 		object->flags = OBJ_FICTITIOUS;
26728634820SAlan Cox 		break;
26828634820SAlan Cox 	case OBJT_PHYS:
26928634820SAlan Cox 		object->flags = OBJ_UNMANAGED;
27028634820SAlan Cox 		break;
27128634820SAlan Cox 	case OBJT_VNODE:
27228634820SAlan Cox 		object->flags = 0;
27328634820SAlan Cox 		break;
27428634820SAlan Cox 	default:
27528634820SAlan Cox 		panic("_vm_object_allocate: type %d is undefined", type);
27628634820SAlan Cox 	}
277df8bae1dSRodney W. Grimes 	object->size = size;
2784c29d2deSMark Johnston 	object->domain.dr_policy = NULL;
279b881da26SAlan Cox 	object->generation = 1;
280a1f6d91cSDavid Greenman 	object->ref_count = 1;
2813153e878SAlan Cox 	object->memattr = VM_MEMATTR_DEFAULT;
282ef694c1aSEdward Tomasz Napierala 	object->cred = NULL;
2833364c323SKonstantin Belousov 	object->charge = 0;
28424a1cce3SDavid Greenman 	object->handle = NULL;
28524a1cce3SDavid Greenman 	object->backing_object = NULL;
286a316d390SJohn Dyson 	object->backing_object_offset = (vm_ooffset_t) 0;
287f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
288f8a47341SAlan Cox 	LIST_INIT(&object->rvq);
289f8a47341SAlan Cox #endif
2901bdbd705SKonstantin Belousov 	umtx_shm_object_init(object);
291df8bae1dSRodney W. Grimes }
292df8bae1dSRodney W. Grimes 
293df8bae1dSRodney W. Grimes /*
29426f9a767SRodney W. Grimes  *	vm_object_init:
29526f9a767SRodney W. Grimes  *
29626f9a767SRodney W. Grimes  *	Initialize the VM objects module.
29726f9a767SRodney W. Grimes  */
29826f9a767SRodney W. Grimes void
2991b40f8c0SMatthew Dillon vm_object_init(void)
30026f9a767SRodney W. Grimes {
30126f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_list);
3026008862bSJohn Baldwin 	mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF);
3030217125fSDavid Greenman 
30489f6b863SAttilio Rao 	rw_init(&kernel_object->lock, "kernel vm object");
305d1780e8dSKonstantin Belousov 	_vm_object_allocate(OBJT_PHYS, atop(VM_MAX_KERNEL_ADDRESS -
306d1780e8dSKonstantin Belousov 	    VM_MIN_KERNEL_ADDRESS), kernel_object);
307f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
308f8a47341SAlan Cox 	kernel_object->flags |= OBJ_COLORED;
309f8a47341SAlan Cox 	kernel_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS);
310f8a47341SAlan Cox #endif
31126f9a767SRodney W. Grimes 
3128dbca793STor Egge 	/*
3138dbca793STor Egge 	 * The lock portion of struct vm_object must be type stable due
3148dbca793STor Egge 	 * to vm_pageout_fallback_object_lock locking a vm object
3158dbca793STor Egge 	 * without holding any references to it.
3168dbca793STor Egge 	 */
3178355f576SJeff Roberson 	obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL,
3188355f576SJeff Roberson #ifdef INVARIANTS
3198355f576SJeff Roberson 	    vm_object_zdtor,
3208355f576SJeff Roberson #else
3218355f576SJeff Roberson 	    NULL,
3228355f576SJeff Roberson #endif
3235df87b21SJeff Roberson 	    vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
324774d251dSAttilio Rao 
325cd1241fbSKonstantin Belousov 	vm_radix_zinit();
32699448ed1SJohn Dyson }
32799448ed1SJohn Dyson 
32899448ed1SJohn Dyson void
3291b40f8c0SMatthew Dillon vm_object_clear_flag(vm_object_t object, u_short bits)
3301b40f8c0SMatthew Dillon {
3315440b5a9SAlan Cox 
33289f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
333b06805adSJake Burkholder 	object->flags &= ~bits;
3341b40f8c0SMatthew Dillon }
3351b40f8c0SMatthew Dillon 
3363153e878SAlan Cox /*
3373153e878SAlan Cox  *	Sets the default memory attribute for the specified object.  Pages
3383153e878SAlan Cox  *	that are allocated to this object are by default assigned this memory
3393153e878SAlan Cox  *	attribute.
3403153e878SAlan Cox  *
3413153e878SAlan Cox  *	Presently, this function must be called before any pages are allocated
3423153e878SAlan Cox  *	to the object.  In the future, this requirement may be relaxed for
3433153e878SAlan Cox  *	"default" and "swap" objects.
3443153e878SAlan Cox  */
3453153e878SAlan Cox int
3463153e878SAlan Cox vm_object_set_memattr(vm_object_t object, vm_memattr_t memattr)
3473153e878SAlan Cox {
3483153e878SAlan Cox 
34989f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
3503153e878SAlan Cox 	switch (object->type) {
3513153e878SAlan Cox 	case OBJT_DEFAULT:
3523153e878SAlan Cox 	case OBJT_DEVICE:
35396b0b92aSAlan Cox 	case OBJT_MGTDEVICE:
3543153e878SAlan Cox 	case OBJT_PHYS:
35501381811SJohn Baldwin 	case OBJT_SG:
3563153e878SAlan Cox 	case OBJT_SWAP:
3573153e878SAlan Cox 	case OBJT_VNODE:
3583153e878SAlan Cox 		if (!TAILQ_EMPTY(&object->memq))
3593153e878SAlan Cox 			return (KERN_FAILURE);
3603153e878SAlan Cox 		break;
3613153e878SAlan Cox 	case OBJT_DEAD:
3623153e878SAlan Cox 		return (KERN_INVALID_ARGUMENT);
36396b0b92aSAlan Cox 	default:
36496b0b92aSAlan Cox 		panic("vm_object_set_memattr: object %p is of undefined type",
36596b0b92aSAlan Cox 		    object);
3663153e878SAlan Cox 	}
3673153e878SAlan Cox 	object->memattr = memattr;
3683153e878SAlan Cox 	return (KERN_SUCCESS);
3693153e878SAlan Cox }
3703153e878SAlan Cox 
3711b40f8c0SMatthew Dillon void
3721b40f8c0SMatthew Dillon vm_object_pip_add(vm_object_t object, short i)
3731b40f8c0SMatthew Dillon {
374f279b88dSAlan Cox 
375cf27e0d1SJeff Roberson 	refcount_acquiren(&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 
382cf27e0d1SJeff Roberson 	refcount_release(&object->paging_in_progress);
3831b40f8c0SMatthew Dillon }
3841b40f8c0SMatthew Dillon 
3851b40f8c0SMatthew Dillon void
3861b40f8c0SMatthew Dillon vm_object_pip_wakeupn(vm_object_t object, short i)
3871b40f8c0SMatthew Dillon {
388d647a0edSAlan Cox 
389cf27e0d1SJeff Roberson 	refcount_releasen(&object->paging_in_progress, i);
3901b40f8c0SMatthew Dillon }
3911b40f8c0SMatthew Dillon 
3921b40f8c0SMatthew Dillon void
3931b40f8c0SMatthew Dillon vm_object_pip_wait(vm_object_t object, char *waitid)
3941b40f8c0SMatthew Dillon {
3951ca58953SAlan Cox 
39689f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
397cf27e0d1SJeff Roberson 
39811b57401SHans Petter Selasky 	while (REFCOUNT_COUNT(object->paging_in_progress) > 0) {
399cf27e0d1SJeff Roberson 		VM_OBJECT_WUNLOCK(object);
400cf27e0d1SJeff Roberson 		refcount_wait(&object->paging_in_progress, waitid, PVM);
401cf27e0d1SJeff Roberson 		VM_OBJECT_WLOCK(object);
4021ca58953SAlan Cox 	}
4031b40f8c0SMatthew Dillon }
4041b40f8c0SMatthew Dillon 
405cf27e0d1SJeff Roberson void
406cf27e0d1SJeff Roberson vm_object_pip_wait_unlocked(vm_object_t object, char *waitid)
407cf27e0d1SJeff Roberson {
408cf27e0d1SJeff Roberson 
409cf27e0d1SJeff Roberson 	VM_OBJECT_ASSERT_UNLOCKED(object);
410cf27e0d1SJeff Roberson 
41111b57401SHans Petter Selasky 	while (REFCOUNT_COUNT(object->paging_in_progress) > 0)
412cf27e0d1SJeff Roberson 		refcount_wait(&object->paging_in_progress, waitid, PVM);
413cf27e0d1SJeff Roberson }
414cf27e0d1SJeff Roberson 
41526f9a767SRodney W. Grimes /*
41626f9a767SRodney W. Grimes  *	vm_object_allocate:
41726f9a767SRodney W. Grimes  *
41826f9a767SRodney W. Grimes  *	Returns a new object with the given size.
41926f9a767SRodney W. Grimes  */
42026f9a767SRodney W. Grimes vm_object_t
4216395da54SIan Dowse vm_object_allocate(objtype_t type, vm_pindex_t size)
42226f9a767SRodney W. Grimes {
42390688d13SAlan Cox 	vm_object_t object;
42490688d13SAlan Cox 
42590688d13SAlan Cox 	object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK);
42690688d13SAlan Cox 	_vm_object_allocate(type, size, object);
42790688d13SAlan Cox 	return (object);
42826f9a767SRodney W. Grimes }
42926f9a767SRodney W. Grimes 
43026f9a767SRodney W. Grimes 
43126f9a767SRodney W. Grimes /*
432df8bae1dSRodney W. Grimes  *	vm_object_reference:
433df8bae1dSRodney W. Grimes  *
43415347817SAlan Cox  *	Gets another reference to the given object.  Note: OBJ_DEAD
43515347817SAlan Cox  *	objects can be referenced during final cleaning.
436df8bae1dSRodney W. Grimes  */
4376476c0d2SJohn Dyson void
4381b40f8c0SMatthew Dillon vm_object_reference(vm_object_t object)
439df8bae1dSRodney W. Grimes {
440df8bae1dSRodney W. Grimes 	if (object == NULL)
441df8bae1dSRodney W. Grimes 		return;
44289f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
44352481a9aSJeff Roberson 	vm_object_reference_locked(object);
44489f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
44595e5e988SJohn Dyson }
44695e5e988SJohn Dyson 
44723955314SAlfred Perlstein /*
448b921a12bSAlan Cox  *	vm_object_reference_locked:
449b921a12bSAlan Cox  *
450b921a12bSAlan Cox  *	Gets another reference to the given object.
451b921a12bSAlan Cox  *
452b921a12bSAlan Cox  *	The object must be locked.
453b921a12bSAlan Cox  */
454b921a12bSAlan Cox void
455b921a12bSAlan Cox vm_object_reference_locked(vm_object_t object)
456b921a12bSAlan Cox {
457b921a12bSAlan Cox 	struct vnode *vp;
458b921a12bSAlan Cox 
45989f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
460b921a12bSAlan Cox 	object->ref_count++;
461b921a12bSAlan Cox 	if (object->type == OBJT_VNODE) {
462b921a12bSAlan Cox 		vp = object->handle;
463b921a12bSAlan Cox 		vref(vp);
464b921a12bSAlan Cox 	}
465b921a12bSAlan Cox }
466b921a12bSAlan Cox 
467b921a12bSAlan Cox /*
4689d5abbddSJens Schweikhardt  * Handle deallocating an object of type OBJT_VNODE.
46923955314SAlfred Perlstein  */
47002dd8331SAlan Cox static void
4711b40f8c0SMatthew Dillon vm_object_vndeallocate(vm_object_t object)
47295e5e988SJohn Dyson {
47395e5e988SJohn Dyson 	struct vnode *vp = (struct vnode *) object->handle;
474219cbf59SEivind Eklund 
47589f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
4765526d2d9SEivind Eklund 	KASSERT(object->type == OBJT_VNODE,
4775526d2d9SEivind Eklund 	    ("vm_object_vndeallocate: not a vnode object"));
478219cbf59SEivind Eklund 	KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
479219cbf59SEivind Eklund #ifdef INVARIANTS
48095e5e988SJohn Dyson 	if (object->ref_count == 0) {
481411455a8SEdward Tomasz Napierala 		vn_printf(vp, "vm_object_vndeallocate ");
48295e5e988SJohn Dyson 		panic("vm_object_vndeallocate: bad object reference count");
48395e5e988SJohn Dyson 	}
48495e5e988SJohn Dyson #endif
48595e5e988SJohn Dyson 
4862a339d9eSKonstantin Belousov 	if (!umtx_shm_vnobj_persistent && object->ref_count == 1)
4871bdbd705SKonstantin Belousov 		umtx_shm_object_terminated(object);
4881bdbd705SKonstantin Belousov 
48995e5e988SJohn Dyson 	object->ref_count--;
49078022527SKonstantin Belousov 
49103fa5b34SKonstantin Belousov 	/* vrele may need the vnode lock. */
49278022527SKonstantin Belousov 	VM_OBJECT_WUNLOCK(object);
49347221757SJohn Dyson 	vrele(vp);
49486769ac0SKonstantin Belousov }
495df8bae1dSRodney W. Grimes 
496df8bae1dSRodney W. Grimes /*
497df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
498df8bae1dSRodney W. Grimes  *
499df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
500df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
501df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
502df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
503df8bae1dSRodney W. Grimes  *	may be relinquished.
504df8bae1dSRodney W. Grimes  *
505df8bae1dSRodney W. Grimes  *	No object may be locked.
506df8bae1dSRodney W. Grimes  */
50726f9a767SRodney W. Grimes void
5081b40f8c0SMatthew Dillon vm_object_deallocate(vm_object_t object)
509df8bae1dSRodney W. Grimes {
510df8bae1dSRodney W. Grimes 	vm_object_t temp;
511df8bae1dSRodney W. Grimes 
512df8bae1dSRodney W. Grimes 	while (object != NULL) {
51389f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
5143b68228cSAlan Cox 		if (object->type == OBJT_VNODE) {
51595e5e988SJohn Dyson 			vm_object_vndeallocate(object);
51623b186d3SAlan Cox 			return;
5175050aa86SKonstantin Belousov 		}
51895e5e988SJohn Dyson 
5198125b1e6SAlfred Perlstein 		KASSERT(object->ref_count != 0,
5208125b1e6SAlfred Perlstein 			("vm_object_deallocate: object deallocated too many times: %d", object->type));
5212be70f79SJohn Dyson 
5222be70f79SJohn Dyson 		/*
5238125b1e6SAlfred Perlstein 		 * If the reference count goes to 0 we start calling
5248125b1e6SAlfred Perlstein 		 * vm_object_terminate() on the object chain.
5258125b1e6SAlfred Perlstein 		 * A ref count of 1 may be a special case depending on the
5268125b1e6SAlfred Perlstein 		 * shadow count being 0 or 1.
5272be70f79SJohn Dyson 		 */
528c0877f10SJohn Dyson 		object->ref_count--;
5298125b1e6SAlfred Perlstein 		if (object->ref_count > 1) {
53089f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
53123b186d3SAlan Cox 			return;
5328125b1e6SAlfred Perlstein 		} else if (object->ref_count == 1) {
5334c8e0452SAlan Cox 			if (object->shadow_count == 0 &&
5344c8e0452SAlan Cox 			    object->handle == NULL &&
5354c8e0452SAlan Cox 			    (object->type == OBJT_DEFAULT ||
5366f2af3fcSKonstantin Belousov 			    (object->type == OBJT_SWAP &&
537f08f7dcaSKonstantin Belousov 			    (object->flags & OBJ_TMPFS_NODE) == 0))) {
5388125b1e6SAlfred Perlstein 				vm_object_set_flag(object, OBJ_ONEMAPPING);
5398125b1e6SAlfred Perlstein 			} else if ((object->shadow_count == 1) &&
5408125b1e6SAlfred Perlstein 			    (object->handle == NULL) &&
54124a1cce3SDavid Greenman 			    (object->type == OBJT_DEFAULT ||
54224a1cce3SDavid Greenman 			     object->type == OBJT_SWAP)) {
543a1f6d91cSDavid Greenman 				vm_object_t robject;
54495e5e988SJohn Dyson 
5451c500307SAlan Cox 				robject = LIST_FIRST(&object->shadow_head);
5465526d2d9SEivind Eklund 				KASSERT(robject != NULL,
547219cbf59SEivind Eklund 				    ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
5485526d2d9SEivind Eklund 					 object->ref_count,
5495526d2d9SEivind Eklund 					 object->shadow_count));
5504bace8e7SKonstantin Belousov 				KASSERT((robject->flags & OBJ_TMPFS_NODE) == 0,
5514bace8e7SKonstantin Belousov 				    ("shadowed tmpfs v_object %p", object));
55289f6b863SAttilio Rao 				if (!VM_OBJECT_TRYWLOCK(robject)) {
553b72b0115SAlan Cox 					/*
554b72b0115SAlan Cox 					 * Avoid a potential deadlock.
555b72b0115SAlan Cox 					 */
556b72b0115SAlan Cox 					object->ref_count++;
55789f6b863SAttilio Rao 					VM_OBJECT_WUNLOCK(object);
558a7d86121SAlan Cox 					/*
559a7d86121SAlan Cox 					 * More likely than not the thread
560a7d86121SAlan Cox 					 * holding robject's lock has lower
561a7d86121SAlan Cox 					 * priority than the current thread.
562a7d86121SAlan Cox 					 * Let the lower priority thread run.
563a7d86121SAlan Cox 					 */
5648db5fc58SJohn Baldwin 					pause("vmo_de", 1);
565b72b0115SAlan Cox 					continue;
566b72b0115SAlan Cox 				}
567d936694fSAlan Cox 				/*
568d936694fSAlan Cox 				 * Collapse object into its shadow unless its
569d936694fSAlan Cox 				 * shadow is dead.  In that case, object will
570d936694fSAlan Cox 				 * be deallocated by the thread that is
571d936694fSAlan Cox 				 * deallocating its shadow.
572d936694fSAlan Cox 				 */
573d936694fSAlan Cox 				if ((robject->flags & OBJ_DEAD) == 0 &&
574d936694fSAlan Cox 				    (robject->handle == NULL) &&
57524a1cce3SDavid Greenman 				    (robject->type == OBJT_DEFAULT ||
57624a1cce3SDavid Greenman 				     robject->type == OBJT_SWAP)) {
577a1f6d91cSDavid Greenman 
57895e5e988SJohn Dyson 					robject->ref_count++;
579138449dcSAlan Cox retry:
58011b57401SHans Petter Selasky 					if (REFCOUNT_COUNT(robject->paging_in_progress) > 0) {
58189f6b863SAttilio Rao 						VM_OBJECT_WUNLOCK(object);
582138449dcSAlan Cox 						vm_object_pip_wait(robject,
583138449dcSAlan Cox 						    "objde1");
5842e9f4a69SAlan Cox 						temp = robject->backing_object;
5852e9f4a69SAlan Cox 						if (object == temp) {
58689f6b863SAttilio Rao 							VM_OBJECT_WLOCK(object);
587138449dcSAlan Cox 							goto retry;
5882e9f4a69SAlan Cox 						}
58911b57401SHans Petter Selasky 					} else if (REFCOUNT_COUNT(object->paging_in_progress) > 0) {
59089f6b863SAttilio Rao 						VM_OBJECT_WUNLOCK(robject);
591cf27e0d1SJeff Roberson 						VM_OBJECT_WUNLOCK(object);
592cf27e0d1SJeff Roberson 						refcount_wait(
593cf27e0d1SJeff Roberson 						    &object->paging_in_progress,
594cf27e0d1SJeff Roberson 						    "objde2", PVM);
59589f6b863SAttilio Rao 						VM_OBJECT_WLOCK(robject);
5962e9f4a69SAlan Cox 						temp = robject->backing_object;
5972e9f4a69SAlan Cox 						if (object == temp) {
59889f6b863SAttilio Rao 							VM_OBJECT_WLOCK(object);
599138449dcSAlan Cox 							goto retry;
600a1f6d91cSDavid Greenman 						}
6012e9f4a69SAlan Cox 					} else
60289f6b863SAttilio Rao 						VM_OBJECT_WUNLOCK(object);
6032e9f4a69SAlan Cox 
60495e5e988SJohn Dyson 					if (robject->ref_count == 1) {
60595e5e988SJohn Dyson 						robject->ref_count--;
606ba8da839SDavid Greenman 						object = robject;
60795e5e988SJohn Dyson 						goto doterm;
60895e5e988SJohn Dyson 					}
60995e5e988SJohn Dyson 					object = robject;
61095e5e988SJohn Dyson 					vm_object_collapse(object);
61189f6b863SAttilio Rao 					VM_OBJECT_WUNLOCK(object);
612ba8da839SDavid Greenman 					continue;
613a1f6d91cSDavid Greenman 				}
61489f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(robject);
61595e5e988SJohn Dyson 			}
61689f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
61723b186d3SAlan Cox 			return;
61895e5e988SJohn Dyson 		}
61995e5e988SJohn Dyson doterm:
6201bdbd705SKonstantin Belousov 		umtx_shm_object_terminated(object);
62124a1cce3SDavid Greenman 		temp = object->backing_object;
622c9917419SAlan Cox 		if (temp != NULL) {
6234bace8e7SKonstantin Belousov 			KASSERT((object->flags & OBJ_TMPFS_NODE) == 0,
6244bace8e7SKonstantin Belousov 			    ("shadowed tmpfs v_object 2 %p", object));
62589f6b863SAttilio Rao 			VM_OBJECT_WLOCK(temp);
6261c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
62795e5e988SJohn Dyson 			temp->shadow_count--;
62889f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(temp);
62995461b45SJohn Dyson 			object->backing_object = NULL;
630de5f6a77SJohn Dyson 		}
631245df27cSMatthew Dillon 		/*
632245df27cSMatthew Dillon 		 * Don't double-terminate, we could be in a termination
633245df27cSMatthew Dillon 		 * recursion due to the terminate having to sync data
634245df27cSMatthew Dillon 		 * to disk.
635245df27cSMatthew Dillon 		 */
636783a68aaSKonstantin Belousov 		if ((object->flags & OBJ_DEAD) == 0) {
637783a68aaSKonstantin Belousov 			vm_object_set_flag(object, OBJ_DEAD);
638df8bae1dSRodney W. Grimes 			vm_object_terminate(object);
639783a68aaSKonstantin Belousov 		} else
64089f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
641df8bae1dSRodney W. Grimes 		object = temp;
642df8bae1dSRodney W. Grimes 	}
643df8bae1dSRodney W. Grimes }
644df8bae1dSRodney W. Grimes 
645df8bae1dSRodney W. Grimes /*
6462ac78f0eSStephan Uphoff  *	vm_object_destroy removes the object from the global object list
6472ac78f0eSStephan Uphoff  *      and frees the space for the object.
6482ac78f0eSStephan Uphoff  */
6492ac78f0eSStephan Uphoff void
6502ac78f0eSStephan Uphoff vm_object_destroy(vm_object_t object)
6512ac78f0eSStephan Uphoff {
6522ac78f0eSStephan Uphoff 
6532ac78f0eSStephan Uphoff 	/*
6543364c323SKonstantin Belousov 	 * Release the allocation charge.
6553364c323SKonstantin Belousov 	 */
656ef694c1aSEdward Tomasz Napierala 	if (object->cred != NULL) {
657ef694c1aSEdward Tomasz Napierala 		swap_release_by_cred(object->charge, object->cred);
6583364c323SKonstantin Belousov 		object->charge = 0;
659ef694c1aSEdward Tomasz Napierala 		crfree(object->cred);
660ef694c1aSEdward Tomasz Napierala 		object->cred = NULL;
6613364c323SKonstantin Belousov 	}
6623364c323SKonstantin Belousov 
6633364c323SKonstantin Belousov 	/*
6642ac78f0eSStephan Uphoff 	 * Free the space for the object.
6652ac78f0eSStephan Uphoff 	 */
6662ac78f0eSStephan Uphoff 	uma_zfree(obj_zone, object);
6672ac78f0eSStephan Uphoff }
6682ac78f0eSStephan Uphoff 
6692ac78f0eSStephan Uphoff /*
6707bbdb843SRuslan Bukin  *	vm_object_terminate_pages removes any remaining pageable pages
6717bbdb843SRuslan Bukin  *	from the object and resets the object to an empty state.
6727bbdb843SRuslan Bukin  */
6737bbdb843SRuslan Bukin static void
6747bbdb843SRuslan Bukin vm_object_terminate_pages(vm_object_t object)
6757bbdb843SRuslan Bukin {
6767bbdb843SRuslan Bukin 	vm_page_t p, p_next;
6777bbdb843SRuslan Bukin 
6787bbdb843SRuslan Bukin 	VM_OBJECT_ASSERT_WLOCKED(object);
6797bbdb843SRuslan Bukin 
6807bbdb843SRuslan Bukin 	/*
6817bbdb843SRuslan Bukin 	 * Free any remaining pageable pages.  This also removes them from the
6827bbdb843SRuslan Bukin 	 * paging queues.  However, don't free wired pages, just remove them
6837bbdb843SRuslan Bukin 	 * from the object.  Rather than incrementally removing each page from
6847bbdb843SRuslan Bukin 	 * the object, the page and object are reset to any empty state.
6857bbdb843SRuslan Bukin 	 */
6867bbdb843SRuslan Bukin 	TAILQ_FOREACH_SAFE(p, &object->memq, listq, p_next) {
6877bbdb843SRuslan Bukin 		vm_page_assert_unbusied(p);
688fee2a2faSMark Johnston 		KASSERT(p->object == object &&
689fee2a2faSMark Johnston 		    (p->ref_count & VPRC_OBJREF) != 0,
690fee2a2faSMark Johnston 		    ("vm_object_terminate_pages: page %p is inconsistent", p));
691fee2a2faSMark Johnston 
6927bbdb843SRuslan Bukin 		p->object = NULL;
693fee2a2faSMark Johnston 		if (vm_page_drop(p, VPRC_OBJREF) == VPRC_OBJREF) {
6945cd29d0fSMark Johnston 			VM_CNT_INC(v_pfree);
6955cd29d0fSMark Johnston 			vm_page_free(p);
6964074d642SAlan Cox 		}
697fee2a2faSMark Johnston 	}
6982fcd1ff6SKonstantin Belousov 
6997bbdb843SRuslan Bukin 	/*
7007bbdb843SRuslan Bukin 	 * If the object contained any pages, then reset it to an empty state.
7017bbdb843SRuslan Bukin 	 * None of the object's fields, including "resident_page_count", were
7027bbdb843SRuslan Bukin 	 * modified by the preceding loop.
7037bbdb843SRuslan Bukin 	 */
7047bbdb843SRuslan Bukin 	if (object->resident_page_count != 0) {
7057bbdb843SRuslan Bukin 		vm_radix_reclaim_allnodes(&object->rtree);
7067bbdb843SRuslan Bukin 		TAILQ_INIT(&object->memq);
7077bbdb843SRuslan Bukin 		object->resident_page_count = 0;
7087bbdb843SRuslan Bukin 		if (object->type == OBJT_VNODE)
7097bbdb843SRuslan Bukin 			vdrop(object->handle);
7107bbdb843SRuslan Bukin 	}
7117bbdb843SRuslan Bukin }
7127bbdb843SRuslan Bukin 
7137bbdb843SRuslan Bukin /*
714df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
715df8bae1dSRodney W. Grimes  *	up all previously used resources.
716df8bae1dSRodney W. Grimes  *
717df8bae1dSRodney W. Grimes  *	The object must be locked.
7181c7c3c6aSMatthew Dillon  *	This routine may block.
719df8bae1dSRodney W. Grimes  */
72095e5e988SJohn Dyson void
7211b40f8c0SMatthew Dillon vm_object_terminate(vm_object_t object)
722df8bae1dSRodney W. Grimes {
72389f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
724783a68aaSKonstantin Belousov 	KASSERT((object->flags & OBJ_DEAD) != 0,
725783a68aaSKonstantin Belousov 	    ("terminating non-dead obj %p", object));
726bef608bdSJohn Dyson 
727cf27e0d1SJeff Roberson 	/*
728cf27e0d1SJeff Roberson 	 * wait for the pageout daemon to be done with the object
729cf27e0d1SJeff Roberson 	 */
730cf27e0d1SJeff Roberson 	vm_object_pip_wait(object, "objtrm");
731cf27e0d1SJeff Roberson 
73211b57401SHans Petter Selasky 	KASSERT(!REFCOUNT_COUNT(object->paging_in_progress),
733cf27e0d1SJeff Roberson 		("vm_object_terminate: pageout in progress"));
734cf27e0d1SJeff Roberson 
735971dd342SAlfred Perlstein 	KASSERT(object->ref_count == 0,
736971dd342SAlfred Perlstein 		("vm_object_terminate: object with references, ref_count=%d",
737971dd342SAlfred Perlstein 		object->ref_count));
738996c772fSJohn Dyson 
7397bbdb843SRuslan Bukin 	if ((object->flags & OBJ_PG_DTOR) == 0)
7407bbdb843SRuslan Bukin 		vm_object_terminate_pages(object);
741bef608bdSJohn Dyson 
742f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
743f8a47341SAlan Cox 	if (__predict_false(!LIST_EMPTY(&object->rvq)))
744f8a47341SAlan Cox 		vm_reserv_break_all(object);
745f8a47341SAlan Cox #endif
7467bfda801SAlan Cox 
747e735691bSJohn Baldwin 	KASSERT(object->cred == NULL || object->type == OBJT_DEFAULT ||
748e735691bSJohn Baldwin 	    object->type == OBJT_SWAP,
749e735691bSJohn Baldwin 	    ("%s: non-swap obj %p has cred", __func__, object));
750e735691bSJohn Baldwin 
7512d8acc0fSJohn Dyson 	/*
7529fcfb650SDavid Greenman 	 * Let the pager know object is dead.
7539fcfb650SDavid Greenman 	 */
7549fcfb650SDavid Greenman 	vm_pager_deallocate(object);
75589f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
7569fcfb650SDavid Greenman 
7572ac78f0eSStephan Uphoff 	vm_object_destroy(object);
75847221757SJohn Dyson }
759df8bae1dSRodney W. Grimes 
760edf93b25SAlan Cox /*
761edf93b25SAlan Cox  * Make the page read-only so that we can clear the object flags.  However, if
762edf93b25SAlan Cox  * this is a nosync mmap then the object is likely to stay dirty so do not
763edf93b25SAlan Cox  * mess with the page and do not clear the object flags.  Returns TRUE if the
764edf93b25SAlan Cox  * page should be flushed, and FALSE otherwise.
765edf93b25SAlan Cox  */
7663280870dSKonstantin Belousov static boolean_t
767126d6082SKonstantin Belousov vm_object_page_remove_write(vm_page_t p, int flags, boolean_t *clearobjflags)
7683280870dSKonstantin Belousov {
7693280870dSKonstantin Belousov 
7703280870dSKonstantin Belousov 	/*
7713280870dSKonstantin Belousov 	 * If we have been asked to skip nosync pages and this is a
7723280870dSKonstantin Belousov 	 * nosync page, skip it.  Note that the object flags were not
7733280870dSKonstantin Belousov 	 * cleared in this case so we do not have to set them.
7743280870dSKonstantin Belousov 	 */
7753280870dSKonstantin Belousov 	if ((flags & OBJPC_NOSYNC) != 0 && (p->oflags & VPO_NOSYNC) != 0) {
776126d6082SKonstantin Belousov 		*clearobjflags = FALSE;
7773280870dSKonstantin Belousov 		return (FALSE);
7783280870dSKonstantin Belousov 	} else {
7793280870dSKonstantin Belousov 		pmap_remove_write(p);
7803280870dSKonstantin Belousov 		return (p->dirty != 0);
7813280870dSKonstantin Belousov 	}
7823280870dSKonstantin Belousov }
7833280870dSKonstantin Belousov 
784df8bae1dSRodney W. Grimes /*
785df8bae1dSRodney W. Grimes  *	vm_object_page_clean
786df8bae1dSRodney W. Grimes  *
7874f79d873SMatthew Dillon  *	Clean all dirty pages in the specified range of object.  Leaves page
7884f79d873SMatthew Dillon  * 	on whatever queue it is currently on.   If NOSYNC is set then do not
789b146f9e5SAlan Cox  *	write out pages with VPO_NOSYNC set (originally comes from MAP_NOSYNC),
7904f79d873SMatthew Dillon  *	leaving the object dirty.
79126f9a767SRodney W. Grimes  *
79243b7990eSMatthew Dillon  *	When stuffing pages asynchronously, allow clustering.  XXX we need a
79343b7990eSMatthew Dillon  *	synchronous clustering mode implementation.
79443b7990eSMatthew Dillon  *
79526f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
79626f9a767SRodney W. Grimes  *
79726f9a767SRodney W. Grimes  *	The object must be locked.
798126d6082SKonstantin Belousov  *
799126d6082SKonstantin Belousov  *	Returns FALSE if some page from the range was not written, as
800126d6082SKonstantin Belousov  *	reported by the pager, and TRUE otherwise.
80126f9a767SRodney W. Grimes  */
802126d6082SKonstantin Belousov boolean_t
80317f3095dSAlan Cox vm_object_page_clean(vm_object_t object, vm_ooffset_t start, vm_ooffset_t end,
804e239bb97SKonstantin Belousov     int flags)
805f6b04d2bSDavid Greenman {
806e239bb97SKonstantin Belousov 	vm_page_t np, p;
80717f3095dSAlan Cox 	vm_pindex_t pi, tend, tstart;
808126d6082SKonstantin Belousov 	int curgeneration, n, pagerflags;
809126d6082SKonstantin Belousov 	boolean_t clearobjflags, eio, res;
810f6b04d2bSDavid Greenman 
81189f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
812e5f299ffSKonstantin Belousov 
813e5f299ffSKonstantin Belousov 	/*
814e5f299ffSKonstantin Belousov 	 * The OBJ_MIGHTBEDIRTY flag is only set for OBJT_VNODE
815e5f299ffSKonstantin Belousov 	 * objects.  The check below prevents the function from
816e5f299ffSKonstantin Belousov 	 * operating on non-vnode objects.
817e5f299ffSKonstantin Belousov 	 */
818e239bb97SKonstantin Belousov 	if ((object->flags & OBJ_MIGHTBEDIRTY) == 0 ||
819e239bb97SKonstantin Belousov 	    object->resident_page_count == 0)
820126d6082SKonstantin Belousov 		return (TRUE);
821f6b04d2bSDavid Greenman 
822e239bb97SKonstantin Belousov 	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) != 0 ?
823e239bb97SKonstantin Belousov 	    VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
824e239bb97SKonstantin Belousov 	pagerflags |= (flags & OBJPC_INVAL) != 0 ? VM_PAGER_PUT_INVAL : 0;
825e239bb97SKonstantin Belousov 
82617f3095dSAlan Cox 	tstart = OFF_TO_IDX(start);
82717f3095dSAlan Cox 	tend = (end == 0) ? object->size : OFF_TO_IDX(end + PAGE_MASK);
82817f3095dSAlan Cox 	clearobjflags = tstart == 0 && tend >= object->size;
829126d6082SKonstantin Belousov 	res = TRUE;
830f6b04d2bSDavid Greenman 
831bd7e5f99SJohn Dyson rescan:
8322d8acc0fSJohn Dyson 	curgeneration = object->generation;
8332d8acc0fSJohn Dyson 
83417f3095dSAlan Cox 	for (p = vm_page_find_least(object, tstart); p != NULL; p = np) {
835bd7e5f99SJohn Dyson 		pi = p->pindex;
836e239bb97SKonstantin Belousov 		if (pi >= tend)
837e239bb97SKonstantin Belousov 			break;
838e239bb97SKonstantin Belousov 		np = TAILQ_NEXT(p, listq);
839e239bb97SKonstantin Belousov 		if (p->valid == 0)
840aef922f5SJohn Dyson 			continue;
84163e97555SJeff Roberson 		if (vm_page_busy_acquire(p, VM_ALLOC_WAITFAIL) == 0) {
842e65919f9SKonstantin Belousov 			if (object->generation != curgeneration) {
843e65919f9SKonstantin Belousov 				if ((flags & OBJPC_SYNC) != 0)
844e239bb97SKonstantin Belousov 					goto rescan;
845e65919f9SKonstantin Belousov 				else
846126d6082SKonstantin Belousov 					clearobjflags = FALSE;
847e65919f9SKonstantin Belousov 			}
848780636b7SKonstantin Belousov 			np = vm_page_find_least(object, pi);
849780636b7SKonstantin Belousov 			continue;
850f6b04d2bSDavid Greenman 		}
85163e97555SJeff Roberson 		if (!vm_object_page_remove_write(p, flags, &clearobjflags)) {
85263e97555SJeff Roberson 			vm_page_xunbusy(p);
853bd7e5f99SJohn Dyson 			continue;
85463e97555SJeff Roberson 		}
855e239bb97SKonstantin Belousov 
8563280870dSKonstantin Belousov 		n = vm_object_page_collect_flush(object, p, pagerflags,
857126d6082SKonstantin Belousov 		    flags, &clearobjflags, &eio);
858126d6082SKonstantin Belousov 		if (eio) {
859126d6082SKonstantin Belousov 			res = FALSE;
860126d6082SKonstantin Belousov 			clearobjflags = FALSE;
861126d6082SKonstantin Belousov 		}
862e65919f9SKonstantin Belousov 		if (object->generation != curgeneration) {
863e65919f9SKonstantin Belousov 			if ((flags & OBJPC_SYNC) != 0)
864b9b7a4beSMatthew Dillon 				goto rescan;
865e65919f9SKonstantin Belousov 			else
866126d6082SKonstantin Belousov 				clearobjflags = FALSE;
867e65919f9SKonstantin Belousov 		}
868031ec8c1SKonstantin Belousov 
869031ec8c1SKonstantin Belousov 		/*
870031ec8c1SKonstantin Belousov 		 * If the VOP_PUTPAGES() did a truncated write, so
871031ec8c1SKonstantin Belousov 		 * that even the first page of the run is not fully
872031ec8c1SKonstantin Belousov 		 * written, vm_pageout_flush() returns 0 as the run
873031ec8c1SKonstantin Belousov 		 * length.  Since the condition that caused truncated
874031ec8c1SKonstantin Belousov 		 * write may be permanent, e.g. exhausted free space,
875031ec8c1SKonstantin Belousov 		 * accepting n == 0 would cause an infinite loop.
876031ec8c1SKonstantin Belousov 		 *
877031ec8c1SKonstantin Belousov 		 * Forwarding the iterator leaves the unwritten page
878031ec8c1SKonstantin Belousov 		 * behind, but there is not much we can do there if
879031ec8c1SKonstantin Belousov 		 * filesystem refuses to write it.
880031ec8c1SKonstantin Belousov 		 */
881126d6082SKonstantin Belousov 		if (n == 0) {
882031ec8c1SKonstantin Belousov 			n = 1;
883126d6082SKonstantin Belousov 			clearobjflags = FALSE;
884126d6082SKonstantin Belousov 		}
885e239bb97SKonstantin Belousov 		np = vm_page_find_least(object, pi + n);
886b9b7a4beSMatthew Dillon 	}
887b9b7a4beSMatthew Dillon #if 0
888e239bb97SKonstantin Belousov 	VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC) ? MNT_WAIT : 0);
889b9b7a4beSMatthew Dillon #endif
890b9b7a4beSMatthew Dillon 
891edf93b25SAlan Cox 	if (clearobjflags)
8923280870dSKonstantin Belousov 		vm_object_clear_flag(object, OBJ_MIGHTBEDIRTY);
893126d6082SKonstantin Belousov 	return (res);
894b9b7a4beSMatthew Dillon }
895b9b7a4beSMatthew Dillon 
896b9b7a4beSMatthew Dillon static int
8973280870dSKonstantin Belousov vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int pagerflags,
898126d6082SKonstantin Belousov     int flags, boolean_t *clearobjflags, boolean_t *eio)
899b9b7a4beSMatthew Dillon {
9003157c503SKonstantin Belousov 	vm_page_t ma[vm_pageout_page_count], p_first, tp;
9013157c503SKonstantin Belousov 	int count, i, mreq, runlen;
902b9b7a4beSMatthew Dillon 
9037bec141bSKip Macy 	vm_page_lock_assert(p, MA_NOTOWNED);
90463e97555SJeff Roberson 	vm_page_assert_xbusied(p);
90589f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
9063157c503SKonstantin Belousov 
9073157c503SKonstantin Belousov 	count = 1;
9083157c503SKonstantin Belousov 	mreq = 0;
9093157c503SKonstantin Belousov 
9103157c503SKonstantin Belousov 	for (tp = p; count < vm_pageout_page_count; count++) {
9113157c503SKonstantin Belousov 		tp = vm_page_next(tp);
91263e97555SJeff Roberson 		if (tp == NULL || vm_page_tryxbusy(tp) == 0)
913bd7e5f99SJohn Dyson 			break;
91463e97555SJeff Roberson 		if (!vm_object_page_remove_write(tp, flags, clearobjflags)) {
91563e97555SJeff Roberson 			vm_page_xunbusy(tp);
916bd7e5f99SJohn Dyson 			break;
917bd7e5f99SJohn Dyson 		}
91863e97555SJeff Roberson 	}
919aef922f5SJohn Dyson 
9203157c503SKonstantin Belousov 	for (p_first = p; count < vm_pageout_page_count; count++) {
9213157c503SKonstantin Belousov 		tp = vm_page_prev(p_first);
92263e97555SJeff Roberson 		if (tp == NULL || vm_page_tryxbusy(tp) == 0)
923bd7e5f99SJohn Dyson 			break;
92463e97555SJeff Roberson 		if (!vm_object_page_remove_write(tp, flags, clearobjflags)) {
92563e97555SJeff Roberson 			vm_page_xunbusy(tp);
926bd7e5f99SJohn Dyson 			break;
92763e97555SJeff Roberson 		}
9283157c503SKonstantin Belousov 		p_first = tp;
9293157c503SKonstantin Belousov 		mreq++;
930bd7e5f99SJohn Dyson 	}
931bd7e5f99SJohn Dyson 
9323157c503SKonstantin Belousov 	for (tp = p_first, i = 0; i < count; tp = TAILQ_NEXT(tp, listq), i++)
9333157c503SKonstantin Belousov 		ma[i] = tp;
934cf2819ccSJohn Dyson 
935126d6082SKonstantin Belousov 	vm_pageout_flush(ma, count, pagerflags, mreq, &runlen, eio);
9361e8a675cSKonstantin Belousov 	return (runlen);
93726f9a767SRodney W. Grimes }
938df8bae1dSRodney W. Grimes 
9391efb74fbSJohn Dyson /*
940950f8459SAlan Cox  * Note that there is absolutely no sense in writing out
941950f8459SAlan Cox  * anonymous objects, so we track down the vnode object
942950f8459SAlan Cox  * to write out.
943950f8459SAlan Cox  * We invalidate (remove) all pages from the address space
944950f8459SAlan Cox  * for semantic correctness.
945950f8459SAlan Cox  *
9466bbee8e2SAlan Cox  * If the backing object is a device object with unmanaged pages, then any
9476bbee8e2SAlan Cox  * mappings to the specified range of pages must be removed before this
9486bbee8e2SAlan Cox  * function is called.
9496bbee8e2SAlan Cox  *
950950f8459SAlan Cox  * Note: certain anonymous maps, such as MAP_NOSYNC maps,
951950f8459SAlan Cox  * may start out with a NULL object.
952950f8459SAlan Cox  */
953126d6082SKonstantin Belousov boolean_t
954950f8459SAlan Cox vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size,
955950f8459SAlan Cox     boolean_t syncio, boolean_t invalidate)
956950f8459SAlan Cox {
957950f8459SAlan Cox 	vm_object_t backing_object;
958950f8459SAlan Cox 	struct vnode *vp;
9593b582b4eSTor Egge 	struct mount *mp;
960126d6082SKonstantin Belousov 	int error, flags, fsync_after;
961126d6082SKonstantin Belousov 	boolean_t res;
962950f8459SAlan Cox 
963950f8459SAlan Cox 	if (object == NULL)
964126d6082SKonstantin Belousov 		return (TRUE);
965126d6082SKonstantin Belousov 	res = TRUE;
966126d6082SKonstantin Belousov 	error = 0;
96789f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
968950f8459SAlan Cox 	while ((backing_object = object->backing_object) != NULL) {
96989f6b863SAttilio Rao 		VM_OBJECT_WLOCK(backing_object);
97056e0670fSAlan Cox 		offset += object->backing_object_offset;
97189f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
972950f8459SAlan Cox 		object = backing_object;
973950f8459SAlan Cox 		if (object->size < OFF_TO_IDX(offset + size))
974950f8459SAlan Cox 			size = IDX_TO_OFF(object->size) - offset;
975950f8459SAlan Cox 	}
976950f8459SAlan Cox 	/*
977950f8459SAlan Cox 	 * Flush pages if writing is allowed, invalidate them
978950f8459SAlan Cox 	 * if invalidation requested.  Pages undergoing I/O
979950f8459SAlan Cox 	 * will be ignored by vm_object_page_remove().
980950f8459SAlan Cox 	 *
981950f8459SAlan Cox 	 * We cannot lock the vnode and then wait for paging
982950f8459SAlan Cox 	 * to complete without deadlocking against vm_fault.
983950f8459SAlan Cox 	 * Instead we simply call vm_object_page_remove() and
984950f8459SAlan Cox 	 * allow it to block internally on a page-by-page
985950f8459SAlan Cox 	 * basis when it encounters pages undergoing async
986950f8459SAlan Cox 	 * I/O.
987950f8459SAlan Cox 	 */
988950f8459SAlan Cox 	if (object->type == OBJT_VNODE &&
9895bf94937SKonstantin Belousov 	    (object->flags & OBJ_MIGHTBEDIRTY) != 0 &&
9905bf94937SKonstantin Belousov 	    ((vp = object->handle)->v_vflag & VV_NOSYNC) == 0) {
99189f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
9923b582b4eSTor Egge 		(void) vn_start_write(vp, &mp, V_WAIT);
993cb05b60aSAttilio Rao 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
99475ff604aSKonstantin Belousov 		if (syncio && !invalidate && offset == 0 &&
995d1780e8dSKonstantin Belousov 		    atop(size) == object->size) {
99675ff604aSKonstantin Belousov 			/*
99775ff604aSKonstantin Belousov 			 * If syncing the whole mapping of the file,
99875ff604aSKonstantin Belousov 			 * it is faster to schedule all the writes in
99975ff604aSKonstantin Belousov 			 * async mode, also allowing the clustering,
100075ff604aSKonstantin Belousov 			 * and then wait for i/o to complete.
100175ff604aSKonstantin Belousov 			 */
100275ff604aSKonstantin Belousov 			flags = 0;
100375ff604aSKonstantin Belousov 			fsync_after = TRUE;
100475ff604aSKonstantin Belousov 		} else {
1005950f8459SAlan Cox 			flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
100675ff604aSKonstantin Belousov 			flags |= invalidate ? (OBJPC_SYNC | OBJPC_INVAL) : 0;
100775ff604aSKonstantin Belousov 			fsync_after = FALSE;
100875ff604aSKonstantin Belousov 		}
100989f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
1010126d6082SKonstantin Belousov 		res = vm_object_page_clean(object, offset, offset + size,
1011126d6082SKonstantin Belousov 		    flags);
101289f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
101375ff604aSKonstantin Belousov 		if (fsync_after)
1014126d6082SKonstantin Belousov 			error = VOP_FSYNC(vp, MNT_WAIT, curthread);
101522db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
10163b582b4eSTor Egge 		vn_finished_write(mp);
1017126d6082SKonstantin Belousov 		if (error != 0)
1018126d6082SKonstantin Belousov 			res = FALSE;
101989f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
1020950f8459SAlan Cox 	}
1021950f8459SAlan Cox 	if ((object->type == OBJT_VNODE ||
1022950f8459SAlan Cox 	     object->type == OBJT_DEVICE) && invalidate) {
10236bbee8e2SAlan Cox 		if (object->type == OBJT_DEVICE)
10246bbee8e2SAlan Cox 			/*
10256bbee8e2SAlan Cox 			 * The option OBJPR_NOTMAPPED must be passed here
10266bbee8e2SAlan Cox 			 * because vm_object_page_remove() cannot remove
10276bbee8e2SAlan Cox 			 * unmanaged mappings.
10286bbee8e2SAlan Cox 			 */
10296bbee8e2SAlan Cox 			flags = OBJPR_NOTMAPPED;
10306bbee8e2SAlan Cox 		else if (old_msync)
10316195b24aSKonstantin Belousov 			flags = 0;
10326bbee8e2SAlan Cox 		else
10336195b24aSKonstantin Belousov 			flags = OBJPR_CLEANONLY;
10346bbee8e2SAlan Cox 		vm_object_page_remove(object, OFF_TO_IDX(offset),
10356bbee8e2SAlan Cox 		    OFF_TO_IDX(offset + size + PAGE_MASK), flags);
1036950f8459SAlan Cox 	}
103789f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
1038126d6082SKonstantin Belousov 	return (res);
1039950f8459SAlan Cox }
1040950f8459SAlan Cox 
1041950f8459SAlan Cox /*
1042aa3650eaSMark Johnston  * Determine whether the given advice can be applied to the object.  Advice is
1043aa3650eaSMark Johnston  * not applied to unmanaged pages since they never belong to page queues, and
1044aa3650eaSMark Johnston  * since MADV_FREE is destructive, it can apply only to anonymous pages that
1045aa3650eaSMark Johnston  * have been mapped at most once.
1046aa3650eaSMark Johnston  */
1047aa3650eaSMark Johnston static bool
1048aa3650eaSMark Johnston vm_object_advice_applies(vm_object_t object, int advice)
1049aa3650eaSMark Johnston {
1050aa3650eaSMark Johnston 
1051aa3650eaSMark Johnston 	if ((object->flags & OBJ_UNMANAGED) != 0)
1052aa3650eaSMark Johnston 		return (false);
1053aa3650eaSMark Johnston 	if (advice != MADV_FREE)
1054aa3650eaSMark Johnston 		return (true);
1055aa3650eaSMark Johnston 	return ((object->type == OBJT_DEFAULT || object->type == OBJT_SWAP) &&
1056aa3650eaSMark Johnston 	    (object->flags & OBJ_ONEMAPPING) != 0);
1057aa3650eaSMark Johnston }
1058aa3650eaSMark Johnston 
1059aa3650eaSMark Johnston static void
1060aa3650eaSMark Johnston vm_object_madvise_freespace(vm_object_t object, int advice, vm_pindex_t pindex,
1061aa3650eaSMark Johnston     vm_size_t size)
1062aa3650eaSMark Johnston {
1063aa3650eaSMark Johnston 
1064aa3650eaSMark Johnston 	if (advice == MADV_FREE && object->type == OBJT_SWAP)
1065aa3650eaSMark Johnston 		swap_pager_freespace(object, pindex, size);
1066aa3650eaSMark Johnston }
1067aa3650eaSMark Johnston 
1068aa3650eaSMark Johnston /*
1069867a482dSJohn Dyson  *	vm_object_madvise:
1070867a482dSJohn Dyson  *
1071867a482dSJohn Dyson  *	Implements the madvise function at the object/page level.
10721c7c3c6aSMatthew Dillon  *
1073193b9358SAlan Cox  *	MADV_WILLNEED	(any object)
1074193b9358SAlan Cox  *
1075193b9358SAlan Cox  *	    Activate the specified pages if they are resident.
1076193b9358SAlan Cox  *
1077193b9358SAlan Cox  *	MADV_DONTNEED	(any object)
1078193b9358SAlan Cox  *
1079193b9358SAlan Cox  *	    Deactivate the specified pages if they are resident.
1080193b9358SAlan Cox  *
1081193b9358SAlan Cox  *	MADV_FREE	(OBJT_DEFAULT/OBJT_SWAP objects,
1082193b9358SAlan Cox  *			 OBJ_ONEMAPPING only)
1083193b9358SAlan Cox  *
1084193b9358SAlan Cox  *	    Deactivate and clean the specified pages if they are
1085193b9358SAlan Cox  *	    resident.  This permits the process to reuse the pages
1086193b9358SAlan Cox  *	    without faulting or the kernel to reclaim the pages
1087193b9358SAlan Cox  *	    without I/O.
1088867a482dSJohn Dyson  */
1089867a482dSJohn Dyson void
109092a59946SJohn Baldwin vm_object_madvise(vm_object_t object, vm_pindex_t pindex, vm_pindex_t end,
1091c2655a40SMark Johnston     int advice)
1092867a482dSJohn Dyson {
109392a59946SJohn Baldwin 	vm_pindex_t tpindex;
109434567de7SAlan Cox 	vm_object_t backing_object, tobject;
1095aa3650eaSMark Johnston 	vm_page_t m, tm;
1096867a482dSJohn Dyson 
1097867a482dSJohn Dyson 	if (object == NULL)
1098867a482dSJohn Dyson 		return;
1099c2655a40SMark Johnston 
11006e20a165SJohn Dyson relookup:
1101aa3650eaSMark Johnston 	VM_OBJECT_WLOCK(object);
1102aa3650eaSMark Johnston 	if (!vm_object_advice_applies(object, advice)) {
1103aa3650eaSMark Johnston 		VM_OBJECT_WUNLOCK(object);
1104aa3650eaSMark Johnston 		return;
11056e20a165SJohn Dyson 	}
1106aa3650eaSMark Johnston 	for (m = vm_page_find_least(object, pindex); pindex < end; pindex++) {
1107aa3650eaSMark Johnston 		tobject = object;
1108c2655a40SMark Johnston 
11091ce137beSMatthew Dillon 		/*
1110aa3650eaSMark Johnston 		 * If the next page isn't resident in the top-level object, we
1111aa3650eaSMark Johnston 		 * need to search the shadow chain.  When applying MADV_FREE, we
1112aa3650eaSMark Johnston 		 * take care to release any swap space used to store
1113aa3650eaSMark Johnston 		 * non-resident pages.
1114aa3650eaSMark Johnston 		 */
1115aa3650eaSMark Johnston 		if (m == NULL || pindex < m->pindex) {
1116aa3650eaSMark Johnston 			/*
1117aa3650eaSMark Johnston 			 * Optimize a common case: if the top-level object has
1118aa3650eaSMark Johnston 			 * no backing object, we can skip over the non-resident
1119aa3650eaSMark Johnston 			 * range in constant time.
11201ce137beSMatthew Dillon 			 */
1121c2655a40SMark Johnston 			if (object->backing_object == NULL) {
1122c2655a40SMark Johnston 				tpindex = (m != NULL && m->pindex < end) ?
1123c2655a40SMark Johnston 				    m->pindex : end;
1124aa3650eaSMark Johnston 				vm_object_madvise_freespace(object, advice,
1125aa3650eaSMark Johnston 				    pindex, tpindex - pindex);
1126c2655a40SMark Johnston 				if ((pindex = tpindex) == end)
1127c2655a40SMark Johnston 					break;
1128aa3650eaSMark Johnston 				goto next_page;
1129aa3650eaSMark Johnston 			}
1130aa3650eaSMark Johnston 
1131aa3650eaSMark Johnston 			tpindex = pindex;
1132aa3650eaSMark Johnston 			do {
1133aa3650eaSMark Johnston 				vm_object_madvise_freespace(tobject, advice,
1134aa3650eaSMark Johnston 				    tpindex, 1);
11351ce137beSMatthew Dillon 				/*
1136aa3650eaSMark Johnston 				 * Prepare to search the next object in the
1137aa3650eaSMark Johnston 				 * chain.
11381ce137beSMatthew Dillon 				 */
113934567de7SAlan Cox 				backing_object = tobject->backing_object;
114034567de7SAlan Cox 				if (backing_object == NULL)
1141aa3650eaSMark Johnston 					goto next_pindex;
114289f6b863SAttilio Rao 				VM_OBJECT_WLOCK(backing_object);
1143aa3650eaSMark Johnston 				tpindex +=
1144aa3650eaSMark Johnston 				    OFF_TO_IDX(tobject->backing_object_offset);
11459b98b796SAlan Cox 				if (tobject != object)
114689f6b863SAttilio Rao 					VM_OBJECT_WUNLOCK(tobject);
114734567de7SAlan Cox 				tobject = backing_object;
1148aa3650eaSMark Johnston 				if (!vm_object_advice_applies(tobject, advice))
1149aa3650eaSMark Johnston 					goto next_pindex;
1150aa3650eaSMark Johnston 			} while ((tm = vm_page_lookup(tobject, tpindex)) ==
1151aa3650eaSMark Johnston 			    NULL);
1152aa3650eaSMark Johnston 		} else {
1153aa3650eaSMark Johnston next_page:
1154aa3650eaSMark Johnston 			tm = m;
1155aa3650eaSMark Johnston 			m = TAILQ_NEXT(m, listq);
1156c2655a40SMark Johnston 		}
1157c2655a40SMark Johnston 
1158867a482dSJohn Dyson 		/*
11596a2a3d73SAlan Cox 		 * If the page is not in a normal state, skip it.
1160867a482dSJohn Dyson 		 */
1161fee2a2faSMark Johnston 		if (tm->valid != VM_PAGE_BITS_ALL ||
1162fee2a2faSMark Johnston 		    vm_page_wired(tm))
1163aa3650eaSMark Johnston 			goto next_pindex;
1164aa3650eaSMark Johnston 		KASSERT((tm->flags & PG_FICTITIOUS) == 0,
1165aa3650eaSMark Johnston 		    ("vm_object_madvise: page %p is fictitious", tm));
1166aa3650eaSMark Johnston 		KASSERT((tm->oflags & VPO_UNMANAGED) == 0,
1167aa3650eaSMark Johnston 		    ("vm_object_madvise: page %p is not managed", tm));
116863e97555SJeff Roberson 		if (vm_page_tryxbusy(tm) == 0) {
1169aa3650eaSMark Johnston 			if (object != tobject)
1170aa3650eaSMark Johnston 				VM_OBJECT_WUNLOCK(object);
1171c2655a40SMark Johnston 			if (advice == MADV_WILLNEED) {
1172b11b56b5SAlan Cox 				/*
1173b11b56b5SAlan Cox 				 * Reference the page before unlocking and
1174b11b56b5SAlan Cox 				 * sleeping so that the page daemon is less
1175b11b56b5SAlan Cox 				 * likely to reclaim it.
1176b11b56b5SAlan Cox 				 */
1177aa3650eaSMark Johnston 				vm_page_aflag_set(tm, PGA_REFERENCED);
1178567e51e1SAlan Cox 			}
1179aa3650eaSMark Johnston 			vm_page_busy_sleep(tm, "madvpo", false);
11806e20a165SJohn Dyson   			goto relookup;
118134567de7SAlan Cox 		}
1182fee2a2faSMark Johnston 		vm_page_lock(tm);
1183aa3650eaSMark Johnston 		vm_page_advise(tm, advice);
1184aa3650eaSMark Johnston 		vm_page_unlock(tm);
118563e97555SJeff Roberson 		vm_page_xunbusy(tm);
1186aa3650eaSMark Johnston 		vm_object_madvise_freespace(tobject, advice, tm->pindex, 1);
1187aa3650eaSMark Johnston next_pindex:
11889b98b796SAlan Cox 		if (tobject != object)
118989f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(tobject);
1190867a482dSJohn Dyson 	}
119189f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
1192867a482dSJohn Dyson }
1193867a482dSJohn Dyson 
1194867a482dSJohn Dyson /*
1195df8bae1dSRodney W. Grimes  *	vm_object_shadow:
1196df8bae1dSRodney W. Grimes  *
1197df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
1198df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
1199df8bae1dSRodney W. Grimes  *	object reference is deallocated.
1200df8bae1dSRodney W. Grimes  *
1201df8bae1dSRodney W. Grimes  *	The new object and offset into that object
1202df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
1203df8bae1dSRodney W. Grimes  */
120426f9a767SRodney W. Grimes void
12051b40f8c0SMatthew Dillon vm_object_shadow(
12061b40f8c0SMatthew Dillon 	vm_object_t *object,	/* IN/OUT */
12071b40f8c0SMatthew Dillon 	vm_ooffset_t *offset,	/* IN/OUT */
12081b40f8c0SMatthew Dillon 	vm_size_t length)
1209df8bae1dSRodney W. Grimes {
1210d031cff1SMatthew Dillon 	vm_object_t source;
1211d031cff1SMatthew Dillon 	vm_object_t result;
1212df8bae1dSRodney W. Grimes 
1213df8bae1dSRodney W. Grimes 	source = *object;
1214df8bae1dSRodney W. Grimes 
1215df8bae1dSRodney W. Grimes 	/*
12169a2f6362SAlan Cox 	 * Don't create the new object if the old object isn't shared.
12179a2f6362SAlan Cox 	 */
1218570a2f4aSAlan Cox 	if (source != NULL) {
121989f6b863SAttilio Rao 		VM_OBJECT_WLOCK(source);
1220570a2f4aSAlan Cox 		if (source->ref_count == 1 &&
12219a2f6362SAlan Cox 		    source->handle == NULL &&
12229a2f6362SAlan Cox 		    (source->type == OBJT_DEFAULT ||
12239917e010SAlan Cox 		     source->type == OBJT_SWAP)) {
122489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(source);
12259a2f6362SAlan Cox 			return;
12269917e010SAlan Cox 		}
122789f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(source);
1228570a2f4aSAlan Cox 	}
12299a2f6362SAlan Cox 
12309a2f6362SAlan Cox 	/*
1231570a2f4aSAlan Cox 	 * Allocate a new object with the given length.
1232df8bae1dSRodney W. Grimes 	 */
12330cc74f14SAlan Cox 	result = vm_object_allocate(OBJT_DEFAULT, atop(length));
1234df8bae1dSRodney W. Grimes 
1235df8bae1dSRodney W. Grimes 	/*
12360d94caffSDavid Greenman 	 * The new object shadows the source object, adding a reference to it.
12370d94caffSDavid Greenman 	 * Our caller changes his reference to point to the new object,
12380d94caffSDavid Greenman 	 * removing a reference to the source object.  Net result: no change
12390d94caffSDavid Greenman 	 * of reference count.
12409b09fe24SMatthew Dillon 	 *
12419b09fe24SMatthew Dillon 	 * Try to optimize the result object's page color when shadowing
1242956f3135SPhilippe Charnier 	 * in order to maintain page coloring consistency in the combined
12439b09fe24SMatthew Dillon 	 * shadowed object.
1244df8bae1dSRodney W. Grimes 	 */
124524a1cce3SDavid Greenman 	result->backing_object = source;
12469174ca7bSTor Egge 	/*
12479174ca7bSTor Egge 	 * Store the offset into the source object, and fix up the offset into
12489174ca7bSTor Egge 	 * the new object.
12499174ca7bSTor Egge 	 */
12509174ca7bSTor Egge 	result->backing_object_offset = *offset;
1251570a2f4aSAlan Cox 	if (source != NULL) {
125289f6b863SAttilio Rao 		VM_OBJECT_WLOCK(source);
12533f289c3fSJeff Roberson 		result->domain = source->domain;
12541c500307SAlan Cox 		LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1255eaf13dd7SJohn Dyson 		source->shadow_count++;
1256f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
12577b54b1a9SAlan Cox 		result->flags |= source->flags & OBJ_COLORED;
1258f8a47341SAlan Cox 		result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) &
1259f8a47341SAlan Cox 		    ((1 << (VM_NFREEORDER - 1)) - 1);
1260f8a47341SAlan Cox #endif
126189f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(source);
1262de5f6a77SJohn Dyson 	}
1263df8bae1dSRodney W. Grimes 
1264df8bae1dSRodney W. Grimes 
1265df8bae1dSRodney W. Grimes 	/*
1266df8bae1dSRodney W. Grimes 	 * Return the new things
1267df8bae1dSRodney W. Grimes 	 */
1268df8bae1dSRodney W. Grimes 	*offset = 0;
1269df8bae1dSRodney W. Grimes 	*object = result;
1270df8bae1dSRodney W. Grimes }
1271df8bae1dSRodney W. Grimes 
1272c5aaa06dSAlan Cox /*
1273c5aaa06dSAlan Cox  *	vm_object_split:
1274c5aaa06dSAlan Cox  *
1275c5aaa06dSAlan Cox  * Split the pages in a map entry into a new object.  This affords
1276c5aaa06dSAlan Cox  * easier removal of unused pages, and keeps object inheritance from
1277c5aaa06dSAlan Cox  * being a negative impact on memory usage.
1278c5aaa06dSAlan Cox  */
1279c5aaa06dSAlan Cox void
1280c5aaa06dSAlan Cox vm_object_split(vm_map_entry_t entry)
1281c5aaa06dSAlan Cox {
128273000556SAlan Cox 	vm_page_t m, m_next;
1283c5aaa06dSAlan Cox 	vm_object_t orig_object, new_object, source;
128473000556SAlan Cox 	vm_pindex_t idx, offidxstart;
128573000556SAlan Cox 	vm_size_t size;
1286c5aaa06dSAlan Cox 
1287c5aaa06dSAlan Cox 	orig_object = entry->object.vm_object;
1288c5aaa06dSAlan Cox 	if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1289c5aaa06dSAlan Cox 		return;
1290c5aaa06dSAlan Cox 	if (orig_object->ref_count <= 1)
1291c5aaa06dSAlan Cox 		return;
129289f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(orig_object);
1293c5aaa06dSAlan Cox 
12944da9f125SAlan Cox 	offidxstart = OFF_TO_IDX(entry->offset);
129595442adfSAlan Cox 	size = atop(entry->end - entry->start);
1296c5aaa06dSAlan Cox 
12974da9f125SAlan Cox 	/*
12984da9f125SAlan Cox 	 * If swap_pager_copy() is later called, it will convert new_object
12994da9f125SAlan Cox 	 * into a swap object.
13004da9f125SAlan Cox 	 */
13014da9f125SAlan Cox 	new_object = vm_object_allocate(OBJT_DEFAULT, size);
1302c5aaa06dSAlan Cox 
1303c5474b8fSAlan Cox 	/*
1304c5474b8fSAlan Cox 	 * At this point, the new object is still private, so the order in
1305c5474b8fSAlan Cox 	 * which the original and new objects are locked does not matter.
1306c5474b8fSAlan Cox 	 */
130789f6b863SAttilio Rao 	VM_OBJECT_WLOCK(new_object);
130889f6b863SAttilio Rao 	VM_OBJECT_WLOCK(orig_object);
13093f289c3fSJeff Roberson 	new_object->domain = orig_object->domain;
1310c5aaa06dSAlan Cox 	source = orig_object->backing_object;
1311c5aaa06dSAlan Cox 	if (source != NULL) {
131289f6b863SAttilio Rao 		VM_OBJECT_WLOCK(source);
131319c244d0SAlan Cox 		if ((source->flags & OBJ_DEAD) != 0) {
131489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(source);
131589f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(orig_object);
131689f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(new_object);
131719c244d0SAlan Cox 			vm_object_deallocate(new_object);
131889f6b863SAttilio Rao 			VM_OBJECT_WLOCK(orig_object);
131919c244d0SAlan Cox 			return;
132019c244d0SAlan Cox 		}
13211c500307SAlan Cox 		LIST_INSERT_HEAD(&source->shadow_head,
1322c5aaa06dSAlan Cox 				  new_object, shadow_list);
13238e3a76fbSAlan Cox 		source->shadow_count++;
1324b921a12bSAlan Cox 		vm_object_reference_locked(source);	/* for new_object */
1325c5aaa06dSAlan Cox 		vm_object_clear_flag(source, OBJ_ONEMAPPING);
132689f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(source);
1327c5aaa06dSAlan Cox 		new_object->backing_object_offset =
13284da9f125SAlan Cox 			orig_object->backing_object_offset + entry->offset;
1329c5aaa06dSAlan Cox 		new_object->backing_object = source;
1330c5aaa06dSAlan Cox 	}
1331ef694c1aSEdward Tomasz Napierala 	if (orig_object->cred != NULL) {
1332ef694c1aSEdward Tomasz Napierala 		new_object->cred = orig_object->cred;
1333ef694c1aSEdward Tomasz Napierala 		crhold(orig_object->cred);
13343364c323SKonstantin Belousov 		new_object->charge = ptoa(size);
13353364c323SKonstantin Belousov 		KASSERT(orig_object->charge >= ptoa(size),
13363364c323SKonstantin Belousov 		    ("orig_object->charge < 0"));
13373364c323SKonstantin Belousov 		orig_object->charge -= ptoa(size);
13383364c323SKonstantin Belousov 	}
1339c5aaa06dSAlan Cox retry:
1340b382c10aSKonstantin Belousov 	m = vm_page_find_least(orig_object, offidxstart);
134173000556SAlan Cox 	for (; m != NULL && (idx = m->pindex - offidxstart) < size;
134273000556SAlan Cox 	    m = m_next) {
134373000556SAlan Cox 		m_next = TAILQ_NEXT(m, listq);
1344c5aaa06dSAlan Cox 
1345c5aaa06dSAlan Cox 		/*
1346c5aaa06dSAlan Cox 		 * We must wait for pending I/O to complete before we can
1347c5aaa06dSAlan Cox 		 * rename the page.
1348c5aaa06dSAlan Cox 		 *
1349c5aaa06dSAlan Cox 		 * We do not have to VM_PROT_NONE the page as mappings should
1350c5aaa06dSAlan Cox 		 * not be changed by this operation.
1351c5aaa06dSAlan Cox 		 */
135263e97555SJeff Roberson 		if (vm_page_tryxbusy(m) == 0) {
135389f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(new_object);
13544cdea4a8SJeff Roberson 			vm_page_sleep_if_busy(m, "spltwt");
135589f6b863SAttilio Rao 			VM_OBJECT_WLOCK(new_object);
1356c5aaa06dSAlan Cox 			goto retry;
1357de33beddSAlan Cox 		}
1358e946b949SAttilio Rao 
13593453bca8SAlan Cox 		/* vm_page_rename() will dirty the page. */
1360e946b949SAttilio Rao 		if (vm_page_rename(m, new_object, idx)) {
136163e97555SJeff Roberson 			vm_page_xunbusy(m);
1362e946b949SAttilio Rao 			VM_OBJECT_WUNLOCK(new_object);
1363e946b949SAttilio Rao 			VM_OBJECT_WUNLOCK(orig_object);
13648d6fbbb8SJeff Roberson 			vm_radix_wait();
1365e946b949SAttilio Rao 			VM_OBJECT_WLOCK(orig_object);
1366e946b949SAttilio Rao 			VM_OBJECT_WLOCK(new_object);
1367e946b949SAttilio Rao 			goto retry;
1368e946b949SAttilio Rao 		}
136963e97555SJeff Roberson 
1370b5f359b7SAlan Cox #if VM_NRESERVLEVEL > 0
1371b5f359b7SAlan Cox 		/*
1372b5f359b7SAlan Cox 		 * If some of the reservation's allocated pages remain with
1373b5f359b7SAlan Cox 		 * the original object, then transferring the reservation to
1374b5f359b7SAlan Cox 		 * the new object is neither particularly beneficial nor
1375b5f359b7SAlan Cox 		 * particularly harmful as compared to leaving the reservation
1376b5f359b7SAlan Cox 		 * with the original object.  If, however, all of the
1377b5f359b7SAlan Cox 		 * reservation's allocated pages are transferred to the new
1378b5f359b7SAlan Cox 		 * object, then transferring the reservation is typically
1379b5f359b7SAlan Cox 		 * beneficial.  Determining which of these two cases applies
1380b5f359b7SAlan Cox 		 * would be more costly than unconditionally renaming the
1381b5f359b7SAlan Cox 		 * reservation.
1382b5f359b7SAlan Cox 		 */
1383b5f359b7SAlan Cox 		vm_reserv_rename(m, new_object, orig_object, offidxstart);
1384b5f359b7SAlan Cox #endif
1385*8da1c098SJeff Roberson 		if (orig_object->type != OBJT_SWAP)
1386*8da1c098SJeff Roberson 			vm_page_xunbusy(m);
1387c5aaa06dSAlan Cox 	}
1388d7a013c3SAlan Cox 	if (orig_object->type == OBJT_SWAP) {
1389c5aaa06dSAlan Cox 		/*
1390c7c8dd7eSAlan Cox 		 * swap_pager_copy() can sleep, in which case the orig_object's
1391c7c8dd7eSAlan Cox 		 * and new_object's locks are released and reacquired.
1392c5aaa06dSAlan Cox 		 */
1393c5aaa06dSAlan Cox 		swap_pager_copy(orig_object, new_object, offidxstart, 0);
1394dfd55c0cSAttilio Rao 		TAILQ_FOREACH(m, &new_object->memq, listq)
1395c7aebda8SAttilio Rao 			vm_page_xunbusy(m);
1396c5aaa06dSAlan Cox 	}
139789f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(orig_object);
139889f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(new_object);
1399c5aaa06dSAlan Cox 	entry->object.vm_object = new_object;
1400c5aaa06dSAlan Cox 	entry->offset = 0LL;
1401c5aaa06dSAlan Cox 	vm_object_deallocate(orig_object);
140289f6b863SAttilio Rao 	VM_OBJECT_WLOCK(new_object);
1403c5aaa06dSAlan Cox }
1404c5aaa06dSAlan Cox 
14052ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_NOWAIT	0x0002
14062ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_WAIT	0x0004
14072ad1a3f7SMatthew Dillon 
140899a1570aSKonstantin Belousov static vm_page_t
14094cc8daf7SConrad Meyer vm_object_collapse_scan_wait(vm_object_t object, vm_page_t p, vm_page_t next,
141099a1570aSKonstantin Belousov     int op)
141199a1570aSKonstantin Belousov {
141299a1570aSKonstantin Belousov 	vm_object_t backing_object;
141399a1570aSKonstantin Belousov 
141499a1570aSKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED(object);
141599a1570aSKonstantin Belousov 	backing_object = object->backing_object;
141699a1570aSKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED(backing_object);
141799a1570aSKonstantin Belousov 
141899a1570aSKonstantin Belousov 	KASSERT(p == NULL || p->object == object || p->object == backing_object,
141999a1570aSKonstantin Belousov 	    ("invalid ownership %p %p %p", p, object, backing_object));
142099a1570aSKonstantin Belousov 	if ((op & OBSC_COLLAPSE_NOWAIT) != 0)
142199a1570aSKonstantin Belousov 		return (next);
14228d6fbbb8SJeff Roberson 	/* The page is only NULL when rename fails. */
14234cdea4a8SJeff Roberson 	if (p == NULL) {
14248d6fbbb8SJeff Roberson 		vm_radix_wait();
14254cdea4a8SJeff Roberson 	} else {
14264cdea4a8SJeff Roberson 		if (p->object == object)
14274cdea4a8SJeff Roberson 			VM_OBJECT_WUNLOCK(backing_object);
142899a1570aSKonstantin Belousov 		else
14294cdea4a8SJeff Roberson 			VM_OBJECT_WUNLOCK(object);
14305975e53dSKonstantin Belousov 		vm_page_busy_sleep(p, "vmocol", false);
14314cdea4a8SJeff Roberson 	}
143299a1570aSKonstantin Belousov 	VM_OBJECT_WLOCK(object);
143399a1570aSKonstantin Belousov 	VM_OBJECT_WLOCK(backing_object);
143499a1570aSKonstantin Belousov 	return (TAILQ_FIRST(&backing_object->memq));
143599a1570aSKonstantin Belousov }
143699a1570aSKonstantin Belousov 
143799a1570aSKonstantin Belousov static bool
14384cc8daf7SConrad Meyer vm_object_scan_all_shadowed(vm_object_t object)
14394cc8daf7SConrad Meyer {
14404cc8daf7SConrad Meyer 	vm_object_t backing_object;
14414cc8daf7SConrad Meyer 	vm_page_t p, pp;
144277d6fd97SKonstantin Belousov 	vm_pindex_t backing_offset_index, new_pindex, pi, ps;
14434cc8daf7SConrad Meyer 
14444cc8daf7SConrad Meyer 	VM_OBJECT_ASSERT_WLOCKED(object);
14454cc8daf7SConrad Meyer 	VM_OBJECT_ASSERT_WLOCKED(object->backing_object);
14464cc8daf7SConrad Meyer 
14474cc8daf7SConrad Meyer 	backing_object = object->backing_object;
14484cc8daf7SConrad Meyer 
144977d6fd97SKonstantin Belousov 	if (backing_object->type != OBJT_DEFAULT &&
145077d6fd97SKonstantin Belousov 	    backing_object->type != OBJT_SWAP)
14514cc8daf7SConrad Meyer 		return (false);
14524cc8daf7SConrad Meyer 
145377d6fd97SKonstantin Belousov 	pi = backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
145477d6fd97SKonstantin Belousov 	p = vm_page_find_least(backing_object, pi);
145577d6fd97SKonstantin Belousov 	ps = swap_pager_find_least(backing_object, pi);
14564cc8daf7SConrad Meyer 
14574cc8daf7SConrad Meyer 	/*
145877d6fd97SKonstantin Belousov 	 * Only check pages inside the parent object's range and
145977d6fd97SKonstantin Belousov 	 * inside the parent object's mapping of the backing object.
14604cc8daf7SConrad Meyer 	 */
146177d6fd97SKonstantin Belousov 	for (;; pi++) {
146277d6fd97SKonstantin Belousov 		if (p != NULL && p->pindex < pi)
146377d6fd97SKonstantin Belousov 			p = TAILQ_NEXT(p, listq);
146477d6fd97SKonstantin Belousov 		if (ps < pi)
146577d6fd97SKonstantin Belousov 			ps = swap_pager_find_least(backing_object, pi);
146677d6fd97SKonstantin Belousov 		if (p == NULL && ps >= backing_object->size)
146777d6fd97SKonstantin Belousov 			break;
146877d6fd97SKonstantin Belousov 		else if (p == NULL)
146977d6fd97SKonstantin Belousov 			pi = ps;
147077d6fd97SKonstantin Belousov 		else
147177d6fd97SKonstantin Belousov 			pi = MIN(p->pindex, ps);
147277d6fd97SKonstantin Belousov 
147377d6fd97SKonstantin Belousov 		new_pindex = pi - backing_offset_index;
147477d6fd97SKonstantin Belousov 		if (new_pindex >= object->size)
147577d6fd97SKonstantin Belousov 			break;
14764cc8daf7SConrad Meyer 
14774cc8daf7SConrad Meyer 		/*
14784cc8daf7SConrad Meyer 		 * See if the parent has the page or if the parent's object
14794cc8daf7SConrad Meyer 		 * pager has the page.  If the parent has the page but the page
14804cc8daf7SConrad Meyer 		 * is not valid, the parent's object pager must have the page.
14814cc8daf7SConrad Meyer 		 *
14824cc8daf7SConrad Meyer 		 * If this fails, the parent does not completely shadow the
14834cc8daf7SConrad Meyer 		 * object and we might as well give up now.
14844cc8daf7SConrad Meyer 		 */
14854cc8daf7SConrad Meyer 		pp = vm_page_lookup(object, new_pindex);
14864cc8daf7SConrad Meyer 		if ((pp == NULL || pp->valid == 0) &&
14874cc8daf7SConrad Meyer 		    !vm_pager_has_page(object, new_pindex, NULL, NULL))
14884cc8daf7SConrad Meyer 			return (false);
14894cc8daf7SConrad Meyer 	}
14904cc8daf7SConrad Meyer 	return (true);
14914cc8daf7SConrad Meyer }
14924cc8daf7SConrad Meyer 
14934cc8daf7SConrad Meyer static bool
14944cc8daf7SConrad Meyer vm_object_collapse_scan(vm_object_t object, int op)
14952ad1a3f7SMatthew Dillon {
14962ad1a3f7SMatthew Dillon 	vm_object_t backing_object;
149799a1570aSKonstantin Belousov 	vm_page_t next, p, pp;
149899a1570aSKonstantin Belousov 	vm_pindex_t backing_offset_index, new_pindex;
14992ad1a3f7SMatthew Dillon 
150089f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
150189f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object->backing_object);
15022ad1a3f7SMatthew Dillon 
15032ad1a3f7SMatthew Dillon 	backing_object = object->backing_object;
15042ad1a3f7SMatthew Dillon 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
15052ad1a3f7SMatthew Dillon 
15062ad1a3f7SMatthew Dillon 	/*
15072ad1a3f7SMatthew Dillon 	 * Initial conditions
15082ad1a3f7SMatthew Dillon 	 */
15094cc8daf7SConrad Meyer 	if ((op & OBSC_COLLAPSE_WAIT) != 0)
15102ad1a3f7SMatthew Dillon 		vm_object_set_flag(backing_object, OBJ_DEAD);
15112ad1a3f7SMatthew Dillon 
15122ad1a3f7SMatthew Dillon 	/*
15132ad1a3f7SMatthew Dillon 	 * Our scan
15142ad1a3f7SMatthew Dillon 	 */
15154cc8daf7SConrad Meyer 	for (p = TAILQ_FIRST(&backing_object->memq); p != NULL; p = next) {
151699a1570aSKonstantin Belousov 		next = TAILQ_NEXT(p, listq);
151799a1570aSKonstantin Belousov 		new_pindex = p->pindex - backing_offset_index;
15182ad1a3f7SMatthew Dillon 
15192ad1a3f7SMatthew Dillon 		/*
15202ad1a3f7SMatthew Dillon 		 * Check for busy page
15212ad1a3f7SMatthew Dillon 		 */
152263e97555SJeff Roberson 		if (vm_page_tryxbusy(p) == 0) {
15234cc8daf7SConrad Meyer 			next = vm_object_collapse_scan_wait(object, p, next, op);
15242ad1a3f7SMatthew Dillon 			continue;
15252ad1a3f7SMatthew Dillon 		}
15262ad1a3f7SMatthew Dillon 
152799a1570aSKonstantin Belousov 		KASSERT(p->object == backing_object,
15284cc8daf7SConrad Meyer 		    ("vm_object_collapse_scan: object mismatch"));
15292ad1a3f7SMatthew Dillon 
153099a1570aSKonstantin Belousov 		if (p->pindex < backing_offset_index ||
153199a1570aSKonstantin Belousov 		    new_pindex >= object->size) {
1532e946b949SAttilio Rao 			if (backing_object->type == OBJT_SWAP)
15334cc8daf7SConrad Meyer 				swap_pager_freespace(backing_object, p->pindex,
15344cc8daf7SConrad Meyer 				    1);
1535e946b949SAttilio Rao 
1536f6d89838SAlan Cox 			KASSERT(!pmap_page_is_mapped(p),
1537f6d89838SAlan Cox 			    ("freeing mapped page %p", p));
15380fd977b3SMark Johnston 			if (vm_page_remove(p))
15392ad1a3f7SMatthew Dillon 				vm_page_free(p);
1540*8da1c098SJeff Roberson 			else
1541*8da1c098SJeff Roberson 				vm_page_xunbusy(p);
15422ad1a3f7SMatthew Dillon 			continue;
15432ad1a3f7SMatthew Dillon 		}
15442ad1a3f7SMatthew Dillon 
15452ad1a3f7SMatthew Dillon 		pp = vm_page_lookup(object, new_pindex);
154663e97555SJeff Roberson 		if (pp != NULL && vm_page_tryxbusy(pp) == 0) {
154763e97555SJeff Roberson 			vm_page_xunbusy(p);
1548e18cc7bfSMax Laier 			/*
15494cc8daf7SConrad Meyer 			 * The page in the parent is busy and possibly not
15504cc8daf7SConrad Meyer 			 * (yet) valid.  Until its state is finalized by the
15514cc8daf7SConrad Meyer 			 * busy bit owner, we can't tell whether it shadows the
15524cc8daf7SConrad Meyer 			 * original page.  Therefore, we must either skip it
15534cc8daf7SConrad Meyer 			 * and the original (backing_object) page or wait for
15544cc8daf7SConrad Meyer 			 * its state to be finalized.
1555e18cc7bfSMax Laier 			 *
15564cc8daf7SConrad Meyer 			 * This is due to a race with vm_fault() where we must
15574cc8daf7SConrad Meyer 			 * unbusy the original (backing_obj) page before we can
15584cc8daf7SConrad Meyer 			 * (re)lock the parent.  Hence we can get here.
1559e18cc7bfSMax Laier 			 */
15604cc8daf7SConrad Meyer 			next = vm_object_collapse_scan_wait(object, pp, next,
15614cc8daf7SConrad Meyer 			    op);
1562e18cc7bfSMax Laier 			continue;
1563e18cc7bfSMax Laier 		}
156499a1570aSKonstantin Belousov 
156599a1570aSKonstantin Belousov 		KASSERT(pp == NULL || pp->valid != 0,
156699a1570aSKonstantin Belousov 		    ("unbusy invalid page %p", pp));
156799a1570aSKonstantin Belousov 
15684cc8daf7SConrad Meyer 		if (pp != NULL || vm_pager_has_page(object, new_pindex, NULL,
15694cc8daf7SConrad Meyer 			NULL)) {
157099a1570aSKonstantin Belousov 			/*
15714cc8daf7SConrad Meyer 			 * The page already exists in the parent OR swap exists
15724cc8daf7SConrad Meyer 			 * for this location in the parent.  Leave the parent's
15734cc8daf7SConrad Meyer 			 * page alone.  Destroy the original page from the
15744cc8daf7SConrad Meyer 			 * backing object.
157599a1570aSKonstantin Belousov 			 */
1576e946b949SAttilio Rao 			if (backing_object->type == OBJT_SWAP)
15774cc8daf7SConrad Meyer 				swap_pager_freespace(backing_object, p->pindex,
15784cc8daf7SConrad Meyer 				    1);
1579f6d89838SAlan Cox 			KASSERT(!pmap_page_is_mapped(p),
1580f6d89838SAlan Cox 			    ("freeing mapped page %p", p));
15810fd977b3SMark Johnston 			if (vm_page_remove(p))
15822ad1a3f7SMatthew Dillon 				vm_page_free(p);
1583*8da1c098SJeff Roberson 			else
1584*8da1c098SJeff Roberson 				vm_page_xunbusy(p);
158563e97555SJeff Roberson 			if (pp != NULL)
158663e97555SJeff Roberson 				vm_page_xunbusy(pp);
15872ad1a3f7SMatthew Dillon 			continue;
15882ad1a3f7SMatthew Dillon 		}
15892ad1a3f7SMatthew Dillon 
1590e946b949SAttilio Rao 		/*
15914cc8daf7SConrad Meyer 		 * Page does not exist in parent, rename the page from the
15924cc8daf7SConrad Meyer 		 * backing object to the main object.
1593e946b949SAttilio Rao 		 *
15944cc8daf7SConrad Meyer 		 * If the page was mapped to a process, it can remain mapped
15953453bca8SAlan Cox 		 * through the rename.  vm_page_rename() will dirty the page.
1596e946b949SAttilio Rao 		 */
1597e946b949SAttilio Rao 		if (vm_page_rename(p, object, new_pindex)) {
159863e97555SJeff Roberson 			vm_page_xunbusy(p);
159963e97555SJeff Roberson 			if (pp != NULL)
160063e97555SJeff Roberson 				vm_page_xunbusy(pp);
16014cc8daf7SConrad Meyer 			next = vm_object_collapse_scan_wait(object, NULL, next,
16024cc8daf7SConrad Meyer 			    op);
1603e946b949SAttilio Rao 			continue;
1604e946b949SAttilio Rao 		}
160514a5dc17SAttilio Rao 
160614a5dc17SAttilio Rao 		/* Use the old pindex to free the right page. */
1607e946b949SAttilio Rao 		if (backing_object->type == OBJT_SWAP)
160814a5dc17SAttilio Rao 			swap_pager_freespace(backing_object,
160914a5dc17SAttilio Rao 			    new_pindex + backing_offset_index, 1);
1610e946b949SAttilio Rao 
1611f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
1612f8a47341SAlan Cox 		/*
1613f8a47341SAlan Cox 		 * Rename the reservation.
1614f8a47341SAlan Cox 		 */
1615f8a47341SAlan Cox 		vm_reserv_rename(p, object, backing_object,
1616f8a47341SAlan Cox 		    backing_offset_index);
1617f8a47341SAlan Cox #endif
1618*8da1c098SJeff Roberson 		vm_page_xunbusy(p);
16192ad1a3f7SMatthew Dillon 	}
162099a1570aSKonstantin Belousov 	return (true);
16212ad1a3f7SMatthew Dillon }
16222ad1a3f7SMatthew Dillon 
1623df8bae1dSRodney W. Grimes 
1624df8bae1dSRodney W. Grimes /*
16252fe6e4d7SDavid Greenman  * this version of collapse allows the operation to occur earlier and
16262fe6e4d7SDavid Greenman  * when paging_in_progress is true for an object...  This is not a complete
16272fe6e4d7SDavid Greenman  * operation, but should plug 99.9% of the rest of the leaks.
16282fe6e4d7SDavid Greenman  */
16292fe6e4d7SDavid Greenman static void
16301b40f8c0SMatthew Dillon vm_object_qcollapse(vm_object_t object)
16312fe6e4d7SDavid Greenman {
16322ad1a3f7SMatthew Dillon 	vm_object_t backing_object = object->backing_object;
16332fe6e4d7SDavid Greenman 
163489f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
163589f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(backing_object);
16361b40f8c0SMatthew Dillon 
16372fe6e4d7SDavid Greenman 	if (backing_object->ref_count != 1)
16382fe6e4d7SDavid Greenman 		return;
16392fe6e4d7SDavid Greenman 
16404cc8daf7SConrad Meyer 	vm_object_collapse_scan(object, OBSC_COLLAPSE_NOWAIT);
16412fe6e4d7SDavid Greenman }
16422fe6e4d7SDavid Greenman 
1643df8bae1dSRodney W. Grimes /*
1644df8bae1dSRodney W. Grimes  *	vm_object_collapse:
1645df8bae1dSRodney W. Grimes  *
1646df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
1647df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
1648df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
1649df8bae1dSRodney W. Grimes  */
165026f9a767SRodney W. Grimes void
16511b40f8c0SMatthew Dillon vm_object_collapse(vm_object_t object)
1652df8bae1dSRodney W. Grimes {
165398f139daSKonstantin Belousov 	vm_object_t backing_object, new_backing_object;
165498f139daSKonstantin Belousov 
165589f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
165623955314SAlfred Perlstein 
1657df8bae1dSRodney W. Grimes 	while (TRUE) {
1658df8bae1dSRodney W. Grimes 		/*
1659df8bae1dSRodney W. Grimes 		 * Verify that the conditions are right for collapse:
1660df8bae1dSRodney W. Grimes 		 *
16612ad1a3f7SMatthew Dillon 		 * The object exists and the backing object exists.
1662df8bae1dSRodney W. Grimes 		 */
166324a1cce3SDavid Greenman 		if ((backing_object = object->backing_object) == NULL)
16642ad1a3f7SMatthew Dillon 			break;
1665df8bae1dSRodney W. Grimes 
1666f919ebdeSDavid Greenman 		/*
1667f919ebdeSDavid Greenman 		 * we check the backing object first, because it is most likely
166824a1cce3SDavid Greenman 		 * not collapsable.
1669f919ebdeSDavid Greenman 		 */
167089f6b863SAttilio Rao 		VM_OBJECT_WLOCK(backing_object);
167124a1cce3SDavid Greenman 		if (backing_object->handle != NULL ||
167224a1cce3SDavid Greenman 		    (backing_object->type != OBJT_DEFAULT &&
167312487941SKonstantin Belousov 		    backing_object->type != OBJT_SWAP) ||
167412487941SKonstantin Belousov 		    (backing_object->flags & (OBJ_DEAD | OBJ_NOSPLIT)) != 0 ||
167524a1cce3SDavid Greenman 		    object->handle != NULL ||
167624a1cce3SDavid Greenman 		    (object->type != OBJT_DEFAULT &&
167724a1cce3SDavid Greenman 		     object->type != OBJT_SWAP) ||
167824a1cce3SDavid Greenman 		    (object->flags & OBJ_DEAD)) {
167989f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(backing_object);
16802ad1a3f7SMatthew Dillon 			break;
168124a1cce3SDavid Greenman 		}
16829b4814bbSDavid Greenman 
168311b57401SHans Petter Selasky 		if (REFCOUNT_COUNT(object->paging_in_progress) > 0 ||
168411b57401SHans Petter Selasky 		    REFCOUNT_COUNT(backing_object->paging_in_progress) > 0) {
1685b9921222SDavid Greenman 			vm_object_qcollapse(object);
168689f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(backing_object);
16872ad1a3f7SMatthew Dillon 			break;
1688df8bae1dSRodney W. Grimes 		}
168998f139daSKonstantin Belousov 
169026f9a767SRodney W. Grimes 		/*
16910d94caffSDavid Greenman 		 * We know that we can either collapse the backing object (if
16922ad1a3f7SMatthew Dillon 		 * the parent is the only reference to it) or (perhaps) have
16932ad1a3f7SMatthew Dillon 		 * the parent bypass the object if the parent happens to shadow
16942ad1a3f7SMatthew Dillon 		 * all the resident pages in the entire backing object.
16952ad1a3f7SMatthew Dillon 		 *
16962ad1a3f7SMatthew Dillon 		 * This is ignoring pager-backed pages such as swap pages.
16974cc8daf7SConrad Meyer 		 * vm_object_collapse_scan fails the shadowing test in this
16982ad1a3f7SMatthew Dillon 		 * case.
1699df8bae1dSRodney W. Grimes 		 */
1700df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
1701aa9bc3b1SKonstantin Belousov 			vm_object_pip_add(object, 1);
1702aa9bc3b1SKonstantin Belousov 			vm_object_pip_add(backing_object, 1);
1703aa9bc3b1SKonstantin Belousov 
1704df8bae1dSRodney W. Grimes 			/*
17052ad1a3f7SMatthew Dillon 			 * If there is exactly one reference to the backing
17062ad1a3f7SMatthew Dillon 			 * object, we can collapse it into the parent.
1707df8bae1dSRodney W. Grimes 			 */
17084cc8daf7SConrad Meyer 			vm_object_collapse_scan(object, OBSC_COLLAPSE_WAIT);
1709df8bae1dSRodney W. Grimes 
1710f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
1711f8a47341SAlan Cox 			/*
1712f8a47341SAlan Cox 			 * Break any reservations from backing_object.
1713f8a47341SAlan Cox 			 */
1714f8a47341SAlan Cox 			if (__predict_false(!LIST_EMPTY(&backing_object->rvq)))
1715f8a47341SAlan Cox 				vm_reserv_break_all(backing_object);
1716f8a47341SAlan Cox #endif
1717f8a47341SAlan Cox 
1718df8bae1dSRodney W. Grimes 			/*
1719df8bae1dSRodney W. Grimes 			 * Move the pager from backing_object to object.
1720df8bae1dSRodney W. Grimes 			 */
17216be36525SAlan Cox 			if (backing_object->type == OBJT_SWAP) {
172224a1cce3SDavid Greenman 				/*
1723c7c8dd7eSAlan Cox 				 * swap_pager_copy() can sleep, in which case
1724c7c8dd7eSAlan Cox 				 * the backing_object's and object's locks are
1725c7c8dd7eSAlan Cox 				 * released and reacquired.
1726571a1e92SAttilio Rao 				 * Since swap_pager_copy() is being asked to
1727571a1e92SAttilio Rao 				 * destroy the source, it will change the
1728571a1e92SAttilio Rao 				 * backing_object's type to OBJT_DEFAULT.
172924a1cce3SDavid Greenman 				 */
17301c7c3c6aSMatthew Dillon 				swap_pager_copy(
17311c7c3c6aSMatthew Dillon 				    backing_object,
17321c7c3c6aSMatthew Dillon 				    object,
17331c7c3c6aSMatthew Dillon 				    OFF_TO_IDX(object->backing_object_offset), TRUE);
1734c0503609SDavid Greenman 			}
1735df8bae1dSRodney W. Grimes 			/*
1736df8bae1dSRodney W. Grimes 			 * Object now shadows whatever backing_object did.
17372ad1a3f7SMatthew Dillon 			 * Note that the reference to
17382ad1a3f7SMatthew Dillon 			 * backing_object->backing_object moves from within
17392ad1a3f7SMatthew Dillon 			 * backing_object to within object.
1740df8bae1dSRodney W. Grimes 			 */
17411c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
17424f7c7f6eSAlan Cox 			backing_object->shadow_count--;
1743de5f6a77SJohn Dyson 			if (backing_object->backing_object) {
174489f6b863SAttilio Rao 				VM_OBJECT_WLOCK(backing_object->backing_object);
17451c500307SAlan Cox 				LIST_REMOVE(backing_object, shadow_list);
174643186e53SAlan Cox 				LIST_INSERT_HEAD(
174743186e53SAlan Cox 				    &backing_object->backing_object->shadow_head,
174843186e53SAlan Cox 				    object, shadow_list);
174943186e53SAlan Cox 				/*
175043186e53SAlan Cox 				 * The shadow_count has not changed.
175143186e53SAlan Cox 				 */
175289f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(backing_object->backing_object);
1753de5f6a77SJohn Dyson 			}
175424a1cce3SDavid Greenman 			object->backing_object = backing_object->backing_object;
17552ad1a3f7SMatthew Dillon 			object->backing_object_offset +=
17562ad1a3f7SMatthew Dillon 			    backing_object->backing_object_offset;
17572ad1a3f7SMatthew Dillon 
1758df8bae1dSRodney W. Grimes 			/*
1759df8bae1dSRodney W. Grimes 			 * Discard backing_object.
1760df8bae1dSRodney W. Grimes 			 *
17610d94caffSDavid Greenman 			 * Since the backing object has no pages, no pager left,
17620d94caffSDavid Greenman 			 * and no object references within it, all that is
17630d94caffSDavid Greenman 			 * necessary is to dispose of it.
1764df8bae1dSRodney W. Grimes 			 */
17659b4d473aSKonstantin Belousov 			KASSERT(backing_object->ref_count == 1, (
17669b4d473aSKonstantin Belousov "backing_object %p was somehow re-referenced during collapse!",
17679b4d473aSKonstantin Belousov 			    backing_object));
1768aa9bc3b1SKonstantin Belousov 			vm_object_pip_wakeup(backing_object);
1769e735691bSJohn Baldwin 			backing_object->type = OBJT_DEAD;
1770e735691bSJohn Baldwin 			backing_object->ref_count = 0;
177189f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(backing_object);
17729b4d473aSKonstantin Belousov 			vm_object_destroy(backing_object);
1773df8bae1dSRodney W. Grimes 
1774aa9bc3b1SKonstantin Belousov 			vm_object_pip_wakeup(object);
177511542376SAlan Cox 			counter_u64_add(object_collapses, 1);
17760d94caffSDavid Greenman 		} else {
1777df8bae1dSRodney W. Grimes 			/*
17782ad1a3f7SMatthew Dillon 			 * If we do not entirely shadow the backing object,
17792ad1a3f7SMatthew Dillon 			 * there is nothing we can do so we give up.
1780df8bae1dSRodney W. Grimes 			 */
1781df59a0feSJeff Roberson 			if (object->resident_page_count != object->size &&
17824cc8daf7SConrad Meyer 			    !vm_object_scan_all_shadowed(object)) {
178389f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(backing_object);
17842ad1a3f7SMatthew Dillon 				break;
178524a1cce3SDavid Greenman 			}
1786df8bae1dSRodney W. Grimes 
1787df8bae1dSRodney W. Grimes 			/*
17880d94caffSDavid Greenman 			 * Make the parent shadow the next object in the
17890d94caffSDavid Greenman 			 * chain.  Deallocating backing_object will not remove
17900d94caffSDavid Greenman 			 * it, since its reference count is at least 2.
1791df8bae1dSRodney W. Grimes 			 */
17921c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
1793eaf13dd7SJohn Dyson 			backing_object->shadow_count--;
179495e5e988SJohn Dyson 
179595e5e988SJohn Dyson 			new_backing_object = backing_object->backing_object;
17968aef1712SMatthew Dillon 			if ((object->backing_object = new_backing_object) != NULL) {
179789f6b863SAttilio Rao 				VM_OBJECT_WLOCK(new_backing_object);
17981c500307SAlan Cox 				LIST_INSERT_HEAD(
17992ad1a3f7SMatthew Dillon 				    &new_backing_object->shadow_head,
18002ad1a3f7SMatthew Dillon 				    object,
18012ad1a3f7SMatthew Dillon 				    shadow_list
18022ad1a3f7SMatthew Dillon 				);
1803eaf13dd7SJohn Dyson 				new_backing_object->shadow_count++;
1804b921a12bSAlan Cox 				vm_object_reference_locked(new_backing_object);
180589f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(new_backing_object);
180695e5e988SJohn Dyson 				object->backing_object_offset +=
180795e5e988SJohn Dyson 					backing_object->backing_object_offset;
1808de5f6a77SJohn Dyson 			}
1809df8bae1dSRodney W. Grimes 
1810df8bae1dSRodney W. Grimes 			/*
18110d94caffSDavid Greenman 			 * Drop the reference count on backing_object. Since
181222ec553fSAlan Cox 			 * its ref_count was at least 2, it will not vanish.
1813df8bae1dSRodney W. Grimes 			 */
181422ec553fSAlan Cox 			backing_object->ref_count--;
181589f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(backing_object);
181611542376SAlan Cox 			counter_u64_add(object_bypasses, 1);
1817df8bae1dSRodney W. Grimes 		}
1818df8bae1dSRodney W. Grimes 
1819df8bae1dSRodney W. Grimes 		/*
1820df8bae1dSRodney W. Grimes 		 * Try again with this object's new backing object.
1821df8bae1dSRodney W. Grimes 		 */
1822df8bae1dSRodney W. Grimes 	}
1823df8bae1dSRodney W. Grimes }
1824df8bae1dSRodney W. Grimes 
1825df8bae1dSRodney W. Grimes /*
1826bff99f0dSAlan Cox  *	vm_object_page_remove:
1827df8bae1dSRodney W. Grimes  *
182868855966SAlan Cox  *	For the given object, either frees or invalidates each of the
18296bbee8e2SAlan Cox  *	specified pages.  In general, a page is freed.  However, if a page is
18306bbee8e2SAlan Cox  *	wired for any reason other than the existence of a managed, wired
18316bbee8e2SAlan Cox  *	mapping, then it may be invalidated but not removed from the object.
18326bbee8e2SAlan Cox  *	Pages are specified by the given range ["start", "end") and the option
18336bbee8e2SAlan Cox  *	OBJPR_CLEANONLY.  As a special case, if "end" is zero, then the range
18346bbee8e2SAlan Cox  *	extends from "start" to the end of the object.  If the option
18356bbee8e2SAlan Cox  *	OBJPR_CLEANONLY is specified, then only the non-dirty pages within the
18366bbee8e2SAlan Cox  *	specified range are affected.  If the option OBJPR_NOTMAPPED is
18376bbee8e2SAlan Cox  *	specified, then the pages within the specified range must have no
18386bbee8e2SAlan Cox  *	mappings.  Otherwise, if this option is not specified, any mappings to
18396bbee8e2SAlan Cox  *	the specified pages are removed before the pages are freed or
18406bbee8e2SAlan Cox  *	invalidated.
184168855966SAlan Cox  *
18426bbee8e2SAlan Cox  *	In general, this operation should only be performed on objects that
18436bbee8e2SAlan Cox  *	contain managed pages.  There are, however, two exceptions.  First, it
18446bbee8e2SAlan Cox  *	is performed on the kernel and kmem objects by vm_map_entry_delete().
18456bbee8e2SAlan Cox  *	Second, it is used by msync(..., MS_INVALIDATE) to invalidate device-
18466bbee8e2SAlan Cox  *	backed pages.  In both of these cases, the option OBJPR_CLEANONLY must
18476bbee8e2SAlan Cox  *	not be specified and the option OBJPR_NOTMAPPED must be specified.
1848df8bae1dSRodney W. Grimes  *
1849df8bae1dSRodney W. Grimes  *	The object must be locked.
1850df8bae1dSRodney W. Grimes  */
185126f9a767SRodney W. Grimes void
1852ecde4b32SAlan Cox vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
18536bbee8e2SAlan Cox     int options)
1854df8bae1dSRodney W. Grimes {
1855d031cff1SMatthew Dillon 	vm_page_t p, next;
1856df8bae1dSRodney W. Grimes 
185789f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
185828634820SAlan Cox 	KASSERT((object->flags & OBJ_UNMANAGED) == 0 ||
18596bbee8e2SAlan Cox 	    (options & (OBJPR_CLEANONLY | OBJPR_NOTMAPPED)) == OBJPR_NOTMAPPED,
18606bbee8e2SAlan Cox 	    ("vm_object_page_remove: illegal options for object %p", object));
1861ecde4b32SAlan Cox 	if (object->resident_page_count == 0)
18627667839aSAlan Cox 		return;
1863d474eaaaSDoug Rabson 	vm_object_pip_add(object, 1);
186426f9a767SRodney W. Grimes again:
1865b382c10aSKonstantin Belousov 	p = vm_page_find_least(object, start);
18662965a453SKip Macy 
186775741c04SAlan Cox 	/*
18686bbee8e2SAlan Cox 	 * Here, the variable "p" is either (1) the page with the least pindex
18696bbee8e2SAlan Cox 	 * greater than or equal to the parameter "start" or (2) NULL.
187075741c04SAlan Cox 	 */
18716bbee8e2SAlan Cox 	for (; p != NULL && (p->pindex < end || end == 0); p = next) {
1872b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
187375741c04SAlan Cox 
187459677d3cSAlan Cox 		/*
18756bbee8e2SAlan Cox 		 * If the page is wired for any reason besides the existence
18766bbee8e2SAlan Cox 		 * of managed, wired mappings, then it cannot be freed.  For
18776bbee8e2SAlan Cox 		 * example, fictitious pages, which represent device memory,
18786bbee8e2SAlan Cox 		 * are inherently wired and cannot be freed.  They can,
18796bbee8e2SAlan Cox 		 * however, be invalidated if the option OBJPR_CLEANONLY is
18806bbee8e2SAlan Cox 		 * not specified.
188159677d3cSAlan Cox 		 */
188263e97555SJeff Roberson 		if (vm_page_tryxbusy(p) == 0) {
18834cdea4a8SJeff Roberson 			vm_page_sleep_if_busy(p, "vmopar");
1884fee2a2faSMark Johnston 			goto again;
1885fee2a2faSMark Johnston 		}
1886d842aa51SMark Johnston 		if (vm_page_wired(p)) {
1887fee2a2faSMark Johnston wired:
1888cf060942SAlan Cox 			if ((options & OBJPR_NOTMAPPED) == 0 &&
1889cf060942SAlan Cox 			    object->ref_count != 0)
18904fec79beSAlan Cox 				pmap_remove_all(p);
18916bbee8e2SAlan Cox 			if ((options & OBJPR_CLEANONLY) == 0) {
1892bd7e5f99SJohn Dyson 				p->valid = 0;
1893a28042d1SAlan Cox 				vm_page_undirty(p);
1894a28042d1SAlan Cox 			}
189563e97555SJeff Roberson 			vm_page_xunbusy(p);
189693c5d3a4SKonstantin Belousov 			continue;
18970d94caffSDavid Greenman 		}
189868855966SAlan Cox 		KASSERT((p->flags & PG_FICTITIOUS) == 0,
189968855966SAlan Cox 		    ("vm_object_page_remove: page %p is fictitious", p));
19006bbee8e2SAlan Cox 		if ((options & OBJPR_CLEANONLY) != 0 && p->valid != 0) {
1901cf060942SAlan Cox 			if ((options & OBJPR_NOTMAPPED) == 0 &&
1902fee2a2faSMark Johnston 			    object->ref_count != 0 &&
1903fee2a2faSMark Johnston 			    !vm_page_try_remove_write(p))
1904fee2a2faSMark Johnston 				goto wired;
190563e97555SJeff Roberson 			if (p->dirty != 0) {
190663e97555SJeff Roberson 				vm_page_xunbusy(p);
190793c5d3a4SKonstantin Belousov 				continue;
19082965a453SKip Macy 			}
190963e97555SJeff Roberson 		}
1910fee2a2faSMark Johnston 		if ((options & OBJPR_NOTMAPPED) == 0 &&
1911fee2a2faSMark Johnston 		    object->ref_count != 0 && !vm_page_try_remove_all(p))
1912fee2a2faSMark Johnston 			goto wired;
19135cd29d0fSMark Johnston 		vm_page_free(p);
19142965a453SKip Macy 	}
1915f919ebdeSDavid Greenman 	vm_object_pip_wakeup(object);
1916c0503609SDavid Greenman }
1917df8bae1dSRodney W. Grimes 
1918df8bae1dSRodney W. Grimes /*
19193138cd36SMark Johnston  *	vm_object_page_noreuse:
1920936c09acSJohn Baldwin  *
19213138cd36SMark Johnston  *	For the given object, attempt to move the specified pages to
19223138cd36SMark Johnston  *	the head of the inactive queue.  This bypasses regular LRU
19233138cd36SMark Johnston  *	operation and allows the pages to be reused quickly under memory
19243138cd36SMark Johnston  *	pressure.  If a page is wired for any reason, then it will not
19253138cd36SMark Johnston  *	be queued.  Pages are specified by the range ["start", "end").
19263138cd36SMark Johnston  *	As a special case, if "end" is zero, then the range extends from
19273138cd36SMark Johnston  *	"start" to the end of the object.
1928936c09acSJohn Baldwin  *
1929936c09acSJohn Baldwin  *	This operation should only be performed on objects that
193028634820SAlan Cox  *	contain non-fictitious, managed pages.
1931936c09acSJohn Baldwin  *
1932936c09acSJohn Baldwin  *	The object must be locked.
1933936c09acSJohn Baldwin  */
1934936c09acSJohn Baldwin void
19353138cd36SMark Johnston vm_object_page_noreuse(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1936936c09acSJohn Baldwin {
193793c5d3a4SKonstantin Belousov 	struct mtx *mtx;
1938936c09acSJohn Baldwin 	vm_page_t p, next;
1939936c09acSJohn Baldwin 
194052d1addaSAlan Cox 	VM_OBJECT_ASSERT_LOCKED(object);
194128634820SAlan Cox 	KASSERT((object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0,
19423138cd36SMark Johnston 	    ("vm_object_page_noreuse: illegal object %p", object));
1943936c09acSJohn Baldwin 	if (object->resident_page_count == 0)
1944936c09acSJohn Baldwin 		return;
1945936c09acSJohn Baldwin 	p = vm_page_find_least(object, start);
1946936c09acSJohn Baldwin 
1947936c09acSJohn Baldwin 	/*
1948936c09acSJohn Baldwin 	 * Here, the variable "p" is either (1) the page with the least pindex
1949936c09acSJohn Baldwin 	 * greater than or equal to the parameter "start" or (2) NULL.
1950936c09acSJohn Baldwin 	 */
1951936c09acSJohn Baldwin 	mtx = NULL;
1952936c09acSJohn Baldwin 	for (; p != NULL && (p->pindex < end || end == 0); p = next) {
1953936c09acSJohn Baldwin 		next = TAILQ_NEXT(p, listq);
195493c5d3a4SKonstantin Belousov 		vm_page_change_lock(p, &mtx);
19553138cd36SMark Johnston 		vm_page_deactivate_noreuse(p);
1956936c09acSJohn Baldwin 	}
1957936c09acSJohn Baldwin 	if (mtx != NULL)
1958936c09acSJohn Baldwin 		mtx_unlock(mtx);
1959936c09acSJohn Baldwin }
1960936c09acSJohn Baldwin 
1961936c09acSJohn Baldwin /*
1962387aabc5SAlan Cox  *	Populate the specified range of the object with valid pages.  Returns
1963387aabc5SAlan Cox  *	TRUE if the range is successfully populated and FALSE otherwise.
1964387aabc5SAlan Cox  *
1965387aabc5SAlan Cox  *	Note: This function should be optimized to pass a larger array of
1966387aabc5SAlan Cox  *	pages to vm_pager_get_pages() before it is applied to a non-
1967387aabc5SAlan Cox  *	OBJT_DEVICE object.
1968387aabc5SAlan Cox  *
1969387aabc5SAlan Cox  *	The object must be locked.
1970387aabc5SAlan Cox  */
1971387aabc5SAlan Cox boolean_t
1972387aabc5SAlan Cox vm_object_populate(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1973387aabc5SAlan Cox {
1974093c7f39SGleb Smirnoff 	vm_page_t m;
1975387aabc5SAlan Cox 	vm_pindex_t pindex;
1976387aabc5SAlan Cox 	int rv;
1977387aabc5SAlan Cox 
197889f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
1979387aabc5SAlan Cox 	for (pindex = start; pindex < end; pindex++) {
1980c7575748SJeff Roberson 		rv = vm_page_grab_valid(&m, object, pindex, VM_ALLOC_NORMAL);
1981c7575748SJeff Roberson 		if (rv != VM_PAGER_OK)
1982387aabc5SAlan Cox 			break;
1983c7575748SJeff Roberson 
1984387aabc5SAlan Cox 		/*
1985387aabc5SAlan Cox 		 * Keep "m" busy because a subsequent iteration may unlock
1986387aabc5SAlan Cox 		 * the object.
1987387aabc5SAlan Cox 		 */
1988387aabc5SAlan Cox 	}
1989387aabc5SAlan Cox 	if (pindex > start) {
1990387aabc5SAlan Cox 		m = vm_page_lookup(object, start);
1991387aabc5SAlan Cox 		while (m != NULL && m->pindex < pindex) {
1992c7aebda8SAttilio Rao 			vm_page_xunbusy(m);
1993387aabc5SAlan Cox 			m = TAILQ_NEXT(m, listq);
1994387aabc5SAlan Cox 		}
1995387aabc5SAlan Cox 	}
1996387aabc5SAlan Cox 	return (pindex == end);
1997387aabc5SAlan Cox }
1998387aabc5SAlan Cox 
1999387aabc5SAlan Cox /*
2000df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
2001df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
2002df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
2003df8bae1dSRodney W. Grimes  *
2004df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
2005df8bae1dSRodney W. Grimes  *
2006df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
2007df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
2008df8bae1dSRodney W. Grimes  *
2009df8bae1dSRodney W. Grimes  *	Parameters:
2010df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
2011df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
2012df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
201357a21abaSAlan Cox  *		next_size	Size of reference to the second object
20143364c323SKonstantin Belousov  *		reserved	Indicator that extension region has
20153364c323SKonstantin Belousov  *				swap accounted for
2016df8bae1dSRodney W. Grimes  *
2017df8bae1dSRodney W. Grimes  *	Conditions:
2018df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
2019df8bae1dSRodney W. Grimes  */
20200d94caffSDavid Greenman boolean_t
202157a21abaSAlan Cox vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset,
20223364c323SKonstantin Belousov     vm_size_t prev_size, vm_size_t next_size, boolean_t reserved)
2023df8bae1dSRodney W. Grimes {
2024ea41812fSAlan Cox 	vm_pindex_t next_pindex;
2025df8bae1dSRodney W. Grimes 
202600e1854aSAlan Cox 	if (prev_object == NULL)
2027df8bae1dSRodney W. Grimes 		return (TRUE);
202889f6b863SAttilio Rao 	VM_OBJECT_WLOCK(prev_object);
20299ded9474SKonstantin Belousov 	if ((prev_object->type != OBJT_DEFAULT &&
20309ded9474SKonstantin Belousov 	    prev_object->type != OBJT_SWAP) ||
20314f49b435SKonstantin Belousov 	    (prev_object->flags & OBJ_NOSPLIT) != 0) {
203289f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(prev_object);
203330dcfc09SJohn Dyson 		return (FALSE);
203430dcfc09SJohn Dyson 	}
203530dcfc09SJohn Dyson 
2036df8bae1dSRodney W. Grimes 	/*
2037df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
2038df8bae1dSRodney W. Grimes 	 */
2039df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
2040df8bae1dSRodney W. Grimes 
2041df8bae1dSRodney W. Grimes 	/*
20420d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
20430d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
20440d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
2045df8bae1dSRodney W. Grimes 	 */
20468cc7e047SJohn Dyson 	if (prev_object->backing_object != NULL) {
204789f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(prev_object);
2048df8bae1dSRodney W. Grimes 		return (FALSE);
2049df8bae1dSRodney W. Grimes 	}
2050a316d390SJohn Dyson 
2051a316d390SJohn Dyson 	prev_size >>= PAGE_SHIFT;
2052a316d390SJohn Dyson 	next_size >>= PAGE_SHIFT;
205357a21abaSAlan Cox 	next_pindex = OFF_TO_IDX(prev_offset) + prev_size;
20548cc7e047SJohn Dyson 
20550e48e068SMark Johnston 	if (prev_object->ref_count > 1 &&
20560e48e068SMark Johnston 	    prev_object->size != next_pindex &&
20570e48e068SMark Johnston 	    (prev_object->flags & OBJ_ONEMAPPING) == 0) {
205889f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(prev_object);
20598cc7e047SJohn Dyson 		return (FALSE);
20608cc7e047SJohn Dyson 	}
20618cc7e047SJohn Dyson 
2062df8bae1dSRodney W. Grimes 	/*
20633364c323SKonstantin Belousov 	 * Account for the charge.
20643364c323SKonstantin Belousov 	 */
2065ef694c1aSEdward Tomasz Napierala 	if (prev_object->cred != NULL) {
20663364c323SKonstantin Belousov 
20673364c323SKonstantin Belousov 		/*
20683364c323SKonstantin Belousov 		 * If prev_object was charged, then this mapping,
2069763df3ecSPedro F. Giffuni 		 * although not charged now, may become writable
2070ef694c1aSEdward Tomasz Napierala 		 * later. Non-NULL cred in the object would prevent
20713364c323SKonstantin Belousov 		 * swap reservation during enabling of the write
20723364c323SKonstantin Belousov 		 * access, so reserve swap now. Failed reservation
20733364c323SKonstantin Belousov 		 * cause allocation of the separate object for the map
20743364c323SKonstantin Belousov 		 * entry, and swap reservation for this entry is
20753364c323SKonstantin Belousov 		 * managed in appropriate time.
20763364c323SKonstantin Belousov 		 */
2077ef694c1aSEdward Tomasz Napierala 		if (!reserved && !swap_reserve_by_cred(ptoa(next_size),
2078ef694c1aSEdward Tomasz Napierala 		    prev_object->cred)) {
20799f790a17SKonstantin Belousov 			VM_OBJECT_WUNLOCK(prev_object);
20803364c323SKonstantin Belousov 			return (FALSE);
20813364c323SKonstantin Belousov 		}
20823364c323SKonstantin Belousov 		prev_object->charge += ptoa(next_size);
20833364c323SKonstantin Belousov 	}
20843364c323SKonstantin Belousov 
20853364c323SKonstantin Belousov 	/*
20860d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
20870d94caffSDavid Greenman 	 * deallocation.
2088df8bae1dSRodney W. Grimes 	 */
2089ea41812fSAlan Cox 	if (next_pindex < prev_object->size) {
20906bbee8e2SAlan Cox 		vm_object_page_remove(prev_object, next_pindex, next_pindex +
20916bbee8e2SAlan Cox 		    next_size, 0);
2092ea41812fSAlan Cox 		if (prev_object->type == OBJT_SWAP)
2093ea41812fSAlan Cox 			swap_pager_freespace(prev_object,
2094ea41812fSAlan Cox 					     next_pindex, next_size);
20953364c323SKonstantin Belousov #if 0
2096ef694c1aSEdward Tomasz Napierala 		if (prev_object->cred != NULL) {
20973364c323SKonstantin Belousov 			KASSERT(prev_object->charge >=
20983364c323SKonstantin Belousov 			    ptoa(prev_object->size - next_pindex),
20993364c323SKonstantin Belousov 			    ("object %p overcharged 1 %jx %jx", prev_object,
21003364c323SKonstantin Belousov 				(uintmax_t)next_pindex, (uintmax_t)next_size));
21013364c323SKonstantin Belousov 			prev_object->charge -= ptoa(prev_object->size -
21023364c323SKonstantin Belousov 			    next_pindex);
21033364c323SKonstantin Belousov 		}
21043364c323SKonstantin Belousov #endif
2105ea41812fSAlan Cox 	}
2106df8bae1dSRodney W. Grimes 
2107df8bae1dSRodney W. Grimes 	/*
2108df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
2109df8bae1dSRodney W. Grimes 	 */
2110ea41812fSAlan Cox 	if (next_pindex + next_size > prev_object->size)
2111ea41812fSAlan Cox 		prev_object->size = next_pindex + next_size;
2112df8bae1dSRodney W. Grimes 
211389f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(prev_object);
2114df8bae1dSRodney W. Grimes 	return (TRUE);
2115df8bae1dSRodney W. Grimes }
2116df8bae1dSRodney W. Grimes 
21177a5a6352SMatthew Dillon void
21187a5a6352SMatthew Dillon vm_object_set_writeable_dirty(vm_object_t object)
21197a5a6352SMatthew Dillon {
21207a5a6352SMatthew Dillon 
212189f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
2122f40cb1c6SKonstantin Belousov 	if (object->type != OBJT_VNODE) {
2123f40cb1c6SKonstantin Belousov 		if ((object->flags & OBJ_TMPFS_NODE) != 0) {
2124f40cb1c6SKonstantin Belousov 			KASSERT(object->type == OBJT_SWAP, ("non-swap tmpfs"));
2125f40cb1c6SKonstantin Belousov 			vm_object_set_flag(object, OBJ_TMPFS_DIRTY);
2126f40cb1c6SKonstantin Belousov 		}
21273280870dSKonstantin Belousov 		return;
2128f40cb1c6SKonstantin Belousov 	}
21293280870dSKonstantin Belousov 	object->generation++;
21303280870dSKonstantin Belousov 	if ((object->flags & OBJ_MIGHTBEDIRTY) != 0)
2131ee39666aSJeff Roberson 		return;
2132af51d7bfSAlan Cox 	vm_object_set_flag(object, OBJ_MIGHTBEDIRTY);
21337a5a6352SMatthew Dillon }
21347a5a6352SMatthew Dillon 
213503462509SAlan Cox /*
213603462509SAlan Cox  *	vm_object_unwire:
213703462509SAlan Cox  *
213803462509SAlan Cox  *	For each page offset within the specified range of the given object,
213903462509SAlan Cox  *	find the highest-level page in the shadow chain and unwire it.  A page
214003462509SAlan Cox  *	must exist at every page offset, and the highest-level page must be
214103462509SAlan Cox  *	wired.
214203462509SAlan Cox  */
214303462509SAlan Cox void
214403462509SAlan Cox vm_object_unwire(vm_object_t object, vm_ooffset_t offset, vm_size_t length,
214503462509SAlan Cox     uint8_t queue)
214603462509SAlan Cox {
214720e4afbfSKonstantin Belousov 	vm_object_t tobject, t1object;
214803462509SAlan Cox 	vm_page_t m, tm;
214903462509SAlan Cox 	vm_pindex_t end_pindex, pindex, tpindex;
215003462509SAlan Cox 	int depth, locked_depth;
215103462509SAlan Cox 
215203462509SAlan Cox 	KASSERT((offset & PAGE_MASK) == 0,
215303462509SAlan Cox 	    ("vm_object_unwire: offset is not page aligned"));
215403462509SAlan Cox 	KASSERT((length & PAGE_MASK) == 0,
215503462509SAlan Cox 	    ("vm_object_unwire: length is not a multiple of PAGE_SIZE"));
215603462509SAlan Cox 	/* The wired count of a fictitious page never changes. */
215703462509SAlan Cox 	if ((object->flags & OBJ_FICTITIOUS) != 0)
215803462509SAlan Cox 		return;
215903462509SAlan Cox 	pindex = OFF_TO_IDX(offset);
216003462509SAlan Cox 	end_pindex = pindex + atop(length);
216120e4afbfSKonstantin Belousov again:
216203462509SAlan Cox 	locked_depth = 1;
216303462509SAlan Cox 	VM_OBJECT_RLOCK(object);
216403462509SAlan Cox 	m = vm_page_find_least(object, pindex);
216503462509SAlan Cox 	while (pindex < end_pindex) {
216603462509SAlan Cox 		if (m == NULL || pindex < m->pindex) {
216703462509SAlan Cox 			/*
216803462509SAlan Cox 			 * The first object in the shadow chain doesn't
216903462509SAlan Cox 			 * contain a page at the current index.  Therefore,
217003462509SAlan Cox 			 * the page must exist in a backing object.
217103462509SAlan Cox 			 */
217203462509SAlan Cox 			tobject = object;
217303462509SAlan Cox 			tpindex = pindex;
217403462509SAlan Cox 			depth = 0;
217503462509SAlan Cox 			do {
217603462509SAlan Cox 				tpindex +=
217703462509SAlan Cox 				    OFF_TO_IDX(tobject->backing_object_offset);
217803462509SAlan Cox 				tobject = tobject->backing_object;
217903462509SAlan Cox 				KASSERT(tobject != NULL,
218003462509SAlan Cox 				    ("vm_object_unwire: missing page"));
218103462509SAlan Cox 				if ((tobject->flags & OBJ_FICTITIOUS) != 0)
218203462509SAlan Cox 					goto next_page;
218303462509SAlan Cox 				depth++;
218403462509SAlan Cox 				if (depth == locked_depth) {
218503462509SAlan Cox 					locked_depth++;
218603462509SAlan Cox 					VM_OBJECT_RLOCK(tobject);
218703462509SAlan Cox 				}
218803462509SAlan Cox 			} while ((tm = vm_page_lookup(tobject, tpindex)) ==
218903462509SAlan Cox 			    NULL);
219003462509SAlan Cox 		} else {
219103462509SAlan Cox 			tm = m;
219203462509SAlan Cox 			m = TAILQ_NEXT(m, listq);
219303462509SAlan Cox 		}
219463e97555SJeff Roberson 		if (vm_page_trysbusy(tm) == 0) {
219587e93ea6SMark Johnston 			for (tobject = object; locked_depth >= 1;
219620e4afbfSKonstantin Belousov 			    locked_depth--) {
219720e4afbfSKonstantin Belousov 				t1object = tobject->backing_object;
219887e93ea6SMark Johnston 				if (tm->object != tobject)
219920e4afbfSKonstantin Belousov 					VM_OBJECT_RUNLOCK(tobject);
220020e4afbfSKonstantin Belousov 				tobject = t1object;
220120e4afbfSKonstantin Belousov 			}
220220e4afbfSKonstantin Belousov 			vm_page_busy_sleep(tm, "unwbo", true);
220320e4afbfSKonstantin Belousov 			goto again;
220420e4afbfSKonstantin Belousov 		}
220503462509SAlan Cox 		vm_page_unwire(tm, queue);
220663e97555SJeff Roberson 		vm_page_sunbusy(tm);
220703462509SAlan Cox next_page:
220803462509SAlan Cox 		pindex++;
220903462509SAlan Cox 	}
221003462509SAlan Cox 	/* Release the accumulated object locks. */
221120e4afbfSKonstantin Belousov 	for (tobject = object; locked_depth >= 1; locked_depth--) {
221220e4afbfSKonstantin Belousov 		t1object = tobject->backing_object;
221320e4afbfSKonstantin Belousov 		VM_OBJECT_RUNLOCK(tobject);
221420e4afbfSKonstantin Belousov 		tobject = t1object;
221503462509SAlan Cox 	}
221603462509SAlan Cox }
221703462509SAlan Cox 
22180951bd36SEric van Gyzen /*
22190951bd36SEric van Gyzen  * Return the vnode for the given object, or NULL if none exists.
22200951bd36SEric van Gyzen  * For tmpfs objects, the function may return NULL if there is
22210951bd36SEric van Gyzen  * no vnode allocated at the time of the call.
22220951bd36SEric van Gyzen  */
222363e4c6cdSEric van Gyzen struct vnode *
222463e4c6cdSEric van Gyzen vm_object_vnode(vm_object_t object)
222563e4c6cdSEric van Gyzen {
22260951bd36SEric van Gyzen 	struct vnode *vp;
222763e4c6cdSEric van Gyzen 
222863e4c6cdSEric van Gyzen 	VM_OBJECT_ASSERT_LOCKED(object);
22290951bd36SEric van Gyzen 	if (object->type == OBJT_VNODE) {
22300951bd36SEric van Gyzen 		vp = object->handle;
22310951bd36SEric van Gyzen 		KASSERT(vp != NULL, ("%s: OBJT_VNODE has no vnode", __func__));
22320951bd36SEric van Gyzen 	} else if (object->type == OBJT_SWAP &&
22330951bd36SEric van Gyzen 	    (object->flags & OBJ_TMPFS) != 0) {
22340951bd36SEric van Gyzen 		vp = object->un_pager.swp.swp_tmpfs;
22350951bd36SEric van Gyzen 		KASSERT(vp != NULL, ("%s: OBJT_TMPFS has no vnode", __func__));
22360951bd36SEric van Gyzen 	} else {
22370951bd36SEric van Gyzen 		vp = NULL;
22380951bd36SEric van Gyzen 	}
22390951bd36SEric van Gyzen 	return (vp);
224063e4c6cdSEric van Gyzen }
224163e4c6cdSEric van Gyzen 
22425e38e3f5SEric van Gyzen /*
22435e38e3f5SEric van Gyzen  * Return the kvme type of the given object.
22445e38e3f5SEric van Gyzen  * If vpp is not NULL, set it to the object's vm_object_vnode() or NULL.
22455e38e3f5SEric van Gyzen  */
22465e38e3f5SEric van Gyzen int
22475e38e3f5SEric van Gyzen vm_object_kvme_type(vm_object_t object, struct vnode **vpp)
22485e38e3f5SEric van Gyzen {
22495e38e3f5SEric van Gyzen 
22505e38e3f5SEric van Gyzen 	VM_OBJECT_ASSERT_LOCKED(object);
22515e38e3f5SEric van Gyzen 	if (vpp != NULL)
22525e38e3f5SEric van Gyzen 		*vpp = vm_object_vnode(object);
22535e38e3f5SEric van Gyzen 	switch (object->type) {
22545e38e3f5SEric van Gyzen 	case OBJT_DEFAULT:
22555e38e3f5SEric van Gyzen 		return (KVME_TYPE_DEFAULT);
22565e38e3f5SEric van Gyzen 	case OBJT_VNODE:
22575e38e3f5SEric van Gyzen 		return (KVME_TYPE_VNODE);
22585e38e3f5SEric van Gyzen 	case OBJT_SWAP:
22595e38e3f5SEric van Gyzen 		if ((object->flags & OBJ_TMPFS_NODE) != 0)
22605e38e3f5SEric van Gyzen 			return (KVME_TYPE_VNODE);
22615e38e3f5SEric van Gyzen 		return (KVME_TYPE_SWAP);
22625e38e3f5SEric van Gyzen 	case OBJT_DEVICE:
22635e38e3f5SEric van Gyzen 		return (KVME_TYPE_DEVICE);
22645e38e3f5SEric van Gyzen 	case OBJT_PHYS:
22655e38e3f5SEric van Gyzen 		return (KVME_TYPE_PHYS);
22665e38e3f5SEric van Gyzen 	case OBJT_DEAD:
22675e38e3f5SEric van Gyzen 		return (KVME_TYPE_DEAD);
22685e38e3f5SEric van Gyzen 	case OBJT_SG:
22695e38e3f5SEric van Gyzen 		return (KVME_TYPE_SG);
22705e38e3f5SEric van Gyzen 	case OBJT_MGTDEVICE:
22715e38e3f5SEric van Gyzen 		return (KVME_TYPE_MGTDEVICE);
22725e38e3f5SEric van Gyzen 	default:
22735e38e3f5SEric van Gyzen 		return (KVME_TYPE_UNKNOWN);
22745e38e3f5SEric van Gyzen 	}
22755e38e3f5SEric van Gyzen }
22765e38e3f5SEric van Gyzen 
2277ff87ae35SJohn Baldwin static int
2278ff87ae35SJohn Baldwin sysctl_vm_object_list(SYSCTL_HANDLER_ARGS)
2279ff87ae35SJohn Baldwin {
22800ecee546SKonstantin Belousov 	struct kinfo_vmobject *kvo;
2281ff87ae35SJohn Baldwin 	char *fullpath, *freepath;
2282ff87ae35SJohn Baldwin 	struct vnode *vp;
2283ff87ae35SJohn Baldwin 	struct vattr va;
2284ff87ae35SJohn Baldwin 	vm_object_t obj;
2285ff87ae35SJohn Baldwin 	vm_page_t m;
2286ff87ae35SJohn Baldwin 	int count, error;
2287ff87ae35SJohn Baldwin 
2288ff87ae35SJohn Baldwin 	if (req->oldptr == NULL) {
2289ff87ae35SJohn Baldwin 		/*
2290ff87ae35SJohn Baldwin 		 * If an old buffer has not been provided, generate an
2291ff87ae35SJohn Baldwin 		 * estimate of the space needed for a subsequent call.
2292ff87ae35SJohn Baldwin 		 */
2293ff87ae35SJohn Baldwin 		mtx_lock(&vm_object_list_mtx);
2294ff87ae35SJohn Baldwin 		count = 0;
2295ff87ae35SJohn Baldwin 		TAILQ_FOREACH(obj, &vm_object_list, object_list) {
2296ff87ae35SJohn Baldwin 			if (obj->type == OBJT_DEAD)
2297ff87ae35SJohn Baldwin 				continue;
2298ff87ae35SJohn Baldwin 			count++;
2299ff87ae35SJohn Baldwin 		}
2300ff87ae35SJohn Baldwin 		mtx_unlock(&vm_object_list_mtx);
2301ff87ae35SJohn Baldwin 		return (SYSCTL_OUT(req, NULL, sizeof(struct kinfo_vmobject) *
2302ff87ae35SJohn Baldwin 		    count * 11 / 10));
2303ff87ae35SJohn Baldwin 	}
2304ff87ae35SJohn Baldwin 
23050ecee546SKonstantin Belousov 	kvo = malloc(sizeof(*kvo), M_TEMP, M_WAITOK);
2306ff87ae35SJohn Baldwin 	error = 0;
2307ff87ae35SJohn Baldwin 
2308ff87ae35SJohn Baldwin 	/*
2309ff87ae35SJohn Baldwin 	 * VM objects are type stable and are never removed from the
2310ff87ae35SJohn Baldwin 	 * list once added.  This allows us to safely read obj->object_list
2311ff87ae35SJohn Baldwin 	 * after reacquiring the VM object lock.
2312ff87ae35SJohn Baldwin 	 */
2313ff87ae35SJohn Baldwin 	mtx_lock(&vm_object_list_mtx);
2314ff87ae35SJohn Baldwin 	TAILQ_FOREACH(obj, &vm_object_list, object_list) {
2315ff87ae35SJohn Baldwin 		if (obj->type == OBJT_DEAD)
2316ff87ae35SJohn Baldwin 			continue;
2317ff87ae35SJohn Baldwin 		VM_OBJECT_RLOCK(obj);
2318ff87ae35SJohn Baldwin 		if (obj->type == OBJT_DEAD) {
2319ff87ae35SJohn Baldwin 			VM_OBJECT_RUNLOCK(obj);
2320ff87ae35SJohn Baldwin 			continue;
2321ff87ae35SJohn Baldwin 		}
2322ff87ae35SJohn Baldwin 		mtx_unlock(&vm_object_list_mtx);
23230ecee546SKonstantin Belousov 		kvo->kvo_size = ptoa(obj->size);
23240ecee546SKonstantin Belousov 		kvo->kvo_resident = obj->resident_page_count;
23250ecee546SKonstantin Belousov 		kvo->kvo_ref_count = obj->ref_count;
23260ecee546SKonstantin Belousov 		kvo->kvo_shadow_count = obj->shadow_count;
23270ecee546SKonstantin Belousov 		kvo->kvo_memattr = obj->memattr;
23280ecee546SKonstantin Belousov 		kvo->kvo_active = 0;
23290ecee546SKonstantin Belousov 		kvo->kvo_inactive = 0;
2330ff87ae35SJohn Baldwin 		TAILQ_FOREACH(m, &obj->memq, listq) {
2331ff87ae35SJohn Baldwin 			/*
2332ff87ae35SJohn Baldwin 			 * A page may belong to the object but be
2333ff87ae35SJohn Baldwin 			 * dequeued and set to PQ_NONE while the
2334ff87ae35SJohn Baldwin 			 * object lock is not held.  This makes the
2335ff87ae35SJohn Baldwin 			 * reads of m->queue below racy, and we do not
2336ff87ae35SJohn Baldwin 			 * count pages set to PQ_NONE.  However, this
2337ff87ae35SJohn Baldwin 			 * sysctl is only meant to give an
2338ff87ae35SJohn Baldwin 			 * approximation of the system anyway.
2339ff87ae35SJohn Baldwin 			 */
2340e8bcf696SMark Johnston 			if (m->queue == PQ_ACTIVE)
23410ecee546SKonstantin Belousov 				kvo->kvo_active++;
2342e8bcf696SMark Johnston 			else if (m->queue == PQ_INACTIVE)
23430ecee546SKonstantin Belousov 				kvo->kvo_inactive++;
2344ff87ae35SJohn Baldwin 		}
2345ff87ae35SJohn Baldwin 
23460ecee546SKonstantin Belousov 		kvo->kvo_vn_fileid = 0;
23470ecee546SKonstantin Belousov 		kvo->kvo_vn_fsid = 0;
23480ecee546SKonstantin Belousov 		kvo->kvo_vn_fsid_freebsd11 = 0;
2349ff87ae35SJohn Baldwin 		freepath = NULL;
2350ff87ae35SJohn Baldwin 		fullpath = "";
23515e38e3f5SEric van Gyzen 		kvo->kvo_type = vm_object_kvme_type(obj, &vp);
23525e38e3f5SEric van Gyzen 		if (vp != NULL)
2353ff87ae35SJohn Baldwin 			vref(vp);
2354ff87ae35SJohn Baldwin 		VM_OBJECT_RUNLOCK(obj);
2355ff87ae35SJohn Baldwin 		if (vp != NULL) {
2356ff87ae35SJohn Baldwin 			vn_fullpath(curthread, vp, &fullpath, &freepath);
2357ff87ae35SJohn Baldwin 			vn_lock(vp, LK_SHARED | LK_RETRY);
2358ff87ae35SJohn Baldwin 			if (VOP_GETATTR(vp, &va, curthread->td_ucred) == 0) {
23590ecee546SKonstantin Belousov 				kvo->kvo_vn_fileid = va.va_fileid;
23600ecee546SKonstantin Belousov 				kvo->kvo_vn_fsid = va.va_fsid;
23610ecee546SKonstantin Belousov 				kvo->kvo_vn_fsid_freebsd11 = va.va_fsid;
236269921123SKonstantin Belousov 								/* truncate */
2363ff87ae35SJohn Baldwin 			}
2364ff87ae35SJohn Baldwin 			vput(vp);
2365ff87ae35SJohn Baldwin 		}
2366ff87ae35SJohn Baldwin 
23670ecee546SKonstantin Belousov 		strlcpy(kvo->kvo_path, fullpath, sizeof(kvo->kvo_path));
2368ff87ae35SJohn Baldwin 		if (freepath != NULL)
2369ff87ae35SJohn Baldwin 			free(freepath, M_TEMP);
2370ff87ae35SJohn Baldwin 
2371ff87ae35SJohn Baldwin 		/* Pack record size down */
23720ecee546SKonstantin Belousov 		kvo->kvo_structsize = offsetof(struct kinfo_vmobject, kvo_path)
23730ecee546SKonstantin Belousov 		    + strlen(kvo->kvo_path) + 1;
23740ecee546SKonstantin Belousov 		kvo->kvo_structsize = roundup(kvo->kvo_structsize,
2375ff87ae35SJohn Baldwin 		    sizeof(uint64_t));
23760ecee546SKonstantin Belousov 		error = SYSCTL_OUT(req, kvo, kvo->kvo_structsize);
2377ff87ae35SJohn Baldwin 		mtx_lock(&vm_object_list_mtx);
2378ff87ae35SJohn Baldwin 		if (error)
2379ff87ae35SJohn Baldwin 			break;
2380ff87ae35SJohn Baldwin 	}
2381ff87ae35SJohn Baldwin 	mtx_unlock(&vm_object_list_mtx);
23820ecee546SKonstantin Belousov 	free(kvo, M_TEMP);
2383ff87ae35SJohn Baldwin 	return (error);
2384ff87ae35SJohn Baldwin }
2385ff87ae35SJohn Baldwin SYSCTL_PROC(_vm, OID_AUTO, objects, CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_SKIP |
2386ff87ae35SJohn Baldwin     CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_object_list, "S,kinfo_vmobject",
2387ff87ae35SJohn Baldwin     "List of VM objects");
2388ff87ae35SJohn Baldwin 
2389c7c34a24SBruce Evans #include "opt_ddb.h"
2390c3cb3e12SDavid Greenman #ifdef DDB
2391c7c34a24SBruce Evans #include <sys/kernel.h>
2392c7c34a24SBruce Evans 
2393ce9edcf5SPoul-Henning Kamp #include <sys/cons.h>
2394c7c34a24SBruce Evans 
2395c7c34a24SBruce Evans #include <ddb/ddb.h>
2396c7c34a24SBruce Evans 
2397cac597e4SBruce Evans static int
23981b40f8c0SMatthew Dillon _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
2399a1f6d91cSDavid Greenman {
2400a1f6d91cSDavid Greenman 	vm_map_t tmpm;
2401a1f6d91cSDavid Greenman 	vm_map_entry_t tmpe;
2402a1f6d91cSDavid Greenman 	vm_object_t obj;
2403a1f6d91cSDavid Greenman 
2404a1f6d91cSDavid Greenman 	if (map == 0)
2405a1f6d91cSDavid Greenman 		return 0;
2406a1f6d91cSDavid Greenman 
2407a1f6d91cSDavid Greenman 	if (entry == 0) {
24082288078cSDoug Moore 		VM_MAP_ENTRY_FOREACH(tmpe, map) {
2409a1f6d91cSDavid Greenman 			if (_vm_object_in_map(map, object, tmpe)) {
2410a1f6d91cSDavid Greenman 				return 1;
2411a1f6d91cSDavid Greenman 			}
2412a1f6d91cSDavid Greenman 		}
24139fdfe602SMatthew Dillon 	} else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
24149fdfe602SMatthew Dillon 		tmpm = entry->object.sub_map;
24152288078cSDoug Moore 		VM_MAP_ENTRY_FOREACH(tmpe, tmpm) {
2416a1f6d91cSDavid Greenman 			if (_vm_object_in_map(tmpm, object, tmpe)) {
2417a1f6d91cSDavid Greenman 				return 1;
2418a1f6d91cSDavid Greenman 			}
2419a1f6d91cSDavid Greenman 		}
24208aef1712SMatthew Dillon 	} else if ((obj = entry->object.vm_object) != NULL) {
242124a1cce3SDavid Greenman 		for (; obj; obj = obj->backing_object)
2422a1f6d91cSDavid Greenman 			if (obj == object) {
2423a1f6d91cSDavid Greenman 				return 1;
2424a1f6d91cSDavid Greenman 			}
2425a1f6d91cSDavid Greenman 	}
2426a1f6d91cSDavid Greenman 	return 0;
2427a1f6d91cSDavid Greenman }
2428a1f6d91cSDavid Greenman 
2429cac597e4SBruce Evans static int
24301b40f8c0SMatthew Dillon vm_object_in_map(vm_object_t object)
2431a1f6d91cSDavid Greenman {
2432a1f6d91cSDavid Greenman 	struct proc *p;
24331005a129SJohn Baldwin 
243460517fd1SJohn Baldwin 	/* sx_slock(&allproc_lock); */
2435f67af5c9SXin LI 	FOREACH_PROC_IN_SYSTEM(p) {
2436a1f6d91cSDavid Greenman 		if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
2437a1f6d91cSDavid Greenman 			continue;
2438553629ebSJake Burkholder 		if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
243960517fd1SJohn Baldwin 			/* sx_sunlock(&allproc_lock); */
2440a1f6d91cSDavid Greenman 			return 1;
2441a1f6d91cSDavid Greenman 		}
2442553629ebSJake Burkholder 	}
244360517fd1SJohn Baldwin 	/* sx_sunlock(&allproc_lock); */
2444a1f6d91cSDavid Greenman 	if (_vm_object_in_map(kernel_map, object, 0))
2445a1f6d91cSDavid Greenman 		return 1;
2446a1f6d91cSDavid Greenman 	return 0;
2447a1f6d91cSDavid Greenman }
2448a1f6d91cSDavid Greenman 
2449c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check)
2450f708ef1bSPoul-Henning Kamp {
2451a1f6d91cSDavid Greenman 	vm_object_t object;
2452a1f6d91cSDavid Greenman 
2453a1f6d91cSDavid Greenman 	/*
2454a1f6d91cSDavid Greenman 	 * make sure that internal objs are in a map somewhere
2455a1f6d91cSDavid Greenman 	 * and none have zero ref counts.
2456a1f6d91cSDavid Greenman 	 */
2457cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
245824a1cce3SDavid Greenman 		if (object->handle == NULL &&
245924a1cce3SDavid Greenman 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2460a1f6d91cSDavid Greenman 			if (object->ref_count == 0) {
24613efc015bSPeter Wemm 				db_printf("vmochk: internal obj has zero ref count: %ld\n",
24623efc015bSPeter Wemm 					(long)object->size);
2463a1f6d91cSDavid Greenman 			}
2464a1f6d91cSDavid Greenman 			if (!vm_object_in_map(object)) {
2465fc62ef1fSBruce Evans 				db_printf(
2466fc62ef1fSBruce Evans 			"vmochk: internal obj is not in a map: "
2467fc62ef1fSBruce Evans 			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2468fc62ef1fSBruce Evans 				    object->ref_count, (u_long)object->size,
2469fc62ef1fSBruce Evans 				    (u_long)object->size,
2470fc62ef1fSBruce Evans 				    (void *)object->backing_object);
2471a1f6d91cSDavid Greenman 			}
2472a1f6d91cSDavid Greenman 		}
2473a1f6d91cSDavid Greenman 	}
2474a1f6d91cSDavid Greenman }
2475a1f6d91cSDavid Greenman 
247626f9a767SRodney W. Grimes /*
2477df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
2478df8bae1dSRodney W. Grimes  */
2479c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static)
2480df8bae1dSRodney W. Grimes {
2481c7c34a24SBruce Evans 	/* XXX convert args. */
2482c7c34a24SBruce Evans 	vm_object_t object = (vm_object_t)addr;
2483c7c34a24SBruce Evans 	boolean_t full = have_addr;
2484c7c34a24SBruce Evans 
2485d031cff1SMatthew Dillon 	vm_page_t p;
2486df8bae1dSRodney W. Grimes 
2487c7c34a24SBruce Evans 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
2488c7c34a24SBruce Evans #define	count	was_count
2489c7c34a24SBruce Evans 
2490d031cff1SMatthew Dillon 	int count;
2491df8bae1dSRodney W. Grimes 
2492df8bae1dSRodney W. Grimes 	if (object == NULL)
2493df8bae1dSRodney W. Grimes 		return;
2494df8bae1dSRodney W. Grimes 
2495eb95adefSBruce Evans 	db_iprintf(
2496ef694c1aSEdward Tomasz Napierala 	    "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x ruid %d charge %jx\n",
2497e47cd172SMaxime Henrion 	    object, (int)object->type, (uintmax_t)object->size,
24983364c323SKonstantin Belousov 	    object->resident_page_count, object->ref_count, object->flags,
2499ef694c1aSEdward Tomasz Napierala 	    object->cred ? object->cred->cr_ruid : -1, (uintmax_t)object->charge);
2500e47cd172SMaxime Henrion 	db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n",
25011c7c3c6aSMatthew Dillon 	    object->shadow_count,
2502eb95adefSBruce Evans 	    object->backing_object ? object->backing_object->ref_count : 0,
2503e47cd172SMaxime Henrion 	    object->backing_object, (uintmax_t)object->backing_object_offset);
2504df8bae1dSRodney W. Grimes 
2505df8bae1dSRodney W. Grimes 	if (!full)
2506df8bae1dSRodney W. Grimes 		return;
2507df8bae1dSRodney W. Grimes 
2508c7c34a24SBruce Evans 	db_indent += 2;
2509df8bae1dSRodney W. Grimes 	count = 0;
2510fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(p, &object->memq, listq) {
2511df8bae1dSRodney W. Grimes 		if (count == 0)
2512c7c34a24SBruce Evans 			db_iprintf("memory:=");
2513df8bae1dSRodney W. Grimes 		else if (count == 6) {
2514c7c34a24SBruce Evans 			db_printf("\n");
2515c7c34a24SBruce Evans 			db_iprintf(" ...");
2516df8bae1dSRodney W. Grimes 			count = 0;
2517df8bae1dSRodney W. Grimes 		} else
2518c7c34a24SBruce Evans 			db_printf(",");
2519df8bae1dSRodney W. Grimes 		count++;
2520df8bae1dSRodney W. Grimes 
2521e47cd172SMaxime Henrion 		db_printf("(off=0x%jx,page=0x%jx)",
2522e47cd172SMaxime Henrion 		    (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p));
2523df8bae1dSRodney W. Grimes 	}
2524df8bae1dSRodney W. Grimes 	if (count != 0)
2525c7c34a24SBruce Evans 		db_printf("\n");
2526c7c34a24SBruce Evans 	db_indent -= 2;
2527df8bae1dSRodney W. Grimes }
25285070c7f8SJohn Dyson 
2529c7c34a24SBruce Evans /* XXX. */
2530c7c34a24SBruce Evans #undef count
2531c7c34a24SBruce Evans 
2532c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */
25335070c7f8SJohn Dyson void
25341b40f8c0SMatthew Dillon vm_object_print(
25351b40f8c0SMatthew Dillon         /* db_expr_t */ long addr,
25361b40f8c0SMatthew Dillon 	boolean_t have_addr,
25371b40f8c0SMatthew Dillon 	/* db_expr_t */ long count,
25381b40f8c0SMatthew Dillon 	char *modif)
2539c7c34a24SBruce Evans {
2540c7c34a24SBruce Evans 	vm_object_print_static(addr, have_addr, count, modif);
2541c7c34a24SBruce Evans }
2542c7c34a24SBruce Evans 
2543c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
25445070c7f8SJohn Dyson {
25455070c7f8SJohn Dyson 	vm_object_t object;
2546bb2ac86fSKonstantin Belousov 	vm_pindex_t fidx;
2547bb2ac86fSKonstantin Belousov 	vm_paddr_t pa;
2548bb2ac86fSKonstantin Belousov 	vm_page_t m, prev_m;
2549bb2ac86fSKonstantin Belousov 	int rcount, nl, c;
2550cc64b484SAlfred Perlstein 
2551bb2ac86fSKonstantin Belousov 	nl = 0;
2552cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
2553fc62ef1fSBruce Evans 		db_printf("new object: %p\n", (void *)object);
25545070c7f8SJohn Dyson 		if (nl > 18) {
25555070c7f8SJohn Dyson 			c = cngetc();
25565070c7f8SJohn Dyson 			if (c != ' ')
25575070c7f8SJohn Dyson 				return;
25585070c7f8SJohn Dyson 			nl = 0;
25595070c7f8SJohn Dyson 		}
25605070c7f8SJohn Dyson 		nl++;
25615070c7f8SJohn Dyson 		rcount = 0;
25625070c7f8SJohn Dyson 		fidx = 0;
2563bb2ac86fSKonstantin Belousov 		pa = -1;
2564bb2ac86fSKonstantin Belousov 		TAILQ_FOREACH(m, &object->memq, listq) {
2565bb2ac86fSKonstantin Belousov 			if (m->pindex > 128)
2566bb2ac86fSKonstantin Belousov 				break;
2567bb2ac86fSKonstantin Belousov 			if ((prev_m = TAILQ_PREV(m, pglist, listq)) != NULL &&
2568bb2ac86fSKonstantin Belousov 			    prev_m->pindex + 1 != m->pindex) {
25695070c7f8SJohn Dyson 				if (rcount) {
25703efc015bSPeter Wemm 					db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
25713efc015bSPeter Wemm 						(long)fidx, rcount, (long)pa);
25725070c7f8SJohn Dyson 					if (nl > 18) {
25735070c7f8SJohn Dyson 						c = cngetc();
25745070c7f8SJohn Dyson 						if (c != ' ')
25755070c7f8SJohn Dyson 							return;
25765070c7f8SJohn Dyson 						nl = 0;
25775070c7f8SJohn Dyson 					}
25785070c7f8SJohn Dyson 					nl++;
25795070c7f8SJohn Dyson 					rcount = 0;
25805070c7f8SJohn Dyson 				}
25815070c7f8SJohn Dyson 			}
25825070c7f8SJohn Dyson 			if (rcount &&
25835070c7f8SJohn Dyson 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
25845070c7f8SJohn Dyson 				++rcount;
25855070c7f8SJohn Dyson 				continue;
25865070c7f8SJohn Dyson 			}
25875070c7f8SJohn Dyson 			if (rcount) {
25882446e4f0SAlan Cox 				db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
25893efc015bSPeter Wemm 					(long)fidx, rcount, (long)pa);
25905070c7f8SJohn Dyson 				if (nl > 18) {
25915070c7f8SJohn Dyson 					c = cngetc();
25925070c7f8SJohn Dyson 					if (c != ' ')
25935070c7f8SJohn Dyson 						return;
25945070c7f8SJohn Dyson 					nl = 0;
25955070c7f8SJohn Dyson 				}
25965070c7f8SJohn Dyson 				nl++;
25975070c7f8SJohn Dyson 			}
2598bb2ac86fSKonstantin Belousov 			fidx = m->pindex;
25995070c7f8SJohn Dyson 			pa = VM_PAGE_TO_PHYS(m);
26005070c7f8SJohn Dyson 			rcount = 1;
26015070c7f8SJohn Dyson 		}
26025070c7f8SJohn Dyson 		if (rcount) {
26033efc015bSPeter Wemm 			db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
26043efc015bSPeter Wemm 				(long)fidx, rcount, (long)pa);
26055070c7f8SJohn Dyson 			if (nl > 18) {
26065070c7f8SJohn Dyson 				c = cngetc();
26075070c7f8SJohn Dyson 				if (c != ' ')
26085070c7f8SJohn Dyson 					return;
26095070c7f8SJohn Dyson 				nl = 0;
26105070c7f8SJohn Dyson 			}
26115070c7f8SJohn Dyson 			nl++;
26125070c7f8SJohn Dyson 		}
26135070c7f8SJohn Dyson 	}
26145070c7f8SJohn Dyson }
2615c3cb3e12SDavid Greenman #endif /* DDB */
2616