xref: /freebsd/sys/vm/vm_object.c (revision 5440b5a97411f51889d3bdf37b8c97dc5f87d054)
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>
81e47cd172SMaxime Henrion #include <sys/stdint.h>
82fb919e4dSMark Murray #include <sys/vnode.h>
83fb919e4dSMark Murray #include <sys/vmmeter.h>
841005a129SJohn Baldwin #include <sys/sx.h>
85df8bae1dSRodney W. Grimes 
86df8bae1dSRodney W. Grimes #include <vm/vm.h>
87efeaf95aSDavid Greenman #include <vm/vm_param.h>
88efeaf95aSDavid Greenman #include <vm/pmap.h>
89efeaf95aSDavid Greenman #include <vm/vm_map.h>
90efeaf95aSDavid Greenman #include <vm/vm_object.h>
91df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
9226f9a767SRodney W. Grimes #include <vm/vm_pageout.h>
930d94caffSDavid Greenman #include <vm/vm_pager.h>
9405f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h>
95a1f6d91cSDavid Greenman #include <vm/vm_kern.h>
96efeaf95aSDavid Greenman #include <vm/vm_extern.h>
97670d17b5SJeff Roberson #include <vm/uma.h>
9826f9a767SRodney W. Grimes 
99b9b7a4beSMatthew Dillon #define EASY_SCAN_FACTOR       8
100b9b7a4beSMatthew Dillon 
101b9b7a4beSMatthew Dillon #define MSYNC_FLUSH_HARDSEQ	0x01
102b9b7a4beSMatthew Dillon #define MSYNC_FLUSH_SOFTSEQ	0x02
103b9b7a4beSMatthew Dillon 
104b9b7a4beSMatthew Dillon /*
105b9b7a4beSMatthew Dillon  * msync / VM object flushing optimizations
106b9b7a4beSMatthew Dillon  */
107b9b7a4beSMatthew Dillon static int msync_flush_flags = MSYNC_FLUSH_HARDSEQ | MSYNC_FLUSH_SOFTSEQ;
108b9b7a4beSMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, msync_flush_flags,
109b9b7a4beSMatthew Dillon         CTLFLAG_RW, &msync_flush_flags, 0, "");
110b9b7a4beSMatthew Dillon 
111b9b7a4beSMatthew Dillon static void	vm_object_qcollapse(vm_object_t object);
112b9b7a4beSMatthew Dillon static int	vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags);
113f6b04d2bSDavid Greenman 
114df8bae1dSRodney W. Grimes /*
115df8bae1dSRodney W. Grimes  *	Virtual memory objects maintain the actual data
116df8bae1dSRodney W. Grimes  *	associated with allocated virtual memory.  A given
117df8bae1dSRodney W. Grimes  *	page of memory exists within exactly one object.
118df8bae1dSRodney W. Grimes  *
119df8bae1dSRodney W. Grimes  *	An object is only deallocated when all "references"
120df8bae1dSRodney W. Grimes  *	are given up.  Only one "reference" to a given
121df8bae1dSRodney W. Grimes  *	region of an object should be writeable.
122df8bae1dSRodney W. Grimes  *
123df8bae1dSRodney W. Grimes  *	Associated with each object is a list of all resident
124df8bae1dSRodney W. Grimes  *	memory pages belonging to that object; this list is
125df8bae1dSRodney W. Grimes  *	maintained by the "vm_page" module, and locked by the object's
126df8bae1dSRodney W. Grimes  *	lock.
127df8bae1dSRodney W. Grimes  *
128df8bae1dSRodney W. Grimes  *	Each object also records a "pager" routine which is
129df8bae1dSRodney W. Grimes  *	used to retrieve (and store) pages to the proper backing
130df8bae1dSRodney W. Grimes  *	storage.  In addition, objects may be backed by other
131df8bae1dSRodney W. Grimes  *	objects from which they were virtual-copied.
132df8bae1dSRodney W. Grimes  *
133df8bae1dSRodney W. Grimes  *	The only items within the object structure which are
134df8bae1dSRodney W. Grimes  *	modified after time of creation are:
135df8bae1dSRodney W. Grimes  *		reference count		locked by object's lock
136df8bae1dSRodney W. Grimes  *		pager routine		locked by object's lock
137df8bae1dSRodney W. Grimes  *
138df8bae1dSRodney W. Grimes  */
139df8bae1dSRodney W. Grimes 
14028f8db14SBruce Evans struct object_q vm_object_list;
141a5698387SAlan Cox struct mtx vm_object_list_mtx;	/* lock for object list and count */
14228f8db14SBruce Evans vm_object_t kernel_object;
14328f8db14SBruce Evans vm_object_t kmem_object;
144f708ef1bSPoul-Henning Kamp static struct vm_object kernel_object_store;
145f708ef1bSPoul-Henning Kamp static struct vm_object kmem_object_store;
146aef922f5SJohn Dyson extern int vm_pageout_page_count;
147df8bae1dSRodney W. Grimes 
148f708ef1bSPoul-Henning Kamp static long object_collapses;
149f708ef1bSPoul-Henning Kamp static long object_bypasses;
150dcc5840eSAlan Cox static int next_index;
151670d17b5SJeff Roberson static uma_zone_t obj_zone;
15299448ed1SJohn Dyson #define VM_OBJECTS_INIT 256
1538355f576SJeff Roberson 
1548355f576SJeff Roberson static void vm_object_zinit(void *mem, int size);
1558355f576SJeff Roberson 
1568355f576SJeff Roberson #ifdef INVARIANTS
1578355f576SJeff Roberson static void vm_object_zdtor(void *mem, int size, void *arg);
1588355f576SJeff Roberson 
1598355f576SJeff Roberson static void
1608355f576SJeff Roberson vm_object_zdtor(void *mem, int size, void *arg)
1618355f576SJeff Roberson {
1628355f576SJeff Roberson 	vm_object_t object;
1638355f576SJeff Roberson 
1648355f576SJeff Roberson 	object = (vm_object_t)mem;
1658355f576SJeff Roberson 	KASSERT(object->paging_in_progress == 0,
1668355f576SJeff Roberson 	    ("object %p paging_in_progress = %d",
1678355f576SJeff Roberson 	    object, object->paging_in_progress));
1688355f576SJeff Roberson 	KASSERT(object->resident_page_count == 0,
1698355f576SJeff Roberson 	    ("object %p resident_page_count = %d",
1708355f576SJeff Roberson 	    object, object->resident_page_count));
1718355f576SJeff Roberson 	KASSERT(object->shadow_count == 0,
1728355f576SJeff Roberson 	    ("object %p shadow_count = %d",
1738355f576SJeff Roberson 	    object, object->shadow_count));
1748355f576SJeff Roberson }
1758355f576SJeff Roberson #endif
1768355f576SJeff Roberson 
1778355f576SJeff Roberson static void
1788355f576SJeff Roberson vm_object_zinit(void *mem, int size)
1798355f576SJeff Roberson {
1808355f576SJeff Roberson 	vm_object_t object;
1818355f576SJeff Roberson 
1828355f576SJeff Roberson 	object = (vm_object_t)mem;
183e3a9e1b2SAlan Cox 	bzero(&object->mtx, sizeof(object->mtx));
1848355f576SJeff Roberson 
1858355f576SJeff Roberson 	/* These are true for any object that has been freed */
1868355f576SJeff Roberson 	object->paging_in_progress = 0;
1878355f576SJeff Roberson 	object->resident_page_count = 0;
1888355f576SJeff Roberson 	object->shadow_count = 0;
1898355f576SJeff Roberson }
190df8bae1dSRodney W. Grimes 
1913075778bSJohn Dyson void
1926395da54SIan Dowse _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
193df8bae1dSRodney W. Grimes {
194cf3e6e48SAlan Cox 	int incr;
1950cddd8f0SMatthew Dillon 
1964b420d50SAlan Cox 	mtx_init(&object->mtx, "vm object", NULL, MTX_DEF);
1974b420d50SAlan Cox 
198df8bae1dSRodney W. Grimes 	TAILQ_INIT(&object->memq);
19924a1cce3SDavid Greenman 	TAILQ_INIT(&object->shadow_head);
200a1f6d91cSDavid Greenman 
201b86ec922SMatthew Dillon 	object->root = NULL;
20224a1cce3SDavid Greenman 	object->type = type;
203df8bae1dSRodney W. Grimes 	object->size = size;
204a1f6d91cSDavid Greenman 	object->ref_count = 1;
20524a1cce3SDavid Greenman 	object->flags = 0;
20660517fd1SJohn Baldwin 	if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
20760517fd1SJohn Baldwin 		vm_object_set_flag(object, OBJ_ONEMAPPING);
20899448ed1SJohn Dyson 	if (size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
20999448ed1SJohn Dyson 		incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
21099448ed1SJohn Dyson 	else
21199448ed1SJohn Dyson 		incr = size;
212dcc5840eSAlan Cox 	do
213dcc5840eSAlan Cox 		object->pg_color = next_index;
214dcc5840eSAlan Cox 	while (!atomic_cmpset_int(&next_index, object->pg_color,
21579660d83SAlan Cox 				  (object->pg_color + incr) & PQ_L2_MASK));
21624a1cce3SDavid Greenman 	object->handle = NULL;
21724a1cce3SDavid Greenman 	object->backing_object = NULL;
218a316d390SJohn Dyson 	object->backing_object_offset = (vm_ooffset_t) 0;
219a1f6d91cSDavid Greenman 
22081f71edaSMatt Jacob 	atomic_add_int(&object->generation, 1);
22160517fd1SJohn Baldwin 
222a5698387SAlan Cox 	mtx_lock(&vm_object_list_mtx);
22360517fd1SJohn Baldwin 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
224a5698387SAlan Cox 	mtx_unlock(&vm_object_list_mtx);
225df8bae1dSRodney W. Grimes }
226df8bae1dSRodney W. Grimes 
227df8bae1dSRodney W. Grimes /*
22826f9a767SRodney W. Grimes  *	vm_object_init:
22926f9a767SRodney W. Grimes  *
23026f9a767SRodney W. Grimes  *	Initialize the VM objects module.
23126f9a767SRodney W. Grimes  */
23226f9a767SRodney W. Grimes void
2331b40f8c0SMatthew Dillon vm_object_init(void)
23426f9a767SRodney W. Grimes {
23526f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_list);
2366008862bSJohn Baldwin 	mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF);
2370217125fSDavid Greenman 
23826f9a767SRodney W. Grimes 	kernel_object = &kernel_object_store;
239a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
24026f9a767SRodney W. Grimes 	    kernel_object);
24126f9a767SRodney W. Grimes 
24226f9a767SRodney W. Grimes 	kmem_object = &kmem_object_store;
243a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
24426f9a767SRodney W. Grimes 	    kmem_object);
2458355f576SJeff Roberson 	obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL,
2468355f576SJeff Roberson #ifdef INVARIANTS
2478355f576SJeff Roberson 	    vm_object_zdtor,
2488355f576SJeff Roberson #else
2498355f576SJeff Roberson 	    NULL,
2508355f576SJeff Roberson #endif
2518355f576SJeff Roberson 	    vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
2528355f576SJeff Roberson 	uma_prealloc(obj_zone, VM_OBJECTS_INIT);
25399448ed1SJohn Dyson }
25499448ed1SJohn Dyson 
25599448ed1SJohn Dyson void
2561b40f8c0SMatthew Dillon vm_object_set_flag(vm_object_t object, u_short bits)
2571b40f8c0SMatthew Dillon {
258b06805adSJake Burkholder 	object->flags |= bits;
2591b40f8c0SMatthew Dillon }
2601b40f8c0SMatthew Dillon 
2611b40f8c0SMatthew Dillon void
2621b40f8c0SMatthew Dillon vm_object_clear_flag(vm_object_t object, u_short bits)
2631b40f8c0SMatthew Dillon {
2645440b5a9SAlan Cox 
2655440b5a9SAlan Cox 	mtx_assert(object == kmem_object ? &object->mtx : &Giant, MA_OWNED);
266b06805adSJake Burkholder 	object->flags &= ~bits;
2671b40f8c0SMatthew Dillon }
2681b40f8c0SMatthew Dillon 
2691b40f8c0SMatthew Dillon void
2701b40f8c0SMatthew Dillon vm_object_pip_add(vm_object_t object, short i)
2711b40f8c0SMatthew Dillon {
2721b40f8c0SMatthew Dillon 	GIANT_REQUIRED;
273b06805adSJake Burkholder 	object->paging_in_progress += i;
2741b40f8c0SMatthew Dillon }
2751b40f8c0SMatthew Dillon 
2761b40f8c0SMatthew Dillon void
2771b40f8c0SMatthew Dillon vm_object_pip_subtract(vm_object_t object, short i)
2781b40f8c0SMatthew Dillon {
2791b40f8c0SMatthew Dillon 	GIANT_REQUIRED;
280b06805adSJake Burkholder 	object->paging_in_progress -= i;
2811b40f8c0SMatthew Dillon }
2821b40f8c0SMatthew Dillon 
2831b40f8c0SMatthew Dillon void
2841b40f8c0SMatthew Dillon vm_object_pip_wakeup(vm_object_t object)
2851b40f8c0SMatthew Dillon {
2861b40f8c0SMatthew Dillon 	GIANT_REQUIRED;
287b06805adSJake Burkholder 	object->paging_in_progress--;
2881b40f8c0SMatthew Dillon 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
2891b40f8c0SMatthew Dillon 		vm_object_clear_flag(object, OBJ_PIPWNT);
2901b40f8c0SMatthew Dillon 		wakeup(object);
2911b40f8c0SMatthew Dillon 	}
2921b40f8c0SMatthew Dillon }
2931b40f8c0SMatthew Dillon 
2941b40f8c0SMatthew Dillon void
2951b40f8c0SMatthew Dillon vm_object_pip_wakeupn(vm_object_t object, short i)
2961b40f8c0SMatthew Dillon {
2971b40f8c0SMatthew Dillon 	GIANT_REQUIRED;
2981b40f8c0SMatthew Dillon 	if (i)
299b06805adSJake Burkholder 		object->paging_in_progress -= i;
3001b40f8c0SMatthew Dillon 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
3011b40f8c0SMatthew Dillon 		vm_object_clear_flag(object, OBJ_PIPWNT);
3021b40f8c0SMatthew Dillon 		wakeup(object);
3031b40f8c0SMatthew Dillon 	}
3041b40f8c0SMatthew Dillon }
3051b40f8c0SMatthew Dillon 
3061b40f8c0SMatthew Dillon void
3071b40f8c0SMatthew Dillon vm_object_pip_sleep(vm_object_t object, char *waitid)
3081b40f8c0SMatthew Dillon {
3091b40f8c0SMatthew Dillon 	GIANT_REQUIRED;
3101b40f8c0SMatthew Dillon 	if (object->paging_in_progress) {
3111b40f8c0SMatthew Dillon 		int s = splvm();
3121b40f8c0SMatthew Dillon 		if (object->paging_in_progress) {
3131b40f8c0SMatthew Dillon 			vm_object_set_flag(object, OBJ_PIPWNT);
3141b40f8c0SMatthew Dillon 			tsleep(object, PVM, waitid, 0);
3151b40f8c0SMatthew Dillon 		}
3161b40f8c0SMatthew Dillon 		splx(s);
3171b40f8c0SMatthew Dillon 	}
3181b40f8c0SMatthew Dillon }
3191b40f8c0SMatthew Dillon 
3201b40f8c0SMatthew Dillon void
3211b40f8c0SMatthew Dillon vm_object_pip_wait(vm_object_t object, char *waitid)
3221b40f8c0SMatthew Dillon {
3231b40f8c0SMatthew Dillon 	GIANT_REQUIRED;
3241b40f8c0SMatthew Dillon 	while (object->paging_in_progress)
3251b40f8c0SMatthew Dillon 		vm_object_pip_sleep(object, waitid);
3261b40f8c0SMatthew Dillon }
3271b40f8c0SMatthew Dillon 
32826f9a767SRodney W. Grimes /*
32998cb733cSKenneth D. Merry  *	vm_object_allocate_wait
33098cb733cSKenneth D. Merry  *
33198cb733cSKenneth D. Merry  *	Return a new object with the given size, and give the user the
33298cb733cSKenneth D. Merry  *	option of waiting for it to complete or failing if the needed
33398cb733cSKenneth D. Merry  *	memory isn't available.
33498cb733cSKenneth D. Merry  */
33598cb733cSKenneth D. Merry vm_object_t
33698cb733cSKenneth D. Merry vm_object_allocate_wait(objtype_t type, vm_pindex_t size, int flags)
33798cb733cSKenneth D. Merry {
33898cb733cSKenneth D. Merry 	vm_object_t result;
33998cb733cSKenneth D. Merry 
34098cb733cSKenneth D. Merry 	result = (vm_object_t) uma_zalloc(obj_zone, flags);
34198cb733cSKenneth D. Merry 
34298cb733cSKenneth D. Merry 	if (result != NULL)
34398cb733cSKenneth D. Merry 		_vm_object_allocate(type, size, result);
34498cb733cSKenneth D. Merry 
34598cb733cSKenneth D. Merry 	return (result);
34698cb733cSKenneth D. Merry }
34798cb733cSKenneth D. Merry 
34898cb733cSKenneth D. Merry /*
34926f9a767SRodney W. Grimes  *	vm_object_allocate:
35026f9a767SRodney W. Grimes  *
35126f9a767SRodney W. Grimes  *	Returns a new object with the given size.
35226f9a767SRodney W. Grimes  */
35326f9a767SRodney W. Grimes vm_object_t
3546395da54SIan Dowse vm_object_allocate(objtype_t type, vm_pindex_t size)
35526f9a767SRodney W. Grimes {
35698cb733cSKenneth D. Merry 	return(vm_object_allocate_wait(type, size, M_WAITOK));
35726f9a767SRodney W. Grimes }
35826f9a767SRodney W. Grimes 
35926f9a767SRodney W. Grimes 
36026f9a767SRodney W. Grimes /*
361df8bae1dSRodney W. Grimes  *	vm_object_reference:
362df8bae1dSRodney W. Grimes  *
363df8bae1dSRodney W. Grimes  *	Gets another reference to the given object.
364df8bae1dSRodney W. Grimes  */
3656476c0d2SJohn Dyson void
3661b40f8c0SMatthew Dillon vm_object_reference(vm_object_t object)
367df8bae1dSRodney W. Grimes {
368df8bae1dSRodney W. Grimes 	if (object == NULL)
369df8bae1dSRodney W. Grimes 		return;
37095e5e988SJohn Dyson 
37115c176c1SAlan Cox 	vm_object_lock(object);
372245df27cSMatthew Dillon #if 0
373245df27cSMatthew Dillon 	/* object can be re-referenced during final cleaning */
3745526d2d9SEivind Eklund 	KASSERT(!(object->flags & OBJ_DEAD),
3755526d2d9SEivind Eklund 	    ("vm_object_reference: attempting to reference dead obj"));
376245df27cSMatthew Dillon #endif
37795e5e988SJohn Dyson 
378df8bae1dSRodney W. Grimes 	object->ref_count++;
37947221757SJohn Dyson 	if (object->type == OBJT_VNODE) {
3803ef3e7c4SJeff Roberson 		while (vget((struct vnode *) object->handle, LK_RETRY, curthread)) {
38147221757SJohn Dyson 			printf("vm_object_reference: delay in getting object\n");
38247221757SJohn Dyson 		}
38347221757SJohn Dyson 	}
38415c176c1SAlan Cox 	vm_object_unlock(object);
38595e5e988SJohn Dyson }
38695e5e988SJohn Dyson 
38723955314SAlfred Perlstein /*
3889d5abbddSJens Schweikhardt  * Handle deallocating an object of type OBJT_VNODE.
38923955314SAlfred Perlstein  */
390bf27292bSJohn Dyson void
3911b40f8c0SMatthew Dillon vm_object_vndeallocate(vm_object_t object)
39295e5e988SJohn Dyson {
39395e5e988SJohn Dyson 	struct vnode *vp = (struct vnode *) object->handle;
394219cbf59SEivind Eklund 
3950cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
3965526d2d9SEivind Eklund 	KASSERT(object->type == OBJT_VNODE,
3975526d2d9SEivind Eklund 	    ("vm_object_vndeallocate: not a vnode object"));
398219cbf59SEivind Eklund 	KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
399219cbf59SEivind Eklund #ifdef INVARIANTS
40095e5e988SJohn Dyson 	if (object->ref_count == 0) {
40195e5e988SJohn Dyson 		vprint("vm_object_vndeallocate", vp);
40295e5e988SJohn Dyson 		panic("vm_object_vndeallocate: bad object reference count");
40395e5e988SJohn Dyson 	}
40495e5e988SJohn Dyson #endif
40595e5e988SJohn Dyson 
40695e5e988SJohn Dyson 	object->ref_count--;
40747221757SJohn Dyson 	if (object->ref_count == 0) {
408e6e370a7SJeff Roberson 		mp_fixme("Unlocked vflag access.");
409e6e370a7SJeff Roberson 		vp->v_vflag &= ~VV_TEXT;
410c0b6bbb8SAlan Cox #ifdef ENABLE_VFS_IOOPT
411069e9bc1SDoug Rabson 		vm_object_clear_flag(object, OBJ_OPT);
412c0b6bbb8SAlan Cox #endif
4132be70f79SJohn Dyson 	}
41423955314SAlfred Perlstein 	/*
41523955314SAlfred Perlstein 	 * vrele may need a vop lock
41623955314SAlfred Perlstein 	 */
41747221757SJohn Dyson 	vrele(vp);
418df8bae1dSRodney W. Grimes }
419df8bae1dSRodney W. Grimes 
420df8bae1dSRodney W. Grimes /*
421df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
422df8bae1dSRodney W. Grimes  *
423df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
424df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
425df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
426df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
427df8bae1dSRodney W. Grimes  *	may be relinquished.
428df8bae1dSRodney W. Grimes  *
429df8bae1dSRodney W. Grimes  *	No object may be locked.
430df8bae1dSRodney W. Grimes  */
43126f9a767SRodney W. Grimes void
4321b40f8c0SMatthew Dillon vm_object_deallocate(vm_object_t object)
433df8bae1dSRodney W. Grimes {
434df8bae1dSRodney W. Grimes 	vm_object_t temp;
435df8bae1dSRodney W. Grimes 
43647c3ccc4SAlan Cox 	mtx_lock(&Giant);
437df8bae1dSRodney W. Grimes 	while (object != NULL) {
438df8bae1dSRodney W. Grimes 
43995e5e988SJohn Dyson 		if (object->type == OBJT_VNODE) {
44095e5e988SJohn Dyson 			vm_object_vndeallocate(object);
44147c3ccc4SAlan Cox 			mtx_unlock(&Giant);
44295e5e988SJohn Dyson 			return;
44395e5e988SJohn Dyson 		}
44495e5e988SJohn Dyson 
4458125b1e6SAlfred Perlstein 		KASSERT(object->ref_count != 0,
4468125b1e6SAlfred Perlstein 			("vm_object_deallocate: object deallocated too many times: %d", object->type));
4472be70f79SJohn Dyson 
4482be70f79SJohn Dyson 		/*
4498125b1e6SAlfred Perlstein 		 * If the reference count goes to 0 we start calling
4508125b1e6SAlfred Perlstein 		 * vm_object_terminate() on the object chain.
4518125b1e6SAlfred Perlstein 		 * A ref count of 1 may be a special case depending on the
4528125b1e6SAlfred Perlstein 		 * shadow count being 0 or 1.
4532be70f79SJohn Dyson 		 */
454c0877f10SJohn Dyson 		object->ref_count--;
4558125b1e6SAlfred Perlstein 		if (object->ref_count > 1) {
45647c3ccc4SAlan Cox 			mtx_unlock(&Giant);
457c0877f10SJohn Dyson 			return;
4588125b1e6SAlfred Perlstein 		} else if (object->ref_count == 1) {
4598125b1e6SAlfred Perlstein 			if (object->shadow_count == 0) {
4608125b1e6SAlfred Perlstein 				vm_object_set_flag(object, OBJ_ONEMAPPING);
4618125b1e6SAlfred Perlstein 			} else if ((object->shadow_count == 1) &&
4628125b1e6SAlfred Perlstein 			    (object->handle == NULL) &&
46324a1cce3SDavid Greenman 			    (object->type == OBJT_DEFAULT ||
46424a1cce3SDavid Greenman 			     object->type == OBJT_SWAP)) {
465a1f6d91cSDavid Greenman 				vm_object_t robject;
46695e5e988SJohn Dyson 
467b18bfc3dSJohn Dyson 				robject = TAILQ_FIRST(&object->shadow_head);
4685526d2d9SEivind Eklund 				KASSERT(robject != NULL,
469219cbf59SEivind Eklund 				    ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
4705526d2d9SEivind Eklund 					 object->ref_count,
4715526d2d9SEivind Eklund 					 object->shadow_count));
47295e5e988SJohn Dyson 				if ((robject->handle == NULL) &&
47324a1cce3SDavid Greenman 				    (robject->type == OBJT_DEFAULT ||
47424a1cce3SDavid Greenman 				     robject->type == OBJT_SWAP)) {
475a1f6d91cSDavid Greenman 
47695e5e988SJohn Dyson 					robject->ref_count++;
47795e5e988SJohn Dyson 
4781c7c3c6aSMatthew Dillon 					while (
4791c7c3c6aSMatthew Dillon 						robject->paging_in_progress ||
4801c7c3c6aSMatthew Dillon 						object->paging_in_progress
4811c7c3c6aSMatthew Dillon 					) {
48266095752SJohn Dyson 						vm_object_pip_sleep(robject, "objde1");
48366095752SJohn Dyson 						vm_object_pip_sleep(object, "objde2");
484a1f6d91cSDavid Greenman 					}
485a1f6d91cSDavid Greenman 
48695e5e988SJohn Dyson 					if (robject->ref_count == 1) {
48795e5e988SJohn Dyson 						robject->ref_count--;
488ba8da839SDavid Greenman 						object = robject;
48995e5e988SJohn Dyson 						goto doterm;
49095e5e988SJohn Dyson 					}
49195e5e988SJohn Dyson 
49295e5e988SJohn Dyson 					object = robject;
49395e5e988SJohn Dyson 					vm_object_collapse(object);
494ba8da839SDavid Greenman 					continue;
495a1f6d91cSDavid Greenman 				}
49695e5e988SJohn Dyson 			}
49747c3ccc4SAlan Cox 			mtx_unlock(&Giant);
498a1f6d91cSDavid Greenman 			return;
49995e5e988SJohn Dyson 		}
50095e5e988SJohn Dyson doterm:
50124a1cce3SDavid Greenman 		temp = object->backing_object;
502de5f6a77SJohn Dyson 		if (temp) {
50324a1cce3SDavid Greenman 			TAILQ_REMOVE(&temp->shadow_head, object, shadow_list);
50495e5e988SJohn Dyson 			temp->shadow_count--;
505c0b6bbb8SAlan Cox #ifdef ENABLE_VFS_IOOPT
5062d8acc0fSJohn Dyson 			if (temp->ref_count == 0)
507069e9bc1SDoug Rabson 				vm_object_clear_flag(temp, OBJ_OPT);
508c0b6bbb8SAlan Cox #endif
509eaf13dd7SJohn Dyson 			temp->generation++;
51095461b45SJohn Dyson 			object->backing_object = NULL;
511de5f6a77SJohn Dyson 		}
512245df27cSMatthew Dillon 		/*
513245df27cSMatthew Dillon 		 * Don't double-terminate, we could be in a termination
514245df27cSMatthew Dillon 		 * recursion due to the terminate having to sync data
515245df27cSMatthew Dillon 		 * to disk.
516245df27cSMatthew Dillon 		 */
517245df27cSMatthew Dillon 		if ((object->flags & OBJ_DEAD) == 0)
518df8bae1dSRodney W. Grimes 			vm_object_terminate(object);
519df8bae1dSRodney W. Grimes 		object = temp;
520df8bae1dSRodney W. Grimes 	}
52147c3ccc4SAlan Cox 	mtx_unlock(&Giant);
522df8bae1dSRodney W. Grimes }
523df8bae1dSRodney W. Grimes 
524df8bae1dSRodney W. Grimes /*
525df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
526df8bae1dSRodney W. Grimes  *	up all previously used resources.
527df8bae1dSRodney W. Grimes  *
528df8bae1dSRodney W. Grimes  *	The object must be locked.
5291c7c3c6aSMatthew Dillon  *	This routine may block.
530df8bae1dSRodney W. Grimes  */
53195e5e988SJohn Dyson void
5321b40f8c0SMatthew Dillon vm_object_terminate(vm_object_t object)
533df8bae1dSRodney W. Grimes {
534d031cff1SMatthew Dillon 	vm_page_t p;
535e4b7635dSDavid Greenman 	int s;
536df8bae1dSRodney W. Grimes 
5370cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
5380cddd8f0SMatthew Dillon 
53995e5e988SJohn Dyson 	/*
54095e5e988SJohn Dyson 	 * Make sure no one uses us.
54195e5e988SJohn Dyson 	 */
542069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_DEAD);
5433c631446SJohn Dyson 
544df8bae1dSRodney W. Grimes 	/*
545f6b04d2bSDavid Greenman 	 * wait for the pageout daemon to be done with the object
546df8bae1dSRodney W. Grimes 	 */
54766095752SJohn Dyson 	vm_object_pip_wait(object, "objtrm");
548df8bae1dSRodney W. Grimes 
5495526d2d9SEivind Eklund 	KASSERT(!object->paging_in_progress,
5505526d2d9SEivind Eklund 		("vm_object_terminate: pageout in progress"));
55126f9a767SRodney W. Grimes 
55226f9a767SRodney W. Grimes 	/*
5530d94caffSDavid Greenman 	 * Clean and free the pages, as appropriate. All references to the
5540d94caffSDavid Greenman 	 * object are gone, so we don't need to lock it.
55526f9a767SRodney W. Grimes 	 */
55624a1cce3SDavid Greenman 	if (object->type == OBJT_VNODE) {
55795e5e988SJohn Dyson 		struct vnode *vp;
55895e5e988SJohn Dyson 
559c0b6bbb8SAlan Cox #ifdef ENABLE_VFS_IOOPT
56095e5e988SJohn Dyson 		/*
56195e5e988SJohn Dyson 		 * Freeze optimized copies.
56295e5e988SJohn Dyson 		 */
56395e5e988SJohn Dyson 		vm_freeze_copyopts(object, 0, object->size);
564c0b6bbb8SAlan Cox #endif
56595e5e988SJohn Dyson 		/*
56695e5e988SJohn Dyson 		 * Clean pages and flush buffers.
56795e5e988SJohn Dyson 		 */
5688f9110f6SJohn Dyson 		vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
56995e5e988SJohn Dyson 
57095e5e988SJohn Dyson 		vp = (struct vnode *) object->handle;
571f6b04d2bSDavid Greenman 		vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
572bef608bdSJohn Dyson 	}
573bef608bdSJohn Dyson 
574971dd342SAlfred Perlstein 	KASSERT(object->ref_count == 0,
575971dd342SAlfred Perlstein 		("vm_object_terminate: object with references, ref_count=%d",
576971dd342SAlfred Perlstein 		object->ref_count));
577996c772fSJohn Dyson 
5780d94caffSDavid Greenman 	/*
579356863ebSDavid Greenman 	 * Now free any remaining pages. For internal objects, this also
580356863ebSDavid Greenman 	 * removes them from paging queues. Don't free wired pages, just
581356863ebSDavid Greenman 	 * remove them from the object.
582df8bae1dSRodney W. Grimes 	 */
583e4b7635dSDavid Greenman 	s = splvm();
58456030358SAlan Cox 	vm_page_lock_queues();
585b18bfc3dSJohn Dyson 	while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
586971dd342SAlfred Perlstein 		KASSERT(!p->busy && (p->flags & PG_BUSY) == 0,
587971dd342SAlfred Perlstein 			("vm_object_terminate: freeing busy page %p "
588971dd342SAlfred Perlstein 			"p->busy = %d, p->flags %x\n", p, p->busy, p->flags));
5890b10ba98SDavid Greenman 		if (p->wire_count == 0) {
590e69763a3SDoug Rabson 			vm_page_busy(p);
591df8bae1dSRodney W. Grimes 			vm_page_free(p);
592df8bae1dSRodney W. Grimes 			cnt.v_pfree++;
5930b10ba98SDavid Greenman 		} else {
594356863ebSDavid Greenman 			vm_page_busy(p);
5950b10ba98SDavid Greenman 			vm_page_remove(p);
5960b10ba98SDavid Greenman 		}
597df8bae1dSRodney W. Grimes 	}
59856030358SAlan Cox 	vm_page_unlock_queues();
599e4b7635dSDavid Greenman 	splx(s);
600bef608bdSJohn Dyson 
6012d8acc0fSJohn Dyson 	/*
6029fcfb650SDavid Greenman 	 * Let the pager know object is dead.
6039fcfb650SDavid Greenman 	 */
6049fcfb650SDavid Greenman 	vm_pager_deallocate(object);
6059fcfb650SDavid Greenman 
6069fcfb650SDavid Greenman 	/*
6070b10ba98SDavid Greenman 	 * Remove the object from the global object list.
6082d8acc0fSJohn Dyson 	 */
6099ed346baSBosko Milekic 	mtx_lock(&vm_object_list_mtx);
610df8bae1dSRodney W. Grimes 	TAILQ_REMOVE(&vm_object_list, object, object_list);
6119ed346baSBosko Milekic 	mtx_unlock(&vm_object_list_mtx);
6120b10ba98SDavid Greenman 
6134b420d50SAlan Cox 	mtx_destroy(&object->mtx);
6140b10ba98SDavid Greenman 	wakeup(object);
6150b10ba98SDavid Greenman 
616df8bae1dSRodney W. Grimes 	/*
617df8bae1dSRodney W. Grimes 	 * Free the space for the object.
618df8bae1dSRodney W. Grimes 	 */
619670d17b5SJeff Roberson 	uma_zfree(obj_zone, object);
62047221757SJohn Dyson }
621df8bae1dSRodney W. Grimes 
622df8bae1dSRodney W. Grimes /*
623df8bae1dSRodney W. Grimes  *	vm_object_page_clean
624df8bae1dSRodney W. Grimes  *
6254f79d873SMatthew Dillon  *	Clean all dirty pages in the specified range of object.  Leaves page
6264f79d873SMatthew Dillon  * 	on whatever queue it is currently on.   If NOSYNC is set then do not
6274f79d873SMatthew Dillon  *	write out pages with PG_NOSYNC set (originally comes from MAP_NOSYNC),
6284f79d873SMatthew Dillon  *	leaving the object dirty.
62926f9a767SRodney W. Grimes  *
63043b7990eSMatthew Dillon  *	When stuffing pages asynchronously, allow clustering.  XXX we need a
63143b7990eSMatthew Dillon  *	synchronous clustering mode implementation.
63243b7990eSMatthew Dillon  *
63326f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
63426f9a767SRodney W. Grimes  *
63526f9a767SRodney W. Grimes  *	The object must be locked.
63626f9a767SRodney W. Grimes  */
637f6b04d2bSDavid Greenman void
6381b40f8c0SMatthew Dillon vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end, int flags)
639f6b04d2bSDavid Greenman {
640b9b7a4beSMatthew Dillon 	vm_page_t p, np;
6416395da54SIan Dowse 	vm_pindex_t tstart, tend;
642bd7e5f99SJohn Dyson 	vm_pindex_t pi;
64324a1cce3SDavid Greenman 	struct vnode *vp;
6444f79d873SMatthew Dillon 	int clearobjflags;
6458f9110f6SJohn Dyson 	int pagerflags;
6462d8acc0fSJohn Dyson 	int curgeneration;
647f6b04d2bSDavid Greenman 
6480cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
6490cddd8f0SMatthew Dillon 
650aef922f5SJohn Dyson 	if (object->type != OBJT_VNODE ||
651aef922f5SJohn Dyson 		(object->flags & OBJ_MIGHTBEDIRTY) == 0)
652f6b04d2bSDavid Greenman 		return;
653f6b04d2bSDavid Greenman 
65443b7990eSMatthew Dillon 	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
6558f9110f6SJohn Dyson 	pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
6568f9110f6SJohn Dyson 
65724a1cce3SDavid Greenman 	vp = object->handle;
65824a1cce3SDavid Greenman 
659069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_CLEANING);
66024a1cce3SDavid Greenman 
661f6b04d2bSDavid Greenman 	tstart = start;
662f6b04d2bSDavid Greenman 	if (end == 0) {
663f6b04d2bSDavid Greenman 		tend = object->size;
664f6b04d2bSDavid Greenman 	} else {
665f6b04d2bSDavid Greenman 		tend = end;
666f6b04d2bSDavid Greenman 	}
667eaf13dd7SJohn Dyson 
66835c01631SAlan Cox 	vm_page_lock_queues();
6694f79d873SMatthew Dillon 	/*
670b9b7a4beSMatthew Dillon 	 * If the caller is smart and only msync()s a range he knows is
671b9b7a4beSMatthew Dillon 	 * dirty, we may be able to avoid an object scan.  This results in
672b9b7a4beSMatthew Dillon 	 * a phenominal improvement in performance.  We cannot do this
673b9b7a4beSMatthew Dillon 	 * as a matter of course because the object may be huge - e.g.
674b9b7a4beSMatthew Dillon 	 * the size might be in the gigabytes or terrabytes.
675b9b7a4beSMatthew Dillon 	 */
676b9b7a4beSMatthew Dillon 	if (msync_flush_flags & MSYNC_FLUSH_HARDSEQ) {
677300b96acSIan Dowse 		vm_pindex_t tscan;
678b9b7a4beSMatthew Dillon 		int scanlimit;
679b9b7a4beSMatthew Dillon 		int scanreset;
680b9b7a4beSMatthew Dillon 
681b9b7a4beSMatthew Dillon 		scanreset = object->resident_page_count / EASY_SCAN_FACTOR;
682b9b7a4beSMatthew Dillon 		if (scanreset < 16)
683b9b7a4beSMatthew Dillon 			scanreset = 16;
68443b7990eSMatthew Dillon 		pagerflags |= VM_PAGER_IGNORE_CLEANCHK;
685b9b7a4beSMatthew Dillon 
686b9b7a4beSMatthew Dillon 		scanlimit = scanreset;
687b9b7a4beSMatthew Dillon 		tscan = tstart;
688b9b7a4beSMatthew Dillon 		while (tscan < tend) {
689b9b7a4beSMatthew Dillon 			curgeneration = object->generation;
690b9b7a4beSMatthew Dillon 			p = vm_page_lookup(object, tscan);
691b9b7a4beSMatthew Dillon 			if (p == NULL || p->valid == 0 ||
692b9b7a4beSMatthew Dillon 			    (p->queue - p->pc) == PQ_CACHE) {
693b9b7a4beSMatthew Dillon 				if (--scanlimit == 0)
694b9b7a4beSMatthew Dillon 					break;
695b9b7a4beSMatthew Dillon 				++tscan;
696b9b7a4beSMatthew Dillon 				continue;
697b9b7a4beSMatthew Dillon 			}
698b9b7a4beSMatthew Dillon 			vm_page_test_dirty(p);
699b9b7a4beSMatthew Dillon 			if ((p->dirty & p->valid) == 0) {
700b9b7a4beSMatthew Dillon 				if (--scanlimit == 0)
701b9b7a4beSMatthew Dillon 					break;
702b9b7a4beSMatthew Dillon 				++tscan;
703b9b7a4beSMatthew Dillon 				continue;
704b9b7a4beSMatthew Dillon 			}
705b9b7a4beSMatthew Dillon 			/*
706b9b7a4beSMatthew Dillon 			 * If we have been asked to skip nosync pages and
707b9b7a4beSMatthew Dillon 			 * this is a nosync page, we can't continue.
708b9b7a4beSMatthew Dillon 			 */
709b9b7a4beSMatthew Dillon 			if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
710b9b7a4beSMatthew Dillon 				if (--scanlimit == 0)
711b9b7a4beSMatthew Dillon 					break;
712b9b7a4beSMatthew Dillon 				++tscan;
713b9b7a4beSMatthew Dillon 				continue;
714b9b7a4beSMatthew Dillon 			}
715b9b7a4beSMatthew Dillon 			scanlimit = scanreset;
716b9b7a4beSMatthew Dillon 
717b9b7a4beSMatthew Dillon 			/*
718b9b7a4beSMatthew Dillon 			 * This returns 0 if it was unable to busy the first
719b9b7a4beSMatthew Dillon 			 * page (i.e. had to sleep).
720b9b7a4beSMatthew Dillon 			 */
721b9b7a4beSMatthew Dillon 			tscan += vm_object_page_collect_flush(object, p, curgeneration, pagerflags);
722b9b7a4beSMatthew Dillon 		}
723b9b7a4beSMatthew Dillon 
724b9b7a4beSMatthew Dillon 		/*
725b9b7a4beSMatthew Dillon 		 * If everything was dirty and we flushed it successfully,
726b9b7a4beSMatthew Dillon 		 * and the requested range is not the entire object, we
727b9b7a4beSMatthew Dillon 		 * don't have to mess with CLEANCHK or MIGHTBEDIRTY and can
728b9b7a4beSMatthew Dillon 		 * return immediately.
729b9b7a4beSMatthew Dillon 		 */
730b9b7a4beSMatthew Dillon 		if (tscan >= tend && (tstart || tend < object->size)) {
73135c01631SAlan Cox 			vm_page_unlock_queues();
732b9b7a4beSMatthew Dillon 			vm_object_clear_flag(object, OBJ_CLEANING);
733b9b7a4beSMatthew Dillon 			return;
734b9b7a4beSMatthew Dillon 		}
73543b7990eSMatthew Dillon 		pagerflags &= ~VM_PAGER_IGNORE_CLEANCHK;
736b9b7a4beSMatthew Dillon 	}
737b9b7a4beSMatthew Dillon 
738b9b7a4beSMatthew Dillon 	/*
7394f79d873SMatthew Dillon 	 * Generally set CLEANCHK interlock and make the page read-only so
7404f79d873SMatthew Dillon 	 * we can then clear the object flags.
7414f79d873SMatthew Dillon 	 *
7424f79d873SMatthew Dillon 	 * However, if this is a nosync mmap then the object is likely to
7434f79d873SMatthew Dillon 	 * stay dirty so do not mess with the page and do not clear the
7444f79d873SMatthew Dillon 	 * object flags.
7454f79d873SMatthew Dillon 	 */
7464f79d873SMatthew Dillon 	clearobjflags = 1;
747fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(p, &object->memq, listq) {
748e69763a3SDoug Rabson 		vm_page_flag_set(p, PG_CLEANCHK);
7494f79d873SMatthew Dillon 		if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC))
7504f79d873SMatthew Dillon 			clearobjflags = 0;
7514f79d873SMatthew Dillon 		else
752a12cc0e4SAlan Cox 			pmap_page_protect(p, VM_PROT_READ);
753eaf13dd7SJohn Dyson 	}
754eaf13dd7SJohn Dyson 
7554f79d873SMatthew Dillon 	if (clearobjflags && (tstart == 0) && (tend == object->size)) {
756245df27cSMatthew Dillon 		struct vnode *vp;
757245df27cSMatthew Dillon 
758069e9bc1SDoug Rabson 		vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
759245df27cSMatthew Dillon 		if (object->type == OBJT_VNODE &&
760245df27cSMatthew Dillon 		    (vp = (struct vnode *)object->handle) != NULL) {
761e6e370a7SJeff Roberson 			VI_LOCK(vp);
762e6e370a7SJeff Roberson 			if (vp->v_iflag & VI_OBJDIRTY)
763e6e370a7SJeff Roberson 				vp->v_iflag &= ~VI_OBJDIRTY;
764e6e370a7SJeff Roberson 			VI_UNLOCK(vp);
765245df27cSMatthew Dillon 		}
766ec4f9fb0SDavid Greenman 	}
767f6b04d2bSDavid Greenman 
768bd7e5f99SJohn Dyson rescan:
7692d8acc0fSJohn Dyson 	curgeneration = object->generation;
7702d8acc0fSJohn Dyson 
771b18bfc3dSJohn Dyson 	for (p = TAILQ_FIRST(&object->memq); p; p = np) {
772b9b7a4beSMatthew Dillon 		int n;
773b9b7a4beSMatthew Dillon 
774b18bfc3dSJohn Dyson 		np = TAILQ_NEXT(p, listq);
775bd7e5f99SJohn Dyson 
776b9b7a4beSMatthew Dillon again:
777bd7e5f99SJohn Dyson 		pi = p->pindex;
778bd7e5f99SJohn Dyson 		if (((p->flags & PG_CLEANCHK) == 0) ||
779bd7e5f99SJohn Dyson 			(pi < tstart) || (pi >= tend) ||
7805070c7f8SJohn Dyson 			(p->valid == 0) ||
7815070c7f8SJohn Dyson 			((p->queue - p->pc) == PQ_CACHE)) {
782e69763a3SDoug Rabson 			vm_page_flag_clear(p, PG_CLEANCHK);
783aef922f5SJohn Dyson 			continue;
784f6b04d2bSDavid Greenman 		}
785f6b04d2bSDavid Greenman 
786bd7e5f99SJohn Dyson 		vm_page_test_dirty(p);
787bd7e5f99SJohn Dyson 		if ((p->dirty & p->valid) == 0) {
788e69763a3SDoug Rabson 			vm_page_flag_clear(p, PG_CLEANCHK);
789bd7e5f99SJohn Dyson 			continue;
790bd7e5f99SJohn Dyson 		}
791ec4f9fb0SDavid Greenman 
7924f79d873SMatthew Dillon 		/*
7934f79d873SMatthew Dillon 		 * If we have been asked to skip nosync pages and this is a
7944f79d873SMatthew Dillon 		 * nosync page, skip it.  Note that the object flags were
7954f79d873SMatthew Dillon 		 * not cleared in this case so we do not have to set them.
7964f79d873SMatthew Dillon 		 */
7974f79d873SMatthew Dillon 		if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
7984f79d873SMatthew Dillon 			vm_page_flag_clear(p, PG_CLEANCHK);
7994f79d873SMatthew Dillon 			continue;
8004f79d873SMatthew Dillon 		}
8014f79d873SMatthew Dillon 
802b9b7a4beSMatthew Dillon 		n = vm_object_page_collect_flush(object, p,
803b9b7a4beSMatthew Dillon 			curgeneration, pagerflags);
804b9b7a4beSMatthew Dillon 		if (n == 0)
805b9b7a4beSMatthew Dillon 			goto rescan;
806b9b7a4beSMatthew Dillon 
807b9b7a4beSMatthew Dillon 		if (object->generation != curgeneration)
808b9b7a4beSMatthew Dillon 			goto rescan;
809b9b7a4beSMatthew Dillon 
810b9b7a4beSMatthew Dillon 		/*
811b9b7a4beSMatthew Dillon 		 * Try to optimize the next page.  If we can't we pick up
812b9b7a4beSMatthew Dillon 		 * our (random) scan where we left off.
813b9b7a4beSMatthew Dillon 		 */
814b9b7a4beSMatthew Dillon 		if (msync_flush_flags & MSYNC_FLUSH_SOFTSEQ) {
815b9b7a4beSMatthew Dillon 			if ((p = vm_page_lookup(object, pi + n)) != NULL)
816b9b7a4beSMatthew Dillon 				goto again;
817b9b7a4beSMatthew Dillon 		}
818b9b7a4beSMatthew Dillon 	}
81935c01631SAlan Cox 	vm_page_unlock_queues();
820b9b7a4beSMatthew Dillon #if 0
821b9b7a4beSMatthew Dillon 	VOP_FSYNC(vp, NULL, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
822b9b7a4beSMatthew Dillon #endif
823b9b7a4beSMatthew Dillon 
824b9b7a4beSMatthew Dillon 	vm_object_clear_flag(object, OBJ_CLEANING);
825b9b7a4beSMatthew Dillon 	return;
826b9b7a4beSMatthew Dillon }
827b9b7a4beSMatthew Dillon 
828b9b7a4beSMatthew Dillon static int
829b9b7a4beSMatthew Dillon vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags)
830b9b7a4beSMatthew Dillon {
831b9b7a4beSMatthew Dillon 	int runlen;
832b9b7a4beSMatthew Dillon 	int s;
833b9b7a4beSMatthew Dillon 	int maxf;
834b9b7a4beSMatthew Dillon 	int chkb;
835b9b7a4beSMatthew Dillon 	int maxb;
836b9b7a4beSMatthew Dillon 	int i;
837b9b7a4beSMatthew Dillon 	vm_pindex_t pi;
838b9b7a4beSMatthew Dillon 	vm_page_t maf[vm_pageout_page_count];
839b9b7a4beSMatthew Dillon 	vm_page_t mab[vm_pageout_page_count];
840b9b7a4beSMatthew Dillon 	vm_page_t ma[vm_pageout_page_count];
841b9b7a4beSMatthew Dillon 
842b18bfc3dSJohn Dyson 	s = splvm();
84335c01631SAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
844b9b7a4beSMatthew Dillon 	pi = p->pindex;
845bd82dc74SAlan Cox 	while (vm_page_sleep_if_busy(p, TRUE, "vpcwai")) {
84635c01631SAlan Cox 		vm_page_lock_queues();
8472d8acc0fSJohn Dyson 		if (object->generation != curgeneration) {
848f6b04d2bSDavid Greenman 			splx(s);
849b9b7a4beSMatthew Dillon 			return(0);
850f6b04d2bSDavid Greenman 		}
851bd82dc74SAlan Cox 	}
852bd7e5f99SJohn Dyson 	maxf = 0;
853bd7e5f99SJohn Dyson 	for(i = 1; i < vm_pageout_page_count; i++) {
854b9b7a4beSMatthew Dillon 		vm_page_t tp;
855b9b7a4beSMatthew Dillon 
8568aef1712SMatthew Dillon 		if ((tp = vm_page_lookup(object, pi + i)) != NULL) {
857bd7e5f99SJohn Dyson 			if ((tp->flags & PG_BUSY) ||
85843b7990eSMatthew Dillon 				((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
85943b7990eSMatthew Dillon 				 (tp->flags & PG_CLEANCHK) == 0) ||
860ffc82b0aSJohn Dyson 				(tp->busy != 0))
861bd7e5f99SJohn Dyson 				break;
8625070c7f8SJohn Dyson 			if((tp->queue - tp->pc) == PQ_CACHE) {
863e69763a3SDoug Rabson 				vm_page_flag_clear(tp, PG_CLEANCHK);
8643077a9c2SJohn Dyson 				break;
8653077a9c2SJohn Dyson 			}
866bd7e5f99SJohn Dyson 			vm_page_test_dirty(tp);
867bd7e5f99SJohn Dyson 			if ((tp->dirty & tp->valid) == 0) {
868e69763a3SDoug Rabson 				vm_page_flag_clear(tp, PG_CLEANCHK);
869bd7e5f99SJohn Dyson 				break;
870bd7e5f99SJohn Dyson 			}
871bd7e5f99SJohn Dyson 			maf[ i - 1 ] = tp;
872bd7e5f99SJohn Dyson 			maxf++;
873bd7e5f99SJohn Dyson 			continue;
874bd7e5f99SJohn Dyson 		}
875bd7e5f99SJohn Dyson 		break;
876bd7e5f99SJohn Dyson 	}
877aef922f5SJohn Dyson 
878bd7e5f99SJohn Dyson 	maxb = 0;
879bd7e5f99SJohn Dyson 	chkb = vm_pageout_page_count -  maxf;
880bd7e5f99SJohn Dyson 	if (chkb) {
881bd7e5f99SJohn Dyson 		for(i = 1; i < chkb;i++) {
882b9b7a4beSMatthew Dillon 			vm_page_t tp;
883b9b7a4beSMatthew Dillon 
8848aef1712SMatthew Dillon 			if ((tp = vm_page_lookup(object, pi - i)) != NULL) {
885bd7e5f99SJohn Dyson 				if ((tp->flags & PG_BUSY) ||
88643b7990eSMatthew Dillon 					((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
88743b7990eSMatthew Dillon 					 (tp->flags & PG_CLEANCHK) == 0) ||
888ffc82b0aSJohn Dyson 					(tp->busy != 0))
889bd7e5f99SJohn Dyson 					break;
8905070c7f8SJohn Dyson 				if ((tp->queue - tp->pc) == PQ_CACHE) {
891e69763a3SDoug Rabson 					vm_page_flag_clear(tp, PG_CLEANCHK);
8923077a9c2SJohn Dyson 					break;
8933077a9c2SJohn Dyson 				}
894bd7e5f99SJohn Dyson 				vm_page_test_dirty(tp);
895bd7e5f99SJohn Dyson 				if ((tp->dirty & tp->valid) == 0) {
896e69763a3SDoug Rabson 					vm_page_flag_clear(tp, PG_CLEANCHK);
897bd7e5f99SJohn Dyson 					break;
898bd7e5f99SJohn Dyson 				}
899bd7e5f99SJohn Dyson 				mab[ i - 1 ] = tp;
900bd7e5f99SJohn Dyson 				maxb++;
901bd7e5f99SJohn Dyson 				continue;
902bd7e5f99SJohn Dyson 			}
903bd7e5f99SJohn Dyson 			break;
904bd7e5f99SJohn Dyson 		}
905bd7e5f99SJohn Dyson 	}
906bd7e5f99SJohn Dyson 
907bd7e5f99SJohn Dyson 	for(i = 0; i < maxb; i++) {
908bd7e5f99SJohn Dyson 		int index = (maxb - i) - 1;
909bd7e5f99SJohn Dyson 		ma[index] = mab[i];
910e69763a3SDoug Rabson 		vm_page_flag_clear(ma[index], PG_CLEANCHK);
911bd7e5f99SJohn Dyson 	}
912e69763a3SDoug Rabson 	vm_page_flag_clear(p, PG_CLEANCHK);
913bd7e5f99SJohn Dyson 	ma[maxb] = p;
914bd7e5f99SJohn Dyson 	for(i = 0; i < maxf; i++) {
915bd7e5f99SJohn Dyson 		int index = (maxb + i) + 1;
916bd7e5f99SJohn Dyson 		ma[index] = maf[i];
917e69763a3SDoug Rabson 		vm_page_flag_clear(ma[index], PG_CLEANCHK);
918f6b04d2bSDavid Greenman 	}
919bd7e5f99SJohn Dyson 	runlen = maxb + maxf + 1;
920cf2819ccSJohn Dyson 
921f35329acSJohn Dyson 	splx(s);
9228f9110f6SJohn Dyson 	vm_pageout_flush(ma, runlen, pagerflags);
923cf2819ccSJohn Dyson 	for (i = 0; i < runlen; i++) {
924cf2819ccSJohn Dyson 		if (ma[i]->valid & ma[i]->dirty) {
925a12cc0e4SAlan Cox 			pmap_page_protect(ma[i], VM_PROT_READ);
926e69763a3SDoug Rabson 			vm_page_flag_set(ma[i], PG_CLEANCHK);
927aef922f5SJohn Dyson 
928b9b7a4beSMatthew Dillon 			/*
929b9b7a4beSMatthew Dillon 			 * maxf will end up being the actual number of pages
930b9b7a4beSMatthew Dillon 			 * we wrote out contiguously, non-inclusive of the
931b9b7a4beSMatthew Dillon 			 * first page.  We do not count look-behind pages.
932b9b7a4beSMatthew Dillon 			 */
933b9b7a4beSMatthew Dillon 			if (i >= maxb + 1 && (maxf > i - maxb - 1))
934b9b7a4beSMatthew Dillon 				maxf = i - maxb - 1;
935b9b7a4beSMatthew Dillon 		}
936b9b7a4beSMatthew Dillon 	}
937b9b7a4beSMatthew Dillon 	return(maxf + 1);
93826f9a767SRodney W. Grimes }
939df8bae1dSRodney W. Grimes 
94072353893SAlan Cox #ifdef ENABLE_VFS_IOOPT
941df8bae1dSRodney W. Grimes /*
9428e3ad7c9SMatthew Dillon  * Same as vm_object_pmap_copy, except range checking really
9431efb74fbSJohn Dyson  * works, and is meant for small sections of an object.
9448e3ad7c9SMatthew Dillon  *
9458e3ad7c9SMatthew Dillon  * This code protects resident pages by making them read-only
9468e3ad7c9SMatthew Dillon  * and is typically called on a fork or split when a page
9478e3ad7c9SMatthew Dillon  * is converted to copy-on-write.
9488e3ad7c9SMatthew Dillon  *
9498e3ad7c9SMatthew Dillon  * NOTE: If the page is already at VM_PROT_NONE, calling
950a12cc0e4SAlan Cox  * pmap_page_protect will have no effect.
9511efb74fbSJohn Dyson  */
9521efb74fbSJohn Dyson void
9531b40f8c0SMatthew Dillon vm_object_pmap_copy_1(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
9541efb74fbSJohn Dyson {
9551efb74fbSJohn Dyson 	vm_pindex_t idx;
956d031cff1SMatthew Dillon 	vm_page_t p;
9571efb74fbSJohn Dyson 
9580cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
9590cddd8f0SMatthew Dillon 
9601efb74fbSJohn Dyson 	if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
9611efb74fbSJohn Dyson 		return;
9623a199de3SAlan Cox 	vm_page_lock_queues();
9631efb74fbSJohn Dyson 	for (idx = start; idx < end; idx++) {
9641efb74fbSJohn Dyson 		p = vm_page_lookup(object, idx);
9651efb74fbSJohn Dyson 		if (p == NULL)
9661efb74fbSJohn Dyson 			continue;
967a12cc0e4SAlan Cox 		pmap_page_protect(p, VM_PROT_READ);
9681efb74fbSJohn Dyson 	}
9693a199de3SAlan Cox 	vm_page_unlock_queues();
9701efb74fbSJohn Dyson }
97172353893SAlan Cox #endif
9721efb74fbSJohn Dyson 
9731efb74fbSJohn Dyson /*
974867a482dSJohn Dyson  *	vm_object_madvise:
975867a482dSJohn Dyson  *
976867a482dSJohn Dyson  *	Implements the madvise function at the object/page level.
9771c7c3c6aSMatthew Dillon  *
978193b9358SAlan Cox  *	MADV_WILLNEED	(any object)
979193b9358SAlan Cox  *
980193b9358SAlan Cox  *	    Activate the specified pages if they are resident.
981193b9358SAlan Cox  *
982193b9358SAlan Cox  *	MADV_DONTNEED	(any object)
983193b9358SAlan Cox  *
984193b9358SAlan Cox  *	    Deactivate the specified pages if they are resident.
985193b9358SAlan Cox  *
986193b9358SAlan Cox  *	MADV_FREE	(OBJT_DEFAULT/OBJT_SWAP objects,
987193b9358SAlan Cox  *			 OBJ_ONEMAPPING only)
988193b9358SAlan Cox  *
989193b9358SAlan Cox  *	    Deactivate and clean the specified pages if they are
990193b9358SAlan Cox  *	    resident.  This permits the process to reuse the pages
991193b9358SAlan Cox  *	    without faulting or the kernel to reclaim the pages
992193b9358SAlan Cox  *	    without I/O.
993867a482dSJohn Dyson  */
994867a482dSJohn Dyson void
9951b40f8c0SMatthew Dillon vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
996867a482dSJohn Dyson {
9976e20a165SJohn Dyson 	vm_pindex_t end, tpindex;
9986e20a165SJohn Dyson 	vm_object_t tobject;
999867a482dSJohn Dyson 	vm_page_t m;
1000867a482dSJohn Dyson 
1001867a482dSJohn Dyson 	if (object == NULL)
1002867a482dSJohn Dyson 		return;
1003867a482dSJohn Dyson 
100415c176c1SAlan Cox 	vm_object_lock(object);
1005094f6d26SAlan Cox 
1006867a482dSJohn Dyson 	end = pindex + count;
1007867a482dSJohn Dyson 
10081c7c3c6aSMatthew Dillon 	/*
10091c7c3c6aSMatthew Dillon 	 * Locate and adjust resident pages
10101c7c3c6aSMatthew Dillon 	 */
10111c7c3c6aSMatthew Dillon 	for (; pindex < end; pindex += 1) {
10126e20a165SJohn Dyson relookup:
10136e20a165SJohn Dyson 		tobject = object;
10146e20a165SJohn Dyson 		tpindex = pindex;
10156e20a165SJohn Dyson shadowlookup:
101658b4e6ccSAlan Cox 		/*
101758b4e6ccSAlan Cox 		 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
101858b4e6ccSAlan Cox 		 * and those pages must be OBJ_ONEMAPPING.
101958b4e6ccSAlan Cox 		 */
102058b4e6ccSAlan Cox 		if (advise == MADV_FREE) {
102158b4e6ccSAlan Cox 			if ((tobject->type != OBJT_DEFAULT &&
102258b4e6ccSAlan Cox 			     tobject->type != OBJT_SWAP) ||
102358b4e6ccSAlan Cox 			    (tobject->flags & OBJ_ONEMAPPING) == 0) {
10246e20a165SJohn Dyson 				continue;
10256e20a165SJohn Dyson 			}
102658b4e6ccSAlan Cox 		}
10271c7c3c6aSMatthew Dillon 
10281c7c3c6aSMatthew Dillon 		m = vm_page_lookup(tobject, tpindex);
10291c7c3c6aSMatthew Dillon 
10301c7c3c6aSMatthew Dillon 		if (m == NULL) {
10311ce137beSMatthew Dillon 			/*
10321ce137beSMatthew Dillon 			 * There may be swap even if there is no backing page
10331ce137beSMatthew Dillon 			 */
10341ce137beSMatthew Dillon 			if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
10351ce137beSMatthew Dillon 				swap_pager_freespace(tobject, tpindex, 1);
10361ce137beSMatthew Dillon 
10371ce137beSMatthew Dillon 			/*
10381ce137beSMatthew Dillon 			 * next object
10391ce137beSMatthew Dillon 			 */
10406e20a165SJohn Dyson 			tobject = tobject->backing_object;
10411c7c3c6aSMatthew Dillon 			if (tobject == NULL)
10421c7c3c6aSMatthew Dillon 				continue;
10436e20a165SJohn Dyson 			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
10446e20a165SJohn Dyson 			goto shadowlookup;
10456e20a165SJohn Dyson 		}
1046867a482dSJohn Dyson 
1047867a482dSJohn Dyson 		/*
1048867a482dSJohn Dyson 		 * If the page is busy or not in a normal active state,
10498b03c8edSMatthew Dillon 		 * we skip it.  If the page is not managed there are no
10508b03c8edSMatthew Dillon 		 * page queues to mess with.  Things can break if we mess
10518b03c8edSMatthew Dillon 		 * with pages in any of the below states.
1052867a482dSJohn Dyson 		 */
105332585dd6SAlan Cox 		vm_page_lock_queues();
105432585dd6SAlan Cox 		if (m->hold_count ||
10551c7c3c6aSMatthew Dillon 		    m->wire_count ||
10568b03c8edSMatthew Dillon 		    (m->flags & PG_UNMANAGED) ||
105732585dd6SAlan Cox 		    m->valid != VM_PAGE_BITS_ALL) {
105832585dd6SAlan Cox 			vm_page_unlock_queues();
1059867a482dSJohn Dyson 			continue;
10606e20a165SJohn Dyson 		}
106132585dd6SAlan Cox  		if (vm_page_sleep_if_busy(m, TRUE, "madvpo"))
10626e20a165SJohn Dyson   			goto relookup;
1063867a482dSJohn Dyson 		if (advise == MADV_WILLNEED) {
1064867a482dSJohn Dyson 			vm_page_activate(m);
10656e20a165SJohn Dyson 		} else if (advise == MADV_DONTNEED) {
1066479112dfSMatthew Dillon 			vm_page_dontneed(m);
10670a47b48bSJohn Dyson 		} else if (advise == MADV_FREE) {
10681c7c3c6aSMatthew Dillon 			/*
10692aaeadf8SMatthew Dillon 			 * Mark the page clean.  This will allow the page
10702aaeadf8SMatthew Dillon 			 * to be freed up by the system.  However, such pages
10712aaeadf8SMatthew Dillon 			 * are often reused quickly by malloc()/free()
10722aaeadf8SMatthew Dillon 			 * so we do not do anything that would cause
10732aaeadf8SMatthew Dillon 			 * a page fault if we can help it.
10742aaeadf8SMatthew Dillon 			 *
10752aaeadf8SMatthew Dillon 			 * Specifically, we do not try to actually free
10762aaeadf8SMatthew Dillon 			 * the page now nor do we try to put it in the
10772aaeadf8SMatthew Dillon 			 * cache (which would cause a page fault on reuse).
107841c67e12SMatthew Dillon 			 *
107941c67e12SMatthew Dillon 			 * But we do make the page is freeable as we
108041c67e12SMatthew Dillon 			 * can without actually taking the step of unmapping
108141c67e12SMatthew Dillon 			 * it.
10821c7c3c6aSMatthew Dillon 			 */
10830385347cSPeter Wemm 			pmap_clear_modify(m);
10846e20a165SJohn Dyson 			m->dirty = 0;
108541c67e12SMatthew Dillon 			m->act_count = 0;
1086479112dfSMatthew Dillon 			vm_page_dontneed(m);
1087867a482dSJohn Dyson 		}
10882999e9faSAlan Cox 		vm_page_unlock_queues();
10892999e9faSAlan Cox 		if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
10902999e9faSAlan Cox 			swap_pager_freespace(tobject, tpindex, 1);
1091867a482dSJohn Dyson 	}
109215c176c1SAlan Cox 	vm_object_unlock(object);
1093867a482dSJohn Dyson }
1094867a482dSJohn Dyson 
1095867a482dSJohn Dyson /*
1096df8bae1dSRodney W. Grimes  *	vm_object_shadow:
1097df8bae1dSRodney W. Grimes  *
1098df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
1099df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
1100df8bae1dSRodney W. Grimes  *	object reference is deallocated.
1101df8bae1dSRodney W. Grimes  *
1102df8bae1dSRodney W. Grimes  *	The new object and offset into that object
1103df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
1104df8bae1dSRodney W. Grimes  */
110526f9a767SRodney W. Grimes void
11061b40f8c0SMatthew Dillon vm_object_shadow(
11071b40f8c0SMatthew Dillon 	vm_object_t *object,	/* IN/OUT */
11081b40f8c0SMatthew Dillon 	vm_ooffset_t *offset,	/* IN/OUT */
11091b40f8c0SMatthew Dillon 	vm_size_t length)
1110df8bae1dSRodney W. Grimes {
1111d031cff1SMatthew Dillon 	vm_object_t source;
1112d031cff1SMatthew Dillon 	vm_object_t result;
1113df8bae1dSRodney W. Grimes 
1114df8bae1dSRodney W. Grimes 	source = *object;
1115df8bae1dSRodney W. Grimes 
111615c176c1SAlan Cox 	vm_object_lock(source);
1117df8bae1dSRodney W. Grimes 	/*
11189a2f6362SAlan Cox 	 * Don't create the new object if the old object isn't shared.
11199a2f6362SAlan Cox 	 */
1120c7997d57SAlan Cox 	if (source != NULL &&
1121c7997d57SAlan Cox 	    source->ref_count == 1 &&
11229a2f6362SAlan Cox 	    source->handle == NULL &&
11239a2f6362SAlan Cox 	    (source->type == OBJT_DEFAULT ||
11249917e010SAlan Cox 	     source->type == OBJT_SWAP)) {
112515c176c1SAlan Cox 		vm_object_unlock(source);
11269a2f6362SAlan Cox 		return;
11279917e010SAlan Cox 	}
11289a2f6362SAlan Cox 
11299a2f6362SAlan Cox 	/*
1130df8bae1dSRodney W. Grimes 	 * Allocate a new object with the given length
1131df8bae1dSRodney W. Grimes 	 */
1132971dd342SAlfred Perlstein 	result = vm_object_allocate(OBJT_DEFAULT, length);
1133971dd342SAlfred Perlstein 	KASSERT(result != NULL, ("vm_object_shadow: no object for shadowing"));
1134df8bae1dSRodney W. Grimes 
1135df8bae1dSRodney W. Grimes 	/*
11360d94caffSDavid Greenman 	 * The new object shadows the source object, adding a reference to it.
11370d94caffSDavid Greenman 	 * Our caller changes his reference to point to the new object,
11380d94caffSDavid Greenman 	 * removing a reference to the source object.  Net result: no change
11390d94caffSDavid Greenman 	 * of reference count.
11409b09fe24SMatthew Dillon 	 *
11419b09fe24SMatthew Dillon 	 * Try to optimize the result object's page color when shadowing
1142956f3135SPhilippe Charnier 	 * in order to maintain page coloring consistency in the combined
11439b09fe24SMatthew Dillon 	 * shadowed object.
1144df8bae1dSRodney W. Grimes 	 */
114524a1cce3SDavid Greenman 	result->backing_object = source;
1146de5f6a77SJohn Dyson 	if (source) {
1147de5f6a77SJohn Dyson 		TAILQ_INSERT_TAIL(&source->shadow_head, result, shadow_list);
1148eaf13dd7SJohn Dyson 		source->shadow_count++;
1149eaf13dd7SJohn Dyson 		source->generation++;
1150dcc5840eSAlan Cox 		if (length < source->size)
1151dcc5840eSAlan Cox 			length = source->size;
1152dcc5840eSAlan Cox 		if (length > PQ_L2_SIZE / 3 + PQ_PRIME1 ||
1153dcc5840eSAlan Cox 		    source->generation > 1)
1154dcc5840eSAlan Cox 			length = PQ_L2_SIZE / 3 + PQ_PRIME1;
1155dcc5840eSAlan Cox 		result->pg_color = (source->pg_color +
1156dcc5840eSAlan Cox 		    length * source->generation) & PQ_L2_MASK;
1157dcc5840eSAlan Cox 		next_index = (result->pg_color + PQ_L2_SIZE / 3 + PQ_PRIME1) &
1158dcc5840eSAlan Cox 		    PQ_L2_MASK;
1159de5f6a77SJohn Dyson 	}
1160df8bae1dSRodney W. Grimes 
1161df8bae1dSRodney W. Grimes 	/*
11620d94caffSDavid Greenman 	 * Store the offset into the source object, and fix up the offset into
11630d94caffSDavid Greenman 	 * the new object.
1164df8bae1dSRodney W. Grimes 	 */
116524a1cce3SDavid Greenman 	result->backing_object_offset = *offset;
1166df8bae1dSRodney W. Grimes 
1167df8bae1dSRodney W. Grimes 	/*
1168df8bae1dSRodney W. Grimes 	 * Return the new things
1169df8bae1dSRodney W. Grimes 	 */
1170df8bae1dSRodney W. Grimes 	*offset = 0;
1171df8bae1dSRodney W. Grimes 	*object = result;
11729917e010SAlan Cox 
117315c176c1SAlan Cox 	vm_object_unlock(source);
1174df8bae1dSRodney W. Grimes }
1175df8bae1dSRodney W. Grimes 
1176c5aaa06dSAlan Cox /*
1177c5aaa06dSAlan Cox  *	vm_object_split:
1178c5aaa06dSAlan Cox  *
1179c5aaa06dSAlan Cox  * Split the pages in a map entry into a new object.  This affords
1180c5aaa06dSAlan Cox  * easier removal of unused pages, and keeps object inheritance from
1181c5aaa06dSAlan Cox  * being a negative impact on memory usage.
1182c5aaa06dSAlan Cox  */
1183c5aaa06dSAlan Cox void
1184c5aaa06dSAlan Cox vm_object_split(vm_map_entry_t entry)
1185c5aaa06dSAlan Cox {
1186c5aaa06dSAlan Cox 	vm_page_t m;
1187c5aaa06dSAlan Cox 	vm_object_t orig_object, new_object, source;
1188c5aaa06dSAlan Cox 	vm_offset_t s, e;
118923f09d50SIan Dowse 	vm_pindex_t offidxstart, offidxend;
119023f09d50SIan Dowse 	vm_size_t idx, size;
1191c5aaa06dSAlan Cox 	vm_ooffset_t offset;
1192c5aaa06dSAlan Cox 
1193c5aaa06dSAlan Cox 	GIANT_REQUIRED;
1194c5aaa06dSAlan Cox 
1195c5aaa06dSAlan Cox 	orig_object = entry->object.vm_object;
1196c5aaa06dSAlan Cox 	if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1197c5aaa06dSAlan Cox 		return;
1198c5aaa06dSAlan Cox 	if (orig_object->ref_count <= 1)
1199c5aaa06dSAlan Cox 		return;
1200c5aaa06dSAlan Cox 
1201c5aaa06dSAlan Cox 	offset = entry->offset;
1202c5aaa06dSAlan Cox 	s = entry->start;
1203c5aaa06dSAlan Cox 	e = entry->end;
1204c5aaa06dSAlan Cox 
1205c5aaa06dSAlan Cox 	offidxstart = OFF_TO_IDX(offset);
1206c5aaa06dSAlan Cox 	offidxend = offidxstart + OFF_TO_IDX(e - s);
1207c5aaa06dSAlan Cox 	size = offidxend - offidxstart;
1208c5aaa06dSAlan Cox 
1209c5aaa06dSAlan Cox 	new_object = vm_pager_allocate(orig_object->type,
1210c5aaa06dSAlan Cox 		NULL, IDX_TO_OFF(size), VM_PROT_ALL, 0LL);
1211c5aaa06dSAlan Cox 	if (new_object == NULL)
1212c5aaa06dSAlan Cox 		return;
1213c5aaa06dSAlan Cox 
1214c5aaa06dSAlan Cox 	source = orig_object->backing_object;
1215c5aaa06dSAlan Cox 	if (source != NULL) {
1216c5aaa06dSAlan Cox 		vm_object_reference(source);	/* Referenced by new_object */
1217c5aaa06dSAlan Cox 		TAILQ_INSERT_TAIL(&source->shadow_head,
1218c5aaa06dSAlan Cox 				  new_object, shadow_list);
1219c5aaa06dSAlan Cox 		vm_object_clear_flag(source, OBJ_ONEMAPPING);
1220c5aaa06dSAlan Cox 		new_object->backing_object_offset =
122156030358SAlan Cox 			orig_object->backing_object_offset + offset;
1222c5aaa06dSAlan Cox 		new_object->backing_object = source;
1223c5aaa06dSAlan Cox 		source->shadow_count++;
1224c5aaa06dSAlan Cox 		source->generation++;
1225c5aaa06dSAlan Cox 	}
1226c5aaa06dSAlan Cox 	for (idx = 0; idx < size; idx++) {
1227c5aaa06dSAlan Cox 	retry:
1228c5aaa06dSAlan Cox 		m = vm_page_lookup(orig_object, offidxstart + idx);
1229c5aaa06dSAlan Cox 		if (m == NULL)
1230c5aaa06dSAlan Cox 			continue;
1231c5aaa06dSAlan Cox 
1232c5aaa06dSAlan Cox 		/*
1233c5aaa06dSAlan Cox 		 * We must wait for pending I/O to complete before we can
1234c5aaa06dSAlan Cox 		 * rename the page.
1235c5aaa06dSAlan Cox 		 *
1236c5aaa06dSAlan Cox 		 * We do not have to VM_PROT_NONE the page as mappings should
1237c5aaa06dSAlan Cox 		 * not be changed by this operation.
1238c5aaa06dSAlan Cox 		 */
123900f9e8b4SAlan Cox 		vm_page_lock_queues();
124000f9e8b4SAlan Cox 		if (vm_page_sleep_if_busy(m, TRUE, "spltwt"))
1241c5aaa06dSAlan Cox 			goto retry;
1242c5aaa06dSAlan Cox 
1243c5aaa06dSAlan Cox 		vm_page_busy(m);
1244c5aaa06dSAlan Cox 		vm_page_rename(m, new_object, idx);
1245c5aaa06dSAlan Cox 		/* page automatically made dirty by rename and cache handled */
1246c5aaa06dSAlan Cox 		vm_page_busy(m);
1247bd82dc74SAlan Cox 		vm_page_unlock_queues();
1248c5aaa06dSAlan Cox 	}
1249c5aaa06dSAlan Cox 	if (orig_object->type == OBJT_SWAP) {
1250c5aaa06dSAlan Cox 		vm_object_pip_add(orig_object, 1);
1251c5aaa06dSAlan Cox 		/*
1252c5aaa06dSAlan Cox 		 * copy orig_object pages into new_object
1253c5aaa06dSAlan Cox 		 * and destroy unneeded pages in
1254c5aaa06dSAlan Cox 		 * shadow object.
1255c5aaa06dSAlan Cox 		 */
1256c5aaa06dSAlan Cox 		swap_pager_copy(orig_object, new_object, offidxstart, 0);
1257c5aaa06dSAlan Cox 		vm_object_pip_wakeup(orig_object);
1258c5aaa06dSAlan Cox 	}
1259dc907f66SAlan Cox 	vm_page_lock_queues();
1260c7118ed6SAlan Cox 	TAILQ_FOREACH(m, &new_object->memq, listq)
1261c5aaa06dSAlan Cox 		vm_page_wakeup(m);
1262dc907f66SAlan Cox 	vm_page_unlock_queues();
1263c5aaa06dSAlan Cox 	entry->object.vm_object = new_object;
1264c5aaa06dSAlan Cox 	entry->offset = 0LL;
1265c5aaa06dSAlan Cox 	vm_object_deallocate(orig_object);
1266c5aaa06dSAlan Cox }
1267c5aaa06dSAlan Cox 
12682ad1a3f7SMatthew Dillon #define	OBSC_TEST_ALL_SHADOWED	0x0001
12692ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_NOWAIT	0x0002
12702ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_WAIT	0x0004
12712ad1a3f7SMatthew Dillon 
12722ad1a3f7SMatthew Dillon static __inline int
12732ad1a3f7SMatthew Dillon vm_object_backing_scan(vm_object_t object, int op)
12742ad1a3f7SMatthew Dillon {
12752ad1a3f7SMatthew Dillon 	int s;
12762ad1a3f7SMatthew Dillon 	int r = 1;
12772ad1a3f7SMatthew Dillon 	vm_page_t p;
12782ad1a3f7SMatthew Dillon 	vm_object_t backing_object;
12792ad1a3f7SMatthew Dillon 	vm_pindex_t backing_offset_index;
12802ad1a3f7SMatthew Dillon 
12812ad1a3f7SMatthew Dillon 	s = splvm();
12820cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
12832ad1a3f7SMatthew Dillon 
12842ad1a3f7SMatthew Dillon 	backing_object = object->backing_object;
12852ad1a3f7SMatthew Dillon 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
12862ad1a3f7SMatthew Dillon 
12872ad1a3f7SMatthew Dillon 	/*
12882ad1a3f7SMatthew Dillon 	 * Initial conditions
12892ad1a3f7SMatthew Dillon 	 */
12902ad1a3f7SMatthew Dillon 	if (op & OBSC_TEST_ALL_SHADOWED) {
12912ad1a3f7SMatthew Dillon 		/*
1292956f3135SPhilippe Charnier 		 * We do not want to have to test for the existence of
12932ad1a3f7SMatthew Dillon 		 * swap pages in the backing object.  XXX but with the
12942ad1a3f7SMatthew Dillon 		 * new swapper this would be pretty easy to do.
12952ad1a3f7SMatthew Dillon 		 *
12962ad1a3f7SMatthew Dillon 		 * XXX what about anonymous MAP_SHARED memory that hasn't
12972ad1a3f7SMatthew Dillon 		 * been ZFOD faulted yet?  If we do not test for this, the
12982ad1a3f7SMatthew Dillon 		 * shadow test may succeed! XXX
12992ad1a3f7SMatthew Dillon 		 */
13002ad1a3f7SMatthew Dillon 		if (backing_object->type != OBJT_DEFAULT) {
13012ad1a3f7SMatthew Dillon 			splx(s);
13022ad1a3f7SMatthew Dillon 			return (0);
13032ad1a3f7SMatthew Dillon 		}
13042ad1a3f7SMatthew Dillon 	}
13052ad1a3f7SMatthew Dillon 	if (op & OBSC_COLLAPSE_WAIT) {
13062ad1a3f7SMatthew Dillon 		vm_object_set_flag(backing_object, OBJ_DEAD);
13072ad1a3f7SMatthew Dillon 	}
13082ad1a3f7SMatthew Dillon 
13092ad1a3f7SMatthew Dillon 	/*
13102ad1a3f7SMatthew Dillon 	 * Our scan
13112ad1a3f7SMatthew Dillon 	 */
13122ad1a3f7SMatthew Dillon 	p = TAILQ_FIRST(&backing_object->memq);
13132ad1a3f7SMatthew Dillon 	while (p) {
13142ad1a3f7SMatthew Dillon 		vm_page_t next = TAILQ_NEXT(p, listq);
13152ad1a3f7SMatthew Dillon 		vm_pindex_t new_pindex = p->pindex - backing_offset_index;
13162ad1a3f7SMatthew Dillon 
13172ad1a3f7SMatthew Dillon 		if (op & OBSC_TEST_ALL_SHADOWED) {
13182ad1a3f7SMatthew Dillon 			vm_page_t pp;
13192ad1a3f7SMatthew Dillon 
13202ad1a3f7SMatthew Dillon 			/*
13212ad1a3f7SMatthew Dillon 			 * Ignore pages outside the parent object's range
13222ad1a3f7SMatthew Dillon 			 * and outside the parent object's mapping of the
13232ad1a3f7SMatthew Dillon 			 * backing object.
13242ad1a3f7SMatthew Dillon 			 *
13252ad1a3f7SMatthew Dillon 			 * note that we do not busy the backing object's
13262ad1a3f7SMatthew Dillon 			 * page.
13272ad1a3f7SMatthew Dillon 			 */
13282ad1a3f7SMatthew Dillon 			if (
13292ad1a3f7SMatthew Dillon 			    p->pindex < backing_offset_index ||
13302ad1a3f7SMatthew Dillon 			    new_pindex >= object->size
13312ad1a3f7SMatthew Dillon 			) {
13322ad1a3f7SMatthew Dillon 				p = next;
13332ad1a3f7SMatthew Dillon 				continue;
13342ad1a3f7SMatthew Dillon 			}
13352ad1a3f7SMatthew Dillon 
13362ad1a3f7SMatthew Dillon 			/*
13372ad1a3f7SMatthew Dillon 			 * See if the parent has the page or if the parent's
13382ad1a3f7SMatthew Dillon 			 * object pager has the page.  If the parent has the
13392ad1a3f7SMatthew Dillon 			 * page but the page is not valid, the parent's
13402ad1a3f7SMatthew Dillon 			 * object pager must have the page.
13412ad1a3f7SMatthew Dillon 			 *
13422ad1a3f7SMatthew Dillon 			 * If this fails, the parent does not completely shadow
13432ad1a3f7SMatthew Dillon 			 * the object and we might as well give up now.
13442ad1a3f7SMatthew Dillon 			 */
13452ad1a3f7SMatthew Dillon 
13462ad1a3f7SMatthew Dillon 			pp = vm_page_lookup(object, new_pindex);
13472ad1a3f7SMatthew Dillon 			if (
13482ad1a3f7SMatthew Dillon 			    (pp == NULL || pp->valid == 0) &&
13492ad1a3f7SMatthew Dillon 			    !vm_pager_has_page(object, new_pindex, NULL, NULL)
13502ad1a3f7SMatthew Dillon 			) {
13512ad1a3f7SMatthew Dillon 				r = 0;
13522ad1a3f7SMatthew Dillon 				break;
13532ad1a3f7SMatthew Dillon 			}
13542ad1a3f7SMatthew Dillon 		}
13552ad1a3f7SMatthew Dillon 
13562ad1a3f7SMatthew Dillon 		/*
13572ad1a3f7SMatthew Dillon 		 * Check for busy page
13582ad1a3f7SMatthew Dillon 		 */
13592ad1a3f7SMatthew Dillon 		if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
13602ad1a3f7SMatthew Dillon 			vm_page_t pp;
13612ad1a3f7SMatthew Dillon 
136200f9e8b4SAlan Cox 			vm_page_lock_queues();
13632ad1a3f7SMatthew Dillon 			if (op & OBSC_COLLAPSE_NOWAIT) {
136400f9e8b4SAlan Cox 				if ((p->flags & PG_BUSY) ||
13652ad1a3f7SMatthew Dillon 				    !p->valid ||
13662ad1a3f7SMatthew Dillon 				    p->hold_count ||
13672ad1a3f7SMatthew Dillon 				    p->wire_count ||
136800f9e8b4SAlan Cox 				    p->busy) {
136900f9e8b4SAlan Cox 					vm_page_unlock_queues();
13702ad1a3f7SMatthew Dillon 					p = next;
13712ad1a3f7SMatthew Dillon 					continue;
13722ad1a3f7SMatthew Dillon 				}
13732ad1a3f7SMatthew Dillon 			} else if (op & OBSC_COLLAPSE_WAIT) {
137400f9e8b4SAlan Cox 				if (vm_page_sleep_if_busy(p, TRUE, "vmocol")) {
13752ad1a3f7SMatthew Dillon 					/*
13762ad1a3f7SMatthew Dillon 					 * If we slept, anything could have
13772ad1a3f7SMatthew Dillon 					 * happened.  Since the object is
13782ad1a3f7SMatthew Dillon 					 * marked dead, the backing offset
13792ad1a3f7SMatthew Dillon 					 * should not have changed so we
13802ad1a3f7SMatthew Dillon 					 * just restart our scan.
13812ad1a3f7SMatthew Dillon 					 */
13822ad1a3f7SMatthew Dillon 					p = TAILQ_FIRST(&backing_object->memq);
13832ad1a3f7SMatthew Dillon 					continue;
13842ad1a3f7SMatthew Dillon 				}
13852ad1a3f7SMatthew Dillon 			}
13862ad1a3f7SMatthew Dillon 
13872ad1a3f7SMatthew Dillon 			/*
13882ad1a3f7SMatthew Dillon 			 * Busy the page
13892ad1a3f7SMatthew Dillon 			 */
13902ad1a3f7SMatthew Dillon 			vm_page_busy(p);
139100f9e8b4SAlan Cox 			vm_page_unlock_queues();
13922ad1a3f7SMatthew Dillon 
13932ad1a3f7SMatthew Dillon 			KASSERT(
13942ad1a3f7SMatthew Dillon 			    p->object == backing_object,
13952ad1a3f7SMatthew Dillon 			    ("vm_object_qcollapse(): object mismatch")
13962ad1a3f7SMatthew Dillon 			);
13972ad1a3f7SMatthew Dillon 
13982ad1a3f7SMatthew Dillon 			/*
13992ad1a3f7SMatthew Dillon 			 * Destroy any associated swap
14002ad1a3f7SMatthew Dillon 			 */
14012ad1a3f7SMatthew Dillon 			if (backing_object->type == OBJT_SWAP) {
14022ad1a3f7SMatthew Dillon 				swap_pager_freespace(
14032ad1a3f7SMatthew Dillon 				    backing_object,
14042ad1a3f7SMatthew Dillon 				    p->pindex,
14052ad1a3f7SMatthew Dillon 				    1
14062ad1a3f7SMatthew Dillon 				);
14072ad1a3f7SMatthew Dillon 			}
14082ad1a3f7SMatthew Dillon 
14092ad1a3f7SMatthew Dillon 			if (
14102ad1a3f7SMatthew Dillon 			    p->pindex < backing_offset_index ||
14112ad1a3f7SMatthew Dillon 			    new_pindex >= object->size
14122ad1a3f7SMatthew Dillon 			) {
14132ad1a3f7SMatthew Dillon 				/*
14142ad1a3f7SMatthew Dillon 				 * Page is out of the parent object's range, we
14152ad1a3f7SMatthew Dillon 				 * can simply destroy it.
14162ad1a3f7SMatthew Dillon 				 */
14176a684ecfSAlan Cox 				vm_page_lock_queues();
14184fec79beSAlan Cox 				pmap_remove_all(p);
14192ad1a3f7SMatthew Dillon 				vm_page_free(p);
14206a684ecfSAlan Cox 				vm_page_unlock_queues();
14212ad1a3f7SMatthew Dillon 				p = next;
14222ad1a3f7SMatthew Dillon 				continue;
14232ad1a3f7SMatthew Dillon 			}
14242ad1a3f7SMatthew Dillon 
14252ad1a3f7SMatthew Dillon 			pp = vm_page_lookup(object, new_pindex);
14262ad1a3f7SMatthew Dillon 			if (
14272ad1a3f7SMatthew Dillon 			    pp != NULL ||
14282ad1a3f7SMatthew Dillon 			    vm_pager_has_page(object, new_pindex, NULL, NULL)
14292ad1a3f7SMatthew Dillon 			) {
14302ad1a3f7SMatthew Dillon 				/*
14312ad1a3f7SMatthew Dillon 				 * page already exists in parent OR swap exists
14322ad1a3f7SMatthew Dillon 				 * for this location in the parent.  Destroy
14332ad1a3f7SMatthew Dillon 				 * the original page from the backing object.
14342ad1a3f7SMatthew Dillon 				 *
14352ad1a3f7SMatthew Dillon 				 * Leave the parent's page alone
14362ad1a3f7SMatthew Dillon 				 */
14376a684ecfSAlan Cox 				vm_page_lock_queues();
14384fec79beSAlan Cox 				pmap_remove_all(p);
14392ad1a3f7SMatthew Dillon 				vm_page_free(p);
14406a684ecfSAlan Cox 				vm_page_unlock_queues();
14412ad1a3f7SMatthew Dillon 				p = next;
14422ad1a3f7SMatthew Dillon 				continue;
14432ad1a3f7SMatthew Dillon 			}
14442ad1a3f7SMatthew Dillon 
14452ad1a3f7SMatthew Dillon 			/*
14462ad1a3f7SMatthew Dillon 			 * Page does not exist in parent, rename the
14472ad1a3f7SMatthew Dillon 			 * page from the backing object to the main object.
1448d1bf5d56SMatthew Dillon 			 *
1449d1bf5d56SMatthew Dillon 			 * If the page was mapped to a process, it can remain
1450d1bf5d56SMatthew Dillon 			 * mapped through the rename.
14512ad1a3f7SMatthew Dillon 			 */
1452a28cc55eSAlan Cox 			vm_page_lock_queues();
14532ad1a3f7SMatthew Dillon 			vm_page_rename(p, object, new_pindex);
1454a28cc55eSAlan Cox 			vm_page_unlock_queues();
14552ad1a3f7SMatthew Dillon 			/* page automatically made dirty by rename */
14562ad1a3f7SMatthew Dillon 		}
14572ad1a3f7SMatthew Dillon 		p = next;
14582ad1a3f7SMatthew Dillon 	}
14592ad1a3f7SMatthew Dillon 	splx(s);
14602ad1a3f7SMatthew Dillon 	return (r);
14612ad1a3f7SMatthew Dillon }
14622ad1a3f7SMatthew Dillon 
1463df8bae1dSRodney W. Grimes 
1464df8bae1dSRodney W. Grimes /*
14652fe6e4d7SDavid Greenman  * this version of collapse allows the operation to occur earlier and
14662fe6e4d7SDavid Greenman  * when paging_in_progress is true for an object...  This is not a complete
14672fe6e4d7SDavid Greenman  * operation, but should plug 99.9% of the rest of the leaks.
14682fe6e4d7SDavid Greenman  */
14692fe6e4d7SDavid Greenman static void
14701b40f8c0SMatthew Dillon vm_object_qcollapse(vm_object_t object)
14712fe6e4d7SDavid Greenman {
14722ad1a3f7SMatthew Dillon 	vm_object_t backing_object = object->backing_object;
14732fe6e4d7SDavid Greenman 
14741b40f8c0SMatthew Dillon 	GIANT_REQUIRED;
14751b40f8c0SMatthew Dillon 
14762fe6e4d7SDavid Greenman 	if (backing_object->ref_count != 1)
14772fe6e4d7SDavid Greenman 		return;
14782fe6e4d7SDavid Greenman 
1479010cf3b9SDavid Greenman 	backing_object->ref_count += 2;
1480010cf3b9SDavid Greenman 
14812ad1a3f7SMatthew Dillon 	vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
14821c7c3c6aSMatthew Dillon 
1483010cf3b9SDavid Greenman 	backing_object->ref_count -= 2;
14842fe6e4d7SDavid Greenman }
14852fe6e4d7SDavid Greenman 
1486df8bae1dSRodney W. Grimes /*
1487df8bae1dSRodney W. Grimes  *	vm_object_collapse:
1488df8bae1dSRodney W. Grimes  *
1489df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
1490df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
1491df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
1492df8bae1dSRodney W. Grimes  */
149326f9a767SRodney W. Grimes void
14941b40f8c0SMatthew Dillon vm_object_collapse(vm_object_t object)
1495df8bae1dSRodney W. Grimes {
14960cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
149723955314SAlfred Perlstein 
1498df8bae1dSRodney W. Grimes 	while (TRUE) {
14992ad1a3f7SMatthew Dillon 		vm_object_t backing_object;
15002ad1a3f7SMatthew Dillon 
1501df8bae1dSRodney W. Grimes 		/*
1502df8bae1dSRodney W. Grimes 		 * Verify that the conditions are right for collapse:
1503df8bae1dSRodney W. Grimes 		 *
15042ad1a3f7SMatthew Dillon 		 * The object exists and the backing object exists.
1505df8bae1dSRodney W. Grimes 		 */
15062fe6e4d7SDavid Greenman 		if (object == NULL)
15072ad1a3f7SMatthew Dillon 			break;
1508df8bae1dSRodney W. Grimes 
150924a1cce3SDavid Greenman 		if ((backing_object = object->backing_object) == NULL)
15102ad1a3f7SMatthew Dillon 			break;
1511df8bae1dSRodney W. Grimes 
1512f919ebdeSDavid Greenman 		/*
1513f919ebdeSDavid Greenman 		 * we check the backing object first, because it is most likely
151424a1cce3SDavid Greenman 		 * not collapsable.
1515f919ebdeSDavid Greenman 		 */
151624a1cce3SDavid Greenman 		if (backing_object->handle != NULL ||
151724a1cce3SDavid Greenman 		    (backing_object->type != OBJT_DEFAULT &&
151824a1cce3SDavid Greenman 		     backing_object->type != OBJT_SWAP) ||
1519f919ebdeSDavid Greenman 		    (backing_object->flags & OBJ_DEAD) ||
152024a1cce3SDavid Greenman 		    object->handle != NULL ||
152124a1cce3SDavid Greenman 		    (object->type != OBJT_DEFAULT &&
152224a1cce3SDavid Greenman 		     object->type != OBJT_SWAP) ||
152324a1cce3SDavid Greenman 		    (object->flags & OBJ_DEAD)) {
15242ad1a3f7SMatthew Dillon 			break;
152524a1cce3SDavid Greenman 		}
15269b4814bbSDavid Greenman 
15272ad1a3f7SMatthew Dillon 		if (
15282ad1a3f7SMatthew Dillon 		    object->paging_in_progress != 0 ||
15292ad1a3f7SMatthew Dillon 		    backing_object->paging_in_progress != 0
15302ad1a3f7SMatthew Dillon 		) {
1531b9921222SDavid Greenman 			vm_object_qcollapse(object);
15322ad1a3f7SMatthew Dillon 			break;
1533df8bae1dSRodney W. Grimes 		}
1534f919ebdeSDavid Greenman 
153526f9a767SRodney W. Grimes 		/*
15360d94caffSDavid Greenman 		 * We know that we can either collapse the backing object (if
15372ad1a3f7SMatthew Dillon 		 * the parent is the only reference to it) or (perhaps) have
15382ad1a3f7SMatthew Dillon 		 * the parent bypass the object if the parent happens to shadow
15392ad1a3f7SMatthew Dillon 		 * all the resident pages in the entire backing object.
15402ad1a3f7SMatthew Dillon 		 *
15412ad1a3f7SMatthew Dillon 		 * This is ignoring pager-backed pages such as swap pages.
15422ad1a3f7SMatthew Dillon 		 * vm_object_backing_scan fails the shadowing test in this
15432ad1a3f7SMatthew Dillon 		 * case.
1544df8bae1dSRodney W. Grimes 		 */
1545df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
1546df8bae1dSRodney W. Grimes 			/*
15472ad1a3f7SMatthew Dillon 			 * If there is exactly one reference to the backing
15482ad1a3f7SMatthew Dillon 			 * object, we can collapse it into the parent.
1549df8bae1dSRodney W. Grimes 			 */
15502ad1a3f7SMatthew Dillon 			vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1551df8bae1dSRodney W. Grimes 
1552df8bae1dSRodney W. Grimes 			/*
1553df8bae1dSRodney W. Grimes 			 * Move the pager from backing_object to object.
1554df8bae1dSRodney W. Grimes 			 */
155524a1cce3SDavid Greenman 			if (backing_object->type == OBJT_SWAP) {
1556d474eaaaSDoug Rabson 				vm_object_pip_add(backing_object, 1);
155724a1cce3SDavid Greenman 
155824a1cce3SDavid Greenman 				/*
15591c7c3c6aSMatthew Dillon 				 * scrap the paging_offset junk and do a
15601c7c3c6aSMatthew Dillon 				 * discrete copy.  This also removes major
15611c7c3c6aSMatthew Dillon 				 * assumptions about how the swap-pager
15621c7c3c6aSMatthew Dillon 				 * works from where it doesn't belong.  The
15631c7c3c6aSMatthew Dillon 				 * new swapper is able to optimize the
15641c7c3c6aSMatthew Dillon 				 * destroy-source case.
156524a1cce3SDavid Greenman 				 */
15661c7c3c6aSMatthew Dillon 				vm_object_pip_add(object, 1);
15671c7c3c6aSMatthew Dillon 				swap_pager_copy(
15681c7c3c6aSMatthew Dillon 				    backing_object,
15691c7c3c6aSMatthew Dillon 				    object,
15701c7c3c6aSMatthew Dillon 				    OFF_TO_IDX(object->backing_object_offset), TRUE);
1571f919ebdeSDavid Greenman 				vm_object_pip_wakeup(object);
1572c0503609SDavid Greenman 
1573f919ebdeSDavid Greenman 				vm_object_pip_wakeup(backing_object);
1574c0503609SDavid Greenman 			}
1575df8bae1dSRodney W. Grimes 			/*
1576df8bae1dSRodney W. Grimes 			 * Object now shadows whatever backing_object did.
15772ad1a3f7SMatthew Dillon 			 * Note that the reference to
15782ad1a3f7SMatthew Dillon 			 * backing_object->backing_object moves from within
15792ad1a3f7SMatthew Dillon 			 * backing_object to within object.
1580df8bae1dSRodney W. Grimes 			 */
15812ad1a3f7SMatthew Dillon 			TAILQ_REMOVE(
15822ad1a3f7SMatthew Dillon 			    &object->backing_object->shadow_head,
15832ad1a3f7SMatthew Dillon 			    object,
15842ad1a3f7SMatthew Dillon 			    shadow_list
15852ad1a3f7SMatthew Dillon 			);
1586eaf13dd7SJohn Dyson 			object->backing_object->shadow_count--;
1587eaf13dd7SJohn Dyson 			object->backing_object->generation++;
1588de5f6a77SJohn Dyson 			if (backing_object->backing_object) {
15892ad1a3f7SMatthew Dillon 				TAILQ_REMOVE(
15902ad1a3f7SMatthew Dillon 				    &backing_object->backing_object->shadow_head,
15912ad1a3f7SMatthew Dillon 				    backing_object,
15922ad1a3f7SMatthew Dillon 				    shadow_list
15932ad1a3f7SMatthew Dillon 				);
1594eaf13dd7SJohn Dyson 				backing_object->backing_object->shadow_count--;
1595eaf13dd7SJohn Dyson 				backing_object->backing_object->generation++;
1596de5f6a77SJohn Dyson 			}
159724a1cce3SDavid Greenman 			object->backing_object = backing_object->backing_object;
1598de5f6a77SJohn Dyson 			if (object->backing_object) {
15992ad1a3f7SMatthew Dillon 				TAILQ_INSERT_TAIL(
16002ad1a3f7SMatthew Dillon 				    &object->backing_object->shadow_head,
16012ad1a3f7SMatthew Dillon 				    object,
16022ad1a3f7SMatthew Dillon 				    shadow_list
16032ad1a3f7SMatthew Dillon 				);
1604eaf13dd7SJohn Dyson 				object->backing_object->shadow_count++;
1605eaf13dd7SJohn Dyson 				object->backing_object->generation++;
1606de5f6a77SJohn Dyson 			}
16072fe6e4d7SDavid Greenman 
16082ad1a3f7SMatthew Dillon 			object->backing_object_offset +=
16092ad1a3f7SMatthew Dillon 			    backing_object->backing_object_offset;
16102ad1a3f7SMatthew Dillon 
1611df8bae1dSRodney W. Grimes 			/*
1612df8bae1dSRodney W. Grimes 			 * Discard backing_object.
1613df8bae1dSRodney W. Grimes 			 *
16140d94caffSDavid Greenman 			 * Since the backing object has no pages, no pager left,
16150d94caffSDavid Greenman 			 * and no object references within it, all that is
16160d94caffSDavid Greenman 			 * necessary is to dispose of it.
1617df8bae1dSRodney W. Grimes 			 */
1618245df27cSMatthew Dillon 			KASSERT(backing_object->ref_count == 1, ("backing_object %p was somehow re-referenced during collapse!", backing_object));
1619245df27cSMatthew Dillon 			KASSERT(TAILQ_FIRST(&backing_object->memq) == NULL, ("backing_object %p somehow has left over pages during collapse!", backing_object));
1620df8bae1dSRodney W. Grimes 
1621a5698387SAlan Cox 			mtx_lock(&vm_object_list_mtx);
16222ad1a3f7SMatthew Dillon 			TAILQ_REMOVE(
16232ad1a3f7SMatthew Dillon 			    &vm_object_list,
16242ad1a3f7SMatthew Dillon 			    backing_object,
16252ad1a3f7SMatthew Dillon 			    object_list
16262ad1a3f7SMatthew Dillon 			);
1627a5698387SAlan Cox 			mtx_unlock(&vm_object_list_mtx);
1628df8bae1dSRodney W. Grimes 
1629e3a9e1b2SAlan Cox 			mtx_destroy(&backing_object->mtx);
1630e3a9e1b2SAlan Cox 
1631670d17b5SJeff Roberson 			uma_zfree(obj_zone, backing_object);
1632df8bae1dSRodney W. Grimes 
1633df8bae1dSRodney W. Grimes 			object_collapses++;
16340d94caffSDavid Greenman 		} else {
163595e5e988SJohn Dyson 			vm_object_t new_backing_object;
1636df8bae1dSRodney W. Grimes 
1637df8bae1dSRodney W. Grimes 			/*
16382ad1a3f7SMatthew Dillon 			 * If we do not entirely shadow the backing object,
16392ad1a3f7SMatthew Dillon 			 * there is nothing we can do so we give up.
1640df8bae1dSRodney W. Grimes 			 */
16412ad1a3f7SMatthew Dillon 			if (vm_object_backing_scan(object, OBSC_TEST_ALL_SHADOWED) == 0) {
16422ad1a3f7SMatthew Dillon 				break;
164324a1cce3SDavid Greenman 			}
1644df8bae1dSRodney W. Grimes 
1645df8bae1dSRodney W. Grimes 			/*
16460d94caffSDavid Greenman 			 * Make the parent shadow the next object in the
16470d94caffSDavid Greenman 			 * chain.  Deallocating backing_object will not remove
16480d94caffSDavid Greenman 			 * it, since its reference count is at least 2.
1649df8bae1dSRodney W. Grimes 			 */
16502ad1a3f7SMatthew Dillon 			TAILQ_REMOVE(
16512ad1a3f7SMatthew Dillon 			    &backing_object->shadow_head,
16522ad1a3f7SMatthew Dillon 			    object,
16532ad1a3f7SMatthew Dillon 			    shadow_list
16542ad1a3f7SMatthew Dillon 			);
1655eaf13dd7SJohn Dyson 			backing_object->shadow_count--;
1656eaf13dd7SJohn Dyson 			backing_object->generation++;
165795e5e988SJohn Dyson 
165895e5e988SJohn Dyson 			new_backing_object = backing_object->backing_object;
16598aef1712SMatthew Dillon 			if ((object->backing_object = new_backing_object) != NULL) {
166095e5e988SJohn Dyson 				vm_object_reference(new_backing_object);
16612ad1a3f7SMatthew Dillon 				TAILQ_INSERT_TAIL(
16622ad1a3f7SMatthew Dillon 				    &new_backing_object->shadow_head,
16632ad1a3f7SMatthew Dillon 				    object,
16642ad1a3f7SMatthew Dillon 				    shadow_list
16652ad1a3f7SMatthew Dillon 				);
1666eaf13dd7SJohn Dyson 				new_backing_object->shadow_count++;
1667eaf13dd7SJohn Dyson 				new_backing_object->generation++;
166895e5e988SJohn Dyson 				object->backing_object_offset +=
166995e5e988SJohn Dyson 					backing_object->backing_object_offset;
1670de5f6a77SJohn Dyson 			}
1671df8bae1dSRodney W. Grimes 
1672df8bae1dSRodney W. Grimes 			/*
16730d94caffSDavid Greenman 			 * Drop the reference count on backing_object. Since
16740d94caffSDavid Greenman 			 * its ref_count was at least 2, it will not vanish;
1675eaf13dd7SJohn Dyson 			 * so we don't need to call vm_object_deallocate, but
1676eaf13dd7SJohn Dyson 			 * we do anyway.
1677df8bae1dSRodney W. Grimes 			 */
167895e5e988SJohn Dyson 			vm_object_deallocate(backing_object);
1679df8bae1dSRodney W. Grimes 			object_bypasses++;
1680df8bae1dSRodney W. Grimes 		}
1681df8bae1dSRodney W. Grimes 
1682df8bae1dSRodney W. Grimes 		/*
1683df8bae1dSRodney W. Grimes 		 * Try again with this object's new backing object.
1684df8bae1dSRodney W. Grimes 		 */
1685df8bae1dSRodney W. Grimes 	}
1686df8bae1dSRodney W. Grimes }
1687df8bae1dSRodney W. Grimes 
1688df8bae1dSRodney W. Grimes /*
1689df8bae1dSRodney W. Grimes  *	vm_object_page_remove: [internal]
1690df8bae1dSRodney W. Grimes  *
1691df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
1692df8bae1dSRodney W. Grimes  *	object range from the object's list of pages.
1693df8bae1dSRodney W. Grimes  *
1694df8bae1dSRodney W. Grimes  *	The object must be locked.
1695df8bae1dSRodney W. Grimes  */
169626f9a767SRodney W. Grimes void
16971b40f8c0SMatthew Dillon vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end, boolean_t clean_only)
1698df8bae1dSRodney W. Grimes {
1699d031cff1SMatthew Dillon 	vm_page_t p, next;
17006395da54SIan Dowse 	vm_pindex_t size;
1701f5ef029eSPoul-Henning Kamp 	int all;
1702df8bae1dSRodney W. Grimes 
17032840cabeSAlan Cox 	if (object == NULL ||
17042840cabeSAlan Cox 	    object->resident_page_count == 0)
1705df8bae1dSRodney W. Grimes 		return;
170695e5e988SJohn Dyson 	all = ((end == 0) && (start == 0));
170795e5e988SJohn Dyson 
17088b03c8edSMatthew Dillon 	/*
17098b03c8edSMatthew Dillon 	 * Since physically-backed objects do not use managed pages, we can't
17108b03c8edSMatthew Dillon 	 * remove pages from the object (we must instead remove the page
17118b03c8edSMatthew Dillon 	 * references, and then destroy the object).
17128b03c8edSMatthew Dillon 	 */
17138b03c8edSMatthew Dillon 	KASSERT(object->type != OBJT_PHYS, ("attempt to remove pages from a physical object"));
17148b03c8edSMatthew Dillon 
1715d474eaaaSDoug Rabson 	vm_object_pip_add(object, 1);
171626f9a767SRodney W. Grimes again:
171732585dd6SAlan Cox 	vm_page_lock_queues();
171826f9a767SRodney W. Grimes 	size = end - start;
1719a1a54e9fSAlan Cox 	if (all || size > object->resident_page_count / 4) {
1720b18bfc3dSJohn Dyson 		for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
1721b18bfc3dSJohn Dyson 			next = TAILQ_NEXT(p, listq);
172295e5e988SJohn Dyson 			if (all || ((start <= p->pindex) && (p->pindex < end))) {
1723bd7e5f99SJohn Dyson 				if (p->wire_count != 0) {
17244fec79beSAlan Cox 					pmap_remove_all(p);
1725ce65e68cSDavid Greenman 					if (!clean_only)
1726bd7e5f99SJohn Dyson 						p->valid = 0;
17270d94caffSDavid Greenman 					continue;
17280d94caffSDavid Greenman 				}
17290891ef4cSJohn Dyson 
1730b18bfc3dSJohn Dyson 				/*
1731b18bfc3dSJohn Dyson 				 * The busy flags are only cleared at
1732b18bfc3dSJohn Dyson 				 * interrupt -- minimize the spl transitions
1733b18bfc3dSJohn Dyson 				 */
173432585dd6SAlan Cox  				if (vm_page_sleep_if_busy(p, TRUE, "vmopar"))
173526f9a767SRodney W. Grimes  					goto again;
17360891ef4cSJohn Dyson 
17378f9110f6SJohn Dyson 				if (clean_only && p->valid) {
17387c1f6cedSDavid Greenman 					vm_page_test_dirty(p);
17397c1f6cedSDavid Greenman 					if (p->valid & p->dirty)
17407c1f6cedSDavid Greenman 						continue;
17417c1f6cedSDavid Greenman 				}
1742e69763a3SDoug Rabson 				vm_page_busy(p);
17434fec79beSAlan Cox 				pmap_remove_all(p);
1744df8bae1dSRodney W. Grimes 				vm_page_free(p);
174526f9a767SRodney W. Grimes 			}
174626f9a767SRodney W. Grimes 		}
174726f9a767SRodney W. Grimes 	} else {
174826f9a767SRodney W. Grimes 		while (size > 0) {
174932585dd6SAlan Cox 			if ((p = vm_page_lookup(object, start)) != NULL) {
1750bd7e5f99SJohn Dyson 				if (p->wire_count != 0) {
17514fec79beSAlan Cox 					pmap_remove_all(p);
1752ce65e68cSDavid Greenman 					if (!clean_only)
1753ce65e68cSDavid Greenman 						p->valid = 0;
1754bd7e5f99SJohn Dyson 					start += 1;
1755bd7e5f99SJohn Dyson 					size -= 1;
1756bd7e5f99SJohn Dyson 					continue;
17570d94caffSDavid Greenman 				}
1758eaf13dd7SJohn Dyson 
1759b18bfc3dSJohn Dyson 				/*
1760b18bfc3dSJohn Dyson 				 * The busy flags are only cleared at
1761b18bfc3dSJohn Dyson 				 * interrupt -- minimize the spl transitions
1762b18bfc3dSJohn Dyson 				 */
176332585dd6SAlan Cox 				if (vm_page_sleep_if_busy(p, TRUE, "vmopar"))
176426f9a767SRodney W. Grimes 					goto again;
1765eaf13dd7SJohn Dyson 
17668f9110f6SJohn Dyson 				if (clean_only && p->valid) {
17677c1f6cedSDavid Greenman 					vm_page_test_dirty(p);
1768bd7e5f99SJohn Dyson 					if (p->valid & p->dirty) {
1769bd7e5f99SJohn Dyson 						start += 1;
1770bd7e5f99SJohn Dyson 						size -= 1;
17717c1f6cedSDavid Greenman 						continue;
17727c1f6cedSDavid Greenman 					}
1773bd7e5f99SJohn Dyson 				}
1774e69763a3SDoug Rabson 				vm_page_busy(p);
17754fec79beSAlan Cox 				pmap_remove_all(p);
177626f9a767SRodney W. Grimes 				vm_page_free(p);
177726f9a767SRodney W. Grimes 			}
1778a316d390SJohn Dyson 			start += 1;
1779a316d390SJohn Dyson 			size -= 1;
1780df8bae1dSRodney W. Grimes 		}
1781df8bae1dSRodney W. Grimes 	}
178232585dd6SAlan Cox 	vm_page_unlock_queues();
1783f919ebdeSDavid Greenman 	vm_object_pip_wakeup(object);
1784c0503609SDavid Greenman }
1785df8bae1dSRodney W. Grimes 
1786df8bae1dSRodney W. Grimes /*
1787df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
1788df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
1789df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
1790df8bae1dSRodney W. Grimes  *
1791df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
1792df8bae1dSRodney W. Grimes  *
1793df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
1794df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
1795df8bae1dSRodney W. Grimes  *
1796df8bae1dSRodney W. Grimes  *	Parameters:
1797df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
1798df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
1799df8bae1dSRodney W. Grimes  *		next_object	Second object into coalesce
1800df8bae1dSRodney W. Grimes  *		next_offset	Offset into next_object
1801df8bae1dSRodney W. Grimes  *
1802df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
1803df8bae1dSRodney W. Grimes  *		next_size	Size of reference to next_object
1804df8bae1dSRodney W. Grimes  *
1805df8bae1dSRodney W. Grimes  *	Conditions:
1806df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
1807df8bae1dSRodney W. Grimes  */
18080d94caffSDavid Greenman boolean_t
180900e1854aSAlan Cox vm_object_coalesce(vm_object_t prev_object, vm_pindex_t prev_pindex,
181000e1854aSAlan Cox 	vm_size_t prev_size, vm_size_t next_size)
1811df8bae1dSRodney W. Grimes {
1812ea41812fSAlan Cox 	vm_pindex_t next_pindex;
1813df8bae1dSRodney W. Grimes 
181400e1854aSAlan Cox 	if (prev_object == NULL)
1815df8bae1dSRodney W. Grimes 		return (TRUE);
181615c176c1SAlan Cox 	vm_object_lock(prev_object);
18174112823fSMatthew Dillon 	if (prev_object->type != OBJT_DEFAULT &&
18184112823fSMatthew Dillon 	    prev_object->type != OBJT_SWAP) {
181915c176c1SAlan Cox 		vm_object_unlock(prev_object);
182030dcfc09SJohn Dyson 		return (FALSE);
182130dcfc09SJohn Dyson 	}
182230dcfc09SJohn Dyson 
1823df8bae1dSRodney W. Grimes 	/*
1824df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
1825df8bae1dSRodney W. Grimes 	 */
1826df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
1827df8bae1dSRodney W. Grimes 
1828df8bae1dSRodney W. Grimes 	/*
18290d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
18300d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
18310d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
1832df8bae1dSRodney W. Grimes 	 */
18338cc7e047SJohn Dyson 	if (prev_object->backing_object != NULL) {
183415c176c1SAlan Cox 		vm_object_unlock(prev_object);
1835df8bae1dSRodney W. Grimes 		return (FALSE);
1836df8bae1dSRodney W. Grimes 	}
1837a316d390SJohn Dyson 
1838a316d390SJohn Dyson 	prev_size >>= PAGE_SHIFT;
1839a316d390SJohn Dyson 	next_size >>= PAGE_SHIFT;
1840ea41812fSAlan Cox 	next_pindex = prev_pindex + prev_size;
18418cc7e047SJohn Dyson 
18428cc7e047SJohn Dyson 	if ((prev_object->ref_count > 1) &&
1843ea41812fSAlan Cox 	    (prev_object->size != next_pindex)) {
184415c176c1SAlan Cox 		vm_object_unlock(prev_object);
18458cc7e047SJohn Dyson 		return (FALSE);
18468cc7e047SJohn Dyson 	}
18478cc7e047SJohn Dyson 
1848df8bae1dSRodney W. Grimes 	/*
18490d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
18500d94caffSDavid Greenman 	 * deallocation.
1851df8bae1dSRodney W. Grimes 	 */
1852ea41812fSAlan Cox 	if (next_pindex < prev_object->size) {
1853df8bae1dSRodney W. Grimes 		vm_object_page_remove(prev_object,
1854ea41812fSAlan Cox 				      next_pindex,
1855ea41812fSAlan Cox 				      next_pindex + next_size, FALSE);
1856ea41812fSAlan Cox 		if (prev_object->type == OBJT_SWAP)
1857ea41812fSAlan Cox 			swap_pager_freespace(prev_object,
1858ea41812fSAlan Cox 					     next_pindex, next_size);
1859ea41812fSAlan Cox 	}
1860df8bae1dSRodney W. Grimes 
1861df8bae1dSRodney W. Grimes 	/*
1862df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
1863df8bae1dSRodney W. Grimes 	 */
1864ea41812fSAlan Cox 	if (next_pindex + next_size > prev_object->size)
1865ea41812fSAlan Cox 		prev_object->size = next_pindex + next_size;
1866df8bae1dSRodney W. Grimes 
186715c176c1SAlan Cox 	vm_object_unlock(prev_object);
1868df8bae1dSRodney W. Grimes 	return (TRUE);
1869df8bae1dSRodney W. Grimes }
1870df8bae1dSRodney W. Grimes 
18717a5a6352SMatthew Dillon void
18727a5a6352SMatthew Dillon vm_object_set_writeable_dirty(vm_object_t object)
18737a5a6352SMatthew Dillon {
18747a5a6352SMatthew Dillon 	struct vnode *vp;
18757a5a6352SMatthew Dillon 
18767a5a6352SMatthew Dillon 	vm_object_set_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
18777a5a6352SMatthew Dillon 	if (object->type == OBJT_VNODE &&
18787a5a6352SMatthew Dillon 	    (vp = (struct vnode *)object->handle) != NULL) {
1879e6e370a7SJeff Roberson 		VI_LOCK(vp);
1880e6e370a7SJeff Roberson 		if ((vp->v_iflag & VI_OBJDIRTY) == 0)
1881e6e370a7SJeff Roberson 			vp->v_iflag |= VI_OBJDIRTY;
1882e6e370a7SJeff Roberson 		VI_UNLOCK(vp);
18837a5a6352SMatthew Dillon 	}
18847a5a6352SMatthew Dillon }
18857a5a6352SMatthew Dillon 
1886c0b6bbb8SAlan Cox #ifdef ENABLE_VFS_IOOPT
1887e86256c1SAlan Cox /*
1888c0b6bbb8SAlan Cox  * Experimental support for zero-copy I/O
1889c0b6bbb8SAlan Cox  *
1890e86256c1SAlan Cox  * Performs the copy_on_write operations necessary to allow the virtual copies
1891e86256c1SAlan Cox  * into user space to work.  This has to be called for write(2) system calls
1892e86256c1SAlan Cox  * from other processes, file unlinking, and file size shrinkage.
1893e86256c1SAlan Cox  */
1894e86256c1SAlan Cox void
1895e86256c1SAlan Cox vm_freeze_copyopts(vm_object_t object, vm_pindex_t froma, vm_pindex_t toa)
1896e86256c1SAlan Cox {
1897e86256c1SAlan Cox 	int rv;
1898e86256c1SAlan Cox 	vm_object_t robject;
1899e86256c1SAlan Cox 	vm_pindex_t idx;
1900e86256c1SAlan Cox 
1901e86256c1SAlan Cox 	GIANT_REQUIRED;
1902e86256c1SAlan Cox 	if ((object == NULL) ||
1903e86256c1SAlan Cox 		((object->flags & OBJ_OPT) == 0))
1904e86256c1SAlan Cox 		return;
1905e86256c1SAlan Cox 
1906e86256c1SAlan Cox 	if (object->shadow_count > object->ref_count)
1907e86256c1SAlan Cox 		panic("vm_freeze_copyopts: sc > rc");
1908e86256c1SAlan Cox 
1909e86256c1SAlan Cox 	while ((robject = TAILQ_FIRST(&object->shadow_head)) != NULL) {
1910e86256c1SAlan Cox 		vm_pindex_t bo_pindex;
1911e86256c1SAlan Cox 		vm_page_t m_in, m_out;
1912e86256c1SAlan Cox 
1913e86256c1SAlan Cox 		bo_pindex = OFF_TO_IDX(robject->backing_object_offset);
1914e86256c1SAlan Cox 
1915e86256c1SAlan Cox 		vm_object_reference(robject);
1916e86256c1SAlan Cox 
1917e86256c1SAlan Cox 		vm_object_pip_wait(robject, "objfrz");
1918e86256c1SAlan Cox 
1919e86256c1SAlan Cox 		if (robject->ref_count == 1) {
1920e86256c1SAlan Cox 			vm_object_deallocate(robject);
1921e86256c1SAlan Cox 			continue;
1922e86256c1SAlan Cox 		}
1923e86256c1SAlan Cox 
1924e86256c1SAlan Cox 		vm_object_pip_add(robject, 1);
1925e86256c1SAlan Cox 
1926e86256c1SAlan Cox 		for (idx = 0; idx < robject->size; idx++) {
1927e86256c1SAlan Cox 
1928e86256c1SAlan Cox 			m_out = vm_page_grab(robject, idx,
1929e86256c1SAlan Cox 						VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1930e86256c1SAlan Cox 
1931e86256c1SAlan Cox 			if (m_out->valid == 0) {
1932e86256c1SAlan Cox 				m_in = vm_page_grab(object, bo_pindex + idx,
1933e86256c1SAlan Cox 						VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
193413dc71edSAlan Cox 				vm_page_lock_queues();
1935e86256c1SAlan Cox 				if (m_in->valid == 0) {
193613dc71edSAlan Cox 					vm_page_unlock_queues();
1937e86256c1SAlan Cox 					rv = vm_pager_get_pages(object, &m_in, 1, 0);
1938e86256c1SAlan Cox 					if (rv != VM_PAGER_OK) {
1939e86256c1SAlan Cox 						printf("vm_freeze_copyopts: cannot read page from file: %lx\n", (long)m_in->pindex);
1940e86256c1SAlan Cox 						continue;
1941e86256c1SAlan Cox 					}
194291bb74a8SAlan Cox 					vm_page_lock_queues();
1943e86256c1SAlan Cox 					vm_page_deactivate(m_in);
1944e86256c1SAlan Cox 				}
1945e86256c1SAlan Cox 
19464fec79beSAlan Cox 				pmap_remove_all(m_in);
194713dc71edSAlan Cox 				vm_page_unlock_queues();
1948e86256c1SAlan Cox 				pmap_copy_page(m_in, m_out);
1949dc907f66SAlan Cox 				vm_page_lock_queues();
1950e86256c1SAlan Cox 				m_out->valid = m_in->valid;
1951e86256c1SAlan Cox 				vm_page_dirty(m_out);
1952e86256c1SAlan Cox 				vm_page_activate(m_out);
1953e86256c1SAlan Cox 				vm_page_wakeup(m_in);
1954dc907f66SAlan Cox 			} else
1955dc907f66SAlan Cox 				vm_page_lock_queues();
1956e86256c1SAlan Cox 			vm_page_wakeup(m_out);
1957dc907f66SAlan Cox 			vm_page_unlock_queues();
1958e86256c1SAlan Cox 		}
1959e86256c1SAlan Cox 
1960e86256c1SAlan Cox 		object->shadow_count--;
1961e86256c1SAlan Cox 		object->ref_count--;
1962e86256c1SAlan Cox 		TAILQ_REMOVE(&object->shadow_head, robject, shadow_list);
1963e86256c1SAlan Cox 		robject->backing_object = NULL;
1964e86256c1SAlan Cox 		robject->backing_object_offset = 0;
1965e86256c1SAlan Cox 
1966e86256c1SAlan Cox 		vm_object_pip_wakeup(robject);
1967e86256c1SAlan Cox 		vm_object_deallocate(robject);
1968e86256c1SAlan Cox 	}
1969e86256c1SAlan Cox 
1970e86256c1SAlan Cox 	vm_object_clear_flag(object, OBJ_OPT);
1971e86256c1SAlan Cox }
1972c0b6bbb8SAlan Cox #endif
1973e86256c1SAlan Cox 
1974c7c34a24SBruce Evans #include "opt_ddb.h"
1975c3cb3e12SDavid Greenman #ifdef DDB
1976c7c34a24SBruce Evans #include <sys/kernel.h>
1977c7c34a24SBruce Evans 
1978ce9edcf5SPoul-Henning Kamp #include <sys/cons.h>
1979c7c34a24SBruce Evans 
1980c7c34a24SBruce Evans #include <ddb/ddb.h>
1981c7c34a24SBruce Evans 
1982cac597e4SBruce Evans static int
19831b40f8c0SMatthew Dillon _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
1984a1f6d91cSDavid Greenman {
1985a1f6d91cSDavid Greenman 	vm_map_t tmpm;
1986a1f6d91cSDavid Greenman 	vm_map_entry_t tmpe;
1987a1f6d91cSDavid Greenman 	vm_object_t obj;
1988a1f6d91cSDavid Greenman 	int entcount;
1989a1f6d91cSDavid Greenman 
1990a1f6d91cSDavid Greenman 	if (map == 0)
1991a1f6d91cSDavid Greenman 		return 0;
1992a1f6d91cSDavid Greenman 
1993a1f6d91cSDavid Greenman 	if (entry == 0) {
1994a1f6d91cSDavid Greenman 		tmpe = map->header.next;
1995a1f6d91cSDavid Greenman 		entcount = map->nentries;
1996a1f6d91cSDavid Greenman 		while (entcount-- && (tmpe != &map->header)) {
1997a1f6d91cSDavid Greenman 			if (_vm_object_in_map(map, object, tmpe)) {
1998a1f6d91cSDavid Greenman 				return 1;
1999a1f6d91cSDavid Greenman 			}
2000a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
2001a1f6d91cSDavid Greenman 		}
20029fdfe602SMatthew Dillon 	} else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
20039fdfe602SMatthew Dillon 		tmpm = entry->object.sub_map;
2004a1f6d91cSDavid Greenman 		tmpe = tmpm->header.next;
2005a1f6d91cSDavid Greenman 		entcount = tmpm->nentries;
2006a1f6d91cSDavid Greenman 		while (entcount-- && tmpe != &tmpm->header) {
2007a1f6d91cSDavid Greenman 			if (_vm_object_in_map(tmpm, object, tmpe)) {
2008a1f6d91cSDavid Greenman 				return 1;
2009a1f6d91cSDavid Greenman 			}
2010a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
2011a1f6d91cSDavid Greenman 		}
20128aef1712SMatthew Dillon 	} else if ((obj = entry->object.vm_object) != NULL) {
201324a1cce3SDavid Greenman 		for (; obj; obj = obj->backing_object)
2014a1f6d91cSDavid Greenman 			if (obj == object) {
2015a1f6d91cSDavid Greenman 				return 1;
2016a1f6d91cSDavid Greenman 			}
2017a1f6d91cSDavid Greenman 	}
2018a1f6d91cSDavid Greenman 	return 0;
2019a1f6d91cSDavid Greenman }
2020a1f6d91cSDavid Greenman 
2021cac597e4SBruce Evans static int
20221b40f8c0SMatthew Dillon vm_object_in_map(vm_object_t object)
2023a1f6d91cSDavid Greenman {
2024a1f6d91cSDavid Greenman 	struct proc *p;
20251005a129SJohn Baldwin 
202660517fd1SJohn Baldwin 	/* sx_slock(&allproc_lock); */
2027fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(p, &allproc, p_list) {
2028a1f6d91cSDavid Greenman 		if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
2029a1f6d91cSDavid Greenman 			continue;
2030553629ebSJake Burkholder 		if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
203160517fd1SJohn Baldwin 			/* sx_sunlock(&allproc_lock); */
2032a1f6d91cSDavid Greenman 			return 1;
2033a1f6d91cSDavid Greenman 		}
2034553629ebSJake Burkholder 	}
203560517fd1SJohn Baldwin 	/* sx_sunlock(&allproc_lock); */
2036a1f6d91cSDavid Greenman 	if (_vm_object_in_map(kernel_map, object, 0))
2037a1f6d91cSDavid Greenman 		return 1;
2038a1f6d91cSDavid Greenman 	if (_vm_object_in_map(kmem_map, object, 0))
2039a1f6d91cSDavid Greenman 		return 1;
2040a1f6d91cSDavid Greenman 	if (_vm_object_in_map(pager_map, object, 0))
2041a1f6d91cSDavid Greenman 		return 1;
2042a1f6d91cSDavid Greenman 	if (_vm_object_in_map(buffer_map, object, 0))
2043a1f6d91cSDavid Greenman 		return 1;
2044a1f6d91cSDavid Greenman 	return 0;
2045a1f6d91cSDavid Greenman }
2046a1f6d91cSDavid Greenman 
2047c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check)
2048f708ef1bSPoul-Henning Kamp {
2049a1f6d91cSDavid Greenman 	vm_object_t object;
2050a1f6d91cSDavid Greenman 
2051a1f6d91cSDavid Greenman 	/*
2052a1f6d91cSDavid Greenman 	 * make sure that internal objs are in a map somewhere
2053a1f6d91cSDavid Greenman 	 * and none have zero ref counts.
2054a1f6d91cSDavid Greenman 	 */
2055cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
205624a1cce3SDavid Greenman 		if (object->handle == NULL &&
205724a1cce3SDavid Greenman 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2058a1f6d91cSDavid Greenman 			if (object->ref_count == 0) {
20593efc015bSPeter Wemm 				db_printf("vmochk: internal obj has zero ref count: %ld\n",
20603efc015bSPeter Wemm 					(long)object->size);
2061a1f6d91cSDavid Greenman 			}
2062a1f6d91cSDavid Greenman 			if (!vm_object_in_map(object)) {
2063fc62ef1fSBruce Evans 				db_printf(
2064fc62ef1fSBruce Evans 			"vmochk: internal obj is not in a map: "
2065fc62ef1fSBruce Evans 			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2066fc62ef1fSBruce Evans 				    object->ref_count, (u_long)object->size,
2067fc62ef1fSBruce Evans 				    (u_long)object->size,
2068fc62ef1fSBruce Evans 				    (void *)object->backing_object);
2069a1f6d91cSDavid Greenman 			}
2070a1f6d91cSDavid Greenman 		}
2071a1f6d91cSDavid Greenman 	}
2072a1f6d91cSDavid Greenman }
2073a1f6d91cSDavid Greenman 
207426f9a767SRodney W. Grimes /*
2075df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
2076df8bae1dSRodney W. Grimes  */
2077c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static)
2078df8bae1dSRodney W. Grimes {
2079c7c34a24SBruce Evans 	/* XXX convert args. */
2080c7c34a24SBruce Evans 	vm_object_t object = (vm_object_t)addr;
2081c7c34a24SBruce Evans 	boolean_t full = have_addr;
2082c7c34a24SBruce Evans 
2083d031cff1SMatthew Dillon 	vm_page_t p;
2084df8bae1dSRodney W. Grimes 
2085c7c34a24SBruce Evans 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
2086c7c34a24SBruce Evans #define	count	was_count
2087c7c34a24SBruce Evans 
2088d031cff1SMatthew Dillon 	int count;
2089df8bae1dSRodney W. Grimes 
2090df8bae1dSRodney W. Grimes 	if (object == NULL)
2091df8bae1dSRodney W. Grimes 		return;
2092df8bae1dSRodney W. Grimes 
2093eb95adefSBruce Evans 	db_iprintf(
2094e47cd172SMaxime Henrion 	    "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x\n",
2095e47cd172SMaxime Henrion 	    object, (int)object->type, (uintmax_t)object->size,
2096eb95adefSBruce Evans 	    object->resident_page_count, object->ref_count, object->flags);
2097e47cd172SMaxime Henrion 	db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n",
20981c7c3c6aSMatthew Dillon 	    object->shadow_count,
2099eb95adefSBruce Evans 	    object->backing_object ? object->backing_object->ref_count : 0,
2100e47cd172SMaxime Henrion 	    object->backing_object, (uintmax_t)object->backing_object_offset);
2101df8bae1dSRodney W. Grimes 
2102df8bae1dSRodney W. Grimes 	if (!full)
2103df8bae1dSRodney W. Grimes 		return;
2104df8bae1dSRodney W. Grimes 
2105c7c34a24SBruce Evans 	db_indent += 2;
2106df8bae1dSRodney W. Grimes 	count = 0;
2107fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(p, &object->memq, listq) {
2108df8bae1dSRodney W. Grimes 		if (count == 0)
2109c7c34a24SBruce Evans 			db_iprintf("memory:=");
2110df8bae1dSRodney W. Grimes 		else if (count == 6) {
2111c7c34a24SBruce Evans 			db_printf("\n");
2112c7c34a24SBruce Evans 			db_iprintf(" ...");
2113df8bae1dSRodney W. Grimes 			count = 0;
2114df8bae1dSRodney W. Grimes 		} else
2115c7c34a24SBruce Evans 			db_printf(",");
2116df8bae1dSRodney W. Grimes 		count++;
2117df8bae1dSRodney W. Grimes 
2118e47cd172SMaxime Henrion 		db_printf("(off=0x%jx,page=0x%jx)",
2119e47cd172SMaxime Henrion 		    (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p));
2120df8bae1dSRodney W. Grimes 	}
2121df8bae1dSRodney W. Grimes 	if (count != 0)
2122c7c34a24SBruce Evans 		db_printf("\n");
2123c7c34a24SBruce Evans 	db_indent -= 2;
2124df8bae1dSRodney W. Grimes }
21255070c7f8SJohn Dyson 
2126c7c34a24SBruce Evans /* XXX. */
2127c7c34a24SBruce Evans #undef count
2128c7c34a24SBruce Evans 
2129c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */
21305070c7f8SJohn Dyson void
21311b40f8c0SMatthew Dillon vm_object_print(
21321b40f8c0SMatthew Dillon         /* db_expr_t */ long addr,
21331b40f8c0SMatthew Dillon 	boolean_t have_addr,
21341b40f8c0SMatthew Dillon 	/* db_expr_t */ long count,
21351b40f8c0SMatthew Dillon 	char *modif)
2136c7c34a24SBruce Evans {
2137c7c34a24SBruce Evans 	vm_object_print_static(addr, have_addr, count, modif);
2138c7c34a24SBruce Evans }
2139c7c34a24SBruce Evans 
2140c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
21415070c7f8SJohn Dyson {
21425070c7f8SJohn Dyson 	vm_object_t object;
21435070c7f8SJohn Dyson 	int nl = 0;
21445070c7f8SJohn Dyson 	int c;
2145cc64b484SAlfred Perlstein 
2146cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
21475070c7f8SJohn Dyson 		vm_pindex_t idx, fidx;
21485070c7f8SJohn Dyson 		vm_pindex_t osize;
21495070c7f8SJohn Dyson 		vm_offset_t pa = -1, padiff;
21505070c7f8SJohn Dyson 		int rcount;
21515070c7f8SJohn Dyson 		vm_page_t m;
21525070c7f8SJohn Dyson 
2153fc62ef1fSBruce Evans 		db_printf("new object: %p\n", (void *)object);
21545070c7f8SJohn Dyson 		if (nl > 18) {
21555070c7f8SJohn Dyson 			c = cngetc();
21565070c7f8SJohn Dyson 			if (c != ' ')
21575070c7f8SJohn Dyson 				return;
21585070c7f8SJohn Dyson 			nl = 0;
21595070c7f8SJohn Dyson 		}
21605070c7f8SJohn Dyson 		nl++;
21615070c7f8SJohn Dyson 		rcount = 0;
21625070c7f8SJohn Dyson 		fidx = 0;
21635070c7f8SJohn Dyson 		osize = object->size;
21645070c7f8SJohn Dyson 		if (osize > 128)
21655070c7f8SJohn Dyson 			osize = 128;
21665070c7f8SJohn Dyson 		for (idx = 0; idx < osize; idx++) {
21675070c7f8SJohn Dyson 			m = vm_page_lookup(object, idx);
21685070c7f8SJohn Dyson 			if (m == NULL) {
21695070c7f8SJohn Dyson 				if (rcount) {
21703efc015bSPeter Wemm 					db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
21713efc015bSPeter Wemm 						(long)fidx, rcount, (long)pa);
21725070c7f8SJohn Dyson 					if (nl > 18) {
21735070c7f8SJohn Dyson 						c = cngetc();
21745070c7f8SJohn Dyson 						if (c != ' ')
21755070c7f8SJohn Dyson 							return;
21765070c7f8SJohn Dyson 						nl = 0;
21775070c7f8SJohn Dyson 					}
21785070c7f8SJohn Dyson 					nl++;
21795070c7f8SJohn Dyson 					rcount = 0;
21805070c7f8SJohn Dyson 				}
21815070c7f8SJohn Dyson 				continue;
21825070c7f8SJohn Dyson 			}
21835070c7f8SJohn Dyson 
21845070c7f8SJohn Dyson 
21855070c7f8SJohn Dyson 			if (rcount &&
21865070c7f8SJohn Dyson 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
21875070c7f8SJohn Dyson 				++rcount;
21885070c7f8SJohn Dyson 				continue;
21895070c7f8SJohn Dyson 			}
21905070c7f8SJohn Dyson 			if (rcount) {
21915070c7f8SJohn Dyson 				padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
21925070c7f8SJohn Dyson 				padiff >>= PAGE_SHIFT;
21935070c7f8SJohn Dyson 				padiff &= PQ_L2_MASK;
21945070c7f8SJohn Dyson 				if (padiff == 0) {
21955070c7f8SJohn Dyson 					pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
21965070c7f8SJohn Dyson 					++rcount;
21975070c7f8SJohn Dyson 					continue;
21985070c7f8SJohn Dyson 				}
21993efc015bSPeter Wemm 				db_printf(" index(%ld)run(%d)pa(0x%lx)",
22003efc015bSPeter Wemm 					(long)fidx, rcount, (long)pa);
22013efc015bSPeter Wemm 				db_printf("pd(%ld)\n", (long)padiff);
22025070c7f8SJohn Dyson 				if (nl > 18) {
22035070c7f8SJohn Dyson 					c = cngetc();
22045070c7f8SJohn Dyson 					if (c != ' ')
22055070c7f8SJohn Dyson 						return;
22065070c7f8SJohn Dyson 					nl = 0;
22075070c7f8SJohn Dyson 				}
22085070c7f8SJohn Dyson 				nl++;
22095070c7f8SJohn Dyson 			}
22105070c7f8SJohn Dyson 			fidx = idx;
22115070c7f8SJohn Dyson 			pa = VM_PAGE_TO_PHYS(m);
22125070c7f8SJohn Dyson 			rcount = 1;
22135070c7f8SJohn Dyson 		}
22145070c7f8SJohn Dyson 		if (rcount) {
22153efc015bSPeter Wemm 			db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
22163efc015bSPeter Wemm 				(long)fidx, rcount, (long)pa);
22175070c7f8SJohn Dyson 			if (nl > 18) {
22185070c7f8SJohn Dyson 				c = cngetc();
22195070c7f8SJohn Dyson 				if (c != ' ')
22205070c7f8SJohn Dyson 					return;
22215070c7f8SJohn Dyson 				nl = 0;
22225070c7f8SJohn Dyson 			}
22235070c7f8SJohn Dyson 			nl++;
22245070c7f8SJohn Dyson 		}
22255070c7f8SJohn Dyson 	}
22265070c7f8SJohn Dyson }
2227c3cb3e12SDavid Greenman #endif /* DDB */
2228