4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * The Mach Operating System project at Carnegie-Mellon University.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * from: @(#)vm_object.c 8.5 (Berkeley) 3/22/94
41 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
42 * All rights reserved.
44 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
46 * Permission to use, copy, modify and distribute this software and
47 * its documentation is hereby granted, provided that both the copyright
48 * notice and this permission notice appear in all copies of the
49 * software, derivative works or modified versions, and any portions
50 * thereof, and that both notices appear in supporting documentation.
52 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
53 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
54 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
56 * Carnegie Mellon requests users of this software to return to
58 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
59 * School of Computer Science
60 * Carnegie Mellon University
61 * Pittsburgh PA 15213-3890
63 * any improvements or extensions that they make and grant Carnegie the
64 * rights to redistribute these changes.
66 * $FreeBSD: src/sys/vm/vm_object.c,v 1.171.2.8 2003/05/26 19:17:56 alc Exp $
70 * Virtual memory object module.
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/proc.h> /* for curproc, pageproc */
76 #include <sys/thread.h>
77 #include <sys/vnode.h>
78 #include <sys/vmmeter.h>
80 #include <sys/mount.h>
81 #include <sys/kernel.h>
82 #include <sys/sysctl.h>
83 #include <sys/refcount.h>
86 #include <vm/vm_param.h>
88 #include <vm/vm_map.h>
89 #include <vm/vm_object.h>
90 #include <vm/vm_page.h>
91 #include <vm/vm_pageout.h>
92 #include <vm/vm_pager.h>
93 #include <vm/swap_pager.h>
94 #include <vm/vm_kern.h>
95 #include <vm/vm_extern.h>
96 #include <vm/vm_zone.h>
98 #define EASY_SCAN_FACTOR 8
100 static void vm_object_qcollapse(vm_object_t object,
101 vm_object_t backing_object);
102 static int vm_object_page_collect_flush(vm_object_t object, vm_page_t p,
104 static void vm_object_lock_init(vm_object_t);
108 * Virtual memory objects maintain the actual data
109 * associated with allocated virtual memory. A given
110 * page of memory exists within exactly one object.
112 * An object is only deallocated when all "references"
113 * are given up. Only one "reference" to a given
114 * region of an object should be writeable.
116 * Associated with each object is a list of all resident
117 * memory pages belonging to that object; this list is
118 * maintained by the "vm_page" module, and locked by the object's
121 * Each object also records a "pager" routine which is
122 * used to retrieve (and store) pages to the proper backing
123 * storage. In addition, objects may be backed by other
124 * objects from which they were virtual-copied.
126 * The only items within the object structure which are
127 * modified after time of creation are:
128 * reference count locked by object's lock
129 * pager routine locked by object's lock
133 struct object_q vm_object_list; /* locked by vmobj_token */
134 struct vm_object kernel_object;
136 static long vm_object_count; /* locked by vmobj_token */
137 extern int vm_pageout_page_count;
139 static long object_collapses;
140 static long object_bypasses;
141 static int next_index;
142 static vm_zone_t obj_zone;
143 static struct vm_zone obj_zone_store;
144 #define VM_OBJECTS_INIT 256
145 static struct vm_object vm_objects_init[VM_OBJECTS_INIT];
148 * Misc low level routines
151 vm_object_lock_init(vm_object_t obj)
153 #if defined(DEBUG_LOCKS)
156 obj->debug_hold_bitmap = 0;
157 obj->debug_hold_ovfl = 0;
158 for (i = 0; i < VMOBJ_DEBUG_ARRAY_SIZE; i++) {
159 obj->debug_hold_thrs[i] = NULL;
160 obj->debug_hold_file[i] = NULL;
161 obj->debug_hold_line[i] = 0;
167 vm_object_lock_swap(void)
173 vm_object_lock(vm_object_t obj)
175 lwkt_getpooltoken(obj);
179 vm_object_lock_shared(vm_object_t obj)
181 lwkt_token_t tok = lwkt_token_pool_lookup(obj);
182 lwkt_gettoken_shared(tok);
186 vm_object_unlock(vm_object_t obj)
188 lwkt_relpooltoken(obj);
192 vm_object_assert_held(vm_object_t obj)
194 ASSERT_LWKT_TOKEN_HELD(lwkt_token_pool_lookup(obj));
199 vm_object_hold(vm_object_t obj)
201 debugvm_object_hold(vm_object_t obj, char *file, int line)
204 KKASSERT(obj != NULL);
207 * Object must be held (object allocation is stable due to callers
208 * context, typically already holding the token on a parent object)
209 * prior to potentially blocking on the lock, otherwise the object
210 * can get ripped away from us.
212 refcount_acquire(&obj->hold_count);
215 #if defined(DEBUG_LOCKS)
218 i = ffs(~obj->debug_hold_bitmap) - 1;
220 kprintf("vm_object hold count > VMOBJ_DEBUG_ARRAY_SIZE");
221 obj->debug_hold_ovfl = 1;
224 obj->debug_hold_bitmap |= (1 << i);
225 obj->debug_hold_thrs[i] = curthread;
226 obj->debug_hold_file[i] = file;
227 obj->debug_hold_line[i] = line;
233 vm_object_hold_shared(vm_object_t obj)
235 debugvm_object_hold_shared(vm_object_t obj, char *file, int line)
238 KKASSERT(obj != NULL);
241 * Object must be held (object allocation is stable due to callers
242 * context, typically already holding the token on a parent object)
243 * prior to potentially blocking on the lock, otherwise the object
244 * can get ripped away from us.
246 refcount_acquire(&obj->hold_count);
247 vm_object_lock_shared(obj);
249 #if defined(DEBUG_LOCKS)
252 i = ffs(~obj->debug_hold_bitmap) - 1;
254 kprintf("vm_object hold count > VMOBJ_DEBUG_ARRAY_SIZE");
255 obj->debug_hold_ovfl = 1;
258 obj->debug_hold_bitmap |= (1 << i);
259 obj->debug_hold_thrs[i] = curthread;
260 obj->debug_hold_file[i] = file;
261 obj->debug_hold_line[i] = line;
266 * Drop the token and hold_count on the object.
269 vm_object_drop(vm_object_t obj)
274 #if defined(DEBUG_LOCKS)
278 for (i = 0; i < VMOBJ_DEBUG_ARRAY_SIZE; i++) {
279 if ((obj->debug_hold_bitmap & (1 << i)) &&
280 (obj->debug_hold_thrs[i] == curthread)) {
281 obj->debug_hold_bitmap &= ~(1 << i);
282 obj->debug_hold_thrs[i] = NULL;
283 obj->debug_hold_file[i] = NULL;
284 obj->debug_hold_line[i] = 0;
290 if (found == 0 && obj->debug_hold_ovfl == 0)
291 panic("vm_object: attempt to drop hold on non-self-held obj");
295 * The lock is a pool token, no new holders should be possible once
296 * we drop hold_count 1->0 as there is no longer any way to reference
299 KKASSERT(obj->hold_count > 0);
300 if (refcount_release(&obj->hold_count)) {
301 if (obj->ref_count == 0 && (obj->flags & OBJ_DEAD))
302 zfree(obj_zone, obj);
304 vm_object_unlock(obj); /* uses pool token, ok to call on freed obj */
308 * Initialize a freshly allocated object
310 * Used only by vm_object_allocate() and zinitna().
315 _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
319 RB_INIT(&object->rb_memq);
320 LIST_INIT(&object->shadow_head);
324 object->ref_count = 1;
325 object->hold_count = 0;
327 if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
328 vm_object_set_flag(object, OBJ_ONEMAPPING);
329 object->paging_in_progress = 0;
330 object->resident_page_count = 0;
331 object->agg_pv_list_count = 0;
332 object->shadow_count = 0;
334 /* cpu localization twist */
335 object->pg_color = (int)(intptr_t)curthread;
337 object->pg_color = next_index;
339 if ( size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
340 incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
343 next_index = (next_index + incr) & PQ_L2_MASK;
344 object->handle = NULL;
345 object->backing_object = NULL;
346 object->backing_object_offset = (vm_ooffset_t)0;
348 object->generation++;
349 object->swblock_count = 0;
350 RB_INIT(&object->swblock_root);
351 vm_object_lock_init(object);
353 lwkt_gettoken(&vmobj_token);
354 TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
356 lwkt_reltoken(&vmobj_token);
360 * Initialize the VM objects module.
362 * Called from the low level boot code only.
367 TAILQ_INIT(&vm_object_list);
369 _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(KvaEnd),
372 obj_zone = &obj_zone_store;
373 zbootinit(obj_zone, "VM OBJECT", sizeof (struct vm_object),
374 vm_objects_init, VM_OBJECTS_INIT);
378 vm_object_init2(void)
380 zinitna(obj_zone, NULL, NULL, 0, 0, ZONE_PANICFAIL, 1);
384 * Allocate and return a new object of the specified type and size.
389 vm_object_allocate(objtype_t type, vm_pindex_t size)
393 result = (vm_object_t) zalloc(obj_zone);
395 _vm_object_allocate(type, size, result);
401 * Add an additional reference to a vm_object. The object must already be
402 * held. The original non-lock version is no longer supported. The object
403 * must NOT be chain locked by anyone at the time the reference is added.
405 * Referencing a chain-locked object can blow up the fairly sensitive
406 * ref_count and shadow_count tests in the deallocator. Most callers
407 * will call vm_object_chain_wait() prior to calling
408 * vm_object_reference_locked() to avoid the case.
410 * The object must be held.
413 vm_object_reference_locked(vm_object_t object)
415 KKASSERT(object != NULL);
416 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
417 KKASSERT((object->flags & OBJ_CHAINLOCK) == 0);
419 if (object->type == OBJT_VNODE) {
420 vref(object->handle);
421 /* XXX what if the vnode is being destroyed? */
426 * Object OBJ_CHAINLOCK lock handling.
428 * The caller can chain-lock backing objects recursively and then
429 * use vm_object_chain_release_all() to undo the whole chain.
431 * Chain locks are used to prevent collapses and are only applicable
432 * to OBJT_DEFAULT and OBJT_SWAP objects. Chain locking operations
433 * on other object types are ignored. This is also important because
434 * it allows e.g. the vnode underlying a memory mapping to take concurrent
437 * The object must usually be held on entry, though intermediate
438 * objects need not be held on release.
441 vm_object_chain_wait(vm_object_t object)
443 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
444 while (object->flags & OBJ_CHAINLOCK) {
445 vm_object_set_flag(object, OBJ_CHAINWANT);
446 tsleep(object, 0, "objchain", 0);
451 vm_object_chain_acquire(vm_object_t object)
453 if (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP) {
454 vm_object_chain_wait(object);
455 vm_object_set_flag(object, OBJ_CHAINLOCK);
460 vm_object_chain_release(vm_object_t object)
462 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
463 if (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP) {
464 KKASSERT(object->flags & OBJ_CHAINLOCK);
465 if (object->flags & OBJ_CHAINWANT) {
466 vm_object_clear_flag(object,
467 OBJ_CHAINLOCK | OBJ_CHAINWANT);
470 vm_object_clear_flag(object, OBJ_CHAINLOCK);
476 * This releases the entire chain of objects from first_object to and
477 * including stopobj, flowing through object->backing_object.
479 * We release stopobj first as an optimization as this object is most
480 * likely to be shared across multiple processes.
483 vm_object_chain_release_all(vm_object_t first_object, vm_object_t stopobj)
485 vm_object_t backing_object;
488 vm_object_chain_release(stopobj);
489 object = first_object;
491 while (object != stopobj) {
493 if (object != first_object)
494 vm_object_hold(object);
495 backing_object = object->backing_object;
496 vm_object_chain_release(object);
497 if (object != first_object)
498 vm_object_drop(object);
499 object = backing_object;
504 * Dereference an object and its underlying vnode.
506 * The object must be held and will be held on return.
509 vm_object_vndeallocate(vm_object_t object)
511 struct vnode *vp = (struct vnode *) object->handle;
513 KASSERT(object->type == OBJT_VNODE,
514 ("vm_object_vndeallocate: not a vnode object"));
515 KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
516 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
518 if (object->ref_count == 0) {
519 vprint("vm_object_vndeallocate", vp);
520 panic("vm_object_vndeallocate: bad object reference count");
524 if (object->ref_count == 0)
525 vclrflags(vp, VTEXT);
530 * Release a reference to the specified object, gained either through a
531 * vm_object_allocate or a vm_object_reference call. When all references
532 * are gone, storage associated with this object may be relinquished.
534 * The caller does not have to hold the object locked but must have control
535 * over the reference in question in order to guarantee that the object
536 * does not get ripped out from under us.
539 vm_object_deallocate(vm_object_t object)
542 vm_object_hold(object);
543 vm_object_deallocate_locked(object);
544 vm_object_drop(object);
549 vm_object_deallocate_locked(vm_object_t object)
551 struct vm_object_dealloc_list *dlist = NULL;
552 struct vm_object_dealloc_list *dtmp;
557 * We may chain deallocate object, but additional objects may
558 * collect on the dlist which also have to be deallocated. We
559 * must avoid a recursion, vm_object chains can get deep.
562 while (object != NULL) {
565 * Don't rip a ref_count out from under an object undergoing
566 * collapse, it will confuse the collapse code.
568 vm_object_chain_wait(object);
570 if (object->type == OBJT_VNODE) {
571 vm_object_vndeallocate(object);
575 if (object->ref_count == 0) {
576 panic("vm_object_deallocate: object deallocated "
577 "too many times: %d", object->type);
579 if (object->ref_count > 2) {
585 * Here on ref_count of one or two, which are special cases for
588 * Nominal ref_count > 1 case if the second ref is not from
591 if (object->ref_count == 2 && object->shadow_count == 0) {
592 vm_object_set_flag(object, OBJ_ONEMAPPING);
598 * If the second ref is from a shadow we chain along it
599 * upwards if object's handle is exhausted.
601 * We have to decrement object->ref_count before potentially
602 * collapsing the first shadow object or the collapse code
603 * will not be able to handle the degenerate case to remove
604 * object. However, if we do it too early the object can
605 * get ripped out from under us.
607 if (object->ref_count == 2 && object->shadow_count == 1 &&
608 object->handle == NULL && (object->type == OBJT_DEFAULT ||
609 object->type == OBJT_SWAP)) {
610 temp = LIST_FIRST(&object->shadow_head);
611 KKASSERT(temp != NULL);
612 vm_object_hold(temp);
615 * Wait for any paging to complete so the collapse
616 * doesn't (or isn't likely to) qcollapse. pip
617 * waiting must occur before we acquire the
621 temp->paging_in_progress ||
622 object->paging_in_progress
624 vm_object_pip_wait(temp, "objde1");
625 vm_object_pip_wait(object, "objde2");
629 * If the parent is locked we have to give up, as
630 * otherwise we would be acquiring locks in the
631 * wrong order and potentially deadlock.
633 if (temp->flags & OBJ_CHAINLOCK) {
634 vm_object_drop(temp);
637 vm_object_chain_acquire(temp);
640 * Recheck/retry after the hold and the paging
641 * wait, both of which can block us.
643 if (object->ref_count != 2 ||
644 object->shadow_count != 1 ||
646 LIST_FIRST(&object->shadow_head) != temp ||
647 (object->type != OBJT_DEFAULT &&
648 object->type != OBJT_SWAP)) {
649 vm_object_chain_release(temp);
650 vm_object_drop(temp);
655 * We can safely drop object's ref_count now.
657 KKASSERT(object->ref_count == 2);
661 * If our single parent is not collapseable just
662 * decrement ref_count (2->1) and stop.
664 if (temp->handle || (temp->type != OBJT_DEFAULT &&
665 temp->type != OBJT_SWAP)) {
666 vm_object_chain_release(temp);
667 vm_object_drop(temp);
672 * At this point we have already dropped object's
673 * ref_count so it is possible for a race to
674 * deallocate obj out from under us. Any collapse
675 * will re-check the situation. We must not block
676 * until we are able to collapse.
678 * Bump temp's ref_count to avoid an unwanted
679 * degenerate recursion (can't call
680 * vm_object_reference_locked() because it asserts
681 * that CHAINLOCK is not set).
684 KKASSERT(temp->ref_count > 1);
687 * Collapse temp, then deallocate the extra ref
690 vm_object_collapse(temp, &dlist);
691 vm_object_chain_release(temp);
693 vm_object_lock_swap();
694 vm_object_drop(object);
702 * Drop the ref and handle termination on the 1->0 transition.
703 * We may have blocked above so we have to recheck.
706 KKASSERT(object->ref_count != 0);
707 if (object->ref_count >= 2) {
711 KKASSERT(object->ref_count == 1);
714 * 1->0 transition. Chain through the backing_object.
715 * Maintain the ref until we've located the backing object,
718 while ((temp = object->backing_object) != NULL) {
719 vm_object_hold(temp);
720 if (temp == object->backing_object)
722 vm_object_drop(temp);
726 * 1->0 transition verified, retry if ref_count is no longer
727 * 1. Otherwise disconnect the backing_object (temp) and
730 if (object->ref_count != 1) {
731 vm_object_drop(temp);
736 * It shouldn't be possible for the object to be chain locked
737 * if we're removing the last ref on it.
739 KKASSERT((object->flags & OBJ_CHAINLOCK) == 0);
742 LIST_REMOVE(object, shadow_list);
743 temp->shadow_count--;
745 object->backing_object = NULL;
749 if ((object->flags & OBJ_DEAD) == 0)
750 vm_object_terminate(object);
751 if (must_drop && temp)
752 vm_object_lock_swap();
754 vm_object_drop(object);
758 if (must_drop && object)
759 vm_object_drop(object);
762 * Additional tail recursion on dlist. Avoid a recursion. Objects
763 * on the dlist have a hold count but are not locked.
765 if ((dtmp = dlist) != NULL) {
767 object = dtmp->object;
770 vm_object_lock(object); /* already held, add lock */
771 must_drop = 1; /* and we're responsible for it */
777 * Destroy the specified object, freeing up related resources.
779 * The object must have zero references.
781 * The object must held. The caller is responsible for dropping the object
782 * after terminate returns. Terminate does NOT drop the object.
784 static int vm_object_terminate_callback(vm_page_t p, void *data);
787 vm_object_terminate(vm_object_t object)
790 * Make sure no one uses us. Once we set OBJ_DEAD we should be
791 * able to safely block.
793 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
794 KKASSERT((object->flags & OBJ_DEAD) == 0);
795 vm_object_set_flag(object, OBJ_DEAD);
798 * Wait for the pageout daemon to be done with the object
800 vm_object_pip_wait(object, "objtrm1");
802 KASSERT(!object->paging_in_progress,
803 ("vm_object_terminate: pageout in progress"));
806 * Clean and free the pages, as appropriate. All references to the
807 * object are gone, so we don't need to lock it.
809 if (object->type == OBJT_VNODE) {
813 * Clean pages and flush buffers.
815 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
817 vp = (struct vnode *) object->handle;
818 vinvalbuf(vp, V_SAVE, 0, 0);
822 * Wait for any I/O to complete, after which there had better not
823 * be any references left on the object.
825 vm_object_pip_wait(object, "objtrm2");
827 if (object->ref_count != 0) {
828 panic("vm_object_terminate: object with references, "
829 "ref_count=%d", object->ref_count);
833 * Now free any remaining pages. For internal objects, this also
834 * removes them from paging queues. Don't free wired pages, just
835 * remove them from the object.
837 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
838 vm_object_terminate_callback, NULL);
841 * Let the pager know object is dead.
843 vm_pager_deallocate(object);
846 * Wait for the object hold count to hit 1, clean out pages as
847 * we go. vmobj_token interlocks any race conditions that might
848 * pick the object up from the vm_object_list after we have cleared
852 if (RB_ROOT(&object->rb_memq) == NULL)
854 kprintf("vm_object_terminate: Warning, object %p "
855 "still has %d pages\n",
856 object, object->resident_page_count);
857 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
858 vm_object_terminate_callback, NULL);
862 * There had better not be any pages left
864 KKASSERT(object->resident_page_count == 0);
867 * Remove the object from the global object list.
869 lwkt_gettoken(&vmobj_token);
870 TAILQ_REMOVE(&vm_object_list, object, object_list);
872 lwkt_reltoken(&vmobj_token);
873 vm_object_dead_wakeup(object);
875 if (object->ref_count != 0) {
876 panic("vm_object_terminate2: object with references, "
877 "ref_count=%d", object->ref_count);
881 * NOTE: The object hold_count is at least 1, so we cannot zfree()
882 * the object here. See vm_object_drop().
887 * The caller must hold the object.
890 vm_object_terminate_callback(vm_page_t p, void *data __unused)
895 vm_page_busy_wait(p, TRUE, "vmpgtrm");
896 if (object != p->object) {
897 kprintf("vm_object_terminate: Warning: Encountered "
898 "busied page %p on queue %d\n", p, p->queue);
900 } else if (p->wire_count == 0) {
902 mycpu->gd_cnt.v_pfree++;
904 if (p->queue != PQ_NONE)
905 kprintf("vm_object_terminate: Warning: Encountered "
906 "wired page %p on queue %d\n", p, p->queue);
915 * The object is dead but still has an object<->pager association. Sleep
916 * and return. The caller typically retests the association in a loop.
918 * The caller must hold the object.
921 vm_object_dead_sleep(vm_object_t object, const char *wmesg)
923 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
924 if (object->handle) {
925 vm_object_set_flag(object, OBJ_DEADWNT);
926 tsleep(object, 0, wmesg, 0);
927 /* object may be invalid after this point */
932 * Wakeup anyone waiting for the object<->pager disassociation on
935 * The caller must hold the object.
938 vm_object_dead_wakeup(vm_object_t object)
940 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
941 if (object->flags & OBJ_DEADWNT) {
942 vm_object_clear_flag(object, OBJ_DEADWNT);
948 * Clean all dirty pages in the specified range of object. Leaves page
949 * on whatever queue it is currently on. If NOSYNC is set then do not
950 * write out pages with PG_NOSYNC set (originally comes from MAP_NOSYNC),
951 * leaving the object dirty.
953 * When stuffing pages asynchronously, allow clustering. XXX we need a
954 * synchronous clustering mode implementation.
956 * Odd semantics: if start == end, we clean everything.
958 * The object must be locked? XXX
960 static int vm_object_page_clean_pass1(struct vm_page *p, void *data);
961 static int vm_object_page_clean_pass2(struct vm_page *p, void *data);
964 vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
967 struct rb_vm_page_scan_info info;
973 vm_object_hold(object);
974 if (object->type != OBJT_VNODE ||
975 (object->flags & OBJ_MIGHTBEDIRTY) == 0) {
976 vm_object_drop(object);
980 pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ?
981 VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
982 pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
987 * Interlock other major object operations. This allows us to
988 * temporarily clear OBJ_WRITEABLE and OBJ_MIGHTBEDIRTY.
990 vm_object_set_flag(object, OBJ_CLEANING);
993 * Handle 'entire object' case
995 info.start_pindex = start;
997 info.end_pindex = object->size - 1;
999 info.end_pindex = end - 1;
1001 wholescan = (start == 0 && info.end_pindex == object->size - 1);
1003 info.pagerflags = pagerflags;
1004 info.object = object;
1007 * If cleaning the entire object do a pass to mark the pages read-only.
1008 * If everything worked out ok, clear OBJ_WRITEABLE and
1013 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
1014 vm_object_page_clean_pass1, &info);
1015 if (info.error == 0) {
1016 vm_object_clear_flag(object,
1017 OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
1018 if (object->type == OBJT_VNODE &&
1019 (vp = (struct vnode *)object->handle) != NULL) {
1020 if (vp->v_flag & VOBJDIRTY)
1021 vclrflags(vp, VOBJDIRTY);
1027 * Do a pass to clean all the dirty pages we find.
1031 generation = object->generation;
1032 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
1033 vm_object_page_clean_pass2, &info);
1034 } while (info.error || generation != object->generation);
1036 vm_object_clear_flag(object, OBJ_CLEANING);
1037 vm_object_drop(object);
1041 * The caller must hold the object.
1045 vm_object_page_clean_pass1(struct vm_page *p, void *data)
1047 struct rb_vm_page_scan_info *info = data;
1049 vm_page_flag_set(p, PG_CLEANCHK);
1050 if ((info->limit & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
1052 } else if (vm_page_busy_try(p, FALSE) == 0) {
1053 vm_page_protect(p, VM_PROT_READ); /* must not block */
1063 * The caller must hold the object
1067 vm_object_page_clean_pass2(struct vm_page *p, void *data)
1069 struct rb_vm_page_scan_info *info = data;
1073 * Do not mess with pages that were inserted after we started
1074 * the cleaning pass.
1076 if ((p->flags & PG_CLEANCHK) == 0)
1079 generation = info->object->generation;
1080 vm_page_busy_wait(p, TRUE, "vpcwai");
1081 if (p->object != info->object ||
1082 info->object->generation != generation) {
1089 * Before wasting time traversing the pmaps, check for trivial
1090 * cases where the page cannot be dirty.
1092 if (p->valid == 0 || (p->queue - p->pc) == PQ_CACHE) {
1093 KKASSERT((p->dirty & p->valid) == 0);
1099 * Check whether the page is dirty or not. The page has been set
1100 * to be read-only so the check will not race a user dirtying the
1103 vm_page_test_dirty(p);
1104 if ((p->dirty & p->valid) == 0) {
1105 vm_page_flag_clear(p, PG_CLEANCHK);
1111 * If we have been asked to skip nosync pages and this is a
1112 * nosync page, skip it. Note that the object flags were
1113 * not cleared in this case (because pass1 will have returned an
1114 * error), so we do not have to set them.
1116 if ((info->limit & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
1117 vm_page_flag_clear(p, PG_CLEANCHK);
1123 * Flush as many pages as we can. PG_CLEANCHK will be cleared on
1124 * the pages that get successfully flushed. Set info->error if
1125 * we raced an object modification.
1127 vm_object_page_collect_flush(info->object, p, info->pagerflags);
1134 * Collect the specified page and nearby pages and flush them out.
1135 * The number of pages flushed is returned. The passed page is busied
1136 * by the caller and we are responsible for its disposition.
1138 * The caller must hold the object.
1141 vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int pagerflags)
1150 vm_page_t maf[vm_pageout_page_count];
1151 vm_page_t mab[vm_pageout_page_count];
1152 vm_page_t ma[vm_pageout_page_count];
1154 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
1159 for(i = 1; i < vm_pageout_page_count; i++) {
1162 tp = vm_page_lookup_busy_try(object, pi + i, TRUE, &error);
1167 if ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
1168 (tp->flags & PG_CLEANCHK) == 0) {
1172 if ((tp->queue - tp->pc) == PQ_CACHE) {
1173 vm_page_flag_clear(tp, PG_CLEANCHK);
1177 vm_page_test_dirty(tp);
1178 if ((tp->dirty & tp->valid) == 0) {
1179 vm_page_flag_clear(tp, PG_CLEANCHK);
1188 chkb = vm_pageout_page_count - maxf;
1190 * NOTE: chkb can be 0
1192 for(i = 1; chkb && i < chkb; i++) {
1195 tp = vm_page_lookup_busy_try(object, pi - i, TRUE, &error);
1200 if ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
1201 (tp->flags & PG_CLEANCHK) == 0) {
1205 if ((tp->queue - tp->pc) == PQ_CACHE) {
1206 vm_page_flag_clear(tp, PG_CLEANCHK);
1210 vm_page_test_dirty(tp);
1211 if ((tp->dirty & tp->valid) == 0) {
1212 vm_page_flag_clear(tp, PG_CLEANCHK);
1221 * All pages in the maf[] and mab[] array are busied.
1223 for (i = 0; i < maxb; i++) {
1224 int index = (maxb - i) - 1;
1226 vm_page_flag_clear(ma[index], PG_CLEANCHK);
1228 vm_page_flag_clear(p, PG_CLEANCHK);
1230 for(i = 0; i < maxf; i++) {
1231 int index = (maxb + i) + 1;
1233 vm_page_flag_clear(ma[index], PG_CLEANCHK);
1235 runlen = maxb + maxf + 1;
1237 for (i = 0; i < runlen; i++)
1238 vm_page_hold(ma[i]);
1240 vm_pageout_flush(ma, runlen, pagerflags);
1242 for (i = 0; i < runlen; i++) {
1243 if (ma[i]->valid & ma[i]->dirty) {
1244 vm_page_protect(ma[i], VM_PROT_READ);
1245 vm_page_flag_set(ma[i], PG_CLEANCHK);
1248 * maxf will end up being the actual number of pages
1249 * we wrote out contiguously, non-inclusive of the
1250 * first page. We do not count look-behind pages.
1252 if (i >= maxb + 1 && (maxf > i - maxb - 1))
1253 maxf = i - maxb - 1;
1255 vm_page_unhold(ma[i]);
1261 * Same as vm_object_pmap_copy, except range checking really
1262 * works, and is meant for small sections of an object.
1264 * This code protects resident pages by making them read-only
1265 * and is typically called on a fork or split when a page
1266 * is converted to copy-on-write.
1268 * NOTE: If the page is already at VM_PROT_NONE, calling
1269 * vm_page_protect will have no effect.
1272 vm_object_pmap_copy_1(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1277 if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
1280 vm_object_hold(object);
1281 for (idx = start; idx < end; idx++) {
1282 p = vm_page_lookup(object, idx);
1285 vm_page_protect(p, VM_PROT_READ);
1287 vm_object_drop(object);
1291 * Removes all physical pages in the specified object range from all
1294 * The object must *not* be locked.
1297 static int vm_object_pmap_remove_callback(vm_page_t p, void *data);
1300 vm_object_pmap_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1302 struct rb_vm_page_scan_info info;
1306 info.start_pindex = start;
1307 info.end_pindex = end - 1;
1309 vm_object_hold(object);
1310 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
1311 vm_object_pmap_remove_callback, &info);
1312 if (start == 0 && end == object->size)
1313 vm_object_clear_flag(object, OBJ_WRITEABLE);
1314 vm_object_drop(object);
1318 * The caller must hold the object
1321 vm_object_pmap_remove_callback(vm_page_t p, void *data __unused)
1323 vm_page_protect(p, VM_PROT_NONE);
1328 * Implements the madvise function at the object/page level.
1330 * MADV_WILLNEED (any object)
1332 * Activate the specified pages if they are resident.
1334 * MADV_DONTNEED (any object)
1336 * Deactivate the specified pages if they are resident.
1338 * MADV_FREE (OBJT_DEFAULT/OBJT_SWAP objects, OBJ_ONEMAPPING only)
1340 * Deactivate and clean the specified pages if they are
1341 * resident. This permits the process to reuse the pages
1342 * without faulting or the kernel to reclaim the pages
1348 vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
1350 vm_pindex_t end, tpindex;
1351 vm_object_t tobject;
1359 end = pindex + count;
1361 vm_object_hold(object);
1365 * Locate and adjust resident pages
1367 for (; pindex < end; pindex += 1) {
1369 if (tobject != object)
1370 vm_object_drop(tobject);
1375 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
1376 * and those pages must be OBJ_ONEMAPPING.
1378 if (advise == MADV_FREE) {
1379 if ((tobject->type != OBJT_DEFAULT &&
1380 tobject->type != OBJT_SWAP) ||
1381 (tobject->flags & OBJ_ONEMAPPING) == 0) {
1386 m = vm_page_lookup_busy_try(tobject, tpindex, TRUE, &error);
1389 vm_page_sleep_busy(m, TRUE, "madvpo");
1394 * There may be swap even if there is no backing page
1396 if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1397 swap_pager_freespace(tobject, tpindex, 1);
1402 while ((xobj = tobject->backing_object) != NULL) {
1403 KKASSERT(xobj != object);
1404 vm_object_hold(xobj);
1405 if (xobj == tobject->backing_object)
1407 vm_object_drop(xobj);
1411 tpindex += OFF_TO_IDX(tobject->backing_object_offset);
1412 if (tobject != object) {
1413 vm_object_lock_swap();
1414 vm_object_drop(tobject);
1421 * If the page is not in a normal active state, we skip it.
1422 * If the page is not managed there are no page queues to
1423 * mess with. Things can break if we mess with pages in
1424 * any of the below states.
1427 /*m->hold_count ||*/
1429 (m->flags & PG_UNMANAGED) ||
1430 m->valid != VM_PAGE_BITS_ALL
1437 * Theoretically once a page is known not to be busy, an
1438 * interrupt cannot come along and rip it out from under us.
1441 if (advise == MADV_WILLNEED) {
1442 vm_page_activate(m);
1443 } else if (advise == MADV_DONTNEED) {
1444 vm_page_dontneed(m);
1445 } else if (advise == MADV_FREE) {
1447 * Mark the page clean. This will allow the page
1448 * to be freed up by the system. However, such pages
1449 * are often reused quickly by malloc()/free()
1450 * so we do not do anything that would cause
1451 * a page fault if we can help it.
1453 * Specifically, we do not try to actually free
1454 * the page now nor do we try to put it in the
1455 * cache (which would cause a page fault on reuse).
1457 * But we do make the page is freeable as we
1458 * can without actually taking the step of unmapping
1461 pmap_clear_modify(m);
1464 vm_page_dontneed(m);
1465 if (tobject->type == OBJT_SWAP)
1466 swap_pager_freespace(tobject, tpindex, 1);
1470 if (tobject != object)
1471 vm_object_drop(tobject);
1472 vm_object_drop(object);
1476 * Create a new object which is backed by the specified existing object
1477 * range. Replace the pointer and offset that was pointing at the existing
1478 * object with the pointer/offset for the new object.
1480 * No other requirements.
1483 vm_object_shadow(vm_object_t *objectp, vm_ooffset_t *offset, vm_size_t length,
1492 * Don't create the new object if the old object isn't shared.
1493 * We have to chain wait before adding the reference to avoid
1494 * racing a collapse or deallocation.
1496 * Add the additional ref to source here to avoid racing a later
1497 * collapse or deallocation. Clear the ONEMAPPING flag whether
1498 * addref is TRUE or not in this case because the original object
1502 vm_object_hold(source);
1503 vm_object_chain_wait(source);
1504 if (source->ref_count == 1 &&
1505 source->handle == NULL &&
1506 (source->type == OBJT_DEFAULT ||
1507 source->type == OBJT_SWAP)) {
1508 vm_object_drop(source);
1510 vm_object_reference_locked(source);
1511 vm_object_clear_flag(source, OBJ_ONEMAPPING);
1515 vm_object_reference_locked(source);
1516 vm_object_clear_flag(source, OBJ_ONEMAPPING);
1520 * Allocate a new object with the given length. The new object
1521 * is returned referenced but we may have to add another one.
1522 * If we are adding a second reference we must clear OBJ_ONEMAPPING.
1523 * (typically because the caller is about to clone a vm_map_entry).
1525 * The source object currently has an extra reference to prevent
1526 * collapses into it while we mess with its shadow list, which
1527 * we will remove later in this routine.
1529 if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL)
1530 panic("vm_object_shadow: no object for shadowing");
1531 vm_object_hold(result);
1533 vm_object_reference_locked(result);
1534 vm_object_clear_flag(result, OBJ_ONEMAPPING);
1538 * The new object shadows the source object. Chain wait before
1539 * adjusting shadow_count or the shadow list to avoid races.
1541 * Try to optimize the result object's page color when shadowing
1542 * in order to maintain page coloring consistency in the combined
1545 KKASSERT(result->backing_object == NULL);
1546 result->backing_object = source;
1548 vm_object_chain_wait(source);
1549 LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1550 source->shadow_count++;
1551 source->generation++;
1553 /* cpu localization twist */
1554 result->pg_color = (int)(intptr_t)curthread;
1556 result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) &
1562 * Adjust the return storage. Drop the ref on source before
1565 result->backing_object_offset = *offset;
1566 vm_object_drop(result);
1569 vm_object_deallocate_locked(source);
1570 vm_object_drop(source);
1574 * Return the new things
1579 #define OBSC_TEST_ALL_SHADOWED 0x0001
1580 #define OBSC_COLLAPSE_NOWAIT 0x0002
1581 #define OBSC_COLLAPSE_WAIT 0x0004
1583 static int vm_object_backing_scan_callback(vm_page_t p, void *data);
1586 * The caller must hold the object.
1589 vm_object_backing_scan(vm_object_t object, vm_object_t backing_object, int op)
1591 struct rb_vm_page_scan_info info;
1593 vm_object_assert_held(object);
1594 vm_object_assert_held(backing_object);
1596 KKASSERT(backing_object == object->backing_object);
1597 info.backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
1600 * Initial conditions
1602 if (op & OBSC_TEST_ALL_SHADOWED) {
1604 * We do not want to have to test for the existence of
1605 * swap pages in the backing object. XXX but with the
1606 * new swapper this would be pretty easy to do.
1608 * XXX what about anonymous MAP_SHARED memory that hasn't
1609 * been ZFOD faulted yet? If we do not test for this, the
1610 * shadow test may succeed! XXX
1612 if (backing_object->type != OBJT_DEFAULT)
1615 if (op & OBSC_COLLAPSE_WAIT) {
1616 KKASSERT((backing_object->flags & OBJ_DEAD) == 0);
1617 vm_object_set_flag(backing_object, OBJ_DEAD);
1618 lwkt_gettoken(&vmobj_token);
1619 TAILQ_REMOVE(&vm_object_list, backing_object, object_list);
1621 lwkt_reltoken(&vmobj_token);
1622 vm_object_dead_wakeup(backing_object);
1626 * Our scan. We have to retry if a negative error code is returned,
1627 * otherwise 0 or 1 will be returned in info.error. 0 Indicates that
1628 * the scan had to be stopped because the parent does not completely
1631 info.object = object;
1632 info.backing_object = backing_object;
1636 vm_page_rb_tree_RB_SCAN(&backing_object->rb_memq, NULL,
1637 vm_object_backing_scan_callback,
1639 } while (info.error < 0);
1645 * The caller must hold the object.
1648 vm_object_backing_scan_callback(vm_page_t p, void *data)
1650 struct rb_vm_page_scan_info *info = data;
1651 vm_object_t backing_object;
1654 vm_pindex_t new_pindex;
1655 vm_pindex_t backing_offset_index;
1659 new_pindex = pindex - info->backing_offset_index;
1661 object = info->object;
1662 backing_object = info->backing_object;
1663 backing_offset_index = info->backing_offset_index;
1665 if (op & OBSC_TEST_ALL_SHADOWED) {
1669 * Ignore pages outside the parent object's range
1670 * and outside the parent object's mapping of the
1673 * note that we do not busy the backing object's
1676 if (pindex < backing_offset_index ||
1677 new_pindex >= object->size
1683 * See if the parent has the page or if the parent's
1684 * object pager has the page. If the parent has the
1685 * page but the page is not valid, the parent's
1686 * object pager must have the page.
1688 * If this fails, the parent does not completely shadow
1689 * the object and we might as well give up now.
1691 pp = vm_page_lookup(object, new_pindex);
1692 if ((pp == NULL || pp->valid == 0) &&
1693 !vm_pager_has_page(object, new_pindex)
1695 info->error = 0; /* problemo */
1696 return(-1); /* stop the scan */
1701 * Check for busy page. Note that we may have lost (p) when we
1702 * possibly blocked above.
1704 if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
1707 if (vm_page_busy_try(p, TRUE)) {
1708 if (op & OBSC_COLLAPSE_NOWAIT) {
1712 * If we slept, anything could have
1713 * happened. Ask that the scan be restarted.
1715 * Since the object is marked dead, the
1716 * backing offset should not have changed.
1718 vm_page_sleep_busy(p, TRUE, "vmocol");
1725 * If (p) is no longer valid restart the scan.
1727 if (p->object != backing_object || p->pindex != pindex) {
1728 kprintf("vm_object_backing_scan: Warning: page "
1729 "%p ripped out from under us\n", p);
1735 if (op & OBSC_COLLAPSE_NOWAIT) {
1736 if (p->valid == 0 /*|| p->hold_count*/ ||
1742 /* XXX what if p->valid == 0 , hold_count, etc? */
1746 p->object == backing_object,
1747 ("vm_object_qcollapse(): object mismatch")
1751 * Destroy any associated swap
1753 if (backing_object->type == OBJT_SWAP)
1754 swap_pager_freespace(backing_object, p->pindex, 1);
1757 p->pindex < backing_offset_index ||
1758 new_pindex >= object->size
1761 * Page is out of the parent object's range, we
1762 * can simply destroy it.
1764 vm_page_protect(p, VM_PROT_NONE);
1769 pp = vm_page_lookup(object, new_pindex);
1770 if (pp != NULL || vm_pager_has_page(object, new_pindex)) {
1772 * page already exists in parent OR swap exists
1773 * for this location in the parent. Destroy
1774 * the original page from the backing object.
1776 * Leave the parent's page alone
1778 vm_page_protect(p, VM_PROT_NONE);
1784 * Page does not exist in parent, rename the
1785 * page from the backing object to the main object.
1787 * If the page was mapped to a process, it can remain
1788 * mapped through the rename.
1790 if ((p->queue - p->pc) == PQ_CACHE)
1791 vm_page_deactivate(p);
1793 vm_page_rename(p, object, new_pindex);
1795 /* page automatically made dirty by rename */
1801 * This version of collapse allows the operation to occur earlier and
1802 * when paging_in_progress is true for an object... This is not a complete
1803 * operation, but should plug 99.9% of the rest of the leaks.
1805 * The caller must hold the object and backing_object and both must be
1808 * (only called from vm_object_collapse)
1811 vm_object_qcollapse(vm_object_t object, vm_object_t backing_object)
1813 if (backing_object->ref_count == 1) {
1814 backing_object->ref_count += 2;
1815 vm_object_backing_scan(object, backing_object,
1816 OBSC_COLLAPSE_NOWAIT);
1817 backing_object->ref_count -= 2;
1822 * Collapse an object with the object backing it. Pages in the backing
1823 * object are moved into the parent, and the backing object is deallocated.
1824 * Any conflict is resolved in favor of the parent's existing pages.
1826 * object must be held and chain-locked on call.
1828 * The caller must have an extra ref on object to prevent a race from
1829 * destroying it during the collapse.
1832 vm_object_collapse(vm_object_t object, struct vm_object_dealloc_list **dlistp)
1834 struct vm_object_dealloc_list *dlist = NULL;
1835 vm_object_t backing_object;
1838 * Only one thread is attempting a collapse at any given moment.
1839 * There are few restrictions for (object) that callers of this
1840 * function check so reentrancy is likely.
1842 KKASSERT(object != NULL);
1843 vm_object_assert_held(object);
1844 KKASSERT(object->flags & OBJ_CHAINLOCK);
1851 * We have to hold the backing object, check races.
1853 while ((backing_object = object->backing_object) != NULL) {
1854 vm_object_hold(backing_object);
1855 if (backing_object == object->backing_object)
1857 vm_object_drop(backing_object);
1861 * No backing object? Nothing to collapse then.
1863 if (backing_object == NULL)
1867 * You can't collapse with a non-default/non-swap object.
1869 if (backing_object->type != OBJT_DEFAULT &&
1870 backing_object->type != OBJT_SWAP) {
1871 vm_object_drop(backing_object);
1872 backing_object = NULL;
1877 * Chain-lock the backing object too because if we
1878 * successfully merge its pages into the top object we
1879 * will collapse backing_object->backing_object as the
1880 * new backing_object. Re-check that it is still our
1883 vm_object_chain_acquire(backing_object);
1884 if (backing_object != object->backing_object) {
1885 vm_object_chain_release(backing_object);
1886 vm_object_drop(backing_object);
1891 * we check the backing object first, because it is most likely
1894 if (backing_object->handle != NULL ||
1895 (backing_object->type != OBJT_DEFAULT &&
1896 backing_object->type != OBJT_SWAP) ||
1897 (backing_object->flags & OBJ_DEAD) ||
1898 object->handle != NULL ||
1899 (object->type != OBJT_DEFAULT &&
1900 object->type != OBJT_SWAP) ||
1901 (object->flags & OBJ_DEAD)) {
1906 * If paging is in progress we can't do a normal collapse.
1909 object->paging_in_progress != 0 ||
1910 backing_object->paging_in_progress != 0
1912 vm_object_qcollapse(object, backing_object);
1917 * We know that we can either collapse the backing object (if
1918 * the parent is the only reference to it) or (perhaps) have
1919 * the parent bypass the object if the parent happens to shadow
1920 * all the resident pages in the entire backing object.
1922 * This is ignoring pager-backed pages such as swap pages.
1923 * vm_object_backing_scan fails the shadowing test in this
1926 if (backing_object->ref_count == 1) {
1928 * If there is exactly one reference to the backing
1929 * object, we can collapse it into the parent.
1931 KKASSERT(object->backing_object == backing_object);
1932 vm_object_backing_scan(object, backing_object,
1933 OBSC_COLLAPSE_WAIT);
1936 * Move the pager from backing_object to object.
1938 if (backing_object->type == OBJT_SWAP) {
1939 vm_object_pip_add(backing_object, 1);
1942 * scrap the paging_offset junk and do a
1943 * discrete copy. This also removes major
1944 * assumptions about how the swap-pager
1945 * works from where it doesn't belong. The
1946 * new swapper is able to optimize the
1947 * destroy-source case.
1949 vm_object_pip_add(object, 1);
1950 swap_pager_copy(backing_object, object,
1951 OFF_TO_IDX(object->backing_object_offset),
1953 vm_object_pip_wakeup(object);
1954 vm_object_pip_wakeup(backing_object);
1958 * Object now shadows whatever backing_object did.
1959 * Remove object from backing_object's shadow_list.
1961 LIST_REMOVE(object, shadow_list);
1962 KKASSERT(object->backing_object == backing_object);
1963 backing_object->shadow_count--;
1964 backing_object->generation++;
1967 * backing_object->backing_object moves from within
1968 * backing_object to within object.
1970 while ((bbobj = backing_object->backing_object) != NULL) {
1971 vm_object_hold(bbobj);
1972 if (bbobj == backing_object->backing_object)
1974 vm_object_drop(bbobj);
1977 LIST_REMOVE(backing_object, shadow_list);
1978 bbobj->shadow_count--;
1979 bbobj->generation++;
1980 backing_object->backing_object = NULL;
1982 object->backing_object = bbobj;
1984 LIST_INSERT_HEAD(&bbobj->shadow_head,
1985 object, shadow_list);
1986 bbobj->shadow_count++;
1987 bbobj->generation++;
1990 object->backing_object_offset +=
1991 backing_object->backing_object_offset;
1993 vm_object_drop(bbobj);
1996 * Discard the old backing_object. Nothing should be
1997 * able to ref it, other than a vm_map_split(),
1998 * and vm_map_split() will stall on our chain lock.
1999 * And we control the parent so it shouldn't be
2000 * possible for it to go away either.
2002 * Since the backing object has no pages, no pager
2003 * left, and no object references within it, all
2004 * that is necessary is to dispose of it.
2006 KASSERT(backing_object->ref_count == 1,
2007 ("backing_object %p was somehow "
2008 "re-referenced during collapse!",
2010 KASSERT(RB_EMPTY(&backing_object->rb_memq),
2011 ("backing_object %p somehow has left "
2012 "over pages during collapse!",
2016 * The object can be destroyed.
2018 * XXX just fall through and dodealloc instead
2019 * of forcing destruction?
2021 --backing_object->ref_count;
2022 if ((backing_object->flags & OBJ_DEAD) == 0)
2023 vm_object_terminate(backing_object);
2028 * If we do not entirely shadow the backing object,
2029 * there is nothing we can do so we give up.
2031 if (vm_object_backing_scan(object, backing_object,
2032 OBSC_TEST_ALL_SHADOWED) == 0) {
2037 * bbobj is backing_object->backing_object. Since
2038 * object completely shadows backing_object we can
2039 * bypass it and become backed by bbobj instead.
2041 while ((bbobj = backing_object->backing_object) != NULL) {
2042 vm_object_hold(bbobj);
2043 if (bbobj == backing_object->backing_object)
2045 vm_object_drop(bbobj);
2049 * Make object shadow bbobj instead of backing_object.
2050 * Remove object from backing_object's shadow list.
2052 * Deallocating backing_object will not remove
2053 * it, since its reference count is at least 2.
2055 KKASSERT(object->backing_object == backing_object);
2056 LIST_REMOVE(object, shadow_list);
2057 backing_object->shadow_count--;
2058 backing_object->generation++;
2061 * Add a ref to bbobj, bbobj now shadows object.
2063 * NOTE: backing_object->backing_object still points
2064 * to bbobj. That relationship remains intact
2065 * because backing_object has > 1 ref, so
2066 * someone else is pointing to it (hence why
2067 * we can't collapse it into object and can
2068 * only handle the all-shadowed bypass case).
2071 vm_object_chain_wait(bbobj);
2072 vm_object_reference_locked(bbobj);
2073 LIST_INSERT_HEAD(&bbobj->shadow_head,
2074 object, shadow_list);
2075 bbobj->shadow_count++;
2076 bbobj->generation++;
2077 object->backing_object_offset +=
2078 backing_object->backing_object_offset;
2079 object->backing_object = bbobj;
2080 vm_object_drop(bbobj);
2082 object->backing_object = NULL;
2086 * Drop the reference count on backing_object. To
2087 * handle ref_count races properly we can't assume
2088 * that the ref_count is still at least 2 so we
2089 * have to actually call vm_object_deallocate()
2090 * (after clearing the chainlock).
2097 * Ok, we want to loop on the new object->bbobj association,
2098 * possibly collapsing it further. However if dodealloc is
2099 * non-zero we have to deallocate the backing_object which
2100 * itself can potentially undergo a collapse, creating a
2101 * recursion depth issue with the LWKT token subsystem.
2103 * In the case where we must deallocate the backing_object
2104 * it is possible now that the backing_object has a single
2105 * shadow count on some other object (not represented here
2106 * as yet), since it no longer shadows us. Thus when we
2107 * call vm_object_deallocate() it may attempt to collapse
2108 * itself into its remaining parent.
2111 struct vm_object_dealloc_list *dtmp;
2113 vm_object_chain_release(backing_object);
2114 vm_object_unlock(backing_object);
2115 /* backing_object remains held */
2118 * Auto-deallocation list for caller convenience.
2123 dtmp = kmalloc(sizeof(*dtmp), M_TEMP, M_WAITOK);
2124 dtmp->object = backing_object;
2125 dtmp->next = *dlistp;
2128 vm_object_chain_release(backing_object);
2129 vm_object_drop(backing_object);
2131 /* backing_object = NULL; not needed */
2136 * Clean up any left over backing_object
2138 if (backing_object) {
2139 vm_object_chain_release(backing_object);
2140 vm_object_drop(backing_object);
2144 * Clean up any auto-deallocation list. This is a convenience
2145 * for top-level callers so they don't have to pass &dlist.
2146 * Do not clean up any caller-passed dlistp, the caller will
2150 vm_object_deallocate_list(&dlist);
2155 * vm_object_collapse() may collect additional objects in need of
2156 * deallocation. This routine deallocates these objects. The
2157 * deallocation itself can trigger additional collapses (which the
2158 * deallocate function takes care of). This procedure is used to
2159 * reduce procedural recursion since these vm_object shadow chains
2160 * can become quite long.
2163 vm_object_deallocate_list(struct vm_object_dealloc_list **dlistp)
2165 struct vm_object_dealloc_list *dlist;
2167 while ((dlist = *dlistp) != NULL) {
2168 *dlistp = dlist->next;
2169 vm_object_lock(dlist->object);
2170 vm_object_deallocate_locked(dlist->object);
2171 vm_object_drop(dlist->object);
2172 kfree(dlist, M_TEMP);
2177 * Removes all physical pages in the specified object range from the
2178 * object's list of pages.
2182 static int vm_object_page_remove_callback(vm_page_t p, void *data);
2185 vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
2186 boolean_t clean_only)
2188 struct rb_vm_page_scan_info info;
2192 * Degenerate cases and assertions
2194 vm_object_hold(object);
2195 if (object == NULL ||
2196 (object->resident_page_count == 0 && object->swblock_count == 0)) {
2197 vm_object_drop(object);
2200 KASSERT(object->type != OBJT_PHYS,
2201 ("attempt to remove pages from a physical object"));
2204 * Indicate that paging is occuring on the object
2206 vm_object_pip_add(object, 1);
2209 * Figure out the actual removal range and whether we are removing
2210 * the entire contents of the object or not. If removing the entire
2211 * contents, be sure to get all pages, even those that might be
2212 * beyond the end of the object.
2214 info.start_pindex = start;
2216 info.end_pindex = (vm_pindex_t)-1;
2218 info.end_pindex = end - 1;
2219 info.limit = clean_only;
2220 all = (start == 0 && info.end_pindex >= object->size - 1);
2223 * Loop until we are sure we have gotten them all.
2227 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
2228 vm_object_page_remove_callback, &info);
2229 } while (info.error);
2232 * Remove any related swap if throwing away pages, or for
2233 * non-swap objects (the swap is a clean copy in that case).
2235 if (object->type != OBJT_SWAP || clean_only == FALSE) {
2237 swap_pager_freespace_all(object);
2239 swap_pager_freespace(object, info.start_pindex,
2240 info.end_pindex - info.start_pindex + 1);
2246 vm_object_pip_wakeup(object);
2247 vm_object_drop(object);
2251 * The caller must hold the object
2254 vm_object_page_remove_callback(vm_page_t p, void *data)
2256 struct rb_vm_page_scan_info *info = data;
2258 if (vm_page_busy_try(p, TRUE)) {
2259 vm_page_sleep_busy(p, TRUE, "vmopar");
2265 * Wired pages cannot be destroyed, but they can be invalidated
2266 * and we do so if clean_only (limit) is not set.
2268 * WARNING! The page may be wired due to being part of a buffer
2269 * cache buffer, and the buffer might be marked B_CACHE.
2270 * This is fine as part of a truncation but VFSs must be
2271 * sure to fix the buffer up when re-extending the file.
2273 if (p->wire_count != 0) {
2274 vm_page_protect(p, VM_PROT_NONE);
2275 if (info->limit == 0)
2282 * limit is our clean_only flag. If set and the page is dirty, do
2283 * not free it. If set and the page is being held by someone, do
2286 if (info->limit && p->valid) {
2287 vm_page_test_dirty(p);
2288 if (p->valid & p->dirty) {
2293 if (p->hold_count) {
2303 vm_page_protect(p, VM_PROT_NONE);
2309 * Coalesces two objects backing up adjoining regions of memory into a
2312 * returns TRUE if objects were combined.
2314 * NOTE: Only works at the moment if the second object is NULL -
2315 * if it's not, which object do we lock first?
2318 * prev_object First object to coalesce
2319 * prev_offset Offset into prev_object
2320 * next_object Second object into coalesce
2321 * next_offset Offset into next_object
2323 * prev_size Size of reference to prev_object
2324 * next_size Size of reference to next_object
2326 * The caller does not need to hold (prev_object) but must have a stable
2327 * pointer to it (typically by holding the vm_map locked).
2330 vm_object_coalesce(vm_object_t prev_object, vm_pindex_t prev_pindex,
2331 vm_size_t prev_size, vm_size_t next_size)
2333 vm_pindex_t next_pindex;
2335 if (prev_object == NULL)
2338 vm_object_hold(prev_object);
2340 if (prev_object->type != OBJT_DEFAULT &&
2341 prev_object->type != OBJT_SWAP) {
2342 vm_object_drop(prev_object);
2347 * Try to collapse the object first
2349 vm_object_chain_acquire(prev_object);
2350 vm_object_collapse(prev_object, NULL);
2353 * Can't coalesce if: . more than one reference . paged out . shadows
2354 * another object . has a copy elsewhere (any of which mean that the
2355 * pages not mapped to prev_entry may be in use anyway)
2358 if (prev_object->backing_object != NULL) {
2359 vm_object_chain_release(prev_object);
2360 vm_object_drop(prev_object);
2364 prev_size >>= PAGE_SHIFT;
2365 next_size >>= PAGE_SHIFT;
2366 next_pindex = prev_pindex + prev_size;
2368 if ((prev_object->ref_count > 1) &&
2369 (prev_object->size != next_pindex)) {
2370 vm_object_chain_release(prev_object);
2371 vm_object_drop(prev_object);
2376 * Remove any pages that may still be in the object from a previous
2379 if (next_pindex < prev_object->size) {
2380 vm_object_page_remove(prev_object,
2382 next_pindex + next_size, FALSE);
2383 if (prev_object->type == OBJT_SWAP)
2384 swap_pager_freespace(prev_object,
2385 next_pindex, next_size);
2389 * Extend the object if necessary.
2391 if (next_pindex + next_size > prev_object->size)
2392 prev_object->size = next_pindex + next_size;
2394 vm_object_chain_release(prev_object);
2395 vm_object_drop(prev_object);
2400 * Make the object writable and flag is being possibly dirty.
2402 * The caller must hold the object. XXX called from vm_page_dirty(),
2403 * There is currently no requirement to hold the object.
2406 vm_object_set_writeable_dirty(vm_object_t object)
2410 /*vm_object_assert_held(object);*/
2412 * Avoid contention in vm fault path by checking the state before
2413 * issuing an atomic op on it.
2415 if ((object->flags & (OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY)) !=
2416 (OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY)) {
2417 vm_object_set_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
2419 if (object->type == OBJT_VNODE &&
2420 (vp = (struct vnode *)object->handle) != NULL) {
2421 if ((vp->v_flag & VOBJDIRTY) == 0) {
2422 vsetflags(vp, VOBJDIRTY);
2427 #include "opt_ddb.h"
2429 #include <sys/kernel.h>
2431 #include <sys/cons.h>
2433 #include <ddb/ddb.h>
2435 static int _vm_object_in_map (vm_map_t map, vm_object_t object,
2436 vm_map_entry_t entry);
2437 static int vm_object_in_map (vm_object_t object);
2440 * The caller must hold the object.
2443 _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
2446 vm_map_entry_t tmpe;
2447 vm_object_t obj, nobj;
2453 tmpe = map->header.next;
2454 entcount = map->nentries;
2455 while (entcount-- && (tmpe != &map->header)) {
2456 if( _vm_object_in_map(map, object, tmpe)) {
2463 switch(entry->maptype) {
2464 case VM_MAPTYPE_SUBMAP:
2465 tmpm = entry->object.sub_map;
2466 tmpe = tmpm->header.next;
2467 entcount = tmpm->nentries;
2468 while (entcount-- && tmpe != &tmpm->header) {
2469 if( _vm_object_in_map(tmpm, object, tmpe)) {
2475 case VM_MAPTYPE_NORMAL:
2476 case VM_MAPTYPE_VPAGETABLE:
2477 obj = entry->object.vm_object;
2479 if (obj == object) {
2480 if (obj != entry->object.vm_object)
2481 vm_object_drop(obj);
2484 while ((nobj = obj->backing_object) != NULL) {
2485 vm_object_hold(nobj);
2486 if (nobj == obj->backing_object)
2488 vm_object_drop(nobj);
2490 if (obj != entry->object.vm_object) {
2492 vm_object_lock_swap();
2493 vm_object_drop(obj);
2504 static int vm_object_in_map_callback(struct proc *p, void *data);
2506 struct vm_object_in_map_info {
2515 vm_object_in_map(vm_object_t object)
2517 struct vm_object_in_map_info info;
2520 info.object = object;
2522 allproc_scan(vm_object_in_map_callback, &info);
2525 if( _vm_object_in_map(&kernel_map, object, 0))
2527 if( _vm_object_in_map(&pager_map, object, 0))
2529 if( _vm_object_in_map(&buffer_map, object, 0))
2538 vm_object_in_map_callback(struct proc *p, void *data)
2540 struct vm_object_in_map_info *info = data;
2543 if (_vm_object_in_map(&p->p_vmspace->vm_map, info->object, 0)) {
2551 DB_SHOW_COMMAND(vmochk, vm_object_check)
2556 * make sure that internal objs are in a map somewhere
2557 * and none have zero ref counts.
2559 for (object = TAILQ_FIRST(&vm_object_list);
2561 object = TAILQ_NEXT(object, object_list)) {
2562 if (object->type == OBJT_MARKER)
2564 if (object->handle == NULL &&
2565 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2566 if (object->ref_count == 0) {
2567 db_printf("vmochk: internal obj has zero ref count: %ld\n",
2568 (long)object->size);
2570 if (!vm_object_in_map(object)) {
2572 "vmochk: internal obj is not in a map: "
2573 "ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2574 object->ref_count, (u_long)object->size,
2575 (u_long)object->size,
2576 (void *)object->backing_object);
2585 DB_SHOW_COMMAND(object, vm_object_print_static)
2587 /* XXX convert args. */
2588 vm_object_t object = (vm_object_t)addr;
2589 boolean_t full = have_addr;
2593 /* XXX count is an (unused) arg. Avoid shadowing it. */
2594 #define count was_count
2602 "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n",
2603 object, (int)object->type, (u_long)object->size,
2604 object->resident_page_count, object->ref_count, object->flags);
2606 * XXX no %qd in kernel. Truncate object->backing_object_offset.
2608 db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%lx\n",
2609 object->shadow_count,
2610 object->backing_object ? object->backing_object->ref_count : 0,
2611 object->backing_object, (long)object->backing_object_offset);
2618 RB_FOREACH(p, vm_page_rb_tree, &object->rb_memq) {
2620 db_iprintf("memory:=");
2621 else if (count == 6) {
2629 db_printf("(off=0x%lx,page=0x%lx)",
2630 (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
2641 * XXX need this non-static entry for calling from vm_map_print.
2646 vm_object_print(/* db_expr_t */ long addr,
2647 boolean_t have_addr,
2648 /* db_expr_t */ long count,
2651 vm_object_print_static(addr, have_addr, count, modif);
2657 DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
2662 for (object = TAILQ_FIRST(&vm_object_list);
2664 object = TAILQ_NEXT(object, object_list)) {
2665 vm_pindex_t idx, fidx;
2667 vm_paddr_t pa = -1, padiff;
2671 if (object->type == OBJT_MARKER)
2673 db_printf("new object: %p\n", (void *)object);
2683 osize = object->size;
2686 for (idx = 0; idx < osize; idx++) {
2687 m = vm_page_lookup(object, idx);
2690 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2691 (long)fidx, rcount, (long)pa);
2706 (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
2711 padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
2712 padiff >>= PAGE_SHIFT;
2713 padiff &= PQ_L2_MASK;
2715 pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
2719 db_printf(" index(%ld)run(%d)pa(0x%lx)",
2720 (long)fidx, rcount, (long)pa);
2721 db_printf("pd(%ld)\n", (long)padiff);
2731 pa = VM_PAGE_TO_PHYS(m);
2735 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2736 (long)fidx, rcount, (long)pa);