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.135 1998/11/05 14:28:26 dg 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 #define VM_OBJECTS_INIT 256
138 static struct vm_object vm_objects_init[VM_OBJECTS_INIT];
139 static int objidnumber;
142 _vm_object_allocate(type, size, object)
145 register vm_object_t object;
148 TAILQ_INIT(&object->memq);
149 TAILQ_INIT(&object->shadow_head);
153 object->ref_count = 1;
155 object->id = ++objidnumber;
156 if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
157 vm_object_set_flag(object, OBJ_ONEMAPPING);
158 object->behavior = OBJ_NORMAL;
159 object->paging_in_progress = 0;
160 object->resident_page_count = 0;
161 object->cache_count = 0;
162 object->wire_count = 0;
163 object->shadow_count = 0;
164 object->pg_color = next_index;
165 if ( size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
166 incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
169 next_index = (next_index + incr) & PQ_L2_MASK;
170 object->handle = NULL;
171 object->paging_offset = (vm_ooffset_t) 0;
172 object->backing_object = NULL;
173 object->backing_object_offset = (vm_ooffset_t) 0;
174 object->page_hint = NULL;
176 object->last_read = 0;
177 object->generation++;
179 TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
186 * Initialize the VM objects module.
191 TAILQ_INIT(&vm_object_list);
192 simple_lock_init(&vm_object_list_lock);
195 kernel_object = &kernel_object_store;
196 _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
199 kmem_object = &kmem_object_store;
200 _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
203 obj_zone = &obj_zone_store;
204 zbootinit(obj_zone, "VM OBJECT", sizeof (struct vm_object),
205 vm_objects_init, VM_OBJECTS_INIT);
210 zinitna(obj_zone, NULL, NULL, 0, 0, 0, 1);
214 * vm_object_allocate:
216 * Returns a new object with the given size.
220 vm_object_allocate(type, size)
224 register vm_object_t result;
225 result = (vm_object_t) zalloc(obj_zone);
227 _vm_object_allocate(type, size, result);
234 * vm_object_reference:
236 * Gets another reference to the given object.
239 vm_object_reference(object)
240 register vm_object_t object;
245 #if defined(DIAGNOSTIC)
246 if (object->flags & OBJ_DEAD)
247 panic("vm_object_reference: attempting to reference dead obj");
251 if (object->type == OBJT_VNODE) {
252 while (vget((struct vnode *) object->handle, LK_RETRY|LK_NOOBJ, curproc)) {
253 #if !defined(MAX_PERF)
254 printf("vm_object_reference: delay in getting object\n");
261 vm_object_vndeallocate(object)
264 struct vnode *vp = (struct vnode *) object->handle;
265 #if defined(DIAGNOSTIC)
266 if (object->type != OBJT_VNODE)
267 panic("vm_object_vndeallocate: not a vnode object");
269 panic("vm_object_vndeallocate: missing vp");
270 if (object->ref_count == 0) {
271 vprint("vm_object_vndeallocate", vp);
272 panic("vm_object_vndeallocate: bad object reference count");
277 if (object->ref_count == 0) {
278 vp->v_flag &= ~VTEXT;
279 vm_object_clear_flag(object, OBJ_OPT);
285 * vm_object_deallocate:
287 * Release a reference to the specified object,
288 * gained either through a vm_object_allocate
289 * or a vm_object_reference call. When all references
290 * are gone, storage associated with this object
291 * may be relinquished.
293 * No object may be locked.
296 vm_object_deallocate(object)
301 while (object != NULL) {
303 if (object->type == OBJT_VNODE) {
304 vm_object_vndeallocate(object);
308 if (object->ref_count == 0) {
309 panic("vm_object_deallocate: object deallocated too many times: %d", object->type);
310 } else if (object->ref_count > 2) {
316 * Here on ref_count of one or two, which are special cases for
319 if ((object->ref_count == 2) && (object->shadow_count == 0)) {
320 vm_object_set_flag(object, OBJ_ONEMAPPING);
323 } else if ((object->ref_count == 2) && (object->shadow_count == 1)) {
325 if ((object->handle == NULL) &&
326 (object->type == OBJT_DEFAULT ||
327 object->type == OBJT_SWAP)) {
330 robject = TAILQ_FIRST(&object->shadow_head);
331 #if defined(DIAGNOSTIC)
333 panic("vm_object_deallocate: ref_count: %d,"
335 object->ref_count, object->shadow_count);
337 if ((robject->handle == NULL) &&
338 (robject->type == OBJT_DEFAULT ||
339 robject->type == OBJT_SWAP)) {
341 robject->ref_count++;
344 if (robject->paging_in_progress ||
345 object->paging_in_progress) {
346 vm_object_pip_sleep(robject, "objde1");
347 if (robject->paging_in_progress &&
348 robject->type == OBJT_SWAP) {
353 vm_object_pip_sleep(object, "objde2");
354 if (object->paging_in_progress &&
355 object->type == OBJT_SWAP) {
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.
405 vm_object_terminate(object)
406 register vm_object_t object;
408 register vm_page_t p;
412 * Make sure no one uses us.
414 vm_object_set_flag(object, OBJ_DEAD);
417 * wait for the pageout daemon to be done with the object
419 vm_object_pip_wait(object, "objtrm");
421 #if defined(DIAGNOSTIC)
422 if (object->paging_in_progress != 0)
423 panic("vm_object_terminate: pageout in progress");
427 * Clean and free the pages, as appropriate. All references to the
428 * object are gone, so we don't need to lock it.
430 if (object->type == OBJT_VNODE) {
434 * Freeze optimized copies.
436 vm_freeze_copyopts(object, 0, object->size);
439 * Clean pages and flush buffers.
441 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
443 vp = (struct vnode *) object->handle;
444 vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
447 if (object->ref_count != 0)
448 panic("vm_object_terminate: object with references, ref_count=%d", object->ref_count);
451 * Now free any remaining pages. For internal objects, this also
452 * removes them from paging queues. Don't free wired pages, just
453 * remove them from the object.
456 while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
457 #if !defined(MAX_PERF)
458 if (p->busy || (p->flags & PG_BUSY))
459 printf("vm_object_terminate: freeing busy page\n");
461 if (p->wire_count == 0) {
473 * Let the pager know object is dead.
475 vm_pager_deallocate(object);
478 * Remove the object from the global object list.
480 simple_lock(&vm_object_list_lock);
481 TAILQ_REMOVE(&vm_object_list, object, object_list);
482 simple_unlock(&vm_object_list_lock);
487 * Free the space for the object.
489 zfree(obj_zone, object);
493 * vm_object_page_clean
495 * Clean all dirty pages in the specified range of object.
496 * Leaves page on whatever queue it is currently on.
498 * Odd semantics: if start == end, we clean everything.
500 * The object must be locked.
504 vm_object_page_clean(object, start, end, flags)
510 register vm_page_t p, np, tp;
511 register vm_offset_t tstart, tend;
521 vm_page_t maf[vm_pageout_page_count];
522 vm_page_t mab[vm_pageout_page_count];
523 vm_page_t ma[vm_pageout_page_count];
526 if (object->type != OBJT_VNODE ||
527 (object->flags & OBJ_MIGHTBEDIRTY) == 0)
530 pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : 0;
531 pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
535 vm_object_set_flag(object, OBJ_CLEANING);
544 for(p = TAILQ_FIRST(&object->memq); p; p = TAILQ_NEXT(p, listq)) {
545 vm_page_flag_set(p, PG_CLEANCHK);
546 vm_page_protect(p, VM_PROT_READ);
549 if ((tstart == 0) && (tend == object->size)) {
550 vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
554 curgeneration = object->generation;
556 for(p = TAILQ_FIRST(&object->memq); p; p = np) {
557 np = TAILQ_NEXT(p, listq);
560 if (((p->flags & PG_CLEANCHK) == 0) ||
561 (pi < tstart) || (pi >= tend) ||
563 ((p->queue - p->pc) == PQ_CACHE)) {
564 vm_page_flag_clear(p, PG_CLEANCHK);
568 vm_page_test_dirty(p);
569 if ((p->dirty & p->valid) == 0) {
570 vm_page_flag_clear(p, PG_CLEANCHK);
575 while ((p->flags & PG_BUSY) || p->busy) {
576 vm_page_flag_set(p, PG_WANTED | PG_REFERENCED);
577 tsleep(p, PVM, "vpcwai", 0);
578 if (object->generation != curgeneration) {
585 for(i=1;i<vm_pageout_page_count;i++) {
586 if (tp = vm_page_lookup(object, pi + i)) {
587 if ((tp->flags & PG_BUSY) ||
588 (tp->flags & PG_CLEANCHK) == 0 ||
591 if((tp->queue - tp->pc) == PQ_CACHE) {
592 vm_page_flag_clear(tp, PG_CLEANCHK);
595 vm_page_test_dirty(tp);
596 if ((tp->dirty & tp->valid) == 0) {
597 vm_page_flag_clear(tp, PG_CLEANCHK);
608 chkb = vm_pageout_page_count - maxf;
610 for(i = 1; i < chkb;i++) {
611 if (tp = vm_page_lookup(object, pi - i)) {
612 if ((tp->flags & PG_BUSY) ||
613 (tp->flags & PG_CLEANCHK) == 0 ||
616 if((tp->queue - tp->pc) == PQ_CACHE) {
617 vm_page_flag_clear(tp, PG_CLEANCHK);
620 vm_page_test_dirty(tp);
621 if ((tp->dirty & tp->valid) == 0) {
622 vm_page_flag_clear(tp, PG_CLEANCHK);
633 for(i=0;i<maxb;i++) {
634 int index = (maxb - i) - 1;
636 vm_page_flag_clear(ma[index], PG_CLEANCHK);
638 vm_page_flag_clear(p, PG_CLEANCHK);
640 for(i=0;i<maxf;i++) {
641 int index = (maxb + i) + 1;
643 vm_page_flag_clear(ma[index], PG_CLEANCHK);
645 runlen = maxb + maxf + 1;
648 vm_pageout_flush(ma, runlen, pagerflags);
649 for (i = 0; i<runlen; i++) {
650 if (ma[i]->valid & ma[i]->dirty) {
651 vm_page_protect(ma[i], VM_PROT_READ);
652 vm_page_flag_set(ma[i], PG_CLEANCHK);
655 if (object->generation != curgeneration)
659 VOP_FSYNC(vp, NULL, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
661 vm_object_clear_flag(object, OBJ_CLEANING);
666 /* XXX I cannot tell if this should be an exported symbol */
668 * vm_object_deactivate_pages
670 * Deactivate all pages in the specified object. (Keep its pages
671 * in memory even though it is no longer referenced.)
673 * The object must be locked.
676 vm_object_deactivate_pages(object)
677 register vm_object_t object;
679 register vm_page_t p, next;
681 for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
682 next = TAILQ_NEXT(p, listq);
683 vm_page_deactivate(p);
689 * vm_object_pmap_copy:
691 * Makes all physical pages in the specified
692 * object range copy-on-write. No writeable
693 * references to these pages should remain.
695 * The object must *not* be locked.
698 vm_object_pmap_copy(object, start, end)
699 register vm_object_t object;
700 register vm_pindex_t start;
701 register vm_pindex_t end;
703 register vm_page_t p;
705 if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
708 for (p = TAILQ_FIRST(&object->memq);
710 p = TAILQ_NEXT(p, listq)) {
711 vm_page_protect(p, VM_PROT_READ);
714 vm_object_clear_flag(object, OBJ_WRITEABLE);
718 * Same as vm_object_pmap_copy_1, except range checking really
719 * works, and is meant for small sections of an object.
722 vm_object_pmap_copy_1(object, start, end)
723 register vm_object_t object;
724 register vm_pindex_t start;
725 register vm_pindex_t end;
728 register vm_page_t p;
730 if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
733 for (idx = start; idx < end; idx++) {
734 p = vm_page_lookup(object, idx);
737 vm_page_protect(p, VM_PROT_READ);
742 * vm_object_pmap_remove:
744 * Removes all physical pages in the specified
745 * object range from all physical maps.
747 * The object must *not* be locked.
750 vm_object_pmap_remove(object, start, end)
751 register vm_object_t object;
752 register vm_pindex_t start;
753 register vm_pindex_t end;
755 register vm_page_t p;
758 for (p = TAILQ_FIRST(&object->memq);
760 p = TAILQ_NEXT(p, listq)) {
761 if (p->pindex >= start && p->pindex < end)
762 vm_page_protect(p, VM_PROT_NONE);
764 if ((start == 0) && (object->size == end))
765 vm_object_clear_flag(object, OBJ_WRITEABLE);
771 * Implements the madvise function at the object/page level.
774 vm_object_madvise(object, pindex, count, advise)
780 vm_pindex_t end, tpindex;
787 end = pindex + count;
789 for (; pindex < end; pindex += 1) {
795 m = vm_page_lookup(tobject, tpindex);
797 if (tobject->type != OBJT_DEFAULT) {
801 tobject = tobject->backing_object;
802 if ((tobject == NULL) || (tobject->ref_count != 1)) {
805 tpindex += OFF_TO_IDX(tobject->backing_object_offset);
810 * If the page is busy or not in a normal active state,
811 * we skip it. Things can break if we mess with pages
812 * in any of the below states.
814 if (m->hold_count || m->wire_count ||
815 m->valid != VM_PAGE_BITS_ALL) {
819 if (vm_page_sleep(m, "madvpo", &m->busy))
822 if (advise == MADV_WILLNEED) {
824 } else if (advise == MADV_DONTNEED) {
825 vm_page_deactivate(m);
826 } else if (advise == MADV_FREE) {
827 pmap_clear_modify(VM_PAGE_TO_PHYS(m));
830 * Force a demand zero if attempt to read from swap.
831 * We currently don't handle vnode files correctly,
832 * and will reread stale contents unnecessarily.
834 if (object->type == OBJT_SWAP)
835 swap_pager_dmzspace(tobject, m->pindex, 1);
843 * Create a new object which is backed by the
844 * specified existing object range. The source
845 * object reference is deallocated.
847 * The new object and offset into that object
848 * are returned in the source parameters.
852 vm_object_shadow(object, offset, length)
853 vm_object_t *object; /* IN/OUT */
854 vm_ooffset_t *offset; /* IN/OUT */
857 register vm_object_t source;
858 register vm_object_t result;
863 * Allocate a new object with the given length
866 if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL)
867 panic("vm_object_shadow: no object for shadowing");
870 * The new object shadows the source object, adding a reference to it.
871 * Our caller changes his reference to point to the new object,
872 * removing a reference to the source object. Net result: no change
873 * of reference count.
875 result->backing_object = source;
877 TAILQ_INSERT_TAIL(&source->shadow_head, result, shadow_list);
878 vm_object_clear_flag(source, OBJ_ONEMAPPING);
879 source->shadow_count++;
880 source->generation++;
884 * Store the offset into the source object, and fix up the offset into
888 result->backing_object_offset = *offset;
891 * Return the new things
900 * this version of collapse allows the operation to occur earlier and
901 * when paging_in_progress is true for an object... This is not a complete
902 * operation, but should plug 99.9% of the rest of the leaks.
905 vm_object_qcollapse(object)
906 register vm_object_t object;
908 register vm_object_t backing_object;
909 register vm_pindex_t backing_offset_index, paging_offset_index;
910 vm_pindex_t backing_object_paging_offset_index;
911 vm_pindex_t new_pindex;
912 register vm_page_t p, pp;
913 register vm_size_t size;
915 backing_object = object->backing_object;
916 if (backing_object->ref_count != 1)
919 backing_object->ref_count += 2;
921 backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
922 backing_object_paging_offset_index = OFF_TO_IDX(backing_object->paging_offset);
923 paging_offset_index = OFF_TO_IDX(object->paging_offset);
925 p = TAILQ_FIRST(&backing_object->memq);
929 next = TAILQ_NEXT(p, listq);
930 if ((p->flags & (PG_BUSY | PG_FICTITIOUS)) ||
931 !p->valid || p->hold_count || p->wire_count || p->busy) {
937 new_pindex = p->pindex - backing_offset_index;
938 if (p->pindex < backing_offset_index ||
939 new_pindex >= size) {
940 if (backing_object->type == OBJT_SWAP)
941 swap_pager_freespace(backing_object,
942 backing_object_paging_offset_index+p->pindex,
944 vm_page_protect(p, VM_PROT_NONE);
947 pp = vm_page_lookup(object, new_pindex);
949 (object->type == OBJT_SWAP && vm_pager_has_page(object,
950 paging_offset_index + new_pindex, NULL, NULL))) {
951 if (backing_object->type == OBJT_SWAP)
952 swap_pager_freespace(backing_object,
953 backing_object_paging_offset_index + p->pindex, 1);
954 vm_page_protect(p, VM_PROT_NONE);
957 if (backing_object->type == OBJT_SWAP)
958 swap_pager_freespace(backing_object,
959 backing_object_paging_offset_index + p->pindex, 1);
961 if ((p->queue - p->pc) == PQ_CACHE)
962 vm_page_deactivate(p);
964 vm_page_protect(p, VM_PROT_NONE);
966 vm_page_rename(p, object, new_pindex);
967 p->dirty = VM_PAGE_BITS_ALL;
972 backing_object->ref_count -= 2;
976 * vm_object_collapse:
978 * Collapse an object with the object backing it.
979 * Pages in the backing object are moved into the
980 * parent, and the backing object is deallocated.
983 vm_object_collapse(object)
987 vm_object_t backing_object;
988 vm_ooffset_t backing_offset;
990 vm_pindex_t new_pindex, backing_offset_index;
995 * Verify that the conditions are right for collapse:
997 * The object exists and no pages in it are currently being paged
1004 * Make sure there is a backing object.
1006 if ((backing_object = object->backing_object) == NULL)
1010 * we check the backing object first, because it is most likely
1013 if (backing_object->handle != NULL ||
1014 (backing_object->type != OBJT_DEFAULT &&
1015 backing_object->type != OBJT_SWAP) ||
1016 (backing_object->flags & OBJ_DEAD) ||
1017 object->handle != NULL ||
1018 (object->type != OBJT_DEFAULT &&
1019 object->type != OBJT_SWAP) ||
1020 (object->flags & OBJ_DEAD)) {
1024 if (object->paging_in_progress != 0 ||
1025 backing_object->paging_in_progress != 0) {
1026 vm_object_qcollapse(object);
1031 * We know that we can either collapse the backing object (if
1032 * the parent is the only reference to it) or (perhaps) remove
1033 * the parent's reference to it.
1036 backing_offset = object->backing_object_offset;
1037 backing_offset_index = OFF_TO_IDX(backing_offset);
1038 size = object->size;
1041 * If there is exactly one reference to the backing object, we
1042 * can collapse it into the parent.
1045 if (backing_object->ref_count == 1) {
1047 vm_object_set_flag(backing_object, OBJ_DEAD);
1049 * We can collapse the backing object.
1051 * Move all in-memory pages from backing_object to the
1052 * parent. Pages that have been paged out will be
1053 * overwritten by any of the parent's pages that
1057 while ((p = TAILQ_FIRST(&backing_object->memq)) != 0) {
1059 new_pindex = p->pindex - backing_offset_index;
1063 * If the parent has a page here, or if this
1064 * page falls outside the parent, dispose of
1067 * Otherwise, move it as planned.
1070 if (p->pindex < backing_offset_index ||
1071 new_pindex >= size) {
1072 vm_page_protect(p, VM_PROT_NONE);
1075 pp = vm_page_lookup(object, new_pindex);
1076 if (pp != NULL || (object->type == OBJT_SWAP && vm_pager_has_page(object,
1077 OFF_TO_IDX(object->paging_offset) + new_pindex, NULL, NULL))) {
1078 vm_page_protect(p, VM_PROT_NONE);
1081 if ((p->queue - p->pc) == PQ_CACHE)
1082 vm_page_deactivate(p);
1084 vm_page_protect(p, VM_PROT_NONE);
1085 vm_page_rename(p, object, new_pindex);
1086 p->dirty = VM_PAGE_BITS_ALL;
1092 * Move the pager from backing_object to object.
1095 if (backing_object->type == OBJT_SWAP) {
1096 vm_object_pip_add(backing_object, 1);
1097 if (object->type == OBJT_SWAP) {
1098 vm_object_pip_add(object, 1);
1100 * copy shadow object pages into ours
1101 * and destroy unneeded pages in
1106 OFF_TO_IDX(backing_object->paging_offset),
1108 OFF_TO_IDX(object->paging_offset),
1109 OFF_TO_IDX(object->backing_object_offset), TRUE);
1110 vm_object_pip_wakeup(object);
1112 vm_object_pip_add(object, 1);
1114 * move the shadow backing_object's pager data to
1115 * "object" and convert "object" type to OBJT_SWAP.
1117 object->type = OBJT_SWAP;
1118 object->un_pager.swp.swp_nblocks =
1119 backing_object->un_pager.swp.swp_nblocks;
1120 object->un_pager.swp.swp_allocsize =
1121 backing_object->un_pager.swp.swp_allocsize;
1122 object->un_pager.swp.swp_blocks =
1123 backing_object->un_pager.swp.swp_blocks;
1124 object->un_pager.swp.swp_poip = /* XXX */
1125 backing_object->un_pager.swp.swp_poip;
1126 object->paging_offset = backing_object->paging_offset + backing_offset;
1127 TAILQ_INSERT_TAIL(&swap_pager_un_object_list, object, pager_object_list);
1130 * Convert backing object from OBJT_SWAP to
1131 * OBJT_DEFAULT. XXX - only the TAILQ_REMOVE is
1132 * actually necessary.
1134 backing_object->type = OBJT_DEFAULT;
1135 TAILQ_REMOVE(&swap_pager_un_object_list, backing_object, pager_object_list);
1137 * free unnecessary blocks
1139 swap_pager_freespace(object, 0,
1140 OFF_TO_IDX(object->paging_offset));
1141 vm_object_pip_wakeup(object);
1144 vm_object_pip_wakeup(backing_object);
1147 * Object now shadows whatever backing_object did.
1148 * Note that the reference to backing_object->backing_object
1149 * moves from within backing_object to within object.
1152 TAILQ_REMOVE(&object->backing_object->shadow_head, object,
1154 object->backing_object->shadow_count--;
1155 object->backing_object->generation++;
1156 if (backing_object->backing_object) {
1157 TAILQ_REMOVE(&backing_object->backing_object->shadow_head,
1158 backing_object, shadow_list);
1159 backing_object->backing_object->shadow_count--;
1160 backing_object->backing_object->generation++;
1162 object->backing_object = backing_object->backing_object;
1163 if (object->backing_object) {
1164 TAILQ_INSERT_TAIL(&object->backing_object->shadow_head,
1165 object, shadow_list);
1166 object->backing_object->shadow_count++;
1167 object->backing_object->generation++;
1170 object->backing_object_offset += backing_object->backing_object_offset;
1172 * Discard backing_object.
1174 * Since the backing object has no pages, no pager left,
1175 * and no object references within it, all that is
1176 * necessary is to dispose of it.
1179 TAILQ_REMOVE(&vm_object_list, backing_object,
1183 zfree(obj_zone, backing_object);
1187 vm_object_t new_backing_object;
1189 * If all of the pages in the backing object are
1190 * shadowed by the parent object, the parent object no
1191 * longer has to shadow the backing object; it can
1192 * shadow the next one in the chain.
1194 * The backing object must not be paged out - we'd have
1195 * to check all of the paged-out pages, as well.
1198 if (backing_object->type != OBJT_DEFAULT) {
1202 * Should have a check for a 'small' number of pages
1206 for (p = TAILQ_FIRST(&backing_object->memq); p;
1207 p = TAILQ_NEXT(p, listq)) {
1209 new_pindex = p->pindex - backing_offset_index;
1213 * If the parent has a page here, or if this
1214 * page falls outside the parent, keep going.
1216 * Otherwise, the backing_object must be left in
1220 if (p->pindex >= backing_offset_index &&
1221 new_pindex <= size) {
1223 pp = vm_page_lookup(object, new_pindex);
1225 if ((pp == NULL) || (pp->flags & PG_BUSY) || pp->busy) {
1231 if ((pp->valid == 0) &&
1232 !vm_pager_has_page(object, OFF_TO_IDX(object->paging_offset) + new_pindex, NULL, NULL)) {
1234 * Page still needed. Can't go any
1247 * Make the parent shadow the next object in the
1248 * chain. Deallocating backing_object will not remove
1249 * it, since its reference count is at least 2.
1252 TAILQ_REMOVE(&backing_object->shadow_head,
1253 object, shadow_list);
1254 backing_object->shadow_count--;
1255 backing_object->generation++;
1257 new_backing_object = backing_object->backing_object;
1258 if (object->backing_object = new_backing_object) {
1259 vm_object_reference(new_backing_object);
1260 TAILQ_INSERT_TAIL(&new_backing_object->shadow_head,
1261 object, shadow_list);
1262 new_backing_object->shadow_count++;
1263 new_backing_object->generation++;
1264 object->backing_object_offset +=
1265 backing_object->backing_object_offset;
1269 * Drop the reference count on backing_object. Since
1270 * its ref_count was at least 2, it will not vanish;
1271 * so we don't need to call vm_object_deallocate, but
1274 vm_object_deallocate(backing_object);
1279 * Try again with this object's new backing object.
1285 * vm_object_page_remove: [internal]
1287 * Removes all physical pages in the specified
1288 * object range from the object's list of pages.
1290 * The object must be locked.
1293 vm_object_page_remove(object, start, end, clean_only)
1294 register vm_object_t object;
1295 register vm_pindex_t start;
1296 register vm_pindex_t end;
1297 boolean_t clean_only;
1299 register vm_page_t p, next;
1306 all = ((end == 0) && (start == 0));
1308 vm_object_pip_add(object, 1);
1311 if (all || size > 4 || size >= object->size / 4) {
1312 for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
1313 next = TAILQ_NEXT(p, listq);
1314 if (all || ((start <= p->pindex) && (p->pindex < end))) {
1315 if (p->wire_count != 0) {
1316 vm_page_protect(p, VM_PROT_NONE);
1323 * The busy flags are only cleared at
1324 * interrupt -- minimize the spl transitions
1327 if (vm_page_sleep(p, "vmopar", &p->busy))
1330 if (clean_only && p->valid) {
1331 vm_page_test_dirty(p);
1332 if (p->valid & p->dirty)
1337 vm_page_protect(p, VM_PROT_NONE);
1343 if ((p = vm_page_lookup(object, start)) != 0) {
1345 if (p->wire_count != 0) {
1346 vm_page_protect(p, VM_PROT_NONE);
1355 * The busy flags are only cleared at
1356 * interrupt -- minimize the spl transitions
1358 if (vm_page_sleep(p, "vmopar", &p->busy))
1361 if (clean_only && p->valid) {
1362 vm_page_test_dirty(p);
1363 if (p->valid & p->dirty) {
1371 vm_page_protect(p, VM_PROT_NONE);
1378 vm_object_pip_wakeup(object);
1382 * Routine: vm_object_coalesce
1383 * Function: Coalesces two objects backing up adjoining
1384 * regions of memory into a single object.
1386 * returns TRUE if objects were combined.
1388 * NOTE: Only works at the moment if the second object is NULL -
1389 * if it's not, which object do we lock first?
1392 * prev_object First object to coalesce
1393 * prev_offset Offset into prev_object
1394 * next_object Second object into coalesce
1395 * next_offset Offset into next_object
1397 * prev_size Size of reference to prev_object
1398 * next_size Size of reference to next_object
1401 * The object must *not* be locked.
1404 vm_object_coalesce(prev_object, prev_pindex, prev_size, next_size)
1405 register vm_object_t prev_object;
1406 vm_pindex_t prev_pindex;
1407 vm_size_t prev_size, next_size;
1411 if (prev_object == NULL) {
1415 if (prev_object->type != OBJT_DEFAULT) {
1420 * Try to collapse the object first
1422 vm_object_collapse(prev_object);
1425 * Can't coalesce if: . more than one reference . paged out . shadows
1426 * another object . has a copy elsewhere (any of which mean that the
1427 * pages not mapped to prev_entry may be in use anyway)
1430 if (prev_object->backing_object != NULL) {
1434 prev_size >>= PAGE_SHIFT;
1435 next_size >>= PAGE_SHIFT;
1437 if ((prev_object->ref_count > 1) &&
1438 (prev_object->size != prev_pindex + prev_size)) {
1443 * Remove any pages that may still be in the object from a previous
1447 vm_object_page_remove(prev_object,
1448 prev_pindex + prev_size,
1449 prev_pindex + prev_size + next_size, FALSE);
1452 * Extend the object if necessary.
1454 newsize = prev_pindex + prev_size + next_size;
1455 if (newsize > prev_object->size)
1456 prev_object->size = newsize;
1461 #include "opt_ddb.h"
1463 #include <sys/kernel.h>
1465 #include <machine/cons.h>
1467 #include <ddb/ddb.h>
1469 static int _vm_object_in_map __P((vm_map_t map, vm_object_t object,
1470 vm_map_entry_t entry));
1471 static int vm_object_in_map __P((vm_object_t object));
1474 _vm_object_in_map(map, object, entry)
1477 vm_map_entry_t entry;
1480 vm_map_entry_t tmpe;
1488 tmpe = map->header.next;
1489 entcount = map->nentries;
1490 while (entcount-- && (tmpe != &map->header)) {
1491 if( _vm_object_in_map(map, object, tmpe)) {
1496 } else if (entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) {
1497 tmpm = entry->object.share_map;
1498 tmpe = tmpm->header.next;
1499 entcount = tmpm->nentries;
1500 while (entcount-- && tmpe != &tmpm->header) {
1501 if( _vm_object_in_map(tmpm, object, tmpe)) {
1506 } else if (obj = entry->object.vm_object) {
1507 for(; obj; obj=obj->backing_object)
1508 if( obj == object) {
1516 vm_object_in_map( object)
1520 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
1521 if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1523 if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0))
1526 if( _vm_object_in_map( kernel_map, object, 0))
1528 if( _vm_object_in_map( kmem_map, object, 0))
1530 if( _vm_object_in_map( pager_map, object, 0))
1532 if( _vm_object_in_map( buffer_map, object, 0))
1534 if( _vm_object_in_map( io_map, object, 0))
1536 if( _vm_object_in_map( phys_map, object, 0))
1538 if( _vm_object_in_map( mb_map, object, 0))
1540 if( _vm_object_in_map( u_map, object, 0))
1545 DB_SHOW_COMMAND(vmochk, vm_object_check)
1550 * make sure that internal objs are in a map somewhere
1551 * and none have zero ref counts.
1553 for (object = TAILQ_FIRST(&vm_object_list);
1555 object = TAILQ_NEXT(object, object_list)) {
1556 if (object->handle == NULL &&
1557 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
1558 if (object->ref_count == 0) {
1559 db_printf("vmochk: internal obj has zero ref count: %d\n",
1562 if (!vm_object_in_map(object)) {
1564 "vmochk: internal obj is not in a map: "
1565 "ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
1566 object->ref_count, (u_long)object->size,
1567 (u_long)object->size,
1568 (void *)object->backing_object);
1575 * vm_object_print: [ debug ]
1577 DB_SHOW_COMMAND(object, vm_object_print_static)
1579 /* XXX convert args. */
1580 vm_object_t object = (vm_object_t)addr;
1581 boolean_t full = have_addr;
1583 register vm_page_t p;
1585 /* XXX count is an (unused) arg. Avoid shadowing it. */
1586 #define count was_count
1594 "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n",
1595 object, (int)object->type, (u_long)object->size,
1596 object->resident_page_count, object->ref_count, object->flags);
1598 * XXX no %qd in kernel. Truncate object->paging_offset and
1599 * object->backing_object_offset.
1601 db_iprintf(" sref=%d, offset=0x%lx, backing_object(%d)=(%p)+0x%lx\n",
1602 object->shadow_count, (long)object->paging_offset,
1603 object->backing_object ? object->backing_object->ref_count : 0,
1604 object->backing_object, (long)object->backing_object_offset);
1611 for (p = TAILQ_FIRST(&object->memq); p != NULL; p = TAILQ_NEXT(p, listq)) {
1613 db_iprintf("memory:=");
1614 else if (count == 6) {
1622 db_printf("(off=0x%lx,page=0x%lx)",
1623 (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
1633 /* XXX need this non-static entry for calling from vm_map_print. */
1635 vm_object_print(addr, have_addr, count, modif)
1636 /* db_expr_t */ long addr;
1637 boolean_t have_addr;
1638 /* db_expr_t */ long count;
1641 vm_object_print_static(addr, have_addr, count, modif);
1644 DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
1649 for (object = TAILQ_FIRST(&vm_object_list);
1651 object = TAILQ_NEXT(object, object_list)) {
1652 vm_pindex_t idx, fidx;
1654 vm_offset_t pa = -1, padiff;
1658 db_printf("new object: %p\n", (void *)object);
1668 osize = object->size;
1671 for(idx=0;idx<osize;idx++) {
1672 m = vm_page_lookup(object, idx);
1675 db_printf(" index(%d)run(%d)pa(0x%x)\n",
1691 (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
1696 padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
1697 padiff >>= PAGE_SHIFT;
1698 padiff &= PQ_L2_MASK;
1700 pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
1704 db_printf(" index(%d)run(%d)pa(0x%x)", fidx, rcount, pa);
1705 db_printf("pd(%d)\n", padiff);
1715 pa = VM_PAGE_TO_PHYS(m);
1719 db_printf(" index(%d)run(%d)pa(0x%x)\n", fidx, rcount, pa);