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 * - The object mutex is held when inserting or removing
71 * pages from an object (vm_page_insert() or vm_page_remove()).
76 * Resident memory management module.
79 #include <sys/cdefs.h>
80 __FBSDID("$FreeBSD$");
84 #include <sys/param.h>
85 #include <sys/systm.h>
87 #include <sys/kernel.h>
88 #include <sys/limits.h>
89 #include <sys/malloc.h>
90 #include <sys/msgbuf.h>
91 #include <sys/mutex.h>
93 #include <sys/sysctl.h>
94 #include <sys/vmmeter.h>
95 #include <sys/vnode.h>
99 #include <vm/vm_param.h>
100 #include <vm/vm_kern.h>
101 #include <vm/vm_object.h>
102 #include <vm/vm_page.h>
103 #include <vm/vm_pageout.h>
104 #include <vm/vm_pager.h>
105 #include <vm/vm_phys.h>
106 #include <vm/vm_reserv.h>
107 #include <vm/vm_extern.h>
109 #include <vm/uma_int.h>
111 #include <machine/md_var.h>
114 * Associated with page of user-allocatable memory is a
118 struct vpgqueues vm_page_queues[PQ_COUNT];
119 struct vpglocks vm_page_queue_lock;
120 struct vpglocks vm_page_queue_free_lock;
122 struct vpglocks pa_lock[PA_LOCK_COUNT];
124 vm_page_t vm_page_array = 0;
125 int vm_page_array_size = 0;
127 int vm_page_zero_count = 0;
129 static int boot_pages = UMA_BOOT_PAGES;
130 TUNABLE_INT("vm.boot_pages", &boot_pages);
131 SYSCTL_INT(_vm, OID_AUTO, boot_pages, CTLFLAG_RD, &boot_pages, 0,
132 "number of pages allocated for bootstrapping the VM system");
134 static int pa_tryrelock_restart;
135 SYSCTL_INT(_vm, OID_AUTO, tryrelock_restart, CTLFLAG_RD,
136 &pa_tryrelock_restart, 0, "Number of tryrelock restarts");
138 static uma_zone_t fakepg_zone;
140 static void vm_page_clear_dirty_mask(vm_page_t m, int pagebits);
141 static void vm_page_queue_remove(int queue, vm_page_t m);
142 static void vm_page_enqueue(int queue, vm_page_t m);
143 static void vm_page_init_fakepg(void *dummy);
145 SYSINIT(vm_page, SI_SUB_VM, SI_ORDER_SECOND, vm_page_init_fakepg, NULL);
148 vm_page_init_fakepg(void *dummy)
151 fakepg_zone = uma_zcreate("fakepg", sizeof(struct vm_page), NULL, NULL,
152 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE | UMA_ZONE_VM);
155 /* Make sure that u_long is at least 64 bits when PAGE_SIZE is 32K. */
156 #if PAGE_SIZE == 32768
158 CTASSERT(sizeof(u_long) >= 8);
163 * Try to acquire a physical address lock while a pmap is locked. If we
164 * fail to trylock we unlock and lock the pmap directly and cache the
165 * locked pa in *locked. The caller should then restart their loop in case
166 * the virtual to physical mapping has changed.
169 vm_page_pa_tryrelock(pmap_t pmap, vm_paddr_t pa, vm_paddr_t *locked)
176 PA_LOCK_ASSERT(lockpa, MA_OWNED);
177 if (PA_LOCKPTR(pa) == PA_LOCKPTR(lockpa))
184 atomic_add_int(&pa_tryrelock_restart, 1);
193 * Sets the page size, perhaps based upon the memory
194 * size. Must be called before any use of page-size
195 * dependent functions.
198 vm_set_page_size(void)
200 if (cnt.v_page_size == 0)
201 cnt.v_page_size = PAGE_SIZE;
202 if (((cnt.v_page_size - 1) & cnt.v_page_size) != 0)
203 panic("vm_set_page_size: page size not a power of two");
207 * vm_page_blacklist_lookup:
209 * See if a physical address in this page has been listed
210 * in the blacklist tunable. Entries in the tunable are
211 * separated by spaces or commas. If an invalid integer is
212 * encountered then the rest of the string is skipped.
215 vm_page_blacklist_lookup(char *list, vm_paddr_t pa)
220 for (pos = list; *pos != '\0'; pos = cp) {
221 bad = strtoq(pos, &cp, 0);
223 if (*cp == ' ' || *cp == ',') {
230 if (pa == trunc_page(bad))
239 * Initializes the resident memory module.
241 * Allocates memory for the page cells, and
242 * for the object/offset-to-page hash table headers.
243 * Each page cell is initialized and placed on the free list.
246 vm_page_startup(vm_offset_t vaddr)
249 vm_paddr_t page_range;
256 /* the biggest memory array is the second group of pages */
258 vm_paddr_t biggestsize;
259 vm_paddr_t low_water, high_water;
264 vaddr = round_page(vaddr);
266 for (i = 0; phys_avail[i + 1]; i += 2) {
267 phys_avail[i] = round_page(phys_avail[i]);
268 phys_avail[i + 1] = trunc_page(phys_avail[i + 1]);
271 low_water = phys_avail[0];
272 high_water = phys_avail[1];
274 for (i = 0; phys_avail[i + 1]; i += 2) {
275 vm_paddr_t size = phys_avail[i + 1] - phys_avail[i];
277 if (size > biggestsize) {
281 if (phys_avail[i] < low_water)
282 low_water = phys_avail[i];
283 if (phys_avail[i + 1] > high_water)
284 high_water = phys_avail[i + 1];
291 end = phys_avail[biggestone+1];
294 * Initialize the locks.
296 mtx_init(&vm_page_queue_mtx, "vm page queue mutex", NULL, MTX_DEF |
298 mtx_init(&vm_page_queue_free_mtx, "vm page queue free mutex", NULL,
301 /* Setup page locks. */
302 for (i = 0; i < PA_LOCK_COUNT; i++)
303 mtx_init(&pa_lock[i].data, "page lock", NULL, MTX_DEF);
306 * Initialize the queue headers for the hold queue, the active queue,
307 * and the inactive queue.
309 for (i = 0; i < PQ_COUNT; i++)
310 TAILQ_INIT(&vm_page_queues[i].pl);
311 vm_page_queues[PQ_INACTIVE].cnt = &cnt.v_inactive_count;
312 vm_page_queues[PQ_ACTIVE].cnt = &cnt.v_active_count;
313 vm_page_queues[PQ_HOLD].cnt = &cnt.v_active_count;
316 * Allocate memory for use when boot strapping the kernel memory
319 new_end = end - (boot_pages * UMA_SLAB_SIZE);
320 new_end = trunc_page(new_end);
321 mapped = pmap_map(&vaddr, new_end, end,
322 VM_PROT_READ | VM_PROT_WRITE);
323 bzero((void *)mapped, end - new_end);
324 uma_startup((void *)mapped, boot_pages);
326 #if defined(__amd64__) || defined(__i386__) || defined(__arm__) || \
329 * Allocate a bitmap to indicate that a random physical page
330 * needs to be included in a minidump.
332 * The amd64 port needs this to indicate which direct map pages
333 * need to be dumped, via calls to dump_add_page()/dump_drop_page().
335 * However, i386 still needs this workspace internally within the
336 * minidump code. In theory, they are not needed on i386, but are
337 * included should the sf_buf code decide to use them.
340 for (i = 0; dump_avail[i + 1] != 0; i += 2)
341 if (dump_avail[i + 1] > last_pa)
342 last_pa = dump_avail[i + 1];
343 page_range = last_pa / PAGE_SIZE;
344 vm_page_dump_size = round_page(roundup2(page_range, NBBY) / NBBY);
345 new_end -= vm_page_dump_size;
346 vm_page_dump = (void *)(uintptr_t)pmap_map(&vaddr, new_end,
347 new_end + vm_page_dump_size, VM_PROT_READ | VM_PROT_WRITE);
348 bzero((void *)vm_page_dump, vm_page_dump_size);
352 * Request that the physical pages underlying the message buffer be
353 * included in a crash dump. Since the message buffer is accessed
354 * through the direct map, they are not automatically included.
356 pa = DMAP_TO_PHYS((vm_offset_t)msgbufp->msg_ptr);
357 last_pa = pa + round_page(msgbufsize);
358 while (pa < last_pa) {
364 * Compute the number of pages of memory that will be available for
365 * use (taking into account the overhead of a page structure per
368 first_page = low_water / PAGE_SIZE;
369 #ifdef VM_PHYSSEG_SPARSE
371 for (i = 0; phys_avail[i + 1] != 0; i += 2)
372 page_range += atop(phys_avail[i + 1] - phys_avail[i]);
373 #elif defined(VM_PHYSSEG_DENSE)
374 page_range = high_water / PAGE_SIZE - first_page;
376 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
381 * Reserve an unmapped guard page to trap access to vm_page_array[-1].
386 * Initialize the mem entry structures now, and put them in the free
389 new_end = trunc_page(end - page_range * sizeof(struct vm_page));
390 mapped = pmap_map(&vaddr, new_end, end,
391 VM_PROT_READ | VM_PROT_WRITE);
392 vm_page_array = (vm_page_t) mapped;
393 #if VM_NRESERVLEVEL > 0
395 * Allocate memory for the reservation management system's data
398 new_end = vm_reserv_startup(&vaddr, new_end, high_water);
400 #if defined(__amd64__) || defined(__mips__)
402 * pmap_map on amd64 and mips can come out of the direct-map, not kvm
403 * like i386, so the pages must be tracked for a crashdump to include
404 * this data. This includes the vm_page_array and the early UMA
407 for (pa = new_end; pa < phys_avail[biggestone + 1]; pa += PAGE_SIZE)
410 phys_avail[biggestone + 1] = new_end;
413 * Clear all of the page structures
415 bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page));
416 for (i = 0; i < page_range; i++)
417 vm_page_array[i].order = VM_NFREEORDER;
418 vm_page_array_size = page_range;
421 * Initialize the physical memory allocator.
426 * Add every available physical page that is not blacklisted to
429 cnt.v_page_count = 0;
430 cnt.v_free_count = 0;
431 list = getenv("vm.blacklist");
432 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
434 last_pa = phys_avail[i + 1];
435 while (pa < last_pa) {
437 vm_page_blacklist_lookup(list, pa))
438 printf("Skipping page with pa 0x%jx\n",
441 vm_phys_add_page(pa);
446 #if VM_NRESERVLEVEL > 0
448 * Initialize the reservation management system.
456 CTASSERT(offsetof(struct vm_page, aflags) % sizeof(uint32_t) == 0);
459 vm_page_aflag_set(vm_page_t m, uint8_t bits)
464 * The PGA_WRITEABLE flag can only be set if the page is managed and
465 * VPO_BUSY. Currently, this flag is only set by pmap_enter().
467 KASSERT((bits & PGA_WRITEABLE) == 0 ||
468 (m->oflags & (VPO_UNMANAGED | VPO_BUSY)) == VPO_BUSY,
469 ("PGA_WRITEABLE and !VPO_BUSY"));
472 * We want to use atomic updates for m->aflags, which is a
473 * byte wide. Not all architectures provide atomic operations
474 * on the single-byte destination. Punt and access the whole
475 * 4-byte word with an atomic update. Parallel non-atomic
476 * updates to the fields included in the update by proximity
477 * are handled properly by atomics.
479 addr = (void *)&m->aflags;
480 MPASS(((uintptr_t)addr & (sizeof(uint32_t) - 1)) == 0);
482 #if BYTE_ORDER == BIG_ENDIAN
485 atomic_set_32(addr, val);
489 vm_page_aflag_clear(vm_page_t m, uint8_t bits)
494 * The PGA_REFERENCED flag can only be cleared if the object
495 * containing the page is locked.
497 KASSERT((bits & PGA_REFERENCED) == 0 || VM_OBJECT_LOCKED(m->object),
498 ("PGA_REFERENCED and !VM_OBJECT_LOCKED"));
501 * See the comment in vm_page_aflag_set().
503 addr = (void *)&m->aflags;
504 MPASS(((uintptr_t)addr & (sizeof(uint32_t) - 1)) == 0);
506 #if BYTE_ORDER == BIG_ENDIAN
509 atomic_clear_32(addr, val);
513 vm_page_reference(vm_page_t m)
516 vm_page_aflag_set(m, PGA_REFERENCED);
520 vm_page_busy(vm_page_t m)
523 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
524 KASSERT((m->oflags & VPO_BUSY) == 0,
525 ("vm_page_busy: page already busy!!!"));
526 m->oflags |= VPO_BUSY;
532 * wakeup anyone waiting for the page.
535 vm_page_flash(vm_page_t m)
538 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
539 if (m->oflags & VPO_WANTED) {
540 m->oflags &= ~VPO_WANTED;
548 * clear the VPO_BUSY flag and wakeup anyone waiting for the
553 vm_page_wakeup(vm_page_t m)
556 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
557 KASSERT(m->oflags & VPO_BUSY, ("vm_page_wakeup: page not busy!!!"));
558 m->oflags &= ~VPO_BUSY;
563 vm_page_io_start(vm_page_t m)
566 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
571 vm_page_io_finish(vm_page_t m)
574 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
575 KASSERT(m->busy > 0, ("vm_page_io_finish: page %p is not busy", m));
582 * Keep page from being freed by the page daemon
583 * much of the same effect as wiring, except much lower
584 * overhead and should be used only for *very* temporary
585 * holding ("wiring").
588 vm_page_hold(vm_page_t mem)
591 vm_page_lock_assert(mem, MA_OWNED);
596 vm_page_unhold(vm_page_t mem)
599 vm_page_lock_assert(mem, MA_OWNED);
601 KASSERT(mem->hold_count >= 0, ("vm_page_unhold: hold count < 0!!!"));
602 if (mem->hold_count == 0 && mem->queue == PQ_HOLD)
603 vm_page_free_toq(mem);
607 * vm_page_unhold_pages:
609 * Unhold each of the pages that is referenced by the given array.
612 vm_page_unhold_pages(vm_page_t *ma, int count)
614 struct mtx *mtx, *new_mtx;
617 for (; count != 0; count--) {
619 * Avoid releasing and reacquiring the same page lock.
621 new_mtx = vm_page_lockptr(*ma);
622 if (mtx != new_mtx) {
638 * Create a fictitious page with the specified physical address and
639 * memory attribute. The memory attribute is the only the machine-
640 * dependent aspect of a fictitious page that must be initialized.
643 vm_page_getfake(vm_paddr_t paddr, vm_memattr_t memattr)
647 m = uma_zalloc(fakepg_zone, M_WAITOK | M_ZERO);
648 m->phys_addr = paddr;
650 /* Fictitious pages don't use "segind". */
651 m->flags = PG_FICTITIOUS;
652 /* Fictitious pages don't use "order" or "pool". */
653 m->oflags = VPO_BUSY | VPO_UNMANAGED;
655 pmap_page_set_memattr(m, memattr);
662 * Release a fictitious page.
665 vm_page_putfake(vm_page_t m)
668 KASSERT((m->flags & PG_FICTITIOUS) != 0,
669 ("vm_page_putfake: bad page %p", m));
670 uma_zfree(fakepg_zone, m);
674 * vm_page_updatefake:
676 * Update the given fictitious page to the specified physical address and
680 vm_page_updatefake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
683 KASSERT((m->flags & PG_FICTITIOUS) != 0,
684 ("vm_page_updatefake: bad page %p", m));
685 m->phys_addr = paddr;
686 pmap_page_set_memattr(m, memattr);
695 vm_page_free(vm_page_t m)
698 m->flags &= ~PG_ZERO;
705 * Free a page to the zerod-pages queue
708 vm_page_free_zero(vm_page_t m)
718 * Sleep and release the page and page queues locks.
720 * The object containing the given page must be locked.
723 vm_page_sleep(vm_page_t m, const char *msg)
726 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
727 if (mtx_owned(&vm_page_queue_mtx))
728 vm_page_unlock_queues();
729 if (mtx_owned(vm_page_lockptr(m)))
733 * It's possible that while we sleep, the page will get
734 * unbusied and freed. If we are holding the object
735 * lock, we will assume we hold a reference to the object
736 * such that even if m->object changes, we can re-lock
739 m->oflags |= VPO_WANTED;
740 msleep(m, VM_OBJECT_MTX(m->object), PVM, msg, 0);
746 * Set all bits in the page's dirty field.
748 * The object containing the specified page must be locked if the
749 * call is made from the machine-independent layer.
751 * See vm_page_clear_dirty_mask().
754 vm_page_dirty(vm_page_t m)
757 KASSERT((m->flags & PG_CACHED) == 0,
758 ("vm_page_dirty: page in cache!"));
759 KASSERT(!VM_PAGE_IS_FREE(m),
760 ("vm_page_dirty: page is free!"));
761 KASSERT(m->valid == VM_PAGE_BITS_ALL,
762 ("vm_page_dirty: page is invalid!"));
763 m->dirty = VM_PAGE_BITS_ALL;
769 * Implements Sleator and Tarjan's top-down splay algorithm. Returns
770 * the vm_page containing the given pindex. If, however, that
771 * pindex is not found in the vm_object, returns a vm_page that is
772 * adjacent to the pindex, coming before or after it.
775 vm_page_splay(vm_pindex_t pindex, vm_page_t root)
777 struct vm_page dummy;
778 vm_page_t lefttreemax, righttreemin, y;
782 lefttreemax = righttreemin = &dummy;
784 if (pindex < root->pindex) {
785 if ((y = root->left) == NULL)
787 if (pindex < y->pindex) {
789 root->left = y->right;
792 if ((y = root->left) == NULL)
795 /* Link into the new root's right tree. */
796 righttreemin->left = root;
798 } else if (pindex > root->pindex) {
799 if ((y = root->right) == NULL)
801 if (pindex > y->pindex) {
803 root->right = y->left;
806 if ((y = root->right) == NULL)
809 /* Link into the new root's left tree. */
810 lefttreemax->right = root;
815 /* Assemble the new root. */
816 lefttreemax->right = root->left;
817 righttreemin->left = root->right;
818 root->left = dummy.right;
819 root->right = dummy.left;
824 * vm_page_insert: [ internal use only ]
826 * Inserts the given mem entry into the object and object list.
828 * The pagetables are not updated but will presumably fault the page
829 * in if necessary, or if a kernel page the caller will at some point
830 * enter the page into the kernel's pmap. We are not allowed to block
831 * here so we *can't* do this anyway.
833 * The object and page must be locked.
834 * This routine may not block.
837 vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex)
841 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
842 if (m->object != NULL)
843 panic("vm_page_insert: page already inserted");
846 * Record the object/offset pair in this page
852 * Now link into the object's ordered list of backed pages.
858 TAILQ_INSERT_TAIL(&object->memq, m, listq);
860 root = vm_page_splay(pindex, root);
861 if (pindex < root->pindex) {
862 m->left = root->left;
865 TAILQ_INSERT_BEFORE(root, m, listq);
866 } else if (pindex == root->pindex)
867 panic("vm_page_insert: offset already allocated");
869 m->right = root->right;
872 TAILQ_INSERT_AFTER(&object->memq, root, m, listq);
878 * show that the object has one more resident page.
880 object->resident_page_count++;
882 * Hold the vnode until the last page is released.
884 if (object->resident_page_count == 1 && object->type == OBJT_VNODE)
885 vhold((struct vnode *)object->handle);
888 * Since we are inserting a new and possibly dirty page,
889 * update the object's OBJ_MIGHTBEDIRTY flag.
891 if (m->aflags & PGA_WRITEABLE)
892 vm_object_set_writeable_dirty(object);
897 * NOTE: used by device pager as well -wfj
899 * Removes the given mem entry from the object/offset-page
900 * table and the object page list, but do not invalidate/terminate
903 * The object and page must be locked.
904 * The underlying pmap entry (if any) is NOT removed here.
905 * This routine may not block.
908 vm_page_remove(vm_page_t m)
911 vm_page_t next, prev, root;
913 if ((m->oflags & VPO_UNMANAGED) == 0)
914 vm_page_lock_assert(m, MA_OWNED);
915 if ((object = m->object) == NULL)
917 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
918 if (m->oflags & VPO_BUSY) {
919 m->oflags &= ~VPO_BUSY;
924 * Now remove from the object's list of backed pages.
926 if ((next = TAILQ_NEXT(m, listq)) != NULL && next->left == m) {
928 * Since the page's successor in the list is also its parent
929 * in the tree, its right subtree must be empty.
931 next->left = m->left;
932 KASSERT(m->right == NULL,
933 ("vm_page_remove: page %p has right child", m));
934 } else if ((prev = TAILQ_PREV(m, pglist, listq)) != NULL &&
937 * Since the page's predecessor in the list is also its parent
938 * in the tree, its left subtree must be empty.
940 KASSERT(m->left == NULL,
941 ("vm_page_remove: page %p has left child", m));
942 prev->right = m->right;
944 if (m != object->root)
945 vm_page_splay(m->pindex, object->root);
948 else if (m->right == NULL)
952 * Move the page's successor to the root, because
953 * pages are usually removed in ascending order.
955 if (m->right != next)
956 vm_page_splay(m->pindex, m->right);
957 next->left = m->left;
962 TAILQ_REMOVE(&object->memq, m, listq);
965 * And show that the object has one fewer resident page.
967 object->resident_page_count--;
969 * The vnode may now be recycled.
971 if (object->resident_page_count == 0 && object->type == OBJT_VNODE)
972 vdrop((struct vnode *)object->handle);
980 * Returns the page associated with the object/offset
981 * pair specified; if none is found, NULL is returned.
983 * The object must be locked.
984 * This routine may not block.
985 * This is a critical path routine
988 vm_page_lookup(vm_object_t object, vm_pindex_t pindex)
992 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
993 if ((m = object->root) != NULL && m->pindex != pindex) {
994 m = vm_page_splay(pindex, m);
995 if ((object->root = m)->pindex != pindex)
1002 * vm_page_find_least:
1004 * Returns the page associated with the object with least pindex
1005 * greater than or equal to the parameter pindex, or NULL.
1007 * The object must be locked.
1008 * The routine may not block.
1011 vm_page_find_least(vm_object_t object, vm_pindex_t pindex)
1015 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1016 if ((m = TAILQ_FIRST(&object->memq)) != NULL) {
1017 if (m->pindex < pindex) {
1018 m = vm_page_splay(pindex, object->root);
1019 if ((object->root = m)->pindex < pindex)
1020 m = TAILQ_NEXT(m, listq);
1027 * Returns the given page's successor (by pindex) within the object if it is
1028 * resident; if none is found, NULL is returned.
1030 * The object must be locked.
1033 vm_page_next(vm_page_t m)
1037 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1038 if ((next = TAILQ_NEXT(m, listq)) != NULL &&
1039 next->pindex != m->pindex + 1)
1045 * Returns the given page's predecessor (by pindex) within the object if it is
1046 * resident; if none is found, NULL is returned.
1048 * The object must be locked.
1051 vm_page_prev(vm_page_t m)
1055 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1056 if ((prev = TAILQ_PREV(m, pglist, listq)) != NULL &&
1057 prev->pindex != m->pindex - 1)
1065 * Move the given memory entry from its
1066 * current object to the specified target object/offset.
1068 * The object must be locked.
1069 * This routine may not block.
1071 * Note: swap associated with the page must be invalidated by the move. We
1072 * have to do this for several reasons: (1) we aren't freeing the
1073 * page, (2) we are dirtying the page, (3) the VM system is probably
1074 * moving the page from object A to B, and will then later move
1075 * the backing store from A to B and we can't have a conflict.
1077 * Note: we *always* dirty the page. It is necessary both for the
1078 * fact that we moved it, and because we may be invalidating
1079 * swap. If the page is on the cache, we have to deactivate it
1080 * or vm_page_dirty() will panic. Dirty pages are not allowed
1084 vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex)
1088 vm_page_insert(m, new_object, new_pindex);
1093 * Convert all of the given object's cached pages that have a
1094 * pindex within the given range into free pages. If the value
1095 * zero is given for "end", then the range's upper bound is
1096 * infinity. If the given object is backed by a vnode and it
1097 * transitions from having one or more cached pages to none, the
1098 * vnode's hold count is reduced.
1101 vm_page_cache_free(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1103 vm_page_t m, m_next;
1106 mtx_lock(&vm_page_queue_free_mtx);
1107 if (__predict_false(object->cache == NULL)) {
1108 mtx_unlock(&vm_page_queue_free_mtx);
1111 m = object->cache = vm_page_splay(start, object->cache);
1112 if (m->pindex < start) {
1113 if (m->right == NULL)
1116 m_next = vm_page_splay(start, m->right);
1119 m = object->cache = m_next;
1124 * At this point, "m" is either (1) a reference to the page
1125 * with the least pindex that is greater than or equal to
1126 * "start" or (2) NULL.
1128 for (; m != NULL && (m->pindex < end || end == 0); m = m_next) {
1130 * Find "m"'s successor and remove "m" from the
1133 if (m->right == NULL) {
1134 object->cache = m->left;
1137 m_next = vm_page_splay(start, m->right);
1138 m_next->left = m->left;
1139 object->cache = m_next;
1141 /* Convert "m" to a free page. */
1144 /* Clear PG_CACHED and set PG_FREE. */
1145 m->flags ^= PG_CACHED | PG_FREE;
1146 KASSERT((m->flags & (PG_CACHED | PG_FREE)) == PG_FREE,
1147 ("vm_page_cache_free: page %p has inconsistent flags", m));
1148 cnt.v_cache_count--;
1151 empty = object->cache == NULL;
1152 mtx_unlock(&vm_page_queue_free_mtx);
1153 if (object->type == OBJT_VNODE && empty)
1154 vdrop(object->handle);
1158 * Returns the cached page that is associated with the given
1159 * object and offset. If, however, none exists, returns NULL.
1161 * The free page queue must be locked.
1163 static inline vm_page_t
1164 vm_page_cache_lookup(vm_object_t object, vm_pindex_t pindex)
1168 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1169 if ((m = object->cache) != NULL && m->pindex != pindex) {
1170 m = vm_page_splay(pindex, m);
1171 if ((object->cache = m)->pindex != pindex)
1178 * Remove the given cached page from its containing object's
1179 * collection of cached pages.
1181 * The free page queue must be locked.
1184 vm_page_cache_remove(vm_page_t m)
1189 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1190 KASSERT((m->flags & PG_CACHED) != 0,
1191 ("vm_page_cache_remove: page %p is not cached", m));
1193 if (m != object->cache) {
1194 root = vm_page_splay(m->pindex, object->cache);
1196 ("vm_page_cache_remove: page %p is not cached in object %p",
1199 if (m->left == NULL)
1201 else if (m->right == NULL)
1204 root = vm_page_splay(m->pindex, m->left);
1205 root->right = m->right;
1207 object->cache = root;
1209 cnt.v_cache_count--;
1213 * Transfer all of the cached pages with offset greater than or
1214 * equal to 'offidxstart' from the original object's cache to the
1215 * new object's cache. However, any cached pages with offset
1216 * greater than or equal to the new object's size are kept in the
1217 * original object. Initially, the new object's cache must be
1218 * empty. Offset 'offidxstart' in the original object must
1219 * correspond to offset zero in the new object.
1221 * The new object must be locked.
1224 vm_page_cache_transfer(vm_object_t orig_object, vm_pindex_t offidxstart,
1225 vm_object_t new_object)
1227 vm_page_t m, m_next;
1230 * Insertion into an object's collection of cached pages
1231 * requires the object to be locked. In contrast, removal does
1234 VM_OBJECT_LOCK_ASSERT(new_object, MA_OWNED);
1235 KASSERT(new_object->cache == NULL,
1236 ("vm_page_cache_transfer: object %p has cached pages",
1238 mtx_lock(&vm_page_queue_free_mtx);
1239 if ((m = orig_object->cache) != NULL) {
1241 * Transfer all of the pages with offset greater than or
1242 * equal to 'offidxstart' from the original object's
1243 * cache to the new object's cache.
1245 m = vm_page_splay(offidxstart, m);
1246 if (m->pindex < offidxstart) {
1247 orig_object->cache = m;
1248 new_object->cache = m->right;
1251 orig_object->cache = m->left;
1252 new_object->cache = m;
1255 while ((m = new_object->cache) != NULL) {
1256 if ((m->pindex - offidxstart) >= new_object->size) {
1258 * Return all of the cached pages with
1259 * offset greater than or equal to the
1260 * new object's size to the original
1263 new_object->cache = m->left;
1264 m->left = orig_object->cache;
1265 orig_object->cache = m;
1268 m_next = vm_page_splay(m->pindex, m->right);
1269 /* Update the page's object and offset. */
1270 m->object = new_object;
1271 m->pindex -= offidxstart;
1276 new_object->cache = m_next;
1278 KASSERT(new_object->cache == NULL ||
1279 new_object->type == OBJT_SWAP,
1280 ("vm_page_cache_transfer: object %p's type is incompatible"
1281 " with cached pages", new_object));
1283 mtx_unlock(&vm_page_queue_free_mtx);
1289 * Allocate and return a memory cell associated
1290 * with this VM object/offset pair.
1292 * The caller must always specify an allocation class.
1294 * allocation classes:
1295 * VM_ALLOC_NORMAL normal process request
1296 * VM_ALLOC_SYSTEM system *really* needs a page
1297 * VM_ALLOC_INTERRUPT interrupt time request
1299 * optional allocation flags:
1300 * VM_ALLOC_ZERO prefer a zeroed page
1301 * VM_ALLOC_WIRED wire the allocated page
1302 * VM_ALLOC_NOOBJ page is not associated with a vm object
1303 * VM_ALLOC_NOBUSY do not set the page busy
1304 * VM_ALLOC_IFCACHED return page only if it is cached
1305 * VM_ALLOC_IFNOTCACHED return NULL, do not reactivate if the page
1308 * This routine may not sleep.
1311 vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req)
1313 struct vnode *vp = NULL;
1314 vm_object_t m_object;
1316 int flags, page_req;
1318 if ((req & VM_ALLOC_NOOBJ) == 0) {
1319 KASSERT(object != NULL,
1320 ("vm_page_alloc: NULL object."));
1321 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1324 page_req = req & VM_ALLOC_CLASS_MASK;
1327 * The pager is allowed to eat deeper into the free page list.
1329 if ((curproc == pageproc) && (page_req != VM_ALLOC_INTERRUPT))
1330 page_req = VM_ALLOC_SYSTEM;
1332 mtx_lock(&vm_page_queue_free_mtx);
1333 if (cnt.v_free_count + cnt.v_cache_count > cnt.v_free_reserved ||
1334 (page_req == VM_ALLOC_SYSTEM &&
1335 cnt.v_free_count + cnt.v_cache_count > cnt.v_interrupt_free_min) ||
1336 (page_req == VM_ALLOC_INTERRUPT &&
1337 cnt.v_free_count + cnt.v_cache_count > 0)) {
1339 * Allocate from the free queue if the number of free pages
1340 * exceeds the minimum for the request class.
1342 if (object != NULL &&
1343 (m = vm_page_cache_lookup(object, pindex)) != NULL) {
1344 if ((req & VM_ALLOC_IFNOTCACHED) != 0) {
1345 mtx_unlock(&vm_page_queue_free_mtx);
1348 if (vm_phys_unfree_page(m))
1349 vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m, 0);
1350 #if VM_NRESERVLEVEL > 0
1351 else if (!vm_reserv_reactivate_page(m))
1355 panic("vm_page_alloc: cache page %p is missing"
1356 " from the free queue", m);
1357 } else if ((req & VM_ALLOC_IFCACHED) != 0) {
1358 mtx_unlock(&vm_page_queue_free_mtx);
1360 #if VM_NRESERVLEVEL > 0
1361 } else if (object == NULL || object->type == OBJT_DEVICE ||
1362 object->type == OBJT_SG ||
1363 (object->flags & OBJ_COLORED) == 0 ||
1364 (m = vm_reserv_alloc_page(object, pindex)) == NULL) {
1368 m = vm_phys_alloc_pages(object != NULL ?
1369 VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT, 0);
1370 #if VM_NRESERVLEVEL > 0
1371 if (m == NULL && vm_reserv_reclaim_inactive()) {
1372 m = vm_phys_alloc_pages(object != NULL ?
1373 VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT,
1380 * Not allocatable, give up.
1382 mtx_unlock(&vm_page_queue_free_mtx);
1383 atomic_add_int(&vm_pageout_deficit,
1384 MAX((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1));
1385 pagedaemon_wakeup();
1390 * At this point we had better have found a good page.
1393 KASSERT(m != NULL, ("vm_page_alloc: missing page"));
1394 KASSERT(m->queue == PQ_NONE,
1395 ("vm_page_alloc: page %p has unexpected queue %d", m, m->queue));
1396 KASSERT(m->wire_count == 0, ("vm_page_alloc: page %p is wired", m));
1397 KASSERT(m->hold_count == 0, ("vm_page_alloc: page %p is held", m));
1398 KASSERT(m->busy == 0, ("vm_page_alloc: page %p is busy", m));
1399 KASSERT(m->dirty == 0, ("vm_page_alloc: page %p is dirty", m));
1400 KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT,
1401 ("vm_page_alloc: page %p has unexpected memattr %d", m,
1402 pmap_page_get_memattr(m)));
1403 if ((m->flags & PG_CACHED) != 0) {
1404 KASSERT(m->valid != 0,
1405 ("vm_page_alloc: cached page %p is invalid", m));
1406 if (m->object == object && m->pindex == pindex)
1407 cnt.v_reactivated++;
1410 m_object = m->object;
1411 vm_page_cache_remove(m);
1412 if (m_object->type == OBJT_VNODE && m_object->cache == NULL)
1413 vp = m_object->handle;
1415 KASSERT(VM_PAGE_IS_FREE(m),
1416 ("vm_page_alloc: page %p is not free", m));
1417 KASSERT(m->valid == 0,
1418 ("vm_page_alloc: free page %p is valid", m));
1423 * Only the PG_ZERO flag is inherited. The PG_CACHED or PG_FREE flag
1424 * must be cleared before the free page queues lock is released.
1427 if (m->flags & PG_ZERO) {
1428 vm_page_zero_count--;
1429 if (req & VM_ALLOC_ZERO)
1433 mtx_unlock(&vm_page_queue_free_mtx);
1435 if (object == NULL || object->type == OBJT_PHYS)
1436 m->oflags = VPO_UNMANAGED;
1439 if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ)) == 0)
1440 m->oflags |= VPO_BUSY;
1441 if (req & VM_ALLOC_WIRED) {
1443 * The page lock is not required for wiring a page until that
1444 * page is inserted into the object.
1446 atomic_add_int(&cnt.v_wire_count, 1);
1451 if (object != NULL) {
1452 /* Ignore device objects; the pager sets "memattr" for them. */
1453 if (object->memattr != VM_MEMATTR_DEFAULT &&
1454 object->type != OBJT_DEVICE && object->type != OBJT_SG)
1455 pmap_page_set_memattr(m, object->memattr);
1456 vm_page_insert(m, object, pindex);
1461 * The following call to vdrop() must come after the above call
1462 * to vm_page_insert() in case both affect the same object and
1463 * vnode. Otherwise, the affected vnode's hold count could
1464 * temporarily become zero.
1470 * Don't wakeup too often - wakeup the pageout daemon when
1471 * we would be nearly out of memory.
1473 if (vm_paging_needed())
1474 pagedaemon_wakeup();
1480 * Initialize a page that has been freshly dequeued from a freelist.
1481 * The caller has to drop the vnode returned, if it is not NULL.
1483 * To be called with vm_page_queue_free_mtx held.
1486 vm_page_alloc_init(vm_page_t m)
1489 vm_object_t m_object;
1491 KASSERT(m->queue == PQ_NONE,
1492 ("vm_page_alloc_init: page %p has unexpected queue %d",
1494 KASSERT(m->wire_count == 0,
1495 ("vm_page_alloc_init: page %p is wired", m));
1496 KASSERT(m->hold_count == 0,
1497 ("vm_page_alloc_init: page %p is held", m));
1498 KASSERT(m->busy == 0,
1499 ("vm_page_alloc_init: page %p is busy", m));
1500 KASSERT(m->dirty == 0,
1501 ("vm_page_alloc_init: page %p is dirty", m));
1502 KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT,
1503 ("vm_page_alloc_init: page %p has unexpected memattr %d",
1504 m, pmap_page_get_memattr(m)));
1505 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1507 if ((m->flags & PG_CACHED) != 0) {
1509 m_object = m->object;
1510 vm_page_cache_remove(m);
1511 if (m_object->type == OBJT_VNODE &&
1512 m_object->cache == NULL)
1513 drop = m_object->handle;
1515 KASSERT(VM_PAGE_IS_FREE(m),
1516 ("vm_page_alloc_init: page %p is not free", m));
1517 KASSERT(m->valid == 0,
1518 ("vm_page_alloc_init: free page %p is valid", m));
1521 if (m->flags & PG_ZERO)
1522 vm_page_zero_count--;
1523 /* Don't clear the PG_ZERO flag; we'll need it later. */
1524 m->flags &= PG_ZERO;
1526 m->oflags = VPO_UNMANAGED;
1527 /* Unmanaged pages don't use "act_count". */
1532 * vm_page_alloc_freelist:
1534 * Allocate a page from the specified freelist.
1535 * Only the ALLOC_CLASS values in req are honored, other request flags
1539 vm_page_alloc_freelist(int flind, int req)
1546 page_req = req & VM_ALLOC_CLASS_MASK;
1547 mtx_lock(&vm_page_queue_free_mtx);
1549 * Do not allocate reserved pages unless the req has asked for it.
1551 if (cnt.v_free_count + cnt.v_cache_count > cnt.v_free_reserved ||
1552 (page_req == VM_ALLOC_SYSTEM &&
1553 cnt.v_free_count + cnt.v_cache_count > cnt.v_interrupt_free_min) ||
1554 (page_req == VM_ALLOC_INTERRUPT &&
1555 cnt.v_free_count + cnt.v_cache_count > 0)) {
1556 m = vm_phys_alloc_freelist_pages(flind, VM_FREEPOOL_DIRECT, 0);
1559 mtx_unlock(&vm_page_queue_free_mtx);
1562 drop = vm_page_alloc_init(m);
1563 mtx_unlock(&vm_page_queue_free_mtx);
1570 * vm_wait: (also see VM_WAIT macro)
1572 * Block until free pages are available for allocation
1573 * - Called in various places before memory allocations.
1579 mtx_lock(&vm_page_queue_free_mtx);
1580 if (curproc == pageproc) {
1581 vm_pageout_pages_needed = 1;
1582 msleep(&vm_pageout_pages_needed, &vm_page_queue_free_mtx,
1583 PDROP | PSWP, "VMWait", 0);
1585 if (!vm_pages_needed) {
1586 vm_pages_needed = 1;
1587 wakeup(&vm_pages_needed);
1589 msleep(&cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PVM,
1595 * vm_waitpfault: (also see VM_WAITPFAULT macro)
1597 * Block until free pages are available for allocation
1598 * - Called only in vm_fault so that processes page faulting
1599 * can be easily tracked.
1600 * - Sleeps at a lower priority than vm_wait() so that vm_wait()ing
1601 * processes will be able to grab memory first. Do not change
1602 * this balance without careful testing first.
1608 mtx_lock(&vm_page_queue_free_mtx);
1609 if (!vm_pages_needed) {
1610 vm_pages_needed = 1;
1611 wakeup(&vm_pages_needed);
1613 msleep(&cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PUSER,
1620 * Move the given page to the tail of its present page queue.
1622 * The page queues must be locked.
1625 vm_page_requeue(vm_page_t m)
1627 struct vpgqueues *vpq;
1630 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1632 KASSERT(queue != PQ_NONE,
1633 ("vm_page_requeue: page %p is not queued", m));
1634 vpq = &vm_page_queues[queue];
1635 TAILQ_REMOVE(&vpq->pl, m, pageq);
1636 TAILQ_INSERT_TAIL(&vpq->pl, m, pageq);
1640 * vm_page_queue_remove:
1642 * Remove the given page from the specified queue.
1644 * The page and page queues must be locked.
1646 static __inline void
1647 vm_page_queue_remove(int queue, vm_page_t m)
1649 struct vpgqueues *pq;
1651 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1652 vm_page_lock_assert(m, MA_OWNED);
1653 pq = &vm_page_queues[queue];
1654 TAILQ_REMOVE(&pq->pl, m, pageq);
1661 * Remove a page from its queue.
1663 * The given page must be locked.
1664 * This routine may not block.
1667 vm_pageq_remove(vm_page_t m)
1671 vm_page_lock_assert(m, MA_OWNED);
1672 if ((queue = m->queue) != PQ_NONE) {
1673 vm_page_lock_queues();
1675 vm_page_queue_remove(queue, m);
1676 vm_page_unlock_queues();
1683 * Add the given page to the specified queue.
1685 * The page queues must be locked.
1688 vm_page_enqueue(int queue, vm_page_t m)
1690 struct vpgqueues *vpq;
1692 vpq = &vm_page_queues[queue];
1694 TAILQ_INSERT_TAIL(&vpq->pl, m, pageq);
1701 * Put the specified page on the active list (if appropriate).
1702 * Ensure that act_count is at least ACT_INIT but do not otherwise
1705 * The page must be locked.
1706 * This routine may not block.
1709 vm_page_activate(vm_page_t m)
1713 vm_page_lock_assert(m, MA_OWNED);
1714 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1715 if ((queue = m->queue) != PQ_ACTIVE) {
1716 if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) {
1717 if (m->act_count < ACT_INIT)
1718 m->act_count = ACT_INIT;
1719 vm_page_lock_queues();
1720 if (queue != PQ_NONE)
1721 vm_page_queue_remove(queue, m);
1722 vm_page_enqueue(PQ_ACTIVE, m);
1723 vm_page_unlock_queues();
1725 KASSERT(queue == PQ_NONE,
1726 ("vm_page_activate: wired page %p is queued", m));
1728 if (m->act_count < ACT_INIT)
1729 m->act_count = ACT_INIT;
1734 * vm_page_free_wakeup:
1736 * Helper routine for vm_page_free_toq() and vm_page_cache(). This
1737 * routine is called when a page has been added to the cache or free
1740 * The page queues must be locked.
1741 * This routine may not block.
1744 vm_page_free_wakeup(void)
1747 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1749 * if pageout daemon needs pages, then tell it that there are
1752 if (vm_pageout_pages_needed &&
1753 cnt.v_cache_count + cnt.v_free_count >= cnt.v_pageout_free_min) {
1754 wakeup(&vm_pageout_pages_needed);
1755 vm_pageout_pages_needed = 0;
1758 * wakeup processes that are waiting on memory if we hit a
1759 * high water mark. And wakeup scheduler process if we have
1760 * lots of memory. this process will swapin processes.
1762 if (vm_pages_needed && !vm_page_count_min()) {
1763 vm_pages_needed = 0;
1764 wakeup(&cnt.v_free_count);
1771 * Returns the given page to the free list,
1772 * disassociating it with any VM object.
1774 * Object and page must be locked prior to entry.
1775 * This routine may not block.
1779 vm_page_free_toq(vm_page_t m)
1782 if ((m->oflags & VPO_UNMANAGED) == 0) {
1783 vm_page_lock_assert(m, MA_OWNED);
1784 KASSERT(!pmap_page_is_mapped(m),
1785 ("vm_page_free_toq: freeing mapped page %p", m));
1787 PCPU_INC(cnt.v_tfree);
1789 if (VM_PAGE_IS_FREE(m))
1790 panic("vm_page_free: freeing free page %p", m);
1791 else if (m->busy != 0)
1792 panic("vm_page_free: freeing busy page %p", m);
1795 * unqueue, then remove page. Note that we cannot destroy
1796 * the page here because we do not want to call the pager's
1797 * callback routine until after we've put the page on the
1798 * appropriate free queue.
1800 if ((m->oflags & VPO_UNMANAGED) == 0)
1805 * If fictitious remove object association and
1806 * return, otherwise delay object association removal.
1808 if ((m->flags & PG_FICTITIOUS) != 0) {
1815 if (m->wire_count != 0)
1816 panic("vm_page_free: freeing wired page %p", m);
1817 if (m->hold_count != 0) {
1818 m->flags &= ~PG_ZERO;
1819 vm_page_lock_queues();
1820 vm_page_enqueue(PQ_HOLD, m);
1821 vm_page_unlock_queues();
1824 * Restore the default memory attribute to the page.
1826 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT)
1827 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT);
1830 * Insert the page into the physical memory allocator's
1831 * cache/free page queues.
1833 mtx_lock(&vm_page_queue_free_mtx);
1834 m->flags |= PG_FREE;
1836 #if VM_NRESERVLEVEL > 0
1837 if (!vm_reserv_free_page(m))
1841 vm_phys_free_pages(m, 0);
1842 if ((m->flags & PG_ZERO) != 0)
1843 ++vm_page_zero_count;
1845 vm_page_zero_idle_wakeup();
1846 vm_page_free_wakeup();
1847 mtx_unlock(&vm_page_queue_free_mtx);
1854 * Mark this page as wired down by yet
1855 * another map, removing it from paging queues
1858 * If the page is fictitious, then its wire count must remain one.
1860 * The page must be locked.
1861 * This routine may not block.
1864 vm_page_wire(vm_page_t m)
1868 * Only bump the wire statistics if the page is not already wired,
1869 * and only unqueue the page if it is on some queue (if it is unmanaged
1870 * it is already off the queues).
1872 vm_page_lock_assert(m, MA_OWNED);
1873 if ((m->flags & PG_FICTITIOUS) != 0) {
1874 KASSERT(m->wire_count == 1,
1875 ("vm_page_wire: fictitious page %p's wire count isn't one",
1879 if (m->wire_count == 0) {
1880 if ((m->oflags & VPO_UNMANAGED) == 0)
1882 atomic_add_int(&cnt.v_wire_count, 1);
1885 KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m));
1891 * Release one wiring of the specified page, potentially enabling it to be
1892 * paged again. If paging is enabled, then the value of the parameter
1893 * "activate" determines to which queue the page is added. If "activate" is
1894 * non-zero, then the page is added to the active queue. Otherwise, it is
1895 * added to the inactive queue.
1897 * However, unless the page belongs to an object, it is not enqueued because
1898 * it cannot be paged out.
1900 * If a page is fictitious, then its wire count must alway be one.
1902 * A managed page must be locked.
1905 vm_page_unwire(vm_page_t m, int activate)
1908 if ((m->oflags & VPO_UNMANAGED) == 0)
1909 vm_page_lock_assert(m, MA_OWNED);
1910 if ((m->flags & PG_FICTITIOUS) != 0) {
1911 KASSERT(m->wire_count == 1,
1912 ("vm_page_unwire: fictitious page %p's wire count isn't one", m));
1915 if (m->wire_count > 0) {
1917 if (m->wire_count == 0) {
1918 atomic_subtract_int(&cnt.v_wire_count, 1);
1919 if ((m->oflags & VPO_UNMANAGED) != 0 ||
1922 vm_page_lock_queues();
1924 vm_page_enqueue(PQ_ACTIVE, m);
1926 m->flags &= ~PG_WINATCFLS;
1927 vm_page_enqueue(PQ_INACTIVE, m);
1929 vm_page_unlock_queues();
1932 panic("vm_page_unwire: page %p's wire count is zero", m);
1936 * Move the specified page to the inactive queue.
1938 * Many pages placed on the inactive queue should actually go
1939 * into the cache, but it is difficult to figure out which. What
1940 * we do instead, if the inactive target is well met, is to put
1941 * clean pages at the head of the inactive queue instead of the tail.
1942 * This will cause them to be moved to the cache more quickly and
1943 * if not actively re-referenced, reclaimed more quickly. If we just
1944 * stick these pages at the end of the inactive queue, heavy filesystem
1945 * meta-data accesses can cause an unnecessary paging load on memory bound
1946 * processes. This optimization causes one-time-use metadata to be
1947 * reused more quickly.
1949 * Normally athead is 0 resulting in LRU operation. athead is set
1950 * to 1 if we want this page to be 'as if it were placed in the cache',
1951 * except without unmapping it from the process address space.
1953 * This routine may not block.
1956 _vm_page_deactivate(vm_page_t m, int athead)
1960 vm_page_lock_assert(m, MA_OWNED);
1963 * Ignore if already inactive.
1965 if ((queue = m->queue) == PQ_INACTIVE)
1967 if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) {
1968 vm_page_lock_queues();
1969 m->flags &= ~PG_WINATCFLS;
1970 if (queue != PQ_NONE)
1971 vm_page_queue_remove(queue, m);
1973 TAILQ_INSERT_HEAD(&vm_page_queues[PQ_INACTIVE].pl, m,
1976 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m,
1978 m->queue = PQ_INACTIVE;
1979 cnt.v_inactive_count++;
1980 vm_page_unlock_queues();
1985 * Move the specified page to the inactive queue.
1987 * The page must be locked.
1990 vm_page_deactivate(vm_page_t m)
1993 _vm_page_deactivate(m, 0);
1997 * vm_page_try_to_cache:
1999 * Returns 0 on failure, 1 on success
2002 vm_page_try_to_cache(vm_page_t m)
2005 vm_page_lock_assert(m, MA_OWNED);
2006 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2007 if (m->dirty || m->hold_count || m->busy || m->wire_count ||
2008 (m->oflags & (VPO_BUSY | VPO_UNMANAGED)) != 0)
2018 * vm_page_try_to_free()
2020 * Attempt to free the page. If we cannot free it, we do nothing.
2021 * 1 is returned on success, 0 on failure.
2024 vm_page_try_to_free(vm_page_t m)
2027 vm_page_lock_assert(m, MA_OWNED);
2028 if (m->object != NULL)
2029 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2030 if (m->dirty || m->hold_count || m->busy || m->wire_count ||
2031 (m->oflags & (VPO_BUSY | VPO_UNMANAGED)) != 0)
2043 * Put the specified page onto the page cache queue (if appropriate).
2045 * This routine may not block.
2048 vm_page_cache(vm_page_t m)
2051 vm_page_t next, prev, root;
2053 vm_page_lock_assert(m, MA_OWNED);
2055 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2056 if ((m->oflags & (VPO_UNMANAGED | VPO_BUSY)) || m->busy ||
2057 m->hold_count || m->wire_count)
2058 panic("vm_page_cache: attempting to cache busy page");
2061 panic("vm_page_cache: page %p is dirty", m);
2062 if (m->valid == 0 || object->type == OBJT_DEFAULT ||
2063 (object->type == OBJT_SWAP &&
2064 !vm_pager_has_page(object, m->pindex, NULL, NULL))) {
2066 * Hypothesis: A cache-elgible page belonging to a
2067 * default object or swap object but without a backing
2068 * store must be zero filled.
2073 KASSERT((m->flags & PG_CACHED) == 0,
2074 ("vm_page_cache: page %p is already cached", m));
2075 PCPU_INC(cnt.v_tcached);
2078 * Remove the page from the paging queues.
2083 * Remove the page from the object's collection of resident
2086 if ((next = TAILQ_NEXT(m, listq)) != NULL && next->left == m) {
2088 * Since the page's successor in the list is also its parent
2089 * in the tree, its right subtree must be empty.
2091 next->left = m->left;
2092 KASSERT(m->right == NULL,
2093 ("vm_page_cache: page %p has right child", m));
2094 } else if ((prev = TAILQ_PREV(m, pglist, listq)) != NULL &&
2097 * Since the page's predecessor in the list is also its parent
2098 * in the tree, its left subtree must be empty.
2100 KASSERT(m->left == NULL,
2101 ("vm_page_cache: page %p has left child", m));
2102 prev->right = m->right;
2104 if (m != object->root)
2105 vm_page_splay(m->pindex, object->root);
2106 if (m->left == NULL)
2108 else if (m->right == NULL)
2112 * Move the page's successor to the root, because
2113 * pages are usually removed in ascending order.
2115 if (m->right != next)
2116 vm_page_splay(m->pindex, m->right);
2117 next->left = m->left;
2120 object->root = root;
2122 TAILQ_REMOVE(&object->memq, m, listq);
2123 object->resident_page_count--;
2126 * Restore the default memory attribute to the page.
2128 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT)
2129 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT);
2132 * Insert the page into the object's collection of cached pages
2133 * and the physical memory allocator's cache/free page queues.
2135 m->flags &= ~PG_ZERO;
2136 mtx_lock(&vm_page_queue_free_mtx);
2137 m->flags |= PG_CACHED;
2138 cnt.v_cache_count++;
2139 root = object->cache;
2144 root = vm_page_splay(m->pindex, root);
2145 if (m->pindex < root->pindex) {
2146 m->left = root->left;
2149 } else if (__predict_false(m->pindex == root->pindex))
2150 panic("vm_page_cache: offset already cached");
2152 m->right = root->right;
2158 #if VM_NRESERVLEVEL > 0
2159 if (!vm_reserv_free_page(m)) {
2163 vm_phys_set_pool(VM_FREEPOOL_CACHE, m, 0);
2164 vm_phys_free_pages(m, 0);
2166 vm_page_free_wakeup();
2167 mtx_unlock(&vm_page_queue_free_mtx);
2170 * Increment the vnode's hold count if this is the object's only
2171 * cached page. Decrement the vnode's hold count if this was
2172 * the object's only resident page.
2174 if (object->type == OBJT_VNODE) {
2175 if (root == NULL && object->resident_page_count != 0)
2176 vhold(object->handle);
2177 else if (root != NULL && object->resident_page_count == 0)
2178 vdrop(object->handle);
2185 * Cache, deactivate, or do nothing as appropriate. This routine
2186 * is typically used by madvise() MADV_DONTNEED.
2188 * Generally speaking we want to move the page into the cache so
2189 * it gets reused quickly. However, this can result in a silly syndrome
2190 * due to the page recycling too quickly. Small objects will not be
2191 * fully cached. On the otherhand, if we move the page to the inactive
2192 * queue we wind up with a problem whereby very large objects
2193 * unnecessarily blow away our inactive and cache queues.
2195 * The solution is to move the pages based on a fixed weighting. We
2196 * either leave them alone, deactivate them, or move them to the cache,
2197 * where moving them to the cache has the highest weighting.
2198 * By forcing some pages into other queues we eventually force the
2199 * system to balance the queues, potentially recovering other unrelated
2200 * space from active. The idea is to not force this to happen too
2204 vm_page_dontneed(vm_page_t m)
2209 vm_page_lock_assert(m, MA_OWNED);
2210 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2211 dnw = PCPU_GET(dnweight);
2215 * Occasionally leave the page alone.
2217 if ((dnw & 0x01F0) == 0 || m->queue == PQ_INACTIVE) {
2218 if (m->act_count >= ACT_INIT)
2224 * Clear any references to the page. Otherwise, the page daemon will
2225 * immediately reactivate the page.
2227 * Perform the pmap_clear_reference() first. Otherwise, a concurrent
2228 * pmap operation, such as pmap_remove(), could clear a reference in
2229 * the pmap and set PGA_REFERENCED on the page before the
2230 * pmap_clear_reference() had completed. Consequently, the page would
2231 * appear referenced based upon an old reference that occurred before
2232 * this function ran.
2234 pmap_clear_reference(m);
2235 vm_page_aflag_clear(m, PGA_REFERENCED);
2237 if (m->dirty == 0 && pmap_is_modified(m))
2240 if (m->dirty || (dnw & 0x0070) == 0) {
2242 * Deactivate the page 3 times out of 32.
2247 * Cache the page 28 times out of every 32. Note that
2248 * the page is deactivated instead of cached, but placed
2249 * at the head of the queue instead of the tail.
2253 _vm_page_deactivate(m, head);
2257 * Grab a page, waiting until we are waken up due to the page
2258 * changing state. We keep on waiting, if the page continues
2259 * to be in the object. If the page doesn't exist, first allocate it
2260 * and then conditionally zero it.
2262 * The caller must always specify the VM_ALLOC_RETRY flag. This is intended
2263 * to facilitate its eventual removal.
2265 * This routine may block.
2268 vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags)
2272 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2273 KASSERT((allocflags & VM_ALLOC_RETRY) != 0,
2274 ("vm_page_grab: VM_ALLOC_RETRY is required"));
2276 if ((m = vm_page_lookup(object, pindex)) != NULL) {
2277 if ((m->oflags & VPO_BUSY) != 0 ||
2278 ((allocflags & VM_ALLOC_IGN_SBUSY) == 0 && m->busy != 0)) {
2280 * Reference the page before unlocking and
2281 * sleeping so that the page daemon is less
2282 * likely to reclaim it.
2284 vm_page_aflag_set(m, PGA_REFERENCED);
2285 vm_page_sleep(m, "pgrbwt");
2288 if ((allocflags & VM_ALLOC_WIRED) != 0) {
2293 if ((allocflags & VM_ALLOC_NOBUSY) == 0)
2298 m = vm_page_alloc(object, pindex, allocflags & ~(VM_ALLOC_RETRY |
2299 VM_ALLOC_IGN_SBUSY));
2301 VM_OBJECT_UNLOCK(object);
2303 VM_OBJECT_LOCK(object);
2305 } else if (m->valid != 0)
2307 if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0)
2313 * Mapping function for valid bits or for dirty bits in
2314 * a page. May not block.
2316 * Inputs are required to range within a page.
2319 vm_page_bits(int base, int size)
2325 base + size <= PAGE_SIZE,
2326 ("vm_page_bits: illegal base/size %d/%d", base, size)
2329 if (size == 0) /* handle degenerate case */
2332 first_bit = base >> DEV_BSHIFT;
2333 last_bit = (base + size - 1) >> DEV_BSHIFT;
2335 return ((2 << last_bit) - (1 << first_bit));
2339 * vm_page_set_valid:
2341 * Sets portions of a page valid. The arguments are expected
2342 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
2343 * of any partial chunks touched by the range. The invalid portion of
2344 * such chunks will be zeroed.
2346 * (base + size) must be less then or equal to PAGE_SIZE.
2349 vm_page_set_valid(vm_page_t m, int base, int size)
2353 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2354 if (size == 0) /* handle degenerate case */
2358 * If the base is not DEV_BSIZE aligned and the valid
2359 * bit is clear, we have to zero out a portion of the
2362 if ((frag = base & ~(DEV_BSIZE - 1)) != base &&
2363 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0)
2364 pmap_zero_page_area(m, frag, base - frag);
2367 * If the ending offset is not DEV_BSIZE aligned and the
2368 * valid bit is clear, we have to zero out a portion of
2371 endoff = base + size;
2372 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff &&
2373 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0)
2374 pmap_zero_page_area(m, endoff,
2375 DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
2378 * Assert that no previously invalid block that is now being validated
2381 KASSERT((~m->valid & vm_page_bits(base, size) & m->dirty) == 0,
2382 ("vm_page_set_valid: page %p is dirty", m));
2385 * Set valid bits inclusive of any overlap.
2387 m->valid |= vm_page_bits(base, size);
2391 * Clear the given bits from the specified page's dirty field.
2393 static __inline void
2394 vm_page_clear_dirty_mask(vm_page_t m, int pagebits)
2397 #if PAGE_SIZE < 16384
2402 * If the object is locked and the page is neither VPO_BUSY nor
2403 * PGA_WRITEABLE, then the page's dirty field cannot possibly be
2404 * set by a concurrent pmap operation.
2406 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2407 if ((m->oflags & VPO_BUSY) == 0 && (m->aflags & PGA_WRITEABLE) == 0)
2408 m->dirty &= ~pagebits;
2411 * The pmap layer can call vm_page_dirty() without
2412 * holding a distinguished lock. The combination of
2413 * the object's lock and an atomic operation suffice
2414 * to guarantee consistency of the page dirty field.
2416 * For PAGE_SIZE == 32768 case, compiler already
2417 * properly aligns the dirty field, so no forcible
2418 * alignment is needed. Only require existence of
2419 * atomic_clear_64 when page size is 32768.
2421 addr = (uintptr_t)&m->dirty;
2422 #if PAGE_SIZE == 32768
2423 #error pagebits too short
2424 atomic_clear_64((uint64_t *)addr, pagebits);
2425 #elif PAGE_SIZE == 16384
2426 atomic_clear_32((uint32_t *)addr, pagebits);
2427 #else /* PAGE_SIZE <= 8192 */
2429 * Use a trick to perform a 32-bit atomic on the
2430 * containing aligned word, to not depend on the existence
2431 * of atomic_clear_{8, 16}.
2433 shift = addr & (sizeof(uint32_t) - 1);
2434 #if BYTE_ORDER == BIG_ENDIAN
2435 shift = (sizeof(uint32_t) - sizeof(m->dirty) - shift) * NBBY;
2439 addr &= ~(sizeof(uint32_t) - 1);
2440 atomic_clear_32((uint32_t *)addr, pagebits << shift);
2441 #endif /* PAGE_SIZE */
2446 * vm_page_set_validclean:
2448 * Sets portions of a page valid and clean. The arguments are expected
2449 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
2450 * of any partial chunks touched by the range. The invalid portion of
2451 * such chunks will be zero'd.
2453 * This routine may not block.
2455 * (base + size) must be less then or equal to PAGE_SIZE.
2458 vm_page_set_validclean(vm_page_t m, int base, int size)
2461 int endoff, frag, pagebits;
2463 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2464 if (size == 0) /* handle degenerate case */
2468 * If the base is not DEV_BSIZE aligned and the valid
2469 * bit is clear, we have to zero out a portion of the
2472 if ((frag = base & ~(DEV_BSIZE - 1)) != base &&
2473 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0)
2474 pmap_zero_page_area(m, frag, base - frag);
2477 * If the ending offset is not DEV_BSIZE aligned and the
2478 * valid bit is clear, we have to zero out a portion of
2481 endoff = base + size;
2482 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff &&
2483 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0)
2484 pmap_zero_page_area(m, endoff,
2485 DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
2488 * Set valid, clear dirty bits. If validating the entire
2489 * page we can safely clear the pmap modify bit. We also
2490 * use this opportunity to clear the VPO_NOSYNC flag. If a process
2491 * takes a write fault on a MAP_NOSYNC memory area the flag will
2494 * We set valid bits inclusive of any overlap, but we can only
2495 * clear dirty bits for DEV_BSIZE chunks that are fully within
2498 oldvalid = m->valid;
2499 pagebits = vm_page_bits(base, size);
2500 m->valid |= pagebits;
2502 if ((frag = base & (DEV_BSIZE - 1)) != 0) {
2503 frag = DEV_BSIZE - frag;
2509 pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1));
2511 if (base == 0 && size == PAGE_SIZE) {
2513 * The page can only be modified within the pmap if it is
2514 * mapped, and it can only be mapped if it was previously
2517 if (oldvalid == VM_PAGE_BITS_ALL)
2519 * Perform the pmap_clear_modify() first. Otherwise,
2520 * a concurrent pmap operation, such as
2521 * pmap_protect(), could clear a modification in the
2522 * pmap and set the dirty field on the page before
2523 * pmap_clear_modify() had begun and after the dirty
2524 * field was cleared here.
2526 pmap_clear_modify(m);
2528 m->oflags &= ~VPO_NOSYNC;
2529 } else if (oldvalid != VM_PAGE_BITS_ALL)
2530 m->dirty &= ~pagebits;
2532 vm_page_clear_dirty_mask(m, pagebits);
2536 vm_page_clear_dirty(vm_page_t m, int base, int size)
2539 vm_page_clear_dirty_mask(m, vm_page_bits(base, size));
2543 * vm_page_set_invalid:
2545 * Invalidates DEV_BSIZE'd chunks within a page. Both the
2546 * valid and dirty bits for the effected areas are cleared.
2551 vm_page_set_invalid(vm_page_t m, int base, int size)
2555 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2556 KASSERT((m->oflags & VPO_BUSY) == 0,
2557 ("vm_page_set_invalid: page %p is busy", m));
2558 bits = vm_page_bits(base, size);
2559 if (m->valid == VM_PAGE_BITS_ALL && bits != 0)
2561 KASSERT(!pmap_page_is_mapped(m),
2562 ("vm_page_set_invalid: page %p is mapped", m));
2568 * vm_page_zero_invalid()
2570 * The kernel assumes that the invalid portions of a page contain
2571 * garbage, but such pages can be mapped into memory by user code.
2572 * When this occurs, we must zero out the non-valid portions of the
2573 * page so user code sees what it expects.
2575 * Pages are most often semi-valid when the end of a file is mapped
2576 * into memory and the file's size is not page aligned.
2579 vm_page_zero_invalid(vm_page_t m, boolean_t setvalid)
2584 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2586 * Scan the valid bits looking for invalid sections that
2587 * must be zerod. Invalid sub-DEV_BSIZE'd areas ( where the
2588 * valid bit may be set ) have already been zerod by
2589 * vm_page_set_validclean().
2591 for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) {
2592 if (i == (PAGE_SIZE / DEV_BSIZE) ||
2593 (m->valid & (1 << i))
2596 pmap_zero_page_area(m,
2597 b << DEV_BSHIFT, (i - b) << DEV_BSHIFT);
2604 * setvalid is TRUE when we can safely set the zero'd areas
2605 * as being valid. We can do this if there are no cache consistancy
2606 * issues. e.g. it is ok to do with UFS, but not ok to do with NFS.
2609 m->valid = VM_PAGE_BITS_ALL;
2615 * Is (partial) page valid? Note that the case where size == 0
2616 * will return FALSE in the degenerate case where the page is
2617 * entirely invalid, and TRUE otherwise.
2622 vm_page_is_valid(vm_page_t m, int base, int size)
2624 int bits = vm_page_bits(base, size);
2626 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2627 if (m->valid && ((m->valid & bits) == bits))
2634 * update dirty bits from pmap/mmu. May not block.
2637 vm_page_test_dirty(vm_page_t m)
2640 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2641 if (m->dirty != VM_PAGE_BITS_ALL && pmap_is_modified(m))
2645 int so_zerocp_fullpage = 0;
2648 * Replace the given page with a copy. The copied page assumes
2649 * the portion of the given page's "wire_count" that is not the
2650 * responsibility of this copy-on-write mechanism.
2652 * The object containing the given page must have a non-zero
2653 * paging-in-progress count and be locked.
2656 vm_page_cowfault(vm_page_t m)
2662 mtx_assert(&vm_page_queue_mtx, MA_NOTOWNED);
2663 vm_page_lock_assert(m, MA_OWNED);
2665 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2666 KASSERT(object->paging_in_progress != 0,
2667 ("vm_page_cowfault: object %p's paging-in-progress count is zero.",
2674 mnew = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL | VM_ALLOC_NOBUSY);
2676 vm_page_insert(m, object, pindex);
2678 VM_OBJECT_UNLOCK(object);
2680 VM_OBJECT_LOCK(object);
2681 if (m == vm_page_lookup(object, pindex)) {
2686 * Page disappeared during the wait.
2694 * check to see if we raced with an xmit complete when
2695 * waiting to allocate a page. If so, put things back
2701 vm_page_unlock(mnew);
2702 vm_page_insert(m, object, pindex);
2703 } else { /* clear COW & copy page */
2704 if (!so_zerocp_fullpage)
2705 pmap_copy_page(m, mnew);
2706 mnew->valid = VM_PAGE_BITS_ALL;
2707 vm_page_dirty(mnew);
2708 mnew->wire_count = m->wire_count - m->cow;
2709 m->wire_count = m->cow;
2715 vm_page_cowclear(vm_page_t m)
2718 vm_page_lock_assert(m, MA_OWNED);
2722 * let vm_fault add back write permission lazily
2726 * sf_buf_free() will free the page, so we needn't do it here
2731 vm_page_cowsetup(vm_page_t m)
2734 vm_page_lock_assert(m, MA_OWNED);
2735 if ((m->flags & PG_FICTITIOUS) != 0 ||
2736 (m->oflags & VPO_UNMANAGED) != 0 ||
2737 m->cow == USHRT_MAX - 1 || !VM_OBJECT_TRYLOCK(m->object))
2740 pmap_remove_write(m);
2741 VM_OBJECT_UNLOCK(m->object);
2747 vm_page_object_lock_assert(vm_page_t m)
2751 * Certain of the page's fields may only be modified by the
2752 * holder of the containing object's lock or the setter of the
2753 * page's VPO_BUSY flag. Unfortunately, the setter of the
2754 * VPO_BUSY flag is not recorded, and thus cannot be checked
2757 if (m->object != NULL && (m->oflags & VPO_BUSY) == 0)
2758 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2762 #include "opt_ddb.h"
2764 #include <sys/kernel.h>
2766 #include <ddb/ddb.h>
2768 DB_SHOW_COMMAND(page, vm_page_print_page_info)
2770 db_printf("cnt.v_free_count: %d\n", cnt.v_free_count);
2771 db_printf("cnt.v_cache_count: %d\n", cnt.v_cache_count);
2772 db_printf("cnt.v_inactive_count: %d\n", cnt.v_inactive_count);
2773 db_printf("cnt.v_active_count: %d\n", cnt.v_active_count);
2774 db_printf("cnt.v_wire_count: %d\n", cnt.v_wire_count);
2775 db_printf("cnt.v_free_reserved: %d\n", cnt.v_free_reserved);
2776 db_printf("cnt.v_free_min: %d\n", cnt.v_free_min);
2777 db_printf("cnt.v_free_target: %d\n", cnt.v_free_target);
2778 db_printf("cnt.v_cache_min: %d\n", cnt.v_cache_min);
2779 db_printf("cnt.v_inactive_target: %d\n", cnt.v_inactive_target);
2782 DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info)
2785 db_printf("PQ_FREE:");
2786 db_printf(" %d", cnt.v_free_count);
2789 db_printf("PQ_CACHE:");
2790 db_printf(" %d", cnt.v_cache_count);
2793 db_printf("PQ_ACTIVE: %d, PQ_INACTIVE: %d\n",
2794 *vm_page_queues[PQ_ACTIVE].cnt,
2795 *vm_page_queues[PQ_INACTIVE].cnt);