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