xref: /freebsd/sys/vm/vm_object.c (revision a14a0223ae1b172e96dd2a1d849e22026a98b692)
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/proc.h>		/* for curproc, pageproc */
74 #include <sys/vnode.h>
75 #include <sys/vmmeter.h>
76 #include <sys/mman.h>
77 #include <sys/mount.h>
78 
79 #include <vm/vm.h>
80 #include <vm/vm_param.h>
81 #include <vm/pmap.h>
82 #include <vm/vm_map.h>
83 #include <vm/vm_object.h>
84 #include <vm/vm_page.h>
85 #include <vm/vm_pageout.h>
86 #include <vm/vm_pager.h>
87 #include <vm/swap_pager.h>
88 #include <vm/vm_kern.h>
89 #include <vm/vm_extern.h>
90 #include <vm/vm_zone.h>
91 
92 static void	vm_object_qcollapse __P((vm_object_t object));
93 
94 /*
95  *	Virtual memory objects maintain the actual data
96  *	associated with allocated virtual memory.  A given
97  *	page of memory exists within exactly one object.
98  *
99  *	An object is only deallocated when all "references"
100  *	are given up.  Only one "reference" to a given
101  *	region of an object should be writeable.
102  *
103  *	Associated with each object is a list of all resident
104  *	memory pages belonging to that object; this list is
105  *	maintained by the "vm_page" module, and locked by the object's
106  *	lock.
107  *
108  *	Each object also records a "pager" routine which is
109  *	used to retrieve (and store) pages to the proper backing
110  *	storage.  In addition, objects may be backed by other
111  *	objects from which they were virtual-copied.
112  *
113  *	The only items within the object structure which are
114  *	modified after time of creation are:
115  *		reference count		locked by object's lock
116  *		pager routine		locked by object's lock
117  *
118  */
119 
120 struct object_q vm_object_list;
121 #ifndef NULL_SIMPLELOCKS
122 static struct simplelock vm_object_list_lock;
123 #endif
124 static long vm_object_count;		/* count of all objects */
125 vm_object_t kernel_object;
126 vm_object_t kmem_object;
127 static struct vm_object kernel_object_store;
128 static struct vm_object kmem_object_store;
129 extern int vm_pageout_page_count;
130 
131 static long object_collapses;
132 static long object_bypasses;
133 static int next_index;
134 static vm_zone_t obj_zone;
135 static struct vm_zone obj_zone_store;
136 static int object_hash_rand;
137 #define VM_OBJECTS_INIT 256
138 static struct vm_object vm_objects_init[VM_OBJECTS_INIT];
139 
140 void
141 _vm_object_allocate(type, size, object)
142 	objtype_t type;
143 	vm_size_t size;
144 	vm_object_t object;
145 {
146 	int incr;
147 	TAILQ_INIT(&object->memq);
148 	TAILQ_INIT(&object->shadow_head);
149 
150 	object->type = type;
151 	object->size = size;
152 	object->ref_count = 1;
153 	object->flags = 0;
154 	if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
155 		vm_object_set_flag(object, OBJ_ONEMAPPING);
156 	object->paging_in_progress = 0;
157 	object->resident_page_count = 0;
158 	object->shadow_count = 0;
159 	object->pg_color = next_index;
160 	if ( size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
161 		incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
162 	else
163 		incr = size;
164 	next_index = (next_index + incr) & PQ_L2_MASK;
165 	object->handle = NULL;
166 	object->backing_object = NULL;
167 	object->backing_object_offset = (vm_ooffset_t) 0;
168 	/*
169 	 * Try to generate a number that will spread objects out in the
170 	 * hash table.  We 'wipe' new objects across the hash in 128 page
171 	 * increments plus 1 more to offset it a little more by the time
172 	 * it wraps around.
173 	 */
174 	object->hash_rand = object_hash_rand - 129;
175 
176 	object->generation++;
177 
178 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
179 	vm_object_count++;
180 	object_hash_rand = object->hash_rand;
181 }
182 
183 /*
184  *	vm_object_init:
185  *
186  *	Initialize the VM objects module.
187  */
188 void
189 vm_object_init()
190 {
191 	TAILQ_INIT(&vm_object_list);
192 	simple_lock_init(&vm_object_list_lock);
193 	vm_object_count = 0;
194 
195 	kernel_object = &kernel_object_store;
196 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
197 	    kernel_object);
198 
199 	kmem_object = &kmem_object_store;
200 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
201 	    kmem_object);
202 
203 	obj_zone = &obj_zone_store;
204 	zbootinit(obj_zone, "VM OBJECT", sizeof (struct vm_object),
205 		vm_objects_init, VM_OBJECTS_INIT);
206 }
207 
208 void
209 vm_object_init2() {
210 	zinitna(obj_zone, NULL, NULL, 0, 0, 0, 1);
211 }
212 
213 /*
214  *	vm_object_allocate:
215  *
216  *	Returns a new object with the given size.
217  */
218 
219 vm_object_t
220 vm_object_allocate(type, size)
221 	objtype_t type;
222 	vm_size_t size;
223 {
224 	vm_object_t result;
225 
226 	result = (vm_object_t) zalloc(obj_zone);
227 
228 	_vm_object_allocate(type, size, result);
229 
230 	return (result);
231 }
232 
233 
234 /*
235  *	vm_object_reference:
236  *
237  *	Gets another reference to the given object.
238  */
239 void
240 vm_object_reference(object)
241 	vm_object_t object;
242 {
243 	if (object == NULL)
244 		return;
245 
246 	KASSERT(!(object->flags & OBJ_DEAD),
247 	    ("vm_object_reference: attempting to reference dead obj"));
248 
249 	object->ref_count++;
250 	if (object->type == OBJT_VNODE) {
251 		while (vget((struct vnode *) object->handle, LK_RETRY|LK_NOOBJ, curproc)) {
252 #if !defined(MAX_PERF)
253 			printf("vm_object_reference: delay in getting object\n");
254 #endif
255 		}
256 	}
257 }
258 
259 void
260 vm_object_vndeallocate(object)
261 	vm_object_t object;
262 {
263 	struct vnode *vp = (struct vnode *) object->handle;
264 
265 	KASSERT(object->type == OBJT_VNODE,
266 	    ("vm_object_vndeallocate: not a vnode object"));
267 	KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
268 #ifdef INVARIANTS
269 	if (object->ref_count == 0) {
270 		vprint("vm_object_vndeallocate", vp);
271 		panic("vm_object_vndeallocate: bad object reference count");
272 	}
273 #endif
274 
275 	object->ref_count--;
276 	if (object->ref_count == 0) {
277 		vp->v_flag &= ~VTEXT;
278 		vm_object_clear_flag(object, OBJ_OPT);
279 	}
280 	vrele(vp);
281 }
282 
283 /*
284  *	vm_object_deallocate:
285  *
286  *	Release a reference to the specified object,
287  *	gained either through a vm_object_allocate
288  *	or a vm_object_reference call.  When all references
289  *	are gone, storage associated with this object
290  *	may be relinquished.
291  *
292  *	No object may be locked.
293  */
294 void
295 vm_object_deallocate(object)
296 	vm_object_t object;
297 {
298 	vm_object_t temp;
299 
300 	while (object != NULL) {
301 
302 		if (object->type == OBJT_VNODE) {
303 			vm_object_vndeallocate(object);
304 			return;
305 		}
306 
307 		if (object->ref_count == 0) {
308 			panic("vm_object_deallocate: object deallocated too many times: %d", object->type);
309 		} else if (object->ref_count > 2) {
310 			object->ref_count--;
311 			return;
312 		}
313 
314 		/*
315 		 * Here on ref_count of one or two, which are special cases for
316 		 * objects.
317 		 */
318 		if ((object->ref_count == 2) && (object->shadow_count == 0)) {
319 			vm_object_set_flag(object, OBJ_ONEMAPPING);
320 			object->ref_count--;
321 			return;
322 		} else if ((object->ref_count == 2) && (object->shadow_count == 1)) {
323 			object->ref_count--;
324 			if ((object->handle == NULL) &&
325 			    (object->type == OBJT_DEFAULT ||
326 			     object->type == OBJT_SWAP)) {
327 				vm_object_t robject;
328 
329 				robject = TAILQ_FIRST(&object->shadow_head);
330 				KASSERT(robject != NULL,
331 				    ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
332 					 object->ref_count,
333 					 object->shadow_count));
334 				if ((robject->handle == NULL) &&
335 				    (robject->type == OBJT_DEFAULT ||
336 				     robject->type == OBJT_SWAP)) {
337 
338 					robject->ref_count++;
339 
340 					while (
341 						robject->paging_in_progress ||
342 						object->paging_in_progress
343 					) {
344 						vm_object_pip_sleep(robject, "objde1");
345 						vm_object_pip_sleep(object, "objde2");
346 					}
347 
348 					if (robject->ref_count == 1) {
349 						robject->ref_count--;
350 						object = robject;
351 						goto doterm;
352 					}
353 
354 					object = robject;
355 					vm_object_collapse(object);
356 					continue;
357 				}
358 			}
359 
360 			return;
361 
362 		} else {
363 			object->ref_count--;
364 			if (object->ref_count != 0)
365 				return;
366 		}
367 
368 doterm:
369 
370 		temp = object->backing_object;
371 		if (temp) {
372 			TAILQ_REMOVE(&temp->shadow_head, object, shadow_list);
373 			temp->shadow_count--;
374 			if (temp->ref_count == 0)
375 				vm_object_clear_flag(temp, OBJ_OPT);
376 			temp->generation++;
377 			object->backing_object = NULL;
378 		}
379 		vm_object_terminate(object);
380 		/* unlocks and deallocates object */
381 		object = temp;
382 	}
383 }
384 
385 /*
386  *	vm_object_terminate actually destroys the specified object, freeing
387  *	up all previously used resources.
388  *
389  *	The object must be locked.
390  *	This routine may block.
391  */
392 void
393 vm_object_terminate(object)
394 	vm_object_t object;
395 {
396 	vm_page_t p;
397 	int s;
398 
399 	/*
400 	 * Make sure no one uses us.
401 	 */
402 	vm_object_set_flag(object, OBJ_DEAD);
403 
404 	/*
405 	 * wait for the pageout daemon to be done with the object
406 	 */
407 	vm_object_pip_wait(object, "objtrm");
408 
409 	KASSERT(!object->paging_in_progress,
410 		("vm_object_terminate: pageout in progress"));
411 
412 	/*
413 	 * Clean and free the pages, as appropriate. All references to the
414 	 * object are gone, so we don't need to lock it.
415 	 */
416 	if (object->type == OBJT_VNODE) {
417 		struct vnode *vp;
418 
419 		/*
420 		 * Freeze optimized copies.
421 		 */
422 		vm_freeze_copyopts(object, 0, object->size);
423 
424 		/*
425 		 * Clean pages and flush buffers.
426 		 */
427 		vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
428 
429 		vp = (struct vnode *) object->handle;
430 		vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
431 	}
432 
433 	if (object->ref_count != 0)
434 		panic("vm_object_terminate: object with references, ref_count=%d", object->ref_count);
435 
436 	/*
437 	 * Now free any remaining pages. For internal objects, this also
438 	 * removes them from paging queues. Don't free wired pages, just
439 	 * remove them from the object.
440 	 */
441 	s = splvm();
442 	while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
443 #if !defined(MAX_PERF)
444 		if (p->busy || (p->flags & PG_BUSY))
445 			panic("vm_object_terminate: freeing busy page %p\n", p);
446 #endif
447 		if (p->wire_count == 0) {
448 			vm_page_busy(p);
449 			vm_page_free(p);
450 			cnt.v_pfree++;
451 		} else {
452 			vm_page_busy(p);
453 			vm_page_remove(p);
454 		}
455 	}
456 	splx(s);
457 
458 	/*
459 	 * Let the pager know object is dead.
460 	 */
461 	vm_pager_deallocate(object);
462 
463 	/*
464 	 * Remove the object from the global object list.
465 	 */
466 	simple_lock(&vm_object_list_lock);
467 	TAILQ_REMOVE(&vm_object_list, object, object_list);
468 	simple_unlock(&vm_object_list_lock);
469 
470 	wakeup(object);
471 
472 	/*
473 	 * Free the space for the object.
474 	 */
475 	zfree(obj_zone, object);
476 }
477 
478 /*
479  *	vm_object_page_clean
480  *
481  *	Clean all dirty pages in the specified range of object.
482  *	Leaves page on whatever queue it is currently on.
483  *
484  *	Odd semantics: if start == end, we clean everything.
485  *
486  *	The object must be locked.
487  */
488 
489 void
490 vm_object_page_clean(object, start, end, flags)
491 	vm_object_t object;
492 	vm_pindex_t start;
493 	vm_pindex_t end;
494 	int flags;
495 {
496 	vm_page_t p, np, tp;
497 	vm_offset_t tstart, tend;
498 	vm_pindex_t pi;
499 	int s;
500 	struct vnode *vp;
501 	int runlen;
502 	int maxf;
503 	int chkb;
504 	int maxb;
505 	int i;
506 	int pagerflags;
507 	vm_page_t maf[vm_pageout_page_count];
508 	vm_page_t mab[vm_pageout_page_count];
509 	vm_page_t ma[vm_pageout_page_count];
510 	int curgeneration;
511 
512 	if (object->type != OBJT_VNODE ||
513 		(object->flags & OBJ_MIGHTBEDIRTY) == 0)
514 		return;
515 
516 	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : 0;
517 	pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
518 
519 	vp = object->handle;
520 
521 	vm_object_set_flag(object, OBJ_CLEANING);
522 
523 	tstart = start;
524 	if (end == 0) {
525 		tend = object->size;
526 	} else {
527 		tend = end;
528 	}
529 
530 	for(p = TAILQ_FIRST(&object->memq); p; p = TAILQ_NEXT(p, listq)) {
531 		vm_page_flag_set(p, PG_CLEANCHK);
532 		vm_page_protect(p, VM_PROT_READ);
533 	}
534 
535 	if ((tstart == 0) && (tend == object->size)) {
536 		vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
537 	}
538 
539 rescan:
540 	curgeneration = object->generation;
541 
542 	for(p = TAILQ_FIRST(&object->memq); p; p = np) {
543 		np = TAILQ_NEXT(p, listq);
544 
545 		pi = p->pindex;
546 		if (((p->flags & PG_CLEANCHK) == 0) ||
547 			(pi < tstart) || (pi >= tend) ||
548 			(p->valid == 0) ||
549 			((p->queue - p->pc) == PQ_CACHE)) {
550 			vm_page_flag_clear(p, PG_CLEANCHK);
551 			continue;
552 		}
553 
554 		vm_page_test_dirty(p);
555 		if ((p->dirty & p->valid) == 0) {
556 			vm_page_flag_clear(p, PG_CLEANCHK);
557 			continue;
558 		}
559 
560 		s = splvm();
561 		while (vm_page_sleep_busy(p, TRUE, "vpcwai")) {
562 			if (object->generation != curgeneration) {
563 				splx(s);
564 				goto rescan;
565 			}
566 		}
567 
568 		maxf = 0;
569 		for(i=1;i<vm_pageout_page_count;i++) {
570 			if ((tp = vm_page_lookup(object, pi + i)) != NULL) {
571 				if ((tp->flags & PG_BUSY) ||
572 					(tp->flags & PG_CLEANCHK) == 0 ||
573 					(tp->busy != 0))
574 					break;
575 				if((tp->queue - tp->pc) == PQ_CACHE) {
576 					vm_page_flag_clear(tp, PG_CLEANCHK);
577 					break;
578 				}
579 				vm_page_test_dirty(tp);
580 				if ((tp->dirty & tp->valid) == 0) {
581 					vm_page_flag_clear(tp, PG_CLEANCHK);
582 					break;
583 				}
584 				maf[ i - 1 ] = tp;
585 				maxf++;
586 				continue;
587 			}
588 			break;
589 		}
590 
591 		maxb = 0;
592 		chkb = vm_pageout_page_count -  maxf;
593 		if (chkb) {
594 			for(i = 1; i < chkb;i++) {
595 				if ((tp = vm_page_lookup(object, pi - i)) != NULL) {
596 					if ((tp->flags & PG_BUSY) ||
597 						(tp->flags & PG_CLEANCHK) == 0 ||
598 						(tp->busy != 0))
599 						break;
600 					if((tp->queue - tp->pc) == PQ_CACHE) {
601 						vm_page_flag_clear(tp, PG_CLEANCHK);
602 						break;
603 					}
604 					vm_page_test_dirty(tp);
605 					if ((tp->dirty & tp->valid) == 0) {
606 						vm_page_flag_clear(tp, PG_CLEANCHK);
607 						break;
608 					}
609 					mab[ i - 1 ] = tp;
610 					maxb++;
611 					continue;
612 				}
613 				break;
614 			}
615 		}
616 
617 		for(i=0;i<maxb;i++) {
618 			int index = (maxb - i) - 1;
619 			ma[index] = mab[i];
620 			vm_page_flag_clear(ma[index], PG_CLEANCHK);
621 		}
622 		vm_page_flag_clear(p, PG_CLEANCHK);
623 		ma[maxb] = p;
624 		for(i=0;i<maxf;i++) {
625 			int index = (maxb + i) + 1;
626 			ma[index] = maf[i];
627 			vm_page_flag_clear(ma[index], PG_CLEANCHK);
628 		}
629 		runlen = maxb + maxf + 1;
630 
631 		splx(s);
632 		vm_pageout_flush(ma, runlen, pagerflags);
633 		for (i = 0; i<runlen; i++) {
634 			if (ma[i]->valid & ma[i]->dirty) {
635 				vm_page_protect(ma[i], VM_PROT_READ);
636 				vm_page_flag_set(ma[i], PG_CLEANCHK);
637 			}
638 		}
639 		if (object->generation != curgeneration)
640 			goto rescan;
641 	}
642 
643 #if 0
644 	VOP_FSYNC(vp, NULL, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
645 #endif
646 
647 	vm_object_clear_flag(object, OBJ_CLEANING);
648 	return;
649 }
650 
651 #ifdef not_used
652 /* XXX I cannot tell if this should be an exported symbol */
653 /*
654  *	vm_object_deactivate_pages
655  *
656  *	Deactivate all pages in the specified object.  (Keep its pages
657  *	in memory even though it is no longer referenced.)
658  *
659  *	The object must be locked.
660  */
661 static void
662 vm_object_deactivate_pages(object)
663 	vm_object_t object;
664 {
665 	vm_page_t p, next;
666 
667 	for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
668 		next = TAILQ_NEXT(p, listq);
669 		vm_page_deactivate(p);
670 	}
671 }
672 #endif
673 
674 /*
675  * Same as vm_object_pmap_copy, except range checking really
676  * works, and is meant for small sections of an object.
677  *
678  * This code protects resident pages by making them read-only
679  * and is typically called on a fork or split when a page
680  * is converted to copy-on-write.
681  *
682  * NOTE: If the page is already at VM_PROT_NONE, calling
683  * vm_page_protect will have no effect.
684  */
685 
686 void
687 vm_object_pmap_copy_1(object, start, end)
688 	vm_object_t object;
689 	vm_pindex_t start;
690 	vm_pindex_t end;
691 {
692 	vm_pindex_t idx;
693 	vm_page_t p;
694 
695 	if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
696 		return;
697 
698 	for (idx = start; idx < end; idx++) {
699 		p = vm_page_lookup(object, idx);
700 		if (p == NULL)
701 			continue;
702 		vm_page_protect(p, VM_PROT_READ);
703 	}
704 }
705 
706 /*
707  *	vm_object_pmap_remove:
708  *
709  *	Removes all physical pages in the specified
710  *	object range from all physical maps.
711  *
712  *	The object must *not* be locked.
713  */
714 void
715 vm_object_pmap_remove(object, start, end)
716 	vm_object_t object;
717 	vm_pindex_t start;
718 	vm_pindex_t end;
719 {
720 	vm_page_t p;
721 
722 	if (object == NULL)
723 		return;
724 	for (p = TAILQ_FIRST(&object->memq);
725 		p != NULL;
726 		p = TAILQ_NEXT(p, listq)) {
727 		if (p->pindex >= start && p->pindex < end)
728 			vm_page_protect(p, VM_PROT_NONE);
729 	}
730 	if ((start == 0) && (object->size == end))
731 		vm_object_clear_flag(object, OBJ_WRITEABLE);
732 }
733 
734 /*
735  *	vm_object_madvise:
736  *
737  *	Implements the madvise function at the object/page level.
738  *
739  *	MADV_WILLNEED	(any object)
740  *
741  *	    Activate the specified pages if they are resident.
742  *
743  *	MADV_DONTNEED	(any object)
744  *
745  *	    Deactivate the specified pages if they are resident.
746  *
747  *	MADV_FREE	(OBJT_DEFAULT/OBJT_SWAP objects,
748  *			 OBJ_ONEMAPPING only)
749  *
750  *	    Deactivate and clean the specified pages if they are
751  *	    resident.  This permits the process to reuse the pages
752  *	    without faulting or the kernel to reclaim the pages
753  *	    without I/O.
754  */
755 void
756 vm_object_madvise(object, pindex, count, advise)
757 	vm_object_t object;
758 	vm_pindex_t pindex;
759 	int count;
760 	int advise;
761 {
762 	vm_pindex_t end, tpindex;
763 	vm_object_t tobject;
764 	vm_page_t m;
765 
766 	if (object == NULL)
767 		return;
768 
769 	end = pindex + count;
770 
771 	/*
772 	 * Locate and adjust resident pages
773 	 */
774 
775 	for (; pindex < end; pindex += 1) {
776 relookup:
777 		tobject = object;
778 		tpindex = pindex;
779 shadowlookup:
780 		/*
781 		 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
782 		 * and those pages must be OBJ_ONEMAPPING.
783 		 */
784 		if (advise == MADV_FREE) {
785 			if ((tobject->type != OBJT_DEFAULT &&
786 			     tobject->type != OBJT_SWAP) ||
787 			    (tobject->flags & OBJ_ONEMAPPING) == 0) {
788 				continue;
789 			}
790 		}
791 
792 		m = vm_page_lookup(tobject, tpindex);
793 
794 		if (m == NULL) {
795 			/*
796 			 * There may be swap even if there is no backing page
797 			 */
798 			if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
799 				swap_pager_freespace(tobject, tpindex, 1);
800 
801 			/*
802 			 * next object
803 			 */
804 			tobject = tobject->backing_object;
805 			if (tobject == NULL)
806 				continue;
807 			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
808 			goto shadowlookup;
809 		}
810 
811 		/*
812 		 * If the page is busy or not in a normal active state,
813 		 * we skip it.  Things can break if we mess with pages
814 		 * in any of the below states.
815 		 */
816 		if (
817 		    m->hold_count ||
818 		    m->wire_count ||
819 		    m->valid != VM_PAGE_BITS_ALL
820 		) {
821 			continue;
822 		}
823 
824  		if (vm_page_sleep_busy(m, TRUE, "madvpo"))
825   			goto relookup;
826 
827 		if (advise == MADV_WILLNEED) {
828 			vm_page_activate(m);
829 		} else if (advise == MADV_DONTNEED) {
830 			vm_page_dontneed(m);
831 		} else if (advise == MADV_FREE) {
832 			/*
833 			 * Mark the page clean.  This will allow the page
834 			 * to be freed up by the system.  However, such pages
835 			 * are often reused quickly by malloc()/free()
836 			 * so we do not do anything that would cause
837 			 * a page fault if we can help it.
838 			 *
839 			 * Specifically, we do not try to actually free
840 			 * the page now nor do we try to put it in the
841 			 * cache (which would cause a page fault on reuse).
842 			 *
843 			 * But we do make the page is freeable as we
844 			 * can without actually taking the step of unmapping
845 			 * it.
846 			 */
847 			pmap_clear_modify(VM_PAGE_TO_PHYS(m));
848 			m->dirty = 0;
849 			m->act_count = 0;
850 			vm_page_dontneed(m);
851 			if (tobject->type == OBJT_SWAP)
852 				swap_pager_freespace(tobject, tpindex, 1);
853 		}
854 	}
855 }
856 
857 /*
858  *	vm_object_shadow:
859  *
860  *	Create a new object which is backed by the
861  *	specified existing object range.  The source
862  *	object reference is deallocated.
863  *
864  *	The new object and offset into that object
865  *	are returned in the source parameters.
866  */
867 
868 void
869 vm_object_shadow(object, offset, length)
870 	vm_object_t *object;	/* IN/OUT */
871 	vm_ooffset_t *offset;	/* IN/OUT */
872 	vm_size_t length;
873 {
874 	vm_object_t source;
875 	vm_object_t result;
876 
877 	source = *object;
878 
879 	/*
880 	 * Don't create the new object if the old object isn't shared.
881 	 */
882 
883 	if (source != NULL &&
884 	    source->ref_count == 1 &&
885 	    source->handle == NULL &&
886 	    (source->type == OBJT_DEFAULT ||
887 	     source->type == OBJT_SWAP))
888 		return;
889 
890 	KASSERT((source->flags & OBJ_ONEMAPPING) == 0,
891 		("vm_object_shadow: source object has OBJ_ONEMAPPING set.\n"));
892 
893 	/*
894 	 * Allocate a new object with the given length
895 	 */
896 
897 	if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL)
898 		panic("vm_object_shadow: no object for shadowing");
899 
900 	/*
901 	 * The new object shadows the source object, adding a reference to it.
902 	 * Our caller changes his reference to point to the new object,
903 	 * removing a reference to the source object.  Net result: no change
904 	 * of reference count.
905 	 *
906 	 * Try to optimize the result object's page color when shadowing
907 	 * in order to maintain page coloring consistancy in the combined
908 	 * shadowed object.
909 	 */
910 	result->backing_object = source;
911 	if (source) {
912 		TAILQ_INSERT_TAIL(&source->shadow_head, result, shadow_list);
913 		source->shadow_count++;
914 		source->generation++;
915 		result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) & PQ_L2_MASK;
916 	}
917 
918 	/*
919 	 * Store the offset into the source object, and fix up the offset into
920 	 * the new object.
921 	 */
922 
923 	result->backing_object_offset = *offset;
924 
925 	/*
926 	 * Return the new things
927 	 */
928 
929 	*offset = 0;
930 	*object = result;
931 }
932 
933 #define	OBSC_TEST_ALL_SHADOWED	0x0001
934 #define	OBSC_COLLAPSE_NOWAIT	0x0002
935 #define	OBSC_COLLAPSE_WAIT	0x0004
936 
937 static __inline int
938 vm_object_backing_scan(vm_object_t object, int op)
939 {
940 	int s;
941 	int r = 1;
942 	vm_page_t p;
943 	vm_object_t backing_object;
944 	vm_pindex_t backing_offset_index;
945 
946 	s = splvm();
947 
948 	backing_object = object->backing_object;
949 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
950 
951 	/*
952 	 * Initial conditions
953 	 */
954 
955 	if (op & OBSC_TEST_ALL_SHADOWED) {
956 		/*
957 		 * We do not want to have to test for the existance of
958 		 * swap pages in the backing object.  XXX but with the
959 		 * new swapper this would be pretty easy to do.
960 		 *
961 		 * XXX what about anonymous MAP_SHARED memory that hasn't
962 		 * been ZFOD faulted yet?  If we do not test for this, the
963 		 * shadow test may succeed! XXX
964 		 */
965 		if (backing_object->type != OBJT_DEFAULT) {
966 			splx(s);
967 			return(0);
968 		}
969 	}
970 	if (op & OBSC_COLLAPSE_WAIT) {
971 		vm_object_set_flag(backing_object, OBJ_DEAD);
972 	}
973 
974 	/*
975 	 * Our scan
976 	 */
977 
978 	p = TAILQ_FIRST(&backing_object->memq);
979 	while (p) {
980 		vm_page_t next = TAILQ_NEXT(p, listq);
981 		vm_pindex_t new_pindex = p->pindex - backing_offset_index;
982 
983 		if (op & OBSC_TEST_ALL_SHADOWED) {
984 			vm_page_t pp;
985 
986 			/*
987 			 * Ignore pages outside the parent object's range
988 			 * and outside the parent object's mapping of the
989 			 * backing object.
990 			 *
991 			 * note that we do not busy the backing object's
992 			 * page.
993 			 */
994 
995 			if (
996 			    p->pindex < backing_offset_index ||
997 			    new_pindex >= object->size
998 			) {
999 				p = next;
1000 				continue;
1001 			}
1002 
1003 			/*
1004 			 * See if the parent has the page or if the parent's
1005 			 * object pager has the page.  If the parent has the
1006 			 * page but the page is not valid, the parent's
1007 			 * object pager must have the page.
1008 			 *
1009 			 * If this fails, the parent does not completely shadow
1010 			 * the object and we might as well give up now.
1011 			 */
1012 
1013 			pp = vm_page_lookup(object, new_pindex);
1014 			if (
1015 			    (pp == NULL || pp->valid == 0) &&
1016 			    !vm_pager_has_page(object, new_pindex, NULL, NULL)
1017 			) {
1018 				r = 0;
1019 				break;
1020 			}
1021 		}
1022 
1023 		/*
1024 		 * Check for busy page
1025 		 */
1026 
1027 		if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
1028 			vm_page_t pp;
1029 
1030 			if (op & OBSC_COLLAPSE_NOWAIT) {
1031 				if (
1032 				    (p->flags & PG_BUSY) ||
1033 				    !p->valid ||
1034 				    p->hold_count ||
1035 				    p->wire_count ||
1036 				    p->busy
1037 				) {
1038 					p = next;
1039 					continue;
1040 				}
1041 			} else if (op & OBSC_COLLAPSE_WAIT) {
1042 				if (vm_page_sleep_busy(p, TRUE, "vmocol")) {
1043 					/*
1044 					 * If we slept, anything could have
1045 					 * happened.  Since the object is
1046 					 * marked dead, the backing offset
1047 					 * should not have changed so we
1048 					 * just restart our scan.
1049 					 */
1050 					p = TAILQ_FIRST(&backing_object->memq);
1051 					continue;
1052 				}
1053 			}
1054 
1055 			/*
1056 			 * Busy the page
1057 			 */
1058 			vm_page_busy(p);
1059 
1060 			KASSERT(
1061 			    p->object == backing_object,
1062 			    ("vm_object_qcollapse(): object mismatch")
1063 			);
1064 
1065 			/*
1066 			 * Destroy any associated swap
1067 			 */
1068 			if (backing_object->type == OBJT_SWAP) {
1069 				swap_pager_freespace(
1070 				    backing_object,
1071 				    p->pindex,
1072 				    1
1073 				);
1074 			}
1075 
1076 			if (
1077 			    p->pindex < backing_offset_index ||
1078 			    new_pindex >= object->size
1079 			) {
1080 				/*
1081 				 * Page is out of the parent object's range, we
1082 				 * can simply destroy it.
1083 				 */
1084 				vm_page_protect(p, VM_PROT_NONE);
1085 				vm_page_free(p);
1086 				p = next;
1087 				continue;
1088 			}
1089 
1090 			pp = vm_page_lookup(object, new_pindex);
1091 			if (
1092 			    pp != NULL ||
1093 			    vm_pager_has_page(object, new_pindex, NULL, NULL)
1094 			) {
1095 				/*
1096 				 * page already exists in parent OR swap exists
1097 				 * for this location in the parent.  Destroy
1098 				 * the original page from the backing object.
1099 				 *
1100 				 * Leave the parent's page alone
1101 				 */
1102 				vm_page_protect(p, VM_PROT_NONE);
1103 				vm_page_free(p);
1104 				p = next;
1105 				continue;
1106 			}
1107 
1108 			/*
1109 			 * Page does not exist in parent, rename the
1110 			 * page from the backing object to the main object.
1111 			 *
1112 			 * If the page was mapped to a process, it can remain
1113 			 * mapped through the rename.
1114 			 */
1115 			if ((p->queue - p->pc) == PQ_CACHE)
1116 				vm_page_deactivate(p);
1117 
1118 			vm_page_rename(p, object, new_pindex);
1119 			/* page automatically made dirty by rename */
1120 		}
1121 		p = next;
1122 	}
1123 	splx(s);
1124 	return(r);
1125 }
1126 
1127 
1128 /*
1129  * this version of collapse allows the operation to occur earlier and
1130  * when paging_in_progress is true for an object...  This is not a complete
1131  * operation, but should plug 99.9% of the rest of the leaks.
1132  */
1133 static void
1134 vm_object_qcollapse(object)
1135 	vm_object_t object;
1136 {
1137 	vm_object_t backing_object = object->backing_object;
1138 
1139 	if (backing_object->ref_count != 1)
1140 		return;
1141 
1142 	backing_object->ref_count += 2;
1143 
1144 	vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
1145 
1146 	backing_object->ref_count -= 2;
1147 }
1148 
1149 /*
1150  *	vm_object_collapse:
1151  *
1152  *	Collapse an object with the object backing it.
1153  *	Pages in the backing object are moved into the
1154  *	parent, and the backing object is deallocated.
1155  */
1156 void
1157 vm_object_collapse(object)
1158 	vm_object_t object;
1159 {
1160 	while (TRUE) {
1161 		vm_object_t backing_object;
1162 
1163 		/*
1164 		 * Verify that the conditions are right for collapse:
1165 		 *
1166 		 * The object exists and the backing object exists.
1167 		 */
1168 		if (object == NULL)
1169 			break;
1170 
1171 		if ((backing_object = object->backing_object) == NULL)
1172 			break;
1173 
1174 		/*
1175 		 * we check the backing object first, because it is most likely
1176 		 * not collapsable.
1177 		 */
1178 		if (backing_object->handle != NULL ||
1179 		    (backing_object->type != OBJT_DEFAULT &&
1180 		     backing_object->type != OBJT_SWAP) ||
1181 		    (backing_object->flags & OBJ_DEAD) ||
1182 		    object->handle != NULL ||
1183 		    (object->type != OBJT_DEFAULT &&
1184 		     object->type != OBJT_SWAP) ||
1185 		    (object->flags & OBJ_DEAD)) {
1186 			break;
1187 		}
1188 
1189 		if (
1190 		    object->paging_in_progress != 0 ||
1191 		    backing_object->paging_in_progress != 0
1192 		) {
1193 			vm_object_qcollapse(object);
1194 			break;
1195 		}
1196 
1197 		/*
1198 		 * We know that we can either collapse the backing object (if
1199 		 * the parent is the only reference to it) or (perhaps) have
1200 		 * the parent bypass the object if the parent happens to shadow
1201 		 * all the resident pages in the entire backing object.
1202 		 *
1203 		 * This is ignoring pager-backed pages such as swap pages.
1204 		 * vm_object_backing_scan fails the shadowing test in this
1205 		 * case.
1206 		 */
1207 
1208 		if (backing_object->ref_count == 1) {
1209 			/*
1210 			 * If there is exactly one reference to the backing
1211 			 * object, we can collapse it into the parent.
1212 			 */
1213 
1214 			vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1215 
1216 			/*
1217 			 * Move the pager from backing_object to object.
1218 			 */
1219 
1220 			if (backing_object->type == OBJT_SWAP) {
1221 				vm_object_pip_add(backing_object, 1);
1222 
1223 				/*
1224 				 * scrap the paging_offset junk and do a
1225 				 * discrete copy.  This also removes major
1226 				 * assumptions about how the swap-pager
1227 				 * works from where it doesn't belong.  The
1228 				 * new swapper is able to optimize the
1229 				 * destroy-source case.
1230 				 */
1231 
1232 				vm_object_pip_add(object, 1);
1233 				swap_pager_copy(
1234 				    backing_object,
1235 				    object,
1236 				    OFF_TO_IDX(object->backing_object_offset), TRUE);
1237 				vm_object_pip_wakeup(object);
1238 
1239 				vm_object_pip_wakeup(backing_object);
1240 			}
1241 			/*
1242 			 * Object now shadows whatever backing_object did.
1243 			 * Note that the reference to
1244 			 * backing_object->backing_object moves from within
1245 			 * backing_object to within object.
1246 			 */
1247 
1248 			TAILQ_REMOVE(
1249 			    &object->backing_object->shadow_head,
1250 			    object,
1251 			    shadow_list
1252 			);
1253 			object->backing_object->shadow_count--;
1254 			object->backing_object->generation++;
1255 			if (backing_object->backing_object) {
1256 				TAILQ_REMOVE(
1257 				    &backing_object->backing_object->shadow_head,
1258 				    backing_object,
1259 				    shadow_list
1260 				);
1261 				backing_object->backing_object->shadow_count--;
1262 				backing_object->backing_object->generation++;
1263 			}
1264 			object->backing_object = backing_object->backing_object;
1265 			if (object->backing_object) {
1266 				TAILQ_INSERT_TAIL(
1267 				    &object->backing_object->shadow_head,
1268 				    object,
1269 				    shadow_list
1270 				);
1271 				object->backing_object->shadow_count++;
1272 				object->backing_object->generation++;
1273 			}
1274 
1275 			object->backing_object_offset +=
1276 			    backing_object->backing_object_offset;
1277 
1278 			/*
1279 			 * Discard backing_object.
1280 			 *
1281 			 * Since the backing object has no pages, no pager left,
1282 			 * and no object references within it, all that is
1283 			 * necessary is to dispose of it.
1284 			 */
1285 
1286 			TAILQ_REMOVE(
1287 			    &vm_object_list,
1288 			    backing_object,
1289 			    object_list
1290 			);
1291 			vm_object_count--;
1292 
1293 			zfree(obj_zone, backing_object);
1294 
1295 			object_collapses++;
1296 		} else {
1297 			vm_object_t new_backing_object;
1298 
1299 			/*
1300 			 * If we do not entirely shadow the backing object,
1301 			 * there is nothing we can do so we give up.
1302 			 */
1303 
1304 			if (vm_object_backing_scan(object, OBSC_TEST_ALL_SHADOWED) == 0) {
1305 				break;
1306 			}
1307 
1308 			/*
1309 			 * Make the parent shadow the next object in the
1310 			 * chain.  Deallocating backing_object will not remove
1311 			 * it, since its reference count is at least 2.
1312 			 */
1313 
1314 			TAILQ_REMOVE(
1315 			    &backing_object->shadow_head,
1316 			    object,
1317 			    shadow_list
1318 			);
1319 			backing_object->shadow_count--;
1320 			backing_object->generation++;
1321 
1322 			new_backing_object = backing_object->backing_object;
1323 			if ((object->backing_object = new_backing_object) != NULL) {
1324 				vm_object_reference(new_backing_object);
1325 				TAILQ_INSERT_TAIL(
1326 				    &new_backing_object->shadow_head,
1327 				    object,
1328 				    shadow_list
1329 				);
1330 				new_backing_object->shadow_count++;
1331 				new_backing_object->generation++;
1332 				object->backing_object_offset +=
1333 					backing_object->backing_object_offset;
1334 			}
1335 
1336 			/*
1337 			 * Drop the reference count on backing_object. Since
1338 			 * its ref_count was at least 2, it will not vanish;
1339 			 * so we don't need to call vm_object_deallocate, but
1340 			 * we do anyway.
1341 			 */
1342 			vm_object_deallocate(backing_object);
1343 			object_bypasses++;
1344 		}
1345 
1346 		/*
1347 		 * Try again with this object's new backing object.
1348 		 */
1349 	}
1350 }
1351 
1352 /*
1353  *	vm_object_page_remove: [internal]
1354  *
1355  *	Removes all physical pages in the specified
1356  *	object range from the object's list of pages.
1357  *
1358  *	The object must be locked.
1359  */
1360 void
1361 vm_object_page_remove(object, start, end, clean_only)
1362 	vm_object_t object;
1363 	vm_pindex_t start;
1364 	vm_pindex_t end;
1365 	boolean_t clean_only;
1366 {
1367 	vm_page_t p, next;
1368 	unsigned int size;
1369 	int all;
1370 
1371 	if (object == NULL ||
1372 	    object->resident_page_count == 0)
1373 		return;
1374 
1375 	all = ((end == 0) && (start == 0));
1376 
1377 	vm_object_pip_add(object, 1);
1378 again:
1379 	size = end - start;
1380 	if (all || size > object->resident_page_count / 4) {
1381 		for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
1382 			next = TAILQ_NEXT(p, listq);
1383 			if (all || ((start <= p->pindex) && (p->pindex < end))) {
1384 				if (p->wire_count != 0) {
1385 					vm_page_protect(p, VM_PROT_NONE);
1386 					if (!clean_only)
1387 						p->valid = 0;
1388 					continue;
1389 				}
1390 
1391 				/*
1392 				 * The busy flags are only cleared at
1393 				 * interrupt -- minimize the spl transitions
1394 				 */
1395 
1396  				if (vm_page_sleep_busy(p, TRUE, "vmopar"))
1397  					goto again;
1398 
1399 				if (clean_only && p->valid) {
1400 					vm_page_test_dirty(p);
1401 					if (p->valid & p->dirty)
1402 						continue;
1403 				}
1404 
1405 				vm_page_busy(p);
1406 				vm_page_protect(p, VM_PROT_NONE);
1407 				vm_page_free(p);
1408 			}
1409 		}
1410 	} else {
1411 		while (size > 0) {
1412 			if ((p = vm_page_lookup(object, start)) != 0) {
1413 
1414 				if (p->wire_count != 0) {
1415 					vm_page_protect(p, VM_PROT_NONE);
1416 					if (!clean_only)
1417 						p->valid = 0;
1418 					start += 1;
1419 					size -= 1;
1420 					continue;
1421 				}
1422 
1423 				/*
1424 				 * The busy flags are only cleared at
1425 				 * interrupt -- minimize the spl transitions
1426 				 */
1427  				if (vm_page_sleep_busy(p, TRUE, "vmopar"))
1428 					goto again;
1429 
1430 				if (clean_only && p->valid) {
1431 					vm_page_test_dirty(p);
1432 					if (p->valid & p->dirty) {
1433 						start += 1;
1434 						size -= 1;
1435 						continue;
1436 					}
1437 				}
1438 
1439 				vm_page_busy(p);
1440 				vm_page_protect(p, VM_PROT_NONE);
1441 				vm_page_free(p);
1442 			}
1443 			start += 1;
1444 			size -= 1;
1445 		}
1446 	}
1447 	vm_object_pip_wakeup(object);
1448 }
1449 
1450 /*
1451  *	Routine:	vm_object_coalesce
1452  *	Function:	Coalesces two objects backing up adjoining
1453  *			regions of memory into a single object.
1454  *
1455  *	returns TRUE if objects were combined.
1456  *
1457  *	NOTE:	Only works at the moment if the second object is NULL -
1458  *		if it's not, which object do we lock first?
1459  *
1460  *	Parameters:
1461  *		prev_object	First object to coalesce
1462  *		prev_offset	Offset into prev_object
1463  *		next_object	Second object into coalesce
1464  *		next_offset	Offset into next_object
1465  *
1466  *		prev_size	Size of reference to prev_object
1467  *		next_size	Size of reference to next_object
1468  *
1469  *	Conditions:
1470  *	The object must *not* be locked.
1471  */
1472 boolean_t
1473 vm_object_coalesce(prev_object, prev_pindex, prev_size, next_size)
1474 	vm_object_t prev_object;
1475 	vm_pindex_t prev_pindex;
1476 	vm_size_t prev_size, next_size;
1477 {
1478 	vm_pindex_t next_pindex;
1479 
1480 	if (prev_object == NULL) {
1481 		return (TRUE);
1482 	}
1483 
1484 	if (prev_object->type != OBJT_DEFAULT &&
1485 	    prev_object->type != OBJT_SWAP) {
1486 		return (FALSE);
1487 	}
1488 
1489 	/*
1490 	 * Try to collapse the object first
1491 	 */
1492 	vm_object_collapse(prev_object);
1493 
1494 	/*
1495 	 * Can't coalesce if: . more than one reference . paged out . shadows
1496 	 * another object . has a copy elsewhere (any of which mean that the
1497 	 * pages not mapped to prev_entry may be in use anyway)
1498 	 */
1499 
1500 	if (prev_object->backing_object != NULL) {
1501 		return (FALSE);
1502 	}
1503 
1504 	prev_size >>= PAGE_SHIFT;
1505 	next_size >>= PAGE_SHIFT;
1506 	next_pindex = prev_pindex + prev_size;
1507 
1508 	if ((prev_object->ref_count > 1) &&
1509 	    (prev_object->size != next_pindex)) {
1510 		return (FALSE);
1511 	}
1512 
1513 	/*
1514 	 * Remove any pages that may still be in the object from a previous
1515 	 * deallocation.
1516 	 */
1517 	if (next_pindex < prev_object->size) {
1518 		vm_object_page_remove(prev_object,
1519 				      next_pindex,
1520 				      next_pindex + next_size, FALSE);
1521 		if (prev_object->type == OBJT_SWAP)
1522 			swap_pager_freespace(prev_object,
1523 					     next_pindex, next_size);
1524 	}
1525 
1526 	/*
1527 	 * Extend the object if necessary.
1528 	 */
1529 	if (next_pindex + next_size > prev_object->size)
1530 		prev_object->size = next_pindex + next_size;
1531 
1532 	return (TRUE);
1533 }
1534 
1535 #include "opt_ddb.h"
1536 #ifdef DDB
1537 #include <sys/kernel.h>
1538 
1539 #include <sys/cons.h>
1540 
1541 #include <ddb/ddb.h>
1542 
1543 static int	_vm_object_in_map __P((vm_map_t map, vm_object_t object,
1544 				       vm_map_entry_t entry));
1545 static int	vm_object_in_map __P((vm_object_t object));
1546 
1547 static int
1548 _vm_object_in_map(map, object, entry)
1549 	vm_map_t map;
1550 	vm_object_t object;
1551 	vm_map_entry_t entry;
1552 {
1553 	vm_map_t tmpm;
1554 	vm_map_entry_t tmpe;
1555 	vm_object_t obj;
1556 	int entcount;
1557 
1558 	if (map == 0)
1559 		return 0;
1560 
1561 	if (entry == 0) {
1562 		tmpe = map->header.next;
1563 		entcount = map->nentries;
1564 		while (entcount-- && (tmpe != &map->header)) {
1565 			if( _vm_object_in_map(map, object, tmpe)) {
1566 				return 1;
1567 			}
1568 			tmpe = tmpe->next;
1569 		}
1570 	} else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
1571 		tmpm = entry->object.sub_map;
1572 		tmpe = tmpm->header.next;
1573 		entcount = tmpm->nentries;
1574 		while (entcount-- && tmpe != &tmpm->header) {
1575 			if( _vm_object_in_map(tmpm, object, tmpe)) {
1576 				return 1;
1577 			}
1578 			tmpe = tmpe->next;
1579 		}
1580 	} else if ((obj = entry->object.vm_object) != NULL) {
1581 		for(; obj; obj=obj->backing_object)
1582 			if( obj == object) {
1583 				return 1;
1584 			}
1585 	}
1586 	return 0;
1587 }
1588 
1589 static int
1590 vm_object_in_map( object)
1591 	vm_object_t object;
1592 {
1593 	struct proc *p;
1594 	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
1595 		if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1596 			continue;
1597 		if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0))
1598 			return 1;
1599 	}
1600 	if( _vm_object_in_map( kernel_map, object, 0))
1601 		return 1;
1602 	if( _vm_object_in_map( kmem_map, object, 0))
1603 		return 1;
1604 	if( _vm_object_in_map( pager_map, object, 0))
1605 		return 1;
1606 	if( _vm_object_in_map( buffer_map, object, 0))
1607 		return 1;
1608 	if( _vm_object_in_map( mb_map, object, 0))
1609 		return 1;
1610 	return 0;
1611 }
1612 
1613 DB_SHOW_COMMAND(vmochk, vm_object_check)
1614 {
1615 	vm_object_t object;
1616 
1617 	/*
1618 	 * make sure that internal objs are in a map somewhere
1619 	 * and none have zero ref counts.
1620 	 */
1621 	for (object = TAILQ_FIRST(&vm_object_list);
1622 			object != NULL;
1623 			object = TAILQ_NEXT(object, object_list)) {
1624 		if (object->handle == NULL &&
1625 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
1626 			if (object->ref_count == 0) {
1627 				db_printf("vmochk: internal obj has zero ref count: %ld\n",
1628 					(long)object->size);
1629 			}
1630 			if (!vm_object_in_map(object)) {
1631 				db_printf(
1632 			"vmochk: internal obj is not in a map: "
1633 			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
1634 				    object->ref_count, (u_long)object->size,
1635 				    (u_long)object->size,
1636 				    (void *)object->backing_object);
1637 			}
1638 		}
1639 	}
1640 }
1641 
1642 /*
1643  *	vm_object_print:	[ debug ]
1644  */
1645 DB_SHOW_COMMAND(object, vm_object_print_static)
1646 {
1647 	/* XXX convert args. */
1648 	vm_object_t object = (vm_object_t)addr;
1649 	boolean_t full = have_addr;
1650 
1651 	vm_page_t p;
1652 
1653 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
1654 #define	count	was_count
1655 
1656 	int count;
1657 
1658 	if (object == NULL)
1659 		return;
1660 
1661 	db_iprintf(
1662 	    "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n",
1663 	    object, (int)object->type, (u_long)object->size,
1664 	    object->resident_page_count, object->ref_count, object->flags);
1665 	/*
1666 	 * XXX no %qd in kernel.  Truncate object->backing_object_offset.
1667 	 */
1668 	db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%lx\n",
1669 	    object->shadow_count,
1670 	    object->backing_object ? object->backing_object->ref_count : 0,
1671 	    object->backing_object, (long)object->backing_object_offset);
1672 
1673 	if (!full)
1674 		return;
1675 
1676 	db_indent += 2;
1677 	count = 0;
1678 	for (p = TAILQ_FIRST(&object->memq); p != NULL; p = TAILQ_NEXT(p, listq)) {
1679 		if (count == 0)
1680 			db_iprintf("memory:=");
1681 		else if (count == 6) {
1682 			db_printf("\n");
1683 			db_iprintf(" ...");
1684 			count = 0;
1685 		} else
1686 			db_printf(",");
1687 		count++;
1688 
1689 		db_printf("(off=0x%lx,page=0x%lx)",
1690 		    (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
1691 	}
1692 	if (count != 0)
1693 		db_printf("\n");
1694 	db_indent -= 2;
1695 }
1696 
1697 /* XXX. */
1698 #undef count
1699 
1700 /* XXX need this non-static entry for calling from vm_map_print. */
1701 void
1702 vm_object_print(addr, have_addr, count, modif)
1703         /* db_expr_t */ long addr;
1704 	boolean_t have_addr;
1705 	/* db_expr_t */ long count;
1706 	char *modif;
1707 {
1708 	vm_object_print_static(addr, have_addr, count, modif);
1709 }
1710 
1711 DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
1712 {
1713 	vm_object_t object;
1714 	int nl = 0;
1715 	int c;
1716 	for (object = TAILQ_FIRST(&vm_object_list);
1717 			object != NULL;
1718 			object = TAILQ_NEXT(object, object_list)) {
1719 		vm_pindex_t idx, fidx;
1720 		vm_pindex_t osize;
1721 		vm_offset_t pa = -1, padiff;
1722 		int rcount;
1723 		vm_page_t m;
1724 
1725 		db_printf("new object: %p\n", (void *)object);
1726 		if ( nl > 18) {
1727 			c = cngetc();
1728 			if (c != ' ')
1729 				return;
1730 			nl = 0;
1731 		}
1732 		nl++;
1733 		rcount = 0;
1734 		fidx = 0;
1735 		osize = object->size;
1736 		if (osize > 128)
1737 			osize = 128;
1738 		for(idx=0;idx<osize;idx++) {
1739 			m = vm_page_lookup(object, idx);
1740 			if (m == NULL) {
1741 				if (rcount) {
1742 					db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
1743 						(long)fidx, rcount, (long)pa);
1744 					if ( nl > 18) {
1745 						c = cngetc();
1746 						if (c != ' ')
1747 							return;
1748 						nl = 0;
1749 					}
1750 					nl++;
1751 					rcount = 0;
1752 				}
1753 				continue;
1754 			}
1755 
1756 
1757 			if (rcount &&
1758 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
1759 				++rcount;
1760 				continue;
1761 			}
1762 			if (rcount) {
1763 				padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
1764 				padiff >>= PAGE_SHIFT;
1765 				padiff &= PQ_L2_MASK;
1766 				if (padiff == 0) {
1767 					pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
1768 					++rcount;
1769 					continue;
1770 				}
1771 				db_printf(" index(%ld)run(%d)pa(0x%lx)",
1772 					(long)fidx, rcount, (long)pa);
1773 				db_printf("pd(%ld)\n", (long)padiff);
1774 				if ( nl > 18) {
1775 					c = cngetc();
1776 					if (c != ' ')
1777 						return;
1778 					nl = 0;
1779 				}
1780 				nl++;
1781 			}
1782 			fidx = idx;
1783 			pa = VM_PAGE_TO_PHYS(m);
1784 			rcount = 1;
1785 		}
1786 		if (rcount) {
1787 			db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
1788 				(long)fidx, rcount, (long)pa);
1789 			if ( nl > 18) {
1790 				c = cngetc();
1791 				if (c != ' ')
1792 					return;
1793 				nl = 0;
1794 			}
1795 			nl++;
1796 		}
1797 	}
1798 }
1799 #endif /* DDB */
1800