xref: /freebsd/sys/vm/vm_object.c (revision 282a3889ebf826db9839be296ff1dd903f6d6d6e)
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_NODE(_vm_stats, OID_AUTO, object, CTLFLAG_RD, 0, "VM object stats");
148 
149 static long object_collapses;
150 SYSCTL_LONG(_vm_stats_object, OID_AUTO, collapses, CTLFLAG_RD,
151     &object_collapses, 0, "VM object collapses");
152 
153 static long object_bypasses;
154 SYSCTL_LONG(_vm_stats_object, OID_AUTO, bypasses, CTLFLAG_RD,
155     &object_bypasses, 0, "VM object bypasses");
156 
157 static uma_zone_t obj_zone;
158 
159 static int vm_object_zinit(void *mem, int size, int flags);
160 
161 #ifdef INVARIANTS
162 static void vm_object_zdtor(void *mem, int size, void *arg);
163 
164 static void
165 vm_object_zdtor(void *mem, int size, void *arg)
166 {
167 	vm_object_t object;
168 
169 	object = (vm_object_t)mem;
170 	KASSERT(TAILQ_EMPTY(&object->memq),
171 	    ("object %p has resident pages",
172 	    object));
173 	KASSERT(object->paging_in_progress == 0,
174 	    ("object %p paging_in_progress = %d",
175 	    object, object->paging_in_progress));
176 	KASSERT(object->resident_page_count == 0,
177 	    ("object %p resident_page_count = %d",
178 	    object, object->resident_page_count));
179 	KASSERT(object->shadow_count == 0,
180 	    ("object %p shadow_count = %d",
181 	    object, object->shadow_count));
182 }
183 #endif
184 
185 static int
186 vm_object_zinit(void *mem, int size, int flags)
187 {
188 	vm_object_t object;
189 
190 	object = (vm_object_t)mem;
191 	bzero(&object->mtx, sizeof(object->mtx));
192 	VM_OBJECT_LOCK_INIT(object, "standard object");
193 
194 	/* These are true for any object that has been freed */
195 	object->paging_in_progress = 0;
196 	object->resident_page_count = 0;
197 	object->shadow_count = 0;
198 	return (0);
199 }
200 
201 void
202 _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
203 {
204 
205 	TAILQ_INIT(&object->memq);
206 	LIST_INIT(&object->shadow_head);
207 
208 	object->root = NULL;
209 	object->type = type;
210 	object->size = size;
211 	object->generation = 1;
212 	object->ref_count = 1;
213 	object->flags = 0;
214 	if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
215 		object->flags = OBJ_ONEMAPPING;
216 	object->pg_color = 0;
217 	object->handle = NULL;
218 	object->backing_object = NULL;
219 	object->backing_object_offset = (vm_ooffset_t) 0;
220 
221 	mtx_lock(&vm_object_list_mtx);
222 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
223 	mtx_unlock(&vm_object_list_mtx);
224 }
225 
226 /*
227  *	vm_object_init:
228  *
229  *	Initialize the VM objects module.
230  */
231 void
232 vm_object_init(void)
233 {
234 	TAILQ_INIT(&vm_object_list);
235 	mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF);
236 
237 	VM_OBJECT_LOCK_INIT(&kernel_object_store, "kernel object");
238 	_vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
239 	    kernel_object);
240 
241 	VM_OBJECT_LOCK_INIT(&kmem_object_store, "kmem object");
242 	_vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
243 	    kmem_object);
244 
245 	/*
246 	 * The lock portion of struct vm_object must be type stable due
247 	 * to vm_pageout_fallback_object_lock locking a vm object
248 	 * without holding any references to it.
249 	 */
250 	obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL,
251 #ifdef INVARIANTS
252 	    vm_object_zdtor,
253 #else
254 	    NULL,
255 #endif
256 	    vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM|UMA_ZONE_NOFREE);
257 }
258 
259 void
260 vm_object_clear_flag(vm_object_t object, u_short bits)
261 {
262 
263 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
264 	object->flags &= ~bits;
265 }
266 
267 void
268 vm_object_pip_add(vm_object_t object, short i)
269 {
270 
271 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
272 	object->paging_in_progress += i;
273 }
274 
275 void
276 vm_object_pip_subtract(vm_object_t object, short i)
277 {
278 
279 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
280 	object->paging_in_progress -= i;
281 }
282 
283 void
284 vm_object_pip_wakeup(vm_object_t object)
285 {
286 
287 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
288 	object->paging_in_progress--;
289 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
290 		vm_object_clear_flag(object, OBJ_PIPWNT);
291 		wakeup(object);
292 	}
293 }
294 
295 void
296 vm_object_pip_wakeupn(vm_object_t object, short i)
297 {
298 
299 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
300 	if (i)
301 		object->paging_in_progress -= i;
302 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
303 		vm_object_clear_flag(object, OBJ_PIPWNT);
304 		wakeup(object);
305 	}
306 }
307 
308 void
309 vm_object_pip_wait(vm_object_t object, char *waitid)
310 {
311 
312 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
313 	while (object->paging_in_progress) {
314 		object->flags |= OBJ_PIPWNT;
315 		msleep(object, VM_OBJECT_MTX(object), PVM, waitid, 0);
316 	}
317 }
318 
319 /*
320  *	vm_object_allocate:
321  *
322  *	Returns a new object with the given size.
323  */
324 vm_object_t
325 vm_object_allocate(objtype_t type, vm_pindex_t size)
326 {
327 	vm_object_t object;
328 
329 	object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK);
330 	_vm_object_allocate(type, size, object);
331 	return (object);
332 }
333 
334 
335 /*
336  *	vm_object_reference:
337  *
338  *	Gets another reference to the given object.  Note: OBJ_DEAD
339  *	objects can be referenced during final cleaning.
340  */
341 void
342 vm_object_reference(vm_object_t object)
343 {
344 	struct vnode *vp;
345 
346 	if (object == NULL)
347 		return;
348 	VM_OBJECT_LOCK(object);
349 	object->ref_count++;
350 	if (object->type == OBJT_VNODE) {
351 		int vfslocked;
352 
353 		vp = object->handle;
354 		VM_OBJECT_UNLOCK(object);
355 		vfslocked = VFS_LOCK_GIANT(vp->v_mount);
356 		vget(vp, LK_RETRY, curthread);
357 		VFS_UNLOCK_GIANT(vfslocked);
358 	} else
359 		VM_OBJECT_UNLOCK(object);
360 }
361 
362 /*
363  *	vm_object_reference_locked:
364  *
365  *	Gets another reference to the given object.
366  *
367  *	The object must be locked.
368  */
369 void
370 vm_object_reference_locked(vm_object_t object)
371 {
372 	struct vnode *vp;
373 
374 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
375 	KASSERT((object->flags & OBJ_DEAD) == 0,
376 	    ("vm_object_reference_locked: dead object referenced"));
377 	object->ref_count++;
378 	if (object->type == OBJT_VNODE) {
379 		vp = object->handle;
380 		vref(vp);
381 	}
382 }
383 
384 /*
385  * Handle deallocating an object of type OBJT_VNODE.
386  */
387 static void
388 vm_object_vndeallocate(vm_object_t object)
389 {
390 	struct vnode *vp = (struct vnode *) object->handle;
391 
392 	VFS_ASSERT_GIANT(vp->v_mount);
393 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
394 	KASSERT(object->type == OBJT_VNODE,
395 	    ("vm_object_vndeallocate: not a vnode object"));
396 	KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
397 #ifdef INVARIANTS
398 	if (object->ref_count == 0) {
399 		vprint("vm_object_vndeallocate", vp);
400 		panic("vm_object_vndeallocate: bad object reference count");
401 	}
402 #endif
403 
404 	object->ref_count--;
405 	if (object->ref_count == 0) {
406 		mp_fixme("Unlocked vflag access.");
407 		vp->v_vflag &= ~VV_TEXT;
408 	}
409 	VM_OBJECT_UNLOCK(object);
410 	/*
411 	 * vrele may need a vop lock
412 	 */
413 	vrele(vp);
414 }
415 
416 /*
417  *	vm_object_deallocate:
418  *
419  *	Release a reference to the specified object,
420  *	gained either through a vm_object_allocate
421  *	or a vm_object_reference call.  When all references
422  *	are gone, storage associated with this object
423  *	may be relinquished.
424  *
425  *	No object may be locked.
426  */
427 void
428 vm_object_deallocate(vm_object_t object)
429 {
430 	vm_object_t temp;
431 
432 	while (object != NULL) {
433 		int vfslocked;
434 
435 		vfslocked = 0;
436 	restart:
437 		VM_OBJECT_LOCK(object);
438 		if (object->type == OBJT_VNODE) {
439 			struct vnode *vp = (struct vnode *) object->handle;
440 
441 			/*
442 			 * Conditionally acquire Giant for a vnode-backed
443 			 * object.  We have to be careful since the type of
444 			 * a vnode object can change while the object is
445 			 * unlocked.
446 			 */
447 			if (VFS_NEEDSGIANT(vp->v_mount) && !vfslocked) {
448 				vfslocked = 1;
449 				if (!mtx_trylock(&Giant)) {
450 					VM_OBJECT_UNLOCK(object);
451 					mtx_lock(&Giant);
452 					goto restart;
453 				}
454 			}
455 			vm_object_vndeallocate(object);
456 			VFS_UNLOCK_GIANT(vfslocked);
457 			return;
458 		} else
459 			/*
460 			 * This is to handle the case that the object
461 			 * changed type while we dropped its lock to
462 			 * obtain Giant.
463 			 */
464 			VFS_UNLOCK_GIANT(vfslocked);
465 
466 		KASSERT(object->ref_count != 0,
467 			("vm_object_deallocate: object deallocated too many times: %d", object->type));
468 
469 		/*
470 		 * If the reference count goes to 0 we start calling
471 		 * vm_object_terminate() on the object chain.
472 		 * A ref count of 1 may be a special case depending on the
473 		 * shadow count being 0 or 1.
474 		 */
475 		object->ref_count--;
476 		if (object->ref_count > 1) {
477 			VM_OBJECT_UNLOCK(object);
478 			return;
479 		} else if (object->ref_count == 1) {
480 			if (object->shadow_count == 0) {
481 				vm_object_set_flag(object, OBJ_ONEMAPPING);
482 			} else if ((object->shadow_count == 1) &&
483 			    (object->handle == NULL) &&
484 			    (object->type == OBJT_DEFAULT ||
485 			     object->type == OBJT_SWAP)) {
486 				vm_object_t robject;
487 
488 				robject = LIST_FIRST(&object->shadow_head);
489 				KASSERT(robject != NULL,
490 				    ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
491 					 object->ref_count,
492 					 object->shadow_count));
493 				if (!VM_OBJECT_TRYLOCK(robject)) {
494 					/*
495 					 * Avoid a potential deadlock.
496 					 */
497 					object->ref_count++;
498 					VM_OBJECT_UNLOCK(object);
499 					/*
500 					 * More likely than not the thread
501 					 * holding robject's lock has lower
502 					 * priority than the current thread.
503 					 * Let the lower priority thread run.
504 					 */
505 					pause("vmo_de", 1);
506 					continue;
507 				}
508 				/*
509 				 * Collapse object into its shadow unless its
510 				 * shadow is dead.  In that case, object will
511 				 * be deallocated by the thread that is
512 				 * deallocating its shadow.
513 				 */
514 				if ((robject->flags & OBJ_DEAD) == 0 &&
515 				    (robject->handle == NULL) &&
516 				    (robject->type == OBJT_DEFAULT ||
517 				     robject->type == OBJT_SWAP)) {
518 
519 					robject->ref_count++;
520 retry:
521 					if (robject->paging_in_progress) {
522 						VM_OBJECT_UNLOCK(object);
523 						vm_object_pip_wait(robject,
524 						    "objde1");
525 						temp = robject->backing_object;
526 						if (object == temp) {
527 							VM_OBJECT_LOCK(object);
528 							goto retry;
529 						}
530 					} else if (object->paging_in_progress) {
531 						VM_OBJECT_UNLOCK(robject);
532 						object->flags |= OBJ_PIPWNT;
533 						msleep(object,
534 						    VM_OBJECT_MTX(object),
535 						    PDROP | PVM, "objde2", 0);
536 						VM_OBJECT_LOCK(robject);
537 						temp = robject->backing_object;
538 						if (object == temp) {
539 							VM_OBJECT_LOCK(object);
540 							goto retry;
541 						}
542 					} else
543 						VM_OBJECT_UNLOCK(object);
544 
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->oflags & VPO_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 VPO_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->oflags & VPO_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 		p->oflags |= VPO_CLEANCHK;
793 		if ((flags & OBJPC_NOSYNC) && (p->oflags & VPO_NOSYNC))
794 			clearobjflags = 0;
795 		else
796 			pmap_remove_write(p);
797 	}
798 
799 	if (clearobjflags && (tstart == 0) && (tend == object->size)) {
800 		struct vnode *vp;
801 
802 		vm_object_clear_flag(object, 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->oflags & VPO_CLEANCHK) == 0 ||
823 			(pi < tstart) || (pi >= tend) ||
824 			(p->valid == 0) ||
825 		    VM_PAGE_INQUEUE1(p, PQ_CACHE)) {
826 			p->oflags &= ~VPO_CLEANCHK;
827 			continue;
828 		}
829 
830 		vm_page_test_dirty(p);
831 		if ((p->dirty & p->valid) == 0) {
832 			p->oflags &= ~VPO_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->oflags & VPO_NOSYNC)) {
842 			p->oflags &= ~VPO_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->oflags & VPO_BUSY) ||
899 				((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
900 				 (tp->oflags & VPO_CLEANCHK) == 0) ||
901 				(tp->busy != 0))
902 				break;
903 			if (VM_PAGE_INQUEUE1(tp, PQ_CACHE)) {
904 				tp->oflags &= ~VPO_CLEANCHK;
905 				break;
906 			}
907 			vm_page_test_dirty(tp);
908 			if ((tp->dirty & tp->valid) == 0) {
909 				tp->oflags &= ~VPO_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->oflags & VPO_BUSY) ||
927 					((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
928 					 (tp->oflags & VPO_CLEANCHK) == 0) ||
929 					(tp->busy != 0))
930 					break;
931 				if (VM_PAGE_INQUEUE1(tp, PQ_CACHE)) {
932 					tp->oflags &= ~VPO_CLEANCHK;
933 					break;
934 				}
935 				vm_page_test_dirty(tp);
936 				if ((tp->dirty & tp->valid) == 0) {
937 					tp->oflags &= ~VPO_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 		ma[index]->oflags &= ~VPO_CLEANCHK;
952 	}
953 	p->oflags &= ~VPO_CLEANCHK;
954 	ma[maxb] = p;
955 	for(i = 0; i < maxf; i++) {
956 		int index = (maxb + i) + 1;
957 		ma[index] = maf[i];
958 		ma[index]->oflags &= ~VPO_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_remove_write(ma[i]);
966 			ma[i]->oflags |= VPO_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->oflags & VPO_BUSY) || m->busy) {
1141 			vm_page_flag_set(m, PG_REFERENCED);
1142 			vm_page_unlock_queues();
1143 			if (object != tobject)
1144 				VM_OBJECT_UNLOCK(object);
1145 			m->oflags |= VPO_WANTED;
1146 			msleep(m, VM_OBJECT_MTX(tobject), PDROP | PVM, "madvpo", 0);
1147 			VM_OBJECT_LOCK(object);
1148   			goto relookup;
1149 		}
1150 		if (advise == MADV_WILLNEED) {
1151 			vm_page_activate(m);
1152 		} else if (advise == MADV_DONTNEED) {
1153 			vm_page_dontneed(m);
1154 		} else if (advise == MADV_FREE) {
1155 			/*
1156 			 * Mark the page clean.  This will allow the page
1157 			 * to be freed up by the system.  However, such pages
1158 			 * are often reused quickly by malloc()/free()
1159 			 * so we do not do anything that would cause
1160 			 * a page fault if we can help it.
1161 			 *
1162 			 * Specifically, we do not try to actually free
1163 			 * the page now nor do we try to put it in the
1164 			 * cache (which would cause a page fault on reuse).
1165 			 *
1166 			 * But we do make the page is freeable as we
1167 			 * can without actually taking the step of unmapping
1168 			 * it.
1169 			 */
1170 			pmap_clear_modify(m);
1171 			m->dirty = 0;
1172 			m->act_count = 0;
1173 			vm_page_dontneed(m);
1174 		}
1175 		vm_page_unlock_queues();
1176 		if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1177 			swap_pager_freespace(tobject, tpindex, 1);
1178 unlock_tobject:
1179 		if (tobject != object)
1180 			VM_OBJECT_UNLOCK(tobject);
1181 	}
1182 	VM_OBJECT_UNLOCK(object);
1183 }
1184 
1185 /*
1186  *	vm_object_shadow:
1187  *
1188  *	Create a new object which is backed by the
1189  *	specified existing object range.  The source
1190  *	object reference is deallocated.
1191  *
1192  *	The new object and offset into that object
1193  *	are returned in the source parameters.
1194  */
1195 void
1196 vm_object_shadow(
1197 	vm_object_t *object,	/* IN/OUT */
1198 	vm_ooffset_t *offset,	/* IN/OUT */
1199 	vm_size_t length)
1200 {
1201 	vm_object_t source;
1202 	vm_object_t result;
1203 
1204 	source = *object;
1205 
1206 	/*
1207 	 * Don't create the new object if the old object isn't shared.
1208 	 */
1209 	if (source != NULL) {
1210 		VM_OBJECT_LOCK(source);
1211 		if (source->ref_count == 1 &&
1212 		    source->handle == NULL &&
1213 		    (source->type == OBJT_DEFAULT ||
1214 		     source->type == OBJT_SWAP)) {
1215 			VM_OBJECT_UNLOCK(source);
1216 			return;
1217 		}
1218 		VM_OBJECT_UNLOCK(source);
1219 	}
1220 
1221 	/*
1222 	 * Allocate a new object with the given length.
1223 	 */
1224 	result = vm_object_allocate(OBJT_DEFAULT, length);
1225 
1226 	/*
1227 	 * The new object shadows the source object, adding a reference to it.
1228 	 * Our caller changes his reference to point to the new object,
1229 	 * removing a reference to the source object.  Net result: no change
1230 	 * of reference count.
1231 	 *
1232 	 * Try to optimize the result object's page color when shadowing
1233 	 * in order to maintain page coloring consistency in the combined
1234 	 * shadowed object.
1235 	 */
1236 	result->backing_object = source;
1237 	/*
1238 	 * Store the offset into the source object, and fix up the offset into
1239 	 * the new object.
1240 	 */
1241 	result->backing_object_offset = *offset;
1242 	if (source != NULL) {
1243 		VM_OBJECT_LOCK(source);
1244 		LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1245 		source->shadow_count++;
1246 		source->generation++;
1247 		result->flags |= source->flags & OBJ_NEEDGIANT;
1248 		VM_OBJECT_UNLOCK(source);
1249 	}
1250 
1251 
1252 	/*
1253 	 * Return the new things
1254 	 */
1255 	*offset = 0;
1256 	*object = result;
1257 }
1258 
1259 /*
1260  *	vm_object_split:
1261  *
1262  * Split the pages in a map entry into a new object.  This affords
1263  * easier removal of unused pages, and keeps object inheritance from
1264  * being a negative impact on memory usage.
1265  */
1266 void
1267 vm_object_split(vm_map_entry_t entry)
1268 {
1269 	vm_page_t m, m_next;
1270 	vm_object_t orig_object, new_object, source;
1271 	vm_pindex_t idx, offidxstart;
1272 	vm_size_t size;
1273 
1274 	orig_object = entry->object.vm_object;
1275 	if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1276 		return;
1277 	if (orig_object->ref_count <= 1)
1278 		return;
1279 	VM_OBJECT_UNLOCK(orig_object);
1280 
1281 	offidxstart = OFF_TO_IDX(entry->offset);
1282 	size = atop(entry->end - entry->start);
1283 
1284 	/*
1285 	 * If swap_pager_copy() is later called, it will convert new_object
1286 	 * into a swap object.
1287 	 */
1288 	new_object = vm_object_allocate(OBJT_DEFAULT, size);
1289 
1290 	/*
1291 	 * At this point, the new object is still private, so the order in
1292 	 * which the original and new objects are locked does not matter.
1293 	 */
1294 	VM_OBJECT_LOCK(new_object);
1295 	VM_OBJECT_LOCK(orig_object);
1296 	source = orig_object->backing_object;
1297 	if (source != NULL) {
1298 		VM_OBJECT_LOCK(source);
1299 		if ((source->flags & OBJ_DEAD) != 0) {
1300 			VM_OBJECT_UNLOCK(source);
1301 			VM_OBJECT_UNLOCK(orig_object);
1302 			VM_OBJECT_UNLOCK(new_object);
1303 			vm_object_deallocate(new_object);
1304 			VM_OBJECT_LOCK(orig_object);
1305 			return;
1306 		}
1307 		LIST_INSERT_HEAD(&source->shadow_head,
1308 				  new_object, shadow_list);
1309 		source->shadow_count++;
1310 		source->generation++;
1311 		vm_object_reference_locked(source);	/* for new_object */
1312 		vm_object_clear_flag(source, OBJ_ONEMAPPING);
1313 		VM_OBJECT_UNLOCK(source);
1314 		new_object->backing_object_offset =
1315 			orig_object->backing_object_offset + entry->offset;
1316 		new_object->backing_object = source;
1317 	}
1318 	new_object->flags |= orig_object->flags & OBJ_NEEDGIANT;
1319 retry:
1320 	if ((m = TAILQ_FIRST(&orig_object->memq)) != NULL) {
1321 		if (m->pindex < offidxstart) {
1322 			m = vm_page_splay(offidxstart, orig_object->root);
1323 			if ((orig_object->root = m)->pindex < offidxstart)
1324 				m = TAILQ_NEXT(m, listq);
1325 		}
1326 	}
1327 	vm_page_lock_queues();
1328 	for (; m != NULL && (idx = m->pindex - offidxstart) < size;
1329 	    m = m_next) {
1330 		m_next = TAILQ_NEXT(m, listq);
1331 
1332 		/*
1333 		 * We must wait for pending I/O to complete before we can
1334 		 * rename the page.
1335 		 *
1336 		 * We do not have to VM_PROT_NONE the page as mappings should
1337 		 * not be changed by this operation.
1338 		 */
1339 		if ((m->oflags & VPO_BUSY) || m->busy) {
1340 			vm_page_flag_set(m, PG_REFERENCED);
1341 			vm_page_unlock_queues();
1342 			VM_OBJECT_UNLOCK(new_object);
1343 			m->oflags |= VPO_WANTED;
1344 			msleep(m, VM_OBJECT_MTX(orig_object), PVM, "spltwt", 0);
1345 			VM_OBJECT_LOCK(new_object);
1346 			goto retry;
1347 		}
1348 		vm_page_rename(m, new_object, idx);
1349 		/* page automatically made dirty by rename and cache handled */
1350 		vm_page_busy(m);
1351 	}
1352 	vm_page_unlock_queues();
1353 	if (orig_object->type == OBJT_SWAP) {
1354 		/*
1355 		 * swap_pager_copy() can sleep, in which case the orig_object's
1356 		 * and new_object's locks are released and reacquired.
1357 		 */
1358 		swap_pager_copy(orig_object, new_object, offidxstart, 0);
1359 	}
1360 	VM_OBJECT_UNLOCK(orig_object);
1361 	TAILQ_FOREACH(m, &new_object->memq, listq)
1362 		vm_page_wakeup(m);
1363 	VM_OBJECT_UNLOCK(new_object);
1364 	entry->object.vm_object = new_object;
1365 	entry->offset = 0LL;
1366 	vm_object_deallocate(orig_object);
1367 	VM_OBJECT_LOCK(new_object);
1368 }
1369 
1370 #define	OBSC_TEST_ALL_SHADOWED	0x0001
1371 #define	OBSC_COLLAPSE_NOWAIT	0x0002
1372 #define	OBSC_COLLAPSE_WAIT	0x0004
1373 
1374 static int
1375 vm_object_backing_scan(vm_object_t object, int op)
1376 {
1377 	int r = 1;
1378 	vm_page_t p;
1379 	vm_object_t backing_object;
1380 	vm_pindex_t backing_offset_index;
1381 
1382 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1383 	VM_OBJECT_LOCK_ASSERT(object->backing_object, MA_OWNED);
1384 
1385 	backing_object = object->backing_object;
1386 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
1387 
1388 	/*
1389 	 * Initial conditions
1390 	 */
1391 	if (op & OBSC_TEST_ALL_SHADOWED) {
1392 		/*
1393 		 * We do not want to have to test for the existence of
1394 		 * swap pages in the backing object.  XXX but with the
1395 		 * new swapper this would be pretty easy to do.
1396 		 *
1397 		 * XXX what about anonymous MAP_SHARED memory that hasn't
1398 		 * been ZFOD faulted yet?  If we do not test for this, the
1399 		 * shadow test may succeed! XXX
1400 		 */
1401 		if (backing_object->type != OBJT_DEFAULT) {
1402 			return (0);
1403 		}
1404 	}
1405 	if (op & OBSC_COLLAPSE_WAIT) {
1406 		vm_object_set_flag(backing_object, OBJ_DEAD);
1407 	}
1408 
1409 	/*
1410 	 * Our scan
1411 	 */
1412 	p = TAILQ_FIRST(&backing_object->memq);
1413 	while (p) {
1414 		vm_page_t next = TAILQ_NEXT(p, listq);
1415 		vm_pindex_t new_pindex = p->pindex - backing_offset_index;
1416 
1417 		if (op & OBSC_TEST_ALL_SHADOWED) {
1418 			vm_page_t pp;
1419 
1420 			/*
1421 			 * Ignore pages outside the parent object's range
1422 			 * and outside the parent object's mapping of the
1423 			 * backing object.
1424 			 *
1425 			 * note that we do not busy the backing object's
1426 			 * page.
1427 			 */
1428 			if (
1429 			    p->pindex < backing_offset_index ||
1430 			    new_pindex >= object->size
1431 			) {
1432 				p = next;
1433 				continue;
1434 			}
1435 
1436 			/*
1437 			 * See if the parent has the page or if the parent's
1438 			 * object pager has the page.  If the parent has the
1439 			 * page but the page is not valid, the parent's
1440 			 * object pager must have the page.
1441 			 *
1442 			 * If this fails, the parent does not completely shadow
1443 			 * the object and we might as well give up now.
1444 			 */
1445 
1446 			pp = vm_page_lookup(object, new_pindex);
1447 			if (
1448 			    (pp == NULL || pp->valid == 0) &&
1449 			    !vm_pager_has_page(object, new_pindex, NULL, NULL)
1450 			) {
1451 				r = 0;
1452 				break;
1453 			}
1454 		}
1455 
1456 		/*
1457 		 * Check for busy page
1458 		 */
1459 		if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
1460 			vm_page_t pp;
1461 
1462 			if (op & OBSC_COLLAPSE_NOWAIT) {
1463 				if ((p->oflags & VPO_BUSY) ||
1464 				    !p->valid ||
1465 				    p->busy) {
1466 					p = next;
1467 					continue;
1468 				}
1469 			} else if (op & OBSC_COLLAPSE_WAIT) {
1470 				if ((p->oflags & VPO_BUSY) || p->busy) {
1471 					vm_page_lock_queues();
1472 					vm_page_flag_set(p, PG_REFERENCED);
1473 					vm_page_unlock_queues();
1474 					VM_OBJECT_UNLOCK(object);
1475 					p->oflags |= VPO_WANTED;
1476 					msleep(p, VM_OBJECT_MTX(backing_object),
1477 					    PDROP | PVM, "vmocol", 0);
1478 					VM_OBJECT_LOCK(object);
1479 					VM_OBJECT_LOCK(backing_object);
1480 					/*
1481 					 * If we slept, anything could have
1482 					 * happened.  Since the object is
1483 					 * marked dead, the backing offset
1484 					 * should not have changed so we
1485 					 * just restart our scan.
1486 					 */
1487 					p = TAILQ_FIRST(&backing_object->memq);
1488 					continue;
1489 				}
1490 			}
1491 
1492 			KASSERT(
1493 			    p->object == backing_object,
1494 			    ("vm_object_backing_scan: object mismatch")
1495 			);
1496 
1497 			/*
1498 			 * Destroy any associated swap
1499 			 */
1500 			if (backing_object->type == OBJT_SWAP) {
1501 				swap_pager_freespace(
1502 				    backing_object,
1503 				    p->pindex,
1504 				    1
1505 				);
1506 			}
1507 
1508 			if (
1509 			    p->pindex < backing_offset_index ||
1510 			    new_pindex >= object->size
1511 			) {
1512 				/*
1513 				 * Page is out of the parent object's range, we
1514 				 * can simply destroy it.
1515 				 */
1516 				vm_page_lock_queues();
1517 				KASSERT(!pmap_page_is_mapped(p),
1518 				    ("freeing mapped page %p", p));
1519 				if (p->wire_count == 0)
1520 					vm_page_free(p);
1521 				else
1522 					vm_page_remove(p);
1523 				vm_page_unlock_queues();
1524 				p = next;
1525 				continue;
1526 			}
1527 
1528 			pp = vm_page_lookup(object, new_pindex);
1529 			if (
1530 			    pp != NULL ||
1531 			    vm_pager_has_page(object, new_pindex, NULL, NULL)
1532 			) {
1533 				/*
1534 				 * page already exists in parent OR swap exists
1535 				 * for this location in the parent.  Destroy
1536 				 * the original page from the backing object.
1537 				 *
1538 				 * Leave the parent's page alone
1539 				 */
1540 				vm_page_lock_queues();
1541 				KASSERT(!pmap_page_is_mapped(p),
1542 				    ("freeing mapped page %p", p));
1543 				if (p->wire_count == 0)
1544 					vm_page_free(p);
1545 				else
1546 					vm_page_remove(p);
1547 				vm_page_unlock_queues();
1548 				p = next;
1549 				continue;
1550 			}
1551 
1552 			/*
1553 			 * Page does not exist in parent, rename the
1554 			 * page from the backing object to the main object.
1555 			 *
1556 			 * If the page was mapped to a process, it can remain
1557 			 * mapped through the rename.
1558 			 */
1559 			vm_page_lock_queues();
1560 			vm_page_rename(p, object, new_pindex);
1561 			vm_page_unlock_queues();
1562 			/* page automatically made dirty by rename */
1563 		}
1564 		p = next;
1565 	}
1566 	return (r);
1567 }
1568 
1569 
1570 /*
1571  * this version of collapse allows the operation to occur earlier and
1572  * when paging_in_progress is true for an object...  This is not a complete
1573  * operation, but should plug 99.9% of the rest of the leaks.
1574  */
1575 static void
1576 vm_object_qcollapse(vm_object_t object)
1577 {
1578 	vm_object_t backing_object = object->backing_object;
1579 
1580 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1581 	VM_OBJECT_LOCK_ASSERT(backing_object, MA_OWNED);
1582 
1583 	if (backing_object->ref_count != 1)
1584 		return;
1585 
1586 	vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
1587 }
1588 
1589 /*
1590  *	vm_object_collapse:
1591  *
1592  *	Collapse an object with the object backing it.
1593  *	Pages in the backing object are moved into the
1594  *	parent, and the backing object is deallocated.
1595  */
1596 void
1597 vm_object_collapse(vm_object_t object)
1598 {
1599 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1600 
1601 	while (TRUE) {
1602 		vm_object_t backing_object;
1603 
1604 		/*
1605 		 * Verify that the conditions are right for collapse:
1606 		 *
1607 		 * The object exists and the backing object exists.
1608 		 */
1609 		if ((backing_object = object->backing_object) == NULL)
1610 			break;
1611 
1612 		/*
1613 		 * we check the backing object first, because it is most likely
1614 		 * not collapsable.
1615 		 */
1616 		VM_OBJECT_LOCK(backing_object);
1617 		if (backing_object->handle != NULL ||
1618 		    (backing_object->type != OBJT_DEFAULT &&
1619 		     backing_object->type != OBJT_SWAP) ||
1620 		    (backing_object->flags & OBJ_DEAD) ||
1621 		    object->handle != NULL ||
1622 		    (object->type != OBJT_DEFAULT &&
1623 		     object->type != OBJT_SWAP) ||
1624 		    (object->flags & OBJ_DEAD)) {
1625 			VM_OBJECT_UNLOCK(backing_object);
1626 			break;
1627 		}
1628 
1629 		if (
1630 		    object->paging_in_progress != 0 ||
1631 		    backing_object->paging_in_progress != 0
1632 		) {
1633 			vm_object_qcollapse(object);
1634 			VM_OBJECT_UNLOCK(backing_object);
1635 			break;
1636 		}
1637 		/*
1638 		 * We know that we can either collapse the backing object (if
1639 		 * the parent is the only reference to it) or (perhaps) have
1640 		 * the parent bypass the object if the parent happens to shadow
1641 		 * all the resident pages in the entire backing object.
1642 		 *
1643 		 * This is ignoring pager-backed pages such as swap pages.
1644 		 * vm_object_backing_scan fails the shadowing test in this
1645 		 * case.
1646 		 */
1647 		if (backing_object->ref_count == 1) {
1648 			/*
1649 			 * If there is exactly one reference to the backing
1650 			 * object, we can collapse it into the parent.
1651 			 */
1652 			vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1653 
1654 			/*
1655 			 * Move the pager from backing_object to object.
1656 			 */
1657 			if (backing_object->type == OBJT_SWAP) {
1658 				/*
1659 				 * swap_pager_copy() can sleep, in which case
1660 				 * the backing_object's and object's locks are
1661 				 * released and reacquired.
1662 				 */
1663 				swap_pager_copy(
1664 				    backing_object,
1665 				    object,
1666 				    OFF_TO_IDX(object->backing_object_offset), TRUE);
1667 			}
1668 			/*
1669 			 * Object now shadows whatever backing_object did.
1670 			 * Note that the reference to
1671 			 * backing_object->backing_object moves from within
1672 			 * backing_object to within object.
1673 			 */
1674 			LIST_REMOVE(object, shadow_list);
1675 			backing_object->shadow_count--;
1676 			backing_object->generation++;
1677 			if (backing_object->backing_object) {
1678 				VM_OBJECT_LOCK(backing_object->backing_object);
1679 				LIST_REMOVE(backing_object, shadow_list);
1680 				LIST_INSERT_HEAD(
1681 				    &backing_object->backing_object->shadow_head,
1682 				    object, shadow_list);
1683 				/*
1684 				 * The shadow_count has not changed.
1685 				 */
1686 				backing_object->backing_object->generation++;
1687 				VM_OBJECT_UNLOCK(backing_object->backing_object);
1688 			}
1689 			object->backing_object = backing_object->backing_object;
1690 			object->backing_object_offset +=
1691 			    backing_object->backing_object_offset;
1692 
1693 			/*
1694 			 * Discard backing_object.
1695 			 *
1696 			 * Since the backing object has no pages, no pager left,
1697 			 * and no object references within it, all that is
1698 			 * necessary is to dispose of it.
1699 			 */
1700 			KASSERT(backing_object->ref_count == 1, ("backing_object %p was somehow re-referenced during collapse!", backing_object));
1701 			VM_OBJECT_UNLOCK(backing_object);
1702 
1703 			mtx_lock(&vm_object_list_mtx);
1704 			TAILQ_REMOVE(
1705 			    &vm_object_list,
1706 			    backing_object,
1707 			    object_list
1708 			);
1709 			mtx_unlock(&vm_object_list_mtx);
1710 
1711 			uma_zfree(obj_zone, backing_object);
1712 
1713 			object_collapses++;
1714 		} else {
1715 			vm_object_t new_backing_object;
1716 
1717 			/*
1718 			 * If we do not entirely shadow the backing object,
1719 			 * there is nothing we can do so we give up.
1720 			 */
1721 			if (object->resident_page_count != object->size &&
1722 			    vm_object_backing_scan(object,
1723 			    OBSC_TEST_ALL_SHADOWED) == 0) {
1724 				VM_OBJECT_UNLOCK(backing_object);
1725 				break;
1726 			}
1727 
1728 			/*
1729 			 * Make the parent shadow the next object in the
1730 			 * chain.  Deallocating backing_object will not remove
1731 			 * it, since its reference count is at least 2.
1732 			 */
1733 			LIST_REMOVE(object, shadow_list);
1734 			backing_object->shadow_count--;
1735 			backing_object->generation++;
1736 
1737 			new_backing_object = backing_object->backing_object;
1738 			if ((object->backing_object = new_backing_object) != NULL) {
1739 				VM_OBJECT_LOCK(new_backing_object);
1740 				LIST_INSERT_HEAD(
1741 				    &new_backing_object->shadow_head,
1742 				    object,
1743 				    shadow_list
1744 				);
1745 				new_backing_object->shadow_count++;
1746 				new_backing_object->generation++;
1747 				vm_object_reference_locked(new_backing_object);
1748 				VM_OBJECT_UNLOCK(new_backing_object);
1749 				object->backing_object_offset +=
1750 					backing_object->backing_object_offset;
1751 			}
1752 
1753 			/*
1754 			 * Drop the reference count on backing_object. Since
1755 			 * its ref_count was at least 2, it will not vanish.
1756 			 */
1757 			backing_object->ref_count--;
1758 			VM_OBJECT_UNLOCK(backing_object);
1759 			object_bypasses++;
1760 		}
1761 
1762 		/*
1763 		 * Try again with this object's new backing object.
1764 		 */
1765 	}
1766 }
1767 
1768 /*
1769  *	vm_object_page_remove:
1770  *
1771  *	Removes all physical pages in the given range from the
1772  *	object's list of pages.  If the range's end is zero, all
1773  *	physical pages from the range's start to the end of the object
1774  *	are deleted.
1775  *
1776  *	The object must be locked.
1777  */
1778 void
1779 vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
1780     boolean_t clean_only)
1781 {
1782 	vm_page_t p, next;
1783 
1784 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1785 	if (object->resident_page_count == 0)
1786 		return;
1787 
1788 	/*
1789 	 * Since physically-backed objects do not use managed pages, we can't
1790 	 * remove pages from the object (we must instead remove the page
1791 	 * references, and then destroy the object).
1792 	 */
1793 	KASSERT(object->type != OBJT_PHYS || object == kernel_object ||
1794 	    object == kmem_object,
1795 	    ("attempt to remove pages from a physical object"));
1796 
1797 	vm_object_pip_add(object, 1);
1798 again:
1799 	vm_page_lock_queues();
1800 	if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
1801 		if (p->pindex < start) {
1802 			p = vm_page_splay(start, object->root);
1803 			if ((object->root = p)->pindex < start)
1804 				p = TAILQ_NEXT(p, listq);
1805 		}
1806 	}
1807 	/*
1808 	 * Assert: the variable p is either (1) the page with the
1809 	 * least pindex greater than or equal to the parameter pindex
1810 	 * or (2) NULL.
1811 	 */
1812 	for (;
1813 	     p != NULL && (p->pindex < end || end == 0);
1814 	     p = next) {
1815 		next = TAILQ_NEXT(p, listq);
1816 
1817 		if (p->wire_count != 0) {
1818 			pmap_remove_all(p);
1819 			if (!clean_only)
1820 				p->valid = 0;
1821 			continue;
1822 		}
1823 		if (vm_page_sleep_if_busy(p, TRUE, "vmopar"))
1824 			goto again;
1825 		if (clean_only && p->valid) {
1826 			pmap_remove_write(p);
1827 			if (p->valid & p->dirty)
1828 				continue;
1829 		}
1830 		pmap_remove_all(p);
1831 		vm_page_free(p);
1832 	}
1833 	vm_page_unlock_queues();
1834 	vm_object_pip_wakeup(object);
1835 }
1836 
1837 /*
1838  *	Routine:	vm_object_coalesce
1839  *	Function:	Coalesces two objects backing up adjoining
1840  *			regions of memory into a single object.
1841  *
1842  *	returns TRUE if objects were combined.
1843  *
1844  *	NOTE:	Only works at the moment if the second object is NULL -
1845  *		if it's not, which object do we lock first?
1846  *
1847  *	Parameters:
1848  *		prev_object	First object to coalesce
1849  *		prev_offset	Offset into prev_object
1850  *		prev_size	Size of reference to prev_object
1851  *		next_size	Size of reference to the second object
1852  *
1853  *	Conditions:
1854  *	The object must *not* be locked.
1855  */
1856 boolean_t
1857 vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset,
1858 	vm_size_t prev_size, vm_size_t next_size)
1859 {
1860 	vm_pindex_t next_pindex;
1861 
1862 	if (prev_object == NULL)
1863 		return (TRUE);
1864 	VM_OBJECT_LOCK(prev_object);
1865 	if (prev_object->type != OBJT_DEFAULT &&
1866 	    prev_object->type != OBJT_SWAP) {
1867 		VM_OBJECT_UNLOCK(prev_object);
1868 		return (FALSE);
1869 	}
1870 
1871 	/*
1872 	 * Try to collapse the object first
1873 	 */
1874 	vm_object_collapse(prev_object);
1875 
1876 	/*
1877 	 * Can't coalesce if: . more than one reference . paged out . shadows
1878 	 * another object . has a copy elsewhere (any of which mean that the
1879 	 * pages not mapped to prev_entry may be in use anyway)
1880 	 */
1881 	if (prev_object->backing_object != NULL) {
1882 		VM_OBJECT_UNLOCK(prev_object);
1883 		return (FALSE);
1884 	}
1885 
1886 	prev_size >>= PAGE_SHIFT;
1887 	next_size >>= PAGE_SHIFT;
1888 	next_pindex = OFF_TO_IDX(prev_offset) + prev_size;
1889 
1890 	if ((prev_object->ref_count > 1) &&
1891 	    (prev_object->size != next_pindex)) {
1892 		VM_OBJECT_UNLOCK(prev_object);
1893 		return (FALSE);
1894 	}
1895 
1896 	/*
1897 	 * Remove any pages that may still be in the object from a previous
1898 	 * deallocation.
1899 	 */
1900 	if (next_pindex < prev_object->size) {
1901 		vm_object_page_remove(prev_object,
1902 				      next_pindex,
1903 				      next_pindex + next_size, FALSE);
1904 		if (prev_object->type == OBJT_SWAP)
1905 			swap_pager_freespace(prev_object,
1906 					     next_pindex, next_size);
1907 	}
1908 
1909 	/*
1910 	 * Extend the object if necessary.
1911 	 */
1912 	if (next_pindex + next_size > prev_object->size)
1913 		prev_object->size = next_pindex + next_size;
1914 
1915 	VM_OBJECT_UNLOCK(prev_object);
1916 	return (TRUE);
1917 }
1918 
1919 void
1920 vm_object_set_writeable_dirty(vm_object_t object)
1921 {
1922 	struct vnode *vp;
1923 
1924 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1925 	if ((object->flags & OBJ_MIGHTBEDIRTY) != 0)
1926 		return;
1927 	vm_object_set_flag(object, OBJ_MIGHTBEDIRTY);
1928 	if (object->type == OBJT_VNODE &&
1929 	    (vp = (struct vnode *)object->handle) != NULL) {
1930 		VI_LOCK(vp);
1931 		vp->v_iflag |= VI_OBJDIRTY;
1932 		VI_UNLOCK(vp);
1933 	}
1934 }
1935 
1936 #include "opt_ddb.h"
1937 #ifdef DDB
1938 #include <sys/kernel.h>
1939 
1940 #include <sys/cons.h>
1941 
1942 #include <ddb/ddb.h>
1943 
1944 static int
1945 _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
1946 {
1947 	vm_map_t tmpm;
1948 	vm_map_entry_t tmpe;
1949 	vm_object_t obj;
1950 	int entcount;
1951 
1952 	if (map == 0)
1953 		return 0;
1954 
1955 	if (entry == 0) {
1956 		tmpe = map->header.next;
1957 		entcount = map->nentries;
1958 		while (entcount-- && (tmpe != &map->header)) {
1959 			if (_vm_object_in_map(map, object, tmpe)) {
1960 				return 1;
1961 			}
1962 			tmpe = tmpe->next;
1963 		}
1964 	} else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
1965 		tmpm = entry->object.sub_map;
1966 		tmpe = tmpm->header.next;
1967 		entcount = tmpm->nentries;
1968 		while (entcount-- && tmpe != &tmpm->header) {
1969 			if (_vm_object_in_map(tmpm, object, tmpe)) {
1970 				return 1;
1971 			}
1972 			tmpe = tmpe->next;
1973 		}
1974 	} else if ((obj = entry->object.vm_object) != NULL) {
1975 		for (; obj; obj = obj->backing_object)
1976 			if (obj == object) {
1977 				return 1;
1978 			}
1979 	}
1980 	return 0;
1981 }
1982 
1983 static int
1984 vm_object_in_map(vm_object_t object)
1985 {
1986 	struct proc *p;
1987 
1988 	/* sx_slock(&allproc_lock); */
1989 	FOREACH_PROC_IN_SYSTEM(p) {
1990 		if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1991 			continue;
1992 		if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
1993 			/* sx_sunlock(&allproc_lock); */
1994 			return 1;
1995 		}
1996 	}
1997 	/* sx_sunlock(&allproc_lock); */
1998 	if (_vm_object_in_map(kernel_map, object, 0))
1999 		return 1;
2000 	if (_vm_object_in_map(kmem_map, object, 0))
2001 		return 1;
2002 	if (_vm_object_in_map(pager_map, object, 0))
2003 		return 1;
2004 	if (_vm_object_in_map(buffer_map, object, 0))
2005 		return 1;
2006 	return 0;
2007 }
2008 
2009 DB_SHOW_COMMAND(vmochk, vm_object_check)
2010 {
2011 	vm_object_t object;
2012 
2013 	/*
2014 	 * make sure that internal objs are in a map somewhere
2015 	 * and none have zero ref counts.
2016 	 */
2017 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
2018 		if (object->handle == NULL &&
2019 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2020 			if (object->ref_count == 0) {
2021 				db_printf("vmochk: internal obj has zero ref count: %ld\n",
2022 					(long)object->size);
2023 			}
2024 			if (!vm_object_in_map(object)) {
2025 				db_printf(
2026 			"vmochk: internal obj is not in a map: "
2027 			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2028 				    object->ref_count, (u_long)object->size,
2029 				    (u_long)object->size,
2030 				    (void *)object->backing_object);
2031 			}
2032 		}
2033 	}
2034 }
2035 
2036 /*
2037  *	vm_object_print:	[ debug ]
2038  */
2039 DB_SHOW_COMMAND(object, vm_object_print_static)
2040 {
2041 	/* XXX convert args. */
2042 	vm_object_t object = (vm_object_t)addr;
2043 	boolean_t full = have_addr;
2044 
2045 	vm_page_t p;
2046 
2047 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
2048 #define	count	was_count
2049 
2050 	int count;
2051 
2052 	if (object == NULL)
2053 		return;
2054 
2055 	db_iprintf(
2056 	    "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x\n",
2057 	    object, (int)object->type, (uintmax_t)object->size,
2058 	    object->resident_page_count, object->ref_count, object->flags);
2059 	db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n",
2060 	    object->shadow_count,
2061 	    object->backing_object ? object->backing_object->ref_count : 0,
2062 	    object->backing_object, (uintmax_t)object->backing_object_offset);
2063 
2064 	if (!full)
2065 		return;
2066 
2067 	db_indent += 2;
2068 	count = 0;
2069 	TAILQ_FOREACH(p, &object->memq, listq) {
2070 		if (count == 0)
2071 			db_iprintf("memory:=");
2072 		else if (count == 6) {
2073 			db_printf("\n");
2074 			db_iprintf(" ...");
2075 			count = 0;
2076 		} else
2077 			db_printf(",");
2078 		count++;
2079 
2080 		db_printf("(off=0x%jx,page=0x%jx)",
2081 		    (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p));
2082 	}
2083 	if (count != 0)
2084 		db_printf("\n");
2085 	db_indent -= 2;
2086 }
2087 
2088 /* XXX. */
2089 #undef count
2090 
2091 /* XXX need this non-static entry for calling from vm_map_print. */
2092 void
2093 vm_object_print(
2094         /* db_expr_t */ long addr,
2095 	boolean_t have_addr,
2096 	/* db_expr_t */ long count,
2097 	char *modif)
2098 {
2099 	vm_object_print_static(addr, have_addr, count, modif);
2100 }
2101 
2102 DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
2103 {
2104 	vm_object_t object;
2105 	int nl = 0;
2106 	int c;
2107 
2108 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
2109 		vm_pindex_t idx, fidx;
2110 		vm_pindex_t osize;
2111 		vm_paddr_t pa = -1;
2112 		int rcount;
2113 		vm_page_t m;
2114 
2115 		db_printf("new object: %p\n", (void *)object);
2116 		if (nl > 18) {
2117 			c = cngetc();
2118 			if (c != ' ')
2119 				return;
2120 			nl = 0;
2121 		}
2122 		nl++;
2123 		rcount = 0;
2124 		fidx = 0;
2125 		osize = object->size;
2126 		if (osize > 128)
2127 			osize = 128;
2128 		for (idx = 0; idx < osize; idx++) {
2129 			m = vm_page_lookup(object, idx);
2130 			if (m == NULL) {
2131 				if (rcount) {
2132 					db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2133 						(long)fidx, rcount, (long)pa);
2134 					if (nl > 18) {
2135 						c = cngetc();
2136 						if (c != ' ')
2137 							return;
2138 						nl = 0;
2139 					}
2140 					nl++;
2141 					rcount = 0;
2142 				}
2143 				continue;
2144 			}
2145 
2146 
2147 			if (rcount &&
2148 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
2149 				++rcount;
2150 				continue;
2151 			}
2152 			if (rcount) {
2153 				db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2154 					(long)fidx, rcount, (long)pa);
2155 				if (nl > 18) {
2156 					c = cngetc();
2157 					if (c != ' ')
2158 						return;
2159 					nl = 0;
2160 				}
2161 				nl++;
2162 			}
2163 			fidx = idx;
2164 			pa = VM_PAGE_TO_PHYS(m);
2165 			rcount = 1;
2166 		}
2167 		if (rcount) {
2168 			db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2169 				(long)fidx, rcount, (long)pa);
2170 			if (nl > 18) {
2171 				c = cngetc();
2172 				if (c != ' ')
2173 					return;
2174 				nl = 0;
2175 			}
2176 			nl++;
2177 		}
2178 	}
2179 }
2180 #endif /* DDB */
2181