xref: /freebsd/sys/vm/vm_object.c (revision cccf11b8659f90f2d3e6df8331fe34f28e347c71)
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);
1121ca58953SAlan Cox static void	vm_object_pip_sleep(vm_object_t object, char *waitid);
113f6b04d2bSDavid Greenman 
114df8bae1dSRodney W. Grimes /*
115df8bae1dSRodney W. Grimes  *	Virtual memory objects maintain the actual data
116df8bae1dSRodney W. Grimes  *	associated with allocated virtual memory.  A given
117df8bae1dSRodney W. Grimes  *	page of memory exists within exactly one object.
118df8bae1dSRodney W. Grimes  *
119df8bae1dSRodney W. Grimes  *	An object is only deallocated when all "references"
120df8bae1dSRodney W. Grimes  *	are given up.  Only one "reference" to a given
121df8bae1dSRodney W. Grimes  *	region of an object should be writeable.
122df8bae1dSRodney W. Grimes  *
123df8bae1dSRodney W. Grimes  *	Associated with each object is a list of all resident
124df8bae1dSRodney W. Grimes  *	memory pages belonging to that object; this list is
125df8bae1dSRodney W. Grimes  *	maintained by the "vm_page" module, and locked by the object's
126df8bae1dSRodney W. Grimes  *	lock.
127df8bae1dSRodney W. Grimes  *
128df8bae1dSRodney W. Grimes  *	Each object also records a "pager" routine which is
129df8bae1dSRodney W. Grimes  *	used to retrieve (and store) pages to the proper backing
130df8bae1dSRodney W. Grimes  *	storage.  In addition, objects may be backed by other
131df8bae1dSRodney W. Grimes  *	objects from which they were virtual-copied.
132df8bae1dSRodney W. Grimes  *
133df8bae1dSRodney W. Grimes  *	The only items within the object structure which are
134df8bae1dSRodney W. Grimes  *	modified after time of creation are:
135df8bae1dSRodney W. Grimes  *		reference count		locked by object's lock
136df8bae1dSRodney W. Grimes  *		pager routine		locked by object's lock
137df8bae1dSRodney W. Grimes  *
138df8bae1dSRodney W. Grimes  */
139df8bae1dSRodney W. Grimes 
14028f8db14SBruce Evans struct object_q vm_object_list;
141a5698387SAlan Cox struct mtx vm_object_list_mtx;	/* lock for object list and count */
142cccf11b8SAlan Cox 
143cccf11b8SAlan Cox struct vm_object kernel_object_store;
144cccf11b8SAlan Cox struct vm_object kmem_object_store;
145df8bae1dSRodney W. Grimes 
146f708ef1bSPoul-Henning Kamp static long object_collapses;
147f708ef1bSPoul-Henning Kamp static long object_bypasses;
148dcc5840eSAlan Cox static int next_index;
149670d17b5SJeff Roberson static uma_zone_t obj_zone;
15099448ed1SJohn Dyson #define VM_OBJECTS_INIT 256
1518355f576SJeff Roberson 
1528355f576SJeff Roberson static void vm_object_zinit(void *mem, int size);
1538355f576SJeff Roberson 
1548355f576SJeff Roberson #ifdef INVARIANTS
1558355f576SJeff Roberson static void vm_object_zdtor(void *mem, int size, void *arg);
1568355f576SJeff Roberson 
1578355f576SJeff Roberson static void
1588355f576SJeff Roberson vm_object_zdtor(void *mem, int size, void *arg)
1598355f576SJeff Roberson {
1608355f576SJeff Roberson 	vm_object_t object;
1618355f576SJeff Roberson 
1628355f576SJeff Roberson 	object = (vm_object_t)mem;
1638355f576SJeff Roberson 	KASSERT(object->paging_in_progress == 0,
1648355f576SJeff Roberson 	    ("object %p paging_in_progress = %d",
1658355f576SJeff Roberson 	    object, object->paging_in_progress));
1668355f576SJeff Roberson 	KASSERT(object->resident_page_count == 0,
1678355f576SJeff Roberson 	    ("object %p resident_page_count = %d",
1688355f576SJeff Roberson 	    object, object->resident_page_count));
1698355f576SJeff Roberson 	KASSERT(object->shadow_count == 0,
1708355f576SJeff Roberson 	    ("object %p shadow_count = %d",
1718355f576SJeff Roberson 	    object, object->shadow_count));
1728355f576SJeff Roberson }
1738355f576SJeff Roberson #endif
1748355f576SJeff Roberson 
1758355f576SJeff Roberson static void
1768355f576SJeff Roberson vm_object_zinit(void *mem, int size)
1778355f576SJeff Roberson {
1788355f576SJeff Roberson 	vm_object_t object;
1798355f576SJeff Roberson 
1808355f576SJeff Roberson 	object = (vm_object_t)mem;
181e3a9e1b2SAlan Cox 	bzero(&object->mtx, sizeof(object->mtx));
182ed6a7863SAlan Cox 	VM_OBJECT_LOCK_INIT(object);
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 
195df8bae1dSRodney W. Grimes 	TAILQ_INIT(&object->memq);
1961c500307SAlan Cox 	LIST_INIT(&object->shadow_head);
197a1f6d91cSDavid Greenman 
198b86ec922SMatthew Dillon 	object->root = NULL;
19924a1cce3SDavid Greenman 	object->type = type;
200df8bae1dSRodney W. Grimes 	object->size = size;
201a1f6d91cSDavid Greenman 	object->ref_count = 1;
20224a1cce3SDavid Greenman 	object->flags = 0;
20360517fd1SJohn Baldwin 	if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
20460517fd1SJohn Baldwin 		vm_object_set_flag(object, OBJ_ONEMAPPING);
20599448ed1SJohn Dyson 	if (size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
20699448ed1SJohn Dyson 		incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
20799448ed1SJohn Dyson 	else
20899448ed1SJohn Dyson 		incr = size;
209dcc5840eSAlan Cox 	do
210dcc5840eSAlan Cox 		object->pg_color = next_index;
211dcc5840eSAlan Cox 	while (!atomic_cmpset_int(&next_index, object->pg_color,
21279660d83SAlan Cox 				  (object->pg_color + incr) & PQ_L2_MASK));
21324a1cce3SDavid Greenman 	object->handle = NULL;
21424a1cce3SDavid Greenman 	object->backing_object = NULL;
215a316d390SJohn Dyson 	object->backing_object_offset = (vm_ooffset_t) 0;
216a1f6d91cSDavid Greenman 
21781f71edaSMatt Jacob 	atomic_add_int(&object->generation, 1);
21860517fd1SJohn Baldwin 
219a5698387SAlan Cox 	mtx_lock(&vm_object_list_mtx);
22060517fd1SJohn Baldwin 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
221a5698387SAlan Cox 	mtx_unlock(&vm_object_list_mtx);
222df8bae1dSRodney W. Grimes }
223df8bae1dSRodney W. Grimes 
224df8bae1dSRodney W. Grimes /*
22526f9a767SRodney W. Grimes  *	vm_object_init:
22626f9a767SRodney W. Grimes  *
22726f9a767SRodney W. Grimes  *	Initialize the VM objects module.
22826f9a767SRodney W. Grimes  */
22926f9a767SRodney W. Grimes void
2301b40f8c0SMatthew Dillon vm_object_init(void)
23126f9a767SRodney W. Grimes {
23226f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_list);
2336008862bSJohn Baldwin 	mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF);
2340217125fSDavid Greenman 
235ed6a7863SAlan Cox 	VM_OBJECT_LOCK_INIT(&kernel_object_store);
236a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
23726f9a767SRodney W. Grimes 	    kernel_object);
23826f9a767SRodney W. Grimes 
2393a12f5daSAlan Cox 	/*
2403a12f5daSAlan Cox 	 * The kmem object's mutex is given a unique name, instead of
2413a12f5daSAlan Cox 	 * "vm object", to avoid false reports of lock-order reversal
2423a12f5daSAlan Cox 	 * with a system map mutex.
2433a12f5daSAlan Cox 	 */
2443a12f5daSAlan Cox 	mtx_init(VM_OBJECT_MTX(kmem_object), "kmem object", NULL, MTX_DEF);
245a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
24626f9a767SRodney W. Grimes 	    kmem_object);
247ed6a7863SAlan Cox 
2488355f576SJeff Roberson 	obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL,
2498355f576SJeff Roberson #ifdef INVARIANTS
2508355f576SJeff Roberson 	    vm_object_zdtor,
2518355f576SJeff Roberson #else
2528355f576SJeff Roberson 	    NULL,
2538355f576SJeff Roberson #endif
2548355f576SJeff Roberson 	    vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
2558355f576SJeff Roberson 	uma_prealloc(obj_zone, VM_OBJECTS_INIT);
25699448ed1SJohn Dyson }
25799448ed1SJohn Dyson 
25899448ed1SJohn Dyson void
2591b40f8c0SMatthew Dillon vm_object_set_flag(vm_object_t object, u_short bits)
2601b40f8c0SMatthew Dillon {
261b06805adSJake Burkholder 	object->flags |= bits;
2621b40f8c0SMatthew Dillon }
2631b40f8c0SMatthew Dillon 
2641b40f8c0SMatthew Dillon void
2651b40f8c0SMatthew Dillon vm_object_clear_flag(vm_object_t object, u_short bits)
2661b40f8c0SMatthew Dillon {
2675440b5a9SAlan Cox 
268d647a0edSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
269b06805adSJake Burkholder 	object->flags &= ~bits;
2701b40f8c0SMatthew Dillon }
2711b40f8c0SMatthew Dillon 
2721b40f8c0SMatthew Dillon void
2731b40f8c0SMatthew Dillon vm_object_pip_add(vm_object_t object, short i)
2741b40f8c0SMatthew Dillon {
275f279b88dSAlan Cox 
276d647a0edSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
277b06805adSJake Burkholder 	object->paging_in_progress += i;
2781b40f8c0SMatthew Dillon }
2791b40f8c0SMatthew Dillon 
2801b40f8c0SMatthew Dillon void
2811b40f8c0SMatthew Dillon vm_object_pip_subtract(vm_object_t object, short i)
2821b40f8c0SMatthew Dillon {
283d647a0edSAlan Cox 
2840fa05eaeSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
285b06805adSJake Burkholder 	object->paging_in_progress -= i;
2861b40f8c0SMatthew Dillon }
2871b40f8c0SMatthew Dillon 
2881b40f8c0SMatthew Dillon void
2891b40f8c0SMatthew Dillon vm_object_pip_wakeup(vm_object_t object)
2901b40f8c0SMatthew Dillon {
291f279b88dSAlan Cox 
292d647a0edSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
293b06805adSJake Burkholder 	object->paging_in_progress--;
2941b40f8c0SMatthew Dillon 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
2951b40f8c0SMatthew Dillon 		vm_object_clear_flag(object, OBJ_PIPWNT);
2961b40f8c0SMatthew Dillon 		wakeup(object);
2971b40f8c0SMatthew Dillon 	}
2981b40f8c0SMatthew Dillon }
2991b40f8c0SMatthew Dillon 
3001b40f8c0SMatthew Dillon void
3011b40f8c0SMatthew Dillon vm_object_pip_wakeupn(vm_object_t object, short i)
3021b40f8c0SMatthew Dillon {
303d647a0edSAlan Cox 
3040d420ad3SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
3051b40f8c0SMatthew Dillon 	if (i)
306b06805adSJake Burkholder 		object->paging_in_progress -= i;
3071b40f8c0SMatthew Dillon 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
3081b40f8c0SMatthew Dillon 		vm_object_clear_flag(object, OBJ_PIPWNT);
3091b40f8c0SMatthew Dillon 		wakeup(object);
3101b40f8c0SMatthew Dillon 	}
3111b40f8c0SMatthew Dillon }
3121b40f8c0SMatthew Dillon 
3131ca58953SAlan Cox static void
3141b40f8c0SMatthew Dillon vm_object_pip_sleep(vm_object_t object, char *waitid)
3151b40f8c0SMatthew Dillon {
3161b40f8c0SMatthew Dillon 	GIANT_REQUIRED;
3171b40f8c0SMatthew Dillon 	if (object->paging_in_progress) {
3181b40f8c0SMatthew Dillon 		int s = splvm();
3191b40f8c0SMatthew Dillon 		if (object->paging_in_progress) {
3201b40f8c0SMatthew Dillon 			vm_object_set_flag(object, OBJ_PIPWNT);
3211b40f8c0SMatthew Dillon 			tsleep(object, PVM, waitid, 0);
3221b40f8c0SMatthew Dillon 		}
3231b40f8c0SMatthew Dillon 		splx(s);
3241b40f8c0SMatthew Dillon 	}
3251b40f8c0SMatthew Dillon }
3261b40f8c0SMatthew Dillon 
3271b40f8c0SMatthew Dillon void
3281b40f8c0SMatthew Dillon vm_object_pip_wait(vm_object_t object, char *waitid)
3291b40f8c0SMatthew Dillon {
3301ca58953SAlan Cox 
3311ca58953SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
3321ca58953SAlan Cox 	while (object->paging_in_progress) {
3331ca58953SAlan Cox 		object->flags |= OBJ_PIPWNT;
3341ca58953SAlan Cox 		msleep(object, VM_OBJECT_MTX(object), PVM, waitid, 0);
3351ca58953SAlan Cox 	}
3361b40f8c0SMatthew Dillon }
3371b40f8c0SMatthew Dillon 
33826f9a767SRodney W. Grimes /*
33998cb733cSKenneth D. Merry  *	vm_object_allocate_wait
34098cb733cSKenneth D. Merry  *
34198cb733cSKenneth D. Merry  *	Return a new object with the given size, and give the user the
34298cb733cSKenneth D. Merry  *	option of waiting for it to complete or failing if the needed
34398cb733cSKenneth D. Merry  *	memory isn't available.
34498cb733cSKenneth D. Merry  */
34598cb733cSKenneth D. Merry vm_object_t
34698cb733cSKenneth D. Merry vm_object_allocate_wait(objtype_t type, vm_pindex_t size, int flags)
34798cb733cSKenneth D. Merry {
34898cb733cSKenneth D. Merry 	vm_object_t result;
34998cb733cSKenneth D. Merry 
35098cb733cSKenneth D. Merry 	result = (vm_object_t) uma_zalloc(obj_zone, flags);
35198cb733cSKenneth D. Merry 
35298cb733cSKenneth D. Merry 	if (result != NULL)
35398cb733cSKenneth D. Merry 		_vm_object_allocate(type, size, result);
35498cb733cSKenneth D. Merry 
35598cb733cSKenneth D. Merry 	return (result);
35698cb733cSKenneth D. Merry }
35798cb733cSKenneth D. Merry 
35898cb733cSKenneth D. Merry /*
35926f9a767SRodney W. Grimes  *	vm_object_allocate:
36026f9a767SRodney W. Grimes  *
36126f9a767SRodney W. Grimes  *	Returns a new object with the given size.
36226f9a767SRodney W. Grimes  */
36326f9a767SRodney W. Grimes vm_object_t
3646395da54SIan Dowse vm_object_allocate(objtype_t type, vm_pindex_t size)
36526f9a767SRodney W. Grimes {
366a163d034SWarner Losh 	return(vm_object_allocate_wait(type, size, M_WAITOK));
36726f9a767SRodney W. Grimes }
36826f9a767SRodney W. Grimes 
36926f9a767SRodney W. Grimes 
37026f9a767SRodney W. Grimes /*
371df8bae1dSRodney W. Grimes  *	vm_object_reference:
372df8bae1dSRodney W. Grimes  *
37315347817SAlan Cox  *	Gets another reference to the given object.  Note: OBJ_DEAD
37415347817SAlan Cox  *	objects can be referenced during final cleaning.
375df8bae1dSRodney W. Grimes  */
3766476c0d2SJohn Dyson void
3771b40f8c0SMatthew Dillon vm_object_reference(vm_object_t object)
378df8bae1dSRodney W. Grimes {
379df8bae1dSRodney W. Grimes 	if (object == NULL)
380df8bae1dSRodney W. Grimes 		return;
38115347817SAlan Cox 	if (object != kmem_object)
38215347817SAlan Cox 		mtx_lock(&Giant);
38315347817SAlan Cox 	VM_OBJECT_LOCK(object);
384df8bae1dSRodney W. Grimes 	object->ref_count++;
38515347817SAlan Cox 	VM_OBJECT_UNLOCK(object);
38647221757SJohn Dyson 	if (object->type == OBJT_VNODE) {
3873ef3e7c4SJeff Roberson 		while (vget((struct vnode *) object->handle, LK_RETRY, curthread)) {
38847221757SJohn Dyson 			printf("vm_object_reference: delay in getting object\n");
38947221757SJohn Dyson 		}
39047221757SJohn Dyson 	}
39115347817SAlan Cox 	if (object != kmem_object)
39215347817SAlan Cox 		mtx_unlock(&Giant);
39395e5e988SJohn Dyson }
39495e5e988SJohn Dyson 
39523955314SAlfred Perlstein /*
3969d5abbddSJens Schweikhardt  * Handle deallocating an object of type OBJT_VNODE.
39723955314SAlfred Perlstein  */
398bf27292bSJohn Dyson void
3991b40f8c0SMatthew Dillon vm_object_vndeallocate(vm_object_t object)
40095e5e988SJohn Dyson {
40195e5e988SJohn Dyson 	struct vnode *vp = (struct vnode *) object->handle;
402219cbf59SEivind Eklund 
4030cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
404ad682c48SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
4055526d2d9SEivind Eklund 	KASSERT(object->type == OBJT_VNODE,
4065526d2d9SEivind Eklund 	    ("vm_object_vndeallocate: not a vnode object"));
407219cbf59SEivind Eklund 	KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
408219cbf59SEivind Eklund #ifdef INVARIANTS
40995e5e988SJohn Dyson 	if (object->ref_count == 0) {
41095e5e988SJohn Dyson 		vprint("vm_object_vndeallocate", vp);
41195e5e988SJohn Dyson 		panic("vm_object_vndeallocate: bad object reference count");
41295e5e988SJohn Dyson 	}
41395e5e988SJohn Dyson #endif
41495e5e988SJohn Dyson 
41595e5e988SJohn Dyson 	object->ref_count--;
41647221757SJohn Dyson 	if (object->ref_count == 0) {
417e6e370a7SJeff Roberson 		mp_fixme("Unlocked vflag access.");
418e6e370a7SJeff Roberson 		vp->v_vflag &= ~VV_TEXT;
4192be70f79SJohn Dyson 	}
420ad682c48SAlan Cox 	VM_OBJECT_UNLOCK(object);
42123955314SAlfred Perlstein 	/*
42223955314SAlfred Perlstein 	 * vrele may need a vop lock
42323955314SAlfred Perlstein 	 */
42447221757SJohn Dyson 	vrele(vp);
425df8bae1dSRodney W. Grimes }
426df8bae1dSRodney W. Grimes 
427df8bae1dSRodney W. Grimes /*
428df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
429df8bae1dSRodney W. Grimes  *
430df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
431df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
432df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
433df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
434df8bae1dSRodney W. Grimes  *	may be relinquished.
435df8bae1dSRodney W. Grimes  *
436df8bae1dSRodney W. Grimes  *	No object may be locked.
437df8bae1dSRodney W. Grimes  */
43826f9a767SRodney W. Grimes void
4391b40f8c0SMatthew Dillon vm_object_deallocate(vm_object_t object)
440df8bae1dSRodney W. Grimes {
441df8bae1dSRodney W. Grimes 	vm_object_t temp;
442df8bae1dSRodney W. Grimes 
4434dbeceeeSAlan Cox 	vm_object_lock(object);
444df8bae1dSRodney W. Grimes 	while (object != NULL) {
445df8bae1dSRodney W. Grimes 
44695e5e988SJohn Dyson 		if (object->type == OBJT_VNODE) {
447ad682c48SAlan Cox 			VM_OBJECT_LOCK(object);
44895e5e988SJohn Dyson 			vm_object_vndeallocate(object);
449ad682c48SAlan Cox 			mtx_unlock(&Giant);
45095e5e988SJohn Dyson 			return;
45195e5e988SJohn Dyson 		}
45295e5e988SJohn Dyson 
4538125b1e6SAlfred Perlstein 		KASSERT(object->ref_count != 0,
4548125b1e6SAlfred Perlstein 			("vm_object_deallocate: object deallocated too many times: %d", object->type));
4552be70f79SJohn Dyson 
4562be70f79SJohn Dyson 		/*
4578125b1e6SAlfred Perlstein 		 * If the reference count goes to 0 we start calling
4588125b1e6SAlfred Perlstein 		 * vm_object_terminate() on the object chain.
4598125b1e6SAlfred Perlstein 		 * A ref count of 1 may be a special case depending on the
4608125b1e6SAlfred Perlstein 		 * shadow count being 0 or 1.
4612be70f79SJohn Dyson 		 */
462c0877f10SJohn Dyson 		object->ref_count--;
4638125b1e6SAlfred Perlstein 		if (object->ref_count > 1) {
4644dbeceeeSAlan Cox 			vm_object_unlock(object);
465c0877f10SJohn Dyson 			return;
4668125b1e6SAlfred Perlstein 		} else if (object->ref_count == 1) {
4678125b1e6SAlfred Perlstein 			if (object->shadow_count == 0) {
4688125b1e6SAlfred Perlstein 				vm_object_set_flag(object, OBJ_ONEMAPPING);
4698125b1e6SAlfred Perlstein 			} else if ((object->shadow_count == 1) &&
4708125b1e6SAlfred Perlstein 			    (object->handle == NULL) &&
47124a1cce3SDavid Greenman 			    (object->type == OBJT_DEFAULT ||
47224a1cce3SDavid Greenman 			     object->type == OBJT_SWAP)) {
473a1f6d91cSDavid Greenman 				vm_object_t robject;
47495e5e988SJohn Dyson 
4751c500307SAlan Cox 				robject = LIST_FIRST(&object->shadow_head);
4765526d2d9SEivind Eklund 				KASSERT(robject != NULL,
477219cbf59SEivind Eklund 				    ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
4785526d2d9SEivind Eklund 					 object->ref_count,
4795526d2d9SEivind Eklund 					 object->shadow_count));
48095e5e988SJohn Dyson 				if ((robject->handle == NULL) &&
48124a1cce3SDavid Greenman 				    (robject->type == OBJT_DEFAULT ||
48224a1cce3SDavid Greenman 				     robject->type == OBJT_SWAP)) {
483a1f6d91cSDavid Greenman 
48495e5e988SJohn Dyson 					robject->ref_count++;
48595e5e988SJohn Dyson 
4861c7c3c6aSMatthew Dillon 					while (
4871c7c3c6aSMatthew Dillon 						robject->paging_in_progress ||
4881c7c3c6aSMatthew Dillon 						object->paging_in_progress
4891c7c3c6aSMatthew Dillon 					) {
49066095752SJohn Dyson 						vm_object_pip_sleep(robject, "objde1");
49166095752SJohn Dyson 						vm_object_pip_sleep(object, "objde2");
492a1f6d91cSDavid Greenman 					}
493a1f6d91cSDavid Greenman 
49495e5e988SJohn Dyson 					if (robject->ref_count == 1) {
49595e5e988SJohn Dyson 						robject->ref_count--;
496ba8da839SDavid Greenman 						object = robject;
49795e5e988SJohn Dyson 						goto doterm;
49895e5e988SJohn Dyson 					}
49995e5e988SJohn Dyson 
50095e5e988SJohn Dyson 					object = robject;
50195e5e988SJohn Dyson 					vm_object_collapse(object);
502ba8da839SDavid Greenman 					continue;
503a1f6d91cSDavid Greenman 				}
50495e5e988SJohn Dyson 			}
5054dbeceeeSAlan Cox 			vm_object_unlock(object);
506a1f6d91cSDavid Greenman 			return;
50795e5e988SJohn Dyson 		}
50895e5e988SJohn Dyson doterm:
509c829b9d0SAlan Cox 		VM_OBJECT_LOCK(object);
51024a1cce3SDavid Greenman 		temp = object->backing_object;
511c9917419SAlan Cox 		if (temp != NULL) {
512c9917419SAlan Cox 			VM_OBJECT_LOCK(temp);
5131c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
51495e5e988SJohn Dyson 			temp->shadow_count--;
515eaf13dd7SJohn Dyson 			temp->generation++;
516c9917419SAlan Cox 			VM_OBJECT_UNLOCK(temp);
51795461b45SJohn Dyson 			object->backing_object = NULL;
518de5f6a77SJohn Dyson 		}
519245df27cSMatthew Dillon 		/*
520245df27cSMatthew Dillon 		 * Don't double-terminate, we could be in a termination
521245df27cSMatthew Dillon 		 * recursion due to the terminate having to sync data
522245df27cSMatthew Dillon 		 * to disk.
523245df27cSMatthew Dillon 		 */
524245df27cSMatthew Dillon 		if ((object->flags & OBJ_DEAD) == 0)
525df8bae1dSRodney W. Grimes 			vm_object_terminate(object);
526c829b9d0SAlan Cox 		else
527c829b9d0SAlan Cox 			VM_OBJECT_UNLOCK(object);
528df8bae1dSRodney W. Grimes 		object = temp;
529df8bae1dSRodney W. Grimes 	}
5304dbeceeeSAlan Cox 	vm_object_unlock(object);
531df8bae1dSRodney W. Grimes }
532df8bae1dSRodney W. Grimes 
533df8bae1dSRodney W. Grimes /*
534df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
535df8bae1dSRodney W. Grimes  *	up all previously used resources.
536df8bae1dSRodney W. Grimes  *
537df8bae1dSRodney W. Grimes  *	The object must be locked.
5381c7c3c6aSMatthew Dillon  *	This routine may block.
539df8bae1dSRodney W. Grimes  */
54095e5e988SJohn Dyson void
5411b40f8c0SMatthew Dillon vm_object_terminate(vm_object_t object)
542df8bae1dSRodney W. Grimes {
543d031cff1SMatthew Dillon 	vm_page_t p;
544e4b7635dSDavid Greenman 	int s;
545df8bae1dSRodney W. Grimes 
546c829b9d0SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
5470cddd8f0SMatthew Dillon 
54895e5e988SJohn Dyson 	/*
54995e5e988SJohn Dyson 	 * Make sure no one uses us.
55095e5e988SJohn Dyson 	 */
551069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_DEAD);
5523c631446SJohn Dyson 
553df8bae1dSRodney W. Grimes 	/*
554f6b04d2bSDavid Greenman 	 * wait for the pageout daemon to be done with the object
555df8bae1dSRodney W. Grimes 	 */
55666095752SJohn Dyson 	vm_object_pip_wait(object, "objtrm");
557df8bae1dSRodney W. Grimes 
5585526d2d9SEivind Eklund 	KASSERT(!object->paging_in_progress,
5595526d2d9SEivind Eklund 		("vm_object_terminate: pageout in progress"));
56026f9a767SRodney W. Grimes 
56126f9a767SRodney W. Grimes 	/*
5620d94caffSDavid Greenman 	 * Clean and free the pages, as appropriate. All references to the
5630d94caffSDavid Greenman 	 * object are gone, so we don't need to lock it.
56426f9a767SRodney W. Grimes 	 */
56524a1cce3SDavid Greenman 	if (object->type == OBJT_VNODE) {
566f7dd7b63SAlan Cox 		struct vnode *vp = (struct vnode *)object->handle;
56795e5e988SJohn Dyson 
56895e5e988SJohn Dyson 		/*
56995e5e988SJohn Dyson 		 * Clean pages and flush buffers.
57095e5e988SJohn Dyson 		 */
5718f9110f6SJohn Dyson 		vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
572b6e48e03SAlan Cox 		VM_OBJECT_UNLOCK(object);
57395e5e988SJohn Dyson 
574f6b04d2bSDavid Greenman 		vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
575f7dd7b63SAlan Cox 
576f7dd7b63SAlan Cox 		VM_OBJECT_LOCK(object);
577bef608bdSJohn Dyson 	}
578bef608bdSJohn Dyson 
579971dd342SAlfred Perlstein 	KASSERT(object->ref_count == 0,
580971dd342SAlfred Perlstein 		("vm_object_terminate: object with references, ref_count=%d",
581971dd342SAlfred Perlstein 		object->ref_count));
582996c772fSJohn Dyson 
5830d94caffSDavid Greenman 	/*
584356863ebSDavid Greenman 	 * Now free any remaining pages. For internal objects, this also
585356863ebSDavid Greenman 	 * removes them from paging queues. Don't free wired pages, just
586356863ebSDavid Greenman 	 * remove them from the object.
587df8bae1dSRodney W. Grimes 	 */
588e4b7635dSDavid Greenman 	s = splvm();
58956030358SAlan Cox 	vm_page_lock_queues();
590b18bfc3dSJohn Dyson 	while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
591971dd342SAlfred Perlstein 		KASSERT(!p->busy && (p->flags & PG_BUSY) == 0,
592971dd342SAlfred Perlstein 			("vm_object_terminate: freeing busy page %p "
593971dd342SAlfred Perlstein 			"p->busy = %d, p->flags %x\n", p, p->busy, p->flags));
5940b10ba98SDavid Greenman 		if (p->wire_count == 0) {
595e69763a3SDoug Rabson 			vm_page_busy(p);
596df8bae1dSRodney W. Grimes 			vm_page_free(p);
597df8bae1dSRodney W. Grimes 			cnt.v_pfree++;
5980b10ba98SDavid Greenman 		} else {
599356863ebSDavid Greenman 			vm_page_busy(p);
6000b10ba98SDavid Greenman 			vm_page_remove(p);
6010b10ba98SDavid Greenman 		}
602df8bae1dSRodney W. Grimes 	}
60356030358SAlan Cox 	vm_page_unlock_queues();
604e4b7635dSDavid Greenman 	splx(s);
605bef608bdSJohn Dyson 
6062d8acc0fSJohn Dyson 	/*
6079fcfb650SDavid Greenman 	 * Let the pager know object is dead.
6089fcfb650SDavid Greenman 	 */
6099fcfb650SDavid Greenman 	vm_pager_deallocate(object);
610658ad5ffSAlan Cox 	VM_OBJECT_UNLOCK(object);
6119fcfb650SDavid Greenman 
6129fcfb650SDavid Greenman 	/*
6130b10ba98SDavid Greenman 	 * Remove the object from the global object list.
6142d8acc0fSJohn Dyson 	 */
6159ed346baSBosko Milekic 	mtx_lock(&vm_object_list_mtx);
616df8bae1dSRodney W. Grimes 	TAILQ_REMOVE(&vm_object_list, object, object_list);
6179ed346baSBosko Milekic 	mtx_unlock(&vm_object_list_mtx);
6180b10ba98SDavid Greenman 
6190b10ba98SDavid Greenman 	wakeup(object);
6200b10ba98SDavid Greenman 
621df8bae1dSRodney W. Grimes 	/*
622df8bae1dSRodney W. Grimes 	 * Free the space for the object.
623df8bae1dSRodney W. Grimes 	 */
624670d17b5SJeff Roberson 	uma_zfree(obj_zone, object);
62547221757SJohn Dyson }
626df8bae1dSRodney W. Grimes 
627df8bae1dSRodney W. Grimes /*
628df8bae1dSRodney W. Grimes  *	vm_object_page_clean
629df8bae1dSRodney W. Grimes  *
6304f79d873SMatthew Dillon  *	Clean all dirty pages in the specified range of object.  Leaves page
6314f79d873SMatthew Dillon  * 	on whatever queue it is currently on.   If NOSYNC is set then do not
6324f79d873SMatthew Dillon  *	write out pages with PG_NOSYNC set (originally comes from MAP_NOSYNC),
6334f79d873SMatthew Dillon  *	leaving the object dirty.
63426f9a767SRodney W. Grimes  *
63543b7990eSMatthew Dillon  *	When stuffing pages asynchronously, allow clustering.  XXX we need a
63643b7990eSMatthew Dillon  *	synchronous clustering mode implementation.
63743b7990eSMatthew Dillon  *
63826f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
63926f9a767SRodney W. Grimes  *
64026f9a767SRodney W. Grimes  *	The object must be locked.
64126f9a767SRodney W. Grimes  */
642f6b04d2bSDavid Greenman void
6431b40f8c0SMatthew Dillon vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end, int flags)
644f6b04d2bSDavid Greenman {
645b9b7a4beSMatthew Dillon 	vm_page_t p, np;
6466395da54SIan Dowse 	vm_pindex_t tstart, tend;
647bd7e5f99SJohn Dyson 	vm_pindex_t pi;
64824a1cce3SDavid Greenman 	struct vnode *vp;
6494f79d873SMatthew Dillon 	int clearobjflags;
6508f9110f6SJohn Dyson 	int pagerflags;
6512d8acc0fSJohn Dyson 	int curgeneration;
652f6b04d2bSDavid Greenman 
6530cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
654b6e48e03SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
655aef922f5SJohn Dyson 	if (object->type != OBJT_VNODE ||
656aef922f5SJohn Dyson 		(object->flags & OBJ_MIGHTBEDIRTY) == 0)
657f6b04d2bSDavid Greenman 		return;
658f6b04d2bSDavid Greenman 
65943b7990eSMatthew Dillon 	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
6608f9110f6SJohn Dyson 	pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
6618f9110f6SJohn Dyson 
66224a1cce3SDavid Greenman 	vp = object->handle;
66324a1cce3SDavid Greenman 
664069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_CLEANING);
66524a1cce3SDavid Greenman 
666f6b04d2bSDavid Greenman 	tstart = start;
667f6b04d2bSDavid Greenman 	if (end == 0) {
668f6b04d2bSDavid Greenman 		tend = object->size;
669f6b04d2bSDavid Greenman 	} else {
670f6b04d2bSDavid Greenman 		tend = end;
671f6b04d2bSDavid Greenman 	}
672eaf13dd7SJohn Dyson 
67335c01631SAlan Cox 	vm_page_lock_queues();
6744f79d873SMatthew Dillon 	/*
675b9b7a4beSMatthew Dillon 	 * If the caller is smart and only msync()s a range he knows is
676b9b7a4beSMatthew Dillon 	 * dirty, we may be able to avoid an object scan.  This results in
677b9b7a4beSMatthew Dillon 	 * a phenominal improvement in performance.  We cannot do this
678b9b7a4beSMatthew Dillon 	 * as a matter of course because the object may be huge - e.g.
679b9b7a4beSMatthew Dillon 	 * the size might be in the gigabytes or terrabytes.
680b9b7a4beSMatthew Dillon 	 */
681b9b7a4beSMatthew Dillon 	if (msync_flush_flags & MSYNC_FLUSH_HARDSEQ) {
682300b96acSIan Dowse 		vm_pindex_t tscan;
683b9b7a4beSMatthew Dillon 		int scanlimit;
684b9b7a4beSMatthew Dillon 		int scanreset;
685b9b7a4beSMatthew Dillon 
686b9b7a4beSMatthew Dillon 		scanreset = object->resident_page_count / EASY_SCAN_FACTOR;
687b9b7a4beSMatthew Dillon 		if (scanreset < 16)
688b9b7a4beSMatthew Dillon 			scanreset = 16;
68943b7990eSMatthew Dillon 		pagerflags |= VM_PAGER_IGNORE_CLEANCHK;
690b9b7a4beSMatthew Dillon 
691b9b7a4beSMatthew Dillon 		scanlimit = scanreset;
692b9b7a4beSMatthew Dillon 		tscan = tstart;
693b9b7a4beSMatthew Dillon 		while (tscan < tend) {
694b9b7a4beSMatthew Dillon 			curgeneration = object->generation;
695b9b7a4beSMatthew Dillon 			p = vm_page_lookup(object, tscan);
696b9b7a4beSMatthew Dillon 			if (p == NULL || p->valid == 0 ||
697b9b7a4beSMatthew Dillon 			    (p->queue - p->pc) == PQ_CACHE) {
698b9b7a4beSMatthew Dillon 				if (--scanlimit == 0)
699b9b7a4beSMatthew Dillon 					break;
700b9b7a4beSMatthew Dillon 				++tscan;
701b9b7a4beSMatthew Dillon 				continue;
702b9b7a4beSMatthew Dillon 			}
703b9b7a4beSMatthew Dillon 			vm_page_test_dirty(p);
704b9b7a4beSMatthew Dillon 			if ((p->dirty & p->valid) == 0) {
705b9b7a4beSMatthew Dillon 				if (--scanlimit == 0)
706b9b7a4beSMatthew Dillon 					break;
707b9b7a4beSMatthew Dillon 				++tscan;
708b9b7a4beSMatthew Dillon 				continue;
709b9b7a4beSMatthew Dillon 			}
710b9b7a4beSMatthew Dillon 			/*
711b9b7a4beSMatthew Dillon 			 * If we have been asked to skip nosync pages and
712b9b7a4beSMatthew Dillon 			 * this is a nosync page, we can't continue.
713b9b7a4beSMatthew Dillon 			 */
714b9b7a4beSMatthew Dillon 			if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
715b9b7a4beSMatthew Dillon 				if (--scanlimit == 0)
716b9b7a4beSMatthew Dillon 					break;
717b9b7a4beSMatthew Dillon 				++tscan;
718b9b7a4beSMatthew Dillon 				continue;
719b9b7a4beSMatthew Dillon 			}
720b9b7a4beSMatthew Dillon 			scanlimit = scanreset;
721b9b7a4beSMatthew Dillon 
722b9b7a4beSMatthew Dillon 			/*
723b9b7a4beSMatthew Dillon 			 * This returns 0 if it was unable to busy the first
724b9b7a4beSMatthew Dillon 			 * page (i.e. had to sleep).
725b9b7a4beSMatthew Dillon 			 */
726b9b7a4beSMatthew Dillon 			tscan += vm_object_page_collect_flush(object, p, curgeneration, pagerflags);
727b9b7a4beSMatthew Dillon 		}
728b9b7a4beSMatthew Dillon 
729b9b7a4beSMatthew Dillon 		/*
730b9b7a4beSMatthew Dillon 		 * If everything was dirty and we flushed it successfully,
731b9b7a4beSMatthew Dillon 		 * and the requested range is not the entire object, we
732b9b7a4beSMatthew Dillon 		 * don't have to mess with CLEANCHK or MIGHTBEDIRTY and can
733b9b7a4beSMatthew Dillon 		 * return immediately.
734b9b7a4beSMatthew Dillon 		 */
735b9b7a4beSMatthew Dillon 		if (tscan >= tend && (tstart || tend < object->size)) {
73635c01631SAlan Cox 			vm_page_unlock_queues();
737b9b7a4beSMatthew Dillon 			vm_object_clear_flag(object, OBJ_CLEANING);
738b9b7a4beSMatthew Dillon 			return;
739b9b7a4beSMatthew Dillon 		}
74043b7990eSMatthew Dillon 		pagerflags &= ~VM_PAGER_IGNORE_CLEANCHK;
741b9b7a4beSMatthew Dillon 	}
742b9b7a4beSMatthew Dillon 
743b9b7a4beSMatthew Dillon 	/*
7444f79d873SMatthew Dillon 	 * Generally set CLEANCHK interlock and make the page read-only so
7454f79d873SMatthew Dillon 	 * we can then clear the object flags.
7464f79d873SMatthew Dillon 	 *
7474f79d873SMatthew Dillon 	 * However, if this is a nosync mmap then the object is likely to
7484f79d873SMatthew Dillon 	 * stay dirty so do not mess with the page and do not clear the
7494f79d873SMatthew Dillon 	 * object flags.
7504f79d873SMatthew Dillon 	 */
7514f79d873SMatthew Dillon 	clearobjflags = 1;
752fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(p, &object->memq, listq) {
753e69763a3SDoug Rabson 		vm_page_flag_set(p, PG_CLEANCHK);
7544f79d873SMatthew Dillon 		if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC))
7554f79d873SMatthew Dillon 			clearobjflags = 0;
7564f79d873SMatthew Dillon 		else
757a12cc0e4SAlan Cox 			pmap_page_protect(p, VM_PROT_READ);
758eaf13dd7SJohn Dyson 	}
759eaf13dd7SJohn Dyson 
7604f79d873SMatthew Dillon 	if (clearobjflags && (tstart == 0) && (tend == object->size)) {
761245df27cSMatthew Dillon 		struct vnode *vp;
762245df27cSMatthew Dillon 
763069e9bc1SDoug Rabson 		vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
764245df27cSMatthew Dillon 		if (object->type == OBJT_VNODE &&
765245df27cSMatthew Dillon 		    (vp = (struct vnode *)object->handle) != NULL) {
766e6e370a7SJeff Roberson 			VI_LOCK(vp);
767e6e370a7SJeff Roberson 			if (vp->v_iflag & VI_OBJDIRTY)
768e6e370a7SJeff Roberson 				vp->v_iflag &= ~VI_OBJDIRTY;
769e6e370a7SJeff Roberson 			VI_UNLOCK(vp);
770245df27cSMatthew Dillon 		}
771ec4f9fb0SDavid Greenman 	}
772f6b04d2bSDavid Greenman 
773bd7e5f99SJohn Dyson rescan:
7742d8acc0fSJohn Dyson 	curgeneration = object->generation;
7752d8acc0fSJohn Dyson 
776b18bfc3dSJohn Dyson 	for (p = TAILQ_FIRST(&object->memq); p; p = np) {
777b9b7a4beSMatthew Dillon 		int n;
778b9b7a4beSMatthew Dillon 
779b18bfc3dSJohn Dyson 		np = TAILQ_NEXT(p, listq);
780bd7e5f99SJohn Dyson 
781b9b7a4beSMatthew Dillon again:
782bd7e5f99SJohn Dyson 		pi = p->pindex;
783bd7e5f99SJohn Dyson 		if (((p->flags & PG_CLEANCHK) == 0) ||
784bd7e5f99SJohn Dyson 			(pi < tstart) || (pi >= tend) ||
7855070c7f8SJohn Dyson 			(p->valid == 0) ||
7865070c7f8SJohn Dyson 			((p->queue - p->pc) == PQ_CACHE)) {
787e69763a3SDoug Rabson 			vm_page_flag_clear(p, PG_CLEANCHK);
788aef922f5SJohn Dyson 			continue;
789f6b04d2bSDavid Greenman 		}
790f6b04d2bSDavid Greenman 
791bd7e5f99SJohn Dyson 		vm_page_test_dirty(p);
792bd7e5f99SJohn Dyson 		if ((p->dirty & p->valid) == 0) {
793e69763a3SDoug Rabson 			vm_page_flag_clear(p, PG_CLEANCHK);
794bd7e5f99SJohn Dyson 			continue;
795bd7e5f99SJohn Dyson 		}
796ec4f9fb0SDavid Greenman 
7974f79d873SMatthew Dillon 		/*
7984f79d873SMatthew Dillon 		 * If we have been asked to skip nosync pages and this is a
7994f79d873SMatthew Dillon 		 * nosync page, skip it.  Note that the object flags were
8004f79d873SMatthew Dillon 		 * not cleared in this case so we do not have to set them.
8014f79d873SMatthew Dillon 		 */
8024f79d873SMatthew Dillon 		if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
8034f79d873SMatthew Dillon 			vm_page_flag_clear(p, PG_CLEANCHK);
8044f79d873SMatthew Dillon 			continue;
8054f79d873SMatthew Dillon 		}
8064f79d873SMatthew Dillon 
807b9b7a4beSMatthew Dillon 		n = vm_object_page_collect_flush(object, p,
808b9b7a4beSMatthew Dillon 			curgeneration, pagerflags);
809b9b7a4beSMatthew Dillon 		if (n == 0)
810b9b7a4beSMatthew Dillon 			goto rescan;
811b9b7a4beSMatthew Dillon 
812b9b7a4beSMatthew Dillon 		if (object->generation != curgeneration)
813b9b7a4beSMatthew Dillon 			goto rescan;
814b9b7a4beSMatthew Dillon 
815b9b7a4beSMatthew Dillon 		/*
816b9b7a4beSMatthew Dillon 		 * Try to optimize the next page.  If we can't we pick up
817b9b7a4beSMatthew Dillon 		 * our (random) scan where we left off.
818b9b7a4beSMatthew Dillon 		 */
819b9b7a4beSMatthew Dillon 		if (msync_flush_flags & MSYNC_FLUSH_SOFTSEQ) {
820b9b7a4beSMatthew Dillon 			if ((p = vm_page_lookup(object, pi + n)) != NULL)
821b9b7a4beSMatthew Dillon 				goto again;
822b9b7a4beSMatthew Dillon 		}
823b9b7a4beSMatthew Dillon 	}
82435c01631SAlan Cox 	vm_page_unlock_queues();
825b9b7a4beSMatthew Dillon #if 0
826b9b7a4beSMatthew Dillon 	VOP_FSYNC(vp, NULL, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
827b9b7a4beSMatthew Dillon #endif
828b9b7a4beSMatthew Dillon 
829b9b7a4beSMatthew Dillon 	vm_object_clear_flag(object, OBJ_CLEANING);
830b9b7a4beSMatthew Dillon 	return;
831b9b7a4beSMatthew Dillon }
832b9b7a4beSMatthew Dillon 
833b9b7a4beSMatthew Dillon static int
834b9b7a4beSMatthew Dillon vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags)
835b9b7a4beSMatthew Dillon {
836b9b7a4beSMatthew Dillon 	int runlen;
837b9b7a4beSMatthew Dillon 	int s;
838b9b7a4beSMatthew Dillon 	int maxf;
839b9b7a4beSMatthew Dillon 	int chkb;
840b9b7a4beSMatthew Dillon 	int maxb;
841b9b7a4beSMatthew Dillon 	int i;
842b9b7a4beSMatthew Dillon 	vm_pindex_t pi;
843b9b7a4beSMatthew Dillon 	vm_page_t maf[vm_pageout_page_count];
844b9b7a4beSMatthew Dillon 	vm_page_t mab[vm_pageout_page_count];
845b9b7a4beSMatthew Dillon 	vm_page_t ma[vm_pageout_page_count];
846b9b7a4beSMatthew Dillon 
847b18bfc3dSJohn Dyson 	s = splvm();
84835c01631SAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
849b9b7a4beSMatthew Dillon 	pi = p->pindex;
850bd82dc74SAlan Cox 	while (vm_page_sleep_if_busy(p, TRUE, "vpcwai")) {
85135c01631SAlan Cox 		vm_page_lock_queues();
8522d8acc0fSJohn Dyson 		if (object->generation != curgeneration) {
853f6b04d2bSDavid Greenman 			splx(s);
854b9b7a4beSMatthew Dillon 			return(0);
855f6b04d2bSDavid Greenman 		}
856bd82dc74SAlan Cox 	}
857bd7e5f99SJohn Dyson 	maxf = 0;
858bd7e5f99SJohn Dyson 	for(i = 1; i < vm_pageout_page_count; i++) {
859b9b7a4beSMatthew Dillon 		vm_page_t tp;
860b9b7a4beSMatthew Dillon 
8618aef1712SMatthew Dillon 		if ((tp = vm_page_lookup(object, pi + i)) != NULL) {
862bd7e5f99SJohn Dyson 			if ((tp->flags & PG_BUSY) ||
86343b7990eSMatthew Dillon 				((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
86443b7990eSMatthew Dillon 				 (tp->flags & PG_CLEANCHK) == 0) ||
865ffc82b0aSJohn Dyson 				(tp->busy != 0))
866bd7e5f99SJohn Dyson 				break;
8675070c7f8SJohn Dyson 			if((tp->queue - tp->pc) == PQ_CACHE) {
868e69763a3SDoug Rabson 				vm_page_flag_clear(tp, PG_CLEANCHK);
8693077a9c2SJohn Dyson 				break;
8703077a9c2SJohn Dyson 			}
871bd7e5f99SJohn Dyson 			vm_page_test_dirty(tp);
872bd7e5f99SJohn Dyson 			if ((tp->dirty & tp->valid) == 0) {
873e69763a3SDoug Rabson 				vm_page_flag_clear(tp, PG_CLEANCHK);
874bd7e5f99SJohn Dyson 				break;
875bd7e5f99SJohn Dyson 			}
876bd7e5f99SJohn Dyson 			maf[ i - 1 ] = tp;
877bd7e5f99SJohn Dyson 			maxf++;
878bd7e5f99SJohn Dyson 			continue;
879bd7e5f99SJohn Dyson 		}
880bd7e5f99SJohn Dyson 		break;
881bd7e5f99SJohn Dyson 	}
882aef922f5SJohn Dyson 
883bd7e5f99SJohn Dyson 	maxb = 0;
884bd7e5f99SJohn Dyson 	chkb = vm_pageout_page_count -  maxf;
885bd7e5f99SJohn Dyson 	if (chkb) {
886bd7e5f99SJohn Dyson 		for(i = 1; i < chkb;i++) {
887b9b7a4beSMatthew Dillon 			vm_page_t tp;
888b9b7a4beSMatthew Dillon 
8898aef1712SMatthew Dillon 			if ((tp = vm_page_lookup(object, pi - i)) != NULL) {
890bd7e5f99SJohn Dyson 				if ((tp->flags & PG_BUSY) ||
89143b7990eSMatthew Dillon 					((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
89243b7990eSMatthew Dillon 					 (tp->flags & PG_CLEANCHK) == 0) ||
893ffc82b0aSJohn Dyson 					(tp->busy != 0))
894bd7e5f99SJohn Dyson 					break;
8955070c7f8SJohn Dyson 				if ((tp->queue - tp->pc) == PQ_CACHE) {
896e69763a3SDoug Rabson 					vm_page_flag_clear(tp, PG_CLEANCHK);
8973077a9c2SJohn Dyson 					break;
8983077a9c2SJohn Dyson 				}
899bd7e5f99SJohn Dyson 				vm_page_test_dirty(tp);
900bd7e5f99SJohn Dyson 				if ((tp->dirty & tp->valid) == 0) {
901e69763a3SDoug Rabson 					vm_page_flag_clear(tp, PG_CLEANCHK);
902bd7e5f99SJohn Dyson 					break;
903bd7e5f99SJohn Dyson 				}
904bd7e5f99SJohn Dyson 				mab[ i - 1 ] = tp;
905bd7e5f99SJohn Dyson 				maxb++;
906bd7e5f99SJohn Dyson 				continue;
907bd7e5f99SJohn Dyson 			}
908bd7e5f99SJohn Dyson 			break;
909bd7e5f99SJohn Dyson 		}
910bd7e5f99SJohn Dyson 	}
911bd7e5f99SJohn Dyson 
912bd7e5f99SJohn Dyson 	for(i = 0; i < maxb; i++) {
913bd7e5f99SJohn Dyson 		int index = (maxb - i) - 1;
914bd7e5f99SJohn Dyson 		ma[index] = mab[i];
915e69763a3SDoug Rabson 		vm_page_flag_clear(ma[index], PG_CLEANCHK);
916bd7e5f99SJohn Dyson 	}
917e69763a3SDoug Rabson 	vm_page_flag_clear(p, PG_CLEANCHK);
918bd7e5f99SJohn Dyson 	ma[maxb] = p;
919bd7e5f99SJohn Dyson 	for(i = 0; i < maxf; i++) {
920bd7e5f99SJohn Dyson 		int index = (maxb + i) + 1;
921bd7e5f99SJohn Dyson 		ma[index] = maf[i];
922e69763a3SDoug Rabson 		vm_page_flag_clear(ma[index], PG_CLEANCHK);
923f6b04d2bSDavid Greenman 	}
924bd7e5f99SJohn Dyson 	runlen = maxb + maxf + 1;
925cf2819ccSJohn Dyson 
926f35329acSJohn Dyson 	splx(s);
927b6e48e03SAlan Cox 	vm_pageout_flush(ma, runlen, pagerflags, TRUE);
928cf2819ccSJohn Dyson 	for (i = 0; i < runlen; i++) {
929cf2819ccSJohn Dyson 		if (ma[i]->valid & ma[i]->dirty) {
930a12cc0e4SAlan Cox 			pmap_page_protect(ma[i], VM_PROT_READ);
931e69763a3SDoug Rabson 			vm_page_flag_set(ma[i], PG_CLEANCHK);
932aef922f5SJohn Dyson 
933b9b7a4beSMatthew Dillon 			/*
934b9b7a4beSMatthew Dillon 			 * maxf will end up being the actual number of pages
935b9b7a4beSMatthew Dillon 			 * we wrote out contiguously, non-inclusive of the
936b9b7a4beSMatthew Dillon 			 * first page.  We do not count look-behind pages.
937b9b7a4beSMatthew Dillon 			 */
938b9b7a4beSMatthew Dillon 			if (i >= maxb + 1 && (maxf > i - maxb - 1))
939b9b7a4beSMatthew Dillon 				maxf = i - maxb - 1;
940b9b7a4beSMatthew Dillon 		}
941b9b7a4beSMatthew Dillon 	}
942b9b7a4beSMatthew Dillon 	return(maxf + 1);
94326f9a767SRodney W. Grimes }
944df8bae1dSRodney W. Grimes 
9451efb74fbSJohn Dyson /*
946867a482dSJohn Dyson  *	vm_object_madvise:
947867a482dSJohn Dyson  *
948867a482dSJohn Dyson  *	Implements the madvise function at the object/page level.
9491c7c3c6aSMatthew Dillon  *
950193b9358SAlan Cox  *	MADV_WILLNEED	(any object)
951193b9358SAlan Cox  *
952193b9358SAlan Cox  *	    Activate the specified pages if they are resident.
953193b9358SAlan Cox  *
954193b9358SAlan Cox  *	MADV_DONTNEED	(any object)
955193b9358SAlan Cox  *
956193b9358SAlan Cox  *	    Deactivate the specified pages if they are resident.
957193b9358SAlan Cox  *
958193b9358SAlan Cox  *	MADV_FREE	(OBJT_DEFAULT/OBJT_SWAP objects,
959193b9358SAlan Cox  *			 OBJ_ONEMAPPING only)
960193b9358SAlan Cox  *
961193b9358SAlan Cox  *	    Deactivate and clean the specified pages if they are
962193b9358SAlan Cox  *	    resident.  This permits the process to reuse the pages
963193b9358SAlan Cox  *	    without faulting or the kernel to reclaim the pages
964193b9358SAlan Cox  *	    without I/O.
965867a482dSJohn Dyson  */
966867a482dSJohn Dyson void
9671b40f8c0SMatthew Dillon vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
968867a482dSJohn Dyson {
9696e20a165SJohn Dyson 	vm_pindex_t end, tpindex;
97034567de7SAlan Cox 	vm_object_t backing_object, tobject;
971867a482dSJohn Dyson 	vm_page_t m;
972867a482dSJohn Dyson 
973867a482dSJohn Dyson 	if (object == NULL)
974867a482dSJohn Dyson 		return;
975867a482dSJohn Dyson 
97634567de7SAlan Cox 	mtx_lock(&Giant);
977094f6d26SAlan Cox 
978867a482dSJohn Dyson 	end = pindex + count;
979867a482dSJohn Dyson 
9801c7c3c6aSMatthew Dillon 	/*
9811c7c3c6aSMatthew Dillon 	 * Locate and adjust resident pages
9821c7c3c6aSMatthew Dillon 	 */
9831c7c3c6aSMatthew Dillon 	for (; pindex < end; pindex += 1) {
9846e20a165SJohn Dyson relookup:
9856e20a165SJohn Dyson 		tobject = object;
9866e20a165SJohn Dyson 		tpindex = pindex;
98734567de7SAlan Cox 		VM_OBJECT_LOCK(tobject);
9886e20a165SJohn Dyson shadowlookup:
98958b4e6ccSAlan Cox 		/*
99058b4e6ccSAlan Cox 		 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
99158b4e6ccSAlan Cox 		 * and those pages must be OBJ_ONEMAPPING.
99258b4e6ccSAlan Cox 		 */
99358b4e6ccSAlan Cox 		if (advise == MADV_FREE) {
99458b4e6ccSAlan Cox 			if ((tobject->type != OBJT_DEFAULT &&
99558b4e6ccSAlan Cox 			     tobject->type != OBJT_SWAP) ||
99658b4e6ccSAlan Cox 			    (tobject->flags & OBJ_ONEMAPPING) == 0) {
99734567de7SAlan Cox 				goto unlock_tobject;
9986e20a165SJohn Dyson 			}
99958b4e6ccSAlan Cox 		}
10001c7c3c6aSMatthew Dillon 
10011c7c3c6aSMatthew Dillon 		m = vm_page_lookup(tobject, tpindex);
10021c7c3c6aSMatthew Dillon 
10031c7c3c6aSMatthew Dillon 		if (m == NULL) {
10041ce137beSMatthew Dillon 			/*
10051ce137beSMatthew Dillon 			 * There may be swap even if there is no backing page
10061ce137beSMatthew Dillon 			 */
10071ce137beSMatthew Dillon 			if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
10081ce137beSMatthew Dillon 				swap_pager_freespace(tobject, tpindex, 1);
10091ce137beSMatthew Dillon 
10101ce137beSMatthew Dillon 			/*
10111ce137beSMatthew Dillon 			 * next object
10121ce137beSMatthew Dillon 			 */
101334567de7SAlan Cox 			backing_object = tobject->backing_object;
101434567de7SAlan Cox 			if (backing_object == NULL)
101534567de7SAlan Cox 				goto unlock_tobject;
101634567de7SAlan Cox 			VM_OBJECT_LOCK(backing_object);
101734567de7SAlan Cox 			VM_OBJECT_UNLOCK(tobject);
101834567de7SAlan Cox 			tobject = backing_object;
10196e20a165SJohn Dyson 			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
10206e20a165SJohn Dyson 			goto shadowlookup;
10216e20a165SJohn Dyson 		}
1022867a482dSJohn Dyson 
1023867a482dSJohn Dyson 		/*
1024867a482dSJohn Dyson 		 * If the page is busy or not in a normal active state,
10258b03c8edSMatthew Dillon 		 * we skip it.  If the page is not managed there are no
10268b03c8edSMatthew Dillon 		 * page queues to mess with.  Things can break if we mess
10278b03c8edSMatthew Dillon 		 * with pages in any of the below states.
1028867a482dSJohn Dyson 		 */
102932585dd6SAlan Cox 		vm_page_lock_queues();
103032585dd6SAlan Cox 		if (m->hold_count ||
10311c7c3c6aSMatthew Dillon 		    m->wire_count ||
10328b03c8edSMatthew Dillon 		    (m->flags & PG_UNMANAGED) ||
103332585dd6SAlan Cox 		    m->valid != VM_PAGE_BITS_ALL) {
103432585dd6SAlan Cox 			vm_page_unlock_queues();
103534567de7SAlan Cox 			goto unlock_tobject;
10366e20a165SJohn Dyson 		}
103734567de7SAlan Cox  		if (vm_page_sleep_if_busy(m, TRUE, "madvpo")) {
103834567de7SAlan Cox 			VM_OBJECT_UNLOCK(tobject);
10396e20a165SJohn Dyson   			goto relookup;
104034567de7SAlan Cox 		}
1041867a482dSJohn Dyson 		if (advise == MADV_WILLNEED) {
1042867a482dSJohn Dyson 			vm_page_activate(m);
10436e20a165SJohn Dyson 		} else if (advise == MADV_DONTNEED) {
1044479112dfSMatthew Dillon 			vm_page_dontneed(m);
10450a47b48bSJohn Dyson 		} else if (advise == MADV_FREE) {
10461c7c3c6aSMatthew Dillon 			/*
10472aaeadf8SMatthew Dillon 			 * Mark the page clean.  This will allow the page
10482aaeadf8SMatthew Dillon 			 * to be freed up by the system.  However, such pages
10492aaeadf8SMatthew Dillon 			 * are often reused quickly by malloc()/free()
10502aaeadf8SMatthew Dillon 			 * so we do not do anything that would cause
10512aaeadf8SMatthew Dillon 			 * a page fault if we can help it.
10522aaeadf8SMatthew Dillon 			 *
10532aaeadf8SMatthew Dillon 			 * Specifically, we do not try to actually free
10542aaeadf8SMatthew Dillon 			 * the page now nor do we try to put it in the
10552aaeadf8SMatthew Dillon 			 * cache (which would cause a page fault on reuse).
105641c67e12SMatthew Dillon 			 *
105741c67e12SMatthew Dillon 			 * But we do make the page is freeable as we
105841c67e12SMatthew Dillon 			 * can without actually taking the step of unmapping
105941c67e12SMatthew Dillon 			 * it.
10601c7c3c6aSMatthew Dillon 			 */
10610385347cSPeter Wemm 			pmap_clear_modify(m);
10626e20a165SJohn Dyson 			m->dirty = 0;
106341c67e12SMatthew Dillon 			m->act_count = 0;
1064479112dfSMatthew Dillon 			vm_page_dontneed(m);
1065867a482dSJohn Dyson 		}
10662999e9faSAlan Cox 		vm_page_unlock_queues();
10672999e9faSAlan Cox 		if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
10682999e9faSAlan Cox 			swap_pager_freespace(tobject, tpindex, 1);
106934567de7SAlan Cox unlock_tobject:
107034567de7SAlan Cox 		VM_OBJECT_UNLOCK(tobject);
1071867a482dSJohn Dyson 	}
107234567de7SAlan Cox 	mtx_unlock(&Giant);
1073867a482dSJohn Dyson }
1074867a482dSJohn Dyson 
1075867a482dSJohn Dyson /*
1076df8bae1dSRodney W. Grimes  *	vm_object_shadow:
1077df8bae1dSRodney W. Grimes  *
1078df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
1079df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
1080df8bae1dSRodney W. Grimes  *	object reference is deallocated.
1081df8bae1dSRodney W. Grimes  *
1082df8bae1dSRodney W. Grimes  *	The new object and offset into that object
1083df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
1084df8bae1dSRodney W. Grimes  */
108526f9a767SRodney W. Grimes void
10861b40f8c0SMatthew Dillon vm_object_shadow(
10871b40f8c0SMatthew Dillon 	vm_object_t *object,	/* IN/OUT */
10881b40f8c0SMatthew Dillon 	vm_ooffset_t *offset,	/* IN/OUT */
10891b40f8c0SMatthew Dillon 	vm_size_t length)
1090df8bae1dSRodney W. Grimes {
1091d031cff1SMatthew Dillon 	vm_object_t source;
1092d031cff1SMatthew Dillon 	vm_object_t result;
1093df8bae1dSRodney W. Grimes 
1094570a2f4aSAlan Cox 	GIANT_REQUIRED;
1095570a2f4aSAlan Cox 
1096df8bae1dSRodney W. Grimes 	source = *object;
1097df8bae1dSRodney W. Grimes 
1098df8bae1dSRodney W. Grimes 	/*
10999a2f6362SAlan Cox 	 * Don't create the new object if the old object isn't shared.
11009a2f6362SAlan Cox 	 */
1101570a2f4aSAlan Cox 	if (source != NULL) {
1102570a2f4aSAlan Cox 		VM_OBJECT_LOCK(source);
1103570a2f4aSAlan Cox 		if (source->ref_count == 1 &&
11049a2f6362SAlan Cox 		    source->handle == NULL &&
11059a2f6362SAlan Cox 		    (source->type == OBJT_DEFAULT ||
11069917e010SAlan Cox 		     source->type == OBJT_SWAP)) {
1107570a2f4aSAlan Cox 			VM_OBJECT_UNLOCK(source);
11089a2f6362SAlan Cox 			return;
11099917e010SAlan Cox 		}
1110570a2f4aSAlan Cox 		VM_OBJECT_UNLOCK(source);
1111570a2f4aSAlan Cox 	}
11129a2f6362SAlan Cox 
11139a2f6362SAlan Cox 	/*
1114570a2f4aSAlan Cox 	 * Allocate a new object with the given length.
1115df8bae1dSRodney W. Grimes 	 */
1116971dd342SAlfred Perlstein 	result = vm_object_allocate(OBJT_DEFAULT, length);
1117df8bae1dSRodney W. Grimes 
1118df8bae1dSRodney W. Grimes 	/*
11190d94caffSDavid Greenman 	 * The new object shadows the source object, adding a reference to it.
11200d94caffSDavid Greenman 	 * Our caller changes his reference to point to the new object,
11210d94caffSDavid Greenman 	 * removing a reference to the source object.  Net result: no change
11220d94caffSDavid Greenman 	 * of reference count.
11239b09fe24SMatthew Dillon 	 *
11249b09fe24SMatthew Dillon 	 * Try to optimize the result object's page color when shadowing
1125956f3135SPhilippe Charnier 	 * in order to maintain page coloring consistency in the combined
11269b09fe24SMatthew Dillon 	 * shadowed object.
1127df8bae1dSRodney W. Grimes 	 */
112824a1cce3SDavid Greenman 	result->backing_object = source;
1129570a2f4aSAlan Cox 	if (source != NULL) {
1130570a2f4aSAlan Cox 		VM_OBJECT_LOCK(source);
11311c500307SAlan Cox 		LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1132eaf13dd7SJohn Dyson 		source->shadow_count++;
1133eaf13dd7SJohn Dyson 		source->generation++;
1134dcc5840eSAlan Cox 		if (length < source->size)
1135dcc5840eSAlan Cox 			length = source->size;
1136dcc5840eSAlan Cox 		if (length > PQ_L2_SIZE / 3 + PQ_PRIME1 ||
1137dcc5840eSAlan Cox 		    source->generation > 1)
1138dcc5840eSAlan Cox 			length = PQ_L2_SIZE / 3 + PQ_PRIME1;
1139dcc5840eSAlan Cox 		result->pg_color = (source->pg_color +
1140dcc5840eSAlan Cox 		    length * source->generation) & PQ_L2_MASK;
1141570a2f4aSAlan Cox 		VM_OBJECT_UNLOCK(source);
1142dcc5840eSAlan Cox 		next_index = (result->pg_color + PQ_L2_SIZE / 3 + PQ_PRIME1) &
1143dcc5840eSAlan Cox 		    PQ_L2_MASK;
1144de5f6a77SJohn Dyson 	}
1145df8bae1dSRodney W. Grimes 
1146df8bae1dSRodney W. Grimes 	/*
11470d94caffSDavid Greenman 	 * Store the offset into the source object, and fix up the offset into
11480d94caffSDavid Greenman 	 * the new object.
1149df8bae1dSRodney W. Grimes 	 */
115024a1cce3SDavid Greenman 	result->backing_object_offset = *offset;
1151df8bae1dSRodney W. Grimes 
1152df8bae1dSRodney W. Grimes 	/*
1153df8bae1dSRodney W. Grimes 	 * Return the new things
1154df8bae1dSRodney W. Grimes 	 */
1155df8bae1dSRodney W. Grimes 	*offset = 0;
1156df8bae1dSRodney W. Grimes 	*object = result;
1157df8bae1dSRodney W. Grimes }
1158df8bae1dSRodney W. Grimes 
1159c5aaa06dSAlan Cox /*
1160c5aaa06dSAlan Cox  *	vm_object_split:
1161c5aaa06dSAlan Cox  *
1162c5aaa06dSAlan Cox  * Split the pages in a map entry into a new object.  This affords
1163c5aaa06dSAlan Cox  * easier removal of unused pages, and keeps object inheritance from
1164c5aaa06dSAlan Cox  * being a negative impact on memory usage.
1165c5aaa06dSAlan Cox  */
1166c5aaa06dSAlan Cox void
1167c5aaa06dSAlan Cox vm_object_split(vm_map_entry_t entry)
1168c5aaa06dSAlan Cox {
1169c5aaa06dSAlan Cox 	vm_page_t m;
1170c5aaa06dSAlan Cox 	vm_object_t orig_object, new_object, source;
1171c5aaa06dSAlan Cox 	vm_offset_t s, e;
117223f09d50SIan Dowse 	vm_pindex_t offidxstart, offidxend;
117323f09d50SIan Dowse 	vm_size_t idx, size;
1174c5aaa06dSAlan Cox 	vm_ooffset_t offset;
1175c5aaa06dSAlan Cox 
1176c5aaa06dSAlan Cox 	GIANT_REQUIRED;
1177c5aaa06dSAlan Cox 
1178c5aaa06dSAlan Cox 	orig_object = entry->object.vm_object;
1179c5aaa06dSAlan Cox 	if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1180c5aaa06dSAlan Cox 		return;
1181c5aaa06dSAlan Cox 	if (orig_object->ref_count <= 1)
1182c5aaa06dSAlan Cox 		return;
1183c5aaa06dSAlan Cox 
1184c5aaa06dSAlan Cox 	offset = entry->offset;
1185c5aaa06dSAlan Cox 	s = entry->start;
1186c5aaa06dSAlan Cox 	e = entry->end;
1187c5aaa06dSAlan Cox 
1188c5aaa06dSAlan Cox 	offidxstart = OFF_TO_IDX(offset);
1189c5aaa06dSAlan Cox 	offidxend = offidxstart + OFF_TO_IDX(e - s);
1190c5aaa06dSAlan Cox 	size = offidxend - offidxstart;
1191c5aaa06dSAlan Cox 
1192c5aaa06dSAlan Cox 	new_object = vm_pager_allocate(orig_object->type,
1193c5aaa06dSAlan Cox 		NULL, IDX_TO_OFF(size), VM_PROT_ALL, 0LL);
1194c5aaa06dSAlan Cox 	if (new_object == NULL)
1195c5aaa06dSAlan Cox 		return;
1196c5aaa06dSAlan Cox 
1197c5aaa06dSAlan Cox 	source = orig_object->backing_object;
1198c5aaa06dSAlan Cox 	if (source != NULL) {
1199c5aaa06dSAlan Cox 		vm_object_reference(source);	/* Referenced by new_object */
12008e3a76fbSAlan Cox 		VM_OBJECT_LOCK(source);
12011c500307SAlan Cox 		LIST_INSERT_HEAD(&source->shadow_head,
1202c5aaa06dSAlan Cox 				  new_object, shadow_list);
12038e3a76fbSAlan Cox 		source->shadow_count++;
12048e3a76fbSAlan Cox 		source->generation++;
1205c5aaa06dSAlan Cox 		vm_object_clear_flag(source, OBJ_ONEMAPPING);
1206e2479b4fSAlan Cox 		VM_OBJECT_UNLOCK(source);
1207c5aaa06dSAlan Cox 		new_object->backing_object_offset =
120856030358SAlan Cox 			orig_object->backing_object_offset + offset;
1209c5aaa06dSAlan Cox 		new_object->backing_object = source;
1210c5aaa06dSAlan Cox 	}
12118e3a76fbSAlan Cox 	VM_OBJECT_LOCK(orig_object);
1212c5aaa06dSAlan Cox 	for (idx = 0; idx < size; idx++) {
1213c5aaa06dSAlan Cox 	retry:
1214c5aaa06dSAlan Cox 		m = vm_page_lookup(orig_object, offidxstart + idx);
1215c5aaa06dSAlan Cox 		if (m == NULL)
1216c5aaa06dSAlan Cox 			continue;
1217c5aaa06dSAlan Cox 
1218c5aaa06dSAlan Cox 		/*
1219c5aaa06dSAlan Cox 		 * We must wait for pending I/O to complete before we can
1220c5aaa06dSAlan Cox 		 * rename the page.
1221c5aaa06dSAlan Cox 		 *
1222c5aaa06dSAlan Cox 		 * We do not have to VM_PROT_NONE the page as mappings should
1223c5aaa06dSAlan Cox 		 * not be changed by this operation.
1224c5aaa06dSAlan Cox 		 */
122500f9e8b4SAlan Cox 		vm_page_lock_queues();
122600f9e8b4SAlan Cox 		if (vm_page_sleep_if_busy(m, TRUE, "spltwt"))
1227c5aaa06dSAlan Cox 			goto retry;
1228c5aaa06dSAlan Cox 
1229c5aaa06dSAlan Cox 		vm_page_busy(m);
1230c5aaa06dSAlan Cox 		vm_page_rename(m, new_object, idx);
1231c5aaa06dSAlan Cox 		/* page automatically made dirty by rename and cache handled */
1232c5aaa06dSAlan Cox 		vm_page_busy(m);
1233bd82dc74SAlan Cox 		vm_page_unlock_queues();
1234c5aaa06dSAlan Cox 	}
1235d7a013c3SAlan Cox 	if (orig_object->type == OBJT_SWAP) {
1236c5aaa06dSAlan Cox 		vm_object_pip_add(orig_object, 1);
1237d22bc710SAlan Cox 		VM_OBJECT_UNLOCK(orig_object);
1238c5aaa06dSAlan Cox 		/*
1239c5aaa06dSAlan Cox 		 * copy orig_object pages into new_object
1240c5aaa06dSAlan Cox 		 * and destroy unneeded pages in
1241c5aaa06dSAlan Cox 		 * shadow object.
1242c5aaa06dSAlan Cox 		 */
1243c5aaa06dSAlan Cox 		swap_pager_copy(orig_object, new_object, offidxstart, 0);
1244d7a013c3SAlan Cox 		VM_OBJECT_LOCK(orig_object);
1245c5aaa06dSAlan Cox 		vm_object_pip_wakeup(orig_object);
1246c5aaa06dSAlan Cox 	}
1247d7a013c3SAlan Cox 	VM_OBJECT_UNLOCK(orig_object);
1248dc907f66SAlan Cox 	vm_page_lock_queues();
1249c7118ed6SAlan Cox 	TAILQ_FOREACH(m, &new_object->memq, listq)
1250c5aaa06dSAlan Cox 		vm_page_wakeup(m);
1251dc907f66SAlan Cox 	vm_page_unlock_queues();
1252c5aaa06dSAlan Cox 	entry->object.vm_object = new_object;
1253c5aaa06dSAlan Cox 	entry->offset = 0LL;
1254c5aaa06dSAlan Cox 	vm_object_deallocate(orig_object);
1255c5aaa06dSAlan Cox }
1256c5aaa06dSAlan Cox 
12572ad1a3f7SMatthew Dillon #define	OBSC_TEST_ALL_SHADOWED	0x0001
12582ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_NOWAIT	0x0002
12592ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_WAIT	0x0004
12602ad1a3f7SMatthew Dillon 
12612ad1a3f7SMatthew Dillon static __inline int
12622ad1a3f7SMatthew Dillon vm_object_backing_scan(vm_object_t object, int op)
12632ad1a3f7SMatthew Dillon {
12642ad1a3f7SMatthew Dillon 	int s;
12652ad1a3f7SMatthew Dillon 	int r = 1;
12662ad1a3f7SMatthew Dillon 	vm_page_t p;
12672ad1a3f7SMatthew Dillon 	vm_object_t backing_object;
12682ad1a3f7SMatthew Dillon 	vm_pindex_t backing_offset_index;
12692ad1a3f7SMatthew Dillon 
12702ad1a3f7SMatthew Dillon 	s = splvm();
12710cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
12722ad1a3f7SMatthew Dillon 
12732ad1a3f7SMatthew Dillon 	backing_object = object->backing_object;
12742ad1a3f7SMatthew Dillon 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
12752ad1a3f7SMatthew Dillon 
12762ad1a3f7SMatthew Dillon 	/*
12772ad1a3f7SMatthew Dillon 	 * Initial conditions
12782ad1a3f7SMatthew Dillon 	 */
12792ad1a3f7SMatthew Dillon 	if (op & OBSC_TEST_ALL_SHADOWED) {
12802ad1a3f7SMatthew Dillon 		/*
1281956f3135SPhilippe Charnier 		 * We do not want to have to test for the existence of
12822ad1a3f7SMatthew Dillon 		 * swap pages in the backing object.  XXX but with the
12832ad1a3f7SMatthew Dillon 		 * new swapper this would be pretty easy to do.
12842ad1a3f7SMatthew Dillon 		 *
12852ad1a3f7SMatthew Dillon 		 * XXX what about anonymous MAP_SHARED memory that hasn't
12862ad1a3f7SMatthew Dillon 		 * been ZFOD faulted yet?  If we do not test for this, the
12872ad1a3f7SMatthew Dillon 		 * shadow test may succeed! XXX
12882ad1a3f7SMatthew Dillon 		 */
12892ad1a3f7SMatthew Dillon 		if (backing_object->type != OBJT_DEFAULT) {
12902ad1a3f7SMatthew Dillon 			splx(s);
12912ad1a3f7SMatthew Dillon 			return (0);
12922ad1a3f7SMatthew Dillon 		}
12932ad1a3f7SMatthew Dillon 	}
12942ad1a3f7SMatthew Dillon 	if (op & OBSC_COLLAPSE_WAIT) {
12952ad1a3f7SMatthew Dillon 		vm_object_set_flag(backing_object, OBJ_DEAD);
12962ad1a3f7SMatthew Dillon 	}
12972ad1a3f7SMatthew Dillon 
12982ad1a3f7SMatthew Dillon 	/*
12992ad1a3f7SMatthew Dillon 	 * Our scan
13002ad1a3f7SMatthew Dillon 	 */
13012ad1a3f7SMatthew Dillon 	p = TAILQ_FIRST(&backing_object->memq);
13022ad1a3f7SMatthew Dillon 	while (p) {
13032ad1a3f7SMatthew Dillon 		vm_page_t next = TAILQ_NEXT(p, listq);
13042ad1a3f7SMatthew Dillon 		vm_pindex_t new_pindex = p->pindex - backing_offset_index;
13052ad1a3f7SMatthew Dillon 
13062ad1a3f7SMatthew Dillon 		if (op & OBSC_TEST_ALL_SHADOWED) {
13072ad1a3f7SMatthew Dillon 			vm_page_t pp;
13082ad1a3f7SMatthew Dillon 
13092ad1a3f7SMatthew Dillon 			/*
13102ad1a3f7SMatthew Dillon 			 * Ignore pages outside the parent object's range
13112ad1a3f7SMatthew Dillon 			 * and outside the parent object's mapping of the
13122ad1a3f7SMatthew Dillon 			 * backing object.
13132ad1a3f7SMatthew Dillon 			 *
13142ad1a3f7SMatthew Dillon 			 * note that we do not busy the backing object's
13152ad1a3f7SMatthew Dillon 			 * page.
13162ad1a3f7SMatthew Dillon 			 */
13172ad1a3f7SMatthew Dillon 			if (
13182ad1a3f7SMatthew Dillon 			    p->pindex < backing_offset_index ||
13192ad1a3f7SMatthew Dillon 			    new_pindex >= object->size
13202ad1a3f7SMatthew Dillon 			) {
13212ad1a3f7SMatthew Dillon 				p = next;
13222ad1a3f7SMatthew Dillon 				continue;
13232ad1a3f7SMatthew Dillon 			}
13242ad1a3f7SMatthew Dillon 
13252ad1a3f7SMatthew Dillon 			/*
13262ad1a3f7SMatthew Dillon 			 * See if the parent has the page or if the parent's
13272ad1a3f7SMatthew Dillon 			 * object pager has the page.  If the parent has the
13282ad1a3f7SMatthew Dillon 			 * page but the page is not valid, the parent's
13292ad1a3f7SMatthew Dillon 			 * object pager must have the page.
13302ad1a3f7SMatthew Dillon 			 *
13312ad1a3f7SMatthew Dillon 			 * If this fails, the parent does not completely shadow
13322ad1a3f7SMatthew Dillon 			 * the object and we might as well give up now.
13332ad1a3f7SMatthew Dillon 			 */
13342ad1a3f7SMatthew Dillon 
13352ad1a3f7SMatthew Dillon 			pp = vm_page_lookup(object, new_pindex);
13362ad1a3f7SMatthew Dillon 			if (
13372ad1a3f7SMatthew Dillon 			    (pp == NULL || pp->valid == 0) &&
13382ad1a3f7SMatthew Dillon 			    !vm_pager_has_page(object, new_pindex, NULL, NULL)
13392ad1a3f7SMatthew Dillon 			) {
13402ad1a3f7SMatthew Dillon 				r = 0;
13412ad1a3f7SMatthew Dillon 				break;
13422ad1a3f7SMatthew Dillon 			}
13432ad1a3f7SMatthew Dillon 		}
13442ad1a3f7SMatthew Dillon 
13452ad1a3f7SMatthew Dillon 		/*
13462ad1a3f7SMatthew Dillon 		 * Check for busy page
13472ad1a3f7SMatthew Dillon 		 */
13482ad1a3f7SMatthew Dillon 		if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
13492ad1a3f7SMatthew Dillon 			vm_page_t pp;
13502ad1a3f7SMatthew Dillon 
135100f9e8b4SAlan Cox 			vm_page_lock_queues();
13522ad1a3f7SMatthew Dillon 			if (op & OBSC_COLLAPSE_NOWAIT) {
135300f9e8b4SAlan Cox 				if ((p->flags & PG_BUSY) ||
13542ad1a3f7SMatthew Dillon 				    !p->valid ||
13552ad1a3f7SMatthew Dillon 				    p->hold_count ||
13562ad1a3f7SMatthew Dillon 				    p->wire_count ||
135700f9e8b4SAlan Cox 				    p->busy) {
135800f9e8b4SAlan Cox 					vm_page_unlock_queues();
13592ad1a3f7SMatthew Dillon 					p = next;
13602ad1a3f7SMatthew Dillon 					continue;
13612ad1a3f7SMatthew Dillon 				}
13622ad1a3f7SMatthew Dillon 			} else if (op & OBSC_COLLAPSE_WAIT) {
136300f9e8b4SAlan Cox 				if (vm_page_sleep_if_busy(p, TRUE, "vmocol")) {
13642ad1a3f7SMatthew Dillon 					/*
13652ad1a3f7SMatthew Dillon 					 * If we slept, anything could have
13662ad1a3f7SMatthew Dillon 					 * happened.  Since the object is
13672ad1a3f7SMatthew Dillon 					 * marked dead, the backing offset
13682ad1a3f7SMatthew Dillon 					 * should not have changed so we
13692ad1a3f7SMatthew Dillon 					 * just restart our scan.
13702ad1a3f7SMatthew Dillon 					 */
13712ad1a3f7SMatthew Dillon 					p = TAILQ_FIRST(&backing_object->memq);
13722ad1a3f7SMatthew Dillon 					continue;
13732ad1a3f7SMatthew Dillon 				}
13742ad1a3f7SMatthew Dillon 			}
13752ad1a3f7SMatthew Dillon 
13762ad1a3f7SMatthew Dillon 			/*
13772ad1a3f7SMatthew Dillon 			 * Busy the page
13782ad1a3f7SMatthew Dillon 			 */
13792ad1a3f7SMatthew Dillon 			vm_page_busy(p);
138000f9e8b4SAlan Cox 			vm_page_unlock_queues();
13812ad1a3f7SMatthew Dillon 
13822ad1a3f7SMatthew Dillon 			KASSERT(
13832ad1a3f7SMatthew Dillon 			    p->object == backing_object,
13842ad1a3f7SMatthew Dillon 			    ("vm_object_qcollapse(): object mismatch")
13852ad1a3f7SMatthew Dillon 			);
13862ad1a3f7SMatthew Dillon 
13872ad1a3f7SMatthew Dillon 			/*
13882ad1a3f7SMatthew Dillon 			 * Destroy any associated swap
13892ad1a3f7SMatthew Dillon 			 */
13902ad1a3f7SMatthew Dillon 			if (backing_object->type == OBJT_SWAP) {
13912ad1a3f7SMatthew Dillon 				swap_pager_freespace(
13922ad1a3f7SMatthew Dillon 				    backing_object,
13932ad1a3f7SMatthew Dillon 				    p->pindex,
13942ad1a3f7SMatthew Dillon 				    1
13952ad1a3f7SMatthew Dillon 				);
13962ad1a3f7SMatthew Dillon 			}
13972ad1a3f7SMatthew Dillon 
13982ad1a3f7SMatthew Dillon 			if (
13992ad1a3f7SMatthew Dillon 			    p->pindex < backing_offset_index ||
14002ad1a3f7SMatthew Dillon 			    new_pindex >= object->size
14012ad1a3f7SMatthew Dillon 			) {
14022ad1a3f7SMatthew Dillon 				/*
14032ad1a3f7SMatthew Dillon 				 * Page is out of the parent object's range, we
14042ad1a3f7SMatthew Dillon 				 * can simply destroy it.
14052ad1a3f7SMatthew Dillon 				 */
14066a684ecfSAlan Cox 				vm_page_lock_queues();
14074fec79beSAlan Cox 				pmap_remove_all(p);
14082ad1a3f7SMatthew Dillon 				vm_page_free(p);
14096a684ecfSAlan Cox 				vm_page_unlock_queues();
14102ad1a3f7SMatthew Dillon 				p = next;
14112ad1a3f7SMatthew Dillon 				continue;
14122ad1a3f7SMatthew Dillon 			}
14132ad1a3f7SMatthew Dillon 
14142ad1a3f7SMatthew Dillon 			pp = vm_page_lookup(object, new_pindex);
14152ad1a3f7SMatthew Dillon 			if (
14162ad1a3f7SMatthew Dillon 			    pp != NULL ||
14172ad1a3f7SMatthew Dillon 			    vm_pager_has_page(object, new_pindex, NULL, NULL)
14182ad1a3f7SMatthew Dillon 			) {
14192ad1a3f7SMatthew Dillon 				/*
14202ad1a3f7SMatthew Dillon 				 * page already exists in parent OR swap exists
14212ad1a3f7SMatthew Dillon 				 * for this location in the parent.  Destroy
14222ad1a3f7SMatthew Dillon 				 * the original page from the backing object.
14232ad1a3f7SMatthew Dillon 				 *
14242ad1a3f7SMatthew Dillon 				 * Leave the parent's page alone
14252ad1a3f7SMatthew Dillon 				 */
14266a684ecfSAlan Cox 				vm_page_lock_queues();
14274fec79beSAlan Cox 				pmap_remove_all(p);
14282ad1a3f7SMatthew Dillon 				vm_page_free(p);
14296a684ecfSAlan Cox 				vm_page_unlock_queues();
14302ad1a3f7SMatthew Dillon 				p = next;
14312ad1a3f7SMatthew Dillon 				continue;
14322ad1a3f7SMatthew Dillon 			}
14332ad1a3f7SMatthew Dillon 
14342ad1a3f7SMatthew Dillon 			/*
14352ad1a3f7SMatthew Dillon 			 * Page does not exist in parent, rename the
14362ad1a3f7SMatthew Dillon 			 * page from the backing object to the main object.
1437d1bf5d56SMatthew Dillon 			 *
1438d1bf5d56SMatthew Dillon 			 * If the page was mapped to a process, it can remain
1439d1bf5d56SMatthew Dillon 			 * mapped through the rename.
14402ad1a3f7SMatthew Dillon 			 */
1441a28cc55eSAlan Cox 			vm_page_lock_queues();
14422ad1a3f7SMatthew Dillon 			vm_page_rename(p, object, new_pindex);
1443a28cc55eSAlan Cox 			vm_page_unlock_queues();
14442ad1a3f7SMatthew Dillon 			/* page automatically made dirty by rename */
14452ad1a3f7SMatthew Dillon 		}
14462ad1a3f7SMatthew Dillon 		p = next;
14472ad1a3f7SMatthew Dillon 	}
14482ad1a3f7SMatthew Dillon 	splx(s);
14492ad1a3f7SMatthew Dillon 	return (r);
14502ad1a3f7SMatthew Dillon }
14512ad1a3f7SMatthew Dillon 
1452df8bae1dSRodney W. Grimes 
1453df8bae1dSRodney W. Grimes /*
14542fe6e4d7SDavid Greenman  * this version of collapse allows the operation to occur earlier and
14552fe6e4d7SDavid Greenman  * when paging_in_progress is true for an object...  This is not a complete
14562fe6e4d7SDavid Greenman  * operation, but should plug 99.9% of the rest of the leaks.
14572fe6e4d7SDavid Greenman  */
14582fe6e4d7SDavid Greenman static void
14591b40f8c0SMatthew Dillon vm_object_qcollapse(vm_object_t object)
14602fe6e4d7SDavid Greenman {
14612ad1a3f7SMatthew Dillon 	vm_object_t backing_object = object->backing_object;
14622fe6e4d7SDavid Greenman 
14631b40f8c0SMatthew Dillon 	GIANT_REQUIRED;
14641b40f8c0SMatthew Dillon 
14652fe6e4d7SDavid Greenman 	if (backing_object->ref_count != 1)
14662fe6e4d7SDavid Greenman 		return;
14672fe6e4d7SDavid Greenman 
1468010cf3b9SDavid Greenman 	backing_object->ref_count += 2;
1469010cf3b9SDavid Greenman 
14702ad1a3f7SMatthew Dillon 	vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
14711c7c3c6aSMatthew Dillon 
1472010cf3b9SDavid Greenman 	backing_object->ref_count -= 2;
14732fe6e4d7SDavid Greenman }
14742fe6e4d7SDavid Greenman 
1475df8bae1dSRodney W. Grimes /*
1476df8bae1dSRodney W. Grimes  *	vm_object_collapse:
1477df8bae1dSRodney W. Grimes  *
1478df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
1479df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
1480df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
1481df8bae1dSRodney W. Grimes  */
148226f9a767SRodney W. Grimes void
14831b40f8c0SMatthew Dillon vm_object_collapse(vm_object_t object)
1484df8bae1dSRodney W. Grimes {
14850cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
148623955314SAlfred Perlstein 
1487df8bae1dSRodney W. Grimes 	while (TRUE) {
14882ad1a3f7SMatthew Dillon 		vm_object_t backing_object;
14892ad1a3f7SMatthew Dillon 
1490df8bae1dSRodney W. Grimes 		/*
1491df8bae1dSRodney W. Grimes 		 * Verify that the conditions are right for collapse:
1492df8bae1dSRodney W. Grimes 		 *
14932ad1a3f7SMatthew Dillon 		 * The object exists and the backing object exists.
1494df8bae1dSRodney W. Grimes 		 */
14952fe6e4d7SDavid Greenman 		if (object == NULL)
14962ad1a3f7SMatthew Dillon 			break;
1497df8bae1dSRodney W. Grimes 
149824a1cce3SDavid Greenman 		if ((backing_object = object->backing_object) == NULL)
14992ad1a3f7SMatthew Dillon 			break;
1500df8bae1dSRodney W. Grimes 
1501f919ebdeSDavid Greenman 		/*
1502f919ebdeSDavid Greenman 		 * we check the backing object first, because it is most likely
150324a1cce3SDavid Greenman 		 * not collapsable.
1504f919ebdeSDavid Greenman 		 */
150524a1cce3SDavid Greenman 		if (backing_object->handle != NULL ||
150624a1cce3SDavid Greenman 		    (backing_object->type != OBJT_DEFAULT &&
150724a1cce3SDavid Greenman 		     backing_object->type != OBJT_SWAP) ||
1508f919ebdeSDavid Greenman 		    (backing_object->flags & OBJ_DEAD) ||
150924a1cce3SDavid Greenman 		    object->handle != NULL ||
151024a1cce3SDavid Greenman 		    (object->type != OBJT_DEFAULT &&
151124a1cce3SDavid Greenman 		     object->type != OBJT_SWAP) ||
151224a1cce3SDavid Greenman 		    (object->flags & OBJ_DEAD)) {
15132ad1a3f7SMatthew Dillon 			break;
151424a1cce3SDavid Greenman 		}
15159b4814bbSDavid Greenman 
15162ad1a3f7SMatthew Dillon 		if (
15172ad1a3f7SMatthew Dillon 		    object->paging_in_progress != 0 ||
15182ad1a3f7SMatthew Dillon 		    backing_object->paging_in_progress != 0
15192ad1a3f7SMatthew Dillon 		) {
1520b9921222SDavid Greenman 			vm_object_qcollapse(object);
15212ad1a3f7SMatthew Dillon 			break;
1522df8bae1dSRodney W. Grimes 		}
1523f919ebdeSDavid Greenman 
152426f9a767SRodney W. Grimes 		/*
15250d94caffSDavid Greenman 		 * We know that we can either collapse the backing object (if
15262ad1a3f7SMatthew Dillon 		 * the parent is the only reference to it) or (perhaps) have
15272ad1a3f7SMatthew Dillon 		 * the parent bypass the object if the parent happens to shadow
15282ad1a3f7SMatthew Dillon 		 * all the resident pages in the entire backing object.
15292ad1a3f7SMatthew Dillon 		 *
15302ad1a3f7SMatthew Dillon 		 * This is ignoring pager-backed pages such as swap pages.
15312ad1a3f7SMatthew Dillon 		 * vm_object_backing_scan fails the shadowing test in this
15322ad1a3f7SMatthew Dillon 		 * case.
1533df8bae1dSRodney W. Grimes 		 */
1534df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
1535df8bae1dSRodney W. Grimes 			/*
15362ad1a3f7SMatthew Dillon 			 * If there is exactly one reference to the backing
15372ad1a3f7SMatthew Dillon 			 * object, we can collapse it into the parent.
1538df8bae1dSRodney W. Grimes 			 */
15392ad1a3f7SMatthew Dillon 			vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1540df8bae1dSRodney W. Grimes 
1541df8bae1dSRodney W. Grimes 			/*
1542df8bae1dSRodney W. Grimes 			 * Move the pager from backing_object to object.
1543df8bae1dSRodney W. Grimes 			 */
1544d22bc710SAlan Cox 			VM_OBJECT_LOCK(backing_object);
15456be36525SAlan Cox 			if (backing_object->type == OBJT_SWAP) {
1546d474eaaaSDoug Rabson 				vm_object_pip_add(backing_object, 1);
1547d22bc710SAlan Cox 				VM_OBJECT_UNLOCK(backing_object);
154824a1cce3SDavid Greenman 				/*
15491c7c3c6aSMatthew Dillon 				 * scrap the paging_offset junk and do a
15501c7c3c6aSMatthew Dillon 				 * discrete copy.  This also removes major
15511c7c3c6aSMatthew Dillon 				 * assumptions about how the swap-pager
15521c7c3c6aSMatthew Dillon 				 * works from where it doesn't belong.  The
15531c7c3c6aSMatthew Dillon 				 * new swapper is able to optimize the
15541c7c3c6aSMatthew Dillon 				 * destroy-source case.
155524a1cce3SDavid Greenman 				 */
1556d22bc710SAlan Cox 				VM_OBJECT_LOCK(object);
15571c7c3c6aSMatthew Dillon 				vm_object_pip_add(object, 1);
1558d22bc710SAlan Cox 				VM_OBJECT_UNLOCK(object);
15591c7c3c6aSMatthew Dillon 				swap_pager_copy(
15601c7c3c6aSMatthew Dillon 				    backing_object,
15611c7c3c6aSMatthew Dillon 				    object,
15621c7c3c6aSMatthew Dillon 				    OFF_TO_IDX(object->backing_object_offset), TRUE);
1563d7a013c3SAlan Cox 				VM_OBJECT_LOCK(object);
1564f919ebdeSDavid Greenman 				vm_object_pip_wakeup(object);
1565d7a013c3SAlan Cox 				VM_OBJECT_UNLOCK(object);
1566c0503609SDavid Greenman 
1567d7a013c3SAlan Cox 				VM_OBJECT_LOCK(backing_object);
1568f919ebdeSDavid Greenman 				vm_object_pip_wakeup(backing_object);
1569c0503609SDavid Greenman 			}
1570df8bae1dSRodney W. Grimes 			/*
1571df8bae1dSRodney W. Grimes 			 * Object now shadows whatever backing_object did.
15722ad1a3f7SMatthew Dillon 			 * Note that the reference to
15732ad1a3f7SMatthew Dillon 			 * backing_object->backing_object moves from within
15742ad1a3f7SMatthew Dillon 			 * backing_object to within object.
1575df8bae1dSRodney W. Grimes 			 */
15761c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
15774f7c7f6eSAlan Cox 			backing_object->shadow_count--;
15784f7c7f6eSAlan Cox 			backing_object->generation++;
1579de5f6a77SJohn Dyson 			if (backing_object->backing_object) {
15806be36525SAlan Cox 				VM_OBJECT_LOCK(backing_object->backing_object);
15811c500307SAlan Cox 				LIST_REMOVE(backing_object, shadow_list);
1582eaf13dd7SJohn Dyson 				backing_object->backing_object->shadow_count--;
1583eaf13dd7SJohn Dyson 				backing_object->backing_object->generation++;
15846be36525SAlan Cox 				VM_OBJECT_UNLOCK(backing_object->backing_object);
1585de5f6a77SJohn Dyson 			}
158624a1cce3SDavid Greenman 			object->backing_object = backing_object->backing_object;
1587de5f6a77SJohn Dyson 			if (object->backing_object) {
15886be36525SAlan Cox 				VM_OBJECT_LOCK(object->backing_object);
15891c500307SAlan Cox 				LIST_INSERT_HEAD(
15902ad1a3f7SMatthew Dillon 				    &object->backing_object->shadow_head,
15912ad1a3f7SMatthew Dillon 				    object,
15922ad1a3f7SMatthew Dillon 				    shadow_list
15932ad1a3f7SMatthew Dillon 				);
1594eaf13dd7SJohn Dyson 				object->backing_object->shadow_count++;
1595eaf13dd7SJohn Dyson 				object->backing_object->generation++;
15966be36525SAlan Cox 				VM_OBJECT_UNLOCK(object->backing_object);
1597de5f6a77SJohn Dyson 			}
15982fe6e4d7SDavid Greenman 
15992ad1a3f7SMatthew Dillon 			object->backing_object_offset +=
16002ad1a3f7SMatthew Dillon 			    backing_object->backing_object_offset;
16012ad1a3f7SMatthew Dillon 
1602df8bae1dSRodney W. Grimes 			/*
1603df8bae1dSRodney W. Grimes 			 * Discard backing_object.
1604df8bae1dSRodney W. Grimes 			 *
16050d94caffSDavid Greenman 			 * Since the backing object has no pages, no pager left,
16060d94caffSDavid Greenman 			 * and no object references within it, all that is
16070d94caffSDavid Greenman 			 * necessary is to dispose of it.
1608df8bae1dSRodney W. Grimes 			 */
1609245df27cSMatthew Dillon 			KASSERT(backing_object->ref_count == 1, ("backing_object %p was somehow re-referenced during collapse!", backing_object));
1610245df27cSMatthew Dillon 			KASSERT(TAILQ_FIRST(&backing_object->memq) == NULL, ("backing_object %p somehow has left over pages during collapse!", backing_object));
16116be36525SAlan Cox 			VM_OBJECT_UNLOCK(backing_object);
1612df8bae1dSRodney W. Grimes 
1613a5698387SAlan Cox 			mtx_lock(&vm_object_list_mtx);
16142ad1a3f7SMatthew Dillon 			TAILQ_REMOVE(
16152ad1a3f7SMatthew Dillon 			    &vm_object_list,
16162ad1a3f7SMatthew Dillon 			    backing_object,
16172ad1a3f7SMatthew Dillon 			    object_list
16182ad1a3f7SMatthew Dillon 			);
1619a5698387SAlan Cox 			mtx_unlock(&vm_object_list_mtx);
1620df8bae1dSRodney W. Grimes 
1621670d17b5SJeff Roberson 			uma_zfree(obj_zone, backing_object);
1622df8bae1dSRodney W. Grimes 
1623df8bae1dSRodney W. Grimes 			object_collapses++;
16240d94caffSDavid Greenman 		} else {
162595e5e988SJohn Dyson 			vm_object_t new_backing_object;
1626df8bae1dSRodney W. Grimes 
1627df8bae1dSRodney W. Grimes 			/*
16282ad1a3f7SMatthew Dillon 			 * If we do not entirely shadow the backing object,
16292ad1a3f7SMatthew Dillon 			 * there is nothing we can do so we give up.
1630df8bae1dSRodney W. Grimes 			 */
16312ad1a3f7SMatthew Dillon 			if (vm_object_backing_scan(object, OBSC_TEST_ALL_SHADOWED) == 0) {
16322ad1a3f7SMatthew Dillon 				break;
163324a1cce3SDavid Greenman 			}
1634df8bae1dSRodney W. Grimes 
1635df8bae1dSRodney W. Grimes 			/*
16360d94caffSDavid Greenman 			 * Make the parent shadow the next object in the
16370d94caffSDavid Greenman 			 * chain.  Deallocating backing_object will not remove
16380d94caffSDavid Greenman 			 * it, since its reference count is at least 2.
1639df8bae1dSRodney W. Grimes 			 */
16406be36525SAlan Cox 			VM_OBJECT_LOCK(backing_object);
16411c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
1642eaf13dd7SJohn Dyson 			backing_object->shadow_count--;
1643eaf13dd7SJohn Dyson 			backing_object->generation++;
16446be36525SAlan Cox 			VM_OBJECT_UNLOCK(backing_object);
164595e5e988SJohn Dyson 
164695e5e988SJohn Dyson 			new_backing_object = backing_object->backing_object;
16478aef1712SMatthew Dillon 			if ((object->backing_object = new_backing_object) != NULL) {
164895e5e988SJohn Dyson 				vm_object_reference(new_backing_object);
16496be36525SAlan Cox 				VM_OBJECT_LOCK(new_backing_object);
16501c500307SAlan Cox 				LIST_INSERT_HEAD(
16512ad1a3f7SMatthew Dillon 				    &new_backing_object->shadow_head,
16522ad1a3f7SMatthew Dillon 				    object,
16532ad1a3f7SMatthew Dillon 				    shadow_list
16542ad1a3f7SMatthew Dillon 				);
1655eaf13dd7SJohn Dyson 				new_backing_object->shadow_count++;
1656eaf13dd7SJohn Dyson 				new_backing_object->generation++;
16576be36525SAlan Cox 				VM_OBJECT_UNLOCK(new_backing_object);
165895e5e988SJohn Dyson 				object->backing_object_offset +=
165995e5e988SJohn Dyson 					backing_object->backing_object_offset;
1660de5f6a77SJohn Dyson 			}
1661df8bae1dSRodney W. Grimes 
1662df8bae1dSRodney W. Grimes 			/*
16630d94caffSDavid Greenman 			 * Drop the reference count on backing_object. Since
16640d94caffSDavid Greenman 			 * its ref_count was at least 2, it will not vanish;
1665eaf13dd7SJohn Dyson 			 * so we don't need to call vm_object_deallocate, but
1666eaf13dd7SJohn Dyson 			 * we do anyway.
1667df8bae1dSRodney W. Grimes 			 */
166895e5e988SJohn Dyson 			vm_object_deallocate(backing_object);
1669df8bae1dSRodney W. Grimes 			object_bypasses++;
1670df8bae1dSRodney W. Grimes 		}
1671df8bae1dSRodney W. Grimes 
1672df8bae1dSRodney W. Grimes 		/*
1673df8bae1dSRodney W. Grimes 		 * Try again with this object's new backing object.
1674df8bae1dSRodney W. Grimes 		 */
1675df8bae1dSRodney W. Grimes 	}
1676df8bae1dSRodney W. Grimes }
1677df8bae1dSRodney W. Grimes 
1678df8bae1dSRodney W. Grimes /*
1679bff99f0dSAlan Cox  *	vm_object_page_remove:
1680df8bae1dSRodney W. Grimes  *
1681bff99f0dSAlan Cox  *	Removes all physical pages in the given range from the
1682bff99f0dSAlan Cox  *	object's list of pages.  If the range's end is zero, all
1683bff99f0dSAlan Cox  *	physical pages from the range's start to the end of the object
1684bff99f0dSAlan Cox  *	are deleted.
1685df8bae1dSRodney W. Grimes  *
1686df8bae1dSRodney W. Grimes  *	The object must be locked.
1687df8bae1dSRodney W. Grimes  */
168826f9a767SRodney W. Grimes void
1689ecde4b32SAlan Cox vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
1690ecde4b32SAlan Cox     boolean_t clean_only)
1691df8bae1dSRodney W. Grimes {
1692d031cff1SMatthew Dillon 	vm_page_t p, next;
1693df8bae1dSRodney W. Grimes 
1694ecde4b32SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1695ecde4b32SAlan Cox 	if (object->resident_page_count == 0)
1696df8bae1dSRodney W. Grimes 		return;
169795e5e988SJohn Dyson 
16988b03c8edSMatthew Dillon 	/*
16998b03c8edSMatthew Dillon 	 * Since physically-backed objects do not use managed pages, we can't
17008b03c8edSMatthew Dillon 	 * remove pages from the object (we must instead remove the page
17018b03c8edSMatthew Dillon 	 * references, and then destroy the object).
17028b03c8edSMatthew Dillon 	 */
1703ecde4b32SAlan Cox 	KASSERT(object->type != OBJT_PHYS,
1704ecde4b32SAlan Cox 	    ("attempt to remove pages from a physical object"));
17058b03c8edSMatthew Dillon 
1706d474eaaaSDoug Rabson 	vm_object_pip_add(object, 1);
170726f9a767SRodney W. Grimes again:
170832585dd6SAlan Cox 	vm_page_lock_queues();
170975741c04SAlan Cox 	if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
171075741c04SAlan Cox 		if (p->pindex < start) {
171175741c04SAlan Cox 			p = vm_page_splay(start, object->root);
171275741c04SAlan Cox 			if ((object->root = p)->pindex < start)
171375741c04SAlan Cox 				p = TAILQ_NEXT(p, listq);
171475741c04SAlan Cox 		}
171575741c04SAlan Cox 	}
171675741c04SAlan Cox 	/*
171775741c04SAlan Cox 	 * Assert: the variable p is either (1) the page with the
171875741c04SAlan Cox 	 * least pindex greater than or equal to the parameter pindex
171975741c04SAlan Cox 	 * or (2) NULL.
172075741c04SAlan Cox 	 */
172175741c04SAlan Cox 	for (;
1722bff99f0dSAlan Cox 	     p != NULL && (p->pindex < end || end == 0);
172375741c04SAlan Cox 	     p = next) {
1724b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
172575741c04SAlan Cox 
1726bd7e5f99SJohn Dyson 		if (p->wire_count != 0) {
17274fec79beSAlan Cox 			pmap_remove_all(p);
1728ce65e68cSDavid Greenman 			if (!clean_only)
1729bd7e5f99SJohn Dyson 				p->valid = 0;
17300d94caffSDavid Greenman 			continue;
17310d94caffSDavid Greenman 		}
173232585dd6SAlan Cox 		if (vm_page_sleep_if_busy(p, TRUE, "vmopar"))
173326f9a767SRodney W. Grimes 			goto again;
17348f9110f6SJohn Dyson 		if (clean_only && p->valid) {
17357c1f6cedSDavid Greenman 			vm_page_test_dirty(p);
17367c1f6cedSDavid Greenman 			if (p->valid & p->dirty)
17377c1f6cedSDavid Greenman 				continue;
17387c1f6cedSDavid Greenman 		}
1739e69763a3SDoug Rabson 		vm_page_busy(p);
17404fec79beSAlan Cox 		pmap_remove_all(p);
1741df8bae1dSRodney W. Grimes 		vm_page_free(p);
174226f9a767SRodney W. Grimes 	}
174332585dd6SAlan Cox 	vm_page_unlock_queues();
1744f919ebdeSDavid Greenman 	vm_object_pip_wakeup(object);
1745c0503609SDavid Greenman }
1746df8bae1dSRodney W. Grimes 
1747df8bae1dSRodney W. Grimes /*
1748df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
1749df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
1750df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
1751df8bae1dSRodney W. Grimes  *
1752df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
1753df8bae1dSRodney W. Grimes  *
1754df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
1755df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
1756df8bae1dSRodney W. Grimes  *
1757df8bae1dSRodney W. Grimes  *	Parameters:
1758df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
1759df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
1760df8bae1dSRodney W. Grimes  *		next_object	Second object into coalesce
1761df8bae1dSRodney W. Grimes  *		next_offset	Offset into next_object
1762df8bae1dSRodney W. Grimes  *
1763df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
1764df8bae1dSRodney W. Grimes  *		next_size	Size of reference to next_object
1765df8bae1dSRodney W. Grimes  *
1766df8bae1dSRodney W. Grimes  *	Conditions:
1767df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
1768df8bae1dSRodney W. Grimes  */
17690d94caffSDavid Greenman boolean_t
177000e1854aSAlan Cox vm_object_coalesce(vm_object_t prev_object, vm_pindex_t prev_pindex,
177100e1854aSAlan Cox 	vm_size_t prev_size, vm_size_t next_size)
1772df8bae1dSRodney W. Grimes {
1773ea41812fSAlan Cox 	vm_pindex_t next_pindex;
1774df8bae1dSRodney W. Grimes 
177500e1854aSAlan Cox 	if (prev_object == NULL)
1776df8bae1dSRodney W. Grimes 		return (TRUE);
177715c176c1SAlan Cox 	vm_object_lock(prev_object);
17784112823fSMatthew Dillon 	if (prev_object->type != OBJT_DEFAULT &&
17794112823fSMatthew Dillon 	    prev_object->type != OBJT_SWAP) {
178015c176c1SAlan Cox 		vm_object_unlock(prev_object);
178130dcfc09SJohn Dyson 		return (FALSE);
178230dcfc09SJohn Dyson 	}
178330dcfc09SJohn Dyson 
1784df8bae1dSRodney W. Grimes 	/*
1785df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
1786df8bae1dSRodney W. Grimes 	 */
1787df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
1788df8bae1dSRodney W. Grimes 
1789df8bae1dSRodney W. Grimes 	/*
17900d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
17910d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
17920d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
1793df8bae1dSRodney W. Grimes 	 */
17948cc7e047SJohn Dyson 	if (prev_object->backing_object != NULL) {
179515c176c1SAlan Cox 		vm_object_unlock(prev_object);
1796df8bae1dSRodney W. Grimes 		return (FALSE);
1797df8bae1dSRodney W. Grimes 	}
1798a316d390SJohn Dyson 
1799a316d390SJohn Dyson 	prev_size >>= PAGE_SHIFT;
1800a316d390SJohn Dyson 	next_size >>= PAGE_SHIFT;
1801ea41812fSAlan Cox 	next_pindex = prev_pindex + prev_size;
18028cc7e047SJohn Dyson 
18038cc7e047SJohn Dyson 	if ((prev_object->ref_count > 1) &&
1804ea41812fSAlan Cox 	    (prev_object->size != next_pindex)) {
180515c176c1SAlan Cox 		vm_object_unlock(prev_object);
18068cc7e047SJohn Dyson 		return (FALSE);
18078cc7e047SJohn Dyson 	}
18088cc7e047SJohn Dyson 
1809df8bae1dSRodney W. Grimes 	/*
18100d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
18110d94caffSDavid Greenman 	 * deallocation.
1812df8bae1dSRodney W. Grimes 	 */
1813ea41812fSAlan Cox 	if (next_pindex < prev_object->size) {
1814d647a0edSAlan Cox 		VM_OBJECT_LOCK(prev_object);
1815df8bae1dSRodney W. Grimes 		vm_object_page_remove(prev_object,
1816ea41812fSAlan Cox 				      next_pindex,
1817ea41812fSAlan Cox 				      next_pindex + next_size, FALSE);
1818ea41812fSAlan Cox 		if (prev_object->type == OBJT_SWAP)
1819ea41812fSAlan Cox 			swap_pager_freespace(prev_object,
1820ea41812fSAlan Cox 					     next_pindex, next_size);
1821155080d3SAlan Cox 		VM_OBJECT_UNLOCK(prev_object);
1822ea41812fSAlan Cox 	}
1823df8bae1dSRodney W. Grimes 
1824df8bae1dSRodney W. Grimes 	/*
1825df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
1826df8bae1dSRodney W. Grimes 	 */
1827ea41812fSAlan Cox 	if (next_pindex + next_size > prev_object->size)
1828ea41812fSAlan Cox 		prev_object->size = next_pindex + next_size;
1829df8bae1dSRodney W. Grimes 
183015c176c1SAlan Cox 	vm_object_unlock(prev_object);
1831df8bae1dSRodney W. Grimes 	return (TRUE);
1832df8bae1dSRodney W. Grimes }
1833df8bae1dSRodney W. Grimes 
18347a5a6352SMatthew Dillon void
18357a5a6352SMatthew Dillon vm_object_set_writeable_dirty(vm_object_t object)
18367a5a6352SMatthew Dillon {
18377a5a6352SMatthew Dillon 	struct vnode *vp;
18387a5a6352SMatthew Dillon 
18397a5a6352SMatthew Dillon 	vm_object_set_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
18407a5a6352SMatthew Dillon 	if (object->type == OBJT_VNODE &&
18417a5a6352SMatthew Dillon 	    (vp = (struct vnode *)object->handle) != NULL) {
1842e6e370a7SJeff Roberson 		VI_LOCK(vp);
1843e6e370a7SJeff Roberson 		if ((vp->v_iflag & VI_OBJDIRTY) == 0)
1844e6e370a7SJeff Roberson 			vp->v_iflag |= VI_OBJDIRTY;
1845e6e370a7SJeff Roberson 		VI_UNLOCK(vp);
18467a5a6352SMatthew Dillon 	}
18477a5a6352SMatthew Dillon }
18487a5a6352SMatthew Dillon 
1849c7c34a24SBruce Evans #include "opt_ddb.h"
1850c3cb3e12SDavid Greenman #ifdef DDB
1851c7c34a24SBruce Evans #include <sys/kernel.h>
1852c7c34a24SBruce Evans 
1853ce9edcf5SPoul-Henning Kamp #include <sys/cons.h>
1854c7c34a24SBruce Evans 
1855c7c34a24SBruce Evans #include <ddb/ddb.h>
1856c7c34a24SBruce Evans 
1857cac597e4SBruce Evans static int
18581b40f8c0SMatthew Dillon _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
1859a1f6d91cSDavid Greenman {
1860a1f6d91cSDavid Greenman 	vm_map_t tmpm;
1861a1f6d91cSDavid Greenman 	vm_map_entry_t tmpe;
1862a1f6d91cSDavid Greenman 	vm_object_t obj;
1863a1f6d91cSDavid Greenman 	int entcount;
1864a1f6d91cSDavid Greenman 
1865a1f6d91cSDavid Greenman 	if (map == 0)
1866a1f6d91cSDavid Greenman 		return 0;
1867a1f6d91cSDavid Greenman 
1868a1f6d91cSDavid Greenman 	if (entry == 0) {
1869a1f6d91cSDavid Greenman 		tmpe = map->header.next;
1870a1f6d91cSDavid Greenman 		entcount = map->nentries;
1871a1f6d91cSDavid Greenman 		while (entcount-- && (tmpe != &map->header)) {
1872a1f6d91cSDavid Greenman 			if (_vm_object_in_map(map, object, tmpe)) {
1873a1f6d91cSDavid Greenman 				return 1;
1874a1f6d91cSDavid Greenman 			}
1875a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1876a1f6d91cSDavid Greenman 		}
18779fdfe602SMatthew Dillon 	} else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
18789fdfe602SMatthew Dillon 		tmpm = entry->object.sub_map;
1879a1f6d91cSDavid Greenman 		tmpe = tmpm->header.next;
1880a1f6d91cSDavid Greenman 		entcount = tmpm->nentries;
1881a1f6d91cSDavid Greenman 		while (entcount-- && tmpe != &tmpm->header) {
1882a1f6d91cSDavid Greenman 			if (_vm_object_in_map(tmpm, object, tmpe)) {
1883a1f6d91cSDavid Greenman 				return 1;
1884a1f6d91cSDavid Greenman 			}
1885a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1886a1f6d91cSDavid Greenman 		}
18878aef1712SMatthew Dillon 	} else if ((obj = entry->object.vm_object) != NULL) {
188824a1cce3SDavid Greenman 		for (; obj; obj = obj->backing_object)
1889a1f6d91cSDavid Greenman 			if (obj == object) {
1890a1f6d91cSDavid Greenman 				return 1;
1891a1f6d91cSDavid Greenman 			}
1892a1f6d91cSDavid Greenman 	}
1893a1f6d91cSDavid Greenman 	return 0;
1894a1f6d91cSDavid Greenman }
1895a1f6d91cSDavid Greenman 
1896cac597e4SBruce Evans static int
18971b40f8c0SMatthew Dillon vm_object_in_map(vm_object_t object)
1898a1f6d91cSDavid Greenman {
1899a1f6d91cSDavid Greenman 	struct proc *p;
19001005a129SJohn Baldwin 
190160517fd1SJohn Baldwin 	/* sx_slock(&allproc_lock); */
1902fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(p, &allproc, p_list) {
1903a1f6d91cSDavid Greenman 		if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1904a1f6d91cSDavid Greenman 			continue;
1905553629ebSJake Burkholder 		if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
190660517fd1SJohn Baldwin 			/* sx_sunlock(&allproc_lock); */
1907a1f6d91cSDavid Greenman 			return 1;
1908a1f6d91cSDavid Greenman 		}
1909553629ebSJake Burkholder 	}
191060517fd1SJohn Baldwin 	/* sx_sunlock(&allproc_lock); */
1911a1f6d91cSDavid Greenman 	if (_vm_object_in_map(kernel_map, object, 0))
1912a1f6d91cSDavid Greenman 		return 1;
1913a1f6d91cSDavid Greenman 	if (_vm_object_in_map(kmem_map, object, 0))
1914a1f6d91cSDavid Greenman 		return 1;
1915a1f6d91cSDavid Greenman 	if (_vm_object_in_map(pager_map, object, 0))
1916a1f6d91cSDavid Greenman 		return 1;
1917a1f6d91cSDavid Greenman 	if (_vm_object_in_map(buffer_map, object, 0))
1918a1f6d91cSDavid Greenman 		return 1;
1919a1f6d91cSDavid Greenman 	return 0;
1920a1f6d91cSDavid Greenman }
1921a1f6d91cSDavid Greenman 
1922c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check)
1923f708ef1bSPoul-Henning Kamp {
1924a1f6d91cSDavid Greenman 	vm_object_t object;
1925a1f6d91cSDavid Greenman 
1926a1f6d91cSDavid Greenman 	/*
1927a1f6d91cSDavid Greenman 	 * make sure that internal objs are in a map somewhere
1928a1f6d91cSDavid Greenman 	 * and none have zero ref counts.
1929a1f6d91cSDavid Greenman 	 */
1930cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
193124a1cce3SDavid Greenman 		if (object->handle == NULL &&
193224a1cce3SDavid Greenman 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
1933a1f6d91cSDavid Greenman 			if (object->ref_count == 0) {
19343efc015bSPeter Wemm 				db_printf("vmochk: internal obj has zero ref count: %ld\n",
19353efc015bSPeter Wemm 					(long)object->size);
1936a1f6d91cSDavid Greenman 			}
1937a1f6d91cSDavid Greenman 			if (!vm_object_in_map(object)) {
1938fc62ef1fSBruce Evans 				db_printf(
1939fc62ef1fSBruce Evans 			"vmochk: internal obj is not in a map: "
1940fc62ef1fSBruce Evans 			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
1941fc62ef1fSBruce Evans 				    object->ref_count, (u_long)object->size,
1942fc62ef1fSBruce Evans 				    (u_long)object->size,
1943fc62ef1fSBruce Evans 				    (void *)object->backing_object);
1944a1f6d91cSDavid Greenman 			}
1945a1f6d91cSDavid Greenman 		}
1946a1f6d91cSDavid Greenman 	}
1947a1f6d91cSDavid Greenman }
1948a1f6d91cSDavid Greenman 
194926f9a767SRodney W. Grimes /*
1950df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
1951df8bae1dSRodney W. Grimes  */
1952c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static)
1953df8bae1dSRodney W. Grimes {
1954c7c34a24SBruce Evans 	/* XXX convert args. */
1955c7c34a24SBruce Evans 	vm_object_t object = (vm_object_t)addr;
1956c7c34a24SBruce Evans 	boolean_t full = have_addr;
1957c7c34a24SBruce Evans 
1958d031cff1SMatthew Dillon 	vm_page_t p;
1959df8bae1dSRodney W. Grimes 
1960c7c34a24SBruce Evans 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
1961c7c34a24SBruce Evans #define	count	was_count
1962c7c34a24SBruce Evans 
1963d031cff1SMatthew Dillon 	int count;
1964df8bae1dSRodney W. Grimes 
1965df8bae1dSRodney W. Grimes 	if (object == NULL)
1966df8bae1dSRodney W. Grimes 		return;
1967df8bae1dSRodney W. Grimes 
1968eb95adefSBruce Evans 	db_iprintf(
1969e47cd172SMaxime Henrion 	    "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x\n",
1970e47cd172SMaxime Henrion 	    object, (int)object->type, (uintmax_t)object->size,
1971eb95adefSBruce Evans 	    object->resident_page_count, object->ref_count, object->flags);
1972e47cd172SMaxime Henrion 	db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n",
19731c7c3c6aSMatthew Dillon 	    object->shadow_count,
1974eb95adefSBruce Evans 	    object->backing_object ? object->backing_object->ref_count : 0,
1975e47cd172SMaxime Henrion 	    object->backing_object, (uintmax_t)object->backing_object_offset);
1976df8bae1dSRodney W. Grimes 
1977df8bae1dSRodney W. Grimes 	if (!full)
1978df8bae1dSRodney W. Grimes 		return;
1979df8bae1dSRodney W. Grimes 
1980c7c34a24SBruce Evans 	db_indent += 2;
1981df8bae1dSRodney W. Grimes 	count = 0;
1982fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(p, &object->memq, listq) {
1983df8bae1dSRodney W. Grimes 		if (count == 0)
1984c7c34a24SBruce Evans 			db_iprintf("memory:=");
1985df8bae1dSRodney W. Grimes 		else if (count == 6) {
1986c7c34a24SBruce Evans 			db_printf("\n");
1987c7c34a24SBruce Evans 			db_iprintf(" ...");
1988df8bae1dSRodney W. Grimes 			count = 0;
1989df8bae1dSRodney W. Grimes 		} else
1990c7c34a24SBruce Evans 			db_printf(",");
1991df8bae1dSRodney W. Grimes 		count++;
1992df8bae1dSRodney W. Grimes 
1993e47cd172SMaxime Henrion 		db_printf("(off=0x%jx,page=0x%jx)",
1994e47cd172SMaxime Henrion 		    (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p));
1995df8bae1dSRodney W. Grimes 	}
1996df8bae1dSRodney W. Grimes 	if (count != 0)
1997c7c34a24SBruce Evans 		db_printf("\n");
1998c7c34a24SBruce Evans 	db_indent -= 2;
1999df8bae1dSRodney W. Grimes }
20005070c7f8SJohn Dyson 
2001c7c34a24SBruce Evans /* XXX. */
2002c7c34a24SBruce Evans #undef count
2003c7c34a24SBruce Evans 
2004c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */
20055070c7f8SJohn Dyson void
20061b40f8c0SMatthew Dillon vm_object_print(
20071b40f8c0SMatthew Dillon         /* db_expr_t */ long addr,
20081b40f8c0SMatthew Dillon 	boolean_t have_addr,
20091b40f8c0SMatthew Dillon 	/* db_expr_t */ long count,
20101b40f8c0SMatthew Dillon 	char *modif)
2011c7c34a24SBruce Evans {
2012c7c34a24SBruce Evans 	vm_object_print_static(addr, have_addr, count, modif);
2013c7c34a24SBruce Evans }
2014c7c34a24SBruce Evans 
2015c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
20165070c7f8SJohn Dyson {
20175070c7f8SJohn Dyson 	vm_object_t object;
20185070c7f8SJohn Dyson 	int nl = 0;
20195070c7f8SJohn Dyson 	int c;
2020cc64b484SAlfred Perlstein 
2021cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
20225070c7f8SJohn Dyson 		vm_pindex_t idx, fidx;
20235070c7f8SJohn Dyson 		vm_pindex_t osize;
2024227f9a1cSJake Burkholder 		vm_paddr_t pa = -1, padiff;
20255070c7f8SJohn Dyson 		int rcount;
20265070c7f8SJohn Dyson 		vm_page_t m;
20275070c7f8SJohn Dyson 
2028fc62ef1fSBruce Evans 		db_printf("new object: %p\n", (void *)object);
20295070c7f8SJohn Dyson 		if (nl > 18) {
20305070c7f8SJohn Dyson 			c = cngetc();
20315070c7f8SJohn Dyson 			if (c != ' ')
20325070c7f8SJohn Dyson 				return;
20335070c7f8SJohn Dyson 			nl = 0;
20345070c7f8SJohn Dyson 		}
20355070c7f8SJohn Dyson 		nl++;
20365070c7f8SJohn Dyson 		rcount = 0;
20375070c7f8SJohn Dyson 		fidx = 0;
20385070c7f8SJohn Dyson 		osize = object->size;
20395070c7f8SJohn Dyson 		if (osize > 128)
20405070c7f8SJohn Dyson 			osize = 128;
20415070c7f8SJohn Dyson 		for (idx = 0; idx < osize; idx++) {
20425070c7f8SJohn Dyson 			m = vm_page_lookup(object, idx);
20435070c7f8SJohn Dyson 			if (m == NULL) {
20445070c7f8SJohn Dyson 				if (rcount) {
20453efc015bSPeter Wemm 					db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
20463efc015bSPeter Wemm 						(long)fidx, rcount, (long)pa);
20475070c7f8SJohn Dyson 					if (nl > 18) {
20485070c7f8SJohn Dyson 						c = cngetc();
20495070c7f8SJohn Dyson 						if (c != ' ')
20505070c7f8SJohn Dyson 							return;
20515070c7f8SJohn Dyson 						nl = 0;
20525070c7f8SJohn Dyson 					}
20535070c7f8SJohn Dyson 					nl++;
20545070c7f8SJohn Dyson 					rcount = 0;
20555070c7f8SJohn Dyson 				}
20565070c7f8SJohn Dyson 				continue;
20575070c7f8SJohn Dyson 			}
20585070c7f8SJohn Dyson 
20595070c7f8SJohn Dyson 
20605070c7f8SJohn Dyson 			if (rcount &&
20615070c7f8SJohn Dyson 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
20625070c7f8SJohn Dyson 				++rcount;
20635070c7f8SJohn Dyson 				continue;
20645070c7f8SJohn Dyson 			}
20655070c7f8SJohn Dyson 			if (rcount) {
20665070c7f8SJohn Dyson 				padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
20675070c7f8SJohn Dyson 				padiff >>= PAGE_SHIFT;
20685070c7f8SJohn Dyson 				padiff &= PQ_L2_MASK;
20695070c7f8SJohn Dyson 				if (padiff == 0) {
20705070c7f8SJohn Dyson 					pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
20715070c7f8SJohn Dyson 					++rcount;
20725070c7f8SJohn Dyson 					continue;
20735070c7f8SJohn Dyson 				}
20743efc015bSPeter Wemm 				db_printf(" index(%ld)run(%d)pa(0x%lx)",
20753efc015bSPeter Wemm 					(long)fidx, rcount, (long)pa);
20763efc015bSPeter Wemm 				db_printf("pd(%ld)\n", (long)padiff);
20775070c7f8SJohn Dyson 				if (nl > 18) {
20785070c7f8SJohn Dyson 					c = cngetc();
20795070c7f8SJohn Dyson 					if (c != ' ')
20805070c7f8SJohn Dyson 						return;
20815070c7f8SJohn Dyson 					nl = 0;
20825070c7f8SJohn Dyson 				}
20835070c7f8SJohn Dyson 				nl++;
20845070c7f8SJohn Dyson 			}
20855070c7f8SJohn Dyson 			fidx = idx;
20865070c7f8SJohn Dyson 			pa = VM_PAGE_TO_PHYS(m);
20875070c7f8SJohn Dyson 			rcount = 1;
20885070c7f8SJohn Dyson 		}
20895070c7f8SJohn Dyson 		if (rcount) {
20903efc015bSPeter Wemm 			db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
20913efc015bSPeter Wemm 				(long)fidx, rcount, (long)pa);
20925070c7f8SJohn Dyson 			if (nl > 18) {
20935070c7f8SJohn Dyson 				c = cngetc();
20945070c7f8SJohn Dyson 				if (c != ' ')
20955070c7f8SJohn Dyson 					return;
20965070c7f8SJohn Dyson 				nl = 0;
20975070c7f8SJohn Dyson 			}
20985070c7f8SJohn Dyson 			nl++;
20995070c7f8SJohn Dyson 		}
21005070c7f8SJohn Dyson 	}
21015070c7f8SJohn Dyson }
2102c3cb3e12SDavid Greenman #endif /* DDB */
2103