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