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