xref: /dragonfly/sys/vm/vm_object.c (revision e96fb831)
1 /*
2  * (MPSAFE)
3  *
4  * Copyright (c) 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * The Mach Operating System project at Carnegie-Mellon University.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	from: @(#)vm_object.c	8.5 (Berkeley) 3/22/94
39  *
40  *
41  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
42  * All rights reserved.
43  *
44  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
45  *
46  * Permission to use, copy, modify and distribute this software and
47  * its documentation is hereby granted, provided that both the copyright
48  * notice and this permission notice appear in all copies of the
49  * software, derivative works or modified versions, and any portions
50  * thereof, and that both notices appear in supporting documentation.
51  *
52  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
53  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
54  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
55  *
56  * Carnegie Mellon requests users of this software to return to
57  *
58  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
59  *  School of Computer Science
60  *  Carnegie Mellon University
61  *  Pittsburgh PA 15213-3890
62  *
63  * any improvements or extensions that they make and grant Carnegie the
64  * rights to redistribute these changes.
65  *
66  * $FreeBSD: src/sys/vm/vm_object.c,v 1.171.2.8 2003/05/26 19:17:56 alc Exp $
67  */
68 
69 /*
70  *	Virtual memory object module.
71  */
72 
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/proc.h>		/* for curproc, pageproc */
76 #include <sys/thread.h>
77 #include <sys/vnode.h>
78 #include <sys/vmmeter.h>
79 #include <sys/mman.h>
80 #include <sys/mount.h>
81 #include <sys/kernel.h>
82 #include <sys/sysctl.h>
83 #include <sys/refcount.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/vm_zone.h>
97 
98 #define EASY_SCAN_FACTOR	8
99 
100 static void	vm_object_qcollapse(vm_object_t object,
101 				    vm_object_t backing_object);
102 static int	vm_object_page_collect_flush(vm_object_t object, vm_page_t p,
103 					     int pagerflags);
104 static void	vm_object_lock_init(vm_object_t);
105 
106 
107 /*
108  *	Virtual memory objects maintain the actual data
109  *	associated with allocated virtual memory.  A given
110  *	page of memory exists within exactly one object.
111  *
112  *	An object is only deallocated when all "references"
113  *	are given up.  Only one "reference" to a given
114  *	region of an object should be writeable.
115  *
116  *	Associated with each object is a list of all resident
117  *	memory pages belonging to that object; this list is
118  *	maintained by the "vm_page" module, and locked by the object's
119  *	lock.
120  *
121  *	Each object also records a "pager" routine which is
122  *	used to retrieve (and store) pages to the proper backing
123  *	storage.  In addition, objects may be backed by other
124  *	objects from which they were virtual-copied.
125  *
126  *	The only items within the object structure which are
127  *	modified after time of creation are:
128  *		reference count		locked by object's lock
129  *		pager routine		locked by object's lock
130  *
131  */
132 
133 struct object_q vm_object_list;		/* locked by vmobj_token */
134 struct vm_object kernel_object;
135 
136 static long vm_object_count;		/* locked by vmobj_token */
137 extern int vm_pageout_page_count;
138 
139 static long object_collapses;
140 static long object_bypasses;
141 static int next_index;
142 static vm_zone_t obj_zone;
143 static struct vm_zone obj_zone_store;
144 #define VM_OBJECTS_INIT 256
145 static struct vm_object vm_objects_init[VM_OBJECTS_INIT];
146 
147 /*
148  * Misc low level routines
149  */
150 static void
151 vm_object_lock_init(vm_object_t obj)
152 {
153 #if defined(DEBUG_LOCKS)
154 	int i;
155 
156 	obj->debug_hold_bitmap = 0;
157 	obj->debug_hold_ovfl = 0;
158 	for (i = 0; i < VMOBJ_DEBUG_ARRAY_SIZE; i++) {
159 		obj->debug_hold_thrs[i] = NULL;
160 		obj->debug_hold_file[i] = NULL;
161 		obj->debug_hold_line[i] = 0;
162 	}
163 #endif
164 }
165 
166 void
167 vm_object_lock_swap(void)
168 {
169 	lwkt_token_swap();
170 }
171 
172 void
173 vm_object_lock(vm_object_t obj)
174 {
175 	lwkt_gettoken(&obj->token);
176 }
177 
178 /*
179  * Returns TRUE on sucesss
180  */
181 static int
182 vm_object_lock_try(vm_object_t obj)
183 {
184 	return(lwkt_trytoken(&obj->token));
185 }
186 
187 void
188 vm_object_lock_shared(vm_object_t obj)
189 {
190 	lwkt_gettoken_shared(&obj->token);
191 }
192 
193 void
194 vm_object_unlock(vm_object_t obj)
195 {
196 	lwkt_reltoken(&obj->token);
197 }
198 
199 static __inline void
200 vm_object_assert_held(vm_object_t obj)
201 {
202 	ASSERT_LWKT_TOKEN_HELD(&obj->token);
203 }
204 
205 void
206 #ifndef DEBUG_LOCKS
207 vm_object_hold(vm_object_t obj)
208 #else
209 debugvm_object_hold(vm_object_t obj, char *file, int line)
210 #endif
211 {
212 	KKASSERT(obj != NULL);
213 
214 	/*
215 	 * Object must be held (object allocation is stable due to callers
216 	 * context, typically already holding the token on a parent object)
217 	 * prior to potentially blocking on the lock, otherwise the object
218 	 * can get ripped away from us.
219 	 */
220 	refcount_acquire(&obj->hold_count);
221 	vm_object_lock(obj);
222 
223 #if defined(DEBUG_LOCKS)
224 	int i;
225 	u_int mask;
226 
227 	for (;;) {
228 		mask = ~obj->debug_hold_bitmap;
229 		cpu_ccfence();
230 		if (mask == 0xFFFFFFFFU) {
231 			if (obj->debug_hold_ovfl == 0)
232 				obj->debug_hold_ovfl = 1;
233 			break;
234 		}
235 		i = ffs(mask) - 1;
236 		if (atomic_cmpset_int(&obj->debug_hold_bitmap, ~mask,
237 				      ~mask | (1 << i))) {
238 			obj->debug_hold_bitmap |= (1 << i);
239 			obj->debug_hold_thrs[i] = curthread;
240 			obj->debug_hold_file[i] = file;
241 			obj->debug_hold_line[i] = line;
242 			break;
243 		}
244 	}
245 #endif
246 }
247 
248 int
249 #ifndef DEBUG_LOCKS
250 vm_object_hold_try(vm_object_t obj)
251 #else
252 debugvm_object_hold_try(vm_object_t obj, char *file, int line)
253 #endif
254 {
255 	KKASSERT(obj != NULL);
256 
257 	/*
258 	 * Object must be held (object allocation is stable due to callers
259 	 * context, typically already holding the token on a parent object)
260 	 * prior to potentially blocking on the lock, otherwise the object
261 	 * can get ripped away from us.
262 	 */
263 	refcount_acquire(&obj->hold_count);
264 	if (vm_object_lock_try(obj) == 0) {
265 		if (refcount_release(&obj->hold_count)) {
266 			if (obj->ref_count == 0 && (obj->flags & OBJ_DEAD))
267 				zfree(obj_zone, obj);
268 		}
269 		return(0);
270 	}
271 
272 #if defined(DEBUG_LOCKS)
273 	int i;
274 	u_int mask;
275 
276 	for (;;) {
277 		mask = ~obj->debug_hold_bitmap;
278 		cpu_ccfence();
279 		if (mask == 0xFFFFFFFFU) {
280 			if (obj->debug_hold_ovfl == 0)
281 				obj->debug_hold_ovfl = 1;
282 			break;
283 		}
284 		i = ffs(mask) - 1;
285 		if (atomic_cmpset_int(&obj->debug_hold_bitmap, ~mask,
286 				      ~mask | (1 << i))) {
287 			obj->debug_hold_bitmap |= (1 << i);
288 			obj->debug_hold_thrs[i] = curthread;
289 			obj->debug_hold_file[i] = file;
290 			obj->debug_hold_line[i] = line;
291 			break;
292 		}
293 	}
294 #endif
295 	return(1);
296 }
297 
298 void
299 #ifndef DEBUG_LOCKS
300 vm_object_hold_shared(vm_object_t obj)
301 #else
302 debugvm_object_hold_shared(vm_object_t obj, char *file, int line)
303 #endif
304 {
305 	KKASSERT(obj != NULL);
306 
307 	/*
308 	 * Object must be held (object allocation is stable due to callers
309 	 * context, typically already holding the token on a parent object)
310 	 * prior to potentially blocking on the lock, otherwise the object
311 	 * can get ripped away from us.
312 	 */
313 	refcount_acquire(&obj->hold_count);
314 	vm_object_lock_shared(obj);
315 
316 #if defined(DEBUG_LOCKS)
317 	int i;
318 	u_int mask;
319 
320 	for (;;) {
321 		mask = ~obj->debug_hold_bitmap;
322 		cpu_ccfence();
323 		if (mask == 0xFFFFFFFFU) {
324 			if (obj->debug_hold_ovfl == 0)
325 				obj->debug_hold_ovfl = 1;
326 			break;
327 		}
328 		i = ffs(mask) - 1;
329 		if (atomic_cmpset_int(&obj->debug_hold_bitmap, ~mask,
330 				      ~mask | (1 << i))) {
331 			obj->debug_hold_bitmap |= (1 << i);
332 			obj->debug_hold_thrs[i] = curthread;
333 			obj->debug_hold_file[i] = file;
334 			obj->debug_hold_line[i] = line;
335 			break;
336 		}
337 	}
338 #endif
339 }
340 
341 /*
342  * Drop the token and hold_count on the object.
343  */
344 void
345 vm_object_drop(vm_object_t obj)
346 {
347 	if (obj == NULL)
348 		return;
349 
350 #if defined(DEBUG_LOCKS)
351 	int found = 0;
352 	int i;
353 
354 	for (i = 0; i < VMOBJ_DEBUG_ARRAY_SIZE; i++) {
355 		if ((obj->debug_hold_bitmap & (1 << i)) &&
356 		    (obj->debug_hold_thrs[i] == curthread)) {
357 			obj->debug_hold_bitmap &= ~(1 << i);
358 			obj->debug_hold_thrs[i] = NULL;
359 			obj->debug_hold_file[i] = NULL;
360 			obj->debug_hold_line[i] = 0;
361 			found = 1;
362 			break;
363 		}
364 	}
365 
366 	if (found == 0 && obj->debug_hold_ovfl == 0)
367 		panic("vm_object: attempt to drop hold on non-self-held obj");
368 #endif
369 
370 	/*
371 	 * No new holders should be possible once we drop hold_count 1->0 as
372 	 * there is no longer any way to reference the object.
373 	 */
374 	KKASSERT(obj->hold_count > 0);
375 	if (refcount_release(&obj->hold_count)) {
376 		if (obj->ref_count == 0 && (obj->flags & OBJ_DEAD)) {
377 			vm_object_unlock(obj);
378 			zfree(obj_zone, obj);
379 		} else {
380 			vm_object_unlock(obj);
381 		}
382 	} else {
383 		vm_object_unlock(obj);
384 	}
385 }
386 
387 /*
388  * Initialize a freshly allocated object, returning a held object.
389  *
390  * Used only by vm_object_allocate() and zinitna().
391  *
392  * No requirements.
393  */
394 void
395 _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
396 {
397 	int incr;
398 
399 	RB_INIT(&object->rb_memq);
400 	LIST_INIT(&object->shadow_head);
401 	lwkt_token_init(&object->token, "vmobj");
402 
403 	object->type = type;
404 	object->size = size;
405 	object->ref_count = 1;
406 	object->hold_count = 0;
407 	object->flags = 0;
408 	if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
409 		vm_object_set_flag(object, OBJ_ONEMAPPING);
410 	object->paging_in_progress = 0;
411 	object->resident_page_count = 0;
412 	object->agg_pv_list_count = 0;
413 	object->shadow_count = 0;
414 #ifdef SMP
415 	/* cpu localization twist */
416 	object->pg_color = (int)(intptr_t)curthread;
417 #else
418 	object->pg_color = next_index;
419 #endif
420 	if ( size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
421 		incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
422 	else
423 		incr = size;
424 	next_index = (next_index + incr) & PQ_L2_MASK;
425 	object->handle = NULL;
426 	object->backing_object = NULL;
427 	object->backing_object_offset = (vm_ooffset_t)0;
428 
429 	object->generation++;
430 	object->swblock_count = 0;
431 	RB_INIT(&object->swblock_root);
432 	vm_object_lock_init(object);
433 
434 	vm_object_hold(object);
435 	lwkt_gettoken(&vmobj_token);
436 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
437 	vm_object_count++;
438 	lwkt_reltoken(&vmobj_token);
439 }
440 
441 /*
442  * Initialize the VM objects module.
443  *
444  * Called from the low level boot code only.
445  */
446 void
447 vm_object_init(void)
448 {
449 	TAILQ_INIT(&vm_object_list);
450 
451 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(KvaEnd),
452 			    &kernel_object);
453 	vm_object_drop(&kernel_object);
454 
455 	obj_zone = &obj_zone_store;
456 	zbootinit(obj_zone, "VM OBJECT", sizeof (struct vm_object),
457 		vm_objects_init, VM_OBJECTS_INIT);
458 }
459 
460 void
461 vm_object_init2(void)
462 {
463 	zinitna(obj_zone, NULL, NULL, 0, 0, ZONE_PANICFAIL, 1);
464 }
465 
466 /*
467  * Allocate and return a new object of the specified type and size.
468  *
469  * No requirements.
470  */
471 vm_object_t
472 vm_object_allocate(objtype_t type, vm_pindex_t size)
473 {
474 	vm_object_t result;
475 
476 	result = (vm_object_t) zalloc(obj_zone);
477 
478 	_vm_object_allocate(type, size, result);
479 	vm_object_drop(result);
480 
481 	return (result);
482 }
483 
484 /*
485  * This version returns a held object, allowing further atomic initialization
486  * of the object.
487  */
488 vm_object_t
489 vm_object_allocate_hold(objtype_t type, vm_pindex_t size)
490 {
491 	vm_object_t result;
492 
493 	result = (vm_object_t) zalloc(obj_zone);
494 
495 	_vm_object_allocate(type, size, result);
496 
497 	return (result);
498 }
499 
500 /*
501  * Add an additional reference to a vm_object.  The object must already be
502  * held.  The original non-lock version is no longer supported.  The object
503  * must NOT be chain locked by anyone at the time the reference is added.
504  *
505  * Referencing a chain-locked object can blow up the fairly sensitive
506  * ref_count and shadow_count tests in the deallocator.  Most callers
507  * will call vm_object_chain_wait() prior to calling
508  * vm_object_reference_locked() to avoid the case.
509  *
510  * The object must be held.
511  */
512 void
513 vm_object_reference_locked(vm_object_t object)
514 {
515 	KKASSERT(object != NULL);
516 	ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
517 	KKASSERT((object->flags & OBJ_CHAINLOCK) == 0);
518 	object->ref_count++;
519 	if (object->type == OBJT_VNODE) {
520 		vref(object->handle);
521 		/* XXX what if the vnode is being destroyed? */
522 	}
523 }
524 
525 /*
526  * Object OBJ_CHAINLOCK lock handling.
527  *
528  * The caller can chain-lock backing objects recursively and then
529  * use vm_object_chain_release_all() to undo the whole chain.
530  *
531  * Chain locks are used to prevent collapses and are only applicable
532  * to OBJT_DEFAULT and OBJT_SWAP objects.  Chain locking operations
533  * on other object types are ignored.  This is also important because
534  * it allows e.g. the vnode underlying a memory mapping to take concurrent
535  * faults.
536  *
537  * The object must usually be held on entry, though intermediate
538  * objects need not be held on release.
539  */
540 void
541 vm_object_chain_wait(vm_object_t object)
542 {
543 	ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
544 	while (object->flags & OBJ_CHAINLOCK) {
545 		vm_object_set_flag(object, OBJ_CHAINWANT);
546 		tsleep(object, 0, "objchain", 0);
547 	}
548 }
549 
550 void
551 vm_object_chain_acquire(vm_object_t object)
552 {
553 	if (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP) {
554 		vm_object_chain_wait(object);
555 		vm_object_set_flag(object, OBJ_CHAINLOCK);
556 	}
557 }
558 
559 void
560 vm_object_chain_release(vm_object_t object)
561 {
562 	ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
563 	if (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP) {
564 		KKASSERT(object->flags & OBJ_CHAINLOCK);
565 		if (object->flags & OBJ_CHAINWANT) {
566 			vm_object_clear_flag(object,
567 					     OBJ_CHAINLOCK | OBJ_CHAINWANT);
568 			wakeup(object);
569 		} else {
570 			vm_object_clear_flag(object, OBJ_CHAINLOCK);
571 		}
572 	}
573 }
574 
575 /*
576  * This releases the entire chain of objects from first_object to and
577  * including stopobj, flowing through object->backing_object.
578  *
579  * We release stopobj first as an optimization as this object is most
580  * likely to be shared across multiple processes.
581  */
582 void
583 vm_object_chain_release_all(vm_object_t first_object, vm_object_t stopobj)
584 {
585 	vm_object_t backing_object;
586 	vm_object_t object;
587 
588 	vm_object_chain_release(stopobj);
589 	object = first_object;
590 
591 	while (object != stopobj) {
592 		KKASSERT(object);
593 		if (object != first_object)
594 			vm_object_hold(object);
595 		backing_object = object->backing_object;
596 		vm_object_chain_release(object);
597 		if (object != first_object)
598 			vm_object_drop(object);
599 		object = backing_object;
600 	}
601 }
602 
603 /*
604  * Dereference an object and its underlying vnode.
605  *
606  * The object must be held and will be held on return.
607  */
608 static void
609 vm_object_vndeallocate(vm_object_t object)
610 {
611 	struct vnode *vp = (struct vnode *) object->handle;
612 
613 	KASSERT(object->type == OBJT_VNODE,
614 	    ("vm_object_vndeallocate: not a vnode object"));
615 	KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
616 	ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
617 #ifdef INVARIANTS
618 	if (object->ref_count == 0) {
619 		vprint("vm_object_vndeallocate", vp);
620 		panic("vm_object_vndeallocate: bad object reference count");
621 	}
622 #endif
623 	object->ref_count--;
624 	if (object->ref_count == 0)
625 		vclrflags(vp, VTEXT);
626 	vrele(vp);
627 }
628 
629 /*
630  * Release a reference to the specified object, gained either through a
631  * vm_object_allocate or a vm_object_reference call.  When all references
632  * are gone, storage associated with this object may be relinquished.
633  *
634  * The caller does not have to hold the object locked but must have control
635  * over the reference in question in order to guarantee that the object
636  * does not get ripped out from under us.
637  */
638 void
639 vm_object_deallocate(vm_object_t object)
640 {
641 	if (object) {
642 		vm_object_hold(object);
643 		vm_object_deallocate_locked(object);
644 		vm_object_drop(object);
645 	}
646 }
647 
648 void
649 vm_object_deallocate_locked(vm_object_t object)
650 {
651 	struct vm_object_dealloc_list *dlist = NULL;
652 	struct vm_object_dealloc_list *dtmp;
653 	vm_object_t temp;
654 	int must_drop = 0;
655 
656 	/*
657 	 * We may chain deallocate object, but additional objects may
658 	 * collect on the dlist which also have to be deallocated.  We
659 	 * must avoid a recursion, vm_object chains can get deep.
660 	 */
661 again:
662 	while (object != NULL) {
663 #if 0
664 		/*
665 		 * Don't rip a ref_count out from under an object undergoing
666 		 * collapse, it will confuse the collapse code.
667 		 */
668 		vm_object_chain_wait(object);
669 #endif
670 		if (object->type == OBJT_VNODE) {
671 			vm_object_vndeallocate(object);
672 			break;
673 		}
674 
675 		if (object->ref_count == 0) {
676 			panic("vm_object_deallocate: object deallocated "
677 			      "too many times: %d", object->type);
678 		}
679 		if (object->ref_count > 2) {
680 			object->ref_count--;
681 			break;
682 		}
683 
684 		/*
685 		 * Here on ref_count of one or two, which are special cases for
686 		 * objects.
687 		 *
688 		 * Nominal ref_count > 1 case if the second ref is not from
689 		 * a shadow.
690 		 */
691 		if (object->ref_count == 2 && object->shadow_count == 0) {
692 			vm_object_set_flag(object, OBJ_ONEMAPPING);
693 			object->ref_count--;
694 			break;
695 		}
696 
697 		/*
698 		 * If the second ref is from a shadow we chain along it
699 		 * upwards if object's handle is exhausted.
700 		 *
701 		 * We have to decrement object->ref_count before potentially
702 		 * collapsing the first shadow object or the collapse code
703 		 * will not be able to handle the degenerate case to remove
704 		 * object.  However, if we do it too early the object can
705 		 * get ripped out from under us.
706 		 */
707 		if (object->ref_count == 2 && object->shadow_count == 1 &&
708 		    object->handle == NULL && (object->type == OBJT_DEFAULT ||
709 					       object->type == OBJT_SWAP)) {
710 			temp = LIST_FIRST(&object->shadow_head);
711 			KKASSERT(temp != NULL);
712 			vm_object_hold(temp);
713 
714 			/*
715 			 * Wait for any paging to complete so the collapse
716 			 * doesn't (or isn't likely to) qcollapse.  pip
717 			 * waiting must occur before we acquire the
718 			 * chainlock.
719 			 */
720 			while (
721 				temp->paging_in_progress ||
722 				object->paging_in_progress
723 			) {
724 				vm_object_pip_wait(temp, "objde1");
725 				vm_object_pip_wait(object, "objde2");
726 			}
727 
728 			/*
729 			 * If the parent is locked we have to give up, as
730 			 * otherwise we would be acquiring locks in the
731 			 * wrong order and potentially deadlock.
732 			 */
733 			if (temp->flags & OBJ_CHAINLOCK) {
734 				vm_object_drop(temp);
735 				goto skip;
736 			}
737 			vm_object_chain_acquire(temp);
738 
739 			/*
740 			 * Recheck/retry after the hold and the paging
741 			 * wait, both of which can block us.
742 			 */
743 			if (object->ref_count != 2 ||
744 			    object->shadow_count != 1 ||
745 			    object->handle ||
746 			    LIST_FIRST(&object->shadow_head) != temp ||
747 			    (object->type != OBJT_DEFAULT &&
748 			     object->type != OBJT_SWAP)) {
749 				vm_object_chain_release(temp);
750 				vm_object_drop(temp);
751 				continue;
752 			}
753 
754 			/*
755 			 * We can safely drop object's ref_count now.
756 			 */
757 			KKASSERT(object->ref_count == 2);
758 			object->ref_count--;
759 
760 			/*
761 			 * If our single parent is not collapseable just
762 			 * decrement ref_count (2->1) and stop.
763 			 */
764 			if (temp->handle || (temp->type != OBJT_DEFAULT &&
765 					     temp->type != OBJT_SWAP)) {
766 				vm_object_chain_release(temp);
767 				vm_object_drop(temp);
768 				break;
769 			}
770 
771 			/*
772 			 * At this point we have already dropped object's
773 			 * ref_count so it is possible for a race to
774 			 * deallocate obj out from under us.  Any collapse
775 			 * will re-check the situation.  We must not block
776 			 * until we are able to collapse.
777 			 *
778 			 * Bump temp's ref_count to avoid an unwanted
779 			 * degenerate recursion (can't call
780 			 * vm_object_reference_locked() because it asserts
781 			 * that CHAINLOCK is not set).
782 			 */
783 			temp->ref_count++;
784 			KKASSERT(temp->ref_count > 1);
785 
786 			/*
787 			 * Collapse temp, then deallocate the extra ref
788 			 * formally.
789 			 */
790 			vm_object_collapse(temp, &dlist);
791 			vm_object_chain_release(temp);
792 			if (must_drop) {
793 				vm_object_lock_swap();
794 				vm_object_drop(object);
795 			}
796 			object = temp;
797 			must_drop = 1;
798 			continue;
799 		}
800 
801 		/*
802 		 * Drop the ref and handle termination on the 1->0 transition.
803 		 * We may have blocked above so we have to recheck.
804 		 */
805 skip:
806 		KKASSERT(object->ref_count != 0);
807 		if (object->ref_count >= 2) {
808 			object->ref_count--;
809 			break;
810 		}
811 		KKASSERT(object->ref_count == 1);
812 
813 		/*
814 		 * 1->0 transition.  Chain through the backing_object.
815 		 * Maintain the ref until we've located the backing object,
816 		 * then re-check.
817 		 */
818 		while ((temp = object->backing_object) != NULL) {
819 			vm_object_hold(temp);
820 			if (temp == object->backing_object)
821 				break;
822 			vm_object_drop(temp);
823 		}
824 
825 		/*
826 		 * 1->0 transition verified, retry if ref_count is no longer
827 		 * 1.  Otherwise disconnect the backing_object (temp) and
828 		 * clean up.
829 		 */
830 		if (object->ref_count != 1) {
831 			vm_object_drop(temp);
832 			continue;
833 		}
834 
835 		/*
836 		 * It shouldn't be possible for the object to be chain locked
837 		 * if we're removing the last ref on it.
838 		 */
839 		KKASSERT((object->flags & OBJ_CHAINLOCK) == 0);
840 
841 		if (temp) {
842 			LIST_REMOVE(object, shadow_list);
843 			temp->shadow_count--;
844 			temp->generation++;
845 			object->backing_object = NULL;
846 		}
847 
848 		--object->ref_count;
849 		if ((object->flags & OBJ_DEAD) == 0)
850 			vm_object_terminate(object);
851 		if (must_drop && temp)
852 			vm_object_lock_swap();
853 		if (must_drop)
854 			vm_object_drop(object);
855 		object = temp;
856 		must_drop = 1;
857 	}
858 	if (must_drop && object)
859 		vm_object_drop(object);
860 
861 	/*
862 	 * Additional tail recursion on dlist.  Avoid a recursion.  Objects
863 	 * on the dlist have a hold count but are not locked.
864 	 */
865 	if ((dtmp = dlist) != NULL) {
866 		dlist = dtmp->next;
867 		object = dtmp->object;
868 		kfree(dtmp, M_TEMP);
869 
870 		vm_object_lock(object);	/* already held, add lock */
871 		must_drop = 1;		/* and we're responsible for it */
872 		goto again;
873 	}
874 }
875 
876 /*
877  * Destroy the specified object, freeing up related resources.
878  *
879  * The object must have zero references.
880  *
881  * The object must held.  The caller is responsible for dropping the object
882  * after terminate returns.  Terminate does NOT drop the object.
883  */
884 static int vm_object_terminate_callback(vm_page_t p, void *data);
885 
886 void
887 vm_object_terminate(vm_object_t object)
888 {
889 	/*
890 	 * Make sure no one uses us.  Once we set OBJ_DEAD we should be
891 	 * able to safely block.
892 	 */
893 	ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
894 	KKASSERT((object->flags & OBJ_DEAD) == 0);
895 	vm_object_set_flag(object, OBJ_DEAD);
896 
897 	/*
898 	 * Wait for the pageout daemon to be done with the object
899 	 */
900 	vm_object_pip_wait(object, "objtrm1");
901 
902 	KASSERT(!object->paging_in_progress,
903 		("vm_object_terminate: pageout in progress"));
904 
905 	/*
906 	 * Clean and free the pages, as appropriate. All references to the
907 	 * object are gone, so we don't need to lock it.
908 	 */
909 	if (object->type == OBJT_VNODE) {
910 		struct vnode *vp;
911 
912 		/*
913 		 * Clean pages and flush buffers.
914 		 */
915 		vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
916 
917 		vp = (struct vnode *) object->handle;
918 		vinvalbuf(vp, V_SAVE, 0, 0);
919 	}
920 
921 	/*
922 	 * Wait for any I/O to complete, after which there had better not
923 	 * be any references left on the object.
924 	 */
925 	vm_object_pip_wait(object, "objtrm2");
926 
927 	if (object->ref_count != 0) {
928 		panic("vm_object_terminate: object with references, "
929 		      "ref_count=%d", object->ref_count);
930 	}
931 
932 	/*
933 	 * Now free any remaining pages. For internal objects, this also
934 	 * removes them from paging queues. Don't free wired pages, just
935 	 * remove them from the object.
936 	 */
937 	vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
938 				vm_object_terminate_callback, NULL);
939 
940 	/*
941 	 * Let the pager know object is dead.
942 	 */
943 	vm_pager_deallocate(object);
944 
945 	/*
946 	 * Wait for the object hold count to hit 1, clean out pages as
947 	 * we go.  vmobj_token interlocks any race conditions that might
948 	 * pick the object up from the vm_object_list after we have cleared
949 	 * rb_memq.
950 	 */
951 	for (;;) {
952 		if (RB_ROOT(&object->rb_memq) == NULL)
953 			break;
954 		kprintf("vm_object_terminate: Warning, object %p "
955 			"still has %d pages\n",
956 			object, object->resident_page_count);
957 		vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
958 					vm_object_terminate_callback, NULL);
959 	}
960 
961 	/*
962 	 * There had better not be any pages left
963 	 */
964 	KKASSERT(object->resident_page_count == 0);
965 
966 	/*
967 	 * Remove the object from the global object list.
968 	 */
969 	lwkt_gettoken(&vmobj_token);
970 	TAILQ_REMOVE(&vm_object_list, object, object_list);
971 	vm_object_count--;
972 	lwkt_reltoken(&vmobj_token);
973 	vm_object_dead_wakeup(object);
974 
975 	if (object->ref_count != 0) {
976 		panic("vm_object_terminate2: object with references, "
977 		      "ref_count=%d", object->ref_count);
978 	}
979 
980 	/*
981 	 * NOTE: The object hold_count is at least 1, so we cannot zfree()
982 	 *	 the object here.  See vm_object_drop().
983 	 */
984 }
985 
986 /*
987  * The caller must hold the object.
988  */
989 static int
990 vm_object_terminate_callback(vm_page_t p, void *data __unused)
991 {
992 	vm_object_t object;
993 
994 	object = p->object;
995 	vm_page_busy_wait(p, TRUE, "vmpgtrm");
996 	if (object != p->object) {
997 		kprintf("vm_object_terminate: Warning: Encountered "
998 			"busied page %p on queue %d\n", p, p->queue);
999 		vm_page_wakeup(p);
1000 	} else if (p->wire_count == 0) {
1001 		vm_page_free(p);
1002 		mycpu->gd_cnt.v_pfree++;
1003 	} else {
1004 		if (p->queue != PQ_NONE)
1005 			kprintf("vm_object_terminate: Warning: Encountered "
1006 				"wired page %p on queue %d\n", p, p->queue);
1007 		vm_page_remove(p);
1008 		vm_page_wakeup(p);
1009 	}
1010 	lwkt_yield();
1011 	return(0);
1012 }
1013 
1014 /*
1015  * The object is dead but still has an object<->pager association.  Sleep
1016  * and return.  The caller typically retests the association in a loop.
1017  *
1018  * The caller must hold the object.
1019  */
1020 void
1021 vm_object_dead_sleep(vm_object_t object, const char *wmesg)
1022 {
1023 	ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
1024 	if (object->handle) {
1025 		vm_object_set_flag(object, OBJ_DEADWNT);
1026 		tsleep(object, 0, wmesg, 0);
1027 		/* object may be invalid after this point */
1028 	}
1029 }
1030 
1031 /*
1032  * Wakeup anyone waiting for the object<->pager disassociation on
1033  * a dead object.
1034  *
1035  * The caller must hold the object.
1036  */
1037 void
1038 vm_object_dead_wakeup(vm_object_t object)
1039 {
1040 	ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
1041 	if (object->flags & OBJ_DEADWNT) {
1042 		vm_object_clear_flag(object, OBJ_DEADWNT);
1043 		wakeup(object);
1044 	}
1045 }
1046 
1047 /*
1048  * Clean all dirty pages in the specified range of object.  Leaves page
1049  * on whatever queue it is currently on.   If NOSYNC is set then do not
1050  * write out pages with PG_NOSYNC set (originally comes from MAP_NOSYNC),
1051  * leaving the object dirty.
1052  *
1053  * When stuffing pages asynchronously, allow clustering.  XXX we need a
1054  * synchronous clustering mode implementation.
1055  *
1056  * Odd semantics: if start == end, we clean everything.
1057  *
1058  * The object must be locked? XXX
1059  */
1060 static int vm_object_page_clean_pass1(struct vm_page *p, void *data);
1061 static int vm_object_page_clean_pass2(struct vm_page *p, void *data);
1062 
1063 void
1064 vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
1065 		     int flags)
1066 {
1067 	struct rb_vm_page_scan_info info;
1068 	struct vnode *vp;
1069 	int wholescan;
1070 	int pagerflags;
1071 	int generation;
1072 
1073 	vm_object_hold(object);
1074 	if (object->type != OBJT_VNODE ||
1075 	    (object->flags & OBJ_MIGHTBEDIRTY) == 0) {
1076 		vm_object_drop(object);
1077 		return;
1078 	}
1079 
1080 	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ?
1081 			VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
1082 	pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
1083 
1084 	vp = object->handle;
1085 
1086 	/*
1087 	 * Interlock other major object operations.  This allows us to
1088 	 * temporarily clear OBJ_WRITEABLE and OBJ_MIGHTBEDIRTY.
1089 	 */
1090 	vm_object_set_flag(object, OBJ_CLEANING);
1091 
1092 	/*
1093 	 * Handle 'entire object' case
1094 	 */
1095 	info.start_pindex = start;
1096 	if (end == 0) {
1097 		info.end_pindex = object->size - 1;
1098 	} else {
1099 		info.end_pindex = end - 1;
1100 	}
1101 	wholescan = (start == 0 && info.end_pindex == object->size - 1);
1102 	info.limit = flags;
1103 	info.pagerflags = pagerflags;
1104 	info.object = object;
1105 
1106 	/*
1107 	 * If cleaning the entire object do a pass to mark the pages read-only.
1108 	 * If everything worked out ok, clear OBJ_WRITEABLE and
1109 	 * OBJ_MIGHTBEDIRTY.
1110 	 */
1111 	if (wholescan) {
1112 		info.error = 0;
1113 		vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
1114 					vm_object_page_clean_pass1, &info);
1115 		if (info.error == 0) {
1116 			vm_object_clear_flag(object,
1117 					     OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
1118 			if (object->type == OBJT_VNODE &&
1119 			    (vp = (struct vnode *)object->handle) != NULL) {
1120 				if (vp->v_flag & VOBJDIRTY)
1121 					vclrflags(vp, VOBJDIRTY);
1122 			}
1123 		}
1124 	}
1125 
1126 	/*
1127 	 * Do a pass to clean all the dirty pages we find.
1128 	 */
1129 	do {
1130 		info.error = 0;
1131 		generation = object->generation;
1132 		vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
1133 					vm_object_page_clean_pass2, &info);
1134 	} while (info.error || generation != object->generation);
1135 
1136 	vm_object_clear_flag(object, OBJ_CLEANING);
1137 	vm_object_drop(object);
1138 }
1139 
1140 /*
1141  * The caller must hold the object.
1142  */
1143 static
1144 int
1145 vm_object_page_clean_pass1(struct vm_page *p, void *data)
1146 {
1147 	struct rb_vm_page_scan_info *info = data;
1148 
1149 	vm_page_flag_set(p, PG_CLEANCHK);
1150 	if ((info->limit & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
1151 		info->error = 1;
1152 	} else if (vm_page_busy_try(p, FALSE) == 0) {
1153 		vm_page_protect(p, VM_PROT_READ);	/* must not block */
1154 		vm_page_wakeup(p);
1155 	} else {
1156 		info->error = 1;
1157 	}
1158 	lwkt_yield();
1159 	return(0);
1160 }
1161 
1162 /*
1163  * The caller must hold the object
1164  */
1165 static
1166 int
1167 vm_object_page_clean_pass2(struct vm_page *p, void *data)
1168 {
1169 	struct rb_vm_page_scan_info *info = data;
1170 	int generation;
1171 
1172 	/*
1173 	 * Do not mess with pages that were inserted after we started
1174 	 * the cleaning pass.
1175 	 */
1176 	if ((p->flags & PG_CLEANCHK) == 0)
1177 		goto done;
1178 
1179 	generation = info->object->generation;
1180 	vm_page_busy_wait(p, TRUE, "vpcwai");
1181 	if (p->object != info->object ||
1182 	    info->object->generation != generation) {
1183 		info->error = 1;
1184 		vm_page_wakeup(p);
1185 		goto done;
1186 	}
1187 
1188 	/*
1189 	 * Before wasting time traversing the pmaps, check for trivial
1190 	 * cases where the page cannot be dirty.
1191 	 */
1192 	if (p->valid == 0 || (p->queue - p->pc) == PQ_CACHE) {
1193 		KKASSERT((p->dirty & p->valid) == 0);
1194 		vm_page_wakeup(p);
1195 		goto done;
1196 	}
1197 
1198 	/*
1199 	 * Check whether the page is dirty or not.  The page has been set
1200 	 * to be read-only so the check will not race a user dirtying the
1201 	 * page.
1202 	 */
1203 	vm_page_test_dirty(p);
1204 	if ((p->dirty & p->valid) == 0) {
1205 		vm_page_flag_clear(p, PG_CLEANCHK);
1206 		vm_page_wakeup(p);
1207 		goto done;
1208 	}
1209 
1210 	/*
1211 	 * If we have been asked to skip nosync pages and this is a
1212 	 * nosync page, skip it.  Note that the object flags were
1213 	 * not cleared in this case (because pass1 will have returned an
1214 	 * error), so we do not have to set them.
1215 	 */
1216 	if ((info->limit & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
1217 		vm_page_flag_clear(p, PG_CLEANCHK);
1218 		vm_page_wakeup(p);
1219 		goto done;
1220 	}
1221 
1222 	/*
1223 	 * Flush as many pages as we can.  PG_CLEANCHK will be cleared on
1224 	 * the pages that get successfully flushed.  Set info->error if
1225 	 * we raced an object modification.
1226 	 */
1227 	vm_object_page_collect_flush(info->object, p, info->pagerflags);
1228 done:
1229 	lwkt_yield();
1230 	return(0);
1231 }
1232 
1233 /*
1234  * Collect the specified page and nearby pages and flush them out.
1235  * The number of pages flushed is returned.  The passed page is busied
1236  * by the caller and we are responsible for its disposition.
1237  *
1238  * The caller must hold the object.
1239  */
1240 static int
1241 vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int pagerflags)
1242 {
1243 	int runlen;
1244 	int error;
1245 	int maxf;
1246 	int chkb;
1247 	int maxb;
1248 	int i;
1249 	vm_pindex_t pi;
1250 	vm_page_t maf[vm_pageout_page_count];
1251 	vm_page_t mab[vm_pageout_page_count];
1252 	vm_page_t ma[vm_pageout_page_count];
1253 
1254 	ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
1255 
1256 	pi = p->pindex;
1257 
1258 	maxf = 0;
1259 	for(i = 1; i < vm_pageout_page_count; i++) {
1260 		vm_page_t tp;
1261 
1262 		tp = vm_page_lookup_busy_try(object, pi + i, TRUE, &error);
1263 		if (error)
1264 			break;
1265 		if (tp == NULL)
1266 			break;
1267 		if ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
1268 		    (tp->flags & PG_CLEANCHK) == 0) {
1269 			vm_page_wakeup(tp);
1270 			break;
1271 		}
1272 		if ((tp->queue - tp->pc) == PQ_CACHE) {
1273 			vm_page_flag_clear(tp, PG_CLEANCHK);
1274 			vm_page_wakeup(tp);
1275 			break;
1276 		}
1277 		vm_page_test_dirty(tp);
1278 		if ((tp->dirty & tp->valid) == 0) {
1279 			vm_page_flag_clear(tp, PG_CLEANCHK);
1280 			vm_page_wakeup(tp);
1281 			break;
1282 		}
1283 		maf[i - 1] = tp;
1284 		maxf++;
1285 	}
1286 
1287 	maxb = 0;
1288 	chkb = vm_pageout_page_count -  maxf;
1289 	/*
1290 	 * NOTE: chkb can be 0
1291 	 */
1292 	for(i = 1; chkb && i < chkb; i++) {
1293 		vm_page_t tp;
1294 
1295 		tp = vm_page_lookup_busy_try(object, pi - i, TRUE, &error);
1296 		if (error)
1297 			break;
1298 		if (tp == NULL)
1299 			break;
1300 		if ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
1301 		    (tp->flags & PG_CLEANCHK) == 0) {
1302 			vm_page_wakeup(tp);
1303 			break;
1304 		}
1305 		if ((tp->queue - tp->pc) == PQ_CACHE) {
1306 			vm_page_flag_clear(tp, PG_CLEANCHK);
1307 			vm_page_wakeup(tp);
1308 			break;
1309 		}
1310 		vm_page_test_dirty(tp);
1311 		if ((tp->dirty & tp->valid) == 0) {
1312 			vm_page_flag_clear(tp, PG_CLEANCHK);
1313 			vm_page_wakeup(tp);
1314 			break;
1315 		}
1316 		mab[i - 1] = tp;
1317 		maxb++;
1318 	}
1319 
1320 	/*
1321 	 * All pages in the maf[] and mab[] array are busied.
1322 	 */
1323 	for (i = 0; i < maxb; i++) {
1324 		int index = (maxb - i) - 1;
1325 		ma[index] = mab[i];
1326 		vm_page_flag_clear(ma[index], PG_CLEANCHK);
1327 	}
1328 	vm_page_flag_clear(p, PG_CLEANCHK);
1329 	ma[maxb] = p;
1330 	for(i = 0; i < maxf; i++) {
1331 		int index = (maxb + i) + 1;
1332 		ma[index] = maf[i];
1333 		vm_page_flag_clear(ma[index], PG_CLEANCHK);
1334 	}
1335 	runlen = maxb + maxf + 1;
1336 
1337 	for (i = 0; i < runlen; i++)
1338 		vm_page_hold(ma[i]);
1339 
1340 	vm_pageout_flush(ma, runlen, pagerflags);
1341 
1342 	for (i = 0; i < runlen; i++) {
1343 		if (ma[i]->valid & ma[i]->dirty) {
1344 			vm_page_protect(ma[i], VM_PROT_READ);
1345 			vm_page_flag_set(ma[i], PG_CLEANCHK);
1346 
1347 			/*
1348 			 * maxf will end up being the actual number of pages
1349 			 * we wrote out contiguously, non-inclusive of the
1350 			 * first page.  We do not count look-behind pages.
1351 			 */
1352 			if (i >= maxb + 1 && (maxf > i - maxb - 1))
1353 				maxf = i - maxb - 1;
1354 		}
1355 		vm_page_unhold(ma[i]);
1356 	}
1357 	return(maxf + 1);
1358 }
1359 
1360 /*
1361  * Same as vm_object_pmap_copy, except range checking really
1362  * works, and is meant for small sections of an object.
1363  *
1364  * This code protects resident pages by making them read-only
1365  * and is typically called on a fork or split when a page
1366  * is converted to copy-on-write.
1367  *
1368  * NOTE: If the page is already at VM_PROT_NONE, calling
1369  * vm_page_protect will have no effect.
1370  */
1371 void
1372 vm_object_pmap_copy_1(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1373 {
1374 	vm_pindex_t idx;
1375 	vm_page_t p;
1376 
1377 	if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
1378 		return;
1379 
1380 	vm_object_hold(object);
1381 	for (idx = start; idx < end; idx++) {
1382 		p = vm_page_lookup(object, idx);
1383 		if (p == NULL)
1384 			continue;
1385 		vm_page_protect(p, VM_PROT_READ);
1386 	}
1387 	vm_object_drop(object);
1388 }
1389 
1390 /*
1391  * Removes all physical pages in the specified object range from all
1392  * physical maps.
1393  *
1394  * The object must *not* be locked.
1395  */
1396 
1397 static int vm_object_pmap_remove_callback(vm_page_t p, void *data);
1398 
1399 void
1400 vm_object_pmap_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1401 {
1402 	struct rb_vm_page_scan_info info;
1403 
1404 	if (object == NULL)
1405 		return;
1406 	info.start_pindex = start;
1407 	info.end_pindex = end - 1;
1408 
1409 	vm_object_hold(object);
1410 	vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
1411 				vm_object_pmap_remove_callback, &info);
1412 	if (start == 0 && end == object->size)
1413 		vm_object_clear_flag(object, OBJ_WRITEABLE);
1414 	vm_object_drop(object);
1415 }
1416 
1417 /*
1418  * The caller must hold the object
1419  */
1420 static int
1421 vm_object_pmap_remove_callback(vm_page_t p, void *data __unused)
1422 {
1423 	vm_page_protect(p, VM_PROT_NONE);
1424 	return(0);
1425 }
1426 
1427 /*
1428  * Implements the madvise function at the object/page level.
1429  *
1430  * MADV_WILLNEED	(any object)
1431  *
1432  *	Activate the specified pages if they are resident.
1433  *
1434  * MADV_DONTNEED	(any object)
1435  *
1436  *	Deactivate the specified pages if they are resident.
1437  *
1438  * MADV_FREE	(OBJT_DEFAULT/OBJT_SWAP objects, OBJ_ONEMAPPING only)
1439  *
1440  *	Deactivate and clean the specified pages if they are
1441  *	resident.  This permits the process to reuse the pages
1442  *	without faulting or the kernel to reclaim the pages
1443  *	without I/O.
1444  *
1445  * No requirements.
1446  */
1447 void
1448 vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
1449 {
1450 	vm_pindex_t end, tpindex;
1451 	vm_object_t tobject;
1452 	vm_object_t xobj;
1453 	vm_page_t m;
1454 	int error;
1455 
1456 	if (object == NULL)
1457 		return;
1458 
1459 	end = pindex + count;
1460 
1461 	vm_object_hold(object);
1462 	tobject = object;
1463 
1464 	/*
1465 	 * Locate and adjust resident pages
1466 	 */
1467 	for (; pindex < end; pindex += 1) {
1468 relookup:
1469 		if (tobject != object)
1470 			vm_object_drop(tobject);
1471 		tobject = object;
1472 		tpindex = pindex;
1473 shadowlookup:
1474 		/*
1475 		 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
1476 		 * and those pages must be OBJ_ONEMAPPING.
1477 		 */
1478 		if (advise == MADV_FREE) {
1479 			if ((tobject->type != OBJT_DEFAULT &&
1480 			     tobject->type != OBJT_SWAP) ||
1481 			    (tobject->flags & OBJ_ONEMAPPING) == 0) {
1482 				continue;
1483 			}
1484 		}
1485 
1486 		m = vm_page_lookup_busy_try(tobject, tpindex, TRUE, &error);
1487 
1488 		if (error) {
1489 			vm_page_sleep_busy(m, TRUE, "madvpo");
1490 			goto relookup;
1491 		}
1492 		if (m == NULL) {
1493 			/*
1494 			 * There may be swap even if there is no backing page
1495 			 */
1496 			if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1497 				swap_pager_freespace(tobject, tpindex, 1);
1498 
1499 			/*
1500 			 * next object
1501 			 */
1502 			while ((xobj = tobject->backing_object) != NULL) {
1503 				KKASSERT(xobj != object);
1504 				vm_object_hold(xobj);
1505 				if (xobj == tobject->backing_object)
1506 					break;
1507 				vm_object_drop(xobj);
1508 			}
1509 			if (xobj == NULL)
1510 				continue;
1511 			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
1512 			if (tobject != object) {
1513 				vm_object_lock_swap();
1514 				vm_object_drop(tobject);
1515 			}
1516 			tobject = xobj;
1517 			goto shadowlookup;
1518 		}
1519 
1520 		/*
1521 		 * If the page is not in a normal active state, we skip it.
1522 		 * If the page is not managed there are no page queues to
1523 		 * mess with.  Things can break if we mess with pages in
1524 		 * any of the below states.
1525 		 */
1526 		if (
1527 		    /*m->hold_count ||*/
1528 		    m->wire_count ||
1529 		    (m->flags & PG_UNMANAGED) ||
1530 		    m->valid != VM_PAGE_BITS_ALL
1531 		) {
1532 			vm_page_wakeup(m);
1533 			continue;
1534 		}
1535 
1536 		/*
1537 		 * Theoretically once a page is known not to be busy, an
1538 		 * interrupt cannot come along and rip it out from under us.
1539 		 */
1540 
1541 		if (advise == MADV_WILLNEED) {
1542 			vm_page_activate(m);
1543 		} else if (advise == MADV_DONTNEED) {
1544 			vm_page_dontneed(m);
1545 		} else if (advise == MADV_FREE) {
1546 			/*
1547 			 * Mark the page clean.  This will allow the page
1548 			 * to be freed up by the system.  However, such pages
1549 			 * are often reused quickly by malloc()/free()
1550 			 * so we do not do anything that would cause
1551 			 * a page fault if we can help it.
1552 			 *
1553 			 * Specifically, we do not try to actually free
1554 			 * the page now nor do we try to put it in the
1555 			 * cache (which would cause a page fault on reuse).
1556 			 *
1557 			 * But we do make the page is freeable as we
1558 			 * can without actually taking the step of unmapping
1559 			 * it.
1560 			 */
1561 			pmap_clear_modify(m);
1562 			m->dirty = 0;
1563 			m->act_count = 0;
1564 			vm_page_dontneed(m);
1565 			if (tobject->type == OBJT_SWAP)
1566 				swap_pager_freespace(tobject, tpindex, 1);
1567 		}
1568 		vm_page_wakeup(m);
1569 	}
1570 	if (tobject != object)
1571 		vm_object_drop(tobject);
1572 	vm_object_drop(object);
1573 }
1574 
1575 /*
1576  * Create a new object which is backed by the specified existing object
1577  * range.  Replace the pointer and offset that was pointing at the existing
1578  * object with the pointer/offset for the new object.
1579  *
1580  * No other requirements.
1581  */
1582 void
1583 vm_object_shadow(vm_object_t *objectp, vm_ooffset_t *offset, vm_size_t length,
1584 		 int addref)
1585 {
1586 	vm_object_t source;
1587 	vm_object_t result;
1588 
1589 	source = *objectp;
1590 
1591 	/*
1592 	 * Don't create the new object if the old object isn't shared.
1593 	 * We have to chain wait before adding the reference to avoid
1594 	 * racing a collapse or deallocation.
1595 	 *
1596 	 * Add the additional ref to source here to avoid racing a later
1597 	 * collapse or deallocation. Clear the ONEMAPPING flag whether
1598 	 * addref is TRUE or not in this case because the original object
1599 	 * will be shadowed.
1600 	 */
1601 	if (source) {
1602 		vm_object_hold(source);
1603 		vm_object_chain_wait(source);
1604 		if (source->ref_count == 1 &&
1605 		    source->handle == NULL &&
1606 		    (source->type == OBJT_DEFAULT ||
1607 		     source->type == OBJT_SWAP)) {
1608 			vm_object_drop(source);
1609 			if (addref) {
1610 				vm_object_reference_locked(source);
1611 				vm_object_clear_flag(source, OBJ_ONEMAPPING);
1612 			}
1613 			return;
1614 		}
1615 		vm_object_reference_locked(source);
1616 		vm_object_clear_flag(source, OBJ_ONEMAPPING);
1617 	}
1618 
1619 	/*
1620 	 * Allocate a new object with the given length.  The new object
1621 	 * is returned referenced but we may have to add another one.
1622 	 * If we are adding a second reference we must clear OBJ_ONEMAPPING.
1623 	 * (typically because the caller is about to clone a vm_map_entry).
1624 	 *
1625 	 * The source object currently has an extra reference to prevent
1626 	 * collapses into it while we mess with its shadow list, which
1627 	 * we will remove later in this routine.
1628 	 */
1629 	if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL)
1630 		panic("vm_object_shadow: no object for shadowing");
1631 	vm_object_hold(result);
1632 	if (addref) {
1633 		vm_object_reference_locked(result);
1634 		vm_object_clear_flag(result, OBJ_ONEMAPPING);
1635 	}
1636 
1637 	/*
1638 	 * The new object shadows the source object.  Chain wait before
1639 	 * adjusting shadow_count or the shadow list to avoid races.
1640 	 *
1641 	 * Try to optimize the result object's page color when shadowing
1642 	 * in order to maintain page coloring consistency in the combined
1643 	 * shadowed object.
1644 	 */
1645 	KKASSERT(result->backing_object == NULL);
1646 	result->backing_object = source;
1647 	if (source) {
1648 		vm_object_chain_wait(source);
1649 		LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1650 		source->shadow_count++;
1651 		source->generation++;
1652 #ifdef SMP
1653 		/* cpu localization twist */
1654 		result->pg_color = (int)(intptr_t)curthread;
1655 #else
1656 		result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) &
1657 				   PQ_L2_MASK;
1658 #endif
1659 	}
1660 
1661 	/*
1662 	 * Adjust the return storage.  Drop the ref on source before
1663 	 * returning.
1664 	 */
1665 	result->backing_object_offset = *offset;
1666 	vm_object_drop(result);
1667 	*offset = 0;
1668 	if (source) {
1669 		vm_object_deallocate_locked(source);
1670 		vm_object_drop(source);
1671 	}
1672 
1673 	/*
1674 	 * Return the new things
1675 	 */
1676 	*objectp = result;
1677 }
1678 
1679 #define	OBSC_TEST_ALL_SHADOWED	0x0001
1680 #define	OBSC_COLLAPSE_NOWAIT	0x0002
1681 #define	OBSC_COLLAPSE_WAIT	0x0004
1682 
1683 static int vm_object_backing_scan_callback(vm_page_t p, void *data);
1684 
1685 /*
1686  * The caller must hold the object.
1687  */
1688 static __inline int
1689 vm_object_backing_scan(vm_object_t object, vm_object_t backing_object, int op)
1690 {
1691 	struct rb_vm_page_scan_info info;
1692 
1693 	vm_object_assert_held(object);
1694 	vm_object_assert_held(backing_object);
1695 
1696 	KKASSERT(backing_object == object->backing_object);
1697 	info.backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
1698 
1699 	/*
1700 	 * Initial conditions
1701 	 */
1702 	if (op & OBSC_TEST_ALL_SHADOWED) {
1703 		/*
1704 		 * We do not want to have to test for the existence of
1705 		 * swap pages in the backing object.  XXX but with the
1706 		 * new swapper this would be pretty easy to do.
1707 		 *
1708 		 * XXX what about anonymous MAP_SHARED memory that hasn't
1709 		 * been ZFOD faulted yet?  If we do not test for this, the
1710 		 * shadow test may succeed! XXX
1711 		 */
1712 		if (backing_object->type != OBJT_DEFAULT)
1713 			return(0);
1714 	}
1715 	if (op & OBSC_COLLAPSE_WAIT) {
1716 		KKASSERT((backing_object->flags & OBJ_DEAD) == 0);
1717 		vm_object_set_flag(backing_object, OBJ_DEAD);
1718 		lwkt_gettoken(&vmobj_token);
1719 		TAILQ_REMOVE(&vm_object_list, backing_object, object_list);
1720 		vm_object_count--;
1721 		lwkt_reltoken(&vmobj_token);
1722 		vm_object_dead_wakeup(backing_object);
1723 	}
1724 
1725 	/*
1726 	 * Our scan.   We have to retry if a negative error code is returned,
1727 	 * otherwise 0 or 1 will be returned in info.error.  0 Indicates that
1728 	 * the scan had to be stopped because the parent does not completely
1729 	 * shadow the child.
1730 	 */
1731 	info.object = object;
1732 	info.backing_object = backing_object;
1733 	info.limit = op;
1734 	do {
1735 		info.error = 1;
1736 		vm_page_rb_tree_RB_SCAN(&backing_object->rb_memq, NULL,
1737 					vm_object_backing_scan_callback,
1738 					&info);
1739 	} while (info.error < 0);
1740 
1741 	return(info.error);
1742 }
1743 
1744 /*
1745  * The caller must hold the object.
1746  */
1747 static int
1748 vm_object_backing_scan_callback(vm_page_t p, void *data)
1749 {
1750 	struct rb_vm_page_scan_info *info = data;
1751 	vm_object_t backing_object;
1752 	vm_object_t object;
1753 	vm_pindex_t pindex;
1754 	vm_pindex_t new_pindex;
1755 	vm_pindex_t backing_offset_index;
1756 	int op;
1757 
1758 	pindex = p->pindex;
1759 	new_pindex = pindex - info->backing_offset_index;
1760 	op = info->limit;
1761 	object = info->object;
1762 	backing_object = info->backing_object;
1763 	backing_offset_index = info->backing_offset_index;
1764 
1765 	if (op & OBSC_TEST_ALL_SHADOWED) {
1766 		vm_page_t pp;
1767 
1768 		/*
1769 		 * Ignore pages outside the parent object's range
1770 		 * and outside the parent object's mapping of the
1771 		 * backing object.
1772 		 *
1773 		 * note that we do not busy the backing object's
1774 		 * page.
1775 		 */
1776 		if (pindex < backing_offset_index ||
1777 		    new_pindex >= object->size
1778 		) {
1779 			return(0);
1780 		}
1781 
1782 		/*
1783 		 * See if the parent has the page or if the parent's
1784 		 * object pager has the page.  If the parent has the
1785 		 * page but the page is not valid, the parent's
1786 		 * object pager must have the page.
1787 		 *
1788 		 * If this fails, the parent does not completely shadow
1789 		 * the object and we might as well give up now.
1790 		 */
1791 		pp = vm_page_lookup(object, new_pindex);
1792 		if ((pp == NULL || pp->valid == 0) &&
1793 		    !vm_pager_has_page(object, new_pindex)
1794 		) {
1795 			info->error = 0;	/* problemo */
1796 			return(-1);		/* stop the scan */
1797 		}
1798 	}
1799 
1800 	/*
1801 	 * Check for busy page.  Note that we may have lost (p) when we
1802 	 * possibly blocked above.
1803 	 */
1804 	if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
1805 		vm_page_t pp;
1806 
1807 		if (vm_page_busy_try(p, TRUE)) {
1808 			if (op & OBSC_COLLAPSE_NOWAIT) {
1809 				return(0);
1810 			} else {
1811 				/*
1812 				 * If we slept, anything could have
1813 				 * happened.   Ask that the scan be restarted.
1814 				 *
1815 				 * Since the object is marked dead, the
1816 				 * backing offset should not have changed.
1817 				 */
1818 				vm_page_sleep_busy(p, TRUE, "vmocol");
1819 				info->error = -1;
1820 				return(-1);
1821 			}
1822 		}
1823 
1824 		/*
1825 		 * If (p) is no longer valid restart the scan.
1826 		 */
1827 		if (p->object != backing_object || p->pindex != pindex) {
1828 			kprintf("vm_object_backing_scan: Warning: page "
1829 				"%p ripped out from under us\n", p);
1830 			vm_page_wakeup(p);
1831 			info->error = -1;
1832 			return(-1);
1833 		}
1834 
1835 		if (op & OBSC_COLLAPSE_NOWAIT) {
1836 			if (p->valid == 0 /*|| p->hold_count*/ ||
1837 			    p->wire_count) {
1838 				vm_page_wakeup(p);
1839 				return(0);
1840 			}
1841 		} else {
1842 			/* XXX what if p->valid == 0 , hold_count, etc? */
1843 		}
1844 
1845 		KASSERT(
1846 		    p->object == backing_object,
1847 		    ("vm_object_qcollapse(): object mismatch")
1848 		);
1849 
1850 		/*
1851 		 * Destroy any associated swap
1852 		 */
1853 		if (backing_object->type == OBJT_SWAP)
1854 			swap_pager_freespace(backing_object, p->pindex, 1);
1855 
1856 		if (
1857 		    p->pindex < backing_offset_index ||
1858 		    new_pindex >= object->size
1859 		) {
1860 			/*
1861 			 * Page is out of the parent object's range, we
1862 			 * can simply destroy it.
1863 			 */
1864 			vm_page_protect(p, VM_PROT_NONE);
1865 			vm_page_free(p);
1866 			return(0);
1867 		}
1868 
1869 		pp = vm_page_lookup(object, new_pindex);
1870 		if (pp != NULL || vm_pager_has_page(object, new_pindex)) {
1871 			/*
1872 			 * page already exists in parent OR swap exists
1873 			 * for this location in the parent.  Destroy
1874 			 * the original page from the backing object.
1875 			 *
1876 			 * Leave the parent's page alone
1877 			 */
1878 			vm_page_protect(p, VM_PROT_NONE);
1879 			vm_page_free(p);
1880 			return(0);
1881 		}
1882 
1883 		/*
1884 		 * Page does not exist in parent, rename the
1885 		 * page from the backing object to the main object.
1886 		 *
1887 		 * If the page was mapped to a process, it can remain
1888 		 * mapped through the rename.
1889 		 */
1890 		if ((p->queue - p->pc) == PQ_CACHE)
1891 			vm_page_deactivate(p);
1892 
1893 		vm_page_rename(p, object, new_pindex);
1894 		vm_page_wakeup(p);
1895 		/* page automatically made dirty by rename */
1896 	}
1897 	return(0);
1898 }
1899 
1900 /*
1901  * This version of collapse allows the operation to occur earlier and
1902  * when paging_in_progress is true for an object...  This is not a complete
1903  * operation, but should plug 99.9% of the rest of the leaks.
1904  *
1905  * The caller must hold the object and backing_object and both must be
1906  * chainlocked.
1907  *
1908  * (only called from vm_object_collapse)
1909  */
1910 static void
1911 vm_object_qcollapse(vm_object_t object, vm_object_t backing_object)
1912 {
1913 	if (backing_object->ref_count == 1) {
1914 		backing_object->ref_count += 2;
1915 		vm_object_backing_scan(object, backing_object,
1916 				       OBSC_COLLAPSE_NOWAIT);
1917 		backing_object->ref_count -= 2;
1918 	}
1919 }
1920 
1921 /*
1922  * Collapse an object with the object backing it.  Pages in the backing
1923  * object are moved into the parent, and the backing object is deallocated.
1924  * Any conflict is resolved in favor of the parent's existing pages.
1925  *
1926  * object must be held and chain-locked on call.
1927  *
1928  * The caller must have an extra ref on object to prevent a race from
1929  * destroying it during the collapse.
1930  */
1931 void
1932 vm_object_collapse(vm_object_t object, struct vm_object_dealloc_list **dlistp)
1933 {
1934 	struct vm_object_dealloc_list *dlist = NULL;
1935 	vm_object_t backing_object;
1936 
1937 	/*
1938 	 * Only one thread is attempting a collapse at any given moment.
1939 	 * There are few restrictions for (object) that callers of this
1940 	 * function check so reentrancy is likely.
1941 	 */
1942 	KKASSERT(object != NULL);
1943 	vm_object_assert_held(object);
1944 	KKASSERT(object->flags & OBJ_CHAINLOCK);
1945 
1946 	for (;;) {
1947 		vm_object_t bbobj;
1948 		int dodealloc;
1949 
1950 		/*
1951 		 * We have to hold the backing object, check races.
1952 		 */
1953 		while ((backing_object = object->backing_object) != NULL) {
1954 			vm_object_hold(backing_object);
1955 			if (backing_object == object->backing_object)
1956 				break;
1957 			vm_object_drop(backing_object);
1958 		}
1959 
1960 		/*
1961 		 * No backing object?  Nothing to collapse then.
1962 		 */
1963 		if (backing_object == NULL)
1964 			break;
1965 
1966 		/*
1967 		 * You can't collapse with a non-default/non-swap object.
1968 		 */
1969 		if (backing_object->type != OBJT_DEFAULT &&
1970 		    backing_object->type != OBJT_SWAP) {
1971 			vm_object_drop(backing_object);
1972 			backing_object = NULL;
1973 			break;
1974 		}
1975 
1976 		/*
1977 		 * Chain-lock the backing object too because if we
1978 		 * successfully merge its pages into the top object we
1979 		 * will collapse backing_object->backing_object as the
1980 		 * new backing_object.  Re-check that it is still our
1981 		 * backing object.
1982 		 */
1983 		vm_object_chain_acquire(backing_object);
1984 		if (backing_object != object->backing_object) {
1985 			vm_object_chain_release(backing_object);
1986 			vm_object_drop(backing_object);
1987 			continue;
1988 		}
1989 
1990 		/*
1991 		 * we check the backing object first, because it is most likely
1992 		 * not collapsable.
1993 		 */
1994 		if (backing_object->handle != NULL ||
1995 		    (backing_object->type != OBJT_DEFAULT &&
1996 		     backing_object->type != OBJT_SWAP) ||
1997 		    (backing_object->flags & OBJ_DEAD) ||
1998 		    object->handle != NULL ||
1999 		    (object->type != OBJT_DEFAULT &&
2000 		     object->type != OBJT_SWAP) ||
2001 		    (object->flags & OBJ_DEAD)) {
2002 			break;
2003 		}
2004 
2005 		/*
2006 		 * If paging is in progress we can't do a normal collapse.
2007 		 */
2008 		if (
2009 		    object->paging_in_progress != 0 ||
2010 		    backing_object->paging_in_progress != 0
2011 		) {
2012 			vm_object_qcollapse(object, backing_object);
2013 			break;
2014 		}
2015 
2016 		/*
2017 		 * We know that we can either collapse the backing object (if
2018 		 * the parent is the only reference to it) or (perhaps) have
2019 		 * the parent bypass the object if the parent happens to shadow
2020 		 * all the resident pages in the entire backing object.
2021 		 *
2022 		 * This is ignoring pager-backed pages such as swap pages.
2023 		 * vm_object_backing_scan fails the shadowing test in this
2024 		 * case.
2025 		 */
2026 		if (backing_object->ref_count == 1) {
2027 			/*
2028 			 * If there is exactly one reference to the backing
2029 			 * object, we can collapse it into the parent.
2030 			 */
2031 			KKASSERT(object->backing_object == backing_object);
2032 			vm_object_backing_scan(object, backing_object,
2033 					       OBSC_COLLAPSE_WAIT);
2034 
2035 			/*
2036 			 * Move the pager from backing_object to object.
2037 			 */
2038 			if (backing_object->type == OBJT_SWAP) {
2039 				vm_object_pip_add(backing_object, 1);
2040 
2041 				/*
2042 				 * scrap the paging_offset junk and do a
2043 				 * discrete copy.  This also removes major
2044 				 * assumptions about how the swap-pager
2045 				 * works from where it doesn't belong.  The
2046 				 * new swapper is able to optimize the
2047 				 * destroy-source case.
2048 				 */
2049 				vm_object_pip_add(object, 1);
2050 				swap_pager_copy(backing_object, object,
2051 				    OFF_TO_IDX(object->backing_object_offset),
2052 				    TRUE);
2053 				vm_object_pip_wakeup(object);
2054 				vm_object_pip_wakeup(backing_object);
2055 			}
2056 
2057 			/*
2058 			 * Object now shadows whatever backing_object did.
2059 			 * Remove object from backing_object's shadow_list.
2060 			 */
2061 			LIST_REMOVE(object, shadow_list);
2062 			KKASSERT(object->backing_object == backing_object);
2063 			backing_object->shadow_count--;
2064 			backing_object->generation++;
2065 
2066 			/*
2067 			 * backing_object->backing_object moves from within
2068 			 * backing_object to within object.
2069 			 */
2070 			while ((bbobj = backing_object->backing_object) != NULL) {
2071 				vm_object_hold(bbobj);
2072 				if (bbobj == backing_object->backing_object)
2073 					break;
2074 				vm_object_drop(bbobj);
2075 			}
2076 			if (bbobj) {
2077 				LIST_REMOVE(backing_object, shadow_list);
2078 				bbobj->shadow_count--;
2079 				bbobj->generation++;
2080 				backing_object->backing_object = NULL;
2081 			}
2082 			object->backing_object = bbobj;
2083 			if (bbobj) {
2084 				LIST_INSERT_HEAD(&bbobj->shadow_head,
2085 						 object, shadow_list);
2086 				bbobj->shadow_count++;
2087 				bbobj->generation++;
2088 			}
2089 
2090 			object->backing_object_offset +=
2091 				backing_object->backing_object_offset;
2092 
2093 			vm_object_drop(bbobj);
2094 
2095 			/*
2096 			 * Discard the old backing_object.  Nothing should be
2097 			 * able to ref it, other than a vm_map_split(),
2098 			 * and vm_map_split() will stall on our chain lock.
2099 			 * And we control the parent so it shouldn't be
2100 			 * possible for it to go away either.
2101 			 *
2102 			 * Since the backing object has no pages, no pager
2103 			 * left, and no object references within it, all
2104 			 * that is necessary is to dispose of it.
2105 			 */
2106 			KASSERT(backing_object->ref_count == 1,
2107 				("backing_object %p was somehow "
2108 				 "re-referenced during collapse!",
2109 				 backing_object));
2110 			KASSERT(RB_EMPTY(&backing_object->rb_memq),
2111 				("backing_object %p somehow has left "
2112 				 "over pages during collapse!",
2113 				 backing_object));
2114 
2115 			/*
2116 			 * The object can be destroyed.
2117 			 *
2118 			 * XXX just fall through and dodealloc instead
2119 			 *     of forcing destruction?
2120 			 */
2121 			--backing_object->ref_count;
2122 			if ((backing_object->flags & OBJ_DEAD) == 0)
2123 				vm_object_terminate(backing_object);
2124 			object_collapses++;
2125 			dodealloc = 0;
2126 		} else {
2127 			/*
2128 			 * If we do not entirely shadow the backing object,
2129 			 * there is nothing we can do so we give up.
2130 			 */
2131 			if (vm_object_backing_scan(object, backing_object,
2132 						OBSC_TEST_ALL_SHADOWED) == 0) {
2133 				break;
2134 			}
2135 
2136 			/*
2137 			 * bbobj is backing_object->backing_object.  Since
2138 			 * object completely shadows backing_object we can
2139 			 * bypass it and become backed by bbobj instead.
2140 			 */
2141 			while ((bbobj = backing_object->backing_object) != NULL) {
2142 				vm_object_hold(bbobj);
2143 				if (bbobj == backing_object->backing_object)
2144 					break;
2145 				vm_object_drop(bbobj);
2146 			}
2147 
2148 			/*
2149 			 * Make object shadow bbobj instead of backing_object.
2150 			 * Remove object from backing_object's shadow list.
2151 			 *
2152 			 * Deallocating backing_object will not remove
2153 			 * it, since its reference count is at least 2.
2154 			 */
2155 			KKASSERT(object->backing_object == backing_object);
2156 			LIST_REMOVE(object, shadow_list);
2157 			backing_object->shadow_count--;
2158 			backing_object->generation++;
2159 
2160 			/*
2161 			 * Add a ref to bbobj, bbobj now shadows object.
2162 			 *
2163 			 * NOTE: backing_object->backing_object still points
2164 			 *	 to bbobj.  That relationship remains intact
2165 			 *	 because backing_object has > 1 ref, so
2166 			 *	 someone else is pointing to it (hence why
2167 			 *	 we can't collapse it into object and can
2168 			 *	 only handle the all-shadowed bypass case).
2169 			 */
2170 			if (bbobj) {
2171 				vm_object_chain_wait(bbobj);
2172 				vm_object_reference_locked(bbobj);
2173 				LIST_INSERT_HEAD(&bbobj->shadow_head,
2174 						 object, shadow_list);
2175 				bbobj->shadow_count++;
2176 				bbobj->generation++;
2177 				object->backing_object_offset +=
2178 					backing_object->backing_object_offset;
2179 				object->backing_object = bbobj;
2180 				vm_object_drop(bbobj);
2181 			} else {
2182 				object->backing_object = NULL;
2183 			}
2184 
2185 			/*
2186 			 * Drop the reference count on backing_object.  To
2187 			 * handle ref_count races properly we can't assume
2188 			 * that the ref_count is still at least 2 so we
2189 			 * have to actually call vm_object_deallocate()
2190 			 * (after clearing the chainlock).
2191 			 */
2192 			object_bypasses++;
2193 			dodealloc = 1;
2194 		}
2195 
2196 		/*
2197 		 * Ok, we want to loop on the new object->bbobj association,
2198 		 * possibly collapsing it further.  However if dodealloc is
2199 		 * non-zero we have to deallocate the backing_object which
2200 		 * itself can potentially undergo a collapse, creating a
2201 		 * recursion depth issue with the LWKT token subsystem.
2202 		 *
2203 		 * In the case where we must deallocate the backing_object
2204 		 * it is possible now that the backing_object has a single
2205 		 * shadow count on some other object (not represented here
2206 		 * as yet), since it no longer shadows us.  Thus when we
2207 		 * call vm_object_deallocate() it may attempt to collapse
2208 		 * itself into its remaining parent.
2209 		 */
2210 		if (dodealloc) {
2211 			struct vm_object_dealloc_list *dtmp;
2212 
2213 			vm_object_chain_release(backing_object);
2214 			vm_object_unlock(backing_object);
2215 			/* backing_object remains held */
2216 
2217 			/*
2218 			 * Auto-deallocation list for caller convenience.
2219 			 */
2220 			if (dlistp == NULL)
2221 				dlistp = &dlist;
2222 
2223 			dtmp = kmalloc(sizeof(*dtmp), M_TEMP, M_WAITOK);
2224 			dtmp->object = backing_object;
2225 			dtmp->next = *dlistp;
2226 			*dlistp = dtmp;
2227 		} else {
2228 			vm_object_chain_release(backing_object);
2229 			vm_object_drop(backing_object);
2230 		}
2231 		/* backing_object = NULL; not needed */
2232 		/* loop */
2233 	}
2234 
2235 	/*
2236 	 * Clean up any left over backing_object
2237 	 */
2238 	if (backing_object) {
2239 		vm_object_chain_release(backing_object);
2240 		vm_object_drop(backing_object);
2241 	}
2242 
2243 	/*
2244 	 * Clean up any auto-deallocation list.  This is a convenience
2245 	 * for top-level callers so they don't have to pass &dlist.
2246 	 * Do not clean up any caller-passed dlistp, the caller will
2247 	 * do that.
2248 	 */
2249 	if (dlist)
2250 		vm_object_deallocate_list(&dlist);
2251 
2252 }
2253 
2254 /*
2255  * vm_object_collapse() may collect additional objects in need of
2256  * deallocation.  This routine deallocates these objects.  The
2257  * deallocation itself can trigger additional collapses (which the
2258  * deallocate function takes care of).  This procedure is used to
2259  * reduce procedural recursion since these vm_object shadow chains
2260  * can become quite long.
2261  */
2262 void
2263 vm_object_deallocate_list(struct vm_object_dealloc_list **dlistp)
2264 {
2265 	struct vm_object_dealloc_list *dlist;
2266 
2267 	while ((dlist = *dlistp) != NULL) {
2268 		*dlistp = dlist->next;
2269 		vm_object_lock(dlist->object);
2270 		vm_object_deallocate_locked(dlist->object);
2271 		vm_object_drop(dlist->object);
2272 		kfree(dlist, M_TEMP);
2273 	}
2274 }
2275 
2276 /*
2277  * Removes all physical pages in the specified object range from the
2278  * object's list of pages.
2279  *
2280  * No requirements.
2281  */
2282 static int vm_object_page_remove_callback(vm_page_t p, void *data);
2283 
2284 void
2285 vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
2286 		      boolean_t clean_only)
2287 {
2288 	struct rb_vm_page_scan_info info;
2289 	int all;
2290 
2291 	/*
2292 	 * Degenerate cases and assertions
2293 	 */
2294 	vm_object_hold(object);
2295 	if (object == NULL ||
2296 	    (object->resident_page_count == 0 && object->swblock_count == 0)) {
2297 		vm_object_drop(object);
2298 		return;
2299 	}
2300 	KASSERT(object->type != OBJT_PHYS,
2301 		("attempt to remove pages from a physical object"));
2302 
2303 	/*
2304 	 * Indicate that paging is occuring on the object
2305 	 */
2306 	vm_object_pip_add(object, 1);
2307 
2308 	/*
2309 	 * Figure out the actual removal range and whether we are removing
2310 	 * the entire contents of the object or not.  If removing the entire
2311 	 * contents, be sure to get all pages, even those that might be
2312 	 * beyond the end of the object.
2313 	 */
2314 	info.start_pindex = start;
2315 	if (end == 0)
2316 		info.end_pindex = (vm_pindex_t)-1;
2317 	else
2318 		info.end_pindex = end - 1;
2319 	info.limit = clean_only;
2320 	all = (start == 0 && info.end_pindex >= object->size - 1);
2321 
2322 	/*
2323 	 * Loop until we are sure we have gotten them all.
2324 	 */
2325 	do {
2326 		info.error = 0;
2327 		vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
2328 					vm_object_page_remove_callback, &info);
2329 	} while (info.error);
2330 
2331 	/*
2332 	 * Remove any related swap if throwing away pages, or for
2333 	 * non-swap objects (the swap is a clean copy in that case).
2334 	 */
2335 	if (object->type != OBJT_SWAP || clean_only == FALSE) {
2336 		if (all)
2337 			swap_pager_freespace_all(object);
2338 		else
2339 			swap_pager_freespace(object, info.start_pindex,
2340 			     info.end_pindex - info.start_pindex + 1);
2341 	}
2342 
2343 	/*
2344 	 * Cleanup
2345 	 */
2346 	vm_object_pip_wakeup(object);
2347 	vm_object_drop(object);
2348 }
2349 
2350 /*
2351  * The caller must hold the object
2352  */
2353 static int
2354 vm_object_page_remove_callback(vm_page_t p, void *data)
2355 {
2356 	struct rb_vm_page_scan_info *info = data;
2357 
2358 	if (vm_page_busy_try(p, TRUE)) {
2359 		vm_page_sleep_busy(p, TRUE, "vmopar");
2360 		info->error = 1;
2361 		return(0);
2362 	}
2363 
2364 	/*
2365 	 * Wired pages cannot be destroyed, but they can be invalidated
2366 	 * and we do so if clean_only (limit) is not set.
2367 	 *
2368 	 * WARNING!  The page may be wired due to being part of a buffer
2369 	 *	     cache buffer, and the buffer might be marked B_CACHE.
2370 	 *	     This is fine as part of a truncation but VFSs must be
2371 	 *	     sure to fix the buffer up when re-extending the file.
2372 	 */
2373 	if (p->wire_count != 0) {
2374 		vm_page_protect(p, VM_PROT_NONE);
2375 		if (info->limit == 0)
2376 			p->valid = 0;
2377 		vm_page_wakeup(p);
2378 		return(0);
2379 	}
2380 
2381 	/*
2382 	 * limit is our clean_only flag.  If set and the page is dirty, do
2383 	 * not free it.  If set and the page is being held by someone, do
2384 	 * not free it.
2385 	 */
2386 	if (info->limit && p->valid) {
2387 		vm_page_test_dirty(p);
2388 		if (p->valid & p->dirty) {
2389 			vm_page_wakeup(p);
2390 			return(0);
2391 		}
2392 #if 0
2393 		if (p->hold_count) {
2394 			vm_page_wakeup(p);
2395 			return(0);
2396 		}
2397 #endif
2398 	}
2399 
2400 	/*
2401 	 * Destroy the page
2402 	 */
2403 	vm_page_protect(p, VM_PROT_NONE);
2404 	vm_page_free(p);
2405 	return(0);
2406 }
2407 
2408 /*
2409  * Coalesces two objects backing up adjoining regions of memory into a
2410  * single object.
2411  *
2412  * returns TRUE if objects were combined.
2413  *
2414  * NOTE: Only works at the moment if the second object is NULL -
2415  *	 if it's not, which object do we lock first?
2416  *
2417  * Parameters:
2418  *	prev_object	First object to coalesce
2419  *	prev_offset	Offset into prev_object
2420  *	next_object	Second object into coalesce
2421  *	next_offset	Offset into next_object
2422  *
2423  *	prev_size	Size of reference to prev_object
2424  *	next_size	Size of reference to next_object
2425  *
2426  * The caller does not need to hold (prev_object) but must have a stable
2427  * pointer to it (typically by holding the vm_map locked).
2428  */
2429 boolean_t
2430 vm_object_coalesce(vm_object_t prev_object, vm_pindex_t prev_pindex,
2431 		   vm_size_t prev_size, vm_size_t next_size)
2432 {
2433 	vm_pindex_t next_pindex;
2434 
2435 	if (prev_object == NULL)
2436 		return (TRUE);
2437 
2438 	vm_object_hold(prev_object);
2439 
2440 	if (prev_object->type != OBJT_DEFAULT &&
2441 	    prev_object->type != OBJT_SWAP) {
2442 		vm_object_drop(prev_object);
2443 		return (FALSE);
2444 	}
2445 
2446 	/*
2447 	 * Try to collapse the object first
2448 	 */
2449 	vm_object_chain_acquire(prev_object);
2450 	vm_object_collapse(prev_object, NULL);
2451 
2452 	/*
2453 	 * Can't coalesce if: . more than one reference . paged out . shadows
2454 	 * another object . has a copy elsewhere (any of which mean that the
2455 	 * pages not mapped to prev_entry may be in use anyway)
2456 	 */
2457 
2458 	if (prev_object->backing_object != NULL) {
2459 		vm_object_chain_release(prev_object);
2460 		vm_object_drop(prev_object);
2461 		return (FALSE);
2462 	}
2463 
2464 	prev_size >>= PAGE_SHIFT;
2465 	next_size >>= PAGE_SHIFT;
2466 	next_pindex = prev_pindex + prev_size;
2467 
2468 	if ((prev_object->ref_count > 1) &&
2469 	    (prev_object->size != next_pindex)) {
2470 		vm_object_chain_release(prev_object);
2471 		vm_object_drop(prev_object);
2472 		return (FALSE);
2473 	}
2474 
2475 	/*
2476 	 * Remove any pages that may still be in the object from a previous
2477 	 * deallocation.
2478 	 */
2479 	if (next_pindex < prev_object->size) {
2480 		vm_object_page_remove(prev_object,
2481 				      next_pindex,
2482 				      next_pindex + next_size, FALSE);
2483 		if (prev_object->type == OBJT_SWAP)
2484 			swap_pager_freespace(prev_object,
2485 					     next_pindex, next_size);
2486 	}
2487 
2488 	/*
2489 	 * Extend the object if necessary.
2490 	 */
2491 	if (next_pindex + next_size > prev_object->size)
2492 		prev_object->size = next_pindex + next_size;
2493 
2494 	vm_object_chain_release(prev_object);
2495 	vm_object_drop(prev_object);
2496 	return (TRUE);
2497 }
2498 
2499 /*
2500  * Make the object writable and flag is being possibly dirty.
2501  *
2502  * The caller must hold the object. XXX called from vm_page_dirty(),
2503  * There is currently no requirement to hold the object.
2504  */
2505 void
2506 vm_object_set_writeable_dirty(vm_object_t object)
2507 {
2508 	struct vnode *vp;
2509 
2510 	/*vm_object_assert_held(object);*/
2511 	/*
2512 	 * Avoid contention in vm fault path by checking the state before
2513 	 * issuing an atomic op on it.
2514 	 */
2515 	if ((object->flags & (OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY)) !=
2516 	    (OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY)) {
2517 		vm_object_set_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
2518 	}
2519 	if (object->type == OBJT_VNODE &&
2520 	    (vp = (struct vnode *)object->handle) != NULL) {
2521 		if ((vp->v_flag & VOBJDIRTY) == 0) {
2522 			vsetflags(vp, VOBJDIRTY);
2523 		}
2524 	}
2525 }
2526 
2527 #include "opt_ddb.h"
2528 #ifdef DDB
2529 #include <sys/kernel.h>
2530 
2531 #include <sys/cons.h>
2532 
2533 #include <ddb/ddb.h>
2534 
2535 static int	_vm_object_in_map (vm_map_t map, vm_object_t object,
2536 				       vm_map_entry_t entry);
2537 static int	vm_object_in_map (vm_object_t object);
2538 
2539 /*
2540  * The caller must hold the object.
2541  */
2542 static int
2543 _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
2544 {
2545 	vm_map_t tmpm;
2546 	vm_map_entry_t tmpe;
2547 	vm_object_t obj, nobj;
2548 	int entcount;
2549 
2550 	if (map == 0)
2551 		return 0;
2552 	if (entry == 0) {
2553 		tmpe = map->header.next;
2554 		entcount = map->nentries;
2555 		while (entcount-- && (tmpe != &map->header)) {
2556 			if( _vm_object_in_map(map, object, tmpe)) {
2557 				return 1;
2558 			}
2559 			tmpe = tmpe->next;
2560 		}
2561 		return (0);
2562 	}
2563 	switch(entry->maptype) {
2564 	case VM_MAPTYPE_SUBMAP:
2565 		tmpm = entry->object.sub_map;
2566 		tmpe = tmpm->header.next;
2567 		entcount = tmpm->nentries;
2568 		while (entcount-- && tmpe != &tmpm->header) {
2569 			if( _vm_object_in_map(tmpm, object, tmpe)) {
2570 				return 1;
2571 			}
2572 			tmpe = tmpe->next;
2573 		}
2574 		break;
2575 	case VM_MAPTYPE_NORMAL:
2576 	case VM_MAPTYPE_VPAGETABLE:
2577 		obj = entry->object.vm_object;
2578 		while (obj) {
2579 			if (obj == object) {
2580 				if (obj != entry->object.vm_object)
2581 					vm_object_drop(obj);
2582 				return 1;
2583 			}
2584 			while ((nobj = obj->backing_object) != NULL) {
2585 				vm_object_hold(nobj);
2586 				if (nobj == obj->backing_object)
2587 					break;
2588 				vm_object_drop(nobj);
2589 			}
2590 			if (obj != entry->object.vm_object) {
2591 				if (nobj)
2592 					vm_object_lock_swap();
2593 				vm_object_drop(obj);
2594 			}
2595 			obj = nobj;
2596 		}
2597 		break;
2598 	default:
2599 		break;
2600 	}
2601 	return 0;
2602 }
2603 
2604 static int vm_object_in_map_callback(struct proc *p, void *data);
2605 
2606 struct vm_object_in_map_info {
2607 	vm_object_t object;
2608 	int rv;
2609 };
2610 
2611 /*
2612  * Debugging only
2613  */
2614 static int
2615 vm_object_in_map(vm_object_t object)
2616 {
2617 	struct vm_object_in_map_info info;
2618 
2619 	info.rv = 0;
2620 	info.object = object;
2621 
2622 	allproc_scan(vm_object_in_map_callback, &info);
2623 	if (info.rv)
2624 		return 1;
2625 	if( _vm_object_in_map(&kernel_map, object, 0))
2626 		return 1;
2627 	if( _vm_object_in_map(&pager_map, object, 0))
2628 		return 1;
2629 	if( _vm_object_in_map(&buffer_map, object, 0))
2630 		return 1;
2631 	return 0;
2632 }
2633 
2634 /*
2635  * Debugging only
2636  */
2637 static int
2638 vm_object_in_map_callback(struct proc *p, void *data)
2639 {
2640 	struct vm_object_in_map_info *info = data;
2641 
2642 	if (p->p_vmspace) {
2643 		if (_vm_object_in_map(&p->p_vmspace->vm_map, info->object, 0)) {
2644 			info->rv = 1;
2645 			return -1;
2646 		}
2647 	}
2648 	return (0);
2649 }
2650 
2651 DB_SHOW_COMMAND(vmochk, vm_object_check)
2652 {
2653 	vm_object_t object;
2654 
2655 	/*
2656 	 * make sure that internal objs are in a map somewhere
2657 	 * and none have zero ref counts.
2658 	 */
2659 	for (object = TAILQ_FIRST(&vm_object_list);
2660 			object != NULL;
2661 			object = TAILQ_NEXT(object, object_list)) {
2662 		if (object->type == OBJT_MARKER)
2663 			continue;
2664 		if (object->handle == NULL &&
2665 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2666 			if (object->ref_count == 0) {
2667 				db_printf("vmochk: internal obj has zero ref count: %ld\n",
2668 					(long)object->size);
2669 			}
2670 			if (!vm_object_in_map(object)) {
2671 				db_printf(
2672 			"vmochk: internal obj is not in a map: "
2673 			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2674 				    object->ref_count, (u_long)object->size,
2675 				    (u_long)object->size,
2676 				    (void *)object->backing_object);
2677 			}
2678 		}
2679 	}
2680 }
2681 
2682 /*
2683  * Debugging only
2684  */
2685 DB_SHOW_COMMAND(object, vm_object_print_static)
2686 {
2687 	/* XXX convert args. */
2688 	vm_object_t object = (vm_object_t)addr;
2689 	boolean_t full = have_addr;
2690 
2691 	vm_page_t p;
2692 
2693 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
2694 #define	count	was_count
2695 
2696 	int count;
2697 
2698 	if (object == NULL)
2699 		return;
2700 
2701 	db_iprintf(
2702 	    "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n",
2703 	    object, (int)object->type, (u_long)object->size,
2704 	    object->resident_page_count, object->ref_count, object->flags);
2705 	/*
2706 	 * XXX no %qd in kernel.  Truncate object->backing_object_offset.
2707 	 */
2708 	db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%lx\n",
2709 	    object->shadow_count,
2710 	    object->backing_object ? object->backing_object->ref_count : 0,
2711 	    object->backing_object, (long)object->backing_object_offset);
2712 
2713 	if (!full)
2714 		return;
2715 
2716 	db_indent += 2;
2717 	count = 0;
2718 	RB_FOREACH(p, vm_page_rb_tree, &object->rb_memq) {
2719 		if (count == 0)
2720 			db_iprintf("memory:=");
2721 		else if (count == 6) {
2722 			db_printf("\n");
2723 			db_iprintf(" ...");
2724 			count = 0;
2725 		} else
2726 			db_printf(",");
2727 		count++;
2728 
2729 		db_printf("(off=0x%lx,page=0x%lx)",
2730 		    (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
2731 	}
2732 	if (count != 0)
2733 		db_printf("\n");
2734 	db_indent -= 2;
2735 }
2736 
2737 /* XXX. */
2738 #undef count
2739 
2740 /*
2741  * XXX need this non-static entry for calling from vm_map_print.
2742  *
2743  * Debugging only
2744  */
2745 void
2746 vm_object_print(/* db_expr_t */ long addr,
2747 		boolean_t have_addr,
2748 		/* db_expr_t */ long count,
2749 		char *modif)
2750 {
2751 	vm_object_print_static(addr, have_addr, count, modif);
2752 }
2753 
2754 /*
2755  * Debugging only
2756  */
2757 DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
2758 {
2759 	vm_object_t object;
2760 	int nl = 0;
2761 	int c;
2762 	for (object = TAILQ_FIRST(&vm_object_list);
2763 			object != NULL;
2764 			object = TAILQ_NEXT(object, object_list)) {
2765 		vm_pindex_t idx, fidx;
2766 		vm_pindex_t osize;
2767 		vm_paddr_t pa = -1, padiff;
2768 		int rcount;
2769 		vm_page_t m;
2770 
2771 		if (object->type == OBJT_MARKER)
2772 			continue;
2773 		db_printf("new object: %p\n", (void *)object);
2774 		if ( nl > 18) {
2775 			c = cngetc();
2776 			if (c != ' ')
2777 				return;
2778 			nl = 0;
2779 		}
2780 		nl++;
2781 		rcount = 0;
2782 		fidx = 0;
2783 		osize = object->size;
2784 		if (osize > 128)
2785 			osize = 128;
2786 		for (idx = 0; idx < osize; idx++) {
2787 			m = vm_page_lookup(object, idx);
2788 			if (m == NULL) {
2789 				if (rcount) {
2790 					db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2791 						(long)fidx, rcount, (long)pa);
2792 					if ( nl > 18) {
2793 						c = cngetc();
2794 						if (c != ' ')
2795 							return;
2796 						nl = 0;
2797 					}
2798 					nl++;
2799 					rcount = 0;
2800 				}
2801 				continue;
2802 			}
2803 
2804 
2805 			if (rcount &&
2806 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
2807 				++rcount;
2808 				continue;
2809 			}
2810 			if (rcount) {
2811 				padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
2812 				padiff >>= PAGE_SHIFT;
2813 				padiff &= PQ_L2_MASK;
2814 				if (padiff == 0) {
2815 					pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
2816 					++rcount;
2817 					continue;
2818 				}
2819 				db_printf(" index(%ld)run(%d)pa(0x%lx)",
2820 					(long)fidx, rcount, (long)pa);
2821 				db_printf("pd(%ld)\n", (long)padiff);
2822 				if ( nl > 18) {
2823 					c = cngetc();
2824 					if (c != ' ')
2825 						return;
2826 					nl = 0;
2827 				}
2828 				nl++;
2829 			}
2830 			fidx = idx;
2831 			pa = VM_PAGE_TO_PHYS(m);
2832 			rcount = 1;
2833 		}
2834 		if (rcount) {
2835 			db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2836 				(long)fidx, rcount, (long)pa);
2837 			if ( nl > 18) {
2838 				c = cngetc();
2839 				if (c != ' ')
2840 					return;
2841 				nl = 0;
2842 			}
2843 			nl++;
2844 		}
2845 	}
2846 }
2847 #endif /* DDB */
2848