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$");
70 #include <sys/param.h>
71 #include <sys/systm.h>
74 #include <sys/mount.h>
75 #include <sys/kernel.h>
76 #include <sys/sysctl.h>
77 #include <sys/mutex.h>
78 #include <sys/proc.h> /* for curproc, pageproc */
79 #include <sys/socket.h>
80 #include <sys/vnode.h>
81 #include <sys/vmmeter.h>
85 #include <vm/vm_param.h>
87 #include <vm/vm_map.h>
88 #include <vm/vm_object.h>
89 #include <vm/vm_page.h>
90 #include <vm/vm_pageout.h>
91 #include <vm/vm_pager.h>
92 #include <vm/swap_pager.h>
93 #include <vm/vm_kern.h>
94 #include <vm/vm_extern.h>
95 #include <vm/vm_reserv.h>
98 #define EASY_SCAN_FACTOR 8
100 #define MSYNC_FLUSH_HARDSEQ 0x01
101 #define MSYNC_FLUSH_SOFTSEQ 0x02
104 * msync / VM object flushing optimizations
106 static int msync_flush_flags = MSYNC_FLUSH_HARDSEQ | MSYNC_FLUSH_SOFTSEQ;
107 SYSCTL_INT(_vm, OID_AUTO, msync_flush_flags,
108 CTLFLAG_RW, &msync_flush_flags, 0, "");
110 static int old_msync;
111 SYSCTL_INT(_vm, OID_AUTO, old_msync, CTLFLAG_RW, &old_msync, 0,
112 "Use old (insecure) msync behavior");
114 static void vm_object_qcollapse(vm_object_t object);
115 static int vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags);
116 static void vm_object_vndeallocate(vm_object_t object);
119 * Virtual memory objects maintain the actual data
120 * associated with allocated virtual memory. A given
121 * page of memory exists within exactly one object.
123 * An object is only deallocated when all "references"
124 * are given up. Only one "reference" to a given
125 * region of an object should be writeable.
127 * Associated with each object is a list of all resident
128 * memory pages belonging to that object; this list is
129 * maintained by the "vm_page" module, and locked by the object's
132 * Each object also records a "pager" routine which is
133 * used to retrieve (and store) pages to the proper backing
134 * storage. In addition, objects may be backed by other
135 * objects from which they were virtual-copied.
137 * The only items within the object structure which are
138 * modified after time of creation are:
139 * reference count locked by object's lock
140 * pager routine locked by object's lock
144 struct object_q vm_object_list;
145 struct mtx vm_object_list_mtx; /* lock for object list and count */
147 struct vm_object kernel_object_store;
148 struct vm_object kmem_object_store;
150 SYSCTL_NODE(_vm_stats, OID_AUTO, object, CTLFLAG_RD, 0, "VM object stats");
152 static long object_collapses;
153 SYSCTL_LONG(_vm_stats_object, OID_AUTO, collapses, CTLFLAG_RD,
154 &object_collapses, 0, "VM object collapses");
156 static long object_bypasses;
157 SYSCTL_LONG(_vm_stats_object, OID_AUTO, bypasses, CTLFLAG_RD,
158 &object_bypasses, 0, "VM object bypasses");
160 static uma_zone_t obj_zone;
162 static int vm_object_zinit(void *mem, int size, int flags);
165 static void vm_object_zdtor(void *mem, int size, void *arg);
168 vm_object_zdtor(void *mem, int size, void *arg)
172 object = (vm_object_t)mem;
173 KASSERT(TAILQ_EMPTY(&object->memq),
174 ("object %p has resident pages",
176 #if VM_NRESERVLEVEL > 0
177 KASSERT(LIST_EMPTY(&object->rvq),
178 ("object %p has reservations",
181 KASSERT(object->cache == NULL,
182 ("object %p has cached pages",
184 KASSERT(object->paging_in_progress == 0,
185 ("object %p paging_in_progress = %d",
186 object, object->paging_in_progress));
187 KASSERT(object->resident_page_count == 0,
188 ("object %p resident_page_count = %d",
189 object, object->resident_page_count));
190 KASSERT(object->shadow_count == 0,
191 ("object %p shadow_count = %d",
192 object, object->shadow_count));
197 vm_object_zinit(void *mem, int size, int flags)
201 object = (vm_object_t)mem;
202 bzero(&object->mtx, sizeof(object->mtx));
203 VM_OBJECT_LOCK_INIT(object, "standard object");
205 /* These are true for any object that has been freed */
206 object->paging_in_progress = 0;
207 object->resident_page_count = 0;
208 object->shadow_count = 0;
213 _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
216 TAILQ_INIT(&object->memq);
217 LIST_INIT(&object->shadow_head);
222 object->generation = 1;
223 object->ref_count = 1;
225 if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
226 object->flags = OBJ_ONEMAPPING;
227 object->pg_color = 0;
228 object->handle = NULL;
229 object->backing_object = NULL;
230 object->backing_object_offset = (vm_ooffset_t) 0;
231 #if VM_NRESERVLEVEL > 0
232 LIST_INIT(&object->rvq);
234 object->cache = NULL;
236 mtx_lock(&vm_object_list_mtx);
237 TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
238 mtx_unlock(&vm_object_list_mtx);
244 * Initialize the VM objects module.
249 TAILQ_INIT(&vm_object_list);
250 mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF);
252 VM_OBJECT_LOCK_INIT(&kernel_object_store, "kernel object");
253 _vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
255 #if VM_NRESERVLEVEL > 0
256 kernel_object->flags |= OBJ_COLORED;
257 kernel_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS);
260 VM_OBJECT_LOCK_INIT(&kmem_object_store, "kmem object");
261 _vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
263 #if VM_NRESERVLEVEL > 0
264 kmem_object->flags |= OBJ_COLORED;
265 kmem_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS);
269 * The lock portion of struct vm_object must be type stable due
270 * to vm_pageout_fallback_object_lock locking a vm object
271 * without holding any references to it.
273 obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL,
279 vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM|UMA_ZONE_NOFREE);
283 vm_object_clear_flag(vm_object_t object, u_short bits)
286 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
287 object->flags &= ~bits;
291 vm_object_pip_add(vm_object_t object, short i)
294 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
295 object->paging_in_progress += i;
299 vm_object_pip_subtract(vm_object_t object, short i)
302 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
303 object->paging_in_progress -= i;
307 vm_object_pip_wakeup(vm_object_t object)
310 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
311 object->paging_in_progress--;
312 if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
313 vm_object_clear_flag(object, OBJ_PIPWNT);
319 vm_object_pip_wakeupn(vm_object_t object, short i)
322 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
324 object->paging_in_progress -= i;
325 if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
326 vm_object_clear_flag(object, OBJ_PIPWNT);
332 vm_object_pip_wait(vm_object_t object, char *waitid)
335 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
336 while (object->paging_in_progress) {
337 object->flags |= OBJ_PIPWNT;
338 msleep(object, VM_OBJECT_MTX(object), PVM, waitid, 0);
343 * vm_object_allocate:
345 * Returns a new object with the given size.
348 vm_object_allocate(objtype_t type, vm_pindex_t size)
352 object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK);
353 _vm_object_allocate(type, size, object);
359 * vm_object_reference:
361 * Gets another reference to the given object. Note: OBJ_DEAD
362 * objects can be referenced during final cleaning.
365 vm_object_reference(vm_object_t object)
371 VM_OBJECT_LOCK(object);
373 if (object->type == OBJT_VNODE) {
377 VM_OBJECT_UNLOCK(object);
378 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
379 vget(vp, LK_RETRY, curthread);
380 VFS_UNLOCK_GIANT(vfslocked);
382 VM_OBJECT_UNLOCK(object);
386 * vm_object_reference_locked:
388 * Gets another reference to the given object.
390 * The object must be locked.
393 vm_object_reference_locked(vm_object_t object)
397 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
398 KASSERT((object->flags & OBJ_DEAD) == 0,
399 ("vm_object_reference_locked: dead object referenced"));
401 if (object->type == OBJT_VNODE) {
408 * Handle deallocating an object of type OBJT_VNODE.
411 vm_object_vndeallocate(vm_object_t object)
413 struct vnode *vp = (struct vnode *) object->handle;
415 VFS_ASSERT_GIANT(vp->v_mount);
416 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
417 KASSERT(object->type == OBJT_VNODE,
418 ("vm_object_vndeallocate: not a vnode object"));
419 KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
421 if (object->ref_count == 0) {
422 vprint("vm_object_vndeallocate", vp);
423 panic("vm_object_vndeallocate: bad object reference count");
428 if (object->ref_count == 0) {
429 mp_fixme("Unlocked vflag access.");
430 vp->v_vflag &= ~VV_TEXT;
432 VM_OBJECT_UNLOCK(object);
434 * vrele may need a vop lock
440 * vm_object_deallocate:
442 * Release a reference to the specified object,
443 * gained either through a vm_object_allocate
444 * or a vm_object_reference call. When all references
445 * are gone, storage associated with this object
446 * may be relinquished.
448 * No object may be locked.
451 vm_object_deallocate(vm_object_t object)
455 while (object != NULL) {
460 VM_OBJECT_LOCK(object);
461 if (object->type == OBJT_VNODE) {
462 struct vnode *vp = (struct vnode *) object->handle;
465 * Conditionally acquire Giant for a vnode-backed
466 * object. We have to be careful since the type of
467 * a vnode object can change while the object is
470 if (VFS_NEEDSGIANT(vp->v_mount) && !vfslocked) {
472 if (!mtx_trylock(&Giant)) {
473 VM_OBJECT_UNLOCK(object);
478 vm_object_vndeallocate(object);
479 VFS_UNLOCK_GIANT(vfslocked);
483 * This is to handle the case that the object
484 * changed type while we dropped its lock to
487 VFS_UNLOCK_GIANT(vfslocked);
489 KASSERT(object->ref_count != 0,
490 ("vm_object_deallocate: object deallocated too many times: %d", object->type));
493 * If the reference count goes to 0 we start calling
494 * vm_object_terminate() on the object chain.
495 * A ref count of 1 may be a special case depending on the
496 * shadow count being 0 or 1.
499 if (object->ref_count > 1) {
500 VM_OBJECT_UNLOCK(object);
502 } else if (object->ref_count == 1) {
503 if (object->shadow_count == 0 &&
504 object->handle == NULL &&
505 (object->type == OBJT_DEFAULT ||
506 object->type == OBJT_SWAP)) {
507 vm_object_set_flag(object, OBJ_ONEMAPPING);
508 } else if ((object->shadow_count == 1) &&
509 (object->handle == NULL) &&
510 (object->type == OBJT_DEFAULT ||
511 object->type == OBJT_SWAP)) {
514 robject = LIST_FIRST(&object->shadow_head);
515 KASSERT(robject != NULL,
516 ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
518 object->shadow_count));
519 if (!VM_OBJECT_TRYLOCK(robject)) {
521 * Avoid a potential deadlock.
524 VM_OBJECT_UNLOCK(object);
526 * More likely than not the thread
527 * holding robject's lock has lower
528 * priority than the current thread.
529 * Let the lower priority thread run.
535 * Collapse object into its shadow unless its
536 * shadow is dead. In that case, object will
537 * be deallocated by the thread that is
538 * deallocating its shadow.
540 if ((robject->flags & OBJ_DEAD) == 0 &&
541 (robject->handle == NULL) &&
542 (robject->type == OBJT_DEFAULT ||
543 robject->type == OBJT_SWAP)) {
545 robject->ref_count++;
547 if (robject->paging_in_progress) {
548 VM_OBJECT_UNLOCK(object);
549 vm_object_pip_wait(robject,
551 temp = robject->backing_object;
552 if (object == temp) {
553 VM_OBJECT_LOCK(object);
556 } else if (object->paging_in_progress) {
557 VM_OBJECT_UNLOCK(robject);
558 object->flags |= OBJ_PIPWNT;
560 VM_OBJECT_MTX(object),
561 PDROP | PVM, "objde2", 0);
562 VM_OBJECT_LOCK(robject);
563 temp = robject->backing_object;
564 if (object == temp) {
565 VM_OBJECT_LOCK(object);
569 VM_OBJECT_UNLOCK(object);
571 if (robject->ref_count == 1) {
572 robject->ref_count--;
577 vm_object_collapse(object);
578 VM_OBJECT_UNLOCK(object);
581 VM_OBJECT_UNLOCK(robject);
583 VM_OBJECT_UNLOCK(object);
587 temp = object->backing_object;
589 VM_OBJECT_LOCK(temp);
590 LIST_REMOVE(object, shadow_list);
591 temp->shadow_count--;
593 VM_OBJECT_UNLOCK(temp);
594 object->backing_object = NULL;
597 * Don't double-terminate, we could be in a termination
598 * recursion due to the terminate having to sync data
601 if ((object->flags & OBJ_DEAD) == 0)
602 vm_object_terminate(object);
604 VM_OBJECT_UNLOCK(object);
610 * vm_object_destroy removes the object from the global object list
611 * and frees the space for the object.
614 vm_object_destroy(vm_object_t object)
618 * Remove the object from the global object list.
620 mtx_lock(&vm_object_list_mtx);
621 TAILQ_REMOVE(&vm_object_list, object, object_list);
622 mtx_unlock(&vm_object_list_mtx);
625 * Free the space for the object.
627 uma_zfree(obj_zone, object);
631 * vm_object_terminate actually destroys the specified object, freeing
632 * up all previously used resources.
634 * The object must be locked.
635 * This routine may block.
638 vm_object_terminate(vm_object_t object)
642 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
645 * Make sure no one uses us.
647 vm_object_set_flag(object, OBJ_DEAD);
650 * wait for the pageout daemon to be done with the object
652 vm_object_pip_wait(object, "objtrm");
654 KASSERT(!object->paging_in_progress,
655 ("vm_object_terminate: pageout in progress"));
658 * Clean and free the pages, as appropriate. All references to the
659 * object are gone, so we don't need to lock it.
661 if (object->type == OBJT_VNODE) {
662 struct vnode *vp = (struct vnode *)object->handle;
665 * Clean pages and flush buffers.
667 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
668 VM_OBJECT_UNLOCK(object);
670 vinvalbuf(vp, V_SAVE, NULL, 0, 0);
672 VM_OBJECT_LOCK(object);
675 KASSERT(object->ref_count == 0,
676 ("vm_object_terminate: object with references, ref_count=%d",
680 * Now free any remaining pages. For internal objects, this also
681 * removes them from paging queues. Don't free wired pages, just
682 * remove them from the object.
684 vm_page_lock_queues();
685 while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
686 KASSERT(!p->busy && (p->oflags & VPO_BUSY) == 0,
687 ("vm_object_terminate: freeing busy page %p "
688 "p->busy = %d, p->flags %x\n", p, p->busy, p->flags));
689 if (p->wire_count == 0) {
696 vm_page_unlock_queues();
698 #if VM_NRESERVLEVEL > 0
699 if (__predict_false(!LIST_EMPTY(&object->rvq)))
700 vm_reserv_break_all(object);
702 if (__predict_false(object->cache != NULL))
703 vm_page_cache_free(object, 0, 0);
706 * Let the pager know object is dead.
708 vm_pager_deallocate(object);
709 VM_OBJECT_UNLOCK(object);
711 vm_object_destroy(object);
715 * vm_object_page_clean
717 * Clean all dirty pages in the specified range of object. Leaves page
718 * on whatever queue it is currently on. If NOSYNC is set then do not
719 * write out pages with VPO_NOSYNC set (originally comes from MAP_NOSYNC),
720 * leaving the object dirty.
722 * When stuffing pages asynchronously, allow clustering. XXX we need a
723 * synchronous clustering mode implementation.
725 * Odd semantics: if start == end, we clean everything.
727 * The object must be locked.
730 vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end, int flags)
733 vm_pindex_t tstart, tend;
739 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
740 if (object->type != OBJT_VNODE ||
741 (object->flags & OBJ_MIGHTBEDIRTY) == 0)
744 pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
745 pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
747 vm_object_set_flag(object, OBJ_CLEANING);
756 vm_page_lock_queues();
758 * If the caller is smart and only msync()s a range he knows is
759 * dirty, we may be able to avoid an object scan. This results in
760 * a phenominal improvement in performance. We cannot do this
761 * as a matter of course because the object may be huge - e.g.
762 * the size might be in the gigabytes or terrabytes.
764 if (msync_flush_flags & MSYNC_FLUSH_HARDSEQ) {
769 scanreset = object->resident_page_count / EASY_SCAN_FACTOR;
772 pagerflags |= VM_PAGER_IGNORE_CLEANCHK;
774 scanlimit = scanreset;
776 while (tscan < tend) {
777 curgeneration = object->generation;
778 p = vm_page_lookup(object, tscan);
779 if (p == NULL || p->valid == 0) {
780 if (--scanlimit == 0)
785 vm_page_test_dirty(p);
786 if ((p->dirty & p->valid) == 0) {
787 if (--scanlimit == 0)
793 * If we have been asked to skip nosync pages and
794 * this is a nosync page, we can't continue.
796 if ((flags & OBJPC_NOSYNC) && (p->oflags & VPO_NOSYNC)) {
797 if (--scanlimit == 0)
802 scanlimit = scanreset;
805 * This returns 0 if it was unable to busy the first
806 * page (i.e. had to sleep).
808 tscan += vm_object_page_collect_flush(object, p, curgeneration, pagerflags);
812 * If everything was dirty and we flushed it successfully,
813 * and the requested range is not the entire object, we
814 * don't have to mess with CLEANCHK or MIGHTBEDIRTY and can
815 * return immediately.
817 if (tscan >= tend && (tstart || tend < object->size)) {
818 vm_page_unlock_queues();
819 vm_object_clear_flag(object, OBJ_CLEANING);
822 pagerflags &= ~VM_PAGER_IGNORE_CLEANCHK;
826 * Generally set CLEANCHK interlock and make the page read-only so
827 * we can then clear the object flags.
829 * However, if this is a nosync mmap then the object is likely to
830 * stay dirty so do not mess with the page and do not clear the
834 TAILQ_FOREACH(p, &object->memq, listq) {
835 p->oflags |= VPO_CLEANCHK;
836 if ((flags & OBJPC_NOSYNC) && (p->oflags & VPO_NOSYNC))
839 pmap_remove_write(p);
842 if (clearobjflags && (tstart == 0) && (tend == object->size)) {
845 vm_object_clear_flag(object, OBJ_MIGHTBEDIRTY);
846 if (object->type == OBJT_VNODE &&
847 (vp = (struct vnode *)object->handle) != NULL) {
849 if (vp->v_iflag & VI_OBJDIRTY)
850 vp->v_iflag &= ~VI_OBJDIRTY;
856 curgeneration = object->generation;
858 for (p = TAILQ_FIRST(&object->memq); p; p = np) {
861 np = TAILQ_NEXT(p, listq);
865 if ((p->oflags & VPO_CLEANCHK) == 0 ||
866 (pi < tstart) || (pi >= tend) ||
868 p->oflags &= ~VPO_CLEANCHK;
872 vm_page_test_dirty(p);
873 if ((p->dirty & p->valid) == 0) {
874 p->oflags &= ~VPO_CLEANCHK;
879 * If we have been asked to skip nosync pages and this is a
880 * nosync page, skip it. Note that the object flags were
881 * not cleared in this case so we do not have to set them.
883 if ((flags & OBJPC_NOSYNC) && (p->oflags & VPO_NOSYNC)) {
884 p->oflags &= ~VPO_CLEANCHK;
888 n = vm_object_page_collect_flush(object, p,
889 curgeneration, pagerflags);
893 if (object->generation != curgeneration)
897 * Try to optimize the next page. If we can't we pick up
898 * our (random) scan where we left off.
900 if (msync_flush_flags & MSYNC_FLUSH_SOFTSEQ) {
901 if ((p = vm_page_lookup(object, pi + n)) != NULL)
905 vm_page_unlock_queues();
907 VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
910 vm_object_clear_flag(object, OBJ_CLEANING);
915 vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags)
923 vm_page_t maf[vm_pageout_page_count];
924 vm_page_t mab[vm_pageout_page_count];
925 vm_page_t ma[vm_pageout_page_count];
927 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
929 while (vm_page_sleep_if_busy(p, TRUE, "vpcwai")) {
930 vm_page_lock_queues();
931 if (object->generation != curgeneration) {
936 for(i = 1; i < vm_pageout_page_count; 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->oflags & VPO_CLEANCHK) == 0) ||
945 vm_page_test_dirty(tp);
946 if ((tp->dirty & tp->valid) == 0) {
947 tp->oflags &= ~VPO_CLEANCHK;
958 chkb = vm_pageout_page_count - maxf;
960 for(i = 1; i < chkb;i++) {
963 if ((tp = vm_page_lookup(object, pi - i)) != NULL) {
964 if ((tp->oflags & VPO_BUSY) ||
965 ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
966 (tp->oflags & VPO_CLEANCHK) == 0) ||
969 vm_page_test_dirty(tp);
970 if ((tp->dirty & tp->valid) == 0) {
971 tp->oflags &= ~VPO_CLEANCHK;
982 for(i = 0; i < maxb; i++) {
983 int index = (maxb - i) - 1;
985 ma[index]->oflags &= ~VPO_CLEANCHK;
987 p->oflags &= ~VPO_CLEANCHK;
989 for(i = 0; i < maxf; i++) {
990 int index = (maxb + i) + 1;
992 ma[index]->oflags &= ~VPO_CLEANCHK;
994 runlen = maxb + maxf + 1;
996 vm_pageout_flush(ma, runlen, pagerflags);
997 for (i = 0; i < runlen; i++) {
998 if (ma[i]->valid & ma[i]->dirty) {
999 pmap_remove_write(ma[i]);
1000 ma[i]->oflags |= VPO_CLEANCHK;
1003 * maxf will end up being the actual number of pages
1004 * we wrote out contiguously, non-inclusive of the
1005 * first page. We do not count look-behind pages.
1007 if (i >= maxb + 1 && (maxf > i - maxb - 1))
1008 maxf = i - maxb - 1;
1015 * Note that there is absolutely no sense in writing out
1016 * anonymous objects, so we track down the vnode object
1018 * We invalidate (remove) all pages from the address space
1019 * for semantic correctness.
1021 * Note: certain anonymous maps, such as MAP_NOSYNC maps,
1022 * may start out with a NULL object.
1025 vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size,
1026 boolean_t syncio, boolean_t invalidate)
1028 vm_object_t backing_object;
1035 VM_OBJECT_LOCK(object);
1036 while ((backing_object = object->backing_object) != NULL) {
1037 VM_OBJECT_LOCK(backing_object);
1038 offset += object->backing_object_offset;
1039 VM_OBJECT_UNLOCK(object);
1040 object = backing_object;
1041 if (object->size < OFF_TO_IDX(offset + size))
1042 size = IDX_TO_OFF(object->size) - offset;
1045 * Flush pages if writing is allowed, invalidate them
1046 * if invalidation requested. Pages undergoing I/O
1047 * will be ignored by vm_object_page_remove().
1049 * We cannot lock the vnode and then wait for paging
1050 * to complete without deadlocking against vm_fault.
1051 * Instead we simply call vm_object_page_remove() and
1052 * allow it to block internally on a page-by-page
1053 * basis when it encounters pages undergoing async
1056 if (object->type == OBJT_VNODE &&
1057 (object->flags & OBJ_MIGHTBEDIRTY) != 0) {
1059 vp = object->handle;
1060 VM_OBJECT_UNLOCK(object);
1061 (void) vn_start_write(vp, &mp, V_WAIT);
1062 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1063 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
1064 flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
1065 flags |= invalidate ? OBJPC_INVAL : 0;
1066 VM_OBJECT_LOCK(object);
1067 vm_object_page_clean(object,
1069 OFF_TO_IDX(offset + size + PAGE_MASK),
1071 VM_OBJECT_UNLOCK(object);
1072 VOP_UNLOCK(vp, 0, curthread);
1073 VFS_UNLOCK_GIANT(vfslocked);
1074 vn_finished_write(mp);
1075 VM_OBJECT_LOCK(object);
1077 if ((object->type == OBJT_VNODE ||
1078 object->type == OBJT_DEVICE) && invalidate) {
1080 purge = old_msync || (object->type == OBJT_DEVICE);
1081 vm_object_page_remove(object,
1083 OFF_TO_IDX(offset + size + PAGE_MASK),
1084 purge ? FALSE : TRUE);
1086 VM_OBJECT_UNLOCK(object);
1090 * vm_object_madvise:
1092 * Implements the madvise function at the object/page level.
1094 * MADV_WILLNEED (any object)
1096 * Activate the specified pages if they are resident.
1098 * MADV_DONTNEED (any object)
1100 * Deactivate the specified pages if they are resident.
1102 * MADV_FREE (OBJT_DEFAULT/OBJT_SWAP objects,
1103 * OBJ_ONEMAPPING only)
1105 * Deactivate and clean the specified pages if they are
1106 * resident. This permits the process to reuse the pages
1107 * without faulting or the kernel to reclaim the pages
1111 vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
1113 vm_pindex_t end, tpindex;
1114 vm_object_t backing_object, tobject;
1119 VM_OBJECT_LOCK(object);
1120 end = pindex + count;
1122 * Locate and adjust resident pages
1124 for (; pindex < end; pindex += 1) {
1130 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
1131 * and those pages must be OBJ_ONEMAPPING.
1133 if (advise == MADV_FREE) {
1134 if ((tobject->type != OBJT_DEFAULT &&
1135 tobject->type != OBJT_SWAP) ||
1136 (tobject->flags & OBJ_ONEMAPPING) == 0) {
1137 goto unlock_tobject;
1140 m = vm_page_lookup(tobject, tpindex);
1141 if (m == NULL && advise == MADV_WILLNEED) {
1143 * If the page is cached, reactivate it.
1145 m = vm_page_alloc(tobject, tpindex, VM_ALLOC_IFCACHED |
1150 * There may be swap even if there is no backing page
1152 if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1153 swap_pager_freespace(tobject, tpindex, 1);
1157 backing_object = tobject->backing_object;
1158 if (backing_object == NULL)
1159 goto unlock_tobject;
1160 VM_OBJECT_LOCK(backing_object);
1161 tpindex += OFF_TO_IDX(tobject->backing_object_offset);
1162 if (tobject != object)
1163 VM_OBJECT_UNLOCK(tobject);
1164 tobject = backing_object;
1168 * If the page is busy or not in a normal active state,
1169 * we skip it. If the page is not managed there are no
1170 * page queues to mess with. Things can break if we mess
1171 * with pages in any of the below states.
1173 vm_page_lock_queues();
1174 if (m->hold_count ||
1176 (m->flags & PG_UNMANAGED) ||
1177 m->valid != VM_PAGE_BITS_ALL) {
1178 vm_page_unlock_queues();
1179 goto unlock_tobject;
1181 if ((m->oflags & VPO_BUSY) || m->busy) {
1182 vm_page_flag_set(m, PG_REFERENCED);
1183 vm_page_unlock_queues();
1184 if (object != tobject)
1185 VM_OBJECT_UNLOCK(object);
1186 m->oflags |= VPO_WANTED;
1187 msleep(m, VM_OBJECT_MTX(tobject), PDROP | PVM, "madvpo", 0);
1188 VM_OBJECT_LOCK(object);
1191 if (advise == MADV_WILLNEED) {
1192 vm_page_activate(m);
1193 } else if (advise == MADV_DONTNEED) {
1194 vm_page_dontneed(m);
1195 } else if (advise == MADV_FREE) {
1197 * Mark the page clean. This will allow the page
1198 * to be freed up by the system. However, such pages
1199 * are often reused quickly by malloc()/free()
1200 * so we do not do anything that would cause
1201 * a page fault if we can help it.
1203 * Specifically, we do not try to actually free
1204 * the page now nor do we try to put it in the
1205 * cache (which would cause a page fault on reuse).
1207 * But we do make the page is freeable as we
1208 * can without actually taking the step of unmapping
1211 pmap_clear_modify(m);
1214 vm_page_dontneed(m);
1216 vm_page_unlock_queues();
1217 if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1218 swap_pager_freespace(tobject, tpindex, 1);
1220 if (tobject != object)
1221 VM_OBJECT_UNLOCK(tobject);
1223 VM_OBJECT_UNLOCK(object);
1229 * Create a new object which is backed by the
1230 * specified existing object range. The source
1231 * object reference is deallocated.
1233 * The new object and offset into that object
1234 * are returned in the source parameters.
1238 vm_object_t *object, /* IN/OUT */
1239 vm_ooffset_t *offset, /* IN/OUT */
1248 * Don't create the new object if the old object isn't shared.
1250 if (source != NULL) {
1251 VM_OBJECT_LOCK(source);
1252 if (source->ref_count == 1 &&
1253 source->handle == NULL &&
1254 (source->type == OBJT_DEFAULT ||
1255 source->type == OBJT_SWAP)) {
1256 VM_OBJECT_UNLOCK(source);
1259 VM_OBJECT_UNLOCK(source);
1263 * Allocate a new object with the given length.
1265 result = vm_object_allocate(OBJT_DEFAULT, length);
1268 * The new object shadows the source object, adding a reference to it.
1269 * Our caller changes his reference to point to the new object,
1270 * removing a reference to the source object. Net result: no change
1271 * of reference count.
1273 * Try to optimize the result object's page color when shadowing
1274 * in order to maintain page coloring consistency in the combined
1277 result->backing_object = source;
1279 * Store the offset into the source object, and fix up the offset into
1282 result->backing_object_offset = *offset;
1283 if (source != NULL) {
1284 VM_OBJECT_LOCK(source);
1285 LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1286 source->shadow_count++;
1287 source->generation++;
1288 #if VM_NRESERVLEVEL > 0
1289 result->flags |= source->flags & (OBJ_NEEDGIANT | OBJ_COLORED);
1290 result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) &
1291 ((1 << (VM_NFREEORDER - 1)) - 1);
1293 result->flags |= source->flags & OBJ_NEEDGIANT;
1295 VM_OBJECT_UNLOCK(source);
1300 * Return the new things
1309 * Split the pages in a map entry into a new object. This affords
1310 * easier removal of unused pages, and keeps object inheritance from
1311 * being a negative impact on memory usage.
1314 vm_object_split(vm_map_entry_t entry)
1316 vm_page_t m, m_next;
1317 vm_object_t orig_object, new_object, source;
1318 vm_pindex_t idx, offidxstart;
1321 orig_object = entry->object.vm_object;
1322 if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1324 if (orig_object->ref_count <= 1)
1326 VM_OBJECT_UNLOCK(orig_object);
1328 offidxstart = OFF_TO_IDX(entry->offset);
1329 size = atop(entry->end - entry->start);
1332 * If swap_pager_copy() is later called, it will convert new_object
1333 * into a swap object.
1335 new_object = vm_object_allocate(OBJT_DEFAULT, size);
1338 * At this point, the new object is still private, so the order in
1339 * which the original and new objects are locked does not matter.
1341 VM_OBJECT_LOCK(new_object);
1342 VM_OBJECT_LOCK(orig_object);
1343 source = orig_object->backing_object;
1344 if (source != NULL) {
1345 VM_OBJECT_LOCK(source);
1346 if ((source->flags & OBJ_DEAD) != 0) {
1347 VM_OBJECT_UNLOCK(source);
1348 VM_OBJECT_UNLOCK(orig_object);
1349 VM_OBJECT_UNLOCK(new_object);
1350 vm_object_deallocate(new_object);
1351 VM_OBJECT_LOCK(orig_object);
1354 LIST_INSERT_HEAD(&source->shadow_head,
1355 new_object, shadow_list);
1356 source->shadow_count++;
1357 source->generation++;
1358 vm_object_reference_locked(source); /* for new_object */
1359 vm_object_clear_flag(source, OBJ_ONEMAPPING);
1360 VM_OBJECT_UNLOCK(source);
1361 new_object->backing_object_offset =
1362 orig_object->backing_object_offset + entry->offset;
1363 new_object->backing_object = source;
1365 new_object->flags |= orig_object->flags & OBJ_NEEDGIANT;
1367 if ((m = TAILQ_FIRST(&orig_object->memq)) != NULL) {
1368 if (m->pindex < offidxstart) {
1369 m = vm_page_splay(offidxstart, orig_object->root);
1370 if ((orig_object->root = m)->pindex < offidxstart)
1371 m = TAILQ_NEXT(m, listq);
1374 vm_page_lock_queues();
1375 for (; m != NULL && (idx = m->pindex - offidxstart) < size;
1377 m_next = TAILQ_NEXT(m, listq);
1380 * We must wait for pending I/O to complete before we can
1383 * We do not have to VM_PROT_NONE the page as mappings should
1384 * not be changed by this operation.
1386 if ((m->oflags & VPO_BUSY) || m->busy) {
1387 vm_page_flag_set(m, PG_REFERENCED);
1388 vm_page_unlock_queues();
1389 VM_OBJECT_UNLOCK(new_object);
1390 m->oflags |= VPO_WANTED;
1391 msleep(m, VM_OBJECT_MTX(orig_object), PVM, "spltwt", 0);
1392 VM_OBJECT_LOCK(new_object);
1395 vm_page_rename(m, new_object, idx);
1396 /* page automatically made dirty by rename and cache handled */
1399 vm_page_unlock_queues();
1400 if (orig_object->type == OBJT_SWAP) {
1402 * swap_pager_copy() can sleep, in which case the orig_object's
1403 * and new_object's locks are released and reacquired.
1405 swap_pager_copy(orig_object, new_object, offidxstart, 0);
1408 * Transfer any cached pages from orig_object to new_object.
1410 if (__predict_false(orig_object->cache != NULL))
1411 vm_page_cache_transfer(orig_object, offidxstart,
1414 VM_OBJECT_UNLOCK(orig_object);
1415 TAILQ_FOREACH(m, &new_object->memq, listq)
1417 VM_OBJECT_UNLOCK(new_object);
1418 entry->object.vm_object = new_object;
1419 entry->offset = 0LL;
1420 vm_object_deallocate(orig_object);
1421 VM_OBJECT_LOCK(new_object);
1424 #define OBSC_TEST_ALL_SHADOWED 0x0001
1425 #define OBSC_COLLAPSE_NOWAIT 0x0002
1426 #define OBSC_COLLAPSE_WAIT 0x0004
1429 vm_object_backing_scan(vm_object_t object, int op)
1433 vm_object_t backing_object;
1434 vm_pindex_t backing_offset_index;
1436 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1437 VM_OBJECT_LOCK_ASSERT(object->backing_object, MA_OWNED);
1439 backing_object = object->backing_object;
1440 backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
1443 * Initial conditions
1445 if (op & OBSC_TEST_ALL_SHADOWED) {
1447 * We do not want to have to test for the existence of cache
1448 * or swap pages in the backing object. XXX but with the
1449 * new swapper this would be pretty easy to do.
1451 * XXX what about anonymous MAP_SHARED memory that hasn't
1452 * been ZFOD faulted yet? If we do not test for this, the
1453 * shadow test may succeed! XXX
1455 if (backing_object->type != OBJT_DEFAULT) {
1459 if (op & OBSC_COLLAPSE_WAIT) {
1460 vm_object_set_flag(backing_object, OBJ_DEAD);
1466 p = TAILQ_FIRST(&backing_object->memq);
1468 vm_page_t next = TAILQ_NEXT(p, listq);
1469 vm_pindex_t new_pindex = p->pindex - backing_offset_index;
1471 if (op & OBSC_TEST_ALL_SHADOWED) {
1475 * Ignore pages outside the parent object's range
1476 * and outside the parent object's mapping of the
1479 * note that we do not busy the backing object's
1483 p->pindex < backing_offset_index ||
1484 new_pindex >= object->size
1491 * See if the parent has the page or if the parent's
1492 * object pager has the page. If the parent has the
1493 * page but the page is not valid, the parent's
1494 * object pager must have the page.
1496 * If this fails, the parent does not completely shadow
1497 * the object and we might as well give up now.
1500 pp = vm_page_lookup(object, new_pindex);
1502 (pp == NULL || pp->valid == 0) &&
1503 !vm_pager_has_page(object, new_pindex, NULL, NULL)
1511 * Check for busy page
1513 if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
1516 if (op & OBSC_COLLAPSE_NOWAIT) {
1517 if ((p->oflags & VPO_BUSY) ||
1523 } else if (op & OBSC_COLLAPSE_WAIT) {
1524 if ((p->oflags & VPO_BUSY) || p->busy) {
1525 vm_page_lock_queues();
1526 vm_page_flag_set(p, PG_REFERENCED);
1527 vm_page_unlock_queues();
1528 VM_OBJECT_UNLOCK(object);
1529 p->oflags |= VPO_WANTED;
1530 msleep(p, VM_OBJECT_MTX(backing_object),
1531 PDROP | PVM, "vmocol", 0);
1532 VM_OBJECT_LOCK(object);
1533 VM_OBJECT_LOCK(backing_object);
1535 * If we slept, anything could have
1536 * happened. Since the object is
1537 * marked dead, the backing offset
1538 * should not have changed so we
1539 * just restart our scan.
1541 p = TAILQ_FIRST(&backing_object->memq);
1547 p->object == backing_object,
1548 ("vm_object_backing_scan: object mismatch")
1552 * Destroy any associated swap
1554 if (backing_object->type == OBJT_SWAP) {
1555 swap_pager_freespace(
1563 p->pindex < backing_offset_index ||
1564 new_pindex >= object->size
1567 * Page is out of the parent object's range, we
1568 * can simply destroy it.
1570 vm_page_lock_queues();
1571 KASSERT(!pmap_page_is_mapped(p),
1572 ("freeing mapped page %p", p));
1573 if (p->wire_count == 0)
1577 vm_page_unlock_queues();
1582 pp = vm_page_lookup(object, new_pindex);
1585 vm_pager_has_page(object, new_pindex, NULL, NULL)
1588 * page already exists in parent OR swap exists
1589 * for this location in the parent. Destroy
1590 * the original page from the backing object.
1592 * Leave the parent's page alone
1594 vm_page_lock_queues();
1595 KASSERT(!pmap_page_is_mapped(p),
1596 ("freeing mapped page %p", p));
1597 if (p->wire_count == 0)
1601 vm_page_unlock_queues();
1606 #if VM_NRESERVLEVEL > 0
1608 * Rename the reservation.
1610 vm_reserv_rename(p, object, backing_object,
1611 backing_offset_index);
1615 * Page does not exist in parent, rename the
1616 * page from the backing object to the main object.
1618 * If the page was mapped to a process, it can remain
1619 * mapped through the rename.
1621 vm_page_lock_queues();
1622 vm_page_rename(p, object, new_pindex);
1623 vm_page_unlock_queues();
1624 /* page automatically made dirty by rename */
1633 * this version of collapse allows the operation to occur earlier and
1634 * when paging_in_progress is true for an object... This is not a complete
1635 * operation, but should plug 99.9% of the rest of the leaks.
1638 vm_object_qcollapse(vm_object_t object)
1640 vm_object_t backing_object = object->backing_object;
1642 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1643 VM_OBJECT_LOCK_ASSERT(backing_object, MA_OWNED);
1645 if (backing_object->ref_count != 1)
1648 vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
1652 * vm_object_collapse:
1654 * Collapse an object with the object backing it.
1655 * Pages in the backing object are moved into the
1656 * parent, and the backing object is deallocated.
1659 vm_object_collapse(vm_object_t object)
1661 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1664 vm_object_t backing_object;
1667 * Verify that the conditions are right for collapse:
1669 * The object exists and the backing object exists.
1671 if ((backing_object = object->backing_object) == NULL)
1675 * we check the backing object first, because it is most likely
1678 VM_OBJECT_LOCK(backing_object);
1679 if (backing_object->handle != NULL ||
1680 (backing_object->type != OBJT_DEFAULT &&
1681 backing_object->type != OBJT_SWAP) ||
1682 (backing_object->flags & OBJ_DEAD) ||
1683 object->handle != NULL ||
1684 (object->type != OBJT_DEFAULT &&
1685 object->type != OBJT_SWAP) ||
1686 (object->flags & OBJ_DEAD)) {
1687 VM_OBJECT_UNLOCK(backing_object);
1692 object->paging_in_progress != 0 ||
1693 backing_object->paging_in_progress != 0
1695 vm_object_qcollapse(object);
1696 VM_OBJECT_UNLOCK(backing_object);
1700 * We know that we can either collapse the backing object (if
1701 * the parent is the only reference to it) or (perhaps) have
1702 * the parent bypass the object if the parent happens to shadow
1703 * all the resident pages in the entire backing object.
1705 * This is ignoring pager-backed pages such as swap pages.
1706 * vm_object_backing_scan fails the shadowing test in this
1709 if (backing_object->ref_count == 1) {
1711 * If there is exactly one reference to the backing
1712 * object, we can collapse it into the parent.
1714 vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1716 #if VM_NRESERVLEVEL > 0
1718 * Break any reservations from backing_object.
1720 if (__predict_false(!LIST_EMPTY(&backing_object->rvq)))
1721 vm_reserv_break_all(backing_object);
1725 * Move the pager from backing_object to object.
1727 if (backing_object->type == OBJT_SWAP) {
1729 * swap_pager_copy() can sleep, in which case
1730 * the backing_object's and object's locks are
1731 * released and reacquired.
1736 OFF_TO_IDX(object->backing_object_offset), TRUE);
1739 * Free any cached pages from backing_object.
1741 if (__predict_false(backing_object->cache != NULL))
1742 vm_page_cache_free(backing_object, 0, 0);
1745 * Object now shadows whatever backing_object did.
1746 * Note that the reference to
1747 * backing_object->backing_object moves from within
1748 * backing_object to within object.
1750 LIST_REMOVE(object, shadow_list);
1751 backing_object->shadow_count--;
1752 backing_object->generation++;
1753 if (backing_object->backing_object) {
1754 VM_OBJECT_LOCK(backing_object->backing_object);
1755 LIST_REMOVE(backing_object, shadow_list);
1757 &backing_object->backing_object->shadow_head,
1758 object, shadow_list);
1760 * The shadow_count has not changed.
1762 backing_object->backing_object->generation++;
1763 VM_OBJECT_UNLOCK(backing_object->backing_object);
1765 object->backing_object = backing_object->backing_object;
1766 object->backing_object_offset +=
1767 backing_object->backing_object_offset;
1770 * Discard backing_object.
1772 * Since the backing object has no pages, no pager left,
1773 * and no object references within it, all that is
1774 * necessary is to dispose of it.
1776 KASSERT(backing_object->ref_count == 1, ("backing_object %p was somehow re-referenced during collapse!", backing_object));
1777 VM_OBJECT_UNLOCK(backing_object);
1779 mtx_lock(&vm_object_list_mtx);
1785 mtx_unlock(&vm_object_list_mtx);
1787 uma_zfree(obj_zone, backing_object);
1791 vm_object_t new_backing_object;
1794 * If we do not entirely shadow the backing object,
1795 * there is nothing we can do so we give up.
1797 if (object->resident_page_count != object->size &&
1798 vm_object_backing_scan(object,
1799 OBSC_TEST_ALL_SHADOWED) == 0) {
1800 VM_OBJECT_UNLOCK(backing_object);
1805 * Make the parent shadow the next object in the
1806 * chain. Deallocating backing_object will not remove
1807 * it, since its reference count is at least 2.
1809 LIST_REMOVE(object, shadow_list);
1810 backing_object->shadow_count--;
1811 backing_object->generation++;
1813 new_backing_object = backing_object->backing_object;
1814 if ((object->backing_object = new_backing_object) != NULL) {
1815 VM_OBJECT_LOCK(new_backing_object);
1817 &new_backing_object->shadow_head,
1821 new_backing_object->shadow_count++;
1822 new_backing_object->generation++;
1823 vm_object_reference_locked(new_backing_object);
1824 VM_OBJECT_UNLOCK(new_backing_object);
1825 object->backing_object_offset +=
1826 backing_object->backing_object_offset;
1830 * Drop the reference count on backing_object. Since
1831 * its ref_count was at least 2, it will not vanish.
1833 backing_object->ref_count--;
1834 VM_OBJECT_UNLOCK(backing_object);
1839 * Try again with this object's new backing object.
1845 * vm_object_page_remove:
1847 * For the given object, either frees or invalidates each of the
1848 * specified pages. In general, a page is freed. However, if a
1849 * page is wired for any reason other than the existence of a
1850 * managed, wired mapping, then it may be invalidated but not
1851 * removed from the object. Pages are specified by the given
1852 * range ["start", "end") and Boolean "clean_only". As a
1853 * special case, if "end" is zero, then the range extends from
1854 * "start" to the end of the object. If "clean_only" is TRUE,
1855 * then only the non-dirty pages within the specified range are
1858 * In general, this operation should only be performed on objects
1859 * that contain managed pages. There are two exceptions. First,
1860 * it may be performed on the kernel and kmem objects. Second,
1861 * it may be used by msync(..., MS_INVALIDATE) to invalidate
1862 * device-backed pages.
1864 * The object must be locked.
1867 vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
1868 boolean_t clean_only)
1872 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1873 if (object->resident_page_count == 0)
1877 * Since physically-backed objects do not use managed pages, we can't
1878 * remove pages from the object (we must instead remove the page
1879 * references, and then destroy the object).
1881 KASSERT(object->type != OBJT_PHYS || object == kernel_object ||
1882 object == kmem_object,
1883 ("attempt to remove pages from a physical object"));
1885 vm_object_pip_add(object, 1);
1887 vm_page_lock_queues();
1888 if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
1889 if (p->pindex < start) {
1890 p = vm_page_splay(start, object->root);
1891 if ((object->root = p)->pindex < start)
1892 p = TAILQ_NEXT(p, listq);
1896 * Assert: the variable p is either (1) the page with the
1897 * least pindex greater than or equal to the parameter pindex
1901 p != NULL && (p->pindex < end || end == 0);
1903 next = TAILQ_NEXT(p, listq);
1905 if (p->wire_count != 0) {
1906 /* Fictitious pages do not have managed mappings. */
1907 if ((p->flags & PG_FICTITIOUS) == 0)
1913 if (vm_page_sleep_if_busy(p, TRUE, "vmopar"))
1915 KASSERT((p->flags & PG_FICTITIOUS) == 0,
1916 ("vm_object_page_remove: page %p is fictitious", p));
1917 if (clean_only && p->valid) {
1918 pmap_remove_write(p);
1919 if (p->valid & p->dirty)
1925 vm_page_unlock_queues();
1926 vm_object_pip_wakeup(object);
1928 if (__predict_false(object->cache != NULL))
1929 vm_page_cache_free(object, start, end);
1933 * Routine: vm_object_coalesce
1934 * Function: Coalesces two objects backing up adjoining
1935 * regions of memory into a single object.
1937 * returns TRUE if objects were combined.
1939 * NOTE: Only works at the moment if the second object is NULL -
1940 * if it's not, which object do we lock first?
1943 * prev_object First object to coalesce
1944 * prev_offset Offset into prev_object
1945 * prev_size Size of reference to prev_object
1946 * next_size Size of reference to the second object
1949 * The object must *not* be locked.
1952 vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset,
1953 vm_size_t prev_size, vm_size_t next_size)
1955 vm_pindex_t next_pindex;
1957 if (prev_object == NULL)
1959 VM_OBJECT_LOCK(prev_object);
1960 if (prev_object->type != OBJT_DEFAULT &&
1961 prev_object->type != OBJT_SWAP) {
1962 VM_OBJECT_UNLOCK(prev_object);
1967 * Try to collapse the object first
1969 vm_object_collapse(prev_object);
1972 * Can't coalesce if: . more than one reference . paged out . shadows
1973 * another object . has a copy elsewhere (any of which mean that the
1974 * pages not mapped to prev_entry may be in use anyway)
1976 if (prev_object->backing_object != NULL) {
1977 VM_OBJECT_UNLOCK(prev_object);
1981 prev_size >>= PAGE_SHIFT;
1982 next_size >>= PAGE_SHIFT;
1983 next_pindex = OFF_TO_IDX(prev_offset) + prev_size;
1985 if ((prev_object->ref_count > 1) &&
1986 (prev_object->size != next_pindex)) {
1987 VM_OBJECT_UNLOCK(prev_object);
1992 * Remove any pages that may still be in the object from a previous
1995 if (next_pindex < prev_object->size) {
1996 vm_object_page_remove(prev_object,
1998 next_pindex + next_size, FALSE);
1999 if (prev_object->type == OBJT_SWAP)
2000 swap_pager_freespace(prev_object,
2001 next_pindex, next_size);
2005 * Extend the object if necessary.
2007 if (next_pindex + next_size > prev_object->size)
2008 prev_object->size = next_pindex + next_size;
2010 VM_OBJECT_UNLOCK(prev_object);
2015 vm_object_set_writeable_dirty(vm_object_t object)
2019 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2020 if ((object->flags & OBJ_MIGHTBEDIRTY) != 0)
2022 vm_object_set_flag(object, OBJ_MIGHTBEDIRTY);
2023 if (object->type == OBJT_VNODE &&
2024 (vp = (struct vnode *)object->handle) != NULL) {
2026 vp->v_iflag |= VI_OBJDIRTY;
2031 #include "opt_ddb.h"
2033 #include <sys/kernel.h>
2035 #include <sys/cons.h>
2037 #include <ddb/ddb.h>
2040 _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
2043 vm_map_entry_t tmpe;
2051 tmpe = map->header.next;
2052 entcount = map->nentries;
2053 while (entcount-- && (tmpe != &map->header)) {
2054 if (_vm_object_in_map(map, object, tmpe)) {
2059 } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
2060 tmpm = entry->object.sub_map;
2061 tmpe = tmpm->header.next;
2062 entcount = tmpm->nentries;
2063 while (entcount-- && tmpe != &tmpm->header) {
2064 if (_vm_object_in_map(tmpm, object, tmpe)) {
2069 } else if ((obj = entry->object.vm_object) != NULL) {
2070 for (; obj; obj = obj->backing_object)
2071 if (obj == object) {
2079 vm_object_in_map(vm_object_t object)
2083 /* sx_slock(&allproc_lock); */
2084 FOREACH_PROC_IN_SYSTEM(p) {
2085 if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
2087 if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
2088 /* sx_sunlock(&allproc_lock); */
2092 /* sx_sunlock(&allproc_lock); */
2093 if (_vm_object_in_map(kernel_map, object, 0))
2095 if (_vm_object_in_map(kmem_map, object, 0))
2097 if (_vm_object_in_map(pager_map, object, 0))
2099 if (_vm_object_in_map(buffer_map, object, 0))
2104 DB_SHOW_COMMAND(vmochk, vm_object_check)
2109 * make sure that internal objs are in a map somewhere
2110 * and none have zero ref counts.
2112 TAILQ_FOREACH(object, &vm_object_list, object_list) {
2113 if (object->handle == NULL &&
2114 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2115 if (object->ref_count == 0) {
2116 db_printf("vmochk: internal obj has zero ref count: %ld\n",
2117 (long)object->size);
2119 if (!vm_object_in_map(object)) {
2121 "vmochk: internal obj is not in a map: "
2122 "ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2123 object->ref_count, (u_long)object->size,
2124 (u_long)object->size,
2125 (void *)object->backing_object);
2132 * vm_object_print: [ debug ]
2134 DB_SHOW_COMMAND(object, vm_object_print_static)
2136 /* XXX convert args. */
2137 vm_object_t object = (vm_object_t)addr;
2138 boolean_t full = have_addr;
2142 /* XXX count is an (unused) arg. Avoid shadowing it. */
2143 #define count was_count
2151 "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x\n",
2152 object, (int)object->type, (uintmax_t)object->size,
2153 object->resident_page_count, object->ref_count, object->flags);
2154 db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n",
2155 object->shadow_count,
2156 object->backing_object ? object->backing_object->ref_count : 0,
2157 object->backing_object, (uintmax_t)object->backing_object_offset);
2164 TAILQ_FOREACH(p, &object->memq, listq) {
2166 db_iprintf("memory:=");
2167 else if (count == 6) {
2175 db_printf("(off=0x%jx,page=0x%jx)",
2176 (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p));
2186 /* XXX need this non-static entry for calling from vm_map_print. */
2189 /* db_expr_t */ long addr,
2190 boolean_t have_addr,
2191 /* db_expr_t */ long count,
2194 vm_object_print_static(addr, have_addr, count, modif);
2197 DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
2203 TAILQ_FOREACH(object, &vm_object_list, object_list) {
2204 vm_pindex_t idx, fidx;
2210 db_printf("new object: %p\n", (void *)object);
2220 osize = object->size;
2223 for (idx = 0; idx < osize; idx++) {
2224 m = vm_page_lookup(object, idx);
2227 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2228 (long)fidx, rcount, (long)pa);
2243 (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
2248 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2249 (long)fidx, rcount, (long)pa);
2259 pa = VM_PAGE_TO_PHYS(m);
2263 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2264 (long)fidx, rcount, (long)pa);