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.159 1999/07/11 18:30:32 alc 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->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;
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;
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
176 object->hash_rand = object_hash_rand - 129;
178 object->generation++;
180 TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
182 object_hash_rand = object->hash_rand;
188 * Initialize the VM objects module.
193 TAILQ_INIT(&vm_object_list);
194 simple_lock_init(&vm_object_list_lock);
197 kernel_object = &kernel_object_store;
198 _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
201 kmem_object = &kmem_object_store;
202 _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
205 obj_zone = &obj_zone_store;
206 zbootinit(obj_zone, "VM OBJECT", sizeof (struct vm_object),
207 vm_objects_init, VM_OBJECTS_INIT);
212 zinitna(obj_zone, NULL, NULL, 0, 0, 0, 1);
216 * vm_object_allocate:
218 * Returns a new object with the given size.
222 vm_object_allocate(type, size)
228 result = (vm_object_t) zalloc(obj_zone);
230 _vm_object_allocate(type, size, result);
237 * vm_object_reference:
239 * Gets another reference to the given object.
242 vm_object_reference(object)
248 KASSERT(!(object->flags & OBJ_DEAD),
249 ("vm_object_reference: attempting to reference dead obj"));
252 if (object->type == OBJT_VNODE) {
253 while (vget((struct vnode *) object->handle, LK_RETRY|LK_NOOBJ, curproc)) {
254 #if !defined(MAX_PERF)
255 printf("vm_object_reference: delay in getting object\n");
262 vm_object_vndeallocate(object)
265 struct vnode *vp = (struct vnode *) object->handle;
267 KASSERT(object->type == OBJT_VNODE,
268 ("vm_object_vndeallocate: not a vnode object"));
269 KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
271 if (object->ref_count == 0) {
272 vprint("vm_object_vndeallocate", vp);
273 panic("vm_object_vndeallocate: bad object reference count");
278 if (object->ref_count == 0) {
279 vp->v_flag &= ~VTEXT;
280 vm_object_clear_flag(object, OBJ_OPT);
286 * vm_object_deallocate:
288 * Release a reference to the specified object,
289 * gained either through a vm_object_allocate
290 * or a vm_object_reference call. When all references
291 * are gone, storage associated with this object
292 * may be relinquished.
294 * No object may be locked.
297 vm_object_deallocate(object)
302 while (object != NULL) {
304 if (object->type == OBJT_VNODE) {
305 vm_object_vndeallocate(object);
309 if (object->ref_count == 0) {
310 panic("vm_object_deallocate: object deallocated too many times: %d", object->type);
311 } else if (object->ref_count > 2) {
317 * Here on ref_count of one or two, which are special cases for
320 if ((object->ref_count == 2) && (object->shadow_count == 0)) {
321 vm_object_set_flag(object, OBJ_ONEMAPPING);
324 } else if ((object->ref_count == 2) && (object->shadow_count == 1)) {
326 if ((object->handle == NULL) &&
327 (object->type == OBJT_DEFAULT ||
328 object->type == OBJT_SWAP)) {
331 robject = TAILQ_FIRST(&object->shadow_head);
332 KASSERT(robject != NULL,
333 ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
335 object->shadow_count));
336 if ((robject->handle == NULL) &&
337 (robject->type == OBJT_DEFAULT ||
338 robject->type == OBJT_SWAP)) {
340 robject->ref_count++;
343 robject->paging_in_progress ||
344 object->paging_in_progress
346 vm_object_pip_sleep(robject, "objde1");
347 vm_object_pip_sleep(object, "objde2");
350 if (robject->ref_count == 1) {
351 robject->ref_count--;
357 vm_object_collapse(object);
366 if (object->ref_count != 0)
372 temp = object->backing_object;
374 TAILQ_REMOVE(&temp->shadow_head, object, shadow_list);
375 temp->shadow_count--;
376 if (temp->ref_count == 0)
377 vm_object_clear_flag(temp, OBJ_OPT);
379 object->backing_object = NULL;
381 vm_object_terminate(object);
382 /* unlocks and deallocates object */
388 * vm_object_terminate actually destroys the specified object, freeing
389 * up all previously used resources.
391 * The object must be locked.
392 * This routine may block.
395 vm_object_terminate(object)
402 * Make sure no one uses us.
404 vm_object_set_flag(object, OBJ_DEAD);
407 * wait for the pageout daemon to be done with the object
409 vm_object_pip_wait(object, "objtrm");
411 KASSERT(!object->paging_in_progress,
412 ("vm_object_terminate: pageout in progress"));
415 * Clean and free the pages, as appropriate. All references to the
416 * object are gone, so we don't need to lock it.
418 if (object->type == OBJT_VNODE) {
422 * Freeze optimized copies.
424 vm_freeze_copyopts(object, 0, object->size);
427 * Clean pages and flush buffers.
429 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
431 vp = (struct vnode *) object->handle;
432 vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
435 if (object->ref_count != 0)
436 panic("vm_object_terminate: object with references, ref_count=%d", object->ref_count);
439 * Now free any remaining pages. For internal objects, this also
440 * removes them from paging queues. Don't free wired pages, just
441 * remove them from the object.
444 while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
445 #if !defined(MAX_PERF)
446 if (p->busy || (p->flags & PG_BUSY))
447 panic("vm_object_terminate: freeing busy page %p\n", p);
449 if (p->wire_count == 0) {
461 * Let the pager know object is dead.
463 vm_pager_deallocate(object);
466 * Remove the object from the global object list.
468 simple_lock(&vm_object_list_lock);
469 TAILQ_REMOVE(&vm_object_list, object, object_list);
470 simple_unlock(&vm_object_list_lock);
475 * Free the space for the object.
477 zfree(obj_zone, object);
481 * vm_object_page_clean
483 * Clean all dirty pages in the specified range of object.
484 * Leaves page on whatever queue it is currently on.
486 * Odd semantics: if start == end, we clean everything.
488 * The object must be locked.
492 vm_object_page_clean(object, start, end, flags)
499 vm_offset_t tstart, tend;
509 vm_page_t maf[vm_pageout_page_count];
510 vm_page_t mab[vm_pageout_page_count];
511 vm_page_t ma[vm_pageout_page_count];
514 if (object->type != OBJT_VNODE ||
515 (object->flags & OBJ_MIGHTBEDIRTY) == 0)
518 pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : 0;
519 pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
523 vm_object_set_flag(object, OBJ_CLEANING);
532 for(p = TAILQ_FIRST(&object->memq); p; p = TAILQ_NEXT(p, listq)) {
533 vm_page_flag_set(p, PG_CLEANCHK);
534 vm_page_protect(p, VM_PROT_READ);
537 if ((tstart == 0) && (tend == object->size)) {
538 vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
542 curgeneration = object->generation;
544 for(p = TAILQ_FIRST(&object->memq); p; p = np) {
545 np = TAILQ_NEXT(p, listq);
548 if (((p->flags & PG_CLEANCHK) == 0) ||
549 (pi < tstart) || (pi >= tend) ||
551 ((p->queue - p->pc) == PQ_CACHE)) {
552 vm_page_flag_clear(p, PG_CLEANCHK);
556 vm_page_test_dirty(p);
557 if ((p->dirty & p->valid) == 0) {
558 vm_page_flag_clear(p, PG_CLEANCHK);
563 while (vm_page_sleep_busy(p, TRUE, "vpcwai")) {
564 if (object->generation != curgeneration) {
571 for(i=1;i<vm_pageout_page_count;i++) {
572 if ((tp = vm_page_lookup(object, pi + i)) != NULL) {
573 if ((tp->flags & PG_BUSY) ||
574 (tp->flags & PG_CLEANCHK) == 0 ||
577 if((tp->queue - tp->pc) == PQ_CACHE) {
578 vm_page_flag_clear(tp, PG_CLEANCHK);
581 vm_page_test_dirty(tp);
582 if ((tp->dirty & tp->valid) == 0) {
583 vm_page_flag_clear(tp, PG_CLEANCHK);
594 chkb = vm_pageout_page_count - maxf;
596 for(i = 1; i < chkb;i++) {
597 if ((tp = vm_page_lookup(object, pi - i)) != NULL) {
598 if ((tp->flags & PG_BUSY) ||
599 (tp->flags & PG_CLEANCHK) == 0 ||
602 if((tp->queue - tp->pc) == PQ_CACHE) {
603 vm_page_flag_clear(tp, PG_CLEANCHK);
606 vm_page_test_dirty(tp);
607 if ((tp->dirty & tp->valid) == 0) {
608 vm_page_flag_clear(tp, PG_CLEANCHK);
619 for(i=0;i<maxb;i++) {
620 int index = (maxb - i) - 1;
622 vm_page_flag_clear(ma[index], PG_CLEANCHK);
624 vm_page_flag_clear(p, PG_CLEANCHK);
626 for(i=0;i<maxf;i++) {
627 int index = (maxb + i) + 1;
629 vm_page_flag_clear(ma[index], PG_CLEANCHK);
631 runlen = maxb + maxf + 1;
634 vm_pageout_flush(ma, runlen, pagerflags);
635 for (i = 0; i<runlen; i++) {
636 if (ma[i]->valid & ma[i]->dirty) {
637 vm_page_protect(ma[i], VM_PROT_READ);
638 vm_page_flag_set(ma[i], PG_CLEANCHK);
641 if (object->generation != curgeneration)
645 VOP_FSYNC(vp, NULL, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
647 vm_object_clear_flag(object, OBJ_CLEANING);
652 /* XXX I cannot tell if this should be an exported symbol */
654 * vm_object_deactivate_pages
656 * Deactivate all pages in the specified object. (Keep its pages
657 * in memory even though it is no longer referenced.)
659 * The object must be locked.
662 vm_object_deactivate_pages(object)
667 for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
668 next = TAILQ_NEXT(p, listq);
669 vm_page_deactivate(p);
675 * Same as vm_object_pmap_copy, except range checking really
676 * works, and is meant for small sections of an object.
678 * This code protects resident pages by making them read-only
679 * and is typically called on a fork or split when a page
680 * is converted to copy-on-write.
682 * NOTE: If the page is already at VM_PROT_NONE, calling
683 * vm_page_protect will have no effect.
687 vm_object_pmap_copy_1(object, start, end)
695 if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
698 for (idx = start; idx < end; idx++) {
699 p = vm_page_lookup(object, idx);
702 vm_page_protect(p, VM_PROT_READ);
707 * vm_object_pmap_remove:
709 * Removes all physical pages in the specified
710 * object range from all physical maps.
712 * The object must *not* be locked.
715 vm_object_pmap_remove(object, start, end)
724 for (p = TAILQ_FIRST(&object->memq);
726 p = TAILQ_NEXT(p, listq)) {
727 if (p->pindex >= start && p->pindex < end)
728 vm_page_protect(p, VM_PROT_NONE);
730 if ((start == 0) && (object->size == end))
731 vm_object_clear_flag(object, OBJ_WRITEABLE);
737 * Implements the madvise function at the object/page level.
739 * Currently, madvise() functions are limited to the default and
740 * swap object types only, and also limited to only the unshared portions
741 * of a process's address space. MADV_FREE, certainly, could never be
742 * run on anything else. The others are more flexible and the code could
743 * be adjusted in the future to handle expanded cases for them.
746 vm_object_madvise(object, pindex, count, advise)
752 vm_pindex_t end, tpindex;
759 end = pindex + count;
762 * Locate and adjust resident pages
765 for (; pindex < end; pindex += 1) {
771 if (tobject->type != OBJT_DEFAULT &&
772 tobject->type != OBJT_SWAP
777 if ((tobject->flags & OBJ_ONEMAPPING) == 0)
780 m = vm_page_lookup(tobject, tpindex);
784 * There may be swap even if there is no backing page
786 if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
787 swap_pager_freespace(tobject, tpindex, 1);
792 tobject = tobject->backing_object;
795 tpindex += OFF_TO_IDX(tobject->backing_object_offset);
800 * If the page is busy or not in a normal active state,
801 * we skip it. Things can break if we mess with pages
802 * in any of the below states.
807 m->valid != VM_PAGE_BITS_ALL
812 if (vm_page_sleep_busy(m, TRUE, "madvpo"))
815 if (advise == MADV_WILLNEED) {
817 } else if (advise == MADV_DONTNEED) {
818 vm_page_deactivate(m);
819 } else if (advise == MADV_FREE) {
821 * Mark the page clean. This will allow the page
822 * to be freed up by the system. However, such pages
823 * are often reused quickly by malloc()/free()
824 * so we do not do anything that would cause
825 * a page fault if we can help it.
827 * Specifically, we do not try to actually free
828 * the page now nor do we try to put it in the
829 * cache (which would cause a page fault on reuse).
831 * But we do make the page is freeable as we
832 * can without actually taking the step of unmapping
835 pmap_clear_modify(VM_PAGE_TO_PHYS(m));
838 vm_page_deactivate(m);
839 if (tobject->type == OBJT_SWAP)
840 swap_pager_freespace(tobject, tpindex, 1);
848 * Create a new object which is backed by the
849 * specified existing object range. The source
850 * object reference is deallocated.
852 * The new object and offset into that object
853 * are returned in the source parameters.
857 vm_object_shadow(object, offset, length)
858 vm_object_t *object; /* IN/OUT */
859 vm_ooffset_t *offset; /* IN/OUT */
868 * Don't create the new object if the old object isn't shared.
871 if (source != NULL &&
872 source->ref_count == 1 &&
873 source->handle == NULL &&
874 (source->type == OBJT_DEFAULT ||
875 source->type == OBJT_SWAP))
878 KASSERT((source->flags & OBJ_ONEMAPPING) == 0,
879 ("vm_object_shadow: source object has OBJ_ONEMAPPING set.\n"));
882 * Allocate a new object with the given length
885 if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL)
886 panic("vm_object_shadow: no object for shadowing");
889 * The new object shadows the source object, adding a reference to it.
890 * Our caller changes his reference to point to the new object,
891 * removing a reference to the source object. Net result: no change
892 * of reference count.
894 * Try to optimize the result object's page color when shadowing
895 * in order to maintain page coloring consistancy in the combined
898 result->backing_object = source;
900 TAILQ_INSERT_TAIL(&source->shadow_head, result, shadow_list);
901 source->shadow_count++;
902 source->generation++;
903 result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) & PQ_L2_MASK;
907 * Store the offset into the source object, and fix up the offset into
911 result->backing_object_offset = *offset;
914 * Return the new things
921 #define OBSC_TEST_ALL_SHADOWED 0x0001
922 #define OBSC_COLLAPSE_NOWAIT 0x0002
923 #define OBSC_COLLAPSE_WAIT 0x0004
926 vm_object_backing_scan(vm_object_t object, int op)
931 vm_object_t backing_object;
932 vm_pindex_t backing_offset_index;
936 backing_object = object->backing_object;
937 backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
943 if (op & OBSC_TEST_ALL_SHADOWED) {
945 * We do not want to have to test for the existance of
946 * swap pages in the backing object. XXX but with the
947 * new swapper this would be pretty easy to do.
949 * XXX what about anonymous MAP_SHARED memory that hasn't
950 * been ZFOD faulted yet? If we do not test for this, the
951 * shadow test may succeed! XXX
953 if (backing_object->type != OBJT_DEFAULT) {
958 if (op & OBSC_COLLAPSE_WAIT) {
959 vm_object_set_flag(backing_object, OBJ_DEAD);
966 p = TAILQ_FIRST(&backing_object->memq);
968 vm_page_t next = TAILQ_NEXT(p, listq);
969 vm_pindex_t new_pindex = p->pindex - backing_offset_index;
971 if (op & OBSC_TEST_ALL_SHADOWED) {
975 * Ignore pages outside the parent object's range
976 * and outside the parent object's mapping of the
979 * note that we do not busy the backing object's
984 p->pindex < backing_offset_index ||
985 new_pindex >= object->size
992 * See if the parent has the page or if the parent's
993 * object pager has the page. If the parent has the
994 * page but the page is not valid, the parent's
995 * object pager must have the page.
997 * If this fails, the parent does not completely shadow
998 * the object and we might as well give up now.
1001 pp = vm_page_lookup(object, new_pindex);
1003 (pp == NULL || pp->valid == 0) &&
1004 !vm_pager_has_page(object, new_pindex, NULL, NULL)
1012 * Check for busy page
1015 if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
1018 if (op & OBSC_COLLAPSE_NOWAIT) {
1020 (p->flags & PG_BUSY) ||
1029 } else if (op & OBSC_COLLAPSE_WAIT) {
1030 if (vm_page_sleep_busy(p, TRUE, "vmocol")) {
1032 * If we slept, anything could have
1033 * happened. Since the object is
1034 * marked dead, the backing offset
1035 * should not have changed so we
1036 * just restart our scan.
1038 p = TAILQ_FIRST(&backing_object->memq);
1049 p->object == backing_object,
1050 ("vm_object_qcollapse(): object mismatch")
1054 * Destroy any associated swap
1056 if (backing_object->type == OBJT_SWAP) {
1057 swap_pager_freespace(
1065 p->pindex < backing_offset_index ||
1066 new_pindex >= object->size
1069 * Page is out of the parent object's range, we
1070 * can simply destroy it.
1072 vm_page_protect(p, VM_PROT_NONE);
1078 pp = vm_page_lookup(object, new_pindex);
1081 vm_pager_has_page(object, new_pindex, NULL, NULL)
1084 * page already exists in parent OR swap exists
1085 * for this location in the parent. Destroy
1086 * the original page from the backing object.
1088 * Leave the parent's page alone
1090 vm_page_protect(p, VM_PROT_NONE);
1097 * Page does not exist in parent, rename the
1098 * page from the backing object to the main object.
1100 * If the page was mapped to a process, it can remain
1101 * mapped through the rename.
1103 if ((p->queue - p->pc) == PQ_CACHE)
1104 vm_page_deactivate(p);
1106 vm_page_rename(p, object, new_pindex);
1107 /* page automatically made dirty by rename */
1117 * this version of collapse allows the operation to occur earlier and
1118 * when paging_in_progress is true for an object... This is not a complete
1119 * operation, but should plug 99.9% of the rest of the leaks.
1122 vm_object_qcollapse(object)
1125 vm_object_t backing_object = object->backing_object;
1127 if (backing_object->ref_count != 1)
1130 backing_object->ref_count += 2;
1132 vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
1134 backing_object->ref_count -= 2;
1138 * vm_object_collapse:
1140 * Collapse an object with the object backing it.
1141 * Pages in the backing object are moved into the
1142 * parent, and the backing object is deallocated.
1145 vm_object_collapse(object)
1149 vm_object_t backing_object;
1152 * Verify that the conditions are right for collapse:
1154 * The object exists and the backing object exists.
1159 if ((backing_object = object->backing_object) == NULL)
1163 * we check the backing object first, because it is most likely
1166 if (backing_object->handle != NULL ||
1167 (backing_object->type != OBJT_DEFAULT &&
1168 backing_object->type != OBJT_SWAP) ||
1169 (backing_object->flags & OBJ_DEAD) ||
1170 object->handle != NULL ||
1171 (object->type != OBJT_DEFAULT &&
1172 object->type != OBJT_SWAP) ||
1173 (object->flags & OBJ_DEAD)) {
1178 object->paging_in_progress != 0 ||
1179 backing_object->paging_in_progress != 0
1181 vm_object_qcollapse(object);
1186 * We know that we can either collapse the backing object (if
1187 * the parent is the only reference to it) or (perhaps) have
1188 * the parent bypass the object if the parent happens to shadow
1189 * all the resident pages in the entire backing object.
1191 * This is ignoring pager-backed pages such as swap pages.
1192 * vm_object_backing_scan fails the shadowing test in this
1196 if (backing_object->ref_count == 1) {
1198 * If there is exactly one reference to the backing
1199 * object, we can collapse it into the parent.
1202 vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1205 * Move the pager from backing_object to object.
1208 if (backing_object->type == OBJT_SWAP) {
1209 vm_object_pip_add(backing_object, 1);
1212 * scrap the paging_offset junk and do a
1213 * discrete copy. This also removes major
1214 * assumptions about how the swap-pager
1215 * works from where it doesn't belong. The
1216 * new swapper is able to optimize the
1217 * destroy-source case.
1220 vm_object_pip_add(object, 1);
1224 OFF_TO_IDX(object->backing_object_offset), TRUE);
1225 vm_object_pip_wakeup(object);
1227 vm_object_pip_wakeup(backing_object);
1230 * Object now shadows whatever backing_object did.
1231 * Note that the reference to
1232 * backing_object->backing_object moves from within
1233 * backing_object to within object.
1237 &object->backing_object->shadow_head,
1241 object->backing_object->shadow_count--;
1242 object->backing_object->generation++;
1243 if (backing_object->backing_object) {
1245 &backing_object->backing_object->shadow_head,
1249 backing_object->backing_object->shadow_count--;
1250 backing_object->backing_object->generation++;
1252 object->backing_object = backing_object->backing_object;
1253 if (object->backing_object) {
1255 &object->backing_object->shadow_head,
1259 object->backing_object->shadow_count++;
1260 object->backing_object->generation++;
1263 object->backing_object_offset +=
1264 backing_object->backing_object_offset;
1267 * Discard backing_object.
1269 * Since the backing object has no pages, no pager left,
1270 * and no object references within it, all that is
1271 * necessary is to dispose of it.
1281 zfree(obj_zone, backing_object);
1285 vm_object_t new_backing_object;
1288 * If we do not entirely shadow the backing object,
1289 * there is nothing we can do so we give up.
1292 if (vm_object_backing_scan(object, OBSC_TEST_ALL_SHADOWED) == 0) {
1297 * Make the parent shadow the next object in the
1298 * chain. Deallocating backing_object will not remove
1299 * it, since its reference count is at least 2.
1303 &backing_object->shadow_head,
1307 backing_object->shadow_count--;
1308 backing_object->generation++;
1310 new_backing_object = backing_object->backing_object;
1311 if ((object->backing_object = new_backing_object) != NULL) {
1312 vm_object_reference(new_backing_object);
1314 &new_backing_object->shadow_head,
1318 new_backing_object->shadow_count++;
1319 new_backing_object->generation++;
1320 object->backing_object_offset +=
1321 backing_object->backing_object_offset;
1325 * Drop the reference count on backing_object. Since
1326 * its ref_count was at least 2, it will not vanish;
1327 * so we don't need to call vm_object_deallocate, but
1330 vm_object_deallocate(backing_object);
1335 * Try again with this object's new backing object.
1341 * vm_object_page_remove: [internal]
1343 * Removes all physical pages in the specified
1344 * object range from the object's list of pages.
1346 * The object must be locked.
1349 vm_object_page_remove(object, start, end, clean_only)
1353 boolean_t clean_only;
1359 if (object == NULL ||
1360 object->resident_page_count == 0)
1363 all = ((end == 0) && (start == 0));
1365 vm_object_pip_add(object, 1);
1368 if (all || size > object->resident_page_count / 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;
1466 vm_pindex_t next_pindex;
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;
1494 next_pindex = prev_pindex + prev_size;
1496 if ((prev_object->ref_count > 1) &&
1497 (prev_object->size != next_pindex)) {
1502 * Remove any pages that may still be in the object from a previous
1505 if (next_pindex < prev_object->size) {
1506 vm_object_page_remove(prev_object,
1508 next_pindex + next_size, FALSE);
1509 if (prev_object->type == OBJT_SWAP)
1510 swap_pager_freespace(prev_object,
1511 next_pindex, next_size);
1515 * Extend the object if necessary.
1517 if (next_pindex + next_size > prev_object->size)
1518 prev_object->size = next_pindex + next_size;
1523 #include "opt_ddb.h"
1525 #include <sys/kernel.h>
1527 #include <machine/cons.h>
1529 #include <ddb/ddb.h>
1531 static int _vm_object_in_map __P((vm_map_t map, vm_object_t object,
1532 vm_map_entry_t entry));
1533 static int vm_object_in_map __P((vm_object_t object));
1536 _vm_object_in_map(map, object, entry)
1539 vm_map_entry_t entry;
1542 vm_map_entry_t tmpe;
1550 tmpe = map->header.next;
1551 entcount = map->nentries;
1552 while (entcount-- && (tmpe != &map->header)) {
1553 if( _vm_object_in_map(map, object, tmpe)) {
1558 } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
1559 tmpm = entry->object.sub_map;
1560 tmpe = tmpm->header.next;
1561 entcount = tmpm->nentries;
1562 while (entcount-- && tmpe != &tmpm->header) {
1563 if( _vm_object_in_map(tmpm, object, tmpe)) {
1568 } else if ((obj = entry->object.vm_object) != NULL) {
1569 for(; obj; obj=obj->backing_object)
1570 if( obj == object) {
1578 vm_object_in_map( object)
1582 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
1583 if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1585 if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0))
1588 if( _vm_object_in_map( kernel_map, object, 0))
1590 if( _vm_object_in_map( kmem_map, object, 0))
1592 if( _vm_object_in_map( pager_map, object, 0))
1594 if( _vm_object_in_map( buffer_map, object, 0))
1596 if( _vm_object_in_map( io_map, object, 0))
1598 if( _vm_object_in_map( phys_map, object, 0))
1600 if( _vm_object_in_map( mb_map, object, 0))
1602 if( _vm_object_in_map( u_map, object, 0))
1607 DB_SHOW_COMMAND(vmochk, vm_object_check)
1612 * make sure that internal objs are in a map somewhere
1613 * and none have zero ref counts.
1615 for (object = TAILQ_FIRST(&vm_object_list);
1617 object = TAILQ_NEXT(object, object_list)) {
1618 if (object->handle == NULL &&
1619 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
1620 if (object->ref_count == 0) {
1621 db_printf("vmochk: internal obj has zero ref count: %ld\n",
1622 (long)object->size);
1624 if (!vm_object_in_map(object)) {
1626 "vmochk: internal obj is not in a map: "
1627 "ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
1628 object->ref_count, (u_long)object->size,
1629 (u_long)object->size,
1630 (void *)object->backing_object);
1637 * vm_object_print: [ debug ]
1639 DB_SHOW_COMMAND(object, vm_object_print_static)
1641 /* XXX convert args. */
1642 vm_object_t object = (vm_object_t)addr;
1643 boolean_t full = have_addr;
1647 /* XXX count is an (unused) arg. Avoid shadowing it. */
1648 #define count was_count
1656 "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n",
1657 object, (int)object->type, (u_long)object->size,
1658 object->resident_page_count, object->ref_count, object->flags);
1660 * XXX no %qd in kernel. Truncate object->backing_object_offset.
1662 db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%lx\n",
1663 object->shadow_count,
1664 object->backing_object ? object->backing_object->ref_count : 0,
1665 object->backing_object, (long)object->backing_object_offset);
1672 for (p = TAILQ_FIRST(&object->memq); p != NULL; p = TAILQ_NEXT(p, listq)) {
1674 db_iprintf("memory:=");
1675 else if (count == 6) {
1683 db_printf("(off=0x%lx,page=0x%lx)",
1684 (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
1694 /* XXX need this non-static entry for calling from vm_map_print. */
1696 vm_object_print(addr, have_addr, count, modif)
1697 /* db_expr_t */ long addr;
1698 boolean_t have_addr;
1699 /* db_expr_t */ long count;
1702 vm_object_print_static(addr, have_addr, count, modif);
1705 DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
1710 for (object = TAILQ_FIRST(&vm_object_list);
1712 object = TAILQ_NEXT(object, object_list)) {
1713 vm_pindex_t idx, fidx;
1715 vm_offset_t pa = -1, padiff;
1719 db_printf("new object: %p\n", (void *)object);
1729 osize = object->size;
1732 for(idx=0;idx<osize;idx++) {
1733 m = vm_page_lookup(object, idx);
1736 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
1737 (long)fidx, rcount, (long)pa);
1752 (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
1757 padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
1758 padiff >>= PAGE_SHIFT;
1759 padiff &= PQ_L2_MASK;
1761 pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
1765 db_printf(" index(%ld)run(%d)pa(0x%lx)",
1766 (long)fidx, rcount, (long)pa);
1767 db_printf("pd(%ld)\n", (long)padiff);
1777 pa = VM_PAGE_TO_PHYS(m);
1781 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
1782 (long)fidx, rcount, (long)pa);