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