xref: /freebsd/sys/vm/vm_object.c (revision e946b94934e6205383b30f615799b6b60c391a3f)
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;
204*e946b949SAttilio Rao 	object->rtree.rt_flags = 0;
2058355f576SJeff Roberson 	object->paging_in_progress = 0;
2068355f576SJeff Roberson 	object->resident_page_count = 0;
2078355f576SJeff Roberson 	object->shadow_count = 0;
208774d251dSAttilio Rao 	object->cache.rt_root = 0;
209*e946b949SAttilio Rao 	object->cache.rt_flags = 0;
210b23f72e9SBrian Feldman 	return (0);
2118355f576SJeff Roberson }
212df8bae1dSRodney W. Grimes 
213a4915c21SAttilio Rao static void
2146395da54SIan Dowse _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
215df8bae1dSRodney W. Grimes {
2160cddd8f0SMatthew Dillon 
217df8bae1dSRodney W. Grimes 	TAILQ_INIT(&object->memq);
2181c500307SAlan Cox 	LIST_INIT(&object->shadow_head);
219a1f6d91cSDavid Greenman 
22024a1cce3SDavid Greenman 	object->type = type;
22128634820SAlan Cox 	switch (type) {
22228634820SAlan Cox 	case OBJT_DEAD:
22328634820SAlan Cox 		panic("_vm_object_allocate: can't create OBJT_DEAD");
22428634820SAlan Cox 	case OBJT_DEFAULT:
22528634820SAlan Cox 	case OBJT_SWAP:
22628634820SAlan Cox 		object->flags = OBJ_ONEMAPPING;
22728634820SAlan Cox 		break;
22828634820SAlan Cox 	case OBJT_DEVICE:
22928634820SAlan Cox 	case OBJT_SG:
23028634820SAlan Cox 		object->flags = OBJ_FICTITIOUS | OBJ_UNMANAGED;
23128634820SAlan Cox 		break;
23228634820SAlan Cox 	case OBJT_MGTDEVICE:
23328634820SAlan Cox 		object->flags = OBJ_FICTITIOUS;
23428634820SAlan Cox 		break;
23528634820SAlan Cox 	case OBJT_PHYS:
23628634820SAlan Cox 		object->flags = OBJ_UNMANAGED;
23728634820SAlan Cox 		break;
23828634820SAlan Cox 	case OBJT_VNODE:
23928634820SAlan Cox 		object->flags = 0;
24028634820SAlan Cox 		break;
24128634820SAlan Cox 	default:
24228634820SAlan Cox 		panic("_vm_object_allocate: type %d is undefined", type);
24328634820SAlan Cox 	}
244df8bae1dSRodney W. Grimes 	object->size = size;
245b881da26SAlan Cox 	object->generation = 1;
246a1f6d91cSDavid Greenman 	object->ref_count = 1;
2473153e878SAlan Cox 	object->memattr = VM_MEMATTR_DEFAULT;
248ef694c1aSEdward Tomasz Napierala 	object->cred = NULL;
2493364c323SKonstantin Belousov 	object->charge = 0;
25024a1cce3SDavid Greenman 	object->handle = NULL;
25124a1cce3SDavid Greenman 	object->backing_object = NULL;
252a316d390SJohn Dyson 	object->backing_object_offset = (vm_ooffset_t) 0;
253f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
254f8a47341SAlan Cox 	LIST_INIT(&object->rvq);
255f8a47341SAlan Cox #endif
256a1f6d91cSDavid Greenman 
257a5698387SAlan Cox 	mtx_lock(&vm_object_list_mtx);
25860517fd1SJohn Baldwin 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
259a5698387SAlan Cox 	mtx_unlock(&vm_object_list_mtx);
260df8bae1dSRodney W. Grimes }
261df8bae1dSRodney W. Grimes 
262df8bae1dSRodney W. Grimes /*
26326f9a767SRodney W. Grimes  *	vm_object_init:
26426f9a767SRodney W. Grimes  *
26526f9a767SRodney W. Grimes  *	Initialize the VM objects module.
26626f9a767SRodney W. Grimes  */
26726f9a767SRodney W. Grimes void
2681b40f8c0SMatthew Dillon vm_object_init(void)
26926f9a767SRodney W. Grimes {
27026f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_list);
2716008862bSJohn Baldwin 	mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF);
2720217125fSDavid Greenman 
27389f6b863SAttilio Rao 	rw_init(&kernel_object->lock, "kernel vm object");
2749f5c801bSAlan Cox 	_vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
27526f9a767SRodney W. Grimes 	    kernel_object);
276f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
277f8a47341SAlan Cox 	kernel_object->flags |= OBJ_COLORED;
278f8a47341SAlan Cox 	kernel_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS);
279f8a47341SAlan Cox #endif
28026f9a767SRodney W. Grimes 
28189f6b863SAttilio Rao 	rw_init(&kmem_object->lock, "kmem vm object");
2829f5c801bSAlan Cox 	_vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
28326f9a767SRodney W. Grimes 	    kmem_object);
284f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
285f8a47341SAlan Cox 	kmem_object->flags |= OBJ_COLORED;
286f8a47341SAlan Cox 	kmem_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS);
287f8a47341SAlan Cox #endif
288ed6a7863SAlan Cox 
2898dbca793STor Egge 	/*
2908dbca793STor Egge 	 * The lock portion of struct vm_object must be type stable due
2918dbca793STor Egge 	 * to vm_pageout_fallback_object_lock locking a vm object
2928dbca793STor Egge 	 * without holding any references to it.
2938dbca793STor Egge 	 */
2948355f576SJeff Roberson 	obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL,
2958355f576SJeff Roberson #ifdef INVARIANTS
2968355f576SJeff Roberson 	    vm_object_zdtor,
2978355f576SJeff Roberson #else
2988355f576SJeff Roberson 	    NULL,
2998355f576SJeff Roberson #endif
3005df87b21SJeff Roberson 	    vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
301774d251dSAttilio Rao 
302774d251dSAttilio Rao 	vm_radix_init();
30399448ed1SJohn Dyson }
30499448ed1SJohn Dyson 
30599448ed1SJohn Dyson void
3061b40f8c0SMatthew Dillon vm_object_clear_flag(vm_object_t object, u_short bits)
3071b40f8c0SMatthew Dillon {
3085440b5a9SAlan Cox 
30989f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
310b06805adSJake Burkholder 	object->flags &= ~bits;
3111b40f8c0SMatthew Dillon }
3121b40f8c0SMatthew Dillon 
3133153e878SAlan Cox /*
3143153e878SAlan Cox  *	Sets the default memory attribute for the specified object.  Pages
3153153e878SAlan Cox  *	that are allocated to this object are by default assigned this memory
3163153e878SAlan Cox  *	attribute.
3173153e878SAlan Cox  *
3183153e878SAlan Cox  *	Presently, this function must be called before any pages are allocated
3193153e878SAlan Cox  *	to the object.  In the future, this requirement may be relaxed for
3203153e878SAlan Cox  *	"default" and "swap" objects.
3213153e878SAlan Cox  */
3223153e878SAlan Cox int
3233153e878SAlan Cox vm_object_set_memattr(vm_object_t object, vm_memattr_t memattr)
3243153e878SAlan Cox {
3253153e878SAlan Cox 
32689f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
3273153e878SAlan Cox 	switch (object->type) {
3283153e878SAlan Cox 	case OBJT_DEFAULT:
3293153e878SAlan Cox 	case OBJT_DEVICE:
33096b0b92aSAlan Cox 	case OBJT_MGTDEVICE:
3313153e878SAlan Cox 	case OBJT_PHYS:
33201381811SJohn Baldwin 	case OBJT_SG:
3333153e878SAlan Cox 	case OBJT_SWAP:
3343153e878SAlan Cox 	case OBJT_VNODE:
3353153e878SAlan Cox 		if (!TAILQ_EMPTY(&object->memq))
3363153e878SAlan Cox 			return (KERN_FAILURE);
3373153e878SAlan Cox 		break;
3383153e878SAlan Cox 	case OBJT_DEAD:
3393153e878SAlan Cox 		return (KERN_INVALID_ARGUMENT);
34096b0b92aSAlan Cox 	default:
34196b0b92aSAlan Cox 		panic("vm_object_set_memattr: object %p is of undefined type",
34296b0b92aSAlan Cox 		    object);
3433153e878SAlan Cox 	}
3443153e878SAlan Cox 	object->memattr = memattr;
3453153e878SAlan Cox 	return (KERN_SUCCESS);
3463153e878SAlan Cox }
3473153e878SAlan Cox 
3481b40f8c0SMatthew Dillon void
3491b40f8c0SMatthew Dillon vm_object_pip_add(vm_object_t object, short i)
3501b40f8c0SMatthew Dillon {
351f279b88dSAlan Cox 
35289f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
353b06805adSJake Burkholder 	object->paging_in_progress += i;
3541b40f8c0SMatthew Dillon }
3551b40f8c0SMatthew Dillon 
3561b40f8c0SMatthew Dillon void
3571b40f8c0SMatthew Dillon vm_object_pip_subtract(vm_object_t object, short i)
3581b40f8c0SMatthew Dillon {
359d647a0edSAlan Cox 
36089f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
361b06805adSJake Burkholder 	object->paging_in_progress -= i;
3621b40f8c0SMatthew Dillon }
3631b40f8c0SMatthew Dillon 
3641b40f8c0SMatthew Dillon void
3651b40f8c0SMatthew Dillon vm_object_pip_wakeup(vm_object_t object)
3661b40f8c0SMatthew Dillon {
367f279b88dSAlan Cox 
36889f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
369b06805adSJake Burkholder 	object->paging_in_progress--;
3701b40f8c0SMatthew Dillon 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
3711b40f8c0SMatthew Dillon 		vm_object_clear_flag(object, OBJ_PIPWNT);
3721b40f8c0SMatthew Dillon 		wakeup(object);
3731b40f8c0SMatthew Dillon 	}
3741b40f8c0SMatthew Dillon }
3751b40f8c0SMatthew Dillon 
3761b40f8c0SMatthew Dillon void
3771b40f8c0SMatthew Dillon vm_object_pip_wakeupn(vm_object_t object, short i)
3781b40f8c0SMatthew Dillon {
379d647a0edSAlan Cox 
38089f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
3811b40f8c0SMatthew Dillon 	if (i)
382b06805adSJake Burkholder 		object->paging_in_progress -= i;
3831b40f8c0SMatthew Dillon 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
3841b40f8c0SMatthew Dillon 		vm_object_clear_flag(object, OBJ_PIPWNT);
3851b40f8c0SMatthew Dillon 		wakeup(object);
3861b40f8c0SMatthew Dillon 	}
3871b40f8c0SMatthew Dillon }
3881b40f8c0SMatthew Dillon 
3891b40f8c0SMatthew Dillon void
3901b40f8c0SMatthew Dillon vm_object_pip_wait(vm_object_t object, char *waitid)
3911b40f8c0SMatthew Dillon {
3921ca58953SAlan Cox 
39389f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
3941ca58953SAlan Cox 	while (object->paging_in_progress) {
3951ca58953SAlan Cox 		object->flags |= OBJ_PIPWNT;
3960dde287bSAttilio Rao 		VM_OBJECT_SLEEP(object, object, PVM, waitid, 0);
3971ca58953SAlan Cox 	}
3981b40f8c0SMatthew Dillon }
3991b40f8c0SMatthew Dillon 
40026f9a767SRodney W. Grimes /*
40126f9a767SRodney W. Grimes  *	vm_object_allocate:
40226f9a767SRodney W. Grimes  *
40326f9a767SRodney W. Grimes  *	Returns a new object with the given size.
40426f9a767SRodney W. Grimes  */
40526f9a767SRodney W. Grimes vm_object_t
4066395da54SIan Dowse vm_object_allocate(objtype_t type, vm_pindex_t size)
40726f9a767SRodney W. Grimes {
40890688d13SAlan Cox 	vm_object_t object;
40990688d13SAlan Cox 
41090688d13SAlan Cox 	object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK);
41190688d13SAlan Cox 	_vm_object_allocate(type, size, object);
41290688d13SAlan Cox 	return (object);
41326f9a767SRodney W. Grimes }
41426f9a767SRodney W. Grimes 
41526f9a767SRodney W. Grimes 
41626f9a767SRodney W. Grimes /*
417df8bae1dSRodney W. Grimes  *	vm_object_reference:
418df8bae1dSRodney W. Grimes  *
41915347817SAlan Cox  *	Gets another reference to the given object.  Note: OBJ_DEAD
42015347817SAlan Cox  *	objects can be referenced during final cleaning.
421df8bae1dSRodney W. Grimes  */
4226476c0d2SJohn Dyson void
4231b40f8c0SMatthew Dillon vm_object_reference(vm_object_t object)
424df8bae1dSRodney W. Grimes {
425df8bae1dSRodney W. Grimes 	if (object == NULL)
426df8bae1dSRodney W. Grimes 		return;
42789f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
42852481a9aSJeff Roberson 	vm_object_reference_locked(object);
42989f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
43095e5e988SJohn Dyson }
43195e5e988SJohn Dyson 
43223955314SAlfred Perlstein /*
433b921a12bSAlan Cox  *	vm_object_reference_locked:
434b921a12bSAlan Cox  *
435b921a12bSAlan Cox  *	Gets another reference to the given object.
436b921a12bSAlan Cox  *
437b921a12bSAlan Cox  *	The object must be locked.
438b921a12bSAlan Cox  */
439b921a12bSAlan Cox void
440b921a12bSAlan Cox vm_object_reference_locked(vm_object_t object)
441b921a12bSAlan Cox {
442b921a12bSAlan Cox 	struct vnode *vp;
443b921a12bSAlan Cox 
44489f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
445b921a12bSAlan Cox 	object->ref_count++;
446b921a12bSAlan Cox 	if (object->type == OBJT_VNODE) {
447b921a12bSAlan Cox 		vp = object->handle;
448b921a12bSAlan Cox 		vref(vp);
449b921a12bSAlan Cox 	}
450b921a12bSAlan Cox }
451b921a12bSAlan Cox 
452b921a12bSAlan Cox /*
4539d5abbddSJens Schweikhardt  * Handle deallocating an object of type OBJT_VNODE.
45423955314SAlfred Perlstein  */
45502dd8331SAlan Cox static void
4561b40f8c0SMatthew Dillon vm_object_vndeallocate(vm_object_t object)
45795e5e988SJohn Dyson {
45895e5e988SJohn Dyson 	struct vnode *vp = (struct vnode *) object->handle;
459219cbf59SEivind Eklund 
46089f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
4615526d2d9SEivind Eklund 	KASSERT(object->type == OBJT_VNODE,
4625526d2d9SEivind Eklund 	    ("vm_object_vndeallocate: not a vnode object"));
463219cbf59SEivind Eklund 	KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
464219cbf59SEivind Eklund #ifdef INVARIANTS
46595e5e988SJohn Dyson 	if (object->ref_count == 0) {
46695e5e988SJohn Dyson 		vprint("vm_object_vndeallocate", vp);
46795e5e988SJohn Dyson 		panic("vm_object_vndeallocate: bad object reference count");
46895e5e988SJohn Dyson 	}
46995e5e988SJohn Dyson #endif
47095e5e988SJohn Dyson 
47103fa5b34SKonstantin Belousov 	if (object->ref_count > 1) {
47295e5e988SJohn Dyson 		object->ref_count--;
47389f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
47403fa5b34SKonstantin Belousov 		/* vrele may need the vnode lock. */
47547221757SJohn Dyson 		vrele(vp);
47603fa5b34SKonstantin Belousov 	} else {
47786769ac0SKonstantin Belousov 		vhold(vp);
47889f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
47903fa5b34SKonstantin Belousov 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
48086769ac0SKonstantin Belousov 		vdrop(vp);
48189f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
48203fa5b34SKonstantin Belousov 		object->ref_count--;
48386769ac0SKonstantin Belousov 		if (object->type == OBJT_DEAD) {
48489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
48586769ac0SKonstantin Belousov 			VOP_UNLOCK(vp, 0);
48686769ac0SKonstantin Belousov 		} else {
48703fa5b34SKonstantin Belousov 			if (object->ref_count == 0)
488877d24acSKonstantin Belousov 				VOP_UNSET_TEXT(vp);
48989f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
49003fa5b34SKonstantin Belousov 			vput(vp);
49103fa5b34SKonstantin Belousov 		}
492df8bae1dSRodney W. Grimes 	}
49386769ac0SKonstantin Belousov }
494df8bae1dSRodney W. Grimes 
495df8bae1dSRodney W. Grimes /*
496df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
497df8bae1dSRodney W. Grimes  *
498df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
499df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
500df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
501df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
502df8bae1dSRodney W. Grimes  *	may be relinquished.
503df8bae1dSRodney W. Grimes  *
504df8bae1dSRodney W. Grimes  *	No object may be locked.
505df8bae1dSRodney W. Grimes  */
50626f9a767SRodney W. Grimes void
5071b40f8c0SMatthew Dillon vm_object_deallocate(vm_object_t object)
508df8bae1dSRodney W. Grimes {
509df8bae1dSRodney W. Grimes 	vm_object_t temp;
5106f2af3fcSKonstantin Belousov 	struct vnode *vp;
511df8bae1dSRodney W. Grimes 
512df8bae1dSRodney W. Grimes 	while (object != NULL) {
51389f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
5143b68228cSAlan Cox 		if (object->type == OBJT_VNODE) {
51595e5e988SJohn Dyson 			vm_object_vndeallocate(object);
51623b186d3SAlan Cox 			return;
5175050aa86SKonstantin Belousov 		}
51895e5e988SJohn Dyson 
5198125b1e6SAlfred Perlstein 		KASSERT(object->ref_count != 0,
5208125b1e6SAlfred Perlstein 			("vm_object_deallocate: object deallocated too many times: %d", object->type));
5212be70f79SJohn Dyson 
5222be70f79SJohn Dyson 		/*
5238125b1e6SAlfred Perlstein 		 * If the reference count goes to 0 we start calling
5248125b1e6SAlfred Perlstein 		 * vm_object_terminate() on the object chain.
5258125b1e6SAlfred Perlstein 		 * A ref count of 1 may be a special case depending on the
5268125b1e6SAlfred Perlstein 		 * shadow count being 0 or 1.
5272be70f79SJohn Dyson 		 */
528c0877f10SJohn Dyson 		object->ref_count--;
5298125b1e6SAlfred Perlstein 		if (object->ref_count > 1) {
53089f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
53123b186d3SAlan Cox 			return;
5328125b1e6SAlfred Perlstein 		} else if (object->ref_count == 1) {
5336f2af3fcSKonstantin Belousov 			if (object->type == OBJT_SWAP &&
5346f2af3fcSKonstantin Belousov 			    (object->flags & OBJ_TMPFS) != 0) {
5356f2af3fcSKonstantin Belousov 				vp = object->un_pager.swp.swp_tmpfs;
5366f2af3fcSKonstantin Belousov 				vhold(vp);
5376f2af3fcSKonstantin Belousov 				VM_OBJECT_WUNLOCK(object);
5386f2af3fcSKonstantin Belousov 				vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
5396f2af3fcSKonstantin Belousov 				vdrop(vp);
5406f2af3fcSKonstantin Belousov 				VM_OBJECT_WLOCK(object);
5417560005cSKonstantin Belousov 				if (object->type == OBJT_DEAD ||
5427560005cSKonstantin Belousov 				    object->ref_count != 1) {
5436f2af3fcSKonstantin Belousov 					VM_OBJECT_WUNLOCK(object);
5446f2af3fcSKonstantin Belousov 					VOP_UNLOCK(vp, 0);
5456f2af3fcSKonstantin Belousov 					return;
5467560005cSKonstantin Belousov 				}
5477560005cSKonstantin Belousov 				if ((object->flags & OBJ_TMPFS) != 0)
5486f2af3fcSKonstantin Belousov 					VOP_UNSET_TEXT(vp);
5496f2af3fcSKonstantin Belousov 				VOP_UNLOCK(vp, 0);
5506f2af3fcSKonstantin Belousov 			}
5514c8e0452SAlan Cox 			if (object->shadow_count == 0 &&
5524c8e0452SAlan Cox 			    object->handle == NULL &&
5534c8e0452SAlan Cox 			    (object->type == OBJT_DEFAULT ||
5546f2af3fcSKonstantin Belousov 			    (object->type == OBJT_SWAP &&
5556f2af3fcSKonstantin Belousov 			    (object->flags & OBJ_TMPFS) == 0))) {
5568125b1e6SAlfred Perlstein 				vm_object_set_flag(object, OBJ_ONEMAPPING);
5578125b1e6SAlfred Perlstein 			} else if ((object->shadow_count == 1) &&
5588125b1e6SAlfred Perlstein 			    (object->handle == NULL) &&
55924a1cce3SDavid Greenman 			    (object->type == OBJT_DEFAULT ||
56024a1cce3SDavid Greenman 			     object->type == OBJT_SWAP)) {
5616f2af3fcSKonstantin Belousov 				KASSERT((object->flags & OBJ_TMPFS) == 0,
562782d4a63SKonstantin Belousov 				    ("shadowed tmpfs v_object %p", object));
563a1f6d91cSDavid Greenman 				vm_object_t robject;
56495e5e988SJohn Dyson 
5651c500307SAlan Cox 				robject = LIST_FIRST(&object->shadow_head);
5665526d2d9SEivind Eklund 				KASSERT(robject != NULL,
567219cbf59SEivind Eklund 				    ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
5685526d2d9SEivind Eklund 					 object->ref_count,
5695526d2d9SEivind Eklund 					 object->shadow_count));
57089f6b863SAttilio Rao 				if (!VM_OBJECT_TRYWLOCK(robject)) {
571b72b0115SAlan Cox 					/*
572b72b0115SAlan Cox 					 * Avoid a potential deadlock.
573b72b0115SAlan Cox 					 */
574b72b0115SAlan Cox 					object->ref_count++;
57589f6b863SAttilio Rao 					VM_OBJECT_WUNLOCK(object);
576a7d86121SAlan Cox 					/*
577a7d86121SAlan Cox 					 * More likely than not the thread
578a7d86121SAlan Cox 					 * holding robject's lock has lower
579a7d86121SAlan Cox 					 * priority than the current thread.
580a7d86121SAlan Cox 					 * Let the lower priority thread run.
581a7d86121SAlan Cox 					 */
5828db5fc58SJohn Baldwin 					pause("vmo_de", 1);
583b72b0115SAlan Cox 					continue;
584b72b0115SAlan Cox 				}
585d936694fSAlan Cox 				/*
586d936694fSAlan Cox 				 * Collapse object into its shadow unless its
587d936694fSAlan Cox 				 * shadow is dead.  In that case, object will
588d936694fSAlan Cox 				 * be deallocated by the thread that is
589d936694fSAlan Cox 				 * deallocating its shadow.
590d936694fSAlan Cox 				 */
591d936694fSAlan Cox 				if ((robject->flags & OBJ_DEAD) == 0 &&
592d936694fSAlan Cox 				    (robject->handle == NULL) &&
59324a1cce3SDavid Greenman 				    (robject->type == OBJT_DEFAULT ||
59424a1cce3SDavid Greenman 				     robject->type == OBJT_SWAP)) {
595a1f6d91cSDavid Greenman 
59695e5e988SJohn Dyson 					robject->ref_count++;
597138449dcSAlan Cox retry:
598138449dcSAlan Cox 					if (robject->paging_in_progress) {
59989f6b863SAttilio Rao 						VM_OBJECT_WUNLOCK(object);
600138449dcSAlan Cox 						vm_object_pip_wait(robject,
601138449dcSAlan Cox 						    "objde1");
6022e9f4a69SAlan Cox 						temp = robject->backing_object;
6032e9f4a69SAlan Cox 						if (object == temp) {
60489f6b863SAttilio Rao 							VM_OBJECT_WLOCK(object);
605138449dcSAlan Cox 							goto retry;
6062e9f4a69SAlan Cox 						}
607138449dcSAlan Cox 					} else if (object->paging_in_progress) {
60889f6b863SAttilio Rao 						VM_OBJECT_WUNLOCK(robject);
609138449dcSAlan Cox 						object->flags |= OBJ_PIPWNT;
6100dde287bSAttilio Rao 						VM_OBJECT_SLEEP(object, object,
611138449dcSAlan Cox 						    PDROP | PVM, "objde2", 0);
61289f6b863SAttilio Rao 						VM_OBJECT_WLOCK(robject);
6132e9f4a69SAlan Cox 						temp = robject->backing_object;
6142e9f4a69SAlan Cox 						if (object == temp) {
61589f6b863SAttilio Rao 							VM_OBJECT_WLOCK(object);
616138449dcSAlan Cox 							goto retry;
617a1f6d91cSDavid Greenman 						}
6182e9f4a69SAlan Cox 					} else
61989f6b863SAttilio Rao 						VM_OBJECT_WUNLOCK(object);
6202e9f4a69SAlan Cox 
62195e5e988SJohn Dyson 					if (robject->ref_count == 1) {
62295e5e988SJohn Dyson 						robject->ref_count--;
623ba8da839SDavid Greenman 						object = robject;
62495e5e988SJohn Dyson 						goto doterm;
62595e5e988SJohn Dyson 					}
62695e5e988SJohn Dyson 					object = robject;
62795e5e988SJohn Dyson 					vm_object_collapse(object);
62889f6b863SAttilio Rao 					VM_OBJECT_WUNLOCK(object);
629ba8da839SDavid Greenman 					continue;
630a1f6d91cSDavid Greenman 				}
63189f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(robject);
63295e5e988SJohn Dyson 			}
63389f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
63423b186d3SAlan Cox 			return;
63595e5e988SJohn Dyson 		}
63695e5e988SJohn Dyson doterm:
63724a1cce3SDavid Greenman 		temp = object->backing_object;
638c9917419SAlan Cox 		if (temp != NULL) {
63989f6b863SAttilio Rao 			VM_OBJECT_WLOCK(temp);
6401c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
64195e5e988SJohn Dyson 			temp->shadow_count--;
64289f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(temp);
64395461b45SJohn Dyson 			object->backing_object = NULL;
644de5f6a77SJohn Dyson 		}
645245df27cSMatthew Dillon 		/*
646245df27cSMatthew Dillon 		 * Don't double-terminate, we could be in a termination
647245df27cSMatthew Dillon 		 * recursion due to the terminate having to sync data
648245df27cSMatthew Dillon 		 * to disk.
649245df27cSMatthew Dillon 		 */
650245df27cSMatthew Dillon 		if ((object->flags & OBJ_DEAD) == 0)
651df8bae1dSRodney W. Grimes 			vm_object_terminate(object);
652c829b9d0SAlan Cox 		else
65389f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
654df8bae1dSRodney W. Grimes 		object = temp;
655df8bae1dSRodney W. Grimes 	}
656df8bae1dSRodney W. Grimes }
657df8bae1dSRodney W. Grimes 
658df8bae1dSRodney W. Grimes /*
6592ac78f0eSStephan Uphoff  *	vm_object_destroy removes the object from the global object list
6602ac78f0eSStephan Uphoff  *      and frees the space for the object.
6612ac78f0eSStephan Uphoff  */
6622ac78f0eSStephan Uphoff void
6632ac78f0eSStephan Uphoff vm_object_destroy(vm_object_t object)
6642ac78f0eSStephan Uphoff {
6652ac78f0eSStephan Uphoff 
6662ac78f0eSStephan Uphoff 	/*
6672ac78f0eSStephan Uphoff 	 * Remove the object from the global object list.
6682ac78f0eSStephan Uphoff 	 */
6692ac78f0eSStephan Uphoff 	mtx_lock(&vm_object_list_mtx);
6702ac78f0eSStephan Uphoff 	TAILQ_REMOVE(&vm_object_list, object, object_list);
6712ac78f0eSStephan Uphoff 	mtx_unlock(&vm_object_list_mtx);
6722ac78f0eSStephan Uphoff 
6732ac78f0eSStephan Uphoff 	/*
6743364c323SKonstantin Belousov 	 * Release the allocation charge.
6753364c323SKonstantin Belousov 	 */
676ef694c1aSEdward Tomasz Napierala 	if (object->cred != NULL) {
6773364c323SKonstantin Belousov 		KASSERT(object->type == OBJT_DEFAULT ||
6783364c323SKonstantin Belousov 		    object->type == OBJT_SWAP,
679ef694c1aSEdward Tomasz Napierala 		    ("vm_object_terminate: non-swap obj %p has cred",
6803364c323SKonstantin Belousov 		     object));
681ef694c1aSEdward Tomasz Napierala 		swap_release_by_cred(object->charge, object->cred);
6823364c323SKonstantin Belousov 		object->charge = 0;
683ef694c1aSEdward Tomasz Napierala 		crfree(object->cred);
684ef694c1aSEdward Tomasz Napierala 		object->cred = NULL;
6853364c323SKonstantin Belousov 	}
6863364c323SKonstantin Belousov 
6873364c323SKonstantin Belousov 	/*
6882ac78f0eSStephan Uphoff 	 * Free the space for the object.
6892ac78f0eSStephan Uphoff 	 */
6902ac78f0eSStephan Uphoff 	uma_zfree(obj_zone, object);
6912ac78f0eSStephan Uphoff }
6922ac78f0eSStephan Uphoff 
6932ac78f0eSStephan Uphoff /*
694df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
695df8bae1dSRodney W. Grimes  *	up all previously used resources.
696df8bae1dSRodney W. Grimes  *
697df8bae1dSRodney W. Grimes  *	The object must be locked.
6981c7c3c6aSMatthew Dillon  *	This routine may block.
699df8bae1dSRodney W. Grimes  */
70095e5e988SJohn Dyson void
7011b40f8c0SMatthew Dillon vm_object_terminate(vm_object_t object)
702df8bae1dSRodney W. Grimes {
70317ea6f00SAlan Cox 	vm_page_t p, p_next;
704df8bae1dSRodney W. Grimes 
70589f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
7060cddd8f0SMatthew Dillon 
70795e5e988SJohn Dyson 	/*
70895e5e988SJohn Dyson 	 * Make sure no one uses us.
70995e5e988SJohn Dyson 	 */
710069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_DEAD);
7113c631446SJohn Dyson 
712df8bae1dSRodney W. Grimes 	/*
713f6b04d2bSDavid Greenman 	 * wait for the pageout daemon to be done with the object
714df8bae1dSRodney W. Grimes 	 */
71566095752SJohn Dyson 	vm_object_pip_wait(object, "objtrm");
716df8bae1dSRodney W. Grimes 
7175526d2d9SEivind Eklund 	KASSERT(!object->paging_in_progress,
7185526d2d9SEivind Eklund 		("vm_object_terminate: pageout in progress"));
71926f9a767SRodney W. Grimes 
72026f9a767SRodney W. Grimes 	/*
7210d94caffSDavid Greenman 	 * Clean and free the pages, as appropriate. All references to the
7220d94caffSDavid Greenman 	 * object are gone, so we don't need to lock it.
72326f9a767SRodney W. Grimes 	 */
72424a1cce3SDavid Greenman 	if (object->type == OBJT_VNODE) {
725f7dd7b63SAlan Cox 		struct vnode *vp = (struct vnode *)object->handle;
72695e5e988SJohn Dyson 
72795e5e988SJohn Dyson 		/*
72895e5e988SJohn Dyson 		 * Clean pages and flush buffers.
72995e5e988SJohn Dyson 		 */
7308f9110f6SJohn Dyson 		vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
73189f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
73295e5e988SJohn Dyson 
7330d7935fdSAttilio Rao 		vinvalbuf(vp, V_SAVE, 0, 0);
734f7dd7b63SAlan Cox 
73589f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
736bef608bdSJohn Dyson 	}
737bef608bdSJohn Dyson 
738971dd342SAlfred Perlstein 	KASSERT(object->ref_count == 0,
739971dd342SAlfred Perlstein 		("vm_object_terminate: object with references, ref_count=%d",
740971dd342SAlfred Perlstein 		object->ref_count));
741996c772fSJohn Dyson 
7420d94caffSDavid Greenman 	/*
74317ea6f00SAlan Cox 	 * Free any remaining pageable pages.  This also removes them from the
74417ea6f00SAlan Cox 	 * paging queues.  However, don't free wired pages, just remove them
74517ea6f00SAlan Cox 	 * from the object.  Rather than incrementally removing each page from
74617ea6f00SAlan Cox 	 * the object, the page and object are reset to any empty state.
747df8bae1dSRodney W. Grimes 	 */
74817ea6f00SAlan Cox 	TAILQ_FOREACH_SAFE(p, &object->memq, listq, p_next) {
749c7aebda8SAttilio Rao 		vm_page_assert_unbusied(p);
7502965a453SKip Macy 		vm_page_lock(p);
75117ea6f00SAlan Cox 		/*
75217ea6f00SAlan Cox 		 * Optimize the page's removal from the object by resetting
75317ea6f00SAlan Cox 		 * its "object" field.  Specifically, if the page is not
75417ea6f00SAlan Cox 		 * wired, then the effect of this assignment is that
75517ea6f00SAlan Cox 		 * vm_page_free()'s call to vm_page_remove() will return
75617ea6f00SAlan Cox 		 * immediately without modifying the page or the object.
75717ea6f00SAlan Cox 		 */
75817ea6f00SAlan Cox 		p->object = NULL;
7590b10ba98SDavid Greenman 		if (p->wire_count == 0) {
760df8bae1dSRodney W. Grimes 			vm_page_free(p);
76170721880SAlan Cox 			PCPU_INC(cnt.v_pfree);
76217ea6f00SAlan Cox 		}
7632965a453SKip Macy 		vm_page_unlock(p);
7642965a453SKip Macy 	}
76517ea6f00SAlan Cox 	/*
76617ea6f00SAlan Cox 	 * If the object contained any pages, then reset it to an empty state.
76717ea6f00SAlan Cox 	 * None of the object's fields, including "resident_page_count", were
76817ea6f00SAlan Cox 	 * modified by the preceding loop.
76917ea6f00SAlan Cox 	 */
77017ea6f00SAlan Cox 	if (object->resident_page_count != 0) {
771774d251dSAttilio Rao 		vm_radix_reclaim_allnodes(&object->rtree);
77217ea6f00SAlan Cox 		TAILQ_INIT(&object->memq);
77317ea6f00SAlan Cox 		object->resident_page_count = 0;
77417ea6f00SAlan Cox 		if (object->type == OBJT_VNODE)
77517ea6f00SAlan Cox 			vdrop(object->handle);
77617ea6f00SAlan Cox 	}
777bef608bdSJohn Dyson 
778f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
779f8a47341SAlan Cox 	if (__predict_false(!LIST_EMPTY(&object->rvq)))
780f8a47341SAlan Cox 		vm_reserv_break_all(object);
781f8a47341SAlan Cox #endif
782c9341161SAttilio Rao 	if (__predict_false(!vm_object_cache_is_empty(object)))
783c9444914SAlan Cox 		vm_page_cache_free(object, 0, 0);
7847bfda801SAlan Cox 
7852d8acc0fSJohn Dyson 	/*
7869fcfb650SDavid Greenman 	 * Let the pager know object is dead.
7879fcfb650SDavid Greenman 	 */
7889fcfb650SDavid Greenman 	vm_pager_deallocate(object);
78989f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
7909fcfb650SDavid Greenman 
7912ac78f0eSStephan Uphoff 	vm_object_destroy(object);
79247221757SJohn Dyson }
793df8bae1dSRodney W. Grimes 
794edf93b25SAlan Cox /*
795edf93b25SAlan Cox  * Make the page read-only so that we can clear the object flags.  However, if
796edf93b25SAlan Cox  * this is a nosync mmap then the object is likely to stay dirty so do not
797edf93b25SAlan Cox  * mess with the page and do not clear the object flags.  Returns TRUE if the
798edf93b25SAlan Cox  * page should be flushed, and FALSE otherwise.
799edf93b25SAlan Cox  */
8003280870dSKonstantin Belousov static boolean_t
801126d6082SKonstantin Belousov vm_object_page_remove_write(vm_page_t p, int flags, boolean_t *clearobjflags)
8023280870dSKonstantin Belousov {
8033280870dSKonstantin Belousov 
8043280870dSKonstantin Belousov 	/*
8053280870dSKonstantin Belousov 	 * If we have been asked to skip nosync pages and this is a
8063280870dSKonstantin Belousov 	 * nosync page, skip it.  Note that the object flags were not
8073280870dSKonstantin Belousov 	 * cleared in this case so we do not have to set them.
8083280870dSKonstantin Belousov 	 */
8093280870dSKonstantin Belousov 	if ((flags & OBJPC_NOSYNC) != 0 && (p->oflags & VPO_NOSYNC) != 0) {
810126d6082SKonstantin Belousov 		*clearobjflags = FALSE;
8113280870dSKonstantin Belousov 		return (FALSE);
8123280870dSKonstantin Belousov 	} else {
8133280870dSKonstantin Belousov 		pmap_remove_write(p);
8143280870dSKonstantin Belousov 		return (p->dirty != 0);
8153280870dSKonstantin Belousov 	}
8163280870dSKonstantin Belousov }
8173280870dSKonstantin Belousov 
818df8bae1dSRodney W. Grimes /*
819df8bae1dSRodney W. Grimes  *	vm_object_page_clean
820df8bae1dSRodney W. Grimes  *
8214f79d873SMatthew Dillon  *	Clean all dirty pages in the specified range of object.  Leaves page
8224f79d873SMatthew Dillon  * 	on whatever queue it is currently on.   If NOSYNC is set then do not
823b146f9e5SAlan Cox  *	write out pages with VPO_NOSYNC set (originally comes from MAP_NOSYNC),
8244f79d873SMatthew Dillon  *	leaving the object dirty.
82526f9a767SRodney W. Grimes  *
82643b7990eSMatthew Dillon  *	When stuffing pages asynchronously, allow clustering.  XXX we need a
82743b7990eSMatthew Dillon  *	synchronous clustering mode implementation.
82843b7990eSMatthew Dillon  *
82926f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
83026f9a767SRodney W. Grimes  *
83126f9a767SRodney W. Grimes  *	The object must be locked.
832126d6082SKonstantin Belousov  *
833126d6082SKonstantin Belousov  *	Returns FALSE if some page from the range was not written, as
834126d6082SKonstantin Belousov  *	reported by the pager, and TRUE otherwise.
83526f9a767SRodney W. Grimes  */
836126d6082SKonstantin Belousov boolean_t
83717f3095dSAlan Cox vm_object_page_clean(vm_object_t object, vm_ooffset_t start, vm_ooffset_t end,
838e239bb97SKonstantin Belousov     int flags)
839f6b04d2bSDavid Greenman {
840e239bb97SKonstantin Belousov 	vm_page_t np, p;
84117f3095dSAlan Cox 	vm_pindex_t pi, tend, tstart;
842126d6082SKonstantin Belousov 	int curgeneration, n, pagerflags;
843126d6082SKonstantin Belousov 	boolean_t clearobjflags, eio, res;
844f6b04d2bSDavid Greenman 
84589f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
846e5f299ffSKonstantin Belousov 
847e5f299ffSKonstantin Belousov 	/*
848e5f299ffSKonstantin Belousov 	 * The OBJ_MIGHTBEDIRTY flag is only set for OBJT_VNODE
849e5f299ffSKonstantin Belousov 	 * objects.  The check below prevents the function from
850e5f299ffSKonstantin Belousov 	 * operating on non-vnode objects.
851e5f299ffSKonstantin Belousov 	 */
852e239bb97SKonstantin Belousov 	if ((object->flags & OBJ_MIGHTBEDIRTY) == 0 ||
853e239bb97SKonstantin Belousov 	    object->resident_page_count == 0)
854126d6082SKonstantin Belousov 		return (TRUE);
855f6b04d2bSDavid Greenman 
856e239bb97SKonstantin Belousov 	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) != 0 ?
857e239bb97SKonstantin Belousov 	    VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
858e239bb97SKonstantin Belousov 	pagerflags |= (flags & OBJPC_INVAL) != 0 ? VM_PAGER_PUT_INVAL : 0;
859e239bb97SKonstantin Belousov 
86017f3095dSAlan Cox 	tstart = OFF_TO_IDX(start);
86117f3095dSAlan Cox 	tend = (end == 0) ? object->size : OFF_TO_IDX(end + PAGE_MASK);
86217f3095dSAlan Cox 	clearobjflags = tstart == 0 && tend >= object->size;
863126d6082SKonstantin Belousov 	res = TRUE;
864f6b04d2bSDavid Greenman 
865bd7e5f99SJohn Dyson rescan:
8662d8acc0fSJohn Dyson 	curgeneration = object->generation;
8672d8acc0fSJohn Dyson 
86817f3095dSAlan Cox 	for (p = vm_page_find_least(object, tstart); p != NULL; p = np) {
869bd7e5f99SJohn Dyson 		pi = p->pindex;
870e239bb97SKonstantin Belousov 		if (pi >= tend)
871e239bb97SKonstantin Belousov 			break;
872e239bb97SKonstantin Belousov 		np = TAILQ_NEXT(p, listq);
873e239bb97SKonstantin Belousov 		if (p->valid == 0)
874aef922f5SJohn Dyson 			continue;
875c7aebda8SAttilio Rao 		if (vm_page_sleep_if_busy(p, "vpcwai")) {
876e65919f9SKonstantin Belousov 			if (object->generation != curgeneration) {
877e65919f9SKonstantin Belousov 				if ((flags & OBJPC_SYNC) != 0)
878e239bb97SKonstantin Belousov 					goto rescan;
879e65919f9SKonstantin Belousov 				else
880126d6082SKonstantin Belousov 					clearobjflags = FALSE;
881e65919f9SKonstantin Belousov 			}
882780636b7SKonstantin Belousov 			np = vm_page_find_least(object, pi);
883780636b7SKonstantin Belousov 			continue;
884f6b04d2bSDavid Greenman 		}
8853280870dSKonstantin Belousov 		if (!vm_object_page_remove_write(p, flags, &clearobjflags))
886bd7e5f99SJohn Dyson 			continue;
887e239bb97SKonstantin Belousov 
8883280870dSKonstantin Belousov 		n = vm_object_page_collect_flush(object, p, pagerflags,
889126d6082SKonstantin Belousov 		    flags, &clearobjflags, &eio);
890126d6082SKonstantin Belousov 		if (eio) {
891126d6082SKonstantin Belousov 			res = FALSE;
892126d6082SKonstantin Belousov 			clearobjflags = FALSE;
893126d6082SKonstantin Belousov 		}
894e65919f9SKonstantin Belousov 		if (object->generation != curgeneration) {
895e65919f9SKonstantin Belousov 			if ((flags & OBJPC_SYNC) != 0)
896b9b7a4beSMatthew Dillon 				goto rescan;
897e65919f9SKonstantin Belousov 			else
898126d6082SKonstantin Belousov 				clearobjflags = FALSE;
899e65919f9SKonstantin Belousov 		}
900031ec8c1SKonstantin Belousov 
901031ec8c1SKonstantin Belousov 		/*
902031ec8c1SKonstantin Belousov 		 * If the VOP_PUTPAGES() did a truncated write, so
903031ec8c1SKonstantin Belousov 		 * that even the first page of the run is not fully
904031ec8c1SKonstantin Belousov 		 * written, vm_pageout_flush() returns 0 as the run
905031ec8c1SKonstantin Belousov 		 * length.  Since the condition that caused truncated
906031ec8c1SKonstantin Belousov 		 * write may be permanent, e.g. exhausted free space,
907031ec8c1SKonstantin Belousov 		 * accepting n == 0 would cause an infinite loop.
908031ec8c1SKonstantin Belousov 		 *
909031ec8c1SKonstantin Belousov 		 * Forwarding the iterator leaves the unwritten page
910031ec8c1SKonstantin Belousov 		 * behind, but there is not much we can do there if
911031ec8c1SKonstantin Belousov 		 * filesystem refuses to write it.
912031ec8c1SKonstantin Belousov 		 */
913126d6082SKonstantin Belousov 		if (n == 0) {
914031ec8c1SKonstantin Belousov 			n = 1;
915126d6082SKonstantin Belousov 			clearobjflags = FALSE;
916126d6082SKonstantin Belousov 		}
917e239bb97SKonstantin Belousov 		np = vm_page_find_least(object, pi + n);
918b9b7a4beSMatthew Dillon 	}
919b9b7a4beSMatthew Dillon #if 0
920e239bb97SKonstantin Belousov 	VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC) ? MNT_WAIT : 0);
921b9b7a4beSMatthew Dillon #endif
922b9b7a4beSMatthew Dillon 
923edf93b25SAlan Cox 	if (clearobjflags)
9243280870dSKonstantin Belousov 		vm_object_clear_flag(object, OBJ_MIGHTBEDIRTY);
925126d6082SKonstantin Belousov 	return (res);
926b9b7a4beSMatthew Dillon }
927b9b7a4beSMatthew Dillon 
928b9b7a4beSMatthew Dillon static int
9293280870dSKonstantin Belousov vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int pagerflags,
930126d6082SKonstantin Belousov     int flags, boolean_t *clearobjflags, boolean_t *eio)
931b9b7a4beSMatthew Dillon {
9323157c503SKonstantin Belousov 	vm_page_t ma[vm_pageout_page_count], p_first, tp;
9333157c503SKonstantin Belousov 	int count, i, mreq, runlen;
934b9b7a4beSMatthew Dillon 
9357bec141bSKip Macy 	vm_page_lock_assert(p, MA_NOTOWNED);
93689f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
9373157c503SKonstantin Belousov 
9383157c503SKonstantin Belousov 	count = 1;
9393157c503SKonstantin Belousov 	mreq = 0;
9403157c503SKonstantin Belousov 
9413157c503SKonstantin Belousov 	for (tp = p; count < vm_pageout_page_count; count++) {
9423157c503SKonstantin Belousov 		tp = vm_page_next(tp);
943c7aebda8SAttilio Rao 		if (tp == NULL || vm_page_busied(tp))
944bd7e5f99SJohn Dyson 			break;
9453280870dSKonstantin Belousov 		if (!vm_object_page_remove_write(tp, flags, clearobjflags))
946bd7e5f99SJohn Dyson 			break;
947bd7e5f99SJohn Dyson 	}
948aef922f5SJohn Dyson 
9493157c503SKonstantin Belousov 	for (p_first = p; count < vm_pageout_page_count; count++) {
9503157c503SKonstantin Belousov 		tp = vm_page_prev(p_first);
951c7aebda8SAttilio Rao 		if (tp == NULL || vm_page_busied(tp))
952bd7e5f99SJohn Dyson 			break;
9533280870dSKonstantin Belousov 		if (!vm_object_page_remove_write(tp, flags, clearobjflags))
954bd7e5f99SJohn Dyson 			break;
9553157c503SKonstantin Belousov 		p_first = tp;
9563157c503SKonstantin Belousov 		mreq++;
957bd7e5f99SJohn Dyson 	}
958bd7e5f99SJohn Dyson 
9593157c503SKonstantin Belousov 	for (tp = p_first, i = 0; i < count; tp = TAILQ_NEXT(tp, listq), i++)
9603157c503SKonstantin Belousov 		ma[i] = tp;
961cf2819ccSJohn Dyson 
962126d6082SKonstantin Belousov 	vm_pageout_flush(ma, count, pagerflags, mreq, &runlen, eio);
9631e8a675cSKonstantin Belousov 	return (runlen);
96426f9a767SRodney W. Grimes }
965df8bae1dSRodney W. Grimes 
9661efb74fbSJohn Dyson /*
967950f8459SAlan Cox  * Note that there is absolutely no sense in writing out
968950f8459SAlan Cox  * anonymous objects, so we track down the vnode object
969950f8459SAlan Cox  * to write out.
970950f8459SAlan Cox  * We invalidate (remove) all pages from the address space
971950f8459SAlan Cox  * for semantic correctness.
972950f8459SAlan Cox  *
9736bbee8e2SAlan Cox  * If the backing object is a device object with unmanaged pages, then any
9746bbee8e2SAlan Cox  * mappings to the specified range of pages must be removed before this
9756bbee8e2SAlan Cox  * function is called.
9766bbee8e2SAlan Cox  *
977950f8459SAlan Cox  * Note: certain anonymous maps, such as MAP_NOSYNC maps,
978950f8459SAlan Cox  * may start out with a NULL object.
979950f8459SAlan Cox  */
980126d6082SKonstantin Belousov boolean_t
981950f8459SAlan Cox vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size,
982950f8459SAlan Cox     boolean_t syncio, boolean_t invalidate)
983950f8459SAlan Cox {
984950f8459SAlan Cox 	vm_object_t backing_object;
985950f8459SAlan Cox 	struct vnode *vp;
9863b582b4eSTor Egge 	struct mount *mp;
987126d6082SKonstantin Belousov 	int error, flags, fsync_after;
988126d6082SKonstantin Belousov 	boolean_t res;
989950f8459SAlan Cox 
990950f8459SAlan Cox 	if (object == NULL)
991126d6082SKonstantin Belousov 		return (TRUE);
992126d6082SKonstantin Belousov 	res = TRUE;
993126d6082SKonstantin Belousov 	error = 0;
99489f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
995950f8459SAlan Cox 	while ((backing_object = object->backing_object) != NULL) {
99689f6b863SAttilio Rao 		VM_OBJECT_WLOCK(backing_object);
99756e0670fSAlan Cox 		offset += object->backing_object_offset;
99889f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
999950f8459SAlan Cox 		object = backing_object;
1000950f8459SAlan Cox 		if (object->size < OFF_TO_IDX(offset + size))
1001950f8459SAlan Cox 			size = IDX_TO_OFF(object->size) - offset;
1002950f8459SAlan Cox 	}
1003950f8459SAlan Cox 	/*
1004950f8459SAlan Cox 	 * Flush pages if writing is allowed, invalidate them
1005950f8459SAlan Cox 	 * if invalidation requested.  Pages undergoing I/O
1006950f8459SAlan Cox 	 * will be ignored by vm_object_page_remove().
1007950f8459SAlan Cox 	 *
1008950f8459SAlan Cox 	 * We cannot lock the vnode and then wait for paging
1009950f8459SAlan Cox 	 * to complete without deadlocking against vm_fault.
1010950f8459SAlan Cox 	 * Instead we simply call vm_object_page_remove() and
1011950f8459SAlan Cox 	 * allow it to block internally on a page-by-page
1012950f8459SAlan Cox 	 * basis when it encounters pages undergoing async
1013950f8459SAlan Cox 	 * I/O.
1014950f8459SAlan Cox 	 */
1015950f8459SAlan Cox 	if (object->type == OBJT_VNODE &&
1016950f8459SAlan Cox 	    (object->flags & OBJ_MIGHTBEDIRTY) != 0) {
1017950f8459SAlan Cox 		vp = object->handle;
101889f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
10193b582b4eSTor Egge 		(void) vn_start_write(vp, &mp, V_WAIT);
1020cb05b60aSAttilio Rao 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
102175ff604aSKonstantin Belousov 		if (syncio && !invalidate && offset == 0 &&
102275ff604aSKonstantin Belousov 		    OFF_TO_IDX(size) == object->size) {
102375ff604aSKonstantin Belousov 			/*
102475ff604aSKonstantin Belousov 			 * If syncing the whole mapping of the file,
102575ff604aSKonstantin Belousov 			 * it is faster to schedule all the writes in
102675ff604aSKonstantin Belousov 			 * async mode, also allowing the clustering,
102775ff604aSKonstantin Belousov 			 * and then wait for i/o to complete.
102875ff604aSKonstantin Belousov 			 */
102975ff604aSKonstantin Belousov 			flags = 0;
103075ff604aSKonstantin Belousov 			fsync_after = TRUE;
103175ff604aSKonstantin Belousov 		} else {
1032950f8459SAlan Cox 			flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
103375ff604aSKonstantin Belousov 			flags |= invalidate ? (OBJPC_SYNC | OBJPC_INVAL) : 0;
103475ff604aSKonstantin Belousov 			fsync_after = FALSE;
103575ff604aSKonstantin Belousov 		}
103689f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
1037126d6082SKonstantin Belousov 		res = vm_object_page_clean(object, offset, offset + size,
1038126d6082SKonstantin Belousov 		    flags);
103989f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
104075ff604aSKonstantin Belousov 		if (fsync_after)
1041126d6082SKonstantin Belousov 			error = VOP_FSYNC(vp, MNT_WAIT, curthread);
104222db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
10433b582b4eSTor Egge 		vn_finished_write(mp);
1044126d6082SKonstantin Belousov 		if (error != 0)
1045126d6082SKonstantin Belousov 			res = FALSE;
104689f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
1047950f8459SAlan Cox 	}
1048950f8459SAlan Cox 	if ((object->type == OBJT_VNODE ||
1049950f8459SAlan Cox 	     object->type == OBJT_DEVICE) && invalidate) {
10506bbee8e2SAlan Cox 		if (object->type == OBJT_DEVICE)
10516bbee8e2SAlan Cox 			/*
10526bbee8e2SAlan Cox 			 * The option OBJPR_NOTMAPPED must be passed here
10536bbee8e2SAlan Cox 			 * because vm_object_page_remove() cannot remove
10546bbee8e2SAlan Cox 			 * unmanaged mappings.
10556bbee8e2SAlan Cox 			 */
10566bbee8e2SAlan Cox 			flags = OBJPR_NOTMAPPED;
10576bbee8e2SAlan Cox 		else if (old_msync)
1058ebf5d94eSKonstantin Belousov 			flags = OBJPR_NOTWIRED;
10596bbee8e2SAlan Cox 		else
1060ebf5d94eSKonstantin Belousov 			flags = OBJPR_CLEANONLY | OBJPR_NOTWIRED;
10616bbee8e2SAlan Cox 		vm_object_page_remove(object, OFF_TO_IDX(offset),
10626bbee8e2SAlan Cox 		    OFF_TO_IDX(offset + size + PAGE_MASK), flags);
1063950f8459SAlan Cox 	}
106489f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
1065126d6082SKonstantin Belousov 	return (res);
1066950f8459SAlan Cox }
1067950f8459SAlan Cox 
1068950f8459SAlan Cox /*
1069867a482dSJohn Dyson  *	vm_object_madvise:
1070867a482dSJohn Dyson  *
1071867a482dSJohn Dyson  *	Implements the madvise function at the object/page level.
10721c7c3c6aSMatthew Dillon  *
1073193b9358SAlan Cox  *	MADV_WILLNEED	(any object)
1074193b9358SAlan Cox  *
1075193b9358SAlan Cox  *	    Activate the specified pages if they are resident.
1076193b9358SAlan Cox  *
1077193b9358SAlan Cox  *	MADV_DONTNEED	(any object)
1078193b9358SAlan Cox  *
1079193b9358SAlan Cox  *	    Deactivate the specified pages if they are resident.
1080193b9358SAlan Cox  *
1081193b9358SAlan Cox  *	MADV_FREE	(OBJT_DEFAULT/OBJT_SWAP objects,
1082193b9358SAlan Cox  *			 OBJ_ONEMAPPING only)
1083193b9358SAlan Cox  *
1084193b9358SAlan Cox  *	    Deactivate and clean the specified pages if they are
1085193b9358SAlan Cox  *	    resident.  This permits the process to reuse the pages
1086193b9358SAlan Cox  *	    without faulting or the kernel to reclaim the pages
1087193b9358SAlan Cox  *	    without I/O.
1088867a482dSJohn Dyson  */
1089867a482dSJohn Dyson void
109092a59946SJohn Baldwin vm_object_madvise(vm_object_t object, vm_pindex_t pindex, vm_pindex_t end,
109192a59946SJohn Baldwin     int advise)
1092867a482dSJohn Dyson {
109392a59946SJohn Baldwin 	vm_pindex_t tpindex;
109434567de7SAlan Cox 	vm_object_t backing_object, tobject;
1095867a482dSJohn Dyson 	vm_page_t m;
1096867a482dSJohn Dyson 
1097867a482dSJohn Dyson 	if (object == NULL)
1098867a482dSJohn Dyson 		return;
109989f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
11001c7c3c6aSMatthew Dillon 	/*
11011c7c3c6aSMatthew Dillon 	 * Locate and adjust resident pages
11021c7c3c6aSMatthew Dillon 	 */
11031c7c3c6aSMatthew Dillon 	for (; pindex < end; pindex += 1) {
11046e20a165SJohn Dyson relookup:
11056e20a165SJohn Dyson 		tobject = object;
11066e20a165SJohn Dyson 		tpindex = pindex;
11076e20a165SJohn Dyson shadowlookup:
110858b4e6ccSAlan Cox 		/*
110958b4e6ccSAlan Cox 		 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
111058b4e6ccSAlan Cox 		 * and those pages must be OBJ_ONEMAPPING.
111158b4e6ccSAlan Cox 		 */
111258b4e6ccSAlan Cox 		if (advise == MADV_FREE) {
111358b4e6ccSAlan Cox 			if ((tobject->type != OBJT_DEFAULT &&
111458b4e6ccSAlan Cox 			     tobject->type != OBJT_SWAP) ||
111558b4e6ccSAlan Cox 			    (tobject->flags & OBJ_ONEMAPPING) == 0) {
111634567de7SAlan Cox 				goto unlock_tobject;
11176e20a165SJohn Dyson 			}
111828634820SAlan Cox 		} else if ((tobject->flags & OBJ_UNMANAGED) != 0)
11196a2a3d73SAlan Cox 			goto unlock_tobject;
11201c7c3c6aSMatthew Dillon 		m = vm_page_lookup(tobject, tpindex);
11217bfda801SAlan Cox 		if (m == NULL && advise == MADV_WILLNEED) {
11227bfda801SAlan Cox 			/*
11237bfda801SAlan Cox 			 * If the page is cached, reactivate it.
11247bfda801SAlan Cox 			 */
1125f3a2ed4bSAlan Cox 			m = vm_page_alloc(tobject, tpindex, VM_ALLOC_IFCACHED |
1126f3a2ed4bSAlan Cox 			    VM_ALLOC_NOBUSY);
11277bfda801SAlan Cox 		}
11281c7c3c6aSMatthew Dillon 		if (m == NULL) {
11291ce137beSMatthew Dillon 			/*
11301ce137beSMatthew Dillon 			 * There may be swap even if there is no backing page
11311ce137beSMatthew Dillon 			 */
11321ce137beSMatthew Dillon 			if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
11331ce137beSMatthew Dillon 				swap_pager_freespace(tobject, tpindex, 1);
11341ce137beSMatthew Dillon 			/*
11351ce137beSMatthew Dillon 			 * next object
11361ce137beSMatthew Dillon 			 */
113734567de7SAlan Cox 			backing_object = tobject->backing_object;
113834567de7SAlan Cox 			if (backing_object == NULL)
113934567de7SAlan Cox 				goto unlock_tobject;
114089f6b863SAttilio Rao 			VM_OBJECT_WLOCK(backing_object);
114156e0670fSAlan Cox 			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
11429b98b796SAlan Cox 			if (tobject != object)
114389f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(tobject);
114434567de7SAlan Cox 			tobject = backing_object;
11456e20a165SJohn Dyson 			goto shadowlookup;
11466a2a3d73SAlan Cox 		} else if (m->valid != VM_PAGE_BITS_ALL)
11476a2a3d73SAlan Cox 			goto unlock_tobject;
1148867a482dSJohn Dyson 		/*
11496a2a3d73SAlan Cox 		 * If the page is not in a normal state, skip it.
1150867a482dSJohn Dyson 		 */
11512965a453SKip Macy 		vm_page_lock(m);
11526a2a3d73SAlan Cox 		if (m->hold_count != 0 || m->wire_count != 0) {
11532965a453SKip Macy 			vm_page_unlock(m);
115434567de7SAlan Cox 			goto unlock_tobject;
11556e20a165SJohn Dyson 		}
1156d98d0ce2SKonstantin Belousov 		KASSERT((m->flags & PG_FICTITIOUS) == 0,
1157d98d0ce2SKonstantin Belousov 		    ("vm_object_madvise: page %p is fictitious", m));
1158d98d0ce2SKonstantin Belousov 		KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1159567e51e1SAlan Cox 		    ("vm_object_madvise: page %p is not managed", m));
1160c7aebda8SAttilio Rao 		if (vm_page_busied(m)) {
1161567e51e1SAlan Cox 			if (advise == MADV_WILLNEED) {
1162b11b56b5SAlan Cox 				/*
1163b11b56b5SAlan Cox 				 * Reference the page before unlocking and
1164b11b56b5SAlan Cox 				 * sleeping so that the page daemon is less
1165b11b56b5SAlan Cox 				 * likely to reclaim it.
1166b11b56b5SAlan Cox 				 */
11673407fefeSKonstantin Belousov 				vm_page_aflag_set(m, PGA_REFERENCED);
1168567e51e1SAlan Cox 			}
11699b98b796SAlan Cox 			if (object != tobject)
117089f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(object);
1171c7aebda8SAttilio Rao 			VM_OBJECT_WUNLOCK(tobject);
1172c7aebda8SAttilio Rao 			vm_page_busy_sleep(m, "madvpo");
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 		 */
1347c7aebda8SAttilio Rao 		if (vm_page_busied(m)) {
134889f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(new_object);
1349c7aebda8SAttilio Rao 			vm_page_lock(m);
1350c7aebda8SAttilio Rao 			VM_OBJECT_WUNLOCK(orig_object);
1351c7aebda8SAttilio Rao 			vm_page_busy_sleep(m, "spltwt");
1352c7aebda8SAttilio Rao 			VM_OBJECT_WLOCK(orig_object);
135389f6b863SAttilio Rao 			VM_OBJECT_WLOCK(new_object);
1354c5aaa06dSAlan Cox 			goto retry;
1355de33beddSAlan Cox 		}
1356*e946b949SAttilio Rao 
1357*e946b949SAttilio Rao 		/* vm_page_rename() will handle dirty and cache. */
1358*e946b949SAttilio Rao 		if (vm_page_rename(m, new_object, idx)) {
1359*e946b949SAttilio Rao 			VM_OBJECT_WUNLOCK(new_object);
1360*e946b949SAttilio Rao 			VM_OBJECT_WUNLOCK(orig_object);
1361*e946b949SAttilio Rao 			VM_WAIT;
1362*e946b949SAttilio Rao 			VM_OBJECT_WLOCK(orig_object);
1363*e946b949SAttilio Rao 			VM_OBJECT_WLOCK(new_object);
1364*e946b949SAttilio Rao 			goto retry;
1365*e946b949SAttilio Rao 		}
1366b5f359b7SAlan Cox #if VM_NRESERVLEVEL > 0
1367b5f359b7SAlan Cox 		/*
1368b5f359b7SAlan Cox 		 * If some of the reservation's allocated pages remain with
1369b5f359b7SAlan Cox 		 * the original object, then transferring the reservation to
1370b5f359b7SAlan Cox 		 * the new object is neither particularly beneficial nor
1371b5f359b7SAlan Cox 		 * particularly harmful as compared to leaving the reservation
1372b5f359b7SAlan Cox 		 * with the original object.  If, however, all of the
1373b5f359b7SAlan Cox 		 * reservation's allocated pages are transferred to the new
1374b5f359b7SAlan Cox 		 * object, then transferring the reservation is typically
1375b5f359b7SAlan Cox 		 * beneficial.  Determining which of these two cases applies
1376b5f359b7SAlan Cox 		 * would be more costly than unconditionally renaming the
1377b5f359b7SAlan Cox 		 * reservation.
1378b5f359b7SAlan Cox 		 */
1379b5f359b7SAlan Cox 		vm_reserv_rename(m, new_object, orig_object, offidxstart);
1380b5f359b7SAlan Cox #endif
1381dfd55c0cSAttilio Rao 		if (orig_object->type == OBJT_SWAP)
1382c7aebda8SAttilio Rao 			vm_page_xbusy(m);
1383c5aaa06dSAlan Cox 	}
1384d7a013c3SAlan Cox 	if (orig_object->type == OBJT_SWAP) {
1385c5aaa06dSAlan Cox 		/*
1386c7c8dd7eSAlan Cox 		 * swap_pager_copy() can sleep, in which case the orig_object's
1387c7c8dd7eSAlan Cox 		 * and new_object's locks are released and reacquired.
1388c5aaa06dSAlan Cox 		 */
1389c5aaa06dSAlan Cox 		swap_pager_copy(orig_object, new_object, offidxstart, 0);
1390dfd55c0cSAttilio Rao 		TAILQ_FOREACH(m, &new_object->memq, listq)
1391c7aebda8SAttilio Rao 			vm_page_xunbusy(m);
13927bfda801SAlan Cox 
13937bfda801SAlan Cox 		/*
13947bfda801SAlan Cox 		 * Transfer any cached pages from orig_object to new_object.
1395571a1e92SAttilio Rao 		 * If swap_pager_copy() found swapped out pages within the
1396571a1e92SAttilio Rao 		 * specified range of orig_object, then it changed
1397571a1e92SAttilio Rao 		 * new_object's type to OBJT_SWAP when it transferred those
1398571a1e92SAttilio Rao 		 * pages to new_object.  Otherwise, new_object's type
1399571a1e92SAttilio Rao 		 * should still be OBJT_DEFAULT and orig_object should not
1400571a1e92SAttilio Rao 		 * contain any cached pages within the specified range.
14017bfda801SAlan Cox 		 */
1402c9341161SAttilio Rao 		if (__predict_false(!vm_object_cache_is_empty(orig_object)))
14037bfda801SAlan Cox 			vm_page_cache_transfer(orig_object, offidxstart,
14047bfda801SAlan Cox 			    new_object);
1405c5aaa06dSAlan Cox 	}
140689f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(orig_object);
140789f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(new_object);
1408c5aaa06dSAlan Cox 	entry->object.vm_object = new_object;
1409c5aaa06dSAlan Cox 	entry->offset = 0LL;
1410c5aaa06dSAlan Cox 	vm_object_deallocate(orig_object);
141189f6b863SAttilio Rao 	VM_OBJECT_WLOCK(new_object);
1412c5aaa06dSAlan Cox }
1413c5aaa06dSAlan Cox 
14142ad1a3f7SMatthew Dillon #define	OBSC_TEST_ALL_SHADOWED	0x0001
14152ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_NOWAIT	0x0002
14162ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_WAIT	0x0004
14172ad1a3f7SMatthew Dillon 
1418b4ae4780SPoul-Henning Kamp static int
14192ad1a3f7SMatthew Dillon vm_object_backing_scan(vm_object_t object, int op)
14202ad1a3f7SMatthew Dillon {
14212ad1a3f7SMatthew Dillon 	int r = 1;
14222ad1a3f7SMatthew Dillon 	vm_page_t p;
14232ad1a3f7SMatthew Dillon 	vm_object_t backing_object;
14242ad1a3f7SMatthew Dillon 	vm_pindex_t backing_offset_index;
14252ad1a3f7SMatthew Dillon 
142689f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
142789f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object->backing_object);
14282ad1a3f7SMatthew Dillon 
14292ad1a3f7SMatthew Dillon 	backing_object = object->backing_object;
14302ad1a3f7SMatthew Dillon 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
14312ad1a3f7SMatthew Dillon 
14322ad1a3f7SMatthew Dillon 	/*
14332ad1a3f7SMatthew Dillon 	 * Initial conditions
14342ad1a3f7SMatthew Dillon 	 */
14352ad1a3f7SMatthew Dillon 	if (op & OBSC_TEST_ALL_SHADOWED) {
14362ad1a3f7SMatthew Dillon 		/*
14377bfda801SAlan Cox 		 * We do not want to have to test for the existence of cache
14387bfda801SAlan Cox 		 * or swap pages in the backing object.  XXX but with the
14392ad1a3f7SMatthew Dillon 		 * new swapper this would be pretty easy to do.
14402ad1a3f7SMatthew Dillon 		 *
14412ad1a3f7SMatthew Dillon 		 * XXX what about anonymous MAP_SHARED memory that hasn't
14422ad1a3f7SMatthew Dillon 		 * been ZFOD faulted yet?  If we do not test for this, the
14432ad1a3f7SMatthew Dillon 		 * shadow test may succeed! XXX
14442ad1a3f7SMatthew Dillon 		 */
14452ad1a3f7SMatthew Dillon 		if (backing_object->type != OBJT_DEFAULT) {
14462ad1a3f7SMatthew Dillon 			return (0);
14472ad1a3f7SMatthew Dillon 		}
14482ad1a3f7SMatthew Dillon 	}
14492ad1a3f7SMatthew Dillon 	if (op & OBSC_COLLAPSE_WAIT) {
14502ad1a3f7SMatthew Dillon 		vm_object_set_flag(backing_object, OBJ_DEAD);
14512ad1a3f7SMatthew Dillon 	}
14522ad1a3f7SMatthew Dillon 
14532ad1a3f7SMatthew Dillon 	/*
14542ad1a3f7SMatthew Dillon 	 * Our scan
14552ad1a3f7SMatthew Dillon 	 */
14562ad1a3f7SMatthew Dillon 	p = TAILQ_FIRST(&backing_object->memq);
14572ad1a3f7SMatthew Dillon 	while (p) {
14582ad1a3f7SMatthew Dillon 		vm_page_t next = TAILQ_NEXT(p, listq);
14592ad1a3f7SMatthew Dillon 		vm_pindex_t new_pindex = p->pindex - backing_offset_index;
14602ad1a3f7SMatthew Dillon 
14612ad1a3f7SMatthew Dillon 		if (op & OBSC_TEST_ALL_SHADOWED) {
14622ad1a3f7SMatthew Dillon 			vm_page_t pp;
14632ad1a3f7SMatthew Dillon 
14642ad1a3f7SMatthew Dillon 			/*
14652ad1a3f7SMatthew Dillon 			 * Ignore pages outside the parent object's range
14662ad1a3f7SMatthew Dillon 			 * and outside the parent object's mapping of the
14672ad1a3f7SMatthew Dillon 			 * backing object.
14682ad1a3f7SMatthew Dillon 			 *
14692ad1a3f7SMatthew Dillon 			 * note that we do not busy the backing object's
14702ad1a3f7SMatthew Dillon 			 * page.
14712ad1a3f7SMatthew Dillon 			 */
14722ad1a3f7SMatthew Dillon 			if (
14732ad1a3f7SMatthew Dillon 			    p->pindex < backing_offset_index ||
14742ad1a3f7SMatthew Dillon 			    new_pindex >= object->size
14752ad1a3f7SMatthew Dillon 			) {
14762ad1a3f7SMatthew Dillon 				p = next;
14772ad1a3f7SMatthew Dillon 				continue;
14782ad1a3f7SMatthew Dillon 			}
14792ad1a3f7SMatthew Dillon 
14802ad1a3f7SMatthew Dillon 			/*
14812ad1a3f7SMatthew Dillon 			 * See if the parent has the page or if the parent's
14822ad1a3f7SMatthew Dillon 			 * object pager has the page.  If the parent has the
14832ad1a3f7SMatthew Dillon 			 * page but the page is not valid, the parent's
14842ad1a3f7SMatthew Dillon 			 * object pager must have the page.
14852ad1a3f7SMatthew Dillon 			 *
14862ad1a3f7SMatthew Dillon 			 * If this fails, the parent does not completely shadow
14872ad1a3f7SMatthew Dillon 			 * the object and we might as well give up now.
14882ad1a3f7SMatthew Dillon 			 */
14892ad1a3f7SMatthew Dillon 
14902ad1a3f7SMatthew Dillon 			pp = vm_page_lookup(object, new_pindex);
14912ad1a3f7SMatthew Dillon 			if (
14922ad1a3f7SMatthew Dillon 			    (pp == NULL || pp->valid == 0) &&
14932ad1a3f7SMatthew Dillon 			    !vm_pager_has_page(object, new_pindex, NULL, NULL)
14942ad1a3f7SMatthew Dillon 			) {
14952ad1a3f7SMatthew Dillon 				r = 0;
14962ad1a3f7SMatthew Dillon 				break;
14972ad1a3f7SMatthew Dillon 			}
14982ad1a3f7SMatthew Dillon 		}
14992ad1a3f7SMatthew Dillon 
15002ad1a3f7SMatthew Dillon 		/*
15012ad1a3f7SMatthew Dillon 		 * Check for busy page
15022ad1a3f7SMatthew Dillon 		 */
15032ad1a3f7SMatthew Dillon 		if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
15042ad1a3f7SMatthew Dillon 			vm_page_t pp;
15052ad1a3f7SMatthew Dillon 
15062ad1a3f7SMatthew Dillon 			if (op & OBSC_COLLAPSE_NOWAIT) {
1507c7aebda8SAttilio Rao 				if (!p->valid || vm_page_busied(p)) {
15082ad1a3f7SMatthew Dillon 					p = next;
15092ad1a3f7SMatthew Dillon 					continue;
15102ad1a3f7SMatthew Dillon 				}
15112ad1a3f7SMatthew Dillon 			} else if (op & OBSC_COLLAPSE_WAIT) {
1512c7aebda8SAttilio Rao 				if (vm_page_busied(p)) {
151389f6b863SAttilio Rao 					VM_OBJECT_WUNLOCK(object);
1514c7aebda8SAttilio Rao 					vm_page_lock(p);
1515c7aebda8SAttilio Rao 					VM_OBJECT_WUNLOCK(backing_object);
1516c7aebda8SAttilio Rao 					vm_page_busy_sleep(p, "vmocol");
151789f6b863SAttilio Rao 					VM_OBJECT_WLOCK(object);
151889f6b863SAttilio Rao 					VM_OBJECT_WLOCK(backing_object);
15192ad1a3f7SMatthew Dillon 					/*
15202ad1a3f7SMatthew Dillon 					 * If we slept, anything could have
15212ad1a3f7SMatthew Dillon 					 * happened.  Since the object is
15222ad1a3f7SMatthew Dillon 					 * marked dead, the backing offset
15232ad1a3f7SMatthew Dillon 					 * should not have changed so we
15242ad1a3f7SMatthew Dillon 					 * just restart our scan.
15252ad1a3f7SMatthew Dillon 					 */
15262ad1a3f7SMatthew Dillon 					p = TAILQ_FIRST(&backing_object->memq);
15272ad1a3f7SMatthew Dillon 					continue;
15282ad1a3f7SMatthew Dillon 				}
15292ad1a3f7SMatthew Dillon 			}
15302ad1a3f7SMatthew Dillon 
15312ad1a3f7SMatthew Dillon 			KASSERT(
15322ad1a3f7SMatthew Dillon 			    p->object == backing_object,
15338e99783bSAlan Cox 			    ("vm_object_backing_scan: object mismatch")
15342ad1a3f7SMatthew Dillon 			);
15352ad1a3f7SMatthew Dillon 
15362ad1a3f7SMatthew Dillon 			if (
15372ad1a3f7SMatthew Dillon 			    p->pindex < backing_offset_index ||
15382ad1a3f7SMatthew Dillon 			    new_pindex >= object->size
15392ad1a3f7SMatthew Dillon 			) {
1540*e946b949SAttilio Rao 				if (backing_object->type == OBJT_SWAP)
1541*e946b949SAttilio Rao 					swap_pager_freespace(backing_object,
1542*e946b949SAttilio Rao 					    p->pindex, 1);
1543*e946b949SAttilio Rao 
15442ad1a3f7SMatthew Dillon 				/*
15452ad1a3f7SMatthew Dillon 				 * Page is out of the parent object's range, we
15462ad1a3f7SMatthew Dillon 				 * can simply destroy it.
15472ad1a3f7SMatthew Dillon 				 */
15482965a453SKip Macy 				vm_page_lock(p);
1549f6d89838SAlan Cox 				KASSERT(!pmap_page_is_mapped(p),
1550f6d89838SAlan Cox 				    ("freeing mapped page %p", p));
1551f6d89838SAlan Cox 				if (p->wire_count == 0)
15522ad1a3f7SMatthew Dillon 					vm_page_free(p);
1553f6d89838SAlan Cox 				else
1554f6d89838SAlan Cox 					vm_page_remove(p);
15552965a453SKip Macy 				vm_page_unlock(p);
15562ad1a3f7SMatthew Dillon 				p = next;
15572ad1a3f7SMatthew Dillon 				continue;
15582ad1a3f7SMatthew Dillon 			}
15592ad1a3f7SMatthew Dillon 
15602ad1a3f7SMatthew Dillon 			pp = vm_page_lookup(object, new_pindex);
15612ad1a3f7SMatthew Dillon 			if (
1562e18cc7bfSMax Laier 			    (op & OBSC_COLLAPSE_NOWAIT) != 0 &&
1563e18cc7bfSMax Laier 			    (pp != NULL && pp->valid == 0)
1564e18cc7bfSMax Laier 			) {
1565*e946b949SAttilio Rao 				if (backing_object->type == OBJT_SWAP)
1566*e946b949SAttilio Rao 					swap_pager_freespace(backing_object,
1567*e946b949SAttilio Rao 					    p->pindex, 1);
1568*e946b949SAttilio Rao 
1569e18cc7bfSMax Laier 				/*
1570e18cc7bfSMax Laier 				 * The page in the parent is not (yet) valid.
1571e18cc7bfSMax Laier 				 * We don't know anything about the state of
1572e18cc7bfSMax Laier 				 * the original page.  It might be mapped,
1573e18cc7bfSMax Laier 				 * so we must avoid the next if here.
1574e18cc7bfSMax Laier 				 *
1575e18cc7bfSMax Laier 				 * This is due to a race in vm_fault() where
1576e18cc7bfSMax Laier 				 * we must unbusy the original (backing_obj)
1577e18cc7bfSMax Laier 				 * page before we can (re)lock the parent.
1578e18cc7bfSMax Laier 				 * Hence we can get here.
1579e18cc7bfSMax Laier 				 */
1580e18cc7bfSMax Laier 				p = next;
1581e18cc7bfSMax Laier 				continue;
1582e18cc7bfSMax Laier 			}
1583e18cc7bfSMax Laier 			if (
15842ad1a3f7SMatthew Dillon 			    pp != NULL ||
15852ad1a3f7SMatthew Dillon 			    vm_pager_has_page(object, new_pindex, NULL, NULL)
15862ad1a3f7SMatthew Dillon 			) {
1587*e946b949SAttilio Rao 				if (backing_object->type == OBJT_SWAP)
1588*e946b949SAttilio Rao 					swap_pager_freespace(backing_object,
1589*e946b949SAttilio Rao 					    p->pindex, 1);
1590*e946b949SAttilio Rao 
15912ad1a3f7SMatthew Dillon 				/*
15922ad1a3f7SMatthew Dillon 				 * page already exists in parent OR swap exists
15932ad1a3f7SMatthew Dillon 				 * for this location in the parent.  Destroy
15942ad1a3f7SMatthew Dillon 				 * the original page from the backing object.
15952ad1a3f7SMatthew Dillon 				 *
15962ad1a3f7SMatthew Dillon 				 * Leave the parent's page alone
15972ad1a3f7SMatthew Dillon 				 */
15982965a453SKip Macy 				vm_page_lock(p);
1599f6d89838SAlan Cox 				KASSERT(!pmap_page_is_mapped(p),
1600f6d89838SAlan Cox 				    ("freeing mapped page %p", p));
1601f6d89838SAlan Cox 				if (p->wire_count == 0)
16022ad1a3f7SMatthew Dillon 					vm_page_free(p);
1603f6d89838SAlan Cox 				else
1604f6d89838SAlan Cox 					vm_page_remove(p);
16052965a453SKip Macy 				vm_page_unlock(p);
16062ad1a3f7SMatthew Dillon 				p = next;
16072ad1a3f7SMatthew Dillon 				continue;
16082ad1a3f7SMatthew Dillon 			}
16092ad1a3f7SMatthew Dillon 
1610*e946b949SAttilio Rao 			/*
1611*e946b949SAttilio Rao 			 * Page does not exist in parent, rename the
1612*e946b949SAttilio Rao 			 * page from the backing object to the main object.
1613*e946b949SAttilio Rao 			 *
1614*e946b949SAttilio Rao 			 * If the page was mapped to a process, it can remain
1615*e946b949SAttilio Rao 			 * mapped through the rename.
1616*e946b949SAttilio Rao 			 * vm_page_rename() will handle dirty and cache.
1617*e946b949SAttilio Rao 			 */
1618*e946b949SAttilio Rao 			if (vm_page_rename(p, object, new_pindex)) {
1619*e946b949SAttilio Rao 				if (op & OBSC_COLLAPSE_NOWAIT) {
1620*e946b949SAttilio Rao 					p = next;
1621*e946b949SAttilio Rao 					continue;
1622*e946b949SAttilio Rao 				}
1623*e946b949SAttilio Rao 				VM_OBJECT_WLOCK(backing_object);
1624*e946b949SAttilio Rao 				VM_OBJECT_WUNLOCK(object);
1625*e946b949SAttilio Rao 				VM_WAIT;
1626*e946b949SAttilio Rao 				VM_OBJECT_WLOCK(object);
1627*e946b949SAttilio Rao 				VM_OBJECT_WLOCK(backing_object);
1628*e946b949SAttilio Rao 				p = TAILQ_FIRST(&backing_object->memq);
1629*e946b949SAttilio Rao 				continue;
1630*e946b949SAttilio Rao 			}
1631*e946b949SAttilio Rao 			if (backing_object->type == OBJT_SWAP)
1632*e946b949SAttilio Rao 				swap_pager_freespace(backing_object, p->pindex,
1633*e946b949SAttilio Rao 				    1);
1634*e946b949SAttilio Rao 
1635f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
1636f8a47341SAlan Cox 			/*
1637f8a47341SAlan Cox 			 * Rename the reservation.
1638f8a47341SAlan Cox 			 */
1639f8a47341SAlan Cox 			vm_reserv_rename(p, object, backing_object,
1640f8a47341SAlan Cox 			    backing_offset_index);
1641f8a47341SAlan Cox #endif
16422ad1a3f7SMatthew Dillon 		}
16432ad1a3f7SMatthew Dillon 		p = next;
16442ad1a3f7SMatthew Dillon 	}
16452ad1a3f7SMatthew Dillon 	return (r);
16462ad1a3f7SMatthew Dillon }
16472ad1a3f7SMatthew Dillon 
1648df8bae1dSRodney W. Grimes 
1649df8bae1dSRodney W. Grimes /*
16502fe6e4d7SDavid Greenman  * this version of collapse allows the operation to occur earlier and
16512fe6e4d7SDavid Greenman  * when paging_in_progress is true for an object...  This is not a complete
16522fe6e4d7SDavid Greenman  * operation, but should plug 99.9% of the rest of the leaks.
16532fe6e4d7SDavid Greenman  */
16542fe6e4d7SDavid Greenman static void
16551b40f8c0SMatthew Dillon vm_object_qcollapse(vm_object_t object)
16562fe6e4d7SDavid Greenman {
16572ad1a3f7SMatthew Dillon 	vm_object_t backing_object = object->backing_object;
16582fe6e4d7SDavid Greenman 
165989f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
166089f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(backing_object);
16611b40f8c0SMatthew Dillon 
16622fe6e4d7SDavid Greenman 	if (backing_object->ref_count != 1)
16632fe6e4d7SDavid Greenman 		return;
16642fe6e4d7SDavid Greenman 
16652ad1a3f7SMatthew Dillon 	vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
16662fe6e4d7SDavid Greenman }
16672fe6e4d7SDavid Greenman 
1668df8bae1dSRodney W. Grimes /*
1669df8bae1dSRodney W. Grimes  *	vm_object_collapse:
1670df8bae1dSRodney W. Grimes  *
1671df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
1672df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
1673df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
1674df8bae1dSRodney W. Grimes  */
167526f9a767SRodney W. Grimes void
16761b40f8c0SMatthew Dillon vm_object_collapse(vm_object_t object)
1677df8bae1dSRodney W. Grimes {
167889f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
167923955314SAlfred Perlstein 
1680df8bae1dSRodney W. Grimes 	while (TRUE) {
16812ad1a3f7SMatthew Dillon 		vm_object_t backing_object;
16822ad1a3f7SMatthew Dillon 
1683df8bae1dSRodney W. Grimes 		/*
1684df8bae1dSRodney W. Grimes 		 * Verify that the conditions are right for collapse:
1685df8bae1dSRodney W. Grimes 		 *
16862ad1a3f7SMatthew Dillon 		 * The object exists and the backing object exists.
1687df8bae1dSRodney W. Grimes 		 */
168824a1cce3SDavid Greenman 		if ((backing_object = object->backing_object) == NULL)
16892ad1a3f7SMatthew Dillon 			break;
1690df8bae1dSRodney W. Grimes 
1691f919ebdeSDavid Greenman 		/*
1692f919ebdeSDavid Greenman 		 * we check the backing object first, because it is most likely
169324a1cce3SDavid Greenman 		 * not collapsable.
1694f919ebdeSDavid Greenman 		 */
169589f6b863SAttilio Rao 		VM_OBJECT_WLOCK(backing_object);
169624a1cce3SDavid Greenman 		if (backing_object->handle != NULL ||
169724a1cce3SDavid Greenman 		    (backing_object->type != OBJT_DEFAULT &&
169824a1cce3SDavid Greenman 		     backing_object->type != OBJT_SWAP) ||
1699f919ebdeSDavid Greenman 		    (backing_object->flags & OBJ_DEAD) ||
170024a1cce3SDavid Greenman 		    object->handle != NULL ||
170124a1cce3SDavid Greenman 		    (object->type != OBJT_DEFAULT &&
170224a1cce3SDavid Greenman 		     object->type != OBJT_SWAP) ||
170324a1cce3SDavid Greenman 		    (object->flags & OBJ_DEAD)) {
170489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(backing_object);
17052ad1a3f7SMatthew Dillon 			break;
170624a1cce3SDavid Greenman 		}
17079b4814bbSDavid Greenman 
17082ad1a3f7SMatthew Dillon 		if (
17092ad1a3f7SMatthew Dillon 		    object->paging_in_progress != 0 ||
17102ad1a3f7SMatthew Dillon 		    backing_object->paging_in_progress != 0
17112ad1a3f7SMatthew Dillon 		) {
1712b9921222SDavid Greenman 			vm_object_qcollapse(object);
171389f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(backing_object);
17142ad1a3f7SMatthew Dillon 			break;
1715df8bae1dSRodney W. Grimes 		}
171626f9a767SRodney W. Grimes 		/*
17170d94caffSDavid Greenman 		 * We know that we can either collapse the backing object (if
17182ad1a3f7SMatthew Dillon 		 * the parent is the only reference to it) or (perhaps) have
17192ad1a3f7SMatthew Dillon 		 * the parent bypass the object if the parent happens to shadow
17202ad1a3f7SMatthew Dillon 		 * all the resident pages in the entire backing object.
17212ad1a3f7SMatthew Dillon 		 *
17222ad1a3f7SMatthew Dillon 		 * This is ignoring pager-backed pages such as swap pages.
17232ad1a3f7SMatthew Dillon 		 * vm_object_backing_scan fails the shadowing test in this
17242ad1a3f7SMatthew Dillon 		 * case.
1725df8bae1dSRodney W. Grimes 		 */
1726df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
1727df8bae1dSRodney W. Grimes 			/*
17282ad1a3f7SMatthew Dillon 			 * If there is exactly one reference to the backing
17292ad1a3f7SMatthew Dillon 			 * object, we can collapse it into the parent.
1730df8bae1dSRodney W. Grimes 			 */
17312ad1a3f7SMatthew Dillon 			vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1732df8bae1dSRodney W. Grimes 
1733f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
1734f8a47341SAlan Cox 			/*
1735f8a47341SAlan Cox 			 * Break any reservations from backing_object.
1736f8a47341SAlan Cox 			 */
1737f8a47341SAlan Cox 			if (__predict_false(!LIST_EMPTY(&backing_object->rvq)))
1738f8a47341SAlan Cox 				vm_reserv_break_all(backing_object);
1739f8a47341SAlan Cox #endif
1740f8a47341SAlan Cox 
1741df8bae1dSRodney W. Grimes 			/*
1742df8bae1dSRodney W. Grimes 			 * Move the pager from backing_object to object.
1743df8bae1dSRodney W. Grimes 			 */
17446be36525SAlan Cox 			if (backing_object->type == OBJT_SWAP) {
174524a1cce3SDavid Greenman 				/*
1746c7c8dd7eSAlan Cox 				 * swap_pager_copy() can sleep, in which case
1747c7c8dd7eSAlan Cox 				 * the backing_object's and object's locks are
1748c7c8dd7eSAlan Cox 				 * released and reacquired.
1749571a1e92SAttilio Rao 				 * Since swap_pager_copy() is being asked to
1750571a1e92SAttilio Rao 				 * destroy the source, it will change the
1751571a1e92SAttilio Rao 				 * backing_object's type to OBJT_DEFAULT.
175224a1cce3SDavid Greenman 				 */
17531c7c3c6aSMatthew Dillon 				swap_pager_copy(
17541c7c3c6aSMatthew Dillon 				    backing_object,
17551c7c3c6aSMatthew Dillon 				    object,
17561c7c3c6aSMatthew Dillon 				    OFF_TO_IDX(object->backing_object_offset), TRUE);
17577bfda801SAlan Cox 
17587bfda801SAlan Cox 				/*
17597bfda801SAlan Cox 				 * Free any cached pages from backing_object.
17607bfda801SAlan Cox 				 */
1761c9341161SAttilio Rao 				if (__predict_false(
1762c9341161SAttilio Rao 				    !vm_object_cache_is_empty(backing_object)))
1763c9444914SAlan Cox 					vm_page_cache_free(backing_object, 0, 0);
1764c0503609SDavid Greenman 			}
1765df8bae1dSRodney W. Grimes 			/*
1766df8bae1dSRodney W. Grimes 			 * Object now shadows whatever backing_object did.
17672ad1a3f7SMatthew Dillon 			 * Note that the reference to
17682ad1a3f7SMatthew Dillon 			 * backing_object->backing_object moves from within
17692ad1a3f7SMatthew Dillon 			 * backing_object to within object.
1770df8bae1dSRodney W. Grimes 			 */
17711c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
17724f7c7f6eSAlan Cox 			backing_object->shadow_count--;
1773de5f6a77SJohn Dyson 			if (backing_object->backing_object) {
177489f6b863SAttilio Rao 				VM_OBJECT_WLOCK(backing_object->backing_object);
17751c500307SAlan Cox 				LIST_REMOVE(backing_object, shadow_list);
177643186e53SAlan Cox 				LIST_INSERT_HEAD(
177743186e53SAlan Cox 				    &backing_object->backing_object->shadow_head,
177843186e53SAlan Cox 				    object, shadow_list);
177943186e53SAlan Cox 				/*
178043186e53SAlan Cox 				 * The shadow_count has not changed.
178143186e53SAlan Cox 				 */
178289f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(backing_object->backing_object);
1783de5f6a77SJohn Dyson 			}
178424a1cce3SDavid Greenman 			object->backing_object = backing_object->backing_object;
17852ad1a3f7SMatthew Dillon 			object->backing_object_offset +=
17862ad1a3f7SMatthew Dillon 			    backing_object->backing_object_offset;
17872ad1a3f7SMatthew Dillon 
1788df8bae1dSRodney W. Grimes 			/*
1789df8bae1dSRodney W. Grimes 			 * Discard backing_object.
1790df8bae1dSRodney W. Grimes 			 *
17910d94caffSDavid Greenman 			 * Since the backing object has no pages, no pager left,
17920d94caffSDavid Greenman 			 * and no object references within it, all that is
17930d94caffSDavid Greenman 			 * necessary is to dispose of it.
1794df8bae1dSRodney W. Grimes 			 */
17959b4d473aSKonstantin Belousov 			KASSERT(backing_object->ref_count == 1, (
17969b4d473aSKonstantin Belousov "backing_object %p was somehow re-referenced during collapse!",
17979b4d473aSKonstantin Belousov 			    backing_object));
179889f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(backing_object);
17999b4d473aSKonstantin Belousov 			vm_object_destroy(backing_object);
1800df8bae1dSRodney W. Grimes 
1801df8bae1dSRodney W. Grimes 			object_collapses++;
18020d94caffSDavid Greenman 		} else {
180395e5e988SJohn Dyson 			vm_object_t new_backing_object;
1804df8bae1dSRodney W. Grimes 
1805df8bae1dSRodney W. Grimes 			/*
18062ad1a3f7SMatthew Dillon 			 * If we do not entirely shadow the backing object,
18072ad1a3f7SMatthew Dillon 			 * there is nothing we can do so we give up.
1808df8bae1dSRodney W. Grimes 			 */
1809df59a0feSJeff Roberson 			if (object->resident_page_count != object->size &&
1810df59a0feSJeff Roberson 			    vm_object_backing_scan(object,
1811df59a0feSJeff Roberson 			    OBSC_TEST_ALL_SHADOWED) == 0) {
181289f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(backing_object);
18132ad1a3f7SMatthew Dillon 				break;
181424a1cce3SDavid Greenman 			}
1815df8bae1dSRodney W. Grimes 
1816df8bae1dSRodney W. Grimes 			/*
18170d94caffSDavid Greenman 			 * Make the parent shadow the next object in the
18180d94caffSDavid Greenman 			 * chain.  Deallocating backing_object will not remove
18190d94caffSDavid Greenman 			 * it, since its reference count is at least 2.
1820df8bae1dSRodney W. Grimes 			 */
18211c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
1822eaf13dd7SJohn Dyson 			backing_object->shadow_count--;
182395e5e988SJohn Dyson 
182495e5e988SJohn Dyson 			new_backing_object = backing_object->backing_object;
18258aef1712SMatthew Dillon 			if ((object->backing_object = new_backing_object) != NULL) {
182689f6b863SAttilio Rao 				VM_OBJECT_WLOCK(new_backing_object);
18271c500307SAlan Cox 				LIST_INSERT_HEAD(
18282ad1a3f7SMatthew Dillon 				    &new_backing_object->shadow_head,
18292ad1a3f7SMatthew Dillon 				    object,
18302ad1a3f7SMatthew Dillon 				    shadow_list
18312ad1a3f7SMatthew Dillon 				);
1832eaf13dd7SJohn Dyson 				new_backing_object->shadow_count++;
1833b921a12bSAlan Cox 				vm_object_reference_locked(new_backing_object);
183489f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(new_backing_object);
183595e5e988SJohn Dyson 				object->backing_object_offset +=
183695e5e988SJohn Dyson 					backing_object->backing_object_offset;
1837de5f6a77SJohn Dyson 			}
1838df8bae1dSRodney W. Grimes 
1839df8bae1dSRodney W. Grimes 			/*
18400d94caffSDavid Greenman 			 * Drop the reference count on backing_object. Since
184122ec553fSAlan Cox 			 * its ref_count was at least 2, it will not vanish.
1842df8bae1dSRodney W. Grimes 			 */
184322ec553fSAlan Cox 			backing_object->ref_count--;
184489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(backing_object);
1845df8bae1dSRodney W. Grimes 			object_bypasses++;
1846df8bae1dSRodney W. Grimes 		}
1847df8bae1dSRodney W. Grimes 
1848df8bae1dSRodney W. Grimes 		/*
1849df8bae1dSRodney W. Grimes 		 * Try again with this object's new backing object.
1850df8bae1dSRodney W. Grimes 		 */
1851df8bae1dSRodney W. Grimes 	}
1852df8bae1dSRodney W. Grimes }
1853df8bae1dSRodney W. Grimes 
1854df8bae1dSRodney W. Grimes /*
1855bff99f0dSAlan Cox  *	vm_object_page_remove:
1856df8bae1dSRodney W. Grimes  *
185768855966SAlan Cox  *	For the given object, either frees or invalidates each of the
18586bbee8e2SAlan Cox  *	specified pages.  In general, a page is freed.  However, if a page is
18596bbee8e2SAlan Cox  *	wired for any reason other than the existence of a managed, wired
18606bbee8e2SAlan Cox  *	mapping, then it may be invalidated but not removed from the object.
18616bbee8e2SAlan Cox  *	Pages are specified by the given range ["start", "end") and the option
18626bbee8e2SAlan Cox  *	OBJPR_CLEANONLY.  As a special case, if "end" is zero, then the range
18636bbee8e2SAlan Cox  *	extends from "start" to the end of the object.  If the option
18646bbee8e2SAlan Cox  *	OBJPR_CLEANONLY is specified, then only the non-dirty pages within the
18656bbee8e2SAlan Cox  *	specified range are affected.  If the option OBJPR_NOTMAPPED is
18666bbee8e2SAlan Cox  *	specified, then the pages within the specified range must have no
18676bbee8e2SAlan Cox  *	mappings.  Otherwise, if this option is not specified, any mappings to
18686bbee8e2SAlan Cox  *	the specified pages are removed before the pages are freed or
18696bbee8e2SAlan Cox  *	invalidated.
187068855966SAlan Cox  *
18716bbee8e2SAlan Cox  *	In general, this operation should only be performed on objects that
18726bbee8e2SAlan Cox  *	contain managed pages.  There are, however, two exceptions.  First, it
18736bbee8e2SAlan Cox  *	is performed on the kernel and kmem objects by vm_map_entry_delete().
18746bbee8e2SAlan Cox  *	Second, it is used by msync(..., MS_INVALIDATE) to invalidate device-
18756bbee8e2SAlan Cox  *	backed pages.  In both of these cases, the option OBJPR_CLEANONLY must
18766bbee8e2SAlan Cox  *	not be specified and the option OBJPR_NOTMAPPED must be specified.
1877df8bae1dSRodney W. Grimes  *
1878df8bae1dSRodney W. Grimes  *	The object must be locked.
1879df8bae1dSRodney W. Grimes  */
188026f9a767SRodney W. Grimes void
1881ecde4b32SAlan Cox vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
18826bbee8e2SAlan Cox     int options)
1883df8bae1dSRodney W. Grimes {
1884d031cff1SMatthew Dillon 	vm_page_t p, next;
188559677d3cSAlan Cox 	int wirings;
1886df8bae1dSRodney W. Grimes 
188789f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
188828634820SAlan Cox 	KASSERT((object->flags & OBJ_UNMANAGED) == 0 ||
18896bbee8e2SAlan Cox 	    (options & (OBJPR_CLEANONLY | OBJPR_NOTMAPPED)) == OBJPR_NOTMAPPED,
18906bbee8e2SAlan Cox 	    ("vm_object_page_remove: illegal options for object %p", object));
1891ecde4b32SAlan Cox 	if (object->resident_page_count == 0)
189225732691SAlan Cox 		goto skipmemq;
1893d474eaaaSDoug Rabson 	vm_object_pip_add(object, 1);
189426f9a767SRodney W. Grimes again:
1895b382c10aSKonstantin Belousov 	p = vm_page_find_least(object, start);
18962965a453SKip Macy 
189775741c04SAlan Cox 	/*
18986bbee8e2SAlan Cox 	 * Here, the variable "p" is either (1) the page with the least pindex
18996bbee8e2SAlan Cox 	 * greater than or equal to the parameter "start" or (2) NULL.
190075741c04SAlan Cox 	 */
19016bbee8e2SAlan Cox 	for (; p != NULL && (p->pindex < end || end == 0); p = next) {
1902b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
190375741c04SAlan Cox 
190459677d3cSAlan Cox 		/*
19056bbee8e2SAlan Cox 		 * If the page is wired for any reason besides the existence
19066bbee8e2SAlan Cox 		 * of managed, wired mappings, then it cannot be freed.  For
19076bbee8e2SAlan Cox 		 * example, fictitious pages, which represent device memory,
19086bbee8e2SAlan Cox 		 * are inherently wired and cannot be freed.  They can,
19096bbee8e2SAlan Cox 		 * however, be invalidated if the option OBJPR_CLEANONLY is
19106bbee8e2SAlan Cox 		 * not specified.
191159677d3cSAlan Cox 		 */
19122965a453SKip Macy 		vm_page_lock(p);
191359677d3cSAlan Cox 		if ((wirings = p->wire_count) != 0 &&
191459677d3cSAlan Cox 		    (wirings = pmap_page_wired_mappings(p)) != p->wire_count) {
1915ebf5d94eSKonstantin Belousov 			if ((options & (OBJPR_NOTWIRED | OBJPR_NOTMAPPED)) ==
1916ebf5d94eSKonstantin Belousov 			    0) {
19174fec79beSAlan Cox 				pmap_remove_all(p);
19186bbee8e2SAlan Cox 				/* Account for removal of wired mappings. */
19196bbee8e2SAlan Cox 				if (wirings != 0)
192059677d3cSAlan Cox 					p->wire_count -= wirings;
19216bbee8e2SAlan Cox 			}
19226bbee8e2SAlan Cox 			if ((options & OBJPR_CLEANONLY) == 0) {
1923bd7e5f99SJohn Dyson 				p->valid = 0;
1924a28042d1SAlan Cox 				vm_page_undirty(p);
1925a28042d1SAlan Cox 			}
1926ebf5d94eSKonstantin Belousov 			goto next;
19270d94caffSDavid Greenman 		}
1928c7aebda8SAttilio Rao 		if (vm_page_busied(p)) {
1929c7aebda8SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
1930c7aebda8SAttilio Rao 			vm_page_busy_sleep(p, "vmopar");
1931c7aebda8SAttilio Rao 			VM_OBJECT_WLOCK(object);
193226f9a767SRodney W. Grimes 			goto again;
1933c7aebda8SAttilio Rao 		}
193468855966SAlan Cox 		KASSERT((p->flags & PG_FICTITIOUS) == 0,
193568855966SAlan Cox 		    ("vm_object_page_remove: page %p is fictitious", p));
19366bbee8e2SAlan Cox 		if ((options & OBJPR_CLEANONLY) != 0 && p->valid != 0) {
19376bbee8e2SAlan Cox 			if ((options & OBJPR_NOTMAPPED) == 0)
193878985e42SAlan Cox 				pmap_remove_write(p);
1939ebf5d94eSKonstantin Belousov 			if (p->dirty)
1940ebf5d94eSKonstantin Belousov 				goto next;
19412965a453SKip Macy 		}
19426bbee8e2SAlan Cox 		if ((options & OBJPR_NOTMAPPED) == 0) {
1943ebf5d94eSKonstantin Belousov 			if ((options & OBJPR_NOTWIRED) != 0 && wirings != 0)
1944ebf5d94eSKonstantin Belousov 				goto next;
19454fec79beSAlan Cox 			pmap_remove_all(p);
19466bbee8e2SAlan Cox 			/* Account for removal of wired mappings. */
19475f9c767bSKonstantin Belousov 			if (wirings != 0) {
19485f9c767bSKonstantin Belousov 				KASSERT(p->wire_count == wirings,
19495f9c767bSKonstantin Belousov 				    ("inconsistent wire count %d %d %p",
19505f9c767bSKonstantin Belousov 				    p->wire_count, wirings, p));
19515f9c767bSKonstantin Belousov 				p->wire_count = 0;
19525f9c767bSKonstantin Belousov 				atomic_subtract_int(&cnt.v_wire_count, 1);
19535f9c767bSKonstantin Belousov 			}
19546bbee8e2SAlan Cox 		}
1955df8bae1dSRodney W. Grimes 		vm_page_free(p);
1956ebf5d94eSKonstantin Belousov next:
19572965a453SKip Macy 		vm_page_unlock(p);
19582965a453SKip Macy 	}
1959f919ebdeSDavid Greenman 	vm_object_pip_wakeup(object);
196025732691SAlan Cox skipmemq:
1961c9341161SAttilio Rao 	if (__predict_false(!vm_object_cache_is_empty(object)))
1962c9444914SAlan Cox 		vm_page_cache_free(object, start, end);
1963c0503609SDavid Greenman }
1964df8bae1dSRodney W. Grimes 
1965df8bae1dSRodney W. Grimes /*
1966936c09acSJohn Baldwin  *	vm_object_page_cache:
1967936c09acSJohn Baldwin  *
1968936c09acSJohn Baldwin  *	For the given object, attempt to move the specified clean
1969936c09acSJohn Baldwin  *	pages to the cache queue.  If a page is wired for any reason,
1970936c09acSJohn Baldwin  *	then it will not be changed.  Pages are specified by the given
1971936c09acSJohn Baldwin  *	range ["start", "end").  As a special case, if "end" is zero,
1972936c09acSJohn Baldwin  *	then the range extends from "start" to the end of the object.
1973936c09acSJohn Baldwin  *	Any mappings to the specified pages are removed before the
1974936c09acSJohn Baldwin  *	pages are moved to the cache queue.
1975936c09acSJohn Baldwin  *
1976936c09acSJohn Baldwin  *	This operation should only be performed on objects that
197728634820SAlan Cox  *	contain non-fictitious, managed pages.
1978936c09acSJohn Baldwin  *
1979936c09acSJohn Baldwin  *	The object must be locked.
1980936c09acSJohn Baldwin  */
1981936c09acSJohn Baldwin void
1982936c09acSJohn Baldwin vm_object_page_cache(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1983936c09acSJohn Baldwin {
1984936c09acSJohn Baldwin 	struct mtx *mtx, *new_mtx;
1985936c09acSJohn Baldwin 	vm_page_t p, next;
1986936c09acSJohn Baldwin 
198789f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
198828634820SAlan Cox 	KASSERT((object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0,
1989936c09acSJohn Baldwin 	    ("vm_object_page_cache: illegal object %p", object));
1990936c09acSJohn Baldwin 	if (object->resident_page_count == 0)
1991936c09acSJohn Baldwin 		return;
1992936c09acSJohn Baldwin 	p = vm_page_find_least(object, start);
1993936c09acSJohn Baldwin 
1994936c09acSJohn Baldwin 	/*
1995936c09acSJohn Baldwin 	 * Here, the variable "p" is either (1) the page with the least pindex
1996936c09acSJohn Baldwin 	 * greater than or equal to the parameter "start" or (2) NULL.
1997936c09acSJohn Baldwin 	 */
1998936c09acSJohn Baldwin 	mtx = NULL;
1999936c09acSJohn Baldwin 	for (; p != NULL && (p->pindex < end || end == 0); p = next) {
2000936c09acSJohn Baldwin 		next = TAILQ_NEXT(p, listq);
2001936c09acSJohn Baldwin 
2002936c09acSJohn Baldwin 		/*
2003936c09acSJohn Baldwin 		 * Avoid releasing and reacquiring the same page lock.
2004936c09acSJohn Baldwin 		 */
2005936c09acSJohn Baldwin 		new_mtx = vm_page_lockptr(p);
2006936c09acSJohn Baldwin 		if (mtx != new_mtx) {
2007936c09acSJohn Baldwin 			if (mtx != NULL)
2008936c09acSJohn Baldwin 				mtx_unlock(mtx);
2009936c09acSJohn Baldwin 			mtx = new_mtx;
2010936c09acSJohn Baldwin 			mtx_lock(mtx);
2011936c09acSJohn Baldwin 		}
2012936c09acSJohn Baldwin 		vm_page_try_to_cache(p);
2013936c09acSJohn Baldwin 	}
2014936c09acSJohn Baldwin 	if (mtx != NULL)
2015936c09acSJohn Baldwin 		mtx_unlock(mtx);
2016936c09acSJohn Baldwin }
2017936c09acSJohn Baldwin 
2018936c09acSJohn Baldwin /*
2019387aabc5SAlan Cox  *	Populate the specified range of the object with valid pages.  Returns
2020387aabc5SAlan Cox  *	TRUE if the range is successfully populated and FALSE otherwise.
2021387aabc5SAlan Cox  *
2022387aabc5SAlan Cox  *	Note: This function should be optimized to pass a larger array of
2023387aabc5SAlan Cox  *	pages to vm_pager_get_pages() before it is applied to a non-
2024387aabc5SAlan Cox  *	OBJT_DEVICE object.
2025387aabc5SAlan Cox  *
2026387aabc5SAlan Cox  *	The object must be locked.
2027387aabc5SAlan Cox  */
2028387aabc5SAlan Cox boolean_t
2029387aabc5SAlan Cox vm_object_populate(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
2030387aabc5SAlan Cox {
2031387aabc5SAlan Cox 	vm_page_t m, ma[1];
2032387aabc5SAlan Cox 	vm_pindex_t pindex;
2033387aabc5SAlan Cox 	int rv;
2034387aabc5SAlan Cox 
203589f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
2036387aabc5SAlan Cox 	for (pindex = start; pindex < end; pindex++) {
2037387aabc5SAlan Cox 		m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL |
2038387aabc5SAlan Cox 		    VM_ALLOC_RETRY);
2039387aabc5SAlan Cox 		if (m->valid != VM_PAGE_BITS_ALL) {
2040387aabc5SAlan Cox 			ma[0] = m;
2041387aabc5SAlan Cox 			rv = vm_pager_get_pages(object, ma, 1, 0);
2042387aabc5SAlan Cox 			m = vm_page_lookup(object, pindex);
2043387aabc5SAlan Cox 			if (m == NULL)
2044387aabc5SAlan Cox 				break;
2045387aabc5SAlan Cox 			if (rv != VM_PAGER_OK) {
20462965a453SKip Macy 				vm_page_lock(m);
2047387aabc5SAlan Cox 				vm_page_free(m);
20482965a453SKip Macy 				vm_page_unlock(m);
2049387aabc5SAlan Cox 				break;
2050387aabc5SAlan Cox 			}
2051387aabc5SAlan Cox 		}
2052387aabc5SAlan Cox 		/*
2053387aabc5SAlan Cox 		 * Keep "m" busy because a subsequent iteration may unlock
2054387aabc5SAlan Cox 		 * the object.
2055387aabc5SAlan Cox 		 */
2056387aabc5SAlan Cox 	}
2057387aabc5SAlan Cox 	if (pindex > start) {
2058387aabc5SAlan Cox 		m = vm_page_lookup(object, start);
2059387aabc5SAlan Cox 		while (m != NULL && m->pindex < pindex) {
2060c7aebda8SAttilio Rao 			vm_page_xunbusy(m);
2061387aabc5SAlan Cox 			m = TAILQ_NEXT(m, listq);
2062387aabc5SAlan Cox 		}
2063387aabc5SAlan Cox 	}
2064387aabc5SAlan Cox 	return (pindex == end);
2065387aabc5SAlan Cox }
2066387aabc5SAlan Cox 
2067387aabc5SAlan Cox /*
2068df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
2069df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
2070df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
2071df8bae1dSRodney W. Grimes  *
2072df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
2073df8bae1dSRodney W. Grimes  *
2074df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
2075df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
2076df8bae1dSRodney W. Grimes  *
2077df8bae1dSRodney W. Grimes  *	Parameters:
2078df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
2079df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
2080df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
208157a21abaSAlan Cox  *		next_size	Size of reference to the second object
20823364c323SKonstantin Belousov  *		reserved	Indicator that extension region has
20833364c323SKonstantin Belousov  *				swap accounted for
2084df8bae1dSRodney W. Grimes  *
2085df8bae1dSRodney W. Grimes  *	Conditions:
2086df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
2087df8bae1dSRodney W. Grimes  */
20880d94caffSDavid Greenman boolean_t
208957a21abaSAlan Cox vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset,
20903364c323SKonstantin Belousov     vm_size_t prev_size, vm_size_t next_size, boolean_t reserved)
2091df8bae1dSRodney W. Grimes {
2092ea41812fSAlan Cox 	vm_pindex_t next_pindex;
2093df8bae1dSRodney W. Grimes 
209400e1854aSAlan Cox 	if (prev_object == NULL)
2095df8bae1dSRodney W. Grimes 		return (TRUE);
209689f6b863SAttilio Rao 	VM_OBJECT_WLOCK(prev_object);
20974112823fSMatthew Dillon 	if (prev_object->type != OBJT_DEFAULT &&
20984112823fSMatthew Dillon 	    prev_object->type != OBJT_SWAP) {
209989f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(prev_object);
210030dcfc09SJohn Dyson 		return (FALSE);
210130dcfc09SJohn Dyson 	}
210230dcfc09SJohn Dyson 
2103df8bae1dSRodney W. Grimes 	/*
2104df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
2105df8bae1dSRodney W. Grimes 	 */
2106df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
2107df8bae1dSRodney W. Grimes 
2108df8bae1dSRodney W. Grimes 	/*
21090d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
21100d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
21110d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
2112df8bae1dSRodney W. Grimes 	 */
21138cc7e047SJohn Dyson 	if (prev_object->backing_object != NULL) {
211489f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(prev_object);
2115df8bae1dSRodney W. Grimes 		return (FALSE);
2116df8bae1dSRodney W. Grimes 	}
2117a316d390SJohn Dyson 
2118a316d390SJohn Dyson 	prev_size >>= PAGE_SHIFT;
2119a316d390SJohn Dyson 	next_size >>= PAGE_SHIFT;
212057a21abaSAlan Cox 	next_pindex = OFF_TO_IDX(prev_offset) + prev_size;
21218cc7e047SJohn Dyson 
21228cc7e047SJohn Dyson 	if ((prev_object->ref_count > 1) &&
2123ea41812fSAlan Cox 	    (prev_object->size != next_pindex)) {
212489f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(prev_object);
21258cc7e047SJohn Dyson 		return (FALSE);
21268cc7e047SJohn Dyson 	}
21278cc7e047SJohn Dyson 
2128df8bae1dSRodney W. Grimes 	/*
21293364c323SKonstantin Belousov 	 * Account for the charge.
21303364c323SKonstantin Belousov 	 */
2131ef694c1aSEdward Tomasz Napierala 	if (prev_object->cred != NULL) {
21323364c323SKonstantin Belousov 
21333364c323SKonstantin Belousov 		/*
21343364c323SKonstantin Belousov 		 * If prev_object was charged, then this mapping,
21353364c323SKonstantin Belousov 		 * althought not charged now, may become writable
2136ef694c1aSEdward Tomasz Napierala 		 * later. Non-NULL cred in the object would prevent
21373364c323SKonstantin Belousov 		 * swap reservation during enabling of the write
21383364c323SKonstantin Belousov 		 * access, so reserve swap now. Failed reservation
21393364c323SKonstantin Belousov 		 * cause allocation of the separate object for the map
21403364c323SKonstantin Belousov 		 * entry, and swap reservation for this entry is
21413364c323SKonstantin Belousov 		 * managed in appropriate time.
21423364c323SKonstantin Belousov 		 */
2143ef694c1aSEdward Tomasz Napierala 		if (!reserved && !swap_reserve_by_cred(ptoa(next_size),
2144ef694c1aSEdward Tomasz Napierala 		    prev_object->cred)) {
21453364c323SKonstantin Belousov 			return (FALSE);
21463364c323SKonstantin Belousov 		}
21473364c323SKonstantin Belousov 		prev_object->charge += ptoa(next_size);
21483364c323SKonstantin Belousov 	}
21493364c323SKonstantin Belousov 
21503364c323SKonstantin Belousov 	/*
21510d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
21520d94caffSDavid Greenman 	 * deallocation.
2153df8bae1dSRodney W. Grimes 	 */
2154ea41812fSAlan Cox 	if (next_pindex < prev_object->size) {
21556bbee8e2SAlan Cox 		vm_object_page_remove(prev_object, next_pindex, next_pindex +
21566bbee8e2SAlan Cox 		    next_size, 0);
2157ea41812fSAlan Cox 		if (prev_object->type == OBJT_SWAP)
2158ea41812fSAlan Cox 			swap_pager_freespace(prev_object,
2159ea41812fSAlan Cox 					     next_pindex, next_size);
21603364c323SKonstantin Belousov #if 0
2161ef694c1aSEdward Tomasz Napierala 		if (prev_object->cred != NULL) {
21623364c323SKonstantin Belousov 			KASSERT(prev_object->charge >=
21633364c323SKonstantin Belousov 			    ptoa(prev_object->size - next_pindex),
21643364c323SKonstantin Belousov 			    ("object %p overcharged 1 %jx %jx", prev_object,
21653364c323SKonstantin Belousov 				(uintmax_t)next_pindex, (uintmax_t)next_size));
21663364c323SKonstantin Belousov 			prev_object->charge -= ptoa(prev_object->size -
21673364c323SKonstantin Belousov 			    next_pindex);
21683364c323SKonstantin Belousov 		}
21693364c323SKonstantin Belousov #endif
2170ea41812fSAlan Cox 	}
2171df8bae1dSRodney W. Grimes 
2172df8bae1dSRodney W. Grimes 	/*
2173df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
2174df8bae1dSRodney W. Grimes 	 */
2175ea41812fSAlan Cox 	if (next_pindex + next_size > prev_object->size)
2176ea41812fSAlan Cox 		prev_object->size = next_pindex + next_size;
2177df8bae1dSRodney W. Grimes 
217889f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(prev_object);
2179df8bae1dSRodney W. Grimes 	return (TRUE);
2180df8bae1dSRodney W. Grimes }
2181df8bae1dSRodney W. Grimes 
21827a5a6352SMatthew Dillon void
21837a5a6352SMatthew Dillon vm_object_set_writeable_dirty(vm_object_t object)
21847a5a6352SMatthew Dillon {
21857a5a6352SMatthew Dillon 
218689f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
21873280870dSKonstantin Belousov 	if (object->type != OBJT_VNODE)
21883280870dSKonstantin Belousov 		return;
21893280870dSKonstantin Belousov 	object->generation++;
21903280870dSKonstantin Belousov 	if ((object->flags & OBJ_MIGHTBEDIRTY) != 0)
2191ee39666aSJeff Roberson 		return;
2192af51d7bfSAlan Cox 	vm_object_set_flag(object, OBJ_MIGHTBEDIRTY);
21937a5a6352SMatthew Dillon }
21947a5a6352SMatthew Dillon 
2195c7c34a24SBruce Evans #include "opt_ddb.h"
2196c3cb3e12SDavid Greenman #ifdef DDB
2197c7c34a24SBruce Evans #include <sys/kernel.h>
2198c7c34a24SBruce Evans 
2199ce9edcf5SPoul-Henning Kamp #include <sys/cons.h>
2200c7c34a24SBruce Evans 
2201c7c34a24SBruce Evans #include <ddb/ddb.h>
2202c7c34a24SBruce Evans 
2203cac597e4SBruce Evans static int
22041b40f8c0SMatthew Dillon _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
2205a1f6d91cSDavid Greenman {
2206a1f6d91cSDavid Greenman 	vm_map_t tmpm;
2207a1f6d91cSDavid Greenman 	vm_map_entry_t tmpe;
2208a1f6d91cSDavid Greenman 	vm_object_t obj;
2209a1f6d91cSDavid Greenman 	int entcount;
2210a1f6d91cSDavid Greenman 
2211a1f6d91cSDavid Greenman 	if (map == 0)
2212a1f6d91cSDavid Greenman 		return 0;
2213a1f6d91cSDavid Greenman 
2214a1f6d91cSDavid Greenman 	if (entry == 0) {
2215a1f6d91cSDavid Greenman 		tmpe = map->header.next;
2216a1f6d91cSDavid Greenman 		entcount = map->nentries;
2217a1f6d91cSDavid Greenman 		while (entcount-- && (tmpe != &map->header)) {
2218a1f6d91cSDavid Greenman 			if (_vm_object_in_map(map, object, tmpe)) {
2219a1f6d91cSDavid Greenman 				return 1;
2220a1f6d91cSDavid Greenman 			}
2221a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
2222a1f6d91cSDavid Greenman 		}
22239fdfe602SMatthew Dillon 	} else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
22249fdfe602SMatthew Dillon 		tmpm = entry->object.sub_map;
2225a1f6d91cSDavid Greenman 		tmpe = tmpm->header.next;
2226a1f6d91cSDavid Greenman 		entcount = tmpm->nentries;
2227a1f6d91cSDavid Greenman 		while (entcount-- && tmpe != &tmpm->header) {
2228a1f6d91cSDavid Greenman 			if (_vm_object_in_map(tmpm, object, tmpe)) {
2229a1f6d91cSDavid Greenman 				return 1;
2230a1f6d91cSDavid Greenman 			}
2231a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
2232a1f6d91cSDavid Greenman 		}
22338aef1712SMatthew Dillon 	} else if ((obj = entry->object.vm_object) != NULL) {
223424a1cce3SDavid Greenman 		for (; obj; obj = obj->backing_object)
2235a1f6d91cSDavid Greenman 			if (obj == object) {
2236a1f6d91cSDavid Greenman 				return 1;
2237a1f6d91cSDavid Greenman 			}
2238a1f6d91cSDavid Greenman 	}
2239a1f6d91cSDavid Greenman 	return 0;
2240a1f6d91cSDavid Greenman }
2241a1f6d91cSDavid Greenman 
2242cac597e4SBruce Evans static int
22431b40f8c0SMatthew Dillon vm_object_in_map(vm_object_t object)
2244a1f6d91cSDavid Greenman {
2245a1f6d91cSDavid Greenman 	struct proc *p;
22461005a129SJohn Baldwin 
224760517fd1SJohn Baldwin 	/* sx_slock(&allproc_lock); */
2248f67af5c9SXin LI 	FOREACH_PROC_IN_SYSTEM(p) {
2249a1f6d91cSDavid Greenman 		if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
2250a1f6d91cSDavid Greenman 			continue;
2251553629ebSJake Burkholder 		if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
225260517fd1SJohn Baldwin 			/* sx_sunlock(&allproc_lock); */
2253a1f6d91cSDavid Greenman 			return 1;
2254a1f6d91cSDavid Greenman 		}
2255553629ebSJake Burkholder 	}
225660517fd1SJohn Baldwin 	/* sx_sunlock(&allproc_lock); */
2257a1f6d91cSDavid Greenman 	if (_vm_object_in_map(kernel_map, object, 0))
2258a1f6d91cSDavid Greenman 		return 1;
2259a1f6d91cSDavid Greenman 	return 0;
2260a1f6d91cSDavid Greenman }
2261a1f6d91cSDavid Greenman 
2262c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check)
2263f708ef1bSPoul-Henning Kamp {
2264a1f6d91cSDavid Greenman 	vm_object_t object;
2265a1f6d91cSDavid Greenman 
2266a1f6d91cSDavid Greenman 	/*
2267a1f6d91cSDavid Greenman 	 * make sure that internal objs are in a map somewhere
2268a1f6d91cSDavid Greenman 	 * and none have zero ref counts.
2269a1f6d91cSDavid Greenman 	 */
2270cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
227124a1cce3SDavid Greenman 		if (object->handle == NULL &&
227224a1cce3SDavid Greenman 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2273a1f6d91cSDavid Greenman 			if (object->ref_count == 0) {
22743efc015bSPeter Wemm 				db_printf("vmochk: internal obj has zero ref count: %ld\n",
22753efc015bSPeter Wemm 					(long)object->size);
2276a1f6d91cSDavid Greenman 			}
2277a1f6d91cSDavid Greenman 			if (!vm_object_in_map(object)) {
2278fc62ef1fSBruce Evans 				db_printf(
2279fc62ef1fSBruce Evans 			"vmochk: internal obj is not in a map: "
2280fc62ef1fSBruce Evans 			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2281fc62ef1fSBruce Evans 				    object->ref_count, (u_long)object->size,
2282fc62ef1fSBruce Evans 				    (u_long)object->size,
2283fc62ef1fSBruce Evans 				    (void *)object->backing_object);
2284a1f6d91cSDavid Greenman 			}
2285a1f6d91cSDavid Greenman 		}
2286a1f6d91cSDavid Greenman 	}
2287a1f6d91cSDavid Greenman }
2288a1f6d91cSDavid Greenman 
228926f9a767SRodney W. Grimes /*
2290df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
2291df8bae1dSRodney W. Grimes  */
2292c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static)
2293df8bae1dSRodney W. Grimes {
2294c7c34a24SBruce Evans 	/* XXX convert args. */
2295c7c34a24SBruce Evans 	vm_object_t object = (vm_object_t)addr;
2296c7c34a24SBruce Evans 	boolean_t full = have_addr;
2297c7c34a24SBruce Evans 
2298d031cff1SMatthew Dillon 	vm_page_t p;
2299df8bae1dSRodney W. Grimes 
2300c7c34a24SBruce Evans 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
2301c7c34a24SBruce Evans #define	count	was_count
2302c7c34a24SBruce Evans 
2303d031cff1SMatthew Dillon 	int count;
2304df8bae1dSRodney W. Grimes 
2305df8bae1dSRodney W. Grimes 	if (object == NULL)
2306df8bae1dSRodney W. Grimes 		return;
2307df8bae1dSRodney W. Grimes 
2308eb95adefSBruce Evans 	db_iprintf(
2309ef694c1aSEdward Tomasz Napierala 	    "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x ruid %d charge %jx\n",
2310e47cd172SMaxime Henrion 	    object, (int)object->type, (uintmax_t)object->size,
23113364c323SKonstantin Belousov 	    object->resident_page_count, object->ref_count, object->flags,
2312ef694c1aSEdward Tomasz Napierala 	    object->cred ? object->cred->cr_ruid : -1, (uintmax_t)object->charge);
2313e47cd172SMaxime Henrion 	db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n",
23141c7c3c6aSMatthew Dillon 	    object->shadow_count,
2315eb95adefSBruce Evans 	    object->backing_object ? object->backing_object->ref_count : 0,
2316e47cd172SMaxime Henrion 	    object->backing_object, (uintmax_t)object->backing_object_offset);
2317df8bae1dSRodney W. Grimes 
2318df8bae1dSRodney W. Grimes 	if (!full)
2319df8bae1dSRodney W. Grimes 		return;
2320df8bae1dSRodney W. Grimes 
2321c7c34a24SBruce Evans 	db_indent += 2;
2322df8bae1dSRodney W. Grimes 	count = 0;
2323fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(p, &object->memq, listq) {
2324df8bae1dSRodney W. Grimes 		if (count == 0)
2325c7c34a24SBruce Evans 			db_iprintf("memory:=");
2326df8bae1dSRodney W. Grimes 		else if (count == 6) {
2327c7c34a24SBruce Evans 			db_printf("\n");
2328c7c34a24SBruce Evans 			db_iprintf(" ...");
2329df8bae1dSRodney W. Grimes 			count = 0;
2330df8bae1dSRodney W. Grimes 		} else
2331c7c34a24SBruce Evans 			db_printf(",");
2332df8bae1dSRodney W. Grimes 		count++;
2333df8bae1dSRodney W. Grimes 
2334e47cd172SMaxime Henrion 		db_printf("(off=0x%jx,page=0x%jx)",
2335e47cd172SMaxime Henrion 		    (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p));
2336df8bae1dSRodney W. Grimes 	}
2337df8bae1dSRodney W. Grimes 	if (count != 0)
2338c7c34a24SBruce Evans 		db_printf("\n");
2339c7c34a24SBruce Evans 	db_indent -= 2;
2340df8bae1dSRodney W. Grimes }
23415070c7f8SJohn Dyson 
2342c7c34a24SBruce Evans /* XXX. */
2343c7c34a24SBruce Evans #undef count
2344c7c34a24SBruce Evans 
2345c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */
23465070c7f8SJohn Dyson void
23471b40f8c0SMatthew Dillon vm_object_print(
23481b40f8c0SMatthew Dillon         /* db_expr_t */ long addr,
23491b40f8c0SMatthew Dillon 	boolean_t have_addr,
23501b40f8c0SMatthew Dillon 	/* db_expr_t */ long count,
23511b40f8c0SMatthew Dillon 	char *modif)
2352c7c34a24SBruce Evans {
2353c7c34a24SBruce Evans 	vm_object_print_static(addr, have_addr, count, modif);
2354c7c34a24SBruce Evans }
2355c7c34a24SBruce Evans 
2356c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
23575070c7f8SJohn Dyson {
23585070c7f8SJohn Dyson 	vm_object_t object;
2359bb2ac86fSKonstantin Belousov 	vm_pindex_t fidx;
2360bb2ac86fSKonstantin Belousov 	vm_paddr_t pa;
2361bb2ac86fSKonstantin Belousov 	vm_page_t m, prev_m;
2362bb2ac86fSKonstantin Belousov 	int rcount, nl, c;
2363cc64b484SAlfred Perlstein 
2364bb2ac86fSKonstantin Belousov 	nl = 0;
2365cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
2366fc62ef1fSBruce Evans 		db_printf("new object: %p\n", (void *)object);
23675070c7f8SJohn Dyson 		if (nl > 18) {
23685070c7f8SJohn Dyson 			c = cngetc();
23695070c7f8SJohn Dyson 			if (c != ' ')
23705070c7f8SJohn Dyson 				return;
23715070c7f8SJohn Dyson 			nl = 0;
23725070c7f8SJohn Dyson 		}
23735070c7f8SJohn Dyson 		nl++;
23745070c7f8SJohn Dyson 		rcount = 0;
23755070c7f8SJohn Dyson 		fidx = 0;
2376bb2ac86fSKonstantin Belousov 		pa = -1;
2377bb2ac86fSKonstantin Belousov 		TAILQ_FOREACH(m, &object->memq, listq) {
2378bb2ac86fSKonstantin Belousov 			if (m->pindex > 128)
2379bb2ac86fSKonstantin Belousov 				break;
2380bb2ac86fSKonstantin Belousov 			if ((prev_m = TAILQ_PREV(m, pglist, listq)) != NULL &&
2381bb2ac86fSKonstantin Belousov 			    prev_m->pindex + 1 != m->pindex) {
23825070c7f8SJohn Dyson 				if (rcount) {
23833efc015bSPeter Wemm 					db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
23843efc015bSPeter Wemm 						(long)fidx, rcount, (long)pa);
23855070c7f8SJohn Dyson 					if (nl > 18) {
23865070c7f8SJohn Dyson 						c = cngetc();
23875070c7f8SJohn Dyson 						if (c != ' ')
23885070c7f8SJohn Dyson 							return;
23895070c7f8SJohn Dyson 						nl = 0;
23905070c7f8SJohn Dyson 					}
23915070c7f8SJohn Dyson 					nl++;
23925070c7f8SJohn Dyson 					rcount = 0;
23935070c7f8SJohn Dyson 				}
23945070c7f8SJohn Dyson 			}
23955070c7f8SJohn Dyson 			if (rcount &&
23965070c7f8SJohn Dyson 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
23975070c7f8SJohn Dyson 				++rcount;
23985070c7f8SJohn Dyson 				continue;
23995070c7f8SJohn Dyson 			}
24005070c7f8SJohn Dyson 			if (rcount) {
24012446e4f0SAlan Cox 				db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
24023efc015bSPeter Wemm 					(long)fidx, rcount, (long)pa);
24035070c7f8SJohn Dyson 				if (nl > 18) {
24045070c7f8SJohn Dyson 					c = cngetc();
24055070c7f8SJohn Dyson 					if (c != ' ')
24065070c7f8SJohn Dyson 						return;
24075070c7f8SJohn Dyson 					nl = 0;
24085070c7f8SJohn Dyson 				}
24095070c7f8SJohn Dyson 				nl++;
24105070c7f8SJohn Dyson 			}
2411bb2ac86fSKonstantin Belousov 			fidx = m->pindex;
24125070c7f8SJohn Dyson 			pa = VM_PAGE_TO_PHYS(m);
24135070c7f8SJohn Dyson 			rcount = 1;
24145070c7f8SJohn Dyson 		}
24155070c7f8SJohn Dyson 		if (rcount) {
24163efc015bSPeter Wemm 			db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
24173efc015bSPeter Wemm 				(long)fidx, rcount, (long)pa);
24185070c7f8SJohn Dyson 			if (nl > 18) {
24195070c7f8SJohn Dyson 				c = cngetc();
24205070c7f8SJohn Dyson 				if (c != ' ')
24215070c7f8SJohn Dyson 					return;
24225070c7f8SJohn Dyson 				nl = 0;
24235070c7f8SJohn Dyson 			}
24245070c7f8SJohn Dyson 			nl++;
24255070c7f8SJohn Dyson 		}
24265070c7f8SJohn Dyson 	}
24275070c7f8SJohn Dyson }
2428c3cb3e12SDavid Greenman #endif /* DDB */
2429