xref: /freebsd/sys/vm/vm_object.c (revision b9b7a4be908b4c527f807b02288961da550112c6)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
6df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
9df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
10df8bae1dSRodney W. Grimes  * are met:
11df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
12df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
13df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
15df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
175929bcfaSPhilippe Charnier  *    must display the following acknowledgement:
18df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
19df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
20df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
21df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
22df8bae1dSRodney W. Grimes  *    without specific prior written permission.
23df8bae1dSRodney W. Grimes  *
24df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
35df8bae1dSRodney W. Grimes  *
363c4dd356SDavid Greenman  *	from: @(#)vm_object.c	8.5 (Berkeley) 3/22/94
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  *
39df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40df8bae1dSRodney W. Grimes  * All rights reserved.
41df8bae1dSRodney W. Grimes  *
42df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
43df8bae1dSRodney W. Grimes  *
44df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
45df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
46df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
47df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
48df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
49df8bae1dSRodney W. Grimes  *
50df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
53df8bae1dSRodney W. Grimes  *
54df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
55df8bae1dSRodney W. Grimes  *
56df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
57df8bae1dSRodney W. Grimes  *  School of Computer Science
58df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
59df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
60df8bae1dSRodney W. Grimes  *
61df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
62df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
633c4dd356SDavid Greenman  *
64c3aac50fSPeter Wemm  * $FreeBSD$
65df8bae1dSRodney W. Grimes  */
66df8bae1dSRodney W. Grimes 
67df8bae1dSRodney W. Grimes /*
68df8bae1dSRodney W. Grimes  *	Virtual memory object module.
69df8bae1dSRodney W. Grimes  */
70df8bae1dSRodney W. Grimes 
71df8bae1dSRodney W. Grimes #include <sys/param.h>
72df8bae1dSRodney W. Grimes #include <sys/systm.h>
73fb919e4dSMark Murray #include <sys/lock.h>
74867a482dSJohn Dyson #include <sys/mman.h>
75cf2819ccSJohn Dyson #include <sys/mount.h>
76b9b7a4beSMatthew Dillon #include <sys/kernel.h>
77b9b7a4beSMatthew Dillon #include <sys/sysctl.h>
781b367556SJason Evans #include <sys/mutex.h>
79fb919e4dSMark Murray #include <sys/proc.h>		/* for curproc, pageproc */
80fb919e4dSMark Murray #include <sys/socket.h>
81fb919e4dSMark Murray #include <sys/vnode.h>
82fb919e4dSMark Murray #include <sys/vmmeter.h>
831005a129SJohn Baldwin #include <sys/sx.h>
84df8bae1dSRodney W. Grimes 
85df8bae1dSRodney W. Grimes #include <vm/vm.h>
86efeaf95aSDavid Greenman #include <vm/vm_param.h>
87efeaf95aSDavid Greenman #include <vm/pmap.h>
88efeaf95aSDavid Greenman #include <vm/vm_map.h>
89efeaf95aSDavid Greenman #include <vm/vm_object.h>
90df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
9126f9a767SRodney W. Grimes #include <vm/vm_pageout.h>
920d94caffSDavid Greenman #include <vm/vm_pager.h>
9321cd6e62SSeigo Tanimura #include <vm/vm_zone.h>
9405f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h>
95a1f6d91cSDavid Greenman #include <vm/vm_kern.h>
96efeaf95aSDavid Greenman #include <vm/vm_extern.h>
9726f9a767SRodney W. Grimes 
98b9b7a4beSMatthew Dillon #define EASY_SCAN_FACTOR       8
99b9b7a4beSMatthew Dillon 
100b9b7a4beSMatthew Dillon #define MSYNC_FLUSH_HARDSEQ	0x01
101b9b7a4beSMatthew Dillon #define MSYNC_FLUSH_SOFTSEQ	0x02
102b9b7a4beSMatthew Dillon 
103b9b7a4beSMatthew Dillon /*
104b9b7a4beSMatthew Dillon  * msync / VM object flushing optimizations
105b9b7a4beSMatthew Dillon  */
106b9b7a4beSMatthew Dillon static int msync_flush_flags = MSYNC_FLUSH_HARDSEQ | MSYNC_FLUSH_SOFTSEQ;
107b9b7a4beSMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, msync_flush_flags,
108b9b7a4beSMatthew Dillon         CTLFLAG_RW, &msync_flush_flags, 0, "");
109b9b7a4beSMatthew Dillon 
110b9b7a4beSMatthew Dillon static void	vm_object_qcollapse(vm_object_t object);
111b9b7a4beSMatthew Dillon static int	vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags);
112f6b04d2bSDavid Greenman 
113df8bae1dSRodney W. Grimes /*
114df8bae1dSRodney W. Grimes  *	Virtual memory objects maintain the actual data
115df8bae1dSRodney W. Grimes  *	associated with allocated virtual memory.  A given
116df8bae1dSRodney W. Grimes  *	page of memory exists within exactly one object.
117df8bae1dSRodney W. Grimes  *
118df8bae1dSRodney W. Grimes  *	An object is only deallocated when all "references"
119df8bae1dSRodney W. Grimes  *	are given up.  Only one "reference" to a given
120df8bae1dSRodney W. Grimes  *	region of an object should be writeable.
121df8bae1dSRodney W. Grimes  *
122df8bae1dSRodney W. Grimes  *	Associated with each object is a list of all resident
123df8bae1dSRodney W. Grimes  *	memory pages belonging to that object; this list is
124df8bae1dSRodney W. Grimes  *	maintained by the "vm_page" module, and locked by the object's
125df8bae1dSRodney W. Grimes  *	lock.
126df8bae1dSRodney W. Grimes  *
127df8bae1dSRodney W. Grimes  *	Each object also records a "pager" routine which is
128df8bae1dSRodney W. Grimes  *	used to retrieve (and store) pages to the proper backing
129df8bae1dSRodney W. Grimes  *	storage.  In addition, objects may be backed by other
130df8bae1dSRodney W. Grimes  *	objects from which they were virtual-copied.
131df8bae1dSRodney W. Grimes  *
132df8bae1dSRodney W. Grimes  *	The only items within the object structure which are
133df8bae1dSRodney W. Grimes  *	modified after time of creation are:
134df8bae1dSRodney W. Grimes  *		reference count		locked by object's lock
135df8bae1dSRodney W. Grimes  *		pager routine		locked by object's lock
136df8bae1dSRodney W. Grimes  *
137df8bae1dSRodney W. Grimes  */
138df8bae1dSRodney W. Grimes 
13928f8db14SBruce Evans struct object_q vm_object_list;
140cc64b484SAlfred Perlstein static struct mtx vm_object_list_mtx;	/* lock for object list and count */
1414de628deSBruce Evans static long vm_object_count;		/* count of all objects */
14228f8db14SBruce Evans vm_object_t kernel_object;
14328f8db14SBruce Evans vm_object_t kmem_object;
144f708ef1bSPoul-Henning Kamp static struct vm_object kernel_object_store;
145f708ef1bSPoul-Henning Kamp static struct vm_object kmem_object_store;
146aef922f5SJohn Dyson extern int vm_pageout_page_count;
147df8bae1dSRodney W. Grimes 
148f708ef1bSPoul-Henning Kamp static long object_collapses;
149f708ef1bSPoul-Henning Kamp static long object_bypasses;
1505070c7f8SJohn Dyson static int next_index;
15199448ed1SJohn Dyson static vm_zone_t obj_zone;
15299448ed1SJohn Dyson static struct vm_zone obj_zone_store;
1531c7c3c6aSMatthew Dillon static int object_hash_rand;
15499448ed1SJohn Dyson #define VM_OBJECTS_INIT 256
155303b270bSEivind Eklund static struct vm_object vm_objects_init[VM_OBJECTS_INIT];
156df8bae1dSRodney W. Grimes 
1573075778bSJohn Dyson void
1581b40f8c0SMatthew Dillon _vm_object_allocate(objtype_t type, vm_size_t size, vm_object_t object)
159df8bae1dSRodney W. Grimes {
16099448ed1SJohn Dyson 	int incr;
16123955314SAlfred Perlstein 
1620cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
1630cddd8f0SMatthew Dillon 
164df8bae1dSRodney W. Grimes 	TAILQ_INIT(&object->memq);
16524a1cce3SDavid Greenman 	TAILQ_INIT(&object->shadow_head);
166a1f6d91cSDavid Greenman 
16724a1cce3SDavid Greenman 	object->type = type;
168df8bae1dSRodney W. Grimes 	object->size = size;
169a1f6d91cSDavid Greenman 	object->ref_count = 1;
17024a1cce3SDavid Greenman 	object->flags = 0;
17160517fd1SJohn Baldwin 	if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
17260517fd1SJohn Baldwin 		vm_object_set_flag(object, OBJ_ONEMAPPING);
173df8bae1dSRodney W. Grimes 	object->paging_in_progress = 0;
174a1f6d91cSDavid Greenman 	object->resident_page_count = 0;
175de5f6a77SJohn Dyson 	object->shadow_count = 0;
1765070c7f8SJohn Dyson 	object->pg_color = next_index;
17799448ed1SJohn Dyson 	if ( size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
17899448ed1SJohn Dyson 		incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
17999448ed1SJohn Dyson 	else
18099448ed1SJohn Dyson 		incr = size;
18199448ed1SJohn Dyson 	next_index = (next_index + incr) & PQ_L2_MASK;
18224a1cce3SDavid Greenman 	object->handle = NULL;
18324a1cce3SDavid Greenman 	object->backing_object = NULL;
184a316d390SJohn Dyson 	object->backing_object_offset = (vm_ooffset_t) 0;
1851c7c3c6aSMatthew Dillon 	/*
1861c7c3c6aSMatthew Dillon 	 * Try to generate a number that will spread objects out in the
1871c7c3c6aSMatthew Dillon 	 * hash table.  We 'wipe' new objects across the hash in 128 page
1881c7c3c6aSMatthew Dillon 	 * increments plus 1 more to offset it a little more by the time
1891c7c3c6aSMatthew Dillon 	 * it wraps around.
1901c7c3c6aSMatthew Dillon 	 */
1911c7c3c6aSMatthew Dillon 	object->hash_rand = object_hash_rand - 129;
192a1f6d91cSDavid Greenman 
19360517fd1SJohn Baldwin 	object->generation++;
19460517fd1SJohn Baldwin 
19560517fd1SJohn Baldwin 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
19660517fd1SJohn Baldwin 	vm_object_count++;
1971c7c3c6aSMatthew Dillon 	object_hash_rand = object->hash_rand;
198df8bae1dSRodney W. Grimes }
199df8bae1dSRodney W. Grimes 
200df8bae1dSRodney W. Grimes /*
20126f9a767SRodney W. Grimes  *	vm_object_init:
20226f9a767SRodney W. Grimes  *
20326f9a767SRodney W. Grimes  *	Initialize the VM objects module.
20426f9a767SRodney W. Grimes  */
20526f9a767SRodney W. Grimes void
2061b40f8c0SMatthew Dillon vm_object_init(void)
20726f9a767SRodney W. Grimes {
2080cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
20960517fd1SJohn Baldwin 
21026f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_list);
2111b367556SJason Evans 	mtx_init(&vm_object_list_mtx, "vm object_list", MTX_DEF);
21226f9a767SRodney W. Grimes 	vm_object_count = 0;
2130217125fSDavid Greenman 
21426f9a767SRodney W. Grimes 	kernel_object = &kernel_object_store;
215a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
21626f9a767SRodney W. Grimes 	    kernel_object);
21726f9a767SRodney W. Grimes 
21826f9a767SRodney W. Grimes 	kmem_object = &kmem_object_store;
219a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
22026f9a767SRodney W. Grimes 	    kmem_object);
22199448ed1SJohn Dyson 
22299448ed1SJohn Dyson 	obj_zone = &obj_zone_store;
22399448ed1SJohn Dyson 	zbootinit(obj_zone, "VM OBJECT", sizeof (struct vm_object),
22499448ed1SJohn Dyson 		vm_objects_init, VM_OBJECTS_INIT);
22599448ed1SJohn Dyson }
22699448ed1SJohn Dyson 
22799448ed1SJohn Dyson void
2281b40f8c0SMatthew Dillon vm_object_init2(void)
22960517fd1SJohn Baldwin {
2300a80f406SJohn Dyson 	zinitna(obj_zone, NULL, NULL, 0, 0, 0, 1);
23126f9a767SRodney W. Grimes }
23226f9a767SRodney W. Grimes 
2331b40f8c0SMatthew Dillon void
2341b40f8c0SMatthew Dillon vm_object_set_flag(vm_object_t object, u_short bits)
2351b40f8c0SMatthew Dillon {
2361b40f8c0SMatthew Dillon 	GIANT_REQUIRED;
237b06805adSJake Burkholder 	object->flags |= bits;
2381b40f8c0SMatthew Dillon }
2391b40f8c0SMatthew Dillon 
2401b40f8c0SMatthew Dillon void
2411b40f8c0SMatthew Dillon vm_object_clear_flag(vm_object_t object, u_short bits)
2421b40f8c0SMatthew Dillon {
2431b40f8c0SMatthew Dillon 	GIANT_REQUIRED;
244b06805adSJake Burkholder 	object->flags &= ~bits;
2451b40f8c0SMatthew Dillon }
2461b40f8c0SMatthew Dillon 
2471b40f8c0SMatthew Dillon void
2481b40f8c0SMatthew Dillon vm_object_pip_add(vm_object_t object, short i)
2491b40f8c0SMatthew Dillon {
2501b40f8c0SMatthew Dillon 	GIANT_REQUIRED;
251b06805adSJake Burkholder 	object->paging_in_progress += i;
2521b40f8c0SMatthew Dillon }
2531b40f8c0SMatthew Dillon 
2541b40f8c0SMatthew Dillon void
2551b40f8c0SMatthew Dillon vm_object_pip_subtract(vm_object_t object, short i)
2561b40f8c0SMatthew Dillon {
2571b40f8c0SMatthew Dillon 	GIANT_REQUIRED;
258b06805adSJake Burkholder 	object->paging_in_progress -= i;
2591b40f8c0SMatthew Dillon }
2601b40f8c0SMatthew Dillon 
2611b40f8c0SMatthew Dillon void
2621b40f8c0SMatthew Dillon vm_object_pip_wakeup(vm_object_t object)
2631b40f8c0SMatthew Dillon {
2641b40f8c0SMatthew Dillon 	GIANT_REQUIRED;
265b06805adSJake Burkholder 	object->paging_in_progress--;
2661b40f8c0SMatthew Dillon 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
2671b40f8c0SMatthew Dillon 		vm_object_clear_flag(object, OBJ_PIPWNT);
2681b40f8c0SMatthew Dillon 		wakeup(object);
2691b40f8c0SMatthew Dillon 	}
2701b40f8c0SMatthew Dillon }
2711b40f8c0SMatthew Dillon 
2721b40f8c0SMatthew Dillon void
2731b40f8c0SMatthew Dillon vm_object_pip_wakeupn(vm_object_t object, short i)
2741b40f8c0SMatthew Dillon {
2751b40f8c0SMatthew Dillon 	GIANT_REQUIRED;
2761b40f8c0SMatthew Dillon 	if (i)
277b06805adSJake Burkholder 		object->paging_in_progress -= i;
2781b40f8c0SMatthew Dillon 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
2791b40f8c0SMatthew Dillon 		vm_object_clear_flag(object, OBJ_PIPWNT);
2801b40f8c0SMatthew Dillon 		wakeup(object);
2811b40f8c0SMatthew Dillon 	}
2821b40f8c0SMatthew Dillon }
2831b40f8c0SMatthew Dillon 
2841b40f8c0SMatthew Dillon void
2851b40f8c0SMatthew Dillon vm_object_pip_sleep(vm_object_t object, char *waitid)
2861b40f8c0SMatthew Dillon {
2871b40f8c0SMatthew Dillon 	GIANT_REQUIRED;
2881b40f8c0SMatthew Dillon 	if (object->paging_in_progress) {
2891b40f8c0SMatthew Dillon 		int s = splvm();
2901b40f8c0SMatthew Dillon 		if (object->paging_in_progress) {
2911b40f8c0SMatthew Dillon 			vm_object_set_flag(object, OBJ_PIPWNT);
2921b40f8c0SMatthew Dillon 			tsleep(object, PVM, waitid, 0);
2931b40f8c0SMatthew Dillon 		}
2941b40f8c0SMatthew Dillon 		splx(s);
2951b40f8c0SMatthew Dillon 	}
2961b40f8c0SMatthew Dillon }
2971b40f8c0SMatthew Dillon 
2981b40f8c0SMatthew Dillon void
2991b40f8c0SMatthew Dillon vm_object_pip_wait(vm_object_t object, char *waitid)
3001b40f8c0SMatthew Dillon {
3011b40f8c0SMatthew Dillon 	GIANT_REQUIRED;
3021b40f8c0SMatthew Dillon 	while (object->paging_in_progress)
3031b40f8c0SMatthew Dillon 		vm_object_pip_sleep(object, waitid);
3041b40f8c0SMatthew Dillon }
3051b40f8c0SMatthew Dillon 
30626f9a767SRodney W. Grimes /*
30726f9a767SRodney W. Grimes  *	vm_object_allocate:
30826f9a767SRodney W. Grimes  *
30926f9a767SRodney W. Grimes  *	Returns a new object with the given size.
31026f9a767SRodney W. Grimes  */
31126f9a767SRodney W. Grimes 
31226f9a767SRodney W. Grimes vm_object_t
3131b40f8c0SMatthew Dillon vm_object_allocate(objtype_t type, vm_size_t size)
31426f9a767SRodney W. Grimes {
315d031cff1SMatthew Dillon 	vm_object_t result;
316d031cff1SMatthew Dillon 
3170cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
3180cddd8f0SMatthew Dillon 
31999448ed1SJohn Dyson 	result = (vm_object_t) zalloc(obj_zone);
32024a1cce3SDavid Greenman 	_vm_object_allocate(type, size, result);
32126f9a767SRodney W. Grimes 
32226f9a767SRodney W. Grimes 	return (result);
32326f9a767SRodney W. Grimes }
32426f9a767SRodney W. Grimes 
32526f9a767SRodney W. Grimes 
32626f9a767SRodney W. Grimes /*
327df8bae1dSRodney W. Grimes  *	vm_object_reference:
328df8bae1dSRodney W. Grimes  *
329df8bae1dSRodney W. Grimes  *	Gets another reference to the given object.
330df8bae1dSRodney W. Grimes  */
3316476c0d2SJohn Dyson void
3321b40f8c0SMatthew Dillon vm_object_reference(vm_object_t object)
333df8bae1dSRodney W. Grimes {
3340cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
33560517fd1SJohn Baldwin 
336df8bae1dSRodney W. Grimes 	if (object == NULL)
337df8bae1dSRodney W. Grimes 		return;
33895e5e988SJohn Dyson 
339245df27cSMatthew Dillon #if 0
340245df27cSMatthew Dillon 	/* object can be re-referenced during final cleaning */
3415526d2d9SEivind Eklund 	KASSERT(!(object->flags & OBJ_DEAD),
3425526d2d9SEivind Eklund 	    ("vm_object_reference: attempting to reference dead obj"));
343245df27cSMatthew Dillon #endif
34495e5e988SJohn Dyson 
345df8bae1dSRodney W. Grimes 	object->ref_count++;
34647221757SJohn Dyson 	if (object->type == OBJT_VNODE) {
347b40ce416SJulian Elischer 		while (vget((struct vnode *) object->handle, LK_RETRY|LK_NOOBJ, curthread)) {
34847221757SJohn Dyson 			printf("vm_object_reference: delay in getting object\n");
34947221757SJohn Dyson 		}
35047221757SJohn Dyson 	}
35195e5e988SJohn Dyson }
35295e5e988SJohn Dyson 
35323955314SAlfred Perlstein /*
35423955314SAlfred Perlstein  * handle deallocating a object of type OBJT_VNODE
35523955314SAlfred Perlstein  */
356bf27292bSJohn Dyson void
3571b40f8c0SMatthew Dillon vm_object_vndeallocate(vm_object_t object)
35895e5e988SJohn Dyson {
35995e5e988SJohn Dyson 	struct vnode *vp = (struct vnode *) object->handle;
360219cbf59SEivind Eklund 
3610cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
3625526d2d9SEivind Eklund 	KASSERT(object->type == OBJT_VNODE,
3635526d2d9SEivind Eklund 	    ("vm_object_vndeallocate: not a vnode object"));
364219cbf59SEivind Eklund 	KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
365219cbf59SEivind Eklund #ifdef INVARIANTS
36695e5e988SJohn Dyson 	if (object->ref_count == 0) {
36795e5e988SJohn Dyson 		vprint("vm_object_vndeallocate", vp);
36895e5e988SJohn Dyson 		panic("vm_object_vndeallocate: bad object reference count");
36995e5e988SJohn Dyson 	}
37095e5e988SJohn Dyson #endif
37195e5e988SJohn Dyson 
37295e5e988SJohn Dyson 	object->ref_count--;
37347221757SJohn Dyson 	if (object->ref_count == 0) {
374bf27292bSJohn Dyson 		vp->v_flag &= ~VTEXT;
375069e9bc1SDoug Rabson 		vm_object_clear_flag(object, OBJ_OPT);
3762be70f79SJohn Dyson 	}
37723955314SAlfred Perlstein 	/*
37823955314SAlfred Perlstein 	 * vrele may need a vop lock
37923955314SAlfred Perlstein 	 */
38047221757SJohn Dyson 	vrele(vp);
381df8bae1dSRodney W. Grimes }
382df8bae1dSRodney W. Grimes 
383df8bae1dSRodney W. Grimes /*
384df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
385df8bae1dSRodney W. Grimes  *
386df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
387df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
388df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
389df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
390df8bae1dSRodney W. Grimes  *	may be relinquished.
391df8bae1dSRodney W. Grimes  *
392df8bae1dSRodney W. Grimes  *	No object may be locked.
393df8bae1dSRodney W. Grimes  */
39426f9a767SRodney W. Grimes void
3951b40f8c0SMatthew Dillon vm_object_deallocate(vm_object_t object)
396df8bae1dSRodney W. Grimes {
397df8bae1dSRodney W. Grimes 	vm_object_t temp;
398df8bae1dSRodney W. Grimes 
3990cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
4000cddd8f0SMatthew Dillon 
401df8bae1dSRodney W. Grimes 	while (object != NULL) {
402df8bae1dSRodney W. Grimes 
40395e5e988SJohn Dyson 		if (object->type == OBJT_VNODE) {
40495e5e988SJohn Dyson 			vm_object_vndeallocate(object);
40595e5e988SJohn Dyson 			return;
40695e5e988SJohn Dyson 		}
40795e5e988SJohn Dyson 
4088125b1e6SAlfred Perlstein 		KASSERT(object->ref_count != 0,
4098125b1e6SAlfred Perlstein 			("vm_object_deallocate: object deallocated too many times: %d", object->type));
4102be70f79SJohn Dyson 
4112be70f79SJohn Dyson 		/*
4128125b1e6SAlfred Perlstein 		 * If the reference count goes to 0 we start calling
4138125b1e6SAlfred Perlstein 		 * vm_object_terminate() on the object chain.
4148125b1e6SAlfred Perlstein 		 * A ref count of 1 may be a special case depending on the
4158125b1e6SAlfred Perlstein 		 * shadow count being 0 or 1.
4162be70f79SJohn Dyson 		 */
417c0877f10SJohn Dyson 		object->ref_count--;
4188125b1e6SAlfred Perlstein 		if (object->ref_count > 1) {
419c0877f10SJohn Dyson 			return;
4208125b1e6SAlfred Perlstein 		} else if (object->ref_count == 1) {
4218125b1e6SAlfred Perlstein 			if (object->shadow_count == 0) {
4228125b1e6SAlfred Perlstein 				vm_object_set_flag(object, OBJ_ONEMAPPING);
4238125b1e6SAlfred Perlstein 			} else if ((object->shadow_count == 1) &&
4248125b1e6SAlfred Perlstein 			    (object->handle == NULL) &&
42524a1cce3SDavid Greenman 			    (object->type == OBJT_DEFAULT ||
42624a1cce3SDavid Greenman 			     object->type == OBJT_SWAP)) {
427a1f6d91cSDavid Greenman 				vm_object_t robject;
42895e5e988SJohn Dyson 
429b18bfc3dSJohn Dyson 				robject = TAILQ_FIRST(&object->shadow_head);
4305526d2d9SEivind Eklund 				KASSERT(robject != NULL,
431219cbf59SEivind Eklund 				    ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
4325526d2d9SEivind Eklund 					 object->ref_count,
4335526d2d9SEivind Eklund 					 object->shadow_count));
43495e5e988SJohn Dyson 				if ((robject->handle == NULL) &&
43524a1cce3SDavid Greenman 				    (robject->type == OBJT_DEFAULT ||
43624a1cce3SDavid Greenman 				     robject->type == OBJT_SWAP)) {
437a1f6d91cSDavid Greenman 
43895e5e988SJohn Dyson 					robject->ref_count++;
43995e5e988SJohn Dyson 
4401c7c3c6aSMatthew Dillon 					while (
4411c7c3c6aSMatthew Dillon 						robject->paging_in_progress ||
4421c7c3c6aSMatthew Dillon 						object->paging_in_progress
4431c7c3c6aSMatthew Dillon 					) {
44466095752SJohn Dyson 						vm_object_pip_sleep(robject, "objde1");
44566095752SJohn Dyson 						vm_object_pip_sleep(object, "objde2");
446a1f6d91cSDavid Greenman 					}
447a1f6d91cSDavid Greenman 
44895e5e988SJohn Dyson 					if (robject->ref_count == 1) {
44995e5e988SJohn Dyson 						robject->ref_count--;
450ba8da839SDavid Greenman 						object = robject;
45195e5e988SJohn Dyson 						goto doterm;
45295e5e988SJohn Dyson 					}
45395e5e988SJohn Dyson 
45495e5e988SJohn Dyson 					object = robject;
45595e5e988SJohn Dyson 					vm_object_collapse(object);
456ba8da839SDavid Greenman 					continue;
457a1f6d91cSDavid Greenman 				}
45895e5e988SJohn Dyson 			}
45995e5e988SJohn Dyson 
460a1f6d91cSDavid Greenman 			return;
461df8bae1dSRodney W. Grimes 
46295e5e988SJohn Dyson 		}
46395e5e988SJohn Dyson 
46495e5e988SJohn Dyson doterm:
4651efb74fbSJohn Dyson 
46624a1cce3SDavid Greenman 		temp = object->backing_object;
467de5f6a77SJohn Dyson 		if (temp) {
46824a1cce3SDavid Greenman 			TAILQ_REMOVE(&temp->shadow_head, object, shadow_list);
46995e5e988SJohn Dyson 			temp->shadow_count--;
4702d8acc0fSJohn Dyson 			if (temp->ref_count == 0)
471069e9bc1SDoug Rabson 				vm_object_clear_flag(temp, OBJ_OPT);
472eaf13dd7SJohn Dyson 			temp->generation++;
47395461b45SJohn Dyson 			object->backing_object = NULL;
474de5f6a77SJohn Dyson 		}
475245df27cSMatthew Dillon 		/*
476245df27cSMatthew Dillon 		 * Don't double-terminate, we could be in a termination
477245df27cSMatthew Dillon 		 * recursion due to the terminate having to sync data
478245df27cSMatthew Dillon 		 * to disk.
479245df27cSMatthew Dillon 		 */
480245df27cSMatthew Dillon 		if ((object->flags & OBJ_DEAD) == 0)
481df8bae1dSRodney W. Grimes 			vm_object_terminate(object);
482df8bae1dSRodney W. Grimes 		object = temp;
483df8bae1dSRodney W. Grimes 	}
484df8bae1dSRodney W. Grimes }
485df8bae1dSRodney W. Grimes 
486df8bae1dSRodney W. Grimes /*
487df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
488df8bae1dSRodney W. Grimes  *	up all previously used resources.
489df8bae1dSRodney W. Grimes  *
490df8bae1dSRodney W. Grimes  *	The object must be locked.
4911c7c3c6aSMatthew Dillon  *	This routine may block.
492df8bae1dSRodney W. Grimes  */
49395e5e988SJohn Dyson void
4941b40f8c0SMatthew Dillon vm_object_terminate(vm_object_t object)
495df8bae1dSRodney W. Grimes {
496d031cff1SMatthew Dillon 	vm_page_t p;
497e4b7635dSDavid Greenman 	int s;
498df8bae1dSRodney W. Grimes 
4990cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
5000cddd8f0SMatthew Dillon 
50195e5e988SJohn Dyson 	/*
50295e5e988SJohn Dyson 	 * Make sure no one uses us.
50395e5e988SJohn Dyson 	 */
504069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_DEAD);
5053c631446SJohn Dyson 
506df8bae1dSRodney W. Grimes 	/*
507f6b04d2bSDavid Greenman 	 * wait for the pageout daemon to be done with the object
508df8bae1dSRodney W. Grimes 	 */
50966095752SJohn Dyson 	vm_object_pip_wait(object, "objtrm");
510df8bae1dSRodney W. Grimes 
5115526d2d9SEivind Eklund 	KASSERT(!object->paging_in_progress,
5125526d2d9SEivind Eklund 		("vm_object_terminate: pageout in progress"));
51326f9a767SRodney W. Grimes 
51426f9a767SRodney W. Grimes 	/*
5150d94caffSDavid Greenman 	 * Clean and free the pages, as appropriate. All references to the
5160d94caffSDavid Greenman 	 * object are gone, so we don't need to lock it.
51726f9a767SRodney W. Grimes 	 */
51824a1cce3SDavid Greenman 	if (object->type == OBJT_VNODE) {
51995e5e988SJohn Dyson 		struct vnode *vp;
52095e5e988SJohn Dyson 
52195e5e988SJohn Dyson 		/*
52295e5e988SJohn Dyson 		 * Freeze optimized copies.
52395e5e988SJohn Dyson 		 */
52495e5e988SJohn Dyson 		vm_freeze_copyopts(object, 0, object->size);
52595e5e988SJohn Dyson 
52695e5e988SJohn Dyson 		/*
52795e5e988SJohn Dyson 		 * Clean pages and flush buffers.
52895e5e988SJohn Dyson 		 */
5298f9110f6SJohn Dyson 		vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
53095e5e988SJohn Dyson 
53195e5e988SJohn Dyson 		vp = (struct vnode *) object->handle;
532f6b04d2bSDavid Greenman 		vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
533bef608bdSJohn Dyson 	}
534bef608bdSJohn Dyson 
535971dd342SAlfred Perlstein 	KASSERT(object->ref_count == 0,
536971dd342SAlfred Perlstein 		("vm_object_terminate: object with references, ref_count=%d",
537971dd342SAlfred Perlstein 		object->ref_count));
538996c772fSJohn Dyson 
5390d94caffSDavid Greenman 	/*
540356863ebSDavid Greenman 	 * Now free any remaining pages. For internal objects, this also
541356863ebSDavid Greenman 	 * removes them from paging queues. Don't free wired pages, just
542356863ebSDavid Greenman 	 * remove them from the object.
543df8bae1dSRodney W. Grimes 	 */
544e4b7635dSDavid Greenman 	s = splvm();
545b18bfc3dSJohn Dyson 	while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
546971dd342SAlfred Perlstein 		KASSERT(!p->busy && (p->flags & PG_BUSY) == 0,
547971dd342SAlfred Perlstein 			("vm_object_terminate: freeing busy page %p "
548971dd342SAlfred Perlstein 			"p->busy = %d, p->flags %x\n", p, p->busy, p->flags));
5490b10ba98SDavid Greenman 		if (p->wire_count == 0) {
550e69763a3SDoug Rabson 			vm_page_busy(p);
551df8bae1dSRodney W. Grimes 			vm_page_free(p);
552df8bae1dSRodney W. Grimes 			cnt.v_pfree++;
5530b10ba98SDavid Greenman 		} else {
554356863ebSDavid Greenman 			vm_page_busy(p);
5550b10ba98SDavid Greenman 			vm_page_remove(p);
5560b10ba98SDavid Greenman 		}
557df8bae1dSRodney W. Grimes 	}
558e4b7635dSDavid Greenman 	splx(s);
559bef608bdSJohn Dyson 
5602d8acc0fSJohn Dyson 	/*
5619fcfb650SDavid Greenman 	 * Let the pager know object is dead.
5629fcfb650SDavid Greenman 	 */
5639fcfb650SDavid Greenman 	vm_pager_deallocate(object);
5649fcfb650SDavid Greenman 
5659fcfb650SDavid Greenman 	/*
5660b10ba98SDavid Greenman 	 * Remove the object from the global object list.
5672d8acc0fSJohn Dyson 	 */
5689ed346baSBosko Milekic 	mtx_lock(&vm_object_list_mtx);
569df8bae1dSRodney W. Grimes 	TAILQ_REMOVE(&vm_object_list, object, object_list);
5709ed346baSBosko Milekic 	mtx_unlock(&vm_object_list_mtx);
5710b10ba98SDavid Greenman 
5720b10ba98SDavid Greenman 	wakeup(object);
5730b10ba98SDavid Greenman 
574df8bae1dSRodney W. Grimes 	/*
575df8bae1dSRodney W. Grimes 	 * Free the space for the object.
576df8bae1dSRodney W. Grimes 	 */
57799448ed1SJohn Dyson 	zfree(obj_zone, object);
57847221757SJohn Dyson }
579df8bae1dSRodney W. Grimes 
580df8bae1dSRodney W. Grimes /*
581df8bae1dSRodney W. Grimes  *	vm_object_page_clean
582df8bae1dSRodney W. Grimes  *
5834f79d873SMatthew Dillon  *	Clean all dirty pages in the specified range of object.  Leaves page
5844f79d873SMatthew Dillon  * 	on whatever queue it is currently on.   If NOSYNC is set then do not
5854f79d873SMatthew Dillon  *	write out pages with PG_NOSYNC set (originally comes from MAP_NOSYNC),
5864f79d873SMatthew Dillon  *	leaving the object dirty.
58726f9a767SRodney W. Grimes  *
58826f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
58926f9a767SRodney W. Grimes  *
59026f9a767SRodney W. Grimes  *	The object must be locked.
59126f9a767SRodney W. Grimes  */
592f6b04d2bSDavid Greenman 
593f6b04d2bSDavid Greenman void
5941b40f8c0SMatthew Dillon vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end, int flags)
595f6b04d2bSDavid Greenman {
596b9b7a4beSMatthew Dillon 	vm_page_t p, np;
597d031cff1SMatthew Dillon 	vm_offset_t tstart, tend;
598bd7e5f99SJohn Dyson 	vm_pindex_t pi;
59924a1cce3SDavid Greenman 	struct vnode *vp;
6004f79d873SMatthew Dillon 	int clearobjflags;
6018f9110f6SJohn Dyson 	int pagerflags;
6022d8acc0fSJohn Dyson 	int curgeneration;
603f6b04d2bSDavid Greenman 
6040cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
6050cddd8f0SMatthew Dillon 
606aef922f5SJohn Dyson 	if (object->type != OBJT_VNODE ||
607aef922f5SJohn Dyson 		(object->flags & OBJ_MIGHTBEDIRTY) == 0)
608f6b04d2bSDavid Greenman 		return;
609f6b04d2bSDavid Greenman 
6108f9110f6SJohn Dyson 	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : 0;
6118f9110f6SJohn Dyson 	pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
6128f9110f6SJohn Dyson 
61324a1cce3SDavid Greenman 	vp = object->handle;
61424a1cce3SDavid Greenman 
615069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_CLEANING);
61624a1cce3SDavid Greenman 
617f6b04d2bSDavid Greenman 	tstart = start;
618f6b04d2bSDavid Greenman 	if (end == 0) {
619f6b04d2bSDavid Greenman 		tend = object->size;
620f6b04d2bSDavid Greenman 	} else {
621f6b04d2bSDavid Greenman 		tend = end;
622f6b04d2bSDavid Greenman 	}
623eaf13dd7SJohn Dyson 
6244f79d873SMatthew Dillon 	/*
625b9b7a4beSMatthew Dillon 	 * If the caller is smart and only msync()s a range he knows is
626b9b7a4beSMatthew Dillon 	 * dirty, we may be able to avoid an object scan.  This results in
627b9b7a4beSMatthew Dillon 	 * a phenominal improvement in performance.  We cannot do this
628b9b7a4beSMatthew Dillon 	 * as a matter of course because the object may be huge - e.g.
629b9b7a4beSMatthew Dillon 	 * the size might be in the gigabytes or terrabytes.
630b9b7a4beSMatthew Dillon 	 */
631b9b7a4beSMatthew Dillon 	if (msync_flush_flags & MSYNC_FLUSH_HARDSEQ) {
632b9b7a4beSMatthew Dillon 		vm_offset_t tscan;
633b9b7a4beSMatthew Dillon 		int scanlimit;
634b9b7a4beSMatthew Dillon 		int scanreset;
635b9b7a4beSMatthew Dillon 
636b9b7a4beSMatthew Dillon 		scanreset = object->resident_page_count / EASY_SCAN_FACTOR;
637b9b7a4beSMatthew Dillon 		if (scanreset < 16)
638b9b7a4beSMatthew Dillon 			scanreset = 16;
639b9b7a4beSMatthew Dillon 
640b9b7a4beSMatthew Dillon 		scanlimit = scanreset;
641b9b7a4beSMatthew Dillon 		tscan = tstart;
642b9b7a4beSMatthew Dillon 		while (tscan < tend) {
643b9b7a4beSMatthew Dillon 			curgeneration = object->generation;
644b9b7a4beSMatthew Dillon 			p = vm_page_lookup(object, tscan);
645b9b7a4beSMatthew Dillon 			if (p == NULL || p->valid == 0 ||
646b9b7a4beSMatthew Dillon 			    (p->queue - p->pc) == PQ_CACHE) {
647b9b7a4beSMatthew Dillon 				if (--scanlimit == 0)
648b9b7a4beSMatthew Dillon 					break;
649b9b7a4beSMatthew Dillon 				++tscan;
650b9b7a4beSMatthew Dillon 				continue;
651b9b7a4beSMatthew Dillon 			}
652b9b7a4beSMatthew Dillon 			vm_page_test_dirty(p);
653b9b7a4beSMatthew Dillon 			if ((p->dirty & p->valid) == 0) {
654b9b7a4beSMatthew Dillon 				if (--scanlimit == 0)
655b9b7a4beSMatthew Dillon 					break;
656b9b7a4beSMatthew Dillon 				++tscan;
657b9b7a4beSMatthew Dillon 				continue;
658b9b7a4beSMatthew Dillon 			}
659b9b7a4beSMatthew Dillon 			/*
660b9b7a4beSMatthew Dillon 			 * If we have been asked to skip nosync pages and
661b9b7a4beSMatthew Dillon 			 * this is a nosync page, we can't continue.
662b9b7a4beSMatthew Dillon 			 */
663b9b7a4beSMatthew Dillon 			if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
664b9b7a4beSMatthew Dillon 				if (--scanlimit == 0)
665b9b7a4beSMatthew Dillon 					break;
666b9b7a4beSMatthew Dillon 				++tscan;
667b9b7a4beSMatthew Dillon 				continue;
668b9b7a4beSMatthew Dillon 			}
669b9b7a4beSMatthew Dillon 			scanlimit = scanreset;
670b9b7a4beSMatthew Dillon 
671b9b7a4beSMatthew Dillon 			/*
672b9b7a4beSMatthew Dillon 			 * This returns 0 if it was unable to busy the first
673b9b7a4beSMatthew Dillon 			 * page (i.e. had to sleep).
674b9b7a4beSMatthew Dillon 			 */
675b9b7a4beSMatthew Dillon 			tscan += vm_object_page_collect_flush(object, p, curgeneration, pagerflags);
676b9b7a4beSMatthew Dillon 		}
677b9b7a4beSMatthew Dillon 
678b9b7a4beSMatthew Dillon 		/*
679b9b7a4beSMatthew Dillon 		 * If everything was dirty and we flushed it successfully,
680b9b7a4beSMatthew Dillon 		 * and the requested range is not the entire object, we
681b9b7a4beSMatthew Dillon 		 * don't have to mess with CLEANCHK or MIGHTBEDIRTY and can
682b9b7a4beSMatthew Dillon 		 * return immediately.
683b9b7a4beSMatthew Dillon 		 */
684b9b7a4beSMatthew Dillon 		if (tscan >= tend && (tstart || tend < object->size)) {
685b9b7a4beSMatthew Dillon 			vm_object_clear_flag(object, OBJ_CLEANING);
686b9b7a4beSMatthew Dillon 			return;
687b9b7a4beSMatthew Dillon 		}
688b9b7a4beSMatthew Dillon 	}
689b9b7a4beSMatthew Dillon 
690b9b7a4beSMatthew Dillon 	/*
6914f79d873SMatthew Dillon 	 * Generally set CLEANCHK interlock and make the page read-only so
6924f79d873SMatthew Dillon 	 * we can then clear the object flags.
6934f79d873SMatthew Dillon 	 *
6944f79d873SMatthew Dillon 	 * However, if this is a nosync mmap then the object is likely to
6954f79d873SMatthew Dillon 	 * stay dirty so do not mess with the page and do not clear the
6964f79d873SMatthew Dillon 	 * object flags.
6974f79d873SMatthew Dillon 	 */
6984f79d873SMatthew Dillon 
6994f79d873SMatthew Dillon 	clearobjflags = 1;
7004f79d873SMatthew Dillon 
701fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(p, &object->memq, listq) {
702e69763a3SDoug Rabson 		vm_page_flag_set(p, PG_CLEANCHK);
7034f79d873SMatthew Dillon 		if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC))
7044f79d873SMatthew Dillon 			clearobjflags = 0;
7054f79d873SMatthew Dillon 		else
706eaf13dd7SJohn Dyson 			vm_page_protect(p, VM_PROT_READ);
707eaf13dd7SJohn Dyson 	}
708eaf13dd7SJohn Dyson 
7094f79d873SMatthew Dillon 	if (clearobjflags && (tstart == 0) && (tend == object->size)) {
710245df27cSMatthew Dillon 		struct vnode *vp;
711245df27cSMatthew Dillon 
712069e9bc1SDoug Rabson 		vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
713245df27cSMatthew Dillon 		if (object->type == OBJT_VNODE &&
714245df27cSMatthew Dillon 		    (vp = (struct vnode *)object->handle) != NULL) {
715245df27cSMatthew Dillon 			if (vp->v_flag & VOBJDIRTY) {
716245df27cSMatthew Dillon 				mtx_lock(&vp->v_interlock);
717245df27cSMatthew Dillon 				vp->v_flag &= ~VOBJDIRTY;
718245df27cSMatthew Dillon 				mtx_unlock(&vp->v_interlock);
719245df27cSMatthew Dillon 			}
720245df27cSMatthew Dillon 		}
721ec4f9fb0SDavid Greenman 	}
722f6b04d2bSDavid Greenman 
723bd7e5f99SJohn Dyson rescan:
7242d8acc0fSJohn Dyson 	curgeneration = object->generation;
7252d8acc0fSJohn Dyson 
726b18bfc3dSJohn Dyson 	for (p = TAILQ_FIRST(&object->memq); p; p = np) {
727b9b7a4beSMatthew Dillon 		int n;
728b9b7a4beSMatthew Dillon 
729b18bfc3dSJohn Dyson 		np = TAILQ_NEXT(p, listq);
730bd7e5f99SJohn Dyson 
731b9b7a4beSMatthew Dillon again:
732bd7e5f99SJohn Dyson 		pi = p->pindex;
733bd7e5f99SJohn Dyson 		if (((p->flags & PG_CLEANCHK) == 0) ||
734bd7e5f99SJohn Dyson 			(pi < tstart) || (pi >= tend) ||
7355070c7f8SJohn Dyson 			(p->valid == 0) ||
7365070c7f8SJohn Dyson 			((p->queue - p->pc) == PQ_CACHE)) {
737e69763a3SDoug Rabson 			vm_page_flag_clear(p, PG_CLEANCHK);
738aef922f5SJohn Dyson 			continue;
739f6b04d2bSDavid Greenman 		}
740f6b04d2bSDavid Greenman 
741bd7e5f99SJohn Dyson 		vm_page_test_dirty(p);
742bd7e5f99SJohn Dyson 		if ((p->dirty & p->valid) == 0) {
743e69763a3SDoug Rabson 			vm_page_flag_clear(p, PG_CLEANCHK);
744bd7e5f99SJohn Dyson 			continue;
745bd7e5f99SJohn Dyson 		}
746ec4f9fb0SDavid Greenman 
7474f79d873SMatthew Dillon 		/*
7484f79d873SMatthew Dillon 		 * If we have been asked to skip nosync pages and this is a
7494f79d873SMatthew Dillon 		 * nosync page, skip it.  Note that the object flags were
7504f79d873SMatthew Dillon 		 * not cleared in this case so we do not have to set them.
7514f79d873SMatthew Dillon 		 */
7524f79d873SMatthew Dillon 		if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
7534f79d873SMatthew Dillon 			vm_page_flag_clear(p, PG_CLEANCHK);
7544f79d873SMatthew Dillon 			continue;
7554f79d873SMatthew Dillon 		}
7564f79d873SMatthew Dillon 
757b9b7a4beSMatthew Dillon 		n = vm_object_page_collect_flush(object, p,
758b9b7a4beSMatthew Dillon 			curgeneration, pagerflags);
759b9b7a4beSMatthew Dillon 		if (n == 0)
760b9b7a4beSMatthew Dillon 			goto rescan;
761b9b7a4beSMatthew Dillon 
762b9b7a4beSMatthew Dillon 		if (object->generation != curgeneration)
763b9b7a4beSMatthew Dillon 			goto rescan;
764b9b7a4beSMatthew Dillon 
765b9b7a4beSMatthew Dillon 		/*
766b9b7a4beSMatthew Dillon 		 * Try to optimize the next page.  If we can't we pick up
767b9b7a4beSMatthew Dillon 		 * our (random) scan where we left off.
768b9b7a4beSMatthew Dillon 		 */
769b9b7a4beSMatthew Dillon 		if (msync_flush_flags & MSYNC_FLUSH_SOFTSEQ) {
770b9b7a4beSMatthew Dillon 			if ((p = vm_page_lookup(object, pi + n)) != NULL)
771b9b7a4beSMatthew Dillon 				goto again;
772b9b7a4beSMatthew Dillon 		}
773b9b7a4beSMatthew Dillon 	}
774b9b7a4beSMatthew Dillon 
775b9b7a4beSMatthew Dillon #if 0
776b9b7a4beSMatthew Dillon 	VOP_FSYNC(vp, NULL, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
777b9b7a4beSMatthew Dillon #endif
778b9b7a4beSMatthew Dillon 
779b9b7a4beSMatthew Dillon 	vm_object_clear_flag(object, OBJ_CLEANING);
780b9b7a4beSMatthew Dillon 	return;
781b9b7a4beSMatthew Dillon }
782b9b7a4beSMatthew Dillon 
783b9b7a4beSMatthew Dillon static int
784b9b7a4beSMatthew Dillon vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags)
785b9b7a4beSMatthew Dillon {
786b9b7a4beSMatthew Dillon 	int runlen;
787b9b7a4beSMatthew Dillon 	int s;
788b9b7a4beSMatthew Dillon 	int maxf;
789b9b7a4beSMatthew Dillon 	int chkb;
790b9b7a4beSMatthew Dillon 	int maxb;
791b9b7a4beSMatthew Dillon 	int i;
792b9b7a4beSMatthew Dillon 	vm_pindex_t pi;
793b9b7a4beSMatthew Dillon 	vm_page_t maf[vm_pageout_page_count];
794b9b7a4beSMatthew Dillon 	vm_page_t mab[vm_pageout_page_count];
795b9b7a4beSMatthew Dillon 	vm_page_t ma[vm_pageout_page_count];
796b9b7a4beSMatthew Dillon 
797b18bfc3dSJohn Dyson 	s = splvm();
798b9b7a4beSMatthew Dillon 	pi = p->pindex;
7991c7c3c6aSMatthew Dillon 	while (vm_page_sleep_busy(p, TRUE, "vpcwai")) {
8002d8acc0fSJohn Dyson 		if (object->generation != curgeneration) {
801f6b04d2bSDavid Greenman 			splx(s);
802b9b7a4beSMatthew Dillon 			return(0);
803f6b04d2bSDavid Greenman 		}
8042d8acc0fSJohn Dyson 	}
805f6b04d2bSDavid Greenman 
806bd7e5f99SJohn Dyson 	maxf = 0;
807bd7e5f99SJohn Dyson 	for(i = 1; i < vm_pageout_page_count; i++) {
808b9b7a4beSMatthew Dillon 		vm_page_t tp;
809b9b7a4beSMatthew Dillon 
8108aef1712SMatthew Dillon 		if ((tp = vm_page_lookup(object, pi + i)) != NULL) {
811bd7e5f99SJohn Dyson 			if ((tp->flags & PG_BUSY) ||
812ffc82b0aSJohn Dyson 				(tp->flags & PG_CLEANCHK) == 0 ||
813ffc82b0aSJohn Dyson 				(tp->busy != 0))
814bd7e5f99SJohn Dyson 				break;
8155070c7f8SJohn Dyson 			if((tp->queue - tp->pc) == PQ_CACHE) {
816e69763a3SDoug Rabson 				vm_page_flag_clear(tp, PG_CLEANCHK);
8173077a9c2SJohn Dyson 				break;
8183077a9c2SJohn Dyson 			}
819bd7e5f99SJohn Dyson 			vm_page_test_dirty(tp);
820bd7e5f99SJohn Dyson 			if ((tp->dirty & tp->valid) == 0) {
821e69763a3SDoug Rabson 				vm_page_flag_clear(tp, PG_CLEANCHK);
822bd7e5f99SJohn Dyson 				break;
823bd7e5f99SJohn Dyson 			}
824bd7e5f99SJohn Dyson 			maf[ i - 1 ] = tp;
825bd7e5f99SJohn Dyson 			maxf++;
826bd7e5f99SJohn Dyson 			continue;
827bd7e5f99SJohn Dyson 		}
828bd7e5f99SJohn Dyson 		break;
829bd7e5f99SJohn Dyson 	}
830aef922f5SJohn Dyson 
831bd7e5f99SJohn Dyson 	maxb = 0;
832bd7e5f99SJohn Dyson 	chkb = vm_pageout_page_count -  maxf;
833bd7e5f99SJohn Dyson 	if (chkb) {
834bd7e5f99SJohn Dyson 		for(i = 1; i < chkb;i++) {
835b9b7a4beSMatthew Dillon 			vm_page_t tp;
836b9b7a4beSMatthew Dillon 
8378aef1712SMatthew Dillon 			if ((tp = vm_page_lookup(object, pi - i)) != NULL) {
838bd7e5f99SJohn Dyson 				if ((tp->flags & PG_BUSY) ||
839ffc82b0aSJohn Dyson 					(tp->flags & PG_CLEANCHK) == 0 ||
840ffc82b0aSJohn Dyson 					(tp->busy != 0))
841bd7e5f99SJohn Dyson 					break;
8425070c7f8SJohn Dyson 				if((tp->queue - tp->pc) == PQ_CACHE) {
843e69763a3SDoug Rabson 					vm_page_flag_clear(tp, PG_CLEANCHK);
8443077a9c2SJohn Dyson 					break;
8453077a9c2SJohn Dyson 				}
846bd7e5f99SJohn Dyson 				vm_page_test_dirty(tp);
847bd7e5f99SJohn Dyson 				if ((tp->dirty & tp->valid) == 0) {
848e69763a3SDoug Rabson 					vm_page_flag_clear(tp, PG_CLEANCHK);
849bd7e5f99SJohn Dyson 					break;
850bd7e5f99SJohn Dyson 				}
851bd7e5f99SJohn Dyson 				mab[ i - 1 ] = tp;
852bd7e5f99SJohn Dyson 				maxb++;
853bd7e5f99SJohn Dyson 				continue;
854bd7e5f99SJohn Dyson 			}
855bd7e5f99SJohn Dyson 			break;
856bd7e5f99SJohn Dyson 		}
857bd7e5f99SJohn Dyson 	}
858bd7e5f99SJohn Dyson 
859bd7e5f99SJohn Dyson 	for(i = 0; i < maxb; i++) {
860bd7e5f99SJohn Dyson 		int index = (maxb - i) - 1;
861bd7e5f99SJohn Dyson 		ma[index] = mab[i];
862e69763a3SDoug Rabson 		vm_page_flag_clear(ma[index], PG_CLEANCHK);
863bd7e5f99SJohn Dyson 	}
864e69763a3SDoug Rabson 	vm_page_flag_clear(p, PG_CLEANCHK);
865bd7e5f99SJohn Dyson 	ma[maxb] = p;
866bd7e5f99SJohn Dyson 	for(i = 0; i < maxf; i++) {
867bd7e5f99SJohn Dyson 		int index = (maxb + i) + 1;
868bd7e5f99SJohn Dyson 		ma[index] = maf[i];
869e69763a3SDoug Rabson 		vm_page_flag_clear(ma[index], PG_CLEANCHK);
870f6b04d2bSDavid Greenman 	}
871bd7e5f99SJohn Dyson 	runlen = maxb + maxf + 1;
872cf2819ccSJohn Dyson 
873f35329acSJohn Dyson 	splx(s);
8748f9110f6SJohn Dyson 	vm_pageout_flush(ma, runlen, pagerflags);
875cf2819ccSJohn Dyson 	for (i = 0; i < runlen; i++) {
876cf2819ccSJohn Dyson 		if (ma[i]->valid & ma[i]->dirty) {
877cf2819ccSJohn Dyson 			vm_page_protect(ma[i], VM_PROT_READ);
878e69763a3SDoug Rabson 			vm_page_flag_set(ma[i], PG_CLEANCHK);
879aef922f5SJohn Dyson 
880b9b7a4beSMatthew Dillon 			/*
881b9b7a4beSMatthew Dillon 			 * maxf will end up being the actual number of pages
882b9b7a4beSMatthew Dillon 			 * we wrote out contiguously, non-inclusive of the
883b9b7a4beSMatthew Dillon 			 * first page.  We do not count look-behind pages.
884b9b7a4beSMatthew Dillon 			 */
885b9b7a4beSMatthew Dillon 			if (i >= maxb + 1 && (maxf > i - maxb - 1))
886b9b7a4beSMatthew Dillon 				maxf = i - maxb - 1;
887b9b7a4beSMatthew Dillon 		}
888b9b7a4beSMatthew Dillon 	}
889b9b7a4beSMatthew Dillon 	return(maxf + 1);
89026f9a767SRodney W. Grimes }
891df8bae1dSRodney W. Grimes 
892df8bae1dSRodney W. Grimes /*
8938e3ad7c9SMatthew Dillon  * Same as vm_object_pmap_copy, except range checking really
8941efb74fbSJohn Dyson  * works, and is meant for small sections of an object.
8958e3ad7c9SMatthew Dillon  *
8968e3ad7c9SMatthew Dillon  * This code protects resident pages by making them read-only
8978e3ad7c9SMatthew Dillon  * and is typically called on a fork or split when a page
8988e3ad7c9SMatthew Dillon  * is converted to copy-on-write.
8998e3ad7c9SMatthew Dillon  *
9008e3ad7c9SMatthew Dillon  * NOTE: If the page is already at VM_PROT_NONE, calling
9018e3ad7c9SMatthew Dillon  * vm_page_protect will have no effect.
9021efb74fbSJohn Dyson  */
9038e3ad7c9SMatthew Dillon 
9041efb74fbSJohn Dyson void
9051b40f8c0SMatthew Dillon vm_object_pmap_copy_1(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
9061efb74fbSJohn Dyson {
9071efb74fbSJohn Dyson 	vm_pindex_t idx;
908d031cff1SMatthew Dillon 	vm_page_t p;
9091efb74fbSJohn Dyson 
9100cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
9110cddd8f0SMatthew Dillon 
9121efb74fbSJohn Dyson 	if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
9131efb74fbSJohn Dyson 		return;
9141efb74fbSJohn Dyson 
9151efb74fbSJohn Dyson 	for (idx = start; idx < end; idx++) {
9161efb74fbSJohn Dyson 		p = vm_page_lookup(object, idx);
9171efb74fbSJohn Dyson 		if (p == NULL)
9181efb74fbSJohn Dyson 			continue;
9191efb74fbSJohn Dyson 		vm_page_protect(p, VM_PROT_READ);
9201efb74fbSJohn Dyson 	}
9211efb74fbSJohn Dyson }
9221efb74fbSJohn Dyson 
9231efb74fbSJohn Dyson /*
924df8bae1dSRodney W. Grimes  *	vm_object_pmap_remove:
925df8bae1dSRodney W. Grimes  *
926df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
927df8bae1dSRodney W. Grimes  *	object range from all physical maps.
928df8bae1dSRodney W. Grimes  *
929df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
930df8bae1dSRodney W. Grimes  */
93126f9a767SRodney W. Grimes void
9321b40f8c0SMatthew Dillon vm_object_pmap_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
933df8bae1dSRodney W. Grimes {
934d031cff1SMatthew Dillon 	vm_page_t p;
935d031cff1SMatthew Dillon 
9360cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
937df8bae1dSRodney W. Grimes 	if (object == NULL)
938df8bae1dSRodney W. Grimes 		return;
939cc64b484SAlfred Perlstein 	TAILQ_FOREACH(p, &object->memq, listq) {
940bd7e5f99SJohn Dyson 		if (p->pindex >= start && p->pindex < end)
941f919ebdeSDavid Greenman 			vm_page_protect(p, VM_PROT_NONE);
94226f9a767SRodney W. Grimes 	}
9436e20a165SJohn Dyson 	if ((start == 0) && (object->size == end))
944069e9bc1SDoug Rabson 		vm_object_clear_flag(object, OBJ_WRITEABLE);
94526f9a767SRodney W. Grimes }
946df8bae1dSRodney W. Grimes 
947df8bae1dSRodney W. Grimes /*
948867a482dSJohn Dyson  *	vm_object_madvise:
949867a482dSJohn Dyson  *
950867a482dSJohn Dyson  *	Implements the madvise function at the object/page level.
9511c7c3c6aSMatthew Dillon  *
952193b9358SAlan Cox  *	MADV_WILLNEED	(any object)
953193b9358SAlan Cox  *
954193b9358SAlan Cox  *	    Activate the specified pages if they are resident.
955193b9358SAlan Cox  *
956193b9358SAlan Cox  *	MADV_DONTNEED	(any object)
957193b9358SAlan Cox  *
958193b9358SAlan Cox  *	    Deactivate the specified pages if they are resident.
959193b9358SAlan Cox  *
960193b9358SAlan Cox  *	MADV_FREE	(OBJT_DEFAULT/OBJT_SWAP objects,
961193b9358SAlan Cox  *			 OBJ_ONEMAPPING only)
962193b9358SAlan Cox  *
963193b9358SAlan Cox  *	    Deactivate and clean the specified pages if they are
964193b9358SAlan Cox  *	    resident.  This permits the process to reuse the pages
965193b9358SAlan Cox  *	    without faulting or the kernel to reclaim the pages
966193b9358SAlan Cox  *	    without I/O.
967867a482dSJohn Dyson  */
968867a482dSJohn Dyson void
9691b40f8c0SMatthew Dillon vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
970867a482dSJohn Dyson {
9716e20a165SJohn Dyson 	vm_pindex_t end, tpindex;
9726e20a165SJohn Dyson 	vm_object_t tobject;
973867a482dSJohn Dyson 	vm_page_t m;
974867a482dSJohn Dyson 
9750cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
976867a482dSJohn Dyson 	if (object == NULL)
977867a482dSJohn Dyson 		return;
978867a482dSJohn Dyson 
979867a482dSJohn Dyson 	end = pindex + count;
980867a482dSJohn Dyson 
9811c7c3c6aSMatthew Dillon 	/*
9821c7c3c6aSMatthew Dillon 	 * Locate and adjust resident pages
9831c7c3c6aSMatthew Dillon 	 */
9841c7c3c6aSMatthew Dillon 
9851c7c3c6aSMatthew Dillon 	for (; pindex < end; pindex += 1) {
9866e20a165SJohn Dyson relookup:
9876e20a165SJohn Dyson 		tobject = object;
9886e20a165SJohn Dyson 		tpindex = pindex;
9896e20a165SJohn Dyson shadowlookup:
99058b4e6ccSAlan Cox 		/*
99158b4e6ccSAlan Cox 		 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
99258b4e6ccSAlan Cox 		 * and those pages must be OBJ_ONEMAPPING.
99358b4e6ccSAlan Cox 		 */
99458b4e6ccSAlan Cox 		if (advise == MADV_FREE) {
99558b4e6ccSAlan Cox 			if ((tobject->type != OBJT_DEFAULT &&
99658b4e6ccSAlan Cox 			     tobject->type != OBJT_SWAP) ||
99758b4e6ccSAlan Cox 			    (tobject->flags & OBJ_ONEMAPPING) == 0) {
9986e20a165SJohn Dyson 				continue;
9996e20a165SJohn Dyson 			}
100058b4e6ccSAlan Cox 		}
10011c7c3c6aSMatthew Dillon 
10021c7c3c6aSMatthew Dillon 		m = vm_page_lookup(tobject, tpindex);
10031c7c3c6aSMatthew Dillon 
10041c7c3c6aSMatthew Dillon 		if (m == NULL) {
10051ce137beSMatthew Dillon 			/*
10061ce137beSMatthew Dillon 			 * There may be swap even if there is no backing page
10071ce137beSMatthew Dillon 			 */
10081ce137beSMatthew Dillon 			if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
10091ce137beSMatthew Dillon 				swap_pager_freespace(tobject, tpindex, 1);
10101ce137beSMatthew Dillon 
10111ce137beSMatthew Dillon 			/*
10121ce137beSMatthew Dillon 			 * next object
10131ce137beSMatthew Dillon 			 */
10146e20a165SJohn Dyson 			tobject = tobject->backing_object;
10151c7c3c6aSMatthew Dillon 			if (tobject == NULL)
10161c7c3c6aSMatthew Dillon 				continue;
10176e20a165SJohn Dyson 			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
10186e20a165SJohn Dyson 			goto shadowlookup;
10196e20a165SJohn Dyson 		}
1020867a482dSJohn Dyson 
1021867a482dSJohn Dyson 		/*
1022867a482dSJohn Dyson 		 * If the page is busy or not in a normal active state,
10238b03c8edSMatthew Dillon 		 * we skip it.  If the page is not managed there are no
10248b03c8edSMatthew Dillon 		 * page queues to mess with.  Things can break if we mess
10258b03c8edSMatthew Dillon 		 * with pages in any of the below states.
1026867a482dSJohn Dyson 		 */
10271c7c3c6aSMatthew Dillon 		if (
10281c7c3c6aSMatthew Dillon 		    m->hold_count ||
10291c7c3c6aSMatthew Dillon 		    m->wire_count ||
10308b03c8edSMatthew Dillon 		    (m->flags & PG_UNMANAGED) ||
10311c7c3c6aSMatthew Dillon 		    m->valid != VM_PAGE_BITS_ALL
10321c7c3c6aSMatthew Dillon 		) {
1033867a482dSJohn Dyson 			continue;
10346e20a165SJohn Dyson 		}
10356e20a165SJohn Dyson 
10361c7c3c6aSMatthew Dillon  		if (vm_page_sleep_busy(m, TRUE, "madvpo"))
10376e20a165SJohn Dyson   			goto relookup;
1038867a482dSJohn Dyson 
1039867a482dSJohn Dyson 		if (advise == MADV_WILLNEED) {
1040867a482dSJohn Dyson 			vm_page_activate(m);
10416e20a165SJohn Dyson 		} else if (advise == MADV_DONTNEED) {
1042479112dfSMatthew Dillon 			vm_page_dontneed(m);
10430a47b48bSJohn Dyson 		} else if (advise == MADV_FREE) {
10441c7c3c6aSMatthew Dillon 			/*
10452aaeadf8SMatthew Dillon 			 * Mark the page clean.  This will allow the page
10462aaeadf8SMatthew Dillon 			 * to be freed up by the system.  However, such pages
10472aaeadf8SMatthew Dillon 			 * are often reused quickly by malloc()/free()
10482aaeadf8SMatthew Dillon 			 * so we do not do anything that would cause
10492aaeadf8SMatthew Dillon 			 * a page fault if we can help it.
10502aaeadf8SMatthew Dillon 			 *
10512aaeadf8SMatthew Dillon 			 * Specifically, we do not try to actually free
10522aaeadf8SMatthew Dillon 			 * the page now nor do we try to put it in the
10532aaeadf8SMatthew Dillon 			 * cache (which would cause a page fault on reuse).
105441c67e12SMatthew Dillon 			 *
105541c67e12SMatthew Dillon 			 * But we do make the page is freeable as we
105641c67e12SMatthew Dillon 			 * can without actually taking the step of unmapping
105741c67e12SMatthew Dillon 			 * it.
10581c7c3c6aSMatthew Dillon 			 */
10590385347cSPeter Wemm 			pmap_clear_modify(m);
10606e20a165SJohn Dyson 			m->dirty = 0;
106141c67e12SMatthew Dillon 			m->act_count = 0;
1062479112dfSMatthew Dillon 			vm_page_dontneed(m);
10631ce137beSMatthew Dillon 			if (tobject->type == OBJT_SWAP)
10641ce137beSMatthew Dillon 				swap_pager_freespace(tobject, tpindex, 1);
1065867a482dSJohn Dyson 		}
1066867a482dSJohn Dyson 	}
1067867a482dSJohn Dyson }
1068867a482dSJohn Dyson 
1069867a482dSJohn Dyson /*
1070df8bae1dSRodney W. Grimes  *	vm_object_shadow:
1071df8bae1dSRodney W. Grimes  *
1072df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
1073df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
1074df8bae1dSRodney W. Grimes  *	object reference is deallocated.
1075df8bae1dSRodney W. Grimes  *
1076df8bae1dSRodney W. Grimes  *	The new object and offset into that object
1077df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
1078df8bae1dSRodney W. Grimes  */
1079df8bae1dSRodney W. Grimes 
108026f9a767SRodney W. Grimes void
10811b40f8c0SMatthew Dillon vm_object_shadow(
10821b40f8c0SMatthew Dillon 	vm_object_t *object,	/* IN/OUT */
10831b40f8c0SMatthew Dillon 	vm_ooffset_t *offset,	/* IN/OUT */
10841b40f8c0SMatthew Dillon 	vm_size_t length)
1085df8bae1dSRodney W. Grimes {
1086d031cff1SMatthew Dillon 	vm_object_t source;
1087d031cff1SMatthew Dillon 	vm_object_t result;
1088df8bae1dSRodney W. Grimes 
10890cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
1090df8bae1dSRodney W. Grimes 	source = *object;
1091df8bae1dSRodney W. Grimes 
1092df8bae1dSRodney W. Grimes 	/*
10939a2f6362SAlan Cox 	 * Don't create the new object if the old object isn't shared.
10949a2f6362SAlan Cox 	 */
10959a2f6362SAlan Cox 
1096c7997d57SAlan Cox 	if (source != NULL &&
1097c7997d57SAlan Cox 	    source->ref_count == 1 &&
10989a2f6362SAlan Cox 	    source->handle == NULL &&
10999a2f6362SAlan Cox 	    (source->type == OBJT_DEFAULT ||
11009a2f6362SAlan Cox 	     source->type == OBJT_SWAP))
11019a2f6362SAlan Cox 		return;
11029a2f6362SAlan Cox 
11039a2f6362SAlan Cox 	/*
1104df8bae1dSRodney W. Grimes 	 * Allocate a new object with the given length
1105df8bae1dSRodney W. Grimes 	 */
1106971dd342SAlfred Perlstein 	result = vm_object_allocate(OBJT_DEFAULT, length);
1107971dd342SAlfred Perlstein 	KASSERT(result != NULL, ("vm_object_shadow: no object for shadowing"));
1108df8bae1dSRodney W. Grimes 
1109df8bae1dSRodney W. Grimes 	/*
11100d94caffSDavid Greenman 	 * The new object shadows the source object, adding a reference to it.
11110d94caffSDavid Greenman 	 * Our caller changes his reference to point to the new object,
11120d94caffSDavid Greenman 	 * removing a reference to the source object.  Net result: no change
11130d94caffSDavid Greenman 	 * of reference count.
11149b09fe24SMatthew Dillon 	 *
11159b09fe24SMatthew Dillon 	 * Try to optimize the result object's page color when shadowing
1116956f3135SPhilippe Charnier 	 * in order to maintain page coloring consistency in the combined
11179b09fe24SMatthew Dillon 	 * shadowed object.
1118df8bae1dSRodney W. Grimes 	 */
111924a1cce3SDavid Greenman 	result->backing_object = source;
1120de5f6a77SJohn Dyson 	if (source) {
1121de5f6a77SJohn Dyson 		TAILQ_INSERT_TAIL(&source->shadow_head, result, shadow_list);
1122eaf13dd7SJohn Dyson 		source->shadow_count++;
1123eaf13dd7SJohn Dyson 		source->generation++;
11249b09fe24SMatthew Dillon 		result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) & PQ_L2_MASK;
1125de5f6a77SJohn Dyson 	}
1126df8bae1dSRodney W. Grimes 
1127df8bae1dSRodney W. Grimes 	/*
11280d94caffSDavid Greenman 	 * Store the offset into the source object, and fix up the offset into
11290d94caffSDavid Greenman 	 * the new object.
1130df8bae1dSRodney W. Grimes 	 */
1131df8bae1dSRodney W. Grimes 
113224a1cce3SDavid Greenman 	result->backing_object_offset = *offset;
1133df8bae1dSRodney W. Grimes 
1134df8bae1dSRodney W. Grimes 	/*
1135df8bae1dSRodney W. Grimes 	 * Return the new things
1136df8bae1dSRodney W. Grimes 	 */
1137df8bae1dSRodney W. Grimes 
1138df8bae1dSRodney W. Grimes 	*offset = 0;
1139df8bae1dSRodney W. Grimes 	*object = result;
1140df8bae1dSRodney W. Grimes }
1141df8bae1dSRodney W. Grimes 
11422ad1a3f7SMatthew Dillon #define	OBSC_TEST_ALL_SHADOWED	0x0001
11432ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_NOWAIT	0x0002
11442ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_WAIT	0x0004
11452ad1a3f7SMatthew Dillon 
11462ad1a3f7SMatthew Dillon static __inline int
11472ad1a3f7SMatthew Dillon vm_object_backing_scan(vm_object_t object, int op)
11482ad1a3f7SMatthew Dillon {
11492ad1a3f7SMatthew Dillon 	int s;
11502ad1a3f7SMatthew Dillon 	int r = 1;
11512ad1a3f7SMatthew Dillon 	vm_page_t p;
11522ad1a3f7SMatthew Dillon 	vm_object_t backing_object;
11532ad1a3f7SMatthew Dillon 	vm_pindex_t backing_offset_index;
11542ad1a3f7SMatthew Dillon 
11552ad1a3f7SMatthew Dillon 	s = splvm();
11560cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
11572ad1a3f7SMatthew Dillon 
11582ad1a3f7SMatthew Dillon 	backing_object = object->backing_object;
11592ad1a3f7SMatthew Dillon 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
11602ad1a3f7SMatthew Dillon 
11612ad1a3f7SMatthew Dillon 	/*
11622ad1a3f7SMatthew Dillon 	 * Initial conditions
11632ad1a3f7SMatthew Dillon 	 */
11642ad1a3f7SMatthew Dillon 
11652ad1a3f7SMatthew Dillon 	if (op & OBSC_TEST_ALL_SHADOWED) {
11662ad1a3f7SMatthew Dillon 		/*
1167956f3135SPhilippe Charnier 		 * We do not want to have to test for the existence of
11682ad1a3f7SMatthew Dillon 		 * swap pages in the backing object.  XXX but with the
11692ad1a3f7SMatthew Dillon 		 * new swapper this would be pretty easy to do.
11702ad1a3f7SMatthew Dillon 		 *
11712ad1a3f7SMatthew Dillon 		 * XXX what about anonymous MAP_SHARED memory that hasn't
11722ad1a3f7SMatthew Dillon 		 * been ZFOD faulted yet?  If we do not test for this, the
11732ad1a3f7SMatthew Dillon 		 * shadow test may succeed! XXX
11742ad1a3f7SMatthew Dillon 		 */
11752ad1a3f7SMatthew Dillon 		if (backing_object->type != OBJT_DEFAULT) {
11762ad1a3f7SMatthew Dillon 			splx(s);
11772ad1a3f7SMatthew Dillon 			return(0);
11782ad1a3f7SMatthew Dillon 		}
11792ad1a3f7SMatthew Dillon 	}
11802ad1a3f7SMatthew Dillon 	if (op & OBSC_COLLAPSE_WAIT) {
11812ad1a3f7SMatthew Dillon 		vm_object_set_flag(backing_object, OBJ_DEAD);
11822ad1a3f7SMatthew Dillon 	}
11832ad1a3f7SMatthew Dillon 
11842ad1a3f7SMatthew Dillon 	/*
11852ad1a3f7SMatthew Dillon 	 * Our scan
11862ad1a3f7SMatthew Dillon 	 */
11872ad1a3f7SMatthew Dillon 
11882ad1a3f7SMatthew Dillon 	p = TAILQ_FIRST(&backing_object->memq);
11892ad1a3f7SMatthew Dillon 	while (p) {
11902ad1a3f7SMatthew Dillon 		vm_page_t next = TAILQ_NEXT(p, listq);
11912ad1a3f7SMatthew Dillon 		vm_pindex_t new_pindex = p->pindex - backing_offset_index;
11922ad1a3f7SMatthew Dillon 
11932ad1a3f7SMatthew Dillon 		if (op & OBSC_TEST_ALL_SHADOWED) {
11942ad1a3f7SMatthew Dillon 			vm_page_t pp;
11952ad1a3f7SMatthew Dillon 
11962ad1a3f7SMatthew Dillon 			/*
11972ad1a3f7SMatthew Dillon 			 * Ignore pages outside the parent object's range
11982ad1a3f7SMatthew Dillon 			 * and outside the parent object's mapping of the
11992ad1a3f7SMatthew Dillon 			 * backing object.
12002ad1a3f7SMatthew Dillon 			 *
12012ad1a3f7SMatthew Dillon 			 * note that we do not busy the backing object's
12022ad1a3f7SMatthew Dillon 			 * page.
12032ad1a3f7SMatthew Dillon 			 */
12042ad1a3f7SMatthew Dillon 
12052ad1a3f7SMatthew Dillon 			if (
12062ad1a3f7SMatthew Dillon 			    p->pindex < backing_offset_index ||
12072ad1a3f7SMatthew Dillon 			    new_pindex >= object->size
12082ad1a3f7SMatthew Dillon 			) {
12092ad1a3f7SMatthew Dillon 				p = next;
12102ad1a3f7SMatthew Dillon 				continue;
12112ad1a3f7SMatthew Dillon 			}
12122ad1a3f7SMatthew Dillon 
12132ad1a3f7SMatthew Dillon 			/*
12142ad1a3f7SMatthew Dillon 			 * See if the parent has the page or if the parent's
12152ad1a3f7SMatthew Dillon 			 * object pager has the page.  If the parent has the
12162ad1a3f7SMatthew Dillon 			 * page but the page is not valid, the parent's
12172ad1a3f7SMatthew Dillon 			 * object pager must have the page.
12182ad1a3f7SMatthew Dillon 			 *
12192ad1a3f7SMatthew Dillon 			 * If this fails, the parent does not completely shadow
12202ad1a3f7SMatthew Dillon 			 * the object and we might as well give up now.
12212ad1a3f7SMatthew Dillon 			 */
12222ad1a3f7SMatthew Dillon 
12232ad1a3f7SMatthew Dillon 			pp = vm_page_lookup(object, new_pindex);
12242ad1a3f7SMatthew Dillon 			if (
12252ad1a3f7SMatthew Dillon 			    (pp == NULL || pp->valid == 0) &&
12262ad1a3f7SMatthew Dillon 			    !vm_pager_has_page(object, new_pindex, NULL, NULL)
12272ad1a3f7SMatthew Dillon 			) {
12282ad1a3f7SMatthew Dillon 				r = 0;
12292ad1a3f7SMatthew Dillon 				break;
12302ad1a3f7SMatthew Dillon 			}
12312ad1a3f7SMatthew Dillon 		}
12322ad1a3f7SMatthew Dillon 
12332ad1a3f7SMatthew Dillon 		/*
12342ad1a3f7SMatthew Dillon 		 * Check for busy page
12352ad1a3f7SMatthew Dillon 		 */
12362ad1a3f7SMatthew Dillon 
12372ad1a3f7SMatthew Dillon 		if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
12382ad1a3f7SMatthew Dillon 			vm_page_t pp;
12392ad1a3f7SMatthew Dillon 
12402ad1a3f7SMatthew Dillon 			if (op & OBSC_COLLAPSE_NOWAIT) {
12412ad1a3f7SMatthew Dillon 				if (
12422ad1a3f7SMatthew Dillon 				    (p->flags & PG_BUSY) ||
12432ad1a3f7SMatthew Dillon 				    !p->valid ||
12442ad1a3f7SMatthew Dillon 				    p->hold_count ||
12452ad1a3f7SMatthew Dillon 				    p->wire_count ||
12462ad1a3f7SMatthew Dillon 				    p->busy
12472ad1a3f7SMatthew Dillon 				) {
12482ad1a3f7SMatthew Dillon 					p = next;
12492ad1a3f7SMatthew Dillon 					continue;
12502ad1a3f7SMatthew Dillon 				}
12512ad1a3f7SMatthew Dillon 			} else if (op & OBSC_COLLAPSE_WAIT) {
12522ad1a3f7SMatthew Dillon 				if (vm_page_sleep_busy(p, TRUE, "vmocol")) {
12532ad1a3f7SMatthew Dillon 					/*
12542ad1a3f7SMatthew Dillon 					 * If we slept, anything could have
12552ad1a3f7SMatthew Dillon 					 * happened.  Since the object is
12562ad1a3f7SMatthew Dillon 					 * marked dead, the backing offset
12572ad1a3f7SMatthew Dillon 					 * should not have changed so we
12582ad1a3f7SMatthew Dillon 					 * just restart our scan.
12592ad1a3f7SMatthew Dillon 					 */
12602ad1a3f7SMatthew Dillon 					p = TAILQ_FIRST(&backing_object->memq);
12612ad1a3f7SMatthew Dillon 					continue;
12622ad1a3f7SMatthew Dillon 				}
12632ad1a3f7SMatthew Dillon 			}
12642ad1a3f7SMatthew Dillon 
12652ad1a3f7SMatthew Dillon 			/*
12662ad1a3f7SMatthew Dillon 			 * Busy the page
12672ad1a3f7SMatthew Dillon 			 */
12682ad1a3f7SMatthew Dillon 			vm_page_busy(p);
12692ad1a3f7SMatthew Dillon 
12702ad1a3f7SMatthew Dillon 			KASSERT(
12712ad1a3f7SMatthew Dillon 			    p->object == backing_object,
12722ad1a3f7SMatthew Dillon 			    ("vm_object_qcollapse(): object mismatch")
12732ad1a3f7SMatthew Dillon 			);
12742ad1a3f7SMatthew Dillon 
12752ad1a3f7SMatthew Dillon 			/*
12762ad1a3f7SMatthew Dillon 			 * Destroy any associated swap
12772ad1a3f7SMatthew Dillon 			 */
12782ad1a3f7SMatthew Dillon 			if (backing_object->type == OBJT_SWAP) {
12792ad1a3f7SMatthew Dillon 				swap_pager_freespace(
12802ad1a3f7SMatthew Dillon 				    backing_object,
12812ad1a3f7SMatthew Dillon 				    p->pindex,
12822ad1a3f7SMatthew Dillon 				    1
12832ad1a3f7SMatthew Dillon 				);
12842ad1a3f7SMatthew Dillon 			}
12852ad1a3f7SMatthew Dillon 
12862ad1a3f7SMatthew Dillon 			if (
12872ad1a3f7SMatthew Dillon 			    p->pindex < backing_offset_index ||
12882ad1a3f7SMatthew Dillon 			    new_pindex >= object->size
12892ad1a3f7SMatthew Dillon 			) {
12902ad1a3f7SMatthew Dillon 				/*
12912ad1a3f7SMatthew Dillon 				 * Page is out of the parent object's range, we
12922ad1a3f7SMatthew Dillon 				 * can simply destroy it.
12932ad1a3f7SMatthew Dillon 				 */
12942ad1a3f7SMatthew Dillon 				vm_page_protect(p, VM_PROT_NONE);
12952ad1a3f7SMatthew Dillon 				vm_page_free(p);
12962ad1a3f7SMatthew Dillon 				p = next;
12972ad1a3f7SMatthew Dillon 				continue;
12982ad1a3f7SMatthew Dillon 			}
12992ad1a3f7SMatthew Dillon 
13002ad1a3f7SMatthew Dillon 			pp = vm_page_lookup(object, new_pindex);
13012ad1a3f7SMatthew Dillon 			if (
13022ad1a3f7SMatthew Dillon 			    pp != NULL ||
13032ad1a3f7SMatthew Dillon 			    vm_pager_has_page(object, new_pindex, NULL, NULL)
13042ad1a3f7SMatthew Dillon 			) {
13052ad1a3f7SMatthew Dillon 				/*
13062ad1a3f7SMatthew Dillon 				 * page already exists in parent OR swap exists
13072ad1a3f7SMatthew Dillon 				 * for this location in the parent.  Destroy
13082ad1a3f7SMatthew Dillon 				 * the original page from the backing object.
13092ad1a3f7SMatthew Dillon 				 *
13102ad1a3f7SMatthew Dillon 				 * Leave the parent's page alone
13112ad1a3f7SMatthew Dillon 				 */
13122ad1a3f7SMatthew Dillon 				vm_page_protect(p, VM_PROT_NONE);
13132ad1a3f7SMatthew Dillon 				vm_page_free(p);
13142ad1a3f7SMatthew Dillon 				p = next;
13152ad1a3f7SMatthew Dillon 				continue;
13162ad1a3f7SMatthew Dillon 			}
13172ad1a3f7SMatthew Dillon 
13182ad1a3f7SMatthew Dillon 			/*
13192ad1a3f7SMatthew Dillon 			 * Page does not exist in parent, rename the
13202ad1a3f7SMatthew Dillon 			 * page from the backing object to the main object.
1321d1bf5d56SMatthew Dillon 			 *
1322d1bf5d56SMatthew Dillon 			 * If the page was mapped to a process, it can remain
1323d1bf5d56SMatthew Dillon 			 * mapped through the rename.
13242ad1a3f7SMatthew Dillon 			 */
13252ad1a3f7SMatthew Dillon 			if ((p->queue - p->pc) == PQ_CACHE)
13262ad1a3f7SMatthew Dillon 				vm_page_deactivate(p);
13272ad1a3f7SMatthew Dillon 
13282ad1a3f7SMatthew Dillon 			vm_page_rename(p, object, new_pindex);
13292ad1a3f7SMatthew Dillon 			/* page automatically made dirty by rename */
13302ad1a3f7SMatthew Dillon 		}
13312ad1a3f7SMatthew Dillon 		p = next;
13322ad1a3f7SMatthew Dillon 	}
13332ad1a3f7SMatthew Dillon 	splx(s);
13342ad1a3f7SMatthew Dillon 	return(r);
13352ad1a3f7SMatthew Dillon }
13362ad1a3f7SMatthew Dillon 
1337df8bae1dSRodney W. Grimes 
1338df8bae1dSRodney W. Grimes /*
13392fe6e4d7SDavid Greenman  * this version of collapse allows the operation to occur earlier and
13402fe6e4d7SDavid Greenman  * when paging_in_progress is true for an object...  This is not a complete
13412fe6e4d7SDavid Greenman  * operation, but should plug 99.9% of the rest of the leaks.
13422fe6e4d7SDavid Greenman  */
13432fe6e4d7SDavid Greenman static void
13441b40f8c0SMatthew Dillon vm_object_qcollapse(vm_object_t object)
13452fe6e4d7SDavid Greenman {
13462ad1a3f7SMatthew Dillon 	vm_object_t backing_object = object->backing_object;
13472fe6e4d7SDavid Greenman 
13481b40f8c0SMatthew Dillon 	GIANT_REQUIRED;
13491b40f8c0SMatthew Dillon 
13502fe6e4d7SDavid Greenman 	if (backing_object->ref_count != 1)
13512fe6e4d7SDavid Greenman 		return;
13522fe6e4d7SDavid Greenman 
1353010cf3b9SDavid Greenman 	backing_object->ref_count += 2;
1354010cf3b9SDavid Greenman 
13552ad1a3f7SMatthew Dillon 	vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
13561c7c3c6aSMatthew Dillon 
1357010cf3b9SDavid Greenman 	backing_object->ref_count -= 2;
13582fe6e4d7SDavid Greenman }
13592fe6e4d7SDavid Greenman 
1360df8bae1dSRodney W. Grimes /*
1361df8bae1dSRodney W. Grimes  *	vm_object_collapse:
1362df8bae1dSRodney W. Grimes  *
1363df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
1364df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
1365df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
1366df8bae1dSRodney W. Grimes  */
136726f9a767SRodney W. Grimes void
13681b40f8c0SMatthew Dillon vm_object_collapse(vm_object_t object)
1369df8bae1dSRodney W. Grimes {
13700cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
137123955314SAlfred Perlstein 
1372df8bae1dSRodney W. Grimes 	while (TRUE) {
13732ad1a3f7SMatthew Dillon 		vm_object_t backing_object;
13742ad1a3f7SMatthew Dillon 
1375df8bae1dSRodney W. Grimes 		/*
1376df8bae1dSRodney W. Grimes 		 * Verify that the conditions are right for collapse:
1377df8bae1dSRodney W. Grimes 		 *
13782ad1a3f7SMatthew Dillon 		 * The object exists and the backing object exists.
1379df8bae1dSRodney W. Grimes 		 */
13802fe6e4d7SDavid Greenman 		if (object == NULL)
13812ad1a3f7SMatthew Dillon 			break;
1382df8bae1dSRodney W. Grimes 
138324a1cce3SDavid Greenman 		if ((backing_object = object->backing_object) == NULL)
13842ad1a3f7SMatthew Dillon 			break;
1385df8bae1dSRodney W. Grimes 
1386f919ebdeSDavid Greenman 		/*
1387f919ebdeSDavid Greenman 		 * we check the backing object first, because it is most likely
138824a1cce3SDavid Greenman 		 * not collapsable.
1389f919ebdeSDavid Greenman 		 */
139024a1cce3SDavid Greenman 		if (backing_object->handle != NULL ||
139124a1cce3SDavid Greenman 		    (backing_object->type != OBJT_DEFAULT &&
139224a1cce3SDavid Greenman 		     backing_object->type != OBJT_SWAP) ||
1393f919ebdeSDavid Greenman 		    (backing_object->flags & OBJ_DEAD) ||
139424a1cce3SDavid Greenman 		    object->handle != NULL ||
139524a1cce3SDavid Greenman 		    (object->type != OBJT_DEFAULT &&
139624a1cce3SDavid Greenman 		     object->type != OBJT_SWAP) ||
139724a1cce3SDavid Greenman 		    (object->flags & OBJ_DEAD)) {
13982ad1a3f7SMatthew Dillon 			break;
139924a1cce3SDavid Greenman 		}
14009b4814bbSDavid Greenman 
14012ad1a3f7SMatthew Dillon 		if (
14022ad1a3f7SMatthew Dillon 		    object->paging_in_progress != 0 ||
14032ad1a3f7SMatthew Dillon 		    backing_object->paging_in_progress != 0
14042ad1a3f7SMatthew Dillon 		) {
1405b9921222SDavid Greenman 			vm_object_qcollapse(object);
14062ad1a3f7SMatthew Dillon 			break;
1407df8bae1dSRodney W. Grimes 		}
1408f919ebdeSDavid Greenman 
140926f9a767SRodney W. Grimes 		/*
14100d94caffSDavid Greenman 		 * We know that we can either collapse the backing object (if
14112ad1a3f7SMatthew Dillon 		 * the parent is the only reference to it) or (perhaps) have
14122ad1a3f7SMatthew Dillon 		 * the parent bypass the object if the parent happens to shadow
14132ad1a3f7SMatthew Dillon 		 * all the resident pages in the entire backing object.
14142ad1a3f7SMatthew Dillon 		 *
14152ad1a3f7SMatthew Dillon 		 * This is ignoring pager-backed pages such as swap pages.
14162ad1a3f7SMatthew Dillon 		 * vm_object_backing_scan fails the shadowing test in this
14172ad1a3f7SMatthew Dillon 		 * case.
1418df8bae1dSRodney W. Grimes 		 */
1419df8bae1dSRodney W. Grimes 
1420df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
1421df8bae1dSRodney W. Grimes 			/*
14222ad1a3f7SMatthew Dillon 			 * If there is exactly one reference to the backing
14232ad1a3f7SMatthew Dillon 			 * object, we can collapse it into the parent.
1424df8bae1dSRodney W. Grimes 			 */
1425df8bae1dSRodney W. Grimes 
14262ad1a3f7SMatthew Dillon 			vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1427df8bae1dSRodney W. Grimes 
1428df8bae1dSRodney W. Grimes 			/*
1429df8bae1dSRodney W. Grimes 			 * Move the pager from backing_object to object.
1430df8bae1dSRodney W. Grimes 			 */
1431df8bae1dSRodney W. Grimes 
143224a1cce3SDavid Greenman 			if (backing_object->type == OBJT_SWAP) {
1433d474eaaaSDoug Rabson 				vm_object_pip_add(backing_object, 1);
143424a1cce3SDavid Greenman 
143524a1cce3SDavid Greenman 				/*
14361c7c3c6aSMatthew Dillon 				 * scrap the paging_offset junk and do a
14371c7c3c6aSMatthew Dillon 				 * discrete copy.  This also removes major
14381c7c3c6aSMatthew Dillon 				 * assumptions about how the swap-pager
14391c7c3c6aSMatthew Dillon 				 * works from where it doesn't belong.  The
14401c7c3c6aSMatthew Dillon 				 * new swapper is able to optimize the
14411c7c3c6aSMatthew Dillon 				 * destroy-source case.
144224a1cce3SDavid Greenman 				 */
14431c7c3c6aSMatthew Dillon 
14441c7c3c6aSMatthew Dillon 				vm_object_pip_add(object, 1);
14451c7c3c6aSMatthew Dillon 				swap_pager_copy(
14461c7c3c6aSMatthew Dillon 				    backing_object,
14471c7c3c6aSMatthew Dillon 				    object,
14481c7c3c6aSMatthew Dillon 				    OFF_TO_IDX(object->backing_object_offset), TRUE);
1449f919ebdeSDavid Greenman 				vm_object_pip_wakeup(object);
1450c0503609SDavid Greenman 
1451f919ebdeSDavid Greenman 				vm_object_pip_wakeup(backing_object);
1452c0503609SDavid Greenman 			}
1453df8bae1dSRodney W. Grimes 			/*
1454df8bae1dSRodney W. Grimes 			 * Object now shadows whatever backing_object did.
14552ad1a3f7SMatthew Dillon 			 * Note that the reference to
14562ad1a3f7SMatthew Dillon 			 * backing_object->backing_object moves from within
14572ad1a3f7SMatthew Dillon 			 * backing_object to within object.
1458df8bae1dSRodney W. Grimes 			 */
1459df8bae1dSRodney W. Grimes 
14602ad1a3f7SMatthew Dillon 			TAILQ_REMOVE(
14612ad1a3f7SMatthew Dillon 			    &object->backing_object->shadow_head,
14622ad1a3f7SMatthew Dillon 			    object,
14632ad1a3f7SMatthew Dillon 			    shadow_list
14642ad1a3f7SMatthew Dillon 			);
1465eaf13dd7SJohn Dyson 			object->backing_object->shadow_count--;
1466eaf13dd7SJohn Dyson 			object->backing_object->generation++;
1467de5f6a77SJohn Dyson 			if (backing_object->backing_object) {
14682ad1a3f7SMatthew Dillon 				TAILQ_REMOVE(
14692ad1a3f7SMatthew Dillon 				    &backing_object->backing_object->shadow_head,
14702ad1a3f7SMatthew Dillon 				    backing_object,
14712ad1a3f7SMatthew Dillon 				    shadow_list
14722ad1a3f7SMatthew Dillon 				);
1473eaf13dd7SJohn Dyson 				backing_object->backing_object->shadow_count--;
1474eaf13dd7SJohn Dyson 				backing_object->backing_object->generation++;
1475de5f6a77SJohn Dyson 			}
147624a1cce3SDavid Greenman 			object->backing_object = backing_object->backing_object;
1477de5f6a77SJohn Dyson 			if (object->backing_object) {
14782ad1a3f7SMatthew Dillon 				TAILQ_INSERT_TAIL(
14792ad1a3f7SMatthew Dillon 				    &object->backing_object->shadow_head,
14802ad1a3f7SMatthew Dillon 				    object,
14812ad1a3f7SMatthew Dillon 				    shadow_list
14822ad1a3f7SMatthew Dillon 				);
1483eaf13dd7SJohn Dyson 				object->backing_object->shadow_count++;
1484eaf13dd7SJohn Dyson 				object->backing_object->generation++;
1485de5f6a77SJohn Dyson 			}
14862fe6e4d7SDavid Greenman 
14872ad1a3f7SMatthew Dillon 			object->backing_object_offset +=
14882ad1a3f7SMatthew Dillon 			    backing_object->backing_object_offset;
14892ad1a3f7SMatthew Dillon 
1490df8bae1dSRodney W. Grimes 			/*
1491df8bae1dSRodney W. Grimes 			 * Discard backing_object.
1492df8bae1dSRodney W. Grimes 			 *
14930d94caffSDavid Greenman 			 * Since the backing object has no pages, no pager left,
14940d94caffSDavid Greenman 			 * and no object references within it, all that is
14950d94caffSDavid Greenman 			 * necessary is to dispose of it.
1496df8bae1dSRodney W. Grimes 			 */
1497245df27cSMatthew Dillon 			KASSERT(backing_object->ref_count == 1, ("backing_object %p was somehow re-referenced during collapse!", backing_object));
1498245df27cSMatthew Dillon 			KASSERT(TAILQ_FIRST(&backing_object->memq) == NULL, ("backing_object %p somehow has left over pages during collapse!", backing_object));
1499df8bae1dSRodney W. Grimes 
15002ad1a3f7SMatthew Dillon 			TAILQ_REMOVE(
15012ad1a3f7SMatthew Dillon 			    &vm_object_list,
15022ad1a3f7SMatthew Dillon 			    backing_object,
15032ad1a3f7SMatthew Dillon 			    object_list
15042ad1a3f7SMatthew Dillon 			);
1505df8bae1dSRodney W. Grimes 			vm_object_count--;
1506df8bae1dSRodney W. Grimes 
150799448ed1SJohn Dyson 			zfree(obj_zone, backing_object);
1508df8bae1dSRodney W. Grimes 
1509df8bae1dSRodney W. Grimes 			object_collapses++;
15100d94caffSDavid Greenman 		} else {
151195e5e988SJohn Dyson 			vm_object_t new_backing_object;
1512df8bae1dSRodney W. Grimes 
1513df8bae1dSRodney W. Grimes 			/*
15142ad1a3f7SMatthew Dillon 			 * If we do not entirely shadow the backing object,
15152ad1a3f7SMatthew Dillon 			 * there is nothing we can do so we give up.
1516df8bae1dSRodney W. Grimes 			 */
1517df8bae1dSRodney W. Grimes 
15182ad1a3f7SMatthew Dillon 			if (vm_object_backing_scan(object, OBSC_TEST_ALL_SHADOWED) == 0) {
15192ad1a3f7SMatthew Dillon 				break;
152024a1cce3SDavid Greenman 			}
1521df8bae1dSRodney W. Grimes 
1522df8bae1dSRodney W. Grimes 			/*
15230d94caffSDavid Greenman 			 * Make the parent shadow the next object in the
15240d94caffSDavid Greenman 			 * chain.  Deallocating backing_object will not remove
15250d94caffSDavid Greenman 			 * it, since its reference count is at least 2.
1526df8bae1dSRodney W. Grimes 			 */
1527df8bae1dSRodney W. Grimes 
15282ad1a3f7SMatthew Dillon 			TAILQ_REMOVE(
15292ad1a3f7SMatthew Dillon 			    &backing_object->shadow_head,
15302ad1a3f7SMatthew Dillon 			    object,
15312ad1a3f7SMatthew Dillon 			    shadow_list
15322ad1a3f7SMatthew Dillon 			);
1533eaf13dd7SJohn Dyson 			backing_object->shadow_count--;
1534eaf13dd7SJohn Dyson 			backing_object->generation++;
153595e5e988SJohn Dyson 
153695e5e988SJohn Dyson 			new_backing_object = backing_object->backing_object;
15378aef1712SMatthew Dillon 			if ((object->backing_object = new_backing_object) != NULL) {
153895e5e988SJohn Dyson 				vm_object_reference(new_backing_object);
15392ad1a3f7SMatthew Dillon 				TAILQ_INSERT_TAIL(
15402ad1a3f7SMatthew Dillon 				    &new_backing_object->shadow_head,
15412ad1a3f7SMatthew Dillon 				    object,
15422ad1a3f7SMatthew Dillon 				    shadow_list
15432ad1a3f7SMatthew Dillon 				);
1544eaf13dd7SJohn Dyson 				new_backing_object->shadow_count++;
1545eaf13dd7SJohn Dyson 				new_backing_object->generation++;
154695e5e988SJohn Dyson 				object->backing_object_offset +=
154795e5e988SJohn Dyson 					backing_object->backing_object_offset;
1548de5f6a77SJohn Dyson 			}
1549df8bae1dSRodney W. Grimes 
1550df8bae1dSRodney W. Grimes 			/*
15510d94caffSDavid Greenman 			 * Drop the reference count on backing_object. Since
15520d94caffSDavid Greenman 			 * its ref_count was at least 2, it will not vanish;
1553eaf13dd7SJohn Dyson 			 * so we don't need to call vm_object_deallocate, but
1554eaf13dd7SJohn Dyson 			 * we do anyway.
1555df8bae1dSRodney W. Grimes 			 */
155695e5e988SJohn Dyson 			vm_object_deallocate(backing_object);
1557df8bae1dSRodney W. Grimes 			object_bypasses++;
1558df8bae1dSRodney W. Grimes 		}
1559df8bae1dSRodney W. Grimes 
1560df8bae1dSRodney W. Grimes 		/*
1561df8bae1dSRodney W. Grimes 		 * Try again with this object's new backing object.
1562df8bae1dSRodney W. Grimes 		 */
1563df8bae1dSRodney W. Grimes 	}
1564df8bae1dSRodney W. Grimes }
1565df8bae1dSRodney W. Grimes 
1566df8bae1dSRodney W. Grimes /*
1567df8bae1dSRodney W. Grimes  *	vm_object_page_remove: [internal]
1568df8bae1dSRodney W. Grimes  *
1569df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
1570df8bae1dSRodney W. Grimes  *	object range from the object's list of pages.
1571df8bae1dSRodney W. Grimes  *
1572df8bae1dSRodney W. Grimes  *	The object must be locked.
1573df8bae1dSRodney W. Grimes  */
157426f9a767SRodney W. Grimes void
15751b40f8c0SMatthew Dillon vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end, boolean_t clean_only)
1576df8bae1dSRodney W. Grimes {
1577d031cff1SMatthew Dillon 	vm_page_t p, next;
1578a316d390SJohn Dyson 	unsigned int size;
1579f5ef029eSPoul-Henning Kamp 	int all;
1580df8bae1dSRodney W. Grimes 
15810cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
158223955314SAlfred Perlstein 
1583a1a54e9fSAlan Cox 	if (object == NULL ||
1584a1a54e9fSAlan Cox 	    object->resident_page_count == 0)
1585df8bae1dSRodney W. Grimes 		return;
1586df8bae1dSRodney W. Grimes 
158795e5e988SJohn Dyson 	all = ((end == 0) && (start == 0));
158895e5e988SJohn Dyson 
15898b03c8edSMatthew Dillon 	/*
15908b03c8edSMatthew Dillon 	 * Since physically-backed objects do not use managed pages, we can't
15918b03c8edSMatthew Dillon 	 * remove pages from the object (we must instead remove the page
15928b03c8edSMatthew Dillon 	 * references, and then destroy the object).
15938b03c8edSMatthew Dillon 	 */
15948b03c8edSMatthew Dillon 	KASSERT(object->type != OBJT_PHYS, ("attempt to remove pages from a physical object"));
15958b03c8edSMatthew Dillon 
1596d474eaaaSDoug Rabson 	vm_object_pip_add(object, 1);
159726f9a767SRodney W. Grimes again:
159826f9a767SRodney W. Grimes 	size = end - start;
1599a1a54e9fSAlan Cox 	if (all || size > object->resident_page_count / 4) {
1600b18bfc3dSJohn Dyson 		for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
1601b18bfc3dSJohn Dyson 			next = TAILQ_NEXT(p, listq);
160295e5e988SJohn Dyson 			if (all || ((start <= p->pindex) && (p->pindex < end))) {
1603bd7e5f99SJohn Dyson 				if (p->wire_count != 0) {
1604bd7e5f99SJohn Dyson 					vm_page_protect(p, VM_PROT_NONE);
1605ce65e68cSDavid Greenman 					if (!clean_only)
1606bd7e5f99SJohn Dyson 						p->valid = 0;
16070d94caffSDavid Greenman 					continue;
16080d94caffSDavid Greenman 				}
16090891ef4cSJohn Dyson 
1610b18bfc3dSJohn Dyson 				/*
1611b18bfc3dSJohn Dyson 				 * The busy flags are only cleared at
1612b18bfc3dSJohn Dyson 				 * interrupt -- minimize the spl transitions
1613b18bfc3dSJohn Dyson 				 */
1614ffc82b0aSJohn Dyson 
16151c7c3c6aSMatthew Dillon  				if (vm_page_sleep_busy(p, TRUE, "vmopar"))
161626f9a767SRodney W. Grimes  					goto again;
16170891ef4cSJohn Dyson 
16188f9110f6SJohn Dyson 				if (clean_only && p->valid) {
16197c1f6cedSDavid Greenman 					vm_page_test_dirty(p);
16207c1f6cedSDavid Greenman 					if (p->valid & p->dirty)
16217c1f6cedSDavid Greenman 						continue;
16227c1f6cedSDavid Greenman 				}
1623eaf13dd7SJohn Dyson 
1624e69763a3SDoug Rabson 				vm_page_busy(p);
1625f919ebdeSDavid Greenman 				vm_page_protect(p, VM_PROT_NONE);
1626df8bae1dSRodney W. Grimes 				vm_page_free(p);
162726f9a767SRodney W. Grimes 			}
162826f9a767SRodney W. Grimes 		}
162926f9a767SRodney W. Grimes 	} else {
163026f9a767SRodney W. Grimes 		while (size > 0) {
1631bd7e5f99SJohn Dyson 			if ((p = vm_page_lookup(object, start)) != 0) {
1632eaf13dd7SJohn Dyson 
1633bd7e5f99SJohn Dyson 				if (p->wire_count != 0) {
1634bd7e5f99SJohn Dyson 					vm_page_protect(p, VM_PROT_NONE);
1635ce65e68cSDavid Greenman 					if (!clean_only)
1636ce65e68cSDavid Greenman 						p->valid = 0;
1637bd7e5f99SJohn Dyson 					start += 1;
1638bd7e5f99SJohn Dyson 					size -= 1;
1639bd7e5f99SJohn Dyson 					continue;
16400d94caffSDavid Greenman 				}
1641eaf13dd7SJohn Dyson 
1642b18bfc3dSJohn Dyson 				/*
1643b18bfc3dSJohn Dyson 				 * The busy flags are only cleared at
1644b18bfc3dSJohn Dyson 				 * interrupt -- minimize the spl transitions
1645b18bfc3dSJohn Dyson 				 */
16461c7c3c6aSMatthew Dillon  				if (vm_page_sleep_busy(p, TRUE, "vmopar"))
164726f9a767SRodney W. Grimes 					goto again;
1648eaf13dd7SJohn Dyson 
16498f9110f6SJohn Dyson 				if (clean_only && p->valid) {
16507c1f6cedSDavid Greenman 					vm_page_test_dirty(p);
1651bd7e5f99SJohn Dyson 					if (p->valid & p->dirty) {
1652bd7e5f99SJohn Dyson 						start += 1;
1653bd7e5f99SJohn Dyson 						size -= 1;
16547c1f6cedSDavid Greenman 						continue;
16557c1f6cedSDavid Greenman 					}
1656bd7e5f99SJohn Dyson 				}
1657eaf13dd7SJohn Dyson 
1658e69763a3SDoug Rabson 				vm_page_busy(p);
1659f919ebdeSDavid Greenman 				vm_page_protect(p, VM_PROT_NONE);
166026f9a767SRodney W. Grimes 				vm_page_free(p);
166126f9a767SRodney W. Grimes 			}
1662a316d390SJohn Dyson 			start += 1;
1663a316d390SJohn Dyson 			size -= 1;
1664df8bae1dSRodney W. Grimes 		}
1665df8bae1dSRodney W. Grimes 	}
1666f919ebdeSDavid Greenman 	vm_object_pip_wakeup(object);
1667c0503609SDavid Greenman }
1668df8bae1dSRodney W. Grimes 
1669df8bae1dSRodney W. Grimes /*
1670df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
1671df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
1672df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
1673df8bae1dSRodney W. Grimes  *
1674df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
1675df8bae1dSRodney W. Grimes  *
1676df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
1677df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
1678df8bae1dSRodney W. Grimes  *
1679df8bae1dSRodney W. Grimes  *	Parameters:
1680df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
1681df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
1682df8bae1dSRodney W. Grimes  *		next_object	Second object into coalesce
1683df8bae1dSRodney W. Grimes  *		next_offset	Offset into next_object
1684df8bae1dSRodney W. Grimes  *
1685df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
1686df8bae1dSRodney W. Grimes  *		next_size	Size of reference to next_object
1687df8bae1dSRodney W. Grimes  *
1688df8bae1dSRodney W. Grimes  *	Conditions:
1689df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
1690df8bae1dSRodney W. Grimes  */
16910d94caffSDavid Greenman boolean_t
16921b40f8c0SMatthew Dillon vm_object_coalesce(vm_object_t prev_object, vm_pindex_t prev_pindex, vm_size_t prev_size, vm_size_t next_size)
1693df8bae1dSRodney W. Grimes {
1694ea41812fSAlan Cox 	vm_pindex_t next_pindex;
1695df8bae1dSRodney W. Grimes 
16960cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
169723955314SAlfred Perlstein 
1698df8bae1dSRodney W. Grimes 	if (prev_object == NULL) {
1699df8bae1dSRodney W. Grimes 		return (TRUE);
1700df8bae1dSRodney W. Grimes 	}
1701df8bae1dSRodney W. Grimes 
17024112823fSMatthew Dillon 	if (prev_object->type != OBJT_DEFAULT &&
17034112823fSMatthew Dillon 	    prev_object->type != OBJT_SWAP) {
170430dcfc09SJohn Dyson 		return (FALSE);
170530dcfc09SJohn Dyson 	}
170630dcfc09SJohn Dyson 
1707df8bae1dSRodney W. Grimes 	/*
1708df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
1709df8bae1dSRodney W. Grimes 	 */
1710df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
1711df8bae1dSRodney W. Grimes 
1712df8bae1dSRodney W. Grimes 	/*
17130d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
17140d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
17150d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
1716df8bae1dSRodney W. Grimes 	 */
1717df8bae1dSRodney W. Grimes 
17188cc7e047SJohn Dyson 	if (prev_object->backing_object != NULL) {
1719df8bae1dSRodney W. Grimes 		return (FALSE);
1720df8bae1dSRodney W. Grimes 	}
1721a316d390SJohn Dyson 
1722a316d390SJohn Dyson 	prev_size >>= PAGE_SHIFT;
1723a316d390SJohn Dyson 	next_size >>= PAGE_SHIFT;
1724ea41812fSAlan Cox 	next_pindex = prev_pindex + prev_size;
17258cc7e047SJohn Dyson 
17268cc7e047SJohn Dyson 	if ((prev_object->ref_count > 1) &&
1727ea41812fSAlan Cox 	    (prev_object->size != next_pindex)) {
17288cc7e047SJohn Dyson 		return (FALSE);
17298cc7e047SJohn Dyson 	}
17308cc7e047SJohn Dyson 
1731df8bae1dSRodney W. Grimes 	/*
17320d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
17330d94caffSDavid Greenman 	 * deallocation.
1734df8bae1dSRodney W. Grimes 	 */
1735ea41812fSAlan Cox 	if (next_pindex < prev_object->size) {
1736df8bae1dSRodney W. Grimes 		vm_object_page_remove(prev_object,
1737ea41812fSAlan Cox 				      next_pindex,
1738ea41812fSAlan Cox 				      next_pindex + next_size, FALSE);
1739ea41812fSAlan Cox 		if (prev_object->type == OBJT_SWAP)
1740ea41812fSAlan Cox 			swap_pager_freespace(prev_object,
1741ea41812fSAlan Cox 					     next_pindex, next_size);
1742ea41812fSAlan Cox 	}
1743df8bae1dSRodney W. Grimes 
1744df8bae1dSRodney W. Grimes 	/*
1745df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
1746df8bae1dSRodney W. Grimes 	 */
1747ea41812fSAlan Cox 	if (next_pindex + next_size > prev_object->size)
1748ea41812fSAlan Cox 		prev_object->size = next_pindex + next_size;
1749df8bae1dSRodney W. Grimes 
1750df8bae1dSRodney W. Grimes 	return (TRUE);
1751df8bae1dSRodney W. Grimes }
1752df8bae1dSRodney W. Grimes 
17537a5a6352SMatthew Dillon void
17547a5a6352SMatthew Dillon vm_object_set_writeable_dirty(vm_object_t object)
17557a5a6352SMatthew Dillon {
17567a5a6352SMatthew Dillon 	struct vnode *vp;
17577a5a6352SMatthew Dillon 
17587a5a6352SMatthew Dillon 	vm_object_set_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
17597a5a6352SMatthew Dillon 	if (object->type == OBJT_VNODE &&
17607a5a6352SMatthew Dillon 	    (vp = (struct vnode *)object->handle) != NULL) {
17617a5a6352SMatthew Dillon 		if ((vp->v_flag & VOBJDIRTY) == 0) {
17627a5a6352SMatthew Dillon 			mtx_lock(&vp->v_interlock);
17637a5a6352SMatthew Dillon 			vp->v_flag |= VOBJDIRTY;
17647a5a6352SMatthew Dillon 			mtx_unlock(&vp->v_interlock);
17657a5a6352SMatthew Dillon 		}
17667a5a6352SMatthew Dillon 	}
17677a5a6352SMatthew Dillon }
17687a5a6352SMatthew Dillon 
1769c7c34a24SBruce Evans #include "opt_ddb.h"
1770c3cb3e12SDavid Greenman #ifdef DDB
1771c7c34a24SBruce Evans #include <sys/kernel.h>
1772c7c34a24SBruce Evans 
1773ce9edcf5SPoul-Henning Kamp #include <sys/cons.h>
1774c7c34a24SBruce Evans 
1775c7c34a24SBruce Evans #include <ddb/ddb.h>
1776c7c34a24SBruce Evans 
1777cac597e4SBruce Evans static int
17781b40f8c0SMatthew Dillon _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
1779a1f6d91cSDavid Greenman {
1780a1f6d91cSDavid Greenman 	vm_map_t tmpm;
1781a1f6d91cSDavid Greenman 	vm_map_entry_t tmpe;
1782a1f6d91cSDavid Greenman 	vm_object_t obj;
1783a1f6d91cSDavid Greenman 	int entcount;
1784a1f6d91cSDavid Greenman 
1785a1f6d91cSDavid Greenman 	if (map == 0)
1786a1f6d91cSDavid Greenman 		return 0;
1787a1f6d91cSDavid Greenman 
1788a1f6d91cSDavid Greenman 	if (entry == 0) {
1789a1f6d91cSDavid Greenman 		tmpe = map->header.next;
1790a1f6d91cSDavid Greenman 		entcount = map->nentries;
1791a1f6d91cSDavid Greenman 		while (entcount-- && (tmpe != &map->header)) {
1792a1f6d91cSDavid Greenman 			if( _vm_object_in_map(map, object, tmpe)) {
1793a1f6d91cSDavid Greenman 				return 1;
1794a1f6d91cSDavid Greenman 			}
1795a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1796a1f6d91cSDavid Greenman 		}
17979fdfe602SMatthew Dillon 	} else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
17989fdfe602SMatthew Dillon 		tmpm = entry->object.sub_map;
1799a1f6d91cSDavid Greenman 		tmpe = tmpm->header.next;
1800a1f6d91cSDavid Greenman 		entcount = tmpm->nentries;
1801a1f6d91cSDavid Greenman 		while (entcount-- && tmpe != &tmpm->header) {
1802a1f6d91cSDavid Greenman 			if( _vm_object_in_map(tmpm, object, tmpe)) {
1803a1f6d91cSDavid Greenman 				return 1;
1804a1f6d91cSDavid Greenman 			}
1805a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1806a1f6d91cSDavid Greenman 		}
18078aef1712SMatthew Dillon 	} else if ((obj = entry->object.vm_object) != NULL) {
180824a1cce3SDavid Greenman 		for (; obj; obj = obj->backing_object)
1809a1f6d91cSDavid Greenman 			if( obj == object) {
1810a1f6d91cSDavid Greenman 				return 1;
1811a1f6d91cSDavid Greenman 			}
1812a1f6d91cSDavid Greenman 	}
1813a1f6d91cSDavid Greenman 	return 0;
1814a1f6d91cSDavid Greenman }
1815a1f6d91cSDavid Greenman 
1816cac597e4SBruce Evans static int
18171b40f8c0SMatthew Dillon vm_object_in_map(vm_object_t object)
1818a1f6d91cSDavid Greenman {
1819a1f6d91cSDavid Greenman 	struct proc *p;
18201005a129SJohn Baldwin 
182160517fd1SJohn Baldwin 	/* sx_slock(&allproc_lock); */
1822fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(p, &allproc, p_list) {
1823a1f6d91cSDavid Greenman 		if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1824a1f6d91cSDavid Greenman 			continue;
1825553629ebSJake Burkholder 		if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
182660517fd1SJohn Baldwin 			/* sx_sunlock(&allproc_lock); */
1827a1f6d91cSDavid Greenman 			return 1;
1828a1f6d91cSDavid Greenman 		}
1829553629ebSJake Burkholder 	}
183060517fd1SJohn Baldwin 	/* sx_sunlock(&allproc_lock); */
1831a1f6d91cSDavid Greenman 	if( _vm_object_in_map( kernel_map, object, 0))
1832a1f6d91cSDavid Greenman 		return 1;
1833a1f6d91cSDavid Greenman 	if( _vm_object_in_map( kmem_map, object, 0))
1834a1f6d91cSDavid Greenman 		return 1;
1835a1f6d91cSDavid Greenman 	if( _vm_object_in_map( pager_map, object, 0))
1836a1f6d91cSDavid Greenman 		return 1;
1837a1f6d91cSDavid Greenman 	if( _vm_object_in_map( buffer_map, object, 0))
1838a1f6d91cSDavid Greenman 		return 1;
1839a1f6d91cSDavid Greenman 	return 0;
1840a1f6d91cSDavid Greenman }
1841a1f6d91cSDavid Greenman 
1842c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check)
1843f708ef1bSPoul-Henning Kamp {
1844a1f6d91cSDavid Greenman 	vm_object_t object;
1845a1f6d91cSDavid Greenman 
1846a1f6d91cSDavid Greenman 	/*
1847a1f6d91cSDavid Greenman 	 * make sure that internal objs are in a map somewhere
1848a1f6d91cSDavid Greenman 	 * and none have zero ref counts.
1849a1f6d91cSDavid Greenman 	 */
1850cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
185124a1cce3SDavid Greenman 		if (object->handle == NULL &&
185224a1cce3SDavid Greenman 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
1853a1f6d91cSDavid Greenman 			if (object->ref_count == 0) {
18543efc015bSPeter Wemm 				db_printf("vmochk: internal obj has zero ref count: %ld\n",
18553efc015bSPeter Wemm 					(long)object->size);
1856a1f6d91cSDavid Greenman 			}
1857a1f6d91cSDavid Greenman 			if (!vm_object_in_map(object)) {
1858fc62ef1fSBruce Evans 				db_printf(
1859fc62ef1fSBruce Evans 			"vmochk: internal obj is not in a map: "
1860fc62ef1fSBruce Evans 			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
1861fc62ef1fSBruce Evans 				    object->ref_count, (u_long)object->size,
1862fc62ef1fSBruce Evans 				    (u_long)object->size,
1863fc62ef1fSBruce Evans 				    (void *)object->backing_object);
1864a1f6d91cSDavid Greenman 			}
1865a1f6d91cSDavid Greenman 		}
1866a1f6d91cSDavid Greenman 	}
1867a1f6d91cSDavid Greenman }
1868a1f6d91cSDavid Greenman 
186926f9a767SRodney W. Grimes /*
1870df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
1871df8bae1dSRodney W. Grimes  */
1872c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static)
1873df8bae1dSRodney W. Grimes {
1874c7c34a24SBruce Evans 	/* XXX convert args. */
1875c7c34a24SBruce Evans 	vm_object_t object = (vm_object_t)addr;
1876c7c34a24SBruce Evans 	boolean_t full = have_addr;
1877c7c34a24SBruce Evans 
1878d031cff1SMatthew Dillon 	vm_page_t p;
1879df8bae1dSRodney W. Grimes 
1880c7c34a24SBruce Evans 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
1881c7c34a24SBruce Evans #define	count	was_count
1882c7c34a24SBruce Evans 
1883d031cff1SMatthew Dillon 	int count;
1884df8bae1dSRodney W. Grimes 
1885df8bae1dSRodney W. Grimes 	if (object == NULL)
1886df8bae1dSRodney W. Grimes 		return;
1887df8bae1dSRodney W. Grimes 
1888eb95adefSBruce Evans 	db_iprintf(
1889eb95adefSBruce Evans 	    "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n",
1890eb95adefSBruce Evans 	    object, (int)object->type, (u_long)object->size,
1891eb95adefSBruce Evans 	    object->resident_page_count, object->ref_count, object->flags);
1892eb95adefSBruce Evans 	/*
18931c7c3c6aSMatthew Dillon 	 * XXX no %qd in kernel.  Truncate object->backing_object_offset.
1894eb95adefSBruce Evans 	 */
18951c7c3c6aSMatthew Dillon 	db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%lx\n",
18961c7c3c6aSMatthew Dillon 	    object->shadow_count,
1897eb95adefSBruce Evans 	    object->backing_object ? object->backing_object->ref_count : 0,
1898eb95adefSBruce Evans 	    object->backing_object, (long)object->backing_object_offset);
1899df8bae1dSRodney W. Grimes 
1900df8bae1dSRodney W. Grimes 	if (!full)
1901df8bae1dSRodney W. Grimes 		return;
1902df8bae1dSRodney W. Grimes 
1903c7c34a24SBruce Evans 	db_indent += 2;
1904df8bae1dSRodney W. Grimes 	count = 0;
1905fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(p, &object->memq, listq) {
1906df8bae1dSRodney W. Grimes 		if (count == 0)
1907c7c34a24SBruce Evans 			db_iprintf("memory:=");
1908df8bae1dSRodney W. Grimes 		else if (count == 6) {
1909c7c34a24SBruce Evans 			db_printf("\n");
1910c7c34a24SBruce Evans 			db_iprintf(" ...");
1911df8bae1dSRodney W. Grimes 			count = 0;
1912df8bae1dSRodney W. Grimes 		} else
1913c7c34a24SBruce Evans 			db_printf(",");
1914df8bae1dSRodney W. Grimes 		count++;
1915df8bae1dSRodney W. Grimes 
1916c7c34a24SBruce Evans 		db_printf("(off=0x%lx,page=0x%lx)",
1917a316d390SJohn Dyson 		    (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
1918df8bae1dSRodney W. Grimes 	}
1919df8bae1dSRodney W. Grimes 	if (count != 0)
1920c7c34a24SBruce Evans 		db_printf("\n");
1921c7c34a24SBruce Evans 	db_indent -= 2;
1922df8bae1dSRodney W. Grimes }
19235070c7f8SJohn Dyson 
1924c7c34a24SBruce Evans /* XXX. */
1925c7c34a24SBruce Evans #undef count
1926c7c34a24SBruce Evans 
1927c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */
19285070c7f8SJohn Dyson void
19291b40f8c0SMatthew Dillon vm_object_print(
19301b40f8c0SMatthew Dillon         /* db_expr_t */ long addr,
19311b40f8c0SMatthew Dillon 	boolean_t have_addr,
19321b40f8c0SMatthew Dillon 	/* db_expr_t */ long count,
19331b40f8c0SMatthew Dillon 	char *modif)
1934c7c34a24SBruce Evans {
1935c7c34a24SBruce Evans 	vm_object_print_static(addr, have_addr, count, modif);
1936c7c34a24SBruce Evans }
1937c7c34a24SBruce Evans 
1938c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
19395070c7f8SJohn Dyson {
19405070c7f8SJohn Dyson 	vm_object_t object;
19415070c7f8SJohn Dyson 	int nl = 0;
19425070c7f8SJohn Dyson 	int c;
1943cc64b484SAlfred Perlstein 
1944cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
19455070c7f8SJohn Dyson 		vm_pindex_t idx, fidx;
19465070c7f8SJohn Dyson 		vm_pindex_t osize;
19475070c7f8SJohn Dyson 		vm_offset_t pa = -1, padiff;
19485070c7f8SJohn Dyson 		int rcount;
19495070c7f8SJohn Dyson 		vm_page_t m;
19505070c7f8SJohn Dyson 
1951fc62ef1fSBruce Evans 		db_printf("new object: %p\n", (void *)object);
19525070c7f8SJohn Dyson 		if ( nl > 18) {
19535070c7f8SJohn Dyson 			c = cngetc();
19545070c7f8SJohn Dyson 			if (c != ' ')
19555070c7f8SJohn Dyson 				return;
19565070c7f8SJohn Dyson 			nl = 0;
19575070c7f8SJohn Dyson 		}
19585070c7f8SJohn Dyson 		nl++;
19595070c7f8SJohn Dyson 		rcount = 0;
19605070c7f8SJohn Dyson 		fidx = 0;
19615070c7f8SJohn Dyson 		osize = object->size;
19625070c7f8SJohn Dyson 		if (osize > 128)
19635070c7f8SJohn Dyson 			osize = 128;
19645070c7f8SJohn Dyson 		for (idx = 0; idx < osize; idx++) {
19655070c7f8SJohn Dyson 			m = vm_page_lookup(object, idx);
19665070c7f8SJohn Dyson 			if (m == NULL) {
19675070c7f8SJohn Dyson 				if (rcount) {
19683efc015bSPeter Wemm 					db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
19693efc015bSPeter Wemm 						(long)fidx, rcount, (long)pa);
19705070c7f8SJohn Dyson 					if ( nl > 18) {
19715070c7f8SJohn Dyson 						c = cngetc();
19725070c7f8SJohn Dyson 						if (c != ' ')
19735070c7f8SJohn Dyson 							return;
19745070c7f8SJohn Dyson 						nl = 0;
19755070c7f8SJohn Dyson 					}
19765070c7f8SJohn Dyson 					nl++;
19775070c7f8SJohn Dyson 					rcount = 0;
19785070c7f8SJohn Dyson 				}
19795070c7f8SJohn Dyson 				continue;
19805070c7f8SJohn Dyson 			}
19815070c7f8SJohn Dyson 
19825070c7f8SJohn Dyson 
19835070c7f8SJohn Dyson 			if (rcount &&
19845070c7f8SJohn Dyson 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
19855070c7f8SJohn Dyson 				++rcount;
19865070c7f8SJohn Dyson 				continue;
19875070c7f8SJohn Dyson 			}
19885070c7f8SJohn Dyson 			if (rcount) {
19895070c7f8SJohn Dyson 				padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
19905070c7f8SJohn Dyson 				padiff >>= PAGE_SHIFT;
19915070c7f8SJohn Dyson 				padiff &= PQ_L2_MASK;
19925070c7f8SJohn Dyson 				if (padiff == 0) {
19935070c7f8SJohn Dyson 					pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
19945070c7f8SJohn Dyson 					++rcount;
19955070c7f8SJohn Dyson 					continue;
19965070c7f8SJohn Dyson 				}
19973efc015bSPeter Wemm 				db_printf(" index(%ld)run(%d)pa(0x%lx)",
19983efc015bSPeter Wemm 					(long)fidx, rcount, (long)pa);
19993efc015bSPeter Wemm 				db_printf("pd(%ld)\n", (long)padiff);
20005070c7f8SJohn Dyson 				if ( nl > 18) {
20015070c7f8SJohn Dyson 					c = cngetc();
20025070c7f8SJohn Dyson 					if (c != ' ')
20035070c7f8SJohn Dyson 						return;
20045070c7f8SJohn Dyson 					nl = 0;
20055070c7f8SJohn Dyson 				}
20065070c7f8SJohn Dyson 				nl++;
20075070c7f8SJohn Dyson 			}
20085070c7f8SJohn Dyson 			fidx = idx;
20095070c7f8SJohn Dyson 			pa = VM_PAGE_TO_PHYS(m);
20105070c7f8SJohn Dyson 			rcount = 1;
20115070c7f8SJohn Dyson 		}
20125070c7f8SJohn Dyson 		if (rcount) {
20133efc015bSPeter Wemm 			db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
20143efc015bSPeter Wemm 				(long)fidx, rcount, (long)pa);
20155070c7f8SJohn Dyson 			if ( nl > 18) {
20165070c7f8SJohn Dyson 				c = cngetc();
20175070c7f8SJohn Dyson 				if (c != ' ')
20185070c7f8SJohn Dyson 					return;
20195070c7f8SJohn Dyson 				nl = 0;
20205070c7f8SJohn Dyson 			}
20215070c7f8SJohn Dyson 			nl++;
20225070c7f8SJohn Dyson 		}
20235070c7f8SJohn Dyson 	}
20245070c7f8SJohn Dyson }
2025c3cb3e12SDavid Greenman #endif /* DDB */
2026