xref: /freebsd/sys/vm/vm_object.c (revision 5df87b21d3fc592f112c1721d9c2d533d4411ae4)
160727d8bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
6df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
9df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
10df8bae1dSRodney W. Grimes  * are met:
11df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
12df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
13df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
15df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
17df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
18df8bae1dSRodney W. Grimes  *    without specific prior written permission.
19df8bae1dSRodney W. Grimes  *
20df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
31df8bae1dSRodney W. Grimes  *
323c4dd356SDavid Greenman  *	from: @(#)vm_object.c	8.5 (Berkeley) 3/22/94
33df8bae1dSRodney W. Grimes  *
34df8bae1dSRodney W. Grimes  *
35df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36df8bae1dSRodney W. Grimes  * All rights reserved.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
39df8bae1dSRodney W. Grimes  *
40df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
41df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
42df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
43df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
44df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
45df8bae1dSRodney W. Grimes  *
46df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49df8bae1dSRodney W. Grimes  *
50df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
51df8bae1dSRodney W. Grimes  *
52df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
53df8bae1dSRodney W. Grimes  *  School of Computer Science
54df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
55df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
56df8bae1dSRodney W. Grimes  *
57df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
58df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
59df8bae1dSRodney W. Grimes  */
60df8bae1dSRodney W. Grimes 
61df8bae1dSRodney W. Grimes /*
62df8bae1dSRodney W. Grimes  *	Virtual memory object module.
63df8bae1dSRodney W. Grimes  */
64df8bae1dSRodney W. Grimes 
65874651b1SDavid E. O'Brien #include <sys/cdefs.h>
66874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
67874651b1SDavid E. O'Brien 
68f8a47341SAlan Cox #include "opt_vm.h"
69f8a47341SAlan Cox 
70df8bae1dSRodney W. Grimes #include <sys/param.h>
71df8bae1dSRodney W. Grimes #include <sys/systm.h>
72fb919e4dSMark Murray #include <sys/lock.h>
73867a482dSJohn Dyson #include <sys/mman.h>
74cf2819ccSJohn Dyson #include <sys/mount.h>
75b9b7a4beSMatthew Dillon #include <sys/kernel.h>
76b9b7a4beSMatthew Dillon #include <sys/sysctl.h>
771b367556SJason Evans #include <sys/mutex.h>
78fb919e4dSMark Murray #include <sys/proc.h>		/* for curproc, pageproc */
79fb919e4dSMark Murray #include <sys/socket.h>
803364c323SKonstantin Belousov #include <sys/resourcevar.h>
8189f6b863SAttilio Rao #include <sys/rwlock.h>
82fb919e4dSMark Murray #include <sys/vnode.h>
83fb919e4dSMark Murray #include <sys/vmmeter.h>
841005a129SJohn Baldwin #include <sys/sx.h>
85df8bae1dSRodney W. Grimes 
86df8bae1dSRodney W. Grimes #include <vm/vm.h>
87efeaf95aSDavid Greenman #include <vm/vm_param.h>
88efeaf95aSDavid Greenman #include <vm/pmap.h>
89efeaf95aSDavid Greenman #include <vm/vm_map.h>
90efeaf95aSDavid Greenman #include <vm/vm_object.h>
91df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
9226f9a767SRodney W. Grimes #include <vm/vm_pageout.h>
930d94caffSDavid Greenman #include <vm/vm_pager.h>
9405f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h>
95a1f6d91cSDavid Greenman #include <vm/vm_kern.h>
96efeaf95aSDavid Greenman #include <vm/vm_extern.h>
97774d251dSAttilio Rao #include <vm/vm_radix.h>
98f8a47341SAlan Cox #include <vm/vm_reserv.h>
99670d17b5SJeff Roberson #include <vm/uma.h>
10026f9a767SRodney W. Grimes 
101c53f7aceSDag-Erling Smørgrav static int old_msync;
102c53f7aceSDag-Erling Smørgrav SYSCTL_INT(_vm, OID_AUTO, old_msync, CTLFLAG_RW, &old_msync, 0,
103c53f7aceSDag-Erling Smørgrav     "Use old (insecure) msync behavior");
104c53f7aceSDag-Erling Smørgrav 
105757216f3SKonstantin Belousov static int	vm_object_page_collect_flush(vm_object_t object, vm_page_t p,
106126d6082SKonstantin Belousov 		    int pagerflags, int flags, boolean_t *clearobjflags,
107126d6082SKonstantin Belousov 		    boolean_t *eio);
1083280870dSKonstantin Belousov static boolean_t vm_object_page_remove_write(vm_page_t p, int flags,
109126d6082SKonstantin Belousov 		    boolean_t *clearobjflags);
110b9b7a4beSMatthew Dillon static void	vm_object_qcollapse(vm_object_t object);
11102dd8331SAlan Cox static void	vm_object_vndeallocate(vm_object_t object);
112f6b04d2bSDavid Greenman 
113df8bae1dSRodney W. Grimes /*
114df8bae1dSRodney W. Grimes  *	Virtual memory objects maintain the actual data
115df8bae1dSRodney W. Grimes  *	associated with allocated virtual memory.  A given
116df8bae1dSRodney W. Grimes  *	page of memory exists within exactly one object.
117df8bae1dSRodney W. Grimes  *
118df8bae1dSRodney W. Grimes  *	An object is only deallocated when all "references"
119df8bae1dSRodney W. Grimes  *	are given up.  Only one "reference" to a given
120df8bae1dSRodney W. Grimes  *	region of an object should be writeable.
121df8bae1dSRodney W. Grimes  *
122df8bae1dSRodney W. Grimes  *	Associated with each object is a list of all resident
123df8bae1dSRodney W. Grimes  *	memory pages belonging to that object; this list is
124df8bae1dSRodney W. Grimes  *	maintained by the "vm_page" module, and locked by the object's
125df8bae1dSRodney W. Grimes  *	lock.
126df8bae1dSRodney W. Grimes  *
127df8bae1dSRodney W. Grimes  *	Each object also records a "pager" routine which is
128df8bae1dSRodney W. Grimes  *	used to retrieve (and store) pages to the proper backing
129df8bae1dSRodney W. Grimes  *	storage.  In addition, objects may be backed by other
130df8bae1dSRodney W. Grimes  *	objects from which they were virtual-copied.
131df8bae1dSRodney W. Grimes  *
132df8bae1dSRodney W. Grimes  *	The only items within the object structure which are
133df8bae1dSRodney W. Grimes  *	modified after time of creation are:
134df8bae1dSRodney W. Grimes  *		reference count		locked by object's lock
135df8bae1dSRodney W. Grimes  *		pager routine		locked by object's lock
136df8bae1dSRodney W. Grimes  *
137df8bae1dSRodney W. Grimes  */
138df8bae1dSRodney W. Grimes 
13928f8db14SBruce Evans struct object_q vm_object_list;
140a5698387SAlan Cox struct mtx vm_object_list_mtx;	/* lock for object list and count */
141cccf11b8SAlan Cox 
142cccf11b8SAlan Cox struct vm_object kernel_object_store;
143cccf11b8SAlan Cox struct vm_object kmem_object_store;
144df8bae1dSRodney W. Grimes 
1456472ac3dSEd Schouten static SYSCTL_NODE(_vm_stats, OID_AUTO, object, CTLFLAG_RD, 0,
1466472ac3dSEd Schouten     "VM object stats");
147604c2bbcSAlan Cox 
148f708ef1bSPoul-Henning Kamp static long object_collapses;
149604c2bbcSAlan Cox SYSCTL_LONG(_vm_stats_object, OID_AUTO, collapses, CTLFLAG_RD,
150604c2bbcSAlan Cox     &object_collapses, 0, "VM object collapses");
151604c2bbcSAlan Cox 
152f708ef1bSPoul-Henning Kamp static long object_bypasses;
153604c2bbcSAlan Cox SYSCTL_LONG(_vm_stats_object, OID_AUTO, bypasses, CTLFLAG_RD,
154604c2bbcSAlan Cox     &object_bypasses, 0, "VM object bypasses");
155dad740e9SAlan Cox 
156670d17b5SJeff Roberson static uma_zone_t obj_zone;
1578355f576SJeff Roberson 
158b23f72e9SBrian Feldman static int vm_object_zinit(void *mem, int size, int flags);
1598355f576SJeff Roberson 
1608355f576SJeff Roberson #ifdef INVARIANTS
1618355f576SJeff Roberson static void vm_object_zdtor(void *mem, int size, void *arg);
1628355f576SJeff Roberson 
1638355f576SJeff Roberson static void
1648355f576SJeff Roberson vm_object_zdtor(void *mem, int size, void *arg)
1658355f576SJeff Roberson {
1668355f576SJeff Roberson 	vm_object_t object;
1678355f576SJeff Roberson 
1688355f576SJeff Roberson 	object = (vm_object_t)mem;
16943186e53SAlan Cox 	KASSERT(TAILQ_EMPTY(&object->memq),
170198da1b2SAttilio Rao 	    ("object %p has resident pages in its memq", object));
171774d251dSAttilio Rao 	KASSERT(vm_radix_is_empty(&object->rtree),
172774d251dSAttilio Rao 	    ("object %p has resident pages in its trie", object));
173f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
174f8a47341SAlan Cox 	KASSERT(LIST_EMPTY(&object->rvq),
175f8a47341SAlan Cox 	    ("object %p has reservations",
176f8a47341SAlan Cox 	    object));
177f8a47341SAlan Cox #endif
178c9341161SAttilio Rao 	KASSERT(vm_object_cache_is_empty(object),
1797bfda801SAlan Cox 	    ("object %p has cached pages",
1807bfda801SAlan Cox 	    object));
1818355f576SJeff Roberson 	KASSERT(object->paging_in_progress == 0,
1828355f576SJeff Roberson 	    ("object %p paging_in_progress = %d",
1838355f576SJeff Roberson 	    object, object->paging_in_progress));
1848355f576SJeff Roberson 	KASSERT(object->resident_page_count == 0,
1858355f576SJeff Roberson 	    ("object %p resident_page_count = %d",
1868355f576SJeff Roberson 	    object, object->resident_page_count));
1878355f576SJeff Roberson 	KASSERT(object->shadow_count == 0,
1888355f576SJeff Roberson 	    ("object %p shadow_count = %d",
1898355f576SJeff Roberson 	    object, object->shadow_count));
1908355f576SJeff Roberson }
1918355f576SJeff Roberson #endif
1928355f576SJeff Roberson 
193b23f72e9SBrian Feldman static int
194b23f72e9SBrian Feldman vm_object_zinit(void *mem, int size, int flags)
1958355f576SJeff Roberson {
1968355f576SJeff Roberson 	vm_object_t object;
1978355f576SJeff Roberson 
1988355f576SJeff Roberson 	object = (vm_object_t)mem;
19989f6b863SAttilio Rao 	bzero(&object->lock, sizeof(object->lock));
20089f6b863SAttilio Rao 	rw_init_flags(&object->lock, "vm object", RW_DUPOK);
2018355f576SJeff Roberson 
2028355f576SJeff Roberson 	/* These are true for any object that has been freed */
203774d251dSAttilio Rao 	object->rtree.rt_root = 0;
2048355f576SJeff Roberson 	object->paging_in_progress = 0;
2058355f576SJeff Roberson 	object->resident_page_count = 0;
2068355f576SJeff Roberson 	object->shadow_count = 0;
207774d251dSAttilio Rao 	object->cache.rt_root = 0;
208b23f72e9SBrian Feldman 	return (0);
2098355f576SJeff Roberson }
210df8bae1dSRodney W. Grimes 
211a4915c21SAttilio Rao static void
2126395da54SIan Dowse _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
213df8bae1dSRodney W. Grimes {
2140cddd8f0SMatthew Dillon 
215df8bae1dSRodney W. Grimes 	TAILQ_INIT(&object->memq);
2161c500307SAlan Cox 	LIST_INIT(&object->shadow_head);
217a1f6d91cSDavid Greenman 
21824a1cce3SDavid Greenman 	object->type = type;
21928634820SAlan Cox 	switch (type) {
22028634820SAlan Cox 	case OBJT_DEAD:
22128634820SAlan Cox 		panic("_vm_object_allocate: can't create OBJT_DEAD");
22228634820SAlan Cox 	case OBJT_DEFAULT:
22328634820SAlan Cox 	case OBJT_SWAP:
22428634820SAlan Cox 		object->flags = OBJ_ONEMAPPING;
22528634820SAlan Cox 		break;
22628634820SAlan Cox 	case OBJT_DEVICE:
22728634820SAlan Cox 	case OBJT_SG:
22828634820SAlan Cox 		object->flags = OBJ_FICTITIOUS | OBJ_UNMANAGED;
22928634820SAlan Cox 		break;
23028634820SAlan Cox 	case OBJT_MGTDEVICE:
23128634820SAlan Cox 		object->flags = OBJ_FICTITIOUS;
23228634820SAlan Cox 		break;
23328634820SAlan Cox 	case OBJT_PHYS:
23428634820SAlan Cox 		object->flags = OBJ_UNMANAGED;
23528634820SAlan Cox 		break;
23628634820SAlan Cox 	case OBJT_VNODE:
23728634820SAlan Cox 		object->flags = 0;
23828634820SAlan Cox 		break;
23928634820SAlan Cox 	default:
24028634820SAlan Cox 		panic("_vm_object_allocate: type %d is undefined", type);
24128634820SAlan Cox 	}
242df8bae1dSRodney W. Grimes 	object->size = size;
243b881da26SAlan Cox 	object->generation = 1;
244a1f6d91cSDavid Greenman 	object->ref_count = 1;
2453153e878SAlan Cox 	object->memattr = VM_MEMATTR_DEFAULT;
246ef694c1aSEdward Tomasz Napierala 	object->cred = NULL;
2473364c323SKonstantin Belousov 	object->charge = 0;
24824a1cce3SDavid Greenman 	object->handle = NULL;
24924a1cce3SDavid Greenman 	object->backing_object = NULL;
250a316d390SJohn Dyson 	object->backing_object_offset = (vm_ooffset_t) 0;
251f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
252f8a47341SAlan Cox 	LIST_INIT(&object->rvq);
253f8a47341SAlan Cox #endif
254a1f6d91cSDavid Greenman 
255a5698387SAlan Cox 	mtx_lock(&vm_object_list_mtx);
25660517fd1SJohn Baldwin 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
257a5698387SAlan Cox 	mtx_unlock(&vm_object_list_mtx);
258df8bae1dSRodney W. Grimes }
259df8bae1dSRodney W. Grimes 
260df8bae1dSRodney W. Grimes /*
26126f9a767SRodney W. Grimes  *	vm_object_init:
26226f9a767SRodney W. Grimes  *
26326f9a767SRodney W. Grimes  *	Initialize the VM objects module.
26426f9a767SRodney W. Grimes  */
26526f9a767SRodney W. Grimes void
2661b40f8c0SMatthew Dillon vm_object_init(void)
26726f9a767SRodney W. Grimes {
26826f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_list);
2696008862bSJohn Baldwin 	mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF);
2700217125fSDavid Greenman 
27189f6b863SAttilio Rao 	rw_init(&kernel_object->lock, "kernel vm object");
2729f5c801bSAlan Cox 	_vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
27326f9a767SRodney W. Grimes 	    kernel_object);
274f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
275f8a47341SAlan Cox 	kernel_object->flags |= OBJ_COLORED;
276f8a47341SAlan Cox 	kernel_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS);
277f8a47341SAlan Cox #endif
27826f9a767SRodney W. Grimes 
27989f6b863SAttilio Rao 	rw_init(&kmem_object->lock, "kmem vm object");
2809f5c801bSAlan Cox 	_vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
28126f9a767SRodney W. Grimes 	    kmem_object);
282f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
283f8a47341SAlan Cox 	kmem_object->flags |= OBJ_COLORED;
284f8a47341SAlan Cox 	kmem_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS);
285f8a47341SAlan Cox #endif
286ed6a7863SAlan Cox 
2878dbca793STor Egge 	/*
2888dbca793STor Egge 	 * The lock portion of struct vm_object must be type stable due
2898dbca793STor Egge 	 * to vm_pageout_fallback_object_lock locking a vm object
2908dbca793STor Egge 	 * without holding any references to it.
2918dbca793STor Egge 	 */
2928355f576SJeff Roberson 	obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL,
2938355f576SJeff Roberson #ifdef INVARIANTS
2948355f576SJeff Roberson 	    vm_object_zdtor,
2958355f576SJeff Roberson #else
2968355f576SJeff Roberson 	    NULL,
2978355f576SJeff Roberson #endif
298*5df87b21SJeff Roberson 	    vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
299774d251dSAttilio Rao 
300774d251dSAttilio Rao 	vm_radix_init();
30199448ed1SJohn Dyson }
30299448ed1SJohn Dyson 
30399448ed1SJohn Dyson void
3041b40f8c0SMatthew Dillon vm_object_clear_flag(vm_object_t object, u_short bits)
3051b40f8c0SMatthew Dillon {
3065440b5a9SAlan Cox 
30789f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
308b06805adSJake Burkholder 	object->flags &= ~bits;
3091b40f8c0SMatthew Dillon }
3101b40f8c0SMatthew Dillon 
3113153e878SAlan Cox /*
3123153e878SAlan Cox  *	Sets the default memory attribute for the specified object.  Pages
3133153e878SAlan Cox  *	that are allocated to this object are by default assigned this memory
3143153e878SAlan Cox  *	attribute.
3153153e878SAlan Cox  *
3163153e878SAlan Cox  *	Presently, this function must be called before any pages are allocated
3173153e878SAlan Cox  *	to the object.  In the future, this requirement may be relaxed for
3183153e878SAlan Cox  *	"default" and "swap" objects.
3193153e878SAlan Cox  */
3203153e878SAlan Cox int
3213153e878SAlan Cox vm_object_set_memattr(vm_object_t object, vm_memattr_t memattr)
3223153e878SAlan Cox {
3233153e878SAlan Cox 
32489f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
3253153e878SAlan Cox 	switch (object->type) {
3263153e878SAlan Cox 	case OBJT_DEFAULT:
3273153e878SAlan Cox 	case OBJT_DEVICE:
32896b0b92aSAlan Cox 	case OBJT_MGTDEVICE:
3293153e878SAlan Cox 	case OBJT_PHYS:
33001381811SJohn Baldwin 	case OBJT_SG:
3313153e878SAlan Cox 	case OBJT_SWAP:
3323153e878SAlan Cox 	case OBJT_VNODE:
3333153e878SAlan Cox 		if (!TAILQ_EMPTY(&object->memq))
3343153e878SAlan Cox 			return (KERN_FAILURE);
3353153e878SAlan Cox 		break;
3363153e878SAlan Cox 	case OBJT_DEAD:
3373153e878SAlan Cox 		return (KERN_INVALID_ARGUMENT);
33896b0b92aSAlan Cox 	default:
33996b0b92aSAlan Cox 		panic("vm_object_set_memattr: object %p is of undefined type",
34096b0b92aSAlan Cox 		    object);
3413153e878SAlan Cox 	}
3423153e878SAlan Cox 	object->memattr = memattr;
3433153e878SAlan Cox 	return (KERN_SUCCESS);
3443153e878SAlan Cox }
3453153e878SAlan Cox 
3461b40f8c0SMatthew Dillon void
3471b40f8c0SMatthew Dillon vm_object_pip_add(vm_object_t object, short i)
3481b40f8c0SMatthew Dillon {
349f279b88dSAlan Cox 
35089f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
351b06805adSJake Burkholder 	object->paging_in_progress += i;
3521b40f8c0SMatthew Dillon }
3531b40f8c0SMatthew Dillon 
3541b40f8c0SMatthew Dillon void
3551b40f8c0SMatthew Dillon vm_object_pip_subtract(vm_object_t object, short i)
3561b40f8c0SMatthew Dillon {
357d647a0edSAlan Cox 
35889f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
359b06805adSJake Burkholder 	object->paging_in_progress -= i;
3601b40f8c0SMatthew Dillon }
3611b40f8c0SMatthew Dillon 
3621b40f8c0SMatthew Dillon void
3631b40f8c0SMatthew Dillon vm_object_pip_wakeup(vm_object_t object)
3641b40f8c0SMatthew Dillon {
365f279b88dSAlan Cox 
36689f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
367b06805adSJake Burkholder 	object->paging_in_progress--;
3681b40f8c0SMatthew Dillon 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
3691b40f8c0SMatthew Dillon 		vm_object_clear_flag(object, OBJ_PIPWNT);
3701b40f8c0SMatthew Dillon 		wakeup(object);
3711b40f8c0SMatthew Dillon 	}
3721b40f8c0SMatthew Dillon }
3731b40f8c0SMatthew Dillon 
3741b40f8c0SMatthew Dillon void
3751b40f8c0SMatthew Dillon vm_object_pip_wakeupn(vm_object_t object, short i)
3761b40f8c0SMatthew Dillon {
377d647a0edSAlan Cox 
37889f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
3791b40f8c0SMatthew Dillon 	if (i)
380b06805adSJake Burkholder 		object->paging_in_progress -= i;
3811b40f8c0SMatthew Dillon 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
3821b40f8c0SMatthew Dillon 		vm_object_clear_flag(object, OBJ_PIPWNT);
3831b40f8c0SMatthew Dillon 		wakeup(object);
3841b40f8c0SMatthew Dillon 	}
3851b40f8c0SMatthew Dillon }
3861b40f8c0SMatthew Dillon 
3871b40f8c0SMatthew Dillon void
3881b40f8c0SMatthew Dillon vm_object_pip_wait(vm_object_t object, char *waitid)
3891b40f8c0SMatthew Dillon {
3901ca58953SAlan Cox 
39189f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
3921ca58953SAlan Cox 	while (object->paging_in_progress) {
3931ca58953SAlan Cox 		object->flags |= OBJ_PIPWNT;
3940dde287bSAttilio Rao 		VM_OBJECT_SLEEP(object, object, PVM, waitid, 0);
3951ca58953SAlan Cox 	}
3961b40f8c0SMatthew Dillon }
3971b40f8c0SMatthew Dillon 
39826f9a767SRodney W. Grimes /*
39926f9a767SRodney W. Grimes  *	vm_object_allocate:
40026f9a767SRodney W. Grimes  *
40126f9a767SRodney W. Grimes  *	Returns a new object with the given size.
40226f9a767SRodney W. Grimes  */
40326f9a767SRodney W. Grimes vm_object_t
4046395da54SIan Dowse vm_object_allocate(objtype_t type, vm_pindex_t size)
40526f9a767SRodney W. Grimes {
40690688d13SAlan Cox 	vm_object_t object;
40790688d13SAlan Cox 
40890688d13SAlan Cox 	object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK);
40990688d13SAlan Cox 	_vm_object_allocate(type, size, object);
41090688d13SAlan Cox 	return (object);
41126f9a767SRodney W. Grimes }
41226f9a767SRodney W. Grimes 
41326f9a767SRodney W. Grimes 
41426f9a767SRodney W. Grimes /*
415df8bae1dSRodney W. Grimes  *	vm_object_reference:
416df8bae1dSRodney W. Grimes  *
41715347817SAlan Cox  *	Gets another reference to the given object.  Note: OBJ_DEAD
41815347817SAlan Cox  *	objects can be referenced during final cleaning.
419df8bae1dSRodney W. Grimes  */
4206476c0d2SJohn Dyson void
4211b40f8c0SMatthew Dillon vm_object_reference(vm_object_t object)
422df8bae1dSRodney W. Grimes {
423df8bae1dSRodney W. Grimes 	if (object == NULL)
424df8bae1dSRodney W. Grimes 		return;
42589f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
42652481a9aSJeff Roberson 	vm_object_reference_locked(object);
42789f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
42895e5e988SJohn Dyson }
42995e5e988SJohn Dyson 
43023955314SAlfred Perlstein /*
431b921a12bSAlan Cox  *	vm_object_reference_locked:
432b921a12bSAlan Cox  *
433b921a12bSAlan Cox  *	Gets another reference to the given object.
434b921a12bSAlan Cox  *
435b921a12bSAlan Cox  *	The object must be locked.
436b921a12bSAlan Cox  */
437b921a12bSAlan Cox void
438b921a12bSAlan Cox vm_object_reference_locked(vm_object_t object)
439b921a12bSAlan Cox {
440b921a12bSAlan Cox 	struct vnode *vp;
441b921a12bSAlan Cox 
44289f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
443b921a12bSAlan Cox 	object->ref_count++;
444b921a12bSAlan Cox 	if (object->type == OBJT_VNODE) {
445b921a12bSAlan Cox 		vp = object->handle;
446b921a12bSAlan Cox 		vref(vp);
447b921a12bSAlan Cox 	}
448b921a12bSAlan Cox }
449b921a12bSAlan Cox 
450b921a12bSAlan Cox /*
4519d5abbddSJens Schweikhardt  * Handle deallocating an object of type OBJT_VNODE.
45223955314SAlfred Perlstein  */
45302dd8331SAlan Cox static void
4541b40f8c0SMatthew Dillon vm_object_vndeallocate(vm_object_t object)
45595e5e988SJohn Dyson {
45695e5e988SJohn Dyson 	struct vnode *vp = (struct vnode *) object->handle;
457219cbf59SEivind Eklund 
45889f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
4595526d2d9SEivind Eklund 	KASSERT(object->type == OBJT_VNODE,
4605526d2d9SEivind Eklund 	    ("vm_object_vndeallocate: not a vnode object"));
461219cbf59SEivind Eklund 	KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
462219cbf59SEivind Eklund #ifdef INVARIANTS
46395e5e988SJohn Dyson 	if (object->ref_count == 0) {
46495e5e988SJohn Dyson 		vprint("vm_object_vndeallocate", vp);
46595e5e988SJohn Dyson 		panic("vm_object_vndeallocate: bad object reference count");
46695e5e988SJohn Dyson 	}
46795e5e988SJohn Dyson #endif
46895e5e988SJohn Dyson 
46903fa5b34SKonstantin Belousov 	if (object->ref_count > 1) {
47095e5e988SJohn Dyson 		object->ref_count--;
47189f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
47203fa5b34SKonstantin Belousov 		/* vrele may need the vnode lock. */
47347221757SJohn Dyson 		vrele(vp);
47403fa5b34SKonstantin Belousov 	} else {
47586769ac0SKonstantin Belousov 		vhold(vp);
47689f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
47703fa5b34SKonstantin Belousov 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
47886769ac0SKonstantin Belousov 		vdrop(vp);
47989f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
48003fa5b34SKonstantin Belousov 		object->ref_count--;
48186769ac0SKonstantin Belousov 		if (object->type == OBJT_DEAD) {
48289f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
48386769ac0SKonstantin Belousov 			VOP_UNLOCK(vp, 0);
48486769ac0SKonstantin Belousov 		} else {
48503fa5b34SKonstantin Belousov 			if (object->ref_count == 0)
486877d24acSKonstantin Belousov 				VOP_UNSET_TEXT(vp);
48789f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
48803fa5b34SKonstantin Belousov 			vput(vp);
48903fa5b34SKonstantin Belousov 		}
490df8bae1dSRodney W. Grimes 	}
49186769ac0SKonstantin Belousov }
492df8bae1dSRodney W. Grimes 
493df8bae1dSRodney W. Grimes /*
494df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
495df8bae1dSRodney W. Grimes  *
496df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
497df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
498df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
499df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
500df8bae1dSRodney W. Grimes  *	may be relinquished.
501df8bae1dSRodney W. Grimes  *
502df8bae1dSRodney W. Grimes  *	No object may be locked.
503df8bae1dSRodney W. Grimes  */
50426f9a767SRodney W. Grimes void
5051b40f8c0SMatthew Dillon vm_object_deallocate(vm_object_t object)
506df8bae1dSRodney W. Grimes {
507df8bae1dSRodney W. Grimes 	vm_object_t temp;
5086f2af3fcSKonstantin Belousov 	struct vnode *vp;
509df8bae1dSRodney W. Grimes 
510df8bae1dSRodney W. Grimes 	while (object != NULL) {
51189f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
5123b68228cSAlan Cox 		if (object->type == OBJT_VNODE) {
51395e5e988SJohn Dyson 			vm_object_vndeallocate(object);
51423b186d3SAlan Cox 			return;
5155050aa86SKonstantin Belousov 		}
51695e5e988SJohn Dyson 
5178125b1e6SAlfred Perlstein 		KASSERT(object->ref_count != 0,
5188125b1e6SAlfred Perlstein 			("vm_object_deallocate: object deallocated too many times: %d", object->type));
5192be70f79SJohn Dyson 
5202be70f79SJohn Dyson 		/*
5218125b1e6SAlfred Perlstein 		 * If the reference count goes to 0 we start calling
5228125b1e6SAlfred Perlstein 		 * vm_object_terminate() on the object chain.
5238125b1e6SAlfred Perlstein 		 * A ref count of 1 may be a special case depending on the
5248125b1e6SAlfred Perlstein 		 * shadow count being 0 or 1.
5252be70f79SJohn Dyson 		 */
526c0877f10SJohn Dyson 		object->ref_count--;
5278125b1e6SAlfred Perlstein 		if (object->ref_count > 1) {
52889f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
52923b186d3SAlan Cox 			return;
5308125b1e6SAlfred Perlstein 		} else if (object->ref_count == 1) {
5316f2af3fcSKonstantin Belousov 			if (object->type == OBJT_SWAP &&
5326f2af3fcSKonstantin Belousov 			    (object->flags & OBJ_TMPFS) != 0) {
5336f2af3fcSKonstantin Belousov 				vp = object->un_pager.swp.swp_tmpfs;
5346f2af3fcSKonstantin Belousov 				vhold(vp);
5356f2af3fcSKonstantin Belousov 				VM_OBJECT_WUNLOCK(object);
5366f2af3fcSKonstantin Belousov 				vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
5376f2af3fcSKonstantin Belousov 				vdrop(vp);
5386f2af3fcSKonstantin Belousov 				VM_OBJECT_WLOCK(object);
5397560005cSKonstantin Belousov 				if (object->type == OBJT_DEAD ||
5407560005cSKonstantin Belousov 				    object->ref_count != 1) {
5416f2af3fcSKonstantin Belousov 					VM_OBJECT_WUNLOCK(object);
5426f2af3fcSKonstantin Belousov 					VOP_UNLOCK(vp, 0);
5436f2af3fcSKonstantin Belousov 					return;
5447560005cSKonstantin Belousov 				}
5457560005cSKonstantin Belousov 				if ((object->flags & OBJ_TMPFS) != 0)
5466f2af3fcSKonstantin Belousov 					VOP_UNSET_TEXT(vp);
5476f2af3fcSKonstantin Belousov 				VOP_UNLOCK(vp, 0);
5486f2af3fcSKonstantin Belousov 			}
5494c8e0452SAlan Cox 			if (object->shadow_count == 0 &&
5504c8e0452SAlan Cox 			    object->handle == NULL &&
5514c8e0452SAlan Cox 			    (object->type == OBJT_DEFAULT ||
5526f2af3fcSKonstantin Belousov 			    (object->type == OBJT_SWAP &&
5536f2af3fcSKonstantin Belousov 			    (object->flags & OBJ_TMPFS) == 0))) {
5548125b1e6SAlfred Perlstein 				vm_object_set_flag(object, OBJ_ONEMAPPING);
5558125b1e6SAlfred Perlstein 			} else if ((object->shadow_count == 1) &&
5568125b1e6SAlfred Perlstein 			    (object->handle == NULL) &&
55724a1cce3SDavid Greenman 			    (object->type == OBJT_DEFAULT ||
55824a1cce3SDavid Greenman 			     object->type == OBJT_SWAP)) {
5596f2af3fcSKonstantin Belousov 				KASSERT((object->flags & OBJ_TMPFS) == 0,
560782d4a63SKonstantin Belousov 				    ("shadowed tmpfs v_object %p", object));
561a1f6d91cSDavid Greenman 				vm_object_t robject;
56295e5e988SJohn Dyson 
5631c500307SAlan Cox 				robject = LIST_FIRST(&object->shadow_head);
5645526d2d9SEivind Eklund 				KASSERT(robject != NULL,
565219cbf59SEivind Eklund 				    ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
5665526d2d9SEivind Eklund 					 object->ref_count,
5675526d2d9SEivind Eklund 					 object->shadow_count));
56889f6b863SAttilio Rao 				if (!VM_OBJECT_TRYWLOCK(robject)) {
569b72b0115SAlan Cox 					/*
570b72b0115SAlan Cox 					 * Avoid a potential deadlock.
571b72b0115SAlan Cox 					 */
572b72b0115SAlan Cox 					object->ref_count++;
57389f6b863SAttilio Rao 					VM_OBJECT_WUNLOCK(object);
574a7d86121SAlan Cox 					/*
575a7d86121SAlan Cox 					 * More likely than not the thread
576a7d86121SAlan Cox 					 * holding robject's lock has lower
577a7d86121SAlan Cox 					 * priority than the current thread.
578a7d86121SAlan Cox 					 * Let the lower priority thread run.
579a7d86121SAlan Cox 					 */
5808db5fc58SJohn Baldwin 					pause("vmo_de", 1);
581b72b0115SAlan Cox 					continue;
582b72b0115SAlan Cox 				}
583d936694fSAlan Cox 				/*
584d936694fSAlan Cox 				 * Collapse object into its shadow unless its
585d936694fSAlan Cox 				 * shadow is dead.  In that case, object will
586d936694fSAlan Cox 				 * be deallocated by the thread that is
587d936694fSAlan Cox 				 * deallocating its shadow.
588d936694fSAlan Cox 				 */
589d936694fSAlan Cox 				if ((robject->flags & OBJ_DEAD) == 0 &&
590d936694fSAlan Cox 				    (robject->handle == NULL) &&
59124a1cce3SDavid Greenman 				    (robject->type == OBJT_DEFAULT ||
59224a1cce3SDavid Greenman 				     robject->type == OBJT_SWAP)) {
593a1f6d91cSDavid Greenman 
59495e5e988SJohn Dyson 					robject->ref_count++;
595138449dcSAlan Cox retry:
596138449dcSAlan Cox 					if (robject->paging_in_progress) {
59789f6b863SAttilio Rao 						VM_OBJECT_WUNLOCK(object);
598138449dcSAlan Cox 						vm_object_pip_wait(robject,
599138449dcSAlan Cox 						    "objde1");
6002e9f4a69SAlan Cox 						temp = robject->backing_object;
6012e9f4a69SAlan Cox 						if (object == temp) {
60289f6b863SAttilio Rao 							VM_OBJECT_WLOCK(object);
603138449dcSAlan Cox 							goto retry;
6042e9f4a69SAlan Cox 						}
605138449dcSAlan Cox 					} else if (object->paging_in_progress) {
60689f6b863SAttilio Rao 						VM_OBJECT_WUNLOCK(robject);
607138449dcSAlan Cox 						object->flags |= OBJ_PIPWNT;
6080dde287bSAttilio Rao 						VM_OBJECT_SLEEP(object, object,
609138449dcSAlan Cox 						    PDROP | PVM, "objde2", 0);
61089f6b863SAttilio Rao 						VM_OBJECT_WLOCK(robject);
6112e9f4a69SAlan Cox 						temp = robject->backing_object;
6122e9f4a69SAlan Cox 						if (object == temp) {
61389f6b863SAttilio Rao 							VM_OBJECT_WLOCK(object);
614138449dcSAlan Cox 							goto retry;
615a1f6d91cSDavid Greenman 						}
6162e9f4a69SAlan Cox 					} else
61789f6b863SAttilio Rao 						VM_OBJECT_WUNLOCK(object);
6182e9f4a69SAlan Cox 
61995e5e988SJohn Dyson 					if (robject->ref_count == 1) {
62095e5e988SJohn Dyson 						robject->ref_count--;
621ba8da839SDavid Greenman 						object = robject;
62295e5e988SJohn Dyson 						goto doterm;
62395e5e988SJohn Dyson 					}
62495e5e988SJohn Dyson 					object = robject;
62595e5e988SJohn Dyson 					vm_object_collapse(object);
62689f6b863SAttilio Rao 					VM_OBJECT_WUNLOCK(object);
627ba8da839SDavid Greenman 					continue;
628a1f6d91cSDavid Greenman 				}
62989f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(robject);
63095e5e988SJohn Dyson 			}
63189f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
63223b186d3SAlan Cox 			return;
63395e5e988SJohn Dyson 		}
63495e5e988SJohn Dyson doterm:
63524a1cce3SDavid Greenman 		temp = object->backing_object;
636c9917419SAlan Cox 		if (temp != NULL) {
63789f6b863SAttilio Rao 			VM_OBJECT_WLOCK(temp);
6381c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
63995e5e988SJohn Dyson 			temp->shadow_count--;
64089f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(temp);
64195461b45SJohn Dyson 			object->backing_object = NULL;
642de5f6a77SJohn Dyson 		}
643245df27cSMatthew Dillon 		/*
644245df27cSMatthew Dillon 		 * Don't double-terminate, we could be in a termination
645245df27cSMatthew Dillon 		 * recursion due to the terminate having to sync data
646245df27cSMatthew Dillon 		 * to disk.
647245df27cSMatthew Dillon 		 */
648245df27cSMatthew Dillon 		if ((object->flags & OBJ_DEAD) == 0)
649df8bae1dSRodney W. Grimes 			vm_object_terminate(object);
650c829b9d0SAlan Cox 		else
65189f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
652df8bae1dSRodney W. Grimes 		object = temp;
653df8bae1dSRodney W. Grimes 	}
654df8bae1dSRodney W. Grimes }
655df8bae1dSRodney W. Grimes 
656df8bae1dSRodney W. Grimes /*
6572ac78f0eSStephan Uphoff  *	vm_object_destroy removes the object from the global object list
6582ac78f0eSStephan Uphoff  *      and frees the space for the object.
6592ac78f0eSStephan Uphoff  */
6602ac78f0eSStephan Uphoff void
6612ac78f0eSStephan Uphoff vm_object_destroy(vm_object_t object)
6622ac78f0eSStephan Uphoff {
6632ac78f0eSStephan Uphoff 
6642ac78f0eSStephan Uphoff 	/*
6652ac78f0eSStephan Uphoff 	 * Remove the object from the global object list.
6662ac78f0eSStephan Uphoff 	 */
6672ac78f0eSStephan Uphoff 	mtx_lock(&vm_object_list_mtx);
6682ac78f0eSStephan Uphoff 	TAILQ_REMOVE(&vm_object_list, object, object_list);
6692ac78f0eSStephan Uphoff 	mtx_unlock(&vm_object_list_mtx);
6702ac78f0eSStephan Uphoff 
6712ac78f0eSStephan Uphoff 	/*
6723364c323SKonstantin Belousov 	 * Release the allocation charge.
6733364c323SKonstantin Belousov 	 */
674ef694c1aSEdward Tomasz Napierala 	if (object->cred != NULL) {
6753364c323SKonstantin Belousov 		KASSERT(object->type == OBJT_DEFAULT ||
6763364c323SKonstantin Belousov 		    object->type == OBJT_SWAP,
677ef694c1aSEdward Tomasz Napierala 		    ("vm_object_terminate: non-swap obj %p has cred",
6783364c323SKonstantin Belousov 		     object));
679ef694c1aSEdward Tomasz Napierala 		swap_release_by_cred(object->charge, object->cred);
6803364c323SKonstantin Belousov 		object->charge = 0;
681ef694c1aSEdward Tomasz Napierala 		crfree(object->cred);
682ef694c1aSEdward Tomasz Napierala 		object->cred = NULL;
6833364c323SKonstantin Belousov 	}
6843364c323SKonstantin Belousov 
6853364c323SKonstantin Belousov 	/*
6862ac78f0eSStephan Uphoff 	 * Free the space for the object.
6872ac78f0eSStephan Uphoff 	 */
6882ac78f0eSStephan Uphoff 	uma_zfree(obj_zone, object);
6892ac78f0eSStephan Uphoff }
6902ac78f0eSStephan Uphoff 
6912ac78f0eSStephan Uphoff /*
692df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
693df8bae1dSRodney W. Grimes  *	up all previously used resources.
694df8bae1dSRodney W. Grimes  *
695df8bae1dSRodney W. Grimes  *	The object must be locked.
6961c7c3c6aSMatthew Dillon  *	This routine may block.
697df8bae1dSRodney W. Grimes  */
69895e5e988SJohn Dyson void
6991b40f8c0SMatthew Dillon vm_object_terminate(vm_object_t object)
700df8bae1dSRodney W. Grimes {
70117ea6f00SAlan Cox 	vm_page_t p, p_next;
702df8bae1dSRodney W. Grimes 
70389f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
7040cddd8f0SMatthew Dillon 
70595e5e988SJohn Dyson 	/*
70695e5e988SJohn Dyson 	 * Make sure no one uses us.
70795e5e988SJohn Dyson 	 */
708069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_DEAD);
7093c631446SJohn Dyson 
710df8bae1dSRodney W. Grimes 	/*
711f6b04d2bSDavid Greenman 	 * wait for the pageout daemon to be done with the object
712df8bae1dSRodney W. Grimes 	 */
71366095752SJohn Dyson 	vm_object_pip_wait(object, "objtrm");
714df8bae1dSRodney W. Grimes 
7155526d2d9SEivind Eklund 	KASSERT(!object->paging_in_progress,
7165526d2d9SEivind Eklund 		("vm_object_terminate: pageout in progress"));
71726f9a767SRodney W. Grimes 
71826f9a767SRodney W. Grimes 	/*
7190d94caffSDavid Greenman 	 * Clean and free the pages, as appropriate. All references to the
7200d94caffSDavid Greenman 	 * object are gone, so we don't need to lock it.
72126f9a767SRodney W. Grimes 	 */
72224a1cce3SDavid Greenman 	if (object->type == OBJT_VNODE) {
723f7dd7b63SAlan Cox 		struct vnode *vp = (struct vnode *)object->handle;
72495e5e988SJohn Dyson 
72595e5e988SJohn Dyson 		/*
72695e5e988SJohn Dyson 		 * Clean pages and flush buffers.
72795e5e988SJohn Dyson 		 */
7288f9110f6SJohn Dyson 		vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
72989f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
73095e5e988SJohn Dyson 
7310d7935fdSAttilio Rao 		vinvalbuf(vp, V_SAVE, 0, 0);
732f7dd7b63SAlan Cox 
73389f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
734bef608bdSJohn Dyson 	}
735bef608bdSJohn Dyson 
736971dd342SAlfred Perlstein 	KASSERT(object->ref_count == 0,
737971dd342SAlfred Perlstein 		("vm_object_terminate: object with references, ref_count=%d",
738971dd342SAlfred Perlstein 		object->ref_count));
739996c772fSJohn Dyson 
7400d94caffSDavid Greenman 	/*
74117ea6f00SAlan Cox 	 * Free any remaining pageable pages.  This also removes them from the
74217ea6f00SAlan Cox 	 * paging queues.  However, don't free wired pages, just remove them
74317ea6f00SAlan Cox 	 * from the object.  Rather than incrementally removing each page from
74417ea6f00SAlan Cox 	 * the object, the page and object are reset to any empty state.
745df8bae1dSRodney W. Grimes 	 */
74617ea6f00SAlan Cox 	TAILQ_FOREACH_SAFE(p, &object->memq, listq, p_next) {
7479af80719SAlan Cox 		KASSERT(!p->busy && (p->oflags & VPO_BUSY) == 0,
74817ea6f00SAlan Cox 		    ("vm_object_terminate: freeing busy page %p", p));
7492965a453SKip Macy 		vm_page_lock(p);
75017ea6f00SAlan Cox 		/*
75117ea6f00SAlan Cox 		 * Optimize the page's removal from the object by resetting
75217ea6f00SAlan Cox 		 * its "object" field.  Specifically, if the page is not
75317ea6f00SAlan Cox 		 * wired, then the effect of this assignment is that
75417ea6f00SAlan Cox 		 * vm_page_free()'s call to vm_page_remove() will return
75517ea6f00SAlan Cox 		 * immediately without modifying the page or the object.
75617ea6f00SAlan Cox 		 */
75717ea6f00SAlan Cox 		p->object = NULL;
7580b10ba98SDavid Greenman 		if (p->wire_count == 0) {
759df8bae1dSRodney W. Grimes 			vm_page_free(p);
76070721880SAlan Cox 			PCPU_INC(cnt.v_pfree);
76117ea6f00SAlan Cox 		}
7622965a453SKip Macy 		vm_page_unlock(p);
7632965a453SKip Macy 	}
76417ea6f00SAlan Cox 	/*
76517ea6f00SAlan Cox 	 * If the object contained any pages, then reset it to an empty state.
76617ea6f00SAlan Cox 	 * None of the object's fields, including "resident_page_count", were
76717ea6f00SAlan Cox 	 * modified by the preceding loop.
76817ea6f00SAlan Cox 	 */
76917ea6f00SAlan Cox 	if (object->resident_page_count != 0) {
770774d251dSAttilio Rao 		vm_radix_reclaim_allnodes(&object->rtree);
77117ea6f00SAlan Cox 		TAILQ_INIT(&object->memq);
77217ea6f00SAlan Cox 		object->resident_page_count = 0;
77317ea6f00SAlan Cox 		if (object->type == OBJT_VNODE)
77417ea6f00SAlan Cox 			vdrop(object->handle);
77517ea6f00SAlan Cox 	}
776bef608bdSJohn Dyson 
777f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
778f8a47341SAlan Cox 	if (__predict_false(!LIST_EMPTY(&object->rvq)))
779f8a47341SAlan Cox 		vm_reserv_break_all(object);
780f8a47341SAlan Cox #endif
781c9341161SAttilio Rao 	if (__predict_false(!vm_object_cache_is_empty(object)))
782c9444914SAlan Cox 		vm_page_cache_free(object, 0, 0);
7837bfda801SAlan Cox 
7842d8acc0fSJohn Dyson 	/*
7859fcfb650SDavid Greenman 	 * Let the pager know object is dead.
7869fcfb650SDavid Greenman 	 */
7879fcfb650SDavid Greenman 	vm_pager_deallocate(object);
78889f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
7899fcfb650SDavid Greenman 
7902ac78f0eSStephan Uphoff 	vm_object_destroy(object);
79147221757SJohn Dyson }
792df8bae1dSRodney W. Grimes 
793edf93b25SAlan Cox /*
794edf93b25SAlan Cox  * Make the page read-only so that we can clear the object flags.  However, if
795edf93b25SAlan Cox  * this is a nosync mmap then the object is likely to stay dirty so do not
796edf93b25SAlan Cox  * mess with the page and do not clear the object flags.  Returns TRUE if the
797edf93b25SAlan Cox  * page should be flushed, and FALSE otherwise.
798edf93b25SAlan Cox  */
7993280870dSKonstantin Belousov static boolean_t
800126d6082SKonstantin Belousov vm_object_page_remove_write(vm_page_t p, int flags, boolean_t *clearobjflags)
8013280870dSKonstantin Belousov {
8023280870dSKonstantin Belousov 
8033280870dSKonstantin Belousov 	/*
8043280870dSKonstantin Belousov 	 * If we have been asked to skip nosync pages and this is a
8053280870dSKonstantin Belousov 	 * nosync page, skip it.  Note that the object flags were not
8063280870dSKonstantin Belousov 	 * cleared in this case so we do not have to set them.
8073280870dSKonstantin Belousov 	 */
8083280870dSKonstantin Belousov 	if ((flags & OBJPC_NOSYNC) != 0 && (p->oflags & VPO_NOSYNC) != 0) {
809126d6082SKonstantin Belousov 		*clearobjflags = FALSE;
8103280870dSKonstantin Belousov 		return (FALSE);
8113280870dSKonstantin Belousov 	} else {
8123280870dSKonstantin Belousov 		pmap_remove_write(p);
8133280870dSKonstantin Belousov 		return (p->dirty != 0);
8143280870dSKonstantin Belousov 	}
8153280870dSKonstantin Belousov }
8163280870dSKonstantin Belousov 
817df8bae1dSRodney W. Grimes /*
818df8bae1dSRodney W. Grimes  *	vm_object_page_clean
819df8bae1dSRodney W. Grimes  *
8204f79d873SMatthew Dillon  *	Clean all dirty pages in the specified range of object.  Leaves page
8214f79d873SMatthew Dillon  * 	on whatever queue it is currently on.   If NOSYNC is set then do not
822b146f9e5SAlan Cox  *	write out pages with VPO_NOSYNC set (originally comes from MAP_NOSYNC),
8234f79d873SMatthew Dillon  *	leaving the object dirty.
82426f9a767SRodney W. Grimes  *
82543b7990eSMatthew Dillon  *	When stuffing pages asynchronously, allow clustering.  XXX we need a
82643b7990eSMatthew Dillon  *	synchronous clustering mode implementation.
82743b7990eSMatthew Dillon  *
82826f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
82926f9a767SRodney W. Grimes  *
83026f9a767SRodney W. Grimes  *	The object must be locked.
831126d6082SKonstantin Belousov  *
832126d6082SKonstantin Belousov  *	Returns FALSE if some page from the range was not written, as
833126d6082SKonstantin Belousov  *	reported by the pager, and TRUE otherwise.
83426f9a767SRodney W. Grimes  */
835126d6082SKonstantin Belousov boolean_t
83617f3095dSAlan Cox vm_object_page_clean(vm_object_t object, vm_ooffset_t start, vm_ooffset_t end,
837e239bb97SKonstantin Belousov     int flags)
838f6b04d2bSDavid Greenman {
839e239bb97SKonstantin Belousov 	vm_page_t np, p;
84017f3095dSAlan Cox 	vm_pindex_t pi, tend, tstart;
841126d6082SKonstantin Belousov 	int curgeneration, n, pagerflags;
842126d6082SKonstantin Belousov 	boolean_t clearobjflags, eio, res;
843f6b04d2bSDavid Greenman 
84489f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
845e5f299ffSKonstantin Belousov 
846e5f299ffSKonstantin Belousov 	/*
847e5f299ffSKonstantin Belousov 	 * The OBJ_MIGHTBEDIRTY flag is only set for OBJT_VNODE
848e5f299ffSKonstantin Belousov 	 * objects.  The check below prevents the function from
849e5f299ffSKonstantin Belousov 	 * operating on non-vnode objects.
850e5f299ffSKonstantin Belousov 	 */
851e239bb97SKonstantin Belousov 	if ((object->flags & OBJ_MIGHTBEDIRTY) == 0 ||
852e239bb97SKonstantin Belousov 	    object->resident_page_count == 0)
853126d6082SKonstantin Belousov 		return (TRUE);
854f6b04d2bSDavid Greenman 
855e239bb97SKonstantin Belousov 	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) != 0 ?
856e239bb97SKonstantin Belousov 	    VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
857e239bb97SKonstantin Belousov 	pagerflags |= (flags & OBJPC_INVAL) != 0 ? VM_PAGER_PUT_INVAL : 0;
858e239bb97SKonstantin Belousov 
85917f3095dSAlan Cox 	tstart = OFF_TO_IDX(start);
86017f3095dSAlan Cox 	tend = (end == 0) ? object->size : OFF_TO_IDX(end + PAGE_MASK);
86117f3095dSAlan Cox 	clearobjflags = tstart == 0 && tend >= object->size;
862126d6082SKonstantin Belousov 	res = TRUE;
863f6b04d2bSDavid Greenman 
864bd7e5f99SJohn Dyson rescan:
8652d8acc0fSJohn Dyson 	curgeneration = object->generation;
8662d8acc0fSJohn Dyson 
86717f3095dSAlan Cox 	for (p = vm_page_find_least(object, tstart); p != NULL; p = np) {
868bd7e5f99SJohn Dyson 		pi = p->pindex;
869e239bb97SKonstantin Belousov 		if (pi >= tend)
870e239bb97SKonstantin Belousov 			break;
871e239bb97SKonstantin Belousov 		np = TAILQ_NEXT(p, listq);
872e239bb97SKonstantin Belousov 		if (p->valid == 0)
873aef922f5SJohn Dyson 			continue;
874780636b7SKonstantin Belousov 		if (vm_page_sleep_if_busy(p, TRUE, "vpcwai")) {
875e65919f9SKonstantin Belousov 			if (object->generation != curgeneration) {
876e65919f9SKonstantin Belousov 				if ((flags & OBJPC_SYNC) != 0)
877e239bb97SKonstantin Belousov 					goto rescan;
878e65919f9SKonstantin Belousov 				else
879126d6082SKonstantin Belousov 					clearobjflags = FALSE;
880e65919f9SKonstantin Belousov 			}
881780636b7SKonstantin Belousov 			np = vm_page_find_least(object, pi);
882780636b7SKonstantin Belousov 			continue;
883f6b04d2bSDavid Greenman 		}
8843280870dSKonstantin Belousov 		if (!vm_object_page_remove_write(p, flags, &clearobjflags))
885bd7e5f99SJohn Dyson 			continue;
886e239bb97SKonstantin Belousov 
8873280870dSKonstantin Belousov 		n = vm_object_page_collect_flush(object, p, pagerflags,
888126d6082SKonstantin Belousov 		    flags, &clearobjflags, &eio);
889126d6082SKonstantin Belousov 		if (eio) {
890126d6082SKonstantin Belousov 			res = FALSE;
891126d6082SKonstantin Belousov 			clearobjflags = FALSE;
892126d6082SKonstantin Belousov 		}
893e65919f9SKonstantin Belousov 		if (object->generation != curgeneration) {
894e65919f9SKonstantin Belousov 			if ((flags & OBJPC_SYNC) != 0)
895b9b7a4beSMatthew Dillon 				goto rescan;
896e65919f9SKonstantin Belousov 			else
897126d6082SKonstantin Belousov 				clearobjflags = FALSE;
898e65919f9SKonstantin Belousov 		}
899031ec8c1SKonstantin Belousov 
900031ec8c1SKonstantin Belousov 		/*
901031ec8c1SKonstantin Belousov 		 * If the VOP_PUTPAGES() did a truncated write, so
902031ec8c1SKonstantin Belousov 		 * that even the first page of the run is not fully
903031ec8c1SKonstantin Belousov 		 * written, vm_pageout_flush() returns 0 as the run
904031ec8c1SKonstantin Belousov 		 * length.  Since the condition that caused truncated
905031ec8c1SKonstantin Belousov 		 * write may be permanent, e.g. exhausted free space,
906031ec8c1SKonstantin Belousov 		 * accepting n == 0 would cause an infinite loop.
907031ec8c1SKonstantin Belousov 		 *
908031ec8c1SKonstantin Belousov 		 * Forwarding the iterator leaves the unwritten page
909031ec8c1SKonstantin Belousov 		 * behind, but there is not much we can do there if
910031ec8c1SKonstantin Belousov 		 * filesystem refuses to write it.
911031ec8c1SKonstantin Belousov 		 */
912126d6082SKonstantin Belousov 		if (n == 0) {
913031ec8c1SKonstantin Belousov 			n = 1;
914126d6082SKonstantin Belousov 			clearobjflags = FALSE;
915126d6082SKonstantin Belousov 		}
916e239bb97SKonstantin Belousov 		np = vm_page_find_least(object, pi + n);
917b9b7a4beSMatthew Dillon 	}
918b9b7a4beSMatthew Dillon #if 0
919e239bb97SKonstantin Belousov 	VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC) ? MNT_WAIT : 0);
920b9b7a4beSMatthew Dillon #endif
921b9b7a4beSMatthew Dillon 
922edf93b25SAlan Cox 	if (clearobjflags)
9233280870dSKonstantin Belousov 		vm_object_clear_flag(object, OBJ_MIGHTBEDIRTY);
924126d6082SKonstantin Belousov 	return (res);
925b9b7a4beSMatthew Dillon }
926b9b7a4beSMatthew Dillon 
927b9b7a4beSMatthew Dillon static int
9283280870dSKonstantin Belousov vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int pagerflags,
929126d6082SKonstantin Belousov     int flags, boolean_t *clearobjflags, boolean_t *eio)
930b9b7a4beSMatthew Dillon {
9313157c503SKonstantin Belousov 	vm_page_t ma[vm_pageout_page_count], p_first, tp;
9323157c503SKonstantin Belousov 	int count, i, mreq, runlen;
933b9b7a4beSMatthew Dillon 
9347bec141bSKip Macy 	vm_page_lock_assert(p, MA_NOTOWNED);
93589f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
9363157c503SKonstantin Belousov 
9373157c503SKonstantin Belousov 	count = 1;
9383157c503SKonstantin Belousov 	mreq = 0;
9393157c503SKonstantin Belousov 
9403157c503SKonstantin Belousov 	for (tp = p; count < vm_pageout_page_count; count++) {
9413157c503SKonstantin Belousov 		tp = vm_page_next(tp);
942e239bb97SKonstantin Belousov 		if (tp == NULL || tp->busy != 0 || (tp->oflags & VPO_BUSY) != 0)
943bd7e5f99SJohn Dyson 			break;
9443280870dSKonstantin Belousov 		if (!vm_object_page_remove_write(tp, flags, clearobjflags))
945bd7e5f99SJohn Dyson 			break;
946bd7e5f99SJohn Dyson 	}
947aef922f5SJohn Dyson 
9483157c503SKonstantin Belousov 	for (p_first = p; count < vm_pageout_page_count; count++) {
9493157c503SKonstantin Belousov 		tp = vm_page_prev(p_first);
950e239bb97SKonstantin Belousov 		if (tp == NULL || tp->busy != 0 || (tp->oflags & VPO_BUSY) != 0)
951bd7e5f99SJohn Dyson 			break;
9523280870dSKonstantin Belousov 		if (!vm_object_page_remove_write(tp, flags, clearobjflags))
953bd7e5f99SJohn Dyson 			break;
9543157c503SKonstantin Belousov 		p_first = tp;
9553157c503SKonstantin Belousov 		mreq++;
956bd7e5f99SJohn Dyson 	}
957bd7e5f99SJohn Dyson 
9583157c503SKonstantin Belousov 	for (tp = p_first, i = 0; i < count; tp = TAILQ_NEXT(tp, listq), i++)
9593157c503SKonstantin Belousov 		ma[i] = tp;
960cf2819ccSJohn Dyson 
961126d6082SKonstantin Belousov 	vm_pageout_flush(ma, count, pagerflags, mreq, &runlen, eio);
9621e8a675cSKonstantin Belousov 	return (runlen);
96326f9a767SRodney W. Grimes }
964df8bae1dSRodney W. Grimes 
9651efb74fbSJohn Dyson /*
966950f8459SAlan Cox  * Note that there is absolutely no sense in writing out
967950f8459SAlan Cox  * anonymous objects, so we track down the vnode object
968950f8459SAlan Cox  * to write out.
969950f8459SAlan Cox  * We invalidate (remove) all pages from the address space
970950f8459SAlan Cox  * for semantic correctness.
971950f8459SAlan Cox  *
9726bbee8e2SAlan Cox  * If the backing object is a device object with unmanaged pages, then any
9736bbee8e2SAlan Cox  * mappings to the specified range of pages must be removed before this
9746bbee8e2SAlan Cox  * function is called.
9756bbee8e2SAlan Cox  *
976950f8459SAlan Cox  * Note: certain anonymous maps, such as MAP_NOSYNC maps,
977950f8459SAlan Cox  * may start out with a NULL object.
978950f8459SAlan Cox  */
979126d6082SKonstantin Belousov boolean_t
980950f8459SAlan Cox vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size,
981950f8459SAlan Cox     boolean_t syncio, boolean_t invalidate)
982950f8459SAlan Cox {
983950f8459SAlan Cox 	vm_object_t backing_object;
984950f8459SAlan Cox 	struct vnode *vp;
9853b582b4eSTor Egge 	struct mount *mp;
986126d6082SKonstantin Belousov 	int error, flags, fsync_after;
987126d6082SKonstantin Belousov 	boolean_t res;
988950f8459SAlan Cox 
989950f8459SAlan Cox 	if (object == NULL)
990126d6082SKonstantin Belousov 		return (TRUE);
991126d6082SKonstantin Belousov 	res = TRUE;
992126d6082SKonstantin Belousov 	error = 0;
99389f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
994950f8459SAlan Cox 	while ((backing_object = object->backing_object) != NULL) {
99589f6b863SAttilio Rao 		VM_OBJECT_WLOCK(backing_object);
99656e0670fSAlan Cox 		offset += object->backing_object_offset;
99789f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
998950f8459SAlan Cox 		object = backing_object;
999950f8459SAlan Cox 		if (object->size < OFF_TO_IDX(offset + size))
1000950f8459SAlan Cox 			size = IDX_TO_OFF(object->size) - offset;
1001950f8459SAlan Cox 	}
1002950f8459SAlan Cox 	/*
1003950f8459SAlan Cox 	 * Flush pages if writing is allowed, invalidate them
1004950f8459SAlan Cox 	 * if invalidation requested.  Pages undergoing I/O
1005950f8459SAlan Cox 	 * will be ignored by vm_object_page_remove().
1006950f8459SAlan Cox 	 *
1007950f8459SAlan Cox 	 * We cannot lock the vnode and then wait for paging
1008950f8459SAlan Cox 	 * to complete without deadlocking against vm_fault.
1009950f8459SAlan Cox 	 * Instead we simply call vm_object_page_remove() and
1010950f8459SAlan Cox 	 * allow it to block internally on a page-by-page
1011950f8459SAlan Cox 	 * basis when it encounters pages undergoing async
1012950f8459SAlan Cox 	 * I/O.
1013950f8459SAlan Cox 	 */
1014950f8459SAlan Cox 	if (object->type == OBJT_VNODE &&
1015950f8459SAlan Cox 	    (object->flags & OBJ_MIGHTBEDIRTY) != 0) {
1016950f8459SAlan Cox 		vp = object->handle;
101789f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
10183b582b4eSTor Egge 		(void) vn_start_write(vp, &mp, V_WAIT);
1019cb05b60aSAttilio Rao 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
102075ff604aSKonstantin Belousov 		if (syncio && !invalidate && offset == 0 &&
102175ff604aSKonstantin Belousov 		    OFF_TO_IDX(size) == object->size) {
102275ff604aSKonstantin Belousov 			/*
102375ff604aSKonstantin Belousov 			 * If syncing the whole mapping of the file,
102475ff604aSKonstantin Belousov 			 * it is faster to schedule all the writes in
102575ff604aSKonstantin Belousov 			 * async mode, also allowing the clustering,
102675ff604aSKonstantin Belousov 			 * and then wait for i/o to complete.
102775ff604aSKonstantin Belousov 			 */
102875ff604aSKonstantin Belousov 			flags = 0;
102975ff604aSKonstantin Belousov 			fsync_after = TRUE;
103075ff604aSKonstantin Belousov 		} else {
1031950f8459SAlan Cox 			flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
103275ff604aSKonstantin Belousov 			flags |= invalidate ? (OBJPC_SYNC | OBJPC_INVAL) : 0;
103375ff604aSKonstantin Belousov 			fsync_after = FALSE;
103475ff604aSKonstantin Belousov 		}
103589f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
1036126d6082SKonstantin Belousov 		res = vm_object_page_clean(object, offset, offset + size,
1037126d6082SKonstantin Belousov 		    flags);
103889f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
103975ff604aSKonstantin Belousov 		if (fsync_after)
1040126d6082SKonstantin Belousov 			error = VOP_FSYNC(vp, MNT_WAIT, curthread);
104122db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
10423b582b4eSTor Egge 		vn_finished_write(mp);
1043126d6082SKonstantin Belousov 		if (error != 0)
1044126d6082SKonstantin Belousov 			res = FALSE;
104589f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
1046950f8459SAlan Cox 	}
1047950f8459SAlan Cox 	if ((object->type == OBJT_VNODE ||
1048950f8459SAlan Cox 	     object->type == OBJT_DEVICE) && invalidate) {
10496bbee8e2SAlan Cox 		if (object->type == OBJT_DEVICE)
10506bbee8e2SAlan Cox 			/*
10516bbee8e2SAlan Cox 			 * The option OBJPR_NOTMAPPED must be passed here
10526bbee8e2SAlan Cox 			 * because vm_object_page_remove() cannot remove
10536bbee8e2SAlan Cox 			 * unmanaged mappings.
10546bbee8e2SAlan Cox 			 */
10556bbee8e2SAlan Cox 			flags = OBJPR_NOTMAPPED;
10566bbee8e2SAlan Cox 		else if (old_msync)
1057ebf5d94eSKonstantin Belousov 			flags = OBJPR_NOTWIRED;
10586bbee8e2SAlan Cox 		else
1059ebf5d94eSKonstantin Belousov 			flags = OBJPR_CLEANONLY | OBJPR_NOTWIRED;
10606bbee8e2SAlan Cox 		vm_object_page_remove(object, OFF_TO_IDX(offset),
10616bbee8e2SAlan Cox 		    OFF_TO_IDX(offset + size + PAGE_MASK), flags);
1062950f8459SAlan Cox 	}
106389f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
1064126d6082SKonstantin Belousov 	return (res);
1065950f8459SAlan Cox }
1066950f8459SAlan Cox 
1067950f8459SAlan Cox /*
1068867a482dSJohn Dyson  *	vm_object_madvise:
1069867a482dSJohn Dyson  *
1070867a482dSJohn Dyson  *	Implements the madvise function at the object/page level.
10711c7c3c6aSMatthew Dillon  *
1072193b9358SAlan Cox  *	MADV_WILLNEED	(any object)
1073193b9358SAlan Cox  *
1074193b9358SAlan Cox  *	    Activate the specified pages if they are resident.
1075193b9358SAlan Cox  *
1076193b9358SAlan Cox  *	MADV_DONTNEED	(any object)
1077193b9358SAlan Cox  *
1078193b9358SAlan Cox  *	    Deactivate the specified pages if they are resident.
1079193b9358SAlan Cox  *
1080193b9358SAlan Cox  *	MADV_FREE	(OBJT_DEFAULT/OBJT_SWAP objects,
1081193b9358SAlan Cox  *			 OBJ_ONEMAPPING only)
1082193b9358SAlan Cox  *
1083193b9358SAlan Cox  *	    Deactivate and clean the specified pages if they are
1084193b9358SAlan Cox  *	    resident.  This permits the process to reuse the pages
1085193b9358SAlan Cox  *	    without faulting or the kernel to reclaim the pages
1086193b9358SAlan Cox  *	    without I/O.
1087867a482dSJohn Dyson  */
1088867a482dSJohn Dyson void
108992a59946SJohn Baldwin vm_object_madvise(vm_object_t object, vm_pindex_t pindex, vm_pindex_t end,
109092a59946SJohn Baldwin     int advise)
1091867a482dSJohn Dyson {
109292a59946SJohn Baldwin 	vm_pindex_t tpindex;
109334567de7SAlan Cox 	vm_object_t backing_object, tobject;
1094867a482dSJohn Dyson 	vm_page_t m;
1095867a482dSJohn Dyson 
1096867a482dSJohn Dyson 	if (object == NULL)
1097867a482dSJohn Dyson 		return;
109889f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
10991c7c3c6aSMatthew Dillon 	/*
11001c7c3c6aSMatthew Dillon 	 * Locate and adjust resident pages
11011c7c3c6aSMatthew Dillon 	 */
11021c7c3c6aSMatthew Dillon 	for (; pindex < end; pindex += 1) {
11036e20a165SJohn Dyson relookup:
11046e20a165SJohn Dyson 		tobject = object;
11056e20a165SJohn Dyson 		tpindex = pindex;
11066e20a165SJohn Dyson shadowlookup:
110758b4e6ccSAlan Cox 		/*
110858b4e6ccSAlan Cox 		 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
110958b4e6ccSAlan Cox 		 * and those pages must be OBJ_ONEMAPPING.
111058b4e6ccSAlan Cox 		 */
111158b4e6ccSAlan Cox 		if (advise == MADV_FREE) {
111258b4e6ccSAlan Cox 			if ((tobject->type != OBJT_DEFAULT &&
111358b4e6ccSAlan Cox 			     tobject->type != OBJT_SWAP) ||
111458b4e6ccSAlan Cox 			    (tobject->flags & OBJ_ONEMAPPING) == 0) {
111534567de7SAlan Cox 				goto unlock_tobject;
11166e20a165SJohn Dyson 			}
111728634820SAlan Cox 		} else if ((tobject->flags & OBJ_UNMANAGED) != 0)
11186a2a3d73SAlan Cox 			goto unlock_tobject;
11191c7c3c6aSMatthew Dillon 		m = vm_page_lookup(tobject, tpindex);
11207bfda801SAlan Cox 		if (m == NULL && advise == MADV_WILLNEED) {
11217bfda801SAlan Cox 			/*
11227bfda801SAlan Cox 			 * If the page is cached, reactivate it.
11237bfda801SAlan Cox 			 */
1124f3a2ed4bSAlan Cox 			m = vm_page_alloc(tobject, tpindex, VM_ALLOC_IFCACHED |
1125f3a2ed4bSAlan Cox 			    VM_ALLOC_NOBUSY);
11267bfda801SAlan Cox 		}
11271c7c3c6aSMatthew Dillon 		if (m == NULL) {
11281ce137beSMatthew Dillon 			/*
11291ce137beSMatthew Dillon 			 * There may be swap even if there is no backing page
11301ce137beSMatthew Dillon 			 */
11311ce137beSMatthew Dillon 			if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
11321ce137beSMatthew Dillon 				swap_pager_freespace(tobject, tpindex, 1);
11331ce137beSMatthew Dillon 			/*
11341ce137beSMatthew Dillon 			 * next object
11351ce137beSMatthew Dillon 			 */
113634567de7SAlan Cox 			backing_object = tobject->backing_object;
113734567de7SAlan Cox 			if (backing_object == NULL)
113834567de7SAlan Cox 				goto unlock_tobject;
113989f6b863SAttilio Rao 			VM_OBJECT_WLOCK(backing_object);
114056e0670fSAlan Cox 			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
11419b98b796SAlan Cox 			if (tobject != object)
114289f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(tobject);
114334567de7SAlan Cox 			tobject = backing_object;
11446e20a165SJohn Dyson 			goto shadowlookup;
11456a2a3d73SAlan Cox 		} else if (m->valid != VM_PAGE_BITS_ALL)
11466a2a3d73SAlan Cox 			goto unlock_tobject;
1147867a482dSJohn Dyson 		/*
11486a2a3d73SAlan Cox 		 * If the page is not in a normal state, skip it.
1149867a482dSJohn Dyson 		 */
11502965a453SKip Macy 		vm_page_lock(m);
11516a2a3d73SAlan Cox 		if (m->hold_count != 0 || m->wire_count != 0) {
11522965a453SKip Macy 			vm_page_unlock(m);
115334567de7SAlan Cox 			goto unlock_tobject;
11546e20a165SJohn Dyson 		}
1155d98d0ce2SKonstantin Belousov 		KASSERT((m->flags & PG_FICTITIOUS) == 0,
1156d98d0ce2SKonstantin Belousov 		    ("vm_object_madvise: page %p is fictitious", m));
1157d98d0ce2SKonstantin Belousov 		KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1158567e51e1SAlan Cox 		    ("vm_object_madvise: page %p is not managed", m));
11599af80719SAlan Cox 		if ((m->oflags & VPO_BUSY) || m->busy) {
1160567e51e1SAlan Cox 			if (advise == MADV_WILLNEED) {
1161b11b56b5SAlan Cox 				/*
1162b11b56b5SAlan Cox 				 * Reference the page before unlocking and
1163b11b56b5SAlan Cox 				 * sleeping so that the page daemon is less
1164b11b56b5SAlan Cox 				 * likely to reclaim it.
1165b11b56b5SAlan Cox 				 */
11663407fefeSKonstantin Belousov 				vm_page_aflag_set(m, PGA_REFERENCED);
1167567e51e1SAlan Cox 			}
11682965a453SKip Macy 			vm_page_unlock(m);
11699b98b796SAlan Cox 			if (object != tobject)
117089f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(object);
11715786be7cSAlan Cox 			m->oflags |= VPO_WANTED;
11720dde287bSAttilio Rao 			VM_OBJECT_SLEEP(tobject, m, PDROP | PVM, "madvpo", 0);
117389f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
11746e20a165SJohn Dyson   			goto relookup;
117534567de7SAlan Cox 		}
1176867a482dSJohn Dyson 		if (advise == MADV_WILLNEED) {
1177867a482dSJohn Dyson 			vm_page_activate(m);
11782051980fSAlan Cox 		} else {
11792051980fSAlan Cox 			vm_page_advise(m, advise);
1180867a482dSJohn Dyson 		}
11812965a453SKip Macy 		vm_page_unlock(m);
11822999e9faSAlan Cox 		if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
11832999e9faSAlan Cox 			swap_pager_freespace(tobject, tpindex, 1);
118434567de7SAlan Cox unlock_tobject:
11859b98b796SAlan Cox 		if (tobject != object)
118689f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(tobject);
1187867a482dSJohn Dyson 	}
118889f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
1189867a482dSJohn Dyson }
1190867a482dSJohn Dyson 
1191867a482dSJohn Dyson /*
1192df8bae1dSRodney W. Grimes  *	vm_object_shadow:
1193df8bae1dSRodney W. Grimes  *
1194df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
1195df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
1196df8bae1dSRodney W. Grimes  *	object reference is deallocated.
1197df8bae1dSRodney W. Grimes  *
1198df8bae1dSRodney W. Grimes  *	The new object and offset into that object
1199df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
1200df8bae1dSRodney W. Grimes  */
120126f9a767SRodney W. Grimes void
12021b40f8c0SMatthew Dillon vm_object_shadow(
12031b40f8c0SMatthew Dillon 	vm_object_t *object,	/* IN/OUT */
12041b40f8c0SMatthew Dillon 	vm_ooffset_t *offset,	/* IN/OUT */
12051b40f8c0SMatthew Dillon 	vm_size_t length)
1206df8bae1dSRodney W. Grimes {
1207d031cff1SMatthew Dillon 	vm_object_t source;
1208d031cff1SMatthew Dillon 	vm_object_t result;
1209df8bae1dSRodney W. Grimes 
1210df8bae1dSRodney W. Grimes 	source = *object;
1211df8bae1dSRodney W. Grimes 
1212df8bae1dSRodney W. Grimes 	/*
12139a2f6362SAlan Cox 	 * Don't create the new object if the old object isn't shared.
12149a2f6362SAlan Cox 	 */
1215570a2f4aSAlan Cox 	if (source != NULL) {
121689f6b863SAttilio Rao 		VM_OBJECT_WLOCK(source);
1217570a2f4aSAlan Cox 		if (source->ref_count == 1 &&
12189a2f6362SAlan Cox 		    source->handle == NULL &&
12199a2f6362SAlan Cox 		    (source->type == OBJT_DEFAULT ||
12209917e010SAlan Cox 		     source->type == OBJT_SWAP)) {
122189f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(source);
12229a2f6362SAlan Cox 			return;
12239917e010SAlan Cox 		}
122489f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(source);
1225570a2f4aSAlan Cox 	}
12269a2f6362SAlan Cox 
12279a2f6362SAlan Cox 	/*
1228570a2f4aSAlan Cox 	 * Allocate a new object with the given length.
1229df8bae1dSRodney W. Grimes 	 */
12300cc74f14SAlan Cox 	result = vm_object_allocate(OBJT_DEFAULT, atop(length));
1231df8bae1dSRodney W. Grimes 
1232df8bae1dSRodney W. Grimes 	/*
12330d94caffSDavid Greenman 	 * The new object shadows the source object, adding a reference to it.
12340d94caffSDavid Greenman 	 * Our caller changes his reference to point to the new object,
12350d94caffSDavid Greenman 	 * removing a reference to the source object.  Net result: no change
12360d94caffSDavid Greenman 	 * of reference count.
12379b09fe24SMatthew Dillon 	 *
12389b09fe24SMatthew Dillon 	 * Try to optimize the result object's page color when shadowing
1239956f3135SPhilippe Charnier 	 * in order to maintain page coloring consistency in the combined
12409b09fe24SMatthew Dillon 	 * shadowed object.
1241df8bae1dSRodney W. Grimes 	 */
124224a1cce3SDavid Greenman 	result->backing_object = source;
12439174ca7bSTor Egge 	/*
12449174ca7bSTor Egge 	 * Store the offset into the source object, and fix up the offset into
12459174ca7bSTor Egge 	 * the new object.
12469174ca7bSTor Egge 	 */
12479174ca7bSTor Egge 	result->backing_object_offset = *offset;
1248570a2f4aSAlan Cox 	if (source != NULL) {
124989f6b863SAttilio Rao 		VM_OBJECT_WLOCK(source);
12501c500307SAlan Cox 		LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1251eaf13dd7SJohn Dyson 		source->shadow_count++;
1252f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
12537b54b1a9SAlan Cox 		result->flags |= source->flags & OBJ_COLORED;
1254f8a47341SAlan Cox 		result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) &
1255f8a47341SAlan Cox 		    ((1 << (VM_NFREEORDER - 1)) - 1);
1256f8a47341SAlan Cox #endif
125789f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(source);
1258de5f6a77SJohn Dyson 	}
1259df8bae1dSRodney W. Grimes 
1260df8bae1dSRodney W. Grimes 
1261df8bae1dSRodney W. Grimes 	/*
1262df8bae1dSRodney W. Grimes 	 * Return the new things
1263df8bae1dSRodney W. Grimes 	 */
1264df8bae1dSRodney W. Grimes 	*offset = 0;
1265df8bae1dSRodney W. Grimes 	*object = result;
1266df8bae1dSRodney W. Grimes }
1267df8bae1dSRodney W. Grimes 
1268c5aaa06dSAlan Cox /*
1269c5aaa06dSAlan Cox  *	vm_object_split:
1270c5aaa06dSAlan Cox  *
1271c5aaa06dSAlan Cox  * Split the pages in a map entry into a new object.  This affords
1272c5aaa06dSAlan Cox  * easier removal of unused pages, and keeps object inheritance from
1273c5aaa06dSAlan Cox  * being a negative impact on memory usage.
1274c5aaa06dSAlan Cox  */
1275c5aaa06dSAlan Cox void
1276c5aaa06dSAlan Cox vm_object_split(vm_map_entry_t entry)
1277c5aaa06dSAlan Cox {
127873000556SAlan Cox 	vm_page_t m, m_next;
1279c5aaa06dSAlan Cox 	vm_object_t orig_object, new_object, source;
128073000556SAlan Cox 	vm_pindex_t idx, offidxstart;
128173000556SAlan Cox 	vm_size_t size;
1282c5aaa06dSAlan Cox 
1283c5aaa06dSAlan Cox 	orig_object = entry->object.vm_object;
1284c5aaa06dSAlan Cox 	if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1285c5aaa06dSAlan Cox 		return;
1286c5aaa06dSAlan Cox 	if (orig_object->ref_count <= 1)
1287c5aaa06dSAlan Cox 		return;
128889f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(orig_object);
1289c5aaa06dSAlan Cox 
12904da9f125SAlan Cox 	offidxstart = OFF_TO_IDX(entry->offset);
129195442adfSAlan Cox 	size = atop(entry->end - entry->start);
1292c5aaa06dSAlan Cox 
12934da9f125SAlan Cox 	/*
12944da9f125SAlan Cox 	 * If swap_pager_copy() is later called, it will convert new_object
12954da9f125SAlan Cox 	 * into a swap object.
12964da9f125SAlan Cox 	 */
12974da9f125SAlan Cox 	new_object = vm_object_allocate(OBJT_DEFAULT, size);
1298c5aaa06dSAlan Cox 
1299c5474b8fSAlan Cox 	/*
1300c5474b8fSAlan Cox 	 * At this point, the new object is still private, so the order in
1301c5474b8fSAlan Cox 	 * which the original and new objects are locked does not matter.
1302c5474b8fSAlan Cox 	 */
130389f6b863SAttilio Rao 	VM_OBJECT_WLOCK(new_object);
130489f6b863SAttilio Rao 	VM_OBJECT_WLOCK(orig_object);
1305c5aaa06dSAlan Cox 	source = orig_object->backing_object;
1306c5aaa06dSAlan Cox 	if (source != NULL) {
130789f6b863SAttilio Rao 		VM_OBJECT_WLOCK(source);
130819c244d0SAlan Cox 		if ((source->flags & OBJ_DEAD) != 0) {
130989f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(source);
131089f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(orig_object);
131189f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(new_object);
131219c244d0SAlan Cox 			vm_object_deallocate(new_object);
131389f6b863SAttilio Rao 			VM_OBJECT_WLOCK(orig_object);
131419c244d0SAlan Cox 			return;
131519c244d0SAlan Cox 		}
13161c500307SAlan Cox 		LIST_INSERT_HEAD(&source->shadow_head,
1317c5aaa06dSAlan Cox 				  new_object, shadow_list);
13188e3a76fbSAlan Cox 		source->shadow_count++;
1319b921a12bSAlan Cox 		vm_object_reference_locked(source);	/* for new_object */
1320c5aaa06dSAlan Cox 		vm_object_clear_flag(source, OBJ_ONEMAPPING);
132189f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(source);
1322c5aaa06dSAlan Cox 		new_object->backing_object_offset =
13234da9f125SAlan Cox 			orig_object->backing_object_offset + entry->offset;
1324c5aaa06dSAlan Cox 		new_object->backing_object = source;
1325c5aaa06dSAlan Cox 	}
1326ef694c1aSEdward Tomasz Napierala 	if (orig_object->cred != NULL) {
1327ef694c1aSEdward Tomasz Napierala 		new_object->cred = orig_object->cred;
1328ef694c1aSEdward Tomasz Napierala 		crhold(orig_object->cred);
13293364c323SKonstantin Belousov 		new_object->charge = ptoa(size);
13303364c323SKonstantin Belousov 		KASSERT(orig_object->charge >= ptoa(size),
13313364c323SKonstantin Belousov 		    ("orig_object->charge < 0"));
13323364c323SKonstantin Belousov 		orig_object->charge -= ptoa(size);
13333364c323SKonstantin Belousov 	}
1334c5aaa06dSAlan Cox retry:
1335b382c10aSKonstantin Belousov 	m = vm_page_find_least(orig_object, offidxstart);
133673000556SAlan Cox 	for (; m != NULL && (idx = m->pindex - offidxstart) < size;
133773000556SAlan Cox 	    m = m_next) {
133873000556SAlan Cox 		m_next = TAILQ_NEXT(m, listq);
1339c5aaa06dSAlan Cox 
1340c5aaa06dSAlan Cox 		/*
1341c5aaa06dSAlan Cox 		 * We must wait for pending I/O to complete before we can
1342c5aaa06dSAlan Cox 		 * rename the page.
1343c5aaa06dSAlan Cox 		 *
1344c5aaa06dSAlan Cox 		 * We do not have to VM_PROT_NONE the page as mappings should
1345c5aaa06dSAlan Cox 		 * not be changed by this operation.
1346c5aaa06dSAlan Cox 		 */
13479af80719SAlan Cox 		if ((m->oflags & VPO_BUSY) || m->busy) {
134889f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(new_object);
13495786be7cSAlan Cox 			m->oflags |= VPO_WANTED;
13500dde287bSAttilio Rao 			VM_OBJECT_SLEEP(orig_object, m, PVM, "spltwt", 0);
135189f6b863SAttilio Rao 			VM_OBJECT_WLOCK(new_object);
1352c5aaa06dSAlan Cox 			goto retry;
1353de33beddSAlan Cox 		}
1354b5f359b7SAlan Cox #if VM_NRESERVLEVEL > 0
1355b5f359b7SAlan Cox 		/*
1356b5f359b7SAlan Cox 		 * If some of the reservation's allocated pages remain with
1357b5f359b7SAlan Cox 		 * the original object, then transferring the reservation to
1358b5f359b7SAlan Cox 		 * the new object is neither particularly beneficial nor
1359b5f359b7SAlan Cox 		 * particularly harmful as compared to leaving the reservation
1360b5f359b7SAlan Cox 		 * with the original object.  If, however, all of the
1361b5f359b7SAlan Cox 		 * reservation's allocated pages are transferred to the new
1362b5f359b7SAlan Cox 		 * object, then transferring the reservation is typically
1363b5f359b7SAlan Cox 		 * beneficial.  Determining which of these two cases applies
1364b5f359b7SAlan Cox 		 * would be more costly than unconditionally renaming the
1365b5f359b7SAlan Cox 		 * reservation.
1366b5f359b7SAlan Cox 		 */
1367b5f359b7SAlan Cox 		vm_reserv_rename(m, new_object, orig_object, offidxstart);
1368b5f359b7SAlan Cox #endif
13692965a453SKip Macy 		vm_page_lock(m);
1370c5aaa06dSAlan Cox 		vm_page_rename(m, new_object, idx);
13712965a453SKip Macy 		vm_page_unlock(m);
1372c5aaa06dSAlan Cox 		/* page automatically made dirty by rename and cache handled */
1373dfd55c0cSAttilio Rao 		if (orig_object->type == OBJT_SWAP)
1374c5aaa06dSAlan Cox 			vm_page_busy(m);
1375c5aaa06dSAlan Cox 	}
1376d7a013c3SAlan Cox 	if (orig_object->type == OBJT_SWAP) {
1377c5aaa06dSAlan Cox 		/*
1378c7c8dd7eSAlan Cox 		 * swap_pager_copy() can sleep, in which case the orig_object's
1379c7c8dd7eSAlan Cox 		 * and new_object's locks are released and reacquired.
1380c5aaa06dSAlan Cox 		 */
1381c5aaa06dSAlan Cox 		swap_pager_copy(orig_object, new_object, offidxstart, 0);
1382dfd55c0cSAttilio Rao 		TAILQ_FOREACH(m, &new_object->memq, listq)
1383dfd55c0cSAttilio Rao 			vm_page_wakeup(m);
13847bfda801SAlan Cox 
13857bfda801SAlan Cox 		/*
13867bfda801SAlan Cox 		 * Transfer any cached pages from orig_object to new_object.
1387571a1e92SAttilio Rao 		 * If swap_pager_copy() found swapped out pages within the
1388571a1e92SAttilio Rao 		 * specified range of orig_object, then it changed
1389571a1e92SAttilio Rao 		 * new_object's type to OBJT_SWAP when it transferred those
1390571a1e92SAttilio Rao 		 * pages to new_object.  Otherwise, new_object's type
1391571a1e92SAttilio Rao 		 * should still be OBJT_DEFAULT and orig_object should not
1392571a1e92SAttilio Rao 		 * contain any cached pages within the specified range.
13937bfda801SAlan Cox 		 */
1394c9341161SAttilio Rao 		if (__predict_false(!vm_object_cache_is_empty(orig_object)))
13957bfda801SAlan Cox 			vm_page_cache_transfer(orig_object, offidxstart,
13967bfda801SAlan Cox 			    new_object);
1397c5aaa06dSAlan Cox 	}
139889f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(orig_object);
139989f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(new_object);
1400c5aaa06dSAlan Cox 	entry->object.vm_object = new_object;
1401c5aaa06dSAlan Cox 	entry->offset = 0LL;
1402c5aaa06dSAlan Cox 	vm_object_deallocate(orig_object);
140389f6b863SAttilio Rao 	VM_OBJECT_WLOCK(new_object);
1404c5aaa06dSAlan Cox }
1405c5aaa06dSAlan Cox 
14062ad1a3f7SMatthew Dillon #define	OBSC_TEST_ALL_SHADOWED	0x0001
14072ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_NOWAIT	0x0002
14082ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_WAIT	0x0004
14092ad1a3f7SMatthew Dillon 
1410b4ae4780SPoul-Henning Kamp static int
14112ad1a3f7SMatthew Dillon vm_object_backing_scan(vm_object_t object, int op)
14122ad1a3f7SMatthew Dillon {
14132ad1a3f7SMatthew Dillon 	int r = 1;
14142ad1a3f7SMatthew Dillon 	vm_page_t p;
14152ad1a3f7SMatthew Dillon 	vm_object_t backing_object;
14162ad1a3f7SMatthew Dillon 	vm_pindex_t backing_offset_index;
14172ad1a3f7SMatthew Dillon 
141889f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
141989f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object->backing_object);
14202ad1a3f7SMatthew Dillon 
14212ad1a3f7SMatthew Dillon 	backing_object = object->backing_object;
14222ad1a3f7SMatthew Dillon 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
14232ad1a3f7SMatthew Dillon 
14242ad1a3f7SMatthew Dillon 	/*
14252ad1a3f7SMatthew Dillon 	 * Initial conditions
14262ad1a3f7SMatthew Dillon 	 */
14272ad1a3f7SMatthew Dillon 	if (op & OBSC_TEST_ALL_SHADOWED) {
14282ad1a3f7SMatthew Dillon 		/*
14297bfda801SAlan Cox 		 * We do not want to have to test for the existence of cache
14307bfda801SAlan Cox 		 * or swap pages in the backing object.  XXX but with the
14312ad1a3f7SMatthew Dillon 		 * new swapper this would be pretty easy to do.
14322ad1a3f7SMatthew Dillon 		 *
14332ad1a3f7SMatthew Dillon 		 * XXX what about anonymous MAP_SHARED memory that hasn't
14342ad1a3f7SMatthew Dillon 		 * been ZFOD faulted yet?  If we do not test for this, the
14352ad1a3f7SMatthew Dillon 		 * shadow test may succeed! XXX
14362ad1a3f7SMatthew Dillon 		 */
14372ad1a3f7SMatthew Dillon 		if (backing_object->type != OBJT_DEFAULT) {
14382ad1a3f7SMatthew Dillon 			return (0);
14392ad1a3f7SMatthew Dillon 		}
14402ad1a3f7SMatthew Dillon 	}
14412ad1a3f7SMatthew Dillon 	if (op & OBSC_COLLAPSE_WAIT) {
14422ad1a3f7SMatthew Dillon 		vm_object_set_flag(backing_object, OBJ_DEAD);
14432ad1a3f7SMatthew Dillon 	}
14442ad1a3f7SMatthew Dillon 
14452ad1a3f7SMatthew Dillon 	/*
14462ad1a3f7SMatthew Dillon 	 * Our scan
14472ad1a3f7SMatthew Dillon 	 */
14482ad1a3f7SMatthew Dillon 	p = TAILQ_FIRST(&backing_object->memq);
14492ad1a3f7SMatthew Dillon 	while (p) {
14502ad1a3f7SMatthew Dillon 		vm_page_t next = TAILQ_NEXT(p, listq);
14512ad1a3f7SMatthew Dillon 		vm_pindex_t new_pindex = p->pindex - backing_offset_index;
14522ad1a3f7SMatthew Dillon 
14532ad1a3f7SMatthew Dillon 		if (op & OBSC_TEST_ALL_SHADOWED) {
14542ad1a3f7SMatthew Dillon 			vm_page_t pp;
14552ad1a3f7SMatthew Dillon 
14562ad1a3f7SMatthew Dillon 			/*
14572ad1a3f7SMatthew Dillon 			 * Ignore pages outside the parent object's range
14582ad1a3f7SMatthew Dillon 			 * and outside the parent object's mapping of the
14592ad1a3f7SMatthew Dillon 			 * backing object.
14602ad1a3f7SMatthew Dillon 			 *
14612ad1a3f7SMatthew Dillon 			 * note that we do not busy the backing object's
14622ad1a3f7SMatthew Dillon 			 * page.
14632ad1a3f7SMatthew Dillon 			 */
14642ad1a3f7SMatthew Dillon 			if (
14652ad1a3f7SMatthew Dillon 			    p->pindex < backing_offset_index ||
14662ad1a3f7SMatthew Dillon 			    new_pindex >= object->size
14672ad1a3f7SMatthew Dillon 			) {
14682ad1a3f7SMatthew Dillon 				p = next;
14692ad1a3f7SMatthew Dillon 				continue;
14702ad1a3f7SMatthew Dillon 			}
14712ad1a3f7SMatthew Dillon 
14722ad1a3f7SMatthew Dillon 			/*
14732ad1a3f7SMatthew Dillon 			 * See if the parent has the page or if the parent's
14742ad1a3f7SMatthew Dillon 			 * object pager has the page.  If the parent has the
14752ad1a3f7SMatthew Dillon 			 * page but the page is not valid, the parent's
14762ad1a3f7SMatthew Dillon 			 * object pager must have the page.
14772ad1a3f7SMatthew Dillon 			 *
14782ad1a3f7SMatthew Dillon 			 * If this fails, the parent does not completely shadow
14792ad1a3f7SMatthew Dillon 			 * the object and we might as well give up now.
14802ad1a3f7SMatthew Dillon 			 */
14812ad1a3f7SMatthew Dillon 
14822ad1a3f7SMatthew Dillon 			pp = vm_page_lookup(object, new_pindex);
14832ad1a3f7SMatthew Dillon 			if (
14842ad1a3f7SMatthew Dillon 			    (pp == NULL || pp->valid == 0) &&
14852ad1a3f7SMatthew Dillon 			    !vm_pager_has_page(object, new_pindex, NULL, NULL)
14862ad1a3f7SMatthew Dillon 			) {
14872ad1a3f7SMatthew Dillon 				r = 0;
14882ad1a3f7SMatthew Dillon 				break;
14892ad1a3f7SMatthew Dillon 			}
14902ad1a3f7SMatthew Dillon 		}
14912ad1a3f7SMatthew Dillon 
14922ad1a3f7SMatthew Dillon 		/*
14932ad1a3f7SMatthew Dillon 		 * Check for busy page
14942ad1a3f7SMatthew Dillon 		 */
14952ad1a3f7SMatthew Dillon 		if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
14962ad1a3f7SMatthew Dillon 			vm_page_t pp;
14972ad1a3f7SMatthew Dillon 
14982ad1a3f7SMatthew Dillon 			if (op & OBSC_COLLAPSE_NOWAIT) {
14999af80719SAlan Cox 				if ((p->oflags & VPO_BUSY) ||
15002ad1a3f7SMatthew Dillon 				    !p->valid ||
150100f9e8b4SAlan Cox 				    p->busy) {
15022ad1a3f7SMatthew Dillon 					p = next;
15032ad1a3f7SMatthew Dillon 					continue;
15042ad1a3f7SMatthew Dillon 				}
15052ad1a3f7SMatthew Dillon 			} else if (op & OBSC_COLLAPSE_WAIT) {
15069af80719SAlan Cox 				if ((p->oflags & VPO_BUSY) || p->busy) {
150789f6b863SAttilio Rao 					VM_OBJECT_WUNLOCK(object);
15085786be7cSAlan Cox 					p->oflags |= VPO_WANTED;
15090dde287bSAttilio Rao 					VM_OBJECT_SLEEP(backing_object, p,
15107ca33ad1SAlan Cox 					    PDROP | PVM, "vmocol", 0);
151189f6b863SAttilio Rao 					VM_OBJECT_WLOCK(object);
151289f6b863SAttilio Rao 					VM_OBJECT_WLOCK(backing_object);
15132ad1a3f7SMatthew Dillon 					/*
15142ad1a3f7SMatthew Dillon 					 * If we slept, anything could have
15152ad1a3f7SMatthew Dillon 					 * happened.  Since the object is
15162ad1a3f7SMatthew Dillon 					 * marked dead, the backing offset
15172ad1a3f7SMatthew Dillon 					 * should not have changed so we
15182ad1a3f7SMatthew Dillon 					 * just restart our scan.
15192ad1a3f7SMatthew Dillon 					 */
15202ad1a3f7SMatthew Dillon 					p = TAILQ_FIRST(&backing_object->memq);
15212ad1a3f7SMatthew Dillon 					continue;
15222ad1a3f7SMatthew Dillon 				}
15232ad1a3f7SMatthew Dillon 			}
15242ad1a3f7SMatthew Dillon 
15252ad1a3f7SMatthew Dillon 			KASSERT(
15262ad1a3f7SMatthew Dillon 			    p->object == backing_object,
15278e99783bSAlan Cox 			    ("vm_object_backing_scan: object mismatch")
15282ad1a3f7SMatthew Dillon 			);
15292ad1a3f7SMatthew Dillon 
15302ad1a3f7SMatthew Dillon 			/*
15312ad1a3f7SMatthew Dillon 			 * Destroy any associated swap
15322ad1a3f7SMatthew Dillon 			 */
15332ad1a3f7SMatthew Dillon 			if (backing_object->type == OBJT_SWAP) {
15342ad1a3f7SMatthew Dillon 				swap_pager_freespace(
15352ad1a3f7SMatthew Dillon 				    backing_object,
15362ad1a3f7SMatthew Dillon 				    p->pindex,
15372ad1a3f7SMatthew Dillon 				    1
15382ad1a3f7SMatthew Dillon 				);
15392ad1a3f7SMatthew Dillon 			}
15402ad1a3f7SMatthew Dillon 
15412ad1a3f7SMatthew Dillon 			if (
15422ad1a3f7SMatthew Dillon 			    p->pindex < backing_offset_index ||
15432ad1a3f7SMatthew Dillon 			    new_pindex >= object->size
15442ad1a3f7SMatthew Dillon 			) {
15452ad1a3f7SMatthew Dillon 				/*
15462ad1a3f7SMatthew Dillon 				 * Page is out of the parent object's range, we
15472ad1a3f7SMatthew Dillon 				 * can simply destroy it.
15482ad1a3f7SMatthew Dillon 				 */
15492965a453SKip Macy 				vm_page_lock(p);
1550f6d89838SAlan Cox 				KASSERT(!pmap_page_is_mapped(p),
1551f6d89838SAlan Cox 				    ("freeing mapped page %p", p));
1552f6d89838SAlan Cox 				if (p->wire_count == 0)
15532ad1a3f7SMatthew Dillon 					vm_page_free(p);
1554f6d89838SAlan Cox 				else
1555f6d89838SAlan Cox 					vm_page_remove(p);
15562965a453SKip Macy 				vm_page_unlock(p);
15572ad1a3f7SMatthew Dillon 				p = next;
15582ad1a3f7SMatthew Dillon 				continue;
15592ad1a3f7SMatthew Dillon 			}
15602ad1a3f7SMatthew Dillon 
15612ad1a3f7SMatthew Dillon 			pp = vm_page_lookup(object, new_pindex);
15622ad1a3f7SMatthew Dillon 			if (
1563e18cc7bfSMax Laier 			    (op & OBSC_COLLAPSE_NOWAIT) != 0 &&
1564e18cc7bfSMax Laier 			    (pp != NULL && pp->valid == 0)
1565e18cc7bfSMax Laier 			) {
1566e18cc7bfSMax Laier 				/*
1567e18cc7bfSMax Laier 				 * The page in the parent is not (yet) valid.
1568e18cc7bfSMax Laier 				 * We don't know anything about the state of
1569e18cc7bfSMax Laier 				 * the original page.  It might be mapped,
1570e18cc7bfSMax Laier 				 * so we must avoid the next if here.
1571e18cc7bfSMax Laier 				 *
1572e18cc7bfSMax Laier 				 * This is due to a race in vm_fault() where
1573e18cc7bfSMax Laier 				 * we must unbusy the original (backing_obj)
1574e18cc7bfSMax Laier 				 * page before we can (re)lock the parent.
1575e18cc7bfSMax Laier 				 * Hence we can get here.
1576e18cc7bfSMax Laier 				 */
1577e18cc7bfSMax Laier 				p = next;
1578e18cc7bfSMax Laier 				continue;
1579e18cc7bfSMax Laier 			}
1580e18cc7bfSMax Laier 			if (
15812ad1a3f7SMatthew Dillon 			    pp != NULL ||
15822ad1a3f7SMatthew Dillon 			    vm_pager_has_page(object, new_pindex, NULL, NULL)
15832ad1a3f7SMatthew Dillon 			) {
15842ad1a3f7SMatthew Dillon 				/*
15852ad1a3f7SMatthew Dillon 				 * page already exists in parent OR swap exists
15862ad1a3f7SMatthew Dillon 				 * for this location in the parent.  Destroy
15872ad1a3f7SMatthew Dillon 				 * the original page from the backing object.
15882ad1a3f7SMatthew Dillon 				 *
15892ad1a3f7SMatthew Dillon 				 * Leave the parent's page alone
15902ad1a3f7SMatthew Dillon 				 */
15912965a453SKip Macy 				vm_page_lock(p);
1592f6d89838SAlan Cox 				KASSERT(!pmap_page_is_mapped(p),
1593f6d89838SAlan Cox 				    ("freeing mapped page %p", p));
1594f6d89838SAlan Cox 				if (p->wire_count == 0)
15952ad1a3f7SMatthew Dillon 					vm_page_free(p);
1596f6d89838SAlan Cox 				else
1597f6d89838SAlan Cox 					vm_page_remove(p);
15982965a453SKip Macy 				vm_page_unlock(p);
15992ad1a3f7SMatthew Dillon 				p = next;
16002ad1a3f7SMatthew Dillon 				continue;
16012ad1a3f7SMatthew Dillon 			}
16022ad1a3f7SMatthew Dillon 
1603f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
1604f8a47341SAlan Cox 			/*
1605f8a47341SAlan Cox 			 * Rename the reservation.
1606f8a47341SAlan Cox 			 */
1607f8a47341SAlan Cox 			vm_reserv_rename(p, object, backing_object,
1608f8a47341SAlan Cox 			    backing_offset_index);
1609f8a47341SAlan Cox #endif
1610f8a47341SAlan Cox 
16112ad1a3f7SMatthew Dillon 			/*
16122ad1a3f7SMatthew Dillon 			 * Page does not exist in parent, rename the
16132ad1a3f7SMatthew Dillon 			 * page from the backing object to the main object.
1614d1bf5d56SMatthew Dillon 			 *
1615d1bf5d56SMatthew Dillon 			 * If the page was mapped to a process, it can remain
1616d1bf5d56SMatthew Dillon 			 * mapped through the rename.
16172ad1a3f7SMatthew Dillon 			 */
16182965a453SKip Macy 			vm_page_lock(p);
16192ad1a3f7SMatthew Dillon 			vm_page_rename(p, object, new_pindex);
16202965a453SKip Macy 			vm_page_unlock(p);
16212ad1a3f7SMatthew Dillon 			/* page automatically made dirty by rename */
16222ad1a3f7SMatthew Dillon 		}
16232ad1a3f7SMatthew Dillon 		p = next;
16242ad1a3f7SMatthew Dillon 	}
16252ad1a3f7SMatthew Dillon 	return (r);
16262ad1a3f7SMatthew Dillon }
16272ad1a3f7SMatthew Dillon 
1628df8bae1dSRodney W. Grimes 
1629df8bae1dSRodney W. Grimes /*
16302fe6e4d7SDavid Greenman  * this version of collapse allows the operation to occur earlier and
16312fe6e4d7SDavid Greenman  * when paging_in_progress is true for an object...  This is not a complete
16322fe6e4d7SDavid Greenman  * operation, but should plug 99.9% of the rest of the leaks.
16332fe6e4d7SDavid Greenman  */
16342fe6e4d7SDavid Greenman static void
16351b40f8c0SMatthew Dillon vm_object_qcollapse(vm_object_t object)
16362fe6e4d7SDavid Greenman {
16372ad1a3f7SMatthew Dillon 	vm_object_t backing_object = object->backing_object;
16382fe6e4d7SDavid Greenman 
163989f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
164089f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(backing_object);
16411b40f8c0SMatthew Dillon 
16422fe6e4d7SDavid Greenman 	if (backing_object->ref_count != 1)
16432fe6e4d7SDavid Greenman 		return;
16442fe6e4d7SDavid Greenman 
16452ad1a3f7SMatthew Dillon 	vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
16462fe6e4d7SDavid Greenman }
16472fe6e4d7SDavid Greenman 
1648df8bae1dSRodney W. Grimes /*
1649df8bae1dSRodney W. Grimes  *	vm_object_collapse:
1650df8bae1dSRodney W. Grimes  *
1651df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
1652df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
1653df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
1654df8bae1dSRodney W. Grimes  */
165526f9a767SRodney W. Grimes void
16561b40f8c0SMatthew Dillon vm_object_collapse(vm_object_t object)
1657df8bae1dSRodney W. Grimes {
165889f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
165923955314SAlfred Perlstein 
1660df8bae1dSRodney W. Grimes 	while (TRUE) {
16612ad1a3f7SMatthew Dillon 		vm_object_t backing_object;
16622ad1a3f7SMatthew Dillon 
1663df8bae1dSRodney W. Grimes 		/*
1664df8bae1dSRodney W. Grimes 		 * Verify that the conditions are right for collapse:
1665df8bae1dSRodney W. Grimes 		 *
16662ad1a3f7SMatthew Dillon 		 * The object exists and the backing object exists.
1667df8bae1dSRodney W. Grimes 		 */
166824a1cce3SDavid Greenman 		if ((backing_object = object->backing_object) == NULL)
16692ad1a3f7SMatthew Dillon 			break;
1670df8bae1dSRodney W. Grimes 
1671f919ebdeSDavid Greenman 		/*
1672f919ebdeSDavid Greenman 		 * we check the backing object first, because it is most likely
167324a1cce3SDavid Greenman 		 * not collapsable.
1674f919ebdeSDavid Greenman 		 */
167589f6b863SAttilio Rao 		VM_OBJECT_WLOCK(backing_object);
167624a1cce3SDavid Greenman 		if (backing_object->handle != NULL ||
167724a1cce3SDavid Greenman 		    (backing_object->type != OBJT_DEFAULT &&
167824a1cce3SDavid Greenman 		     backing_object->type != OBJT_SWAP) ||
1679f919ebdeSDavid Greenman 		    (backing_object->flags & OBJ_DEAD) ||
168024a1cce3SDavid Greenman 		    object->handle != NULL ||
168124a1cce3SDavid Greenman 		    (object->type != OBJT_DEFAULT &&
168224a1cce3SDavid Greenman 		     object->type != OBJT_SWAP) ||
168324a1cce3SDavid Greenman 		    (object->flags & OBJ_DEAD)) {
168489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(backing_object);
16852ad1a3f7SMatthew Dillon 			break;
168624a1cce3SDavid Greenman 		}
16879b4814bbSDavid Greenman 
16882ad1a3f7SMatthew Dillon 		if (
16892ad1a3f7SMatthew Dillon 		    object->paging_in_progress != 0 ||
16902ad1a3f7SMatthew Dillon 		    backing_object->paging_in_progress != 0
16912ad1a3f7SMatthew Dillon 		) {
1692b9921222SDavid Greenman 			vm_object_qcollapse(object);
169389f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(backing_object);
16942ad1a3f7SMatthew Dillon 			break;
1695df8bae1dSRodney W. Grimes 		}
169626f9a767SRodney W. Grimes 		/*
16970d94caffSDavid Greenman 		 * We know that we can either collapse the backing object (if
16982ad1a3f7SMatthew Dillon 		 * the parent is the only reference to it) or (perhaps) have
16992ad1a3f7SMatthew Dillon 		 * the parent bypass the object if the parent happens to shadow
17002ad1a3f7SMatthew Dillon 		 * all the resident pages in the entire backing object.
17012ad1a3f7SMatthew Dillon 		 *
17022ad1a3f7SMatthew Dillon 		 * This is ignoring pager-backed pages such as swap pages.
17032ad1a3f7SMatthew Dillon 		 * vm_object_backing_scan fails the shadowing test in this
17042ad1a3f7SMatthew Dillon 		 * case.
1705df8bae1dSRodney W. Grimes 		 */
1706df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
1707df8bae1dSRodney W. Grimes 			/*
17082ad1a3f7SMatthew Dillon 			 * If there is exactly one reference to the backing
17092ad1a3f7SMatthew Dillon 			 * object, we can collapse it into the parent.
1710df8bae1dSRodney W. Grimes 			 */
17112ad1a3f7SMatthew Dillon 			vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1712df8bae1dSRodney W. Grimes 
1713f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
1714f8a47341SAlan Cox 			/*
1715f8a47341SAlan Cox 			 * Break any reservations from backing_object.
1716f8a47341SAlan Cox 			 */
1717f8a47341SAlan Cox 			if (__predict_false(!LIST_EMPTY(&backing_object->rvq)))
1718f8a47341SAlan Cox 				vm_reserv_break_all(backing_object);
1719f8a47341SAlan Cox #endif
1720f8a47341SAlan Cox 
1721df8bae1dSRodney W. Grimes 			/*
1722df8bae1dSRodney W. Grimes 			 * Move the pager from backing_object to object.
1723df8bae1dSRodney W. Grimes 			 */
17246be36525SAlan Cox 			if (backing_object->type == OBJT_SWAP) {
172524a1cce3SDavid Greenman 				/*
1726c7c8dd7eSAlan Cox 				 * swap_pager_copy() can sleep, in which case
1727c7c8dd7eSAlan Cox 				 * the backing_object's and object's locks are
1728c7c8dd7eSAlan Cox 				 * released and reacquired.
1729571a1e92SAttilio Rao 				 * Since swap_pager_copy() is being asked to
1730571a1e92SAttilio Rao 				 * destroy the source, it will change the
1731571a1e92SAttilio Rao 				 * backing_object's type to OBJT_DEFAULT.
173224a1cce3SDavid Greenman 				 */
17331c7c3c6aSMatthew Dillon 				swap_pager_copy(
17341c7c3c6aSMatthew Dillon 				    backing_object,
17351c7c3c6aSMatthew Dillon 				    object,
17361c7c3c6aSMatthew Dillon 				    OFF_TO_IDX(object->backing_object_offset), TRUE);
17377bfda801SAlan Cox 
17387bfda801SAlan Cox 				/*
17397bfda801SAlan Cox 				 * Free any cached pages from backing_object.
17407bfda801SAlan Cox 				 */
1741c9341161SAttilio Rao 				if (__predict_false(
1742c9341161SAttilio Rao 				    !vm_object_cache_is_empty(backing_object)))
1743c9444914SAlan Cox 					vm_page_cache_free(backing_object, 0, 0);
1744c0503609SDavid Greenman 			}
1745df8bae1dSRodney W. Grimes 			/*
1746df8bae1dSRodney W. Grimes 			 * Object now shadows whatever backing_object did.
17472ad1a3f7SMatthew Dillon 			 * Note that the reference to
17482ad1a3f7SMatthew Dillon 			 * backing_object->backing_object moves from within
17492ad1a3f7SMatthew Dillon 			 * backing_object to within object.
1750df8bae1dSRodney W. Grimes 			 */
17511c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
17524f7c7f6eSAlan Cox 			backing_object->shadow_count--;
1753de5f6a77SJohn Dyson 			if (backing_object->backing_object) {
175489f6b863SAttilio Rao 				VM_OBJECT_WLOCK(backing_object->backing_object);
17551c500307SAlan Cox 				LIST_REMOVE(backing_object, shadow_list);
175643186e53SAlan Cox 				LIST_INSERT_HEAD(
175743186e53SAlan Cox 				    &backing_object->backing_object->shadow_head,
175843186e53SAlan Cox 				    object, shadow_list);
175943186e53SAlan Cox 				/*
176043186e53SAlan Cox 				 * The shadow_count has not changed.
176143186e53SAlan Cox 				 */
176289f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(backing_object->backing_object);
1763de5f6a77SJohn Dyson 			}
176424a1cce3SDavid Greenman 			object->backing_object = backing_object->backing_object;
17652ad1a3f7SMatthew Dillon 			object->backing_object_offset +=
17662ad1a3f7SMatthew Dillon 			    backing_object->backing_object_offset;
17672ad1a3f7SMatthew Dillon 
1768df8bae1dSRodney W. Grimes 			/*
1769df8bae1dSRodney W. Grimes 			 * Discard backing_object.
1770df8bae1dSRodney W. Grimes 			 *
17710d94caffSDavid Greenman 			 * Since the backing object has no pages, no pager left,
17720d94caffSDavid Greenman 			 * and no object references within it, all that is
17730d94caffSDavid Greenman 			 * necessary is to dispose of it.
1774df8bae1dSRodney W. Grimes 			 */
17759b4d473aSKonstantin Belousov 			KASSERT(backing_object->ref_count == 1, (
17769b4d473aSKonstantin Belousov "backing_object %p was somehow re-referenced during collapse!",
17779b4d473aSKonstantin Belousov 			    backing_object));
177889f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(backing_object);
17799b4d473aSKonstantin Belousov 			vm_object_destroy(backing_object);
1780df8bae1dSRodney W. Grimes 
1781df8bae1dSRodney W. Grimes 			object_collapses++;
17820d94caffSDavid Greenman 		} else {
178395e5e988SJohn Dyson 			vm_object_t new_backing_object;
1784df8bae1dSRodney W. Grimes 
1785df8bae1dSRodney W. Grimes 			/*
17862ad1a3f7SMatthew Dillon 			 * If we do not entirely shadow the backing object,
17872ad1a3f7SMatthew Dillon 			 * there is nothing we can do so we give up.
1788df8bae1dSRodney W. Grimes 			 */
1789df59a0feSJeff Roberson 			if (object->resident_page_count != object->size &&
1790df59a0feSJeff Roberson 			    vm_object_backing_scan(object,
1791df59a0feSJeff Roberson 			    OBSC_TEST_ALL_SHADOWED) == 0) {
179289f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(backing_object);
17932ad1a3f7SMatthew Dillon 				break;
179424a1cce3SDavid Greenman 			}
1795df8bae1dSRodney W. Grimes 
1796df8bae1dSRodney W. Grimes 			/*
17970d94caffSDavid Greenman 			 * Make the parent shadow the next object in the
17980d94caffSDavid Greenman 			 * chain.  Deallocating backing_object will not remove
17990d94caffSDavid Greenman 			 * it, since its reference count is at least 2.
1800df8bae1dSRodney W. Grimes 			 */
18011c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
1802eaf13dd7SJohn Dyson 			backing_object->shadow_count--;
180395e5e988SJohn Dyson 
180495e5e988SJohn Dyson 			new_backing_object = backing_object->backing_object;
18058aef1712SMatthew Dillon 			if ((object->backing_object = new_backing_object) != NULL) {
180689f6b863SAttilio Rao 				VM_OBJECT_WLOCK(new_backing_object);
18071c500307SAlan Cox 				LIST_INSERT_HEAD(
18082ad1a3f7SMatthew Dillon 				    &new_backing_object->shadow_head,
18092ad1a3f7SMatthew Dillon 				    object,
18102ad1a3f7SMatthew Dillon 				    shadow_list
18112ad1a3f7SMatthew Dillon 				);
1812eaf13dd7SJohn Dyson 				new_backing_object->shadow_count++;
1813b921a12bSAlan Cox 				vm_object_reference_locked(new_backing_object);
181489f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(new_backing_object);
181595e5e988SJohn Dyson 				object->backing_object_offset +=
181695e5e988SJohn Dyson 					backing_object->backing_object_offset;
1817de5f6a77SJohn Dyson 			}
1818df8bae1dSRodney W. Grimes 
1819df8bae1dSRodney W. Grimes 			/*
18200d94caffSDavid Greenman 			 * Drop the reference count on backing_object. Since
182122ec553fSAlan Cox 			 * its ref_count was at least 2, it will not vanish.
1822df8bae1dSRodney W. Grimes 			 */
182322ec553fSAlan Cox 			backing_object->ref_count--;
182489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(backing_object);
1825df8bae1dSRodney W. Grimes 			object_bypasses++;
1826df8bae1dSRodney W. Grimes 		}
1827df8bae1dSRodney W. Grimes 
1828df8bae1dSRodney W. Grimes 		/*
1829df8bae1dSRodney W. Grimes 		 * Try again with this object's new backing object.
1830df8bae1dSRodney W. Grimes 		 */
1831df8bae1dSRodney W. Grimes 	}
1832df8bae1dSRodney W. Grimes }
1833df8bae1dSRodney W. Grimes 
1834df8bae1dSRodney W. Grimes /*
1835bff99f0dSAlan Cox  *	vm_object_page_remove:
1836df8bae1dSRodney W. Grimes  *
183768855966SAlan Cox  *	For the given object, either frees or invalidates each of the
18386bbee8e2SAlan Cox  *	specified pages.  In general, a page is freed.  However, if a page is
18396bbee8e2SAlan Cox  *	wired for any reason other than the existence of a managed, wired
18406bbee8e2SAlan Cox  *	mapping, then it may be invalidated but not removed from the object.
18416bbee8e2SAlan Cox  *	Pages are specified by the given range ["start", "end") and the option
18426bbee8e2SAlan Cox  *	OBJPR_CLEANONLY.  As a special case, if "end" is zero, then the range
18436bbee8e2SAlan Cox  *	extends from "start" to the end of the object.  If the option
18446bbee8e2SAlan Cox  *	OBJPR_CLEANONLY is specified, then only the non-dirty pages within the
18456bbee8e2SAlan Cox  *	specified range are affected.  If the option OBJPR_NOTMAPPED is
18466bbee8e2SAlan Cox  *	specified, then the pages within the specified range must have no
18476bbee8e2SAlan Cox  *	mappings.  Otherwise, if this option is not specified, any mappings to
18486bbee8e2SAlan Cox  *	the specified pages are removed before the pages are freed or
18496bbee8e2SAlan Cox  *	invalidated.
185068855966SAlan Cox  *
18516bbee8e2SAlan Cox  *	In general, this operation should only be performed on objects that
18526bbee8e2SAlan Cox  *	contain managed pages.  There are, however, two exceptions.  First, it
18536bbee8e2SAlan Cox  *	is performed on the kernel and kmem objects by vm_map_entry_delete().
18546bbee8e2SAlan Cox  *	Second, it is used by msync(..., MS_INVALIDATE) to invalidate device-
18556bbee8e2SAlan Cox  *	backed pages.  In both of these cases, the option OBJPR_CLEANONLY must
18566bbee8e2SAlan Cox  *	not be specified and the option OBJPR_NOTMAPPED must be specified.
1857df8bae1dSRodney W. Grimes  *
1858df8bae1dSRodney W. Grimes  *	The object must be locked.
1859df8bae1dSRodney W. Grimes  */
186026f9a767SRodney W. Grimes void
1861ecde4b32SAlan Cox vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
18626bbee8e2SAlan Cox     int options)
1863df8bae1dSRodney W. Grimes {
1864d031cff1SMatthew Dillon 	vm_page_t p, next;
186559677d3cSAlan Cox 	int wirings;
1866df8bae1dSRodney W. Grimes 
186789f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
186828634820SAlan Cox 	KASSERT((object->flags & OBJ_UNMANAGED) == 0 ||
18696bbee8e2SAlan Cox 	    (options & (OBJPR_CLEANONLY | OBJPR_NOTMAPPED)) == OBJPR_NOTMAPPED,
18706bbee8e2SAlan Cox 	    ("vm_object_page_remove: illegal options for object %p", object));
1871ecde4b32SAlan Cox 	if (object->resident_page_count == 0)
187225732691SAlan Cox 		goto skipmemq;
1873d474eaaaSDoug Rabson 	vm_object_pip_add(object, 1);
187426f9a767SRodney W. Grimes again:
1875b382c10aSKonstantin Belousov 	p = vm_page_find_least(object, start);
18762965a453SKip Macy 
187775741c04SAlan Cox 	/*
18786bbee8e2SAlan Cox 	 * Here, the variable "p" is either (1) the page with the least pindex
18796bbee8e2SAlan Cox 	 * greater than or equal to the parameter "start" or (2) NULL.
188075741c04SAlan Cox 	 */
18816bbee8e2SAlan Cox 	for (; p != NULL && (p->pindex < end || end == 0); p = next) {
1882b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
188375741c04SAlan Cox 
188459677d3cSAlan Cox 		/*
18856bbee8e2SAlan Cox 		 * If the page is wired for any reason besides the existence
18866bbee8e2SAlan Cox 		 * of managed, wired mappings, then it cannot be freed.  For
18876bbee8e2SAlan Cox 		 * example, fictitious pages, which represent device memory,
18886bbee8e2SAlan Cox 		 * are inherently wired and cannot be freed.  They can,
18896bbee8e2SAlan Cox 		 * however, be invalidated if the option OBJPR_CLEANONLY is
18906bbee8e2SAlan Cox 		 * not specified.
189159677d3cSAlan Cox 		 */
18922965a453SKip Macy 		vm_page_lock(p);
189359677d3cSAlan Cox 		if ((wirings = p->wire_count) != 0 &&
189459677d3cSAlan Cox 		    (wirings = pmap_page_wired_mappings(p)) != p->wire_count) {
1895ebf5d94eSKonstantin Belousov 			if ((options & (OBJPR_NOTWIRED | OBJPR_NOTMAPPED)) ==
1896ebf5d94eSKonstantin Belousov 			    0) {
18974fec79beSAlan Cox 				pmap_remove_all(p);
18986bbee8e2SAlan Cox 				/* Account for removal of wired mappings. */
18996bbee8e2SAlan Cox 				if (wirings != 0)
190059677d3cSAlan Cox 					p->wire_count -= wirings;
19016bbee8e2SAlan Cox 			}
19026bbee8e2SAlan Cox 			if ((options & OBJPR_CLEANONLY) == 0) {
1903bd7e5f99SJohn Dyson 				p->valid = 0;
1904a28042d1SAlan Cox 				vm_page_undirty(p);
1905a28042d1SAlan Cox 			}
1906ebf5d94eSKonstantin Belousov 			goto next;
19070d94caffSDavid Greenman 		}
190832585dd6SAlan Cox 		if (vm_page_sleep_if_busy(p, TRUE, "vmopar"))
190926f9a767SRodney W. Grimes 			goto again;
191068855966SAlan Cox 		KASSERT((p->flags & PG_FICTITIOUS) == 0,
191168855966SAlan Cox 		    ("vm_object_page_remove: page %p is fictitious", p));
19126bbee8e2SAlan Cox 		if ((options & OBJPR_CLEANONLY) != 0 && p->valid != 0) {
19136bbee8e2SAlan Cox 			if ((options & OBJPR_NOTMAPPED) == 0)
191478985e42SAlan Cox 				pmap_remove_write(p);
1915ebf5d94eSKonstantin Belousov 			if (p->dirty)
1916ebf5d94eSKonstantin Belousov 				goto next;
19172965a453SKip Macy 		}
19186bbee8e2SAlan Cox 		if ((options & OBJPR_NOTMAPPED) == 0) {
1919ebf5d94eSKonstantin Belousov 			if ((options & OBJPR_NOTWIRED) != 0 && wirings != 0)
1920ebf5d94eSKonstantin Belousov 				goto next;
19214fec79beSAlan Cox 			pmap_remove_all(p);
19226bbee8e2SAlan Cox 			/* Account for removal of wired mappings. */
19235f9c767bSKonstantin Belousov 			if (wirings != 0) {
19245f9c767bSKonstantin Belousov 				KASSERT(p->wire_count == wirings,
19255f9c767bSKonstantin Belousov 				    ("inconsistent wire count %d %d %p",
19265f9c767bSKonstantin Belousov 				    p->wire_count, wirings, p));
19275f9c767bSKonstantin Belousov 				p->wire_count = 0;
19285f9c767bSKonstantin Belousov 				atomic_subtract_int(&cnt.v_wire_count, 1);
19295f9c767bSKonstantin Belousov 			}
19306bbee8e2SAlan Cox 		}
1931df8bae1dSRodney W. Grimes 		vm_page_free(p);
1932ebf5d94eSKonstantin Belousov next:
19332965a453SKip Macy 		vm_page_unlock(p);
19342965a453SKip Macy 	}
1935f919ebdeSDavid Greenman 	vm_object_pip_wakeup(object);
193625732691SAlan Cox skipmemq:
1937c9341161SAttilio Rao 	if (__predict_false(!vm_object_cache_is_empty(object)))
1938c9444914SAlan Cox 		vm_page_cache_free(object, start, end);
1939c0503609SDavid Greenman }
1940df8bae1dSRodney W. Grimes 
1941df8bae1dSRodney W. Grimes /*
1942936c09acSJohn Baldwin  *	vm_object_page_cache:
1943936c09acSJohn Baldwin  *
1944936c09acSJohn Baldwin  *	For the given object, attempt to move the specified clean
1945936c09acSJohn Baldwin  *	pages to the cache queue.  If a page is wired for any reason,
1946936c09acSJohn Baldwin  *	then it will not be changed.  Pages are specified by the given
1947936c09acSJohn Baldwin  *	range ["start", "end").  As a special case, if "end" is zero,
1948936c09acSJohn Baldwin  *	then the range extends from "start" to the end of the object.
1949936c09acSJohn Baldwin  *	Any mappings to the specified pages are removed before the
1950936c09acSJohn Baldwin  *	pages are moved to the cache queue.
1951936c09acSJohn Baldwin  *
1952936c09acSJohn Baldwin  *	This operation should only be performed on objects that
195328634820SAlan Cox  *	contain non-fictitious, managed pages.
1954936c09acSJohn Baldwin  *
1955936c09acSJohn Baldwin  *	The object must be locked.
1956936c09acSJohn Baldwin  */
1957936c09acSJohn Baldwin void
1958936c09acSJohn Baldwin vm_object_page_cache(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1959936c09acSJohn Baldwin {
1960936c09acSJohn Baldwin 	struct mtx *mtx, *new_mtx;
1961936c09acSJohn Baldwin 	vm_page_t p, next;
1962936c09acSJohn Baldwin 
196389f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
196428634820SAlan Cox 	KASSERT((object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0,
1965936c09acSJohn Baldwin 	    ("vm_object_page_cache: illegal object %p", object));
1966936c09acSJohn Baldwin 	if (object->resident_page_count == 0)
1967936c09acSJohn Baldwin 		return;
1968936c09acSJohn Baldwin 	p = vm_page_find_least(object, start);
1969936c09acSJohn Baldwin 
1970936c09acSJohn Baldwin 	/*
1971936c09acSJohn Baldwin 	 * Here, the variable "p" is either (1) the page with the least pindex
1972936c09acSJohn Baldwin 	 * greater than or equal to the parameter "start" or (2) NULL.
1973936c09acSJohn Baldwin 	 */
1974936c09acSJohn Baldwin 	mtx = NULL;
1975936c09acSJohn Baldwin 	for (; p != NULL && (p->pindex < end || end == 0); p = next) {
1976936c09acSJohn Baldwin 		next = TAILQ_NEXT(p, listq);
1977936c09acSJohn Baldwin 
1978936c09acSJohn Baldwin 		/*
1979936c09acSJohn Baldwin 		 * Avoid releasing and reacquiring the same page lock.
1980936c09acSJohn Baldwin 		 */
1981936c09acSJohn Baldwin 		new_mtx = vm_page_lockptr(p);
1982936c09acSJohn Baldwin 		if (mtx != new_mtx) {
1983936c09acSJohn Baldwin 			if (mtx != NULL)
1984936c09acSJohn Baldwin 				mtx_unlock(mtx);
1985936c09acSJohn Baldwin 			mtx = new_mtx;
1986936c09acSJohn Baldwin 			mtx_lock(mtx);
1987936c09acSJohn Baldwin 		}
1988936c09acSJohn Baldwin 		vm_page_try_to_cache(p);
1989936c09acSJohn Baldwin 	}
1990936c09acSJohn Baldwin 	if (mtx != NULL)
1991936c09acSJohn Baldwin 		mtx_unlock(mtx);
1992936c09acSJohn Baldwin }
1993936c09acSJohn Baldwin 
1994936c09acSJohn Baldwin /*
1995387aabc5SAlan Cox  *	Populate the specified range of the object with valid pages.  Returns
1996387aabc5SAlan Cox  *	TRUE if the range is successfully populated and FALSE otherwise.
1997387aabc5SAlan Cox  *
1998387aabc5SAlan Cox  *	Note: This function should be optimized to pass a larger array of
1999387aabc5SAlan Cox  *	pages to vm_pager_get_pages() before it is applied to a non-
2000387aabc5SAlan Cox  *	OBJT_DEVICE object.
2001387aabc5SAlan Cox  *
2002387aabc5SAlan Cox  *	The object must be locked.
2003387aabc5SAlan Cox  */
2004387aabc5SAlan Cox boolean_t
2005387aabc5SAlan Cox vm_object_populate(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
2006387aabc5SAlan Cox {
2007387aabc5SAlan Cox 	vm_page_t m, ma[1];
2008387aabc5SAlan Cox 	vm_pindex_t pindex;
2009387aabc5SAlan Cox 	int rv;
2010387aabc5SAlan Cox 
201189f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
2012387aabc5SAlan Cox 	for (pindex = start; pindex < end; pindex++) {
2013387aabc5SAlan Cox 		m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL |
2014387aabc5SAlan Cox 		    VM_ALLOC_RETRY);
2015387aabc5SAlan Cox 		if (m->valid != VM_PAGE_BITS_ALL) {
2016387aabc5SAlan Cox 			ma[0] = m;
2017387aabc5SAlan Cox 			rv = vm_pager_get_pages(object, ma, 1, 0);
2018387aabc5SAlan Cox 			m = vm_page_lookup(object, pindex);
2019387aabc5SAlan Cox 			if (m == NULL)
2020387aabc5SAlan Cox 				break;
2021387aabc5SAlan Cox 			if (rv != VM_PAGER_OK) {
20222965a453SKip Macy 				vm_page_lock(m);
2023387aabc5SAlan Cox 				vm_page_free(m);
20242965a453SKip Macy 				vm_page_unlock(m);
2025387aabc5SAlan Cox 				break;
2026387aabc5SAlan Cox 			}
2027387aabc5SAlan Cox 		}
2028387aabc5SAlan Cox 		/*
2029387aabc5SAlan Cox 		 * Keep "m" busy because a subsequent iteration may unlock
2030387aabc5SAlan Cox 		 * the object.
2031387aabc5SAlan Cox 		 */
2032387aabc5SAlan Cox 	}
2033387aabc5SAlan Cox 	if (pindex > start) {
2034387aabc5SAlan Cox 		m = vm_page_lookup(object, start);
2035387aabc5SAlan Cox 		while (m != NULL && m->pindex < pindex) {
2036387aabc5SAlan Cox 			vm_page_wakeup(m);
2037387aabc5SAlan Cox 			m = TAILQ_NEXT(m, listq);
2038387aabc5SAlan Cox 		}
2039387aabc5SAlan Cox 	}
2040387aabc5SAlan Cox 	return (pindex == end);
2041387aabc5SAlan Cox }
2042387aabc5SAlan Cox 
2043387aabc5SAlan Cox /*
2044df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
2045df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
2046df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
2047df8bae1dSRodney W. Grimes  *
2048df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
2049df8bae1dSRodney W. Grimes  *
2050df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
2051df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
2052df8bae1dSRodney W. Grimes  *
2053df8bae1dSRodney W. Grimes  *	Parameters:
2054df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
2055df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
2056df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
205757a21abaSAlan Cox  *		next_size	Size of reference to the second object
20583364c323SKonstantin Belousov  *		reserved	Indicator that extension region has
20593364c323SKonstantin Belousov  *				swap accounted for
2060df8bae1dSRodney W. Grimes  *
2061df8bae1dSRodney W. Grimes  *	Conditions:
2062df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
2063df8bae1dSRodney W. Grimes  */
20640d94caffSDavid Greenman boolean_t
206557a21abaSAlan Cox vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset,
20663364c323SKonstantin Belousov     vm_size_t prev_size, vm_size_t next_size, boolean_t reserved)
2067df8bae1dSRodney W. Grimes {
2068ea41812fSAlan Cox 	vm_pindex_t next_pindex;
2069df8bae1dSRodney W. Grimes 
207000e1854aSAlan Cox 	if (prev_object == NULL)
2071df8bae1dSRodney W. Grimes 		return (TRUE);
207289f6b863SAttilio Rao 	VM_OBJECT_WLOCK(prev_object);
20734112823fSMatthew Dillon 	if (prev_object->type != OBJT_DEFAULT &&
20744112823fSMatthew Dillon 	    prev_object->type != OBJT_SWAP) {
207589f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(prev_object);
207630dcfc09SJohn Dyson 		return (FALSE);
207730dcfc09SJohn Dyson 	}
207830dcfc09SJohn Dyson 
2079df8bae1dSRodney W. Grimes 	/*
2080df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
2081df8bae1dSRodney W. Grimes 	 */
2082df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
2083df8bae1dSRodney W. Grimes 
2084df8bae1dSRodney W. Grimes 	/*
20850d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
20860d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
20870d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
2088df8bae1dSRodney W. Grimes 	 */
20898cc7e047SJohn Dyson 	if (prev_object->backing_object != NULL) {
209089f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(prev_object);
2091df8bae1dSRodney W. Grimes 		return (FALSE);
2092df8bae1dSRodney W. Grimes 	}
2093a316d390SJohn Dyson 
2094a316d390SJohn Dyson 	prev_size >>= PAGE_SHIFT;
2095a316d390SJohn Dyson 	next_size >>= PAGE_SHIFT;
209657a21abaSAlan Cox 	next_pindex = OFF_TO_IDX(prev_offset) + prev_size;
20978cc7e047SJohn Dyson 
20988cc7e047SJohn Dyson 	if ((prev_object->ref_count > 1) &&
2099ea41812fSAlan Cox 	    (prev_object->size != next_pindex)) {
210089f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(prev_object);
21018cc7e047SJohn Dyson 		return (FALSE);
21028cc7e047SJohn Dyson 	}
21038cc7e047SJohn Dyson 
2104df8bae1dSRodney W. Grimes 	/*
21053364c323SKonstantin Belousov 	 * Account for the charge.
21063364c323SKonstantin Belousov 	 */
2107ef694c1aSEdward Tomasz Napierala 	if (prev_object->cred != NULL) {
21083364c323SKonstantin Belousov 
21093364c323SKonstantin Belousov 		/*
21103364c323SKonstantin Belousov 		 * If prev_object was charged, then this mapping,
21113364c323SKonstantin Belousov 		 * althought not charged now, may become writable
2112ef694c1aSEdward Tomasz Napierala 		 * later. Non-NULL cred in the object would prevent
21133364c323SKonstantin Belousov 		 * swap reservation during enabling of the write
21143364c323SKonstantin Belousov 		 * access, so reserve swap now. Failed reservation
21153364c323SKonstantin Belousov 		 * cause allocation of the separate object for the map
21163364c323SKonstantin Belousov 		 * entry, and swap reservation for this entry is
21173364c323SKonstantin Belousov 		 * managed in appropriate time.
21183364c323SKonstantin Belousov 		 */
2119ef694c1aSEdward Tomasz Napierala 		if (!reserved && !swap_reserve_by_cred(ptoa(next_size),
2120ef694c1aSEdward Tomasz Napierala 		    prev_object->cred)) {
21213364c323SKonstantin Belousov 			return (FALSE);
21223364c323SKonstantin Belousov 		}
21233364c323SKonstantin Belousov 		prev_object->charge += ptoa(next_size);
21243364c323SKonstantin Belousov 	}
21253364c323SKonstantin Belousov 
21263364c323SKonstantin Belousov 	/*
21270d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
21280d94caffSDavid Greenman 	 * deallocation.
2129df8bae1dSRodney W. Grimes 	 */
2130ea41812fSAlan Cox 	if (next_pindex < prev_object->size) {
21316bbee8e2SAlan Cox 		vm_object_page_remove(prev_object, next_pindex, next_pindex +
21326bbee8e2SAlan Cox 		    next_size, 0);
2133ea41812fSAlan Cox 		if (prev_object->type == OBJT_SWAP)
2134ea41812fSAlan Cox 			swap_pager_freespace(prev_object,
2135ea41812fSAlan Cox 					     next_pindex, next_size);
21363364c323SKonstantin Belousov #if 0
2137ef694c1aSEdward Tomasz Napierala 		if (prev_object->cred != NULL) {
21383364c323SKonstantin Belousov 			KASSERT(prev_object->charge >=
21393364c323SKonstantin Belousov 			    ptoa(prev_object->size - next_pindex),
21403364c323SKonstantin Belousov 			    ("object %p overcharged 1 %jx %jx", prev_object,
21413364c323SKonstantin Belousov 				(uintmax_t)next_pindex, (uintmax_t)next_size));
21423364c323SKonstantin Belousov 			prev_object->charge -= ptoa(prev_object->size -
21433364c323SKonstantin Belousov 			    next_pindex);
21443364c323SKonstantin Belousov 		}
21453364c323SKonstantin Belousov #endif
2146ea41812fSAlan Cox 	}
2147df8bae1dSRodney W. Grimes 
2148df8bae1dSRodney W. Grimes 	/*
2149df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
2150df8bae1dSRodney W. Grimes 	 */
2151ea41812fSAlan Cox 	if (next_pindex + next_size > prev_object->size)
2152ea41812fSAlan Cox 		prev_object->size = next_pindex + next_size;
2153df8bae1dSRodney W. Grimes 
215489f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(prev_object);
2155df8bae1dSRodney W. Grimes 	return (TRUE);
2156df8bae1dSRodney W. Grimes }
2157df8bae1dSRodney W. Grimes 
21587a5a6352SMatthew Dillon void
21597a5a6352SMatthew Dillon vm_object_set_writeable_dirty(vm_object_t object)
21607a5a6352SMatthew Dillon {
21617a5a6352SMatthew Dillon 
216289f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
21633280870dSKonstantin Belousov 	if (object->type != OBJT_VNODE)
21643280870dSKonstantin Belousov 		return;
21653280870dSKonstantin Belousov 	object->generation++;
21663280870dSKonstantin Belousov 	if ((object->flags & OBJ_MIGHTBEDIRTY) != 0)
2167ee39666aSJeff Roberson 		return;
2168af51d7bfSAlan Cox 	vm_object_set_flag(object, OBJ_MIGHTBEDIRTY);
21697a5a6352SMatthew Dillon }
21707a5a6352SMatthew Dillon 
2171c7c34a24SBruce Evans #include "opt_ddb.h"
2172c3cb3e12SDavid Greenman #ifdef DDB
2173c7c34a24SBruce Evans #include <sys/kernel.h>
2174c7c34a24SBruce Evans 
2175ce9edcf5SPoul-Henning Kamp #include <sys/cons.h>
2176c7c34a24SBruce Evans 
2177c7c34a24SBruce Evans #include <ddb/ddb.h>
2178c7c34a24SBruce Evans 
2179cac597e4SBruce Evans static int
21801b40f8c0SMatthew Dillon _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
2181a1f6d91cSDavid Greenman {
2182a1f6d91cSDavid Greenman 	vm_map_t tmpm;
2183a1f6d91cSDavid Greenman 	vm_map_entry_t tmpe;
2184a1f6d91cSDavid Greenman 	vm_object_t obj;
2185a1f6d91cSDavid Greenman 	int entcount;
2186a1f6d91cSDavid Greenman 
2187a1f6d91cSDavid Greenman 	if (map == 0)
2188a1f6d91cSDavid Greenman 		return 0;
2189a1f6d91cSDavid Greenman 
2190a1f6d91cSDavid Greenman 	if (entry == 0) {
2191a1f6d91cSDavid Greenman 		tmpe = map->header.next;
2192a1f6d91cSDavid Greenman 		entcount = map->nentries;
2193a1f6d91cSDavid Greenman 		while (entcount-- && (tmpe != &map->header)) {
2194a1f6d91cSDavid Greenman 			if (_vm_object_in_map(map, object, tmpe)) {
2195a1f6d91cSDavid Greenman 				return 1;
2196a1f6d91cSDavid Greenman 			}
2197a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
2198a1f6d91cSDavid Greenman 		}
21999fdfe602SMatthew Dillon 	} else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
22009fdfe602SMatthew Dillon 		tmpm = entry->object.sub_map;
2201a1f6d91cSDavid Greenman 		tmpe = tmpm->header.next;
2202a1f6d91cSDavid Greenman 		entcount = tmpm->nentries;
2203a1f6d91cSDavid Greenman 		while (entcount-- && tmpe != &tmpm->header) {
2204a1f6d91cSDavid Greenman 			if (_vm_object_in_map(tmpm, object, tmpe)) {
2205a1f6d91cSDavid Greenman 				return 1;
2206a1f6d91cSDavid Greenman 			}
2207a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
2208a1f6d91cSDavid Greenman 		}
22098aef1712SMatthew Dillon 	} else if ((obj = entry->object.vm_object) != NULL) {
221024a1cce3SDavid Greenman 		for (; obj; obj = obj->backing_object)
2211a1f6d91cSDavid Greenman 			if (obj == object) {
2212a1f6d91cSDavid Greenman 				return 1;
2213a1f6d91cSDavid Greenman 			}
2214a1f6d91cSDavid Greenman 	}
2215a1f6d91cSDavid Greenman 	return 0;
2216a1f6d91cSDavid Greenman }
2217a1f6d91cSDavid Greenman 
2218cac597e4SBruce Evans static int
22191b40f8c0SMatthew Dillon vm_object_in_map(vm_object_t object)
2220a1f6d91cSDavid Greenman {
2221a1f6d91cSDavid Greenman 	struct proc *p;
22221005a129SJohn Baldwin 
222360517fd1SJohn Baldwin 	/* sx_slock(&allproc_lock); */
2224f67af5c9SXin LI 	FOREACH_PROC_IN_SYSTEM(p) {
2225a1f6d91cSDavid Greenman 		if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
2226a1f6d91cSDavid Greenman 			continue;
2227553629ebSJake Burkholder 		if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
222860517fd1SJohn Baldwin 			/* sx_sunlock(&allproc_lock); */
2229a1f6d91cSDavid Greenman 			return 1;
2230a1f6d91cSDavid Greenman 		}
2231553629ebSJake Burkholder 	}
223260517fd1SJohn Baldwin 	/* sx_sunlock(&allproc_lock); */
2233a1f6d91cSDavid Greenman 	if (_vm_object_in_map(kernel_map, object, 0))
2234a1f6d91cSDavid Greenman 		return 1;
2235a1f6d91cSDavid Greenman 	return 0;
2236a1f6d91cSDavid Greenman }
2237a1f6d91cSDavid Greenman 
2238c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check)
2239f708ef1bSPoul-Henning Kamp {
2240a1f6d91cSDavid Greenman 	vm_object_t object;
2241a1f6d91cSDavid Greenman 
2242a1f6d91cSDavid Greenman 	/*
2243a1f6d91cSDavid Greenman 	 * make sure that internal objs are in a map somewhere
2244a1f6d91cSDavid Greenman 	 * and none have zero ref counts.
2245a1f6d91cSDavid Greenman 	 */
2246cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
224724a1cce3SDavid Greenman 		if (object->handle == NULL &&
224824a1cce3SDavid Greenman 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2249a1f6d91cSDavid Greenman 			if (object->ref_count == 0) {
22503efc015bSPeter Wemm 				db_printf("vmochk: internal obj has zero ref count: %ld\n",
22513efc015bSPeter Wemm 					(long)object->size);
2252a1f6d91cSDavid Greenman 			}
2253a1f6d91cSDavid Greenman 			if (!vm_object_in_map(object)) {
2254fc62ef1fSBruce Evans 				db_printf(
2255fc62ef1fSBruce Evans 			"vmochk: internal obj is not in a map: "
2256fc62ef1fSBruce Evans 			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2257fc62ef1fSBruce Evans 				    object->ref_count, (u_long)object->size,
2258fc62ef1fSBruce Evans 				    (u_long)object->size,
2259fc62ef1fSBruce Evans 				    (void *)object->backing_object);
2260a1f6d91cSDavid Greenman 			}
2261a1f6d91cSDavid Greenman 		}
2262a1f6d91cSDavid Greenman 	}
2263a1f6d91cSDavid Greenman }
2264a1f6d91cSDavid Greenman 
226526f9a767SRodney W. Grimes /*
2266df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
2267df8bae1dSRodney W. Grimes  */
2268c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static)
2269df8bae1dSRodney W. Grimes {
2270c7c34a24SBruce Evans 	/* XXX convert args. */
2271c7c34a24SBruce Evans 	vm_object_t object = (vm_object_t)addr;
2272c7c34a24SBruce Evans 	boolean_t full = have_addr;
2273c7c34a24SBruce Evans 
2274d031cff1SMatthew Dillon 	vm_page_t p;
2275df8bae1dSRodney W. Grimes 
2276c7c34a24SBruce Evans 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
2277c7c34a24SBruce Evans #define	count	was_count
2278c7c34a24SBruce Evans 
2279d031cff1SMatthew Dillon 	int count;
2280df8bae1dSRodney W. Grimes 
2281df8bae1dSRodney W. Grimes 	if (object == NULL)
2282df8bae1dSRodney W. Grimes 		return;
2283df8bae1dSRodney W. Grimes 
2284eb95adefSBruce Evans 	db_iprintf(
2285ef694c1aSEdward Tomasz Napierala 	    "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x ruid %d charge %jx\n",
2286e47cd172SMaxime Henrion 	    object, (int)object->type, (uintmax_t)object->size,
22873364c323SKonstantin Belousov 	    object->resident_page_count, object->ref_count, object->flags,
2288ef694c1aSEdward Tomasz Napierala 	    object->cred ? object->cred->cr_ruid : -1, (uintmax_t)object->charge);
2289e47cd172SMaxime Henrion 	db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n",
22901c7c3c6aSMatthew Dillon 	    object->shadow_count,
2291eb95adefSBruce Evans 	    object->backing_object ? object->backing_object->ref_count : 0,
2292e47cd172SMaxime Henrion 	    object->backing_object, (uintmax_t)object->backing_object_offset);
2293df8bae1dSRodney W. Grimes 
2294df8bae1dSRodney W. Grimes 	if (!full)
2295df8bae1dSRodney W. Grimes 		return;
2296df8bae1dSRodney W. Grimes 
2297c7c34a24SBruce Evans 	db_indent += 2;
2298df8bae1dSRodney W. Grimes 	count = 0;
2299fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(p, &object->memq, listq) {
2300df8bae1dSRodney W. Grimes 		if (count == 0)
2301c7c34a24SBruce Evans 			db_iprintf("memory:=");
2302df8bae1dSRodney W. Grimes 		else if (count == 6) {
2303c7c34a24SBruce Evans 			db_printf("\n");
2304c7c34a24SBruce Evans 			db_iprintf(" ...");
2305df8bae1dSRodney W. Grimes 			count = 0;
2306df8bae1dSRodney W. Grimes 		} else
2307c7c34a24SBruce Evans 			db_printf(",");
2308df8bae1dSRodney W. Grimes 		count++;
2309df8bae1dSRodney W. Grimes 
2310e47cd172SMaxime Henrion 		db_printf("(off=0x%jx,page=0x%jx)",
2311e47cd172SMaxime Henrion 		    (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p));
2312df8bae1dSRodney W. Grimes 	}
2313df8bae1dSRodney W. Grimes 	if (count != 0)
2314c7c34a24SBruce Evans 		db_printf("\n");
2315c7c34a24SBruce Evans 	db_indent -= 2;
2316df8bae1dSRodney W. Grimes }
23175070c7f8SJohn Dyson 
2318c7c34a24SBruce Evans /* XXX. */
2319c7c34a24SBruce Evans #undef count
2320c7c34a24SBruce Evans 
2321c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */
23225070c7f8SJohn Dyson void
23231b40f8c0SMatthew Dillon vm_object_print(
23241b40f8c0SMatthew Dillon         /* db_expr_t */ long addr,
23251b40f8c0SMatthew Dillon 	boolean_t have_addr,
23261b40f8c0SMatthew Dillon 	/* db_expr_t */ long count,
23271b40f8c0SMatthew Dillon 	char *modif)
2328c7c34a24SBruce Evans {
2329c7c34a24SBruce Evans 	vm_object_print_static(addr, have_addr, count, modif);
2330c7c34a24SBruce Evans }
2331c7c34a24SBruce Evans 
2332c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
23335070c7f8SJohn Dyson {
23345070c7f8SJohn Dyson 	vm_object_t object;
2335bb2ac86fSKonstantin Belousov 	vm_pindex_t fidx;
2336bb2ac86fSKonstantin Belousov 	vm_paddr_t pa;
2337bb2ac86fSKonstantin Belousov 	vm_page_t m, prev_m;
2338bb2ac86fSKonstantin Belousov 	int rcount, nl, c;
2339cc64b484SAlfred Perlstein 
2340bb2ac86fSKonstantin Belousov 	nl = 0;
2341cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
2342fc62ef1fSBruce Evans 		db_printf("new object: %p\n", (void *)object);
23435070c7f8SJohn Dyson 		if (nl > 18) {
23445070c7f8SJohn Dyson 			c = cngetc();
23455070c7f8SJohn Dyson 			if (c != ' ')
23465070c7f8SJohn Dyson 				return;
23475070c7f8SJohn Dyson 			nl = 0;
23485070c7f8SJohn Dyson 		}
23495070c7f8SJohn Dyson 		nl++;
23505070c7f8SJohn Dyson 		rcount = 0;
23515070c7f8SJohn Dyson 		fidx = 0;
2352bb2ac86fSKonstantin Belousov 		pa = -1;
2353bb2ac86fSKonstantin Belousov 		TAILQ_FOREACH(m, &object->memq, listq) {
2354bb2ac86fSKonstantin Belousov 			if (m->pindex > 128)
2355bb2ac86fSKonstantin Belousov 				break;
2356bb2ac86fSKonstantin Belousov 			if ((prev_m = TAILQ_PREV(m, pglist, listq)) != NULL &&
2357bb2ac86fSKonstantin Belousov 			    prev_m->pindex + 1 != m->pindex) {
23585070c7f8SJohn Dyson 				if (rcount) {
23593efc015bSPeter Wemm 					db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
23603efc015bSPeter Wemm 						(long)fidx, rcount, (long)pa);
23615070c7f8SJohn Dyson 					if (nl > 18) {
23625070c7f8SJohn Dyson 						c = cngetc();
23635070c7f8SJohn Dyson 						if (c != ' ')
23645070c7f8SJohn Dyson 							return;
23655070c7f8SJohn Dyson 						nl = 0;
23665070c7f8SJohn Dyson 					}
23675070c7f8SJohn Dyson 					nl++;
23685070c7f8SJohn Dyson 					rcount = 0;
23695070c7f8SJohn Dyson 				}
23705070c7f8SJohn Dyson 			}
23715070c7f8SJohn Dyson 			if (rcount &&
23725070c7f8SJohn Dyson 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
23735070c7f8SJohn Dyson 				++rcount;
23745070c7f8SJohn Dyson 				continue;
23755070c7f8SJohn Dyson 			}
23765070c7f8SJohn Dyson 			if (rcount) {
23772446e4f0SAlan Cox 				db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
23783efc015bSPeter Wemm 					(long)fidx, rcount, (long)pa);
23795070c7f8SJohn Dyson 				if (nl > 18) {
23805070c7f8SJohn Dyson 					c = cngetc();
23815070c7f8SJohn Dyson 					if (c != ' ')
23825070c7f8SJohn Dyson 						return;
23835070c7f8SJohn Dyson 					nl = 0;
23845070c7f8SJohn Dyson 				}
23855070c7f8SJohn Dyson 				nl++;
23865070c7f8SJohn Dyson 			}
2387bb2ac86fSKonstantin Belousov 			fidx = m->pindex;
23885070c7f8SJohn Dyson 			pa = VM_PAGE_TO_PHYS(m);
23895070c7f8SJohn Dyson 			rcount = 1;
23905070c7f8SJohn Dyson 		}
23915070c7f8SJohn Dyson 		if (rcount) {
23923efc015bSPeter Wemm 			db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
23933efc015bSPeter Wemm 				(long)fidx, rcount, (long)pa);
23945070c7f8SJohn Dyson 			if (nl > 18) {
23955070c7f8SJohn Dyson 				c = cngetc();
23965070c7f8SJohn Dyson 				if (c != ' ')
23975070c7f8SJohn Dyson 					return;
23985070c7f8SJohn Dyson 				nl = 0;
23995070c7f8SJohn Dyson 			}
24005070c7f8SJohn Dyson 			nl++;
24015070c7f8SJohn Dyson 		}
24025070c7f8SJohn Dyson 	}
24035070c7f8SJohn Dyson }
2404c3cb3e12SDavid Greenman #endif /* DDB */
2405