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