vm_object.c revision 166074
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 166074 2007-01-17 15:05:52Z delphij $");
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_DEFAULT, 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_DEFAULT, 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					tsleep(&proc0, PVM, "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		vm_page_flag_set(p, PG_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->flags & PG_CLEANCHK) == 0) ||
837			(pi < tstart) || (pi >= tend) ||
838			(p->valid == 0) ||
839		    VM_PAGE_INQUEUE1(p, PQ_CACHE)) {
840			vm_page_flag_clear(p, PG_CLEANCHK);
841			continue;
842		}
843
844		vm_page_test_dirty(p);
845		if ((p->dirty & p->valid) == 0) {
846			vm_page_flag_clear(p, PG_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			vm_page_flag_clear(p, PG_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->flags & PG_CLEANCHK) == 0) ||
915				(tp->busy != 0))
916				break;
917			if (VM_PAGE_INQUEUE1(tp, PQ_CACHE)) {
918				vm_page_flag_clear(tp, PG_CLEANCHK);
919				break;
920			}
921			vm_page_test_dirty(tp);
922			if ((tp->dirty & tp->valid) == 0) {
923				vm_page_flag_clear(tp, PG_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->flags & PG_CLEANCHK) == 0) ||
943					(tp->busy != 0))
944					break;
945				if (VM_PAGE_INQUEUE1(tp, PQ_CACHE)) {
946					vm_page_flag_clear(tp, PG_CLEANCHK);
947					break;
948				}
949				vm_page_test_dirty(tp);
950				if ((tp->dirty & tp->valid) == 0) {
951					vm_page_flag_clear(tp, PG_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		vm_page_flag_clear(ma[index], PG_CLEANCHK);
966	}
967	vm_page_flag_clear(p, PG_CLEANCHK);
968	ma[maxb] = p;
969	for(i = 0; i < maxf; i++) {
970		int index = (maxb + i) + 1;
971		ma[index] = maf[i];
972		vm_page_flag_clear(ma[index], PG_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			vm_page_flag_set(ma[i], PG_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	VM_OBJECT_LOCK(new_object);
1312	VM_OBJECT_LOCK(orig_object);
1313	source = orig_object->backing_object;
1314	if (source != NULL) {
1315		VM_OBJECT_LOCK(source);
1316		LIST_INSERT_HEAD(&source->shadow_head,
1317				  new_object, shadow_list);
1318		source->shadow_count++;
1319		source->generation++;
1320		vm_object_reference_locked(source);	/* for new_object */
1321		vm_object_clear_flag(source, OBJ_ONEMAPPING);
1322		VM_OBJECT_UNLOCK(source);
1323		new_object->backing_object_offset =
1324			orig_object->backing_object_offset + entry->offset;
1325		new_object->backing_object = source;
1326	}
1327	new_object->flags |= orig_object->flags & OBJ_NEEDGIANT;
1328retry:
1329	if ((m = TAILQ_FIRST(&orig_object->memq)) != NULL) {
1330		if (m->pindex < offidxstart) {
1331			m = vm_page_splay(offidxstart, orig_object->root);
1332			if ((orig_object->root = m)->pindex < offidxstart)
1333				m = TAILQ_NEXT(m, listq);
1334		}
1335	}
1336	vm_page_lock_queues();
1337	for (; m != NULL && (idx = m->pindex - offidxstart) < size;
1338	    m = m_next) {
1339		m_next = TAILQ_NEXT(m, listq);
1340
1341		/*
1342		 * We must wait for pending I/O to complete before we can
1343		 * rename the page.
1344		 *
1345		 * We do not have to VM_PROT_NONE the page as mappings should
1346		 * not be changed by this operation.
1347		 */
1348		if ((m->oflags & VPO_BUSY) || m->busy) {
1349			vm_page_flag_set(m, PG_REFERENCED);
1350			vm_page_unlock_queues();
1351			VM_OBJECT_UNLOCK(new_object);
1352			m->oflags |= VPO_WANTED;
1353			msleep(m, VM_OBJECT_MTX(orig_object), PDROP | PVM, "spltwt", 0);
1354			VM_OBJECT_LOCK(new_object);
1355			VM_OBJECT_LOCK(orig_object);
1356			goto retry;
1357		}
1358		vm_page_rename(m, new_object, idx);
1359		/* page automatically made dirty by rename and cache handled */
1360		vm_page_busy(m);
1361	}
1362	vm_page_unlock_queues();
1363	if (orig_object->type == OBJT_SWAP) {
1364		/*
1365		 * swap_pager_copy() can sleep, in which case the orig_object's
1366		 * and new_object's locks are released and reacquired.
1367		 */
1368		swap_pager_copy(orig_object, new_object, offidxstart, 0);
1369	}
1370	VM_OBJECT_UNLOCK(orig_object);
1371	TAILQ_FOREACH(m, &new_object->memq, listq)
1372		vm_page_wakeup(m);
1373	VM_OBJECT_UNLOCK(new_object);
1374	entry->object.vm_object = new_object;
1375	entry->offset = 0LL;
1376	vm_object_deallocate(orig_object);
1377	VM_OBJECT_LOCK(new_object);
1378}
1379
1380#define	OBSC_TEST_ALL_SHADOWED	0x0001
1381#define	OBSC_COLLAPSE_NOWAIT	0x0002
1382#define	OBSC_COLLAPSE_WAIT	0x0004
1383
1384static int
1385vm_object_backing_scan(vm_object_t object, int op)
1386{
1387	int r = 1;
1388	vm_page_t p;
1389	vm_object_t backing_object;
1390	vm_pindex_t backing_offset_index;
1391
1392	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1393	VM_OBJECT_LOCK_ASSERT(object->backing_object, MA_OWNED);
1394
1395	backing_object = object->backing_object;
1396	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
1397
1398	/*
1399	 * Initial conditions
1400	 */
1401	if (op & OBSC_TEST_ALL_SHADOWED) {
1402		/*
1403		 * We do not want to have to test for the existence of
1404		 * swap pages in the backing object.  XXX but with the
1405		 * new swapper this would be pretty easy to do.
1406		 *
1407		 * XXX what about anonymous MAP_SHARED memory that hasn't
1408		 * been ZFOD faulted yet?  If we do not test for this, the
1409		 * shadow test may succeed! XXX
1410		 */
1411		if (backing_object->type != OBJT_DEFAULT) {
1412			return (0);
1413		}
1414	}
1415	if (op & OBSC_COLLAPSE_WAIT) {
1416		vm_object_set_flag(backing_object, OBJ_DEAD);
1417	}
1418
1419	/*
1420	 * Our scan
1421	 */
1422	p = TAILQ_FIRST(&backing_object->memq);
1423	while (p) {
1424		vm_page_t next = TAILQ_NEXT(p, listq);
1425		vm_pindex_t new_pindex = p->pindex - backing_offset_index;
1426
1427		if (op & OBSC_TEST_ALL_SHADOWED) {
1428			vm_page_t pp;
1429
1430			/*
1431			 * Ignore pages outside the parent object's range
1432			 * and outside the parent object's mapping of the
1433			 * backing object.
1434			 *
1435			 * note that we do not busy the backing object's
1436			 * page.
1437			 */
1438			if (
1439			    p->pindex < backing_offset_index ||
1440			    new_pindex >= object->size
1441			) {
1442				p = next;
1443				continue;
1444			}
1445
1446			/*
1447			 * See if the parent has the page or if the parent's
1448			 * object pager has the page.  If the parent has the
1449			 * page but the page is not valid, the parent's
1450			 * object pager must have the page.
1451			 *
1452			 * If this fails, the parent does not completely shadow
1453			 * the object and we might as well give up now.
1454			 */
1455
1456			pp = vm_page_lookup(object, new_pindex);
1457			if (
1458			    (pp == NULL || pp->valid == 0) &&
1459			    !vm_pager_has_page(object, new_pindex, NULL, NULL)
1460			) {
1461				r = 0;
1462				break;
1463			}
1464		}
1465
1466		/*
1467		 * Check for busy page
1468		 */
1469		if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
1470			vm_page_t pp;
1471
1472			if (op & OBSC_COLLAPSE_NOWAIT) {
1473				if ((p->oflags & VPO_BUSY) ||
1474				    !p->valid ||
1475				    p->busy) {
1476					p = next;
1477					continue;
1478				}
1479			} else if (op & OBSC_COLLAPSE_WAIT) {
1480				if ((p->oflags & VPO_BUSY) || p->busy) {
1481					vm_page_lock_queues();
1482					vm_page_flag_set(p, PG_REFERENCED);
1483					vm_page_unlock_queues();
1484					VM_OBJECT_UNLOCK(object);
1485					p->oflags |= VPO_WANTED;
1486					msleep(p, VM_OBJECT_MTX(backing_object),
1487					    PDROP | PVM, "vmocol", 0);
1488					VM_OBJECT_LOCK(object);
1489					VM_OBJECT_LOCK(backing_object);
1490					/*
1491					 * If we slept, anything could have
1492					 * happened.  Since the object is
1493					 * marked dead, the backing offset
1494					 * should not have changed so we
1495					 * just restart our scan.
1496					 */
1497					p = TAILQ_FIRST(&backing_object->memq);
1498					continue;
1499				}
1500			}
1501
1502			KASSERT(
1503			    p->object == backing_object,
1504			    ("vm_object_backing_scan: object mismatch")
1505			);
1506
1507			/*
1508			 * Destroy any associated swap
1509			 */
1510			if (backing_object->type == OBJT_SWAP) {
1511				swap_pager_freespace(
1512				    backing_object,
1513				    p->pindex,
1514				    1
1515				);
1516			}
1517
1518			if (
1519			    p->pindex < backing_offset_index ||
1520			    new_pindex >= object->size
1521			) {
1522				/*
1523				 * Page is out of the parent object's range, we
1524				 * can simply destroy it.
1525				 */
1526				vm_page_lock_queues();
1527				KASSERT(!pmap_page_is_mapped(p),
1528				    ("freeing mapped page %p", p));
1529				if (p->wire_count == 0)
1530					vm_page_free(p);
1531				else
1532					vm_page_remove(p);
1533				vm_page_unlock_queues();
1534				p = next;
1535				continue;
1536			}
1537
1538			pp = vm_page_lookup(object, new_pindex);
1539			if (
1540			    pp != NULL ||
1541			    vm_pager_has_page(object, new_pindex, NULL, NULL)
1542			) {
1543				/*
1544				 * page already exists in parent OR swap exists
1545				 * for this location in the parent.  Destroy
1546				 * the original page from the backing object.
1547				 *
1548				 * Leave the parent's page alone
1549				 */
1550				vm_page_lock_queues();
1551				KASSERT(!pmap_page_is_mapped(p),
1552				    ("freeing mapped page %p", p));
1553				if (p->wire_count == 0)
1554					vm_page_free(p);
1555				else
1556					vm_page_remove(p);
1557				vm_page_unlock_queues();
1558				p = next;
1559				continue;
1560			}
1561
1562			/*
1563			 * Page does not exist in parent, rename the
1564			 * page from the backing object to the main object.
1565			 *
1566			 * If the page was mapped to a process, it can remain
1567			 * mapped through the rename.
1568			 */
1569			vm_page_lock_queues();
1570			vm_page_rename(p, object, new_pindex);
1571			vm_page_unlock_queues();
1572			/* page automatically made dirty by rename */
1573		}
1574		p = next;
1575	}
1576	return (r);
1577}
1578
1579
1580/*
1581 * this version of collapse allows the operation to occur earlier and
1582 * when paging_in_progress is true for an object...  This is not a complete
1583 * operation, but should plug 99.9% of the rest of the leaks.
1584 */
1585static void
1586vm_object_qcollapse(vm_object_t object)
1587{
1588	vm_object_t backing_object = object->backing_object;
1589
1590	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1591	VM_OBJECT_LOCK_ASSERT(backing_object, MA_OWNED);
1592
1593	if (backing_object->ref_count != 1)
1594		return;
1595
1596	vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
1597}
1598
1599/*
1600 *	vm_object_collapse:
1601 *
1602 *	Collapse an object with the object backing it.
1603 *	Pages in the backing object are moved into the
1604 *	parent, and the backing object is deallocated.
1605 */
1606void
1607vm_object_collapse(vm_object_t object)
1608{
1609	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1610
1611	while (TRUE) {
1612		vm_object_t backing_object;
1613
1614		/*
1615		 * Verify that the conditions are right for collapse:
1616		 *
1617		 * The object exists and the backing object exists.
1618		 */
1619		if ((backing_object = object->backing_object) == NULL)
1620			break;
1621
1622		/*
1623		 * we check the backing object first, because it is most likely
1624		 * not collapsable.
1625		 */
1626		VM_OBJECT_LOCK(backing_object);
1627		if (backing_object->handle != NULL ||
1628		    (backing_object->type != OBJT_DEFAULT &&
1629		     backing_object->type != OBJT_SWAP) ||
1630		    (backing_object->flags & OBJ_DEAD) ||
1631		    object->handle != NULL ||
1632		    (object->type != OBJT_DEFAULT &&
1633		     object->type != OBJT_SWAP) ||
1634		    (object->flags & OBJ_DEAD)) {
1635			VM_OBJECT_UNLOCK(backing_object);
1636			break;
1637		}
1638
1639		if (
1640		    object->paging_in_progress != 0 ||
1641		    backing_object->paging_in_progress != 0
1642		) {
1643			vm_object_qcollapse(object);
1644			VM_OBJECT_UNLOCK(backing_object);
1645			break;
1646		}
1647		/*
1648		 * We know that we can either collapse the backing object (if
1649		 * the parent is the only reference to it) or (perhaps) have
1650		 * the parent bypass the object if the parent happens to shadow
1651		 * all the resident pages in the entire backing object.
1652		 *
1653		 * This is ignoring pager-backed pages such as swap pages.
1654		 * vm_object_backing_scan fails the shadowing test in this
1655		 * case.
1656		 */
1657		if (backing_object->ref_count == 1) {
1658			/*
1659			 * If there is exactly one reference to the backing
1660			 * object, we can collapse it into the parent.
1661			 */
1662			vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1663
1664			/*
1665			 * Move the pager from backing_object to object.
1666			 */
1667			if (backing_object->type == OBJT_SWAP) {
1668				/*
1669				 * swap_pager_copy() can sleep, in which case
1670				 * the backing_object's and object's locks are
1671				 * released and reacquired.
1672				 */
1673				swap_pager_copy(
1674				    backing_object,
1675				    object,
1676				    OFF_TO_IDX(object->backing_object_offset), TRUE);
1677			}
1678			/*
1679			 * Object now shadows whatever backing_object did.
1680			 * Note that the reference to
1681			 * backing_object->backing_object moves from within
1682			 * backing_object to within object.
1683			 */
1684			LIST_REMOVE(object, shadow_list);
1685			backing_object->shadow_count--;
1686			backing_object->generation++;
1687			if (backing_object->backing_object) {
1688				VM_OBJECT_LOCK(backing_object->backing_object);
1689				LIST_REMOVE(backing_object, shadow_list);
1690				LIST_INSERT_HEAD(
1691				    &backing_object->backing_object->shadow_head,
1692				    object, shadow_list);
1693				/*
1694				 * The shadow_count has not changed.
1695				 */
1696				backing_object->backing_object->generation++;
1697				VM_OBJECT_UNLOCK(backing_object->backing_object);
1698			}
1699			object->backing_object = backing_object->backing_object;
1700			object->backing_object_offset +=
1701			    backing_object->backing_object_offset;
1702
1703			/*
1704			 * Discard backing_object.
1705			 *
1706			 * Since the backing object has no pages, no pager left,
1707			 * and no object references within it, all that is
1708			 * necessary is to dispose of it.
1709			 */
1710			KASSERT(backing_object->ref_count == 1, ("backing_object %p was somehow re-referenced during collapse!", backing_object));
1711			VM_OBJECT_UNLOCK(backing_object);
1712
1713			mtx_lock(&vm_object_list_mtx);
1714			TAILQ_REMOVE(
1715			    &vm_object_list,
1716			    backing_object,
1717			    object_list
1718			);
1719			mtx_unlock(&vm_object_list_mtx);
1720
1721			uma_zfree(obj_zone, backing_object);
1722
1723			object_collapses++;
1724		} else {
1725			vm_object_t new_backing_object;
1726
1727			/*
1728			 * If we do not entirely shadow the backing object,
1729			 * there is nothing we can do so we give up.
1730			 */
1731			if (object->resident_page_count != object->size &&
1732			    vm_object_backing_scan(object,
1733			    OBSC_TEST_ALL_SHADOWED) == 0) {
1734				VM_OBJECT_UNLOCK(backing_object);
1735				break;
1736			}
1737
1738			/*
1739			 * Make the parent shadow the next object in the
1740			 * chain.  Deallocating backing_object will not remove
1741			 * it, since its reference count is at least 2.
1742			 */
1743			LIST_REMOVE(object, shadow_list);
1744			backing_object->shadow_count--;
1745			backing_object->generation++;
1746
1747			new_backing_object = backing_object->backing_object;
1748			if ((object->backing_object = new_backing_object) != NULL) {
1749				VM_OBJECT_LOCK(new_backing_object);
1750				LIST_INSERT_HEAD(
1751				    &new_backing_object->shadow_head,
1752				    object,
1753				    shadow_list
1754				);
1755				new_backing_object->shadow_count++;
1756				new_backing_object->generation++;
1757				vm_object_reference_locked(new_backing_object);
1758				VM_OBJECT_UNLOCK(new_backing_object);
1759				object->backing_object_offset +=
1760					backing_object->backing_object_offset;
1761			}
1762
1763			/*
1764			 * Drop the reference count on backing_object. Since
1765			 * its ref_count was at least 2, it will not vanish.
1766			 */
1767			backing_object->ref_count--;
1768			VM_OBJECT_UNLOCK(backing_object);
1769			object_bypasses++;
1770		}
1771
1772		/*
1773		 * Try again with this object's new backing object.
1774		 */
1775	}
1776}
1777
1778/*
1779 *	vm_object_page_remove:
1780 *
1781 *	Removes all physical pages in the given range from the
1782 *	object's list of pages.  If the range's end is zero, all
1783 *	physical pages from the range's start to the end of the object
1784 *	are deleted.
1785 *
1786 *	The object must be locked.
1787 */
1788void
1789vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
1790    boolean_t clean_only)
1791{
1792	vm_page_t p, next;
1793
1794	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1795	if (object->resident_page_count == 0)
1796		return;
1797
1798	/*
1799	 * Since physically-backed objects do not use managed pages, we can't
1800	 * remove pages from the object (we must instead remove the page
1801	 * references, and then destroy the object).
1802	 */
1803	KASSERT(object->type != OBJT_PHYS,
1804	    ("attempt to remove pages from a physical object"));
1805
1806	vm_object_pip_add(object, 1);
1807again:
1808	vm_page_lock_queues();
1809	if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
1810		if (p->pindex < start) {
1811			p = vm_page_splay(start, object->root);
1812			if ((object->root = p)->pindex < start)
1813				p = TAILQ_NEXT(p, listq);
1814		}
1815	}
1816	/*
1817	 * Assert: the variable p is either (1) the page with the
1818	 * least pindex greater than or equal to the parameter pindex
1819	 * or (2) NULL.
1820	 */
1821	for (;
1822	     p != NULL && (p->pindex < end || end == 0);
1823	     p = next) {
1824		next = TAILQ_NEXT(p, listq);
1825
1826		if (p->wire_count != 0) {
1827			pmap_remove_all(p);
1828			if (!clean_only)
1829				p->valid = 0;
1830			continue;
1831		}
1832		if (vm_page_sleep_if_busy(p, TRUE, "vmopar"))
1833			goto again;
1834		if (clean_only && p->valid) {
1835			pmap_remove_write(p);
1836			if (p->valid & p->dirty)
1837				continue;
1838		}
1839		pmap_remove_all(p);
1840		vm_page_free(p);
1841	}
1842	vm_page_unlock_queues();
1843	vm_object_pip_wakeup(object);
1844}
1845
1846/*
1847 *	Routine:	vm_object_coalesce
1848 *	Function:	Coalesces two objects backing up adjoining
1849 *			regions of memory into a single object.
1850 *
1851 *	returns TRUE if objects were combined.
1852 *
1853 *	NOTE:	Only works at the moment if the second object is NULL -
1854 *		if it's not, which object do we lock first?
1855 *
1856 *	Parameters:
1857 *		prev_object	First object to coalesce
1858 *		prev_offset	Offset into prev_object
1859 *		prev_size	Size of reference to prev_object
1860 *		next_size	Size of reference to the second object
1861 *
1862 *	Conditions:
1863 *	The object must *not* be locked.
1864 */
1865boolean_t
1866vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset,
1867	vm_size_t prev_size, vm_size_t next_size)
1868{
1869	vm_pindex_t next_pindex;
1870
1871	if (prev_object == NULL)
1872		return (TRUE);
1873	VM_OBJECT_LOCK(prev_object);
1874	if (prev_object->type != OBJT_DEFAULT &&
1875	    prev_object->type != OBJT_SWAP) {
1876		VM_OBJECT_UNLOCK(prev_object);
1877		return (FALSE);
1878	}
1879
1880	/*
1881	 * Try to collapse the object first
1882	 */
1883	vm_object_collapse(prev_object);
1884
1885	/*
1886	 * Can't coalesce if: . more than one reference . paged out . shadows
1887	 * another object . has a copy elsewhere (any of which mean that the
1888	 * pages not mapped to prev_entry may be in use anyway)
1889	 */
1890	if (prev_object->backing_object != NULL) {
1891		VM_OBJECT_UNLOCK(prev_object);
1892		return (FALSE);
1893	}
1894
1895	prev_size >>= PAGE_SHIFT;
1896	next_size >>= PAGE_SHIFT;
1897	next_pindex = OFF_TO_IDX(prev_offset) + prev_size;
1898
1899	if ((prev_object->ref_count > 1) &&
1900	    (prev_object->size != next_pindex)) {
1901		VM_OBJECT_UNLOCK(prev_object);
1902		return (FALSE);
1903	}
1904
1905	/*
1906	 * Remove any pages that may still be in the object from a previous
1907	 * deallocation.
1908	 */
1909	if (next_pindex < prev_object->size) {
1910		vm_object_page_remove(prev_object,
1911				      next_pindex,
1912				      next_pindex + next_size, FALSE);
1913		if (prev_object->type == OBJT_SWAP)
1914			swap_pager_freespace(prev_object,
1915					     next_pindex, next_size);
1916	}
1917
1918	/*
1919	 * Extend the object if necessary.
1920	 */
1921	if (next_pindex + next_size > prev_object->size)
1922		prev_object->size = next_pindex + next_size;
1923
1924	VM_OBJECT_UNLOCK(prev_object);
1925	return (TRUE);
1926}
1927
1928void
1929vm_object_set_writeable_dirty(vm_object_t object)
1930{
1931	struct vnode *vp;
1932
1933	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1934	if ((object->flags & OBJ_MIGHTBEDIRTY) != 0)
1935		return;
1936	vm_object_set_flag(object, OBJ_MIGHTBEDIRTY);
1937	if (object->type == OBJT_VNODE &&
1938	    (vp = (struct vnode *)object->handle) != NULL) {
1939		VI_LOCK(vp);
1940		vp->v_iflag |= VI_OBJDIRTY;
1941		VI_UNLOCK(vp);
1942	}
1943}
1944
1945#include "opt_ddb.h"
1946#ifdef DDB
1947#include <sys/kernel.h>
1948
1949#include <sys/cons.h>
1950
1951#include <ddb/ddb.h>
1952
1953static int
1954_vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
1955{
1956	vm_map_t tmpm;
1957	vm_map_entry_t tmpe;
1958	vm_object_t obj;
1959	int entcount;
1960
1961	if (map == 0)
1962		return 0;
1963
1964	if (entry == 0) {
1965		tmpe = map->header.next;
1966		entcount = map->nentries;
1967		while (entcount-- && (tmpe != &map->header)) {
1968			if (_vm_object_in_map(map, object, tmpe)) {
1969				return 1;
1970			}
1971			tmpe = tmpe->next;
1972		}
1973	} else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
1974		tmpm = entry->object.sub_map;
1975		tmpe = tmpm->header.next;
1976		entcount = tmpm->nentries;
1977		while (entcount-- && tmpe != &tmpm->header) {
1978			if (_vm_object_in_map(tmpm, object, tmpe)) {
1979				return 1;
1980			}
1981			tmpe = tmpe->next;
1982		}
1983	} else if ((obj = entry->object.vm_object) != NULL) {
1984		for (; obj; obj = obj->backing_object)
1985			if (obj == object) {
1986				return 1;
1987			}
1988	}
1989	return 0;
1990}
1991
1992static int
1993vm_object_in_map(vm_object_t object)
1994{
1995	struct proc *p;
1996
1997	/* sx_slock(&allproc_lock); */
1998	FOREACH_PROC_IN_SYSTEM(p) {
1999		if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
2000			continue;
2001		if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
2002			/* sx_sunlock(&allproc_lock); */
2003			return 1;
2004		}
2005	}
2006	/* sx_sunlock(&allproc_lock); */
2007	if (_vm_object_in_map(kernel_map, object, 0))
2008		return 1;
2009	if (_vm_object_in_map(kmem_map, object, 0))
2010		return 1;
2011	if (_vm_object_in_map(pager_map, object, 0))
2012		return 1;
2013	if (_vm_object_in_map(buffer_map, object, 0))
2014		return 1;
2015	return 0;
2016}
2017
2018DB_SHOW_COMMAND(vmochk, vm_object_check)
2019{
2020	vm_object_t object;
2021
2022	/*
2023	 * make sure that internal objs are in a map somewhere
2024	 * and none have zero ref counts.
2025	 */
2026	TAILQ_FOREACH(object, &vm_object_list, object_list) {
2027		if (object->handle == NULL &&
2028		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2029			if (object->ref_count == 0) {
2030				db_printf("vmochk: internal obj has zero ref count: %ld\n",
2031					(long)object->size);
2032			}
2033			if (!vm_object_in_map(object)) {
2034				db_printf(
2035			"vmochk: internal obj is not in a map: "
2036			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2037				    object->ref_count, (u_long)object->size,
2038				    (u_long)object->size,
2039				    (void *)object->backing_object);
2040			}
2041		}
2042	}
2043}
2044
2045/*
2046 *	vm_object_print:	[ debug ]
2047 */
2048DB_SHOW_COMMAND(object, vm_object_print_static)
2049{
2050	/* XXX convert args. */
2051	vm_object_t object = (vm_object_t)addr;
2052	boolean_t full = have_addr;
2053
2054	vm_page_t p;
2055
2056	/* XXX count is an (unused) arg.  Avoid shadowing it. */
2057#define	count	was_count
2058
2059	int count;
2060
2061	if (object == NULL)
2062		return;
2063
2064	db_iprintf(
2065	    "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x\n",
2066	    object, (int)object->type, (uintmax_t)object->size,
2067	    object->resident_page_count, object->ref_count, object->flags);
2068	db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n",
2069	    object->shadow_count,
2070	    object->backing_object ? object->backing_object->ref_count : 0,
2071	    object->backing_object, (uintmax_t)object->backing_object_offset);
2072
2073	if (!full)
2074		return;
2075
2076	db_indent += 2;
2077	count = 0;
2078	TAILQ_FOREACH(p, &object->memq, listq) {
2079		if (count == 0)
2080			db_iprintf("memory:=");
2081		else if (count == 6) {
2082			db_printf("\n");
2083			db_iprintf(" ...");
2084			count = 0;
2085		} else
2086			db_printf(",");
2087		count++;
2088
2089		db_printf("(off=0x%jx,page=0x%jx)",
2090		    (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p));
2091	}
2092	if (count != 0)
2093		db_printf("\n");
2094	db_indent -= 2;
2095}
2096
2097/* XXX. */
2098#undef count
2099
2100/* XXX need this non-static entry for calling from vm_map_print. */
2101void
2102vm_object_print(
2103        /* db_expr_t */ long addr,
2104	boolean_t have_addr,
2105	/* db_expr_t */ long count,
2106	char *modif)
2107{
2108	vm_object_print_static(addr, have_addr, count, modif);
2109}
2110
2111DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
2112{
2113	vm_object_t object;
2114	int nl = 0;
2115	int c;
2116
2117	TAILQ_FOREACH(object, &vm_object_list, object_list) {
2118		vm_pindex_t idx, fidx;
2119		vm_pindex_t osize;
2120		vm_paddr_t pa = -1, padiff;
2121		int rcount;
2122		vm_page_t m;
2123
2124		db_printf("new object: %p\n", (void *)object);
2125		if (nl > 18) {
2126			c = cngetc();
2127			if (c != ' ')
2128				return;
2129			nl = 0;
2130		}
2131		nl++;
2132		rcount = 0;
2133		fidx = 0;
2134		osize = object->size;
2135		if (osize > 128)
2136			osize = 128;
2137		for (idx = 0; idx < osize; idx++) {
2138			m = vm_page_lookup(object, idx);
2139			if (m == NULL) {
2140				if (rcount) {
2141					db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2142						(long)fidx, rcount, (long)pa);
2143					if (nl > 18) {
2144						c = cngetc();
2145						if (c != ' ')
2146							return;
2147						nl = 0;
2148					}
2149					nl++;
2150					rcount = 0;
2151				}
2152				continue;
2153			}
2154
2155
2156			if (rcount &&
2157				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
2158				++rcount;
2159				continue;
2160			}
2161			if (rcount) {
2162				padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
2163				padiff >>= PAGE_SHIFT;
2164				padiff &= PQ_COLORMASK;
2165				if (padiff == 0) {
2166					pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
2167					++rcount;
2168					continue;
2169				}
2170				db_printf(" index(%ld)run(%d)pa(0x%lx)",
2171					(long)fidx, rcount, (long)pa);
2172				db_printf("pd(%ld)\n", (long)padiff);
2173				if (nl > 18) {
2174					c = cngetc();
2175					if (c != ' ')
2176						return;
2177					nl = 0;
2178				}
2179				nl++;
2180			}
2181			fidx = idx;
2182			pa = VM_PAGE_TO_PHYS(m);
2183			rcount = 1;
2184		}
2185		if (rcount) {
2186			db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2187				(long)fidx, rcount, (long)pa);
2188			if (nl > 18) {
2189				c = cngetc();
2190				if (c != ' ')
2191					return;
2192				nl = 0;
2193			}
2194			nl++;
2195		}
2196	}
2197}
2198#endif /* DDB */
2199