xref: /freebsd/sys/vm/vm_object.c (revision 8aef171243894d9b06e5ac740bfa5e8686fc4c1a)
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
17df8bae1dSRodney W. Grimes  *    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  *
648aef1712SMatthew Dillon  * $Id: vm_object.c,v 1.141 1999/01/24 01:01:38 dillon Exp $
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>
73f23b4c91SGarrett Wollman #include <sys/proc.h>		/* for curproc, pageproc */
740d94caffSDavid Greenman #include <sys/vnode.h>
75efeaf95aSDavid Greenman #include <sys/vmmeter.h>
76867a482dSJohn Dyson #include <sys/mman.h>
77cf2819ccSJohn Dyson #include <sys/mount.h>
78df8bae1dSRodney W. Grimes 
79df8bae1dSRodney W. Grimes #include <vm/vm.h>
80efeaf95aSDavid Greenman #include <vm/vm_param.h>
81efeaf95aSDavid Greenman #include <vm/vm_prot.h>
82efeaf95aSDavid Greenman #include <vm/pmap.h>
83efeaf95aSDavid Greenman #include <vm/vm_map.h>
84efeaf95aSDavid Greenman #include <vm/vm_object.h>
85df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
8626f9a767SRodney W. Grimes #include <vm/vm_pageout.h>
870d94caffSDavid Greenman #include <vm/vm_pager.h>
8805f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h>
89a1f6d91cSDavid Greenman #include <vm/vm_kern.h>
90efeaf95aSDavid Greenman #include <vm/vm_extern.h>
9199448ed1SJohn Dyson #include <vm/vm_zone.h>
9226f9a767SRodney W. Grimes 
93cac597e4SBruce Evans static void	vm_object_qcollapse __P((vm_object_t object));
94f6b04d2bSDavid Greenman 
95df8bae1dSRodney W. Grimes /*
96df8bae1dSRodney W. Grimes  *	Virtual memory objects maintain the actual data
97df8bae1dSRodney W. Grimes  *	associated with allocated virtual memory.  A given
98df8bae1dSRodney W. Grimes  *	page of memory exists within exactly one object.
99df8bae1dSRodney W. Grimes  *
100df8bae1dSRodney W. Grimes  *	An object is only deallocated when all "references"
101df8bae1dSRodney W. Grimes  *	are given up.  Only one "reference" to a given
102df8bae1dSRodney W. Grimes  *	region of an object should be writeable.
103df8bae1dSRodney W. Grimes  *
104df8bae1dSRodney W. Grimes  *	Associated with each object is a list of all resident
105df8bae1dSRodney W. Grimes  *	memory pages belonging to that object; this list is
106df8bae1dSRodney W. Grimes  *	maintained by the "vm_page" module, and locked by the object's
107df8bae1dSRodney W. Grimes  *	lock.
108df8bae1dSRodney W. Grimes  *
109df8bae1dSRodney W. Grimes  *	Each object also records a "pager" routine which is
110df8bae1dSRodney W. Grimes  *	used to retrieve (and store) pages to the proper backing
111df8bae1dSRodney W. Grimes  *	storage.  In addition, objects may be backed by other
112df8bae1dSRodney W. Grimes  *	objects from which they were virtual-copied.
113df8bae1dSRodney W. Grimes  *
114df8bae1dSRodney W. Grimes  *	The only items within the object structure which are
115df8bae1dSRodney W. Grimes  *	modified after time of creation are:
116df8bae1dSRodney W. Grimes  *		reference count		locked by object's lock
117df8bae1dSRodney W. Grimes  *		pager routine		locked by object's lock
118df8bae1dSRodney W. Grimes  *
119df8bae1dSRodney W. Grimes  */
120df8bae1dSRodney W. Grimes 
12128f8db14SBruce Evans struct object_q vm_object_list;
122289bdf33SBruce Evans #ifndef NULL_SIMPLELOCKS
123303b270bSEivind Eklund static struct simplelock vm_object_list_lock;
124289bdf33SBruce Evans #endif
1254de628deSBruce Evans static long vm_object_count;		/* count of all objects */
12628f8db14SBruce Evans vm_object_t kernel_object;
12728f8db14SBruce Evans vm_object_t kmem_object;
128f708ef1bSPoul-Henning Kamp static struct vm_object kernel_object_store;
129f708ef1bSPoul-Henning Kamp static struct vm_object kmem_object_store;
130aef922f5SJohn Dyson extern int vm_pageout_page_count;
131df8bae1dSRodney W. Grimes 
132f708ef1bSPoul-Henning Kamp static long object_collapses;
133f708ef1bSPoul-Henning Kamp static long object_bypasses;
1345070c7f8SJohn Dyson static int next_index;
13599448ed1SJohn Dyson static vm_zone_t obj_zone;
13699448ed1SJohn Dyson static struct vm_zone obj_zone_store;
1371c7c3c6aSMatthew Dillon static int object_hash_rand;
13899448ed1SJohn Dyson #define VM_OBJECTS_INIT 256
139303b270bSEivind Eklund static struct vm_object vm_objects_init[VM_OBJECTS_INIT];
1401c7c3c6aSMatthew Dillon #if 0
141c0877f10SJohn Dyson static int objidnumber;
1421c7c3c6aSMatthew Dillon #endif
143df8bae1dSRodney W. Grimes 
1443075778bSJohn Dyson void
14524a1cce3SDavid Greenman _vm_object_allocate(type, size, object)
14624a1cce3SDavid Greenman 	objtype_t type;
147df8bae1dSRodney W. Grimes 	vm_size_t size;
148df8bae1dSRodney W. Grimes 	register vm_object_t object;
149df8bae1dSRodney W. Grimes {
15099448ed1SJohn Dyson 	int incr;
151df8bae1dSRodney W. Grimes 	TAILQ_INIT(&object->memq);
15224a1cce3SDavid Greenman 	TAILQ_INIT(&object->shadow_head);
153a1f6d91cSDavid Greenman 
15424a1cce3SDavid Greenman 	object->type = type;
155df8bae1dSRodney W. Grimes 	object->size = size;
156a1f6d91cSDavid Greenman 	object->ref_count = 1;
15724a1cce3SDavid Greenman 	object->flags = 0;
1581c7c3c6aSMatthew Dillon #if 0
159c0877f10SJohn Dyson 	object->id = ++objidnumber;
1601c7c3c6aSMatthew Dillon #endif
161c0877f10SJohn Dyson 	if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
162069e9bc1SDoug Rabson 		vm_object_set_flag(object, OBJ_ONEMAPPING);
163867a482dSJohn Dyson 	object->behavior = OBJ_NORMAL;
164df8bae1dSRodney W. Grimes 	object->paging_in_progress = 0;
165a1f6d91cSDavid Greenman 	object->resident_page_count = 0;
166eaf13dd7SJohn Dyson 	object->cache_count = 0;
167eaf13dd7SJohn Dyson 	object->wire_count = 0;
168de5f6a77SJohn Dyson 	object->shadow_count = 0;
1695070c7f8SJohn Dyson 	object->pg_color = next_index;
17099448ed1SJohn Dyson 	if ( size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
17199448ed1SJohn Dyson 		incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
17299448ed1SJohn Dyson 	else
17399448ed1SJohn Dyson 		incr = size;
17499448ed1SJohn Dyson 	next_index = (next_index + incr) & PQ_L2_MASK;
17524a1cce3SDavid Greenman 	object->handle = NULL;
17624a1cce3SDavid Greenman 	object->backing_object = NULL;
177a316d390SJohn Dyson 	object->backing_object_offset = (vm_ooffset_t) 0;
1781c7c3c6aSMatthew Dillon #if 0
179a2f4a846SJohn Dyson 	object->page_hint = NULL;
1801c7c3c6aSMatthew Dillon #endif
1811c7c3c6aSMatthew Dillon 	/*
1821c7c3c6aSMatthew Dillon 	 * Try to generate a number that will spread objects out in the
1831c7c3c6aSMatthew Dillon 	 * hash table.  We 'wipe' new objects across the hash in 128 page
1841c7c3c6aSMatthew Dillon 	 * increments plus 1 more to offset it a little more by the time
1851c7c3c6aSMatthew Dillon 	 * it wraps around.
1861c7c3c6aSMatthew Dillon 	 */
1871c7c3c6aSMatthew Dillon 	object->hash_rand = object_hash_rand - 129;
188a1f6d91cSDavid Greenman 
189a1f6d91cSDavid Greenman 	object->last_read = 0;
1902d8acc0fSJohn Dyson 	object->generation++;
191df8bae1dSRodney W. Grimes 
192df8bae1dSRodney W. Grimes 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
193df8bae1dSRodney W. Grimes 	vm_object_count++;
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);
206996c772fSJohn Dyson 	simple_lock_init(&vm_object_list_lock);
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 {
23826f9a767SRodney W. Grimes 	register vm_object_t result;
23999448ed1SJohn Dyson 	result = (vm_object_t) zalloc(obj_zone);
24067bf6868SJohn Dyson 
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)
254df8bae1dSRodney W. Grimes 	register 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) {
26447221757SJohn Dyson 		while (vget((struct vnode *) object->handle, LK_RETRY|LK_NOOBJ, curproc)) {
26595461b45SJohn Dyson #if !defined(MAX_PERF)
26647221757SJohn Dyson 			printf("vm_object_reference: delay in getting object\n");
26795461b45SJohn Dyson #endif
26847221757SJohn Dyson 		}
26947221757SJohn Dyson 	}
27095e5e988SJohn Dyson }
27195e5e988SJohn Dyson 
272bf27292bSJohn Dyson void
27395e5e988SJohn Dyson vm_object_vndeallocate(object)
27495e5e988SJohn Dyson 	vm_object_t object;
27595e5e988SJohn Dyson {
27695e5e988SJohn Dyson 	struct vnode *vp = (struct vnode *) object->handle;
277219cbf59SEivind Eklund 
2785526d2d9SEivind Eklund 	KASSERT(object->type == OBJT_VNODE,
2795526d2d9SEivind Eklund 	    ("vm_object_vndeallocate: not a vnode object"));
280219cbf59SEivind Eklund 	KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
281219cbf59SEivind Eklund #ifdef INVARIANTS
28295e5e988SJohn Dyson 	if (object->ref_count == 0) {
28395e5e988SJohn Dyson 		vprint("vm_object_vndeallocate", vp);
28495e5e988SJohn Dyson 		panic("vm_object_vndeallocate: bad object reference count");
28595e5e988SJohn Dyson 	}
28695e5e988SJohn Dyson #endif
28795e5e988SJohn Dyson 
28895e5e988SJohn Dyson 	object->ref_count--;
28947221757SJohn Dyson 	if (object->ref_count == 0) {
290bf27292bSJohn Dyson 		vp->v_flag &= ~VTEXT;
291069e9bc1SDoug Rabson 		vm_object_clear_flag(object, OBJ_OPT);
2922be70f79SJohn Dyson 	}
29347221757SJohn Dyson 	vrele(vp);
294df8bae1dSRodney W. Grimes }
295df8bae1dSRodney W. Grimes 
296df8bae1dSRodney W. Grimes /*
297df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
298df8bae1dSRodney W. Grimes  *
299df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
300df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
301df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
302df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
303df8bae1dSRodney W. Grimes  *	may be relinquished.
304df8bae1dSRodney W. Grimes  *
305df8bae1dSRodney W. Grimes  *	No object may be locked.
306df8bae1dSRodney W. Grimes  */
30726f9a767SRodney W. Grimes void
30826f9a767SRodney W. Grimes vm_object_deallocate(object)
30926f9a767SRodney W. Grimes 	vm_object_t object;
310df8bae1dSRodney W. Grimes {
311df8bae1dSRodney W. Grimes 	vm_object_t temp;
312df8bae1dSRodney W. Grimes 
313df8bae1dSRodney W. Grimes 	while (object != NULL) {
314df8bae1dSRodney W. Grimes 
31595e5e988SJohn Dyson 		if (object->type == OBJT_VNODE) {
31695e5e988SJohn Dyson 			vm_object_vndeallocate(object);
31795e5e988SJohn Dyson 			return;
31895e5e988SJohn Dyson 		}
31995e5e988SJohn Dyson 
3202be70f79SJohn Dyson 		if (object->ref_count == 0) {
32147221757SJohn Dyson 			panic("vm_object_deallocate: object deallocated too many times: %d", object->type);
3222be70f79SJohn Dyson 		} else if (object->ref_count > 2) {
3232be70f79SJohn Dyson 			object->ref_count--;
3242be70f79SJohn Dyson 			return;
3252be70f79SJohn Dyson 		}
3262be70f79SJohn Dyson 
3272be70f79SJohn Dyson 		/*
3282be70f79SJohn Dyson 		 * Here on ref_count of one or two, which are special cases for
3292be70f79SJohn Dyson 		 * objects.
3302be70f79SJohn Dyson 		 */
331c0877f10SJohn Dyson 		if ((object->ref_count == 2) && (object->shadow_count == 0)) {
332069e9bc1SDoug Rabson 			vm_object_set_flag(object, OBJ_ONEMAPPING);
333c0877f10SJohn Dyson 			object->ref_count--;
334c0877f10SJohn Dyson 			return;
335c0877f10SJohn Dyson 		} else if ((object->ref_count == 2) && (object->shadow_count == 1)) {
336be6d5bfaSDavid Greenman 			object->ref_count--;
3372be70f79SJohn Dyson 			if ((object->handle == NULL) &&
33824a1cce3SDavid Greenman 			    (object->type == OBJT_DEFAULT ||
33924a1cce3SDavid Greenman 			     object->type == OBJT_SWAP)) {
340a1f6d91cSDavid Greenman 				vm_object_t robject;
34195e5e988SJohn Dyson 
342b18bfc3dSJohn Dyson 				robject = TAILQ_FIRST(&object->shadow_head);
3435526d2d9SEivind Eklund 				KASSERT(robject != NULL,
344219cbf59SEivind Eklund 				    ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
3455526d2d9SEivind Eklund 					 object->ref_count,
3465526d2d9SEivind Eklund 					 object->shadow_count));
34795e5e988SJohn Dyson 				if ((robject->handle == NULL) &&
34824a1cce3SDavid Greenman 				    (robject->type == OBJT_DEFAULT ||
34924a1cce3SDavid Greenman 				     robject->type == OBJT_SWAP)) {
350a1f6d91cSDavid Greenman 
35195e5e988SJohn Dyson 					robject->ref_count++;
35295e5e988SJohn Dyson 
3531c7c3c6aSMatthew Dillon 					while (
3541c7c3c6aSMatthew Dillon 						robject->paging_in_progress ||
3551c7c3c6aSMatthew Dillon 						object->paging_in_progress
3561c7c3c6aSMatthew Dillon 					) {
35766095752SJohn Dyson 						vm_object_pip_sleep(robject, "objde1");
35866095752SJohn Dyson 						vm_object_pip_sleep(object, "objde2");
359a1f6d91cSDavid Greenman 					}
360a1f6d91cSDavid Greenman 
36195e5e988SJohn Dyson 					if (robject->ref_count == 1) {
36295e5e988SJohn Dyson 						robject->ref_count--;
363ba8da839SDavid Greenman 						object = robject;
36495e5e988SJohn Dyson 						goto doterm;
36595e5e988SJohn Dyson 					}
36695e5e988SJohn Dyson 
36795e5e988SJohn Dyson 					object = robject;
36895e5e988SJohn Dyson 					vm_object_collapse(object);
369ba8da839SDavid Greenman 					continue;
370a1f6d91cSDavid Greenman 				}
37195e5e988SJohn Dyson 			}
37295e5e988SJohn Dyson 
373a1f6d91cSDavid Greenman 			return;
374df8bae1dSRodney W. Grimes 
37595e5e988SJohn Dyson 		} else {
3761efb74fbSJohn Dyson 			object->ref_count--;
37795e5e988SJohn Dyson 			if (object->ref_count != 0)
37895e5e988SJohn Dyson 				return;
37995e5e988SJohn Dyson 		}
38095e5e988SJohn Dyson 
38195e5e988SJohn Dyson doterm:
3821efb74fbSJohn Dyson 
38324a1cce3SDavid Greenman 		temp = object->backing_object;
384de5f6a77SJohn Dyson 		if (temp) {
38524a1cce3SDavid Greenman 			TAILQ_REMOVE(&temp->shadow_head, object, shadow_list);
38695e5e988SJohn Dyson 			temp->shadow_count--;
3872d8acc0fSJohn Dyson 			if (temp->ref_count == 0)
388069e9bc1SDoug Rabson 				vm_object_clear_flag(temp, OBJ_OPT);
389eaf13dd7SJohn Dyson 			temp->generation++;
39095461b45SJohn Dyson 			object->backing_object = NULL;
391de5f6a77SJohn Dyson 		}
392df8bae1dSRodney W. Grimes 		vm_object_terminate(object);
393df8bae1dSRodney W. Grimes 		/* unlocks and deallocates object */
394df8bae1dSRodney W. Grimes 		object = temp;
395df8bae1dSRodney W. Grimes 	}
396df8bae1dSRodney W. Grimes }
397df8bae1dSRodney W. Grimes 
398df8bae1dSRodney W. Grimes /*
399df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
400df8bae1dSRodney W. Grimes  *	up all previously used resources.
401df8bae1dSRodney W. Grimes  *
402df8bae1dSRodney W. Grimes  *	The object must be locked.
4031c7c3c6aSMatthew Dillon  *	This routine may block.
404df8bae1dSRodney W. Grimes  */
40595e5e988SJohn Dyson void
40626f9a767SRodney W. Grimes vm_object_terminate(object)
407df8bae1dSRodney W. Grimes 	register vm_object_t object;
408df8bae1dSRodney W. Grimes {
4093af76890SPoul-Henning Kamp 	register vm_page_t p;
410e4b7635dSDavid Greenman 	int s;
411df8bae1dSRodney W. Grimes 
41295e5e988SJohn Dyson 	/*
41395e5e988SJohn Dyson 	 * Make sure no one uses us.
41495e5e988SJohn Dyson 	 */
415069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_DEAD);
4163c631446SJohn Dyson 
417df8bae1dSRodney W. Grimes 	/*
418f6b04d2bSDavid Greenman 	 * wait for the pageout daemon to be done with the object
419df8bae1dSRodney W. Grimes 	 */
42066095752SJohn Dyson 	vm_object_pip_wait(object, "objtrm");
421df8bae1dSRodney W. Grimes 
4225526d2d9SEivind Eklund 	KASSERT(!object->paging_in_progress,
4235526d2d9SEivind Eklund 		("vm_object_terminate: pageout in progress"));
42426f9a767SRodney W. Grimes 
42526f9a767SRodney W. Grimes 	/*
4260d94caffSDavid Greenman 	 * Clean and free the pages, as appropriate. All references to the
4270d94caffSDavid Greenman 	 * object are gone, so we don't need to lock it.
42826f9a767SRodney W. Grimes 	 */
42924a1cce3SDavid Greenman 	if (object->type == OBJT_VNODE) {
43095e5e988SJohn Dyson 		struct vnode *vp;
43195e5e988SJohn Dyson 
43295e5e988SJohn Dyson 		/*
43395e5e988SJohn Dyson 		 * Freeze optimized copies.
43495e5e988SJohn Dyson 		 */
43595e5e988SJohn Dyson 		vm_freeze_copyopts(object, 0, object->size);
43695e5e988SJohn Dyson 
43795e5e988SJohn Dyson 		/*
43895e5e988SJohn Dyson 		 * Clean pages and flush buffers.
43995e5e988SJohn Dyson 		 */
4408f9110f6SJohn Dyson 		vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
44195e5e988SJohn Dyson 
44295e5e988SJohn Dyson 		vp = (struct vnode *) object->handle;
443f6b04d2bSDavid Greenman 		vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
444bef608bdSJohn Dyson 	}
445bef608bdSJohn Dyson 
4460b10ba98SDavid Greenman 	if (object->ref_count != 0)
4470b10ba98SDavid Greenman 		panic("vm_object_terminate: object with references, ref_count=%d", object->ref_count);
448996c772fSJohn Dyson 
4490d94caffSDavid Greenman 	/*
450356863ebSDavid Greenman 	 * Now free any remaining pages. For internal objects, this also
451356863ebSDavid Greenman 	 * removes them from paging queues. Don't free wired pages, just
452356863ebSDavid Greenman 	 * remove them from the object.
453df8bae1dSRodney W. Grimes 	 */
454e4b7635dSDavid Greenman 	s = splvm();
455b18bfc3dSJohn Dyson 	while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
45695461b45SJohn Dyson #if !defined(MAX_PERF)
457a2f4a846SJohn Dyson 		if (p->busy || (p->flags & PG_BUSY))
4581c7c3c6aSMatthew Dillon 			panic("vm_object_terminate: freeing busy page %p\n", p);
45995461b45SJohn Dyson #endif
4600b10ba98SDavid Greenman 		if (p->wire_count == 0) {
461e69763a3SDoug Rabson 			vm_page_busy(p);
462df8bae1dSRodney W. Grimes 			vm_page_free(p);
463df8bae1dSRodney W. Grimes 			cnt.v_pfree++;
4640b10ba98SDavid Greenman 		} else {
465356863ebSDavid Greenman 			vm_page_busy(p);
4660b10ba98SDavid Greenman 			vm_page_remove(p);
4670b10ba98SDavid Greenman 		}
468df8bae1dSRodney W. Grimes 	}
469e4b7635dSDavid Greenman 	splx(s);
470bef608bdSJohn Dyson 
4712d8acc0fSJohn Dyson 	/*
4729fcfb650SDavid Greenman 	 * Let the pager know object is dead.
4739fcfb650SDavid Greenman 	 */
4749fcfb650SDavid Greenman 	vm_pager_deallocate(object);
4759fcfb650SDavid Greenman 
4769fcfb650SDavid Greenman 	/*
4770b10ba98SDavid Greenman 	 * Remove the object from the global object list.
4782d8acc0fSJohn Dyson 	 */
479996c772fSJohn Dyson 	simple_lock(&vm_object_list_lock);
480df8bae1dSRodney W. Grimes 	TAILQ_REMOVE(&vm_object_list, object, object_list);
481996c772fSJohn Dyson 	simple_unlock(&vm_object_list_lock);
4820b10ba98SDavid Greenman 
4830b10ba98SDavid Greenman 	wakeup(object);
4840b10ba98SDavid Greenman 
485df8bae1dSRodney W. Grimes 	/*
486df8bae1dSRodney W. Grimes 	 * Free the space for the object.
487df8bae1dSRodney W. Grimes 	 */
48899448ed1SJohn Dyson 	zfree(obj_zone, object);
48947221757SJohn Dyson }
490df8bae1dSRodney W. Grimes 
491df8bae1dSRodney W. Grimes /*
492df8bae1dSRodney W. Grimes  *	vm_object_page_clean
493df8bae1dSRodney W. Grimes  *
494df8bae1dSRodney W. Grimes  *	Clean all dirty pages in the specified range of object.
49526f9a767SRodney W. Grimes  *	Leaves page on whatever queue it is currently on.
49626f9a767SRodney W. Grimes  *
49726f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
49826f9a767SRodney W. Grimes  *
49926f9a767SRodney W. Grimes  *	The object must be locked.
50026f9a767SRodney W. Grimes  */
501f6b04d2bSDavid Greenman 
502f6b04d2bSDavid Greenman void
5038f9110f6SJohn Dyson vm_object_page_clean(object, start, end, flags)
504f6b04d2bSDavid Greenman 	vm_object_t object;
505a316d390SJohn Dyson 	vm_pindex_t start;
506a316d390SJohn Dyson 	vm_pindex_t end;
5078f9110f6SJohn Dyson 	int flags;
508f6b04d2bSDavid Greenman {
509bd7e5f99SJohn Dyson 	register vm_page_t p, np, tp;
510f6b04d2bSDavid Greenman 	register vm_offset_t tstart, tend;
511bd7e5f99SJohn Dyson 	vm_pindex_t pi;
512aef922f5SJohn Dyson 	int s;
51324a1cce3SDavid Greenman 	struct vnode *vp;
514aef922f5SJohn Dyson 	int runlen;
515bd7e5f99SJohn Dyson 	int maxf;
516bd7e5f99SJohn Dyson 	int chkb;
517bd7e5f99SJohn Dyson 	int maxb;
518bd7e5f99SJohn Dyson 	int i;
5198f9110f6SJohn Dyson 	int pagerflags;
520bd7e5f99SJohn Dyson 	vm_page_t maf[vm_pageout_page_count];
521bd7e5f99SJohn Dyson 	vm_page_t mab[vm_pageout_page_count];
522aef922f5SJohn Dyson 	vm_page_t ma[vm_pageout_page_count];
5232d8acc0fSJohn Dyson 	int curgeneration;
524f6b04d2bSDavid Greenman 
525aef922f5SJohn Dyson 	if (object->type != OBJT_VNODE ||
526aef922f5SJohn Dyson 		(object->flags & OBJ_MIGHTBEDIRTY) == 0)
527f6b04d2bSDavid Greenman 		return;
528f6b04d2bSDavid Greenman 
5298f9110f6SJohn Dyson 	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : 0;
5308f9110f6SJohn Dyson 	pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
5318f9110f6SJohn Dyson 
53224a1cce3SDavid Greenman 	vp = object->handle;
53324a1cce3SDavid Greenman 
534069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_CLEANING);
53524a1cce3SDavid Greenman 
536f6b04d2bSDavid Greenman 	tstart = start;
537f6b04d2bSDavid Greenman 	if (end == 0) {
538f6b04d2bSDavid Greenman 		tend = object->size;
539f6b04d2bSDavid Greenman 	} else {
540f6b04d2bSDavid Greenman 		tend = end;
541f6b04d2bSDavid Greenman 	}
542eaf13dd7SJohn Dyson 
543eaf13dd7SJohn Dyson 	for(p = TAILQ_FIRST(&object->memq); p; p = TAILQ_NEXT(p, listq)) {
544e69763a3SDoug Rabson 		vm_page_flag_set(p, PG_CLEANCHK);
545eaf13dd7SJohn Dyson 		vm_page_protect(p, VM_PROT_READ);
546eaf13dd7SJohn Dyson 	}
547eaf13dd7SJohn Dyson 
548a316d390SJohn Dyson 	if ((tstart == 0) && (tend == object->size)) {
549069e9bc1SDoug Rabson 		vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
550ec4f9fb0SDavid Greenman 	}
551f6b04d2bSDavid Greenman 
552bd7e5f99SJohn Dyson rescan:
5532d8acc0fSJohn Dyson 	curgeneration = object->generation;
5542d8acc0fSJohn Dyson 
555b18bfc3dSJohn Dyson 	for(p = TAILQ_FIRST(&object->memq); p; p = np) {
556b18bfc3dSJohn Dyson 		np = TAILQ_NEXT(p, listq);
557bd7e5f99SJohn Dyson 
558bd7e5f99SJohn Dyson 		pi = p->pindex;
559bd7e5f99SJohn Dyson 		if (((p->flags & PG_CLEANCHK) == 0) ||
560bd7e5f99SJohn Dyson 			(pi < tstart) || (pi >= tend) ||
5615070c7f8SJohn Dyson 			(p->valid == 0) ||
5625070c7f8SJohn Dyson 			((p->queue - p->pc) == PQ_CACHE)) {
563e69763a3SDoug Rabson 			vm_page_flag_clear(p, PG_CLEANCHK);
564aef922f5SJohn Dyson 			continue;
565f6b04d2bSDavid Greenman 		}
566f6b04d2bSDavid Greenman 
567bd7e5f99SJohn Dyson 		vm_page_test_dirty(p);
568bd7e5f99SJohn Dyson 		if ((p->dirty & p->valid) == 0) {
569e69763a3SDoug Rabson 			vm_page_flag_clear(p, PG_CLEANCHK);
570bd7e5f99SJohn Dyson 			continue;
571bd7e5f99SJohn Dyson 		}
572ec4f9fb0SDavid Greenman 
573b18bfc3dSJohn Dyson 		s = splvm();
5741c7c3c6aSMatthew Dillon 		while (vm_page_sleep_busy(p, TRUE, "vpcwai")) {
5752d8acc0fSJohn Dyson 			if (object->generation != curgeneration) {
576f6b04d2bSDavid Greenman 				splx(s);
577bd7e5f99SJohn Dyson 				goto rescan;
578f6b04d2bSDavid Greenman 			}
5792d8acc0fSJohn Dyson 		}
580f6b04d2bSDavid Greenman 
581bd7e5f99SJohn Dyson 		maxf = 0;
582bd7e5f99SJohn Dyson 		for(i=1;i<vm_pageout_page_count;i++) {
5838aef1712SMatthew Dillon 			if ((tp = vm_page_lookup(object, pi + i)) != NULL) {
584bd7e5f99SJohn Dyson 				if ((tp->flags & PG_BUSY) ||
585ffc82b0aSJohn Dyson 					(tp->flags & PG_CLEANCHK) == 0 ||
586ffc82b0aSJohn Dyson 					(tp->busy != 0))
587bd7e5f99SJohn Dyson 					break;
5885070c7f8SJohn Dyson 				if((tp->queue - tp->pc) == PQ_CACHE) {
589e69763a3SDoug Rabson 					vm_page_flag_clear(tp, PG_CLEANCHK);
5903077a9c2SJohn Dyson 					break;
5913077a9c2SJohn Dyson 				}
592bd7e5f99SJohn Dyson 				vm_page_test_dirty(tp);
593bd7e5f99SJohn Dyson 				if ((tp->dirty & tp->valid) == 0) {
594e69763a3SDoug Rabson 					vm_page_flag_clear(tp, PG_CLEANCHK);
595bd7e5f99SJohn Dyson 					break;
596bd7e5f99SJohn Dyson 				}
597bd7e5f99SJohn Dyson 				maf[ i - 1 ] = tp;
598bd7e5f99SJohn Dyson 				maxf++;
599bd7e5f99SJohn Dyson 				continue;
600bd7e5f99SJohn Dyson 			}
601bd7e5f99SJohn Dyson 			break;
602bd7e5f99SJohn Dyson 		}
603aef922f5SJohn Dyson 
604bd7e5f99SJohn Dyson 		maxb = 0;
605bd7e5f99SJohn Dyson 		chkb = vm_pageout_page_count -  maxf;
606bd7e5f99SJohn Dyson 		if (chkb) {
607bd7e5f99SJohn Dyson 			for(i = 1; i < chkb;i++) {
6088aef1712SMatthew Dillon 				if ((tp = vm_page_lookup(object, pi - i)) != NULL) {
609bd7e5f99SJohn Dyson 					if ((tp->flags & PG_BUSY) ||
610ffc82b0aSJohn Dyson 						(tp->flags & PG_CLEANCHK) == 0 ||
611ffc82b0aSJohn Dyson 						(tp->busy != 0))
612bd7e5f99SJohn Dyson 						break;
6135070c7f8SJohn Dyson 					if((tp->queue - tp->pc) == PQ_CACHE) {
614e69763a3SDoug Rabson 						vm_page_flag_clear(tp, PG_CLEANCHK);
6153077a9c2SJohn Dyson 						break;
6163077a9c2SJohn Dyson 					}
617bd7e5f99SJohn Dyson 					vm_page_test_dirty(tp);
618bd7e5f99SJohn Dyson 					if ((tp->dirty & tp->valid) == 0) {
619e69763a3SDoug Rabson 						vm_page_flag_clear(tp, PG_CLEANCHK);
620bd7e5f99SJohn Dyson 						break;
621bd7e5f99SJohn Dyson 					}
622bd7e5f99SJohn Dyson 					mab[ i - 1 ] = tp;
623bd7e5f99SJohn Dyson 					maxb++;
624bd7e5f99SJohn Dyson 					continue;
625bd7e5f99SJohn Dyson 				}
626bd7e5f99SJohn Dyson 				break;
627bd7e5f99SJohn Dyson 			}
628bd7e5f99SJohn Dyson 		}
629bd7e5f99SJohn Dyson 
630bd7e5f99SJohn Dyson 		for(i=0;i<maxb;i++) {
631bd7e5f99SJohn Dyson 			int index = (maxb - i) - 1;
632bd7e5f99SJohn Dyson 			ma[index] = mab[i];
633e69763a3SDoug Rabson 			vm_page_flag_clear(ma[index], PG_CLEANCHK);
634bd7e5f99SJohn Dyson 		}
635e69763a3SDoug Rabson 		vm_page_flag_clear(p, PG_CLEANCHK);
636bd7e5f99SJohn Dyson 		ma[maxb] = p;
637bd7e5f99SJohn Dyson 		for(i=0;i<maxf;i++) {
638bd7e5f99SJohn Dyson 			int index = (maxb + i) + 1;
639bd7e5f99SJohn Dyson 			ma[index] = maf[i];
640e69763a3SDoug Rabson 			vm_page_flag_clear(ma[index], PG_CLEANCHK);
641f6b04d2bSDavid Greenman 		}
642bd7e5f99SJohn Dyson 		runlen = maxb + maxf + 1;
643cf2819ccSJohn Dyson 
644f35329acSJohn Dyson 		splx(s);
6458f9110f6SJohn Dyson 		vm_pageout_flush(ma, runlen, pagerflags);
646cf2819ccSJohn Dyson 		for (i = 0; i<runlen; i++) {
647cf2819ccSJohn Dyson 			if (ma[i]->valid & ma[i]->dirty) {
648cf2819ccSJohn Dyson 				vm_page_protect(ma[i], VM_PROT_READ);
649e69763a3SDoug Rabson 				vm_page_flag_set(ma[i], PG_CLEANCHK);
650cf2819ccSJohn Dyson 			}
651cf2819ccSJohn Dyson 		}
6522d8acc0fSJohn Dyson 		if (object->generation != curgeneration)
653bd7e5f99SJohn Dyson 			goto rescan;
654f6b04d2bSDavid Greenman 	}
655aef922f5SJohn Dyson 
656cf2819ccSJohn Dyson 	VOP_FSYNC(vp, NULL, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
657aef922f5SJohn Dyson 
658069e9bc1SDoug Rabson 	vm_object_clear_flag(object, OBJ_CLEANING);
659f5cf85d4SDavid Greenman 	return;
66026f9a767SRodney W. Grimes }
661df8bae1dSRodney W. Grimes 
662f708ef1bSPoul-Henning Kamp #ifdef not_used
663f708ef1bSPoul-Henning Kamp /* XXX I cannot tell if this should be an exported symbol */
664df8bae1dSRodney W. Grimes /*
665df8bae1dSRodney W. Grimes  *	vm_object_deactivate_pages
666df8bae1dSRodney W. Grimes  *
667df8bae1dSRodney W. Grimes  *	Deactivate all pages in the specified object.  (Keep its pages
668df8bae1dSRodney W. Grimes  *	in memory even though it is no longer referenced.)
669df8bae1dSRodney W. Grimes  *
670df8bae1dSRodney W. Grimes  *	The object must be locked.
671df8bae1dSRodney W. Grimes  */
672f708ef1bSPoul-Henning Kamp static void
673df8bae1dSRodney W. Grimes vm_object_deactivate_pages(object)
674df8bae1dSRodney W. Grimes 	register vm_object_t object;
675df8bae1dSRodney W. Grimes {
676df8bae1dSRodney W. Grimes 	register vm_page_t p, next;
677df8bae1dSRodney W. Grimes 
678b18bfc3dSJohn Dyson 	for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
679b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
680df8bae1dSRodney W. Grimes 		vm_page_deactivate(p);
681df8bae1dSRodney W. Grimes 	}
682df8bae1dSRodney W. Grimes }
683f708ef1bSPoul-Henning Kamp #endif
684df8bae1dSRodney W. Grimes 
6858e3ad7c9SMatthew Dillon #if 0
6868e3ad7c9SMatthew Dillon 
687df8bae1dSRodney W. Grimes /*
688df8bae1dSRodney W. Grimes  *	vm_object_pmap_copy:
689df8bae1dSRodney W. Grimes  *
690df8bae1dSRodney W. Grimes  *	Makes all physical pages in the specified
691df8bae1dSRodney W. Grimes  *	object range copy-on-write.  No writeable
692df8bae1dSRodney W. Grimes  *	references to these pages should remain.
693df8bae1dSRodney W. Grimes  *
694df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
695df8bae1dSRodney W. Grimes  */
6960d94caffSDavid Greenman void
6970d94caffSDavid Greenman vm_object_pmap_copy(object, start, end)
698df8bae1dSRodney W. Grimes 	register vm_object_t object;
699a316d390SJohn Dyson 	register vm_pindex_t start;
700a316d390SJohn Dyson 	register vm_pindex_t end;
701df8bae1dSRodney W. Grimes {
702df8bae1dSRodney W. Grimes 	register vm_page_t p;
703df8bae1dSRodney W. Grimes 
704aef922f5SJohn Dyson 	if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
705df8bae1dSRodney W. Grimes 		return;
706df8bae1dSRodney W. Grimes 
707b5b40fa6SJohn Dyson 	for (p = TAILQ_FIRST(&object->memq);
708b5b40fa6SJohn Dyson 		p != NULL;
709b5b40fa6SJohn Dyson 		p = TAILQ_NEXT(p, listq)) {
710f919ebdeSDavid Greenman 		vm_page_protect(p, VM_PROT_READ);
711df8bae1dSRodney W. Grimes 	}
712aef922f5SJohn Dyson 
713069e9bc1SDoug Rabson 	vm_object_clear_flag(object, OBJ_WRITEABLE);
714df8bae1dSRodney W. Grimes }
715df8bae1dSRodney W. Grimes 
7168e3ad7c9SMatthew Dillon #endif
7178e3ad7c9SMatthew Dillon 
718df8bae1dSRodney W. Grimes /*
7198e3ad7c9SMatthew Dillon  * Same as vm_object_pmap_copy, except range checking really
7201efb74fbSJohn Dyson  * works, and is meant for small sections of an object.
7218e3ad7c9SMatthew Dillon  *
7228e3ad7c9SMatthew Dillon  * This code protects resident pages by making them read-only
7238e3ad7c9SMatthew Dillon  * and is typically called on a fork or split when a page
7248e3ad7c9SMatthew Dillon  * is converted to copy-on-write.
7258e3ad7c9SMatthew Dillon  *
7268e3ad7c9SMatthew Dillon  * NOTE: If the page is already at VM_PROT_NONE, calling
7278e3ad7c9SMatthew Dillon  * vm_page_protect will have no effect.
7281efb74fbSJohn Dyson  */
7298e3ad7c9SMatthew Dillon 
7301efb74fbSJohn Dyson void
7311efb74fbSJohn Dyson vm_object_pmap_copy_1(object, start, end)
7321efb74fbSJohn Dyson 	register vm_object_t object;
7331efb74fbSJohn Dyson 	register vm_pindex_t start;
7341efb74fbSJohn Dyson 	register vm_pindex_t end;
7351efb74fbSJohn Dyson {
7361efb74fbSJohn Dyson 	vm_pindex_t idx;
7371efb74fbSJohn Dyson 	register vm_page_t p;
7381efb74fbSJohn Dyson 
7391efb74fbSJohn Dyson 	if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
7401efb74fbSJohn Dyson 		return;
7411efb74fbSJohn Dyson 
7421efb74fbSJohn Dyson 	for (idx = start; idx < end; idx++) {
7431efb74fbSJohn Dyson 		p = vm_page_lookup(object, idx);
7441efb74fbSJohn Dyson 		if (p == NULL)
7451efb74fbSJohn Dyson 			continue;
7461efb74fbSJohn Dyson 		vm_page_protect(p, VM_PROT_READ);
7471efb74fbSJohn Dyson 	}
7481efb74fbSJohn Dyson }
7491efb74fbSJohn Dyson 
7501efb74fbSJohn Dyson /*
751df8bae1dSRodney W. Grimes  *	vm_object_pmap_remove:
752df8bae1dSRodney W. Grimes  *
753df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
754df8bae1dSRodney W. Grimes  *	object range from all physical maps.
755df8bae1dSRodney W. Grimes  *
756df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
757df8bae1dSRodney W. Grimes  */
75826f9a767SRodney W. Grimes void
75926f9a767SRodney W. Grimes vm_object_pmap_remove(object, start, end)
760df8bae1dSRodney W. Grimes 	register vm_object_t object;
761a316d390SJohn Dyson 	register vm_pindex_t start;
762a316d390SJohn Dyson 	register vm_pindex_t end;
763df8bae1dSRodney W. Grimes {
764df8bae1dSRodney W. Grimes 	register vm_page_t p;
765df8bae1dSRodney W. Grimes 	if (object == NULL)
766df8bae1dSRodney W. Grimes 		return;
767b5b40fa6SJohn Dyson 	for (p = TAILQ_FIRST(&object->memq);
768b5b40fa6SJohn Dyson 		p != NULL;
769b5b40fa6SJohn Dyson 		p = TAILQ_NEXT(p, listq)) {
770bd7e5f99SJohn Dyson 		if (p->pindex >= start && p->pindex < end)
771f919ebdeSDavid Greenman 			vm_page_protect(p, VM_PROT_NONE);
77226f9a767SRodney W. Grimes 	}
7736e20a165SJohn Dyson 	if ((start == 0) && (object->size == end))
774069e9bc1SDoug Rabson 		vm_object_clear_flag(object, OBJ_WRITEABLE);
77526f9a767SRodney W. Grimes }
776df8bae1dSRodney W. Grimes 
777df8bae1dSRodney W. Grimes /*
778867a482dSJohn Dyson  *	vm_object_madvise:
779867a482dSJohn Dyson  *
780867a482dSJohn Dyson  *	Implements the madvise function at the object/page level.
7811c7c3c6aSMatthew Dillon  *
7821c7c3c6aSMatthew Dillon  *	Currently, madvise() functions are limited to the default and
7831c7c3c6aSMatthew Dillon  *	swap object types only, and also limited to only the unshared portions
7841c7c3c6aSMatthew Dillon  *	of a process's address space.  MADV_FREE, certainly, could never be
7851c7c3c6aSMatthew Dillon  *	run on anything else.  The others are more flexible and the code could
7861c7c3c6aSMatthew Dillon  *	be adjusted in the future to handle expanded cases for them.
787867a482dSJohn Dyson  */
788867a482dSJohn Dyson void
789867a482dSJohn Dyson vm_object_madvise(object, pindex, count, advise)
790867a482dSJohn Dyson 	vm_object_t object;
791867a482dSJohn Dyson 	vm_pindex_t pindex;
792867a482dSJohn Dyson 	int count;
793867a482dSJohn Dyson 	int advise;
794867a482dSJohn Dyson {
7956e20a165SJohn Dyson 	vm_pindex_t end, tpindex;
7966e20a165SJohn Dyson 	vm_object_t tobject;
797867a482dSJohn Dyson 	vm_page_t m;
798867a482dSJohn Dyson 
799867a482dSJohn Dyson 	if (object == NULL)
800867a482dSJohn Dyson 		return;
801867a482dSJohn Dyson 
802867a482dSJohn Dyson 	end = pindex + count;
803867a482dSJohn Dyson 
8041c7c3c6aSMatthew Dillon 	/*
8051c7c3c6aSMatthew Dillon 	 * MADV_FREE special case - free any swap backing store (as well
8061c7c3c6aSMatthew Dillon 	 * as resident pages later on).
8071c7c3c6aSMatthew Dillon 	 */
8086e20a165SJohn Dyson 
8091c7c3c6aSMatthew Dillon 	if (advise == MADV_FREE) {
8101c7c3c6aSMatthew Dillon 		tobject = object;
8111c7c3c6aSMatthew Dillon 		tpindex = pindex;
8121c7c3c6aSMatthew Dillon 
8131c7c3c6aSMatthew Dillon 		while (
8141c7c3c6aSMatthew Dillon 		    (tobject->type == OBJT_DEFAULT ||
8151c7c3c6aSMatthew Dillon 		     tobject->type == OBJT_SWAP) &&
8161c7c3c6aSMatthew Dillon 		    (tobject->flags & OBJ_ONEMAPPING)
8171c7c3c6aSMatthew Dillon 		) {
8181c7c3c6aSMatthew Dillon 			if (tobject->type == OBJT_SWAP) {
8191c7c3c6aSMatthew Dillon 				swap_pager_freespace(tobject, tpindex, count);
8201c7c3c6aSMatthew Dillon 			}
8211c7c3c6aSMatthew Dillon 			if ((tobject = tobject->backing_object) == NULL)
8221c7c3c6aSMatthew Dillon 				break;
8231c7c3c6aSMatthew Dillon 			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
8241c7c3c6aSMatthew Dillon 		}
8251c7c3c6aSMatthew Dillon 	}
8261c7c3c6aSMatthew Dillon 
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:
8361c7c3c6aSMatthew Dillon 
8371c7c3c6aSMatthew Dillon 		if (tobject->type != OBJT_DEFAULT &&
8381c7c3c6aSMatthew Dillon 		    tobject->type != OBJT_SWAP
8391c7c3c6aSMatthew Dillon 		) {
8406e20a165SJohn Dyson 			continue;
8416e20a165SJohn Dyson 		}
8426e20a165SJohn Dyson 
8431c7c3c6aSMatthew Dillon 		if ((tobject->flags & OBJ_ONEMAPPING) == 0)
8441c7c3c6aSMatthew Dillon 			continue;
8451c7c3c6aSMatthew Dillon 
8461c7c3c6aSMatthew Dillon 		m = vm_page_lookup(tobject, tpindex);
8471c7c3c6aSMatthew Dillon 
8481c7c3c6aSMatthew Dillon 		if (m == NULL) {
8496e20a165SJohn Dyson 			tobject = tobject->backing_object;
8501c7c3c6aSMatthew Dillon 			if (tobject == NULL)
8511c7c3c6aSMatthew Dillon 				continue;
8521c7c3c6aSMatthew Dillon #if 0
8536e20a165SJohn Dyson 			if ((tobject == NULL) || (tobject->ref_count != 1)) {
8546e20a165SJohn Dyson 				continue;
8556e20a165SJohn Dyson 			}
8561c7c3c6aSMatthew Dillon #endif
8576e20a165SJohn Dyson 			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
8586e20a165SJohn Dyson 			goto shadowlookup;
8596e20a165SJohn Dyson 		}
860867a482dSJohn Dyson 
861867a482dSJohn Dyson 		/*
862867a482dSJohn Dyson 		 * If the page is busy or not in a normal active state,
863867a482dSJohn Dyson 		 * we skip it.  Things can break if we mess with pages
864867a482dSJohn Dyson 		 * in any of the below states.
865867a482dSJohn Dyson 		 */
8661c7c3c6aSMatthew Dillon 		if (
8671c7c3c6aSMatthew Dillon 		    m->hold_count ||
8681c7c3c6aSMatthew Dillon 		    m->wire_count ||
8691c7c3c6aSMatthew Dillon 		    m->valid != VM_PAGE_BITS_ALL
8701c7c3c6aSMatthew Dillon 		) {
871867a482dSJohn Dyson 			continue;
8726e20a165SJohn Dyson 		}
8736e20a165SJohn Dyson 
8741c7c3c6aSMatthew Dillon  		if (vm_page_sleep_busy(m, TRUE, "madvpo"))
8756e20a165SJohn Dyson   			goto relookup;
876867a482dSJohn Dyson 
877867a482dSJohn Dyson 		if (advise == MADV_WILLNEED) {
878867a482dSJohn Dyson 			vm_page_activate(m);
8796e20a165SJohn Dyson 		} else if (advise == MADV_DONTNEED) {
880867a482dSJohn Dyson 			vm_page_deactivate(m);
8810a47b48bSJohn Dyson 		} else if (advise == MADV_FREE) {
8821c7c3c6aSMatthew Dillon 			/*
8831c7c3c6aSMatthew Dillon 			 * If MADV_FREE_FORCE_FREE is defined, we attempt to
8841c7c3c6aSMatthew Dillon 			 * immediately free the page.  Otherwise we just
8851c7c3c6aSMatthew Dillon 			 * destroy any swap backing store, mark it clean,
8861c7c3c6aSMatthew Dillon 			 * and stuff it into the cache.
8871c7c3c6aSMatthew Dillon 			 */
8886e20a165SJohn Dyson 			pmap_clear_modify(VM_PAGE_TO_PHYS(m));
8896e20a165SJohn Dyson 			m->dirty = 0;
8901c7c3c6aSMatthew Dillon 
8911c7c3c6aSMatthew Dillon #ifdef MADV_FREE_FORCE_FREE
8921c7c3c6aSMatthew Dillon 			if (tobject->resident_page_count > 1) {
8931c7c3c6aSMatthew Dillon 				vm_page_busy(m);
8941c7c3c6aSMatthew Dillon 				vm_page_protect(m, VM_PROT_NONE);
8951c7c3c6aSMatthew Dillon 				vm_page_free(m);
8961c7c3c6aSMatthew Dillon 			} else
8971c7c3c6aSMatthew Dillon #endif
8981c7c3c6aSMatthew Dillon 			{
8991c7c3c6aSMatthew Dillon 				vm_page_cache(m);
9001c7c3c6aSMatthew Dillon 			}
901867a482dSJohn Dyson 		}
902867a482dSJohn Dyson 	}
903867a482dSJohn Dyson }
904867a482dSJohn Dyson 
905867a482dSJohn Dyson /*
906df8bae1dSRodney W. Grimes  *	vm_object_shadow:
907df8bae1dSRodney W. Grimes  *
908df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
909df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
910df8bae1dSRodney W. Grimes  *	object reference is deallocated.
911df8bae1dSRodney W. Grimes  *
912df8bae1dSRodney W. Grimes  *	The new object and offset into that object
913df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
914df8bae1dSRodney W. Grimes  */
915df8bae1dSRodney W. Grimes 
91626f9a767SRodney W. Grimes void
91726f9a767SRodney W. Grimes vm_object_shadow(object, offset, length)
918df8bae1dSRodney W. Grimes 	vm_object_t *object;	/* IN/OUT */
919a316d390SJohn Dyson 	vm_ooffset_t *offset;	/* IN/OUT */
920df8bae1dSRodney W. Grimes 	vm_size_t length;
921df8bae1dSRodney W. Grimes {
922df8bae1dSRodney W. Grimes 	register vm_object_t source;
923df8bae1dSRodney W. Grimes 	register vm_object_t result;
924df8bae1dSRodney W. Grimes 
925df8bae1dSRodney W. Grimes 	source = *object;
926df8bae1dSRodney W. Grimes 
927df8bae1dSRodney W. Grimes 	/*
928df8bae1dSRodney W. Grimes 	 * Allocate a new object with the given length
929df8bae1dSRodney W. Grimes 	 */
930df8bae1dSRodney W. Grimes 
93124a1cce3SDavid Greenman 	if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL)
932df8bae1dSRodney W. Grimes 		panic("vm_object_shadow: no object for shadowing");
933df8bae1dSRodney W. Grimes 
934df8bae1dSRodney W. Grimes 	/*
9350d94caffSDavid Greenman 	 * The new object shadows the source object, adding a reference to it.
9360d94caffSDavid Greenman 	 * Our caller changes his reference to point to the new object,
9370d94caffSDavid Greenman 	 * removing a reference to the source object.  Net result: no change
9380d94caffSDavid Greenman 	 * of reference count.
939df8bae1dSRodney W. Grimes 	 */
94024a1cce3SDavid Greenman 	result->backing_object = source;
941de5f6a77SJohn Dyson 	if (source) {
942de5f6a77SJohn Dyson 		TAILQ_INSERT_TAIL(&source->shadow_head, result, shadow_list);
943069e9bc1SDoug Rabson 		vm_object_clear_flag(source, OBJ_ONEMAPPING);
944eaf13dd7SJohn Dyson 		source->shadow_count++;
945eaf13dd7SJohn Dyson 		source->generation++;
946de5f6a77SJohn Dyson 	}
947df8bae1dSRodney W. Grimes 
948df8bae1dSRodney W. Grimes 	/*
9490d94caffSDavid Greenman 	 * Store the offset into the source object, and fix up the offset into
9500d94caffSDavid Greenman 	 * the new object.
951df8bae1dSRodney W. Grimes 	 */
952df8bae1dSRodney W. Grimes 
95324a1cce3SDavid Greenman 	result->backing_object_offset = *offset;
954df8bae1dSRodney W. Grimes 
955df8bae1dSRodney W. Grimes 	/*
956df8bae1dSRodney W. Grimes 	 * Return the new things
957df8bae1dSRodney W. Grimes 	 */
958df8bae1dSRodney W. Grimes 
959df8bae1dSRodney W. Grimes 	*offset = 0;
960df8bae1dSRodney W. Grimes 	*object = result;
961df8bae1dSRodney W. Grimes }
962df8bae1dSRodney W. Grimes 
963df8bae1dSRodney W. Grimes 
964df8bae1dSRodney W. Grimes /*
9652fe6e4d7SDavid Greenman  * this version of collapse allows the operation to occur earlier and
9662fe6e4d7SDavid Greenman  * when paging_in_progress is true for an object...  This is not a complete
9672fe6e4d7SDavid Greenman  * operation, but should plug 99.9% of the rest of the leaks.
9682fe6e4d7SDavid Greenman  */
9692fe6e4d7SDavid Greenman static void
9702fe6e4d7SDavid Greenman vm_object_qcollapse(object)
9712fe6e4d7SDavid Greenman 	register vm_object_t object;
9722fe6e4d7SDavid Greenman {
9732fe6e4d7SDavid Greenman 	register vm_object_t backing_object;
9741c7c3c6aSMatthew Dillon 	register vm_pindex_t backing_offset_index;
975a316d390SJohn Dyson 	vm_pindex_t new_pindex;
9762fe6e4d7SDavid Greenman 	register vm_page_t p, pp;
9772fe6e4d7SDavid Greenman 	register vm_size_t size;
9782fe6e4d7SDavid Greenman 
97924a1cce3SDavid Greenman 	backing_object = object->backing_object;
9802fe6e4d7SDavid Greenman 	if (backing_object->ref_count != 1)
9812fe6e4d7SDavid Greenman 		return;
9822fe6e4d7SDavid Greenman 
983010cf3b9SDavid Greenman 	backing_object->ref_count += 2;
984010cf3b9SDavid Greenman 
985a316d390SJohn Dyson 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
9862fe6e4d7SDavid Greenman 	size = object->size;
9871c7c3c6aSMatthew Dillon 
988b18bfc3dSJohn Dyson 	p = TAILQ_FIRST(&backing_object->memq);
9892fe6e4d7SDavid Greenman 	while (p) {
9902fe6e4d7SDavid Greenman 		vm_page_t next;
9910d94caffSDavid Greenman 
9921c7c3c6aSMatthew Dillon 		/*
9931c7c3c6aSMatthew Dillon 		 * setup for loop.
9941c7c3c6aSMatthew Dillon 		 * loop if the page isn't trivial.
9951c7c3c6aSMatthew Dillon 		 */
9961c7c3c6aSMatthew Dillon 
997b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
998bd7e5f99SJohn Dyson 		if ((p->flags & (PG_BUSY | PG_FICTITIOUS)) ||
9995070c7f8SJohn Dyson 		    !p->valid || p->hold_count || p->wire_count || p->busy) {
10002fe6e4d7SDavid Greenman 			p = next;
10012fe6e4d7SDavid Greenman 			continue;
10022fe6e4d7SDavid Greenman 		}
10031c7c3c6aSMatthew Dillon 
10041c7c3c6aSMatthew Dillon 		/*
10051c7c3c6aSMatthew Dillon 		 * busy the page and move it from the backing store to the
10061c7c3c6aSMatthew Dillon 		 * parent object.
10071c7c3c6aSMatthew Dillon 		 */
10081c7c3c6aSMatthew Dillon 
1009e69763a3SDoug Rabson 		vm_page_busy(p);
1010eaf13dd7SJohn Dyson 
10111c7c3c6aSMatthew Dillon 		KASSERT(p->object == object, ("vm_object_qcollapse(): object mismatch"));
10121c7c3c6aSMatthew Dillon 
1013a316d390SJohn Dyson 		new_pindex = p->pindex - backing_offset_index;
1014a316d390SJohn Dyson 		if (p->pindex < backing_offset_index ||
1015a316d390SJohn Dyson 		    new_pindex >= size) {
101624a1cce3SDavid Greenman 			if (backing_object->type == OBJT_SWAP)
101724a1cce3SDavid Greenman 				swap_pager_freespace(backing_object,
10181c7c3c6aSMatthew Dillon 				    p->pindex,
1019a316d390SJohn Dyson 				    1);
1020b18bfc3dSJohn Dyson 			vm_page_protect(p, VM_PROT_NONE);
10212fe6e4d7SDavid Greenman 			vm_page_free(p);
10222fe6e4d7SDavid Greenman 		} else {
1023a316d390SJohn Dyson 			pp = vm_page_lookup(object, new_pindex);
1024eaf13dd7SJohn Dyson 			if (pp != NULL ||
1025eaf13dd7SJohn Dyson 				(object->type == OBJT_SWAP && vm_pager_has_page(object,
10261c7c3c6aSMatthew Dillon 				    new_pindex, NULL, NULL))) {
102724a1cce3SDavid Greenman 				if (backing_object->type == OBJT_SWAP)
102824a1cce3SDavid Greenman 					swap_pager_freespace(backing_object,
10291c7c3c6aSMatthew Dillon 					    p->pindex, 1);
1030b18bfc3dSJohn Dyson 				vm_page_protect(p, VM_PROT_NONE);
10312fe6e4d7SDavid Greenman 				vm_page_free(p);
10322fe6e4d7SDavid Greenman 			} else {
103324a1cce3SDavid Greenman 				if (backing_object->type == OBJT_SWAP)
103424a1cce3SDavid Greenman 					swap_pager_freespace(backing_object,
10351c7c3c6aSMatthew Dillon 					    p->pindex, 1);
1036eaf13dd7SJohn Dyson 
1037edd97f3aSJohn Dyson 				if ((p->queue - p->pc) == PQ_CACHE)
1038edd97f3aSJohn Dyson 					vm_page_deactivate(p);
1039edd97f3aSJohn Dyson 				else
10406e20a165SJohn Dyson 					vm_page_protect(p, VM_PROT_NONE);
1041edd97f3aSJohn Dyson 
1042edd97f3aSJohn Dyson 				vm_page_rename(p, object, new_pindex);
10431c7c3c6aSMatthew Dillon 				/* page automatically made dirty by rename */
10442fe6e4d7SDavid Greenman 			}
10452fe6e4d7SDavid Greenman 		}
10462fe6e4d7SDavid Greenman 		p = next;
10472fe6e4d7SDavid Greenman 	}
1048010cf3b9SDavid Greenman 	backing_object->ref_count -= 2;
10492fe6e4d7SDavid Greenman }
10502fe6e4d7SDavid Greenman 
1051df8bae1dSRodney W. Grimes /*
1052df8bae1dSRodney W. Grimes  *	vm_object_collapse:
1053df8bae1dSRodney W. Grimes  *
1054df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
1055df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
1056df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
1057df8bae1dSRodney W. Grimes  */
105826f9a767SRodney W. Grimes void
105926f9a767SRodney W. Grimes vm_object_collapse(object)
106024a1cce3SDavid Greenman 	vm_object_t object;
1061df8bae1dSRodney W. Grimes 
1062df8bae1dSRodney W. Grimes {
106324a1cce3SDavid Greenman 	vm_object_t backing_object;
1064a316d390SJohn Dyson 	vm_ooffset_t backing_offset;
106524a1cce3SDavid Greenman 	vm_size_t size;
1066a316d390SJohn Dyson 	vm_pindex_t new_pindex, backing_offset_index;
106724a1cce3SDavid Greenman 	vm_page_t p, pp;
1068df8bae1dSRodney W. Grimes 
1069df8bae1dSRodney W. Grimes 	while (TRUE) {
1070df8bae1dSRodney W. Grimes 		/*
1071df8bae1dSRodney W. Grimes 		 * Verify that the conditions are right for collapse:
1072df8bae1dSRodney W. Grimes 		 *
10730d94caffSDavid Greenman 		 * The object exists and no pages in it are currently being paged
10740d94caffSDavid Greenman 		 * out.
1075df8bae1dSRodney W. Grimes 		 */
10762fe6e4d7SDavid Greenman 		if (object == NULL)
1077df8bae1dSRodney W. Grimes 			return;
1078df8bae1dSRodney W. Grimes 
1079b9921222SDavid Greenman 		/*
1080b9921222SDavid Greenman 		 * Make sure there is a backing object.
1081b9921222SDavid Greenman 		 */
108224a1cce3SDavid Greenman 		if ((backing_object = object->backing_object) == NULL)
1083df8bae1dSRodney W. Grimes 			return;
1084df8bae1dSRodney W. Grimes 
1085f919ebdeSDavid Greenman 		/*
1086f919ebdeSDavid Greenman 		 * we check the backing object first, because it is most likely
108724a1cce3SDavid Greenman 		 * not collapsable.
1088f919ebdeSDavid Greenman 		 */
108924a1cce3SDavid Greenman 		if (backing_object->handle != NULL ||
109024a1cce3SDavid Greenman 		    (backing_object->type != OBJT_DEFAULT &&
109124a1cce3SDavid Greenman 		     backing_object->type != OBJT_SWAP) ||
1092f919ebdeSDavid Greenman 		    (backing_object->flags & OBJ_DEAD) ||
109324a1cce3SDavid Greenman 		    object->handle != NULL ||
109424a1cce3SDavid Greenman 		    (object->type != OBJT_DEFAULT &&
109524a1cce3SDavid Greenman 		     object->type != OBJT_SWAP) ||
109624a1cce3SDavid Greenman 		    (object->flags & OBJ_DEAD)) {
10979b4814bbSDavid Greenman 			return;
109824a1cce3SDavid Greenman 		}
10999b4814bbSDavid Greenman 
1100f919ebdeSDavid Greenman 		if (object->paging_in_progress != 0 ||
1101f919ebdeSDavid Greenman 		    backing_object->paging_in_progress != 0) {
1102b9921222SDavid Greenman 			vm_object_qcollapse(object);
1103df8bae1dSRodney W. Grimes 			return;
1104df8bae1dSRodney W. Grimes 		}
1105f919ebdeSDavid Greenman 
110626f9a767SRodney W. Grimes 		/*
11070d94caffSDavid Greenman 		 * We know that we can either collapse the backing object (if
11080d94caffSDavid Greenman 		 * the parent is the only reference to it) or (perhaps) remove
11090d94caffSDavid Greenman 		 * the parent's reference to it.
1110df8bae1dSRodney W. Grimes 		 */
1111df8bae1dSRodney W. Grimes 
111224a1cce3SDavid Greenman 		backing_offset = object->backing_object_offset;
1113a316d390SJohn Dyson 		backing_offset_index = OFF_TO_IDX(backing_offset);
1114df8bae1dSRodney W. Grimes 		size = object->size;
1115df8bae1dSRodney W. Grimes 
1116df8bae1dSRodney W. Grimes 		/*
11170d94caffSDavid Greenman 		 * If there is exactly one reference to the backing object, we
11180d94caffSDavid Greenman 		 * can collapse it into the parent.
1119df8bae1dSRodney W. Grimes 		 */
1120df8bae1dSRodney W. Grimes 
1121df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
1122df8bae1dSRodney W. Grimes 
1123069e9bc1SDoug Rabson 			vm_object_set_flag(backing_object, OBJ_DEAD);
1124df8bae1dSRodney W. Grimes 			/*
1125df8bae1dSRodney W. Grimes 			 * We can collapse the backing object.
1126df8bae1dSRodney W. Grimes 			 *
11270d94caffSDavid Greenman 			 * Move all in-memory pages from backing_object to the
11280d94caffSDavid Greenman 			 * parent.  Pages that have been paged out will be
11290d94caffSDavid Greenman 			 * overwritten by any of the parent's pages that
11300d94caffSDavid Greenman 			 * shadow them.
1131df8bae1dSRodney W. Grimes 			 */
1132df8bae1dSRodney W. Grimes 
1133b18bfc3dSJohn Dyson 			while ((p = TAILQ_FIRST(&backing_object->memq)) != 0) {
11341c7c3c6aSMatthew Dillon 				if (vm_page_sleep_busy(p, TRUE, "vmocol"))
11351c7c3c6aSMatthew Dillon 					continue;
1136e69763a3SDoug Rabson 				vm_page_busy(p);
11371c7c3c6aSMatthew Dillon 				new_pindex = p->pindex - backing_offset_index;
1138df8bae1dSRodney W. Grimes 
1139df8bae1dSRodney W. Grimes 				/*
11400d94caffSDavid Greenman 				 * If the parent has a page here, or if this
11410d94caffSDavid Greenman 				 * page falls outside the parent, dispose of
11420d94caffSDavid Greenman 				 * it.
1143df8bae1dSRodney W. Grimes 				 *
1144df8bae1dSRodney W. Grimes 				 * Otherwise, move it as planned.
1145df8bae1dSRodney W. Grimes 				 */
1146df8bae1dSRodney W. Grimes 
1147a316d390SJohn Dyson 				if (p->pindex < backing_offset_index ||
1148a316d390SJohn Dyson 				    new_pindex >= size) {
1149f919ebdeSDavid Greenman 					vm_page_protect(p, VM_PROT_NONE);
1150df8bae1dSRodney W. Grimes 					vm_page_free(p);
1151df8bae1dSRodney W. Grimes 				} else {
1152a316d390SJohn Dyson 					pp = vm_page_lookup(object, new_pindex);
115324a1cce3SDavid Greenman 					if (pp != NULL || (object->type == OBJT_SWAP && vm_pager_has_page(object,
11541c7c3c6aSMatthew Dillon 					    new_pindex, NULL, NULL))) {
1155f919ebdeSDavid Greenman 						vm_page_protect(p, VM_PROT_NONE);
1156df8bae1dSRodney W. Grimes 						vm_page_free(p);
115726f9a767SRodney W. Grimes 					} else {
1158edd97f3aSJohn Dyson 						if ((p->queue - p->pc) == PQ_CACHE)
1159edd97f3aSJohn Dyson 							vm_page_deactivate(p);
1160edd97f3aSJohn Dyson 						else
11616e20a165SJohn Dyson 							vm_page_protect(p, VM_PROT_NONE);
1162a316d390SJohn Dyson 						vm_page_rename(p, object, new_pindex);
11631c7c3c6aSMatthew Dillon 						/* page automatically made dirty by rename */
1164df8bae1dSRodney W. Grimes 					}
1165df8bae1dSRodney W. Grimes 				}
1166df8bae1dSRodney W. Grimes 			}
1167df8bae1dSRodney W. Grimes 
1168df8bae1dSRodney W. Grimes 			/*
1169df8bae1dSRodney W. Grimes 			 * Move the pager from backing_object to object.
1170df8bae1dSRodney W. Grimes 			 */
1171df8bae1dSRodney W. Grimes 
117224a1cce3SDavid Greenman 			if (backing_object->type == OBJT_SWAP) {
1173d474eaaaSDoug Rabson 				vm_object_pip_add(backing_object, 1);
117424a1cce3SDavid Greenman 
117524a1cce3SDavid Greenman 				/*
11761c7c3c6aSMatthew Dillon 				 * scrap the paging_offset junk and do a
11771c7c3c6aSMatthew Dillon 				 * discrete copy.  This also removes major
11781c7c3c6aSMatthew Dillon 				 * assumptions about how the swap-pager
11791c7c3c6aSMatthew Dillon 				 * works from where it doesn't belong.  The
11801c7c3c6aSMatthew Dillon 				 * new swapper is able to optimize the
11811c7c3c6aSMatthew Dillon 				 * destroy-source case.
118224a1cce3SDavid Greenman 				 */
11831c7c3c6aSMatthew Dillon 
11841c7c3c6aSMatthew Dillon 				vm_object_pip_add(object, 1);
11851c7c3c6aSMatthew Dillon 				swap_pager_copy(
11861c7c3c6aSMatthew Dillon 				    backing_object,
11871c7c3c6aSMatthew Dillon 				    object,
11881c7c3c6aSMatthew Dillon 				    OFF_TO_IDX(object->backing_object_offset), TRUE);
1189f919ebdeSDavid Greenman 				vm_object_pip_wakeup(object);
1190c0503609SDavid Greenman 
1191f919ebdeSDavid Greenman 				vm_object_pip_wakeup(backing_object);
1192c0503609SDavid Greenman 			}
1193df8bae1dSRodney W. Grimes 			/*
1194df8bae1dSRodney W. Grimes 			 * Object now shadows whatever backing_object did.
119524a1cce3SDavid Greenman 			 * Note that the reference to backing_object->backing_object
1196df8bae1dSRodney W. Grimes 			 * moves from within backing_object to within object.
1197df8bae1dSRodney W. Grimes 			 */
1198df8bae1dSRodney W. Grimes 
119924a1cce3SDavid Greenman 			TAILQ_REMOVE(&object->backing_object->shadow_head, object,
120024a1cce3SDavid Greenman 			    shadow_list);
1201eaf13dd7SJohn Dyson 			object->backing_object->shadow_count--;
1202eaf13dd7SJohn Dyson 			object->backing_object->generation++;
1203de5f6a77SJohn Dyson 			if (backing_object->backing_object) {
120424a1cce3SDavid Greenman 				TAILQ_REMOVE(&backing_object->backing_object->shadow_head,
120524a1cce3SDavid Greenman 				    backing_object, shadow_list);
1206eaf13dd7SJohn Dyson 				backing_object->backing_object->shadow_count--;
1207eaf13dd7SJohn Dyson 				backing_object->backing_object->generation++;
1208de5f6a77SJohn Dyson 			}
120924a1cce3SDavid Greenman 			object->backing_object = backing_object->backing_object;
1210de5f6a77SJohn Dyson 			if (object->backing_object) {
121124a1cce3SDavid Greenman 				TAILQ_INSERT_TAIL(&object->backing_object->shadow_head,
121224a1cce3SDavid Greenman 				    object, shadow_list);
1213eaf13dd7SJohn Dyson 				object->backing_object->shadow_count++;
1214eaf13dd7SJohn Dyson 				object->backing_object->generation++;
1215de5f6a77SJohn Dyson 			}
12162fe6e4d7SDavid Greenman 
121724a1cce3SDavid Greenman 			object->backing_object_offset += backing_object->backing_object_offset;
1218df8bae1dSRodney W. Grimes 			/*
1219df8bae1dSRodney W. Grimes 			 * Discard backing_object.
1220df8bae1dSRodney W. Grimes 			 *
12210d94caffSDavid Greenman 			 * Since the backing object has no pages, no pager left,
12220d94caffSDavid Greenman 			 * and no object references within it, all that is
12230d94caffSDavid Greenman 			 * necessary is to dispose of it.
1224df8bae1dSRodney W. Grimes 			 */
1225df8bae1dSRodney W. Grimes 
1226df8bae1dSRodney W. Grimes 			TAILQ_REMOVE(&vm_object_list, backing_object,
1227df8bae1dSRodney W. Grimes 			    object_list);
1228df8bae1dSRodney W. Grimes 			vm_object_count--;
1229df8bae1dSRodney W. Grimes 
123099448ed1SJohn Dyson 			zfree(obj_zone, backing_object);
1231df8bae1dSRodney W. Grimes 
1232df8bae1dSRodney W. Grimes 			object_collapses++;
12330d94caffSDavid Greenman 		} else {
123495e5e988SJohn Dyson 			vm_object_t new_backing_object;
1235df8bae1dSRodney W. Grimes 			/*
1236df8bae1dSRodney W. Grimes 			 * If all of the pages in the backing object are
12370d94caffSDavid Greenman 			 * shadowed by the parent object, the parent object no
12380d94caffSDavid Greenman 			 * longer has to shadow the backing object; it can
12390d94caffSDavid Greenman 			 * shadow the next one in the chain.
1240df8bae1dSRodney W. Grimes 			 *
12410d94caffSDavid Greenman 			 * The backing object must not be paged out - we'd have
12420d94caffSDavid Greenman 			 * to check all of the paged-out pages, as well.
1243df8bae1dSRodney W. Grimes 			 */
1244df8bae1dSRodney W. Grimes 
124524a1cce3SDavid Greenman 			if (backing_object->type != OBJT_DEFAULT) {
1246df8bae1dSRodney W. Grimes 				return;
1247df8bae1dSRodney W. Grimes 			}
1248df8bae1dSRodney W. Grimes 			/*
12490d94caffSDavid Greenman 			 * Should have a check for a 'small' number of pages
12500d94caffSDavid Greenman 			 * here.
1251df8bae1dSRodney W. Grimes 			 */
1252df8bae1dSRodney W. Grimes 
1253eaf13dd7SJohn Dyson 			for (p = TAILQ_FIRST(&backing_object->memq); p;
1254eaf13dd7SJohn Dyson 					p = TAILQ_NEXT(p, listq)) {
1255eaf13dd7SJohn Dyson 
1256a316d390SJohn Dyson 				new_pindex = p->pindex - backing_offset_index;
1257e69763a3SDoug Rabson 				vm_page_busy(p);
1258df8bae1dSRodney W. Grimes 
1259df8bae1dSRodney W. Grimes 				/*
12600d94caffSDavid Greenman 				 * If the parent has a page here, or if this
12610d94caffSDavid Greenman 				 * page falls outside the parent, keep going.
1262df8bae1dSRodney W. Grimes 				 *
12630d94caffSDavid Greenman 				 * Otherwise, the backing_object must be left in
12640d94caffSDavid Greenman 				 * the chain.
1265df8bae1dSRodney W. Grimes 				 */
1266df8bae1dSRodney W. Grimes 
1267a316d390SJohn Dyson 				if (p->pindex >= backing_offset_index &&
1268a316d390SJohn Dyson 					new_pindex <= size) {
126924a1cce3SDavid Greenman 
1270a316d390SJohn Dyson 					pp = vm_page_lookup(object, new_pindex);
127124a1cce3SDavid Greenman 
1272eaf13dd7SJohn Dyson 					if ((pp == NULL) || (pp->flags & PG_BUSY) || pp->busy) {
1273e69763a3SDoug Rabson 						vm_page_wakeup(p);
1274eaf13dd7SJohn Dyson 						return;
1275eaf13dd7SJohn Dyson 					}
1276eaf13dd7SJohn Dyson 
1277e69763a3SDoug Rabson 					vm_page_busy(pp);
1278eaf13dd7SJohn Dyson 					if ((pp->valid == 0) &&
12791c7c3c6aSMatthew Dillon 				   	    !vm_pager_has_page(object, new_pindex, NULL, NULL)) {
1280df8bae1dSRodney W. Grimes 						/*
12810d94caffSDavid Greenman 						 * Page still needed. Can't go any
12820d94caffSDavid Greenman 						 * further.
1283df8bae1dSRodney W. Grimes 						 */
1284e69763a3SDoug Rabson 						vm_page_wakeup(pp);
1285e69763a3SDoug Rabson 						vm_page_wakeup(p);
1286df8bae1dSRodney W. Grimes 						return;
1287df8bae1dSRodney W. Grimes 					}
1288e69763a3SDoug Rabson 					vm_page_wakeup(pp);
1289df8bae1dSRodney W. Grimes 				}
1290e69763a3SDoug Rabson 				vm_page_wakeup(p);
129124a1cce3SDavid Greenman 			}
1292df8bae1dSRodney W. Grimes 
1293df8bae1dSRodney W. Grimes 			/*
12940d94caffSDavid Greenman 			 * Make the parent shadow the next object in the
12950d94caffSDavid Greenman 			 * chain.  Deallocating backing_object will not remove
12960d94caffSDavid Greenman 			 * it, since its reference count is at least 2.
1297df8bae1dSRodney W. Grimes 			 */
1298df8bae1dSRodney W. Grimes 
129995e5e988SJohn Dyson 			TAILQ_REMOVE(&backing_object->shadow_head,
130024a1cce3SDavid Greenman 			    object, shadow_list);
1301eaf13dd7SJohn Dyson 			backing_object->shadow_count--;
1302eaf13dd7SJohn Dyson 			backing_object->generation++;
130395e5e988SJohn Dyson 
130495e5e988SJohn Dyson 			new_backing_object = backing_object->backing_object;
13058aef1712SMatthew Dillon 			if ((object->backing_object = new_backing_object) != NULL) {
130695e5e988SJohn Dyson 				vm_object_reference(new_backing_object);
130795e5e988SJohn Dyson 				TAILQ_INSERT_TAIL(&new_backing_object->shadow_head,
130824a1cce3SDavid Greenman 				    object, shadow_list);
1309eaf13dd7SJohn Dyson 				new_backing_object->shadow_count++;
1310eaf13dd7SJohn Dyson 				new_backing_object->generation++;
131195e5e988SJohn Dyson 				object->backing_object_offset +=
131295e5e988SJohn Dyson 					backing_object->backing_object_offset;
1313de5f6a77SJohn Dyson 			}
1314df8bae1dSRodney W. Grimes 
1315df8bae1dSRodney W. Grimes 			/*
13160d94caffSDavid Greenman 			 * Drop the reference count on backing_object. Since
13170d94caffSDavid Greenman 			 * its ref_count was at least 2, it will not vanish;
1318eaf13dd7SJohn Dyson 			 * so we don't need to call vm_object_deallocate, but
1319eaf13dd7SJohn Dyson 			 * we do anyway.
1320df8bae1dSRodney W. Grimes 			 */
132195e5e988SJohn Dyson 			vm_object_deallocate(backing_object);
1322df8bae1dSRodney W. Grimes 			object_bypasses++;
1323df8bae1dSRodney W. Grimes 		}
1324df8bae1dSRodney W. Grimes 
1325df8bae1dSRodney W. Grimes 		/*
1326df8bae1dSRodney W. Grimes 		 * Try again with this object's new backing object.
1327df8bae1dSRodney W. Grimes 		 */
1328df8bae1dSRodney W. Grimes 	}
1329df8bae1dSRodney W. Grimes }
1330df8bae1dSRodney W. Grimes 
1331df8bae1dSRodney W. Grimes /*
1332df8bae1dSRodney W. Grimes  *	vm_object_page_remove: [internal]
1333df8bae1dSRodney W. Grimes  *
1334df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
1335df8bae1dSRodney W. Grimes  *	object range from the object's list of pages.
1336df8bae1dSRodney W. Grimes  *
1337df8bae1dSRodney W. Grimes  *	The object must be locked.
1338df8bae1dSRodney W. Grimes  */
133926f9a767SRodney W. Grimes void
13407c1f6cedSDavid Greenman vm_object_page_remove(object, start, end, clean_only)
1341df8bae1dSRodney W. Grimes 	register vm_object_t object;
1342a316d390SJohn Dyson 	register vm_pindex_t start;
1343a316d390SJohn Dyson 	register vm_pindex_t end;
13447c1f6cedSDavid Greenman 	boolean_t clean_only;
1345df8bae1dSRodney W. Grimes {
1346df8bae1dSRodney W. Grimes 	register vm_page_t p, next;
1347a316d390SJohn Dyson 	unsigned int size;
1348f5ef029eSPoul-Henning Kamp 	int all;
1349df8bae1dSRodney W. Grimes 
1350df8bae1dSRodney W. Grimes 	if (object == NULL)
1351df8bae1dSRodney W. Grimes 		return;
1352df8bae1dSRodney W. Grimes 
135395e5e988SJohn Dyson 	all = ((end == 0) && (start == 0));
135495e5e988SJohn Dyson 
1355d474eaaaSDoug Rabson 	vm_object_pip_add(object, 1);
135626f9a767SRodney W. Grimes again:
135726f9a767SRodney W. Grimes 	size = end - start;
135895e5e988SJohn Dyson 	if (all || size > 4 || size >= object->size / 4) {
1359b18bfc3dSJohn Dyson 		for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
1360b18bfc3dSJohn Dyson 			next = TAILQ_NEXT(p, listq);
136195e5e988SJohn Dyson 			if (all || ((start <= p->pindex) && (p->pindex < end))) {
1362bd7e5f99SJohn Dyson 				if (p->wire_count != 0) {
1363bd7e5f99SJohn Dyson 					vm_page_protect(p, VM_PROT_NONE);
1364ce65e68cSDavid Greenman 					if (!clean_only)
1365bd7e5f99SJohn Dyson 						p->valid = 0;
13660d94caffSDavid Greenman 					continue;
13670d94caffSDavid Greenman 				}
13680891ef4cSJohn Dyson 
1369b18bfc3dSJohn Dyson 				/*
1370b18bfc3dSJohn Dyson 				 * The busy flags are only cleared at
1371b18bfc3dSJohn Dyson 				 * interrupt -- minimize the spl transitions
1372b18bfc3dSJohn Dyson 				 */
1373ffc82b0aSJohn Dyson 
13741c7c3c6aSMatthew Dillon  				if (vm_page_sleep_busy(p, TRUE, "vmopar"))
137526f9a767SRodney W. Grimes  					goto again;
13760891ef4cSJohn Dyson 
13778f9110f6SJohn Dyson 				if (clean_only && p->valid) {
13787c1f6cedSDavid Greenman 					vm_page_test_dirty(p);
13797c1f6cedSDavid Greenman 					if (p->valid & p->dirty)
13807c1f6cedSDavid Greenman 						continue;
13817c1f6cedSDavid Greenman 				}
1382eaf13dd7SJohn Dyson 
1383e69763a3SDoug Rabson 				vm_page_busy(p);
1384f919ebdeSDavid Greenman 				vm_page_protect(p, VM_PROT_NONE);
1385df8bae1dSRodney W. Grimes 				vm_page_free(p);
138626f9a767SRodney W. Grimes 			}
138726f9a767SRodney W. Grimes 		}
138826f9a767SRodney W. Grimes 	} else {
138926f9a767SRodney W. Grimes 		while (size > 0) {
1390bd7e5f99SJohn Dyson 			if ((p = vm_page_lookup(object, start)) != 0) {
1391eaf13dd7SJohn Dyson 
1392bd7e5f99SJohn Dyson 				if (p->wire_count != 0) {
1393bd7e5f99SJohn Dyson 					vm_page_protect(p, VM_PROT_NONE);
1394ce65e68cSDavid Greenman 					if (!clean_only)
1395ce65e68cSDavid Greenman 						p->valid = 0;
1396bd7e5f99SJohn Dyson 					start += 1;
1397bd7e5f99SJohn Dyson 					size -= 1;
1398bd7e5f99SJohn Dyson 					continue;
13990d94caffSDavid Greenman 				}
1400eaf13dd7SJohn Dyson 
1401b18bfc3dSJohn Dyson 				/*
1402b18bfc3dSJohn Dyson 				 * The busy flags are only cleared at
1403b18bfc3dSJohn Dyson 				 * interrupt -- minimize the spl transitions
1404b18bfc3dSJohn Dyson 				 */
14051c7c3c6aSMatthew Dillon  				if (vm_page_sleep_busy(p, TRUE, "vmopar"))
140626f9a767SRodney W. Grimes 					goto again;
1407eaf13dd7SJohn Dyson 
14088f9110f6SJohn Dyson 				if (clean_only && p->valid) {
14097c1f6cedSDavid Greenman 					vm_page_test_dirty(p);
1410bd7e5f99SJohn Dyson 					if (p->valid & p->dirty) {
1411bd7e5f99SJohn Dyson 						start += 1;
1412bd7e5f99SJohn Dyson 						size -= 1;
14137c1f6cedSDavid Greenman 						continue;
14147c1f6cedSDavid Greenman 					}
1415bd7e5f99SJohn Dyson 				}
1416eaf13dd7SJohn Dyson 
1417e69763a3SDoug Rabson 				vm_page_busy(p);
1418f919ebdeSDavid Greenman 				vm_page_protect(p, VM_PROT_NONE);
141926f9a767SRodney W. Grimes 				vm_page_free(p);
142026f9a767SRodney W. Grimes 			}
1421a316d390SJohn Dyson 			start += 1;
1422a316d390SJohn Dyson 			size -= 1;
1423df8bae1dSRodney W. Grimes 		}
1424df8bae1dSRodney W. Grimes 	}
1425f919ebdeSDavid Greenman 	vm_object_pip_wakeup(object);
1426c0503609SDavid Greenman }
1427df8bae1dSRodney W. Grimes 
1428df8bae1dSRodney W. Grimes /*
1429df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
1430df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
1431df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
1432df8bae1dSRodney W. Grimes  *
1433df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
1434df8bae1dSRodney W. Grimes  *
1435df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
1436df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
1437df8bae1dSRodney W. Grimes  *
1438df8bae1dSRodney W. Grimes  *	Parameters:
1439df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
1440df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
1441df8bae1dSRodney W. Grimes  *		next_object	Second object into coalesce
1442df8bae1dSRodney W. Grimes  *		next_offset	Offset into next_object
1443df8bae1dSRodney W. Grimes  *
1444df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
1445df8bae1dSRodney W. Grimes  *		next_size	Size of reference to next_object
1446df8bae1dSRodney W. Grimes  *
1447df8bae1dSRodney W. Grimes  *	Conditions:
1448df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
1449df8bae1dSRodney W. Grimes  */
14500d94caffSDavid Greenman boolean_t
1451a316d390SJohn Dyson vm_object_coalesce(prev_object, prev_pindex, prev_size, next_size)
1452df8bae1dSRodney W. Grimes 	register vm_object_t prev_object;
1453a316d390SJohn Dyson 	vm_pindex_t prev_pindex;
1454df8bae1dSRodney W. Grimes 	vm_size_t prev_size, next_size;
1455df8bae1dSRodney W. Grimes {
1456df8bae1dSRodney W. Grimes 	vm_size_t newsize;
1457df8bae1dSRodney W. Grimes 
1458df8bae1dSRodney W. Grimes 	if (prev_object == NULL) {
1459df8bae1dSRodney W. Grimes 		return (TRUE);
1460df8bae1dSRodney W. Grimes 	}
1461df8bae1dSRodney W. Grimes 
146230dcfc09SJohn Dyson 	if (prev_object->type != OBJT_DEFAULT) {
146330dcfc09SJohn Dyson 		return (FALSE);
146430dcfc09SJohn Dyson 	}
146530dcfc09SJohn Dyson 
1466df8bae1dSRodney W. Grimes 	/*
1467df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
1468df8bae1dSRodney W. Grimes 	 */
1469df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
1470df8bae1dSRodney W. Grimes 
1471df8bae1dSRodney W. Grimes 	/*
14720d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
14730d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
14740d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
1475df8bae1dSRodney W. Grimes 	 */
1476df8bae1dSRodney W. Grimes 
14778cc7e047SJohn Dyson 	if (prev_object->backing_object != NULL) {
1478df8bae1dSRodney W. Grimes 		return (FALSE);
1479df8bae1dSRodney W. Grimes 	}
1480a316d390SJohn Dyson 
1481a316d390SJohn Dyson 	prev_size >>= PAGE_SHIFT;
1482a316d390SJohn Dyson 	next_size >>= PAGE_SHIFT;
14838cc7e047SJohn Dyson 
14848cc7e047SJohn Dyson 	if ((prev_object->ref_count > 1) &&
14858cc7e047SJohn Dyson 	    (prev_object->size != prev_pindex + prev_size)) {
14868cc7e047SJohn Dyson 		return (FALSE);
14878cc7e047SJohn Dyson 	}
14888cc7e047SJohn Dyson 
1489df8bae1dSRodney W. Grimes 	/*
14900d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
14910d94caffSDavid Greenman 	 * deallocation.
1492df8bae1dSRodney W. Grimes 	 */
1493df8bae1dSRodney W. Grimes 
1494df8bae1dSRodney W. Grimes 	vm_object_page_remove(prev_object,
1495a316d390SJohn Dyson 	    prev_pindex + prev_size,
1496a316d390SJohn Dyson 	    prev_pindex + prev_size + next_size, FALSE);
1497df8bae1dSRodney W. Grimes 
1498df8bae1dSRodney W. Grimes 	/*
1499df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
1500df8bae1dSRodney W. Grimes 	 */
1501a316d390SJohn Dyson 	newsize = prev_pindex + prev_size + next_size;
1502df8bae1dSRodney W. Grimes 	if (newsize > prev_object->size)
1503df8bae1dSRodney W. Grimes 		prev_object->size = newsize;
1504df8bae1dSRodney W. Grimes 
1505df8bae1dSRodney W. Grimes 	return (TRUE);
1506df8bae1dSRodney W. Grimes }
1507df8bae1dSRodney W. Grimes 
1508c7c34a24SBruce Evans #include "opt_ddb.h"
1509c3cb3e12SDavid Greenman #ifdef DDB
1510c7c34a24SBruce Evans #include <sys/kernel.h>
1511c7c34a24SBruce Evans 
1512c7c34a24SBruce Evans #include <machine/cons.h>
1513c7c34a24SBruce Evans 
1514c7c34a24SBruce Evans #include <ddb/ddb.h>
1515c7c34a24SBruce Evans 
1516c7c34a24SBruce Evans static int	_vm_object_in_map __P((vm_map_t map, vm_object_t object,
1517c7c34a24SBruce Evans 				       vm_map_entry_t entry));
1518c7c34a24SBruce Evans static int	vm_object_in_map __P((vm_object_t object));
1519c3cb3e12SDavid Greenman 
1520cac597e4SBruce Evans static int
1521a1f6d91cSDavid Greenman _vm_object_in_map(map, object, entry)
1522a1f6d91cSDavid Greenman 	vm_map_t map;
1523a1f6d91cSDavid Greenman 	vm_object_t object;
1524a1f6d91cSDavid Greenman 	vm_map_entry_t entry;
1525a1f6d91cSDavid Greenman {
1526a1f6d91cSDavid Greenman 	vm_map_t tmpm;
1527a1f6d91cSDavid Greenman 	vm_map_entry_t tmpe;
1528a1f6d91cSDavid Greenman 	vm_object_t obj;
1529a1f6d91cSDavid Greenman 	int entcount;
1530a1f6d91cSDavid Greenman 
1531a1f6d91cSDavid Greenman 	if (map == 0)
1532a1f6d91cSDavid Greenman 		return 0;
1533a1f6d91cSDavid Greenman 
1534a1f6d91cSDavid Greenman 	if (entry == 0) {
1535a1f6d91cSDavid Greenman 		tmpe = map->header.next;
1536a1f6d91cSDavid Greenman 		entcount = map->nentries;
1537a1f6d91cSDavid Greenman 		while (entcount-- && (tmpe != &map->header)) {
1538a1f6d91cSDavid Greenman 			if( _vm_object_in_map(map, object, tmpe)) {
1539a1f6d91cSDavid Greenman 				return 1;
1540a1f6d91cSDavid Greenman 			}
1541a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1542a1f6d91cSDavid Greenman 		}
1543afa07f7eSJohn Dyson 	} else if (entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) {
1544a1f6d91cSDavid Greenman 		tmpm = entry->object.share_map;
1545a1f6d91cSDavid Greenman 		tmpe = tmpm->header.next;
1546a1f6d91cSDavid Greenman 		entcount = tmpm->nentries;
1547a1f6d91cSDavid Greenman 		while (entcount-- && tmpe != &tmpm->header) {
1548a1f6d91cSDavid Greenman 			if( _vm_object_in_map(tmpm, object, tmpe)) {
1549a1f6d91cSDavid Greenman 				return 1;
1550a1f6d91cSDavid Greenman 			}
1551a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1552a1f6d91cSDavid Greenman 		}
15538aef1712SMatthew Dillon 	} else if ((obj = entry->object.vm_object) != NULL) {
155424a1cce3SDavid Greenman 		for(; obj; obj=obj->backing_object)
1555a1f6d91cSDavid Greenman 			if( obj == object) {
1556a1f6d91cSDavid Greenman 				return 1;
1557a1f6d91cSDavid Greenman 			}
1558a1f6d91cSDavid Greenman 	}
1559a1f6d91cSDavid Greenman 	return 0;
1560a1f6d91cSDavid Greenman }
1561a1f6d91cSDavid Greenman 
1562cac597e4SBruce Evans static int
1563a1f6d91cSDavid Greenman vm_object_in_map( object)
1564a1f6d91cSDavid Greenman 	vm_object_t object;
1565a1f6d91cSDavid Greenman {
1566a1f6d91cSDavid Greenman 	struct proc *p;
15671b67ec6dSJeffrey Hsu 	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
1568a1f6d91cSDavid Greenman 		if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1569a1f6d91cSDavid Greenman 			continue;
1570a1f6d91cSDavid Greenman 		if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0))
1571a1f6d91cSDavid Greenman 			return 1;
1572a1f6d91cSDavid Greenman 	}
1573a1f6d91cSDavid Greenman 	if( _vm_object_in_map( kernel_map, object, 0))
1574a1f6d91cSDavid Greenman 		return 1;
1575a1f6d91cSDavid Greenman 	if( _vm_object_in_map( kmem_map, object, 0))
1576a1f6d91cSDavid Greenman 		return 1;
1577a1f6d91cSDavid Greenman 	if( _vm_object_in_map( pager_map, object, 0))
1578a1f6d91cSDavid Greenman 		return 1;
1579a1f6d91cSDavid Greenman 	if( _vm_object_in_map( buffer_map, object, 0))
1580a1f6d91cSDavid Greenman 		return 1;
1581a1f6d91cSDavid Greenman 	if( _vm_object_in_map( io_map, object, 0))
1582a1f6d91cSDavid Greenman 		return 1;
1583a1f6d91cSDavid Greenman 	if( _vm_object_in_map( phys_map, object, 0))
1584a1f6d91cSDavid Greenman 		return 1;
1585a1f6d91cSDavid Greenman 	if( _vm_object_in_map( mb_map, object, 0))
1586a1f6d91cSDavid Greenman 		return 1;
1587a1f6d91cSDavid Greenman 	if( _vm_object_in_map( u_map, object, 0))
1588a1f6d91cSDavid Greenman 		return 1;
1589a1f6d91cSDavid Greenman 	return 0;
1590a1f6d91cSDavid Greenman }
1591a1f6d91cSDavid Greenman 
1592c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check)
1593f708ef1bSPoul-Henning Kamp {
1594a1f6d91cSDavid Greenman 	vm_object_t object;
1595a1f6d91cSDavid Greenman 
1596a1f6d91cSDavid Greenman 	/*
1597a1f6d91cSDavid Greenman 	 * make sure that internal objs are in a map somewhere
1598a1f6d91cSDavid Greenman 	 * and none have zero ref counts.
1599a1f6d91cSDavid Greenman 	 */
1600b18bfc3dSJohn Dyson 	for (object = TAILQ_FIRST(&vm_object_list);
1601a1f6d91cSDavid Greenman 			object != NULL;
1602b18bfc3dSJohn Dyson 			object = TAILQ_NEXT(object, object_list)) {
160324a1cce3SDavid Greenman 		if (object->handle == NULL &&
160424a1cce3SDavid Greenman 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
1605a1f6d91cSDavid Greenman 			if (object->ref_count == 0) {
1606c7c34a24SBruce Evans 				db_printf("vmochk: internal obj has zero ref count: %d\n",
1607a1f6d91cSDavid Greenman 					object->size);
1608a1f6d91cSDavid Greenman 			}
1609a1f6d91cSDavid Greenman 			if (!vm_object_in_map(object)) {
1610fc62ef1fSBruce Evans 				db_printf(
1611fc62ef1fSBruce Evans 			"vmochk: internal obj is not in a map: "
1612fc62ef1fSBruce Evans 			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
1613fc62ef1fSBruce Evans 				    object->ref_count, (u_long)object->size,
1614fc62ef1fSBruce Evans 				    (u_long)object->size,
1615fc62ef1fSBruce Evans 				    (void *)object->backing_object);
1616a1f6d91cSDavid Greenman 			}
1617a1f6d91cSDavid Greenman 		}
1618a1f6d91cSDavid Greenman 	}
1619a1f6d91cSDavid Greenman }
1620a1f6d91cSDavid Greenman 
162126f9a767SRodney W. Grimes /*
1622df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
1623df8bae1dSRodney W. Grimes  */
1624c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static)
1625df8bae1dSRodney W. Grimes {
1626c7c34a24SBruce Evans 	/* XXX convert args. */
1627c7c34a24SBruce Evans 	vm_object_t object = (vm_object_t)addr;
1628c7c34a24SBruce Evans 	boolean_t full = have_addr;
1629c7c34a24SBruce Evans 
1630df8bae1dSRodney W. Grimes 	register vm_page_t p;
1631df8bae1dSRodney W. Grimes 
1632c7c34a24SBruce Evans 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
1633c7c34a24SBruce Evans #define	count	was_count
1634c7c34a24SBruce Evans 
1635df8bae1dSRodney W. Grimes 	register int count;
1636df8bae1dSRodney W. Grimes 
1637df8bae1dSRodney W. Grimes 	if (object == NULL)
1638df8bae1dSRodney W. Grimes 		return;
1639df8bae1dSRodney W. Grimes 
1640eb95adefSBruce Evans 	db_iprintf(
1641eb95adefSBruce Evans 	    "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n",
1642eb95adefSBruce Evans 	    object, (int)object->type, (u_long)object->size,
1643eb95adefSBruce Evans 	    object->resident_page_count, object->ref_count, object->flags);
1644eb95adefSBruce Evans 	/*
16451c7c3c6aSMatthew Dillon 	 * XXX no %qd in kernel.  Truncate object->backing_object_offset.
1646eb95adefSBruce Evans 	 */
16471c7c3c6aSMatthew Dillon 	db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%lx\n",
16481c7c3c6aSMatthew Dillon 	    object->shadow_count,
1649eb95adefSBruce Evans 	    object->backing_object ? object->backing_object->ref_count : 0,
1650eb95adefSBruce Evans 	    object->backing_object, (long)object->backing_object_offset);
1651df8bae1dSRodney W. Grimes 
1652df8bae1dSRodney W. Grimes 	if (!full)
1653df8bae1dSRodney W. Grimes 		return;
1654df8bae1dSRodney W. Grimes 
1655c7c34a24SBruce Evans 	db_indent += 2;
1656df8bae1dSRodney W. Grimes 	count = 0;
1657b18bfc3dSJohn Dyson 	for (p = TAILQ_FIRST(&object->memq); p != NULL; p = TAILQ_NEXT(p, listq)) {
1658df8bae1dSRodney W. Grimes 		if (count == 0)
1659c7c34a24SBruce Evans 			db_iprintf("memory:=");
1660df8bae1dSRodney W. Grimes 		else if (count == 6) {
1661c7c34a24SBruce Evans 			db_printf("\n");
1662c7c34a24SBruce Evans 			db_iprintf(" ...");
1663df8bae1dSRodney W. Grimes 			count = 0;
1664df8bae1dSRodney W. Grimes 		} else
1665c7c34a24SBruce Evans 			db_printf(",");
1666df8bae1dSRodney W. Grimes 		count++;
1667df8bae1dSRodney W. Grimes 
1668c7c34a24SBruce Evans 		db_printf("(off=0x%lx,page=0x%lx)",
1669a316d390SJohn Dyson 		    (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
1670df8bae1dSRodney W. Grimes 	}
1671df8bae1dSRodney W. Grimes 	if (count != 0)
1672c7c34a24SBruce Evans 		db_printf("\n");
1673c7c34a24SBruce Evans 	db_indent -= 2;
1674df8bae1dSRodney W. Grimes }
16755070c7f8SJohn Dyson 
1676c7c34a24SBruce Evans /* XXX. */
1677c7c34a24SBruce Evans #undef count
1678c7c34a24SBruce Evans 
1679c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */
16805070c7f8SJohn Dyson void
1681c7c34a24SBruce Evans vm_object_print(addr, have_addr, count, modif)
1682ecbb00a2SDoug Rabson         /* db_expr_t */ long addr;
1683c7c34a24SBruce Evans 	boolean_t have_addr;
1684ecbb00a2SDoug Rabson 	/* db_expr_t */ long count;
1685c7c34a24SBruce Evans 	char *modif;
1686c7c34a24SBruce Evans {
1687c7c34a24SBruce Evans 	vm_object_print_static(addr, have_addr, count, modif);
1688c7c34a24SBruce Evans }
1689c7c34a24SBruce Evans 
1690c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
16915070c7f8SJohn Dyson {
16925070c7f8SJohn Dyson 	vm_object_t object;
16935070c7f8SJohn Dyson 	int nl = 0;
16945070c7f8SJohn Dyson 	int c;
16955070c7f8SJohn Dyson 	for (object = TAILQ_FIRST(&vm_object_list);
16965070c7f8SJohn Dyson 			object != NULL;
16975070c7f8SJohn Dyson 			object = TAILQ_NEXT(object, object_list)) {
16985070c7f8SJohn Dyson 		vm_pindex_t idx, fidx;
16995070c7f8SJohn Dyson 		vm_pindex_t osize;
17005070c7f8SJohn Dyson 		vm_offset_t pa = -1, padiff;
17015070c7f8SJohn Dyson 		int rcount;
17025070c7f8SJohn Dyson 		vm_page_t m;
17035070c7f8SJohn Dyson 
1704fc62ef1fSBruce Evans 		db_printf("new object: %p\n", (void *)object);
17055070c7f8SJohn Dyson 		if ( nl > 18) {
17065070c7f8SJohn Dyson 			c = cngetc();
17075070c7f8SJohn Dyson 			if (c != ' ')
17085070c7f8SJohn Dyson 				return;
17095070c7f8SJohn Dyson 			nl = 0;
17105070c7f8SJohn Dyson 		}
17115070c7f8SJohn Dyson 		nl++;
17125070c7f8SJohn Dyson 		rcount = 0;
17135070c7f8SJohn Dyson 		fidx = 0;
17145070c7f8SJohn Dyson 		osize = object->size;
17155070c7f8SJohn Dyson 		if (osize > 128)
17165070c7f8SJohn Dyson 			osize = 128;
17175070c7f8SJohn Dyson 		for(idx=0;idx<osize;idx++) {
17185070c7f8SJohn Dyson 			m = vm_page_lookup(object, idx);
17195070c7f8SJohn Dyson 			if (m == NULL) {
17205070c7f8SJohn Dyson 				if (rcount) {
17215070c7f8SJohn Dyson 					db_printf(" index(%d)run(%d)pa(0x%x)\n",
17225070c7f8SJohn Dyson 						fidx, rcount, pa);
17235070c7f8SJohn Dyson 					if ( nl > 18) {
17245070c7f8SJohn Dyson 						c = cngetc();
17255070c7f8SJohn Dyson 						if (c != ' ')
17265070c7f8SJohn Dyson 							return;
17275070c7f8SJohn Dyson 						nl = 0;
17285070c7f8SJohn Dyson 					}
17295070c7f8SJohn Dyson 					nl++;
17305070c7f8SJohn Dyson 					rcount = 0;
17315070c7f8SJohn Dyson 				}
17325070c7f8SJohn Dyson 				continue;
17335070c7f8SJohn Dyson 			}
17345070c7f8SJohn Dyson 
17355070c7f8SJohn Dyson 
17365070c7f8SJohn Dyson 			if (rcount &&
17375070c7f8SJohn Dyson 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
17385070c7f8SJohn Dyson 				++rcount;
17395070c7f8SJohn Dyson 				continue;
17405070c7f8SJohn Dyson 			}
17415070c7f8SJohn Dyson 			if (rcount) {
17425070c7f8SJohn Dyson 				padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
17435070c7f8SJohn Dyson 				padiff >>= PAGE_SHIFT;
17445070c7f8SJohn Dyson 				padiff &= PQ_L2_MASK;
17455070c7f8SJohn Dyson 				if (padiff == 0) {
17465070c7f8SJohn Dyson 					pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
17475070c7f8SJohn Dyson 					++rcount;
17485070c7f8SJohn Dyson 					continue;
17495070c7f8SJohn Dyson 				}
17505070c7f8SJohn Dyson 				db_printf(" index(%d)run(%d)pa(0x%x)", fidx, rcount, pa);
17515070c7f8SJohn Dyson 				db_printf("pd(%d)\n", padiff);
17525070c7f8SJohn Dyson 				if ( nl > 18) {
17535070c7f8SJohn Dyson 					c = cngetc();
17545070c7f8SJohn Dyson 					if (c != ' ')
17555070c7f8SJohn Dyson 						return;
17565070c7f8SJohn Dyson 					nl = 0;
17575070c7f8SJohn Dyson 				}
17585070c7f8SJohn Dyson 				nl++;
17595070c7f8SJohn Dyson 			}
17605070c7f8SJohn Dyson 			fidx = idx;
17615070c7f8SJohn Dyson 			pa = VM_PAGE_TO_PHYS(m);
17625070c7f8SJohn Dyson 			rcount = 1;
17635070c7f8SJohn Dyson 		}
17645070c7f8SJohn Dyson 		if (rcount) {
17655070c7f8SJohn Dyson 			db_printf(" index(%d)run(%d)pa(0x%x)\n", fidx, rcount, pa);
17665070c7f8SJohn Dyson 			if ( nl > 18) {
17675070c7f8SJohn Dyson 				c = cngetc();
17685070c7f8SJohn Dyson 				if (c != ' ')
17695070c7f8SJohn Dyson 					return;
17705070c7f8SJohn Dyson 				nl = 0;
17715070c7f8SJohn Dyson 			}
17725070c7f8SJohn Dyson 			nl++;
17735070c7f8SJohn Dyson 		}
17745070c7f8SJohn Dyson 	}
17755070c7f8SJohn Dyson }
1776c3cb3e12SDavid Greenman #endif /* DDB */
1777