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>
100 SYSCTL_INT(_vm, OID_AUTO, old_msync, CTLFLAG_RW, &old_msync, 0,
101 "Use old (insecure) msync behavior");
103 static int vm_object_page_collect_flush(vm_object_t object, vm_page_t p,
105 static void vm_object_qcollapse(vm_object_t object);
106 static void vm_object_vndeallocate(vm_object_t object);
109 * Virtual memory objects maintain the actual data
110 * associated with allocated virtual memory. A given
111 * page of memory exists within exactly one object.
113 * An object is only deallocated when all "references"
114 * are given up. Only one "reference" to a given
115 * region of an object should be writeable.
117 * Associated with each object is a list of all resident
118 * memory pages belonging to that object; this list is
119 * maintained by the "vm_page" module, and locked by the object's
122 * Each object also records a "pager" routine which is
123 * used to retrieve (and store) pages to the proper backing
124 * storage. In addition, objects may be backed by other
125 * objects from which they were virtual-copied.
127 * The only items within the object structure which are
128 * modified after time of creation are:
129 * reference count locked by object's lock
130 * pager routine locked by object's lock
134 struct object_q vm_object_list;
135 struct mtx vm_object_list_mtx; /* lock for object list and count */
137 struct vm_object kernel_object_store;
138 struct vm_object kmem_object_store;
140 SYSCTL_NODE(_vm_stats, OID_AUTO, object, CTLFLAG_RD, 0, "VM object stats");
142 static long object_collapses;
143 SYSCTL_LONG(_vm_stats_object, OID_AUTO, collapses, CTLFLAG_RD,
144 &object_collapses, 0, "VM object collapses");
146 static long object_bypasses;
147 SYSCTL_LONG(_vm_stats_object, OID_AUTO, bypasses, CTLFLAG_RD,
148 &object_bypasses, 0, "VM object bypasses");
150 static uma_zone_t obj_zone;
152 static int vm_object_zinit(void *mem, int size, int flags);
155 static void vm_object_zdtor(void *mem, int size, void *arg);
158 vm_object_zdtor(void *mem, int size, void *arg)
162 object = (vm_object_t)mem;
163 KASSERT(TAILQ_EMPTY(&object->memq),
164 ("object %p has resident pages",
166 #if VM_NRESERVLEVEL > 0
167 KASSERT(LIST_EMPTY(&object->rvq),
168 ("object %p has reservations",
171 KASSERT(object->cache == NULL,
172 ("object %p has cached pages",
174 KASSERT(object->paging_in_progress == 0,
175 ("object %p paging_in_progress = %d",
176 object, object->paging_in_progress));
177 KASSERT(object->resident_page_count == 0,
178 ("object %p resident_page_count = %d",
179 object, object->resident_page_count));
180 KASSERT(object->shadow_count == 0,
181 ("object %p shadow_count = %d",
182 object, object->shadow_count));
187 vm_object_zinit(void *mem, int size, int flags)
191 object = (vm_object_t)mem;
192 bzero(&object->mtx, sizeof(object->mtx));
193 VM_OBJECT_LOCK_INIT(object, "standard object");
195 /* These are true for any object that has been freed */
196 object->paging_in_progress = 0;
197 object->resident_page_count = 0;
198 object->shadow_count = 0;
203 _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
206 TAILQ_INIT(&object->memq);
207 LIST_INIT(&object->shadow_head);
212 object->generation = 1;
213 object->ref_count = 1;
214 object->memattr = VM_MEMATTR_DEFAULT;
218 if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
219 object->flags = OBJ_ONEMAPPING;
220 object->pg_color = 0;
221 object->handle = NULL;
222 object->backing_object = NULL;
223 object->backing_object_offset = (vm_ooffset_t) 0;
224 #if VM_NRESERVLEVEL > 0
225 LIST_INIT(&object->rvq);
227 object->cache = NULL;
229 mtx_lock(&vm_object_list_mtx);
230 TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
231 mtx_unlock(&vm_object_list_mtx);
237 * Initialize the VM objects module.
242 TAILQ_INIT(&vm_object_list);
243 mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF);
245 VM_OBJECT_LOCK_INIT(&kernel_object_store, "kernel object");
246 _vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
248 #if VM_NRESERVLEVEL > 0
249 kernel_object->flags |= OBJ_COLORED;
250 kernel_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS);
253 VM_OBJECT_LOCK_INIT(&kmem_object_store, "kmem object");
254 _vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
256 #if VM_NRESERVLEVEL > 0
257 kmem_object->flags |= OBJ_COLORED;
258 kmem_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS);
262 * The lock portion of struct vm_object must be type stable due
263 * to vm_pageout_fallback_object_lock locking a vm object
264 * without holding any references to it.
266 obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL,
272 vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM|UMA_ZONE_NOFREE);
276 vm_object_clear_flag(vm_object_t object, u_short bits)
279 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
280 object->flags &= ~bits;
284 * Sets the default memory attribute for the specified object. Pages
285 * that are allocated to this object are by default assigned this memory
288 * Presently, this function must be called before any pages are allocated
289 * to the object. In the future, this requirement may be relaxed for
290 * "default" and "swap" objects.
293 vm_object_set_memattr(vm_object_t object, vm_memattr_t memattr)
296 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
297 switch (object->type) {
304 if (!TAILQ_EMPTY(&object->memq))
305 return (KERN_FAILURE);
308 return (KERN_INVALID_ARGUMENT);
310 object->memattr = memattr;
311 return (KERN_SUCCESS);
315 vm_object_pip_add(vm_object_t object, short i)
318 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
319 object->paging_in_progress += i;
323 vm_object_pip_subtract(vm_object_t object, short i)
326 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
327 object->paging_in_progress -= i;
331 vm_object_pip_wakeup(vm_object_t object)
334 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
335 object->paging_in_progress--;
336 if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
337 vm_object_clear_flag(object, OBJ_PIPWNT);
343 vm_object_pip_wakeupn(vm_object_t object, short i)
346 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
348 object->paging_in_progress -= i;
349 if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
350 vm_object_clear_flag(object, OBJ_PIPWNT);
356 vm_object_pip_wait(vm_object_t object, char *waitid)
359 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
360 while (object->paging_in_progress) {
361 object->flags |= OBJ_PIPWNT;
362 msleep(object, VM_OBJECT_MTX(object), PVM, waitid, 0);
367 * vm_object_allocate:
369 * Returns a new object with the given size.
372 vm_object_allocate(objtype_t type, vm_pindex_t size)
376 object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK);
377 _vm_object_allocate(type, size, object);
383 * vm_object_reference:
385 * Gets another reference to the given object. Note: OBJ_DEAD
386 * objects can be referenced during final cleaning.
389 vm_object_reference(vm_object_t object)
393 VM_OBJECT_LOCK(object);
394 vm_object_reference_locked(object);
395 VM_OBJECT_UNLOCK(object);
399 * vm_object_reference_locked:
401 * Gets another reference to the given object.
403 * The object must be locked.
406 vm_object_reference_locked(vm_object_t object)
410 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
412 if (object->type == OBJT_VNODE) {
419 * Handle deallocating an object of type OBJT_VNODE.
422 vm_object_vndeallocate(vm_object_t object)
424 struct vnode *vp = (struct vnode *) object->handle;
426 VFS_ASSERT_GIANT(vp->v_mount);
427 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
428 KASSERT(object->type == OBJT_VNODE,
429 ("vm_object_vndeallocate: not a vnode object"));
430 KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
432 if (object->ref_count == 0) {
433 vprint("vm_object_vndeallocate", vp);
434 panic("vm_object_vndeallocate: bad object reference count");
439 if (object->ref_count == 0) {
440 mp_fixme("Unlocked vflag access.");
441 vp->v_vflag &= ~VV_TEXT;
443 VM_OBJECT_UNLOCK(object);
445 * vrele may need a vop lock
451 * vm_object_deallocate:
453 * Release a reference to the specified object,
454 * gained either through a vm_object_allocate
455 * or a vm_object_reference call. When all references
456 * are gone, storage associated with this object
457 * may be relinquished.
459 * No object may be locked.
462 vm_object_deallocate(vm_object_t object)
466 while (object != NULL) {
471 VM_OBJECT_LOCK(object);
472 if (object->type == OBJT_VNODE) {
473 struct vnode *vp = (struct vnode *) object->handle;
476 * Conditionally acquire Giant for a vnode-backed
477 * object. We have to be careful since the type of
478 * a vnode object can change while the object is
481 if (VFS_NEEDSGIANT(vp->v_mount) && !vfslocked) {
483 if (!mtx_trylock(&Giant)) {
484 VM_OBJECT_UNLOCK(object);
489 vm_object_vndeallocate(object);
490 VFS_UNLOCK_GIANT(vfslocked);
494 * This is to handle the case that the object
495 * changed type while we dropped its lock to
498 VFS_UNLOCK_GIANT(vfslocked);
500 KASSERT(object->ref_count != 0,
501 ("vm_object_deallocate: object deallocated too many times: %d", object->type));
504 * If the reference count goes to 0 we start calling
505 * vm_object_terminate() on the object chain.
506 * A ref count of 1 may be a special case depending on the
507 * shadow count being 0 or 1.
510 if (object->ref_count > 1) {
511 VM_OBJECT_UNLOCK(object);
513 } else if (object->ref_count == 1) {
514 if (object->shadow_count == 0 &&
515 object->handle == NULL &&
516 (object->type == OBJT_DEFAULT ||
517 object->type == OBJT_SWAP)) {
518 vm_object_set_flag(object, OBJ_ONEMAPPING);
519 } else if ((object->shadow_count == 1) &&
520 (object->handle == NULL) &&
521 (object->type == OBJT_DEFAULT ||
522 object->type == OBJT_SWAP)) {
525 robject = LIST_FIRST(&object->shadow_head);
526 KASSERT(robject != NULL,
527 ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
529 object->shadow_count));
530 if (!VM_OBJECT_TRYLOCK(robject)) {
532 * Avoid a potential deadlock.
535 VM_OBJECT_UNLOCK(object);
537 * More likely than not the thread
538 * holding robject's lock has lower
539 * priority than the current thread.
540 * Let the lower priority thread run.
546 * Collapse object into its shadow unless its
547 * shadow is dead. In that case, object will
548 * be deallocated by the thread that is
549 * deallocating its shadow.
551 if ((robject->flags & OBJ_DEAD) == 0 &&
552 (robject->handle == NULL) &&
553 (robject->type == OBJT_DEFAULT ||
554 robject->type == OBJT_SWAP)) {
556 robject->ref_count++;
558 if (robject->paging_in_progress) {
559 VM_OBJECT_UNLOCK(object);
560 vm_object_pip_wait(robject,
562 temp = robject->backing_object;
563 if (object == temp) {
564 VM_OBJECT_LOCK(object);
567 } else if (object->paging_in_progress) {
568 VM_OBJECT_UNLOCK(robject);
569 object->flags |= OBJ_PIPWNT;
571 VM_OBJECT_MTX(object),
572 PDROP | PVM, "objde2", 0);
573 VM_OBJECT_LOCK(robject);
574 temp = robject->backing_object;
575 if (object == temp) {
576 VM_OBJECT_LOCK(object);
580 VM_OBJECT_UNLOCK(object);
582 if (robject->ref_count == 1) {
583 robject->ref_count--;
588 vm_object_collapse(object);
589 VM_OBJECT_UNLOCK(object);
592 VM_OBJECT_UNLOCK(robject);
594 VM_OBJECT_UNLOCK(object);
598 temp = object->backing_object;
600 VM_OBJECT_LOCK(temp);
601 LIST_REMOVE(object, shadow_list);
602 temp->shadow_count--;
603 VM_OBJECT_UNLOCK(temp);
604 object->backing_object = NULL;
607 * Don't double-terminate, we could be in a termination
608 * recursion due to the terminate having to sync data
611 if ((object->flags & OBJ_DEAD) == 0)
612 vm_object_terminate(object);
614 VM_OBJECT_UNLOCK(object);
620 * vm_object_destroy removes the object from the global object list
621 * and frees the space for the object.
624 vm_object_destroy(vm_object_t object)
628 * Remove the object from the global object list.
630 mtx_lock(&vm_object_list_mtx);
631 TAILQ_REMOVE(&vm_object_list, object, object_list);
632 mtx_unlock(&vm_object_list_mtx);
635 * Release the allocation charge.
637 if (object->uip != NULL) {
638 KASSERT(object->type == OBJT_DEFAULT ||
639 object->type == OBJT_SWAP,
640 ("vm_object_terminate: non-swap obj %p has uip",
642 swap_release_by_uid(object->charge, object->uip);
649 * Free the space for the object.
651 uma_zfree(obj_zone, object);
655 * vm_object_terminate actually destroys the specified object, freeing
656 * up all previously used resources.
658 * The object must be locked.
659 * This routine may block.
662 vm_object_terminate(vm_object_t object)
666 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
669 * Make sure no one uses us.
671 vm_object_set_flag(object, OBJ_DEAD);
674 * wait for the pageout daemon to be done with the object
676 vm_object_pip_wait(object, "objtrm");
678 KASSERT(!object->paging_in_progress,
679 ("vm_object_terminate: pageout in progress"));
682 * Clean and free the pages, as appropriate. All references to the
683 * object are gone, so we don't need to lock it.
685 if (object->type == OBJT_VNODE) {
686 struct vnode *vp = (struct vnode *)object->handle;
689 * Clean pages and flush buffers.
691 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
692 VM_OBJECT_UNLOCK(object);
694 vinvalbuf(vp, V_SAVE, 0, 0);
696 VM_OBJECT_LOCK(object);
699 KASSERT(object->ref_count == 0,
700 ("vm_object_terminate: object with references, ref_count=%d",
704 * Now free any remaining pages. For internal objects, this also
705 * removes them from paging queues. Don't free wired pages, just
706 * remove them from the object.
708 while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
709 KASSERT(!p->busy && (p->oflags & VPO_BUSY) == 0,
710 ("vm_object_terminate: freeing busy page %p "
711 "p->busy = %d, p->oflags %x\n", p, p->busy, p->oflags));
713 if (p->wire_count == 0) {
715 PCPU_INC(cnt.v_pfree);
721 #if VM_NRESERVLEVEL > 0
722 if (__predict_false(!LIST_EMPTY(&object->rvq)))
723 vm_reserv_break_all(object);
725 if (__predict_false(object->cache != NULL))
726 vm_page_cache_free(object, 0, 0);
729 * Let the pager know object is dead.
731 vm_pager_deallocate(object);
732 VM_OBJECT_UNLOCK(object);
734 vm_object_destroy(object);
738 * vm_object_page_clean
740 * Clean all dirty pages in the specified range of object. Leaves page
741 * on whatever queue it is currently on. If NOSYNC is set then do not
742 * write out pages with VPO_NOSYNC set (originally comes from MAP_NOSYNC),
743 * leaving the object dirty.
745 * When stuffing pages asynchronously, allow clustering. XXX we need a
746 * synchronous clustering mode implementation.
748 * Odd semantics: if start == end, we clean everything.
750 * The object must be locked.
753 vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
757 vm_pindex_t pi, tend;
758 int clearobjflags, curgeneration, n, pagerflags;
760 mtx_assert(&vm_page_queue_mtx, MA_NOTOWNED);
761 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
762 KASSERT(object->type == OBJT_VNODE, ("Not a vnode object"));
763 if ((object->flags & OBJ_MIGHTBEDIRTY) == 0 ||
764 object->resident_page_count == 0)
767 pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) != 0 ?
768 VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
769 pagerflags |= (flags & OBJPC_INVAL) != 0 ? VM_PAGER_PUT_INVAL : 0;
771 tend = (end == 0) ? object->size : end;
773 vm_object_set_flag(object, OBJ_CLEANING);
776 * Make the page read-only so we can then clear the object flags.
778 * However, if this is a nosync mmap then the object is likely to
779 * stay dirty so do not mess with the page and do not clear the
783 for (p = vm_page_find_least(object, start);
784 p != NULL && p->pindex < tend; p = TAILQ_NEXT(p, listq)) {
785 if ((flags & OBJPC_NOSYNC) != 0 &&
786 (p->oflags & VPO_NOSYNC) != 0)
789 pmap_remove_write(p);
792 if (clearobjflags && (start == 0) && (tend == object->size))
793 vm_object_clear_flag(object, OBJ_MIGHTBEDIRTY);
796 curgeneration = object->generation;
798 for (p = vm_page_find_least(object, start); p != NULL; p = np) {
802 np = TAILQ_NEXT(p, listq);
805 while (vm_page_sleep_if_busy(p, TRUE, "vpcwai")) {
806 if (object->generation != curgeneration)
809 vm_page_test_dirty(p);
814 * If we have been asked to skip nosync pages and this is a
815 * nosync page, skip it. Note that the object flags were
816 * not cleared in this case so we do not have to set them.
818 if ((flags & OBJPC_NOSYNC) != 0 &&
819 (p->oflags & VPO_NOSYNC) != 0)
822 n = vm_object_page_collect_flush(object, p, pagerflags);
823 KASSERT(n > 0, ("vm_object_page_collect_flush failed"));
824 if (object->generation != curgeneration)
826 np = vm_page_find_least(object, pi + n);
829 VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC) ? MNT_WAIT : 0);
832 vm_object_clear_flag(object, OBJ_CLEANING);
836 vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int pagerflags)
844 vm_page_t maf[vm_pageout_page_count];
845 vm_page_t mab[vm_pageout_page_count];
846 vm_page_t ma[vm_pageout_page_count];
849 mtx_assert(&vm_page_queue_mtx, MA_NOTOWNED);
850 vm_page_lock_assert(p, MA_NOTOWNED);
851 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
854 for (i = 1, p1 = p; i < vm_pageout_page_count; i++) {
855 tp = vm_page_next(p1);
856 if (tp == NULL || tp->busy != 0 || (tp->oflags & VPO_BUSY) != 0)
858 vm_page_test_dirty(tp);
861 maf[i - 1] = p1 = tp;
866 chkb = vm_pageout_page_count - maxf;
867 for (i = 1, p1 = p; i < chkb; i++) {
868 tp = vm_page_prev(p1);
869 if (tp == NULL || tp->busy != 0 || (tp->oflags & VPO_BUSY) != 0)
871 vm_page_test_dirty(tp);
874 mab[i - 1] = p1 = tp;
878 for (i = 0; i < maxb; i++) {
879 index = (maxb - i) - 1;
883 for (i = 0; i < maxf; i++) {
884 index = (maxb + i) + 1;
888 vm_pageout_flush(ma, maxb + maxf + 1, pagerflags, maxb + 1, &runlen);
893 * Note that there is absolutely no sense in writing out
894 * anonymous objects, so we track down the vnode object
896 * We invalidate (remove) all pages from the address space
897 * for semantic correctness.
899 * Note: certain anonymous maps, such as MAP_NOSYNC maps,
900 * may start out with a NULL object.
903 vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size,
904 boolean_t syncio, boolean_t invalidate)
906 vm_object_t backing_object;
913 VM_OBJECT_LOCK(object);
914 while ((backing_object = object->backing_object) != NULL) {
915 VM_OBJECT_LOCK(backing_object);
916 offset += object->backing_object_offset;
917 VM_OBJECT_UNLOCK(object);
918 object = backing_object;
919 if (object->size < OFF_TO_IDX(offset + size))
920 size = IDX_TO_OFF(object->size) - offset;
923 * Flush pages if writing is allowed, invalidate them
924 * if invalidation requested. Pages undergoing I/O
925 * will be ignored by vm_object_page_remove().
927 * We cannot lock the vnode and then wait for paging
928 * to complete without deadlocking against vm_fault.
929 * Instead we simply call vm_object_page_remove() and
930 * allow it to block internally on a page-by-page
931 * basis when it encounters pages undergoing async
934 if (object->type == OBJT_VNODE &&
935 (object->flags & OBJ_MIGHTBEDIRTY) != 0) {
938 VM_OBJECT_UNLOCK(object);
939 (void) vn_start_write(vp, &mp, V_WAIT);
940 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
941 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
942 flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
943 flags |= invalidate ? OBJPC_INVAL : 0;
944 VM_OBJECT_LOCK(object);
945 vm_object_page_clean(object,
947 OFF_TO_IDX(offset + size + PAGE_MASK),
949 VM_OBJECT_UNLOCK(object);
951 VFS_UNLOCK_GIANT(vfslocked);
952 vn_finished_write(mp);
953 VM_OBJECT_LOCK(object);
955 if ((object->type == OBJT_VNODE ||
956 object->type == OBJT_DEVICE) && invalidate) {
958 purge = old_msync || (object->type == OBJT_DEVICE);
959 vm_object_page_remove(object,
961 OFF_TO_IDX(offset + size + PAGE_MASK),
962 purge ? FALSE : TRUE);
964 VM_OBJECT_UNLOCK(object);
970 * Implements the madvise function at the object/page level.
972 * MADV_WILLNEED (any object)
974 * Activate the specified pages if they are resident.
976 * MADV_DONTNEED (any object)
978 * Deactivate the specified pages if they are resident.
980 * MADV_FREE (OBJT_DEFAULT/OBJT_SWAP objects,
981 * OBJ_ONEMAPPING only)
983 * Deactivate and clean the specified pages if they are
984 * resident. This permits the process to reuse the pages
985 * without faulting or the kernel to reclaim the pages
989 vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
991 vm_pindex_t end, tpindex;
992 vm_object_t backing_object, tobject;
997 VM_OBJECT_LOCK(object);
998 end = pindex + count;
1000 * Locate and adjust resident pages
1002 for (; pindex < end; pindex += 1) {
1008 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
1009 * and those pages must be OBJ_ONEMAPPING.
1011 if (advise == MADV_FREE) {
1012 if ((tobject->type != OBJT_DEFAULT &&
1013 tobject->type != OBJT_SWAP) ||
1014 (tobject->flags & OBJ_ONEMAPPING) == 0) {
1015 goto unlock_tobject;
1017 } else if (tobject->type == OBJT_PHYS)
1018 goto unlock_tobject;
1019 m = vm_page_lookup(tobject, tpindex);
1020 if (m == NULL && advise == MADV_WILLNEED) {
1022 * If the page is cached, reactivate it.
1024 m = vm_page_alloc(tobject, tpindex, VM_ALLOC_IFCACHED |
1029 * There may be swap even if there is no backing page
1031 if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1032 swap_pager_freespace(tobject, tpindex, 1);
1036 backing_object = tobject->backing_object;
1037 if (backing_object == NULL)
1038 goto unlock_tobject;
1039 VM_OBJECT_LOCK(backing_object);
1040 tpindex += OFF_TO_IDX(tobject->backing_object_offset);
1041 if (tobject != object)
1042 VM_OBJECT_UNLOCK(tobject);
1043 tobject = backing_object;
1045 } else if (m->valid != VM_PAGE_BITS_ALL)
1046 goto unlock_tobject;
1048 * If the page is not in a normal state, skip it.
1051 if (m->hold_count != 0 || m->wire_count != 0) {
1053 goto unlock_tobject;
1055 KASSERT((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0,
1056 ("vm_object_madvise: page %p is not managed", m));
1057 if ((m->oflags & VPO_BUSY) || m->busy) {
1058 if (advise == MADV_WILLNEED) {
1060 * Reference the page before unlocking and
1061 * sleeping so that the page daemon is less
1062 * likely to reclaim it.
1064 vm_page_lock_queues();
1065 vm_page_flag_set(m, PG_REFERENCED);
1066 vm_page_unlock_queues();
1069 if (object != tobject)
1070 VM_OBJECT_UNLOCK(object);
1071 m->oflags |= VPO_WANTED;
1072 msleep(m, VM_OBJECT_MTX(tobject), PDROP | PVM, "madvpo",
1074 VM_OBJECT_LOCK(object);
1077 if (advise == MADV_WILLNEED) {
1078 vm_page_activate(m);
1079 } else if (advise == MADV_DONTNEED) {
1080 vm_page_dontneed(m);
1081 } else if (advise == MADV_FREE) {
1083 * Mark the page clean. This will allow the page
1084 * to be freed up by the system. However, such pages
1085 * are often reused quickly by malloc()/free()
1086 * so we do not do anything that would cause
1087 * a page fault if we can help it.
1089 * Specifically, we do not try to actually free
1090 * the page now nor do we try to put it in the
1091 * cache (which would cause a page fault on reuse).
1093 * But we do make the page is freeable as we
1094 * can without actually taking the step of unmapping
1097 pmap_clear_modify(m);
1100 vm_page_dontneed(m);
1103 if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1104 swap_pager_freespace(tobject, tpindex, 1);
1106 if (tobject != object)
1107 VM_OBJECT_UNLOCK(tobject);
1109 VM_OBJECT_UNLOCK(object);
1115 * Create a new object which is backed by the
1116 * specified existing object range. The source
1117 * object reference is deallocated.
1119 * The new object and offset into that object
1120 * are returned in the source parameters.
1124 vm_object_t *object, /* IN/OUT */
1125 vm_ooffset_t *offset, /* IN/OUT */
1134 * Don't create the new object if the old object isn't shared.
1136 if (source != NULL) {
1137 VM_OBJECT_LOCK(source);
1138 if (source->ref_count == 1 &&
1139 source->handle == NULL &&
1140 (source->type == OBJT_DEFAULT ||
1141 source->type == OBJT_SWAP)) {
1142 VM_OBJECT_UNLOCK(source);
1145 VM_OBJECT_UNLOCK(source);
1149 * Allocate a new object with the given length.
1151 result = vm_object_allocate(OBJT_DEFAULT, length);
1154 * The new object shadows the source object, adding a reference to it.
1155 * Our caller changes his reference to point to the new object,
1156 * removing a reference to the source object. Net result: no change
1157 * of reference count.
1159 * Try to optimize the result object's page color when shadowing
1160 * in order to maintain page coloring consistency in the combined
1163 result->backing_object = source;
1165 * Store the offset into the source object, and fix up the offset into
1168 result->backing_object_offset = *offset;
1169 if (source != NULL) {
1170 VM_OBJECT_LOCK(source);
1171 LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1172 source->shadow_count++;
1173 #if VM_NRESERVLEVEL > 0
1174 result->flags |= source->flags & OBJ_COLORED;
1175 result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) &
1176 ((1 << (VM_NFREEORDER - 1)) - 1);
1178 VM_OBJECT_UNLOCK(source);
1183 * Return the new things
1192 * Split the pages in a map entry into a new object. This affords
1193 * easier removal of unused pages, and keeps object inheritance from
1194 * being a negative impact on memory usage.
1197 vm_object_split(vm_map_entry_t entry)
1199 vm_page_t m, m_next;
1200 vm_object_t orig_object, new_object, source;
1201 vm_pindex_t idx, offidxstart;
1204 orig_object = entry->object.vm_object;
1205 if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1207 if (orig_object->ref_count <= 1)
1209 VM_OBJECT_UNLOCK(orig_object);
1211 offidxstart = OFF_TO_IDX(entry->offset);
1212 size = atop(entry->end - entry->start);
1215 * If swap_pager_copy() is later called, it will convert new_object
1216 * into a swap object.
1218 new_object = vm_object_allocate(OBJT_DEFAULT, size);
1221 * At this point, the new object is still private, so the order in
1222 * which the original and new objects are locked does not matter.
1224 VM_OBJECT_LOCK(new_object);
1225 VM_OBJECT_LOCK(orig_object);
1226 source = orig_object->backing_object;
1227 if (source != NULL) {
1228 VM_OBJECT_LOCK(source);
1229 if ((source->flags & OBJ_DEAD) != 0) {
1230 VM_OBJECT_UNLOCK(source);
1231 VM_OBJECT_UNLOCK(orig_object);
1232 VM_OBJECT_UNLOCK(new_object);
1233 vm_object_deallocate(new_object);
1234 VM_OBJECT_LOCK(orig_object);
1237 LIST_INSERT_HEAD(&source->shadow_head,
1238 new_object, shadow_list);
1239 source->shadow_count++;
1240 vm_object_reference_locked(source); /* for new_object */
1241 vm_object_clear_flag(source, OBJ_ONEMAPPING);
1242 VM_OBJECT_UNLOCK(source);
1243 new_object->backing_object_offset =
1244 orig_object->backing_object_offset + entry->offset;
1245 new_object->backing_object = source;
1247 if (orig_object->uip != NULL) {
1248 new_object->uip = orig_object->uip;
1249 uihold(orig_object->uip);
1250 new_object->charge = ptoa(size);
1251 KASSERT(orig_object->charge >= ptoa(size),
1252 ("orig_object->charge < 0"));
1253 orig_object->charge -= ptoa(size);
1256 m = vm_page_find_least(orig_object, offidxstart);
1257 for (; m != NULL && (idx = m->pindex - offidxstart) < size;
1259 m_next = TAILQ_NEXT(m, listq);
1262 * We must wait for pending I/O to complete before we can
1265 * We do not have to VM_PROT_NONE the page as mappings should
1266 * not be changed by this operation.
1268 if ((m->oflags & VPO_BUSY) || m->busy) {
1269 VM_OBJECT_UNLOCK(new_object);
1270 m->oflags |= VPO_WANTED;
1271 msleep(m, VM_OBJECT_MTX(orig_object), PVM, "spltwt", 0);
1272 VM_OBJECT_LOCK(new_object);
1276 vm_page_rename(m, new_object, idx);
1278 /* page automatically made dirty by rename and cache handled */
1281 if (orig_object->type == OBJT_SWAP) {
1283 * swap_pager_copy() can sleep, in which case the orig_object's
1284 * and new_object's locks are released and reacquired.
1286 swap_pager_copy(orig_object, new_object, offidxstart, 0);
1289 * Transfer any cached pages from orig_object to new_object.
1291 if (__predict_false(orig_object->cache != NULL))
1292 vm_page_cache_transfer(orig_object, offidxstart,
1295 VM_OBJECT_UNLOCK(orig_object);
1296 TAILQ_FOREACH(m, &new_object->memq, listq)
1298 VM_OBJECT_UNLOCK(new_object);
1299 entry->object.vm_object = new_object;
1300 entry->offset = 0LL;
1301 vm_object_deallocate(orig_object);
1302 VM_OBJECT_LOCK(new_object);
1305 #define OBSC_TEST_ALL_SHADOWED 0x0001
1306 #define OBSC_COLLAPSE_NOWAIT 0x0002
1307 #define OBSC_COLLAPSE_WAIT 0x0004
1310 vm_object_backing_scan(vm_object_t object, int op)
1314 vm_object_t backing_object;
1315 vm_pindex_t backing_offset_index;
1317 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1318 VM_OBJECT_LOCK_ASSERT(object->backing_object, MA_OWNED);
1320 backing_object = object->backing_object;
1321 backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
1324 * Initial conditions
1326 if (op & OBSC_TEST_ALL_SHADOWED) {
1328 * We do not want to have to test for the existence of cache
1329 * or swap pages in the backing object. XXX but with the
1330 * new swapper this would be pretty easy to do.
1332 * XXX what about anonymous MAP_SHARED memory that hasn't
1333 * been ZFOD faulted yet? If we do not test for this, the
1334 * shadow test may succeed! XXX
1336 if (backing_object->type != OBJT_DEFAULT) {
1340 if (op & OBSC_COLLAPSE_WAIT) {
1341 vm_object_set_flag(backing_object, OBJ_DEAD);
1347 p = TAILQ_FIRST(&backing_object->memq);
1349 vm_page_t next = TAILQ_NEXT(p, listq);
1350 vm_pindex_t new_pindex = p->pindex - backing_offset_index;
1352 if (op & OBSC_TEST_ALL_SHADOWED) {
1356 * Ignore pages outside the parent object's range
1357 * and outside the parent object's mapping of the
1360 * note that we do not busy the backing object's
1364 p->pindex < backing_offset_index ||
1365 new_pindex >= object->size
1372 * See if the parent has the page or if the parent's
1373 * object pager has the page. If the parent has the
1374 * page but the page is not valid, the parent's
1375 * object pager must have the page.
1377 * If this fails, the parent does not completely shadow
1378 * the object and we might as well give up now.
1381 pp = vm_page_lookup(object, new_pindex);
1383 (pp == NULL || pp->valid == 0) &&
1384 !vm_pager_has_page(object, new_pindex, NULL, NULL)
1392 * Check for busy page
1394 if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
1397 if (op & OBSC_COLLAPSE_NOWAIT) {
1398 if ((p->oflags & VPO_BUSY) ||
1404 } else if (op & OBSC_COLLAPSE_WAIT) {
1405 if ((p->oflags & VPO_BUSY) || p->busy) {
1406 VM_OBJECT_UNLOCK(object);
1407 p->oflags |= VPO_WANTED;
1408 msleep(p, VM_OBJECT_MTX(backing_object),
1409 PDROP | PVM, "vmocol", 0);
1410 VM_OBJECT_LOCK(object);
1411 VM_OBJECT_LOCK(backing_object);
1413 * If we slept, anything could have
1414 * happened. Since the object is
1415 * marked dead, the backing offset
1416 * should not have changed so we
1417 * just restart our scan.
1419 p = TAILQ_FIRST(&backing_object->memq);
1425 p->object == backing_object,
1426 ("vm_object_backing_scan: object mismatch")
1430 * Destroy any associated swap
1432 if (backing_object->type == OBJT_SWAP) {
1433 swap_pager_freespace(
1441 p->pindex < backing_offset_index ||
1442 new_pindex >= object->size
1445 * Page is out of the parent object's range, we
1446 * can simply destroy it.
1449 KASSERT(!pmap_page_is_mapped(p),
1450 ("freeing mapped page %p", p));
1451 if (p->wire_count == 0)
1460 pp = vm_page_lookup(object, new_pindex);
1463 vm_pager_has_page(object, new_pindex, NULL, NULL)
1466 * page already exists in parent OR swap exists
1467 * for this location in the parent. Destroy
1468 * the original page from the backing object.
1470 * Leave the parent's page alone
1473 KASSERT(!pmap_page_is_mapped(p),
1474 ("freeing mapped page %p", p));
1475 if (p->wire_count == 0)
1484 #if VM_NRESERVLEVEL > 0
1486 * Rename the reservation.
1488 vm_reserv_rename(p, object, backing_object,
1489 backing_offset_index);
1493 * Page does not exist in parent, rename the
1494 * page from the backing object to the main object.
1496 * If the page was mapped to a process, it can remain
1497 * mapped through the rename.
1500 vm_page_rename(p, object, new_pindex);
1502 /* page automatically made dirty by rename */
1511 * this version of collapse allows the operation to occur earlier and
1512 * when paging_in_progress is true for an object... This is not a complete
1513 * operation, but should plug 99.9% of the rest of the leaks.
1516 vm_object_qcollapse(vm_object_t object)
1518 vm_object_t backing_object = object->backing_object;
1520 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1521 VM_OBJECT_LOCK_ASSERT(backing_object, MA_OWNED);
1523 if (backing_object->ref_count != 1)
1526 vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
1530 * vm_object_collapse:
1532 * Collapse an object with the object backing it.
1533 * Pages in the backing object are moved into the
1534 * parent, and the backing object is deallocated.
1537 vm_object_collapse(vm_object_t object)
1539 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1542 vm_object_t backing_object;
1545 * Verify that the conditions are right for collapse:
1547 * The object exists and the backing object exists.
1549 if ((backing_object = object->backing_object) == NULL)
1553 * we check the backing object first, because it is most likely
1556 VM_OBJECT_LOCK(backing_object);
1557 if (backing_object->handle != NULL ||
1558 (backing_object->type != OBJT_DEFAULT &&
1559 backing_object->type != OBJT_SWAP) ||
1560 (backing_object->flags & OBJ_DEAD) ||
1561 object->handle != NULL ||
1562 (object->type != OBJT_DEFAULT &&
1563 object->type != OBJT_SWAP) ||
1564 (object->flags & OBJ_DEAD)) {
1565 VM_OBJECT_UNLOCK(backing_object);
1570 object->paging_in_progress != 0 ||
1571 backing_object->paging_in_progress != 0
1573 vm_object_qcollapse(object);
1574 VM_OBJECT_UNLOCK(backing_object);
1578 * We know that we can either collapse the backing object (if
1579 * the parent is the only reference to it) or (perhaps) have
1580 * the parent bypass the object if the parent happens to shadow
1581 * all the resident pages in the entire backing object.
1583 * This is ignoring pager-backed pages such as swap pages.
1584 * vm_object_backing_scan fails the shadowing test in this
1587 if (backing_object->ref_count == 1) {
1589 * If there is exactly one reference to the backing
1590 * object, we can collapse it into the parent.
1592 vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1594 #if VM_NRESERVLEVEL > 0
1596 * Break any reservations from backing_object.
1598 if (__predict_false(!LIST_EMPTY(&backing_object->rvq)))
1599 vm_reserv_break_all(backing_object);
1603 * Move the pager from backing_object to object.
1605 if (backing_object->type == OBJT_SWAP) {
1607 * swap_pager_copy() can sleep, in which case
1608 * the backing_object's and object's locks are
1609 * released and reacquired.
1614 OFF_TO_IDX(object->backing_object_offset), TRUE);
1617 * Free any cached pages from backing_object.
1619 if (__predict_false(backing_object->cache != NULL))
1620 vm_page_cache_free(backing_object, 0, 0);
1623 * Object now shadows whatever backing_object did.
1624 * Note that the reference to
1625 * backing_object->backing_object moves from within
1626 * backing_object to within object.
1628 LIST_REMOVE(object, shadow_list);
1629 backing_object->shadow_count--;
1630 if (backing_object->backing_object) {
1631 VM_OBJECT_LOCK(backing_object->backing_object);
1632 LIST_REMOVE(backing_object, shadow_list);
1634 &backing_object->backing_object->shadow_head,
1635 object, shadow_list);
1637 * The shadow_count has not changed.
1639 VM_OBJECT_UNLOCK(backing_object->backing_object);
1641 object->backing_object = backing_object->backing_object;
1642 object->backing_object_offset +=
1643 backing_object->backing_object_offset;
1646 * Discard backing_object.
1648 * Since the backing object has no pages, no pager left,
1649 * and no object references within it, all that is
1650 * necessary is to dispose of it.
1652 KASSERT(backing_object->ref_count == 1, (
1653 "backing_object %p was somehow re-referenced during collapse!",
1655 VM_OBJECT_UNLOCK(backing_object);
1656 vm_object_destroy(backing_object);
1660 vm_object_t new_backing_object;
1663 * If we do not entirely shadow the backing object,
1664 * there is nothing we can do so we give up.
1666 if (object->resident_page_count != object->size &&
1667 vm_object_backing_scan(object,
1668 OBSC_TEST_ALL_SHADOWED) == 0) {
1669 VM_OBJECT_UNLOCK(backing_object);
1674 * Make the parent shadow the next object in the
1675 * chain. Deallocating backing_object will not remove
1676 * it, since its reference count is at least 2.
1678 LIST_REMOVE(object, shadow_list);
1679 backing_object->shadow_count--;
1681 new_backing_object = backing_object->backing_object;
1682 if ((object->backing_object = new_backing_object) != NULL) {
1683 VM_OBJECT_LOCK(new_backing_object);
1685 &new_backing_object->shadow_head,
1689 new_backing_object->shadow_count++;
1690 vm_object_reference_locked(new_backing_object);
1691 VM_OBJECT_UNLOCK(new_backing_object);
1692 object->backing_object_offset +=
1693 backing_object->backing_object_offset;
1697 * Drop the reference count on backing_object. Since
1698 * its ref_count was at least 2, it will not vanish.
1700 backing_object->ref_count--;
1701 VM_OBJECT_UNLOCK(backing_object);
1706 * Try again with this object's new backing object.
1712 * vm_object_page_remove:
1714 * For the given object, either frees or invalidates each of the
1715 * specified pages. In general, a page is freed. However, if a
1716 * page is wired for any reason other than the existence of a
1717 * managed, wired mapping, then it may be invalidated but not
1718 * removed from the object. Pages are specified by the given
1719 * range ["start", "end") and Boolean "clean_only". As a
1720 * special case, if "end" is zero, then the range extends from
1721 * "start" to the end of the object. If "clean_only" is TRUE,
1722 * then only the non-dirty pages within the specified range are
1725 * In general, this operation should only be performed on objects
1726 * that contain managed pages. There are two exceptions. First,
1727 * it may be performed on the kernel and kmem objects. Second,
1728 * it may be used by msync(..., MS_INVALIDATE) to invalidate
1729 * device-backed pages. In both of these cases, "clean_only"
1732 * The object must be locked.
1735 vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
1736 boolean_t clean_only)
1741 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1742 if (object->resident_page_count == 0)
1746 * Since physically-backed objects do not use managed pages, we can't
1747 * remove pages from the object (we must instead remove the page
1748 * references, and then destroy the object).
1750 KASSERT(object->type != OBJT_PHYS || object == kernel_object ||
1751 object == kmem_object,
1752 ("attempt to remove pages from a physical object"));
1754 vm_object_pip_add(object, 1);
1756 p = vm_page_find_least(object, start);
1759 * Assert: the variable p is either (1) the page with the
1760 * least pindex greater than or equal to the parameter pindex
1764 p != NULL && (p->pindex < end || end == 0);
1766 next = TAILQ_NEXT(p, listq);
1769 * If the page is wired for any reason besides the
1770 * existence of managed, wired mappings, then it cannot
1771 * be freed. For example, fictitious pages, which
1772 * represent device memory, are inherently wired and
1773 * cannot be freed. They can, however, be invalidated
1774 * if "clean_only" is FALSE.
1777 if ((wirings = p->wire_count) != 0 &&
1778 (wirings = pmap_page_wired_mappings(p)) != p->wire_count) {
1779 /* Fictitious pages do not have managed mappings. */
1780 if ((p->flags & PG_FICTITIOUS) == 0)
1782 /* Account for removal of managed, wired mappings. */
1783 p->wire_count -= wirings;
1791 if (vm_page_sleep_if_busy(p, TRUE, "vmopar"))
1793 KASSERT((p->flags & PG_FICTITIOUS) == 0,
1794 ("vm_object_page_remove: page %p is fictitious", p));
1795 if (clean_only && p->valid) {
1796 pmap_remove_write(p);
1803 /* Account for removal of managed, wired mappings. */
1805 p->wire_count -= wirings;
1809 vm_object_pip_wakeup(object);
1811 if (__predict_false(object->cache != NULL))
1812 vm_page_cache_free(object, start, end);
1816 * Populate the specified range of the object with valid pages. Returns
1817 * TRUE if the range is successfully populated and FALSE otherwise.
1819 * Note: This function should be optimized to pass a larger array of
1820 * pages to vm_pager_get_pages() before it is applied to a non-
1821 * OBJT_DEVICE object.
1823 * The object must be locked.
1826 vm_object_populate(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1832 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1833 for (pindex = start; pindex < end; pindex++) {
1834 m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL |
1836 if (m->valid != VM_PAGE_BITS_ALL) {
1838 rv = vm_pager_get_pages(object, ma, 1, 0);
1839 m = vm_page_lookup(object, pindex);
1842 if (rv != VM_PAGER_OK) {
1850 * Keep "m" busy because a subsequent iteration may unlock
1854 if (pindex > start) {
1855 m = vm_page_lookup(object, start);
1856 while (m != NULL && m->pindex < pindex) {
1858 m = TAILQ_NEXT(m, listq);
1861 return (pindex == end);
1865 * Routine: vm_object_coalesce
1866 * Function: Coalesces two objects backing up adjoining
1867 * regions of memory into a single object.
1869 * returns TRUE if objects were combined.
1871 * NOTE: Only works at the moment if the second object is NULL -
1872 * if it's not, which object do we lock first?
1875 * prev_object First object to coalesce
1876 * prev_offset Offset into prev_object
1877 * prev_size Size of reference to prev_object
1878 * next_size Size of reference to the second object
1879 * reserved Indicator that extension region has
1880 * swap accounted for
1883 * The object must *not* be locked.
1886 vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset,
1887 vm_size_t prev_size, vm_size_t next_size, boolean_t reserved)
1889 vm_pindex_t next_pindex;
1891 if (prev_object == NULL)
1893 VM_OBJECT_LOCK(prev_object);
1894 if (prev_object->type != OBJT_DEFAULT &&
1895 prev_object->type != OBJT_SWAP) {
1896 VM_OBJECT_UNLOCK(prev_object);
1901 * Try to collapse the object first
1903 vm_object_collapse(prev_object);
1906 * Can't coalesce if: . more than one reference . paged out . shadows
1907 * another object . has a copy elsewhere (any of which mean that the
1908 * pages not mapped to prev_entry may be in use anyway)
1910 if (prev_object->backing_object != NULL) {
1911 VM_OBJECT_UNLOCK(prev_object);
1915 prev_size >>= PAGE_SHIFT;
1916 next_size >>= PAGE_SHIFT;
1917 next_pindex = OFF_TO_IDX(prev_offset) + prev_size;
1919 if ((prev_object->ref_count > 1) &&
1920 (prev_object->size != next_pindex)) {
1921 VM_OBJECT_UNLOCK(prev_object);
1926 * Account for the charge.
1928 if (prev_object->uip != NULL) {
1931 * If prev_object was charged, then this mapping,
1932 * althought not charged now, may become writable
1933 * later. Non-NULL uip in the object would prevent
1934 * swap reservation during enabling of the write
1935 * access, so reserve swap now. Failed reservation
1936 * cause allocation of the separate object for the map
1937 * entry, and swap reservation for this entry is
1938 * managed in appropriate time.
1940 if (!reserved && !swap_reserve_by_uid(ptoa(next_size),
1941 prev_object->uip)) {
1944 prev_object->charge += ptoa(next_size);
1948 * Remove any pages that may still be in the object from a previous
1951 if (next_pindex < prev_object->size) {
1952 vm_object_page_remove(prev_object,
1954 next_pindex + next_size, FALSE);
1955 if (prev_object->type == OBJT_SWAP)
1956 swap_pager_freespace(prev_object,
1957 next_pindex, next_size);
1959 if (prev_object->uip != NULL) {
1960 KASSERT(prev_object->charge >=
1961 ptoa(prev_object->size - next_pindex),
1962 ("object %p overcharged 1 %jx %jx", prev_object,
1963 (uintmax_t)next_pindex, (uintmax_t)next_size));
1964 prev_object->charge -= ptoa(prev_object->size -
1971 * Extend the object if necessary.
1973 if (next_pindex + next_size > prev_object->size)
1974 prev_object->size = next_pindex + next_size;
1976 VM_OBJECT_UNLOCK(prev_object);
1981 vm_object_set_writeable_dirty(vm_object_t object)
1984 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1985 if (object->type != OBJT_VNODE ||
1986 (object->flags & OBJ_MIGHTBEDIRTY) != 0)
1988 vm_object_set_flag(object, OBJ_MIGHTBEDIRTY);
1991 #include "opt_ddb.h"
1993 #include <sys/kernel.h>
1995 #include <sys/cons.h>
1997 #include <ddb/ddb.h>
2000 _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
2003 vm_map_entry_t tmpe;
2011 tmpe = map->header.next;
2012 entcount = map->nentries;
2013 while (entcount-- && (tmpe != &map->header)) {
2014 if (_vm_object_in_map(map, object, tmpe)) {
2019 } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
2020 tmpm = entry->object.sub_map;
2021 tmpe = tmpm->header.next;
2022 entcount = tmpm->nentries;
2023 while (entcount-- && tmpe != &tmpm->header) {
2024 if (_vm_object_in_map(tmpm, object, tmpe)) {
2029 } else if ((obj = entry->object.vm_object) != NULL) {
2030 for (; obj; obj = obj->backing_object)
2031 if (obj == object) {
2039 vm_object_in_map(vm_object_t object)
2043 /* sx_slock(&allproc_lock); */
2044 FOREACH_PROC_IN_SYSTEM(p) {
2045 if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
2047 if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
2048 /* sx_sunlock(&allproc_lock); */
2052 /* sx_sunlock(&allproc_lock); */
2053 if (_vm_object_in_map(kernel_map, object, 0))
2055 if (_vm_object_in_map(kmem_map, object, 0))
2057 if (_vm_object_in_map(pager_map, object, 0))
2059 if (_vm_object_in_map(buffer_map, object, 0))
2064 DB_SHOW_COMMAND(vmochk, vm_object_check)
2069 * make sure that internal objs are in a map somewhere
2070 * and none have zero ref counts.
2072 TAILQ_FOREACH(object, &vm_object_list, object_list) {
2073 if (object->handle == NULL &&
2074 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2075 if (object->ref_count == 0) {
2076 db_printf("vmochk: internal obj has zero ref count: %ld\n",
2077 (long)object->size);
2079 if (!vm_object_in_map(object)) {
2081 "vmochk: internal obj is not in a map: "
2082 "ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2083 object->ref_count, (u_long)object->size,
2084 (u_long)object->size,
2085 (void *)object->backing_object);
2092 * vm_object_print: [ debug ]
2094 DB_SHOW_COMMAND(object, vm_object_print_static)
2096 /* XXX convert args. */
2097 vm_object_t object = (vm_object_t)addr;
2098 boolean_t full = have_addr;
2102 /* XXX count is an (unused) arg. Avoid shadowing it. */
2103 #define count was_count
2111 "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x uip %d charge %jx\n",
2112 object, (int)object->type, (uintmax_t)object->size,
2113 object->resident_page_count, object->ref_count, object->flags,
2114 object->uip ? object->uip->ui_uid : -1, (uintmax_t)object->charge);
2115 db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n",
2116 object->shadow_count,
2117 object->backing_object ? object->backing_object->ref_count : 0,
2118 object->backing_object, (uintmax_t)object->backing_object_offset);
2125 TAILQ_FOREACH(p, &object->memq, listq) {
2127 db_iprintf("memory:=");
2128 else if (count == 6) {
2136 db_printf("(off=0x%jx,page=0x%jx)",
2137 (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p));
2147 /* XXX need this non-static entry for calling from vm_map_print. */
2150 /* db_expr_t */ long addr,
2151 boolean_t have_addr,
2152 /* db_expr_t */ long count,
2155 vm_object_print_static(addr, have_addr, count, modif);
2158 DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
2163 vm_page_t m, prev_m;
2167 TAILQ_FOREACH(object, &vm_object_list, object_list) {
2168 db_printf("new object: %p\n", (void *)object);
2179 TAILQ_FOREACH(m, &object->memq, listq) {
2180 if (m->pindex > 128)
2182 if ((prev_m = TAILQ_PREV(m, pglist, listq)) != NULL &&
2183 prev_m->pindex + 1 != m->pindex) {
2185 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2186 (long)fidx, rcount, (long)pa);
2198 (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
2203 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2204 (long)fidx, rcount, (long)pa);
2214 pa = VM_PAGE_TO_PHYS(m);
2218 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2219 (long)fidx, rcount, (long)pa);