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 * $FreeBSD: src/sys/vm/vm_object.c,v 1.171.2.8 2003/05/26 19:17:56 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/thread.h>
75 #include <sys/vnode.h>
76 #include <sys/vmmeter.h>
78 #include <sys/mount.h>
79 #include <sys/kernel.h>
80 #include <sys/sysctl.h>
81 #include <sys/refcount.h>
84 #include <vm/vm_param.h>
86 #include <vm/vm_map.h>
87 #include <vm/vm_object.h>
88 #include <vm/vm_page.h>
89 #include <vm/vm_pageout.h>
90 #include <vm/vm_pager.h>
91 #include <vm/swap_pager.h>
92 #include <vm/vm_kern.h>
93 #include <vm/vm_extern.h>
94 #include <vm/vm_zone.h>
96 #define EASY_SCAN_FACTOR 8
98 static void vm_object_qcollapse(vm_object_t object,
99 vm_object_t backing_object);
100 static void vm_object_page_collect_flush(vm_object_t object, vm_page_t p,
102 static void vm_object_lock_init(vm_object_t);
106 * Virtual memory objects maintain the actual data
107 * associated with allocated virtual memory. A given
108 * page of memory exists within exactly one object.
110 * An object is only deallocated when all "references"
111 * are given up. Only one "reference" to a given
112 * region of an object should be writeable.
114 * Associated with each object is a list of all resident
115 * memory pages belonging to that object; this list is
116 * maintained by the "vm_page" module, and locked by the object's
119 * Each object also records a "pager" routine which is
120 * used to retrieve (and store) pages to the proper backing
121 * storage. In addition, objects may be backed by other
122 * objects from which they were virtual-copied.
124 * The only items within the object structure which are
125 * modified after time of creation are:
126 * reference count locked by object's lock
127 * pager routine locked by object's lock
131 struct object_q vm_object_list; /* locked by vmobj_token */
132 struct vm_object kernel_object;
134 static long vm_object_count; /* locked by vmobj_token */
135 extern int vm_pageout_page_count;
137 static long object_collapses;
138 static long object_bypasses;
139 static int next_index;
140 static vm_zone_t obj_zone;
141 static struct vm_zone obj_zone_store;
142 #define VM_OBJECTS_INIT 256
143 static struct vm_object vm_objects_init[VM_OBJECTS_INIT];
146 * Misc low level routines
149 vm_object_lock_init(vm_object_t obj)
151 #if defined(DEBUG_LOCKS)
154 obj->debug_hold_bitmap = 0;
155 obj->debug_hold_ovfl = 0;
156 for (i = 0; i < VMOBJ_DEBUG_ARRAY_SIZE; i++) {
157 obj->debug_hold_thrs[i] = NULL;
158 obj->debug_hold_file[i] = NULL;
159 obj->debug_hold_line[i] = 0;
165 vm_object_lock_swap(void)
171 vm_object_lock(vm_object_t obj)
173 lwkt_gettoken(&obj->token);
177 * Returns TRUE on sucesss
180 vm_object_lock_try(vm_object_t obj)
182 return(lwkt_trytoken(&obj->token));
186 vm_object_lock_shared(vm_object_t obj)
188 lwkt_gettoken_shared(&obj->token);
192 vm_object_unlock(vm_object_t obj)
194 lwkt_reltoken(&obj->token);
198 vm_object_assert_held(vm_object_t obj)
200 ASSERT_LWKT_TOKEN_HELD(&obj->token);
205 vm_object_hold(vm_object_t obj)
207 debugvm_object_hold(vm_object_t obj, char *file, int line)
210 KKASSERT(obj != NULL);
213 * Object must be held (object allocation is stable due to callers
214 * context, typically already holding the token on a parent object)
215 * prior to potentially blocking on the lock, otherwise the object
216 * can get ripped away from us.
218 refcount_acquire(&obj->hold_count);
221 #if defined(DEBUG_LOCKS)
226 mask = ~obj->debug_hold_bitmap;
228 if (mask == 0xFFFFFFFFU) {
229 if (obj->debug_hold_ovfl == 0)
230 obj->debug_hold_ovfl = 1;
234 if (atomic_cmpset_int(&obj->debug_hold_bitmap, ~mask,
236 obj->debug_hold_bitmap |= (1 << i);
237 obj->debug_hold_thrs[i] = curthread;
238 obj->debug_hold_file[i] = file;
239 obj->debug_hold_line[i] = line;
248 vm_object_hold_try(vm_object_t obj)
250 debugvm_object_hold_try(vm_object_t obj, char *file, int line)
253 KKASSERT(obj != NULL);
256 * Object must be held (object allocation is stable due to callers
257 * context, typically already holding the token on a parent object)
258 * prior to potentially blocking on the lock, otherwise the object
259 * can get ripped away from us.
261 refcount_acquire(&obj->hold_count);
262 if (vm_object_lock_try(obj) == 0) {
263 if (refcount_release(&obj->hold_count)) {
264 if (obj->ref_count == 0 && (obj->flags & OBJ_DEAD))
265 zfree(obj_zone, obj);
270 #if defined(DEBUG_LOCKS)
275 mask = ~obj->debug_hold_bitmap;
277 if (mask == 0xFFFFFFFFU) {
278 if (obj->debug_hold_ovfl == 0)
279 obj->debug_hold_ovfl = 1;
283 if (atomic_cmpset_int(&obj->debug_hold_bitmap, ~mask,
285 obj->debug_hold_bitmap |= (1 << i);
286 obj->debug_hold_thrs[i] = curthread;
287 obj->debug_hold_file[i] = file;
288 obj->debug_hold_line[i] = line;
298 vm_object_hold_shared(vm_object_t obj)
300 debugvm_object_hold_shared(vm_object_t obj, char *file, int line)
303 KKASSERT(obj != NULL);
306 * Object must be held (object allocation is stable due to callers
307 * context, typically already holding the token on a parent object)
308 * prior to potentially blocking on the lock, otherwise the object
309 * can get ripped away from us.
311 refcount_acquire(&obj->hold_count);
312 vm_object_lock_shared(obj);
314 #if defined(DEBUG_LOCKS)
319 mask = ~obj->debug_hold_bitmap;
321 if (mask == 0xFFFFFFFFU) {
322 if (obj->debug_hold_ovfl == 0)
323 obj->debug_hold_ovfl = 1;
327 if (atomic_cmpset_int(&obj->debug_hold_bitmap, ~mask,
329 obj->debug_hold_bitmap |= (1 << i);
330 obj->debug_hold_thrs[i] = curthread;
331 obj->debug_hold_file[i] = file;
332 obj->debug_hold_line[i] = line;
340 * Obtain either a shared or exclusive lock on VM object
341 * based on whether this is a terminal vnode object or not.
345 vm_object_hold_maybe_shared(vm_object_t obj)
347 debugvm_object_hold_maybe_shared(vm_object_t obj, char *file, int line)
350 if (vm_shared_fault &&
351 obj->type == OBJT_VNODE &&
352 obj->backing_object == NULL) {
353 vm_object_hold_shared(obj);
362 * Drop the token and hold_count on the object.
365 vm_object_drop(vm_object_t obj)
370 #if defined(DEBUG_LOCKS)
374 for (i = 0; i < VMOBJ_DEBUG_ARRAY_SIZE; i++) {
375 if ((obj->debug_hold_bitmap & (1 << i)) &&
376 (obj->debug_hold_thrs[i] == curthread)) {
377 obj->debug_hold_bitmap &= ~(1 << i);
378 obj->debug_hold_thrs[i] = NULL;
379 obj->debug_hold_file[i] = NULL;
380 obj->debug_hold_line[i] = 0;
386 if (found == 0 && obj->debug_hold_ovfl == 0)
387 panic("vm_object: attempt to drop hold on non-self-held obj");
391 * No new holders should be possible once we drop hold_count 1->0 as
392 * there is no longer any way to reference the object.
394 KKASSERT(obj->hold_count > 0);
395 if (refcount_release(&obj->hold_count)) {
396 if (obj->ref_count == 0 && (obj->flags & OBJ_DEAD)) {
397 vm_object_unlock(obj);
398 zfree(obj_zone, obj);
400 vm_object_unlock(obj);
403 vm_object_unlock(obj);
408 * Initialize a freshly allocated object, returning a held object.
410 * Used only by vm_object_allocate() and zinitna().
415 _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
419 RB_INIT(&object->rb_memq);
420 LIST_INIT(&object->shadow_head);
421 lwkt_token_init(&object->token, "vmobj");
425 object->ref_count = 1;
426 object->hold_count = 0;
428 if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
429 vm_object_set_flag(object, OBJ_ONEMAPPING);
430 object->paging_in_progress = 0;
431 object->resident_page_count = 0;
432 object->agg_pv_list_count = 0;
433 object->shadow_count = 0;
434 /* cpu localization twist */
435 object->pg_color = (int)(intptr_t)curthread;
436 if ( size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
437 incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
440 next_index = (next_index + incr) & PQ_L2_MASK;
441 object->handle = NULL;
442 object->backing_object = NULL;
443 object->backing_object_offset = (vm_ooffset_t)0;
445 object->generation++;
446 object->swblock_count = 0;
447 RB_INIT(&object->swblock_root);
448 vm_object_lock_init(object);
449 pmap_object_init(object);
451 vm_object_hold(object);
452 lwkt_gettoken(&vmobj_token);
453 TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
455 lwkt_reltoken(&vmobj_token);
459 * Initialize the VM objects module.
461 * Called from the low level boot code only.
466 TAILQ_INIT(&vm_object_list);
468 _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(KvaEnd),
470 vm_object_drop(&kernel_object);
472 obj_zone = &obj_zone_store;
473 zbootinit(obj_zone, "VM OBJECT", sizeof (struct vm_object),
474 vm_objects_init, VM_OBJECTS_INIT);
478 vm_object_init2(void)
480 zinitna(obj_zone, NULL, NULL, 0, 0, ZONE_PANICFAIL, 1);
484 * Allocate and return a new object of the specified type and size.
489 vm_object_allocate(objtype_t type, vm_pindex_t size)
493 result = (vm_object_t) zalloc(obj_zone);
495 _vm_object_allocate(type, size, result);
496 vm_object_drop(result);
502 * This version returns a held object, allowing further atomic initialization
506 vm_object_allocate_hold(objtype_t type, vm_pindex_t size)
510 result = (vm_object_t) zalloc(obj_zone);
512 _vm_object_allocate(type, size, result);
518 * Add an additional reference to a vm_object. The object must already be
519 * held. The original non-lock version is no longer supported. The object
520 * must NOT be chain locked by anyone at the time the reference is added.
522 * Referencing a chain-locked object can blow up the fairly sensitive
523 * ref_count and shadow_count tests in the deallocator. Most callers
524 * will call vm_object_chain_wait() prior to calling
525 * vm_object_reference_locked() to avoid the case.
527 * The object must be held, but may be held shared if desired (hence why
528 * we use an atomic op).
531 vm_object_reference_locked(vm_object_t object)
533 KKASSERT(object != NULL);
534 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
535 KKASSERT((object->flags & OBJ_CHAINLOCK) == 0);
536 atomic_add_int(&object->ref_count, 1);
537 if (object->type == OBJT_VNODE) {
538 vref(object->handle);
539 /* XXX what if the vnode is being destroyed? */
544 * Object OBJ_CHAINLOCK lock handling.
546 * The caller can chain-lock backing objects recursively and then
547 * use vm_object_chain_release_all() to undo the whole chain.
549 * Chain locks are used to prevent collapses and are only applicable
550 * to OBJT_DEFAULT and OBJT_SWAP objects. Chain locking operations
551 * on other object types are ignored. This is also important because
552 * it allows e.g. the vnode underlying a memory mapping to take concurrent
555 * The object must usually be held on entry, though intermediate
556 * objects need not be held on release.
559 vm_object_chain_wait(vm_object_t object)
561 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
562 while (object->flags & OBJ_CHAINLOCK) {
563 vm_object_set_flag(object, OBJ_CHAINWANT);
564 tsleep(object, 0, "objchain", 0);
569 vm_object_chain_acquire(vm_object_t object)
571 if (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP) {
572 vm_object_chain_wait(object);
573 vm_object_set_flag(object, OBJ_CHAINLOCK);
578 vm_object_chain_release(vm_object_t object)
580 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
581 if (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP) {
582 KKASSERT(object->flags & OBJ_CHAINLOCK);
583 if (object->flags & OBJ_CHAINWANT) {
584 vm_object_clear_flag(object,
585 OBJ_CHAINLOCK | OBJ_CHAINWANT);
588 vm_object_clear_flag(object, OBJ_CHAINLOCK);
594 * This releases the entire chain of objects from first_object to and
595 * including stopobj, flowing through object->backing_object.
597 * We release stopobj first as an optimization as this object is most
598 * likely to be shared across multiple processes.
601 vm_object_chain_release_all(vm_object_t first_object, vm_object_t stopobj)
603 vm_object_t backing_object;
606 vm_object_chain_release(stopobj);
607 object = first_object;
609 while (object != stopobj) {
611 if (object != first_object)
612 vm_object_hold(object);
613 backing_object = object->backing_object;
614 vm_object_chain_release(object);
615 if (object != first_object)
616 vm_object_drop(object);
617 object = backing_object;
622 * Dereference an object and its underlying vnode.
624 * The object must be held exclusively and will remain held on return.
625 * (We don't need an atomic op due to the exclusivity).
628 vm_object_vndeallocate(vm_object_t object)
630 struct vnode *vp = (struct vnode *) object->handle;
632 KASSERT(object->type == OBJT_VNODE,
633 ("vm_object_vndeallocate: not a vnode object"));
634 KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
635 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
637 if (object->ref_count == 0) {
638 vprint("vm_object_vndeallocate", vp);
639 panic("vm_object_vndeallocate: bad object reference count");
643 if (object->ref_count == 0)
644 vclrflags(vp, VTEXT);
649 * Release a reference to the specified object, gained either through a
650 * vm_object_allocate or a vm_object_reference call. When all references
651 * are gone, storage associated with this object may be relinquished.
653 * The caller does not have to hold the object locked but must have control
654 * over the reference in question in order to guarantee that the object
655 * does not get ripped out from under us.
657 * XXX Currently all deallocations require an exclusive lock.
660 vm_object_deallocate(vm_object_t object)
663 vm_object_hold(object);
664 vm_object_deallocate_locked(object);
665 vm_object_drop(object);
670 vm_object_deallocate_locked(vm_object_t object)
672 struct vm_object_dealloc_list *dlist = NULL;
673 struct vm_object_dealloc_list *dtmp;
678 * We may chain deallocate object, but additional objects may
679 * collect on the dlist which also have to be deallocated. We
680 * must avoid a recursion, vm_object chains can get deep.
683 while (object != NULL) {
684 ASSERT_LWKT_TOKEN_HELD_EXCL(&object->token);
687 * Don't rip a ref_count out from under an object undergoing
688 * collapse, it will confuse the collapse code.
690 vm_object_chain_wait(object);
692 if (object->type == OBJT_VNODE) {
693 vm_object_vndeallocate(object);
697 if (object->ref_count == 0) {
698 panic("vm_object_deallocate: object deallocated "
699 "too many times: %d", object->type);
701 if (object->ref_count > 2) {
707 * Here on ref_count of one or two, which are special cases for
710 * Nominal ref_count > 1 case if the second ref is not from
713 * (ONEMAPPING only applies to DEFAULT AND SWAP objects)
715 if (object->ref_count == 2 && object->shadow_count == 0) {
716 if (object->type == OBJT_DEFAULT ||
717 object->type == OBJT_SWAP) {
718 vm_object_set_flag(object, OBJ_ONEMAPPING);
725 * If the second ref is from a shadow we chain along it
726 * upwards if object's handle is exhausted.
728 * We have to decrement object->ref_count before potentially
729 * collapsing the first shadow object or the collapse code
730 * will not be able to handle the degenerate case to remove
731 * object. However, if we do it too early the object can
732 * get ripped out from under us.
734 if (object->ref_count == 2 && object->shadow_count == 1 &&
735 object->handle == NULL && (object->type == OBJT_DEFAULT ||
736 object->type == OBJT_SWAP)) {
737 temp = LIST_FIRST(&object->shadow_head);
738 KKASSERT(temp != NULL);
739 vm_object_hold(temp);
742 * Wait for any paging to complete so the collapse
743 * doesn't (or isn't likely to) qcollapse. pip
744 * waiting must occur before we acquire the
748 temp->paging_in_progress ||
749 object->paging_in_progress
751 vm_object_pip_wait(temp, "objde1");
752 vm_object_pip_wait(object, "objde2");
756 * If the parent is locked we have to give up, as
757 * otherwise we would be acquiring locks in the
758 * wrong order and potentially deadlock.
760 if (temp->flags & OBJ_CHAINLOCK) {
761 vm_object_drop(temp);
764 vm_object_chain_acquire(temp);
767 * Recheck/retry after the hold and the paging
768 * wait, both of which can block us.
770 if (object->ref_count != 2 ||
771 object->shadow_count != 1 ||
773 LIST_FIRST(&object->shadow_head) != temp ||
774 (object->type != OBJT_DEFAULT &&
775 object->type != OBJT_SWAP)) {
776 vm_object_chain_release(temp);
777 vm_object_drop(temp);
782 * We can safely drop object's ref_count now.
784 KKASSERT(object->ref_count == 2);
788 * If our single parent is not collapseable just
789 * decrement ref_count (2->1) and stop.
791 if (temp->handle || (temp->type != OBJT_DEFAULT &&
792 temp->type != OBJT_SWAP)) {
793 vm_object_chain_release(temp);
794 vm_object_drop(temp);
799 * At this point we have already dropped object's
800 * ref_count so it is possible for a race to
801 * deallocate obj out from under us. Any collapse
802 * will re-check the situation. We must not block
803 * until we are able to collapse.
805 * Bump temp's ref_count to avoid an unwanted
806 * degenerate recursion (can't call
807 * vm_object_reference_locked() because it asserts
808 * that CHAINLOCK is not set).
811 KKASSERT(temp->ref_count > 1);
814 * Collapse temp, then deallocate the extra ref
817 vm_object_collapse(temp, &dlist);
818 vm_object_chain_release(temp);
820 vm_object_lock_swap();
821 vm_object_drop(object);
829 * Drop the ref and handle termination on the 1->0 transition.
830 * We may have blocked above so we have to recheck.
833 KKASSERT(object->ref_count != 0);
834 if (object->ref_count >= 2) {
838 KKASSERT(object->ref_count == 1);
841 * 1->0 transition. Chain through the backing_object.
842 * Maintain the ref until we've located the backing object,
845 while ((temp = object->backing_object) != NULL) {
846 vm_object_hold(temp);
847 if (temp == object->backing_object)
849 vm_object_drop(temp);
853 * 1->0 transition verified, retry if ref_count is no longer
854 * 1. Otherwise disconnect the backing_object (temp) and
857 if (object->ref_count != 1) {
858 vm_object_drop(temp);
863 * It shouldn't be possible for the object to be chain locked
864 * if we're removing the last ref on it.
866 KKASSERT((object->flags & OBJ_CHAINLOCK) == 0);
869 LIST_REMOVE(object, shadow_list);
870 temp->shadow_count--;
872 object->backing_object = NULL;
876 if ((object->flags & OBJ_DEAD) == 0)
877 vm_object_terminate(object);
878 if (must_drop && temp)
879 vm_object_lock_swap();
881 vm_object_drop(object);
885 if (must_drop && object)
886 vm_object_drop(object);
889 * Additional tail recursion on dlist. Avoid a recursion. Objects
890 * on the dlist have a hold count but are not locked.
892 if ((dtmp = dlist) != NULL) {
894 object = dtmp->object;
897 vm_object_lock(object); /* already held, add lock */
898 must_drop = 1; /* and we're responsible for it */
904 * Destroy the specified object, freeing up related resources.
906 * The object must have zero references.
908 * The object must held. The caller is responsible for dropping the object
909 * after terminate returns. Terminate does NOT drop the object.
911 static int vm_object_terminate_callback(vm_page_t p, void *data);
914 vm_object_terminate(vm_object_t object)
917 * Make sure no one uses us. Once we set OBJ_DEAD we should be
918 * able to safely block.
920 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
921 KKASSERT((object->flags & OBJ_DEAD) == 0);
922 vm_object_set_flag(object, OBJ_DEAD);
925 * Wait for the pageout daemon to be done with the object
927 vm_object_pip_wait(object, "objtrm1");
929 KASSERT(!object->paging_in_progress,
930 ("vm_object_terminate: pageout in progress"));
933 * Clean and free the pages, as appropriate. All references to the
934 * object are gone, so we don't need to lock it.
936 if (object->type == OBJT_VNODE) {
940 * Clean pages and flush buffers.
942 * NOTE! TMPFS buffer flushes do not typically flush the
943 * actual page to swap as this would be highly
944 * inefficient, and normal filesystems usually wrap
945 * page flushes with buffer cache buffers.
947 * To deal with this we have to call vinvalbuf() both
948 * before and after the vm_object_page_clean().
950 vp = (struct vnode *) object->handle;
951 vinvalbuf(vp, V_SAVE, 0, 0);
952 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
953 vinvalbuf(vp, V_SAVE, 0, 0);
957 * Wait for any I/O to complete, after which there had better not
958 * be any references left on the object.
960 vm_object_pip_wait(object, "objtrm2");
962 if (object->ref_count != 0) {
963 panic("vm_object_terminate: object with references, "
964 "ref_count=%d", object->ref_count);
968 * Cleanup any shared pmaps associated with this object.
970 pmap_object_free(object);
973 * Now free any remaining pages. For internal objects, this also
974 * removes them from paging queues. Don't free wired pages, just
975 * remove them from the object.
977 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
978 vm_object_terminate_callback, NULL);
981 * Let the pager know object is dead.
983 vm_pager_deallocate(object);
986 * Wait for the object hold count to hit 1, clean out pages as
987 * we go. vmobj_token interlocks any race conditions that might
988 * pick the object up from the vm_object_list after we have cleared
992 if (RB_ROOT(&object->rb_memq) == NULL)
994 kprintf("vm_object_terminate: Warning, object %p "
995 "still has %d pages\n",
996 object, object->resident_page_count);
997 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
998 vm_object_terminate_callback, NULL);
1002 * There had better not be any pages left
1004 KKASSERT(object->resident_page_count == 0);
1007 * Remove the object from the global object list.
1009 lwkt_gettoken(&vmobj_token);
1010 TAILQ_REMOVE(&vm_object_list, object, object_list);
1012 lwkt_reltoken(&vmobj_token);
1013 vm_object_dead_wakeup(object);
1015 if (object->ref_count != 0) {
1016 panic("vm_object_terminate2: object with references, "
1017 "ref_count=%d", object->ref_count);
1021 * NOTE: The object hold_count is at least 1, so we cannot zfree()
1022 * the object here. See vm_object_drop().
1027 * The caller must hold the object.
1030 vm_object_terminate_callback(vm_page_t p, void *data __unused)
1035 vm_page_busy_wait(p, TRUE, "vmpgtrm");
1036 if (object != p->object) {
1037 kprintf("vm_object_terminate: Warning: Encountered "
1038 "busied page %p on queue %d\n", p, p->queue);
1040 } else if (p->wire_count == 0) {
1042 * NOTE: p->dirty and PG_NEED_COMMIT are ignored.
1045 mycpu->gd_cnt.v_pfree++;
1047 if (p->queue != PQ_NONE)
1048 kprintf("vm_object_terminate: Warning: Encountered "
1049 "wired page %p on queue %d\n", p, p->queue);
1058 * The object is dead but still has an object<->pager association. Sleep
1059 * and return. The caller typically retests the association in a loop.
1061 * The caller must hold the object.
1064 vm_object_dead_sleep(vm_object_t object, const char *wmesg)
1066 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
1067 if (object->handle) {
1068 vm_object_set_flag(object, OBJ_DEADWNT);
1069 tsleep(object, 0, wmesg, 0);
1070 /* object may be invalid after this point */
1075 * Wakeup anyone waiting for the object<->pager disassociation on
1078 * The caller must hold the object.
1081 vm_object_dead_wakeup(vm_object_t object)
1083 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
1084 if (object->flags & OBJ_DEADWNT) {
1085 vm_object_clear_flag(object, OBJ_DEADWNT);
1091 * Clean all dirty pages in the specified range of object. Leaves page
1092 * on whatever queue it is currently on. If NOSYNC is set then do not
1093 * write out pages with PG_NOSYNC set (originally comes from MAP_NOSYNC),
1094 * leaving the object dirty.
1096 * When stuffing pages asynchronously, allow clustering. XXX we need a
1097 * synchronous clustering mode implementation.
1099 * Odd semantics: if start == end, we clean everything.
1101 * The object must be locked? XXX
1103 static int vm_object_page_clean_pass1(struct vm_page *p, void *data);
1104 static int vm_object_page_clean_pass2(struct vm_page *p, void *data);
1107 vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
1110 struct rb_vm_page_scan_info info;
1116 vm_object_hold(object);
1117 if (object->type != OBJT_VNODE ||
1118 (object->flags & OBJ_MIGHTBEDIRTY) == 0) {
1119 vm_object_drop(object);
1123 pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ?
1124 VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
1125 pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
1127 vp = object->handle;
1130 * Interlock other major object operations. This allows us to
1131 * temporarily clear OBJ_WRITEABLE and OBJ_MIGHTBEDIRTY.
1133 vm_object_set_flag(object, OBJ_CLEANING);
1136 * Handle 'entire object' case
1138 info.start_pindex = start;
1140 info.end_pindex = object->size - 1;
1142 info.end_pindex = end - 1;
1144 wholescan = (start == 0 && info.end_pindex == object->size - 1);
1146 info.pagerflags = pagerflags;
1147 info.object = object;
1150 * If cleaning the entire object do a pass to mark the pages read-only.
1151 * If everything worked out ok, clear OBJ_WRITEABLE and
1156 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
1157 vm_object_page_clean_pass1, &info);
1158 if (info.error == 0) {
1159 vm_object_clear_flag(object,
1160 OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
1161 if (object->type == OBJT_VNODE &&
1162 (vp = (struct vnode *)object->handle) != NULL) {
1163 if (vp->v_flag & VOBJDIRTY)
1164 vclrflags(vp, VOBJDIRTY);
1170 * Do a pass to clean all the dirty pages we find.
1174 generation = object->generation;
1175 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
1176 vm_object_page_clean_pass2, &info);
1177 } while (info.error || generation != object->generation);
1179 vm_object_clear_flag(object, OBJ_CLEANING);
1180 vm_object_drop(object);
1184 * The caller must hold the object.
1188 vm_object_page_clean_pass1(struct vm_page *p, void *data)
1190 struct rb_vm_page_scan_info *info = data;
1192 vm_page_flag_set(p, PG_CLEANCHK);
1193 if ((info->limit & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
1195 } else if (vm_page_busy_try(p, FALSE) == 0) {
1196 vm_page_protect(p, VM_PROT_READ); /* must not block */
1206 * The caller must hold the object
1210 vm_object_page_clean_pass2(struct vm_page *p, void *data)
1212 struct rb_vm_page_scan_info *info = data;
1216 * Do not mess with pages that were inserted after we started
1217 * the cleaning pass.
1219 if ((p->flags & PG_CLEANCHK) == 0)
1222 generation = info->object->generation;
1223 vm_page_busy_wait(p, TRUE, "vpcwai");
1224 if (p->object != info->object ||
1225 info->object->generation != generation) {
1232 * Before wasting time traversing the pmaps, check for trivial
1233 * cases where the page cannot be dirty.
1235 if (p->valid == 0 || (p->queue - p->pc) == PQ_CACHE) {
1236 KKASSERT((p->dirty & p->valid) == 0 &&
1237 (p->flags & PG_NEED_COMMIT) == 0);
1243 * Check whether the page is dirty or not. The page has been set
1244 * to be read-only so the check will not race a user dirtying the
1247 vm_page_test_dirty(p);
1248 if ((p->dirty & p->valid) == 0 && (p->flags & PG_NEED_COMMIT) == 0) {
1249 vm_page_flag_clear(p, PG_CLEANCHK);
1255 * If we have been asked to skip nosync pages and this is a
1256 * nosync page, skip it. Note that the object flags were
1257 * not cleared in this case (because pass1 will have returned an
1258 * error), so we do not have to set them.
1260 if ((info->limit & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
1261 vm_page_flag_clear(p, PG_CLEANCHK);
1267 * Flush as many pages as we can. PG_CLEANCHK will be cleared on
1268 * the pages that get successfully flushed. Set info->error if
1269 * we raced an object modification.
1271 vm_object_page_collect_flush(info->object, p, info->pagerflags);
1279 * Collect the specified page and nearby pages and flush them out.
1280 * The number of pages flushed is returned. The passed page is busied
1281 * by the caller and we are responsible for its disposition.
1283 * The caller must hold the object.
1286 vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int pagerflags)
1295 vm_page_t maf[vm_pageout_page_count];
1296 vm_page_t mab[vm_pageout_page_count];
1297 vm_page_t ma[vm_pageout_page_count];
1299 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
1304 for(i = 1; i < vm_pageout_page_count; i++) {
1307 tp = vm_page_lookup_busy_try(object, pi + i, TRUE, &error);
1312 if ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
1313 (tp->flags & PG_CLEANCHK) == 0) {
1317 if ((tp->queue - tp->pc) == PQ_CACHE) {
1318 vm_page_flag_clear(tp, PG_CLEANCHK);
1322 vm_page_test_dirty(tp);
1323 if ((tp->dirty & tp->valid) == 0 &&
1324 (tp->flags & PG_NEED_COMMIT) == 0) {
1325 vm_page_flag_clear(tp, PG_CLEANCHK);
1334 chkb = vm_pageout_page_count - maxf;
1336 * NOTE: chkb can be 0
1338 for(i = 1; chkb && i < chkb; i++) {
1341 tp = vm_page_lookup_busy_try(object, pi - i, TRUE, &error);
1346 if ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
1347 (tp->flags & PG_CLEANCHK) == 0) {
1351 if ((tp->queue - tp->pc) == PQ_CACHE) {
1352 vm_page_flag_clear(tp, PG_CLEANCHK);
1356 vm_page_test_dirty(tp);
1357 if ((tp->dirty & tp->valid) == 0 &&
1358 (tp->flags & PG_NEED_COMMIT) == 0) {
1359 vm_page_flag_clear(tp, PG_CLEANCHK);
1368 * All pages in the maf[] and mab[] array are busied.
1370 for (i = 0; i < maxb; i++) {
1371 int index = (maxb - i) - 1;
1373 vm_page_flag_clear(ma[index], PG_CLEANCHK);
1375 vm_page_flag_clear(p, PG_CLEANCHK);
1377 for(i = 0; i < maxf; i++) {
1378 int index = (maxb + i) + 1;
1380 vm_page_flag_clear(ma[index], PG_CLEANCHK);
1382 runlen = maxb + maxf + 1;
1384 for (i = 0; i < runlen; i++) /* XXX need this any more? */
1385 vm_page_hold(ma[i]);
1387 vm_pageout_flush(ma, runlen, pagerflags);
1389 for (i = 0; i < runlen; i++) /* XXX need this any more? */
1390 vm_page_unhold(ma[i]);
1394 * Same as vm_object_pmap_copy, except range checking really
1395 * works, and is meant for small sections of an object.
1397 * This code protects resident pages by making them read-only
1398 * and is typically called on a fork or split when a page
1399 * is converted to copy-on-write.
1401 * NOTE: If the page is already at VM_PROT_NONE, calling
1402 * vm_page_protect will have no effect.
1405 vm_object_pmap_copy_1(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1410 if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
1413 vm_object_hold(object);
1414 for (idx = start; idx < end; idx++) {
1415 p = vm_page_lookup(object, idx);
1418 vm_page_protect(p, VM_PROT_READ);
1420 vm_object_drop(object);
1424 * Removes all physical pages in the specified object range from all
1427 * The object must *not* be locked.
1430 static int vm_object_pmap_remove_callback(vm_page_t p, void *data);
1433 vm_object_pmap_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1435 struct rb_vm_page_scan_info info;
1439 info.start_pindex = start;
1440 info.end_pindex = end - 1;
1442 vm_object_hold(object);
1443 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
1444 vm_object_pmap_remove_callback, &info);
1445 if (start == 0 && end == object->size)
1446 vm_object_clear_flag(object, OBJ_WRITEABLE);
1447 vm_object_drop(object);
1451 * The caller must hold the object
1454 vm_object_pmap_remove_callback(vm_page_t p, void *data __unused)
1456 vm_page_protect(p, VM_PROT_NONE);
1461 * Implements the madvise function at the object/page level.
1463 * MADV_WILLNEED (any object)
1465 * Activate the specified pages if they are resident.
1467 * MADV_DONTNEED (any object)
1469 * Deactivate the specified pages if they are resident.
1471 * MADV_FREE (OBJT_DEFAULT/OBJT_SWAP objects, OBJ_ONEMAPPING only)
1473 * Deactivate and clean the specified pages if they are
1474 * resident. This permits the process to reuse the pages
1475 * without faulting or the kernel to reclaim the pages
1481 vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
1483 vm_pindex_t end, tpindex;
1484 vm_object_t tobject;
1492 end = pindex + count;
1494 vm_object_hold(object);
1498 * Locate and adjust resident pages
1500 for (; pindex < end; pindex += 1) {
1502 if (tobject != object)
1503 vm_object_drop(tobject);
1508 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
1509 * and those pages must be OBJ_ONEMAPPING.
1511 if (advise == MADV_FREE) {
1512 if ((tobject->type != OBJT_DEFAULT &&
1513 tobject->type != OBJT_SWAP) ||
1514 (tobject->flags & OBJ_ONEMAPPING) == 0) {
1519 m = vm_page_lookup_busy_try(tobject, tpindex, TRUE, &error);
1522 vm_page_sleep_busy(m, TRUE, "madvpo");
1527 * There may be swap even if there is no backing page
1529 if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1530 swap_pager_freespace(tobject, tpindex, 1);
1535 while ((xobj = tobject->backing_object) != NULL) {
1536 KKASSERT(xobj != object);
1537 vm_object_hold(xobj);
1538 if (xobj == tobject->backing_object)
1540 vm_object_drop(xobj);
1544 tpindex += OFF_TO_IDX(tobject->backing_object_offset);
1545 if (tobject != object) {
1546 vm_object_lock_swap();
1547 vm_object_drop(tobject);
1554 * If the page is not in a normal active state, we skip it.
1555 * If the page is not managed there are no page queues to
1556 * mess with. Things can break if we mess with pages in
1557 * any of the below states.
1559 if (m->wire_count ||
1560 (m->flags & (PG_UNMANAGED | PG_NEED_COMMIT)) ||
1561 m->valid != VM_PAGE_BITS_ALL
1568 * Theoretically once a page is known not to be busy, an
1569 * interrupt cannot come along and rip it out from under us.
1572 if (advise == MADV_WILLNEED) {
1573 vm_page_activate(m);
1574 } else if (advise == MADV_DONTNEED) {
1575 vm_page_dontneed(m);
1576 } else if (advise == MADV_FREE) {
1578 * Mark the page clean. This will allow the page
1579 * to be freed up by the system. However, such pages
1580 * are often reused quickly by malloc()/free()
1581 * so we do not do anything that would cause
1582 * a page fault if we can help it.
1584 * Specifically, we do not try to actually free
1585 * the page now nor do we try to put it in the
1586 * cache (which would cause a page fault on reuse).
1588 * But we do make the page is freeable as we
1589 * can without actually taking the step of unmapping
1592 pmap_clear_modify(m);
1595 vm_page_dontneed(m);
1596 if (tobject->type == OBJT_SWAP)
1597 swap_pager_freespace(tobject, tpindex, 1);
1601 if (tobject != object)
1602 vm_object_drop(tobject);
1603 vm_object_drop(object);
1607 * Create a new object which is backed by the specified existing object
1608 * range. Replace the pointer and offset that was pointing at the existing
1609 * object with the pointer/offset for the new object.
1611 * No other requirements.
1614 vm_object_shadow(vm_object_t *objectp, vm_ooffset_t *offset, vm_size_t length,
1623 * Don't create the new object if the old object isn't shared.
1624 * We have to chain wait before adding the reference to avoid
1625 * racing a collapse or deallocation.
1627 * Add the additional ref to source here to avoid racing a later
1628 * collapse or deallocation. Clear the ONEMAPPING flag whether
1629 * addref is TRUE or not in this case because the original object
1633 vm_object_hold(source);
1634 vm_object_chain_wait(source);
1635 if (source->ref_count == 1 &&
1636 source->handle == NULL &&
1637 (source->type == OBJT_DEFAULT ||
1638 source->type == OBJT_SWAP)) {
1639 vm_object_drop(source);
1641 vm_object_reference_locked(source);
1642 vm_object_clear_flag(source, OBJ_ONEMAPPING);
1646 vm_object_reference_locked(source);
1647 vm_object_clear_flag(source, OBJ_ONEMAPPING);
1651 * Allocate a new object with the given length. The new object
1652 * is returned referenced but we may have to add another one.
1653 * If we are adding a second reference we must clear OBJ_ONEMAPPING.
1654 * (typically because the caller is about to clone a vm_map_entry).
1656 * The source object currently has an extra reference to prevent
1657 * collapses into it while we mess with its shadow list, which
1658 * we will remove later in this routine.
1660 if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL)
1661 panic("vm_object_shadow: no object for shadowing");
1662 vm_object_hold(result);
1664 vm_object_reference_locked(result);
1665 vm_object_clear_flag(result, OBJ_ONEMAPPING);
1669 * The new object shadows the source object. Chain wait before
1670 * adjusting shadow_count or the shadow list to avoid races.
1672 * Try to optimize the result object's page color when shadowing
1673 * in order to maintain page coloring consistency in the combined
1676 KKASSERT(result->backing_object == NULL);
1677 result->backing_object = source;
1679 vm_object_chain_wait(source);
1680 LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1681 source->shadow_count++;
1682 source->generation++;
1683 /* cpu localization twist */
1684 result->pg_color = (int)(intptr_t)curthread;
1688 * Adjust the return storage. Drop the ref on source before
1691 result->backing_object_offset = *offset;
1692 vm_object_drop(result);
1695 vm_object_deallocate_locked(source);
1696 vm_object_drop(source);
1700 * Return the new things
1705 #define OBSC_TEST_ALL_SHADOWED 0x0001
1706 #define OBSC_COLLAPSE_NOWAIT 0x0002
1707 #define OBSC_COLLAPSE_WAIT 0x0004
1709 static int vm_object_backing_scan_callback(vm_page_t p, void *data);
1712 * The caller must hold the object.
1715 vm_object_backing_scan(vm_object_t object, vm_object_t backing_object, int op)
1717 struct rb_vm_page_scan_info info;
1719 vm_object_assert_held(object);
1720 vm_object_assert_held(backing_object);
1722 KKASSERT(backing_object == object->backing_object);
1723 info.backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
1726 * Initial conditions
1728 if (op & OBSC_TEST_ALL_SHADOWED) {
1730 * We do not want to have to test for the existence of
1731 * swap pages in the backing object. XXX but with the
1732 * new swapper this would be pretty easy to do.
1734 * XXX what about anonymous MAP_SHARED memory that hasn't
1735 * been ZFOD faulted yet? If we do not test for this, the
1736 * shadow test may succeed! XXX
1738 if (backing_object->type != OBJT_DEFAULT)
1741 if (op & OBSC_COLLAPSE_WAIT) {
1742 KKASSERT((backing_object->flags & OBJ_DEAD) == 0);
1743 vm_object_set_flag(backing_object, OBJ_DEAD);
1744 lwkt_gettoken(&vmobj_token);
1745 TAILQ_REMOVE(&vm_object_list, backing_object, object_list);
1747 lwkt_reltoken(&vmobj_token);
1748 vm_object_dead_wakeup(backing_object);
1752 * Our scan. We have to retry if a negative error code is returned,
1753 * otherwise 0 or 1 will be returned in info.error. 0 Indicates that
1754 * the scan had to be stopped because the parent does not completely
1757 info.object = object;
1758 info.backing_object = backing_object;
1762 vm_page_rb_tree_RB_SCAN(&backing_object->rb_memq, NULL,
1763 vm_object_backing_scan_callback,
1765 } while (info.error < 0);
1771 * The caller must hold the object.
1774 vm_object_backing_scan_callback(vm_page_t p, void *data)
1776 struct rb_vm_page_scan_info *info = data;
1777 vm_object_t backing_object;
1780 vm_pindex_t new_pindex;
1781 vm_pindex_t backing_offset_index;
1785 new_pindex = pindex - info->backing_offset_index;
1787 object = info->object;
1788 backing_object = info->backing_object;
1789 backing_offset_index = info->backing_offset_index;
1791 if (op & OBSC_TEST_ALL_SHADOWED) {
1795 * Ignore pages outside the parent object's range
1796 * and outside the parent object's mapping of the
1799 * note that we do not busy the backing object's
1802 if (pindex < backing_offset_index ||
1803 new_pindex >= object->size
1809 * See if the parent has the page or if the parent's
1810 * object pager has the page. If the parent has the
1811 * page but the page is not valid, the parent's
1812 * object pager must have the page.
1814 * If this fails, the parent does not completely shadow
1815 * the object and we might as well give up now.
1817 pp = vm_page_lookup(object, new_pindex);
1818 if ((pp == NULL || pp->valid == 0) &&
1819 !vm_pager_has_page(object, new_pindex)
1821 info->error = 0; /* problemo */
1822 return(-1); /* stop the scan */
1827 * Check for busy page. Note that we may have lost (p) when we
1828 * possibly blocked above.
1830 if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
1833 if (vm_page_busy_try(p, TRUE)) {
1834 if (op & OBSC_COLLAPSE_NOWAIT) {
1838 * If we slept, anything could have
1839 * happened. Ask that the scan be restarted.
1841 * Since the object is marked dead, the
1842 * backing offset should not have changed.
1844 vm_page_sleep_busy(p, TRUE, "vmocol");
1851 * If (p) is no longer valid restart the scan.
1853 if (p->object != backing_object || p->pindex != pindex) {
1854 kprintf("vm_object_backing_scan: Warning: page "
1855 "%p ripped out from under us\n", p);
1861 if (op & OBSC_COLLAPSE_NOWAIT) {
1862 if (p->valid == 0 ||
1864 (p->flags & PG_NEED_COMMIT)) {
1869 /* XXX what if p->valid == 0 , hold_count, etc? */
1873 p->object == backing_object,
1874 ("vm_object_qcollapse(): object mismatch")
1878 * Destroy any associated swap
1880 if (backing_object->type == OBJT_SWAP)
1881 swap_pager_freespace(backing_object, p->pindex, 1);
1884 p->pindex < backing_offset_index ||
1885 new_pindex >= object->size
1888 * Page is out of the parent object's range, we
1889 * can simply destroy it.
1891 vm_page_protect(p, VM_PROT_NONE);
1896 pp = vm_page_lookup(object, new_pindex);
1897 if (pp != NULL || vm_pager_has_page(object, new_pindex)) {
1899 * page already exists in parent OR swap exists
1900 * for this location in the parent. Destroy
1901 * the original page from the backing object.
1903 * Leave the parent's page alone
1905 vm_page_protect(p, VM_PROT_NONE);
1911 * Page does not exist in parent, rename the
1912 * page from the backing object to the main object.
1914 * If the page was mapped to a process, it can remain
1915 * mapped through the rename.
1917 if ((p->queue - p->pc) == PQ_CACHE)
1918 vm_page_deactivate(p);
1920 vm_page_rename(p, object, new_pindex);
1922 /* page automatically made dirty by rename */
1928 * This version of collapse allows the operation to occur earlier and
1929 * when paging_in_progress is true for an object... This is not a complete
1930 * operation, but should plug 99.9% of the rest of the leaks.
1932 * The caller must hold the object and backing_object and both must be
1935 * (only called from vm_object_collapse)
1938 vm_object_qcollapse(vm_object_t object, vm_object_t backing_object)
1940 if (backing_object->ref_count == 1) {
1941 backing_object->ref_count += 2;
1942 vm_object_backing_scan(object, backing_object,
1943 OBSC_COLLAPSE_NOWAIT);
1944 backing_object->ref_count -= 2;
1949 * Collapse an object with the object backing it. Pages in the backing
1950 * object are moved into the parent, and the backing object is deallocated.
1951 * Any conflict is resolved in favor of the parent's existing pages.
1953 * object must be held and chain-locked on call.
1955 * The caller must have an extra ref on object to prevent a race from
1956 * destroying it during the collapse.
1959 vm_object_collapse(vm_object_t object, struct vm_object_dealloc_list **dlistp)
1961 struct vm_object_dealloc_list *dlist = NULL;
1962 vm_object_t backing_object;
1965 * Only one thread is attempting a collapse at any given moment.
1966 * There are few restrictions for (object) that callers of this
1967 * function check so reentrancy is likely.
1969 KKASSERT(object != NULL);
1970 vm_object_assert_held(object);
1971 KKASSERT(object->flags & OBJ_CHAINLOCK);
1978 * We have to hold the backing object, check races.
1980 while ((backing_object = object->backing_object) != NULL) {
1981 vm_object_hold(backing_object);
1982 if (backing_object == object->backing_object)
1984 vm_object_drop(backing_object);
1988 * No backing object? Nothing to collapse then.
1990 if (backing_object == NULL)
1994 * You can't collapse with a non-default/non-swap object.
1996 if (backing_object->type != OBJT_DEFAULT &&
1997 backing_object->type != OBJT_SWAP) {
1998 vm_object_drop(backing_object);
1999 backing_object = NULL;
2004 * Chain-lock the backing object too because if we
2005 * successfully merge its pages into the top object we
2006 * will collapse backing_object->backing_object as the
2007 * new backing_object. Re-check that it is still our
2010 vm_object_chain_acquire(backing_object);
2011 if (backing_object != object->backing_object) {
2012 vm_object_chain_release(backing_object);
2013 vm_object_drop(backing_object);
2018 * we check the backing object first, because it is most likely
2021 if (backing_object->handle != NULL ||
2022 (backing_object->type != OBJT_DEFAULT &&
2023 backing_object->type != OBJT_SWAP) ||
2024 (backing_object->flags & OBJ_DEAD) ||
2025 object->handle != NULL ||
2026 (object->type != OBJT_DEFAULT &&
2027 object->type != OBJT_SWAP) ||
2028 (object->flags & OBJ_DEAD)) {
2033 * If paging is in progress we can't do a normal collapse.
2036 object->paging_in_progress != 0 ||
2037 backing_object->paging_in_progress != 0
2039 vm_object_qcollapse(object, backing_object);
2044 * We know that we can either collapse the backing object (if
2045 * the parent is the only reference to it) or (perhaps) have
2046 * the parent bypass the object if the parent happens to shadow
2047 * all the resident pages in the entire backing object.
2049 * This is ignoring pager-backed pages such as swap pages.
2050 * vm_object_backing_scan fails the shadowing test in this
2053 if (backing_object->ref_count == 1) {
2055 * If there is exactly one reference to the backing
2056 * object, we can collapse it into the parent.
2058 KKASSERT(object->backing_object == backing_object);
2059 vm_object_backing_scan(object, backing_object,
2060 OBSC_COLLAPSE_WAIT);
2063 * Move the pager from backing_object to object.
2065 if (backing_object->type == OBJT_SWAP) {
2066 vm_object_pip_add(backing_object, 1);
2069 * scrap the paging_offset junk and do a
2070 * discrete copy. This also removes major
2071 * assumptions about how the swap-pager
2072 * works from where it doesn't belong. The
2073 * new swapper is able to optimize the
2074 * destroy-source case.
2076 vm_object_pip_add(object, 1);
2077 swap_pager_copy(backing_object, object,
2078 OFF_TO_IDX(object->backing_object_offset),
2080 vm_object_pip_wakeup(object);
2081 vm_object_pip_wakeup(backing_object);
2085 * Object now shadows whatever backing_object did.
2086 * Remove object from backing_object's shadow_list.
2088 LIST_REMOVE(object, shadow_list);
2089 KKASSERT(object->backing_object == backing_object);
2090 backing_object->shadow_count--;
2091 backing_object->generation++;
2094 * backing_object->backing_object moves from within
2095 * backing_object to within object.
2097 while ((bbobj = backing_object->backing_object) != NULL) {
2098 vm_object_hold(bbobj);
2099 if (bbobj == backing_object->backing_object)
2101 vm_object_drop(bbobj);
2104 LIST_REMOVE(backing_object, shadow_list);
2105 bbobj->shadow_count--;
2106 bbobj->generation++;
2107 backing_object->backing_object = NULL;
2109 object->backing_object = bbobj;
2111 LIST_INSERT_HEAD(&bbobj->shadow_head,
2112 object, shadow_list);
2113 bbobj->shadow_count++;
2114 bbobj->generation++;
2117 object->backing_object_offset +=
2118 backing_object->backing_object_offset;
2120 vm_object_drop(bbobj);
2123 * Discard the old backing_object. Nothing should be
2124 * able to ref it, other than a vm_map_split(),
2125 * and vm_map_split() will stall on our chain lock.
2126 * And we control the parent so it shouldn't be
2127 * possible for it to go away either.
2129 * Since the backing object has no pages, no pager
2130 * left, and no object references within it, all
2131 * that is necessary is to dispose of it.
2133 KASSERT(backing_object->ref_count == 1,
2134 ("backing_object %p was somehow "
2135 "re-referenced during collapse!",
2137 KASSERT(RB_EMPTY(&backing_object->rb_memq),
2138 ("backing_object %p somehow has left "
2139 "over pages during collapse!",
2143 * The object can be destroyed.
2145 * XXX just fall through and dodealloc instead
2146 * of forcing destruction?
2148 --backing_object->ref_count;
2149 if ((backing_object->flags & OBJ_DEAD) == 0)
2150 vm_object_terminate(backing_object);
2155 * If we do not entirely shadow the backing object,
2156 * there is nothing we can do so we give up.
2158 if (vm_object_backing_scan(object, backing_object,
2159 OBSC_TEST_ALL_SHADOWED) == 0) {
2164 * bbobj is backing_object->backing_object. Since
2165 * object completely shadows backing_object we can
2166 * bypass it and become backed by bbobj instead.
2168 while ((bbobj = backing_object->backing_object) != NULL) {
2169 vm_object_hold(bbobj);
2170 if (bbobj == backing_object->backing_object)
2172 vm_object_drop(bbobj);
2176 * Make object shadow bbobj instead of backing_object.
2177 * Remove object from backing_object's shadow list.
2179 * Deallocating backing_object will not remove
2180 * it, since its reference count is at least 2.
2182 KKASSERT(object->backing_object == backing_object);
2183 LIST_REMOVE(object, shadow_list);
2184 backing_object->shadow_count--;
2185 backing_object->generation++;
2188 * Add a ref to bbobj, bbobj now shadows object.
2190 * NOTE: backing_object->backing_object still points
2191 * to bbobj. That relationship remains intact
2192 * because backing_object has > 1 ref, so
2193 * someone else is pointing to it (hence why
2194 * we can't collapse it into object and can
2195 * only handle the all-shadowed bypass case).
2198 vm_object_chain_wait(bbobj);
2199 vm_object_reference_locked(bbobj);
2200 LIST_INSERT_HEAD(&bbobj->shadow_head,
2201 object, shadow_list);
2202 bbobj->shadow_count++;
2203 bbobj->generation++;
2204 object->backing_object_offset +=
2205 backing_object->backing_object_offset;
2206 object->backing_object = bbobj;
2207 vm_object_drop(bbobj);
2209 object->backing_object = NULL;
2213 * Drop the reference count on backing_object. To
2214 * handle ref_count races properly we can't assume
2215 * that the ref_count is still at least 2 so we
2216 * have to actually call vm_object_deallocate()
2217 * (after clearing the chainlock).
2224 * Ok, we want to loop on the new object->bbobj association,
2225 * possibly collapsing it further. However if dodealloc is
2226 * non-zero we have to deallocate the backing_object which
2227 * itself can potentially undergo a collapse, creating a
2228 * recursion depth issue with the LWKT token subsystem.
2230 * In the case where we must deallocate the backing_object
2231 * it is possible now that the backing_object has a single
2232 * shadow count on some other object (not represented here
2233 * as yet), since it no longer shadows us. Thus when we
2234 * call vm_object_deallocate() it may attempt to collapse
2235 * itself into its remaining parent.
2238 struct vm_object_dealloc_list *dtmp;
2240 vm_object_chain_release(backing_object);
2241 vm_object_unlock(backing_object);
2242 /* backing_object remains held */
2245 * Auto-deallocation list for caller convenience.
2250 dtmp = kmalloc(sizeof(*dtmp), M_TEMP, M_WAITOK);
2251 dtmp->object = backing_object;
2252 dtmp->next = *dlistp;
2255 vm_object_chain_release(backing_object);
2256 vm_object_drop(backing_object);
2258 /* backing_object = NULL; not needed */
2263 * Clean up any left over backing_object
2265 if (backing_object) {
2266 vm_object_chain_release(backing_object);
2267 vm_object_drop(backing_object);
2271 * Clean up any auto-deallocation list. This is a convenience
2272 * for top-level callers so they don't have to pass &dlist.
2273 * Do not clean up any caller-passed dlistp, the caller will
2277 vm_object_deallocate_list(&dlist);
2282 * vm_object_collapse() may collect additional objects in need of
2283 * deallocation. This routine deallocates these objects. The
2284 * deallocation itself can trigger additional collapses (which the
2285 * deallocate function takes care of). This procedure is used to
2286 * reduce procedural recursion since these vm_object shadow chains
2287 * can become quite long.
2290 vm_object_deallocate_list(struct vm_object_dealloc_list **dlistp)
2292 struct vm_object_dealloc_list *dlist;
2294 while ((dlist = *dlistp) != NULL) {
2295 *dlistp = dlist->next;
2296 vm_object_lock(dlist->object);
2297 vm_object_deallocate_locked(dlist->object);
2298 vm_object_drop(dlist->object);
2299 kfree(dlist, M_TEMP);
2304 * Removes all physical pages in the specified object range from the
2305 * object's list of pages.
2309 static int vm_object_page_remove_callback(vm_page_t p, void *data);
2312 vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
2313 boolean_t clean_only)
2315 struct rb_vm_page_scan_info info;
2319 * Degenerate cases and assertions
2321 vm_object_hold(object);
2322 if (object == NULL ||
2323 (object->resident_page_count == 0 && object->swblock_count == 0)) {
2324 vm_object_drop(object);
2327 KASSERT(object->type != OBJT_PHYS,
2328 ("attempt to remove pages from a physical object"));
2331 * Indicate that paging is occuring on the object
2333 vm_object_pip_add(object, 1);
2336 * Figure out the actual removal range and whether we are removing
2337 * the entire contents of the object or not. If removing the entire
2338 * contents, be sure to get all pages, even those that might be
2339 * beyond the end of the object.
2341 info.start_pindex = start;
2343 info.end_pindex = (vm_pindex_t)-1;
2345 info.end_pindex = end - 1;
2346 info.limit = clean_only;
2347 all = (start == 0 && info.end_pindex >= object->size - 1);
2350 * Loop until we are sure we have gotten them all.
2354 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
2355 vm_object_page_remove_callback, &info);
2356 } while (info.error);
2359 * Remove any related swap if throwing away pages, or for
2360 * non-swap objects (the swap is a clean copy in that case).
2362 if (object->type != OBJT_SWAP || clean_only == FALSE) {
2364 swap_pager_freespace_all(object);
2366 swap_pager_freespace(object, info.start_pindex,
2367 info.end_pindex - info.start_pindex + 1);
2373 vm_object_pip_wakeup(object);
2374 vm_object_drop(object);
2378 * The caller must hold the object
2381 vm_object_page_remove_callback(vm_page_t p, void *data)
2383 struct rb_vm_page_scan_info *info = data;
2385 if (vm_page_busy_try(p, TRUE)) {
2386 vm_page_sleep_busy(p, TRUE, "vmopar");
2392 * Wired pages cannot be destroyed, but they can be invalidated
2393 * and we do so if clean_only (limit) is not set.
2395 * WARNING! The page may be wired due to being part of a buffer
2396 * cache buffer, and the buffer might be marked B_CACHE.
2397 * This is fine as part of a truncation but VFSs must be
2398 * sure to fix the buffer up when re-extending the file.
2400 * NOTE! PG_NEED_COMMIT is ignored.
2402 if (p->wire_count != 0) {
2403 vm_page_protect(p, VM_PROT_NONE);
2404 if (info->limit == 0)
2411 * limit is our clean_only flag. If set and the page is dirty or
2412 * requires a commit, do not free it. If set and the page is being
2413 * held by someone, do not free it.
2415 if (info->limit && p->valid) {
2416 vm_page_test_dirty(p);
2417 if ((p->valid & p->dirty) || (p->flags & PG_NEED_COMMIT)) {
2422 if (p->hold_count) {
2432 vm_page_protect(p, VM_PROT_NONE);
2438 * Coalesces two objects backing up adjoining regions of memory into a
2441 * returns TRUE if objects were combined.
2443 * NOTE: Only works at the moment if the second object is NULL -
2444 * if it's not, which object do we lock first?
2447 * prev_object First object to coalesce
2448 * prev_offset Offset into prev_object
2449 * next_object Second object into coalesce
2450 * next_offset Offset into next_object
2452 * prev_size Size of reference to prev_object
2453 * next_size Size of reference to next_object
2455 * The caller does not need to hold (prev_object) but must have a stable
2456 * pointer to it (typically by holding the vm_map locked).
2459 vm_object_coalesce(vm_object_t prev_object, vm_pindex_t prev_pindex,
2460 vm_size_t prev_size, vm_size_t next_size)
2462 vm_pindex_t next_pindex;
2464 if (prev_object == NULL)
2467 vm_object_hold(prev_object);
2469 if (prev_object->type != OBJT_DEFAULT &&
2470 prev_object->type != OBJT_SWAP) {
2471 vm_object_drop(prev_object);
2476 * Try to collapse the object first
2478 vm_object_chain_acquire(prev_object);
2479 vm_object_collapse(prev_object, NULL);
2482 * Can't coalesce if: . more than one reference . paged out . shadows
2483 * another object . has a copy elsewhere (any of which mean that the
2484 * pages not mapped to prev_entry may be in use anyway)
2487 if (prev_object->backing_object != NULL) {
2488 vm_object_chain_release(prev_object);
2489 vm_object_drop(prev_object);
2493 prev_size >>= PAGE_SHIFT;
2494 next_size >>= PAGE_SHIFT;
2495 next_pindex = prev_pindex + prev_size;
2497 if ((prev_object->ref_count > 1) &&
2498 (prev_object->size != next_pindex)) {
2499 vm_object_chain_release(prev_object);
2500 vm_object_drop(prev_object);
2505 * Remove any pages that may still be in the object from a previous
2508 if (next_pindex < prev_object->size) {
2509 vm_object_page_remove(prev_object,
2511 next_pindex + next_size, FALSE);
2512 if (prev_object->type == OBJT_SWAP)
2513 swap_pager_freespace(prev_object,
2514 next_pindex, next_size);
2518 * Extend the object if necessary.
2520 if (next_pindex + next_size > prev_object->size)
2521 prev_object->size = next_pindex + next_size;
2523 vm_object_chain_release(prev_object);
2524 vm_object_drop(prev_object);
2529 * Make the object writable and flag is being possibly dirty.
2531 * The caller must hold the object. XXX called from vm_page_dirty(),
2532 * There is currently no requirement to hold the object.
2535 vm_object_set_writeable_dirty(vm_object_t object)
2539 /*vm_object_assert_held(object);*/
2541 * Avoid contention in vm fault path by checking the state before
2542 * issuing an atomic op on it.
2544 if ((object->flags & (OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY)) !=
2545 (OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY)) {
2546 vm_object_set_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
2548 if (object->type == OBJT_VNODE &&
2549 (vp = (struct vnode *)object->handle) != NULL) {
2550 if ((vp->v_flag & VOBJDIRTY) == 0) {
2551 vsetflags(vp, VOBJDIRTY);
2556 #include "opt_ddb.h"
2558 #include <sys/kernel.h>
2560 #include <sys/cons.h>
2562 #include <ddb/ddb.h>
2564 static int _vm_object_in_map (vm_map_t map, vm_object_t object,
2565 vm_map_entry_t entry);
2566 static int vm_object_in_map (vm_object_t object);
2569 * The caller must hold the object.
2572 _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
2575 vm_map_entry_t tmpe;
2576 vm_object_t obj, nobj;
2582 tmpe = map->header.next;
2583 entcount = map->nentries;
2584 while (entcount-- && (tmpe != &map->header)) {
2585 if( _vm_object_in_map(map, object, tmpe)) {
2592 switch(entry->maptype) {
2593 case VM_MAPTYPE_SUBMAP:
2594 tmpm = entry->object.sub_map;
2595 tmpe = tmpm->header.next;
2596 entcount = tmpm->nentries;
2597 while (entcount-- && tmpe != &tmpm->header) {
2598 if( _vm_object_in_map(tmpm, object, tmpe)) {
2604 case VM_MAPTYPE_NORMAL:
2605 case VM_MAPTYPE_VPAGETABLE:
2606 obj = entry->object.vm_object;
2608 if (obj == object) {
2609 if (obj != entry->object.vm_object)
2610 vm_object_drop(obj);
2613 while ((nobj = obj->backing_object) != NULL) {
2614 vm_object_hold(nobj);
2615 if (nobj == obj->backing_object)
2617 vm_object_drop(nobj);
2619 if (obj != entry->object.vm_object) {
2621 vm_object_lock_swap();
2622 vm_object_drop(obj);
2633 static int vm_object_in_map_callback(struct proc *p, void *data);
2635 struct vm_object_in_map_info {
2644 vm_object_in_map(vm_object_t object)
2646 struct vm_object_in_map_info info;
2649 info.object = object;
2651 allproc_scan(vm_object_in_map_callback, &info);
2654 if( _vm_object_in_map(&kernel_map, object, 0))
2656 if( _vm_object_in_map(&pager_map, object, 0))
2658 if( _vm_object_in_map(&buffer_map, object, 0))
2667 vm_object_in_map_callback(struct proc *p, void *data)
2669 struct vm_object_in_map_info *info = data;
2672 if (_vm_object_in_map(&p->p_vmspace->vm_map, info->object, 0)) {
2680 DB_SHOW_COMMAND(vmochk, vm_object_check)
2685 * make sure that internal objs are in a map somewhere
2686 * and none have zero ref counts.
2688 for (object = TAILQ_FIRST(&vm_object_list);
2690 object = TAILQ_NEXT(object, object_list)) {
2691 if (object->type == OBJT_MARKER)
2693 if (object->handle == NULL &&
2694 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2695 if (object->ref_count == 0) {
2696 db_printf("vmochk: internal obj has zero ref count: %ld\n",
2697 (long)object->size);
2699 if (!vm_object_in_map(object)) {
2701 "vmochk: internal obj is not in a map: "
2702 "ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2703 object->ref_count, (u_long)object->size,
2704 (u_long)object->size,
2705 (void *)object->backing_object);
2714 DB_SHOW_COMMAND(object, vm_object_print_static)
2716 /* XXX convert args. */
2717 vm_object_t object = (vm_object_t)addr;
2718 boolean_t full = have_addr;
2722 /* XXX count is an (unused) arg. Avoid shadowing it. */
2723 #define count was_count
2731 "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n",
2732 object, (int)object->type, (u_long)object->size,
2733 object->resident_page_count, object->ref_count, object->flags);
2735 * XXX no %qd in kernel. Truncate object->backing_object_offset.
2737 db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%lx\n",
2738 object->shadow_count,
2739 object->backing_object ? object->backing_object->ref_count : 0,
2740 object->backing_object, (long)object->backing_object_offset);
2747 RB_FOREACH(p, vm_page_rb_tree, &object->rb_memq) {
2749 db_iprintf("memory:=");
2750 else if (count == 6) {
2758 db_printf("(off=0x%lx,page=0x%lx)",
2759 (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
2770 * XXX need this non-static entry for calling from vm_map_print.
2775 vm_object_print(/* db_expr_t */ long addr,
2776 boolean_t have_addr,
2777 /* db_expr_t */ long count,
2780 vm_object_print_static(addr, have_addr, count, modif);
2786 DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
2791 for (object = TAILQ_FIRST(&vm_object_list);
2793 object = TAILQ_NEXT(object, object_list)) {
2794 vm_pindex_t idx, fidx;
2796 vm_paddr_t pa = -1, padiff;
2800 if (object->type == OBJT_MARKER)
2802 db_printf("new object: %p\n", (void *)object);
2812 osize = object->size;
2815 for (idx = 0; idx < osize; idx++) {
2816 m = vm_page_lookup(object, idx);
2819 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2820 (long)fidx, rcount, (long)pa);
2835 (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
2840 padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
2841 padiff >>= PAGE_SHIFT;
2842 padiff &= PQ_L2_MASK;
2844 pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
2848 db_printf(" index(%ld)run(%d)pa(0x%lx)",
2849 (long)fidx, rcount, (long)pa);
2850 db_printf("pd(%ld)\n", (long)padiff);
2860 pa = VM_PAGE_TO_PHYS(m);
2864 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2865 (long)fidx, rcount, (long)pa);