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$");
68 #include <sys/param.h>
69 #include <sys/systm.h>
72 #include <sys/mount.h>
73 #include <sys/kernel.h>
74 #include <sys/sysctl.h>
75 #include <sys/mutex.h>
76 #include <sys/proc.h> /* for curproc, pageproc */
77 #include <sys/socket.h>
78 #include <sys/vnode.h>
79 #include <sys/vmmeter.h>
83 #include <vm/vm_param.h>
85 #include <vm/vm_map.h>
86 #include <vm/vm_object.h>
87 #include <vm/vm_page.h>
88 #include <vm/vm_pageout.h>
89 #include <vm/vm_pager.h>
90 #include <vm/swap_pager.h>
91 #include <vm/vm_kern.h>
92 #include <vm/vm_extern.h>
95 #define EASY_SCAN_FACTOR 8
97 #define MSYNC_FLUSH_HARDSEQ 0x01
98 #define MSYNC_FLUSH_SOFTSEQ 0x02
101 * msync / VM object flushing optimizations
103 static int msync_flush_flags = MSYNC_FLUSH_HARDSEQ | MSYNC_FLUSH_SOFTSEQ;
104 SYSCTL_INT(_vm, OID_AUTO, msync_flush_flags,
105 CTLFLAG_RW, &msync_flush_flags, 0, "");
107 static int old_msync;
108 SYSCTL_INT(_vm, OID_AUTO, old_msync, CTLFLAG_RW, &old_msync, 0,
109 "Use old (insecure) msync behavior");
111 static void vm_object_qcollapse(vm_object_t object);
112 static int vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags);
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 static long object_collapses;
147 static long object_bypasses;
150 * next_index determines the page color that is assigned to the next
151 * allocated object. Accesses to next_index are not synchronized
152 * because the effects of two or more object allocations using
153 * next_index simultaneously are inconsequential. At any given time,
154 * numerous objects have the same page color.
156 static int next_index;
158 static uma_zone_t obj_zone;
159 #define VM_OBJECTS_INIT 256
161 static int vm_object_zinit(void *mem, int size, int flags);
164 static void vm_object_zdtor(void *mem, int size, void *arg);
167 vm_object_zdtor(void *mem, int size, void *arg)
171 object = (vm_object_t)mem;
172 KASSERT(TAILQ_EMPTY(&object->memq),
173 ("object %p has resident pages",
175 KASSERT(object->paging_in_progress == 0,
176 ("object %p paging_in_progress = %d",
177 object, object->paging_in_progress));
178 KASSERT(object->resident_page_count == 0,
179 ("object %p resident_page_count = %d",
180 object, object->resident_page_count));
181 KASSERT(object->shadow_count == 0,
182 ("object %p shadow_count = %d",
183 object, object->shadow_count));
188 vm_object_zinit(void *mem, int size, int flags)
192 object = (vm_object_t)mem;
193 bzero(&object->mtx, sizeof(object->mtx));
194 VM_OBJECT_LOCK_INIT(object, "standard object");
196 /* These are true for any object that has been freed */
197 object->paging_in_progress = 0;
198 object->resident_page_count = 0;
199 object->shadow_count = 0;
204 _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
208 TAILQ_INIT(&object->memq);
209 LIST_INIT(&object->shadow_head);
214 object->generation = 1;
215 object->ref_count = 1;
217 if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
218 object->flags = OBJ_ONEMAPPING;
219 if (size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
220 incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
223 object->pg_color = next_index;
224 next_index = (object->pg_color + incr) & PQ_L2_MASK;
225 object->handle = NULL;
226 object->backing_object = NULL;
227 object->backing_object_offset = (vm_ooffset_t) 0;
229 mtx_lock(&vm_object_list_mtx);
230 TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
231 mtx_unlock(&vm_object_list_mtx);
237 * Initialize the VM objects module.
242 TAILQ_INIT(&vm_object_list);
243 mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF);
245 VM_OBJECT_LOCK_INIT(&kernel_object_store, "kernel object");
246 _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
249 VM_OBJECT_LOCK_INIT(&kmem_object_store, "kmem object");
250 _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
253 obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL,
259 vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM|UMA_ZONE_NOFREE);
260 uma_prealloc(obj_zone, VM_OBJECTS_INIT);
264 vm_object_clear_flag(vm_object_t object, u_short bits)
267 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
268 object->flags &= ~bits;
272 vm_object_pip_add(vm_object_t object, short i)
275 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
276 object->paging_in_progress += i;
280 vm_object_pip_subtract(vm_object_t object, short i)
283 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
284 object->paging_in_progress -= i;
288 vm_object_pip_wakeup(vm_object_t object)
291 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
292 object->paging_in_progress--;
293 if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
294 vm_object_clear_flag(object, OBJ_PIPWNT);
300 vm_object_pip_wakeupn(vm_object_t object, short i)
303 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
305 object->paging_in_progress -= i;
306 if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
307 vm_object_clear_flag(object, OBJ_PIPWNT);
313 vm_object_pip_wait(vm_object_t object, char *waitid)
316 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
317 while (object->paging_in_progress) {
318 object->flags |= OBJ_PIPWNT;
319 msleep(object, VM_OBJECT_MTX(object), PVM, waitid, 0);
324 * vm_object_allocate:
326 * Returns a new object with the given size.
329 vm_object_allocate(objtype_t type, vm_pindex_t size)
333 object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK);
334 _vm_object_allocate(type, size, object);
340 * vm_object_reference:
342 * Gets another reference to the given object. Note: OBJ_DEAD
343 * objects can be referenced during final cleaning.
346 vm_object_reference(vm_object_t object)
353 VM_OBJECT_LOCK(object);
355 if (object->type == OBJT_VNODE) {
358 VM_OBJECT_UNLOCK(object);
359 for (flags = LK_INTERLOCK; vget(vp, flags, curthread);
361 printf("vm_object_reference: delay in vget\n");
363 VM_OBJECT_UNLOCK(object);
367 * vm_object_reference_locked:
369 * Gets another reference to the given object.
371 * The object must be locked.
374 vm_object_reference_locked(vm_object_t object)
378 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
379 KASSERT((object->flags & OBJ_DEAD) == 0,
380 ("vm_object_reference_locked: dead object referenced"));
382 if (object->type == OBJT_VNODE) {
389 * Handle deallocating an object of type OBJT_VNODE.
392 vm_object_vndeallocate(vm_object_t object)
394 struct vnode *vp = (struct vnode *) object->handle;
396 VFS_ASSERT_GIANT(vp->v_mount);
397 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
398 KASSERT(object->type == OBJT_VNODE,
399 ("vm_object_vndeallocate: not a vnode object"));
400 KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
402 if (object->ref_count == 0) {
403 vprint("vm_object_vndeallocate", vp);
404 panic("vm_object_vndeallocate: bad object reference count");
409 if (object->ref_count == 0) {
410 mp_fixme("Unlocked vflag access.");
411 vp->v_vflag &= ~VV_TEXT;
413 VM_OBJECT_UNLOCK(object);
415 * vrele may need a vop lock
421 * vm_object_deallocate:
423 * Release a reference to the specified object,
424 * gained either through a vm_object_allocate
425 * or a vm_object_reference call. When all references
426 * are gone, storage associated with this object
427 * may be relinquished.
429 * No object may be locked.
432 vm_object_deallocate(vm_object_t object)
436 while (object != NULL) {
439 * In general, the object should be locked when working with
440 * its type. In this case, in order to maintain proper lock
441 * ordering, an exception is possible because a vnode-backed
442 * object never changes its type.
445 if (object->type == OBJT_VNODE) {
446 struct vnode *vp = (struct vnode *) object->handle;
447 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
449 VM_OBJECT_LOCK(object);
450 if (object->type == OBJT_VNODE) {
451 vm_object_vndeallocate(object);
452 VFS_UNLOCK_GIANT(vfslocked);
456 KASSERT(object->ref_count != 0,
457 ("vm_object_deallocate: object deallocated too many times: %d", object->type));
460 * If the reference count goes to 0 we start calling
461 * vm_object_terminate() on the object chain.
462 * A ref count of 1 may be a special case depending on the
463 * shadow count being 0 or 1.
466 if (object->ref_count > 1) {
467 VM_OBJECT_UNLOCK(object);
469 } else if (object->ref_count == 1) {
470 if (object->shadow_count == 0) {
471 vm_object_set_flag(object, OBJ_ONEMAPPING);
472 } else if ((object->shadow_count == 1) &&
473 (object->handle == NULL) &&
474 (object->type == OBJT_DEFAULT ||
475 object->type == OBJT_SWAP)) {
478 robject = LIST_FIRST(&object->shadow_head);
479 KASSERT(robject != NULL,
480 ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
482 object->shadow_count));
483 if (!VM_OBJECT_TRYLOCK(robject)) {
485 * Avoid a potential deadlock.
488 VM_OBJECT_UNLOCK(object);
490 * More likely than not the thread
491 * holding robject's lock has lower
492 * priority than the current thread.
493 * Let the lower priority thread run.
495 tsleep(&proc0, PVM, "vmo_de", 1);
499 * Collapse object into its shadow unless its
500 * shadow is dead. In that case, object will
501 * be deallocated by the thread that is
502 * deallocating its shadow.
504 if ((robject->flags & OBJ_DEAD) == 0 &&
505 (robject->handle == NULL) &&
506 (robject->type == OBJT_DEFAULT ||
507 robject->type == OBJT_SWAP)) {
509 robject->ref_count++;
511 if (robject->paging_in_progress) {
512 VM_OBJECT_UNLOCK(object);
513 vm_object_pip_wait(robject,
515 VM_OBJECT_LOCK(object);
517 } else if (object->paging_in_progress) {
518 VM_OBJECT_UNLOCK(robject);
519 object->flags |= OBJ_PIPWNT;
521 VM_OBJECT_MTX(object),
522 PDROP | PVM, "objde2", 0);
523 VM_OBJECT_LOCK(robject);
524 VM_OBJECT_LOCK(object);
527 VM_OBJECT_UNLOCK(object);
528 if (robject->ref_count == 1) {
529 robject->ref_count--;
534 vm_object_collapse(object);
535 VM_OBJECT_UNLOCK(object);
538 VM_OBJECT_UNLOCK(robject);
540 VM_OBJECT_UNLOCK(object);
544 temp = object->backing_object;
546 VM_OBJECT_LOCK(temp);
547 LIST_REMOVE(object, shadow_list);
548 temp->shadow_count--;
550 VM_OBJECT_UNLOCK(temp);
551 object->backing_object = NULL;
554 * Don't double-terminate, we could be in a termination
555 * recursion due to the terminate having to sync data
558 if ((object->flags & OBJ_DEAD) == 0)
559 vm_object_terminate(object);
561 VM_OBJECT_UNLOCK(object);
567 * vm_object_terminate actually destroys the specified object, freeing
568 * up all previously used resources.
570 * The object must be locked.
571 * This routine may block.
574 vm_object_terminate(vm_object_t object)
578 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
581 * Make sure no one uses us.
583 vm_object_set_flag(object, OBJ_DEAD);
586 * wait for the pageout daemon to be done with the object
588 vm_object_pip_wait(object, "objtrm");
590 KASSERT(!object->paging_in_progress,
591 ("vm_object_terminate: pageout in progress"));
594 * Clean and free the pages, as appropriate. All references to the
595 * object are gone, so we don't need to lock it.
597 if (object->type == OBJT_VNODE) {
598 struct vnode *vp = (struct vnode *)object->handle;
601 * Clean pages and flush buffers.
603 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
604 VM_OBJECT_UNLOCK(object);
606 vinvalbuf(vp, V_SAVE, NULL, 0, 0);
608 VM_OBJECT_LOCK(object);
611 KASSERT(object->ref_count == 0,
612 ("vm_object_terminate: object with references, ref_count=%d",
616 * Now free any remaining pages. For internal objects, this also
617 * removes them from paging queues. Don't free wired pages, just
618 * remove them from the object.
620 vm_page_lock_queues();
621 while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
622 KASSERT(!p->busy && (p->flags & PG_BUSY) == 0,
623 ("vm_object_terminate: freeing busy page %p "
624 "p->busy = %d, p->flags %x\n", p, p->busy, p->flags));
625 if (p->wire_count == 0) {
632 vm_page_unlock_queues();
635 * Let the pager know object is dead.
637 vm_pager_deallocate(object);
638 VM_OBJECT_UNLOCK(object);
641 * Remove the object from the global object list.
643 mtx_lock(&vm_object_list_mtx);
644 TAILQ_REMOVE(&vm_object_list, object, object_list);
645 mtx_unlock(&vm_object_list_mtx);
648 * Free the space for the object.
650 uma_zfree(obj_zone, object);
654 * vm_object_page_clean
656 * Clean all dirty pages in the specified range of object. Leaves page
657 * on whatever queue it is currently on. If NOSYNC is set then do not
658 * write out pages with PG_NOSYNC set (originally comes from MAP_NOSYNC),
659 * leaving the object dirty.
661 * When stuffing pages asynchronously, allow clustering. XXX we need a
662 * synchronous clustering mode implementation.
664 * Odd semantics: if start == end, we clean everything.
666 * The object must be locked.
669 vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end, int flags)
672 vm_pindex_t tstart, tend;
678 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
679 if (object->type != OBJT_VNODE ||
680 (object->flags & OBJ_MIGHTBEDIRTY) == 0)
683 pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
684 pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
686 vm_object_set_flag(object, OBJ_CLEANING);
695 vm_page_lock_queues();
697 * If the caller is smart and only msync()s a range he knows is
698 * dirty, we may be able to avoid an object scan. This results in
699 * a phenominal improvement in performance. We cannot do this
700 * as a matter of course because the object may be huge - e.g.
701 * the size might be in the gigabytes or terrabytes.
703 if (msync_flush_flags & MSYNC_FLUSH_HARDSEQ) {
708 scanreset = object->resident_page_count / EASY_SCAN_FACTOR;
711 pagerflags |= VM_PAGER_IGNORE_CLEANCHK;
713 scanlimit = scanreset;
715 while (tscan < tend) {
716 curgeneration = object->generation;
717 p = vm_page_lookup(object, tscan);
718 if (p == NULL || p->valid == 0 ||
719 (p->queue - p->pc) == PQ_CACHE) {
720 if (--scanlimit == 0)
725 vm_page_test_dirty(p);
726 if ((p->dirty & p->valid) == 0) {
727 if (--scanlimit == 0)
733 * If we have been asked to skip nosync pages and
734 * this is a nosync page, we can't continue.
736 if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
737 if (--scanlimit == 0)
742 scanlimit = scanreset;
745 * This returns 0 if it was unable to busy the first
746 * page (i.e. had to sleep).
748 tscan += vm_object_page_collect_flush(object, p, curgeneration, pagerflags);
752 * If everything was dirty and we flushed it successfully,
753 * and the requested range is not the entire object, we
754 * don't have to mess with CLEANCHK or MIGHTBEDIRTY and can
755 * return immediately.
757 if (tscan >= tend && (tstart || tend < object->size)) {
758 vm_page_unlock_queues();
759 vm_object_clear_flag(object, OBJ_CLEANING);
762 pagerflags &= ~VM_PAGER_IGNORE_CLEANCHK;
766 * Generally set CLEANCHK interlock and make the page read-only so
767 * we can then clear the object flags.
769 * However, if this is a nosync mmap then the object is likely to
770 * stay dirty so do not mess with the page and do not clear the
774 TAILQ_FOREACH(p, &object->memq, listq) {
775 vm_page_flag_set(p, PG_CLEANCHK);
776 if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC))
779 pmap_page_protect(p, VM_PROT_READ);
782 if (clearobjflags && (tstart == 0) && (tend == object->size)) {
785 vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
786 if (object->type == OBJT_VNODE &&
787 (vp = (struct vnode *)object->handle) != NULL) {
789 if (vp->v_iflag & VI_OBJDIRTY)
790 vp->v_iflag &= ~VI_OBJDIRTY;
796 curgeneration = object->generation;
798 for (p = TAILQ_FIRST(&object->memq); p; p = np) {
801 np = TAILQ_NEXT(p, listq);
805 if (((p->flags & PG_CLEANCHK) == 0) ||
806 (pi < tstart) || (pi >= tend) ||
808 ((p->queue - p->pc) == PQ_CACHE)) {
809 vm_page_flag_clear(p, PG_CLEANCHK);
813 vm_page_test_dirty(p);
814 if ((p->dirty & p->valid) == 0) {
815 vm_page_flag_clear(p, PG_CLEANCHK);
820 * If we have been asked to skip nosync pages and this is a
821 * nosync page, skip it. Note that the object flags were
822 * not cleared in this case so we do not have to set them.
824 if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
825 vm_page_flag_clear(p, PG_CLEANCHK);
829 n = vm_object_page_collect_flush(object, p,
830 curgeneration, pagerflags);
834 if (object->generation != curgeneration)
838 * Try to optimize the next page. If we can't we pick up
839 * our (random) scan where we left off.
841 if (msync_flush_flags & MSYNC_FLUSH_SOFTSEQ) {
842 if ((p = vm_page_lookup(object, pi + n)) != NULL)
846 vm_page_unlock_queues();
848 VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
851 vm_object_clear_flag(object, OBJ_CLEANING);
856 vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags)
864 vm_page_t maf[vm_pageout_page_count];
865 vm_page_t mab[vm_pageout_page_count];
866 vm_page_t ma[vm_pageout_page_count];
868 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
870 while (vm_page_sleep_if_busy(p, TRUE, "vpcwai")) {
871 vm_page_lock_queues();
872 if (object->generation != curgeneration) {
877 for(i = 1; i < vm_pageout_page_count; i++) {
880 if ((tp = vm_page_lookup(object, pi + i)) != NULL) {
881 if ((tp->flags & PG_BUSY) ||
882 ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
883 (tp->flags & PG_CLEANCHK) == 0) ||
886 if((tp->queue - tp->pc) == PQ_CACHE) {
887 vm_page_flag_clear(tp, PG_CLEANCHK);
890 vm_page_test_dirty(tp);
891 if ((tp->dirty & tp->valid) == 0) {
892 vm_page_flag_clear(tp, PG_CLEANCHK);
903 chkb = vm_pageout_page_count - maxf;
905 for(i = 1; i < chkb;i++) {
908 if ((tp = vm_page_lookup(object, pi - i)) != NULL) {
909 if ((tp->flags & PG_BUSY) ||
910 ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
911 (tp->flags & PG_CLEANCHK) == 0) ||
914 if ((tp->queue - tp->pc) == PQ_CACHE) {
915 vm_page_flag_clear(tp, PG_CLEANCHK);
918 vm_page_test_dirty(tp);
919 if ((tp->dirty & tp->valid) == 0) {
920 vm_page_flag_clear(tp, PG_CLEANCHK);
931 for(i = 0; i < maxb; i++) {
932 int index = (maxb - i) - 1;
934 vm_page_flag_clear(ma[index], PG_CLEANCHK);
936 vm_page_flag_clear(p, PG_CLEANCHK);
938 for(i = 0; i < maxf; i++) {
939 int index = (maxb + i) + 1;
941 vm_page_flag_clear(ma[index], PG_CLEANCHK);
943 runlen = maxb + maxf + 1;
945 vm_pageout_flush(ma, runlen, pagerflags);
946 for (i = 0; i < runlen; i++) {
947 if (ma[i]->valid & ma[i]->dirty) {
948 pmap_page_protect(ma[i], VM_PROT_READ);
949 vm_page_flag_set(ma[i], PG_CLEANCHK);
952 * maxf will end up being the actual number of pages
953 * we wrote out contiguously, non-inclusive of the
954 * first page. We do not count look-behind pages.
956 if (i >= maxb + 1 && (maxf > i - maxb - 1))
964 * Note that there is absolutely no sense in writing out
965 * anonymous objects, so we track down the vnode object
967 * We invalidate (remove) all pages from the address space
968 * for semantic correctness.
970 * Note: certain anonymous maps, such as MAP_NOSYNC maps,
971 * may start out with a NULL object.
974 vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size,
975 boolean_t syncio, boolean_t invalidate)
977 vm_object_t backing_object;
983 VM_OBJECT_LOCK(object);
984 while ((backing_object = object->backing_object) != NULL) {
985 VM_OBJECT_LOCK(backing_object);
986 offset += object->backing_object_offset;
987 VM_OBJECT_UNLOCK(object);
988 object = backing_object;
989 if (object->size < OFF_TO_IDX(offset + size))
990 size = IDX_TO_OFF(object->size) - offset;
993 * Flush pages if writing is allowed, invalidate them
994 * if invalidation requested. Pages undergoing I/O
995 * will be ignored by vm_object_page_remove().
997 * We cannot lock the vnode and then wait for paging
998 * to complete without deadlocking against vm_fault.
999 * Instead we simply call vm_object_page_remove() and
1000 * allow it to block internally on a page-by-page
1001 * basis when it encounters pages undergoing async
1004 if (object->type == OBJT_VNODE &&
1005 (object->flags & OBJ_MIGHTBEDIRTY) != 0) {
1007 vp = object->handle;
1008 VM_OBJECT_UNLOCK(object);
1009 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1010 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
1011 flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
1012 flags |= invalidate ? OBJPC_INVAL : 0;
1013 VM_OBJECT_LOCK(object);
1014 vm_object_page_clean(object,
1016 OFF_TO_IDX(offset + size + PAGE_MASK),
1018 VM_OBJECT_UNLOCK(object);
1019 VOP_UNLOCK(vp, 0, curthread);
1020 VFS_UNLOCK_GIANT(vfslocked);
1021 VM_OBJECT_LOCK(object);
1023 if ((object->type == OBJT_VNODE ||
1024 object->type == OBJT_DEVICE) && invalidate) {
1026 purge = old_msync || (object->type == OBJT_DEVICE);
1027 vm_object_page_remove(object,
1029 OFF_TO_IDX(offset + size + PAGE_MASK),
1030 purge ? FALSE : TRUE);
1032 VM_OBJECT_UNLOCK(object);
1036 * vm_object_madvise:
1038 * Implements the madvise function at the object/page level.
1040 * MADV_WILLNEED (any object)
1042 * Activate the specified pages if they are resident.
1044 * MADV_DONTNEED (any object)
1046 * Deactivate the specified pages if they are resident.
1048 * MADV_FREE (OBJT_DEFAULT/OBJT_SWAP objects,
1049 * OBJ_ONEMAPPING only)
1051 * Deactivate and clean the specified pages if they are
1052 * resident. This permits the process to reuse the pages
1053 * without faulting or the kernel to reclaim the pages
1057 vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
1059 vm_pindex_t end, tpindex;
1060 vm_object_t backing_object, tobject;
1065 VM_OBJECT_LOCK(object);
1066 end = pindex + count;
1068 * Locate and adjust resident pages
1070 for (; pindex < end; pindex += 1) {
1076 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
1077 * and those pages must be OBJ_ONEMAPPING.
1079 if (advise == MADV_FREE) {
1080 if ((tobject->type != OBJT_DEFAULT &&
1081 tobject->type != OBJT_SWAP) ||
1082 (tobject->flags & OBJ_ONEMAPPING) == 0) {
1083 goto unlock_tobject;
1086 m = vm_page_lookup(tobject, tpindex);
1089 * There may be swap even if there is no backing page
1091 if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1092 swap_pager_freespace(tobject, tpindex, 1);
1096 backing_object = tobject->backing_object;
1097 if (backing_object == NULL)
1098 goto unlock_tobject;
1099 VM_OBJECT_LOCK(backing_object);
1100 tpindex += OFF_TO_IDX(tobject->backing_object_offset);
1101 if (tobject != object)
1102 VM_OBJECT_UNLOCK(tobject);
1103 tobject = backing_object;
1107 * If the page is busy or not in a normal active state,
1108 * we skip it. If the page is not managed there are no
1109 * page queues to mess with. Things can break if we mess
1110 * with pages in any of the below states.
1112 vm_page_lock_queues();
1113 if (m->hold_count ||
1115 (m->flags & PG_UNMANAGED) ||
1116 m->valid != VM_PAGE_BITS_ALL) {
1117 vm_page_unlock_queues();
1118 goto unlock_tobject;
1120 if ((m->flags & PG_BUSY) || m->busy) {
1121 vm_page_flag_set(m, PG_WANTED | PG_REFERENCED);
1122 if (object != tobject)
1123 VM_OBJECT_UNLOCK(object);
1124 VM_OBJECT_UNLOCK(tobject);
1125 msleep(m, &vm_page_queue_mtx, PDROP | PVM, "madvpo", 0);
1126 VM_OBJECT_LOCK(object);
1129 if (advise == MADV_WILLNEED) {
1130 vm_page_activate(m);
1131 } else if (advise == MADV_DONTNEED) {
1132 vm_page_dontneed(m);
1133 } else if (advise == MADV_FREE) {
1135 * Mark the page clean. This will allow the page
1136 * to be freed up by the system. However, such pages
1137 * are often reused quickly by malloc()/free()
1138 * so we do not do anything that would cause
1139 * a page fault if we can help it.
1141 * Specifically, we do not try to actually free
1142 * the page now nor do we try to put it in the
1143 * cache (which would cause a page fault on reuse).
1145 * But we do make the page is freeable as we
1146 * can without actually taking the step of unmapping
1149 pmap_clear_modify(m);
1152 vm_page_dontneed(m);
1154 vm_page_unlock_queues();
1155 if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1156 swap_pager_freespace(tobject, tpindex, 1);
1158 if (tobject != object)
1159 VM_OBJECT_UNLOCK(tobject);
1161 VM_OBJECT_UNLOCK(object);
1167 * Create a new object which is backed by the
1168 * specified existing object range. The source
1169 * object reference is deallocated.
1171 * The new object and offset into that object
1172 * are returned in the source parameters.
1176 vm_object_t *object, /* IN/OUT */
1177 vm_ooffset_t *offset, /* IN/OUT */
1186 * Don't create the new object if the old object isn't shared.
1188 if (source != NULL) {
1189 VM_OBJECT_LOCK(source);
1190 if (source->ref_count == 1 &&
1191 source->handle == NULL &&
1192 (source->type == OBJT_DEFAULT ||
1193 source->type == OBJT_SWAP)) {
1194 VM_OBJECT_UNLOCK(source);
1197 VM_OBJECT_UNLOCK(source);
1201 * Allocate a new object with the given length.
1203 result = vm_object_allocate(OBJT_DEFAULT, length);
1206 * The new object shadows the source object, adding a reference to it.
1207 * Our caller changes his reference to point to the new object,
1208 * removing a reference to the source object. Net result: no change
1209 * of reference count.
1211 * Try to optimize the result object's page color when shadowing
1212 * in order to maintain page coloring consistency in the combined
1215 result->backing_object = source;
1217 * Store the offset into the source object, and fix up the offset into
1220 result->backing_object_offset = *offset;
1221 if (source != NULL) {
1222 VM_OBJECT_LOCK(source);
1223 LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1224 source->shadow_count++;
1225 source->generation++;
1226 if (length < source->size)
1227 length = source->size;
1228 if (length > PQ_L2_SIZE / 3 + PQ_PRIME1 ||
1229 source->generation > 1)
1230 length = PQ_L2_SIZE / 3 + PQ_PRIME1;
1231 result->pg_color = (source->pg_color +
1232 length * source->generation) & PQ_L2_MASK;
1233 result->flags |= source->flags & OBJ_NEEDGIANT;
1234 VM_OBJECT_UNLOCK(source);
1235 next_index = (result->pg_color + PQ_L2_SIZE / 3 + PQ_PRIME1) &
1241 * Return the new things
1250 * Split the pages in a map entry into a new object. This affords
1251 * easier removal of unused pages, and keeps object inheritance from
1252 * being a negative impact on memory usage.
1255 vm_object_split(vm_map_entry_t entry)
1258 vm_object_t orig_object, new_object, source;
1259 vm_pindex_t offidxstart, offidxend;
1260 vm_size_t idx, size;
1262 orig_object = entry->object.vm_object;
1263 if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1265 if (orig_object->ref_count <= 1)
1267 VM_OBJECT_UNLOCK(orig_object);
1269 offidxstart = OFF_TO_IDX(entry->offset);
1270 offidxend = offidxstart + OFF_TO_IDX(entry->end - entry->start);
1271 size = offidxend - offidxstart;
1274 * If swap_pager_copy() is later called, it will convert new_object
1275 * into a swap object.
1277 new_object = vm_object_allocate(OBJT_DEFAULT, size);
1279 VM_OBJECT_LOCK(new_object);
1280 VM_OBJECT_LOCK(orig_object);
1281 source = orig_object->backing_object;
1282 if (source != NULL) {
1283 VM_OBJECT_LOCK(source);
1284 LIST_INSERT_HEAD(&source->shadow_head,
1285 new_object, shadow_list);
1286 source->shadow_count++;
1287 source->generation++;
1288 vm_object_reference_locked(source); /* for new_object */
1289 vm_object_clear_flag(source, OBJ_ONEMAPPING);
1290 VM_OBJECT_UNLOCK(source);
1291 new_object->backing_object_offset =
1292 orig_object->backing_object_offset + entry->offset;
1293 new_object->backing_object = source;
1295 new_object->flags |= orig_object->flags & OBJ_NEEDGIANT;
1296 vm_page_lock_queues();
1297 for (idx = 0; idx < size; idx++) {
1299 m = vm_page_lookup(orig_object, offidxstart + idx);
1304 * We must wait for pending I/O to complete before we can
1307 * We do not have to VM_PROT_NONE the page as mappings should
1308 * not be changed by this operation.
1310 if ((m->flags & PG_BUSY) || m->busy) {
1311 vm_page_flag_set(m, PG_WANTED | PG_REFERENCED);
1312 VM_OBJECT_UNLOCK(orig_object);
1313 VM_OBJECT_UNLOCK(new_object);
1314 msleep(m, &vm_page_queue_mtx, PDROP | PVM, "spltwt", 0);
1315 VM_OBJECT_LOCK(new_object);
1316 VM_OBJECT_LOCK(orig_object);
1317 vm_page_lock_queues();
1320 vm_page_rename(m, new_object, idx);
1321 /* page automatically made dirty by rename and cache handled */
1324 vm_page_unlock_queues();
1325 if (orig_object->type == OBJT_SWAP) {
1327 * swap_pager_copy() can sleep, in which case the orig_object's
1328 * and new_object's locks are released and reacquired.
1330 swap_pager_copy(orig_object, new_object, offidxstart, 0);
1332 VM_OBJECT_UNLOCK(orig_object);
1333 vm_page_lock_queues();
1334 TAILQ_FOREACH(m, &new_object->memq, listq)
1336 vm_page_unlock_queues();
1337 VM_OBJECT_UNLOCK(new_object);
1338 entry->object.vm_object = new_object;
1339 entry->offset = 0LL;
1340 vm_object_deallocate(orig_object);
1341 VM_OBJECT_LOCK(new_object);
1344 #define OBSC_TEST_ALL_SHADOWED 0x0001
1345 #define OBSC_COLLAPSE_NOWAIT 0x0002
1346 #define OBSC_COLLAPSE_WAIT 0x0004
1349 vm_object_backing_scan(vm_object_t object, int op)
1353 vm_object_t backing_object;
1354 vm_pindex_t backing_offset_index;
1356 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1357 VM_OBJECT_LOCK_ASSERT(object->backing_object, MA_OWNED);
1359 backing_object = object->backing_object;
1360 backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
1363 * Initial conditions
1365 if (op & OBSC_TEST_ALL_SHADOWED) {
1367 * We do not want to have to test for the existence of
1368 * swap pages in the backing object. XXX but with the
1369 * new swapper this would be pretty easy to do.
1371 * XXX what about anonymous MAP_SHARED memory that hasn't
1372 * been ZFOD faulted yet? If we do not test for this, the
1373 * shadow test may succeed! XXX
1375 if (backing_object->type != OBJT_DEFAULT) {
1379 if (op & OBSC_COLLAPSE_WAIT) {
1380 vm_object_set_flag(backing_object, OBJ_DEAD);
1386 p = TAILQ_FIRST(&backing_object->memq);
1388 vm_page_t next = TAILQ_NEXT(p, listq);
1389 vm_pindex_t new_pindex = p->pindex - backing_offset_index;
1391 if (op & OBSC_TEST_ALL_SHADOWED) {
1395 * Ignore pages outside the parent object's range
1396 * and outside the parent object's mapping of the
1399 * note that we do not busy the backing object's
1403 p->pindex < backing_offset_index ||
1404 new_pindex >= object->size
1411 * See if the parent has the page or if the parent's
1412 * object pager has the page. If the parent has the
1413 * page but the page is not valid, the parent's
1414 * object pager must have the page.
1416 * If this fails, the parent does not completely shadow
1417 * the object and we might as well give up now.
1420 pp = vm_page_lookup(object, new_pindex);
1422 (pp == NULL || pp->valid == 0) &&
1423 !vm_pager_has_page(object, new_pindex, NULL, NULL)
1431 * Check for busy page
1433 if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
1436 if (op & OBSC_COLLAPSE_NOWAIT) {
1437 if ((p->flags & PG_BUSY) ||
1443 } else if (op & OBSC_COLLAPSE_WAIT) {
1444 if ((p->flags & PG_BUSY) || p->busy) {
1445 vm_page_lock_queues();
1447 PG_WANTED | PG_REFERENCED);
1448 VM_OBJECT_UNLOCK(backing_object);
1449 VM_OBJECT_UNLOCK(object);
1450 msleep(p, &vm_page_queue_mtx,
1451 PDROP | PVM, "vmocol", 0);
1452 VM_OBJECT_LOCK(object);
1453 VM_OBJECT_LOCK(backing_object);
1455 * If we slept, anything could have
1456 * happened. Since the object is
1457 * marked dead, the backing offset
1458 * should not have changed so we
1459 * just restart our scan.
1461 p = TAILQ_FIRST(&backing_object->memq);
1467 p->object == backing_object,
1468 ("vm_object_backing_scan: object mismatch")
1472 * Destroy any associated swap
1474 if (backing_object->type == OBJT_SWAP) {
1475 swap_pager_freespace(
1483 p->pindex < backing_offset_index ||
1484 new_pindex >= object->size
1487 * Page is out of the parent object's range, we
1488 * can simply destroy it.
1490 vm_page_lock_queues();
1493 vm_page_unlock_queues();
1498 pp = vm_page_lookup(object, new_pindex);
1501 vm_pager_has_page(object, new_pindex, NULL, NULL)
1504 * page already exists in parent OR swap exists
1505 * for this location in the parent. Destroy
1506 * the original page from the backing object.
1508 * Leave the parent's page alone
1510 vm_page_lock_queues();
1513 vm_page_unlock_queues();
1519 * Page does not exist in parent, rename the
1520 * page from the backing object to the main object.
1522 * If the page was mapped to a process, it can remain
1523 * mapped through the rename.
1525 vm_page_lock_queues();
1526 vm_page_rename(p, object, new_pindex);
1527 vm_page_unlock_queues();
1528 /* page automatically made dirty by rename */
1537 * this version of collapse allows the operation to occur earlier and
1538 * when paging_in_progress is true for an object... This is not a complete
1539 * operation, but should plug 99.9% of the rest of the leaks.
1542 vm_object_qcollapse(vm_object_t object)
1544 vm_object_t backing_object = object->backing_object;
1546 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1547 VM_OBJECT_LOCK_ASSERT(backing_object, MA_OWNED);
1549 if (backing_object->ref_count != 1)
1552 vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
1556 * vm_object_collapse:
1558 * Collapse an object with the object backing it.
1559 * Pages in the backing object are moved into the
1560 * parent, and the backing object is deallocated.
1563 vm_object_collapse(vm_object_t object)
1565 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1568 vm_object_t backing_object;
1571 * Verify that the conditions are right for collapse:
1573 * The object exists and the backing object exists.
1575 if ((backing_object = object->backing_object) == NULL)
1579 * we check the backing object first, because it is most likely
1582 VM_OBJECT_LOCK(backing_object);
1583 if (backing_object->handle != NULL ||
1584 (backing_object->type != OBJT_DEFAULT &&
1585 backing_object->type != OBJT_SWAP) ||
1586 (backing_object->flags & OBJ_DEAD) ||
1587 object->handle != NULL ||
1588 (object->type != OBJT_DEFAULT &&
1589 object->type != OBJT_SWAP) ||
1590 (object->flags & OBJ_DEAD)) {
1591 VM_OBJECT_UNLOCK(backing_object);
1596 object->paging_in_progress != 0 ||
1597 backing_object->paging_in_progress != 0
1599 vm_object_qcollapse(object);
1600 VM_OBJECT_UNLOCK(backing_object);
1604 * We know that we can either collapse the backing object (if
1605 * the parent is the only reference to it) or (perhaps) have
1606 * the parent bypass the object if the parent happens to shadow
1607 * all the resident pages in the entire backing object.
1609 * This is ignoring pager-backed pages such as swap pages.
1610 * vm_object_backing_scan fails the shadowing test in this
1613 if (backing_object->ref_count == 1) {
1615 * If there is exactly one reference to the backing
1616 * object, we can collapse it into the parent.
1618 vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1621 * Move the pager from backing_object to object.
1623 if (backing_object->type == OBJT_SWAP) {
1625 * swap_pager_copy() can sleep, in which case
1626 * the backing_object's and object's locks are
1627 * released and reacquired.
1632 OFF_TO_IDX(object->backing_object_offset), TRUE);
1635 * Object now shadows whatever backing_object did.
1636 * Note that the reference to
1637 * backing_object->backing_object moves from within
1638 * backing_object to within object.
1640 LIST_REMOVE(object, shadow_list);
1641 backing_object->shadow_count--;
1642 backing_object->generation++;
1643 if (backing_object->backing_object) {
1644 VM_OBJECT_LOCK(backing_object->backing_object);
1645 LIST_REMOVE(backing_object, shadow_list);
1647 &backing_object->backing_object->shadow_head,
1648 object, shadow_list);
1650 * The shadow_count has not changed.
1652 backing_object->backing_object->generation++;
1653 VM_OBJECT_UNLOCK(backing_object->backing_object);
1655 object->backing_object = backing_object->backing_object;
1656 object->backing_object_offset +=
1657 backing_object->backing_object_offset;
1660 * Discard backing_object.
1662 * Since the backing object has no pages, no pager left,
1663 * and no object references within it, all that is
1664 * necessary is to dispose of it.
1666 KASSERT(backing_object->ref_count == 1, ("backing_object %p was somehow re-referenced during collapse!", backing_object));
1667 VM_OBJECT_UNLOCK(backing_object);
1669 mtx_lock(&vm_object_list_mtx);
1675 mtx_unlock(&vm_object_list_mtx);
1677 uma_zfree(obj_zone, backing_object);
1681 vm_object_t new_backing_object;
1684 * If we do not entirely shadow the backing object,
1685 * there is nothing we can do so we give up.
1687 if (vm_object_backing_scan(object, OBSC_TEST_ALL_SHADOWED) == 0) {
1688 VM_OBJECT_UNLOCK(backing_object);
1693 * Make the parent shadow the next object in the
1694 * chain. Deallocating backing_object will not remove
1695 * it, since its reference count is at least 2.
1697 LIST_REMOVE(object, shadow_list);
1698 backing_object->shadow_count--;
1699 backing_object->generation++;
1701 new_backing_object = backing_object->backing_object;
1702 if ((object->backing_object = new_backing_object) != NULL) {
1703 VM_OBJECT_LOCK(new_backing_object);
1705 &new_backing_object->shadow_head,
1709 new_backing_object->shadow_count++;
1710 new_backing_object->generation++;
1711 vm_object_reference_locked(new_backing_object);
1712 VM_OBJECT_UNLOCK(new_backing_object);
1713 object->backing_object_offset +=
1714 backing_object->backing_object_offset;
1718 * Drop the reference count on backing_object. Since
1719 * its ref_count was at least 2, it will not vanish.
1721 backing_object->ref_count--;
1722 VM_OBJECT_UNLOCK(backing_object);
1727 * Try again with this object's new backing object.
1733 * vm_object_page_remove:
1735 * Removes all physical pages in the given range from the
1736 * object's list of pages. If the range's end is zero, all
1737 * physical pages from the range's start to the end of the object
1740 * The object must be locked.
1743 vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
1744 boolean_t clean_only)
1748 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1749 if (object->resident_page_count == 0)
1753 * Since physically-backed objects do not use managed pages, we can't
1754 * remove pages from the object (we must instead remove the page
1755 * references, and then destroy the object).
1757 KASSERT(object->type != OBJT_PHYS,
1758 ("attempt to remove pages from a physical object"));
1760 vm_object_pip_add(object, 1);
1762 vm_page_lock_queues();
1763 if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
1764 if (p->pindex < start) {
1765 p = vm_page_splay(start, object->root);
1766 if ((object->root = p)->pindex < start)
1767 p = TAILQ_NEXT(p, listq);
1771 * Assert: the variable p is either (1) the page with the
1772 * least pindex greater than or equal to the parameter pindex
1776 p != NULL && (p->pindex < end || end == 0);
1778 next = TAILQ_NEXT(p, listq);
1780 if (p->wire_count != 0) {
1786 if (vm_page_sleep_if_busy(p, TRUE, "vmopar"))
1788 if (clean_only && p->valid) {
1789 pmap_page_protect(p, VM_PROT_READ | VM_PROT_EXECUTE);
1790 if (p->valid & p->dirty)
1796 vm_page_unlock_queues();
1797 vm_object_pip_wakeup(object);
1801 * Routine: vm_object_coalesce
1802 * Function: Coalesces two objects backing up adjoining
1803 * regions of memory into a single object.
1805 * returns TRUE if objects were combined.
1807 * NOTE: Only works at the moment if the second object is NULL -
1808 * if it's not, which object do we lock first?
1811 * prev_object First object to coalesce
1812 * prev_offset Offset into prev_object
1813 * prev_size Size of reference to prev_object
1814 * next_size Size of reference to the second object
1817 * The object must *not* be locked.
1820 vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset,
1821 vm_size_t prev_size, vm_size_t next_size)
1823 vm_pindex_t next_pindex;
1825 if (prev_object == NULL)
1827 VM_OBJECT_LOCK(prev_object);
1828 if (prev_object->type != OBJT_DEFAULT &&
1829 prev_object->type != OBJT_SWAP) {
1830 VM_OBJECT_UNLOCK(prev_object);
1835 * Try to collapse the object first
1837 vm_object_collapse(prev_object);
1840 * Can't coalesce if: . more than one reference . paged out . shadows
1841 * another object . has a copy elsewhere (any of which mean that the
1842 * pages not mapped to prev_entry may be in use anyway)
1844 if (prev_object->backing_object != NULL) {
1845 VM_OBJECT_UNLOCK(prev_object);
1849 prev_size >>= PAGE_SHIFT;
1850 next_size >>= PAGE_SHIFT;
1851 next_pindex = OFF_TO_IDX(prev_offset) + prev_size;
1853 if ((prev_object->ref_count > 1) &&
1854 (prev_object->size != next_pindex)) {
1855 VM_OBJECT_UNLOCK(prev_object);
1860 * Remove any pages that may still be in the object from a previous
1863 if (next_pindex < prev_object->size) {
1864 vm_object_page_remove(prev_object,
1866 next_pindex + next_size, FALSE);
1867 if (prev_object->type == OBJT_SWAP)
1868 swap_pager_freespace(prev_object,
1869 next_pindex, next_size);
1873 * Extend the object if necessary.
1875 if (next_pindex + next_size > prev_object->size)
1876 prev_object->size = next_pindex + next_size;
1878 VM_OBJECT_UNLOCK(prev_object);
1883 vm_object_set_writeable_dirty(vm_object_t object)
1887 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1888 if ((object->flags & (OBJ_MIGHTBEDIRTY|OBJ_WRITEABLE)) ==
1889 (OBJ_MIGHTBEDIRTY|OBJ_WRITEABLE))
1891 vm_object_set_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
1892 if (object->type == OBJT_VNODE &&
1893 (vp = (struct vnode *)object->handle) != NULL) {
1895 vp->v_iflag |= VI_OBJDIRTY;
1900 #include "opt_ddb.h"
1902 #include <sys/kernel.h>
1904 #include <sys/cons.h>
1906 #include <ddb/ddb.h>
1909 _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
1912 vm_map_entry_t tmpe;
1920 tmpe = map->header.next;
1921 entcount = map->nentries;
1922 while (entcount-- && (tmpe != &map->header)) {
1923 if (_vm_object_in_map(map, object, tmpe)) {
1928 } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
1929 tmpm = entry->object.sub_map;
1930 tmpe = tmpm->header.next;
1931 entcount = tmpm->nentries;
1932 while (entcount-- && tmpe != &tmpm->header) {
1933 if (_vm_object_in_map(tmpm, object, tmpe)) {
1938 } else if ((obj = entry->object.vm_object) != NULL) {
1939 for (; obj; obj = obj->backing_object)
1940 if (obj == object) {
1948 vm_object_in_map(vm_object_t object)
1952 /* sx_slock(&allproc_lock); */
1953 LIST_FOREACH(p, &allproc, p_list) {
1954 if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1956 if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
1957 /* sx_sunlock(&allproc_lock); */
1961 /* sx_sunlock(&allproc_lock); */
1962 if (_vm_object_in_map(kernel_map, object, 0))
1964 if (_vm_object_in_map(kmem_map, object, 0))
1966 if (_vm_object_in_map(pager_map, object, 0))
1968 if (_vm_object_in_map(buffer_map, object, 0))
1973 DB_SHOW_COMMAND(vmochk, vm_object_check)
1978 * make sure that internal objs are in a map somewhere
1979 * and none have zero ref counts.
1981 TAILQ_FOREACH(object, &vm_object_list, object_list) {
1982 if (object->handle == NULL &&
1983 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
1984 if (object->ref_count == 0) {
1985 db_printf("vmochk: internal obj has zero ref count: %ld\n",
1986 (long)object->size);
1988 if (!vm_object_in_map(object)) {
1990 "vmochk: internal obj is not in a map: "
1991 "ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
1992 object->ref_count, (u_long)object->size,
1993 (u_long)object->size,
1994 (void *)object->backing_object);
2001 * vm_object_print: [ debug ]
2003 DB_SHOW_COMMAND(object, vm_object_print_static)
2005 /* XXX convert args. */
2006 vm_object_t object = (vm_object_t)addr;
2007 boolean_t full = have_addr;
2011 /* XXX count is an (unused) arg. Avoid shadowing it. */
2012 #define count was_count
2020 "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x\n",
2021 object, (int)object->type, (uintmax_t)object->size,
2022 object->resident_page_count, object->ref_count, object->flags);
2023 db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n",
2024 object->shadow_count,
2025 object->backing_object ? object->backing_object->ref_count : 0,
2026 object->backing_object, (uintmax_t)object->backing_object_offset);
2033 TAILQ_FOREACH(p, &object->memq, listq) {
2035 db_iprintf("memory:=");
2036 else if (count == 6) {
2044 db_printf("(off=0x%jx,page=0x%jx)",
2045 (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p));
2055 /* XXX need this non-static entry for calling from vm_map_print. */
2058 /* db_expr_t */ long addr,
2059 boolean_t have_addr,
2060 /* db_expr_t */ long count,
2063 vm_object_print_static(addr, have_addr, count, modif);
2066 DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
2072 TAILQ_FOREACH(object, &vm_object_list, object_list) {
2073 vm_pindex_t idx, fidx;
2075 vm_paddr_t pa = -1, padiff;
2079 db_printf("new object: %p\n", (void *)object);
2089 osize = object->size;
2092 for (idx = 0; idx < osize; idx++) {
2093 m = vm_page_lookup(object, idx);
2096 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2097 (long)fidx, rcount, (long)pa);
2112 (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
2117 padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
2118 padiff >>= PAGE_SHIFT;
2119 padiff &= PQ_L2_MASK;
2121 pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
2125 db_printf(" index(%ld)run(%d)pa(0x%lx)",
2126 (long)fidx, rcount, (long)pa);
2127 db_printf("pd(%ld)\n", (long)padiff);
2137 pa = VM_PAGE_TO_PHYS(m);
2141 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2142 (long)fidx, rcount, (long)pa);