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;
160 static int vm_object_zinit(void *mem, int size, int flags);
163 static void vm_object_zdtor(void *mem, int size, void *arg);
166 vm_object_zdtor(void *mem, int size, void *arg)
170 object = (vm_object_t)mem;
171 KASSERT(TAILQ_EMPTY(&object->memq),
172 ("object %p has resident pages",
174 KASSERT(object->paging_in_progress == 0,
175 ("object %p paging_in_progress = %d",
176 object, object->paging_in_progress));
177 KASSERT(object->resident_page_count == 0,
178 ("object %p resident_page_count = %d",
179 object, object->resident_page_count));
180 KASSERT(object->shadow_count == 0,
181 ("object %p shadow_count = %d",
182 object, object->shadow_count));
187 vm_object_zinit(void *mem, int size, int flags)
191 object = (vm_object_t)mem;
192 bzero(&object->mtx, sizeof(object->mtx));
193 VM_OBJECT_LOCK_INIT(object, "standard object");
195 /* These are true for any object that has been freed */
196 object->paging_in_progress = 0;
197 object->resident_page_count = 0;
198 object->shadow_count = 0;
203 _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
207 TAILQ_INIT(&object->memq);
208 LIST_INIT(&object->shadow_head);
213 object->generation = 1;
214 object->ref_count = 1;
216 if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
217 object->flags = OBJ_ONEMAPPING;
218 if (size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
219 incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
222 object->pg_color = next_index;
223 next_index = (object->pg_color + incr) & PQ_L2_MASK;
224 object->handle = NULL;
225 object->backing_object = NULL;
226 object->backing_object_offset = (vm_ooffset_t) 0;
228 mtx_lock(&vm_object_list_mtx);
229 TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
230 mtx_unlock(&vm_object_list_mtx);
236 * Initialize the VM objects module.
241 TAILQ_INIT(&vm_object_list);
242 mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF);
244 VM_OBJECT_LOCK_INIT(&kernel_object_store, "kernel object");
245 _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
248 VM_OBJECT_LOCK_INIT(&kmem_object_store, "kmem object");
249 _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
253 * The lock portion of struct vm_object must be type stable due
254 * to vm_pageout_fallback_object_lock locking a vm object
255 * without holding any references to it.
257 obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL,
263 vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM|UMA_ZONE_NOFREE);
267 vm_object_clear_flag(vm_object_t object, u_short bits)
270 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
271 object->flags &= ~bits;
275 vm_object_pip_add(vm_object_t object, short i)
278 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
279 object->paging_in_progress += i;
283 vm_object_pip_subtract(vm_object_t object, short i)
286 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
287 object->paging_in_progress -= i;
291 vm_object_pip_wakeup(vm_object_t object)
294 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
295 object->paging_in_progress--;
296 if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
297 vm_object_clear_flag(object, OBJ_PIPWNT);
303 vm_object_pip_wakeupn(vm_object_t object, short i)
306 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
308 object->paging_in_progress -= i;
309 if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
310 vm_object_clear_flag(object, OBJ_PIPWNT);
316 vm_object_pip_wait(vm_object_t object, char *waitid)
319 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
320 while (object->paging_in_progress) {
321 object->flags |= OBJ_PIPWNT;
322 msleep(object, VM_OBJECT_MTX(object), PVM, waitid, 0);
327 * vm_object_allocate:
329 * Returns a new object with the given size.
332 vm_object_allocate(objtype_t type, vm_pindex_t size)
336 object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK);
337 _vm_object_allocate(type, size, object);
343 * vm_object_reference:
345 * Gets another reference to the given object. Note: OBJ_DEAD
346 * objects can be referenced during final cleaning.
349 vm_object_reference(vm_object_t object)
356 VM_OBJECT_LOCK(object);
358 if (object->type == OBJT_VNODE) {
361 VM_OBJECT_UNLOCK(object);
362 for (flags = LK_INTERLOCK; vget(vp, flags, curthread);
364 printf("vm_object_reference: delay in vget\n");
366 VM_OBJECT_UNLOCK(object);
370 * vm_object_reference_locked:
372 * Gets another reference to the given object.
374 * The object must be locked.
377 vm_object_reference_locked(vm_object_t object)
381 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
382 KASSERT((object->flags & OBJ_DEAD) == 0,
383 ("vm_object_reference_locked: dead object referenced"));
385 if (object->type == OBJT_VNODE) {
392 * Handle deallocating an object of type OBJT_VNODE.
395 vm_object_vndeallocate(vm_object_t object)
397 struct vnode *vp = (struct vnode *) object->handle;
399 VFS_ASSERT_GIANT(vp->v_mount);
400 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
401 KASSERT(object->type == OBJT_VNODE,
402 ("vm_object_vndeallocate: not a vnode object"));
403 KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
405 if (object->ref_count == 0) {
406 vprint("vm_object_vndeallocate", vp);
407 panic("vm_object_vndeallocate: bad object reference count");
412 if (object->ref_count == 0) {
413 mp_fixme("Unlocked vflag access.");
414 vp->v_vflag &= ~VV_TEXT;
416 VM_OBJECT_UNLOCK(object);
418 * vrele may need a vop lock
424 * vm_object_deallocate:
426 * Release a reference to the specified object,
427 * gained either through a vm_object_allocate
428 * or a vm_object_reference call. When all references
429 * are gone, storage associated with this object
430 * may be relinquished.
432 * No object may be locked.
435 vm_object_deallocate(vm_object_t object)
439 while (object != NULL) {
442 * In general, the object should be locked when working with
443 * its type. In this case, in order to maintain proper lock
444 * ordering, an exception is possible because a vnode-backed
445 * object never changes its type.
448 if (object->type == OBJT_VNODE) {
449 struct vnode *vp = (struct vnode *) object->handle;
450 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
452 VM_OBJECT_LOCK(object);
453 if (object->type == OBJT_VNODE) {
454 vm_object_vndeallocate(object);
455 VFS_UNLOCK_GIANT(vfslocked);
459 KASSERT(object->ref_count != 0,
460 ("vm_object_deallocate: object deallocated too many times: %d", object->type));
463 * If the reference count goes to 0 we start calling
464 * vm_object_terminate() on the object chain.
465 * A ref count of 1 may be a special case depending on the
466 * shadow count being 0 or 1.
469 if (object->ref_count > 1) {
470 VM_OBJECT_UNLOCK(object);
472 } else if (object->ref_count == 1) {
473 if (object->shadow_count == 0) {
474 vm_object_set_flag(object, OBJ_ONEMAPPING);
475 } else if ((object->shadow_count == 1) &&
476 (object->handle == NULL) &&
477 (object->type == OBJT_DEFAULT ||
478 object->type == OBJT_SWAP)) {
481 robject = LIST_FIRST(&object->shadow_head);
482 KASSERT(robject != NULL,
483 ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
485 object->shadow_count));
486 if (!VM_OBJECT_TRYLOCK(robject)) {
488 * Avoid a potential deadlock.
491 VM_OBJECT_UNLOCK(object);
493 * More likely than not the thread
494 * holding robject's lock has lower
495 * priority than the current thread.
496 * Let the lower priority thread run.
498 tsleep(&proc0, PVM, "vmo_de", 1);
502 * Collapse object into its shadow unless its
503 * shadow is dead. In that case, object will
504 * be deallocated by the thread that is
505 * deallocating its shadow.
507 if ((robject->flags & OBJ_DEAD) == 0 &&
508 (robject->handle == NULL) &&
509 (robject->type == OBJT_DEFAULT ||
510 robject->type == OBJT_SWAP)) {
512 robject->ref_count++;
514 if (robject->paging_in_progress) {
515 VM_OBJECT_UNLOCK(object);
516 vm_object_pip_wait(robject,
518 VM_OBJECT_LOCK(object);
520 } else if (object->paging_in_progress) {
521 VM_OBJECT_UNLOCK(robject);
522 object->flags |= OBJ_PIPWNT;
524 VM_OBJECT_MTX(object),
525 PDROP | PVM, "objde2", 0);
526 VM_OBJECT_LOCK(robject);
527 VM_OBJECT_LOCK(object);
530 VM_OBJECT_UNLOCK(object);
531 if (robject->ref_count == 1) {
532 robject->ref_count--;
537 vm_object_collapse(object);
538 VM_OBJECT_UNLOCK(object);
541 VM_OBJECT_UNLOCK(robject);
543 VM_OBJECT_UNLOCK(object);
547 temp = object->backing_object;
549 VM_OBJECT_LOCK(temp);
550 LIST_REMOVE(object, shadow_list);
551 temp->shadow_count--;
553 VM_OBJECT_UNLOCK(temp);
554 object->backing_object = NULL;
557 * Don't double-terminate, we could be in a termination
558 * recursion due to the terminate having to sync data
561 if ((object->flags & OBJ_DEAD) == 0)
562 vm_object_terminate(object);
564 VM_OBJECT_UNLOCK(object);
570 * vm_object_terminate actually destroys the specified object, freeing
571 * up all previously used resources.
573 * The object must be locked.
574 * This routine may block.
577 vm_object_terminate(vm_object_t object)
581 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
584 * Make sure no one uses us.
586 vm_object_set_flag(object, OBJ_DEAD);
589 * wait for the pageout daemon to be done with the object
591 vm_object_pip_wait(object, "objtrm");
593 KASSERT(!object->paging_in_progress,
594 ("vm_object_terminate: pageout in progress"));
597 * Clean and free the pages, as appropriate. All references to the
598 * object are gone, so we don't need to lock it.
600 if (object->type == OBJT_VNODE) {
601 struct vnode *vp = (struct vnode *)object->handle;
604 * Clean pages and flush buffers.
606 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
607 VM_OBJECT_UNLOCK(object);
609 vinvalbuf(vp, V_SAVE, NULL, 0, 0);
611 VM_OBJECT_LOCK(object);
614 KASSERT(object->ref_count == 0,
615 ("vm_object_terminate: object with references, ref_count=%d",
619 * Now free any remaining pages. For internal objects, this also
620 * removes them from paging queues. Don't free wired pages, just
621 * remove them from the object.
623 vm_page_lock_queues();
624 while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
625 KASSERT(!p->busy && (p->flags & PG_BUSY) == 0,
626 ("vm_object_terminate: freeing busy page %p "
627 "p->busy = %d, p->flags %x\n", p, p->busy, p->flags));
628 if (p->wire_count == 0) {
635 vm_page_unlock_queues();
638 * Let the pager know object is dead.
640 vm_pager_deallocate(object);
641 VM_OBJECT_UNLOCK(object);
644 * Remove the object from the global object list.
646 mtx_lock(&vm_object_list_mtx);
647 TAILQ_REMOVE(&vm_object_list, object, object_list);
648 mtx_unlock(&vm_object_list_mtx);
651 * Free the space for the object.
653 uma_zfree(obj_zone, object);
657 * vm_object_page_clean
659 * Clean all dirty pages in the specified range of object. Leaves page
660 * on whatever queue it is currently on. If NOSYNC is set then do not
661 * write out pages with PG_NOSYNC set (originally comes from MAP_NOSYNC),
662 * leaving the object dirty.
664 * When stuffing pages asynchronously, allow clustering. XXX we need a
665 * synchronous clustering mode implementation.
667 * Odd semantics: if start == end, we clean everything.
669 * The object must be locked.
672 vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end, int flags)
675 vm_pindex_t tstart, tend;
681 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
682 if (object->type != OBJT_VNODE ||
683 (object->flags & OBJ_MIGHTBEDIRTY) == 0)
686 pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
687 pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
689 vm_object_set_flag(object, OBJ_CLEANING);
698 vm_page_lock_queues();
700 * If the caller is smart and only msync()s a range he knows is
701 * dirty, we may be able to avoid an object scan. This results in
702 * a phenominal improvement in performance. We cannot do this
703 * as a matter of course because the object may be huge - e.g.
704 * the size might be in the gigabytes or terrabytes.
706 if (msync_flush_flags & MSYNC_FLUSH_HARDSEQ) {
711 scanreset = object->resident_page_count / EASY_SCAN_FACTOR;
714 pagerflags |= VM_PAGER_IGNORE_CLEANCHK;
716 scanlimit = scanreset;
718 while (tscan < tend) {
719 curgeneration = object->generation;
720 p = vm_page_lookup(object, tscan);
721 if (p == NULL || p->valid == 0 ||
722 (p->queue - p->pc) == PQ_CACHE) {
723 if (--scanlimit == 0)
728 vm_page_test_dirty(p);
729 if ((p->dirty & p->valid) == 0) {
730 if (--scanlimit == 0)
736 * If we have been asked to skip nosync pages and
737 * this is a nosync page, we can't continue.
739 if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
740 if (--scanlimit == 0)
745 scanlimit = scanreset;
748 * This returns 0 if it was unable to busy the first
749 * page (i.e. had to sleep).
751 tscan += vm_object_page_collect_flush(object, p, curgeneration, pagerflags);
755 * If everything was dirty and we flushed it successfully,
756 * and the requested range is not the entire object, we
757 * don't have to mess with CLEANCHK or MIGHTBEDIRTY and can
758 * return immediately.
760 if (tscan >= tend && (tstart || tend < object->size)) {
761 vm_page_unlock_queues();
762 vm_object_clear_flag(object, OBJ_CLEANING);
765 pagerflags &= ~VM_PAGER_IGNORE_CLEANCHK;
769 * Generally set CLEANCHK interlock and make the page read-only so
770 * we can then clear the object flags.
772 * However, if this is a nosync mmap then the object is likely to
773 * stay dirty so do not mess with the page and do not clear the
777 TAILQ_FOREACH(p, &object->memq, listq) {
778 vm_page_flag_set(p, PG_CLEANCHK);
779 if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC))
782 pmap_page_protect(p, VM_PROT_READ);
785 if (clearobjflags && (tstart == 0) && (tend == object->size)) {
788 vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
789 if (object->type == OBJT_VNODE &&
790 (vp = (struct vnode *)object->handle) != NULL) {
792 if (vp->v_iflag & VI_OBJDIRTY)
793 vp->v_iflag &= ~VI_OBJDIRTY;
799 curgeneration = object->generation;
801 for (p = TAILQ_FIRST(&object->memq); p; p = np) {
804 np = TAILQ_NEXT(p, listq);
808 if (((p->flags & PG_CLEANCHK) == 0) ||
809 (pi < tstart) || (pi >= tend) ||
811 ((p->queue - p->pc) == PQ_CACHE)) {
812 vm_page_flag_clear(p, PG_CLEANCHK);
816 vm_page_test_dirty(p);
817 if ((p->dirty & p->valid) == 0) {
818 vm_page_flag_clear(p, PG_CLEANCHK);
823 * If we have been asked to skip nosync pages and this is a
824 * nosync page, skip it. Note that the object flags were
825 * not cleared in this case so we do not have to set them.
827 if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
828 vm_page_flag_clear(p, PG_CLEANCHK);
832 n = vm_object_page_collect_flush(object, p,
833 curgeneration, pagerflags);
837 if (object->generation != curgeneration)
841 * Try to optimize the next page. If we can't we pick up
842 * our (random) scan where we left off.
844 if (msync_flush_flags & MSYNC_FLUSH_SOFTSEQ) {
845 if ((p = vm_page_lookup(object, pi + n)) != NULL)
849 vm_page_unlock_queues();
851 VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
854 vm_object_clear_flag(object, OBJ_CLEANING);
859 vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags)
867 vm_page_t maf[vm_pageout_page_count];
868 vm_page_t mab[vm_pageout_page_count];
869 vm_page_t ma[vm_pageout_page_count];
871 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
873 while (vm_page_sleep_if_busy(p, TRUE, "vpcwai")) {
874 vm_page_lock_queues();
875 if (object->generation != curgeneration) {
880 for(i = 1; i < vm_pageout_page_count; i++) {
883 if ((tp = vm_page_lookup(object, pi + i)) != NULL) {
884 if ((tp->flags & PG_BUSY) ||
885 ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
886 (tp->flags & PG_CLEANCHK) == 0) ||
889 if((tp->queue - tp->pc) == PQ_CACHE) {
890 vm_page_flag_clear(tp, PG_CLEANCHK);
893 vm_page_test_dirty(tp);
894 if ((tp->dirty & tp->valid) == 0) {
895 vm_page_flag_clear(tp, PG_CLEANCHK);
906 chkb = vm_pageout_page_count - maxf;
908 for(i = 1; i < chkb;i++) {
911 if ((tp = vm_page_lookup(object, pi - i)) != NULL) {
912 if ((tp->flags & PG_BUSY) ||
913 ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
914 (tp->flags & PG_CLEANCHK) == 0) ||
917 if ((tp->queue - tp->pc) == PQ_CACHE) {
918 vm_page_flag_clear(tp, PG_CLEANCHK);
921 vm_page_test_dirty(tp);
922 if ((tp->dirty & tp->valid) == 0) {
923 vm_page_flag_clear(tp, PG_CLEANCHK);
934 for(i = 0; i < maxb; i++) {
935 int index = (maxb - i) - 1;
937 vm_page_flag_clear(ma[index], PG_CLEANCHK);
939 vm_page_flag_clear(p, PG_CLEANCHK);
941 for(i = 0; i < maxf; i++) {
942 int index = (maxb + i) + 1;
944 vm_page_flag_clear(ma[index], PG_CLEANCHK);
946 runlen = maxb + maxf + 1;
948 vm_pageout_flush(ma, runlen, pagerflags);
949 for (i = 0; i < runlen; i++) {
950 if (ma[i]->valid & ma[i]->dirty) {
951 pmap_page_protect(ma[i], VM_PROT_READ);
952 vm_page_flag_set(ma[i], PG_CLEANCHK);
955 * maxf will end up being the actual number of pages
956 * we wrote out contiguously, non-inclusive of the
957 * first page. We do not count look-behind pages.
959 if (i >= maxb + 1 && (maxf > i - maxb - 1))
967 * Note that there is absolutely no sense in writing out
968 * anonymous objects, so we track down the vnode object
970 * We invalidate (remove) all pages from the address space
971 * for semantic correctness.
973 * Note: certain anonymous maps, such as MAP_NOSYNC maps,
974 * may start out with a NULL object.
977 vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size,
978 boolean_t syncio, boolean_t invalidate)
980 vm_object_t backing_object;
986 VM_OBJECT_LOCK(object);
987 while ((backing_object = object->backing_object) != NULL) {
988 VM_OBJECT_LOCK(backing_object);
989 offset += object->backing_object_offset;
990 VM_OBJECT_UNLOCK(object);
991 object = backing_object;
992 if (object->size < OFF_TO_IDX(offset + size))
993 size = IDX_TO_OFF(object->size) - offset;
996 * Flush pages if writing is allowed, invalidate them
997 * if invalidation requested. Pages undergoing I/O
998 * will be ignored by vm_object_page_remove().
1000 * We cannot lock the vnode and then wait for paging
1001 * to complete without deadlocking against vm_fault.
1002 * Instead we simply call vm_object_page_remove() and
1003 * allow it to block internally on a page-by-page
1004 * basis when it encounters pages undergoing async
1007 if (object->type == OBJT_VNODE &&
1008 (object->flags & OBJ_MIGHTBEDIRTY) != 0) {
1010 vp = object->handle;
1011 VM_OBJECT_UNLOCK(object);
1012 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1013 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
1014 flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
1015 flags |= invalidate ? OBJPC_INVAL : 0;
1016 VM_OBJECT_LOCK(object);
1017 vm_object_page_clean(object,
1019 OFF_TO_IDX(offset + size + PAGE_MASK),
1021 VM_OBJECT_UNLOCK(object);
1022 VOP_UNLOCK(vp, 0, curthread);
1023 VFS_UNLOCK_GIANT(vfslocked);
1024 VM_OBJECT_LOCK(object);
1026 if ((object->type == OBJT_VNODE ||
1027 object->type == OBJT_DEVICE) && invalidate) {
1029 purge = old_msync || (object->type == OBJT_DEVICE);
1030 vm_object_page_remove(object,
1032 OFF_TO_IDX(offset + size + PAGE_MASK),
1033 purge ? FALSE : TRUE);
1035 VM_OBJECT_UNLOCK(object);
1039 * vm_object_madvise:
1041 * Implements the madvise function at the object/page level.
1043 * MADV_WILLNEED (any object)
1045 * Activate the specified pages if they are resident.
1047 * MADV_DONTNEED (any object)
1049 * Deactivate the specified pages if they are resident.
1051 * MADV_FREE (OBJT_DEFAULT/OBJT_SWAP objects,
1052 * OBJ_ONEMAPPING only)
1054 * Deactivate and clean the specified pages if they are
1055 * resident. This permits the process to reuse the pages
1056 * without faulting or the kernel to reclaim the pages
1060 vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
1062 vm_pindex_t end, tpindex;
1063 vm_object_t backing_object, tobject;
1068 VM_OBJECT_LOCK(object);
1069 end = pindex + count;
1071 * Locate and adjust resident pages
1073 for (; pindex < end; pindex += 1) {
1079 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
1080 * and those pages must be OBJ_ONEMAPPING.
1082 if (advise == MADV_FREE) {
1083 if ((tobject->type != OBJT_DEFAULT &&
1084 tobject->type != OBJT_SWAP) ||
1085 (tobject->flags & OBJ_ONEMAPPING) == 0) {
1086 goto unlock_tobject;
1089 m = vm_page_lookup(tobject, tpindex);
1092 * There may be swap even if there is no backing page
1094 if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1095 swap_pager_freespace(tobject, tpindex, 1);
1099 backing_object = tobject->backing_object;
1100 if (backing_object == NULL)
1101 goto unlock_tobject;
1102 VM_OBJECT_LOCK(backing_object);
1103 tpindex += OFF_TO_IDX(tobject->backing_object_offset);
1104 if (tobject != object)
1105 VM_OBJECT_UNLOCK(tobject);
1106 tobject = backing_object;
1110 * If the page is busy or not in a normal active state,
1111 * we skip it. If the page is not managed there are no
1112 * page queues to mess with. Things can break if we mess
1113 * with pages in any of the below states.
1115 vm_page_lock_queues();
1116 if (m->hold_count ||
1118 (m->flags & PG_UNMANAGED) ||
1119 m->valid != VM_PAGE_BITS_ALL) {
1120 vm_page_unlock_queues();
1121 goto unlock_tobject;
1123 if ((m->flags & PG_BUSY) || m->busy) {
1124 vm_page_flag_set(m, PG_WANTED | PG_REFERENCED);
1125 if (object != tobject)
1126 VM_OBJECT_UNLOCK(object);
1127 VM_OBJECT_UNLOCK(tobject);
1128 msleep(m, &vm_page_queue_mtx, PDROP | PVM, "madvpo", 0);
1129 VM_OBJECT_LOCK(object);
1132 if (advise == MADV_WILLNEED) {
1133 vm_page_activate(m);
1134 } else if (advise == MADV_DONTNEED) {
1135 vm_page_dontneed(m);
1136 } else if (advise == MADV_FREE) {
1138 * Mark the page clean. This will allow the page
1139 * to be freed up by the system. However, such pages
1140 * are often reused quickly by malloc()/free()
1141 * so we do not do anything that would cause
1142 * a page fault if we can help it.
1144 * Specifically, we do not try to actually free
1145 * the page now nor do we try to put it in the
1146 * cache (which would cause a page fault on reuse).
1148 * But we do make the page is freeable as we
1149 * can without actually taking the step of unmapping
1152 pmap_clear_modify(m);
1155 vm_page_dontneed(m);
1157 vm_page_unlock_queues();
1158 if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1159 swap_pager_freespace(tobject, tpindex, 1);
1161 if (tobject != object)
1162 VM_OBJECT_UNLOCK(tobject);
1164 VM_OBJECT_UNLOCK(object);
1170 * Create a new object which is backed by the
1171 * specified existing object range. The source
1172 * object reference is deallocated.
1174 * The new object and offset into that object
1175 * are returned in the source parameters.
1179 vm_object_t *object, /* IN/OUT */
1180 vm_ooffset_t *offset, /* IN/OUT */
1189 * Don't create the new object if the old object isn't shared.
1191 if (source != NULL) {
1192 VM_OBJECT_LOCK(source);
1193 if (source->ref_count == 1 &&
1194 source->handle == NULL &&
1195 (source->type == OBJT_DEFAULT ||
1196 source->type == OBJT_SWAP)) {
1197 VM_OBJECT_UNLOCK(source);
1200 VM_OBJECT_UNLOCK(source);
1204 * Allocate a new object with the given length.
1206 result = vm_object_allocate(OBJT_DEFAULT, length);
1209 * The new object shadows the source object, adding a reference to it.
1210 * Our caller changes his reference to point to the new object,
1211 * removing a reference to the source object. Net result: no change
1212 * of reference count.
1214 * Try to optimize the result object's page color when shadowing
1215 * in order to maintain page coloring consistency in the combined
1218 result->backing_object = source;
1220 * Store the offset into the source object, and fix up the offset into
1223 result->backing_object_offset = *offset;
1224 if (source != NULL) {
1225 VM_OBJECT_LOCK(source);
1226 LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1227 source->shadow_count++;
1228 source->generation++;
1229 if (length < source->size)
1230 length = source->size;
1231 if (length > PQ_L2_SIZE / 3 + PQ_PRIME1 ||
1232 source->generation > 1)
1233 length = PQ_L2_SIZE / 3 + PQ_PRIME1;
1234 result->pg_color = (source->pg_color +
1235 length * source->generation) & PQ_L2_MASK;
1236 result->flags |= source->flags & OBJ_NEEDGIANT;
1237 VM_OBJECT_UNLOCK(source);
1238 next_index = (result->pg_color + PQ_L2_SIZE / 3 + PQ_PRIME1) &
1244 * Return the new things
1253 * Split the pages in a map entry into a new object. This affords
1254 * easier removal of unused pages, and keeps object inheritance from
1255 * being a negative impact on memory usage.
1258 vm_object_split(vm_map_entry_t entry)
1261 vm_object_t orig_object, new_object, source;
1262 vm_pindex_t offidxstart, offidxend;
1263 vm_size_t idx, size;
1265 orig_object = entry->object.vm_object;
1266 if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1268 if (orig_object->ref_count <= 1)
1270 VM_OBJECT_UNLOCK(orig_object);
1272 offidxstart = OFF_TO_IDX(entry->offset);
1273 offidxend = offidxstart + OFF_TO_IDX(entry->end - entry->start);
1274 size = offidxend - offidxstart;
1277 * If swap_pager_copy() is later called, it will convert new_object
1278 * into a swap object.
1280 new_object = vm_object_allocate(OBJT_DEFAULT, size);
1282 VM_OBJECT_LOCK(new_object);
1283 VM_OBJECT_LOCK(orig_object);
1284 source = orig_object->backing_object;
1285 if (source != NULL) {
1286 VM_OBJECT_LOCK(source);
1287 LIST_INSERT_HEAD(&source->shadow_head,
1288 new_object, shadow_list);
1289 source->shadow_count++;
1290 source->generation++;
1291 vm_object_reference_locked(source); /* for new_object */
1292 vm_object_clear_flag(source, OBJ_ONEMAPPING);
1293 VM_OBJECT_UNLOCK(source);
1294 new_object->backing_object_offset =
1295 orig_object->backing_object_offset + entry->offset;
1296 new_object->backing_object = source;
1298 new_object->flags |= orig_object->flags & OBJ_NEEDGIANT;
1299 vm_page_lock_queues();
1300 for (idx = 0; idx < size; idx++) {
1302 m = vm_page_lookup(orig_object, offidxstart + idx);
1307 * We must wait for pending I/O to complete before we can
1310 * We do not have to VM_PROT_NONE the page as mappings should
1311 * not be changed by this operation.
1313 if ((m->flags & PG_BUSY) || m->busy) {
1314 vm_page_flag_set(m, PG_WANTED | PG_REFERENCED);
1315 VM_OBJECT_UNLOCK(orig_object);
1316 VM_OBJECT_UNLOCK(new_object);
1317 msleep(m, &vm_page_queue_mtx, PDROP | PVM, "spltwt", 0);
1318 VM_OBJECT_LOCK(new_object);
1319 VM_OBJECT_LOCK(orig_object);
1320 vm_page_lock_queues();
1323 vm_page_rename(m, new_object, idx);
1324 /* page automatically made dirty by rename and cache handled */
1327 vm_page_unlock_queues();
1328 if (orig_object->type == OBJT_SWAP) {
1330 * swap_pager_copy() can sleep, in which case the orig_object's
1331 * and new_object's locks are released and reacquired.
1333 swap_pager_copy(orig_object, new_object, offidxstart, 0);
1335 VM_OBJECT_UNLOCK(orig_object);
1336 vm_page_lock_queues();
1337 TAILQ_FOREACH(m, &new_object->memq, listq)
1339 vm_page_unlock_queues();
1340 VM_OBJECT_UNLOCK(new_object);
1341 entry->object.vm_object = new_object;
1342 entry->offset = 0LL;
1343 vm_object_deallocate(orig_object);
1344 VM_OBJECT_LOCK(new_object);
1347 #define OBSC_TEST_ALL_SHADOWED 0x0001
1348 #define OBSC_COLLAPSE_NOWAIT 0x0002
1349 #define OBSC_COLLAPSE_WAIT 0x0004
1352 vm_object_backing_scan(vm_object_t object, int op)
1356 vm_object_t backing_object;
1357 vm_pindex_t backing_offset_index;
1359 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1360 VM_OBJECT_LOCK_ASSERT(object->backing_object, MA_OWNED);
1362 backing_object = object->backing_object;
1363 backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
1366 * Initial conditions
1368 if (op & OBSC_TEST_ALL_SHADOWED) {
1370 * We do not want to have to test for the existence of
1371 * swap pages in the backing object. XXX but with the
1372 * new swapper this would be pretty easy to do.
1374 * XXX what about anonymous MAP_SHARED memory that hasn't
1375 * been ZFOD faulted yet? If we do not test for this, the
1376 * shadow test may succeed! XXX
1378 if (backing_object->type != OBJT_DEFAULT) {
1382 if (op & OBSC_COLLAPSE_WAIT) {
1383 vm_object_set_flag(backing_object, OBJ_DEAD);
1389 p = TAILQ_FIRST(&backing_object->memq);
1391 vm_page_t next = TAILQ_NEXT(p, listq);
1392 vm_pindex_t new_pindex = p->pindex - backing_offset_index;
1394 if (op & OBSC_TEST_ALL_SHADOWED) {
1398 * Ignore pages outside the parent object's range
1399 * and outside the parent object's mapping of the
1402 * note that we do not busy the backing object's
1406 p->pindex < backing_offset_index ||
1407 new_pindex >= object->size
1414 * See if the parent has the page or if the parent's
1415 * object pager has the page. If the parent has the
1416 * page but the page is not valid, the parent's
1417 * object pager must have the page.
1419 * If this fails, the parent does not completely shadow
1420 * the object and we might as well give up now.
1423 pp = vm_page_lookup(object, new_pindex);
1425 (pp == NULL || pp->valid == 0) &&
1426 !vm_pager_has_page(object, new_pindex, NULL, NULL)
1434 * Check for busy page
1436 if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
1439 if (op & OBSC_COLLAPSE_NOWAIT) {
1440 if ((p->flags & PG_BUSY) ||
1446 } else if (op & OBSC_COLLAPSE_WAIT) {
1447 if ((p->flags & PG_BUSY) || p->busy) {
1448 vm_page_lock_queues();
1450 PG_WANTED | PG_REFERENCED);
1451 VM_OBJECT_UNLOCK(backing_object);
1452 VM_OBJECT_UNLOCK(object);
1453 msleep(p, &vm_page_queue_mtx,
1454 PDROP | PVM, "vmocol", 0);
1455 VM_OBJECT_LOCK(object);
1456 VM_OBJECT_LOCK(backing_object);
1458 * If we slept, anything could have
1459 * happened. Since the object is
1460 * marked dead, the backing offset
1461 * should not have changed so we
1462 * just restart our scan.
1464 p = TAILQ_FIRST(&backing_object->memq);
1470 p->object == backing_object,
1471 ("vm_object_backing_scan: object mismatch")
1475 * Destroy any associated swap
1477 if (backing_object->type == OBJT_SWAP) {
1478 swap_pager_freespace(
1486 p->pindex < backing_offset_index ||
1487 new_pindex >= object->size
1490 * Page is out of the parent object's range, we
1491 * can simply destroy it.
1493 vm_page_lock_queues();
1494 KASSERT(!pmap_page_is_mapped(p),
1495 ("freeing mapped page %p", p));
1496 if (p->wire_count == 0)
1500 vm_page_unlock_queues();
1505 pp = vm_page_lookup(object, new_pindex);
1508 vm_pager_has_page(object, new_pindex, NULL, NULL)
1511 * page already exists in parent OR swap exists
1512 * for this location in the parent. Destroy
1513 * the original page from the backing object.
1515 * Leave the parent's page alone
1517 vm_page_lock_queues();
1518 KASSERT(!pmap_page_is_mapped(p),
1519 ("freeing mapped page %p", p));
1520 if (p->wire_count == 0)
1524 vm_page_unlock_queues();
1530 * Page does not exist in parent, rename the
1531 * page from the backing object to the main object.
1533 * If the page was mapped to a process, it can remain
1534 * mapped through the rename.
1536 vm_page_lock_queues();
1537 vm_page_rename(p, object, new_pindex);
1538 vm_page_unlock_queues();
1539 /* page automatically made dirty by rename */
1548 * this version of collapse allows the operation to occur earlier and
1549 * when paging_in_progress is true for an object... This is not a complete
1550 * operation, but should plug 99.9% of the rest of the leaks.
1553 vm_object_qcollapse(vm_object_t object)
1555 vm_object_t backing_object = object->backing_object;
1557 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1558 VM_OBJECT_LOCK_ASSERT(backing_object, MA_OWNED);
1560 if (backing_object->ref_count != 1)
1563 vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
1567 * vm_object_collapse:
1569 * Collapse an object with the object backing it.
1570 * Pages in the backing object are moved into the
1571 * parent, and the backing object is deallocated.
1574 vm_object_collapse(vm_object_t object)
1576 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1579 vm_object_t backing_object;
1582 * Verify that the conditions are right for collapse:
1584 * The object exists and the backing object exists.
1586 if ((backing_object = object->backing_object) == NULL)
1590 * we check the backing object first, because it is most likely
1593 VM_OBJECT_LOCK(backing_object);
1594 if (backing_object->handle != NULL ||
1595 (backing_object->type != OBJT_DEFAULT &&
1596 backing_object->type != OBJT_SWAP) ||
1597 (backing_object->flags & OBJ_DEAD) ||
1598 object->handle != NULL ||
1599 (object->type != OBJT_DEFAULT &&
1600 object->type != OBJT_SWAP) ||
1601 (object->flags & OBJ_DEAD)) {
1602 VM_OBJECT_UNLOCK(backing_object);
1607 object->paging_in_progress != 0 ||
1608 backing_object->paging_in_progress != 0
1610 vm_object_qcollapse(object);
1611 VM_OBJECT_UNLOCK(backing_object);
1615 * We know that we can either collapse the backing object (if
1616 * the parent is the only reference to it) or (perhaps) have
1617 * the parent bypass the object if the parent happens to shadow
1618 * all the resident pages in the entire backing object.
1620 * This is ignoring pager-backed pages such as swap pages.
1621 * vm_object_backing_scan fails the shadowing test in this
1624 if (backing_object->ref_count == 1) {
1626 * If there is exactly one reference to the backing
1627 * object, we can collapse it into the parent.
1629 vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1632 * Move the pager from backing_object to object.
1634 if (backing_object->type == OBJT_SWAP) {
1636 * swap_pager_copy() can sleep, in which case
1637 * the backing_object's and object's locks are
1638 * released and reacquired.
1643 OFF_TO_IDX(object->backing_object_offset), TRUE);
1646 * Object now shadows whatever backing_object did.
1647 * Note that the reference to
1648 * backing_object->backing_object moves from within
1649 * backing_object to within object.
1651 LIST_REMOVE(object, shadow_list);
1652 backing_object->shadow_count--;
1653 backing_object->generation++;
1654 if (backing_object->backing_object) {
1655 VM_OBJECT_LOCK(backing_object->backing_object);
1656 LIST_REMOVE(backing_object, shadow_list);
1658 &backing_object->backing_object->shadow_head,
1659 object, shadow_list);
1661 * The shadow_count has not changed.
1663 backing_object->backing_object->generation++;
1664 VM_OBJECT_UNLOCK(backing_object->backing_object);
1666 object->backing_object = backing_object->backing_object;
1667 object->backing_object_offset +=
1668 backing_object->backing_object_offset;
1671 * Discard backing_object.
1673 * Since the backing object has no pages, no pager left,
1674 * and no object references within it, all that is
1675 * necessary is to dispose of it.
1677 KASSERT(backing_object->ref_count == 1, ("backing_object %p was somehow re-referenced during collapse!", backing_object));
1678 VM_OBJECT_UNLOCK(backing_object);
1680 mtx_lock(&vm_object_list_mtx);
1686 mtx_unlock(&vm_object_list_mtx);
1688 uma_zfree(obj_zone, backing_object);
1692 vm_object_t new_backing_object;
1695 * If we do not entirely shadow the backing object,
1696 * there is nothing we can do so we give up.
1698 if (vm_object_backing_scan(object, OBSC_TEST_ALL_SHADOWED) == 0) {
1699 VM_OBJECT_UNLOCK(backing_object);
1704 * Make the parent shadow the next object in the
1705 * chain. Deallocating backing_object will not remove
1706 * it, since its reference count is at least 2.
1708 LIST_REMOVE(object, shadow_list);
1709 backing_object->shadow_count--;
1710 backing_object->generation++;
1712 new_backing_object = backing_object->backing_object;
1713 if ((object->backing_object = new_backing_object) != NULL) {
1714 VM_OBJECT_LOCK(new_backing_object);
1716 &new_backing_object->shadow_head,
1720 new_backing_object->shadow_count++;
1721 new_backing_object->generation++;
1722 vm_object_reference_locked(new_backing_object);
1723 VM_OBJECT_UNLOCK(new_backing_object);
1724 object->backing_object_offset +=
1725 backing_object->backing_object_offset;
1729 * Drop the reference count on backing_object. Since
1730 * its ref_count was at least 2, it will not vanish.
1732 backing_object->ref_count--;
1733 VM_OBJECT_UNLOCK(backing_object);
1738 * Try again with this object's new backing object.
1744 * vm_object_page_remove:
1746 * Removes all physical pages in the given range from the
1747 * object's list of pages. If the range's end is zero, all
1748 * physical pages from the range's start to the end of the object
1751 * The object must be locked.
1754 vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
1755 boolean_t clean_only)
1759 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1760 if (object->resident_page_count == 0)
1764 * Since physically-backed objects do not use managed pages, we can't
1765 * remove pages from the object (we must instead remove the page
1766 * references, and then destroy the object).
1768 KASSERT(object->type != OBJT_PHYS,
1769 ("attempt to remove pages from a physical object"));
1771 vm_object_pip_add(object, 1);
1773 vm_page_lock_queues();
1774 if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
1775 if (p->pindex < start) {
1776 p = vm_page_splay(start, object->root);
1777 if ((object->root = p)->pindex < start)
1778 p = TAILQ_NEXT(p, listq);
1782 * Assert: the variable p is either (1) the page with the
1783 * least pindex greater than or equal to the parameter pindex
1787 p != NULL && (p->pindex < end || end == 0);
1789 next = TAILQ_NEXT(p, listq);
1791 if (p->wire_count != 0) {
1797 if (vm_page_sleep_if_busy(p, TRUE, "vmopar"))
1799 if (clean_only && p->valid) {
1800 pmap_page_protect(p, VM_PROT_READ | VM_PROT_EXECUTE);
1801 if (p->valid & p->dirty)
1807 vm_page_unlock_queues();
1808 vm_object_pip_wakeup(object);
1812 * Routine: vm_object_coalesce
1813 * Function: Coalesces two objects backing up adjoining
1814 * regions of memory into a single object.
1816 * returns TRUE if objects were combined.
1818 * NOTE: Only works at the moment if the second object is NULL -
1819 * if it's not, which object do we lock first?
1822 * prev_object First object to coalesce
1823 * prev_offset Offset into prev_object
1824 * prev_size Size of reference to prev_object
1825 * next_size Size of reference to the second object
1828 * The object must *not* be locked.
1831 vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset,
1832 vm_size_t prev_size, vm_size_t next_size)
1834 vm_pindex_t next_pindex;
1836 if (prev_object == NULL)
1838 VM_OBJECT_LOCK(prev_object);
1839 if (prev_object->type != OBJT_DEFAULT &&
1840 prev_object->type != OBJT_SWAP) {
1841 VM_OBJECT_UNLOCK(prev_object);
1846 * Try to collapse the object first
1848 vm_object_collapse(prev_object);
1851 * Can't coalesce if: . more than one reference . paged out . shadows
1852 * another object . has a copy elsewhere (any of which mean that the
1853 * pages not mapped to prev_entry may be in use anyway)
1855 if (prev_object->backing_object != NULL) {
1856 VM_OBJECT_UNLOCK(prev_object);
1860 prev_size >>= PAGE_SHIFT;
1861 next_size >>= PAGE_SHIFT;
1862 next_pindex = OFF_TO_IDX(prev_offset) + prev_size;
1864 if ((prev_object->ref_count > 1) &&
1865 (prev_object->size != next_pindex)) {
1866 VM_OBJECT_UNLOCK(prev_object);
1871 * Remove any pages that may still be in the object from a previous
1874 if (next_pindex < prev_object->size) {
1875 vm_object_page_remove(prev_object,
1877 next_pindex + next_size, FALSE);
1878 if (prev_object->type == OBJT_SWAP)
1879 swap_pager_freespace(prev_object,
1880 next_pindex, next_size);
1884 * Extend the object if necessary.
1886 if (next_pindex + next_size > prev_object->size)
1887 prev_object->size = next_pindex + next_size;
1889 VM_OBJECT_UNLOCK(prev_object);
1894 vm_object_set_writeable_dirty(vm_object_t object)
1898 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1899 if ((object->flags & (OBJ_MIGHTBEDIRTY|OBJ_WRITEABLE)) ==
1900 (OBJ_MIGHTBEDIRTY|OBJ_WRITEABLE))
1902 vm_object_set_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
1903 if (object->type == OBJT_VNODE &&
1904 (vp = (struct vnode *)object->handle) != NULL) {
1906 vp->v_iflag |= VI_OBJDIRTY;
1911 #include "opt_ddb.h"
1913 #include <sys/kernel.h>
1915 #include <sys/cons.h>
1917 #include <ddb/ddb.h>
1920 _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
1923 vm_map_entry_t tmpe;
1931 tmpe = map->header.next;
1932 entcount = map->nentries;
1933 while (entcount-- && (tmpe != &map->header)) {
1934 if (_vm_object_in_map(map, object, tmpe)) {
1939 } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
1940 tmpm = entry->object.sub_map;
1941 tmpe = tmpm->header.next;
1942 entcount = tmpm->nentries;
1943 while (entcount-- && tmpe != &tmpm->header) {
1944 if (_vm_object_in_map(tmpm, object, tmpe)) {
1949 } else if ((obj = entry->object.vm_object) != NULL) {
1950 for (; obj; obj = obj->backing_object)
1951 if (obj == object) {
1959 vm_object_in_map(vm_object_t object)
1963 /* sx_slock(&allproc_lock); */
1964 LIST_FOREACH(p, &allproc, p_list) {
1965 if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1967 if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
1968 /* sx_sunlock(&allproc_lock); */
1972 /* sx_sunlock(&allproc_lock); */
1973 if (_vm_object_in_map(kernel_map, object, 0))
1975 if (_vm_object_in_map(kmem_map, object, 0))
1977 if (_vm_object_in_map(pager_map, object, 0))
1979 if (_vm_object_in_map(buffer_map, object, 0))
1984 DB_SHOW_COMMAND(vmochk, vm_object_check)
1989 * make sure that internal objs are in a map somewhere
1990 * and none have zero ref counts.
1992 TAILQ_FOREACH(object, &vm_object_list, object_list) {
1993 if (object->handle == NULL &&
1994 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
1995 if (object->ref_count == 0) {
1996 db_printf("vmochk: internal obj has zero ref count: %ld\n",
1997 (long)object->size);
1999 if (!vm_object_in_map(object)) {
2001 "vmochk: internal obj is not in a map: "
2002 "ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2003 object->ref_count, (u_long)object->size,
2004 (u_long)object->size,
2005 (void *)object->backing_object);
2012 * vm_object_print: [ debug ]
2014 DB_SHOW_COMMAND(object, vm_object_print_static)
2016 /* XXX convert args. */
2017 vm_object_t object = (vm_object_t)addr;
2018 boolean_t full = have_addr;
2022 /* XXX count is an (unused) arg. Avoid shadowing it. */
2023 #define count was_count
2031 "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x\n",
2032 object, (int)object->type, (uintmax_t)object->size,
2033 object->resident_page_count, object->ref_count, object->flags);
2034 db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n",
2035 object->shadow_count,
2036 object->backing_object ? object->backing_object->ref_count : 0,
2037 object->backing_object, (uintmax_t)object->backing_object_offset);
2044 TAILQ_FOREACH(p, &object->memq, listq) {
2046 db_iprintf("memory:=");
2047 else if (count == 6) {
2055 db_printf("(off=0x%jx,page=0x%jx)",
2056 (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p));
2066 /* XXX need this non-static entry for calling from vm_map_print. */
2069 /* db_expr_t */ long addr,
2070 boolean_t have_addr,
2071 /* db_expr_t */ long count,
2074 vm_object_print_static(addr, have_addr, count, modif);
2077 DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
2083 TAILQ_FOREACH(object, &vm_object_list, object_list) {
2084 vm_pindex_t idx, fidx;
2086 vm_paddr_t pa = -1, padiff;
2090 db_printf("new object: %p\n", (void *)object);
2100 osize = object->size;
2103 for (idx = 0; idx < osize; idx++) {
2104 m = vm_page_lookup(object, idx);
2107 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2108 (long)fidx, rcount, (long)pa);
2123 (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
2128 padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
2129 padiff >>= PAGE_SHIFT;
2130 padiff &= PQ_L2_MASK;
2132 pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
2136 db_printf(" index(%ld)run(%d)pa(0x%lx)",
2137 (long)fidx, rcount, (long)pa);
2138 db_printf("pd(%ld)\n", (long)padiff);
2148 pa = VM_PAGE_TO_PHYS(m);
2152 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2153 (long)fidx, rcount, (long)pa);