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