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