2 * Copyright (c) 1991, 1993, 2013
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. 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 #include <vm/vm_page2.h>
94 #include <machine/specialreg.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 vm_object kernel_object;
133 static long vm_object_count;
135 static long object_collapses;
136 static long object_bypasses;
137 static int next_index;
138 static vm_zone_t obj_zone;
139 static struct vm_zone obj_zone_store;
140 #define VM_OBJECTS_INIT 256
141 static struct vm_object vm_objects_init[VM_OBJECTS_INIT];
143 struct object_q vm_object_lists[VMOBJ_HSIZE];
144 struct lwkt_token vmobj_tokens[VMOBJ_HSIZE];
147 * Misc low level routines
150 vm_object_lock_init(vm_object_t obj)
152 #if defined(DEBUG_LOCKS)
155 obj->debug_hold_bitmap = 0;
156 obj->debug_hold_ovfl = 0;
157 for (i = 0; i < VMOBJ_DEBUG_ARRAY_SIZE; i++) {
158 obj->debug_hold_thrs[i] = NULL;
159 obj->debug_hold_file[i] = NULL;
160 obj->debug_hold_line[i] = 0;
166 vm_object_lock_swap(void)
172 vm_object_lock(vm_object_t obj)
174 lwkt_gettoken(&obj->token);
178 * Returns TRUE on sucesss
181 vm_object_lock_try(vm_object_t obj)
183 return(lwkt_trytoken(&obj->token));
187 vm_object_lock_shared(vm_object_t obj)
189 lwkt_gettoken_shared(&obj->token);
193 vm_object_unlock(vm_object_t obj)
195 lwkt_reltoken(&obj->token);
199 vm_object_upgrade(vm_object_t obj)
201 lwkt_reltoken(&obj->token);
202 lwkt_gettoken(&obj->token);
206 vm_object_downgrade(vm_object_t obj)
208 lwkt_reltoken(&obj->token);
209 lwkt_gettoken_shared(&obj->token);
213 vm_object_assert_held(vm_object_t obj)
215 ASSERT_LWKT_TOKEN_HELD(&obj->token);
220 vm_object_hold(vm_object_t obj)
222 debugvm_object_hold(vm_object_t obj, char *file, int line)
225 KKASSERT(obj != NULL);
228 * Object must be held (object allocation is stable due to callers
229 * context, typically already holding the token on a parent object)
230 * prior to potentially blocking on the lock, otherwise the object
231 * can get ripped away from us.
233 refcount_acquire(&obj->hold_count);
236 #if defined(DEBUG_LOCKS)
241 mask = ~obj->debug_hold_bitmap;
243 if (mask == 0xFFFFFFFFU) {
244 if (obj->debug_hold_ovfl == 0)
245 obj->debug_hold_ovfl = 1;
249 if (atomic_cmpset_int(&obj->debug_hold_bitmap, ~mask,
251 obj->debug_hold_bitmap |= (1 << i);
252 obj->debug_hold_thrs[i] = curthread;
253 obj->debug_hold_file[i] = file;
254 obj->debug_hold_line[i] = line;
263 vm_object_hold_try(vm_object_t obj)
265 debugvm_object_hold_try(vm_object_t obj, char *file, int line)
268 KKASSERT(obj != NULL);
271 * Object must be held (object allocation is stable due to callers
272 * context, typically already holding the token on a parent object)
273 * prior to potentially blocking on the lock, otherwise the object
274 * can get ripped away from us.
276 refcount_acquire(&obj->hold_count);
277 if (vm_object_lock_try(obj) == 0) {
278 if (refcount_release(&obj->hold_count)) {
279 if (obj->ref_count == 0 && (obj->flags & OBJ_DEAD))
280 zfree(obj_zone, obj);
285 #if defined(DEBUG_LOCKS)
290 mask = ~obj->debug_hold_bitmap;
292 if (mask == 0xFFFFFFFFU) {
293 if (obj->debug_hold_ovfl == 0)
294 obj->debug_hold_ovfl = 1;
298 if (atomic_cmpset_int(&obj->debug_hold_bitmap, ~mask,
300 obj->debug_hold_bitmap |= (1 << i);
301 obj->debug_hold_thrs[i] = curthread;
302 obj->debug_hold_file[i] = file;
303 obj->debug_hold_line[i] = line;
313 vm_object_hold_shared(vm_object_t obj)
315 debugvm_object_hold_shared(vm_object_t obj, char *file, int line)
318 KKASSERT(obj != NULL);
321 * Object must be held (object allocation is stable due to callers
322 * context, typically already holding the token on a parent object)
323 * prior to potentially blocking on the lock, otherwise the object
324 * can get ripped away from us.
326 refcount_acquire(&obj->hold_count);
327 vm_object_lock_shared(obj);
329 #if defined(DEBUG_LOCKS)
334 mask = ~obj->debug_hold_bitmap;
336 if (mask == 0xFFFFFFFFU) {
337 if (obj->debug_hold_ovfl == 0)
338 obj->debug_hold_ovfl = 1;
342 if (atomic_cmpset_int(&obj->debug_hold_bitmap, ~mask,
344 obj->debug_hold_bitmap |= (1 << i);
345 obj->debug_hold_thrs[i] = curthread;
346 obj->debug_hold_file[i] = file;
347 obj->debug_hold_line[i] = line;
355 * Drop the token and hold_count on the object.
357 * WARNING! Token might be shared.
360 vm_object_drop(vm_object_t obj)
365 #if defined(DEBUG_LOCKS)
369 for (i = 0; i < VMOBJ_DEBUG_ARRAY_SIZE; i++) {
370 if ((obj->debug_hold_bitmap & (1 << i)) &&
371 (obj->debug_hold_thrs[i] == curthread)) {
372 obj->debug_hold_bitmap &= ~(1 << i);
373 obj->debug_hold_thrs[i] = NULL;
374 obj->debug_hold_file[i] = NULL;
375 obj->debug_hold_line[i] = 0;
381 if (found == 0 && obj->debug_hold_ovfl == 0)
382 panic("vm_object: attempt to drop hold on non-self-held obj");
386 * No new holders should be possible once we drop hold_count 1->0 as
387 * there is no longer any way to reference the object.
389 KKASSERT(obj->hold_count > 0);
390 if (refcount_release(&obj->hold_count)) {
391 if (obj->ref_count == 0 && (obj->flags & OBJ_DEAD)) {
392 vm_object_unlock(obj);
393 zfree(obj_zone, obj);
395 vm_object_unlock(obj);
398 vm_object_unlock(obj);
403 * Initialize a freshly allocated object, returning a held object.
405 * Used only by vm_object_allocate() and zinitna().
410 _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->memattr = VM_MEMATTR_DEFAULT;
423 object->hold_count = 0;
425 if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
426 vm_object_set_flag(object, OBJ_ONEMAPPING);
427 object->paging_in_progress = 0;
428 object->resident_page_count = 0;
429 object->agg_pv_list_count = 0;
430 object->shadow_count = 0;
431 /* cpu localization twist */
432 object->pg_color = (int)(intptr_t)curthread;
433 if ( size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
434 incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
437 next_index = (next_index + incr) & PQ_L2_MASK;
438 object->handle = NULL;
439 object->backing_object = NULL;
440 object->backing_object_offset = (vm_ooffset_t)0;
442 object->generation++;
443 object->swblock_count = 0;
444 RB_INIT(&object->swblock_root);
445 vm_object_lock_init(object);
446 pmap_object_init(object);
448 vm_object_hold(object);
450 n = VMOBJ_HASH(object);
451 atomic_add_long(&vm_object_count, 1);
452 lwkt_gettoken(&vmobj_tokens[n]);
453 TAILQ_INSERT_TAIL(&vm_object_lists[n], object, object_list);
454 lwkt_reltoken(&vmobj_tokens[n]);
458 * Initialize the VM objects module.
460 * Called from the low level boot code only.
467 for (i = 0; i < VMOBJ_HSIZE; ++i) {
468 TAILQ_INIT(&vm_object_lists[i]);
469 lwkt_token_init(&vmobj_tokens[i], "vmobjlst");
472 _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(KvaEnd),
474 vm_object_drop(&kernel_object);
476 obj_zone = &obj_zone_store;
477 zbootinit(obj_zone, "VM OBJECT", sizeof (struct vm_object),
478 vm_objects_init, VM_OBJECTS_INIT);
482 vm_object_init2(void)
484 zinitna(obj_zone, NULL, NULL, 0, 0, ZONE_PANICFAIL, 1);
488 * Allocate and return a new object of the specified type and size.
493 vm_object_allocate(objtype_t type, vm_pindex_t size)
497 result = (vm_object_t) zalloc(obj_zone);
499 _vm_object_allocate(type, size, result);
500 vm_object_drop(result);
506 * This version returns a held object, allowing further atomic initialization
510 vm_object_allocate_hold(objtype_t type, vm_pindex_t size)
514 result = (vm_object_t) zalloc(obj_zone);
516 _vm_object_allocate(type, size, result);
522 * Add an additional reference to a vm_object. The object must already be
523 * held. The original non-lock version is no longer supported. The object
524 * must NOT be chain locked by anyone at the time the reference is added.
526 * Referencing a chain-locked object can blow up the fairly sensitive
527 * ref_count and shadow_count tests in the deallocator. Most callers
528 * will call vm_object_chain_wait() prior to calling
529 * vm_object_reference_locked() to avoid the case.
531 * The object must be held, but may be held shared if desired (hence why
532 * we use an atomic op).
535 vm_object_reference_locked(vm_object_t object)
537 KKASSERT(object != NULL);
538 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
539 KKASSERT((object->chainlk & (CHAINLK_EXCL | CHAINLK_MASK)) == 0);
540 atomic_add_int(&object->ref_count, 1);
541 if (object->type == OBJT_VNODE) {
542 vref(object->handle);
543 /* XXX what if the vnode is being destroyed? */
548 * This version is only allowed for vnode objects.
551 vm_object_reference_quick(vm_object_t object)
553 KKASSERT(object->type == OBJT_VNODE);
554 atomic_add_int(&object->ref_count, 1);
555 vref(object->handle);
559 * Object OBJ_CHAINLOCK lock handling.
561 * The caller can chain-lock backing objects recursively and then
562 * use vm_object_chain_release_all() to undo the whole chain.
564 * Chain locks are used to prevent collapses and are only applicable
565 * to OBJT_DEFAULT and OBJT_SWAP objects. Chain locking operations
566 * on other object types are ignored. This is also important because
567 * it allows e.g. the vnode underlying a memory mapping to take concurrent
570 * The object must usually be held on entry, though intermediate
571 * objects need not be held on release. The object must be held exclusively,
572 * NOT shared. Note that the prefault path checks the shared state and
573 * avoids using the chain functions.
576 vm_object_chain_wait(vm_object_t object, int shared)
578 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
580 uint32_t chainlk = object->chainlk;
584 if (chainlk & (CHAINLK_EXCL | CHAINLK_EXCLREQ)) {
585 tsleep_interlock(object, 0);
586 if (atomic_cmpset_int(&object->chainlk,
588 chainlk | CHAINLK_WAIT)) {
589 tsleep(object, PINTERLOCKED,
598 if (chainlk & (CHAINLK_MASK | CHAINLK_EXCL)) {
599 tsleep_interlock(object, 0);
600 if (atomic_cmpset_int(&object->chainlk,
602 chainlk | CHAINLK_WAIT))
604 tsleep(object, PINTERLOCKED,
609 if (atomic_cmpset_int(&object->chainlk,
611 chainlk & ~CHAINLK_WAIT))
613 if (chainlk & CHAINLK_WAIT)
625 vm_object_chain_acquire(vm_object_t object, int shared)
627 if (object->type != OBJT_DEFAULT && object->type != OBJT_SWAP)
629 if (vm_shared_fault == 0)
633 uint32_t chainlk = object->chainlk;
637 if (chainlk & (CHAINLK_EXCL | CHAINLK_EXCLREQ)) {
638 tsleep_interlock(object, 0);
639 if (atomic_cmpset_int(&object->chainlk,
641 chainlk | CHAINLK_WAIT)) {
642 tsleep(object, PINTERLOCKED,
646 } else if (atomic_cmpset_int(&object->chainlk,
647 chainlk, chainlk + 1)) {
652 if (chainlk & (CHAINLK_MASK | CHAINLK_EXCL)) {
653 tsleep_interlock(object, 0);
654 if (atomic_cmpset_int(&object->chainlk,
659 tsleep(object, PINTERLOCKED,
664 if (atomic_cmpset_int(&object->chainlk,
666 (chainlk | CHAINLK_EXCL) &
669 if (chainlk & CHAINLK_WAIT)
681 vm_object_chain_release(vm_object_t object)
683 /*ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));*/
684 if (object->type != OBJT_DEFAULT && object->type != OBJT_SWAP)
686 KKASSERT(object->chainlk & (CHAINLK_MASK | CHAINLK_EXCL));
688 uint32_t chainlk = object->chainlk;
691 if (chainlk & CHAINLK_MASK) {
692 if ((chainlk & CHAINLK_MASK) == 1 &&
693 atomic_cmpset_int(&object->chainlk,
695 (chainlk - 1) & ~CHAINLK_WAIT)) {
696 if (chainlk & CHAINLK_WAIT)
700 if ((chainlk & CHAINLK_MASK) > 1 &&
701 atomic_cmpset_int(&object->chainlk,
702 chainlk, chainlk - 1)) {
707 KKASSERT(chainlk & CHAINLK_EXCL);
708 if (atomic_cmpset_int(&object->chainlk,
710 chainlk & ~(CHAINLK_EXCL |
712 if (chainlk & CHAINLK_WAIT)
721 * Release the chain from first_object through and including stopobj.
722 * The caller is typically holding the first and last object locked
723 * (shared or exclusive) to prevent destruction races.
725 * We release stopobj first as an optimization as this object is most
726 * likely to be shared across multiple processes.
729 vm_object_chain_release_all(vm_object_t first_object, vm_object_t stopobj)
731 vm_object_t backing_object;
734 vm_object_chain_release(stopobj);
735 object = first_object;
737 while (object != stopobj) {
739 backing_object = object->backing_object;
740 vm_object_chain_release(object);
741 object = backing_object;
746 * Dereference an object and its underlying vnode. The object may be
747 * held shared. On return the object will remain held.
749 * This function may return a vnode in *vpp which the caller must release
750 * after the caller drops its own lock. If vpp is NULL, we assume that
751 * the caller was holding an exclusive lock on the object and we vrele()
755 vm_object_vndeallocate(vm_object_t object, struct vnode **vpp)
757 struct vnode *vp = (struct vnode *) object->handle;
759 KASSERT(object->type == OBJT_VNODE,
760 ("vm_object_vndeallocate: not a vnode object"));
761 KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
762 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
764 if (object->ref_count == 0) {
765 vprint("vm_object_vndeallocate", vp);
766 panic("vm_object_vndeallocate: bad object reference count");
770 int count = object->ref_count;
773 vm_object_upgrade(object);
774 if (atomic_cmpset_int(&object->ref_count, count, 0)) {
775 vclrflags(vp, VTEXT);
779 if (atomic_cmpset_int(&object->ref_count,
788 * vrele or return the vp to vrele. We can only safely vrele(vp)
789 * if the object was locked exclusively. But there are two races
792 * We had to upgrade the object above to safely clear VTEXT
793 * but the alternative path where the shared lock is retained
794 * can STILL race to 0 in other paths and cause our own vrele()
795 * to terminate the vnode. We can't allow that if the VM object
796 * is still locked shared.
805 * Release a reference to the specified object, gained either through a
806 * vm_object_allocate or a vm_object_reference call. When all references
807 * are gone, storage associated with this object may be relinquished.
809 * The caller does not have to hold the object locked but must have control
810 * over the reference in question in order to guarantee that the object
811 * does not get ripped out from under us.
813 * XXX Currently all deallocations require an exclusive lock.
816 vm_object_deallocate(vm_object_t object)
824 count = object->ref_count;
828 * If decrementing the count enters into special handling
829 * territory (0, 1, or 2) we have to do it the hard way.
830 * Fortunate though, objects with only a few refs like this
831 * are not likely to be heavily contended anyway.
833 * For vnode objects we only care about 1->0 transitions.
835 if (count <= 3 || (object->type == OBJT_VNODE && count <= 1)) {
836 vm_object_hold(object);
837 vm_object_deallocate_locked(object);
838 vm_object_drop(object);
843 * Try to decrement ref_count without acquiring a hold on
844 * the object. This is particularly important for the exec*()
845 * and exit*() code paths because the program binary may
846 * have a great deal of sharing and an exclusive lock will
847 * crowbar performance in those circumstances.
849 if (object->type == OBJT_VNODE) {
850 vp = (struct vnode *)object->handle;
851 if (atomic_cmpset_int(&object->ref_count,
858 if (atomic_cmpset_int(&object->ref_count,
869 vm_object_deallocate_locked(vm_object_t object)
871 struct vm_object_dealloc_list *dlist = NULL;
872 struct vm_object_dealloc_list *dtmp;
877 * We may chain deallocate object, but additional objects may
878 * collect on the dlist which also have to be deallocated. We
879 * must avoid a recursion, vm_object chains can get deep.
883 while (object != NULL) {
885 * vnode case, caller either locked the object exclusively
886 * or this is a recursion with must_drop != 0 and the vnode
887 * object will be locked shared.
889 * If locked shared we have to drop the object before we can
890 * call vrele() or risk a shared/exclusive livelock.
892 if (object->type == OBJT_VNODE) {
893 ASSERT_LWKT_TOKEN_HELD(&object->token);
895 struct vnode *tmp_vp;
897 vm_object_vndeallocate(object, &tmp_vp);
898 vm_object_drop(object);
903 vm_object_vndeallocate(object, NULL);
907 ASSERT_LWKT_TOKEN_HELD_EXCL(&object->token);
910 * Normal case (object is locked exclusively)
912 if (object->ref_count == 0) {
913 panic("vm_object_deallocate: object deallocated "
914 "too many times: %d", object->type);
916 if (object->ref_count > 2) {
917 atomic_add_int(&object->ref_count, -1);
922 * Here on ref_count of one or two, which are special cases for
925 * Nominal ref_count > 1 case if the second ref is not from
928 * (ONEMAPPING only applies to DEFAULT AND SWAP objects)
930 if (object->ref_count == 2 && object->shadow_count == 0) {
931 if (object->type == OBJT_DEFAULT ||
932 object->type == OBJT_SWAP) {
933 vm_object_set_flag(object, OBJ_ONEMAPPING);
935 atomic_add_int(&object->ref_count, -1);
940 * If the second ref is from a shadow we chain along it
941 * upwards if object's handle is exhausted.
943 * We have to decrement object->ref_count before potentially
944 * collapsing the first shadow object or the collapse code
945 * will not be able to handle the degenerate case to remove
946 * object. However, if we do it too early the object can
947 * get ripped out from under us.
949 if (object->ref_count == 2 && object->shadow_count == 1 &&
950 object->handle == NULL && (object->type == OBJT_DEFAULT ||
951 object->type == OBJT_SWAP)) {
952 temp = LIST_FIRST(&object->shadow_head);
953 KKASSERT(temp != NULL);
954 vm_object_hold(temp);
957 * Wait for any paging to complete so the collapse
958 * doesn't (or isn't likely to) qcollapse. pip
959 * waiting must occur before we acquire the
963 temp->paging_in_progress ||
964 object->paging_in_progress
966 vm_object_pip_wait(temp, "objde1");
967 vm_object_pip_wait(object, "objde2");
971 * If the parent is locked we have to give up, as
972 * otherwise we would be acquiring locks in the
973 * wrong order and potentially deadlock.
975 if (temp->chainlk & (CHAINLK_EXCL | CHAINLK_MASK)) {
976 vm_object_drop(temp);
979 vm_object_chain_acquire(temp, 0);
982 * Recheck/retry after the hold and the paging
983 * wait, both of which can block us.
985 if (object->ref_count != 2 ||
986 object->shadow_count != 1 ||
988 LIST_FIRST(&object->shadow_head) != temp ||
989 (object->type != OBJT_DEFAULT &&
990 object->type != OBJT_SWAP)) {
991 vm_object_chain_release(temp);
992 vm_object_drop(temp);
997 * We can safely drop object's ref_count now.
999 KKASSERT(object->ref_count == 2);
1000 atomic_add_int(&object->ref_count, -1);
1003 * If our single parent is not collapseable just
1004 * decrement ref_count (2->1) and stop.
1006 if (temp->handle || (temp->type != OBJT_DEFAULT &&
1007 temp->type != OBJT_SWAP)) {
1008 vm_object_chain_release(temp);
1009 vm_object_drop(temp);
1014 * At this point we have already dropped object's
1015 * ref_count so it is possible for a race to
1016 * deallocate obj out from under us. Any collapse
1017 * will re-check the situation. We must not block
1018 * until we are able to collapse.
1020 * Bump temp's ref_count to avoid an unwanted
1021 * degenerate recursion (can't call
1022 * vm_object_reference_locked() because it asserts
1023 * that CHAINLOCK is not set).
1025 atomic_add_int(&temp->ref_count, 1);
1026 KKASSERT(temp->ref_count > 1);
1029 * Collapse temp, then deallocate the extra ref
1032 vm_object_collapse(temp, &dlist);
1033 vm_object_chain_release(temp);
1035 vm_object_lock_swap();
1036 vm_object_drop(object);
1044 * Drop the ref and handle termination on the 1->0 transition.
1045 * We may have blocked above so we have to recheck.
1048 KKASSERT(object->ref_count != 0);
1049 if (object->ref_count >= 2) {
1050 atomic_add_int(&object->ref_count, -1);
1053 KKASSERT(object->ref_count == 1);
1056 * 1->0 transition. Chain through the backing_object.
1057 * Maintain the ref until we've located the backing object,
1060 while ((temp = object->backing_object) != NULL) {
1061 if (temp->type == OBJT_VNODE)
1062 vm_object_hold_shared(temp);
1064 vm_object_hold(temp);
1065 if (temp == object->backing_object)
1067 vm_object_drop(temp);
1071 * 1->0 transition verified, retry if ref_count is no longer
1072 * 1. Otherwise disconnect the backing_object (temp) and
1075 if (object->ref_count != 1) {
1076 vm_object_drop(temp);
1081 * It shouldn't be possible for the object to be chain locked
1082 * if we're removing the last ref on it.
1084 KKASSERT((object->chainlk & (CHAINLK_EXCL|CHAINLK_MASK)) == 0);
1087 if (object->flags & OBJ_ONSHADOW) {
1088 LIST_REMOVE(object, shadow_list);
1089 temp->shadow_count--;
1091 vm_object_clear_flag(object, OBJ_ONSHADOW);
1093 object->backing_object = NULL;
1096 atomic_add_int(&object->ref_count, -1);
1097 if ((object->flags & OBJ_DEAD) == 0)
1098 vm_object_terminate(object);
1099 if (must_drop && temp)
1100 vm_object_lock_swap();
1102 vm_object_drop(object);
1107 if (must_drop && object)
1108 vm_object_drop(object);
1111 * Additional tail recursion on dlist. Avoid a recursion. Objects
1112 * on the dlist have a hold count but are not locked.
1114 if ((dtmp = dlist) != NULL) {
1116 object = dtmp->object;
1117 kfree(dtmp, M_TEMP);
1119 vm_object_lock(object); /* already held, add lock */
1120 must_drop = 1; /* and we're responsible for it */
1126 * Destroy the specified object, freeing up related resources.
1128 * The object must have zero references.
1130 * The object must held. The caller is responsible for dropping the object
1131 * after terminate returns. Terminate does NOT drop the object.
1133 static int vm_object_terminate_callback(vm_page_t p, void *data);
1136 vm_object_terminate(vm_object_t object)
1141 * Make sure no one uses us. Once we set OBJ_DEAD we should be
1142 * able to safely block.
1144 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
1145 KKASSERT((object->flags & OBJ_DEAD) == 0);
1146 vm_object_set_flag(object, OBJ_DEAD);
1149 * Wait for the pageout daemon to be done with the object
1151 vm_object_pip_wait(object, "objtrm1");
1153 KASSERT(!object->paging_in_progress,
1154 ("vm_object_terminate: pageout in progress"));
1157 * Clean and free the pages, as appropriate. All references to the
1158 * object are gone, so we don't need to lock it.
1160 if (object->type == OBJT_VNODE) {
1164 * Clean pages and flush buffers.
1166 * NOTE! TMPFS buffer flushes do not typically flush the
1167 * actual page to swap as this would be highly
1168 * inefficient, and normal filesystems usually wrap
1169 * page flushes with buffer cache buffers.
1171 * To deal with this we have to call vinvalbuf() both
1172 * before and after the vm_object_page_clean().
1174 vp = (struct vnode *) object->handle;
1175 vinvalbuf(vp, V_SAVE, 0, 0);
1176 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
1177 vinvalbuf(vp, V_SAVE, 0, 0);
1181 * Wait for any I/O to complete, after which there had better not
1182 * be any references left on the object.
1184 vm_object_pip_wait(object, "objtrm2");
1186 if (object->ref_count != 0) {
1187 panic("vm_object_terminate: object with references, "
1188 "ref_count=%d", object->ref_count);
1192 * Cleanup any shared pmaps associated with this object.
1194 pmap_object_free(object);
1197 * Now free any remaining pages. For internal objects, this also
1198 * removes them from paging queues. Don't free wired pages, just
1199 * remove them from the object.
1201 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
1202 vm_object_terminate_callback, NULL);
1205 * Let the pager know object is dead.
1207 vm_pager_deallocate(object);
1210 * Wait for the object hold count to hit 1, clean out pages as
1211 * we go. vmobj_token interlocks any race conditions that might
1212 * pick the object up from the vm_object_list after we have cleared
1216 if (RB_ROOT(&object->rb_memq) == NULL)
1218 kprintf("vm_object_terminate: Warning, object %p "
1219 "still has %d pages\n",
1220 object, object->resident_page_count);
1221 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
1222 vm_object_terminate_callback, NULL);
1226 * There had better not be any pages left
1228 KKASSERT(object->resident_page_count == 0);
1231 * Remove the object from the global object list.
1233 n = VMOBJ_HASH(object);
1234 lwkt_gettoken(&vmobj_tokens[n]);
1235 TAILQ_REMOVE(&vm_object_lists[n], object, object_list);
1236 lwkt_reltoken(&vmobj_tokens[n]);
1237 atomic_add_long(&vm_object_count, -1);
1239 if (object->ref_count != 0) {
1240 panic("vm_object_terminate2: object with references, "
1241 "ref_count=%d", object->ref_count);
1245 * NOTE: The object hold_count is at least 1, so we cannot zfree()
1246 * the object here. See vm_object_drop().
1251 * The caller must hold the object.
1254 vm_object_terminate_callback(vm_page_t p, void *data __unused)
1259 vm_page_busy_wait(p, TRUE, "vmpgtrm");
1260 if (object != p->object) {
1261 kprintf("vm_object_terminate: Warning: Encountered "
1262 "busied page %p on queue %d\n", p, p->queue);
1264 } else if (p->wire_count == 0) {
1266 * NOTE: p->dirty and PG_NEED_COMMIT are ignored.
1269 mycpu->gd_cnt.v_pfree++;
1271 if (p->queue != PQ_NONE)
1272 kprintf("vm_object_terminate: Warning: Encountered "
1273 "wired page %p on queue %d\n", p, p->queue);
1282 * Clean all dirty pages in the specified range of object. Leaves page
1283 * on whatever queue it is currently on. If NOSYNC is set then do not
1284 * write out pages with PG_NOSYNC set (originally comes from MAP_NOSYNC),
1285 * leaving the object dirty.
1287 * When stuffing pages asynchronously, allow clustering. XXX we need a
1288 * synchronous clustering mode implementation.
1290 * Odd semantics: if start == end, we clean everything.
1292 * The object must be locked? XXX
1294 static int vm_object_page_clean_pass1(struct vm_page *p, void *data);
1295 static int vm_object_page_clean_pass2(struct vm_page *p, void *data);
1298 vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
1301 struct rb_vm_page_scan_info info;
1307 vm_object_hold(object);
1308 if (object->type != OBJT_VNODE ||
1309 (object->flags & OBJ_MIGHTBEDIRTY) == 0) {
1310 vm_object_drop(object);
1314 pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ?
1315 VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
1316 pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
1318 vp = object->handle;
1321 * Interlock other major object operations. This allows us to
1322 * temporarily clear OBJ_WRITEABLE and OBJ_MIGHTBEDIRTY.
1324 vm_object_set_flag(object, OBJ_CLEANING);
1327 * Handle 'entire object' case
1329 info.start_pindex = start;
1331 info.end_pindex = object->size - 1;
1333 info.end_pindex = end - 1;
1335 wholescan = (start == 0 && info.end_pindex == object->size - 1);
1337 info.pagerflags = pagerflags;
1338 info.object = object;
1341 * If cleaning the entire object do a pass to mark the pages read-only.
1342 * If everything worked out ok, clear OBJ_WRITEABLE and
1347 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
1348 vm_object_page_clean_pass1, &info);
1349 if (info.error == 0) {
1350 vm_object_clear_flag(object,
1351 OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
1352 if (object->type == OBJT_VNODE &&
1353 (vp = (struct vnode *)object->handle) != NULL) {
1355 * Use new-style interface to clear VISDIRTY
1356 * because the vnode is not necessarily removed
1357 * from the syncer list(s) as often as it was
1358 * under the old interface, which can leave
1359 * the vnode on the syncer list after reclaim.
1367 * Do a pass to clean all the dirty pages we find.
1371 generation = object->generation;
1372 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
1373 vm_object_page_clean_pass2, &info);
1374 } while (info.error || generation != object->generation);
1376 vm_object_clear_flag(object, OBJ_CLEANING);
1377 vm_object_drop(object);
1381 * The caller must hold the object.
1385 vm_object_page_clean_pass1(struct vm_page *p, void *data)
1387 struct rb_vm_page_scan_info *info = data;
1389 vm_page_flag_set(p, PG_CLEANCHK);
1390 if ((info->limit & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
1392 } else if (vm_page_busy_try(p, FALSE) == 0) {
1393 vm_page_protect(p, VM_PROT_READ); /* must not block */
1403 * The caller must hold the object
1407 vm_object_page_clean_pass2(struct vm_page *p, void *data)
1409 struct rb_vm_page_scan_info *info = data;
1413 * Do not mess with pages that were inserted after we started
1414 * the cleaning pass.
1416 if ((p->flags & PG_CLEANCHK) == 0)
1419 generation = info->object->generation;
1420 vm_page_busy_wait(p, TRUE, "vpcwai");
1421 if (p->object != info->object ||
1422 info->object->generation != generation) {
1429 * Before wasting time traversing the pmaps, check for trivial
1430 * cases where the page cannot be dirty.
1432 if (p->valid == 0 || (p->queue - p->pc) == PQ_CACHE) {
1433 KKASSERT((p->dirty & p->valid) == 0 &&
1434 (p->flags & PG_NEED_COMMIT) == 0);
1440 * Check whether the page is dirty or not. The page has been set
1441 * to be read-only so the check will not race a user dirtying the
1444 vm_page_test_dirty(p);
1445 if ((p->dirty & p->valid) == 0 && (p->flags & PG_NEED_COMMIT) == 0) {
1446 vm_page_flag_clear(p, PG_CLEANCHK);
1452 * If we have been asked to skip nosync pages and this is a
1453 * nosync page, skip it. Note that the object flags were
1454 * not cleared in this case (because pass1 will have returned an
1455 * error), so we do not have to set them.
1457 if ((info->limit & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
1458 vm_page_flag_clear(p, PG_CLEANCHK);
1464 * Flush as many pages as we can. PG_CLEANCHK will be cleared on
1465 * the pages that get successfully flushed. Set info->error if
1466 * we raced an object modification.
1468 vm_object_page_collect_flush(info->object, p, info->pagerflags);
1476 * Collect the specified page and nearby pages and flush them out.
1477 * The number of pages flushed is returned. The passed page is busied
1478 * by the caller and we are responsible for its disposition.
1480 * The caller must hold the object.
1483 vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int pagerflags)
1491 vm_page_t ma[BLIST_MAX_ALLOC];
1493 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
1496 page_base = pi % BLIST_MAX_ALLOC;
1504 tp = vm_page_lookup_busy_try(object, pi - page_base + ib,
1510 if ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
1511 (tp->flags & PG_CLEANCHK) == 0) {
1515 if ((tp->queue - tp->pc) == PQ_CACHE) {
1516 vm_page_flag_clear(tp, PG_CLEANCHK);
1520 vm_page_test_dirty(tp);
1521 if ((tp->dirty & tp->valid) == 0 &&
1522 (tp->flags & PG_NEED_COMMIT) == 0) {
1523 vm_page_flag_clear(tp, PG_CLEANCHK);
1532 while (is < BLIST_MAX_ALLOC &&
1533 pi - page_base + is < object->size) {
1536 tp = vm_page_lookup_busy_try(object, pi - page_base + is,
1542 if ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
1543 (tp->flags & PG_CLEANCHK) == 0) {
1547 if ((tp->queue - tp->pc) == PQ_CACHE) {
1548 vm_page_flag_clear(tp, PG_CLEANCHK);
1552 vm_page_test_dirty(tp);
1553 if ((tp->dirty & tp->valid) == 0 &&
1554 (tp->flags & PG_NEED_COMMIT) == 0) {
1555 vm_page_flag_clear(tp, PG_CLEANCHK);
1564 * All pages in the ma[] array are busied now
1566 for (i = ib; i < is; ++i) {
1567 vm_page_flag_clear(ma[i], PG_CLEANCHK);
1568 vm_page_hold(ma[i]); /* XXX need this any more? */
1570 vm_pageout_flush(&ma[ib], is - ib, pagerflags);
1571 for (i = ib; i < is; ++i) /* XXX need this any more? */
1572 vm_page_unhold(ma[i]);
1576 * Same as vm_object_pmap_copy, except range checking really
1577 * works, and is meant for small sections of an object.
1579 * This code protects resident pages by making them read-only
1580 * and is typically called on a fork or split when a page
1581 * is converted to copy-on-write.
1583 * NOTE: If the page is already at VM_PROT_NONE, calling
1584 * vm_page_protect will have no effect.
1587 vm_object_pmap_copy_1(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1592 if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
1595 vm_object_hold(object);
1596 for (idx = start; idx < end; idx++) {
1597 p = vm_page_lookup(object, idx);
1600 vm_page_protect(p, VM_PROT_READ);
1602 vm_object_drop(object);
1606 * Removes all physical pages in the specified object range from all
1609 * The object must *not* be locked.
1612 static int vm_object_pmap_remove_callback(vm_page_t p, void *data);
1615 vm_object_pmap_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1617 struct rb_vm_page_scan_info info;
1621 info.start_pindex = start;
1622 info.end_pindex = end - 1;
1624 vm_object_hold(object);
1625 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
1626 vm_object_pmap_remove_callback, &info);
1627 if (start == 0 && end == object->size)
1628 vm_object_clear_flag(object, OBJ_WRITEABLE);
1629 vm_object_drop(object);
1633 * The caller must hold the object
1636 vm_object_pmap_remove_callback(vm_page_t p, void *data __unused)
1638 vm_page_protect(p, VM_PROT_NONE);
1643 * Implements the madvise function at the object/page level.
1645 * MADV_WILLNEED (any object)
1647 * Activate the specified pages if they are resident.
1649 * MADV_DONTNEED (any object)
1651 * Deactivate the specified pages if they are resident.
1653 * MADV_FREE (OBJT_DEFAULT/OBJT_SWAP objects, OBJ_ONEMAPPING only)
1655 * Deactivate and clean the specified pages if they are
1656 * resident. This permits the process to reuse the pages
1657 * without faulting or the kernel to reclaim the pages
1663 vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
1665 vm_pindex_t end, tpindex;
1666 vm_object_t tobject;
1674 end = pindex + count;
1676 vm_object_hold(object);
1680 * Locate and adjust resident pages
1682 for (; pindex < end; pindex += 1) {
1684 if (tobject != object)
1685 vm_object_drop(tobject);
1690 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
1691 * and those pages must be OBJ_ONEMAPPING.
1693 if (advise == MADV_FREE) {
1694 if ((tobject->type != OBJT_DEFAULT &&
1695 tobject->type != OBJT_SWAP) ||
1696 (tobject->flags & OBJ_ONEMAPPING) == 0) {
1701 m = vm_page_lookup_busy_try(tobject, tpindex, TRUE, &error);
1704 vm_page_sleep_busy(m, TRUE, "madvpo");
1709 * There may be swap even if there is no backing page
1711 if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1712 swap_pager_freespace(tobject, tpindex, 1);
1717 while ((xobj = tobject->backing_object) != NULL) {
1718 KKASSERT(xobj != object);
1719 vm_object_hold(xobj);
1720 if (xobj == tobject->backing_object)
1722 vm_object_drop(xobj);
1726 tpindex += OFF_TO_IDX(tobject->backing_object_offset);
1727 if (tobject != object) {
1728 vm_object_lock_swap();
1729 vm_object_drop(tobject);
1736 * If the page is not in a normal active state, we skip it.
1737 * If the page is not managed there are no page queues to
1738 * mess with. Things can break if we mess with pages in
1739 * any of the below states.
1741 if (m->wire_count ||
1742 (m->flags & (PG_UNMANAGED | PG_NEED_COMMIT)) ||
1743 m->valid != VM_PAGE_BITS_ALL
1750 * Theoretically once a page is known not to be busy, an
1751 * interrupt cannot come along and rip it out from under us.
1754 if (advise == MADV_WILLNEED) {
1755 vm_page_activate(m);
1756 } else if (advise == MADV_DONTNEED) {
1757 vm_page_dontneed(m);
1758 } else if (advise == MADV_FREE) {
1760 * Mark the page clean. This will allow the page
1761 * to be freed up by the system. However, such pages
1762 * are often reused quickly by malloc()/free()
1763 * so we do not do anything that would cause
1764 * a page fault if we can help it.
1766 * Specifically, we do not try to actually free
1767 * the page now nor do we try to put it in the
1768 * cache (which would cause a page fault on reuse).
1770 * But we do make the page is freeable as we
1771 * can without actually taking the step of unmapping
1774 pmap_clear_modify(m);
1777 vm_page_dontneed(m);
1778 if (tobject->type == OBJT_SWAP)
1779 swap_pager_freespace(tobject, tpindex, 1);
1783 if (tobject != object)
1784 vm_object_drop(tobject);
1785 vm_object_drop(object);
1789 * Create a new object which is backed by the specified existing object
1790 * range. Replace the pointer and offset that was pointing at the existing
1791 * object with the pointer/offset for the new object.
1793 * No other requirements.
1796 vm_object_shadow(vm_object_t *objectp, vm_ooffset_t *offset, vm_size_t length,
1806 * Don't create the new object if the old object isn't shared.
1807 * We have to chain wait before adding the reference to avoid
1808 * racing a collapse or deallocation.
1810 * Add the additional ref to source here to avoid racing a later
1811 * collapse or deallocation. Clear the ONEMAPPING flag whether
1812 * addref is TRUE or not in this case because the original object
1817 if (source->type != OBJT_VNODE) {
1819 vm_object_hold(source);
1820 vm_object_chain_wait(source, 0);
1821 if (source->ref_count == 1 &&
1822 source->handle == NULL &&
1823 (source->type == OBJT_DEFAULT ||
1824 source->type == OBJT_SWAP)) {
1826 vm_object_reference_locked(source);
1827 vm_object_clear_flag(source, OBJ_ONEMAPPING);
1829 vm_object_drop(source);
1832 vm_object_reference_locked(source);
1833 vm_object_clear_flag(source, OBJ_ONEMAPPING);
1835 vm_object_reference_quick(source);
1836 vm_object_clear_flag(source, OBJ_ONEMAPPING);
1841 * Allocate a new object with the given length. The new object
1842 * is returned referenced but we may have to add another one.
1843 * If we are adding a second reference we must clear OBJ_ONEMAPPING.
1844 * (typically because the caller is about to clone a vm_map_entry).
1846 * The source object currently has an extra reference to prevent
1847 * collapses into it while we mess with its shadow list, which
1848 * we will remove later in this routine.
1850 if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL)
1851 panic("vm_object_shadow: no object for shadowing");
1852 vm_object_hold(result);
1854 vm_object_reference_locked(result);
1855 vm_object_clear_flag(result, OBJ_ONEMAPPING);
1859 * The new object shadows the source object. Chain wait before
1860 * adjusting shadow_count or the shadow list to avoid races.
1862 * Try to optimize the result object's page color when shadowing
1863 * in order to maintain page coloring consistency in the combined
1866 * SHADOWING IS NOT APPLICABLE TO OBJT_VNODE OBJECTS
1868 KKASSERT(result->backing_object == NULL);
1869 result->backing_object = source;
1871 if (useshadowlist) {
1872 vm_object_chain_wait(source, 0);
1873 LIST_INSERT_HEAD(&source->shadow_head,
1874 result, shadow_list);
1875 source->shadow_count++;
1876 source->generation++;
1877 vm_object_set_flag(result, OBJ_ONSHADOW);
1879 /* cpu localization twist */
1880 result->pg_color = (int)(intptr_t)curthread;
1884 * Adjust the return storage. Drop the ref on source before
1887 result->backing_object_offset = *offset;
1888 vm_object_drop(result);
1891 if (useshadowlist) {
1892 vm_object_deallocate_locked(source);
1893 vm_object_drop(source);
1895 vm_object_deallocate(source);
1900 * Return the new things
1905 #define OBSC_TEST_ALL_SHADOWED 0x0001
1906 #define OBSC_COLLAPSE_NOWAIT 0x0002
1907 #define OBSC_COLLAPSE_WAIT 0x0004
1909 static int vm_object_backing_scan_callback(vm_page_t p, void *data);
1912 * The caller must hold the object.
1915 vm_object_backing_scan(vm_object_t object, vm_object_t backing_object, int op)
1917 struct rb_vm_page_scan_info info;
1920 vm_object_assert_held(object);
1921 vm_object_assert_held(backing_object);
1923 KKASSERT(backing_object == object->backing_object);
1924 info.backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
1927 * Initial conditions
1929 if (op & OBSC_TEST_ALL_SHADOWED) {
1931 * We do not want to have to test for the existence of
1932 * swap pages in the backing object. XXX but with the
1933 * new swapper this would be pretty easy to do.
1935 * XXX what about anonymous MAP_SHARED memory that hasn't
1936 * been ZFOD faulted yet? If we do not test for this, the
1937 * shadow test may succeed! XXX
1939 if (backing_object->type != OBJT_DEFAULT)
1942 if (op & OBSC_COLLAPSE_WAIT) {
1943 KKASSERT((backing_object->flags & OBJ_DEAD) == 0);
1944 vm_object_set_flag(backing_object, OBJ_DEAD);
1946 n = VMOBJ_HASH(backing_object);
1947 lwkt_gettoken(&vmobj_tokens[n]);
1948 TAILQ_REMOVE(&vm_object_lists[n], backing_object, object_list);
1949 lwkt_reltoken(&vmobj_tokens[n]);
1950 atomic_add_long(&vm_object_count, -1);
1954 * Our scan. We have to retry if a negative error code is returned,
1955 * otherwise 0 or 1 will be returned in info.error. 0 Indicates that
1956 * the scan had to be stopped because the parent does not completely
1959 info.object = object;
1960 info.backing_object = backing_object;
1964 vm_page_rb_tree_RB_SCAN(&backing_object->rb_memq, NULL,
1965 vm_object_backing_scan_callback,
1967 } while (info.error < 0);
1973 * The caller must hold the object.
1976 vm_object_backing_scan_callback(vm_page_t p, void *data)
1978 struct rb_vm_page_scan_info *info = data;
1979 vm_object_t backing_object;
1982 vm_pindex_t new_pindex;
1983 vm_pindex_t backing_offset_index;
1987 new_pindex = pindex - info->backing_offset_index;
1989 object = info->object;
1990 backing_object = info->backing_object;
1991 backing_offset_index = info->backing_offset_index;
1993 if (op & OBSC_TEST_ALL_SHADOWED) {
1997 * Ignore pages outside the parent object's range
1998 * and outside the parent object's mapping of the
2001 * note that we do not busy the backing object's
2004 if (pindex < backing_offset_index ||
2005 new_pindex >= object->size
2011 * See if the parent has the page or if the parent's
2012 * object pager has the page. If the parent has the
2013 * page but the page is not valid, the parent's
2014 * object pager must have the page.
2016 * If this fails, the parent does not completely shadow
2017 * the object and we might as well give up now.
2019 pp = vm_page_lookup(object, new_pindex);
2020 if ((pp == NULL || pp->valid == 0) &&
2021 !vm_pager_has_page(object, new_pindex)
2023 info->error = 0; /* problemo */
2024 return(-1); /* stop the scan */
2029 * Check for busy page. Note that we may have lost (p) when we
2030 * possibly blocked above.
2032 if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
2035 if (vm_page_busy_try(p, TRUE)) {
2036 if (op & OBSC_COLLAPSE_NOWAIT) {
2040 * If we slept, anything could have
2041 * happened. Ask that the scan be restarted.
2043 * Since the object is marked dead, the
2044 * backing offset should not have changed.
2046 vm_page_sleep_busy(p, TRUE, "vmocol");
2053 * If (p) is no longer valid restart the scan.
2055 if (p->object != backing_object || p->pindex != pindex) {
2056 kprintf("vm_object_backing_scan: Warning: page "
2057 "%p ripped out from under us\n", p);
2063 if (op & OBSC_COLLAPSE_NOWAIT) {
2064 if (p->valid == 0 ||
2066 (p->flags & PG_NEED_COMMIT)) {
2071 /* XXX what if p->valid == 0 , hold_count, etc? */
2075 p->object == backing_object,
2076 ("vm_object_qcollapse(): object mismatch")
2080 * Destroy any associated swap
2082 if (backing_object->type == OBJT_SWAP)
2083 swap_pager_freespace(backing_object, p->pindex, 1);
2086 p->pindex < backing_offset_index ||
2087 new_pindex >= object->size
2090 * Page is out of the parent object's range, we
2091 * can simply destroy it.
2093 vm_page_protect(p, VM_PROT_NONE);
2098 pp = vm_page_lookup(object, new_pindex);
2099 if (pp != NULL || vm_pager_has_page(object, new_pindex)) {
2101 * page already exists in parent OR swap exists
2102 * for this location in the parent. Destroy
2103 * the original page from the backing object.
2105 * Leave the parent's page alone
2107 vm_page_protect(p, VM_PROT_NONE);
2113 * Page does not exist in parent, rename the
2114 * page from the backing object to the main object.
2116 * If the page was mapped to a process, it can remain
2117 * mapped through the rename.
2119 if ((p->queue - p->pc) == PQ_CACHE)
2120 vm_page_deactivate(p);
2122 vm_page_rename(p, object, new_pindex);
2124 /* page automatically made dirty by rename */
2130 * This version of collapse allows the operation to occur earlier and
2131 * when paging_in_progress is true for an object... This is not a complete
2132 * operation, but should plug 99.9% of the rest of the leaks.
2134 * The caller must hold the object and backing_object and both must be
2137 * (only called from vm_object_collapse)
2140 vm_object_qcollapse(vm_object_t object, vm_object_t backing_object)
2142 if (backing_object->ref_count == 1) {
2143 atomic_add_int(&backing_object->ref_count, 2);
2144 vm_object_backing_scan(object, backing_object,
2145 OBSC_COLLAPSE_NOWAIT);
2146 atomic_add_int(&backing_object->ref_count, -2);
2151 * Collapse an object with the object backing it. Pages in the backing
2152 * object are moved into the parent, and the backing object is deallocated.
2153 * Any conflict is resolved in favor of the parent's existing pages.
2155 * object must be held and chain-locked on call.
2157 * The caller must have an extra ref on object to prevent a race from
2158 * destroying it during the collapse.
2161 vm_object_collapse(vm_object_t object, struct vm_object_dealloc_list **dlistp)
2163 struct vm_object_dealloc_list *dlist = NULL;
2164 vm_object_t backing_object;
2167 * Only one thread is attempting a collapse at any given moment.
2168 * There are few restrictions for (object) that callers of this
2169 * function check so reentrancy is likely.
2171 KKASSERT(object != NULL);
2172 vm_object_assert_held(object);
2173 KKASSERT(object->chainlk & (CHAINLK_MASK | CHAINLK_EXCL));
2180 * We can only collapse a DEFAULT/SWAP object with a
2181 * DEFAULT/SWAP object.
2183 if (object->type != OBJT_DEFAULT && object->type != OBJT_SWAP) {
2184 backing_object = NULL;
2188 backing_object = object->backing_object;
2189 if (backing_object == NULL)
2191 if (backing_object->type != OBJT_DEFAULT &&
2192 backing_object->type != OBJT_SWAP) {
2193 backing_object = NULL;
2198 * Hold the backing_object and check for races
2200 vm_object_hold(backing_object);
2201 if (backing_object != object->backing_object ||
2202 (backing_object->type != OBJT_DEFAULT &&
2203 backing_object->type != OBJT_SWAP)) {
2204 vm_object_drop(backing_object);
2209 * Chain-lock the backing object too because if we
2210 * successfully merge its pages into the top object we
2211 * will collapse backing_object->backing_object as the
2212 * new backing_object. Re-check that it is still our
2215 vm_object_chain_acquire(backing_object, 0);
2216 if (backing_object != object->backing_object) {
2217 vm_object_chain_release(backing_object);
2218 vm_object_drop(backing_object);
2223 * we check the backing object first, because it is most likely
2226 if (backing_object->handle != NULL ||
2227 (backing_object->type != OBJT_DEFAULT &&
2228 backing_object->type != OBJT_SWAP) ||
2229 (backing_object->flags & OBJ_DEAD) ||
2230 object->handle != NULL ||
2231 (object->type != OBJT_DEFAULT &&
2232 object->type != OBJT_SWAP) ||
2233 (object->flags & OBJ_DEAD)) {
2238 * If paging is in progress we can't do a normal collapse.
2241 object->paging_in_progress != 0 ||
2242 backing_object->paging_in_progress != 0
2244 vm_object_qcollapse(object, backing_object);
2249 * We know that we can either collapse the backing object (if
2250 * the parent is the only reference to it) or (perhaps) have
2251 * the parent bypass the object if the parent happens to shadow
2252 * all the resident pages in the entire backing object.
2254 * This is ignoring pager-backed pages such as swap pages.
2255 * vm_object_backing_scan fails the shadowing test in this
2258 if (backing_object->ref_count == 1) {
2260 * If there is exactly one reference to the backing
2261 * object, we can collapse it into the parent.
2263 KKASSERT(object->backing_object == backing_object);
2264 vm_object_backing_scan(object, backing_object,
2265 OBSC_COLLAPSE_WAIT);
2268 * Move the pager from backing_object to object.
2270 if (backing_object->type == OBJT_SWAP) {
2271 vm_object_pip_add(backing_object, 1);
2274 * scrap the paging_offset junk and do a
2275 * discrete copy. This also removes major
2276 * assumptions about how the swap-pager
2277 * works from where it doesn't belong. The
2278 * new swapper is able to optimize the
2279 * destroy-source case.
2281 vm_object_pip_add(object, 1);
2282 swap_pager_copy(backing_object, object,
2283 OFF_TO_IDX(object->backing_object_offset),
2285 vm_object_pip_wakeup(object);
2286 vm_object_pip_wakeup(backing_object);
2290 * Object now shadows whatever backing_object did.
2291 * Remove object from backing_object's shadow_list.
2293 KKASSERT(object->backing_object == backing_object);
2294 if (object->flags & OBJ_ONSHADOW) {
2295 LIST_REMOVE(object, shadow_list);
2296 backing_object->shadow_count--;
2297 backing_object->generation++;
2298 vm_object_clear_flag(object, OBJ_ONSHADOW);
2302 * backing_object->backing_object moves from within
2303 * backing_object to within object.
2305 * OBJT_VNODE bbobj's should have empty shadow lists.
2307 while ((bbobj = backing_object->backing_object) != NULL) {
2308 if (bbobj->type == OBJT_VNODE)
2309 vm_object_hold_shared(bbobj);
2311 vm_object_hold(bbobj);
2312 if (bbobj == backing_object->backing_object)
2314 vm_object_drop(bbobj);
2317 if (backing_object->flags & OBJ_ONSHADOW) {
2318 /* not locked exclusively if vnode */
2319 KKASSERT(bbobj->type != OBJT_VNODE);
2320 LIST_REMOVE(backing_object,
2322 bbobj->shadow_count--;
2323 bbobj->generation++;
2324 vm_object_clear_flag(backing_object,
2327 backing_object->backing_object = NULL;
2329 object->backing_object = bbobj;
2331 if (bbobj->type != OBJT_VNODE) {
2332 LIST_INSERT_HEAD(&bbobj->shadow_head,
2333 object, shadow_list);
2334 bbobj->shadow_count++;
2335 bbobj->generation++;
2336 vm_object_set_flag(object,
2341 object->backing_object_offset +=
2342 backing_object->backing_object_offset;
2344 vm_object_drop(bbobj);
2347 * Discard the old backing_object. Nothing should be
2348 * able to ref it, other than a vm_map_split(),
2349 * and vm_map_split() will stall on our chain lock.
2350 * And we control the parent so it shouldn't be
2351 * possible for it to go away either.
2353 * Since the backing object has no pages, no pager
2354 * left, and no object references within it, all
2355 * that is necessary is to dispose of it.
2357 KASSERT(backing_object->ref_count == 1,
2358 ("backing_object %p was somehow "
2359 "re-referenced during collapse!",
2361 KASSERT(RB_EMPTY(&backing_object->rb_memq),
2362 ("backing_object %p somehow has left "
2363 "over pages during collapse!",
2367 * The object can be destroyed.
2369 * XXX just fall through and dodealloc instead
2370 * of forcing destruction?
2372 atomic_add_int(&backing_object->ref_count, -1);
2373 if ((backing_object->flags & OBJ_DEAD) == 0)
2374 vm_object_terminate(backing_object);
2379 * If we do not entirely shadow the backing object,
2380 * there is nothing we can do so we give up.
2382 if (vm_object_backing_scan(object, backing_object,
2383 OBSC_TEST_ALL_SHADOWED) == 0) {
2388 * bbobj is backing_object->backing_object. Since
2389 * object completely shadows backing_object we can
2390 * bypass it and become backed by bbobj instead.
2392 * The shadow list for vnode backing objects is not
2393 * used and a shared hold is allowed.
2395 while ((bbobj = backing_object->backing_object) != NULL) {
2396 if (bbobj->type == OBJT_VNODE)
2397 vm_object_hold_shared(bbobj);
2399 vm_object_hold(bbobj);
2400 if (bbobj == backing_object->backing_object)
2402 vm_object_drop(bbobj);
2406 * Make object shadow bbobj instead of backing_object.
2407 * Remove object from backing_object's shadow list.
2409 * Deallocating backing_object will not remove
2410 * it, since its reference count is at least 2.
2412 KKASSERT(object->backing_object == backing_object);
2413 if (object->flags & OBJ_ONSHADOW) {
2414 LIST_REMOVE(object, shadow_list);
2415 backing_object->shadow_count--;
2416 backing_object->generation++;
2417 vm_object_clear_flag(object, OBJ_ONSHADOW);
2421 * Add a ref to bbobj, bbobj now shadows object.
2423 * NOTE: backing_object->backing_object still points
2424 * to bbobj. That relationship remains intact
2425 * because backing_object has > 1 ref, so
2426 * someone else is pointing to it (hence why
2427 * we can't collapse it into object and can
2428 * only handle the all-shadowed bypass case).
2431 if (bbobj->type != OBJT_VNODE) {
2432 vm_object_chain_wait(bbobj, 0);
2433 vm_object_reference_locked(bbobj);
2434 LIST_INSERT_HEAD(&bbobj->shadow_head,
2435 object, shadow_list);
2436 bbobj->shadow_count++;
2437 bbobj->generation++;
2438 vm_object_set_flag(object,
2441 vm_object_reference_quick(bbobj);
2443 object->backing_object_offset +=
2444 backing_object->backing_object_offset;
2445 object->backing_object = bbobj;
2446 vm_object_drop(bbobj);
2448 object->backing_object = NULL;
2452 * Drop the reference count on backing_object. To
2453 * handle ref_count races properly we can't assume
2454 * that the ref_count is still at least 2 so we
2455 * have to actually call vm_object_deallocate()
2456 * (after clearing the chainlock).
2463 * Ok, we want to loop on the new object->bbobj association,
2464 * possibly collapsing it further. However if dodealloc is
2465 * non-zero we have to deallocate the backing_object which
2466 * itself can potentially undergo a collapse, creating a
2467 * recursion depth issue with the LWKT token subsystem.
2469 * In the case where we must deallocate the backing_object
2470 * it is possible now that the backing_object has a single
2471 * shadow count on some other object (not represented here
2472 * as yet), since it no longer shadows us. Thus when we
2473 * call vm_object_deallocate() it may attempt to collapse
2474 * itself into its remaining parent.
2477 struct vm_object_dealloc_list *dtmp;
2479 vm_object_chain_release(backing_object);
2480 vm_object_unlock(backing_object);
2481 /* backing_object remains held */
2484 * Auto-deallocation list for caller convenience.
2489 dtmp = kmalloc(sizeof(*dtmp), M_TEMP, M_WAITOK);
2490 dtmp->object = backing_object;
2491 dtmp->next = *dlistp;
2494 vm_object_chain_release(backing_object);
2495 vm_object_drop(backing_object);
2497 /* backing_object = NULL; not needed */
2502 * Clean up any left over backing_object
2504 if (backing_object) {
2505 vm_object_chain_release(backing_object);
2506 vm_object_drop(backing_object);
2510 * Clean up any auto-deallocation list. This is a convenience
2511 * for top-level callers so they don't have to pass &dlist.
2512 * Do not clean up any caller-passed dlistp, the caller will
2516 vm_object_deallocate_list(&dlist);
2521 * vm_object_collapse() may collect additional objects in need of
2522 * deallocation. This routine deallocates these objects. The
2523 * deallocation itself can trigger additional collapses (which the
2524 * deallocate function takes care of). This procedure is used to
2525 * reduce procedural recursion since these vm_object shadow chains
2526 * can become quite long.
2529 vm_object_deallocate_list(struct vm_object_dealloc_list **dlistp)
2531 struct vm_object_dealloc_list *dlist;
2533 while ((dlist = *dlistp) != NULL) {
2534 *dlistp = dlist->next;
2535 vm_object_lock(dlist->object);
2536 vm_object_deallocate_locked(dlist->object);
2537 vm_object_drop(dlist->object);
2538 kfree(dlist, M_TEMP);
2543 * Removes all physical pages in the specified object range from the
2544 * object's list of pages.
2548 static int vm_object_page_remove_callback(vm_page_t p, void *data);
2551 vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
2552 boolean_t clean_only)
2554 struct rb_vm_page_scan_info info;
2558 * Degenerate cases and assertions
2560 vm_object_hold(object);
2561 if (object == NULL ||
2562 (object->resident_page_count == 0 && object->swblock_count == 0)) {
2563 vm_object_drop(object);
2566 KASSERT(object->type != OBJT_PHYS,
2567 ("attempt to remove pages from a physical object"));
2570 * Indicate that paging is occuring on the object
2572 vm_object_pip_add(object, 1);
2575 * Figure out the actual removal range and whether we are removing
2576 * the entire contents of the object or not. If removing the entire
2577 * contents, be sure to get all pages, even those that might be
2578 * beyond the end of the object.
2580 info.start_pindex = start;
2582 info.end_pindex = (vm_pindex_t)-1;
2584 info.end_pindex = end - 1;
2585 info.limit = clean_only;
2586 all = (start == 0 && info.end_pindex >= object->size - 1);
2589 * Loop until we are sure we have gotten them all.
2593 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
2594 vm_object_page_remove_callback, &info);
2595 } while (info.error);
2598 * Remove any related swap if throwing away pages, or for
2599 * non-swap objects (the swap is a clean copy in that case).
2601 if (object->type != OBJT_SWAP || clean_only == FALSE) {
2603 swap_pager_freespace_all(object);
2605 swap_pager_freespace(object, info.start_pindex,
2606 info.end_pindex - info.start_pindex + 1);
2612 vm_object_pip_wakeup(object);
2613 vm_object_drop(object);
2617 * The caller must hold the object
2620 vm_object_page_remove_callback(vm_page_t p, void *data)
2622 struct rb_vm_page_scan_info *info = data;
2624 if (vm_page_busy_try(p, TRUE)) {
2625 vm_page_sleep_busy(p, TRUE, "vmopar");
2631 * Wired pages cannot be destroyed, but they can be invalidated
2632 * and we do so if clean_only (limit) is not set.
2634 * WARNING! The page may be wired due to being part of a buffer
2635 * cache buffer, and the buffer might be marked B_CACHE.
2636 * This is fine as part of a truncation but VFSs must be
2637 * sure to fix the buffer up when re-extending the file.
2639 * NOTE! PG_NEED_COMMIT is ignored.
2641 if (p->wire_count != 0) {
2642 vm_page_protect(p, VM_PROT_NONE);
2643 if (info->limit == 0)
2650 * limit is our clean_only flag. If set and the page is dirty or
2651 * requires a commit, do not free it. If set and the page is being
2652 * held by someone, do not free it.
2654 if (info->limit && p->valid) {
2655 vm_page_test_dirty(p);
2656 if ((p->valid & p->dirty) || (p->flags & PG_NEED_COMMIT)) {
2665 vm_page_protect(p, VM_PROT_NONE);
2671 * Coalesces two objects backing up adjoining regions of memory into a
2674 * returns TRUE if objects were combined.
2676 * NOTE: Only works at the moment if the second object is NULL -
2677 * if it's not, which object do we lock first?
2680 * prev_object First object to coalesce
2681 * prev_offset Offset into prev_object
2682 * next_object Second object into coalesce
2683 * next_offset Offset into next_object
2685 * prev_size Size of reference to prev_object
2686 * next_size Size of reference to next_object
2688 * The caller does not need to hold (prev_object) but must have a stable
2689 * pointer to it (typically by holding the vm_map locked).
2692 vm_object_coalesce(vm_object_t prev_object, vm_pindex_t prev_pindex,
2693 vm_size_t prev_size, vm_size_t next_size)
2695 vm_pindex_t next_pindex;
2697 if (prev_object == NULL)
2700 vm_object_hold(prev_object);
2702 if (prev_object->type != OBJT_DEFAULT &&
2703 prev_object->type != OBJT_SWAP) {
2704 vm_object_drop(prev_object);
2709 * Try to collapse the object first
2711 vm_object_chain_acquire(prev_object, 0);
2712 vm_object_collapse(prev_object, NULL);
2715 * Can't coalesce if: . more than one reference . paged out . shadows
2716 * another object . has a copy elsewhere (any of which mean that the
2717 * pages not mapped to prev_entry may be in use anyway)
2720 if (prev_object->backing_object != NULL) {
2721 vm_object_chain_release(prev_object);
2722 vm_object_drop(prev_object);
2726 prev_size >>= PAGE_SHIFT;
2727 next_size >>= PAGE_SHIFT;
2728 next_pindex = prev_pindex + prev_size;
2730 if ((prev_object->ref_count > 1) &&
2731 (prev_object->size != next_pindex)) {
2732 vm_object_chain_release(prev_object);
2733 vm_object_drop(prev_object);
2738 * Remove any pages that may still be in the object from a previous
2741 if (next_pindex < prev_object->size) {
2742 vm_object_page_remove(prev_object,
2744 next_pindex + next_size, FALSE);
2745 if (prev_object->type == OBJT_SWAP)
2746 swap_pager_freespace(prev_object,
2747 next_pindex, next_size);
2751 * Extend the object if necessary.
2753 if (next_pindex + next_size > prev_object->size)
2754 prev_object->size = next_pindex + next_size;
2756 vm_object_chain_release(prev_object);
2757 vm_object_drop(prev_object);
2762 * Make the object writable and flag is being possibly dirty.
2764 * The object might not be held (or might be held but held shared),
2765 * the related vnode is probably not held either. Object and vnode are
2766 * stable by virtue of the vm_page busied by the caller preventing
2769 * If the related mount is flagged MNTK_THR_SYNC we need to call
2770 * vsetobjdirty(). Filesystems using this option usually shortcut
2771 * synchronization by only scanning the syncer list.
2774 vm_object_set_writeable_dirty(vm_object_t object)
2778 /*vm_object_assert_held(object);*/
2780 * Avoid contention in vm fault path by checking the state before
2781 * issuing an atomic op on it.
2783 if ((object->flags & (OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY)) !=
2784 (OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY)) {
2785 vm_object_set_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
2787 if (object->type == OBJT_VNODE &&
2788 (vp = (struct vnode *)object->handle) != NULL) {
2789 if ((vp->v_flag & VOBJDIRTY) == 0) {
2791 (vp->v_mount->mnt_kern_flag & MNTK_THR_SYNC)) {
2793 * New style THR_SYNC places vnodes on the
2794 * syncer list more deterministically.
2799 * Old style scan would not necessarily place
2800 * a vnode on the syncer list when possibly
2801 * modified via mmap.
2803 vsetflags(vp, VOBJDIRTY);
2809 #include "opt_ddb.h"
2811 #include <sys/kernel.h>
2813 #include <sys/cons.h>
2815 #include <ddb/ddb.h>
2817 static int _vm_object_in_map (vm_map_t map, vm_object_t object,
2818 vm_map_entry_t entry);
2819 static int vm_object_in_map (vm_object_t object);
2822 * The caller must hold the object.
2825 _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
2828 vm_map_entry_t tmpe;
2829 vm_object_t obj, nobj;
2835 tmpe = map->header.next;
2836 entcount = map->nentries;
2837 while (entcount-- && (tmpe != &map->header)) {
2838 if( _vm_object_in_map(map, object, tmpe)) {
2845 switch(entry->maptype) {
2846 case VM_MAPTYPE_SUBMAP:
2847 tmpm = entry->object.sub_map;
2848 tmpe = tmpm->header.next;
2849 entcount = tmpm->nentries;
2850 while (entcount-- && tmpe != &tmpm->header) {
2851 if( _vm_object_in_map(tmpm, object, tmpe)) {
2857 case VM_MAPTYPE_NORMAL:
2858 case VM_MAPTYPE_VPAGETABLE:
2859 obj = entry->object.vm_object;
2861 if (obj == object) {
2862 if (obj != entry->object.vm_object)
2863 vm_object_drop(obj);
2866 while ((nobj = obj->backing_object) != NULL) {
2867 vm_object_hold(nobj);
2868 if (nobj == obj->backing_object)
2870 vm_object_drop(nobj);
2872 if (obj != entry->object.vm_object) {
2874 vm_object_lock_swap();
2875 vm_object_drop(obj);
2886 static int vm_object_in_map_callback(struct proc *p, void *data);
2888 struct vm_object_in_map_info {
2897 vm_object_in_map(vm_object_t object)
2899 struct vm_object_in_map_info info;
2902 info.object = object;
2904 allproc_scan(vm_object_in_map_callback, &info);
2907 if( _vm_object_in_map(&kernel_map, object, 0))
2909 if( _vm_object_in_map(&pager_map, object, 0))
2911 if( _vm_object_in_map(&buffer_map, object, 0))
2920 vm_object_in_map_callback(struct proc *p, void *data)
2922 struct vm_object_in_map_info *info = data;
2925 if (_vm_object_in_map(&p->p_vmspace->vm_map, info->object, 0)) {
2933 DB_SHOW_COMMAND(vmochk, vm_object_check)
2939 * make sure that internal objs are in a map somewhere
2940 * and none have zero ref counts.
2942 for (n = 0; n < VMOBJ_HSIZE; ++n) {
2943 for (object = TAILQ_FIRST(&vm_object_lists[n]);
2945 object = TAILQ_NEXT(object, object_list)) {
2946 if (object->type == OBJT_MARKER)
2948 if (object->handle != NULL ||
2949 (object->type != OBJT_DEFAULT &&
2950 object->type != OBJT_SWAP)) {
2953 if (object->ref_count == 0) {
2954 db_printf("vmochk: internal obj has "
2955 "zero ref count: %ld\n",
2956 (long)object->size);
2958 if (vm_object_in_map(object))
2960 db_printf("vmochk: internal obj is not in a map: "
2961 "ref: %d, size: %lu: 0x%lx, "
2962 "backing_object: %p\n",
2963 object->ref_count, (u_long)object->size,
2964 (u_long)object->size,
2965 (void *)object->backing_object);
2973 DB_SHOW_COMMAND(object, vm_object_print_static)
2975 /* XXX convert args. */
2976 vm_object_t object = (vm_object_t)addr;
2977 boolean_t full = have_addr;
2981 /* XXX count is an (unused) arg. Avoid shadowing it. */
2982 #define count was_count
2990 "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n",
2991 object, (int)object->type, (u_long)object->size,
2992 object->resident_page_count, object->ref_count, object->flags);
2994 * XXX no %qd in kernel. Truncate object->backing_object_offset.
2996 db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%lx\n",
2997 object->shadow_count,
2998 object->backing_object ? object->backing_object->ref_count : 0,
2999 object->backing_object, (long)object->backing_object_offset);
3006 RB_FOREACH(p, vm_page_rb_tree, &object->rb_memq) {
3008 db_iprintf("memory:=");
3009 else if (count == 6) {
3017 db_printf("(off=0x%lx,page=0x%lx)",
3018 (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
3029 * XXX need this non-static entry for calling from vm_map_print.
3034 vm_object_print(/* db_expr_t */ long addr,
3035 boolean_t have_addr,
3036 /* db_expr_t */ long count,
3039 vm_object_print_static(addr, have_addr, count, modif);
3045 DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
3052 for (n = 0; n < VMOBJ_HSIZE; ++n) {
3053 for (object = TAILQ_FIRST(&vm_object_lists[n]);
3055 object = TAILQ_NEXT(object, object_list)) {
3056 vm_pindex_t idx, fidx;
3058 vm_paddr_t pa = -1, padiff;
3062 if (object->type == OBJT_MARKER)
3064 db_printf("new object: %p\n", (void *)object);
3074 osize = object->size;
3077 for (idx = 0; idx < osize; idx++) {
3078 m = vm_page_lookup(object, idx);
3081 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
3082 (long)fidx, rcount, (long)pa);
3096 (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
3101 padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
3102 padiff >>= PAGE_SHIFT;
3103 padiff &= PQ_L2_MASK;
3105 pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
3109 db_printf(" index(%ld)run(%d)pa(0x%lx)",
3110 (long)fidx, rcount, (long)pa);
3111 db_printf("pd(%ld)\n", (long)padiff);
3121 pa = VM_PAGE_TO_PHYS(m);
3125 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
3126 (long)fidx, rcount, (long)pa);