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/resourcevar.h>
81 #include <sys/vnode.h>
82 #include <sys/vmmeter.h>
86 #include <vm/vm_param.h>
88 #include <vm/vm_map.h>
89 #include <vm/vm_object.h>
90 #include <vm/vm_page.h>
91 #include <vm/vm_pageout.h>
92 #include <vm/vm_pager.h>
93 #include <vm/swap_pager.h>
94 #include <vm/vm_kern.h>
95 #include <vm/vm_extern.h>
96 #include <vm/vm_reserv.h>
99 #define EASY_SCAN_FACTOR 8
101 #define MSYNC_FLUSH_HARDSEQ 0x01
102 #define MSYNC_FLUSH_SOFTSEQ 0x02
105 * msync / VM object flushing optimizations
107 static int msync_flush_flags = MSYNC_FLUSH_HARDSEQ | MSYNC_FLUSH_SOFTSEQ;
108 SYSCTL_INT(_vm, OID_AUTO, msync_flush_flags, CTLFLAG_RW, &msync_flush_flags, 0,
109 "Enable sequential iteration optimization");
111 static int old_msync;
112 SYSCTL_INT(_vm, OID_AUTO, old_msync, CTLFLAG_RW, &old_msync, 0,
113 "Use old (insecure) msync behavior");
115 static void vm_object_qcollapse(vm_object_t object);
116 static int vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags);
117 static void vm_object_vndeallocate(vm_object_t object);
120 * Virtual memory objects maintain the actual data
121 * associated with allocated virtual memory. A given
122 * page of memory exists within exactly one object.
124 * An object is only deallocated when all "references"
125 * are given up. Only one "reference" to a given
126 * region of an object should be writeable.
128 * Associated with each object is a list of all resident
129 * memory pages belonging to that object; this list is
130 * maintained by the "vm_page" module, and locked by the object's
133 * Each object also records a "pager" routine which is
134 * used to retrieve (and store) pages to the proper backing
135 * storage. In addition, objects may be backed by other
136 * objects from which they were virtual-copied.
138 * The only items within the object structure which are
139 * modified after time of creation are:
140 * reference count locked by object's lock
141 * pager routine locked by object's lock
145 struct object_q vm_object_list;
146 struct mtx vm_object_list_mtx; /* lock for object list and count */
148 struct vm_object kernel_object_store;
149 struct vm_object kmem_object_store;
151 SYSCTL_NODE(_vm_stats, OID_AUTO, object, CTLFLAG_RD, 0, "VM object stats");
153 static long object_collapses;
154 SYSCTL_LONG(_vm_stats_object, OID_AUTO, collapses, CTLFLAG_RD,
155 &object_collapses, 0, "VM object collapses");
157 static long object_bypasses;
158 SYSCTL_LONG(_vm_stats_object, OID_AUTO, bypasses, CTLFLAG_RD,
159 &object_bypasses, 0, "VM object bypasses");
161 static uma_zone_t obj_zone;
163 static int vm_object_zinit(void *mem, int size, int flags);
166 static void vm_object_zdtor(void *mem, int size, void *arg);
169 vm_object_zdtor(void *mem, int size, void *arg)
173 object = (vm_object_t)mem;
174 KASSERT(TAILQ_EMPTY(&object->memq),
175 ("object %p has resident pages",
177 #if VM_NRESERVLEVEL > 0
178 KASSERT(LIST_EMPTY(&object->rvq),
179 ("object %p has reservations",
182 KASSERT(object->cache == NULL,
183 ("object %p has cached pages",
185 KASSERT(object->paging_in_progress == 0,
186 ("object %p paging_in_progress = %d",
187 object, object->paging_in_progress));
188 KASSERT(object->resident_page_count == 0,
189 ("object %p resident_page_count = %d",
190 object, object->resident_page_count));
191 KASSERT(object->shadow_count == 0,
192 ("object %p shadow_count = %d",
193 object, object->shadow_count));
198 vm_object_zinit(void *mem, int size, int flags)
202 object = (vm_object_t)mem;
203 bzero(&object->mtx, sizeof(object->mtx));
204 VM_OBJECT_LOCK_INIT(object, "standard object");
206 /* These are true for any object that has been freed */
207 object->paging_in_progress = 0;
208 object->resident_page_count = 0;
209 object->shadow_count = 0;
214 _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
217 TAILQ_INIT(&object->memq);
218 LIST_INIT(&object->shadow_head);
223 object->generation = 1;
224 object->ref_count = 1;
225 object->memattr = VM_MEMATTR_DEFAULT;
229 if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
230 object->flags = OBJ_ONEMAPPING;
231 object->pg_color = 0;
232 object->handle = NULL;
233 object->backing_object = NULL;
234 object->backing_object_offset = (vm_ooffset_t) 0;
235 #if VM_NRESERVLEVEL > 0
236 LIST_INIT(&object->rvq);
238 object->cache = NULL;
240 mtx_lock(&vm_object_list_mtx);
241 TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
242 mtx_unlock(&vm_object_list_mtx);
248 * Initialize the VM objects module.
253 TAILQ_INIT(&vm_object_list);
254 mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF);
256 VM_OBJECT_LOCK_INIT(&kernel_object_store, "kernel object");
257 _vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
259 #if VM_NRESERVLEVEL > 0
260 kernel_object->flags |= OBJ_COLORED;
261 kernel_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS);
264 VM_OBJECT_LOCK_INIT(&kmem_object_store, "kmem object");
265 _vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
267 #if VM_NRESERVLEVEL > 0
268 kmem_object->flags |= OBJ_COLORED;
269 kmem_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS);
273 * The lock portion of struct vm_object must be type stable due
274 * to vm_pageout_fallback_object_lock locking a vm object
275 * without holding any references to it.
277 obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL,
283 vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM|UMA_ZONE_NOFREE);
287 vm_object_clear_flag(vm_object_t object, u_short bits)
290 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
291 object->flags &= ~bits;
295 * Sets the default memory attribute for the specified object. Pages
296 * that are allocated to this object are by default assigned this memory
299 * Presently, this function must be called before any pages are allocated
300 * to the object. In the future, this requirement may be relaxed for
301 * "default" and "swap" objects.
304 vm_object_set_memattr(vm_object_t object, vm_memattr_t memattr)
307 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
308 switch (object->type) {
314 if (!TAILQ_EMPTY(&object->memq))
315 return (KERN_FAILURE);
318 return (KERN_INVALID_ARGUMENT);
320 object->memattr = memattr;
321 return (KERN_SUCCESS);
325 vm_object_pip_add(vm_object_t object, short i)
328 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
329 object->paging_in_progress += i;
333 vm_object_pip_subtract(vm_object_t object, short i)
336 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
337 object->paging_in_progress -= i;
341 vm_object_pip_wakeup(vm_object_t object)
344 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
345 object->paging_in_progress--;
346 if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
347 vm_object_clear_flag(object, OBJ_PIPWNT);
353 vm_object_pip_wakeupn(vm_object_t object, short i)
356 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
358 object->paging_in_progress -= i;
359 if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
360 vm_object_clear_flag(object, OBJ_PIPWNT);
366 vm_object_pip_wait(vm_object_t object, char *waitid)
369 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
370 while (object->paging_in_progress) {
371 object->flags |= OBJ_PIPWNT;
372 msleep(object, VM_OBJECT_MTX(object), PVM, waitid, 0);
377 * vm_object_allocate:
379 * Returns a new object with the given size.
382 vm_object_allocate(objtype_t type, vm_pindex_t size)
386 object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK);
387 _vm_object_allocate(type, size, object);
393 * vm_object_reference:
395 * Gets another reference to the given object. Note: OBJ_DEAD
396 * objects can be referenced during final cleaning.
399 vm_object_reference(vm_object_t object)
403 VM_OBJECT_LOCK(object);
404 vm_object_reference_locked(object);
405 VM_OBJECT_UNLOCK(object);
409 * vm_object_reference_locked:
411 * Gets another reference to the given object.
413 * The object must be locked.
416 vm_object_reference_locked(vm_object_t object)
420 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
422 if (object->type == OBJT_VNODE) {
429 * Handle deallocating an object of type OBJT_VNODE.
432 vm_object_vndeallocate(vm_object_t object)
434 struct vnode *vp = (struct vnode *) object->handle;
436 VFS_ASSERT_GIANT(vp->v_mount);
437 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
438 KASSERT(object->type == OBJT_VNODE,
439 ("vm_object_vndeallocate: not a vnode object"));
440 KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
442 if (object->ref_count == 0) {
443 vprint("vm_object_vndeallocate", vp);
444 panic("vm_object_vndeallocate: bad object reference count");
449 if (object->ref_count == 0) {
450 mp_fixme("Unlocked vflag access.");
451 vp->v_vflag &= ~VV_TEXT;
453 VM_OBJECT_UNLOCK(object);
455 * vrele may need a vop lock
461 * vm_object_deallocate:
463 * Release a reference to the specified object,
464 * gained either through a vm_object_allocate
465 * or a vm_object_reference call. When all references
466 * are gone, storage associated with this object
467 * may be relinquished.
469 * No object may be locked.
472 vm_object_deallocate(vm_object_t object)
476 while (object != NULL) {
481 VM_OBJECT_LOCK(object);
482 if (object->type == OBJT_VNODE) {
483 struct vnode *vp = (struct vnode *) object->handle;
486 * Conditionally acquire Giant for a vnode-backed
487 * object. We have to be careful since the type of
488 * a vnode object can change while the object is
491 if (VFS_NEEDSGIANT(vp->v_mount) && !vfslocked) {
493 if (!mtx_trylock(&Giant)) {
494 VM_OBJECT_UNLOCK(object);
499 vm_object_vndeallocate(object);
500 VFS_UNLOCK_GIANT(vfslocked);
504 * This is to handle the case that the object
505 * changed type while we dropped its lock to
508 VFS_UNLOCK_GIANT(vfslocked);
510 KASSERT(object->ref_count != 0,
511 ("vm_object_deallocate: object deallocated too many times: %d", object->type));
514 * If the reference count goes to 0 we start calling
515 * vm_object_terminate() on the object chain.
516 * A ref count of 1 may be a special case depending on the
517 * shadow count being 0 or 1.
520 if (object->ref_count > 1) {
521 VM_OBJECT_UNLOCK(object);
523 } else if (object->ref_count == 1) {
524 if (object->shadow_count == 0 &&
525 object->handle == NULL &&
526 (object->type == OBJT_DEFAULT ||
527 object->type == OBJT_SWAP)) {
528 vm_object_set_flag(object, OBJ_ONEMAPPING);
529 } else if ((object->shadow_count == 1) &&
530 (object->handle == NULL) &&
531 (object->type == OBJT_DEFAULT ||
532 object->type == OBJT_SWAP)) {
535 robject = LIST_FIRST(&object->shadow_head);
536 KASSERT(robject != NULL,
537 ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
539 object->shadow_count));
540 if (!VM_OBJECT_TRYLOCK(robject)) {
542 * Avoid a potential deadlock.
545 VM_OBJECT_UNLOCK(object);
547 * More likely than not the thread
548 * holding robject's lock has lower
549 * priority than the current thread.
550 * Let the lower priority thread run.
556 * Collapse object into its shadow unless its
557 * shadow is dead. In that case, object will
558 * be deallocated by the thread that is
559 * deallocating its shadow.
561 if ((robject->flags & OBJ_DEAD) == 0 &&
562 (robject->handle == NULL) &&
563 (robject->type == OBJT_DEFAULT ||
564 robject->type == OBJT_SWAP)) {
566 robject->ref_count++;
568 if (robject->paging_in_progress) {
569 VM_OBJECT_UNLOCK(object);
570 vm_object_pip_wait(robject,
572 temp = robject->backing_object;
573 if (object == temp) {
574 VM_OBJECT_LOCK(object);
577 } else if (object->paging_in_progress) {
578 VM_OBJECT_UNLOCK(robject);
579 object->flags |= OBJ_PIPWNT;
581 VM_OBJECT_MTX(object),
582 PDROP | PVM, "objde2", 0);
583 VM_OBJECT_LOCK(robject);
584 temp = robject->backing_object;
585 if (object == temp) {
586 VM_OBJECT_LOCK(object);
590 VM_OBJECT_UNLOCK(object);
592 if (robject->ref_count == 1) {
593 robject->ref_count--;
598 vm_object_collapse(object);
599 VM_OBJECT_UNLOCK(object);
602 VM_OBJECT_UNLOCK(robject);
604 VM_OBJECT_UNLOCK(object);
608 temp = object->backing_object;
610 VM_OBJECT_LOCK(temp);
611 LIST_REMOVE(object, shadow_list);
612 temp->shadow_count--;
614 VM_OBJECT_UNLOCK(temp);
615 object->backing_object = NULL;
618 * Don't double-terminate, we could be in a termination
619 * recursion due to the terminate having to sync data
622 if ((object->flags & OBJ_DEAD) == 0)
623 vm_object_terminate(object);
625 VM_OBJECT_UNLOCK(object);
631 * vm_object_destroy removes the object from the global object list
632 * and frees the space for the object.
635 vm_object_destroy(vm_object_t object)
639 * Remove the object from the global object list.
641 mtx_lock(&vm_object_list_mtx);
642 TAILQ_REMOVE(&vm_object_list, object, object_list);
643 mtx_unlock(&vm_object_list_mtx);
646 * Release the allocation charge.
648 if (object->uip != NULL) {
649 KASSERT(object->type == OBJT_DEFAULT ||
650 object->type == OBJT_SWAP,
651 ("vm_object_terminate: non-swap obj %p has uip",
653 swap_release_by_uid(object->charge, object->uip);
660 * Free the space for the object.
662 uma_zfree(obj_zone, object);
666 * vm_object_terminate actually destroys the specified object, freeing
667 * up all previously used resources.
669 * The object must be locked.
670 * This routine may block.
673 vm_object_terminate(vm_object_t object)
677 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
680 * Make sure no one uses us.
682 vm_object_set_flag(object, OBJ_DEAD);
685 * wait for the pageout daemon to be done with the object
687 vm_object_pip_wait(object, "objtrm");
689 KASSERT(!object->paging_in_progress,
690 ("vm_object_terminate: pageout in progress"));
693 * Clean and free the pages, as appropriate. All references to the
694 * object are gone, so we don't need to lock it.
696 if (object->type == OBJT_VNODE) {
697 struct vnode *vp = (struct vnode *)object->handle;
700 * Clean pages and flush buffers.
702 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
703 VM_OBJECT_UNLOCK(object);
705 vinvalbuf(vp, V_SAVE, 0, 0);
707 VM_OBJECT_LOCK(object);
710 KASSERT(object->ref_count == 0,
711 ("vm_object_terminate: object with references, ref_count=%d",
715 * Now free any remaining pages. For internal objects, this also
716 * removes them from paging queues. Don't free wired pages, just
717 * remove them from the object.
719 vm_page_lock_queues();
720 while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
721 KASSERT(!p->busy && (p->oflags & VPO_BUSY) == 0,
722 ("vm_object_terminate: freeing busy page %p "
723 "p->busy = %d, p->oflags %x\n", p, p->busy, p->oflags));
724 if (p->wire_count == 0) {
731 vm_page_unlock_queues();
733 #if VM_NRESERVLEVEL > 0
734 if (__predict_false(!LIST_EMPTY(&object->rvq)))
735 vm_reserv_break_all(object);
737 if (__predict_false(object->cache != NULL))
738 vm_page_cache_free(object, 0, 0);
741 * Let the pager know object is dead.
743 vm_pager_deallocate(object);
744 VM_OBJECT_UNLOCK(object);
746 vm_object_destroy(object);
750 * vm_object_page_clean
752 * Clean all dirty pages in the specified range of object. Leaves page
753 * on whatever queue it is currently on. If NOSYNC is set then do not
754 * write out pages with VPO_NOSYNC set (originally comes from MAP_NOSYNC),
755 * leaving the object dirty.
757 * When stuffing pages asynchronously, allow clustering. XXX we need a
758 * synchronous clustering mode implementation.
760 * Odd semantics: if start == end, we clean everything.
762 * The object must be locked.
765 vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end, int flags)
768 vm_pindex_t tstart, tend;
774 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
775 if (object->type != OBJT_VNODE ||
776 (object->flags & OBJ_MIGHTBEDIRTY) == 0)
779 pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
780 pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
782 vm_object_set_flag(object, OBJ_CLEANING);
791 vm_page_lock_queues();
793 * If the caller is smart and only msync()s a range he knows is
794 * dirty, we may be able to avoid an object scan. This results in
795 * a phenominal improvement in performance. We cannot do this
796 * as a matter of course because the object may be huge - e.g.
797 * the size might be in the gigabytes or terrabytes.
799 if (msync_flush_flags & MSYNC_FLUSH_HARDSEQ) {
804 scanreset = object->resident_page_count / EASY_SCAN_FACTOR;
807 pagerflags |= VM_PAGER_IGNORE_CLEANCHK;
809 scanlimit = scanreset;
811 while (tscan < tend) {
812 curgeneration = object->generation;
813 p = vm_page_lookup(object, tscan);
814 if (p == NULL || p->valid == 0) {
815 if (--scanlimit == 0)
820 vm_page_test_dirty(p);
822 if (--scanlimit == 0)
828 * If we have been asked to skip nosync pages and
829 * this is a nosync page, we can't continue.
831 if ((flags & OBJPC_NOSYNC) && (p->oflags & VPO_NOSYNC)) {
832 if (--scanlimit == 0)
837 scanlimit = scanreset;
840 * This returns 0 if it was unable to busy the first
841 * page (i.e. had to sleep).
843 tscan += vm_object_page_collect_flush(object, p, curgeneration, pagerflags);
847 * If everything was dirty and we flushed it successfully,
848 * and the requested range is not the entire object, we
849 * don't have to mess with CLEANCHK or MIGHTBEDIRTY and can
850 * return immediately.
852 if (tscan >= tend && (tstart || tend < object->size)) {
853 vm_page_unlock_queues();
854 vm_object_clear_flag(object, OBJ_CLEANING);
857 pagerflags &= ~VM_PAGER_IGNORE_CLEANCHK;
861 * Generally set CLEANCHK interlock and make the page read-only so
862 * we can then clear the object flags.
864 * However, if this is a nosync mmap then the object is likely to
865 * stay dirty so do not mess with the page and do not clear the
869 TAILQ_FOREACH(p, &object->memq, listq) {
870 p->oflags |= VPO_CLEANCHK;
871 if ((flags & OBJPC_NOSYNC) && (p->oflags & VPO_NOSYNC))
874 pmap_remove_write(p);
877 if (clearobjflags && (tstart == 0) && (tend == object->size)) {
880 vm_object_clear_flag(object, OBJ_MIGHTBEDIRTY);
881 if (object->type == OBJT_VNODE &&
882 (vp = (struct vnode *)object->handle) != NULL) {
884 if (vp->v_iflag & VI_OBJDIRTY)
885 vp->v_iflag &= ~VI_OBJDIRTY;
891 curgeneration = object->generation;
893 for (p = TAILQ_FIRST(&object->memq); p; p = np) {
896 np = TAILQ_NEXT(p, listq);
900 if ((p->oflags & VPO_CLEANCHK) == 0 ||
901 (pi < tstart) || (pi >= tend) ||
903 p->oflags &= ~VPO_CLEANCHK;
907 vm_page_test_dirty(p);
909 p->oflags &= ~VPO_CLEANCHK;
914 * If we have been asked to skip nosync pages and this is a
915 * nosync page, skip it. Note that the object flags were
916 * not cleared in this case so we do not have to set them.
918 if ((flags & OBJPC_NOSYNC) && (p->oflags & VPO_NOSYNC)) {
919 p->oflags &= ~VPO_CLEANCHK;
923 n = vm_object_page_collect_flush(object, p,
924 curgeneration, pagerflags);
928 if (object->generation != curgeneration)
932 * Try to optimize the next page. If we can't we pick up
933 * our (random) scan where we left off.
935 if (msync_flush_flags & MSYNC_FLUSH_SOFTSEQ) {
936 if ((p = vm_page_lookup(object, pi + n)) != NULL)
940 vm_page_unlock_queues();
942 VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
945 vm_object_clear_flag(object, OBJ_CLEANING);
950 vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags)
958 vm_page_t maf[vm_pageout_page_count];
959 vm_page_t mab[vm_pageout_page_count];
960 vm_page_t ma[vm_pageout_page_count];
962 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
964 while (vm_page_sleep_if_busy(p, TRUE, "vpcwai")) {
965 vm_page_lock_queues();
966 if (object->generation != curgeneration) {
971 for(i = 1; i < vm_pageout_page_count; i++) {
974 if ((tp = vm_page_lookup(object, pi + i)) != NULL) {
975 if ((tp->oflags & VPO_BUSY) ||
976 ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
977 (tp->oflags & VPO_CLEANCHK) == 0) ||
980 vm_page_test_dirty(tp);
981 if (tp->dirty == 0) {
982 tp->oflags &= ~VPO_CLEANCHK;
993 chkb = vm_pageout_page_count - maxf;
995 for(i = 1; i < chkb;i++) {
998 if ((tp = vm_page_lookup(object, pi - i)) != NULL) {
999 if ((tp->oflags & VPO_BUSY) ||
1000 ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
1001 (tp->oflags & VPO_CLEANCHK) == 0) ||
1004 vm_page_test_dirty(tp);
1005 if (tp->dirty == 0) {
1006 tp->oflags &= ~VPO_CLEANCHK;
1017 for(i = 0; i < maxb; i++) {
1018 int index = (maxb - i) - 1;
1020 ma[index]->oflags &= ~VPO_CLEANCHK;
1022 p->oflags &= ~VPO_CLEANCHK;
1024 for(i = 0; i < maxf; i++) {
1025 int index = (maxb + i) + 1;
1027 ma[index]->oflags &= ~VPO_CLEANCHK;
1029 runlen = maxb + maxf + 1;
1031 vm_pageout_flush(ma, runlen, pagerflags);
1032 for (i = 0; i < runlen; i++) {
1034 pmap_remove_write(ma[i]);
1035 ma[i]->oflags |= VPO_CLEANCHK;
1038 * maxf will end up being the actual number of pages
1039 * we wrote out contiguously, non-inclusive of the
1040 * first page. We do not count look-behind pages.
1042 if (i >= maxb + 1 && (maxf > i - maxb - 1))
1043 maxf = i - maxb - 1;
1050 * Note that there is absolutely no sense in writing out
1051 * anonymous objects, so we track down the vnode object
1053 * We invalidate (remove) all pages from the address space
1054 * for semantic correctness.
1056 * Note: certain anonymous maps, such as MAP_NOSYNC maps,
1057 * may start out with a NULL object.
1060 vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size,
1061 boolean_t syncio, boolean_t invalidate)
1063 vm_object_t backing_object;
1070 VM_OBJECT_LOCK(object);
1071 while ((backing_object = object->backing_object) != NULL) {
1072 VM_OBJECT_LOCK(backing_object);
1073 offset += object->backing_object_offset;
1074 VM_OBJECT_UNLOCK(object);
1075 object = backing_object;
1076 if (object->size < OFF_TO_IDX(offset + size))
1077 size = IDX_TO_OFF(object->size) - offset;
1080 * Flush pages if writing is allowed, invalidate them
1081 * if invalidation requested. Pages undergoing I/O
1082 * will be ignored by vm_object_page_remove().
1084 * We cannot lock the vnode and then wait for paging
1085 * to complete without deadlocking against vm_fault.
1086 * Instead we simply call vm_object_page_remove() and
1087 * allow it to block internally on a page-by-page
1088 * basis when it encounters pages undergoing async
1091 if (object->type == OBJT_VNODE &&
1092 (object->flags & OBJ_MIGHTBEDIRTY) != 0) {
1094 vp = object->handle;
1095 VM_OBJECT_UNLOCK(object);
1096 (void) vn_start_write(vp, &mp, V_WAIT);
1097 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1098 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1099 flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
1100 flags |= invalidate ? OBJPC_INVAL : 0;
1101 VM_OBJECT_LOCK(object);
1102 vm_object_page_clean(object,
1104 OFF_TO_IDX(offset + size + PAGE_MASK),
1106 VM_OBJECT_UNLOCK(object);
1108 VFS_UNLOCK_GIANT(vfslocked);
1109 vn_finished_write(mp);
1110 VM_OBJECT_LOCK(object);
1112 if ((object->type == OBJT_VNODE ||
1113 object->type == OBJT_DEVICE) && invalidate) {
1115 purge = old_msync || (object->type == OBJT_DEVICE);
1116 vm_object_page_remove(object,
1118 OFF_TO_IDX(offset + size + PAGE_MASK),
1119 purge ? FALSE : TRUE);
1121 VM_OBJECT_UNLOCK(object);
1125 * vm_object_madvise:
1127 * Implements the madvise function at the object/page level.
1129 * MADV_WILLNEED (any object)
1131 * Activate the specified pages if they are resident.
1133 * MADV_DONTNEED (any object)
1135 * Deactivate the specified pages if they are resident.
1137 * MADV_FREE (OBJT_DEFAULT/OBJT_SWAP objects,
1138 * OBJ_ONEMAPPING only)
1140 * Deactivate and clean the specified pages if they are
1141 * resident. This permits the process to reuse the pages
1142 * without faulting or the kernel to reclaim the pages
1146 vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
1148 vm_pindex_t end, tpindex;
1149 vm_object_t backing_object, tobject;
1154 VM_OBJECT_LOCK(object);
1155 end = pindex + count;
1157 * Locate and adjust resident pages
1159 for (; pindex < end; pindex += 1) {
1165 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
1166 * and those pages must be OBJ_ONEMAPPING.
1168 if (advise == MADV_FREE) {
1169 if ((tobject->type != OBJT_DEFAULT &&
1170 tobject->type != OBJT_SWAP) ||
1171 (tobject->flags & OBJ_ONEMAPPING) == 0) {
1172 goto unlock_tobject;
1175 m = vm_page_lookup(tobject, tpindex);
1176 if (m == NULL && advise == MADV_WILLNEED) {
1178 * If the page is cached, reactivate it.
1180 m = vm_page_alloc(tobject, tpindex, VM_ALLOC_IFCACHED |
1185 * There may be swap even if there is no backing page
1187 if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1188 swap_pager_freespace(tobject, tpindex, 1);
1192 backing_object = tobject->backing_object;
1193 if (backing_object == NULL)
1194 goto unlock_tobject;
1195 VM_OBJECT_LOCK(backing_object);
1196 tpindex += OFF_TO_IDX(tobject->backing_object_offset);
1197 if (tobject != object)
1198 VM_OBJECT_UNLOCK(tobject);
1199 tobject = backing_object;
1203 * If the page is busy or not in a normal active state,
1204 * we skip it. If the page is not managed there are no
1205 * page queues to mess with. Things can break if we mess
1206 * with pages in any of the below states.
1208 vm_page_lock_queues();
1209 if (m->hold_count ||
1211 (m->flags & PG_UNMANAGED) ||
1212 m->valid != VM_PAGE_BITS_ALL) {
1213 vm_page_unlock_queues();
1214 goto unlock_tobject;
1216 if ((m->oflags & VPO_BUSY) || m->busy) {
1217 vm_page_flag_set(m, PG_REFERENCED);
1218 vm_page_unlock_queues();
1219 if (object != tobject)
1220 VM_OBJECT_UNLOCK(object);
1221 m->oflags |= VPO_WANTED;
1222 msleep(m, VM_OBJECT_MTX(tobject), PDROP | PVM, "madvpo", 0);
1223 VM_OBJECT_LOCK(object);
1226 if (advise == MADV_WILLNEED) {
1227 vm_page_activate(m);
1228 } else if (advise == MADV_DONTNEED) {
1229 vm_page_dontneed(m);
1230 } else if (advise == MADV_FREE) {
1232 * Mark the page clean. This will allow the page
1233 * to be freed up by the system. However, such pages
1234 * are often reused quickly by malloc()/free()
1235 * so we do not do anything that would cause
1236 * a page fault if we can help it.
1238 * Specifically, we do not try to actually free
1239 * the page now nor do we try to put it in the
1240 * cache (which would cause a page fault on reuse).
1242 * But we do make the page is freeable as we
1243 * can without actually taking the step of unmapping
1246 pmap_clear_modify(m);
1249 vm_page_dontneed(m);
1251 vm_page_unlock_queues();
1252 if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1253 swap_pager_freespace(tobject, tpindex, 1);
1255 if (tobject != object)
1256 VM_OBJECT_UNLOCK(tobject);
1258 VM_OBJECT_UNLOCK(object);
1264 * Create a new object which is backed by the
1265 * specified existing object range. The source
1266 * object reference is deallocated.
1268 * The new object and offset into that object
1269 * are returned in the source parameters.
1273 vm_object_t *object, /* IN/OUT */
1274 vm_ooffset_t *offset, /* IN/OUT */
1283 * Don't create the new object if the old object isn't shared.
1285 if (source != NULL) {
1286 VM_OBJECT_LOCK(source);
1287 if (source->ref_count == 1 &&
1288 source->handle == NULL &&
1289 (source->type == OBJT_DEFAULT ||
1290 source->type == OBJT_SWAP)) {
1291 VM_OBJECT_UNLOCK(source);
1294 VM_OBJECT_UNLOCK(source);
1298 * Allocate a new object with the given length.
1300 result = vm_object_allocate(OBJT_DEFAULT, length);
1303 * The new object shadows the source object, adding a reference to it.
1304 * Our caller changes his reference to point to the new object,
1305 * removing a reference to the source object. Net result: no change
1306 * of reference count.
1308 * Try to optimize the result object's page color when shadowing
1309 * in order to maintain page coloring consistency in the combined
1312 result->backing_object = source;
1314 * Store the offset into the source object, and fix up the offset into
1317 result->backing_object_offset = *offset;
1318 if (source != NULL) {
1319 VM_OBJECT_LOCK(source);
1320 LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1321 source->shadow_count++;
1322 source->generation++;
1323 #if VM_NRESERVLEVEL > 0
1324 result->flags |= source->flags & OBJ_COLORED;
1325 result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) &
1326 ((1 << (VM_NFREEORDER - 1)) - 1);
1328 VM_OBJECT_UNLOCK(source);
1333 * Return the new things
1342 * Split the pages in a map entry into a new object. This affords
1343 * easier removal of unused pages, and keeps object inheritance from
1344 * being a negative impact on memory usage.
1347 vm_object_split(vm_map_entry_t entry)
1349 vm_page_t m, m_next;
1350 vm_object_t orig_object, new_object, source;
1351 vm_pindex_t idx, offidxstart;
1354 orig_object = entry->object.vm_object;
1355 if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1357 if (orig_object->ref_count <= 1)
1359 VM_OBJECT_UNLOCK(orig_object);
1361 offidxstart = OFF_TO_IDX(entry->offset);
1362 size = atop(entry->end - entry->start);
1365 * If swap_pager_copy() is later called, it will convert new_object
1366 * into a swap object.
1368 new_object = vm_object_allocate(OBJT_DEFAULT, size);
1371 * At this point, the new object is still private, so the order in
1372 * which the original and new objects are locked does not matter.
1374 VM_OBJECT_LOCK(new_object);
1375 VM_OBJECT_LOCK(orig_object);
1376 source = orig_object->backing_object;
1377 if (source != NULL) {
1378 VM_OBJECT_LOCK(source);
1379 if ((source->flags & OBJ_DEAD) != 0) {
1380 VM_OBJECT_UNLOCK(source);
1381 VM_OBJECT_UNLOCK(orig_object);
1382 VM_OBJECT_UNLOCK(new_object);
1383 vm_object_deallocate(new_object);
1384 VM_OBJECT_LOCK(orig_object);
1387 LIST_INSERT_HEAD(&source->shadow_head,
1388 new_object, shadow_list);
1389 source->shadow_count++;
1390 source->generation++;
1391 vm_object_reference_locked(source); /* for new_object */
1392 vm_object_clear_flag(source, OBJ_ONEMAPPING);
1393 VM_OBJECT_UNLOCK(source);
1394 new_object->backing_object_offset =
1395 orig_object->backing_object_offset + entry->offset;
1396 new_object->backing_object = source;
1398 if (orig_object->uip != NULL) {
1399 new_object->uip = orig_object->uip;
1400 uihold(orig_object->uip);
1401 new_object->charge = ptoa(size);
1402 KASSERT(orig_object->charge >= ptoa(size),
1403 ("orig_object->charge < 0"));
1404 orig_object->charge -= ptoa(size);
1407 if ((m = TAILQ_FIRST(&orig_object->memq)) != NULL) {
1408 if (m->pindex < offidxstart) {
1409 m = vm_page_splay(offidxstart, orig_object->root);
1410 if ((orig_object->root = m)->pindex < offidxstart)
1411 m = TAILQ_NEXT(m, listq);
1414 vm_page_lock_queues();
1415 for (; m != NULL && (idx = m->pindex - offidxstart) < size;
1417 m_next = TAILQ_NEXT(m, listq);
1420 * We must wait for pending I/O to complete before we can
1423 * We do not have to VM_PROT_NONE the page as mappings should
1424 * not be changed by this operation.
1426 if ((m->oflags & VPO_BUSY) || m->busy) {
1427 vm_page_flag_set(m, PG_REFERENCED);
1428 vm_page_unlock_queues();
1429 VM_OBJECT_UNLOCK(new_object);
1430 m->oflags |= VPO_WANTED;
1431 msleep(m, VM_OBJECT_MTX(orig_object), PVM, "spltwt", 0);
1432 VM_OBJECT_LOCK(new_object);
1435 vm_page_rename(m, new_object, idx);
1436 /* page automatically made dirty by rename and cache handled */
1439 vm_page_unlock_queues();
1440 if (orig_object->type == OBJT_SWAP) {
1442 * swap_pager_copy() can sleep, in which case the orig_object's
1443 * and new_object's locks are released and reacquired.
1445 swap_pager_copy(orig_object, new_object, offidxstart, 0);
1448 * Transfer any cached pages from orig_object to new_object.
1450 if (__predict_false(orig_object->cache != NULL))
1451 vm_page_cache_transfer(orig_object, offidxstart,
1454 VM_OBJECT_UNLOCK(orig_object);
1455 TAILQ_FOREACH(m, &new_object->memq, listq)
1457 VM_OBJECT_UNLOCK(new_object);
1458 entry->object.vm_object = new_object;
1459 entry->offset = 0LL;
1460 vm_object_deallocate(orig_object);
1461 VM_OBJECT_LOCK(new_object);
1464 #define OBSC_TEST_ALL_SHADOWED 0x0001
1465 #define OBSC_COLLAPSE_NOWAIT 0x0002
1466 #define OBSC_COLLAPSE_WAIT 0x0004
1469 vm_object_backing_scan(vm_object_t object, int op)
1473 vm_object_t backing_object;
1474 vm_pindex_t backing_offset_index;
1476 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1477 VM_OBJECT_LOCK_ASSERT(object->backing_object, MA_OWNED);
1479 backing_object = object->backing_object;
1480 backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
1483 * Initial conditions
1485 if (op & OBSC_TEST_ALL_SHADOWED) {
1487 * We do not want to have to test for the existence of cache
1488 * or swap pages in the backing object. XXX but with the
1489 * new swapper this would be pretty easy to do.
1491 * XXX what about anonymous MAP_SHARED memory that hasn't
1492 * been ZFOD faulted yet? If we do not test for this, the
1493 * shadow test may succeed! XXX
1495 if (backing_object->type != OBJT_DEFAULT) {
1499 if (op & OBSC_COLLAPSE_WAIT) {
1500 vm_object_set_flag(backing_object, OBJ_DEAD);
1506 p = TAILQ_FIRST(&backing_object->memq);
1508 vm_page_t next = TAILQ_NEXT(p, listq);
1509 vm_pindex_t new_pindex = p->pindex - backing_offset_index;
1511 if (op & OBSC_TEST_ALL_SHADOWED) {
1515 * Ignore pages outside the parent object's range
1516 * and outside the parent object's mapping of the
1519 * note that we do not busy the backing object's
1523 p->pindex < backing_offset_index ||
1524 new_pindex >= object->size
1531 * See if the parent has the page or if the parent's
1532 * object pager has the page. If the parent has the
1533 * page but the page is not valid, the parent's
1534 * object pager must have the page.
1536 * If this fails, the parent does not completely shadow
1537 * the object and we might as well give up now.
1540 pp = vm_page_lookup(object, new_pindex);
1542 (pp == NULL || pp->valid == 0) &&
1543 !vm_pager_has_page(object, new_pindex, NULL, NULL)
1551 * Check for busy page
1553 if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
1556 if (op & OBSC_COLLAPSE_NOWAIT) {
1557 if ((p->oflags & VPO_BUSY) ||
1563 } else if (op & OBSC_COLLAPSE_WAIT) {
1564 if ((p->oflags & VPO_BUSY) || p->busy) {
1565 vm_page_lock_queues();
1566 vm_page_flag_set(p, PG_REFERENCED);
1567 vm_page_unlock_queues();
1568 VM_OBJECT_UNLOCK(object);
1569 p->oflags |= VPO_WANTED;
1570 msleep(p, VM_OBJECT_MTX(backing_object),
1571 PDROP | PVM, "vmocol", 0);
1572 VM_OBJECT_LOCK(object);
1573 VM_OBJECT_LOCK(backing_object);
1575 * If we slept, anything could have
1576 * happened. Since the object is
1577 * marked dead, the backing offset
1578 * should not have changed so we
1579 * just restart our scan.
1581 p = TAILQ_FIRST(&backing_object->memq);
1587 p->object == backing_object,
1588 ("vm_object_backing_scan: object mismatch")
1592 * Destroy any associated swap
1594 if (backing_object->type == OBJT_SWAP) {
1595 swap_pager_freespace(
1603 p->pindex < backing_offset_index ||
1604 new_pindex >= object->size
1607 * Page is out of the parent object's range, we
1608 * can simply destroy it.
1610 vm_page_lock_queues();
1611 KASSERT(!pmap_page_is_mapped(p),
1612 ("freeing mapped page %p", p));
1613 if (p->wire_count == 0)
1617 vm_page_unlock_queues();
1622 pp = vm_page_lookup(object, new_pindex);
1625 vm_pager_has_page(object, new_pindex, NULL, NULL)
1628 * page already exists in parent OR swap exists
1629 * for this location in the parent. Destroy
1630 * the original page from the backing object.
1632 * Leave the parent's page alone
1634 vm_page_lock_queues();
1635 KASSERT(!pmap_page_is_mapped(p),
1636 ("freeing mapped page %p", p));
1637 if (p->wire_count == 0)
1641 vm_page_unlock_queues();
1646 #if VM_NRESERVLEVEL > 0
1648 * Rename the reservation.
1650 vm_reserv_rename(p, object, backing_object,
1651 backing_offset_index);
1655 * Page does not exist in parent, rename the
1656 * page from the backing object to the main object.
1658 * If the page was mapped to a process, it can remain
1659 * mapped through the rename.
1661 vm_page_lock_queues();
1662 vm_page_rename(p, object, new_pindex);
1663 vm_page_unlock_queues();
1664 /* page automatically made dirty by rename */
1673 * this version of collapse allows the operation to occur earlier and
1674 * when paging_in_progress is true for an object... This is not a complete
1675 * operation, but should plug 99.9% of the rest of the leaks.
1678 vm_object_qcollapse(vm_object_t object)
1680 vm_object_t backing_object = object->backing_object;
1682 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1683 VM_OBJECT_LOCK_ASSERT(backing_object, MA_OWNED);
1685 if (backing_object->ref_count != 1)
1688 vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
1692 * vm_object_collapse:
1694 * Collapse an object with the object backing it.
1695 * Pages in the backing object are moved into the
1696 * parent, and the backing object is deallocated.
1699 vm_object_collapse(vm_object_t object)
1701 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1704 vm_object_t backing_object;
1707 * Verify that the conditions are right for collapse:
1709 * The object exists and the backing object exists.
1711 if ((backing_object = object->backing_object) == NULL)
1715 * we check the backing object first, because it is most likely
1718 VM_OBJECT_LOCK(backing_object);
1719 if (backing_object->handle != NULL ||
1720 (backing_object->type != OBJT_DEFAULT &&
1721 backing_object->type != OBJT_SWAP) ||
1722 (backing_object->flags & OBJ_DEAD) ||
1723 object->handle != NULL ||
1724 (object->type != OBJT_DEFAULT &&
1725 object->type != OBJT_SWAP) ||
1726 (object->flags & OBJ_DEAD)) {
1727 VM_OBJECT_UNLOCK(backing_object);
1732 object->paging_in_progress != 0 ||
1733 backing_object->paging_in_progress != 0
1735 vm_object_qcollapse(object);
1736 VM_OBJECT_UNLOCK(backing_object);
1740 * We know that we can either collapse the backing object (if
1741 * the parent is the only reference to it) or (perhaps) have
1742 * the parent bypass the object if the parent happens to shadow
1743 * all the resident pages in the entire backing object.
1745 * This is ignoring pager-backed pages such as swap pages.
1746 * vm_object_backing_scan fails the shadowing test in this
1749 if (backing_object->ref_count == 1) {
1751 * If there is exactly one reference to the backing
1752 * object, we can collapse it into the parent.
1754 vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1756 #if VM_NRESERVLEVEL > 0
1758 * Break any reservations from backing_object.
1760 if (__predict_false(!LIST_EMPTY(&backing_object->rvq)))
1761 vm_reserv_break_all(backing_object);
1765 * Move the pager from backing_object to object.
1767 if (backing_object->type == OBJT_SWAP) {
1769 * swap_pager_copy() can sleep, in which case
1770 * the backing_object's and object's locks are
1771 * released and reacquired.
1776 OFF_TO_IDX(object->backing_object_offset), TRUE);
1779 * Free any cached pages from backing_object.
1781 if (__predict_false(backing_object->cache != NULL))
1782 vm_page_cache_free(backing_object, 0, 0);
1785 * Object now shadows whatever backing_object did.
1786 * Note that the reference to
1787 * backing_object->backing_object moves from within
1788 * backing_object to within object.
1790 LIST_REMOVE(object, shadow_list);
1791 backing_object->shadow_count--;
1792 backing_object->generation++;
1793 if (backing_object->backing_object) {
1794 VM_OBJECT_LOCK(backing_object->backing_object);
1795 LIST_REMOVE(backing_object, shadow_list);
1797 &backing_object->backing_object->shadow_head,
1798 object, shadow_list);
1800 * The shadow_count has not changed.
1802 backing_object->backing_object->generation++;
1803 VM_OBJECT_UNLOCK(backing_object->backing_object);
1805 object->backing_object = backing_object->backing_object;
1806 object->backing_object_offset +=
1807 backing_object->backing_object_offset;
1810 * Discard backing_object.
1812 * Since the backing object has no pages, no pager left,
1813 * and no object references within it, all that is
1814 * necessary is to dispose of it.
1816 KASSERT(backing_object->ref_count == 1, (
1817 "backing_object %p was somehow re-referenced during collapse!",
1819 VM_OBJECT_UNLOCK(backing_object);
1820 vm_object_destroy(backing_object);
1824 vm_object_t new_backing_object;
1827 * If we do not entirely shadow the backing object,
1828 * there is nothing we can do so we give up.
1830 if (object->resident_page_count != object->size &&
1831 vm_object_backing_scan(object,
1832 OBSC_TEST_ALL_SHADOWED) == 0) {
1833 VM_OBJECT_UNLOCK(backing_object);
1838 * Make the parent shadow the next object in the
1839 * chain. Deallocating backing_object will not remove
1840 * it, since its reference count is at least 2.
1842 LIST_REMOVE(object, shadow_list);
1843 backing_object->shadow_count--;
1844 backing_object->generation++;
1846 new_backing_object = backing_object->backing_object;
1847 if ((object->backing_object = new_backing_object) != NULL) {
1848 VM_OBJECT_LOCK(new_backing_object);
1850 &new_backing_object->shadow_head,
1854 new_backing_object->shadow_count++;
1855 new_backing_object->generation++;
1856 vm_object_reference_locked(new_backing_object);
1857 VM_OBJECT_UNLOCK(new_backing_object);
1858 object->backing_object_offset +=
1859 backing_object->backing_object_offset;
1863 * Drop the reference count on backing_object. Since
1864 * its ref_count was at least 2, it will not vanish.
1866 backing_object->ref_count--;
1867 VM_OBJECT_UNLOCK(backing_object);
1872 * Try again with this object's new backing object.
1878 * vm_object_page_remove:
1880 * For the given object, either frees or invalidates each of the
1881 * specified pages. In general, a page is freed. However, if a
1882 * page is wired for any reason other than the existence of a
1883 * managed, wired mapping, then it may be invalidated but not
1884 * removed from the object. Pages are specified by the given
1885 * range ["start", "end") and Boolean "clean_only". As a
1886 * special case, if "end" is zero, then the range extends from
1887 * "start" to the end of the object. If "clean_only" is TRUE,
1888 * then only the non-dirty pages within the specified range are
1891 * In general, this operation should only be performed on objects
1892 * that contain managed pages. There are two exceptions. First,
1893 * it may be performed on the kernel and kmem objects. Second,
1894 * it may be used by msync(..., MS_INVALIDATE) to invalidate
1895 * device-backed pages.
1897 * The object must be locked.
1900 vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
1901 boolean_t clean_only)
1906 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1907 if (object->resident_page_count == 0)
1911 * Since physically-backed objects do not use managed pages, we can't
1912 * remove pages from the object (we must instead remove the page
1913 * references, and then destroy the object).
1915 KASSERT(object->type != OBJT_PHYS || object == kernel_object ||
1916 object == kmem_object,
1917 ("attempt to remove pages from a physical object"));
1919 vm_object_pip_add(object, 1);
1921 if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
1922 if (p->pindex < start) {
1923 p = vm_page_splay(start, object->root);
1924 if ((object->root = p)->pindex < start)
1925 p = TAILQ_NEXT(p, listq);
1928 vm_page_lock_queues();
1930 * Assert: the variable p is either (1) the page with the
1931 * least pindex greater than or equal to the parameter pindex
1935 p != NULL && (p->pindex < end || end == 0);
1937 next = TAILQ_NEXT(p, listq);
1940 * If the page is wired for any reason besides the
1941 * existence of managed, wired mappings, then it cannot
1942 * be freed. For example, fictitious pages, which
1943 * represent device memory, are inherently wired and
1944 * cannot be freed. They can, however, be invalidated
1945 * if "clean_only" is FALSE.
1947 if ((wirings = p->wire_count) != 0 &&
1948 (wirings = pmap_page_wired_mappings(p)) != p->wire_count) {
1949 /* Fictitious pages do not have managed mappings. */
1950 if ((p->flags & PG_FICTITIOUS) == 0)
1952 /* Account for removal of managed, wired mappings. */
1953 p->wire_count -= wirings;
1960 if (vm_page_sleep_if_busy(p, TRUE, "vmopar"))
1962 KASSERT((p->flags & PG_FICTITIOUS) == 0,
1963 ("vm_object_page_remove: page %p is fictitious", p));
1964 if (clean_only && p->valid) {
1965 pmap_remove_write(p);
1970 /* Account for removal of managed, wired mappings. */
1972 p->wire_count -= wirings;
1975 vm_page_unlock_queues();
1976 vm_object_pip_wakeup(object);
1978 if (__predict_false(object->cache != NULL))
1979 vm_page_cache_free(object, start, end);
1983 * Populate the specified range of the object with valid pages. Returns
1984 * TRUE if the range is successfully populated and FALSE otherwise.
1986 * Note: This function should be optimized to pass a larger array of
1987 * pages to vm_pager_get_pages() before it is applied to a non-
1988 * OBJT_DEVICE object.
1990 * The object must be locked.
1993 vm_object_populate(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1999 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2000 for (pindex = start; pindex < end; pindex++) {
2001 m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL |
2003 if (m->valid != VM_PAGE_BITS_ALL) {
2005 rv = vm_pager_get_pages(object, ma, 1, 0);
2006 m = vm_page_lookup(object, pindex);
2009 if (rv != VM_PAGER_OK) {
2010 vm_page_lock_queues();
2012 vm_page_unlock_queues();
2017 * Keep "m" busy because a subsequent iteration may unlock
2021 if (pindex > start) {
2022 m = vm_page_lookup(object, start);
2023 while (m != NULL && m->pindex < pindex) {
2025 m = TAILQ_NEXT(m, listq);
2028 return (pindex == end);
2032 * Routine: vm_object_coalesce
2033 * Function: Coalesces two objects backing up adjoining
2034 * regions of memory into a single object.
2036 * returns TRUE if objects were combined.
2038 * NOTE: Only works at the moment if the second object is NULL -
2039 * if it's not, which object do we lock first?
2042 * prev_object First object to coalesce
2043 * prev_offset Offset into prev_object
2044 * prev_size Size of reference to prev_object
2045 * next_size Size of reference to the second object
2046 * reserved Indicator that extension region has
2047 * swap accounted for
2050 * The object must *not* be locked.
2053 vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset,
2054 vm_size_t prev_size, vm_size_t next_size, boolean_t reserved)
2056 vm_pindex_t next_pindex;
2058 if (prev_object == NULL)
2060 VM_OBJECT_LOCK(prev_object);
2061 if (prev_object->type != OBJT_DEFAULT &&
2062 prev_object->type != OBJT_SWAP) {
2063 VM_OBJECT_UNLOCK(prev_object);
2068 * Try to collapse the object first
2070 vm_object_collapse(prev_object);
2073 * Can't coalesce if: . more than one reference . paged out . shadows
2074 * another object . has a copy elsewhere (any of which mean that the
2075 * pages not mapped to prev_entry may be in use anyway)
2077 if (prev_object->backing_object != NULL) {
2078 VM_OBJECT_UNLOCK(prev_object);
2082 prev_size >>= PAGE_SHIFT;
2083 next_size >>= PAGE_SHIFT;
2084 next_pindex = OFF_TO_IDX(prev_offset) + prev_size;
2086 if ((prev_object->ref_count > 1) &&
2087 (prev_object->size != next_pindex)) {
2088 VM_OBJECT_UNLOCK(prev_object);
2093 * Account for the charge.
2095 if (prev_object->uip != NULL) {
2098 * If prev_object was charged, then this mapping,
2099 * althought not charged now, may become writable
2100 * later. Non-NULL uip in the object would prevent
2101 * swap reservation during enabling of the write
2102 * access, so reserve swap now. Failed reservation
2103 * cause allocation of the separate object for the map
2104 * entry, and swap reservation for this entry is
2105 * managed in appropriate time.
2107 if (!reserved && !swap_reserve_by_uid(ptoa(next_size),
2108 prev_object->uip)) {
2111 prev_object->charge += ptoa(next_size);
2115 * Remove any pages that may still be in the object from a previous
2118 if (next_pindex < prev_object->size) {
2119 vm_object_page_remove(prev_object,
2121 next_pindex + next_size, FALSE);
2122 if (prev_object->type == OBJT_SWAP)
2123 swap_pager_freespace(prev_object,
2124 next_pindex, next_size);
2126 if (prev_object->uip != NULL) {
2127 KASSERT(prev_object->charge >=
2128 ptoa(prev_object->size - next_pindex),
2129 ("object %p overcharged 1 %jx %jx", prev_object,
2130 (uintmax_t)next_pindex, (uintmax_t)next_size));
2131 prev_object->charge -= ptoa(prev_object->size -
2138 * Extend the object if necessary.
2140 if (next_pindex + next_size > prev_object->size)
2141 prev_object->size = next_pindex + next_size;
2143 VM_OBJECT_UNLOCK(prev_object);
2148 vm_object_set_writeable_dirty(vm_object_t object)
2152 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2153 if ((object->flags & OBJ_MIGHTBEDIRTY) != 0)
2155 vm_object_set_flag(object, OBJ_MIGHTBEDIRTY);
2156 if (object->type == OBJT_VNODE &&
2157 (vp = (struct vnode *)object->handle) != NULL) {
2159 vp->v_iflag |= VI_OBJDIRTY;
2164 #include "opt_ddb.h"
2166 #include <sys/kernel.h>
2168 #include <sys/cons.h>
2170 #include <ddb/ddb.h>
2173 _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
2176 vm_map_entry_t tmpe;
2184 tmpe = map->header.next;
2185 entcount = map->nentries;
2186 while (entcount-- && (tmpe != &map->header)) {
2187 if (_vm_object_in_map(map, object, tmpe)) {
2192 } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
2193 tmpm = entry->object.sub_map;
2194 tmpe = tmpm->header.next;
2195 entcount = tmpm->nentries;
2196 while (entcount-- && tmpe != &tmpm->header) {
2197 if (_vm_object_in_map(tmpm, object, tmpe)) {
2202 } else if ((obj = entry->object.vm_object) != NULL) {
2203 for (; obj; obj = obj->backing_object)
2204 if (obj == object) {
2212 vm_object_in_map(vm_object_t object)
2216 /* sx_slock(&allproc_lock); */
2217 FOREACH_PROC_IN_SYSTEM(p) {
2218 if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
2220 if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
2221 /* sx_sunlock(&allproc_lock); */
2225 /* sx_sunlock(&allproc_lock); */
2226 if (_vm_object_in_map(kernel_map, object, 0))
2228 if (_vm_object_in_map(kmem_map, object, 0))
2230 if (_vm_object_in_map(pager_map, object, 0))
2232 if (_vm_object_in_map(buffer_map, object, 0))
2237 DB_SHOW_COMMAND(vmochk, vm_object_check)
2242 * make sure that internal objs are in a map somewhere
2243 * and none have zero ref counts.
2245 TAILQ_FOREACH(object, &vm_object_list, object_list) {
2246 if (object->handle == NULL &&
2247 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2248 if (object->ref_count == 0) {
2249 db_printf("vmochk: internal obj has zero ref count: %ld\n",
2250 (long)object->size);
2252 if (!vm_object_in_map(object)) {
2254 "vmochk: internal obj is not in a map: "
2255 "ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2256 object->ref_count, (u_long)object->size,
2257 (u_long)object->size,
2258 (void *)object->backing_object);
2265 * vm_object_print: [ debug ]
2267 DB_SHOW_COMMAND(object, vm_object_print_static)
2269 /* XXX convert args. */
2270 vm_object_t object = (vm_object_t)addr;
2271 boolean_t full = have_addr;
2275 /* XXX count is an (unused) arg. Avoid shadowing it. */
2276 #define count was_count
2284 "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x uip %d charge %jx\n",
2285 object, (int)object->type, (uintmax_t)object->size,
2286 object->resident_page_count, object->ref_count, object->flags,
2287 object->uip ? object->uip->ui_uid : -1, (uintmax_t)object->charge);
2288 db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n",
2289 object->shadow_count,
2290 object->backing_object ? object->backing_object->ref_count : 0,
2291 object->backing_object, (uintmax_t)object->backing_object_offset);
2298 TAILQ_FOREACH(p, &object->memq, listq) {
2300 db_iprintf("memory:=");
2301 else if (count == 6) {
2309 db_printf("(off=0x%jx,page=0x%jx)",
2310 (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p));
2320 /* XXX need this non-static entry for calling from vm_map_print. */
2323 /* db_expr_t */ long addr,
2324 boolean_t have_addr,
2325 /* db_expr_t */ long count,
2328 vm_object_print_static(addr, have_addr, count, modif);
2331 DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
2336 vm_page_t m, prev_m;
2340 TAILQ_FOREACH(object, &vm_object_list, object_list) {
2341 db_printf("new object: %p\n", (void *)object);
2352 TAILQ_FOREACH(m, &object->memq, listq) {
2353 if (m->pindex > 128)
2355 if ((prev_m = TAILQ_PREV(m, pglist, listq)) != NULL &&
2356 prev_m->pindex + 1 != m->pindex) {
2358 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2359 (long)fidx, rcount, (long)pa);
2371 (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
2376 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2377 (long)fidx, rcount, (long)pa);
2387 pa = VM_PAGE_TO_PHYS(m);
2391 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2392 (long)fidx, rcount, (long)pa);