xref: /freebsd/sys/vm/vm_object.c (revision f279b88deb98bb709182f4c768e6802f0c99fa36)
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  * 3. All advertising materials mentioning features or use of this software
175929bcfaSPhilippe Charnier  *    must display the following acknowledgement:
18df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
19df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
20df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
21df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
22df8bae1dSRodney W. Grimes  *    without specific prior written permission.
23df8bae1dSRodney W. Grimes  *
24df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
35df8bae1dSRodney W. Grimes  *
363c4dd356SDavid Greenman  *	from: @(#)vm_object.c	8.5 (Berkeley) 3/22/94
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  *
39df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40df8bae1dSRodney W. Grimes  * All rights reserved.
41df8bae1dSRodney W. Grimes  *
42df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
43df8bae1dSRodney W. Grimes  *
44df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
45df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
46df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
47df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
48df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
49df8bae1dSRodney W. Grimes  *
50df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
53df8bae1dSRodney W. Grimes  *
54df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
55df8bae1dSRodney W. Grimes  *
56df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
57df8bae1dSRodney W. Grimes  *  School of Computer Science
58df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
59df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
60df8bae1dSRodney W. Grimes  *
61df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
62df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
633c4dd356SDavid Greenman  *
64c3aac50fSPeter Wemm  * $FreeBSD$
65df8bae1dSRodney W. Grimes  */
66df8bae1dSRodney W. Grimes 
67df8bae1dSRodney W. Grimes /*
68df8bae1dSRodney W. Grimes  *	Virtual memory object module.
69df8bae1dSRodney W. Grimes  */
70df8bae1dSRodney W. Grimes 
71df8bae1dSRodney W. Grimes #include <sys/param.h>
72df8bae1dSRodney W. Grimes #include <sys/systm.h>
73fb919e4dSMark Murray #include <sys/lock.h>
74867a482dSJohn Dyson #include <sys/mman.h>
75cf2819ccSJohn Dyson #include <sys/mount.h>
76b9b7a4beSMatthew Dillon #include <sys/kernel.h>
77b9b7a4beSMatthew Dillon #include <sys/sysctl.h>
781b367556SJason Evans #include <sys/mutex.h>
79fb919e4dSMark Murray #include <sys/proc.h>		/* for curproc, pageproc */
80fb919e4dSMark Murray #include <sys/socket.h>
81fb919e4dSMark Murray #include <sys/vnode.h>
82fb919e4dSMark Murray #include <sys/vmmeter.h>
831005a129SJohn Baldwin #include <sys/sx.h>
84df8bae1dSRodney W. Grimes 
85df8bae1dSRodney W. Grimes #include <vm/vm.h>
86efeaf95aSDavid Greenman #include <vm/vm_param.h>
87efeaf95aSDavid Greenman #include <vm/pmap.h>
88efeaf95aSDavid Greenman #include <vm/vm_map.h>
89efeaf95aSDavid Greenman #include <vm/vm_object.h>
90df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
9126f9a767SRodney W. Grimes #include <vm/vm_pageout.h>
920d94caffSDavid Greenman #include <vm/vm_pager.h>
9305f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h>
94a1f6d91cSDavid Greenman #include <vm/vm_kern.h>
95efeaf95aSDavid Greenman #include <vm/vm_extern.h>
96670d17b5SJeff Roberson #include <vm/uma.h>
9726f9a767SRodney W. Grimes 
98b9b7a4beSMatthew Dillon #define EASY_SCAN_FACTOR       8
99b9b7a4beSMatthew Dillon 
100b9b7a4beSMatthew Dillon #define MSYNC_FLUSH_HARDSEQ	0x01
101b9b7a4beSMatthew Dillon #define MSYNC_FLUSH_SOFTSEQ	0x02
102b9b7a4beSMatthew Dillon 
103b9b7a4beSMatthew Dillon /*
104b9b7a4beSMatthew Dillon  * msync / VM object flushing optimizations
105b9b7a4beSMatthew Dillon  */
106b9b7a4beSMatthew Dillon static int msync_flush_flags = MSYNC_FLUSH_HARDSEQ | MSYNC_FLUSH_SOFTSEQ;
107b9b7a4beSMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, msync_flush_flags,
108b9b7a4beSMatthew Dillon         CTLFLAG_RW, &msync_flush_flags, 0, "");
109b9b7a4beSMatthew Dillon 
110b9b7a4beSMatthew Dillon static void	vm_object_qcollapse(vm_object_t object);
111b9b7a4beSMatthew Dillon static int	vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags);
112f6b04d2bSDavid Greenman 
113df8bae1dSRodney W. Grimes /*
114df8bae1dSRodney W. Grimes  *	Virtual memory objects maintain the actual data
115df8bae1dSRodney W. Grimes  *	associated with allocated virtual memory.  A given
116df8bae1dSRodney W. Grimes  *	page of memory exists within exactly one object.
117df8bae1dSRodney W. Grimes  *
118df8bae1dSRodney W. Grimes  *	An object is only deallocated when all "references"
119df8bae1dSRodney W. Grimes  *	are given up.  Only one "reference" to a given
120df8bae1dSRodney W. Grimes  *	region of an object should be writeable.
121df8bae1dSRodney W. Grimes  *
122df8bae1dSRodney W. Grimes  *	Associated with each object is a list of all resident
123df8bae1dSRodney W. Grimes  *	memory pages belonging to that object; this list is
124df8bae1dSRodney W. Grimes  *	maintained by the "vm_page" module, and locked by the object's
125df8bae1dSRodney W. Grimes  *	lock.
126df8bae1dSRodney W. Grimes  *
127df8bae1dSRodney W. Grimes  *	Each object also records a "pager" routine which is
128df8bae1dSRodney W. Grimes  *	used to retrieve (and store) pages to the proper backing
129df8bae1dSRodney W. Grimes  *	storage.  In addition, objects may be backed by other
130df8bae1dSRodney W. Grimes  *	objects from which they were virtual-copied.
131df8bae1dSRodney W. Grimes  *
132df8bae1dSRodney W. Grimes  *	The only items within the object structure which are
133df8bae1dSRodney W. Grimes  *	modified after time of creation are:
134df8bae1dSRodney W. Grimes  *		reference count		locked by object's lock
135df8bae1dSRodney W. Grimes  *		pager routine		locked by object's lock
136df8bae1dSRodney W. Grimes  *
137df8bae1dSRodney W. Grimes  */
138df8bae1dSRodney W. Grimes 
13928f8db14SBruce Evans struct object_q vm_object_list;
140a5698387SAlan Cox struct mtx vm_object_list_mtx;	/* lock for object list and count */
14128f8db14SBruce Evans vm_object_t kernel_object;
14228f8db14SBruce Evans vm_object_t kmem_object;
143f708ef1bSPoul-Henning Kamp static struct vm_object kernel_object_store;
144f708ef1bSPoul-Henning Kamp static struct vm_object kmem_object_store;
145aef922f5SJohn Dyson extern int vm_pageout_page_count;
146df8bae1dSRodney W. Grimes 
147f708ef1bSPoul-Henning Kamp static long object_collapses;
148f708ef1bSPoul-Henning Kamp static long object_bypasses;
149dcc5840eSAlan Cox static int next_index;
150670d17b5SJeff Roberson static uma_zone_t obj_zone;
15199448ed1SJohn Dyson #define VM_OBJECTS_INIT 256
1528355f576SJeff Roberson 
1538355f576SJeff Roberson static void vm_object_zinit(void *mem, int size);
1548355f576SJeff Roberson 
1558355f576SJeff Roberson #ifdef INVARIANTS
1568355f576SJeff Roberson static void vm_object_zdtor(void *mem, int size, void *arg);
1578355f576SJeff Roberson 
1588355f576SJeff Roberson static void
1598355f576SJeff Roberson vm_object_zdtor(void *mem, int size, void *arg)
1608355f576SJeff Roberson {
1618355f576SJeff Roberson 	vm_object_t object;
1628355f576SJeff Roberson 
1638355f576SJeff Roberson 	object = (vm_object_t)mem;
1648355f576SJeff Roberson 	KASSERT(object->paging_in_progress == 0,
1658355f576SJeff Roberson 	    ("object %p paging_in_progress = %d",
1668355f576SJeff Roberson 	    object, object->paging_in_progress));
1678355f576SJeff Roberson 	KASSERT(object->resident_page_count == 0,
1688355f576SJeff Roberson 	    ("object %p resident_page_count = %d",
1698355f576SJeff Roberson 	    object, object->resident_page_count));
1708355f576SJeff Roberson 	KASSERT(object->shadow_count == 0,
1718355f576SJeff Roberson 	    ("object %p shadow_count = %d",
1728355f576SJeff Roberson 	    object, object->shadow_count));
1738355f576SJeff Roberson }
1748355f576SJeff Roberson #endif
1758355f576SJeff Roberson 
1768355f576SJeff Roberson static void
1778355f576SJeff Roberson vm_object_zinit(void *mem, int size)
1788355f576SJeff Roberson {
1798355f576SJeff Roberson 	vm_object_t object;
1808355f576SJeff Roberson 
1818355f576SJeff Roberson 	object = (vm_object_t)mem;
182e3a9e1b2SAlan Cox 	bzero(&object->mtx, sizeof(object->mtx));
1838355f576SJeff Roberson 
1848355f576SJeff Roberson 	/* These are true for any object that has been freed */
1858355f576SJeff Roberson 	object->paging_in_progress = 0;
1868355f576SJeff Roberson 	object->resident_page_count = 0;
1878355f576SJeff Roberson 	object->shadow_count = 0;
1888355f576SJeff Roberson }
189df8bae1dSRodney W. Grimes 
1903075778bSJohn Dyson void
1916395da54SIan Dowse _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
192df8bae1dSRodney W. Grimes {
193cf3e6e48SAlan Cox 	int incr;
1940cddd8f0SMatthew Dillon 
1954b420d50SAlan Cox 	mtx_init(&object->mtx, "vm object", NULL, MTX_DEF);
1964b420d50SAlan Cox 
197df8bae1dSRodney W. Grimes 	TAILQ_INIT(&object->memq);
19824a1cce3SDavid Greenman 	TAILQ_INIT(&object->shadow_head);
199a1f6d91cSDavid Greenman 
200b86ec922SMatthew Dillon 	object->root = NULL;
20124a1cce3SDavid Greenman 	object->type = type;
202df8bae1dSRodney W. Grimes 	object->size = size;
203a1f6d91cSDavid Greenman 	object->ref_count = 1;
20424a1cce3SDavid Greenman 	object->flags = 0;
20560517fd1SJohn Baldwin 	if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
20660517fd1SJohn Baldwin 		vm_object_set_flag(object, OBJ_ONEMAPPING);
20799448ed1SJohn Dyson 	if (size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
20899448ed1SJohn Dyson 		incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
20999448ed1SJohn Dyson 	else
21099448ed1SJohn Dyson 		incr = size;
211dcc5840eSAlan Cox 	do
212dcc5840eSAlan Cox 		object->pg_color = next_index;
213dcc5840eSAlan Cox 	while (!atomic_cmpset_int(&next_index, object->pg_color,
21479660d83SAlan Cox 				  (object->pg_color + incr) & PQ_L2_MASK));
21524a1cce3SDavid Greenman 	object->handle = NULL;
21624a1cce3SDavid Greenman 	object->backing_object = NULL;
217a316d390SJohn Dyson 	object->backing_object_offset = (vm_ooffset_t) 0;
218a1f6d91cSDavid Greenman 
21981f71edaSMatt Jacob 	atomic_add_int(&object->generation, 1);
22060517fd1SJohn Baldwin 
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 
23726f9a767SRodney W. Grimes 	kernel_object = &kernel_object_store;
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 
24126f9a767SRodney W. Grimes 	kmem_object = &kmem_object_store;
242a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
24326f9a767SRodney W. Grimes 	    kmem_object);
2448355f576SJeff Roberson 	obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL,
2458355f576SJeff Roberson #ifdef INVARIANTS
2468355f576SJeff Roberson 	    vm_object_zdtor,
2478355f576SJeff Roberson #else
2488355f576SJeff Roberson 	    NULL,
2498355f576SJeff Roberson #endif
2508355f576SJeff Roberson 	    vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
2518355f576SJeff Roberson 	uma_prealloc(obj_zone, VM_OBJECTS_INIT);
25299448ed1SJohn Dyson }
25399448ed1SJohn Dyson 
25499448ed1SJohn Dyson void
2551b40f8c0SMatthew Dillon vm_object_set_flag(vm_object_t object, u_short bits)
2561b40f8c0SMatthew Dillon {
257b06805adSJake Burkholder 	object->flags |= bits;
2581b40f8c0SMatthew Dillon }
2591b40f8c0SMatthew Dillon 
2601b40f8c0SMatthew Dillon void
2611b40f8c0SMatthew Dillon vm_object_clear_flag(vm_object_t object, u_short bits)
2621b40f8c0SMatthew Dillon {
2635440b5a9SAlan Cox 
2645440b5a9SAlan Cox 	mtx_assert(object == kmem_object ? &object->mtx : &Giant, MA_OWNED);
265b06805adSJake Burkholder 	object->flags &= ~bits;
2661b40f8c0SMatthew Dillon }
2671b40f8c0SMatthew Dillon 
2681b40f8c0SMatthew Dillon void
2691b40f8c0SMatthew Dillon vm_object_pip_add(vm_object_t object, short i)
2701b40f8c0SMatthew Dillon {
271f279b88dSAlan Cox 
272f279b88dSAlan Cox 	mtx_assert(object == kmem_object ? &object->mtx : &Giant, MA_OWNED);
273b06805adSJake Burkholder 	object->paging_in_progress += i;
2741b40f8c0SMatthew Dillon }
2751b40f8c0SMatthew Dillon 
2761b40f8c0SMatthew Dillon void
2771b40f8c0SMatthew Dillon vm_object_pip_subtract(vm_object_t object, short i)
2781b40f8c0SMatthew Dillon {
2791b40f8c0SMatthew Dillon 	GIANT_REQUIRED;
280b06805adSJake Burkholder 	object->paging_in_progress -= i;
2811b40f8c0SMatthew Dillon }
2821b40f8c0SMatthew Dillon 
2831b40f8c0SMatthew Dillon void
2841b40f8c0SMatthew Dillon vm_object_pip_wakeup(vm_object_t object)
2851b40f8c0SMatthew Dillon {
286f279b88dSAlan Cox 
287f279b88dSAlan Cox 	mtx_assert(object == kmem_object ? &object->mtx : &Giant, MA_OWNED);
288b06805adSJake Burkholder 	object->paging_in_progress--;
2891b40f8c0SMatthew Dillon 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
2901b40f8c0SMatthew Dillon 		vm_object_clear_flag(object, OBJ_PIPWNT);
2911b40f8c0SMatthew Dillon 		wakeup(object);
2921b40f8c0SMatthew Dillon 	}
2931b40f8c0SMatthew Dillon }
2941b40f8c0SMatthew Dillon 
2951b40f8c0SMatthew Dillon void
2961b40f8c0SMatthew Dillon vm_object_pip_wakeupn(vm_object_t object, short i)
2971b40f8c0SMatthew Dillon {
2981b40f8c0SMatthew Dillon 	GIANT_REQUIRED;
2991b40f8c0SMatthew Dillon 	if (i)
300b06805adSJake Burkholder 		object->paging_in_progress -= i;
3011b40f8c0SMatthew Dillon 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
3021b40f8c0SMatthew Dillon 		vm_object_clear_flag(object, OBJ_PIPWNT);
3031b40f8c0SMatthew Dillon 		wakeup(object);
3041b40f8c0SMatthew Dillon 	}
3051b40f8c0SMatthew Dillon }
3061b40f8c0SMatthew Dillon 
3071b40f8c0SMatthew Dillon void
3081b40f8c0SMatthew Dillon vm_object_pip_sleep(vm_object_t object, char *waitid)
3091b40f8c0SMatthew Dillon {
3101b40f8c0SMatthew Dillon 	GIANT_REQUIRED;
3111b40f8c0SMatthew Dillon 	if (object->paging_in_progress) {
3121b40f8c0SMatthew Dillon 		int s = splvm();
3131b40f8c0SMatthew Dillon 		if (object->paging_in_progress) {
3141b40f8c0SMatthew Dillon 			vm_object_set_flag(object, OBJ_PIPWNT);
3151b40f8c0SMatthew Dillon 			tsleep(object, PVM, waitid, 0);
3161b40f8c0SMatthew Dillon 		}
3171b40f8c0SMatthew Dillon 		splx(s);
3181b40f8c0SMatthew Dillon 	}
3191b40f8c0SMatthew Dillon }
3201b40f8c0SMatthew Dillon 
3211b40f8c0SMatthew Dillon void
3221b40f8c0SMatthew Dillon vm_object_pip_wait(vm_object_t object, char *waitid)
3231b40f8c0SMatthew Dillon {
3241b40f8c0SMatthew Dillon 	GIANT_REQUIRED;
3251b40f8c0SMatthew Dillon 	while (object->paging_in_progress)
3261b40f8c0SMatthew Dillon 		vm_object_pip_sleep(object, waitid);
3271b40f8c0SMatthew Dillon }
3281b40f8c0SMatthew Dillon 
32926f9a767SRodney W. Grimes /*
33098cb733cSKenneth D. Merry  *	vm_object_allocate_wait
33198cb733cSKenneth D. Merry  *
33298cb733cSKenneth D. Merry  *	Return a new object with the given size, and give the user the
33398cb733cSKenneth D. Merry  *	option of waiting for it to complete or failing if the needed
33498cb733cSKenneth D. Merry  *	memory isn't available.
33598cb733cSKenneth D. Merry  */
33698cb733cSKenneth D. Merry vm_object_t
33798cb733cSKenneth D. Merry vm_object_allocate_wait(objtype_t type, vm_pindex_t size, int flags)
33898cb733cSKenneth D. Merry {
33998cb733cSKenneth D. Merry 	vm_object_t result;
34098cb733cSKenneth D. Merry 
34198cb733cSKenneth D. Merry 	result = (vm_object_t) uma_zalloc(obj_zone, flags);
34298cb733cSKenneth D. Merry 
34398cb733cSKenneth D. Merry 	if (result != NULL)
34498cb733cSKenneth D. Merry 		_vm_object_allocate(type, size, result);
34598cb733cSKenneth D. Merry 
34698cb733cSKenneth D. Merry 	return (result);
34798cb733cSKenneth D. Merry }
34898cb733cSKenneth D. Merry 
34998cb733cSKenneth D. Merry /*
35026f9a767SRodney W. Grimes  *	vm_object_allocate:
35126f9a767SRodney W. Grimes  *
35226f9a767SRodney W. Grimes  *	Returns a new object with the given size.
35326f9a767SRodney W. Grimes  */
35426f9a767SRodney W. Grimes vm_object_t
3556395da54SIan Dowse vm_object_allocate(objtype_t type, vm_pindex_t size)
35626f9a767SRodney W. Grimes {
357a163d034SWarner Losh 	return(vm_object_allocate_wait(type, size, M_WAITOK));
35826f9a767SRodney W. Grimes }
35926f9a767SRodney W. Grimes 
36026f9a767SRodney W. Grimes 
36126f9a767SRodney W. Grimes /*
362df8bae1dSRodney W. Grimes  *	vm_object_reference:
363df8bae1dSRodney W. Grimes  *
364df8bae1dSRodney W. Grimes  *	Gets another reference to the given object.
365df8bae1dSRodney W. Grimes  */
3666476c0d2SJohn Dyson void
3671b40f8c0SMatthew Dillon vm_object_reference(vm_object_t object)
368df8bae1dSRodney W. Grimes {
369df8bae1dSRodney W. Grimes 	if (object == NULL)
370df8bae1dSRodney W. Grimes 		return;
37195e5e988SJohn Dyson 
37215c176c1SAlan Cox 	vm_object_lock(object);
373245df27cSMatthew Dillon #if 0
374245df27cSMatthew Dillon 	/* object can be re-referenced during final cleaning */
3755526d2d9SEivind Eklund 	KASSERT(!(object->flags & OBJ_DEAD),
3765526d2d9SEivind Eklund 	    ("vm_object_reference: attempting to reference dead obj"));
377245df27cSMatthew Dillon #endif
37895e5e988SJohn Dyson 
379df8bae1dSRodney W. Grimes 	object->ref_count++;
38047221757SJohn Dyson 	if (object->type == OBJT_VNODE) {
3813ef3e7c4SJeff Roberson 		while (vget((struct vnode *) object->handle, LK_RETRY, curthread)) {
38247221757SJohn Dyson 			printf("vm_object_reference: delay in getting object\n");
38347221757SJohn Dyson 		}
38447221757SJohn Dyson 	}
38515c176c1SAlan Cox 	vm_object_unlock(object);
38695e5e988SJohn Dyson }
38795e5e988SJohn Dyson 
38823955314SAlfred Perlstein /*
3899d5abbddSJens Schweikhardt  * Handle deallocating an object of type OBJT_VNODE.
39023955314SAlfred Perlstein  */
391bf27292bSJohn Dyson void
3921b40f8c0SMatthew Dillon vm_object_vndeallocate(vm_object_t object)
39395e5e988SJohn Dyson {
39495e5e988SJohn Dyson 	struct vnode *vp = (struct vnode *) object->handle;
395219cbf59SEivind Eklund 
3960cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
3975526d2d9SEivind Eklund 	KASSERT(object->type == OBJT_VNODE,
3985526d2d9SEivind Eklund 	    ("vm_object_vndeallocate: not a vnode object"));
399219cbf59SEivind Eklund 	KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
400219cbf59SEivind Eklund #ifdef INVARIANTS
40195e5e988SJohn Dyson 	if (object->ref_count == 0) {
40295e5e988SJohn Dyson 		vprint("vm_object_vndeallocate", vp);
40395e5e988SJohn Dyson 		panic("vm_object_vndeallocate: bad object reference count");
40495e5e988SJohn Dyson 	}
40595e5e988SJohn Dyson #endif
40695e5e988SJohn Dyson 
40795e5e988SJohn Dyson 	object->ref_count--;
40847221757SJohn Dyson 	if (object->ref_count == 0) {
409e6e370a7SJeff Roberson 		mp_fixme("Unlocked vflag access.");
410e6e370a7SJeff Roberson 		vp->v_vflag &= ~VV_TEXT;
4112be70f79SJohn Dyson 	}
41223955314SAlfred Perlstein 	/*
41323955314SAlfred Perlstein 	 * vrele may need a vop lock
41423955314SAlfred Perlstein 	 */
41547221757SJohn Dyson 	vrele(vp);
416df8bae1dSRodney W. Grimes }
417df8bae1dSRodney W. Grimes 
418df8bae1dSRodney W. Grimes /*
419df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
420df8bae1dSRodney W. Grimes  *
421df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
422df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
423df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
424df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
425df8bae1dSRodney W. Grimes  *	may be relinquished.
426df8bae1dSRodney W. Grimes  *
427df8bae1dSRodney W. Grimes  *	No object may be locked.
428df8bae1dSRodney W. Grimes  */
42926f9a767SRodney W. Grimes void
4301b40f8c0SMatthew Dillon vm_object_deallocate(vm_object_t object)
431df8bae1dSRodney W. Grimes {
432df8bae1dSRodney W. Grimes 	vm_object_t temp;
433df8bae1dSRodney W. Grimes 
4344dbeceeeSAlan Cox 	vm_object_lock(object);
435df8bae1dSRodney W. Grimes 	while (object != NULL) {
436df8bae1dSRodney W. Grimes 
43795e5e988SJohn Dyson 		if (object->type == OBJT_VNODE) {
43895e5e988SJohn Dyson 			vm_object_vndeallocate(object);
4394dbeceeeSAlan Cox 			vm_object_unlock(object);
44095e5e988SJohn Dyson 			return;
44195e5e988SJohn Dyson 		}
44295e5e988SJohn Dyson 
4438125b1e6SAlfred Perlstein 		KASSERT(object->ref_count != 0,
4448125b1e6SAlfred Perlstein 			("vm_object_deallocate: object deallocated too many times: %d", object->type));
4452be70f79SJohn Dyson 
4462be70f79SJohn Dyson 		/*
4478125b1e6SAlfred Perlstein 		 * If the reference count goes to 0 we start calling
4488125b1e6SAlfred Perlstein 		 * vm_object_terminate() on the object chain.
4498125b1e6SAlfred Perlstein 		 * A ref count of 1 may be a special case depending on the
4508125b1e6SAlfred Perlstein 		 * shadow count being 0 or 1.
4512be70f79SJohn Dyson 		 */
452c0877f10SJohn Dyson 		object->ref_count--;
4538125b1e6SAlfred Perlstein 		if (object->ref_count > 1) {
4544dbeceeeSAlan Cox 			vm_object_unlock(object);
455c0877f10SJohn Dyson 			return;
4568125b1e6SAlfred Perlstein 		} else if (object->ref_count == 1) {
4578125b1e6SAlfred Perlstein 			if (object->shadow_count == 0) {
4588125b1e6SAlfred Perlstein 				vm_object_set_flag(object, OBJ_ONEMAPPING);
4598125b1e6SAlfred Perlstein 			} else if ((object->shadow_count == 1) &&
4608125b1e6SAlfred Perlstein 			    (object->handle == NULL) &&
46124a1cce3SDavid Greenman 			    (object->type == OBJT_DEFAULT ||
46224a1cce3SDavid Greenman 			     object->type == OBJT_SWAP)) {
463a1f6d91cSDavid Greenman 				vm_object_t robject;
46495e5e988SJohn Dyson 
465b18bfc3dSJohn Dyson 				robject = TAILQ_FIRST(&object->shadow_head);
4665526d2d9SEivind Eklund 				KASSERT(robject != NULL,
467219cbf59SEivind Eklund 				    ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
4685526d2d9SEivind Eklund 					 object->ref_count,
4695526d2d9SEivind Eklund 					 object->shadow_count));
47095e5e988SJohn Dyson 				if ((robject->handle == NULL) &&
47124a1cce3SDavid Greenman 				    (robject->type == OBJT_DEFAULT ||
47224a1cce3SDavid Greenman 				     robject->type == OBJT_SWAP)) {
473a1f6d91cSDavid Greenman 
47495e5e988SJohn Dyson 					robject->ref_count++;
47595e5e988SJohn Dyson 
4761c7c3c6aSMatthew Dillon 					while (
4771c7c3c6aSMatthew Dillon 						robject->paging_in_progress ||
4781c7c3c6aSMatthew Dillon 						object->paging_in_progress
4791c7c3c6aSMatthew Dillon 					) {
48066095752SJohn Dyson 						vm_object_pip_sleep(robject, "objde1");
48166095752SJohn Dyson 						vm_object_pip_sleep(object, "objde2");
482a1f6d91cSDavid Greenman 					}
483a1f6d91cSDavid Greenman 
48495e5e988SJohn Dyson 					if (robject->ref_count == 1) {
48595e5e988SJohn Dyson 						robject->ref_count--;
486ba8da839SDavid Greenman 						object = robject;
48795e5e988SJohn Dyson 						goto doterm;
48895e5e988SJohn Dyson 					}
48995e5e988SJohn Dyson 
49095e5e988SJohn Dyson 					object = robject;
49195e5e988SJohn Dyson 					vm_object_collapse(object);
492ba8da839SDavid Greenman 					continue;
493a1f6d91cSDavid Greenman 				}
49495e5e988SJohn Dyson 			}
4954dbeceeeSAlan Cox 			vm_object_unlock(object);
496a1f6d91cSDavid Greenman 			return;
49795e5e988SJohn Dyson 		}
49895e5e988SJohn Dyson doterm:
49924a1cce3SDavid Greenman 		temp = object->backing_object;
500de5f6a77SJohn Dyson 		if (temp) {
50124a1cce3SDavid Greenman 			TAILQ_REMOVE(&temp->shadow_head, object, shadow_list);
50295e5e988SJohn Dyson 			temp->shadow_count--;
503eaf13dd7SJohn Dyson 			temp->generation++;
50495461b45SJohn Dyson 			object->backing_object = NULL;
505de5f6a77SJohn Dyson 		}
506245df27cSMatthew Dillon 		/*
507245df27cSMatthew Dillon 		 * Don't double-terminate, we could be in a termination
508245df27cSMatthew Dillon 		 * recursion due to the terminate having to sync data
509245df27cSMatthew Dillon 		 * to disk.
510245df27cSMatthew Dillon 		 */
511245df27cSMatthew Dillon 		if ((object->flags & OBJ_DEAD) == 0)
512df8bae1dSRodney W. Grimes 			vm_object_terminate(object);
513df8bae1dSRodney W. Grimes 		object = temp;
514df8bae1dSRodney W. Grimes 	}
5154dbeceeeSAlan Cox 	vm_object_unlock(object);
516df8bae1dSRodney W. Grimes }
517df8bae1dSRodney W. Grimes 
518df8bae1dSRodney W. Grimes /*
519df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
520df8bae1dSRodney W. Grimes  *	up all previously used resources.
521df8bae1dSRodney W. Grimes  *
522df8bae1dSRodney W. Grimes  *	The object must be locked.
5231c7c3c6aSMatthew Dillon  *	This routine may block.
524df8bae1dSRodney W. Grimes  */
52595e5e988SJohn Dyson void
5261b40f8c0SMatthew Dillon vm_object_terminate(vm_object_t object)
527df8bae1dSRodney W. Grimes {
528d031cff1SMatthew Dillon 	vm_page_t p;
529e4b7635dSDavid Greenman 	int s;
530df8bae1dSRodney W. Grimes 
5310cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
5320cddd8f0SMatthew Dillon 
53395e5e988SJohn Dyson 	/*
53495e5e988SJohn Dyson 	 * Make sure no one uses us.
53595e5e988SJohn Dyson 	 */
536069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_DEAD);
5373c631446SJohn Dyson 
538df8bae1dSRodney W. Grimes 	/*
539f6b04d2bSDavid Greenman 	 * wait for the pageout daemon to be done with the object
540df8bae1dSRodney W. Grimes 	 */
54166095752SJohn Dyson 	vm_object_pip_wait(object, "objtrm");
542df8bae1dSRodney W. Grimes 
5435526d2d9SEivind Eklund 	KASSERT(!object->paging_in_progress,
5445526d2d9SEivind Eklund 		("vm_object_terminate: pageout in progress"));
54526f9a767SRodney W. Grimes 
54626f9a767SRodney W. Grimes 	/*
5470d94caffSDavid Greenman 	 * Clean and free the pages, as appropriate. All references to the
5480d94caffSDavid Greenman 	 * object are gone, so we don't need to lock it.
54926f9a767SRodney W. Grimes 	 */
55024a1cce3SDavid Greenman 	if (object->type == OBJT_VNODE) {
55195e5e988SJohn Dyson 		struct vnode *vp;
55295e5e988SJohn Dyson 
55395e5e988SJohn Dyson 		/*
55495e5e988SJohn Dyson 		 * Clean pages and flush buffers.
55595e5e988SJohn Dyson 		 */
5568f9110f6SJohn Dyson 		vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
55795e5e988SJohn Dyson 
55895e5e988SJohn Dyson 		vp = (struct vnode *) object->handle;
559f6b04d2bSDavid Greenman 		vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
560bef608bdSJohn Dyson 	}
561bef608bdSJohn Dyson 
562971dd342SAlfred Perlstein 	KASSERT(object->ref_count == 0,
563971dd342SAlfred Perlstein 		("vm_object_terminate: object with references, ref_count=%d",
564971dd342SAlfred Perlstein 		object->ref_count));
565996c772fSJohn Dyson 
5660d94caffSDavid Greenman 	/*
567356863ebSDavid Greenman 	 * Now free any remaining pages. For internal objects, this also
568356863ebSDavid Greenman 	 * removes them from paging queues. Don't free wired pages, just
569356863ebSDavid Greenman 	 * remove them from the object.
570df8bae1dSRodney W. Grimes 	 */
571e4b7635dSDavid Greenman 	s = splvm();
57256030358SAlan Cox 	vm_page_lock_queues();
573b18bfc3dSJohn Dyson 	while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
574971dd342SAlfred Perlstein 		KASSERT(!p->busy && (p->flags & PG_BUSY) == 0,
575971dd342SAlfred Perlstein 			("vm_object_terminate: freeing busy page %p "
576971dd342SAlfred Perlstein 			"p->busy = %d, p->flags %x\n", p, p->busy, p->flags));
5770b10ba98SDavid Greenman 		if (p->wire_count == 0) {
578e69763a3SDoug Rabson 			vm_page_busy(p);
579df8bae1dSRodney W. Grimes 			vm_page_free(p);
580df8bae1dSRodney W. Grimes 			cnt.v_pfree++;
5810b10ba98SDavid Greenman 		} else {
582356863ebSDavid Greenman 			vm_page_busy(p);
5830b10ba98SDavid Greenman 			vm_page_remove(p);
5840b10ba98SDavid Greenman 		}
585df8bae1dSRodney W. Grimes 	}
58656030358SAlan Cox 	vm_page_unlock_queues();
587e4b7635dSDavid Greenman 	splx(s);
588bef608bdSJohn Dyson 
5892d8acc0fSJohn Dyson 	/*
5909fcfb650SDavid Greenman 	 * Let the pager know object is dead.
5919fcfb650SDavid Greenman 	 */
5929fcfb650SDavid Greenman 	vm_pager_deallocate(object);
5939fcfb650SDavid Greenman 
5949fcfb650SDavid Greenman 	/*
5950b10ba98SDavid Greenman 	 * Remove the object from the global object list.
5962d8acc0fSJohn Dyson 	 */
5979ed346baSBosko Milekic 	mtx_lock(&vm_object_list_mtx);
598df8bae1dSRodney W. Grimes 	TAILQ_REMOVE(&vm_object_list, object, object_list);
5999ed346baSBosko Milekic 	mtx_unlock(&vm_object_list_mtx);
6000b10ba98SDavid Greenman 
6014b420d50SAlan Cox 	mtx_destroy(&object->mtx);
6020b10ba98SDavid Greenman 	wakeup(object);
6030b10ba98SDavid Greenman 
604df8bae1dSRodney W. Grimes 	/*
605df8bae1dSRodney W. Grimes 	 * Free the space for the object.
606df8bae1dSRodney W. Grimes 	 */
607670d17b5SJeff Roberson 	uma_zfree(obj_zone, object);
60847221757SJohn Dyson }
609df8bae1dSRodney W. Grimes 
610df8bae1dSRodney W. Grimes /*
611df8bae1dSRodney W. Grimes  *	vm_object_page_clean
612df8bae1dSRodney W. Grimes  *
6134f79d873SMatthew Dillon  *	Clean all dirty pages in the specified range of object.  Leaves page
6144f79d873SMatthew Dillon  * 	on whatever queue it is currently on.   If NOSYNC is set then do not
6154f79d873SMatthew Dillon  *	write out pages with PG_NOSYNC set (originally comes from MAP_NOSYNC),
6164f79d873SMatthew Dillon  *	leaving the object dirty.
61726f9a767SRodney W. Grimes  *
61843b7990eSMatthew Dillon  *	When stuffing pages asynchronously, allow clustering.  XXX we need a
61943b7990eSMatthew Dillon  *	synchronous clustering mode implementation.
62043b7990eSMatthew Dillon  *
62126f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
62226f9a767SRodney W. Grimes  *
62326f9a767SRodney W. Grimes  *	The object must be locked.
62426f9a767SRodney W. Grimes  */
625f6b04d2bSDavid Greenman void
6261b40f8c0SMatthew Dillon vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end, int flags)
627f6b04d2bSDavid Greenman {
628b9b7a4beSMatthew Dillon 	vm_page_t p, np;
6296395da54SIan Dowse 	vm_pindex_t tstart, tend;
630bd7e5f99SJohn Dyson 	vm_pindex_t pi;
63124a1cce3SDavid Greenman 	struct vnode *vp;
6324f79d873SMatthew Dillon 	int clearobjflags;
6338f9110f6SJohn Dyson 	int pagerflags;
6342d8acc0fSJohn Dyson 	int curgeneration;
635f6b04d2bSDavid Greenman 
6360cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
6370cddd8f0SMatthew Dillon 
638aef922f5SJohn Dyson 	if (object->type != OBJT_VNODE ||
639aef922f5SJohn Dyson 		(object->flags & OBJ_MIGHTBEDIRTY) == 0)
640f6b04d2bSDavid Greenman 		return;
641f6b04d2bSDavid Greenman 
64243b7990eSMatthew Dillon 	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
6438f9110f6SJohn Dyson 	pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
6448f9110f6SJohn Dyson 
64524a1cce3SDavid Greenman 	vp = object->handle;
64624a1cce3SDavid Greenman 
647069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_CLEANING);
64824a1cce3SDavid Greenman 
649f6b04d2bSDavid Greenman 	tstart = start;
650f6b04d2bSDavid Greenman 	if (end == 0) {
651f6b04d2bSDavid Greenman 		tend = object->size;
652f6b04d2bSDavid Greenman 	} else {
653f6b04d2bSDavid Greenman 		tend = end;
654f6b04d2bSDavid Greenman 	}
655eaf13dd7SJohn Dyson 
65635c01631SAlan Cox 	vm_page_lock_queues();
6574f79d873SMatthew Dillon 	/*
658b9b7a4beSMatthew Dillon 	 * If the caller is smart and only msync()s a range he knows is
659b9b7a4beSMatthew Dillon 	 * dirty, we may be able to avoid an object scan.  This results in
660b9b7a4beSMatthew Dillon 	 * a phenominal improvement in performance.  We cannot do this
661b9b7a4beSMatthew Dillon 	 * as a matter of course because the object may be huge - e.g.
662b9b7a4beSMatthew Dillon 	 * the size might be in the gigabytes or terrabytes.
663b9b7a4beSMatthew Dillon 	 */
664b9b7a4beSMatthew Dillon 	if (msync_flush_flags & MSYNC_FLUSH_HARDSEQ) {
665300b96acSIan Dowse 		vm_pindex_t tscan;
666b9b7a4beSMatthew Dillon 		int scanlimit;
667b9b7a4beSMatthew Dillon 		int scanreset;
668b9b7a4beSMatthew Dillon 
669b9b7a4beSMatthew Dillon 		scanreset = object->resident_page_count / EASY_SCAN_FACTOR;
670b9b7a4beSMatthew Dillon 		if (scanreset < 16)
671b9b7a4beSMatthew Dillon 			scanreset = 16;
67243b7990eSMatthew Dillon 		pagerflags |= VM_PAGER_IGNORE_CLEANCHK;
673b9b7a4beSMatthew Dillon 
674b9b7a4beSMatthew Dillon 		scanlimit = scanreset;
675b9b7a4beSMatthew Dillon 		tscan = tstart;
676b9b7a4beSMatthew Dillon 		while (tscan < tend) {
677b9b7a4beSMatthew Dillon 			curgeneration = object->generation;
678b9b7a4beSMatthew Dillon 			p = vm_page_lookup(object, tscan);
679b9b7a4beSMatthew Dillon 			if (p == NULL || p->valid == 0 ||
680b9b7a4beSMatthew Dillon 			    (p->queue - p->pc) == PQ_CACHE) {
681b9b7a4beSMatthew Dillon 				if (--scanlimit == 0)
682b9b7a4beSMatthew Dillon 					break;
683b9b7a4beSMatthew Dillon 				++tscan;
684b9b7a4beSMatthew Dillon 				continue;
685b9b7a4beSMatthew Dillon 			}
686b9b7a4beSMatthew Dillon 			vm_page_test_dirty(p);
687b9b7a4beSMatthew Dillon 			if ((p->dirty & p->valid) == 0) {
688b9b7a4beSMatthew Dillon 				if (--scanlimit == 0)
689b9b7a4beSMatthew Dillon 					break;
690b9b7a4beSMatthew Dillon 				++tscan;
691b9b7a4beSMatthew Dillon 				continue;
692b9b7a4beSMatthew Dillon 			}
693b9b7a4beSMatthew Dillon 			/*
694b9b7a4beSMatthew Dillon 			 * If we have been asked to skip nosync pages and
695b9b7a4beSMatthew Dillon 			 * this is a nosync page, we can't continue.
696b9b7a4beSMatthew Dillon 			 */
697b9b7a4beSMatthew Dillon 			if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
698b9b7a4beSMatthew Dillon 				if (--scanlimit == 0)
699b9b7a4beSMatthew Dillon 					break;
700b9b7a4beSMatthew Dillon 				++tscan;
701b9b7a4beSMatthew Dillon 				continue;
702b9b7a4beSMatthew Dillon 			}
703b9b7a4beSMatthew Dillon 			scanlimit = scanreset;
704b9b7a4beSMatthew Dillon 
705b9b7a4beSMatthew Dillon 			/*
706b9b7a4beSMatthew Dillon 			 * This returns 0 if it was unable to busy the first
707b9b7a4beSMatthew Dillon 			 * page (i.e. had to sleep).
708b9b7a4beSMatthew Dillon 			 */
709b9b7a4beSMatthew Dillon 			tscan += vm_object_page_collect_flush(object, p, curgeneration, pagerflags);
710b9b7a4beSMatthew Dillon 		}
711b9b7a4beSMatthew Dillon 
712b9b7a4beSMatthew Dillon 		/*
713b9b7a4beSMatthew Dillon 		 * If everything was dirty and we flushed it successfully,
714b9b7a4beSMatthew Dillon 		 * and the requested range is not the entire object, we
715b9b7a4beSMatthew Dillon 		 * don't have to mess with CLEANCHK or MIGHTBEDIRTY and can
716b9b7a4beSMatthew Dillon 		 * return immediately.
717b9b7a4beSMatthew Dillon 		 */
718b9b7a4beSMatthew Dillon 		if (tscan >= tend && (tstart || tend < object->size)) {
71935c01631SAlan Cox 			vm_page_unlock_queues();
720b9b7a4beSMatthew Dillon 			vm_object_clear_flag(object, OBJ_CLEANING);
721b9b7a4beSMatthew Dillon 			return;
722b9b7a4beSMatthew Dillon 		}
72343b7990eSMatthew Dillon 		pagerflags &= ~VM_PAGER_IGNORE_CLEANCHK;
724b9b7a4beSMatthew Dillon 	}
725b9b7a4beSMatthew Dillon 
726b9b7a4beSMatthew Dillon 	/*
7274f79d873SMatthew Dillon 	 * Generally set CLEANCHK interlock and make the page read-only so
7284f79d873SMatthew Dillon 	 * we can then clear the object flags.
7294f79d873SMatthew Dillon 	 *
7304f79d873SMatthew Dillon 	 * However, if this is a nosync mmap then the object is likely to
7314f79d873SMatthew Dillon 	 * stay dirty so do not mess with the page and do not clear the
7324f79d873SMatthew Dillon 	 * object flags.
7334f79d873SMatthew Dillon 	 */
7344f79d873SMatthew Dillon 	clearobjflags = 1;
735fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(p, &object->memq, listq) {
736e69763a3SDoug Rabson 		vm_page_flag_set(p, PG_CLEANCHK);
7374f79d873SMatthew Dillon 		if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC))
7384f79d873SMatthew Dillon 			clearobjflags = 0;
7394f79d873SMatthew Dillon 		else
740a12cc0e4SAlan Cox 			pmap_page_protect(p, VM_PROT_READ);
741eaf13dd7SJohn Dyson 	}
742eaf13dd7SJohn Dyson 
7434f79d873SMatthew Dillon 	if (clearobjflags && (tstart == 0) && (tend == object->size)) {
744245df27cSMatthew Dillon 		struct vnode *vp;
745245df27cSMatthew Dillon 
746069e9bc1SDoug Rabson 		vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
747245df27cSMatthew Dillon 		if (object->type == OBJT_VNODE &&
748245df27cSMatthew Dillon 		    (vp = (struct vnode *)object->handle) != NULL) {
749e6e370a7SJeff Roberson 			VI_LOCK(vp);
750e6e370a7SJeff Roberson 			if (vp->v_iflag & VI_OBJDIRTY)
751e6e370a7SJeff Roberson 				vp->v_iflag &= ~VI_OBJDIRTY;
752e6e370a7SJeff Roberson 			VI_UNLOCK(vp);
753245df27cSMatthew Dillon 		}
754ec4f9fb0SDavid Greenman 	}
755f6b04d2bSDavid Greenman 
756bd7e5f99SJohn Dyson rescan:
7572d8acc0fSJohn Dyson 	curgeneration = object->generation;
7582d8acc0fSJohn Dyson 
759b18bfc3dSJohn Dyson 	for (p = TAILQ_FIRST(&object->memq); p; p = np) {
760b9b7a4beSMatthew Dillon 		int n;
761b9b7a4beSMatthew Dillon 
762b18bfc3dSJohn Dyson 		np = TAILQ_NEXT(p, listq);
763bd7e5f99SJohn Dyson 
764b9b7a4beSMatthew Dillon again:
765bd7e5f99SJohn Dyson 		pi = p->pindex;
766bd7e5f99SJohn Dyson 		if (((p->flags & PG_CLEANCHK) == 0) ||
767bd7e5f99SJohn Dyson 			(pi < tstart) || (pi >= tend) ||
7685070c7f8SJohn Dyson 			(p->valid == 0) ||
7695070c7f8SJohn Dyson 			((p->queue - p->pc) == PQ_CACHE)) {
770e69763a3SDoug Rabson 			vm_page_flag_clear(p, PG_CLEANCHK);
771aef922f5SJohn Dyson 			continue;
772f6b04d2bSDavid Greenman 		}
773f6b04d2bSDavid Greenman 
774bd7e5f99SJohn Dyson 		vm_page_test_dirty(p);
775bd7e5f99SJohn Dyson 		if ((p->dirty & p->valid) == 0) {
776e69763a3SDoug Rabson 			vm_page_flag_clear(p, PG_CLEANCHK);
777bd7e5f99SJohn Dyson 			continue;
778bd7e5f99SJohn Dyson 		}
779ec4f9fb0SDavid Greenman 
7804f79d873SMatthew Dillon 		/*
7814f79d873SMatthew Dillon 		 * If we have been asked to skip nosync pages and this is a
7824f79d873SMatthew Dillon 		 * nosync page, skip it.  Note that the object flags were
7834f79d873SMatthew Dillon 		 * not cleared in this case so we do not have to set them.
7844f79d873SMatthew Dillon 		 */
7854f79d873SMatthew Dillon 		if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
7864f79d873SMatthew Dillon 			vm_page_flag_clear(p, PG_CLEANCHK);
7874f79d873SMatthew Dillon 			continue;
7884f79d873SMatthew Dillon 		}
7894f79d873SMatthew Dillon 
790b9b7a4beSMatthew Dillon 		n = vm_object_page_collect_flush(object, p,
791b9b7a4beSMatthew Dillon 			curgeneration, pagerflags);
792b9b7a4beSMatthew Dillon 		if (n == 0)
793b9b7a4beSMatthew Dillon 			goto rescan;
794b9b7a4beSMatthew Dillon 
795b9b7a4beSMatthew Dillon 		if (object->generation != curgeneration)
796b9b7a4beSMatthew Dillon 			goto rescan;
797b9b7a4beSMatthew Dillon 
798b9b7a4beSMatthew Dillon 		/*
799b9b7a4beSMatthew Dillon 		 * Try to optimize the next page.  If we can't we pick up
800b9b7a4beSMatthew Dillon 		 * our (random) scan where we left off.
801b9b7a4beSMatthew Dillon 		 */
802b9b7a4beSMatthew Dillon 		if (msync_flush_flags & MSYNC_FLUSH_SOFTSEQ) {
803b9b7a4beSMatthew Dillon 			if ((p = vm_page_lookup(object, pi + n)) != NULL)
804b9b7a4beSMatthew Dillon 				goto again;
805b9b7a4beSMatthew Dillon 		}
806b9b7a4beSMatthew Dillon 	}
80735c01631SAlan Cox 	vm_page_unlock_queues();
808b9b7a4beSMatthew Dillon #if 0
809b9b7a4beSMatthew Dillon 	VOP_FSYNC(vp, NULL, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
810b9b7a4beSMatthew Dillon #endif
811b9b7a4beSMatthew Dillon 
812b9b7a4beSMatthew Dillon 	vm_object_clear_flag(object, OBJ_CLEANING);
813b9b7a4beSMatthew Dillon 	return;
814b9b7a4beSMatthew Dillon }
815b9b7a4beSMatthew Dillon 
816b9b7a4beSMatthew Dillon static int
817b9b7a4beSMatthew Dillon vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags)
818b9b7a4beSMatthew Dillon {
819b9b7a4beSMatthew Dillon 	int runlen;
820b9b7a4beSMatthew Dillon 	int s;
821b9b7a4beSMatthew Dillon 	int maxf;
822b9b7a4beSMatthew Dillon 	int chkb;
823b9b7a4beSMatthew Dillon 	int maxb;
824b9b7a4beSMatthew Dillon 	int i;
825b9b7a4beSMatthew Dillon 	vm_pindex_t pi;
826b9b7a4beSMatthew Dillon 	vm_page_t maf[vm_pageout_page_count];
827b9b7a4beSMatthew Dillon 	vm_page_t mab[vm_pageout_page_count];
828b9b7a4beSMatthew Dillon 	vm_page_t ma[vm_pageout_page_count];
829b9b7a4beSMatthew Dillon 
830b18bfc3dSJohn Dyson 	s = splvm();
83135c01631SAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
832b9b7a4beSMatthew Dillon 	pi = p->pindex;
833bd82dc74SAlan Cox 	while (vm_page_sleep_if_busy(p, TRUE, "vpcwai")) {
83435c01631SAlan Cox 		vm_page_lock_queues();
8352d8acc0fSJohn Dyson 		if (object->generation != curgeneration) {
836f6b04d2bSDavid Greenman 			splx(s);
837b9b7a4beSMatthew Dillon 			return(0);
838f6b04d2bSDavid Greenman 		}
839bd82dc74SAlan Cox 	}
840bd7e5f99SJohn Dyson 	maxf = 0;
841bd7e5f99SJohn Dyson 	for(i = 1; i < vm_pageout_page_count; i++) {
842b9b7a4beSMatthew Dillon 		vm_page_t tp;
843b9b7a4beSMatthew Dillon 
8448aef1712SMatthew Dillon 		if ((tp = vm_page_lookup(object, pi + i)) != NULL) {
845bd7e5f99SJohn Dyson 			if ((tp->flags & PG_BUSY) ||
84643b7990eSMatthew Dillon 				((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
84743b7990eSMatthew Dillon 				 (tp->flags & PG_CLEANCHK) == 0) ||
848ffc82b0aSJohn Dyson 				(tp->busy != 0))
849bd7e5f99SJohn Dyson 				break;
8505070c7f8SJohn Dyson 			if((tp->queue - tp->pc) == PQ_CACHE) {
851e69763a3SDoug Rabson 				vm_page_flag_clear(tp, PG_CLEANCHK);
8523077a9c2SJohn Dyson 				break;
8533077a9c2SJohn Dyson 			}
854bd7e5f99SJohn Dyson 			vm_page_test_dirty(tp);
855bd7e5f99SJohn Dyson 			if ((tp->dirty & tp->valid) == 0) {
856e69763a3SDoug Rabson 				vm_page_flag_clear(tp, PG_CLEANCHK);
857bd7e5f99SJohn Dyson 				break;
858bd7e5f99SJohn Dyson 			}
859bd7e5f99SJohn Dyson 			maf[ i - 1 ] = tp;
860bd7e5f99SJohn Dyson 			maxf++;
861bd7e5f99SJohn Dyson 			continue;
862bd7e5f99SJohn Dyson 		}
863bd7e5f99SJohn Dyson 		break;
864bd7e5f99SJohn Dyson 	}
865aef922f5SJohn Dyson 
866bd7e5f99SJohn Dyson 	maxb = 0;
867bd7e5f99SJohn Dyson 	chkb = vm_pageout_page_count -  maxf;
868bd7e5f99SJohn Dyson 	if (chkb) {
869bd7e5f99SJohn Dyson 		for(i = 1; i < chkb;i++) {
870b9b7a4beSMatthew Dillon 			vm_page_t tp;
871b9b7a4beSMatthew Dillon 
8728aef1712SMatthew Dillon 			if ((tp = vm_page_lookup(object, pi - i)) != NULL) {
873bd7e5f99SJohn Dyson 				if ((tp->flags & PG_BUSY) ||
87443b7990eSMatthew Dillon 					((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
87543b7990eSMatthew Dillon 					 (tp->flags & PG_CLEANCHK) == 0) ||
876ffc82b0aSJohn Dyson 					(tp->busy != 0))
877bd7e5f99SJohn Dyson 					break;
8785070c7f8SJohn Dyson 				if ((tp->queue - tp->pc) == PQ_CACHE) {
879e69763a3SDoug Rabson 					vm_page_flag_clear(tp, PG_CLEANCHK);
8803077a9c2SJohn Dyson 					break;
8813077a9c2SJohn Dyson 				}
882bd7e5f99SJohn Dyson 				vm_page_test_dirty(tp);
883bd7e5f99SJohn Dyson 				if ((tp->dirty & tp->valid) == 0) {
884e69763a3SDoug Rabson 					vm_page_flag_clear(tp, PG_CLEANCHK);
885bd7e5f99SJohn Dyson 					break;
886bd7e5f99SJohn Dyson 				}
887bd7e5f99SJohn Dyson 				mab[ i - 1 ] = tp;
888bd7e5f99SJohn Dyson 				maxb++;
889bd7e5f99SJohn Dyson 				continue;
890bd7e5f99SJohn Dyson 			}
891bd7e5f99SJohn Dyson 			break;
892bd7e5f99SJohn Dyson 		}
893bd7e5f99SJohn Dyson 	}
894bd7e5f99SJohn Dyson 
895bd7e5f99SJohn Dyson 	for(i = 0; i < maxb; i++) {
896bd7e5f99SJohn Dyson 		int index = (maxb - i) - 1;
897bd7e5f99SJohn Dyson 		ma[index] = mab[i];
898e69763a3SDoug Rabson 		vm_page_flag_clear(ma[index], PG_CLEANCHK);
899bd7e5f99SJohn Dyson 	}
900e69763a3SDoug Rabson 	vm_page_flag_clear(p, PG_CLEANCHK);
901bd7e5f99SJohn Dyson 	ma[maxb] = p;
902bd7e5f99SJohn Dyson 	for(i = 0; i < maxf; i++) {
903bd7e5f99SJohn Dyson 		int index = (maxb + i) + 1;
904bd7e5f99SJohn Dyson 		ma[index] = maf[i];
905e69763a3SDoug Rabson 		vm_page_flag_clear(ma[index], PG_CLEANCHK);
906f6b04d2bSDavid Greenman 	}
907bd7e5f99SJohn Dyson 	runlen = maxb + maxf + 1;
908cf2819ccSJohn Dyson 
909f35329acSJohn Dyson 	splx(s);
9108f9110f6SJohn Dyson 	vm_pageout_flush(ma, runlen, pagerflags);
911cf2819ccSJohn Dyson 	for (i = 0; i < runlen; i++) {
912cf2819ccSJohn Dyson 		if (ma[i]->valid & ma[i]->dirty) {
913a12cc0e4SAlan Cox 			pmap_page_protect(ma[i], VM_PROT_READ);
914e69763a3SDoug Rabson 			vm_page_flag_set(ma[i], PG_CLEANCHK);
915aef922f5SJohn Dyson 
916b9b7a4beSMatthew Dillon 			/*
917b9b7a4beSMatthew Dillon 			 * maxf will end up being the actual number of pages
918b9b7a4beSMatthew Dillon 			 * we wrote out contiguously, non-inclusive of the
919b9b7a4beSMatthew Dillon 			 * first page.  We do not count look-behind pages.
920b9b7a4beSMatthew Dillon 			 */
921b9b7a4beSMatthew Dillon 			if (i >= maxb + 1 && (maxf > i - maxb - 1))
922b9b7a4beSMatthew Dillon 				maxf = i - maxb - 1;
923b9b7a4beSMatthew Dillon 		}
924b9b7a4beSMatthew Dillon 	}
925b9b7a4beSMatthew Dillon 	return(maxf + 1);
92626f9a767SRodney W. Grimes }
927df8bae1dSRodney W. Grimes 
9281efb74fbSJohn Dyson /*
929867a482dSJohn Dyson  *	vm_object_madvise:
930867a482dSJohn Dyson  *
931867a482dSJohn Dyson  *	Implements the madvise function at the object/page level.
9321c7c3c6aSMatthew Dillon  *
933193b9358SAlan Cox  *	MADV_WILLNEED	(any object)
934193b9358SAlan Cox  *
935193b9358SAlan Cox  *	    Activate the specified pages if they are resident.
936193b9358SAlan Cox  *
937193b9358SAlan Cox  *	MADV_DONTNEED	(any object)
938193b9358SAlan Cox  *
939193b9358SAlan Cox  *	    Deactivate the specified pages if they are resident.
940193b9358SAlan Cox  *
941193b9358SAlan Cox  *	MADV_FREE	(OBJT_DEFAULT/OBJT_SWAP objects,
942193b9358SAlan Cox  *			 OBJ_ONEMAPPING only)
943193b9358SAlan Cox  *
944193b9358SAlan Cox  *	    Deactivate and clean the specified pages if they are
945193b9358SAlan Cox  *	    resident.  This permits the process to reuse the pages
946193b9358SAlan Cox  *	    without faulting or the kernel to reclaim the pages
947193b9358SAlan Cox  *	    without I/O.
948867a482dSJohn Dyson  */
949867a482dSJohn Dyson void
9501b40f8c0SMatthew Dillon vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
951867a482dSJohn Dyson {
9526e20a165SJohn Dyson 	vm_pindex_t end, tpindex;
9536e20a165SJohn Dyson 	vm_object_t tobject;
954867a482dSJohn Dyson 	vm_page_t m;
955867a482dSJohn Dyson 
956867a482dSJohn Dyson 	if (object == NULL)
957867a482dSJohn Dyson 		return;
958867a482dSJohn Dyson 
95915c176c1SAlan Cox 	vm_object_lock(object);
960094f6d26SAlan Cox 
961867a482dSJohn Dyson 	end = pindex + count;
962867a482dSJohn Dyson 
9631c7c3c6aSMatthew Dillon 	/*
9641c7c3c6aSMatthew Dillon 	 * Locate and adjust resident pages
9651c7c3c6aSMatthew Dillon 	 */
9661c7c3c6aSMatthew Dillon 	for (; pindex < end; pindex += 1) {
9676e20a165SJohn Dyson relookup:
9686e20a165SJohn Dyson 		tobject = object;
9696e20a165SJohn Dyson 		tpindex = pindex;
9706e20a165SJohn Dyson shadowlookup:
97158b4e6ccSAlan Cox 		/*
97258b4e6ccSAlan Cox 		 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
97358b4e6ccSAlan Cox 		 * and those pages must be OBJ_ONEMAPPING.
97458b4e6ccSAlan Cox 		 */
97558b4e6ccSAlan Cox 		if (advise == MADV_FREE) {
97658b4e6ccSAlan Cox 			if ((tobject->type != OBJT_DEFAULT &&
97758b4e6ccSAlan Cox 			     tobject->type != OBJT_SWAP) ||
97858b4e6ccSAlan Cox 			    (tobject->flags & OBJ_ONEMAPPING) == 0) {
9796e20a165SJohn Dyson 				continue;
9806e20a165SJohn Dyson 			}
98158b4e6ccSAlan Cox 		}
9821c7c3c6aSMatthew Dillon 
9831c7c3c6aSMatthew Dillon 		m = vm_page_lookup(tobject, tpindex);
9841c7c3c6aSMatthew Dillon 
9851c7c3c6aSMatthew Dillon 		if (m == NULL) {
9861ce137beSMatthew Dillon 			/*
9871ce137beSMatthew Dillon 			 * There may be swap even if there is no backing page
9881ce137beSMatthew Dillon 			 */
9891ce137beSMatthew Dillon 			if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
9901ce137beSMatthew Dillon 				swap_pager_freespace(tobject, tpindex, 1);
9911ce137beSMatthew Dillon 
9921ce137beSMatthew Dillon 			/*
9931ce137beSMatthew Dillon 			 * next object
9941ce137beSMatthew Dillon 			 */
9956e20a165SJohn Dyson 			tobject = tobject->backing_object;
9961c7c3c6aSMatthew Dillon 			if (tobject == NULL)
9971c7c3c6aSMatthew Dillon 				continue;
9986e20a165SJohn Dyson 			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
9996e20a165SJohn Dyson 			goto shadowlookup;
10006e20a165SJohn Dyson 		}
1001867a482dSJohn Dyson 
1002867a482dSJohn Dyson 		/*
1003867a482dSJohn Dyson 		 * If the page is busy or not in a normal active state,
10048b03c8edSMatthew Dillon 		 * we skip it.  If the page is not managed there are no
10058b03c8edSMatthew Dillon 		 * page queues to mess with.  Things can break if we mess
10068b03c8edSMatthew Dillon 		 * with pages in any of the below states.
1007867a482dSJohn Dyson 		 */
100832585dd6SAlan Cox 		vm_page_lock_queues();
100932585dd6SAlan Cox 		if (m->hold_count ||
10101c7c3c6aSMatthew Dillon 		    m->wire_count ||
10118b03c8edSMatthew Dillon 		    (m->flags & PG_UNMANAGED) ||
101232585dd6SAlan Cox 		    m->valid != VM_PAGE_BITS_ALL) {
101332585dd6SAlan Cox 			vm_page_unlock_queues();
1014867a482dSJohn Dyson 			continue;
10156e20a165SJohn Dyson 		}
101632585dd6SAlan Cox  		if (vm_page_sleep_if_busy(m, TRUE, "madvpo"))
10176e20a165SJohn Dyson   			goto relookup;
1018867a482dSJohn Dyson 		if (advise == MADV_WILLNEED) {
1019867a482dSJohn Dyson 			vm_page_activate(m);
10206e20a165SJohn Dyson 		} else if (advise == MADV_DONTNEED) {
1021479112dfSMatthew Dillon 			vm_page_dontneed(m);
10220a47b48bSJohn Dyson 		} else if (advise == MADV_FREE) {
10231c7c3c6aSMatthew Dillon 			/*
10242aaeadf8SMatthew Dillon 			 * Mark the page clean.  This will allow the page
10252aaeadf8SMatthew Dillon 			 * to be freed up by the system.  However, such pages
10262aaeadf8SMatthew Dillon 			 * are often reused quickly by malloc()/free()
10272aaeadf8SMatthew Dillon 			 * so we do not do anything that would cause
10282aaeadf8SMatthew Dillon 			 * a page fault if we can help it.
10292aaeadf8SMatthew Dillon 			 *
10302aaeadf8SMatthew Dillon 			 * Specifically, we do not try to actually free
10312aaeadf8SMatthew Dillon 			 * the page now nor do we try to put it in the
10322aaeadf8SMatthew Dillon 			 * cache (which would cause a page fault on reuse).
103341c67e12SMatthew Dillon 			 *
103441c67e12SMatthew Dillon 			 * But we do make the page is freeable as we
103541c67e12SMatthew Dillon 			 * can without actually taking the step of unmapping
103641c67e12SMatthew Dillon 			 * it.
10371c7c3c6aSMatthew Dillon 			 */
10380385347cSPeter Wemm 			pmap_clear_modify(m);
10396e20a165SJohn Dyson 			m->dirty = 0;
104041c67e12SMatthew Dillon 			m->act_count = 0;
1041479112dfSMatthew Dillon 			vm_page_dontneed(m);
1042867a482dSJohn Dyson 		}
10432999e9faSAlan Cox 		vm_page_unlock_queues();
10442999e9faSAlan Cox 		if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
10452999e9faSAlan Cox 			swap_pager_freespace(tobject, tpindex, 1);
1046867a482dSJohn Dyson 	}
104715c176c1SAlan Cox 	vm_object_unlock(object);
1048867a482dSJohn Dyson }
1049867a482dSJohn Dyson 
1050867a482dSJohn Dyson /*
1051df8bae1dSRodney W. Grimes  *	vm_object_shadow:
1052df8bae1dSRodney W. Grimes  *
1053df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
1054df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
1055df8bae1dSRodney W. Grimes  *	object reference is deallocated.
1056df8bae1dSRodney W. Grimes  *
1057df8bae1dSRodney W. Grimes  *	The new object and offset into that object
1058df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
1059df8bae1dSRodney W. Grimes  */
106026f9a767SRodney W. Grimes void
10611b40f8c0SMatthew Dillon vm_object_shadow(
10621b40f8c0SMatthew Dillon 	vm_object_t *object,	/* IN/OUT */
10631b40f8c0SMatthew Dillon 	vm_ooffset_t *offset,	/* IN/OUT */
10641b40f8c0SMatthew Dillon 	vm_size_t length)
1065df8bae1dSRodney W. Grimes {
1066d031cff1SMatthew Dillon 	vm_object_t source;
1067d031cff1SMatthew Dillon 	vm_object_t result;
1068df8bae1dSRodney W. Grimes 
1069df8bae1dSRodney W. Grimes 	source = *object;
1070df8bae1dSRodney W. Grimes 
107115c176c1SAlan Cox 	vm_object_lock(source);
1072df8bae1dSRodney W. Grimes 	/*
10739a2f6362SAlan Cox 	 * Don't create the new object if the old object isn't shared.
10749a2f6362SAlan Cox 	 */
1075c7997d57SAlan Cox 	if (source != NULL &&
1076c7997d57SAlan Cox 	    source->ref_count == 1 &&
10779a2f6362SAlan Cox 	    source->handle == NULL &&
10789a2f6362SAlan Cox 	    (source->type == OBJT_DEFAULT ||
10799917e010SAlan Cox 	     source->type == OBJT_SWAP)) {
108015c176c1SAlan Cox 		vm_object_unlock(source);
10819a2f6362SAlan Cox 		return;
10829917e010SAlan Cox 	}
10839a2f6362SAlan Cox 
10849a2f6362SAlan Cox 	/*
1085df8bae1dSRodney W. Grimes 	 * Allocate a new object with the given length
1086df8bae1dSRodney W. Grimes 	 */
1087971dd342SAlfred Perlstein 	result = vm_object_allocate(OBJT_DEFAULT, length);
1088971dd342SAlfred Perlstein 	KASSERT(result != NULL, ("vm_object_shadow: no object for shadowing"));
1089df8bae1dSRodney W. Grimes 
1090df8bae1dSRodney W. Grimes 	/*
10910d94caffSDavid Greenman 	 * The new object shadows the source object, adding a reference to it.
10920d94caffSDavid Greenman 	 * Our caller changes his reference to point to the new object,
10930d94caffSDavid Greenman 	 * removing a reference to the source object.  Net result: no change
10940d94caffSDavid Greenman 	 * of reference count.
10959b09fe24SMatthew Dillon 	 *
10969b09fe24SMatthew Dillon 	 * Try to optimize the result object's page color when shadowing
1097956f3135SPhilippe Charnier 	 * in order to maintain page coloring consistency in the combined
10989b09fe24SMatthew Dillon 	 * shadowed object.
1099df8bae1dSRodney W. Grimes 	 */
110024a1cce3SDavid Greenman 	result->backing_object = source;
1101de5f6a77SJohn Dyson 	if (source) {
1102de5f6a77SJohn Dyson 		TAILQ_INSERT_TAIL(&source->shadow_head, result, shadow_list);
1103eaf13dd7SJohn Dyson 		source->shadow_count++;
1104eaf13dd7SJohn Dyson 		source->generation++;
1105dcc5840eSAlan Cox 		if (length < source->size)
1106dcc5840eSAlan Cox 			length = source->size;
1107dcc5840eSAlan Cox 		if (length > PQ_L2_SIZE / 3 + PQ_PRIME1 ||
1108dcc5840eSAlan Cox 		    source->generation > 1)
1109dcc5840eSAlan Cox 			length = PQ_L2_SIZE / 3 + PQ_PRIME1;
1110dcc5840eSAlan Cox 		result->pg_color = (source->pg_color +
1111dcc5840eSAlan Cox 		    length * source->generation) & PQ_L2_MASK;
1112dcc5840eSAlan Cox 		next_index = (result->pg_color + PQ_L2_SIZE / 3 + PQ_PRIME1) &
1113dcc5840eSAlan Cox 		    PQ_L2_MASK;
1114de5f6a77SJohn Dyson 	}
1115df8bae1dSRodney W. Grimes 
1116df8bae1dSRodney W. Grimes 	/*
11170d94caffSDavid Greenman 	 * Store the offset into the source object, and fix up the offset into
11180d94caffSDavid Greenman 	 * the new object.
1119df8bae1dSRodney W. Grimes 	 */
112024a1cce3SDavid Greenman 	result->backing_object_offset = *offset;
1121df8bae1dSRodney W. Grimes 
1122df8bae1dSRodney W. Grimes 	/*
1123df8bae1dSRodney W. Grimes 	 * Return the new things
1124df8bae1dSRodney W. Grimes 	 */
1125df8bae1dSRodney W. Grimes 	*offset = 0;
1126df8bae1dSRodney W. Grimes 	*object = result;
11279917e010SAlan Cox 
112815c176c1SAlan Cox 	vm_object_unlock(source);
1129df8bae1dSRodney W. Grimes }
1130df8bae1dSRodney W. Grimes 
1131c5aaa06dSAlan Cox /*
1132c5aaa06dSAlan Cox  *	vm_object_split:
1133c5aaa06dSAlan Cox  *
1134c5aaa06dSAlan Cox  * Split the pages in a map entry into a new object.  This affords
1135c5aaa06dSAlan Cox  * easier removal of unused pages, and keeps object inheritance from
1136c5aaa06dSAlan Cox  * being a negative impact on memory usage.
1137c5aaa06dSAlan Cox  */
1138c5aaa06dSAlan Cox void
1139c5aaa06dSAlan Cox vm_object_split(vm_map_entry_t entry)
1140c5aaa06dSAlan Cox {
1141c5aaa06dSAlan Cox 	vm_page_t m;
1142c5aaa06dSAlan Cox 	vm_object_t orig_object, new_object, source;
1143c5aaa06dSAlan Cox 	vm_offset_t s, e;
114423f09d50SIan Dowse 	vm_pindex_t offidxstart, offidxend;
114523f09d50SIan Dowse 	vm_size_t idx, size;
1146c5aaa06dSAlan Cox 	vm_ooffset_t offset;
1147c5aaa06dSAlan Cox 
1148c5aaa06dSAlan Cox 	GIANT_REQUIRED;
1149c5aaa06dSAlan Cox 
1150c5aaa06dSAlan Cox 	orig_object = entry->object.vm_object;
1151c5aaa06dSAlan Cox 	if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1152c5aaa06dSAlan Cox 		return;
1153c5aaa06dSAlan Cox 	if (orig_object->ref_count <= 1)
1154c5aaa06dSAlan Cox 		return;
1155c5aaa06dSAlan Cox 
1156c5aaa06dSAlan Cox 	offset = entry->offset;
1157c5aaa06dSAlan Cox 	s = entry->start;
1158c5aaa06dSAlan Cox 	e = entry->end;
1159c5aaa06dSAlan Cox 
1160c5aaa06dSAlan Cox 	offidxstart = OFF_TO_IDX(offset);
1161c5aaa06dSAlan Cox 	offidxend = offidxstart + OFF_TO_IDX(e - s);
1162c5aaa06dSAlan Cox 	size = offidxend - offidxstart;
1163c5aaa06dSAlan Cox 
1164c5aaa06dSAlan Cox 	new_object = vm_pager_allocate(orig_object->type,
1165c5aaa06dSAlan Cox 		NULL, IDX_TO_OFF(size), VM_PROT_ALL, 0LL);
1166c5aaa06dSAlan Cox 	if (new_object == NULL)
1167c5aaa06dSAlan Cox 		return;
1168c5aaa06dSAlan Cox 
1169c5aaa06dSAlan Cox 	source = orig_object->backing_object;
1170c5aaa06dSAlan Cox 	if (source != NULL) {
1171c5aaa06dSAlan Cox 		vm_object_reference(source);	/* Referenced by new_object */
1172c5aaa06dSAlan Cox 		TAILQ_INSERT_TAIL(&source->shadow_head,
1173c5aaa06dSAlan Cox 				  new_object, shadow_list);
1174c5aaa06dSAlan Cox 		vm_object_clear_flag(source, OBJ_ONEMAPPING);
1175c5aaa06dSAlan Cox 		new_object->backing_object_offset =
117656030358SAlan Cox 			orig_object->backing_object_offset + offset;
1177c5aaa06dSAlan Cox 		new_object->backing_object = source;
1178c5aaa06dSAlan Cox 		source->shadow_count++;
1179c5aaa06dSAlan Cox 		source->generation++;
1180c5aaa06dSAlan Cox 	}
1181c5aaa06dSAlan Cox 	for (idx = 0; idx < size; idx++) {
1182c5aaa06dSAlan Cox 	retry:
1183c5aaa06dSAlan Cox 		m = vm_page_lookup(orig_object, offidxstart + idx);
1184c5aaa06dSAlan Cox 		if (m == NULL)
1185c5aaa06dSAlan Cox 			continue;
1186c5aaa06dSAlan Cox 
1187c5aaa06dSAlan Cox 		/*
1188c5aaa06dSAlan Cox 		 * We must wait for pending I/O to complete before we can
1189c5aaa06dSAlan Cox 		 * rename the page.
1190c5aaa06dSAlan Cox 		 *
1191c5aaa06dSAlan Cox 		 * We do not have to VM_PROT_NONE the page as mappings should
1192c5aaa06dSAlan Cox 		 * not be changed by this operation.
1193c5aaa06dSAlan Cox 		 */
119400f9e8b4SAlan Cox 		vm_page_lock_queues();
119500f9e8b4SAlan Cox 		if (vm_page_sleep_if_busy(m, TRUE, "spltwt"))
1196c5aaa06dSAlan Cox 			goto retry;
1197c5aaa06dSAlan Cox 
1198c5aaa06dSAlan Cox 		vm_page_busy(m);
1199c5aaa06dSAlan Cox 		vm_page_rename(m, new_object, idx);
1200c5aaa06dSAlan Cox 		/* page automatically made dirty by rename and cache handled */
1201c5aaa06dSAlan Cox 		vm_page_busy(m);
1202bd82dc74SAlan Cox 		vm_page_unlock_queues();
1203c5aaa06dSAlan Cox 	}
1204c5aaa06dSAlan Cox 	if (orig_object->type == OBJT_SWAP) {
1205c5aaa06dSAlan Cox 		vm_object_pip_add(orig_object, 1);
1206c5aaa06dSAlan Cox 		/*
1207c5aaa06dSAlan Cox 		 * copy orig_object pages into new_object
1208c5aaa06dSAlan Cox 		 * and destroy unneeded pages in
1209c5aaa06dSAlan Cox 		 * shadow object.
1210c5aaa06dSAlan Cox 		 */
1211c5aaa06dSAlan Cox 		swap_pager_copy(orig_object, new_object, offidxstart, 0);
1212c5aaa06dSAlan Cox 		vm_object_pip_wakeup(orig_object);
1213c5aaa06dSAlan Cox 	}
1214dc907f66SAlan Cox 	vm_page_lock_queues();
1215c7118ed6SAlan Cox 	TAILQ_FOREACH(m, &new_object->memq, listq)
1216c5aaa06dSAlan Cox 		vm_page_wakeup(m);
1217dc907f66SAlan Cox 	vm_page_unlock_queues();
1218c5aaa06dSAlan Cox 	entry->object.vm_object = new_object;
1219c5aaa06dSAlan Cox 	entry->offset = 0LL;
1220c5aaa06dSAlan Cox 	vm_object_deallocate(orig_object);
1221c5aaa06dSAlan Cox }
1222c5aaa06dSAlan Cox 
12232ad1a3f7SMatthew Dillon #define	OBSC_TEST_ALL_SHADOWED	0x0001
12242ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_NOWAIT	0x0002
12252ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_WAIT	0x0004
12262ad1a3f7SMatthew Dillon 
12272ad1a3f7SMatthew Dillon static __inline int
12282ad1a3f7SMatthew Dillon vm_object_backing_scan(vm_object_t object, int op)
12292ad1a3f7SMatthew Dillon {
12302ad1a3f7SMatthew Dillon 	int s;
12312ad1a3f7SMatthew Dillon 	int r = 1;
12322ad1a3f7SMatthew Dillon 	vm_page_t p;
12332ad1a3f7SMatthew Dillon 	vm_object_t backing_object;
12342ad1a3f7SMatthew Dillon 	vm_pindex_t backing_offset_index;
12352ad1a3f7SMatthew Dillon 
12362ad1a3f7SMatthew Dillon 	s = splvm();
12370cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
12382ad1a3f7SMatthew Dillon 
12392ad1a3f7SMatthew Dillon 	backing_object = object->backing_object;
12402ad1a3f7SMatthew Dillon 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
12412ad1a3f7SMatthew Dillon 
12422ad1a3f7SMatthew Dillon 	/*
12432ad1a3f7SMatthew Dillon 	 * Initial conditions
12442ad1a3f7SMatthew Dillon 	 */
12452ad1a3f7SMatthew Dillon 	if (op & OBSC_TEST_ALL_SHADOWED) {
12462ad1a3f7SMatthew Dillon 		/*
1247956f3135SPhilippe Charnier 		 * We do not want to have to test for the existence of
12482ad1a3f7SMatthew Dillon 		 * swap pages in the backing object.  XXX but with the
12492ad1a3f7SMatthew Dillon 		 * new swapper this would be pretty easy to do.
12502ad1a3f7SMatthew Dillon 		 *
12512ad1a3f7SMatthew Dillon 		 * XXX what about anonymous MAP_SHARED memory that hasn't
12522ad1a3f7SMatthew Dillon 		 * been ZFOD faulted yet?  If we do not test for this, the
12532ad1a3f7SMatthew Dillon 		 * shadow test may succeed! XXX
12542ad1a3f7SMatthew Dillon 		 */
12552ad1a3f7SMatthew Dillon 		if (backing_object->type != OBJT_DEFAULT) {
12562ad1a3f7SMatthew Dillon 			splx(s);
12572ad1a3f7SMatthew Dillon 			return (0);
12582ad1a3f7SMatthew Dillon 		}
12592ad1a3f7SMatthew Dillon 	}
12602ad1a3f7SMatthew Dillon 	if (op & OBSC_COLLAPSE_WAIT) {
12612ad1a3f7SMatthew Dillon 		vm_object_set_flag(backing_object, OBJ_DEAD);
12622ad1a3f7SMatthew Dillon 	}
12632ad1a3f7SMatthew Dillon 
12642ad1a3f7SMatthew Dillon 	/*
12652ad1a3f7SMatthew Dillon 	 * Our scan
12662ad1a3f7SMatthew Dillon 	 */
12672ad1a3f7SMatthew Dillon 	p = TAILQ_FIRST(&backing_object->memq);
12682ad1a3f7SMatthew Dillon 	while (p) {
12692ad1a3f7SMatthew Dillon 		vm_page_t next = TAILQ_NEXT(p, listq);
12702ad1a3f7SMatthew Dillon 		vm_pindex_t new_pindex = p->pindex - backing_offset_index;
12712ad1a3f7SMatthew Dillon 
12722ad1a3f7SMatthew Dillon 		if (op & OBSC_TEST_ALL_SHADOWED) {
12732ad1a3f7SMatthew Dillon 			vm_page_t pp;
12742ad1a3f7SMatthew Dillon 
12752ad1a3f7SMatthew Dillon 			/*
12762ad1a3f7SMatthew Dillon 			 * Ignore pages outside the parent object's range
12772ad1a3f7SMatthew Dillon 			 * and outside the parent object's mapping of the
12782ad1a3f7SMatthew Dillon 			 * backing object.
12792ad1a3f7SMatthew Dillon 			 *
12802ad1a3f7SMatthew Dillon 			 * note that we do not busy the backing object's
12812ad1a3f7SMatthew Dillon 			 * page.
12822ad1a3f7SMatthew Dillon 			 */
12832ad1a3f7SMatthew Dillon 			if (
12842ad1a3f7SMatthew Dillon 			    p->pindex < backing_offset_index ||
12852ad1a3f7SMatthew Dillon 			    new_pindex >= object->size
12862ad1a3f7SMatthew Dillon 			) {
12872ad1a3f7SMatthew Dillon 				p = next;
12882ad1a3f7SMatthew Dillon 				continue;
12892ad1a3f7SMatthew Dillon 			}
12902ad1a3f7SMatthew Dillon 
12912ad1a3f7SMatthew Dillon 			/*
12922ad1a3f7SMatthew Dillon 			 * See if the parent has the page or if the parent's
12932ad1a3f7SMatthew Dillon 			 * object pager has the page.  If the parent has the
12942ad1a3f7SMatthew Dillon 			 * page but the page is not valid, the parent's
12952ad1a3f7SMatthew Dillon 			 * object pager must have the page.
12962ad1a3f7SMatthew Dillon 			 *
12972ad1a3f7SMatthew Dillon 			 * If this fails, the parent does not completely shadow
12982ad1a3f7SMatthew Dillon 			 * the object and we might as well give up now.
12992ad1a3f7SMatthew Dillon 			 */
13002ad1a3f7SMatthew Dillon 
13012ad1a3f7SMatthew Dillon 			pp = vm_page_lookup(object, new_pindex);
13022ad1a3f7SMatthew Dillon 			if (
13032ad1a3f7SMatthew Dillon 			    (pp == NULL || pp->valid == 0) &&
13042ad1a3f7SMatthew Dillon 			    !vm_pager_has_page(object, new_pindex, NULL, NULL)
13052ad1a3f7SMatthew Dillon 			) {
13062ad1a3f7SMatthew Dillon 				r = 0;
13072ad1a3f7SMatthew Dillon 				break;
13082ad1a3f7SMatthew Dillon 			}
13092ad1a3f7SMatthew Dillon 		}
13102ad1a3f7SMatthew Dillon 
13112ad1a3f7SMatthew Dillon 		/*
13122ad1a3f7SMatthew Dillon 		 * Check for busy page
13132ad1a3f7SMatthew Dillon 		 */
13142ad1a3f7SMatthew Dillon 		if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
13152ad1a3f7SMatthew Dillon 			vm_page_t pp;
13162ad1a3f7SMatthew Dillon 
131700f9e8b4SAlan Cox 			vm_page_lock_queues();
13182ad1a3f7SMatthew Dillon 			if (op & OBSC_COLLAPSE_NOWAIT) {
131900f9e8b4SAlan Cox 				if ((p->flags & PG_BUSY) ||
13202ad1a3f7SMatthew Dillon 				    !p->valid ||
13212ad1a3f7SMatthew Dillon 				    p->hold_count ||
13222ad1a3f7SMatthew Dillon 				    p->wire_count ||
132300f9e8b4SAlan Cox 				    p->busy) {
132400f9e8b4SAlan Cox 					vm_page_unlock_queues();
13252ad1a3f7SMatthew Dillon 					p = next;
13262ad1a3f7SMatthew Dillon 					continue;
13272ad1a3f7SMatthew Dillon 				}
13282ad1a3f7SMatthew Dillon 			} else if (op & OBSC_COLLAPSE_WAIT) {
132900f9e8b4SAlan Cox 				if (vm_page_sleep_if_busy(p, TRUE, "vmocol")) {
13302ad1a3f7SMatthew Dillon 					/*
13312ad1a3f7SMatthew Dillon 					 * If we slept, anything could have
13322ad1a3f7SMatthew Dillon 					 * happened.  Since the object is
13332ad1a3f7SMatthew Dillon 					 * marked dead, the backing offset
13342ad1a3f7SMatthew Dillon 					 * should not have changed so we
13352ad1a3f7SMatthew Dillon 					 * just restart our scan.
13362ad1a3f7SMatthew Dillon 					 */
13372ad1a3f7SMatthew Dillon 					p = TAILQ_FIRST(&backing_object->memq);
13382ad1a3f7SMatthew Dillon 					continue;
13392ad1a3f7SMatthew Dillon 				}
13402ad1a3f7SMatthew Dillon 			}
13412ad1a3f7SMatthew Dillon 
13422ad1a3f7SMatthew Dillon 			/*
13432ad1a3f7SMatthew Dillon 			 * Busy the page
13442ad1a3f7SMatthew Dillon 			 */
13452ad1a3f7SMatthew Dillon 			vm_page_busy(p);
134600f9e8b4SAlan Cox 			vm_page_unlock_queues();
13472ad1a3f7SMatthew Dillon 
13482ad1a3f7SMatthew Dillon 			KASSERT(
13492ad1a3f7SMatthew Dillon 			    p->object == backing_object,
13502ad1a3f7SMatthew Dillon 			    ("vm_object_qcollapse(): object mismatch")
13512ad1a3f7SMatthew Dillon 			);
13522ad1a3f7SMatthew Dillon 
13532ad1a3f7SMatthew Dillon 			/*
13542ad1a3f7SMatthew Dillon 			 * Destroy any associated swap
13552ad1a3f7SMatthew Dillon 			 */
13562ad1a3f7SMatthew Dillon 			if (backing_object->type == OBJT_SWAP) {
13572ad1a3f7SMatthew Dillon 				swap_pager_freespace(
13582ad1a3f7SMatthew Dillon 				    backing_object,
13592ad1a3f7SMatthew Dillon 				    p->pindex,
13602ad1a3f7SMatthew Dillon 				    1
13612ad1a3f7SMatthew Dillon 				);
13622ad1a3f7SMatthew Dillon 			}
13632ad1a3f7SMatthew Dillon 
13642ad1a3f7SMatthew Dillon 			if (
13652ad1a3f7SMatthew Dillon 			    p->pindex < backing_offset_index ||
13662ad1a3f7SMatthew Dillon 			    new_pindex >= object->size
13672ad1a3f7SMatthew Dillon 			) {
13682ad1a3f7SMatthew Dillon 				/*
13692ad1a3f7SMatthew Dillon 				 * Page is out of the parent object's range, we
13702ad1a3f7SMatthew Dillon 				 * can simply destroy it.
13712ad1a3f7SMatthew Dillon 				 */
13726a684ecfSAlan Cox 				vm_page_lock_queues();
13734fec79beSAlan Cox 				pmap_remove_all(p);
13742ad1a3f7SMatthew Dillon 				vm_page_free(p);
13756a684ecfSAlan Cox 				vm_page_unlock_queues();
13762ad1a3f7SMatthew Dillon 				p = next;
13772ad1a3f7SMatthew Dillon 				continue;
13782ad1a3f7SMatthew Dillon 			}
13792ad1a3f7SMatthew Dillon 
13802ad1a3f7SMatthew Dillon 			pp = vm_page_lookup(object, new_pindex);
13812ad1a3f7SMatthew Dillon 			if (
13822ad1a3f7SMatthew Dillon 			    pp != NULL ||
13832ad1a3f7SMatthew Dillon 			    vm_pager_has_page(object, new_pindex, NULL, NULL)
13842ad1a3f7SMatthew Dillon 			) {
13852ad1a3f7SMatthew Dillon 				/*
13862ad1a3f7SMatthew Dillon 				 * page already exists in parent OR swap exists
13872ad1a3f7SMatthew Dillon 				 * for this location in the parent.  Destroy
13882ad1a3f7SMatthew Dillon 				 * the original page from the backing object.
13892ad1a3f7SMatthew Dillon 				 *
13902ad1a3f7SMatthew Dillon 				 * Leave the parent's page alone
13912ad1a3f7SMatthew Dillon 				 */
13926a684ecfSAlan Cox 				vm_page_lock_queues();
13934fec79beSAlan Cox 				pmap_remove_all(p);
13942ad1a3f7SMatthew Dillon 				vm_page_free(p);
13956a684ecfSAlan Cox 				vm_page_unlock_queues();
13962ad1a3f7SMatthew Dillon 				p = next;
13972ad1a3f7SMatthew Dillon 				continue;
13982ad1a3f7SMatthew Dillon 			}
13992ad1a3f7SMatthew Dillon 
14002ad1a3f7SMatthew Dillon 			/*
14012ad1a3f7SMatthew Dillon 			 * Page does not exist in parent, rename the
14022ad1a3f7SMatthew Dillon 			 * page from the backing object to the main object.
1403d1bf5d56SMatthew Dillon 			 *
1404d1bf5d56SMatthew Dillon 			 * If the page was mapped to a process, it can remain
1405d1bf5d56SMatthew Dillon 			 * mapped through the rename.
14062ad1a3f7SMatthew Dillon 			 */
1407a28cc55eSAlan Cox 			vm_page_lock_queues();
14082ad1a3f7SMatthew Dillon 			vm_page_rename(p, object, new_pindex);
1409a28cc55eSAlan Cox 			vm_page_unlock_queues();
14102ad1a3f7SMatthew Dillon 			/* page automatically made dirty by rename */
14112ad1a3f7SMatthew Dillon 		}
14122ad1a3f7SMatthew Dillon 		p = next;
14132ad1a3f7SMatthew Dillon 	}
14142ad1a3f7SMatthew Dillon 	splx(s);
14152ad1a3f7SMatthew Dillon 	return (r);
14162ad1a3f7SMatthew Dillon }
14172ad1a3f7SMatthew Dillon 
1418df8bae1dSRodney W. Grimes 
1419df8bae1dSRodney W. Grimes /*
14202fe6e4d7SDavid Greenman  * this version of collapse allows the operation to occur earlier and
14212fe6e4d7SDavid Greenman  * when paging_in_progress is true for an object...  This is not a complete
14222fe6e4d7SDavid Greenman  * operation, but should plug 99.9% of the rest of the leaks.
14232fe6e4d7SDavid Greenman  */
14242fe6e4d7SDavid Greenman static void
14251b40f8c0SMatthew Dillon vm_object_qcollapse(vm_object_t object)
14262fe6e4d7SDavid Greenman {
14272ad1a3f7SMatthew Dillon 	vm_object_t backing_object = object->backing_object;
14282fe6e4d7SDavid Greenman 
14291b40f8c0SMatthew Dillon 	GIANT_REQUIRED;
14301b40f8c0SMatthew Dillon 
14312fe6e4d7SDavid Greenman 	if (backing_object->ref_count != 1)
14322fe6e4d7SDavid Greenman 		return;
14332fe6e4d7SDavid Greenman 
1434010cf3b9SDavid Greenman 	backing_object->ref_count += 2;
1435010cf3b9SDavid Greenman 
14362ad1a3f7SMatthew Dillon 	vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
14371c7c3c6aSMatthew Dillon 
1438010cf3b9SDavid Greenman 	backing_object->ref_count -= 2;
14392fe6e4d7SDavid Greenman }
14402fe6e4d7SDavid Greenman 
1441df8bae1dSRodney W. Grimes /*
1442df8bae1dSRodney W. Grimes  *	vm_object_collapse:
1443df8bae1dSRodney W. Grimes  *
1444df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
1445df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
1446df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
1447df8bae1dSRodney W. Grimes  */
144826f9a767SRodney W. Grimes void
14491b40f8c0SMatthew Dillon vm_object_collapse(vm_object_t object)
1450df8bae1dSRodney W. Grimes {
14510cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
145223955314SAlfred Perlstein 
1453df8bae1dSRodney W. Grimes 	while (TRUE) {
14542ad1a3f7SMatthew Dillon 		vm_object_t backing_object;
14552ad1a3f7SMatthew Dillon 
1456df8bae1dSRodney W. Grimes 		/*
1457df8bae1dSRodney W. Grimes 		 * Verify that the conditions are right for collapse:
1458df8bae1dSRodney W. Grimes 		 *
14592ad1a3f7SMatthew Dillon 		 * The object exists and the backing object exists.
1460df8bae1dSRodney W. Grimes 		 */
14612fe6e4d7SDavid Greenman 		if (object == NULL)
14622ad1a3f7SMatthew Dillon 			break;
1463df8bae1dSRodney W. Grimes 
146424a1cce3SDavid Greenman 		if ((backing_object = object->backing_object) == NULL)
14652ad1a3f7SMatthew Dillon 			break;
1466df8bae1dSRodney W. Grimes 
1467f919ebdeSDavid Greenman 		/*
1468f919ebdeSDavid Greenman 		 * we check the backing object first, because it is most likely
146924a1cce3SDavid Greenman 		 * not collapsable.
1470f919ebdeSDavid Greenman 		 */
147124a1cce3SDavid Greenman 		if (backing_object->handle != NULL ||
147224a1cce3SDavid Greenman 		    (backing_object->type != OBJT_DEFAULT &&
147324a1cce3SDavid Greenman 		     backing_object->type != OBJT_SWAP) ||
1474f919ebdeSDavid Greenman 		    (backing_object->flags & OBJ_DEAD) ||
147524a1cce3SDavid Greenman 		    object->handle != NULL ||
147624a1cce3SDavid Greenman 		    (object->type != OBJT_DEFAULT &&
147724a1cce3SDavid Greenman 		     object->type != OBJT_SWAP) ||
147824a1cce3SDavid Greenman 		    (object->flags & OBJ_DEAD)) {
14792ad1a3f7SMatthew Dillon 			break;
148024a1cce3SDavid Greenman 		}
14819b4814bbSDavid Greenman 
14822ad1a3f7SMatthew Dillon 		if (
14832ad1a3f7SMatthew Dillon 		    object->paging_in_progress != 0 ||
14842ad1a3f7SMatthew Dillon 		    backing_object->paging_in_progress != 0
14852ad1a3f7SMatthew Dillon 		) {
1486b9921222SDavid Greenman 			vm_object_qcollapse(object);
14872ad1a3f7SMatthew Dillon 			break;
1488df8bae1dSRodney W. Grimes 		}
1489f919ebdeSDavid Greenman 
149026f9a767SRodney W. Grimes 		/*
14910d94caffSDavid Greenman 		 * We know that we can either collapse the backing object (if
14922ad1a3f7SMatthew Dillon 		 * the parent is the only reference to it) or (perhaps) have
14932ad1a3f7SMatthew Dillon 		 * the parent bypass the object if the parent happens to shadow
14942ad1a3f7SMatthew Dillon 		 * all the resident pages in the entire backing object.
14952ad1a3f7SMatthew Dillon 		 *
14962ad1a3f7SMatthew Dillon 		 * This is ignoring pager-backed pages such as swap pages.
14972ad1a3f7SMatthew Dillon 		 * vm_object_backing_scan fails the shadowing test in this
14982ad1a3f7SMatthew Dillon 		 * case.
1499df8bae1dSRodney W. Grimes 		 */
1500df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
1501df8bae1dSRodney W. Grimes 			/*
15022ad1a3f7SMatthew Dillon 			 * If there is exactly one reference to the backing
15032ad1a3f7SMatthew Dillon 			 * object, we can collapse it into the parent.
1504df8bae1dSRodney W. Grimes 			 */
15052ad1a3f7SMatthew Dillon 			vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1506df8bae1dSRodney W. Grimes 
1507df8bae1dSRodney W. Grimes 			/*
1508df8bae1dSRodney W. Grimes 			 * Move the pager from backing_object to object.
1509df8bae1dSRodney W. Grimes 			 */
151024a1cce3SDavid Greenman 			if (backing_object->type == OBJT_SWAP) {
1511d474eaaaSDoug Rabson 				vm_object_pip_add(backing_object, 1);
151224a1cce3SDavid Greenman 
151324a1cce3SDavid Greenman 				/*
15141c7c3c6aSMatthew Dillon 				 * scrap the paging_offset junk and do a
15151c7c3c6aSMatthew Dillon 				 * discrete copy.  This also removes major
15161c7c3c6aSMatthew Dillon 				 * assumptions about how the swap-pager
15171c7c3c6aSMatthew Dillon 				 * works from where it doesn't belong.  The
15181c7c3c6aSMatthew Dillon 				 * new swapper is able to optimize the
15191c7c3c6aSMatthew Dillon 				 * destroy-source case.
152024a1cce3SDavid Greenman 				 */
15211c7c3c6aSMatthew Dillon 				vm_object_pip_add(object, 1);
15221c7c3c6aSMatthew Dillon 				swap_pager_copy(
15231c7c3c6aSMatthew Dillon 				    backing_object,
15241c7c3c6aSMatthew Dillon 				    object,
15251c7c3c6aSMatthew Dillon 				    OFF_TO_IDX(object->backing_object_offset), TRUE);
1526f919ebdeSDavid Greenman 				vm_object_pip_wakeup(object);
1527c0503609SDavid Greenman 
1528f919ebdeSDavid Greenman 				vm_object_pip_wakeup(backing_object);
1529c0503609SDavid Greenman 			}
1530df8bae1dSRodney W. Grimes 			/*
1531df8bae1dSRodney W. Grimes 			 * Object now shadows whatever backing_object did.
15322ad1a3f7SMatthew Dillon 			 * Note that the reference to
15332ad1a3f7SMatthew Dillon 			 * backing_object->backing_object moves from within
15342ad1a3f7SMatthew Dillon 			 * backing_object to within object.
1535df8bae1dSRodney W. Grimes 			 */
15362ad1a3f7SMatthew Dillon 			TAILQ_REMOVE(
15372ad1a3f7SMatthew Dillon 			    &object->backing_object->shadow_head,
15382ad1a3f7SMatthew Dillon 			    object,
15392ad1a3f7SMatthew Dillon 			    shadow_list
15402ad1a3f7SMatthew Dillon 			);
1541eaf13dd7SJohn Dyson 			object->backing_object->shadow_count--;
1542eaf13dd7SJohn Dyson 			object->backing_object->generation++;
1543de5f6a77SJohn Dyson 			if (backing_object->backing_object) {
15442ad1a3f7SMatthew Dillon 				TAILQ_REMOVE(
15452ad1a3f7SMatthew Dillon 				    &backing_object->backing_object->shadow_head,
15462ad1a3f7SMatthew Dillon 				    backing_object,
15472ad1a3f7SMatthew Dillon 				    shadow_list
15482ad1a3f7SMatthew Dillon 				);
1549eaf13dd7SJohn Dyson 				backing_object->backing_object->shadow_count--;
1550eaf13dd7SJohn Dyson 				backing_object->backing_object->generation++;
1551de5f6a77SJohn Dyson 			}
155224a1cce3SDavid Greenman 			object->backing_object = backing_object->backing_object;
1553de5f6a77SJohn Dyson 			if (object->backing_object) {
15542ad1a3f7SMatthew Dillon 				TAILQ_INSERT_TAIL(
15552ad1a3f7SMatthew Dillon 				    &object->backing_object->shadow_head,
15562ad1a3f7SMatthew Dillon 				    object,
15572ad1a3f7SMatthew Dillon 				    shadow_list
15582ad1a3f7SMatthew Dillon 				);
1559eaf13dd7SJohn Dyson 				object->backing_object->shadow_count++;
1560eaf13dd7SJohn Dyson 				object->backing_object->generation++;
1561de5f6a77SJohn Dyson 			}
15622fe6e4d7SDavid Greenman 
15632ad1a3f7SMatthew Dillon 			object->backing_object_offset +=
15642ad1a3f7SMatthew Dillon 			    backing_object->backing_object_offset;
15652ad1a3f7SMatthew Dillon 
1566df8bae1dSRodney W. Grimes 			/*
1567df8bae1dSRodney W. Grimes 			 * Discard backing_object.
1568df8bae1dSRodney W. Grimes 			 *
15690d94caffSDavid Greenman 			 * Since the backing object has no pages, no pager left,
15700d94caffSDavid Greenman 			 * and no object references within it, all that is
15710d94caffSDavid Greenman 			 * necessary is to dispose of it.
1572df8bae1dSRodney W. Grimes 			 */
1573245df27cSMatthew Dillon 			KASSERT(backing_object->ref_count == 1, ("backing_object %p was somehow re-referenced during collapse!", backing_object));
1574245df27cSMatthew Dillon 			KASSERT(TAILQ_FIRST(&backing_object->memq) == NULL, ("backing_object %p somehow has left over pages during collapse!", backing_object));
1575df8bae1dSRodney W. Grimes 
1576a5698387SAlan Cox 			mtx_lock(&vm_object_list_mtx);
15772ad1a3f7SMatthew Dillon 			TAILQ_REMOVE(
15782ad1a3f7SMatthew Dillon 			    &vm_object_list,
15792ad1a3f7SMatthew Dillon 			    backing_object,
15802ad1a3f7SMatthew Dillon 			    object_list
15812ad1a3f7SMatthew Dillon 			);
1582a5698387SAlan Cox 			mtx_unlock(&vm_object_list_mtx);
1583df8bae1dSRodney W. Grimes 
1584e3a9e1b2SAlan Cox 			mtx_destroy(&backing_object->mtx);
1585e3a9e1b2SAlan Cox 
1586670d17b5SJeff Roberson 			uma_zfree(obj_zone, backing_object);
1587df8bae1dSRodney W. Grimes 
1588df8bae1dSRodney W. Grimes 			object_collapses++;
15890d94caffSDavid Greenman 		} else {
159095e5e988SJohn Dyson 			vm_object_t new_backing_object;
1591df8bae1dSRodney W. Grimes 
1592df8bae1dSRodney W. Grimes 			/*
15932ad1a3f7SMatthew Dillon 			 * If we do not entirely shadow the backing object,
15942ad1a3f7SMatthew Dillon 			 * there is nothing we can do so we give up.
1595df8bae1dSRodney W. Grimes 			 */
15962ad1a3f7SMatthew Dillon 			if (vm_object_backing_scan(object, OBSC_TEST_ALL_SHADOWED) == 0) {
15972ad1a3f7SMatthew Dillon 				break;
159824a1cce3SDavid Greenman 			}
1599df8bae1dSRodney W. Grimes 
1600df8bae1dSRodney W. Grimes 			/*
16010d94caffSDavid Greenman 			 * Make the parent shadow the next object in the
16020d94caffSDavid Greenman 			 * chain.  Deallocating backing_object will not remove
16030d94caffSDavid Greenman 			 * it, since its reference count is at least 2.
1604df8bae1dSRodney W. Grimes 			 */
16052ad1a3f7SMatthew Dillon 			TAILQ_REMOVE(
16062ad1a3f7SMatthew Dillon 			    &backing_object->shadow_head,
16072ad1a3f7SMatthew Dillon 			    object,
16082ad1a3f7SMatthew Dillon 			    shadow_list
16092ad1a3f7SMatthew Dillon 			);
1610eaf13dd7SJohn Dyson 			backing_object->shadow_count--;
1611eaf13dd7SJohn Dyson 			backing_object->generation++;
161295e5e988SJohn Dyson 
161395e5e988SJohn Dyson 			new_backing_object = backing_object->backing_object;
16148aef1712SMatthew Dillon 			if ((object->backing_object = new_backing_object) != NULL) {
161595e5e988SJohn Dyson 				vm_object_reference(new_backing_object);
16162ad1a3f7SMatthew Dillon 				TAILQ_INSERT_TAIL(
16172ad1a3f7SMatthew Dillon 				    &new_backing_object->shadow_head,
16182ad1a3f7SMatthew Dillon 				    object,
16192ad1a3f7SMatthew Dillon 				    shadow_list
16202ad1a3f7SMatthew Dillon 				);
1621eaf13dd7SJohn Dyson 				new_backing_object->shadow_count++;
1622eaf13dd7SJohn Dyson 				new_backing_object->generation++;
162395e5e988SJohn Dyson 				object->backing_object_offset +=
162495e5e988SJohn Dyson 					backing_object->backing_object_offset;
1625de5f6a77SJohn Dyson 			}
1626df8bae1dSRodney W. Grimes 
1627df8bae1dSRodney W. Grimes 			/*
16280d94caffSDavid Greenman 			 * Drop the reference count on backing_object. Since
16290d94caffSDavid Greenman 			 * its ref_count was at least 2, it will not vanish;
1630eaf13dd7SJohn Dyson 			 * so we don't need to call vm_object_deallocate, but
1631eaf13dd7SJohn Dyson 			 * we do anyway.
1632df8bae1dSRodney W. Grimes 			 */
163395e5e988SJohn Dyson 			vm_object_deallocate(backing_object);
1634df8bae1dSRodney W. Grimes 			object_bypasses++;
1635df8bae1dSRodney W. Grimes 		}
1636df8bae1dSRodney W. Grimes 
1637df8bae1dSRodney W. Grimes 		/*
1638df8bae1dSRodney W. Grimes 		 * Try again with this object's new backing object.
1639df8bae1dSRodney W. Grimes 		 */
1640df8bae1dSRodney W. Grimes 	}
1641df8bae1dSRodney W. Grimes }
1642df8bae1dSRodney W. Grimes 
1643df8bae1dSRodney W. Grimes /*
1644df8bae1dSRodney W. Grimes  *	vm_object_page_remove: [internal]
1645df8bae1dSRodney W. Grimes  *
1646df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
1647df8bae1dSRodney W. Grimes  *	object range from the object's list of pages.
1648df8bae1dSRodney W. Grimes  *
1649df8bae1dSRodney W. Grimes  *	The object must be locked.
1650df8bae1dSRodney W. Grimes  */
165126f9a767SRodney W. Grimes void
16521b40f8c0SMatthew Dillon vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end, boolean_t clean_only)
1653df8bae1dSRodney W. Grimes {
1654d031cff1SMatthew Dillon 	vm_page_t p, next;
1655f5ef029eSPoul-Henning Kamp 	int all;
1656df8bae1dSRodney W. Grimes 
16572840cabeSAlan Cox 	if (object == NULL ||
16582840cabeSAlan Cox 	    object->resident_page_count == 0)
1659df8bae1dSRodney W. Grimes 		return;
166095e5e988SJohn Dyson 	all = ((end == 0) && (start == 0));
166195e5e988SJohn Dyson 
16628b03c8edSMatthew Dillon 	/*
16638b03c8edSMatthew Dillon 	 * Since physically-backed objects do not use managed pages, we can't
16648b03c8edSMatthew Dillon 	 * remove pages from the object (we must instead remove the page
16658b03c8edSMatthew Dillon 	 * references, and then destroy the object).
16668b03c8edSMatthew Dillon 	 */
16678b03c8edSMatthew Dillon 	KASSERT(object->type != OBJT_PHYS, ("attempt to remove pages from a physical object"));
16688b03c8edSMatthew Dillon 
1669d474eaaaSDoug Rabson 	vm_object_pip_add(object, 1);
167026f9a767SRodney W. Grimes again:
167132585dd6SAlan Cox 	vm_page_lock_queues();
167275741c04SAlan Cox 	if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
167375741c04SAlan Cox 		if (p->pindex < start) {
167475741c04SAlan Cox 			p = vm_page_splay(start, object->root);
167575741c04SAlan Cox 			if ((object->root = p)->pindex < start)
167675741c04SAlan Cox 				p = TAILQ_NEXT(p, listq);
167775741c04SAlan Cox 		}
167875741c04SAlan Cox 	}
167975741c04SAlan Cox 	/*
168075741c04SAlan Cox 	 * Assert: the variable p is either (1) the page with the
168175741c04SAlan Cox 	 * least pindex greater than or equal to the parameter pindex
168275741c04SAlan Cox 	 * or (2) NULL.
168375741c04SAlan Cox 	 */
168475741c04SAlan Cox 	for (;
168575741c04SAlan Cox 	     p != NULL && (all || p->pindex < end);
168675741c04SAlan Cox 	     p = next) {
1687b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
168875741c04SAlan Cox 
1689bd7e5f99SJohn Dyson 		if (p->wire_count != 0) {
16904fec79beSAlan Cox 			pmap_remove_all(p);
1691ce65e68cSDavid Greenman 			if (!clean_only)
1692bd7e5f99SJohn Dyson 				p->valid = 0;
16930d94caffSDavid Greenman 			continue;
16940d94caffSDavid Greenman 		}
169532585dd6SAlan Cox 		if (vm_page_sleep_if_busy(p, TRUE, "vmopar"))
169626f9a767SRodney W. Grimes 			goto again;
16978f9110f6SJohn Dyson 		if (clean_only && p->valid) {
16987c1f6cedSDavid Greenman 			vm_page_test_dirty(p);
16997c1f6cedSDavid Greenman 			if (p->valid & p->dirty)
17007c1f6cedSDavid Greenman 				continue;
17017c1f6cedSDavid Greenman 		}
1702e69763a3SDoug Rabson 		vm_page_busy(p);
17034fec79beSAlan Cox 		pmap_remove_all(p);
1704df8bae1dSRodney W. Grimes 		vm_page_free(p);
170526f9a767SRodney W. Grimes 	}
170632585dd6SAlan Cox 	vm_page_unlock_queues();
1707f919ebdeSDavid Greenman 	vm_object_pip_wakeup(object);
1708c0503609SDavid Greenman }
1709df8bae1dSRodney W. Grimes 
1710df8bae1dSRodney W. Grimes /*
1711df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
1712df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
1713df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
1714df8bae1dSRodney W. Grimes  *
1715df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
1716df8bae1dSRodney W. Grimes  *
1717df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
1718df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
1719df8bae1dSRodney W. Grimes  *
1720df8bae1dSRodney W. Grimes  *	Parameters:
1721df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
1722df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
1723df8bae1dSRodney W. Grimes  *		next_object	Second object into coalesce
1724df8bae1dSRodney W. Grimes  *		next_offset	Offset into next_object
1725df8bae1dSRodney W. Grimes  *
1726df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
1727df8bae1dSRodney W. Grimes  *		next_size	Size of reference to next_object
1728df8bae1dSRodney W. Grimes  *
1729df8bae1dSRodney W. Grimes  *	Conditions:
1730df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
1731df8bae1dSRodney W. Grimes  */
17320d94caffSDavid Greenman boolean_t
173300e1854aSAlan Cox vm_object_coalesce(vm_object_t prev_object, vm_pindex_t prev_pindex,
173400e1854aSAlan Cox 	vm_size_t prev_size, vm_size_t next_size)
1735df8bae1dSRodney W. Grimes {
1736ea41812fSAlan Cox 	vm_pindex_t next_pindex;
1737df8bae1dSRodney W. Grimes 
173800e1854aSAlan Cox 	if (prev_object == NULL)
1739df8bae1dSRodney W. Grimes 		return (TRUE);
174015c176c1SAlan Cox 	vm_object_lock(prev_object);
17414112823fSMatthew Dillon 	if (prev_object->type != OBJT_DEFAULT &&
17424112823fSMatthew Dillon 	    prev_object->type != OBJT_SWAP) {
174315c176c1SAlan Cox 		vm_object_unlock(prev_object);
174430dcfc09SJohn Dyson 		return (FALSE);
174530dcfc09SJohn Dyson 	}
174630dcfc09SJohn Dyson 
1747df8bae1dSRodney W. Grimes 	/*
1748df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
1749df8bae1dSRodney W. Grimes 	 */
1750df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
1751df8bae1dSRodney W. Grimes 
1752df8bae1dSRodney W. Grimes 	/*
17530d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
17540d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
17550d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
1756df8bae1dSRodney W. Grimes 	 */
17578cc7e047SJohn Dyson 	if (prev_object->backing_object != NULL) {
175815c176c1SAlan Cox 		vm_object_unlock(prev_object);
1759df8bae1dSRodney W. Grimes 		return (FALSE);
1760df8bae1dSRodney W. Grimes 	}
1761a316d390SJohn Dyson 
1762a316d390SJohn Dyson 	prev_size >>= PAGE_SHIFT;
1763a316d390SJohn Dyson 	next_size >>= PAGE_SHIFT;
1764ea41812fSAlan Cox 	next_pindex = prev_pindex + prev_size;
17658cc7e047SJohn Dyson 
17668cc7e047SJohn Dyson 	if ((prev_object->ref_count > 1) &&
1767ea41812fSAlan Cox 	    (prev_object->size != next_pindex)) {
176815c176c1SAlan Cox 		vm_object_unlock(prev_object);
17698cc7e047SJohn Dyson 		return (FALSE);
17708cc7e047SJohn Dyson 	}
17718cc7e047SJohn Dyson 
1772df8bae1dSRodney W. Grimes 	/*
17730d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
17740d94caffSDavid Greenman 	 * deallocation.
1775df8bae1dSRodney W. Grimes 	 */
1776ea41812fSAlan Cox 	if (next_pindex < prev_object->size) {
1777df8bae1dSRodney W. Grimes 		vm_object_page_remove(prev_object,
1778ea41812fSAlan Cox 				      next_pindex,
1779ea41812fSAlan Cox 				      next_pindex + next_size, FALSE);
1780ea41812fSAlan Cox 		if (prev_object->type == OBJT_SWAP)
1781ea41812fSAlan Cox 			swap_pager_freespace(prev_object,
1782ea41812fSAlan Cox 					     next_pindex, next_size);
1783ea41812fSAlan Cox 	}
1784df8bae1dSRodney W. Grimes 
1785df8bae1dSRodney W. Grimes 	/*
1786df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
1787df8bae1dSRodney W. Grimes 	 */
1788ea41812fSAlan Cox 	if (next_pindex + next_size > prev_object->size)
1789ea41812fSAlan Cox 		prev_object->size = next_pindex + next_size;
1790df8bae1dSRodney W. Grimes 
179115c176c1SAlan Cox 	vm_object_unlock(prev_object);
1792df8bae1dSRodney W. Grimes 	return (TRUE);
1793df8bae1dSRodney W. Grimes }
1794df8bae1dSRodney W. Grimes 
17957a5a6352SMatthew Dillon void
17967a5a6352SMatthew Dillon vm_object_set_writeable_dirty(vm_object_t object)
17977a5a6352SMatthew Dillon {
17987a5a6352SMatthew Dillon 	struct vnode *vp;
17997a5a6352SMatthew Dillon 
18007a5a6352SMatthew Dillon 	vm_object_set_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
18017a5a6352SMatthew Dillon 	if (object->type == OBJT_VNODE &&
18027a5a6352SMatthew Dillon 	    (vp = (struct vnode *)object->handle) != NULL) {
1803e6e370a7SJeff Roberson 		VI_LOCK(vp);
1804e6e370a7SJeff Roberson 		if ((vp->v_iflag & VI_OBJDIRTY) == 0)
1805e6e370a7SJeff Roberson 			vp->v_iflag |= VI_OBJDIRTY;
1806e6e370a7SJeff Roberson 		VI_UNLOCK(vp);
18077a5a6352SMatthew Dillon 	}
18087a5a6352SMatthew Dillon }
18097a5a6352SMatthew Dillon 
1810c7c34a24SBruce Evans #include "opt_ddb.h"
1811c3cb3e12SDavid Greenman #ifdef DDB
1812c7c34a24SBruce Evans #include <sys/kernel.h>
1813c7c34a24SBruce Evans 
1814ce9edcf5SPoul-Henning Kamp #include <sys/cons.h>
1815c7c34a24SBruce Evans 
1816c7c34a24SBruce Evans #include <ddb/ddb.h>
1817c7c34a24SBruce Evans 
1818cac597e4SBruce Evans static int
18191b40f8c0SMatthew Dillon _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
1820a1f6d91cSDavid Greenman {
1821a1f6d91cSDavid Greenman 	vm_map_t tmpm;
1822a1f6d91cSDavid Greenman 	vm_map_entry_t tmpe;
1823a1f6d91cSDavid Greenman 	vm_object_t obj;
1824a1f6d91cSDavid Greenman 	int entcount;
1825a1f6d91cSDavid Greenman 
1826a1f6d91cSDavid Greenman 	if (map == 0)
1827a1f6d91cSDavid Greenman 		return 0;
1828a1f6d91cSDavid Greenman 
1829a1f6d91cSDavid Greenman 	if (entry == 0) {
1830a1f6d91cSDavid Greenman 		tmpe = map->header.next;
1831a1f6d91cSDavid Greenman 		entcount = map->nentries;
1832a1f6d91cSDavid Greenman 		while (entcount-- && (tmpe != &map->header)) {
1833a1f6d91cSDavid Greenman 			if (_vm_object_in_map(map, object, tmpe)) {
1834a1f6d91cSDavid Greenman 				return 1;
1835a1f6d91cSDavid Greenman 			}
1836a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1837a1f6d91cSDavid Greenman 		}
18389fdfe602SMatthew Dillon 	} else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
18399fdfe602SMatthew Dillon 		tmpm = entry->object.sub_map;
1840a1f6d91cSDavid Greenman 		tmpe = tmpm->header.next;
1841a1f6d91cSDavid Greenman 		entcount = tmpm->nentries;
1842a1f6d91cSDavid Greenman 		while (entcount-- && tmpe != &tmpm->header) {
1843a1f6d91cSDavid Greenman 			if (_vm_object_in_map(tmpm, object, tmpe)) {
1844a1f6d91cSDavid Greenman 				return 1;
1845a1f6d91cSDavid Greenman 			}
1846a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1847a1f6d91cSDavid Greenman 		}
18488aef1712SMatthew Dillon 	} else if ((obj = entry->object.vm_object) != NULL) {
184924a1cce3SDavid Greenman 		for (; obj; obj = obj->backing_object)
1850a1f6d91cSDavid Greenman 			if (obj == object) {
1851a1f6d91cSDavid Greenman 				return 1;
1852a1f6d91cSDavid Greenman 			}
1853a1f6d91cSDavid Greenman 	}
1854a1f6d91cSDavid Greenman 	return 0;
1855a1f6d91cSDavid Greenman }
1856a1f6d91cSDavid Greenman 
1857cac597e4SBruce Evans static int
18581b40f8c0SMatthew Dillon vm_object_in_map(vm_object_t object)
1859a1f6d91cSDavid Greenman {
1860a1f6d91cSDavid Greenman 	struct proc *p;
18611005a129SJohn Baldwin 
186260517fd1SJohn Baldwin 	/* sx_slock(&allproc_lock); */
1863fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(p, &allproc, p_list) {
1864a1f6d91cSDavid Greenman 		if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1865a1f6d91cSDavid Greenman 			continue;
1866553629ebSJake Burkholder 		if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
186760517fd1SJohn Baldwin 			/* sx_sunlock(&allproc_lock); */
1868a1f6d91cSDavid Greenman 			return 1;
1869a1f6d91cSDavid Greenman 		}
1870553629ebSJake Burkholder 	}
187160517fd1SJohn Baldwin 	/* sx_sunlock(&allproc_lock); */
1872a1f6d91cSDavid Greenman 	if (_vm_object_in_map(kernel_map, object, 0))
1873a1f6d91cSDavid Greenman 		return 1;
1874a1f6d91cSDavid Greenman 	if (_vm_object_in_map(kmem_map, object, 0))
1875a1f6d91cSDavid Greenman 		return 1;
1876a1f6d91cSDavid Greenman 	if (_vm_object_in_map(pager_map, object, 0))
1877a1f6d91cSDavid Greenman 		return 1;
1878a1f6d91cSDavid Greenman 	if (_vm_object_in_map(buffer_map, object, 0))
1879a1f6d91cSDavid Greenman 		return 1;
1880a1f6d91cSDavid Greenman 	return 0;
1881a1f6d91cSDavid Greenman }
1882a1f6d91cSDavid Greenman 
1883c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check)
1884f708ef1bSPoul-Henning Kamp {
1885a1f6d91cSDavid Greenman 	vm_object_t object;
1886a1f6d91cSDavid Greenman 
1887a1f6d91cSDavid Greenman 	/*
1888a1f6d91cSDavid Greenman 	 * make sure that internal objs are in a map somewhere
1889a1f6d91cSDavid Greenman 	 * and none have zero ref counts.
1890a1f6d91cSDavid Greenman 	 */
1891cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
189224a1cce3SDavid Greenman 		if (object->handle == NULL &&
189324a1cce3SDavid Greenman 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
1894a1f6d91cSDavid Greenman 			if (object->ref_count == 0) {
18953efc015bSPeter Wemm 				db_printf("vmochk: internal obj has zero ref count: %ld\n",
18963efc015bSPeter Wemm 					(long)object->size);
1897a1f6d91cSDavid Greenman 			}
1898a1f6d91cSDavid Greenman 			if (!vm_object_in_map(object)) {
1899fc62ef1fSBruce Evans 				db_printf(
1900fc62ef1fSBruce Evans 			"vmochk: internal obj is not in a map: "
1901fc62ef1fSBruce Evans 			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
1902fc62ef1fSBruce Evans 				    object->ref_count, (u_long)object->size,
1903fc62ef1fSBruce Evans 				    (u_long)object->size,
1904fc62ef1fSBruce Evans 				    (void *)object->backing_object);
1905a1f6d91cSDavid Greenman 			}
1906a1f6d91cSDavid Greenman 		}
1907a1f6d91cSDavid Greenman 	}
1908a1f6d91cSDavid Greenman }
1909a1f6d91cSDavid Greenman 
191026f9a767SRodney W. Grimes /*
1911df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
1912df8bae1dSRodney W. Grimes  */
1913c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static)
1914df8bae1dSRodney W. Grimes {
1915c7c34a24SBruce Evans 	/* XXX convert args. */
1916c7c34a24SBruce Evans 	vm_object_t object = (vm_object_t)addr;
1917c7c34a24SBruce Evans 	boolean_t full = have_addr;
1918c7c34a24SBruce Evans 
1919d031cff1SMatthew Dillon 	vm_page_t p;
1920df8bae1dSRodney W. Grimes 
1921c7c34a24SBruce Evans 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
1922c7c34a24SBruce Evans #define	count	was_count
1923c7c34a24SBruce Evans 
1924d031cff1SMatthew Dillon 	int count;
1925df8bae1dSRodney W. Grimes 
1926df8bae1dSRodney W. Grimes 	if (object == NULL)
1927df8bae1dSRodney W. Grimes 		return;
1928df8bae1dSRodney W. Grimes 
1929eb95adefSBruce Evans 	db_iprintf(
1930e47cd172SMaxime Henrion 	    "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x\n",
1931e47cd172SMaxime Henrion 	    object, (int)object->type, (uintmax_t)object->size,
1932eb95adefSBruce Evans 	    object->resident_page_count, object->ref_count, object->flags);
1933e47cd172SMaxime Henrion 	db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n",
19341c7c3c6aSMatthew Dillon 	    object->shadow_count,
1935eb95adefSBruce Evans 	    object->backing_object ? object->backing_object->ref_count : 0,
1936e47cd172SMaxime Henrion 	    object->backing_object, (uintmax_t)object->backing_object_offset);
1937df8bae1dSRodney W. Grimes 
1938df8bae1dSRodney W. Grimes 	if (!full)
1939df8bae1dSRodney W. Grimes 		return;
1940df8bae1dSRodney W. Grimes 
1941c7c34a24SBruce Evans 	db_indent += 2;
1942df8bae1dSRodney W. Grimes 	count = 0;
1943fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(p, &object->memq, listq) {
1944df8bae1dSRodney W. Grimes 		if (count == 0)
1945c7c34a24SBruce Evans 			db_iprintf("memory:=");
1946df8bae1dSRodney W. Grimes 		else if (count == 6) {
1947c7c34a24SBruce Evans 			db_printf("\n");
1948c7c34a24SBruce Evans 			db_iprintf(" ...");
1949df8bae1dSRodney W. Grimes 			count = 0;
1950df8bae1dSRodney W. Grimes 		} else
1951c7c34a24SBruce Evans 			db_printf(",");
1952df8bae1dSRodney W. Grimes 		count++;
1953df8bae1dSRodney W. Grimes 
1954e47cd172SMaxime Henrion 		db_printf("(off=0x%jx,page=0x%jx)",
1955e47cd172SMaxime Henrion 		    (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p));
1956df8bae1dSRodney W. Grimes 	}
1957df8bae1dSRodney W. Grimes 	if (count != 0)
1958c7c34a24SBruce Evans 		db_printf("\n");
1959c7c34a24SBruce Evans 	db_indent -= 2;
1960df8bae1dSRodney W. Grimes }
19615070c7f8SJohn Dyson 
1962c7c34a24SBruce Evans /* XXX. */
1963c7c34a24SBruce Evans #undef count
1964c7c34a24SBruce Evans 
1965c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */
19665070c7f8SJohn Dyson void
19671b40f8c0SMatthew Dillon vm_object_print(
19681b40f8c0SMatthew Dillon         /* db_expr_t */ long addr,
19691b40f8c0SMatthew Dillon 	boolean_t have_addr,
19701b40f8c0SMatthew Dillon 	/* db_expr_t */ long count,
19711b40f8c0SMatthew Dillon 	char *modif)
1972c7c34a24SBruce Evans {
1973c7c34a24SBruce Evans 	vm_object_print_static(addr, have_addr, count, modif);
1974c7c34a24SBruce Evans }
1975c7c34a24SBruce Evans 
1976c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
19775070c7f8SJohn Dyson {
19785070c7f8SJohn Dyson 	vm_object_t object;
19795070c7f8SJohn Dyson 	int nl = 0;
19805070c7f8SJohn Dyson 	int c;
1981cc64b484SAlfred Perlstein 
1982cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
19835070c7f8SJohn Dyson 		vm_pindex_t idx, fidx;
19845070c7f8SJohn Dyson 		vm_pindex_t osize;
1985227f9a1cSJake Burkholder 		vm_paddr_t pa = -1, padiff;
19865070c7f8SJohn Dyson 		int rcount;
19875070c7f8SJohn Dyson 		vm_page_t m;
19885070c7f8SJohn Dyson 
1989fc62ef1fSBruce Evans 		db_printf("new object: %p\n", (void *)object);
19905070c7f8SJohn Dyson 		if (nl > 18) {
19915070c7f8SJohn Dyson 			c = cngetc();
19925070c7f8SJohn Dyson 			if (c != ' ')
19935070c7f8SJohn Dyson 				return;
19945070c7f8SJohn Dyson 			nl = 0;
19955070c7f8SJohn Dyson 		}
19965070c7f8SJohn Dyson 		nl++;
19975070c7f8SJohn Dyson 		rcount = 0;
19985070c7f8SJohn Dyson 		fidx = 0;
19995070c7f8SJohn Dyson 		osize = object->size;
20005070c7f8SJohn Dyson 		if (osize > 128)
20015070c7f8SJohn Dyson 			osize = 128;
20025070c7f8SJohn Dyson 		for (idx = 0; idx < osize; idx++) {
20035070c7f8SJohn Dyson 			m = vm_page_lookup(object, idx);
20045070c7f8SJohn Dyson 			if (m == NULL) {
20055070c7f8SJohn Dyson 				if (rcount) {
20063efc015bSPeter Wemm 					db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
20073efc015bSPeter Wemm 						(long)fidx, rcount, (long)pa);
20085070c7f8SJohn Dyson 					if (nl > 18) {
20095070c7f8SJohn Dyson 						c = cngetc();
20105070c7f8SJohn Dyson 						if (c != ' ')
20115070c7f8SJohn Dyson 							return;
20125070c7f8SJohn Dyson 						nl = 0;
20135070c7f8SJohn Dyson 					}
20145070c7f8SJohn Dyson 					nl++;
20155070c7f8SJohn Dyson 					rcount = 0;
20165070c7f8SJohn Dyson 				}
20175070c7f8SJohn Dyson 				continue;
20185070c7f8SJohn Dyson 			}
20195070c7f8SJohn Dyson 
20205070c7f8SJohn Dyson 
20215070c7f8SJohn Dyson 			if (rcount &&
20225070c7f8SJohn Dyson 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
20235070c7f8SJohn Dyson 				++rcount;
20245070c7f8SJohn Dyson 				continue;
20255070c7f8SJohn Dyson 			}
20265070c7f8SJohn Dyson 			if (rcount) {
20275070c7f8SJohn Dyson 				padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
20285070c7f8SJohn Dyson 				padiff >>= PAGE_SHIFT;
20295070c7f8SJohn Dyson 				padiff &= PQ_L2_MASK;
20305070c7f8SJohn Dyson 				if (padiff == 0) {
20315070c7f8SJohn Dyson 					pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
20325070c7f8SJohn Dyson 					++rcount;
20335070c7f8SJohn Dyson 					continue;
20345070c7f8SJohn Dyson 				}
20353efc015bSPeter Wemm 				db_printf(" index(%ld)run(%d)pa(0x%lx)",
20363efc015bSPeter Wemm 					(long)fidx, rcount, (long)pa);
20373efc015bSPeter Wemm 				db_printf("pd(%ld)\n", (long)padiff);
20385070c7f8SJohn Dyson 				if (nl > 18) {
20395070c7f8SJohn Dyson 					c = cngetc();
20405070c7f8SJohn Dyson 					if (c != ' ')
20415070c7f8SJohn Dyson 						return;
20425070c7f8SJohn Dyson 					nl = 0;
20435070c7f8SJohn Dyson 				}
20445070c7f8SJohn Dyson 				nl++;
20455070c7f8SJohn Dyson 			}
20465070c7f8SJohn Dyson 			fidx = idx;
20475070c7f8SJohn Dyson 			pa = VM_PAGE_TO_PHYS(m);
20485070c7f8SJohn Dyson 			rcount = 1;
20495070c7f8SJohn Dyson 		}
20505070c7f8SJohn Dyson 		if (rcount) {
20513efc015bSPeter Wemm 			db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
20523efc015bSPeter Wemm 				(long)fidx, rcount, (long)pa);
20535070c7f8SJohn Dyson 			if (nl > 18) {
20545070c7f8SJohn Dyson 				c = cngetc();
20555070c7f8SJohn Dyson 				if (c != ' ')
20565070c7f8SJohn Dyson 					return;
20575070c7f8SJohn Dyson 				nl = 0;
20585070c7f8SJohn Dyson 			}
20595070c7f8SJohn Dyson 			nl++;
20605070c7f8SJohn Dyson 		}
20615070c7f8SJohn Dyson 	}
20625070c7f8SJohn Dyson }
2063c3cb3e12SDavid Greenman #endif /* DDB */
2064