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);
156 * Sets the page size, perhaps based upon the memory
157 * size. Must be called before any use of page-size
158 * dependent functions.
161 vm_set_page_size(void)
163 if (cnt.v_page_size == 0)
164 cnt.v_page_size = PAGE_SIZE;
165 if (((cnt.v_page_size - 1) & cnt.v_page_size) != 0)
166 panic("vm_set_page_size: page size not a power of two");
170 * vm_page_blacklist_lookup:
172 * See if a physical address in this page has been listed
173 * in the blacklist tunable. Entries in the tunable are
174 * separated by spaces or commas. If an invalid integer is
175 * encountered then the rest of the string is skipped.
178 vm_page_blacklist_lookup(char *list, vm_paddr_t pa)
183 for (pos = list; *pos != '\0'; pos = cp) {
184 bad = strtoq(pos, &cp, 0);
186 if (*cp == ' ' || *cp == ',') {
193 if (pa == trunc_page(bad))
202 * Initializes the resident memory module.
204 * Allocates memory for the page cells, and
205 * for the object/offset-to-page hash table headers.
206 * Each page cell is initialized and placed on the free list.
209 vm_page_startup(vm_offset_t vaddr)
212 vm_paddr_t page_range;
220 /* the biggest memory array is the second group of pages */
222 vm_paddr_t biggestsize;
223 vm_paddr_t low_water, high_water;
229 vaddr = round_page(vaddr);
231 for (i = 0; phys_avail[i + 1]; i += 2) {
232 phys_avail[i] = round_page(phys_avail[i]);
233 phys_avail[i + 1] = trunc_page(phys_avail[i + 1]);
236 low_water = phys_avail[0];
237 high_water = phys_avail[1];
239 for (i = 0; phys_avail[i + 1]; i += 2) {
240 vm_paddr_t size = phys_avail[i + 1] - phys_avail[i];
242 if (size > biggestsize) {
246 if (phys_avail[i] < low_water)
247 low_water = phys_avail[i];
248 if (phys_avail[i + 1] > high_water)
249 high_water = phys_avail[i + 1];
253 end = phys_avail[biggestone+1];
256 * Initialize the locks.
258 mtx_init(&vm_page_queue_mtx, "vm page queue mutex", NULL, MTX_DEF |
260 mtx_init(&vm_page_queue_free_mtx, "vm page queue free mutex", NULL,
264 * Initialize the queue headers for the free queue, the active queue
265 * and the inactive queue.
267 for (i = 0; i < PQ_COUNT; i++)
268 TAILQ_INIT(&vm_page_queues[i].pl);
269 vm_page_queues[PQ_INACTIVE].cnt = &cnt.v_inactive_count;
270 vm_page_queues[PQ_ACTIVE].cnt = &cnt.v_active_count;
271 vm_page_queues[PQ_HOLD].cnt = &cnt.v_active_count;
274 * Allocate memory for use when boot strapping the kernel memory
277 new_end = end - (boot_pages * UMA_SLAB_SIZE);
278 new_end = trunc_page(new_end);
279 mapped = pmap_map(&vaddr, new_end, end,
280 VM_PROT_READ | VM_PROT_WRITE);
281 bzero((void *)mapped, end - new_end);
282 uma_startup((void *)mapped, boot_pages);
284 #if defined(__amd64__) || defined(__i386__)
286 * Allocate a bitmap to indicate that a random physical page
287 * needs to be included in a minidump.
289 * The amd64 port needs this to indicate which direct map pages
290 * need to be dumped, via calls to dump_add_page()/dump_drop_page().
292 * However, i386 still needs this workspace internally within the
293 * minidump code. In theory, they are not needed on i386, but are
294 * included should the sf_buf code decide to use them.
296 page_range = phys_avail[(nblocks - 1) * 2 + 1] / PAGE_SIZE;
297 vm_page_dump_size = round_page(roundup2(page_range, NBBY) / NBBY);
298 new_end -= vm_page_dump_size;
299 vm_page_dump = (void *)(uintptr_t)pmap_map(&vaddr, new_end,
300 new_end + vm_page_dump_size, VM_PROT_READ | VM_PROT_WRITE);
301 bzero((void *)vm_page_dump, vm_page_dump_size);
304 * Compute the number of pages of memory that will be available for
305 * use (taking into account the overhead of a page structure per
308 first_page = low_water / PAGE_SIZE;
309 #ifdef VM_PHYSSEG_SPARSE
311 for (i = 0; phys_avail[i + 1] != 0; i += 2)
312 page_range += atop(phys_avail[i + 1] - phys_avail[i]);
313 #elif defined(VM_PHYSSEG_DENSE)
314 page_range = high_water / PAGE_SIZE - first_page;
316 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
321 * Reserve an unmapped guard page to trap access to vm_page_array[-1].
326 * Initialize the mem entry structures now, and put them in the free
329 new_end = trunc_page(end - page_range * sizeof(struct vm_page));
330 mapped = pmap_map(&vaddr, new_end, end,
331 VM_PROT_READ | VM_PROT_WRITE);
332 vm_page_array = (vm_page_t) mapped;
333 #if VM_NRESERVLEVEL > 0
335 * Allocate memory for the reservation management system's data
338 new_end = vm_reserv_startup(&vaddr, new_end, high_water);
342 * pmap_map on amd64 comes out of the direct-map, not kvm like i386,
343 * so the pages must be tracked for a crashdump to include this data.
344 * This includes the vm_page_array and the early UMA bootstrap pages.
346 for (pa = new_end; pa < phys_avail[biggestone + 1]; pa += PAGE_SIZE)
349 phys_avail[biggestone + 1] = new_end;
352 * Clear all of the page structures
354 bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page));
355 for (i = 0; i < page_range; i++)
356 vm_page_array[i].order = VM_NFREEORDER;
357 vm_page_array_size = page_range;
360 * Initialize the physical memory allocator.
365 * Add every available physical page that is not blacklisted to
368 cnt.v_page_count = 0;
369 cnt.v_free_count = 0;
370 list = getenv("vm.blacklist");
371 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
373 last_pa = phys_avail[i + 1];
374 while (pa < last_pa) {
376 vm_page_blacklist_lookup(list, pa))
377 printf("Skipping page with pa 0x%jx\n",
380 vm_phys_add_page(pa);
385 #if VM_NRESERVLEVEL > 0
387 * Initialize the reservation management system.
395 vm_page_flag_set(vm_page_t m, unsigned short bits)
398 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
403 vm_page_flag_clear(vm_page_t m, unsigned short bits)
406 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
411 vm_page_busy(vm_page_t m)
414 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
415 KASSERT((m->oflags & VPO_BUSY) == 0,
416 ("vm_page_busy: page already busy!!!"));
417 m->oflags |= VPO_BUSY;
423 * wakeup anyone waiting for the page.
426 vm_page_flash(vm_page_t m)
429 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
430 if (m->oflags & VPO_WANTED) {
431 m->oflags &= ~VPO_WANTED;
439 * clear the VPO_BUSY flag and wakeup anyone waiting for the
444 vm_page_wakeup(vm_page_t m)
447 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
448 KASSERT(m->oflags & VPO_BUSY, ("vm_page_wakeup: page not busy!!!"));
449 m->oflags &= ~VPO_BUSY;
454 vm_page_io_start(vm_page_t m)
457 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
462 vm_page_io_finish(vm_page_t m)
465 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
472 * Keep page from being freed by the page daemon
473 * much of the same effect as wiring, except much lower
474 * overhead and should be used only for *very* temporary
475 * holding ("wiring").
478 vm_page_hold(vm_page_t mem)
481 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
486 vm_page_unhold(vm_page_t mem)
489 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
491 KASSERT(mem->hold_count >= 0, ("vm_page_unhold: hold count < 0!!!"));
492 if (mem->hold_count == 0 && VM_PAGE_INQUEUE2(mem, PQ_HOLD))
493 vm_page_free_toq(mem);
502 vm_page_free(vm_page_t m)
505 m->flags &= ~PG_ZERO;
512 * Free a page to the zerod-pages queue
515 vm_page_free_zero(vm_page_t m)
525 * Sleep and release the page queues lock.
527 * The object containing the given page must be locked.
530 vm_page_sleep(vm_page_t m, const char *msg)
533 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
534 if (!mtx_owned(&vm_page_queue_mtx))
535 vm_page_lock_queues();
536 vm_page_flag_set(m, PG_REFERENCED);
537 vm_page_unlock_queues();
540 * It's possible that while we sleep, the page will get
541 * unbusied and freed. If we are holding the object
542 * lock, we will assume we hold a reference to the object
543 * such that even if m->object changes, we can re-lock
546 m->oflags |= VPO_WANTED;
547 msleep(m, VM_OBJECT_MTX(m->object), PVM, msg, 0);
553 * make page all dirty
556 vm_page_dirty(vm_page_t m)
558 KASSERT((m->flags & PG_CACHED) == 0,
559 ("vm_page_dirty: page in cache!"));
560 KASSERT(!VM_PAGE_IS_FREE(m),
561 ("vm_page_dirty: page is free!"));
562 m->dirty = VM_PAGE_BITS_ALL;
568 * Implements Sleator and Tarjan's top-down splay algorithm. Returns
569 * the vm_page containing the given pindex. If, however, that
570 * pindex is not found in the vm_object, returns a vm_page that is
571 * adjacent to the pindex, coming before or after it.
574 vm_page_splay(vm_pindex_t pindex, vm_page_t root)
576 struct vm_page dummy;
577 vm_page_t lefttreemax, righttreemin, y;
581 lefttreemax = righttreemin = &dummy;
583 if (pindex < root->pindex) {
584 if ((y = root->left) == NULL)
586 if (pindex < y->pindex) {
588 root->left = y->right;
591 if ((y = root->left) == NULL)
594 /* Link into the new root's right tree. */
595 righttreemin->left = root;
597 } else if (pindex > root->pindex) {
598 if ((y = root->right) == NULL)
600 if (pindex > y->pindex) {
602 root->right = y->left;
605 if ((y = root->right) == NULL)
608 /* Link into the new root's left tree. */
609 lefttreemax->right = root;
614 /* Assemble the new root. */
615 lefttreemax->right = root->left;
616 righttreemin->left = root->right;
617 root->left = dummy.right;
618 root->right = dummy.left;
623 * vm_page_insert: [ internal use only ]
625 * Inserts the given mem entry into the object and object list.
627 * The pagetables are not updated but will presumably fault the page
628 * in if necessary, or if a kernel page the caller will at some point
629 * enter the page into the kernel's pmap. We are not allowed to block
630 * here so we *can't* do this anyway.
632 * The object and page must be locked.
633 * This routine may not block.
636 vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex)
640 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
641 if (m->object != NULL)
642 panic("vm_page_insert: page already inserted");
645 * Record the object/offset pair in this page
651 * Now link into the object's ordered list of backed pages.
657 TAILQ_INSERT_TAIL(&object->memq, m, listq);
659 root = vm_page_splay(pindex, root);
660 if (pindex < root->pindex) {
661 m->left = root->left;
664 TAILQ_INSERT_BEFORE(root, m, listq);
665 } else if (pindex == root->pindex)
666 panic("vm_page_insert: offset already allocated");
668 m->right = root->right;
671 TAILQ_INSERT_AFTER(&object->memq, root, m, listq);
675 object->generation++;
678 * show that the object has one more resident page.
680 object->resident_page_count++;
682 * Hold the vnode until the last page is released.
684 if (object->resident_page_count == 1 && object->type == OBJT_VNODE)
685 vhold((struct vnode *)object->handle);
688 * Since we are inserting a new and possibly dirty page,
689 * update the object's OBJ_MIGHTBEDIRTY flag.
691 if (m->flags & PG_WRITEABLE)
692 vm_object_set_writeable_dirty(object);
697 * NOTE: used by device pager as well -wfj
699 * Removes the given mem entry from the object/offset-page
700 * table and the object page list, but do not invalidate/terminate
703 * The object and page must be locked.
704 * The underlying pmap entry (if any) is NOT removed here.
705 * This routine may not block.
708 vm_page_remove(vm_page_t m)
713 if ((object = m->object) == NULL)
715 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
716 if (m->oflags & VPO_BUSY) {
717 m->oflags &= ~VPO_BUSY;
720 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
723 * Now remove from the object's list of backed pages.
725 if (m != object->root)
726 vm_page_splay(m->pindex, object->root);
730 root = vm_page_splay(m->pindex, m->left);
731 root->right = m->right;
734 TAILQ_REMOVE(&object->memq, m, listq);
737 * And show that the object has one fewer resident page.
739 object->resident_page_count--;
740 object->generation++;
742 * The vnode may now be recycled.
744 if (object->resident_page_count == 0 && object->type == OBJT_VNODE)
745 vdrop((struct vnode *)object->handle);
753 * Returns the page associated with the object/offset
754 * pair specified; if none is found, NULL is returned.
756 * The object must be locked.
757 * This routine may not block.
758 * This is a critical path routine
761 vm_page_lookup(vm_object_t object, vm_pindex_t pindex)
765 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
766 if ((m = object->root) != NULL && m->pindex != pindex) {
767 m = vm_page_splay(pindex, m);
768 if ((object->root = m)->pindex != pindex)
777 * Move the given memory entry from its
778 * current object to the specified target object/offset.
780 * The object must be locked.
781 * This routine may not block.
783 * Note: swap associated with the page must be invalidated by the move. We
784 * have to do this for several reasons: (1) we aren't freeing the
785 * page, (2) we are dirtying the page, (3) the VM system is probably
786 * moving the page from object A to B, and will then later move
787 * the backing store from A to B and we can't have a conflict.
789 * Note: we *always* dirty the page. It is necessary both for the
790 * fact that we moved it, and because we may be invalidating
791 * swap. If the page is on the cache, we have to deactivate it
792 * or vm_page_dirty() will panic. Dirty pages are not allowed
796 vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex)
800 vm_page_insert(m, new_object, new_pindex);
805 * Convert all of the given object's cached pages that have a
806 * pindex within the given range into free pages. If the value
807 * zero is given for "end", then the range's upper bound is
808 * infinity. If the given object is backed by a vnode and it
809 * transitions from having one or more cached pages to none, the
810 * vnode's hold count is reduced.
813 vm_page_cache_free(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
818 mtx_lock(&vm_page_queue_free_mtx);
819 if (__predict_false(object->cache == NULL)) {
820 mtx_unlock(&vm_page_queue_free_mtx);
823 m = object->cache = vm_page_splay(start, object->cache);
824 if (m->pindex < start) {
825 if (m->right == NULL)
828 m_next = vm_page_splay(start, m->right);
831 m = object->cache = m_next;
836 * At this point, "m" is either (1) a reference to the page
837 * with the least pindex that is greater than or equal to
838 * "start" or (2) NULL.
840 for (; m != NULL && (m->pindex < end || end == 0); m = m_next) {
842 * Find "m"'s successor and remove "m" from the
845 if (m->right == NULL) {
846 object->cache = m->left;
849 m_next = vm_page_splay(start, m->right);
850 m_next->left = m->left;
851 object->cache = m_next;
853 /* Convert "m" to a free page. */
856 /* Clear PG_CACHED and set PG_FREE. */
857 m->flags ^= PG_CACHED | PG_FREE;
858 KASSERT((m->flags & (PG_CACHED | PG_FREE)) == PG_FREE,
859 ("vm_page_cache_free: page %p has inconsistent flags", m));
863 empty = object->cache == NULL;
864 mtx_unlock(&vm_page_queue_free_mtx);
865 if (object->type == OBJT_VNODE && empty)
866 vdrop(object->handle);
870 * Returns the cached page that is associated with the given
871 * object and offset. If, however, none exists, returns NULL.
873 * The free page queue must be locked.
875 static inline vm_page_t
876 vm_page_cache_lookup(vm_object_t object, vm_pindex_t pindex)
880 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
881 if ((m = object->cache) != NULL && m->pindex != pindex) {
882 m = vm_page_splay(pindex, m);
883 if ((object->cache = m)->pindex != pindex)
890 * Remove the given cached page from its containing object's
891 * collection of cached pages.
893 * The free page queue must be locked.
896 vm_page_cache_remove(vm_page_t m)
901 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
902 KASSERT((m->flags & PG_CACHED) != 0,
903 ("vm_page_cache_remove: page %p is not cached", m));
905 if (m != object->cache) {
906 root = vm_page_splay(m->pindex, object->cache);
908 ("vm_page_cache_remove: page %p is not cached in object %p",
913 else if (m->right == NULL)
916 root = vm_page_splay(m->pindex, m->left);
917 root->right = m->right;
919 object->cache = root;
925 * Transfer all of the cached pages with offset greater than or
926 * equal to 'offidxstart' from the original object's cache to the
927 * new object's cache. However, any cached pages with offset
928 * greater than or equal to the new object's size are kept in the
929 * original object. Initially, the new object's cache must be
930 * empty. Offset 'offidxstart' in the original object must
931 * correspond to offset zero in the new object.
933 * The new object must be locked.
936 vm_page_cache_transfer(vm_object_t orig_object, vm_pindex_t offidxstart,
937 vm_object_t new_object)
942 * Insertion into an object's collection of cached pages
943 * requires the object to be locked. In contrast, removal does
946 VM_OBJECT_LOCK_ASSERT(new_object, MA_OWNED);
947 KASSERT(new_object->cache == NULL,
948 ("vm_page_cache_transfer: object %p has cached pages",
950 mtx_lock(&vm_page_queue_free_mtx);
951 if ((m = orig_object->cache) != NULL) {
953 * Transfer all of the pages with offset greater than or
954 * equal to 'offidxstart' from the original object's
955 * cache to the new object's cache.
957 m = vm_page_splay(offidxstart, m);
958 if (m->pindex < offidxstart) {
959 orig_object->cache = m;
960 new_object->cache = m->right;
963 orig_object->cache = m->left;
964 new_object->cache = m;
967 while ((m = new_object->cache) != NULL) {
968 if ((m->pindex - offidxstart) >= new_object->size) {
970 * Return all of the cached pages with
971 * offset greater than or equal to the
972 * new object's size to the original
975 new_object->cache = m->left;
976 m->left = orig_object->cache;
977 orig_object->cache = m;
980 m_next = vm_page_splay(m->pindex, m->right);
981 /* Update the page's object and offset. */
982 m->object = new_object;
983 m->pindex -= offidxstart;
988 new_object->cache = m_next;
990 KASSERT(new_object->cache == NULL ||
991 new_object->type == OBJT_SWAP,
992 ("vm_page_cache_transfer: object %p's type is incompatible"
993 " with cached pages", new_object));
995 mtx_unlock(&vm_page_queue_free_mtx);
1001 * Allocate and return a memory cell associated
1002 * with this VM object/offset pair.
1005 * VM_ALLOC_NORMAL normal process request
1006 * VM_ALLOC_SYSTEM system *really* needs a page
1007 * VM_ALLOC_INTERRUPT interrupt time request
1008 * VM_ALLOC_ZERO zero page
1010 * This routine may not block.
1013 vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req)
1015 struct vnode *vp = NULL;
1016 vm_object_t m_object;
1018 int flags, page_req;
1020 page_req = req & VM_ALLOC_CLASS_MASK;
1021 KASSERT(curthread->td_intr_nesting_level == 0 ||
1022 page_req == VM_ALLOC_INTERRUPT,
1023 ("vm_page_alloc(NORMAL|SYSTEM) in interrupt context"));
1025 if ((req & VM_ALLOC_NOOBJ) == 0) {
1026 KASSERT(object != NULL,
1027 ("vm_page_alloc: NULL object."));
1028 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1032 * The pager is allowed to eat deeper into the free page list.
1034 if ((curproc == pageproc) && (page_req != VM_ALLOC_INTERRUPT)) {
1035 page_req = VM_ALLOC_SYSTEM;
1038 mtx_lock(&vm_page_queue_free_mtx);
1039 if (cnt.v_free_count + cnt.v_cache_count > cnt.v_free_reserved ||
1040 (page_req == VM_ALLOC_SYSTEM &&
1041 cnt.v_free_count + cnt.v_cache_count > cnt.v_interrupt_free_min) ||
1042 (page_req == VM_ALLOC_INTERRUPT &&
1043 cnt.v_free_count + cnt.v_cache_count > 0)) {
1045 * Allocate from the free queue if the number of free pages
1046 * exceeds the minimum for the request class.
1048 if (object != NULL &&
1049 (m = vm_page_cache_lookup(object, pindex)) != NULL) {
1050 if ((req & VM_ALLOC_IFNOTCACHED) != 0) {
1051 mtx_unlock(&vm_page_queue_free_mtx);
1054 if (vm_phys_unfree_page(m))
1055 vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m, 0);
1056 #if VM_NRESERVLEVEL > 0
1057 else if (!vm_reserv_reactivate_page(m))
1061 panic("vm_page_alloc: cache page %p is missing"
1062 " from the free queue", m);
1063 } else if ((req & VM_ALLOC_IFCACHED) != 0) {
1064 mtx_unlock(&vm_page_queue_free_mtx);
1066 #if VM_NRESERVLEVEL > 0
1067 } else if (object == NULL || object->type == OBJT_DEVICE ||
1068 (object->flags & OBJ_COLORED) == 0 ||
1069 (m = vm_reserv_alloc_page(object, pindex)) == NULL) {
1073 m = vm_phys_alloc_pages(object != NULL ?
1074 VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT, 0);
1075 #if VM_NRESERVLEVEL > 0
1076 if (m == NULL && vm_reserv_reclaim_inactive()) {
1077 m = vm_phys_alloc_pages(object != NULL ?
1078 VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT,
1085 * Not allocatable, give up.
1087 mtx_unlock(&vm_page_queue_free_mtx);
1088 atomic_add_int(&vm_pageout_deficit, 1);
1089 pagedaemon_wakeup();
1094 * At this point we had better have found a good page.
1099 ("vm_page_alloc(): missing page on free queue")
1101 if ((m->flags & PG_CACHED) != 0) {
1102 KASSERT(m->valid != 0,
1103 ("vm_page_alloc: cached page %p is invalid", m));
1104 if (m->object == object && m->pindex == pindex)
1105 cnt.v_reactivated++;
1108 m_object = m->object;
1109 vm_page_cache_remove(m);
1110 if (m_object->type == OBJT_VNODE && m_object->cache == NULL)
1111 vp = m_object->handle;
1113 KASSERT(VM_PAGE_IS_FREE(m),
1114 ("vm_page_alloc: page %p is not free", m));
1115 KASSERT(m->valid == 0,
1116 ("vm_page_alloc: free page %p is valid", m));
1121 * Initialize structure. Only the PG_ZERO flag is inherited.
1124 if (m->flags & PG_ZERO) {
1125 vm_page_zero_count--;
1126 if (req & VM_ALLOC_ZERO)
1129 if (object == NULL || object->type == OBJT_PHYS)
1130 flags |= PG_UNMANAGED;
1132 if (req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ))
1135 m->oflags = VPO_BUSY;
1136 if (req & VM_ALLOC_WIRED) {
1137 atomic_add_int(&cnt.v_wire_count, 1);
1144 KASSERT(m->dirty == 0, ("vm_page_alloc: free/cache page %p was dirty", m));
1145 mtx_unlock(&vm_page_queue_free_mtx);
1147 if ((req & VM_ALLOC_NOOBJ) == 0)
1148 vm_page_insert(m, object, pindex);
1153 * The following call to vdrop() must come after the above call
1154 * to vm_page_insert() in case both affect the same object and
1155 * vnode. Otherwise, the affected vnode's hold count could
1156 * temporarily become zero.
1162 * Don't wakeup too often - wakeup the pageout daemon when
1163 * we would be nearly out of memory.
1165 if (vm_paging_needed())
1166 pagedaemon_wakeup();
1172 * vm_wait: (also see VM_WAIT macro)
1174 * Block until free pages are available for allocation
1175 * - Called in various places before memory allocations.
1181 mtx_lock(&vm_page_queue_free_mtx);
1182 if (curproc == pageproc) {
1183 vm_pageout_pages_needed = 1;
1184 msleep(&vm_pageout_pages_needed, &vm_page_queue_free_mtx,
1185 PDROP | PSWP, "VMWait", 0);
1187 if (!vm_pages_needed) {
1188 vm_pages_needed = 1;
1189 wakeup(&vm_pages_needed);
1191 msleep(&cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PVM,
1197 * vm_waitpfault: (also see VM_WAITPFAULT macro)
1199 * Block until free pages are available for allocation
1200 * - Called only in vm_fault so that processes page faulting
1201 * can be easily tracked.
1202 * - Sleeps at a lower priority than vm_wait() so that vm_wait()ing
1203 * processes will be able to grab memory first. Do not change
1204 * this balance without careful testing first.
1210 mtx_lock(&vm_page_queue_free_mtx);
1211 if (!vm_pages_needed) {
1212 vm_pages_needed = 1;
1213 wakeup(&vm_pages_needed);
1215 msleep(&cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PUSER,
1222 * If the given page is contained within a page queue, move it to the tail
1225 * The page queues must be locked.
1228 vm_page_requeue(vm_page_t m)
1230 int queue = VM_PAGE_GETQUEUE(m);
1231 struct vpgqueues *vpq;
1233 if (queue != PQ_NONE) {
1234 vpq = &vm_page_queues[queue];
1235 TAILQ_REMOVE(&vpq->pl, m, pageq);
1236 TAILQ_INSERT_TAIL(&vpq->pl, m, pageq);
1243 * Remove a page from its queue.
1245 * The queue containing the given page must be locked.
1246 * This routine may not block.
1249 vm_pageq_remove(vm_page_t m)
1251 int queue = VM_PAGE_GETQUEUE(m);
1252 struct vpgqueues *pq;
1254 if (queue != PQ_NONE) {
1255 VM_PAGE_SETQUEUE2(m, PQ_NONE);
1256 pq = &vm_page_queues[queue];
1257 TAILQ_REMOVE(&pq->pl, m, pageq);
1265 * Add the given page to the specified queue.
1267 * The page queues must be locked.
1270 vm_page_enqueue(int queue, vm_page_t m)
1272 struct vpgqueues *vpq;
1274 vpq = &vm_page_queues[queue];
1275 VM_PAGE_SETQUEUE2(m, queue);
1276 TAILQ_INSERT_TAIL(&vpq->pl, m, pageq);
1283 * Put the specified page on the active list (if appropriate).
1284 * Ensure that act_count is at least ACT_INIT but do not otherwise
1287 * The page queues must be locked.
1288 * This routine may not block.
1291 vm_page_activate(vm_page_t m)
1294 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1295 if (VM_PAGE_GETKNOWNQUEUE2(m) != PQ_ACTIVE) {
1297 if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) {
1298 if (m->act_count < ACT_INIT)
1299 m->act_count = ACT_INIT;
1300 vm_page_enqueue(PQ_ACTIVE, m);
1303 if (m->act_count < ACT_INIT)
1304 m->act_count = ACT_INIT;
1309 * vm_page_free_wakeup:
1311 * Helper routine for vm_page_free_toq() and vm_page_cache(). This
1312 * routine is called when a page has been added to the cache or free
1315 * The page queues must be locked.
1316 * This routine may not block.
1319 vm_page_free_wakeup(void)
1322 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1324 * if pageout daemon needs pages, then tell it that there are
1327 if (vm_pageout_pages_needed &&
1328 cnt.v_cache_count + cnt.v_free_count >= cnt.v_pageout_free_min) {
1329 wakeup(&vm_pageout_pages_needed);
1330 vm_pageout_pages_needed = 0;
1333 * wakeup processes that are waiting on memory if we hit a
1334 * high water mark. And wakeup scheduler process if we have
1335 * lots of memory. this process will swapin processes.
1337 if (vm_pages_needed && !vm_page_count_min()) {
1338 vm_pages_needed = 0;
1339 wakeup(&cnt.v_free_count);
1346 * Returns the given page to the free list,
1347 * disassociating it with any VM object.
1349 * Object and page must be locked prior to entry.
1350 * This routine may not block.
1354 vm_page_free_toq(vm_page_t m)
1357 if (VM_PAGE_GETQUEUE(m) != PQ_NONE)
1358 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1359 KASSERT(!pmap_page_is_mapped(m),
1360 ("vm_page_free_toq: freeing mapped page %p", m));
1361 PCPU_INC(cnt.v_tfree);
1363 if (m->busy || VM_PAGE_IS_FREE(m)) {
1365 "vm_page_free: pindex(%lu), busy(%d), VPO_BUSY(%d), hold(%d)\n",
1366 (u_long)m->pindex, m->busy, (m->oflags & VPO_BUSY) ? 1 : 0,
1368 if (VM_PAGE_IS_FREE(m))
1369 panic("vm_page_free: freeing free page");
1371 panic("vm_page_free: freeing busy page");
1375 * unqueue, then remove page. Note that we cannot destroy
1376 * the page here because we do not want to call the pager's
1377 * callback routine until after we've put the page on the
1378 * appropriate free queue.
1384 * If fictitious remove object association and
1385 * return, otherwise delay object association removal.
1387 if ((m->flags & PG_FICTITIOUS) != 0) {
1394 if (m->wire_count != 0) {
1395 if (m->wire_count > 1) {
1396 panic("vm_page_free: invalid wire count (%d), pindex: 0x%lx",
1397 m->wire_count, (long)m->pindex);
1399 panic("vm_page_free: freeing wired page");
1401 if (m->hold_count != 0) {
1402 m->flags &= ~PG_ZERO;
1403 vm_page_enqueue(PQ_HOLD, m);
1405 mtx_lock(&vm_page_queue_free_mtx);
1406 m->flags |= PG_FREE;
1408 #if VM_NRESERVLEVEL > 0
1409 if (!vm_reserv_free_page(m))
1413 vm_phys_free_pages(m, 0);
1414 if ((m->flags & PG_ZERO) != 0)
1415 ++vm_page_zero_count;
1417 vm_page_zero_idle_wakeup();
1418 vm_page_free_wakeup();
1419 mtx_unlock(&vm_page_queue_free_mtx);
1426 * Mark this page as wired down by yet
1427 * another map, removing it from paging queues
1430 * The page queues must be locked.
1431 * This routine may not block.
1434 vm_page_wire(vm_page_t m)
1438 * Only bump the wire statistics if the page is not already wired,
1439 * and only unqueue the page if it is on some queue (if it is unmanaged
1440 * it is already off the queues).
1442 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1443 if (m->flags & PG_FICTITIOUS)
1445 if (m->wire_count == 0) {
1446 if ((m->flags & PG_UNMANAGED) == 0)
1448 atomic_add_int(&cnt.v_wire_count, 1);
1451 KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m));
1457 * Release one wiring of this page, potentially
1458 * enabling it to be paged again.
1460 * Many pages placed on the inactive queue should actually go
1461 * into the cache, but it is difficult to figure out which. What
1462 * we do instead, if the inactive target is well met, is to put
1463 * clean pages at the head of the inactive queue instead of the tail.
1464 * This will cause them to be moved to the cache more quickly and
1465 * if not actively re-referenced, freed more quickly. If we just
1466 * stick these pages at the end of the inactive queue, heavy filesystem
1467 * meta-data accesses can cause an unnecessary paging load on memory bound
1468 * processes. This optimization causes one-time-use metadata to be
1469 * reused more quickly.
1471 * BUT, if we are in a low-memory situation we have no choice but to
1472 * put clean pages on the cache queue.
1474 * A number of routines use vm_page_unwire() to guarantee that the page
1475 * will go into either the inactive or active queues, and will NEVER
1476 * be placed in the cache - for example, just after dirtying a page.
1477 * dirty pages in the cache are not allowed.
1479 * The page queues must be locked.
1480 * This routine may not block.
1483 vm_page_unwire(vm_page_t m, int activate)
1486 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1487 if (m->flags & PG_FICTITIOUS)
1489 if (m->wire_count > 0) {
1491 if (m->wire_count == 0) {
1492 atomic_subtract_int(&cnt.v_wire_count, 1);
1493 if (m->flags & PG_UNMANAGED) {
1495 } else if (activate)
1496 vm_page_enqueue(PQ_ACTIVE, m);
1498 vm_page_flag_clear(m, PG_WINATCFLS);
1499 vm_page_enqueue(PQ_INACTIVE, m);
1503 panic("vm_page_unwire: invalid wire count: %d", m->wire_count);
1509 * Move the specified page to the inactive queue. If the page has
1510 * any associated swap, the swap is deallocated.
1512 * Normally athead is 0 resulting in LRU operation. athead is set
1513 * to 1 if we want this page to be 'as if it were placed in the cache',
1514 * except without unmapping it from the process address space.
1516 * This routine may not block.
1519 _vm_page_deactivate(vm_page_t m, int athead)
1522 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1525 * Ignore if already inactive.
1527 if (VM_PAGE_INQUEUE2(m, PQ_INACTIVE))
1529 if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) {
1530 vm_page_flag_clear(m, PG_WINATCFLS);
1533 TAILQ_INSERT_HEAD(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
1535 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
1536 VM_PAGE_SETQUEUE2(m, PQ_INACTIVE);
1537 cnt.v_inactive_count++;
1542 vm_page_deactivate(vm_page_t m)
1544 _vm_page_deactivate(m, 0);
1548 * vm_page_try_to_cache:
1550 * Returns 0 on failure, 1 on success
1553 vm_page_try_to_cache(vm_page_t m)
1556 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1557 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1558 if (m->dirty || m->hold_count || m->busy || m->wire_count ||
1559 (m->oflags & VPO_BUSY) || (m->flags & PG_UNMANAGED)) {
1570 * vm_page_try_to_free()
1572 * Attempt to free the page. If we cannot free it, we do nothing.
1573 * 1 is returned on success, 0 on failure.
1576 vm_page_try_to_free(vm_page_t m)
1579 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1580 if (m->object != NULL)
1581 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1582 if (m->dirty || m->hold_count || m->busy || m->wire_count ||
1583 (m->oflags & VPO_BUSY) || (m->flags & PG_UNMANAGED)) {
1596 * Put the specified page onto the page cache queue (if appropriate).
1598 * This routine may not block.
1601 vm_page_cache(vm_page_t m)
1606 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1608 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1609 if ((m->flags & PG_UNMANAGED) || (m->oflags & VPO_BUSY) || m->busy ||
1610 m->hold_count || m->wire_count) {
1611 panic("vm_page_cache: attempting to cache busy page");
1615 panic("vm_page_cache: page %p is dirty", m);
1616 if (m->valid == 0 || object->type == OBJT_DEFAULT ||
1617 (object->type == OBJT_SWAP &&
1618 !vm_pager_has_page(object, m->pindex, NULL, NULL))) {
1620 * Hypothesis: A cache-elgible page belonging to a
1621 * default object or swap object but without a backing
1622 * store must be zero filled.
1627 KASSERT((m->flags & PG_CACHED) == 0,
1628 ("vm_page_cache: page %p is already cached", m));
1632 * Remove the page from the paging queues.
1637 * Remove the page from the object's collection of resident
1640 if (m != object->root)
1641 vm_page_splay(m->pindex, object->root);
1642 if (m->left == NULL)
1645 root = vm_page_splay(m->pindex, m->left);
1646 root->right = m->right;
1648 object->root = root;
1649 TAILQ_REMOVE(&object->memq, m, listq);
1650 object->resident_page_count--;
1651 object->generation++;
1654 * Insert the page into the object's collection of cached pages
1655 * and the physical memory allocator's cache/free page queues.
1657 vm_page_flag_clear(m, PG_ZERO);
1658 mtx_lock(&vm_page_queue_free_mtx);
1659 m->flags |= PG_CACHED;
1660 cnt.v_cache_count++;
1661 root = object->cache;
1666 root = vm_page_splay(m->pindex, root);
1667 if (m->pindex < root->pindex) {
1668 m->left = root->left;
1671 } else if (__predict_false(m->pindex == root->pindex))
1672 panic("vm_page_cache: offset already cached");
1674 m->right = root->right;
1680 #if VM_NRESERVLEVEL > 0
1681 if (!vm_reserv_free_page(m)) {
1685 vm_phys_set_pool(VM_FREEPOOL_CACHE, m, 0);
1686 vm_phys_free_pages(m, 0);
1688 vm_page_free_wakeup();
1689 mtx_unlock(&vm_page_queue_free_mtx);
1692 * Increment the vnode's hold count if this is the object's only
1693 * cached page. Decrement the vnode's hold count if this was
1694 * the object's only resident page.
1696 if (object->type == OBJT_VNODE) {
1697 if (root == NULL && object->resident_page_count != 0)
1698 vhold(object->handle);
1699 else if (root != NULL && object->resident_page_count == 0)
1700 vdrop(object->handle);
1707 * Cache, deactivate, or do nothing as appropriate. This routine
1708 * is typically used by madvise() MADV_DONTNEED.
1710 * Generally speaking we want to move the page into the cache so
1711 * it gets reused quickly. However, this can result in a silly syndrome
1712 * due to the page recycling too quickly. Small objects will not be
1713 * fully cached. On the otherhand, if we move the page to the inactive
1714 * queue we wind up with a problem whereby very large objects
1715 * unnecessarily blow away our inactive and cache queues.
1717 * The solution is to move the pages based on a fixed weighting. We
1718 * either leave them alone, deactivate them, or move them to the cache,
1719 * where moving them to the cache has the highest weighting.
1720 * By forcing some pages into other queues we eventually force the
1721 * system to balance the queues, potentially recovering other unrelated
1722 * space from active. The idea is to not force this to happen too
1726 vm_page_dontneed(vm_page_t m)
1728 static int dnweight;
1732 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1736 * occassionally leave the page alone
1738 if ((dnw & 0x01F0) == 0 ||
1739 VM_PAGE_INQUEUE2(m, PQ_INACTIVE)) {
1740 if (m->act_count >= ACT_INIT)
1746 * Clear any references to the page. Otherwise, the page daemon will
1747 * immediately reactivate the page.
1749 vm_page_flag_clear(m, PG_REFERENCED);
1750 pmap_clear_reference(m);
1752 if (m->dirty == 0 && pmap_is_modified(m))
1755 if (m->dirty || (dnw & 0x0070) == 0) {
1757 * Deactivate the page 3 times out of 32.
1762 * Cache the page 28 times out of every 32. Note that
1763 * the page is deactivated instead of cached, but placed
1764 * at the head of the queue instead of the tail.
1768 _vm_page_deactivate(m, head);
1772 * Grab a page, waiting until we are waken up due to the page
1773 * changing state. We keep on waiting, if the page continues
1774 * to be in the object. If the page doesn't exist, first allocate it
1775 * and then conditionally zero it.
1777 * This routine may block.
1780 vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags)
1784 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1786 if ((m = vm_page_lookup(object, pindex)) != NULL) {
1787 if (vm_page_sleep_if_busy(m, TRUE, "pgrbwt")) {
1788 if ((allocflags & VM_ALLOC_RETRY) == 0)
1792 if ((allocflags & VM_ALLOC_WIRED) != 0) {
1793 vm_page_lock_queues();
1795 vm_page_unlock_queues();
1797 if ((allocflags & VM_ALLOC_NOBUSY) == 0)
1802 m = vm_page_alloc(object, pindex, allocflags & ~VM_ALLOC_RETRY);
1804 VM_OBJECT_UNLOCK(object);
1806 VM_OBJECT_LOCK(object);
1807 if ((allocflags & VM_ALLOC_RETRY) == 0)
1810 } else if (m->valid != 0)
1812 if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0)
1818 * Mapping function for valid bits or for dirty bits in
1819 * a page. May not block.
1821 * Inputs are required to range within a page.
1824 vm_page_bits(int base, int size)
1830 base + size <= PAGE_SIZE,
1831 ("vm_page_bits: illegal base/size %d/%d", base, size)
1834 if (size == 0) /* handle degenerate case */
1837 first_bit = base >> DEV_BSHIFT;
1838 last_bit = (base + size - 1) >> DEV_BSHIFT;
1840 return ((2 << last_bit) - (1 << first_bit));
1844 * vm_page_set_validclean:
1846 * Sets portions of a page valid and clean. The arguments are expected
1847 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
1848 * of any partial chunks touched by the range. The invalid portion of
1849 * such chunks will be zero'd.
1851 * This routine may not block.
1853 * (base + size) must be less then or equal to PAGE_SIZE.
1856 vm_page_set_validclean(vm_page_t m, int base, int size)
1862 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1863 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1864 if (size == 0) /* handle degenerate case */
1868 * If the base is not DEV_BSIZE aligned and the valid
1869 * bit is clear, we have to zero out a portion of the
1872 if ((frag = base & ~(DEV_BSIZE - 1)) != base &&
1873 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0)
1874 pmap_zero_page_area(m, frag, base - frag);
1877 * If the ending offset is not DEV_BSIZE aligned and the
1878 * valid bit is clear, we have to zero out a portion of
1881 endoff = base + size;
1882 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff &&
1883 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0)
1884 pmap_zero_page_area(m, endoff,
1885 DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
1888 * Set valid, clear dirty bits. If validating the entire
1889 * page we can safely clear the pmap modify bit. We also
1890 * use this opportunity to clear the VPO_NOSYNC flag. If a process
1891 * takes a write fault on a MAP_NOSYNC memory area the flag will
1894 * We set valid bits inclusive of any overlap, but we can only
1895 * clear dirty bits for DEV_BSIZE chunks that are fully within
1898 pagebits = vm_page_bits(base, size);
1899 m->valid |= pagebits;
1901 if ((frag = base & (DEV_BSIZE - 1)) != 0) {
1902 frag = DEV_BSIZE - frag;
1908 pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1));
1910 m->dirty &= ~pagebits;
1911 if (base == 0 && size == PAGE_SIZE) {
1912 pmap_clear_modify(m);
1913 m->oflags &= ~VPO_NOSYNC;
1918 vm_page_clear_dirty(vm_page_t m, int base, int size)
1921 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1922 m->dirty &= ~vm_page_bits(base, size);
1926 * vm_page_set_invalid:
1928 * Invalidates DEV_BSIZE'd chunks within a page. Both the
1929 * valid and dirty bits for the effected areas are cleared.
1934 vm_page_set_invalid(vm_page_t m, int base, int size)
1938 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1939 bits = vm_page_bits(base, size);
1940 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1941 if (m->valid == VM_PAGE_BITS_ALL && bits != 0)
1945 m->object->generation++;
1949 * vm_page_zero_invalid()
1951 * The kernel assumes that the invalid portions of a page contain
1952 * garbage, but such pages can be mapped into memory by user code.
1953 * When this occurs, we must zero out the non-valid portions of the
1954 * page so user code sees what it expects.
1956 * Pages are most often semi-valid when the end of a file is mapped
1957 * into memory and the file's size is not page aligned.
1960 vm_page_zero_invalid(vm_page_t m, boolean_t setvalid)
1965 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1967 * Scan the valid bits looking for invalid sections that
1968 * must be zerod. Invalid sub-DEV_BSIZE'd areas ( where the
1969 * valid bit may be set ) have already been zerod by
1970 * vm_page_set_validclean().
1972 for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) {
1973 if (i == (PAGE_SIZE / DEV_BSIZE) ||
1974 (m->valid & (1 << i))
1977 pmap_zero_page_area(m,
1978 b << DEV_BSHIFT, (i - b) << DEV_BSHIFT);
1985 * setvalid is TRUE when we can safely set the zero'd areas
1986 * as being valid. We can do this if there are no cache consistancy
1987 * issues. e.g. it is ok to do with UFS, but not ok to do with NFS.
1990 m->valid = VM_PAGE_BITS_ALL;
1996 * Is (partial) page valid? Note that the case where size == 0
1997 * will return FALSE in the degenerate case where the page is
1998 * entirely invalid, and TRUE otherwise.
2003 vm_page_is_valid(vm_page_t m, int base, int size)
2005 int bits = vm_page_bits(base, size);
2007 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2008 if (m->valid && ((m->valid & bits) == bits))
2015 * update dirty bits from pmap/mmu. May not block.
2018 vm_page_test_dirty(vm_page_t m)
2020 if ((m->dirty != VM_PAGE_BITS_ALL) && pmap_is_modified(m)) {
2025 int so_zerocp_fullpage = 0;
2028 * Replace the given page with a copy. The copied page assumes
2029 * the portion of the given page's "wire_count" that is not the
2030 * responsibility of this copy-on-write mechanism.
2032 * The object containing the given page must have a non-zero
2033 * paging-in-progress count and be locked.
2036 vm_page_cowfault(vm_page_t m)
2043 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2044 KASSERT(object->paging_in_progress != 0,
2045 ("vm_page_cowfault: object %p's paging-in-progress count is zero.",
2052 mnew = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL | VM_ALLOC_NOBUSY);
2054 vm_page_insert(m, object, pindex);
2055 vm_page_unlock_queues();
2056 VM_OBJECT_UNLOCK(object);
2058 VM_OBJECT_LOCK(object);
2059 if (m == vm_page_lookup(object, pindex)) {
2060 vm_page_lock_queues();
2064 * Page disappeared during the wait.
2066 vm_page_lock_queues();
2073 * check to see if we raced with an xmit complete when
2074 * waiting to allocate a page. If so, put things back
2078 vm_page_insert(m, object, pindex);
2079 } else { /* clear COW & copy page */
2080 if (!so_zerocp_fullpage)
2081 pmap_copy_page(m, mnew);
2082 mnew->valid = VM_PAGE_BITS_ALL;
2083 vm_page_dirty(mnew);
2084 mnew->wire_count = m->wire_count - m->cow;
2085 m->wire_count = m->cow;
2090 vm_page_cowclear(vm_page_t m)
2093 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2097 * let vm_fault add back write permission lazily
2101 * sf_buf_free() will free the page, so we needn't do it here
2106 vm_page_cowsetup(vm_page_t m)
2109 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2110 if (m->cow == USHRT_MAX - 1)
2113 pmap_remove_write(m);
2117 #include "opt_ddb.h"
2119 #include <sys/kernel.h>
2121 #include <ddb/ddb.h>
2123 DB_SHOW_COMMAND(page, vm_page_print_page_info)
2125 db_printf("cnt.v_free_count: %d\n", cnt.v_free_count);
2126 db_printf("cnt.v_cache_count: %d\n", cnt.v_cache_count);
2127 db_printf("cnt.v_inactive_count: %d\n", cnt.v_inactive_count);
2128 db_printf("cnt.v_active_count: %d\n", cnt.v_active_count);
2129 db_printf("cnt.v_wire_count: %d\n", cnt.v_wire_count);
2130 db_printf("cnt.v_free_reserved: %d\n", cnt.v_free_reserved);
2131 db_printf("cnt.v_free_min: %d\n", cnt.v_free_min);
2132 db_printf("cnt.v_free_target: %d\n", cnt.v_free_target);
2133 db_printf("cnt.v_cache_min: %d\n", cnt.v_cache_min);
2134 db_printf("cnt.v_inactive_target: %d\n", cnt.v_inactive_target);
2137 DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info)
2140 db_printf("PQ_FREE:");
2141 db_printf(" %d", cnt.v_free_count);
2144 db_printf("PQ_CACHE:");
2145 db_printf(" %d", cnt.v_cache_count);
2148 db_printf("PQ_ACTIVE: %d, PQ_INACTIVE: %d\n",
2149 *vm_page_queues[PQ_ACTIVE].cnt,
2150 *vm_page_queues[PQ_INACTIVE].cnt);