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