xref: /freebsd/sys/vm/vm_object.c (revision 02dd83311ac1c426cf756027ac473679b29c8ac6)
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 
147f708ef1bSPoul-Henning Kamp static long object_collapses;
148f708ef1bSPoul-Henning Kamp static long object_bypasses;
149dad740e9SAlan Cox 
150dad740e9SAlan Cox /*
151dad740e9SAlan Cox  * next_index determines the page color that is assigned to the next
152dad740e9SAlan Cox  * allocated object.  Accesses to next_index are not synchronized
153dad740e9SAlan Cox  * because the effects of two or more object allocations using
154dad740e9SAlan Cox  * next_index simultaneously are inconsequential.  At any given time,
155dad740e9SAlan Cox  * numerous objects have the same page color.
156dad740e9SAlan Cox  */
157dcc5840eSAlan Cox static int next_index;
158dad740e9SAlan Cox 
159670d17b5SJeff Roberson static uma_zone_t obj_zone;
1608355f576SJeff Roberson 
161b23f72e9SBrian Feldman static int vm_object_zinit(void *mem, int size, int flags);
1628355f576SJeff Roberson 
1638355f576SJeff Roberson #ifdef INVARIANTS
1648355f576SJeff Roberson static void vm_object_zdtor(void *mem, int size, void *arg);
1658355f576SJeff Roberson 
1668355f576SJeff Roberson static void
1678355f576SJeff Roberson vm_object_zdtor(void *mem, int size, void *arg)
1688355f576SJeff Roberson {
1698355f576SJeff Roberson 	vm_object_t object;
1708355f576SJeff Roberson 
1718355f576SJeff Roberson 	object = (vm_object_t)mem;
17243186e53SAlan Cox 	KASSERT(TAILQ_EMPTY(&object->memq),
17343186e53SAlan Cox 	    ("object %p has resident pages",
17443186e53SAlan Cox 	    object));
1758355f576SJeff Roberson 	KASSERT(object->paging_in_progress == 0,
1768355f576SJeff Roberson 	    ("object %p paging_in_progress = %d",
1778355f576SJeff Roberson 	    object, object->paging_in_progress));
1788355f576SJeff Roberson 	KASSERT(object->resident_page_count == 0,
1798355f576SJeff Roberson 	    ("object %p resident_page_count = %d",
1808355f576SJeff Roberson 	    object, object->resident_page_count));
1818355f576SJeff Roberson 	KASSERT(object->shadow_count == 0,
1828355f576SJeff Roberson 	    ("object %p shadow_count = %d",
1838355f576SJeff Roberson 	    object, object->shadow_count));
1848355f576SJeff Roberson }
1858355f576SJeff Roberson #endif
1868355f576SJeff Roberson 
187b23f72e9SBrian Feldman static int
188b23f72e9SBrian Feldman vm_object_zinit(void *mem, int size, int flags)
1898355f576SJeff Roberson {
1908355f576SJeff Roberson 	vm_object_t object;
1918355f576SJeff Roberson 
1928355f576SJeff Roberson 	object = (vm_object_t)mem;
193e3a9e1b2SAlan Cox 	bzero(&object->mtx, sizeof(object->mtx));
1945285558aSAlan Cox 	VM_OBJECT_LOCK_INIT(object, "standard object");
1958355f576SJeff Roberson 
1968355f576SJeff Roberson 	/* These are true for any object that has been freed */
1978355f576SJeff Roberson 	object->paging_in_progress = 0;
1988355f576SJeff Roberson 	object->resident_page_count = 0;
1998355f576SJeff Roberson 	object->shadow_count = 0;
200b23f72e9SBrian Feldman 	return (0);
2018355f576SJeff Roberson }
202df8bae1dSRodney W. Grimes 
2033075778bSJohn Dyson void
2046395da54SIan Dowse _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
205df8bae1dSRodney W. Grimes {
206cf3e6e48SAlan Cox 	int incr;
2070cddd8f0SMatthew Dillon 
208df8bae1dSRodney W. Grimes 	TAILQ_INIT(&object->memq);
2091c500307SAlan Cox 	LIST_INIT(&object->shadow_head);
210a1f6d91cSDavid Greenman 
211b86ec922SMatthew Dillon 	object->root = NULL;
21224a1cce3SDavid Greenman 	object->type = type;
213df8bae1dSRodney W. Grimes 	object->size = size;
214b881da26SAlan Cox 	object->generation = 1;
215a1f6d91cSDavid Greenman 	object->ref_count = 1;
21624a1cce3SDavid Greenman 	object->flags = 0;
21760517fd1SJohn Baldwin 	if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
2183471677cSAlan Cox 		object->flags = OBJ_ONEMAPPING;
219ef39c05bSAlexander Leidinger 	incr = PQ_MAXLENGTH;
220ef39c05bSAlexander Leidinger 	if (size <= incr)
22199448ed1SJohn Dyson 		incr = size;
222dcc5840eSAlan Cox 	object->pg_color = next_index;
223ef39c05bSAlexander Leidinger 	next_index = (object->pg_color + incr) & PQ_COLORMASK;
22424a1cce3SDavid Greenman 	object->handle = NULL;
22524a1cce3SDavid Greenman 	object->backing_object = NULL;
226a316d390SJohn Dyson 	object->backing_object_offset = (vm_ooffset_t) 0;
227a1f6d91cSDavid Greenman 
228a5698387SAlan Cox 	mtx_lock(&vm_object_list_mtx);
22960517fd1SJohn Baldwin 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
230a5698387SAlan Cox 	mtx_unlock(&vm_object_list_mtx);
231df8bae1dSRodney W. Grimes }
232df8bae1dSRodney W. Grimes 
233df8bae1dSRodney W. Grimes /*
23426f9a767SRodney W. Grimes  *	vm_object_init:
23526f9a767SRodney W. Grimes  *
23626f9a767SRodney W. Grimes  *	Initialize the VM objects module.
23726f9a767SRodney W. Grimes  */
23826f9a767SRodney W. Grimes void
2391b40f8c0SMatthew Dillon vm_object_init(void)
24026f9a767SRodney W. Grimes {
24126f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_list);
2426008862bSJohn Baldwin 	mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF);
2430217125fSDavid Greenman 
2445285558aSAlan Cox 	VM_OBJECT_LOCK_INIT(&kernel_object_store, "kernel object");
245a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
24626f9a767SRodney W. Grimes 	    kernel_object);
24726f9a767SRodney W. Grimes 
2485285558aSAlan Cox 	VM_OBJECT_LOCK_INIT(&kmem_object_store, "kmem object");
249a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
25026f9a767SRodney W. Grimes 	    kmem_object);
251ed6a7863SAlan Cox 
2528dbca793STor Egge 	/*
2538dbca793STor Egge 	 * The lock portion of struct vm_object must be type stable due
2548dbca793STor Egge 	 * to vm_pageout_fallback_object_lock locking a vm object
2558dbca793STor Egge 	 * without holding any references to it.
2568dbca793STor Egge 	 */
2578355f576SJeff Roberson 	obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL,
2588355f576SJeff Roberson #ifdef INVARIANTS
2598355f576SJeff Roberson 	    vm_object_zdtor,
2608355f576SJeff Roberson #else
2618355f576SJeff Roberson 	    NULL,
2628355f576SJeff Roberson #endif
263f3c625e4SJeff Roberson 	    vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM|UMA_ZONE_NOFREE);
26499448ed1SJohn Dyson }
26599448ed1SJohn Dyson 
26699448ed1SJohn Dyson void
2671b40f8c0SMatthew Dillon vm_object_clear_flag(vm_object_t object, u_short bits)
2681b40f8c0SMatthew Dillon {
2695440b5a9SAlan Cox 
270d647a0edSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
271b06805adSJake Burkholder 	object->flags &= ~bits;
2721b40f8c0SMatthew Dillon }
2731b40f8c0SMatthew Dillon 
2741b40f8c0SMatthew Dillon void
2751b40f8c0SMatthew Dillon vm_object_pip_add(vm_object_t object, short i)
2761b40f8c0SMatthew Dillon {
277f279b88dSAlan Cox 
278d647a0edSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
279b06805adSJake Burkholder 	object->paging_in_progress += i;
2801b40f8c0SMatthew Dillon }
2811b40f8c0SMatthew Dillon 
2821b40f8c0SMatthew Dillon void
2831b40f8c0SMatthew Dillon vm_object_pip_subtract(vm_object_t object, short i)
2841b40f8c0SMatthew Dillon {
285d647a0edSAlan Cox 
2860fa05eaeSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
287b06805adSJake Burkholder 	object->paging_in_progress -= i;
2881b40f8c0SMatthew Dillon }
2891b40f8c0SMatthew Dillon 
2901b40f8c0SMatthew Dillon void
2911b40f8c0SMatthew Dillon vm_object_pip_wakeup(vm_object_t object)
2921b40f8c0SMatthew Dillon {
293f279b88dSAlan Cox 
294d647a0edSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
295b06805adSJake Burkholder 	object->paging_in_progress--;
2961b40f8c0SMatthew Dillon 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
2971b40f8c0SMatthew Dillon 		vm_object_clear_flag(object, OBJ_PIPWNT);
2981b40f8c0SMatthew Dillon 		wakeup(object);
2991b40f8c0SMatthew Dillon 	}
3001b40f8c0SMatthew Dillon }
3011b40f8c0SMatthew Dillon 
3021b40f8c0SMatthew Dillon void
3031b40f8c0SMatthew Dillon vm_object_pip_wakeupn(vm_object_t object, short i)
3041b40f8c0SMatthew Dillon {
305d647a0edSAlan Cox 
3060d420ad3SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
3071b40f8c0SMatthew Dillon 	if (i)
308b06805adSJake Burkholder 		object->paging_in_progress -= i;
3091b40f8c0SMatthew Dillon 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
3101b40f8c0SMatthew Dillon 		vm_object_clear_flag(object, OBJ_PIPWNT);
3111b40f8c0SMatthew Dillon 		wakeup(object);
3121b40f8c0SMatthew Dillon 	}
3131b40f8c0SMatthew Dillon }
3141b40f8c0SMatthew Dillon 
3151b40f8c0SMatthew Dillon void
3161b40f8c0SMatthew Dillon vm_object_pip_wait(vm_object_t object, char *waitid)
3171b40f8c0SMatthew Dillon {
3181ca58953SAlan Cox 
3191ca58953SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
3201ca58953SAlan Cox 	while (object->paging_in_progress) {
3211ca58953SAlan Cox 		object->flags |= OBJ_PIPWNT;
3221ca58953SAlan Cox 		msleep(object, VM_OBJECT_MTX(object), PVM, waitid, 0);
3231ca58953SAlan Cox 	}
3241b40f8c0SMatthew Dillon }
3251b40f8c0SMatthew Dillon 
32626f9a767SRodney W. Grimes /*
32726f9a767SRodney W. Grimes  *	vm_object_allocate:
32826f9a767SRodney W. Grimes  *
32926f9a767SRodney W. Grimes  *	Returns a new object with the given size.
33026f9a767SRodney W. Grimes  */
33126f9a767SRodney W. Grimes vm_object_t
3326395da54SIan Dowse vm_object_allocate(objtype_t type, vm_pindex_t size)
33326f9a767SRodney W. Grimes {
33490688d13SAlan Cox 	vm_object_t object;
33590688d13SAlan Cox 
33690688d13SAlan Cox 	object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK);
33790688d13SAlan Cox 	_vm_object_allocate(type, size, object);
33890688d13SAlan Cox 	return (object);
33926f9a767SRodney W. Grimes }
34026f9a767SRodney W. Grimes 
34126f9a767SRodney W. Grimes 
34226f9a767SRodney W. Grimes /*
343df8bae1dSRodney W. Grimes  *	vm_object_reference:
344df8bae1dSRodney W. Grimes  *
34515347817SAlan Cox  *	Gets another reference to the given object.  Note: OBJ_DEAD
34615347817SAlan Cox  *	objects can be referenced during final cleaning.
347df8bae1dSRodney W. Grimes  */
3486476c0d2SJohn Dyson void
3491b40f8c0SMatthew Dillon vm_object_reference(vm_object_t object)
350df8bae1dSRodney W. Grimes {
35182f9defeSAlan Cox 	struct vnode *vp;
35282f9defeSAlan Cox 	int flags;
35382f9defeSAlan Cox 
354df8bae1dSRodney W. Grimes 	if (object == NULL)
355df8bae1dSRodney W. Grimes 		return;
35615347817SAlan Cox 	VM_OBJECT_LOCK(object);
357df8bae1dSRodney W. Grimes 	object->ref_count++;
35847221757SJohn Dyson 	if (object->type == OBJT_VNODE) {
35982f9defeSAlan Cox 		vp = object->handle;
36082f9defeSAlan Cox 		VI_LOCK(vp);
36182f9defeSAlan Cox 		VM_OBJECT_UNLOCK(object);
36282f9defeSAlan Cox 		for (flags = LK_INTERLOCK; vget(vp, flags, curthread);
36382f9defeSAlan Cox 		     flags = 0)
36482f9defeSAlan Cox 			printf("vm_object_reference: delay in vget\n");
36582f9defeSAlan Cox 	} else
36682f9defeSAlan Cox 		VM_OBJECT_UNLOCK(object);
36795e5e988SJohn Dyson }
36895e5e988SJohn Dyson 
36923955314SAlfred Perlstein /*
370b921a12bSAlan Cox  *	vm_object_reference_locked:
371b921a12bSAlan Cox  *
372b921a12bSAlan Cox  *	Gets another reference to the given object.
373b921a12bSAlan Cox  *
374b921a12bSAlan Cox  *	The object must be locked.
375b921a12bSAlan Cox  */
376b921a12bSAlan Cox void
377b921a12bSAlan Cox vm_object_reference_locked(vm_object_t object)
378b921a12bSAlan Cox {
379b921a12bSAlan Cox 	struct vnode *vp;
380b921a12bSAlan Cox 
381b921a12bSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
382b921a12bSAlan Cox 	KASSERT((object->flags & OBJ_DEAD) == 0,
383b921a12bSAlan Cox 	    ("vm_object_reference_locked: dead object referenced"));
384b921a12bSAlan Cox 	object->ref_count++;
385b921a12bSAlan Cox 	if (object->type == OBJT_VNODE) {
386b921a12bSAlan Cox 		vp = object->handle;
387b921a12bSAlan Cox 		vref(vp);
388b921a12bSAlan Cox 	}
389b921a12bSAlan Cox }
390b921a12bSAlan Cox 
391b921a12bSAlan Cox /*
3929d5abbddSJens Schweikhardt  * Handle deallocating an object of type OBJT_VNODE.
39323955314SAlfred Perlstein  */
39402dd8331SAlan Cox static void
3951b40f8c0SMatthew Dillon vm_object_vndeallocate(vm_object_t object)
39695e5e988SJohn Dyson {
39795e5e988SJohn Dyson 	struct vnode *vp = (struct vnode *) object->handle;
398219cbf59SEivind Eklund 
399ae51ff11SJeff Roberson 	VFS_ASSERT_GIANT(vp->v_mount);
400ad682c48SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
4015526d2d9SEivind Eklund 	KASSERT(object->type == OBJT_VNODE,
4025526d2d9SEivind Eklund 	    ("vm_object_vndeallocate: not a vnode object"));
403219cbf59SEivind Eklund 	KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
404219cbf59SEivind Eklund #ifdef INVARIANTS
40595e5e988SJohn Dyson 	if (object->ref_count == 0) {
40695e5e988SJohn Dyson 		vprint("vm_object_vndeallocate", vp);
40795e5e988SJohn Dyson 		panic("vm_object_vndeallocate: bad object reference count");
40895e5e988SJohn Dyson 	}
40995e5e988SJohn Dyson #endif
41095e5e988SJohn Dyson 
41195e5e988SJohn Dyson 	object->ref_count--;
41247221757SJohn Dyson 	if (object->ref_count == 0) {
413e6e370a7SJeff Roberson 		mp_fixme("Unlocked vflag access.");
414e6e370a7SJeff Roberson 		vp->v_vflag &= ~VV_TEXT;
4152be70f79SJohn Dyson 	}
416ad682c48SAlan Cox 	VM_OBJECT_UNLOCK(object);
41723955314SAlfred Perlstein 	/*
41823955314SAlfred Perlstein 	 * vrele may need a vop lock
41923955314SAlfred Perlstein 	 */
42047221757SJohn Dyson 	vrele(vp);
421df8bae1dSRodney W. Grimes }
422df8bae1dSRodney W. Grimes 
423df8bae1dSRodney W. Grimes /*
424df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
425df8bae1dSRodney W. Grimes  *
426df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
427df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
428df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
429df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
430df8bae1dSRodney W. Grimes  *	may be relinquished.
431df8bae1dSRodney W. Grimes  *
432df8bae1dSRodney W. Grimes  *	No object may be locked.
433df8bae1dSRodney W. Grimes  */
43426f9a767SRodney W. Grimes void
4351b40f8c0SMatthew Dillon vm_object_deallocate(vm_object_t object)
436df8bae1dSRodney W. Grimes {
437df8bae1dSRodney W. Grimes 	vm_object_t temp;
438df8bae1dSRodney W. Grimes 
439df8bae1dSRodney W. Grimes 	while (object != NULL) {
440ae51ff11SJeff Roberson 		int vfslocked;
44123b186d3SAlan Cox 		/*
44223b186d3SAlan Cox 		 * In general, the object should be locked when working with
44323b186d3SAlan Cox 		 * its type.  In this case, in order to maintain proper lock
44423b186d3SAlan Cox 		 * ordering, an exception is possible because a vnode-backed
44523b186d3SAlan Cox 		 * object never changes its type.
44623b186d3SAlan Cox 		 */
447ae51ff11SJeff Roberson 		vfslocked = 0;
448ae51ff11SJeff Roberson 		if (object->type == OBJT_VNODE) {
449ae51ff11SJeff Roberson 			struct vnode *vp = (struct vnode *) object->handle;
450ae51ff11SJeff Roberson 			vfslocked = VFS_LOCK_GIANT(vp->v_mount);
451ae51ff11SJeff Roberson 		}
452ad682c48SAlan Cox 		VM_OBJECT_LOCK(object);
4533b68228cSAlan Cox 		if (object->type == OBJT_VNODE) {
45495e5e988SJohn Dyson 			vm_object_vndeallocate(object);
455ae51ff11SJeff Roberson 			VFS_UNLOCK_GIANT(vfslocked);
45623b186d3SAlan Cox 			return;
45795e5e988SJohn Dyson 		}
45895e5e988SJohn Dyson 
4598125b1e6SAlfred Perlstein 		KASSERT(object->ref_count != 0,
4608125b1e6SAlfred Perlstein 			("vm_object_deallocate: object deallocated too many times: %d", object->type));
4612be70f79SJohn Dyson 
4622be70f79SJohn Dyson 		/*
4638125b1e6SAlfred Perlstein 		 * If the reference count goes to 0 we start calling
4648125b1e6SAlfred Perlstein 		 * vm_object_terminate() on the object chain.
4658125b1e6SAlfred Perlstein 		 * A ref count of 1 may be a special case depending on the
4668125b1e6SAlfred Perlstein 		 * shadow count being 0 or 1.
4672be70f79SJohn Dyson 		 */
468c0877f10SJohn Dyson 		object->ref_count--;
4698125b1e6SAlfred Perlstein 		if (object->ref_count > 1) {
4703b68228cSAlan Cox 			VM_OBJECT_UNLOCK(object);
47123b186d3SAlan Cox 			return;
4728125b1e6SAlfred Perlstein 		} else if (object->ref_count == 1) {
4738125b1e6SAlfred Perlstein 			if (object->shadow_count == 0) {
4748125b1e6SAlfred Perlstein 				vm_object_set_flag(object, OBJ_ONEMAPPING);
4758125b1e6SAlfred Perlstein 			} else if ((object->shadow_count == 1) &&
4768125b1e6SAlfred Perlstein 			    (object->handle == NULL) &&
47724a1cce3SDavid Greenman 			    (object->type == OBJT_DEFAULT ||
47824a1cce3SDavid Greenman 			     object->type == OBJT_SWAP)) {
479a1f6d91cSDavid Greenman 				vm_object_t robject;
48095e5e988SJohn Dyson 
4811c500307SAlan Cox 				robject = LIST_FIRST(&object->shadow_head);
4825526d2d9SEivind Eklund 				KASSERT(robject != NULL,
483219cbf59SEivind Eklund 				    ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
4845526d2d9SEivind Eklund 					 object->ref_count,
4855526d2d9SEivind Eklund 					 object->shadow_count));
486b72b0115SAlan Cox 				if (!VM_OBJECT_TRYLOCK(robject)) {
487b72b0115SAlan Cox 					/*
488b72b0115SAlan Cox 					 * Avoid a potential deadlock.
489b72b0115SAlan Cox 					 */
490b72b0115SAlan Cox 					object->ref_count++;
491b72b0115SAlan Cox 					VM_OBJECT_UNLOCK(object);
492a7d86121SAlan Cox 					/*
493a7d86121SAlan Cox 					 * More likely than not the thread
494a7d86121SAlan Cox 					 * holding robject's lock has lower
495a7d86121SAlan Cox 					 * priority than the current thread.
496a7d86121SAlan Cox 					 * Let the lower priority thread run.
497a7d86121SAlan Cox 					 */
498a7d86121SAlan Cox 					tsleep(&proc0, PVM, "vmo_de", 1);
499b72b0115SAlan Cox 					continue;
500b72b0115SAlan Cox 				}
501d936694fSAlan Cox 				/*
502d936694fSAlan Cox 				 * Collapse object into its shadow unless its
503d936694fSAlan Cox 				 * shadow is dead.  In that case, object will
504d936694fSAlan Cox 				 * be deallocated by the thread that is
505d936694fSAlan Cox 				 * deallocating its shadow.
506d936694fSAlan Cox 				 */
507d936694fSAlan Cox 				if ((robject->flags & OBJ_DEAD) == 0 &&
508d936694fSAlan Cox 				    (robject->handle == NULL) &&
50924a1cce3SDavid Greenman 				    (robject->type == OBJT_DEFAULT ||
51024a1cce3SDavid Greenman 				     robject->type == OBJT_SWAP)) {
511a1f6d91cSDavid Greenman 
51295e5e988SJohn Dyson 					robject->ref_count++;
513138449dcSAlan Cox retry:
514138449dcSAlan Cox 					if (robject->paging_in_progress) {
515138449dcSAlan Cox 						VM_OBJECT_UNLOCK(object);
516138449dcSAlan Cox 						vm_object_pip_wait(robject,
517138449dcSAlan Cox 						    "objde1");
518138449dcSAlan Cox 						VM_OBJECT_LOCK(object);
519138449dcSAlan Cox 						goto retry;
520138449dcSAlan Cox 					} else if (object->paging_in_progress) {
521138449dcSAlan Cox 						VM_OBJECT_UNLOCK(robject);
522138449dcSAlan Cox 						object->flags |= OBJ_PIPWNT;
523138449dcSAlan Cox 						msleep(object,
524138449dcSAlan Cox 						    VM_OBJECT_MTX(object),
525138449dcSAlan Cox 						    PDROP | PVM, "objde2", 0);
526138449dcSAlan Cox 						VM_OBJECT_LOCK(robject);
527138449dcSAlan Cox 						VM_OBJECT_LOCK(object);
528138449dcSAlan Cox 						goto retry;
529a1f6d91cSDavid Greenman 					}
5303b68228cSAlan Cox 					VM_OBJECT_UNLOCK(object);
53195e5e988SJohn Dyson 					if (robject->ref_count == 1) {
53295e5e988SJohn Dyson 						robject->ref_count--;
533ba8da839SDavid Greenman 						object = robject;
53495e5e988SJohn Dyson 						goto doterm;
53595e5e988SJohn Dyson 					}
53695e5e988SJohn Dyson 					object = robject;
53795e5e988SJohn Dyson 					vm_object_collapse(object);
538d7fc2210SAlan Cox 					VM_OBJECT_UNLOCK(object);
539ba8da839SDavid Greenman 					continue;
540a1f6d91cSDavid Greenman 				}
541b72b0115SAlan Cox 				VM_OBJECT_UNLOCK(robject);
54295e5e988SJohn Dyson 			}
5433b68228cSAlan Cox 			VM_OBJECT_UNLOCK(object);
54423b186d3SAlan Cox 			return;
54595e5e988SJohn Dyson 		}
54695e5e988SJohn Dyson doterm:
54724a1cce3SDavid Greenman 		temp = object->backing_object;
548c9917419SAlan Cox 		if (temp != NULL) {
549c9917419SAlan Cox 			VM_OBJECT_LOCK(temp);
5501c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
55195e5e988SJohn Dyson 			temp->shadow_count--;
552eaf13dd7SJohn Dyson 			temp->generation++;
553c9917419SAlan Cox 			VM_OBJECT_UNLOCK(temp);
55495461b45SJohn Dyson 			object->backing_object = NULL;
555de5f6a77SJohn Dyson 		}
556245df27cSMatthew Dillon 		/*
557245df27cSMatthew Dillon 		 * Don't double-terminate, we could be in a termination
558245df27cSMatthew Dillon 		 * recursion due to the terminate having to sync data
559245df27cSMatthew Dillon 		 * to disk.
560245df27cSMatthew Dillon 		 */
561245df27cSMatthew Dillon 		if ((object->flags & OBJ_DEAD) == 0)
562df8bae1dSRodney W. Grimes 			vm_object_terminate(object);
563c829b9d0SAlan Cox 		else
564c829b9d0SAlan Cox 			VM_OBJECT_UNLOCK(object);
565df8bae1dSRodney W. Grimes 		object = temp;
566df8bae1dSRodney W. Grimes 	}
567df8bae1dSRodney W. Grimes }
568df8bae1dSRodney W. Grimes 
569df8bae1dSRodney W. Grimes /*
570df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
571df8bae1dSRodney W. Grimes  *	up all previously used resources.
572df8bae1dSRodney W. Grimes  *
573df8bae1dSRodney W. Grimes  *	The object must be locked.
5741c7c3c6aSMatthew Dillon  *	This routine may block.
575df8bae1dSRodney W. Grimes  */
57695e5e988SJohn Dyson void
5771b40f8c0SMatthew Dillon vm_object_terminate(vm_object_t object)
578df8bae1dSRodney W. Grimes {
579d031cff1SMatthew Dillon 	vm_page_t p;
580df8bae1dSRodney W. Grimes 
581c829b9d0SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
5820cddd8f0SMatthew Dillon 
58395e5e988SJohn Dyson 	/*
58495e5e988SJohn Dyson 	 * Make sure no one uses us.
58595e5e988SJohn Dyson 	 */
586069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_DEAD);
5873c631446SJohn Dyson 
588df8bae1dSRodney W. Grimes 	/*
589f6b04d2bSDavid Greenman 	 * wait for the pageout daemon to be done with the object
590df8bae1dSRodney W. Grimes 	 */
59166095752SJohn Dyson 	vm_object_pip_wait(object, "objtrm");
592df8bae1dSRodney W. Grimes 
5935526d2d9SEivind Eklund 	KASSERT(!object->paging_in_progress,
5945526d2d9SEivind Eklund 		("vm_object_terminate: pageout in progress"));
59526f9a767SRodney W. Grimes 
59626f9a767SRodney W. Grimes 	/*
5970d94caffSDavid Greenman 	 * Clean and free the pages, as appropriate. All references to the
5980d94caffSDavid Greenman 	 * object are gone, so we don't need to lock it.
59926f9a767SRodney W. Grimes 	 */
60024a1cce3SDavid Greenman 	if (object->type == OBJT_VNODE) {
601f7dd7b63SAlan Cox 		struct vnode *vp = (struct vnode *)object->handle;
60295e5e988SJohn Dyson 
60395e5e988SJohn Dyson 		/*
60495e5e988SJohn Dyson 		 * Clean pages and flush buffers.
60595e5e988SJohn Dyson 		 */
6068f9110f6SJohn Dyson 		vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
607b6e48e03SAlan Cox 		VM_OBJECT_UNLOCK(object);
60895e5e988SJohn Dyson 
6097c0745eeSPoul-Henning Kamp 		vinvalbuf(vp, V_SAVE, NULL, 0, 0);
610f7dd7b63SAlan Cox 
611f7dd7b63SAlan Cox 		VM_OBJECT_LOCK(object);
612bef608bdSJohn Dyson 	}
613bef608bdSJohn Dyson 
614971dd342SAlfred Perlstein 	KASSERT(object->ref_count == 0,
615971dd342SAlfred Perlstein 		("vm_object_terminate: object with references, ref_count=%d",
616971dd342SAlfred Perlstein 		object->ref_count));
617996c772fSJohn Dyson 
6180d94caffSDavid Greenman 	/*
619356863ebSDavid Greenman 	 * Now free any remaining pages. For internal objects, this also
620356863ebSDavid Greenman 	 * removes them from paging queues. Don't free wired pages, just
621356863ebSDavid Greenman 	 * remove them from the object.
622df8bae1dSRodney W. Grimes 	 */
62356030358SAlan Cox 	vm_page_lock_queues();
624b18bfc3dSJohn Dyson 	while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
625971dd342SAlfred Perlstein 		KASSERT(!p->busy && (p->flags & PG_BUSY) == 0,
626971dd342SAlfred Perlstein 			("vm_object_terminate: freeing busy page %p "
627971dd342SAlfred Perlstein 			"p->busy = %d, p->flags %x\n", p, p->busy, p->flags));
6280b10ba98SDavid Greenman 		if (p->wire_count == 0) {
629df8bae1dSRodney W. Grimes 			vm_page_free(p);
630df8bae1dSRodney W. Grimes 			cnt.v_pfree++;
6310b10ba98SDavid Greenman 		} else {
6320b10ba98SDavid Greenman 			vm_page_remove(p);
6330b10ba98SDavid Greenman 		}
634df8bae1dSRodney W. Grimes 	}
63556030358SAlan Cox 	vm_page_unlock_queues();
636bef608bdSJohn Dyson 
6372d8acc0fSJohn Dyson 	/*
6389fcfb650SDavid Greenman 	 * Let the pager know object is dead.
6399fcfb650SDavid Greenman 	 */
6409fcfb650SDavid Greenman 	vm_pager_deallocate(object);
641658ad5ffSAlan Cox 	VM_OBJECT_UNLOCK(object);
6429fcfb650SDavid Greenman 
6439fcfb650SDavid Greenman 	/*
6440b10ba98SDavid Greenman 	 * Remove the object from the global object list.
6452d8acc0fSJohn Dyson 	 */
6469ed346baSBosko Milekic 	mtx_lock(&vm_object_list_mtx);
647df8bae1dSRodney W. Grimes 	TAILQ_REMOVE(&vm_object_list, object, object_list);
6489ed346baSBosko Milekic 	mtx_unlock(&vm_object_list_mtx);
6490b10ba98SDavid Greenman 
650df8bae1dSRodney W. Grimes 	/*
651df8bae1dSRodney W. Grimes 	 * Free the space for the object.
652df8bae1dSRodney W. Grimes 	 */
653670d17b5SJeff Roberson 	uma_zfree(obj_zone, object);
65447221757SJohn Dyson }
655df8bae1dSRodney W. Grimes 
656df8bae1dSRodney W. Grimes /*
657df8bae1dSRodney W. Grimes  *	vm_object_page_clean
658df8bae1dSRodney W. Grimes  *
6594f79d873SMatthew Dillon  *	Clean all dirty pages in the specified range of object.  Leaves page
6604f79d873SMatthew Dillon  * 	on whatever queue it is currently on.   If NOSYNC is set then do not
6614f79d873SMatthew Dillon  *	write out pages with PG_NOSYNC set (originally comes from MAP_NOSYNC),
6624f79d873SMatthew Dillon  *	leaving the object dirty.
66326f9a767SRodney W. Grimes  *
66443b7990eSMatthew Dillon  *	When stuffing pages asynchronously, allow clustering.  XXX we need a
66543b7990eSMatthew Dillon  *	synchronous clustering mode implementation.
66643b7990eSMatthew Dillon  *
66726f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
66826f9a767SRodney W. Grimes  *
66926f9a767SRodney W. Grimes  *	The object must be locked.
67026f9a767SRodney W. Grimes  */
671f6b04d2bSDavid Greenman void
6721b40f8c0SMatthew Dillon vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end, int flags)
673f6b04d2bSDavid Greenman {
674b9b7a4beSMatthew Dillon 	vm_page_t p, np;
6756395da54SIan Dowse 	vm_pindex_t tstart, tend;
676bd7e5f99SJohn Dyson 	vm_pindex_t pi;
6774f79d873SMatthew Dillon 	int clearobjflags;
6788f9110f6SJohn Dyson 	int pagerflags;
6792d8acc0fSJohn Dyson 	int curgeneration;
680f6b04d2bSDavid Greenman 
681b6e48e03SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
682aef922f5SJohn Dyson 	if (object->type != OBJT_VNODE ||
683aef922f5SJohn Dyson 		(object->flags & OBJ_MIGHTBEDIRTY) == 0)
684f6b04d2bSDavid Greenman 		return;
685f6b04d2bSDavid Greenman 
68643b7990eSMatthew Dillon 	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
6878f9110f6SJohn Dyson 	pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
6888f9110f6SJohn Dyson 
689069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_CLEANING);
69024a1cce3SDavid Greenman 
691f6b04d2bSDavid Greenman 	tstart = start;
692f6b04d2bSDavid Greenman 	if (end == 0) {
693f6b04d2bSDavid Greenman 		tend = object->size;
694f6b04d2bSDavid Greenman 	} else {
695f6b04d2bSDavid Greenman 		tend = end;
696f6b04d2bSDavid Greenman 	}
697eaf13dd7SJohn Dyson 
69835c01631SAlan Cox 	vm_page_lock_queues();
6994f79d873SMatthew Dillon 	/*
700b9b7a4beSMatthew Dillon 	 * If the caller is smart and only msync()s a range he knows is
701b9b7a4beSMatthew Dillon 	 * dirty, we may be able to avoid an object scan.  This results in
702b9b7a4beSMatthew Dillon 	 * a phenominal improvement in performance.  We cannot do this
703b9b7a4beSMatthew Dillon 	 * as a matter of course because the object may be huge - e.g.
704b9b7a4beSMatthew Dillon 	 * the size might be in the gigabytes or terrabytes.
705b9b7a4beSMatthew Dillon 	 */
706b9b7a4beSMatthew Dillon 	if (msync_flush_flags & MSYNC_FLUSH_HARDSEQ) {
707300b96acSIan Dowse 		vm_pindex_t tscan;
708b9b7a4beSMatthew Dillon 		int scanlimit;
709b9b7a4beSMatthew Dillon 		int scanreset;
710b9b7a4beSMatthew Dillon 
711b9b7a4beSMatthew Dillon 		scanreset = object->resident_page_count / EASY_SCAN_FACTOR;
712b9b7a4beSMatthew Dillon 		if (scanreset < 16)
713b9b7a4beSMatthew Dillon 			scanreset = 16;
71443b7990eSMatthew Dillon 		pagerflags |= VM_PAGER_IGNORE_CLEANCHK;
715b9b7a4beSMatthew Dillon 
716b9b7a4beSMatthew Dillon 		scanlimit = scanreset;
717b9b7a4beSMatthew Dillon 		tscan = tstart;
718b9b7a4beSMatthew Dillon 		while (tscan < tend) {
719b9b7a4beSMatthew Dillon 			curgeneration = object->generation;
720b9b7a4beSMatthew Dillon 			p = vm_page_lookup(object, tscan);
721b9b7a4beSMatthew Dillon 			if (p == NULL || p->valid == 0 ||
722b9b7a4beSMatthew Dillon 			    (p->queue - p->pc) == PQ_CACHE) {
723b9b7a4beSMatthew Dillon 				if (--scanlimit == 0)
724b9b7a4beSMatthew Dillon 					break;
725b9b7a4beSMatthew Dillon 				++tscan;
726b9b7a4beSMatthew Dillon 				continue;
727b9b7a4beSMatthew Dillon 			}
728b9b7a4beSMatthew Dillon 			vm_page_test_dirty(p);
729b9b7a4beSMatthew Dillon 			if ((p->dirty & p->valid) == 0) {
730b9b7a4beSMatthew Dillon 				if (--scanlimit == 0)
731b9b7a4beSMatthew Dillon 					break;
732b9b7a4beSMatthew Dillon 				++tscan;
733b9b7a4beSMatthew Dillon 				continue;
734b9b7a4beSMatthew Dillon 			}
735b9b7a4beSMatthew Dillon 			/*
736b9b7a4beSMatthew Dillon 			 * If we have been asked to skip nosync pages and
737b9b7a4beSMatthew Dillon 			 * this is a nosync page, we can't continue.
738b9b7a4beSMatthew Dillon 			 */
739b9b7a4beSMatthew Dillon 			if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
740b9b7a4beSMatthew Dillon 				if (--scanlimit == 0)
741b9b7a4beSMatthew Dillon 					break;
742b9b7a4beSMatthew Dillon 				++tscan;
743b9b7a4beSMatthew Dillon 				continue;
744b9b7a4beSMatthew Dillon 			}
745b9b7a4beSMatthew Dillon 			scanlimit = scanreset;
746b9b7a4beSMatthew Dillon 
747b9b7a4beSMatthew Dillon 			/*
748b9b7a4beSMatthew Dillon 			 * This returns 0 if it was unable to busy the first
749b9b7a4beSMatthew Dillon 			 * page (i.e. had to sleep).
750b9b7a4beSMatthew Dillon 			 */
751b9b7a4beSMatthew Dillon 			tscan += vm_object_page_collect_flush(object, p, curgeneration, pagerflags);
752b9b7a4beSMatthew Dillon 		}
753b9b7a4beSMatthew Dillon 
754b9b7a4beSMatthew Dillon 		/*
755b9b7a4beSMatthew Dillon 		 * If everything was dirty and we flushed it successfully,
756b9b7a4beSMatthew Dillon 		 * and the requested range is not the entire object, we
757b9b7a4beSMatthew Dillon 		 * don't have to mess with CLEANCHK or MIGHTBEDIRTY and can
758b9b7a4beSMatthew Dillon 		 * return immediately.
759b9b7a4beSMatthew Dillon 		 */
760b9b7a4beSMatthew Dillon 		if (tscan >= tend && (tstart || tend < object->size)) {
76135c01631SAlan Cox 			vm_page_unlock_queues();
762b9b7a4beSMatthew Dillon 			vm_object_clear_flag(object, OBJ_CLEANING);
763b9b7a4beSMatthew Dillon 			return;
764b9b7a4beSMatthew Dillon 		}
76543b7990eSMatthew Dillon 		pagerflags &= ~VM_PAGER_IGNORE_CLEANCHK;
766b9b7a4beSMatthew Dillon 	}
767b9b7a4beSMatthew Dillon 
768b9b7a4beSMatthew Dillon 	/*
7694f79d873SMatthew Dillon 	 * Generally set CLEANCHK interlock and make the page read-only so
7704f79d873SMatthew Dillon 	 * we can then clear the object flags.
7714f79d873SMatthew Dillon 	 *
7724f79d873SMatthew Dillon 	 * However, if this is a nosync mmap then the object is likely to
7734f79d873SMatthew Dillon 	 * stay dirty so do not mess with the page and do not clear the
7744f79d873SMatthew Dillon 	 * object flags.
7754f79d873SMatthew Dillon 	 */
7764f79d873SMatthew Dillon 	clearobjflags = 1;
777fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(p, &object->memq, listq) {
778e69763a3SDoug Rabson 		vm_page_flag_set(p, PG_CLEANCHK);
7794f79d873SMatthew Dillon 		if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC))
7804f79d873SMatthew Dillon 			clearobjflags = 0;
7814f79d873SMatthew Dillon 		else
782a12cc0e4SAlan Cox 			pmap_page_protect(p, VM_PROT_READ);
783eaf13dd7SJohn Dyson 	}
784eaf13dd7SJohn Dyson 
7854f79d873SMatthew Dillon 	if (clearobjflags && (tstart == 0) && (tend == object->size)) {
786245df27cSMatthew Dillon 		struct vnode *vp;
787245df27cSMatthew Dillon 
788069e9bc1SDoug Rabson 		vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
789245df27cSMatthew Dillon 		if (object->type == OBJT_VNODE &&
790245df27cSMatthew Dillon 		    (vp = (struct vnode *)object->handle) != NULL) {
791e6e370a7SJeff Roberson 			VI_LOCK(vp);
792e6e370a7SJeff Roberson 			if (vp->v_iflag & VI_OBJDIRTY)
793e6e370a7SJeff Roberson 				vp->v_iflag &= ~VI_OBJDIRTY;
794e6e370a7SJeff Roberson 			VI_UNLOCK(vp);
795245df27cSMatthew Dillon 		}
796ec4f9fb0SDavid Greenman 	}
797f6b04d2bSDavid Greenman 
798bd7e5f99SJohn Dyson rescan:
7992d8acc0fSJohn Dyson 	curgeneration = object->generation;
8002d8acc0fSJohn Dyson 
801b18bfc3dSJohn Dyson 	for (p = TAILQ_FIRST(&object->memq); p; p = np) {
802b9b7a4beSMatthew Dillon 		int n;
803b9b7a4beSMatthew Dillon 
804b18bfc3dSJohn Dyson 		np = TAILQ_NEXT(p, listq);
805bd7e5f99SJohn Dyson 
806b9b7a4beSMatthew Dillon again:
807bd7e5f99SJohn Dyson 		pi = p->pindex;
808bd7e5f99SJohn Dyson 		if (((p->flags & PG_CLEANCHK) == 0) ||
809bd7e5f99SJohn Dyson 			(pi < tstart) || (pi >= tend) ||
8105070c7f8SJohn Dyson 			(p->valid == 0) ||
8115070c7f8SJohn Dyson 			((p->queue - p->pc) == PQ_CACHE)) {
812e69763a3SDoug Rabson 			vm_page_flag_clear(p, PG_CLEANCHK);
813aef922f5SJohn Dyson 			continue;
814f6b04d2bSDavid Greenman 		}
815f6b04d2bSDavid Greenman 
816bd7e5f99SJohn Dyson 		vm_page_test_dirty(p);
817bd7e5f99SJohn Dyson 		if ((p->dirty & p->valid) == 0) {
818e69763a3SDoug Rabson 			vm_page_flag_clear(p, PG_CLEANCHK);
819bd7e5f99SJohn Dyson 			continue;
820bd7e5f99SJohn Dyson 		}
821ec4f9fb0SDavid Greenman 
8224f79d873SMatthew Dillon 		/*
8234f79d873SMatthew Dillon 		 * If we have been asked to skip nosync pages and this is a
8244f79d873SMatthew Dillon 		 * nosync page, skip it.  Note that the object flags were
8254f79d873SMatthew Dillon 		 * not cleared in this case so we do not have to set them.
8264f79d873SMatthew Dillon 		 */
8274f79d873SMatthew Dillon 		if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
8284f79d873SMatthew Dillon 			vm_page_flag_clear(p, PG_CLEANCHK);
8294f79d873SMatthew Dillon 			continue;
8304f79d873SMatthew Dillon 		}
8314f79d873SMatthew Dillon 
832b9b7a4beSMatthew Dillon 		n = vm_object_page_collect_flush(object, p,
833b9b7a4beSMatthew Dillon 			curgeneration, pagerflags);
834b9b7a4beSMatthew Dillon 		if (n == 0)
835b9b7a4beSMatthew Dillon 			goto rescan;
836b9b7a4beSMatthew Dillon 
837b9b7a4beSMatthew Dillon 		if (object->generation != curgeneration)
838b9b7a4beSMatthew Dillon 			goto rescan;
839b9b7a4beSMatthew Dillon 
840b9b7a4beSMatthew Dillon 		/*
841b9b7a4beSMatthew Dillon 		 * Try to optimize the next page.  If we can't we pick up
842b9b7a4beSMatthew Dillon 		 * our (random) scan where we left off.
843b9b7a4beSMatthew Dillon 		 */
844b9b7a4beSMatthew Dillon 		if (msync_flush_flags & MSYNC_FLUSH_SOFTSEQ) {
845b9b7a4beSMatthew Dillon 			if ((p = vm_page_lookup(object, pi + n)) != NULL)
846b9b7a4beSMatthew Dillon 				goto again;
847b9b7a4beSMatthew Dillon 		}
848b9b7a4beSMatthew Dillon 	}
84935c01631SAlan Cox 	vm_page_unlock_queues();
850b9b7a4beSMatthew Dillon #if 0
8518df6bac4SPoul-Henning Kamp 	VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
852b9b7a4beSMatthew Dillon #endif
853b9b7a4beSMatthew Dillon 
854b9b7a4beSMatthew Dillon 	vm_object_clear_flag(object, OBJ_CLEANING);
855b9b7a4beSMatthew Dillon 	return;
856b9b7a4beSMatthew Dillon }
857b9b7a4beSMatthew Dillon 
858b9b7a4beSMatthew Dillon static int
859b9b7a4beSMatthew Dillon vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags)
860b9b7a4beSMatthew Dillon {
861b9b7a4beSMatthew Dillon 	int runlen;
862b9b7a4beSMatthew Dillon 	int maxf;
863b9b7a4beSMatthew Dillon 	int chkb;
864b9b7a4beSMatthew Dillon 	int maxb;
865b9b7a4beSMatthew Dillon 	int i;
866b9b7a4beSMatthew Dillon 	vm_pindex_t pi;
867b9b7a4beSMatthew Dillon 	vm_page_t maf[vm_pageout_page_count];
868b9b7a4beSMatthew Dillon 	vm_page_t mab[vm_pageout_page_count];
869b9b7a4beSMatthew Dillon 	vm_page_t ma[vm_pageout_page_count];
870b9b7a4beSMatthew Dillon 
87135c01631SAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
872b9b7a4beSMatthew Dillon 	pi = p->pindex;
873bd82dc74SAlan Cox 	while (vm_page_sleep_if_busy(p, TRUE, "vpcwai")) {
87435c01631SAlan Cox 		vm_page_lock_queues();
8752d8acc0fSJohn Dyson 		if (object->generation != curgeneration) {
876b9b7a4beSMatthew Dillon 			return(0);
877f6b04d2bSDavid Greenman 		}
878bd82dc74SAlan Cox 	}
879bd7e5f99SJohn Dyson 	maxf = 0;
880bd7e5f99SJohn Dyson 	for(i = 1; i < vm_pageout_page_count; i++) {
881b9b7a4beSMatthew Dillon 		vm_page_t tp;
882b9b7a4beSMatthew Dillon 
8838aef1712SMatthew Dillon 		if ((tp = vm_page_lookup(object, pi + i)) != NULL) {
884bd7e5f99SJohn Dyson 			if ((tp->flags & PG_BUSY) ||
88543b7990eSMatthew Dillon 				((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
88643b7990eSMatthew Dillon 				 (tp->flags & PG_CLEANCHK) == 0) ||
887ffc82b0aSJohn Dyson 				(tp->busy != 0))
888bd7e5f99SJohn Dyson 				break;
8895070c7f8SJohn Dyson 			if((tp->queue - tp->pc) == PQ_CACHE) {
890e69763a3SDoug Rabson 				vm_page_flag_clear(tp, PG_CLEANCHK);
8913077a9c2SJohn Dyson 				break;
8923077a9c2SJohn Dyson 			}
893bd7e5f99SJohn Dyson 			vm_page_test_dirty(tp);
894bd7e5f99SJohn Dyson 			if ((tp->dirty & tp->valid) == 0) {
895e69763a3SDoug Rabson 				vm_page_flag_clear(tp, PG_CLEANCHK);
896bd7e5f99SJohn Dyson 				break;
897bd7e5f99SJohn Dyson 			}
898bd7e5f99SJohn Dyson 			maf[ i - 1 ] = tp;
899bd7e5f99SJohn Dyson 			maxf++;
900bd7e5f99SJohn Dyson 			continue;
901bd7e5f99SJohn Dyson 		}
902bd7e5f99SJohn Dyson 		break;
903bd7e5f99SJohn Dyson 	}
904aef922f5SJohn Dyson 
905bd7e5f99SJohn Dyson 	maxb = 0;
906bd7e5f99SJohn Dyson 	chkb = vm_pageout_page_count -  maxf;
907bd7e5f99SJohn Dyson 	if (chkb) {
908bd7e5f99SJohn Dyson 		for(i = 1; i < chkb;i++) {
909b9b7a4beSMatthew Dillon 			vm_page_t tp;
910b9b7a4beSMatthew Dillon 
9118aef1712SMatthew Dillon 			if ((tp = vm_page_lookup(object, pi - i)) != NULL) {
912bd7e5f99SJohn Dyson 				if ((tp->flags & PG_BUSY) ||
91343b7990eSMatthew Dillon 					((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
91443b7990eSMatthew Dillon 					 (tp->flags & PG_CLEANCHK) == 0) ||
915ffc82b0aSJohn Dyson 					(tp->busy != 0))
916bd7e5f99SJohn Dyson 					break;
9175070c7f8SJohn Dyson 				if ((tp->queue - tp->pc) == PQ_CACHE) {
918e69763a3SDoug Rabson 					vm_page_flag_clear(tp, PG_CLEANCHK);
9193077a9c2SJohn Dyson 					break;
9203077a9c2SJohn Dyson 				}
921bd7e5f99SJohn Dyson 				vm_page_test_dirty(tp);
922bd7e5f99SJohn Dyson 				if ((tp->dirty & tp->valid) == 0) {
923e69763a3SDoug Rabson 					vm_page_flag_clear(tp, PG_CLEANCHK);
924bd7e5f99SJohn Dyson 					break;
925bd7e5f99SJohn Dyson 				}
926bd7e5f99SJohn Dyson 				mab[ i - 1 ] = tp;
927bd7e5f99SJohn Dyson 				maxb++;
928bd7e5f99SJohn Dyson 				continue;
929bd7e5f99SJohn Dyson 			}
930bd7e5f99SJohn Dyson 			break;
931bd7e5f99SJohn Dyson 		}
932bd7e5f99SJohn Dyson 	}
933bd7e5f99SJohn Dyson 
934bd7e5f99SJohn Dyson 	for(i = 0; i < maxb; i++) {
935bd7e5f99SJohn Dyson 		int index = (maxb - i) - 1;
936bd7e5f99SJohn Dyson 		ma[index] = mab[i];
937e69763a3SDoug Rabson 		vm_page_flag_clear(ma[index], PG_CLEANCHK);
938bd7e5f99SJohn Dyson 	}
939e69763a3SDoug Rabson 	vm_page_flag_clear(p, PG_CLEANCHK);
940bd7e5f99SJohn Dyson 	ma[maxb] = p;
941bd7e5f99SJohn Dyson 	for(i = 0; i < maxf; i++) {
942bd7e5f99SJohn Dyson 		int index = (maxb + i) + 1;
943bd7e5f99SJohn Dyson 		ma[index] = maf[i];
944e69763a3SDoug Rabson 		vm_page_flag_clear(ma[index], PG_CLEANCHK);
945f6b04d2bSDavid Greenman 	}
946bd7e5f99SJohn Dyson 	runlen = maxb + maxf + 1;
947cf2819ccSJohn Dyson 
9487a935082SAlan Cox 	vm_pageout_flush(ma, runlen, pagerflags);
949cf2819ccSJohn Dyson 	for (i = 0; i < runlen; i++) {
950cf2819ccSJohn Dyson 		if (ma[i]->valid & ma[i]->dirty) {
951a12cc0e4SAlan Cox 			pmap_page_protect(ma[i], VM_PROT_READ);
952e69763a3SDoug Rabson 			vm_page_flag_set(ma[i], PG_CLEANCHK);
953aef922f5SJohn Dyson 
954b9b7a4beSMatthew Dillon 			/*
955b9b7a4beSMatthew Dillon 			 * maxf will end up being the actual number of pages
956b9b7a4beSMatthew Dillon 			 * we wrote out contiguously, non-inclusive of the
957b9b7a4beSMatthew Dillon 			 * first page.  We do not count look-behind pages.
958b9b7a4beSMatthew Dillon 			 */
959b9b7a4beSMatthew Dillon 			if (i >= maxb + 1 && (maxf > i - maxb - 1))
960b9b7a4beSMatthew Dillon 				maxf = i - maxb - 1;
961b9b7a4beSMatthew Dillon 		}
962b9b7a4beSMatthew Dillon 	}
963b9b7a4beSMatthew Dillon 	return(maxf + 1);
96426f9a767SRodney W. Grimes }
965df8bae1dSRodney W. Grimes 
9661efb74fbSJohn Dyson /*
967950f8459SAlan Cox  * Note that there is absolutely no sense in writing out
968950f8459SAlan Cox  * anonymous objects, so we track down the vnode object
969950f8459SAlan Cox  * to write out.
970950f8459SAlan Cox  * We invalidate (remove) all pages from the address space
971950f8459SAlan Cox  * for semantic correctness.
972950f8459SAlan Cox  *
973950f8459SAlan Cox  * Note: certain anonymous maps, such as MAP_NOSYNC maps,
974950f8459SAlan Cox  * may start out with a NULL object.
975950f8459SAlan Cox  */
976950f8459SAlan Cox void
977950f8459SAlan Cox vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size,
978950f8459SAlan Cox     boolean_t syncio, boolean_t invalidate)
979950f8459SAlan Cox {
980950f8459SAlan Cox 	vm_object_t backing_object;
981950f8459SAlan Cox 	struct vnode *vp;
982950f8459SAlan Cox 	int flags;
983950f8459SAlan Cox 
984950f8459SAlan Cox 	if (object == NULL)
985950f8459SAlan Cox 		return;
986950f8459SAlan Cox 	VM_OBJECT_LOCK(object);
987950f8459SAlan Cox 	while ((backing_object = object->backing_object) != NULL) {
988950f8459SAlan Cox 		VM_OBJECT_LOCK(backing_object);
98956e0670fSAlan Cox 		offset += object->backing_object_offset;
990950f8459SAlan Cox 		VM_OBJECT_UNLOCK(object);
991950f8459SAlan Cox 		object = backing_object;
992950f8459SAlan Cox 		if (object->size < OFF_TO_IDX(offset + size))
993950f8459SAlan Cox 			size = IDX_TO_OFF(object->size) - offset;
994950f8459SAlan Cox 	}
995950f8459SAlan Cox 	/*
996950f8459SAlan Cox 	 * Flush pages if writing is allowed, invalidate them
997950f8459SAlan Cox 	 * if invalidation requested.  Pages undergoing I/O
998950f8459SAlan Cox 	 * will be ignored by vm_object_page_remove().
999950f8459SAlan Cox 	 *
1000950f8459SAlan Cox 	 * We cannot lock the vnode and then wait for paging
1001950f8459SAlan Cox 	 * to complete without deadlocking against vm_fault.
1002950f8459SAlan Cox 	 * Instead we simply call vm_object_page_remove() and
1003950f8459SAlan Cox 	 * allow it to block internally on a page-by-page
1004950f8459SAlan Cox 	 * basis when it encounters pages undergoing async
1005950f8459SAlan Cox 	 * I/O.
1006950f8459SAlan Cox 	 */
1007950f8459SAlan Cox 	if (object->type == OBJT_VNODE &&
1008950f8459SAlan Cox 	    (object->flags & OBJ_MIGHTBEDIRTY) != 0) {
1009ae51ff11SJeff Roberson 		int vfslocked;
1010950f8459SAlan Cox 		vp = object->handle;
1011950f8459SAlan Cox 		VM_OBJECT_UNLOCK(object);
1012ae51ff11SJeff Roberson 		vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1013950f8459SAlan Cox 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
1014950f8459SAlan Cox 		flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
1015950f8459SAlan Cox 		flags |= invalidate ? OBJPC_INVAL : 0;
1016950f8459SAlan Cox 		VM_OBJECT_LOCK(object);
1017950f8459SAlan Cox 		vm_object_page_clean(object,
1018950f8459SAlan Cox 		    OFF_TO_IDX(offset),
1019950f8459SAlan Cox 		    OFF_TO_IDX(offset + size + PAGE_MASK),
1020950f8459SAlan Cox 		    flags);
1021950f8459SAlan Cox 		VM_OBJECT_UNLOCK(object);
1022950f8459SAlan Cox 		VOP_UNLOCK(vp, 0, curthread);
1023ae51ff11SJeff Roberson 		VFS_UNLOCK_GIANT(vfslocked);
1024950f8459SAlan Cox 		VM_OBJECT_LOCK(object);
1025950f8459SAlan Cox 	}
1026950f8459SAlan Cox 	if ((object->type == OBJT_VNODE ||
1027950f8459SAlan Cox 	     object->type == OBJT_DEVICE) && invalidate) {
1028874f0135SDoug Rabson 		boolean_t purge;
1029874f0135SDoug Rabson 		purge = old_msync || (object->type == OBJT_DEVICE);
1030950f8459SAlan Cox 		vm_object_page_remove(object,
1031950f8459SAlan Cox 		    OFF_TO_IDX(offset),
1032950f8459SAlan Cox 		    OFF_TO_IDX(offset + size + PAGE_MASK),
1033874f0135SDoug Rabson 		    purge ? FALSE : TRUE);
1034950f8459SAlan Cox 	}
1035950f8459SAlan Cox 	VM_OBJECT_UNLOCK(object);
1036950f8459SAlan Cox }
1037950f8459SAlan Cox 
1038950f8459SAlan Cox /*
1039867a482dSJohn Dyson  *	vm_object_madvise:
1040867a482dSJohn Dyson  *
1041867a482dSJohn Dyson  *	Implements the madvise function at the object/page level.
10421c7c3c6aSMatthew Dillon  *
1043193b9358SAlan Cox  *	MADV_WILLNEED	(any object)
1044193b9358SAlan Cox  *
1045193b9358SAlan Cox  *	    Activate the specified pages if they are resident.
1046193b9358SAlan Cox  *
1047193b9358SAlan Cox  *	MADV_DONTNEED	(any object)
1048193b9358SAlan Cox  *
1049193b9358SAlan Cox  *	    Deactivate the specified pages if they are resident.
1050193b9358SAlan Cox  *
1051193b9358SAlan Cox  *	MADV_FREE	(OBJT_DEFAULT/OBJT_SWAP objects,
1052193b9358SAlan Cox  *			 OBJ_ONEMAPPING only)
1053193b9358SAlan Cox  *
1054193b9358SAlan Cox  *	    Deactivate and clean the specified pages if they are
1055193b9358SAlan Cox  *	    resident.  This permits the process to reuse the pages
1056193b9358SAlan Cox  *	    without faulting or the kernel to reclaim the pages
1057193b9358SAlan Cox  *	    without I/O.
1058867a482dSJohn Dyson  */
1059867a482dSJohn Dyson void
10601b40f8c0SMatthew Dillon vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
1061867a482dSJohn Dyson {
10626e20a165SJohn Dyson 	vm_pindex_t end, tpindex;
106334567de7SAlan Cox 	vm_object_t backing_object, tobject;
1064867a482dSJohn Dyson 	vm_page_t m;
1065867a482dSJohn Dyson 
1066867a482dSJohn Dyson 	if (object == NULL)
1067867a482dSJohn Dyson 		return;
10689b98b796SAlan Cox 	VM_OBJECT_LOCK(object);
1069867a482dSJohn Dyson 	end = pindex + count;
10701c7c3c6aSMatthew Dillon 	/*
10711c7c3c6aSMatthew Dillon 	 * Locate and adjust resident pages
10721c7c3c6aSMatthew Dillon 	 */
10731c7c3c6aSMatthew Dillon 	for (; pindex < end; pindex += 1) {
10746e20a165SJohn Dyson relookup:
10756e20a165SJohn Dyson 		tobject = object;
10766e20a165SJohn Dyson 		tpindex = pindex;
10776e20a165SJohn Dyson shadowlookup:
107858b4e6ccSAlan Cox 		/*
107958b4e6ccSAlan Cox 		 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
108058b4e6ccSAlan Cox 		 * and those pages must be OBJ_ONEMAPPING.
108158b4e6ccSAlan Cox 		 */
108258b4e6ccSAlan Cox 		if (advise == MADV_FREE) {
108358b4e6ccSAlan Cox 			if ((tobject->type != OBJT_DEFAULT &&
108458b4e6ccSAlan Cox 			     tobject->type != OBJT_SWAP) ||
108558b4e6ccSAlan Cox 			    (tobject->flags & OBJ_ONEMAPPING) == 0) {
108634567de7SAlan Cox 				goto unlock_tobject;
10876e20a165SJohn Dyson 			}
108858b4e6ccSAlan Cox 		}
10891c7c3c6aSMatthew Dillon 		m = vm_page_lookup(tobject, tpindex);
10901c7c3c6aSMatthew Dillon 		if (m == NULL) {
10911ce137beSMatthew Dillon 			/*
10921ce137beSMatthew Dillon 			 * There may be swap even if there is no backing page
10931ce137beSMatthew Dillon 			 */
10941ce137beSMatthew Dillon 			if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
10951ce137beSMatthew Dillon 				swap_pager_freespace(tobject, tpindex, 1);
10961ce137beSMatthew Dillon 			/*
10971ce137beSMatthew Dillon 			 * next object
10981ce137beSMatthew Dillon 			 */
109934567de7SAlan Cox 			backing_object = tobject->backing_object;
110034567de7SAlan Cox 			if (backing_object == NULL)
110134567de7SAlan Cox 				goto unlock_tobject;
110234567de7SAlan Cox 			VM_OBJECT_LOCK(backing_object);
110356e0670fSAlan Cox 			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
11049b98b796SAlan Cox 			if (tobject != object)
110534567de7SAlan Cox 				VM_OBJECT_UNLOCK(tobject);
110634567de7SAlan Cox 			tobject = backing_object;
11076e20a165SJohn Dyson 			goto shadowlookup;
11086e20a165SJohn Dyson 		}
1109867a482dSJohn Dyson 		/*
1110867a482dSJohn Dyson 		 * If the page is busy or not in a normal active state,
11118b03c8edSMatthew Dillon 		 * we skip it.  If the page is not managed there are no
11128b03c8edSMatthew Dillon 		 * page queues to mess with.  Things can break if we mess
11138b03c8edSMatthew Dillon 		 * with pages in any of the below states.
1114867a482dSJohn Dyson 		 */
111532585dd6SAlan Cox 		vm_page_lock_queues();
111632585dd6SAlan Cox 		if (m->hold_count ||
11171c7c3c6aSMatthew Dillon 		    m->wire_count ||
11188b03c8edSMatthew Dillon 		    (m->flags & PG_UNMANAGED) ||
111932585dd6SAlan Cox 		    m->valid != VM_PAGE_BITS_ALL) {
112032585dd6SAlan Cox 			vm_page_unlock_queues();
112134567de7SAlan Cox 			goto unlock_tobject;
11226e20a165SJohn Dyson 		}
11239b98b796SAlan Cox 		if ((m->flags & PG_BUSY) || m->busy) {
11249b98b796SAlan Cox 			vm_page_flag_set(m, PG_WANTED | PG_REFERENCED);
11259b98b796SAlan Cox 			if (object != tobject)
11269b98b796SAlan Cox 				VM_OBJECT_UNLOCK(object);
112734567de7SAlan Cox 			VM_OBJECT_UNLOCK(tobject);
11289b98b796SAlan Cox 			msleep(m, &vm_page_queue_mtx, PDROP | PVM, "madvpo", 0);
11299b98b796SAlan Cox 			VM_OBJECT_LOCK(object);
11306e20a165SJohn Dyson   			goto relookup;
113134567de7SAlan Cox 		}
1132867a482dSJohn Dyson 		if (advise == MADV_WILLNEED) {
1133867a482dSJohn Dyson 			vm_page_activate(m);
11346e20a165SJohn Dyson 		} else if (advise == MADV_DONTNEED) {
1135479112dfSMatthew Dillon 			vm_page_dontneed(m);
11360a47b48bSJohn Dyson 		} else if (advise == MADV_FREE) {
11371c7c3c6aSMatthew Dillon 			/*
11382aaeadf8SMatthew Dillon 			 * Mark the page clean.  This will allow the page
11392aaeadf8SMatthew Dillon 			 * to be freed up by the system.  However, such pages
11402aaeadf8SMatthew Dillon 			 * are often reused quickly by malloc()/free()
11412aaeadf8SMatthew Dillon 			 * so we do not do anything that would cause
11422aaeadf8SMatthew Dillon 			 * a page fault if we can help it.
11432aaeadf8SMatthew Dillon 			 *
11442aaeadf8SMatthew Dillon 			 * Specifically, we do not try to actually free
11452aaeadf8SMatthew Dillon 			 * the page now nor do we try to put it in the
11462aaeadf8SMatthew Dillon 			 * cache (which would cause a page fault on reuse).
114741c67e12SMatthew Dillon 			 *
114841c67e12SMatthew Dillon 			 * But we do make the page is freeable as we
114941c67e12SMatthew Dillon 			 * can without actually taking the step of unmapping
115041c67e12SMatthew Dillon 			 * it.
11511c7c3c6aSMatthew Dillon 			 */
11520385347cSPeter Wemm 			pmap_clear_modify(m);
11536e20a165SJohn Dyson 			m->dirty = 0;
115441c67e12SMatthew Dillon 			m->act_count = 0;
1155479112dfSMatthew Dillon 			vm_page_dontneed(m);
1156867a482dSJohn Dyson 		}
11572999e9faSAlan Cox 		vm_page_unlock_queues();
11582999e9faSAlan Cox 		if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
11592999e9faSAlan Cox 			swap_pager_freespace(tobject, tpindex, 1);
116034567de7SAlan Cox unlock_tobject:
11619b98b796SAlan Cox 		if (tobject != object)
116234567de7SAlan Cox 			VM_OBJECT_UNLOCK(tobject);
1163867a482dSJohn Dyson 	}
11649b98b796SAlan Cox 	VM_OBJECT_UNLOCK(object);
1165867a482dSJohn Dyson }
1166867a482dSJohn Dyson 
1167867a482dSJohn Dyson /*
1168df8bae1dSRodney W. Grimes  *	vm_object_shadow:
1169df8bae1dSRodney W. Grimes  *
1170df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
1171df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
1172df8bae1dSRodney W. Grimes  *	object reference is deallocated.
1173df8bae1dSRodney W. Grimes  *
1174df8bae1dSRodney W. Grimes  *	The new object and offset into that object
1175df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
1176df8bae1dSRodney W. Grimes  */
117726f9a767SRodney W. Grimes void
11781b40f8c0SMatthew Dillon vm_object_shadow(
11791b40f8c0SMatthew Dillon 	vm_object_t *object,	/* IN/OUT */
11801b40f8c0SMatthew Dillon 	vm_ooffset_t *offset,	/* IN/OUT */
11811b40f8c0SMatthew Dillon 	vm_size_t length)
1182df8bae1dSRodney W. Grimes {
1183d031cff1SMatthew Dillon 	vm_object_t source;
1184d031cff1SMatthew Dillon 	vm_object_t result;
1185df8bae1dSRodney W. Grimes 
1186df8bae1dSRodney W. Grimes 	source = *object;
1187df8bae1dSRodney W. Grimes 
1188df8bae1dSRodney W. Grimes 	/*
11899a2f6362SAlan Cox 	 * Don't create the new object if the old object isn't shared.
11909a2f6362SAlan Cox 	 */
1191570a2f4aSAlan Cox 	if (source != NULL) {
1192570a2f4aSAlan Cox 		VM_OBJECT_LOCK(source);
1193570a2f4aSAlan Cox 		if (source->ref_count == 1 &&
11949a2f6362SAlan Cox 		    source->handle == NULL &&
11959a2f6362SAlan Cox 		    (source->type == OBJT_DEFAULT ||
11969917e010SAlan Cox 		     source->type == OBJT_SWAP)) {
1197570a2f4aSAlan Cox 			VM_OBJECT_UNLOCK(source);
11989a2f6362SAlan Cox 			return;
11999917e010SAlan Cox 		}
1200570a2f4aSAlan Cox 		VM_OBJECT_UNLOCK(source);
1201570a2f4aSAlan Cox 	}
12029a2f6362SAlan Cox 
12039a2f6362SAlan Cox 	/*
1204570a2f4aSAlan Cox 	 * Allocate a new object with the given length.
1205df8bae1dSRodney W. Grimes 	 */
1206971dd342SAlfred Perlstein 	result = vm_object_allocate(OBJT_DEFAULT, length);
1207df8bae1dSRodney W. Grimes 
1208df8bae1dSRodney W. Grimes 	/*
12090d94caffSDavid Greenman 	 * The new object shadows the source object, adding a reference to it.
12100d94caffSDavid Greenman 	 * Our caller changes his reference to point to the new object,
12110d94caffSDavid Greenman 	 * removing a reference to the source object.  Net result: no change
12120d94caffSDavid Greenman 	 * of reference count.
12139b09fe24SMatthew Dillon 	 *
12149b09fe24SMatthew Dillon 	 * Try to optimize the result object's page color when shadowing
1215956f3135SPhilippe Charnier 	 * in order to maintain page coloring consistency in the combined
12169b09fe24SMatthew Dillon 	 * shadowed object.
1217df8bae1dSRodney W. Grimes 	 */
121824a1cce3SDavid Greenman 	result->backing_object = source;
12199174ca7bSTor Egge 	/*
12209174ca7bSTor Egge 	 * Store the offset into the source object, and fix up the offset into
12219174ca7bSTor Egge 	 * the new object.
12229174ca7bSTor Egge 	 */
12239174ca7bSTor Egge 	result->backing_object_offset = *offset;
1224570a2f4aSAlan Cox 	if (source != NULL) {
1225570a2f4aSAlan Cox 		VM_OBJECT_LOCK(source);
12261c500307SAlan Cox 		LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1227eaf13dd7SJohn Dyson 		source->shadow_count++;
1228eaf13dd7SJohn Dyson 		source->generation++;
1229dcc5840eSAlan Cox 		if (length < source->size)
1230dcc5840eSAlan Cox 			length = source->size;
1231ef39c05bSAlexander Leidinger 		if (length > PQ_MAXLENGTH || source->generation > 1)
1232ef39c05bSAlexander Leidinger 			length = PQ_MAXLENGTH;
1233dcc5840eSAlan Cox 		result->pg_color = (source->pg_color +
1234ef39c05bSAlexander Leidinger 		    length * source->generation) & PQ_COLORMASK;
1235ed4fe4f4SJeff Roberson 		result->flags |= source->flags & OBJ_NEEDGIANT;
1236570a2f4aSAlan Cox 		VM_OBJECT_UNLOCK(source);
1237ef39c05bSAlexander Leidinger 		next_index = (result->pg_color + PQ_MAXLENGTH) & PQ_COLORMASK;
1238de5f6a77SJohn Dyson 	}
1239df8bae1dSRodney W. Grimes 
1240df8bae1dSRodney W. Grimes 
1241df8bae1dSRodney W. Grimes 	/*
1242df8bae1dSRodney W. Grimes 	 * Return the new things
1243df8bae1dSRodney W. Grimes 	 */
1244df8bae1dSRodney W. Grimes 	*offset = 0;
1245df8bae1dSRodney W. Grimes 	*object = result;
1246df8bae1dSRodney W. Grimes }
1247df8bae1dSRodney W. Grimes 
1248c5aaa06dSAlan Cox /*
1249c5aaa06dSAlan Cox  *	vm_object_split:
1250c5aaa06dSAlan Cox  *
1251c5aaa06dSAlan Cox  * Split the pages in a map entry into a new object.  This affords
1252c5aaa06dSAlan Cox  * easier removal of unused pages, and keeps object inheritance from
1253c5aaa06dSAlan Cox  * being a negative impact on memory usage.
1254c5aaa06dSAlan Cox  */
1255c5aaa06dSAlan Cox void
1256c5aaa06dSAlan Cox vm_object_split(vm_map_entry_t entry)
1257c5aaa06dSAlan Cox {
1258c5aaa06dSAlan Cox 	vm_page_t m;
1259c5aaa06dSAlan Cox 	vm_object_t orig_object, new_object, source;
126023f09d50SIan Dowse 	vm_pindex_t offidxstart, offidxend;
126123f09d50SIan Dowse 	vm_size_t idx, size;
1262c5aaa06dSAlan Cox 
1263c5aaa06dSAlan Cox 	orig_object = entry->object.vm_object;
1264c5aaa06dSAlan Cox 	if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1265c5aaa06dSAlan Cox 		return;
1266c5aaa06dSAlan Cox 	if (orig_object->ref_count <= 1)
1267c5aaa06dSAlan Cox 		return;
12684da9f125SAlan Cox 	VM_OBJECT_UNLOCK(orig_object);
1269c5aaa06dSAlan Cox 
12704da9f125SAlan Cox 	offidxstart = OFF_TO_IDX(entry->offset);
12714da9f125SAlan Cox 	offidxend = offidxstart + OFF_TO_IDX(entry->end - entry->start);
1272c5aaa06dSAlan Cox 	size = offidxend - offidxstart;
1273c5aaa06dSAlan Cox 
12744da9f125SAlan Cox 	/*
12754da9f125SAlan Cox 	 * If swap_pager_copy() is later called, it will convert new_object
12764da9f125SAlan Cox 	 * into a swap object.
12774da9f125SAlan Cox 	 */
12784da9f125SAlan Cox 	new_object = vm_object_allocate(OBJT_DEFAULT, size);
1279c5aaa06dSAlan Cox 
128063f6cefcSAlan Cox 	VM_OBJECT_LOCK(new_object);
128163f6cefcSAlan Cox 	VM_OBJECT_LOCK(orig_object);
1282c5aaa06dSAlan Cox 	source = orig_object->backing_object;
1283c5aaa06dSAlan Cox 	if (source != NULL) {
12848e3a76fbSAlan Cox 		VM_OBJECT_LOCK(source);
12851c500307SAlan Cox 		LIST_INSERT_HEAD(&source->shadow_head,
1286c5aaa06dSAlan Cox 				  new_object, shadow_list);
12878e3a76fbSAlan Cox 		source->shadow_count++;
12888e3a76fbSAlan Cox 		source->generation++;
1289b921a12bSAlan Cox 		vm_object_reference_locked(source);	/* for new_object */
1290c5aaa06dSAlan Cox 		vm_object_clear_flag(source, OBJ_ONEMAPPING);
1291e2479b4fSAlan Cox 		VM_OBJECT_UNLOCK(source);
1292c5aaa06dSAlan Cox 		new_object->backing_object_offset =
12934da9f125SAlan Cox 			orig_object->backing_object_offset + entry->offset;
1294c5aaa06dSAlan Cox 		new_object->backing_object = source;
1295c5aaa06dSAlan Cox 	}
1296b8a0b997SJeff Roberson 	new_object->flags |= orig_object->flags & OBJ_NEEDGIANT;
12975ba514bcSAlan Cox 	vm_page_lock_queues();
1298c5aaa06dSAlan Cox 	for (idx = 0; idx < size; idx++) {
1299c5aaa06dSAlan Cox 	retry:
1300c5aaa06dSAlan Cox 		m = vm_page_lookup(orig_object, offidxstart + idx);
1301c5aaa06dSAlan Cox 		if (m == NULL)
1302c5aaa06dSAlan Cox 			continue;
1303c5aaa06dSAlan Cox 
1304c5aaa06dSAlan Cox 		/*
1305c5aaa06dSAlan Cox 		 * We must wait for pending I/O to complete before we can
1306c5aaa06dSAlan Cox 		 * rename the page.
1307c5aaa06dSAlan Cox 		 *
1308c5aaa06dSAlan Cox 		 * We do not have to VM_PROT_NONE the page as mappings should
1309c5aaa06dSAlan Cox 		 * not be changed by this operation.
1310c5aaa06dSAlan Cox 		 */
1311de33beddSAlan Cox 		if ((m->flags & PG_BUSY) || m->busy) {
1312de33beddSAlan Cox 			vm_page_flag_set(m, PG_WANTED | PG_REFERENCED);
1313de33beddSAlan Cox 			VM_OBJECT_UNLOCK(orig_object);
1314de33beddSAlan Cox 			VM_OBJECT_UNLOCK(new_object);
1315de33beddSAlan Cox 			msleep(m, &vm_page_queue_mtx, PDROP | PVM, "spltwt", 0);
1316de33beddSAlan Cox 			VM_OBJECT_LOCK(new_object);
1317de33beddSAlan Cox 			VM_OBJECT_LOCK(orig_object);
13185ba514bcSAlan Cox 			vm_page_lock_queues();
1319c5aaa06dSAlan Cox 			goto retry;
1320de33beddSAlan Cox 		}
1321c5aaa06dSAlan Cox 		vm_page_rename(m, new_object, idx);
1322c5aaa06dSAlan Cox 		/* page automatically made dirty by rename and cache handled */
1323c5aaa06dSAlan Cox 		vm_page_busy(m);
1324c5aaa06dSAlan Cox 	}
13255ba514bcSAlan Cox 	vm_page_unlock_queues();
1326d7a013c3SAlan Cox 	if (orig_object->type == OBJT_SWAP) {
1327c5aaa06dSAlan Cox 		/*
1328c7c8dd7eSAlan Cox 		 * swap_pager_copy() can sleep, in which case the orig_object's
1329c7c8dd7eSAlan Cox 		 * and new_object's locks are released and reacquired.
1330c5aaa06dSAlan Cox 		 */
1331c5aaa06dSAlan Cox 		swap_pager_copy(orig_object, new_object, offidxstart, 0);
1332c5aaa06dSAlan Cox 	}
1333d7a013c3SAlan Cox 	VM_OBJECT_UNLOCK(orig_object);
1334dc907f66SAlan Cox 	vm_page_lock_queues();
1335c7118ed6SAlan Cox 	TAILQ_FOREACH(m, &new_object->memq, listq)
1336c5aaa06dSAlan Cox 		vm_page_wakeup(m);
1337dc907f66SAlan Cox 	vm_page_unlock_queues();
1338c7c8dd7eSAlan Cox 	VM_OBJECT_UNLOCK(new_object);
1339c5aaa06dSAlan Cox 	entry->object.vm_object = new_object;
1340c5aaa06dSAlan Cox 	entry->offset = 0LL;
1341c5aaa06dSAlan Cox 	vm_object_deallocate(orig_object);
13424da9f125SAlan Cox 	VM_OBJECT_LOCK(new_object);
1343c5aaa06dSAlan Cox }
1344c5aaa06dSAlan Cox 
13452ad1a3f7SMatthew Dillon #define	OBSC_TEST_ALL_SHADOWED	0x0001
13462ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_NOWAIT	0x0002
13472ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_WAIT	0x0004
13482ad1a3f7SMatthew Dillon 
1349b4ae4780SPoul-Henning Kamp static int
13502ad1a3f7SMatthew Dillon vm_object_backing_scan(vm_object_t object, int op)
13512ad1a3f7SMatthew Dillon {
13522ad1a3f7SMatthew Dillon 	int r = 1;
13532ad1a3f7SMatthew Dillon 	vm_page_t p;
13542ad1a3f7SMatthew Dillon 	vm_object_t backing_object;
13552ad1a3f7SMatthew Dillon 	vm_pindex_t backing_offset_index;
13562ad1a3f7SMatthew Dillon 
13577ca33ad1SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
13587ca33ad1SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object->backing_object, MA_OWNED);
13592ad1a3f7SMatthew Dillon 
13602ad1a3f7SMatthew Dillon 	backing_object = object->backing_object;
13612ad1a3f7SMatthew Dillon 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
13622ad1a3f7SMatthew Dillon 
13632ad1a3f7SMatthew Dillon 	/*
13642ad1a3f7SMatthew Dillon 	 * Initial conditions
13652ad1a3f7SMatthew Dillon 	 */
13662ad1a3f7SMatthew Dillon 	if (op & OBSC_TEST_ALL_SHADOWED) {
13672ad1a3f7SMatthew Dillon 		/*
1368956f3135SPhilippe Charnier 		 * We do not want to have to test for the existence of
13692ad1a3f7SMatthew Dillon 		 * swap pages in the backing object.  XXX but with the
13702ad1a3f7SMatthew Dillon 		 * new swapper this would be pretty easy to do.
13712ad1a3f7SMatthew Dillon 		 *
13722ad1a3f7SMatthew Dillon 		 * XXX what about anonymous MAP_SHARED memory that hasn't
13732ad1a3f7SMatthew Dillon 		 * been ZFOD faulted yet?  If we do not test for this, the
13742ad1a3f7SMatthew Dillon 		 * shadow test may succeed! XXX
13752ad1a3f7SMatthew Dillon 		 */
13762ad1a3f7SMatthew Dillon 		if (backing_object->type != OBJT_DEFAULT) {
13772ad1a3f7SMatthew Dillon 			return (0);
13782ad1a3f7SMatthew Dillon 		}
13792ad1a3f7SMatthew Dillon 	}
13802ad1a3f7SMatthew Dillon 	if (op & OBSC_COLLAPSE_WAIT) {
13812ad1a3f7SMatthew Dillon 		vm_object_set_flag(backing_object, OBJ_DEAD);
13822ad1a3f7SMatthew Dillon 	}
13832ad1a3f7SMatthew Dillon 
13842ad1a3f7SMatthew Dillon 	/*
13852ad1a3f7SMatthew Dillon 	 * Our scan
13862ad1a3f7SMatthew Dillon 	 */
13872ad1a3f7SMatthew Dillon 	p = TAILQ_FIRST(&backing_object->memq);
13882ad1a3f7SMatthew Dillon 	while (p) {
13892ad1a3f7SMatthew Dillon 		vm_page_t next = TAILQ_NEXT(p, listq);
13902ad1a3f7SMatthew Dillon 		vm_pindex_t new_pindex = p->pindex - backing_offset_index;
13912ad1a3f7SMatthew Dillon 
13922ad1a3f7SMatthew Dillon 		if (op & OBSC_TEST_ALL_SHADOWED) {
13932ad1a3f7SMatthew Dillon 			vm_page_t pp;
13942ad1a3f7SMatthew Dillon 
13952ad1a3f7SMatthew Dillon 			/*
13962ad1a3f7SMatthew Dillon 			 * Ignore pages outside the parent object's range
13972ad1a3f7SMatthew Dillon 			 * and outside the parent object's mapping of the
13982ad1a3f7SMatthew Dillon 			 * backing object.
13992ad1a3f7SMatthew Dillon 			 *
14002ad1a3f7SMatthew Dillon 			 * note that we do not busy the backing object's
14012ad1a3f7SMatthew Dillon 			 * page.
14022ad1a3f7SMatthew Dillon 			 */
14032ad1a3f7SMatthew Dillon 			if (
14042ad1a3f7SMatthew Dillon 			    p->pindex < backing_offset_index ||
14052ad1a3f7SMatthew Dillon 			    new_pindex >= object->size
14062ad1a3f7SMatthew Dillon 			) {
14072ad1a3f7SMatthew Dillon 				p = next;
14082ad1a3f7SMatthew Dillon 				continue;
14092ad1a3f7SMatthew Dillon 			}
14102ad1a3f7SMatthew Dillon 
14112ad1a3f7SMatthew Dillon 			/*
14122ad1a3f7SMatthew Dillon 			 * See if the parent has the page or if the parent's
14132ad1a3f7SMatthew Dillon 			 * object pager has the page.  If the parent has the
14142ad1a3f7SMatthew Dillon 			 * page but the page is not valid, the parent's
14152ad1a3f7SMatthew Dillon 			 * object pager must have the page.
14162ad1a3f7SMatthew Dillon 			 *
14172ad1a3f7SMatthew Dillon 			 * If this fails, the parent does not completely shadow
14182ad1a3f7SMatthew Dillon 			 * the object and we might as well give up now.
14192ad1a3f7SMatthew Dillon 			 */
14202ad1a3f7SMatthew Dillon 
14212ad1a3f7SMatthew Dillon 			pp = vm_page_lookup(object, new_pindex);
14222ad1a3f7SMatthew Dillon 			if (
14232ad1a3f7SMatthew Dillon 			    (pp == NULL || pp->valid == 0) &&
14242ad1a3f7SMatthew Dillon 			    !vm_pager_has_page(object, new_pindex, NULL, NULL)
14252ad1a3f7SMatthew Dillon 			) {
14262ad1a3f7SMatthew Dillon 				r = 0;
14272ad1a3f7SMatthew Dillon 				break;
14282ad1a3f7SMatthew Dillon 			}
14292ad1a3f7SMatthew Dillon 		}
14302ad1a3f7SMatthew Dillon 
14312ad1a3f7SMatthew Dillon 		/*
14322ad1a3f7SMatthew Dillon 		 * Check for busy page
14332ad1a3f7SMatthew Dillon 		 */
14342ad1a3f7SMatthew Dillon 		if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
14352ad1a3f7SMatthew Dillon 			vm_page_t pp;
14362ad1a3f7SMatthew Dillon 
14372ad1a3f7SMatthew Dillon 			if (op & OBSC_COLLAPSE_NOWAIT) {
143800f9e8b4SAlan Cox 				if ((p->flags & PG_BUSY) ||
14392ad1a3f7SMatthew Dillon 				    !p->valid ||
144000f9e8b4SAlan Cox 				    p->busy) {
14412ad1a3f7SMatthew Dillon 					p = next;
14422ad1a3f7SMatthew Dillon 					continue;
14432ad1a3f7SMatthew Dillon 				}
14442ad1a3f7SMatthew Dillon 			} else if (op & OBSC_COLLAPSE_WAIT) {
14457ca33ad1SAlan Cox 				if ((p->flags & PG_BUSY) || p->busy) {
1446c6ec6a7cSAlan Cox 					vm_page_lock_queues();
14477ca33ad1SAlan Cox 					vm_page_flag_set(p,
14487ca33ad1SAlan Cox 					    PG_WANTED | PG_REFERENCED);
14497ca33ad1SAlan Cox 					VM_OBJECT_UNLOCK(backing_object);
14507ca33ad1SAlan Cox 					VM_OBJECT_UNLOCK(object);
14517ca33ad1SAlan Cox 					msleep(p, &vm_page_queue_mtx,
14527ca33ad1SAlan Cox 					    PDROP | PVM, "vmocol", 0);
14537ca33ad1SAlan Cox 					VM_OBJECT_LOCK(object);
14547ca33ad1SAlan Cox 					VM_OBJECT_LOCK(backing_object);
14552ad1a3f7SMatthew Dillon 					/*
14562ad1a3f7SMatthew Dillon 					 * If we slept, anything could have
14572ad1a3f7SMatthew Dillon 					 * happened.  Since the object is
14582ad1a3f7SMatthew Dillon 					 * marked dead, the backing offset
14592ad1a3f7SMatthew Dillon 					 * should not have changed so we
14602ad1a3f7SMatthew Dillon 					 * just restart our scan.
14612ad1a3f7SMatthew Dillon 					 */
14622ad1a3f7SMatthew Dillon 					p = TAILQ_FIRST(&backing_object->memq);
14632ad1a3f7SMatthew Dillon 					continue;
14642ad1a3f7SMatthew Dillon 				}
14652ad1a3f7SMatthew Dillon 			}
14662ad1a3f7SMatthew Dillon 
14672ad1a3f7SMatthew Dillon 			KASSERT(
14682ad1a3f7SMatthew Dillon 			    p->object == backing_object,
14698e99783bSAlan Cox 			    ("vm_object_backing_scan: object mismatch")
14702ad1a3f7SMatthew Dillon 			);
14712ad1a3f7SMatthew Dillon 
14722ad1a3f7SMatthew Dillon 			/*
14732ad1a3f7SMatthew Dillon 			 * Destroy any associated swap
14742ad1a3f7SMatthew Dillon 			 */
14752ad1a3f7SMatthew Dillon 			if (backing_object->type == OBJT_SWAP) {
14762ad1a3f7SMatthew Dillon 				swap_pager_freespace(
14772ad1a3f7SMatthew Dillon 				    backing_object,
14782ad1a3f7SMatthew Dillon 				    p->pindex,
14792ad1a3f7SMatthew Dillon 				    1
14802ad1a3f7SMatthew Dillon 				);
14812ad1a3f7SMatthew Dillon 			}
14822ad1a3f7SMatthew Dillon 
14832ad1a3f7SMatthew Dillon 			if (
14842ad1a3f7SMatthew Dillon 			    p->pindex < backing_offset_index ||
14852ad1a3f7SMatthew Dillon 			    new_pindex >= object->size
14862ad1a3f7SMatthew Dillon 			) {
14872ad1a3f7SMatthew Dillon 				/*
14882ad1a3f7SMatthew Dillon 				 * Page is out of the parent object's range, we
14892ad1a3f7SMatthew Dillon 				 * can simply destroy it.
14902ad1a3f7SMatthew Dillon 				 */
14916a684ecfSAlan Cox 				vm_page_lock_queues();
1492f6d89838SAlan Cox 				KASSERT(!pmap_page_is_mapped(p),
1493f6d89838SAlan Cox 				    ("freeing mapped page %p", p));
1494f6d89838SAlan Cox 				if (p->wire_count == 0)
14952ad1a3f7SMatthew Dillon 					vm_page_free(p);
1496f6d89838SAlan Cox 				else
1497f6d89838SAlan Cox 					vm_page_remove(p);
14986a684ecfSAlan Cox 				vm_page_unlock_queues();
14992ad1a3f7SMatthew Dillon 				p = next;
15002ad1a3f7SMatthew Dillon 				continue;
15012ad1a3f7SMatthew Dillon 			}
15022ad1a3f7SMatthew Dillon 
15032ad1a3f7SMatthew Dillon 			pp = vm_page_lookup(object, new_pindex);
15042ad1a3f7SMatthew Dillon 			if (
15052ad1a3f7SMatthew Dillon 			    pp != NULL ||
15062ad1a3f7SMatthew Dillon 			    vm_pager_has_page(object, new_pindex, NULL, NULL)
15072ad1a3f7SMatthew Dillon 			) {
15082ad1a3f7SMatthew Dillon 				/*
15092ad1a3f7SMatthew Dillon 				 * page already exists in parent OR swap exists
15102ad1a3f7SMatthew Dillon 				 * for this location in the parent.  Destroy
15112ad1a3f7SMatthew Dillon 				 * the original page from the backing object.
15122ad1a3f7SMatthew Dillon 				 *
15132ad1a3f7SMatthew Dillon 				 * Leave the parent's page alone
15142ad1a3f7SMatthew Dillon 				 */
15156a684ecfSAlan Cox 				vm_page_lock_queues();
1516f6d89838SAlan Cox 				KASSERT(!pmap_page_is_mapped(p),
1517f6d89838SAlan Cox 				    ("freeing mapped page %p", p));
1518f6d89838SAlan Cox 				if (p->wire_count == 0)
15192ad1a3f7SMatthew Dillon 					vm_page_free(p);
1520f6d89838SAlan Cox 				else
1521f6d89838SAlan Cox 					vm_page_remove(p);
15226a684ecfSAlan Cox 				vm_page_unlock_queues();
15232ad1a3f7SMatthew Dillon 				p = next;
15242ad1a3f7SMatthew Dillon 				continue;
15252ad1a3f7SMatthew Dillon 			}
15262ad1a3f7SMatthew Dillon 
15272ad1a3f7SMatthew Dillon 			/*
15282ad1a3f7SMatthew Dillon 			 * Page does not exist in parent, rename the
15292ad1a3f7SMatthew Dillon 			 * page from the backing object to the main object.
1530d1bf5d56SMatthew Dillon 			 *
1531d1bf5d56SMatthew Dillon 			 * If the page was mapped to a process, it can remain
1532d1bf5d56SMatthew Dillon 			 * mapped through the rename.
15332ad1a3f7SMatthew Dillon 			 */
1534a28cc55eSAlan Cox 			vm_page_lock_queues();
15352ad1a3f7SMatthew Dillon 			vm_page_rename(p, object, new_pindex);
1536a28cc55eSAlan Cox 			vm_page_unlock_queues();
15372ad1a3f7SMatthew Dillon 			/* page automatically made dirty by rename */
15382ad1a3f7SMatthew Dillon 		}
15392ad1a3f7SMatthew Dillon 		p = next;
15402ad1a3f7SMatthew Dillon 	}
15412ad1a3f7SMatthew Dillon 	return (r);
15422ad1a3f7SMatthew Dillon }
15432ad1a3f7SMatthew Dillon 
1544df8bae1dSRodney W. Grimes 
1545df8bae1dSRodney W. Grimes /*
15462fe6e4d7SDavid Greenman  * this version of collapse allows the operation to occur earlier and
15472fe6e4d7SDavid Greenman  * when paging_in_progress is true for an object...  This is not a complete
15482fe6e4d7SDavid Greenman  * operation, but should plug 99.9% of the rest of the leaks.
15492fe6e4d7SDavid Greenman  */
15502fe6e4d7SDavid Greenman static void
15511b40f8c0SMatthew Dillon vm_object_qcollapse(vm_object_t object)
15522fe6e4d7SDavid Greenman {
15532ad1a3f7SMatthew Dillon 	vm_object_t backing_object = object->backing_object;
15542fe6e4d7SDavid Greenman 
155506ecade7SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
155606ecade7SAlan Cox 	VM_OBJECT_LOCK_ASSERT(backing_object, MA_OWNED);
15571b40f8c0SMatthew Dillon 
15582fe6e4d7SDavid Greenman 	if (backing_object->ref_count != 1)
15592fe6e4d7SDavid Greenman 		return;
15602fe6e4d7SDavid Greenman 
15612ad1a3f7SMatthew Dillon 	vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
15622fe6e4d7SDavid Greenman }
15632fe6e4d7SDavid Greenman 
1564df8bae1dSRodney W. Grimes /*
1565df8bae1dSRodney W. Grimes  *	vm_object_collapse:
1566df8bae1dSRodney W. Grimes  *
1567df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
1568df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
1569df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
1570df8bae1dSRodney W. Grimes  */
157126f9a767SRodney W. Grimes void
15721b40f8c0SMatthew Dillon vm_object_collapse(vm_object_t object)
1573df8bae1dSRodney W. Grimes {
1574d7fc2210SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
157523955314SAlfred Perlstein 
1576df8bae1dSRodney W. Grimes 	while (TRUE) {
15772ad1a3f7SMatthew Dillon 		vm_object_t backing_object;
15782ad1a3f7SMatthew Dillon 
1579df8bae1dSRodney W. Grimes 		/*
1580df8bae1dSRodney W. Grimes 		 * Verify that the conditions are right for collapse:
1581df8bae1dSRodney W. Grimes 		 *
15822ad1a3f7SMatthew Dillon 		 * The object exists and the backing object exists.
1583df8bae1dSRodney W. Grimes 		 */
158424a1cce3SDavid Greenman 		if ((backing_object = object->backing_object) == NULL)
15852ad1a3f7SMatthew Dillon 			break;
1586df8bae1dSRodney W. Grimes 
1587f919ebdeSDavid Greenman 		/*
1588f919ebdeSDavid Greenman 		 * we check the backing object first, because it is most likely
158924a1cce3SDavid Greenman 		 * not collapsable.
1590f919ebdeSDavid Greenman 		 */
159140b808a8SAlan Cox 		VM_OBJECT_LOCK(backing_object);
159224a1cce3SDavid Greenman 		if (backing_object->handle != NULL ||
159324a1cce3SDavid Greenman 		    (backing_object->type != OBJT_DEFAULT &&
159424a1cce3SDavid Greenman 		     backing_object->type != OBJT_SWAP) ||
1595f919ebdeSDavid Greenman 		    (backing_object->flags & OBJ_DEAD) ||
159624a1cce3SDavid Greenman 		    object->handle != NULL ||
159724a1cce3SDavid Greenman 		    (object->type != OBJT_DEFAULT &&
159824a1cce3SDavid Greenman 		     object->type != OBJT_SWAP) ||
159924a1cce3SDavid Greenman 		    (object->flags & OBJ_DEAD)) {
160040b808a8SAlan Cox 			VM_OBJECT_UNLOCK(backing_object);
16012ad1a3f7SMatthew Dillon 			break;
160224a1cce3SDavid Greenman 		}
16039b4814bbSDavid Greenman 
16042ad1a3f7SMatthew Dillon 		if (
16052ad1a3f7SMatthew Dillon 		    object->paging_in_progress != 0 ||
16062ad1a3f7SMatthew Dillon 		    backing_object->paging_in_progress != 0
16072ad1a3f7SMatthew Dillon 		) {
1608b9921222SDavid Greenman 			vm_object_qcollapse(object);
160940b808a8SAlan Cox 			VM_OBJECT_UNLOCK(backing_object);
16102ad1a3f7SMatthew Dillon 			break;
1611df8bae1dSRodney W. Grimes 		}
161226f9a767SRodney W. Grimes 		/*
16130d94caffSDavid Greenman 		 * We know that we can either collapse the backing object (if
16142ad1a3f7SMatthew Dillon 		 * the parent is the only reference to it) or (perhaps) have
16152ad1a3f7SMatthew Dillon 		 * the parent bypass the object if the parent happens to shadow
16162ad1a3f7SMatthew Dillon 		 * all the resident pages in the entire backing object.
16172ad1a3f7SMatthew Dillon 		 *
16182ad1a3f7SMatthew Dillon 		 * This is ignoring pager-backed pages such as swap pages.
16192ad1a3f7SMatthew Dillon 		 * vm_object_backing_scan fails the shadowing test in this
16202ad1a3f7SMatthew Dillon 		 * case.
1621df8bae1dSRodney W. Grimes 		 */
1622df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
1623df8bae1dSRodney W. Grimes 			/*
16242ad1a3f7SMatthew Dillon 			 * If there is exactly one reference to the backing
16252ad1a3f7SMatthew Dillon 			 * object, we can collapse it into the parent.
1626df8bae1dSRodney W. Grimes 			 */
16272ad1a3f7SMatthew Dillon 			vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1628df8bae1dSRodney W. Grimes 
1629df8bae1dSRodney W. Grimes 			/*
1630df8bae1dSRodney W. Grimes 			 * Move the pager from backing_object to object.
1631df8bae1dSRodney W. Grimes 			 */
16326be36525SAlan Cox 			if (backing_object->type == OBJT_SWAP) {
163324a1cce3SDavid Greenman 				/*
1634c7c8dd7eSAlan Cox 				 * swap_pager_copy() can sleep, in which case
1635c7c8dd7eSAlan Cox 				 * the backing_object's and object's locks are
1636c7c8dd7eSAlan Cox 				 * released and reacquired.
163724a1cce3SDavid Greenman 				 */
16381c7c3c6aSMatthew Dillon 				swap_pager_copy(
16391c7c3c6aSMatthew Dillon 				    backing_object,
16401c7c3c6aSMatthew Dillon 				    object,
16411c7c3c6aSMatthew Dillon 				    OFF_TO_IDX(object->backing_object_offset), TRUE);
1642c0503609SDavid Greenman 			}
1643df8bae1dSRodney W. Grimes 			/*
1644df8bae1dSRodney W. Grimes 			 * Object now shadows whatever backing_object did.
16452ad1a3f7SMatthew Dillon 			 * Note that the reference to
16462ad1a3f7SMatthew Dillon 			 * backing_object->backing_object moves from within
16472ad1a3f7SMatthew Dillon 			 * backing_object to within object.
1648df8bae1dSRodney W. Grimes 			 */
16491c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
16504f7c7f6eSAlan Cox 			backing_object->shadow_count--;
16514f7c7f6eSAlan Cox 			backing_object->generation++;
1652de5f6a77SJohn Dyson 			if (backing_object->backing_object) {
16536be36525SAlan Cox 				VM_OBJECT_LOCK(backing_object->backing_object);
16541c500307SAlan Cox 				LIST_REMOVE(backing_object, shadow_list);
165543186e53SAlan Cox 				LIST_INSERT_HEAD(
165643186e53SAlan Cox 				    &backing_object->backing_object->shadow_head,
165743186e53SAlan Cox 				    object, shadow_list);
165843186e53SAlan Cox 				/*
165943186e53SAlan Cox 				 * The shadow_count has not changed.
166043186e53SAlan Cox 				 */
1661eaf13dd7SJohn Dyson 				backing_object->backing_object->generation++;
16626be36525SAlan Cox 				VM_OBJECT_UNLOCK(backing_object->backing_object);
1663de5f6a77SJohn Dyson 			}
166424a1cce3SDavid Greenman 			object->backing_object = backing_object->backing_object;
16652ad1a3f7SMatthew Dillon 			object->backing_object_offset +=
16662ad1a3f7SMatthew Dillon 			    backing_object->backing_object_offset;
16672ad1a3f7SMatthew Dillon 
1668df8bae1dSRodney W. Grimes 			/*
1669df8bae1dSRodney W. Grimes 			 * Discard backing_object.
1670df8bae1dSRodney W. Grimes 			 *
16710d94caffSDavid Greenman 			 * Since the backing object has no pages, no pager left,
16720d94caffSDavid Greenman 			 * and no object references within it, all that is
16730d94caffSDavid Greenman 			 * necessary is to dispose of it.
1674df8bae1dSRodney W. Grimes 			 */
1675245df27cSMatthew Dillon 			KASSERT(backing_object->ref_count == 1, ("backing_object %p was somehow re-referenced during collapse!", backing_object));
16766be36525SAlan Cox 			VM_OBJECT_UNLOCK(backing_object);
1677df8bae1dSRodney W. Grimes 
1678a5698387SAlan Cox 			mtx_lock(&vm_object_list_mtx);
16792ad1a3f7SMatthew Dillon 			TAILQ_REMOVE(
16802ad1a3f7SMatthew Dillon 			    &vm_object_list,
16812ad1a3f7SMatthew Dillon 			    backing_object,
16822ad1a3f7SMatthew Dillon 			    object_list
16832ad1a3f7SMatthew Dillon 			);
1684a5698387SAlan Cox 			mtx_unlock(&vm_object_list_mtx);
1685df8bae1dSRodney W. Grimes 
1686670d17b5SJeff Roberson 			uma_zfree(obj_zone, backing_object);
1687df8bae1dSRodney W. Grimes 
1688df8bae1dSRodney W. Grimes 			object_collapses++;
16890d94caffSDavid Greenman 		} else {
169095e5e988SJohn Dyson 			vm_object_t new_backing_object;
1691df8bae1dSRodney W. Grimes 
1692df8bae1dSRodney W. Grimes 			/*
16932ad1a3f7SMatthew Dillon 			 * If we do not entirely shadow the backing object,
16942ad1a3f7SMatthew Dillon 			 * there is nothing we can do so we give up.
1695df8bae1dSRodney W. Grimes 			 */
16962ad1a3f7SMatthew Dillon 			if (vm_object_backing_scan(object, OBSC_TEST_ALL_SHADOWED) == 0) {
169740b808a8SAlan Cox 				VM_OBJECT_UNLOCK(backing_object);
16982ad1a3f7SMatthew Dillon 				break;
169924a1cce3SDavid Greenman 			}
1700df8bae1dSRodney W. Grimes 
1701df8bae1dSRodney W. Grimes 			/*
17020d94caffSDavid Greenman 			 * Make the parent shadow the next object in the
17030d94caffSDavid Greenman 			 * chain.  Deallocating backing_object will not remove
17040d94caffSDavid Greenman 			 * it, since its reference count is at least 2.
1705df8bae1dSRodney W. Grimes 			 */
17061c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
1707eaf13dd7SJohn Dyson 			backing_object->shadow_count--;
1708eaf13dd7SJohn Dyson 			backing_object->generation++;
170995e5e988SJohn Dyson 
171095e5e988SJohn Dyson 			new_backing_object = backing_object->backing_object;
17118aef1712SMatthew Dillon 			if ((object->backing_object = new_backing_object) != NULL) {
17126be36525SAlan Cox 				VM_OBJECT_LOCK(new_backing_object);
17131c500307SAlan Cox 				LIST_INSERT_HEAD(
17142ad1a3f7SMatthew Dillon 				    &new_backing_object->shadow_head,
17152ad1a3f7SMatthew Dillon 				    object,
17162ad1a3f7SMatthew Dillon 				    shadow_list
17172ad1a3f7SMatthew Dillon 				);
1718eaf13dd7SJohn Dyson 				new_backing_object->shadow_count++;
1719eaf13dd7SJohn Dyson 				new_backing_object->generation++;
1720b921a12bSAlan Cox 				vm_object_reference_locked(new_backing_object);
17216be36525SAlan Cox 				VM_OBJECT_UNLOCK(new_backing_object);
172295e5e988SJohn Dyson 				object->backing_object_offset +=
172395e5e988SJohn Dyson 					backing_object->backing_object_offset;
1724de5f6a77SJohn Dyson 			}
1725df8bae1dSRodney W. Grimes 
1726df8bae1dSRodney W. Grimes 			/*
17270d94caffSDavid Greenman 			 * Drop the reference count on backing_object. Since
172822ec553fSAlan Cox 			 * its ref_count was at least 2, it will not vanish.
1729df8bae1dSRodney W. Grimes 			 */
173022ec553fSAlan Cox 			backing_object->ref_count--;
173122ec553fSAlan Cox 			VM_OBJECT_UNLOCK(backing_object);
1732df8bae1dSRodney W. Grimes 			object_bypasses++;
1733df8bae1dSRodney W. Grimes 		}
1734df8bae1dSRodney W. Grimes 
1735df8bae1dSRodney W. Grimes 		/*
1736df8bae1dSRodney W. Grimes 		 * Try again with this object's new backing object.
1737df8bae1dSRodney W. Grimes 		 */
1738df8bae1dSRodney W. Grimes 	}
1739df8bae1dSRodney W. Grimes }
1740df8bae1dSRodney W. Grimes 
1741df8bae1dSRodney W. Grimes /*
1742bff99f0dSAlan Cox  *	vm_object_page_remove:
1743df8bae1dSRodney W. Grimes  *
1744bff99f0dSAlan Cox  *	Removes all physical pages in the given range from the
1745bff99f0dSAlan Cox  *	object's list of pages.  If the range's end is zero, all
1746bff99f0dSAlan Cox  *	physical pages from the range's start to the end of the object
1747bff99f0dSAlan Cox  *	are deleted.
1748df8bae1dSRodney W. Grimes  *
1749df8bae1dSRodney W. Grimes  *	The object must be locked.
1750df8bae1dSRodney W. Grimes  */
175126f9a767SRodney W. Grimes void
1752ecde4b32SAlan Cox vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
1753ecde4b32SAlan Cox     boolean_t clean_only)
1754df8bae1dSRodney W. Grimes {
1755d031cff1SMatthew Dillon 	vm_page_t p, next;
1756df8bae1dSRodney W. Grimes 
1757ecde4b32SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1758ecde4b32SAlan Cox 	if (object->resident_page_count == 0)
1759df8bae1dSRodney W. Grimes 		return;
176095e5e988SJohn Dyson 
17618b03c8edSMatthew Dillon 	/*
17628b03c8edSMatthew Dillon 	 * Since physically-backed objects do not use managed pages, we can't
17638b03c8edSMatthew Dillon 	 * remove pages from the object (we must instead remove the page
17648b03c8edSMatthew Dillon 	 * references, and then destroy the object).
17658b03c8edSMatthew Dillon 	 */
1766ecde4b32SAlan Cox 	KASSERT(object->type != OBJT_PHYS,
1767ecde4b32SAlan Cox 	    ("attempt to remove pages from a physical object"));
17688b03c8edSMatthew Dillon 
1769d474eaaaSDoug Rabson 	vm_object_pip_add(object, 1);
177026f9a767SRodney W. Grimes again:
177132585dd6SAlan Cox 	vm_page_lock_queues();
177275741c04SAlan Cox 	if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
177375741c04SAlan Cox 		if (p->pindex < start) {
177475741c04SAlan Cox 			p = vm_page_splay(start, object->root);
177575741c04SAlan Cox 			if ((object->root = p)->pindex < start)
177675741c04SAlan Cox 				p = TAILQ_NEXT(p, listq);
177775741c04SAlan Cox 		}
177875741c04SAlan Cox 	}
177975741c04SAlan Cox 	/*
178075741c04SAlan Cox 	 * Assert: the variable p is either (1) the page with the
178175741c04SAlan Cox 	 * least pindex greater than or equal to the parameter pindex
178275741c04SAlan Cox 	 * or (2) NULL.
178375741c04SAlan Cox 	 */
178475741c04SAlan Cox 	for (;
1785bff99f0dSAlan Cox 	     p != NULL && (p->pindex < end || end == 0);
178675741c04SAlan Cox 	     p = next) {
1787b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
178875741c04SAlan Cox 
1789bd7e5f99SJohn Dyson 		if (p->wire_count != 0) {
17904fec79beSAlan Cox 			pmap_remove_all(p);
1791ce65e68cSDavid Greenman 			if (!clean_only)
1792bd7e5f99SJohn Dyson 				p->valid = 0;
17930d94caffSDavid Greenman 			continue;
17940d94caffSDavid Greenman 		}
179532585dd6SAlan Cox 		if (vm_page_sleep_if_busy(p, TRUE, "vmopar"))
179626f9a767SRodney W. Grimes 			goto again;
17978f9110f6SJohn Dyson 		if (clean_only && p->valid) {
179885b8d6b4SAlan Cox 			pmap_page_protect(p, VM_PROT_READ | VM_PROT_EXECUTE);
17997c1f6cedSDavid Greenman 			if (p->valid & p->dirty)
18007c1f6cedSDavid Greenman 				continue;
18017c1f6cedSDavid Greenman 		}
18024fec79beSAlan Cox 		pmap_remove_all(p);
1803df8bae1dSRodney W. Grimes 		vm_page_free(p);
180426f9a767SRodney W. Grimes 	}
180532585dd6SAlan Cox 	vm_page_unlock_queues();
1806f919ebdeSDavid Greenman 	vm_object_pip_wakeup(object);
1807c0503609SDavid Greenman }
1808df8bae1dSRodney W. Grimes 
1809df8bae1dSRodney W. Grimes /*
1810df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
1811df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
1812df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
1813df8bae1dSRodney W. Grimes  *
1814df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
1815df8bae1dSRodney W. Grimes  *
1816df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
1817df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
1818df8bae1dSRodney W. Grimes  *
1819df8bae1dSRodney W. Grimes  *	Parameters:
1820df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
1821df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
1822df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
182357a21abaSAlan Cox  *		next_size	Size of reference to the second object
1824df8bae1dSRodney W. Grimes  *
1825df8bae1dSRodney W. Grimes  *	Conditions:
1826df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
1827df8bae1dSRodney W. Grimes  */
18280d94caffSDavid Greenman boolean_t
182957a21abaSAlan Cox vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset,
183000e1854aSAlan Cox 	vm_size_t prev_size, vm_size_t next_size)
1831df8bae1dSRodney W. Grimes {
1832ea41812fSAlan Cox 	vm_pindex_t next_pindex;
1833df8bae1dSRodney W. Grimes 
183400e1854aSAlan Cox 	if (prev_object == NULL)
1835df8bae1dSRodney W. Grimes 		return (TRUE);
1836bdbfbaafSAlan Cox 	VM_OBJECT_LOCK(prev_object);
18374112823fSMatthew Dillon 	if (prev_object->type != OBJT_DEFAULT &&
18384112823fSMatthew Dillon 	    prev_object->type != OBJT_SWAP) {
1839bdbfbaafSAlan Cox 		VM_OBJECT_UNLOCK(prev_object);
184030dcfc09SJohn Dyson 		return (FALSE);
184130dcfc09SJohn Dyson 	}
184230dcfc09SJohn Dyson 
1843df8bae1dSRodney W. Grimes 	/*
1844df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
1845df8bae1dSRodney W. Grimes 	 */
1846df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
1847df8bae1dSRodney W. Grimes 
1848df8bae1dSRodney W. Grimes 	/*
18490d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
18500d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
18510d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
1852df8bae1dSRodney W. Grimes 	 */
18538cc7e047SJohn Dyson 	if (prev_object->backing_object != NULL) {
1854bdbfbaafSAlan Cox 		VM_OBJECT_UNLOCK(prev_object);
1855df8bae1dSRodney W. Grimes 		return (FALSE);
1856df8bae1dSRodney W. Grimes 	}
1857a316d390SJohn Dyson 
1858a316d390SJohn Dyson 	prev_size >>= PAGE_SHIFT;
1859a316d390SJohn Dyson 	next_size >>= PAGE_SHIFT;
186057a21abaSAlan Cox 	next_pindex = OFF_TO_IDX(prev_offset) + prev_size;
18618cc7e047SJohn Dyson 
18628cc7e047SJohn Dyson 	if ((prev_object->ref_count > 1) &&
1863ea41812fSAlan Cox 	    (prev_object->size != next_pindex)) {
1864bdbfbaafSAlan Cox 		VM_OBJECT_UNLOCK(prev_object);
18658cc7e047SJohn Dyson 		return (FALSE);
18668cc7e047SJohn Dyson 	}
18678cc7e047SJohn Dyson 
1868df8bae1dSRodney W. Grimes 	/*
18690d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
18700d94caffSDavid Greenman 	 * deallocation.
1871df8bae1dSRodney W. Grimes 	 */
1872ea41812fSAlan Cox 	if (next_pindex < prev_object->size) {
1873df8bae1dSRodney W. Grimes 		vm_object_page_remove(prev_object,
1874ea41812fSAlan Cox 				      next_pindex,
1875ea41812fSAlan Cox 				      next_pindex + next_size, FALSE);
1876ea41812fSAlan Cox 		if (prev_object->type == OBJT_SWAP)
1877ea41812fSAlan Cox 			swap_pager_freespace(prev_object,
1878ea41812fSAlan Cox 					     next_pindex, next_size);
1879ea41812fSAlan Cox 	}
1880df8bae1dSRodney W. Grimes 
1881df8bae1dSRodney W. Grimes 	/*
1882df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
1883df8bae1dSRodney W. Grimes 	 */
1884ea41812fSAlan Cox 	if (next_pindex + next_size > prev_object->size)
1885ea41812fSAlan Cox 		prev_object->size = next_pindex + next_size;
1886df8bae1dSRodney W. Grimes 
1887bdbfbaafSAlan Cox 	VM_OBJECT_UNLOCK(prev_object);
1888df8bae1dSRodney W. Grimes 	return (TRUE);
1889df8bae1dSRodney W. Grimes }
1890df8bae1dSRodney W. Grimes 
18917a5a6352SMatthew Dillon void
18927a5a6352SMatthew Dillon vm_object_set_writeable_dirty(vm_object_t object)
18937a5a6352SMatthew Dillon {
18947a5a6352SMatthew Dillon 	struct vnode *vp;
18957a5a6352SMatthew Dillon 
1896de33beddSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1897ee39666aSJeff Roberson 	if ((object->flags & (OBJ_MIGHTBEDIRTY|OBJ_WRITEABLE)) ==
1898ee39666aSJeff Roberson 	    (OBJ_MIGHTBEDIRTY|OBJ_WRITEABLE))
1899ee39666aSJeff Roberson 		return;
19007a5a6352SMatthew Dillon 	vm_object_set_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
19017a5a6352SMatthew Dillon 	if (object->type == OBJT_VNODE &&
19027a5a6352SMatthew Dillon 	    (vp = (struct vnode *)object->handle) != NULL) {
1903e6e370a7SJeff Roberson 		VI_LOCK(vp);
1904e6e370a7SJeff Roberson 		vp->v_iflag |= VI_OBJDIRTY;
1905e6e370a7SJeff Roberson 		VI_UNLOCK(vp);
19067a5a6352SMatthew Dillon 	}
19077a5a6352SMatthew Dillon }
19087a5a6352SMatthew Dillon 
1909c7c34a24SBruce Evans #include "opt_ddb.h"
1910c3cb3e12SDavid Greenman #ifdef DDB
1911c7c34a24SBruce Evans #include <sys/kernel.h>
1912c7c34a24SBruce Evans 
1913ce9edcf5SPoul-Henning Kamp #include <sys/cons.h>
1914c7c34a24SBruce Evans 
1915c7c34a24SBruce Evans #include <ddb/ddb.h>
1916c7c34a24SBruce Evans 
1917cac597e4SBruce Evans static int
19181b40f8c0SMatthew Dillon _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
1919a1f6d91cSDavid Greenman {
1920a1f6d91cSDavid Greenman 	vm_map_t tmpm;
1921a1f6d91cSDavid Greenman 	vm_map_entry_t tmpe;
1922a1f6d91cSDavid Greenman 	vm_object_t obj;
1923a1f6d91cSDavid Greenman 	int entcount;
1924a1f6d91cSDavid Greenman 
1925a1f6d91cSDavid Greenman 	if (map == 0)
1926a1f6d91cSDavid Greenman 		return 0;
1927a1f6d91cSDavid Greenman 
1928a1f6d91cSDavid Greenman 	if (entry == 0) {
1929a1f6d91cSDavid Greenman 		tmpe = map->header.next;
1930a1f6d91cSDavid Greenman 		entcount = map->nentries;
1931a1f6d91cSDavid Greenman 		while (entcount-- && (tmpe != &map->header)) {
1932a1f6d91cSDavid Greenman 			if (_vm_object_in_map(map, object, tmpe)) {
1933a1f6d91cSDavid Greenman 				return 1;
1934a1f6d91cSDavid Greenman 			}
1935a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1936a1f6d91cSDavid Greenman 		}
19379fdfe602SMatthew Dillon 	} else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
19389fdfe602SMatthew Dillon 		tmpm = entry->object.sub_map;
1939a1f6d91cSDavid Greenman 		tmpe = tmpm->header.next;
1940a1f6d91cSDavid Greenman 		entcount = tmpm->nentries;
1941a1f6d91cSDavid Greenman 		while (entcount-- && tmpe != &tmpm->header) {
1942a1f6d91cSDavid Greenman 			if (_vm_object_in_map(tmpm, object, tmpe)) {
1943a1f6d91cSDavid Greenman 				return 1;
1944a1f6d91cSDavid Greenman 			}
1945a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1946a1f6d91cSDavid Greenman 		}
19478aef1712SMatthew Dillon 	} else if ((obj = entry->object.vm_object) != NULL) {
194824a1cce3SDavid Greenman 		for (; obj; obj = obj->backing_object)
1949a1f6d91cSDavid Greenman 			if (obj == object) {
1950a1f6d91cSDavid Greenman 				return 1;
1951a1f6d91cSDavid Greenman 			}
1952a1f6d91cSDavid Greenman 	}
1953a1f6d91cSDavid Greenman 	return 0;
1954a1f6d91cSDavid Greenman }
1955a1f6d91cSDavid Greenman 
1956cac597e4SBruce Evans static int
19571b40f8c0SMatthew Dillon vm_object_in_map(vm_object_t object)
1958a1f6d91cSDavid Greenman {
1959a1f6d91cSDavid Greenman 	struct proc *p;
19601005a129SJohn Baldwin 
196160517fd1SJohn Baldwin 	/* sx_slock(&allproc_lock); */
1962fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(p, &allproc, p_list) {
1963a1f6d91cSDavid Greenman 		if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1964a1f6d91cSDavid Greenman 			continue;
1965553629ebSJake Burkholder 		if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
196660517fd1SJohn Baldwin 			/* sx_sunlock(&allproc_lock); */
1967a1f6d91cSDavid Greenman 			return 1;
1968a1f6d91cSDavid Greenman 		}
1969553629ebSJake Burkholder 	}
197060517fd1SJohn Baldwin 	/* sx_sunlock(&allproc_lock); */
1971a1f6d91cSDavid Greenman 	if (_vm_object_in_map(kernel_map, object, 0))
1972a1f6d91cSDavid Greenman 		return 1;
1973a1f6d91cSDavid Greenman 	if (_vm_object_in_map(kmem_map, object, 0))
1974a1f6d91cSDavid Greenman 		return 1;
1975a1f6d91cSDavid Greenman 	if (_vm_object_in_map(pager_map, object, 0))
1976a1f6d91cSDavid Greenman 		return 1;
1977a1f6d91cSDavid Greenman 	if (_vm_object_in_map(buffer_map, object, 0))
1978a1f6d91cSDavid Greenman 		return 1;
1979a1f6d91cSDavid Greenman 	return 0;
1980a1f6d91cSDavid Greenman }
1981a1f6d91cSDavid Greenman 
1982c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check)
1983f708ef1bSPoul-Henning Kamp {
1984a1f6d91cSDavid Greenman 	vm_object_t object;
1985a1f6d91cSDavid Greenman 
1986a1f6d91cSDavid Greenman 	/*
1987a1f6d91cSDavid Greenman 	 * make sure that internal objs are in a map somewhere
1988a1f6d91cSDavid Greenman 	 * and none have zero ref counts.
1989a1f6d91cSDavid Greenman 	 */
1990cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
199124a1cce3SDavid Greenman 		if (object->handle == NULL &&
199224a1cce3SDavid Greenman 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
1993a1f6d91cSDavid Greenman 			if (object->ref_count == 0) {
19943efc015bSPeter Wemm 				db_printf("vmochk: internal obj has zero ref count: %ld\n",
19953efc015bSPeter Wemm 					(long)object->size);
1996a1f6d91cSDavid Greenman 			}
1997a1f6d91cSDavid Greenman 			if (!vm_object_in_map(object)) {
1998fc62ef1fSBruce Evans 				db_printf(
1999fc62ef1fSBruce Evans 			"vmochk: internal obj is not in a map: "
2000fc62ef1fSBruce Evans 			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2001fc62ef1fSBruce Evans 				    object->ref_count, (u_long)object->size,
2002fc62ef1fSBruce Evans 				    (u_long)object->size,
2003fc62ef1fSBruce Evans 				    (void *)object->backing_object);
2004a1f6d91cSDavid Greenman 			}
2005a1f6d91cSDavid Greenman 		}
2006a1f6d91cSDavid Greenman 	}
2007a1f6d91cSDavid Greenman }
2008a1f6d91cSDavid Greenman 
200926f9a767SRodney W. Grimes /*
2010df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
2011df8bae1dSRodney W. Grimes  */
2012c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static)
2013df8bae1dSRodney W. Grimes {
2014c7c34a24SBruce Evans 	/* XXX convert args. */
2015c7c34a24SBruce Evans 	vm_object_t object = (vm_object_t)addr;
2016c7c34a24SBruce Evans 	boolean_t full = have_addr;
2017c7c34a24SBruce Evans 
2018d031cff1SMatthew Dillon 	vm_page_t p;
2019df8bae1dSRodney W. Grimes 
2020c7c34a24SBruce Evans 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
2021c7c34a24SBruce Evans #define	count	was_count
2022c7c34a24SBruce Evans 
2023d031cff1SMatthew Dillon 	int count;
2024df8bae1dSRodney W. Grimes 
2025df8bae1dSRodney W. Grimes 	if (object == NULL)
2026df8bae1dSRodney W. Grimes 		return;
2027df8bae1dSRodney W. Grimes 
2028eb95adefSBruce Evans 	db_iprintf(
2029e47cd172SMaxime Henrion 	    "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x\n",
2030e47cd172SMaxime Henrion 	    object, (int)object->type, (uintmax_t)object->size,
2031eb95adefSBruce Evans 	    object->resident_page_count, object->ref_count, object->flags);
2032e47cd172SMaxime Henrion 	db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n",
20331c7c3c6aSMatthew Dillon 	    object->shadow_count,
2034eb95adefSBruce Evans 	    object->backing_object ? object->backing_object->ref_count : 0,
2035e47cd172SMaxime Henrion 	    object->backing_object, (uintmax_t)object->backing_object_offset);
2036df8bae1dSRodney W. Grimes 
2037df8bae1dSRodney W. Grimes 	if (!full)
2038df8bae1dSRodney W. Grimes 		return;
2039df8bae1dSRodney W. Grimes 
2040c7c34a24SBruce Evans 	db_indent += 2;
2041df8bae1dSRodney W. Grimes 	count = 0;
2042fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(p, &object->memq, listq) {
2043df8bae1dSRodney W. Grimes 		if (count == 0)
2044c7c34a24SBruce Evans 			db_iprintf("memory:=");
2045df8bae1dSRodney W. Grimes 		else if (count == 6) {
2046c7c34a24SBruce Evans 			db_printf("\n");
2047c7c34a24SBruce Evans 			db_iprintf(" ...");
2048df8bae1dSRodney W. Grimes 			count = 0;
2049df8bae1dSRodney W. Grimes 		} else
2050c7c34a24SBruce Evans 			db_printf(",");
2051df8bae1dSRodney W. Grimes 		count++;
2052df8bae1dSRodney W. Grimes 
2053e47cd172SMaxime Henrion 		db_printf("(off=0x%jx,page=0x%jx)",
2054e47cd172SMaxime Henrion 		    (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p));
2055df8bae1dSRodney W. Grimes 	}
2056df8bae1dSRodney W. Grimes 	if (count != 0)
2057c7c34a24SBruce Evans 		db_printf("\n");
2058c7c34a24SBruce Evans 	db_indent -= 2;
2059df8bae1dSRodney W. Grimes }
20605070c7f8SJohn Dyson 
2061c7c34a24SBruce Evans /* XXX. */
2062c7c34a24SBruce Evans #undef count
2063c7c34a24SBruce Evans 
2064c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */
20655070c7f8SJohn Dyson void
20661b40f8c0SMatthew Dillon vm_object_print(
20671b40f8c0SMatthew Dillon         /* db_expr_t */ long addr,
20681b40f8c0SMatthew Dillon 	boolean_t have_addr,
20691b40f8c0SMatthew Dillon 	/* db_expr_t */ long count,
20701b40f8c0SMatthew Dillon 	char *modif)
2071c7c34a24SBruce Evans {
2072c7c34a24SBruce Evans 	vm_object_print_static(addr, have_addr, count, modif);
2073c7c34a24SBruce Evans }
2074c7c34a24SBruce Evans 
2075c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
20765070c7f8SJohn Dyson {
20775070c7f8SJohn Dyson 	vm_object_t object;
20785070c7f8SJohn Dyson 	int nl = 0;
20795070c7f8SJohn Dyson 	int c;
2080cc64b484SAlfred Perlstein 
2081cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
20825070c7f8SJohn Dyson 		vm_pindex_t idx, fidx;
20835070c7f8SJohn Dyson 		vm_pindex_t osize;
2084227f9a1cSJake Burkholder 		vm_paddr_t pa = -1, padiff;
20855070c7f8SJohn Dyson 		int rcount;
20865070c7f8SJohn Dyson 		vm_page_t m;
20875070c7f8SJohn Dyson 
2088fc62ef1fSBruce Evans 		db_printf("new object: %p\n", (void *)object);
20895070c7f8SJohn Dyson 		if (nl > 18) {
20905070c7f8SJohn Dyson 			c = cngetc();
20915070c7f8SJohn Dyson 			if (c != ' ')
20925070c7f8SJohn Dyson 				return;
20935070c7f8SJohn Dyson 			nl = 0;
20945070c7f8SJohn Dyson 		}
20955070c7f8SJohn Dyson 		nl++;
20965070c7f8SJohn Dyson 		rcount = 0;
20975070c7f8SJohn Dyson 		fidx = 0;
20985070c7f8SJohn Dyson 		osize = object->size;
20995070c7f8SJohn Dyson 		if (osize > 128)
21005070c7f8SJohn Dyson 			osize = 128;
21015070c7f8SJohn Dyson 		for (idx = 0; idx < osize; idx++) {
21025070c7f8SJohn Dyson 			m = vm_page_lookup(object, idx);
21035070c7f8SJohn Dyson 			if (m == NULL) {
21045070c7f8SJohn Dyson 				if (rcount) {
21053efc015bSPeter Wemm 					db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
21063efc015bSPeter Wemm 						(long)fidx, rcount, (long)pa);
21075070c7f8SJohn Dyson 					if (nl > 18) {
21085070c7f8SJohn Dyson 						c = cngetc();
21095070c7f8SJohn Dyson 						if (c != ' ')
21105070c7f8SJohn Dyson 							return;
21115070c7f8SJohn Dyson 						nl = 0;
21125070c7f8SJohn Dyson 					}
21135070c7f8SJohn Dyson 					nl++;
21145070c7f8SJohn Dyson 					rcount = 0;
21155070c7f8SJohn Dyson 				}
21165070c7f8SJohn Dyson 				continue;
21175070c7f8SJohn Dyson 			}
21185070c7f8SJohn Dyson 
21195070c7f8SJohn Dyson 
21205070c7f8SJohn Dyson 			if (rcount &&
21215070c7f8SJohn Dyson 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
21225070c7f8SJohn Dyson 				++rcount;
21235070c7f8SJohn Dyson 				continue;
21245070c7f8SJohn Dyson 			}
21255070c7f8SJohn Dyson 			if (rcount) {
21265070c7f8SJohn Dyson 				padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
21275070c7f8SJohn Dyson 				padiff >>= PAGE_SHIFT;
2128ef39c05bSAlexander Leidinger 				padiff &= PQ_COLORMASK;
21295070c7f8SJohn Dyson 				if (padiff == 0) {
21305070c7f8SJohn Dyson 					pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
21315070c7f8SJohn Dyson 					++rcount;
21325070c7f8SJohn Dyson 					continue;
21335070c7f8SJohn Dyson 				}
21343efc015bSPeter Wemm 				db_printf(" index(%ld)run(%d)pa(0x%lx)",
21353efc015bSPeter Wemm 					(long)fidx, rcount, (long)pa);
21363efc015bSPeter Wemm 				db_printf("pd(%ld)\n", (long)padiff);
21375070c7f8SJohn Dyson 				if (nl > 18) {
21385070c7f8SJohn Dyson 					c = cngetc();
21395070c7f8SJohn Dyson 					if (c != ' ')
21405070c7f8SJohn Dyson 						return;
21415070c7f8SJohn Dyson 					nl = 0;
21425070c7f8SJohn Dyson 				}
21435070c7f8SJohn Dyson 				nl++;
21445070c7f8SJohn Dyson 			}
21455070c7f8SJohn Dyson 			fidx = idx;
21465070c7f8SJohn Dyson 			pa = VM_PAGE_TO_PHYS(m);
21475070c7f8SJohn Dyson 			rcount = 1;
21485070c7f8SJohn Dyson 		}
21495070c7f8SJohn Dyson 		if (rcount) {
21503efc015bSPeter Wemm 			db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
21513efc015bSPeter Wemm 				(long)fidx, rcount, (long)pa);
21525070c7f8SJohn Dyson 			if (nl > 18) {
21535070c7f8SJohn Dyson 				c = cngetc();
21545070c7f8SJohn Dyson 				if (c != ' ')
21555070c7f8SJohn Dyson 					return;
21565070c7f8SJohn Dyson 				nl = 0;
21575070c7f8SJohn Dyson 			}
21585070c7f8SJohn Dyson 			nl++;
21595070c7f8SJohn Dyson 		}
21605070c7f8SJohn Dyson 	}
21615070c7f8SJohn Dyson }
2162c3cb3e12SDavid Greenman #endif /* DDB */
2163