2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * The Mach Operating System project at Carnegie-Mellon University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * from: @(#)vm_object.c 8.5 (Berkeley) 3/22/94
35 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36 * All rights reserved.
38 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
40 * Permission to use, copy, modify and distribute this software and
41 * its documentation is hereby granted, provided that both the copyright
42 * notice and this permission notice appear in all copies of the
43 * software, derivative works or modified versions, and any portions
44 * thereof, and that both notices appear in supporting documentation.
46 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
50 * Carnegie Mellon requests users of this software to return to
52 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
53 * School of Computer Science
54 * Carnegie Mellon University
55 * Pittsburgh PA 15213-3890
57 * any improvements or extensions that they make and grant Carnegie the
58 * rights to redistribute these changes.
62 * Virtual memory object module.
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
68 #include <sys/param.h>
69 #include <sys/systm.h>
72 #include <sys/mount.h>
73 #include <sys/kernel.h>
74 #include <sys/sysctl.h>
75 #include <sys/mutex.h>
76 #include <sys/proc.h> /* for curproc, pageproc */
77 #include <sys/socket.h>
78 #include <sys/vnode.h>
79 #include <sys/vmmeter.h>
83 #include <vm/vm_param.h>
85 #include <vm/vm_map.h>
86 #include <vm/vm_object.h>
87 #include <vm/vm_page.h>
88 #include <vm/vm_pageout.h>
89 #include <vm/vm_pager.h>
90 #include <vm/swap_pager.h>
91 #include <vm/vm_kern.h>
92 #include <vm/vm_extern.h>
95 #define EASY_SCAN_FACTOR 8
97 #define MSYNC_FLUSH_HARDSEQ 0x01
98 #define MSYNC_FLUSH_SOFTSEQ 0x02
101 * msync / VM object flushing optimizations
103 static int msync_flush_flags = MSYNC_FLUSH_HARDSEQ | MSYNC_FLUSH_SOFTSEQ;
104 SYSCTL_INT(_vm, OID_AUTO, msync_flush_flags,
105 CTLFLAG_RW, &msync_flush_flags, 0, "");
107 static int old_msync;
108 SYSCTL_INT(_vm, OID_AUTO, old_msync, CTLFLAG_RW, &old_msync, 0,
109 "Use old (insecure) msync behavior");
111 static void vm_object_qcollapse(vm_object_t object);
112 static int vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags);
113 static void vm_object_vndeallocate(vm_object_t object);
116 * Virtual memory objects maintain the actual data
117 * associated with allocated virtual memory. A given
118 * page of memory exists within exactly one object.
120 * An object is only deallocated when all "references"
121 * are given up. Only one "reference" to a given
122 * region of an object should be writeable.
124 * Associated with each object is a list of all resident
125 * memory pages belonging to that object; this list is
126 * maintained by the "vm_page" module, and locked by the object's
129 * Each object also records a "pager" routine which is
130 * used to retrieve (and store) pages to the proper backing
131 * storage. In addition, objects may be backed by other
132 * objects from which they were virtual-copied.
134 * The only items within the object structure which are
135 * modified after time of creation are:
136 * reference count locked by object's lock
137 * pager routine locked by object's lock
141 struct object_q vm_object_list;
142 struct mtx vm_object_list_mtx; /* lock for object list and count */
144 struct vm_object kernel_object_store;
145 struct vm_object kmem_object_store;
147 SYSCTL_NODE(_vm_stats, OID_AUTO, object, CTLFLAG_RD, 0, "VM object stats");
149 static long object_collapses;
150 SYSCTL_LONG(_vm_stats_object, OID_AUTO, collapses, CTLFLAG_RD,
151 &object_collapses, 0, "VM object collapses");
153 static long object_bypasses;
154 SYSCTL_LONG(_vm_stats_object, OID_AUTO, bypasses, CTLFLAG_RD,
155 &object_bypasses, 0, "VM object bypasses");
158 * next_index determines the page color that is assigned to the next
159 * allocated object. Accesses to next_index are not synchronized
160 * because the effects of two or more object allocations using
161 * next_index simultaneously are inconsequential. At any given time,
162 * numerous objects have the same page color.
164 static int next_index;
166 static uma_zone_t obj_zone;
168 static int vm_object_zinit(void *mem, int size, int flags);
171 static void vm_object_zdtor(void *mem, int size, void *arg);
174 vm_object_zdtor(void *mem, int size, void *arg)
178 object = (vm_object_t)mem;
179 KASSERT(TAILQ_EMPTY(&object->memq),
180 ("object %p has resident pages",
182 KASSERT(object->paging_in_progress == 0,
183 ("object %p paging_in_progress = %d",
184 object, object->paging_in_progress));
185 KASSERT(object->resident_page_count == 0,
186 ("object %p resident_page_count = %d",
187 object, object->resident_page_count));
188 KASSERT(object->shadow_count == 0,
189 ("object %p shadow_count = %d",
190 object, object->shadow_count));
195 vm_object_zinit(void *mem, int size, int flags)
199 object = (vm_object_t)mem;
200 bzero(&object->mtx, sizeof(object->mtx));
201 VM_OBJECT_LOCK_INIT(object, "standard object");
203 /* These are true for any object that has been freed */
204 object->paging_in_progress = 0;
205 object->resident_page_count = 0;
206 object->shadow_count = 0;
211 _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
215 TAILQ_INIT(&object->memq);
216 LIST_INIT(&object->shadow_head);
221 object->generation = 1;
222 object->ref_count = 1;
224 if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
225 object->flags = OBJ_ONEMAPPING;
229 object->pg_color = next_index;
230 next_index = (object->pg_color + incr) & PQ_COLORMASK;
231 object->handle = NULL;
232 object->backing_object = NULL;
233 object->backing_object_offset = (vm_ooffset_t) 0;
235 mtx_lock(&vm_object_list_mtx);
236 TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
237 mtx_unlock(&vm_object_list_mtx);
243 * Initialize the VM objects module.
248 TAILQ_INIT(&vm_object_list);
249 mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF);
251 VM_OBJECT_LOCK_INIT(&kernel_object_store, "kernel object");
252 _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
255 VM_OBJECT_LOCK_INIT(&kmem_object_store, "kmem object");
256 _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
260 * The lock portion of struct vm_object must be type stable due
261 * to vm_pageout_fallback_object_lock locking a vm object
262 * without holding any references to it.
264 obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL,
270 vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM|UMA_ZONE_NOFREE);
274 vm_object_clear_flag(vm_object_t object, u_short bits)
277 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
278 object->flags &= ~bits;
282 vm_object_pip_add(vm_object_t object, short i)
285 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
286 object->paging_in_progress += i;
290 vm_object_pip_subtract(vm_object_t object, short i)
293 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
294 object->paging_in_progress -= i;
298 vm_object_pip_wakeup(vm_object_t object)
301 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
302 object->paging_in_progress--;
303 if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
304 vm_object_clear_flag(object, OBJ_PIPWNT);
310 vm_object_pip_wakeupn(vm_object_t object, short i)
313 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
315 object->paging_in_progress -= i;
316 if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
317 vm_object_clear_flag(object, OBJ_PIPWNT);
323 vm_object_pip_wait(vm_object_t object, char *waitid)
326 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
327 while (object->paging_in_progress) {
328 object->flags |= OBJ_PIPWNT;
329 msleep(object, VM_OBJECT_MTX(object), PVM, waitid, 0);
334 * vm_object_allocate:
336 * Returns a new object with the given size.
339 vm_object_allocate(objtype_t type, vm_pindex_t size)
343 object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK);
344 _vm_object_allocate(type, size, object);
350 * vm_object_reference:
352 * Gets another reference to the given object. Note: OBJ_DEAD
353 * objects can be referenced during final cleaning.
356 vm_object_reference(vm_object_t object)
362 VM_OBJECT_LOCK(object);
364 if (object->type == OBJT_VNODE) {
368 VM_OBJECT_UNLOCK(object);
369 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
370 vget(vp, LK_RETRY, curthread);
371 VFS_UNLOCK_GIANT(vfslocked);
373 VM_OBJECT_UNLOCK(object);
377 * vm_object_reference_locked:
379 * Gets another reference to the given object.
381 * The object must be locked.
384 vm_object_reference_locked(vm_object_t object)
388 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
389 KASSERT((object->flags & OBJ_DEAD) == 0,
390 ("vm_object_reference_locked: dead object referenced"));
392 if (object->type == OBJT_VNODE) {
399 * Handle deallocating an object of type OBJT_VNODE.
402 vm_object_vndeallocate(vm_object_t object)
404 struct vnode *vp = (struct vnode *) object->handle;
406 VFS_ASSERT_GIANT(vp->v_mount);
407 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
408 KASSERT(object->type == OBJT_VNODE,
409 ("vm_object_vndeallocate: not a vnode object"));
410 KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
412 if (object->ref_count == 0) {
413 vprint("vm_object_vndeallocate", vp);
414 panic("vm_object_vndeallocate: bad object reference count");
419 if (object->ref_count == 0) {
420 mp_fixme("Unlocked vflag access.");
421 vp->v_vflag &= ~VV_TEXT;
423 VM_OBJECT_UNLOCK(object);
425 * vrele may need a vop lock
431 * vm_object_deallocate:
433 * Release a reference to the specified object,
434 * gained either through a vm_object_allocate
435 * or a vm_object_reference call. When all references
436 * are gone, storage associated with this object
437 * may be relinquished.
439 * No object may be locked.
442 vm_object_deallocate(vm_object_t object)
446 while (object != NULL) {
451 VM_OBJECT_LOCK(object);
452 if (object->type == OBJT_VNODE) {
453 struct vnode *vp = (struct vnode *) object->handle;
456 * Conditionally acquire Giant for a vnode-backed
457 * object. We have to be careful since the type of
458 * a vnode object can change while the object is
461 if (VFS_NEEDSGIANT(vp->v_mount) && !vfslocked) {
463 if (!mtx_trylock(&Giant)) {
464 VM_OBJECT_UNLOCK(object);
469 vm_object_vndeallocate(object);
470 VFS_UNLOCK_GIANT(vfslocked);
474 * This is to handle the case that the object
475 * changed type while we dropped its lock to
478 VFS_UNLOCK_GIANT(vfslocked);
480 KASSERT(object->ref_count != 0,
481 ("vm_object_deallocate: object deallocated too many times: %d", object->type));
484 * If the reference count goes to 0 we start calling
485 * vm_object_terminate() on the object chain.
486 * A ref count of 1 may be a special case depending on the
487 * shadow count being 0 or 1.
490 if (object->ref_count > 1) {
491 VM_OBJECT_UNLOCK(object);
493 } else if (object->ref_count == 1) {
494 if (object->shadow_count == 0) {
495 vm_object_set_flag(object, OBJ_ONEMAPPING);
496 } else if ((object->shadow_count == 1) &&
497 (object->handle == NULL) &&
498 (object->type == OBJT_DEFAULT ||
499 object->type == OBJT_SWAP)) {
502 robject = LIST_FIRST(&object->shadow_head);
503 KASSERT(robject != NULL,
504 ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
506 object->shadow_count));
507 if (!VM_OBJECT_TRYLOCK(robject)) {
509 * Avoid a potential deadlock.
512 VM_OBJECT_UNLOCK(object);
514 * More likely than not the thread
515 * holding robject's lock has lower
516 * priority than the current thread.
517 * Let the lower priority thread run.
519 tsleep(&proc0, PVM, "vmo_de", 1);
523 * Collapse object into its shadow unless its
524 * shadow is dead. In that case, object will
525 * be deallocated by the thread that is
526 * deallocating its shadow.
528 if ((robject->flags & OBJ_DEAD) == 0 &&
529 (robject->handle == NULL) &&
530 (robject->type == OBJT_DEFAULT ||
531 robject->type == OBJT_SWAP)) {
533 robject->ref_count++;
535 if (robject->paging_in_progress) {
536 VM_OBJECT_UNLOCK(object);
537 vm_object_pip_wait(robject,
539 temp = robject->backing_object;
540 if (object == temp) {
541 VM_OBJECT_LOCK(object);
544 } else if (object->paging_in_progress) {
545 VM_OBJECT_UNLOCK(robject);
546 object->flags |= OBJ_PIPWNT;
548 VM_OBJECT_MTX(object),
549 PDROP | PVM, "objde2", 0);
550 VM_OBJECT_LOCK(robject);
551 temp = robject->backing_object;
552 if (object == temp) {
553 VM_OBJECT_LOCK(object);
557 VM_OBJECT_UNLOCK(object);
559 if (robject->ref_count == 1) {
560 robject->ref_count--;
565 vm_object_collapse(object);
566 VM_OBJECT_UNLOCK(object);
569 VM_OBJECT_UNLOCK(robject);
571 VM_OBJECT_UNLOCK(object);
575 temp = object->backing_object;
577 VM_OBJECT_LOCK(temp);
578 LIST_REMOVE(object, shadow_list);
579 temp->shadow_count--;
581 VM_OBJECT_UNLOCK(temp);
582 object->backing_object = NULL;
585 * Don't double-terminate, we could be in a termination
586 * recursion due to the terminate having to sync data
589 if ((object->flags & OBJ_DEAD) == 0)
590 vm_object_terminate(object);
592 VM_OBJECT_UNLOCK(object);
598 * vm_object_terminate actually destroys the specified object, freeing
599 * up all previously used resources.
601 * The object must be locked.
602 * This routine may block.
605 vm_object_terminate(vm_object_t object)
609 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
612 * Make sure no one uses us.
614 vm_object_set_flag(object, OBJ_DEAD);
617 * wait for the pageout daemon to be done with the object
619 vm_object_pip_wait(object, "objtrm");
621 KASSERT(!object->paging_in_progress,
622 ("vm_object_terminate: pageout in progress"));
625 * Clean and free the pages, as appropriate. All references to the
626 * object are gone, so we don't need to lock it.
628 if (object->type == OBJT_VNODE) {
629 struct vnode *vp = (struct vnode *)object->handle;
632 * Clean pages and flush buffers.
634 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
635 VM_OBJECT_UNLOCK(object);
637 vinvalbuf(vp, V_SAVE, NULL, 0, 0);
639 VM_OBJECT_LOCK(object);
642 KASSERT(object->ref_count == 0,
643 ("vm_object_terminate: object with references, ref_count=%d",
647 * Now free any remaining pages. For internal objects, this also
648 * removes them from paging queues. Don't free wired pages, just
649 * remove them from the object.
651 vm_page_lock_queues();
652 while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
653 KASSERT(!p->busy && (p->oflags & VPO_BUSY) == 0,
654 ("vm_object_terminate: freeing busy page %p "
655 "p->busy = %d, p->flags %x\n", p, p->busy, p->flags));
656 if (p->wire_count == 0) {
663 vm_page_unlock_queues();
666 * Let the pager know object is dead.
668 vm_pager_deallocate(object);
669 VM_OBJECT_UNLOCK(object);
672 * Remove the object from the global object list.
674 mtx_lock(&vm_object_list_mtx);
675 TAILQ_REMOVE(&vm_object_list, object, object_list);
676 mtx_unlock(&vm_object_list_mtx);
679 * Free the space for the object.
681 uma_zfree(obj_zone, object);
685 * vm_object_page_clean
687 * Clean all dirty pages in the specified range of object. Leaves page
688 * on whatever queue it is currently on. If NOSYNC is set then do not
689 * write out pages with VPO_NOSYNC set (originally comes from MAP_NOSYNC),
690 * leaving the object dirty.
692 * When stuffing pages asynchronously, allow clustering. XXX we need a
693 * synchronous clustering mode implementation.
695 * Odd semantics: if start == end, we clean everything.
697 * The object must be locked.
700 vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end, int flags)
703 vm_pindex_t tstart, tend;
709 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
710 if (object->type != OBJT_VNODE ||
711 (object->flags & OBJ_MIGHTBEDIRTY) == 0)
714 pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
715 pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
717 vm_object_set_flag(object, OBJ_CLEANING);
726 vm_page_lock_queues();
728 * If the caller is smart and only msync()s a range he knows is
729 * dirty, we may be able to avoid an object scan. This results in
730 * a phenominal improvement in performance. We cannot do this
731 * as a matter of course because the object may be huge - e.g.
732 * the size might be in the gigabytes or terrabytes.
734 if (msync_flush_flags & MSYNC_FLUSH_HARDSEQ) {
739 scanreset = object->resident_page_count / EASY_SCAN_FACTOR;
742 pagerflags |= VM_PAGER_IGNORE_CLEANCHK;
744 scanlimit = scanreset;
746 while (tscan < tend) {
747 curgeneration = object->generation;
748 p = vm_page_lookup(object, tscan);
749 if (p == NULL || p->valid == 0 ||
750 VM_PAGE_INQUEUE1(p, PQ_CACHE)) {
751 if (--scanlimit == 0)
756 vm_page_test_dirty(p);
757 if ((p->dirty & p->valid) == 0) {
758 if (--scanlimit == 0)
764 * If we have been asked to skip nosync pages and
765 * this is a nosync page, we can't continue.
767 if ((flags & OBJPC_NOSYNC) && (p->oflags & VPO_NOSYNC)) {
768 if (--scanlimit == 0)
773 scanlimit = scanreset;
776 * This returns 0 if it was unable to busy the first
777 * page (i.e. had to sleep).
779 tscan += vm_object_page_collect_flush(object, p, curgeneration, pagerflags);
783 * If everything was dirty and we flushed it successfully,
784 * and the requested range is not the entire object, we
785 * don't have to mess with CLEANCHK or MIGHTBEDIRTY and can
786 * return immediately.
788 if (tscan >= tend && (tstart || tend < object->size)) {
789 vm_page_unlock_queues();
790 vm_object_clear_flag(object, OBJ_CLEANING);
793 pagerflags &= ~VM_PAGER_IGNORE_CLEANCHK;
797 * Generally set CLEANCHK interlock and make the page read-only so
798 * we can then clear the object flags.
800 * However, if this is a nosync mmap then the object is likely to
801 * stay dirty so do not mess with the page and do not clear the
805 TAILQ_FOREACH(p, &object->memq, listq) {
806 vm_page_flag_set(p, PG_CLEANCHK);
807 if ((flags & OBJPC_NOSYNC) && (p->oflags & VPO_NOSYNC))
810 pmap_remove_write(p);
813 if (clearobjflags && (tstart == 0) && (tend == object->size)) {
816 vm_object_clear_flag(object, OBJ_MIGHTBEDIRTY);
817 if (object->type == OBJT_VNODE &&
818 (vp = (struct vnode *)object->handle) != NULL) {
820 if (vp->v_iflag & VI_OBJDIRTY)
821 vp->v_iflag &= ~VI_OBJDIRTY;
827 curgeneration = object->generation;
829 for (p = TAILQ_FIRST(&object->memq); p; p = np) {
832 np = TAILQ_NEXT(p, listq);
836 if (((p->flags & PG_CLEANCHK) == 0) ||
837 (pi < tstart) || (pi >= tend) ||
839 VM_PAGE_INQUEUE1(p, PQ_CACHE)) {
840 vm_page_flag_clear(p, PG_CLEANCHK);
844 vm_page_test_dirty(p);
845 if ((p->dirty & p->valid) == 0) {
846 vm_page_flag_clear(p, PG_CLEANCHK);
851 * If we have been asked to skip nosync pages and this is a
852 * nosync page, skip it. Note that the object flags were
853 * not cleared in this case so we do not have to set them.
855 if ((flags & OBJPC_NOSYNC) && (p->oflags & VPO_NOSYNC)) {
856 vm_page_flag_clear(p, PG_CLEANCHK);
860 n = vm_object_page_collect_flush(object, p,
861 curgeneration, pagerflags);
865 if (object->generation != curgeneration)
869 * Try to optimize the next page. If we can't we pick up
870 * our (random) scan where we left off.
872 if (msync_flush_flags & MSYNC_FLUSH_SOFTSEQ) {
873 if ((p = vm_page_lookup(object, pi + n)) != NULL)
877 vm_page_unlock_queues();
879 VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
882 vm_object_clear_flag(object, OBJ_CLEANING);
887 vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags)
895 vm_page_t maf[vm_pageout_page_count];
896 vm_page_t mab[vm_pageout_page_count];
897 vm_page_t ma[vm_pageout_page_count];
899 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
901 while (vm_page_sleep_if_busy(p, TRUE, "vpcwai")) {
902 vm_page_lock_queues();
903 if (object->generation != curgeneration) {
908 for(i = 1; i < vm_pageout_page_count; i++) {
911 if ((tp = vm_page_lookup(object, pi + i)) != NULL) {
912 if ((tp->oflags & VPO_BUSY) ||
913 ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
914 (tp->flags & PG_CLEANCHK) == 0) ||
917 if (VM_PAGE_INQUEUE1(tp, PQ_CACHE)) {
918 vm_page_flag_clear(tp, PG_CLEANCHK);
921 vm_page_test_dirty(tp);
922 if ((tp->dirty & tp->valid) == 0) {
923 vm_page_flag_clear(tp, PG_CLEANCHK);
934 chkb = vm_pageout_page_count - maxf;
936 for(i = 1; i < chkb;i++) {
939 if ((tp = vm_page_lookup(object, pi - i)) != NULL) {
940 if ((tp->oflags & VPO_BUSY) ||
941 ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
942 (tp->flags & PG_CLEANCHK) == 0) ||
945 if (VM_PAGE_INQUEUE1(tp, PQ_CACHE)) {
946 vm_page_flag_clear(tp, PG_CLEANCHK);
949 vm_page_test_dirty(tp);
950 if ((tp->dirty & tp->valid) == 0) {
951 vm_page_flag_clear(tp, PG_CLEANCHK);
962 for(i = 0; i < maxb; i++) {
963 int index = (maxb - i) - 1;
965 vm_page_flag_clear(ma[index], PG_CLEANCHK);
967 vm_page_flag_clear(p, PG_CLEANCHK);
969 for(i = 0; i < maxf; i++) {
970 int index = (maxb + i) + 1;
972 vm_page_flag_clear(ma[index], PG_CLEANCHK);
974 runlen = maxb + maxf + 1;
976 vm_pageout_flush(ma, runlen, pagerflags);
977 for (i = 0; i < runlen; i++) {
978 if (ma[i]->valid & ma[i]->dirty) {
979 pmap_remove_write(ma[i]);
980 vm_page_flag_set(ma[i], PG_CLEANCHK);
983 * maxf will end up being the actual number of pages
984 * we wrote out contiguously, non-inclusive of the
985 * first page. We do not count look-behind pages.
987 if (i >= maxb + 1 && (maxf > i - maxb - 1))
995 * Note that there is absolutely no sense in writing out
996 * anonymous objects, so we track down the vnode object
998 * We invalidate (remove) all pages from the address space
999 * for semantic correctness.
1001 * Note: certain anonymous maps, such as MAP_NOSYNC maps,
1002 * may start out with a NULL object.
1005 vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size,
1006 boolean_t syncio, boolean_t invalidate)
1008 vm_object_t backing_object;
1015 VM_OBJECT_LOCK(object);
1016 while ((backing_object = object->backing_object) != NULL) {
1017 VM_OBJECT_LOCK(backing_object);
1018 offset += object->backing_object_offset;
1019 VM_OBJECT_UNLOCK(object);
1020 object = backing_object;
1021 if (object->size < OFF_TO_IDX(offset + size))
1022 size = IDX_TO_OFF(object->size) - offset;
1025 * Flush pages if writing is allowed, invalidate them
1026 * if invalidation requested. Pages undergoing I/O
1027 * will be ignored by vm_object_page_remove().
1029 * We cannot lock the vnode and then wait for paging
1030 * to complete without deadlocking against vm_fault.
1031 * Instead we simply call vm_object_page_remove() and
1032 * allow it to block internally on a page-by-page
1033 * basis when it encounters pages undergoing async
1036 if (object->type == OBJT_VNODE &&
1037 (object->flags & OBJ_MIGHTBEDIRTY) != 0) {
1039 vp = object->handle;
1040 VM_OBJECT_UNLOCK(object);
1041 (void) vn_start_write(vp, &mp, V_WAIT);
1042 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1043 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
1044 flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
1045 flags |= invalidate ? OBJPC_INVAL : 0;
1046 VM_OBJECT_LOCK(object);
1047 vm_object_page_clean(object,
1049 OFF_TO_IDX(offset + size + PAGE_MASK),
1051 VM_OBJECT_UNLOCK(object);
1052 VOP_UNLOCK(vp, 0, curthread);
1053 VFS_UNLOCK_GIANT(vfslocked);
1054 vn_finished_write(mp);
1055 VM_OBJECT_LOCK(object);
1057 if ((object->type == OBJT_VNODE ||
1058 object->type == OBJT_DEVICE) && invalidate) {
1060 purge = old_msync || (object->type == OBJT_DEVICE);
1061 vm_object_page_remove(object,
1063 OFF_TO_IDX(offset + size + PAGE_MASK),
1064 purge ? FALSE : TRUE);
1066 VM_OBJECT_UNLOCK(object);
1070 * vm_object_madvise:
1072 * Implements the madvise function at the object/page level.
1074 * MADV_WILLNEED (any object)
1076 * Activate the specified pages if they are resident.
1078 * MADV_DONTNEED (any object)
1080 * Deactivate the specified pages if they are resident.
1082 * MADV_FREE (OBJT_DEFAULT/OBJT_SWAP objects,
1083 * OBJ_ONEMAPPING only)
1085 * Deactivate and clean the specified pages if they are
1086 * resident. This permits the process to reuse the pages
1087 * without faulting or the kernel to reclaim the pages
1091 vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
1093 vm_pindex_t end, tpindex;
1094 vm_object_t backing_object, tobject;
1099 VM_OBJECT_LOCK(object);
1100 end = pindex + count;
1102 * Locate and adjust resident pages
1104 for (; pindex < end; pindex += 1) {
1110 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
1111 * and those pages must be OBJ_ONEMAPPING.
1113 if (advise == MADV_FREE) {
1114 if ((tobject->type != OBJT_DEFAULT &&
1115 tobject->type != OBJT_SWAP) ||
1116 (tobject->flags & OBJ_ONEMAPPING) == 0) {
1117 goto unlock_tobject;
1120 m = vm_page_lookup(tobject, tpindex);
1123 * There may be swap even if there is no backing page
1125 if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1126 swap_pager_freespace(tobject, tpindex, 1);
1130 backing_object = tobject->backing_object;
1131 if (backing_object == NULL)
1132 goto unlock_tobject;
1133 VM_OBJECT_LOCK(backing_object);
1134 tpindex += OFF_TO_IDX(tobject->backing_object_offset);
1135 if (tobject != object)
1136 VM_OBJECT_UNLOCK(tobject);
1137 tobject = backing_object;
1141 * If the page is busy or not in a normal active state,
1142 * we skip it. If the page is not managed there are no
1143 * page queues to mess with. Things can break if we mess
1144 * with pages in any of the below states.
1146 vm_page_lock_queues();
1147 if (m->hold_count ||
1149 (m->flags & PG_UNMANAGED) ||
1150 m->valid != VM_PAGE_BITS_ALL) {
1151 vm_page_unlock_queues();
1152 goto unlock_tobject;
1154 if ((m->oflags & VPO_BUSY) || m->busy) {
1155 vm_page_flag_set(m, PG_REFERENCED);
1156 vm_page_unlock_queues();
1157 if (object != tobject)
1158 VM_OBJECT_UNLOCK(object);
1159 m->oflags |= VPO_WANTED;
1160 msleep(m, VM_OBJECT_MTX(tobject), PDROP | PVM, "madvpo", 0);
1161 VM_OBJECT_LOCK(object);
1164 if (advise == MADV_WILLNEED) {
1165 vm_page_activate(m);
1166 } else if (advise == MADV_DONTNEED) {
1167 vm_page_dontneed(m);
1168 } else if (advise == MADV_FREE) {
1170 * Mark the page clean. This will allow the page
1171 * to be freed up by the system. However, such pages
1172 * are often reused quickly by malloc()/free()
1173 * so we do not do anything that would cause
1174 * a page fault if we can help it.
1176 * Specifically, we do not try to actually free
1177 * the page now nor do we try to put it in the
1178 * cache (which would cause a page fault on reuse).
1180 * But we do make the page is freeable as we
1181 * can without actually taking the step of unmapping
1184 pmap_clear_modify(m);
1187 vm_page_dontneed(m);
1189 vm_page_unlock_queues();
1190 if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1191 swap_pager_freespace(tobject, tpindex, 1);
1193 if (tobject != object)
1194 VM_OBJECT_UNLOCK(tobject);
1196 VM_OBJECT_UNLOCK(object);
1202 * Create a new object which is backed by the
1203 * specified existing object range. The source
1204 * object reference is deallocated.
1206 * The new object and offset into that object
1207 * are returned in the source parameters.
1211 vm_object_t *object, /* IN/OUT */
1212 vm_ooffset_t *offset, /* IN/OUT */
1221 * Don't create the new object if the old object isn't shared.
1223 if (source != NULL) {
1224 VM_OBJECT_LOCK(source);
1225 if (source->ref_count == 1 &&
1226 source->handle == NULL &&
1227 (source->type == OBJT_DEFAULT ||
1228 source->type == OBJT_SWAP)) {
1229 VM_OBJECT_UNLOCK(source);
1232 VM_OBJECT_UNLOCK(source);
1236 * Allocate a new object with the given length.
1238 result = vm_object_allocate(OBJT_DEFAULT, length);
1241 * The new object shadows the source object, adding a reference to it.
1242 * Our caller changes his reference to point to the new object,
1243 * removing a reference to the source object. Net result: no change
1244 * of reference count.
1246 * Try to optimize the result object's page color when shadowing
1247 * in order to maintain page coloring consistency in the combined
1250 result->backing_object = source;
1252 * Store the offset into the source object, and fix up the offset into
1255 result->backing_object_offset = *offset;
1256 if (source != NULL) {
1257 VM_OBJECT_LOCK(source);
1258 LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1259 source->shadow_count++;
1260 source->generation++;
1261 if (length < source->size)
1262 length = source->size;
1263 if (length > PQ_MAXLENGTH || source->generation > 1)
1264 length = PQ_MAXLENGTH;
1265 result->pg_color = (source->pg_color +
1266 length * source->generation) & PQ_COLORMASK;
1267 result->flags |= source->flags & OBJ_NEEDGIANT;
1268 VM_OBJECT_UNLOCK(source);
1269 next_index = (result->pg_color + PQ_MAXLENGTH) & PQ_COLORMASK;
1274 * Return the new things
1283 * Split the pages in a map entry into a new object. This affords
1284 * easier removal of unused pages, and keeps object inheritance from
1285 * being a negative impact on memory usage.
1288 vm_object_split(vm_map_entry_t entry)
1290 vm_page_t m, m_next;
1291 vm_object_t orig_object, new_object, source;
1292 vm_pindex_t idx, offidxstart;
1295 orig_object = entry->object.vm_object;
1296 if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1298 if (orig_object->ref_count <= 1)
1300 VM_OBJECT_UNLOCK(orig_object);
1302 offidxstart = OFF_TO_IDX(entry->offset);
1303 size = atop(entry->end - entry->start);
1306 * If swap_pager_copy() is later called, it will convert new_object
1307 * into a swap object.
1309 new_object = vm_object_allocate(OBJT_DEFAULT, size);
1311 VM_OBJECT_LOCK(new_object);
1312 VM_OBJECT_LOCK(orig_object);
1313 source = orig_object->backing_object;
1314 if (source != NULL) {
1315 VM_OBJECT_LOCK(source);
1316 LIST_INSERT_HEAD(&source->shadow_head,
1317 new_object, shadow_list);
1318 source->shadow_count++;
1319 source->generation++;
1320 vm_object_reference_locked(source); /* for new_object */
1321 vm_object_clear_flag(source, OBJ_ONEMAPPING);
1322 VM_OBJECT_UNLOCK(source);
1323 new_object->backing_object_offset =
1324 orig_object->backing_object_offset + entry->offset;
1325 new_object->backing_object = source;
1327 new_object->flags |= orig_object->flags & OBJ_NEEDGIANT;
1329 if ((m = TAILQ_FIRST(&orig_object->memq)) != NULL) {
1330 if (m->pindex < offidxstart) {
1331 m = vm_page_splay(offidxstart, orig_object->root);
1332 if ((orig_object->root = m)->pindex < offidxstart)
1333 m = TAILQ_NEXT(m, listq);
1336 vm_page_lock_queues();
1337 for (; m != NULL && (idx = m->pindex - offidxstart) < size;
1339 m_next = TAILQ_NEXT(m, listq);
1342 * We must wait for pending I/O to complete before we can
1345 * We do not have to VM_PROT_NONE the page as mappings should
1346 * not be changed by this operation.
1348 if ((m->oflags & VPO_BUSY) || m->busy) {
1349 vm_page_flag_set(m, PG_REFERENCED);
1350 vm_page_unlock_queues();
1351 VM_OBJECT_UNLOCK(new_object);
1352 m->oflags |= VPO_WANTED;
1353 msleep(m, VM_OBJECT_MTX(orig_object), PDROP | PVM, "spltwt", 0);
1354 VM_OBJECT_LOCK(new_object);
1355 VM_OBJECT_LOCK(orig_object);
1358 vm_page_rename(m, new_object, idx);
1359 /* page automatically made dirty by rename and cache handled */
1362 vm_page_unlock_queues();
1363 if (orig_object->type == OBJT_SWAP) {
1365 * swap_pager_copy() can sleep, in which case the orig_object's
1366 * and new_object's locks are released and reacquired.
1368 swap_pager_copy(orig_object, new_object, offidxstart, 0);
1370 VM_OBJECT_UNLOCK(orig_object);
1371 TAILQ_FOREACH(m, &new_object->memq, listq)
1373 VM_OBJECT_UNLOCK(new_object);
1374 entry->object.vm_object = new_object;
1375 entry->offset = 0LL;
1376 vm_object_deallocate(orig_object);
1377 VM_OBJECT_LOCK(new_object);
1380 #define OBSC_TEST_ALL_SHADOWED 0x0001
1381 #define OBSC_COLLAPSE_NOWAIT 0x0002
1382 #define OBSC_COLLAPSE_WAIT 0x0004
1385 vm_object_backing_scan(vm_object_t object, int op)
1389 vm_object_t backing_object;
1390 vm_pindex_t backing_offset_index;
1392 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1393 VM_OBJECT_LOCK_ASSERT(object->backing_object, MA_OWNED);
1395 backing_object = object->backing_object;
1396 backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
1399 * Initial conditions
1401 if (op & OBSC_TEST_ALL_SHADOWED) {
1403 * We do not want to have to test for the existence of
1404 * swap pages in the backing object. XXX but with the
1405 * new swapper this would be pretty easy to do.
1407 * XXX what about anonymous MAP_SHARED memory that hasn't
1408 * been ZFOD faulted yet? If we do not test for this, the
1409 * shadow test may succeed! XXX
1411 if (backing_object->type != OBJT_DEFAULT) {
1415 if (op & OBSC_COLLAPSE_WAIT) {
1416 vm_object_set_flag(backing_object, OBJ_DEAD);
1422 p = TAILQ_FIRST(&backing_object->memq);
1424 vm_page_t next = TAILQ_NEXT(p, listq);
1425 vm_pindex_t new_pindex = p->pindex - backing_offset_index;
1427 if (op & OBSC_TEST_ALL_SHADOWED) {
1431 * Ignore pages outside the parent object's range
1432 * and outside the parent object's mapping of the
1435 * note that we do not busy the backing object's
1439 p->pindex < backing_offset_index ||
1440 new_pindex >= object->size
1447 * See if the parent has the page or if the parent's
1448 * object pager has the page. If the parent has the
1449 * page but the page is not valid, the parent's
1450 * object pager must have the page.
1452 * If this fails, the parent does not completely shadow
1453 * the object and we might as well give up now.
1456 pp = vm_page_lookup(object, new_pindex);
1458 (pp == NULL || pp->valid == 0) &&
1459 !vm_pager_has_page(object, new_pindex, NULL, NULL)
1467 * Check for busy page
1469 if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
1472 if (op & OBSC_COLLAPSE_NOWAIT) {
1473 if ((p->oflags & VPO_BUSY) ||
1479 } else if (op & OBSC_COLLAPSE_WAIT) {
1480 if ((p->oflags & VPO_BUSY) || p->busy) {
1481 vm_page_lock_queues();
1482 vm_page_flag_set(p, PG_REFERENCED);
1483 vm_page_unlock_queues();
1484 VM_OBJECT_UNLOCK(object);
1485 p->oflags |= VPO_WANTED;
1486 msleep(p, VM_OBJECT_MTX(backing_object),
1487 PDROP | PVM, "vmocol", 0);
1488 VM_OBJECT_LOCK(object);
1489 VM_OBJECT_LOCK(backing_object);
1491 * If we slept, anything could have
1492 * happened. Since the object is
1493 * marked dead, the backing offset
1494 * should not have changed so we
1495 * just restart our scan.
1497 p = TAILQ_FIRST(&backing_object->memq);
1503 p->object == backing_object,
1504 ("vm_object_backing_scan: object mismatch")
1508 * Destroy any associated swap
1510 if (backing_object->type == OBJT_SWAP) {
1511 swap_pager_freespace(
1519 p->pindex < backing_offset_index ||
1520 new_pindex >= object->size
1523 * Page is out of the parent object's range, we
1524 * can simply destroy it.
1526 vm_page_lock_queues();
1527 KASSERT(!pmap_page_is_mapped(p),
1528 ("freeing mapped page %p", p));
1529 if (p->wire_count == 0)
1533 vm_page_unlock_queues();
1538 pp = vm_page_lookup(object, new_pindex);
1541 vm_pager_has_page(object, new_pindex, NULL, NULL)
1544 * page already exists in parent OR swap exists
1545 * for this location in the parent. Destroy
1546 * the original page from the backing object.
1548 * Leave the parent's page alone
1550 vm_page_lock_queues();
1551 KASSERT(!pmap_page_is_mapped(p),
1552 ("freeing mapped page %p", p));
1553 if (p->wire_count == 0)
1557 vm_page_unlock_queues();
1563 * Page does not exist in parent, rename the
1564 * page from the backing object to the main object.
1566 * If the page was mapped to a process, it can remain
1567 * mapped through the rename.
1569 vm_page_lock_queues();
1570 vm_page_rename(p, object, new_pindex);
1571 vm_page_unlock_queues();
1572 /* page automatically made dirty by rename */
1581 * this version of collapse allows the operation to occur earlier and
1582 * when paging_in_progress is true for an object... This is not a complete
1583 * operation, but should plug 99.9% of the rest of the leaks.
1586 vm_object_qcollapse(vm_object_t object)
1588 vm_object_t backing_object = object->backing_object;
1590 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1591 VM_OBJECT_LOCK_ASSERT(backing_object, MA_OWNED);
1593 if (backing_object->ref_count != 1)
1596 vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
1600 * vm_object_collapse:
1602 * Collapse an object with the object backing it.
1603 * Pages in the backing object are moved into the
1604 * parent, and the backing object is deallocated.
1607 vm_object_collapse(vm_object_t object)
1609 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1612 vm_object_t backing_object;
1615 * Verify that the conditions are right for collapse:
1617 * The object exists and the backing object exists.
1619 if ((backing_object = object->backing_object) == NULL)
1623 * we check the backing object first, because it is most likely
1626 VM_OBJECT_LOCK(backing_object);
1627 if (backing_object->handle != NULL ||
1628 (backing_object->type != OBJT_DEFAULT &&
1629 backing_object->type != OBJT_SWAP) ||
1630 (backing_object->flags & OBJ_DEAD) ||
1631 object->handle != NULL ||
1632 (object->type != OBJT_DEFAULT &&
1633 object->type != OBJT_SWAP) ||
1634 (object->flags & OBJ_DEAD)) {
1635 VM_OBJECT_UNLOCK(backing_object);
1640 object->paging_in_progress != 0 ||
1641 backing_object->paging_in_progress != 0
1643 vm_object_qcollapse(object);
1644 VM_OBJECT_UNLOCK(backing_object);
1648 * We know that we can either collapse the backing object (if
1649 * the parent is the only reference to it) or (perhaps) have
1650 * the parent bypass the object if the parent happens to shadow
1651 * all the resident pages in the entire backing object.
1653 * This is ignoring pager-backed pages such as swap pages.
1654 * vm_object_backing_scan fails the shadowing test in this
1657 if (backing_object->ref_count == 1) {
1659 * If there is exactly one reference to the backing
1660 * object, we can collapse it into the parent.
1662 vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1665 * Move the pager from backing_object to object.
1667 if (backing_object->type == OBJT_SWAP) {
1669 * swap_pager_copy() can sleep, in which case
1670 * the backing_object's and object's locks are
1671 * released and reacquired.
1676 OFF_TO_IDX(object->backing_object_offset), TRUE);
1679 * Object now shadows whatever backing_object did.
1680 * Note that the reference to
1681 * backing_object->backing_object moves from within
1682 * backing_object to within object.
1684 LIST_REMOVE(object, shadow_list);
1685 backing_object->shadow_count--;
1686 backing_object->generation++;
1687 if (backing_object->backing_object) {
1688 VM_OBJECT_LOCK(backing_object->backing_object);
1689 LIST_REMOVE(backing_object, shadow_list);
1691 &backing_object->backing_object->shadow_head,
1692 object, shadow_list);
1694 * The shadow_count has not changed.
1696 backing_object->backing_object->generation++;
1697 VM_OBJECT_UNLOCK(backing_object->backing_object);
1699 object->backing_object = backing_object->backing_object;
1700 object->backing_object_offset +=
1701 backing_object->backing_object_offset;
1704 * Discard backing_object.
1706 * Since the backing object has no pages, no pager left,
1707 * and no object references within it, all that is
1708 * necessary is to dispose of it.
1710 KASSERT(backing_object->ref_count == 1, ("backing_object %p was somehow re-referenced during collapse!", backing_object));
1711 VM_OBJECT_UNLOCK(backing_object);
1713 mtx_lock(&vm_object_list_mtx);
1719 mtx_unlock(&vm_object_list_mtx);
1721 uma_zfree(obj_zone, backing_object);
1725 vm_object_t new_backing_object;
1728 * If we do not entirely shadow the backing object,
1729 * there is nothing we can do so we give up.
1731 if (object->resident_page_count != object->size &&
1732 vm_object_backing_scan(object,
1733 OBSC_TEST_ALL_SHADOWED) == 0) {
1734 VM_OBJECT_UNLOCK(backing_object);
1739 * Make the parent shadow the next object in the
1740 * chain. Deallocating backing_object will not remove
1741 * it, since its reference count is at least 2.
1743 LIST_REMOVE(object, shadow_list);
1744 backing_object->shadow_count--;
1745 backing_object->generation++;
1747 new_backing_object = backing_object->backing_object;
1748 if ((object->backing_object = new_backing_object) != NULL) {
1749 VM_OBJECT_LOCK(new_backing_object);
1751 &new_backing_object->shadow_head,
1755 new_backing_object->shadow_count++;
1756 new_backing_object->generation++;
1757 vm_object_reference_locked(new_backing_object);
1758 VM_OBJECT_UNLOCK(new_backing_object);
1759 object->backing_object_offset +=
1760 backing_object->backing_object_offset;
1764 * Drop the reference count on backing_object. Since
1765 * its ref_count was at least 2, it will not vanish.
1767 backing_object->ref_count--;
1768 VM_OBJECT_UNLOCK(backing_object);
1773 * Try again with this object's new backing object.
1779 * vm_object_page_remove:
1781 * Removes all physical pages in the given range from the
1782 * object's list of pages. If the range's end is zero, all
1783 * physical pages from the range's start to the end of the object
1786 * The object must be locked.
1789 vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
1790 boolean_t clean_only)
1794 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1795 if (object->resident_page_count == 0)
1799 * Since physically-backed objects do not use managed pages, we can't
1800 * remove pages from the object (we must instead remove the page
1801 * references, and then destroy the object).
1803 KASSERT(object->type != OBJT_PHYS,
1804 ("attempt to remove pages from a physical object"));
1806 vm_object_pip_add(object, 1);
1808 vm_page_lock_queues();
1809 if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
1810 if (p->pindex < start) {
1811 p = vm_page_splay(start, object->root);
1812 if ((object->root = p)->pindex < start)
1813 p = TAILQ_NEXT(p, listq);
1817 * Assert: the variable p is either (1) the page with the
1818 * least pindex greater than or equal to the parameter pindex
1822 p != NULL && (p->pindex < end || end == 0);
1824 next = TAILQ_NEXT(p, listq);
1826 if (p->wire_count != 0) {
1832 if (vm_page_sleep_if_busy(p, TRUE, "vmopar"))
1834 if (clean_only && p->valid) {
1835 pmap_remove_write(p);
1836 if (p->valid & p->dirty)
1842 vm_page_unlock_queues();
1843 vm_object_pip_wakeup(object);
1847 * Routine: vm_object_coalesce
1848 * Function: Coalesces two objects backing up adjoining
1849 * regions of memory into a single object.
1851 * returns TRUE if objects were combined.
1853 * NOTE: Only works at the moment if the second object is NULL -
1854 * if it's not, which object do we lock first?
1857 * prev_object First object to coalesce
1858 * prev_offset Offset into prev_object
1859 * prev_size Size of reference to prev_object
1860 * next_size Size of reference to the second object
1863 * The object must *not* be locked.
1866 vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset,
1867 vm_size_t prev_size, vm_size_t next_size)
1869 vm_pindex_t next_pindex;
1871 if (prev_object == NULL)
1873 VM_OBJECT_LOCK(prev_object);
1874 if (prev_object->type != OBJT_DEFAULT &&
1875 prev_object->type != OBJT_SWAP) {
1876 VM_OBJECT_UNLOCK(prev_object);
1881 * Try to collapse the object first
1883 vm_object_collapse(prev_object);
1886 * Can't coalesce if: . more than one reference . paged out . shadows
1887 * another object . has a copy elsewhere (any of which mean that the
1888 * pages not mapped to prev_entry may be in use anyway)
1890 if (prev_object->backing_object != NULL) {
1891 VM_OBJECT_UNLOCK(prev_object);
1895 prev_size >>= PAGE_SHIFT;
1896 next_size >>= PAGE_SHIFT;
1897 next_pindex = OFF_TO_IDX(prev_offset) + prev_size;
1899 if ((prev_object->ref_count > 1) &&
1900 (prev_object->size != next_pindex)) {
1901 VM_OBJECT_UNLOCK(prev_object);
1906 * Remove any pages that may still be in the object from a previous
1909 if (next_pindex < prev_object->size) {
1910 vm_object_page_remove(prev_object,
1912 next_pindex + next_size, FALSE);
1913 if (prev_object->type == OBJT_SWAP)
1914 swap_pager_freespace(prev_object,
1915 next_pindex, next_size);
1919 * Extend the object if necessary.
1921 if (next_pindex + next_size > prev_object->size)
1922 prev_object->size = next_pindex + next_size;
1924 VM_OBJECT_UNLOCK(prev_object);
1929 vm_object_set_writeable_dirty(vm_object_t object)
1933 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1934 if ((object->flags & OBJ_MIGHTBEDIRTY) != 0)
1936 vm_object_set_flag(object, OBJ_MIGHTBEDIRTY);
1937 if (object->type == OBJT_VNODE &&
1938 (vp = (struct vnode *)object->handle) != NULL) {
1940 vp->v_iflag |= VI_OBJDIRTY;
1945 #include "opt_ddb.h"
1947 #include <sys/kernel.h>
1949 #include <sys/cons.h>
1951 #include <ddb/ddb.h>
1954 _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
1957 vm_map_entry_t tmpe;
1965 tmpe = map->header.next;
1966 entcount = map->nentries;
1967 while (entcount-- && (tmpe != &map->header)) {
1968 if (_vm_object_in_map(map, object, tmpe)) {
1973 } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
1974 tmpm = entry->object.sub_map;
1975 tmpe = tmpm->header.next;
1976 entcount = tmpm->nentries;
1977 while (entcount-- && tmpe != &tmpm->header) {
1978 if (_vm_object_in_map(tmpm, object, tmpe)) {
1983 } else if ((obj = entry->object.vm_object) != NULL) {
1984 for (; obj; obj = obj->backing_object)
1985 if (obj == object) {
1993 vm_object_in_map(vm_object_t object)
1997 /* sx_slock(&allproc_lock); */
1998 FOREACH_PROC_IN_SYSTEM(p) {
1999 if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
2001 if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
2002 /* sx_sunlock(&allproc_lock); */
2006 /* sx_sunlock(&allproc_lock); */
2007 if (_vm_object_in_map(kernel_map, object, 0))
2009 if (_vm_object_in_map(kmem_map, object, 0))
2011 if (_vm_object_in_map(pager_map, object, 0))
2013 if (_vm_object_in_map(buffer_map, object, 0))
2018 DB_SHOW_COMMAND(vmochk, vm_object_check)
2023 * make sure that internal objs are in a map somewhere
2024 * and none have zero ref counts.
2026 TAILQ_FOREACH(object, &vm_object_list, object_list) {
2027 if (object->handle == NULL &&
2028 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2029 if (object->ref_count == 0) {
2030 db_printf("vmochk: internal obj has zero ref count: %ld\n",
2031 (long)object->size);
2033 if (!vm_object_in_map(object)) {
2035 "vmochk: internal obj is not in a map: "
2036 "ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2037 object->ref_count, (u_long)object->size,
2038 (u_long)object->size,
2039 (void *)object->backing_object);
2046 * vm_object_print: [ debug ]
2048 DB_SHOW_COMMAND(object, vm_object_print_static)
2050 /* XXX convert args. */
2051 vm_object_t object = (vm_object_t)addr;
2052 boolean_t full = have_addr;
2056 /* XXX count is an (unused) arg. Avoid shadowing it. */
2057 #define count was_count
2065 "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x\n",
2066 object, (int)object->type, (uintmax_t)object->size,
2067 object->resident_page_count, object->ref_count, object->flags);
2068 db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n",
2069 object->shadow_count,
2070 object->backing_object ? object->backing_object->ref_count : 0,
2071 object->backing_object, (uintmax_t)object->backing_object_offset);
2078 TAILQ_FOREACH(p, &object->memq, listq) {
2080 db_iprintf("memory:=");
2081 else if (count == 6) {
2089 db_printf("(off=0x%jx,page=0x%jx)",
2090 (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p));
2100 /* XXX need this non-static entry for calling from vm_map_print. */
2103 /* db_expr_t */ long addr,
2104 boolean_t have_addr,
2105 /* db_expr_t */ long count,
2108 vm_object_print_static(addr, have_addr, count, modif);
2111 DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
2117 TAILQ_FOREACH(object, &vm_object_list, object_list) {
2118 vm_pindex_t idx, fidx;
2120 vm_paddr_t pa = -1, padiff;
2124 db_printf("new object: %p\n", (void *)object);
2134 osize = object->size;
2137 for (idx = 0; idx < osize; idx++) {
2138 m = vm_page_lookup(object, idx);
2141 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2142 (long)fidx, rcount, (long)pa);
2157 (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
2162 padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
2163 padiff >>= PAGE_SHIFT;
2164 padiff &= PQ_COLORMASK;
2166 pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
2170 db_printf(" index(%ld)run(%d)pa(0x%lx)",
2171 (long)fidx, rcount, (long)pa);
2172 db_printf("pd(%ld)\n", (long)padiff);
2182 pa = VM_PAGE_TO_PHYS(m);
2186 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2187 (long)fidx, rcount, (long)pa);