xref: /freebsd/sys/vm/vm_object.c (revision 17f3095d1a2ebf8f3eaf76fd51fc6235e56d2cfb)
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 
99c53f7aceSDag-Erling Smørgrav static int old_msync;
100c53f7aceSDag-Erling Smørgrav SYSCTL_INT(_vm, OID_AUTO, old_msync, CTLFLAG_RW, &old_msync, 0,
101c53f7aceSDag-Erling Smørgrav     "Use old (insecure) msync behavior");
102c53f7aceSDag-Erling Smørgrav 
103757216f3SKonstantin Belousov static int	vm_object_page_collect_flush(vm_object_t object, vm_page_t p,
1043280870dSKonstantin Belousov 		    int pagerflags, int flags, int *clearobjflags);
1053280870dSKonstantin Belousov static boolean_t vm_object_page_remove_write(vm_page_t p, int flags,
1063280870dSKonstantin Belousov 		    int *clearobjflags);
107b9b7a4beSMatthew Dillon static void	vm_object_qcollapse(vm_object_t object);
10802dd8331SAlan Cox static void	vm_object_vndeallocate(vm_object_t object);
109f6b04d2bSDavid Greenman 
110df8bae1dSRodney W. Grimes /*
111df8bae1dSRodney W. Grimes  *	Virtual memory objects maintain the actual data
112df8bae1dSRodney W. Grimes  *	associated with allocated virtual memory.  A given
113df8bae1dSRodney W. Grimes  *	page of memory exists within exactly one object.
114df8bae1dSRodney W. Grimes  *
115df8bae1dSRodney W. Grimes  *	An object is only deallocated when all "references"
116df8bae1dSRodney W. Grimes  *	are given up.  Only one "reference" to a given
117df8bae1dSRodney W. Grimes  *	region of an object should be writeable.
118df8bae1dSRodney W. Grimes  *
119df8bae1dSRodney W. Grimes  *	Associated with each object is a list of all resident
120df8bae1dSRodney W. Grimes  *	memory pages belonging to that object; this list is
121df8bae1dSRodney W. Grimes  *	maintained by the "vm_page" module, and locked by the object's
122df8bae1dSRodney W. Grimes  *	lock.
123df8bae1dSRodney W. Grimes  *
124df8bae1dSRodney W. Grimes  *	Each object also records a "pager" routine which is
125df8bae1dSRodney W. Grimes  *	used to retrieve (and store) pages to the proper backing
126df8bae1dSRodney W. Grimes  *	storage.  In addition, objects may be backed by other
127df8bae1dSRodney W. Grimes  *	objects from which they were virtual-copied.
128df8bae1dSRodney W. Grimes  *
129df8bae1dSRodney W. Grimes  *	The only items within the object structure which are
130df8bae1dSRodney W. Grimes  *	modified after time of creation are:
131df8bae1dSRodney W. Grimes  *		reference count		locked by object's lock
132df8bae1dSRodney W. Grimes  *		pager routine		locked by object's lock
133df8bae1dSRodney W. Grimes  *
134df8bae1dSRodney W. Grimes  */
135df8bae1dSRodney W. Grimes 
13628f8db14SBruce Evans struct object_q vm_object_list;
137a5698387SAlan Cox struct mtx vm_object_list_mtx;	/* lock for object list and count */
138cccf11b8SAlan Cox 
139cccf11b8SAlan Cox struct vm_object kernel_object_store;
140cccf11b8SAlan Cox struct vm_object kmem_object_store;
141df8bae1dSRodney W. Grimes 
142604c2bbcSAlan Cox SYSCTL_NODE(_vm_stats, OID_AUTO, object, CTLFLAG_RD, 0, "VM object stats");
143604c2bbcSAlan Cox 
144f708ef1bSPoul-Henning Kamp static long object_collapses;
145604c2bbcSAlan Cox SYSCTL_LONG(_vm_stats_object, OID_AUTO, collapses, CTLFLAG_RD,
146604c2bbcSAlan Cox     &object_collapses, 0, "VM object collapses");
147604c2bbcSAlan Cox 
148f708ef1bSPoul-Henning Kamp static long object_bypasses;
149604c2bbcSAlan Cox SYSCTL_LONG(_vm_stats_object, OID_AUTO, bypasses, CTLFLAG_RD,
150604c2bbcSAlan Cox     &object_bypasses, 0, "VM object bypasses");
151dad740e9SAlan Cox 
152670d17b5SJeff Roberson static uma_zone_t obj_zone;
1538355f576SJeff Roberson 
154b23f72e9SBrian Feldman static int vm_object_zinit(void *mem, int size, int flags);
1558355f576SJeff Roberson 
1568355f576SJeff Roberson #ifdef INVARIANTS
1578355f576SJeff Roberson static void vm_object_zdtor(void *mem, int size, void *arg);
1588355f576SJeff Roberson 
1598355f576SJeff Roberson static void
1608355f576SJeff Roberson vm_object_zdtor(void *mem, int size, void *arg)
1618355f576SJeff Roberson {
1628355f576SJeff Roberson 	vm_object_t object;
1638355f576SJeff Roberson 
1648355f576SJeff Roberson 	object = (vm_object_t)mem;
16543186e53SAlan Cox 	KASSERT(TAILQ_EMPTY(&object->memq),
16643186e53SAlan Cox 	    ("object %p has resident pages",
16743186e53SAlan Cox 	    object));
168f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
169f8a47341SAlan Cox 	KASSERT(LIST_EMPTY(&object->rvq),
170f8a47341SAlan Cox 	    ("object %p has reservations",
171f8a47341SAlan Cox 	    object));
172f8a47341SAlan Cox #endif
1737bfda801SAlan Cox 	KASSERT(object->cache == NULL,
1747bfda801SAlan Cox 	    ("object %p has cached pages",
1757bfda801SAlan Cox 	    object));
1768355f576SJeff Roberson 	KASSERT(object->paging_in_progress == 0,
1778355f576SJeff Roberson 	    ("object %p paging_in_progress = %d",
1788355f576SJeff Roberson 	    object, object->paging_in_progress));
1798355f576SJeff Roberson 	KASSERT(object->resident_page_count == 0,
1808355f576SJeff Roberson 	    ("object %p resident_page_count = %d",
1818355f576SJeff Roberson 	    object, object->resident_page_count));
1828355f576SJeff Roberson 	KASSERT(object->shadow_count == 0,
1838355f576SJeff Roberson 	    ("object %p shadow_count = %d",
1848355f576SJeff Roberson 	    object, object->shadow_count));
1858355f576SJeff Roberson }
1868355f576SJeff Roberson #endif
1878355f576SJeff Roberson 
188b23f72e9SBrian Feldman static int
189b23f72e9SBrian Feldman vm_object_zinit(void *mem, int size, int flags)
1908355f576SJeff Roberson {
1918355f576SJeff Roberson 	vm_object_t object;
1928355f576SJeff Roberson 
1938355f576SJeff Roberson 	object = (vm_object_t)mem;
194e3a9e1b2SAlan Cox 	bzero(&object->mtx, sizeof(object->mtx));
1955285558aSAlan Cox 	VM_OBJECT_LOCK_INIT(object, "standard object");
1968355f576SJeff Roberson 
1978355f576SJeff Roberson 	/* These are true for any object that has been freed */
1988355f576SJeff Roberson 	object->paging_in_progress = 0;
1998355f576SJeff Roberson 	object->resident_page_count = 0;
2008355f576SJeff Roberson 	object->shadow_count = 0;
201b23f72e9SBrian Feldman 	return (0);
2028355f576SJeff Roberson }
203df8bae1dSRodney W. Grimes 
2043075778bSJohn Dyson void
2056395da54SIan Dowse _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
206df8bae1dSRodney W. Grimes {
2070cddd8f0SMatthew Dillon 
208df8bae1dSRodney W. Grimes 	TAILQ_INIT(&object->memq);
2091c500307SAlan Cox 	LIST_INIT(&object->shadow_head);
210a1f6d91cSDavid Greenman 
211b86ec922SMatthew Dillon 	object->root = NULL;
21224a1cce3SDavid Greenman 	object->type = type;
213df8bae1dSRodney W. Grimes 	object->size = size;
214b881da26SAlan Cox 	object->generation = 1;
215a1f6d91cSDavid Greenman 	object->ref_count = 1;
2163153e878SAlan Cox 	object->memattr = VM_MEMATTR_DEFAULT;
21724a1cce3SDavid Greenman 	object->flags = 0;
218ef694c1aSEdward Tomasz Napierala 	object->cred = NULL;
2193364c323SKonstantin Belousov 	object->charge = 0;
22060517fd1SJohn Baldwin 	if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
2213471677cSAlan Cox 		object->flags = OBJ_ONEMAPPING;
2222446e4f0SAlan Cox 	object->pg_color = 0;
22324a1cce3SDavid Greenman 	object->handle = NULL;
22424a1cce3SDavid Greenman 	object->backing_object = NULL;
225a316d390SJohn Dyson 	object->backing_object_offset = (vm_ooffset_t) 0;
226f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
227f8a47341SAlan Cox 	LIST_INIT(&object->rvq);
228f8a47341SAlan Cox #endif
2297bfda801SAlan Cox 	object->cache = NULL;
230a1f6d91cSDavid Greenman 
231a5698387SAlan Cox 	mtx_lock(&vm_object_list_mtx);
23260517fd1SJohn Baldwin 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
233a5698387SAlan Cox 	mtx_unlock(&vm_object_list_mtx);
234df8bae1dSRodney W. Grimes }
235df8bae1dSRodney W. Grimes 
236df8bae1dSRodney W. Grimes /*
23726f9a767SRodney W. Grimes  *	vm_object_init:
23826f9a767SRodney W. Grimes  *
23926f9a767SRodney W. Grimes  *	Initialize the VM objects module.
24026f9a767SRodney W. Grimes  */
24126f9a767SRodney W. Grimes void
2421b40f8c0SMatthew Dillon vm_object_init(void)
24326f9a767SRodney W. Grimes {
24426f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_list);
2456008862bSJohn Baldwin 	mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF);
2460217125fSDavid Greenman 
247c6c9025bSKonstantin Belousov 	VM_OBJECT_LOCK_INIT(kernel_object, "kernel object");
2489f5c801bSAlan Cox 	_vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
24926f9a767SRodney W. Grimes 	    kernel_object);
250f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
251f8a47341SAlan Cox 	kernel_object->flags |= OBJ_COLORED;
252f8a47341SAlan Cox 	kernel_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS);
253f8a47341SAlan Cox #endif
25426f9a767SRodney W. Grimes 
255c6c9025bSKonstantin Belousov 	VM_OBJECT_LOCK_INIT(kmem_object, "kmem object");
2569f5c801bSAlan Cox 	_vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
25726f9a767SRodney W. Grimes 	    kmem_object);
258f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
259f8a47341SAlan Cox 	kmem_object->flags |= OBJ_COLORED;
260f8a47341SAlan Cox 	kmem_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS);
261f8a47341SAlan Cox #endif
262ed6a7863SAlan Cox 
2638dbca793STor Egge 	/*
2648dbca793STor Egge 	 * The lock portion of struct vm_object must be type stable due
2658dbca793STor Egge 	 * to vm_pageout_fallback_object_lock locking a vm object
2668dbca793STor Egge 	 * without holding any references to it.
2678dbca793STor Egge 	 */
2688355f576SJeff Roberson 	obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL,
2698355f576SJeff Roberson #ifdef INVARIANTS
2708355f576SJeff Roberson 	    vm_object_zdtor,
2718355f576SJeff Roberson #else
2728355f576SJeff Roberson 	    NULL,
2738355f576SJeff Roberson #endif
274f3c625e4SJeff Roberson 	    vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM|UMA_ZONE_NOFREE);
27599448ed1SJohn Dyson }
27699448ed1SJohn Dyson 
27799448ed1SJohn Dyson void
2781b40f8c0SMatthew Dillon vm_object_clear_flag(vm_object_t object, u_short bits)
2791b40f8c0SMatthew Dillon {
2805440b5a9SAlan Cox 
281d647a0edSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
282b06805adSJake Burkholder 	object->flags &= ~bits;
2831b40f8c0SMatthew Dillon }
2841b40f8c0SMatthew Dillon 
2853153e878SAlan Cox /*
2863153e878SAlan Cox  *	Sets the default memory attribute for the specified object.  Pages
2873153e878SAlan Cox  *	that are allocated to this object are by default assigned this memory
2883153e878SAlan Cox  *	attribute.
2893153e878SAlan Cox  *
2903153e878SAlan Cox  *	Presently, this function must be called before any pages are allocated
2913153e878SAlan Cox  *	to the object.  In the future, this requirement may be relaxed for
2923153e878SAlan Cox  *	"default" and "swap" objects.
2933153e878SAlan Cox  */
2943153e878SAlan Cox int
2953153e878SAlan Cox vm_object_set_memattr(vm_object_t object, vm_memattr_t memattr)
2963153e878SAlan Cox {
2973153e878SAlan Cox 
2983153e878SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2993153e878SAlan Cox 	switch (object->type) {
3003153e878SAlan Cox 	case OBJT_DEFAULT:
3013153e878SAlan Cox 	case OBJT_DEVICE:
3023153e878SAlan Cox 	case OBJT_PHYS:
30301381811SJohn Baldwin 	case OBJT_SG:
3043153e878SAlan Cox 	case OBJT_SWAP:
3053153e878SAlan Cox 	case OBJT_VNODE:
3063153e878SAlan Cox 		if (!TAILQ_EMPTY(&object->memq))
3073153e878SAlan Cox 			return (KERN_FAILURE);
3083153e878SAlan Cox 		break;
3093153e878SAlan Cox 	case OBJT_DEAD:
3103153e878SAlan Cox 		return (KERN_INVALID_ARGUMENT);
3113153e878SAlan Cox 	}
3123153e878SAlan Cox 	object->memattr = memattr;
3133153e878SAlan Cox 	return (KERN_SUCCESS);
3143153e878SAlan Cox }
3153153e878SAlan Cox 
3161b40f8c0SMatthew Dillon void
3171b40f8c0SMatthew Dillon vm_object_pip_add(vm_object_t object, short i)
3181b40f8c0SMatthew Dillon {
319f279b88dSAlan Cox 
320d647a0edSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
321b06805adSJake Burkholder 	object->paging_in_progress += i;
3221b40f8c0SMatthew Dillon }
3231b40f8c0SMatthew Dillon 
3241b40f8c0SMatthew Dillon void
3251b40f8c0SMatthew Dillon vm_object_pip_subtract(vm_object_t object, short i)
3261b40f8c0SMatthew Dillon {
327d647a0edSAlan Cox 
3280fa05eaeSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
329b06805adSJake Burkholder 	object->paging_in_progress -= i;
3301b40f8c0SMatthew Dillon }
3311b40f8c0SMatthew Dillon 
3321b40f8c0SMatthew Dillon void
3331b40f8c0SMatthew Dillon vm_object_pip_wakeup(vm_object_t object)
3341b40f8c0SMatthew Dillon {
335f279b88dSAlan Cox 
336d647a0edSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
337b06805adSJake Burkholder 	object->paging_in_progress--;
3381b40f8c0SMatthew Dillon 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
3391b40f8c0SMatthew Dillon 		vm_object_clear_flag(object, OBJ_PIPWNT);
3401b40f8c0SMatthew Dillon 		wakeup(object);
3411b40f8c0SMatthew Dillon 	}
3421b40f8c0SMatthew Dillon }
3431b40f8c0SMatthew Dillon 
3441b40f8c0SMatthew Dillon void
3451b40f8c0SMatthew Dillon vm_object_pip_wakeupn(vm_object_t object, short i)
3461b40f8c0SMatthew Dillon {
347d647a0edSAlan Cox 
3480d420ad3SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
3491b40f8c0SMatthew Dillon 	if (i)
350b06805adSJake Burkholder 		object->paging_in_progress -= i;
3511b40f8c0SMatthew Dillon 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
3521b40f8c0SMatthew Dillon 		vm_object_clear_flag(object, OBJ_PIPWNT);
3531b40f8c0SMatthew Dillon 		wakeup(object);
3541b40f8c0SMatthew Dillon 	}
3551b40f8c0SMatthew Dillon }
3561b40f8c0SMatthew Dillon 
3571b40f8c0SMatthew Dillon void
3581b40f8c0SMatthew Dillon vm_object_pip_wait(vm_object_t object, char *waitid)
3591b40f8c0SMatthew Dillon {
3601ca58953SAlan Cox 
3611ca58953SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
3621ca58953SAlan Cox 	while (object->paging_in_progress) {
3631ca58953SAlan Cox 		object->flags |= OBJ_PIPWNT;
3641ca58953SAlan Cox 		msleep(object, VM_OBJECT_MTX(object), PVM, waitid, 0);
3651ca58953SAlan Cox 	}
3661b40f8c0SMatthew Dillon }
3671b40f8c0SMatthew Dillon 
36826f9a767SRodney W. Grimes /*
36926f9a767SRodney W. Grimes  *	vm_object_allocate:
37026f9a767SRodney W. Grimes  *
37126f9a767SRodney W. Grimes  *	Returns a new object with the given size.
37226f9a767SRodney W. Grimes  */
37326f9a767SRodney W. Grimes vm_object_t
3746395da54SIan Dowse vm_object_allocate(objtype_t type, vm_pindex_t size)
37526f9a767SRodney W. Grimes {
37690688d13SAlan Cox 	vm_object_t object;
37790688d13SAlan Cox 
37890688d13SAlan Cox 	object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK);
37990688d13SAlan Cox 	_vm_object_allocate(type, size, object);
38090688d13SAlan Cox 	return (object);
38126f9a767SRodney W. Grimes }
38226f9a767SRodney W. Grimes 
38326f9a767SRodney W. Grimes 
38426f9a767SRodney W. Grimes /*
385df8bae1dSRodney W. Grimes  *	vm_object_reference:
386df8bae1dSRodney W. Grimes  *
38715347817SAlan Cox  *	Gets another reference to the given object.  Note: OBJ_DEAD
38815347817SAlan Cox  *	objects can be referenced during final cleaning.
389df8bae1dSRodney W. Grimes  */
3906476c0d2SJohn Dyson void
3911b40f8c0SMatthew Dillon vm_object_reference(vm_object_t object)
392df8bae1dSRodney W. Grimes {
393df8bae1dSRodney W. Grimes 	if (object == NULL)
394df8bae1dSRodney W. Grimes 		return;
39515347817SAlan Cox 	VM_OBJECT_LOCK(object);
39652481a9aSJeff Roberson 	vm_object_reference_locked(object);
39782f9defeSAlan Cox 	VM_OBJECT_UNLOCK(object);
39895e5e988SJohn Dyson }
39995e5e988SJohn Dyson 
40023955314SAlfred Perlstein /*
401b921a12bSAlan Cox  *	vm_object_reference_locked:
402b921a12bSAlan Cox  *
403b921a12bSAlan Cox  *	Gets another reference to the given object.
404b921a12bSAlan Cox  *
405b921a12bSAlan Cox  *	The object must be locked.
406b921a12bSAlan Cox  */
407b921a12bSAlan Cox void
408b921a12bSAlan Cox vm_object_reference_locked(vm_object_t object)
409b921a12bSAlan Cox {
410b921a12bSAlan Cox 	struct vnode *vp;
411b921a12bSAlan Cox 
412b921a12bSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
413b921a12bSAlan Cox 	object->ref_count++;
414b921a12bSAlan Cox 	if (object->type == OBJT_VNODE) {
415b921a12bSAlan Cox 		vp = object->handle;
416b921a12bSAlan Cox 		vref(vp);
417b921a12bSAlan Cox 	}
418b921a12bSAlan Cox }
419b921a12bSAlan Cox 
420b921a12bSAlan Cox /*
4219d5abbddSJens Schweikhardt  * Handle deallocating an object of type OBJT_VNODE.
42223955314SAlfred Perlstein  */
42302dd8331SAlan Cox static void
4241b40f8c0SMatthew Dillon vm_object_vndeallocate(vm_object_t object)
42595e5e988SJohn Dyson {
42695e5e988SJohn Dyson 	struct vnode *vp = (struct vnode *) object->handle;
427219cbf59SEivind Eklund 
428ae51ff11SJeff Roberson 	VFS_ASSERT_GIANT(vp->v_mount);
429ad682c48SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
4305526d2d9SEivind Eklund 	KASSERT(object->type == OBJT_VNODE,
4315526d2d9SEivind Eklund 	    ("vm_object_vndeallocate: not a vnode object"));
432219cbf59SEivind Eklund 	KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
433219cbf59SEivind Eklund #ifdef INVARIANTS
43495e5e988SJohn Dyson 	if (object->ref_count == 0) {
43595e5e988SJohn Dyson 		vprint("vm_object_vndeallocate", vp);
43695e5e988SJohn Dyson 		panic("vm_object_vndeallocate: bad object reference count");
43795e5e988SJohn Dyson 	}
43895e5e988SJohn Dyson #endif
43995e5e988SJohn Dyson 
44095e5e988SJohn Dyson 	object->ref_count--;
44147221757SJohn Dyson 	if (object->ref_count == 0) {
442e6e370a7SJeff Roberson 		mp_fixme("Unlocked vflag access.");
443e6e370a7SJeff Roberson 		vp->v_vflag &= ~VV_TEXT;
4442be70f79SJohn Dyson 	}
445ad682c48SAlan Cox 	VM_OBJECT_UNLOCK(object);
44623955314SAlfred Perlstein 	/*
44723955314SAlfred Perlstein 	 * vrele may need a vop lock
44823955314SAlfred Perlstein 	 */
44947221757SJohn Dyson 	vrele(vp);
450df8bae1dSRodney W. Grimes }
451df8bae1dSRodney W. Grimes 
452df8bae1dSRodney W. Grimes /*
453df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
454df8bae1dSRodney W. Grimes  *
455df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
456df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
457df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
458df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
459df8bae1dSRodney W. Grimes  *	may be relinquished.
460df8bae1dSRodney W. Grimes  *
461df8bae1dSRodney W. Grimes  *	No object may be locked.
462df8bae1dSRodney W. Grimes  */
46326f9a767SRodney W. Grimes void
4641b40f8c0SMatthew Dillon vm_object_deallocate(vm_object_t object)
465df8bae1dSRodney W. Grimes {
466df8bae1dSRodney W. Grimes 	vm_object_t temp;
467df8bae1dSRodney W. Grimes 
468df8bae1dSRodney W. Grimes 	while (object != NULL) {
469ae51ff11SJeff Roberson 		int vfslocked;
470ca95b514SJohn Baldwin 
471ae51ff11SJeff Roberson 		vfslocked = 0;
472ca95b514SJohn Baldwin 	restart:
473ad682c48SAlan Cox 		VM_OBJECT_LOCK(object);
4743b68228cSAlan Cox 		if (object->type == OBJT_VNODE) {
475ca95b514SJohn Baldwin 			struct vnode *vp = (struct vnode *) object->handle;
476ca95b514SJohn Baldwin 
477ca95b514SJohn Baldwin 			/*
478ca95b514SJohn Baldwin 			 * Conditionally acquire Giant for a vnode-backed
479ca95b514SJohn Baldwin 			 * object.  We have to be careful since the type of
480ca95b514SJohn Baldwin 			 * a vnode object can change while the object is
481ca95b514SJohn Baldwin 			 * unlocked.
482ca95b514SJohn Baldwin 			 */
483ca95b514SJohn Baldwin 			if (VFS_NEEDSGIANT(vp->v_mount) && !vfslocked) {
484ca95b514SJohn Baldwin 				vfslocked = 1;
485ca95b514SJohn Baldwin 				if (!mtx_trylock(&Giant)) {
486ca95b514SJohn Baldwin 					VM_OBJECT_UNLOCK(object);
487ca95b514SJohn Baldwin 					mtx_lock(&Giant);
488ca95b514SJohn Baldwin 					goto restart;
489ca95b514SJohn Baldwin 				}
490ca95b514SJohn Baldwin 			}
49195e5e988SJohn Dyson 			vm_object_vndeallocate(object);
492ae51ff11SJeff Roberson 			VFS_UNLOCK_GIANT(vfslocked);
49323b186d3SAlan Cox 			return;
494ca95b514SJohn Baldwin 		} else
495ca95b514SJohn Baldwin 			/*
496ca95b514SJohn Baldwin 			 * This is to handle the case that the object
497ca95b514SJohn Baldwin 			 * changed type while we dropped its lock to
498ca95b514SJohn Baldwin 			 * obtain Giant.
499ca95b514SJohn Baldwin 			 */
500ca95b514SJohn Baldwin 			VFS_UNLOCK_GIANT(vfslocked);
50195e5e988SJohn Dyson 
5028125b1e6SAlfred Perlstein 		KASSERT(object->ref_count != 0,
5038125b1e6SAlfred Perlstein 			("vm_object_deallocate: object deallocated too many times: %d", object->type));
5042be70f79SJohn Dyson 
5052be70f79SJohn Dyson 		/*
5068125b1e6SAlfred Perlstein 		 * If the reference count goes to 0 we start calling
5078125b1e6SAlfred Perlstein 		 * vm_object_terminate() on the object chain.
5088125b1e6SAlfred Perlstein 		 * A ref count of 1 may be a special case depending on the
5098125b1e6SAlfred Perlstein 		 * shadow count being 0 or 1.
5102be70f79SJohn Dyson 		 */
511c0877f10SJohn Dyson 		object->ref_count--;
5128125b1e6SAlfred Perlstein 		if (object->ref_count > 1) {
5133b68228cSAlan Cox 			VM_OBJECT_UNLOCK(object);
51423b186d3SAlan Cox 			return;
5158125b1e6SAlfred Perlstein 		} else if (object->ref_count == 1) {
5164c8e0452SAlan Cox 			if (object->shadow_count == 0 &&
5174c8e0452SAlan Cox 			    object->handle == NULL &&
5184c8e0452SAlan Cox 			    (object->type == OBJT_DEFAULT ||
5194c8e0452SAlan Cox 			     object->type == OBJT_SWAP)) {
5208125b1e6SAlfred Perlstein 				vm_object_set_flag(object, OBJ_ONEMAPPING);
5218125b1e6SAlfred Perlstein 			} else if ((object->shadow_count == 1) &&
5228125b1e6SAlfred Perlstein 			    (object->handle == NULL) &&
52324a1cce3SDavid Greenman 			    (object->type == OBJT_DEFAULT ||
52424a1cce3SDavid Greenman 			     object->type == OBJT_SWAP)) {
525a1f6d91cSDavid Greenman 				vm_object_t robject;
52695e5e988SJohn Dyson 
5271c500307SAlan Cox 				robject = LIST_FIRST(&object->shadow_head);
5285526d2d9SEivind Eklund 				KASSERT(robject != NULL,
529219cbf59SEivind Eklund 				    ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
5305526d2d9SEivind Eklund 					 object->ref_count,
5315526d2d9SEivind Eklund 					 object->shadow_count));
532b72b0115SAlan Cox 				if (!VM_OBJECT_TRYLOCK(robject)) {
533b72b0115SAlan Cox 					/*
534b72b0115SAlan Cox 					 * Avoid a potential deadlock.
535b72b0115SAlan Cox 					 */
536b72b0115SAlan Cox 					object->ref_count++;
537b72b0115SAlan Cox 					VM_OBJECT_UNLOCK(object);
538a7d86121SAlan Cox 					/*
539a7d86121SAlan Cox 					 * More likely than not the thread
540a7d86121SAlan Cox 					 * holding robject's lock has lower
541a7d86121SAlan Cox 					 * priority than the current thread.
542a7d86121SAlan Cox 					 * Let the lower priority thread run.
543a7d86121SAlan Cox 					 */
5448db5fc58SJohn Baldwin 					pause("vmo_de", 1);
545b72b0115SAlan Cox 					continue;
546b72b0115SAlan Cox 				}
547d936694fSAlan Cox 				/*
548d936694fSAlan Cox 				 * Collapse object into its shadow unless its
549d936694fSAlan Cox 				 * shadow is dead.  In that case, object will
550d936694fSAlan Cox 				 * be deallocated by the thread that is
551d936694fSAlan Cox 				 * deallocating its shadow.
552d936694fSAlan Cox 				 */
553d936694fSAlan Cox 				if ((robject->flags & OBJ_DEAD) == 0 &&
554d936694fSAlan Cox 				    (robject->handle == NULL) &&
55524a1cce3SDavid Greenman 				    (robject->type == OBJT_DEFAULT ||
55624a1cce3SDavid Greenman 				     robject->type == OBJT_SWAP)) {
557a1f6d91cSDavid Greenman 
55895e5e988SJohn Dyson 					robject->ref_count++;
559138449dcSAlan Cox retry:
560138449dcSAlan Cox 					if (robject->paging_in_progress) {
561138449dcSAlan Cox 						VM_OBJECT_UNLOCK(object);
562138449dcSAlan Cox 						vm_object_pip_wait(robject,
563138449dcSAlan Cox 						    "objde1");
5642e9f4a69SAlan Cox 						temp = robject->backing_object;
5652e9f4a69SAlan Cox 						if (object == temp) {
566138449dcSAlan Cox 							VM_OBJECT_LOCK(object);
567138449dcSAlan Cox 							goto retry;
5682e9f4a69SAlan Cox 						}
569138449dcSAlan Cox 					} else if (object->paging_in_progress) {
570138449dcSAlan Cox 						VM_OBJECT_UNLOCK(robject);
571138449dcSAlan Cox 						object->flags |= OBJ_PIPWNT;
572138449dcSAlan Cox 						msleep(object,
573138449dcSAlan Cox 						    VM_OBJECT_MTX(object),
574138449dcSAlan Cox 						    PDROP | PVM, "objde2", 0);
575138449dcSAlan Cox 						VM_OBJECT_LOCK(robject);
5762e9f4a69SAlan Cox 						temp = robject->backing_object;
5772e9f4a69SAlan Cox 						if (object == temp) {
578138449dcSAlan Cox 							VM_OBJECT_LOCK(object);
579138449dcSAlan Cox 							goto retry;
580a1f6d91cSDavid Greenman 						}
5812e9f4a69SAlan Cox 					} else
5823b68228cSAlan Cox 						VM_OBJECT_UNLOCK(object);
5832e9f4a69SAlan Cox 
58495e5e988SJohn Dyson 					if (robject->ref_count == 1) {
58595e5e988SJohn Dyson 						robject->ref_count--;
586ba8da839SDavid Greenman 						object = robject;
58795e5e988SJohn Dyson 						goto doterm;
58895e5e988SJohn Dyson 					}
58995e5e988SJohn Dyson 					object = robject;
59095e5e988SJohn Dyson 					vm_object_collapse(object);
591d7fc2210SAlan Cox 					VM_OBJECT_UNLOCK(object);
592ba8da839SDavid Greenman 					continue;
593a1f6d91cSDavid Greenman 				}
594b72b0115SAlan Cox 				VM_OBJECT_UNLOCK(robject);
59595e5e988SJohn Dyson 			}
5963b68228cSAlan Cox 			VM_OBJECT_UNLOCK(object);
59723b186d3SAlan Cox 			return;
59895e5e988SJohn Dyson 		}
59995e5e988SJohn Dyson doterm:
60024a1cce3SDavid Greenman 		temp = object->backing_object;
601c9917419SAlan Cox 		if (temp != NULL) {
602c9917419SAlan Cox 			VM_OBJECT_LOCK(temp);
6031c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
60495e5e988SJohn Dyson 			temp->shadow_count--;
605c9917419SAlan Cox 			VM_OBJECT_UNLOCK(temp);
60695461b45SJohn Dyson 			object->backing_object = NULL;
607de5f6a77SJohn Dyson 		}
608245df27cSMatthew Dillon 		/*
609245df27cSMatthew Dillon 		 * Don't double-terminate, we could be in a termination
610245df27cSMatthew Dillon 		 * recursion due to the terminate having to sync data
611245df27cSMatthew Dillon 		 * to disk.
612245df27cSMatthew Dillon 		 */
613245df27cSMatthew Dillon 		if ((object->flags & OBJ_DEAD) == 0)
614df8bae1dSRodney W. Grimes 			vm_object_terminate(object);
615c829b9d0SAlan Cox 		else
616c829b9d0SAlan Cox 			VM_OBJECT_UNLOCK(object);
617df8bae1dSRodney W. Grimes 		object = temp;
618df8bae1dSRodney W. Grimes 	}
619df8bae1dSRodney W. Grimes }
620df8bae1dSRodney W. Grimes 
621df8bae1dSRodney W. Grimes /*
6222ac78f0eSStephan Uphoff  *	vm_object_destroy removes the object from the global object list
6232ac78f0eSStephan Uphoff  *      and frees the space for the object.
6242ac78f0eSStephan Uphoff  */
6252ac78f0eSStephan Uphoff void
6262ac78f0eSStephan Uphoff vm_object_destroy(vm_object_t object)
6272ac78f0eSStephan Uphoff {
6282ac78f0eSStephan Uphoff 
6292ac78f0eSStephan Uphoff 	/*
6302ac78f0eSStephan Uphoff 	 * Remove the object from the global object list.
6312ac78f0eSStephan Uphoff 	 */
6322ac78f0eSStephan Uphoff 	mtx_lock(&vm_object_list_mtx);
6332ac78f0eSStephan Uphoff 	TAILQ_REMOVE(&vm_object_list, object, object_list);
6342ac78f0eSStephan Uphoff 	mtx_unlock(&vm_object_list_mtx);
6352ac78f0eSStephan Uphoff 
6362ac78f0eSStephan Uphoff 	/*
6373364c323SKonstantin Belousov 	 * Release the allocation charge.
6383364c323SKonstantin Belousov 	 */
639ef694c1aSEdward Tomasz Napierala 	if (object->cred != NULL) {
6403364c323SKonstantin Belousov 		KASSERT(object->type == OBJT_DEFAULT ||
6413364c323SKonstantin Belousov 		    object->type == OBJT_SWAP,
642ef694c1aSEdward Tomasz Napierala 		    ("vm_object_terminate: non-swap obj %p has cred",
6433364c323SKonstantin Belousov 		     object));
644ef694c1aSEdward Tomasz Napierala 		swap_release_by_cred(object->charge, object->cred);
6453364c323SKonstantin Belousov 		object->charge = 0;
646ef694c1aSEdward Tomasz Napierala 		crfree(object->cred);
647ef694c1aSEdward Tomasz Napierala 		object->cred = NULL;
6483364c323SKonstantin Belousov 	}
6493364c323SKonstantin Belousov 
6503364c323SKonstantin Belousov 	/*
6512ac78f0eSStephan Uphoff 	 * Free the space for the object.
6522ac78f0eSStephan Uphoff 	 */
6532ac78f0eSStephan Uphoff 	uma_zfree(obj_zone, object);
6542ac78f0eSStephan Uphoff }
6552ac78f0eSStephan Uphoff 
6562ac78f0eSStephan Uphoff /*
657df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
658df8bae1dSRodney W. Grimes  *	up all previously used resources.
659df8bae1dSRodney W. Grimes  *
660df8bae1dSRodney W. Grimes  *	The object must be locked.
6611c7c3c6aSMatthew Dillon  *	This routine may block.
662df8bae1dSRodney W. Grimes  */
66395e5e988SJohn Dyson void
6641b40f8c0SMatthew Dillon vm_object_terminate(vm_object_t object)
665df8bae1dSRodney W. Grimes {
66617ea6f00SAlan Cox 	vm_page_t p, p_next;
667df8bae1dSRodney W. Grimes 
668c829b9d0SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
6690cddd8f0SMatthew Dillon 
67095e5e988SJohn Dyson 	/*
67195e5e988SJohn Dyson 	 * Make sure no one uses us.
67295e5e988SJohn Dyson 	 */
673069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_DEAD);
6743c631446SJohn Dyson 
675df8bae1dSRodney W. Grimes 	/*
676f6b04d2bSDavid Greenman 	 * wait for the pageout daemon to be done with the object
677df8bae1dSRodney W. Grimes 	 */
67866095752SJohn Dyson 	vm_object_pip_wait(object, "objtrm");
679df8bae1dSRodney W. Grimes 
6805526d2d9SEivind Eklund 	KASSERT(!object->paging_in_progress,
6815526d2d9SEivind Eklund 		("vm_object_terminate: pageout in progress"));
68226f9a767SRodney W. Grimes 
68326f9a767SRodney W. Grimes 	/*
6840d94caffSDavid Greenman 	 * Clean and free the pages, as appropriate. All references to the
6850d94caffSDavid Greenman 	 * object are gone, so we don't need to lock it.
68626f9a767SRodney W. Grimes 	 */
68724a1cce3SDavid Greenman 	if (object->type == OBJT_VNODE) {
688f7dd7b63SAlan Cox 		struct vnode *vp = (struct vnode *)object->handle;
68995e5e988SJohn Dyson 
69095e5e988SJohn Dyson 		/*
69195e5e988SJohn Dyson 		 * Clean pages and flush buffers.
69295e5e988SJohn Dyson 		 */
6938f9110f6SJohn Dyson 		vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
694b6e48e03SAlan Cox 		VM_OBJECT_UNLOCK(object);
69595e5e988SJohn Dyson 
6960d7935fdSAttilio Rao 		vinvalbuf(vp, V_SAVE, 0, 0);
697f7dd7b63SAlan Cox 
698f7dd7b63SAlan Cox 		VM_OBJECT_LOCK(object);
699bef608bdSJohn Dyson 	}
700bef608bdSJohn Dyson 
701971dd342SAlfred Perlstein 	KASSERT(object->ref_count == 0,
702971dd342SAlfred Perlstein 		("vm_object_terminate: object with references, ref_count=%d",
703971dd342SAlfred Perlstein 		object->ref_count));
704996c772fSJohn Dyson 
7050d94caffSDavid Greenman 	/*
70617ea6f00SAlan Cox 	 * Free any remaining pageable pages.  This also removes them from the
70717ea6f00SAlan Cox 	 * paging queues.  However, don't free wired pages, just remove them
70817ea6f00SAlan Cox 	 * from the object.  Rather than incrementally removing each page from
70917ea6f00SAlan Cox 	 * the object, the page and object are reset to any empty state.
710df8bae1dSRodney W. Grimes 	 */
71117ea6f00SAlan Cox 	TAILQ_FOREACH_SAFE(p, &object->memq, listq, p_next) {
7129af80719SAlan Cox 		KASSERT(!p->busy && (p->oflags & VPO_BUSY) == 0,
71317ea6f00SAlan Cox 		    ("vm_object_terminate: freeing busy page %p", p));
7142965a453SKip Macy 		vm_page_lock(p);
71517ea6f00SAlan Cox 		/*
71617ea6f00SAlan Cox 		 * Optimize the page's removal from the object by resetting
71717ea6f00SAlan Cox 		 * its "object" field.  Specifically, if the page is not
71817ea6f00SAlan Cox 		 * wired, then the effect of this assignment is that
71917ea6f00SAlan Cox 		 * vm_page_free()'s call to vm_page_remove() will return
72017ea6f00SAlan Cox 		 * immediately without modifying the page or the object.
72117ea6f00SAlan Cox 		 */
72217ea6f00SAlan Cox 		p->object = NULL;
7230b10ba98SDavid Greenman 		if (p->wire_count == 0) {
724df8bae1dSRodney W. Grimes 			vm_page_free(p);
72570721880SAlan Cox 			PCPU_INC(cnt.v_pfree);
72617ea6f00SAlan Cox 		}
7272965a453SKip Macy 		vm_page_unlock(p);
7282965a453SKip Macy 	}
72917ea6f00SAlan Cox 	/*
73017ea6f00SAlan Cox 	 * If the object contained any pages, then reset it to an empty state.
73117ea6f00SAlan Cox 	 * None of the object's fields, including "resident_page_count", were
73217ea6f00SAlan Cox 	 * modified by the preceding loop.
73317ea6f00SAlan Cox 	 */
73417ea6f00SAlan Cox 	if (object->resident_page_count != 0) {
73517ea6f00SAlan Cox 		object->root = NULL;
73617ea6f00SAlan Cox 		TAILQ_INIT(&object->memq);
73717ea6f00SAlan Cox 		object->resident_page_count = 0;
73817ea6f00SAlan Cox 		if (object->type == OBJT_VNODE)
73917ea6f00SAlan Cox 			vdrop(object->handle);
74017ea6f00SAlan Cox 	}
741bef608bdSJohn Dyson 
742f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
743f8a47341SAlan Cox 	if (__predict_false(!LIST_EMPTY(&object->rvq)))
744f8a47341SAlan Cox 		vm_reserv_break_all(object);
745f8a47341SAlan Cox #endif
7467bfda801SAlan Cox 	if (__predict_false(object->cache != NULL))
747c9444914SAlan Cox 		vm_page_cache_free(object, 0, 0);
7487bfda801SAlan Cox 
7492d8acc0fSJohn Dyson 	/*
7509fcfb650SDavid Greenman 	 * Let the pager know object is dead.
7519fcfb650SDavid Greenman 	 */
7529fcfb650SDavid Greenman 	vm_pager_deallocate(object);
753658ad5ffSAlan Cox 	VM_OBJECT_UNLOCK(object);
7549fcfb650SDavid Greenman 
7552ac78f0eSStephan Uphoff 	vm_object_destroy(object);
75647221757SJohn Dyson }
757df8bae1dSRodney W. Grimes 
758edf93b25SAlan Cox /*
759edf93b25SAlan Cox  * Make the page read-only so that we can clear the object flags.  However, if
760edf93b25SAlan Cox  * this is a nosync mmap then the object is likely to stay dirty so do not
761edf93b25SAlan Cox  * mess with the page and do not clear the object flags.  Returns TRUE if the
762edf93b25SAlan Cox  * page should be flushed, and FALSE otherwise.
763edf93b25SAlan Cox  */
7643280870dSKonstantin Belousov static boolean_t
7653280870dSKonstantin Belousov vm_object_page_remove_write(vm_page_t p, int flags, int *clearobjflags)
7663280870dSKonstantin Belousov {
7673280870dSKonstantin Belousov 
7683280870dSKonstantin Belousov 	/*
7693280870dSKonstantin Belousov 	 * If we have been asked to skip nosync pages and this is a
7703280870dSKonstantin Belousov 	 * nosync page, skip it.  Note that the object flags were not
7713280870dSKonstantin Belousov 	 * cleared in this case so we do not have to set them.
7723280870dSKonstantin Belousov 	 */
7733280870dSKonstantin Belousov 	if ((flags & OBJPC_NOSYNC) != 0 && (p->oflags & VPO_NOSYNC) != 0) {
7743280870dSKonstantin Belousov 		*clearobjflags = 0;
7753280870dSKonstantin Belousov 		return (FALSE);
7763280870dSKonstantin Belousov 	} else {
7773280870dSKonstantin Belousov 		pmap_remove_write(p);
7783280870dSKonstantin Belousov 		return (p->dirty != 0);
7793280870dSKonstantin Belousov 	}
7803280870dSKonstantin Belousov }
7813280870dSKonstantin Belousov 
782df8bae1dSRodney W. Grimes /*
783df8bae1dSRodney W. Grimes  *	vm_object_page_clean
784df8bae1dSRodney W. Grimes  *
7854f79d873SMatthew Dillon  *	Clean all dirty pages in the specified range of object.  Leaves page
7864f79d873SMatthew Dillon  * 	on whatever queue it is currently on.   If NOSYNC is set then do not
787b146f9e5SAlan Cox  *	write out pages with VPO_NOSYNC set (originally comes from MAP_NOSYNC),
7884f79d873SMatthew Dillon  *	leaving the object dirty.
78926f9a767SRodney W. Grimes  *
79043b7990eSMatthew Dillon  *	When stuffing pages asynchronously, allow clustering.  XXX we need a
79143b7990eSMatthew Dillon  *	synchronous clustering mode implementation.
79243b7990eSMatthew Dillon  *
79326f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
79426f9a767SRodney W. Grimes  *
79526f9a767SRodney W. Grimes  *	The object must be locked.
79626f9a767SRodney W. Grimes  */
797f6b04d2bSDavid Greenman void
798*17f3095dSAlan Cox vm_object_page_clean(vm_object_t object, vm_ooffset_t start, vm_ooffset_t end,
799e239bb97SKonstantin Belousov     int flags)
800f6b04d2bSDavid Greenman {
801e239bb97SKonstantin Belousov 	vm_page_t np, p;
802*17f3095dSAlan Cox 	vm_pindex_t pi, tend, tstart;
803e239bb97SKonstantin Belousov 	int clearobjflags, curgeneration, n, pagerflags;
804f6b04d2bSDavid Greenman 
8057bec141bSKip Macy 	mtx_assert(&vm_page_queue_mtx, MA_NOTOWNED);
806b6e48e03SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
807757216f3SKonstantin Belousov 	KASSERT(object->type == OBJT_VNODE, ("Not a vnode object"));
808e239bb97SKonstantin Belousov 	if ((object->flags & OBJ_MIGHTBEDIRTY) == 0 ||
809e239bb97SKonstantin Belousov 	    object->resident_page_count == 0)
810f6b04d2bSDavid Greenman 		return;
811f6b04d2bSDavid Greenman 
812e239bb97SKonstantin Belousov 	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) != 0 ?
813e239bb97SKonstantin Belousov 	    VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
814e239bb97SKonstantin Belousov 	pagerflags |= (flags & OBJPC_INVAL) != 0 ? VM_PAGER_PUT_INVAL : 0;
815e239bb97SKonstantin Belousov 
816*17f3095dSAlan Cox 	tstart = OFF_TO_IDX(start);
817*17f3095dSAlan Cox 	tend = (end == 0) ? object->size : OFF_TO_IDX(end + PAGE_MASK);
818*17f3095dSAlan Cox 	clearobjflags = tstart == 0 && tend >= object->size;
819f6b04d2bSDavid Greenman 
820bd7e5f99SJohn Dyson rescan:
8212d8acc0fSJohn Dyson 	curgeneration = object->generation;
8222d8acc0fSJohn Dyson 
823*17f3095dSAlan Cox 	for (p = vm_page_find_least(object, tstart); p != NULL; p = np) {
824bd7e5f99SJohn Dyson 		pi = p->pindex;
825e239bb97SKonstantin Belousov 		if (pi >= tend)
826e239bb97SKonstantin Belousov 			break;
827e239bb97SKonstantin Belousov 		np = TAILQ_NEXT(p, listq);
828e239bb97SKonstantin Belousov 		if (p->valid == 0)
829aef922f5SJohn Dyson 			continue;
830780636b7SKonstantin Belousov 		if (vm_page_sleep_if_busy(p, TRUE, "vpcwai")) {
831e239bb97SKonstantin Belousov 			if (object->generation != curgeneration)
832e239bb97SKonstantin Belousov 				goto rescan;
833780636b7SKonstantin Belousov 			np = vm_page_find_least(object, pi);
834780636b7SKonstantin Belousov 			continue;
835f6b04d2bSDavid Greenman 		}
8363280870dSKonstantin Belousov 		if (!vm_object_page_remove_write(p, flags, &clearobjflags))
837bd7e5f99SJohn Dyson 			continue;
838e239bb97SKonstantin Belousov 
8393280870dSKonstantin Belousov 		n = vm_object_page_collect_flush(object, p, pagerflags,
8403280870dSKonstantin Belousov 		    flags, &clearobjflags);
8417bec141bSKip Macy 		if (object->generation != curgeneration)
842b9b7a4beSMatthew Dillon 			goto rescan;
843e239bb97SKonstantin Belousov 		np = vm_page_find_least(object, pi + n);
844b9b7a4beSMatthew Dillon 	}
845b9b7a4beSMatthew Dillon #if 0
846e239bb97SKonstantin Belousov 	VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC) ? MNT_WAIT : 0);
847b9b7a4beSMatthew Dillon #endif
848b9b7a4beSMatthew Dillon 
849edf93b25SAlan Cox 	if (clearobjflags)
8503280870dSKonstantin Belousov 		vm_object_clear_flag(object, OBJ_MIGHTBEDIRTY);
851b9b7a4beSMatthew Dillon }
852b9b7a4beSMatthew Dillon 
853b9b7a4beSMatthew Dillon static int
8543280870dSKonstantin Belousov vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int pagerflags,
8553280870dSKonstantin Belousov     int flags, int *clearobjflags)
856b9b7a4beSMatthew Dillon {
8573157c503SKonstantin Belousov 	vm_page_t ma[vm_pageout_page_count], p_first, tp;
8583157c503SKonstantin Belousov 	int count, i, mreq, runlen;
859b9b7a4beSMatthew Dillon 
8607bec141bSKip Macy 	mtx_assert(&vm_page_queue_mtx, MA_NOTOWNED);
8617bec141bSKip Macy 	vm_page_lock_assert(p, MA_NOTOWNED);
8627bec141bSKip Macy 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
8633157c503SKonstantin Belousov 
8643157c503SKonstantin Belousov 	count = 1;
8653157c503SKonstantin Belousov 	mreq = 0;
8663157c503SKonstantin Belousov 
8673157c503SKonstantin Belousov 	for (tp = p; count < vm_pageout_page_count; count++) {
8683157c503SKonstantin Belousov 		tp = vm_page_next(tp);
869e239bb97SKonstantin Belousov 		if (tp == NULL || tp->busy != 0 || (tp->oflags & VPO_BUSY) != 0)
870bd7e5f99SJohn Dyson 			break;
8713280870dSKonstantin Belousov 		if (!vm_object_page_remove_write(tp, flags, clearobjflags))
872bd7e5f99SJohn Dyson 			break;
873bd7e5f99SJohn Dyson 	}
874aef922f5SJohn Dyson 
8753157c503SKonstantin Belousov 	for (p_first = p; count < vm_pageout_page_count; count++) {
8763157c503SKonstantin Belousov 		tp = vm_page_prev(p_first);
877e239bb97SKonstantin Belousov 		if (tp == NULL || tp->busy != 0 || (tp->oflags & VPO_BUSY) != 0)
878bd7e5f99SJohn Dyson 			break;
8793280870dSKonstantin Belousov 		if (!vm_object_page_remove_write(tp, flags, clearobjflags))
880bd7e5f99SJohn Dyson 			break;
8813157c503SKonstantin Belousov 		p_first = tp;
8823157c503SKonstantin Belousov 		mreq++;
883bd7e5f99SJohn Dyson 	}
884bd7e5f99SJohn Dyson 
8853157c503SKonstantin Belousov 	for (tp = p_first, i = 0; i < count; tp = TAILQ_NEXT(tp, listq), i++)
8863157c503SKonstantin Belousov 		ma[i] = tp;
887cf2819ccSJohn Dyson 
8883157c503SKonstantin Belousov 	vm_pageout_flush(ma, count, pagerflags, mreq, &runlen);
8891e8a675cSKonstantin Belousov 	return (runlen);
89026f9a767SRodney W. Grimes }
891df8bae1dSRodney W. Grimes 
8921efb74fbSJohn Dyson /*
893950f8459SAlan Cox  * Note that there is absolutely no sense in writing out
894950f8459SAlan Cox  * anonymous objects, so we track down the vnode object
895950f8459SAlan Cox  * to write out.
896950f8459SAlan Cox  * We invalidate (remove) all pages from the address space
897950f8459SAlan Cox  * for semantic correctness.
898950f8459SAlan Cox  *
899950f8459SAlan Cox  * Note: certain anonymous maps, such as MAP_NOSYNC maps,
900950f8459SAlan Cox  * may start out with a NULL object.
901950f8459SAlan Cox  */
902950f8459SAlan Cox void
903950f8459SAlan Cox vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size,
904950f8459SAlan Cox     boolean_t syncio, boolean_t invalidate)
905950f8459SAlan Cox {
906950f8459SAlan Cox 	vm_object_t backing_object;
907950f8459SAlan Cox 	struct vnode *vp;
9083b582b4eSTor Egge 	struct mount *mp;
909950f8459SAlan Cox 	int flags;
910950f8459SAlan Cox 
911950f8459SAlan Cox 	if (object == NULL)
912950f8459SAlan Cox 		return;
913950f8459SAlan Cox 	VM_OBJECT_LOCK(object);
914950f8459SAlan Cox 	while ((backing_object = object->backing_object) != NULL) {
915950f8459SAlan Cox 		VM_OBJECT_LOCK(backing_object);
91656e0670fSAlan Cox 		offset += object->backing_object_offset;
917950f8459SAlan Cox 		VM_OBJECT_UNLOCK(object);
918950f8459SAlan Cox 		object = backing_object;
919950f8459SAlan Cox 		if (object->size < OFF_TO_IDX(offset + size))
920950f8459SAlan Cox 			size = IDX_TO_OFF(object->size) - offset;
921950f8459SAlan Cox 	}
922950f8459SAlan Cox 	/*
923950f8459SAlan Cox 	 * Flush pages if writing is allowed, invalidate them
924950f8459SAlan Cox 	 * if invalidation requested.  Pages undergoing I/O
925950f8459SAlan Cox 	 * will be ignored by vm_object_page_remove().
926950f8459SAlan Cox 	 *
927950f8459SAlan Cox 	 * We cannot lock the vnode and then wait for paging
928950f8459SAlan Cox 	 * to complete without deadlocking against vm_fault.
929950f8459SAlan Cox 	 * Instead we simply call vm_object_page_remove() and
930950f8459SAlan Cox 	 * allow it to block internally on a page-by-page
931950f8459SAlan Cox 	 * basis when it encounters pages undergoing async
932950f8459SAlan Cox 	 * I/O.
933950f8459SAlan Cox 	 */
934950f8459SAlan Cox 	if (object->type == OBJT_VNODE &&
935950f8459SAlan Cox 	    (object->flags & OBJ_MIGHTBEDIRTY) != 0) {
936ae51ff11SJeff Roberson 		int vfslocked;
937950f8459SAlan Cox 		vp = object->handle;
938950f8459SAlan Cox 		VM_OBJECT_UNLOCK(object);
9393b582b4eSTor Egge 		(void) vn_start_write(vp, &mp, V_WAIT);
940ae51ff11SJeff Roberson 		vfslocked = VFS_LOCK_GIANT(vp->v_mount);
941cb05b60aSAttilio Rao 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
942950f8459SAlan Cox 		flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
943950f8459SAlan Cox 		flags |= invalidate ? OBJPC_INVAL : 0;
944950f8459SAlan Cox 		VM_OBJECT_LOCK(object);
945*17f3095dSAlan Cox 		vm_object_page_clean(object, offset, offset + size, flags);
946950f8459SAlan Cox 		VM_OBJECT_UNLOCK(object);
94722db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
948ae51ff11SJeff Roberson 		VFS_UNLOCK_GIANT(vfslocked);
9493b582b4eSTor Egge 		vn_finished_write(mp);
950950f8459SAlan Cox 		VM_OBJECT_LOCK(object);
951950f8459SAlan Cox 	}
952950f8459SAlan Cox 	if ((object->type == OBJT_VNODE ||
953950f8459SAlan Cox 	     object->type == OBJT_DEVICE) && invalidate) {
954874f0135SDoug Rabson 		boolean_t purge;
955874f0135SDoug Rabson 		purge = old_msync || (object->type == OBJT_DEVICE);
956950f8459SAlan Cox 		vm_object_page_remove(object,
957950f8459SAlan Cox 		    OFF_TO_IDX(offset),
958950f8459SAlan Cox 		    OFF_TO_IDX(offset + size + PAGE_MASK),
959874f0135SDoug Rabson 		    purge ? FALSE : TRUE);
960950f8459SAlan Cox 	}
961950f8459SAlan Cox 	VM_OBJECT_UNLOCK(object);
962950f8459SAlan Cox }
963950f8459SAlan Cox 
964950f8459SAlan Cox /*
965867a482dSJohn Dyson  *	vm_object_madvise:
966867a482dSJohn Dyson  *
967867a482dSJohn Dyson  *	Implements the madvise function at the object/page level.
9681c7c3c6aSMatthew Dillon  *
969193b9358SAlan Cox  *	MADV_WILLNEED	(any object)
970193b9358SAlan Cox  *
971193b9358SAlan Cox  *	    Activate the specified pages if they are resident.
972193b9358SAlan Cox  *
973193b9358SAlan Cox  *	MADV_DONTNEED	(any object)
974193b9358SAlan Cox  *
975193b9358SAlan Cox  *	    Deactivate the specified pages if they are resident.
976193b9358SAlan Cox  *
977193b9358SAlan Cox  *	MADV_FREE	(OBJT_DEFAULT/OBJT_SWAP objects,
978193b9358SAlan Cox  *			 OBJ_ONEMAPPING only)
979193b9358SAlan Cox  *
980193b9358SAlan Cox  *	    Deactivate and clean the specified pages if they are
981193b9358SAlan Cox  *	    resident.  This permits the process to reuse the pages
982193b9358SAlan Cox  *	    without faulting or the kernel to reclaim the pages
983193b9358SAlan Cox  *	    without I/O.
984867a482dSJohn Dyson  */
985867a482dSJohn Dyson void
9861b40f8c0SMatthew Dillon vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
987867a482dSJohn Dyson {
9886e20a165SJohn Dyson 	vm_pindex_t end, tpindex;
98934567de7SAlan Cox 	vm_object_t backing_object, tobject;
990867a482dSJohn Dyson 	vm_page_t m;
991867a482dSJohn Dyson 
992867a482dSJohn Dyson 	if (object == NULL)
993867a482dSJohn Dyson 		return;
9949b98b796SAlan Cox 	VM_OBJECT_LOCK(object);
995867a482dSJohn Dyson 	end = pindex + count;
9961c7c3c6aSMatthew Dillon 	/*
9971c7c3c6aSMatthew Dillon 	 * Locate and adjust resident pages
9981c7c3c6aSMatthew Dillon 	 */
9991c7c3c6aSMatthew Dillon 	for (; pindex < end; pindex += 1) {
10006e20a165SJohn Dyson relookup:
10016e20a165SJohn Dyson 		tobject = object;
10026e20a165SJohn Dyson 		tpindex = pindex;
10036e20a165SJohn Dyson shadowlookup:
100458b4e6ccSAlan Cox 		/*
100558b4e6ccSAlan Cox 		 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
100658b4e6ccSAlan Cox 		 * and those pages must be OBJ_ONEMAPPING.
100758b4e6ccSAlan Cox 		 */
100858b4e6ccSAlan Cox 		if (advise == MADV_FREE) {
100958b4e6ccSAlan Cox 			if ((tobject->type != OBJT_DEFAULT &&
101058b4e6ccSAlan Cox 			     tobject->type != OBJT_SWAP) ||
101158b4e6ccSAlan Cox 			    (tobject->flags & OBJ_ONEMAPPING) == 0) {
101234567de7SAlan Cox 				goto unlock_tobject;
10136e20a165SJohn Dyson 			}
10146a2a3d73SAlan Cox 		} else if (tobject->type == OBJT_PHYS)
10156a2a3d73SAlan Cox 			goto unlock_tobject;
10161c7c3c6aSMatthew Dillon 		m = vm_page_lookup(tobject, tpindex);
10177bfda801SAlan Cox 		if (m == NULL && advise == MADV_WILLNEED) {
10187bfda801SAlan Cox 			/*
10197bfda801SAlan Cox 			 * If the page is cached, reactivate it.
10207bfda801SAlan Cox 			 */
1021f3a2ed4bSAlan Cox 			m = vm_page_alloc(tobject, tpindex, VM_ALLOC_IFCACHED |
1022f3a2ed4bSAlan Cox 			    VM_ALLOC_NOBUSY);
10237bfda801SAlan Cox 		}
10241c7c3c6aSMatthew Dillon 		if (m == NULL) {
10251ce137beSMatthew Dillon 			/*
10261ce137beSMatthew Dillon 			 * There may be swap even if there is no backing page
10271ce137beSMatthew Dillon 			 */
10281ce137beSMatthew Dillon 			if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
10291ce137beSMatthew Dillon 				swap_pager_freespace(tobject, tpindex, 1);
10301ce137beSMatthew Dillon 			/*
10311ce137beSMatthew Dillon 			 * next object
10321ce137beSMatthew Dillon 			 */
103334567de7SAlan Cox 			backing_object = tobject->backing_object;
103434567de7SAlan Cox 			if (backing_object == NULL)
103534567de7SAlan Cox 				goto unlock_tobject;
103634567de7SAlan Cox 			VM_OBJECT_LOCK(backing_object);
103756e0670fSAlan Cox 			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
10389b98b796SAlan Cox 			if (tobject != object)
103934567de7SAlan Cox 				VM_OBJECT_UNLOCK(tobject);
104034567de7SAlan Cox 			tobject = backing_object;
10416e20a165SJohn Dyson 			goto shadowlookup;
10426a2a3d73SAlan Cox 		} else if (m->valid != VM_PAGE_BITS_ALL)
10436a2a3d73SAlan Cox 			goto unlock_tobject;
1044867a482dSJohn Dyson 		/*
10456a2a3d73SAlan Cox 		 * If the page is not in a normal state, skip it.
1046867a482dSJohn Dyson 		 */
10472965a453SKip Macy 		vm_page_lock(m);
10486a2a3d73SAlan Cox 		if (m->hold_count != 0 || m->wire_count != 0) {
10492965a453SKip Macy 			vm_page_unlock(m);
105034567de7SAlan Cox 			goto unlock_tobject;
10516e20a165SJohn Dyson 		}
1052567e51e1SAlan Cox 		KASSERT((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0,
1053567e51e1SAlan Cox 		    ("vm_object_madvise: page %p is not managed", m));
10549af80719SAlan Cox 		if ((m->oflags & VPO_BUSY) || m->busy) {
1055567e51e1SAlan Cox 			if (advise == MADV_WILLNEED) {
1056b11b56b5SAlan Cox 				/*
1057b11b56b5SAlan Cox 				 * Reference the page before unlocking and
1058b11b56b5SAlan Cox 				 * sleeping so that the page daemon is less
1059b11b56b5SAlan Cox 				 * likely to reclaim it.
1060b11b56b5SAlan Cox 				 */
1061567e51e1SAlan Cox 				vm_page_lock_queues();
10625786be7cSAlan Cox 				vm_page_flag_set(m, PG_REFERENCED);
106391449ce9SAlan Cox 				vm_page_unlock_queues();
1064567e51e1SAlan Cox 			}
10652965a453SKip Macy 			vm_page_unlock(m);
10669b98b796SAlan Cox 			if (object != tobject)
10679b98b796SAlan Cox 				VM_OBJECT_UNLOCK(object);
10685786be7cSAlan Cox 			m->oflags |= VPO_WANTED;
1069b11b56b5SAlan Cox 			msleep(m, VM_OBJECT_MTX(tobject), PDROP | PVM, "madvpo",
1070b11b56b5SAlan Cox 			    0);
10719b98b796SAlan Cox 			VM_OBJECT_LOCK(object);
10726e20a165SJohn Dyson   			goto relookup;
107334567de7SAlan Cox 		}
1074867a482dSJohn Dyson 		if (advise == MADV_WILLNEED) {
1075867a482dSJohn Dyson 			vm_page_activate(m);
10766e20a165SJohn Dyson 		} else if (advise == MADV_DONTNEED) {
1077479112dfSMatthew Dillon 			vm_page_dontneed(m);
10780a47b48bSJohn Dyson 		} else if (advise == MADV_FREE) {
10791c7c3c6aSMatthew Dillon 			/*
10802aaeadf8SMatthew Dillon 			 * Mark the page clean.  This will allow the page
10812aaeadf8SMatthew Dillon 			 * to be freed up by the system.  However, such pages
10822aaeadf8SMatthew Dillon 			 * are often reused quickly by malloc()/free()
10832aaeadf8SMatthew Dillon 			 * so we do not do anything that would cause
10842aaeadf8SMatthew Dillon 			 * a page fault if we can help it.
10852aaeadf8SMatthew Dillon 			 *
10862aaeadf8SMatthew Dillon 			 * Specifically, we do not try to actually free
10872aaeadf8SMatthew Dillon 			 * the page now nor do we try to put it in the
10882aaeadf8SMatthew Dillon 			 * cache (which would cause a page fault on reuse).
108941c67e12SMatthew Dillon 			 *
109041c67e12SMatthew Dillon 			 * But we do make the page is freeable as we
109141c67e12SMatthew Dillon 			 * can without actually taking the step of unmapping
109241c67e12SMatthew Dillon 			 * it.
10931c7c3c6aSMatthew Dillon 			 */
10940385347cSPeter Wemm 			pmap_clear_modify(m);
10956e20a165SJohn Dyson 			m->dirty = 0;
109641c67e12SMatthew Dillon 			m->act_count = 0;
1097479112dfSMatthew Dillon 			vm_page_dontneed(m);
1098867a482dSJohn Dyson 		}
10992965a453SKip Macy 		vm_page_unlock(m);
11002999e9faSAlan Cox 		if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
11012999e9faSAlan Cox 			swap_pager_freespace(tobject, tpindex, 1);
110234567de7SAlan Cox unlock_tobject:
11039b98b796SAlan Cox 		if (tobject != object)
110434567de7SAlan Cox 			VM_OBJECT_UNLOCK(tobject);
1105867a482dSJohn Dyson 	}
11069b98b796SAlan Cox 	VM_OBJECT_UNLOCK(object);
1107867a482dSJohn Dyson }
1108867a482dSJohn Dyson 
1109867a482dSJohn Dyson /*
1110df8bae1dSRodney W. Grimes  *	vm_object_shadow:
1111df8bae1dSRodney W. Grimes  *
1112df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
1113df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
1114df8bae1dSRodney W. Grimes  *	object reference is deallocated.
1115df8bae1dSRodney W. Grimes  *
1116df8bae1dSRodney W. Grimes  *	The new object and offset into that object
1117df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
1118df8bae1dSRodney W. Grimes  */
111926f9a767SRodney W. Grimes void
11201b40f8c0SMatthew Dillon vm_object_shadow(
11211b40f8c0SMatthew Dillon 	vm_object_t *object,	/* IN/OUT */
11221b40f8c0SMatthew Dillon 	vm_ooffset_t *offset,	/* IN/OUT */
11231b40f8c0SMatthew Dillon 	vm_size_t length)
1124df8bae1dSRodney W. Grimes {
1125d031cff1SMatthew Dillon 	vm_object_t source;
1126d031cff1SMatthew Dillon 	vm_object_t result;
1127df8bae1dSRodney W. Grimes 
1128df8bae1dSRodney W. Grimes 	source = *object;
1129df8bae1dSRodney W. Grimes 
1130df8bae1dSRodney W. Grimes 	/*
11319a2f6362SAlan Cox 	 * Don't create the new object if the old object isn't shared.
11329a2f6362SAlan Cox 	 */
1133570a2f4aSAlan Cox 	if (source != NULL) {
1134570a2f4aSAlan Cox 		VM_OBJECT_LOCK(source);
1135570a2f4aSAlan Cox 		if (source->ref_count == 1 &&
11369a2f6362SAlan Cox 		    source->handle == NULL &&
11379a2f6362SAlan Cox 		    (source->type == OBJT_DEFAULT ||
11389917e010SAlan Cox 		     source->type == OBJT_SWAP)) {
1139570a2f4aSAlan Cox 			VM_OBJECT_UNLOCK(source);
11409a2f6362SAlan Cox 			return;
11419917e010SAlan Cox 		}
1142570a2f4aSAlan Cox 		VM_OBJECT_UNLOCK(source);
1143570a2f4aSAlan Cox 	}
11449a2f6362SAlan Cox 
11459a2f6362SAlan Cox 	/*
1146570a2f4aSAlan Cox 	 * Allocate a new object with the given length.
1147df8bae1dSRodney W. Grimes 	 */
11480cc74f14SAlan Cox 	result = vm_object_allocate(OBJT_DEFAULT, atop(length));
1149df8bae1dSRodney W. Grimes 
1150df8bae1dSRodney W. Grimes 	/*
11510d94caffSDavid Greenman 	 * The new object shadows the source object, adding a reference to it.
11520d94caffSDavid Greenman 	 * Our caller changes his reference to point to the new object,
11530d94caffSDavid Greenman 	 * removing a reference to the source object.  Net result: no change
11540d94caffSDavid Greenman 	 * of reference count.
11559b09fe24SMatthew Dillon 	 *
11569b09fe24SMatthew Dillon 	 * Try to optimize the result object's page color when shadowing
1157956f3135SPhilippe Charnier 	 * in order to maintain page coloring consistency in the combined
11589b09fe24SMatthew Dillon 	 * shadowed object.
1159df8bae1dSRodney W. Grimes 	 */
116024a1cce3SDavid Greenman 	result->backing_object = source;
11619174ca7bSTor Egge 	/*
11629174ca7bSTor Egge 	 * Store the offset into the source object, and fix up the offset into
11639174ca7bSTor Egge 	 * the new object.
11649174ca7bSTor Egge 	 */
11659174ca7bSTor Egge 	result->backing_object_offset = *offset;
1166570a2f4aSAlan Cox 	if (source != NULL) {
1167570a2f4aSAlan Cox 		VM_OBJECT_LOCK(source);
11681c500307SAlan Cox 		LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1169eaf13dd7SJohn Dyson 		source->shadow_count++;
1170f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
11717b54b1a9SAlan Cox 		result->flags |= source->flags & OBJ_COLORED;
1172f8a47341SAlan Cox 		result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) &
1173f8a47341SAlan Cox 		    ((1 << (VM_NFREEORDER - 1)) - 1);
1174f8a47341SAlan Cox #endif
1175570a2f4aSAlan Cox 		VM_OBJECT_UNLOCK(source);
1176de5f6a77SJohn Dyson 	}
1177df8bae1dSRodney W. Grimes 
1178df8bae1dSRodney W. Grimes 
1179df8bae1dSRodney W. Grimes 	/*
1180df8bae1dSRodney W. Grimes 	 * Return the new things
1181df8bae1dSRodney W. Grimes 	 */
1182df8bae1dSRodney W. Grimes 	*offset = 0;
1183df8bae1dSRodney W. Grimes 	*object = result;
1184df8bae1dSRodney W. Grimes }
1185df8bae1dSRodney W. Grimes 
1186c5aaa06dSAlan Cox /*
1187c5aaa06dSAlan Cox  *	vm_object_split:
1188c5aaa06dSAlan Cox  *
1189c5aaa06dSAlan Cox  * Split the pages in a map entry into a new object.  This affords
1190c5aaa06dSAlan Cox  * easier removal of unused pages, and keeps object inheritance from
1191c5aaa06dSAlan Cox  * being a negative impact on memory usage.
1192c5aaa06dSAlan Cox  */
1193c5aaa06dSAlan Cox void
1194c5aaa06dSAlan Cox vm_object_split(vm_map_entry_t entry)
1195c5aaa06dSAlan Cox {
119673000556SAlan Cox 	vm_page_t m, m_next;
1197c5aaa06dSAlan Cox 	vm_object_t orig_object, new_object, source;
119873000556SAlan Cox 	vm_pindex_t idx, offidxstart;
119973000556SAlan Cox 	vm_size_t size;
1200c5aaa06dSAlan Cox 
1201c5aaa06dSAlan Cox 	orig_object = entry->object.vm_object;
1202c5aaa06dSAlan Cox 	if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1203c5aaa06dSAlan Cox 		return;
1204c5aaa06dSAlan Cox 	if (orig_object->ref_count <= 1)
1205c5aaa06dSAlan Cox 		return;
12064da9f125SAlan Cox 	VM_OBJECT_UNLOCK(orig_object);
1207c5aaa06dSAlan Cox 
12084da9f125SAlan Cox 	offidxstart = OFF_TO_IDX(entry->offset);
120995442adfSAlan Cox 	size = atop(entry->end - entry->start);
1210c5aaa06dSAlan Cox 
12114da9f125SAlan Cox 	/*
12124da9f125SAlan Cox 	 * If swap_pager_copy() is later called, it will convert new_object
12134da9f125SAlan Cox 	 * into a swap object.
12144da9f125SAlan Cox 	 */
12154da9f125SAlan Cox 	new_object = vm_object_allocate(OBJT_DEFAULT, size);
1216c5aaa06dSAlan Cox 
1217c5474b8fSAlan Cox 	/*
1218c5474b8fSAlan Cox 	 * At this point, the new object is still private, so the order in
1219c5474b8fSAlan Cox 	 * which the original and new objects are locked does not matter.
1220c5474b8fSAlan Cox 	 */
122163f6cefcSAlan Cox 	VM_OBJECT_LOCK(new_object);
122263f6cefcSAlan Cox 	VM_OBJECT_LOCK(orig_object);
1223c5aaa06dSAlan Cox 	source = orig_object->backing_object;
1224c5aaa06dSAlan Cox 	if (source != NULL) {
12258e3a76fbSAlan Cox 		VM_OBJECT_LOCK(source);
122619c244d0SAlan Cox 		if ((source->flags & OBJ_DEAD) != 0) {
122719c244d0SAlan Cox 			VM_OBJECT_UNLOCK(source);
122819c244d0SAlan Cox 			VM_OBJECT_UNLOCK(orig_object);
122919c244d0SAlan Cox 			VM_OBJECT_UNLOCK(new_object);
123019c244d0SAlan Cox 			vm_object_deallocate(new_object);
123119c244d0SAlan Cox 			VM_OBJECT_LOCK(orig_object);
123219c244d0SAlan Cox 			return;
123319c244d0SAlan Cox 		}
12341c500307SAlan Cox 		LIST_INSERT_HEAD(&source->shadow_head,
1235c5aaa06dSAlan Cox 				  new_object, shadow_list);
12368e3a76fbSAlan Cox 		source->shadow_count++;
1237b921a12bSAlan Cox 		vm_object_reference_locked(source);	/* for new_object */
1238c5aaa06dSAlan Cox 		vm_object_clear_flag(source, OBJ_ONEMAPPING);
1239e2479b4fSAlan Cox 		VM_OBJECT_UNLOCK(source);
1240c5aaa06dSAlan Cox 		new_object->backing_object_offset =
12414da9f125SAlan Cox 			orig_object->backing_object_offset + entry->offset;
1242c5aaa06dSAlan Cox 		new_object->backing_object = source;
1243c5aaa06dSAlan Cox 	}
1244ef694c1aSEdward Tomasz Napierala 	if (orig_object->cred != NULL) {
1245ef694c1aSEdward Tomasz Napierala 		new_object->cred = orig_object->cred;
1246ef694c1aSEdward Tomasz Napierala 		crhold(orig_object->cred);
12473364c323SKonstantin Belousov 		new_object->charge = ptoa(size);
12483364c323SKonstantin Belousov 		KASSERT(orig_object->charge >= ptoa(size),
12493364c323SKonstantin Belousov 		    ("orig_object->charge < 0"));
12503364c323SKonstantin Belousov 		orig_object->charge -= ptoa(size);
12513364c323SKonstantin Belousov 	}
1252c5aaa06dSAlan Cox retry:
1253b382c10aSKonstantin Belousov 	m = vm_page_find_least(orig_object, offidxstart);
125473000556SAlan Cox 	for (; m != NULL && (idx = m->pindex - offidxstart) < size;
125573000556SAlan Cox 	    m = m_next) {
125673000556SAlan Cox 		m_next = TAILQ_NEXT(m, listq);
1257c5aaa06dSAlan Cox 
1258c5aaa06dSAlan Cox 		/*
1259c5aaa06dSAlan Cox 		 * We must wait for pending I/O to complete before we can
1260c5aaa06dSAlan Cox 		 * rename the page.
1261c5aaa06dSAlan Cox 		 *
1262c5aaa06dSAlan Cox 		 * We do not have to VM_PROT_NONE the page as mappings should
1263c5aaa06dSAlan Cox 		 * not be changed by this operation.
1264c5aaa06dSAlan Cox 		 */
12659af80719SAlan Cox 		if ((m->oflags & VPO_BUSY) || m->busy) {
1266de33beddSAlan Cox 			VM_OBJECT_UNLOCK(new_object);
12675786be7cSAlan Cox 			m->oflags |= VPO_WANTED;
1268c5474b8fSAlan Cox 			msleep(m, VM_OBJECT_MTX(orig_object), PVM, "spltwt", 0);
1269de33beddSAlan Cox 			VM_OBJECT_LOCK(new_object);
1270c5aaa06dSAlan Cox 			goto retry;
1271de33beddSAlan Cox 		}
12722965a453SKip Macy 		vm_page_lock(m);
1273c5aaa06dSAlan Cox 		vm_page_rename(m, new_object, idx);
12742965a453SKip Macy 		vm_page_unlock(m);
1275c5aaa06dSAlan Cox 		/* page automatically made dirty by rename and cache handled */
1276c5aaa06dSAlan Cox 		vm_page_busy(m);
1277c5aaa06dSAlan Cox 	}
1278d7a013c3SAlan Cox 	if (orig_object->type == OBJT_SWAP) {
1279c5aaa06dSAlan Cox 		/*
1280c7c8dd7eSAlan Cox 		 * swap_pager_copy() can sleep, in which case the orig_object's
1281c7c8dd7eSAlan Cox 		 * and new_object's locks are released and reacquired.
1282c5aaa06dSAlan Cox 		 */
1283c5aaa06dSAlan Cox 		swap_pager_copy(orig_object, new_object, offidxstart, 0);
12847bfda801SAlan Cox 
12857bfda801SAlan Cox 		/*
12867bfda801SAlan Cox 		 * Transfer any cached pages from orig_object to new_object.
12877bfda801SAlan Cox 		 */
12887bfda801SAlan Cox 		if (__predict_false(orig_object->cache != NULL))
12897bfda801SAlan Cox 			vm_page_cache_transfer(orig_object, offidxstart,
12907bfda801SAlan Cox 			    new_object);
1291c5aaa06dSAlan Cox 	}
1292d7a013c3SAlan Cox 	VM_OBJECT_UNLOCK(orig_object);
1293c7118ed6SAlan Cox 	TAILQ_FOREACH(m, &new_object->memq, listq)
1294c5aaa06dSAlan Cox 		vm_page_wakeup(m);
1295c7c8dd7eSAlan Cox 	VM_OBJECT_UNLOCK(new_object);
1296c5aaa06dSAlan Cox 	entry->object.vm_object = new_object;
1297c5aaa06dSAlan Cox 	entry->offset = 0LL;
1298c5aaa06dSAlan Cox 	vm_object_deallocate(orig_object);
12994da9f125SAlan Cox 	VM_OBJECT_LOCK(new_object);
1300c5aaa06dSAlan Cox }
1301c5aaa06dSAlan Cox 
13022ad1a3f7SMatthew Dillon #define	OBSC_TEST_ALL_SHADOWED	0x0001
13032ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_NOWAIT	0x0002
13042ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_WAIT	0x0004
13052ad1a3f7SMatthew Dillon 
1306b4ae4780SPoul-Henning Kamp static int
13072ad1a3f7SMatthew Dillon vm_object_backing_scan(vm_object_t object, int op)
13082ad1a3f7SMatthew Dillon {
13092ad1a3f7SMatthew Dillon 	int r = 1;
13102ad1a3f7SMatthew Dillon 	vm_page_t p;
13112ad1a3f7SMatthew Dillon 	vm_object_t backing_object;
13122ad1a3f7SMatthew Dillon 	vm_pindex_t backing_offset_index;
13132ad1a3f7SMatthew Dillon 
13147ca33ad1SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
13157ca33ad1SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object->backing_object, MA_OWNED);
13162ad1a3f7SMatthew Dillon 
13172ad1a3f7SMatthew Dillon 	backing_object = object->backing_object;
13182ad1a3f7SMatthew Dillon 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
13192ad1a3f7SMatthew Dillon 
13202ad1a3f7SMatthew Dillon 	/*
13212ad1a3f7SMatthew Dillon 	 * Initial conditions
13222ad1a3f7SMatthew Dillon 	 */
13232ad1a3f7SMatthew Dillon 	if (op & OBSC_TEST_ALL_SHADOWED) {
13242ad1a3f7SMatthew Dillon 		/*
13257bfda801SAlan Cox 		 * We do not want to have to test for the existence of cache
13267bfda801SAlan Cox 		 * or swap pages in the backing object.  XXX but with the
13272ad1a3f7SMatthew Dillon 		 * new swapper this would be pretty easy to do.
13282ad1a3f7SMatthew Dillon 		 *
13292ad1a3f7SMatthew Dillon 		 * XXX what about anonymous MAP_SHARED memory that hasn't
13302ad1a3f7SMatthew Dillon 		 * been ZFOD faulted yet?  If we do not test for this, the
13312ad1a3f7SMatthew Dillon 		 * shadow test may succeed! XXX
13322ad1a3f7SMatthew Dillon 		 */
13332ad1a3f7SMatthew Dillon 		if (backing_object->type != OBJT_DEFAULT) {
13342ad1a3f7SMatthew Dillon 			return (0);
13352ad1a3f7SMatthew Dillon 		}
13362ad1a3f7SMatthew Dillon 	}
13372ad1a3f7SMatthew Dillon 	if (op & OBSC_COLLAPSE_WAIT) {
13382ad1a3f7SMatthew Dillon 		vm_object_set_flag(backing_object, OBJ_DEAD);
13392ad1a3f7SMatthew Dillon 	}
13402ad1a3f7SMatthew Dillon 
13412ad1a3f7SMatthew Dillon 	/*
13422ad1a3f7SMatthew Dillon 	 * Our scan
13432ad1a3f7SMatthew Dillon 	 */
13442ad1a3f7SMatthew Dillon 	p = TAILQ_FIRST(&backing_object->memq);
13452ad1a3f7SMatthew Dillon 	while (p) {
13462ad1a3f7SMatthew Dillon 		vm_page_t next = TAILQ_NEXT(p, listq);
13472ad1a3f7SMatthew Dillon 		vm_pindex_t new_pindex = p->pindex - backing_offset_index;
13482ad1a3f7SMatthew Dillon 
13492ad1a3f7SMatthew Dillon 		if (op & OBSC_TEST_ALL_SHADOWED) {
13502ad1a3f7SMatthew Dillon 			vm_page_t pp;
13512ad1a3f7SMatthew Dillon 
13522ad1a3f7SMatthew Dillon 			/*
13532ad1a3f7SMatthew Dillon 			 * Ignore pages outside the parent object's range
13542ad1a3f7SMatthew Dillon 			 * and outside the parent object's mapping of the
13552ad1a3f7SMatthew Dillon 			 * backing object.
13562ad1a3f7SMatthew Dillon 			 *
13572ad1a3f7SMatthew Dillon 			 * note that we do not busy the backing object's
13582ad1a3f7SMatthew Dillon 			 * page.
13592ad1a3f7SMatthew Dillon 			 */
13602ad1a3f7SMatthew Dillon 			if (
13612ad1a3f7SMatthew Dillon 			    p->pindex < backing_offset_index ||
13622ad1a3f7SMatthew Dillon 			    new_pindex >= object->size
13632ad1a3f7SMatthew Dillon 			) {
13642ad1a3f7SMatthew Dillon 				p = next;
13652ad1a3f7SMatthew Dillon 				continue;
13662ad1a3f7SMatthew Dillon 			}
13672ad1a3f7SMatthew Dillon 
13682ad1a3f7SMatthew Dillon 			/*
13692ad1a3f7SMatthew Dillon 			 * See if the parent has the page or if the parent's
13702ad1a3f7SMatthew Dillon 			 * object pager has the page.  If the parent has the
13712ad1a3f7SMatthew Dillon 			 * page but the page is not valid, the parent's
13722ad1a3f7SMatthew Dillon 			 * object pager must have the page.
13732ad1a3f7SMatthew Dillon 			 *
13742ad1a3f7SMatthew Dillon 			 * If this fails, the parent does not completely shadow
13752ad1a3f7SMatthew Dillon 			 * the object and we might as well give up now.
13762ad1a3f7SMatthew Dillon 			 */
13772ad1a3f7SMatthew Dillon 
13782ad1a3f7SMatthew Dillon 			pp = vm_page_lookup(object, new_pindex);
13792ad1a3f7SMatthew Dillon 			if (
13802ad1a3f7SMatthew Dillon 			    (pp == NULL || pp->valid == 0) &&
13812ad1a3f7SMatthew Dillon 			    !vm_pager_has_page(object, new_pindex, NULL, NULL)
13822ad1a3f7SMatthew Dillon 			) {
13832ad1a3f7SMatthew Dillon 				r = 0;
13842ad1a3f7SMatthew Dillon 				break;
13852ad1a3f7SMatthew Dillon 			}
13862ad1a3f7SMatthew Dillon 		}
13872ad1a3f7SMatthew Dillon 
13882ad1a3f7SMatthew Dillon 		/*
13892ad1a3f7SMatthew Dillon 		 * Check for busy page
13902ad1a3f7SMatthew Dillon 		 */
13912ad1a3f7SMatthew Dillon 		if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
13922ad1a3f7SMatthew Dillon 			vm_page_t pp;
13932ad1a3f7SMatthew Dillon 
13942ad1a3f7SMatthew Dillon 			if (op & OBSC_COLLAPSE_NOWAIT) {
13959af80719SAlan Cox 				if ((p->oflags & VPO_BUSY) ||
13962ad1a3f7SMatthew Dillon 				    !p->valid ||
139700f9e8b4SAlan Cox 				    p->busy) {
13982ad1a3f7SMatthew Dillon 					p = next;
13992ad1a3f7SMatthew Dillon 					continue;
14002ad1a3f7SMatthew Dillon 				}
14012ad1a3f7SMatthew Dillon 			} else if (op & OBSC_COLLAPSE_WAIT) {
14029af80719SAlan Cox 				if ((p->oflags & VPO_BUSY) || p->busy) {
14037ca33ad1SAlan Cox 					VM_OBJECT_UNLOCK(object);
14045786be7cSAlan Cox 					p->oflags |= VPO_WANTED;
140591449ce9SAlan Cox 					msleep(p, VM_OBJECT_MTX(backing_object),
14067ca33ad1SAlan Cox 					    PDROP | PVM, "vmocol", 0);
14077ca33ad1SAlan Cox 					VM_OBJECT_LOCK(object);
14087ca33ad1SAlan Cox 					VM_OBJECT_LOCK(backing_object);
14092ad1a3f7SMatthew Dillon 					/*
14102ad1a3f7SMatthew Dillon 					 * If we slept, anything could have
14112ad1a3f7SMatthew Dillon 					 * happened.  Since the object is
14122ad1a3f7SMatthew Dillon 					 * marked dead, the backing offset
14132ad1a3f7SMatthew Dillon 					 * should not have changed so we
14142ad1a3f7SMatthew Dillon 					 * just restart our scan.
14152ad1a3f7SMatthew Dillon 					 */
14162ad1a3f7SMatthew Dillon 					p = TAILQ_FIRST(&backing_object->memq);
14172ad1a3f7SMatthew Dillon 					continue;
14182ad1a3f7SMatthew Dillon 				}
14192ad1a3f7SMatthew Dillon 			}
14202ad1a3f7SMatthew Dillon 
14212ad1a3f7SMatthew Dillon 			KASSERT(
14222ad1a3f7SMatthew Dillon 			    p->object == backing_object,
14238e99783bSAlan Cox 			    ("vm_object_backing_scan: object mismatch")
14242ad1a3f7SMatthew Dillon 			);
14252ad1a3f7SMatthew Dillon 
14262ad1a3f7SMatthew Dillon 			/*
14272ad1a3f7SMatthew Dillon 			 * Destroy any associated swap
14282ad1a3f7SMatthew Dillon 			 */
14292ad1a3f7SMatthew Dillon 			if (backing_object->type == OBJT_SWAP) {
14302ad1a3f7SMatthew Dillon 				swap_pager_freespace(
14312ad1a3f7SMatthew Dillon 				    backing_object,
14322ad1a3f7SMatthew Dillon 				    p->pindex,
14332ad1a3f7SMatthew Dillon 				    1
14342ad1a3f7SMatthew Dillon 				);
14352ad1a3f7SMatthew Dillon 			}
14362ad1a3f7SMatthew Dillon 
14372ad1a3f7SMatthew Dillon 			if (
14382ad1a3f7SMatthew Dillon 			    p->pindex < backing_offset_index ||
14392ad1a3f7SMatthew Dillon 			    new_pindex >= object->size
14402ad1a3f7SMatthew Dillon 			) {
14412ad1a3f7SMatthew Dillon 				/*
14422ad1a3f7SMatthew Dillon 				 * Page is out of the parent object's range, we
14432ad1a3f7SMatthew Dillon 				 * can simply destroy it.
14442ad1a3f7SMatthew Dillon 				 */
14452965a453SKip Macy 				vm_page_lock(p);
1446f6d89838SAlan Cox 				KASSERT(!pmap_page_is_mapped(p),
1447f6d89838SAlan Cox 				    ("freeing mapped page %p", p));
1448f6d89838SAlan Cox 				if (p->wire_count == 0)
14492ad1a3f7SMatthew Dillon 					vm_page_free(p);
1450f6d89838SAlan Cox 				else
1451f6d89838SAlan Cox 					vm_page_remove(p);
14522965a453SKip Macy 				vm_page_unlock(p);
14532ad1a3f7SMatthew Dillon 				p = next;
14542ad1a3f7SMatthew Dillon 				continue;
14552ad1a3f7SMatthew Dillon 			}
14562ad1a3f7SMatthew Dillon 
14572ad1a3f7SMatthew Dillon 			pp = vm_page_lookup(object, new_pindex);
14582ad1a3f7SMatthew Dillon 			if (
14592ad1a3f7SMatthew Dillon 			    pp != NULL ||
14602ad1a3f7SMatthew Dillon 			    vm_pager_has_page(object, new_pindex, NULL, NULL)
14612ad1a3f7SMatthew Dillon 			) {
14622ad1a3f7SMatthew Dillon 				/*
14632ad1a3f7SMatthew Dillon 				 * page already exists in parent OR swap exists
14642ad1a3f7SMatthew Dillon 				 * for this location in the parent.  Destroy
14652ad1a3f7SMatthew Dillon 				 * the original page from the backing object.
14662ad1a3f7SMatthew Dillon 				 *
14672ad1a3f7SMatthew Dillon 				 * Leave the parent's page alone
14682ad1a3f7SMatthew Dillon 				 */
14692965a453SKip Macy 				vm_page_lock(p);
1470f6d89838SAlan Cox 				KASSERT(!pmap_page_is_mapped(p),
1471f6d89838SAlan Cox 				    ("freeing mapped page %p", p));
1472f6d89838SAlan Cox 				if (p->wire_count == 0)
14732ad1a3f7SMatthew Dillon 					vm_page_free(p);
1474f6d89838SAlan Cox 				else
1475f6d89838SAlan Cox 					vm_page_remove(p);
14762965a453SKip Macy 				vm_page_unlock(p);
14772ad1a3f7SMatthew Dillon 				p = next;
14782ad1a3f7SMatthew Dillon 				continue;
14792ad1a3f7SMatthew Dillon 			}
14802ad1a3f7SMatthew Dillon 
1481f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
1482f8a47341SAlan Cox 			/*
1483f8a47341SAlan Cox 			 * Rename the reservation.
1484f8a47341SAlan Cox 			 */
1485f8a47341SAlan Cox 			vm_reserv_rename(p, object, backing_object,
1486f8a47341SAlan Cox 			    backing_offset_index);
1487f8a47341SAlan Cox #endif
1488f8a47341SAlan Cox 
14892ad1a3f7SMatthew Dillon 			/*
14902ad1a3f7SMatthew Dillon 			 * Page does not exist in parent, rename the
14912ad1a3f7SMatthew Dillon 			 * page from the backing object to the main object.
1492d1bf5d56SMatthew Dillon 			 *
1493d1bf5d56SMatthew Dillon 			 * If the page was mapped to a process, it can remain
1494d1bf5d56SMatthew Dillon 			 * mapped through the rename.
14952ad1a3f7SMatthew Dillon 			 */
14962965a453SKip Macy 			vm_page_lock(p);
14972ad1a3f7SMatthew Dillon 			vm_page_rename(p, object, new_pindex);
14982965a453SKip Macy 			vm_page_unlock(p);
14992ad1a3f7SMatthew Dillon 			/* page automatically made dirty by rename */
15002ad1a3f7SMatthew Dillon 		}
15012ad1a3f7SMatthew Dillon 		p = next;
15022ad1a3f7SMatthew Dillon 	}
15032ad1a3f7SMatthew Dillon 	return (r);
15042ad1a3f7SMatthew Dillon }
15052ad1a3f7SMatthew Dillon 
1506df8bae1dSRodney W. Grimes 
1507df8bae1dSRodney W. Grimes /*
15082fe6e4d7SDavid Greenman  * this version of collapse allows the operation to occur earlier and
15092fe6e4d7SDavid Greenman  * when paging_in_progress is true for an object...  This is not a complete
15102fe6e4d7SDavid Greenman  * operation, but should plug 99.9% of the rest of the leaks.
15112fe6e4d7SDavid Greenman  */
15122fe6e4d7SDavid Greenman static void
15131b40f8c0SMatthew Dillon vm_object_qcollapse(vm_object_t object)
15142fe6e4d7SDavid Greenman {
15152ad1a3f7SMatthew Dillon 	vm_object_t backing_object = object->backing_object;
15162fe6e4d7SDavid Greenman 
151706ecade7SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
151806ecade7SAlan Cox 	VM_OBJECT_LOCK_ASSERT(backing_object, MA_OWNED);
15191b40f8c0SMatthew Dillon 
15202fe6e4d7SDavid Greenman 	if (backing_object->ref_count != 1)
15212fe6e4d7SDavid Greenman 		return;
15222fe6e4d7SDavid Greenman 
15232ad1a3f7SMatthew Dillon 	vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
15242fe6e4d7SDavid Greenman }
15252fe6e4d7SDavid Greenman 
1526df8bae1dSRodney W. Grimes /*
1527df8bae1dSRodney W. Grimes  *	vm_object_collapse:
1528df8bae1dSRodney W. Grimes  *
1529df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
1530df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
1531df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
1532df8bae1dSRodney W. Grimes  */
153326f9a767SRodney W. Grimes void
15341b40f8c0SMatthew Dillon vm_object_collapse(vm_object_t object)
1535df8bae1dSRodney W. Grimes {
1536d7fc2210SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
153723955314SAlfred Perlstein 
1538df8bae1dSRodney W. Grimes 	while (TRUE) {
15392ad1a3f7SMatthew Dillon 		vm_object_t backing_object;
15402ad1a3f7SMatthew Dillon 
1541df8bae1dSRodney W. Grimes 		/*
1542df8bae1dSRodney W. Grimes 		 * Verify that the conditions are right for collapse:
1543df8bae1dSRodney W. Grimes 		 *
15442ad1a3f7SMatthew Dillon 		 * The object exists and the backing object exists.
1545df8bae1dSRodney W. Grimes 		 */
154624a1cce3SDavid Greenman 		if ((backing_object = object->backing_object) == NULL)
15472ad1a3f7SMatthew Dillon 			break;
1548df8bae1dSRodney W. Grimes 
1549f919ebdeSDavid Greenman 		/*
1550f919ebdeSDavid Greenman 		 * we check the backing object first, because it is most likely
155124a1cce3SDavid Greenman 		 * not collapsable.
1552f919ebdeSDavid Greenman 		 */
155340b808a8SAlan Cox 		VM_OBJECT_LOCK(backing_object);
155424a1cce3SDavid Greenman 		if (backing_object->handle != NULL ||
155524a1cce3SDavid Greenman 		    (backing_object->type != OBJT_DEFAULT &&
155624a1cce3SDavid Greenman 		     backing_object->type != OBJT_SWAP) ||
1557f919ebdeSDavid Greenman 		    (backing_object->flags & OBJ_DEAD) ||
155824a1cce3SDavid Greenman 		    object->handle != NULL ||
155924a1cce3SDavid Greenman 		    (object->type != OBJT_DEFAULT &&
156024a1cce3SDavid Greenman 		     object->type != OBJT_SWAP) ||
156124a1cce3SDavid Greenman 		    (object->flags & OBJ_DEAD)) {
156240b808a8SAlan Cox 			VM_OBJECT_UNLOCK(backing_object);
15632ad1a3f7SMatthew Dillon 			break;
156424a1cce3SDavid Greenman 		}
15659b4814bbSDavid Greenman 
15662ad1a3f7SMatthew Dillon 		if (
15672ad1a3f7SMatthew Dillon 		    object->paging_in_progress != 0 ||
15682ad1a3f7SMatthew Dillon 		    backing_object->paging_in_progress != 0
15692ad1a3f7SMatthew Dillon 		) {
1570b9921222SDavid Greenman 			vm_object_qcollapse(object);
157140b808a8SAlan Cox 			VM_OBJECT_UNLOCK(backing_object);
15722ad1a3f7SMatthew Dillon 			break;
1573df8bae1dSRodney W. Grimes 		}
157426f9a767SRodney W. Grimes 		/*
15750d94caffSDavid Greenman 		 * We know that we can either collapse the backing object (if
15762ad1a3f7SMatthew Dillon 		 * the parent is the only reference to it) or (perhaps) have
15772ad1a3f7SMatthew Dillon 		 * the parent bypass the object if the parent happens to shadow
15782ad1a3f7SMatthew Dillon 		 * all the resident pages in the entire backing object.
15792ad1a3f7SMatthew Dillon 		 *
15802ad1a3f7SMatthew Dillon 		 * This is ignoring pager-backed pages such as swap pages.
15812ad1a3f7SMatthew Dillon 		 * vm_object_backing_scan fails the shadowing test in this
15822ad1a3f7SMatthew Dillon 		 * case.
1583df8bae1dSRodney W. Grimes 		 */
1584df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
1585df8bae1dSRodney W. Grimes 			/*
15862ad1a3f7SMatthew Dillon 			 * If there is exactly one reference to the backing
15872ad1a3f7SMatthew Dillon 			 * object, we can collapse it into the parent.
1588df8bae1dSRodney W. Grimes 			 */
15892ad1a3f7SMatthew Dillon 			vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1590df8bae1dSRodney W. Grimes 
1591f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
1592f8a47341SAlan Cox 			/*
1593f8a47341SAlan Cox 			 * Break any reservations from backing_object.
1594f8a47341SAlan Cox 			 */
1595f8a47341SAlan Cox 			if (__predict_false(!LIST_EMPTY(&backing_object->rvq)))
1596f8a47341SAlan Cox 				vm_reserv_break_all(backing_object);
1597f8a47341SAlan Cox #endif
1598f8a47341SAlan Cox 
1599df8bae1dSRodney W. Grimes 			/*
1600df8bae1dSRodney W. Grimes 			 * Move the pager from backing_object to object.
1601df8bae1dSRodney W. Grimes 			 */
16026be36525SAlan Cox 			if (backing_object->type == OBJT_SWAP) {
160324a1cce3SDavid Greenman 				/*
1604c7c8dd7eSAlan Cox 				 * swap_pager_copy() can sleep, in which case
1605c7c8dd7eSAlan Cox 				 * the backing_object's and object's locks are
1606c7c8dd7eSAlan Cox 				 * released and reacquired.
160724a1cce3SDavid Greenman 				 */
16081c7c3c6aSMatthew Dillon 				swap_pager_copy(
16091c7c3c6aSMatthew Dillon 				    backing_object,
16101c7c3c6aSMatthew Dillon 				    object,
16111c7c3c6aSMatthew Dillon 				    OFF_TO_IDX(object->backing_object_offset), TRUE);
16127bfda801SAlan Cox 
16137bfda801SAlan Cox 				/*
16147bfda801SAlan Cox 				 * Free any cached pages from backing_object.
16157bfda801SAlan Cox 				 */
16167bfda801SAlan Cox 				if (__predict_false(backing_object->cache != NULL))
1617c9444914SAlan Cox 					vm_page_cache_free(backing_object, 0, 0);
1618c0503609SDavid Greenman 			}
1619df8bae1dSRodney W. Grimes 			/*
1620df8bae1dSRodney W. Grimes 			 * Object now shadows whatever backing_object did.
16212ad1a3f7SMatthew Dillon 			 * Note that the reference to
16222ad1a3f7SMatthew Dillon 			 * backing_object->backing_object moves from within
16232ad1a3f7SMatthew Dillon 			 * backing_object to within object.
1624df8bae1dSRodney W. Grimes 			 */
16251c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
16264f7c7f6eSAlan Cox 			backing_object->shadow_count--;
1627de5f6a77SJohn Dyson 			if (backing_object->backing_object) {
16286be36525SAlan Cox 				VM_OBJECT_LOCK(backing_object->backing_object);
16291c500307SAlan Cox 				LIST_REMOVE(backing_object, shadow_list);
163043186e53SAlan Cox 				LIST_INSERT_HEAD(
163143186e53SAlan Cox 				    &backing_object->backing_object->shadow_head,
163243186e53SAlan Cox 				    object, shadow_list);
163343186e53SAlan Cox 				/*
163443186e53SAlan Cox 				 * The shadow_count has not changed.
163543186e53SAlan Cox 				 */
16366be36525SAlan Cox 				VM_OBJECT_UNLOCK(backing_object->backing_object);
1637de5f6a77SJohn Dyson 			}
163824a1cce3SDavid Greenman 			object->backing_object = backing_object->backing_object;
16392ad1a3f7SMatthew Dillon 			object->backing_object_offset +=
16402ad1a3f7SMatthew Dillon 			    backing_object->backing_object_offset;
16412ad1a3f7SMatthew Dillon 
1642df8bae1dSRodney W. Grimes 			/*
1643df8bae1dSRodney W. Grimes 			 * Discard backing_object.
1644df8bae1dSRodney W. Grimes 			 *
16450d94caffSDavid Greenman 			 * Since the backing object has no pages, no pager left,
16460d94caffSDavid Greenman 			 * and no object references within it, all that is
16470d94caffSDavid Greenman 			 * necessary is to dispose of it.
1648df8bae1dSRodney W. Grimes 			 */
16499b4d473aSKonstantin Belousov 			KASSERT(backing_object->ref_count == 1, (
16509b4d473aSKonstantin Belousov "backing_object %p was somehow re-referenced during collapse!",
16519b4d473aSKonstantin Belousov 			    backing_object));
16526be36525SAlan Cox 			VM_OBJECT_UNLOCK(backing_object);
16539b4d473aSKonstantin Belousov 			vm_object_destroy(backing_object);
1654df8bae1dSRodney W. Grimes 
1655df8bae1dSRodney W. Grimes 			object_collapses++;
16560d94caffSDavid Greenman 		} else {
165795e5e988SJohn Dyson 			vm_object_t new_backing_object;
1658df8bae1dSRodney W. Grimes 
1659df8bae1dSRodney W. Grimes 			/*
16602ad1a3f7SMatthew Dillon 			 * If we do not entirely shadow the backing object,
16612ad1a3f7SMatthew Dillon 			 * there is nothing we can do so we give up.
1662df8bae1dSRodney W. Grimes 			 */
1663df59a0feSJeff Roberson 			if (object->resident_page_count != object->size &&
1664df59a0feSJeff Roberson 			    vm_object_backing_scan(object,
1665df59a0feSJeff Roberson 			    OBSC_TEST_ALL_SHADOWED) == 0) {
166640b808a8SAlan Cox 				VM_OBJECT_UNLOCK(backing_object);
16672ad1a3f7SMatthew Dillon 				break;
166824a1cce3SDavid Greenman 			}
1669df8bae1dSRodney W. Grimes 
1670df8bae1dSRodney W. Grimes 			/*
16710d94caffSDavid Greenman 			 * Make the parent shadow the next object in the
16720d94caffSDavid Greenman 			 * chain.  Deallocating backing_object will not remove
16730d94caffSDavid Greenman 			 * it, since its reference count is at least 2.
1674df8bae1dSRodney W. Grimes 			 */
16751c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
1676eaf13dd7SJohn Dyson 			backing_object->shadow_count--;
167795e5e988SJohn Dyson 
167895e5e988SJohn Dyson 			new_backing_object = backing_object->backing_object;
16798aef1712SMatthew Dillon 			if ((object->backing_object = new_backing_object) != NULL) {
16806be36525SAlan Cox 				VM_OBJECT_LOCK(new_backing_object);
16811c500307SAlan Cox 				LIST_INSERT_HEAD(
16822ad1a3f7SMatthew Dillon 				    &new_backing_object->shadow_head,
16832ad1a3f7SMatthew Dillon 				    object,
16842ad1a3f7SMatthew Dillon 				    shadow_list
16852ad1a3f7SMatthew Dillon 				);
1686eaf13dd7SJohn Dyson 				new_backing_object->shadow_count++;
1687b921a12bSAlan Cox 				vm_object_reference_locked(new_backing_object);
16886be36525SAlan Cox 				VM_OBJECT_UNLOCK(new_backing_object);
168995e5e988SJohn Dyson 				object->backing_object_offset +=
169095e5e988SJohn Dyson 					backing_object->backing_object_offset;
1691de5f6a77SJohn Dyson 			}
1692df8bae1dSRodney W. Grimes 
1693df8bae1dSRodney W. Grimes 			/*
16940d94caffSDavid Greenman 			 * Drop the reference count on backing_object. Since
169522ec553fSAlan Cox 			 * its ref_count was at least 2, it will not vanish.
1696df8bae1dSRodney W. Grimes 			 */
169722ec553fSAlan Cox 			backing_object->ref_count--;
169822ec553fSAlan Cox 			VM_OBJECT_UNLOCK(backing_object);
1699df8bae1dSRodney W. Grimes 			object_bypasses++;
1700df8bae1dSRodney W. Grimes 		}
1701df8bae1dSRodney W. Grimes 
1702df8bae1dSRodney W. Grimes 		/*
1703df8bae1dSRodney W. Grimes 		 * Try again with this object's new backing object.
1704df8bae1dSRodney W. Grimes 		 */
1705df8bae1dSRodney W. Grimes 	}
1706df8bae1dSRodney W. Grimes }
1707df8bae1dSRodney W. Grimes 
1708df8bae1dSRodney W. Grimes /*
1709bff99f0dSAlan Cox  *	vm_object_page_remove:
1710df8bae1dSRodney W. Grimes  *
171168855966SAlan Cox  *	For the given object, either frees or invalidates each of the
171268855966SAlan Cox  *	specified pages.  In general, a page is freed.  However, if a
171368855966SAlan Cox  *	page is wired for any reason other than the existence of a
171468855966SAlan Cox  *	managed, wired mapping, then it may be invalidated but not
171568855966SAlan Cox  *	removed from the object.  Pages are specified by the given
171668855966SAlan Cox  *	range ["start", "end") and Boolean "clean_only".  As a
171768855966SAlan Cox  *	special case, if "end" is zero, then the range extends from
171868855966SAlan Cox  *	"start" to the end of the object.  If "clean_only" is TRUE,
171968855966SAlan Cox  *	then only the non-dirty pages within the specified range are
172068855966SAlan Cox  *	affected.
172168855966SAlan Cox  *
172268855966SAlan Cox  *	In general, this operation should only be performed on objects
172368855966SAlan Cox  *	that contain managed pages.  There are two exceptions.  First,
172468855966SAlan Cox  *	it may be performed on the kernel and kmem objects.  Second,
172568855966SAlan Cox  *	it may be used by msync(..., MS_INVALIDATE) to invalidate
1726a1a95cd6SAlan Cox  *	device-backed pages.  In both of these cases, "clean_only"
1727a1a95cd6SAlan Cox  *	must be FALSE.
1728df8bae1dSRodney W. Grimes  *
1729df8bae1dSRodney W. Grimes  *	The object must be locked.
1730df8bae1dSRodney W. Grimes  */
173126f9a767SRodney W. Grimes void
1732ecde4b32SAlan Cox vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
1733ecde4b32SAlan Cox     boolean_t clean_only)
1734df8bae1dSRodney W. Grimes {
1735d031cff1SMatthew Dillon 	vm_page_t p, next;
173659677d3cSAlan Cox 	int wirings;
1737df8bae1dSRodney W. Grimes 
1738ecde4b32SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1739ecde4b32SAlan Cox 	if (object->resident_page_count == 0)
174025732691SAlan Cox 		goto skipmemq;
174195e5e988SJohn Dyson 
17428b03c8edSMatthew Dillon 	/*
17438b03c8edSMatthew Dillon 	 * Since physically-backed objects do not use managed pages, we can't
17448b03c8edSMatthew Dillon 	 * remove pages from the object (we must instead remove the page
17458b03c8edSMatthew Dillon 	 * references, and then destroy the object).
17468b03c8edSMatthew Dillon 	 */
17479f5c801bSAlan Cox 	KASSERT(object->type != OBJT_PHYS || object == kernel_object ||
17489f5c801bSAlan Cox 	    object == kmem_object,
1749ecde4b32SAlan Cox 	    ("attempt to remove pages from a physical object"));
17508b03c8edSMatthew Dillon 
1751d474eaaaSDoug Rabson 	vm_object_pip_add(object, 1);
175226f9a767SRodney W. Grimes again:
1753b382c10aSKonstantin Belousov 	p = vm_page_find_least(object, start);
17542965a453SKip Macy 
175575741c04SAlan Cox 	/*
175675741c04SAlan Cox 	 * Assert: the variable p is either (1) the page with the
175775741c04SAlan Cox 	 * least pindex greater than or equal to the parameter pindex
175875741c04SAlan Cox 	 * or (2) NULL.
175975741c04SAlan Cox 	 */
176075741c04SAlan Cox 	for (;
1761bff99f0dSAlan Cox 	     p != NULL && (p->pindex < end || end == 0);
176275741c04SAlan Cox 	     p = next) {
1763b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
176475741c04SAlan Cox 
176559677d3cSAlan Cox 		/*
176659677d3cSAlan Cox 		 * If the page is wired for any reason besides the
176759677d3cSAlan Cox 		 * existence of managed, wired mappings, then it cannot
176868855966SAlan Cox 		 * be freed.  For example, fictitious pages, which
176968855966SAlan Cox 		 * represent device memory, are inherently wired and
177068855966SAlan Cox 		 * cannot be freed.  They can, however, be invalidated
177168855966SAlan Cox 		 * if "clean_only" is FALSE.
177259677d3cSAlan Cox 		 */
17732965a453SKip Macy 		vm_page_lock(p);
177459677d3cSAlan Cox 		if ((wirings = p->wire_count) != 0 &&
177559677d3cSAlan Cox 		    (wirings = pmap_page_wired_mappings(p)) != p->wire_count) {
177668855966SAlan Cox 			/* Fictitious pages do not have managed mappings. */
177768855966SAlan Cox 			if ((p->flags & PG_FICTITIOUS) == 0)
17784fec79beSAlan Cox 				pmap_remove_all(p);
177959677d3cSAlan Cox 			/* Account for removal of managed, wired mappings. */
178059677d3cSAlan Cox 			p->wire_count -= wirings;
1781a28042d1SAlan Cox 			if (!clean_only) {
1782bd7e5f99SJohn Dyson 				p->valid = 0;
1783a28042d1SAlan Cox 				vm_page_undirty(p);
1784a28042d1SAlan Cox 			}
17852965a453SKip Macy 			vm_page_unlock(p);
17860d94caffSDavid Greenman 			continue;
17870d94caffSDavid Greenman 		}
178832585dd6SAlan Cox 		if (vm_page_sleep_if_busy(p, TRUE, "vmopar"))
178926f9a767SRodney W. Grimes 			goto again;
179068855966SAlan Cox 		KASSERT((p->flags & PG_FICTITIOUS) == 0,
179168855966SAlan Cox 		    ("vm_object_page_remove: page %p is fictitious", p));
17928f9110f6SJohn Dyson 		if (clean_only && p->valid) {
179378985e42SAlan Cox 			pmap_remove_write(p);
17942965a453SKip Macy 			if (p->dirty) {
17952965a453SKip Macy 				vm_page_unlock(p);
17967c1f6cedSDavid Greenman 				continue;
17977c1f6cedSDavid Greenman 			}
17982965a453SKip Macy 		}
17994fec79beSAlan Cox 		pmap_remove_all(p);
180059677d3cSAlan Cox 		/* Account for removal of managed, wired mappings. */
180159677d3cSAlan Cox 		if (wirings != 0)
180259677d3cSAlan Cox 			p->wire_count -= wirings;
1803df8bae1dSRodney W. Grimes 		vm_page_free(p);
18042965a453SKip Macy 		vm_page_unlock(p);
18052965a453SKip Macy 	}
1806f919ebdeSDavid Greenman 	vm_object_pip_wakeup(object);
180725732691SAlan Cox skipmemq:
1808c9444914SAlan Cox 	if (__predict_false(object->cache != NULL))
1809c9444914SAlan Cox 		vm_page_cache_free(object, start, end);
1810c0503609SDavid Greenman }
1811df8bae1dSRodney W. Grimes 
1812df8bae1dSRodney W. Grimes /*
1813387aabc5SAlan Cox  *	Populate the specified range of the object with valid pages.  Returns
1814387aabc5SAlan Cox  *	TRUE if the range is successfully populated and FALSE otherwise.
1815387aabc5SAlan Cox  *
1816387aabc5SAlan Cox  *	Note: This function should be optimized to pass a larger array of
1817387aabc5SAlan Cox  *	pages to vm_pager_get_pages() before it is applied to a non-
1818387aabc5SAlan Cox  *	OBJT_DEVICE object.
1819387aabc5SAlan Cox  *
1820387aabc5SAlan Cox  *	The object must be locked.
1821387aabc5SAlan Cox  */
1822387aabc5SAlan Cox boolean_t
1823387aabc5SAlan Cox vm_object_populate(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1824387aabc5SAlan Cox {
1825387aabc5SAlan Cox 	vm_page_t m, ma[1];
1826387aabc5SAlan Cox 	vm_pindex_t pindex;
1827387aabc5SAlan Cox 	int rv;
1828387aabc5SAlan Cox 
1829387aabc5SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1830387aabc5SAlan Cox 	for (pindex = start; pindex < end; pindex++) {
1831387aabc5SAlan Cox 		m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL |
1832387aabc5SAlan Cox 		    VM_ALLOC_RETRY);
1833387aabc5SAlan Cox 		if (m->valid != VM_PAGE_BITS_ALL) {
1834387aabc5SAlan Cox 			ma[0] = m;
1835387aabc5SAlan Cox 			rv = vm_pager_get_pages(object, ma, 1, 0);
1836387aabc5SAlan Cox 			m = vm_page_lookup(object, pindex);
1837387aabc5SAlan Cox 			if (m == NULL)
1838387aabc5SAlan Cox 				break;
1839387aabc5SAlan Cox 			if (rv != VM_PAGER_OK) {
18402965a453SKip Macy 				vm_page_lock(m);
1841387aabc5SAlan Cox 				vm_page_free(m);
18422965a453SKip Macy 				vm_page_unlock(m);
1843387aabc5SAlan Cox 				break;
1844387aabc5SAlan Cox 			}
1845387aabc5SAlan Cox 		}
1846387aabc5SAlan Cox 		/*
1847387aabc5SAlan Cox 		 * Keep "m" busy because a subsequent iteration may unlock
1848387aabc5SAlan Cox 		 * the object.
1849387aabc5SAlan Cox 		 */
1850387aabc5SAlan Cox 	}
1851387aabc5SAlan Cox 	if (pindex > start) {
1852387aabc5SAlan Cox 		m = vm_page_lookup(object, start);
1853387aabc5SAlan Cox 		while (m != NULL && m->pindex < pindex) {
1854387aabc5SAlan Cox 			vm_page_wakeup(m);
1855387aabc5SAlan Cox 			m = TAILQ_NEXT(m, listq);
1856387aabc5SAlan Cox 		}
1857387aabc5SAlan Cox 	}
1858387aabc5SAlan Cox 	return (pindex == end);
1859387aabc5SAlan Cox }
1860387aabc5SAlan Cox 
1861387aabc5SAlan Cox /*
1862df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
1863df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
1864df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
1865df8bae1dSRodney W. Grimes  *
1866df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
1867df8bae1dSRodney W. Grimes  *
1868df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
1869df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
1870df8bae1dSRodney W. Grimes  *
1871df8bae1dSRodney W. Grimes  *	Parameters:
1872df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
1873df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
1874df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
187557a21abaSAlan Cox  *		next_size	Size of reference to the second object
18763364c323SKonstantin Belousov  *		reserved	Indicator that extension region has
18773364c323SKonstantin Belousov  *				swap accounted for
1878df8bae1dSRodney W. Grimes  *
1879df8bae1dSRodney W. Grimes  *	Conditions:
1880df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
1881df8bae1dSRodney W. Grimes  */
18820d94caffSDavid Greenman boolean_t
188357a21abaSAlan Cox vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset,
18843364c323SKonstantin Belousov     vm_size_t prev_size, vm_size_t next_size, boolean_t reserved)
1885df8bae1dSRodney W. Grimes {
1886ea41812fSAlan Cox 	vm_pindex_t next_pindex;
1887df8bae1dSRodney W. Grimes 
188800e1854aSAlan Cox 	if (prev_object == NULL)
1889df8bae1dSRodney W. Grimes 		return (TRUE);
1890bdbfbaafSAlan Cox 	VM_OBJECT_LOCK(prev_object);
18914112823fSMatthew Dillon 	if (prev_object->type != OBJT_DEFAULT &&
18924112823fSMatthew Dillon 	    prev_object->type != OBJT_SWAP) {
1893bdbfbaafSAlan Cox 		VM_OBJECT_UNLOCK(prev_object);
189430dcfc09SJohn Dyson 		return (FALSE);
189530dcfc09SJohn Dyson 	}
189630dcfc09SJohn Dyson 
1897df8bae1dSRodney W. Grimes 	/*
1898df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
1899df8bae1dSRodney W. Grimes 	 */
1900df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
1901df8bae1dSRodney W. Grimes 
1902df8bae1dSRodney W. Grimes 	/*
19030d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
19040d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
19050d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
1906df8bae1dSRodney W. Grimes 	 */
19078cc7e047SJohn Dyson 	if (prev_object->backing_object != NULL) {
1908bdbfbaafSAlan Cox 		VM_OBJECT_UNLOCK(prev_object);
1909df8bae1dSRodney W. Grimes 		return (FALSE);
1910df8bae1dSRodney W. Grimes 	}
1911a316d390SJohn Dyson 
1912a316d390SJohn Dyson 	prev_size >>= PAGE_SHIFT;
1913a316d390SJohn Dyson 	next_size >>= PAGE_SHIFT;
191457a21abaSAlan Cox 	next_pindex = OFF_TO_IDX(prev_offset) + prev_size;
19158cc7e047SJohn Dyson 
19168cc7e047SJohn Dyson 	if ((prev_object->ref_count > 1) &&
1917ea41812fSAlan Cox 	    (prev_object->size != next_pindex)) {
1918bdbfbaafSAlan Cox 		VM_OBJECT_UNLOCK(prev_object);
19198cc7e047SJohn Dyson 		return (FALSE);
19208cc7e047SJohn Dyson 	}
19218cc7e047SJohn Dyson 
1922df8bae1dSRodney W. Grimes 	/*
19233364c323SKonstantin Belousov 	 * Account for the charge.
19243364c323SKonstantin Belousov 	 */
1925ef694c1aSEdward Tomasz Napierala 	if (prev_object->cred != NULL) {
19263364c323SKonstantin Belousov 
19273364c323SKonstantin Belousov 		/*
19283364c323SKonstantin Belousov 		 * If prev_object was charged, then this mapping,
19293364c323SKonstantin Belousov 		 * althought not charged now, may become writable
1930ef694c1aSEdward Tomasz Napierala 		 * later. Non-NULL cred in the object would prevent
19313364c323SKonstantin Belousov 		 * swap reservation during enabling of the write
19323364c323SKonstantin Belousov 		 * access, so reserve swap now. Failed reservation
19333364c323SKonstantin Belousov 		 * cause allocation of the separate object for the map
19343364c323SKonstantin Belousov 		 * entry, and swap reservation for this entry is
19353364c323SKonstantin Belousov 		 * managed in appropriate time.
19363364c323SKonstantin Belousov 		 */
1937ef694c1aSEdward Tomasz Napierala 		if (!reserved && !swap_reserve_by_cred(ptoa(next_size),
1938ef694c1aSEdward Tomasz Napierala 		    prev_object->cred)) {
19393364c323SKonstantin Belousov 			return (FALSE);
19403364c323SKonstantin Belousov 		}
19413364c323SKonstantin Belousov 		prev_object->charge += ptoa(next_size);
19423364c323SKonstantin Belousov 	}
19433364c323SKonstantin Belousov 
19443364c323SKonstantin Belousov 	/*
19450d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
19460d94caffSDavid Greenman 	 * deallocation.
1947df8bae1dSRodney W. Grimes 	 */
1948ea41812fSAlan Cox 	if (next_pindex < prev_object->size) {
1949df8bae1dSRodney W. Grimes 		vm_object_page_remove(prev_object,
1950ea41812fSAlan Cox 				      next_pindex,
1951ea41812fSAlan Cox 				      next_pindex + next_size, FALSE);
1952ea41812fSAlan Cox 		if (prev_object->type == OBJT_SWAP)
1953ea41812fSAlan Cox 			swap_pager_freespace(prev_object,
1954ea41812fSAlan Cox 					     next_pindex, next_size);
19553364c323SKonstantin Belousov #if 0
1956ef694c1aSEdward Tomasz Napierala 		if (prev_object->cred != NULL) {
19573364c323SKonstantin Belousov 			KASSERT(prev_object->charge >=
19583364c323SKonstantin Belousov 			    ptoa(prev_object->size - next_pindex),
19593364c323SKonstantin Belousov 			    ("object %p overcharged 1 %jx %jx", prev_object,
19603364c323SKonstantin Belousov 				(uintmax_t)next_pindex, (uintmax_t)next_size));
19613364c323SKonstantin Belousov 			prev_object->charge -= ptoa(prev_object->size -
19623364c323SKonstantin Belousov 			    next_pindex);
19633364c323SKonstantin Belousov 		}
19643364c323SKonstantin Belousov #endif
1965ea41812fSAlan Cox 	}
1966df8bae1dSRodney W. Grimes 
1967df8bae1dSRodney W. Grimes 	/*
1968df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
1969df8bae1dSRodney W. Grimes 	 */
1970ea41812fSAlan Cox 	if (next_pindex + next_size > prev_object->size)
1971ea41812fSAlan Cox 		prev_object->size = next_pindex + next_size;
1972df8bae1dSRodney W. Grimes 
1973bdbfbaafSAlan Cox 	VM_OBJECT_UNLOCK(prev_object);
1974df8bae1dSRodney W. Grimes 	return (TRUE);
1975df8bae1dSRodney W. Grimes }
1976df8bae1dSRodney W. Grimes 
19777a5a6352SMatthew Dillon void
19787a5a6352SMatthew Dillon vm_object_set_writeable_dirty(vm_object_t object)
19797a5a6352SMatthew Dillon {
19807a5a6352SMatthew Dillon 
1981de33beddSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
19823280870dSKonstantin Belousov 	if (object->type != OBJT_VNODE)
19833280870dSKonstantin Belousov 		return;
19843280870dSKonstantin Belousov 	object->generation++;
19853280870dSKonstantin Belousov 	if ((object->flags & OBJ_MIGHTBEDIRTY) != 0)
1986ee39666aSJeff Roberson 		return;
1987af51d7bfSAlan Cox 	vm_object_set_flag(object, OBJ_MIGHTBEDIRTY);
19887a5a6352SMatthew Dillon }
19897a5a6352SMatthew Dillon 
1990c7c34a24SBruce Evans #include "opt_ddb.h"
1991c3cb3e12SDavid Greenman #ifdef DDB
1992c7c34a24SBruce Evans #include <sys/kernel.h>
1993c7c34a24SBruce Evans 
1994ce9edcf5SPoul-Henning Kamp #include <sys/cons.h>
1995c7c34a24SBruce Evans 
1996c7c34a24SBruce Evans #include <ddb/ddb.h>
1997c7c34a24SBruce Evans 
1998cac597e4SBruce Evans static int
19991b40f8c0SMatthew Dillon _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
2000a1f6d91cSDavid Greenman {
2001a1f6d91cSDavid Greenman 	vm_map_t tmpm;
2002a1f6d91cSDavid Greenman 	vm_map_entry_t tmpe;
2003a1f6d91cSDavid Greenman 	vm_object_t obj;
2004a1f6d91cSDavid Greenman 	int entcount;
2005a1f6d91cSDavid Greenman 
2006a1f6d91cSDavid Greenman 	if (map == 0)
2007a1f6d91cSDavid Greenman 		return 0;
2008a1f6d91cSDavid Greenman 
2009a1f6d91cSDavid Greenman 	if (entry == 0) {
2010a1f6d91cSDavid Greenman 		tmpe = map->header.next;
2011a1f6d91cSDavid Greenman 		entcount = map->nentries;
2012a1f6d91cSDavid Greenman 		while (entcount-- && (tmpe != &map->header)) {
2013a1f6d91cSDavid Greenman 			if (_vm_object_in_map(map, object, tmpe)) {
2014a1f6d91cSDavid Greenman 				return 1;
2015a1f6d91cSDavid Greenman 			}
2016a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
2017a1f6d91cSDavid Greenman 		}
20189fdfe602SMatthew Dillon 	} else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
20199fdfe602SMatthew Dillon 		tmpm = entry->object.sub_map;
2020a1f6d91cSDavid Greenman 		tmpe = tmpm->header.next;
2021a1f6d91cSDavid Greenman 		entcount = tmpm->nentries;
2022a1f6d91cSDavid Greenman 		while (entcount-- && tmpe != &tmpm->header) {
2023a1f6d91cSDavid Greenman 			if (_vm_object_in_map(tmpm, object, tmpe)) {
2024a1f6d91cSDavid Greenman 				return 1;
2025a1f6d91cSDavid Greenman 			}
2026a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
2027a1f6d91cSDavid Greenman 		}
20288aef1712SMatthew Dillon 	} else if ((obj = entry->object.vm_object) != NULL) {
202924a1cce3SDavid Greenman 		for (; obj; obj = obj->backing_object)
2030a1f6d91cSDavid Greenman 			if (obj == object) {
2031a1f6d91cSDavid Greenman 				return 1;
2032a1f6d91cSDavid Greenman 			}
2033a1f6d91cSDavid Greenman 	}
2034a1f6d91cSDavid Greenman 	return 0;
2035a1f6d91cSDavid Greenman }
2036a1f6d91cSDavid Greenman 
2037cac597e4SBruce Evans static int
20381b40f8c0SMatthew Dillon vm_object_in_map(vm_object_t object)
2039a1f6d91cSDavid Greenman {
2040a1f6d91cSDavid Greenman 	struct proc *p;
20411005a129SJohn Baldwin 
204260517fd1SJohn Baldwin 	/* sx_slock(&allproc_lock); */
2043f67af5c9SXin LI 	FOREACH_PROC_IN_SYSTEM(p) {
2044a1f6d91cSDavid Greenman 		if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
2045a1f6d91cSDavid Greenman 			continue;
2046553629ebSJake Burkholder 		if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
204760517fd1SJohn Baldwin 			/* sx_sunlock(&allproc_lock); */
2048a1f6d91cSDavid Greenman 			return 1;
2049a1f6d91cSDavid Greenman 		}
2050553629ebSJake Burkholder 	}
205160517fd1SJohn Baldwin 	/* sx_sunlock(&allproc_lock); */
2052a1f6d91cSDavid Greenman 	if (_vm_object_in_map(kernel_map, object, 0))
2053a1f6d91cSDavid Greenman 		return 1;
2054a1f6d91cSDavid Greenman 	if (_vm_object_in_map(kmem_map, object, 0))
2055a1f6d91cSDavid Greenman 		return 1;
2056a1f6d91cSDavid Greenman 	if (_vm_object_in_map(pager_map, object, 0))
2057a1f6d91cSDavid Greenman 		return 1;
2058a1f6d91cSDavid Greenman 	if (_vm_object_in_map(buffer_map, object, 0))
2059a1f6d91cSDavid Greenman 		return 1;
2060a1f6d91cSDavid Greenman 	return 0;
2061a1f6d91cSDavid Greenman }
2062a1f6d91cSDavid Greenman 
2063c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check)
2064f708ef1bSPoul-Henning Kamp {
2065a1f6d91cSDavid Greenman 	vm_object_t object;
2066a1f6d91cSDavid Greenman 
2067a1f6d91cSDavid Greenman 	/*
2068a1f6d91cSDavid Greenman 	 * make sure that internal objs are in a map somewhere
2069a1f6d91cSDavid Greenman 	 * and none have zero ref counts.
2070a1f6d91cSDavid Greenman 	 */
2071cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
207224a1cce3SDavid Greenman 		if (object->handle == NULL &&
207324a1cce3SDavid Greenman 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2074a1f6d91cSDavid Greenman 			if (object->ref_count == 0) {
20753efc015bSPeter Wemm 				db_printf("vmochk: internal obj has zero ref count: %ld\n",
20763efc015bSPeter Wemm 					(long)object->size);
2077a1f6d91cSDavid Greenman 			}
2078a1f6d91cSDavid Greenman 			if (!vm_object_in_map(object)) {
2079fc62ef1fSBruce Evans 				db_printf(
2080fc62ef1fSBruce Evans 			"vmochk: internal obj is not in a map: "
2081fc62ef1fSBruce Evans 			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2082fc62ef1fSBruce Evans 				    object->ref_count, (u_long)object->size,
2083fc62ef1fSBruce Evans 				    (u_long)object->size,
2084fc62ef1fSBruce Evans 				    (void *)object->backing_object);
2085a1f6d91cSDavid Greenman 			}
2086a1f6d91cSDavid Greenman 		}
2087a1f6d91cSDavid Greenman 	}
2088a1f6d91cSDavid Greenman }
2089a1f6d91cSDavid Greenman 
209026f9a767SRodney W. Grimes /*
2091df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
2092df8bae1dSRodney W. Grimes  */
2093c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static)
2094df8bae1dSRodney W. Grimes {
2095c7c34a24SBruce Evans 	/* XXX convert args. */
2096c7c34a24SBruce Evans 	vm_object_t object = (vm_object_t)addr;
2097c7c34a24SBruce Evans 	boolean_t full = have_addr;
2098c7c34a24SBruce Evans 
2099d031cff1SMatthew Dillon 	vm_page_t p;
2100df8bae1dSRodney W. Grimes 
2101c7c34a24SBruce Evans 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
2102c7c34a24SBruce Evans #define	count	was_count
2103c7c34a24SBruce Evans 
2104d031cff1SMatthew Dillon 	int count;
2105df8bae1dSRodney W. Grimes 
2106df8bae1dSRodney W. Grimes 	if (object == NULL)
2107df8bae1dSRodney W. Grimes 		return;
2108df8bae1dSRodney W. Grimes 
2109eb95adefSBruce Evans 	db_iprintf(
2110ef694c1aSEdward Tomasz Napierala 	    "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x ruid %d charge %jx\n",
2111e47cd172SMaxime Henrion 	    object, (int)object->type, (uintmax_t)object->size,
21123364c323SKonstantin Belousov 	    object->resident_page_count, object->ref_count, object->flags,
2113ef694c1aSEdward Tomasz Napierala 	    object->cred ? object->cred->cr_ruid : -1, (uintmax_t)object->charge);
2114e47cd172SMaxime Henrion 	db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n",
21151c7c3c6aSMatthew Dillon 	    object->shadow_count,
2116eb95adefSBruce Evans 	    object->backing_object ? object->backing_object->ref_count : 0,
2117e47cd172SMaxime Henrion 	    object->backing_object, (uintmax_t)object->backing_object_offset);
2118df8bae1dSRodney W. Grimes 
2119df8bae1dSRodney W. Grimes 	if (!full)
2120df8bae1dSRodney W. Grimes 		return;
2121df8bae1dSRodney W. Grimes 
2122c7c34a24SBruce Evans 	db_indent += 2;
2123df8bae1dSRodney W. Grimes 	count = 0;
2124fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(p, &object->memq, listq) {
2125df8bae1dSRodney W. Grimes 		if (count == 0)
2126c7c34a24SBruce Evans 			db_iprintf("memory:=");
2127df8bae1dSRodney W. Grimes 		else if (count == 6) {
2128c7c34a24SBruce Evans 			db_printf("\n");
2129c7c34a24SBruce Evans 			db_iprintf(" ...");
2130df8bae1dSRodney W. Grimes 			count = 0;
2131df8bae1dSRodney W. Grimes 		} else
2132c7c34a24SBruce Evans 			db_printf(",");
2133df8bae1dSRodney W. Grimes 		count++;
2134df8bae1dSRodney W. Grimes 
2135e47cd172SMaxime Henrion 		db_printf("(off=0x%jx,page=0x%jx)",
2136e47cd172SMaxime Henrion 		    (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p));
2137df8bae1dSRodney W. Grimes 	}
2138df8bae1dSRodney W. Grimes 	if (count != 0)
2139c7c34a24SBruce Evans 		db_printf("\n");
2140c7c34a24SBruce Evans 	db_indent -= 2;
2141df8bae1dSRodney W. Grimes }
21425070c7f8SJohn Dyson 
2143c7c34a24SBruce Evans /* XXX. */
2144c7c34a24SBruce Evans #undef count
2145c7c34a24SBruce Evans 
2146c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */
21475070c7f8SJohn Dyson void
21481b40f8c0SMatthew Dillon vm_object_print(
21491b40f8c0SMatthew Dillon         /* db_expr_t */ long addr,
21501b40f8c0SMatthew Dillon 	boolean_t have_addr,
21511b40f8c0SMatthew Dillon 	/* db_expr_t */ long count,
21521b40f8c0SMatthew Dillon 	char *modif)
2153c7c34a24SBruce Evans {
2154c7c34a24SBruce Evans 	vm_object_print_static(addr, have_addr, count, modif);
2155c7c34a24SBruce Evans }
2156c7c34a24SBruce Evans 
2157c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
21585070c7f8SJohn Dyson {
21595070c7f8SJohn Dyson 	vm_object_t object;
2160bb2ac86fSKonstantin Belousov 	vm_pindex_t fidx;
2161bb2ac86fSKonstantin Belousov 	vm_paddr_t pa;
2162bb2ac86fSKonstantin Belousov 	vm_page_t m, prev_m;
2163bb2ac86fSKonstantin Belousov 	int rcount, nl, c;
2164cc64b484SAlfred Perlstein 
2165bb2ac86fSKonstantin Belousov 	nl = 0;
2166cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
2167fc62ef1fSBruce Evans 		db_printf("new object: %p\n", (void *)object);
21685070c7f8SJohn Dyson 		if (nl > 18) {
21695070c7f8SJohn Dyson 			c = cngetc();
21705070c7f8SJohn Dyson 			if (c != ' ')
21715070c7f8SJohn Dyson 				return;
21725070c7f8SJohn Dyson 			nl = 0;
21735070c7f8SJohn Dyson 		}
21745070c7f8SJohn Dyson 		nl++;
21755070c7f8SJohn Dyson 		rcount = 0;
21765070c7f8SJohn Dyson 		fidx = 0;
2177bb2ac86fSKonstantin Belousov 		pa = -1;
2178bb2ac86fSKonstantin Belousov 		TAILQ_FOREACH(m, &object->memq, listq) {
2179bb2ac86fSKonstantin Belousov 			if (m->pindex > 128)
2180bb2ac86fSKonstantin Belousov 				break;
2181bb2ac86fSKonstantin Belousov 			if ((prev_m = TAILQ_PREV(m, pglist, listq)) != NULL &&
2182bb2ac86fSKonstantin Belousov 			    prev_m->pindex + 1 != m->pindex) {
21835070c7f8SJohn Dyson 				if (rcount) {
21843efc015bSPeter Wemm 					db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
21853efc015bSPeter Wemm 						(long)fidx, rcount, (long)pa);
21865070c7f8SJohn Dyson 					if (nl > 18) {
21875070c7f8SJohn Dyson 						c = cngetc();
21885070c7f8SJohn Dyson 						if (c != ' ')
21895070c7f8SJohn Dyson 							return;
21905070c7f8SJohn Dyson 						nl = 0;
21915070c7f8SJohn Dyson 					}
21925070c7f8SJohn Dyson 					nl++;
21935070c7f8SJohn Dyson 					rcount = 0;
21945070c7f8SJohn Dyson 				}
21955070c7f8SJohn Dyson 			}
21965070c7f8SJohn Dyson 			if (rcount &&
21975070c7f8SJohn Dyson 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
21985070c7f8SJohn Dyson 				++rcount;
21995070c7f8SJohn Dyson 				continue;
22005070c7f8SJohn Dyson 			}
22015070c7f8SJohn Dyson 			if (rcount) {
22022446e4f0SAlan Cox 				db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
22033efc015bSPeter Wemm 					(long)fidx, rcount, (long)pa);
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 			}
2212bb2ac86fSKonstantin Belousov 			fidx = m->pindex;
22135070c7f8SJohn Dyson 			pa = VM_PAGE_TO_PHYS(m);
22145070c7f8SJohn Dyson 			rcount = 1;
22155070c7f8SJohn Dyson 		}
22165070c7f8SJohn Dyson 		if (rcount) {
22173efc015bSPeter Wemm 			db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
22183efc015bSPeter Wemm 				(long)fidx, rcount, (long)pa);
22195070c7f8SJohn Dyson 			if (nl > 18) {
22205070c7f8SJohn Dyson 				c = cngetc();
22215070c7f8SJohn Dyson 				if (c != ' ')
22225070c7f8SJohn Dyson 					return;
22235070c7f8SJohn Dyson 				nl = 0;
22245070c7f8SJohn Dyson 			}
22255070c7f8SJohn Dyson 			nl++;
22265070c7f8SJohn Dyson 		}
22275070c7f8SJohn Dyson 	}
22285070c7f8SJohn Dyson }
2229c3cb3e12SDavid Greenman #endif /* DDB */
2230