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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * from: @(#)vm_object.c 8.5 (Berkeley) 3/22/94
35 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36 * All rights reserved.
38 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
40 * Permission to use, copy, modify and distribute this software and
41 * its documentation is hereby granted, provided that both the copyright
42 * notice and this permission notice appear in all copies of the
43 * software, derivative works or modified versions, and any portions
44 * thereof, and that both notices appear in supporting documentation.
46 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
50 * Carnegie Mellon requests users of this software to return to
52 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
53 * School of Computer Science
54 * Carnegie Mellon University
55 * Pittsburgh PA 15213-3890
57 * any improvements or extensions that they make and grant Carnegie the
58 * rights to redistribute these changes.
60 * $FreeBSD: src/sys/vm/vm_object.c,v 1.171.2.8 2003/05/26 19:17:56 alc Exp $
64 * Virtual memory object module.
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/proc.h> /* for curproc, pageproc */
70 #include <sys/thread.h>
71 #include <sys/vnode.h>
72 #include <sys/vmmeter.h>
74 #include <sys/mount.h>
75 #include <sys/kernel.h>
76 #include <sys/sysctl.h>
77 #include <sys/refcount.h>
80 #include <vm/vm_param.h>
82 #include <vm/vm_map.h>
83 #include <vm/vm_object.h>
84 #include <vm/vm_page.h>
85 #include <vm/vm_pageout.h>
86 #include <vm/vm_pager.h>
87 #include <vm/swap_pager.h>
88 #include <vm/vm_kern.h>
89 #include <vm/vm_extern.h>
90 #include <vm/vm_zone.h>
92 #define EASY_SCAN_FACTOR 8
94 static void vm_object_qcollapse(vm_object_t object,
95 vm_object_t backing_object);
96 static void vm_object_page_collect_flush(vm_object_t object, vm_page_t p,
98 static void vm_object_lock_init(vm_object_t);
102 * Virtual memory objects maintain the actual data
103 * associated with allocated virtual memory. A given
104 * page of memory exists within exactly one object.
106 * An object is only deallocated when all "references"
107 * are given up. Only one "reference" to a given
108 * region of an object should be writeable.
110 * Associated with each object is a list of all resident
111 * memory pages belonging to that object; this list is
112 * maintained by the "vm_page" module, and locked by the object's
115 * Each object also records a "pager" routine which is
116 * used to retrieve (and store) pages to the proper backing
117 * storage. In addition, objects may be backed by other
118 * objects from which they were virtual-copied.
120 * The only items within the object structure which are
121 * modified after time of creation are:
122 * reference count locked by object's lock
123 * pager routine locked by object's lock
127 struct object_q vm_object_list; /* locked by vmobj_token */
128 struct vm_object kernel_object;
130 static long vm_object_count; /* locked by vmobj_token */
131 extern int vm_pageout_page_count;
133 static long object_collapses;
134 static long object_bypasses;
135 static int next_index;
136 static vm_zone_t obj_zone;
137 static struct vm_zone obj_zone_store;
138 #define VM_OBJECTS_INIT 256
139 static struct vm_object vm_objects_init[VM_OBJECTS_INIT];
142 * Misc low level routines
145 vm_object_lock_init(vm_object_t obj)
147 #if defined(DEBUG_LOCKS)
150 obj->debug_hold_bitmap = 0;
151 obj->debug_hold_ovfl = 0;
152 for (i = 0; i < VMOBJ_DEBUG_ARRAY_SIZE; i++) {
153 obj->debug_hold_thrs[i] = NULL;
154 obj->debug_hold_file[i] = NULL;
155 obj->debug_hold_line[i] = 0;
161 vm_object_lock_swap(void)
167 vm_object_lock(vm_object_t obj)
169 lwkt_gettoken(&obj->token);
173 * Returns TRUE on sucesss
176 vm_object_lock_try(vm_object_t obj)
178 return(lwkt_trytoken(&obj->token));
182 vm_object_lock_shared(vm_object_t obj)
184 lwkt_gettoken_shared(&obj->token);
188 vm_object_unlock(vm_object_t obj)
190 lwkt_reltoken(&obj->token);
194 vm_object_assert_held(vm_object_t obj)
196 ASSERT_LWKT_TOKEN_HELD(&obj->token);
201 vm_object_hold(vm_object_t obj)
203 debugvm_object_hold(vm_object_t obj, char *file, int line)
206 KKASSERT(obj != NULL);
209 * Object must be held (object allocation is stable due to callers
210 * context, typically already holding the token on a parent object)
211 * prior to potentially blocking on the lock, otherwise the object
212 * can get ripped away from us.
214 refcount_acquire(&obj->hold_count);
217 #if defined(DEBUG_LOCKS)
222 mask = ~obj->debug_hold_bitmap;
224 if (mask == 0xFFFFFFFFU) {
225 if (obj->debug_hold_ovfl == 0)
226 obj->debug_hold_ovfl = 1;
230 if (atomic_cmpset_int(&obj->debug_hold_bitmap, ~mask,
232 obj->debug_hold_bitmap |= (1 << i);
233 obj->debug_hold_thrs[i] = curthread;
234 obj->debug_hold_file[i] = file;
235 obj->debug_hold_line[i] = line;
244 vm_object_hold_try(vm_object_t obj)
246 debugvm_object_hold_try(vm_object_t obj, char *file, int line)
249 KKASSERT(obj != NULL);
252 * Object must be held (object allocation is stable due to callers
253 * context, typically already holding the token on a parent object)
254 * prior to potentially blocking on the lock, otherwise the object
255 * can get ripped away from us.
257 refcount_acquire(&obj->hold_count);
258 if (vm_object_lock_try(obj) == 0) {
259 if (refcount_release(&obj->hold_count)) {
260 if (obj->ref_count == 0 && (obj->flags & OBJ_DEAD))
261 zfree(obj_zone, obj);
266 #if defined(DEBUG_LOCKS)
271 mask = ~obj->debug_hold_bitmap;
273 if (mask == 0xFFFFFFFFU) {
274 if (obj->debug_hold_ovfl == 0)
275 obj->debug_hold_ovfl = 1;
279 if (atomic_cmpset_int(&obj->debug_hold_bitmap, ~mask,
281 obj->debug_hold_bitmap |= (1 << i);
282 obj->debug_hold_thrs[i] = curthread;
283 obj->debug_hold_file[i] = file;
284 obj->debug_hold_line[i] = line;
294 vm_object_hold_shared(vm_object_t obj)
296 debugvm_object_hold_shared(vm_object_t obj, char *file, int line)
299 KKASSERT(obj != NULL);
302 * Object must be held (object allocation is stable due to callers
303 * context, typically already holding the token on a parent object)
304 * prior to potentially blocking on the lock, otherwise the object
305 * can get ripped away from us.
307 refcount_acquire(&obj->hold_count);
308 vm_object_lock_shared(obj);
310 #if defined(DEBUG_LOCKS)
315 mask = ~obj->debug_hold_bitmap;
317 if (mask == 0xFFFFFFFFU) {
318 if (obj->debug_hold_ovfl == 0)
319 obj->debug_hold_ovfl = 1;
323 if (atomic_cmpset_int(&obj->debug_hold_bitmap, ~mask,
325 obj->debug_hold_bitmap |= (1 << i);
326 obj->debug_hold_thrs[i] = curthread;
327 obj->debug_hold_file[i] = file;
328 obj->debug_hold_line[i] = line;
336 * Obtain either a shared or exclusive lock on VM object
337 * based on whether this is a terminal vnode object or not.
341 vm_object_hold_maybe_shared(vm_object_t obj)
343 debugvm_object_hold_maybe_shared(vm_object_t obj, char *file, int line)
346 if (vm_shared_fault &&
347 obj->type == OBJT_VNODE &&
348 obj->backing_object == NULL) {
349 vm_object_hold_shared(obj);
358 * Drop the token and hold_count on the object.
361 vm_object_drop(vm_object_t obj)
366 #if defined(DEBUG_LOCKS)
370 for (i = 0; i < VMOBJ_DEBUG_ARRAY_SIZE; i++) {
371 if ((obj->debug_hold_bitmap & (1 << i)) &&
372 (obj->debug_hold_thrs[i] == curthread)) {
373 obj->debug_hold_bitmap &= ~(1 << i);
374 obj->debug_hold_thrs[i] = NULL;
375 obj->debug_hold_file[i] = NULL;
376 obj->debug_hold_line[i] = 0;
382 if (found == 0 && obj->debug_hold_ovfl == 0)
383 panic("vm_object: attempt to drop hold on non-self-held obj");
387 * No new holders should be possible once we drop hold_count 1->0 as
388 * there is no longer any way to reference the object.
390 KKASSERT(obj->hold_count > 0);
391 if (refcount_release(&obj->hold_count)) {
392 if (obj->ref_count == 0 && (obj->flags & OBJ_DEAD)) {
393 vm_object_unlock(obj);
394 zfree(obj_zone, obj);
396 vm_object_unlock(obj);
399 vm_object_unlock(obj);
404 * Initialize a freshly allocated object, returning a held object.
406 * Used only by vm_object_allocate() and zinitna().
411 _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
415 RB_INIT(&object->rb_memq);
416 LIST_INIT(&object->shadow_head);
417 lwkt_token_init(&object->token, "vmobj");
421 object->ref_count = 1;
422 object->hold_count = 0;
424 if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
425 vm_object_set_flag(object, OBJ_ONEMAPPING);
426 object->paging_in_progress = 0;
427 object->resident_page_count = 0;
428 object->agg_pv_list_count = 0;
429 object->shadow_count = 0;
430 /* cpu localization twist */
431 object->pg_color = (int)(intptr_t)curthread;
432 if ( size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
433 incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
436 next_index = (next_index + incr) & PQ_L2_MASK;
437 object->handle = NULL;
438 object->backing_object = NULL;
439 object->backing_object_offset = (vm_ooffset_t)0;
441 object->generation++;
442 object->swblock_count = 0;
443 RB_INIT(&object->swblock_root);
444 vm_object_lock_init(object);
445 pmap_object_init(object);
447 vm_object_hold(object);
448 lwkt_gettoken(&vmobj_token);
449 TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
451 lwkt_reltoken(&vmobj_token);
455 * Initialize the VM objects module.
457 * Called from the low level boot code only.
462 TAILQ_INIT(&vm_object_list);
464 _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(KvaEnd),
466 vm_object_drop(&kernel_object);
468 obj_zone = &obj_zone_store;
469 zbootinit(obj_zone, "VM OBJECT", sizeof (struct vm_object),
470 vm_objects_init, VM_OBJECTS_INIT);
474 vm_object_init2(void)
476 zinitna(obj_zone, NULL, NULL, 0, 0, ZONE_PANICFAIL, 1);
480 * Allocate and return a new object of the specified type and size.
485 vm_object_allocate(objtype_t type, vm_pindex_t size)
489 result = (vm_object_t) zalloc(obj_zone);
491 _vm_object_allocate(type, size, result);
492 vm_object_drop(result);
498 * This version returns a held object, allowing further atomic initialization
502 vm_object_allocate_hold(objtype_t type, vm_pindex_t size)
506 result = (vm_object_t) zalloc(obj_zone);
508 _vm_object_allocate(type, size, result);
514 * Add an additional reference to a vm_object. The object must already be
515 * held. The original non-lock version is no longer supported. The object
516 * must NOT be chain locked by anyone at the time the reference is added.
518 * Referencing a chain-locked object can blow up the fairly sensitive
519 * ref_count and shadow_count tests in the deallocator. Most callers
520 * will call vm_object_chain_wait() prior to calling
521 * vm_object_reference_locked() to avoid the case.
523 * The object must be held, but may be held shared if desired (hence why
524 * we use an atomic op).
527 vm_object_reference_locked(vm_object_t object)
529 KKASSERT(object != NULL);
530 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
531 KKASSERT((object->flags & OBJ_CHAINLOCK) == 0);
532 atomic_add_int(&object->ref_count, 1);
533 if (object->type == OBJT_VNODE) {
534 vref(object->handle);
535 /* XXX what if the vnode is being destroyed? */
540 * Object OBJ_CHAINLOCK lock handling.
542 * The caller can chain-lock backing objects recursively and then
543 * use vm_object_chain_release_all() to undo the whole chain.
545 * Chain locks are used to prevent collapses and are only applicable
546 * to OBJT_DEFAULT and OBJT_SWAP objects. Chain locking operations
547 * on other object types are ignored. This is also important because
548 * it allows e.g. the vnode underlying a memory mapping to take concurrent
551 * The object must usually be held on entry, though intermediate
552 * objects need not be held on release.
555 vm_object_chain_wait(vm_object_t object)
557 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
558 while (object->flags & OBJ_CHAINLOCK) {
559 vm_object_set_flag(object, OBJ_CHAINWANT);
560 tsleep(object, 0, "objchain", 0);
565 vm_object_chain_acquire(vm_object_t object)
567 if (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP) {
568 vm_object_chain_wait(object);
569 vm_object_set_flag(object, OBJ_CHAINLOCK);
574 vm_object_chain_release(vm_object_t object)
576 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
577 if (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP) {
578 KKASSERT(object->flags & OBJ_CHAINLOCK);
579 if (object->flags & OBJ_CHAINWANT) {
580 vm_object_clear_flag(object,
581 OBJ_CHAINLOCK | OBJ_CHAINWANT);
584 vm_object_clear_flag(object, OBJ_CHAINLOCK);
590 * This releases the entire chain of objects from first_object to and
591 * including stopobj, flowing through object->backing_object.
593 * We release stopobj first as an optimization as this object is most
594 * likely to be shared across multiple processes.
597 vm_object_chain_release_all(vm_object_t first_object, vm_object_t stopobj)
599 vm_object_t backing_object;
602 vm_object_chain_release(stopobj);
603 object = first_object;
605 while (object != stopobj) {
607 if (object != first_object)
608 vm_object_hold(object);
609 backing_object = object->backing_object;
610 vm_object_chain_release(object);
611 if (object != first_object)
612 vm_object_drop(object);
613 object = backing_object;
618 * Dereference an object and its underlying vnode.
620 * The object must be held exclusively and will remain held on return.
621 * (We don't need an atomic op due to the exclusivity).
624 vm_object_vndeallocate(vm_object_t object)
626 struct vnode *vp = (struct vnode *) object->handle;
628 KASSERT(object->type == OBJT_VNODE,
629 ("vm_object_vndeallocate: not a vnode object"));
630 KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
631 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
633 if (object->ref_count == 0) {
634 vprint("vm_object_vndeallocate", vp);
635 panic("vm_object_vndeallocate: bad object reference count");
639 if (object->ref_count == 0)
640 vclrflags(vp, VTEXT);
645 * Release a reference to the specified object, gained either through a
646 * vm_object_allocate or a vm_object_reference call. When all references
647 * are gone, storage associated with this object may be relinquished.
649 * The caller does not have to hold the object locked but must have control
650 * over the reference in question in order to guarantee that the object
651 * does not get ripped out from under us.
653 * XXX Currently all deallocations require an exclusive lock.
656 vm_object_deallocate(vm_object_t object)
659 vm_object_hold(object);
660 vm_object_deallocate_locked(object);
661 vm_object_drop(object);
666 vm_object_deallocate_locked(vm_object_t object)
668 struct vm_object_dealloc_list *dlist = NULL;
669 struct vm_object_dealloc_list *dtmp;
674 * We may chain deallocate object, but additional objects may
675 * collect on the dlist which also have to be deallocated. We
676 * must avoid a recursion, vm_object chains can get deep.
679 while (object != NULL) {
680 ASSERT_LWKT_TOKEN_HELD_EXCL(&object->token);
683 * Don't rip a ref_count out from under an object undergoing
684 * collapse, it will confuse the collapse code.
686 vm_object_chain_wait(object);
688 if (object->type == OBJT_VNODE) {
689 vm_object_vndeallocate(object);
693 if (object->ref_count == 0) {
694 panic("vm_object_deallocate: object deallocated "
695 "too many times: %d", object->type);
697 if (object->ref_count > 2) {
703 * Here on ref_count of one or two, which are special cases for
706 * Nominal ref_count > 1 case if the second ref is not from
709 * (ONEMAPPING only applies to DEFAULT AND SWAP objects)
711 if (object->ref_count == 2 && object->shadow_count == 0) {
712 if (object->type == OBJT_DEFAULT ||
713 object->type == OBJT_SWAP) {
714 vm_object_set_flag(object, OBJ_ONEMAPPING);
721 * If the second ref is from a shadow we chain along it
722 * upwards if object's handle is exhausted.
724 * We have to decrement object->ref_count before potentially
725 * collapsing the first shadow object or the collapse code
726 * will not be able to handle the degenerate case to remove
727 * object. However, if we do it too early the object can
728 * get ripped out from under us.
730 if (object->ref_count == 2 && object->shadow_count == 1 &&
731 object->handle == NULL && (object->type == OBJT_DEFAULT ||
732 object->type == OBJT_SWAP)) {
733 temp = LIST_FIRST(&object->shadow_head);
734 KKASSERT(temp != NULL);
735 vm_object_hold(temp);
738 * Wait for any paging to complete so the collapse
739 * doesn't (or isn't likely to) qcollapse. pip
740 * waiting must occur before we acquire the
744 temp->paging_in_progress ||
745 object->paging_in_progress
747 vm_object_pip_wait(temp, "objde1");
748 vm_object_pip_wait(object, "objde2");
752 * If the parent is locked we have to give up, as
753 * otherwise we would be acquiring locks in the
754 * wrong order and potentially deadlock.
756 if (temp->flags & OBJ_CHAINLOCK) {
757 vm_object_drop(temp);
760 vm_object_chain_acquire(temp);
763 * Recheck/retry after the hold and the paging
764 * wait, both of which can block us.
766 if (object->ref_count != 2 ||
767 object->shadow_count != 1 ||
769 LIST_FIRST(&object->shadow_head) != temp ||
770 (object->type != OBJT_DEFAULT &&
771 object->type != OBJT_SWAP)) {
772 vm_object_chain_release(temp);
773 vm_object_drop(temp);
778 * We can safely drop object's ref_count now.
780 KKASSERT(object->ref_count == 2);
784 * If our single parent is not collapseable just
785 * decrement ref_count (2->1) and stop.
787 if (temp->handle || (temp->type != OBJT_DEFAULT &&
788 temp->type != OBJT_SWAP)) {
789 vm_object_chain_release(temp);
790 vm_object_drop(temp);
795 * At this point we have already dropped object's
796 * ref_count so it is possible for a race to
797 * deallocate obj out from under us. Any collapse
798 * will re-check the situation. We must not block
799 * until we are able to collapse.
801 * Bump temp's ref_count to avoid an unwanted
802 * degenerate recursion (can't call
803 * vm_object_reference_locked() because it asserts
804 * that CHAINLOCK is not set).
807 KKASSERT(temp->ref_count > 1);
810 * Collapse temp, then deallocate the extra ref
813 vm_object_collapse(temp, &dlist);
814 vm_object_chain_release(temp);
816 vm_object_lock_swap();
817 vm_object_drop(object);
825 * Drop the ref and handle termination on the 1->0 transition.
826 * We may have blocked above so we have to recheck.
829 KKASSERT(object->ref_count != 0);
830 if (object->ref_count >= 2) {
834 KKASSERT(object->ref_count == 1);
837 * 1->0 transition. Chain through the backing_object.
838 * Maintain the ref until we've located the backing object,
841 while ((temp = object->backing_object) != NULL) {
842 vm_object_hold(temp);
843 if (temp == object->backing_object)
845 vm_object_drop(temp);
849 * 1->0 transition verified, retry if ref_count is no longer
850 * 1. Otherwise disconnect the backing_object (temp) and
853 if (object->ref_count != 1) {
854 vm_object_drop(temp);
859 * It shouldn't be possible for the object to be chain locked
860 * if we're removing the last ref on it.
862 KKASSERT((object->flags & OBJ_CHAINLOCK) == 0);
865 LIST_REMOVE(object, shadow_list);
866 temp->shadow_count--;
868 object->backing_object = NULL;
872 if ((object->flags & OBJ_DEAD) == 0)
873 vm_object_terminate(object);
874 if (must_drop && temp)
875 vm_object_lock_swap();
877 vm_object_drop(object);
881 if (must_drop && object)
882 vm_object_drop(object);
885 * Additional tail recursion on dlist. Avoid a recursion. Objects
886 * on the dlist have a hold count but are not locked.
888 if ((dtmp = dlist) != NULL) {
890 object = dtmp->object;
893 vm_object_lock(object); /* already held, add lock */
894 must_drop = 1; /* and we're responsible for it */
900 * Destroy the specified object, freeing up related resources.
902 * The object must have zero references.
904 * The object must held. The caller is responsible for dropping the object
905 * after terminate returns. Terminate does NOT drop the object.
907 static int vm_object_terminate_callback(vm_page_t p, void *data);
910 vm_object_terminate(vm_object_t object)
913 * Make sure no one uses us. Once we set OBJ_DEAD we should be
914 * able to safely block.
916 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
917 KKASSERT((object->flags & OBJ_DEAD) == 0);
918 vm_object_set_flag(object, OBJ_DEAD);
921 * Wait for the pageout daemon to be done with the object
923 vm_object_pip_wait(object, "objtrm1");
925 KASSERT(!object->paging_in_progress,
926 ("vm_object_terminate: pageout in progress"));
929 * Clean and free the pages, as appropriate. All references to the
930 * object are gone, so we don't need to lock it.
932 if (object->type == OBJT_VNODE) {
936 * Clean pages and flush buffers.
938 * NOTE! TMPFS buffer flushes do not typically flush the
939 * actual page to swap as this would be highly
940 * inefficient, and normal filesystems usually wrap
941 * page flushes with buffer cache buffers.
943 * To deal with this we have to call vinvalbuf() both
944 * before and after the vm_object_page_clean().
946 vp = (struct vnode *) object->handle;
947 vinvalbuf(vp, V_SAVE, 0, 0);
948 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
949 vinvalbuf(vp, V_SAVE, 0, 0);
953 * Wait for any I/O to complete, after which there had better not
954 * be any references left on the object.
956 vm_object_pip_wait(object, "objtrm2");
958 if (object->ref_count != 0) {
959 panic("vm_object_terminate: object with references, "
960 "ref_count=%d", object->ref_count);
964 * Cleanup any shared pmaps associated with this object.
966 pmap_object_free(object);
969 * Now free any remaining pages. For internal objects, this also
970 * removes them from paging queues. Don't free wired pages, just
971 * remove them from the object.
973 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
974 vm_object_terminate_callback, NULL);
977 * Let the pager know object is dead.
979 vm_pager_deallocate(object);
982 * Wait for the object hold count to hit 1, clean out pages as
983 * we go. vmobj_token interlocks any race conditions that might
984 * pick the object up from the vm_object_list after we have cleared
988 if (RB_ROOT(&object->rb_memq) == NULL)
990 kprintf("vm_object_terminate: Warning, object %p "
991 "still has %d pages\n",
992 object, object->resident_page_count);
993 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
994 vm_object_terminate_callback, NULL);
998 * There had better not be any pages left
1000 KKASSERT(object->resident_page_count == 0);
1003 * Remove the object from the global object list.
1005 lwkt_gettoken(&vmobj_token);
1006 TAILQ_REMOVE(&vm_object_list, object, object_list);
1008 lwkt_reltoken(&vmobj_token);
1009 vm_object_dead_wakeup(object);
1011 if (object->ref_count != 0) {
1012 panic("vm_object_terminate2: object with references, "
1013 "ref_count=%d", object->ref_count);
1017 * NOTE: The object hold_count is at least 1, so we cannot zfree()
1018 * the object here. See vm_object_drop().
1023 * The caller must hold the object.
1026 vm_object_terminate_callback(vm_page_t p, void *data __unused)
1031 vm_page_busy_wait(p, TRUE, "vmpgtrm");
1032 if (object != p->object) {
1033 kprintf("vm_object_terminate: Warning: Encountered "
1034 "busied page %p on queue %d\n", p, p->queue);
1036 } else if (p->wire_count == 0) {
1038 * NOTE: p->dirty and PG_NEED_COMMIT are ignored.
1041 mycpu->gd_cnt.v_pfree++;
1043 if (p->queue != PQ_NONE)
1044 kprintf("vm_object_terminate: Warning: Encountered "
1045 "wired page %p on queue %d\n", p, p->queue);
1054 * The object is dead but still has an object<->pager association. Sleep
1055 * and return. The caller typically retests the association in a loop.
1057 * The caller must hold the object.
1060 vm_object_dead_sleep(vm_object_t object, const char *wmesg)
1062 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
1063 if (object->handle) {
1064 vm_object_set_flag(object, OBJ_DEADWNT);
1065 tsleep(object, 0, wmesg, 0);
1066 /* object may be invalid after this point */
1071 * Wakeup anyone waiting for the object<->pager disassociation on
1074 * The caller must hold the object.
1077 vm_object_dead_wakeup(vm_object_t object)
1079 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
1080 if (object->flags & OBJ_DEADWNT) {
1081 vm_object_clear_flag(object, OBJ_DEADWNT);
1087 * Clean all dirty pages in the specified range of object. Leaves page
1088 * on whatever queue it is currently on. If NOSYNC is set then do not
1089 * write out pages with PG_NOSYNC set (originally comes from MAP_NOSYNC),
1090 * leaving the object dirty.
1092 * When stuffing pages asynchronously, allow clustering. XXX we need a
1093 * synchronous clustering mode implementation.
1095 * Odd semantics: if start == end, we clean everything.
1097 * The object must be locked? XXX
1099 static int vm_object_page_clean_pass1(struct vm_page *p, void *data);
1100 static int vm_object_page_clean_pass2(struct vm_page *p, void *data);
1103 vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
1106 struct rb_vm_page_scan_info info;
1112 vm_object_hold(object);
1113 if (object->type != OBJT_VNODE ||
1114 (object->flags & OBJ_MIGHTBEDIRTY) == 0) {
1115 vm_object_drop(object);
1119 pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ?
1120 VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
1121 pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
1123 vp = object->handle;
1126 * Interlock other major object operations. This allows us to
1127 * temporarily clear OBJ_WRITEABLE and OBJ_MIGHTBEDIRTY.
1129 vm_object_set_flag(object, OBJ_CLEANING);
1132 * Handle 'entire object' case
1134 info.start_pindex = start;
1136 info.end_pindex = object->size - 1;
1138 info.end_pindex = end - 1;
1140 wholescan = (start == 0 && info.end_pindex == object->size - 1);
1142 info.pagerflags = pagerflags;
1143 info.object = object;
1146 * If cleaning the entire object do a pass to mark the pages read-only.
1147 * If everything worked out ok, clear OBJ_WRITEABLE and
1152 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
1153 vm_object_page_clean_pass1, &info);
1154 if (info.error == 0) {
1155 vm_object_clear_flag(object,
1156 OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
1157 if (object->type == OBJT_VNODE &&
1158 (vp = (struct vnode *)object->handle) != NULL) {
1159 if (vp->v_flag & VOBJDIRTY)
1160 vclrflags(vp, VOBJDIRTY);
1166 * Do a pass to clean all the dirty pages we find.
1170 generation = object->generation;
1171 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
1172 vm_object_page_clean_pass2, &info);
1173 } while (info.error || generation != object->generation);
1175 vm_object_clear_flag(object, OBJ_CLEANING);
1176 vm_object_drop(object);
1180 * The caller must hold the object.
1184 vm_object_page_clean_pass1(struct vm_page *p, void *data)
1186 struct rb_vm_page_scan_info *info = data;
1188 vm_page_flag_set(p, PG_CLEANCHK);
1189 if ((info->limit & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
1191 } else if (vm_page_busy_try(p, FALSE) == 0) {
1192 vm_page_protect(p, VM_PROT_READ); /* must not block */
1202 * The caller must hold the object
1206 vm_object_page_clean_pass2(struct vm_page *p, void *data)
1208 struct rb_vm_page_scan_info *info = data;
1212 * Do not mess with pages that were inserted after we started
1213 * the cleaning pass.
1215 if ((p->flags & PG_CLEANCHK) == 0)
1218 generation = info->object->generation;
1219 vm_page_busy_wait(p, TRUE, "vpcwai");
1220 if (p->object != info->object ||
1221 info->object->generation != generation) {
1228 * Before wasting time traversing the pmaps, check for trivial
1229 * cases where the page cannot be dirty.
1231 if (p->valid == 0 || (p->queue - p->pc) == PQ_CACHE) {
1232 KKASSERT((p->dirty & p->valid) == 0 &&
1233 (p->flags & PG_NEED_COMMIT) == 0);
1239 * Check whether the page is dirty or not. The page has been set
1240 * to be read-only so the check will not race a user dirtying the
1243 vm_page_test_dirty(p);
1244 if ((p->dirty & p->valid) == 0 && (p->flags & PG_NEED_COMMIT) == 0) {
1245 vm_page_flag_clear(p, PG_CLEANCHK);
1251 * If we have been asked to skip nosync pages and this is a
1252 * nosync page, skip it. Note that the object flags were
1253 * not cleared in this case (because pass1 will have returned an
1254 * error), so we do not have to set them.
1256 if ((info->limit & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
1257 vm_page_flag_clear(p, PG_CLEANCHK);
1263 * Flush as many pages as we can. PG_CLEANCHK will be cleared on
1264 * the pages that get successfully flushed. Set info->error if
1265 * we raced an object modification.
1267 vm_object_page_collect_flush(info->object, p, info->pagerflags);
1275 * Collect the specified page and nearby pages and flush them out.
1276 * The number of pages flushed is returned. The passed page is busied
1277 * by the caller and we are responsible for its disposition.
1279 * The caller must hold the object.
1282 vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int pagerflags)
1291 vm_page_t maf[vm_pageout_page_count];
1292 vm_page_t mab[vm_pageout_page_count];
1293 vm_page_t ma[vm_pageout_page_count];
1295 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
1300 for(i = 1; i < vm_pageout_page_count; i++) {
1303 tp = vm_page_lookup_busy_try(object, pi + i, TRUE, &error);
1308 if ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
1309 (tp->flags & PG_CLEANCHK) == 0) {
1313 if ((tp->queue - tp->pc) == PQ_CACHE) {
1314 vm_page_flag_clear(tp, PG_CLEANCHK);
1318 vm_page_test_dirty(tp);
1319 if ((tp->dirty & tp->valid) == 0 &&
1320 (tp->flags & PG_NEED_COMMIT) == 0) {
1321 vm_page_flag_clear(tp, PG_CLEANCHK);
1330 chkb = vm_pageout_page_count - maxf;
1332 * NOTE: chkb can be 0
1334 for(i = 1; chkb && i < chkb; i++) {
1337 tp = vm_page_lookup_busy_try(object, pi - i, TRUE, &error);
1342 if ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
1343 (tp->flags & PG_CLEANCHK) == 0) {
1347 if ((tp->queue - tp->pc) == PQ_CACHE) {
1348 vm_page_flag_clear(tp, PG_CLEANCHK);
1352 vm_page_test_dirty(tp);
1353 if ((tp->dirty & tp->valid) == 0 &&
1354 (tp->flags & PG_NEED_COMMIT) == 0) {
1355 vm_page_flag_clear(tp, PG_CLEANCHK);
1364 * All pages in the maf[] and mab[] array are busied.
1366 for (i = 0; i < maxb; i++) {
1367 int index = (maxb - i) - 1;
1369 vm_page_flag_clear(ma[index], PG_CLEANCHK);
1371 vm_page_flag_clear(p, PG_CLEANCHK);
1373 for(i = 0; i < maxf; i++) {
1374 int index = (maxb + i) + 1;
1376 vm_page_flag_clear(ma[index], PG_CLEANCHK);
1378 runlen = maxb + maxf + 1;
1380 for (i = 0; i < runlen; i++) /* XXX need this any more? */
1381 vm_page_hold(ma[i]);
1383 vm_pageout_flush(ma, runlen, pagerflags);
1385 for (i = 0; i < runlen; i++) /* XXX need this any more? */
1386 vm_page_unhold(ma[i]);
1390 * Same as vm_object_pmap_copy, except range checking really
1391 * works, and is meant for small sections of an object.
1393 * This code protects resident pages by making them read-only
1394 * and is typically called on a fork or split when a page
1395 * is converted to copy-on-write.
1397 * NOTE: If the page is already at VM_PROT_NONE, calling
1398 * vm_page_protect will have no effect.
1401 vm_object_pmap_copy_1(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1406 if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
1409 vm_object_hold(object);
1410 for (idx = start; idx < end; idx++) {
1411 p = vm_page_lookup(object, idx);
1414 vm_page_protect(p, VM_PROT_READ);
1416 vm_object_drop(object);
1420 * Removes all physical pages in the specified object range from all
1423 * The object must *not* be locked.
1426 static int vm_object_pmap_remove_callback(vm_page_t p, void *data);
1429 vm_object_pmap_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1431 struct rb_vm_page_scan_info info;
1435 info.start_pindex = start;
1436 info.end_pindex = end - 1;
1438 vm_object_hold(object);
1439 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
1440 vm_object_pmap_remove_callback, &info);
1441 if (start == 0 && end == object->size)
1442 vm_object_clear_flag(object, OBJ_WRITEABLE);
1443 vm_object_drop(object);
1447 * The caller must hold the object
1450 vm_object_pmap_remove_callback(vm_page_t p, void *data __unused)
1452 vm_page_protect(p, VM_PROT_NONE);
1457 * Implements the madvise function at the object/page level.
1459 * MADV_WILLNEED (any object)
1461 * Activate the specified pages if they are resident.
1463 * MADV_DONTNEED (any object)
1465 * Deactivate the specified pages if they are resident.
1467 * MADV_FREE (OBJT_DEFAULT/OBJT_SWAP objects, OBJ_ONEMAPPING only)
1469 * Deactivate and clean the specified pages if they are
1470 * resident. This permits the process to reuse the pages
1471 * without faulting or the kernel to reclaim the pages
1477 vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
1479 vm_pindex_t end, tpindex;
1480 vm_object_t tobject;
1488 end = pindex + count;
1490 vm_object_hold(object);
1494 * Locate and adjust resident pages
1496 for (; pindex < end; pindex += 1) {
1498 if (tobject != object)
1499 vm_object_drop(tobject);
1504 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
1505 * and those pages must be OBJ_ONEMAPPING.
1507 if (advise == MADV_FREE) {
1508 if ((tobject->type != OBJT_DEFAULT &&
1509 tobject->type != OBJT_SWAP) ||
1510 (tobject->flags & OBJ_ONEMAPPING) == 0) {
1515 m = vm_page_lookup_busy_try(tobject, tpindex, TRUE, &error);
1518 vm_page_sleep_busy(m, TRUE, "madvpo");
1523 * There may be swap even if there is no backing page
1525 if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1526 swap_pager_freespace(tobject, tpindex, 1);
1531 while ((xobj = tobject->backing_object) != NULL) {
1532 KKASSERT(xobj != object);
1533 vm_object_hold(xobj);
1534 if (xobj == tobject->backing_object)
1536 vm_object_drop(xobj);
1540 tpindex += OFF_TO_IDX(tobject->backing_object_offset);
1541 if (tobject != object) {
1542 vm_object_lock_swap();
1543 vm_object_drop(tobject);
1550 * If the page is not in a normal active state, we skip it.
1551 * If the page is not managed there are no page queues to
1552 * mess with. Things can break if we mess with pages in
1553 * any of the below states.
1555 if (m->wire_count ||
1556 (m->flags & (PG_UNMANAGED | PG_NEED_COMMIT)) ||
1557 m->valid != VM_PAGE_BITS_ALL
1564 * Theoretically once a page is known not to be busy, an
1565 * interrupt cannot come along and rip it out from under us.
1568 if (advise == MADV_WILLNEED) {
1569 vm_page_activate(m);
1570 } else if (advise == MADV_DONTNEED) {
1571 vm_page_dontneed(m);
1572 } else if (advise == MADV_FREE) {
1574 * Mark the page clean. This will allow the page
1575 * to be freed up by the system. However, such pages
1576 * are often reused quickly by malloc()/free()
1577 * so we do not do anything that would cause
1578 * a page fault if we can help it.
1580 * Specifically, we do not try to actually free
1581 * the page now nor do we try to put it in the
1582 * cache (which would cause a page fault on reuse).
1584 * But we do make the page is freeable as we
1585 * can without actually taking the step of unmapping
1588 pmap_clear_modify(m);
1591 vm_page_dontneed(m);
1592 if (tobject->type == OBJT_SWAP)
1593 swap_pager_freespace(tobject, tpindex, 1);
1597 if (tobject != object)
1598 vm_object_drop(tobject);
1599 vm_object_drop(object);
1603 * Create a new object which is backed by the specified existing object
1604 * range. Replace the pointer and offset that was pointing at the existing
1605 * object with the pointer/offset for the new object.
1607 * No other requirements.
1610 vm_object_shadow(vm_object_t *objectp, vm_ooffset_t *offset, vm_size_t length,
1619 * Don't create the new object if the old object isn't shared.
1620 * We have to chain wait before adding the reference to avoid
1621 * racing a collapse or deallocation.
1623 * Add the additional ref to source here to avoid racing a later
1624 * collapse or deallocation. Clear the ONEMAPPING flag whether
1625 * addref is TRUE or not in this case because the original object
1629 vm_object_hold(source);
1630 vm_object_chain_wait(source);
1631 if (source->ref_count == 1 &&
1632 source->handle == NULL &&
1633 (source->type == OBJT_DEFAULT ||
1634 source->type == OBJT_SWAP)) {
1635 vm_object_drop(source);
1637 vm_object_reference_locked(source);
1638 vm_object_clear_flag(source, OBJ_ONEMAPPING);
1642 vm_object_reference_locked(source);
1643 vm_object_clear_flag(source, OBJ_ONEMAPPING);
1647 * Allocate a new object with the given length. The new object
1648 * is returned referenced but we may have to add another one.
1649 * If we are adding a second reference we must clear OBJ_ONEMAPPING.
1650 * (typically because the caller is about to clone a vm_map_entry).
1652 * The source object currently has an extra reference to prevent
1653 * collapses into it while we mess with its shadow list, which
1654 * we will remove later in this routine.
1656 if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL)
1657 panic("vm_object_shadow: no object for shadowing");
1658 vm_object_hold(result);
1660 vm_object_reference_locked(result);
1661 vm_object_clear_flag(result, OBJ_ONEMAPPING);
1665 * The new object shadows the source object. Chain wait before
1666 * adjusting shadow_count or the shadow list to avoid races.
1668 * Try to optimize the result object's page color when shadowing
1669 * in order to maintain page coloring consistency in the combined
1672 KKASSERT(result->backing_object == NULL);
1673 result->backing_object = source;
1675 vm_object_chain_wait(source);
1676 LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1677 source->shadow_count++;
1678 source->generation++;
1679 /* cpu localization twist */
1680 result->pg_color = (int)(intptr_t)curthread;
1684 * Adjust the return storage. Drop the ref on source before
1687 result->backing_object_offset = *offset;
1688 vm_object_drop(result);
1691 vm_object_deallocate_locked(source);
1692 vm_object_drop(source);
1696 * Return the new things
1701 #define OBSC_TEST_ALL_SHADOWED 0x0001
1702 #define OBSC_COLLAPSE_NOWAIT 0x0002
1703 #define OBSC_COLLAPSE_WAIT 0x0004
1705 static int vm_object_backing_scan_callback(vm_page_t p, void *data);
1708 * The caller must hold the object.
1711 vm_object_backing_scan(vm_object_t object, vm_object_t backing_object, int op)
1713 struct rb_vm_page_scan_info info;
1715 vm_object_assert_held(object);
1716 vm_object_assert_held(backing_object);
1718 KKASSERT(backing_object == object->backing_object);
1719 info.backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
1722 * Initial conditions
1724 if (op & OBSC_TEST_ALL_SHADOWED) {
1726 * We do not want to have to test for the existence of
1727 * swap pages in the backing object. XXX but with the
1728 * new swapper this would be pretty easy to do.
1730 * XXX what about anonymous MAP_SHARED memory that hasn't
1731 * been ZFOD faulted yet? If we do not test for this, the
1732 * shadow test may succeed! XXX
1734 if (backing_object->type != OBJT_DEFAULT)
1737 if (op & OBSC_COLLAPSE_WAIT) {
1738 KKASSERT((backing_object->flags & OBJ_DEAD) == 0);
1739 vm_object_set_flag(backing_object, OBJ_DEAD);
1740 lwkt_gettoken(&vmobj_token);
1741 TAILQ_REMOVE(&vm_object_list, backing_object, object_list);
1743 lwkt_reltoken(&vmobj_token);
1744 vm_object_dead_wakeup(backing_object);
1748 * Our scan. We have to retry if a negative error code is returned,
1749 * otherwise 0 or 1 will be returned in info.error. 0 Indicates that
1750 * the scan had to be stopped because the parent does not completely
1753 info.object = object;
1754 info.backing_object = backing_object;
1758 vm_page_rb_tree_RB_SCAN(&backing_object->rb_memq, NULL,
1759 vm_object_backing_scan_callback,
1761 } while (info.error < 0);
1767 * The caller must hold the object.
1770 vm_object_backing_scan_callback(vm_page_t p, void *data)
1772 struct rb_vm_page_scan_info *info = data;
1773 vm_object_t backing_object;
1776 vm_pindex_t new_pindex;
1777 vm_pindex_t backing_offset_index;
1781 new_pindex = pindex - info->backing_offset_index;
1783 object = info->object;
1784 backing_object = info->backing_object;
1785 backing_offset_index = info->backing_offset_index;
1787 if (op & OBSC_TEST_ALL_SHADOWED) {
1791 * Ignore pages outside the parent object's range
1792 * and outside the parent object's mapping of the
1795 * note that we do not busy the backing object's
1798 if (pindex < backing_offset_index ||
1799 new_pindex >= object->size
1805 * See if the parent has the page or if the parent's
1806 * object pager has the page. If the parent has the
1807 * page but the page is not valid, the parent's
1808 * object pager must have the page.
1810 * If this fails, the parent does not completely shadow
1811 * the object and we might as well give up now.
1813 pp = vm_page_lookup(object, new_pindex);
1814 if ((pp == NULL || pp->valid == 0) &&
1815 !vm_pager_has_page(object, new_pindex)
1817 info->error = 0; /* problemo */
1818 return(-1); /* stop the scan */
1823 * Check for busy page. Note that we may have lost (p) when we
1824 * possibly blocked above.
1826 if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
1829 if (vm_page_busy_try(p, TRUE)) {
1830 if (op & OBSC_COLLAPSE_NOWAIT) {
1834 * If we slept, anything could have
1835 * happened. Ask that the scan be restarted.
1837 * Since the object is marked dead, the
1838 * backing offset should not have changed.
1840 vm_page_sleep_busy(p, TRUE, "vmocol");
1847 * If (p) is no longer valid restart the scan.
1849 if (p->object != backing_object || p->pindex != pindex) {
1850 kprintf("vm_object_backing_scan: Warning: page "
1851 "%p ripped out from under us\n", p);
1857 if (op & OBSC_COLLAPSE_NOWAIT) {
1858 if (p->valid == 0 ||
1860 (p->flags & PG_NEED_COMMIT)) {
1865 /* XXX what if p->valid == 0 , hold_count, etc? */
1869 p->object == backing_object,
1870 ("vm_object_qcollapse(): object mismatch")
1874 * Destroy any associated swap
1876 if (backing_object->type == OBJT_SWAP)
1877 swap_pager_freespace(backing_object, p->pindex, 1);
1880 p->pindex < backing_offset_index ||
1881 new_pindex >= object->size
1884 * Page is out of the parent object's range, we
1885 * can simply destroy it.
1887 vm_page_protect(p, VM_PROT_NONE);
1892 pp = vm_page_lookup(object, new_pindex);
1893 if (pp != NULL || vm_pager_has_page(object, new_pindex)) {
1895 * page already exists in parent OR swap exists
1896 * for this location in the parent. Destroy
1897 * the original page from the backing object.
1899 * Leave the parent's page alone
1901 vm_page_protect(p, VM_PROT_NONE);
1907 * Page does not exist in parent, rename the
1908 * page from the backing object to the main object.
1910 * If the page was mapped to a process, it can remain
1911 * mapped through the rename.
1913 if ((p->queue - p->pc) == PQ_CACHE)
1914 vm_page_deactivate(p);
1916 vm_page_rename(p, object, new_pindex);
1918 /* page automatically made dirty by rename */
1924 * This version of collapse allows the operation to occur earlier and
1925 * when paging_in_progress is true for an object... This is not a complete
1926 * operation, but should plug 99.9% of the rest of the leaks.
1928 * The caller must hold the object and backing_object and both must be
1931 * (only called from vm_object_collapse)
1934 vm_object_qcollapse(vm_object_t object, vm_object_t backing_object)
1936 if (backing_object->ref_count == 1) {
1937 backing_object->ref_count += 2;
1938 vm_object_backing_scan(object, backing_object,
1939 OBSC_COLLAPSE_NOWAIT);
1940 backing_object->ref_count -= 2;
1945 * Collapse an object with the object backing it. Pages in the backing
1946 * object are moved into the parent, and the backing object is deallocated.
1947 * Any conflict is resolved in favor of the parent's existing pages.
1949 * object must be held and chain-locked on call.
1951 * The caller must have an extra ref on object to prevent a race from
1952 * destroying it during the collapse.
1955 vm_object_collapse(vm_object_t object, struct vm_object_dealloc_list **dlistp)
1957 struct vm_object_dealloc_list *dlist = NULL;
1958 vm_object_t backing_object;
1961 * Only one thread is attempting a collapse at any given moment.
1962 * There are few restrictions for (object) that callers of this
1963 * function check so reentrancy is likely.
1965 KKASSERT(object != NULL);
1966 vm_object_assert_held(object);
1967 KKASSERT(object->flags & OBJ_CHAINLOCK);
1974 * We have to hold the backing object, check races.
1976 while ((backing_object = object->backing_object) != NULL) {
1977 vm_object_hold(backing_object);
1978 if (backing_object == object->backing_object)
1980 vm_object_drop(backing_object);
1984 * No backing object? Nothing to collapse then.
1986 if (backing_object == NULL)
1990 * You can't collapse with a non-default/non-swap object.
1992 if (backing_object->type != OBJT_DEFAULT &&
1993 backing_object->type != OBJT_SWAP) {
1994 vm_object_drop(backing_object);
1995 backing_object = NULL;
2000 * Chain-lock the backing object too because if we
2001 * successfully merge its pages into the top object we
2002 * will collapse backing_object->backing_object as the
2003 * new backing_object. Re-check that it is still our
2006 vm_object_chain_acquire(backing_object);
2007 if (backing_object != object->backing_object) {
2008 vm_object_chain_release(backing_object);
2009 vm_object_drop(backing_object);
2014 * we check the backing object first, because it is most likely
2017 if (backing_object->handle != NULL ||
2018 (backing_object->type != OBJT_DEFAULT &&
2019 backing_object->type != OBJT_SWAP) ||
2020 (backing_object->flags & OBJ_DEAD) ||
2021 object->handle != NULL ||
2022 (object->type != OBJT_DEFAULT &&
2023 object->type != OBJT_SWAP) ||
2024 (object->flags & OBJ_DEAD)) {
2029 * If paging is in progress we can't do a normal collapse.
2032 object->paging_in_progress != 0 ||
2033 backing_object->paging_in_progress != 0
2035 vm_object_qcollapse(object, backing_object);
2040 * We know that we can either collapse the backing object (if
2041 * the parent is the only reference to it) or (perhaps) have
2042 * the parent bypass the object if the parent happens to shadow
2043 * all the resident pages in the entire backing object.
2045 * This is ignoring pager-backed pages such as swap pages.
2046 * vm_object_backing_scan fails the shadowing test in this
2049 if (backing_object->ref_count == 1) {
2051 * If there is exactly one reference to the backing
2052 * object, we can collapse it into the parent.
2054 KKASSERT(object->backing_object == backing_object);
2055 vm_object_backing_scan(object, backing_object,
2056 OBSC_COLLAPSE_WAIT);
2059 * Move the pager from backing_object to object.
2061 if (backing_object->type == OBJT_SWAP) {
2062 vm_object_pip_add(backing_object, 1);
2065 * scrap the paging_offset junk and do a
2066 * discrete copy. This also removes major
2067 * assumptions about how the swap-pager
2068 * works from where it doesn't belong. The
2069 * new swapper is able to optimize the
2070 * destroy-source case.
2072 vm_object_pip_add(object, 1);
2073 swap_pager_copy(backing_object, object,
2074 OFF_TO_IDX(object->backing_object_offset),
2076 vm_object_pip_wakeup(object);
2077 vm_object_pip_wakeup(backing_object);
2081 * Object now shadows whatever backing_object did.
2082 * Remove object from backing_object's shadow_list.
2084 LIST_REMOVE(object, shadow_list);
2085 KKASSERT(object->backing_object == backing_object);
2086 backing_object->shadow_count--;
2087 backing_object->generation++;
2090 * backing_object->backing_object moves from within
2091 * backing_object to within object.
2093 while ((bbobj = backing_object->backing_object) != NULL) {
2094 vm_object_hold(bbobj);
2095 if (bbobj == backing_object->backing_object)
2097 vm_object_drop(bbobj);
2100 LIST_REMOVE(backing_object, shadow_list);
2101 bbobj->shadow_count--;
2102 bbobj->generation++;
2103 backing_object->backing_object = NULL;
2105 object->backing_object = bbobj;
2107 LIST_INSERT_HEAD(&bbobj->shadow_head,
2108 object, shadow_list);
2109 bbobj->shadow_count++;
2110 bbobj->generation++;
2113 object->backing_object_offset +=
2114 backing_object->backing_object_offset;
2116 vm_object_drop(bbobj);
2119 * Discard the old backing_object. Nothing should be
2120 * able to ref it, other than a vm_map_split(),
2121 * and vm_map_split() will stall on our chain lock.
2122 * And we control the parent so it shouldn't be
2123 * possible for it to go away either.
2125 * Since the backing object has no pages, no pager
2126 * left, and no object references within it, all
2127 * that is necessary is to dispose of it.
2129 KASSERT(backing_object->ref_count == 1,
2130 ("backing_object %p was somehow "
2131 "re-referenced during collapse!",
2133 KASSERT(RB_EMPTY(&backing_object->rb_memq),
2134 ("backing_object %p somehow has left "
2135 "over pages during collapse!",
2139 * The object can be destroyed.
2141 * XXX just fall through and dodealloc instead
2142 * of forcing destruction?
2144 --backing_object->ref_count;
2145 if ((backing_object->flags & OBJ_DEAD) == 0)
2146 vm_object_terminate(backing_object);
2151 * If we do not entirely shadow the backing object,
2152 * there is nothing we can do so we give up.
2154 if (vm_object_backing_scan(object, backing_object,
2155 OBSC_TEST_ALL_SHADOWED) == 0) {
2160 * bbobj is backing_object->backing_object. Since
2161 * object completely shadows backing_object we can
2162 * bypass it and become backed by bbobj instead.
2164 while ((bbobj = backing_object->backing_object) != NULL) {
2165 vm_object_hold(bbobj);
2166 if (bbobj == backing_object->backing_object)
2168 vm_object_drop(bbobj);
2172 * Make object shadow bbobj instead of backing_object.
2173 * Remove object from backing_object's shadow list.
2175 * Deallocating backing_object will not remove
2176 * it, since its reference count is at least 2.
2178 KKASSERT(object->backing_object == backing_object);
2179 LIST_REMOVE(object, shadow_list);
2180 backing_object->shadow_count--;
2181 backing_object->generation++;
2184 * Add a ref to bbobj, bbobj now shadows object.
2186 * NOTE: backing_object->backing_object still points
2187 * to bbobj. That relationship remains intact
2188 * because backing_object has > 1 ref, so
2189 * someone else is pointing to it (hence why
2190 * we can't collapse it into object and can
2191 * only handle the all-shadowed bypass case).
2194 vm_object_chain_wait(bbobj);
2195 vm_object_reference_locked(bbobj);
2196 LIST_INSERT_HEAD(&bbobj->shadow_head,
2197 object, shadow_list);
2198 bbobj->shadow_count++;
2199 bbobj->generation++;
2200 object->backing_object_offset +=
2201 backing_object->backing_object_offset;
2202 object->backing_object = bbobj;
2203 vm_object_drop(bbobj);
2205 object->backing_object = NULL;
2209 * Drop the reference count on backing_object. To
2210 * handle ref_count races properly we can't assume
2211 * that the ref_count is still at least 2 so we
2212 * have to actually call vm_object_deallocate()
2213 * (after clearing the chainlock).
2220 * Ok, we want to loop on the new object->bbobj association,
2221 * possibly collapsing it further. However if dodealloc is
2222 * non-zero we have to deallocate the backing_object which
2223 * itself can potentially undergo a collapse, creating a
2224 * recursion depth issue with the LWKT token subsystem.
2226 * In the case where we must deallocate the backing_object
2227 * it is possible now that the backing_object has a single
2228 * shadow count on some other object (not represented here
2229 * as yet), since it no longer shadows us. Thus when we
2230 * call vm_object_deallocate() it may attempt to collapse
2231 * itself into its remaining parent.
2234 struct vm_object_dealloc_list *dtmp;
2236 vm_object_chain_release(backing_object);
2237 vm_object_unlock(backing_object);
2238 /* backing_object remains held */
2241 * Auto-deallocation list for caller convenience.
2246 dtmp = kmalloc(sizeof(*dtmp), M_TEMP, M_WAITOK);
2247 dtmp->object = backing_object;
2248 dtmp->next = *dlistp;
2251 vm_object_chain_release(backing_object);
2252 vm_object_drop(backing_object);
2254 /* backing_object = NULL; not needed */
2259 * Clean up any left over backing_object
2261 if (backing_object) {
2262 vm_object_chain_release(backing_object);
2263 vm_object_drop(backing_object);
2267 * Clean up any auto-deallocation list. This is a convenience
2268 * for top-level callers so they don't have to pass &dlist.
2269 * Do not clean up any caller-passed dlistp, the caller will
2273 vm_object_deallocate_list(&dlist);
2278 * vm_object_collapse() may collect additional objects in need of
2279 * deallocation. This routine deallocates these objects. The
2280 * deallocation itself can trigger additional collapses (which the
2281 * deallocate function takes care of). This procedure is used to
2282 * reduce procedural recursion since these vm_object shadow chains
2283 * can become quite long.
2286 vm_object_deallocate_list(struct vm_object_dealloc_list **dlistp)
2288 struct vm_object_dealloc_list *dlist;
2290 while ((dlist = *dlistp) != NULL) {
2291 *dlistp = dlist->next;
2292 vm_object_lock(dlist->object);
2293 vm_object_deallocate_locked(dlist->object);
2294 vm_object_drop(dlist->object);
2295 kfree(dlist, M_TEMP);
2300 * Removes all physical pages in the specified object range from the
2301 * object's list of pages.
2305 static int vm_object_page_remove_callback(vm_page_t p, void *data);
2308 vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
2309 boolean_t clean_only)
2311 struct rb_vm_page_scan_info info;
2315 * Degenerate cases and assertions
2317 vm_object_hold(object);
2318 if (object == NULL ||
2319 (object->resident_page_count == 0 && object->swblock_count == 0)) {
2320 vm_object_drop(object);
2323 KASSERT(object->type != OBJT_PHYS,
2324 ("attempt to remove pages from a physical object"));
2327 * Indicate that paging is occuring on the object
2329 vm_object_pip_add(object, 1);
2332 * Figure out the actual removal range and whether we are removing
2333 * the entire contents of the object or not. If removing the entire
2334 * contents, be sure to get all pages, even those that might be
2335 * beyond the end of the object.
2337 info.start_pindex = start;
2339 info.end_pindex = (vm_pindex_t)-1;
2341 info.end_pindex = end - 1;
2342 info.limit = clean_only;
2343 all = (start == 0 && info.end_pindex >= object->size - 1);
2346 * Loop until we are sure we have gotten them all.
2350 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
2351 vm_object_page_remove_callback, &info);
2352 } while (info.error);
2355 * Remove any related swap if throwing away pages, or for
2356 * non-swap objects (the swap is a clean copy in that case).
2358 if (object->type != OBJT_SWAP || clean_only == FALSE) {
2360 swap_pager_freespace_all(object);
2362 swap_pager_freespace(object, info.start_pindex,
2363 info.end_pindex - info.start_pindex + 1);
2369 vm_object_pip_wakeup(object);
2370 vm_object_drop(object);
2374 * The caller must hold the object
2377 vm_object_page_remove_callback(vm_page_t p, void *data)
2379 struct rb_vm_page_scan_info *info = data;
2381 if (vm_page_busy_try(p, TRUE)) {
2382 vm_page_sleep_busy(p, TRUE, "vmopar");
2388 * Wired pages cannot be destroyed, but they can be invalidated
2389 * and we do so if clean_only (limit) is not set.
2391 * WARNING! The page may be wired due to being part of a buffer
2392 * cache buffer, and the buffer might be marked B_CACHE.
2393 * This is fine as part of a truncation but VFSs must be
2394 * sure to fix the buffer up when re-extending the file.
2396 * NOTE! PG_NEED_COMMIT is ignored.
2398 if (p->wire_count != 0) {
2399 vm_page_protect(p, VM_PROT_NONE);
2400 if (info->limit == 0)
2407 * limit is our clean_only flag. If set and the page is dirty or
2408 * requires a commit, do not free it. If set and the page is being
2409 * held by someone, do not free it.
2411 if (info->limit && p->valid) {
2412 vm_page_test_dirty(p);
2413 if ((p->valid & p->dirty) || (p->flags & PG_NEED_COMMIT)) {
2418 if (p->hold_count) {
2428 vm_page_protect(p, VM_PROT_NONE);
2434 * Coalesces two objects backing up adjoining regions of memory into a
2437 * returns TRUE if objects were combined.
2439 * NOTE: Only works at the moment if the second object is NULL -
2440 * if it's not, which object do we lock first?
2443 * prev_object First object to coalesce
2444 * prev_offset Offset into prev_object
2445 * next_object Second object into coalesce
2446 * next_offset Offset into next_object
2448 * prev_size Size of reference to prev_object
2449 * next_size Size of reference to next_object
2451 * The caller does not need to hold (prev_object) but must have a stable
2452 * pointer to it (typically by holding the vm_map locked).
2455 vm_object_coalesce(vm_object_t prev_object, vm_pindex_t prev_pindex,
2456 vm_size_t prev_size, vm_size_t next_size)
2458 vm_pindex_t next_pindex;
2460 if (prev_object == NULL)
2463 vm_object_hold(prev_object);
2465 if (prev_object->type != OBJT_DEFAULT &&
2466 prev_object->type != OBJT_SWAP) {
2467 vm_object_drop(prev_object);
2472 * Try to collapse the object first
2474 vm_object_chain_acquire(prev_object);
2475 vm_object_collapse(prev_object, NULL);
2478 * Can't coalesce if: . more than one reference . paged out . shadows
2479 * another object . has a copy elsewhere (any of which mean that the
2480 * pages not mapped to prev_entry may be in use anyway)
2483 if (prev_object->backing_object != NULL) {
2484 vm_object_chain_release(prev_object);
2485 vm_object_drop(prev_object);
2489 prev_size >>= PAGE_SHIFT;
2490 next_size >>= PAGE_SHIFT;
2491 next_pindex = prev_pindex + prev_size;
2493 if ((prev_object->ref_count > 1) &&
2494 (prev_object->size != next_pindex)) {
2495 vm_object_chain_release(prev_object);
2496 vm_object_drop(prev_object);
2501 * Remove any pages that may still be in the object from a previous
2504 if (next_pindex < prev_object->size) {
2505 vm_object_page_remove(prev_object,
2507 next_pindex + next_size, FALSE);
2508 if (prev_object->type == OBJT_SWAP)
2509 swap_pager_freespace(prev_object,
2510 next_pindex, next_size);
2514 * Extend the object if necessary.
2516 if (next_pindex + next_size > prev_object->size)
2517 prev_object->size = next_pindex + next_size;
2519 vm_object_chain_release(prev_object);
2520 vm_object_drop(prev_object);
2525 * Make the object writable and flag is being possibly dirty.
2527 * The caller must hold the object. XXX called from vm_page_dirty(),
2528 * There is currently no requirement to hold the object.
2531 vm_object_set_writeable_dirty(vm_object_t object)
2535 /*vm_object_assert_held(object);*/
2537 * Avoid contention in vm fault path by checking the state before
2538 * issuing an atomic op on it.
2540 if ((object->flags & (OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY)) !=
2541 (OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY)) {
2542 vm_object_set_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
2544 if (object->type == OBJT_VNODE &&
2545 (vp = (struct vnode *)object->handle) != NULL) {
2546 if ((vp->v_flag & VOBJDIRTY) == 0) {
2547 vsetflags(vp, VOBJDIRTY);
2552 #include "opt_ddb.h"
2554 #include <sys/kernel.h>
2556 #include <sys/cons.h>
2558 #include <ddb/ddb.h>
2560 static int _vm_object_in_map (vm_map_t map, vm_object_t object,
2561 vm_map_entry_t entry);
2562 static int vm_object_in_map (vm_object_t object);
2565 * The caller must hold the object.
2568 _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
2571 vm_map_entry_t tmpe;
2572 vm_object_t obj, nobj;
2578 tmpe = map->header.next;
2579 entcount = map->nentries;
2580 while (entcount-- && (tmpe != &map->header)) {
2581 if( _vm_object_in_map(map, object, tmpe)) {
2588 switch(entry->maptype) {
2589 case VM_MAPTYPE_SUBMAP:
2590 tmpm = entry->object.sub_map;
2591 tmpe = tmpm->header.next;
2592 entcount = tmpm->nentries;
2593 while (entcount-- && tmpe != &tmpm->header) {
2594 if( _vm_object_in_map(tmpm, object, tmpe)) {
2600 case VM_MAPTYPE_NORMAL:
2601 case VM_MAPTYPE_VPAGETABLE:
2602 obj = entry->object.vm_object;
2604 if (obj == object) {
2605 if (obj != entry->object.vm_object)
2606 vm_object_drop(obj);
2609 while ((nobj = obj->backing_object) != NULL) {
2610 vm_object_hold(nobj);
2611 if (nobj == obj->backing_object)
2613 vm_object_drop(nobj);
2615 if (obj != entry->object.vm_object) {
2617 vm_object_lock_swap();
2618 vm_object_drop(obj);
2629 static int vm_object_in_map_callback(struct proc *p, void *data);
2631 struct vm_object_in_map_info {
2640 vm_object_in_map(vm_object_t object)
2642 struct vm_object_in_map_info info;
2645 info.object = object;
2647 allproc_scan(vm_object_in_map_callback, &info);
2650 if( _vm_object_in_map(&kernel_map, object, 0))
2652 if( _vm_object_in_map(&pager_map, object, 0))
2654 if( _vm_object_in_map(&buffer_map, object, 0))
2663 vm_object_in_map_callback(struct proc *p, void *data)
2665 struct vm_object_in_map_info *info = data;
2668 if (_vm_object_in_map(&p->p_vmspace->vm_map, info->object, 0)) {
2676 DB_SHOW_COMMAND(vmochk, vm_object_check)
2681 * make sure that internal objs are in a map somewhere
2682 * and none have zero ref counts.
2684 for (object = TAILQ_FIRST(&vm_object_list);
2686 object = TAILQ_NEXT(object, object_list)) {
2687 if (object->type == OBJT_MARKER)
2689 if (object->handle == NULL &&
2690 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2691 if (object->ref_count == 0) {
2692 db_printf("vmochk: internal obj has zero ref count: %ld\n",
2693 (long)object->size);
2695 if (!vm_object_in_map(object)) {
2697 "vmochk: internal obj is not in a map: "
2698 "ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2699 object->ref_count, (u_long)object->size,
2700 (u_long)object->size,
2701 (void *)object->backing_object);
2710 DB_SHOW_COMMAND(object, vm_object_print_static)
2712 /* XXX convert args. */
2713 vm_object_t object = (vm_object_t)addr;
2714 boolean_t full = have_addr;
2718 /* XXX count is an (unused) arg. Avoid shadowing it. */
2719 #define count was_count
2727 "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n",
2728 object, (int)object->type, (u_long)object->size,
2729 object->resident_page_count, object->ref_count, object->flags);
2731 * XXX no %qd in kernel. Truncate object->backing_object_offset.
2733 db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%lx\n",
2734 object->shadow_count,
2735 object->backing_object ? object->backing_object->ref_count : 0,
2736 object->backing_object, (long)object->backing_object_offset);
2743 RB_FOREACH(p, vm_page_rb_tree, &object->rb_memq) {
2745 db_iprintf("memory:=");
2746 else if (count == 6) {
2754 db_printf("(off=0x%lx,page=0x%lx)",
2755 (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
2766 * XXX need this non-static entry for calling from vm_map_print.
2771 vm_object_print(/* db_expr_t */ long addr,
2772 boolean_t have_addr,
2773 /* db_expr_t */ long count,
2776 vm_object_print_static(addr, have_addr, count, modif);
2782 DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
2787 for (object = TAILQ_FIRST(&vm_object_list);
2789 object = TAILQ_NEXT(object, object_list)) {
2790 vm_pindex_t idx, fidx;
2792 vm_paddr_t pa = -1, padiff;
2796 if (object->type == OBJT_MARKER)
2798 db_printf("new object: %p\n", (void *)object);
2808 osize = object->size;
2811 for (idx = 0; idx < osize; idx++) {
2812 m = vm_page_lookup(object, idx);
2815 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2816 (long)fidx, rcount, (long)pa);
2831 (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
2836 padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
2837 padiff >>= PAGE_SHIFT;
2838 padiff &= PQ_L2_MASK;
2840 pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
2844 db_printf(" index(%ld)run(%d)pa(0x%lx)",
2845 (long)fidx, rcount, (long)pa);
2846 db_printf("pd(%ld)\n", (long)padiff);
2856 pa = VM_PAGE_TO_PHYS(m);
2860 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2861 (long)fidx, rcount, (long)pa);