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