2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * The Mach Operating System project at Carnegie-Mellon University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
36 * from: @(#)vm_object.c 8.5 (Berkeley) 3/22/94
39 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40 * All rights reserved.
42 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
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.
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.
54 * Carnegie Mellon requests users of this software to return to
56 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
57 * School of Computer Science
58 * Carnegie Mellon University
59 * Pittsburgh PA 15213-3890
61 * any improvements or extensions that they make and grant Carnegie the
62 * rights to redistribute these changes.
64 * $Id: vm_object.c,v 1.144 1999/02/04 17:47:52 dillon Exp $
68 * Virtual memory object module.
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>
77 #include <sys/mount.h>
80 #include <vm/vm_param.h>
81 #include <vm/vm_prot.h>
83 #include <vm/vm_map.h>
84 #include <vm/vm_object.h>
85 #include <vm/vm_page.h>
86 #include <vm/vm_pageout.h>
87 #include <vm/vm_pager.h>
88 #include <vm/swap_pager.h>
89 #include <vm/vm_kern.h>
90 #include <vm/vm_extern.h>
91 #include <vm/vm_zone.h>
93 static void vm_object_qcollapse __P((vm_object_t object));
96 * Virtual memory objects maintain the actual data
97 * associated with allocated virtual memory. A given
98 * page of memory exists within exactly one object.
100 * An object is only deallocated when all "references"
101 * are given up. Only one "reference" to a given
102 * region of an object should be writeable.
104 * Associated with each object is a list of all resident
105 * memory pages belonging to that object; this list is
106 * maintained by the "vm_page" module, and locked by the object's
109 * Each object also records a "pager" routine which is
110 * used to retrieve (and store) pages to the proper backing
111 * storage. In addition, objects may be backed by other
112 * objects from which they were virtual-copied.
114 * The only items within the object structure which are
115 * modified after time of creation are:
116 * reference count locked by object's lock
117 * pager routine locked by object's lock
121 struct object_q vm_object_list;
122 #ifndef NULL_SIMPLELOCKS
123 static struct simplelock vm_object_list_lock;
125 static long vm_object_count; /* count of all objects */
126 vm_object_t kernel_object;
127 vm_object_t kmem_object;
128 static struct vm_object kernel_object_store;
129 static struct vm_object kmem_object_store;
130 extern int vm_pageout_page_count;
132 static long object_collapses;
133 static long object_bypasses;
134 static int next_index;
135 static vm_zone_t obj_zone;
136 static struct vm_zone obj_zone_store;
137 static int object_hash_rand;
138 #define VM_OBJECTS_INIT 256
139 static struct vm_object vm_objects_init[VM_OBJECTS_INIT];
141 static int objidnumber;
145 _vm_object_allocate(type, size, object)
148 register vm_object_t object;
151 TAILQ_INIT(&object->memq);
152 TAILQ_INIT(&object->shadow_head);
156 object->ref_count = 1;
159 object->id = ++objidnumber;
161 if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
162 vm_object_set_flag(object, OBJ_ONEMAPPING);
163 object->behavior = OBJ_NORMAL;
164 object->paging_in_progress = 0;
165 object->resident_page_count = 0;
166 object->cache_count = 0;
167 object->wire_count = 0;
168 object->shadow_count = 0;
169 object->pg_color = next_index;
170 if ( size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
171 incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
174 next_index = (next_index + incr) & PQ_L2_MASK;
175 object->handle = NULL;
176 object->backing_object = NULL;
177 object->backing_object_offset = (vm_ooffset_t) 0;
179 object->page_hint = NULL;
182 * Try to generate a number that will spread objects out in the
183 * hash table. We 'wipe' new objects across the hash in 128 page
184 * increments plus 1 more to offset it a little more by the time
187 object->hash_rand = object_hash_rand - 129;
189 object->last_read = 0;
190 object->generation++;
192 TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
194 object_hash_rand = object->hash_rand;
200 * Initialize the VM objects module.
205 TAILQ_INIT(&vm_object_list);
206 simple_lock_init(&vm_object_list_lock);
209 kernel_object = &kernel_object_store;
210 _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
213 kmem_object = &kmem_object_store;
214 _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
217 obj_zone = &obj_zone_store;
218 zbootinit(obj_zone, "VM OBJECT", sizeof (struct vm_object),
219 vm_objects_init, VM_OBJECTS_INIT);
224 zinitna(obj_zone, NULL, NULL, 0, 0, 0, 1);
228 * vm_object_allocate:
230 * Returns a new object with the given size.
234 vm_object_allocate(type, size)
238 register vm_object_t result;
239 result = (vm_object_t) zalloc(obj_zone);
241 _vm_object_allocate(type, size, result);
248 * vm_object_reference:
250 * Gets another reference to the given object.
253 vm_object_reference(object)
254 register vm_object_t object;
259 KASSERT(!(object->flags & OBJ_DEAD),
260 ("vm_object_reference: attempting to reference dead obj"));
263 if (object->type == OBJT_VNODE) {
264 while (vget((struct vnode *) object->handle, LK_RETRY|LK_NOOBJ, curproc)) {
265 #if !defined(MAX_PERF)
266 printf("vm_object_reference: delay in getting object\n");
273 vm_object_vndeallocate(object)
276 struct vnode *vp = (struct vnode *) object->handle;
278 KASSERT(object->type == OBJT_VNODE,
279 ("vm_object_vndeallocate: not a vnode object"));
280 KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
282 if (object->ref_count == 0) {
283 vprint("vm_object_vndeallocate", vp);
284 panic("vm_object_vndeallocate: bad object reference count");
289 if (object->ref_count == 0) {
290 vp->v_flag &= ~VTEXT;
291 vm_object_clear_flag(object, OBJ_OPT);
297 * vm_object_deallocate:
299 * Release a reference to the specified object,
300 * gained either through a vm_object_allocate
301 * or a vm_object_reference call. When all references
302 * are gone, storage associated with this object
303 * may be relinquished.
305 * No object may be locked.
308 vm_object_deallocate(object)
313 while (object != NULL) {
315 if (object->type == OBJT_VNODE) {
316 vm_object_vndeallocate(object);
320 if (object->ref_count == 0) {
321 panic("vm_object_deallocate: object deallocated too many times: %d", object->type);
322 } else if (object->ref_count > 2) {
328 * Here on ref_count of one or two, which are special cases for
331 if ((object->ref_count == 2) && (object->shadow_count == 0)) {
332 vm_object_set_flag(object, OBJ_ONEMAPPING);
335 } else if ((object->ref_count == 2) && (object->shadow_count == 1)) {
337 if ((object->handle == NULL) &&
338 (object->type == OBJT_DEFAULT ||
339 object->type == OBJT_SWAP)) {
342 robject = TAILQ_FIRST(&object->shadow_head);
343 KASSERT(robject != NULL,
344 ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
346 object->shadow_count));
347 if ((robject->handle == NULL) &&
348 (robject->type == OBJT_DEFAULT ||
349 robject->type == OBJT_SWAP)) {
351 robject->ref_count++;
354 robject->paging_in_progress ||
355 object->paging_in_progress
357 vm_object_pip_sleep(robject, "objde1");
358 vm_object_pip_sleep(object, "objde2");
361 if (robject->ref_count == 1) {
362 robject->ref_count--;
368 vm_object_collapse(object);
377 if (object->ref_count != 0)
383 temp = object->backing_object;
385 TAILQ_REMOVE(&temp->shadow_head, object, shadow_list);
386 temp->shadow_count--;
387 if (temp->ref_count == 0)
388 vm_object_clear_flag(temp, OBJ_OPT);
390 object->backing_object = NULL;
392 vm_object_terminate(object);
393 /* unlocks and deallocates object */
399 * vm_object_terminate actually destroys the specified object, freeing
400 * up all previously used resources.
402 * The object must be locked.
403 * This routine may block.
406 vm_object_terminate(object)
407 register vm_object_t object;
409 register vm_page_t p;
413 * Make sure no one uses us.
415 vm_object_set_flag(object, OBJ_DEAD);
418 * wait for the pageout daemon to be done with the object
420 vm_object_pip_wait(object, "objtrm");
422 KASSERT(!object->paging_in_progress,
423 ("vm_object_terminate: pageout in progress"));
426 * Clean and free the pages, as appropriate. All references to the
427 * object are gone, so we don't need to lock it.
429 if (object->type == OBJT_VNODE) {
433 * Freeze optimized copies.
435 vm_freeze_copyopts(object, 0, object->size);
438 * Clean pages and flush buffers.
440 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
442 vp = (struct vnode *) object->handle;
443 vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
446 if (object->ref_count != 0)
447 panic("vm_object_terminate: object with references, ref_count=%d", object->ref_count);
450 * Now free any remaining pages. For internal objects, this also
451 * removes them from paging queues. Don't free wired pages, just
452 * remove them from the object.
455 while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
456 #if !defined(MAX_PERF)
457 if (p->busy || (p->flags & PG_BUSY))
458 panic("vm_object_terminate: freeing busy page %p\n", p);
460 if (p->wire_count == 0) {
472 * Let the pager know object is dead.
474 vm_pager_deallocate(object);
477 * Remove the object from the global object list.
479 simple_lock(&vm_object_list_lock);
480 TAILQ_REMOVE(&vm_object_list, object, object_list);
481 simple_unlock(&vm_object_list_lock);
486 * Free the space for the object.
488 zfree(obj_zone, object);
492 * vm_object_page_clean
494 * Clean all dirty pages in the specified range of object.
495 * Leaves page on whatever queue it is currently on.
497 * Odd semantics: if start == end, we clean everything.
499 * The object must be locked.
503 vm_object_page_clean(object, start, end, flags)
509 register vm_page_t p, np, tp;
510 register vm_offset_t tstart, tend;
520 vm_page_t maf[vm_pageout_page_count];
521 vm_page_t mab[vm_pageout_page_count];
522 vm_page_t ma[vm_pageout_page_count];
525 if (object->type != OBJT_VNODE ||
526 (object->flags & OBJ_MIGHTBEDIRTY) == 0)
529 pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : 0;
530 pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
534 vm_object_set_flag(object, OBJ_CLEANING);
543 for(p = TAILQ_FIRST(&object->memq); p; p = TAILQ_NEXT(p, listq)) {
544 vm_page_flag_set(p, PG_CLEANCHK);
545 vm_page_protect(p, VM_PROT_READ);
548 if ((tstart == 0) && (tend == object->size)) {
549 vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
553 curgeneration = object->generation;
555 for(p = TAILQ_FIRST(&object->memq); p; p = np) {
556 np = TAILQ_NEXT(p, listq);
559 if (((p->flags & PG_CLEANCHK) == 0) ||
560 (pi < tstart) || (pi >= tend) ||
562 ((p->queue - p->pc) == PQ_CACHE)) {
563 vm_page_flag_clear(p, PG_CLEANCHK);
567 vm_page_test_dirty(p);
568 if ((p->dirty & p->valid) == 0) {
569 vm_page_flag_clear(p, PG_CLEANCHK);
574 while (vm_page_sleep_busy(p, TRUE, "vpcwai")) {
575 if (object->generation != curgeneration) {
582 for(i=1;i<vm_pageout_page_count;i++) {
583 if ((tp = vm_page_lookup(object, pi + i)) != NULL) {
584 if ((tp->flags & PG_BUSY) ||
585 (tp->flags & PG_CLEANCHK) == 0 ||
588 if((tp->queue - tp->pc) == PQ_CACHE) {
589 vm_page_flag_clear(tp, PG_CLEANCHK);
592 vm_page_test_dirty(tp);
593 if ((tp->dirty & tp->valid) == 0) {
594 vm_page_flag_clear(tp, PG_CLEANCHK);
605 chkb = vm_pageout_page_count - maxf;
607 for(i = 1; i < chkb;i++) {
608 if ((tp = vm_page_lookup(object, pi - i)) != NULL) {
609 if ((tp->flags & PG_BUSY) ||
610 (tp->flags & PG_CLEANCHK) == 0 ||
613 if((tp->queue - tp->pc) == PQ_CACHE) {
614 vm_page_flag_clear(tp, PG_CLEANCHK);
617 vm_page_test_dirty(tp);
618 if ((tp->dirty & tp->valid) == 0) {
619 vm_page_flag_clear(tp, PG_CLEANCHK);
630 for(i=0;i<maxb;i++) {
631 int index = (maxb - i) - 1;
633 vm_page_flag_clear(ma[index], PG_CLEANCHK);
635 vm_page_flag_clear(p, PG_CLEANCHK);
637 for(i=0;i<maxf;i++) {
638 int index = (maxb + i) + 1;
640 vm_page_flag_clear(ma[index], PG_CLEANCHK);
642 runlen = maxb + maxf + 1;
645 vm_pageout_flush(ma, runlen, pagerflags);
646 for (i = 0; i<runlen; i++) {
647 if (ma[i]->valid & ma[i]->dirty) {
648 vm_page_protect(ma[i], VM_PROT_READ);
649 vm_page_flag_set(ma[i], PG_CLEANCHK);
652 if (object->generation != curgeneration)
656 VOP_FSYNC(vp, NULL, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
658 vm_object_clear_flag(object, OBJ_CLEANING);
663 /* XXX I cannot tell if this should be an exported symbol */
665 * vm_object_deactivate_pages
667 * Deactivate all pages in the specified object. (Keep its pages
668 * in memory even though it is no longer referenced.)
670 * The object must be locked.
673 vm_object_deactivate_pages(object)
674 register vm_object_t object;
676 register vm_page_t p, next;
678 for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
679 next = TAILQ_NEXT(p, listq);
680 vm_page_deactivate(p);
688 * vm_object_pmap_copy:
690 * Makes all physical pages in the specified
691 * object range copy-on-write. No writeable
692 * references to these pages should remain.
694 * The object must *not* be locked.
697 vm_object_pmap_copy(object, start, end)
698 register vm_object_t object;
699 register vm_pindex_t start;
700 register vm_pindex_t end;
702 register vm_page_t p;
704 if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
707 for (p = TAILQ_FIRST(&object->memq);
709 p = TAILQ_NEXT(p, listq)) {
710 vm_page_protect(p, VM_PROT_READ);
713 vm_object_clear_flag(object, OBJ_WRITEABLE);
719 * Same as vm_object_pmap_copy, except range checking really
720 * works, and is meant for small sections of an object.
722 * This code protects resident pages by making them read-only
723 * and is typically called on a fork or split when a page
724 * is converted to copy-on-write.
726 * NOTE: If the page is already at VM_PROT_NONE, calling
727 * vm_page_protect will have no effect.
731 vm_object_pmap_copy_1(object, start, end)
732 register vm_object_t object;
733 register vm_pindex_t start;
734 register vm_pindex_t end;
737 register vm_page_t p;
739 if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
742 for (idx = start; idx < end; idx++) {
743 p = vm_page_lookup(object, idx);
746 vm_page_protect(p, VM_PROT_READ);
751 * vm_object_pmap_remove:
753 * Removes all physical pages in the specified
754 * object range from all physical maps.
756 * The object must *not* be locked.
759 vm_object_pmap_remove(object, start, end)
760 register vm_object_t object;
761 register vm_pindex_t start;
762 register vm_pindex_t end;
764 register vm_page_t p;
767 for (p = TAILQ_FIRST(&object->memq);
769 p = TAILQ_NEXT(p, listq)) {
770 if (p->pindex >= start && p->pindex < end)
771 vm_page_protect(p, VM_PROT_NONE);
773 if ((start == 0) && (object->size == end))
774 vm_object_clear_flag(object, OBJ_WRITEABLE);
780 * Implements the madvise function at the object/page level.
782 * Currently, madvise() functions are limited to the default and
783 * swap object types only, and also limited to only the unshared portions
784 * of a process's address space. MADV_FREE, certainly, could never be
785 * run on anything else. The others are more flexible and the code could
786 * be adjusted in the future to handle expanded cases for them.
789 vm_object_madvise(object, pindex, count, advise)
795 vm_pindex_t end, tpindex;
802 end = pindex + count;
805 * MADV_FREE special case - free any swap backing store (as well
806 * as resident pages later on).
809 if (advise == MADV_FREE) {
814 (tobject->type == OBJT_DEFAULT ||
815 tobject->type == OBJT_SWAP) &&
816 (tobject->flags & OBJ_ONEMAPPING)
818 if (tobject->type == OBJT_SWAP) {
819 swap_pager_freespace(tobject, tpindex, count);
821 if ((tobject = tobject->backing_object) == NULL)
823 tpindex += OFF_TO_IDX(tobject->backing_object_offset);
828 * Locate and adjust resident pages
831 for (; pindex < end; pindex += 1) {
837 if (tobject->type != OBJT_DEFAULT &&
838 tobject->type != OBJT_SWAP
843 if ((tobject->flags & OBJ_ONEMAPPING) == 0)
846 m = vm_page_lookup(tobject, tpindex);
849 tobject = tobject->backing_object;
853 if ((tobject == NULL) || (tobject->ref_count != 1)) {
857 tpindex += OFF_TO_IDX(tobject->backing_object_offset);
862 * If the page is busy or not in a normal active state,
863 * we skip it. Things can break if we mess with pages
864 * in any of the below states.
869 m->valid != VM_PAGE_BITS_ALL
874 if (vm_page_sleep_busy(m, TRUE, "madvpo"))
877 if (advise == MADV_WILLNEED) {
879 } else if (advise == MADV_DONTNEED) {
880 vm_page_deactivate(m);
881 } else if (advise == MADV_FREE) {
883 * If MADV_FREE_FORCE_FREE is defined, we attempt to
884 * immediately free the page. Otherwise we just
885 * destroy any swap backing store, mark it clean,
886 * and stuff it into the cache.
888 pmap_clear_modify(VM_PAGE_TO_PHYS(m));
891 #ifdef MADV_FREE_FORCE_FREE
892 if (tobject->resident_page_count > 1) {
894 vm_page_protect(m, VM_PROT_NONE);
908 * Create a new object which is backed by the
909 * specified existing object range. The source
910 * object reference is deallocated.
912 * The new object and offset into that object
913 * are returned in the source parameters.
917 vm_object_shadow(object, offset, length)
918 vm_object_t *object; /* IN/OUT */
919 vm_ooffset_t *offset; /* IN/OUT */
922 register vm_object_t source;
923 register vm_object_t result;
928 * Allocate a new object with the given length
931 if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL)
932 panic("vm_object_shadow: no object for shadowing");
935 * The new object shadows the source object, adding a reference to it.
936 * Our caller changes his reference to point to the new object,
937 * removing a reference to the source object. Net result: no change
938 * of reference count.
940 * Try to optimize the result object's page color when shadowing
941 * in order to maintain page coloring consistancy in the combined
944 result->backing_object = source;
946 TAILQ_INSERT_TAIL(&source->shadow_head, result, shadow_list);
947 vm_object_clear_flag(source, OBJ_ONEMAPPING);
948 source->shadow_count++;
949 source->generation++;
950 result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) & PQ_L2_MASK;
954 * Store the offset into the source object, and fix up the offset into
958 result->backing_object_offset = *offset;
961 * Return the new things
970 * this version of collapse allows the operation to occur earlier and
971 * when paging_in_progress is true for an object... This is not a complete
972 * operation, but should plug 99.9% of the rest of the leaks.
975 vm_object_qcollapse(object)
976 register vm_object_t object;
978 register vm_object_t backing_object;
979 register vm_pindex_t backing_offset_index;
980 register vm_page_t p, pp;
981 register vm_size_t size;
984 backing_object = object->backing_object;
985 if (backing_object->ref_count != 1)
988 backing_object->ref_count += 2;
990 backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
994 * Since paging is in progress, we have to run at splbio() to
995 * avoid busied pages from getting ripped out from under us
996 * and screwing up the list sequencing.
1001 p = TAILQ_FIRST(&backing_object->memq);
1004 vm_pindex_t new_pindex;
1005 vm_pindex_t old_pindex;
1009 * loop if the page isn't trivial.
1012 next = TAILQ_NEXT(p, listq);
1013 if ((p->flags & (PG_BUSY | PG_FICTITIOUS)) ||
1014 !p->valid || p->hold_count || p->wire_count || p->busy) {
1020 * busy the page and move it from the backing store to the
1026 KASSERT(p->object == backing_object, ("vm_object_qcollapse(): object mismatch"));
1028 old_pindex = p->pindex;
1029 new_pindex = old_pindex - backing_offset_index;
1031 if (old_pindex < backing_offset_index || new_pindex >= size) {
1033 * Page is out of the parent object's range, we
1034 * can simply destroy it.
1036 vm_page_protect(p, VM_PROT_NONE);
1039 pp = vm_page_lookup(object, new_pindex);
1041 (object->type == OBJT_SWAP && vm_pager_has_page(object,
1042 new_pindex, NULL, NULL))) {
1044 * page already exists in parent OR swap exists
1045 * for this location in the parent. Destroy
1046 * the original page from the backing object.
1048 vm_page_protect(p, VM_PROT_NONE);
1052 * Page does not exist in parent, rename the
1055 if ((p->queue - p->pc) == PQ_CACHE)
1056 vm_page_deactivate(p);
1058 vm_page_protect(p, VM_PROT_NONE);
1060 vm_page_rename(p, object, new_pindex);
1061 /* page automatically made dirty by rename */
1066 * Destroy any swap assigned to the backing object at this
1067 * location. This is an optimization.
1069 if (backing_object->type == OBJT_SWAP) {
1070 swap_pager_freespace(backing_object, old_pindex, 1);
1074 backing_object->ref_count -= 2;
1080 * vm_object_collapse:
1082 * Collapse an object with the object backing it.
1083 * Pages in the backing object are moved into the
1084 * parent, and the backing object is deallocated.
1087 vm_object_collapse(object)
1091 vm_object_t backing_object;
1092 vm_ooffset_t backing_offset;
1094 vm_pindex_t new_pindex, backing_offset_index;
1099 * Verify that the conditions are right for collapse:
1101 * The object exists and no pages in it are currently being paged
1108 * Make sure there is a backing object.
1110 if ((backing_object = object->backing_object) == NULL)
1114 * we check the backing object first, because it is most likely
1117 if (backing_object->handle != NULL ||
1118 (backing_object->type != OBJT_DEFAULT &&
1119 backing_object->type != OBJT_SWAP) ||
1120 (backing_object->flags & OBJ_DEAD) ||
1121 object->handle != NULL ||
1122 (object->type != OBJT_DEFAULT &&
1123 object->type != OBJT_SWAP) ||
1124 (object->flags & OBJ_DEAD)) {
1128 if (object->paging_in_progress != 0 ||
1129 backing_object->paging_in_progress != 0) {
1130 vm_object_qcollapse(object);
1135 * We know that we can either collapse the backing object (if
1136 * the parent is the only reference to it) or (perhaps) remove
1137 * the parent's reference to it.
1140 backing_offset = object->backing_object_offset;
1141 backing_offset_index = OFF_TO_IDX(backing_offset);
1142 size = object->size;
1145 * If there is exactly one reference to the backing object, we
1146 * can collapse it into the parent.
1149 if (backing_object->ref_count == 1) {
1151 vm_object_set_flag(backing_object, OBJ_DEAD);
1153 * We can collapse the backing object.
1155 * Move all in-memory pages from backing_object to the
1156 * parent. Pages that have been paged out will be
1157 * overwritten by any of the parent's pages that
1161 while ((p = TAILQ_FIRST(&backing_object->memq)) != 0) {
1162 if (vm_page_sleep_busy(p, TRUE, "vmocol"))
1165 new_pindex = p->pindex - backing_offset_index;
1168 * If the parent has a page here, or if this
1169 * page falls outside the parent, dispose of
1172 * Otherwise, move it as planned.
1175 if (p->pindex < backing_offset_index ||
1176 new_pindex >= size) {
1177 vm_page_protect(p, VM_PROT_NONE);
1180 pp = vm_page_lookup(object, new_pindex);
1181 if (pp != NULL || (object->type == OBJT_SWAP && vm_pager_has_page(object,
1182 new_pindex, NULL, NULL))) {
1183 vm_page_protect(p, VM_PROT_NONE);
1186 if ((p->queue - p->pc) == PQ_CACHE)
1187 vm_page_deactivate(p);
1189 vm_page_protect(p, VM_PROT_NONE);
1190 vm_page_rename(p, object, new_pindex);
1191 /* page automatically made dirty by rename */
1197 * Move the pager from backing_object to object.
1200 if (backing_object->type == OBJT_SWAP) {
1201 vm_object_pip_add(backing_object, 1);
1204 * scrap the paging_offset junk and do a
1205 * discrete copy. This also removes major
1206 * assumptions about how the swap-pager
1207 * works from where it doesn't belong. The
1208 * new swapper is able to optimize the
1209 * destroy-source case.
1212 vm_object_pip_add(object, 1);
1216 OFF_TO_IDX(object->backing_object_offset), TRUE);
1217 vm_object_pip_wakeup(object);
1219 vm_object_pip_wakeup(backing_object);
1222 * Object now shadows whatever backing_object did.
1223 * Note that the reference to backing_object->backing_object
1224 * moves from within backing_object to within object.
1227 TAILQ_REMOVE(&object->backing_object->shadow_head, object,
1229 object->backing_object->shadow_count--;
1230 object->backing_object->generation++;
1231 if (backing_object->backing_object) {
1232 TAILQ_REMOVE(&backing_object->backing_object->shadow_head,
1233 backing_object, shadow_list);
1234 backing_object->backing_object->shadow_count--;
1235 backing_object->backing_object->generation++;
1237 object->backing_object = backing_object->backing_object;
1238 if (object->backing_object) {
1239 TAILQ_INSERT_TAIL(&object->backing_object->shadow_head,
1240 object, shadow_list);
1241 object->backing_object->shadow_count++;
1242 object->backing_object->generation++;
1245 object->backing_object_offset += backing_object->backing_object_offset;
1247 * Discard backing_object.
1249 * Since the backing object has no pages, no pager left,
1250 * and no object references within it, all that is
1251 * necessary is to dispose of it.
1254 TAILQ_REMOVE(&vm_object_list, backing_object,
1258 zfree(obj_zone, backing_object);
1262 vm_object_t new_backing_object;
1264 * If all of the pages in the backing object are
1265 * shadowed by the parent object, the parent object no
1266 * longer has to shadow the backing object; it can
1267 * shadow the next one in the chain.
1269 * The backing object must not be paged out - we'd have
1270 * to check all of the paged-out pages, as well.
1273 if (backing_object->type != OBJT_DEFAULT) {
1277 * Should have a check for a 'small' number of pages
1281 for (p = TAILQ_FIRST(&backing_object->memq); p;
1282 p = TAILQ_NEXT(p, listq)) {
1284 new_pindex = p->pindex - backing_offset_index;
1288 * If the parent has a page here, or if this
1289 * page falls outside the parent, keep going.
1291 * Otherwise, the backing_object must be left in
1295 if (p->pindex >= backing_offset_index &&
1296 new_pindex <= size) {
1298 pp = vm_page_lookup(object, new_pindex);
1300 if ((pp == NULL) || (pp->flags & PG_BUSY) || pp->busy) {
1306 if ((pp->valid == 0) &&
1307 !vm_pager_has_page(object, new_pindex, NULL, NULL)) {
1309 * Page still needed. Can't go any
1322 * Make the parent shadow the next object in the
1323 * chain. Deallocating backing_object will not remove
1324 * it, since its reference count is at least 2.
1327 TAILQ_REMOVE(&backing_object->shadow_head,
1328 object, shadow_list);
1329 backing_object->shadow_count--;
1330 backing_object->generation++;
1332 new_backing_object = backing_object->backing_object;
1333 if ((object->backing_object = new_backing_object) != NULL) {
1334 vm_object_reference(new_backing_object);
1335 TAILQ_INSERT_TAIL(&new_backing_object->shadow_head,
1336 object, shadow_list);
1337 new_backing_object->shadow_count++;
1338 new_backing_object->generation++;
1339 object->backing_object_offset +=
1340 backing_object->backing_object_offset;
1344 * Drop the reference count on backing_object. Since
1345 * its ref_count was at least 2, it will not vanish;
1346 * so we don't need to call vm_object_deallocate, but
1349 vm_object_deallocate(backing_object);
1354 * Try again with this object's new backing object.
1360 * vm_object_page_remove: [internal]
1362 * Removes all physical pages in the specified
1363 * object range from the object's list of pages.
1365 * The object must be locked.
1368 vm_object_page_remove(object, start, end, clean_only)
1369 register vm_object_t object;
1370 register vm_pindex_t start;
1371 register vm_pindex_t end;
1372 boolean_t clean_only;
1374 register vm_page_t p, next;
1381 all = ((end == 0) && (start == 0));
1383 vm_object_pip_add(object, 1);
1386 if (all || size > 4 || size >= object->size / 4) {
1387 for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
1388 next = TAILQ_NEXT(p, listq);
1389 if (all || ((start <= p->pindex) && (p->pindex < end))) {
1390 if (p->wire_count != 0) {
1391 vm_page_protect(p, VM_PROT_NONE);
1398 * The busy flags are only cleared at
1399 * interrupt -- minimize the spl transitions
1402 if (vm_page_sleep_busy(p, TRUE, "vmopar"))
1405 if (clean_only && p->valid) {
1406 vm_page_test_dirty(p);
1407 if (p->valid & p->dirty)
1412 vm_page_protect(p, VM_PROT_NONE);
1418 if ((p = vm_page_lookup(object, start)) != 0) {
1420 if (p->wire_count != 0) {
1421 vm_page_protect(p, VM_PROT_NONE);
1430 * The busy flags are only cleared at
1431 * interrupt -- minimize the spl transitions
1433 if (vm_page_sleep_busy(p, TRUE, "vmopar"))
1436 if (clean_only && p->valid) {
1437 vm_page_test_dirty(p);
1438 if (p->valid & p->dirty) {
1446 vm_page_protect(p, VM_PROT_NONE);
1453 vm_object_pip_wakeup(object);
1457 * Routine: vm_object_coalesce
1458 * Function: Coalesces two objects backing up adjoining
1459 * regions of memory into a single object.
1461 * returns TRUE if objects were combined.
1463 * NOTE: Only works at the moment if the second object is NULL -
1464 * if it's not, which object do we lock first?
1467 * prev_object First object to coalesce
1468 * prev_offset Offset into prev_object
1469 * next_object Second object into coalesce
1470 * next_offset Offset into next_object
1472 * prev_size Size of reference to prev_object
1473 * next_size Size of reference to next_object
1476 * The object must *not* be locked.
1479 vm_object_coalesce(prev_object, prev_pindex, prev_size, next_size)
1480 register vm_object_t prev_object;
1481 vm_pindex_t prev_pindex;
1482 vm_size_t prev_size, next_size;
1486 if (prev_object == NULL) {
1490 if (prev_object->type != OBJT_DEFAULT &&
1491 prev_object->type != OBJT_SWAP) {
1496 * Try to collapse the object first
1498 vm_object_collapse(prev_object);
1501 * Can't coalesce if: . more than one reference . paged out . shadows
1502 * another object . has a copy elsewhere (any of which mean that the
1503 * pages not mapped to prev_entry may be in use anyway)
1506 if (prev_object->backing_object != NULL) {
1510 prev_size >>= PAGE_SHIFT;
1511 next_size >>= PAGE_SHIFT;
1513 if ((prev_object->ref_count > 1) &&
1514 (prev_object->size != prev_pindex + prev_size)) {
1519 * Remove any pages that may still be in the object from a previous
1523 vm_object_page_remove(prev_object,
1524 prev_pindex + prev_size,
1525 prev_pindex + prev_size + next_size, FALSE);
1528 * Extend the object if necessary.
1530 newsize = prev_pindex + prev_size + next_size;
1531 if (newsize > prev_object->size)
1532 prev_object->size = newsize;
1537 #include "opt_ddb.h"
1539 #include <sys/kernel.h>
1541 #include <machine/cons.h>
1543 #include <ddb/ddb.h>
1545 static int _vm_object_in_map __P((vm_map_t map, vm_object_t object,
1546 vm_map_entry_t entry));
1547 static int vm_object_in_map __P((vm_object_t object));
1550 _vm_object_in_map(map, object, entry)
1553 vm_map_entry_t entry;
1556 vm_map_entry_t tmpe;
1564 tmpe = map->header.next;
1565 entcount = map->nentries;
1566 while (entcount-- && (tmpe != &map->header)) {
1567 if( _vm_object_in_map(map, object, tmpe)) {
1572 } else if (entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) {
1573 tmpm = entry->object.share_map;
1574 tmpe = tmpm->header.next;
1575 entcount = tmpm->nentries;
1576 while (entcount-- && tmpe != &tmpm->header) {
1577 if( _vm_object_in_map(tmpm, object, tmpe)) {
1582 } else if ((obj = entry->object.vm_object) != NULL) {
1583 for(; obj; obj=obj->backing_object)
1584 if( obj == object) {
1592 vm_object_in_map( object)
1596 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
1597 if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1599 if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0))
1602 if( _vm_object_in_map( kernel_map, object, 0))
1604 if( _vm_object_in_map( kmem_map, object, 0))
1606 if( _vm_object_in_map( pager_map, object, 0))
1608 if( _vm_object_in_map( buffer_map, object, 0))
1610 if( _vm_object_in_map( io_map, object, 0))
1612 if( _vm_object_in_map( phys_map, object, 0))
1614 if( _vm_object_in_map( mb_map, object, 0))
1616 if( _vm_object_in_map( u_map, object, 0))
1621 DB_SHOW_COMMAND(vmochk, vm_object_check)
1626 * make sure that internal objs are in a map somewhere
1627 * and none have zero ref counts.
1629 for (object = TAILQ_FIRST(&vm_object_list);
1631 object = TAILQ_NEXT(object, object_list)) {
1632 if (object->handle == NULL &&
1633 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
1634 if (object->ref_count == 0) {
1635 db_printf("vmochk: internal obj has zero ref count: %d\n",
1638 if (!vm_object_in_map(object)) {
1640 "vmochk: internal obj is not in a map: "
1641 "ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
1642 object->ref_count, (u_long)object->size,
1643 (u_long)object->size,
1644 (void *)object->backing_object);
1651 * vm_object_print: [ debug ]
1653 DB_SHOW_COMMAND(object, vm_object_print_static)
1655 /* XXX convert args. */
1656 vm_object_t object = (vm_object_t)addr;
1657 boolean_t full = have_addr;
1659 register vm_page_t p;
1661 /* XXX count is an (unused) arg. Avoid shadowing it. */
1662 #define count was_count
1670 "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n",
1671 object, (int)object->type, (u_long)object->size,
1672 object->resident_page_count, object->ref_count, object->flags);
1674 * XXX no %qd in kernel. Truncate object->backing_object_offset.
1676 db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%lx\n",
1677 object->shadow_count,
1678 object->backing_object ? object->backing_object->ref_count : 0,
1679 object->backing_object, (long)object->backing_object_offset);
1686 for (p = TAILQ_FIRST(&object->memq); p != NULL; p = TAILQ_NEXT(p, listq)) {
1688 db_iprintf("memory:=");
1689 else if (count == 6) {
1697 db_printf("(off=0x%lx,page=0x%lx)",
1698 (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
1708 /* XXX need this non-static entry for calling from vm_map_print. */
1710 vm_object_print(addr, have_addr, count, modif)
1711 /* db_expr_t */ long addr;
1712 boolean_t have_addr;
1713 /* db_expr_t */ long count;
1716 vm_object_print_static(addr, have_addr, count, modif);
1719 DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
1724 for (object = TAILQ_FIRST(&vm_object_list);
1726 object = TAILQ_NEXT(object, object_list)) {
1727 vm_pindex_t idx, fidx;
1729 vm_offset_t pa = -1, padiff;
1733 db_printf("new object: %p\n", (void *)object);
1743 osize = object->size;
1746 for(idx=0;idx<osize;idx++) {
1747 m = vm_page_lookup(object, idx);
1750 db_printf(" index(%d)run(%d)pa(0x%x)\n",
1766 (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
1771 padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
1772 padiff >>= PAGE_SHIFT;
1773 padiff &= PQ_L2_MASK;
1775 pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
1779 db_printf(" index(%d)run(%d)pa(0x%x)", fidx, rcount, pa);
1780 db_printf("pd(%d)\n", padiff);
1790 pa = VM_PAGE_TO_PHYS(m);
1794 db_printf(" index(%d)run(%d)pa(0x%x)\n", fidx, rcount, pa);