xref: /freebsd/sys/vm/vm_object.c (revision 2446e4f02c066904b8d68afcbdfecf4c83b79e9f)
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 
68df8bae1dSRodney W. Grimes #include <sys/param.h>
69df8bae1dSRodney W. Grimes #include <sys/systm.h>
70fb919e4dSMark Murray #include <sys/lock.h>
71867a482dSJohn Dyson #include <sys/mman.h>
72cf2819ccSJohn Dyson #include <sys/mount.h>
73b9b7a4beSMatthew Dillon #include <sys/kernel.h>
74b9b7a4beSMatthew Dillon #include <sys/sysctl.h>
751b367556SJason Evans #include <sys/mutex.h>
76fb919e4dSMark Murray #include <sys/proc.h>		/* for curproc, pageproc */
77fb919e4dSMark Murray #include <sys/socket.h>
78fb919e4dSMark Murray #include <sys/vnode.h>
79fb919e4dSMark Murray #include <sys/vmmeter.h>
801005a129SJohn Baldwin #include <sys/sx.h>
81df8bae1dSRodney W. Grimes 
82df8bae1dSRodney W. Grimes #include <vm/vm.h>
83efeaf95aSDavid Greenman #include <vm/vm_param.h>
84efeaf95aSDavid Greenman #include <vm/pmap.h>
85efeaf95aSDavid Greenman #include <vm/vm_map.h>
86efeaf95aSDavid Greenman #include <vm/vm_object.h>
87df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
8826f9a767SRodney W. Grimes #include <vm/vm_pageout.h>
890d94caffSDavid Greenman #include <vm/vm_pager.h>
9005f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h>
91a1f6d91cSDavid Greenman #include <vm/vm_kern.h>
92efeaf95aSDavid Greenman #include <vm/vm_extern.h>
93670d17b5SJeff Roberson #include <vm/uma.h>
9426f9a767SRodney W. Grimes 
95b9b7a4beSMatthew Dillon #define EASY_SCAN_FACTOR       8
96b9b7a4beSMatthew Dillon 
97b9b7a4beSMatthew Dillon #define MSYNC_FLUSH_HARDSEQ	0x01
98b9b7a4beSMatthew Dillon #define MSYNC_FLUSH_SOFTSEQ	0x02
99b9b7a4beSMatthew Dillon 
100b9b7a4beSMatthew Dillon /*
101b9b7a4beSMatthew Dillon  * msync / VM object flushing optimizations
102b9b7a4beSMatthew Dillon  */
103b9b7a4beSMatthew Dillon static int msync_flush_flags = MSYNC_FLUSH_HARDSEQ | MSYNC_FLUSH_SOFTSEQ;
104b9b7a4beSMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, msync_flush_flags,
105b9b7a4beSMatthew Dillon         CTLFLAG_RW, &msync_flush_flags, 0, "");
106b9b7a4beSMatthew Dillon 
107c53f7aceSDag-Erling Smørgrav static int old_msync;
108c53f7aceSDag-Erling Smørgrav SYSCTL_INT(_vm, OID_AUTO, old_msync, CTLFLAG_RW, &old_msync, 0,
109c53f7aceSDag-Erling Smørgrav     "Use old (insecure) msync behavior");
110c53f7aceSDag-Erling Smørgrav 
111b9b7a4beSMatthew Dillon static void	vm_object_qcollapse(vm_object_t object);
112b9b7a4beSMatthew Dillon static int	vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags);
11302dd8331SAlan Cox static void	vm_object_vndeallocate(vm_object_t object);
114f6b04d2bSDavid Greenman 
115df8bae1dSRodney W. Grimes /*
116df8bae1dSRodney W. Grimes  *	Virtual memory objects maintain the actual data
117df8bae1dSRodney W. Grimes  *	associated with allocated virtual memory.  A given
118df8bae1dSRodney W. Grimes  *	page of memory exists within exactly one object.
119df8bae1dSRodney W. Grimes  *
120df8bae1dSRodney W. Grimes  *	An object is only deallocated when all "references"
121df8bae1dSRodney W. Grimes  *	are given up.  Only one "reference" to a given
122df8bae1dSRodney W. Grimes  *	region of an object should be writeable.
123df8bae1dSRodney W. Grimes  *
124df8bae1dSRodney W. Grimes  *	Associated with each object is a list of all resident
125df8bae1dSRodney W. Grimes  *	memory pages belonging to that object; this list is
126df8bae1dSRodney W. Grimes  *	maintained by the "vm_page" module, and locked by the object's
127df8bae1dSRodney W. Grimes  *	lock.
128df8bae1dSRodney W. Grimes  *
129df8bae1dSRodney W. Grimes  *	Each object also records a "pager" routine which is
130df8bae1dSRodney W. Grimes  *	used to retrieve (and store) pages to the proper backing
131df8bae1dSRodney W. Grimes  *	storage.  In addition, objects may be backed by other
132df8bae1dSRodney W. Grimes  *	objects from which they were virtual-copied.
133df8bae1dSRodney W. Grimes  *
134df8bae1dSRodney W. Grimes  *	The only items within the object structure which are
135df8bae1dSRodney W. Grimes  *	modified after time of creation are:
136df8bae1dSRodney W. Grimes  *		reference count		locked by object's lock
137df8bae1dSRodney W. Grimes  *		pager routine		locked by object's lock
138df8bae1dSRodney W. Grimes  *
139df8bae1dSRodney W. Grimes  */
140df8bae1dSRodney W. Grimes 
14128f8db14SBruce Evans struct object_q vm_object_list;
142a5698387SAlan Cox struct mtx vm_object_list_mtx;	/* lock for object list and count */
143cccf11b8SAlan Cox 
144cccf11b8SAlan Cox struct vm_object kernel_object_store;
145cccf11b8SAlan Cox struct vm_object kmem_object_store;
146df8bae1dSRodney W. Grimes 
147604c2bbcSAlan Cox SYSCTL_NODE(_vm_stats, OID_AUTO, object, CTLFLAG_RD, 0, "VM object stats");
148604c2bbcSAlan Cox 
149f708ef1bSPoul-Henning Kamp static long object_collapses;
150604c2bbcSAlan Cox SYSCTL_LONG(_vm_stats_object, OID_AUTO, collapses, CTLFLAG_RD,
151604c2bbcSAlan Cox     &object_collapses, 0, "VM object collapses");
152604c2bbcSAlan Cox 
153f708ef1bSPoul-Henning Kamp static long object_bypasses;
154604c2bbcSAlan Cox SYSCTL_LONG(_vm_stats_object, OID_AUTO, bypasses, CTLFLAG_RD,
155604c2bbcSAlan Cox     &object_bypasses, 0, "VM object bypasses");
156dad740e9SAlan Cox 
157670d17b5SJeff Roberson static uma_zone_t obj_zone;
1588355f576SJeff Roberson 
159b23f72e9SBrian Feldman static int vm_object_zinit(void *mem, int size, int flags);
1608355f576SJeff Roberson 
1618355f576SJeff Roberson #ifdef INVARIANTS
1628355f576SJeff Roberson static void vm_object_zdtor(void *mem, int size, void *arg);
1638355f576SJeff Roberson 
1648355f576SJeff Roberson static void
1658355f576SJeff Roberson vm_object_zdtor(void *mem, int size, void *arg)
1668355f576SJeff Roberson {
1678355f576SJeff Roberson 	vm_object_t object;
1688355f576SJeff Roberson 
1698355f576SJeff Roberson 	object = (vm_object_t)mem;
17043186e53SAlan Cox 	KASSERT(TAILQ_EMPTY(&object->memq),
17143186e53SAlan Cox 	    ("object %p has resident pages",
17243186e53SAlan Cox 	    object));
1738355f576SJeff Roberson 	KASSERT(object->paging_in_progress == 0,
1748355f576SJeff Roberson 	    ("object %p paging_in_progress = %d",
1758355f576SJeff Roberson 	    object, object->paging_in_progress));
1768355f576SJeff Roberson 	KASSERT(object->resident_page_count == 0,
1778355f576SJeff Roberson 	    ("object %p resident_page_count = %d",
1788355f576SJeff Roberson 	    object, object->resident_page_count));
1798355f576SJeff Roberson 	KASSERT(object->shadow_count == 0,
1808355f576SJeff Roberson 	    ("object %p shadow_count = %d",
1818355f576SJeff Roberson 	    object, object->shadow_count));
1828355f576SJeff Roberson }
1838355f576SJeff Roberson #endif
1848355f576SJeff Roberson 
185b23f72e9SBrian Feldman static int
186b23f72e9SBrian Feldman vm_object_zinit(void *mem, int size, int flags)
1878355f576SJeff Roberson {
1888355f576SJeff Roberson 	vm_object_t object;
1898355f576SJeff Roberson 
1908355f576SJeff Roberson 	object = (vm_object_t)mem;
191e3a9e1b2SAlan Cox 	bzero(&object->mtx, sizeof(object->mtx));
1925285558aSAlan Cox 	VM_OBJECT_LOCK_INIT(object, "standard object");
1938355f576SJeff Roberson 
1948355f576SJeff Roberson 	/* These are true for any object that has been freed */
1958355f576SJeff Roberson 	object->paging_in_progress = 0;
1968355f576SJeff Roberson 	object->resident_page_count = 0;
1978355f576SJeff Roberson 	object->shadow_count = 0;
198b23f72e9SBrian Feldman 	return (0);
1998355f576SJeff Roberson }
200df8bae1dSRodney W. Grimes 
2013075778bSJohn Dyson void
2026395da54SIan Dowse _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
203df8bae1dSRodney W. Grimes {
2040cddd8f0SMatthew Dillon 
205df8bae1dSRodney W. Grimes 	TAILQ_INIT(&object->memq);
2061c500307SAlan Cox 	LIST_INIT(&object->shadow_head);
207a1f6d91cSDavid Greenman 
208b86ec922SMatthew Dillon 	object->root = NULL;
20924a1cce3SDavid Greenman 	object->type = type;
210df8bae1dSRodney W. Grimes 	object->size = size;
211b881da26SAlan Cox 	object->generation = 1;
212a1f6d91cSDavid Greenman 	object->ref_count = 1;
21324a1cce3SDavid Greenman 	object->flags = 0;
21460517fd1SJohn Baldwin 	if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
2153471677cSAlan Cox 		object->flags = OBJ_ONEMAPPING;
2162446e4f0SAlan Cox 	object->pg_color = 0;
21724a1cce3SDavid Greenman 	object->handle = NULL;
21824a1cce3SDavid Greenman 	object->backing_object = NULL;
219a316d390SJohn Dyson 	object->backing_object_offset = (vm_ooffset_t) 0;
220a1f6d91cSDavid Greenman 
221a5698387SAlan Cox 	mtx_lock(&vm_object_list_mtx);
22260517fd1SJohn Baldwin 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
223a5698387SAlan Cox 	mtx_unlock(&vm_object_list_mtx);
224df8bae1dSRodney W. Grimes }
225df8bae1dSRodney W. Grimes 
226df8bae1dSRodney W. Grimes /*
22726f9a767SRodney W. Grimes  *	vm_object_init:
22826f9a767SRodney W. Grimes  *
22926f9a767SRodney W. Grimes  *	Initialize the VM objects module.
23026f9a767SRodney W. Grimes  */
23126f9a767SRodney W. Grimes void
2321b40f8c0SMatthew Dillon vm_object_init(void)
23326f9a767SRodney W. Grimes {
23426f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_list);
2356008862bSJohn Baldwin 	mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF);
2360217125fSDavid Greenman 
2375285558aSAlan Cox 	VM_OBJECT_LOCK_INIT(&kernel_object_store, "kernel object");
2389f5c801bSAlan Cox 	_vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
23926f9a767SRodney W. Grimes 	    kernel_object);
24026f9a767SRodney W. Grimes 
2415285558aSAlan Cox 	VM_OBJECT_LOCK_INIT(&kmem_object_store, "kmem object");
2429f5c801bSAlan Cox 	_vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
24326f9a767SRodney W. Grimes 	    kmem_object);
244ed6a7863SAlan Cox 
2458dbca793STor Egge 	/*
2468dbca793STor Egge 	 * The lock portion of struct vm_object must be type stable due
2478dbca793STor Egge 	 * to vm_pageout_fallback_object_lock locking a vm object
2488dbca793STor Egge 	 * without holding any references to it.
2498dbca793STor Egge 	 */
2508355f576SJeff Roberson 	obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL,
2518355f576SJeff Roberson #ifdef INVARIANTS
2528355f576SJeff Roberson 	    vm_object_zdtor,
2538355f576SJeff Roberson #else
2548355f576SJeff Roberson 	    NULL,
2558355f576SJeff Roberson #endif
256f3c625e4SJeff Roberson 	    vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM|UMA_ZONE_NOFREE);
25799448ed1SJohn Dyson }
25899448ed1SJohn Dyson 
25999448ed1SJohn Dyson void
2601b40f8c0SMatthew Dillon vm_object_clear_flag(vm_object_t object, u_short bits)
2611b40f8c0SMatthew Dillon {
2625440b5a9SAlan Cox 
263d647a0edSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
264b06805adSJake Burkholder 	object->flags &= ~bits;
2651b40f8c0SMatthew Dillon }
2661b40f8c0SMatthew Dillon 
2671b40f8c0SMatthew Dillon void
2681b40f8c0SMatthew Dillon vm_object_pip_add(vm_object_t object, short i)
2691b40f8c0SMatthew Dillon {
270f279b88dSAlan Cox 
271d647a0edSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
272b06805adSJake Burkholder 	object->paging_in_progress += i;
2731b40f8c0SMatthew Dillon }
2741b40f8c0SMatthew Dillon 
2751b40f8c0SMatthew Dillon void
2761b40f8c0SMatthew Dillon vm_object_pip_subtract(vm_object_t object, short i)
2771b40f8c0SMatthew Dillon {
278d647a0edSAlan Cox 
2790fa05eaeSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
280b06805adSJake Burkholder 	object->paging_in_progress -= i;
2811b40f8c0SMatthew Dillon }
2821b40f8c0SMatthew Dillon 
2831b40f8c0SMatthew Dillon void
2841b40f8c0SMatthew Dillon vm_object_pip_wakeup(vm_object_t object)
2851b40f8c0SMatthew Dillon {
286f279b88dSAlan Cox 
287d647a0edSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
288b06805adSJake Burkholder 	object->paging_in_progress--;
2891b40f8c0SMatthew Dillon 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
2901b40f8c0SMatthew Dillon 		vm_object_clear_flag(object, OBJ_PIPWNT);
2911b40f8c0SMatthew Dillon 		wakeup(object);
2921b40f8c0SMatthew Dillon 	}
2931b40f8c0SMatthew Dillon }
2941b40f8c0SMatthew Dillon 
2951b40f8c0SMatthew Dillon void
2961b40f8c0SMatthew Dillon vm_object_pip_wakeupn(vm_object_t object, short i)
2971b40f8c0SMatthew Dillon {
298d647a0edSAlan Cox 
2990d420ad3SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
3001b40f8c0SMatthew Dillon 	if (i)
301b06805adSJake Burkholder 		object->paging_in_progress -= i;
3021b40f8c0SMatthew Dillon 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
3031b40f8c0SMatthew Dillon 		vm_object_clear_flag(object, OBJ_PIPWNT);
3041b40f8c0SMatthew Dillon 		wakeup(object);
3051b40f8c0SMatthew Dillon 	}
3061b40f8c0SMatthew Dillon }
3071b40f8c0SMatthew Dillon 
3081b40f8c0SMatthew Dillon void
3091b40f8c0SMatthew Dillon vm_object_pip_wait(vm_object_t object, char *waitid)
3101b40f8c0SMatthew Dillon {
3111ca58953SAlan Cox 
3121ca58953SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
3131ca58953SAlan Cox 	while (object->paging_in_progress) {
3141ca58953SAlan Cox 		object->flags |= OBJ_PIPWNT;
3151ca58953SAlan Cox 		msleep(object, VM_OBJECT_MTX(object), PVM, waitid, 0);
3161ca58953SAlan Cox 	}
3171b40f8c0SMatthew Dillon }
3181b40f8c0SMatthew Dillon 
31926f9a767SRodney W. Grimes /*
32026f9a767SRodney W. Grimes  *	vm_object_allocate:
32126f9a767SRodney W. Grimes  *
32226f9a767SRodney W. Grimes  *	Returns a new object with the given size.
32326f9a767SRodney W. Grimes  */
32426f9a767SRodney W. Grimes vm_object_t
3256395da54SIan Dowse vm_object_allocate(objtype_t type, vm_pindex_t size)
32626f9a767SRodney W. Grimes {
32790688d13SAlan Cox 	vm_object_t object;
32890688d13SAlan Cox 
32990688d13SAlan Cox 	object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK);
33090688d13SAlan Cox 	_vm_object_allocate(type, size, object);
33190688d13SAlan Cox 	return (object);
33226f9a767SRodney W. Grimes }
33326f9a767SRodney W. Grimes 
33426f9a767SRodney W. Grimes 
33526f9a767SRodney W. Grimes /*
336df8bae1dSRodney W. Grimes  *	vm_object_reference:
337df8bae1dSRodney W. Grimes  *
33815347817SAlan Cox  *	Gets another reference to the given object.  Note: OBJ_DEAD
33915347817SAlan Cox  *	objects can be referenced during final cleaning.
340df8bae1dSRodney W. Grimes  */
3416476c0d2SJohn Dyson void
3421b40f8c0SMatthew Dillon vm_object_reference(vm_object_t object)
343df8bae1dSRodney W. Grimes {
34482f9defeSAlan Cox 	struct vnode *vp;
34582f9defeSAlan Cox 
346df8bae1dSRodney W. Grimes 	if (object == NULL)
347df8bae1dSRodney W. Grimes 		return;
34815347817SAlan Cox 	VM_OBJECT_LOCK(object);
349df8bae1dSRodney W. Grimes 	object->ref_count++;
35047221757SJohn Dyson 	if (object->type == OBJT_VNODE) {
351c05e22d4SJeff Roberson 		int vfslocked;
352c05e22d4SJeff Roberson 
35382f9defeSAlan Cox 		vp = object->handle;
35482f9defeSAlan Cox 		VM_OBJECT_UNLOCK(object);
355c05e22d4SJeff Roberson 		vfslocked = VFS_LOCK_GIANT(vp->v_mount);
356c05e22d4SJeff Roberson 		vget(vp, LK_RETRY, curthread);
357c05e22d4SJeff Roberson 		VFS_UNLOCK_GIANT(vfslocked);
35882f9defeSAlan Cox 	} else
35982f9defeSAlan Cox 		VM_OBJECT_UNLOCK(object);
36095e5e988SJohn Dyson }
36195e5e988SJohn Dyson 
36223955314SAlfred Perlstein /*
363b921a12bSAlan Cox  *	vm_object_reference_locked:
364b921a12bSAlan Cox  *
365b921a12bSAlan Cox  *	Gets another reference to the given object.
366b921a12bSAlan Cox  *
367b921a12bSAlan Cox  *	The object must be locked.
368b921a12bSAlan Cox  */
369b921a12bSAlan Cox void
370b921a12bSAlan Cox vm_object_reference_locked(vm_object_t object)
371b921a12bSAlan Cox {
372b921a12bSAlan Cox 	struct vnode *vp;
373b921a12bSAlan Cox 
374b921a12bSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
375b921a12bSAlan Cox 	KASSERT((object->flags & OBJ_DEAD) == 0,
376b921a12bSAlan Cox 	    ("vm_object_reference_locked: dead object referenced"));
377b921a12bSAlan Cox 	object->ref_count++;
378b921a12bSAlan Cox 	if (object->type == OBJT_VNODE) {
379b921a12bSAlan Cox 		vp = object->handle;
380b921a12bSAlan Cox 		vref(vp);
381b921a12bSAlan Cox 	}
382b921a12bSAlan Cox }
383b921a12bSAlan Cox 
384b921a12bSAlan Cox /*
3859d5abbddSJens Schweikhardt  * Handle deallocating an object of type OBJT_VNODE.
38623955314SAlfred Perlstein  */
38702dd8331SAlan Cox static void
3881b40f8c0SMatthew Dillon vm_object_vndeallocate(vm_object_t object)
38995e5e988SJohn Dyson {
39095e5e988SJohn Dyson 	struct vnode *vp = (struct vnode *) object->handle;
391219cbf59SEivind Eklund 
392ae51ff11SJeff Roberson 	VFS_ASSERT_GIANT(vp->v_mount);
393ad682c48SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
3945526d2d9SEivind Eklund 	KASSERT(object->type == OBJT_VNODE,
3955526d2d9SEivind Eklund 	    ("vm_object_vndeallocate: not a vnode object"));
396219cbf59SEivind Eklund 	KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
397219cbf59SEivind Eklund #ifdef INVARIANTS
39895e5e988SJohn Dyson 	if (object->ref_count == 0) {
39995e5e988SJohn Dyson 		vprint("vm_object_vndeallocate", vp);
40095e5e988SJohn Dyson 		panic("vm_object_vndeallocate: bad object reference count");
40195e5e988SJohn Dyson 	}
40295e5e988SJohn Dyson #endif
40395e5e988SJohn Dyson 
40495e5e988SJohn Dyson 	object->ref_count--;
40547221757SJohn Dyson 	if (object->ref_count == 0) {
406e6e370a7SJeff Roberson 		mp_fixme("Unlocked vflag access.");
407e6e370a7SJeff Roberson 		vp->v_vflag &= ~VV_TEXT;
4082be70f79SJohn Dyson 	}
409ad682c48SAlan Cox 	VM_OBJECT_UNLOCK(object);
41023955314SAlfred Perlstein 	/*
41123955314SAlfred Perlstein 	 * vrele may need a vop lock
41223955314SAlfred Perlstein 	 */
41347221757SJohn Dyson 	vrele(vp);
414df8bae1dSRodney W. Grimes }
415df8bae1dSRodney W. Grimes 
416df8bae1dSRodney W. Grimes /*
417df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
418df8bae1dSRodney W. Grimes  *
419df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
420df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
421df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
422df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
423df8bae1dSRodney W. Grimes  *	may be relinquished.
424df8bae1dSRodney W. Grimes  *
425df8bae1dSRodney W. Grimes  *	No object may be locked.
426df8bae1dSRodney W. Grimes  */
42726f9a767SRodney W. Grimes void
4281b40f8c0SMatthew Dillon vm_object_deallocate(vm_object_t object)
429df8bae1dSRodney W. Grimes {
430df8bae1dSRodney W. Grimes 	vm_object_t temp;
431df8bae1dSRodney W. Grimes 
432df8bae1dSRodney W. Grimes 	while (object != NULL) {
433ae51ff11SJeff Roberson 		int vfslocked;
434ca95b514SJohn Baldwin 
435ae51ff11SJeff Roberson 		vfslocked = 0;
436ca95b514SJohn Baldwin 	restart:
437ad682c48SAlan Cox 		VM_OBJECT_LOCK(object);
4383b68228cSAlan Cox 		if (object->type == OBJT_VNODE) {
439ca95b514SJohn Baldwin 			struct vnode *vp = (struct vnode *) object->handle;
440ca95b514SJohn Baldwin 
441ca95b514SJohn Baldwin 			/*
442ca95b514SJohn Baldwin 			 * Conditionally acquire Giant for a vnode-backed
443ca95b514SJohn Baldwin 			 * object.  We have to be careful since the type of
444ca95b514SJohn Baldwin 			 * a vnode object can change while the object is
445ca95b514SJohn Baldwin 			 * unlocked.
446ca95b514SJohn Baldwin 			 */
447ca95b514SJohn Baldwin 			if (VFS_NEEDSGIANT(vp->v_mount) && !vfslocked) {
448ca95b514SJohn Baldwin 				vfslocked = 1;
449ca95b514SJohn Baldwin 				if (!mtx_trylock(&Giant)) {
450ca95b514SJohn Baldwin 					VM_OBJECT_UNLOCK(object);
451ca95b514SJohn Baldwin 					mtx_lock(&Giant);
452ca95b514SJohn Baldwin 					goto restart;
453ca95b514SJohn Baldwin 				}
454ca95b514SJohn Baldwin 			}
45595e5e988SJohn Dyson 			vm_object_vndeallocate(object);
456ae51ff11SJeff Roberson 			VFS_UNLOCK_GIANT(vfslocked);
45723b186d3SAlan Cox 			return;
458ca95b514SJohn Baldwin 		} else
459ca95b514SJohn Baldwin 			/*
460ca95b514SJohn Baldwin 			 * This is to handle the case that the object
461ca95b514SJohn Baldwin 			 * changed type while we dropped its lock to
462ca95b514SJohn Baldwin 			 * obtain Giant.
463ca95b514SJohn Baldwin 			 */
464ca95b514SJohn Baldwin 			VFS_UNLOCK_GIANT(vfslocked);
46595e5e988SJohn Dyson 
4668125b1e6SAlfred Perlstein 		KASSERT(object->ref_count != 0,
4678125b1e6SAlfred Perlstein 			("vm_object_deallocate: object deallocated too many times: %d", object->type));
4682be70f79SJohn Dyson 
4692be70f79SJohn Dyson 		/*
4708125b1e6SAlfred Perlstein 		 * If the reference count goes to 0 we start calling
4718125b1e6SAlfred Perlstein 		 * vm_object_terminate() on the object chain.
4728125b1e6SAlfred Perlstein 		 * A ref count of 1 may be a special case depending on the
4738125b1e6SAlfred Perlstein 		 * shadow count being 0 or 1.
4742be70f79SJohn Dyson 		 */
475c0877f10SJohn Dyson 		object->ref_count--;
4768125b1e6SAlfred Perlstein 		if (object->ref_count > 1) {
4773b68228cSAlan Cox 			VM_OBJECT_UNLOCK(object);
47823b186d3SAlan Cox 			return;
4798125b1e6SAlfred Perlstein 		} else if (object->ref_count == 1) {
4808125b1e6SAlfred Perlstein 			if (object->shadow_count == 0) {
4818125b1e6SAlfred Perlstein 				vm_object_set_flag(object, OBJ_ONEMAPPING);
4828125b1e6SAlfred Perlstein 			} else if ((object->shadow_count == 1) &&
4838125b1e6SAlfred Perlstein 			    (object->handle == NULL) &&
48424a1cce3SDavid Greenman 			    (object->type == OBJT_DEFAULT ||
48524a1cce3SDavid Greenman 			     object->type == OBJT_SWAP)) {
486a1f6d91cSDavid Greenman 				vm_object_t robject;
48795e5e988SJohn Dyson 
4881c500307SAlan Cox 				robject = LIST_FIRST(&object->shadow_head);
4895526d2d9SEivind Eklund 				KASSERT(robject != NULL,
490219cbf59SEivind Eklund 				    ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
4915526d2d9SEivind Eklund 					 object->ref_count,
4925526d2d9SEivind Eklund 					 object->shadow_count));
493b72b0115SAlan Cox 				if (!VM_OBJECT_TRYLOCK(robject)) {
494b72b0115SAlan Cox 					/*
495b72b0115SAlan Cox 					 * Avoid a potential deadlock.
496b72b0115SAlan Cox 					 */
497b72b0115SAlan Cox 					object->ref_count++;
498b72b0115SAlan Cox 					VM_OBJECT_UNLOCK(object);
499a7d86121SAlan Cox 					/*
500a7d86121SAlan Cox 					 * More likely than not the thread
501a7d86121SAlan Cox 					 * holding robject's lock has lower
502a7d86121SAlan Cox 					 * priority than the current thread.
503a7d86121SAlan Cox 					 * Let the lower priority thread run.
504a7d86121SAlan Cox 					 */
5058db5fc58SJohn Baldwin 					pause("vmo_de", 1);
506b72b0115SAlan Cox 					continue;
507b72b0115SAlan Cox 				}
508d936694fSAlan Cox 				/*
509d936694fSAlan Cox 				 * Collapse object into its shadow unless its
510d936694fSAlan Cox 				 * shadow is dead.  In that case, object will
511d936694fSAlan Cox 				 * be deallocated by the thread that is
512d936694fSAlan Cox 				 * deallocating its shadow.
513d936694fSAlan Cox 				 */
514d936694fSAlan Cox 				if ((robject->flags & OBJ_DEAD) == 0 &&
515d936694fSAlan Cox 				    (robject->handle == NULL) &&
51624a1cce3SDavid Greenman 				    (robject->type == OBJT_DEFAULT ||
51724a1cce3SDavid Greenman 				     robject->type == OBJT_SWAP)) {
518a1f6d91cSDavid Greenman 
51995e5e988SJohn Dyson 					robject->ref_count++;
520138449dcSAlan Cox retry:
521138449dcSAlan Cox 					if (robject->paging_in_progress) {
522138449dcSAlan Cox 						VM_OBJECT_UNLOCK(object);
523138449dcSAlan Cox 						vm_object_pip_wait(robject,
524138449dcSAlan Cox 						    "objde1");
5252e9f4a69SAlan Cox 						temp = robject->backing_object;
5262e9f4a69SAlan Cox 						if (object == temp) {
527138449dcSAlan Cox 							VM_OBJECT_LOCK(object);
528138449dcSAlan Cox 							goto retry;
5292e9f4a69SAlan Cox 						}
530138449dcSAlan Cox 					} else if (object->paging_in_progress) {
531138449dcSAlan Cox 						VM_OBJECT_UNLOCK(robject);
532138449dcSAlan Cox 						object->flags |= OBJ_PIPWNT;
533138449dcSAlan Cox 						msleep(object,
534138449dcSAlan Cox 						    VM_OBJECT_MTX(object),
535138449dcSAlan Cox 						    PDROP | PVM, "objde2", 0);
536138449dcSAlan Cox 						VM_OBJECT_LOCK(robject);
5372e9f4a69SAlan Cox 						temp = robject->backing_object;
5382e9f4a69SAlan Cox 						if (object == temp) {
539138449dcSAlan Cox 							VM_OBJECT_LOCK(object);
540138449dcSAlan Cox 							goto retry;
541a1f6d91cSDavid Greenman 						}
5422e9f4a69SAlan Cox 					} else
5433b68228cSAlan Cox 						VM_OBJECT_UNLOCK(object);
5442e9f4a69SAlan Cox 
54595e5e988SJohn Dyson 					if (robject->ref_count == 1) {
54695e5e988SJohn Dyson 						robject->ref_count--;
547ba8da839SDavid Greenman 						object = robject;
54895e5e988SJohn Dyson 						goto doterm;
54995e5e988SJohn Dyson 					}
55095e5e988SJohn Dyson 					object = robject;
55195e5e988SJohn Dyson 					vm_object_collapse(object);
552d7fc2210SAlan Cox 					VM_OBJECT_UNLOCK(object);
553ba8da839SDavid Greenman 					continue;
554a1f6d91cSDavid Greenman 				}
555b72b0115SAlan Cox 				VM_OBJECT_UNLOCK(robject);
55695e5e988SJohn Dyson 			}
5573b68228cSAlan Cox 			VM_OBJECT_UNLOCK(object);
55823b186d3SAlan Cox 			return;
55995e5e988SJohn Dyson 		}
56095e5e988SJohn Dyson doterm:
56124a1cce3SDavid Greenman 		temp = object->backing_object;
562c9917419SAlan Cox 		if (temp != NULL) {
563c9917419SAlan Cox 			VM_OBJECT_LOCK(temp);
5641c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
56595e5e988SJohn Dyson 			temp->shadow_count--;
566eaf13dd7SJohn Dyson 			temp->generation++;
567c9917419SAlan Cox 			VM_OBJECT_UNLOCK(temp);
56895461b45SJohn Dyson 			object->backing_object = NULL;
569de5f6a77SJohn Dyson 		}
570245df27cSMatthew Dillon 		/*
571245df27cSMatthew Dillon 		 * Don't double-terminate, we could be in a termination
572245df27cSMatthew Dillon 		 * recursion due to the terminate having to sync data
573245df27cSMatthew Dillon 		 * to disk.
574245df27cSMatthew Dillon 		 */
575245df27cSMatthew Dillon 		if ((object->flags & OBJ_DEAD) == 0)
576df8bae1dSRodney W. Grimes 			vm_object_terminate(object);
577c829b9d0SAlan Cox 		else
578c829b9d0SAlan Cox 			VM_OBJECT_UNLOCK(object);
579df8bae1dSRodney W. Grimes 		object = temp;
580df8bae1dSRodney W. Grimes 	}
581df8bae1dSRodney W. Grimes }
582df8bae1dSRodney W. Grimes 
583df8bae1dSRodney W. Grimes /*
584df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
585df8bae1dSRodney W. Grimes  *	up all previously used resources.
586df8bae1dSRodney W. Grimes  *
587df8bae1dSRodney W. Grimes  *	The object must be locked.
5881c7c3c6aSMatthew Dillon  *	This routine may block.
589df8bae1dSRodney W. Grimes  */
59095e5e988SJohn Dyson void
5911b40f8c0SMatthew Dillon vm_object_terminate(vm_object_t object)
592df8bae1dSRodney W. Grimes {
593d031cff1SMatthew Dillon 	vm_page_t p;
594df8bae1dSRodney W. Grimes 
595c829b9d0SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
5960cddd8f0SMatthew Dillon 
59795e5e988SJohn Dyson 	/*
59895e5e988SJohn Dyson 	 * Make sure no one uses us.
59995e5e988SJohn Dyson 	 */
600069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_DEAD);
6013c631446SJohn Dyson 
602df8bae1dSRodney W. Grimes 	/*
603f6b04d2bSDavid Greenman 	 * wait for the pageout daemon to be done with the object
604df8bae1dSRodney W. Grimes 	 */
60566095752SJohn Dyson 	vm_object_pip_wait(object, "objtrm");
606df8bae1dSRodney W. Grimes 
6075526d2d9SEivind Eklund 	KASSERT(!object->paging_in_progress,
6085526d2d9SEivind Eklund 		("vm_object_terminate: pageout in progress"));
60926f9a767SRodney W. Grimes 
61026f9a767SRodney W. Grimes 	/*
6110d94caffSDavid Greenman 	 * Clean and free the pages, as appropriate. All references to the
6120d94caffSDavid Greenman 	 * object are gone, so we don't need to lock it.
61326f9a767SRodney W. Grimes 	 */
61424a1cce3SDavid Greenman 	if (object->type == OBJT_VNODE) {
615f7dd7b63SAlan Cox 		struct vnode *vp = (struct vnode *)object->handle;
61695e5e988SJohn Dyson 
61795e5e988SJohn Dyson 		/*
61895e5e988SJohn Dyson 		 * Clean pages and flush buffers.
61995e5e988SJohn Dyson 		 */
6208f9110f6SJohn Dyson 		vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
621b6e48e03SAlan Cox 		VM_OBJECT_UNLOCK(object);
62295e5e988SJohn Dyson 
6237c0745eeSPoul-Henning Kamp 		vinvalbuf(vp, V_SAVE, NULL, 0, 0);
624f7dd7b63SAlan Cox 
625f7dd7b63SAlan Cox 		VM_OBJECT_LOCK(object);
626bef608bdSJohn Dyson 	}
627bef608bdSJohn Dyson 
628971dd342SAlfred Perlstein 	KASSERT(object->ref_count == 0,
629971dd342SAlfred Perlstein 		("vm_object_terminate: object with references, ref_count=%d",
630971dd342SAlfred Perlstein 		object->ref_count));
631996c772fSJohn Dyson 
6320d94caffSDavid Greenman 	/*
633356863ebSDavid Greenman 	 * Now free any remaining pages. For internal objects, this also
634356863ebSDavid Greenman 	 * removes them from paging queues. Don't free wired pages, just
635356863ebSDavid Greenman 	 * remove them from the object.
636df8bae1dSRodney W. Grimes 	 */
63756030358SAlan Cox 	vm_page_lock_queues();
638b18bfc3dSJohn Dyson 	while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
6399af80719SAlan Cox 		KASSERT(!p->busy && (p->oflags & VPO_BUSY) == 0,
640971dd342SAlfred Perlstein 			("vm_object_terminate: freeing busy page %p "
641971dd342SAlfred Perlstein 			"p->busy = %d, p->flags %x\n", p, p->busy, p->flags));
6420b10ba98SDavid Greenman 		if (p->wire_count == 0) {
643df8bae1dSRodney W. Grimes 			vm_page_free(p);
644393a081dSAttilio Rao 			cnt.v_pfree++;
6450b10ba98SDavid Greenman 		} else {
6460b10ba98SDavid Greenman 			vm_page_remove(p);
6470b10ba98SDavid Greenman 		}
648df8bae1dSRodney W. Grimes 	}
64956030358SAlan Cox 	vm_page_unlock_queues();
650bef608bdSJohn Dyson 
6512d8acc0fSJohn Dyson 	/*
6529fcfb650SDavid Greenman 	 * Let the pager know object is dead.
6539fcfb650SDavid Greenman 	 */
6549fcfb650SDavid Greenman 	vm_pager_deallocate(object);
655658ad5ffSAlan Cox 	VM_OBJECT_UNLOCK(object);
6569fcfb650SDavid Greenman 
6579fcfb650SDavid Greenman 	/*
6580b10ba98SDavid Greenman 	 * Remove the object from the global object list.
6592d8acc0fSJohn Dyson 	 */
6609ed346baSBosko Milekic 	mtx_lock(&vm_object_list_mtx);
661df8bae1dSRodney W. Grimes 	TAILQ_REMOVE(&vm_object_list, object, object_list);
6629ed346baSBosko Milekic 	mtx_unlock(&vm_object_list_mtx);
6630b10ba98SDavid Greenman 
664df8bae1dSRodney W. Grimes 	/*
665df8bae1dSRodney W. Grimes 	 * Free the space for the object.
666df8bae1dSRodney W. Grimes 	 */
667670d17b5SJeff Roberson 	uma_zfree(obj_zone, object);
66847221757SJohn Dyson }
669df8bae1dSRodney W. Grimes 
670df8bae1dSRodney W. Grimes /*
671df8bae1dSRodney W. Grimes  *	vm_object_page_clean
672df8bae1dSRodney W. Grimes  *
6734f79d873SMatthew Dillon  *	Clean all dirty pages in the specified range of object.  Leaves page
6744f79d873SMatthew Dillon  * 	on whatever queue it is currently on.   If NOSYNC is set then do not
675b146f9e5SAlan Cox  *	write out pages with VPO_NOSYNC set (originally comes from MAP_NOSYNC),
6764f79d873SMatthew Dillon  *	leaving the object dirty.
67726f9a767SRodney W. Grimes  *
67843b7990eSMatthew Dillon  *	When stuffing pages asynchronously, allow clustering.  XXX we need a
67943b7990eSMatthew Dillon  *	synchronous clustering mode implementation.
68043b7990eSMatthew Dillon  *
68126f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
68226f9a767SRodney W. Grimes  *
68326f9a767SRodney W. Grimes  *	The object must be locked.
68426f9a767SRodney W. Grimes  */
685f6b04d2bSDavid Greenman void
6861b40f8c0SMatthew Dillon vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end, int flags)
687f6b04d2bSDavid Greenman {
688b9b7a4beSMatthew Dillon 	vm_page_t p, np;
6896395da54SIan Dowse 	vm_pindex_t tstart, tend;
690bd7e5f99SJohn Dyson 	vm_pindex_t pi;
6914f79d873SMatthew Dillon 	int clearobjflags;
6928f9110f6SJohn Dyson 	int pagerflags;
6932d8acc0fSJohn Dyson 	int curgeneration;
694f6b04d2bSDavid Greenman 
695b6e48e03SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
696aef922f5SJohn Dyson 	if (object->type != OBJT_VNODE ||
697aef922f5SJohn Dyson 		(object->flags & OBJ_MIGHTBEDIRTY) == 0)
698f6b04d2bSDavid Greenman 		return;
699f6b04d2bSDavid Greenman 
70043b7990eSMatthew Dillon 	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
7018f9110f6SJohn Dyson 	pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
7028f9110f6SJohn Dyson 
703069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_CLEANING);
70424a1cce3SDavid Greenman 
705f6b04d2bSDavid Greenman 	tstart = start;
706f6b04d2bSDavid Greenman 	if (end == 0) {
707f6b04d2bSDavid Greenman 		tend = object->size;
708f6b04d2bSDavid Greenman 	} else {
709f6b04d2bSDavid Greenman 		tend = end;
710f6b04d2bSDavid Greenman 	}
711eaf13dd7SJohn Dyson 
71235c01631SAlan Cox 	vm_page_lock_queues();
7134f79d873SMatthew Dillon 	/*
714b9b7a4beSMatthew Dillon 	 * If the caller is smart and only msync()s a range he knows is
715b9b7a4beSMatthew Dillon 	 * dirty, we may be able to avoid an object scan.  This results in
716b9b7a4beSMatthew Dillon 	 * a phenominal improvement in performance.  We cannot do this
717b9b7a4beSMatthew Dillon 	 * as a matter of course because the object may be huge - e.g.
718b9b7a4beSMatthew Dillon 	 * the size might be in the gigabytes or terrabytes.
719b9b7a4beSMatthew Dillon 	 */
720b9b7a4beSMatthew Dillon 	if (msync_flush_flags & MSYNC_FLUSH_HARDSEQ) {
721300b96acSIan Dowse 		vm_pindex_t tscan;
722b9b7a4beSMatthew Dillon 		int scanlimit;
723b9b7a4beSMatthew Dillon 		int scanreset;
724b9b7a4beSMatthew Dillon 
725b9b7a4beSMatthew Dillon 		scanreset = object->resident_page_count / EASY_SCAN_FACTOR;
726b9b7a4beSMatthew Dillon 		if (scanreset < 16)
727b9b7a4beSMatthew Dillon 			scanreset = 16;
72843b7990eSMatthew Dillon 		pagerflags |= VM_PAGER_IGNORE_CLEANCHK;
729b9b7a4beSMatthew Dillon 
730b9b7a4beSMatthew Dillon 		scanlimit = scanreset;
731b9b7a4beSMatthew Dillon 		tscan = tstart;
732b9b7a4beSMatthew Dillon 		while (tscan < tend) {
733b9b7a4beSMatthew Dillon 			curgeneration = object->generation;
734b9b7a4beSMatthew Dillon 			p = vm_page_lookup(object, tscan);
735b9b7a4beSMatthew Dillon 			if (p == NULL || p->valid == 0 ||
736997e1c25SAlan Cox 			    VM_PAGE_INQUEUE1(p, PQ_CACHE)) {
737b9b7a4beSMatthew Dillon 				if (--scanlimit == 0)
738b9b7a4beSMatthew Dillon 					break;
739b9b7a4beSMatthew Dillon 				++tscan;
740b9b7a4beSMatthew Dillon 				continue;
741b9b7a4beSMatthew Dillon 			}
742b9b7a4beSMatthew Dillon 			vm_page_test_dirty(p);
743b9b7a4beSMatthew Dillon 			if ((p->dirty & p->valid) == 0) {
744b9b7a4beSMatthew Dillon 				if (--scanlimit == 0)
745b9b7a4beSMatthew Dillon 					break;
746b9b7a4beSMatthew Dillon 				++tscan;
747b9b7a4beSMatthew Dillon 				continue;
748b9b7a4beSMatthew Dillon 			}
749b9b7a4beSMatthew Dillon 			/*
750b9b7a4beSMatthew Dillon 			 * If we have been asked to skip nosync pages and
751b9b7a4beSMatthew Dillon 			 * this is a nosync page, we can't continue.
752b9b7a4beSMatthew Dillon 			 */
753b146f9e5SAlan Cox 			if ((flags & OBJPC_NOSYNC) && (p->oflags & VPO_NOSYNC)) {
754b9b7a4beSMatthew Dillon 				if (--scanlimit == 0)
755b9b7a4beSMatthew Dillon 					break;
756b9b7a4beSMatthew Dillon 				++tscan;
757b9b7a4beSMatthew Dillon 				continue;
758b9b7a4beSMatthew Dillon 			}
759b9b7a4beSMatthew Dillon 			scanlimit = scanreset;
760b9b7a4beSMatthew Dillon 
761b9b7a4beSMatthew Dillon 			/*
762b9b7a4beSMatthew Dillon 			 * This returns 0 if it was unable to busy the first
763b9b7a4beSMatthew Dillon 			 * page (i.e. had to sleep).
764b9b7a4beSMatthew Dillon 			 */
765b9b7a4beSMatthew Dillon 			tscan += vm_object_page_collect_flush(object, p, curgeneration, pagerflags);
766b9b7a4beSMatthew Dillon 		}
767b9b7a4beSMatthew Dillon 
768b9b7a4beSMatthew Dillon 		/*
769b9b7a4beSMatthew Dillon 		 * If everything was dirty and we flushed it successfully,
770b9b7a4beSMatthew Dillon 		 * and the requested range is not the entire object, we
771b9b7a4beSMatthew Dillon 		 * don't have to mess with CLEANCHK or MIGHTBEDIRTY and can
772b9b7a4beSMatthew Dillon 		 * return immediately.
773b9b7a4beSMatthew Dillon 		 */
774b9b7a4beSMatthew Dillon 		if (tscan >= tend && (tstart || tend < object->size)) {
77535c01631SAlan Cox 			vm_page_unlock_queues();
776b9b7a4beSMatthew Dillon 			vm_object_clear_flag(object, OBJ_CLEANING);
777b9b7a4beSMatthew Dillon 			return;
778b9b7a4beSMatthew Dillon 		}
77943b7990eSMatthew Dillon 		pagerflags &= ~VM_PAGER_IGNORE_CLEANCHK;
780b9b7a4beSMatthew Dillon 	}
781b9b7a4beSMatthew Dillon 
782b9b7a4beSMatthew Dillon 	/*
7834f79d873SMatthew Dillon 	 * Generally set CLEANCHK interlock and make the page read-only so
7844f79d873SMatthew Dillon 	 * we can then clear the object flags.
7854f79d873SMatthew Dillon 	 *
7864f79d873SMatthew Dillon 	 * However, if this is a nosync mmap then the object is likely to
7874f79d873SMatthew Dillon 	 * stay dirty so do not mess with the page and do not clear the
7884f79d873SMatthew Dillon 	 * object flags.
7894f79d873SMatthew Dillon 	 */
7904f79d873SMatthew Dillon 	clearobjflags = 1;
791fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(p, &object->memq, listq) {
7920cd31a0dSAlan Cox 		p->oflags |= VPO_CLEANCHK;
793b146f9e5SAlan Cox 		if ((flags & OBJPC_NOSYNC) && (p->oflags & VPO_NOSYNC))
7944f79d873SMatthew Dillon 			clearobjflags = 0;
7954f79d873SMatthew Dillon 		else
79678985e42SAlan Cox 			pmap_remove_write(p);
797eaf13dd7SJohn Dyson 	}
798eaf13dd7SJohn Dyson 
7994f79d873SMatthew Dillon 	if (clearobjflags && (tstart == 0) && (tend == object->size)) {
800245df27cSMatthew Dillon 		struct vnode *vp;
801245df27cSMatthew Dillon 
802af51d7bfSAlan Cox 		vm_object_clear_flag(object, OBJ_MIGHTBEDIRTY);
803245df27cSMatthew Dillon 		if (object->type == OBJT_VNODE &&
804245df27cSMatthew Dillon 		    (vp = (struct vnode *)object->handle) != NULL) {
805e6e370a7SJeff Roberson 			VI_LOCK(vp);
806e6e370a7SJeff Roberson 			if (vp->v_iflag & VI_OBJDIRTY)
807e6e370a7SJeff Roberson 				vp->v_iflag &= ~VI_OBJDIRTY;
808e6e370a7SJeff Roberson 			VI_UNLOCK(vp);
809245df27cSMatthew Dillon 		}
810ec4f9fb0SDavid Greenman 	}
811f6b04d2bSDavid Greenman 
812bd7e5f99SJohn Dyson rescan:
8132d8acc0fSJohn Dyson 	curgeneration = object->generation;
8142d8acc0fSJohn Dyson 
815b18bfc3dSJohn Dyson 	for (p = TAILQ_FIRST(&object->memq); p; p = np) {
816b9b7a4beSMatthew Dillon 		int n;
817b9b7a4beSMatthew Dillon 
818b18bfc3dSJohn Dyson 		np = TAILQ_NEXT(p, listq);
819bd7e5f99SJohn Dyson 
820b9b7a4beSMatthew Dillon again:
821bd7e5f99SJohn Dyson 		pi = p->pindex;
8220cd31a0dSAlan Cox 		if ((p->oflags & VPO_CLEANCHK) == 0 ||
823bd7e5f99SJohn Dyson 			(pi < tstart) || (pi >= tend) ||
8245070c7f8SJohn Dyson 			(p->valid == 0) ||
825997e1c25SAlan Cox 		    VM_PAGE_INQUEUE1(p, PQ_CACHE)) {
8260cd31a0dSAlan Cox 			p->oflags &= ~VPO_CLEANCHK;
827aef922f5SJohn Dyson 			continue;
828f6b04d2bSDavid Greenman 		}
829f6b04d2bSDavid Greenman 
830bd7e5f99SJohn Dyson 		vm_page_test_dirty(p);
831bd7e5f99SJohn Dyson 		if ((p->dirty & p->valid) == 0) {
8320cd31a0dSAlan Cox 			p->oflags &= ~VPO_CLEANCHK;
833bd7e5f99SJohn Dyson 			continue;
834bd7e5f99SJohn Dyson 		}
835ec4f9fb0SDavid Greenman 
8364f79d873SMatthew Dillon 		/*
8374f79d873SMatthew Dillon 		 * If we have been asked to skip nosync pages and this is a
8384f79d873SMatthew Dillon 		 * nosync page, skip it.  Note that the object flags were
8394f79d873SMatthew Dillon 		 * not cleared in this case so we do not have to set them.
8404f79d873SMatthew Dillon 		 */
841b146f9e5SAlan Cox 		if ((flags & OBJPC_NOSYNC) && (p->oflags & VPO_NOSYNC)) {
8420cd31a0dSAlan Cox 			p->oflags &= ~VPO_CLEANCHK;
8434f79d873SMatthew Dillon 			continue;
8444f79d873SMatthew Dillon 		}
8454f79d873SMatthew Dillon 
846b9b7a4beSMatthew Dillon 		n = vm_object_page_collect_flush(object, p,
847b9b7a4beSMatthew Dillon 			curgeneration, pagerflags);
848b9b7a4beSMatthew Dillon 		if (n == 0)
849b9b7a4beSMatthew Dillon 			goto rescan;
850b9b7a4beSMatthew Dillon 
851b9b7a4beSMatthew Dillon 		if (object->generation != curgeneration)
852b9b7a4beSMatthew Dillon 			goto rescan;
853b9b7a4beSMatthew Dillon 
854b9b7a4beSMatthew Dillon 		/*
855b9b7a4beSMatthew Dillon 		 * Try to optimize the next page.  If we can't we pick up
856b9b7a4beSMatthew Dillon 		 * our (random) scan where we left off.
857b9b7a4beSMatthew Dillon 		 */
858b9b7a4beSMatthew Dillon 		if (msync_flush_flags & MSYNC_FLUSH_SOFTSEQ) {
859b9b7a4beSMatthew Dillon 			if ((p = vm_page_lookup(object, pi + n)) != NULL)
860b9b7a4beSMatthew Dillon 				goto again;
861b9b7a4beSMatthew Dillon 		}
862b9b7a4beSMatthew Dillon 	}
86335c01631SAlan Cox 	vm_page_unlock_queues();
864b9b7a4beSMatthew Dillon #if 0
8658df6bac4SPoul-Henning Kamp 	VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
866b9b7a4beSMatthew Dillon #endif
867b9b7a4beSMatthew Dillon 
868b9b7a4beSMatthew Dillon 	vm_object_clear_flag(object, OBJ_CLEANING);
869b9b7a4beSMatthew Dillon 	return;
870b9b7a4beSMatthew Dillon }
871b9b7a4beSMatthew Dillon 
872b9b7a4beSMatthew Dillon static int
873b9b7a4beSMatthew Dillon vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags)
874b9b7a4beSMatthew Dillon {
875b9b7a4beSMatthew Dillon 	int runlen;
876b9b7a4beSMatthew Dillon 	int maxf;
877b9b7a4beSMatthew Dillon 	int chkb;
878b9b7a4beSMatthew Dillon 	int maxb;
879b9b7a4beSMatthew Dillon 	int i;
880b9b7a4beSMatthew Dillon 	vm_pindex_t pi;
881b9b7a4beSMatthew Dillon 	vm_page_t maf[vm_pageout_page_count];
882b9b7a4beSMatthew Dillon 	vm_page_t mab[vm_pageout_page_count];
883b9b7a4beSMatthew Dillon 	vm_page_t ma[vm_pageout_page_count];
884b9b7a4beSMatthew Dillon 
88535c01631SAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
886b9b7a4beSMatthew Dillon 	pi = p->pindex;
887bd82dc74SAlan Cox 	while (vm_page_sleep_if_busy(p, TRUE, "vpcwai")) {
88835c01631SAlan Cox 		vm_page_lock_queues();
8892d8acc0fSJohn Dyson 		if (object->generation != curgeneration) {
890b9b7a4beSMatthew Dillon 			return(0);
891f6b04d2bSDavid Greenman 		}
892bd82dc74SAlan Cox 	}
893bd7e5f99SJohn Dyson 	maxf = 0;
894bd7e5f99SJohn Dyson 	for(i = 1; i < vm_pageout_page_count; i++) {
895b9b7a4beSMatthew Dillon 		vm_page_t tp;
896b9b7a4beSMatthew Dillon 
8978aef1712SMatthew Dillon 		if ((tp = vm_page_lookup(object, pi + i)) != NULL) {
8989af80719SAlan Cox 			if ((tp->oflags & VPO_BUSY) ||
89943b7990eSMatthew Dillon 				((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
9000cd31a0dSAlan Cox 				 (tp->oflags & VPO_CLEANCHK) == 0) ||
901ffc82b0aSJohn Dyson 				(tp->busy != 0))
902bd7e5f99SJohn Dyson 				break;
903997e1c25SAlan Cox 			if (VM_PAGE_INQUEUE1(tp, PQ_CACHE)) {
9040cd31a0dSAlan Cox 				tp->oflags &= ~VPO_CLEANCHK;
9053077a9c2SJohn Dyson 				break;
9063077a9c2SJohn Dyson 			}
907bd7e5f99SJohn Dyson 			vm_page_test_dirty(tp);
908bd7e5f99SJohn Dyson 			if ((tp->dirty & tp->valid) == 0) {
9090cd31a0dSAlan Cox 				tp->oflags &= ~VPO_CLEANCHK;
910bd7e5f99SJohn Dyson 				break;
911bd7e5f99SJohn Dyson 			}
912bd7e5f99SJohn Dyson 			maf[ i - 1 ] = tp;
913bd7e5f99SJohn Dyson 			maxf++;
914bd7e5f99SJohn Dyson 			continue;
915bd7e5f99SJohn Dyson 		}
916bd7e5f99SJohn Dyson 		break;
917bd7e5f99SJohn Dyson 	}
918aef922f5SJohn Dyson 
919bd7e5f99SJohn Dyson 	maxb = 0;
920bd7e5f99SJohn Dyson 	chkb = vm_pageout_page_count -  maxf;
921bd7e5f99SJohn Dyson 	if (chkb) {
922bd7e5f99SJohn Dyson 		for(i = 1; i < chkb;i++) {
923b9b7a4beSMatthew Dillon 			vm_page_t tp;
924b9b7a4beSMatthew Dillon 
9258aef1712SMatthew Dillon 			if ((tp = vm_page_lookup(object, pi - i)) != NULL) {
9269af80719SAlan Cox 				if ((tp->oflags & VPO_BUSY) ||
92743b7990eSMatthew Dillon 					((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
9280cd31a0dSAlan Cox 					 (tp->oflags & VPO_CLEANCHK) == 0) ||
929ffc82b0aSJohn Dyson 					(tp->busy != 0))
930bd7e5f99SJohn Dyson 					break;
931997e1c25SAlan Cox 				if (VM_PAGE_INQUEUE1(tp, PQ_CACHE)) {
9320cd31a0dSAlan Cox 					tp->oflags &= ~VPO_CLEANCHK;
9333077a9c2SJohn Dyson 					break;
9343077a9c2SJohn Dyson 				}
935bd7e5f99SJohn Dyson 				vm_page_test_dirty(tp);
936bd7e5f99SJohn Dyson 				if ((tp->dirty & tp->valid) == 0) {
9370cd31a0dSAlan Cox 					tp->oflags &= ~VPO_CLEANCHK;
938bd7e5f99SJohn Dyson 					break;
939bd7e5f99SJohn Dyson 				}
940bd7e5f99SJohn Dyson 				mab[ i - 1 ] = tp;
941bd7e5f99SJohn Dyson 				maxb++;
942bd7e5f99SJohn Dyson 				continue;
943bd7e5f99SJohn Dyson 			}
944bd7e5f99SJohn Dyson 			break;
945bd7e5f99SJohn Dyson 		}
946bd7e5f99SJohn Dyson 	}
947bd7e5f99SJohn Dyson 
948bd7e5f99SJohn Dyson 	for(i = 0; i < maxb; i++) {
949bd7e5f99SJohn Dyson 		int index = (maxb - i) - 1;
950bd7e5f99SJohn Dyson 		ma[index] = mab[i];
9510cd31a0dSAlan Cox 		ma[index]->oflags &= ~VPO_CLEANCHK;
952bd7e5f99SJohn Dyson 	}
9530cd31a0dSAlan Cox 	p->oflags &= ~VPO_CLEANCHK;
954bd7e5f99SJohn Dyson 	ma[maxb] = p;
955bd7e5f99SJohn Dyson 	for(i = 0; i < maxf; i++) {
956bd7e5f99SJohn Dyson 		int index = (maxb + i) + 1;
957bd7e5f99SJohn Dyson 		ma[index] = maf[i];
9580cd31a0dSAlan Cox 		ma[index]->oflags &= ~VPO_CLEANCHK;
959f6b04d2bSDavid Greenman 	}
960bd7e5f99SJohn Dyson 	runlen = maxb + maxf + 1;
961cf2819ccSJohn Dyson 
9627a935082SAlan Cox 	vm_pageout_flush(ma, runlen, pagerflags);
963cf2819ccSJohn Dyson 	for (i = 0; i < runlen; i++) {
964cf2819ccSJohn Dyson 		if (ma[i]->valid & ma[i]->dirty) {
96578985e42SAlan Cox 			pmap_remove_write(ma[i]);
9660cd31a0dSAlan Cox 			ma[i]->oflags |= VPO_CLEANCHK;
967aef922f5SJohn Dyson 
968b9b7a4beSMatthew Dillon 			/*
969b9b7a4beSMatthew Dillon 			 * maxf will end up being the actual number of pages
970b9b7a4beSMatthew Dillon 			 * we wrote out contiguously, non-inclusive of the
971b9b7a4beSMatthew Dillon 			 * first page.  We do not count look-behind pages.
972b9b7a4beSMatthew Dillon 			 */
973b9b7a4beSMatthew Dillon 			if (i >= maxb + 1 && (maxf > i - maxb - 1))
974b9b7a4beSMatthew Dillon 				maxf = i - maxb - 1;
975b9b7a4beSMatthew Dillon 		}
976b9b7a4beSMatthew Dillon 	}
977b9b7a4beSMatthew Dillon 	return(maxf + 1);
97826f9a767SRodney W. Grimes }
979df8bae1dSRodney W. Grimes 
9801efb74fbSJohn Dyson /*
981950f8459SAlan Cox  * Note that there is absolutely no sense in writing out
982950f8459SAlan Cox  * anonymous objects, so we track down the vnode object
983950f8459SAlan Cox  * to write out.
984950f8459SAlan Cox  * We invalidate (remove) all pages from the address space
985950f8459SAlan Cox  * for semantic correctness.
986950f8459SAlan Cox  *
987950f8459SAlan Cox  * Note: certain anonymous maps, such as MAP_NOSYNC maps,
988950f8459SAlan Cox  * may start out with a NULL object.
989950f8459SAlan Cox  */
990950f8459SAlan Cox void
991950f8459SAlan Cox vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size,
992950f8459SAlan Cox     boolean_t syncio, boolean_t invalidate)
993950f8459SAlan Cox {
994950f8459SAlan Cox 	vm_object_t backing_object;
995950f8459SAlan Cox 	struct vnode *vp;
9963b582b4eSTor Egge 	struct mount *mp;
997950f8459SAlan Cox 	int flags;
998950f8459SAlan Cox 
999950f8459SAlan Cox 	if (object == NULL)
1000950f8459SAlan Cox 		return;
1001950f8459SAlan Cox 	VM_OBJECT_LOCK(object);
1002950f8459SAlan Cox 	while ((backing_object = object->backing_object) != NULL) {
1003950f8459SAlan Cox 		VM_OBJECT_LOCK(backing_object);
100456e0670fSAlan Cox 		offset += object->backing_object_offset;
1005950f8459SAlan Cox 		VM_OBJECT_UNLOCK(object);
1006950f8459SAlan Cox 		object = backing_object;
1007950f8459SAlan Cox 		if (object->size < OFF_TO_IDX(offset + size))
1008950f8459SAlan Cox 			size = IDX_TO_OFF(object->size) - offset;
1009950f8459SAlan Cox 	}
1010950f8459SAlan Cox 	/*
1011950f8459SAlan Cox 	 * Flush pages if writing is allowed, invalidate them
1012950f8459SAlan Cox 	 * if invalidation requested.  Pages undergoing I/O
1013950f8459SAlan Cox 	 * will be ignored by vm_object_page_remove().
1014950f8459SAlan Cox 	 *
1015950f8459SAlan Cox 	 * We cannot lock the vnode and then wait for paging
1016950f8459SAlan Cox 	 * to complete without deadlocking against vm_fault.
1017950f8459SAlan Cox 	 * Instead we simply call vm_object_page_remove() and
1018950f8459SAlan Cox 	 * allow it to block internally on a page-by-page
1019950f8459SAlan Cox 	 * basis when it encounters pages undergoing async
1020950f8459SAlan Cox 	 * I/O.
1021950f8459SAlan Cox 	 */
1022950f8459SAlan Cox 	if (object->type == OBJT_VNODE &&
1023950f8459SAlan Cox 	    (object->flags & OBJ_MIGHTBEDIRTY) != 0) {
1024ae51ff11SJeff Roberson 		int vfslocked;
1025950f8459SAlan Cox 		vp = object->handle;
1026950f8459SAlan Cox 		VM_OBJECT_UNLOCK(object);
10273b582b4eSTor Egge 		(void) vn_start_write(vp, &mp, V_WAIT);
1028ae51ff11SJeff Roberson 		vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1029950f8459SAlan Cox 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
1030950f8459SAlan Cox 		flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
1031950f8459SAlan Cox 		flags |= invalidate ? OBJPC_INVAL : 0;
1032950f8459SAlan Cox 		VM_OBJECT_LOCK(object);
1033950f8459SAlan Cox 		vm_object_page_clean(object,
1034950f8459SAlan Cox 		    OFF_TO_IDX(offset),
1035950f8459SAlan Cox 		    OFF_TO_IDX(offset + size + PAGE_MASK),
1036950f8459SAlan Cox 		    flags);
1037950f8459SAlan Cox 		VM_OBJECT_UNLOCK(object);
1038950f8459SAlan Cox 		VOP_UNLOCK(vp, 0, curthread);
1039ae51ff11SJeff Roberson 		VFS_UNLOCK_GIANT(vfslocked);
10403b582b4eSTor Egge 		vn_finished_write(mp);
1041950f8459SAlan Cox 		VM_OBJECT_LOCK(object);
1042950f8459SAlan Cox 	}
1043950f8459SAlan Cox 	if ((object->type == OBJT_VNODE ||
1044950f8459SAlan Cox 	     object->type == OBJT_DEVICE) && invalidate) {
1045874f0135SDoug Rabson 		boolean_t purge;
1046874f0135SDoug Rabson 		purge = old_msync || (object->type == OBJT_DEVICE);
1047950f8459SAlan Cox 		vm_object_page_remove(object,
1048950f8459SAlan Cox 		    OFF_TO_IDX(offset),
1049950f8459SAlan Cox 		    OFF_TO_IDX(offset + size + PAGE_MASK),
1050874f0135SDoug Rabson 		    purge ? FALSE : TRUE);
1051950f8459SAlan Cox 	}
1052950f8459SAlan Cox 	VM_OBJECT_UNLOCK(object);
1053950f8459SAlan Cox }
1054950f8459SAlan Cox 
1055950f8459SAlan Cox /*
1056867a482dSJohn Dyson  *	vm_object_madvise:
1057867a482dSJohn Dyson  *
1058867a482dSJohn Dyson  *	Implements the madvise function at the object/page level.
10591c7c3c6aSMatthew Dillon  *
1060193b9358SAlan Cox  *	MADV_WILLNEED	(any object)
1061193b9358SAlan Cox  *
1062193b9358SAlan Cox  *	    Activate the specified pages if they are resident.
1063193b9358SAlan Cox  *
1064193b9358SAlan Cox  *	MADV_DONTNEED	(any object)
1065193b9358SAlan Cox  *
1066193b9358SAlan Cox  *	    Deactivate the specified pages if they are resident.
1067193b9358SAlan Cox  *
1068193b9358SAlan Cox  *	MADV_FREE	(OBJT_DEFAULT/OBJT_SWAP objects,
1069193b9358SAlan Cox  *			 OBJ_ONEMAPPING only)
1070193b9358SAlan Cox  *
1071193b9358SAlan Cox  *	    Deactivate and clean the specified pages if they are
1072193b9358SAlan Cox  *	    resident.  This permits the process to reuse the pages
1073193b9358SAlan Cox  *	    without faulting or the kernel to reclaim the pages
1074193b9358SAlan Cox  *	    without I/O.
1075867a482dSJohn Dyson  */
1076867a482dSJohn Dyson void
10771b40f8c0SMatthew Dillon vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
1078867a482dSJohn Dyson {
10796e20a165SJohn Dyson 	vm_pindex_t end, tpindex;
108034567de7SAlan Cox 	vm_object_t backing_object, tobject;
1081867a482dSJohn Dyson 	vm_page_t m;
1082867a482dSJohn Dyson 
1083867a482dSJohn Dyson 	if (object == NULL)
1084867a482dSJohn Dyson 		return;
10859b98b796SAlan Cox 	VM_OBJECT_LOCK(object);
1086867a482dSJohn Dyson 	end = pindex + count;
10871c7c3c6aSMatthew Dillon 	/*
10881c7c3c6aSMatthew Dillon 	 * Locate and adjust resident pages
10891c7c3c6aSMatthew Dillon 	 */
10901c7c3c6aSMatthew Dillon 	for (; pindex < end; pindex += 1) {
10916e20a165SJohn Dyson relookup:
10926e20a165SJohn Dyson 		tobject = object;
10936e20a165SJohn Dyson 		tpindex = pindex;
10946e20a165SJohn Dyson shadowlookup:
109558b4e6ccSAlan Cox 		/*
109658b4e6ccSAlan Cox 		 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
109758b4e6ccSAlan Cox 		 * and those pages must be OBJ_ONEMAPPING.
109858b4e6ccSAlan Cox 		 */
109958b4e6ccSAlan Cox 		if (advise == MADV_FREE) {
110058b4e6ccSAlan Cox 			if ((tobject->type != OBJT_DEFAULT &&
110158b4e6ccSAlan Cox 			     tobject->type != OBJT_SWAP) ||
110258b4e6ccSAlan Cox 			    (tobject->flags & OBJ_ONEMAPPING) == 0) {
110334567de7SAlan Cox 				goto unlock_tobject;
11046e20a165SJohn Dyson 			}
110558b4e6ccSAlan Cox 		}
11061c7c3c6aSMatthew Dillon 		m = vm_page_lookup(tobject, tpindex);
11071c7c3c6aSMatthew Dillon 		if (m == NULL) {
11081ce137beSMatthew Dillon 			/*
11091ce137beSMatthew Dillon 			 * There may be swap even if there is no backing page
11101ce137beSMatthew Dillon 			 */
11111ce137beSMatthew Dillon 			if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
11121ce137beSMatthew Dillon 				swap_pager_freespace(tobject, tpindex, 1);
11131ce137beSMatthew Dillon 			/*
11141ce137beSMatthew Dillon 			 * next object
11151ce137beSMatthew Dillon 			 */
111634567de7SAlan Cox 			backing_object = tobject->backing_object;
111734567de7SAlan Cox 			if (backing_object == NULL)
111834567de7SAlan Cox 				goto unlock_tobject;
111934567de7SAlan Cox 			VM_OBJECT_LOCK(backing_object);
112056e0670fSAlan Cox 			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
11219b98b796SAlan Cox 			if (tobject != object)
112234567de7SAlan Cox 				VM_OBJECT_UNLOCK(tobject);
112334567de7SAlan Cox 			tobject = backing_object;
11246e20a165SJohn Dyson 			goto shadowlookup;
11256e20a165SJohn Dyson 		}
1126867a482dSJohn Dyson 		/*
1127867a482dSJohn Dyson 		 * If the page is busy or not in a normal active state,
11288b03c8edSMatthew Dillon 		 * we skip it.  If the page is not managed there are no
11298b03c8edSMatthew Dillon 		 * page queues to mess with.  Things can break if we mess
11308b03c8edSMatthew Dillon 		 * with pages in any of the below states.
1131867a482dSJohn Dyson 		 */
113232585dd6SAlan Cox 		vm_page_lock_queues();
113332585dd6SAlan Cox 		if (m->hold_count ||
11341c7c3c6aSMatthew Dillon 		    m->wire_count ||
11358b03c8edSMatthew Dillon 		    (m->flags & PG_UNMANAGED) ||
113632585dd6SAlan Cox 		    m->valid != VM_PAGE_BITS_ALL) {
113732585dd6SAlan Cox 			vm_page_unlock_queues();
113834567de7SAlan Cox 			goto unlock_tobject;
11396e20a165SJohn Dyson 		}
11409af80719SAlan Cox 		if ((m->oflags & VPO_BUSY) || m->busy) {
11415786be7cSAlan Cox 			vm_page_flag_set(m, PG_REFERENCED);
114291449ce9SAlan Cox 			vm_page_unlock_queues();
11439b98b796SAlan Cox 			if (object != tobject)
11449b98b796SAlan Cox 				VM_OBJECT_UNLOCK(object);
11455786be7cSAlan Cox 			m->oflags |= VPO_WANTED;
114691449ce9SAlan Cox 			msleep(m, VM_OBJECT_MTX(tobject), PDROP | PVM, "madvpo", 0);
11479b98b796SAlan Cox 			VM_OBJECT_LOCK(object);
11486e20a165SJohn Dyson   			goto relookup;
114934567de7SAlan Cox 		}
1150867a482dSJohn Dyson 		if (advise == MADV_WILLNEED) {
1151867a482dSJohn Dyson 			vm_page_activate(m);
11526e20a165SJohn Dyson 		} else if (advise == MADV_DONTNEED) {
1153479112dfSMatthew Dillon 			vm_page_dontneed(m);
11540a47b48bSJohn Dyson 		} else if (advise == MADV_FREE) {
11551c7c3c6aSMatthew Dillon 			/*
11562aaeadf8SMatthew Dillon 			 * Mark the page clean.  This will allow the page
11572aaeadf8SMatthew Dillon 			 * to be freed up by the system.  However, such pages
11582aaeadf8SMatthew Dillon 			 * are often reused quickly by malloc()/free()
11592aaeadf8SMatthew Dillon 			 * so we do not do anything that would cause
11602aaeadf8SMatthew Dillon 			 * a page fault if we can help it.
11612aaeadf8SMatthew Dillon 			 *
11622aaeadf8SMatthew Dillon 			 * Specifically, we do not try to actually free
11632aaeadf8SMatthew Dillon 			 * the page now nor do we try to put it in the
11642aaeadf8SMatthew Dillon 			 * cache (which would cause a page fault on reuse).
116541c67e12SMatthew Dillon 			 *
116641c67e12SMatthew Dillon 			 * But we do make the page is freeable as we
116741c67e12SMatthew Dillon 			 * can without actually taking the step of unmapping
116841c67e12SMatthew Dillon 			 * it.
11691c7c3c6aSMatthew Dillon 			 */
11700385347cSPeter Wemm 			pmap_clear_modify(m);
11716e20a165SJohn Dyson 			m->dirty = 0;
117241c67e12SMatthew Dillon 			m->act_count = 0;
1173479112dfSMatthew Dillon 			vm_page_dontneed(m);
1174867a482dSJohn Dyson 		}
11752999e9faSAlan Cox 		vm_page_unlock_queues();
11762999e9faSAlan Cox 		if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
11772999e9faSAlan Cox 			swap_pager_freespace(tobject, tpindex, 1);
117834567de7SAlan Cox unlock_tobject:
11799b98b796SAlan Cox 		if (tobject != object)
118034567de7SAlan Cox 			VM_OBJECT_UNLOCK(tobject);
1181867a482dSJohn Dyson 	}
11829b98b796SAlan Cox 	VM_OBJECT_UNLOCK(object);
1183867a482dSJohn Dyson }
1184867a482dSJohn Dyson 
1185867a482dSJohn Dyson /*
1186df8bae1dSRodney W. Grimes  *	vm_object_shadow:
1187df8bae1dSRodney W. Grimes  *
1188df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
1189df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
1190df8bae1dSRodney W. Grimes  *	object reference is deallocated.
1191df8bae1dSRodney W. Grimes  *
1192df8bae1dSRodney W. Grimes  *	The new object and offset into that object
1193df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
1194df8bae1dSRodney W. Grimes  */
119526f9a767SRodney W. Grimes void
11961b40f8c0SMatthew Dillon vm_object_shadow(
11971b40f8c0SMatthew Dillon 	vm_object_t *object,	/* IN/OUT */
11981b40f8c0SMatthew Dillon 	vm_ooffset_t *offset,	/* IN/OUT */
11991b40f8c0SMatthew Dillon 	vm_size_t length)
1200df8bae1dSRodney W. Grimes {
1201d031cff1SMatthew Dillon 	vm_object_t source;
1202d031cff1SMatthew Dillon 	vm_object_t result;
1203df8bae1dSRodney W. Grimes 
1204df8bae1dSRodney W. Grimes 	source = *object;
1205df8bae1dSRodney W. Grimes 
1206df8bae1dSRodney W. Grimes 	/*
12079a2f6362SAlan Cox 	 * Don't create the new object if the old object isn't shared.
12089a2f6362SAlan Cox 	 */
1209570a2f4aSAlan Cox 	if (source != NULL) {
1210570a2f4aSAlan Cox 		VM_OBJECT_LOCK(source);
1211570a2f4aSAlan Cox 		if (source->ref_count == 1 &&
12129a2f6362SAlan Cox 		    source->handle == NULL &&
12139a2f6362SAlan Cox 		    (source->type == OBJT_DEFAULT ||
12149917e010SAlan Cox 		     source->type == OBJT_SWAP)) {
1215570a2f4aSAlan Cox 			VM_OBJECT_UNLOCK(source);
12169a2f6362SAlan Cox 			return;
12179917e010SAlan Cox 		}
1218570a2f4aSAlan Cox 		VM_OBJECT_UNLOCK(source);
1219570a2f4aSAlan Cox 	}
12209a2f6362SAlan Cox 
12219a2f6362SAlan Cox 	/*
1222570a2f4aSAlan Cox 	 * Allocate a new object with the given length.
1223df8bae1dSRodney W. Grimes 	 */
1224971dd342SAlfred Perlstein 	result = vm_object_allocate(OBJT_DEFAULT, length);
1225df8bae1dSRodney W. Grimes 
1226df8bae1dSRodney W. Grimes 	/*
12270d94caffSDavid Greenman 	 * The new object shadows the source object, adding a reference to it.
12280d94caffSDavid Greenman 	 * Our caller changes his reference to point to the new object,
12290d94caffSDavid Greenman 	 * removing a reference to the source object.  Net result: no change
12300d94caffSDavid Greenman 	 * of reference count.
12319b09fe24SMatthew Dillon 	 *
12329b09fe24SMatthew Dillon 	 * Try to optimize the result object's page color when shadowing
1233956f3135SPhilippe Charnier 	 * in order to maintain page coloring consistency in the combined
12349b09fe24SMatthew Dillon 	 * shadowed object.
1235df8bae1dSRodney W. Grimes 	 */
123624a1cce3SDavid Greenman 	result->backing_object = source;
12379174ca7bSTor Egge 	/*
12389174ca7bSTor Egge 	 * Store the offset into the source object, and fix up the offset into
12399174ca7bSTor Egge 	 * the new object.
12409174ca7bSTor Egge 	 */
12419174ca7bSTor Egge 	result->backing_object_offset = *offset;
1242570a2f4aSAlan Cox 	if (source != NULL) {
1243570a2f4aSAlan Cox 		VM_OBJECT_LOCK(source);
12441c500307SAlan Cox 		LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1245eaf13dd7SJohn Dyson 		source->shadow_count++;
1246eaf13dd7SJohn Dyson 		source->generation++;
1247ed4fe4f4SJeff Roberson 		result->flags |= source->flags & OBJ_NEEDGIANT;
1248570a2f4aSAlan Cox 		VM_OBJECT_UNLOCK(source);
1249de5f6a77SJohn Dyson 	}
1250df8bae1dSRodney W. Grimes 
1251df8bae1dSRodney W. Grimes 
1252df8bae1dSRodney W. Grimes 	/*
1253df8bae1dSRodney W. Grimes 	 * Return the new things
1254df8bae1dSRodney W. Grimes 	 */
1255df8bae1dSRodney W. Grimes 	*offset = 0;
1256df8bae1dSRodney W. Grimes 	*object = result;
1257df8bae1dSRodney W. Grimes }
1258df8bae1dSRodney W. Grimes 
1259c5aaa06dSAlan Cox /*
1260c5aaa06dSAlan Cox  *	vm_object_split:
1261c5aaa06dSAlan Cox  *
1262c5aaa06dSAlan Cox  * Split the pages in a map entry into a new object.  This affords
1263c5aaa06dSAlan Cox  * easier removal of unused pages, and keeps object inheritance from
1264c5aaa06dSAlan Cox  * being a negative impact on memory usage.
1265c5aaa06dSAlan Cox  */
1266c5aaa06dSAlan Cox void
1267c5aaa06dSAlan Cox vm_object_split(vm_map_entry_t entry)
1268c5aaa06dSAlan Cox {
126973000556SAlan Cox 	vm_page_t m, m_next;
1270c5aaa06dSAlan Cox 	vm_object_t orig_object, new_object, source;
127173000556SAlan Cox 	vm_pindex_t idx, offidxstart;
127273000556SAlan Cox 	vm_size_t size;
1273c5aaa06dSAlan Cox 
1274c5aaa06dSAlan Cox 	orig_object = entry->object.vm_object;
1275c5aaa06dSAlan Cox 	if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1276c5aaa06dSAlan Cox 		return;
1277c5aaa06dSAlan Cox 	if (orig_object->ref_count <= 1)
1278c5aaa06dSAlan Cox 		return;
12794da9f125SAlan Cox 	VM_OBJECT_UNLOCK(orig_object);
1280c5aaa06dSAlan Cox 
12814da9f125SAlan Cox 	offidxstart = OFF_TO_IDX(entry->offset);
128295442adfSAlan Cox 	size = atop(entry->end - entry->start);
1283c5aaa06dSAlan Cox 
12844da9f125SAlan Cox 	/*
12854da9f125SAlan Cox 	 * If swap_pager_copy() is later called, it will convert new_object
12864da9f125SAlan Cox 	 * into a swap object.
12874da9f125SAlan Cox 	 */
12884da9f125SAlan Cox 	new_object = vm_object_allocate(OBJT_DEFAULT, size);
1289c5aaa06dSAlan Cox 
1290c5474b8fSAlan Cox 	/*
1291c5474b8fSAlan Cox 	 * At this point, the new object is still private, so the order in
1292c5474b8fSAlan Cox 	 * which the original and new objects are locked does not matter.
1293c5474b8fSAlan Cox 	 */
129463f6cefcSAlan Cox 	VM_OBJECT_LOCK(new_object);
129563f6cefcSAlan Cox 	VM_OBJECT_LOCK(orig_object);
1296c5aaa06dSAlan Cox 	source = orig_object->backing_object;
1297c5aaa06dSAlan Cox 	if (source != NULL) {
12988e3a76fbSAlan Cox 		VM_OBJECT_LOCK(source);
129919c244d0SAlan Cox 		if ((source->flags & OBJ_DEAD) != 0) {
130019c244d0SAlan Cox 			VM_OBJECT_UNLOCK(source);
130119c244d0SAlan Cox 			VM_OBJECT_UNLOCK(orig_object);
130219c244d0SAlan Cox 			VM_OBJECT_UNLOCK(new_object);
130319c244d0SAlan Cox 			vm_object_deallocate(new_object);
130419c244d0SAlan Cox 			VM_OBJECT_LOCK(orig_object);
130519c244d0SAlan Cox 			return;
130619c244d0SAlan Cox 		}
13071c500307SAlan Cox 		LIST_INSERT_HEAD(&source->shadow_head,
1308c5aaa06dSAlan Cox 				  new_object, shadow_list);
13098e3a76fbSAlan Cox 		source->shadow_count++;
13108e3a76fbSAlan Cox 		source->generation++;
1311b921a12bSAlan Cox 		vm_object_reference_locked(source);	/* for new_object */
1312c5aaa06dSAlan Cox 		vm_object_clear_flag(source, OBJ_ONEMAPPING);
1313e2479b4fSAlan Cox 		VM_OBJECT_UNLOCK(source);
1314c5aaa06dSAlan Cox 		new_object->backing_object_offset =
13154da9f125SAlan Cox 			orig_object->backing_object_offset + entry->offset;
1316c5aaa06dSAlan Cox 		new_object->backing_object = source;
1317c5aaa06dSAlan Cox 	}
1318b8a0b997SJeff Roberson 	new_object->flags |= orig_object->flags & OBJ_NEEDGIANT;
1319c5aaa06dSAlan Cox retry:
132073000556SAlan Cox 	if ((m = TAILQ_FIRST(&orig_object->memq)) != NULL) {
132173000556SAlan Cox 		if (m->pindex < offidxstart) {
132273000556SAlan Cox 			m = vm_page_splay(offidxstart, orig_object->root);
132373000556SAlan Cox 			if ((orig_object->root = m)->pindex < offidxstart)
132473000556SAlan Cox 				m = TAILQ_NEXT(m, listq);
132573000556SAlan Cox 		}
132673000556SAlan Cox 	}
132773000556SAlan Cox 	vm_page_lock_queues();
132873000556SAlan Cox 	for (; m != NULL && (idx = m->pindex - offidxstart) < size;
132973000556SAlan Cox 	    m = m_next) {
133073000556SAlan Cox 		m_next = TAILQ_NEXT(m, listq);
1331c5aaa06dSAlan Cox 
1332c5aaa06dSAlan Cox 		/*
1333c5aaa06dSAlan Cox 		 * We must wait for pending I/O to complete before we can
1334c5aaa06dSAlan Cox 		 * rename the page.
1335c5aaa06dSAlan Cox 		 *
1336c5aaa06dSAlan Cox 		 * We do not have to VM_PROT_NONE the page as mappings should
1337c5aaa06dSAlan Cox 		 * not be changed by this operation.
1338c5aaa06dSAlan Cox 		 */
13399af80719SAlan Cox 		if ((m->oflags & VPO_BUSY) || m->busy) {
13405786be7cSAlan Cox 			vm_page_flag_set(m, PG_REFERENCED);
134191449ce9SAlan Cox 			vm_page_unlock_queues();
1342de33beddSAlan Cox 			VM_OBJECT_UNLOCK(new_object);
13435786be7cSAlan Cox 			m->oflags |= VPO_WANTED;
1344c5474b8fSAlan Cox 			msleep(m, VM_OBJECT_MTX(orig_object), PVM, "spltwt", 0);
1345de33beddSAlan Cox 			VM_OBJECT_LOCK(new_object);
1346c5aaa06dSAlan Cox 			goto retry;
1347de33beddSAlan Cox 		}
1348c5aaa06dSAlan Cox 		vm_page_rename(m, new_object, idx);
1349c5aaa06dSAlan Cox 		/* page automatically made dirty by rename and cache handled */
1350c5aaa06dSAlan Cox 		vm_page_busy(m);
1351c5aaa06dSAlan Cox 	}
13525ba514bcSAlan Cox 	vm_page_unlock_queues();
1353d7a013c3SAlan Cox 	if (orig_object->type == OBJT_SWAP) {
1354c5aaa06dSAlan Cox 		/*
1355c7c8dd7eSAlan Cox 		 * swap_pager_copy() can sleep, in which case the orig_object's
1356c7c8dd7eSAlan Cox 		 * and new_object's locks are released and reacquired.
1357c5aaa06dSAlan Cox 		 */
1358c5aaa06dSAlan Cox 		swap_pager_copy(orig_object, new_object, offidxstart, 0);
1359c5aaa06dSAlan Cox 	}
1360d7a013c3SAlan Cox 	VM_OBJECT_UNLOCK(orig_object);
1361c7118ed6SAlan Cox 	TAILQ_FOREACH(m, &new_object->memq, listq)
1362c5aaa06dSAlan Cox 		vm_page_wakeup(m);
1363c7c8dd7eSAlan Cox 	VM_OBJECT_UNLOCK(new_object);
1364c5aaa06dSAlan Cox 	entry->object.vm_object = new_object;
1365c5aaa06dSAlan Cox 	entry->offset = 0LL;
1366c5aaa06dSAlan Cox 	vm_object_deallocate(orig_object);
13674da9f125SAlan Cox 	VM_OBJECT_LOCK(new_object);
1368c5aaa06dSAlan Cox }
1369c5aaa06dSAlan Cox 
13702ad1a3f7SMatthew Dillon #define	OBSC_TEST_ALL_SHADOWED	0x0001
13712ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_NOWAIT	0x0002
13722ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_WAIT	0x0004
13732ad1a3f7SMatthew Dillon 
1374b4ae4780SPoul-Henning Kamp static int
13752ad1a3f7SMatthew Dillon vm_object_backing_scan(vm_object_t object, int op)
13762ad1a3f7SMatthew Dillon {
13772ad1a3f7SMatthew Dillon 	int r = 1;
13782ad1a3f7SMatthew Dillon 	vm_page_t p;
13792ad1a3f7SMatthew Dillon 	vm_object_t backing_object;
13802ad1a3f7SMatthew Dillon 	vm_pindex_t backing_offset_index;
13812ad1a3f7SMatthew Dillon 
13827ca33ad1SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
13837ca33ad1SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object->backing_object, MA_OWNED);
13842ad1a3f7SMatthew Dillon 
13852ad1a3f7SMatthew Dillon 	backing_object = object->backing_object;
13862ad1a3f7SMatthew Dillon 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
13872ad1a3f7SMatthew Dillon 
13882ad1a3f7SMatthew Dillon 	/*
13892ad1a3f7SMatthew Dillon 	 * Initial conditions
13902ad1a3f7SMatthew Dillon 	 */
13912ad1a3f7SMatthew Dillon 	if (op & OBSC_TEST_ALL_SHADOWED) {
13922ad1a3f7SMatthew Dillon 		/*
1393956f3135SPhilippe Charnier 		 * We do not want to have to test for the existence of
13942ad1a3f7SMatthew Dillon 		 * swap pages in the backing object.  XXX but with the
13952ad1a3f7SMatthew Dillon 		 * new swapper this would be pretty easy to do.
13962ad1a3f7SMatthew Dillon 		 *
13972ad1a3f7SMatthew Dillon 		 * XXX what about anonymous MAP_SHARED memory that hasn't
13982ad1a3f7SMatthew Dillon 		 * been ZFOD faulted yet?  If we do not test for this, the
13992ad1a3f7SMatthew Dillon 		 * shadow test may succeed! XXX
14002ad1a3f7SMatthew Dillon 		 */
14012ad1a3f7SMatthew Dillon 		if (backing_object->type != OBJT_DEFAULT) {
14022ad1a3f7SMatthew Dillon 			return (0);
14032ad1a3f7SMatthew Dillon 		}
14042ad1a3f7SMatthew Dillon 	}
14052ad1a3f7SMatthew Dillon 	if (op & OBSC_COLLAPSE_WAIT) {
14062ad1a3f7SMatthew Dillon 		vm_object_set_flag(backing_object, OBJ_DEAD);
14072ad1a3f7SMatthew Dillon 	}
14082ad1a3f7SMatthew Dillon 
14092ad1a3f7SMatthew Dillon 	/*
14102ad1a3f7SMatthew Dillon 	 * Our scan
14112ad1a3f7SMatthew Dillon 	 */
14122ad1a3f7SMatthew Dillon 	p = TAILQ_FIRST(&backing_object->memq);
14132ad1a3f7SMatthew Dillon 	while (p) {
14142ad1a3f7SMatthew Dillon 		vm_page_t next = TAILQ_NEXT(p, listq);
14152ad1a3f7SMatthew Dillon 		vm_pindex_t new_pindex = p->pindex - backing_offset_index;
14162ad1a3f7SMatthew Dillon 
14172ad1a3f7SMatthew Dillon 		if (op & OBSC_TEST_ALL_SHADOWED) {
14182ad1a3f7SMatthew Dillon 			vm_page_t pp;
14192ad1a3f7SMatthew Dillon 
14202ad1a3f7SMatthew Dillon 			/*
14212ad1a3f7SMatthew Dillon 			 * Ignore pages outside the parent object's range
14222ad1a3f7SMatthew Dillon 			 * and outside the parent object's mapping of the
14232ad1a3f7SMatthew Dillon 			 * backing object.
14242ad1a3f7SMatthew Dillon 			 *
14252ad1a3f7SMatthew Dillon 			 * note that we do not busy the backing object's
14262ad1a3f7SMatthew Dillon 			 * page.
14272ad1a3f7SMatthew Dillon 			 */
14282ad1a3f7SMatthew Dillon 			if (
14292ad1a3f7SMatthew Dillon 			    p->pindex < backing_offset_index ||
14302ad1a3f7SMatthew Dillon 			    new_pindex >= object->size
14312ad1a3f7SMatthew Dillon 			) {
14322ad1a3f7SMatthew Dillon 				p = next;
14332ad1a3f7SMatthew Dillon 				continue;
14342ad1a3f7SMatthew Dillon 			}
14352ad1a3f7SMatthew Dillon 
14362ad1a3f7SMatthew Dillon 			/*
14372ad1a3f7SMatthew Dillon 			 * See if the parent has the page or if the parent's
14382ad1a3f7SMatthew Dillon 			 * object pager has the page.  If the parent has the
14392ad1a3f7SMatthew Dillon 			 * page but the page is not valid, the parent's
14402ad1a3f7SMatthew Dillon 			 * object pager must have the page.
14412ad1a3f7SMatthew Dillon 			 *
14422ad1a3f7SMatthew Dillon 			 * If this fails, the parent does not completely shadow
14432ad1a3f7SMatthew Dillon 			 * the object and we might as well give up now.
14442ad1a3f7SMatthew Dillon 			 */
14452ad1a3f7SMatthew Dillon 
14462ad1a3f7SMatthew Dillon 			pp = vm_page_lookup(object, new_pindex);
14472ad1a3f7SMatthew Dillon 			if (
14482ad1a3f7SMatthew Dillon 			    (pp == NULL || pp->valid == 0) &&
14492ad1a3f7SMatthew Dillon 			    !vm_pager_has_page(object, new_pindex, NULL, NULL)
14502ad1a3f7SMatthew Dillon 			) {
14512ad1a3f7SMatthew Dillon 				r = 0;
14522ad1a3f7SMatthew Dillon 				break;
14532ad1a3f7SMatthew Dillon 			}
14542ad1a3f7SMatthew Dillon 		}
14552ad1a3f7SMatthew Dillon 
14562ad1a3f7SMatthew Dillon 		/*
14572ad1a3f7SMatthew Dillon 		 * Check for busy page
14582ad1a3f7SMatthew Dillon 		 */
14592ad1a3f7SMatthew Dillon 		if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
14602ad1a3f7SMatthew Dillon 			vm_page_t pp;
14612ad1a3f7SMatthew Dillon 
14622ad1a3f7SMatthew Dillon 			if (op & OBSC_COLLAPSE_NOWAIT) {
14639af80719SAlan Cox 				if ((p->oflags & VPO_BUSY) ||
14642ad1a3f7SMatthew Dillon 				    !p->valid ||
146500f9e8b4SAlan Cox 				    p->busy) {
14662ad1a3f7SMatthew Dillon 					p = next;
14672ad1a3f7SMatthew Dillon 					continue;
14682ad1a3f7SMatthew Dillon 				}
14692ad1a3f7SMatthew Dillon 			} else if (op & OBSC_COLLAPSE_WAIT) {
14709af80719SAlan Cox 				if ((p->oflags & VPO_BUSY) || p->busy) {
1471c6ec6a7cSAlan Cox 					vm_page_lock_queues();
14725786be7cSAlan Cox 					vm_page_flag_set(p, PG_REFERENCED);
147391449ce9SAlan Cox 					vm_page_unlock_queues();
14747ca33ad1SAlan Cox 					VM_OBJECT_UNLOCK(object);
14755786be7cSAlan Cox 					p->oflags |= VPO_WANTED;
147691449ce9SAlan Cox 					msleep(p, VM_OBJECT_MTX(backing_object),
14777ca33ad1SAlan Cox 					    PDROP | PVM, "vmocol", 0);
14787ca33ad1SAlan Cox 					VM_OBJECT_LOCK(object);
14797ca33ad1SAlan Cox 					VM_OBJECT_LOCK(backing_object);
14802ad1a3f7SMatthew Dillon 					/*
14812ad1a3f7SMatthew Dillon 					 * If we slept, anything could have
14822ad1a3f7SMatthew Dillon 					 * happened.  Since the object is
14832ad1a3f7SMatthew Dillon 					 * marked dead, the backing offset
14842ad1a3f7SMatthew Dillon 					 * should not have changed so we
14852ad1a3f7SMatthew Dillon 					 * just restart our scan.
14862ad1a3f7SMatthew Dillon 					 */
14872ad1a3f7SMatthew Dillon 					p = TAILQ_FIRST(&backing_object->memq);
14882ad1a3f7SMatthew Dillon 					continue;
14892ad1a3f7SMatthew Dillon 				}
14902ad1a3f7SMatthew Dillon 			}
14912ad1a3f7SMatthew Dillon 
14922ad1a3f7SMatthew Dillon 			KASSERT(
14932ad1a3f7SMatthew Dillon 			    p->object == backing_object,
14948e99783bSAlan Cox 			    ("vm_object_backing_scan: object mismatch")
14952ad1a3f7SMatthew Dillon 			);
14962ad1a3f7SMatthew Dillon 
14972ad1a3f7SMatthew Dillon 			/*
14982ad1a3f7SMatthew Dillon 			 * Destroy any associated swap
14992ad1a3f7SMatthew Dillon 			 */
15002ad1a3f7SMatthew Dillon 			if (backing_object->type == OBJT_SWAP) {
15012ad1a3f7SMatthew Dillon 				swap_pager_freespace(
15022ad1a3f7SMatthew Dillon 				    backing_object,
15032ad1a3f7SMatthew Dillon 				    p->pindex,
15042ad1a3f7SMatthew Dillon 				    1
15052ad1a3f7SMatthew Dillon 				);
15062ad1a3f7SMatthew Dillon 			}
15072ad1a3f7SMatthew Dillon 
15082ad1a3f7SMatthew Dillon 			if (
15092ad1a3f7SMatthew Dillon 			    p->pindex < backing_offset_index ||
15102ad1a3f7SMatthew Dillon 			    new_pindex >= object->size
15112ad1a3f7SMatthew Dillon 			) {
15122ad1a3f7SMatthew Dillon 				/*
15132ad1a3f7SMatthew Dillon 				 * Page is out of the parent object's range, we
15142ad1a3f7SMatthew Dillon 				 * can simply destroy it.
15152ad1a3f7SMatthew Dillon 				 */
15166a684ecfSAlan Cox 				vm_page_lock_queues();
1517f6d89838SAlan Cox 				KASSERT(!pmap_page_is_mapped(p),
1518f6d89838SAlan Cox 				    ("freeing mapped page %p", p));
1519f6d89838SAlan Cox 				if (p->wire_count == 0)
15202ad1a3f7SMatthew Dillon 					vm_page_free(p);
1521f6d89838SAlan Cox 				else
1522f6d89838SAlan Cox 					vm_page_remove(p);
15236a684ecfSAlan Cox 				vm_page_unlock_queues();
15242ad1a3f7SMatthew Dillon 				p = next;
15252ad1a3f7SMatthew Dillon 				continue;
15262ad1a3f7SMatthew Dillon 			}
15272ad1a3f7SMatthew Dillon 
15282ad1a3f7SMatthew Dillon 			pp = vm_page_lookup(object, new_pindex);
15292ad1a3f7SMatthew Dillon 			if (
15302ad1a3f7SMatthew Dillon 			    pp != NULL ||
15312ad1a3f7SMatthew Dillon 			    vm_pager_has_page(object, new_pindex, NULL, NULL)
15322ad1a3f7SMatthew Dillon 			) {
15332ad1a3f7SMatthew Dillon 				/*
15342ad1a3f7SMatthew Dillon 				 * page already exists in parent OR swap exists
15352ad1a3f7SMatthew Dillon 				 * for this location in the parent.  Destroy
15362ad1a3f7SMatthew Dillon 				 * the original page from the backing object.
15372ad1a3f7SMatthew Dillon 				 *
15382ad1a3f7SMatthew Dillon 				 * Leave the parent's page alone
15392ad1a3f7SMatthew Dillon 				 */
15406a684ecfSAlan Cox 				vm_page_lock_queues();
1541f6d89838SAlan Cox 				KASSERT(!pmap_page_is_mapped(p),
1542f6d89838SAlan Cox 				    ("freeing mapped page %p", p));
1543f6d89838SAlan Cox 				if (p->wire_count == 0)
15442ad1a3f7SMatthew Dillon 					vm_page_free(p);
1545f6d89838SAlan Cox 				else
1546f6d89838SAlan Cox 					vm_page_remove(p);
15476a684ecfSAlan Cox 				vm_page_unlock_queues();
15482ad1a3f7SMatthew Dillon 				p = next;
15492ad1a3f7SMatthew Dillon 				continue;
15502ad1a3f7SMatthew Dillon 			}
15512ad1a3f7SMatthew Dillon 
15522ad1a3f7SMatthew Dillon 			/*
15532ad1a3f7SMatthew Dillon 			 * Page does not exist in parent, rename the
15542ad1a3f7SMatthew Dillon 			 * page from the backing object to the main object.
1555d1bf5d56SMatthew Dillon 			 *
1556d1bf5d56SMatthew Dillon 			 * If the page was mapped to a process, it can remain
1557d1bf5d56SMatthew Dillon 			 * mapped through the rename.
15582ad1a3f7SMatthew Dillon 			 */
1559a28cc55eSAlan Cox 			vm_page_lock_queues();
15602ad1a3f7SMatthew Dillon 			vm_page_rename(p, object, new_pindex);
1561a28cc55eSAlan Cox 			vm_page_unlock_queues();
15622ad1a3f7SMatthew Dillon 			/* page automatically made dirty by rename */
15632ad1a3f7SMatthew Dillon 		}
15642ad1a3f7SMatthew Dillon 		p = next;
15652ad1a3f7SMatthew Dillon 	}
15662ad1a3f7SMatthew Dillon 	return (r);
15672ad1a3f7SMatthew Dillon }
15682ad1a3f7SMatthew Dillon 
1569df8bae1dSRodney W. Grimes 
1570df8bae1dSRodney W. Grimes /*
15712fe6e4d7SDavid Greenman  * this version of collapse allows the operation to occur earlier and
15722fe6e4d7SDavid Greenman  * when paging_in_progress is true for an object...  This is not a complete
15732fe6e4d7SDavid Greenman  * operation, but should plug 99.9% of the rest of the leaks.
15742fe6e4d7SDavid Greenman  */
15752fe6e4d7SDavid Greenman static void
15761b40f8c0SMatthew Dillon vm_object_qcollapse(vm_object_t object)
15772fe6e4d7SDavid Greenman {
15782ad1a3f7SMatthew Dillon 	vm_object_t backing_object = object->backing_object;
15792fe6e4d7SDavid Greenman 
158006ecade7SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
158106ecade7SAlan Cox 	VM_OBJECT_LOCK_ASSERT(backing_object, MA_OWNED);
15821b40f8c0SMatthew Dillon 
15832fe6e4d7SDavid Greenman 	if (backing_object->ref_count != 1)
15842fe6e4d7SDavid Greenman 		return;
15852fe6e4d7SDavid Greenman 
15862ad1a3f7SMatthew Dillon 	vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
15872fe6e4d7SDavid Greenman }
15882fe6e4d7SDavid Greenman 
1589df8bae1dSRodney W. Grimes /*
1590df8bae1dSRodney W. Grimes  *	vm_object_collapse:
1591df8bae1dSRodney W. Grimes  *
1592df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
1593df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
1594df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
1595df8bae1dSRodney W. Grimes  */
159626f9a767SRodney W. Grimes void
15971b40f8c0SMatthew Dillon vm_object_collapse(vm_object_t object)
1598df8bae1dSRodney W. Grimes {
1599d7fc2210SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
160023955314SAlfred Perlstein 
1601df8bae1dSRodney W. Grimes 	while (TRUE) {
16022ad1a3f7SMatthew Dillon 		vm_object_t backing_object;
16032ad1a3f7SMatthew Dillon 
1604df8bae1dSRodney W. Grimes 		/*
1605df8bae1dSRodney W. Grimes 		 * Verify that the conditions are right for collapse:
1606df8bae1dSRodney W. Grimes 		 *
16072ad1a3f7SMatthew Dillon 		 * The object exists and the backing object exists.
1608df8bae1dSRodney W. Grimes 		 */
160924a1cce3SDavid Greenman 		if ((backing_object = object->backing_object) == NULL)
16102ad1a3f7SMatthew Dillon 			break;
1611df8bae1dSRodney W. Grimes 
1612f919ebdeSDavid Greenman 		/*
1613f919ebdeSDavid Greenman 		 * we check the backing object first, because it is most likely
161424a1cce3SDavid Greenman 		 * not collapsable.
1615f919ebdeSDavid Greenman 		 */
161640b808a8SAlan Cox 		VM_OBJECT_LOCK(backing_object);
161724a1cce3SDavid Greenman 		if (backing_object->handle != NULL ||
161824a1cce3SDavid Greenman 		    (backing_object->type != OBJT_DEFAULT &&
161924a1cce3SDavid Greenman 		     backing_object->type != OBJT_SWAP) ||
1620f919ebdeSDavid Greenman 		    (backing_object->flags & OBJ_DEAD) ||
162124a1cce3SDavid Greenman 		    object->handle != NULL ||
162224a1cce3SDavid Greenman 		    (object->type != OBJT_DEFAULT &&
162324a1cce3SDavid Greenman 		     object->type != OBJT_SWAP) ||
162424a1cce3SDavid Greenman 		    (object->flags & OBJ_DEAD)) {
162540b808a8SAlan Cox 			VM_OBJECT_UNLOCK(backing_object);
16262ad1a3f7SMatthew Dillon 			break;
162724a1cce3SDavid Greenman 		}
16289b4814bbSDavid Greenman 
16292ad1a3f7SMatthew Dillon 		if (
16302ad1a3f7SMatthew Dillon 		    object->paging_in_progress != 0 ||
16312ad1a3f7SMatthew Dillon 		    backing_object->paging_in_progress != 0
16322ad1a3f7SMatthew Dillon 		) {
1633b9921222SDavid Greenman 			vm_object_qcollapse(object);
163440b808a8SAlan Cox 			VM_OBJECT_UNLOCK(backing_object);
16352ad1a3f7SMatthew Dillon 			break;
1636df8bae1dSRodney W. Grimes 		}
163726f9a767SRodney W. Grimes 		/*
16380d94caffSDavid Greenman 		 * We know that we can either collapse the backing object (if
16392ad1a3f7SMatthew Dillon 		 * the parent is the only reference to it) or (perhaps) have
16402ad1a3f7SMatthew Dillon 		 * the parent bypass the object if the parent happens to shadow
16412ad1a3f7SMatthew Dillon 		 * all the resident pages in the entire backing object.
16422ad1a3f7SMatthew Dillon 		 *
16432ad1a3f7SMatthew Dillon 		 * This is ignoring pager-backed pages such as swap pages.
16442ad1a3f7SMatthew Dillon 		 * vm_object_backing_scan fails the shadowing test in this
16452ad1a3f7SMatthew Dillon 		 * case.
1646df8bae1dSRodney W. Grimes 		 */
1647df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
1648df8bae1dSRodney W. Grimes 			/*
16492ad1a3f7SMatthew Dillon 			 * If there is exactly one reference to the backing
16502ad1a3f7SMatthew Dillon 			 * object, we can collapse it into the parent.
1651df8bae1dSRodney W. Grimes 			 */
16522ad1a3f7SMatthew Dillon 			vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1653df8bae1dSRodney W. Grimes 
1654df8bae1dSRodney W. Grimes 			/*
1655df8bae1dSRodney W. Grimes 			 * Move the pager from backing_object to object.
1656df8bae1dSRodney W. Grimes 			 */
16576be36525SAlan Cox 			if (backing_object->type == OBJT_SWAP) {
165824a1cce3SDavid Greenman 				/*
1659c7c8dd7eSAlan Cox 				 * swap_pager_copy() can sleep, in which case
1660c7c8dd7eSAlan Cox 				 * the backing_object's and object's locks are
1661c7c8dd7eSAlan Cox 				 * released and reacquired.
166224a1cce3SDavid Greenman 				 */
16631c7c3c6aSMatthew Dillon 				swap_pager_copy(
16641c7c3c6aSMatthew Dillon 				    backing_object,
16651c7c3c6aSMatthew Dillon 				    object,
16661c7c3c6aSMatthew Dillon 				    OFF_TO_IDX(object->backing_object_offset), TRUE);
1667c0503609SDavid Greenman 			}
1668df8bae1dSRodney W. Grimes 			/*
1669df8bae1dSRodney W. Grimes 			 * Object now shadows whatever backing_object did.
16702ad1a3f7SMatthew Dillon 			 * Note that the reference to
16712ad1a3f7SMatthew Dillon 			 * backing_object->backing_object moves from within
16722ad1a3f7SMatthew Dillon 			 * backing_object to within object.
1673df8bae1dSRodney W. Grimes 			 */
16741c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
16754f7c7f6eSAlan Cox 			backing_object->shadow_count--;
16764f7c7f6eSAlan Cox 			backing_object->generation++;
1677de5f6a77SJohn Dyson 			if (backing_object->backing_object) {
16786be36525SAlan Cox 				VM_OBJECT_LOCK(backing_object->backing_object);
16791c500307SAlan Cox 				LIST_REMOVE(backing_object, shadow_list);
168043186e53SAlan Cox 				LIST_INSERT_HEAD(
168143186e53SAlan Cox 				    &backing_object->backing_object->shadow_head,
168243186e53SAlan Cox 				    object, shadow_list);
168343186e53SAlan Cox 				/*
168443186e53SAlan Cox 				 * The shadow_count has not changed.
168543186e53SAlan Cox 				 */
1686eaf13dd7SJohn Dyson 				backing_object->backing_object->generation++;
16876be36525SAlan Cox 				VM_OBJECT_UNLOCK(backing_object->backing_object);
1688de5f6a77SJohn Dyson 			}
168924a1cce3SDavid Greenman 			object->backing_object = backing_object->backing_object;
16902ad1a3f7SMatthew Dillon 			object->backing_object_offset +=
16912ad1a3f7SMatthew Dillon 			    backing_object->backing_object_offset;
16922ad1a3f7SMatthew Dillon 
1693df8bae1dSRodney W. Grimes 			/*
1694df8bae1dSRodney W. Grimes 			 * Discard backing_object.
1695df8bae1dSRodney W. Grimes 			 *
16960d94caffSDavid Greenman 			 * Since the backing object has no pages, no pager left,
16970d94caffSDavid Greenman 			 * and no object references within it, all that is
16980d94caffSDavid Greenman 			 * necessary is to dispose of it.
1699df8bae1dSRodney W. Grimes 			 */
1700245df27cSMatthew Dillon 			KASSERT(backing_object->ref_count == 1, ("backing_object %p was somehow re-referenced during collapse!", backing_object));
17016be36525SAlan Cox 			VM_OBJECT_UNLOCK(backing_object);
1702df8bae1dSRodney W. Grimes 
1703a5698387SAlan Cox 			mtx_lock(&vm_object_list_mtx);
17042ad1a3f7SMatthew Dillon 			TAILQ_REMOVE(
17052ad1a3f7SMatthew Dillon 			    &vm_object_list,
17062ad1a3f7SMatthew Dillon 			    backing_object,
17072ad1a3f7SMatthew Dillon 			    object_list
17082ad1a3f7SMatthew Dillon 			);
1709a5698387SAlan Cox 			mtx_unlock(&vm_object_list_mtx);
1710df8bae1dSRodney W. Grimes 
1711670d17b5SJeff Roberson 			uma_zfree(obj_zone, backing_object);
1712df8bae1dSRodney W. Grimes 
1713df8bae1dSRodney W. Grimes 			object_collapses++;
17140d94caffSDavid Greenman 		} else {
171595e5e988SJohn Dyson 			vm_object_t new_backing_object;
1716df8bae1dSRodney W. Grimes 
1717df8bae1dSRodney W. Grimes 			/*
17182ad1a3f7SMatthew Dillon 			 * If we do not entirely shadow the backing object,
17192ad1a3f7SMatthew Dillon 			 * there is nothing we can do so we give up.
1720df8bae1dSRodney W. Grimes 			 */
1721df59a0feSJeff Roberson 			if (object->resident_page_count != object->size &&
1722df59a0feSJeff Roberson 			    vm_object_backing_scan(object,
1723df59a0feSJeff Roberson 			    OBSC_TEST_ALL_SHADOWED) == 0) {
172440b808a8SAlan Cox 				VM_OBJECT_UNLOCK(backing_object);
17252ad1a3f7SMatthew Dillon 				break;
172624a1cce3SDavid Greenman 			}
1727df8bae1dSRodney W. Grimes 
1728df8bae1dSRodney W. Grimes 			/*
17290d94caffSDavid Greenman 			 * Make the parent shadow the next object in the
17300d94caffSDavid Greenman 			 * chain.  Deallocating backing_object will not remove
17310d94caffSDavid Greenman 			 * it, since its reference count is at least 2.
1732df8bae1dSRodney W. Grimes 			 */
17331c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
1734eaf13dd7SJohn Dyson 			backing_object->shadow_count--;
1735eaf13dd7SJohn Dyson 			backing_object->generation++;
173695e5e988SJohn Dyson 
173795e5e988SJohn Dyson 			new_backing_object = backing_object->backing_object;
17388aef1712SMatthew Dillon 			if ((object->backing_object = new_backing_object) != NULL) {
17396be36525SAlan Cox 				VM_OBJECT_LOCK(new_backing_object);
17401c500307SAlan Cox 				LIST_INSERT_HEAD(
17412ad1a3f7SMatthew Dillon 				    &new_backing_object->shadow_head,
17422ad1a3f7SMatthew Dillon 				    object,
17432ad1a3f7SMatthew Dillon 				    shadow_list
17442ad1a3f7SMatthew Dillon 				);
1745eaf13dd7SJohn Dyson 				new_backing_object->shadow_count++;
1746eaf13dd7SJohn Dyson 				new_backing_object->generation++;
1747b921a12bSAlan Cox 				vm_object_reference_locked(new_backing_object);
17486be36525SAlan Cox 				VM_OBJECT_UNLOCK(new_backing_object);
174995e5e988SJohn Dyson 				object->backing_object_offset +=
175095e5e988SJohn Dyson 					backing_object->backing_object_offset;
1751de5f6a77SJohn Dyson 			}
1752df8bae1dSRodney W. Grimes 
1753df8bae1dSRodney W. Grimes 			/*
17540d94caffSDavid Greenman 			 * Drop the reference count on backing_object. Since
175522ec553fSAlan Cox 			 * its ref_count was at least 2, it will not vanish.
1756df8bae1dSRodney W. Grimes 			 */
175722ec553fSAlan Cox 			backing_object->ref_count--;
175822ec553fSAlan Cox 			VM_OBJECT_UNLOCK(backing_object);
1759df8bae1dSRodney W. Grimes 			object_bypasses++;
1760df8bae1dSRodney W. Grimes 		}
1761df8bae1dSRodney W. Grimes 
1762df8bae1dSRodney W. Grimes 		/*
1763df8bae1dSRodney W. Grimes 		 * Try again with this object's new backing object.
1764df8bae1dSRodney W. Grimes 		 */
1765df8bae1dSRodney W. Grimes 	}
1766df8bae1dSRodney W. Grimes }
1767df8bae1dSRodney W. Grimes 
1768df8bae1dSRodney W. Grimes /*
1769bff99f0dSAlan Cox  *	vm_object_page_remove:
1770df8bae1dSRodney W. Grimes  *
1771bff99f0dSAlan Cox  *	Removes all physical pages in the given range from the
1772bff99f0dSAlan Cox  *	object's list of pages.  If the range's end is zero, all
1773bff99f0dSAlan Cox  *	physical pages from the range's start to the end of the object
1774bff99f0dSAlan Cox  *	are deleted.
1775df8bae1dSRodney W. Grimes  *
1776df8bae1dSRodney W. Grimes  *	The object must be locked.
1777df8bae1dSRodney W. Grimes  */
177826f9a767SRodney W. Grimes void
1779ecde4b32SAlan Cox vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
1780ecde4b32SAlan Cox     boolean_t clean_only)
1781df8bae1dSRodney W. Grimes {
1782d031cff1SMatthew Dillon 	vm_page_t p, next;
1783df8bae1dSRodney W. Grimes 
1784ecde4b32SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1785ecde4b32SAlan Cox 	if (object->resident_page_count == 0)
1786df8bae1dSRodney W. Grimes 		return;
178795e5e988SJohn Dyson 
17888b03c8edSMatthew Dillon 	/*
17898b03c8edSMatthew Dillon 	 * Since physically-backed objects do not use managed pages, we can't
17908b03c8edSMatthew Dillon 	 * remove pages from the object (we must instead remove the page
17918b03c8edSMatthew Dillon 	 * references, and then destroy the object).
17928b03c8edSMatthew Dillon 	 */
17939f5c801bSAlan Cox 	KASSERT(object->type != OBJT_PHYS || object == kernel_object ||
17949f5c801bSAlan Cox 	    object == kmem_object,
1795ecde4b32SAlan Cox 	    ("attempt to remove pages from a physical object"));
17968b03c8edSMatthew Dillon 
1797d474eaaaSDoug Rabson 	vm_object_pip_add(object, 1);
179826f9a767SRodney W. Grimes again:
179932585dd6SAlan Cox 	vm_page_lock_queues();
180075741c04SAlan Cox 	if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
180175741c04SAlan Cox 		if (p->pindex < start) {
180275741c04SAlan Cox 			p = vm_page_splay(start, object->root);
180375741c04SAlan Cox 			if ((object->root = p)->pindex < start)
180475741c04SAlan Cox 				p = TAILQ_NEXT(p, listq);
180575741c04SAlan Cox 		}
180675741c04SAlan Cox 	}
180775741c04SAlan Cox 	/*
180875741c04SAlan Cox 	 * Assert: the variable p is either (1) the page with the
180975741c04SAlan Cox 	 * least pindex greater than or equal to the parameter pindex
181075741c04SAlan Cox 	 * or (2) NULL.
181175741c04SAlan Cox 	 */
181275741c04SAlan Cox 	for (;
1813bff99f0dSAlan Cox 	     p != NULL && (p->pindex < end || end == 0);
181475741c04SAlan Cox 	     p = next) {
1815b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
181675741c04SAlan Cox 
1817bd7e5f99SJohn Dyson 		if (p->wire_count != 0) {
18184fec79beSAlan Cox 			pmap_remove_all(p);
1819ce65e68cSDavid Greenman 			if (!clean_only)
1820bd7e5f99SJohn Dyson 				p->valid = 0;
18210d94caffSDavid Greenman 			continue;
18220d94caffSDavid Greenman 		}
182332585dd6SAlan Cox 		if (vm_page_sleep_if_busy(p, TRUE, "vmopar"))
182426f9a767SRodney W. Grimes 			goto again;
18258f9110f6SJohn Dyson 		if (clean_only && p->valid) {
182678985e42SAlan Cox 			pmap_remove_write(p);
18277c1f6cedSDavid Greenman 			if (p->valid & p->dirty)
18287c1f6cedSDavid Greenman 				continue;
18297c1f6cedSDavid Greenman 		}
18304fec79beSAlan Cox 		pmap_remove_all(p);
1831df8bae1dSRodney W. Grimes 		vm_page_free(p);
183226f9a767SRodney W. Grimes 	}
183332585dd6SAlan Cox 	vm_page_unlock_queues();
1834f919ebdeSDavid Greenman 	vm_object_pip_wakeup(object);
1835c0503609SDavid Greenman }
1836df8bae1dSRodney W. Grimes 
1837df8bae1dSRodney W. Grimes /*
1838df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
1839df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
1840df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
1841df8bae1dSRodney W. Grimes  *
1842df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
1843df8bae1dSRodney W. Grimes  *
1844df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
1845df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
1846df8bae1dSRodney W. Grimes  *
1847df8bae1dSRodney W. Grimes  *	Parameters:
1848df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
1849df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
1850df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
185157a21abaSAlan Cox  *		next_size	Size of reference to the second object
1852df8bae1dSRodney W. Grimes  *
1853df8bae1dSRodney W. Grimes  *	Conditions:
1854df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
1855df8bae1dSRodney W. Grimes  */
18560d94caffSDavid Greenman boolean_t
185757a21abaSAlan Cox vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset,
185800e1854aSAlan Cox 	vm_size_t prev_size, vm_size_t next_size)
1859df8bae1dSRodney W. Grimes {
1860ea41812fSAlan Cox 	vm_pindex_t next_pindex;
1861df8bae1dSRodney W. Grimes 
186200e1854aSAlan Cox 	if (prev_object == NULL)
1863df8bae1dSRodney W. Grimes 		return (TRUE);
1864bdbfbaafSAlan Cox 	VM_OBJECT_LOCK(prev_object);
18654112823fSMatthew Dillon 	if (prev_object->type != OBJT_DEFAULT &&
18664112823fSMatthew Dillon 	    prev_object->type != OBJT_SWAP) {
1867bdbfbaafSAlan Cox 		VM_OBJECT_UNLOCK(prev_object);
186830dcfc09SJohn Dyson 		return (FALSE);
186930dcfc09SJohn Dyson 	}
187030dcfc09SJohn Dyson 
1871df8bae1dSRodney W. Grimes 	/*
1872df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
1873df8bae1dSRodney W. Grimes 	 */
1874df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
1875df8bae1dSRodney W. Grimes 
1876df8bae1dSRodney W. Grimes 	/*
18770d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
18780d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
18790d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
1880df8bae1dSRodney W. Grimes 	 */
18818cc7e047SJohn Dyson 	if (prev_object->backing_object != NULL) {
1882bdbfbaafSAlan Cox 		VM_OBJECT_UNLOCK(prev_object);
1883df8bae1dSRodney W. Grimes 		return (FALSE);
1884df8bae1dSRodney W. Grimes 	}
1885a316d390SJohn Dyson 
1886a316d390SJohn Dyson 	prev_size >>= PAGE_SHIFT;
1887a316d390SJohn Dyson 	next_size >>= PAGE_SHIFT;
188857a21abaSAlan Cox 	next_pindex = OFF_TO_IDX(prev_offset) + prev_size;
18898cc7e047SJohn Dyson 
18908cc7e047SJohn Dyson 	if ((prev_object->ref_count > 1) &&
1891ea41812fSAlan Cox 	    (prev_object->size != next_pindex)) {
1892bdbfbaafSAlan Cox 		VM_OBJECT_UNLOCK(prev_object);
18938cc7e047SJohn Dyson 		return (FALSE);
18948cc7e047SJohn Dyson 	}
18958cc7e047SJohn Dyson 
1896df8bae1dSRodney W. Grimes 	/*
18970d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
18980d94caffSDavid Greenman 	 * deallocation.
1899df8bae1dSRodney W. Grimes 	 */
1900ea41812fSAlan Cox 	if (next_pindex < prev_object->size) {
1901df8bae1dSRodney W. Grimes 		vm_object_page_remove(prev_object,
1902ea41812fSAlan Cox 				      next_pindex,
1903ea41812fSAlan Cox 				      next_pindex + next_size, FALSE);
1904ea41812fSAlan Cox 		if (prev_object->type == OBJT_SWAP)
1905ea41812fSAlan Cox 			swap_pager_freespace(prev_object,
1906ea41812fSAlan Cox 					     next_pindex, next_size);
1907ea41812fSAlan Cox 	}
1908df8bae1dSRodney W. Grimes 
1909df8bae1dSRodney W. Grimes 	/*
1910df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
1911df8bae1dSRodney W. Grimes 	 */
1912ea41812fSAlan Cox 	if (next_pindex + next_size > prev_object->size)
1913ea41812fSAlan Cox 		prev_object->size = next_pindex + next_size;
1914df8bae1dSRodney W. Grimes 
1915bdbfbaafSAlan Cox 	VM_OBJECT_UNLOCK(prev_object);
1916df8bae1dSRodney W. Grimes 	return (TRUE);
1917df8bae1dSRodney W. Grimes }
1918df8bae1dSRodney W. Grimes 
19197a5a6352SMatthew Dillon void
19207a5a6352SMatthew Dillon vm_object_set_writeable_dirty(vm_object_t object)
19217a5a6352SMatthew Dillon {
19227a5a6352SMatthew Dillon 	struct vnode *vp;
19237a5a6352SMatthew Dillon 
1924de33beddSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1925af51d7bfSAlan Cox 	if ((object->flags & OBJ_MIGHTBEDIRTY) != 0)
1926ee39666aSJeff Roberson 		return;
1927af51d7bfSAlan Cox 	vm_object_set_flag(object, OBJ_MIGHTBEDIRTY);
19287a5a6352SMatthew Dillon 	if (object->type == OBJT_VNODE &&
19297a5a6352SMatthew Dillon 	    (vp = (struct vnode *)object->handle) != NULL) {
1930e6e370a7SJeff Roberson 		VI_LOCK(vp);
1931e6e370a7SJeff Roberson 		vp->v_iflag |= VI_OBJDIRTY;
1932e6e370a7SJeff Roberson 		VI_UNLOCK(vp);
19337a5a6352SMatthew Dillon 	}
19347a5a6352SMatthew Dillon }
19357a5a6352SMatthew Dillon 
1936c7c34a24SBruce Evans #include "opt_ddb.h"
1937c3cb3e12SDavid Greenman #ifdef DDB
1938c7c34a24SBruce Evans #include <sys/kernel.h>
1939c7c34a24SBruce Evans 
1940ce9edcf5SPoul-Henning Kamp #include <sys/cons.h>
1941c7c34a24SBruce Evans 
1942c7c34a24SBruce Evans #include <ddb/ddb.h>
1943c7c34a24SBruce Evans 
1944cac597e4SBruce Evans static int
19451b40f8c0SMatthew Dillon _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
1946a1f6d91cSDavid Greenman {
1947a1f6d91cSDavid Greenman 	vm_map_t tmpm;
1948a1f6d91cSDavid Greenman 	vm_map_entry_t tmpe;
1949a1f6d91cSDavid Greenman 	vm_object_t obj;
1950a1f6d91cSDavid Greenman 	int entcount;
1951a1f6d91cSDavid Greenman 
1952a1f6d91cSDavid Greenman 	if (map == 0)
1953a1f6d91cSDavid Greenman 		return 0;
1954a1f6d91cSDavid Greenman 
1955a1f6d91cSDavid Greenman 	if (entry == 0) {
1956a1f6d91cSDavid Greenman 		tmpe = map->header.next;
1957a1f6d91cSDavid Greenman 		entcount = map->nentries;
1958a1f6d91cSDavid Greenman 		while (entcount-- && (tmpe != &map->header)) {
1959a1f6d91cSDavid Greenman 			if (_vm_object_in_map(map, object, tmpe)) {
1960a1f6d91cSDavid Greenman 				return 1;
1961a1f6d91cSDavid Greenman 			}
1962a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1963a1f6d91cSDavid Greenman 		}
19649fdfe602SMatthew Dillon 	} else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
19659fdfe602SMatthew Dillon 		tmpm = entry->object.sub_map;
1966a1f6d91cSDavid Greenman 		tmpe = tmpm->header.next;
1967a1f6d91cSDavid Greenman 		entcount = tmpm->nentries;
1968a1f6d91cSDavid Greenman 		while (entcount-- && tmpe != &tmpm->header) {
1969a1f6d91cSDavid Greenman 			if (_vm_object_in_map(tmpm, object, tmpe)) {
1970a1f6d91cSDavid Greenman 				return 1;
1971a1f6d91cSDavid Greenman 			}
1972a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1973a1f6d91cSDavid Greenman 		}
19748aef1712SMatthew Dillon 	} else if ((obj = entry->object.vm_object) != NULL) {
197524a1cce3SDavid Greenman 		for (; obj; obj = obj->backing_object)
1976a1f6d91cSDavid Greenman 			if (obj == object) {
1977a1f6d91cSDavid Greenman 				return 1;
1978a1f6d91cSDavid Greenman 			}
1979a1f6d91cSDavid Greenman 	}
1980a1f6d91cSDavid Greenman 	return 0;
1981a1f6d91cSDavid Greenman }
1982a1f6d91cSDavid Greenman 
1983cac597e4SBruce Evans static int
19841b40f8c0SMatthew Dillon vm_object_in_map(vm_object_t object)
1985a1f6d91cSDavid Greenman {
1986a1f6d91cSDavid Greenman 	struct proc *p;
19871005a129SJohn Baldwin 
198860517fd1SJohn Baldwin 	/* sx_slock(&allproc_lock); */
1989f67af5c9SXin LI 	FOREACH_PROC_IN_SYSTEM(p) {
1990a1f6d91cSDavid Greenman 		if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1991a1f6d91cSDavid Greenman 			continue;
1992553629ebSJake Burkholder 		if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
199360517fd1SJohn Baldwin 			/* sx_sunlock(&allproc_lock); */
1994a1f6d91cSDavid Greenman 			return 1;
1995a1f6d91cSDavid Greenman 		}
1996553629ebSJake Burkholder 	}
199760517fd1SJohn Baldwin 	/* sx_sunlock(&allproc_lock); */
1998a1f6d91cSDavid Greenman 	if (_vm_object_in_map(kernel_map, object, 0))
1999a1f6d91cSDavid Greenman 		return 1;
2000a1f6d91cSDavid Greenman 	if (_vm_object_in_map(kmem_map, object, 0))
2001a1f6d91cSDavid Greenman 		return 1;
2002a1f6d91cSDavid Greenman 	if (_vm_object_in_map(pager_map, object, 0))
2003a1f6d91cSDavid Greenman 		return 1;
2004a1f6d91cSDavid Greenman 	if (_vm_object_in_map(buffer_map, object, 0))
2005a1f6d91cSDavid Greenman 		return 1;
2006a1f6d91cSDavid Greenman 	return 0;
2007a1f6d91cSDavid Greenman }
2008a1f6d91cSDavid Greenman 
2009c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check)
2010f708ef1bSPoul-Henning Kamp {
2011a1f6d91cSDavid Greenman 	vm_object_t object;
2012a1f6d91cSDavid Greenman 
2013a1f6d91cSDavid Greenman 	/*
2014a1f6d91cSDavid Greenman 	 * make sure that internal objs are in a map somewhere
2015a1f6d91cSDavid Greenman 	 * and none have zero ref counts.
2016a1f6d91cSDavid Greenman 	 */
2017cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
201824a1cce3SDavid Greenman 		if (object->handle == NULL &&
201924a1cce3SDavid Greenman 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2020a1f6d91cSDavid Greenman 			if (object->ref_count == 0) {
20213efc015bSPeter Wemm 				db_printf("vmochk: internal obj has zero ref count: %ld\n",
20223efc015bSPeter Wemm 					(long)object->size);
2023a1f6d91cSDavid Greenman 			}
2024a1f6d91cSDavid Greenman 			if (!vm_object_in_map(object)) {
2025fc62ef1fSBruce Evans 				db_printf(
2026fc62ef1fSBruce Evans 			"vmochk: internal obj is not in a map: "
2027fc62ef1fSBruce Evans 			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2028fc62ef1fSBruce Evans 				    object->ref_count, (u_long)object->size,
2029fc62ef1fSBruce Evans 				    (u_long)object->size,
2030fc62ef1fSBruce Evans 				    (void *)object->backing_object);
2031a1f6d91cSDavid Greenman 			}
2032a1f6d91cSDavid Greenman 		}
2033a1f6d91cSDavid Greenman 	}
2034a1f6d91cSDavid Greenman }
2035a1f6d91cSDavid Greenman 
203626f9a767SRodney W. Grimes /*
2037df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
2038df8bae1dSRodney W. Grimes  */
2039c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static)
2040df8bae1dSRodney W. Grimes {
2041c7c34a24SBruce Evans 	/* XXX convert args. */
2042c7c34a24SBruce Evans 	vm_object_t object = (vm_object_t)addr;
2043c7c34a24SBruce Evans 	boolean_t full = have_addr;
2044c7c34a24SBruce Evans 
2045d031cff1SMatthew Dillon 	vm_page_t p;
2046df8bae1dSRodney W. Grimes 
2047c7c34a24SBruce Evans 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
2048c7c34a24SBruce Evans #define	count	was_count
2049c7c34a24SBruce Evans 
2050d031cff1SMatthew Dillon 	int count;
2051df8bae1dSRodney W. Grimes 
2052df8bae1dSRodney W. Grimes 	if (object == NULL)
2053df8bae1dSRodney W. Grimes 		return;
2054df8bae1dSRodney W. Grimes 
2055eb95adefSBruce Evans 	db_iprintf(
2056e47cd172SMaxime Henrion 	    "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x\n",
2057e47cd172SMaxime Henrion 	    object, (int)object->type, (uintmax_t)object->size,
2058eb95adefSBruce Evans 	    object->resident_page_count, object->ref_count, object->flags);
2059e47cd172SMaxime Henrion 	db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n",
20601c7c3c6aSMatthew Dillon 	    object->shadow_count,
2061eb95adefSBruce Evans 	    object->backing_object ? object->backing_object->ref_count : 0,
2062e47cd172SMaxime Henrion 	    object->backing_object, (uintmax_t)object->backing_object_offset);
2063df8bae1dSRodney W. Grimes 
2064df8bae1dSRodney W. Grimes 	if (!full)
2065df8bae1dSRodney W. Grimes 		return;
2066df8bae1dSRodney W. Grimes 
2067c7c34a24SBruce Evans 	db_indent += 2;
2068df8bae1dSRodney W. Grimes 	count = 0;
2069fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(p, &object->memq, listq) {
2070df8bae1dSRodney W. Grimes 		if (count == 0)
2071c7c34a24SBruce Evans 			db_iprintf("memory:=");
2072df8bae1dSRodney W. Grimes 		else if (count == 6) {
2073c7c34a24SBruce Evans 			db_printf("\n");
2074c7c34a24SBruce Evans 			db_iprintf(" ...");
2075df8bae1dSRodney W. Grimes 			count = 0;
2076df8bae1dSRodney W. Grimes 		} else
2077c7c34a24SBruce Evans 			db_printf(",");
2078df8bae1dSRodney W. Grimes 		count++;
2079df8bae1dSRodney W. Grimes 
2080e47cd172SMaxime Henrion 		db_printf("(off=0x%jx,page=0x%jx)",
2081e47cd172SMaxime Henrion 		    (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p));
2082df8bae1dSRodney W. Grimes 	}
2083df8bae1dSRodney W. Grimes 	if (count != 0)
2084c7c34a24SBruce Evans 		db_printf("\n");
2085c7c34a24SBruce Evans 	db_indent -= 2;
2086df8bae1dSRodney W. Grimes }
20875070c7f8SJohn Dyson 
2088c7c34a24SBruce Evans /* XXX. */
2089c7c34a24SBruce Evans #undef count
2090c7c34a24SBruce Evans 
2091c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */
20925070c7f8SJohn Dyson void
20931b40f8c0SMatthew Dillon vm_object_print(
20941b40f8c0SMatthew Dillon         /* db_expr_t */ long addr,
20951b40f8c0SMatthew Dillon 	boolean_t have_addr,
20961b40f8c0SMatthew Dillon 	/* db_expr_t */ long count,
20971b40f8c0SMatthew Dillon 	char *modif)
2098c7c34a24SBruce Evans {
2099c7c34a24SBruce Evans 	vm_object_print_static(addr, have_addr, count, modif);
2100c7c34a24SBruce Evans }
2101c7c34a24SBruce Evans 
2102c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
21035070c7f8SJohn Dyson {
21045070c7f8SJohn Dyson 	vm_object_t object;
21055070c7f8SJohn Dyson 	int nl = 0;
21065070c7f8SJohn Dyson 	int c;
2107cc64b484SAlfred Perlstein 
2108cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
21095070c7f8SJohn Dyson 		vm_pindex_t idx, fidx;
21105070c7f8SJohn Dyson 		vm_pindex_t osize;
21112446e4f0SAlan Cox 		vm_paddr_t pa = -1;
21125070c7f8SJohn Dyson 		int rcount;
21135070c7f8SJohn Dyson 		vm_page_t m;
21145070c7f8SJohn Dyson 
2115fc62ef1fSBruce Evans 		db_printf("new object: %p\n", (void *)object);
21165070c7f8SJohn Dyson 		if (nl > 18) {
21175070c7f8SJohn Dyson 			c = cngetc();
21185070c7f8SJohn Dyson 			if (c != ' ')
21195070c7f8SJohn Dyson 				return;
21205070c7f8SJohn Dyson 			nl = 0;
21215070c7f8SJohn Dyson 		}
21225070c7f8SJohn Dyson 		nl++;
21235070c7f8SJohn Dyson 		rcount = 0;
21245070c7f8SJohn Dyson 		fidx = 0;
21255070c7f8SJohn Dyson 		osize = object->size;
21265070c7f8SJohn Dyson 		if (osize > 128)
21275070c7f8SJohn Dyson 			osize = 128;
21285070c7f8SJohn Dyson 		for (idx = 0; idx < osize; idx++) {
21295070c7f8SJohn Dyson 			m = vm_page_lookup(object, idx);
21305070c7f8SJohn Dyson 			if (m == NULL) {
21315070c7f8SJohn Dyson 				if (rcount) {
21323efc015bSPeter Wemm 					db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
21333efc015bSPeter Wemm 						(long)fidx, rcount, (long)pa);
21345070c7f8SJohn Dyson 					if (nl > 18) {
21355070c7f8SJohn Dyson 						c = cngetc();
21365070c7f8SJohn Dyson 						if (c != ' ')
21375070c7f8SJohn Dyson 							return;
21385070c7f8SJohn Dyson 						nl = 0;
21395070c7f8SJohn Dyson 					}
21405070c7f8SJohn Dyson 					nl++;
21415070c7f8SJohn Dyson 					rcount = 0;
21425070c7f8SJohn Dyson 				}
21435070c7f8SJohn Dyson 				continue;
21445070c7f8SJohn Dyson 			}
21455070c7f8SJohn Dyson 
21465070c7f8SJohn Dyson 
21475070c7f8SJohn Dyson 			if (rcount &&
21485070c7f8SJohn Dyson 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
21495070c7f8SJohn Dyson 				++rcount;
21505070c7f8SJohn Dyson 				continue;
21515070c7f8SJohn Dyson 			}
21525070c7f8SJohn Dyson 			if (rcount) {
21532446e4f0SAlan Cox 				db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
21543efc015bSPeter Wemm 					(long)fidx, rcount, (long)pa);
21555070c7f8SJohn Dyson 				if (nl > 18) {
21565070c7f8SJohn Dyson 					c = cngetc();
21575070c7f8SJohn Dyson 					if (c != ' ')
21585070c7f8SJohn Dyson 						return;
21595070c7f8SJohn Dyson 					nl = 0;
21605070c7f8SJohn Dyson 				}
21615070c7f8SJohn Dyson 				nl++;
21625070c7f8SJohn Dyson 			}
21635070c7f8SJohn Dyson 			fidx = idx;
21645070c7f8SJohn Dyson 			pa = VM_PAGE_TO_PHYS(m);
21655070c7f8SJohn Dyson 			rcount = 1;
21665070c7f8SJohn Dyson 		}
21675070c7f8SJohn Dyson 		if (rcount) {
21683efc015bSPeter Wemm 			db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
21693efc015bSPeter Wemm 				(long)fidx, rcount, (long)pa);
21705070c7f8SJohn Dyson 			if (nl > 18) {
21715070c7f8SJohn Dyson 				c = cngetc();
21725070c7f8SJohn Dyson 				if (c != ' ')
21735070c7f8SJohn Dyson 					return;
21745070c7f8SJohn Dyson 				nl = 0;
21755070c7f8SJohn Dyson 			}
21765070c7f8SJohn Dyson 			nl++;
21775070c7f8SJohn Dyson 		}
21785070c7f8SJohn Dyson 	}
21795070c7f8SJohn Dyson }
2180c3cb3e12SDavid Greenman #endif /* DDB */
2181