2 * Copyright (c) 1991 Regents of the University of California.
4 * Copyright (c) 1998 Matthew Dillon. All Rights Reserved.
6 * This code is derived from software contributed to Berkeley by
7 * The Mach Operating System project at Carnegie-Mellon University.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * from: @(#)vm_page.c 7.4 (Berkeley) 5/7/91
37 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
38 * All rights reserved.
40 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
42 * Permission to use, copy, modify and distribute this software and
43 * its documentation is hereby granted, provided that both the copyright
44 * notice and this permission notice appear in all copies of the
45 * software, derivative works or modified versions, and any portions
46 * thereof, and that both notices appear in supporting documentation.
48 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
50 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
52 * Carnegie Mellon requests users of this software to return to
54 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
55 * School of Computer Science
56 * Carnegie Mellon University
57 * Pittsburgh PA 15213-3890
59 * any improvements or extensions that they make and grant Carnegie the
60 * rights to redistribute these changes.
64 * GENERAL RULES ON VM_PAGE MANIPULATION
66 * - a pageq mutex is required when adding or removing a page from a
67 * page queue (vm_page_queue[]), regardless of other mutexes or the
68 * busy state of a page.
70 * - a hash chain mutex is required when associating or disassociating
71 * a page from the VM PAGE CACHE hash table (vm_page_buckets),
72 * regardless of other mutexes or the busy state of a page.
74 * - either a hash chain mutex OR a busied page is required in order
75 * to modify the page flags. A hash chain mutex must be obtained in
76 * order to busy a page. A page's flags cannot be modified by a
77 * hash chain mutex if the page is marked busy.
79 * - The object memq mutex is held when inserting or removing
80 * pages from an object (vm_page_insert() or vm_page_remove()). This
81 * is different from the object's main mutex.
83 * Generally speaking, you have to be aware of side effects when running
84 * vm_page ops. A vm_page_lookup() will return with the hash chain
85 * locked, whether it was able to lookup the page or not. vm_page_free(),
86 * vm_page_cache(), vm_page_activate(), and a number of other routines
87 * will release the hash chain mutex for you. Intermediate manipulation
88 * routines such as vm_page_flag_set() expect the hash chain to be held
89 * on entry and the hash chain will remain held on return.
91 * pageq scanning can only occur with the pageq in question locked.
92 * We have a known bottleneck with the active queue, but the cache
93 * and free queues are actually arrays already.
97 * Resident memory management module.
100 #include <sys/cdefs.h>
101 __FBSDID("$FreeBSD$");
105 #include <sys/param.h>
106 #include <sys/systm.h>
107 #include <sys/lock.h>
108 #include <sys/kernel.h>
109 #include <sys/limits.h>
110 #include <sys/malloc.h>
111 #include <sys/mutex.h>
112 #include <sys/proc.h>
113 #include <sys/sysctl.h>
114 #include <sys/vmmeter.h>
115 #include <sys/vnode.h>
118 #include <vm/vm_param.h>
119 #include <vm/vm_kern.h>
120 #include <vm/vm_object.h>
121 #include <vm/vm_page.h>
122 #include <vm/vm_pageout.h>
123 #include <vm/vm_pager.h>
124 #include <vm/vm_phys.h>
125 #include <vm/vm_reserv.h>
126 #include <vm/vm_extern.h>
128 #include <vm/uma_int.h>
130 #include <machine/md_var.h>
133 * Associated with page of user-allocatable memory is a
137 struct vpgqueues vm_page_queues[PQ_COUNT];
138 struct mtx vm_page_queue_mtx;
139 struct mtx vm_page_queue_free_mtx;
141 vm_page_t vm_page_array = 0;
142 int vm_page_array_size = 0;
144 int vm_page_zero_count = 0;
146 static int boot_pages = UMA_BOOT_PAGES;
147 TUNABLE_INT("vm.boot_pages", &boot_pages);
148 SYSCTL_INT(_vm, OID_AUTO, boot_pages, CTLFLAG_RD, &boot_pages, 0,
149 "number of pages allocated for bootstrapping the VM system");
151 static void vm_page_enqueue(int queue, vm_page_t m);
153 /* Make sure that u_long is at least 64 bits when PAGE_SIZE is 32K. */
154 #if PAGE_SIZE == 32768
156 CTASSERT(sizeof(u_long) >= 8);
163 * Sets the page size, perhaps based upon the memory
164 * size. Must be called before any use of page-size
165 * dependent functions.
168 vm_set_page_size(void)
170 if (cnt.v_page_size == 0)
171 cnt.v_page_size = PAGE_SIZE;
172 if (((cnt.v_page_size - 1) & cnt.v_page_size) != 0)
173 panic("vm_set_page_size: page size not a power of two");
177 * vm_page_blacklist_lookup:
179 * See if a physical address in this page has been listed
180 * in the blacklist tunable. Entries in the tunable are
181 * separated by spaces or commas. If an invalid integer is
182 * encountered then the rest of the string is skipped.
185 vm_page_blacklist_lookup(char *list, vm_paddr_t pa)
190 for (pos = list; *pos != '\0'; pos = cp) {
191 bad = strtoq(pos, &cp, 0);
193 if (*cp == ' ' || *cp == ',') {
200 if (pa == trunc_page(bad))
209 * Initializes the resident memory module.
211 * Allocates memory for the page cells, and
212 * for the object/offset-to-page hash table headers.
213 * Each page cell is initialized and placed on the free list.
216 vm_page_startup(vm_offset_t vaddr)
219 vm_paddr_t page_range;
227 /* the biggest memory array is the second group of pages */
229 vm_paddr_t biggestsize;
230 vm_paddr_t low_water, high_water;
236 vaddr = round_page(vaddr);
238 for (i = 0; phys_avail[i + 1]; i += 2) {
239 phys_avail[i] = round_page(phys_avail[i]);
240 phys_avail[i + 1] = trunc_page(phys_avail[i + 1]);
243 low_water = phys_avail[0];
244 high_water = phys_avail[1];
246 for (i = 0; phys_avail[i + 1]; i += 2) {
247 vm_paddr_t size = phys_avail[i + 1] - phys_avail[i];
249 if (size > biggestsize) {
253 if (phys_avail[i] < low_water)
254 low_water = phys_avail[i];
255 if (phys_avail[i + 1] > high_water)
256 high_water = phys_avail[i + 1];
264 end = phys_avail[biggestone+1];
267 * Initialize the locks.
269 mtx_init(&vm_page_queue_mtx, "vm page queue mutex", NULL, MTX_DEF |
271 mtx_init(&vm_page_queue_free_mtx, "vm page queue free mutex", NULL,
275 * Initialize the queue headers for the hold queue, the active queue,
276 * and the inactive queue.
278 for (i = 0; i < PQ_COUNT; i++)
279 TAILQ_INIT(&vm_page_queues[i].pl);
280 vm_page_queues[PQ_INACTIVE].cnt = &cnt.v_inactive_count;
281 vm_page_queues[PQ_ACTIVE].cnt = &cnt.v_active_count;
282 vm_page_queues[PQ_HOLD].cnt = &cnt.v_active_count;
285 * Allocate memory for use when boot strapping the kernel memory
288 new_end = end - (boot_pages * UMA_SLAB_SIZE);
289 new_end = trunc_page(new_end);
290 mapped = pmap_map(&vaddr, new_end, end,
291 VM_PROT_READ | VM_PROT_WRITE);
292 bzero((void *)mapped, end - new_end);
293 uma_startup((void *)mapped, boot_pages);
295 #if defined(__amd64__) || defined(__i386__) || defined(__arm__)
297 * Allocate a bitmap to indicate that a random physical page
298 * needs to be included in a minidump.
300 * The amd64 port needs this to indicate which direct map pages
301 * need to be dumped, via calls to dump_add_page()/dump_drop_page().
303 * However, i386 still needs this workspace internally within the
304 * minidump code. In theory, they are not needed on i386, but are
305 * included should the sf_buf code decide to use them.
307 page_range = phys_avail[(nblocks - 1) * 2 + 1] / PAGE_SIZE;
308 vm_page_dump_size = round_page(roundup2(page_range, NBBY) / NBBY);
309 new_end -= vm_page_dump_size;
310 vm_page_dump = (void *)(uintptr_t)pmap_map(&vaddr, new_end,
311 new_end + vm_page_dump_size, VM_PROT_READ | VM_PROT_WRITE);
312 bzero((void *)vm_page_dump, vm_page_dump_size);
315 * Compute the number of pages of memory that will be available for
316 * use (taking into account the overhead of a page structure per
319 first_page = low_water / PAGE_SIZE;
320 #ifdef VM_PHYSSEG_SPARSE
322 for (i = 0; phys_avail[i + 1] != 0; i += 2)
323 page_range += atop(phys_avail[i + 1] - phys_avail[i]);
324 #elif defined(VM_PHYSSEG_DENSE)
325 page_range = high_water / PAGE_SIZE - first_page;
327 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
332 * Reserve an unmapped guard page to trap access to vm_page_array[-1].
337 * Initialize the mem entry structures now, and put them in the free
340 new_end = trunc_page(end - page_range * sizeof(struct vm_page));
341 mapped = pmap_map(&vaddr, new_end, end,
342 VM_PROT_READ | VM_PROT_WRITE);
343 vm_page_array = (vm_page_t) mapped;
344 #if VM_NRESERVLEVEL > 0
346 * Allocate memory for the reservation management system's data
349 new_end = vm_reserv_startup(&vaddr, new_end, high_water);
353 * pmap_map on amd64 comes out of the direct-map, not kvm like i386,
354 * so the pages must be tracked for a crashdump to include this data.
355 * This includes the vm_page_array and the early UMA bootstrap pages.
357 for (pa = new_end; pa < phys_avail[biggestone + 1]; pa += PAGE_SIZE)
360 phys_avail[biggestone + 1] = new_end;
363 * Clear all of the page structures
365 bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page));
366 for (i = 0; i < page_range; i++)
367 vm_page_array[i].order = VM_NFREEORDER;
368 vm_page_array_size = page_range;
371 * Initialize the physical memory allocator.
376 * Add every available physical page that is not blacklisted to
379 cnt.v_page_count = 0;
380 cnt.v_free_count = 0;
381 list = getenv("vm.blacklist");
382 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
384 last_pa = phys_avail[i + 1];
385 while (pa < last_pa) {
387 vm_page_blacklist_lookup(list, pa))
388 printf("Skipping page with pa 0x%jx\n",
391 vm_phys_add_page(pa);
396 #if VM_NRESERVLEVEL > 0
398 * Initialize the reservation management system.
406 vm_page_flag_set(vm_page_t m, unsigned short bits)
409 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
414 vm_page_flag_clear(vm_page_t m, unsigned short bits)
417 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
422 vm_page_busy(vm_page_t m)
425 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
426 KASSERT((m->oflags & VPO_BUSY) == 0,
427 ("vm_page_busy: page already busy!!!"));
428 m->oflags |= VPO_BUSY;
434 * wakeup anyone waiting for the page.
437 vm_page_flash(vm_page_t m)
440 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
441 if (m->oflags & VPO_WANTED) {
442 m->oflags &= ~VPO_WANTED;
450 * clear the VPO_BUSY flag and wakeup anyone waiting for the
455 vm_page_wakeup(vm_page_t m)
458 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
459 KASSERT(m->oflags & VPO_BUSY, ("vm_page_wakeup: page not busy!!!"));
460 m->oflags &= ~VPO_BUSY;
465 vm_page_io_start(vm_page_t m)
468 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
473 vm_page_io_finish(vm_page_t m)
476 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
483 * Keep page from being freed by the page daemon
484 * much of the same effect as wiring, except much lower
485 * overhead and should be used only for *very* temporary
486 * holding ("wiring").
489 vm_page_hold(vm_page_t mem)
492 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
497 vm_page_unhold(vm_page_t mem)
500 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
502 KASSERT(mem->hold_count >= 0, ("vm_page_unhold: hold count < 0!!!"));
503 if (mem->hold_count == 0 && VM_PAGE_INQUEUE2(mem, PQ_HOLD))
504 vm_page_free_toq(mem);
513 vm_page_free(vm_page_t m)
516 m->flags &= ~PG_ZERO;
523 * Free a page to the zerod-pages queue
526 vm_page_free_zero(vm_page_t m)
536 * Sleep and release the page queues lock.
538 * The object containing the given page must be locked.
541 vm_page_sleep(vm_page_t m, const char *msg)
544 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
545 if (!mtx_owned(&vm_page_queue_mtx))
546 vm_page_lock_queues();
547 vm_page_flag_set(m, PG_REFERENCED);
548 vm_page_unlock_queues();
551 * It's possible that while we sleep, the page will get
552 * unbusied and freed. If we are holding the object
553 * lock, we will assume we hold a reference to the object
554 * such that even if m->object changes, we can re-lock
557 m->oflags |= VPO_WANTED;
558 msleep(m, VM_OBJECT_MTX(m->object), PVM, msg, 0);
564 * make page all dirty
567 vm_page_dirty(vm_page_t m)
570 KASSERT((m->flags & PG_CACHED) == 0,
571 ("vm_page_dirty: page in cache!"));
572 KASSERT(!VM_PAGE_IS_FREE(m),
573 ("vm_page_dirty: page is free!"));
574 KASSERT(m->valid == VM_PAGE_BITS_ALL,
575 ("vm_page_dirty: page is invalid!"));
576 m->dirty = VM_PAGE_BITS_ALL;
582 * Implements Sleator and Tarjan's top-down splay algorithm. Returns
583 * the vm_page containing the given pindex. If, however, that
584 * pindex is not found in the vm_object, returns a vm_page that is
585 * adjacent to the pindex, coming before or after it.
588 vm_page_splay(vm_pindex_t pindex, vm_page_t root)
590 struct vm_page dummy;
591 vm_page_t lefttreemax, righttreemin, y;
595 lefttreemax = righttreemin = &dummy;
597 if (pindex < root->pindex) {
598 if ((y = root->left) == NULL)
600 if (pindex < y->pindex) {
602 root->left = y->right;
605 if ((y = root->left) == NULL)
608 /* Link into the new root's right tree. */
609 righttreemin->left = root;
611 } else if (pindex > root->pindex) {
612 if ((y = root->right) == NULL)
614 if (pindex > y->pindex) {
616 root->right = y->left;
619 if ((y = root->right) == NULL)
622 /* Link into the new root's left tree. */
623 lefttreemax->right = root;
628 /* Assemble the new root. */
629 lefttreemax->right = root->left;
630 righttreemin->left = root->right;
631 root->left = dummy.right;
632 root->right = dummy.left;
637 * vm_page_insert: [ internal use only ]
639 * Inserts the given mem entry into the object and object list.
641 * The pagetables are not updated but will presumably fault the page
642 * in if necessary, or if a kernel page the caller will at some point
643 * enter the page into the kernel's pmap. We are not allowed to block
644 * here so we *can't* do this anyway.
646 * The object and page must be locked.
647 * This routine may not block.
650 vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex)
654 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
655 if (m->object != NULL)
656 panic("vm_page_insert: page already inserted");
659 * Record the object/offset pair in this page
665 * Now link into the object's ordered list of backed pages.
671 TAILQ_INSERT_TAIL(&object->memq, m, listq);
673 root = vm_page_splay(pindex, root);
674 if (pindex < root->pindex) {
675 m->left = root->left;
678 TAILQ_INSERT_BEFORE(root, m, listq);
679 } else if (pindex == root->pindex)
680 panic("vm_page_insert: offset already allocated");
682 m->right = root->right;
685 TAILQ_INSERT_AFTER(&object->memq, root, m, listq);
689 object->generation++;
692 * show that the object has one more resident page.
694 object->resident_page_count++;
696 * Hold the vnode until the last page is released.
698 if (object->resident_page_count == 1 && object->type == OBJT_VNODE)
699 vhold((struct vnode *)object->handle);
702 * Since we are inserting a new and possibly dirty page,
703 * update the object's OBJ_MIGHTBEDIRTY flag.
705 if (m->flags & PG_WRITEABLE)
706 vm_object_set_writeable_dirty(object);
711 * NOTE: used by device pager as well -wfj
713 * Removes the given mem entry from the object/offset-page
714 * table and the object page list, but do not invalidate/terminate
717 * The object and page must be locked.
718 * The underlying pmap entry (if any) is NOT removed here.
719 * This routine may not block.
722 vm_page_remove(vm_page_t m)
727 if ((object = m->object) == NULL)
729 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
730 if (m->oflags & VPO_BUSY) {
731 m->oflags &= ~VPO_BUSY;
734 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
737 * Now remove from the object's list of backed pages.
739 if (m != object->root)
740 vm_page_splay(m->pindex, object->root);
744 root = vm_page_splay(m->pindex, m->left);
745 root->right = m->right;
748 TAILQ_REMOVE(&object->memq, m, listq);
751 * And show that the object has one fewer resident page.
753 object->resident_page_count--;
754 object->generation++;
756 * The vnode may now be recycled.
758 if (object->resident_page_count == 0 && object->type == OBJT_VNODE)
759 vdrop((struct vnode *)object->handle);
767 * Returns the page associated with the object/offset
768 * pair specified; if none is found, NULL is returned.
770 * The object must be locked.
771 * This routine may not block.
772 * This is a critical path routine
775 vm_page_lookup(vm_object_t object, vm_pindex_t pindex)
779 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
780 if ((m = object->root) != NULL && m->pindex != pindex) {
781 m = vm_page_splay(pindex, m);
782 if ((object->root = m)->pindex != pindex)
791 * Move the given memory entry from its
792 * current object to the specified target object/offset.
794 * The object must be locked.
795 * This routine may not block.
797 * Note: swap associated with the page must be invalidated by the move. We
798 * have to do this for several reasons: (1) we aren't freeing the
799 * page, (2) we are dirtying the page, (3) the VM system is probably
800 * moving the page from object A to B, and will then later move
801 * the backing store from A to B and we can't have a conflict.
803 * Note: we *always* dirty the page. It is necessary both for the
804 * fact that we moved it, and because we may be invalidating
805 * swap. If the page is on the cache, we have to deactivate it
806 * or vm_page_dirty() will panic. Dirty pages are not allowed
810 vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex)
814 vm_page_insert(m, new_object, new_pindex);
819 * Convert all of the given object's cached pages that have a
820 * pindex within the given range into free pages. If the value
821 * zero is given for "end", then the range's upper bound is
822 * infinity. If the given object is backed by a vnode and it
823 * transitions from having one or more cached pages to none, the
824 * vnode's hold count is reduced.
827 vm_page_cache_free(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
832 mtx_lock(&vm_page_queue_free_mtx);
833 if (__predict_false(object->cache == NULL)) {
834 mtx_unlock(&vm_page_queue_free_mtx);
837 m = object->cache = vm_page_splay(start, object->cache);
838 if (m->pindex < start) {
839 if (m->right == NULL)
842 m_next = vm_page_splay(start, m->right);
845 m = object->cache = m_next;
850 * At this point, "m" is either (1) a reference to the page
851 * with the least pindex that is greater than or equal to
852 * "start" or (2) NULL.
854 for (; m != NULL && (m->pindex < end || end == 0); m = m_next) {
856 * Find "m"'s successor and remove "m" from the
859 if (m->right == NULL) {
860 object->cache = m->left;
863 m_next = vm_page_splay(start, m->right);
864 m_next->left = m->left;
865 object->cache = m_next;
867 /* Convert "m" to a free page. */
870 /* Clear PG_CACHED and set PG_FREE. */
871 m->flags ^= PG_CACHED | PG_FREE;
872 KASSERT((m->flags & (PG_CACHED | PG_FREE)) == PG_FREE,
873 ("vm_page_cache_free: page %p has inconsistent flags", m));
877 empty = object->cache == NULL;
878 mtx_unlock(&vm_page_queue_free_mtx);
879 if (object->type == OBJT_VNODE && empty)
880 vdrop(object->handle);
884 * Returns the cached page that is associated with the given
885 * object and offset. If, however, none exists, returns NULL.
887 * The free page queue must be locked.
889 static inline vm_page_t
890 vm_page_cache_lookup(vm_object_t object, vm_pindex_t pindex)
894 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
895 if ((m = object->cache) != NULL && m->pindex != pindex) {
896 m = vm_page_splay(pindex, m);
897 if ((object->cache = m)->pindex != pindex)
904 * Remove the given cached page from its containing object's
905 * collection of cached pages.
907 * The free page queue must be locked.
910 vm_page_cache_remove(vm_page_t m)
915 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
916 KASSERT((m->flags & PG_CACHED) != 0,
917 ("vm_page_cache_remove: page %p is not cached", m));
919 if (m != object->cache) {
920 root = vm_page_splay(m->pindex, object->cache);
922 ("vm_page_cache_remove: page %p is not cached in object %p",
927 else if (m->right == NULL)
930 root = vm_page_splay(m->pindex, m->left);
931 root->right = m->right;
933 object->cache = root;
939 * Transfer all of the cached pages with offset greater than or
940 * equal to 'offidxstart' from the original object's cache to the
941 * new object's cache. However, any cached pages with offset
942 * greater than or equal to the new object's size are kept in the
943 * original object. Initially, the new object's cache must be
944 * empty. Offset 'offidxstart' in the original object must
945 * correspond to offset zero in the new object.
947 * The new object must be locked.
950 vm_page_cache_transfer(vm_object_t orig_object, vm_pindex_t offidxstart,
951 vm_object_t new_object)
956 * Insertion into an object's collection of cached pages
957 * requires the object to be locked. In contrast, removal does
960 VM_OBJECT_LOCK_ASSERT(new_object, MA_OWNED);
961 KASSERT(new_object->cache == NULL,
962 ("vm_page_cache_transfer: object %p has cached pages",
964 mtx_lock(&vm_page_queue_free_mtx);
965 if ((m = orig_object->cache) != NULL) {
967 * Transfer all of the pages with offset greater than or
968 * equal to 'offidxstart' from the original object's
969 * cache to the new object's cache.
971 m = vm_page_splay(offidxstart, m);
972 if (m->pindex < offidxstart) {
973 orig_object->cache = m;
974 new_object->cache = m->right;
977 orig_object->cache = m->left;
978 new_object->cache = m;
981 while ((m = new_object->cache) != NULL) {
982 if ((m->pindex - offidxstart) >= new_object->size) {
984 * Return all of the cached pages with
985 * offset greater than or equal to the
986 * new object's size to the original
989 new_object->cache = m->left;
990 m->left = orig_object->cache;
991 orig_object->cache = m;
994 m_next = vm_page_splay(m->pindex, m->right);
995 /* Update the page's object and offset. */
996 m->object = new_object;
997 m->pindex -= offidxstart;
1002 new_object->cache = m_next;
1004 KASSERT(new_object->cache == NULL ||
1005 new_object->type == OBJT_SWAP,
1006 ("vm_page_cache_transfer: object %p's type is incompatible"
1007 " with cached pages", new_object));
1009 mtx_unlock(&vm_page_queue_free_mtx);
1015 * Allocate and return a memory cell associated
1016 * with this VM object/offset pair.
1019 * VM_ALLOC_NORMAL normal process request
1020 * VM_ALLOC_SYSTEM system *really* needs a page
1021 * VM_ALLOC_INTERRUPT interrupt time request
1022 * VM_ALLOC_ZERO zero page
1023 * VM_ALLOC_WIRED wire the allocated page
1024 * VM_ALLOC_NOOBJ page is not associated with a vm object
1025 * VM_ALLOC_NOBUSY do not set the page busy
1026 * VM_ALLOC_IFNOTCACHED return NULL, do not reactivate if the page
1029 * This routine may not sleep.
1032 vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req)
1034 struct vnode *vp = NULL;
1035 vm_object_t m_object;
1037 int flags, page_req;
1039 page_req = req & VM_ALLOC_CLASS_MASK;
1040 KASSERT(curthread->td_intr_nesting_level == 0 ||
1041 page_req == VM_ALLOC_INTERRUPT,
1042 ("vm_page_alloc(NORMAL|SYSTEM) in interrupt context"));
1044 if ((req & VM_ALLOC_NOOBJ) == 0) {
1045 KASSERT(object != NULL,
1046 ("vm_page_alloc: NULL object."));
1047 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1051 * The pager is allowed to eat deeper into the free page list.
1053 if ((curproc == pageproc) && (page_req != VM_ALLOC_INTERRUPT)) {
1054 page_req = VM_ALLOC_SYSTEM;
1057 mtx_lock(&vm_page_queue_free_mtx);
1058 if (cnt.v_free_count + cnt.v_cache_count > cnt.v_free_reserved ||
1059 (page_req == VM_ALLOC_SYSTEM &&
1060 cnt.v_free_count + cnt.v_cache_count > cnt.v_interrupt_free_min) ||
1061 (page_req == VM_ALLOC_INTERRUPT &&
1062 cnt.v_free_count + cnt.v_cache_count > 0)) {
1064 * Allocate from the free queue if the number of free pages
1065 * exceeds the minimum for the request class.
1067 if (object != NULL &&
1068 (m = vm_page_cache_lookup(object, pindex)) != NULL) {
1069 if ((req & VM_ALLOC_IFNOTCACHED) != 0) {
1070 mtx_unlock(&vm_page_queue_free_mtx);
1073 if (vm_phys_unfree_page(m))
1074 vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m, 0);
1075 #if VM_NRESERVLEVEL > 0
1076 else if (!vm_reserv_reactivate_page(m))
1080 panic("vm_page_alloc: cache page %p is missing"
1081 " from the free queue", m);
1082 } else if ((req & VM_ALLOC_IFCACHED) != 0) {
1083 mtx_unlock(&vm_page_queue_free_mtx);
1085 #if VM_NRESERVLEVEL > 0
1086 } else if (object == NULL || object->type == OBJT_DEVICE ||
1087 object->type == OBJT_SG ||
1088 (object->flags & OBJ_COLORED) == 0 ||
1089 (m = vm_reserv_alloc_page(object, pindex)) == NULL) {
1093 m = vm_phys_alloc_pages(object != NULL ?
1094 VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT, 0);
1095 #if VM_NRESERVLEVEL > 0
1096 if (m == NULL && vm_reserv_reclaim_inactive()) {
1097 m = vm_phys_alloc_pages(object != NULL ?
1098 VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT,
1105 * Not allocatable, give up.
1107 mtx_unlock(&vm_page_queue_free_mtx);
1108 atomic_add_int(&vm_pageout_deficit, 1);
1109 pagedaemon_wakeup();
1114 * At this point we had better have found a good page.
1117 KASSERT(m != NULL, ("vm_page_alloc: missing page"));
1118 KASSERT(m->queue == PQ_NONE,
1119 ("vm_page_alloc: page %p has unexpected queue %d", m, m->queue));
1120 KASSERT(m->wire_count == 0, ("vm_page_alloc: page %p is wired", m));
1121 KASSERT(m->hold_count == 0, ("vm_page_alloc: page %p is held", m));
1122 KASSERT(m->busy == 0, ("vm_page_alloc: page %p is busy", m));
1123 KASSERT(m->dirty == 0, ("vm_page_alloc: page %p is dirty", m));
1124 KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT,
1125 ("vm_page_alloc: page %p has unexpected memattr %d", m,
1126 pmap_page_get_memattr(m)));
1127 if ((m->flags & PG_CACHED) != 0) {
1128 KASSERT(m->valid != 0,
1129 ("vm_page_alloc: cached page %p is invalid", m));
1130 if (m->object == object && m->pindex == pindex)
1131 cnt.v_reactivated++;
1134 m_object = m->object;
1135 vm_page_cache_remove(m);
1136 if (m_object->type == OBJT_VNODE && m_object->cache == NULL)
1137 vp = m_object->handle;
1139 KASSERT(VM_PAGE_IS_FREE(m),
1140 ("vm_page_alloc: page %p is not free", m));
1141 KASSERT(m->valid == 0,
1142 ("vm_page_alloc: free page %p is valid", m));
1147 * Initialize structure. Only the PG_ZERO flag is inherited.
1150 if (m->flags & PG_ZERO) {
1151 vm_page_zero_count--;
1152 if (req & VM_ALLOC_ZERO)
1155 if (object == NULL || object->type == OBJT_PHYS)
1156 flags |= PG_UNMANAGED;
1158 if (req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ))
1161 m->oflags = VPO_BUSY;
1162 if (req & VM_ALLOC_WIRED) {
1163 atomic_add_int(&cnt.v_wire_count, 1);
1167 mtx_unlock(&vm_page_queue_free_mtx);
1169 if (object != NULL) {
1170 /* Ignore device objects; the pager sets "memattr" for them. */
1171 if (object->memattr != VM_MEMATTR_DEFAULT &&
1172 object->type != OBJT_DEVICE && object->type != OBJT_SG)
1173 pmap_page_set_memattr(m, object->memattr);
1174 vm_page_insert(m, object, pindex);
1179 * The following call to vdrop() must come after the above call
1180 * to vm_page_insert() in case both affect the same object and
1181 * vnode. Otherwise, the affected vnode's hold count could
1182 * temporarily become zero.
1188 * Don't wakeup too often - wakeup the pageout daemon when
1189 * we would be nearly out of memory.
1191 if (vm_paging_needed())
1192 pagedaemon_wakeup();
1198 * vm_wait: (also see VM_WAIT macro)
1200 * Block until free pages are available for allocation
1201 * - Called in various places before memory allocations.
1207 mtx_lock(&vm_page_queue_free_mtx);
1208 if (curproc == pageproc) {
1209 vm_pageout_pages_needed = 1;
1210 msleep(&vm_pageout_pages_needed, &vm_page_queue_free_mtx,
1211 PDROP | PSWP, "VMWait", 0);
1213 if (!vm_pages_needed) {
1214 vm_pages_needed = 1;
1215 wakeup(&vm_pages_needed);
1217 msleep(&cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PVM,
1223 * vm_waitpfault: (also see VM_WAITPFAULT macro)
1225 * Block until free pages are available for allocation
1226 * - Called only in vm_fault so that processes page faulting
1227 * can be easily tracked.
1228 * - Sleeps at a lower priority than vm_wait() so that vm_wait()ing
1229 * processes will be able to grab memory first. Do not change
1230 * this balance without careful testing first.
1236 mtx_lock(&vm_page_queue_free_mtx);
1237 if (!vm_pages_needed) {
1238 vm_pages_needed = 1;
1239 wakeup(&vm_pages_needed);
1241 msleep(&cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PUSER,
1248 * If the given page is contained within a page queue, move it to the tail
1251 * The page queues must be locked.
1254 vm_page_requeue(vm_page_t m)
1256 int queue = VM_PAGE_GETQUEUE(m);
1257 struct vpgqueues *vpq;
1259 if (queue != PQ_NONE) {
1260 vpq = &vm_page_queues[queue];
1261 TAILQ_REMOVE(&vpq->pl, m, pageq);
1262 TAILQ_INSERT_TAIL(&vpq->pl, m, pageq);
1269 * Remove a page from its queue.
1271 * The queue containing the given page must be locked.
1272 * This routine may not block.
1275 vm_pageq_remove(vm_page_t m)
1277 int queue = VM_PAGE_GETQUEUE(m);
1278 struct vpgqueues *pq;
1280 if (queue != PQ_NONE) {
1281 VM_PAGE_SETQUEUE2(m, PQ_NONE);
1282 pq = &vm_page_queues[queue];
1283 TAILQ_REMOVE(&pq->pl, m, pageq);
1291 * Add the given page to the specified queue.
1293 * The page queues must be locked.
1296 vm_page_enqueue(int queue, vm_page_t m)
1298 struct vpgqueues *vpq;
1300 vpq = &vm_page_queues[queue];
1301 VM_PAGE_SETQUEUE2(m, queue);
1302 TAILQ_INSERT_TAIL(&vpq->pl, m, pageq);
1309 * Put the specified page on the active list (if appropriate).
1310 * Ensure that act_count is at least ACT_INIT but do not otherwise
1313 * The page queues must be locked.
1314 * This routine may not block.
1317 vm_page_activate(vm_page_t m)
1320 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1321 if (VM_PAGE_GETKNOWNQUEUE2(m) != PQ_ACTIVE) {
1323 if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) {
1324 if (m->act_count < ACT_INIT)
1325 m->act_count = ACT_INIT;
1326 vm_page_enqueue(PQ_ACTIVE, m);
1329 if (m->act_count < ACT_INIT)
1330 m->act_count = ACT_INIT;
1335 * vm_page_free_wakeup:
1337 * Helper routine for vm_page_free_toq() and vm_page_cache(). This
1338 * routine is called when a page has been added to the cache or free
1341 * The page queues must be locked.
1342 * This routine may not block.
1345 vm_page_free_wakeup(void)
1348 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1350 * if pageout daemon needs pages, then tell it that there are
1353 if (vm_pageout_pages_needed &&
1354 cnt.v_cache_count + cnt.v_free_count >= cnt.v_pageout_free_min) {
1355 wakeup(&vm_pageout_pages_needed);
1356 vm_pageout_pages_needed = 0;
1359 * wakeup processes that are waiting on memory if we hit a
1360 * high water mark. And wakeup scheduler process if we have
1361 * lots of memory. this process will swapin processes.
1363 if (vm_pages_needed && !vm_page_count_min()) {
1364 vm_pages_needed = 0;
1365 wakeup(&cnt.v_free_count);
1372 * Returns the given page to the free list,
1373 * disassociating it with any VM object.
1375 * Object and page must be locked prior to entry.
1376 * This routine may not block.
1380 vm_page_free_toq(vm_page_t m)
1383 if (VM_PAGE_GETQUEUE(m) != PQ_NONE)
1384 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1385 KASSERT(!pmap_page_is_mapped(m),
1386 ("vm_page_free_toq: freeing mapped page %p", m));
1387 PCPU_INC(cnt.v_tfree);
1389 if (m->busy || VM_PAGE_IS_FREE(m)) {
1391 "vm_page_free: pindex(%lu), busy(%d), VPO_BUSY(%d), hold(%d)\n",
1392 (u_long)m->pindex, m->busy, (m->oflags & VPO_BUSY) ? 1 : 0,
1394 if (VM_PAGE_IS_FREE(m))
1395 panic("vm_page_free: freeing free page");
1397 panic("vm_page_free: freeing busy page");
1401 * unqueue, then remove page. Note that we cannot destroy
1402 * the page here because we do not want to call the pager's
1403 * callback routine until after we've put the page on the
1404 * appropriate free queue.
1410 * If fictitious remove object association and
1411 * return, otherwise delay object association removal.
1413 if ((m->flags & PG_FICTITIOUS) != 0) {
1420 if (m->wire_count != 0) {
1421 if (m->wire_count > 1) {
1422 panic("vm_page_free: invalid wire count (%d), pindex: 0x%lx",
1423 m->wire_count, (long)m->pindex);
1425 panic("vm_page_free: freeing wired page");
1427 if (m->hold_count != 0) {
1428 m->flags &= ~PG_ZERO;
1429 vm_page_enqueue(PQ_HOLD, m);
1432 * Restore the default memory attribute to the page.
1434 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT)
1435 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT);
1438 * Insert the page into the physical memory allocator's
1439 * cache/free page queues.
1441 mtx_lock(&vm_page_queue_free_mtx);
1442 m->flags |= PG_FREE;
1444 #if VM_NRESERVLEVEL > 0
1445 if (!vm_reserv_free_page(m))
1449 vm_phys_free_pages(m, 0);
1450 if ((m->flags & PG_ZERO) != 0)
1451 ++vm_page_zero_count;
1453 vm_page_zero_idle_wakeup();
1454 vm_page_free_wakeup();
1455 mtx_unlock(&vm_page_queue_free_mtx);
1462 * Mark this page as wired down by yet
1463 * another map, removing it from paging queues
1466 * The page queues must be locked.
1467 * This routine may not block.
1470 vm_page_wire(vm_page_t m)
1474 * Only bump the wire statistics if the page is not already wired,
1475 * and only unqueue the page if it is on some queue (if it is unmanaged
1476 * it is already off the queues).
1478 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1479 if (m->flags & PG_FICTITIOUS)
1481 if (m->wire_count == 0) {
1482 if ((m->flags & PG_UNMANAGED) == 0)
1484 atomic_add_int(&cnt.v_wire_count, 1);
1487 KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m));
1493 * Release one wiring of this page, potentially
1494 * enabling it to be paged again.
1496 * Many pages placed on the inactive queue should actually go
1497 * into the cache, but it is difficult to figure out which. What
1498 * we do instead, if the inactive target is well met, is to put
1499 * clean pages at the head of the inactive queue instead of the tail.
1500 * This will cause them to be moved to the cache more quickly and
1501 * if not actively re-referenced, freed more quickly. If we just
1502 * stick these pages at the end of the inactive queue, heavy filesystem
1503 * meta-data accesses can cause an unnecessary paging load on memory bound
1504 * processes. This optimization causes one-time-use metadata to be
1505 * reused more quickly.
1507 * BUT, if we are in a low-memory situation we have no choice but to
1508 * put clean pages on the cache queue.
1510 * A number of routines use vm_page_unwire() to guarantee that the page
1511 * will go into either the inactive or active queues, and will NEVER
1512 * be placed in the cache - for example, just after dirtying a page.
1513 * dirty pages in the cache are not allowed.
1515 * The page queues must be locked.
1516 * This routine may not block.
1519 vm_page_unwire(vm_page_t m, int activate)
1522 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1523 if (m->flags & PG_FICTITIOUS)
1525 if (m->wire_count > 0) {
1527 if (m->wire_count == 0) {
1528 atomic_subtract_int(&cnt.v_wire_count, 1);
1529 if (m->flags & PG_UNMANAGED) {
1531 } else if (activate)
1532 vm_page_enqueue(PQ_ACTIVE, m);
1534 vm_page_flag_clear(m, PG_WINATCFLS);
1535 vm_page_enqueue(PQ_INACTIVE, m);
1539 panic("vm_page_unwire: invalid wire count: %d", m->wire_count);
1545 * Move the specified page to the inactive queue. If the page has
1546 * any associated swap, the swap is deallocated.
1548 * Normally athead is 0 resulting in LRU operation. athead is set
1549 * to 1 if we want this page to be 'as if it were placed in the cache',
1550 * except without unmapping it from the process address space.
1552 * This routine may not block.
1555 _vm_page_deactivate(vm_page_t m, int athead)
1558 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1561 * Ignore if already inactive.
1563 if (VM_PAGE_INQUEUE2(m, PQ_INACTIVE))
1565 if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) {
1566 vm_page_flag_clear(m, PG_WINATCFLS);
1569 TAILQ_INSERT_HEAD(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
1571 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
1572 VM_PAGE_SETQUEUE2(m, PQ_INACTIVE);
1573 cnt.v_inactive_count++;
1578 vm_page_deactivate(vm_page_t m)
1580 _vm_page_deactivate(m, 0);
1584 * vm_page_try_to_cache:
1586 * Returns 0 on failure, 1 on success
1589 vm_page_try_to_cache(vm_page_t m)
1592 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1593 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1594 if (m->dirty || m->hold_count || m->busy || m->wire_count ||
1595 (m->oflags & VPO_BUSY) || (m->flags & PG_UNMANAGED)) {
1606 * vm_page_try_to_free()
1608 * Attempt to free the page. If we cannot free it, we do nothing.
1609 * 1 is returned on success, 0 on failure.
1612 vm_page_try_to_free(vm_page_t m)
1615 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1616 if (m->object != NULL)
1617 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1618 if (m->dirty || m->hold_count || m->busy || m->wire_count ||
1619 (m->oflags & VPO_BUSY) || (m->flags & PG_UNMANAGED)) {
1632 * Put the specified page onto the page cache queue (if appropriate).
1634 * This routine may not block.
1637 vm_page_cache(vm_page_t m)
1642 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1644 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1645 if ((m->flags & PG_UNMANAGED) || (m->oflags & VPO_BUSY) || m->busy ||
1646 m->hold_count || m->wire_count) {
1647 panic("vm_page_cache: attempting to cache busy page");
1651 panic("vm_page_cache: page %p is dirty", m);
1652 if (m->valid == 0 || object->type == OBJT_DEFAULT ||
1653 (object->type == OBJT_SWAP &&
1654 !vm_pager_has_page(object, m->pindex, NULL, NULL))) {
1656 * Hypothesis: A cache-elgible page belonging to a
1657 * default object or swap object but without a backing
1658 * store must be zero filled.
1663 KASSERT((m->flags & PG_CACHED) == 0,
1664 ("vm_page_cache: page %p is already cached", m));
1668 * Remove the page from the paging queues.
1673 * Remove the page from the object's collection of resident
1676 if (m != object->root)
1677 vm_page_splay(m->pindex, object->root);
1678 if (m->left == NULL)
1681 root = vm_page_splay(m->pindex, m->left);
1682 root->right = m->right;
1684 object->root = root;
1685 TAILQ_REMOVE(&object->memq, m, listq);
1686 object->resident_page_count--;
1687 object->generation++;
1690 * Restore the default memory attribute to the page.
1692 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT)
1693 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT);
1696 * Insert the page into the object's collection of cached pages
1697 * and the physical memory allocator's cache/free page queues.
1699 vm_page_flag_clear(m, PG_ZERO);
1700 mtx_lock(&vm_page_queue_free_mtx);
1701 m->flags |= PG_CACHED;
1702 cnt.v_cache_count++;
1703 root = object->cache;
1708 root = vm_page_splay(m->pindex, root);
1709 if (m->pindex < root->pindex) {
1710 m->left = root->left;
1713 } else if (__predict_false(m->pindex == root->pindex))
1714 panic("vm_page_cache: offset already cached");
1716 m->right = root->right;
1722 #if VM_NRESERVLEVEL > 0
1723 if (!vm_reserv_free_page(m)) {
1727 vm_phys_set_pool(VM_FREEPOOL_CACHE, m, 0);
1728 vm_phys_free_pages(m, 0);
1730 vm_page_free_wakeup();
1731 mtx_unlock(&vm_page_queue_free_mtx);
1734 * Increment the vnode's hold count if this is the object's only
1735 * cached page. Decrement the vnode's hold count if this was
1736 * the object's only resident page.
1738 if (object->type == OBJT_VNODE) {
1739 if (root == NULL && object->resident_page_count != 0)
1740 vhold(object->handle);
1741 else if (root != NULL && object->resident_page_count == 0)
1742 vdrop(object->handle);
1749 * Cache, deactivate, or do nothing as appropriate. This routine
1750 * is typically used by madvise() MADV_DONTNEED.
1752 * Generally speaking we want to move the page into the cache so
1753 * it gets reused quickly. However, this can result in a silly syndrome
1754 * due to the page recycling too quickly. Small objects will not be
1755 * fully cached. On the otherhand, if we move the page to the inactive
1756 * queue we wind up with a problem whereby very large objects
1757 * unnecessarily blow away our inactive and cache queues.
1759 * The solution is to move the pages based on a fixed weighting. We
1760 * either leave them alone, deactivate them, or move them to the cache,
1761 * where moving them to the cache has the highest weighting.
1762 * By forcing some pages into other queues we eventually force the
1763 * system to balance the queues, potentially recovering other unrelated
1764 * space from active. The idea is to not force this to happen too
1768 vm_page_dontneed(vm_page_t m)
1770 static int dnweight;
1774 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1778 * occassionally leave the page alone
1780 if ((dnw & 0x01F0) == 0 ||
1781 VM_PAGE_INQUEUE2(m, PQ_INACTIVE)) {
1782 if (m->act_count >= ACT_INIT)
1788 * Clear any references to the page. Otherwise, the page daemon will
1789 * immediately reactivate the page.
1791 vm_page_flag_clear(m, PG_REFERENCED);
1792 pmap_clear_reference(m);
1794 if (m->dirty == 0 && pmap_is_modified(m))
1797 if (m->dirty || (dnw & 0x0070) == 0) {
1799 * Deactivate the page 3 times out of 32.
1804 * Cache the page 28 times out of every 32. Note that
1805 * the page is deactivated instead of cached, but placed
1806 * at the head of the queue instead of the tail.
1810 _vm_page_deactivate(m, head);
1814 * Grab a page, waiting until we are waken up due to the page
1815 * changing state. We keep on waiting, if the page continues
1816 * to be in the object. If the page doesn't exist, first allocate it
1817 * and then conditionally zero it.
1819 * This routine may block.
1822 vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags)
1826 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1828 if ((m = vm_page_lookup(object, pindex)) != NULL) {
1829 if (vm_page_sleep_if_busy(m, TRUE, "pgrbwt")) {
1830 if ((allocflags & VM_ALLOC_RETRY) == 0)
1834 if ((allocflags & VM_ALLOC_WIRED) != 0) {
1835 vm_page_lock_queues();
1837 vm_page_unlock_queues();
1839 if ((allocflags & VM_ALLOC_NOBUSY) == 0)
1844 m = vm_page_alloc(object, pindex, allocflags & ~VM_ALLOC_RETRY);
1846 VM_OBJECT_UNLOCK(object);
1848 VM_OBJECT_LOCK(object);
1849 if ((allocflags & VM_ALLOC_RETRY) == 0)
1852 } else if (m->valid != 0)
1854 if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0)
1860 * Mapping function for valid bits or for dirty bits in
1861 * a page. May not block.
1863 * Inputs are required to range within a page.
1866 vm_page_bits(int base, int size)
1872 base + size <= PAGE_SIZE,
1873 ("vm_page_bits: illegal base/size %d/%d", base, size)
1876 if (size == 0) /* handle degenerate case */
1879 first_bit = base >> DEV_BSHIFT;
1880 last_bit = (base + size - 1) >> DEV_BSHIFT;
1882 return ((2 << last_bit) - (1 << first_bit));
1886 * vm_page_set_valid:
1888 * Sets portions of a page valid. The arguments are expected
1889 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
1890 * of any partial chunks touched by the range. The invalid portion of
1891 * such chunks will be zeroed.
1893 * (base + size) must be less then or equal to PAGE_SIZE.
1896 vm_page_set_valid(vm_page_t m, int base, int size)
1900 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1901 if (size == 0) /* handle degenerate case */
1905 * If the base is not DEV_BSIZE aligned and the valid
1906 * bit is clear, we have to zero out a portion of the
1909 if ((frag = base & ~(DEV_BSIZE - 1)) != base &&
1910 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0)
1911 pmap_zero_page_area(m, frag, base - frag);
1914 * If the ending offset is not DEV_BSIZE aligned and the
1915 * valid bit is clear, we have to zero out a portion of
1918 endoff = base + size;
1919 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff &&
1920 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0)
1921 pmap_zero_page_area(m, endoff,
1922 DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
1925 * Assert that no previously invalid block that is now being validated
1928 KASSERT((~m->valid & vm_page_bits(base, size) & m->dirty) == 0,
1929 ("vm_page_set_valid: page %p is dirty", m));
1932 * Set valid bits inclusive of any overlap.
1934 m->valid |= vm_page_bits(base, size);
1938 * vm_page_set_validclean:
1940 * Sets portions of a page valid and clean. The arguments are expected
1941 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
1942 * of any partial chunks touched by the range. The invalid portion of
1943 * such chunks will be zero'd.
1945 * This routine may not block.
1947 * (base + size) must be less then or equal to PAGE_SIZE.
1950 vm_page_set_validclean(vm_page_t m, int base, int size)
1956 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1957 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1958 if (size == 0) /* handle degenerate case */
1962 * If the base is not DEV_BSIZE aligned and the valid
1963 * bit is clear, we have to zero out a portion of the
1966 if ((frag = base & ~(DEV_BSIZE - 1)) != base &&
1967 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0)
1968 pmap_zero_page_area(m, frag, base - frag);
1971 * If the ending offset is not DEV_BSIZE aligned and the
1972 * valid bit is clear, we have to zero out a portion of
1975 endoff = base + size;
1976 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff &&
1977 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0)
1978 pmap_zero_page_area(m, endoff,
1979 DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
1982 * Set valid, clear dirty bits. If validating the entire
1983 * page we can safely clear the pmap modify bit. We also
1984 * use this opportunity to clear the VPO_NOSYNC flag. If a process
1985 * takes a write fault on a MAP_NOSYNC memory area the flag will
1988 * We set valid bits inclusive of any overlap, but we can only
1989 * clear dirty bits for DEV_BSIZE chunks that are fully within
1992 pagebits = vm_page_bits(base, size);
1993 m->valid |= pagebits;
1995 if ((frag = base & (DEV_BSIZE - 1)) != 0) {
1996 frag = DEV_BSIZE - frag;
2002 pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1));
2004 m->dirty &= ~pagebits;
2005 if (base == 0 && size == PAGE_SIZE) {
2006 pmap_clear_modify(m);
2007 m->oflags &= ~VPO_NOSYNC;
2012 vm_page_clear_dirty(vm_page_t m, int base, int size)
2015 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2016 m->dirty &= ~vm_page_bits(base, size);
2020 * vm_page_set_invalid:
2022 * Invalidates DEV_BSIZE'd chunks within a page. Both the
2023 * valid and dirty bits for the effected areas are cleared.
2028 vm_page_set_invalid(vm_page_t m, int base, int size)
2032 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2033 bits = vm_page_bits(base, size);
2034 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2035 if (m->valid == VM_PAGE_BITS_ALL && bits != 0)
2039 m->object->generation++;
2043 * vm_page_zero_invalid()
2045 * The kernel assumes that the invalid portions of a page contain
2046 * garbage, but such pages can be mapped into memory by user code.
2047 * When this occurs, we must zero out the non-valid portions of the
2048 * page so user code sees what it expects.
2050 * Pages are most often semi-valid when the end of a file is mapped
2051 * into memory and the file's size is not page aligned.
2054 vm_page_zero_invalid(vm_page_t m, boolean_t setvalid)
2059 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2061 * Scan the valid bits looking for invalid sections that
2062 * must be zerod. Invalid sub-DEV_BSIZE'd areas ( where the
2063 * valid bit may be set ) have already been zerod by
2064 * vm_page_set_validclean().
2066 for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) {
2067 if (i == (PAGE_SIZE / DEV_BSIZE) ||
2068 (m->valid & (1 << i))
2071 pmap_zero_page_area(m,
2072 b << DEV_BSHIFT, (i - b) << DEV_BSHIFT);
2079 * setvalid is TRUE when we can safely set the zero'd areas
2080 * as being valid. We can do this if there are no cache consistancy
2081 * issues. e.g. it is ok to do with UFS, but not ok to do with NFS.
2084 m->valid = VM_PAGE_BITS_ALL;
2090 * Is (partial) page valid? Note that the case where size == 0
2091 * will return FALSE in the degenerate case where the page is
2092 * entirely invalid, and TRUE otherwise.
2097 vm_page_is_valid(vm_page_t m, int base, int size)
2099 int bits = vm_page_bits(base, size);
2101 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2102 if (m->valid && ((m->valid & bits) == bits))
2109 * update dirty bits from pmap/mmu. May not block.
2112 vm_page_test_dirty(vm_page_t m)
2114 if ((m->dirty != VM_PAGE_BITS_ALL) && pmap_is_modified(m)) {
2119 int so_zerocp_fullpage = 0;
2122 * Replace the given page with a copy. The copied page assumes
2123 * the portion of the given page's "wire_count" that is not the
2124 * responsibility of this copy-on-write mechanism.
2126 * The object containing the given page must have a non-zero
2127 * paging-in-progress count and be locked.
2130 vm_page_cowfault(vm_page_t m)
2137 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2138 KASSERT(object->paging_in_progress != 0,
2139 ("vm_page_cowfault: object %p's paging-in-progress count is zero.",
2146 mnew = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL | VM_ALLOC_NOBUSY);
2148 vm_page_insert(m, object, pindex);
2149 vm_page_unlock_queues();
2150 VM_OBJECT_UNLOCK(object);
2152 VM_OBJECT_LOCK(object);
2153 if (m == vm_page_lookup(object, pindex)) {
2154 vm_page_lock_queues();
2158 * Page disappeared during the wait.
2160 vm_page_lock_queues();
2167 * check to see if we raced with an xmit complete when
2168 * waiting to allocate a page. If so, put things back
2172 vm_page_insert(m, object, pindex);
2173 } else { /* clear COW & copy page */
2174 if (!so_zerocp_fullpage)
2175 pmap_copy_page(m, mnew);
2176 mnew->valid = VM_PAGE_BITS_ALL;
2177 vm_page_dirty(mnew);
2178 mnew->wire_count = m->wire_count - m->cow;
2179 m->wire_count = m->cow;
2184 vm_page_cowclear(vm_page_t m)
2187 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2191 * let vm_fault add back write permission lazily
2195 * sf_buf_free() will free the page, so we needn't do it here
2200 vm_page_cowsetup(vm_page_t m)
2203 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2204 if (m->cow == USHRT_MAX - 1)
2207 pmap_remove_write(m);
2211 #include "opt_ddb.h"
2213 #include <sys/kernel.h>
2215 #include <ddb/ddb.h>
2217 DB_SHOW_COMMAND(page, vm_page_print_page_info)
2219 db_printf("cnt.v_free_count: %d\n", cnt.v_free_count);
2220 db_printf("cnt.v_cache_count: %d\n", cnt.v_cache_count);
2221 db_printf("cnt.v_inactive_count: %d\n", cnt.v_inactive_count);
2222 db_printf("cnt.v_active_count: %d\n", cnt.v_active_count);
2223 db_printf("cnt.v_wire_count: %d\n", cnt.v_wire_count);
2224 db_printf("cnt.v_free_reserved: %d\n", cnt.v_free_reserved);
2225 db_printf("cnt.v_free_min: %d\n", cnt.v_free_min);
2226 db_printf("cnt.v_free_target: %d\n", cnt.v_free_target);
2227 db_printf("cnt.v_cache_min: %d\n", cnt.v_cache_min);
2228 db_printf("cnt.v_inactive_target: %d\n", cnt.v_inactive_target);
2231 DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info)
2234 db_printf("PQ_FREE:");
2235 db_printf(" %d", cnt.v_free_count);
2238 db_printf("PQ_CACHE:");
2239 db_printf(" %d", cnt.v_cache_count);
2242 db_printf("PQ_ACTIVE: %d, PQ_INACTIVE: %d\n",
2243 *vm_page_queues[PQ_ACTIVE].cnt,
2244 *vm_page_queues[PQ_INACTIVE].cnt);