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