2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * The Mach Operating System project at Carnegie-Mellon University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * from: @(#)vm_object.c 8.5 (Berkeley) 3/22/94
35 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36 * All rights reserved.
38 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
40 * Permission to use, copy, modify and distribute this software and
41 * its documentation is hereby granted, provided that both the copyright
42 * notice and this permission notice appear in all copies of the
43 * software, derivative works or modified versions, and any portions
44 * thereof, and that both notices appear in supporting documentation.
46 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
50 * Carnegie Mellon requests users of this software to return to
52 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
53 * School of Computer Science
54 * Carnegie Mellon University
55 * Pittsburgh PA 15213-3890
57 * any improvements or extensions that they make and grant Carnegie the
58 * rights to redistribute these changes.
62 * Virtual memory object module.
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
70 #include <sys/param.h>
71 #include <sys/systm.h>
74 #include <sys/mount.h>
75 #include <sys/kernel.h>
76 #include <sys/sysctl.h>
77 #include <sys/mutex.h>
78 #include <sys/proc.h> /* for curproc, pageproc */
79 #include <sys/socket.h>
80 #include <sys/resourcevar.h>
81 #include <sys/vnode.h>
82 #include <sys/vmmeter.h>
86 #include <vm/vm_param.h>
88 #include <vm/vm_map.h>
89 #include <vm/vm_object.h>
90 #include <vm/vm_page.h>
91 #include <vm/vm_pageout.h>
92 #include <vm/vm_pager.h>
93 #include <vm/swap_pager.h>
94 #include <vm/vm_kern.h>
95 #include <vm/vm_extern.h>
96 #include <vm/vm_reserv.h>
99 static int msync_flush_flags = 0;
100 SYSCTL_INT(_vm, OID_AUTO, msync_flush_flags, CTLFLAG_RW, &msync_flush_flags, 0,
101 "Does nothing; kept for backward compatibility");
103 static int old_msync;
104 SYSCTL_INT(_vm, OID_AUTO, old_msync, CTLFLAG_RW, &old_msync, 0,
105 "Use old (insecure) msync behavior");
107 static int vm_object_page_collect_flush(vm_object_t object, vm_page_t p,
108 int pagerflags, int flags, int *clearobjflags);
109 static boolean_t vm_object_page_remove_write(vm_page_t p, int flags,
111 static void vm_object_qcollapse(vm_object_t object);
112 static void vm_object_vndeallocate(vm_object_t object);
115 * Virtual memory objects maintain the actual data
116 * associated with allocated virtual memory. A given
117 * page of memory exists within exactly one object.
119 * An object is only deallocated when all "references"
120 * are given up. Only one "reference" to a given
121 * region of an object should be writeable.
123 * Associated with each object is a list of all resident
124 * memory pages belonging to that object; this list is
125 * maintained by the "vm_page" module, and locked by the object's
128 * Each object also records a "pager" routine which is
129 * used to retrieve (and store) pages to the proper backing
130 * storage. In addition, objects may be backed by other
131 * objects from which they were virtual-copied.
133 * The only items within the object structure which are
134 * modified after time of creation are:
135 * reference count locked by object's lock
136 * pager routine locked by object's lock
140 struct object_q vm_object_list;
141 struct mtx vm_object_list_mtx; /* lock for object list and count */
143 struct vm_object kernel_object_store;
144 struct vm_object kmem_object_store;
146 SYSCTL_NODE(_vm_stats, OID_AUTO, object, CTLFLAG_RD, 0, "VM object stats");
148 static long object_collapses;
149 SYSCTL_LONG(_vm_stats_object, OID_AUTO, collapses, CTLFLAG_RD,
150 &object_collapses, 0, "VM object collapses");
152 static long object_bypasses;
153 SYSCTL_LONG(_vm_stats_object, OID_AUTO, bypasses, CTLFLAG_RD,
154 &object_bypasses, 0, "VM object bypasses");
156 static uma_zone_t obj_zone;
158 static int vm_object_zinit(void *mem, int size, int flags);
161 static void vm_object_zdtor(void *mem, int size, void *arg);
164 vm_object_zdtor(void *mem, int size, void *arg)
168 object = (vm_object_t)mem;
169 KASSERT(TAILQ_EMPTY(&object->memq),
170 ("object %p has resident pages",
172 #if VM_NRESERVLEVEL > 0
173 KASSERT(LIST_EMPTY(&object->rvq),
174 ("object %p has reservations",
177 KASSERT(object->cache == NULL,
178 ("object %p has cached pages",
180 KASSERT(object->paging_in_progress == 0,
181 ("object %p paging_in_progress = %d",
182 object, object->paging_in_progress));
183 KASSERT(object->resident_page_count == 0,
184 ("object %p resident_page_count = %d",
185 object, object->resident_page_count));
186 KASSERT(object->shadow_count == 0,
187 ("object %p shadow_count = %d",
188 object, object->shadow_count));
193 vm_object_zinit(void *mem, int size, int flags)
197 object = (vm_object_t)mem;
198 bzero(&object->mtx, sizeof(object->mtx));
199 VM_OBJECT_LOCK_INIT(object, "standard object");
201 /* These are true for any object that has been freed */
202 object->paging_in_progress = 0;
203 object->resident_page_count = 0;
204 object->shadow_count = 0;
209 _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
212 TAILQ_INIT(&object->memq);
213 LIST_INIT(&object->shadow_head);
218 object->generation = 1;
219 object->ref_count = 1;
220 object->memattr = VM_MEMATTR_DEFAULT;
224 if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
225 object->flags = OBJ_ONEMAPPING;
226 object->pg_color = 0;
227 object->handle = NULL;
228 object->backing_object = NULL;
229 object->backing_object_offset = (vm_ooffset_t) 0;
230 #if VM_NRESERVLEVEL > 0
231 LIST_INIT(&object->rvq);
233 object->cache = NULL;
235 mtx_lock(&vm_object_list_mtx);
236 TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
237 mtx_unlock(&vm_object_list_mtx);
243 * Initialize the VM objects module.
248 TAILQ_INIT(&vm_object_list);
249 mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF);
251 VM_OBJECT_LOCK_INIT(kernel_object, "kernel object");
252 _vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
254 #if VM_NRESERVLEVEL > 0
255 kernel_object->flags |= OBJ_COLORED;
256 kernel_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS);
259 VM_OBJECT_LOCK_INIT(kmem_object, "kmem object");
260 _vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
262 #if VM_NRESERVLEVEL > 0
263 kmem_object->flags |= OBJ_COLORED;
264 kmem_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS);
268 * The lock portion of struct vm_object must be type stable due
269 * to vm_pageout_fallback_object_lock locking a vm object
270 * without holding any references to it.
272 obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL,
278 vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM|UMA_ZONE_NOFREE);
282 vm_object_clear_flag(vm_object_t object, u_short bits)
285 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
286 object->flags &= ~bits;
290 * Sets the default memory attribute for the specified object. Pages
291 * that are allocated to this object are by default assigned this memory
294 * Presently, this function must be called before any pages are allocated
295 * to the object. In the future, this requirement may be relaxed for
296 * "default" and "swap" objects.
299 vm_object_set_memattr(vm_object_t object, vm_memattr_t memattr)
302 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
303 switch (object->type) {
310 if (!TAILQ_EMPTY(&object->memq))
311 return (KERN_FAILURE);
314 return (KERN_INVALID_ARGUMENT);
316 object->memattr = memattr;
317 return (KERN_SUCCESS);
321 vm_object_pip_add(vm_object_t object, short i)
324 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
325 object->paging_in_progress += i;
329 vm_object_pip_subtract(vm_object_t object, short i)
332 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
333 object->paging_in_progress -= i;
337 vm_object_pip_wakeup(vm_object_t object)
340 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
341 object->paging_in_progress--;
342 if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
343 vm_object_clear_flag(object, OBJ_PIPWNT);
349 vm_object_pip_wakeupn(vm_object_t object, short i)
352 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
354 object->paging_in_progress -= i;
355 if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
356 vm_object_clear_flag(object, OBJ_PIPWNT);
362 vm_object_pip_wait(vm_object_t object, char *waitid)
365 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
366 while (object->paging_in_progress) {
367 object->flags |= OBJ_PIPWNT;
368 msleep(object, VM_OBJECT_MTX(object), PVM, waitid, 0);
373 * vm_object_allocate:
375 * Returns a new object with the given size.
378 vm_object_allocate(objtype_t type, vm_pindex_t size)
382 object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK);
383 _vm_object_allocate(type, size, object);
389 * vm_object_reference:
391 * Gets another reference to the given object. Note: OBJ_DEAD
392 * objects can be referenced during final cleaning.
395 vm_object_reference(vm_object_t object)
399 VM_OBJECT_LOCK(object);
400 vm_object_reference_locked(object);
401 VM_OBJECT_UNLOCK(object);
405 * vm_object_reference_locked:
407 * Gets another reference to the given object.
409 * The object must be locked.
412 vm_object_reference_locked(vm_object_t object)
416 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
418 if (object->type == OBJT_VNODE) {
425 * Handle deallocating an object of type OBJT_VNODE.
428 vm_object_vndeallocate(vm_object_t object)
430 struct vnode *vp = (struct vnode *) object->handle;
432 VFS_ASSERT_GIANT(vp->v_mount);
433 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
434 KASSERT(object->type == OBJT_VNODE,
435 ("vm_object_vndeallocate: not a vnode object"));
436 KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
438 if (object->ref_count == 0) {
439 vprint("vm_object_vndeallocate", vp);
440 panic("vm_object_vndeallocate: bad object reference count");
445 if (object->ref_count == 0) {
446 mp_fixme("Unlocked vflag access.");
447 vp->v_vflag &= ~VV_TEXT;
449 VM_OBJECT_UNLOCK(object);
451 * vrele may need a vop lock
457 * vm_object_deallocate:
459 * Release a reference to the specified object,
460 * gained either through a vm_object_allocate
461 * or a vm_object_reference call. When all references
462 * are gone, storage associated with this object
463 * may be relinquished.
465 * No object may be locked.
468 vm_object_deallocate(vm_object_t object)
472 while (object != NULL) {
477 VM_OBJECT_LOCK(object);
478 if (object->type == OBJT_VNODE) {
479 struct vnode *vp = (struct vnode *) object->handle;
482 * Conditionally acquire Giant for a vnode-backed
483 * object. We have to be careful since the type of
484 * a vnode object can change while the object is
487 if (VFS_NEEDSGIANT(vp->v_mount) && !vfslocked) {
489 if (!mtx_trylock(&Giant)) {
490 VM_OBJECT_UNLOCK(object);
495 vm_object_vndeallocate(object);
496 VFS_UNLOCK_GIANT(vfslocked);
500 * This is to handle the case that the object
501 * changed type while we dropped its lock to
504 VFS_UNLOCK_GIANT(vfslocked);
506 KASSERT(object->ref_count != 0,
507 ("vm_object_deallocate: object deallocated too many times: %d", object->type));
510 * If the reference count goes to 0 we start calling
511 * vm_object_terminate() on the object chain.
512 * A ref count of 1 may be a special case depending on the
513 * shadow count being 0 or 1.
516 if (object->ref_count > 1) {
517 VM_OBJECT_UNLOCK(object);
519 } else if (object->ref_count == 1) {
520 if (object->shadow_count == 0 &&
521 object->handle == NULL &&
522 (object->type == OBJT_DEFAULT ||
523 object->type == OBJT_SWAP)) {
524 vm_object_set_flag(object, OBJ_ONEMAPPING);
525 } else if ((object->shadow_count == 1) &&
526 (object->handle == NULL) &&
527 (object->type == OBJT_DEFAULT ||
528 object->type == OBJT_SWAP)) {
531 robject = LIST_FIRST(&object->shadow_head);
532 KASSERT(robject != NULL,
533 ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
535 object->shadow_count));
536 if (!VM_OBJECT_TRYLOCK(robject)) {
538 * Avoid a potential deadlock.
541 VM_OBJECT_UNLOCK(object);
543 * More likely than not the thread
544 * holding robject's lock has lower
545 * priority than the current thread.
546 * Let the lower priority thread run.
552 * Collapse object into its shadow unless its
553 * shadow is dead. In that case, object will
554 * be deallocated by the thread that is
555 * deallocating its shadow.
557 if ((robject->flags & OBJ_DEAD) == 0 &&
558 (robject->handle == NULL) &&
559 (robject->type == OBJT_DEFAULT ||
560 robject->type == OBJT_SWAP)) {
562 robject->ref_count++;
564 if (robject->paging_in_progress) {
565 VM_OBJECT_UNLOCK(object);
566 vm_object_pip_wait(robject,
568 temp = robject->backing_object;
569 if (object == temp) {
570 VM_OBJECT_LOCK(object);
573 } else if (object->paging_in_progress) {
574 VM_OBJECT_UNLOCK(robject);
575 object->flags |= OBJ_PIPWNT;
577 VM_OBJECT_MTX(object),
578 PDROP | PVM, "objde2", 0);
579 VM_OBJECT_LOCK(robject);
580 temp = robject->backing_object;
581 if (object == temp) {
582 VM_OBJECT_LOCK(object);
586 VM_OBJECT_UNLOCK(object);
588 if (robject->ref_count == 1) {
589 robject->ref_count--;
594 vm_object_collapse(object);
595 VM_OBJECT_UNLOCK(object);
598 VM_OBJECT_UNLOCK(robject);
600 VM_OBJECT_UNLOCK(object);
604 temp = object->backing_object;
606 VM_OBJECT_LOCK(temp);
607 LIST_REMOVE(object, shadow_list);
608 temp->shadow_count--;
609 VM_OBJECT_UNLOCK(temp);
610 object->backing_object = NULL;
613 * Don't double-terminate, we could be in a termination
614 * recursion due to the terminate having to sync data
617 if ((object->flags & OBJ_DEAD) == 0)
618 vm_object_terminate(object);
620 VM_OBJECT_UNLOCK(object);
626 * vm_object_destroy removes the object from the global object list
627 * and frees the space for the object.
630 vm_object_destroy(vm_object_t object)
634 * Remove the object from the global object list.
636 mtx_lock(&vm_object_list_mtx);
637 TAILQ_REMOVE(&vm_object_list, object, object_list);
638 mtx_unlock(&vm_object_list_mtx);
641 * Release the allocation charge.
643 if (object->uip != NULL) {
644 KASSERT(object->type == OBJT_DEFAULT ||
645 object->type == OBJT_SWAP,
646 ("vm_object_terminate: non-swap obj %p has uip",
648 swap_release_by_uid(object->charge, object->uip);
655 * Free the space for the object.
657 uma_zfree(obj_zone, object);
661 * vm_object_terminate actually destroys the specified object, freeing
662 * up all previously used resources.
664 * The object must be locked.
665 * This routine may block.
668 vm_object_terminate(vm_object_t object)
672 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
675 * Make sure no one uses us.
677 vm_object_set_flag(object, OBJ_DEAD);
680 * wait for the pageout daemon to be done with the object
682 vm_object_pip_wait(object, "objtrm");
684 KASSERT(!object->paging_in_progress,
685 ("vm_object_terminate: pageout in progress"));
688 * Clean and free the pages, as appropriate. All references to the
689 * object are gone, so we don't need to lock it.
691 if (object->type == OBJT_VNODE) {
692 struct vnode *vp = (struct vnode *)object->handle;
695 * Clean pages and flush buffers.
697 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
698 VM_OBJECT_UNLOCK(object);
700 vinvalbuf(vp, V_SAVE, 0, 0);
702 VM_OBJECT_LOCK(object);
705 KASSERT(object->ref_count == 0,
706 ("vm_object_terminate: object with references, ref_count=%d",
710 * Now free any remaining pages. For internal objects, this also
711 * removes them from paging queues. Don't free wired pages, just
712 * remove them from the object.
714 vm_page_lock_queues();
715 while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
716 KASSERT(!p->busy && (p->oflags & VPO_BUSY) == 0,
717 ("vm_object_terminate: freeing busy page %p "
718 "p->busy = %d, p->oflags %x\n", p, p->busy, p->oflags));
719 if (p->wire_count == 0) {
726 vm_page_unlock_queues();
728 #if VM_NRESERVLEVEL > 0
729 if (__predict_false(!LIST_EMPTY(&object->rvq)))
730 vm_reserv_break_all(object);
732 if (__predict_false(object->cache != NULL))
733 vm_page_cache_free(object, 0, 0);
736 * Let the pager know object is dead.
738 vm_pager_deallocate(object);
739 VM_OBJECT_UNLOCK(object);
741 vm_object_destroy(object);
745 vm_object_page_remove_write(vm_page_t p, int flags, int *clearobjflags)
749 * If we have been asked to skip nosync pages and this is a
750 * nosync page, skip it. Note that the object flags were not
751 * cleared in this case so we do not have to set them.
753 if ((flags & OBJPC_NOSYNC) != 0 && (p->oflags & VPO_NOSYNC) != 0) {
757 pmap_remove_write(p);
758 return (p->dirty != 0);
763 * vm_object_page_clean
765 * Clean all dirty pages in the specified range of object. Leaves page
766 * on whatever queue it is currently on. If NOSYNC is set then do not
767 * write out pages with VPO_NOSYNC set (originally comes from MAP_NOSYNC),
768 * leaving the object dirty.
770 * When stuffing pages asynchronously, allow clustering. XXX we need a
771 * synchronous clustering mode implementation.
773 * Odd semantics: if start == end, we clean everything.
775 * The object must be locked.
778 vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
782 vm_pindex_t pi, tend;
783 int clearobjflags, curgeneration, n, pagerflags;
785 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
786 KASSERT(object->type == OBJT_VNODE, ("Not a vnode object"));
787 if ((object->flags & OBJ_MIGHTBEDIRTY) == 0 ||
788 object->resident_page_count == 0)
791 pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) != 0 ?
792 VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
793 pagerflags |= (flags & OBJPC_INVAL) != 0 ? VM_PAGER_PUT_INVAL : 0;
795 tend = (end == 0) ? object->size : end;
797 vm_object_set_flag(object, OBJ_CLEANING);
799 vm_page_lock_queues();
802 * Make the page read-only so we can then clear the object flags.
804 * However, if this is a nosync mmap then the object is likely to
805 * stay dirty so do not mess with the page and do not clear the
811 curgeneration = object->generation;
813 for (p = vm_page_find_least(object, start); p != NULL; p = np) {
817 np = TAILQ_NEXT(p, listq);
820 if (vm_page_sleep_if_busy(p, TRUE, "vpcwai")) {
821 vm_page_lock_queues();
822 if (object->generation != curgeneration)
824 np = vm_page_find_least(object, pi);
827 if (!vm_object_page_remove_write(p, flags, &clearobjflags))
830 n = vm_object_page_collect_flush(object, p, pagerflags,
831 flags, &clearobjflags);
832 if (object->generation != curgeneration)
834 np = vm_page_find_least(object, pi + n);
836 vm_page_unlock_queues();
838 VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC) ? MNT_WAIT : 0);
841 vm_object_clear_flag(object, OBJ_CLEANING);
842 if (clearobjflags && start == 0 && tend == object->size)
843 vm_object_clear_flag(object, OBJ_MIGHTBEDIRTY);
847 vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int pagerflags,
848 int flags, int *clearobjflags)
850 vm_page_t ma[vm_pageout_page_count], p_first, tp;
851 int count, i, mreq, runlen;
853 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
858 for (tp = p; count < vm_pageout_page_count; count++) {
859 tp = vm_page_next(tp);
860 if (tp == NULL || tp->busy != 0 || (tp->oflags & VPO_BUSY) != 0)
862 if (!vm_object_page_remove_write(tp, flags, clearobjflags))
866 for (p_first = p; count < vm_pageout_page_count; count++) {
867 tp = vm_page_prev(p_first);
868 if (tp == NULL || tp->busy != 0 || (tp->oflags & VPO_BUSY) != 0)
870 if (!vm_object_page_remove_write(tp, flags, clearobjflags))
876 for (tp = p_first, i = 0; i < count; tp = TAILQ_NEXT(tp, listq), i++)
879 vm_pageout_flush(ma, count, pagerflags, mreq, &runlen);
884 * Note that there is absolutely no sense in writing out
885 * anonymous objects, so we track down the vnode object
887 * We invalidate (remove) all pages from the address space
888 * for semantic correctness.
890 * Note: certain anonymous maps, such as MAP_NOSYNC maps,
891 * may start out with a NULL object.
894 vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size,
895 boolean_t syncio, boolean_t invalidate)
897 vm_object_t backing_object;
904 VM_OBJECT_LOCK(object);
905 while ((backing_object = object->backing_object) != NULL) {
906 VM_OBJECT_LOCK(backing_object);
907 offset += object->backing_object_offset;
908 VM_OBJECT_UNLOCK(object);
909 object = backing_object;
910 if (object->size < OFF_TO_IDX(offset + size))
911 size = IDX_TO_OFF(object->size) - offset;
914 * Flush pages if writing is allowed, invalidate them
915 * if invalidation requested. Pages undergoing I/O
916 * will be ignored by vm_object_page_remove().
918 * We cannot lock the vnode and then wait for paging
919 * to complete without deadlocking against vm_fault.
920 * Instead we simply call vm_object_page_remove() and
921 * allow it to block internally on a page-by-page
922 * basis when it encounters pages undergoing async
925 if (object->type == OBJT_VNODE &&
926 (object->flags & OBJ_MIGHTBEDIRTY) != 0) {
929 VM_OBJECT_UNLOCK(object);
930 (void) vn_start_write(vp, &mp, V_WAIT);
931 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
932 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
933 flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
934 flags |= invalidate ? OBJPC_INVAL : 0;
935 VM_OBJECT_LOCK(object);
936 vm_object_page_clean(object,
938 OFF_TO_IDX(offset + size + PAGE_MASK),
940 VM_OBJECT_UNLOCK(object);
942 VFS_UNLOCK_GIANT(vfslocked);
943 vn_finished_write(mp);
944 VM_OBJECT_LOCK(object);
946 if ((object->type == OBJT_VNODE ||
947 object->type == OBJT_DEVICE) && invalidate) {
949 purge = old_msync || (object->type == OBJT_DEVICE);
950 vm_object_page_remove(object,
952 OFF_TO_IDX(offset + size + PAGE_MASK),
953 purge ? FALSE : TRUE);
955 VM_OBJECT_UNLOCK(object);
961 * Implements the madvise function at the object/page level.
963 * MADV_WILLNEED (any object)
965 * Activate the specified pages if they are resident.
967 * MADV_DONTNEED (any object)
969 * Deactivate the specified pages if they are resident.
971 * MADV_FREE (OBJT_DEFAULT/OBJT_SWAP objects,
972 * OBJ_ONEMAPPING only)
974 * Deactivate and clean the specified pages if they are
975 * resident. This permits the process to reuse the pages
976 * without faulting or the kernel to reclaim the pages
980 vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
982 vm_pindex_t end, tpindex;
983 vm_object_t backing_object, tobject;
988 VM_OBJECT_LOCK(object);
989 end = pindex + count;
991 * Locate and adjust resident pages
993 for (; pindex < end; pindex += 1) {
999 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
1000 * and those pages must be OBJ_ONEMAPPING.
1002 if (advise == MADV_FREE) {
1003 if ((tobject->type != OBJT_DEFAULT &&
1004 tobject->type != OBJT_SWAP) ||
1005 (tobject->flags & OBJ_ONEMAPPING) == 0) {
1006 goto unlock_tobject;
1008 } else if (tobject->type == OBJT_PHYS)
1009 goto unlock_tobject;
1010 m = vm_page_lookup(tobject, tpindex);
1011 if (m == NULL && advise == MADV_WILLNEED) {
1013 * If the page is cached, reactivate it.
1015 m = vm_page_alloc(tobject, tpindex, VM_ALLOC_IFCACHED |
1020 * There may be swap even if there is no backing page
1022 if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1023 swap_pager_freespace(tobject, tpindex, 1);
1027 backing_object = tobject->backing_object;
1028 if (backing_object == NULL)
1029 goto unlock_tobject;
1030 VM_OBJECT_LOCK(backing_object);
1031 tpindex += OFF_TO_IDX(tobject->backing_object_offset);
1032 if (tobject != object)
1033 VM_OBJECT_UNLOCK(tobject);
1034 tobject = backing_object;
1036 } else if (m->valid != VM_PAGE_BITS_ALL)
1037 goto unlock_tobject;
1039 * If the page is not in a normal state, skip it.
1041 vm_page_lock_queues();
1042 if (m->hold_count != 0 || m->wire_count != 0) {
1043 vm_page_unlock_queues();
1044 goto unlock_tobject;
1046 if ((m->oflags & VPO_BUSY) || m->busy) {
1047 if (advise == MADV_WILLNEED)
1049 * Reference the page before unlocking and
1050 * sleeping so that the page daemon is less
1051 * likely to reclaim it.
1053 vm_page_flag_set(m, PG_REFERENCED);
1054 vm_page_unlock_queues();
1055 if (object != tobject)
1056 VM_OBJECT_UNLOCK(object);
1057 m->oflags |= VPO_WANTED;
1058 msleep(m, VM_OBJECT_MTX(tobject), PDROP | PVM, "madvpo",
1060 VM_OBJECT_LOCK(object);
1063 if (advise == MADV_WILLNEED) {
1064 vm_page_activate(m);
1065 } else if (advise == MADV_DONTNEED) {
1066 vm_page_dontneed(m);
1067 } else if (advise == MADV_FREE) {
1069 * Mark the page clean. This will allow the page
1070 * to be freed up by the system. However, such pages
1071 * are often reused quickly by malloc()/free()
1072 * so we do not do anything that would cause
1073 * a page fault if we can help it.
1075 * Specifically, we do not try to actually free
1076 * the page now nor do we try to put it in the
1077 * cache (which would cause a page fault on reuse).
1079 * But we do make the page is freeable as we
1080 * can without actually taking the step of unmapping
1083 pmap_clear_modify(m);
1086 vm_page_dontneed(m);
1088 vm_page_unlock_queues();
1089 if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1090 swap_pager_freespace(tobject, tpindex, 1);
1092 if (tobject != object)
1093 VM_OBJECT_UNLOCK(tobject);
1095 VM_OBJECT_UNLOCK(object);
1101 * Create a new object which is backed by the
1102 * specified existing object range. The source
1103 * object reference is deallocated.
1105 * The new object and offset into that object
1106 * are returned in the source parameters.
1110 vm_object_t *object, /* IN/OUT */
1111 vm_ooffset_t *offset, /* IN/OUT */
1120 * Don't create the new object if the old object isn't shared.
1122 if (source != NULL) {
1123 VM_OBJECT_LOCK(source);
1124 if (source->ref_count == 1 &&
1125 source->handle == NULL &&
1126 (source->type == OBJT_DEFAULT ||
1127 source->type == OBJT_SWAP)) {
1128 VM_OBJECT_UNLOCK(source);
1131 VM_OBJECT_UNLOCK(source);
1135 * Allocate a new object with the given length.
1137 result = vm_object_allocate(OBJT_DEFAULT, length);
1140 * The new object shadows the source object, adding a reference to it.
1141 * Our caller changes his reference to point to the new object,
1142 * removing a reference to the source object. Net result: no change
1143 * of reference count.
1145 * Try to optimize the result object's page color when shadowing
1146 * in order to maintain page coloring consistency in the combined
1149 result->backing_object = source;
1151 * Store the offset into the source object, and fix up the offset into
1154 result->backing_object_offset = *offset;
1155 if (source != NULL) {
1156 VM_OBJECT_LOCK(source);
1157 LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1158 source->shadow_count++;
1159 #if VM_NRESERVLEVEL > 0
1160 result->flags |= source->flags & OBJ_COLORED;
1161 result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) &
1162 ((1 << (VM_NFREEORDER - 1)) - 1);
1164 VM_OBJECT_UNLOCK(source);
1169 * Return the new things
1178 * Split the pages in a map entry into a new object. This affords
1179 * easier removal of unused pages, and keeps object inheritance from
1180 * being a negative impact on memory usage.
1183 vm_object_split(vm_map_entry_t entry)
1185 vm_page_t m, m_next;
1186 vm_object_t orig_object, new_object, source;
1187 vm_pindex_t idx, offidxstart;
1190 orig_object = entry->object.vm_object;
1191 if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1193 if (orig_object->ref_count <= 1)
1195 VM_OBJECT_UNLOCK(orig_object);
1197 offidxstart = OFF_TO_IDX(entry->offset);
1198 size = atop(entry->end - entry->start);
1201 * If swap_pager_copy() is later called, it will convert new_object
1202 * into a swap object.
1204 new_object = vm_object_allocate(OBJT_DEFAULT, size);
1207 * At this point, the new object is still private, so the order in
1208 * which the original and new objects are locked does not matter.
1210 VM_OBJECT_LOCK(new_object);
1211 VM_OBJECT_LOCK(orig_object);
1212 source = orig_object->backing_object;
1213 if (source != NULL) {
1214 VM_OBJECT_LOCK(source);
1215 if ((source->flags & OBJ_DEAD) != 0) {
1216 VM_OBJECT_UNLOCK(source);
1217 VM_OBJECT_UNLOCK(orig_object);
1218 VM_OBJECT_UNLOCK(new_object);
1219 vm_object_deallocate(new_object);
1220 VM_OBJECT_LOCK(orig_object);
1223 LIST_INSERT_HEAD(&source->shadow_head,
1224 new_object, shadow_list);
1225 source->shadow_count++;
1226 vm_object_reference_locked(source); /* for new_object */
1227 vm_object_clear_flag(source, OBJ_ONEMAPPING);
1228 VM_OBJECT_UNLOCK(source);
1229 new_object->backing_object_offset =
1230 orig_object->backing_object_offset + entry->offset;
1231 new_object->backing_object = source;
1233 if (orig_object->uip != NULL) {
1234 new_object->uip = orig_object->uip;
1235 uihold(orig_object->uip);
1236 new_object->charge = ptoa(size);
1237 KASSERT(orig_object->charge >= ptoa(size),
1238 ("orig_object->charge < 0"));
1239 orig_object->charge -= ptoa(size);
1242 m = vm_page_find_least(orig_object, offidxstart);
1243 vm_page_lock_queues();
1244 for (; m != NULL && (idx = m->pindex - offidxstart) < size;
1246 m_next = TAILQ_NEXT(m, listq);
1249 * We must wait for pending I/O to complete before we can
1252 * We do not have to VM_PROT_NONE the page as mappings should
1253 * not be changed by this operation.
1255 if ((m->oflags & VPO_BUSY) || m->busy) {
1256 vm_page_unlock_queues();
1257 VM_OBJECT_UNLOCK(new_object);
1258 m->oflags |= VPO_WANTED;
1259 msleep(m, VM_OBJECT_MTX(orig_object), PVM, "spltwt", 0);
1260 VM_OBJECT_LOCK(new_object);
1263 vm_page_rename(m, new_object, idx);
1264 /* page automatically made dirty by rename and cache handled */
1267 vm_page_unlock_queues();
1268 if (orig_object->type == OBJT_SWAP) {
1270 * swap_pager_copy() can sleep, in which case the orig_object's
1271 * and new_object's locks are released and reacquired.
1273 swap_pager_copy(orig_object, new_object, offidxstart, 0);
1276 * Transfer any cached pages from orig_object to new_object.
1278 if (__predict_false(orig_object->cache != NULL))
1279 vm_page_cache_transfer(orig_object, offidxstart,
1282 VM_OBJECT_UNLOCK(orig_object);
1283 TAILQ_FOREACH(m, &new_object->memq, listq)
1285 VM_OBJECT_UNLOCK(new_object);
1286 entry->object.vm_object = new_object;
1287 entry->offset = 0LL;
1288 vm_object_deallocate(orig_object);
1289 VM_OBJECT_LOCK(new_object);
1292 #define OBSC_TEST_ALL_SHADOWED 0x0001
1293 #define OBSC_COLLAPSE_NOWAIT 0x0002
1294 #define OBSC_COLLAPSE_WAIT 0x0004
1297 vm_object_backing_scan(vm_object_t object, int op)
1301 vm_object_t backing_object;
1302 vm_pindex_t backing_offset_index;
1304 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1305 VM_OBJECT_LOCK_ASSERT(object->backing_object, MA_OWNED);
1307 backing_object = object->backing_object;
1308 backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
1311 * Initial conditions
1313 if (op & OBSC_TEST_ALL_SHADOWED) {
1315 * We do not want to have to test for the existence of cache
1316 * or swap pages in the backing object. XXX but with the
1317 * new swapper this would be pretty easy to do.
1319 * XXX what about anonymous MAP_SHARED memory that hasn't
1320 * been ZFOD faulted yet? If we do not test for this, the
1321 * shadow test may succeed! XXX
1323 if (backing_object->type != OBJT_DEFAULT) {
1327 if (op & OBSC_COLLAPSE_WAIT) {
1328 vm_object_set_flag(backing_object, OBJ_DEAD);
1334 p = TAILQ_FIRST(&backing_object->memq);
1336 vm_page_t next = TAILQ_NEXT(p, listq);
1337 vm_pindex_t new_pindex = p->pindex - backing_offset_index;
1339 if (op & OBSC_TEST_ALL_SHADOWED) {
1343 * Ignore pages outside the parent object's range
1344 * and outside the parent object's mapping of the
1347 * note that we do not busy the backing object's
1351 p->pindex < backing_offset_index ||
1352 new_pindex >= object->size
1359 * See if the parent has the page or if the parent's
1360 * object pager has the page. If the parent has the
1361 * page but the page is not valid, the parent's
1362 * object pager must have the page.
1364 * If this fails, the parent does not completely shadow
1365 * the object and we might as well give up now.
1368 pp = vm_page_lookup(object, new_pindex);
1370 (pp == NULL || pp->valid == 0) &&
1371 !vm_pager_has_page(object, new_pindex, NULL, NULL)
1379 * Check for busy page
1381 if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
1384 if (op & OBSC_COLLAPSE_NOWAIT) {
1385 if ((p->oflags & VPO_BUSY) ||
1391 } else if (op & OBSC_COLLAPSE_WAIT) {
1392 if ((p->oflags & VPO_BUSY) || p->busy) {
1393 VM_OBJECT_UNLOCK(object);
1394 p->oflags |= VPO_WANTED;
1395 msleep(p, VM_OBJECT_MTX(backing_object),
1396 PDROP | PVM, "vmocol", 0);
1397 VM_OBJECT_LOCK(object);
1398 VM_OBJECT_LOCK(backing_object);
1400 * If we slept, anything could have
1401 * happened. Since the object is
1402 * marked dead, the backing offset
1403 * should not have changed so we
1404 * just restart our scan.
1406 p = TAILQ_FIRST(&backing_object->memq);
1412 p->object == backing_object,
1413 ("vm_object_backing_scan: object mismatch")
1417 * Destroy any associated swap
1419 if (backing_object->type == OBJT_SWAP) {
1420 swap_pager_freespace(
1428 p->pindex < backing_offset_index ||
1429 new_pindex >= object->size
1432 * Page is out of the parent object's range, we
1433 * can simply destroy it.
1435 vm_page_lock_queues();
1436 KASSERT(!pmap_page_is_mapped(p),
1437 ("freeing mapped page %p", p));
1438 if (p->wire_count == 0)
1442 vm_page_unlock_queues();
1447 pp = vm_page_lookup(object, new_pindex);
1450 vm_pager_has_page(object, new_pindex, NULL, NULL)
1453 * page already exists in parent OR swap exists
1454 * for this location in the parent. Destroy
1455 * the original page from the backing object.
1457 * Leave the parent's page alone
1459 vm_page_lock_queues();
1460 KASSERT(!pmap_page_is_mapped(p),
1461 ("freeing mapped page %p", p));
1462 if (p->wire_count == 0)
1466 vm_page_unlock_queues();
1471 #if VM_NRESERVLEVEL > 0
1473 * Rename the reservation.
1475 vm_reserv_rename(p, object, backing_object,
1476 backing_offset_index);
1480 * Page does not exist in parent, rename the
1481 * page from the backing object to the main object.
1483 * If the page was mapped to a process, it can remain
1484 * mapped through the rename.
1486 vm_page_lock_queues();
1487 vm_page_rename(p, object, new_pindex);
1488 vm_page_unlock_queues();
1489 /* page automatically made dirty by rename */
1498 * this version of collapse allows the operation to occur earlier and
1499 * when paging_in_progress is true for an object... This is not a complete
1500 * operation, but should plug 99.9% of the rest of the leaks.
1503 vm_object_qcollapse(vm_object_t object)
1505 vm_object_t backing_object = object->backing_object;
1507 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1508 VM_OBJECT_LOCK_ASSERT(backing_object, MA_OWNED);
1510 if (backing_object->ref_count != 1)
1513 vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
1517 * vm_object_collapse:
1519 * Collapse an object with the object backing it.
1520 * Pages in the backing object are moved into the
1521 * parent, and the backing object is deallocated.
1524 vm_object_collapse(vm_object_t object)
1526 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1529 vm_object_t backing_object;
1532 * Verify that the conditions are right for collapse:
1534 * The object exists and the backing object exists.
1536 if ((backing_object = object->backing_object) == NULL)
1540 * we check the backing object first, because it is most likely
1543 VM_OBJECT_LOCK(backing_object);
1544 if (backing_object->handle != NULL ||
1545 (backing_object->type != OBJT_DEFAULT &&
1546 backing_object->type != OBJT_SWAP) ||
1547 (backing_object->flags & OBJ_DEAD) ||
1548 object->handle != NULL ||
1549 (object->type != OBJT_DEFAULT &&
1550 object->type != OBJT_SWAP) ||
1551 (object->flags & OBJ_DEAD)) {
1552 VM_OBJECT_UNLOCK(backing_object);
1557 object->paging_in_progress != 0 ||
1558 backing_object->paging_in_progress != 0
1560 vm_object_qcollapse(object);
1561 VM_OBJECT_UNLOCK(backing_object);
1565 * We know that we can either collapse the backing object (if
1566 * the parent is the only reference to it) or (perhaps) have
1567 * the parent bypass the object if the parent happens to shadow
1568 * all the resident pages in the entire backing object.
1570 * This is ignoring pager-backed pages such as swap pages.
1571 * vm_object_backing_scan fails the shadowing test in this
1574 if (backing_object->ref_count == 1) {
1576 * If there is exactly one reference to the backing
1577 * object, we can collapse it into the parent.
1579 vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1581 #if VM_NRESERVLEVEL > 0
1583 * Break any reservations from backing_object.
1585 if (__predict_false(!LIST_EMPTY(&backing_object->rvq)))
1586 vm_reserv_break_all(backing_object);
1590 * Move the pager from backing_object to object.
1592 if (backing_object->type == OBJT_SWAP) {
1594 * swap_pager_copy() can sleep, in which case
1595 * the backing_object's and object's locks are
1596 * released and reacquired.
1601 OFF_TO_IDX(object->backing_object_offset), TRUE);
1604 * Free any cached pages from backing_object.
1606 if (__predict_false(backing_object->cache != NULL))
1607 vm_page_cache_free(backing_object, 0, 0);
1610 * Object now shadows whatever backing_object did.
1611 * Note that the reference to
1612 * backing_object->backing_object moves from within
1613 * backing_object to within object.
1615 LIST_REMOVE(object, shadow_list);
1616 backing_object->shadow_count--;
1617 if (backing_object->backing_object) {
1618 VM_OBJECT_LOCK(backing_object->backing_object);
1619 LIST_REMOVE(backing_object, shadow_list);
1621 &backing_object->backing_object->shadow_head,
1622 object, shadow_list);
1624 * The shadow_count has not changed.
1626 VM_OBJECT_UNLOCK(backing_object->backing_object);
1628 object->backing_object = backing_object->backing_object;
1629 object->backing_object_offset +=
1630 backing_object->backing_object_offset;
1633 * Discard backing_object.
1635 * Since the backing object has no pages, no pager left,
1636 * and no object references within it, all that is
1637 * necessary is to dispose of it.
1639 KASSERT(backing_object->ref_count == 1, (
1640 "backing_object %p was somehow re-referenced during collapse!",
1642 VM_OBJECT_UNLOCK(backing_object);
1643 vm_object_destroy(backing_object);
1647 vm_object_t new_backing_object;
1650 * If we do not entirely shadow the backing object,
1651 * there is nothing we can do so we give up.
1653 if (object->resident_page_count != object->size &&
1654 vm_object_backing_scan(object,
1655 OBSC_TEST_ALL_SHADOWED) == 0) {
1656 VM_OBJECT_UNLOCK(backing_object);
1661 * Make the parent shadow the next object in the
1662 * chain. Deallocating backing_object will not remove
1663 * it, since its reference count is at least 2.
1665 LIST_REMOVE(object, shadow_list);
1666 backing_object->shadow_count--;
1668 new_backing_object = backing_object->backing_object;
1669 if ((object->backing_object = new_backing_object) != NULL) {
1670 VM_OBJECT_LOCK(new_backing_object);
1672 &new_backing_object->shadow_head,
1676 new_backing_object->shadow_count++;
1677 vm_object_reference_locked(new_backing_object);
1678 VM_OBJECT_UNLOCK(new_backing_object);
1679 object->backing_object_offset +=
1680 backing_object->backing_object_offset;
1684 * Drop the reference count on backing_object. Since
1685 * its ref_count was at least 2, it will not vanish.
1687 backing_object->ref_count--;
1688 VM_OBJECT_UNLOCK(backing_object);
1693 * Try again with this object's new backing object.
1699 * vm_object_page_remove:
1701 * For the given object, either frees or invalidates each of the
1702 * specified pages. In general, a page is freed. However, if a
1703 * page is wired for any reason other than the existence of a
1704 * managed, wired mapping, then it may be invalidated but not
1705 * removed from the object. Pages are specified by the given
1706 * range ["start", "end") and Boolean "clean_only". As a
1707 * special case, if "end" is zero, then the range extends from
1708 * "start" to the end of the object. If "clean_only" is TRUE,
1709 * then only the non-dirty pages within the specified range are
1712 * In general, this operation should only be performed on objects
1713 * that contain managed pages. There are two exceptions. First,
1714 * it may be performed on the kernel and kmem objects. Second,
1715 * it may be used by msync(..., MS_INVALIDATE) to invalidate
1716 * device-backed pages. In both of these cases, "clean_only"
1719 * The object must be locked.
1722 vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
1723 boolean_t clean_only)
1728 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1729 if (object->resident_page_count == 0)
1733 * Since physically-backed objects do not use managed pages, we can't
1734 * remove pages from the object (we must instead remove the page
1735 * references, and then destroy the object).
1737 KASSERT(object->type != OBJT_PHYS || object == kernel_object ||
1738 object == kmem_object,
1739 ("attempt to remove pages from a physical object"));
1741 vm_object_pip_add(object, 1);
1743 p = vm_page_find_least(object, start);
1744 vm_page_lock_queues();
1746 * Assert: the variable p is either (1) the page with the
1747 * least pindex greater than or equal to the parameter pindex
1751 p != NULL && (p->pindex < end || end == 0);
1753 next = TAILQ_NEXT(p, listq);
1756 * If the page is wired for any reason besides the
1757 * existence of managed, wired mappings, then it cannot
1758 * be freed. For example, fictitious pages, which
1759 * represent device memory, are inherently wired and
1760 * cannot be freed. They can, however, be invalidated
1761 * if "clean_only" is FALSE.
1763 if ((wirings = p->wire_count) != 0 &&
1764 (wirings = pmap_page_wired_mappings(p)) != p->wire_count) {
1765 /* Fictitious pages do not have managed mappings. */
1766 if ((p->flags & PG_FICTITIOUS) == 0)
1768 /* Account for removal of managed, wired mappings. */
1769 p->wire_count -= wirings;
1776 if (vm_page_sleep_if_busy(p, TRUE, "vmopar"))
1778 KASSERT((p->flags & PG_FICTITIOUS) == 0,
1779 ("vm_object_page_remove: page %p is fictitious", p));
1780 if (clean_only && p->valid) {
1781 pmap_remove_write(p);
1786 /* Account for removal of managed, wired mappings. */
1788 p->wire_count -= wirings;
1791 vm_page_unlock_queues();
1792 vm_object_pip_wakeup(object);
1794 if (__predict_false(object->cache != NULL))
1795 vm_page_cache_free(object, start, end);
1799 * Populate the specified range of the object with valid pages. Returns
1800 * TRUE if the range is successfully populated and FALSE otherwise.
1802 * Note: This function should be optimized to pass a larger array of
1803 * pages to vm_pager_get_pages() before it is applied to a non-
1804 * OBJT_DEVICE object.
1806 * The object must be locked.
1809 vm_object_populate(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1815 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1816 for (pindex = start; pindex < end; pindex++) {
1817 m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL |
1819 if (m->valid != VM_PAGE_BITS_ALL) {
1821 rv = vm_pager_get_pages(object, ma, 1, 0);
1822 m = vm_page_lookup(object, pindex);
1825 if (rv != VM_PAGER_OK) {
1826 vm_page_lock_queues();
1828 vm_page_unlock_queues();
1833 * Keep "m" busy because a subsequent iteration may unlock
1837 if (pindex > start) {
1838 m = vm_page_lookup(object, start);
1839 while (m != NULL && m->pindex < pindex) {
1841 m = TAILQ_NEXT(m, listq);
1844 return (pindex == end);
1848 * Routine: vm_object_coalesce
1849 * Function: Coalesces two objects backing up adjoining
1850 * regions of memory into a single object.
1852 * returns TRUE if objects were combined.
1854 * NOTE: Only works at the moment if the second object is NULL -
1855 * if it's not, which object do we lock first?
1858 * prev_object First object to coalesce
1859 * prev_offset Offset into prev_object
1860 * prev_size Size of reference to prev_object
1861 * next_size Size of reference to the second object
1862 * reserved Indicator that extension region has
1863 * swap accounted for
1866 * The object must *not* be locked.
1869 vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset,
1870 vm_size_t prev_size, vm_size_t next_size, boolean_t reserved)
1872 vm_pindex_t next_pindex;
1874 if (prev_object == NULL)
1876 VM_OBJECT_LOCK(prev_object);
1877 if (prev_object->type != OBJT_DEFAULT &&
1878 prev_object->type != OBJT_SWAP) {
1879 VM_OBJECT_UNLOCK(prev_object);
1884 * Try to collapse the object first
1886 vm_object_collapse(prev_object);
1889 * Can't coalesce if: . more than one reference . paged out . shadows
1890 * another object . has a copy elsewhere (any of which mean that the
1891 * pages not mapped to prev_entry may be in use anyway)
1893 if (prev_object->backing_object != NULL) {
1894 VM_OBJECT_UNLOCK(prev_object);
1898 prev_size >>= PAGE_SHIFT;
1899 next_size >>= PAGE_SHIFT;
1900 next_pindex = OFF_TO_IDX(prev_offset) + prev_size;
1902 if ((prev_object->ref_count > 1) &&
1903 (prev_object->size != next_pindex)) {
1904 VM_OBJECT_UNLOCK(prev_object);
1909 * Account for the charge.
1911 if (prev_object->uip != NULL) {
1914 * If prev_object was charged, then this mapping,
1915 * althought not charged now, may become writable
1916 * later. Non-NULL uip in the object would prevent
1917 * swap reservation during enabling of the write
1918 * access, so reserve swap now. Failed reservation
1919 * cause allocation of the separate object for the map
1920 * entry, and swap reservation for this entry is
1921 * managed in appropriate time.
1923 if (!reserved && !swap_reserve_by_uid(ptoa(next_size),
1924 prev_object->uip)) {
1927 prev_object->charge += ptoa(next_size);
1931 * Remove any pages that may still be in the object from a previous
1934 if (next_pindex < prev_object->size) {
1935 vm_object_page_remove(prev_object,
1937 next_pindex + next_size, FALSE);
1938 if (prev_object->type == OBJT_SWAP)
1939 swap_pager_freespace(prev_object,
1940 next_pindex, next_size);
1942 if (prev_object->uip != NULL) {
1943 KASSERT(prev_object->charge >=
1944 ptoa(prev_object->size - next_pindex),
1945 ("object %p overcharged 1 %jx %jx", prev_object,
1946 (uintmax_t)next_pindex, (uintmax_t)next_size));
1947 prev_object->charge -= ptoa(prev_object->size -
1954 * Extend the object if necessary.
1956 if (next_pindex + next_size > prev_object->size)
1957 prev_object->size = next_pindex + next_size;
1959 VM_OBJECT_UNLOCK(prev_object);
1964 vm_object_set_writeable_dirty(vm_object_t object)
1967 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1968 if (object->type != OBJT_VNODE)
1970 object->generation++;
1971 if ((object->flags & OBJ_MIGHTBEDIRTY) != 0)
1973 vm_object_set_flag(object, OBJ_MIGHTBEDIRTY);
1976 #include "opt_ddb.h"
1978 #include <sys/kernel.h>
1980 #include <sys/cons.h>
1982 #include <ddb/ddb.h>
1985 _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
1988 vm_map_entry_t tmpe;
1996 tmpe = map->header.next;
1997 entcount = map->nentries;
1998 while (entcount-- && (tmpe != &map->header)) {
1999 if (_vm_object_in_map(map, object, tmpe)) {
2004 } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
2005 tmpm = entry->object.sub_map;
2006 tmpe = tmpm->header.next;
2007 entcount = tmpm->nentries;
2008 while (entcount-- && tmpe != &tmpm->header) {
2009 if (_vm_object_in_map(tmpm, object, tmpe)) {
2014 } else if ((obj = entry->object.vm_object) != NULL) {
2015 for (; obj; obj = obj->backing_object)
2016 if (obj == object) {
2024 vm_object_in_map(vm_object_t object)
2028 /* sx_slock(&allproc_lock); */
2029 FOREACH_PROC_IN_SYSTEM(p) {
2030 if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
2032 if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
2033 /* sx_sunlock(&allproc_lock); */
2037 /* sx_sunlock(&allproc_lock); */
2038 if (_vm_object_in_map(kernel_map, object, 0))
2040 if (_vm_object_in_map(kmem_map, object, 0))
2042 if (_vm_object_in_map(pager_map, object, 0))
2044 if (_vm_object_in_map(buffer_map, object, 0))
2049 DB_SHOW_COMMAND(vmochk, vm_object_check)
2054 * make sure that internal objs are in a map somewhere
2055 * and none have zero ref counts.
2057 TAILQ_FOREACH(object, &vm_object_list, object_list) {
2058 if (object->handle == NULL &&
2059 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2060 if (object->ref_count == 0) {
2061 db_printf("vmochk: internal obj has zero ref count: %ld\n",
2062 (long)object->size);
2064 if (!vm_object_in_map(object)) {
2066 "vmochk: internal obj is not in a map: "
2067 "ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2068 object->ref_count, (u_long)object->size,
2069 (u_long)object->size,
2070 (void *)object->backing_object);
2077 * vm_object_print: [ debug ]
2079 DB_SHOW_COMMAND(object, vm_object_print_static)
2081 /* XXX convert args. */
2082 vm_object_t object = (vm_object_t)addr;
2083 boolean_t full = have_addr;
2087 /* XXX count is an (unused) arg. Avoid shadowing it. */
2088 #define count was_count
2096 "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x uip %d charge %jx\n",
2097 object, (int)object->type, (uintmax_t)object->size,
2098 object->resident_page_count, object->ref_count, object->flags,
2099 object->uip ? object->uip->ui_uid : -1, (uintmax_t)object->charge);
2100 db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n",
2101 object->shadow_count,
2102 object->backing_object ? object->backing_object->ref_count : 0,
2103 object->backing_object, (uintmax_t)object->backing_object_offset);
2110 TAILQ_FOREACH(p, &object->memq, listq) {
2112 db_iprintf("memory:=");
2113 else if (count == 6) {
2121 db_printf("(off=0x%jx,page=0x%jx)",
2122 (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p));
2132 /* XXX need this non-static entry for calling from vm_map_print. */
2135 /* db_expr_t */ long addr,
2136 boolean_t have_addr,
2137 /* db_expr_t */ long count,
2140 vm_object_print_static(addr, have_addr, count, modif);
2143 DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
2148 vm_page_t m, prev_m;
2152 TAILQ_FOREACH(object, &vm_object_list, object_list) {
2153 db_printf("new object: %p\n", (void *)object);
2164 TAILQ_FOREACH(m, &object->memq, listq) {
2165 if (m->pindex > 128)
2167 if ((prev_m = TAILQ_PREV(m, pglist, listq)) != NULL &&
2168 prev_m->pindex + 1 != m->pindex) {
2170 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2171 (long)fidx, rcount, (long)pa);
2183 (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
2188 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2189 (long)fidx, rcount, (long)pa);
2199 pa = VM_PAGE_TO_PHYS(m);
2203 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2204 (long)fidx, rcount, (long)pa);