xref: /freebsd/sys/vm/vm_object.c (revision 57a21aba930b1b31247f3b74d84334cd5dd90d9b)
1df8bae1dSRodney W. Grimes /*
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);
113f6b04d2bSDavid Greenman 
114df8bae1dSRodney W. Grimes /*
115df8bae1dSRodney W. Grimes  *	Virtual memory objects maintain the actual data
116df8bae1dSRodney W. Grimes  *	associated with allocated virtual memory.  A given
117df8bae1dSRodney W. Grimes  *	page of memory exists within exactly one object.
118df8bae1dSRodney W. Grimes  *
119df8bae1dSRodney W. Grimes  *	An object is only deallocated when all "references"
120df8bae1dSRodney W. Grimes  *	are given up.  Only one "reference" to a given
121df8bae1dSRodney W. Grimes  *	region of an object should be writeable.
122df8bae1dSRodney W. Grimes  *
123df8bae1dSRodney W. Grimes  *	Associated with each object is a list of all resident
124df8bae1dSRodney W. Grimes  *	memory pages belonging to that object; this list is
125df8bae1dSRodney W. Grimes  *	maintained by the "vm_page" module, and locked by the object's
126df8bae1dSRodney W. Grimes  *	lock.
127df8bae1dSRodney W. Grimes  *
128df8bae1dSRodney W. Grimes  *	Each object also records a "pager" routine which is
129df8bae1dSRodney W. Grimes  *	used to retrieve (and store) pages to the proper backing
130df8bae1dSRodney W. Grimes  *	storage.  In addition, objects may be backed by other
131df8bae1dSRodney W. Grimes  *	objects from which they were virtual-copied.
132df8bae1dSRodney W. Grimes  *
133df8bae1dSRodney W. Grimes  *	The only items within the object structure which are
134df8bae1dSRodney W. Grimes  *	modified after time of creation are:
135df8bae1dSRodney W. Grimes  *		reference count		locked by object's lock
136df8bae1dSRodney W. Grimes  *		pager routine		locked by object's lock
137df8bae1dSRodney W. Grimes  *
138df8bae1dSRodney W. Grimes  */
139df8bae1dSRodney W. Grimes 
14028f8db14SBruce Evans struct object_q vm_object_list;
141a5698387SAlan Cox struct mtx vm_object_list_mtx;	/* lock for object list and count */
142cccf11b8SAlan Cox 
143cccf11b8SAlan Cox struct vm_object kernel_object_store;
144cccf11b8SAlan Cox struct vm_object kmem_object_store;
145df8bae1dSRodney W. Grimes 
146f708ef1bSPoul-Henning Kamp static long object_collapses;
147f708ef1bSPoul-Henning Kamp static long object_bypasses;
148dcc5840eSAlan Cox static int next_index;
149670d17b5SJeff Roberson static uma_zone_t obj_zone;
15099448ed1SJohn Dyson #define VM_OBJECTS_INIT 256
1518355f576SJeff Roberson 
1528355f576SJeff Roberson static void vm_object_zinit(void *mem, int size);
1538355f576SJeff Roberson 
1548355f576SJeff Roberson #ifdef INVARIANTS
1558355f576SJeff Roberson static void vm_object_zdtor(void *mem, int size, void *arg);
1568355f576SJeff Roberson 
1578355f576SJeff Roberson static void
1588355f576SJeff Roberson vm_object_zdtor(void *mem, int size, void *arg)
1598355f576SJeff Roberson {
1608355f576SJeff Roberson 	vm_object_t object;
1618355f576SJeff Roberson 
1628355f576SJeff Roberson 	object = (vm_object_t)mem;
16343186e53SAlan Cox 	KASSERT(TAILQ_EMPTY(&object->memq),
16443186e53SAlan Cox 	    ("object %p has resident pages",
16543186e53SAlan Cox 	    object));
1668355f576SJeff Roberson 	KASSERT(object->paging_in_progress == 0,
1678355f576SJeff Roberson 	    ("object %p paging_in_progress = %d",
1688355f576SJeff Roberson 	    object, object->paging_in_progress));
1698355f576SJeff Roberson 	KASSERT(object->resident_page_count == 0,
1708355f576SJeff Roberson 	    ("object %p resident_page_count = %d",
1718355f576SJeff Roberson 	    object, object->resident_page_count));
1728355f576SJeff Roberson 	KASSERT(object->shadow_count == 0,
1738355f576SJeff Roberson 	    ("object %p shadow_count = %d",
1748355f576SJeff Roberson 	    object, object->shadow_count));
1758355f576SJeff Roberson }
1768355f576SJeff Roberson #endif
1778355f576SJeff Roberson 
1788355f576SJeff Roberson static void
1798355f576SJeff Roberson vm_object_zinit(void *mem, int size)
1808355f576SJeff Roberson {
1818355f576SJeff Roberson 	vm_object_t object;
1828355f576SJeff Roberson 
1838355f576SJeff Roberson 	object = (vm_object_t)mem;
184e3a9e1b2SAlan Cox 	bzero(&object->mtx, sizeof(object->mtx));
1855285558aSAlan Cox 	VM_OBJECT_LOCK_INIT(object, "standard object");
1868355f576SJeff Roberson 
1878355f576SJeff Roberson 	/* These are true for any object that has been freed */
1888355f576SJeff Roberson 	object->paging_in_progress = 0;
1898355f576SJeff Roberson 	object->resident_page_count = 0;
1908355f576SJeff Roberson 	object->shadow_count = 0;
1918355f576SJeff Roberson }
192df8bae1dSRodney W. Grimes 
1933075778bSJohn Dyson void
1946395da54SIan Dowse _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
195df8bae1dSRodney W. Grimes {
196cf3e6e48SAlan Cox 	int incr;
1970cddd8f0SMatthew Dillon 
198df8bae1dSRodney W. Grimes 	TAILQ_INIT(&object->memq);
1991c500307SAlan Cox 	LIST_INIT(&object->shadow_head);
200a1f6d91cSDavid Greenman 
201b86ec922SMatthew Dillon 	object->root = NULL;
20224a1cce3SDavid Greenman 	object->type = type;
203df8bae1dSRodney W. Grimes 	object->size = size;
204b881da26SAlan Cox 	object->generation = 1;
205a1f6d91cSDavid Greenman 	object->ref_count = 1;
20624a1cce3SDavid Greenman 	object->flags = 0;
20760517fd1SJohn Baldwin 	if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
2083471677cSAlan Cox 		object->flags = OBJ_ONEMAPPING;
20999448ed1SJohn Dyson 	if (size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
21099448ed1SJohn Dyson 		incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
21199448ed1SJohn Dyson 	else
21299448ed1SJohn Dyson 		incr = size;
213dcc5840eSAlan Cox 	do
214dcc5840eSAlan Cox 		object->pg_color = next_index;
215dcc5840eSAlan Cox 	while (!atomic_cmpset_int(&next_index, object->pg_color,
21679660d83SAlan Cox 				  (object->pg_color + incr) & PQ_L2_MASK));
21724a1cce3SDavid Greenman 	object->handle = NULL;
21824a1cce3SDavid Greenman 	object->backing_object = NULL;
219a316d390SJohn Dyson 	object->backing_object_offset = (vm_ooffset_t) 0;
220a1f6d91cSDavid Greenman 
221a5698387SAlan Cox 	mtx_lock(&vm_object_list_mtx);
22260517fd1SJohn Baldwin 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
223a5698387SAlan Cox 	mtx_unlock(&vm_object_list_mtx);
224df8bae1dSRodney W. Grimes }
225df8bae1dSRodney W. Grimes 
226df8bae1dSRodney W. Grimes /*
22726f9a767SRodney W. Grimes  *	vm_object_init:
22826f9a767SRodney W. Grimes  *
22926f9a767SRodney W. Grimes  *	Initialize the VM objects module.
23026f9a767SRodney W. Grimes  */
23126f9a767SRodney W. Grimes void
2321b40f8c0SMatthew Dillon vm_object_init(void)
23326f9a767SRodney W. Grimes {
23426f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_list);
2356008862bSJohn Baldwin 	mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF);
2360217125fSDavid Greenman 
2375285558aSAlan Cox 	VM_OBJECT_LOCK_INIT(&kernel_object_store, "kernel object");
238a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
23926f9a767SRodney W. Grimes 	    kernel_object);
24026f9a767SRodney W. Grimes 
2415285558aSAlan Cox 	VM_OBJECT_LOCK_INIT(&kmem_object_store, "kmem object");
242a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
24326f9a767SRodney W. Grimes 	    kmem_object);
244ed6a7863SAlan Cox 
2458355f576SJeff Roberson 	obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL,
2468355f576SJeff Roberson #ifdef INVARIANTS
2478355f576SJeff Roberson 	    vm_object_zdtor,
2488355f576SJeff Roberson #else
2498355f576SJeff Roberson 	    NULL,
2508355f576SJeff Roberson #endif
251f3c625e4SJeff Roberson 	    vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM|UMA_ZONE_NOFREE);
2528355f576SJeff Roberson 	uma_prealloc(obj_zone, VM_OBJECTS_INIT);
25399448ed1SJohn Dyson }
25499448ed1SJohn Dyson 
25599448ed1SJohn Dyson void
2561b40f8c0SMatthew Dillon vm_object_clear_flag(vm_object_t object, u_short bits)
2571b40f8c0SMatthew Dillon {
2585440b5a9SAlan Cox 
259d647a0edSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
260b06805adSJake Burkholder 	object->flags &= ~bits;
2611b40f8c0SMatthew Dillon }
2621b40f8c0SMatthew Dillon 
2631b40f8c0SMatthew Dillon void
2641b40f8c0SMatthew Dillon vm_object_pip_add(vm_object_t object, short i)
2651b40f8c0SMatthew Dillon {
266f279b88dSAlan Cox 
267d647a0edSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
268b06805adSJake Burkholder 	object->paging_in_progress += i;
2691b40f8c0SMatthew Dillon }
2701b40f8c0SMatthew Dillon 
2711b40f8c0SMatthew Dillon void
2721b40f8c0SMatthew Dillon vm_object_pip_subtract(vm_object_t object, short i)
2731b40f8c0SMatthew Dillon {
274d647a0edSAlan Cox 
2750fa05eaeSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
276b06805adSJake Burkholder 	object->paging_in_progress -= i;
2771b40f8c0SMatthew Dillon }
2781b40f8c0SMatthew Dillon 
2791b40f8c0SMatthew Dillon void
2801b40f8c0SMatthew Dillon vm_object_pip_wakeup(vm_object_t object)
2811b40f8c0SMatthew Dillon {
282f279b88dSAlan Cox 
283d647a0edSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
284b06805adSJake Burkholder 	object->paging_in_progress--;
2851b40f8c0SMatthew Dillon 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
2861b40f8c0SMatthew Dillon 		vm_object_clear_flag(object, OBJ_PIPWNT);
2871b40f8c0SMatthew Dillon 		wakeup(object);
2881b40f8c0SMatthew Dillon 	}
2891b40f8c0SMatthew Dillon }
2901b40f8c0SMatthew Dillon 
2911b40f8c0SMatthew Dillon void
2921b40f8c0SMatthew Dillon vm_object_pip_wakeupn(vm_object_t object, short i)
2931b40f8c0SMatthew Dillon {
294d647a0edSAlan Cox 
2950d420ad3SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2961b40f8c0SMatthew Dillon 	if (i)
297b06805adSJake Burkholder 		object->paging_in_progress -= i;
2981b40f8c0SMatthew Dillon 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
2991b40f8c0SMatthew Dillon 		vm_object_clear_flag(object, OBJ_PIPWNT);
3001b40f8c0SMatthew Dillon 		wakeup(object);
3011b40f8c0SMatthew Dillon 	}
3021b40f8c0SMatthew Dillon }
3031b40f8c0SMatthew Dillon 
3041b40f8c0SMatthew Dillon void
3051b40f8c0SMatthew Dillon vm_object_pip_wait(vm_object_t object, char *waitid)
3061b40f8c0SMatthew Dillon {
3071ca58953SAlan Cox 
3081ca58953SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
3091ca58953SAlan Cox 	while (object->paging_in_progress) {
3101ca58953SAlan Cox 		object->flags |= OBJ_PIPWNT;
3111ca58953SAlan Cox 		msleep(object, VM_OBJECT_MTX(object), PVM, waitid, 0);
3121ca58953SAlan Cox 	}
3131b40f8c0SMatthew Dillon }
3141b40f8c0SMatthew Dillon 
31526f9a767SRodney W. Grimes /*
31698cb733cSKenneth D. Merry  *	vm_object_allocate_wait
31798cb733cSKenneth D. Merry  *
31898cb733cSKenneth D. Merry  *	Return a new object with the given size, and give the user the
31998cb733cSKenneth D. Merry  *	option of waiting for it to complete or failing if the needed
32098cb733cSKenneth D. Merry  *	memory isn't available.
32198cb733cSKenneth D. Merry  */
32298cb733cSKenneth D. Merry vm_object_t
32398cb733cSKenneth D. Merry vm_object_allocate_wait(objtype_t type, vm_pindex_t size, int flags)
32498cb733cSKenneth D. Merry {
32598cb733cSKenneth D. Merry 	vm_object_t result;
32698cb733cSKenneth D. Merry 
32798cb733cSKenneth D. Merry 	result = (vm_object_t) uma_zalloc(obj_zone, flags);
32898cb733cSKenneth D. Merry 
32998cb733cSKenneth D. Merry 	if (result != NULL)
33098cb733cSKenneth D. Merry 		_vm_object_allocate(type, size, result);
33198cb733cSKenneth D. Merry 
33298cb733cSKenneth D. Merry 	return (result);
33398cb733cSKenneth D. Merry }
33498cb733cSKenneth D. Merry 
33598cb733cSKenneth D. Merry /*
33626f9a767SRodney W. Grimes  *	vm_object_allocate:
33726f9a767SRodney W. Grimes  *
33826f9a767SRodney W. Grimes  *	Returns a new object with the given size.
33926f9a767SRodney W. Grimes  */
34026f9a767SRodney W. Grimes vm_object_t
3416395da54SIan Dowse vm_object_allocate(objtype_t type, vm_pindex_t size)
34226f9a767SRodney W. Grimes {
343a163d034SWarner Losh 	return(vm_object_allocate_wait(type, size, M_WAITOK));
34426f9a767SRodney W. Grimes }
34526f9a767SRodney W. Grimes 
34626f9a767SRodney W. Grimes 
34726f9a767SRodney W. Grimes /*
348df8bae1dSRodney W. Grimes  *	vm_object_reference:
349df8bae1dSRodney W. Grimes  *
35015347817SAlan Cox  *	Gets another reference to the given object.  Note: OBJ_DEAD
35115347817SAlan Cox  *	objects can be referenced during final cleaning.
352df8bae1dSRodney W. Grimes  */
3536476c0d2SJohn Dyson void
3541b40f8c0SMatthew Dillon vm_object_reference(vm_object_t object)
355df8bae1dSRodney W. Grimes {
35682f9defeSAlan Cox 	struct vnode *vp;
35782f9defeSAlan Cox 	int flags;
35882f9defeSAlan Cox 
359df8bae1dSRodney W. Grimes 	if (object == NULL)
360df8bae1dSRodney W. Grimes 		return;
36115347817SAlan Cox 	VM_OBJECT_LOCK(object);
362df8bae1dSRodney W. Grimes 	object->ref_count++;
36347221757SJohn Dyson 	if (object->type == OBJT_VNODE) {
36482f9defeSAlan Cox 		vp = object->handle;
36582f9defeSAlan Cox 		VI_LOCK(vp);
36682f9defeSAlan Cox 		VM_OBJECT_UNLOCK(object);
36782f9defeSAlan Cox 		for (flags = LK_INTERLOCK; vget(vp, flags, curthread);
36882f9defeSAlan Cox 		     flags = 0)
36982f9defeSAlan Cox 			printf("vm_object_reference: delay in vget\n");
37082f9defeSAlan Cox 	} else
37182f9defeSAlan Cox 		VM_OBJECT_UNLOCK(object);
37295e5e988SJohn Dyson }
37395e5e988SJohn Dyson 
37423955314SAlfred Perlstein /*
375b921a12bSAlan Cox  *	vm_object_reference_locked:
376b921a12bSAlan Cox  *
377b921a12bSAlan Cox  *	Gets another reference to the given object.
378b921a12bSAlan Cox  *
379b921a12bSAlan Cox  *	The object must be locked.
380b921a12bSAlan Cox  */
381b921a12bSAlan Cox void
382b921a12bSAlan Cox vm_object_reference_locked(vm_object_t object)
383b921a12bSAlan Cox {
384b921a12bSAlan Cox 	struct vnode *vp;
385b921a12bSAlan Cox 
386b921a12bSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
387b921a12bSAlan Cox 	KASSERT((object->flags & OBJ_DEAD) == 0,
388b921a12bSAlan Cox 	    ("vm_object_reference_locked: dead object referenced"));
389b921a12bSAlan Cox 	object->ref_count++;
390b921a12bSAlan Cox 	if (object->type == OBJT_VNODE) {
391b921a12bSAlan Cox 		vp = object->handle;
392b921a12bSAlan Cox 		vref(vp);
393b921a12bSAlan Cox 	}
394b921a12bSAlan Cox }
395b921a12bSAlan Cox 
396b921a12bSAlan Cox /*
3979d5abbddSJens Schweikhardt  * Handle deallocating an object of type OBJT_VNODE.
39823955314SAlfred Perlstein  */
399bf27292bSJohn Dyson void
4001b40f8c0SMatthew Dillon vm_object_vndeallocate(vm_object_t object)
40195e5e988SJohn Dyson {
40295e5e988SJohn Dyson 	struct vnode *vp = (struct vnode *) object->handle;
403219cbf59SEivind Eklund 
4040cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
405ad682c48SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
4065526d2d9SEivind Eklund 	KASSERT(object->type == OBJT_VNODE,
4075526d2d9SEivind Eklund 	    ("vm_object_vndeallocate: not a vnode object"));
408219cbf59SEivind Eklund 	KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
409219cbf59SEivind Eklund #ifdef INVARIANTS
41095e5e988SJohn Dyson 	if (object->ref_count == 0) {
41195e5e988SJohn Dyson 		vprint("vm_object_vndeallocate", vp);
41295e5e988SJohn Dyson 		panic("vm_object_vndeallocate: bad object reference count");
41395e5e988SJohn Dyson 	}
41495e5e988SJohn Dyson #endif
41595e5e988SJohn Dyson 
41695e5e988SJohn Dyson 	object->ref_count--;
41747221757SJohn Dyson 	if (object->ref_count == 0) {
418e6e370a7SJeff Roberson 		mp_fixme("Unlocked vflag access.");
419e6e370a7SJeff Roberson 		vp->v_vflag &= ~VV_TEXT;
4202be70f79SJohn Dyson 	}
421ad682c48SAlan Cox 	VM_OBJECT_UNLOCK(object);
42223955314SAlfred Perlstein 	/*
42323955314SAlfred Perlstein 	 * vrele may need a vop lock
42423955314SAlfred Perlstein 	 */
42547221757SJohn Dyson 	vrele(vp);
426df8bae1dSRodney W. Grimes }
427df8bae1dSRodney W. Grimes 
428df8bae1dSRodney W. Grimes /*
429df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
430df8bae1dSRodney W. Grimes  *
431df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
432df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
433df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
434df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
435df8bae1dSRodney W. Grimes  *	may be relinquished.
436df8bae1dSRodney W. Grimes  *
437df8bae1dSRodney W. Grimes  *	No object may be locked.
438df8bae1dSRodney W. Grimes  */
43926f9a767SRodney W. Grimes void
4401b40f8c0SMatthew Dillon vm_object_deallocate(vm_object_t object)
441df8bae1dSRodney W. Grimes {
442df8bae1dSRodney W. Grimes 	vm_object_t temp;
443df8bae1dSRodney W. Grimes 
444df8bae1dSRodney W. Grimes 	while (object != NULL) {
44523b186d3SAlan Cox 		/*
44623b186d3SAlan Cox 		 * In general, the object should be locked when working with
44723b186d3SAlan Cox 		 * its type.  In this case, in order to maintain proper lock
44823b186d3SAlan Cox 		 * ordering, an exception is possible because a vnode-backed
44923b186d3SAlan Cox 		 * object never changes its type.
45023b186d3SAlan Cox 		 */
45123b186d3SAlan Cox 		if (object->type == OBJT_VNODE)
45223b186d3SAlan Cox 			mtx_lock(&Giant);
453ad682c48SAlan Cox 		VM_OBJECT_LOCK(object);
4543b68228cSAlan Cox 		if (object->type == OBJT_VNODE) {
45595e5e988SJohn Dyson 			vm_object_vndeallocate(object);
45623b186d3SAlan Cox 			mtx_unlock(&Giant);
45723b186d3SAlan Cox 			return;
45895e5e988SJohn Dyson 		}
45995e5e988SJohn Dyson 
4608125b1e6SAlfred Perlstein 		KASSERT(object->ref_count != 0,
4618125b1e6SAlfred Perlstein 			("vm_object_deallocate: object deallocated too many times: %d", object->type));
4622be70f79SJohn Dyson 
4632be70f79SJohn Dyson 		/*
4648125b1e6SAlfred Perlstein 		 * If the reference count goes to 0 we start calling
4658125b1e6SAlfred Perlstein 		 * vm_object_terminate() on the object chain.
4668125b1e6SAlfred Perlstein 		 * A ref count of 1 may be a special case depending on the
4678125b1e6SAlfred Perlstein 		 * shadow count being 0 or 1.
4682be70f79SJohn Dyson 		 */
469c0877f10SJohn Dyson 		object->ref_count--;
4708125b1e6SAlfred Perlstein 		if (object->ref_count > 1) {
4713b68228cSAlan Cox 			VM_OBJECT_UNLOCK(object);
47223b186d3SAlan Cox 			return;
4738125b1e6SAlfred Perlstein 		} else if (object->ref_count == 1) {
4748125b1e6SAlfred Perlstein 			if (object->shadow_count == 0) {
4758125b1e6SAlfred Perlstein 				vm_object_set_flag(object, OBJ_ONEMAPPING);
4768125b1e6SAlfred Perlstein 			} else if ((object->shadow_count == 1) &&
4778125b1e6SAlfred Perlstein 			    (object->handle == NULL) &&
47824a1cce3SDavid Greenman 			    (object->type == OBJT_DEFAULT ||
47924a1cce3SDavid Greenman 			     object->type == OBJT_SWAP)) {
480a1f6d91cSDavid Greenman 				vm_object_t robject;
48195e5e988SJohn Dyson 
4821c500307SAlan Cox 				robject = LIST_FIRST(&object->shadow_head);
4835526d2d9SEivind Eklund 				KASSERT(robject != NULL,
484219cbf59SEivind Eklund 				    ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
4855526d2d9SEivind Eklund 					 object->ref_count,
4865526d2d9SEivind Eklund 					 object->shadow_count));
487b72b0115SAlan Cox 				if (!VM_OBJECT_TRYLOCK(robject)) {
488b72b0115SAlan Cox 					/*
489b72b0115SAlan Cox 					 * Avoid a potential deadlock.
490b72b0115SAlan Cox 					 */
491b72b0115SAlan Cox 					object->ref_count++;
492b72b0115SAlan Cox 					VM_OBJECT_UNLOCK(object);
493a7d86121SAlan Cox 					/*
494a7d86121SAlan Cox 					 * More likely than not the thread
495a7d86121SAlan Cox 					 * holding robject's lock has lower
496a7d86121SAlan Cox 					 * priority than the current thread.
497a7d86121SAlan Cox 					 * Let the lower priority thread run.
498a7d86121SAlan Cox 					 */
499a7d86121SAlan Cox 					tsleep(&proc0, PVM, "vmo_de", 1);
500b72b0115SAlan Cox 					continue;
501b72b0115SAlan Cox 				}
50295e5e988SJohn Dyson 				if ((robject->handle == NULL) &&
50324a1cce3SDavid Greenman 				    (robject->type == OBJT_DEFAULT ||
50424a1cce3SDavid Greenman 				     robject->type == OBJT_SWAP)) {
505a1f6d91cSDavid Greenman 
50695e5e988SJohn Dyson 					robject->ref_count++;
507138449dcSAlan Cox retry:
508138449dcSAlan Cox 					if (robject->paging_in_progress) {
509138449dcSAlan Cox 						VM_OBJECT_UNLOCK(object);
510138449dcSAlan Cox 						vm_object_pip_wait(robject,
511138449dcSAlan Cox 						    "objde1");
512138449dcSAlan Cox 						VM_OBJECT_LOCK(object);
513138449dcSAlan Cox 						goto retry;
514138449dcSAlan Cox 					} else if (object->paging_in_progress) {
515138449dcSAlan Cox 						VM_OBJECT_UNLOCK(robject);
516138449dcSAlan Cox 						object->flags |= OBJ_PIPWNT;
517138449dcSAlan Cox 						msleep(object,
518138449dcSAlan Cox 						    VM_OBJECT_MTX(object),
519138449dcSAlan Cox 						    PDROP | PVM, "objde2", 0);
520138449dcSAlan Cox 						VM_OBJECT_LOCK(robject);
521138449dcSAlan Cox 						VM_OBJECT_LOCK(object);
522138449dcSAlan Cox 						goto retry;
523a1f6d91cSDavid Greenman 					}
5243b68228cSAlan Cox 					VM_OBJECT_UNLOCK(object);
52595e5e988SJohn Dyson 					if (robject->ref_count == 1) {
52695e5e988SJohn Dyson 						robject->ref_count--;
527ba8da839SDavid Greenman 						object = robject;
52895e5e988SJohn Dyson 						goto doterm;
52995e5e988SJohn Dyson 					}
53095e5e988SJohn Dyson 					object = robject;
53195e5e988SJohn Dyson 					vm_object_collapse(object);
532d7fc2210SAlan Cox 					VM_OBJECT_UNLOCK(object);
533ba8da839SDavid Greenman 					continue;
534a1f6d91cSDavid Greenman 				}
535b72b0115SAlan Cox 				VM_OBJECT_UNLOCK(robject);
53695e5e988SJohn Dyson 			}
5373b68228cSAlan Cox 			VM_OBJECT_UNLOCK(object);
53823b186d3SAlan Cox 			return;
53995e5e988SJohn Dyson 		}
54095e5e988SJohn Dyson doterm:
54124a1cce3SDavid Greenman 		temp = object->backing_object;
542c9917419SAlan Cox 		if (temp != NULL) {
543c9917419SAlan Cox 			VM_OBJECT_LOCK(temp);
5441c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
54595e5e988SJohn Dyson 			temp->shadow_count--;
546eaf13dd7SJohn Dyson 			temp->generation++;
547c9917419SAlan Cox 			VM_OBJECT_UNLOCK(temp);
54895461b45SJohn Dyson 			object->backing_object = NULL;
549de5f6a77SJohn Dyson 		}
550245df27cSMatthew Dillon 		/*
551245df27cSMatthew Dillon 		 * Don't double-terminate, we could be in a termination
552245df27cSMatthew Dillon 		 * recursion due to the terminate having to sync data
553245df27cSMatthew Dillon 		 * to disk.
554245df27cSMatthew Dillon 		 */
555245df27cSMatthew Dillon 		if ((object->flags & OBJ_DEAD) == 0)
556df8bae1dSRodney W. Grimes 			vm_object_terminate(object);
557c829b9d0SAlan Cox 		else
558c829b9d0SAlan Cox 			VM_OBJECT_UNLOCK(object);
559df8bae1dSRodney W. Grimes 		object = temp;
560df8bae1dSRodney W. Grimes 	}
561df8bae1dSRodney W. Grimes }
562df8bae1dSRodney W. Grimes 
563df8bae1dSRodney W. Grimes /*
564df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
565df8bae1dSRodney W. Grimes  *	up all previously used resources.
566df8bae1dSRodney W. Grimes  *
567df8bae1dSRodney W. Grimes  *	The object must be locked.
5681c7c3c6aSMatthew Dillon  *	This routine may block.
569df8bae1dSRodney W. Grimes  */
57095e5e988SJohn Dyson void
5711b40f8c0SMatthew Dillon vm_object_terminate(vm_object_t object)
572df8bae1dSRodney W. Grimes {
573d031cff1SMatthew Dillon 	vm_page_t p;
574e4b7635dSDavid Greenman 	int s;
575df8bae1dSRodney W. Grimes 
576c829b9d0SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
5770cddd8f0SMatthew Dillon 
57895e5e988SJohn Dyson 	/*
57995e5e988SJohn Dyson 	 * Make sure no one uses us.
58095e5e988SJohn Dyson 	 */
581069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_DEAD);
5823c631446SJohn Dyson 
583df8bae1dSRodney W. Grimes 	/*
584f6b04d2bSDavid Greenman 	 * wait for the pageout daemon to be done with the object
585df8bae1dSRodney W. Grimes 	 */
58666095752SJohn Dyson 	vm_object_pip_wait(object, "objtrm");
587df8bae1dSRodney W. Grimes 
5885526d2d9SEivind Eklund 	KASSERT(!object->paging_in_progress,
5895526d2d9SEivind Eklund 		("vm_object_terminate: pageout in progress"));
59026f9a767SRodney W. Grimes 
59126f9a767SRodney W. Grimes 	/*
5920d94caffSDavid Greenman 	 * Clean and free the pages, as appropriate. All references to the
5930d94caffSDavid Greenman 	 * object are gone, so we don't need to lock it.
59426f9a767SRodney W. Grimes 	 */
59524a1cce3SDavid Greenman 	if (object->type == OBJT_VNODE) {
596f7dd7b63SAlan Cox 		struct vnode *vp = (struct vnode *)object->handle;
59795e5e988SJohn Dyson 
59895e5e988SJohn Dyson 		/*
59995e5e988SJohn Dyson 		 * Clean pages and flush buffers.
60095e5e988SJohn Dyson 		 */
6018f9110f6SJohn Dyson 		vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
602b6e48e03SAlan Cox 		VM_OBJECT_UNLOCK(object);
60395e5e988SJohn Dyson 
604f6b04d2bSDavid Greenman 		vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
605f7dd7b63SAlan Cox 
606f7dd7b63SAlan Cox 		VM_OBJECT_LOCK(object);
607bef608bdSJohn Dyson 	}
608bef608bdSJohn Dyson 
609971dd342SAlfred Perlstein 	KASSERT(object->ref_count == 0,
610971dd342SAlfred Perlstein 		("vm_object_terminate: object with references, ref_count=%d",
611971dd342SAlfred Perlstein 		object->ref_count));
612996c772fSJohn Dyson 
6130d94caffSDavid Greenman 	/*
614356863ebSDavid Greenman 	 * Now free any remaining pages. For internal objects, this also
615356863ebSDavid Greenman 	 * removes them from paging queues. Don't free wired pages, just
616356863ebSDavid Greenman 	 * remove them from the object.
617df8bae1dSRodney W. Grimes 	 */
618e4b7635dSDavid Greenman 	s = splvm();
61956030358SAlan Cox 	vm_page_lock_queues();
620b18bfc3dSJohn Dyson 	while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
621971dd342SAlfred Perlstein 		KASSERT(!p->busy && (p->flags & PG_BUSY) == 0,
622971dd342SAlfred Perlstein 			("vm_object_terminate: freeing busy page %p "
623971dd342SAlfred Perlstein 			"p->busy = %d, p->flags %x\n", p, p->busy, p->flags));
6240b10ba98SDavid Greenman 		if (p->wire_count == 0) {
625e69763a3SDoug Rabson 			vm_page_busy(p);
626df8bae1dSRodney W. Grimes 			vm_page_free(p);
627df8bae1dSRodney W. Grimes 			cnt.v_pfree++;
6280b10ba98SDavid Greenman 		} else {
629356863ebSDavid Greenman 			vm_page_busy(p);
6300b10ba98SDavid Greenman 			vm_page_remove(p);
6310b10ba98SDavid Greenman 		}
632df8bae1dSRodney W. Grimes 	}
63356030358SAlan Cox 	vm_page_unlock_queues();
634e4b7635dSDavid Greenman 	splx(s);
635bef608bdSJohn Dyson 
6362d8acc0fSJohn Dyson 	/*
6379fcfb650SDavid Greenman 	 * Let the pager know object is dead.
6389fcfb650SDavid Greenman 	 */
6399fcfb650SDavid Greenman 	vm_pager_deallocate(object);
640658ad5ffSAlan Cox 	VM_OBJECT_UNLOCK(object);
6419fcfb650SDavid Greenman 
6429fcfb650SDavid Greenman 	/*
6430b10ba98SDavid Greenman 	 * Remove the object from the global object list.
6442d8acc0fSJohn Dyson 	 */
6459ed346baSBosko Milekic 	mtx_lock(&vm_object_list_mtx);
646df8bae1dSRodney W. Grimes 	TAILQ_REMOVE(&vm_object_list, object, object_list);
6479ed346baSBosko Milekic 	mtx_unlock(&vm_object_list_mtx);
6480b10ba98SDavid Greenman 
6490b10ba98SDavid Greenman 	wakeup(object);
6500b10ba98SDavid Greenman 
651df8bae1dSRodney W. Grimes 	/*
652df8bae1dSRodney W. Grimes 	 * Free the space for the object.
653df8bae1dSRodney W. Grimes 	 */
654670d17b5SJeff Roberson 	uma_zfree(obj_zone, object);
65547221757SJohn Dyson }
656df8bae1dSRodney W. Grimes 
657df8bae1dSRodney W. Grimes /*
658df8bae1dSRodney W. Grimes  *	vm_object_page_clean
659df8bae1dSRodney W. Grimes  *
6604f79d873SMatthew Dillon  *	Clean all dirty pages in the specified range of object.  Leaves page
6614f79d873SMatthew Dillon  * 	on whatever queue it is currently on.   If NOSYNC is set then do not
6624f79d873SMatthew Dillon  *	write out pages with PG_NOSYNC set (originally comes from MAP_NOSYNC),
6634f79d873SMatthew Dillon  *	leaving the object dirty.
66426f9a767SRodney W. Grimes  *
66543b7990eSMatthew Dillon  *	When stuffing pages asynchronously, allow clustering.  XXX we need a
66643b7990eSMatthew Dillon  *	synchronous clustering mode implementation.
66743b7990eSMatthew Dillon  *
66826f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
66926f9a767SRodney W. Grimes  *
67026f9a767SRodney W. Grimes  *	The object must be locked.
67126f9a767SRodney W. Grimes  */
672f6b04d2bSDavid Greenman void
6731b40f8c0SMatthew Dillon vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end, int flags)
674f6b04d2bSDavid Greenman {
675b9b7a4beSMatthew Dillon 	vm_page_t p, np;
6766395da54SIan Dowse 	vm_pindex_t tstart, tend;
677bd7e5f99SJohn Dyson 	vm_pindex_t pi;
6784f79d873SMatthew Dillon 	int clearobjflags;
6798f9110f6SJohn Dyson 	int pagerflags;
6802d8acc0fSJohn Dyson 	int curgeneration;
681f6b04d2bSDavid Greenman 
6820cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
683b6e48e03SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
684aef922f5SJohn Dyson 	if (object->type != OBJT_VNODE ||
685aef922f5SJohn Dyson 		(object->flags & OBJ_MIGHTBEDIRTY) == 0)
686f6b04d2bSDavid Greenman 		return;
687f6b04d2bSDavid Greenman 
68843b7990eSMatthew Dillon 	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
6898f9110f6SJohn Dyson 	pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
6908f9110f6SJohn Dyson 
691069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_CLEANING);
69224a1cce3SDavid Greenman 
693f6b04d2bSDavid Greenman 	tstart = start;
694f6b04d2bSDavid Greenman 	if (end == 0) {
695f6b04d2bSDavid Greenman 		tend = object->size;
696f6b04d2bSDavid Greenman 	} else {
697f6b04d2bSDavid Greenman 		tend = end;
698f6b04d2bSDavid Greenman 	}
699eaf13dd7SJohn Dyson 
70035c01631SAlan Cox 	vm_page_lock_queues();
7014f79d873SMatthew Dillon 	/*
702b9b7a4beSMatthew Dillon 	 * If the caller is smart and only msync()s a range he knows is
703b9b7a4beSMatthew Dillon 	 * dirty, we may be able to avoid an object scan.  This results in
704b9b7a4beSMatthew Dillon 	 * a phenominal improvement in performance.  We cannot do this
705b9b7a4beSMatthew Dillon 	 * as a matter of course because the object may be huge - e.g.
706b9b7a4beSMatthew Dillon 	 * the size might be in the gigabytes or terrabytes.
707b9b7a4beSMatthew Dillon 	 */
708b9b7a4beSMatthew Dillon 	if (msync_flush_flags & MSYNC_FLUSH_HARDSEQ) {
709300b96acSIan Dowse 		vm_pindex_t tscan;
710b9b7a4beSMatthew Dillon 		int scanlimit;
711b9b7a4beSMatthew Dillon 		int scanreset;
712b9b7a4beSMatthew Dillon 
713b9b7a4beSMatthew Dillon 		scanreset = object->resident_page_count / EASY_SCAN_FACTOR;
714b9b7a4beSMatthew Dillon 		if (scanreset < 16)
715b9b7a4beSMatthew Dillon 			scanreset = 16;
71643b7990eSMatthew Dillon 		pagerflags |= VM_PAGER_IGNORE_CLEANCHK;
717b9b7a4beSMatthew Dillon 
718b9b7a4beSMatthew Dillon 		scanlimit = scanreset;
719b9b7a4beSMatthew Dillon 		tscan = tstart;
720b9b7a4beSMatthew Dillon 		while (tscan < tend) {
721b9b7a4beSMatthew Dillon 			curgeneration = object->generation;
722b9b7a4beSMatthew Dillon 			p = vm_page_lookup(object, tscan);
723b9b7a4beSMatthew Dillon 			if (p == NULL || p->valid == 0 ||
724b9b7a4beSMatthew Dillon 			    (p->queue - p->pc) == PQ_CACHE) {
725b9b7a4beSMatthew Dillon 				if (--scanlimit == 0)
726b9b7a4beSMatthew Dillon 					break;
727b9b7a4beSMatthew Dillon 				++tscan;
728b9b7a4beSMatthew Dillon 				continue;
729b9b7a4beSMatthew Dillon 			}
730b9b7a4beSMatthew Dillon 			vm_page_test_dirty(p);
731b9b7a4beSMatthew Dillon 			if ((p->dirty & p->valid) == 0) {
732b9b7a4beSMatthew Dillon 				if (--scanlimit == 0)
733b9b7a4beSMatthew Dillon 					break;
734b9b7a4beSMatthew Dillon 				++tscan;
735b9b7a4beSMatthew Dillon 				continue;
736b9b7a4beSMatthew Dillon 			}
737b9b7a4beSMatthew Dillon 			/*
738b9b7a4beSMatthew Dillon 			 * If we have been asked to skip nosync pages and
739b9b7a4beSMatthew Dillon 			 * this is a nosync page, we can't continue.
740b9b7a4beSMatthew Dillon 			 */
741b9b7a4beSMatthew Dillon 			if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
742b9b7a4beSMatthew Dillon 				if (--scanlimit == 0)
743b9b7a4beSMatthew Dillon 					break;
744b9b7a4beSMatthew Dillon 				++tscan;
745b9b7a4beSMatthew Dillon 				continue;
746b9b7a4beSMatthew Dillon 			}
747b9b7a4beSMatthew Dillon 			scanlimit = scanreset;
748b9b7a4beSMatthew Dillon 
749b9b7a4beSMatthew Dillon 			/*
750b9b7a4beSMatthew Dillon 			 * This returns 0 if it was unable to busy the first
751b9b7a4beSMatthew Dillon 			 * page (i.e. had to sleep).
752b9b7a4beSMatthew Dillon 			 */
753b9b7a4beSMatthew Dillon 			tscan += vm_object_page_collect_flush(object, p, curgeneration, pagerflags);
754b9b7a4beSMatthew Dillon 		}
755b9b7a4beSMatthew Dillon 
756b9b7a4beSMatthew Dillon 		/*
757b9b7a4beSMatthew Dillon 		 * If everything was dirty and we flushed it successfully,
758b9b7a4beSMatthew Dillon 		 * and the requested range is not the entire object, we
759b9b7a4beSMatthew Dillon 		 * don't have to mess with CLEANCHK or MIGHTBEDIRTY and can
760b9b7a4beSMatthew Dillon 		 * return immediately.
761b9b7a4beSMatthew Dillon 		 */
762b9b7a4beSMatthew Dillon 		if (tscan >= tend && (tstart || tend < object->size)) {
76335c01631SAlan Cox 			vm_page_unlock_queues();
764b9b7a4beSMatthew Dillon 			vm_object_clear_flag(object, OBJ_CLEANING);
765b9b7a4beSMatthew Dillon 			return;
766b9b7a4beSMatthew Dillon 		}
76743b7990eSMatthew Dillon 		pagerflags &= ~VM_PAGER_IGNORE_CLEANCHK;
768b9b7a4beSMatthew Dillon 	}
769b9b7a4beSMatthew Dillon 
770b9b7a4beSMatthew Dillon 	/*
7714f79d873SMatthew Dillon 	 * Generally set CLEANCHK interlock and make the page read-only so
7724f79d873SMatthew Dillon 	 * we can then clear the object flags.
7734f79d873SMatthew Dillon 	 *
7744f79d873SMatthew Dillon 	 * However, if this is a nosync mmap then the object is likely to
7754f79d873SMatthew Dillon 	 * stay dirty so do not mess with the page and do not clear the
7764f79d873SMatthew Dillon 	 * object flags.
7774f79d873SMatthew Dillon 	 */
7784f79d873SMatthew Dillon 	clearobjflags = 1;
779fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(p, &object->memq, listq) {
780e69763a3SDoug Rabson 		vm_page_flag_set(p, PG_CLEANCHK);
7814f79d873SMatthew Dillon 		if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC))
7824f79d873SMatthew Dillon 			clearobjflags = 0;
7834f79d873SMatthew Dillon 		else
784a12cc0e4SAlan Cox 			pmap_page_protect(p, VM_PROT_READ);
785eaf13dd7SJohn Dyson 	}
786eaf13dd7SJohn Dyson 
7874f79d873SMatthew Dillon 	if (clearobjflags && (tstart == 0) && (tend == object->size)) {
788245df27cSMatthew Dillon 		struct vnode *vp;
789245df27cSMatthew Dillon 
790069e9bc1SDoug Rabson 		vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
791245df27cSMatthew Dillon 		if (object->type == OBJT_VNODE &&
792245df27cSMatthew Dillon 		    (vp = (struct vnode *)object->handle) != NULL) {
793e6e370a7SJeff Roberson 			VI_LOCK(vp);
794e6e370a7SJeff Roberson 			if (vp->v_iflag & VI_OBJDIRTY)
795e6e370a7SJeff Roberson 				vp->v_iflag &= ~VI_OBJDIRTY;
796e6e370a7SJeff Roberson 			VI_UNLOCK(vp);
797245df27cSMatthew Dillon 		}
798ec4f9fb0SDavid Greenman 	}
799f6b04d2bSDavid Greenman 
800bd7e5f99SJohn Dyson rescan:
8012d8acc0fSJohn Dyson 	curgeneration = object->generation;
8022d8acc0fSJohn Dyson 
803b18bfc3dSJohn Dyson 	for (p = TAILQ_FIRST(&object->memq); p; p = np) {
804b9b7a4beSMatthew Dillon 		int n;
805b9b7a4beSMatthew Dillon 
806b18bfc3dSJohn Dyson 		np = TAILQ_NEXT(p, listq);
807bd7e5f99SJohn Dyson 
808b9b7a4beSMatthew Dillon again:
809bd7e5f99SJohn Dyson 		pi = p->pindex;
810bd7e5f99SJohn Dyson 		if (((p->flags & PG_CLEANCHK) == 0) ||
811bd7e5f99SJohn Dyson 			(pi < tstart) || (pi >= tend) ||
8125070c7f8SJohn Dyson 			(p->valid == 0) ||
8135070c7f8SJohn Dyson 			((p->queue - p->pc) == PQ_CACHE)) {
814e69763a3SDoug Rabson 			vm_page_flag_clear(p, PG_CLEANCHK);
815aef922f5SJohn Dyson 			continue;
816f6b04d2bSDavid Greenman 		}
817f6b04d2bSDavid Greenman 
818bd7e5f99SJohn Dyson 		vm_page_test_dirty(p);
819bd7e5f99SJohn Dyson 		if ((p->dirty & p->valid) == 0) {
820e69763a3SDoug Rabson 			vm_page_flag_clear(p, PG_CLEANCHK);
821bd7e5f99SJohn Dyson 			continue;
822bd7e5f99SJohn Dyson 		}
823ec4f9fb0SDavid Greenman 
8244f79d873SMatthew Dillon 		/*
8254f79d873SMatthew Dillon 		 * If we have been asked to skip nosync pages and this is a
8264f79d873SMatthew Dillon 		 * nosync page, skip it.  Note that the object flags were
8274f79d873SMatthew Dillon 		 * not cleared in this case so we do not have to set them.
8284f79d873SMatthew Dillon 		 */
8294f79d873SMatthew Dillon 		if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
8304f79d873SMatthew Dillon 			vm_page_flag_clear(p, PG_CLEANCHK);
8314f79d873SMatthew Dillon 			continue;
8324f79d873SMatthew Dillon 		}
8334f79d873SMatthew Dillon 
834b9b7a4beSMatthew Dillon 		n = vm_object_page_collect_flush(object, p,
835b9b7a4beSMatthew Dillon 			curgeneration, pagerflags);
836b9b7a4beSMatthew Dillon 		if (n == 0)
837b9b7a4beSMatthew Dillon 			goto rescan;
838b9b7a4beSMatthew Dillon 
839b9b7a4beSMatthew Dillon 		if (object->generation != curgeneration)
840b9b7a4beSMatthew Dillon 			goto rescan;
841b9b7a4beSMatthew Dillon 
842b9b7a4beSMatthew Dillon 		/*
843b9b7a4beSMatthew Dillon 		 * Try to optimize the next page.  If we can't we pick up
844b9b7a4beSMatthew Dillon 		 * our (random) scan where we left off.
845b9b7a4beSMatthew Dillon 		 */
846b9b7a4beSMatthew Dillon 		if (msync_flush_flags & MSYNC_FLUSH_SOFTSEQ) {
847b9b7a4beSMatthew Dillon 			if ((p = vm_page_lookup(object, pi + n)) != NULL)
848b9b7a4beSMatthew Dillon 				goto again;
849b9b7a4beSMatthew Dillon 		}
850b9b7a4beSMatthew Dillon 	}
85135c01631SAlan Cox 	vm_page_unlock_queues();
852b9b7a4beSMatthew Dillon #if 0
853b9b7a4beSMatthew Dillon 	VOP_FSYNC(vp, NULL, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
854b9b7a4beSMatthew Dillon #endif
855b9b7a4beSMatthew Dillon 
856b9b7a4beSMatthew Dillon 	vm_object_clear_flag(object, OBJ_CLEANING);
857b9b7a4beSMatthew Dillon 	return;
858b9b7a4beSMatthew Dillon }
859b9b7a4beSMatthew Dillon 
860b9b7a4beSMatthew Dillon static int
861b9b7a4beSMatthew Dillon vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags)
862b9b7a4beSMatthew Dillon {
863b9b7a4beSMatthew Dillon 	int runlen;
864b9b7a4beSMatthew Dillon 	int s;
865b9b7a4beSMatthew Dillon 	int maxf;
866b9b7a4beSMatthew Dillon 	int chkb;
867b9b7a4beSMatthew Dillon 	int maxb;
868b9b7a4beSMatthew Dillon 	int i;
869b9b7a4beSMatthew Dillon 	vm_pindex_t pi;
870b9b7a4beSMatthew Dillon 	vm_page_t maf[vm_pageout_page_count];
871b9b7a4beSMatthew Dillon 	vm_page_t mab[vm_pageout_page_count];
872b9b7a4beSMatthew Dillon 	vm_page_t ma[vm_pageout_page_count];
873b9b7a4beSMatthew Dillon 
874b18bfc3dSJohn Dyson 	s = splvm();
87535c01631SAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
876b9b7a4beSMatthew Dillon 	pi = p->pindex;
877bd82dc74SAlan Cox 	while (vm_page_sleep_if_busy(p, TRUE, "vpcwai")) {
87835c01631SAlan Cox 		vm_page_lock_queues();
8792d8acc0fSJohn Dyson 		if (object->generation != curgeneration) {
880f6b04d2bSDavid Greenman 			splx(s);
881b9b7a4beSMatthew Dillon 			return(0);
882f6b04d2bSDavid Greenman 		}
883bd82dc74SAlan Cox 	}
884bd7e5f99SJohn Dyson 	maxf = 0;
885bd7e5f99SJohn Dyson 	for(i = 1; i < vm_pageout_page_count; i++) {
886b9b7a4beSMatthew Dillon 		vm_page_t tp;
887b9b7a4beSMatthew Dillon 
8888aef1712SMatthew Dillon 		if ((tp = vm_page_lookup(object, pi + i)) != NULL) {
889bd7e5f99SJohn Dyson 			if ((tp->flags & PG_BUSY) ||
89043b7990eSMatthew Dillon 				((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
89143b7990eSMatthew Dillon 				 (tp->flags & PG_CLEANCHK) == 0) ||
892ffc82b0aSJohn Dyson 				(tp->busy != 0))
893bd7e5f99SJohn Dyson 				break;
8945070c7f8SJohn Dyson 			if((tp->queue - tp->pc) == PQ_CACHE) {
895e69763a3SDoug Rabson 				vm_page_flag_clear(tp, PG_CLEANCHK);
8963077a9c2SJohn Dyson 				break;
8973077a9c2SJohn Dyson 			}
898bd7e5f99SJohn Dyson 			vm_page_test_dirty(tp);
899bd7e5f99SJohn Dyson 			if ((tp->dirty & tp->valid) == 0) {
900e69763a3SDoug Rabson 				vm_page_flag_clear(tp, PG_CLEANCHK);
901bd7e5f99SJohn Dyson 				break;
902bd7e5f99SJohn Dyson 			}
903bd7e5f99SJohn Dyson 			maf[ i - 1 ] = tp;
904bd7e5f99SJohn Dyson 			maxf++;
905bd7e5f99SJohn Dyson 			continue;
906bd7e5f99SJohn Dyson 		}
907bd7e5f99SJohn Dyson 		break;
908bd7e5f99SJohn Dyson 	}
909aef922f5SJohn Dyson 
910bd7e5f99SJohn Dyson 	maxb = 0;
911bd7e5f99SJohn Dyson 	chkb = vm_pageout_page_count -  maxf;
912bd7e5f99SJohn Dyson 	if (chkb) {
913bd7e5f99SJohn Dyson 		for(i = 1; i < chkb;i++) {
914b9b7a4beSMatthew Dillon 			vm_page_t tp;
915b9b7a4beSMatthew Dillon 
9168aef1712SMatthew Dillon 			if ((tp = vm_page_lookup(object, pi - i)) != NULL) {
917bd7e5f99SJohn Dyson 				if ((tp->flags & PG_BUSY) ||
91843b7990eSMatthew Dillon 					((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
91943b7990eSMatthew Dillon 					 (tp->flags & PG_CLEANCHK) == 0) ||
920ffc82b0aSJohn Dyson 					(tp->busy != 0))
921bd7e5f99SJohn Dyson 					break;
9225070c7f8SJohn Dyson 				if ((tp->queue - tp->pc) == PQ_CACHE) {
923e69763a3SDoug Rabson 					vm_page_flag_clear(tp, PG_CLEANCHK);
9243077a9c2SJohn Dyson 					break;
9253077a9c2SJohn Dyson 				}
926bd7e5f99SJohn Dyson 				vm_page_test_dirty(tp);
927bd7e5f99SJohn Dyson 				if ((tp->dirty & tp->valid) == 0) {
928e69763a3SDoug Rabson 					vm_page_flag_clear(tp, PG_CLEANCHK);
929bd7e5f99SJohn Dyson 					break;
930bd7e5f99SJohn Dyson 				}
931bd7e5f99SJohn Dyson 				mab[ i - 1 ] = tp;
932bd7e5f99SJohn Dyson 				maxb++;
933bd7e5f99SJohn Dyson 				continue;
934bd7e5f99SJohn Dyson 			}
935bd7e5f99SJohn Dyson 			break;
936bd7e5f99SJohn Dyson 		}
937bd7e5f99SJohn Dyson 	}
938bd7e5f99SJohn Dyson 
939bd7e5f99SJohn Dyson 	for(i = 0; i < maxb; i++) {
940bd7e5f99SJohn Dyson 		int index = (maxb - i) - 1;
941bd7e5f99SJohn Dyson 		ma[index] = mab[i];
942e69763a3SDoug Rabson 		vm_page_flag_clear(ma[index], PG_CLEANCHK);
943bd7e5f99SJohn Dyson 	}
944e69763a3SDoug Rabson 	vm_page_flag_clear(p, PG_CLEANCHK);
945bd7e5f99SJohn Dyson 	ma[maxb] = p;
946bd7e5f99SJohn Dyson 	for(i = 0; i < maxf; i++) {
947bd7e5f99SJohn Dyson 		int index = (maxb + i) + 1;
948bd7e5f99SJohn Dyson 		ma[index] = maf[i];
949e69763a3SDoug Rabson 		vm_page_flag_clear(ma[index], PG_CLEANCHK);
950f6b04d2bSDavid Greenman 	}
951bd7e5f99SJohn Dyson 	runlen = maxb + maxf + 1;
952cf2819ccSJohn Dyson 
953f35329acSJohn Dyson 	splx(s);
9547a935082SAlan Cox 	vm_pageout_flush(ma, runlen, pagerflags);
955cf2819ccSJohn Dyson 	for (i = 0; i < runlen; i++) {
956cf2819ccSJohn Dyson 		if (ma[i]->valid & ma[i]->dirty) {
957a12cc0e4SAlan Cox 			pmap_page_protect(ma[i], VM_PROT_READ);
958e69763a3SDoug Rabson 			vm_page_flag_set(ma[i], PG_CLEANCHK);
959aef922f5SJohn Dyson 
960b9b7a4beSMatthew Dillon 			/*
961b9b7a4beSMatthew Dillon 			 * maxf will end up being the actual number of pages
962b9b7a4beSMatthew Dillon 			 * we wrote out contiguously, non-inclusive of the
963b9b7a4beSMatthew Dillon 			 * first page.  We do not count look-behind pages.
964b9b7a4beSMatthew Dillon 			 */
965b9b7a4beSMatthew Dillon 			if (i >= maxb + 1 && (maxf > i - maxb - 1))
966b9b7a4beSMatthew Dillon 				maxf = i - maxb - 1;
967b9b7a4beSMatthew Dillon 		}
968b9b7a4beSMatthew Dillon 	}
969b9b7a4beSMatthew Dillon 	return(maxf + 1);
97026f9a767SRodney W. Grimes }
971df8bae1dSRodney W. Grimes 
9721efb74fbSJohn Dyson /*
973950f8459SAlan Cox  * Note that there is absolutely no sense in writing out
974950f8459SAlan Cox  * anonymous objects, so we track down the vnode object
975950f8459SAlan Cox  * to write out.
976950f8459SAlan Cox  * We invalidate (remove) all pages from the address space
977950f8459SAlan Cox  * for semantic correctness.
978950f8459SAlan Cox  *
979950f8459SAlan Cox  * Note: certain anonymous maps, such as MAP_NOSYNC maps,
980950f8459SAlan Cox  * may start out with a NULL object.
981950f8459SAlan Cox  */
982950f8459SAlan Cox void
983950f8459SAlan Cox vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size,
984950f8459SAlan Cox     boolean_t syncio, boolean_t invalidate)
985950f8459SAlan Cox {
986950f8459SAlan Cox 	vm_object_t backing_object;
987950f8459SAlan Cox 	struct vnode *vp;
988950f8459SAlan Cox 	int flags;
989950f8459SAlan Cox 
990950f8459SAlan Cox 	if (object == NULL)
991950f8459SAlan Cox 		return;
992950f8459SAlan Cox 	VM_OBJECT_LOCK(object);
993950f8459SAlan Cox 	while ((backing_object = object->backing_object) != NULL) {
994950f8459SAlan Cox 		VM_OBJECT_LOCK(backing_object);
995950f8459SAlan Cox 		VM_OBJECT_UNLOCK(object);
996950f8459SAlan Cox 		object = backing_object;
997950f8459SAlan Cox 		offset += object->backing_object_offset;
998950f8459SAlan Cox 		if (object->size < OFF_TO_IDX(offset + size))
999950f8459SAlan Cox 			size = IDX_TO_OFF(object->size) - offset;
1000950f8459SAlan Cox 	}
1001950f8459SAlan Cox 	/*
1002950f8459SAlan Cox 	 * Flush pages if writing is allowed, invalidate them
1003950f8459SAlan Cox 	 * if invalidation requested.  Pages undergoing I/O
1004950f8459SAlan Cox 	 * will be ignored by vm_object_page_remove().
1005950f8459SAlan Cox 	 *
1006950f8459SAlan Cox 	 * We cannot lock the vnode and then wait for paging
1007950f8459SAlan Cox 	 * to complete without deadlocking against vm_fault.
1008950f8459SAlan Cox 	 * Instead we simply call vm_object_page_remove() and
1009950f8459SAlan Cox 	 * allow it to block internally on a page-by-page
1010950f8459SAlan Cox 	 * basis when it encounters pages undergoing async
1011950f8459SAlan Cox 	 * I/O.
1012950f8459SAlan Cox 	 */
1013950f8459SAlan Cox 	if (object->type == OBJT_VNODE &&
1014950f8459SAlan Cox 	    (object->flags & OBJ_MIGHTBEDIRTY) != 0) {
1015950f8459SAlan Cox 		vp = object->handle;
1016950f8459SAlan Cox 		VM_OBJECT_UNLOCK(object);
1017950f8459SAlan Cox 		mtx_lock(&Giant);
1018950f8459SAlan Cox 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
1019950f8459SAlan Cox 		flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
1020950f8459SAlan Cox 		flags |= invalidate ? OBJPC_INVAL : 0;
1021950f8459SAlan Cox 		VM_OBJECT_LOCK(object);
1022950f8459SAlan Cox 		vm_object_page_clean(object,
1023950f8459SAlan Cox 		    OFF_TO_IDX(offset),
1024950f8459SAlan Cox 		    OFF_TO_IDX(offset + size + PAGE_MASK),
1025950f8459SAlan Cox 		    flags);
1026950f8459SAlan Cox 		VM_OBJECT_UNLOCK(object);
1027950f8459SAlan Cox 		VOP_UNLOCK(vp, 0, curthread);
1028950f8459SAlan Cox 		mtx_unlock(&Giant);
1029950f8459SAlan Cox 		VM_OBJECT_LOCK(object);
1030950f8459SAlan Cox 	}
1031950f8459SAlan Cox 	if ((object->type == OBJT_VNODE ||
1032950f8459SAlan Cox 	     object->type == OBJT_DEVICE) && invalidate) {
1033950f8459SAlan Cox 		vm_object_page_remove(object,
1034950f8459SAlan Cox 		    OFF_TO_IDX(offset),
1035950f8459SAlan Cox 		    OFF_TO_IDX(offset + size + PAGE_MASK),
1036c53f7aceSDag-Erling Smørgrav 		    old_msync ? FALSE : TRUE);
1037950f8459SAlan Cox 	}
1038950f8459SAlan Cox 	VM_OBJECT_UNLOCK(object);
1039950f8459SAlan Cox }
1040950f8459SAlan Cox 
1041950f8459SAlan Cox /*
1042867a482dSJohn Dyson  *	vm_object_madvise:
1043867a482dSJohn Dyson  *
1044867a482dSJohn Dyson  *	Implements the madvise function at the object/page level.
10451c7c3c6aSMatthew Dillon  *
1046193b9358SAlan Cox  *	MADV_WILLNEED	(any object)
1047193b9358SAlan Cox  *
1048193b9358SAlan Cox  *	    Activate the specified pages if they are resident.
1049193b9358SAlan Cox  *
1050193b9358SAlan Cox  *	MADV_DONTNEED	(any object)
1051193b9358SAlan Cox  *
1052193b9358SAlan Cox  *	    Deactivate the specified pages if they are resident.
1053193b9358SAlan Cox  *
1054193b9358SAlan Cox  *	MADV_FREE	(OBJT_DEFAULT/OBJT_SWAP objects,
1055193b9358SAlan Cox  *			 OBJ_ONEMAPPING only)
1056193b9358SAlan Cox  *
1057193b9358SAlan Cox  *	    Deactivate and clean the specified pages if they are
1058193b9358SAlan Cox  *	    resident.  This permits the process to reuse the pages
1059193b9358SAlan Cox  *	    without faulting or the kernel to reclaim the pages
1060193b9358SAlan Cox  *	    without I/O.
1061867a482dSJohn Dyson  */
1062867a482dSJohn Dyson void
10631b40f8c0SMatthew Dillon vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
1064867a482dSJohn Dyson {
10656e20a165SJohn Dyson 	vm_pindex_t end, tpindex;
106634567de7SAlan Cox 	vm_object_t backing_object, tobject;
1067867a482dSJohn Dyson 	vm_page_t m;
1068867a482dSJohn Dyson 
1069867a482dSJohn Dyson 	if (object == NULL)
1070867a482dSJohn Dyson 		return;
1071867a482dSJohn Dyson 	end = pindex + count;
10721c7c3c6aSMatthew Dillon 	/*
10731c7c3c6aSMatthew Dillon 	 * Locate and adjust resident pages
10741c7c3c6aSMatthew Dillon 	 */
10751c7c3c6aSMatthew Dillon 	for (; pindex < end; pindex += 1) {
10766e20a165SJohn Dyson relookup:
10776e20a165SJohn Dyson 		tobject = object;
10786e20a165SJohn Dyson 		tpindex = pindex;
107934567de7SAlan Cox 		VM_OBJECT_LOCK(tobject);
10806e20a165SJohn Dyson shadowlookup:
108158b4e6ccSAlan Cox 		/*
108258b4e6ccSAlan Cox 		 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
108358b4e6ccSAlan Cox 		 * and those pages must be OBJ_ONEMAPPING.
108458b4e6ccSAlan Cox 		 */
108558b4e6ccSAlan Cox 		if (advise == MADV_FREE) {
108658b4e6ccSAlan Cox 			if ((tobject->type != OBJT_DEFAULT &&
108758b4e6ccSAlan Cox 			     tobject->type != OBJT_SWAP) ||
108858b4e6ccSAlan Cox 			    (tobject->flags & OBJ_ONEMAPPING) == 0) {
108934567de7SAlan Cox 				goto unlock_tobject;
10906e20a165SJohn Dyson 			}
109158b4e6ccSAlan Cox 		}
10921c7c3c6aSMatthew Dillon 		m = vm_page_lookup(tobject, tpindex);
10931c7c3c6aSMatthew Dillon 		if (m == NULL) {
10941ce137beSMatthew Dillon 			/*
10951ce137beSMatthew Dillon 			 * There may be swap even if there is no backing page
10961ce137beSMatthew Dillon 			 */
10971ce137beSMatthew Dillon 			if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
10981ce137beSMatthew Dillon 				swap_pager_freespace(tobject, tpindex, 1);
10991ce137beSMatthew Dillon 			/*
11001ce137beSMatthew Dillon 			 * next object
11011ce137beSMatthew Dillon 			 */
110234567de7SAlan Cox 			backing_object = tobject->backing_object;
110334567de7SAlan Cox 			if (backing_object == NULL)
110434567de7SAlan Cox 				goto unlock_tobject;
110534567de7SAlan Cox 			VM_OBJECT_LOCK(backing_object);
110634567de7SAlan Cox 			VM_OBJECT_UNLOCK(tobject);
110734567de7SAlan Cox 			tobject = backing_object;
11086e20a165SJohn Dyson 			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
11096e20a165SJohn Dyson 			goto shadowlookup;
11106e20a165SJohn Dyson 		}
1111867a482dSJohn Dyson 		/*
1112867a482dSJohn Dyson 		 * If the page is busy or not in a normal active state,
11138b03c8edSMatthew Dillon 		 * we skip it.  If the page is not managed there are no
11148b03c8edSMatthew Dillon 		 * page queues to mess with.  Things can break if we mess
11158b03c8edSMatthew Dillon 		 * with pages in any of the below states.
1116867a482dSJohn Dyson 		 */
111732585dd6SAlan Cox 		vm_page_lock_queues();
111832585dd6SAlan Cox 		if (m->hold_count ||
11191c7c3c6aSMatthew Dillon 		    m->wire_count ||
11208b03c8edSMatthew Dillon 		    (m->flags & PG_UNMANAGED) ||
112132585dd6SAlan Cox 		    m->valid != VM_PAGE_BITS_ALL) {
112232585dd6SAlan Cox 			vm_page_unlock_queues();
112334567de7SAlan Cox 			goto unlock_tobject;
11246e20a165SJohn Dyson 		}
112534567de7SAlan Cox  		if (vm_page_sleep_if_busy(m, TRUE, "madvpo")) {
112634567de7SAlan Cox 			VM_OBJECT_UNLOCK(tobject);
11276e20a165SJohn Dyson   			goto relookup;
112834567de7SAlan Cox 		}
1129867a482dSJohn Dyson 		if (advise == MADV_WILLNEED) {
1130867a482dSJohn Dyson 			vm_page_activate(m);
11316e20a165SJohn Dyson 		} else if (advise == MADV_DONTNEED) {
1132479112dfSMatthew Dillon 			vm_page_dontneed(m);
11330a47b48bSJohn Dyson 		} else if (advise == MADV_FREE) {
11341c7c3c6aSMatthew Dillon 			/*
11352aaeadf8SMatthew Dillon 			 * Mark the page clean.  This will allow the page
11362aaeadf8SMatthew Dillon 			 * to be freed up by the system.  However, such pages
11372aaeadf8SMatthew Dillon 			 * are often reused quickly by malloc()/free()
11382aaeadf8SMatthew Dillon 			 * so we do not do anything that would cause
11392aaeadf8SMatthew Dillon 			 * a page fault if we can help it.
11402aaeadf8SMatthew Dillon 			 *
11412aaeadf8SMatthew Dillon 			 * Specifically, we do not try to actually free
11422aaeadf8SMatthew Dillon 			 * the page now nor do we try to put it in the
11432aaeadf8SMatthew Dillon 			 * cache (which would cause a page fault on reuse).
114441c67e12SMatthew Dillon 			 *
114541c67e12SMatthew Dillon 			 * But we do make the page is freeable as we
114641c67e12SMatthew Dillon 			 * can without actually taking the step of unmapping
114741c67e12SMatthew Dillon 			 * it.
11481c7c3c6aSMatthew Dillon 			 */
11490385347cSPeter Wemm 			pmap_clear_modify(m);
11506e20a165SJohn Dyson 			m->dirty = 0;
115141c67e12SMatthew Dillon 			m->act_count = 0;
1152479112dfSMatthew Dillon 			vm_page_dontneed(m);
1153867a482dSJohn Dyson 		}
11542999e9faSAlan Cox 		vm_page_unlock_queues();
11552999e9faSAlan Cox 		if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
11562999e9faSAlan Cox 			swap_pager_freespace(tobject, tpindex, 1);
115734567de7SAlan Cox unlock_tobject:
115834567de7SAlan Cox 		VM_OBJECT_UNLOCK(tobject);
1159867a482dSJohn Dyson 	}
1160867a482dSJohn Dyson }
1161867a482dSJohn Dyson 
1162867a482dSJohn Dyson /*
1163df8bae1dSRodney W. Grimes  *	vm_object_shadow:
1164df8bae1dSRodney W. Grimes  *
1165df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
1166df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
1167df8bae1dSRodney W. Grimes  *	object reference is deallocated.
1168df8bae1dSRodney W. Grimes  *
1169df8bae1dSRodney W. Grimes  *	The new object and offset into that object
1170df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
1171df8bae1dSRodney W. Grimes  */
117226f9a767SRodney W. Grimes void
11731b40f8c0SMatthew Dillon vm_object_shadow(
11741b40f8c0SMatthew Dillon 	vm_object_t *object,	/* IN/OUT */
11751b40f8c0SMatthew Dillon 	vm_ooffset_t *offset,	/* IN/OUT */
11761b40f8c0SMatthew Dillon 	vm_size_t length)
1177df8bae1dSRodney W. Grimes {
1178d031cff1SMatthew Dillon 	vm_object_t source;
1179d031cff1SMatthew Dillon 	vm_object_t result;
1180df8bae1dSRodney W. Grimes 
1181df8bae1dSRodney W. Grimes 	source = *object;
1182df8bae1dSRodney W. Grimes 
1183df8bae1dSRodney W. Grimes 	/*
11849a2f6362SAlan Cox 	 * Don't create the new object if the old object isn't shared.
11859a2f6362SAlan Cox 	 */
1186570a2f4aSAlan Cox 	if (source != NULL) {
1187570a2f4aSAlan Cox 		VM_OBJECT_LOCK(source);
1188570a2f4aSAlan Cox 		if (source->ref_count == 1 &&
11899a2f6362SAlan Cox 		    source->handle == NULL &&
11909a2f6362SAlan Cox 		    (source->type == OBJT_DEFAULT ||
11919917e010SAlan Cox 		     source->type == OBJT_SWAP)) {
1192570a2f4aSAlan Cox 			VM_OBJECT_UNLOCK(source);
11939a2f6362SAlan Cox 			return;
11949917e010SAlan Cox 		}
1195570a2f4aSAlan Cox 		VM_OBJECT_UNLOCK(source);
1196570a2f4aSAlan Cox 	}
11979a2f6362SAlan Cox 
11989a2f6362SAlan Cox 	/*
1199570a2f4aSAlan Cox 	 * Allocate a new object with the given length.
1200df8bae1dSRodney W. Grimes 	 */
1201971dd342SAlfred Perlstein 	result = vm_object_allocate(OBJT_DEFAULT, length);
1202df8bae1dSRodney W. Grimes 
1203df8bae1dSRodney W. Grimes 	/*
12040d94caffSDavid Greenman 	 * The new object shadows the source object, adding a reference to it.
12050d94caffSDavid Greenman 	 * Our caller changes his reference to point to the new object,
12060d94caffSDavid Greenman 	 * removing a reference to the source object.  Net result: no change
12070d94caffSDavid Greenman 	 * of reference count.
12089b09fe24SMatthew Dillon 	 *
12099b09fe24SMatthew Dillon 	 * Try to optimize the result object's page color when shadowing
1210956f3135SPhilippe Charnier 	 * in order to maintain page coloring consistency in the combined
12119b09fe24SMatthew Dillon 	 * shadowed object.
1212df8bae1dSRodney W. Grimes 	 */
121324a1cce3SDavid Greenman 	result->backing_object = source;
12149174ca7bSTor Egge 	/*
12159174ca7bSTor Egge 	 * Store the offset into the source object, and fix up the offset into
12169174ca7bSTor Egge 	 * the new object.
12179174ca7bSTor Egge 	 */
12189174ca7bSTor Egge 	result->backing_object_offset = *offset;
1219570a2f4aSAlan Cox 	if (source != NULL) {
1220570a2f4aSAlan Cox 		VM_OBJECT_LOCK(source);
12211c500307SAlan Cox 		LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1222eaf13dd7SJohn Dyson 		source->shadow_count++;
1223eaf13dd7SJohn Dyson 		source->generation++;
1224dcc5840eSAlan Cox 		if (length < source->size)
1225dcc5840eSAlan Cox 			length = source->size;
1226dcc5840eSAlan Cox 		if (length > PQ_L2_SIZE / 3 + PQ_PRIME1 ||
1227dcc5840eSAlan Cox 		    source->generation > 1)
1228dcc5840eSAlan Cox 			length = PQ_L2_SIZE / 3 + PQ_PRIME1;
1229dcc5840eSAlan Cox 		result->pg_color = (source->pg_color +
1230dcc5840eSAlan Cox 		    length * source->generation) & PQ_L2_MASK;
1231570a2f4aSAlan Cox 		VM_OBJECT_UNLOCK(source);
1232dcc5840eSAlan Cox 		next_index = (result->pg_color + PQ_L2_SIZE / 3 + PQ_PRIME1) &
1233dcc5840eSAlan Cox 		    PQ_L2_MASK;
1234de5f6a77SJohn Dyson 	}
1235df8bae1dSRodney W. Grimes 
1236df8bae1dSRodney W. Grimes 
1237df8bae1dSRodney W. Grimes 	/*
1238df8bae1dSRodney W. Grimes 	 * Return the new things
1239df8bae1dSRodney W. Grimes 	 */
1240df8bae1dSRodney W. Grimes 	*offset = 0;
1241df8bae1dSRodney W. Grimes 	*object = result;
1242df8bae1dSRodney W. Grimes }
1243df8bae1dSRodney W. Grimes 
1244c5aaa06dSAlan Cox /*
1245c5aaa06dSAlan Cox  *	vm_object_split:
1246c5aaa06dSAlan Cox  *
1247c5aaa06dSAlan Cox  * Split the pages in a map entry into a new object.  This affords
1248c5aaa06dSAlan Cox  * easier removal of unused pages, and keeps object inheritance from
1249c5aaa06dSAlan Cox  * being a negative impact on memory usage.
1250c5aaa06dSAlan Cox  */
1251c5aaa06dSAlan Cox void
1252c5aaa06dSAlan Cox vm_object_split(vm_map_entry_t entry)
1253c5aaa06dSAlan Cox {
1254c5aaa06dSAlan Cox 	vm_page_t m;
1255c5aaa06dSAlan Cox 	vm_object_t orig_object, new_object, source;
125623f09d50SIan Dowse 	vm_pindex_t offidxstart, offidxend;
125723f09d50SIan Dowse 	vm_size_t idx, size;
1258c5aaa06dSAlan Cox 
1259c5aaa06dSAlan Cox 	orig_object = entry->object.vm_object;
1260c5aaa06dSAlan Cox 	if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1261c5aaa06dSAlan Cox 		return;
1262c5aaa06dSAlan Cox 	if (orig_object->ref_count <= 1)
1263c5aaa06dSAlan Cox 		return;
12644da9f125SAlan Cox 	VM_OBJECT_UNLOCK(orig_object);
1265c5aaa06dSAlan Cox 
12664da9f125SAlan Cox 	offidxstart = OFF_TO_IDX(entry->offset);
12674da9f125SAlan Cox 	offidxend = offidxstart + OFF_TO_IDX(entry->end - entry->start);
1268c5aaa06dSAlan Cox 	size = offidxend - offidxstart;
1269c5aaa06dSAlan Cox 
12704da9f125SAlan Cox 	/*
12714da9f125SAlan Cox 	 * If swap_pager_copy() is later called, it will convert new_object
12724da9f125SAlan Cox 	 * into a swap object.
12734da9f125SAlan Cox 	 */
12744da9f125SAlan Cox 	new_object = vm_object_allocate(OBJT_DEFAULT, size);
1275c5aaa06dSAlan Cox 
127663f6cefcSAlan Cox 	VM_OBJECT_LOCK(new_object);
127763f6cefcSAlan Cox 	VM_OBJECT_LOCK(orig_object);
1278c5aaa06dSAlan Cox 	source = orig_object->backing_object;
1279c5aaa06dSAlan Cox 	if (source != NULL) {
12808e3a76fbSAlan Cox 		VM_OBJECT_LOCK(source);
12811c500307SAlan Cox 		LIST_INSERT_HEAD(&source->shadow_head,
1282c5aaa06dSAlan Cox 				  new_object, shadow_list);
12838e3a76fbSAlan Cox 		source->shadow_count++;
12848e3a76fbSAlan Cox 		source->generation++;
1285b921a12bSAlan Cox 		vm_object_reference_locked(source);	/* for new_object */
1286c5aaa06dSAlan Cox 		vm_object_clear_flag(source, OBJ_ONEMAPPING);
1287e2479b4fSAlan Cox 		VM_OBJECT_UNLOCK(source);
1288c5aaa06dSAlan Cox 		new_object->backing_object_offset =
12894da9f125SAlan Cox 			orig_object->backing_object_offset + entry->offset;
1290c5aaa06dSAlan Cox 		new_object->backing_object = source;
1291c5aaa06dSAlan Cox 	}
1292c5aaa06dSAlan Cox 	for (idx = 0; idx < size; idx++) {
1293c5aaa06dSAlan Cox 	retry:
1294c5aaa06dSAlan Cox 		m = vm_page_lookup(orig_object, offidxstart + idx);
1295c5aaa06dSAlan Cox 		if (m == NULL)
1296c5aaa06dSAlan Cox 			continue;
1297c5aaa06dSAlan Cox 
1298c5aaa06dSAlan Cox 		/*
1299c5aaa06dSAlan Cox 		 * We must wait for pending I/O to complete before we can
1300c5aaa06dSAlan Cox 		 * rename the page.
1301c5aaa06dSAlan Cox 		 *
1302c5aaa06dSAlan Cox 		 * We do not have to VM_PROT_NONE the page as mappings should
1303c5aaa06dSAlan Cox 		 * not be changed by this operation.
1304c5aaa06dSAlan Cox 		 */
130500f9e8b4SAlan Cox 		vm_page_lock_queues();
1306de33beddSAlan Cox 		if ((m->flags & PG_BUSY) || m->busy) {
1307de33beddSAlan Cox 			vm_page_flag_set(m, PG_WANTED | PG_REFERENCED);
1308de33beddSAlan Cox 			VM_OBJECT_UNLOCK(orig_object);
1309de33beddSAlan Cox 			VM_OBJECT_UNLOCK(new_object);
1310de33beddSAlan Cox 			msleep(m, &vm_page_queue_mtx, PDROP | PVM, "spltwt", 0);
1311de33beddSAlan Cox 			VM_OBJECT_LOCK(new_object);
1312de33beddSAlan Cox 			VM_OBJECT_LOCK(orig_object);
1313c5aaa06dSAlan Cox 			goto retry;
1314de33beddSAlan Cox 		}
1315c5aaa06dSAlan Cox 		vm_page_busy(m);
1316c5aaa06dSAlan Cox 		vm_page_rename(m, new_object, idx);
1317c5aaa06dSAlan Cox 		/* page automatically made dirty by rename and cache handled */
1318c5aaa06dSAlan Cox 		vm_page_busy(m);
1319bd82dc74SAlan Cox 		vm_page_unlock_queues();
1320c5aaa06dSAlan Cox 	}
1321d7a013c3SAlan Cox 	if (orig_object->type == OBJT_SWAP) {
1322c5aaa06dSAlan Cox 		/*
1323c7c8dd7eSAlan Cox 		 * swap_pager_copy() can sleep, in which case the orig_object's
1324c7c8dd7eSAlan Cox 		 * and new_object's locks are released and reacquired.
1325c5aaa06dSAlan Cox 		 */
1326c5aaa06dSAlan Cox 		swap_pager_copy(orig_object, new_object, offidxstart, 0);
1327c5aaa06dSAlan Cox 	}
1328d7a013c3SAlan Cox 	VM_OBJECT_UNLOCK(orig_object);
1329dc907f66SAlan Cox 	vm_page_lock_queues();
1330c7118ed6SAlan Cox 	TAILQ_FOREACH(m, &new_object->memq, listq)
1331c5aaa06dSAlan Cox 		vm_page_wakeup(m);
1332dc907f66SAlan Cox 	vm_page_unlock_queues();
1333c7c8dd7eSAlan Cox 	VM_OBJECT_UNLOCK(new_object);
1334c5aaa06dSAlan Cox 	entry->object.vm_object = new_object;
1335c5aaa06dSAlan Cox 	entry->offset = 0LL;
1336c5aaa06dSAlan Cox 	vm_object_deallocate(orig_object);
13374da9f125SAlan Cox 	VM_OBJECT_LOCK(new_object);
1338c5aaa06dSAlan Cox }
1339c5aaa06dSAlan Cox 
13402ad1a3f7SMatthew Dillon #define	OBSC_TEST_ALL_SHADOWED	0x0001
13412ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_NOWAIT	0x0002
13422ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_WAIT	0x0004
13432ad1a3f7SMatthew Dillon 
1344b4ae4780SPoul-Henning Kamp static int
13452ad1a3f7SMatthew Dillon vm_object_backing_scan(vm_object_t object, int op)
13462ad1a3f7SMatthew Dillon {
13472ad1a3f7SMatthew Dillon 	int s;
13482ad1a3f7SMatthew Dillon 	int r = 1;
13492ad1a3f7SMatthew Dillon 	vm_page_t p;
13502ad1a3f7SMatthew Dillon 	vm_object_t backing_object;
13512ad1a3f7SMatthew Dillon 	vm_pindex_t backing_offset_index;
13522ad1a3f7SMatthew Dillon 
13532ad1a3f7SMatthew Dillon 	s = splvm();
13547ca33ad1SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
13557ca33ad1SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object->backing_object, MA_OWNED);
13562ad1a3f7SMatthew Dillon 
13572ad1a3f7SMatthew Dillon 	backing_object = object->backing_object;
13582ad1a3f7SMatthew Dillon 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
13592ad1a3f7SMatthew Dillon 
13602ad1a3f7SMatthew Dillon 	/*
13612ad1a3f7SMatthew Dillon 	 * Initial conditions
13622ad1a3f7SMatthew Dillon 	 */
13632ad1a3f7SMatthew Dillon 	if (op & OBSC_TEST_ALL_SHADOWED) {
13642ad1a3f7SMatthew Dillon 		/*
1365956f3135SPhilippe Charnier 		 * We do not want to have to test for the existence of
13662ad1a3f7SMatthew Dillon 		 * swap pages in the backing object.  XXX but with the
13672ad1a3f7SMatthew Dillon 		 * new swapper this would be pretty easy to do.
13682ad1a3f7SMatthew Dillon 		 *
13692ad1a3f7SMatthew Dillon 		 * XXX what about anonymous MAP_SHARED memory that hasn't
13702ad1a3f7SMatthew Dillon 		 * been ZFOD faulted yet?  If we do not test for this, the
13712ad1a3f7SMatthew Dillon 		 * shadow test may succeed! XXX
13722ad1a3f7SMatthew Dillon 		 */
13732ad1a3f7SMatthew Dillon 		if (backing_object->type != OBJT_DEFAULT) {
13742ad1a3f7SMatthew Dillon 			splx(s);
13752ad1a3f7SMatthew Dillon 			return (0);
13762ad1a3f7SMatthew Dillon 		}
13772ad1a3f7SMatthew Dillon 	}
13782ad1a3f7SMatthew Dillon 	if (op & OBSC_COLLAPSE_WAIT) {
13792ad1a3f7SMatthew Dillon 		vm_object_set_flag(backing_object, OBJ_DEAD);
13802ad1a3f7SMatthew Dillon 	}
13812ad1a3f7SMatthew Dillon 
13822ad1a3f7SMatthew Dillon 	/*
13832ad1a3f7SMatthew Dillon 	 * Our scan
13842ad1a3f7SMatthew Dillon 	 */
13852ad1a3f7SMatthew Dillon 	p = TAILQ_FIRST(&backing_object->memq);
13862ad1a3f7SMatthew Dillon 	while (p) {
13872ad1a3f7SMatthew Dillon 		vm_page_t next = TAILQ_NEXT(p, listq);
13882ad1a3f7SMatthew Dillon 		vm_pindex_t new_pindex = p->pindex - backing_offset_index;
13892ad1a3f7SMatthew Dillon 
13902ad1a3f7SMatthew Dillon 		if (op & OBSC_TEST_ALL_SHADOWED) {
13912ad1a3f7SMatthew Dillon 			vm_page_t pp;
13922ad1a3f7SMatthew Dillon 
13932ad1a3f7SMatthew Dillon 			/*
13942ad1a3f7SMatthew Dillon 			 * Ignore pages outside the parent object's range
13952ad1a3f7SMatthew Dillon 			 * and outside the parent object's mapping of the
13962ad1a3f7SMatthew Dillon 			 * backing object.
13972ad1a3f7SMatthew Dillon 			 *
13982ad1a3f7SMatthew Dillon 			 * note that we do not busy the backing object's
13992ad1a3f7SMatthew Dillon 			 * page.
14002ad1a3f7SMatthew Dillon 			 */
14012ad1a3f7SMatthew Dillon 			if (
14022ad1a3f7SMatthew Dillon 			    p->pindex < backing_offset_index ||
14032ad1a3f7SMatthew Dillon 			    new_pindex >= object->size
14042ad1a3f7SMatthew Dillon 			) {
14052ad1a3f7SMatthew Dillon 				p = next;
14062ad1a3f7SMatthew Dillon 				continue;
14072ad1a3f7SMatthew Dillon 			}
14082ad1a3f7SMatthew Dillon 
14092ad1a3f7SMatthew Dillon 			/*
14102ad1a3f7SMatthew Dillon 			 * See if the parent has the page or if the parent's
14112ad1a3f7SMatthew Dillon 			 * object pager has the page.  If the parent has the
14122ad1a3f7SMatthew Dillon 			 * page but the page is not valid, the parent's
14132ad1a3f7SMatthew Dillon 			 * object pager must have the page.
14142ad1a3f7SMatthew Dillon 			 *
14152ad1a3f7SMatthew Dillon 			 * If this fails, the parent does not completely shadow
14162ad1a3f7SMatthew Dillon 			 * the object and we might as well give up now.
14172ad1a3f7SMatthew Dillon 			 */
14182ad1a3f7SMatthew Dillon 
14192ad1a3f7SMatthew Dillon 			pp = vm_page_lookup(object, new_pindex);
14202ad1a3f7SMatthew Dillon 			if (
14212ad1a3f7SMatthew Dillon 			    (pp == NULL || pp->valid == 0) &&
14222ad1a3f7SMatthew Dillon 			    !vm_pager_has_page(object, new_pindex, NULL, NULL)
14232ad1a3f7SMatthew Dillon 			) {
14242ad1a3f7SMatthew Dillon 				r = 0;
14252ad1a3f7SMatthew Dillon 				break;
14262ad1a3f7SMatthew Dillon 			}
14272ad1a3f7SMatthew Dillon 		}
14282ad1a3f7SMatthew Dillon 
14292ad1a3f7SMatthew Dillon 		/*
14302ad1a3f7SMatthew Dillon 		 * Check for busy page
14312ad1a3f7SMatthew Dillon 		 */
14322ad1a3f7SMatthew Dillon 		if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
14332ad1a3f7SMatthew Dillon 			vm_page_t pp;
14342ad1a3f7SMatthew Dillon 
143500f9e8b4SAlan Cox 			vm_page_lock_queues();
14362ad1a3f7SMatthew Dillon 			if (op & OBSC_COLLAPSE_NOWAIT) {
143700f9e8b4SAlan Cox 				if ((p->flags & PG_BUSY) ||
14382ad1a3f7SMatthew Dillon 				    !p->valid ||
14392ad1a3f7SMatthew Dillon 				    p->hold_count ||
14402ad1a3f7SMatthew Dillon 				    p->wire_count ||
144100f9e8b4SAlan Cox 				    p->busy) {
144200f9e8b4SAlan Cox 					vm_page_unlock_queues();
14432ad1a3f7SMatthew Dillon 					p = next;
14442ad1a3f7SMatthew Dillon 					continue;
14452ad1a3f7SMatthew Dillon 				}
14462ad1a3f7SMatthew Dillon 			} else if (op & OBSC_COLLAPSE_WAIT) {
14477ca33ad1SAlan Cox 				if ((p->flags & PG_BUSY) || p->busy) {
14487ca33ad1SAlan Cox 					vm_page_flag_set(p,
14497ca33ad1SAlan Cox 					    PG_WANTED | PG_REFERENCED);
14507ca33ad1SAlan Cox 					VM_OBJECT_UNLOCK(backing_object);
14517ca33ad1SAlan Cox 					VM_OBJECT_UNLOCK(object);
14527ca33ad1SAlan Cox 					msleep(p, &vm_page_queue_mtx,
14537ca33ad1SAlan Cox 					    PDROP | PVM, "vmocol", 0);
14547ca33ad1SAlan Cox 					VM_OBJECT_LOCK(object);
14557ca33ad1SAlan Cox 					VM_OBJECT_LOCK(backing_object);
14562ad1a3f7SMatthew Dillon 					/*
14572ad1a3f7SMatthew Dillon 					 * If we slept, anything could have
14582ad1a3f7SMatthew Dillon 					 * happened.  Since the object is
14592ad1a3f7SMatthew Dillon 					 * marked dead, the backing offset
14602ad1a3f7SMatthew Dillon 					 * should not have changed so we
14612ad1a3f7SMatthew Dillon 					 * just restart our scan.
14622ad1a3f7SMatthew Dillon 					 */
14632ad1a3f7SMatthew Dillon 					p = TAILQ_FIRST(&backing_object->memq);
14642ad1a3f7SMatthew Dillon 					continue;
14652ad1a3f7SMatthew Dillon 				}
14662ad1a3f7SMatthew Dillon 			}
14672ad1a3f7SMatthew Dillon 
14682ad1a3f7SMatthew Dillon 			/*
14692ad1a3f7SMatthew Dillon 			 * Busy the page
14702ad1a3f7SMatthew Dillon 			 */
14712ad1a3f7SMatthew Dillon 			vm_page_busy(p);
147200f9e8b4SAlan Cox 			vm_page_unlock_queues();
14732ad1a3f7SMatthew Dillon 
14742ad1a3f7SMatthew Dillon 			KASSERT(
14752ad1a3f7SMatthew Dillon 			    p->object == backing_object,
14762ad1a3f7SMatthew Dillon 			    ("vm_object_qcollapse(): object mismatch")
14772ad1a3f7SMatthew Dillon 			);
14782ad1a3f7SMatthew Dillon 
14792ad1a3f7SMatthew Dillon 			/*
14802ad1a3f7SMatthew Dillon 			 * Destroy any associated swap
14812ad1a3f7SMatthew Dillon 			 */
14822ad1a3f7SMatthew Dillon 			if (backing_object->type == OBJT_SWAP) {
14832ad1a3f7SMatthew Dillon 				swap_pager_freespace(
14842ad1a3f7SMatthew Dillon 				    backing_object,
14852ad1a3f7SMatthew Dillon 				    p->pindex,
14862ad1a3f7SMatthew Dillon 				    1
14872ad1a3f7SMatthew Dillon 				);
14882ad1a3f7SMatthew Dillon 			}
14892ad1a3f7SMatthew Dillon 
14902ad1a3f7SMatthew Dillon 			if (
14912ad1a3f7SMatthew Dillon 			    p->pindex < backing_offset_index ||
14922ad1a3f7SMatthew Dillon 			    new_pindex >= object->size
14932ad1a3f7SMatthew Dillon 			) {
14942ad1a3f7SMatthew Dillon 				/*
14952ad1a3f7SMatthew Dillon 				 * Page is out of the parent object's range, we
14962ad1a3f7SMatthew Dillon 				 * can simply destroy it.
14972ad1a3f7SMatthew Dillon 				 */
14986a684ecfSAlan Cox 				vm_page_lock_queues();
14994fec79beSAlan Cox 				pmap_remove_all(p);
15002ad1a3f7SMatthew Dillon 				vm_page_free(p);
15016a684ecfSAlan Cox 				vm_page_unlock_queues();
15022ad1a3f7SMatthew Dillon 				p = next;
15032ad1a3f7SMatthew Dillon 				continue;
15042ad1a3f7SMatthew Dillon 			}
15052ad1a3f7SMatthew Dillon 
15062ad1a3f7SMatthew Dillon 			pp = vm_page_lookup(object, new_pindex);
15072ad1a3f7SMatthew Dillon 			if (
15082ad1a3f7SMatthew Dillon 			    pp != NULL ||
15092ad1a3f7SMatthew Dillon 			    vm_pager_has_page(object, new_pindex, NULL, NULL)
15102ad1a3f7SMatthew Dillon 			) {
15112ad1a3f7SMatthew Dillon 				/*
15122ad1a3f7SMatthew Dillon 				 * page already exists in parent OR swap exists
15132ad1a3f7SMatthew Dillon 				 * for this location in the parent.  Destroy
15142ad1a3f7SMatthew Dillon 				 * the original page from the backing object.
15152ad1a3f7SMatthew Dillon 				 *
15162ad1a3f7SMatthew Dillon 				 * Leave the parent's page alone
15172ad1a3f7SMatthew Dillon 				 */
15186a684ecfSAlan Cox 				vm_page_lock_queues();
15194fec79beSAlan Cox 				pmap_remove_all(p);
15202ad1a3f7SMatthew Dillon 				vm_page_free(p);
15216a684ecfSAlan Cox 				vm_page_unlock_queues();
15222ad1a3f7SMatthew Dillon 				p = next;
15232ad1a3f7SMatthew Dillon 				continue;
15242ad1a3f7SMatthew Dillon 			}
15252ad1a3f7SMatthew Dillon 
15262ad1a3f7SMatthew Dillon 			/*
15272ad1a3f7SMatthew Dillon 			 * Page does not exist in parent, rename the
15282ad1a3f7SMatthew Dillon 			 * page from the backing object to the main object.
1529d1bf5d56SMatthew Dillon 			 *
1530d1bf5d56SMatthew Dillon 			 * If the page was mapped to a process, it can remain
1531d1bf5d56SMatthew Dillon 			 * mapped through the rename.
15322ad1a3f7SMatthew Dillon 			 */
1533a28cc55eSAlan Cox 			vm_page_lock_queues();
15342ad1a3f7SMatthew Dillon 			vm_page_rename(p, object, new_pindex);
1535a28cc55eSAlan Cox 			vm_page_unlock_queues();
15362ad1a3f7SMatthew Dillon 			/* page automatically made dirty by rename */
15372ad1a3f7SMatthew Dillon 		}
15382ad1a3f7SMatthew Dillon 		p = next;
15392ad1a3f7SMatthew Dillon 	}
15402ad1a3f7SMatthew Dillon 	splx(s);
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 
1561010cf3b9SDavid Greenman 	backing_object->ref_count += 2;
1562010cf3b9SDavid Greenman 
15632ad1a3f7SMatthew Dillon 	vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
15641c7c3c6aSMatthew Dillon 
1565010cf3b9SDavid Greenman 	backing_object->ref_count -= 2;
15662fe6e4d7SDavid Greenman }
15672fe6e4d7SDavid Greenman 
1568df8bae1dSRodney W. Grimes /*
1569df8bae1dSRodney W. Grimes  *	vm_object_collapse:
1570df8bae1dSRodney W. Grimes  *
1571df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
1572df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
1573df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
1574df8bae1dSRodney W. Grimes  */
157526f9a767SRodney W. Grimes void
15761b40f8c0SMatthew Dillon vm_object_collapse(vm_object_t object)
1577df8bae1dSRodney W. Grimes {
1578d7fc2210SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
157923955314SAlfred Perlstein 
1580df8bae1dSRodney W. Grimes 	while (TRUE) {
15812ad1a3f7SMatthew Dillon 		vm_object_t backing_object;
15822ad1a3f7SMatthew Dillon 
1583df8bae1dSRodney W. Grimes 		/*
1584df8bae1dSRodney W. Grimes 		 * Verify that the conditions are right for collapse:
1585df8bae1dSRodney W. Grimes 		 *
15862ad1a3f7SMatthew Dillon 		 * The object exists and the backing object exists.
1587df8bae1dSRodney W. Grimes 		 */
158824a1cce3SDavid Greenman 		if ((backing_object = object->backing_object) == NULL)
15892ad1a3f7SMatthew Dillon 			break;
1590df8bae1dSRodney W. Grimes 
1591f919ebdeSDavid Greenman 		/*
1592f919ebdeSDavid Greenman 		 * we check the backing object first, because it is most likely
159324a1cce3SDavid Greenman 		 * not collapsable.
1594f919ebdeSDavid Greenman 		 */
159540b808a8SAlan Cox 		VM_OBJECT_LOCK(backing_object);
159624a1cce3SDavid Greenman 		if (backing_object->handle != NULL ||
159724a1cce3SDavid Greenman 		    (backing_object->type != OBJT_DEFAULT &&
159824a1cce3SDavid Greenman 		     backing_object->type != OBJT_SWAP) ||
1599f919ebdeSDavid Greenman 		    (backing_object->flags & OBJ_DEAD) ||
160024a1cce3SDavid Greenman 		    object->handle != NULL ||
160124a1cce3SDavid Greenman 		    (object->type != OBJT_DEFAULT &&
160224a1cce3SDavid Greenman 		     object->type != OBJT_SWAP) ||
160324a1cce3SDavid Greenman 		    (object->flags & OBJ_DEAD)) {
160440b808a8SAlan Cox 			VM_OBJECT_UNLOCK(backing_object);
16052ad1a3f7SMatthew Dillon 			break;
160624a1cce3SDavid Greenman 		}
16079b4814bbSDavid Greenman 
16082ad1a3f7SMatthew Dillon 		if (
16092ad1a3f7SMatthew Dillon 		    object->paging_in_progress != 0 ||
16102ad1a3f7SMatthew Dillon 		    backing_object->paging_in_progress != 0
16112ad1a3f7SMatthew Dillon 		) {
1612b9921222SDavid Greenman 			vm_object_qcollapse(object);
161340b808a8SAlan Cox 			VM_OBJECT_UNLOCK(backing_object);
16142ad1a3f7SMatthew Dillon 			break;
1615df8bae1dSRodney W. Grimes 		}
161626f9a767SRodney W. Grimes 		/*
16170d94caffSDavid Greenman 		 * We know that we can either collapse the backing object (if
16182ad1a3f7SMatthew Dillon 		 * the parent is the only reference to it) or (perhaps) have
16192ad1a3f7SMatthew Dillon 		 * the parent bypass the object if the parent happens to shadow
16202ad1a3f7SMatthew Dillon 		 * all the resident pages in the entire backing object.
16212ad1a3f7SMatthew Dillon 		 *
16222ad1a3f7SMatthew Dillon 		 * This is ignoring pager-backed pages such as swap pages.
16232ad1a3f7SMatthew Dillon 		 * vm_object_backing_scan fails the shadowing test in this
16242ad1a3f7SMatthew Dillon 		 * case.
1625df8bae1dSRodney W. Grimes 		 */
1626df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
1627df8bae1dSRodney W. Grimes 			/*
16282ad1a3f7SMatthew Dillon 			 * If there is exactly one reference to the backing
16292ad1a3f7SMatthew Dillon 			 * object, we can collapse it into the parent.
1630df8bae1dSRodney W. Grimes 			 */
16312ad1a3f7SMatthew Dillon 			vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1632df8bae1dSRodney W. Grimes 
1633df8bae1dSRodney W. Grimes 			/*
1634df8bae1dSRodney W. Grimes 			 * Move the pager from backing_object to object.
1635df8bae1dSRodney W. Grimes 			 */
16366be36525SAlan Cox 			if (backing_object->type == OBJT_SWAP) {
163724a1cce3SDavid Greenman 				/*
1638c7c8dd7eSAlan Cox 				 * swap_pager_copy() can sleep, in which case
1639c7c8dd7eSAlan Cox 				 * the backing_object's and object's locks are
1640c7c8dd7eSAlan Cox 				 * released and reacquired.
164124a1cce3SDavid Greenman 				 */
16421c7c3c6aSMatthew Dillon 				swap_pager_copy(
16431c7c3c6aSMatthew Dillon 				    backing_object,
16441c7c3c6aSMatthew Dillon 				    object,
16451c7c3c6aSMatthew Dillon 				    OFF_TO_IDX(object->backing_object_offset), TRUE);
1646c0503609SDavid Greenman 			}
1647df8bae1dSRodney W. Grimes 			/*
1648df8bae1dSRodney W. Grimes 			 * Object now shadows whatever backing_object did.
16492ad1a3f7SMatthew Dillon 			 * Note that the reference to
16502ad1a3f7SMatthew Dillon 			 * backing_object->backing_object moves from within
16512ad1a3f7SMatthew Dillon 			 * backing_object to within object.
1652df8bae1dSRodney W. Grimes 			 */
16531c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
16544f7c7f6eSAlan Cox 			backing_object->shadow_count--;
16554f7c7f6eSAlan Cox 			backing_object->generation++;
1656de5f6a77SJohn Dyson 			if (backing_object->backing_object) {
16576be36525SAlan Cox 				VM_OBJECT_LOCK(backing_object->backing_object);
16581c500307SAlan Cox 				LIST_REMOVE(backing_object, shadow_list);
165943186e53SAlan Cox 				LIST_INSERT_HEAD(
166043186e53SAlan Cox 				    &backing_object->backing_object->shadow_head,
166143186e53SAlan Cox 				    object, shadow_list);
166243186e53SAlan Cox 				/*
166343186e53SAlan Cox 				 * The shadow_count has not changed.
166443186e53SAlan Cox 				 */
1665eaf13dd7SJohn Dyson 				backing_object->backing_object->generation++;
16666be36525SAlan Cox 				VM_OBJECT_UNLOCK(backing_object->backing_object);
1667de5f6a77SJohn Dyson 			}
166824a1cce3SDavid Greenman 			object->backing_object = backing_object->backing_object;
16692ad1a3f7SMatthew Dillon 			object->backing_object_offset +=
16702ad1a3f7SMatthew Dillon 			    backing_object->backing_object_offset;
16712ad1a3f7SMatthew Dillon 
1672df8bae1dSRodney W. Grimes 			/*
1673df8bae1dSRodney W. Grimes 			 * Discard backing_object.
1674df8bae1dSRodney W. Grimes 			 *
16750d94caffSDavid Greenman 			 * Since the backing object has no pages, no pager left,
16760d94caffSDavid Greenman 			 * and no object references within it, all that is
16770d94caffSDavid Greenman 			 * necessary is to dispose of it.
1678df8bae1dSRodney W. Grimes 			 */
1679245df27cSMatthew Dillon 			KASSERT(backing_object->ref_count == 1, ("backing_object %p was somehow re-referenced during collapse!", backing_object));
16806be36525SAlan Cox 			VM_OBJECT_UNLOCK(backing_object);
1681df8bae1dSRodney W. Grimes 
1682a5698387SAlan Cox 			mtx_lock(&vm_object_list_mtx);
16832ad1a3f7SMatthew Dillon 			TAILQ_REMOVE(
16842ad1a3f7SMatthew Dillon 			    &vm_object_list,
16852ad1a3f7SMatthew Dillon 			    backing_object,
16862ad1a3f7SMatthew Dillon 			    object_list
16872ad1a3f7SMatthew Dillon 			);
1688a5698387SAlan Cox 			mtx_unlock(&vm_object_list_mtx);
1689df8bae1dSRodney W. Grimes 
1690670d17b5SJeff Roberson 			uma_zfree(obj_zone, backing_object);
1691df8bae1dSRodney W. Grimes 
1692df8bae1dSRodney W. Grimes 			object_collapses++;
16930d94caffSDavid Greenman 		} else {
169495e5e988SJohn Dyson 			vm_object_t new_backing_object;
1695df8bae1dSRodney W. Grimes 
1696df8bae1dSRodney W. Grimes 			/*
16972ad1a3f7SMatthew Dillon 			 * If we do not entirely shadow the backing object,
16982ad1a3f7SMatthew Dillon 			 * there is nothing we can do so we give up.
1699df8bae1dSRodney W. Grimes 			 */
17002ad1a3f7SMatthew Dillon 			if (vm_object_backing_scan(object, OBSC_TEST_ALL_SHADOWED) == 0) {
170140b808a8SAlan Cox 				VM_OBJECT_UNLOCK(backing_object);
17022ad1a3f7SMatthew Dillon 				break;
170324a1cce3SDavid Greenman 			}
1704df8bae1dSRodney W. Grimes 
1705df8bae1dSRodney W. Grimes 			/*
17060d94caffSDavid Greenman 			 * Make the parent shadow the next object in the
17070d94caffSDavid Greenman 			 * chain.  Deallocating backing_object will not remove
17080d94caffSDavid Greenman 			 * it, since its reference count is at least 2.
1709df8bae1dSRodney W. Grimes 			 */
17101c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
1711eaf13dd7SJohn Dyson 			backing_object->shadow_count--;
1712eaf13dd7SJohn Dyson 			backing_object->generation++;
171395e5e988SJohn Dyson 
171495e5e988SJohn Dyson 			new_backing_object = backing_object->backing_object;
17158aef1712SMatthew Dillon 			if ((object->backing_object = new_backing_object) != NULL) {
17166be36525SAlan Cox 				VM_OBJECT_LOCK(new_backing_object);
17171c500307SAlan Cox 				LIST_INSERT_HEAD(
17182ad1a3f7SMatthew Dillon 				    &new_backing_object->shadow_head,
17192ad1a3f7SMatthew Dillon 				    object,
17202ad1a3f7SMatthew Dillon 				    shadow_list
17212ad1a3f7SMatthew Dillon 				);
1722eaf13dd7SJohn Dyson 				new_backing_object->shadow_count++;
1723eaf13dd7SJohn Dyson 				new_backing_object->generation++;
1724b921a12bSAlan Cox 				vm_object_reference_locked(new_backing_object);
17256be36525SAlan Cox 				VM_OBJECT_UNLOCK(new_backing_object);
172695e5e988SJohn Dyson 				object->backing_object_offset +=
172795e5e988SJohn Dyson 					backing_object->backing_object_offset;
1728de5f6a77SJohn Dyson 			}
1729df8bae1dSRodney W. Grimes 
1730df8bae1dSRodney W. Grimes 			/*
17310d94caffSDavid Greenman 			 * Drop the reference count on backing_object. Since
173222ec553fSAlan Cox 			 * its ref_count was at least 2, it will not vanish.
1733df8bae1dSRodney W. Grimes 			 */
173422ec553fSAlan Cox 			backing_object->ref_count--;
173522ec553fSAlan Cox 			VM_OBJECT_UNLOCK(backing_object);
1736df8bae1dSRodney W. Grimes 			object_bypasses++;
1737df8bae1dSRodney W. Grimes 		}
1738df8bae1dSRodney W. Grimes 
1739df8bae1dSRodney W. Grimes 		/*
1740df8bae1dSRodney W. Grimes 		 * Try again with this object's new backing object.
1741df8bae1dSRodney W. Grimes 		 */
1742df8bae1dSRodney W. Grimes 	}
1743df8bae1dSRodney W. Grimes }
1744df8bae1dSRodney W. Grimes 
1745df8bae1dSRodney W. Grimes /*
1746bff99f0dSAlan Cox  *	vm_object_page_remove:
1747df8bae1dSRodney W. Grimes  *
1748bff99f0dSAlan Cox  *	Removes all physical pages in the given range from the
1749bff99f0dSAlan Cox  *	object's list of pages.  If the range's end is zero, all
1750bff99f0dSAlan Cox  *	physical pages from the range's start to the end of the object
1751bff99f0dSAlan Cox  *	are deleted.
1752df8bae1dSRodney W. Grimes  *
1753df8bae1dSRodney W. Grimes  *	The object must be locked.
1754df8bae1dSRodney W. Grimes  */
175526f9a767SRodney W. Grimes void
1756ecde4b32SAlan Cox vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
1757ecde4b32SAlan Cox     boolean_t clean_only)
1758df8bae1dSRodney W. Grimes {
1759d031cff1SMatthew Dillon 	vm_page_t p, next;
1760df8bae1dSRodney W. Grimes 
1761ecde4b32SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1762ecde4b32SAlan Cox 	if (object->resident_page_count == 0)
1763df8bae1dSRodney W. Grimes 		return;
176495e5e988SJohn Dyson 
17658b03c8edSMatthew Dillon 	/*
17668b03c8edSMatthew Dillon 	 * Since physically-backed objects do not use managed pages, we can't
17678b03c8edSMatthew Dillon 	 * remove pages from the object (we must instead remove the page
17688b03c8edSMatthew Dillon 	 * references, and then destroy the object).
17698b03c8edSMatthew Dillon 	 */
1770ecde4b32SAlan Cox 	KASSERT(object->type != OBJT_PHYS,
1771ecde4b32SAlan Cox 	    ("attempt to remove pages from a physical object"));
17728b03c8edSMatthew Dillon 
1773d474eaaaSDoug Rabson 	vm_object_pip_add(object, 1);
177426f9a767SRodney W. Grimes again:
177532585dd6SAlan Cox 	vm_page_lock_queues();
177675741c04SAlan Cox 	if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
177775741c04SAlan Cox 		if (p->pindex < start) {
177875741c04SAlan Cox 			p = vm_page_splay(start, object->root);
177975741c04SAlan Cox 			if ((object->root = p)->pindex < start)
178075741c04SAlan Cox 				p = TAILQ_NEXT(p, listq);
178175741c04SAlan Cox 		}
178275741c04SAlan Cox 	}
178375741c04SAlan Cox 	/*
178475741c04SAlan Cox 	 * Assert: the variable p is either (1) the page with the
178575741c04SAlan Cox 	 * least pindex greater than or equal to the parameter pindex
178675741c04SAlan Cox 	 * or (2) NULL.
178775741c04SAlan Cox 	 */
178875741c04SAlan Cox 	for (;
1789bff99f0dSAlan Cox 	     p != NULL && (p->pindex < end || end == 0);
179075741c04SAlan Cox 	     p = next) {
1791b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
179275741c04SAlan Cox 
1793bd7e5f99SJohn Dyson 		if (p->wire_count != 0) {
17944fec79beSAlan Cox 			pmap_remove_all(p);
1795ce65e68cSDavid Greenman 			if (!clean_only)
1796bd7e5f99SJohn Dyson 				p->valid = 0;
17970d94caffSDavid Greenman 			continue;
17980d94caffSDavid Greenman 		}
179932585dd6SAlan Cox 		if (vm_page_sleep_if_busy(p, TRUE, "vmopar"))
180026f9a767SRodney W. Grimes 			goto again;
18018f9110f6SJohn Dyson 		if (clean_only && p->valid) {
180285b8d6b4SAlan Cox 			pmap_page_protect(p, VM_PROT_READ | VM_PROT_EXECUTE);
18037c1f6cedSDavid Greenman 			if (p->valid & p->dirty)
18047c1f6cedSDavid Greenman 				continue;
18057c1f6cedSDavid Greenman 		}
1806e69763a3SDoug Rabson 		vm_page_busy(p);
18074fec79beSAlan Cox 		pmap_remove_all(p);
1808df8bae1dSRodney W. Grimes 		vm_page_free(p);
180926f9a767SRodney W. Grimes 	}
181032585dd6SAlan Cox 	vm_page_unlock_queues();
1811f919ebdeSDavid Greenman 	vm_object_pip_wakeup(object);
1812c0503609SDavid Greenman }
1813df8bae1dSRodney W. Grimes 
1814df8bae1dSRodney W. Grimes /*
1815df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
1816df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
1817df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
1818df8bae1dSRodney W. Grimes  *
1819df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
1820df8bae1dSRodney W. Grimes  *
1821df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
1822df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
1823df8bae1dSRodney W. Grimes  *
1824df8bae1dSRodney W. Grimes  *	Parameters:
1825df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
1826df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
1827df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
182857a21abaSAlan Cox  *		next_size	Size of reference to the second object
1829df8bae1dSRodney W. Grimes  *
1830df8bae1dSRodney W. Grimes  *	Conditions:
1831df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
1832df8bae1dSRodney W. Grimes  */
18330d94caffSDavid Greenman boolean_t
183457a21abaSAlan Cox vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset,
183500e1854aSAlan Cox 	vm_size_t prev_size, vm_size_t next_size)
1836df8bae1dSRodney W. Grimes {
1837ea41812fSAlan Cox 	vm_pindex_t next_pindex;
1838df8bae1dSRodney W. Grimes 
183900e1854aSAlan Cox 	if (prev_object == NULL)
1840df8bae1dSRodney W. Grimes 		return (TRUE);
1841bdbfbaafSAlan Cox 	VM_OBJECT_LOCK(prev_object);
18424112823fSMatthew Dillon 	if (prev_object->type != OBJT_DEFAULT &&
18434112823fSMatthew Dillon 	    prev_object->type != OBJT_SWAP) {
1844bdbfbaafSAlan Cox 		VM_OBJECT_UNLOCK(prev_object);
184530dcfc09SJohn Dyson 		return (FALSE);
184630dcfc09SJohn Dyson 	}
184730dcfc09SJohn Dyson 
1848df8bae1dSRodney W. Grimes 	/*
1849df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
1850df8bae1dSRodney W. Grimes 	 */
1851df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
1852df8bae1dSRodney W. Grimes 
1853df8bae1dSRodney W. Grimes 	/*
18540d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
18550d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
18560d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
1857df8bae1dSRodney W. Grimes 	 */
18588cc7e047SJohn Dyson 	if (prev_object->backing_object != NULL) {
1859bdbfbaafSAlan Cox 		VM_OBJECT_UNLOCK(prev_object);
1860df8bae1dSRodney W. Grimes 		return (FALSE);
1861df8bae1dSRodney W. Grimes 	}
1862a316d390SJohn Dyson 
1863a316d390SJohn Dyson 	prev_size >>= PAGE_SHIFT;
1864a316d390SJohn Dyson 	next_size >>= PAGE_SHIFT;
186557a21abaSAlan Cox 	next_pindex = OFF_TO_IDX(prev_offset) + prev_size;
18668cc7e047SJohn Dyson 
18678cc7e047SJohn Dyson 	if ((prev_object->ref_count > 1) &&
1868ea41812fSAlan Cox 	    (prev_object->size != next_pindex)) {
1869bdbfbaafSAlan Cox 		VM_OBJECT_UNLOCK(prev_object);
18708cc7e047SJohn Dyson 		return (FALSE);
18718cc7e047SJohn Dyson 	}
18728cc7e047SJohn Dyson 
1873df8bae1dSRodney W. Grimes 	/*
18740d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
18750d94caffSDavid Greenman 	 * deallocation.
1876df8bae1dSRodney W. Grimes 	 */
1877ea41812fSAlan Cox 	if (next_pindex < prev_object->size) {
1878df8bae1dSRodney W. Grimes 		vm_object_page_remove(prev_object,
1879ea41812fSAlan Cox 				      next_pindex,
1880ea41812fSAlan Cox 				      next_pindex + next_size, FALSE);
1881ea41812fSAlan Cox 		if (prev_object->type == OBJT_SWAP)
1882ea41812fSAlan Cox 			swap_pager_freespace(prev_object,
1883ea41812fSAlan Cox 					     next_pindex, next_size);
1884ea41812fSAlan Cox 	}
1885df8bae1dSRodney W. Grimes 
1886df8bae1dSRodney W. Grimes 	/*
1887df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
1888df8bae1dSRodney W. Grimes 	 */
1889ea41812fSAlan Cox 	if (next_pindex + next_size > prev_object->size)
1890ea41812fSAlan Cox 		prev_object->size = next_pindex + next_size;
1891df8bae1dSRodney W. Grimes 
1892bdbfbaafSAlan Cox 	VM_OBJECT_UNLOCK(prev_object);
1893df8bae1dSRodney W. Grimes 	return (TRUE);
1894df8bae1dSRodney W. Grimes }
1895df8bae1dSRodney W. Grimes 
18967a5a6352SMatthew Dillon void
18977a5a6352SMatthew Dillon vm_object_set_writeable_dirty(vm_object_t object)
18987a5a6352SMatthew Dillon {
18997a5a6352SMatthew Dillon 	struct vnode *vp;
19007a5a6352SMatthew Dillon 
1901de33beddSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
19027a5a6352SMatthew Dillon 	vm_object_set_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
19037a5a6352SMatthew Dillon 	if (object->type == OBJT_VNODE &&
19047a5a6352SMatthew Dillon 	    (vp = (struct vnode *)object->handle) != NULL) {
1905e6e370a7SJeff Roberson 		VI_LOCK(vp);
1906e6e370a7SJeff Roberson 		if ((vp->v_iflag & VI_OBJDIRTY) == 0)
1907e6e370a7SJeff Roberson 			vp->v_iflag |= VI_OBJDIRTY;
1908e6e370a7SJeff Roberson 		VI_UNLOCK(vp);
19097a5a6352SMatthew Dillon 	}
19107a5a6352SMatthew Dillon }
19117a5a6352SMatthew Dillon 
1912c7c34a24SBruce Evans #include "opt_ddb.h"
1913c3cb3e12SDavid Greenman #ifdef DDB
1914c7c34a24SBruce Evans #include <sys/kernel.h>
1915c7c34a24SBruce Evans 
1916ce9edcf5SPoul-Henning Kamp #include <sys/cons.h>
1917c7c34a24SBruce Evans 
1918c7c34a24SBruce Evans #include <ddb/ddb.h>
1919c7c34a24SBruce Evans 
1920cac597e4SBruce Evans static int
19211b40f8c0SMatthew Dillon _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
1922a1f6d91cSDavid Greenman {
1923a1f6d91cSDavid Greenman 	vm_map_t tmpm;
1924a1f6d91cSDavid Greenman 	vm_map_entry_t tmpe;
1925a1f6d91cSDavid Greenman 	vm_object_t obj;
1926a1f6d91cSDavid Greenman 	int entcount;
1927a1f6d91cSDavid Greenman 
1928a1f6d91cSDavid Greenman 	if (map == 0)
1929a1f6d91cSDavid Greenman 		return 0;
1930a1f6d91cSDavid Greenman 
1931a1f6d91cSDavid Greenman 	if (entry == 0) {
1932a1f6d91cSDavid Greenman 		tmpe = map->header.next;
1933a1f6d91cSDavid Greenman 		entcount = map->nentries;
1934a1f6d91cSDavid Greenman 		while (entcount-- && (tmpe != &map->header)) {
1935a1f6d91cSDavid Greenman 			if (_vm_object_in_map(map, object, tmpe)) {
1936a1f6d91cSDavid Greenman 				return 1;
1937a1f6d91cSDavid Greenman 			}
1938a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1939a1f6d91cSDavid Greenman 		}
19409fdfe602SMatthew Dillon 	} else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
19419fdfe602SMatthew Dillon 		tmpm = entry->object.sub_map;
1942a1f6d91cSDavid Greenman 		tmpe = tmpm->header.next;
1943a1f6d91cSDavid Greenman 		entcount = tmpm->nentries;
1944a1f6d91cSDavid Greenman 		while (entcount-- && tmpe != &tmpm->header) {
1945a1f6d91cSDavid Greenman 			if (_vm_object_in_map(tmpm, object, tmpe)) {
1946a1f6d91cSDavid Greenman 				return 1;
1947a1f6d91cSDavid Greenman 			}
1948a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1949a1f6d91cSDavid Greenman 		}
19508aef1712SMatthew Dillon 	} else if ((obj = entry->object.vm_object) != NULL) {
195124a1cce3SDavid Greenman 		for (; obj; obj = obj->backing_object)
1952a1f6d91cSDavid Greenman 			if (obj == object) {
1953a1f6d91cSDavid Greenman 				return 1;
1954a1f6d91cSDavid Greenman 			}
1955a1f6d91cSDavid Greenman 	}
1956a1f6d91cSDavid Greenman 	return 0;
1957a1f6d91cSDavid Greenman }
1958a1f6d91cSDavid Greenman 
1959cac597e4SBruce Evans static int
19601b40f8c0SMatthew Dillon vm_object_in_map(vm_object_t object)
1961a1f6d91cSDavid Greenman {
1962a1f6d91cSDavid Greenman 	struct proc *p;
19631005a129SJohn Baldwin 
196460517fd1SJohn Baldwin 	/* sx_slock(&allproc_lock); */
1965fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(p, &allproc, p_list) {
1966a1f6d91cSDavid Greenman 		if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1967a1f6d91cSDavid Greenman 			continue;
1968553629ebSJake Burkholder 		if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
196960517fd1SJohn Baldwin 			/* sx_sunlock(&allproc_lock); */
1970a1f6d91cSDavid Greenman 			return 1;
1971a1f6d91cSDavid Greenman 		}
1972553629ebSJake Burkholder 	}
197360517fd1SJohn Baldwin 	/* sx_sunlock(&allproc_lock); */
1974a1f6d91cSDavid Greenman 	if (_vm_object_in_map(kernel_map, object, 0))
1975a1f6d91cSDavid Greenman 		return 1;
1976a1f6d91cSDavid Greenman 	if (_vm_object_in_map(kmem_map, object, 0))
1977a1f6d91cSDavid Greenman 		return 1;
1978a1f6d91cSDavid Greenman 	if (_vm_object_in_map(pager_map, object, 0))
1979a1f6d91cSDavid Greenman 		return 1;
1980a1f6d91cSDavid Greenman 	if (_vm_object_in_map(buffer_map, object, 0))
1981a1f6d91cSDavid Greenman 		return 1;
1982a1f6d91cSDavid Greenman 	return 0;
1983a1f6d91cSDavid Greenman }
1984a1f6d91cSDavid Greenman 
1985c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check)
1986f708ef1bSPoul-Henning Kamp {
1987a1f6d91cSDavid Greenman 	vm_object_t object;
1988a1f6d91cSDavid Greenman 
1989a1f6d91cSDavid Greenman 	/*
1990a1f6d91cSDavid Greenman 	 * make sure that internal objs are in a map somewhere
1991a1f6d91cSDavid Greenman 	 * and none have zero ref counts.
1992a1f6d91cSDavid Greenman 	 */
1993cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
199424a1cce3SDavid Greenman 		if (object->handle == NULL &&
199524a1cce3SDavid Greenman 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
1996a1f6d91cSDavid Greenman 			if (object->ref_count == 0) {
19973efc015bSPeter Wemm 				db_printf("vmochk: internal obj has zero ref count: %ld\n",
19983efc015bSPeter Wemm 					(long)object->size);
1999a1f6d91cSDavid Greenman 			}
2000a1f6d91cSDavid Greenman 			if (!vm_object_in_map(object)) {
2001fc62ef1fSBruce Evans 				db_printf(
2002fc62ef1fSBruce Evans 			"vmochk: internal obj is not in a map: "
2003fc62ef1fSBruce Evans 			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2004fc62ef1fSBruce Evans 				    object->ref_count, (u_long)object->size,
2005fc62ef1fSBruce Evans 				    (u_long)object->size,
2006fc62ef1fSBruce Evans 				    (void *)object->backing_object);
2007a1f6d91cSDavid Greenman 			}
2008a1f6d91cSDavid Greenman 		}
2009a1f6d91cSDavid Greenman 	}
2010a1f6d91cSDavid Greenman }
2011a1f6d91cSDavid Greenman 
201226f9a767SRodney W. Grimes /*
2013df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
2014df8bae1dSRodney W. Grimes  */
2015c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static)
2016df8bae1dSRodney W. Grimes {
2017c7c34a24SBruce Evans 	/* XXX convert args. */
2018c7c34a24SBruce Evans 	vm_object_t object = (vm_object_t)addr;
2019c7c34a24SBruce Evans 	boolean_t full = have_addr;
2020c7c34a24SBruce Evans 
2021d031cff1SMatthew Dillon 	vm_page_t p;
2022df8bae1dSRodney W. Grimes 
2023c7c34a24SBruce Evans 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
2024c7c34a24SBruce Evans #define	count	was_count
2025c7c34a24SBruce Evans 
2026d031cff1SMatthew Dillon 	int count;
2027df8bae1dSRodney W. Grimes 
2028df8bae1dSRodney W. Grimes 	if (object == NULL)
2029df8bae1dSRodney W. Grimes 		return;
2030df8bae1dSRodney W. Grimes 
2031eb95adefSBruce Evans 	db_iprintf(
2032e47cd172SMaxime Henrion 	    "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x\n",
2033e47cd172SMaxime Henrion 	    object, (int)object->type, (uintmax_t)object->size,
2034eb95adefSBruce Evans 	    object->resident_page_count, object->ref_count, object->flags);
2035e47cd172SMaxime Henrion 	db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n",
20361c7c3c6aSMatthew Dillon 	    object->shadow_count,
2037eb95adefSBruce Evans 	    object->backing_object ? object->backing_object->ref_count : 0,
2038e47cd172SMaxime Henrion 	    object->backing_object, (uintmax_t)object->backing_object_offset);
2039df8bae1dSRodney W. Grimes 
2040df8bae1dSRodney W. Grimes 	if (!full)
2041df8bae1dSRodney W. Grimes 		return;
2042df8bae1dSRodney W. Grimes 
2043c7c34a24SBruce Evans 	db_indent += 2;
2044df8bae1dSRodney W. Grimes 	count = 0;
2045fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(p, &object->memq, listq) {
2046df8bae1dSRodney W. Grimes 		if (count == 0)
2047c7c34a24SBruce Evans 			db_iprintf("memory:=");
2048df8bae1dSRodney W. Grimes 		else if (count == 6) {
2049c7c34a24SBruce Evans 			db_printf("\n");
2050c7c34a24SBruce Evans 			db_iprintf(" ...");
2051df8bae1dSRodney W. Grimes 			count = 0;
2052df8bae1dSRodney W. Grimes 		} else
2053c7c34a24SBruce Evans 			db_printf(",");
2054df8bae1dSRodney W. Grimes 		count++;
2055df8bae1dSRodney W. Grimes 
2056e47cd172SMaxime Henrion 		db_printf("(off=0x%jx,page=0x%jx)",
2057e47cd172SMaxime Henrion 		    (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p));
2058df8bae1dSRodney W. Grimes 	}
2059df8bae1dSRodney W. Grimes 	if (count != 0)
2060c7c34a24SBruce Evans 		db_printf("\n");
2061c7c34a24SBruce Evans 	db_indent -= 2;
2062df8bae1dSRodney W. Grimes }
20635070c7f8SJohn Dyson 
2064c7c34a24SBruce Evans /* XXX. */
2065c7c34a24SBruce Evans #undef count
2066c7c34a24SBruce Evans 
2067c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */
20685070c7f8SJohn Dyson void
20691b40f8c0SMatthew Dillon vm_object_print(
20701b40f8c0SMatthew Dillon         /* db_expr_t */ long addr,
20711b40f8c0SMatthew Dillon 	boolean_t have_addr,
20721b40f8c0SMatthew Dillon 	/* db_expr_t */ long count,
20731b40f8c0SMatthew Dillon 	char *modif)
2074c7c34a24SBruce Evans {
2075c7c34a24SBruce Evans 	vm_object_print_static(addr, have_addr, count, modif);
2076c7c34a24SBruce Evans }
2077c7c34a24SBruce Evans 
2078c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
20795070c7f8SJohn Dyson {
20805070c7f8SJohn Dyson 	vm_object_t object;
20815070c7f8SJohn Dyson 	int nl = 0;
20825070c7f8SJohn Dyson 	int c;
2083cc64b484SAlfred Perlstein 
2084cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
20855070c7f8SJohn Dyson 		vm_pindex_t idx, fidx;
20865070c7f8SJohn Dyson 		vm_pindex_t osize;
2087227f9a1cSJake Burkholder 		vm_paddr_t pa = -1, padiff;
20885070c7f8SJohn Dyson 		int rcount;
20895070c7f8SJohn Dyson 		vm_page_t m;
20905070c7f8SJohn Dyson 
2091fc62ef1fSBruce Evans 		db_printf("new object: %p\n", (void *)object);
20925070c7f8SJohn Dyson 		if (nl > 18) {
20935070c7f8SJohn Dyson 			c = cngetc();
20945070c7f8SJohn Dyson 			if (c != ' ')
20955070c7f8SJohn Dyson 				return;
20965070c7f8SJohn Dyson 			nl = 0;
20975070c7f8SJohn Dyson 		}
20985070c7f8SJohn Dyson 		nl++;
20995070c7f8SJohn Dyson 		rcount = 0;
21005070c7f8SJohn Dyson 		fidx = 0;
21015070c7f8SJohn Dyson 		osize = object->size;
21025070c7f8SJohn Dyson 		if (osize > 128)
21035070c7f8SJohn Dyson 			osize = 128;
21045070c7f8SJohn Dyson 		for (idx = 0; idx < osize; idx++) {
21055070c7f8SJohn Dyson 			m = vm_page_lookup(object, idx);
21065070c7f8SJohn Dyson 			if (m == NULL) {
21075070c7f8SJohn Dyson 				if (rcount) {
21083efc015bSPeter Wemm 					db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
21093efc015bSPeter Wemm 						(long)fidx, rcount, (long)pa);
21105070c7f8SJohn Dyson 					if (nl > 18) {
21115070c7f8SJohn Dyson 						c = cngetc();
21125070c7f8SJohn Dyson 						if (c != ' ')
21135070c7f8SJohn Dyson 							return;
21145070c7f8SJohn Dyson 						nl = 0;
21155070c7f8SJohn Dyson 					}
21165070c7f8SJohn Dyson 					nl++;
21175070c7f8SJohn Dyson 					rcount = 0;
21185070c7f8SJohn Dyson 				}
21195070c7f8SJohn Dyson 				continue;
21205070c7f8SJohn Dyson 			}
21215070c7f8SJohn Dyson 
21225070c7f8SJohn Dyson 
21235070c7f8SJohn Dyson 			if (rcount &&
21245070c7f8SJohn Dyson 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
21255070c7f8SJohn Dyson 				++rcount;
21265070c7f8SJohn Dyson 				continue;
21275070c7f8SJohn Dyson 			}
21285070c7f8SJohn Dyson 			if (rcount) {
21295070c7f8SJohn Dyson 				padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
21305070c7f8SJohn Dyson 				padiff >>= PAGE_SHIFT;
21315070c7f8SJohn Dyson 				padiff &= PQ_L2_MASK;
21325070c7f8SJohn Dyson 				if (padiff == 0) {
21335070c7f8SJohn Dyson 					pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
21345070c7f8SJohn Dyson 					++rcount;
21355070c7f8SJohn Dyson 					continue;
21365070c7f8SJohn Dyson 				}
21373efc015bSPeter Wemm 				db_printf(" index(%ld)run(%d)pa(0x%lx)",
21383efc015bSPeter Wemm 					(long)fidx, rcount, (long)pa);
21393efc015bSPeter Wemm 				db_printf("pd(%ld)\n", (long)padiff);
21405070c7f8SJohn Dyson 				if (nl > 18) {
21415070c7f8SJohn Dyson 					c = cngetc();
21425070c7f8SJohn Dyson 					if (c != ' ')
21435070c7f8SJohn Dyson 						return;
21445070c7f8SJohn Dyson 					nl = 0;
21455070c7f8SJohn Dyson 				}
21465070c7f8SJohn Dyson 				nl++;
21475070c7f8SJohn Dyson 			}
21485070c7f8SJohn Dyson 			fidx = idx;
21495070c7f8SJohn Dyson 			pa = VM_PAGE_TO_PHYS(m);
21505070c7f8SJohn Dyson 			rcount = 1;
21515070c7f8SJohn Dyson 		}
21525070c7f8SJohn Dyson 		if (rcount) {
21533efc015bSPeter Wemm 			db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
21543efc015bSPeter Wemm 				(long)fidx, rcount, (long)pa);
21555070c7f8SJohn Dyson 			if (nl > 18) {
21565070c7f8SJohn Dyson 				c = cngetc();
21575070c7f8SJohn Dyson 				if (c != ' ')
21585070c7f8SJohn Dyson 					return;
21595070c7f8SJohn Dyson 				nl = 0;
21605070c7f8SJohn Dyson 			}
21615070c7f8SJohn Dyson 			nl++;
21625070c7f8SJohn Dyson 		}
21635070c7f8SJohn Dyson 	}
21645070c7f8SJohn Dyson }
2165c3cb3e12SDavid Greenman #endif /* DDB */
2166