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