xref: /freebsd/sys/vm/vm_object.c (revision 2395531439bb140427dff4dfd6d67856f907c15e)
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>
761b367556SJason Evans #include <sys/mutex.h>
77fb919e4dSMark Murray #include <sys/proc.h>		/* for curproc, pageproc */
78fb919e4dSMark Murray #include <sys/socket.h>
79fb919e4dSMark Murray #include <sys/vnode.h>
80fb919e4dSMark Murray #include <sys/vmmeter.h>
811005a129SJohn Baldwin #include <sys/sx.h>
82df8bae1dSRodney W. Grimes 
83df8bae1dSRodney W. Grimes #include <vm/vm.h>
84efeaf95aSDavid Greenman #include <vm/vm_param.h>
85efeaf95aSDavid Greenman #include <vm/pmap.h>
86efeaf95aSDavid Greenman #include <vm/vm_map.h>
87efeaf95aSDavid Greenman #include <vm/vm_object.h>
88df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
8926f9a767SRodney W. Grimes #include <vm/vm_pageout.h>
900d94caffSDavid Greenman #include <vm/vm_pager.h>
9121cd6e62SSeigo Tanimura #include <vm/vm_zone.h>
9205f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h>
93a1f6d91cSDavid Greenman #include <vm/vm_kern.h>
94efeaf95aSDavid Greenman #include <vm/vm_extern.h>
9526f9a767SRodney W. Grimes 
96cac597e4SBruce Evans static void	vm_object_qcollapse __P((vm_object_t object));
97f6b04d2bSDavid Greenman 
98df8bae1dSRodney W. Grimes /*
99df8bae1dSRodney W. Grimes  *	Virtual memory objects maintain the actual data
100df8bae1dSRodney W. Grimes  *	associated with allocated virtual memory.  A given
101df8bae1dSRodney W. Grimes  *	page of memory exists within exactly one object.
102df8bae1dSRodney W. Grimes  *
103df8bae1dSRodney W. Grimes  *	An object is only deallocated when all "references"
104df8bae1dSRodney W. Grimes  *	are given up.  Only one "reference" to a given
105df8bae1dSRodney W. Grimes  *	region of an object should be writeable.
106df8bae1dSRodney W. Grimes  *
107df8bae1dSRodney W. Grimes  *	Associated with each object is a list of all resident
108df8bae1dSRodney W. Grimes  *	memory pages belonging to that object; this list is
109df8bae1dSRodney W. Grimes  *	maintained by the "vm_page" module, and locked by the object's
110df8bae1dSRodney W. Grimes  *	lock.
111df8bae1dSRodney W. Grimes  *
112df8bae1dSRodney W. Grimes  *	Each object also records a "pager" routine which is
113df8bae1dSRodney W. Grimes  *	used to retrieve (and store) pages to the proper backing
114df8bae1dSRodney W. Grimes  *	storage.  In addition, objects may be backed by other
115df8bae1dSRodney W. Grimes  *	objects from which they were virtual-copied.
116df8bae1dSRodney W. Grimes  *
117df8bae1dSRodney W. Grimes  *	The only items within the object structure which are
118df8bae1dSRodney W. Grimes  *	modified after time of creation are:
119df8bae1dSRodney W. Grimes  *		reference count		locked by object's lock
120df8bae1dSRodney W. Grimes  *		pager routine		locked by object's lock
121df8bae1dSRodney W. Grimes  *
122df8bae1dSRodney W. Grimes  */
123df8bae1dSRodney W. Grimes 
12428f8db14SBruce Evans struct object_q vm_object_list;
125cc64b484SAlfred Perlstein static struct mtx vm_object_list_mtx;	/* lock for object list and count */
1264de628deSBruce Evans static long vm_object_count;		/* count of all objects */
12728f8db14SBruce Evans vm_object_t kernel_object;
12828f8db14SBruce Evans vm_object_t kmem_object;
129f708ef1bSPoul-Henning Kamp static struct vm_object kernel_object_store;
130f708ef1bSPoul-Henning Kamp static struct vm_object kmem_object_store;
131aef922f5SJohn Dyson extern int vm_pageout_page_count;
132df8bae1dSRodney W. Grimes 
133f708ef1bSPoul-Henning Kamp static long object_collapses;
134f708ef1bSPoul-Henning Kamp static long object_bypasses;
1355070c7f8SJohn Dyson static int next_index;
13699448ed1SJohn Dyson static vm_zone_t obj_zone;
13799448ed1SJohn Dyson static struct vm_zone obj_zone_store;
1381c7c3c6aSMatthew Dillon static int object_hash_rand;
13999448ed1SJohn Dyson #define VM_OBJECTS_INIT 256
140303b270bSEivind Eklund static struct vm_object vm_objects_init[VM_OBJECTS_INIT];
141df8bae1dSRodney W. Grimes 
1423075778bSJohn Dyson void
14324a1cce3SDavid Greenman _vm_object_allocate(type, size, object)
14424a1cce3SDavid Greenman 	objtype_t type;
145df8bae1dSRodney W. Grimes 	vm_size_t size;
146d031cff1SMatthew Dillon 	vm_object_t object;
147df8bae1dSRodney W. Grimes {
14899448ed1SJohn Dyson 	int incr;
14923955314SAlfred Perlstein 	int hadvmlock;
15023955314SAlfred Perlstein 
15123955314SAlfred Perlstein 	/*
15223955314SAlfred Perlstein 	 * XXX: Not all callers seem to have the lock, compensate.
15323955314SAlfred Perlstein 	 * I'm pretty sure we need to bump the gen count before possibly
15423955314SAlfred Perlstein 	 * nuking the data contained within while under the lock.
15523955314SAlfred Perlstein 	 */
15623955314SAlfred Perlstein 	hadvmlock = mtx_owned(&vm_mtx);
15723955314SAlfred Perlstein 	if (!hadvmlock)
15823955314SAlfred Perlstein 		mtx_lock(&vm_mtx);
15923955314SAlfred Perlstein 	object->generation++;
16023955314SAlfred Perlstein 	if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
16123955314SAlfred Perlstein 		vm_object_set_flag(object, OBJ_ONEMAPPING);
16223955314SAlfred Perlstein 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
16323955314SAlfred Perlstein 	vm_object_count++;
16423955314SAlfred Perlstein 	if (!hadvmlock)
16523955314SAlfred Perlstein 		mtx_unlock(&vm_mtx);
16623955314SAlfred Perlstein 
167df8bae1dSRodney W. Grimes 	TAILQ_INIT(&object->memq);
16824a1cce3SDavid Greenman 	TAILQ_INIT(&object->shadow_head);
169a1f6d91cSDavid Greenman 
17024a1cce3SDavid Greenman 	object->type = type;
171df8bae1dSRodney W. Grimes 	object->size = size;
172a1f6d91cSDavid Greenman 	object->ref_count = 1;
17324a1cce3SDavid Greenman 	object->flags = 0;
174df8bae1dSRodney W. Grimes 	object->paging_in_progress = 0;
175a1f6d91cSDavid Greenman 	object->resident_page_count = 0;
176de5f6a77SJohn Dyson 	object->shadow_count = 0;
1775070c7f8SJohn Dyson 	object->pg_color = next_index;
17899448ed1SJohn Dyson 	if ( size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
17999448ed1SJohn Dyson 		incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
18099448ed1SJohn Dyson 	else
18199448ed1SJohn Dyson 		incr = size;
18299448ed1SJohn Dyson 	next_index = (next_index + incr) & PQ_L2_MASK;
18324a1cce3SDavid Greenman 	object->handle = NULL;
18424a1cce3SDavid Greenman 	object->backing_object = NULL;
185a316d390SJohn Dyson 	object->backing_object_offset = (vm_ooffset_t) 0;
1861c7c3c6aSMatthew Dillon 	/*
1871c7c3c6aSMatthew Dillon 	 * Try to generate a number that will spread objects out in the
1881c7c3c6aSMatthew Dillon 	 * hash table.  We 'wipe' new objects across the hash in 128 page
1891c7c3c6aSMatthew Dillon 	 * increments plus 1 more to offset it a little more by the time
1901c7c3c6aSMatthew Dillon 	 * it wraps around.
1911c7c3c6aSMatthew Dillon 	 */
1921c7c3c6aSMatthew Dillon 	object->hash_rand = object_hash_rand - 129;
193a1f6d91cSDavid Greenman 
1941c7c3c6aSMatthew Dillon 	object_hash_rand = object->hash_rand;
195df8bae1dSRodney W. Grimes }
196df8bae1dSRodney W. Grimes 
197df8bae1dSRodney W. Grimes /*
19826f9a767SRodney W. Grimes  *	vm_object_init:
19926f9a767SRodney W. Grimes  *
20026f9a767SRodney W. Grimes  *	Initialize the VM objects module.
20126f9a767SRodney W. Grimes  */
20226f9a767SRodney W. Grimes void
203a316d390SJohn Dyson vm_object_init()
20426f9a767SRodney W. Grimes {
20526f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_list);
2061b367556SJason Evans 	mtx_init(&vm_object_list_mtx, "vm object_list", MTX_DEF);
20726f9a767SRodney W. Grimes 	vm_object_count = 0;
2080217125fSDavid Greenman 
20926f9a767SRodney W. Grimes 	kernel_object = &kernel_object_store;
210a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
21126f9a767SRodney W. Grimes 	    kernel_object);
21226f9a767SRodney W. Grimes 
21326f9a767SRodney W. Grimes 	kmem_object = &kmem_object_store;
214a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
21526f9a767SRodney W. Grimes 	    kmem_object);
21699448ed1SJohn Dyson 
21799448ed1SJohn Dyson 	obj_zone = &obj_zone_store;
21899448ed1SJohn Dyson 	zbootinit(obj_zone, "VM OBJECT", sizeof (struct vm_object),
21999448ed1SJohn Dyson 		vm_objects_init, VM_OBJECTS_INIT);
22099448ed1SJohn Dyson }
22199448ed1SJohn Dyson 
22299448ed1SJohn Dyson void
22399448ed1SJohn Dyson vm_object_init2() {
2240a80f406SJohn Dyson 	zinitna(obj_zone, NULL, NULL, 0, 0, 0, 1);
22526f9a767SRodney W. Grimes }
22626f9a767SRodney W. Grimes 
22726f9a767SRodney W. Grimes /*
22826f9a767SRodney W. Grimes  *	vm_object_allocate:
22926f9a767SRodney W. Grimes  *
23026f9a767SRodney W. Grimes  *	Returns a new object with the given size.
23126f9a767SRodney W. Grimes  */
23226f9a767SRodney W. Grimes 
23326f9a767SRodney W. Grimes vm_object_t
23424a1cce3SDavid Greenman vm_object_allocate(type, size)
23524a1cce3SDavid Greenman 	objtype_t type;
23626f9a767SRodney W. Grimes 	vm_size_t size;
23726f9a767SRodney W. Grimes {
238d031cff1SMatthew Dillon 	vm_object_t result;
239d031cff1SMatthew Dillon 
24099448ed1SJohn Dyson 	result = (vm_object_t) zalloc(obj_zone);
24124a1cce3SDavid Greenman 	_vm_object_allocate(type, size, result);
24226f9a767SRodney W. Grimes 
24326f9a767SRodney W. Grimes 	return (result);
24426f9a767SRodney W. Grimes }
24526f9a767SRodney W. Grimes 
24626f9a767SRodney W. Grimes 
24726f9a767SRodney W. Grimes /*
248df8bae1dSRodney W. Grimes  *	vm_object_reference:
249df8bae1dSRodney W. Grimes  *
250df8bae1dSRodney W. Grimes  *	Gets another reference to the given object.
251df8bae1dSRodney W. Grimes  */
2526476c0d2SJohn Dyson void
25326f9a767SRodney W. Grimes vm_object_reference(object)
254d031cff1SMatthew Dillon 	vm_object_t object;
255df8bae1dSRodney W. Grimes {
256df8bae1dSRodney W. Grimes 	if (object == NULL)
257df8bae1dSRodney W. Grimes 		return;
25895e5e988SJohn Dyson 
2595526d2d9SEivind Eklund 	KASSERT(!(object->flags & OBJ_DEAD),
2605526d2d9SEivind Eklund 	    ("vm_object_reference: attempting to reference dead obj"));
26195e5e988SJohn Dyson 
262df8bae1dSRodney W. Grimes 	object->ref_count++;
26347221757SJohn Dyson 	if (object->type == OBJT_VNODE) {
26423955314SAlfred Perlstein 		mtx_unlock(&vm_mtx);
26523955314SAlfred Perlstein 		mtx_lock(&Giant);
26647221757SJohn Dyson 		while (vget((struct vnode *) object->handle, LK_RETRY|LK_NOOBJ, curproc)) {
26747221757SJohn Dyson 			printf("vm_object_reference: delay in getting object\n");
26847221757SJohn Dyson 		}
26923955314SAlfred Perlstein 		mtx_unlock(&Giant);
27023955314SAlfred Perlstein 		mtx_lock(&vm_mtx);
27147221757SJohn Dyson 	}
27295e5e988SJohn Dyson }
27395e5e988SJohn Dyson 
27423955314SAlfred Perlstein /*
27523955314SAlfred Perlstein  * handle deallocating a object of type OBJT_VNODE
27623955314SAlfred Perlstein  *
27723955314SAlfred Perlstein  * requires vm_mtx
27823955314SAlfred Perlstein  * may block
27923955314SAlfred Perlstein  */
280bf27292bSJohn Dyson void
28195e5e988SJohn Dyson vm_object_vndeallocate(object)
28295e5e988SJohn Dyson 	vm_object_t object;
28395e5e988SJohn Dyson {
28495e5e988SJohn Dyson 	struct vnode *vp = (struct vnode *) object->handle;
285219cbf59SEivind Eklund 
28623955314SAlfred Perlstein 	mtx_assert(&vm_mtx, MA_OWNED);
2875526d2d9SEivind Eklund 	KASSERT(object->type == OBJT_VNODE,
2885526d2d9SEivind Eklund 	    ("vm_object_vndeallocate: not a vnode object"));
289219cbf59SEivind Eklund 	KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
290219cbf59SEivind Eklund #ifdef INVARIANTS
29195e5e988SJohn Dyson 	if (object->ref_count == 0) {
29295e5e988SJohn Dyson 		vprint("vm_object_vndeallocate", vp);
29395e5e988SJohn Dyson 		panic("vm_object_vndeallocate: bad object reference count");
29495e5e988SJohn Dyson 	}
29595e5e988SJohn Dyson #endif
29695e5e988SJohn Dyson 
29795e5e988SJohn Dyson 	object->ref_count--;
29847221757SJohn Dyson 	if (object->ref_count == 0) {
299bf27292bSJohn Dyson 		vp->v_flag &= ~VTEXT;
300069e9bc1SDoug Rabson 		vm_object_clear_flag(object, OBJ_OPT);
3012be70f79SJohn Dyson 	}
30223955314SAlfred Perlstein 	/*
30323955314SAlfred Perlstein 	 * vrele may need a vop lock
30423955314SAlfred Perlstein 	 */
30523955314SAlfred Perlstein 	mtx_unlock(VM_OBJECT_MTX(object));
30623955314SAlfred Perlstein 	mtx_lock(&Giant);
30747221757SJohn Dyson 	vrele(vp);
30823955314SAlfred Perlstein 	mtx_unlock(&Giant);
30923955314SAlfred Perlstein 	mtx_lock(VM_OBJECT_MTX(object));
310df8bae1dSRodney W. Grimes }
311df8bae1dSRodney W. Grimes 
312df8bae1dSRodney W. Grimes /*
313df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
314df8bae1dSRodney W. Grimes  *
315df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
316df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
317df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
318df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
319df8bae1dSRodney W. Grimes  *	may be relinquished.
320df8bae1dSRodney W. Grimes  *
321df8bae1dSRodney W. Grimes  *	No object may be locked.
32223955314SAlfred Perlstein  *	vm_mtx must be held
323df8bae1dSRodney W. Grimes  */
32426f9a767SRodney W. Grimes void
32526f9a767SRodney W. Grimes vm_object_deallocate(object)
32626f9a767SRodney W. Grimes 	vm_object_t object;
327df8bae1dSRodney W. Grimes {
328df8bae1dSRodney W. Grimes 	vm_object_t temp;
329df8bae1dSRodney W. Grimes 
33023955314SAlfred Perlstein 	mtx_assert(&vm_mtx, MA_OWNED);
331df8bae1dSRodney W. Grimes 	while (object != NULL) {
332df8bae1dSRodney W. Grimes 
33395e5e988SJohn Dyson 		if (object->type == OBJT_VNODE) {
33495e5e988SJohn Dyson 			vm_object_vndeallocate(object);
33595e5e988SJohn Dyson 			return;
33695e5e988SJohn Dyson 		}
33795e5e988SJohn Dyson 
3388125b1e6SAlfred Perlstein 		KASSERT(object->ref_count != 0,
3398125b1e6SAlfred Perlstein 			("vm_object_deallocate: object deallocated too many times: %d", object->type));
3402be70f79SJohn Dyson 
3412be70f79SJohn Dyson 		/*
3428125b1e6SAlfred Perlstein 		 * If the reference count goes to 0 we start calling
3438125b1e6SAlfred Perlstein 		 * vm_object_terminate() on the object chain.
3448125b1e6SAlfred Perlstein 		 * A ref count of 1 may be a special case depending on the
3458125b1e6SAlfred Perlstein 		 * shadow count being 0 or 1.
3462be70f79SJohn Dyson 		 */
347c0877f10SJohn Dyson 		object->ref_count--;
3488125b1e6SAlfred Perlstein 		if (object->ref_count > 1) {
349c0877f10SJohn Dyson 			return;
3508125b1e6SAlfred Perlstein 		} else if (object->ref_count == 1) {
3518125b1e6SAlfred Perlstein 			if (object->shadow_count == 0) {
3528125b1e6SAlfred Perlstein 				vm_object_set_flag(object, OBJ_ONEMAPPING);
3538125b1e6SAlfred Perlstein 			} else if ((object->shadow_count == 1) &&
3548125b1e6SAlfred Perlstein 			    (object->handle == NULL) &&
35524a1cce3SDavid Greenman 			    (object->type == OBJT_DEFAULT ||
35624a1cce3SDavid Greenman 			     object->type == OBJT_SWAP)) {
357a1f6d91cSDavid Greenman 				vm_object_t robject;
35895e5e988SJohn Dyson 
359b18bfc3dSJohn Dyson 				robject = TAILQ_FIRST(&object->shadow_head);
3605526d2d9SEivind Eklund 				KASSERT(robject != NULL,
361219cbf59SEivind Eklund 				    ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
3625526d2d9SEivind Eklund 					 object->ref_count,
3635526d2d9SEivind Eklund 					 object->shadow_count));
36495e5e988SJohn Dyson 				if ((robject->handle == NULL) &&
36524a1cce3SDavid Greenman 				    (robject->type == OBJT_DEFAULT ||
36624a1cce3SDavid Greenman 				     robject->type == OBJT_SWAP)) {
367a1f6d91cSDavid Greenman 
36895e5e988SJohn Dyson 					robject->ref_count++;
36995e5e988SJohn Dyson 
3701c7c3c6aSMatthew Dillon 					while (
3711c7c3c6aSMatthew Dillon 						robject->paging_in_progress ||
3721c7c3c6aSMatthew Dillon 						object->paging_in_progress
3731c7c3c6aSMatthew Dillon 					) {
37466095752SJohn Dyson 						vm_object_pip_sleep(robject, "objde1");
37566095752SJohn Dyson 						vm_object_pip_sleep(object, "objde2");
376a1f6d91cSDavid Greenman 					}
377a1f6d91cSDavid Greenman 
37895e5e988SJohn Dyson 					if (robject->ref_count == 1) {
37995e5e988SJohn Dyson 						robject->ref_count--;
380ba8da839SDavid Greenman 						object = robject;
38195e5e988SJohn Dyson 						goto doterm;
38295e5e988SJohn Dyson 					}
38395e5e988SJohn Dyson 
38495e5e988SJohn Dyson 					object = robject;
38595e5e988SJohn Dyson 					vm_object_collapse(object);
386ba8da839SDavid Greenman 					continue;
387a1f6d91cSDavid Greenman 				}
38895e5e988SJohn Dyson 			}
38995e5e988SJohn Dyson 
390a1f6d91cSDavid Greenman 			return;
391df8bae1dSRodney W. Grimes 
39295e5e988SJohn Dyson 		}
39395e5e988SJohn Dyson 
39495e5e988SJohn Dyson doterm:
3951efb74fbSJohn Dyson 
39624a1cce3SDavid Greenman 		temp = object->backing_object;
397de5f6a77SJohn Dyson 		if (temp) {
39824a1cce3SDavid Greenman 			TAILQ_REMOVE(&temp->shadow_head, object, shadow_list);
39995e5e988SJohn Dyson 			temp->shadow_count--;
4002d8acc0fSJohn Dyson 			if (temp->ref_count == 0)
401069e9bc1SDoug Rabson 				vm_object_clear_flag(temp, OBJ_OPT);
402eaf13dd7SJohn Dyson 			temp->generation++;
40395461b45SJohn Dyson 			object->backing_object = NULL;
404de5f6a77SJohn Dyson 		}
405df8bae1dSRodney W. Grimes 		vm_object_terminate(object);
406df8bae1dSRodney W. Grimes 		/* unlocks and deallocates object */
407df8bae1dSRodney W. Grimes 		object = temp;
408df8bae1dSRodney W. Grimes 	}
409df8bae1dSRodney W. Grimes }
410df8bae1dSRodney W. Grimes 
411df8bae1dSRodney W. Grimes /*
412df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
413df8bae1dSRodney W. Grimes  *	up all previously used resources.
414df8bae1dSRodney W. Grimes  *
415df8bae1dSRodney W. Grimes  *	The object must be locked.
4161c7c3c6aSMatthew Dillon  *	This routine may block.
417df8bae1dSRodney W. Grimes  */
41895e5e988SJohn Dyson void
41926f9a767SRodney W. Grimes vm_object_terminate(object)
420d031cff1SMatthew Dillon 	vm_object_t object;
421df8bae1dSRodney W. Grimes {
422d031cff1SMatthew Dillon 	vm_page_t p;
423e4b7635dSDavid Greenman 	int s;
424df8bae1dSRodney W. Grimes 
42595e5e988SJohn Dyson 	/*
42695e5e988SJohn Dyson 	 * Make sure no one uses us.
42795e5e988SJohn Dyson 	 */
428069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_DEAD);
4293c631446SJohn Dyson 
430df8bae1dSRodney W. Grimes 	/*
431f6b04d2bSDavid Greenman 	 * wait for the pageout daemon to be done with the object
432df8bae1dSRodney W. Grimes 	 */
43366095752SJohn Dyson 	vm_object_pip_wait(object, "objtrm");
434df8bae1dSRodney W. Grimes 
4355526d2d9SEivind Eklund 	KASSERT(!object->paging_in_progress,
4365526d2d9SEivind Eklund 		("vm_object_terminate: pageout in progress"));
43726f9a767SRodney W. Grimes 
43826f9a767SRodney W. Grimes 	/*
4390d94caffSDavid Greenman 	 * Clean and free the pages, as appropriate. All references to the
4400d94caffSDavid Greenman 	 * object are gone, so we don't need to lock it.
44126f9a767SRodney W. Grimes 	 */
44224a1cce3SDavid Greenman 	if (object->type == OBJT_VNODE) {
44395e5e988SJohn Dyson 		struct vnode *vp;
44495e5e988SJohn Dyson 
44595e5e988SJohn Dyson 		/*
44695e5e988SJohn Dyson 		 * Freeze optimized copies.
44795e5e988SJohn Dyson 		 */
44895e5e988SJohn Dyson 		vm_freeze_copyopts(object, 0, object->size);
44995e5e988SJohn Dyson 
45095e5e988SJohn Dyson 		/*
45195e5e988SJohn Dyson 		 * Clean pages and flush buffers.
45295e5e988SJohn Dyson 		 */
4538f9110f6SJohn Dyson 		vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
45495e5e988SJohn Dyson 
45595e5e988SJohn Dyson 		vp = (struct vnode *) object->handle;
45623955314SAlfred Perlstein 		mtx_unlock(VM_OBJECT_MTX(object));
45723955314SAlfred Perlstein 		mtx_lock(&Giant);
458f6b04d2bSDavid Greenman 		vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
45923955314SAlfred Perlstein 		mtx_unlock(&Giant);
46023955314SAlfred Perlstein 		mtx_lock(VM_OBJECT_MTX(object));
461bef608bdSJohn Dyson 	}
462bef608bdSJohn Dyson 
463971dd342SAlfred Perlstein 	KASSERT(object->ref_count == 0,
464971dd342SAlfred Perlstein 		("vm_object_terminate: object with references, ref_count=%d",
465971dd342SAlfred Perlstein 		object->ref_count));
466996c772fSJohn Dyson 
4670d94caffSDavid Greenman 	/*
468356863ebSDavid Greenman 	 * Now free any remaining pages. For internal objects, this also
469356863ebSDavid Greenman 	 * removes them from paging queues. Don't free wired pages, just
470356863ebSDavid Greenman 	 * remove them from the object.
471df8bae1dSRodney W. Grimes 	 */
472e4b7635dSDavid Greenman 	s = splvm();
473b18bfc3dSJohn Dyson 	while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
474971dd342SAlfred Perlstein 		KASSERT(!p->busy && (p->flags & PG_BUSY) == 0,
475971dd342SAlfred Perlstein 			("vm_object_terminate: freeing busy page %p "
476971dd342SAlfred Perlstein 			"p->busy = %d, p->flags %x\n", p, p->busy, p->flags));
4770b10ba98SDavid Greenman 		if (p->wire_count == 0) {
478e69763a3SDoug Rabson 			vm_page_busy(p);
479df8bae1dSRodney W. Grimes 			vm_page_free(p);
480df8bae1dSRodney W. Grimes 			cnt.v_pfree++;
4810b10ba98SDavid Greenman 		} else {
482356863ebSDavid Greenman 			vm_page_busy(p);
4830b10ba98SDavid Greenman 			vm_page_remove(p);
4840b10ba98SDavid Greenman 		}
485df8bae1dSRodney W. Grimes 	}
486e4b7635dSDavid Greenman 	splx(s);
487bef608bdSJohn Dyson 
4882d8acc0fSJohn Dyson 	/*
4899fcfb650SDavid Greenman 	 * Let the pager know object is dead.
4909fcfb650SDavid Greenman 	 */
4919fcfb650SDavid Greenman 	vm_pager_deallocate(object);
4929fcfb650SDavid Greenman 
4939fcfb650SDavid Greenman 	/*
4940b10ba98SDavid Greenman 	 * Remove the object from the global object list.
4952d8acc0fSJohn Dyson 	 */
4969ed346baSBosko Milekic 	mtx_lock(&vm_object_list_mtx);
497df8bae1dSRodney W. Grimes 	TAILQ_REMOVE(&vm_object_list, object, object_list);
4989ed346baSBosko Milekic 	mtx_unlock(&vm_object_list_mtx);
4990b10ba98SDavid Greenman 
5000b10ba98SDavid Greenman 	wakeup(object);
5010b10ba98SDavid Greenman 
502df8bae1dSRodney W. Grimes 	/*
503df8bae1dSRodney W. Grimes 	 * Free the space for the object.
504df8bae1dSRodney W. Grimes 	 */
50599448ed1SJohn Dyson 	zfree(obj_zone, object);
50647221757SJohn Dyson }
507df8bae1dSRodney W. Grimes 
508df8bae1dSRodney W. Grimes /*
509df8bae1dSRodney W. Grimes  *	vm_object_page_clean
510df8bae1dSRodney W. Grimes  *
5114f79d873SMatthew Dillon  *	Clean all dirty pages in the specified range of object.  Leaves page
5124f79d873SMatthew Dillon  * 	on whatever queue it is currently on.   If NOSYNC is set then do not
5134f79d873SMatthew Dillon  *	write out pages with PG_NOSYNC set (originally comes from MAP_NOSYNC),
5144f79d873SMatthew Dillon  *	leaving the object dirty.
51526f9a767SRodney W. Grimes  *
51626f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
51726f9a767SRodney W. Grimes  *
51826f9a767SRodney W. Grimes  *	The object must be locked.
51926f9a767SRodney W. Grimes  */
520f6b04d2bSDavid Greenman 
521f6b04d2bSDavid Greenman void
5228f9110f6SJohn Dyson vm_object_page_clean(object, start, end, flags)
523f6b04d2bSDavid Greenman 	vm_object_t object;
524a316d390SJohn Dyson 	vm_pindex_t start;
525a316d390SJohn Dyson 	vm_pindex_t end;
5268f9110f6SJohn Dyson 	int flags;
527f6b04d2bSDavid Greenman {
528d031cff1SMatthew Dillon 	vm_page_t p, np, tp;
529d031cff1SMatthew Dillon 	vm_offset_t tstart, tend;
530bd7e5f99SJohn Dyson 	vm_pindex_t pi;
531aef922f5SJohn Dyson 	int s;
53224a1cce3SDavid Greenman 	struct vnode *vp;
533aef922f5SJohn Dyson 	int runlen;
534bd7e5f99SJohn Dyson 	int maxf;
535bd7e5f99SJohn Dyson 	int chkb;
536bd7e5f99SJohn Dyson 	int maxb;
537bd7e5f99SJohn Dyson 	int i;
5384f79d873SMatthew Dillon 	int clearobjflags;
5398f9110f6SJohn Dyson 	int pagerflags;
540bd7e5f99SJohn Dyson 	vm_page_t maf[vm_pageout_page_count];
541bd7e5f99SJohn Dyson 	vm_page_t mab[vm_pageout_page_count];
542aef922f5SJohn Dyson 	vm_page_t ma[vm_pageout_page_count];
5432d8acc0fSJohn Dyson 	int curgeneration;
544f6b04d2bSDavid Greenman 
54523955314SAlfred Perlstein 	mtx_assert(VM_OBJECT_MTX(object), MA_OWNED);
546aef922f5SJohn Dyson 	if (object->type != OBJT_VNODE ||
547aef922f5SJohn Dyson 		(object->flags & OBJ_MIGHTBEDIRTY) == 0)
548f6b04d2bSDavid Greenman 		return;
549f6b04d2bSDavid Greenman 
5508f9110f6SJohn Dyson 	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : 0;
5518f9110f6SJohn Dyson 	pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
5528f9110f6SJohn Dyson 
55324a1cce3SDavid Greenman 	vp = object->handle;
55424a1cce3SDavid Greenman 
555069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_CLEANING);
55624a1cce3SDavid Greenman 
557f6b04d2bSDavid Greenman 	tstart = start;
558f6b04d2bSDavid Greenman 	if (end == 0) {
559f6b04d2bSDavid Greenman 		tend = object->size;
560f6b04d2bSDavid Greenman 	} else {
561f6b04d2bSDavid Greenman 		tend = end;
562f6b04d2bSDavid Greenman 	}
563eaf13dd7SJohn Dyson 
5644f79d873SMatthew Dillon 	/*
5654f79d873SMatthew Dillon 	 * Generally set CLEANCHK interlock and make the page read-only so
5664f79d873SMatthew Dillon 	 * we can then clear the object flags.
5674f79d873SMatthew Dillon 	 *
5684f79d873SMatthew Dillon 	 * However, if this is a nosync mmap then the object is likely to
5694f79d873SMatthew Dillon 	 * stay dirty so do not mess with the page and do not clear the
5704f79d873SMatthew Dillon 	 * object flags.
5714f79d873SMatthew Dillon 	 */
5724f79d873SMatthew Dillon 
5734f79d873SMatthew Dillon 	clearobjflags = 1;
5744f79d873SMatthew Dillon 
575fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(p, &object->memq, listq) {
576e69763a3SDoug Rabson 		vm_page_flag_set(p, PG_CLEANCHK);
5774f79d873SMatthew Dillon 		if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC))
5784f79d873SMatthew Dillon 			clearobjflags = 0;
5794f79d873SMatthew Dillon 		else
580eaf13dd7SJohn Dyson 			vm_page_protect(p, VM_PROT_READ);
581eaf13dd7SJohn Dyson 	}
582eaf13dd7SJohn Dyson 
5834f79d873SMatthew Dillon 	if (clearobjflags && (tstart == 0) && (tend == object->size)) {
584069e9bc1SDoug Rabson 		vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
585ec4f9fb0SDavid Greenman 	}
586f6b04d2bSDavid Greenman 
587bd7e5f99SJohn Dyson rescan:
5882d8acc0fSJohn Dyson 	curgeneration = object->generation;
5892d8acc0fSJohn Dyson 
590b18bfc3dSJohn Dyson 	for(p = TAILQ_FIRST(&object->memq); p; p = np) {
591b18bfc3dSJohn Dyson 		np = TAILQ_NEXT(p, listq);
592bd7e5f99SJohn Dyson 
593bd7e5f99SJohn Dyson 		pi = p->pindex;
594bd7e5f99SJohn Dyson 		if (((p->flags & PG_CLEANCHK) == 0) ||
595bd7e5f99SJohn Dyson 			(pi < tstart) || (pi >= tend) ||
5965070c7f8SJohn Dyson 			(p->valid == 0) ||
5975070c7f8SJohn Dyson 			((p->queue - p->pc) == PQ_CACHE)) {
598e69763a3SDoug Rabson 			vm_page_flag_clear(p, PG_CLEANCHK);
599aef922f5SJohn Dyson 			continue;
600f6b04d2bSDavid Greenman 		}
601f6b04d2bSDavid Greenman 
602bd7e5f99SJohn Dyson 		vm_page_test_dirty(p);
603bd7e5f99SJohn Dyson 		if ((p->dirty & p->valid) == 0) {
604e69763a3SDoug Rabson 			vm_page_flag_clear(p, PG_CLEANCHK);
605bd7e5f99SJohn Dyson 			continue;
606bd7e5f99SJohn Dyson 		}
607ec4f9fb0SDavid Greenman 
6084f79d873SMatthew Dillon 		/*
6094f79d873SMatthew Dillon 		 * If we have been asked to skip nosync pages and this is a
6104f79d873SMatthew Dillon 		 * nosync page, skip it.  Note that the object flags were
6114f79d873SMatthew Dillon 		 * not cleared in this case so we do not have to set them.
6124f79d873SMatthew Dillon 		 */
6134f79d873SMatthew Dillon 		if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
6144f79d873SMatthew Dillon 			vm_page_flag_clear(p, PG_CLEANCHK);
6154f79d873SMatthew Dillon 			continue;
6164f79d873SMatthew Dillon 		}
6174f79d873SMatthew Dillon 
618b18bfc3dSJohn Dyson 		s = splvm();
6191c7c3c6aSMatthew Dillon 		while (vm_page_sleep_busy(p, TRUE, "vpcwai")) {
6202d8acc0fSJohn Dyson 			if (object->generation != curgeneration) {
621f6b04d2bSDavid Greenman 				splx(s);
622bd7e5f99SJohn Dyson 				goto rescan;
623f6b04d2bSDavid Greenman 			}
6242d8acc0fSJohn Dyson 		}
625f6b04d2bSDavid Greenman 
626bd7e5f99SJohn Dyson 		maxf = 0;
627bd7e5f99SJohn Dyson 		for(i=1;i<vm_pageout_page_count;i++) {
6288aef1712SMatthew Dillon 			if ((tp = vm_page_lookup(object, pi + i)) != NULL) {
629bd7e5f99SJohn Dyson 				if ((tp->flags & PG_BUSY) ||
630ffc82b0aSJohn Dyson 					(tp->flags & PG_CLEANCHK) == 0 ||
631ffc82b0aSJohn Dyson 					(tp->busy != 0))
632bd7e5f99SJohn Dyson 					break;
6335070c7f8SJohn Dyson 				if((tp->queue - tp->pc) == PQ_CACHE) {
634e69763a3SDoug Rabson 					vm_page_flag_clear(tp, PG_CLEANCHK);
6353077a9c2SJohn Dyson 					break;
6363077a9c2SJohn Dyson 				}
637bd7e5f99SJohn Dyson 				vm_page_test_dirty(tp);
638bd7e5f99SJohn Dyson 				if ((tp->dirty & tp->valid) == 0) {
639e69763a3SDoug Rabson 					vm_page_flag_clear(tp, PG_CLEANCHK);
640bd7e5f99SJohn Dyson 					break;
641bd7e5f99SJohn Dyson 				}
642bd7e5f99SJohn Dyson 				maf[ i - 1 ] = tp;
643bd7e5f99SJohn Dyson 				maxf++;
644bd7e5f99SJohn Dyson 				continue;
645bd7e5f99SJohn Dyson 			}
646bd7e5f99SJohn Dyson 			break;
647bd7e5f99SJohn Dyson 		}
648aef922f5SJohn Dyson 
649bd7e5f99SJohn Dyson 		maxb = 0;
650bd7e5f99SJohn Dyson 		chkb = vm_pageout_page_count -  maxf;
651bd7e5f99SJohn Dyson 		if (chkb) {
652bd7e5f99SJohn Dyson 			for(i = 1; i < chkb;i++) {
6538aef1712SMatthew Dillon 				if ((tp = vm_page_lookup(object, pi - i)) != NULL) {
654bd7e5f99SJohn Dyson 					if ((tp->flags & PG_BUSY) ||
655ffc82b0aSJohn Dyson 						(tp->flags & PG_CLEANCHK) == 0 ||
656ffc82b0aSJohn Dyson 						(tp->busy != 0))
657bd7e5f99SJohn Dyson 						break;
6585070c7f8SJohn Dyson 					if((tp->queue - tp->pc) == PQ_CACHE) {
659e69763a3SDoug Rabson 						vm_page_flag_clear(tp, PG_CLEANCHK);
6603077a9c2SJohn Dyson 						break;
6613077a9c2SJohn Dyson 					}
662bd7e5f99SJohn Dyson 					vm_page_test_dirty(tp);
663bd7e5f99SJohn Dyson 					if ((tp->dirty & tp->valid) == 0) {
664e69763a3SDoug Rabson 						vm_page_flag_clear(tp, PG_CLEANCHK);
665bd7e5f99SJohn Dyson 						break;
666bd7e5f99SJohn Dyson 					}
667bd7e5f99SJohn Dyson 					mab[ i - 1 ] = tp;
668bd7e5f99SJohn Dyson 					maxb++;
669bd7e5f99SJohn Dyson 					continue;
670bd7e5f99SJohn Dyson 				}
671bd7e5f99SJohn Dyson 				break;
672bd7e5f99SJohn Dyson 			}
673bd7e5f99SJohn Dyson 		}
674bd7e5f99SJohn Dyson 
675bd7e5f99SJohn Dyson 		for(i=0;i<maxb;i++) {
676bd7e5f99SJohn Dyson 			int index = (maxb - i) - 1;
677bd7e5f99SJohn Dyson 			ma[index] = mab[i];
678e69763a3SDoug Rabson 			vm_page_flag_clear(ma[index], PG_CLEANCHK);
679bd7e5f99SJohn Dyson 		}
680e69763a3SDoug Rabson 		vm_page_flag_clear(p, PG_CLEANCHK);
681bd7e5f99SJohn Dyson 		ma[maxb] = p;
682bd7e5f99SJohn Dyson 		for(i=0;i<maxf;i++) {
683bd7e5f99SJohn Dyson 			int index = (maxb + i) + 1;
684bd7e5f99SJohn Dyson 			ma[index] = maf[i];
685e69763a3SDoug Rabson 			vm_page_flag_clear(ma[index], PG_CLEANCHK);
686f6b04d2bSDavid Greenman 		}
687bd7e5f99SJohn Dyson 		runlen = maxb + maxf + 1;
688cf2819ccSJohn Dyson 
689f35329acSJohn Dyson 		splx(s);
6908f9110f6SJohn Dyson 		vm_pageout_flush(ma, runlen, pagerflags);
691cf2819ccSJohn Dyson 		for (i = 0; i<runlen; i++) {
692cf2819ccSJohn Dyson 			if (ma[i]->valid & ma[i]->dirty) {
693cf2819ccSJohn Dyson 				vm_page_protect(ma[i], VM_PROT_READ);
694e69763a3SDoug Rabson 				vm_page_flag_set(ma[i], PG_CLEANCHK);
695cf2819ccSJohn Dyson 			}
696cf2819ccSJohn Dyson 		}
6972d8acc0fSJohn Dyson 		if (object->generation != curgeneration)
698bd7e5f99SJohn Dyson 			goto rescan;
699f6b04d2bSDavid Greenman 	}
700aef922f5SJohn Dyson 
701479112dfSMatthew Dillon #if 0
702cf2819ccSJohn Dyson 	VOP_FSYNC(vp, NULL, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
703479112dfSMatthew Dillon #endif
704aef922f5SJohn Dyson 
705069e9bc1SDoug Rabson 	vm_object_clear_flag(object, OBJ_CLEANING);
706f5cf85d4SDavid Greenman 	return;
70726f9a767SRodney W. Grimes }
708df8bae1dSRodney W. Grimes 
709f708ef1bSPoul-Henning Kamp #ifdef not_used
710f708ef1bSPoul-Henning Kamp /* XXX I cannot tell if this should be an exported symbol */
711df8bae1dSRodney W. Grimes /*
712df8bae1dSRodney W. Grimes  *	vm_object_deactivate_pages
713df8bae1dSRodney W. Grimes  *
714df8bae1dSRodney W. Grimes  *	Deactivate all pages in the specified object.  (Keep its pages
715df8bae1dSRodney W. Grimes  *	in memory even though it is no longer referenced.)
716df8bae1dSRodney W. Grimes  *
717df8bae1dSRodney W. Grimes  *	The object must be locked.
718df8bae1dSRodney W. Grimes  */
719f708ef1bSPoul-Henning Kamp static void
720df8bae1dSRodney W. Grimes vm_object_deactivate_pages(object)
721d031cff1SMatthew Dillon 	vm_object_t object;
722df8bae1dSRodney W. Grimes {
723d031cff1SMatthew Dillon 	vm_page_t p, next;
724df8bae1dSRodney W. Grimes 
725b18bfc3dSJohn Dyson 	for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
726b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
727df8bae1dSRodney W. Grimes 		vm_page_deactivate(p);
728df8bae1dSRodney W. Grimes 	}
729df8bae1dSRodney W. Grimes }
730f708ef1bSPoul-Henning Kamp #endif
731df8bae1dSRodney W. Grimes 
732df8bae1dSRodney W. Grimes /*
7338e3ad7c9SMatthew Dillon  * Same as vm_object_pmap_copy, except range checking really
7341efb74fbSJohn Dyson  * works, and is meant for small sections of an object.
7358e3ad7c9SMatthew Dillon  *
7368e3ad7c9SMatthew Dillon  * This code protects resident pages by making them read-only
7378e3ad7c9SMatthew Dillon  * and is typically called on a fork or split when a page
7388e3ad7c9SMatthew Dillon  * is converted to copy-on-write.
7398e3ad7c9SMatthew Dillon  *
7408e3ad7c9SMatthew Dillon  * NOTE: If the page is already at VM_PROT_NONE, calling
7418e3ad7c9SMatthew Dillon  * vm_page_protect will have no effect.
7421efb74fbSJohn Dyson  */
7438e3ad7c9SMatthew Dillon 
7441efb74fbSJohn Dyson void
7451efb74fbSJohn Dyson vm_object_pmap_copy_1(object, start, end)
746d031cff1SMatthew Dillon 	vm_object_t object;
747d031cff1SMatthew Dillon 	vm_pindex_t start;
748d031cff1SMatthew Dillon 	vm_pindex_t end;
7491efb74fbSJohn Dyson {
7501efb74fbSJohn Dyson 	vm_pindex_t idx;
751d031cff1SMatthew Dillon 	vm_page_t p;
7521efb74fbSJohn Dyson 
7531efb74fbSJohn Dyson 	if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
7541efb74fbSJohn Dyson 		return;
7551efb74fbSJohn Dyson 
7561efb74fbSJohn Dyson 	for (idx = start; idx < end; idx++) {
7571efb74fbSJohn Dyson 		p = vm_page_lookup(object, idx);
7581efb74fbSJohn Dyson 		if (p == NULL)
7591efb74fbSJohn Dyson 			continue;
7601efb74fbSJohn Dyson 		vm_page_protect(p, VM_PROT_READ);
7611efb74fbSJohn Dyson 	}
7621efb74fbSJohn Dyson }
7631efb74fbSJohn Dyson 
7641efb74fbSJohn Dyson /*
765df8bae1dSRodney W. Grimes  *	vm_object_pmap_remove:
766df8bae1dSRodney W. Grimes  *
767df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
768df8bae1dSRodney W. Grimes  *	object range from all physical maps.
769df8bae1dSRodney W. Grimes  *
770df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
771df8bae1dSRodney W. Grimes  */
77226f9a767SRodney W. Grimes void
77326f9a767SRodney W. Grimes vm_object_pmap_remove(object, start, end)
774d031cff1SMatthew Dillon 	vm_object_t object;
775d031cff1SMatthew Dillon 	vm_pindex_t start;
776d031cff1SMatthew Dillon 	vm_pindex_t end;
777df8bae1dSRodney W. Grimes {
778d031cff1SMatthew Dillon 	vm_page_t p;
779d031cff1SMatthew Dillon 
780df8bae1dSRodney W. Grimes 	if (object == NULL)
781df8bae1dSRodney W. Grimes 		return;
782cc64b484SAlfred Perlstein 	TAILQ_FOREACH(p, &object->memq, listq) {
783bd7e5f99SJohn Dyson 		if (p->pindex >= start && p->pindex < end)
784f919ebdeSDavid Greenman 			vm_page_protect(p, VM_PROT_NONE);
78526f9a767SRodney W. Grimes 	}
7866e20a165SJohn Dyson 	if ((start == 0) && (object->size == end))
787069e9bc1SDoug Rabson 		vm_object_clear_flag(object, OBJ_WRITEABLE);
78826f9a767SRodney W. Grimes }
789df8bae1dSRodney W. Grimes 
790df8bae1dSRodney W. Grimes /*
791867a482dSJohn Dyson  *	vm_object_madvise:
792867a482dSJohn Dyson  *
793867a482dSJohn Dyson  *	Implements the madvise function at the object/page level.
7941c7c3c6aSMatthew Dillon  *
795193b9358SAlan Cox  *	MADV_WILLNEED	(any object)
796193b9358SAlan Cox  *
797193b9358SAlan Cox  *	    Activate the specified pages if they are resident.
798193b9358SAlan Cox  *
799193b9358SAlan Cox  *	MADV_DONTNEED	(any object)
800193b9358SAlan Cox  *
801193b9358SAlan Cox  *	    Deactivate the specified pages if they are resident.
802193b9358SAlan Cox  *
803193b9358SAlan Cox  *	MADV_FREE	(OBJT_DEFAULT/OBJT_SWAP objects,
804193b9358SAlan Cox  *			 OBJ_ONEMAPPING only)
805193b9358SAlan Cox  *
806193b9358SAlan Cox  *	    Deactivate and clean the specified pages if they are
807193b9358SAlan Cox  *	    resident.  This permits the process to reuse the pages
808193b9358SAlan Cox  *	    without faulting or the kernel to reclaim the pages
809193b9358SAlan Cox  *	    without I/O.
810867a482dSJohn Dyson  */
811867a482dSJohn Dyson void
812867a482dSJohn Dyson vm_object_madvise(object, pindex, count, advise)
813867a482dSJohn Dyson 	vm_object_t object;
814867a482dSJohn Dyson 	vm_pindex_t pindex;
815867a482dSJohn Dyson 	int count;
816867a482dSJohn Dyson 	int advise;
817867a482dSJohn Dyson {
8186e20a165SJohn Dyson 	vm_pindex_t end, tpindex;
8196e20a165SJohn Dyson 	vm_object_t tobject;
820867a482dSJohn Dyson 	vm_page_t m;
821867a482dSJohn Dyson 
822867a482dSJohn Dyson 	if (object == NULL)
823867a482dSJohn Dyson 		return;
824867a482dSJohn Dyson 
825867a482dSJohn Dyson 	end = pindex + count;
826867a482dSJohn Dyson 
8271c7c3c6aSMatthew Dillon 	/*
8281c7c3c6aSMatthew Dillon 	 * Locate and adjust resident pages
8291c7c3c6aSMatthew Dillon 	 */
8301c7c3c6aSMatthew Dillon 
8311c7c3c6aSMatthew Dillon 	for (; pindex < end; pindex += 1) {
8326e20a165SJohn Dyson relookup:
8336e20a165SJohn Dyson 		tobject = object;
8346e20a165SJohn Dyson 		tpindex = pindex;
8356e20a165SJohn Dyson shadowlookup:
83658b4e6ccSAlan Cox 		/*
83758b4e6ccSAlan Cox 		 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
83858b4e6ccSAlan Cox 		 * and those pages must be OBJ_ONEMAPPING.
83958b4e6ccSAlan Cox 		 */
84058b4e6ccSAlan Cox 		if (advise == MADV_FREE) {
84158b4e6ccSAlan Cox 			if ((tobject->type != OBJT_DEFAULT &&
84258b4e6ccSAlan Cox 			     tobject->type != OBJT_SWAP) ||
84358b4e6ccSAlan Cox 			    (tobject->flags & OBJ_ONEMAPPING) == 0) {
8446e20a165SJohn Dyson 				continue;
8456e20a165SJohn Dyson 			}
84658b4e6ccSAlan Cox 		}
8471c7c3c6aSMatthew Dillon 
8481c7c3c6aSMatthew Dillon 		m = vm_page_lookup(tobject, tpindex);
8491c7c3c6aSMatthew Dillon 
8501c7c3c6aSMatthew Dillon 		if (m == NULL) {
8511ce137beSMatthew Dillon 			/*
8521ce137beSMatthew Dillon 			 * There may be swap even if there is no backing page
8531ce137beSMatthew Dillon 			 */
8541ce137beSMatthew Dillon 			if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
8551ce137beSMatthew Dillon 				swap_pager_freespace(tobject, tpindex, 1);
8561ce137beSMatthew Dillon 
8571ce137beSMatthew Dillon 			/*
8581ce137beSMatthew Dillon 			 * next object
8591ce137beSMatthew Dillon 			 */
8606e20a165SJohn Dyson 			tobject = tobject->backing_object;
8611c7c3c6aSMatthew Dillon 			if (tobject == NULL)
8621c7c3c6aSMatthew Dillon 				continue;
8636e20a165SJohn Dyson 			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
8646e20a165SJohn Dyson 			goto shadowlookup;
8656e20a165SJohn Dyson 		}
866867a482dSJohn Dyson 
867867a482dSJohn Dyson 		/*
868867a482dSJohn Dyson 		 * If the page is busy or not in a normal active state,
8698b03c8edSMatthew Dillon 		 * we skip it.  If the page is not managed there are no
8708b03c8edSMatthew Dillon 		 * page queues to mess with.  Things can break if we mess
8718b03c8edSMatthew Dillon 		 * with pages in any of the below states.
872867a482dSJohn Dyson 		 */
8731c7c3c6aSMatthew Dillon 		if (
8741c7c3c6aSMatthew Dillon 		    m->hold_count ||
8751c7c3c6aSMatthew Dillon 		    m->wire_count ||
8768b03c8edSMatthew Dillon 		    (m->flags & PG_UNMANAGED) ||
8771c7c3c6aSMatthew Dillon 		    m->valid != VM_PAGE_BITS_ALL
8781c7c3c6aSMatthew Dillon 		) {
879867a482dSJohn Dyson 			continue;
8806e20a165SJohn Dyson 		}
8816e20a165SJohn Dyson 
8821c7c3c6aSMatthew Dillon  		if (vm_page_sleep_busy(m, TRUE, "madvpo"))
8836e20a165SJohn Dyson   			goto relookup;
884867a482dSJohn Dyson 
885867a482dSJohn Dyson 		if (advise == MADV_WILLNEED) {
886867a482dSJohn Dyson 			vm_page_activate(m);
8876e20a165SJohn Dyson 		} else if (advise == MADV_DONTNEED) {
888479112dfSMatthew Dillon 			vm_page_dontneed(m);
8890a47b48bSJohn Dyson 		} else if (advise == MADV_FREE) {
8901c7c3c6aSMatthew Dillon 			/*
8912aaeadf8SMatthew Dillon 			 * Mark the page clean.  This will allow the page
8922aaeadf8SMatthew Dillon 			 * to be freed up by the system.  However, such pages
8932aaeadf8SMatthew Dillon 			 * are often reused quickly by malloc()/free()
8942aaeadf8SMatthew Dillon 			 * so we do not do anything that would cause
8952aaeadf8SMatthew Dillon 			 * a page fault if we can help it.
8962aaeadf8SMatthew Dillon 			 *
8972aaeadf8SMatthew Dillon 			 * Specifically, we do not try to actually free
8982aaeadf8SMatthew Dillon 			 * the page now nor do we try to put it in the
8992aaeadf8SMatthew Dillon 			 * cache (which would cause a page fault on reuse).
90041c67e12SMatthew Dillon 			 *
90141c67e12SMatthew Dillon 			 * But we do make the page is freeable as we
90241c67e12SMatthew Dillon 			 * can without actually taking the step of unmapping
90341c67e12SMatthew Dillon 			 * it.
9041c7c3c6aSMatthew Dillon 			 */
9050385347cSPeter Wemm 			pmap_clear_modify(m);
9066e20a165SJohn Dyson 			m->dirty = 0;
90741c67e12SMatthew Dillon 			m->act_count = 0;
908479112dfSMatthew Dillon 			vm_page_dontneed(m);
9091ce137beSMatthew Dillon 			if (tobject->type == OBJT_SWAP)
9101ce137beSMatthew Dillon 				swap_pager_freespace(tobject, tpindex, 1);
911867a482dSJohn Dyson 		}
912867a482dSJohn Dyson 	}
913867a482dSJohn Dyson }
914867a482dSJohn Dyson 
915867a482dSJohn Dyson /*
916df8bae1dSRodney W. Grimes  *	vm_object_shadow:
917df8bae1dSRodney W. Grimes  *
918df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
919df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
920df8bae1dSRodney W. Grimes  *	object reference is deallocated.
921df8bae1dSRodney W. Grimes  *
922df8bae1dSRodney W. Grimes  *	The new object and offset into that object
923df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
924df8bae1dSRodney W. Grimes  */
925df8bae1dSRodney W. Grimes 
92626f9a767SRodney W. Grimes void
92726f9a767SRodney W. Grimes vm_object_shadow(object, offset, length)
928df8bae1dSRodney W. Grimes 	vm_object_t *object;	/* IN/OUT */
929a316d390SJohn Dyson 	vm_ooffset_t *offset;	/* IN/OUT */
930df8bae1dSRodney W. Grimes 	vm_size_t length;
931df8bae1dSRodney W. Grimes {
932d031cff1SMatthew Dillon 	vm_object_t source;
933d031cff1SMatthew Dillon 	vm_object_t result;
934df8bae1dSRodney W. Grimes 
935df8bae1dSRodney W. Grimes 	source = *object;
936df8bae1dSRodney W. Grimes 
937df8bae1dSRodney W. Grimes 	/*
9389a2f6362SAlan Cox 	 * Don't create the new object if the old object isn't shared.
9399a2f6362SAlan Cox 	 */
9409a2f6362SAlan Cox 
941c7997d57SAlan Cox 	if (source != NULL &&
942c7997d57SAlan Cox 	    source->ref_count == 1 &&
9439a2f6362SAlan Cox 	    source->handle == NULL &&
9449a2f6362SAlan Cox 	    (source->type == OBJT_DEFAULT ||
9459a2f6362SAlan Cox 	     source->type == OBJT_SWAP))
9469a2f6362SAlan Cox 		return;
9479a2f6362SAlan Cox 
9489a2f6362SAlan Cox 	/*
949df8bae1dSRodney W. Grimes 	 * Allocate a new object with the given length
950df8bae1dSRodney W. Grimes 	 */
951971dd342SAlfred Perlstein 	result = vm_object_allocate(OBJT_DEFAULT, length);
952971dd342SAlfred Perlstein 	KASSERT(result != NULL, ("vm_object_shadow: no object for shadowing"));
953df8bae1dSRodney W. Grimes 
954df8bae1dSRodney W. Grimes 	/*
9550d94caffSDavid Greenman 	 * The new object shadows the source object, adding a reference to it.
9560d94caffSDavid Greenman 	 * Our caller changes his reference to point to the new object,
9570d94caffSDavid Greenman 	 * removing a reference to the source object.  Net result: no change
9580d94caffSDavid Greenman 	 * of reference count.
9599b09fe24SMatthew Dillon 	 *
9609b09fe24SMatthew Dillon 	 * Try to optimize the result object's page color when shadowing
961956f3135SPhilippe Charnier 	 * in order to maintain page coloring consistency in the combined
9629b09fe24SMatthew Dillon 	 * shadowed object.
963df8bae1dSRodney W. Grimes 	 */
96424a1cce3SDavid Greenman 	result->backing_object = source;
965de5f6a77SJohn Dyson 	if (source) {
966de5f6a77SJohn Dyson 		TAILQ_INSERT_TAIL(&source->shadow_head, result, shadow_list);
967eaf13dd7SJohn Dyson 		source->shadow_count++;
968eaf13dd7SJohn Dyson 		source->generation++;
9699b09fe24SMatthew Dillon 		result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) & PQ_L2_MASK;
970de5f6a77SJohn Dyson 	}
971df8bae1dSRodney W. Grimes 
972df8bae1dSRodney W. Grimes 	/*
9730d94caffSDavid Greenman 	 * Store the offset into the source object, and fix up the offset into
9740d94caffSDavid Greenman 	 * the new object.
975df8bae1dSRodney W. Grimes 	 */
976df8bae1dSRodney W. Grimes 
97724a1cce3SDavid Greenman 	result->backing_object_offset = *offset;
978df8bae1dSRodney W. Grimes 
979df8bae1dSRodney W. Grimes 	/*
980df8bae1dSRodney W. Grimes 	 * Return the new things
981df8bae1dSRodney W. Grimes 	 */
982df8bae1dSRodney W. Grimes 
983df8bae1dSRodney W. Grimes 	*offset = 0;
984df8bae1dSRodney W. Grimes 	*object = result;
985df8bae1dSRodney W. Grimes }
986df8bae1dSRodney W. Grimes 
9872ad1a3f7SMatthew Dillon #define	OBSC_TEST_ALL_SHADOWED	0x0001
9882ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_NOWAIT	0x0002
9892ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_WAIT	0x0004
9902ad1a3f7SMatthew Dillon 
9912ad1a3f7SMatthew Dillon static __inline int
9922ad1a3f7SMatthew Dillon vm_object_backing_scan(vm_object_t object, int op)
9932ad1a3f7SMatthew Dillon {
9942ad1a3f7SMatthew Dillon 	int s;
9952ad1a3f7SMatthew Dillon 	int r = 1;
9962ad1a3f7SMatthew Dillon 	vm_page_t p;
9972ad1a3f7SMatthew Dillon 	vm_object_t backing_object;
9982ad1a3f7SMatthew Dillon 	vm_pindex_t backing_offset_index;
9992ad1a3f7SMatthew Dillon 
10002ad1a3f7SMatthew Dillon 	s = splvm();
100123955314SAlfred Perlstein 	mtx_assert(&vm_mtx, MA_OWNED);
10022ad1a3f7SMatthew Dillon 
10032ad1a3f7SMatthew Dillon 	backing_object = object->backing_object;
10042ad1a3f7SMatthew Dillon 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
10052ad1a3f7SMatthew Dillon 
10062ad1a3f7SMatthew Dillon 	/*
10072ad1a3f7SMatthew Dillon 	 * Initial conditions
10082ad1a3f7SMatthew Dillon 	 */
10092ad1a3f7SMatthew Dillon 
10102ad1a3f7SMatthew Dillon 	if (op & OBSC_TEST_ALL_SHADOWED) {
10112ad1a3f7SMatthew Dillon 		/*
1012956f3135SPhilippe Charnier 		 * We do not want to have to test for the existence of
10132ad1a3f7SMatthew Dillon 		 * swap pages in the backing object.  XXX but with the
10142ad1a3f7SMatthew Dillon 		 * new swapper this would be pretty easy to do.
10152ad1a3f7SMatthew Dillon 		 *
10162ad1a3f7SMatthew Dillon 		 * XXX what about anonymous MAP_SHARED memory that hasn't
10172ad1a3f7SMatthew Dillon 		 * been ZFOD faulted yet?  If we do not test for this, the
10182ad1a3f7SMatthew Dillon 		 * shadow test may succeed! XXX
10192ad1a3f7SMatthew Dillon 		 */
10202ad1a3f7SMatthew Dillon 		if (backing_object->type != OBJT_DEFAULT) {
10212ad1a3f7SMatthew Dillon 			splx(s);
10222ad1a3f7SMatthew Dillon 			return(0);
10232ad1a3f7SMatthew Dillon 		}
10242ad1a3f7SMatthew Dillon 	}
10252ad1a3f7SMatthew Dillon 	if (op & OBSC_COLLAPSE_WAIT) {
10262ad1a3f7SMatthew Dillon 		vm_object_set_flag(backing_object, OBJ_DEAD);
10272ad1a3f7SMatthew Dillon 	}
10282ad1a3f7SMatthew Dillon 
10292ad1a3f7SMatthew Dillon 	/*
10302ad1a3f7SMatthew Dillon 	 * Our scan
10312ad1a3f7SMatthew Dillon 	 */
10322ad1a3f7SMatthew Dillon 
10332ad1a3f7SMatthew Dillon 	p = TAILQ_FIRST(&backing_object->memq);
10342ad1a3f7SMatthew Dillon 	while (p) {
10352ad1a3f7SMatthew Dillon 		vm_page_t next = TAILQ_NEXT(p, listq);
10362ad1a3f7SMatthew Dillon 		vm_pindex_t new_pindex = p->pindex - backing_offset_index;
10372ad1a3f7SMatthew Dillon 
10382ad1a3f7SMatthew Dillon 		if (op & OBSC_TEST_ALL_SHADOWED) {
10392ad1a3f7SMatthew Dillon 			vm_page_t pp;
10402ad1a3f7SMatthew Dillon 
10412ad1a3f7SMatthew Dillon 			/*
10422ad1a3f7SMatthew Dillon 			 * Ignore pages outside the parent object's range
10432ad1a3f7SMatthew Dillon 			 * and outside the parent object's mapping of the
10442ad1a3f7SMatthew Dillon 			 * backing object.
10452ad1a3f7SMatthew Dillon 			 *
10462ad1a3f7SMatthew Dillon 			 * note that we do not busy the backing object's
10472ad1a3f7SMatthew Dillon 			 * page.
10482ad1a3f7SMatthew Dillon 			 */
10492ad1a3f7SMatthew Dillon 
10502ad1a3f7SMatthew Dillon 			if (
10512ad1a3f7SMatthew Dillon 			    p->pindex < backing_offset_index ||
10522ad1a3f7SMatthew Dillon 			    new_pindex >= object->size
10532ad1a3f7SMatthew Dillon 			) {
10542ad1a3f7SMatthew Dillon 				p = next;
10552ad1a3f7SMatthew Dillon 				continue;
10562ad1a3f7SMatthew Dillon 			}
10572ad1a3f7SMatthew Dillon 
10582ad1a3f7SMatthew Dillon 			/*
10592ad1a3f7SMatthew Dillon 			 * See if the parent has the page or if the parent's
10602ad1a3f7SMatthew Dillon 			 * object pager has the page.  If the parent has the
10612ad1a3f7SMatthew Dillon 			 * page but the page is not valid, the parent's
10622ad1a3f7SMatthew Dillon 			 * object pager must have the page.
10632ad1a3f7SMatthew Dillon 			 *
10642ad1a3f7SMatthew Dillon 			 * If this fails, the parent does not completely shadow
10652ad1a3f7SMatthew Dillon 			 * the object and we might as well give up now.
10662ad1a3f7SMatthew Dillon 			 */
10672ad1a3f7SMatthew Dillon 
10682ad1a3f7SMatthew Dillon 			pp = vm_page_lookup(object, new_pindex);
10692ad1a3f7SMatthew Dillon 			if (
10702ad1a3f7SMatthew Dillon 			    (pp == NULL || pp->valid == 0) &&
10712ad1a3f7SMatthew Dillon 			    !vm_pager_has_page(object, new_pindex, NULL, NULL)
10722ad1a3f7SMatthew Dillon 			) {
10732ad1a3f7SMatthew Dillon 				r = 0;
10742ad1a3f7SMatthew Dillon 				break;
10752ad1a3f7SMatthew Dillon 			}
10762ad1a3f7SMatthew Dillon 		}
10772ad1a3f7SMatthew Dillon 
10782ad1a3f7SMatthew Dillon 		/*
10792ad1a3f7SMatthew Dillon 		 * Check for busy page
10802ad1a3f7SMatthew Dillon 		 */
10812ad1a3f7SMatthew Dillon 
10822ad1a3f7SMatthew Dillon 		if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
10832ad1a3f7SMatthew Dillon 			vm_page_t pp;
10842ad1a3f7SMatthew Dillon 
10852ad1a3f7SMatthew Dillon 			if (op & OBSC_COLLAPSE_NOWAIT) {
10862ad1a3f7SMatthew Dillon 				if (
10872ad1a3f7SMatthew Dillon 				    (p->flags & PG_BUSY) ||
10882ad1a3f7SMatthew Dillon 				    !p->valid ||
10892ad1a3f7SMatthew Dillon 				    p->hold_count ||
10902ad1a3f7SMatthew Dillon 				    p->wire_count ||
10912ad1a3f7SMatthew Dillon 				    p->busy
10922ad1a3f7SMatthew Dillon 				) {
10932ad1a3f7SMatthew Dillon 					p = next;
10942ad1a3f7SMatthew Dillon 					continue;
10952ad1a3f7SMatthew Dillon 				}
10962ad1a3f7SMatthew Dillon 			} else if (op & OBSC_COLLAPSE_WAIT) {
10972ad1a3f7SMatthew Dillon 				if (vm_page_sleep_busy(p, TRUE, "vmocol")) {
10982ad1a3f7SMatthew Dillon 					/*
10992ad1a3f7SMatthew Dillon 					 * If we slept, anything could have
11002ad1a3f7SMatthew Dillon 					 * happened.  Since the object is
11012ad1a3f7SMatthew Dillon 					 * marked dead, the backing offset
11022ad1a3f7SMatthew Dillon 					 * should not have changed so we
11032ad1a3f7SMatthew Dillon 					 * just restart our scan.
11042ad1a3f7SMatthew Dillon 					 */
11052ad1a3f7SMatthew Dillon 					p = TAILQ_FIRST(&backing_object->memq);
11062ad1a3f7SMatthew Dillon 					continue;
11072ad1a3f7SMatthew Dillon 				}
11082ad1a3f7SMatthew Dillon 			}
11092ad1a3f7SMatthew Dillon 
11102ad1a3f7SMatthew Dillon 			/*
11112ad1a3f7SMatthew Dillon 			 * Busy the page
11122ad1a3f7SMatthew Dillon 			 */
11132ad1a3f7SMatthew Dillon 			vm_page_busy(p);
11142ad1a3f7SMatthew Dillon 
11152ad1a3f7SMatthew Dillon 			KASSERT(
11162ad1a3f7SMatthew Dillon 			    p->object == backing_object,
11172ad1a3f7SMatthew Dillon 			    ("vm_object_qcollapse(): object mismatch")
11182ad1a3f7SMatthew Dillon 			);
11192ad1a3f7SMatthew Dillon 
11202ad1a3f7SMatthew Dillon 			/*
11212ad1a3f7SMatthew Dillon 			 * Destroy any associated swap
11222ad1a3f7SMatthew Dillon 			 */
11232ad1a3f7SMatthew Dillon 			if (backing_object->type == OBJT_SWAP) {
11242ad1a3f7SMatthew Dillon 				swap_pager_freespace(
11252ad1a3f7SMatthew Dillon 				    backing_object,
11262ad1a3f7SMatthew Dillon 				    p->pindex,
11272ad1a3f7SMatthew Dillon 				    1
11282ad1a3f7SMatthew Dillon 				);
11292ad1a3f7SMatthew Dillon 			}
11302ad1a3f7SMatthew Dillon 
11312ad1a3f7SMatthew Dillon 			if (
11322ad1a3f7SMatthew Dillon 			    p->pindex < backing_offset_index ||
11332ad1a3f7SMatthew Dillon 			    new_pindex >= object->size
11342ad1a3f7SMatthew Dillon 			) {
11352ad1a3f7SMatthew Dillon 				/*
11362ad1a3f7SMatthew Dillon 				 * Page is out of the parent object's range, we
11372ad1a3f7SMatthew Dillon 				 * can simply destroy it.
11382ad1a3f7SMatthew Dillon 				 */
11392ad1a3f7SMatthew Dillon 				vm_page_protect(p, VM_PROT_NONE);
11402ad1a3f7SMatthew Dillon 				vm_page_free(p);
11412ad1a3f7SMatthew Dillon 				p = next;
11422ad1a3f7SMatthew Dillon 				continue;
11432ad1a3f7SMatthew Dillon 			}
11442ad1a3f7SMatthew Dillon 
11452ad1a3f7SMatthew Dillon 			pp = vm_page_lookup(object, new_pindex);
11462ad1a3f7SMatthew Dillon 			if (
11472ad1a3f7SMatthew Dillon 			    pp != NULL ||
11482ad1a3f7SMatthew Dillon 			    vm_pager_has_page(object, new_pindex, NULL, NULL)
11492ad1a3f7SMatthew Dillon 			) {
11502ad1a3f7SMatthew Dillon 				/*
11512ad1a3f7SMatthew Dillon 				 * page already exists in parent OR swap exists
11522ad1a3f7SMatthew Dillon 				 * for this location in the parent.  Destroy
11532ad1a3f7SMatthew Dillon 				 * the original page from the backing object.
11542ad1a3f7SMatthew Dillon 				 *
11552ad1a3f7SMatthew Dillon 				 * Leave the parent's page alone
11562ad1a3f7SMatthew Dillon 				 */
11572ad1a3f7SMatthew Dillon 				vm_page_protect(p, VM_PROT_NONE);
11582ad1a3f7SMatthew Dillon 				vm_page_free(p);
11592ad1a3f7SMatthew Dillon 				p = next;
11602ad1a3f7SMatthew Dillon 				continue;
11612ad1a3f7SMatthew Dillon 			}
11622ad1a3f7SMatthew Dillon 
11632ad1a3f7SMatthew Dillon 			/*
11642ad1a3f7SMatthew Dillon 			 * Page does not exist in parent, rename the
11652ad1a3f7SMatthew Dillon 			 * page from the backing object to the main object.
1166d1bf5d56SMatthew Dillon 			 *
1167d1bf5d56SMatthew Dillon 			 * If the page was mapped to a process, it can remain
1168d1bf5d56SMatthew Dillon 			 * mapped through the rename.
11692ad1a3f7SMatthew Dillon 			 */
11702ad1a3f7SMatthew Dillon 			if ((p->queue - p->pc) == PQ_CACHE)
11712ad1a3f7SMatthew Dillon 				vm_page_deactivate(p);
11722ad1a3f7SMatthew Dillon 
11732ad1a3f7SMatthew Dillon 			vm_page_rename(p, object, new_pindex);
11742ad1a3f7SMatthew Dillon 			/* page automatically made dirty by rename */
11752ad1a3f7SMatthew Dillon 		}
11762ad1a3f7SMatthew Dillon 		p = next;
11772ad1a3f7SMatthew Dillon 	}
11782ad1a3f7SMatthew Dillon 	splx(s);
11792ad1a3f7SMatthew Dillon 	return(r);
11802ad1a3f7SMatthew Dillon }
11812ad1a3f7SMatthew Dillon 
1182df8bae1dSRodney W. Grimes 
1183df8bae1dSRodney W. Grimes /*
11842fe6e4d7SDavid Greenman  * this version of collapse allows the operation to occur earlier and
11852fe6e4d7SDavid Greenman  * when paging_in_progress is true for an object...  This is not a complete
11862fe6e4d7SDavid Greenman  * operation, but should plug 99.9% of the rest of the leaks.
11872fe6e4d7SDavid Greenman  */
11882fe6e4d7SDavid Greenman static void
11892fe6e4d7SDavid Greenman vm_object_qcollapse(object)
1190d031cff1SMatthew Dillon 	vm_object_t object;
11912fe6e4d7SDavid Greenman {
11922ad1a3f7SMatthew Dillon 	vm_object_t backing_object = object->backing_object;
11932fe6e4d7SDavid Greenman 
11942fe6e4d7SDavid Greenman 	if (backing_object->ref_count != 1)
11952fe6e4d7SDavid Greenman 		return;
11962fe6e4d7SDavid Greenman 
1197010cf3b9SDavid Greenman 	backing_object->ref_count += 2;
1198010cf3b9SDavid Greenman 
11992ad1a3f7SMatthew Dillon 	vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
12001c7c3c6aSMatthew Dillon 
1201010cf3b9SDavid Greenman 	backing_object->ref_count -= 2;
12022fe6e4d7SDavid Greenman }
12032fe6e4d7SDavid Greenman 
1204df8bae1dSRodney W. Grimes /*
1205df8bae1dSRodney W. Grimes  *	vm_object_collapse:
1206df8bae1dSRodney W. Grimes  *
1207df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
1208df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
1209df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
1210df8bae1dSRodney W. Grimes  */
121126f9a767SRodney W. Grimes void
121226f9a767SRodney W. Grimes vm_object_collapse(object)
121324a1cce3SDavid Greenman 	vm_object_t object;
1214df8bae1dSRodney W. Grimes {
121523955314SAlfred Perlstein 
121623955314SAlfred Perlstein 	mtx_assert(&vm_mtx, MA_OWNED);
121723955314SAlfred Perlstein 
1218df8bae1dSRodney W. Grimes 	while (TRUE) {
12192ad1a3f7SMatthew Dillon 		vm_object_t backing_object;
12202ad1a3f7SMatthew Dillon 
1221df8bae1dSRodney W. Grimes 		/*
1222df8bae1dSRodney W. Grimes 		 * Verify that the conditions are right for collapse:
1223df8bae1dSRodney W. Grimes 		 *
12242ad1a3f7SMatthew Dillon 		 * The object exists and the backing object exists.
1225df8bae1dSRodney W. Grimes 		 */
12262fe6e4d7SDavid Greenman 		if (object == NULL)
12272ad1a3f7SMatthew Dillon 			break;
1228df8bae1dSRodney W. Grimes 
122924a1cce3SDavid Greenman 		if ((backing_object = object->backing_object) == NULL)
12302ad1a3f7SMatthew Dillon 			break;
1231df8bae1dSRodney W. Grimes 
1232f919ebdeSDavid Greenman 		/*
1233f919ebdeSDavid Greenman 		 * we check the backing object first, because it is most likely
123424a1cce3SDavid Greenman 		 * not collapsable.
1235f919ebdeSDavid Greenman 		 */
123624a1cce3SDavid Greenman 		if (backing_object->handle != NULL ||
123724a1cce3SDavid Greenman 		    (backing_object->type != OBJT_DEFAULT &&
123824a1cce3SDavid Greenman 		     backing_object->type != OBJT_SWAP) ||
1239f919ebdeSDavid Greenman 		    (backing_object->flags & OBJ_DEAD) ||
124024a1cce3SDavid Greenman 		    object->handle != NULL ||
124124a1cce3SDavid Greenman 		    (object->type != OBJT_DEFAULT &&
124224a1cce3SDavid Greenman 		     object->type != OBJT_SWAP) ||
124324a1cce3SDavid Greenman 		    (object->flags & OBJ_DEAD)) {
12442ad1a3f7SMatthew Dillon 			break;
124524a1cce3SDavid Greenman 		}
12469b4814bbSDavid Greenman 
12472ad1a3f7SMatthew Dillon 		if (
12482ad1a3f7SMatthew Dillon 		    object->paging_in_progress != 0 ||
12492ad1a3f7SMatthew Dillon 		    backing_object->paging_in_progress != 0
12502ad1a3f7SMatthew Dillon 		) {
1251b9921222SDavid Greenman 			vm_object_qcollapse(object);
12522ad1a3f7SMatthew Dillon 			break;
1253df8bae1dSRodney W. Grimes 		}
1254f919ebdeSDavid Greenman 
125526f9a767SRodney W. Grimes 		/*
12560d94caffSDavid Greenman 		 * We know that we can either collapse the backing object (if
12572ad1a3f7SMatthew Dillon 		 * the parent is the only reference to it) or (perhaps) have
12582ad1a3f7SMatthew Dillon 		 * the parent bypass the object if the parent happens to shadow
12592ad1a3f7SMatthew Dillon 		 * all the resident pages in the entire backing object.
12602ad1a3f7SMatthew Dillon 		 *
12612ad1a3f7SMatthew Dillon 		 * This is ignoring pager-backed pages such as swap pages.
12622ad1a3f7SMatthew Dillon 		 * vm_object_backing_scan fails the shadowing test in this
12632ad1a3f7SMatthew Dillon 		 * case.
1264df8bae1dSRodney W. Grimes 		 */
1265df8bae1dSRodney W. Grimes 
1266df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
1267df8bae1dSRodney W. Grimes 			/*
12682ad1a3f7SMatthew Dillon 			 * If there is exactly one reference to the backing
12692ad1a3f7SMatthew Dillon 			 * object, we can collapse it into the parent.
1270df8bae1dSRodney W. Grimes 			 */
1271df8bae1dSRodney W. Grimes 
12722ad1a3f7SMatthew Dillon 			vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1273df8bae1dSRodney W. Grimes 
1274df8bae1dSRodney W. Grimes 			/*
1275df8bae1dSRodney W. Grimes 			 * Move the pager from backing_object to object.
1276df8bae1dSRodney W. Grimes 			 */
1277df8bae1dSRodney W. Grimes 
127824a1cce3SDavid Greenman 			if (backing_object->type == OBJT_SWAP) {
1279d474eaaaSDoug Rabson 				vm_object_pip_add(backing_object, 1);
128024a1cce3SDavid Greenman 
128124a1cce3SDavid Greenman 				/*
12821c7c3c6aSMatthew Dillon 				 * scrap the paging_offset junk and do a
12831c7c3c6aSMatthew Dillon 				 * discrete copy.  This also removes major
12841c7c3c6aSMatthew Dillon 				 * assumptions about how the swap-pager
12851c7c3c6aSMatthew Dillon 				 * works from where it doesn't belong.  The
12861c7c3c6aSMatthew Dillon 				 * new swapper is able to optimize the
12871c7c3c6aSMatthew Dillon 				 * destroy-source case.
128824a1cce3SDavid Greenman 				 */
12891c7c3c6aSMatthew Dillon 
12901c7c3c6aSMatthew Dillon 				vm_object_pip_add(object, 1);
12911c7c3c6aSMatthew Dillon 				swap_pager_copy(
12921c7c3c6aSMatthew Dillon 				    backing_object,
12931c7c3c6aSMatthew Dillon 				    object,
12941c7c3c6aSMatthew Dillon 				    OFF_TO_IDX(object->backing_object_offset), TRUE);
1295f919ebdeSDavid Greenman 				vm_object_pip_wakeup(object);
1296c0503609SDavid Greenman 
1297f919ebdeSDavid Greenman 				vm_object_pip_wakeup(backing_object);
1298c0503609SDavid Greenman 			}
1299df8bae1dSRodney W. Grimes 			/*
1300df8bae1dSRodney W. Grimes 			 * Object now shadows whatever backing_object did.
13012ad1a3f7SMatthew Dillon 			 * Note that the reference to
13022ad1a3f7SMatthew Dillon 			 * backing_object->backing_object moves from within
13032ad1a3f7SMatthew Dillon 			 * backing_object to within object.
1304df8bae1dSRodney W. Grimes 			 */
1305df8bae1dSRodney W. Grimes 
13062ad1a3f7SMatthew Dillon 			TAILQ_REMOVE(
13072ad1a3f7SMatthew Dillon 			    &object->backing_object->shadow_head,
13082ad1a3f7SMatthew Dillon 			    object,
13092ad1a3f7SMatthew Dillon 			    shadow_list
13102ad1a3f7SMatthew Dillon 			);
1311eaf13dd7SJohn Dyson 			object->backing_object->shadow_count--;
1312eaf13dd7SJohn Dyson 			object->backing_object->generation++;
1313de5f6a77SJohn Dyson 			if (backing_object->backing_object) {
13142ad1a3f7SMatthew Dillon 				TAILQ_REMOVE(
13152ad1a3f7SMatthew Dillon 				    &backing_object->backing_object->shadow_head,
13162ad1a3f7SMatthew Dillon 				    backing_object,
13172ad1a3f7SMatthew Dillon 				    shadow_list
13182ad1a3f7SMatthew Dillon 				);
1319eaf13dd7SJohn Dyson 				backing_object->backing_object->shadow_count--;
1320eaf13dd7SJohn Dyson 				backing_object->backing_object->generation++;
1321de5f6a77SJohn Dyson 			}
132224a1cce3SDavid Greenman 			object->backing_object = backing_object->backing_object;
1323de5f6a77SJohn Dyson 			if (object->backing_object) {
13242ad1a3f7SMatthew Dillon 				TAILQ_INSERT_TAIL(
13252ad1a3f7SMatthew Dillon 				    &object->backing_object->shadow_head,
13262ad1a3f7SMatthew Dillon 				    object,
13272ad1a3f7SMatthew Dillon 				    shadow_list
13282ad1a3f7SMatthew Dillon 				);
1329eaf13dd7SJohn Dyson 				object->backing_object->shadow_count++;
1330eaf13dd7SJohn Dyson 				object->backing_object->generation++;
1331de5f6a77SJohn Dyson 			}
13322fe6e4d7SDavid Greenman 
13332ad1a3f7SMatthew Dillon 			object->backing_object_offset +=
13342ad1a3f7SMatthew Dillon 			    backing_object->backing_object_offset;
13352ad1a3f7SMatthew Dillon 
1336df8bae1dSRodney W. Grimes 			/*
1337df8bae1dSRodney W. Grimes 			 * Discard backing_object.
1338df8bae1dSRodney W. Grimes 			 *
13390d94caffSDavid Greenman 			 * Since the backing object has no pages, no pager left,
13400d94caffSDavid Greenman 			 * and no object references within it, all that is
13410d94caffSDavid Greenman 			 * necessary is to dispose of it.
1342df8bae1dSRodney W. Grimes 			 */
1343df8bae1dSRodney W. Grimes 
13442ad1a3f7SMatthew Dillon 			TAILQ_REMOVE(
13452ad1a3f7SMatthew Dillon 			    &vm_object_list,
13462ad1a3f7SMatthew Dillon 			    backing_object,
13472ad1a3f7SMatthew Dillon 			    object_list
13482ad1a3f7SMatthew Dillon 			);
1349df8bae1dSRodney W. Grimes 			vm_object_count--;
1350df8bae1dSRodney W. Grimes 
135199448ed1SJohn Dyson 			zfree(obj_zone, backing_object);
1352df8bae1dSRodney W. Grimes 
1353df8bae1dSRodney W. Grimes 			object_collapses++;
13540d94caffSDavid Greenman 		} else {
135595e5e988SJohn Dyson 			vm_object_t new_backing_object;
1356df8bae1dSRodney W. Grimes 
1357df8bae1dSRodney W. Grimes 			/*
13582ad1a3f7SMatthew Dillon 			 * If we do not entirely shadow the backing object,
13592ad1a3f7SMatthew Dillon 			 * there is nothing we can do so we give up.
1360df8bae1dSRodney W. Grimes 			 */
1361df8bae1dSRodney W. Grimes 
13622ad1a3f7SMatthew Dillon 			if (vm_object_backing_scan(object, OBSC_TEST_ALL_SHADOWED) == 0) {
13632ad1a3f7SMatthew Dillon 				break;
136424a1cce3SDavid Greenman 			}
1365df8bae1dSRodney W. Grimes 
1366df8bae1dSRodney W. Grimes 			/*
13670d94caffSDavid Greenman 			 * Make the parent shadow the next object in the
13680d94caffSDavid Greenman 			 * chain.  Deallocating backing_object will not remove
13690d94caffSDavid Greenman 			 * it, since its reference count is at least 2.
1370df8bae1dSRodney W. Grimes 			 */
1371df8bae1dSRodney W. Grimes 
13722ad1a3f7SMatthew Dillon 			TAILQ_REMOVE(
13732ad1a3f7SMatthew Dillon 			    &backing_object->shadow_head,
13742ad1a3f7SMatthew Dillon 			    object,
13752ad1a3f7SMatthew Dillon 			    shadow_list
13762ad1a3f7SMatthew Dillon 			);
1377eaf13dd7SJohn Dyson 			backing_object->shadow_count--;
1378eaf13dd7SJohn Dyson 			backing_object->generation++;
137995e5e988SJohn Dyson 
138095e5e988SJohn Dyson 			new_backing_object = backing_object->backing_object;
13818aef1712SMatthew Dillon 			if ((object->backing_object = new_backing_object) != NULL) {
138295e5e988SJohn Dyson 				vm_object_reference(new_backing_object);
13832ad1a3f7SMatthew Dillon 				TAILQ_INSERT_TAIL(
13842ad1a3f7SMatthew Dillon 				    &new_backing_object->shadow_head,
13852ad1a3f7SMatthew Dillon 				    object,
13862ad1a3f7SMatthew Dillon 				    shadow_list
13872ad1a3f7SMatthew Dillon 				);
1388eaf13dd7SJohn Dyson 				new_backing_object->shadow_count++;
1389eaf13dd7SJohn Dyson 				new_backing_object->generation++;
139095e5e988SJohn Dyson 				object->backing_object_offset +=
139195e5e988SJohn Dyson 					backing_object->backing_object_offset;
1392de5f6a77SJohn Dyson 			}
1393df8bae1dSRodney W. Grimes 
1394df8bae1dSRodney W. Grimes 			/*
13950d94caffSDavid Greenman 			 * Drop the reference count on backing_object. Since
13960d94caffSDavid Greenman 			 * its ref_count was at least 2, it will not vanish;
1397eaf13dd7SJohn Dyson 			 * so we don't need to call vm_object_deallocate, but
1398eaf13dd7SJohn Dyson 			 * we do anyway.
1399df8bae1dSRodney W. Grimes 			 */
140095e5e988SJohn Dyson 			vm_object_deallocate(backing_object);
1401df8bae1dSRodney W. Grimes 			object_bypasses++;
1402df8bae1dSRodney W. Grimes 		}
1403df8bae1dSRodney W. Grimes 
1404df8bae1dSRodney W. Grimes 		/*
1405df8bae1dSRodney W. Grimes 		 * Try again with this object's new backing object.
1406df8bae1dSRodney W. Grimes 		 */
1407df8bae1dSRodney W. Grimes 	}
1408df8bae1dSRodney W. Grimes }
1409df8bae1dSRodney W. Grimes 
1410df8bae1dSRodney W. Grimes /*
1411df8bae1dSRodney W. Grimes  *	vm_object_page_remove: [internal]
1412df8bae1dSRodney W. Grimes  *
1413df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
1414df8bae1dSRodney W. Grimes  *	object range from the object's list of pages.
1415df8bae1dSRodney W. Grimes  *
1416df8bae1dSRodney W. Grimes  *	The object must be locked.
1417df8bae1dSRodney W. Grimes  */
141826f9a767SRodney W. Grimes void
14197c1f6cedSDavid Greenman vm_object_page_remove(object, start, end, clean_only)
1420d031cff1SMatthew Dillon 	vm_object_t object;
1421d031cff1SMatthew Dillon 	vm_pindex_t start;
1422d031cff1SMatthew Dillon 	vm_pindex_t end;
14237c1f6cedSDavid Greenman 	boolean_t clean_only;
1424df8bae1dSRodney W. Grimes {
1425d031cff1SMatthew Dillon 	vm_page_t p, next;
1426a316d390SJohn Dyson 	unsigned int size;
1427f5ef029eSPoul-Henning Kamp 	int all;
1428df8bae1dSRodney W. Grimes 
142923955314SAlfred Perlstein 	mtx_assert(&vm_mtx, MA_OWNED);
143023955314SAlfred Perlstein 
1431a1a54e9fSAlan Cox 	if (object == NULL ||
1432a1a54e9fSAlan Cox 	    object->resident_page_count == 0)
1433df8bae1dSRodney W. Grimes 		return;
1434df8bae1dSRodney W. Grimes 
143595e5e988SJohn Dyson 	all = ((end == 0) && (start == 0));
143695e5e988SJohn Dyson 
14378b03c8edSMatthew Dillon 	/*
14388b03c8edSMatthew Dillon 	 * Since physically-backed objects do not use managed pages, we can't
14398b03c8edSMatthew Dillon 	 * remove pages from the object (we must instead remove the page
14408b03c8edSMatthew Dillon 	 * references, and then destroy the object).
14418b03c8edSMatthew Dillon 	 */
14428b03c8edSMatthew Dillon 	KASSERT(object->type != OBJT_PHYS, ("attempt to remove pages from a physical object"));
14438b03c8edSMatthew Dillon 
1444d474eaaaSDoug Rabson 	vm_object_pip_add(object, 1);
144526f9a767SRodney W. Grimes again:
144626f9a767SRodney W. Grimes 	size = end - start;
1447a1a54e9fSAlan Cox 	if (all || size > object->resident_page_count / 4) {
1448b18bfc3dSJohn Dyson 		for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
1449b18bfc3dSJohn Dyson 			next = TAILQ_NEXT(p, listq);
145095e5e988SJohn Dyson 			if (all || ((start <= p->pindex) && (p->pindex < end))) {
1451bd7e5f99SJohn Dyson 				if (p->wire_count != 0) {
1452bd7e5f99SJohn Dyson 					vm_page_protect(p, VM_PROT_NONE);
1453ce65e68cSDavid Greenman 					if (!clean_only)
1454bd7e5f99SJohn Dyson 						p->valid = 0;
14550d94caffSDavid Greenman 					continue;
14560d94caffSDavid Greenman 				}
14570891ef4cSJohn Dyson 
1458b18bfc3dSJohn Dyson 				/*
1459b18bfc3dSJohn Dyson 				 * The busy flags are only cleared at
1460b18bfc3dSJohn Dyson 				 * interrupt -- minimize the spl transitions
1461b18bfc3dSJohn Dyson 				 */
1462ffc82b0aSJohn Dyson 
14631c7c3c6aSMatthew Dillon  				if (vm_page_sleep_busy(p, TRUE, "vmopar"))
146426f9a767SRodney W. Grimes  					goto again;
14650891ef4cSJohn Dyson 
14668f9110f6SJohn Dyson 				if (clean_only && p->valid) {
14677c1f6cedSDavid Greenman 					vm_page_test_dirty(p);
14687c1f6cedSDavid Greenman 					if (p->valid & p->dirty)
14697c1f6cedSDavid Greenman 						continue;
14707c1f6cedSDavid Greenman 				}
1471eaf13dd7SJohn Dyson 
1472e69763a3SDoug Rabson 				vm_page_busy(p);
1473f919ebdeSDavid Greenman 				vm_page_protect(p, VM_PROT_NONE);
1474df8bae1dSRodney W. Grimes 				vm_page_free(p);
147526f9a767SRodney W. Grimes 			}
147626f9a767SRodney W. Grimes 		}
147726f9a767SRodney W. Grimes 	} else {
147826f9a767SRodney W. Grimes 		while (size > 0) {
1479bd7e5f99SJohn Dyson 			if ((p = vm_page_lookup(object, start)) != 0) {
1480eaf13dd7SJohn Dyson 
1481bd7e5f99SJohn Dyson 				if (p->wire_count != 0) {
1482bd7e5f99SJohn Dyson 					vm_page_protect(p, VM_PROT_NONE);
1483ce65e68cSDavid Greenman 					if (!clean_only)
1484ce65e68cSDavid Greenman 						p->valid = 0;
1485bd7e5f99SJohn Dyson 					start += 1;
1486bd7e5f99SJohn Dyson 					size -= 1;
1487bd7e5f99SJohn Dyson 					continue;
14880d94caffSDavid Greenman 				}
1489eaf13dd7SJohn Dyson 
1490b18bfc3dSJohn Dyson 				/*
1491b18bfc3dSJohn Dyson 				 * The busy flags are only cleared at
1492b18bfc3dSJohn Dyson 				 * interrupt -- minimize the spl transitions
1493b18bfc3dSJohn Dyson 				 */
14941c7c3c6aSMatthew Dillon  				if (vm_page_sleep_busy(p, TRUE, "vmopar"))
149526f9a767SRodney W. Grimes 					goto again;
1496eaf13dd7SJohn Dyson 
14978f9110f6SJohn Dyson 				if (clean_only && p->valid) {
14987c1f6cedSDavid Greenman 					vm_page_test_dirty(p);
1499bd7e5f99SJohn Dyson 					if (p->valid & p->dirty) {
1500bd7e5f99SJohn Dyson 						start += 1;
1501bd7e5f99SJohn Dyson 						size -= 1;
15027c1f6cedSDavid Greenman 						continue;
15037c1f6cedSDavid Greenman 					}
1504bd7e5f99SJohn Dyson 				}
1505eaf13dd7SJohn Dyson 
1506e69763a3SDoug Rabson 				vm_page_busy(p);
1507f919ebdeSDavid Greenman 				vm_page_protect(p, VM_PROT_NONE);
150826f9a767SRodney W. Grimes 				vm_page_free(p);
150926f9a767SRodney W. Grimes 			}
1510a316d390SJohn Dyson 			start += 1;
1511a316d390SJohn Dyson 			size -= 1;
1512df8bae1dSRodney W. Grimes 		}
1513df8bae1dSRodney W. Grimes 	}
1514f919ebdeSDavid Greenman 	vm_object_pip_wakeup(object);
1515c0503609SDavid Greenman }
1516df8bae1dSRodney W. Grimes 
1517df8bae1dSRodney W. Grimes /*
1518df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
1519df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
1520df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
1521df8bae1dSRodney W. Grimes  *
1522df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
1523df8bae1dSRodney W. Grimes  *
1524df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
1525df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
1526df8bae1dSRodney W. Grimes  *
1527df8bae1dSRodney W. Grimes  *	Parameters:
1528df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
1529df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
1530df8bae1dSRodney W. Grimes  *		next_object	Second object into coalesce
1531df8bae1dSRodney W. Grimes  *		next_offset	Offset into next_object
1532df8bae1dSRodney W. Grimes  *
1533df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
1534df8bae1dSRodney W. Grimes  *		next_size	Size of reference to next_object
1535df8bae1dSRodney W. Grimes  *
1536df8bae1dSRodney W. Grimes  *	Conditions:
1537df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
1538df8bae1dSRodney W. Grimes  */
15390d94caffSDavid Greenman boolean_t
1540a316d390SJohn Dyson vm_object_coalesce(prev_object, prev_pindex, prev_size, next_size)
1541d031cff1SMatthew Dillon 	vm_object_t prev_object;
1542a316d390SJohn Dyson 	vm_pindex_t prev_pindex;
1543df8bae1dSRodney W. Grimes 	vm_size_t prev_size, next_size;
1544df8bae1dSRodney W. Grimes {
1545ea41812fSAlan Cox 	vm_pindex_t next_pindex;
1546df8bae1dSRodney W. Grimes 
154723955314SAlfred Perlstein 	mtx_assert(&vm_mtx, MA_OWNED);
154823955314SAlfred Perlstein 
1549df8bae1dSRodney W. Grimes 	if (prev_object == NULL) {
1550df8bae1dSRodney W. Grimes 		return (TRUE);
1551df8bae1dSRodney W. Grimes 	}
1552df8bae1dSRodney W. Grimes 
15534112823fSMatthew Dillon 	if (prev_object->type != OBJT_DEFAULT &&
15544112823fSMatthew Dillon 	    prev_object->type != OBJT_SWAP) {
155530dcfc09SJohn Dyson 		return (FALSE);
155630dcfc09SJohn Dyson 	}
155730dcfc09SJohn Dyson 
1558df8bae1dSRodney W. Grimes 	/*
1559df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
1560df8bae1dSRodney W. Grimes 	 */
1561df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
1562df8bae1dSRodney W. Grimes 
1563df8bae1dSRodney W. Grimes 	/*
15640d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
15650d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
15660d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
1567df8bae1dSRodney W. Grimes 	 */
1568df8bae1dSRodney W. Grimes 
15698cc7e047SJohn Dyson 	if (prev_object->backing_object != NULL) {
1570df8bae1dSRodney W. Grimes 		return (FALSE);
1571df8bae1dSRodney W. Grimes 	}
1572a316d390SJohn Dyson 
1573a316d390SJohn Dyson 	prev_size >>= PAGE_SHIFT;
1574a316d390SJohn Dyson 	next_size >>= PAGE_SHIFT;
1575ea41812fSAlan Cox 	next_pindex = prev_pindex + prev_size;
15768cc7e047SJohn Dyson 
15778cc7e047SJohn Dyson 	if ((prev_object->ref_count > 1) &&
1578ea41812fSAlan Cox 	    (prev_object->size != next_pindex)) {
15798cc7e047SJohn Dyson 		return (FALSE);
15808cc7e047SJohn Dyson 	}
15818cc7e047SJohn Dyson 
1582df8bae1dSRodney W. Grimes 	/*
15830d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
15840d94caffSDavid Greenman 	 * deallocation.
1585df8bae1dSRodney W. Grimes 	 */
1586ea41812fSAlan Cox 	if (next_pindex < prev_object->size) {
1587df8bae1dSRodney W. Grimes 		vm_object_page_remove(prev_object,
1588ea41812fSAlan Cox 				      next_pindex,
1589ea41812fSAlan Cox 				      next_pindex + next_size, FALSE);
1590ea41812fSAlan Cox 		if (prev_object->type == OBJT_SWAP)
1591ea41812fSAlan Cox 			swap_pager_freespace(prev_object,
1592ea41812fSAlan Cox 					     next_pindex, next_size);
1593ea41812fSAlan Cox 	}
1594df8bae1dSRodney W. Grimes 
1595df8bae1dSRodney W. Grimes 	/*
1596df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
1597df8bae1dSRodney W. Grimes 	 */
1598ea41812fSAlan Cox 	if (next_pindex + next_size > prev_object->size)
1599ea41812fSAlan Cox 		prev_object->size = next_pindex + next_size;
1600df8bae1dSRodney W. Grimes 
1601df8bae1dSRodney W. Grimes 	return (TRUE);
1602df8bae1dSRodney W. Grimes }
1603df8bae1dSRodney W. Grimes 
1604c7c34a24SBruce Evans #include "opt_ddb.h"
1605c3cb3e12SDavid Greenman #ifdef DDB
1606c7c34a24SBruce Evans #include <sys/kernel.h>
1607c7c34a24SBruce Evans 
1608ce9edcf5SPoul-Henning Kamp #include <sys/cons.h>
1609c7c34a24SBruce Evans 
1610c7c34a24SBruce Evans #include <ddb/ddb.h>
1611c7c34a24SBruce Evans 
1612c7c34a24SBruce Evans static int	_vm_object_in_map __P((vm_map_t map, vm_object_t object,
1613c7c34a24SBruce Evans 				       vm_map_entry_t entry));
1614c7c34a24SBruce Evans static int	vm_object_in_map __P((vm_object_t object));
1615c3cb3e12SDavid Greenman 
1616cac597e4SBruce Evans static int
1617a1f6d91cSDavid Greenman _vm_object_in_map(map, object, entry)
1618a1f6d91cSDavid Greenman 	vm_map_t map;
1619a1f6d91cSDavid Greenman 	vm_object_t object;
1620a1f6d91cSDavid Greenman 	vm_map_entry_t entry;
1621a1f6d91cSDavid Greenman {
1622a1f6d91cSDavid Greenman 	vm_map_t tmpm;
1623a1f6d91cSDavid Greenman 	vm_map_entry_t tmpe;
1624a1f6d91cSDavid Greenman 	vm_object_t obj;
1625a1f6d91cSDavid Greenman 	int entcount;
1626a1f6d91cSDavid Greenman 
1627a1f6d91cSDavid Greenman 	if (map == 0)
1628a1f6d91cSDavid Greenman 		return 0;
1629a1f6d91cSDavid Greenman 
1630a1f6d91cSDavid Greenman 	if (entry == 0) {
1631a1f6d91cSDavid Greenman 		tmpe = map->header.next;
1632a1f6d91cSDavid Greenman 		entcount = map->nentries;
1633a1f6d91cSDavid Greenman 		while (entcount-- && (tmpe != &map->header)) {
1634a1f6d91cSDavid Greenman 			if( _vm_object_in_map(map, object, tmpe)) {
1635a1f6d91cSDavid Greenman 				return 1;
1636a1f6d91cSDavid Greenman 			}
1637a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1638a1f6d91cSDavid Greenman 		}
16399fdfe602SMatthew Dillon 	} else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
16409fdfe602SMatthew Dillon 		tmpm = entry->object.sub_map;
1641a1f6d91cSDavid Greenman 		tmpe = tmpm->header.next;
1642a1f6d91cSDavid Greenman 		entcount = tmpm->nentries;
1643a1f6d91cSDavid Greenman 		while (entcount-- && tmpe != &tmpm->header) {
1644a1f6d91cSDavid Greenman 			if( _vm_object_in_map(tmpm, object, tmpe)) {
1645a1f6d91cSDavid Greenman 				return 1;
1646a1f6d91cSDavid Greenman 			}
1647a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1648a1f6d91cSDavid Greenman 		}
16498aef1712SMatthew Dillon 	} else if ((obj = entry->object.vm_object) != NULL) {
165024a1cce3SDavid Greenman 		for(; obj; obj=obj->backing_object)
1651a1f6d91cSDavid Greenman 			if( obj == object) {
1652a1f6d91cSDavid Greenman 				return 1;
1653a1f6d91cSDavid Greenman 			}
1654a1f6d91cSDavid Greenman 	}
1655a1f6d91cSDavid Greenman 	return 0;
1656a1f6d91cSDavid Greenman }
1657a1f6d91cSDavid Greenman 
1658cac597e4SBruce Evans static int
1659a1f6d91cSDavid Greenman vm_object_in_map( object)
1660a1f6d91cSDavid Greenman 	vm_object_t object;
1661a1f6d91cSDavid Greenman {
1662a1f6d91cSDavid Greenman 	struct proc *p;
16631005a129SJohn Baldwin 
16641005a129SJohn Baldwin 	sx_slock(&allproc_lock);
1665fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(p, &allproc, p_list) {
1666a1f6d91cSDavid Greenman 		if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1667a1f6d91cSDavid Greenman 			continue;
1668553629ebSJake Burkholder 		if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
16691005a129SJohn Baldwin 			sx_sunlock(&allproc_lock);
1670a1f6d91cSDavid Greenman 			return 1;
1671a1f6d91cSDavid Greenman 		}
1672553629ebSJake Burkholder 	}
16731005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
1674a1f6d91cSDavid Greenman 	if( _vm_object_in_map( kernel_map, object, 0))
1675a1f6d91cSDavid Greenman 		return 1;
1676a1f6d91cSDavid Greenman 	if( _vm_object_in_map( kmem_map, object, 0))
1677a1f6d91cSDavid Greenman 		return 1;
1678a1f6d91cSDavid Greenman 	if( _vm_object_in_map( pager_map, object, 0))
1679a1f6d91cSDavid Greenman 		return 1;
1680a1f6d91cSDavid Greenman 	if( _vm_object_in_map( buffer_map, object, 0))
1681a1f6d91cSDavid Greenman 		return 1;
1682a1f6d91cSDavid Greenman 	if( _vm_object_in_map( mb_map, object, 0))
1683a1f6d91cSDavid Greenman 		return 1;
1684a1f6d91cSDavid Greenman 	return 0;
1685a1f6d91cSDavid Greenman }
1686a1f6d91cSDavid Greenman 
1687c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check)
1688f708ef1bSPoul-Henning Kamp {
1689a1f6d91cSDavid Greenman 	vm_object_t object;
1690a1f6d91cSDavid Greenman 
1691a1f6d91cSDavid Greenman 	/*
1692a1f6d91cSDavid Greenman 	 * make sure that internal objs are in a map somewhere
1693a1f6d91cSDavid Greenman 	 * and none have zero ref counts.
1694a1f6d91cSDavid Greenman 	 */
1695cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
169624a1cce3SDavid Greenman 		if (object->handle == NULL &&
169724a1cce3SDavid Greenman 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
1698a1f6d91cSDavid Greenman 			if (object->ref_count == 0) {
16993efc015bSPeter Wemm 				db_printf("vmochk: internal obj has zero ref count: %ld\n",
17003efc015bSPeter Wemm 					(long)object->size);
1701a1f6d91cSDavid Greenman 			}
1702a1f6d91cSDavid Greenman 			if (!vm_object_in_map(object)) {
1703fc62ef1fSBruce Evans 				db_printf(
1704fc62ef1fSBruce Evans 			"vmochk: internal obj is not in a map: "
1705fc62ef1fSBruce Evans 			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
1706fc62ef1fSBruce Evans 				    object->ref_count, (u_long)object->size,
1707fc62ef1fSBruce Evans 				    (u_long)object->size,
1708fc62ef1fSBruce Evans 				    (void *)object->backing_object);
1709a1f6d91cSDavid Greenman 			}
1710a1f6d91cSDavid Greenman 		}
1711a1f6d91cSDavid Greenman 	}
1712a1f6d91cSDavid Greenman }
1713a1f6d91cSDavid Greenman 
171426f9a767SRodney W. Grimes /*
1715df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
1716df8bae1dSRodney W. Grimes  */
1717c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static)
1718df8bae1dSRodney W. Grimes {
1719c7c34a24SBruce Evans 	/* XXX convert args. */
1720c7c34a24SBruce Evans 	vm_object_t object = (vm_object_t)addr;
1721c7c34a24SBruce Evans 	boolean_t full = have_addr;
1722c7c34a24SBruce Evans 
1723d031cff1SMatthew Dillon 	vm_page_t p;
1724df8bae1dSRodney W. Grimes 
1725c7c34a24SBruce Evans 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
1726c7c34a24SBruce Evans #define	count	was_count
1727c7c34a24SBruce Evans 
1728d031cff1SMatthew Dillon 	int count;
1729df8bae1dSRodney W. Grimes 
1730df8bae1dSRodney W. Grimes 	if (object == NULL)
1731df8bae1dSRodney W. Grimes 		return;
1732df8bae1dSRodney W. Grimes 
1733eb95adefSBruce Evans 	db_iprintf(
1734eb95adefSBruce Evans 	    "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n",
1735eb95adefSBruce Evans 	    object, (int)object->type, (u_long)object->size,
1736eb95adefSBruce Evans 	    object->resident_page_count, object->ref_count, object->flags);
1737eb95adefSBruce Evans 	/*
17381c7c3c6aSMatthew Dillon 	 * XXX no %qd in kernel.  Truncate object->backing_object_offset.
1739eb95adefSBruce Evans 	 */
17401c7c3c6aSMatthew Dillon 	db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%lx\n",
17411c7c3c6aSMatthew Dillon 	    object->shadow_count,
1742eb95adefSBruce Evans 	    object->backing_object ? object->backing_object->ref_count : 0,
1743eb95adefSBruce Evans 	    object->backing_object, (long)object->backing_object_offset);
1744df8bae1dSRodney W. Grimes 
1745df8bae1dSRodney W. Grimes 	if (!full)
1746df8bae1dSRodney W. Grimes 		return;
1747df8bae1dSRodney W. Grimes 
1748c7c34a24SBruce Evans 	db_indent += 2;
1749df8bae1dSRodney W. Grimes 	count = 0;
1750fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(p, &object->memq, listq) {
1751df8bae1dSRodney W. Grimes 		if (count == 0)
1752c7c34a24SBruce Evans 			db_iprintf("memory:=");
1753df8bae1dSRodney W. Grimes 		else if (count == 6) {
1754c7c34a24SBruce Evans 			db_printf("\n");
1755c7c34a24SBruce Evans 			db_iprintf(" ...");
1756df8bae1dSRodney W. Grimes 			count = 0;
1757df8bae1dSRodney W. Grimes 		} else
1758c7c34a24SBruce Evans 			db_printf(",");
1759df8bae1dSRodney W. Grimes 		count++;
1760df8bae1dSRodney W. Grimes 
1761c7c34a24SBruce Evans 		db_printf("(off=0x%lx,page=0x%lx)",
1762a316d390SJohn Dyson 		    (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
1763df8bae1dSRodney W. Grimes 	}
1764df8bae1dSRodney W. Grimes 	if (count != 0)
1765c7c34a24SBruce Evans 		db_printf("\n");
1766c7c34a24SBruce Evans 	db_indent -= 2;
1767df8bae1dSRodney W. Grimes }
17685070c7f8SJohn Dyson 
1769c7c34a24SBruce Evans /* XXX. */
1770c7c34a24SBruce Evans #undef count
1771c7c34a24SBruce Evans 
1772c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */
17735070c7f8SJohn Dyson void
1774c7c34a24SBruce Evans vm_object_print(addr, have_addr, count, modif)
1775ecbb00a2SDoug Rabson         /* db_expr_t */ long addr;
1776c7c34a24SBruce Evans 	boolean_t have_addr;
1777ecbb00a2SDoug Rabson 	/* db_expr_t */ long count;
1778c7c34a24SBruce Evans 	char *modif;
1779c7c34a24SBruce Evans {
1780c7c34a24SBruce Evans 	vm_object_print_static(addr, have_addr, count, modif);
1781c7c34a24SBruce Evans }
1782c7c34a24SBruce Evans 
1783c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
17845070c7f8SJohn Dyson {
17855070c7f8SJohn Dyson 	vm_object_t object;
17865070c7f8SJohn Dyson 	int nl = 0;
17875070c7f8SJohn Dyson 	int c;
1788cc64b484SAlfred Perlstein 
1789cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
17905070c7f8SJohn Dyson 		vm_pindex_t idx, fidx;
17915070c7f8SJohn Dyson 		vm_pindex_t osize;
17925070c7f8SJohn Dyson 		vm_offset_t pa = -1, padiff;
17935070c7f8SJohn Dyson 		int rcount;
17945070c7f8SJohn Dyson 		vm_page_t m;
17955070c7f8SJohn Dyson 
1796fc62ef1fSBruce Evans 		db_printf("new object: %p\n", (void *)object);
17975070c7f8SJohn Dyson 		if ( nl > 18) {
17985070c7f8SJohn Dyson 			c = cngetc();
17995070c7f8SJohn Dyson 			if (c != ' ')
18005070c7f8SJohn Dyson 				return;
18015070c7f8SJohn Dyson 			nl = 0;
18025070c7f8SJohn Dyson 		}
18035070c7f8SJohn Dyson 		nl++;
18045070c7f8SJohn Dyson 		rcount = 0;
18055070c7f8SJohn Dyson 		fidx = 0;
18065070c7f8SJohn Dyson 		osize = object->size;
18075070c7f8SJohn Dyson 		if (osize > 128)
18085070c7f8SJohn Dyson 			osize = 128;
18095070c7f8SJohn Dyson 		for(idx=0;idx<osize;idx++) {
18105070c7f8SJohn Dyson 			m = vm_page_lookup(object, idx);
18115070c7f8SJohn Dyson 			if (m == NULL) {
18125070c7f8SJohn Dyson 				if (rcount) {
18133efc015bSPeter Wemm 					db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
18143efc015bSPeter Wemm 						(long)fidx, rcount, (long)pa);
18155070c7f8SJohn Dyson 					if ( nl > 18) {
18165070c7f8SJohn Dyson 						c = cngetc();
18175070c7f8SJohn Dyson 						if (c != ' ')
18185070c7f8SJohn Dyson 							return;
18195070c7f8SJohn Dyson 						nl = 0;
18205070c7f8SJohn Dyson 					}
18215070c7f8SJohn Dyson 					nl++;
18225070c7f8SJohn Dyson 					rcount = 0;
18235070c7f8SJohn Dyson 				}
18245070c7f8SJohn Dyson 				continue;
18255070c7f8SJohn Dyson 			}
18265070c7f8SJohn Dyson 
18275070c7f8SJohn Dyson 
18285070c7f8SJohn Dyson 			if (rcount &&
18295070c7f8SJohn Dyson 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
18305070c7f8SJohn Dyson 				++rcount;
18315070c7f8SJohn Dyson 				continue;
18325070c7f8SJohn Dyson 			}
18335070c7f8SJohn Dyson 			if (rcount) {
18345070c7f8SJohn Dyson 				padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
18355070c7f8SJohn Dyson 				padiff >>= PAGE_SHIFT;
18365070c7f8SJohn Dyson 				padiff &= PQ_L2_MASK;
18375070c7f8SJohn Dyson 				if (padiff == 0) {
18385070c7f8SJohn Dyson 					pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
18395070c7f8SJohn Dyson 					++rcount;
18405070c7f8SJohn Dyson 					continue;
18415070c7f8SJohn Dyson 				}
18423efc015bSPeter Wemm 				db_printf(" index(%ld)run(%d)pa(0x%lx)",
18433efc015bSPeter Wemm 					(long)fidx, rcount, (long)pa);
18443efc015bSPeter Wemm 				db_printf("pd(%ld)\n", (long)padiff);
18455070c7f8SJohn Dyson 				if ( nl > 18) {
18465070c7f8SJohn Dyson 					c = cngetc();
18475070c7f8SJohn Dyson 					if (c != ' ')
18485070c7f8SJohn Dyson 						return;
18495070c7f8SJohn Dyson 					nl = 0;
18505070c7f8SJohn Dyson 				}
18515070c7f8SJohn Dyson 				nl++;
18525070c7f8SJohn Dyson 			}
18535070c7f8SJohn Dyson 			fidx = idx;
18545070c7f8SJohn Dyson 			pa = VM_PAGE_TO_PHYS(m);
18555070c7f8SJohn Dyson 			rcount = 1;
18565070c7f8SJohn Dyson 		}
18575070c7f8SJohn Dyson 		if (rcount) {
18583efc015bSPeter Wemm 			db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
18593efc015bSPeter Wemm 				(long)fidx, rcount, (long)pa);
18605070c7f8SJohn Dyson 			if ( nl > 18) {
18615070c7f8SJohn Dyson 				c = cngetc();
18625070c7f8SJohn Dyson 				if (c != ' ')
18635070c7f8SJohn Dyson 					return;
18645070c7f8SJohn Dyson 				nl = 0;
18655070c7f8SJohn Dyson 			}
18665070c7f8SJohn Dyson 			nl++;
18675070c7f8SJohn Dyson 		}
18685070c7f8SJohn Dyson 	}
18695070c7f8SJohn Dyson }
1870c3cb3e12SDavid Greenman #endif /* DDB */
1871