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