vm_object.c revision 170170
1/*-
2 * Copyright (c) 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * The Mach Operating System project at Carnegie-Mellon University.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 *	from: @(#)vm_object.c	8.5 (Berkeley) 3/22/94
33 *
34 *
35 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36 * All rights reserved.
37 *
38 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
39 *
40 * Permission to use, copy, modify and distribute this software and
41 * its documentation is hereby granted, provided that both the copyright
42 * notice and this permission notice appear in all copies of the
43 * software, derivative works or modified versions, and any portions
44 * thereof, and that both notices appear in supporting documentation.
45 *
46 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49 *
50 * Carnegie Mellon requests users of this software to return to
51 *
52 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
53 *  School of Computer Science
54 *  Carnegie Mellon University
55 *  Pittsburgh PA 15213-3890
56 *
57 * any improvements or extensions that they make and grant Carnegie the
58 * rights to redistribute these changes.
59 */
60
61/*
62 *	Virtual memory object module.
63 */
64
65#include <sys/cdefs.h>
66__FBSDID("$FreeBSD: head/sys/vm/vm_object.c 170170 2007-05-31 22:52:15Z attilio $");
67
68#include <sys/param.h>
69#include <sys/systm.h>
70#include <sys/lock.h>
71#include <sys/mman.h>
72#include <sys/mount.h>
73#include <sys/kernel.h>
74#include <sys/sysctl.h>
75#include <sys/mutex.h>
76#include <sys/proc.h>		/* for curproc, pageproc */
77#include <sys/socket.h>
78#include <sys/vnode.h>
79#include <sys/vmmeter.h>
80#include <sys/sx.h>
81
82#include <vm/vm.h>
83#include <vm/vm_param.h>
84#include <vm/pmap.h>
85#include <vm/vm_map.h>
86#include <vm/vm_object.h>
87#include <vm/vm_page.h>
88#include <vm/vm_pageout.h>
89#include <vm/vm_pager.h>
90#include <vm/swap_pager.h>
91#include <vm/vm_kern.h>
92#include <vm/vm_extern.h>
93#include <vm/uma.h>
94
95#define EASY_SCAN_FACTOR       8
96
97#define MSYNC_FLUSH_HARDSEQ	0x01
98#define MSYNC_FLUSH_SOFTSEQ	0x02
99
100/*
101 * msync / VM object flushing optimizations
102 */
103static int msync_flush_flags = MSYNC_FLUSH_HARDSEQ | MSYNC_FLUSH_SOFTSEQ;
104SYSCTL_INT(_vm, OID_AUTO, msync_flush_flags,
105        CTLFLAG_RW, &msync_flush_flags, 0, "");
106
107static int old_msync;
108SYSCTL_INT(_vm, OID_AUTO, old_msync, CTLFLAG_RW, &old_msync, 0,
109    "Use old (insecure) msync behavior");
110
111static void	vm_object_qcollapse(vm_object_t object);
112static int	vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags);
113static void	vm_object_vndeallocate(vm_object_t object);
114
115/*
116 *	Virtual memory objects maintain the actual data
117 *	associated with allocated virtual memory.  A given
118 *	page of memory exists within exactly one object.
119 *
120 *	An object is only deallocated when all "references"
121 *	are given up.  Only one "reference" to a given
122 *	region of an object should be writeable.
123 *
124 *	Associated with each object is a list of all resident
125 *	memory pages belonging to that object; this list is
126 *	maintained by the "vm_page" module, and locked by the object's
127 *	lock.
128 *
129 *	Each object also records a "pager" routine which is
130 *	used to retrieve (and store) pages to the proper backing
131 *	storage.  In addition, objects may be backed by other
132 *	objects from which they were virtual-copied.
133 *
134 *	The only items within the object structure which are
135 *	modified after time of creation are:
136 *		reference count		locked by object's lock
137 *		pager routine		locked by object's lock
138 *
139 */
140
141struct object_q vm_object_list;
142struct mtx vm_object_list_mtx;	/* lock for object list and count */
143
144struct vm_object kernel_object_store;
145struct vm_object kmem_object_store;
146
147SYSCTL_NODE(_vm_stats, OID_AUTO, object, CTLFLAG_RD, 0, "VM object stats");
148
149static long object_collapses;
150SYSCTL_LONG(_vm_stats_object, OID_AUTO, collapses, CTLFLAG_RD,
151    &object_collapses, 0, "VM object collapses");
152
153static long object_bypasses;
154SYSCTL_LONG(_vm_stats_object, OID_AUTO, bypasses, CTLFLAG_RD,
155    &object_bypasses, 0, "VM object bypasses");
156
157/*
158 * next_index determines the page color that is assigned to the next
159 * allocated object.  Accesses to next_index are not synchronized
160 * because the effects of two or more object allocations using
161 * next_index simultaneously are inconsequential.  At any given time,
162 * numerous objects have the same page color.
163 */
164static int next_index;
165
166static uma_zone_t obj_zone;
167
168static int vm_object_zinit(void *mem, int size, int flags);
169
170#ifdef INVARIANTS
171static void vm_object_zdtor(void *mem, int size, void *arg);
172
173static void
174vm_object_zdtor(void *mem, int size, void *arg)
175{
176	vm_object_t object;
177
178	object = (vm_object_t)mem;
179	KASSERT(TAILQ_EMPTY(&object->memq),
180	    ("object %p has resident pages",
181	    object));
182	KASSERT(object->paging_in_progress == 0,
183	    ("object %p paging_in_progress = %d",
184	    object, object->paging_in_progress));
185	KASSERT(object->resident_page_count == 0,
186	    ("object %p resident_page_count = %d",
187	    object, object->resident_page_count));
188	KASSERT(object->shadow_count == 0,
189	    ("object %p shadow_count = %d",
190	    object, object->shadow_count));
191}
192#endif
193
194static int
195vm_object_zinit(void *mem, int size, int flags)
196{
197	vm_object_t object;
198
199	object = (vm_object_t)mem;
200	bzero(&object->mtx, sizeof(object->mtx));
201	VM_OBJECT_LOCK_INIT(object, "standard object");
202
203	/* These are true for any object that has been freed */
204	object->paging_in_progress = 0;
205	object->resident_page_count = 0;
206	object->shadow_count = 0;
207	return (0);
208}
209
210void
211_vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
212{
213	int incr;
214
215	TAILQ_INIT(&object->memq);
216	LIST_INIT(&object->shadow_head);
217
218	object->root = NULL;
219	object->type = type;
220	object->size = size;
221	object->generation = 1;
222	object->ref_count = 1;
223	object->flags = 0;
224	if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
225		object->flags = OBJ_ONEMAPPING;
226	incr = PQ_MAXLENGTH;
227	if (size <= incr)
228		incr = size;
229	object->pg_color = next_index;
230	next_index = (object->pg_color + incr) & PQ_COLORMASK;
231	object->handle = NULL;
232	object->backing_object = NULL;
233	object->backing_object_offset = (vm_ooffset_t) 0;
234
235	mtx_lock(&vm_object_list_mtx);
236	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
237	mtx_unlock(&vm_object_list_mtx);
238}
239
240/*
241 *	vm_object_init:
242 *
243 *	Initialize the VM objects module.
244 */
245void
246vm_object_init(void)
247{
248	TAILQ_INIT(&vm_object_list);
249	mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF);
250
251	VM_OBJECT_LOCK_INIT(&kernel_object_store, "kernel object");
252	_vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
253	    kernel_object);
254
255	VM_OBJECT_LOCK_INIT(&kmem_object_store, "kmem object");
256	_vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
257	    kmem_object);
258
259	/*
260	 * The lock portion of struct vm_object must be type stable due
261	 * to vm_pageout_fallback_object_lock locking a vm object
262	 * without holding any references to it.
263	 */
264	obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL,
265#ifdef INVARIANTS
266	    vm_object_zdtor,
267#else
268	    NULL,
269#endif
270	    vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM|UMA_ZONE_NOFREE);
271}
272
273void
274vm_object_clear_flag(vm_object_t object, u_short bits)
275{
276
277	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
278	object->flags &= ~bits;
279}
280
281void
282vm_object_pip_add(vm_object_t object, short i)
283{
284
285	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
286	object->paging_in_progress += i;
287}
288
289void
290vm_object_pip_subtract(vm_object_t object, short i)
291{
292
293	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
294	object->paging_in_progress -= i;
295}
296
297void
298vm_object_pip_wakeup(vm_object_t object)
299{
300
301	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
302	object->paging_in_progress--;
303	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
304		vm_object_clear_flag(object, OBJ_PIPWNT);
305		wakeup(object);
306	}
307}
308
309void
310vm_object_pip_wakeupn(vm_object_t object, short i)
311{
312
313	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
314	if (i)
315		object->paging_in_progress -= i;
316	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
317		vm_object_clear_flag(object, OBJ_PIPWNT);
318		wakeup(object);
319	}
320}
321
322void
323vm_object_pip_wait(vm_object_t object, char *waitid)
324{
325
326	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
327	while (object->paging_in_progress) {
328		object->flags |= OBJ_PIPWNT;
329		msleep(object, VM_OBJECT_MTX(object), PVM, waitid, 0);
330	}
331}
332
333/*
334 *	vm_object_allocate:
335 *
336 *	Returns a new object with the given size.
337 */
338vm_object_t
339vm_object_allocate(objtype_t type, vm_pindex_t size)
340{
341	vm_object_t object;
342
343	object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK);
344	_vm_object_allocate(type, size, object);
345	return (object);
346}
347
348
349/*
350 *	vm_object_reference:
351 *
352 *	Gets another reference to the given object.  Note: OBJ_DEAD
353 *	objects can be referenced during final cleaning.
354 */
355void
356vm_object_reference(vm_object_t object)
357{
358	struct vnode *vp;
359
360	if (object == NULL)
361		return;
362	VM_OBJECT_LOCK(object);
363	object->ref_count++;
364	if (object->type == OBJT_VNODE) {
365		int vfslocked;
366
367		vp = object->handle;
368		VM_OBJECT_UNLOCK(object);
369		vfslocked = VFS_LOCK_GIANT(vp->v_mount);
370		vget(vp, LK_RETRY, curthread);
371		VFS_UNLOCK_GIANT(vfslocked);
372	} else
373		VM_OBJECT_UNLOCK(object);
374}
375
376/*
377 *	vm_object_reference_locked:
378 *
379 *	Gets another reference to the given object.
380 *
381 *	The object must be locked.
382 */
383void
384vm_object_reference_locked(vm_object_t object)
385{
386	struct vnode *vp;
387
388	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
389	KASSERT((object->flags & OBJ_DEAD) == 0,
390	    ("vm_object_reference_locked: dead object referenced"));
391	object->ref_count++;
392	if (object->type == OBJT_VNODE) {
393		vp = object->handle;
394		vref(vp);
395	}
396}
397
398/*
399 * Handle deallocating an object of type OBJT_VNODE.
400 */
401static void
402vm_object_vndeallocate(vm_object_t object)
403{
404	struct vnode *vp = (struct vnode *) object->handle;
405
406	VFS_ASSERT_GIANT(vp->v_mount);
407	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
408	KASSERT(object->type == OBJT_VNODE,
409	    ("vm_object_vndeallocate: not a vnode object"));
410	KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
411#ifdef INVARIANTS
412	if (object->ref_count == 0) {
413		vprint("vm_object_vndeallocate", vp);
414		panic("vm_object_vndeallocate: bad object reference count");
415	}
416#endif
417
418	object->ref_count--;
419	if (object->ref_count == 0) {
420		mp_fixme("Unlocked vflag access.");
421		vp->v_vflag &= ~VV_TEXT;
422	}
423	VM_OBJECT_UNLOCK(object);
424	/*
425	 * vrele may need a vop lock
426	 */
427	vrele(vp);
428}
429
430/*
431 *	vm_object_deallocate:
432 *
433 *	Release a reference to the specified object,
434 *	gained either through a vm_object_allocate
435 *	or a vm_object_reference call.  When all references
436 *	are gone, storage associated with this object
437 *	may be relinquished.
438 *
439 *	No object may be locked.
440 */
441void
442vm_object_deallocate(vm_object_t object)
443{
444	vm_object_t temp;
445
446	while (object != NULL) {
447		int vfslocked;
448
449		vfslocked = 0;
450	restart:
451		VM_OBJECT_LOCK(object);
452		if (object->type == OBJT_VNODE) {
453			struct vnode *vp = (struct vnode *) object->handle;
454
455			/*
456			 * Conditionally acquire Giant for a vnode-backed
457			 * object.  We have to be careful since the type of
458			 * a vnode object can change while the object is
459			 * unlocked.
460			 */
461			if (VFS_NEEDSGIANT(vp->v_mount) && !vfslocked) {
462				vfslocked = 1;
463				if (!mtx_trylock(&Giant)) {
464					VM_OBJECT_UNLOCK(object);
465					mtx_lock(&Giant);
466					goto restart;
467				}
468			}
469			vm_object_vndeallocate(object);
470			VFS_UNLOCK_GIANT(vfslocked);
471			return;
472		} else
473			/*
474			 * This is to handle the case that the object
475			 * changed type while we dropped its lock to
476			 * obtain Giant.
477			 */
478			VFS_UNLOCK_GIANT(vfslocked);
479
480		KASSERT(object->ref_count != 0,
481			("vm_object_deallocate: object deallocated too many times: %d", object->type));
482
483		/*
484		 * If the reference count goes to 0 we start calling
485		 * vm_object_terminate() on the object chain.
486		 * A ref count of 1 may be a special case depending on the
487		 * shadow count being 0 or 1.
488		 */
489		object->ref_count--;
490		if (object->ref_count > 1) {
491			VM_OBJECT_UNLOCK(object);
492			return;
493		} else if (object->ref_count == 1) {
494			if (object->shadow_count == 0) {
495				vm_object_set_flag(object, OBJ_ONEMAPPING);
496			} else if ((object->shadow_count == 1) &&
497			    (object->handle == NULL) &&
498			    (object->type == OBJT_DEFAULT ||
499			     object->type == OBJT_SWAP)) {
500				vm_object_t robject;
501
502				robject = LIST_FIRST(&object->shadow_head);
503				KASSERT(robject != NULL,
504				    ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
505					 object->ref_count,
506					 object->shadow_count));
507				if (!VM_OBJECT_TRYLOCK(robject)) {
508					/*
509					 * Avoid a potential deadlock.
510					 */
511					object->ref_count++;
512					VM_OBJECT_UNLOCK(object);
513					/*
514					 * More likely than not the thread
515					 * holding robject's lock has lower
516					 * priority than the current thread.
517					 * Let the lower priority thread run.
518					 */
519					pause("vmo_de", 1);
520					continue;
521				}
522				/*
523				 * Collapse object into its shadow unless its
524				 * shadow is dead.  In that case, object will
525				 * be deallocated by the thread that is
526				 * deallocating its shadow.
527				 */
528				if ((robject->flags & OBJ_DEAD) == 0 &&
529				    (robject->handle == NULL) &&
530				    (robject->type == OBJT_DEFAULT ||
531				     robject->type == OBJT_SWAP)) {
532
533					robject->ref_count++;
534retry:
535					if (robject->paging_in_progress) {
536						VM_OBJECT_UNLOCK(object);
537						vm_object_pip_wait(robject,
538						    "objde1");
539						temp = robject->backing_object;
540						if (object == temp) {
541							VM_OBJECT_LOCK(object);
542							goto retry;
543						}
544					} else if (object->paging_in_progress) {
545						VM_OBJECT_UNLOCK(robject);
546						object->flags |= OBJ_PIPWNT;
547						msleep(object,
548						    VM_OBJECT_MTX(object),
549						    PDROP | PVM, "objde2", 0);
550						VM_OBJECT_LOCK(robject);
551						temp = robject->backing_object;
552						if (object == temp) {
553							VM_OBJECT_LOCK(object);
554							goto retry;
555						}
556					} else
557						VM_OBJECT_UNLOCK(object);
558
559					if (robject->ref_count == 1) {
560						robject->ref_count--;
561						object = robject;
562						goto doterm;
563					}
564					object = robject;
565					vm_object_collapse(object);
566					VM_OBJECT_UNLOCK(object);
567					continue;
568				}
569				VM_OBJECT_UNLOCK(robject);
570			}
571			VM_OBJECT_UNLOCK(object);
572			return;
573		}
574doterm:
575		temp = object->backing_object;
576		if (temp != NULL) {
577			VM_OBJECT_LOCK(temp);
578			LIST_REMOVE(object, shadow_list);
579			temp->shadow_count--;
580			temp->generation++;
581			VM_OBJECT_UNLOCK(temp);
582			object->backing_object = NULL;
583		}
584		/*
585		 * Don't double-terminate, we could be in a termination
586		 * recursion due to the terminate having to sync data
587		 * to disk.
588		 */
589		if ((object->flags & OBJ_DEAD) == 0)
590			vm_object_terminate(object);
591		else
592			VM_OBJECT_UNLOCK(object);
593		object = temp;
594	}
595}
596
597/*
598 *	vm_object_terminate actually destroys the specified object, freeing
599 *	up all previously used resources.
600 *
601 *	The object must be locked.
602 *	This routine may block.
603 */
604void
605vm_object_terminate(vm_object_t object)
606{
607	vm_page_t p;
608
609	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
610
611	/*
612	 * Make sure no one uses us.
613	 */
614	vm_object_set_flag(object, OBJ_DEAD);
615
616	/*
617	 * wait for the pageout daemon to be done with the object
618	 */
619	vm_object_pip_wait(object, "objtrm");
620
621	KASSERT(!object->paging_in_progress,
622		("vm_object_terminate: pageout in progress"));
623
624	/*
625	 * Clean and free the pages, as appropriate. All references to the
626	 * object are gone, so we don't need to lock it.
627	 */
628	if (object->type == OBJT_VNODE) {
629		struct vnode *vp = (struct vnode *)object->handle;
630
631		/*
632		 * Clean pages and flush buffers.
633		 */
634		vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
635		VM_OBJECT_UNLOCK(object);
636
637		vinvalbuf(vp, V_SAVE, NULL, 0, 0);
638
639		VM_OBJECT_LOCK(object);
640	}
641
642	KASSERT(object->ref_count == 0,
643		("vm_object_terminate: object with references, ref_count=%d",
644		object->ref_count));
645
646	/*
647	 * Now free any remaining pages. For internal objects, this also
648	 * removes them from paging queues. Don't free wired pages, just
649	 * remove them from the object.
650	 */
651	vm_page_lock_queues();
652	while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
653		KASSERT(!p->busy && (p->oflags & VPO_BUSY) == 0,
654			("vm_object_terminate: freeing busy page %p "
655			"p->busy = %d, p->flags %x\n", p, p->busy, p->flags));
656		if (p->wire_count == 0) {
657			vm_page_free(p);
658			cnt.v_pfree++;
659		} else {
660			vm_page_remove(p);
661		}
662	}
663	vm_page_unlock_queues();
664
665	/*
666	 * Let the pager know object is dead.
667	 */
668	vm_pager_deallocate(object);
669	VM_OBJECT_UNLOCK(object);
670
671	/*
672	 * Remove the object from the global object list.
673	 */
674	mtx_lock(&vm_object_list_mtx);
675	TAILQ_REMOVE(&vm_object_list, object, object_list);
676	mtx_unlock(&vm_object_list_mtx);
677
678	/*
679	 * Free the space for the object.
680	 */
681	uma_zfree(obj_zone, object);
682}
683
684/*
685 *	vm_object_page_clean
686 *
687 *	Clean all dirty pages in the specified range of object.  Leaves page
688 * 	on whatever queue it is currently on.   If NOSYNC is set then do not
689 *	write out pages with VPO_NOSYNC set (originally comes from MAP_NOSYNC),
690 *	leaving the object dirty.
691 *
692 *	When stuffing pages asynchronously, allow clustering.  XXX we need a
693 *	synchronous clustering mode implementation.
694 *
695 *	Odd semantics: if start == end, we clean everything.
696 *
697 *	The object must be locked.
698 */
699void
700vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end, int flags)
701{
702	vm_page_t p, np;
703	vm_pindex_t tstart, tend;
704	vm_pindex_t pi;
705	int clearobjflags;
706	int pagerflags;
707	int curgeneration;
708
709	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
710	if (object->type != OBJT_VNODE ||
711		(object->flags & OBJ_MIGHTBEDIRTY) == 0)
712		return;
713
714	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
715	pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
716
717	vm_object_set_flag(object, OBJ_CLEANING);
718
719	tstart = start;
720	if (end == 0) {
721		tend = object->size;
722	} else {
723		tend = end;
724	}
725
726	vm_page_lock_queues();
727	/*
728	 * If the caller is smart and only msync()s a range he knows is
729	 * dirty, we may be able to avoid an object scan.  This results in
730	 * a phenominal improvement in performance.  We cannot do this
731	 * as a matter of course because the object may be huge - e.g.
732	 * the size might be in the gigabytes or terrabytes.
733	 */
734	if (msync_flush_flags & MSYNC_FLUSH_HARDSEQ) {
735		vm_pindex_t tscan;
736		int scanlimit;
737		int scanreset;
738
739		scanreset = object->resident_page_count / EASY_SCAN_FACTOR;
740		if (scanreset < 16)
741			scanreset = 16;
742		pagerflags |= VM_PAGER_IGNORE_CLEANCHK;
743
744		scanlimit = scanreset;
745		tscan = tstart;
746		while (tscan < tend) {
747			curgeneration = object->generation;
748			p = vm_page_lookup(object, tscan);
749			if (p == NULL || p->valid == 0 ||
750			    VM_PAGE_INQUEUE1(p, PQ_CACHE)) {
751				if (--scanlimit == 0)
752					break;
753				++tscan;
754				continue;
755			}
756			vm_page_test_dirty(p);
757			if ((p->dirty & p->valid) == 0) {
758				if (--scanlimit == 0)
759					break;
760				++tscan;
761				continue;
762			}
763			/*
764			 * If we have been asked to skip nosync pages and
765			 * this is a nosync page, we can't continue.
766			 */
767			if ((flags & OBJPC_NOSYNC) && (p->oflags & VPO_NOSYNC)) {
768				if (--scanlimit == 0)
769					break;
770				++tscan;
771				continue;
772			}
773			scanlimit = scanreset;
774
775			/*
776			 * This returns 0 if it was unable to busy the first
777			 * page (i.e. had to sleep).
778			 */
779			tscan += vm_object_page_collect_flush(object, p, curgeneration, pagerflags);
780		}
781
782		/*
783		 * If everything was dirty and we flushed it successfully,
784		 * and the requested range is not the entire object, we
785		 * don't have to mess with CLEANCHK or MIGHTBEDIRTY and can
786		 * return immediately.
787		 */
788		if (tscan >= tend && (tstart || tend < object->size)) {
789			vm_page_unlock_queues();
790			vm_object_clear_flag(object, OBJ_CLEANING);
791			return;
792		}
793		pagerflags &= ~VM_PAGER_IGNORE_CLEANCHK;
794	}
795
796	/*
797	 * Generally set CLEANCHK interlock and make the page read-only so
798	 * we can then clear the object flags.
799	 *
800	 * However, if this is a nosync mmap then the object is likely to
801	 * stay dirty so do not mess with the page and do not clear the
802	 * object flags.
803	 */
804	clearobjflags = 1;
805	TAILQ_FOREACH(p, &object->memq, listq) {
806		p->oflags |= VPO_CLEANCHK;
807		if ((flags & OBJPC_NOSYNC) && (p->oflags & VPO_NOSYNC))
808			clearobjflags = 0;
809		else
810			pmap_remove_write(p);
811	}
812
813	if (clearobjflags && (tstart == 0) && (tend == object->size)) {
814		struct vnode *vp;
815
816		vm_object_clear_flag(object, OBJ_MIGHTBEDIRTY);
817		if (object->type == OBJT_VNODE &&
818		    (vp = (struct vnode *)object->handle) != NULL) {
819			VI_LOCK(vp);
820			if (vp->v_iflag & VI_OBJDIRTY)
821				vp->v_iflag &= ~VI_OBJDIRTY;
822			VI_UNLOCK(vp);
823		}
824	}
825
826rescan:
827	curgeneration = object->generation;
828
829	for (p = TAILQ_FIRST(&object->memq); p; p = np) {
830		int n;
831
832		np = TAILQ_NEXT(p, listq);
833
834again:
835		pi = p->pindex;
836		if ((p->oflags & VPO_CLEANCHK) == 0 ||
837			(pi < tstart) || (pi >= tend) ||
838			(p->valid == 0) ||
839		    VM_PAGE_INQUEUE1(p, PQ_CACHE)) {
840			p->oflags &= ~VPO_CLEANCHK;
841			continue;
842		}
843
844		vm_page_test_dirty(p);
845		if ((p->dirty & p->valid) == 0) {
846			p->oflags &= ~VPO_CLEANCHK;
847			continue;
848		}
849
850		/*
851		 * If we have been asked to skip nosync pages and this is a
852		 * nosync page, skip it.  Note that the object flags were
853		 * not cleared in this case so we do not have to set them.
854		 */
855		if ((flags & OBJPC_NOSYNC) && (p->oflags & VPO_NOSYNC)) {
856			p->oflags &= ~VPO_CLEANCHK;
857			continue;
858		}
859
860		n = vm_object_page_collect_flush(object, p,
861			curgeneration, pagerflags);
862		if (n == 0)
863			goto rescan;
864
865		if (object->generation != curgeneration)
866			goto rescan;
867
868		/*
869		 * Try to optimize the next page.  If we can't we pick up
870		 * our (random) scan where we left off.
871		 */
872		if (msync_flush_flags & MSYNC_FLUSH_SOFTSEQ) {
873			if ((p = vm_page_lookup(object, pi + n)) != NULL)
874				goto again;
875		}
876	}
877	vm_page_unlock_queues();
878#if 0
879	VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
880#endif
881
882	vm_object_clear_flag(object, OBJ_CLEANING);
883	return;
884}
885
886static int
887vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags)
888{
889	int runlen;
890	int maxf;
891	int chkb;
892	int maxb;
893	int i;
894	vm_pindex_t pi;
895	vm_page_t maf[vm_pageout_page_count];
896	vm_page_t mab[vm_pageout_page_count];
897	vm_page_t ma[vm_pageout_page_count];
898
899	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
900	pi = p->pindex;
901	while (vm_page_sleep_if_busy(p, TRUE, "vpcwai")) {
902		vm_page_lock_queues();
903		if (object->generation != curgeneration) {
904			return(0);
905		}
906	}
907	maxf = 0;
908	for(i = 1; i < vm_pageout_page_count; i++) {
909		vm_page_t tp;
910
911		if ((tp = vm_page_lookup(object, pi + i)) != NULL) {
912			if ((tp->oflags & VPO_BUSY) ||
913				((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
914				 (tp->oflags & VPO_CLEANCHK) == 0) ||
915				(tp->busy != 0))
916				break;
917			if (VM_PAGE_INQUEUE1(tp, PQ_CACHE)) {
918				tp->oflags &= ~VPO_CLEANCHK;
919				break;
920			}
921			vm_page_test_dirty(tp);
922			if ((tp->dirty & tp->valid) == 0) {
923				tp->oflags &= ~VPO_CLEANCHK;
924				break;
925			}
926			maf[ i - 1 ] = tp;
927			maxf++;
928			continue;
929		}
930		break;
931	}
932
933	maxb = 0;
934	chkb = vm_pageout_page_count -  maxf;
935	if (chkb) {
936		for(i = 1; i < chkb;i++) {
937			vm_page_t tp;
938
939			if ((tp = vm_page_lookup(object, pi - i)) != NULL) {
940				if ((tp->oflags & VPO_BUSY) ||
941					((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
942					 (tp->oflags & VPO_CLEANCHK) == 0) ||
943					(tp->busy != 0))
944					break;
945				if (VM_PAGE_INQUEUE1(tp, PQ_CACHE)) {
946					tp->oflags &= ~VPO_CLEANCHK;
947					break;
948				}
949				vm_page_test_dirty(tp);
950				if ((tp->dirty & tp->valid) == 0) {
951					tp->oflags &= ~VPO_CLEANCHK;
952					break;
953				}
954				mab[ i - 1 ] = tp;
955				maxb++;
956				continue;
957			}
958			break;
959		}
960	}
961
962	for(i = 0; i < maxb; i++) {
963		int index = (maxb - i) - 1;
964		ma[index] = mab[i];
965		ma[index]->oflags &= ~VPO_CLEANCHK;
966	}
967	p->oflags &= ~VPO_CLEANCHK;
968	ma[maxb] = p;
969	for(i = 0; i < maxf; i++) {
970		int index = (maxb + i) + 1;
971		ma[index] = maf[i];
972		ma[index]->oflags &= ~VPO_CLEANCHK;
973	}
974	runlen = maxb + maxf + 1;
975
976	vm_pageout_flush(ma, runlen, pagerflags);
977	for (i = 0; i < runlen; i++) {
978		if (ma[i]->valid & ma[i]->dirty) {
979			pmap_remove_write(ma[i]);
980			ma[i]->oflags |= VPO_CLEANCHK;
981
982			/*
983			 * maxf will end up being the actual number of pages
984			 * we wrote out contiguously, non-inclusive of the
985			 * first page.  We do not count look-behind pages.
986			 */
987			if (i >= maxb + 1 && (maxf > i - maxb - 1))
988				maxf = i - maxb - 1;
989		}
990	}
991	return(maxf + 1);
992}
993
994/*
995 * Note that there is absolutely no sense in writing out
996 * anonymous objects, so we track down the vnode object
997 * to write out.
998 * We invalidate (remove) all pages from the address space
999 * for semantic correctness.
1000 *
1001 * Note: certain anonymous maps, such as MAP_NOSYNC maps,
1002 * may start out with a NULL object.
1003 */
1004void
1005vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size,
1006    boolean_t syncio, boolean_t invalidate)
1007{
1008	vm_object_t backing_object;
1009	struct vnode *vp;
1010	struct mount *mp;
1011	int flags;
1012
1013	if (object == NULL)
1014		return;
1015	VM_OBJECT_LOCK(object);
1016	while ((backing_object = object->backing_object) != NULL) {
1017		VM_OBJECT_LOCK(backing_object);
1018		offset += object->backing_object_offset;
1019		VM_OBJECT_UNLOCK(object);
1020		object = backing_object;
1021		if (object->size < OFF_TO_IDX(offset + size))
1022			size = IDX_TO_OFF(object->size) - offset;
1023	}
1024	/*
1025	 * Flush pages if writing is allowed, invalidate them
1026	 * if invalidation requested.  Pages undergoing I/O
1027	 * will be ignored by vm_object_page_remove().
1028	 *
1029	 * We cannot lock the vnode and then wait for paging
1030	 * to complete without deadlocking against vm_fault.
1031	 * Instead we simply call vm_object_page_remove() and
1032	 * allow it to block internally on a page-by-page
1033	 * basis when it encounters pages undergoing async
1034	 * I/O.
1035	 */
1036	if (object->type == OBJT_VNODE &&
1037	    (object->flags & OBJ_MIGHTBEDIRTY) != 0) {
1038		int vfslocked;
1039		vp = object->handle;
1040		VM_OBJECT_UNLOCK(object);
1041		(void) vn_start_write(vp, &mp, V_WAIT);
1042		vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1043		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
1044		flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
1045		flags |= invalidate ? OBJPC_INVAL : 0;
1046		VM_OBJECT_LOCK(object);
1047		vm_object_page_clean(object,
1048		    OFF_TO_IDX(offset),
1049		    OFF_TO_IDX(offset + size + PAGE_MASK),
1050		    flags);
1051		VM_OBJECT_UNLOCK(object);
1052		VOP_UNLOCK(vp, 0, curthread);
1053		VFS_UNLOCK_GIANT(vfslocked);
1054		vn_finished_write(mp);
1055		VM_OBJECT_LOCK(object);
1056	}
1057	if ((object->type == OBJT_VNODE ||
1058	     object->type == OBJT_DEVICE) && invalidate) {
1059		boolean_t purge;
1060		purge = old_msync || (object->type == OBJT_DEVICE);
1061		vm_object_page_remove(object,
1062		    OFF_TO_IDX(offset),
1063		    OFF_TO_IDX(offset + size + PAGE_MASK),
1064		    purge ? FALSE : TRUE);
1065	}
1066	VM_OBJECT_UNLOCK(object);
1067}
1068
1069/*
1070 *	vm_object_madvise:
1071 *
1072 *	Implements the madvise function at the object/page level.
1073 *
1074 *	MADV_WILLNEED	(any object)
1075 *
1076 *	    Activate the specified pages if they are resident.
1077 *
1078 *	MADV_DONTNEED	(any object)
1079 *
1080 *	    Deactivate the specified pages if they are resident.
1081 *
1082 *	MADV_FREE	(OBJT_DEFAULT/OBJT_SWAP objects,
1083 *			 OBJ_ONEMAPPING only)
1084 *
1085 *	    Deactivate and clean the specified pages if they are
1086 *	    resident.  This permits the process to reuse the pages
1087 *	    without faulting or the kernel to reclaim the pages
1088 *	    without I/O.
1089 */
1090void
1091vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
1092{
1093	vm_pindex_t end, tpindex;
1094	vm_object_t backing_object, tobject;
1095	vm_page_t m;
1096
1097	if (object == NULL)
1098		return;
1099	VM_OBJECT_LOCK(object);
1100	end = pindex + count;
1101	/*
1102	 * Locate and adjust resident pages
1103	 */
1104	for (; pindex < end; pindex += 1) {
1105relookup:
1106		tobject = object;
1107		tpindex = pindex;
1108shadowlookup:
1109		/*
1110		 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
1111		 * and those pages must be OBJ_ONEMAPPING.
1112		 */
1113		if (advise == MADV_FREE) {
1114			if ((tobject->type != OBJT_DEFAULT &&
1115			     tobject->type != OBJT_SWAP) ||
1116			    (tobject->flags & OBJ_ONEMAPPING) == 0) {
1117				goto unlock_tobject;
1118			}
1119		}
1120		m = vm_page_lookup(tobject, tpindex);
1121		if (m == NULL) {
1122			/*
1123			 * There may be swap even if there is no backing page
1124			 */
1125			if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1126				swap_pager_freespace(tobject, tpindex, 1);
1127			/*
1128			 * next object
1129			 */
1130			backing_object = tobject->backing_object;
1131			if (backing_object == NULL)
1132				goto unlock_tobject;
1133			VM_OBJECT_LOCK(backing_object);
1134			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
1135			if (tobject != object)
1136				VM_OBJECT_UNLOCK(tobject);
1137			tobject = backing_object;
1138			goto shadowlookup;
1139		}
1140		/*
1141		 * If the page is busy or not in a normal active state,
1142		 * we skip it.  If the page is not managed there are no
1143		 * page queues to mess with.  Things can break if we mess
1144		 * with pages in any of the below states.
1145		 */
1146		vm_page_lock_queues();
1147		if (m->hold_count ||
1148		    m->wire_count ||
1149		    (m->flags & PG_UNMANAGED) ||
1150		    m->valid != VM_PAGE_BITS_ALL) {
1151			vm_page_unlock_queues();
1152			goto unlock_tobject;
1153		}
1154		if ((m->oflags & VPO_BUSY) || m->busy) {
1155			vm_page_flag_set(m, PG_REFERENCED);
1156			vm_page_unlock_queues();
1157			if (object != tobject)
1158				VM_OBJECT_UNLOCK(object);
1159			m->oflags |= VPO_WANTED;
1160			msleep(m, VM_OBJECT_MTX(tobject), PDROP | PVM, "madvpo", 0);
1161			VM_OBJECT_LOCK(object);
1162  			goto relookup;
1163		}
1164		if (advise == MADV_WILLNEED) {
1165			vm_page_activate(m);
1166		} else if (advise == MADV_DONTNEED) {
1167			vm_page_dontneed(m);
1168		} else if (advise == MADV_FREE) {
1169			/*
1170			 * Mark the page clean.  This will allow the page
1171			 * to be freed up by the system.  However, such pages
1172			 * are often reused quickly by malloc()/free()
1173			 * so we do not do anything that would cause
1174			 * a page fault if we can help it.
1175			 *
1176			 * Specifically, we do not try to actually free
1177			 * the page now nor do we try to put it in the
1178			 * cache (which would cause a page fault on reuse).
1179			 *
1180			 * But we do make the page is freeable as we
1181			 * can without actually taking the step of unmapping
1182			 * it.
1183			 */
1184			pmap_clear_modify(m);
1185			m->dirty = 0;
1186			m->act_count = 0;
1187			vm_page_dontneed(m);
1188		}
1189		vm_page_unlock_queues();
1190		if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1191			swap_pager_freespace(tobject, tpindex, 1);
1192unlock_tobject:
1193		if (tobject != object)
1194			VM_OBJECT_UNLOCK(tobject);
1195	}
1196	VM_OBJECT_UNLOCK(object);
1197}
1198
1199/*
1200 *	vm_object_shadow:
1201 *
1202 *	Create a new object which is backed by the
1203 *	specified existing object range.  The source
1204 *	object reference is deallocated.
1205 *
1206 *	The new object and offset into that object
1207 *	are returned in the source parameters.
1208 */
1209void
1210vm_object_shadow(
1211	vm_object_t *object,	/* IN/OUT */
1212	vm_ooffset_t *offset,	/* IN/OUT */
1213	vm_size_t length)
1214{
1215	vm_object_t source;
1216	vm_object_t result;
1217
1218	source = *object;
1219
1220	/*
1221	 * Don't create the new object if the old object isn't shared.
1222	 */
1223	if (source != NULL) {
1224		VM_OBJECT_LOCK(source);
1225		if (source->ref_count == 1 &&
1226		    source->handle == NULL &&
1227		    (source->type == OBJT_DEFAULT ||
1228		     source->type == OBJT_SWAP)) {
1229			VM_OBJECT_UNLOCK(source);
1230			return;
1231		}
1232		VM_OBJECT_UNLOCK(source);
1233	}
1234
1235	/*
1236	 * Allocate a new object with the given length.
1237	 */
1238	result = vm_object_allocate(OBJT_DEFAULT, length);
1239
1240	/*
1241	 * The new object shadows the source object, adding a reference to it.
1242	 * Our caller changes his reference to point to the new object,
1243	 * removing a reference to the source object.  Net result: no change
1244	 * of reference count.
1245	 *
1246	 * Try to optimize the result object's page color when shadowing
1247	 * in order to maintain page coloring consistency in the combined
1248	 * shadowed object.
1249	 */
1250	result->backing_object = source;
1251	/*
1252	 * Store the offset into the source object, and fix up the offset into
1253	 * the new object.
1254	 */
1255	result->backing_object_offset = *offset;
1256	if (source != NULL) {
1257		VM_OBJECT_LOCK(source);
1258		LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1259		source->shadow_count++;
1260		source->generation++;
1261		if (length < source->size)
1262			length = source->size;
1263		if (length > PQ_MAXLENGTH || source->generation > 1)
1264			length = PQ_MAXLENGTH;
1265		result->pg_color = (source->pg_color +
1266		    length * source->generation) & PQ_COLORMASK;
1267		result->flags |= source->flags & OBJ_NEEDGIANT;
1268		VM_OBJECT_UNLOCK(source);
1269		next_index = (result->pg_color + PQ_MAXLENGTH) & PQ_COLORMASK;
1270	}
1271
1272
1273	/*
1274	 * Return the new things
1275	 */
1276	*offset = 0;
1277	*object = result;
1278}
1279
1280/*
1281 *	vm_object_split:
1282 *
1283 * Split the pages in a map entry into a new object.  This affords
1284 * easier removal of unused pages, and keeps object inheritance from
1285 * being a negative impact on memory usage.
1286 */
1287void
1288vm_object_split(vm_map_entry_t entry)
1289{
1290	vm_page_t m, m_next;
1291	vm_object_t orig_object, new_object, source;
1292	vm_pindex_t idx, offidxstart;
1293	vm_size_t size;
1294
1295	orig_object = entry->object.vm_object;
1296	if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1297		return;
1298	if (orig_object->ref_count <= 1)
1299		return;
1300	VM_OBJECT_UNLOCK(orig_object);
1301
1302	offidxstart = OFF_TO_IDX(entry->offset);
1303	size = atop(entry->end - entry->start);
1304
1305	/*
1306	 * If swap_pager_copy() is later called, it will convert new_object
1307	 * into a swap object.
1308	 */
1309	new_object = vm_object_allocate(OBJT_DEFAULT, size);
1310
1311	/*
1312	 * At this point, the new object is still private, so the order in
1313	 * which the original and new objects are locked does not matter.
1314	 */
1315	VM_OBJECT_LOCK(new_object);
1316	VM_OBJECT_LOCK(orig_object);
1317	source = orig_object->backing_object;
1318	if (source != NULL) {
1319		VM_OBJECT_LOCK(source);
1320		if ((source->flags & OBJ_DEAD) != 0) {
1321			VM_OBJECT_UNLOCK(source);
1322			VM_OBJECT_UNLOCK(orig_object);
1323			VM_OBJECT_UNLOCK(new_object);
1324			vm_object_deallocate(new_object);
1325			VM_OBJECT_LOCK(orig_object);
1326			return;
1327		}
1328		LIST_INSERT_HEAD(&source->shadow_head,
1329				  new_object, shadow_list);
1330		source->shadow_count++;
1331		source->generation++;
1332		vm_object_reference_locked(source);	/* for new_object */
1333		vm_object_clear_flag(source, OBJ_ONEMAPPING);
1334		VM_OBJECT_UNLOCK(source);
1335		new_object->backing_object_offset =
1336			orig_object->backing_object_offset + entry->offset;
1337		new_object->backing_object = source;
1338	}
1339	new_object->flags |= orig_object->flags & OBJ_NEEDGIANT;
1340retry:
1341	if ((m = TAILQ_FIRST(&orig_object->memq)) != NULL) {
1342		if (m->pindex < offidxstart) {
1343			m = vm_page_splay(offidxstart, orig_object->root);
1344			if ((orig_object->root = m)->pindex < offidxstart)
1345				m = TAILQ_NEXT(m, listq);
1346		}
1347	}
1348	vm_page_lock_queues();
1349	for (; m != NULL && (idx = m->pindex - offidxstart) < size;
1350	    m = m_next) {
1351		m_next = TAILQ_NEXT(m, listq);
1352
1353		/*
1354		 * We must wait for pending I/O to complete before we can
1355		 * rename the page.
1356		 *
1357		 * We do not have to VM_PROT_NONE the page as mappings should
1358		 * not be changed by this operation.
1359		 */
1360		if ((m->oflags & VPO_BUSY) || m->busy) {
1361			vm_page_flag_set(m, PG_REFERENCED);
1362			vm_page_unlock_queues();
1363			VM_OBJECT_UNLOCK(new_object);
1364			m->oflags |= VPO_WANTED;
1365			msleep(m, VM_OBJECT_MTX(orig_object), PVM, "spltwt", 0);
1366			VM_OBJECT_LOCK(new_object);
1367			goto retry;
1368		}
1369		vm_page_rename(m, new_object, idx);
1370		/* page automatically made dirty by rename and cache handled */
1371		vm_page_busy(m);
1372	}
1373	vm_page_unlock_queues();
1374	if (orig_object->type == OBJT_SWAP) {
1375		/*
1376		 * swap_pager_copy() can sleep, in which case the orig_object's
1377		 * and new_object's locks are released and reacquired.
1378		 */
1379		swap_pager_copy(orig_object, new_object, offidxstart, 0);
1380	}
1381	VM_OBJECT_UNLOCK(orig_object);
1382	TAILQ_FOREACH(m, &new_object->memq, listq)
1383		vm_page_wakeup(m);
1384	VM_OBJECT_UNLOCK(new_object);
1385	entry->object.vm_object = new_object;
1386	entry->offset = 0LL;
1387	vm_object_deallocate(orig_object);
1388	VM_OBJECT_LOCK(new_object);
1389}
1390
1391#define	OBSC_TEST_ALL_SHADOWED	0x0001
1392#define	OBSC_COLLAPSE_NOWAIT	0x0002
1393#define	OBSC_COLLAPSE_WAIT	0x0004
1394
1395static int
1396vm_object_backing_scan(vm_object_t object, int op)
1397{
1398	int r = 1;
1399	vm_page_t p;
1400	vm_object_t backing_object;
1401	vm_pindex_t backing_offset_index;
1402
1403	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1404	VM_OBJECT_LOCK_ASSERT(object->backing_object, MA_OWNED);
1405
1406	backing_object = object->backing_object;
1407	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
1408
1409	/*
1410	 * Initial conditions
1411	 */
1412	if (op & OBSC_TEST_ALL_SHADOWED) {
1413		/*
1414		 * We do not want to have to test for the existence of
1415		 * swap pages in the backing object.  XXX but with the
1416		 * new swapper this would be pretty easy to do.
1417		 *
1418		 * XXX what about anonymous MAP_SHARED memory that hasn't
1419		 * been ZFOD faulted yet?  If we do not test for this, the
1420		 * shadow test may succeed! XXX
1421		 */
1422		if (backing_object->type != OBJT_DEFAULT) {
1423			return (0);
1424		}
1425	}
1426	if (op & OBSC_COLLAPSE_WAIT) {
1427		vm_object_set_flag(backing_object, OBJ_DEAD);
1428	}
1429
1430	/*
1431	 * Our scan
1432	 */
1433	p = TAILQ_FIRST(&backing_object->memq);
1434	while (p) {
1435		vm_page_t next = TAILQ_NEXT(p, listq);
1436		vm_pindex_t new_pindex = p->pindex - backing_offset_index;
1437
1438		if (op & OBSC_TEST_ALL_SHADOWED) {
1439			vm_page_t pp;
1440
1441			/*
1442			 * Ignore pages outside the parent object's range
1443			 * and outside the parent object's mapping of the
1444			 * backing object.
1445			 *
1446			 * note that we do not busy the backing object's
1447			 * page.
1448			 */
1449			if (
1450			    p->pindex < backing_offset_index ||
1451			    new_pindex >= object->size
1452			) {
1453				p = next;
1454				continue;
1455			}
1456
1457			/*
1458			 * See if the parent has the page or if the parent's
1459			 * object pager has the page.  If the parent has the
1460			 * page but the page is not valid, the parent's
1461			 * object pager must have the page.
1462			 *
1463			 * If this fails, the parent does not completely shadow
1464			 * the object and we might as well give up now.
1465			 */
1466
1467			pp = vm_page_lookup(object, new_pindex);
1468			if (
1469			    (pp == NULL || pp->valid == 0) &&
1470			    !vm_pager_has_page(object, new_pindex, NULL, NULL)
1471			) {
1472				r = 0;
1473				break;
1474			}
1475		}
1476
1477		/*
1478		 * Check for busy page
1479		 */
1480		if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
1481			vm_page_t pp;
1482
1483			if (op & OBSC_COLLAPSE_NOWAIT) {
1484				if ((p->oflags & VPO_BUSY) ||
1485				    !p->valid ||
1486				    p->busy) {
1487					p = next;
1488					continue;
1489				}
1490			} else if (op & OBSC_COLLAPSE_WAIT) {
1491				if ((p->oflags & VPO_BUSY) || p->busy) {
1492					vm_page_lock_queues();
1493					vm_page_flag_set(p, PG_REFERENCED);
1494					vm_page_unlock_queues();
1495					VM_OBJECT_UNLOCK(object);
1496					p->oflags |= VPO_WANTED;
1497					msleep(p, VM_OBJECT_MTX(backing_object),
1498					    PDROP | PVM, "vmocol", 0);
1499					VM_OBJECT_LOCK(object);
1500					VM_OBJECT_LOCK(backing_object);
1501					/*
1502					 * If we slept, anything could have
1503					 * happened.  Since the object is
1504					 * marked dead, the backing offset
1505					 * should not have changed so we
1506					 * just restart our scan.
1507					 */
1508					p = TAILQ_FIRST(&backing_object->memq);
1509					continue;
1510				}
1511			}
1512
1513			KASSERT(
1514			    p->object == backing_object,
1515			    ("vm_object_backing_scan: object mismatch")
1516			);
1517
1518			/*
1519			 * Destroy any associated swap
1520			 */
1521			if (backing_object->type == OBJT_SWAP) {
1522				swap_pager_freespace(
1523				    backing_object,
1524				    p->pindex,
1525				    1
1526				);
1527			}
1528
1529			if (
1530			    p->pindex < backing_offset_index ||
1531			    new_pindex >= object->size
1532			) {
1533				/*
1534				 * Page is out of the parent object's range, we
1535				 * can simply destroy it.
1536				 */
1537				vm_page_lock_queues();
1538				KASSERT(!pmap_page_is_mapped(p),
1539				    ("freeing mapped page %p", p));
1540				if (p->wire_count == 0)
1541					vm_page_free(p);
1542				else
1543					vm_page_remove(p);
1544				vm_page_unlock_queues();
1545				p = next;
1546				continue;
1547			}
1548
1549			pp = vm_page_lookup(object, new_pindex);
1550			if (
1551			    pp != NULL ||
1552			    vm_pager_has_page(object, new_pindex, NULL, NULL)
1553			) {
1554				/*
1555				 * page already exists in parent OR swap exists
1556				 * for this location in the parent.  Destroy
1557				 * the original page from the backing object.
1558				 *
1559				 * Leave the parent's page alone
1560				 */
1561				vm_page_lock_queues();
1562				KASSERT(!pmap_page_is_mapped(p),
1563				    ("freeing mapped page %p", p));
1564				if (p->wire_count == 0)
1565					vm_page_free(p);
1566				else
1567					vm_page_remove(p);
1568				vm_page_unlock_queues();
1569				p = next;
1570				continue;
1571			}
1572
1573			/*
1574			 * Page does not exist in parent, rename the
1575			 * page from the backing object to the main object.
1576			 *
1577			 * If the page was mapped to a process, it can remain
1578			 * mapped through the rename.
1579			 */
1580			vm_page_lock_queues();
1581			vm_page_rename(p, object, new_pindex);
1582			vm_page_unlock_queues();
1583			/* page automatically made dirty by rename */
1584		}
1585		p = next;
1586	}
1587	return (r);
1588}
1589
1590
1591/*
1592 * this version of collapse allows the operation to occur earlier and
1593 * when paging_in_progress is true for an object...  This is not a complete
1594 * operation, but should plug 99.9% of the rest of the leaks.
1595 */
1596static void
1597vm_object_qcollapse(vm_object_t object)
1598{
1599	vm_object_t backing_object = object->backing_object;
1600
1601	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1602	VM_OBJECT_LOCK_ASSERT(backing_object, MA_OWNED);
1603
1604	if (backing_object->ref_count != 1)
1605		return;
1606
1607	vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
1608}
1609
1610/*
1611 *	vm_object_collapse:
1612 *
1613 *	Collapse an object with the object backing it.
1614 *	Pages in the backing object are moved into the
1615 *	parent, and the backing object is deallocated.
1616 */
1617void
1618vm_object_collapse(vm_object_t object)
1619{
1620	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1621
1622	while (TRUE) {
1623		vm_object_t backing_object;
1624
1625		/*
1626		 * Verify that the conditions are right for collapse:
1627		 *
1628		 * The object exists and the backing object exists.
1629		 */
1630		if ((backing_object = object->backing_object) == NULL)
1631			break;
1632
1633		/*
1634		 * we check the backing object first, because it is most likely
1635		 * not collapsable.
1636		 */
1637		VM_OBJECT_LOCK(backing_object);
1638		if (backing_object->handle != NULL ||
1639		    (backing_object->type != OBJT_DEFAULT &&
1640		     backing_object->type != OBJT_SWAP) ||
1641		    (backing_object->flags & OBJ_DEAD) ||
1642		    object->handle != NULL ||
1643		    (object->type != OBJT_DEFAULT &&
1644		     object->type != OBJT_SWAP) ||
1645		    (object->flags & OBJ_DEAD)) {
1646			VM_OBJECT_UNLOCK(backing_object);
1647			break;
1648		}
1649
1650		if (
1651		    object->paging_in_progress != 0 ||
1652		    backing_object->paging_in_progress != 0
1653		) {
1654			vm_object_qcollapse(object);
1655			VM_OBJECT_UNLOCK(backing_object);
1656			break;
1657		}
1658		/*
1659		 * We know that we can either collapse the backing object (if
1660		 * the parent is the only reference to it) or (perhaps) have
1661		 * the parent bypass the object if the parent happens to shadow
1662		 * all the resident pages in the entire backing object.
1663		 *
1664		 * This is ignoring pager-backed pages such as swap pages.
1665		 * vm_object_backing_scan fails the shadowing test in this
1666		 * case.
1667		 */
1668		if (backing_object->ref_count == 1) {
1669			/*
1670			 * If there is exactly one reference to the backing
1671			 * object, we can collapse it into the parent.
1672			 */
1673			vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1674
1675			/*
1676			 * Move the pager from backing_object to object.
1677			 */
1678			if (backing_object->type == OBJT_SWAP) {
1679				/*
1680				 * swap_pager_copy() can sleep, in which case
1681				 * the backing_object's and object's locks are
1682				 * released and reacquired.
1683				 */
1684				swap_pager_copy(
1685				    backing_object,
1686				    object,
1687				    OFF_TO_IDX(object->backing_object_offset), TRUE);
1688			}
1689			/*
1690			 * Object now shadows whatever backing_object did.
1691			 * Note that the reference to
1692			 * backing_object->backing_object moves from within
1693			 * backing_object to within object.
1694			 */
1695			LIST_REMOVE(object, shadow_list);
1696			backing_object->shadow_count--;
1697			backing_object->generation++;
1698			if (backing_object->backing_object) {
1699				VM_OBJECT_LOCK(backing_object->backing_object);
1700				LIST_REMOVE(backing_object, shadow_list);
1701				LIST_INSERT_HEAD(
1702				    &backing_object->backing_object->shadow_head,
1703				    object, shadow_list);
1704				/*
1705				 * The shadow_count has not changed.
1706				 */
1707				backing_object->backing_object->generation++;
1708				VM_OBJECT_UNLOCK(backing_object->backing_object);
1709			}
1710			object->backing_object = backing_object->backing_object;
1711			object->backing_object_offset +=
1712			    backing_object->backing_object_offset;
1713
1714			/*
1715			 * Discard backing_object.
1716			 *
1717			 * Since the backing object has no pages, no pager left,
1718			 * and no object references within it, all that is
1719			 * necessary is to dispose of it.
1720			 */
1721			KASSERT(backing_object->ref_count == 1, ("backing_object %p was somehow re-referenced during collapse!", backing_object));
1722			VM_OBJECT_UNLOCK(backing_object);
1723
1724			mtx_lock(&vm_object_list_mtx);
1725			TAILQ_REMOVE(
1726			    &vm_object_list,
1727			    backing_object,
1728			    object_list
1729			);
1730			mtx_unlock(&vm_object_list_mtx);
1731
1732			uma_zfree(obj_zone, backing_object);
1733
1734			object_collapses++;
1735		} else {
1736			vm_object_t new_backing_object;
1737
1738			/*
1739			 * If we do not entirely shadow the backing object,
1740			 * there is nothing we can do so we give up.
1741			 */
1742			if (object->resident_page_count != object->size &&
1743			    vm_object_backing_scan(object,
1744			    OBSC_TEST_ALL_SHADOWED) == 0) {
1745				VM_OBJECT_UNLOCK(backing_object);
1746				break;
1747			}
1748
1749			/*
1750			 * Make the parent shadow the next object in the
1751			 * chain.  Deallocating backing_object will not remove
1752			 * it, since its reference count is at least 2.
1753			 */
1754			LIST_REMOVE(object, shadow_list);
1755			backing_object->shadow_count--;
1756			backing_object->generation++;
1757
1758			new_backing_object = backing_object->backing_object;
1759			if ((object->backing_object = new_backing_object) != NULL) {
1760				VM_OBJECT_LOCK(new_backing_object);
1761				LIST_INSERT_HEAD(
1762				    &new_backing_object->shadow_head,
1763				    object,
1764				    shadow_list
1765				);
1766				new_backing_object->shadow_count++;
1767				new_backing_object->generation++;
1768				vm_object_reference_locked(new_backing_object);
1769				VM_OBJECT_UNLOCK(new_backing_object);
1770				object->backing_object_offset +=
1771					backing_object->backing_object_offset;
1772			}
1773
1774			/*
1775			 * Drop the reference count on backing_object. Since
1776			 * its ref_count was at least 2, it will not vanish.
1777			 */
1778			backing_object->ref_count--;
1779			VM_OBJECT_UNLOCK(backing_object);
1780			object_bypasses++;
1781		}
1782
1783		/*
1784		 * Try again with this object's new backing object.
1785		 */
1786	}
1787}
1788
1789/*
1790 *	vm_object_page_remove:
1791 *
1792 *	Removes all physical pages in the given range from the
1793 *	object's list of pages.  If the range's end is zero, all
1794 *	physical pages from the range's start to the end of the object
1795 *	are deleted.
1796 *
1797 *	The object must be locked.
1798 */
1799void
1800vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
1801    boolean_t clean_only)
1802{
1803	vm_page_t p, next;
1804
1805	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1806	if (object->resident_page_count == 0)
1807		return;
1808
1809	/*
1810	 * Since physically-backed objects do not use managed pages, we can't
1811	 * remove pages from the object (we must instead remove the page
1812	 * references, and then destroy the object).
1813	 */
1814	KASSERT(object->type != OBJT_PHYS || object == kernel_object ||
1815	    object == kmem_object,
1816	    ("attempt to remove pages from a physical object"));
1817
1818	vm_object_pip_add(object, 1);
1819again:
1820	vm_page_lock_queues();
1821	if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
1822		if (p->pindex < start) {
1823			p = vm_page_splay(start, object->root);
1824			if ((object->root = p)->pindex < start)
1825				p = TAILQ_NEXT(p, listq);
1826		}
1827	}
1828	/*
1829	 * Assert: the variable p is either (1) the page with the
1830	 * least pindex greater than or equal to the parameter pindex
1831	 * or (2) NULL.
1832	 */
1833	for (;
1834	     p != NULL && (p->pindex < end || end == 0);
1835	     p = next) {
1836		next = TAILQ_NEXT(p, listq);
1837
1838		if (p->wire_count != 0) {
1839			pmap_remove_all(p);
1840			if (!clean_only)
1841				p->valid = 0;
1842			continue;
1843		}
1844		if (vm_page_sleep_if_busy(p, TRUE, "vmopar"))
1845			goto again;
1846		if (clean_only && p->valid) {
1847			pmap_remove_write(p);
1848			if (p->valid & p->dirty)
1849				continue;
1850		}
1851		pmap_remove_all(p);
1852		vm_page_free(p);
1853	}
1854	vm_page_unlock_queues();
1855	vm_object_pip_wakeup(object);
1856}
1857
1858/*
1859 *	Routine:	vm_object_coalesce
1860 *	Function:	Coalesces two objects backing up adjoining
1861 *			regions of memory into a single object.
1862 *
1863 *	returns TRUE if objects were combined.
1864 *
1865 *	NOTE:	Only works at the moment if the second object is NULL -
1866 *		if it's not, which object do we lock first?
1867 *
1868 *	Parameters:
1869 *		prev_object	First object to coalesce
1870 *		prev_offset	Offset into prev_object
1871 *		prev_size	Size of reference to prev_object
1872 *		next_size	Size of reference to the second object
1873 *
1874 *	Conditions:
1875 *	The object must *not* be locked.
1876 */
1877boolean_t
1878vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset,
1879	vm_size_t prev_size, vm_size_t next_size)
1880{
1881	vm_pindex_t next_pindex;
1882
1883	if (prev_object == NULL)
1884		return (TRUE);
1885	VM_OBJECT_LOCK(prev_object);
1886	if (prev_object->type != OBJT_DEFAULT &&
1887	    prev_object->type != OBJT_SWAP) {
1888		VM_OBJECT_UNLOCK(prev_object);
1889		return (FALSE);
1890	}
1891
1892	/*
1893	 * Try to collapse the object first
1894	 */
1895	vm_object_collapse(prev_object);
1896
1897	/*
1898	 * Can't coalesce if: . more than one reference . paged out . shadows
1899	 * another object . has a copy elsewhere (any of which mean that the
1900	 * pages not mapped to prev_entry may be in use anyway)
1901	 */
1902	if (prev_object->backing_object != NULL) {
1903		VM_OBJECT_UNLOCK(prev_object);
1904		return (FALSE);
1905	}
1906
1907	prev_size >>= PAGE_SHIFT;
1908	next_size >>= PAGE_SHIFT;
1909	next_pindex = OFF_TO_IDX(prev_offset) + prev_size;
1910
1911	if ((prev_object->ref_count > 1) &&
1912	    (prev_object->size != next_pindex)) {
1913		VM_OBJECT_UNLOCK(prev_object);
1914		return (FALSE);
1915	}
1916
1917	/*
1918	 * Remove any pages that may still be in the object from a previous
1919	 * deallocation.
1920	 */
1921	if (next_pindex < prev_object->size) {
1922		vm_object_page_remove(prev_object,
1923				      next_pindex,
1924				      next_pindex + next_size, FALSE);
1925		if (prev_object->type == OBJT_SWAP)
1926			swap_pager_freespace(prev_object,
1927					     next_pindex, next_size);
1928	}
1929
1930	/*
1931	 * Extend the object if necessary.
1932	 */
1933	if (next_pindex + next_size > prev_object->size)
1934		prev_object->size = next_pindex + next_size;
1935
1936	VM_OBJECT_UNLOCK(prev_object);
1937	return (TRUE);
1938}
1939
1940void
1941vm_object_set_writeable_dirty(vm_object_t object)
1942{
1943	struct vnode *vp;
1944
1945	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1946	if ((object->flags & OBJ_MIGHTBEDIRTY) != 0)
1947		return;
1948	vm_object_set_flag(object, OBJ_MIGHTBEDIRTY);
1949	if (object->type == OBJT_VNODE &&
1950	    (vp = (struct vnode *)object->handle) != NULL) {
1951		VI_LOCK(vp);
1952		vp->v_iflag |= VI_OBJDIRTY;
1953		VI_UNLOCK(vp);
1954	}
1955}
1956
1957#include "opt_ddb.h"
1958#ifdef DDB
1959#include <sys/kernel.h>
1960
1961#include <sys/cons.h>
1962
1963#include <ddb/ddb.h>
1964
1965static int
1966_vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
1967{
1968	vm_map_t tmpm;
1969	vm_map_entry_t tmpe;
1970	vm_object_t obj;
1971	int entcount;
1972
1973	if (map == 0)
1974		return 0;
1975
1976	if (entry == 0) {
1977		tmpe = map->header.next;
1978		entcount = map->nentries;
1979		while (entcount-- && (tmpe != &map->header)) {
1980			if (_vm_object_in_map(map, object, tmpe)) {
1981				return 1;
1982			}
1983			tmpe = tmpe->next;
1984		}
1985	} else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
1986		tmpm = entry->object.sub_map;
1987		tmpe = tmpm->header.next;
1988		entcount = tmpm->nentries;
1989		while (entcount-- && tmpe != &tmpm->header) {
1990			if (_vm_object_in_map(tmpm, object, tmpe)) {
1991				return 1;
1992			}
1993			tmpe = tmpe->next;
1994		}
1995	} else if ((obj = entry->object.vm_object) != NULL) {
1996		for (; obj; obj = obj->backing_object)
1997			if (obj == object) {
1998				return 1;
1999			}
2000	}
2001	return 0;
2002}
2003
2004static int
2005vm_object_in_map(vm_object_t object)
2006{
2007	struct proc *p;
2008
2009	/* sx_slock(&allproc_lock); */
2010	FOREACH_PROC_IN_SYSTEM(p) {
2011		if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
2012			continue;
2013		if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
2014			/* sx_sunlock(&allproc_lock); */
2015			return 1;
2016		}
2017	}
2018	/* sx_sunlock(&allproc_lock); */
2019	if (_vm_object_in_map(kernel_map, object, 0))
2020		return 1;
2021	if (_vm_object_in_map(kmem_map, object, 0))
2022		return 1;
2023	if (_vm_object_in_map(pager_map, object, 0))
2024		return 1;
2025	if (_vm_object_in_map(buffer_map, object, 0))
2026		return 1;
2027	return 0;
2028}
2029
2030DB_SHOW_COMMAND(vmochk, vm_object_check)
2031{
2032	vm_object_t object;
2033
2034	/*
2035	 * make sure that internal objs are in a map somewhere
2036	 * and none have zero ref counts.
2037	 */
2038	TAILQ_FOREACH(object, &vm_object_list, object_list) {
2039		if (object->handle == NULL &&
2040		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2041			if (object->ref_count == 0) {
2042				db_printf("vmochk: internal obj has zero ref count: %ld\n",
2043					(long)object->size);
2044			}
2045			if (!vm_object_in_map(object)) {
2046				db_printf(
2047			"vmochk: internal obj is not in a map: "
2048			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2049				    object->ref_count, (u_long)object->size,
2050				    (u_long)object->size,
2051				    (void *)object->backing_object);
2052			}
2053		}
2054	}
2055}
2056
2057/*
2058 *	vm_object_print:	[ debug ]
2059 */
2060DB_SHOW_COMMAND(object, vm_object_print_static)
2061{
2062	/* XXX convert args. */
2063	vm_object_t object = (vm_object_t)addr;
2064	boolean_t full = have_addr;
2065
2066	vm_page_t p;
2067
2068	/* XXX count is an (unused) arg.  Avoid shadowing it. */
2069#define	count	was_count
2070
2071	int count;
2072
2073	if (object == NULL)
2074		return;
2075
2076	db_iprintf(
2077	    "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x\n",
2078	    object, (int)object->type, (uintmax_t)object->size,
2079	    object->resident_page_count, object->ref_count, object->flags);
2080	db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n",
2081	    object->shadow_count,
2082	    object->backing_object ? object->backing_object->ref_count : 0,
2083	    object->backing_object, (uintmax_t)object->backing_object_offset);
2084
2085	if (!full)
2086		return;
2087
2088	db_indent += 2;
2089	count = 0;
2090	TAILQ_FOREACH(p, &object->memq, listq) {
2091		if (count == 0)
2092			db_iprintf("memory:=");
2093		else if (count == 6) {
2094			db_printf("\n");
2095			db_iprintf(" ...");
2096			count = 0;
2097		} else
2098			db_printf(",");
2099		count++;
2100
2101		db_printf("(off=0x%jx,page=0x%jx)",
2102		    (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p));
2103	}
2104	if (count != 0)
2105		db_printf("\n");
2106	db_indent -= 2;
2107}
2108
2109/* XXX. */
2110#undef count
2111
2112/* XXX need this non-static entry for calling from vm_map_print. */
2113void
2114vm_object_print(
2115        /* db_expr_t */ long addr,
2116	boolean_t have_addr,
2117	/* db_expr_t */ long count,
2118	char *modif)
2119{
2120	vm_object_print_static(addr, have_addr, count, modif);
2121}
2122
2123DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
2124{
2125	vm_object_t object;
2126	int nl = 0;
2127	int c;
2128
2129	TAILQ_FOREACH(object, &vm_object_list, object_list) {
2130		vm_pindex_t idx, fidx;
2131		vm_pindex_t osize;
2132		vm_paddr_t pa = -1, padiff;
2133		int rcount;
2134		vm_page_t m;
2135
2136		db_printf("new object: %p\n", (void *)object);
2137		if (nl > 18) {
2138			c = cngetc();
2139			if (c != ' ')
2140				return;
2141			nl = 0;
2142		}
2143		nl++;
2144		rcount = 0;
2145		fidx = 0;
2146		osize = object->size;
2147		if (osize > 128)
2148			osize = 128;
2149		for (idx = 0; idx < osize; idx++) {
2150			m = vm_page_lookup(object, idx);
2151			if (m == NULL) {
2152				if (rcount) {
2153					db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2154						(long)fidx, rcount, (long)pa);
2155					if (nl > 18) {
2156						c = cngetc();
2157						if (c != ' ')
2158							return;
2159						nl = 0;
2160					}
2161					nl++;
2162					rcount = 0;
2163				}
2164				continue;
2165			}
2166
2167
2168			if (rcount &&
2169				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
2170				++rcount;
2171				continue;
2172			}
2173			if (rcount) {
2174				padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
2175				padiff >>= PAGE_SHIFT;
2176				padiff &= PQ_COLORMASK;
2177				if (padiff == 0) {
2178					pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
2179					++rcount;
2180					continue;
2181				}
2182				db_printf(" index(%ld)run(%d)pa(0x%lx)",
2183					(long)fidx, rcount, (long)pa);
2184				db_printf("pd(%ld)\n", (long)padiff);
2185				if (nl > 18) {
2186					c = cngetc();
2187					if (c != ' ')
2188						return;
2189					nl = 0;
2190				}
2191				nl++;
2192			}
2193			fidx = idx;
2194			pa = VM_PAGE_TO_PHYS(m);
2195			rcount = 1;
2196		}
2197		if (rcount) {
2198			db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2199				(long)fidx, rcount, (long)pa);
2200			if (nl > 18) {
2201				c = cngetc();
2202				if (c != ' ')
2203					return;
2204				nl = 0;
2205			}
2206			nl++;
2207		}
2208	}
2209}
2210#endif /* DDB */
2211