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.147 1999/02/08 05:15:54 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];
142 _vm_object_allocate(type, size, object)
148 TAILQ_INIT(&object->memq);
149 TAILQ_INIT(&object->shadow_head);
153 object->ref_count = 1;
155 if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
156 vm_object_set_flag(object, OBJ_ONEMAPPING);
157 object->behavior = OBJ_NORMAL;
158 object->paging_in_progress = 0;
159 object->resident_page_count = 0;
160 object->cache_count = 0;
161 object->wire_count = 0;
162 object->shadow_count = 0;
163 object->pg_color = next_index;
164 if ( size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
165 incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
168 next_index = (next_index + incr) & PQ_L2_MASK;
169 object->handle = NULL;
170 object->backing_object = NULL;
171 object->backing_object_offset = (vm_ooffset_t) 0;
173 * Try to generate a number that will spread objects out in the
174 * hash table. We 'wipe' new objects across the hash in 128 page
175 * increments plus 1 more to offset it a little more by the time
178 object->hash_rand = object_hash_rand - 129;
180 object->last_read = 0;
181 object->generation++;
183 TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
185 object_hash_rand = object->hash_rand;
191 * Initialize the VM objects module.
196 TAILQ_INIT(&vm_object_list);
197 simple_lock_init(&vm_object_list_lock);
200 kernel_object = &kernel_object_store;
201 _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
204 kmem_object = &kmem_object_store;
205 _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
208 obj_zone = &obj_zone_store;
209 zbootinit(obj_zone, "VM OBJECT", sizeof (struct vm_object),
210 vm_objects_init, VM_OBJECTS_INIT);
215 zinitna(obj_zone, NULL, NULL, 0, 0, 0, 1);
219 * vm_object_allocate:
221 * Returns a new object with the given size.
225 vm_object_allocate(type, size)
231 result = (vm_object_t) zalloc(obj_zone);
233 _vm_object_allocate(type, size, result);
240 * vm_object_reference:
242 * Gets another reference to the given object.
245 vm_object_reference(object)
251 KASSERT(!(object->flags & OBJ_DEAD),
252 ("vm_object_reference: attempting to reference dead obj"));
255 if (object->type == OBJT_VNODE) {
256 while (vget((struct vnode *) object->handle, LK_RETRY|LK_NOOBJ, curproc)) {
257 #if !defined(MAX_PERF)
258 printf("vm_object_reference: delay in getting object\n");
265 vm_object_vndeallocate(object)
268 struct vnode *vp = (struct vnode *) object->handle;
270 KASSERT(object->type == OBJT_VNODE,
271 ("vm_object_vndeallocate: not a vnode object"));
272 KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
274 if (object->ref_count == 0) {
275 vprint("vm_object_vndeallocate", vp);
276 panic("vm_object_vndeallocate: bad object reference count");
281 if (object->ref_count == 0) {
282 vp->v_flag &= ~VTEXT;
283 vm_object_clear_flag(object, OBJ_OPT);
289 * vm_object_deallocate:
291 * Release a reference to the specified object,
292 * gained either through a vm_object_allocate
293 * or a vm_object_reference call. When all references
294 * are gone, storage associated with this object
295 * may be relinquished.
297 * No object may be locked.
300 vm_object_deallocate(object)
305 while (object != NULL) {
307 if (object->type == OBJT_VNODE) {
308 vm_object_vndeallocate(object);
312 if (object->ref_count == 0) {
313 panic("vm_object_deallocate: object deallocated too many times: %d", object->type);
314 } else if (object->ref_count > 2) {
320 * Here on ref_count of one or two, which are special cases for
323 if ((object->ref_count == 2) && (object->shadow_count == 0)) {
324 vm_object_set_flag(object, OBJ_ONEMAPPING);
327 } else if ((object->ref_count == 2) && (object->shadow_count == 1)) {
329 if ((object->handle == NULL) &&
330 (object->type == OBJT_DEFAULT ||
331 object->type == OBJT_SWAP)) {
334 robject = TAILQ_FIRST(&object->shadow_head);
335 KASSERT(robject != NULL,
336 ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
338 object->shadow_count));
339 if ((robject->handle == NULL) &&
340 (robject->type == OBJT_DEFAULT ||
341 robject->type == OBJT_SWAP)) {
343 robject->ref_count++;
346 robject->paging_in_progress ||
347 object->paging_in_progress
349 vm_object_pip_sleep(robject, "objde1");
350 vm_object_pip_sleep(object, "objde2");
353 if (robject->ref_count == 1) {
354 robject->ref_count--;
360 vm_object_collapse(object);
369 if (object->ref_count != 0)
375 temp = object->backing_object;
377 TAILQ_REMOVE(&temp->shadow_head, object, shadow_list);
378 temp->shadow_count--;
379 if (temp->ref_count == 0)
380 vm_object_clear_flag(temp, OBJ_OPT);
382 object->backing_object = NULL;
384 vm_object_terminate(object);
385 /* unlocks and deallocates object */
391 * vm_object_terminate actually destroys the specified object, freeing
392 * up all previously used resources.
394 * The object must be locked.
395 * This routine may block.
398 vm_object_terminate(object)
405 * Make sure no one uses us.
407 vm_object_set_flag(object, OBJ_DEAD);
410 * wait for the pageout daemon to be done with the object
412 vm_object_pip_wait(object, "objtrm");
414 KASSERT(!object->paging_in_progress,
415 ("vm_object_terminate: pageout in progress"));
418 * Clean and free the pages, as appropriate. All references to the
419 * object are gone, so we don't need to lock it.
421 if (object->type == OBJT_VNODE) {
425 * Freeze optimized copies.
427 vm_freeze_copyopts(object, 0, object->size);
430 * Clean pages and flush buffers.
432 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
434 vp = (struct vnode *) object->handle;
435 vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
438 if (object->ref_count != 0)
439 panic("vm_object_terminate: object with references, ref_count=%d", object->ref_count);
442 * Now free any remaining pages. For internal objects, this also
443 * removes them from paging queues. Don't free wired pages, just
444 * remove them from the object.
447 while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
448 #if !defined(MAX_PERF)
449 if (p->busy || (p->flags & PG_BUSY))
450 panic("vm_object_terminate: freeing busy page %p\n", p);
452 if (p->wire_count == 0) {
464 * Let the pager know object is dead.
466 vm_pager_deallocate(object);
469 * Remove the object from the global object list.
471 simple_lock(&vm_object_list_lock);
472 TAILQ_REMOVE(&vm_object_list, object, object_list);
473 simple_unlock(&vm_object_list_lock);
478 * Free the space for the object.
480 zfree(obj_zone, object);
484 * vm_object_page_clean
486 * Clean all dirty pages in the specified range of object.
487 * Leaves page on whatever queue it is currently on.
489 * Odd semantics: if start == end, we clean everything.
491 * The object must be locked.
495 vm_object_page_clean(object, start, end, flags)
502 vm_offset_t tstart, tend;
512 vm_page_t maf[vm_pageout_page_count];
513 vm_page_t mab[vm_pageout_page_count];
514 vm_page_t ma[vm_pageout_page_count];
517 if (object->type != OBJT_VNODE ||
518 (object->flags & OBJ_MIGHTBEDIRTY) == 0)
521 pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : 0;
522 pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
526 vm_object_set_flag(object, OBJ_CLEANING);
535 for(p = TAILQ_FIRST(&object->memq); p; p = TAILQ_NEXT(p, listq)) {
536 vm_page_flag_set(p, PG_CLEANCHK);
537 vm_page_protect(p, VM_PROT_READ);
540 if ((tstart == 0) && (tend == object->size)) {
541 vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
545 curgeneration = object->generation;
547 for(p = TAILQ_FIRST(&object->memq); p; p = np) {
548 np = TAILQ_NEXT(p, listq);
551 if (((p->flags & PG_CLEANCHK) == 0) ||
552 (pi < tstart) || (pi >= tend) ||
554 ((p->queue - p->pc) == PQ_CACHE)) {
555 vm_page_flag_clear(p, PG_CLEANCHK);
559 vm_page_test_dirty(p);
560 if ((p->dirty & p->valid) == 0) {
561 vm_page_flag_clear(p, PG_CLEANCHK);
566 while (vm_page_sleep_busy(p, TRUE, "vpcwai")) {
567 if (object->generation != curgeneration) {
574 for(i=1;i<vm_pageout_page_count;i++) {
575 if ((tp = vm_page_lookup(object, pi + i)) != NULL) {
576 if ((tp->flags & PG_BUSY) ||
577 (tp->flags & PG_CLEANCHK) == 0 ||
580 if((tp->queue - tp->pc) == PQ_CACHE) {
581 vm_page_flag_clear(tp, PG_CLEANCHK);
584 vm_page_test_dirty(tp);
585 if ((tp->dirty & tp->valid) == 0) {
586 vm_page_flag_clear(tp, PG_CLEANCHK);
597 chkb = vm_pageout_page_count - maxf;
599 for(i = 1; i < chkb;i++) {
600 if ((tp = vm_page_lookup(object, pi - i)) != NULL) {
601 if ((tp->flags & PG_BUSY) ||
602 (tp->flags & PG_CLEANCHK) == 0 ||
605 if((tp->queue - tp->pc) == PQ_CACHE) {
606 vm_page_flag_clear(tp, PG_CLEANCHK);
609 vm_page_test_dirty(tp);
610 if ((tp->dirty & tp->valid) == 0) {
611 vm_page_flag_clear(tp, PG_CLEANCHK);
622 for(i=0;i<maxb;i++) {
623 int index = (maxb - i) - 1;
625 vm_page_flag_clear(ma[index], PG_CLEANCHK);
627 vm_page_flag_clear(p, PG_CLEANCHK);
629 for(i=0;i<maxf;i++) {
630 int index = (maxb + i) + 1;
632 vm_page_flag_clear(ma[index], PG_CLEANCHK);
634 runlen = maxb + maxf + 1;
637 vm_pageout_flush(ma, runlen, pagerflags);
638 for (i = 0; i<runlen; i++) {
639 if (ma[i]->valid & ma[i]->dirty) {
640 vm_page_protect(ma[i], VM_PROT_READ);
641 vm_page_flag_set(ma[i], PG_CLEANCHK);
644 if (object->generation != curgeneration)
648 VOP_FSYNC(vp, NULL, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
650 vm_object_clear_flag(object, OBJ_CLEANING);
655 /* XXX I cannot tell if this should be an exported symbol */
657 * vm_object_deactivate_pages
659 * Deactivate all pages in the specified object. (Keep its pages
660 * in memory even though it is no longer referenced.)
662 * The object must be locked.
665 vm_object_deactivate_pages(object)
670 for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
671 next = TAILQ_NEXT(p, listq);
672 vm_page_deactivate(p);
678 * Same as vm_object_pmap_copy, except range checking really
679 * works, and is meant for small sections of an object.
681 * This code protects resident pages by making them read-only
682 * and is typically called on a fork or split when a page
683 * is converted to copy-on-write.
685 * NOTE: If the page is already at VM_PROT_NONE, calling
686 * vm_page_protect will have no effect.
690 vm_object_pmap_copy_1(object, start, end)
698 if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
701 for (idx = start; idx < end; idx++) {
702 p = vm_page_lookup(object, idx);
705 vm_page_protect(p, VM_PROT_READ);
710 * vm_object_pmap_remove:
712 * Removes all physical pages in the specified
713 * object range from all physical maps.
715 * The object must *not* be locked.
718 vm_object_pmap_remove(object, start, end)
727 for (p = TAILQ_FIRST(&object->memq);
729 p = TAILQ_NEXT(p, listq)) {
730 if (p->pindex >= start && p->pindex < end)
731 vm_page_protect(p, VM_PROT_NONE);
733 if ((start == 0) && (object->size == end))
734 vm_object_clear_flag(object, OBJ_WRITEABLE);
740 * Implements the madvise function at the object/page level.
742 * Currently, madvise() functions are limited to the default and
743 * swap object types only, and also limited to only the unshared portions
744 * of a process's address space. MADV_FREE, certainly, could never be
745 * run on anything else. The others are more flexible and the code could
746 * be adjusted in the future to handle expanded cases for them.
749 vm_object_madvise(object, pindex, count, advise)
755 vm_pindex_t end, tpindex;
762 end = pindex + count;
765 * MADV_FREE special case - free any swap backing store (as well
766 * as resident pages later on).
769 if (advise == MADV_FREE) {
774 (tobject->type == OBJT_DEFAULT ||
775 tobject->type == OBJT_SWAP) &&
776 (tobject->flags & OBJ_ONEMAPPING)
778 if (tobject->type == OBJT_SWAP) {
779 swap_pager_freespace(tobject, tpindex, count);
781 if ((tobject = tobject->backing_object) == NULL)
783 tpindex += OFF_TO_IDX(tobject->backing_object_offset);
788 * Locate and adjust resident pages
791 for (; pindex < end; pindex += 1) {
797 if (tobject->type != OBJT_DEFAULT &&
798 tobject->type != OBJT_SWAP
803 if ((tobject->flags & OBJ_ONEMAPPING) == 0)
806 m = vm_page_lookup(tobject, tpindex);
809 tobject = tobject->backing_object;
812 tpindex += OFF_TO_IDX(tobject->backing_object_offset);
817 * If the page is busy or not in a normal active state,
818 * we skip it. Things can break if we mess with pages
819 * in any of the below states.
824 m->valid != VM_PAGE_BITS_ALL
829 if (vm_page_sleep_busy(m, TRUE, "madvpo"))
832 if (advise == MADV_WILLNEED) {
834 } else if (advise == MADV_DONTNEED) {
835 vm_page_deactivate(m);
836 } else if (advise == MADV_FREE) {
838 * If MADV_FREE_FORCE_FREE is defined, we attempt to
839 * immediately free the page. Otherwise we just
840 * destroy any swap backing store, mark it clean,
841 * and stuff it into the cache.
843 pmap_clear_modify(VM_PAGE_TO_PHYS(m));
846 #ifdef MADV_FREE_FORCE_FREE
847 if (tobject->resident_page_count > 1) {
849 vm_page_protect(m, VM_PROT_NONE);
863 * Create a new object which is backed by the
864 * specified existing object range. The source
865 * object reference is deallocated.
867 * The new object and offset into that object
868 * are returned in the source parameters.
872 vm_object_shadow(object, offset, length)
873 vm_object_t *object; /* IN/OUT */
874 vm_ooffset_t *offset; /* IN/OUT */
883 * Allocate a new object with the given length
886 if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL)
887 panic("vm_object_shadow: no object for shadowing");
890 * The new object shadows the source object, adding a reference to it.
891 * Our caller changes his reference to point to the new object,
892 * removing a reference to the source object. Net result: no change
893 * of reference count.
895 * Try to optimize the result object's page color when shadowing
896 * in order to maintain page coloring consistancy in the combined
899 result->backing_object = source;
901 TAILQ_INSERT_TAIL(&source->shadow_head, result, shadow_list);
902 vm_object_clear_flag(source, OBJ_ONEMAPPING);
903 source->shadow_count++;
904 source->generation++;
905 result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) & PQ_L2_MASK;
909 * Store the offset into the source object, and fix up the offset into
913 result->backing_object_offset = *offset;
916 * Return the new things
923 #define OBSC_TEST_ALL_SHADOWED 0x0001
924 #define OBSC_COLLAPSE_NOWAIT 0x0002
925 #define OBSC_COLLAPSE_WAIT 0x0004
928 vm_object_backing_scan(vm_object_t object, int op)
933 vm_object_t backing_object;
934 vm_pindex_t backing_offset_index;
938 backing_object = object->backing_object;
939 backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
945 if (op & OBSC_TEST_ALL_SHADOWED) {
947 * We do not want to have to test for the existance of
948 * swap pages in the backing object. XXX but with the
949 * new swapper this would be pretty easy to do.
951 * XXX what about anonymous MAP_SHARED memory that hasn't
952 * been ZFOD faulted yet? If we do not test for this, the
953 * shadow test may succeed! XXX
955 if (backing_object->type != OBJT_DEFAULT) {
960 if (op & OBSC_COLLAPSE_WAIT) {
961 vm_object_set_flag(backing_object, OBJ_DEAD);
968 p = TAILQ_FIRST(&backing_object->memq);
970 vm_page_t next = TAILQ_NEXT(p, listq);
971 vm_pindex_t new_pindex = p->pindex - backing_offset_index;
973 if (op & OBSC_TEST_ALL_SHADOWED) {
977 * Ignore pages outside the parent object's range
978 * and outside the parent object's mapping of the
981 * note that we do not busy the backing object's
986 p->pindex < backing_offset_index ||
987 new_pindex >= object->size
994 * See if the parent has the page or if the parent's
995 * object pager has the page. If the parent has the
996 * page but the page is not valid, the parent's
997 * object pager must have the page.
999 * If this fails, the parent does not completely shadow
1000 * the object and we might as well give up now.
1003 pp = vm_page_lookup(object, new_pindex);
1005 (pp == NULL || pp->valid == 0) &&
1006 !vm_pager_has_page(object, new_pindex, NULL, NULL)
1014 * Check for busy page
1017 if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
1020 if (op & OBSC_COLLAPSE_NOWAIT) {
1022 (p->flags & PG_BUSY) ||
1031 } else if (op & OBSC_COLLAPSE_WAIT) {
1032 if (vm_page_sleep_busy(p, TRUE, "vmocol")) {
1034 * If we slept, anything could have
1035 * happened. Since the object is
1036 * marked dead, the backing offset
1037 * should not have changed so we
1038 * just restart our scan.
1040 p = TAILQ_FIRST(&backing_object->memq);
1051 p->object == backing_object,
1052 ("vm_object_qcollapse(): object mismatch")
1056 * Destroy any associated swap
1058 if (backing_object->type == OBJT_SWAP) {
1059 swap_pager_freespace(
1067 p->pindex < backing_offset_index ||
1068 new_pindex >= object->size
1071 * Page is out of the parent object's range, we
1072 * can simply destroy it.
1074 vm_page_protect(p, VM_PROT_NONE);
1080 pp = vm_page_lookup(object, new_pindex);
1083 vm_pager_has_page(object, new_pindex, NULL, NULL)
1086 * page already exists in parent OR swap exists
1087 * for this location in the parent. Destroy
1088 * the original page from the backing object.
1090 * Leave the parent's page alone
1092 vm_page_protect(p, VM_PROT_NONE);
1099 * Page does not exist in parent, rename the
1100 * page from the backing object to the main object.
1102 if ((p->queue - p->pc) == PQ_CACHE)
1103 vm_page_deactivate(p);
1105 vm_page_protect(p, VM_PROT_NONE);
1107 vm_page_rename(p, object, new_pindex);
1108 /* page automatically made dirty by rename */
1118 * this version of collapse allows the operation to occur earlier and
1119 * when paging_in_progress is true for an object... This is not a complete
1120 * operation, but should plug 99.9% of the rest of the leaks.
1123 vm_object_qcollapse(object)
1126 vm_object_t backing_object = object->backing_object;
1128 if (backing_object->ref_count != 1)
1131 backing_object->ref_count += 2;
1133 vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
1135 backing_object->ref_count -= 2;
1139 * vm_object_collapse:
1141 * Collapse an object with the object backing it.
1142 * Pages in the backing object are moved into the
1143 * parent, and the backing object is deallocated.
1146 vm_object_collapse(object)
1150 vm_object_t backing_object;
1153 * Verify that the conditions are right for collapse:
1155 * The object exists and the backing object exists.
1160 if ((backing_object = object->backing_object) == NULL)
1164 * we check the backing object first, because it is most likely
1167 if (backing_object->handle != NULL ||
1168 (backing_object->type != OBJT_DEFAULT &&
1169 backing_object->type != OBJT_SWAP) ||
1170 (backing_object->flags & OBJ_DEAD) ||
1171 object->handle != NULL ||
1172 (object->type != OBJT_DEFAULT &&
1173 object->type != OBJT_SWAP) ||
1174 (object->flags & OBJ_DEAD)) {
1179 object->paging_in_progress != 0 ||
1180 backing_object->paging_in_progress != 0
1182 vm_object_qcollapse(object);
1187 * We know that we can either collapse the backing object (if
1188 * the parent is the only reference to it) or (perhaps) have
1189 * the parent bypass the object if the parent happens to shadow
1190 * all the resident pages in the entire backing object.
1192 * This is ignoring pager-backed pages such as swap pages.
1193 * vm_object_backing_scan fails the shadowing test in this
1197 if (backing_object->ref_count == 1) {
1199 * If there is exactly one reference to the backing
1200 * object, we can collapse it into the parent.
1203 vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1206 * Move the pager from backing_object to object.
1209 if (backing_object->type == OBJT_SWAP) {
1210 vm_object_pip_add(backing_object, 1);
1213 * scrap the paging_offset junk and do a
1214 * discrete copy. This also removes major
1215 * assumptions about how the swap-pager
1216 * works from where it doesn't belong. The
1217 * new swapper is able to optimize the
1218 * destroy-source case.
1221 vm_object_pip_add(object, 1);
1225 OFF_TO_IDX(object->backing_object_offset), TRUE);
1226 vm_object_pip_wakeup(object);
1228 vm_object_pip_wakeup(backing_object);
1231 * Object now shadows whatever backing_object did.
1232 * Note that the reference to
1233 * backing_object->backing_object moves from within
1234 * backing_object to within object.
1238 &object->backing_object->shadow_head,
1242 object->backing_object->shadow_count--;
1243 object->backing_object->generation++;
1244 if (backing_object->backing_object) {
1246 &backing_object->backing_object->shadow_head,
1250 backing_object->backing_object->shadow_count--;
1251 backing_object->backing_object->generation++;
1253 object->backing_object = backing_object->backing_object;
1254 if (object->backing_object) {
1256 &object->backing_object->shadow_head,
1260 object->backing_object->shadow_count++;
1261 object->backing_object->generation++;
1264 object->backing_object_offset +=
1265 backing_object->backing_object_offset;
1268 * Discard backing_object.
1270 * Since the backing object has no pages, no pager left,
1271 * and no object references within it, all that is
1272 * necessary is to dispose of it.
1282 zfree(obj_zone, backing_object);
1286 vm_object_t new_backing_object;
1289 * If we do not entirely shadow the backing object,
1290 * there is nothing we can do so we give up.
1293 if (vm_object_backing_scan(object, OBSC_TEST_ALL_SHADOWED) == 0) {
1298 * Make the parent shadow the next object in the
1299 * chain. Deallocating backing_object will not remove
1300 * it, since its reference count is at least 2.
1304 &backing_object->shadow_head,
1308 backing_object->shadow_count--;
1309 backing_object->generation++;
1311 new_backing_object = backing_object->backing_object;
1312 if ((object->backing_object = new_backing_object) != NULL) {
1313 vm_object_reference(new_backing_object);
1315 &new_backing_object->shadow_head,
1319 new_backing_object->shadow_count++;
1320 new_backing_object->generation++;
1321 object->backing_object_offset +=
1322 backing_object->backing_object_offset;
1326 * Drop the reference count on backing_object. Since
1327 * its ref_count was at least 2, it will not vanish;
1328 * so we don't need to call vm_object_deallocate, but
1331 vm_object_deallocate(backing_object);
1336 * Try again with this object's new backing object.
1342 * vm_object_page_remove: [internal]
1344 * Removes all physical pages in the specified
1345 * object range from the object's list of pages.
1347 * The object must be locked.
1350 vm_object_page_remove(object, start, end, clean_only)
1354 boolean_t clean_only;
1363 all = ((end == 0) && (start == 0));
1365 vm_object_pip_add(object, 1);
1368 if (all || size > 4 || size >= object->size / 4) {
1369 for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
1370 next = TAILQ_NEXT(p, listq);
1371 if (all || ((start <= p->pindex) && (p->pindex < end))) {
1372 if (p->wire_count != 0) {
1373 vm_page_protect(p, VM_PROT_NONE);
1380 * The busy flags are only cleared at
1381 * interrupt -- minimize the spl transitions
1384 if (vm_page_sleep_busy(p, TRUE, "vmopar"))
1387 if (clean_only && p->valid) {
1388 vm_page_test_dirty(p);
1389 if (p->valid & p->dirty)
1394 vm_page_protect(p, VM_PROT_NONE);
1400 if ((p = vm_page_lookup(object, start)) != 0) {
1402 if (p->wire_count != 0) {
1403 vm_page_protect(p, VM_PROT_NONE);
1412 * The busy flags are only cleared at
1413 * interrupt -- minimize the spl transitions
1415 if (vm_page_sleep_busy(p, TRUE, "vmopar"))
1418 if (clean_only && p->valid) {
1419 vm_page_test_dirty(p);
1420 if (p->valid & p->dirty) {
1428 vm_page_protect(p, VM_PROT_NONE);
1435 vm_object_pip_wakeup(object);
1439 * Routine: vm_object_coalesce
1440 * Function: Coalesces two objects backing up adjoining
1441 * regions of memory into a single object.
1443 * returns TRUE if objects were combined.
1445 * NOTE: Only works at the moment if the second object is NULL -
1446 * if it's not, which object do we lock first?
1449 * prev_object First object to coalesce
1450 * prev_offset Offset into prev_object
1451 * next_object Second object into coalesce
1452 * next_offset Offset into next_object
1454 * prev_size Size of reference to prev_object
1455 * next_size Size of reference to next_object
1458 * The object must *not* be locked.
1461 vm_object_coalesce(prev_object, prev_pindex, prev_size, next_size)
1462 vm_object_t prev_object;
1463 vm_pindex_t prev_pindex;
1464 vm_size_t prev_size, next_size;
1468 if (prev_object == NULL) {
1472 if (prev_object->type != OBJT_DEFAULT &&
1473 prev_object->type != OBJT_SWAP) {
1478 * Try to collapse the object first
1480 vm_object_collapse(prev_object);
1483 * Can't coalesce if: . more than one reference . paged out . shadows
1484 * another object . has a copy elsewhere (any of which mean that the
1485 * pages not mapped to prev_entry may be in use anyway)
1488 if (prev_object->backing_object != NULL) {
1492 prev_size >>= PAGE_SHIFT;
1493 next_size >>= PAGE_SHIFT;
1495 if ((prev_object->ref_count > 1) &&
1496 (prev_object->size != prev_pindex + prev_size)) {
1501 * Remove any pages that may still be in the object from a previous
1505 vm_object_page_remove(prev_object,
1506 prev_pindex + prev_size,
1507 prev_pindex + prev_size + next_size, FALSE);
1510 * Extend the object if necessary.
1512 newsize = prev_pindex + prev_size + next_size;
1513 if (newsize > prev_object->size)
1514 prev_object->size = newsize;
1519 #include "opt_ddb.h"
1521 #include <sys/kernel.h>
1523 #include <machine/cons.h>
1525 #include <ddb/ddb.h>
1527 static int _vm_object_in_map __P((vm_map_t map, vm_object_t object,
1528 vm_map_entry_t entry));
1529 static int vm_object_in_map __P((vm_object_t object));
1532 _vm_object_in_map(map, object, entry)
1535 vm_map_entry_t entry;
1538 vm_map_entry_t tmpe;
1546 tmpe = map->header.next;
1547 entcount = map->nentries;
1548 while (entcount-- && (tmpe != &map->header)) {
1549 if( _vm_object_in_map(map, object, tmpe)) {
1554 } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
1555 tmpm = entry->object.sub_map;
1556 tmpe = tmpm->header.next;
1557 entcount = tmpm->nentries;
1558 while (entcount-- && tmpe != &tmpm->header) {
1559 if( _vm_object_in_map(tmpm, object, tmpe)) {
1564 } else if ((obj = entry->object.vm_object) != NULL) {
1565 for(; obj; obj=obj->backing_object)
1566 if( obj == object) {
1574 vm_object_in_map( object)
1578 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
1579 if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1581 if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0))
1584 if( _vm_object_in_map( kernel_map, object, 0))
1586 if( _vm_object_in_map( kmem_map, object, 0))
1588 if( _vm_object_in_map( pager_map, object, 0))
1590 if( _vm_object_in_map( buffer_map, object, 0))
1592 if( _vm_object_in_map( io_map, object, 0))
1594 if( _vm_object_in_map( phys_map, object, 0))
1596 if( _vm_object_in_map( mb_map, object, 0))
1598 if( _vm_object_in_map( u_map, object, 0))
1603 DB_SHOW_COMMAND(vmochk, vm_object_check)
1608 * make sure that internal objs are in a map somewhere
1609 * and none have zero ref counts.
1611 for (object = TAILQ_FIRST(&vm_object_list);
1613 object = TAILQ_NEXT(object, object_list)) {
1614 if (object->handle == NULL &&
1615 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
1616 if (object->ref_count == 0) {
1617 db_printf("vmochk: internal obj has zero ref count: %d\n",
1620 if (!vm_object_in_map(object)) {
1622 "vmochk: internal obj is not in a map: "
1623 "ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
1624 object->ref_count, (u_long)object->size,
1625 (u_long)object->size,
1626 (void *)object->backing_object);
1633 * vm_object_print: [ debug ]
1635 DB_SHOW_COMMAND(object, vm_object_print_static)
1637 /* XXX convert args. */
1638 vm_object_t object = (vm_object_t)addr;
1639 boolean_t full = have_addr;
1643 /* XXX count is an (unused) arg. Avoid shadowing it. */
1644 #define count was_count
1652 "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n",
1653 object, (int)object->type, (u_long)object->size,
1654 object->resident_page_count, object->ref_count, object->flags);
1656 * XXX no %qd in kernel. Truncate object->backing_object_offset.
1658 db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%lx\n",
1659 object->shadow_count,
1660 object->backing_object ? object->backing_object->ref_count : 0,
1661 object->backing_object, (long)object->backing_object_offset);
1668 for (p = TAILQ_FIRST(&object->memq); p != NULL; p = TAILQ_NEXT(p, listq)) {
1670 db_iprintf("memory:=");
1671 else if (count == 6) {
1679 db_printf("(off=0x%lx,page=0x%lx)",
1680 (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
1690 /* XXX need this non-static entry for calling from vm_map_print. */
1692 vm_object_print(addr, have_addr, count, modif)
1693 /* db_expr_t */ long addr;
1694 boolean_t have_addr;
1695 /* db_expr_t */ long count;
1698 vm_object_print_static(addr, have_addr, count, modif);
1701 DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
1706 for (object = TAILQ_FIRST(&vm_object_list);
1708 object = TAILQ_NEXT(object, object_list)) {
1709 vm_pindex_t idx, fidx;
1711 vm_offset_t pa = -1, padiff;
1715 db_printf("new object: %p\n", (void *)object);
1725 osize = object->size;
1728 for(idx=0;idx<osize;idx++) {
1729 m = vm_page_lookup(object, idx);
1732 db_printf(" index(%d)run(%d)pa(0x%x)\n",
1748 (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
1753 padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
1754 padiff >>= PAGE_SHIFT;
1755 padiff &= PQ_L2_MASK;
1757 pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
1761 db_printf(" index(%d)run(%d)pa(0x%x)", fidx, rcount, pa);
1762 db_printf("pd(%d)\n", padiff);
1772 pa = VM_PAGE_TO_PHYS(m);
1776 db_printf(" index(%d)run(%d)pa(0x%x)\n", fidx, rcount, pa);