xref: /freebsd/sys/vm/vm_object.c (revision e239bb97303e1767f43e4082373db2aba87abaad)
160727d8bSWarner Losh /*-
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  * 4. Neither the name of the University nor the names of its contributors
17df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
18df8bae1dSRodney W. Grimes  *    without specific prior written permission.
19df8bae1dSRodney W. Grimes  *
20df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
31df8bae1dSRodney W. Grimes  *
323c4dd356SDavid Greenman  *	from: @(#)vm_object.c	8.5 (Berkeley) 3/22/94
33df8bae1dSRodney W. Grimes  *
34df8bae1dSRodney W. Grimes  *
35df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36df8bae1dSRodney W. Grimes  * All rights reserved.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
39df8bae1dSRodney W. Grimes  *
40df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
41df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
42df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
43df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
44df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
45df8bae1dSRodney W. Grimes  *
46df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49df8bae1dSRodney W. Grimes  *
50df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
51df8bae1dSRodney W. Grimes  *
52df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
53df8bae1dSRodney W. Grimes  *  School of Computer Science
54df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
55df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
56df8bae1dSRodney W. Grimes  *
57df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
58df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
59df8bae1dSRodney W. Grimes  */
60df8bae1dSRodney W. Grimes 
61df8bae1dSRodney W. Grimes /*
62df8bae1dSRodney W. Grimes  *	Virtual memory object module.
63df8bae1dSRodney W. Grimes  */
64df8bae1dSRodney W. Grimes 
65874651b1SDavid E. O'Brien #include <sys/cdefs.h>
66874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
67874651b1SDavid E. O'Brien 
68f8a47341SAlan Cox #include "opt_vm.h"
69f8a47341SAlan Cox 
70df8bae1dSRodney W. Grimes #include <sys/param.h>
71df8bae1dSRodney W. Grimes #include <sys/systm.h>
72fb919e4dSMark Murray #include <sys/lock.h>
73867a482dSJohn Dyson #include <sys/mman.h>
74cf2819ccSJohn Dyson #include <sys/mount.h>
75b9b7a4beSMatthew Dillon #include <sys/kernel.h>
76b9b7a4beSMatthew Dillon #include <sys/sysctl.h>
771b367556SJason Evans #include <sys/mutex.h>
78fb919e4dSMark Murray #include <sys/proc.h>		/* for curproc, pageproc */
79fb919e4dSMark Murray #include <sys/socket.h>
803364c323SKonstantin Belousov #include <sys/resourcevar.h>
81fb919e4dSMark Murray #include <sys/vnode.h>
82fb919e4dSMark Murray #include <sys/vmmeter.h>
831005a129SJohn Baldwin #include <sys/sx.h>
84df8bae1dSRodney W. Grimes 
85df8bae1dSRodney W. Grimes #include <vm/vm.h>
86efeaf95aSDavid Greenman #include <vm/vm_param.h>
87efeaf95aSDavid Greenman #include <vm/pmap.h>
88efeaf95aSDavid Greenman #include <vm/vm_map.h>
89efeaf95aSDavid Greenman #include <vm/vm_object.h>
90df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
9126f9a767SRodney W. Grimes #include <vm/vm_pageout.h>
920d94caffSDavid Greenman #include <vm/vm_pager.h>
9305f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h>
94a1f6d91cSDavid Greenman #include <vm/vm_kern.h>
95efeaf95aSDavid Greenman #include <vm/vm_extern.h>
96f8a47341SAlan Cox #include <vm/vm_reserv.h>
97670d17b5SJeff Roberson #include <vm/uma.h>
9826f9a767SRodney W. Grimes 
99b9b7a4beSMatthew Dillon #define EASY_SCAN_FACTOR       8
100b9b7a4beSMatthew Dillon 
101b9b7a4beSMatthew Dillon #define MSYNC_FLUSH_HARDSEQ	0x01
102b9b7a4beSMatthew Dillon #define MSYNC_FLUSH_SOFTSEQ	0x02
103b9b7a4beSMatthew Dillon 
104c53f7aceSDag-Erling Smørgrav static int old_msync;
105c53f7aceSDag-Erling Smørgrav SYSCTL_INT(_vm, OID_AUTO, old_msync, CTLFLAG_RW, &old_msync, 0,
106c53f7aceSDag-Erling Smørgrav     "Use old (insecure) msync behavior");
107c53f7aceSDag-Erling Smørgrav 
108b9b7a4beSMatthew Dillon static void	vm_object_qcollapse(vm_object_t object);
109b9b7a4beSMatthew Dillon static int	vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags);
11002dd8331SAlan Cox static void	vm_object_vndeallocate(vm_object_t object);
111f6b04d2bSDavid Greenman 
112df8bae1dSRodney W. Grimes /*
113df8bae1dSRodney W. Grimes  *	Virtual memory objects maintain the actual data
114df8bae1dSRodney W. Grimes  *	associated with allocated virtual memory.  A given
115df8bae1dSRodney W. Grimes  *	page of memory exists within exactly one object.
116df8bae1dSRodney W. Grimes  *
117df8bae1dSRodney W. Grimes  *	An object is only deallocated when all "references"
118df8bae1dSRodney W. Grimes  *	are given up.  Only one "reference" to a given
119df8bae1dSRodney W. Grimes  *	region of an object should be writeable.
120df8bae1dSRodney W. Grimes  *
121df8bae1dSRodney W. Grimes  *	Associated with each object is a list of all resident
122df8bae1dSRodney W. Grimes  *	memory pages belonging to that object; this list is
123df8bae1dSRodney W. Grimes  *	maintained by the "vm_page" module, and locked by the object's
124df8bae1dSRodney W. Grimes  *	lock.
125df8bae1dSRodney W. Grimes  *
126df8bae1dSRodney W. Grimes  *	Each object also records a "pager" routine which is
127df8bae1dSRodney W. Grimes  *	used to retrieve (and store) pages to the proper backing
128df8bae1dSRodney W. Grimes  *	storage.  In addition, objects may be backed by other
129df8bae1dSRodney W. Grimes  *	objects from which they were virtual-copied.
130df8bae1dSRodney W. Grimes  *
131df8bae1dSRodney W. Grimes  *	The only items within the object structure which are
132df8bae1dSRodney W. Grimes  *	modified after time of creation are:
133df8bae1dSRodney W. Grimes  *		reference count		locked by object's lock
134df8bae1dSRodney W. Grimes  *		pager routine		locked by object's lock
135df8bae1dSRodney W. Grimes  *
136df8bae1dSRodney W. Grimes  */
137df8bae1dSRodney W. Grimes 
13828f8db14SBruce Evans struct object_q vm_object_list;
139a5698387SAlan Cox struct mtx vm_object_list_mtx;	/* lock for object list and count */
140cccf11b8SAlan Cox 
141cccf11b8SAlan Cox struct vm_object kernel_object_store;
142cccf11b8SAlan Cox struct vm_object kmem_object_store;
143df8bae1dSRodney W. Grimes 
144604c2bbcSAlan Cox SYSCTL_NODE(_vm_stats, OID_AUTO, object, CTLFLAG_RD, 0, "VM object stats");
145604c2bbcSAlan Cox 
146f708ef1bSPoul-Henning Kamp static long object_collapses;
147604c2bbcSAlan Cox SYSCTL_LONG(_vm_stats_object, OID_AUTO, collapses, CTLFLAG_RD,
148604c2bbcSAlan Cox     &object_collapses, 0, "VM object collapses");
149604c2bbcSAlan Cox 
150f708ef1bSPoul-Henning Kamp static long object_bypasses;
151604c2bbcSAlan Cox SYSCTL_LONG(_vm_stats_object, OID_AUTO, bypasses, CTLFLAG_RD,
152604c2bbcSAlan Cox     &object_bypasses, 0, "VM object bypasses");
153dad740e9SAlan Cox 
154670d17b5SJeff Roberson static uma_zone_t obj_zone;
1558355f576SJeff Roberson 
156b23f72e9SBrian Feldman static int vm_object_zinit(void *mem, int size, int flags);
1578355f576SJeff Roberson 
1588355f576SJeff Roberson #ifdef INVARIANTS
1598355f576SJeff Roberson static void vm_object_zdtor(void *mem, int size, void *arg);
1608355f576SJeff Roberson 
1618355f576SJeff Roberson static void
1628355f576SJeff Roberson vm_object_zdtor(void *mem, int size, void *arg)
1638355f576SJeff Roberson {
1648355f576SJeff Roberson 	vm_object_t object;
1658355f576SJeff Roberson 
1668355f576SJeff Roberson 	object = (vm_object_t)mem;
16743186e53SAlan Cox 	KASSERT(TAILQ_EMPTY(&object->memq),
16843186e53SAlan Cox 	    ("object %p has resident pages",
16943186e53SAlan Cox 	    object));
170f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
171f8a47341SAlan Cox 	KASSERT(LIST_EMPTY(&object->rvq),
172f8a47341SAlan Cox 	    ("object %p has reservations",
173f8a47341SAlan Cox 	    object));
174f8a47341SAlan Cox #endif
1757bfda801SAlan Cox 	KASSERT(object->cache == NULL,
1767bfda801SAlan Cox 	    ("object %p has cached pages",
1777bfda801SAlan Cox 	    object));
1788355f576SJeff Roberson 	KASSERT(object->paging_in_progress == 0,
1798355f576SJeff Roberson 	    ("object %p paging_in_progress = %d",
1808355f576SJeff Roberson 	    object, object->paging_in_progress));
1818355f576SJeff Roberson 	KASSERT(object->resident_page_count == 0,
1828355f576SJeff Roberson 	    ("object %p resident_page_count = %d",
1838355f576SJeff Roberson 	    object, object->resident_page_count));
1848355f576SJeff Roberson 	KASSERT(object->shadow_count == 0,
1858355f576SJeff Roberson 	    ("object %p shadow_count = %d",
1868355f576SJeff Roberson 	    object, object->shadow_count));
1878355f576SJeff Roberson }
1888355f576SJeff Roberson #endif
1898355f576SJeff Roberson 
190b23f72e9SBrian Feldman static int
191b23f72e9SBrian Feldman vm_object_zinit(void *mem, int size, int flags)
1928355f576SJeff Roberson {
1938355f576SJeff Roberson 	vm_object_t object;
1948355f576SJeff Roberson 
1958355f576SJeff Roberson 	object = (vm_object_t)mem;
196e3a9e1b2SAlan Cox 	bzero(&object->mtx, sizeof(object->mtx));
1975285558aSAlan Cox 	VM_OBJECT_LOCK_INIT(object, "standard object");
1988355f576SJeff Roberson 
1998355f576SJeff Roberson 	/* These are true for any object that has been freed */
2008355f576SJeff Roberson 	object->paging_in_progress = 0;
2018355f576SJeff Roberson 	object->resident_page_count = 0;
2028355f576SJeff Roberson 	object->shadow_count = 0;
203b23f72e9SBrian Feldman 	return (0);
2048355f576SJeff Roberson }
205df8bae1dSRodney W. Grimes 
2063075778bSJohn Dyson void
2076395da54SIan Dowse _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
208df8bae1dSRodney W. Grimes {
2090cddd8f0SMatthew Dillon 
210df8bae1dSRodney W. Grimes 	TAILQ_INIT(&object->memq);
2111c500307SAlan Cox 	LIST_INIT(&object->shadow_head);
212a1f6d91cSDavid Greenman 
213b86ec922SMatthew Dillon 	object->root = NULL;
21424a1cce3SDavid Greenman 	object->type = type;
215df8bae1dSRodney W. Grimes 	object->size = size;
216b881da26SAlan Cox 	object->generation = 1;
217a1f6d91cSDavid Greenman 	object->ref_count = 1;
2183153e878SAlan Cox 	object->memattr = VM_MEMATTR_DEFAULT;
21924a1cce3SDavid Greenman 	object->flags = 0;
2203364c323SKonstantin Belousov 	object->uip = NULL;
2213364c323SKonstantin Belousov 	object->charge = 0;
22260517fd1SJohn Baldwin 	if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
2233471677cSAlan Cox 		object->flags = OBJ_ONEMAPPING;
2242446e4f0SAlan Cox 	object->pg_color = 0;
22524a1cce3SDavid Greenman 	object->handle = NULL;
22624a1cce3SDavid Greenman 	object->backing_object = NULL;
227a316d390SJohn Dyson 	object->backing_object_offset = (vm_ooffset_t) 0;
228f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
229f8a47341SAlan Cox 	LIST_INIT(&object->rvq);
230f8a47341SAlan Cox #endif
2317bfda801SAlan Cox 	object->cache = NULL;
232a1f6d91cSDavid Greenman 
233a5698387SAlan Cox 	mtx_lock(&vm_object_list_mtx);
23460517fd1SJohn Baldwin 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
235a5698387SAlan Cox 	mtx_unlock(&vm_object_list_mtx);
236df8bae1dSRodney W. Grimes }
237df8bae1dSRodney W. Grimes 
238df8bae1dSRodney W. Grimes /*
23926f9a767SRodney W. Grimes  *	vm_object_init:
24026f9a767SRodney W. Grimes  *
24126f9a767SRodney W. Grimes  *	Initialize the VM objects module.
24226f9a767SRodney W. Grimes  */
24326f9a767SRodney W. Grimes void
2441b40f8c0SMatthew Dillon vm_object_init(void)
24526f9a767SRodney W. Grimes {
24626f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_list);
2476008862bSJohn Baldwin 	mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF);
2480217125fSDavid Greenman 
2495285558aSAlan Cox 	VM_OBJECT_LOCK_INIT(&kernel_object_store, "kernel object");
2509f5c801bSAlan Cox 	_vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
25126f9a767SRodney W. Grimes 	    kernel_object);
252f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
253f8a47341SAlan Cox 	kernel_object->flags |= OBJ_COLORED;
254f8a47341SAlan Cox 	kernel_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS);
255f8a47341SAlan Cox #endif
25626f9a767SRodney W. Grimes 
2575285558aSAlan Cox 	VM_OBJECT_LOCK_INIT(&kmem_object_store, "kmem object");
2589f5c801bSAlan Cox 	_vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
25926f9a767SRodney W. Grimes 	    kmem_object);
260f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
261f8a47341SAlan Cox 	kmem_object->flags |= OBJ_COLORED;
262f8a47341SAlan Cox 	kmem_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS);
263f8a47341SAlan Cox #endif
264ed6a7863SAlan Cox 
2658dbca793STor Egge 	/*
2668dbca793STor Egge 	 * The lock portion of struct vm_object must be type stable due
2678dbca793STor Egge 	 * to vm_pageout_fallback_object_lock locking a vm object
2688dbca793STor Egge 	 * without holding any references to it.
2698dbca793STor Egge 	 */
2708355f576SJeff Roberson 	obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL,
2718355f576SJeff Roberson #ifdef INVARIANTS
2728355f576SJeff Roberson 	    vm_object_zdtor,
2738355f576SJeff Roberson #else
2748355f576SJeff Roberson 	    NULL,
2758355f576SJeff Roberson #endif
276f3c625e4SJeff Roberson 	    vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM|UMA_ZONE_NOFREE);
27799448ed1SJohn Dyson }
27899448ed1SJohn Dyson 
27999448ed1SJohn Dyson void
2801b40f8c0SMatthew Dillon vm_object_clear_flag(vm_object_t object, u_short bits)
2811b40f8c0SMatthew Dillon {
2825440b5a9SAlan Cox 
283d647a0edSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
284b06805adSJake Burkholder 	object->flags &= ~bits;
2851b40f8c0SMatthew Dillon }
2861b40f8c0SMatthew Dillon 
2873153e878SAlan Cox /*
2883153e878SAlan Cox  *	Sets the default memory attribute for the specified object.  Pages
2893153e878SAlan Cox  *	that are allocated to this object are by default assigned this memory
2903153e878SAlan Cox  *	attribute.
2913153e878SAlan Cox  *
2923153e878SAlan Cox  *	Presently, this function must be called before any pages are allocated
2933153e878SAlan Cox  *	to the object.  In the future, this requirement may be relaxed for
2943153e878SAlan Cox  *	"default" and "swap" objects.
2953153e878SAlan Cox  */
2963153e878SAlan Cox int
2973153e878SAlan Cox vm_object_set_memattr(vm_object_t object, vm_memattr_t memattr)
2983153e878SAlan Cox {
2993153e878SAlan Cox 
3003153e878SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
3013153e878SAlan Cox 	switch (object->type) {
3023153e878SAlan Cox 	case OBJT_DEFAULT:
3033153e878SAlan Cox 	case OBJT_DEVICE:
3043153e878SAlan Cox 	case OBJT_PHYS:
30501381811SJohn Baldwin 	case OBJT_SG:
3063153e878SAlan Cox 	case OBJT_SWAP:
3073153e878SAlan Cox 	case OBJT_VNODE:
3083153e878SAlan Cox 		if (!TAILQ_EMPTY(&object->memq))
3093153e878SAlan Cox 			return (KERN_FAILURE);
3103153e878SAlan Cox 		break;
3113153e878SAlan Cox 	case OBJT_DEAD:
3123153e878SAlan Cox 		return (KERN_INVALID_ARGUMENT);
3133153e878SAlan Cox 	}
3143153e878SAlan Cox 	object->memattr = memattr;
3153153e878SAlan Cox 	return (KERN_SUCCESS);
3163153e878SAlan Cox }
3173153e878SAlan Cox 
3181b40f8c0SMatthew Dillon void
3191b40f8c0SMatthew Dillon vm_object_pip_add(vm_object_t object, short i)
3201b40f8c0SMatthew Dillon {
321f279b88dSAlan Cox 
322d647a0edSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
323b06805adSJake Burkholder 	object->paging_in_progress += i;
3241b40f8c0SMatthew Dillon }
3251b40f8c0SMatthew Dillon 
3261b40f8c0SMatthew Dillon void
3271b40f8c0SMatthew Dillon vm_object_pip_subtract(vm_object_t object, short i)
3281b40f8c0SMatthew Dillon {
329d647a0edSAlan Cox 
3300fa05eaeSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
331b06805adSJake Burkholder 	object->paging_in_progress -= i;
3321b40f8c0SMatthew Dillon }
3331b40f8c0SMatthew Dillon 
3341b40f8c0SMatthew Dillon void
3351b40f8c0SMatthew Dillon vm_object_pip_wakeup(vm_object_t object)
3361b40f8c0SMatthew Dillon {
337f279b88dSAlan Cox 
338d647a0edSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
339b06805adSJake Burkholder 	object->paging_in_progress--;
3401b40f8c0SMatthew Dillon 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
3411b40f8c0SMatthew Dillon 		vm_object_clear_flag(object, OBJ_PIPWNT);
3421b40f8c0SMatthew Dillon 		wakeup(object);
3431b40f8c0SMatthew Dillon 	}
3441b40f8c0SMatthew Dillon }
3451b40f8c0SMatthew Dillon 
3461b40f8c0SMatthew Dillon void
3471b40f8c0SMatthew Dillon vm_object_pip_wakeupn(vm_object_t object, short i)
3481b40f8c0SMatthew Dillon {
349d647a0edSAlan Cox 
3500d420ad3SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
3511b40f8c0SMatthew Dillon 	if (i)
352b06805adSJake Burkholder 		object->paging_in_progress -= i;
3531b40f8c0SMatthew Dillon 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
3541b40f8c0SMatthew Dillon 		vm_object_clear_flag(object, OBJ_PIPWNT);
3551b40f8c0SMatthew Dillon 		wakeup(object);
3561b40f8c0SMatthew Dillon 	}
3571b40f8c0SMatthew Dillon }
3581b40f8c0SMatthew Dillon 
3591b40f8c0SMatthew Dillon void
3601b40f8c0SMatthew Dillon vm_object_pip_wait(vm_object_t object, char *waitid)
3611b40f8c0SMatthew Dillon {
3621ca58953SAlan Cox 
3631ca58953SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
3641ca58953SAlan Cox 	while (object->paging_in_progress) {
3651ca58953SAlan Cox 		object->flags |= OBJ_PIPWNT;
3661ca58953SAlan Cox 		msleep(object, VM_OBJECT_MTX(object), PVM, waitid, 0);
3671ca58953SAlan Cox 	}
3681b40f8c0SMatthew Dillon }
3691b40f8c0SMatthew Dillon 
37026f9a767SRodney W. Grimes /*
37126f9a767SRodney W. Grimes  *	vm_object_allocate:
37226f9a767SRodney W. Grimes  *
37326f9a767SRodney W. Grimes  *	Returns a new object with the given size.
37426f9a767SRodney W. Grimes  */
37526f9a767SRodney W. Grimes vm_object_t
3766395da54SIan Dowse vm_object_allocate(objtype_t type, vm_pindex_t size)
37726f9a767SRodney W. Grimes {
37890688d13SAlan Cox 	vm_object_t object;
37990688d13SAlan Cox 
38090688d13SAlan Cox 	object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK);
38190688d13SAlan Cox 	_vm_object_allocate(type, size, object);
38290688d13SAlan Cox 	return (object);
38326f9a767SRodney W. Grimes }
38426f9a767SRodney W. Grimes 
38526f9a767SRodney W. Grimes 
38626f9a767SRodney W. Grimes /*
387df8bae1dSRodney W. Grimes  *	vm_object_reference:
388df8bae1dSRodney W. Grimes  *
38915347817SAlan Cox  *	Gets another reference to the given object.  Note: OBJ_DEAD
39015347817SAlan Cox  *	objects can be referenced during final cleaning.
391df8bae1dSRodney W. Grimes  */
3926476c0d2SJohn Dyson void
3931b40f8c0SMatthew Dillon vm_object_reference(vm_object_t object)
394df8bae1dSRodney W. Grimes {
395df8bae1dSRodney W. Grimes 	if (object == NULL)
396df8bae1dSRodney W. Grimes 		return;
39715347817SAlan Cox 	VM_OBJECT_LOCK(object);
39852481a9aSJeff Roberson 	vm_object_reference_locked(object);
39982f9defeSAlan Cox 	VM_OBJECT_UNLOCK(object);
40095e5e988SJohn Dyson }
40195e5e988SJohn Dyson 
40223955314SAlfred Perlstein /*
403b921a12bSAlan Cox  *	vm_object_reference_locked:
404b921a12bSAlan Cox  *
405b921a12bSAlan Cox  *	Gets another reference to the given object.
406b921a12bSAlan Cox  *
407b921a12bSAlan Cox  *	The object must be locked.
408b921a12bSAlan Cox  */
409b921a12bSAlan Cox void
410b921a12bSAlan Cox vm_object_reference_locked(vm_object_t object)
411b921a12bSAlan Cox {
412b921a12bSAlan Cox 	struct vnode *vp;
413b921a12bSAlan Cox 
414b921a12bSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
415b921a12bSAlan Cox 	object->ref_count++;
416b921a12bSAlan Cox 	if (object->type == OBJT_VNODE) {
417b921a12bSAlan Cox 		vp = object->handle;
418b921a12bSAlan Cox 		vref(vp);
419b921a12bSAlan Cox 	}
420b921a12bSAlan Cox }
421b921a12bSAlan Cox 
422b921a12bSAlan Cox /*
4239d5abbddSJens Schweikhardt  * Handle deallocating an object of type OBJT_VNODE.
42423955314SAlfred Perlstein  */
42502dd8331SAlan Cox static void
4261b40f8c0SMatthew Dillon vm_object_vndeallocate(vm_object_t object)
42795e5e988SJohn Dyson {
42895e5e988SJohn Dyson 	struct vnode *vp = (struct vnode *) object->handle;
429219cbf59SEivind Eklund 
430ae51ff11SJeff Roberson 	VFS_ASSERT_GIANT(vp->v_mount);
431ad682c48SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
4325526d2d9SEivind Eklund 	KASSERT(object->type == OBJT_VNODE,
4335526d2d9SEivind Eklund 	    ("vm_object_vndeallocate: not a vnode object"));
434219cbf59SEivind Eklund 	KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
435219cbf59SEivind Eklund #ifdef INVARIANTS
43695e5e988SJohn Dyson 	if (object->ref_count == 0) {
43795e5e988SJohn Dyson 		vprint("vm_object_vndeallocate", vp);
43895e5e988SJohn Dyson 		panic("vm_object_vndeallocate: bad object reference count");
43995e5e988SJohn Dyson 	}
44095e5e988SJohn Dyson #endif
44195e5e988SJohn Dyson 
44295e5e988SJohn Dyson 	object->ref_count--;
44347221757SJohn Dyson 	if (object->ref_count == 0) {
444e6e370a7SJeff Roberson 		mp_fixme("Unlocked vflag access.");
445e6e370a7SJeff Roberson 		vp->v_vflag &= ~VV_TEXT;
4462be70f79SJohn Dyson 	}
447ad682c48SAlan Cox 	VM_OBJECT_UNLOCK(object);
44823955314SAlfred Perlstein 	/*
44923955314SAlfred Perlstein 	 * vrele may need a vop lock
45023955314SAlfred Perlstein 	 */
45147221757SJohn Dyson 	vrele(vp);
452df8bae1dSRodney W. Grimes }
453df8bae1dSRodney W. Grimes 
454df8bae1dSRodney W. Grimes /*
455df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
456df8bae1dSRodney W. Grimes  *
457df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
458df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
459df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
460df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
461df8bae1dSRodney W. Grimes  *	may be relinquished.
462df8bae1dSRodney W. Grimes  *
463df8bae1dSRodney W. Grimes  *	No object may be locked.
464df8bae1dSRodney W. Grimes  */
46526f9a767SRodney W. Grimes void
4661b40f8c0SMatthew Dillon vm_object_deallocate(vm_object_t object)
467df8bae1dSRodney W. Grimes {
468df8bae1dSRodney W. Grimes 	vm_object_t temp;
469df8bae1dSRodney W. Grimes 
470df8bae1dSRodney W. Grimes 	while (object != NULL) {
471ae51ff11SJeff Roberson 		int vfslocked;
472ca95b514SJohn Baldwin 
473ae51ff11SJeff Roberson 		vfslocked = 0;
474ca95b514SJohn Baldwin 	restart:
475ad682c48SAlan Cox 		VM_OBJECT_LOCK(object);
4763b68228cSAlan Cox 		if (object->type == OBJT_VNODE) {
477ca95b514SJohn Baldwin 			struct vnode *vp = (struct vnode *) object->handle;
478ca95b514SJohn Baldwin 
479ca95b514SJohn Baldwin 			/*
480ca95b514SJohn Baldwin 			 * Conditionally acquire Giant for a vnode-backed
481ca95b514SJohn Baldwin 			 * object.  We have to be careful since the type of
482ca95b514SJohn Baldwin 			 * a vnode object can change while the object is
483ca95b514SJohn Baldwin 			 * unlocked.
484ca95b514SJohn Baldwin 			 */
485ca95b514SJohn Baldwin 			if (VFS_NEEDSGIANT(vp->v_mount) && !vfslocked) {
486ca95b514SJohn Baldwin 				vfslocked = 1;
487ca95b514SJohn Baldwin 				if (!mtx_trylock(&Giant)) {
488ca95b514SJohn Baldwin 					VM_OBJECT_UNLOCK(object);
489ca95b514SJohn Baldwin 					mtx_lock(&Giant);
490ca95b514SJohn Baldwin 					goto restart;
491ca95b514SJohn Baldwin 				}
492ca95b514SJohn Baldwin 			}
49395e5e988SJohn Dyson 			vm_object_vndeallocate(object);
494ae51ff11SJeff Roberson 			VFS_UNLOCK_GIANT(vfslocked);
49523b186d3SAlan Cox 			return;
496ca95b514SJohn Baldwin 		} else
497ca95b514SJohn Baldwin 			/*
498ca95b514SJohn Baldwin 			 * This is to handle the case that the object
499ca95b514SJohn Baldwin 			 * changed type while we dropped its lock to
500ca95b514SJohn Baldwin 			 * obtain Giant.
501ca95b514SJohn Baldwin 			 */
502ca95b514SJohn Baldwin 			VFS_UNLOCK_GIANT(vfslocked);
50395e5e988SJohn Dyson 
5048125b1e6SAlfred Perlstein 		KASSERT(object->ref_count != 0,
5058125b1e6SAlfred Perlstein 			("vm_object_deallocate: object deallocated too many times: %d", object->type));
5062be70f79SJohn Dyson 
5072be70f79SJohn Dyson 		/*
5088125b1e6SAlfred Perlstein 		 * If the reference count goes to 0 we start calling
5098125b1e6SAlfred Perlstein 		 * vm_object_terminate() on the object chain.
5108125b1e6SAlfred Perlstein 		 * A ref count of 1 may be a special case depending on the
5118125b1e6SAlfred Perlstein 		 * shadow count being 0 or 1.
5122be70f79SJohn Dyson 		 */
513c0877f10SJohn Dyson 		object->ref_count--;
5148125b1e6SAlfred Perlstein 		if (object->ref_count > 1) {
5153b68228cSAlan Cox 			VM_OBJECT_UNLOCK(object);
51623b186d3SAlan Cox 			return;
5178125b1e6SAlfred Perlstein 		} else if (object->ref_count == 1) {
5184c8e0452SAlan Cox 			if (object->shadow_count == 0 &&
5194c8e0452SAlan Cox 			    object->handle == NULL &&
5204c8e0452SAlan Cox 			    (object->type == OBJT_DEFAULT ||
5214c8e0452SAlan Cox 			     object->type == OBJT_SWAP)) {
5228125b1e6SAlfred Perlstein 				vm_object_set_flag(object, OBJ_ONEMAPPING);
5238125b1e6SAlfred Perlstein 			} else if ((object->shadow_count == 1) &&
5248125b1e6SAlfred Perlstein 			    (object->handle == NULL) &&
52524a1cce3SDavid Greenman 			    (object->type == OBJT_DEFAULT ||
52624a1cce3SDavid Greenman 			     object->type == OBJT_SWAP)) {
527a1f6d91cSDavid Greenman 				vm_object_t robject;
52895e5e988SJohn Dyson 
5291c500307SAlan Cox 				robject = LIST_FIRST(&object->shadow_head);
5305526d2d9SEivind Eklund 				KASSERT(robject != NULL,
531219cbf59SEivind Eklund 				    ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
5325526d2d9SEivind Eklund 					 object->ref_count,
5335526d2d9SEivind Eklund 					 object->shadow_count));
534b72b0115SAlan Cox 				if (!VM_OBJECT_TRYLOCK(robject)) {
535b72b0115SAlan Cox 					/*
536b72b0115SAlan Cox 					 * Avoid a potential deadlock.
537b72b0115SAlan Cox 					 */
538b72b0115SAlan Cox 					object->ref_count++;
539b72b0115SAlan Cox 					VM_OBJECT_UNLOCK(object);
540a7d86121SAlan Cox 					/*
541a7d86121SAlan Cox 					 * More likely than not the thread
542a7d86121SAlan Cox 					 * holding robject's lock has lower
543a7d86121SAlan Cox 					 * priority than the current thread.
544a7d86121SAlan Cox 					 * Let the lower priority thread run.
545a7d86121SAlan Cox 					 */
5468db5fc58SJohn Baldwin 					pause("vmo_de", 1);
547b72b0115SAlan Cox 					continue;
548b72b0115SAlan Cox 				}
549d936694fSAlan Cox 				/*
550d936694fSAlan Cox 				 * Collapse object into its shadow unless its
551d936694fSAlan Cox 				 * shadow is dead.  In that case, object will
552d936694fSAlan Cox 				 * be deallocated by the thread that is
553d936694fSAlan Cox 				 * deallocating its shadow.
554d936694fSAlan Cox 				 */
555d936694fSAlan Cox 				if ((robject->flags & OBJ_DEAD) == 0 &&
556d936694fSAlan Cox 				    (robject->handle == NULL) &&
55724a1cce3SDavid Greenman 				    (robject->type == OBJT_DEFAULT ||
55824a1cce3SDavid Greenman 				     robject->type == OBJT_SWAP)) {
559a1f6d91cSDavid Greenman 
56095e5e988SJohn Dyson 					robject->ref_count++;
561138449dcSAlan Cox retry:
562138449dcSAlan Cox 					if (robject->paging_in_progress) {
563138449dcSAlan Cox 						VM_OBJECT_UNLOCK(object);
564138449dcSAlan Cox 						vm_object_pip_wait(robject,
565138449dcSAlan Cox 						    "objde1");
5662e9f4a69SAlan Cox 						temp = robject->backing_object;
5672e9f4a69SAlan Cox 						if (object == temp) {
568138449dcSAlan Cox 							VM_OBJECT_LOCK(object);
569138449dcSAlan Cox 							goto retry;
5702e9f4a69SAlan Cox 						}
571138449dcSAlan Cox 					} else if (object->paging_in_progress) {
572138449dcSAlan Cox 						VM_OBJECT_UNLOCK(robject);
573138449dcSAlan Cox 						object->flags |= OBJ_PIPWNT;
574138449dcSAlan Cox 						msleep(object,
575138449dcSAlan Cox 						    VM_OBJECT_MTX(object),
576138449dcSAlan Cox 						    PDROP | PVM, "objde2", 0);
577138449dcSAlan Cox 						VM_OBJECT_LOCK(robject);
5782e9f4a69SAlan Cox 						temp = robject->backing_object;
5792e9f4a69SAlan Cox 						if (object == temp) {
580138449dcSAlan Cox 							VM_OBJECT_LOCK(object);
581138449dcSAlan Cox 							goto retry;
582a1f6d91cSDavid Greenman 						}
5832e9f4a69SAlan Cox 					} else
5843b68228cSAlan Cox 						VM_OBJECT_UNLOCK(object);
5852e9f4a69SAlan Cox 
58695e5e988SJohn Dyson 					if (robject->ref_count == 1) {
58795e5e988SJohn Dyson 						robject->ref_count--;
588ba8da839SDavid Greenman 						object = robject;
58995e5e988SJohn Dyson 						goto doterm;
59095e5e988SJohn Dyson 					}
59195e5e988SJohn Dyson 					object = robject;
59295e5e988SJohn Dyson 					vm_object_collapse(object);
593d7fc2210SAlan Cox 					VM_OBJECT_UNLOCK(object);
594ba8da839SDavid Greenman 					continue;
595a1f6d91cSDavid Greenman 				}
596b72b0115SAlan Cox 				VM_OBJECT_UNLOCK(robject);
59795e5e988SJohn Dyson 			}
5983b68228cSAlan Cox 			VM_OBJECT_UNLOCK(object);
59923b186d3SAlan Cox 			return;
60095e5e988SJohn Dyson 		}
60195e5e988SJohn Dyson doterm:
60224a1cce3SDavid Greenman 		temp = object->backing_object;
603c9917419SAlan Cox 		if (temp != NULL) {
604c9917419SAlan Cox 			VM_OBJECT_LOCK(temp);
6051c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
60695e5e988SJohn Dyson 			temp->shadow_count--;
607eaf13dd7SJohn Dyson 			temp->generation++;
608c9917419SAlan Cox 			VM_OBJECT_UNLOCK(temp);
60995461b45SJohn Dyson 			object->backing_object = NULL;
610de5f6a77SJohn Dyson 		}
611245df27cSMatthew Dillon 		/*
612245df27cSMatthew Dillon 		 * Don't double-terminate, we could be in a termination
613245df27cSMatthew Dillon 		 * recursion due to the terminate having to sync data
614245df27cSMatthew Dillon 		 * to disk.
615245df27cSMatthew Dillon 		 */
616245df27cSMatthew Dillon 		if ((object->flags & OBJ_DEAD) == 0)
617df8bae1dSRodney W. Grimes 			vm_object_terminate(object);
618c829b9d0SAlan Cox 		else
619c829b9d0SAlan Cox 			VM_OBJECT_UNLOCK(object);
620df8bae1dSRodney W. Grimes 		object = temp;
621df8bae1dSRodney W. Grimes 	}
622df8bae1dSRodney W. Grimes }
623df8bae1dSRodney W. Grimes 
624df8bae1dSRodney W. Grimes /*
6252ac78f0eSStephan Uphoff  *	vm_object_destroy removes the object from the global object list
6262ac78f0eSStephan Uphoff  *      and frees the space for the object.
6272ac78f0eSStephan Uphoff  */
6282ac78f0eSStephan Uphoff void
6292ac78f0eSStephan Uphoff vm_object_destroy(vm_object_t object)
6302ac78f0eSStephan Uphoff {
6312ac78f0eSStephan Uphoff 
6322ac78f0eSStephan Uphoff 	/*
6332ac78f0eSStephan Uphoff 	 * Remove the object from the global object list.
6342ac78f0eSStephan Uphoff 	 */
6352ac78f0eSStephan Uphoff 	mtx_lock(&vm_object_list_mtx);
6362ac78f0eSStephan Uphoff 	TAILQ_REMOVE(&vm_object_list, object, object_list);
6372ac78f0eSStephan Uphoff 	mtx_unlock(&vm_object_list_mtx);
6382ac78f0eSStephan Uphoff 
6392ac78f0eSStephan Uphoff 	/*
6403364c323SKonstantin Belousov 	 * Release the allocation charge.
6413364c323SKonstantin Belousov 	 */
6423364c323SKonstantin Belousov 	if (object->uip != NULL) {
6433364c323SKonstantin Belousov 		KASSERT(object->type == OBJT_DEFAULT ||
6443364c323SKonstantin Belousov 		    object->type == OBJT_SWAP,
6453364c323SKonstantin Belousov 		    ("vm_object_terminate: non-swap obj %p has uip",
6463364c323SKonstantin Belousov 		     object));
6473364c323SKonstantin Belousov 		swap_release_by_uid(object->charge, object->uip);
6483364c323SKonstantin Belousov 		object->charge = 0;
6493364c323SKonstantin Belousov 		uifree(object->uip);
6503364c323SKonstantin Belousov 		object->uip = NULL;
6513364c323SKonstantin Belousov 	}
6523364c323SKonstantin Belousov 
6533364c323SKonstantin Belousov 	/*
6542ac78f0eSStephan Uphoff 	 * Free the space for the object.
6552ac78f0eSStephan Uphoff 	 */
6562ac78f0eSStephan Uphoff 	uma_zfree(obj_zone, object);
6572ac78f0eSStephan Uphoff }
6582ac78f0eSStephan Uphoff 
6592ac78f0eSStephan Uphoff /*
660df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
661df8bae1dSRodney W. Grimes  *	up all previously used resources.
662df8bae1dSRodney W. Grimes  *
663df8bae1dSRodney W. Grimes  *	The object must be locked.
6641c7c3c6aSMatthew Dillon  *	This routine may block.
665df8bae1dSRodney W. Grimes  */
66695e5e988SJohn Dyson void
6671b40f8c0SMatthew Dillon vm_object_terminate(vm_object_t object)
668df8bae1dSRodney W. Grimes {
669d031cff1SMatthew Dillon 	vm_page_t p;
670df8bae1dSRodney W. Grimes 
671c829b9d0SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
6720cddd8f0SMatthew Dillon 
67395e5e988SJohn Dyson 	/*
67495e5e988SJohn Dyson 	 * Make sure no one uses us.
67595e5e988SJohn Dyson 	 */
676069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_DEAD);
6773c631446SJohn Dyson 
678df8bae1dSRodney W. Grimes 	/*
679f6b04d2bSDavid Greenman 	 * wait for the pageout daemon to be done with the object
680df8bae1dSRodney W. Grimes 	 */
68166095752SJohn Dyson 	vm_object_pip_wait(object, "objtrm");
682df8bae1dSRodney W. Grimes 
6835526d2d9SEivind Eklund 	KASSERT(!object->paging_in_progress,
6845526d2d9SEivind Eklund 		("vm_object_terminate: pageout in progress"));
68526f9a767SRodney W. Grimes 
68626f9a767SRodney W. Grimes 	/*
6870d94caffSDavid Greenman 	 * Clean and free the pages, as appropriate. All references to the
6880d94caffSDavid Greenman 	 * object are gone, so we don't need to lock it.
68926f9a767SRodney W. Grimes 	 */
69024a1cce3SDavid Greenman 	if (object->type == OBJT_VNODE) {
691f7dd7b63SAlan Cox 		struct vnode *vp = (struct vnode *)object->handle;
69295e5e988SJohn Dyson 
69395e5e988SJohn Dyson 		/*
69495e5e988SJohn Dyson 		 * Clean pages and flush buffers.
69595e5e988SJohn Dyson 		 */
6968f9110f6SJohn Dyson 		vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
697b6e48e03SAlan Cox 		VM_OBJECT_UNLOCK(object);
69895e5e988SJohn Dyson 
6990d7935fdSAttilio Rao 		vinvalbuf(vp, V_SAVE, 0, 0);
700f7dd7b63SAlan Cox 
701f7dd7b63SAlan Cox 		VM_OBJECT_LOCK(object);
702bef608bdSJohn Dyson 	}
703bef608bdSJohn Dyson 
704971dd342SAlfred Perlstein 	KASSERT(object->ref_count == 0,
705971dd342SAlfred Perlstein 		("vm_object_terminate: object with references, ref_count=%d",
706971dd342SAlfred Perlstein 		object->ref_count));
707996c772fSJohn Dyson 
7080d94caffSDavid Greenman 	/*
709356863ebSDavid Greenman 	 * Now free any remaining pages. For internal objects, this also
710356863ebSDavid Greenman 	 * removes them from paging queues. Don't free wired pages, just
711356863ebSDavid Greenman 	 * remove them from the object.
712df8bae1dSRodney W. Grimes 	 */
713b18bfc3dSJohn Dyson 	while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
7149af80719SAlan Cox 		KASSERT(!p->busy && (p->oflags & VPO_BUSY) == 0,
715971dd342SAlfred Perlstein 			("vm_object_terminate: freeing busy page %p "
716e9f54126SRobert Noland 			"p->busy = %d, p->oflags %x\n", p, p->busy, p->oflags));
7172965a453SKip Macy 		vm_page_lock(p);
7180b10ba98SDavid Greenman 		if (p->wire_count == 0) {
719df8bae1dSRodney W. Grimes 			vm_page_free(p);
72070721880SAlan Cox 			PCPU_INC(cnt.v_pfree);
72170721880SAlan Cox 		} else
7220b10ba98SDavid Greenman 			vm_page_remove(p);
7232965a453SKip Macy 		vm_page_unlock(p);
7242965a453SKip Macy 	}
725bef608bdSJohn Dyson 
726f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
727f8a47341SAlan Cox 	if (__predict_false(!LIST_EMPTY(&object->rvq)))
728f8a47341SAlan Cox 		vm_reserv_break_all(object);
729f8a47341SAlan Cox #endif
7307bfda801SAlan Cox 	if (__predict_false(object->cache != NULL))
731c9444914SAlan Cox 		vm_page_cache_free(object, 0, 0);
7327bfda801SAlan Cox 
7332d8acc0fSJohn Dyson 	/*
7349fcfb650SDavid Greenman 	 * Let the pager know object is dead.
7359fcfb650SDavid Greenman 	 */
7369fcfb650SDavid Greenman 	vm_pager_deallocate(object);
737658ad5ffSAlan Cox 	VM_OBJECT_UNLOCK(object);
7389fcfb650SDavid Greenman 
7392ac78f0eSStephan Uphoff 	vm_object_destroy(object);
74047221757SJohn Dyson }
741df8bae1dSRodney W. Grimes 
742df8bae1dSRodney W. Grimes /*
743df8bae1dSRodney W. Grimes  *	vm_object_page_clean
744df8bae1dSRodney W. Grimes  *
7454f79d873SMatthew Dillon  *	Clean all dirty pages in the specified range of object.  Leaves page
7464f79d873SMatthew Dillon  * 	on whatever queue it is currently on.   If NOSYNC is set then do not
747b146f9e5SAlan Cox  *	write out pages with VPO_NOSYNC set (originally comes from MAP_NOSYNC),
7484f79d873SMatthew Dillon  *	leaving the object dirty.
74926f9a767SRodney W. Grimes  *
75043b7990eSMatthew Dillon  *	When stuffing pages asynchronously, allow clustering.  XXX we need a
75143b7990eSMatthew Dillon  *	synchronous clustering mode implementation.
75243b7990eSMatthew Dillon  *
75326f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
75426f9a767SRodney W. Grimes  *
75526f9a767SRodney W. Grimes  *	The object must be locked.
75626f9a767SRodney W. Grimes  */
757f6b04d2bSDavid Greenman void
758*e239bb97SKonstantin Belousov vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
759*e239bb97SKonstantin Belousov     int flags)
760f6b04d2bSDavid Greenman {
761*e239bb97SKonstantin Belousov 	vm_page_t np, p;
762*e239bb97SKonstantin Belousov 	vm_pindex_t pi, tend;
763*e239bb97SKonstantin Belousov 	int clearobjflags, curgeneration, n, pagerflags;
764f6b04d2bSDavid Greenman 
7657bec141bSKip Macy 	mtx_assert(&vm_page_queue_mtx, MA_NOTOWNED);
766b6e48e03SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
767*e239bb97SKonstantin Belousov 	if ((object->flags & OBJ_MIGHTBEDIRTY) == 0 ||
768*e239bb97SKonstantin Belousov 	    object->resident_page_count == 0)
769f6b04d2bSDavid Greenman 		return;
77049e3050eSKonstantin Belousov 	KASSERT(object->type == OBJT_VNODE, ("Not a vnode object"));
771f6b04d2bSDavid Greenman 
772*e239bb97SKonstantin Belousov 	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) != 0 ?
773*e239bb97SKonstantin Belousov 	    VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
774*e239bb97SKonstantin Belousov 	pagerflags |= (flags & OBJPC_INVAL) != 0 ? VM_PAGER_PUT_INVAL : 0;
775*e239bb97SKonstantin Belousov 
776*e239bb97SKonstantin Belousov 	tend = (end == 0) ? object->size : end;
7778f9110f6SJohn Dyson 
778069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_CLEANING);
77924a1cce3SDavid Greenman 
7804f79d873SMatthew Dillon 	/*
781*e239bb97SKonstantin Belousov 	 * Make the page read-only so we can then clear the object flags.
7824f79d873SMatthew Dillon 	 *
7834f79d873SMatthew Dillon 	 * However, if this is a nosync mmap then the object is likely to
7844f79d873SMatthew Dillon 	 * stay dirty so do not mess with the page and do not clear the
7854f79d873SMatthew Dillon 	 * object flags.
7864f79d873SMatthew Dillon 	 */
7874f79d873SMatthew Dillon 	clearobjflags = 1;
788*e239bb97SKonstantin Belousov 	for (p = vm_page_find_least(object, start);
789*e239bb97SKonstantin Belousov 	    p != NULL && p->pindex < tend; p = TAILQ_NEXT(p, listq)) {
790*e239bb97SKonstantin Belousov 		if ((flags & OBJPC_NOSYNC) != 0 &&
791*e239bb97SKonstantin Belousov 		    (p->oflags & VPO_NOSYNC) != 0)
7924f79d873SMatthew Dillon 			clearobjflags = 0;
7933c4a2440SAlan Cox 		else
79478985e42SAlan Cox 			pmap_remove_write(p);
795eaf13dd7SJohn Dyson 	}
796eaf13dd7SJohn Dyson 
797*e239bb97SKonstantin Belousov 	if (clearobjflags && (start == 0) && (tend == object->size))
798af51d7bfSAlan Cox 		vm_object_clear_flag(object, OBJ_MIGHTBEDIRTY);
799f6b04d2bSDavid Greenman 
800bd7e5f99SJohn Dyson rescan:
801*e239bb97SKonstantin Belousov 	p = vm_page_find_least(object, start);
8022d8acc0fSJohn Dyson 	curgeneration = object->generation;
8032d8acc0fSJohn Dyson 
804*e239bb97SKonstantin Belousov 	for (; p != NULL; p = np) {
805bd7e5f99SJohn Dyson 		pi = p->pindex;
806*e239bb97SKonstantin Belousov 		if (pi >= tend)
807*e239bb97SKonstantin Belousov 			break;
808*e239bb97SKonstantin Belousov 		np = TAILQ_NEXT(p, listq);
809*e239bb97SKonstantin Belousov 		if (p->valid == 0)
810aef922f5SJohn Dyson 			continue;
811*e239bb97SKonstantin Belousov 		while (vm_page_sleep_if_busy(p, TRUE, "vpcwai")) {
812*e239bb97SKonstantin Belousov 			if (object->generation != curgeneration)
813*e239bb97SKonstantin Belousov 				goto rescan;
814f6b04d2bSDavid Greenman 		}
815bd7e5f99SJohn Dyson 		vm_page_test_dirty(p);
816*e239bb97SKonstantin Belousov 		if (p->dirty == 0)
817bd7e5f99SJohn Dyson 			continue;
818*e239bb97SKonstantin Belousov 
8194f79d873SMatthew Dillon 		/*
8204f79d873SMatthew Dillon 		 * If we have been asked to skip nosync pages and this is a
8214f79d873SMatthew Dillon 		 * nosync page, skip it.  Note that the object flags were
8224f79d873SMatthew Dillon 		 * not cleared in this case so we do not have to set them.
8234f79d873SMatthew Dillon 		 */
824*e239bb97SKonstantin Belousov 		if ((flags & OBJPC_NOSYNC) != 0 &&
825*e239bb97SKonstantin Belousov 		    (p->oflags & VPO_NOSYNC) != 0)
8264f79d873SMatthew Dillon 			continue;
8274f79d873SMatthew Dillon 
828b9b7a4beSMatthew Dillon 		n = vm_object_page_collect_flush(object, p,
829b9b7a4beSMatthew Dillon 		    curgeneration, pagerflags);
830*e239bb97SKonstantin Belousov 		KASSERT(n > 0, ("vm_object_page_collect_flush failed"));
8317bec141bSKip Macy 		if (object->generation != curgeneration)
832b9b7a4beSMatthew Dillon 			goto rescan;
833*e239bb97SKonstantin Belousov 		np = vm_page_find_least(object, pi + n);
834b9b7a4beSMatthew Dillon 	}
835b9b7a4beSMatthew Dillon #if 0
836*e239bb97SKonstantin Belousov 	VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC) ? MNT_WAIT : 0);
837b9b7a4beSMatthew Dillon #endif
838b9b7a4beSMatthew Dillon 
839b9b7a4beSMatthew Dillon 	vm_object_clear_flag(object, OBJ_CLEANING);
840b9b7a4beSMatthew Dillon }
841b9b7a4beSMatthew Dillon 
842b9b7a4beSMatthew Dillon static int
843*e239bb97SKonstantin Belousov vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration,
844*e239bb97SKonstantin Belousov     int pagerflags)
845b9b7a4beSMatthew Dillon {
846b9b7a4beSMatthew Dillon 	int runlen;
847b9b7a4beSMatthew Dillon 	int maxf;
848b9b7a4beSMatthew Dillon 	int chkb;
849b9b7a4beSMatthew Dillon 	int maxb;
850*e239bb97SKonstantin Belousov 	int i, index;
851b9b7a4beSMatthew Dillon 	vm_pindex_t pi;
852b9b7a4beSMatthew Dillon 	vm_page_t maf[vm_pageout_page_count];
853b9b7a4beSMatthew Dillon 	vm_page_t mab[vm_pageout_page_count];
854b9b7a4beSMatthew Dillon 	vm_page_t ma[vm_pageout_page_count];
855*e239bb97SKonstantin Belousov 	vm_page_t tp, p1;
856b9b7a4beSMatthew Dillon 
8577bec141bSKip Macy 	mtx_assert(&vm_page_queue_mtx, MA_NOTOWNED);
8587bec141bSKip Macy 	vm_page_lock_assert(p, MA_NOTOWNED);
8597bec141bSKip Macy 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
860b9b7a4beSMatthew Dillon 	pi = p->pindex;
861bd7e5f99SJohn Dyson 	maxf = 0;
862*e239bb97SKonstantin Belousov 	for (i = 1, p1 = p; i < vm_pageout_page_count; i++) {
863*e239bb97SKonstantin Belousov 		tp = vm_page_next(p1);
864*e239bb97SKonstantin Belousov 		if (tp == NULL || tp->busy != 0 || (tp->oflags & VPO_BUSY) != 0)
865bd7e5f99SJohn Dyson 			break;
866bd7e5f99SJohn Dyson 		vm_page_test_dirty(tp);
867*e239bb97SKonstantin Belousov 		if (tp->dirty == 0)
868bd7e5f99SJohn Dyson 			break;
869*e239bb97SKonstantin Belousov 		maf[i - 1] = p1 = tp;
870bd7e5f99SJohn Dyson 		maxf++;
871bd7e5f99SJohn Dyson 	}
872aef922f5SJohn Dyson 
873bd7e5f99SJohn Dyson 	maxb = 0;
874bd7e5f99SJohn Dyson 	chkb = vm_pageout_page_count -  maxf;
875*e239bb97SKonstantin Belousov 	for (i = 1, p1 = p; i < chkb; i++) {
876*e239bb97SKonstantin Belousov 		tp = vm_page_prev(p1);
877*e239bb97SKonstantin Belousov 		if (tp == NULL || tp->busy != 0 || (tp->oflags & VPO_BUSY) != 0)
878bd7e5f99SJohn Dyson 			break;
879bd7e5f99SJohn Dyson 		vm_page_test_dirty(tp);
880*e239bb97SKonstantin Belousov 		if (tp->dirty == 0)
881bd7e5f99SJohn Dyson 			break;
882*e239bb97SKonstantin Belousov 		mab[i - 1] = p1 = tp;
883bd7e5f99SJohn Dyson 		maxb++;
884bd7e5f99SJohn Dyson 	}
885bd7e5f99SJohn Dyson 
886bd7e5f99SJohn Dyson 	for (i = 0; i < maxb; i++) {
887*e239bb97SKonstantin Belousov 		index = (maxb - i) - 1;
888bd7e5f99SJohn Dyson 		ma[index] = mab[i];
889bd7e5f99SJohn Dyson 	}
890bd7e5f99SJohn Dyson 	ma[maxb] = p;
891bd7e5f99SJohn Dyson 	for (i = 0; i < maxf; i++) {
892*e239bb97SKonstantin Belousov 		index = (maxb + i) + 1;
893bd7e5f99SJohn Dyson 		ma[index] = maf[i];
894f6b04d2bSDavid Greenman 	}
895bd7e5f99SJohn Dyson 	runlen = maxb + maxf + 1;
896cf2819ccSJohn Dyson 
8977a935082SAlan Cox 	vm_pageout_flush(ma, runlen, pagerflags);
898cf2819ccSJohn Dyson 	for (i = 0; i < runlen; i++) {
899*e239bb97SKonstantin Belousov 		if (ma[i]->dirty != 0) {
900*e239bb97SKonstantin Belousov 			KASSERT((ma[i]->flags & PG_WRITEABLE) == 0,
901*e239bb97SKonstantin Belousov 	("vm_object_page_collect_flush: page %p is not write protected",
902*e239bb97SKonstantin Belousov 			    ma[i]));
903*e239bb97SKonstantin Belousov 		}
904*e239bb97SKonstantin Belousov 	}
905*e239bb97SKonstantin Belousov 	for (i = 0; i < maxf; i++) {
906*e239bb97SKonstantin Belousov 		if (ma[i + maxb + 1]->dirty != 0) {
907b9b7a4beSMatthew Dillon 			/*
908b9b7a4beSMatthew Dillon 			 * maxf will end up being the actual number of pages
909b9b7a4beSMatthew Dillon 			 * we wrote out contiguously, non-inclusive of the
910b9b7a4beSMatthew Dillon 			 * first page.  We do not count look-behind pages.
911b9b7a4beSMatthew Dillon 			 */
912*e239bb97SKonstantin Belousov 			if (maxf > i) {
913*e239bb97SKonstantin Belousov 				maxf = i;
914*e239bb97SKonstantin Belousov 				break;
915*e239bb97SKonstantin Belousov 			}
916b9b7a4beSMatthew Dillon 		}
917b9b7a4beSMatthew Dillon 	}
918b9b7a4beSMatthew Dillon 	return (maxf + 1);
91926f9a767SRodney W. Grimes }
920df8bae1dSRodney W. Grimes 
9211efb74fbSJohn Dyson /*
922950f8459SAlan Cox  * Note that there is absolutely no sense in writing out
923950f8459SAlan Cox  * anonymous objects, so we track down the vnode object
924950f8459SAlan Cox  * to write out.
925950f8459SAlan Cox  * We invalidate (remove) all pages from the address space
926950f8459SAlan Cox  * for semantic correctness.
927950f8459SAlan Cox  *
928950f8459SAlan Cox  * Note: certain anonymous maps, such as MAP_NOSYNC maps,
929950f8459SAlan Cox  * may start out with a NULL object.
930950f8459SAlan Cox  */
931950f8459SAlan Cox void
932950f8459SAlan Cox vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size,
933950f8459SAlan Cox     boolean_t syncio, boolean_t invalidate)
934950f8459SAlan Cox {
935950f8459SAlan Cox 	vm_object_t backing_object;
936950f8459SAlan Cox 	struct vnode *vp;
9373b582b4eSTor Egge 	struct mount *mp;
938950f8459SAlan Cox 	int flags;
939950f8459SAlan Cox 
940950f8459SAlan Cox 	if (object == NULL)
941950f8459SAlan Cox 		return;
942950f8459SAlan Cox 	VM_OBJECT_LOCK(object);
943950f8459SAlan Cox 	while ((backing_object = object->backing_object) != NULL) {
944950f8459SAlan Cox 		VM_OBJECT_LOCK(backing_object);
94556e0670fSAlan Cox 		offset += object->backing_object_offset;
946950f8459SAlan Cox 		VM_OBJECT_UNLOCK(object);
947950f8459SAlan Cox 		object = backing_object;
948950f8459SAlan Cox 		if (object->size < OFF_TO_IDX(offset + size))
949950f8459SAlan Cox 			size = IDX_TO_OFF(object->size) - offset;
950950f8459SAlan Cox 	}
951950f8459SAlan Cox 	/*
952950f8459SAlan Cox 	 * Flush pages if writing is allowed, invalidate them
953950f8459SAlan Cox 	 * if invalidation requested.  Pages undergoing I/O
954950f8459SAlan Cox 	 * will be ignored by vm_object_page_remove().
955950f8459SAlan Cox 	 *
956950f8459SAlan Cox 	 * We cannot lock the vnode and then wait for paging
957950f8459SAlan Cox 	 * to complete without deadlocking against vm_fault.
958950f8459SAlan Cox 	 * Instead we simply call vm_object_page_remove() and
959950f8459SAlan Cox 	 * allow it to block internally on a page-by-page
960950f8459SAlan Cox 	 * basis when it encounters pages undergoing async
961950f8459SAlan Cox 	 * I/O.
962950f8459SAlan Cox 	 */
963950f8459SAlan Cox 	if (object->type == OBJT_VNODE &&
964950f8459SAlan Cox 	    (object->flags & OBJ_MIGHTBEDIRTY) != 0) {
965ae51ff11SJeff Roberson 		int vfslocked;
966950f8459SAlan Cox 		vp = object->handle;
967950f8459SAlan Cox 		VM_OBJECT_UNLOCK(object);
9683b582b4eSTor Egge 		(void) vn_start_write(vp, &mp, V_WAIT);
969ae51ff11SJeff Roberson 		vfslocked = VFS_LOCK_GIANT(vp->v_mount);
970cb05b60aSAttilio Rao 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
971950f8459SAlan Cox 		flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
972950f8459SAlan Cox 		flags |= invalidate ? OBJPC_INVAL : 0;
973950f8459SAlan Cox 		VM_OBJECT_LOCK(object);
974950f8459SAlan Cox 		vm_object_page_clean(object,
975950f8459SAlan Cox 		    OFF_TO_IDX(offset),
976950f8459SAlan Cox 		    OFF_TO_IDX(offset + size + PAGE_MASK),
977950f8459SAlan Cox 		    flags);
978950f8459SAlan Cox 		VM_OBJECT_UNLOCK(object);
97922db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
980ae51ff11SJeff Roberson 		VFS_UNLOCK_GIANT(vfslocked);
9813b582b4eSTor Egge 		vn_finished_write(mp);
982950f8459SAlan Cox 		VM_OBJECT_LOCK(object);
983950f8459SAlan Cox 	}
984950f8459SAlan Cox 	if ((object->type == OBJT_VNODE ||
985950f8459SAlan Cox 	     object->type == OBJT_DEVICE) && invalidate) {
986874f0135SDoug Rabson 		boolean_t purge;
987874f0135SDoug Rabson 		purge = old_msync || (object->type == OBJT_DEVICE);
988950f8459SAlan Cox 		vm_object_page_remove(object,
989950f8459SAlan Cox 		    OFF_TO_IDX(offset),
990950f8459SAlan Cox 		    OFF_TO_IDX(offset + size + PAGE_MASK),
991874f0135SDoug Rabson 		    purge ? FALSE : TRUE);
992950f8459SAlan Cox 	}
993950f8459SAlan Cox 	VM_OBJECT_UNLOCK(object);
994950f8459SAlan Cox }
995950f8459SAlan Cox 
996950f8459SAlan Cox /*
997867a482dSJohn Dyson  *	vm_object_madvise:
998867a482dSJohn Dyson  *
999867a482dSJohn Dyson  *	Implements the madvise function at the object/page level.
10001c7c3c6aSMatthew Dillon  *
1001193b9358SAlan Cox  *	MADV_WILLNEED	(any object)
1002193b9358SAlan Cox  *
1003193b9358SAlan Cox  *	    Activate the specified pages if they are resident.
1004193b9358SAlan Cox  *
1005193b9358SAlan Cox  *	MADV_DONTNEED	(any object)
1006193b9358SAlan Cox  *
1007193b9358SAlan Cox  *	    Deactivate the specified pages if they are resident.
1008193b9358SAlan Cox  *
1009193b9358SAlan Cox  *	MADV_FREE	(OBJT_DEFAULT/OBJT_SWAP objects,
1010193b9358SAlan Cox  *			 OBJ_ONEMAPPING only)
1011193b9358SAlan Cox  *
1012193b9358SAlan Cox  *	    Deactivate and clean the specified pages if they are
1013193b9358SAlan Cox  *	    resident.  This permits the process to reuse the pages
1014193b9358SAlan Cox  *	    without faulting or the kernel to reclaim the pages
1015193b9358SAlan Cox  *	    without I/O.
1016867a482dSJohn Dyson  */
1017867a482dSJohn Dyson void
10181b40f8c0SMatthew Dillon vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
1019867a482dSJohn Dyson {
10206e20a165SJohn Dyson 	vm_pindex_t end, tpindex;
102134567de7SAlan Cox 	vm_object_t backing_object, tobject;
1022867a482dSJohn Dyson 	vm_page_t m;
1023867a482dSJohn Dyson 
1024867a482dSJohn Dyson 	if (object == NULL)
1025867a482dSJohn Dyson 		return;
10269b98b796SAlan Cox 	VM_OBJECT_LOCK(object);
1027867a482dSJohn Dyson 	end = pindex + count;
10281c7c3c6aSMatthew Dillon 	/*
10291c7c3c6aSMatthew Dillon 	 * Locate and adjust resident pages
10301c7c3c6aSMatthew Dillon 	 */
10311c7c3c6aSMatthew Dillon 	for (; pindex < end; pindex += 1) {
10326e20a165SJohn Dyson relookup:
10336e20a165SJohn Dyson 		tobject = object;
10346e20a165SJohn Dyson 		tpindex = pindex;
10356e20a165SJohn Dyson shadowlookup:
103658b4e6ccSAlan Cox 		/*
103758b4e6ccSAlan Cox 		 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
103858b4e6ccSAlan Cox 		 * and those pages must be OBJ_ONEMAPPING.
103958b4e6ccSAlan Cox 		 */
104058b4e6ccSAlan Cox 		if (advise == MADV_FREE) {
104158b4e6ccSAlan Cox 			if ((tobject->type != OBJT_DEFAULT &&
104258b4e6ccSAlan Cox 			     tobject->type != OBJT_SWAP) ||
104358b4e6ccSAlan Cox 			    (tobject->flags & OBJ_ONEMAPPING) == 0) {
104434567de7SAlan Cox 				goto unlock_tobject;
10456e20a165SJohn Dyson 			}
10466a2a3d73SAlan Cox 		} else if (tobject->type == OBJT_PHYS)
10476a2a3d73SAlan Cox 			goto unlock_tobject;
10481c7c3c6aSMatthew Dillon 		m = vm_page_lookup(tobject, tpindex);
10497bfda801SAlan Cox 		if (m == NULL && advise == MADV_WILLNEED) {
10507bfda801SAlan Cox 			/*
10517bfda801SAlan Cox 			 * If the page is cached, reactivate it.
10527bfda801SAlan Cox 			 */
1053f3a2ed4bSAlan Cox 			m = vm_page_alloc(tobject, tpindex, VM_ALLOC_IFCACHED |
1054f3a2ed4bSAlan Cox 			    VM_ALLOC_NOBUSY);
10557bfda801SAlan Cox 		}
10561c7c3c6aSMatthew Dillon 		if (m == NULL) {
10571ce137beSMatthew Dillon 			/*
10581ce137beSMatthew Dillon 			 * There may be swap even if there is no backing page
10591ce137beSMatthew Dillon 			 */
10601ce137beSMatthew Dillon 			if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
10611ce137beSMatthew Dillon 				swap_pager_freespace(tobject, tpindex, 1);
10621ce137beSMatthew Dillon 			/*
10631ce137beSMatthew Dillon 			 * next object
10641ce137beSMatthew Dillon 			 */
106534567de7SAlan Cox 			backing_object = tobject->backing_object;
106634567de7SAlan Cox 			if (backing_object == NULL)
106734567de7SAlan Cox 				goto unlock_tobject;
106834567de7SAlan Cox 			VM_OBJECT_LOCK(backing_object);
106956e0670fSAlan Cox 			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
10709b98b796SAlan Cox 			if (tobject != object)
107134567de7SAlan Cox 				VM_OBJECT_UNLOCK(tobject);
107234567de7SAlan Cox 			tobject = backing_object;
10736e20a165SJohn Dyson 			goto shadowlookup;
10746a2a3d73SAlan Cox 		} else if (m->valid != VM_PAGE_BITS_ALL)
10756a2a3d73SAlan Cox 			goto unlock_tobject;
1076867a482dSJohn Dyson 		/*
10776a2a3d73SAlan Cox 		 * If the page is not in a normal state, skip it.
1078867a482dSJohn Dyson 		 */
10792965a453SKip Macy 		vm_page_lock(m);
10806a2a3d73SAlan Cox 		if (m->hold_count != 0 || m->wire_count != 0) {
10812965a453SKip Macy 			vm_page_unlock(m);
108234567de7SAlan Cox 			goto unlock_tobject;
10836e20a165SJohn Dyson 		}
1084567e51e1SAlan Cox 		KASSERT((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0,
1085567e51e1SAlan Cox 		    ("vm_object_madvise: page %p is not managed", m));
10869af80719SAlan Cox 		if ((m->oflags & VPO_BUSY) || m->busy) {
1087567e51e1SAlan Cox 			if (advise == MADV_WILLNEED) {
1088b11b56b5SAlan Cox 				/*
1089b11b56b5SAlan Cox 				 * Reference the page before unlocking and
1090b11b56b5SAlan Cox 				 * sleeping so that the page daemon is less
1091b11b56b5SAlan Cox 				 * likely to reclaim it.
1092b11b56b5SAlan Cox 				 */
1093567e51e1SAlan Cox 				vm_page_lock_queues();
10945786be7cSAlan Cox 				vm_page_flag_set(m, PG_REFERENCED);
109591449ce9SAlan Cox 				vm_page_unlock_queues();
1096567e51e1SAlan Cox 			}
10972965a453SKip Macy 			vm_page_unlock(m);
10989b98b796SAlan Cox 			if (object != tobject)
10999b98b796SAlan Cox 				VM_OBJECT_UNLOCK(object);
11005786be7cSAlan Cox 			m->oflags |= VPO_WANTED;
1101b11b56b5SAlan Cox 			msleep(m, VM_OBJECT_MTX(tobject), PDROP | PVM, "madvpo",
1102b11b56b5SAlan Cox 			    0);
11039b98b796SAlan Cox 			VM_OBJECT_LOCK(object);
11046e20a165SJohn Dyson   			goto relookup;
110534567de7SAlan Cox 		}
1106867a482dSJohn Dyson 		if (advise == MADV_WILLNEED) {
1107867a482dSJohn Dyson 			vm_page_activate(m);
11086e20a165SJohn Dyson 		} else if (advise == MADV_DONTNEED) {
1109479112dfSMatthew Dillon 			vm_page_dontneed(m);
11100a47b48bSJohn Dyson 		} else if (advise == MADV_FREE) {
11111c7c3c6aSMatthew Dillon 			/*
11122aaeadf8SMatthew Dillon 			 * Mark the page clean.  This will allow the page
11132aaeadf8SMatthew Dillon 			 * to be freed up by the system.  However, such pages
11142aaeadf8SMatthew Dillon 			 * are often reused quickly by malloc()/free()
11152aaeadf8SMatthew Dillon 			 * so we do not do anything that would cause
11162aaeadf8SMatthew Dillon 			 * a page fault if we can help it.
11172aaeadf8SMatthew Dillon 			 *
11182aaeadf8SMatthew Dillon 			 * Specifically, we do not try to actually free
11192aaeadf8SMatthew Dillon 			 * the page now nor do we try to put it in the
11202aaeadf8SMatthew Dillon 			 * cache (which would cause a page fault on reuse).
112141c67e12SMatthew Dillon 			 *
112241c67e12SMatthew Dillon 			 * But we do make the page is freeable as we
112341c67e12SMatthew Dillon 			 * can without actually taking the step of unmapping
112441c67e12SMatthew Dillon 			 * it.
11251c7c3c6aSMatthew Dillon 			 */
11260385347cSPeter Wemm 			pmap_clear_modify(m);
11276e20a165SJohn Dyson 			m->dirty = 0;
112841c67e12SMatthew Dillon 			m->act_count = 0;
1129479112dfSMatthew Dillon 			vm_page_dontneed(m);
1130867a482dSJohn Dyson 		}
11312965a453SKip Macy 		vm_page_unlock(m);
11322999e9faSAlan Cox 		if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
11332999e9faSAlan Cox 			swap_pager_freespace(tobject, tpindex, 1);
113434567de7SAlan Cox unlock_tobject:
11359b98b796SAlan Cox 		if (tobject != object)
113634567de7SAlan Cox 			VM_OBJECT_UNLOCK(tobject);
1137867a482dSJohn Dyson 	}
11389b98b796SAlan Cox 	VM_OBJECT_UNLOCK(object);
1139867a482dSJohn Dyson }
1140867a482dSJohn Dyson 
1141867a482dSJohn Dyson /*
1142df8bae1dSRodney W. Grimes  *	vm_object_shadow:
1143df8bae1dSRodney W. Grimes  *
1144df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
1145df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
1146df8bae1dSRodney W. Grimes  *	object reference is deallocated.
1147df8bae1dSRodney W. Grimes  *
1148df8bae1dSRodney W. Grimes  *	The new object and offset into that object
1149df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
1150df8bae1dSRodney W. Grimes  */
115126f9a767SRodney W. Grimes void
11521b40f8c0SMatthew Dillon vm_object_shadow(
11531b40f8c0SMatthew Dillon 	vm_object_t *object,	/* IN/OUT */
11541b40f8c0SMatthew Dillon 	vm_ooffset_t *offset,	/* IN/OUT */
11551b40f8c0SMatthew Dillon 	vm_size_t length)
1156df8bae1dSRodney W. Grimes {
1157d031cff1SMatthew Dillon 	vm_object_t source;
1158d031cff1SMatthew Dillon 	vm_object_t result;
1159df8bae1dSRodney W. Grimes 
1160df8bae1dSRodney W. Grimes 	source = *object;
1161df8bae1dSRodney W. Grimes 
1162df8bae1dSRodney W. Grimes 	/*
11639a2f6362SAlan Cox 	 * Don't create the new object if the old object isn't shared.
11649a2f6362SAlan Cox 	 */
1165570a2f4aSAlan Cox 	if (source != NULL) {
1166570a2f4aSAlan Cox 		VM_OBJECT_LOCK(source);
1167570a2f4aSAlan Cox 		if (source->ref_count == 1 &&
11689a2f6362SAlan Cox 		    source->handle == NULL &&
11699a2f6362SAlan Cox 		    (source->type == OBJT_DEFAULT ||
11709917e010SAlan Cox 		     source->type == OBJT_SWAP)) {
1171570a2f4aSAlan Cox 			VM_OBJECT_UNLOCK(source);
11729a2f6362SAlan Cox 			return;
11739917e010SAlan Cox 		}
1174570a2f4aSAlan Cox 		VM_OBJECT_UNLOCK(source);
1175570a2f4aSAlan Cox 	}
11769a2f6362SAlan Cox 
11779a2f6362SAlan Cox 	/*
1178570a2f4aSAlan Cox 	 * Allocate a new object with the given length.
1179df8bae1dSRodney W. Grimes 	 */
1180971dd342SAlfred Perlstein 	result = vm_object_allocate(OBJT_DEFAULT, length);
1181df8bae1dSRodney W. Grimes 
1182df8bae1dSRodney W. Grimes 	/*
11830d94caffSDavid Greenman 	 * The new object shadows the source object, adding a reference to it.
11840d94caffSDavid Greenman 	 * Our caller changes his reference to point to the new object,
11850d94caffSDavid Greenman 	 * removing a reference to the source object.  Net result: no change
11860d94caffSDavid Greenman 	 * of reference count.
11879b09fe24SMatthew Dillon 	 *
11889b09fe24SMatthew Dillon 	 * Try to optimize the result object's page color when shadowing
1189956f3135SPhilippe Charnier 	 * in order to maintain page coloring consistency in the combined
11909b09fe24SMatthew Dillon 	 * shadowed object.
1191df8bae1dSRodney W. Grimes 	 */
119224a1cce3SDavid Greenman 	result->backing_object = source;
11939174ca7bSTor Egge 	/*
11949174ca7bSTor Egge 	 * Store the offset into the source object, and fix up the offset into
11959174ca7bSTor Egge 	 * the new object.
11969174ca7bSTor Egge 	 */
11979174ca7bSTor Egge 	result->backing_object_offset = *offset;
1198570a2f4aSAlan Cox 	if (source != NULL) {
1199570a2f4aSAlan Cox 		VM_OBJECT_LOCK(source);
12001c500307SAlan Cox 		LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1201eaf13dd7SJohn Dyson 		source->shadow_count++;
1202eaf13dd7SJohn Dyson 		source->generation++;
1203f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
12047b54b1a9SAlan Cox 		result->flags |= source->flags & OBJ_COLORED;
1205f8a47341SAlan Cox 		result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) &
1206f8a47341SAlan Cox 		    ((1 << (VM_NFREEORDER - 1)) - 1);
1207f8a47341SAlan Cox #endif
1208570a2f4aSAlan Cox 		VM_OBJECT_UNLOCK(source);
1209de5f6a77SJohn Dyson 	}
1210df8bae1dSRodney W. Grimes 
1211df8bae1dSRodney W. Grimes 
1212df8bae1dSRodney W. Grimes 	/*
1213df8bae1dSRodney W. Grimes 	 * Return the new things
1214df8bae1dSRodney W. Grimes 	 */
1215df8bae1dSRodney W. Grimes 	*offset = 0;
1216df8bae1dSRodney W. Grimes 	*object = result;
1217df8bae1dSRodney W. Grimes }
1218df8bae1dSRodney W. Grimes 
1219c5aaa06dSAlan Cox /*
1220c5aaa06dSAlan Cox  *	vm_object_split:
1221c5aaa06dSAlan Cox  *
1222c5aaa06dSAlan Cox  * Split the pages in a map entry into a new object.  This affords
1223c5aaa06dSAlan Cox  * easier removal of unused pages, and keeps object inheritance from
1224c5aaa06dSAlan Cox  * being a negative impact on memory usage.
1225c5aaa06dSAlan Cox  */
1226c5aaa06dSAlan Cox void
1227c5aaa06dSAlan Cox vm_object_split(vm_map_entry_t entry)
1228c5aaa06dSAlan Cox {
122973000556SAlan Cox 	vm_page_t m, m_next;
1230c5aaa06dSAlan Cox 	vm_object_t orig_object, new_object, source;
123173000556SAlan Cox 	vm_pindex_t idx, offidxstart;
123273000556SAlan Cox 	vm_size_t size;
1233c5aaa06dSAlan Cox 
1234c5aaa06dSAlan Cox 	orig_object = entry->object.vm_object;
1235c5aaa06dSAlan Cox 	if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1236c5aaa06dSAlan Cox 		return;
1237c5aaa06dSAlan Cox 	if (orig_object->ref_count <= 1)
1238c5aaa06dSAlan Cox 		return;
12394da9f125SAlan Cox 	VM_OBJECT_UNLOCK(orig_object);
1240c5aaa06dSAlan Cox 
12414da9f125SAlan Cox 	offidxstart = OFF_TO_IDX(entry->offset);
124295442adfSAlan Cox 	size = atop(entry->end - entry->start);
1243c5aaa06dSAlan Cox 
12444da9f125SAlan Cox 	/*
12454da9f125SAlan Cox 	 * If swap_pager_copy() is later called, it will convert new_object
12464da9f125SAlan Cox 	 * into a swap object.
12474da9f125SAlan Cox 	 */
12484da9f125SAlan Cox 	new_object = vm_object_allocate(OBJT_DEFAULT, size);
1249c5aaa06dSAlan Cox 
1250c5474b8fSAlan Cox 	/*
1251c5474b8fSAlan Cox 	 * At this point, the new object is still private, so the order in
1252c5474b8fSAlan Cox 	 * which the original and new objects are locked does not matter.
1253c5474b8fSAlan Cox 	 */
125463f6cefcSAlan Cox 	VM_OBJECT_LOCK(new_object);
125563f6cefcSAlan Cox 	VM_OBJECT_LOCK(orig_object);
1256c5aaa06dSAlan Cox 	source = orig_object->backing_object;
1257c5aaa06dSAlan Cox 	if (source != NULL) {
12588e3a76fbSAlan Cox 		VM_OBJECT_LOCK(source);
125919c244d0SAlan Cox 		if ((source->flags & OBJ_DEAD) != 0) {
126019c244d0SAlan Cox 			VM_OBJECT_UNLOCK(source);
126119c244d0SAlan Cox 			VM_OBJECT_UNLOCK(orig_object);
126219c244d0SAlan Cox 			VM_OBJECT_UNLOCK(new_object);
126319c244d0SAlan Cox 			vm_object_deallocate(new_object);
126419c244d0SAlan Cox 			VM_OBJECT_LOCK(orig_object);
126519c244d0SAlan Cox 			return;
126619c244d0SAlan Cox 		}
12671c500307SAlan Cox 		LIST_INSERT_HEAD(&source->shadow_head,
1268c5aaa06dSAlan Cox 				  new_object, shadow_list);
12698e3a76fbSAlan Cox 		source->shadow_count++;
12708e3a76fbSAlan Cox 		source->generation++;
1271b921a12bSAlan Cox 		vm_object_reference_locked(source);	/* for new_object */
1272c5aaa06dSAlan Cox 		vm_object_clear_flag(source, OBJ_ONEMAPPING);
1273e2479b4fSAlan Cox 		VM_OBJECT_UNLOCK(source);
1274c5aaa06dSAlan Cox 		new_object->backing_object_offset =
12754da9f125SAlan Cox 			orig_object->backing_object_offset + entry->offset;
1276c5aaa06dSAlan Cox 		new_object->backing_object = source;
1277c5aaa06dSAlan Cox 	}
12783364c323SKonstantin Belousov 	if (orig_object->uip != NULL) {
12793364c323SKonstantin Belousov 		new_object->uip = orig_object->uip;
12803364c323SKonstantin Belousov 		uihold(orig_object->uip);
12813364c323SKonstantin Belousov 		new_object->charge = ptoa(size);
12823364c323SKonstantin Belousov 		KASSERT(orig_object->charge >= ptoa(size),
12833364c323SKonstantin Belousov 		    ("orig_object->charge < 0"));
12843364c323SKonstantin Belousov 		orig_object->charge -= ptoa(size);
12853364c323SKonstantin Belousov 	}
1286c5aaa06dSAlan Cox retry:
1287b382c10aSKonstantin Belousov 	m = vm_page_find_least(orig_object, offidxstart);
128873000556SAlan Cox 	for (; m != NULL && (idx = m->pindex - offidxstart) < size;
128973000556SAlan Cox 	    m = m_next) {
129073000556SAlan Cox 		m_next = TAILQ_NEXT(m, listq);
1291c5aaa06dSAlan Cox 
1292c5aaa06dSAlan Cox 		/*
1293c5aaa06dSAlan Cox 		 * We must wait for pending I/O to complete before we can
1294c5aaa06dSAlan Cox 		 * rename the page.
1295c5aaa06dSAlan Cox 		 *
1296c5aaa06dSAlan Cox 		 * We do not have to VM_PROT_NONE the page as mappings should
1297c5aaa06dSAlan Cox 		 * not be changed by this operation.
1298c5aaa06dSAlan Cox 		 */
12999af80719SAlan Cox 		if ((m->oflags & VPO_BUSY) || m->busy) {
1300de33beddSAlan Cox 			VM_OBJECT_UNLOCK(new_object);
13015786be7cSAlan Cox 			m->oflags |= VPO_WANTED;
1302c5474b8fSAlan Cox 			msleep(m, VM_OBJECT_MTX(orig_object), PVM, "spltwt", 0);
1303de33beddSAlan Cox 			VM_OBJECT_LOCK(new_object);
1304c5aaa06dSAlan Cox 			goto retry;
1305de33beddSAlan Cox 		}
13062965a453SKip Macy 		vm_page_lock(m);
1307c5aaa06dSAlan Cox 		vm_page_rename(m, new_object, idx);
13082965a453SKip Macy 		vm_page_unlock(m);
1309c5aaa06dSAlan Cox 		/* page automatically made dirty by rename and cache handled */
1310c5aaa06dSAlan Cox 		vm_page_busy(m);
1311c5aaa06dSAlan Cox 	}
1312d7a013c3SAlan Cox 	if (orig_object->type == OBJT_SWAP) {
1313c5aaa06dSAlan Cox 		/*
1314c7c8dd7eSAlan Cox 		 * swap_pager_copy() can sleep, in which case the orig_object's
1315c7c8dd7eSAlan Cox 		 * and new_object's locks are released and reacquired.
1316c5aaa06dSAlan Cox 		 */
1317c5aaa06dSAlan Cox 		swap_pager_copy(orig_object, new_object, offidxstart, 0);
13187bfda801SAlan Cox 
13197bfda801SAlan Cox 		/*
13207bfda801SAlan Cox 		 * Transfer any cached pages from orig_object to new_object.
13217bfda801SAlan Cox 		 */
13227bfda801SAlan Cox 		if (__predict_false(orig_object->cache != NULL))
13237bfda801SAlan Cox 			vm_page_cache_transfer(orig_object, offidxstart,
13247bfda801SAlan Cox 			    new_object);
1325c5aaa06dSAlan Cox 	}
1326d7a013c3SAlan Cox 	VM_OBJECT_UNLOCK(orig_object);
1327c7118ed6SAlan Cox 	TAILQ_FOREACH(m, &new_object->memq, listq)
1328c5aaa06dSAlan Cox 		vm_page_wakeup(m);
1329c7c8dd7eSAlan Cox 	VM_OBJECT_UNLOCK(new_object);
1330c5aaa06dSAlan Cox 	entry->object.vm_object = new_object;
1331c5aaa06dSAlan Cox 	entry->offset = 0LL;
1332c5aaa06dSAlan Cox 	vm_object_deallocate(orig_object);
13334da9f125SAlan Cox 	VM_OBJECT_LOCK(new_object);
1334c5aaa06dSAlan Cox }
1335c5aaa06dSAlan Cox 
13362ad1a3f7SMatthew Dillon #define	OBSC_TEST_ALL_SHADOWED	0x0001
13372ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_NOWAIT	0x0002
13382ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_WAIT	0x0004
13392ad1a3f7SMatthew Dillon 
1340b4ae4780SPoul-Henning Kamp static int
13412ad1a3f7SMatthew Dillon vm_object_backing_scan(vm_object_t object, int op)
13422ad1a3f7SMatthew Dillon {
13432ad1a3f7SMatthew Dillon 	int r = 1;
13442ad1a3f7SMatthew Dillon 	vm_page_t p;
13452ad1a3f7SMatthew Dillon 	vm_object_t backing_object;
13462ad1a3f7SMatthew Dillon 	vm_pindex_t backing_offset_index;
13472ad1a3f7SMatthew Dillon 
13487ca33ad1SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
13497ca33ad1SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object->backing_object, MA_OWNED);
13502ad1a3f7SMatthew Dillon 
13512ad1a3f7SMatthew Dillon 	backing_object = object->backing_object;
13522ad1a3f7SMatthew Dillon 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
13532ad1a3f7SMatthew Dillon 
13542ad1a3f7SMatthew Dillon 	/*
13552ad1a3f7SMatthew Dillon 	 * Initial conditions
13562ad1a3f7SMatthew Dillon 	 */
13572ad1a3f7SMatthew Dillon 	if (op & OBSC_TEST_ALL_SHADOWED) {
13582ad1a3f7SMatthew Dillon 		/*
13597bfda801SAlan Cox 		 * We do not want to have to test for the existence of cache
13607bfda801SAlan Cox 		 * or swap pages in the backing object.  XXX but with the
13612ad1a3f7SMatthew Dillon 		 * new swapper this would be pretty easy to do.
13622ad1a3f7SMatthew Dillon 		 *
13632ad1a3f7SMatthew Dillon 		 * XXX what about anonymous MAP_SHARED memory that hasn't
13642ad1a3f7SMatthew Dillon 		 * been ZFOD faulted yet?  If we do not test for this, the
13652ad1a3f7SMatthew Dillon 		 * shadow test may succeed! XXX
13662ad1a3f7SMatthew Dillon 		 */
13672ad1a3f7SMatthew Dillon 		if (backing_object->type != OBJT_DEFAULT) {
13682ad1a3f7SMatthew Dillon 			return (0);
13692ad1a3f7SMatthew Dillon 		}
13702ad1a3f7SMatthew Dillon 	}
13712ad1a3f7SMatthew Dillon 	if (op & OBSC_COLLAPSE_WAIT) {
13722ad1a3f7SMatthew Dillon 		vm_object_set_flag(backing_object, OBJ_DEAD);
13732ad1a3f7SMatthew Dillon 	}
13742ad1a3f7SMatthew Dillon 
13752ad1a3f7SMatthew Dillon 	/*
13762ad1a3f7SMatthew Dillon 	 * Our scan
13772ad1a3f7SMatthew Dillon 	 */
13782ad1a3f7SMatthew Dillon 	p = TAILQ_FIRST(&backing_object->memq);
13792ad1a3f7SMatthew Dillon 	while (p) {
13802ad1a3f7SMatthew Dillon 		vm_page_t next = TAILQ_NEXT(p, listq);
13812ad1a3f7SMatthew Dillon 		vm_pindex_t new_pindex = p->pindex - backing_offset_index;
13822ad1a3f7SMatthew Dillon 
13832ad1a3f7SMatthew Dillon 		if (op & OBSC_TEST_ALL_SHADOWED) {
13842ad1a3f7SMatthew Dillon 			vm_page_t pp;
13852ad1a3f7SMatthew Dillon 
13862ad1a3f7SMatthew Dillon 			/*
13872ad1a3f7SMatthew Dillon 			 * Ignore pages outside the parent object's range
13882ad1a3f7SMatthew Dillon 			 * and outside the parent object's mapping of the
13892ad1a3f7SMatthew Dillon 			 * backing object.
13902ad1a3f7SMatthew Dillon 			 *
13912ad1a3f7SMatthew Dillon 			 * note that we do not busy the backing object's
13922ad1a3f7SMatthew Dillon 			 * page.
13932ad1a3f7SMatthew Dillon 			 */
13942ad1a3f7SMatthew Dillon 			if (
13952ad1a3f7SMatthew Dillon 			    p->pindex < backing_offset_index ||
13962ad1a3f7SMatthew Dillon 			    new_pindex >= object->size
13972ad1a3f7SMatthew Dillon 			) {
13982ad1a3f7SMatthew Dillon 				p = next;
13992ad1a3f7SMatthew Dillon 				continue;
14002ad1a3f7SMatthew Dillon 			}
14012ad1a3f7SMatthew Dillon 
14022ad1a3f7SMatthew Dillon 			/*
14032ad1a3f7SMatthew Dillon 			 * See if the parent has the page or if the parent's
14042ad1a3f7SMatthew Dillon 			 * object pager has the page.  If the parent has the
14052ad1a3f7SMatthew Dillon 			 * page but the page is not valid, the parent's
14062ad1a3f7SMatthew Dillon 			 * object pager must have the page.
14072ad1a3f7SMatthew Dillon 			 *
14082ad1a3f7SMatthew Dillon 			 * If this fails, the parent does not completely shadow
14092ad1a3f7SMatthew Dillon 			 * the object and we might as well give up now.
14102ad1a3f7SMatthew Dillon 			 */
14112ad1a3f7SMatthew Dillon 
14122ad1a3f7SMatthew Dillon 			pp = vm_page_lookup(object, new_pindex);
14132ad1a3f7SMatthew Dillon 			if (
14142ad1a3f7SMatthew Dillon 			    (pp == NULL || pp->valid == 0) &&
14152ad1a3f7SMatthew Dillon 			    !vm_pager_has_page(object, new_pindex, NULL, NULL)
14162ad1a3f7SMatthew Dillon 			) {
14172ad1a3f7SMatthew Dillon 				r = 0;
14182ad1a3f7SMatthew Dillon 				break;
14192ad1a3f7SMatthew Dillon 			}
14202ad1a3f7SMatthew Dillon 		}
14212ad1a3f7SMatthew Dillon 
14222ad1a3f7SMatthew Dillon 		/*
14232ad1a3f7SMatthew Dillon 		 * Check for busy page
14242ad1a3f7SMatthew Dillon 		 */
14252ad1a3f7SMatthew Dillon 		if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
14262ad1a3f7SMatthew Dillon 			vm_page_t pp;
14272ad1a3f7SMatthew Dillon 
14282ad1a3f7SMatthew Dillon 			if (op & OBSC_COLLAPSE_NOWAIT) {
14299af80719SAlan Cox 				if ((p->oflags & VPO_BUSY) ||
14302ad1a3f7SMatthew Dillon 				    !p->valid ||
143100f9e8b4SAlan Cox 				    p->busy) {
14322ad1a3f7SMatthew Dillon 					p = next;
14332ad1a3f7SMatthew Dillon 					continue;
14342ad1a3f7SMatthew Dillon 				}
14352ad1a3f7SMatthew Dillon 			} else if (op & OBSC_COLLAPSE_WAIT) {
14369af80719SAlan Cox 				if ((p->oflags & VPO_BUSY) || p->busy) {
14377ca33ad1SAlan Cox 					VM_OBJECT_UNLOCK(object);
14385786be7cSAlan Cox 					p->oflags |= VPO_WANTED;
143991449ce9SAlan Cox 					msleep(p, VM_OBJECT_MTX(backing_object),
14407ca33ad1SAlan Cox 					    PDROP | PVM, "vmocol", 0);
14417ca33ad1SAlan Cox 					VM_OBJECT_LOCK(object);
14427ca33ad1SAlan Cox 					VM_OBJECT_LOCK(backing_object);
14432ad1a3f7SMatthew Dillon 					/*
14442ad1a3f7SMatthew Dillon 					 * If we slept, anything could have
14452ad1a3f7SMatthew Dillon 					 * happened.  Since the object is
14462ad1a3f7SMatthew Dillon 					 * marked dead, the backing offset
14472ad1a3f7SMatthew Dillon 					 * should not have changed so we
14482ad1a3f7SMatthew Dillon 					 * just restart our scan.
14492ad1a3f7SMatthew Dillon 					 */
14502ad1a3f7SMatthew Dillon 					p = TAILQ_FIRST(&backing_object->memq);
14512ad1a3f7SMatthew Dillon 					continue;
14522ad1a3f7SMatthew Dillon 				}
14532ad1a3f7SMatthew Dillon 			}
14542ad1a3f7SMatthew Dillon 
14552ad1a3f7SMatthew Dillon 			KASSERT(
14562ad1a3f7SMatthew Dillon 			    p->object == backing_object,
14578e99783bSAlan Cox 			    ("vm_object_backing_scan: object mismatch")
14582ad1a3f7SMatthew Dillon 			);
14592ad1a3f7SMatthew Dillon 
14602ad1a3f7SMatthew Dillon 			/*
14612ad1a3f7SMatthew Dillon 			 * Destroy any associated swap
14622ad1a3f7SMatthew Dillon 			 */
14632ad1a3f7SMatthew Dillon 			if (backing_object->type == OBJT_SWAP) {
14642ad1a3f7SMatthew Dillon 				swap_pager_freespace(
14652ad1a3f7SMatthew Dillon 				    backing_object,
14662ad1a3f7SMatthew Dillon 				    p->pindex,
14672ad1a3f7SMatthew Dillon 				    1
14682ad1a3f7SMatthew Dillon 				);
14692ad1a3f7SMatthew Dillon 			}
14702ad1a3f7SMatthew Dillon 
14712ad1a3f7SMatthew Dillon 			if (
14722ad1a3f7SMatthew Dillon 			    p->pindex < backing_offset_index ||
14732ad1a3f7SMatthew Dillon 			    new_pindex >= object->size
14742ad1a3f7SMatthew Dillon 			) {
14752ad1a3f7SMatthew Dillon 				/*
14762ad1a3f7SMatthew Dillon 				 * Page is out of the parent object's range, we
14772ad1a3f7SMatthew Dillon 				 * can simply destroy it.
14782ad1a3f7SMatthew Dillon 				 */
14792965a453SKip Macy 				vm_page_lock(p);
1480f6d89838SAlan Cox 				KASSERT(!pmap_page_is_mapped(p),
1481f6d89838SAlan Cox 				    ("freeing mapped page %p", p));
1482f6d89838SAlan Cox 				if (p->wire_count == 0)
14832ad1a3f7SMatthew Dillon 					vm_page_free(p);
1484f6d89838SAlan Cox 				else
1485f6d89838SAlan Cox 					vm_page_remove(p);
14862965a453SKip Macy 				vm_page_unlock(p);
14872ad1a3f7SMatthew Dillon 				p = next;
14882ad1a3f7SMatthew Dillon 				continue;
14892ad1a3f7SMatthew Dillon 			}
14902ad1a3f7SMatthew Dillon 
14912ad1a3f7SMatthew Dillon 			pp = vm_page_lookup(object, new_pindex);
14922ad1a3f7SMatthew Dillon 			if (
14932ad1a3f7SMatthew Dillon 			    pp != NULL ||
14942ad1a3f7SMatthew Dillon 			    vm_pager_has_page(object, new_pindex, NULL, NULL)
14952ad1a3f7SMatthew Dillon 			) {
14962ad1a3f7SMatthew Dillon 				/*
14972ad1a3f7SMatthew Dillon 				 * page already exists in parent OR swap exists
14982ad1a3f7SMatthew Dillon 				 * for this location in the parent.  Destroy
14992ad1a3f7SMatthew Dillon 				 * the original page from the backing object.
15002ad1a3f7SMatthew Dillon 				 *
15012ad1a3f7SMatthew Dillon 				 * Leave the parent's page alone
15022ad1a3f7SMatthew Dillon 				 */
15032965a453SKip Macy 				vm_page_lock(p);
1504f6d89838SAlan Cox 				KASSERT(!pmap_page_is_mapped(p),
1505f6d89838SAlan Cox 				    ("freeing mapped page %p", p));
1506f6d89838SAlan Cox 				if (p->wire_count == 0)
15072ad1a3f7SMatthew Dillon 					vm_page_free(p);
1508f6d89838SAlan Cox 				else
1509f6d89838SAlan Cox 					vm_page_remove(p);
15102965a453SKip Macy 				vm_page_unlock(p);
15112ad1a3f7SMatthew Dillon 				p = next;
15122ad1a3f7SMatthew Dillon 				continue;
15132ad1a3f7SMatthew Dillon 			}
15142ad1a3f7SMatthew Dillon 
1515f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
1516f8a47341SAlan Cox 			/*
1517f8a47341SAlan Cox 			 * Rename the reservation.
1518f8a47341SAlan Cox 			 */
1519f8a47341SAlan Cox 			vm_reserv_rename(p, object, backing_object,
1520f8a47341SAlan Cox 			    backing_offset_index);
1521f8a47341SAlan Cox #endif
1522f8a47341SAlan Cox 
15232ad1a3f7SMatthew Dillon 			/*
15242ad1a3f7SMatthew Dillon 			 * Page does not exist in parent, rename the
15252ad1a3f7SMatthew Dillon 			 * page from the backing object to the main object.
1526d1bf5d56SMatthew Dillon 			 *
1527d1bf5d56SMatthew Dillon 			 * If the page was mapped to a process, it can remain
1528d1bf5d56SMatthew Dillon 			 * mapped through the rename.
15292ad1a3f7SMatthew Dillon 			 */
15302965a453SKip Macy 			vm_page_lock(p);
15312ad1a3f7SMatthew Dillon 			vm_page_rename(p, object, new_pindex);
15322965a453SKip Macy 			vm_page_unlock(p);
15332ad1a3f7SMatthew Dillon 			/* page automatically made dirty by rename */
15342ad1a3f7SMatthew Dillon 		}
15352ad1a3f7SMatthew Dillon 		p = next;
15362ad1a3f7SMatthew Dillon 	}
15372ad1a3f7SMatthew Dillon 	return (r);
15382ad1a3f7SMatthew Dillon }
15392ad1a3f7SMatthew Dillon 
1540df8bae1dSRodney W. Grimes 
1541df8bae1dSRodney W. Grimes /*
15422fe6e4d7SDavid Greenman  * this version of collapse allows the operation to occur earlier and
15432fe6e4d7SDavid Greenman  * when paging_in_progress is true for an object...  This is not a complete
15442fe6e4d7SDavid Greenman  * operation, but should plug 99.9% of the rest of the leaks.
15452fe6e4d7SDavid Greenman  */
15462fe6e4d7SDavid Greenman static void
15471b40f8c0SMatthew Dillon vm_object_qcollapse(vm_object_t object)
15482fe6e4d7SDavid Greenman {
15492ad1a3f7SMatthew Dillon 	vm_object_t backing_object = object->backing_object;
15502fe6e4d7SDavid Greenman 
155106ecade7SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
155206ecade7SAlan Cox 	VM_OBJECT_LOCK_ASSERT(backing_object, MA_OWNED);
15531b40f8c0SMatthew Dillon 
15542fe6e4d7SDavid Greenman 	if (backing_object->ref_count != 1)
15552fe6e4d7SDavid Greenman 		return;
15562fe6e4d7SDavid Greenman 
15572ad1a3f7SMatthew Dillon 	vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
15582fe6e4d7SDavid Greenman }
15592fe6e4d7SDavid Greenman 
1560df8bae1dSRodney W. Grimes /*
1561df8bae1dSRodney W. Grimes  *	vm_object_collapse:
1562df8bae1dSRodney W. Grimes  *
1563df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
1564df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
1565df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
1566df8bae1dSRodney W. Grimes  */
156726f9a767SRodney W. Grimes void
15681b40f8c0SMatthew Dillon vm_object_collapse(vm_object_t object)
1569df8bae1dSRodney W. Grimes {
1570d7fc2210SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
157123955314SAlfred Perlstein 
1572df8bae1dSRodney W. Grimes 	while (TRUE) {
15732ad1a3f7SMatthew Dillon 		vm_object_t backing_object;
15742ad1a3f7SMatthew Dillon 
1575df8bae1dSRodney W. Grimes 		/*
1576df8bae1dSRodney W. Grimes 		 * Verify that the conditions are right for collapse:
1577df8bae1dSRodney W. Grimes 		 *
15782ad1a3f7SMatthew Dillon 		 * The object exists and the backing object exists.
1579df8bae1dSRodney W. Grimes 		 */
158024a1cce3SDavid Greenman 		if ((backing_object = object->backing_object) == NULL)
15812ad1a3f7SMatthew Dillon 			break;
1582df8bae1dSRodney W. Grimes 
1583f919ebdeSDavid Greenman 		/*
1584f919ebdeSDavid Greenman 		 * we check the backing object first, because it is most likely
158524a1cce3SDavid Greenman 		 * not collapsable.
1586f919ebdeSDavid Greenman 		 */
158740b808a8SAlan Cox 		VM_OBJECT_LOCK(backing_object);
158824a1cce3SDavid Greenman 		if (backing_object->handle != NULL ||
158924a1cce3SDavid Greenman 		    (backing_object->type != OBJT_DEFAULT &&
159024a1cce3SDavid Greenman 		     backing_object->type != OBJT_SWAP) ||
1591f919ebdeSDavid Greenman 		    (backing_object->flags & OBJ_DEAD) ||
159224a1cce3SDavid Greenman 		    object->handle != NULL ||
159324a1cce3SDavid Greenman 		    (object->type != OBJT_DEFAULT &&
159424a1cce3SDavid Greenman 		     object->type != OBJT_SWAP) ||
159524a1cce3SDavid Greenman 		    (object->flags & OBJ_DEAD)) {
159640b808a8SAlan Cox 			VM_OBJECT_UNLOCK(backing_object);
15972ad1a3f7SMatthew Dillon 			break;
159824a1cce3SDavid Greenman 		}
15999b4814bbSDavid Greenman 
16002ad1a3f7SMatthew Dillon 		if (
16012ad1a3f7SMatthew Dillon 		    object->paging_in_progress != 0 ||
16022ad1a3f7SMatthew Dillon 		    backing_object->paging_in_progress != 0
16032ad1a3f7SMatthew Dillon 		) {
1604b9921222SDavid Greenman 			vm_object_qcollapse(object);
160540b808a8SAlan Cox 			VM_OBJECT_UNLOCK(backing_object);
16062ad1a3f7SMatthew Dillon 			break;
1607df8bae1dSRodney W. Grimes 		}
160826f9a767SRodney W. Grimes 		/*
16090d94caffSDavid Greenman 		 * We know that we can either collapse the backing object (if
16102ad1a3f7SMatthew Dillon 		 * the parent is the only reference to it) or (perhaps) have
16112ad1a3f7SMatthew Dillon 		 * the parent bypass the object if the parent happens to shadow
16122ad1a3f7SMatthew Dillon 		 * all the resident pages in the entire backing object.
16132ad1a3f7SMatthew Dillon 		 *
16142ad1a3f7SMatthew Dillon 		 * This is ignoring pager-backed pages such as swap pages.
16152ad1a3f7SMatthew Dillon 		 * vm_object_backing_scan fails the shadowing test in this
16162ad1a3f7SMatthew Dillon 		 * case.
1617df8bae1dSRodney W. Grimes 		 */
1618df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
1619df8bae1dSRodney W. Grimes 			/*
16202ad1a3f7SMatthew Dillon 			 * If there is exactly one reference to the backing
16212ad1a3f7SMatthew Dillon 			 * object, we can collapse it into the parent.
1622df8bae1dSRodney W. Grimes 			 */
16232ad1a3f7SMatthew Dillon 			vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1624df8bae1dSRodney W. Grimes 
1625f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
1626f8a47341SAlan Cox 			/*
1627f8a47341SAlan Cox 			 * Break any reservations from backing_object.
1628f8a47341SAlan Cox 			 */
1629f8a47341SAlan Cox 			if (__predict_false(!LIST_EMPTY(&backing_object->rvq)))
1630f8a47341SAlan Cox 				vm_reserv_break_all(backing_object);
1631f8a47341SAlan Cox #endif
1632f8a47341SAlan Cox 
1633df8bae1dSRodney W. Grimes 			/*
1634df8bae1dSRodney W. Grimes 			 * Move the pager from backing_object to object.
1635df8bae1dSRodney W. Grimes 			 */
16366be36525SAlan Cox 			if (backing_object->type == OBJT_SWAP) {
163724a1cce3SDavid Greenman 				/*
1638c7c8dd7eSAlan Cox 				 * swap_pager_copy() can sleep, in which case
1639c7c8dd7eSAlan Cox 				 * the backing_object's and object's locks are
1640c7c8dd7eSAlan Cox 				 * released and reacquired.
164124a1cce3SDavid Greenman 				 */
16421c7c3c6aSMatthew Dillon 				swap_pager_copy(
16431c7c3c6aSMatthew Dillon 				    backing_object,
16441c7c3c6aSMatthew Dillon 				    object,
16451c7c3c6aSMatthew Dillon 				    OFF_TO_IDX(object->backing_object_offset), TRUE);
16467bfda801SAlan Cox 
16477bfda801SAlan Cox 				/*
16487bfda801SAlan Cox 				 * Free any cached pages from backing_object.
16497bfda801SAlan Cox 				 */
16507bfda801SAlan Cox 				if (__predict_false(backing_object->cache != NULL))
1651c9444914SAlan Cox 					vm_page_cache_free(backing_object, 0, 0);
1652c0503609SDavid Greenman 			}
1653df8bae1dSRodney W. Grimes 			/*
1654df8bae1dSRodney W. Grimes 			 * Object now shadows whatever backing_object did.
16552ad1a3f7SMatthew Dillon 			 * Note that the reference to
16562ad1a3f7SMatthew Dillon 			 * backing_object->backing_object moves from within
16572ad1a3f7SMatthew Dillon 			 * backing_object to within object.
1658df8bae1dSRodney W. Grimes 			 */
16591c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
16604f7c7f6eSAlan Cox 			backing_object->shadow_count--;
16614f7c7f6eSAlan Cox 			backing_object->generation++;
1662de5f6a77SJohn Dyson 			if (backing_object->backing_object) {
16636be36525SAlan Cox 				VM_OBJECT_LOCK(backing_object->backing_object);
16641c500307SAlan Cox 				LIST_REMOVE(backing_object, shadow_list);
166543186e53SAlan Cox 				LIST_INSERT_HEAD(
166643186e53SAlan Cox 				    &backing_object->backing_object->shadow_head,
166743186e53SAlan Cox 				    object, shadow_list);
166843186e53SAlan Cox 				/*
166943186e53SAlan Cox 				 * The shadow_count has not changed.
167043186e53SAlan Cox 				 */
1671eaf13dd7SJohn Dyson 				backing_object->backing_object->generation++;
16726be36525SAlan Cox 				VM_OBJECT_UNLOCK(backing_object->backing_object);
1673de5f6a77SJohn Dyson 			}
167424a1cce3SDavid Greenman 			object->backing_object = backing_object->backing_object;
16752ad1a3f7SMatthew Dillon 			object->backing_object_offset +=
16762ad1a3f7SMatthew Dillon 			    backing_object->backing_object_offset;
16772ad1a3f7SMatthew Dillon 
1678df8bae1dSRodney W. Grimes 			/*
1679df8bae1dSRodney W. Grimes 			 * Discard backing_object.
1680df8bae1dSRodney W. Grimes 			 *
16810d94caffSDavid Greenman 			 * Since the backing object has no pages, no pager left,
16820d94caffSDavid Greenman 			 * and no object references within it, all that is
16830d94caffSDavid Greenman 			 * necessary is to dispose of it.
1684df8bae1dSRodney W. Grimes 			 */
16859b4d473aSKonstantin Belousov 			KASSERT(backing_object->ref_count == 1, (
16869b4d473aSKonstantin Belousov "backing_object %p was somehow re-referenced during collapse!",
16879b4d473aSKonstantin Belousov 			    backing_object));
16886be36525SAlan Cox 			VM_OBJECT_UNLOCK(backing_object);
16899b4d473aSKonstantin Belousov 			vm_object_destroy(backing_object);
1690df8bae1dSRodney W. Grimes 
1691df8bae1dSRodney W. Grimes 			object_collapses++;
16920d94caffSDavid Greenman 		} else {
169395e5e988SJohn Dyson 			vm_object_t new_backing_object;
1694df8bae1dSRodney W. Grimes 
1695df8bae1dSRodney W. Grimes 			/*
16962ad1a3f7SMatthew Dillon 			 * If we do not entirely shadow the backing object,
16972ad1a3f7SMatthew Dillon 			 * there is nothing we can do so we give up.
1698df8bae1dSRodney W. Grimes 			 */
1699df59a0feSJeff Roberson 			if (object->resident_page_count != object->size &&
1700df59a0feSJeff Roberson 			    vm_object_backing_scan(object,
1701df59a0feSJeff Roberson 			    OBSC_TEST_ALL_SHADOWED) == 0) {
170240b808a8SAlan Cox 				VM_OBJECT_UNLOCK(backing_object);
17032ad1a3f7SMatthew Dillon 				break;
170424a1cce3SDavid Greenman 			}
1705df8bae1dSRodney W. Grimes 
1706df8bae1dSRodney W. Grimes 			/*
17070d94caffSDavid Greenman 			 * Make the parent shadow the next object in the
17080d94caffSDavid Greenman 			 * chain.  Deallocating backing_object will not remove
17090d94caffSDavid Greenman 			 * it, since its reference count is at least 2.
1710df8bae1dSRodney W. Grimes 			 */
17111c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
1712eaf13dd7SJohn Dyson 			backing_object->shadow_count--;
1713eaf13dd7SJohn Dyson 			backing_object->generation++;
171495e5e988SJohn Dyson 
171595e5e988SJohn Dyson 			new_backing_object = backing_object->backing_object;
17168aef1712SMatthew Dillon 			if ((object->backing_object = new_backing_object) != NULL) {
17176be36525SAlan Cox 				VM_OBJECT_LOCK(new_backing_object);
17181c500307SAlan Cox 				LIST_INSERT_HEAD(
17192ad1a3f7SMatthew Dillon 				    &new_backing_object->shadow_head,
17202ad1a3f7SMatthew Dillon 				    object,
17212ad1a3f7SMatthew Dillon 				    shadow_list
17222ad1a3f7SMatthew Dillon 				);
1723eaf13dd7SJohn Dyson 				new_backing_object->shadow_count++;
1724eaf13dd7SJohn Dyson 				new_backing_object->generation++;
1725b921a12bSAlan Cox 				vm_object_reference_locked(new_backing_object);
17266be36525SAlan Cox 				VM_OBJECT_UNLOCK(new_backing_object);
172795e5e988SJohn Dyson 				object->backing_object_offset +=
172895e5e988SJohn Dyson 					backing_object->backing_object_offset;
1729de5f6a77SJohn Dyson 			}
1730df8bae1dSRodney W. Grimes 
1731df8bae1dSRodney W. Grimes 			/*
17320d94caffSDavid Greenman 			 * Drop the reference count on backing_object. Since
173322ec553fSAlan Cox 			 * its ref_count was at least 2, it will not vanish.
1734df8bae1dSRodney W. Grimes 			 */
173522ec553fSAlan Cox 			backing_object->ref_count--;
173622ec553fSAlan Cox 			VM_OBJECT_UNLOCK(backing_object);
1737df8bae1dSRodney W. Grimes 			object_bypasses++;
1738df8bae1dSRodney W. Grimes 		}
1739df8bae1dSRodney W. Grimes 
1740df8bae1dSRodney W. Grimes 		/*
1741df8bae1dSRodney W. Grimes 		 * Try again with this object's new backing object.
1742df8bae1dSRodney W. Grimes 		 */
1743df8bae1dSRodney W. Grimes 	}
1744df8bae1dSRodney W. Grimes }
1745df8bae1dSRodney W. Grimes 
1746df8bae1dSRodney W. Grimes /*
1747bff99f0dSAlan Cox  *	vm_object_page_remove:
1748df8bae1dSRodney W. Grimes  *
174968855966SAlan Cox  *	For the given object, either frees or invalidates each of the
175068855966SAlan Cox  *	specified pages.  In general, a page is freed.  However, if a
175168855966SAlan Cox  *	page is wired for any reason other than the existence of a
175268855966SAlan Cox  *	managed, wired mapping, then it may be invalidated but not
175368855966SAlan Cox  *	removed from the object.  Pages are specified by the given
175468855966SAlan Cox  *	range ["start", "end") and Boolean "clean_only".  As a
175568855966SAlan Cox  *	special case, if "end" is zero, then the range extends from
175668855966SAlan Cox  *	"start" to the end of the object.  If "clean_only" is TRUE,
175768855966SAlan Cox  *	then only the non-dirty pages within the specified range are
175868855966SAlan Cox  *	affected.
175968855966SAlan Cox  *
176068855966SAlan Cox  *	In general, this operation should only be performed on objects
176168855966SAlan Cox  *	that contain managed pages.  There are two exceptions.  First,
176268855966SAlan Cox  *	it may be performed on the kernel and kmem objects.  Second,
176368855966SAlan Cox  *	it may be used by msync(..., MS_INVALIDATE) to invalidate
1764a1a95cd6SAlan Cox  *	device-backed pages.  In both of these cases, "clean_only"
1765a1a95cd6SAlan Cox  *	must be FALSE.
1766df8bae1dSRodney W. Grimes  *
1767df8bae1dSRodney W. Grimes  *	The object must be locked.
1768df8bae1dSRodney W. Grimes  */
176926f9a767SRodney W. Grimes void
1770ecde4b32SAlan Cox vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
1771ecde4b32SAlan Cox     boolean_t clean_only)
1772df8bae1dSRodney W. Grimes {
1773d031cff1SMatthew Dillon 	vm_page_t p, next;
177459677d3cSAlan Cox 	int wirings;
1775df8bae1dSRodney W. Grimes 
1776ecde4b32SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1777ecde4b32SAlan Cox 	if (object->resident_page_count == 0)
177825732691SAlan Cox 		goto skipmemq;
177995e5e988SJohn Dyson 
17808b03c8edSMatthew Dillon 	/*
17818b03c8edSMatthew Dillon 	 * Since physically-backed objects do not use managed pages, we can't
17828b03c8edSMatthew Dillon 	 * remove pages from the object (we must instead remove the page
17838b03c8edSMatthew Dillon 	 * references, and then destroy the object).
17848b03c8edSMatthew Dillon 	 */
17859f5c801bSAlan Cox 	KASSERT(object->type != OBJT_PHYS || object == kernel_object ||
17869f5c801bSAlan Cox 	    object == kmem_object,
1787ecde4b32SAlan Cox 	    ("attempt to remove pages from a physical object"));
17888b03c8edSMatthew Dillon 
1789d474eaaaSDoug Rabson 	vm_object_pip_add(object, 1);
179026f9a767SRodney W. Grimes again:
1791b382c10aSKonstantin Belousov 	p = vm_page_find_least(object, start);
17922965a453SKip Macy 
179375741c04SAlan Cox 	/*
179475741c04SAlan Cox 	 * Assert: the variable p is either (1) the page with the
179575741c04SAlan Cox 	 * least pindex greater than or equal to the parameter pindex
179675741c04SAlan Cox 	 * or (2) NULL.
179775741c04SAlan Cox 	 */
179875741c04SAlan Cox 	for (;
1799bff99f0dSAlan Cox 	     p != NULL && (p->pindex < end || end == 0);
180075741c04SAlan Cox 	     p = next) {
1801b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
180275741c04SAlan Cox 
180359677d3cSAlan Cox 		/*
180459677d3cSAlan Cox 		 * If the page is wired for any reason besides the
180559677d3cSAlan Cox 		 * existence of managed, wired mappings, then it cannot
180668855966SAlan Cox 		 * be freed.  For example, fictitious pages, which
180768855966SAlan Cox 		 * represent device memory, are inherently wired and
180868855966SAlan Cox 		 * cannot be freed.  They can, however, be invalidated
180968855966SAlan Cox 		 * if "clean_only" is FALSE.
181059677d3cSAlan Cox 		 */
18112965a453SKip Macy 		vm_page_lock(p);
181259677d3cSAlan Cox 		if ((wirings = p->wire_count) != 0 &&
181359677d3cSAlan Cox 		    (wirings = pmap_page_wired_mappings(p)) != p->wire_count) {
181468855966SAlan Cox 			/* Fictitious pages do not have managed mappings. */
181568855966SAlan Cox 			if ((p->flags & PG_FICTITIOUS) == 0)
18164fec79beSAlan Cox 				pmap_remove_all(p);
181759677d3cSAlan Cox 			/* Account for removal of managed, wired mappings. */
181859677d3cSAlan Cox 			p->wire_count -= wirings;
1819a28042d1SAlan Cox 			if (!clean_only) {
1820bd7e5f99SJohn Dyson 				p->valid = 0;
1821a28042d1SAlan Cox 				vm_page_undirty(p);
1822a28042d1SAlan Cox 			}
18232965a453SKip Macy 			vm_page_unlock(p);
18240d94caffSDavid Greenman 			continue;
18250d94caffSDavid Greenman 		}
182632585dd6SAlan Cox 		if (vm_page_sleep_if_busy(p, TRUE, "vmopar"))
182726f9a767SRodney W. Grimes 			goto again;
182868855966SAlan Cox 		KASSERT((p->flags & PG_FICTITIOUS) == 0,
182968855966SAlan Cox 		    ("vm_object_page_remove: page %p is fictitious", p));
18308f9110f6SJohn Dyson 		if (clean_only && p->valid) {
183178985e42SAlan Cox 			pmap_remove_write(p);
18322965a453SKip Macy 			if (p->dirty) {
18332965a453SKip Macy 				vm_page_unlock(p);
18347c1f6cedSDavid Greenman 				continue;
18357c1f6cedSDavid Greenman 			}
18362965a453SKip Macy 		}
18374fec79beSAlan Cox 		pmap_remove_all(p);
183859677d3cSAlan Cox 		/* Account for removal of managed, wired mappings. */
183959677d3cSAlan Cox 		if (wirings != 0)
184059677d3cSAlan Cox 			p->wire_count -= wirings;
1841df8bae1dSRodney W. Grimes 		vm_page_free(p);
18422965a453SKip Macy 		vm_page_unlock(p);
18432965a453SKip Macy 	}
1844f919ebdeSDavid Greenman 	vm_object_pip_wakeup(object);
184525732691SAlan Cox skipmemq:
1846c9444914SAlan Cox 	if (__predict_false(object->cache != NULL))
1847c9444914SAlan Cox 		vm_page_cache_free(object, start, end);
1848c0503609SDavid Greenman }
1849df8bae1dSRodney W. Grimes 
1850df8bae1dSRodney W. Grimes /*
1851387aabc5SAlan Cox  *	Populate the specified range of the object with valid pages.  Returns
1852387aabc5SAlan Cox  *	TRUE if the range is successfully populated and FALSE otherwise.
1853387aabc5SAlan Cox  *
1854387aabc5SAlan Cox  *	Note: This function should be optimized to pass a larger array of
1855387aabc5SAlan Cox  *	pages to vm_pager_get_pages() before it is applied to a non-
1856387aabc5SAlan Cox  *	OBJT_DEVICE object.
1857387aabc5SAlan Cox  *
1858387aabc5SAlan Cox  *	The object must be locked.
1859387aabc5SAlan Cox  */
1860387aabc5SAlan Cox boolean_t
1861387aabc5SAlan Cox vm_object_populate(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1862387aabc5SAlan Cox {
1863387aabc5SAlan Cox 	vm_page_t m, ma[1];
1864387aabc5SAlan Cox 	vm_pindex_t pindex;
1865387aabc5SAlan Cox 	int rv;
1866387aabc5SAlan Cox 
1867387aabc5SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1868387aabc5SAlan Cox 	for (pindex = start; pindex < end; pindex++) {
1869387aabc5SAlan Cox 		m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL |
1870387aabc5SAlan Cox 		    VM_ALLOC_RETRY);
1871387aabc5SAlan Cox 		if (m->valid != VM_PAGE_BITS_ALL) {
1872387aabc5SAlan Cox 			ma[0] = m;
1873387aabc5SAlan Cox 			rv = vm_pager_get_pages(object, ma, 1, 0);
1874387aabc5SAlan Cox 			m = vm_page_lookup(object, pindex);
1875387aabc5SAlan Cox 			if (m == NULL)
1876387aabc5SAlan Cox 				break;
1877387aabc5SAlan Cox 			if (rv != VM_PAGER_OK) {
18782965a453SKip Macy 				vm_page_lock(m);
1879387aabc5SAlan Cox 				vm_page_free(m);
18802965a453SKip Macy 				vm_page_unlock(m);
1881387aabc5SAlan Cox 				break;
1882387aabc5SAlan Cox 			}
1883387aabc5SAlan Cox 		}
1884387aabc5SAlan Cox 		/*
1885387aabc5SAlan Cox 		 * Keep "m" busy because a subsequent iteration may unlock
1886387aabc5SAlan Cox 		 * the object.
1887387aabc5SAlan Cox 		 */
1888387aabc5SAlan Cox 	}
1889387aabc5SAlan Cox 	if (pindex > start) {
1890387aabc5SAlan Cox 		m = vm_page_lookup(object, start);
1891387aabc5SAlan Cox 		while (m != NULL && m->pindex < pindex) {
1892387aabc5SAlan Cox 			vm_page_wakeup(m);
1893387aabc5SAlan Cox 			m = TAILQ_NEXT(m, listq);
1894387aabc5SAlan Cox 		}
1895387aabc5SAlan Cox 	}
1896387aabc5SAlan Cox 	return (pindex == end);
1897387aabc5SAlan Cox }
1898387aabc5SAlan Cox 
1899387aabc5SAlan Cox /*
1900df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
1901df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
1902df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
1903df8bae1dSRodney W. Grimes  *
1904df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
1905df8bae1dSRodney W. Grimes  *
1906df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
1907df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
1908df8bae1dSRodney W. Grimes  *
1909df8bae1dSRodney W. Grimes  *	Parameters:
1910df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
1911df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
1912df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
191357a21abaSAlan Cox  *		next_size	Size of reference to the second object
19143364c323SKonstantin Belousov  *		reserved	Indicator that extension region has
19153364c323SKonstantin Belousov  *				swap accounted for
1916df8bae1dSRodney W. Grimes  *
1917df8bae1dSRodney W. Grimes  *	Conditions:
1918df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
1919df8bae1dSRodney W. Grimes  */
19200d94caffSDavid Greenman boolean_t
192157a21abaSAlan Cox vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset,
19223364c323SKonstantin Belousov     vm_size_t prev_size, vm_size_t next_size, boolean_t reserved)
1923df8bae1dSRodney W. Grimes {
1924ea41812fSAlan Cox 	vm_pindex_t next_pindex;
1925df8bae1dSRodney W. Grimes 
192600e1854aSAlan Cox 	if (prev_object == NULL)
1927df8bae1dSRodney W. Grimes 		return (TRUE);
1928bdbfbaafSAlan Cox 	VM_OBJECT_LOCK(prev_object);
19294112823fSMatthew Dillon 	if (prev_object->type != OBJT_DEFAULT &&
19304112823fSMatthew Dillon 	    prev_object->type != OBJT_SWAP) {
1931bdbfbaafSAlan Cox 		VM_OBJECT_UNLOCK(prev_object);
193230dcfc09SJohn Dyson 		return (FALSE);
193330dcfc09SJohn Dyson 	}
193430dcfc09SJohn Dyson 
1935df8bae1dSRodney W. Grimes 	/*
1936df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
1937df8bae1dSRodney W. Grimes 	 */
1938df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
1939df8bae1dSRodney W. Grimes 
1940df8bae1dSRodney W. Grimes 	/*
19410d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
19420d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
19430d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
1944df8bae1dSRodney W. Grimes 	 */
19458cc7e047SJohn Dyson 	if (prev_object->backing_object != NULL) {
1946bdbfbaafSAlan Cox 		VM_OBJECT_UNLOCK(prev_object);
1947df8bae1dSRodney W. Grimes 		return (FALSE);
1948df8bae1dSRodney W. Grimes 	}
1949a316d390SJohn Dyson 
1950a316d390SJohn Dyson 	prev_size >>= PAGE_SHIFT;
1951a316d390SJohn Dyson 	next_size >>= PAGE_SHIFT;
195257a21abaSAlan Cox 	next_pindex = OFF_TO_IDX(prev_offset) + prev_size;
19538cc7e047SJohn Dyson 
19548cc7e047SJohn Dyson 	if ((prev_object->ref_count > 1) &&
1955ea41812fSAlan Cox 	    (prev_object->size != next_pindex)) {
1956bdbfbaafSAlan Cox 		VM_OBJECT_UNLOCK(prev_object);
19578cc7e047SJohn Dyson 		return (FALSE);
19588cc7e047SJohn Dyson 	}
19598cc7e047SJohn Dyson 
1960df8bae1dSRodney W. Grimes 	/*
19613364c323SKonstantin Belousov 	 * Account for the charge.
19623364c323SKonstantin Belousov 	 */
19633364c323SKonstantin Belousov 	if (prev_object->uip != NULL) {
19643364c323SKonstantin Belousov 
19653364c323SKonstantin Belousov 		/*
19663364c323SKonstantin Belousov 		 * If prev_object was charged, then this mapping,
19673364c323SKonstantin Belousov 		 * althought not charged now, may become writable
19683364c323SKonstantin Belousov 		 * later. Non-NULL uip in the object would prevent
19693364c323SKonstantin Belousov 		 * swap reservation during enabling of the write
19703364c323SKonstantin Belousov 		 * access, so reserve swap now. Failed reservation
19713364c323SKonstantin Belousov 		 * cause allocation of the separate object for the map
19723364c323SKonstantin Belousov 		 * entry, and swap reservation for this entry is
19733364c323SKonstantin Belousov 		 * managed in appropriate time.
19743364c323SKonstantin Belousov 		 */
19753364c323SKonstantin Belousov 		if (!reserved && !swap_reserve_by_uid(ptoa(next_size),
19763364c323SKonstantin Belousov 		    prev_object->uip)) {
19773364c323SKonstantin Belousov 			return (FALSE);
19783364c323SKonstantin Belousov 		}
19793364c323SKonstantin Belousov 		prev_object->charge += ptoa(next_size);
19803364c323SKonstantin Belousov 	}
19813364c323SKonstantin Belousov 
19823364c323SKonstantin Belousov 	/*
19830d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
19840d94caffSDavid Greenman 	 * deallocation.
1985df8bae1dSRodney W. Grimes 	 */
1986ea41812fSAlan Cox 	if (next_pindex < prev_object->size) {
1987df8bae1dSRodney W. Grimes 		vm_object_page_remove(prev_object,
1988ea41812fSAlan Cox 				      next_pindex,
1989ea41812fSAlan Cox 				      next_pindex + next_size, FALSE);
1990ea41812fSAlan Cox 		if (prev_object->type == OBJT_SWAP)
1991ea41812fSAlan Cox 			swap_pager_freespace(prev_object,
1992ea41812fSAlan Cox 					     next_pindex, next_size);
19933364c323SKonstantin Belousov #if 0
19943364c323SKonstantin Belousov 		if (prev_object->uip != NULL) {
19953364c323SKonstantin Belousov 			KASSERT(prev_object->charge >=
19963364c323SKonstantin Belousov 			    ptoa(prev_object->size - next_pindex),
19973364c323SKonstantin Belousov 			    ("object %p overcharged 1 %jx %jx", prev_object,
19983364c323SKonstantin Belousov 				(uintmax_t)next_pindex, (uintmax_t)next_size));
19993364c323SKonstantin Belousov 			prev_object->charge -= ptoa(prev_object->size -
20003364c323SKonstantin Belousov 			    next_pindex);
20013364c323SKonstantin Belousov 		}
20023364c323SKonstantin Belousov #endif
2003ea41812fSAlan Cox 	}
2004df8bae1dSRodney W. Grimes 
2005df8bae1dSRodney W. Grimes 	/*
2006df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
2007df8bae1dSRodney W. Grimes 	 */
2008ea41812fSAlan Cox 	if (next_pindex + next_size > prev_object->size)
2009ea41812fSAlan Cox 		prev_object->size = next_pindex + next_size;
2010df8bae1dSRodney W. Grimes 
2011bdbfbaafSAlan Cox 	VM_OBJECT_UNLOCK(prev_object);
2012df8bae1dSRodney W. Grimes 	return (TRUE);
2013df8bae1dSRodney W. Grimes }
2014df8bae1dSRodney W. Grimes 
20157a5a6352SMatthew Dillon void
20167a5a6352SMatthew Dillon vm_object_set_writeable_dirty(vm_object_t object)
20177a5a6352SMatthew Dillon {
20187a5a6352SMatthew Dillon 
2019de33beddSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
202049e3050eSKonstantin Belousov 	if (object->type != OBJT_VNODE ||
202149e3050eSKonstantin Belousov 	    (object->flags & OBJ_MIGHTBEDIRTY) != 0)
2022ee39666aSJeff Roberson 		return;
2023af51d7bfSAlan Cox 	vm_object_set_flag(object, OBJ_MIGHTBEDIRTY);
20247a5a6352SMatthew Dillon }
20257a5a6352SMatthew Dillon 
2026c7c34a24SBruce Evans #include "opt_ddb.h"
2027c3cb3e12SDavid Greenman #ifdef DDB
2028c7c34a24SBruce Evans #include <sys/kernel.h>
2029c7c34a24SBruce Evans 
2030ce9edcf5SPoul-Henning Kamp #include <sys/cons.h>
2031c7c34a24SBruce Evans 
2032c7c34a24SBruce Evans #include <ddb/ddb.h>
2033c7c34a24SBruce Evans 
2034cac597e4SBruce Evans static int
20351b40f8c0SMatthew Dillon _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
2036a1f6d91cSDavid Greenman {
2037a1f6d91cSDavid Greenman 	vm_map_t tmpm;
2038a1f6d91cSDavid Greenman 	vm_map_entry_t tmpe;
2039a1f6d91cSDavid Greenman 	vm_object_t obj;
2040a1f6d91cSDavid Greenman 	int entcount;
2041a1f6d91cSDavid Greenman 
2042a1f6d91cSDavid Greenman 	if (map == 0)
2043a1f6d91cSDavid Greenman 		return 0;
2044a1f6d91cSDavid Greenman 
2045a1f6d91cSDavid Greenman 	if (entry == 0) {
2046a1f6d91cSDavid Greenman 		tmpe = map->header.next;
2047a1f6d91cSDavid Greenman 		entcount = map->nentries;
2048a1f6d91cSDavid Greenman 		while (entcount-- && (tmpe != &map->header)) {
2049a1f6d91cSDavid Greenman 			if (_vm_object_in_map(map, object, tmpe)) {
2050a1f6d91cSDavid Greenman 				return 1;
2051a1f6d91cSDavid Greenman 			}
2052a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
2053a1f6d91cSDavid Greenman 		}
20549fdfe602SMatthew Dillon 	} else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
20559fdfe602SMatthew Dillon 		tmpm = entry->object.sub_map;
2056a1f6d91cSDavid Greenman 		tmpe = tmpm->header.next;
2057a1f6d91cSDavid Greenman 		entcount = tmpm->nentries;
2058a1f6d91cSDavid Greenman 		while (entcount-- && tmpe != &tmpm->header) {
2059a1f6d91cSDavid Greenman 			if (_vm_object_in_map(tmpm, object, tmpe)) {
2060a1f6d91cSDavid Greenman 				return 1;
2061a1f6d91cSDavid Greenman 			}
2062a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
2063a1f6d91cSDavid Greenman 		}
20648aef1712SMatthew Dillon 	} else if ((obj = entry->object.vm_object) != NULL) {
206524a1cce3SDavid Greenman 		for (; obj; obj = obj->backing_object)
2066a1f6d91cSDavid Greenman 			if (obj == object) {
2067a1f6d91cSDavid Greenman 				return 1;
2068a1f6d91cSDavid Greenman 			}
2069a1f6d91cSDavid Greenman 	}
2070a1f6d91cSDavid Greenman 	return 0;
2071a1f6d91cSDavid Greenman }
2072a1f6d91cSDavid Greenman 
2073cac597e4SBruce Evans static int
20741b40f8c0SMatthew Dillon vm_object_in_map(vm_object_t object)
2075a1f6d91cSDavid Greenman {
2076a1f6d91cSDavid Greenman 	struct proc *p;
20771005a129SJohn Baldwin 
207860517fd1SJohn Baldwin 	/* sx_slock(&allproc_lock); */
2079f67af5c9SXin LI 	FOREACH_PROC_IN_SYSTEM(p) {
2080a1f6d91cSDavid Greenman 		if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
2081a1f6d91cSDavid Greenman 			continue;
2082553629ebSJake Burkholder 		if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
208360517fd1SJohn Baldwin 			/* sx_sunlock(&allproc_lock); */
2084a1f6d91cSDavid Greenman 			return 1;
2085a1f6d91cSDavid Greenman 		}
2086553629ebSJake Burkholder 	}
208760517fd1SJohn Baldwin 	/* sx_sunlock(&allproc_lock); */
2088a1f6d91cSDavid Greenman 	if (_vm_object_in_map(kernel_map, object, 0))
2089a1f6d91cSDavid Greenman 		return 1;
2090a1f6d91cSDavid Greenman 	if (_vm_object_in_map(kmem_map, object, 0))
2091a1f6d91cSDavid Greenman 		return 1;
2092a1f6d91cSDavid Greenman 	if (_vm_object_in_map(pager_map, object, 0))
2093a1f6d91cSDavid Greenman 		return 1;
2094a1f6d91cSDavid Greenman 	if (_vm_object_in_map(buffer_map, object, 0))
2095a1f6d91cSDavid Greenman 		return 1;
2096a1f6d91cSDavid Greenman 	return 0;
2097a1f6d91cSDavid Greenman }
2098a1f6d91cSDavid Greenman 
2099c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check)
2100f708ef1bSPoul-Henning Kamp {
2101a1f6d91cSDavid Greenman 	vm_object_t object;
2102a1f6d91cSDavid Greenman 
2103a1f6d91cSDavid Greenman 	/*
2104a1f6d91cSDavid Greenman 	 * make sure that internal objs are in a map somewhere
2105a1f6d91cSDavid Greenman 	 * and none have zero ref counts.
2106a1f6d91cSDavid Greenman 	 */
2107cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
210824a1cce3SDavid Greenman 		if (object->handle == NULL &&
210924a1cce3SDavid Greenman 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2110a1f6d91cSDavid Greenman 			if (object->ref_count == 0) {
21113efc015bSPeter Wemm 				db_printf("vmochk: internal obj has zero ref count: %ld\n",
21123efc015bSPeter Wemm 					(long)object->size);
2113a1f6d91cSDavid Greenman 			}
2114a1f6d91cSDavid Greenman 			if (!vm_object_in_map(object)) {
2115fc62ef1fSBruce Evans 				db_printf(
2116fc62ef1fSBruce Evans 			"vmochk: internal obj is not in a map: "
2117fc62ef1fSBruce Evans 			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2118fc62ef1fSBruce Evans 				    object->ref_count, (u_long)object->size,
2119fc62ef1fSBruce Evans 				    (u_long)object->size,
2120fc62ef1fSBruce Evans 				    (void *)object->backing_object);
2121a1f6d91cSDavid Greenman 			}
2122a1f6d91cSDavid Greenman 		}
2123a1f6d91cSDavid Greenman 	}
2124a1f6d91cSDavid Greenman }
2125a1f6d91cSDavid Greenman 
212626f9a767SRodney W. Grimes /*
2127df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
2128df8bae1dSRodney W. Grimes  */
2129c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static)
2130df8bae1dSRodney W. Grimes {
2131c7c34a24SBruce Evans 	/* XXX convert args. */
2132c7c34a24SBruce Evans 	vm_object_t object = (vm_object_t)addr;
2133c7c34a24SBruce Evans 	boolean_t full = have_addr;
2134c7c34a24SBruce Evans 
2135d031cff1SMatthew Dillon 	vm_page_t p;
2136df8bae1dSRodney W. Grimes 
2137c7c34a24SBruce Evans 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
2138c7c34a24SBruce Evans #define	count	was_count
2139c7c34a24SBruce Evans 
2140d031cff1SMatthew Dillon 	int count;
2141df8bae1dSRodney W. Grimes 
2142df8bae1dSRodney W. Grimes 	if (object == NULL)
2143df8bae1dSRodney W. Grimes 		return;
2144df8bae1dSRodney W. Grimes 
2145eb95adefSBruce Evans 	db_iprintf(
21463364c323SKonstantin Belousov 	    "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x uip %d charge %jx\n",
2147e47cd172SMaxime Henrion 	    object, (int)object->type, (uintmax_t)object->size,
21483364c323SKonstantin Belousov 	    object->resident_page_count, object->ref_count, object->flags,
21493364c323SKonstantin Belousov 	    object->uip ? object->uip->ui_uid : -1, (uintmax_t)object->charge);
2150e47cd172SMaxime Henrion 	db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n",
21511c7c3c6aSMatthew Dillon 	    object->shadow_count,
2152eb95adefSBruce Evans 	    object->backing_object ? object->backing_object->ref_count : 0,
2153e47cd172SMaxime Henrion 	    object->backing_object, (uintmax_t)object->backing_object_offset);
2154df8bae1dSRodney W. Grimes 
2155df8bae1dSRodney W. Grimes 	if (!full)
2156df8bae1dSRodney W. Grimes 		return;
2157df8bae1dSRodney W. Grimes 
2158c7c34a24SBruce Evans 	db_indent += 2;
2159df8bae1dSRodney W. Grimes 	count = 0;
2160fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(p, &object->memq, listq) {
2161df8bae1dSRodney W. Grimes 		if (count == 0)
2162c7c34a24SBruce Evans 			db_iprintf("memory:=");
2163df8bae1dSRodney W. Grimes 		else if (count == 6) {
2164c7c34a24SBruce Evans 			db_printf("\n");
2165c7c34a24SBruce Evans 			db_iprintf(" ...");
2166df8bae1dSRodney W. Grimes 			count = 0;
2167df8bae1dSRodney W. Grimes 		} else
2168c7c34a24SBruce Evans 			db_printf(",");
2169df8bae1dSRodney W. Grimes 		count++;
2170df8bae1dSRodney W. Grimes 
2171e47cd172SMaxime Henrion 		db_printf("(off=0x%jx,page=0x%jx)",
2172e47cd172SMaxime Henrion 		    (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p));
2173df8bae1dSRodney W. Grimes 	}
2174df8bae1dSRodney W. Grimes 	if (count != 0)
2175c7c34a24SBruce Evans 		db_printf("\n");
2176c7c34a24SBruce Evans 	db_indent -= 2;
2177df8bae1dSRodney W. Grimes }
21785070c7f8SJohn Dyson 
2179c7c34a24SBruce Evans /* XXX. */
2180c7c34a24SBruce Evans #undef count
2181c7c34a24SBruce Evans 
2182c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */
21835070c7f8SJohn Dyson void
21841b40f8c0SMatthew Dillon vm_object_print(
21851b40f8c0SMatthew Dillon         /* db_expr_t */ long addr,
21861b40f8c0SMatthew Dillon 	boolean_t have_addr,
21871b40f8c0SMatthew Dillon 	/* db_expr_t */ long count,
21881b40f8c0SMatthew Dillon 	char *modif)
2189c7c34a24SBruce Evans {
2190c7c34a24SBruce Evans 	vm_object_print_static(addr, have_addr, count, modif);
2191c7c34a24SBruce Evans }
2192c7c34a24SBruce Evans 
2193c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
21945070c7f8SJohn Dyson {
21955070c7f8SJohn Dyson 	vm_object_t object;
2196bb2ac86fSKonstantin Belousov 	vm_pindex_t fidx;
2197bb2ac86fSKonstantin Belousov 	vm_paddr_t pa;
2198bb2ac86fSKonstantin Belousov 	vm_page_t m, prev_m;
2199bb2ac86fSKonstantin Belousov 	int rcount, nl, c;
2200cc64b484SAlfred Perlstein 
2201bb2ac86fSKonstantin Belousov 	nl = 0;
2202cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
2203fc62ef1fSBruce Evans 		db_printf("new object: %p\n", (void *)object);
22045070c7f8SJohn Dyson 		if (nl > 18) {
22055070c7f8SJohn Dyson 			c = cngetc();
22065070c7f8SJohn Dyson 			if (c != ' ')
22075070c7f8SJohn Dyson 				return;
22085070c7f8SJohn Dyson 			nl = 0;
22095070c7f8SJohn Dyson 		}
22105070c7f8SJohn Dyson 		nl++;
22115070c7f8SJohn Dyson 		rcount = 0;
22125070c7f8SJohn Dyson 		fidx = 0;
2213bb2ac86fSKonstantin Belousov 		pa = -1;
2214bb2ac86fSKonstantin Belousov 		TAILQ_FOREACH(m, &object->memq, listq) {
2215bb2ac86fSKonstantin Belousov 			if (m->pindex > 128)
2216bb2ac86fSKonstantin Belousov 				break;
2217bb2ac86fSKonstantin Belousov 			if ((prev_m = TAILQ_PREV(m, pglist, listq)) != NULL &&
2218bb2ac86fSKonstantin Belousov 			    prev_m->pindex + 1 != m->pindex) {
22195070c7f8SJohn Dyson 				if (rcount) {
22203efc015bSPeter Wemm 					db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
22213efc015bSPeter Wemm 						(long)fidx, rcount, (long)pa);
22225070c7f8SJohn Dyson 					if (nl > 18) {
22235070c7f8SJohn Dyson 						c = cngetc();
22245070c7f8SJohn Dyson 						if (c != ' ')
22255070c7f8SJohn Dyson 							return;
22265070c7f8SJohn Dyson 						nl = 0;
22275070c7f8SJohn Dyson 					}
22285070c7f8SJohn Dyson 					nl++;
22295070c7f8SJohn Dyson 					rcount = 0;
22305070c7f8SJohn Dyson 				}
22315070c7f8SJohn Dyson 			}
22325070c7f8SJohn Dyson 			if (rcount &&
22335070c7f8SJohn Dyson 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
22345070c7f8SJohn Dyson 				++rcount;
22355070c7f8SJohn Dyson 				continue;
22365070c7f8SJohn Dyson 			}
22375070c7f8SJohn Dyson 			if (rcount) {
22382446e4f0SAlan Cox 				db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
22393efc015bSPeter Wemm 					(long)fidx, rcount, (long)pa);
22405070c7f8SJohn Dyson 				if (nl > 18) {
22415070c7f8SJohn Dyson 					c = cngetc();
22425070c7f8SJohn Dyson 					if (c != ' ')
22435070c7f8SJohn Dyson 						return;
22445070c7f8SJohn Dyson 					nl = 0;
22455070c7f8SJohn Dyson 				}
22465070c7f8SJohn Dyson 				nl++;
22475070c7f8SJohn Dyson 			}
2248bb2ac86fSKonstantin Belousov 			fidx = m->pindex;
22495070c7f8SJohn Dyson 			pa = VM_PAGE_TO_PHYS(m);
22505070c7f8SJohn Dyson 			rcount = 1;
22515070c7f8SJohn Dyson 		}
22525070c7f8SJohn Dyson 		if (rcount) {
22533efc015bSPeter Wemm 			db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
22543efc015bSPeter Wemm 				(long)fidx, rcount, (long)pa);
22555070c7f8SJohn Dyson 			if (nl > 18) {
22565070c7f8SJohn Dyson 				c = cngetc();
22575070c7f8SJohn Dyson 				if (c != ' ')
22585070c7f8SJohn Dyson 					return;
22595070c7f8SJohn Dyson 				nl = 0;
22605070c7f8SJohn Dyson 			}
22615070c7f8SJohn Dyson 			nl++;
22625070c7f8SJohn Dyson 		}
22635070c7f8SJohn Dyson 	}
22645070c7f8SJohn Dyson }
2265c3cb3e12SDavid Greenman #endif /* DDB */
2266