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