xref: /freebsd/sys/vm/vm_object.c (revision 3f289c3fcf39b200550e2702068014cdd801d4da)
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>
74*3f289c3fSJeff 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 */
83fb919e4dSMark Murray #include <sys/socket.h>
843364c323SKonstantin Belousov #include <sys/resourcevar.h>
8589f6b863SAttilio Rao #include <sys/rwlock.h>
86ff87ae35SJohn Baldwin #include <sys/user.h>
87fb919e4dSMark Murray #include <sys/vnode.h>
88fb919e4dSMark Murray #include <sys/vmmeter.h>
891005a129SJohn Baldwin #include <sys/sx.h>
90df8bae1dSRodney W. Grimes 
91df8bae1dSRodney W. Grimes #include <vm/vm.h>
92efeaf95aSDavid Greenman #include <vm/vm_param.h>
93efeaf95aSDavid Greenman #include <vm/pmap.h>
94efeaf95aSDavid Greenman #include <vm/vm_map.h>
95efeaf95aSDavid Greenman #include <vm/vm_object.h>
96df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
9726f9a767SRodney W. Grimes #include <vm/vm_pageout.h>
980d94caffSDavid Greenman #include <vm/vm_pager.h>
9905f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h>
100a1f6d91cSDavid Greenman #include <vm/vm_kern.h>
101efeaf95aSDavid Greenman #include <vm/vm_extern.h>
102774d251dSAttilio Rao #include <vm/vm_radix.h>
103f8a47341SAlan Cox #include <vm/vm_reserv.h>
104670d17b5SJeff Roberson #include <vm/uma.h>
10526f9a767SRodney W. Grimes 
106c53f7aceSDag-Erling Smørgrav static int old_msync;
107c53f7aceSDag-Erling Smørgrav SYSCTL_INT(_vm, OID_AUTO, old_msync, CTLFLAG_RW, &old_msync, 0,
108c53f7aceSDag-Erling Smørgrav     "Use old (insecure) msync behavior");
109c53f7aceSDag-Erling Smørgrav 
110757216f3SKonstantin Belousov static int	vm_object_page_collect_flush(vm_object_t object, vm_page_t p,
111126d6082SKonstantin Belousov 		    int pagerflags, int flags, boolean_t *clearobjflags,
112126d6082SKonstantin Belousov 		    boolean_t *eio);
1133280870dSKonstantin Belousov static boolean_t vm_object_page_remove_write(vm_page_t p, int flags,
114126d6082SKonstantin Belousov 		    boolean_t *clearobjflags);
115b9b7a4beSMatthew Dillon static void	vm_object_qcollapse(vm_object_t object);
11602dd8331SAlan Cox static void	vm_object_vndeallocate(vm_object_t object);
117f6b04d2bSDavid Greenman 
118df8bae1dSRodney W. Grimes /*
119df8bae1dSRodney W. Grimes  *	Virtual memory objects maintain the actual data
120df8bae1dSRodney W. Grimes  *	associated with allocated virtual memory.  A given
121df8bae1dSRodney W. Grimes  *	page of memory exists within exactly one object.
122df8bae1dSRodney W. Grimes  *
123df8bae1dSRodney W. Grimes  *	An object is only deallocated when all "references"
124df8bae1dSRodney W. Grimes  *	are given up.  Only one "reference" to a given
125df8bae1dSRodney W. Grimes  *	region of an object should be writeable.
126df8bae1dSRodney W. Grimes  *
127df8bae1dSRodney W. Grimes  *	Associated with each object is a list of all resident
128df8bae1dSRodney W. Grimes  *	memory pages belonging to that object; this list is
129df8bae1dSRodney W. Grimes  *	maintained by the "vm_page" module, and locked by the object's
130df8bae1dSRodney W. Grimes  *	lock.
131df8bae1dSRodney W. Grimes  *
132df8bae1dSRodney W. Grimes  *	Each object also records a "pager" routine which is
133df8bae1dSRodney W. Grimes  *	used to retrieve (and store) pages to the proper backing
134df8bae1dSRodney W. Grimes  *	storage.  In addition, objects may be backed by other
135df8bae1dSRodney W. Grimes  *	objects from which they were virtual-copied.
136df8bae1dSRodney W. Grimes  *
137df8bae1dSRodney W. Grimes  *	The only items within the object structure which are
138df8bae1dSRodney W. Grimes  *	modified after time of creation are:
139df8bae1dSRodney W. Grimes  *		reference count		locked by object's lock
140df8bae1dSRodney W. Grimes  *		pager routine		locked by object's lock
141df8bae1dSRodney W. Grimes  *
142df8bae1dSRodney W. Grimes  */
143df8bae1dSRodney W. Grimes 
14428f8db14SBruce Evans struct object_q vm_object_list;
145a5698387SAlan Cox struct mtx vm_object_list_mtx;	/* lock for object list and count */
146cccf11b8SAlan Cox 
147cccf11b8SAlan Cox struct vm_object kernel_object_store;
148df8bae1dSRodney W. Grimes 
1496472ac3dSEd Schouten static SYSCTL_NODE(_vm_stats, OID_AUTO, object, CTLFLAG_RD, 0,
1506472ac3dSEd Schouten     "VM object stats");
151604c2bbcSAlan Cox 
15211542376SAlan Cox static counter_u64_t object_collapses = EARLY_COUNTER;
15311542376SAlan Cox SYSCTL_COUNTER_U64(_vm_stats_object, OID_AUTO, collapses, CTLFLAG_RD,
15411542376SAlan Cox     &object_collapses,
15511542376SAlan Cox     "VM object collapses");
156604c2bbcSAlan Cox 
15711542376SAlan Cox static counter_u64_t object_bypasses = EARLY_COUNTER;
15811542376SAlan Cox SYSCTL_COUNTER_U64(_vm_stats_object, OID_AUTO, bypasses, CTLFLAG_RD,
15911542376SAlan Cox     &object_bypasses,
16011542376SAlan Cox     "VM object bypasses");
16111542376SAlan Cox 
16211542376SAlan Cox static void
16311542376SAlan Cox counter_startup(void)
16411542376SAlan Cox {
16511542376SAlan Cox 
16611542376SAlan Cox 	object_collapses = counter_u64_alloc(M_WAITOK);
16711542376SAlan Cox 	object_bypasses = counter_u64_alloc(M_WAITOK);
16811542376SAlan Cox }
16911542376SAlan Cox SYSINIT(object_counters, SI_SUB_CPU, SI_ORDER_ANY, counter_startup, NULL);
170dad740e9SAlan Cox 
171670d17b5SJeff Roberson static uma_zone_t obj_zone;
1728355f576SJeff Roberson 
173b23f72e9SBrian Feldman static int vm_object_zinit(void *mem, int size, int flags);
1748355f576SJeff Roberson 
1758355f576SJeff Roberson #ifdef INVARIANTS
1768355f576SJeff Roberson static void vm_object_zdtor(void *mem, int size, void *arg);
1778355f576SJeff Roberson 
1788355f576SJeff Roberson static void
1798355f576SJeff Roberson vm_object_zdtor(void *mem, int size, void *arg)
1808355f576SJeff Roberson {
1818355f576SJeff Roberson 	vm_object_t object;
1828355f576SJeff Roberson 
1838355f576SJeff Roberson 	object = (vm_object_t)mem;
184e735691bSJohn Baldwin 	KASSERT(object->ref_count == 0,
185e735691bSJohn Baldwin 	    ("object %p ref_count = %d", object, object->ref_count));
18643186e53SAlan Cox 	KASSERT(TAILQ_EMPTY(&object->memq),
187198da1b2SAttilio Rao 	    ("object %p has resident pages in its memq", object));
188774d251dSAttilio Rao 	KASSERT(vm_radix_is_empty(&object->rtree),
189774d251dSAttilio Rao 	    ("object %p has resident pages in its trie", object));
190f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
191f8a47341SAlan Cox 	KASSERT(LIST_EMPTY(&object->rvq),
192f8a47341SAlan Cox 	    ("object %p has reservations",
193f8a47341SAlan Cox 	    object));
194f8a47341SAlan Cox #endif
1958355f576SJeff Roberson 	KASSERT(object->paging_in_progress == 0,
1968355f576SJeff Roberson 	    ("object %p paging_in_progress = %d",
1978355f576SJeff Roberson 	    object, object->paging_in_progress));
1988355f576SJeff Roberson 	KASSERT(object->resident_page_count == 0,
1998355f576SJeff Roberson 	    ("object %p resident_page_count = %d",
2008355f576SJeff Roberson 	    object, object->resident_page_count));
2018355f576SJeff Roberson 	KASSERT(object->shadow_count == 0,
2028355f576SJeff Roberson 	    ("object %p shadow_count = %d",
2038355f576SJeff Roberson 	    object, object->shadow_count));
204e735691bSJohn Baldwin 	KASSERT(object->type == OBJT_DEAD,
205e735691bSJohn Baldwin 	    ("object %p has non-dead type %d",
206e735691bSJohn Baldwin 	    object, object->type));
2078355f576SJeff Roberson }
2088355f576SJeff Roberson #endif
2098355f576SJeff Roberson 
210b23f72e9SBrian Feldman static int
211b23f72e9SBrian Feldman vm_object_zinit(void *mem, int size, int flags)
2128355f576SJeff Roberson {
2138355f576SJeff Roberson 	vm_object_t object;
2148355f576SJeff Roberson 
2158355f576SJeff Roberson 	object = (vm_object_t)mem;
216777a36c5SAlan Cox 	rw_init_flags(&object->lock, "vm object", RW_DUPOK | RW_NEW);
2178355f576SJeff Roberson 
2188355f576SJeff Roberson 	/* These are true for any object that has been freed */
219e735691bSJohn Baldwin 	object->type = OBJT_DEAD;
220e735691bSJohn Baldwin 	object->ref_count = 0;
221cd1241fbSKonstantin Belousov 	vm_radix_init(&object->rtree);
2228355f576SJeff Roberson 	object->paging_in_progress = 0;
2238355f576SJeff Roberson 	object->resident_page_count = 0;
2248355f576SJeff Roberson 	object->shadow_count = 0;
225f425ab8eSKonstantin Belousov 	object->flags = OBJ_DEAD;
226e735691bSJohn Baldwin 
227e735691bSJohn Baldwin 	mtx_lock(&vm_object_list_mtx);
228e735691bSJohn Baldwin 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
229e735691bSJohn Baldwin 	mtx_unlock(&vm_object_list_mtx);
230b23f72e9SBrian Feldman 	return (0);
2318355f576SJeff Roberson }
232df8bae1dSRodney W. Grimes 
233a4915c21SAttilio Rao static void
2346395da54SIan Dowse _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
235df8bae1dSRodney W. Grimes {
2360cddd8f0SMatthew Dillon 
237df8bae1dSRodney W. Grimes 	TAILQ_INIT(&object->memq);
2381c500307SAlan Cox 	LIST_INIT(&object->shadow_head);
239a1f6d91cSDavid Greenman 
24024a1cce3SDavid Greenman 	object->type = type;
241f425ab8eSKonstantin Belousov 	if (type == OBJT_SWAP)
242f425ab8eSKonstantin Belousov 		pctrie_init(&object->un_pager.swp.swp_blks);
243f425ab8eSKonstantin Belousov 
244f425ab8eSKonstantin Belousov 	/*
245f425ab8eSKonstantin Belousov 	 * Ensure that swap_pager_swapoff() iteration over object_list
246f425ab8eSKonstantin Belousov 	 * sees up to date type and pctrie head if it observed
247f425ab8eSKonstantin Belousov 	 * non-dead object.
248f425ab8eSKonstantin Belousov 	 */
249f425ab8eSKonstantin Belousov 	atomic_thread_fence_rel();
250f425ab8eSKonstantin Belousov 
25128634820SAlan Cox 	switch (type) {
25228634820SAlan Cox 	case OBJT_DEAD:
25328634820SAlan Cox 		panic("_vm_object_allocate: can't create OBJT_DEAD");
25428634820SAlan Cox 	case OBJT_DEFAULT:
25528634820SAlan Cox 	case OBJT_SWAP:
25628634820SAlan Cox 		object->flags = OBJ_ONEMAPPING;
25728634820SAlan Cox 		break;
25828634820SAlan Cox 	case OBJT_DEVICE:
25928634820SAlan Cox 	case OBJT_SG:
26028634820SAlan Cox 		object->flags = OBJ_FICTITIOUS | OBJ_UNMANAGED;
26128634820SAlan Cox 		break;
26228634820SAlan Cox 	case OBJT_MGTDEVICE:
26328634820SAlan Cox 		object->flags = OBJ_FICTITIOUS;
26428634820SAlan Cox 		break;
26528634820SAlan Cox 	case OBJT_PHYS:
26628634820SAlan Cox 		object->flags = OBJ_UNMANAGED;
26728634820SAlan Cox 		break;
26828634820SAlan Cox 	case OBJT_VNODE:
26928634820SAlan Cox 		object->flags = 0;
27028634820SAlan Cox 		break;
27128634820SAlan Cox 	default:
27228634820SAlan Cox 		panic("_vm_object_allocate: type %d is undefined", type);
27328634820SAlan Cox 	}
274df8bae1dSRodney W. Grimes 	object->size = size;
275b881da26SAlan Cox 	object->generation = 1;
276a1f6d91cSDavid Greenman 	object->ref_count = 1;
2773153e878SAlan Cox 	object->memattr = VM_MEMATTR_DEFAULT;
278ef694c1aSEdward Tomasz Napierala 	object->cred = NULL;
2793364c323SKonstantin Belousov 	object->charge = 0;
28024a1cce3SDavid Greenman 	object->handle = NULL;
28124a1cce3SDavid Greenman 	object->backing_object = NULL;
282a316d390SJohn Dyson 	object->backing_object_offset = (vm_ooffset_t) 0;
283f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
284f8a47341SAlan Cox 	LIST_INIT(&object->rvq);
285f8a47341SAlan Cox #endif
2861bdbd705SKonstantin Belousov 	umtx_shm_object_init(object);
287df8bae1dSRodney W. Grimes }
288df8bae1dSRodney W. Grimes 
289df8bae1dSRodney W. Grimes /*
29026f9a767SRodney W. Grimes  *	vm_object_init:
29126f9a767SRodney W. Grimes  *
29226f9a767SRodney W. Grimes  *	Initialize the VM objects module.
29326f9a767SRodney W. Grimes  */
29426f9a767SRodney W. Grimes void
2951b40f8c0SMatthew Dillon vm_object_init(void)
29626f9a767SRodney W. Grimes {
29726f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_list);
2986008862bSJohn Baldwin 	mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF);
2990217125fSDavid Greenman 
30089f6b863SAttilio Rao 	rw_init(&kernel_object->lock, "kernel vm object");
301d1780e8dSKonstantin Belousov 	_vm_object_allocate(OBJT_PHYS, atop(VM_MAX_KERNEL_ADDRESS -
302d1780e8dSKonstantin Belousov 	    VM_MIN_KERNEL_ADDRESS), kernel_object);
303f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
304f8a47341SAlan Cox 	kernel_object->flags |= OBJ_COLORED;
305f8a47341SAlan Cox 	kernel_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS);
306f8a47341SAlan Cox #endif
30726f9a767SRodney W. Grimes 
3088dbca793STor Egge 	/*
3098dbca793STor Egge 	 * The lock portion of struct vm_object must be type stable due
3108dbca793STor Egge 	 * to vm_pageout_fallback_object_lock locking a vm object
3118dbca793STor Egge 	 * without holding any references to it.
3128dbca793STor Egge 	 */
3138355f576SJeff Roberson 	obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL,
3148355f576SJeff Roberson #ifdef INVARIANTS
3158355f576SJeff Roberson 	    vm_object_zdtor,
3168355f576SJeff Roberson #else
3178355f576SJeff Roberson 	    NULL,
3188355f576SJeff Roberson #endif
3195df87b21SJeff Roberson 	    vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
320774d251dSAttilio Rao 
321cd1241fbSKonstantin Belousov 	vm_radix_zinit();
32299448ed1SJohn Dyson }
32399448ed1SJohn Dyson 
32499448ed1SJohn Dyson void
3251b40f8c0SMatthew Dillon vm_object_clear_flag(vm_object_t object, u_short bits)
3261b40f8c0SMatthew Dillon {
3275440b5a9SAlan Cox 
32889f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
329b06805adSJake Burkholder 	object->flags &= ~bits;
3301b40f8c0SMatthew Dillon }
3311b40f8c0SMatthew Dillon 
3323153e878SAlan Cox /*
3333153e878SAlan Cox  *	Sets the default memory attribute for the specified object.  Pages
3343153e878SAlan Cox  *	that are allocated to this object are by default assigned this memory
3353153e878SAlan Cox  *	attribute.
3363153e878SAlan Cox  *
3373153e878SAlan Cox  *	Presently, this function must be called before any pages are allocated
3383153e878SAlan Cox  *	to the object.  In the future, this requirement may be relaxed for
3393153e878SAlan Cox  *	"default" and "swap" objects.
3403153e878SAlan Cox  */
3413153e878SAlan Cox int
3423153e878SAlan Cox vm_object_set_memattr(vm_object_t object, vm_memattr_t memattr)
3433153e878SAlan Cox {
3443153e878SAlan Cox 
34589f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
3463153e878SAlan Cox 	switch (object->type) {
3473153e878SAlan Cox 	case OBJT_DEFAULT:
3483153e878SAlan Cox 	case OBJT_DEVICE:
34996b0b92aSAlan Cox 	case OBJT_MGTDEVICE:
3503153e878SAlan Cox 	case OBJT_PHYS:
35101381811SJohn Baldwin 	case OBJT_SG:
3523153e878SAlan Cox 	case OBJT_SWAP:
3533153e878SAlan Cox 	case OBJT_VNODE:
3543153e878SAlan Cox 		if (!TAILQ_EMPTY(&object->memq))
3553153e878SAlan Cox 			return (KERN_FAILURE);
3563153e878SAlan Cox 		break;
3573153e878SAlan Cox 	case OBJT_DEAD:
3583153e878SAlan Cox 		return (KERN_INVALID_ARGUMENT);
35996b0b92aSAlan Cox 	default:
36096b0b92aSAlan Cox 		panic("vm_object_set_memattr: object %p is of undefined type",
36196b0b92aSAlan Cox 		    object);
3623153e878SAlan Cox 	}
3633153e878SAlan Cox 	object->memattr = memattr;
3643153e878SAlan Cox 	return (KERN_SUCCESS);
3653153e878SAlan Cox }
3663153e878SAlan Cox 
3671b40f8c0SMatthew Dillon void
3681b40f8c0SMatthew Dillon vm_object_pip_add(vm_object_t object, short i)
3691b40f8c0SMatthew Dillon {
370f279b88dSAlan Cox 
37189f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
372b06805adSJake Burkholder 	object->paging_in_progress += i;
3731b40f8c0SMatthew Dillon }
3741b40f8c0SMatthew Dillon 
3751b40f8c0SMatthew Dillon void
3761b40f8c0SMatthew Dillon vm_object_pip_subtract(vm_object_t object, short i)
3771b40f8c0SMatthew Dillon {
378d647a0edSAlan Cox 
37989f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
380b06805adSJake Burkholder 	object->paging_in_progress -= i;
3811b40f8c0SMatthew Dillon }
3821b40f8c0SMatthew Dillon 
3831b40f8c0SMatthew Dillon void
3841b40f8c0SMatthew Dillon vm_object_pip_wakeup(vm_object_t object)
3851b40f8c0SMatthew Dillon {
386f279b88dSAlan Cox 
38789f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
388b06805adSJake Burkholder 	object->paging_in_progress--;
3891b40f8c0SMatthew Dillon 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
3901b40f8c0SMatthew Dillon 		vm_object_clear_flag(object, OBJ_PIPWNT);
3911b40f8c0SMatthew Dillon 		wakeup(object);
3921b40f8c0SMatthew Dillon 	}
3931b40f8c0SMatthew Dillon }
3941b40f8c0SMatthew Dillon 
3951b40f8c0SMatthew Dillon void
3961b40f8c0SMatthew Dillon vm_object_pip_wakeupn(vm_object_t object, short i)
3971b40f8c0SMatthew Dillon {
398d647a0edSAlan Cox 
39989f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
4001b40f8c0SMatthew Dillon 	if (i)
401b06805adSJake Burkholder 		object->paging_in_progress -= i;
4021b40f8c0SMatthew Dillon 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
4031b40f8c0SMatthew Dillon 		vm_object_clear_flag(object, OBJ_PIPWNT);
4041b40f8c0SMatthew Dillon 		wakeup(object);
4051b40f8c0SMatthew Dillon 	}
4061b40f8c0SMatthew Dillon }
4071b40f8c0SMatthew Dillon 
4081b40f8c0SMatthew Dillon void
4091b40f8c0SMatthew Dillon vm_object_pip_wait(vm_object_t object, char *waitid)
4101b40f8c0SMatthew Dillon {
4111ca58953SAlan Cox 
41289f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
4131ca58953SAlan Cox 	while (object->paging_in_progress) {
4141ca58953SAlan Cox 		object->flags |= OBJ_PIPWNT;
4150dde287bSAttilio Rao 		VM_OBJECT_SLEEP(object, object, PVM, waitid, 0);
4161ca58953SAlan Cox 	}
4171b40f8c0SMatthew Dillon }
4181b40f8c0SMatthew Dillon 
41926f9a767SRodney W. Grimes /*
42026f9a767SRodney W. Grimes  *	vm_object_allocate:
42126f9a767SRodney W. Grimes  *
42226f9a767SRodney W. Grimes  *	Returns a new object with the given size.
42326f9a767SRodney W. Grimes  */
42426f9a767SRodney W. Grimes vm_object_t
4256395da54SIan Dowse vm_object_allocate(objtype_t type, vm_pindex_t size)
42626f9a767SRodney W. Grimes {
42790688d13SAlan Cox 	vm_object_t object;
42890688d13SAlan Cox 
42990688d13SAlan Cox 	object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK);
43090688d13SAlan Cox 	_vm_object_allocate(type, size, object);
43190688d13SAlan Cox 	return (object);
43226f9a767SRodney W. Grimes }
43326f9a767SRodney W. Grimes 
43426f9a767SRodney W. Grimes 
43526f9a767SRodney W. Grimes /*
436df8bae1dSRodney W. Grimes  *	vm_object_reference:
437df8bae1dSRodney W. Grimes  *
43815347817SAlan Cox  *	Gets another reference to the given object.  Note: OBJ_DEAD
43915347817SAlan Cox  *	objects can be referenced during final cleaning.
440df8bae1dSRodney W. Grimes  */
4416476c0d2SJohn Dyson void
4421b40f8c0SMatthew Dillon vm_object_reference(vm_object_t object)
443df8bae1dSRodney W. Grimes {
444df8bae1dSRodney W. Grimes 	if (object == NULL)
445df8bae1dSRodney W. Grimes 		return;
44689f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
44752481a9aSJeff Roberson 	vm_object_reference_locked(object);
44889f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
44995e5e988SJohn Dyson }
45095e5e988SJohn Dyson 
45123955314SAlfred Perlstein /*
452b921a12bSAlan Cox  *	vm_object_reference_locked:
453b921a12bSAlan Cox  *
454b921a12bSAlan Cox  *	Gets another reference to the given object.
455b921a12bSAlan Cox  *
456b921a12bSAlan Cox  *	The object must be locked.
457b921a12bSAlan Cox  */
458b921a12bSAlan Cox void
459b921a12bSAlan Cox vm_object_reference_locked(vm_object_t object)
460b921a12bSAlan Cox {
461b921a12bSAlan Cox 	struct vnode *vp;
462b921a12bSAlan Cox 
46389f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
464b921a12bSAlan Cox 	object->ref_count++;
465b921a12bSAlan Cox 	if (object->type == OBJT_VNODE) {
466b921a12bSAlan Cox 		vp = object->handle;
467b921a12bSAlan Cox 		vref(vp);
468b921a12bSAlan Cox 	}
469b921a12bSAlan Cox }
470b921a12bSAlan Cox 
471b921a12bSAlan Cox /*
4729d5abbddSJens Schweikhardt  * Handle deallocating an object of type OBJT_VNODE.
47323955314SAlfred Perlstein  */
47402dd8331SAlan Cox static void
4751b40f8c0SMatthew Dillon vm_object_vndeallocate(vm_object_t object)
47695e5e988SJohn Dyson {
47795e5e988SJohn Dyson 	struct vnode *vp = (struct vnode *) object->handle;
478219cbf59SEivind Eklund 
47989f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
4805526d2d9SEivind Eklund 	KASSERT(object->type == OBJT_VNODE,
4815526d2d9SEivind Eklund 	    ("vm_object_vndeallocate: not a vnode object"));
482219cbf59SEivind Eklund 	KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
483219cbf59SEivind Eklund #ifdef INVARIANTS
48495e5e988SJohn Dyson 	if (object->ref_count == 0) {
485411455a8SEdward Tomasz Napierala 		vn_printf(vp, "vm_object_vndeallocate ");
48695e5e988SJohn Dyson 		panic("vm_object_vndeallocate: bad object reference count");
48795e5e988SJohn Dyson 	}
48895e5e988SJohn Dyson #endif
48995e5e988SJohn Dyson 
4902a339d9eSKonstantin Belousov 	if (!umtx_shm_vnobj_persistent && object->ref_count == 1)
4911bdbd705SKonstantin Belousov 		umtx_shm_object_terminated(object);
4921bdbd705SKonstantin Belousov 
49330d57414SKonstantin Belousov 	/*
49430d57414SKonstantin Belousov 	 * The test for text of vp vnode does not need a bypass to
49530d57414SKonstantin Belousov 	 * reach right VV_TEXT there, since it is obtained from
49630d57414SKonstantin Belousov 	 * object->handle.
49730d57414SKonstantin Belousov 	 */
49830d57414SKonstantin Belousov 	if (object->ref_count > 1 || (vp->v_vflag & VV_TEXT) == 0) {
49995e5e988SJohn Dyson 		object->ref_count--;
50089f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
50103fa5b34SKonstantin Belousov 		/* vrele may need the vnode lock. */
50247221757SJohn Dyson 		vrele(vp);
50303fa5b34SKonstantin Belousov 	} else {
50486769ac0SKonstantin Belousov 		vhold(vp);
50589f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
50603fa5b34SKonstantin Belousov 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
50786769ac0SKonstantin Belousov 		vdrop(vp);
50889f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
50903fa5b34SKonstantin Belousov 		object->ref_count--;
51086769ac0SKonstantin Belousov 		if (object->type == OBJT_DEAD) {
51189f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
51286769ac0SKonstantin Belousov 			VOP_UNLOCK(vp, 0);
51386769ac0SKonstantin Belousov 		} else {
51403fa5b34SKonstantin Belousov 			if (object->ref_count == 0)
515877d24acSKonstantin Belousov 				VOP_UNSET_TEXT(vp);
51689f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
51703fa5b34SKonstantin Belousov 			vput(vp);
51803fa5b34SKonstantin Belousov 		}
519df8bae1dSRodney W. Grimes 	}
52086769ac0SKonstantin Belousov }
521df8bae1dSRodney W. Grimes 
522df8bae1dSRodney W. Grimes /*
523df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
524df8bae1dSRodney W. Grimes  *
525df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
526df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
527df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
528df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
529df8bae1dSRodney W. Grimes  *	may be relinquished.
530df8bae1dSRodney W. Grimes  *
531df8bae1dSRodney W. Grimes  *	No object may be locked.
532df8bae1dSRodney W. Grimes  */
53326f9a767SRodney W. Grimes void
5341b40f8c0SMatthew Dillon vm_object_deallocate(vm_object_t object)
535df8bae1dSRodney W. Grimes {
536df8bae1dSRodney W. Grimes 	vm_object_t temp;
5376f2af3fcSKonstantin Belousov 	struct vnode *vp;
538df8bae1dSRodney W. Grimes 
539df8bae1dSRodney W. Grimes 	while (object != NULL) {
54089f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
5413b68228cSAlan Cox 		if (object->type == OBJT_VNODE) {
54295e5e988SJohn Dyson 			vm_object_vndeallocate(object);
54323b186d3SAlan Cox 			return;
5445050aa86SKonstantin Belousov 		}
54595e5e988SJohn Dyson 
5468125b1e6SAlfred Perlstein 		KASSERT(object->ref_count != 0,
5478125b1e6SAlfred Perlstein 			("vm_object_deallocate: object deallocated too many times: %d", object->type));
5482be70f79SJohn Dyson 
5492be70f79SJohn Dyson 		/*
5508125b1e6SAlfred Perlstein 		 * If the reference count goes to 0 we start calling
5518125b1e6SAlfred Perlstein 		 * vm_object_terminate() on the object chain.
5528125b1e6SAlfred Perlstein 		 * A ref count of 1 may be a special case depending on the
5538125b1e6SAlfred Perlstein 		 * shadow count being 0 or 1.
5542be70f79SJohn Dyson 		 */
555c0877f10SJohn Dyson 		object->ref_count--;
5568125b1e6SAlfred Perlstein 		if (object->ref_count > 1) {
55789f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
55823b186d3SAlan Cox 			return;
5598125b1e6SAlfred Perlstein 		} else if (object->ref_count == 1) {
5606f2af3fcSKonstantin Belousov 			if (object->type == OBJT_SWAP &&
5616f2af3fcSKonstantin Belousov 			    (object->flags & OBJ_TMPFS) != 0) {
5626f2af3fcSKonstantin Belousov 				vp = object->un_pager.swp.swp_tmpfs;
5636f2af3fcSKonstantin Belousov 				vhold(vp);
5646f2af3fcSKonstantin Belousov 				VM_OBJECT_WUNLOCK(object);
5656f2af3fcSKonstantin Belousov 				vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
5666f2af3fcSKonstantin Belousov 				VM_OBJECT_WLOCK(object);
5677560005cSKonstantin Belousov 				if (object->type == OBJT_DEAD ||
5687560005cSKonstantin Belousov 				    object->ref_count != 1) {
5696f2af3fcSKonstantin Belousov 					VM_OBJECT_WUNLOCK(object);
5706f2af3fcSKonstantin Belousov 					VOP_UNLOCK(vp, 0);
5712309fa9bSKonstantin Belousov 					vdrop(vp);
5726f2af3fcSKonstantin Belousov 					return;
5737560005cSKonstantin Belousov 				}
5747560005cSKonstantin Belousov 				if ((object->flags & OBJ_TMPFS) != 0)
5756f2af3fcSKonstantin Belousov 					VOP_UNSET_TEXT(vp);
5766f2af3fcSKonstantin Belousov 				VOP_UNLOCK(vp, 0);
5772309fa9bSKonstantin Belousov 				vdrop(vp);
5786f2af3fcSKonstantin Belousov 			}
5794c8e0452SAlan Cox 			if (object->shadow_count == 0 &&
5804c8e0452SAlan Cox 			    object->handle == NULL &&
5814c8e0452SAlan Cox 			    (object->type == OBJT_DEFAULT ||
5826f2af3fcSKonstantin Belousov 			    (object->type == OBJT_SWAP &&
583f08f7dcaSKonstantin Belousov 			    (object->flags & OBJ_TMPFS_NODE) == 0))) {
5848125b1e6SAlfred Perlstein 				vm_object_set_flag(object, OBJ_ONEMAPPING);
5858125b1e6SAlfred Perlstein 			} else if ((object->shadow_count == 1) &&
5868125b1e6SAlfred Perlstein 			    (object->handle == NULL) &&
58724a1cce3SDavid Greenman 			    (object->type == OBJT_DEFAULT ||
58824a1cce3SDavid Greenman 			     object->type == OBJT_SWAP)) {
589a1f6d91cSDavid Greenman 				vm_object_t robject;
59095e5e988SJohn Dyson 
5911c500307SAlan Cox 				robject = LIST_FIRST(&object->shadow_head);
5925526d2d9SEivind Eklund 				KASSERT(robject != NULL,
593219cbf59SEivind Eklund 				    ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
5945526d2d9SEivind Eklund 					 object->ref_count,
5955526d2d9SEivind Eklund 					 object->shadow_count));
5964bace8e7SKonstantin Belousov 				KASSERT((robject->flags & OBJ_TMPFS_NODE) == 0,
5974bace8e7SKonstantin Belousov 				    ("shadowed tmpfs v_object %p", object));
59889f6b863SAttilio Rao 				if (!VM_OBJECT_TRYWLOCK(robject)) {
599b72b0115SAlan Cox 					/*
600b72b0115SAlan Cox 					 * Avoid a potential deadlock.
601b72b0115SAlan Cox 					 */
602b72b0115SAlan Cox 					object->ref_count++;
60389f6b863SAttilio Rao 					VM_OBJECT_WUNLOCK(object);
604a7d86121SAlan Cox 					/*
605a7d86121SAlan Cox 					 * More likely than not the thread
606a7d86121SAlan Cox 					 * holding robject's lock has lower
607a7d86121SAlan Cox 					 * priority than the current thread.
608a7d86121SAlan Cox 					 * Let the lower priority thread run.
609a7d86121SAlan Cox 					 */
6108db5fc58SJohn Baldwin 					pause("vmo_de", 1);
611b72b0115SAlan Cox 					continue;
612b72b0115SAlan Cox 				}
613d936694fSAlan Cox 				/*
614d936694fSAlan Cox 				 * Collapse object into its shadow unless its
615d936694fSAlan Cox 				 * shadow is dead.  In that case, object will
616d936694fSAlan Cox 				 * be deallocated by the thread that is
617d936694fSAlan Cox 				 * deallocating its shadow.
618d936694fSAlan Cox 				 */
619d936694fSAlan Cox 				if ((robject->flags & OBJ_DEAD) == 0 &&
620d936694fSAlan Cox 				    (robject->handle == NULL) &&
62124a1cce3SDavid Greenman 				    (robject->type == OBJT_DEFAULT ||
62224a1cce3SDavid Greenman 				     robject->type == OBJT_SWAP)) {
623a1f6d91cSDavid Greenman 
62495e5e988SJohn Dyson 					robject->ref_count++;
625138449dcSAlan Cox retry:
626138449dcSAlan Cox 					if (robject->paging_in_progress) {
62789f6b863SAttilio Rao 						VM_OBJECT_WUNLOCK(object);
628138449dcSAlan Cox 						vm_object_pip_wait(robject,
629138449dcSAlan Cox 						    "objde1");
6302e9f4a69SAlan Cox 						temp = robject->backing_object;
6312e9f4a69SAlan Cox 						if (object == temp) {
63289f6b863SAttilio Rao 							VM_OBJECT_WLOCK(object);
633138449dcSAlan Cox 							goto retry;
6342e9f4a69SAlan Cox 						}
635138449dcSAlan Cox 					} else if (object->paging_in_progress) {
63689f6b863SAttilio Rao 						VM_OBJECT_WUNLOCK(robject);
637138449dcSAlan Cox 						object->flags |= OBJ_PIPWNT;
6380dde287bSAttilio Rao 						VM_OBJECT_SLEEP(object, object,
639138449dcSAlan Cox 						    PDROP | PVM, "objde2", 0);
64089f6b863SAttilio Rao 						VM_OBJECT_WLOCK(robject);
6412e9f4a69SAlan Cox 						temp = robject->backing_object;
6422e9f4a69SAlan Cox 						if (object == temp) {
64389f6b863SAttilio Rao 							VM_OBJECT_WLOCK(object);
644138449dcSAlan Cox 							goto retry;
645a1f6d91cSDavid Greenman 						}
6462e9f4a69SAlan Cox 					} else
64789f6b863SAttilio Rao 						VM_OBJECT_WUNLOCK(object);
6482e9f4a69SAlan Cox 
64995e5e988SJohn Dyson 					if (robject->ref_count == 1) {
65095e5e988SJohn Dyson 						robject->ref_count--;
651ba8da839SDavid Greenman 						object = robject;
65295e5e988SJohn Dyson 						goto doterm;
65395e5e988SJohn Dyson 					}
65495e5e988SJohn Dyson 					object = robject;
65595e5e988SJohn Dyson 					vm_object_collapse(object);
65689f6b863SAttilio Rao 					VM_OBJECT_WUNLOCK(object);
657ba8da839SDavid Greenman 					continue;
658a1f6d91cSDavid Greenman 				}
65989f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(robject);
66095e5e988SJohn Dyson 			}
66189f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
66223b186d3SAlan Cox 			return;
66395e5e988SJohn Dyson 		}
66495e5e988SJohn Dyson doterm:
6651bdbd705SKonstantin Belousov 		umtx_shm_object_terminated(object);
66624a1cce3SDavid Greenman 		temp = object->backing_object;
667c9917419SAlan Cox 		if (temp != NULL) {
6684bace8e7SKonstantin Belousov 			KASSERT((object->flags & OBJ_TMPFS_NODE) == 0,
6694bace8e7SKonstantin Belousov 			    ("shadowed tmpfs v_object 2 %p", object));
67089f6b863SAttilio Rao 			VM_OBJECT_WLOCK(temp);
6711c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
67295e5e988SJohn Dyson 			temp->shadow_count--;
67389f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(temp);
67495461b45SJohn Dyson 			object->backing_object = NULL;
675de5f6a77SJohn Dyson 		}
676245df27cSMatthew Dillon 		/*
677245df27cSMatthew Dillon 		 * Don't double-terminate, we could be in a termination
678245df27cSMatthew Dillon 		 * recursion due to the terminate having to sync data
679245df27cSMatthew Dillon 		 * to disk.
680245df27cSMatthew Dillon 		 */
681245df27cSMatthew Dillon 		if ((object->flags & OBJ_DEAD) == 0)
682df8bae1dSRodney W. Grimes 			vm_object_terminate(object);
683c829b9d0SAlan Cox 		else
68489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
685df8bae1dSRodney W. Grimes 		object = temp;
686df8bae1dSRodney W. Grimes 	}
687df8bae1dSRodney W. Grimes }
688df8bae1dSRodney W. Grimes 
689df8bae1dSRodney W. Grimes /*
6902ac78f0eSStephan Uphoff  *	vm_object_destroy removes the object from the global object list
6912ac78f0eSStephan Uphoff  *      and frees the space for the object.
6922ac78f0eSStephan Uphoff  */
6932ac78f0eSStephan Uphoff void
6942ac78f0eSStephan Uphoff vm_object_destroy(vm_object_t object)
6952ac78f0eSStephan Uphoff {
6962ac78f0eSStephan Uphoff 
6972ac78f0eSStephan Uphoff 	/*
6983364c323SKonstantin Belousov 	 * Release the allocation charge.
6993364c323SKonstantin Belousov 	 */
700ef694c1aSEdward Tomasz Napierala 	if (object->cred != NULL) {
701ef694c1aSEdward Tomasz Napierala 		swap_release_by_cred(object->charge, object->cred);
7023364c323SKonstantin Belousov 		object->charge = 0;
703ef694c1aSEdward Tomasz Napierala 		crfree(object->cred);
704ef694c1aSEdward Tomasz Napierala 		object->cred = NULL;
7053364c323SKonstantin Belousov 	}
7063364c323SKonstantin Belousov 
7073364c323SKonstantin Belousov 	/*
7082ac78f0eSStephan Uphoff 	 * Free the space for the object.
7092ac78f0eSStephan Uphoff 	 */
7102ac78f0eSStephan Uphoff 	uma_zfree(obj_zone, object);
7112ac78f0eSStephan Uphoff }
7122ac78f0eSStephan Uphoff 
7132ac78f0eSStephan Uphoff /*
7147bbdb843SRuslan Bukin  *	vm_object_terminate_pages removes any remaining pageable pages
7157bbdb843SRuslan Bukin  *	from the object and resets the object to an empty state.
7167bbdb843SRuslan Bukin  */
7177bbdb843SRuslan Bukin static void
7187bbdb843SRuslan Bukin vm_object_terminate_pages(vm_object_t object)
7197bbdb843SRuslan Bukin {
7207bbdb843SRuslan Bukin 	vm_page_t p, p_next;
7212fcd1ff6SKonstantin Belousov 	struct mtx *mtx, *mtx1;
7222fcd1ff6SKonstantin Belousov 	struct vm_pagequeue *pq, *pq1;
7234074d642SAlan Cox 	int dequeued;
7247bbdb843SRuslan Bukin 
7257bbdb843SRuslan Bukin 	VM_OBJECT_ASSERT_WLOCKED(object);
7267bbdb843SRuslan Bukin 
7272fcd1ff6SKonstantin Belousov 	mtx = NULL;
7282fcd1ff6SKonstantin Belousov 	pq = NULL;
7292fcd1ff6SKonstantin Belousov 
7307bbdb843SRuslan Bukin 	/*
7317bbdb843SRuslan Bukin 	 * Free any remaining pageable pages.  This also removes them from the
7327bbdb843SRuslan Bukin 	 * paging queues.  However, don't free wired pages, just remove them
7337bbdb843SRuslan Bukin 	 * from the object.  Rather than incrementally removing each page from
7347bbdb843SRuslan Bukin 	 * the object, the page and object are reset to any empty state.
7357bbdb843SRuslan Bukin 	 */
7367bbdb843SRuslan Bukin 	TAILQ_FOREACH_SAFE(p, &object->memq, listq, p_next) {
7377bbdb843SRuslan Bukin 		vm_page_assert_unbusied(p);
7382fcd1ff6SKonstantin Belousov 		if ((object->flags & OBJ_UNMANAGED) == 0) {
7397bbdb843SRuslan Bukin 			/*
7402fcd1ff6SKonstantin Belousov 			 * vm_page_free_prep() only needs the page
7412fcd1ff6SKonstantin Belousov 			 * lock for managed pages.
7427bbdb843SRuslan Bukin 			 */
7432fcd1ff6SKonstantin Belousov 			mtx1 = vm_page_lockptr(p);
7442fcd1ff6SKonstantin Belousov 			if (mtx1 != mtx) {
7452fcd1ff6SKonstantin Belousov 				if (mtx != NULL)
7462fcd1ff6SKonstantin Belousov 					mtx_unlock(mtx);
7472fcd1ff6SKonstantin Belousov 				if (pq != NULL) {
7484074d642SAlan Cox 					vm_pagequeue_cnt_add(pq, dequeued);
7492fcd1ff6SKonstantin Belousov 					vm_pagequeue_unlock(pq);
7502fcd1ff6SKonstantin Belousov 					pq = NULL;
7512fcd1ff6SKonstantin Belousov 				}
7522fcd1ff6SKonstantin Belousov 				mtx = mtx1;
7532fcd1ff6SKonstantin Belousov 				mtx_lock(mtx);
7542fcd1ff6SKonstantin Belousov 			}
7552fcd1ff6SKonstantin Belousov 		}
7567bbdb843SRuslan Bukin 		p->object = NULL;
7572fcd1ff6SKonstantin Belousov 		if (p->wire_count != 0)
7582fcd1ff6SKonstantin Belousov 			goto unlist;
7597bbdb843SRuslan Bukin 		VM_CNT_INC(v_pfree);
7602fcd1ff6SKonstantin Belousov 		p->flags &= ~PG_ZERO;
7612fcd1ff6SKonstantin Belousov 		if (p->queue != PQ_NONE) {
7622fcd1ff6SKonstantin Belousov 			KASSERT(p->queue < PQ_COUNT, ("vm_object_terminate: "
7632fcd1ff6SKonstantin Belousov 			    "page %p is not queued", p));
7642fcd1ff6SKonstantin Belousov 			pq1 = vm_page_pagequeue(p);
7652fcd1ff6SKonstantin Belousov 			if (pq != pq1) {
7664074d642SAlan Cox 				if (pq != NULL) {
7674074d642SAlan Cox 					vm_pagequeue_cnt_add(pq, dequeued);
7682fcd1ff6SKonstantin Belousov 					vm_pagequeue_unlock(pq);
7694074d642SAlan Cox 				}
7702fcd1ff6SKonstantin Belousov 				pq = pq1;
7712fcd1ff6SKonstantin Belousov 				vm_pagequeue_lock(pq);
7724074d642SAlan Cox 				dequeued = 0;
7737bbdb843SRuslan Bukin 			}
7744074d642SAlan Cox 			p->queue = PQ_NONE;
7754074d642SAlan Cox 			TAILQ_REMOVE(&pq->pq_pl, p, plinks.q);
7764074d642SAlan Cox 			dequeued--;
7777bbdb843SRuslan Bukin 		}
7782fcd1ff6SKonstantin Belousov 		if (vm_page_free_prep(p, true))
7792fcd1ff6SKonstantin Belousov 			continue;
7802fcd1ff6SKonstantin Belousov unlist:
7812fcd1ff6SKonstantin Belousov 		TAILQ_REMOVE(&object->memq, p, listq);
7822fcd1ff6SKonstantin Belousov 	}
7834074d642SAlan Cox 	if (pq != NULL) {
7844074d642SAlan Cox 		vm_pagequeue_cnt_add(pq, dequeued);
7852fcd1ff6SKonstantin Belousov 		vm_pagequeue_unlock(pq);
7864074d642SAlan Cox 	}
7872fcd1ff6SKonstantin Belousov 	if (mtx != NULL)
7882fcd1ff6SKonstantin Belousov 		mtx_unlock(mtx);
7892fcd1ff6SKonstantin Belousov 
7902fcd1ff6SKonstantin Belousov 	vm_page_free_phys_pglist(&object->memq);
7912fcd1ff6SKonstantin Belousov 
7927bbdb843SRuslan Bukin 	/*
7937bbdb843SRuslan Bukin 	 * If the object contained any pages, then reset it to an empty state.
7947bbdb843SRuslan Bukin 	 * None of the object's fields, including "resident_page_count", were
7957bbdb843SRuslan Bukin 	 * modified by the preceding loop.
7967bbdb843SRuslan Bukin 	 */
7977bbdb843SRuslan Bukin 	if (object->resident_page_count != 0) {
7987bbdb843SRuslan Bukin 		vm_radix_reclaim_allnodes(&object->rtree);
7997bbdb843SRuslan Bukin 		TAILQ_INIT(&object->memq);
8007bbdb843SRuslan Bukin 		object->resident_page_count = 0;
8017bbdb843SRuslan Bukin 		if (object->type == OBJT_VNODE)
8027bbdb843SRuslan Bukin 			vdrop(object->handle);
8037bbdb843SRuslan Bukin 	}
8047bbdb843SRuslan Bukin }
8057bbdb843SRuslan Bukin 
8067bbdb843SRuslan Bukin /*
807df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
808df8bae1dSRodney W. Grimes  *	up all previously used resources.
809df8bae1dSRodney W. Grimes  *
810df8bae1dSRodney W. Grimes  *	The object must be locked.
8111c7c3c6aSMatthew Dillon  *	This routine may block.
812df8bae1dSRodney W. Grimes  */
81395e5e988SJohn Dyson void
8141b40f8c0SMatthew Dillon vm_object_terminate(vm_object_t object)
815df8bae1dSRodney W. Grimes {
816df8bae1dSRodney W. Grimes 
81789f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
8180cddd8f0SMatthew Dillon 
81995e5e988SJohn Dyson 	/*
82095e5e988SJohn Dyson 	 * Make sure no one uses us.
82195e5e988SJohn Dyson 	 */
822069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_DEAD);
8233c631446SJohn Dyson 
824df8bae1dSRodney W. Grimes 	/*
825f6b04d2bSDavid Greenman 	 * wait for the pageout daemon to be done with the object
826df8bae1dSRodney W. Grimes 	 */
82766095752SJohn Dyson 	vm_object_pip_wait(object, "objtrm");
828df8bae1dSRodney W. Grimes 
8295526d2d9SEivind Eklund 	KASSERT(!object->paging_in_progress,
8305526d2d9SEivind Eklund 		("vm_object_terminate: pageout in progress"));
83126f9a767SRodney W. Grimes 
83226f9a767SRodney W. Grimes 	/*
8330d94caffSDavid Greenman 	 * Clean and free the pages, as appropriate. All references to the
8340d94caffSDavid Greenman 	 * object are gone, so we don't need to lock it.
83526f9a767SRodney W. Grimes 	 */
83624a1cce3SDavid Greenman 	if (object->type == OBJT_VNODE) {
837f7dd7b63SAlan Cox 		struct vnode *vp = (struct vnode *)object->handle;
83895e5e988SJohn Dyson 
83995e5e988SJohn Dyson 		/*
84095e5e988SJohn Dyson 		 * Clean pages and flush buffers.
84195e5e988SJohn Dyson 		 */
8428f9110f6SJohn Dyson 		vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
84389f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
84495e5e988SJohn Dyson 
8450d7935fdSAttilio Rao 		vinvalbuf(vp, V_SAVE, 0, 0);
846f7dd7b63SAlan Cox 
84719efd8a5SKonstantin Belousov 		BO_LOCK(&vp->v_bufobj);
84819efd8a5SKonstantin Belousov 		vp->v_bufobj.bo_flag |= BO_DEAD;
84919efd8a5SKonstantin Belousov 		BO_UNLOCK(&vp->v_bufobj);
85019efd8a5SKonstantin Belousov 
85189f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
852bef608bdSJohn Dyson 	}
853bef608bdSJohn Dyson 
854971dd342SAlfred Perlstein 	KASSERT(object->ref_count == 0,
855971dd342SAlfred Perlstein 		("vm_object_terminate: object with references, ref_count=%d",
856971dd342SAlfred Perlstein 		object->ref_count));
857996c772fSJohn Dyson 
8587bbdb843SRuslan Bukin 	if ((object->flags & OBJ_PG_DTOR) == 0)
8597bbdb843SRuslan Bukin 		vm_object_terminate_pages(object);
860bef608bdSJohn Dyson 
861f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
862f8a47341SAlan Cox 	if (__predict_false(!LIST_EMPTY(&object->rvq)))
863f8a47341SAlan Cox 		vm_reserv_break_all(object);
864f8a47341SAlan Cox #endif
8657bfda801SAlan Cox 
866e735691bSJohn Baldwin 	KASSERT(object->cred == NULL || object->type == OBJT_DEFAULT ||
867e735691bSJohn Baldwin 	    object->type == OBJT_SWAP,
868e735691bSJohn Baldwin 	    ("%s: non-swap obj %p has cred", __func__, object));
869e735691bSJohn Baldwin 
8702d8acc0fSJohn Dyson 	/*
8719fcfb650SDavid Greenman 	 * Let the pager know object is dead.
8729fcfb650SDavid Greenman 	 */
8739fcfb650SDavid Greenman 	vm_pager_deallocate(object);
87489f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
8759fcfb650SDavid Greenman 
8762ac78f0eSStephan Uphoff 	vm_object_destroy(object);
87747221757SJohn Dyson }
878df8bae1dSRodney W. Grimes 
879edf93b25SAlan Cox /*
880edf93b25SAlan Cox  * Make the page read-only so that we can clear the object flags.  However, if
881edf93b25SAlan Cox  * this is a nosync mmap then the object is likely to stay dirty so do not
882edf93b25SAlan Cox  * mess with the page and do not clear the object flags.  Returns TRUE if the
883edf93b25SAlan Cox  * page should be flushed, and FALSE otherwise.
884edf93b25SAlan Cox  */
8853280870dSKonstantin Belousov static boolean_t
886126d6082SKonstantin Belousov vm_object_page_remove_write(vm_page_t p, int flags, boolean_t *clearobjflags)
8873280870dSKonstantin Belousov {
8883280870dSKonstantin Belousov 
8893280870dSKonstantin Belousov 	/*
8903280870dSKonstantin Belousov 	 * If we have been asked to skip nosync pages and this is a
8913280870dSKonstantin Belousov 	 * nosync page, skip it.  Note that the object flags were not
8923280870dSKonstantin Belousov 	 * cleared in this case so we do not have to set them.
8933280870dSKonstantin Belousov 	 */
8943280870dSKonstantin Belousov 	if ((flags & OBJPC_NOSYNC) != 0 && (p->oflags & VPO_NOSYNC) != 0) {
895126d6082SKonstantin Belousov 		*clearobjflags = FALSE;
8963280870dSKonstantin Belousov 		return (FALSE);
8973280870dSKonstantin Belousov 	} else {
8983280870dSKonstantin Belousov 		pmap_remove_write(p);
8993280870dSKonstantin Belousov 		return (p->dirty != 0);
9003280870dSKonstantin Belousov 	}
9013280870dSKonstantin Belousov }
9023280870dSKonstantin Belousov 
903df8bae1dSRodney W. Grimes /*
904df8bae1dSRodney W. Grimes  *	vm_object_page_clean
905df8bae1dSRodney W. Grimes  *
9064f79d873SMatthew Dillon  *	Clean all dirty pages in the specified range of object.  Leaves page
9074f79d873SMatthew Dillon  * 	on whatever queue it is currently on.   If NOSYNC is set then do not
908b146f9e5SAlan Cox  *	write out pages with VPO_NOSYNC set (originally comes from MAP_NOSYNC),
9094f79d873SMatthew Dillon  *	leaving the object dirty.
91026f9a767SRodney W. Grimes  *
91143b7990eSMatthew Dillon  *	When stuffing pages asynchronously, allow clustering.  XXX we need a
91243b7990eSMatthew Dillon  *	synchronous clustering mode implementation.
91343b7990eSMatthew Dillon  *
91426f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
91526f9a767SRodney W. Grimes  *
91626f9a767SRodney W. Grimes  *	The object must be locked.
917126d6082SKonstantin Belousov  *
918126d6082SKonstantin Belousov  *	Returns FALSE if some page from the range was not written, as
919126d6082SKonstantin Belousov  *	reported by the pager, and TRUE otherwise.
92026f9a767SRodney W. Grimes  */
921126d6082SKonstantin Belousov boolean_t
92217f3095dSAlan Cox vm_object_page_clean(vm_object_t object, vm_ooffset_t start, vm_ooffset_t end,
923e239bb97SKonstantin Belousov     int flags)
924f6b04d2bSDavid Greenman {
925e239bb97SKonstantin Belousov 	vm_page_t np, p;
92617f3095dSAlan Cox 	vm_pindex_t pi, tend, tstart;
927126d6082SKonstantin Belousov 	int curgeneration, n, pagerflags;
928126d6082SKonstantin Belousov 	boolean_t clearobjflags, eio, res;
929f6b04d2bSDavid Greenman 
93089f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
931e5f299ffSKonstantin Belousov 
932e5f299ffSKonstantin Belousov 	/*
933e5f299ffSKonstantin Belousov 	 * The OBJ_MIGHTBEDIRTY flag is only set for OBJT_VNODE
934e5f299ffSKonstantin Belousov 	 * objects.  The check below prevents the function from
935e5f299ffSKonstantin Belousov 	 * operating on non-vnode objects.
936e5f299ffSKonstantin Belousov 	 */
937e239bb97SKonstantin Belousov 	if ((object->flags & OBJ_MIGHTBEDIRTY) == 0 ||
938e239bb97SKonstantin Belousov 	    object->resident_page_count == 0)
939126d6082SKonstantin Belousov 		return (TRUE);
940f6b04d2bSDavid Greenman 
941e239bb97SKonstantin Belousov 	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) != 0 ?
942e239bb97SKonstantin Belousov 	    VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
943e239bb97SKonstantin Belousov 	pagerflags |= (flags & OBJPC_INVAL) != 0 ? VM_PAGER_PUT_INVAL : 0;
944e239bb97SKonstantin Belousov 
94517f3095dSAlan Cox 	tstart = OFF_TO_IDX(start);
94617f3095dSAlan Cox 	tend = (end == 0) ? object->size : OFF_TO_IDX(end + PAGE_MASK);
94717f3095dSAlan Cox 	clearobjflags = tstart == 0 && tend >= object->size;
948126d6082SKonstantin Belousov 	res = TRUE;
949f6b04d2bSDavid Greenman 
950bd7e5f99SJohn Dyson rescan:
9512d8acc0fSJohn Dyson 	curgeneration = object->generation;
9522d8acc0fSJohn Dyson 
95317f3095dSAlan Cox 	for (p = vm_page_find_least(object, tstart); p != NULL; p = np) {
954bd7e5f99SJohn Dyson 		pi = p->pindex;
955e239bb97SKonstantin Belousov 		if (pi >= tend)
956e239bb97SKonstantin Belousov 			break;
957e239bb97SKonstantin Belousov 		np = TAILQ_NEXT(p, listq);
958e239bb97SKonstantin Belousov 		if (p->valid == 0)
959aef922f5SJohn Dyson 			continue;
960c7aebda8SAttilio Rao 		if (vm_page_sleep_if_busy(p, "vpcwai")) {
961e65919f9SKonstantin Belousov 			if (object->generation != curgeneration) {
962e65919f9SKonstantin Belousov 				if ((flags & OBJPC_SYNC) != 0)
963e239bb97SKonstantin Belousov 					goto rescan;
964e65919f9SKonstantin Belousov 				else
965126d6082SKonstantin Belousov 					clearobjflags = FALSE;
966e65919f9SKonstantin Belousov 			}
967780636b7SKonstantin Belousov 			np = vm_page_find_least(object, pi);
968780636b7SKonstantin Belousov 			continue;
969f6b04d2bSDavid Greenman 		}
9703280870dSKonstantin Belousov 		if (!vm_object_page_remove_write(p, flags, &clearobjflags))
971bd7e5f99SJohn Dyson 			continue;
972e239bb97SKonstantin Belousov 
9733280870dSKonstantin Belousov 		n = vm_object_page_collect_flush(object, p, pagerflags,
974126d6082SKonstantin Belousov 		    flags, &clearobjflags, &eio);
975126d6082SKonstantin Belousov 		if (eio) {
976126d6082SKonstantin Belousov 			res = FALSE;
977126d6082SKonstantin Belousov 			clearobjflags = FALSE;
978126d6082SKonstantin Belousov 		}
979e65919f9SKonstantin Belousov 		if (object->generation != curgeneration) {
980e65919f9SKonstantin Belousov 			if ((flags & OBJPC_SYNC) != 0)
981b9b7a4beSMatthew Dillon 				goto rescan;
982e65919f9SKonstantin Belousov 			else
983126d6082SKonstantin Belousov 				clearobjflags = FALSE;
984e65919f9SKonstantin Belousov 		}
985031ec8c1SKonstantin Belousov 
986031ec8c1SKonstantin Belousov 		/*
987031ec8c1SKonstantin Belousov 		 * If the VOP_PUTPAGES() did a truncated write, so
988031ec8c1SKonstantin Belousov 		 * that even the first page of the run is not fully
989031ec8c1SKonstantin Belousov 		 * written, vm_pageout_flush() returns 0 as the run
990031ec8c1SKonstantin Belousov 		 * length.  Since the condition that caused truncated
991031ec8c1SKonstantin Belousov 		 * write may be permanent, e.g. exhausted free space,
992031ec8c1SKonstantin Belousov 		 * accepting n == 0 would cause an infinite loop.
993031ec8c1SKonstantin Belousov 		 *
994031ec8c1SKonstantin Belousov 		 * Forwarding the iterator leaves the unwritten page
995031ec8c1SKonstantin Belousov 		 * behind, but there is not much we can do there if
996031ec8c1SKonstantin Belousov 		 * filesystem refuses to write it.
997031ec8c1SKonstantin Belousov 		 */
998126d6082SKonstantin Belousov 		if (n == 0) {
999031ec8c1SKonstantin Belousov 			n = 1;
1000126d6082SKonstantin Belousov 			clearobjflags = FALSE;
1001126d6082SKonstantin Belousov 		}
1002e239bb97SKonstantin Belousov 		np = vm_page_find_least(object, pi + n);
1003b9b7a4beSMatthew Dillon 	}
1004b9b7a4beSMatthew Dillon #if 0
1005e239bb97SKonstantin Belousov 	VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC) ? MNT_WAIT : 0);
1006b9b7a4beSMatthew Dillon #endif
1007b9b7a4beSMatthew Dillon 
1008edf93b25SAlan Cox 	if (clearobjflags)
10093280870dSKonstantin Belousov 		vm_object_clear_flag(object, OBJ_MIGHTBEDIRTY);
1010126d6082SKonstantin Belousov 	return (res);
1011b9b7a4beSMatthew Dillon }
1012b9b7a4beSMatthew Dillon 
1013b9b7a4beSMatthew Dillon static int
10143280870dSKonstantin Belousov vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int pagerflags,
1015126d6082SKonstantin Belousov     int flags, boolean_t *clearobjflags, boolean_t *eio)
1016b9b7a4beSMatthew Dillon {
10173157c503SKonstantin Belousov 	vm_page_t ma[vm_pageout_page_count], p_first, tp;
10183157c503SKonstantin Belousov 	int count, i, mreq, runlen;
1019b9b7a4beSMatthew Dillon 
10207bec141bSKip Macy 	vm_page_lock_assert(p, MA_NOTOWNED);
102189f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
10223157c503SKonstantin Belousov 
10233157c503SKonstantin Belousov 	count = 1;
10243157c503SKonstantin Belousov 	mreq = 0;
10253157c503SKonstantin Belousov 
10263157c503SKonstantin Belousov 	for (tp = p; count < vm_pageout_page_count; count++) {
10273157c503SKonstantin Belousov 		tp = vm_page_next(tp);
1028c7aebda8SAttilio Rao 		if (tp == NULL || vm_page_busied(tp))
1029bd7e5f99SJohn Dyson 			break;
10303280870dSKonstantin Belousov 		if (!vm_object_page_remove_write(tp, flags, clearobjflags))
1031bd7e5f99SJohn Dyson 			break;
1032bd7e5f99SJohn Dyson 	}
1033aef922f5SJohn Dyson 
10343157c503SKonstantin Belousov 	for (p_first = p; count < vm_pageout_page_count; count++) {
10353157c503SKonstantin Belousov 		tp = vm_page_prev(p_first);
1036c7aebda8SAttilio Rao 		if (tp == NULL || vm_page_busied(tp))
1037bd7e5f99SJohn Dyson 			break;
10383280870dSKonstantin Belousov 		if (!vm_object_page_remove_write(tp, flags, clearobjflags))
1039bd7e5f99SJohn Dyson 			break;
10403157c503SKonstantin Belousov 		p_first = tp;
10413157c503SKonstantin Belousov 		mreq++;
1042bd7e5f99SJohn Dyson 	}
1043bd7e5f99SJohn Dyson 
10443157c503SKonstantin Belousov 	for (tp = p_first, i = 0; i < count; tp = TAILQ_NEXT(tp, listq), i++)
10453157c503SKonstantin Belousov 		ma[i] = tp;
1046cf2819ccSJohn Dyson 
1047126d6082SKonstantin Belousov 	vm_pageout_flush(ma, count, pagerflags, mreq, &runlen, eio);
10481e8a675cSKonstantin Belousov 	return (runlen);
104926f9a767SRodney W. Grimes }
1050df8bae1dSRodney W. Grimes 
10511efb74fbSJohn Dyson /*
1052950f8459SAlan Cox  * Note that there is absolutely no sense in writing out
1053950f8459SAlan Cox  * anonymous objects, so we track down the vnode object
1054950f8459SAlan Cox  * to write out.
1055950f8459SAlan Cox  * We invalidate (remove) all pages from the address space
1056950f8459SAlan Cox  * for semantic correctness.
1057950f8459SAlan Cox  *
10586bbee8e2SAlan Cox  * If the backing object is a device object with unmanaged pages, then any
10596bbee8e2SAlan Cox  * mappings to the specified range of pages must be removed before this
10606bbee8e2SAlan Cox  * function is called.
10616bbee8e2SAlan Cox  *
1062950f8459SAlan Cox  * Note: certain anonymous maps, such as MAP_NOSYNC maps,
1063950f8459SAlan Cox  * may start out with a NULL object.
1064950f8459SAlan Cox  */
1065126d6082SKonstantin Belousov boolean_t
1066950f8459SAlan Cox vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size,
1067950f8459SAlan Cox     boolean_t syncio, boolean_t invalidate)
1068950f8459SAlan Cox {
1069950f8459SAlan Cox 	vm_object_t backing_object;
1070950f8459SAlan Cox 	struct vnode *vp;
10713b582b4eSTor Egge 	struct mount *mp;
1072126d6082SKonstantin Belousov 	int error, flags, fsync_after;
1073126d6082SKonstantin Belousov 	boolean_t res;
1074950f8459SAlan Cox 
1075950f8459SAlan Cox 	if (object == NULL)
1076126d6082SKonstantin Belousov 		return (TRUE);
1077126d6082SKonstantin Belousov 	res = TRUE;
1078126d6082SKonstantin Belousov 	error = 0;
107989f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
1080950f8459SAlan Cox 	while ((backing_object = object->backing_object) != NULL) {
108189f6b863SAttilio Rao 		VM_OBJECT_WLOCK(backing_object);
108256e0670fSAlan Cox 		offset += object->backing_object_offset;
108389f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
1084950f8459SAlan Cox 		object = backing_object;
1085950f8459SAlan Cox 		if (object->size < OFF_TO_IDX(offset + size))
1086950f8459SAlan Cox 			size = IDX_TO_OFF(object->size) - offset;
1087950f8459SAlan Cox 	}
1088950f8459SAlan Cox 	/*
1089950f8459SAlan Cox 	 * Flush pages if writing is allowed, invalidate them
1090950f8459SAlan Cox 	 * if invalidation requested.  Pages undergoing I/O
1091950f8459SAlan Cox 	 * will be ignored by vm_object_page_remove().
1092950f8459SAlan Cox 	 *
1093950f8459SAlan Cox 	 * We cannot lock the vnode and then wait for paging
1094950f8459SAlan Cox 	 * to complete without deadlocking against vm_fault.
1095950f8459SAlan Cox 	 * Instead we simply call vm_object_page_remove() and
1096950f8459SAlan Cox 	 * allow it to block internally on a page-by-page
1097950f8459SAlan Cox 	 * basis when it encounters pages undergoing async
1098950f8459SAlan Cox 	 * I/O.
1099950f8459SAlan Cox 	 */
1100950f8459SAlan Cox 	if (object->type == OBJT_VNODE &&
11015bf94937SKonstantin Belousov 	    (object->flags & OBJ_MIGHTBEDIRTY) != 0 &&
11025bf94937SKonstantin Belousov 	    ((vp = object->handle)->v_vflag & VV_NOSYNC) == 0) {
110389f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
11043b582b4eSTor Egge 		(void) vn_start_write(vp, &mp, V_WAIT);
1105cb05b60aSAttilio Rao 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
110675ff604aSKonstantin Belousov 		if (syncio && !invalidate && offset == 0 &&
1107d1780e8dSKonstantin Belousov 		    atop(size) == object->size) {
110875ff604aSKonstantin Belousov 			/*
110975ff604aSKonstantin Belousov 			 * If syncing the whole mapping of the file,
111075ff604aSKonstantin Belousov 			 * it is faster to schedule all the writes in
111175ff604aSKonstantin Belousov 			 * async mode, also allowing the clustering,
111275ff604aSKonstantin Belousov 			 * and then wait for i/o to complete.
111375ff604aSKonstantin Belousov 			 */
111475ff604aSKonstantin Belousov 			flags = 0;
111575ff604aSKonstantin Belousov 			fsync_after = TRUE;
111675ff604aSKonstantin Belousov 		} else {
1117950f8459SAlan Cox 			flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
111875ff604aSKonstantin Belousov 			flags |= invalidate ? (OBJPC_SYNC | OBJPC_INVAL) : 0;
111975ff604aSKonstantin Belousov 			fsync_after = FALSE;
112075ff604aSKonstantin Belousov 		}
112189f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
1122126d6082SKonstantin Belousov 		res = vm_object_page_clean(object, offset, offset + size,
1123126d6082SKonstantin Belousov 		    flags);
112489f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
112575ff604aSKonstantin Belousov 		if (fsync_after)
1126126d6082SKonstantin Belousov 			error = VOP_FSYNC(vp, MNT_WAIT, curthread);
112722db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
11283b582b4eSTor Egge 		vn_finished_write(mp);
1129126d6082SKonstantin Belousov 		if (error != 0)
1130126d6082SKonstantin Belousov 			res = FALSE;
113189f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
1132950f8459SAlan Cox 	}
1133950f8459SAlan Cox 	if ((object->type == OBJT_VNODE ||
1134950f8459SAlan Cox 	     object->type == OBJT_DEVICE) && invalidate) {
11356bbee8e2SAlan Cox 		if (object->type == OBJT_DEVICE)
11366bbee8e2SAlan Cox 			/*
11376bbee8e2SAlan Cox 			 * The option OBJPR_NOTMAPPED must be passed here
11386bbee8e2SAlan Cox 			 * because vm_object_page_remove() cannot remove
11396bbee8e2SAlan Cox 			 * unmanaged mappings.
11406bbee8e2SAlan Cox 			 */
11416bbee8e2SAlan Cox 			flags = OBJPR_NOTMAPPED;
11426bbee8e2SAlan Cox 		else if (old_msync)
11436195b24aSKonstantin Belousov 			flags = 0;
11446bbee8e2SAlan Cox 		else
11456195b24aSKonstantin Belousov 			flags = OBJPR_CLEANONLY;
11466bbee8e2SAlan Cox 		vm_object_page_remove(object, OFF_TO_IDX(offset),
11476bbee8e2SAlan Cox 		    OFF_TO_IDX(offset + size + PAGE_MASK), flags);
1148950f8459SAlan Cox 	}
114989f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
1150126d6082SKonstantin Belousov 	return (res);
1151950f8459SAlan Cox }
1152950f8459SAlan Cox 
1153950f8459SAlan Cox /*
1154aa3650eaSMark Johnston  * Determine whether the given advice can be applied to the object.  Advice is
1155aa3650eaSMark Johnston  * not applied to unmanaged pages since they never belong to page queues, and
1156aa3650eaSMark Johnston  * since MADV_FREE is destructive, it can apply only to anonymous pages that
1157aa3650eaSMark Johnston  * have been mapped at most once.
1158aa3650eaSMark Johnston  */
1159aa3650eaSMark Johnston static bool
1160aa3650eaSMark Johnston vm_object_advice_applies(vm_object_t object, int advice)
1161aa3650eaSMark Johnston {
1162aa3650eaSMark Johnston 
1163aa3650eaSMark Johnston 	if ((object->flags & OBJ_UNMANAGED) != 0)
1164aa3650eaSMark Johnston 		return (false);
1165aa3650eaSMark Johnston 	if (advice != MADV_FREE)
1166aa3650eaSMark Johnston 		return (true);
1167aa3650eaSMark Johnston 	return ((object->type == OBJT_DEFAULT || object->type == OBJT_SWAP) &&
1168aa3650eaSMark Johnston 	    (object->flags & OBJ_ONEMAPPING) != 0);
1169aa3650eaSMark Johnston }
1170aa3650eaSMark Johnston 
1171aa3650eaSMark Johnston static void
1172aa3650eaSMark Johnston vm_object_madvise_freespace(vm_object_t object, int advice, vm_pindex_t pindex,
1173aa3650eaSMark Johnston     vm_size_t size)
1174aa3650eaSMark Johnston {
1175aa3650eaSMark Johnston 
1176aa3650eaSMark Johnston 	if (advice == MADV_FREE && object->type == OBJT_SWAP)
1177aa3650eaSMark Johnston 		swap_pager_freespace(object, pindex, size);
1178aa3650eaSMark Johnston }
1179aa3650eaSMark Johnston 
1180aa3650eaSMark Johnston /*
1181867a482dSJohn Dyson  *	vm_object_madvise:
1182867a482dSJohn Dyson  *
1183867a482dSJohn Dyson  *	Implements the madvise function at the object/page level.
11841c7c3c6aSMatthew Dillon  *
1185193b9358SAlan Cox  *	MADV_WILLNEED	(any object)
1186193b9358SAlan Cox  *
1187193b9358SAlan Cox  *	    Activate the specified pages if they are resident.
1188193b9358SAlan Cox  *
1189193b9358SAlan Cox  *	MADV_DONTNEED	(any object)
1190193b9358SAlan Cox  *
1191193b9358SAlan Cox  *	    Deactivate the specified pages if they are resident.
1192193b9358SAlan Cox  *
1193193b9358SAlan Cox  *	MADV_FREE	(OBJT_DEFAULT/OBJT_SWAP objects,
1194193b9358SAlan Cox  *			 OBJ_ONEMAPPING only)
1195193b9358SAlan Cox  *
1196193b9358SAlan Cox  *	    Deactivate and clean the specified pages if they are
1197193b9358SAlan Cox  *	    resident.  This permits the process to reuse the pages
1198193b9358SAlan Cox  *	    without faulting or the kernel to reclaim the pages
1199193b9358SAlan Cox  *	    without I/O.
1200867a482dSJohn Dyson  */
1201867a482dSJohn Dyson void
120292a59946SJohn Baldwin vm_object_madvise(vm_object_t object, vm_pindex_t pindex, vm_pindex_t end,
1203c2655a40SMark Johnston     int advice)
1204867a482dSJohn Dyson {
120592a59946SJohn Baldwin 	vm_pindex_t tpindex;
120634567de7SAlan Cox 	vm_object_t backing_object, tobject;
1207aa3650eaSMark Johnston 	vm_page_t m, tm;
1208867a482dSJohn Dyson 
1209867a482dSJohn Dyson 	if (object == NULL)
1210867a482dSJohn Dyson 		return;
1211c2655a40SMark Johnston 
12126e20a165SJohn Dyson relookup:
1213aa3650eaSMark Johnston 	VM_OBJECT_WLOCK(object);
1214aa3650eaSMark Johnston 	if (!vm_object_advice_applies(object, advice)) {
1215aa3650eaSMark Johnston 		VM_OBJECT_WUNLOCK(object);
1216aa3650eaSMark Johnston 		return;
12176e20a165SJohn Dyson 	}
1218aa3650eaSMark Johnston 	for (m = vm_page_find_least(object, pindex); pindex < end; pindex++) {
1219aa3650eaSMark Johnston 		tobject = object;
1220c2655a40SMark Johnston 
12211ce137beSMatthew Dillon 		/*
1222aa3650eaSMark Johnston 		 * If the next page isn't resident in the top-level object, we
1223aa3650eaSMark Johnston 		 * need to search the shadow chain.  When applying MADV_FREE, we
1224aa3650eaSMark Johnston 		 * take care to release any swap space used to store
1225aa3650eaSMark Johnston 		 * non-resident pages.
1226aa3650eaSMark Johnston 		 */
1227aa3650eaSMark Johnston 		if (m == NULL || pindex < m->pindex) {
1228aa3650eaSMark Johnston 			/*
1229aa3650eaSMark Johnston 			 * Optimize a common case: if the top-level object has
1230aa3650eaSMark Johnston 			 * no backing object, we can skip over the non-resident
1231aa3650eaSMark Johnston 			 * range in constant time.
12321ce137beSMatthew Dillon 			 */
1233c2655a40SMark Johnston 			if (object->backing_object == NULL) {
1234c2655a40SMark Johnston 				tpindex = (m != NULL && m->pindex < end) ?
1235c2655a40SMark Johnston 				    m->pindex : end;
1236aa3650eaSMark Johnston 				vm_object_madvise_freespace(object, advice,
1237aa3650eaSMark Johnston 				    pindex, tpindex - pindex);
1238c2655a40SMark Johnston 				if ((pindex = tpindex) == end)
1239c2655a40SMark Johnston 					break;
1240aa3650eaSMark Johnston 				goto next_page;
1241aa3650eaSMark Johnston 			}
1242aa3650eaSMark Johnston 
1243aa3650eaSMark Johnston 			tpindex = pindex;
1244aa3650eaSMark Johnston 			do {
1245aa3650eaSMark Johnston 				vm_object_madvise_freespace(tobject, advice,
1246aa3650eaSMark Johnston 				    tpindex, 1);
12471ce137beSMatthew Dillon 				/*
1248aa3650eaSMark Johnston 				 * Prepare to search the next object in the
1249aa3650eaSMark Johnston 				 * chain.
12501ce137beSMatthew Dillon 				 */
125134567de7SAlan Cox 				backing_object = tobject->backing_object;
125234567de7SAlan Cox 				if (backing_object == NULL)
1253aa3650eaSMark Johnston 					goto next_pindex;
125489f6b863SAttilio Rao 				VM_OBJECT_WLOCK(backing_object);
1255aa3650eaSMark Johnston 				tpindex +=
1256aa3650eaSMark Johnston 				    OFF_TO_IDX(tobject->backing_object_offset);
12579b98b796SAlan Cox 				if (tobject != object)
125889f6b863SAttilio Rao 					VM_OBJECT_WUNLOCK(tobject);
125934567de7SAlan Cox 				tobject = backing_object;
1260aa3650eaSMark Johnston 				if (!vm_object_advice_applies(tobject, advice))
1261aa3650eaSMark Johnston 					goto next_pindex;
1262aa3650eaSMark Johnston 			} while ((tm = vm_page_lookup(tobject, tpindex)) ==
1263aa3650eaSMark Johnston 			    NULL);
1264aa3650eaSMark Johnston 		} else {
1265aa3650eaSMark Johnston next_page:
1266aa3650eaSMark Johnston 			tm = m;
1267aa3650eaSMark Johnston 			m = TAILQ_NEXT(m, listq);
1268c2655a40SMark Johnston 		}
1269c2655a40SMark Johnston 
1270867a482dSJohn Dyson 		/*
12716a2a3d73SAlan Cox 		 * If the page is not in a normal state, skip it.
1272867a482dSJohn Dyson 		 */
1273aa3650eaSMark Johnston 		if (tm->valid != VM_PAGE_BITS_ALL)
1274aa3650eaSMark Johnston 			goto next_pindex;
1275aa3650eaSMark Johnston 		vm_page_lock(tm);
1276aa3650eaSMark Johnston 		if (tm->hold_count != 0 || tm->wire_count != 0) {
1277aa3650eaSMark Johnston 			vm_page_unlock(tm);
1278aa3650eaSMark Johnston 			goto next_pindex;
12796e20a165SJohn Dyson 		}
1280aa3650eaSMark Johnston 		KASSERT((tm->flags & PG_FICTITIOUS) == 0,
1281aa3650eaSMark Johnston 		    ("vm_object_madvise: page %p is fictitious", tm));
1282aa3650eaSMark Johnston 		KASSERT((tm->oflags & VPO_UNMANAGED) == 0,
1283aa3650eaSMark Johnston 		    ("vm_object_madvise: page %p is not managed", tm));
1284aa3650eaSMark Johnston 		if (vm_page_busied(tm)) {
1285aa3650eaSMark Johnston 			if (object != tobject)
1286aa3650eaSMark Johnston 				VM_OBJECT_WUNLOCK(tobject);
1287aa3650eaSMark Johnston 			VM_OBJECT_WUNLOCK(object);
1288c2655a40SMark Johnston 			if (advice == MADV_WILLNEED) {
1289b11b56b5SAlan Cox 				/*
1290b11b56b5SAlan Cox 				 * Reference the page before unlocking and
1291b11b56b5SAlan Cox 				 * sleeping so that the page daemon is less
1292b11b56b5SAlan Cox 				 * likely to reclaim it.
1293b11b56b5SAlan Cox 				 */
1294aa3650eaSMark Johnston 				vm_page_aflag_set(tm, PGA_REFERENCED);
1295567e51e1SAlan Cox 			}
1296aa3650eaSMark Johnston 			vm_page_busy_sleep(tm, "madvpo", false);
12976e20a165SJohn Dyson   			goto relookup;
129834567de7SAlan Cox 		}
1299aa3650eaSMark Johnston 		vm_page_advise(tm, advice);
1300aa3650eaSMark Johnston 		vm_page_unlock(tm);
1301aa3650eaSMark Johnston 		vm_object_madvise_freespace(tobject, advice, tm->pindex, 1);
1302aa3650eaSMark Johnston next_pindex:
13039b98b796SAlan Cox 		if (tobject != object)
130489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(tobject);
1305867a482dSJohn Dyson 	}
130689f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
1307867a482dSJohn Dyson }
1308867a482dSJohn Dyson 
1309867a482dSJohn Dyson /*
1310df8bae1dSRodney W. Grimes  *	vm_object_shadow:
1311df8bae1dSRodney W. Grimes  *
1312df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
1313df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
1314df8bae1dSRodney W. Grimes  *	object reference is deallocated.
1315df8bae1dSRodney W. Grimes  *
1316df8bae1dSRodney W. Grimes  *	The new object and offset into that object
1317df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
1318df8bae1dSRodney W. Grimes  */
131926f9a767SRodney W. Grimes void
13201b40f8c0SMatthew Dillon vm_object_shadow(
13211b40f8c0SMatthew Dillon 	vm_object_t *object,	/* IN/OUT */
13221b40f8c0SMatthew Dillon 	vm_ooffset_t *offset,	/* IN/OUT */
13231b40f8c0SMatthew Dillon 	vm_size_t length)
1324df8bae1dSRodney W. Grimes {
1325d031cff1SMatthew Dillon 	vm_object_t source;
1326d031cff1SMatthew Dillon 	vm_object_t result;
1327df8bae1dSRodney W. Grimes 
1328df8bae1dSRodney W. Grimes 	source = *object;
1329df8bae1dSRodney W. Grimes 
1330df8bae1dSRodney W. Grimes 	/*
13319a2f6362SAlan Cox 	 * Don't create the new object if the old object isn't shared.
13329a2f6362SAlan Cox 	 */
1333570a2f4aSAlan Cox 	if (source != NULL) {
133489f6b863SAttilio Rao 		VM_OBJECT_WLOCK(source);
1335570a2f4aSAlan Cox 		if (source->ref_count == 1 &&
13369a2f6362SAlan Cox 		    source->handle == NULL &&
13379a2f6362SAlan Cox 		    (source->type == OBJT_DEFAULT ||
13389917e010SAlan Cox 		     source->type == OBJT_SWAP)) {
133989f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(source);
13409a2f6362SAlan Cox 			return;
13419917e010SAlan Cox 		}
134289f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(source);
1343570a2f4aSAlan Cox 	}
13449a2f6362SAlan Cox 
13459a2f6362SAlan Cox 	/*
1346570a2f4aSAlan Cox 	 * Allocate a new object with the given length.
1347df8bae1dSRodney W. Grimes 	 */
13480cc74f14SAlan Cox 	result = vm_object_allocate(OBJT_DEFAULT, atop(length));
1349df8bae1dSRodney W. Grimes 
1350df8bae1dSRodney W. Grimes 	/*
13510d94caffSDavid Greenman 	 * The new object shadows the source object, adding a reference to it.
13520d94caffSDavid Greenman 	 * Our caller changes his reference to point to the new object,
13530d94caffSDavid Greenman 	 * removing a reference to the source object.  Net result: no change
13540d94caffSDavid Greenman 	 * of reference count.
13559b09fe24SMatthew Dillon 	 *
13569b09fe24SMatthew Dillon 	 * Try to optimize the result object's page color when shadowing
1357956f3135SPhilippe Charnier 	 * in order to maintain page coloring consistency in the combined
13589b09fe24SMatthew Dillon 	 * shadowed object.
1359df8bae1dSRodney W. Grimes 	 */
136024a1cce3SDavid Greenman 	result->backing_object = source;
13619174ca7bSTor Egge 	/*
13629174ca7bSTor Egge 	 * Store the offset into the source object, and fix up the offset into
13639174ca7bSTor Egge 	 * the new object.
13649174ca7bSTor Egge 	 */
13659174ca7bSTor Egge 	result->backing_object_offset = *offset;
1366570a2f4aSAlan Cox 	if (source != NULL) {
136789f6b863SAttilio Rao 		VM_OBJECT_WLOCK(source);
1368*3f289c3fSJeff Roberson 		result->domain = source->domain;
13691c500307SAlan Cox 		LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1370eaf13dd7SJohn Dyson 		source->shadow_count++;
1371f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
13727b54b1a9SAlan Cox 		result->flags |= source->flags & OBJ_COLORED;
1373f8a47341SAlan Cox 		result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) &
1374f8a47341SAlan Cox 		    ((1 << (VM_NFREEORDER - 1)) - 1);
1375f8a47341SAlan Cox #endif
137689f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(source);
1377de5f6a77SJohn Dyson 	}
1378df8bae1dSRodney W. Grimes 
1379df8bae1dSRodney W. Grimes 
1380df8bae1dSRodney W. Grimes 	/*
1381df8bae1dSRodney W. Grimes 	 * Return the new things
1382df8bae1dSRodney W. Grimes 	 */
1383df8bae1dSRodney W. Grimes 	*offset = 0;
1384df8bae1dSRodney W. Grimes 	*object = result;
1385df8bae1dSRodney W. Grimes }
1386df8bae1dSRodney W. Grimes 
1387c5aaa06dSAlan Cox /*
1388c5aaa06dSAlan Cox  *	vm_object_split:
1389c5aaa06dSAlan Cox  *
1390c5aaa06dSAlan Cox  * Split the pages in a map entry into a new object.  This affords
1391c5aaa06dSAlan Cox  * easier removal of unused pages, and keeps object inheritance from
1392c5aaa06dSAlan Cox  * being a negative impact on memory usage.
1393c5aaa06dSAlan Cox  */
1394c5aaa06dSAlan Cox void
1395c5aaa06dSAlan Cox vm_object_split(vm_map_entry_t entry)
1396c5aaa06dSAlan Cox {
139773000556SAlan Cox 	vm_page_t m, m_next;
1398c5aaa06dSAlan Cox 	vm_object_t orig_object, new_object, source;
139973000556SAlan Cox 	vm_pindex_t idx, offidxstart;
140073000556SAlan Cox 	vm_size_t size;
1401c5aaa06dSAlan Cox 
1402c5aaa06dSAlan Cox 	orig_object = entry->object.vm_object;
1403c5aaa06dSAlan Cox 	if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1404c5aaa06dSAlan Cox 		return;
1405c5aaa06dSAlan Cox 	if (orig_object->ref_count <= 1)
1406c5aaa06dSAlan Cox 		return;
140789f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(orig_object);
1408c5aaa06dSAlan Cox 
14094da9f125SAlan Cox 	offidxstart = OFF_TO_IDX(entry->offset);
141095442adfSAlan Cox 	size = atop(entry->end - entry->start);
1411c5aaa06dSAlan Cox 
14124da9f125SAlan Cox 	/*
14134da9f125SAlan Cox 	 * If swap_pager_copy() is later called, it will convert new_object
14144da9f125SAlan Cox 	 * into a swap object.
14154da9f125SAlan Cox 	 */
14164da9f125SAlan Cox 	new_object = vm_object_allocate(OBJT_DEFAULT, size);
1417c5aaa06dSAlan Cox 
1418c5474b8fSAlan Cox 	/*
1419c5474b8fSAlan Cox 	 * At this point, the new object is still private, so the order in
1420c5474b8fSAlan Cox 	 * which the original and new objects are locked does not matter.
1421c5474b8fSAlan Cox 	 */
142289f6b863SAttilio Rao 	VM_OBJECT_WLOCK(new_object);
142389f6b863SAttilio Rao 	VM_OBJECT_WLOCK(orig_object);
1424*3f289c3fSJeff Roberson 	new_object->domain = orig_object->domain;
1425c5aaa06dSAlan Cox 	source = orig_object->backing_object;
1426c5aaa06dSAlan Cox 	if (source != NULL) {
142789f6b863SAttilio Rao 		VM_OBJECT_WLOCK(source);
142819c244d0SAlan Cox 		if ((source->flags & OBJ_DEAD) != 0) {
142989f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(source);
143089f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(orig_object);
143189f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(new_object);
143219c244d0SAlan Cox 			vm_object_deallocate(new_object);
143389f6b863SAttilio Rao 			VM_OBJECT_WLOCK(orig_object);
143419c244d0SAlan Cox 			return;
143519c244d0SAlan Cox 		}
14361c500307SAlan Cox 		LIST_INSERT_HEAD(&source->shadow_head,
1437c5aaa06dSAlan Cox 				  new_object, shadow_list);
14388e3a76fbSAlan Cox 		source->shadow_count++;
1439b921a12bSAlan Cox 		vm_object_reference_locked(source);	/* for new_object */
1440c5aaa06dSAlan Cox 		vm_object_clear_flag(source, OBJ_ONEMAPPING);
144189f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(source);
1442c5aaa06dSAlan Cox 		new_object->backing_object_offset =
14434da9f125SAlan Cox 			orig_object->backing_object_offset + entry->offset;
1444c5aaa06dSAlan Cox 		new_object->backing_object = source;
1445c5aaa06dSAlan Cox 	}
1446ef694c1aSEdward Tomasz Napierala 	if (orig_object->cred != NULL) {
1447ef694c1aSEdward Tomasz Napierala 		new_object->cred = orig_object->cred;
1448ef694c1aSEdward Tomasz Napierala 		crhold(orig_object->cred);
14493364c323SKonstantin Belousov 		new_object->charge = ptoa(size);
14503364c323SKonstantin Belousov 		KASSERT(orig_object->charge >= ptoa(size),
14513364c323SKonstantin Belousov 		    ("orig_object->charge < 0"));
14523364c323SKonstantin Belousov 		orig_object->charge -= ptoa(size);
14533364c323SKonstantin Belousov 	}
1454c5aaa06dSAlan Cox retry:
1455b382c10aSKonstantin Belousov 	m = vm_page_find_least(orig_object, offidxstart);
145673000556SAlan Cox 	for (; m != NULL && (idx = m->pindex - offidxstart) < size;
145773000556SAlan Cox 	    m = m_next) {
145873000556SAlan Cox 		m_next = TAILQ_NEXT(m, listq);
1459c5aaa06dSAlan Cox 
1460c5aaa06dSAlan Cox 		/*
1461c5aaa06dSAlan Cox 		 * We must wait for pending I/O to complete before we can
1462c5aaa06dSAlan Cox 		 * rename the page.
1463c5aaa06dSAlan Cox 		 *
1464c5aaa06dSAlan Cox 		 * We do not have to VM_PROT_NONE the page as mappings should
1465c5aaa06dSAlan Cox 		 * not be changed by this operation.
1466c5aaa06dSAlan Cox 		 */
1467c7aebda8SAttilio Rao 		if (vm_page_busied(m)) {
146889f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(new_object);
1469c7aebda8SAttilio Rao 			vm_page_lock(m);
1470c7aebda8SAttilio Rao 			VM_OBJECT_WUNLOCK(orig_object);
14715975e53dSKonstantin Belousov 			vm_page_busy_sleep(m, "spltwt", false);
1472c7aebda8SAttilio Rao 			VM_OBJECT_WLOCK(orig_object);
147389f6b863SAttilio Rao 			VM_OBJECT_WLOCK(new_object);
1474c5aaa06dSAlan Cox 			goto retry;
1475de33beddSAlan Cox 		}
1476e946b949SAttilio Rao 
14773453bca8SAlan Cox 		/* vm_page_rename() will dirty the page. */
1478e946b949SAttilio Rao 		if (vm_page_rename(m, new_object, idx)) {
1479e946b949SAttilio Rao 			VM_OBJECT_WUNLOCK(new_object);
1480e946b949SAttilio Rao 			VM_OBJECT_WUNLOCK(orig_object);
14818d6fbbb8SJeff Roberson 			vm_radix_wait();
1482e946b949SAttilio Rao 			VM_OBJECT_WLOCK(orig_object);
1483e946b949SAttilio Rao 			VM_OBJECT_WLOCK(new_object);
1484e946b949SAttilio Rao 			goto retry;
1485e946b949SAttilio Rao 		}
1486b5f359b7SAlan Cox #if VM_NRESERVLEVEL > 0
1487b5f359b7SAlan Cox 		/*
1488b5f359b7SAlan Cox 		 * If some of the reservation's allocated pages remain with
1489b5f359b7SAlan Cox 		 * the original object, then transferring the reservation to
1490b5f359b7SAlan Cox 		 * the new object is neither particularly beneficial nor
1491b5f359b7SAlan Cox 		 * particularly harmful as compared to leaving the reservation
1492b5f359b7SAlan Cox 		 * with the original object.  If, however, all of the
1493b5f359b7SAlan Cox 		 * reservation's allocated pages are transferred to the new
1494b5f359b7SAlan Cox 		 * object, then transferring the reservation is typically
1495b5f359b7SAlan Cox 		 * beneficial.  Determining which of these two cases applies
1496b5f359b7SAlan Cox 		 * would be more costly than unconditionally renaming the
1497b5f359b7SAlan Cox 		 * reservation.
1498b5f359b7SAlan Cox 		 */
1499b5f359b7SAlan Cox 		vm_reserv_rename(m, new_object, orig_object, offidxstart);
1500b5f359b7SAlan Cox #endif
1501dfd55c0cSAttilio Rao 		if (orig_object->type == OBJT_SWAP)
1502c7aebda8SAttilio Rao 			vm_page_xbusy(m);
1503c5aaa06dSAlan Cox 	}
1504d7a013c3SAlan Cox 	if (orig_object->type == OBJT_SWAP) {
1505c5aaa06dSAlan Cox 		/*
1506c7c8dd7eSAlan Cox 		 * swap_pager_copy() can sleep, in which case the orig_object's
1507c7c8dd7eSAlan Cox 		 * and new_object's locks are released and reacquired.
1508c5aaa06dSAlan Cox 		 */
1509c5aaa06dSAlan Cox 		swap_pager_copy(orig_object, new_object, offidxstart, 0);
1510dfd55c0cSAttilio Rao 		TAILQ_FOREACH(m, &new_object->memq, listq)
1511c7aebda8SAttilio Rao 			vm_page_xunbusy(m);
1512c5aaa06dSAlan Cox 	}
151389f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(orig_object);
151489f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(new_object);
1515c5aaa06dSAlan Cox 	entry->object.vm_object = new_object;
1516c5aaa06dSAlan Cox 	entry->offset = 0LL;
1517c5aaa06dSAlan Cox 	vm_object_deallocate(orig_object);
151889f6b863SAttilio Rao 	VM_OBJECT_WLOCK(new_object);
1519c5aaa06dSAlan Cox }
1520c5aaa06dSAlan Cox 
15212ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_NOWAIT	0x0002
15222ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_WAIT	0x0004
15232ad1a3f7SMatthew Dillon 
152499a1570aSKonstantin Belousov static vm_page_t
15254cc8daf7SConrad Meyer vm_object_collapse_scan_wait(vm_object_t object, vm_page_t p, vm_page_t next,
152699a1570aSKonstantin Belousov     int op)
152799a1570aSKonstantin Belousov {
152899a1570aSKonstantin Belousov 	vm_object_t backing_object;
152999a1570aSKonstantin Belousov 
153099a1570aSKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED(object);
153199a1570aSKonstantin Belousov 	backing_object = object->backing_object;
153299a1570aSKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED(backing_object);
153399a1570aSKonstantin Belousov 
153499a1570aSKonstantin Belousov 	KASSERT(p == NULL || vm_page_busied(p), ("unbusy page %p", p));
153599a1570aSKonstantin Belousov 	KASSERT(p == NULL || p->object == object || p->object == backing_object,
153699a1570aSKonstantin Belousov 	    ("invalid ownership %p %p %p", p, object, backing_object));
153799a1570aSKonstantin Belousov 	if ((op & OBSC_COLLAPSE_NOWAIT) != 0)
153899a1570aSKonstantin Belousov 		return (next);
153999a1570aSKonstantin Belousov 	if (p != NULL)
154099a1570aSKonstantin Belousov 		vm_page_lock(p);
154199a1570aSKonstantin Belousov 	VM_OBJECT_WUNLOCK(object);
154299a1570aSKonstantin Belousov 	VM_OBJECT_WUNLOCK(backing_object);
15438d6fbbb8SJeff Roberson 	/* The page is only NULL when rename fails. */
154499a1570aSKonstantin Belousov 	if (p == NULL)
15458d6fbbb8SJeff Roberson 		vm_radix_wait();
154699a1570aSKonstantin Belousov 	else
15475975e53dSKonstantin Belousov 		vm_page_busy_sleep(p, "vmocol", false);
154899a1570aSKonstantin Belousov 	VM_OBJECT_WLOCK(object);
154999a1570aSKonstantin Belousov 	VM_OBJECT_WLOCK(backing_object);
155099a1570aSKonstantin Belousov 	return (TAILQ_FIRST(&backing_object->memq));
155199a1570aSKonstantin Belousov }
155299a1570aSKonstantin Belousov 
155399a1570aSKonstantin Belousov static bool
15544cc8daf7SConrad Meyer vm_object_scan_all_shadowed(vm_object_t object)
15554cc8daf7SConrad Meyer {
15564cc8daf7SConrad Meyer 	vm_object_t backing_object;
15574cc8daf7SConrad Meyer 	vm_page_t p, pp;
155877d6fd97SKonstantin Belousov 	vm_pindex_t backing_offset_index, new_pindex, pi, ps;
15594cc8daf7SConrad Meyer 
15604cc8daf7SConrad Meyer 	VM_OBJECT_ASSERT_WLOCKED(object);
15614cc8daf7SConrad Meyer 	VM_OBJECT_ASSERT_WLOCKED(object->backing_object);
15624cc8daf7SConrad Meyer 
15634cc8daf7SConrad Meyer 	backing_object = object->backing_object;
15644cc8daf7SConrad Meyer 
156577d6fd97SKonstantin Belousov 	if (backing_object->type != OBJT_DEFAULT &&
156677d6fd97SKonstantin Belousov 	    backing_object->type != OBJT_SWAP)
15674cc8daf7SConrad Meyer 		return (false);
15684cc8daf7SConrad Meyer 
156977d6fd97SKonstantin Belousov 	pi = backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
157077d6fd97SKonstantin Belousov 	p = vm_page_find_least(backing_object, pi);
157177d6fd97SKonstantin Belousov 	ps = swap_pager_find_least(backing_object, pi);
15724cc8daf7SConrad Meyer 
15734cc8daf7SConrad Meyer 	/*
157477d6fd97SKonstantin Belousov 	 * Only check pages inside the parent object's range and
157577d6fd97SKonstantin Belousov 	 * inside the parent object's mapping of the backing object.
15764cc8daf7SConrad Meyer 	 */
157777d6fd97SKonstantin Belousov 	for (;; pi++) {
157877d6fd97SKonstantin Belousov 		if (p != NULL && p->pindex < pi)
157977d6fd97SKonstantin Belousov 			p = TAILQ_NEXT(p, listq);
158077d6fd97SKonstantin Belousov 		if (ps < pi)
158177d6fd97SKonstantin Belousov 			ps = swap_pager_find_least(backing_object, pi);
158277d6fd97SKonstantin Belousov 		if (p == NULL && ps >= backing_object->size)
158377d6fd97SKonstantin Belousov 			break;
158477d6fd97SKonstantin Belousov 		else if (p == NULL)
158577d6fd97SKonstantin Belousov 			pi = ps;
158677d6fd97SKonstantin Belousov 		else
158777d6fd97SKonstantin Belousov 			pi = MIN(p->pindex, ps);
158877d6fd97SKonstantin Belousov 
158977d6fd97SKonstantin Belousov 		new_pindex = pi - backing_offset_index;
159077d6fd97SKonstantin Belousov 		if (new_pindex >= object->size)
159177d6fd97SKonstantin Belousov 			break;
15924cc8daf7SConrad Meyer 
15934cc8daf7SConrad Meyer 		/*
15944cc8daf7SConrad Meyer 		 * See if the parent has the page or if the parent's object
15954cc8daf7SConrad Meyer 		 * pager has the page.  If the parent has the page but the page
15964cc8daf7SConrad Meyer 		 * is not valid, the parent's object pager must have the page.
15974cc8daf7SConrad Meyer 		 *
15984cc8daf7SConrad Meyer 		 * If this fails, the parent does not completely shadow the
15994cc8daf7SConrad Meyer 		 * object and we might as well give up now.
16004cc8daf7SConrad Meyer 		 */
16014cc8daf7SConrad Meyer 		pp = vm_page_lookup(object, new_pindex);
16024cc8daf7SConrad Meyer 		if ((pp == NULL || pp->valid == 0) &&
16034cc8daf7SConrad Meyer 		    !vm_pager_has_page(object, new_pindex, NULL, NULL))
16044cc8daf7SConrad Meyer 			return (false);
16054cc8daf7SConrad Meyer 	}
16064cc8daf7SConrad Meyer 	return (true);
16074cc8daf7SConrad Meyer }
16084cc8daf7SConrad Meyer 
16094cc8daf7SConrad Meyer static bool
16104cc8daf7SConrad Meyer vm_object_collapse_scan(vm_object_t object, int op)
16112ad1a3f7SMatthew Dillon {
16122ad1a3f7SMatthew Dillon 	vm_object_t backing_object;
161399a1570aSKonstantin Belousov 	vm_page_t next, p, pp;
161499a1570aSKonstantin Belousov 	vm_pindex_t backing_offset_index, new_pindex;
16152ad1a3f7SMatthew Dillon 
161689f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
161789f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object->backing_object);
16182ad1a3f7SMatthew Dillon 
16192ad1a3f7SMatthew Dillon 	backing_object = object->backing_object;
16202ad1a3f7SMatthew Dillon 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
16212ad1a3f7SMatthew Dillon 
16222ad1a3f7SMatthew Dillon 	/*
16232ad1a3f7SMatthew Dillon 	 * Initial conditions
16242ad1a3f7SMatthew Dillon 	 */
16254cc8daf7SConrad Meyer 	if ((op & OBSC_COLLAPSE_WAIT) != 0)
16262ad1a3f7SMatthew Dillon 		vm_object_set_flag(backing_object, OBJ_DEAD);
16272ad1a3f7SMatthew Dillon 
16282ad1a3f7SMatthew Dillon 	/*
16292ad1a3f7SMatthew Dillon 	 * Our scan
16302ad1a3f7SMatthew Dillon 	 */
16314cc8daf7SConrad Meyer 	for (p = TAILQ_FIRST(&backing_object->memq); p != NULL; p = next) {
163299a1570aSKonstantin Belousov 		next = TAILQ_NEXT(p, listq);
163399a1570aSKonstantin Belousov 		new_pindex = p->pindex - backing_offset_index;
16342ad1a3f7SMatthew Dillon 
16352ad1a3f7SMatthew Dillon 		/*
16362ad1a3f7SMatthew Dillon 		 * Check for busy page
16372ad1a3f7SMatthew Dillon 		 */
1638c7aebda8SAttilio Rao 		if (vm_page_busied(p)) {
16394cc8daf7SConrad Meyer 			next = vm_object_collapse_scan_wait(object, p, next, op);
16402ad1a3f7SMatthew Dillon 			continue;
16412ad1a3f7SMatthew Dillon 		}
16422ad1a3f7SMatthew Dillon 
164399a1570aSKonstantin Belousov 		KASSERT(p->object == backing_object,
16444cc8daf7SConrad Meyer 		    ("vm_object_collapse_scan: object mismatch"));
16452ad1a3f7SMatthew Dillon 
164699a1570aSKonstantin Belousov 		if (p->pindex < backing_offset_index ||
164799a1570aSKonstantin Belousov 		    new_pindex >= object->size) {
1648e946b949SAttilio Rao 			if (backing_object->type == OBJT_SWAP)
16494cc8daf7SConrad Meyer 				swap_pager_freespace(backing_object, p->pindex,
16504cc8daf7SConrad Meyer 				    1);
1651e946b949SAttilio Rao 
16522ad1a3f7SMatthew Dillon 			/*
16534cc8daf7SConrad Meyer 			 * Page is out of the parent object's range, we can
16544cc8daf7SConrad Meyer 			 * simply destroy it.
16552ad1a3f7SMatthew Dillon 			 */
16562965a453SKip Macy 			vm_page_lock(p);
1657f6d89838SAlan Cox 			KASSERT(!pmap_page_is_mapped(p),
1658f6d89838SAlan Cox 			    ("freeing mapped page %p", p));
1659f6d89838SAlan Cox 			if (p->wire_count == 0)
16602ad1a3f7SMatthew Dillon 				vm_page_free(p);
1661f6d89838SAlan Cox 			else
1662f6d89838SAlan Cox 				vm_page_remove(p);
16632965a453SKip Macy 			vm_page_unlock(p);
16642ad1a3f7SMatthew Dillon 			continue;
16652ad1a3f7SMatthew Dillon 		}
16662ad1a3f7SMatthew Dillon 
16672ad1a3f7SMatthew Dillon 		pp = vm_page_lookup(object, new_pindex);
166899a1570aSKonstantin Belousov 		if (pp != NULL && vm_page_busied(pp)) {
1669e18cc7bfSMax Laier 			/*
16704cc8daf7SConrad Meyer 			 * The page in the parent is busy and possibly not
16714cc8daf7SConrad Meyer 			 * (yet) valid.  Until its state is finalized by the
16724cc8daf7SConrad Meyer 			 * busy bit owner, we can't tell whether it shadows the
16734cc8daf7SConrad Meyer 			 * original page.  Therefore, we must either skip it
16744cc8daf7SConrad Meyer 			 * and the original (backing_object) page or wait for
16754cc8daf7SConrad Meyer 			 * its state to be finalized.
1676e18cc7bfSMax Laier 			 *
16774cc8daf7SConrad Meyer 			 * This is due to a race with vm_fault() where we must
16784cc8daf7SConrad Meyer 			 * unbusy the original (backing_obj) page before we can
16794cc8daf7SConrad Meyer 			 * (re)lock the parent.  Hence we can get here.
1680e18cc7bfSMax Laier 			 */
16814cc8daf7SConrad Meyer 			next = vm_object_collapse_scan_wait(object, pp, next,
16824cc8daf7SConrad Meyer 			    op);
1683e18cc7bfSMax Laier 			continue;
1684e18cc7bfSMax Laier 		}
168599a1570aSKonstantin Belousov 
168699a1570aSKonstantin Belousov 		KASSERT(pp == NULL || pp->valid != 0,
168799a1570aSKonstantin Belousov 		    ("unbusy invalid page %p", pp));
168899a1570aSKonstantin Belousov 
16894cc8daf7SConrad Meyer 		if (pp != NULL || vm_pager_has_page(object, new_pindex, NULL,
16904cc8daf7SConrad Meyer 			NULL)) {
169199a1570aSKonstantin Belousov 			/*
16924cc8daf7SConrad Meyer 			 * The page already exists in the parent OR swap exists
16934cc8daf7SConrad Meyer 			 * for this location in the parent.  Leave the parent's
16944cc8daf7SConrad Meyer 			 * page alone.  Destroy the original page from the
16954cc8daf7SConrad Meyer 			 * backing object.
169699a1570aSKonstantin Belousov 			 */
1697e946b949SAttilio Rao 			if (backing_object->type == OBJT_SWAP)
16984cc8daf7SConrad Meyer 				swap_pager_freespace(backing_object, p->pindex,
16994cc8daf7SConrad Meyer 				    1);
17002965a453SKip Macy 			vm_page_lock(p);
1701f6d89838SAlan Cox 			KASSERT(!pmap_page_is_mapped(p),
1702f6d89838SAlan Cox 			    ("freeing mapped page %p", p));
1703f6d89838SAlan Cox 			if (p->wire_count == 0)
17042ad1a3f7SMatthew Dillon 				vm_page_free(p);
1705f6d89838SAlan Cox 			else
1706f6d89838SAlan Cox 				vm_page_remove(p);
17072965a453SKip Macy 			vm_page_unlock(p);
17082ad1a3f7SMatthew Dillon 			continue;
17092ad1a3f7SMatthew Dillon 		}
17102ad1a3f7SMatthew Dillon 
1711e946b949SAttilio Rao 		/*
17124cc8daf7SConrad Meyer 		 * Page does not exist in parent, rename the page from the
17134cc8daf7SConrad Meyer 		 * backing object to the main object.
1714e946b949SAttilio Rao 		 *
17154cc8daf7SConrad Meyer 		 * If the page was mapped to a process, it can remain mapped
17163453bca8SAlan Cox 		 * through the rename.  vm_page_rename() will dirty the page.
1717e946b949SAttilio Rao 		 */
1718e946b949SAttilio Rao 		if (vm_page_rename(p, object, new_pindex)) {
17194cc8daf7SConrad Meyer 			next = vm_object_collapse_scan_wait(object, NULL, next,
17204cc8daf7SConrad Meyer 			    op);
1721e946b949SAttilio Rao 			continue;
1722e946b949SAttilio Rao 		}
172314a5dc17SAttilio Rao 
172414a5dc17SAttilio Rao 		/* Use the old pindex to free the right page. */
1725e946b949SAttilio Rao 		if (backing_object->type == OBJT_SWAP)
172614a5dc17SAttilio Rao 			swap_pager_freespace(backing_object,
172714a5dc17SAttilio Rao 			    new_pindex + backing_offset_index, 1);
1728e946b949SAttilio Rao 
1729f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
1730f8a47341SAlan Cox 		/*
1731f8a47341SAlan Cox 		 * Rename the reservation.
1732f8a47341SAlan Cox 		 */
1733f8a47341SAlan Cox 		vm_reserv_rename(p, object, backing_object,
1734f8a47341SAlan Cox 		    backing_offset_index);
1735f8a47341SAlan Cox #endif
17362ad1a3f7SMatthew Dillon 	}
173799a1570aSKonstantin Belousov 	return (true);
17382ad1a3f7SMatthew Dillon }
17392ad1a3f7SMatthew Dillon 
1740df8bae1dSRodney W. Grimes 
1741df8bae1dSRodney W. Grimes /*
17422fe6e4d7SDavid Greenman  * this version of collapse allows the operation to occur earlier and
17432fe6e4d7SDavid Greenman  * when paging_in_progress is true for an object...  This is not a complete
17442fe6e4d7SDavid Greenman  * operation, but should plug 99.9% of the rest of the leaks.
17452fe6e4d7SDavid Greenman  */
17462fe6e4d7SDavid Greenman static void
17471b40f8c0SMatthew Dillon vm_object_qcollapse(vm_object_t object)
17482fe6e4d7SDavid Greenman {
17492ad1a3f7SMatthew Dillon 	vm_object_t backing_object = object->backing_object;
17502fe6e4d7SDavid Greenman 
175189f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
175289f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(backing_object);
17531b40f8c0SMatthew Dillon 
17542fe6e4d7SDavid Greenman 	if (backing_object->ref_count != 1)
17552fe6e4d7SDavid Greenman 		return;
17562fe6e4d7SDavid Greenman 
17574cc8daf7SConrad Meyer 	vm_object_collapse_scan(object, OBSC_COLLAPSE_NOWAIT);
17582fe6e4d7SDavid Greenman }
17592fe6e4d7SDavid Greenman 
1760df8bae1dSRodney W. Grimes /*
1761df8bae1dSRodney W. Grimes  *	vm_object_collapse:
1762df8bae1dSRodney W. Grimes  *
1763df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
1764df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
1765df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
1766df8bae1dSRodney W. Grimes  */
176726f9a767SRodney W. Grimes void
17681b40f8c0SMatthew Dillon vm_object_collapse(vm_object_t object)
1769df8bae1dSRodney W. Grimes {
177098f139daSKonstantin Belousov 	vm_object_t backing_object, new_backing_object;
177198f139daSKonstantin Belousov 
177289f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
177323955314SAlfred Perlstein 
1774df8bae1dSRodney W. Grimes 	while (TRUE) {
1775df8bae1dSRodney W. Grimes 		/*
1776df8bae1dSRodney W. Grimes 		 * Verify that the conditions are right for collapse:
1777df8bae1dSRodney W. Grimes 		 *
17782ad1a3f7SMatthew Dillon 		 * The object exists and the backing object exists.
1779df8bae1dSRodney W. Grimes 		 */
178024a1cce3SDavid Greenman 		if ((backing_object = object->backing_object) == NULL)
17812ad1a3f7SMatthew Dillon 			break;
1782df8bae1dSRodney W. Grimes 
1783f919ebdeSDavid Greenman 		/*
1784f919ebdeSDavid Greenman 		 * we check the backing object first, because it is most likely
178524a1cce3SDavid Greenman 		 * not collapsable.
1786f919ebdeSDavid Greenman 		 */
178789f6b863SAttilio Rao 		VM_OBJECT_WLOCK(backing_object);
178824a1cce3SDavid Greenman 		if (backing_object->handle != NULL ||
178924a1cce3SDavid Greenman 		    (backing_object->type != OBJT_DEFAULT &&
179024a1cce3SDavid Greenman 		     backing_object->type != OBJT_SWAP) ||
1791f919ebdeSDavid Greenman 		    (backing_object->flags & OBJ_DEAD) ||
179224a1cce3SDavid Greenman 		    object->handle != NULL ||
179324a1cce3SDavid Greenman 		    (object->type != OBJT_DEFAULT &&
179424a1cce3SDavid Greenman 		     object->type != OBJT_SWAP) ||
179524a1cce3SDavid Greenman 		    (object->flags & OBJ_DEAD)) {
179689f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(backing_object);
17972ad1a3f7SMatthew Dillon 			break;
179824a1cce3SDavid Greenman 		}
17999b4814bbSDavid Greenman 
180098f139daSKonstantin Belousov 		if (object->paging_in_progress != 0 ||
180198f139daSKonstantin Belousov 		    backing_object->paging_in_progress != 0) {
1802b9921222SDavid Greenman 			vm_object_qcollapse(object);
180389f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(backing_object);
18042ad1a3f7SMatthew Dillon 			break;
1805df8bae1dSRodney W. Grimes 		}
180698f139daSKonstantin Belousov 
180726f9a767SRodney W. Grimes 		/*
18080d94caffSDavid Greenman 		 * We know that we can either collapse the backing object (if
18092ad1a3f7SMatthew Dillon 		 * the parent is the only reference to it) or (perhaps) have
18102ad1a3f7SMatthew Dillon 		 * the parent bypass the object if the parent happens to shadow
18112ad1a3f7SMatthew Dillon 		 * all the resident pages in the entire backing object.
18122ad1a3f7SMatthew Dillon 		 *
18132ad1a3f7SMatthew Dillon 		 * This is ignoring pager-backed pages such as swap pages.
18144cc8daf7SConrad Meyer 		 * vm_object_collapse_scan fails the shadowing test in this
18152ad1a3f7SMatthew Dillon 		 * case.
1816df8bae1dSRodney W. Grimes 		 */
1817df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
1818aa9bc3b1SKonstantin Belousov 			vm_object_pip_add(object, 1);
1819aa9bc3b1SKonstantin Belousov 			vm_object_pip_add(backing_object, 1);
1820aa9bc3b1SKonstantin Belousov 
1821df8bae1dSRodney W. Grimes 			/*
18222ad1a3f7SMatthew Dillon 			 * If there is exactly one reference to the backing
18232ad1a3f7SMatthew Dillon 			 * object, we can collapse it into the parent.
1824df8bae1dSRodney W. Grimes 			 */
18254cc8daf7SConrad Meyer 			vm_object_collapse_scan(object, OBSC_COLLAPSE_WAIT);
1826df8bae1dSRodney W. Grimes 
1827f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
1828f8a47341SAlan Cox 			/*
1829f8a47341SAlan Cox 			 * Break any reservations from backing_object.
1830f8a47341SAlan Cox 			 */
1831f8a47341SAlan Cox 			if (__predict_false(!LIST_EMPTY(&backing_object->rvq)))
1832f8a47341SAlan Cox 				vm_reserv_break_all(backing_object);
1833f8a47341SAlan Cox #endif
1834f8a47341SAlan Cox 
1835df8bae1dSRodney W. Grimes 			/*
1836df8bae1dSRodney W. Grimes 			 * Move the pager from backing_object to object.
1837df8bae1dSRodney W. Grimes 			 */
18386be36525SAlan Cox 			if (backing_object->type == OBJT_SWAP) {
183924a1cce3SDavid Greenman 				/*
1840c7c8dd7eSAlan Cox 				 * swap_pager_copy() can sleep, in which case
1841c7c8dd7eSAlan Cox 				 * the backing_object's and object's locks are
1842c7c8dd7eSAlan Cox 				 * released and reacquired.
1843571a1e92SAttilio Rao 				 * Since swap_pager_copy() is being asked to
1844571a1e92SAttilio Rao 				 * destroy the source, it will change the
1845571a1e92SAttilio Rao 				 * backing_object's type to OBJT_DEFAULT.
184624a1cce3SDavid Greenman 				 */
18471c7c3c6aSMatthew Dillon 				swap_pager_copy(
18481c7c3c6aSMatthew Dillon 				    backing_object,
18491c7c3c6aSMatthew Dillon 				    object,
18501c7c3c6aSMatthew Dillon 				    OFF_TO_IDX(object->backing_object_offset), TRUE);
1851c0503609SDavid Greenman 			}
1852df8bae1dSRodney W. Grimes 			/*
1853df8bae1dSRodney W. Grimes 			 * Object now shadows whatever backing_object did.
18542ad1a3f7SMatthew Dillon 			 * Note that the reference to
18552ad1a3f7SMatthew Dillon 			 * backing_object->backing_object moves from within
18562ad1a3f7SMatthew Dillon 			 * backing_object to within object.
1857df8bae1dSRodney W. Grimes 			 */
18581c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
18594f7c7f6eSAlan Cox 			backing_object->shadow_count--;
1860de5f6a77SJohn Dyson 			if (backing_object->backing_object) {
186189f6b863SAttilio Rao 				VM_OBJECT_WLOCK(backing_object->backing_object);
18621c500307SAlan Cox 				LIST_REMOVE(backing_object, shadow_list);
186343186e53SAlan Cox 				LIST_INSERT_HEAD(
186443186e53SAlan Cox 				    &backing_object->backing_object->shadow_head,
186543186e53SAlan Cox 				    object, shadow_list);
186643186e53SAlan Cox 				/*
186743186e53SAlan Cox 				 * The shadow_count has not changed.
186843186e53SAlan Cox 				 */
186989f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(backing_object->backing_object);
1870de5f6a77SJohn Dyson 			}
187124a1cce3SDavid Greenman 			object->backing_object = backing_object->backing_object;
18722ad1a3f7SMatthew Dillon 			object->backing_object_offset +=
18732ad1a3f7SMatthew Dillon 			    backing_object->backing_object_offset;
18742ad1a3f7SMatthew Dillon 
1875df8bae1dSRodney W. Grimes 			/*
1876df8bae1dSRodney W. Grimes 			 * Discard backing_object.
1877df8bae1dSRodney W. Grimes 			 *
18780d94caffSDavid Greenman 			 * Since the backing object has no pages, no pager left,
18790d94caffSDavid Greenman 			 * and no object references within it, all that is
18800d94caffSDavid Greenman 			 * necessary is to dispose of it.
1881df8bae1dSRodney W. Grimes 			 */
18829b4d473aSKonstantin Belousov 			KASSERT(backing_object->ref_count == 1, (
18839b4d473aSKonstantin Belousov "backing_object %p was somehow re-referenced during collapse!",
18849b4d473aSKonstantin Belousov 			    backing_object));
1885aa9bc3b1SKonstantin Belousov 			vm_object_pip_wakeup(backing_object);
1886e735691bSJohn Baldwin 			backing_object->type = OBJT_DEAD;
1887e735691bSJohn Baldwin 			backing_object->ref_count = 0;
188889f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(backing_object);
18899b4d473aSKonstantin Belousov 			vm_object_destroy(backing_object);
1890df8bae1dSRodney W. Grimes 
1891aa9bc3b1SKonstantin Belousov 			vm_object_pip_wakeup(object);
189211542376SAlan Cox 			counter_u64_add(object_collapses, 1);
18930d94caffSDavid Greenman 		} else {
1894df8bae1dSRodney W. Grimes 			/*
18952ad1a3f7SMatthew Dillon 			 * If we do not entirely shadow the backing object,
18962ad1a3f7SMatthew Dillon 			 * there is nothing we can do so we give up.
1897df8bae1dSRodney W. Grimes 			 */
1898df59a0feSJeff Roberson 			if (object->resident_page_count != object->size &&
18994cc8daf7SConrad Meyer 			    !vm_object_scan_all_shadowed(object)) {
190089f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(backing_object);
19012ad1a3f7SMatthew Dillon 				break;
190224a1cce3SDavid Greenman 			}
1903df8bae1dSRodney W. Grimes 
1904df8bae1dSRodney W. Grimes 			/*
19050d94caffSDavid Greenman 			 * Make the parent shadow the next object in the
19060d94caffSDavid Greenman 			 * chain.  Deallocating backing_object will not remove
19070d94caffSDavid Greenman 			 * it, since its reference count is at least 2.
1908df8bae1dSRodney W. Grimes 			 */
19091c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
1910eaf13dd7SJohn Dyson 			backing_object->shadow_count--;
191195e5e988SJohn Dyson 
191295e5e988SJohn Dyson 			new_backing_object = backing_object->backing_object;
19138aef1712SMatthew Dillon 			if ((object->backing_object = new_backing_object) != NULL) {
191489f6b863SAttilio Rao 				VM_OBJECT_WLOCK(new_backing_object);
19151c500307SAlan Cox 				LIST_INSERT_HEAD(
19162ad1a3f7SMatthew Dillon 				    &new_backing_object->shadow_head,
19172ad1a3f7SMatthew Dillon 				    object,
19182ad1a3f7SMatthew Dillon 				    shadow_list
19192ad1a3f7SMatthew Dillon 				);
1920eaf13dd7SJohn Dyson 				new_backing_object->shadow_count++;
1921b921a12bSAlan Cox 				vm_object_reference_locked(new_backing_object);
192289f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(new_backing_object);
192395e5e988SJohn Dyson 				object->backing_object_offset +=
192495e5e988SJohn Dyson 					backing_object->backing_object_offset;
1925de5f6a77SJohn Dyson 			}
1926df8bae1dSRodney W. Grimes 
1927df8bae1dSRodney W. Grimes 			/*
19280d94caffSDavid Greenman 			 * Drop the reference count on backing_object. Since
192922ec553fSAlan Cox 			 * its ref_count was at least 2, it will not vanish.
1930df8bae1dSRodney W. Grimes 			 */
193122ec553fSAlan Cox 			backing_object->ref_count--;
193289f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(backing_object);
193311542376SAlan Cox 			counter_u64_add(object_bypasses, 1);
1934df8bae1dSRodney W. Grimes 		}
1935df8bae1dSRodney W. Grimes 
1936df8bae1dSRodney W. Grimes 		/*
1937df8bae1dSRodney W. Grimes 		 * Try again with this object's new backing object.
1938df8bae1dSRodney W. Grimes 		 */
1939df8bae1dSRodney W. Grimes 	}
1940df8bae1dSRodney W. Grimes }
1941df8bae1dSRodney W. Grimes 
1942df8bae1dSRodney W. Grimes /*
1943bff99f0dSAlan Cox  *	vm_object_page_remove:
1944df8bae1dSRodney W. Grimes  *
194568855966SAlan Cox  *	For the given object, either frees or invalidates each of the
19466bbee8e2SAlan Cox  *	specified pages.  In general, a page is freed.  However, if a page is
19476bbee8e2SAlan Cox  *	wired for any reason other than the existence of a managed, wired
19486bbee8e2SAlan Cox  *	mapping, then it may be invalidated but not removed from the object.
19496bbee8e2SAlan Cox  *	Pages are specified by the given range ["start", "end") and the option
19506bbee8e2SAlan Cox  *	OBJPR_CLEANONLY.  As a special case, if "end" is zero, then the range
19516bbee8e2SAlan Cox  *	extends from "start" to the end of the object.  If the option
19526bbee8e2SAlan Cox  *	OBJPR_CLEANONLY is specified, then only the non-dirty pages within the
19536bbee8e2SAlan Cox  *	specified range are affected.  If the option OBJPR_NOTMAPPED is
19546bbee8e2SAlan Cox  *	specified, then the pages within the specified range must have no
19556bbee8e2SAlan Cox  *	mappings.  Otherwise, if this option is not specified, any mappings to
19566bbee8e2SAlan Cox  *	the specified pages are removed before the pages are freed or
19576bbee8e2SAlan Cox  *	invalidated.
195868855966SAlan Cox  *
19596bbee8e2SAlan Cox  *	In general, this operation should only be performed on objects that
19606bbee8e2SAlan Cox  *	contain managed pages.  There are, however, two exceptions.  First, it
19616bbee8e2SAlan Cox  *	is performed on the kernel and kmem objects by vm_map_entry_delete().
19626bbee8e2SAlan Cox  *	Second, it is used by msync(..., MS_INVALIDATE) to invalidate device-
19636bbee8e2SAlan Cox  *	backed pages.  In both of these cases, the option OBJPR_CLEANONLY must
19646bbee8e2SAlan Cox  *	not be specified and the option OBJPR_NOTMAPPED must be specified.
1965df8bae1dSRodney W. Grimes  *
1966df8bae1dSRodney W. Grimes  *	The object must be locked.
1967df8bae1dSRodney W. Grimes  */
196826f9a767SRodney W. Grimes void
1969ecde4b32SAlan Cox vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
19706bbee8e2SAlan Cox     int options)
1971df8bae1dSRodney W. Grimes {
1972d031cff1SMatthew Dillon 	vm_page_t p, next;
197393c5d3a4SKonstantin Belousov 	struct mtx *mtx;
1974bba52ecaSKonstantin Belousov 	struct pglist pgl;
1975df8bae1dSRodney W. Grimes 
197689f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
197728634820SAlan Cox 	KASSERT((object->flags & OBJ_UNMANAGED) == 0 ||
19786bbee8e2SAlan Cox 	    (options & (OBJPR_CLEANONLY | OBJPR_NOTMAPPED)) == OBJPR_NOTMAPPED,
19796bbee8e2SAlan Cox 	    ("vm_object_page_remove: illegal options for object %p", object));
1980ecde4b32SAlan Cox 	if (object->resident_page_count == 0)
19817667839aSAlan Cox 		return;
1982d474eaaaSDoug Rabson 	vm_object_pip_add(object, 1);
1983bba52ecaSKonstantin Belousov 	TAILQ_INIT(&pgl);
198426f9a767SRodney W. Grimes again:
1985b382c10aSKonstantin Belousov 	p = vm_page_find_least(object, start);
198693c5d3a4SKonstantin Belousov 	mtx = NULL;
19872965a453SKip Macy 
198875741c04SAlan Cox 	/*
19896bbee8e2SAlan Cox 	 * Here, the variable "p" is either (1) the page with the least pindex
19906bbee8e2SAlan Cox 	 * greater than or equal to the parameter "start" or (2) NULL.
199175741c04SAlan Cox 	 */
19926bbee8e2SAlan Cox 	for (; p != NULL && (p->pindex < end || end == 0); p = next) {
1993b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
199475741c04SAlan Cox 
199559677d3cSAlan Cox 		/*
19966bbee8e2SAlan Cox 		 * If the page is wired for any reason besides the existence
19976bbee8e2SAlan Cox 		 * of managed, wired mappings, then it cannot be freed.  For
19986bbee8e2SAlan Cox 		 * example, fictitious pages, which represent device memory,
19996bbee8e2SAlan Cox 		 * are inherently wired and cannot be freed.  They can,
20006bbee8e2SAlan Cox 		 * however, be invalidated if the option OBJPR_CLEANONLY is
20016bbee8e2SAlan Cox 		 * not specified.
200259677d3cSAlan Cox 		 */
200393c5d3a4SKonstantin Belousov 		vm_page_change_lock(p, &mtx);
20043aaea6efSKonstantin Belousov 		if (vm_page_xbusied(p)) {
20053aaea6efSKonstantin Belousov 			VM_OBJECT_WUNLOCK(object);
20065975e53dSKonstantin Belousov 			vm_page_busy_sleep(p, "vmopax", true);
20073aaea6efSKonstantin Belousov 			VM_OBJECT_WLOCK(object);
20083aaea6efSKonstantin Belousov 			goto again;
20093aaea6efSKonstantin Belousov 		}
20106195b24aSKonstantin Belousov 		if (p->wire_count != 0) {
2011cf060942SAlan Cox 			if ((options & OBJPR_NOTMAPPED) == 0 &&
2012cf060942SAlan Cox 			    object->ref_count != 0)
20134fec79beSAlan Cox 				pmap_remove_all(p);
20146bbee8e2SAlan Cox 			if ((options & OBJPR_CLEANONLY) == 0) {
2015bd7e5f99SJohn Dyson 				p->valid = 0;
2016a28042d1SAlan Cox 				vm_page_undirty(p);
2017a28042d1SAlan Cox 			}
201893c5d3a4SKonstantin Belousov 			continue;
20190d94caffSDavid Greenman 		}
2020c7aebda8SAttilio Rao 		if (vm_page_busied(p)) {
2021c7aebda8SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
20225975e53dSKonstantin Belousov 			vm_page_busy_sleep(p, "vmopar", false);
2023c7aebda8SAttilio Rao 			VM_OBJECT_WLOCK(object);
202426f9a767SRodney W. Grimes 			goto again;
2025c7aebda8SAttilio Rao 		}
202668855966SAlan Cox 		KASSERT((p->flags & PG_FICTITIOUS) == 0,
202768855966SAlan Cox 		    ("vm_object_page_remove: page %p is fictitious", p));
20286bbee8e2SAlan Cox 		if ((options & OBJPR_CLEANONLY) != 0 && p->valid != 0) {
2029cf060942SAlan Cox 			if ((options & OBJPR_NOTMAPPED) == 0 &&
2030cf060942SAlan Cox 			    object->ref_count != 0)
203178985e42SAlan Cox 				pmap_remove_write(p);
2032cf060942SAlan Cox 			if (p->dirty != 0)
203393c5d3a4SKonstantin Belousov 				continue;
20342965a453SKip Macy 		}
2035cf060942SAlan Cox 		if ((options & OBJPR_NOTMAPPED) == 0 && object->ref_count != 0)
20364fec79beSAlan Cox 			pmap_remove_all(p);
2037bba52ecaSKonstantin Belousov 		p->flags &= ~PG_ZERO;
2038bba52ecaSKonstantin Belousov 		if (vm_page_free_prep(p, false))
2039bba52ecaSKonstantin Belousov 			TAILQ_INSERT_TAIL(&pgl, p, listq);
20402965a453SKip Macy 	}
204193c5d3a4SKonstantin Belousov 	if (mtx != NULL)
204293c5d3a4SKonstantin Belousov 		mtx_unlock(mtx);
2043bba52ecaSKonstantin Belousov 	vm_page_free_phys_pglist(&pgl);
2044f919ebdeSDavid Greenman 	vm_object_pip_wakeup(object);
2045c0503609SDavid Greenman }
2046df8bae1dSRodney W. Grimes 
2047df8bae1dSRodney W. Grimes /*
20483138cd36SMark Johnston  *	vm_object_page_noreuse:
2049936c09acSJohn Baldwin  *
20503138cd36SMark Johnston  *	For the given object, attempt to move the specified pages to
20513138cd36SMark Johnston  *	the head of the inactive queue.  This bypasses regular LRU
20523138cd36SMark Johnston  *	operation and allows the pages to be reused quickly under memory
20533138cd36SMark Johnston  *	pressure.  If a page is wired for any reason, then it will not
20543138cd36SMark Johnston  *	be queued.  Pages are specified by the range ["start", "end").
20553138cd36SMark Johnston  *	As a special case, if "end" is zero, then the range extends from
20563138cd36SMark Johnston  *	"start" to the end of the object.
2057936c09acSJohn Baldwin  *
2058936c09acSJohn Baldwin  *	This operation should only be performed on objects that
205928634820SAlan Cox  *	contain non-fictitious, managed pages.
2060936c09acSJohn Baldwin  *
2061936c09acSJohn Baldwin  *	The object must be locked.
2062936c09acSJohn Baldwin  */
2063936c09acSJohn Baldwin void
20643138cd36SMark Johnston vm_object_page_noreuse(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
2065936c09acSJohn Baldwin {
206693c5d3a4SKonstantin Belousov 	struct mtx *mtx;
2067936c09acSJohn Baldwin 	vm_page_t p, next;
2068936c09acSJohn Baldwin 
206952d1addaSAlan Cox 	VM_OBJECT_ASSERT_LOCKED(object);
207028634820SAlan Cox 	KASSERT((object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0,
20713138cd36SMark Johnston 	    ("vm_object_page_noreuse: illegal object %p", object));
2072936c09acSJohn Baldwin 	if (object->resident_page_count == 0)
2073936c09acSJohn Baldwin 		return;
2074936c09acSJohn Baldwin 	p = vm_page_find_least(object, start);
2075936c09acSJohn Baldwin 
2076936c09acSJohn Baldwin 	/*
2077936c09acSJohn Baldwin 	 * Here, the variable "p" is either (1) the page with the least pindex
2078936c09acSJohn Baldwin 	 * greater than or equal to the parameter "start" or (2) NULL.
2079936c09acSJohn Baldwin 	 */
2080936c09acSJohn Baldwin 	mtx = NULL;
2081936c09acSJohn Baldwin 	for (; p != NULL && (p->pindex < end || end == 0); p = next) {
2082936c09acSJohn Baldwin 		next = TAILQ_NEXT(p, listq);
208393c5d3a4SKonstantin Belousov 		vm_page_change_lock(p, &mtx);
20843138cd36SMark Johnston 		vm_page_deactivate_noreuse(p);
2085936c09acSJohn Baldwin 	}
2086936c09acSJohn Baldwin 	if (mtx != NULL)
2087936c09acSJohn Baldwin 		mtx_unlock(mtx);
2088936c09acSJohn Baldwin }
2089936c09acSJohn Baldwin 
2090936c09acSJohn Baldwin /*
2091387aabc5SAlan Cox  *	Populate the specified range of the object with valid pages.  Returns
2092387aabc5SAlan Cox  *	TRUE if the range is successfully populated and FALSE otherwise.
2093387aabc5SAlan Cox  *
2094387aabc5SAlan Cox  *	Note: This function should be optimized to pass a larger array of
2095387aabc5SAlan Cox  *	pages to vm_pager_get_pages() before it is applied to a non-
2096387aabc5SAlan Cox  *	OBJT_DEVICE object.
2097387aabc5SAlan Cox  *
2098387aabc5SAlan Cox  *	The object must be locked.
2099387aabc5SAlan Cox  */
2100387aabc5SAlan Cox boolean_t
2101387aabc5SAlan Cox vm_object_populate(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
2102387aabc5SAlan Cox {
2103093c7f39SGleb Smirnoff 	vm_page_t m;
2104387aabc5SAlan Cox 	vm_pindex_t pindex;
2105387aabc5SAlan Cox 	int rv;
2106387aabc5SAlan Cox 
210789f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
2108387aabc5SAlan Cox 	for (pindex = start; pindex < end; pindex++) {
21095944de8eSKonstantin Belousov 		m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL);
2110387aabc5SAlan Cox 		if (m->valid != VM_PAGE_BITS_ALL) {
2111b0cd2017SGleb Smirnoff 			rv = vm_pager_get_pages(object, &m, 1, NULL, NULL);
2112387aabc5SAlan Cox 			if (rv != VM_PAGER_OK) {
21132965a453SKip Macy 				vm_page_lock(m);
2114387aabc5SAlan Cox 				vm_page_free(m);
21152965a453SKip Macy 				vm_page_unlock(m);
2116387aabc5SAlan Cox 				break;
2117387aabc5SAlan Cox 			}
2118387aabc5SAlan Cox 		}
2119387aabc5SAlan Cox 		/*
2120387aabc5SAlan Cox 		 * Keep "m" busy because a subsequent iteration may unlock
2121387aabc5SAlan Cox 		 * the object.
2122387aabc5SAlan Cox 		 */
2123387aabc5SAlan Cox 	}
2124387aabc5SAlan Cox 	if (pindex > start) {
2125387aabc5SAlan Cox 		m = vm_page_lookup(object, start);
2126387aabc5SAlan Cox 		while (m != NULL && m->pindex < pindex) {
2127c7aebda8SAttilio Rao 			vm_page_xunbusy(m);
2128387aabc5SAlan Cox 			m = TAILQ_NEXT(m, listq);
2129387aabc5SAlan Cox 		}
2130387aabc5SAlan Cox 	}
2131387aabc5SAlan Cox 	return (pindex == end);
2132387aabc5SAlan Cox }
2133387aabc5SAlan Cox 
2134387aabc5SAlan Cox /*
2135df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
2136df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
2137df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
2138df8bae1dSRodney W. Grimes  *
2139df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
2140df8bae1dSRodney W. Grimes  *
2141df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
2142df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
2143df8bae1dSRodney W. Grimes  *
2144df8bae1dSRodney W. Grimes  *	Parameters:
2145df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
2146df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
2147df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
214857a21abaSAlan Cox  *		next_size	Size of reference to the second object
21493364c323SKonstantin Belousov  *		reserved	Indicator that extension region has
21503364c323SKonstantin Belousov  *				swap accounted for
2151df8bae1dSRodney W. Grimes  *
2152df8bae1dSRodney W. Grimes  *	Conditions:
2153df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
2154df8bae1dSRodney W. Grimes  */
21550d94caffSDavid Greenman boolean_t
215657a21abaSAlan Cox vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset,
21573364c323SKonstantin Belousov     vm_size_t prev_size, vm_size_t next_size, boolean_t reserved)
2158df8bae1dSRodney W. Grimes {
2159ea41812fSAlan Cox 	vm_pindex_t next_pindex;
2160df8bae1dSRodney W. Grimes 
216100e1854aSAlan Cox 	if (prev_object == NULL)
2162df8bae1dSRodney W. Grimes 		return (TRUE);
216389f6b863SAttilio Rao 	VM_OBJECT_WLOCK(prev_object);
21649ded9474SKonstantin Belousov 	if ((prev_object->type != OBJT_DEFAULT &&
21659ded9474SKonstantin Belousov 	    prev_object->type != OBJT_SWAP) ||
2166f08f7dcaSKonstantin Belousov 	    (prev_object->flags & OBJ_TMPFS_NODE) != 0) {
216789f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(prev_object);
216830dcfc09SJohn Dyson 		return (FALSE);
216930dcfc09SJohn Dyson 	}
217030dcfc09SJohn Dyson 
2171df8bae1dSRodney W. Grimes 	/*
2172df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
2173df8bae1dSRodney W. Grimes 	 */
2174df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
2175df8bae1dSRodney W. Grimes 
2176df8bae1dSRodney W. Grimes 	/*
21770d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
21780d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
21790d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
2180df8bae1dSRodney W. Grimes 	 */
21818cc7e047SJohn Dyson 	if (prev_object->backing_object != NULL) {
218289f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(prev_object);
2183df8bae1dSRodney W. Grimes 		return (FALSE);
2184df8bae1dSRodney W. Grimes 	}
2185a316d390SJohn Dyson 
2186a316d390SJohn Dyson 	prev_size >>= PAGE_SHIFT;
2187a316d390SJohn Dyson 	next_size >>= PAGE_SHIFT;
218857a21abaSAlan Cox 	next_pindex = OFF_TO_IDX(prev_offset) + prev_size;
21898cc7e047SJohn Dyson 
21908cc7e047SJohn Dyson 	if ((prev_object->ref_count > 1) &&
2191ea41812fSAlan Cox 	    (prev_object->size != next_pindex)) {
219289f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(prev_object);
21938cc7e047SJohn Dyson 		return (FALSE);
21948cc7e047SJohn Dyson 	}
21958cc7e047SJohn Dyson 
2196df8bae1dSRodney W. Grimes 	/*
21973364c323SKonstantin Belousov 	 * Account for the charge.
21983364c323SKonstantin Belousov 	 */
2199ef694c1aSEdward Tomasz Napierala 	if (prev_object->cred != NULL) {
22003364c323SKonstantin Belousov 
22013364c323SKonstantin Belousov 		/*
22023364c323SKonstantin Belousov 		 * If prev_object was charged, then this mapping,
2203763df3ecSPedro F. Giffuni 		 * although not charged now, may become writable
2204ef694c1aSEdward Tomasz Napierala 		 * later. Non-NULL cred in the object would prevent
22053364c323SKonstantin Belousov 		 * swap reservation during enabling of the write
22063364c323SKonstantin Belousov 		 * access, so reserve swap now. Failed reservation
22073364c323SKonstantin Belousov 		 * cause allocation of the separate object for the map
22083364c323SKonstantin Belousov 		 * entry, and swap reservation for this entry is
22093364c323SKonstantin Belousov 		 * managed in appropriate time.
22103364c323SKonstantin Belousov 		 */
2211ef694c1aSEdward Tomasz Napierala 		if (!reserved && !swap_reserve_by_cred(ptoa(next_size),
2212ef694c1aSEdward Tomasz Napierala 		    prev_object->cred)) {
22139f790a17SKonstantin Belousov 			VM_OBJECT_WUNLOCK(prev_object);
22143364c323SKonstantin Belousov 			return (FALSE);
22153364c323SKonstantin Belousov 		}
22163364c323SKonstantin Belousov 		prev_object->charge += ptoa(next_size);
22173364c323SKonstantin Belousov 	}
22183364c323SKonstantin Belousov 
22193364c323SKonstantin Belousov 	/*
22200d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
22210d94caffSDavid Greenman 	 * deallocation.
2222df8bae1dSRodney W. Grimes 	 */
2223ea41812fSAlan Cox 	if (next_pindex < prev_object->size) {
22246bbee8e2SAlan Cox 		vm_object_page_remove(prev_object, next_pindex, next_pindex +
22256bbee8e2SAlan Cox 		    next_size, 0);
2226ea41812fSAlan Cox 		if (prev_object->type == OBJT_SWAP)
2227ea41812fSAlan Cox 			swap_pager_freespace(prev_object,
2228ea41812fSAlan Cox 					     next_pindex, next_size);
22293364c323SKonstantin Belousov #if 0
2230ef694c1aSEdward Tomasz Napierala 		if (prev_object->cred != NULL) {
22313364c323SKonstantin Belousov 			KASSERT(prev_object->charge >=
22323364c323SKonstantin Belousov 			    ptoa(prev_object->size - next_pindex),
22333364c323SKonstantin Belousov 			    ("object %p overcharged 1 %jx %jx", prev_object,
22343364c323SKonstantin Belousov 				(uintmax_t)next_pindex, (uintmax_t)next_size));
22353364c323SKonstantin Belousov 			prev_object->charge -= ptoa(prev_object->size -
22363364c323SKonstantin Belousov 			    next_pindex);
22373364c323SKonstantin Belousov 		}
22383364c323SKonstantin Belousov #endif
2239ea41812fSAlan Cox 	}
2240df8bae1dSRodney W. Grimes 
2241df8bae1dSRodney W. Grimes 	/*
2242df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
2243df8bae1dSRodney W. Grimes 	 */
2244ea41812fSAlan Cox 	if (next_pindex + next_size > prev_object->size)
2245ea41812fSAlan Cox 		prev_object->size = next_pindex + next_size;
2246df8bae1dSRodney W. Grimes 
224789f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(prev_object);
2248df8bae1dSRodney W. Grimes 	return (TRUE);
2249df8bae1dSRodney W. Grimes }
2250df8bae1dSRodney W. Grimes 
22517a5a6352SMatthew Dillon void
22527a5a6352SMatthew Dillon vm_object_set_writeable_dirty(vm_object_t object)
22537a5a6352SMatthew Dillon {
22547a5a6352SMatthew Dillon 
225589f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
2256f40cb1c6SKonstantin Belousov 	if (object->type != OBJT_VNODE) {
2257f40cb1c6SKonstantin Belousov 		if ((object->flags & OBJ_TMPFS_NODE) != 0) {
2258f40cb1c6SKonstantin Belousov 			KASSERT(object->type == OBJT_SWAP, ("non-swap tmpfs"));
2259f40cb1c6SKonstantin Belousov 			vm_object_set_flag(object, OBJ_TMPFS_DIRTY);
2260f40cb1c6SKonstantin Belousov 		}
22613280870dSKonstantin Belousov 		return;
2262f40cb1c6SKonstantin Belousov 	}
22633280870dSKonstantin Belousov 	object->generation++;
22643280870dSKonstantin Belousov 	if ((object->flags & OBJ_MIGHTBEDIRTY) != 0)
2265ee39666aSJeff Roberson 		return;
2266af51d7bfSAlan Cox 	vm_object_set_flag(object, OBJ_MIGHTBEDIRTY);
22677a5a6352SMatthew Dillon }
22687a5a6352SMatthew Dillon 
226903462509SAlan Cox /*
227003462509SAlan Cox  *	vm_object_unwire:
227103462509SAlan Cox  *
227203462509SAlan Cox  *	For each page offset within the specified range of the given object,
227303462509SAlan Cox  *	find the highest-level page in the shadow chain and unwire it.  A page
227403462509SAlan Cox  *	must exist at every page offset, and the highest-level page must be
227503462509SAlan Cox  *	wired.
227603462509SAlan Cox  */
227703462509SAlan Cox void
227803462509SAlan Cox vm_object_unwire(vm_object_t object, vm_ooffset_t offset, vm_size_t length,
227903462509SAlan Cox     uint8_t queue)
228003462509SAlan Cox {
228103462509SAlan Cox 	vm_object_t tobject;
228203462509SAlan Cox 	vm_page_t m, tm;
228303462509SAlan Cox 	vm_pindex_t end_pindex, pindex, tpindex;
228403462509SAlan Cox 	int depth, locked_depth;
228503462509SAlan Cox 
228603462509SAlan Cox 	KASSERT((offset & PAGE_MASK) == 0,
228703462509SAlan Cox 	    ("vm_object_unwire: offset is not page aligned"));
228803462509SAlan Cox 	KASSERT((length & PAGE_MASK) == 0,
228903462509SAlan Cox 	    ("vm_object_unwire: length is not a multiple of PAGE_SIZE"));
229003462509SAlan Cox 	/* The wired count of a fictitious page never changes. */
229103462509SAlan Cox 	if ((object->flags & OBJ_FICTITIOUS) != 0)
229203462509SAlan Cox 		return;
229303462509SAlan Cox 	pindex = OFF_TO_IDX(offset);
229403462509SAlan Cox 	end_pindex = pindex + atop(length);
229503462509SAlan Cox 	locked_depth = 1;
229603462509SAlan Cox 	VM_OBJECT_RLOCK(object);
229703462509SAlan Cox 	m = vm_page_find_least(object, pindex);
229803462509SAlan Cox 	while (pindex < end_pindex) {
229903462509SAlan Cox 		if (m == NULL || pindex < m->pindex) {
230003462509SAlan Cox 			/*
230103462509SAlan Cox 			 * The first object in the shadow chain doesn't
230203462509SAlan Cox 			 * contain a page at the current index.  Therefore,
230303462509SAlan Cox 			 * the page must exist in a backing object.
230403462509SAlan Cox 			 */
230503462509SAlan Cox 			tobject = object;
230603462509SAlan Cox 			tpindex = pindex;
230703462509SAlan Cox 			depth = 0;
230803462509SAlan Cox 			do {
230903462509SAlan Cox 				tpindex +=
231003462509SAlan Cox 				    OFF_TO_IDX(tobject->backing_object_offset);
231103462509SAlan Cox 				tobject = tobject->backing_object;
231203462509SAlan Cox 				KASSERT(tobject != NULL,
231303462509SAlan Cox 				    ("vm_object_unwire: missing page"));
231403462509SAlan Cox 				if ((tobject->flags & OBJ_FICTITIOUS) != 0)
231503462509SAlan Cox 					goto next_page;
231603462509SAlan Cox 				depth++;
231703462509SAlan Cox 				if (depth == locked_depth) {
231803462509SAlan Cox 					locked_depth++;
231903462509SAlan Cox 					VM_OBJECT_RLOCK(tobject);
232003462509SAlan Cox 				}
232103462509SAlan Cox 			} while ((tm = vm_page_lookup(tobject, tpindex)) ==
232203462509SAlan Cox 			    NULL);
232303462509SAlan Cox 		} else {
232403462509SAlan Cox 			tm = m;
232503462509SAlan Cox 			m = TAILQ_NEXT(m, listq);
232603462509SAlan Cox 		}
232703462509SAlan Cox 		vm_page_lock(tm);
232803462509SAlan Cox 		vm_page_unwire(tm, queue);
232903462509SAlan Cox 		vm_page_unlock(tm);
233003462509SAlan Cox next_page:
233103462509SAlan Cox 		pindex++;
233203462509SAlan Cox 	}
233303462509SAlan Cox 	/* Release the accumulated object locks. */
233403462509SAlan Cox 	for (depth = 0; depth < locked_depth; depth++) {
233503462509SAlan Cox 		tobject = object->backing_object;
233603462509SAlan Cox 		VM_OBJECT_RUNLOCK(object);
233703462509SAlan Cox 		object = tobject;
233803462509SAlan Cox 	}
233903462509SAlan Cox }
234003462509SAlan Cox 
234163e4c6cdSEric van Gyzen struct vnode *
234263e4c6cdSEric van Gyzen vm_object_vnode(vm_object_t object)
234363e4c6cdSEric van Gyzen {
234463e4c6cdSEric van Gyzen 
234563e4c6cdSEric van Gyzen 	VM_OBJECT_ASSERT_LOCKED(object);
234663e4c6cdSEric van Gyzen 	if (object->type == OBJT_VNODE)
234763e4c6cdSEric van Gyzen 		return (object->handle);
234863e4c6cdSEric van Gyzen 	if (object->type == OBJT_SWAP && (object->flags & OBJ_TMPFS) != 0)
234963e4c6cdSEric van Gyzen 		return (object->un_pager.swp.swp_tmpfs);
235063e4c6cdSEric van Gyzen 	return (NULL);
235163e4c6cdSEric van Gyzen }
235263e4c6cdSEric van Gyzen 
2353ff87ae35SJohn Baldwin static int
2354ff87ae35SJohn Baldwin sysctl_vm_object_list(SYSCTL_HANDLER_ARGS)
2355ff87ae35SJohn Baldwin {
23560ecee546SKonstantin Belousov 	struct kinfo_vmobject *kvo;
2357ff87ae35SJohn Baldwin 	char *fullpath, *freepath;
2358ff87ae35SJohn Baldwin 	struct vnode *vp;
2359ff87ae35SJohn Baldwin 	struct vattr va;
2360ff87ae35SJohn Baldwin 	vm_object_t obj;
2361ff87ae35SJohn Baldwin 	vm_page_t m;
2362ff87ae35SJohn Baldwin 	int count, error;
2363ff87ae35SJohn Baldwin 
2364ff87ae35SJohn Baldwin 	if (req->oldptr == NULL) {
2365ff87ae35SJohn Baldwin 		/*
2366ff87ae35SJohn Baldwin 		 * If an old buffer has not been provided, generate an
2367ff87ae35SJohn Baldwin 		 * estimate of the space needed for a subsequent call.
2368ff87ae35SJohn Baldwin 		 */
2369ff87ae35SJohn Baldwin 		mtx_lock(&vm_object_list_mtx);
2370ff87ae35SJohn Baldwin 		count = 0;
2371ff87ae35SJohn Baldwin 		TAILQ_FOREACH(obj, &vm_object_list, object_list) {
2372ff87ae35SJohn Baldwin 			if (obj->type == OBJT_DEAD)
2373ff87ae35SJohn Baldwin 				continue;
2374ff87ae35SJohn Baldwin 			count++;
2375ff87ae35SJohn Baldwin 		}
2376ff87ae35SJohn Baldwin 		mtx_unlock(&vm_object_list_mtx);
2377ff87ae35SJohn Baldwin 		return (SYSCTL_OUT(req, NULL, sizeof(struct kinfo_vmobject) *
2378ff87ae35SJohn Baldwin 		    count * 11 / 10));
2379ff87ae35SJohn Baldwin 	}
2380ff87ae35SJohn Baldwin 
23810ecee546SKonstantin Belousov 	kvo = malloc(sizeof(*kvo), M_TEMP, M_WAITOK);
2382ff87ae35SJohn Baldwin 	error = 0;
2383ff87ae35SJohn Baldwin 
2384ff87ae35SJohn Baldwin 	/*
2385ff87ae35SJohn Baldwin 	 * VM objects are type stable and are never removed from the
2386ff87ae35SJohn Baldwin 	 * list once added.  This allows us to safely read obj->object_list
2387ff87ae35SJohn Baldwin 	 * after reacquiring the VM object lock.
2388ff87ae35SJohn Baldwin 	 */
2389ff87ae35SJohn Baldwin 	mtx_lock(&vm_object_list_mtx);
2390ff87ae35SJohn Baldwin 	TAILQ_FOREACH(obj, &vm_object_list, object_list) {
2391ff87ae35SJohn Baldwin 		if (obj->type == OBJT_DEAD)
2392ff87ae35SJohn Baldwin 			continue;
2393ff87ae35SJohn Baldwin 		VM_OBJECT_RLOCK(obj);
2394ff87ae35SJohn Baldwin 		if (obj->type == OBJT_DEAD) {
2395ff87ae35SJohn Baldwin 			VM_OBJECT_RUNLOCK(obj);
2396ff87ae35SJohn Baldwin 			continue;
2397ff87ae35SJohn Baldwin 		}
2398ff87ae35SJohn Baldwin 		mtx_unlock(&vm_object_list_mtx);
23990ecee546SKonstantin Belousov 		kvo->kvo_size = ptoa(obj->size);
24000ecee546SKonstantin Belousov 		kvo->kvo_resident = obj->resident_page_count;
24010ecee546SKonstantin Belousov 		kvo->kvo_ref_count = obj->ref_count;
24020ecee546SKonstantin Belousov 		kvo->kvo_shadow_count = obj->shadow_count;
24030ecee546SKonstantin Belousov 		kvo->kvo_memattr = obj->memattr;
24040ecee546SKonstantin Belousov 		kvo->kvo_active = 0;
24050ecee546SKonstantin Belousov 		kvo->kvo_inactive = 0;
2406ff87ae35SJohn Baldwin 		TAILQ_FOREACH(m, &obj->memq, listq) {
2407ff87ae35SJohn Baldwin 			/*
2408ff87ae35SJohn Baldwin 			 * A page may belong to the object but be
2409ff87ae35SJohn Baldwin 			 * dequeued and set to PQ_NONE while the
2410ff87ae35SJohn Baldwin 			 * object lock is not held.  This makes the
2411ff87ae35SJohn Baldwin 			 * reads of m->queue below racy, and we do not
2412ff87ae35SJohn Baldwin 			 * count pages set to PQ_NONE.  However, this
2413ff87ae35SJohn Baldwin 			 * sysctl is only meant to give an
2414ff87ae35SJohn Baldwin 			 * approximation of the system anyway.
2415ff87ae35SJohn Baldwin 			 */
2416ebcddc72SAlan Cox 			if (vm_page_active(m))
24170ecee546SKonstantin Belousov 				kvo->kvo_active++;
2418ebcddc72SAlan Cox 			else if (vm_page_inactive(m))
24190ecee546SKonstantin Belousov 				kvo->kvo_inactive++;
2420ff87ae35SJohn Baldwin 		}
2421ff87ae35SJohn Baldwin 
24220ecee546SKonstantin Belousov 		kvo->kvo_vn_fileid = 0;
24230ecee546SKonstantin Belousov 		kvo->kvo_vn_fsid = 0;
24240ecee546SKonstantin Belousov 		kvo->kvo_vn_fsid_freebsd11 = 0;
2425ff87ae35SJohn Baldwin 		freepath = NULL;
2426ff87ae35SJohn Baldwin 		fullpath = "";
2427ff87ae35SJohn Baldwin 		vp = NULL;
2428ff87ae35SJohn Baldwin 		switch (obj->type) {
2429ff87ae35SJohn Baldwin 		case OBJT_DEFAULT:
24300ecee546SKonstantin Belousov 			kvo->kvo_type = KVME_TYPE_DEFAULT;
2431ff87ae35SJohn Baldwin 			break;
2432ff87ae35SJohn Baldwin 		case OBJT_VNODE:
24330ecee546SKonstantin Belousov 			kvo->kvo_type = KVME_TYPE_VNODE;
2434ff87ae35SJohn Baldwin 			vp = obj->handle;
2435ff87ae35SJohn Baldwin 			vref(vp);
2436ff87ae35SJohn Baldwin 			break;
2437ff87ae35SJohn Baldwin 		case OBJT_SWAP:
24380ecee546SKonstantin Belousov 			kvo->kvo_type = KVME_TYPE_SWAP;
2439ff87ae35SJohn Baldwin 			break;
2440ff87ae35SJohn Baldwin 		case OBJT_DEVICE:
24410ecee546SKonstantin Belousov 			kvo->kvo_type = KVME_TYPE_DEVICE;
2442ff87ae35SJohn Baldwin 			break;
2443ff87ae35SJohn Baldwin 		case OBJT_PHYS:
24440ecee546SKonstantin Belousov 			kvo->kvo_type = KVME_TYPE_PHYS;
2445ff87ae35SJohn Baldwin 			break;
2446ff87ae35SJohn Baldwin 		case OBJT_DEAD:
24470ecee546SKonstantin Belousov 			kvo->kvo_type = KVME_TYPE_DEAD;
2448ff87ae35SJohn Baldwin 			break;
2449ff87ae35SJohn Baldwin 		case OBJT_SG:
24500ecee546SKonstantin Belousov 			kvo->kvo_type = KVME_TYPE_SG;
2451ff87ae35SJohn Baldwin 			break;
2452ff87ae35SJohn Baldwin 		case OBJT_MGTDEVICE:
24530ecee546SKonstantin Belousov 			kvo->kvo_type = KVME_TYPE_MGTDEVICE;
2454ff87ae35SJohn Baldwin 			break;
2455ff87ae35SJohn Baldwin 		default:
24560ecee546SKonstantin Belousov 			kvo->kvo_type = KVME_TYPE_UNKNOWN;
2457ff87ae35SJohn Baldwin 			break;
2458ff87ae35SJohn Baldwin 		}
2459ff87ae35SJohn Baldwin 		VM_OBJECT_RUNLOCK(obj);
2460ff87ae35SJohn Baldwin 		if (vp != NULL) {
2461ff87ae35SJohn Baldwin 			vn_fullpath(curthread, vp, &fullpath, &freepath);
2462ff87ae35SJohn Baldwin 			vn_lock(vp, LK_SHARED | LK_RETRY);
2463ff87ae35SJohn Baldwin 			if (VOP_GETATTR(vp, &va, curthread->td_ucred) == 0) {
24640ecee546SKonstantin Belousov 				kvo->kvo_vn_fileid = va.va_fileid;
24650ecee546SKonstantin Belousov 				kvo->kvo_vn_fsid = va.va_fsid;
24660ecee546SKonstantin Belousov 				kvo->kvo_vn_fsid_freebsd11 = va.va_fsid;
246769921123SKonstantin Belousov 								/* truncate */
2468ff87ae35SJohn Baldwin 			}
2469ff87ae35SJohn Baldwin 			vput(vp);
2470ff87ae35SJohn Baldwin 		}
2471ff87ae35SJohn Baldwin 
24720ecee546SKonstantin Belousov 		strlcpy(kvo->kvo_path, fullpath, sizeof(kvo->kvo_path));
2473ff87ae35SJohn Baldwin 		if (freepath != NULL)
2474ff87ae35SJohn Baldwin 			free(freepath, M_TEMP);
2475ff87ae35SJohn Baldwin 
2476ff87ae35SJohn Baldwin 		/* Pack record size down */
24770ecee546SKonstantin Belousov 		kvo->kvo_structsize = offsetof(struct kinfo_vmobject, kvo_path)
24780ecee546SKonstantin Belousov 		    + strlen(kvo->kvo_path) + 1;
24790ecee546SKonstantin Belousov 		kvo->kvo_structsize = roundup(kvo->kvo_structsize,
2480ff87ae35SJohn Baldwin 		    sizeof(uint64_t));
24810ecee546SKonstantin Belousov 		error = SYSCTL_OUT(req, kvo, kvo->kvo_structsize);
2482ff87ae35SJohn Baldwin 		mtx_lock(&vm_object_list_mtx);
2483ff87ae35SJohn Baldwin 		if (error)
2484ff87ae35SJohn Baldwin 			break;
2485ff87ae35SJohn Baldwin 	}
2486ff87ae35SJohn Baldwin 	mtx_unlock(&vm_object_list_mtx);
24870ecee546SKonstantin Belousov 	free(kvo, M_TEMP);
2488ff87ae35SJohn Baldwin 	return (error);
2489ff87ae35SJohn Baldwin }
2490ff87ae35SJohn Baldwin SYSCTL_PROC(_vm, OID_AUTO, objects, CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_SKIP |
2491ff87ae35SJohn Baldwin     CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_object_list, "S,kinfo_vmobject",
2492ff87ae35SJohn Baldwin     "List of VM objects");
2493ff87ae35SJohn Baldwin 
2494c7c34a24SBruce Evans #include "opt_ddb.h"
2495c3cb3e12SDavid Greenman #ifdef DDB
2496c7c34a24SBruce Evans #include <sys/kernel.h>
2497c7c34a24SBruce Evans 
2498ce9edcf5SPoul-Henning Kamp #include <sys/cons.h>
2499c7c34a24SBruce Evans 
2500c7c34a24SBruce Evans #include <ddb/ddb.h>
2501c7c34a24SBruce Evans 
2502cac597e4SBruce Evans static int
25031b40f8c0SMatthew Dillon _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
2504a1f6d91cSDavid Greenman {
2505a1f6d91cSDavid Greenman 	vm_map_t tmpm;
2506a1f6d91cSDavid Greenman 	vm_map_entry_t tmpe;
2507a1f6d91cSDavid Greenman 	vm_object_t obj;
2508a1f6d91cSDavid Greenman 	int entcount;
2509a1f6d91cSDavid Greenman 
2510a1f6d91cSDavid Greenman 	if (map == 0)
2511a1f6d91cSDavid Greenman 		return 0;
2512a1f6d91cSDavid Greenman 
2513a1f6d91cSDavid Greenman 	if (entry == 0) {
2514a1f6d91cSDavid Greenman 		tmpe = map->header.next;
2515a1f6d91cSDavid Greenman 		entcount = map->nentries;
2516a1f6d91cSDavid Greenman 		while (entcount-- && (tmpe != &map->header)) {
2517a1f6d91cSDavid Greenman 			if (_vm_object_in_map(map, object, tmpe)) {
2518a1f6d91cSDavid Greenman 				return 1;
2519a1f6d91cSDavid Greenman 			}
2520a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
2521a1f6d91cSDavid Greenman 		}
25229fdfe602SMatthew Dillon 	} else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
25239fdfe602SMatthew Dillon 		tmpm = entry->object.sub_map;
2524a1f6d91cSDavid Greenman 		tmpe = tmpm->header.next;
2525a1f6d91cSDavid Greenman 		entcount = tmpm->nentries;
2526a1f6d91cSDavid Greenman 		while (entcount-- && tmpe != &tmpm->header) {
2527a1f6d91cSDavid Greenman 			if (_vm_object_in_map(tmpm, object, tmpe)) {
2528a1f6d91cSDavid Greenman 				return 1;
2529a1f6d91cSDavid Greenman 			}
2530a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
2531a1f6d91cSDavid Greenman 		}
25328aef1712SMatthew Dillon 	} else if ((obj = entry->object.vm_object) != NULL) {
253324a1cce3SDavid Greenman 		for (; obj; obj = obj->backing_object)
2534a1f6d91cSDavid Greenman 			if (obj == object) {
2535a1f6d91cSDavid Greenman 				return 1;
2536a1f6d91cSDavid Greenman 			}
2537a1f6d91cSDavid Greenman 	}
2538a1f6d91cSDavid Greenman 	return 0;
2539a1f6d91cSDavid Greenman }
2540a1f6d91cSDavid Greenman 
2541cac597e4SBruce Evans static int
25421b40f8c0SMatthew Dillon vm_object_in_map(vm_object_t object)
2543a1f6d91cSDavid Greenman {
2544a1f6d91cSDavid Greenman 	struct proc *p;
25451005a129SJohn Baldwin 
254660517fd1SJohn Baldwin 	/* sx_slock(&allproc_lock); */
2547f67af5c9SXin LI 	FOREACH_PROC_IN_SYSTEM(p) {
2548a1f6d91cSDavid Greenman 		if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
2549a1f6d91cSDavid Greenman 			continue;
2550553629ebSJake Burkholder 		if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
255160517fd1SJohn Baldwin 			/* sx_sunlock(&allproc_lock); */
2552a1f6d91cSDavid Greenman 			return 1;
2553a1f6d91cSDavid Greenman 		}
2554553629ebSJake Burkholder 	}
255560517fd1SJohn Baldwin 	/* sx_sunlock(&allproc_lock); */
2556a1f6d91cSDavid Greenman 	if (_vm_object_in_map(kernel_map, object, 0))
2557a1f6d91cSDavid Greenman 		return 1;
2558a1f6d91cSDavid Greenman 	return 0;
2559a1f6d91cSDavid Greenman }
2560a1f6d91cSDavid Greenman 
2561c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check)
2562f708ef1bSPoul-Henning Kamp {
2563a1f6d91cSDavid Greenman 	vm_object_t object;
2564a1f6d91cSDavid Greenman 
2565a1f6d91cSDavid Greenman 	/*
2566a1f6d91cSDavid Greenman 	 * make sure that internal objs are in a map somewhere
2567a1f6d91cSDavid Greenman 	 * and none have zero ref counts.
2568a1f6d91cSDavid Greenman 	 */
2569cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
257024a1cce3SDavid Greenman 		if (object->handle == NULL &&
257124a1cce3SDavid Greenman 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2572a1f6d91cSDavid Greenman 			if (object->ref_count == 0) {
25733efc015bSPeter Wemm 				db_printf("vmochk: internal obj has zero ref count: %ld\n",
25743efc015bSPeter Wemm 					(long)object->size);
2575a1f6d91cSDavid Greenman 			}
2576a1f6d91cSDavid Greenman 			if (!vm_object_in_map(object)) {
2577fc62ef1fSBruce Evans 				db_printf(
2578fc62ef1fSBruce Evans 			"vmochk: internal obj is not in a map: "
2579fc62ef1fSBruce Evans 			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2580fc62ef1fSBruce Evans 				    object->ref_count, (u_long)object->size,
2581fc62ef1fSBruce Evans 				    (u_long)object->size,
2582fc62ef1fSBruce Evans 				    (void *)object->backing_object);
2583a1f6d91cSDavid Greenman 			}
2584a1f6d91cSDavid Greenman 		}
2585a1f6d91cSDavid Greenman 	}
2586a1f6d91cSDavid Greenman }
2587a1f6d91cSDavid Greenman 
258826f9a767SRodney W. Grimes /*
2589df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
2590df8bae1dSRodney W. Grimes  */
2591c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static)
2592df8bae1dSRodney W. Grimes {
2593c7c34a24SBruce Evans 	/* XXX convert args. */
2594c7c34a24SBruce Evans 	vm_object_t object = (vm_object_t)addr;
2595c7c34a24SBruce Evans 	boolean_t full = have_addr;
2596c7c34a24SBruce Evans 
2597d031cff1SMatthew Dillon 	vm_page_t p;
2598df8bae1dSRodney W. Grimes 
2599c7c34a24SBruce Evans 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
2600c7c34a24SBruce Evans #define	count	was_count
2601c7c34a24SBruce Evans 
2602d031cff1SMatthew Dillon 	int count;
2603df8bae1dSRodney W. Grimes 
2604df8bae1dSRodney W. Grimes 	if (object == NULL)
2605df8bae1dSRodney W. Grimes 		return;
2606df8bae1dSRodney W. Grimes 
2607eb95adefSBruce Evans 	db_iprintf(
2608ef694c1aSEdward Tomasz Napierala 	    "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x ruid %d charge %jx\n",
2609e47cd172SMaxime Henrion 	    object, (int)object->type, (uintmax_t)object->size,
26103364c323SKonstantin Belousov 	    object->resident_page_count, object->ref_count, object->flags,
2611ef694c1aSEdward Tomasz Napierala 	    object->cred ? object->cred->cr_ruid : -1, (uintmax_t)object->charge);
2612e47cd172SMaxime Henrion 	db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n",
26131c7c3c6aSMatthew Dillon 	    object->shadow_count,
2614eb95adefSBruce Evans 	    object->backing_object ? object->backing_object->ref_count : 0,
2615e47cd172SMaxime Henrion 	    object->backing_object, (uintmax_t)object->backing_object_offset);
2616df8bae1dSRodney W. Grimes 
2617df8bae1dSRodney W. Grimes 	if (!full)
2618df8bae1dSRodney W. Grimes 		return;
2619df8bae1dSRodney W. Grimes 
2620c7c34a24SBruce Evans 	db_indent += 2;
2621df8bae1dSRodney W. Grimes 	count = 0;
2622fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(p, &object->memq, listq) {
2623df8bae1dSRodney W. Grimes 		if (count == 0)
2624c7c34a24SBruce Evans 			db_iprintf("memory:=");
2625df8bae1dSRodney W. Grimes 		else if (count == 6) {
2626c7c34a24SBruce Evans 			db_printf("\n");
2627c7c34a24SBruce Evans 			db_iprintf(" ...");
2628df8bae1dSRodney W. Grimes 			count = 0;
2629df8bae1dSRodney W. Grimes 		} else
2630c7c34a24SBruce Evans 			db_printf(",");
2631df8bae1dSRodney W. Grimes 		count++;
2632df8bae1dSRodney W. Grimes 
2633e47cd172SMaxime Henrion 		db_printf("(off=0x%jx,page=0x%jx)",
2634e47cd172SMaxime Henrion 		    (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p));
2635df8bae1dSRodney W. Grimes 	}
2636df8bae1dSRodney W. Grimes 	if (count != 0)
2637c7c34a24SBruce Evans 		db_printf("\n");
2638c7c34a24SBruce Evans 	db_indent -= 2;
2639df8bae1dSRodney W. Grimes }
26405070c7f8SJohn Dyson 
2641c7c34a24SBruce Evans /* XXX. */
2642c7c34a24SBruce Evans #undef count
2643c7c34a24SBruce Evans 
2644c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */
26455070c7f8SJohn Dyson void
26461b40f8c0SMatthew Dillon vm_object_print(
26471b40f8c0SMatthew Dillon         /* db_expr_t */ long addr,
26481b40f8c0SMatthew Dillon 	boolean_t have_addr,
26491b40f8c0SMatthew Dillon 	/* db_expr_t */ long count,
26501b40f8c0SMatthew Dillon 	char *modif)
2651c7c34a24SBruce Evans {
2652c7c34a24SBruce Evans 	vm_object_print_static(addr, have_addr, count, modif);
2653c7c34a24SBruce Evans }
2654c7c34a24SBruce Evans 
2655c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
26565070c7f8SJohn Dyson {
26575070c7f8SJohn Dyson 	vm_object_t object;
2658bb2ac86fSKonstantin Belousov 	vm_pindex_t fidx;
2659bb2ac86fSKonstantin Belousov 	vm_paddr_t pa;
2660bb2ac86fSKonstantin Belousov 	vm_page_t m, prev_m;
2661bb2ac86fSKonstantin Belousov 	int rcount, nl, c;
2662cc64b484SAlfred Perlstein 
2663bb2ac86fSKonstantin Belousov 	nl = 0;
2664cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
2665fc62ef1fSBruce Evans 		db_printf("new object: %p\n", (void *)object);
26665070c7f8SJohn Dyson 		if (nl > 18) {
26675070c7f8SJohn Dyson 			c = cngetc();
26685070c7f8SJohn Dyson 			if (c != ' ')
26695070c7f8SJohn Dyson 				return;
26705070c7f8SJohn Dyson 			nl = 0;
26715070c7f8SJohn Dyson 		}
26725070c7f8SJohn Dyson 		nl++;
26735070c7f8SJohn Dyson 		rcount = 0;
26745070c7f8SJohn Dyson 		fidx = 0;
2675bb2ac86fSKonstantin Belousov 		pa = -1;
2676bb2ac86fSKonstantin Belousov 		TAILQ_FOREACH(m, &object->memq, listq) {
2677bb2ac86fSKonstantin Belousov 			if (m->pindex > 128)
2678bb2ac86fSKonstantin Belousov 				break;
2679bb2ac86fSKonstantin Belousov 			if ((prev_m = TAILQ_PREV(m, pglist, listq)) != NULL &&
2680bb2ac86fSKonstantin Belousov 			    prev_m->pindex + 1 != m->pindex) {
26815070c7f8SJohn Dyson 				if (rcount) {
26823efc015bSPeter Wemm 					db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
26833efc015bSPeter Wemm 						(long)fidx, rcount, (long)pa);
26845070c7f8SJohn Dyson 					if (nl > 18) {
26855070c7f8SJohn Dyson 						c = cngetc();
26865070c7f8SJohn Dyson 						if (c != ' ')
26875070c7f8SJohn Dyson 							return;
26885070c7f8SJohn Dyson 						nl = 0;
26895070c7f8SJohn Dyson 					}
26905070c7f8SJohn Dyson 					nl++;
26915070c7f8SJohn Dyson 					rcount = 0;
26925070c7f8SJohn Dyson 				}
26935070c7f8SJohn Dyson 			}
26945070c7f8SJohn Dyson 			if (rcount &&
26955070c7f8SJohn Dyson 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
26965070c7f8SJohn Dyson 				++rcount;
26975070c7f8SJohn Dyson 				continue;
26985070c7f8SJohn Dyson 			}
26995070c7f8SJohn Dyson 			if (rcount) {
27002446e4f0SAlan Cox 				db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
27013efc015bSPeter Wemm 					(long)fidx, rcount, (long)pa);
27025070c7f8SJohn Dyson 				if (nl > 18) {
27035070c7f8SJohn Dyson 					c = cngetc();
27045070c7f8SJohn Dyson 					if (c != ' ')
27055070c7f8SJohn Dyson 						return;
27065070c7f8SJohn Dyson 					nl = 0;
27075070c7f8SJohn Dyson 				}
27085070c7f8SJohn Dyson 				nl++;
27095070c7f8SJohn Dyson 			}
2710bb2ac86fSKonstantin Belousov 			fidx = m->pindex;
27115070c7f8SJohn Dyson 			pa = VM_PAGE_TO_PHYS(m);
27125070c7f8SJohn Dyson 			rcount = 1;
27135070c7f8SJohn Dyson 		}
27145070c7f8SJohn Dyson 		if (rcount) {
27153efc015bSPeter Wemm 			db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
27163efc015bSPeter Wemm 				(long)fidx, rcount, (long)pa);
27175070c7f8SJohn Dyson 			if (nl > 18) {
27185070c7f8SJohn Dyson 				c = cngetc();
27195070c7f8SJohn Dyson 				if (c != ' ')
27205070c7f8SJohn Dyson 					return;
27215070c7f8SJohn Dyson 				nl = 0;
27225070c7f8SJohn Dyson 			}
27235070c7f8SJohn Dyson 			nl++;
27245070c7f8SJohn Dyson 		}
27255070c7f8SJohn Dyson 	}
27265070c7f8SJohn Dyson }
2727c3cb3e12SDavid Greenman #endif /* DDB */
2728