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->cred != NULL) {
638 KASSERT(object->type == OBJT_DEFAULT ||
639 object->type == OBJT_SWAP,
640 ("vm_object_terminate: non-swap obj %p has cred",
642 swap_release_by_cred(object->charge, object->cred);
644 crfree(object->cred);
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 * Free any remaining pageable pages. This also removes them from the
705 * paging queues. However, don't free wired pages, just remove them
706 * from the object. Rather than incrementally removing each page from
707 * the object, the page and object are reset to any empty state.
709 TAILQ_FOREACH_SAFE(p, &object->memq, listq, p_next) {
710 KASSERT(!p->busy && (p->oflags & VPO_BUSY) == 0,
711 ("vm_object_terminate: freeing busy page %p", p));
714 * Optimize the page's removal from the object by resetting
715 * its "object" field. Specifically, if the page is not
716 * wired, then the effect of this assignment is that
717 * vm_page_free()'s call to vm_page_remove() will return
718 * immediately without modifying the page or the object.
721 if (p->wire_count == 0) {
723 PCPU_INC(cnt.v_pfree);
728 * If the object contained any pages, then reset it to an empty state.
729 * None of the object's fields, including "resident_page_count", were
730 * modified by the preceding loop.
732 if (object->resident_page_count != 0) {
734 TAILQ_INIT(&object->memq);
735 object->resident_page_count = 0;
736 if (object->type == OBJT_VNODE)
737 vdrop(object->handle);
740 #if VM_NRESERVLEVEL > 0
741 if (__predict_false(!LIST_EMPTY(&object->rvq)))
742 vm_reserv_break_all(object);
744 if (__predict_false(object->cache != NULL))
745 vm_page_cache_free(object, 0, 0);
748 * Let the pager know object is dead.
750 vm_pager_deallocate(object);
751 VM_OBJECT_UNLOCK(object);
753 vm_object_destroy(object);
757 * vm_object_page_clean
759 * Clean all dirty pages in the specified range of object. Leaves page
760 * on whatever queue it is currently on. If NOSYNC is set then do not
761 * write out pages with VPO_NOSYNC set (originally comes from MAP_NOSYNC),
762 * leaving the object dirty.
764 * When stuffing pages asynchronously, allow clustering. XXX we need a
765 * synchronous clustering mode implementation.
767 * Odd semantics: if start == end, we clean everything.
769 * The object must be locked.
772 vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
776 vm_pindex_t pi, tend;
777 int clearobjflags, curgeneration, n, pagerflags;
779 mtx_assert(&vm_page_queue_mtx, MA_NOTOWNED);
780 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
781 KASSERT(object->type == OBJT_VNODE, ("Not a vnode object"));
782 if ((object->flags & OBJ_MIGHTBEDIRTY) == 0 ||
783 object->resident_page_count == 0)
786 pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) != 0 ?
787 VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
788 pagerflags |= (flags & OBJPC_INVAL) != 0 ? VM_PAGER_PUT_INVAL : 0;
790 tend = (end == 0) ? object->size : end;
792 vm_object_set_flag(object, OBJ_CLEANING);
795 * Make the page read-only so we can then clear the object flags.
797 * However, if this is a nosync mmap then the object is likely to
798 * stay dirty so do not mess with the page and do not clear the
802 for (p = vm_page_find_least(object, start);
803 p != NULL && p->pindex < tend; p = TAILQ_NEXT(p, listq)) {
804 if ((flags & OBJPC_NOSYNC) != 0 &&
805 (p->oflags & VPO_NOSYNC) != 0)
808 pmap_remove_write(p);
811 if (clearobjflags && (start == 0) && (tend == object->size))
812 vm_object_clear_flag(object, OBJ_MIGHTBEDIRTY);
815 curgeneration = object->generation;
817 for (p = vm_page_find_least(object, start); p != NULL; p = np) {
821 np = TAILQ_NEXT(p, listq);
824 if (vm_page_sleep_if_busy(p, TRUE, "vpcwai")) {
825 if (object->generation != curgeneration)
827 np = vm_page_find_least(object, pi);
830 vm_page_test_dirty(p);
835 * If we have been asked to skip nosync pages and this is a
836 * nosync page, skip it. Note that the object flags were
837 * not cleared in this case so we do not have to set them.
839 if ((flags & OBJPC_NOSYNC) != 0 &&
840 (p->oflags & VPO_NOSYNC) != 0)
843 n = vm_object_page_collect_flush(object, p, pagerflags);
844 if (object->generation != curgeneration)
846 np = vm_page_find_least(object, pi + n);
849 VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC) ? MNT_WAIT : 0);
852 vm_object_clear_flag(object, OBJ_CLEANING);
856 vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int pagerflags)
858 vm_page_t ma[vm_pageout_page_count], p_first, tp;
859 int count, i, mreq, runlen;
861 mtx_assert(&vm_page_queue_mtx, MA_NOTOWNED);
862 vm_page_lock_assert(p, MA_NOTOWNED);
863 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
868 for (tp = p; count < vm_pageout_page_count; count++) {
869 tp = vm_page_next(tp);
870 if (tp == NULL || tp->busy != 0 || (tp->oflags & VPO_BUSY) != 0)
872 vm_page_test_dirty(tp);
877 for (p_first = p; count < vm_pageout_page_count; count++) {
878 tp = vm_page_prev(p_first);
879 if (tp == NULL || tp->busy != 0 || (tp->oflags & VPO_BUSY) != 0)
881 vm_page_test_dirty(tp);
888 for (tp = p_first, i = 0; i < count; tp = TAILQ_NEXT(tp, listq), i++)
891 vm_pageout_flush(ma, count, pagerflags, mreq, &runlen);
896 * Note that there is absolutely no sense in writing out
897 * anonymous objects, so we track down the vnode object
899 * We invalidate (remove) all pages from the address space
900 * for semantic correctness.
902 * Note: certain anonymous maps, such as MAP_NOSYNC maps,
903 * may start out with a NULL object.
906 vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size,
907 boolean_t syncio, boolean_t invalidate)
909 vm_object_t backing_object;
916 VM_OBJECT_LOCK(object);
917 while ((backing_object = object->backing_object) != NULL) {
918 VM_OBJECT_LOCK(backing_object);
919 offset += object->backing_object_offset;
920 VM_OBJECT_UNLOCK(object);
921 object = backing_object;
922 if (object->size < OFF_TO_IDX(offset + size))
923 size = IDX_TO_OFF(object->size) - offset;
926 * Flush pages if writing is allowed, invalidate them
927 * if invalidation requested. Pages undergoing I/O
928 * will be ignored by vm_object_page_remove().
930 * We cannot lock the vnode and then wait for paging
931 * to complete without deadlocking against vm_fault.
932 * Instead we simply call vm_object_page_remove() and
933 * allow it to block internally on a page-by-page
934 * basis when it encounters pages undergoing async
937 if (object->type == OBJT_VNODE &&
938 (object->flags & OBJ_MIGHTBEDIRTY) != 0) {
941 VM_OBJECT_UNLOCK(object);
942 (void) vn_start_write(vp, &mp, V_WAIT);
943 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
944 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
945 flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
946 flags |= invalidate ? OBJPC_INVAL : 0;
947 VM_OBJECT_LOCK(object);
948 vm_object_page_clean(object,
950 OFF_TO_IDX(offset + size + PAGE_MASK),
952 VM_OBJECT_UNLOCK(object);
954 VFS_UNLOCK_GIANT(vfslocked);
955 vn_finished_write(mp);
956 VM_OBJECT_LOCK(object);
958 if ((object->type == OBJT_VNODE ||
959 object->type == OBJT_DEVICE) && invalidate) {
961 purge = old_msync || (object->type == OBJT_DEVICE);
962 vm_object_page_remove(object,
964 OFF_TO_IDX(offset + size + PAGE_MASK),
965 purge ? FALSE : TRUE);
967 VM_OBJECT_UNLOCK(object);
973 * Implements the madvise function at the object/page level.
975 * MADV_WILLNEED (any object)
977 * Activate the specified pages if they are resident.
979 * MADV_DONTNEED (any object)
981 * Deactivate the specified pages if they are resident.
983 * MADV_FREE (OBJT_DEFAULT/OBJT_SWAP objects,
984 * OBJ_ONEMAPPING only)
986 * Deactivate and clean the specified pages if they are
987 * resident. This permits the process to reuse the pages
988 * without faulting or the kernel to reclaim the pages
992 vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
994 vm_pindex_t end, tpindex;
995 vm_object_t backing_object, tobject;
1000 VM_OBJECT_LOCK(object);
1001 end = pindex + count;
1003 * Locate and adjust resident pages
1005 for (; pindex < end; pindex += 1) {
1011 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
1012 * and those pages must be OBJ_ONEMAPPING.
1014 if (advise == MADV_FREE) {
1015 if ((tobject->type != OBJT_DEFAULT &&
1016 tobject->type != OBJT_SWAP) ||
1017 (tobject->flags & OBJ_ONEMAPPING) == 0) {
1018 goto unlock_tobject;
1020 } else if (tobject->type == OBJT_PHYS)
1021 goto unlock_tobject;
1022 m = vm_page_lookup(tobject, tpindex);
1023 if (m == NULL && advise == MADV_WILLNEED) {
1025 * If the page is cached, reactivate it.
1027 m = vm_page_alloc(tobject, tpindex, VM_ALLOC_IFCACHED |
1032 * There may be swap even if there is no backing page
1034 if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1035 swap_pager_freespace(tobject, tpindex, 1);
1039 backing_object = tobject->backing_object;
1040 if (backing_object == NULL)
1041 goto unlock_tobject;
1042 VM_OBJECT_LOCK(backing_object);
1043 tpindex += OFF_TO_IDX(tobject->backing_object_offset);
1044 if (tobject != object)
1045 VM_OBJECT_UNLOCK(tobject);
1046 tobject = backing_object;
1048 } else if (m->valid != VM_PAGE_BITS_ALL)
1049 goto unlock_tobject;
1051 * If the page is not in a normal state, skip it.
1054 if (m->hold_count != 0 || m->wire_count != 0) {
1056 goto unlock_tobject;
1058 KASSERT((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0,
1059 ("vm_object_madvise: page %p is not managed", m));
1060 if ((m->oflags & VPO_BUSY) || m->busy) {
1061 if (advise == MADV_WILLNEED) {
1063 * Reference the page before unlocking and
1064 * sleeping so that the page daemon is less
1065 * likely to reclaim it.
1067 vm_page_lock_queues();
1068 vm_page_flag_set(m, PG_REFERENCED);
1069 vm_page_unlock_queues();
1072 if (object != tobject)
1073 VM_OBJECT_UNLOCK(object);
1074 m->oflags |= VPO_WANTED;
1075 msleep(m, VM_OBJECT_MTX(tobject), PDROP | PVM, "madvpo",
1077 VM_OBJECT_LOCK(object);
1080 if (advise == MADV_WILLNEED) {
1081 vm_page_activate(m);
1082 } else if (advise == MADV_DONTNEED) {
1083 vm_page_dontneed(m);
1084 } else if (advise == MADV_FREE) {
1086 * Mark the page clean. This will allow the page
1087 * to be freed up by the system. However, such pages
1088 * are often reused quickly by malloc()/free()
1089 * so we do not do anything that would cause
1090 * a page fault if we can help it.
1092 * Specifically, we do not try to actually free
1093 * the page now nor do we try to put it in the
1094 * cache (which would cause a page fault on reuse).
1096 * But we do make the page is freeable as we
1097 * can without actually taking the step of unmapping
1100 pmap_clear_modify(m);
1103 vm_page_dontneed(m);
1106 if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1107 swap_pager_freespace(tobject, tpindex, 1);
1109 if (tobject != object)
1110 VM_OBJECT_UNLOCK(tobject);
1112 VM_OBJECT_UNLOCK(object);
1118 * Create a new object which is backed by the
1119 * specified existing object range. The source
1120 * object reference is deallocated.
1122 * The new object and offset into that object
1123 * are returned in the source parameters.
1127 vm_object_t *object, /* IN/OUT */
1128 vm_ooffset_t *offset, /* IN/OUT */
1137 * Don't create the new object if the old object isn't shared.
1139 if (source != NULL) {
1140 VM_OBJECT_LOCK(source);
1141 if (source->ref_count == 1 &&
1142 source->handle == NULL &&
1143 (source->type == OBJT_DEFAULT ||
1144 source->type == OBJT_SWAP)) {
1145 VM_OBJECT_UNLOCK(source);
1148 VM_OBJECT_UNLOCK(source);
1152 * Allocate a new object with the given length.
1154 result = vm_object_allocate(OBJT_DEFAULT, length);
1157 * The new object shadows the source object, adding a reference to it.
1158 * Our caller changes his reference to point to the new object,
1159 * removing a reference to the source object. Net result: no change
1160 * of reference count.
1162 * Try to optimize the result object's page color when shadowing
1163 * in order to maintain page coloring consistency in the combined
1166 result->backing_object = source;
1168 * Store the offset into the source object, and fix up the offset into
1171 result->backing_object_offset = *offset;
1172 if (source != NULL) {
1173 VM_OBJECT_LOCK(source);
1174 LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1175 source->shadow_count++;
1176 #if VM_NRESERVLEVEL > 0
1177 result->flags |= source->flags & OBJ_COLORED;
1178 result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) &
1179 ((1 << (VM_NFREEORDER - 1)) - 1);
1181 VM_OBJECT_UNLOCK(source);
1186 * Return the new things
1195 * Split the pages in a map entry into a new object. This affords
1196 * easier removal of unused pages, and keeps object inheritance from
1197 * being a negative impact on memory usage.
1200 vm_object_split(vm_map_entry_t entry)
1202 vm_page_t m, m_next;
1203 vm_object_t orig_object, new_object, source;
1204 vm_pindex_t idx, offidxstart;
1207 orig_object = entry->object.vm_object;
1208 if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1210 if (orig_object->ref_count <= 1)
1212 VM_OBJECT_UNLOCK(orig_object);
1214 offidxstart = OFF_TO_IDX(entry->offset);
1215 size = atop(entry->end - entry->start);
1218 * If swap_pager_copy() is later called, it will convert new_object
1219 * into a swap object.
1221 new_object = vm_object_allocate(OBJT_DEFAULT, size);
1224 * At this point, the new object is still private, so the order in
1225 * which the original and new objects are locked does not matter.
1227 VM_OBJECT_LOCK(new_object);
1228 VM_OBJECT_LOCK(orig_object);
1229 source = orig_object->backing_object;
1230 if (source != NULL) {
1231 VM_OBJECT_LOCK(source);
1232 if ((source->flags & OBJ_DEAD) != 0) {
1233 VM_OBJECT_UNLOCK(source);
1234 VM_OBJECT_UNLOCK(orig_object);
1235 VM_OBJECT_UNLOCK(new_object);
1236 vm_object_deallocate(new_object);
1237 VM_OBJECT_LOCK(orig_object);
1240 LIST_INSERT_HEAD(&source->shadow_head,
1241 new_object, shadow_list);
1242 source->shadow_count++;
1243 vm_object_reference_locked(source); /* for new_object */
1244 vm_object_clear_flag(source, OBJ_ONEMAPPING);
1245 VM_OBJECT_UNLOCK(source);
1246 new_object->backing_object_offset =
1247 orig_object->backing_object_offset + entry->offset;
1248 new_object->backing_object = source;
1250 if (orig_object->cred != NULL) {
1251 new_object->cred = orig_object->cred;
1252 crhold(orig_object->cred);
1253 new_object->charge = ptoa(size);
1254 KASSERT(orig_object->charge >= ptoa(size),
1255 ("orig_object->charge < 0"));
1256 orig_object->charge -= ptoa(size);
1259 m = vm_page_find_least(orig_object, offidxstart);
1260 for (; m != NULL && (idx = m->pindex - offidxstart) < size;
1262 m_next = TAILQ_NEXT(m, listq);
1265 * We must wait for pending I/O to complete before we can
1268 * We do not have to VM_PROT_NONE the page as mappings should
1269 * not be changed by this operation.
1271 if ((m->oflags & VPO_BUSY) || m->busy) {
1272 VM_OBJECT_UNLOCK(new_object);
1273 m->oflags |= VPO_WANTED;
1274 msleep(m, VM_OBJECT_MTX(orig_object), PVM, "spltwt", 0);
1275 VM_OBJECT_LOCK(new_object);
1279 vm_page_rename(m, new_object, idx);
1281 /* page automatically made dirty by rename and cache handled */
1284 if (orig_object->type == OBJT_SWAP) {
1286 * swap_pager_copy() can sleep, in which case the orig_object's
1287 * and new_object's locks are released and reacquired.
1289 swap_pager_copy(orig_object, new_object, offidxstart, 0);
1292 * Transfer any cached pages from orig_object to new_object.
1294 if (__predict_false(orig_object->cache != NULL))
1295 vm_page_cache_transfer(orig_object, offidxstart,
1298 VM_OBJECT_UNLOCK(orig_object);
1299 TAILQ_FOREACH(m, &new_object->memq, listq)
1301 VM_OBJECT_UNLOCK(new_object);
1302 entry->object.vm_object = new_object;
1303 entry->offset = 0LL;
1304 vm_object_deallocate(orig_object);
1305 VM_OBJECT_LOCK(new_object);
1308 #define OBSC_TEST_ALL_SHADOWED 0x0001
1309 #define OBSC_COLLAPSE_NOWAIT 0x0002
1310 #define OBSC_COLLAPSE_WAIT 0x0004
1313 vm_object_backing_scan(vm_object_t object, int op)
1317 vm_object_t backing_object;
1318 vm_pindex_t backing_offset_index;
1320 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1321 VM_OBJECT_LOCK_ASSERT(object->backing_object, MA_OWNED);
1323 backing_object = object->backing_object;
1324 backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
1327 * Initial conditions
1329 if (op & OBSC_TEST_ALL_SHADOWED) {
1331 * We do not want to have to test for the existence of cache
1332 * or swap pages in the backing object. XXX but with the
1333 * new swapper this would be pretty easy to do.
1335 * XXX what about anonymous MAP_SHARED memory that hasn't
1336 * been ZFOD faulted yet? If we do not test for this, the
1337 * shadow test may succeed! XXX
1339 if (backing_object->type != OBJT_DEFAULT) {
1343 if (op & OBSC_COLLAPSE_WAIT) {
1344 vm_object_set_flag(backing_object, OBJ_DEAD);
1350 p = TAILQ_FIRST(&backing_object->memq);
1352 vm_page_t next = TAILQ_NEXT(p, listq);
1353 vm_pindex_t new_pindex = p->pindex - backing_offset_index;
1355 if (op & OBSC_TEST_ALL_SHADOWED) {
1359 * Ignore pages outside the parent object's range
1360 * and outside the parent object's mapping of the
1363 * note that we do not busy the backing object's
1367 p->pindex < backing_offset_index ||
1368 new_pindex >= object->size
1375 * See if the parent has the page or if the parent's
1376 * object pager has the page. If the parent has the
1377 * page but the page is not valid, the parent's
1378 * object pager must have the page.
1380 * If this fails, the parent does not completely shadow
1381 * the object and we might as well give up now.
1384 pp = vm_page_lookup(object, new_pindex);
1386 (pp == NULL || pp->valid == 0) &&
1387 !vm_pager_has_page(object, new_pindex, NULL, NULL)
1395 * Check for busy page
1397 if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
1400 if (op & OBSC_COLLAPSE_NOWAIT) {
1401 if ((p->oflags & VPO_BUSY) ||
1407 } else if (op & OBSC_COLLAPSE_WAIT) {
1408 if ((p->oflags & VPO_BUSY) || p->busy) {
1409 VM_OBJECT_UNLOCK(object);
1410 p->oflags |= VPO_WANTED;
1411 msleep(p, VM_OBJECT_MTX(backing_object),
1412 PDROP | PVM, "vmocol", 0);
1413 VM_OBJECT_LOCK(object);
1414 VM_OBJECT_LOCK(backing_object);
1416 * If we slept, anything could have
1417 * happened. Since the object is
1418 * marked dead, the backing offset
1419 * should not have changed so we
1420 * just restart our scan.
1422 p = TAILQ_FIRST(&backing_object->memq);
1428 p->object == backing_object,
1429 ("vm_object_backing_scan: object mismatch")
1433 * Destroy any associated swap
1435 if (backing_object->type == OBJT_SWAP) {
1436 swap_pager_freespace(
1444 p->pindex < backing_offset_index ||
1445 new_pindex >= object->size
1448 * Page is out of the parent object's range, we
1449 * can simply destroy it.
1452 KASSERT(!pmap_page_is_mapped(p),
1453 ("freeing mapped page %p", p));
1454 if (p->wire_count == 0)
1463 pp = vm_page_lookup(object, new_pindex);
1466 vm_pager_has_page(object, new_pindex, NULL, NULL)
1469 * page already exists in parent OR swap exists
1470 * for this location in the parent. Destroy
1471 * the original page from the backing object.
1473 * Leave the parent's page alone
1476 KASSERT(!pmap_page_is_mapped(p),
1477 ("freeing mapped page %p", p));
1478 if (p->wire_count == 0)
1487 #if VM_NRESERVLEVEL > 0
1489 * Rename the reservation.
1491 vm_reserv_rename(p, object, backing_object,
1492 backing_offset_index);
1496 * Page does not exist in parent, rename the
1497 * page from the backing object to the main object.
1499 * If the page was mapped to a process, it can remain
1500 * mapped through the rename.
1503 vm_page_rename(p, object, new_pindex);
1505 /* page automatically made dirty by rename */
1514 * this version of collapse allows the operation to occur earlier and
1515 * when paging_in_progress is true for an object... This is not a complete
1516 * operation, but should plug 99.9% of the rest of the leaks.
1519 vm_object_qcollapse(vm_object_t object)
1521 vm_object_t backing_object = object->backing_object;
1523 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1524 VM_OBJECT_LOCK_ASSERT(backing_object, MA_OWNED);
1526 if (backing_object->ref_count != 1)
1529 vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
1533 * vm_object_collapse:
1535 * Collapse an object with the object backing it.
1536 * Pages in the backing object are moved into the
1537 * parent, and the backing object is deallocated.
1540 vm_object_collapse(vm_object_t object)
1542 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1545 vm_object_t backing_object;
1548 * Verify that the conditions are right for collapse:
1550 * The object exists and the backing object exists.
1552 if ((backing_object = object->backing_object) == NULL)
1556 * we check the backing object first, because it is most likely
1559 VM_OBJECT_LOCK(backing_object);
1560 if (backing_object->handle != NULL ||
1561 (backing_object->type != OBJT_DEFAULT &&
1562 backing_object->type != OBJT_SWAP) ||
1563 (backing_object->flags & OBJ_DEAD) ||
1564 object->handle != NULL ||
1565 (object->type != OBJT_DEFAULT &&
1566 object->type != OBJT_SWAP) ||
1567 (object->flags & OBJ_DEAD)) {
1568 VM_OBJECT_UNLOCK(backing_object);
1573 object->paging_in_progress != 0 ||
1574 backing_object->paging_in_progress != 0
1576 vm_object_qcollapse(object);
1577 VM_OBJECT_UNLOCK(backing_object);
1581 * We know that we can either collapse the backing object (if
1582 * the parent is the only reference to it) or (perhaps) have
1583 * the parent bypass the object if the parent happens to shadow
1584 * all the resident pages in the entire backing object.
1586 * This is ignoring pager-backed pages such as swap pages.
1587 * vm_object_backing_scan fails the shadowing test in this
1590 if (backing_object->ref_count == 1) {
1592 * If there is exactly one reference to the backing
1593 * object, we can collapse it into the parent.
1595 vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1597 #if VM_NRESERVLEVEL > 0
1599 * Break any reservations from backing_object.
1601 if (__predict_false(!LIST_EMPTY(&backing_object->rvq)))
1602 vm_reserv_break_all(backing_object);
1606 * Move the pager from backing_object to object.
1608 if (backing_object->type == OBJT_SWAP) {
1610 * swap_pager_copy() can sleep, in which case
1611 * the backing_object's and object's locks are
1612 * released and reacquired.
1617 OFF_TO_IDX(object->backing_object_offset), TRUE);
1620 * Free any cached pages from backing_object.
1622 if (__predict_false(backing_object->cache != NULL))
1623 vm_page_cache_free(backing_object, 0, 0);
1626 * Object now shadows whatever backing_object did.
1627 * Note that the reference to
1628 * backing_object->backing_object moves from within
1629 * backing_object to within object.
1631 LIST_REMOVE(object, shadow_list);
1632 backing_object->shadow_count--;
1633 if (backing_object->backing_object) {
1634 VM_OBJECT_LOCK(backing_object->backing_object);
1635 LIST_REMOVE(backing_object, shadow_list);
1637 &backing_object->backing_object->shadow_head,
1638 object, shadow_list);
1640 * The shadow_count has not changed.
1642 VM_OBJECT_UNLOCK(backing_object->backing_object);
1644 object->backing_object = backing_object->backing_object;
1645 object->backing_object_offset +=
1646 backing_object->backing_object_offset;
1649 * Discard backing_object.
1651 * Since the backing object has no pages, no pager left,
1652 * and no object references within it, all that is
1653 * necessary is to dispose of it.
1655 KASSERT(backing_object->ref_count == 1, (
1656 "backing_object %p was somehow re-referenced during collapse!",
1658 VM_OBJECT_UNLOCK(backing_object);
1659 vm_object_destroy(backing_object);
1663 vm_object_t new_backing_object;
1666 * If we do not entirely shadow the backing object,
1667 * there is nothing we can do so we give up.
1669 if (object->resident_page_count != object->size &&
1670 vm_object_backing_scan(object,
1671 OBSC_TEST_ALL_SHADOWED) == 0) {
1672 VM_OBJECT_UNLOCK(backing_object);
1677 * Make the parent shadow the next object in the
1678 * chain. Deallocating backing_object will not remove
1679 * it, since its reference count is at least 2.
1681 LIST_REMOVE(object, shadow_list);
1682 backing_object->shadow_count--;
1684 new_backing_object = backing_object->backing_object;
1685 if ((object->backing_object = new_backing_object) != NULL) {
1686 VM_OBJECT_LOCK(new_backing_object);
1688 &new_backing_object->shadow_head,
1692 new_backing_object->shadow_count++;
1693 vm_object_reference_locked(new_backing_object);
1694 VM_OBJECT_UNLOCK(new_backing_object);
1695 object->backing_object_offset +=
1696 backing_object->backing_object_offset;
1700 * Drop the reference count on backing_object. Since
1701 * its ref_count was at least 2, it will not vanish.
1703 backing_object->ref_count--;
1704 VM_OBJECT_UNLOCK(backing_object);
1709 * Try again with this object's new backing object.
1715 * vm_object_page_remove:
1717 * For the given object, either frees or invalidates each of the
1718 * specified pages. In general, a page is freed. However, if a
1719 * page is wired for any reason other than the existence of a
1720 * managed, wired mapping, then it may be invalidated but not
1721 * removed from the object. Pages are specified by the given
1722 * range ["start", "end") and Boolean "clean_only". As a
1723 * special case, if "end" is zero, then the range extends from
1724 * "start" to the end of the object. If "clean_only" is TRUE,
1725 * then only the non-dirty pages within the specified range are
1728 * In general, this operation should only be performed on objects
1729 * that contain managed pages. There are two exceptions. First,
1730 * it may be performed on the kernel and kmem objects. Second,
1731 * it may be used by msync(..., MS_INVALIDATE) to invalidate
1732 * device-backed pages. In both of these cases, "clean_only"
1735 * The object must be locked.
1738 vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
1739 boolean_t clean_only)
1744 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1745 if (object->resident_page_count == 0)
1749 * Since physically-backed objects do not use managed pages, we can't
1750 * remove pages from the object (we must instead remove the page
1751 * references, and then destroy the object).
1753 KASSERT(object->type != OBJT_PHYS || object == kernel_object ||
1754 object == kmem_object,
1755 ("attempt to remove pages from a physical object"));
1757 vm_object_pip_add(object, 1);
1759 p = vm_page_find_least(object, start);
1762 * Assert: the variable p is either (1) the page with the
1763 * least pindex greater than or equal to the parameter pindex
1767 p != NULL && (p->pindex < end || end == 0);
1769 next = TAILQ_NEXT(p, listq);
1772 * If the page is wired for any reason besides the
1773 * existence of managed, wired mappings, then it cannot
1774 * be freed. For example, fictitious pages, which
1775 * represent device memory, are inherently wired and
1776 * cannot be freed. They can, however, be invalidated
1777 * if "clean_only" is FALSE.
1780 if ((wirings = p->wire_count) != 0 &&
1781 (wirings = pmap_page_wired_mappings(p)) != p->wire_count) {
1782 /* Fictitious pages do not have managed mappings. */
1783 if ((p->flags & PG_FICTITIOUS) == 0)
1785 /* Account for removal of managed, wired mappings. */
1786 p->wire_count -= wirings;
1794 if (vm_page_sleep_if_busy(p, TRUE, "vmopar"))
1796 KASSERT((p->flags & PG_FICTITIOUS) == 0,
1797 ("vm_object_page_remove: page %p is fictitious", p));
1798 if (clean_only && p->valid) {
1799 pmap_remove_write(p);
1806 /* Account for removal of managed, wired mappings. */
1808 p->wire_count -= wirings;
1812 vm_object_pip_wakeup(object);
1814 if (__predict_false(object->cache != NULL))
1815 vm_page_cache_free(object, start, end);
1819 * Populate the specified range of the object with valid pages. Returns
1820 * TRUE if the range is successfully populated and FALSE otherwise.
1822 * Note: This function should be optimized to pass a larger array of
1823 * pages to vm_pager_get_pages() before it is applied to a non-
1824 * OBJT_DEVICE object.
1826 * The object must be locked.
1829 vm_object_populate(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1835 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1836 for (pindex = start; pindex < end; pindex++) {
1837 m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL |
1839 if (m->valid != VM_PAGE_BITS_ALL) {
1841 rv = vm_pager_get_pages(object, ma, 1, 0);
1842 m = vm_page_lookup(object, pindex);
1845 if (rv != VM_PAGER_OK) {
1853 * Keep "m" busy because a subsequent iteration may unlock
1857 if (pindex > start) {
1858 m = vm_page_lookup(object, start);
1859 while (m != NULL && m->pindex < pindex) {
1861 m = TAILQ_NEXT(m, listq);
1864 return (pindex == end);
1868 * Routine: vm_object_coalesce
1869 * Function: Coalesces two objects backing up adjoining
1870 * regions of memory into a single object.
1872 * returns TRUE if objects were combined.
1874 * NOTE: Only works at the moment if the second object is NULL -
1875 * if it's not, which object do we lock first?
1878 * prev_object First object to coalesce
1879 * prev_offset Offset into prev_object
1880 * prev_size Size of reference to prev_object
1881 * next_size Size of reference to the second object
1882 * reserved Indicator that extension region has
1883 * swap accounted for
1886 * The object must *not* be locked.
1889 vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset,
1890 vm_size_t prev_size, vm_size_t next_size, boolean_t reserved)
1892 vm_pindex_t next_pindex;
1894 if (prev_object == NULL)
1896 VM_OBJECT_LOCK(prev_object);
1897 if (prev_object->type != OBJT_DEFAULT &&
1898 prev_object->type != OBJT_SWAP) {
1899 VM_OBJECT_UNLOCK(prev_object);
1904 * Try to collapse the object first
1906 vm_object_collapse(prev_object);
1909 * Can't coalesce if: . more than one reference . paged out . shadows
1910 * another object . has a copy elsewhere (any of which mean that the
1911 * pages not mapped to prev_entry may be in use anyway)
1913 if (prev_object->backing_object != NULL) {
1914 VM_OBJECT_UNLOCK(prev_object);
1918 prev_size >>= PAGE_SHIFT;
1919 next_size >>= PAGE_SHIFT;
1920 next_pindex = OFF_TO_IDX(prev_offset) + prev_size;
1922 if ((prev_object->ref_count > 1) &&
1923 (prev_object->size != next_pindex)) {
1924 VM_OBJECT_UNLOCK(prev_object);
1929 * Account for the charge.
1931 if (prev_object->cred != NULL) {
1934 * If prev_object was charged, then this mapping,
1935 * althought not charged now, may become writable
1936 * later. Non-NULL cred in the object would prevent
1937 * swap reservation during enabling of the write
1938 * access, so reserve swap now. Failed reservation
1939 * cause allocation of the separate object for the map
1940 * entry, and swap reservation for this entry is
1941 * managed in appropriate time.
1943 if (!reserved && !swap_reserve_by_cred(ptoa(next_size),
1944 prev_object->cred)) {
1947 prev_object->charge += ptoa(next_size);
1951 * Remove any pages that may still be in the object from a previous
1954 if (next_pindex < prev_object->size) {
1955 vm_object_page_remove(prev_object,
1957 next_pindex + next_size, FALSE);
1958 if (prev_object->type == OBJT_SWAP)
1959 swap_pager_freespace(prev_object,
1960 next_pindex, next_size);
1962 if (prev_object->cred != NULL) {
1963 KASSERT(prev_object->charge >=
1964 ptoa(prev_object->size - next_pindex),
1965 ("object %p overcharged 1 %jx %jx", prev_object,
1966 (uintmax_t)next_pindex, (uintmax_t)next_size));
1967 prev_object->charge -= ptoa(prev_object->size -
1974 * Extend the object if necessary.
1976 if (next_pindex + next_size > prev_object->size)
1977 prev_object->size = next_pindex + next_size;
1979 VM_OBJECT_UNLOCK(prev_object);
1984 vm_object_set_writeable_dirty(vm_object_t object)
1987 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1988 if (object->type != OBJT_VNODE ||
1989 (object->flags & OBJ_MIGHTBEDIRTY) != 0)
1991 vm_object_set_flag(object, OBJ_MIGHTBEDIRTY);
1994 #include "opt_ddb.h"
1996 #include <sys/kernel.h>
1998 #include <sys/cons.h>
2000 #include <ddb/ddb.h>
2003 _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
2006 vm_map_entry_t tmpe;
2014 tmpe = map->header.next;
2015 entcount = map->nentries;
2016 while (entcount-- && (tmpe != &map->header)) {
2017 if (_vm_object_in_map(map, object, tmpe)) {
2022 } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
2023 tmpm = entry->object.sub_map;
2024 tmpe = tmpm->header.next;
2025 entcount = tmpm->nentries;
2026 while (entcount-- && tmpe != &tmpm->header) {
2027 if (_vm_object_in_map(tmpm, object, tmpe)) {
2032 } else if ((obj = entry->object.vm_object) != NULL) {
2033 for (; obj; obj = obj->backing_object)
2034 if (obj == object) {
2042 vm_object_in_map(vm_object_t object)
2046 /* sx_slock(&allproc_lock); */
2047 FOREACH_PROC_IN_SYSTEM(p) {
2048 if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
2050 if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
2051 /* sx_sunlock(&allproc_lock); */
2055 /* sx_sunlock(&allproc_lock); */
2056 if (_vm_object_in_map(kernel_map, object, 0))
2058 if (_vm_object_in_map(kmem_map, object, 0))
2060 if (_vm_object_in_map(pager_map, object, 0))
2062 if (_vm_object_in_map(buffer_map, object, 0))
2067 DB_SHOW_COMMAND(vmochk, vm_object_check)
2072 * make sure that internal objs are in a map somewhere
2073 * and none have zero ref counts.
2075 TAILQ_FOREACH(object, &vm_object_list, object_list) {
2076 if (object->handle == NULL &&
2077 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2078 if (object->ref_count == 0) {
2079 db_printf("vmochk: internal obj has zero ref count: %ld\n",
2080 (long)object->size);
2082 if (!vm_object_in_map(object)) {
2084 "vmochk: internal obj is not in a map: "
2085 "ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2086 object->ref_count, (u_long)object->size,
2087 (u_long)object->size,
2088 (void *)object->backing_object);
2095 * vm_object_print: [ debug ]
2097 DB_SHOW_COMMAND(object, vm_object_print_static)
2099 /* XXX convert args. */
2100 vm_object_t object = (vm_object_t)addr;
2101 boolean_t full = have_addr;
2105 /* XXX count is an (unused) arg. Avoid shadowing it. */
2106 #define count was_count
2114 "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x ruid %d charge %jx\n",
2115 object, (int)object->type, (uintmax_t)object->size,
2116 object->resident_page_count, object->ref_count, object->flags,
2117 object->cred ? object->cred->cr_ruid : -1, (uintmax_t)object->charge);
2118 db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n",
2119 object->shadow_count,
2120 object->backing_object ? object->backing_object->ref_count : 0,
2121 object->backing_object, (uintmax_t)object->backing_object_offset);
2128 TAILQ_FOREACH(p, &object->memq, listq) {
2130 db_iprintf("memory:=");
2131 else if (count == 6) {
2139 db_printf("(off=0x%jx,page=0x%jx)",
2140 (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p));
2150 /* XXX need this non-static entry for calling from vm_map_print. */
2153 /* db_expr_t */ long addr,
2154 boolean_t have_addr,
2155 /* db_expr_t */ long count,
2158 vm_object_print_static(addr, have_addr, count, modif);
2161 DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
2166 vm_page_t m, prev_m;
2170 TAILQ_FOREACH(object, &vm_object_list, object_list) {
2171 db_printf("new object: %p\n", (void *)object);
2182 TAILQ_FOREACH(m, &object->memq, listq) {
2183 if (m->pindex > 128)
2185 if ((prev_m = TAILQ_PREV(m, pglist, listq)) != NULL &&
2186 prev_m->pindex + 1 != m->pindex) {
2188 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2189 (long)fidx, rcount, (long)pa);
2201 (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
2206 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2207 (long)fidx, rcount, (long)pa);
2217 pa = VM_PAGE_TO_PHYS(m);
2221 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2222 (long)fidx, rcount, (long)pa);