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,
104 int pagerflags, int flags, boolean_t *clearobjflags,
106 static boolean_t vm_object_page_remove_write(vm_page_t p, int flags,
107 boolean_t *clearobjflags);
108 static void vm_object_qcollapse(vm_object_t object);
109 static void vm_object_vndeallocate(vm_object_t object);
112 * Virtual memory objects maintain the actual data
113 * associated with allocated virtual memory. A given
114 * page of memory exists within exactly one object.
116 * An object is only deallocated when all "references"
117 * are given up. Only one "reference" to a given
118 * region of an object should be writeable.
120 * Associated with each object is a list of all resident
121 * memory pages belonging to that object; this list is
122 * maintained by the "vm_page" module, and locked by the object's
125 * Each object also records a "pager" routine which is
126 * used to retrieve (and store) pages to the proper backing
127 * storage. In addition, objects may be backed by other
128 * objects from which they were virtual-copied.
130 * The only items within the object structure which are
131 * modified after time of creation are:
132 * reference count locked by object's lock
133 * pager routine locked by object's lock
137 struct object_q vm_object_list;
138 struct mtx vm_object_list_mtx; /* lock for object list and count */
140 struct vm_object kernel_object_store;
141 struct vm_object kmem_object_store;
143 static SYSCTL_NODE(_vm_stats, OID_AUTO, object, CTLFLAG_RD, 0,
146 static long object_collapses;
147 SYSCTL_LONG(_vm_stats_object, OID_AUTO, collapses, CTLFLAG_RD,
148 &object_collapses, 0, "VM object collapses");
150 static long object_bypasses;
151 SYSCTL_LONG(_vm_stats_object, OID_AUTO, bypasses, CTLFLAG_RD,
152 &object_bypasses, 0, "VM object bypasses");
154 static uma_zone_t obj_zone;
156 static int vm_object_zinit(void *mem, int size, int flags);
159 static void vm_object_zdtor(void *mem, int size, void *arg);
162 vm_object_zdtor(void *mem, int size, void *arg)
166 object = (vm_object_t)mem;
167 KASSERT(object->resident_page_count == 0,
168 ("object %p resident_page_count = %d",
169 object, object->resident_page_count));
170 KASSERT(TAILQ_EMPTY(&object->memq),
171 ("object %p has resident pages",
173 #if VM_NRESERVLEVEL > 0
174 KASSERT(LIST_EMPTY(&object->rvq),
175 ("object %p has reservations",
178 KASSERT(object->cached_page_count == 0,
179 ("object %p has cached pages",
181 KASSERT(object->paging_in_progress == 0,
182 ("object %p paging_in_progress = %d",
183 object, object->paging_in_progress));
184 KASSERT(object->shadow_count == 0,
185 ("object %p shadow_count = %d",
186 object, object->shadow_count));
191 vm_object_zinit(void *mem, int size, int flags)
195 object = (vm_object_t)mem;
196 bzero(&object->mtx, sizeof(object->mtx));
197 mtx_init(&object->mtx, "vm object", NULL, MTX_DEF | MTX_DUPOK);
199 /* These are true for any object that has been freed */
200 object->paging_in_progress = 0;
201 object->resident_page_count = 0;
202 object->shadow_count = 0;
207 _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
210 TAILQ_INIT(&object->memq);
211 LIST_INIT(&object->shadow_head);
213 object->rtree.rt_root = 0;
217 panic("_vm_object_allocate: can't create OBJT_DEAD");
220 object->flags = OBJ_ONEMAPPING;
224 object->flags = OBJ_FICTITIOUS | OBJ_UNMANAGED;
227 object->flags = OBJ_FICTITIOUS;
230 object->flags = OBJ_UNMANAGED;
236 panic("_vm_object_allocate: type %d is undefined", type);
239 object->generation = 1;
240 object->ref_count = 1;
241 object->memattr = VM_MEMATTR_DEFAULT;
244 object->handle = NULL;
245 object->backing_object = NULL;
246 object->backing_object_offset = (vm_ooffset_t) 0;
247 #if VM_NRESERVLEVEL > 0
248 LIST_INIT(&object->rvq);
251 mtx_lock(&vm_object_list_mtx);
252 TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
253 mtx_unlock(&vm_object_list_mtx);
259 * Initialize the VM objects module.
264 TAILQ_INIT(&vm_object_list);
265 mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF);
267 mtx_init(&kernel_object->mtx, "vm object", "kernel object", MTX_DEF);
268 _vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
270 #if VM_NRESERVLEVEL > 0
271 kernel_object->flags |= OBJ_COLORED;
272 kernel_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS);
275 mtx_init(&kmem_object->mtx, "vm object", "kmem object", MTX_DEF);
276 _vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
278 #if VM_NRESERVLEVEL > 0
279 kmem_object->flags |= OBJ_COLORED;
280 kmem_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS);
284 * The lock portion of struct vm_object must be type stable due
285 * to vm_pageout_fallback_object_lock locking a vm object
286 * without holding any references to it.
288 obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL,
294 vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM|UMA_ZONE_NOFREE);
298 vm_object_clear_flag(vm_object_t object, u_short bits)
301 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
302 object->flags &= ~bits;
306 * Sets the default memory attribute for the specified object. Pages
307 * that are allocated to this object are by default assigned this memory
310 * Presently, this function must be called before any pages are allocated
311 * to the object. In the future, this requirement may be relaxed for
312 * "default" and "swap" objects.
315 vm_object_set_memattr(vm_object_t object, vm_memattr_t memattr)
318 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
319 switch (object->type) {
327 if (object->resident_page_count == 0)
328 return (KERN_FAILURE);
331 return (KERN_INVALID_ARGUMENT);
333 panic("vm_object_set_memattr: object %p is of undefined type",
336 object->memattr = memattr;
337 return (KERN_SUCCESS);
341 vm_object_pip_add(vm_object_t object, short i)
344 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
345 object->paging_in_progress += i;
349 vm_object_pip_subtract(vm_object_t object, short i)
352 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
353 object->paging_in_progress -= i;
357 vm_object_pip_wakeup(vm_object_t object)
360 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
361 object->paging_in_progress--;
362 if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
363 vm_object_clear_flag(object, OBJ_PIPWNT);
369 vm_object_pip_wakeupn(vm_object_t object, short i)
372 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
374 object->paging_in_progress -= i;
375 if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
376 vm_object_clear_flag(object, OBJ_PIPWNT);
382 vm_object_pip_wait(vm_object_t object, char *waitid)
385 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
386 while (object->paging_in_progress) {
387 object->flags |= OBJ_PIPWNT;
388 VM_OBJECT_SLEEP(object, object, PVM, waitid, 0);
393 * vm_object_allocate:
395 * Returns a new object with the given size.
398 vm_object_allocate(objtype_t type, vm_pindex_t size)
402 object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK);
403 _vm_object_allocate(type, size, object);
409 * vm_object_reference:
411 * Gets another reference to the given object. Note: OBJ_DEAD
412 * objects can be referenced during final cleaning.
415 vm_object_reference(vm_object_t object)
419 VM_OBJECT_LOCK(object);
420 vm_object_reference_locked(object);
421 VM_OBJECT_UNLOCK(object);
425 * vm_object_reference_locked:
427 * Gets another reference to the given object.
429 * The object must be locked.
432 vm_object_reference_locked(vm_object_t object)
436 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
438 if (object->type == OBJT_VNODE) {
445 * Handle deallocating an object of type OBJT_VNODE.
448 vm_object_vndeallocate(vm_object_t object)
450 struct vnode *vp = (struct vnode *) object->handle;
452 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
453 KASSERT(object->type == OBJT_VNODE,
454 ("vm_object_vndeallocate: not a vnode object"));
455 KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
457 if (object->ref_count == 0) {
458 vprint("vm_object_vndeallocate", vp);
459 panic("vm_object_vndeallocate: bad object reference count");
463 if (object->ref_count > 1) {
465 VM_OBJECT_UNLOCK(object);
466 /* vrele may need the vnode lock. */
470 VM_OBJECT_UNLOCK(object);
471 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
473 VM_OBJECT_LOCK(object);
475 if (object->type == OBJT_DEAD) {
476 VM_OBJECT_UNLOCK(object);
479 if (object->ref_count == 0)
481 VM_OBJECT_UNLOCK(object);
488 * vm_object_deallocate:
490 * Release a reference to the specified object,
491 * gained either through a vm_object_allocate
492 * or a vm_object_reference call. When all references
493 * are gone, storage associated with this object
494 * may be relinquished.
496 * No object may be locked.
499 vm_object_deallocate(vm_object_t object)
503 while (object != NULL) {
504 VM_OBJECT_LOCK(object);
505 if (object->type == OBJT_VNODE) {
506 vm_object_vndeallocate(object);
510 KASSERT(object->ref_count != 0,
511 ("vm_object_deallocate: object deallocated too many times: %d", object->type));
514 * If the reference count goes to 0 we start calling
515 * vm_object_terminate() on the object chain.
516 * A ref count of 1 may be a special case depending on the
517 * shadow count being 0 or 1.
520 if (object->ref_count > 1) {
521 VM_OBJECT_UNLOCK(object);
523 } else if (object->ref_count == 1) {
524 if (object->shadow_count == 0 &&
525 object->handle == NULL &&
526 (object->type == OBJT_DEFAULT ||
527 object->type == OBJT_SWAP)) {
528 vm_object_set_flag(object, OBJ_ONEMAPPING);
529 } else if ((object->shadow_count == 1) &&
530 (object->handle == NULL) &&
531 (object->type == OBJT_DEFAULT ||
532 object->type == OBJT_SWAP)) {
535 robject = LIST_FIRST(&object->shadow_head);
536 KASSERT(robject != NULL,
537 ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
539 object->shadow_count));
540 if (!VM_OBJECT_TRYLOCK(robject)) {
542 * Avoid a potential deadlock.
545 VM_OBJECT_UNLOCK(object);
547 * More likely than not the thread
548 * holding robject's lock has lower
549 * priority than the current thread.
550 * Let the lower priority thread run.
556 * Collapse object into its shadow unless its
557 * shadow is dead. In that case, object will
558 * be deallocated by the thread that is
559 * deallocating its shadow.
561 if ((robject->flags & OBJ_DEAD) == 0 &&
562 (robject->handle == NULL) &&
563 (robject->type == OBJT_DEFAULT ||
564 robject->type == OBJT_SWAP)) {
566 robject->ref_count++;
568 if (robject->paging_in_progress) {
569 VM_OBJECT_UNLOCK(object);
570 vm_object_pip_wait(robject,
572 temp = robject->backing_object;
573 if (object == temp) {
574 VM_OBJECT_LOCK(object);
577 } else if (object->paging_in_progress) {
578 VM_OBJECT_UNLOCK(robject);
579 object->flags |= OBJ_PIPWNT;
580 VM_OBJECT_SLEEP(object, object,
581 PDROP | PVM, "objde2", 0);
582 VM_OBJECT_LOCK(robject);
583 temp = robject->backing_object;
584 if (object == temp) {
585 VM_OBJECT_LOCK(object);
589 VM_OBJECT_UNLOCK(object);
591 if (robject->ref_count == 1) {
592 robject->ref_count--;
597 vm_object_collapse(object);
598 VM_OBJECT_UNLOCK(object);
601 VM_OBJECT_UNLOCK(robject);
603 VM_OBJECT_UNLOCK(object);
607 temp = object->backing_object;
609 VM_OBJECT_LOCK(temp);
610 LIST_REMOVE(object, shadow_list);
611 temp->shadow_count--;
612 VM_OBJECT_UNLOCK(temp);
613 object->backing_object = NULL;
616 * Don't double-terminate, we could be in a termination
617 * recursion due to the terminate having to sync data
620 if ((object->flags & OBJ_DEAD) == 0)
621 vm_object_terminate(object);
623 VM_OBJECT_UNLOCK(object);
629 * vm_object_destroy removes the object from the global object list
630 * and frees the space for the object.
633 vm_object_destroy(vm_object_t object)
637 * Remove the object from the global object list.
639 mtx_lock(&vm_object_list_mtx);
640 TAILQ_REMOVE(&vm_object_list, object, object_list);
641 mtx_unlock(&vm_object_list_mtx);
644 * Release the allocation charge.
646 if (object->cred != NULL) {
647 KASSERT(object->type == OBJT_DEFAULT ||
648 object->type == OBJT_SWAP,
649 ("vm_object_terminate: non-swap obj %p has cred",
651 swap_release_by_cred(object->charge, object->cred);
653 crfree(object->cred);
658 * Free the space for the object.
660 uma_zfree(obj_zone, object);
664 * vm_object_terminate actually destroys the specified object, freeing
665 * up all previously used resources.
667 * The object must be locked.
668 * This routine may block.
671 vm_object_terminate(vm_object_t object)
673 vm_page_t pa[VM_RADIX_STACK];
679 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
682 * Make sure no one uses us.
684 vm_object_set_flag(object, OBJ_DEAD);
687 * wait for the pageout daemon to be done with the object
689 vm_object_pip_wait(object, "objtrm");
691 KASSERT(!object->paging_in_progress,
692 ("vm_object_terminate: pageout in progress"));
695 * Clean and free the pages, as appropriate. All references to the
696 * object are gone, so we don't need to lock it.
698 if (object->type == OBJT_VNODE) {
699 struct vnode *vp = (struct vnode *)object->handle;
702 * Clean pages and flush buffers.
704 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
705 VM_OBJECT_UNLOCK(object);
707 vinvalbuf(vp, V_SAVE, 0, 0);
709 VM_OBJECT_LOCK(object);
712 KASSERT(object->ref_count == 0,
713 ("vm_object_terminate: object with references, ref_count=%d",
717 * Free any remaining pageable pages. This also removes them from the
718 * paging queues. However, don't free wired pages, just remove them
719 * from the object. Rather than incrementally removing each page from
720 * the object, the page and object are reset to any empty state.
724 while (exhausted == 0 && (n = vm_radix_lookupn(&object->rtree, start,
725 0, VM_RADIX_ANY, (void **)pa, VM_RADIX_STACK, &start,
727 for (i = 0; i < n; i++) {
730 * Another thread may allocate this cached page from
731 * the queue before we acquire the page queue free
734 if (p->flags & PG_CACHED) {
735 mtx_lock(&vm_page_queue_free_mtx);
736 if (p->object == object) {
739 /* Clear PG_CACHED and set PG_FREE. */
740 p->flags ^= PG_CACHED | PG_FREE;
744 mtx_unlock(&vm_page_queue_free_mtx);
746 } else if (p->object != object)
748 KASSERT(!p->busy && (p->oflags & VPO_BUSY) == 0,
749 ("vm_object_terminate: freeing busy page %p", p));
752 * Optimize the page's removal from the object by
753 * resetting its "object" field. Specifically, if
754 * the page is not wired, then the effect of this
755 * assignment is that vm_page_free()'s call to
756 * vm_page_remove() will return immediately without
757 * modifying the page or the object.
758 * Anyway, the radix tree cannot be accessed anymore
759 * from within the object, thus all the nodes need
760 * to be reclaimed later on.
763 if (p->wire_count == 0) {
765 PCPU_INC(cnt.v_pfree);
769 if (n < VM_RADIX_STACK)
772 vm_radix_reclaim_allnodes(&object->rtree);
774 * If the object contained any pages, then reset it to an empty state.
775 * None of the object's fields, including "resident_page_count", were
776 * modified by the preceding loop.
778 if (object->resident_page_count != 0) {
779 TAILQ_INIT(&object->memq);
780 object->resident_page_count = 0;
781 if (object->type == OBJT_VNODE)
782 vdrop(object->handle);
784 if (object->cached_page_count != 0) {
785 object->cached_page_count = 0;
786 if (object->type == OBJT_VNODE)
787 vdrop(object->handle);
790 #if VM_NRESERVLEVEL > 0
791 if (__predict_false(!LIST_EMPTY(&object->rvq)))
792 vm_reserv_break_all(object);
796 * Let the pager know object is dead.
798 vm_pager_deallocate(object);
799 VM_OBJECT_UNLOCK(object);
801 vm_object_destroy(object);
805 * Make the page read-only so that we can clear the object flags. However, if
806 * this is a nosync mmap then the object is likely to stay dirty so do not
807 * mess with the page and do not clear the object flags. Returns TRUE if the
808 * page should be flushed, and FALSE otherwise.
811 vm_object_page_remove_write(vm_page_t p, int flags, boolean_t *clearobjflags)
815 * If we have been asked to skip nosync pages and this is a
816 * nosync page, skip it. Note that the object flags were not
817 * cleared in this case so we do not have to set them.
819 if ((flags & OBJPC_NOSYNC) != 0 && (p->oflags & VPO_NOSYNC) != 0) {
820 *clearobjflags = FALSE;
823 pmap_remove_write(p);
824 return (p->dirty != 0);
829 * vm_object_page_clean
831 * Clean all dirty pages in the specified range of object. Leaves page
832 * on whatever queue it is currently on. If NOSYNC is set then do not
833 * write out pages with VPO_NOSYNC set (originally comes from MAP_NOSYNC),
834 * leaving the object dirty.
836 * When stuffing pages asynchronously, allow clustering. XXX we need a
837 * synchronous clustering mode implementation.
839 * Odd semantics: if start == end, we clean everything.
841 * The object must be locked.
843 * Returns FALSE if some page from the range was not written, as
844 * reported by the pager, and TRUE otherwise.
847 vm_object_page_clean(vm_object_t object, vm_ooffset_t start, vm_ooffset_t end,
851 vm_pindex_t pi, tend, tstart;
852 int curgeneration, n, pagerflags;
853 boolean_t clearobjflags, eio, res;
855 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
856 KASSERT(object->type == OBJT_VNODE, ("Not a vnode object"));
857 if ((object->flags & OBJ_MIGHTBEDIRTY) == 0 ||
858 object->resident_page_count == 0)
861 pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) != 0 ?
862 VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
863 pagerflags |= (flags & OBJPC_INVAL) != 0 ? VM_PAGER_PUT_INVAL : 0;
865 tstart = OFF_TO_IDX(start);
866 tend = (end == 0) ? object->size : OFF_TO_IDX(end + PAGE_MASK);
867 clearobjflags = tstart == 0 && tend >= object->size;
871 curgeneration = object->generation;
873 for (p = vm_page_find_least(object, tstart); p != NULL; p = np) {
877 np = TAILQ_NEXT(p, listq);
880 if (vm_page_sleep_if_busy(p, TRUE, "vpcwai")) {
881 if (object->generation != curgeneration) {
882 if ((flags & OBJPC_SYNC) != 0)
885 clearobjflags = FALSE;
887 np = vm_page_find_least(object, pi);
890 if (!vm_object_page_remove_write(p, flags, &clearobjflags))
893 n = vm_object_page_collect_flush(object, p, pagerflags,
894 flags, &clearobjflags, &eio);
897 clearobjflags = FALSE;
899 if (object->generation != curgeneration) {
900 if ((flags & OBJPC_SYNC) != 0)
903 clearobjflags = FALSE;
907 * If the VOP_PUTPAGES() did a truncated write, so
908 * that even the first page of the run is not fully
909 * written, vm_pageout_flush() returns 0 as the run
910 * length. Since the condition that caused truncated
911 * write may be permanent, e.g. exhausted free space,
912 * accepting n == 0 would cause an infinite loop.
914 * Forwarding the iterator leaves the unwritten page
915 * behind, but there is not much we can do there if
916 * filesystem refuses to write it.
920 clearobjflags = FALSE;
922 np = vm_page_find_least(object, pi + n);
925 VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC) ? MNT_WAIT : 0);
929 vm_object_clear_flag(object, OBJ_MIGHTBEDIRTY);
934 vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int pagerflags,
935 int flags, boolean_t *clearobjflags, boolean_t *eio)
937 vm_page_t ma[vm_pageout_page_count], p_first, tp;
938 int count, i, mreq, runlen;
940 vm_page_lock_assert(p, MA_NOTOWNED);
941 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
946 for (tp = p; count < vm_pageout_page_count; count++) {
947 tp = vm_page_next(tp);
948 if (tp == NULL || tp->busy != 0 || (tp->oflags & VPO_BUSY) != 0)
950 if (!vm_object_page_remove_write(tp, flags, clearobjflags))
954 for (p_first = p; count < vm_pageout_page_count; count++) {
955 tp = vm_page_prev(p_first);
956 if (tp == NULL || tp->busy != 0 || (tp->oflags & VPO_BUSY) != 0)
958 if (!vm_object_page_remove_write(tp, flags, clearobjflags))
964 for (tp = p_first, i = 0; i < count; tp = TAILQ_NEXT(tp, listq), i++)
967 vm_pageout_flush(ma, count, pagerflags, mreq, &runlen, eio);
972 * Note that there is absolutely no sense in writing out
973 * anonymous objects, so we track down the vnode object
975 * We invalidate (remove) all pages from the address space
976 * for semantic correctness.
978 * If the backing object is a device object with unmanaged pages, then any
979 * mappings to the specified range of pages must be removed before this
980 * function is called.
982 * Note: certain anonymous maps, such as MAP_NOSYNC maps,
983 * may start out with a NULL object.
986 vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size,
987 boolean_t syncio, boolean_t invalidate)
989 vm_object_t backing_object;
992 int error, flags, fsync_after;
999 VM_OBJECT_LOCK(object);
1000 while ((backing_object = object->backing_object) != NULL) {
1001 VM_OBJECT_LOCK(backing_object);
1002 offset += object->backing_object_offset;
1003 VM_OBJECT_UNLOCK(object);
1004 object = backing_object;
1005 if (object->size < OFF_TO_IDX(offset + size))
1006 size = IDX_TO_OFF(object->size) - offset;
1009 * Flush pages if writing is allowed, invalidate them
1010 * if invalidation requested. Pages undergoing I/O
1011 * will be ignored by vm_object_page_remove().
1013 * We cannot lock the vnode and then wait for paging
1014 * to complete without deadlocking against vm_fault.
1015 * Instead we simply call vm_object_page_remove() and
1016 * allow it to block internally on a page-by-page
1017 * basis when it encounters pages undergoing async
1020 if (object->type == OBJT_VNODE &&
1021 (object->flags & OBJ_MIGHTBEDIRTY) != 0) {
1022 vp = object->handle;
1023 VM_OBJECT_UNLOCK(object);
1024 (void) vn_start_write(vp, &mp, V_WAIT);
1025 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1026 if (syncio && !invalidate && offset == 0 &&
1027 OFF_TO_IDX(size) == object->size) {
1029 * If syncing the whole mapping of the file,
1030 * it is faster to schedule all the writes in
1031 * async mode, also allowing the clustering,
1032 * and then wait for i/o to complete.
1037 flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
1038 flags |= invalidate ? (OBJPC_SYNC | OBJPC_INVAL) : 0;
1039 fsync_after = FALSE;
1041 VM_OBJECT_LOCK(object);
1042 res = vm_object_page_clean(object, offset, offset + size,
1044 VM_OBJECT_UNLOCK(object);
1046 error = VOP_FSYNC(vp, MNT_WAIT, curthread);
1048 vn_finished_write(mp);
1051 VM_OBJECT_LOCK(object);
1053 if ((object->type == OBJT_VNODE ||
1054 object->type == OBJT_DEVICE) && invalidate) {
1055 if (object->type == OBJT_DEVICE)
1057 * The option OBJPR_NOTMAPPED must be passed here
1058 * because vm_object_page_remove() cannot remove
1059 * unmanaged mappings.
1061 flags = OBJPR_NOTMAPPED;
1065 flags = OBJPR_CLEANONLY;
1066 vm_object_page_remove(object, OFF_TO_IDX(offset),
1067 OFF_TO_IDX(offset + size + PAGE_MASK), flags);
1069 VM_OBJECT_UNLOCK(object);
1074 * vm_object_madvise:
1076 * Implements the madvise function at the object/page level.
1078 * MADV_WILLNEED (any object)
1080 * Activate the specified pages if they are resident.
1082 * MADV_DONTNEED (any object)
1084 * Deactivate the specified pages if they are resident.
1086 * MADV_FREE (OBJT_DEFAULT/OBJT_SWAP objects,
1087 * OBJ_ONEMAPPING only)
1089 * Deactivate and clean the specified pages if they are
1090 * resident. This permits the process to reuse the pages
1091 * without faulting or the kernel to reclaim the pages
1095 vm_object_madvise(vm_object_t object, vm_pindex_t pindex, vm_pindex_t end,
1098 vm_pindex_t tpindex;
1099 vm_object_t backing_object, tobject;
1104 VM_OBJECT_LOCK(object);
1106 * Locate and adjust resident pages
1108 for (; pindex < end; pindex += 1) {
1114 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
1115 * and those pages must be OBJ_ONEMAPPING.
1117 if (advise == MADV_FREE) {
1118 if ((tobject->type != OBJT_DEFAULT &&
1119 tobject->type != OBJT_SWAP) ||
1120 (tobject->flags & OBJ_ONEMAPPING) == 0) {
1121 goto unlock_tobject;
1123 } else if ((tobject->flags & OBJ_UNMANAGED) != 0)
1124 goto unlock_tobject;
1125 m = vm_page_lookup(tobject, tpindex);
1126 if (m == NULL && advise == MADV_WILLNEED) {
1128 * If the page is cached, reactivate it.
1130 m = vm_page_alloc(tobject, tpindex, VM_ALLOC_IFCACHED |
1135 * There may be swap even if there is no backing page
1137 if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1138 swap_pager_freespace(tobject, tpindex, 1);
1142 backing_object = tobject->backing_object;
1143 if (backing_object == NULL)
1144 goto unlock_tobject;
1145 VM_OBJECT_LOCK(backing_object);
1146 tpindex += OFF_TO_IDX(tobject->backing_object_offset);
1147 if (tobject != object)
1148 VM_OBJECT_UNLOCK(tobject);
1149 tobject = backing_object;
1151 } else if (m->valid != VM_PAGE_BITS_ALL)
1152 goto unlock_tobject;
1154 * If the page is not in a normal state, skip it.
1157 if (m->hold_count != 0 || m->wire_count != 0) {
1159 goto unlock_tobject;
1161 KASSERT((m->flags & PG_FICTITIOUS) == 0,
1162 ("vm_object_madvise: page %p is fictitious", m));
1163 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1164 ("vm_object_madvise: page %p is not managed", m));
1165 if ((m->oflags & VPO_BUSY) || m->busy) {
1166 if (advise == MADV_WILLNEED) {
1168 * Reference the page before unlocking and
1169 * sleeping so that the page daemon is less
1170 * likely to reclaim it.
1172 vm_page_aflag_set(m, PGA_REFERENCED);
1175 if (object != tobject)
1176 VM_OBJECT_UNLOCK(object);
1177 m->oflags |= VPO_WANTED;
1178 VM_OBJECT_SLEEP(tobject, m, PDROP | PVM, "madvpo", 0);
1179 VM_OBJECT_LOCK(object);
1182 if (advise == MADV_WILLNEED) {
1183 vm_page_activate(m);
1184 } else if (advise == MADV_DONTNEED) {
1185 vm_page_dontneed(m);
1186 } else if (advise == MADV_FREE) {
1188 * Mark the page clean. This will allow the page
1189 * to be freed up by the system. However, such pages
1190 * are often reused quickly by malloc()/free()
1191 * so we do not do anything that would cause
1192 * a page fault if we can help it.
1194 * Specifically, we do not try to actually free
1195 * the page now nor do we try to put it in the
1196 * cache (which would cause a page fault on reuse).
1198 * But we do make the page is freeable as we
1199 * can without actually taking the step of unmapping
1202 pmap_clear_modify(m);
1205 vm_page_dontneed(m);
1208 if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1209 swap_pager_freespace(tobject, tpindex, 1);
1211 if (tobject != object)
1212 VM_OBJECT_UNLOCK(tobject);
1214 VM_OBJECT_UNLOCK(object);
1220 * Create a new object which is backed by the
1221 * specified existing object range. The source
1222 * object reference is deallocated.
1224 * The new object and offset into that object
1225 * are returned in the source parameters.
1229 vm_object_t *object, /* IN/OUT */
1230 vm_ooffset_t *offset, /* IN/OUT */
1239 * Don't create the new object if the old object isn't shared.
1241 if (source != NULL) {
1242 VM_OBJECT_LOCK(source);
1243 if (source->ref_count == 1 &&
1244 source->handle == NULL &&
1245 (source->type == OBJT_DEFAULT ||
1246 source->type == OBJT_SWAP)) {
1247 VM_OBJECT_UNLOCK(source);
1250 VM_OBJECT_UNLOCK(source);
1254 * Allocate a new object with the given length.
1256 result = vm_object_allocate(OBJT_DEFAULT, atop(length));
1259 * The new object shadows the source object, adding a reference to it.
1260 * Our caller changes his reference to point to the new object,
1261 * removing a reference to the source object. Net result: no change
1262 * of reference count.
1264 * Try to optimize the result object's page color when shadowing
1265 * in order to maintain page coloring consistency in the combined
1268 result->backing_object = source;
1270 * Store the offset into the source object, and fix up the offset into
1273 result->backing_object_offset = *offset;
1274 if (source != NULL) {
1275 VM_OBJECT_LOCK(source);
1276 LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1277 source->shadow_count++;
1278 #if VM_NRESERVLEVEL > 0
1279 result->flags |= source->flags & OBJ_COLORED;
1280 result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) &
1281 ((1 << (VM_NFREEORDER - 1)) - 1);
1283 VM_OBJECT_UNLOCK(source);
1288 * Return the new things
1297 * Split the pages in a map entry into a new object. This affords
1298 * easier removal of unused pages, and keeps object inheritance from
1299 * being a negative impact on memory usage.
1302 vm_object_split(vm_map_entry_t entry)
1304 vm_page_t ma[VM_RADIX_STACK];
1306 vm_object_t orig_object, new_object, source;
1307 vm_pindex_t idx, offidxstart, start;
1312 orig_object = entry->object.vm_object;
1313 if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1315 if (orig_object->ref_count <= 1)
1317 VM_OBJECT_UNLOCK(orig_object);
1319 offidxstart = OFF_TO_IDX(entry->offset);
1320 size = atop(entry->end - entry->start);
1323 * If swap_pager_copy() is later called, it will convert new_object
1324 * into a swap object.
1326 new_object = vm_object_allocate(OBJT_DEFAULT, size);
1329 * At this point, the new object is still private, so the order in
1330 * which the original and new objects are locked does not matter.
1332 VM_OBJECT_LOCK(new_object);
1333 VM_OBJECT_LOCK(orig_object);
1334 source = orig_object->backing_object;
1335 if (source != NULL) {
1336 VM_OBJECT_LOCK(source);
1337 if ((source->flags & OBJ_DEAD) != 0) {
1338 VM_OBJECT_UNLOCK(source);
1339 VM_OBJECT_UNLOCK(orig_object);
1340 VM_OBJECT_UNLOCK(new_object);
1341 vm_object_deallocate(new_object);
1342 VM_OBJECT_LOCK(orig_object);
1345 LIST_INSERT_HEAD(&source->shadow_head,
1346 new_object, shadow_list);
1347 source->shadow_count++;
1348 vm_object_reference_locked(source); /* for new_object */
1349 vm_object_clear_flag(source, OBJ_ONEMAPPING);
1350 VM_OBJECT_UNLOCK(source);
1351 new_object->backing_object_offset =
1352 orig_object->backing_object_offset + entry->offset;
1353 new_object->backing_object = source;
1355 if (orig_object->cred != NULL) {
1356 new_object->cred = orig_object->cred;
1357 crhold(orig_object->cred);
1358 new_object->charge = ptoa(size);
1359 KASSERT(orig_object->charge >= ptoa(size),
1360 ("orig_object->charge < 0"));
1361 orig_object->charge -= ptoa(size);
1363 start = offidxstart;
1366 while (exhausted == 0 && (n = vm_radix_lookupn(&orig_object->rtree,
1367 start, offidxstart + size, VM_RADIX_ANY, (void **)ma,
1368 VM_RADIX_STACK, &start, &exhausted)) != 0) {
1369 for (i = 0; i < n; i++) {
1371 idx = m->pindex - offidxstart;
1372 if (m->flags & PG_CACHED) {
1373 mtx_lock(&vm_page_queue_free_mtx);
1374 if (m->object == orig_object)
1375 vm_page_cache_rename(m, new_object,
1377 mtx_unlock(&vm_page_queue_free_mtx);
1379 } else if (m->object != orig_object)
1382 * We must wait for pending I/O to complete before
1383 * we can rename the page.
1385 * We do not have to VM_PROT_NONE the page as mappings
1386 * should not be changed by this operation.
1388 if ((m->oflags & VPO_BUSY) || m->busy) {
1390 VM_OBJECT_UNLOCK(new_object);
1391 m->oflags |= VPO_WANTED;
1392 VM_OBJECT_SLEEP(orig_object, m, PVM, "spltwt", 0);
1393 VM_OBJECT_LOCK(new_object);
1396 #if VM_NRESERVLEVEL > 0
1398 * If some of the reservation's allocated pages remain
1399 * with the original object, then transferring the
1400 * reservation to the new object is neither
1401 * particularly beneficial nor particularly harmful as
1402 * compared to leaving the reservation with the
1403 * original object. If, however, all of the
1404 * reservation's allocated pages are transferred to
1405 * the new object, then transferring the reservation
1406 * is typically beneficial. Determining which of
1407 * these two cases applies would be more costly than
1408 * unconditionally renaming the reservation.
1410 vm_reserv_rename(m, new_object, orig_object,
1413 vm_page_rename(m, new_object, idx);
1415 * page automatically made dirty by rename and
1420 if (n < VM_RADIX_STACK)
1423 if (orig_object->type == OBJT_SWAP) {
1425 * swap_pager_copy() can sleep, in which case the orig_object's
1426 * and new_object's locks are released and reacquired.
1428 swap_pager_copy(orig_object, new_object, offidxstart, 0);
1430 VM_OBJECT_UNLOCK(orig_object);
1431 TAILQ_FOREACH(m, &new_object->memq, listq)
1433 VM_OBJECT_UNLOCK(new_object);
1434 entry->object.vm_object = new_object;
1435 entry->offset = 0LL;
1436 vm_object_deallocate(orig_object);
1437 VM_OBJECT_LOCK(new_object);
1440 #define OBSC_TEST_ALL_SHADOWED 0x0001
1441 #define OBSC_COLLAPSE_NOWAIT 0x0002
1442 #define OBSC_COLLAPSE_WAIT 0x0004
1445 vm_object_backing_scan(vm_object_t object, int op)
1447 vm_page_t pa[VM_RADIX_STACK];
1449 vm_object_t backing_object;
1450 vm_pindex_t backing_offset_index, new_pindex;
1456 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1457 VM_OBJECT_LOCK_ASSERT(object->backing_object, MA_OWNED);
1459 backing_object = object->backing_object;
1460 backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
1463 * Initial conditions
1465 if (op & OBSC_TEST_ALL_SHADOWED) {
1467 * We do not want to have to test for the existence of cache
1468 * or swap pages in the backing object. XXX but with the
1469 * new swapper this would be pretty easy to do.
1471 * XXX what about anonymous MAP_SHARED memory that hasn't
1472 * been ZFOD faulted yet? If we do not test for this, the
1473 * shadow test may succeed! XXX
1475 if (backing_object->type != OBJT_DEFAULT) {
1479 if (op & OBSC_COLLAPSE_WAIT) {
1480 vm_object_set_flag(backing_object, OBJ_DEAD);
1482 color = VM_RADIX_BLACK;
1483 if (op & OBSC_COLLAPSE_WAIT)
1484 color |= VM_RADIX_RED;
1490 i = n = VM_RADIX_STACK;
1494 if (n < VM_RADIX_STACK)
1496 if (exhausted != 0 ||
1497 (n = vm_radix_lookupn(&backing_object->rtree,
1498 start, 0, color, (void **)pa, VM_RADIX_STACK,
1499 &start, &exhausted)) == 0)
1505 * Free cached pages. XXX Why? Emulating old behavior here.
1507 if (p->flags & PG_CACHED) {
1508 mtx_lock(&vm_page_queue_free_mtx);
1509 if (p->object == backing_object)
1510 vm_page_cache_free(p);
1511 mtx_unlock(&vm_page_queue_free_mtx);
1513 } else if (p->object != backing_object)
1516 new_pindex = p->pindex - backing_offset_index;
1517 if (op & OBSC_TEST_ALL_SHADOWED) {
1521 * Ignore pages outside the parent object's range
1522 * and outside the parent object's mapping of the
1525 * note that we do not busy the backing object's
1528 if (p->pindex < backing_offset_index ||
1529 new_pindex >= object->size)
1533 * See if the parent has the page or if the parent's
1534 * object pager has the page. If the parent has the
1535 * page but the page is not valid, the parent's
1536 * object pager must have the page.
1538 * If this fails, the parent does not completely shadow
1539 * the object and we might as well give up now.
1542 pp = vm_page_lookup(object, new_pindex);
1544 (pp == NULL || pp->valid == 0) &&
1545 !vm_pager_has_page(object, new_pindex, NULL, NULL)
1553 * Check for busy page
1555 if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
1558 if (op & OBSC_COLLAPSE_NOWAIT) {
1559 if ((p->oflags & VPO_BUSY) || !p->valid ||
1562 } else if (op & OBSC_COLLAPSE_WAIT) {
1563 if ((p->oflags & VPO_BUSY) || p->busy) {
1564 VM_OBJECT_UNLOCK(object);
1565 p->oflags |= VPO_WANTED;
1566 VM_OBJECT_SLEEP(backing_object, p,
1567 PDROP | PVM, "vmocol", 0);
1568 VM_OBJECT_LOCK(object);
1569 VM_OBJECT_LOCK(backing_object);
1571 * If we slept, anything could have
1572 * happened. Since the object is
1573 * marked dead, the backing offset
1574 * should not have changed so we
1575 * just restart our scan.
1582 p->object == backing_object,
1583 ("vm_object_backing_scan: object mismatch")
1587 * Destroy any associated swap
1589 if (backing_object->type == OBJT_SWAP) {
1590 swap_pager_freespace(
1598 p->pindex < backing_offset_index ||
1599 new_pindex >= object->size
1602 * Page is out of the parent object's range, we
1603 * can simply destroy it.
1606 KASSERT(!pmap_page_is_mapped(p),
1607 ("freeing mapped page %p", p));
1608 if (p->wire_count == 0)
1616 pp = vm_page_lookup(object, new_pindex);
1618 (op & OBSC_COLLAPSE_NOWAIT) != 0 &&
1619 (pp != NULL && pp->valid == 0)
1622 * The page in the parent is not (yet) valid.
1623 * We don't know anything about the state of
1624 * the original page. It might be mapped,
1625 * so we must avoid the next if here.
1627 * This is due to a race in vm_fault() where
1628 * we must unbusy the original (backing_obj)
1629 * page before we can (re)lock the parent.
1630 * Hence we can get here.
1636 vm_pager_has_page(object, new_pindex, NULL, NULL)
1639 * page already exists in parent OR swap exists
1640 * for this location in the parent. Destroy
1641 * the original page from the backing object.
1643 * Leave the parent's page alone
1646 KASSERT(!pmap_page_is_mapped(p),
1647 ("freeing mapped page %p", p));
1648 if (p->wire_count == 0)
1656 #if VM_NRESERVLEVEL > 0
1658 * Rename the reservation.
1660 vm_reserv_rename(p, object, backing_object,
1661 backing_offset_index);
1665 * Page does not exist in parent, rename the
1666 * page from the backing object to the main object.
1668 * If the page was mapped to a process, it can remain
1669 * mapped through the rename.
1671 vm_page_rename(p, object, new_pindex);
1672 /* page automatically made dirty by rename */
1680 * this version of collapse allows the operation to occur earlier and
1681 * when paging_in_progress is true for an object... This is not a complete
1682 * operation, but should plug 99.9% of the rest of the leaks.
1685 vm_object_qcollapse(vm_object_t object)
1687 vm_object_t backing_object = object->backing_object;
1689 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1690 VM_OBJECT_LOCK_ASSERT(backing_object, MA_OWNED);
1692 if (backing_object->ref_count != 1)
1695 vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
1699 * vm_object_collapse:
1701 * Collapse an object with the object backing it.
1702 * Pages in the backing object are moved into the
1703 * parent, and the backing object is deallocated.
1706 vm_object_collapse(vm_object_t object)
1708 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1711 vm_object_t backing_object;
1714 * Verify that the conditions are right for collapse:
1716 * The object exists and the backing object exists.
1718 if ((backing_object = object->backing_object) == NULL)
1722 * we check the backing object first, because it is most likely
1725 VM_OBJECT_LOCK(backing_object);
1726 if (backing_object->handle != NULL ||
1727 (backing_object->type != OBJT_DEFAULT &&
1728 backing_object->type != OBJT_SWAP) ||
1729 (backing_object->flags & OBJ_DEAD) ||
1730 object->handle != NULL ||
1731 (object->type != OBJT_DEFAULT &&
1732 object->type != OBJT_SWAP) ||
1733 (object->flags & OBJ_DEAD)) {
1734 VM_OBJECT_UNLOCK(backing_object);
1739 object->paging_in_progress != 0 ||
1740 backing_object->paging_in_progress != 0
1742 vm_object_qcollapse(object);
1743 VM_OBJECT_UNLOCK(backing_object);
1747 * We know that we can either collapse the backing object (if
1748 * the parent is the only reference to it) or (perhaps) have
1749 * the parent bypass the object if the parent happens to shadow
1750 * all the resident pages in the entire backing object.
1752 * This is ignoring pager-backed pages such as swap pages.
1753 * vm_object_backing_scan fails the shadowing test in this
1756 if (backing_object->ref_count == 1) {
1758 * If there is exactly one reference to the backing
1759 * object, we can collapse it into the parent.
1761 vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1763 #if VM_NRESERVLEVEL > 0
1765 * Break any reservations from backing_object.
1767 if (__predict_false(!LIST_EMPTY(&backing_object->rvq)))
1768 vm_reserv_break_all(backing_object);
1772 * Move the pager from backing_object to object.
1774 if (backing_object->type == OBJT_SWAP) {
1776 * swap_pager_copy() can sleep, in which case
1777 * the backing_object's and object's locks are
1778 * released and reacquired.
1779 * Since swap_pager_copy() is being asked to
1780 * destroy the source, it will change the
1781 * backing_object's type to OBJT_DEFAULT.
1786 OFF_TO_IDX(object->backing_object_offset), TRUE);
1789 * Object now shadows whatever backing_object did.
1790 * Note that the reference to
1791 * backing_object->backing_object moves from within
1792 * backing_object to within object.
1794 LIST_REMOVE(object, shadow_list);
1795 backing_object->shadow_count--;
1796 if (backing_object->backing_object) {
1797 VM_OBJECT_LOCK(backing_object->backing_object);
1798 LIST_REMOVE(backing_object, shadow_list);
1800 &backing_object->backing_object->shadow_head,
1801 object, shadow_list);
1803 * The shadow_count has not changed.
1805 VM_OBJECT_UNLOCK(backing_object->backing_object);
1807 object->backing_object = backing_object->backing_object;
1808 object->backing_object_offset +=
1809 backing_object->backing_object_offset;
1812 * Discard backing_object.
1814 * Since the backing object has no pages, no pager left,
1815 * and no object references within it, all that is
1816 * necessary is to dispose of it.
1818 KASSERT(backing_object->ref_count == 1, (
1819 "backing_object %p was somehow re-referenced during collapse!",
1821 VM_OBJECT_UNLOCK(backing_object);
1822 vm_object_destroy(backing_object);
1826 vm_object_t new_backing_object;
1829 * If we do not entirely shadow the backing object,
1830 * there is nothing we can do so we give up.
1832 if (object->resident_page_count != object->size &&
1833 vm_object_backing_scan(object,
1834 OBSC_TEST_ALL_SHADOWED) == 0) {
1835 VM_OBJECT_UNLOCK(backing_object);
1840 * Make the parent shadow the next object in the
1841 * chain. Deallocating backing_object will not remove
1842 * it, since its reference count is at least 2.
1844 LIST_REMOVE(object, shadow_list);
1845 backing_object->shadow_count--;
1847 new_backing_object = backing_object->backing_object;
1848 if ((object->backing_object = new_backing_object) != NULL) {
1849 VM_OBJECT_LOCK(new_backing_object);
1851 &new_backing_object->shadow_head,
1855 new_backing_object->shadow_count++;
1856 vm_object_reference_locked(new_backing_object);
1857 VM_OBJECT_UNLOCK(new_backing_object);
1858 object->backing_object_offset +=
1859 backing_object->backing_object_offset;
1863 * Drop the reference count on backing_object. Since
1864 * its ref_count was at least 2, it will not vanish.
1866 backing_object->ref_count--;
1867 VM_OBJECT_UNLOCK(backing_object);
1872 * Try again with this object's new backing object.
1878 * vm_object_page_remove:
1880 * For the given object, either frees or invalidates each of the
1881 * specified pages. In general, a page is freed. However, if a page is
1882 * wired for any reason other than the existence of a managed, wired
1883 * mapping, then it may be invalidated but not removed from the object.
1884 * Pages are specified by the given range ["start", "end") and the option
1885 * OBJPR_CLEANONLY. As a special case, if "end" is zero, then the range
1886 * extends from "start" to the end of the object. If the option
1887 * OBJPR_CLEANONLY is specified, then only the non-dirty pages within the
1888 * specified range are affected. If the option OBJPR_NOTMAPPED is
1889 * specified, then the pages within the specified range must have no
1890 * mappings. Otherwise, if this option is not specified, any mappings to
1891 * the specified pages are removed before the pages are freed or
1894 * In general, this operation should only be performed on objects that
1895 * contain managed pages. There are, however, two exceptions. First, it
1896 * is performed on the kernel and kmem objects by vm_map_entry_delete().
1897 * Second, it is used by msync(..., MS_INVALIDATE) to invalidate device-
1898 * backed pages. In both of these cases, the option OBJPR_CLEANONLY must
1899 * not be specified and the option OBJPR_NOTMAPPED must be specified.
1901 * The object must be locked.
1904 vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
1908 vm_page_t pa[VM_RADIX_STACK];
1914 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1915 KASSERT((object->flags & OBJ_UNMANAGED) == 0 ||
1916 (options & (OBJPR_CLEANONLY | OBJPR_NOTMAPPED)) == OBJPR_NOTMAPPED,
1917 ("vm_object_page_remove: illegal options for object %p", object));
1918 if (object->resident_page_count == 0 && object->cached_page_count == 0)
1921 vm_object_pip_add(object, 1);
1924 while (exhausted == 0 && (n = vm_radix_lookupn(&object->rtree, start,
1925 end, VM_RADIX_ANY, (void **)pa, VM_RADIX_STACK, &start,
1926 &exhausted)) != 0) {
1927 for (i = 0; i < n; i++) {
1930 * Another thread may allocate this cached page from
1931 * the queue before we acquire the page queue free
1934 if (p->flags & PG_CACHED) {
1935 mtx_lock(&vm_page_queue_free_mtx);
1936 if (p->object == object) {
1937 vm_page_cache_free(p);
1938 if (object->type == OBJT_VNODE &&
1939 object->cached_page_count == 0)
1940 vp = object->handle;
1942 mtx_unlock(&vm_page_queue_free_mtx);
1944 } else if (p->object != object)
1947 * If the page is wired for any reason besides
1948 * the existence of managed, wired mappings, then
1949 * it cannot be freed. For example, fictitious
1950 * pages, which represent device memory, are
1951 * inherently wired and cannot be freed. They can,
1952 * however, be invalidated if the option
1953 * OBJPR_CLEANONLY is not specified.
1956 if ((wirings = p->wire_count) != 0 &&
1957 (wirings = pmap_page_wired_mappings(p)) !=
1959 if ((options & OBJPR_NOTMAPPED) == 0) {
1962 * Account for removal of wired
1966 p->wire_count -= wirings;
1968 if ((options & OBJPR_CLEANONLY) == 0) {
1975 if (vm_page_sleep_if_busy(p, TRUE, "vmopar")) {
1979 KASSERT((p->flags & PG_FICTITIOUS) == 0,
1980 ("vm_object_page_remove: page %p is fictitious",
1982 if ((options & OBJPR_CLEANONLY) != 0 && p->valid != 0) {
1983 if ((options & OBJPR_NOTMAPPED) == 0)
1984 pmap_remove_write(p);
1990 if ((options & OBJPR_NOTMAPPED) == 0) {
1992 /* Account for removal of wired mappings. */
1994 KASSERT(p->wire_count == wirings,
1995 ("inconsistent wire count %d %d %p",
1996 p->wire_count, wirings, p));
1998 atomic_subtract_int(&cnt.v_wire_count,
2002 p->wire_count -= wirings;
2007 if (n < VM_RADIX_STACK)
2010 vm_object_pip_wakeup(object);
2016 * vm_object_page_cache:
2018 * For the given object, attempt to move the specified clean
2019 * pages to the cache queue. If a page is wired for any reason,
2020 * then it will not be changed. Pages are specified by the given
2021 * range ["start", "end"). As a special case, if "end" is zero,
2022 * then the range extends from "start" to the end of the object.
2023 * Any mappings to the specified pages are removed before the
2024 * pages are moved to the cache queue.
2026 * This operation should only be performed on objects that
2027 * contain non-fictitious, managed pages.
2029 * The object must be locked.
2032 vm_object_page_cache(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
2034 struct mtx *mtx, *new_mtx;
2037 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2038 KASSERT((object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0,
2039 ("vm_object_page_cache: illegal object %p", object));
2040 if (object->resident_page_count == 0)
2042 p = vm_page_find_least(object, start);
2045 * Here, the variable "p" is either (1) the page with the least pindex
2046 * greater than or equal to the parameter "start" or (2) NULL.
2049 for (; p != NULL && (p->pindex < end || end == 0); p = next) {
2050 next = TAILQ_NEXT(p, listq);
2053 * Avoid releasing and reacquiring the same page lock.
2055 new_mtx = vm_page_lockptr(p);
2056 if (mtx != new_mtx) {
2062 vm_page_try_to_cache(p);
2069 * Populate the specified range of the object with valid pages. Returns
2070 * TRUE if the range is successfully populated and FALSE otherwise.
2072 * Note: This function should be optimized to pass a larger array of
2073 * pages to vm_pager_get_pages() before it is applied to a non-
2074 * OBJT_DEVICE object.
2076 * The object must be locked.
2079 vm_object_populate(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
2085 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2086 for (pindex = start; pindex < end; pindex++) {
2087 m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL |
2089 if (m->valid != VM_PAGE_BITS_ALL) {
2091 rv = vm_pager_get_pages(object, ma, 1, 0);
2092 m = vm_page_lookup(object, pindex);
2095 if (rv != VM_PAGER_OK) {
2103 * Keep "m" busy because a subsequent iteration may unlock
2107 if (pindex > start) {
2108 m = vm_page_lookup(object, start);
2109 while (m != NULL && m->pindex < pindex) {
2111 m = TAILQ_NEXT(m, listq);
2114 return (pindex == end);
2118 * Routine: vm_object_coalesce
2119 * Function: Coalesces two objects backing up adjoining
2120 * regions of memory into a single object.
2122 * returns TRUE if objects were combined.
2124 * NOTE: Only works at the moment if the second object is NULL -
2125 * if it's not, which object do we lock first?
2128 * prev_object First object to coalesce
2129 * prev_offset Offset into prev_object
2130 * prev_size Size of reference to prev_object
2131 * next_size Size of reference to the second object
2132 * reserved Indicator that extension region has
2133 * swap accounted for
2136 * The object must *not* be locked.
2139 vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset,
2140 vm_size_t prev_size, vm_size_t next_size, boolean_t reserved)
2142 vm_pindex_t next_pindex;
2144 if (prev_object == NULL)
2146 VM_OBJECT_LOCK(prev_object);
2147 if (prev_object->type != OBJT_DEFAULT &&
2148 prev_object->type != OBJT_SWAP) {
2149 VM_OBJECT_UNLOCK(prev_object);
2154 * Try to collapse the object first
2156 vm_object_collapse(prev_object);
2159 * Can't coalesce if: . more than one reference . paged out . shadows
2160 * another object . has a copy elsewhere (any of which mean that the
2161 * pages not mapped to prev_entry may be in use anyway)
2163 if (prev_object->backing_object != NULL) {
2164 VM_OBJECT_UNLOCK(prev_object);
2168 prev_size >>= PAGE_SHIFT;
2169 next_size >>= PAGE_SHIFT;
2170 next_pindex = OFF_TO_IDX(prev_offset) + prev_size;
2172 if ((prev_object->ref_count > 1) &&
2173 (prev_object->size != next_pindex)) {
2174 VM_OBJECT_UNLOCK(prev_object);
2179 * Account for the charge.
2181 if (prev_object->cred != NULL) {
2184 * If prev_object was charged, then this mapping,
2185 * althought not charged now, may become writable
2186 * later. Non-NULL cred in the object would prevent
2187 * swap reservation during enabling of the write
2188 * access, so reserve swap now. Failed reservation
2189 * cause allocation of the separate object for the map
2190 * entry, and swap reservation for this entry is
2191 * managed in appropriate time.
2193 if (!reserved && !swap_reserve_by_cred(ptoa(next_size),
2194 prev_object->cred)) {
2197 prev_object->charge += ptoa(next_size);
2201 * Remove any pages that may still be in the object from a previous
2204 if (next_pindex < prev_object->size) {
2205 vm_object_page_remove(prev_object, next_pindex, next_pindex +
2207 if (prev_object->type == OBJT_SWAP)
2208 swap_pager_freespace(prev_object,
2209 next_pindex, next_size);
2211 if (prev_object->cred != NULL) {
2212 KASSERT(prev_object->charge >=
2213 ptoa(prev_object->size - next_pindex),
2214 ("object %p overcharged 1 %jx %jx", prev_object,
2215 (uintmax_t)next_pindex, (uintmax_t)next_size));
2216 prev_object->charge -= ptoa(prev_object->size -
2223 * Extend the object if necessary.
2225 if (next_pindex + next_size > prev_object->size)
2226 prev_object->size = next_pindex + next_size;
2228 VM_OBJECT_UNLOCK(prev_object);
2233 vm_object_set_writeable_dirty(vm_object_t object)
2236 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2237 if (object->type != OBJT_VNODE)
2239 object->generation++;
2240 if ((object->flags & OBJ_MIGHTBEDIRTY) != 0)
2242 vm_object_set_flag(object, OBJ_MIGHTBEDIRTY);
2245 #include "opt_ddb.h"
2247 #include <sys/kernel.h>
2249 #include <sys/cons.h>
2251 #include <ddb/ddb.h>
2254 _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
2257 vm_map_entry_t tmpe;
2265 tmpe = map->header.next;
2266 entcount = map->nentries;
2267 while (entcount-- && (tmpe != &map->header)) {
2268 if (_vm_object_in_map(map, object, tmpe)) {
2273 } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
2274 tmpm = entry->object.sub_map;
2275 tmpe = tmpm->header.next;
2276 entcount = tmpm->nentries;
2277 while (entcount-- && tmpe != &tmpm->header) {
2278 if (_vm_object_in_map(tmpm, object, tmpe)) {
2283 } else if ((obj = entry->object.vm_object) != NULL) {
2284 for (; obj; obj = obj->backing_object)
2285 if (obj == object) {
2293 vm_object_in_map(vm_object_t object)
2297 /* sx_slock(&allproc_lock); */
2298 FOREACH_PROC_IN_SYSTEM(p) {
2299 if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
2301 if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
2302 /* sx_sunlock(&allproc_lock); */
2306 /* sx_sunlock(&allproc_lock); */
2307 if (_vm_object_in_map(kernel_map, object, 0))
2309 if (_vm_object_in_map(kmem_map, object, 0))
2311 if (_vm_object_in_map(pager_map, object, 0))
2313 if (_vm_object_in_map(buffer_map, object, 0))
2318 DB_SHOW_COMMAND(vmochk, vm_object_check)
2323 * make sure that internal objs are in a map somewhere
2324 * and none have zero ref counts.
2326 TAILQ_FOREACH(object, &vm_object_list, object_list) {
2327 if (object->handle == NULL &&
2328 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2329 if (object->ref_count == 0) {
2330 db_printf("vmochk: internal obj has zero ref count: %ld\n",
2331 (long)object->size);
2333 if (!vm_object_in_map(object)) {
2335 "vmochk: internal obj is not in a map: "
2336 "ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2337 object->ref_count, (u_long)object->size,
2338 (u_long)object->size,
2339 (void *)object->backing_object);
2346 * vm_object_print: [ debug ]
2348 DB_SHOW_COMMAND(object, vm_object_print_static)
2350 /* XXX convert args. */
2351 vm_object_t object = (vm_object_t)addr;
2352 boolean_t full = have_addr;
2356 /* XXX count is an (unused) arg. Avoid shadowing it. */
2357 #define count was_count
2365 "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x ruid %d charge %jx\n",
2366 object, (int)object->type, (uintmax_t)object->size,
2367 object->resident_page_count, object->ref_count, object->flags,
2368 object->cred ? object->cred->cr_ruid : -1, (uintmax_t)object->charge);
2369 db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n",
2370 object->shadow_count,
2371 object->backing_object ? object->backing_object->ref_count : 0,
2372 object->backing_object, (uintmax_t)object->backing_object_offset);
2379 TAILQ_FOREACH(p, &object->memq, listq) {
2381 db_iprintf("memory:=");
2382 else if (count == 6) {
2390 db_printf("(off=0x%jx,page=0x%jx,obj=%p,flags=0x%X)",
2391 (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p),
2392 p->object, p->flags);
2402 /* XXX need this non-static entry for calling from vm_map_print. */
2405 /* db_expr_t */ long addr,
2406 boolean_t have_addr,
2407 /* db_expr_t */ long count,
2410 vm_object_print_static(addr, have_addr, count, modif);
2413 DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
2418 vm_page_t m, prev_m;
2422 TAILQ_FOREACH(object, &vm_object_list, object_list) {
2423 db_printf("new object: %p\n", (void *)object);
2434 TAILQ_FOREACH(m, &object->memq, listq) {
2435 if (m->pindex > 128)
2437 if ((prev_m = TAILQ_PREV(m, pglist, listq)) != NULL &&
2438 prev_m->pindex + 1 != m->pindex) {
2440 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2441 (long)fidx, rcount, (long)pa);
2453 (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
2458 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2459 (long)fidx, rcount, (long)pa);
2469 pa = VM_PAGE_TO_PHYS(m);
2473 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2474 (long)fidx, rcount, (long)pa);