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;
125 long vm_page_array_size;
127 int vm_page_zero_count;
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 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, vm_page_bits_t 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 page and queue locks.
296 mtx_init(&vm_page_queue_mtx, "vm page queue", NULL, MTX_DEF |
298 mtx_init(&vm_page_queue_free_mtx, "vm page free queue", NULL, MTX_DEF);
299 for (i = 0; i < PA_LOCK_COUNT; i++)
300 mtx_init(&pa_lock[i].data, "vm page", NULL, MTX_DEF);
303 * Initialize the queue headers for the hold queue, the active queue,
304 * and the inactive queue.
306 for (i = 0; i < PQ_COUNT; i++)
307 TAILQ_INIT(&vm_page_queues[i].pl);
308 vm_page_queues[PQ_INACTIVE].cnt = &cnt.v_inactive_count;
309 vm_page_queues[PQ_ACTIVE].cnt = &cnt.v_active_count;
310 vm_page_queues[PQ_HOLD].cnt = &cnt.v_active_count;
313 * Allocate memory for use when boot strapping the kernel memory
316 new_end = end - (boot_pages * UMA_SLAB_SIZE);
317 new_end = trunc_page(new_end);
318 mapped = pmap_map(&vaddr, new_end, end,
319 VM_PROT_READ | VM_PROT_WRITE);
320 bzero((void *)mapped, end - new_end);
321 uma_startup((void *)mapped, boot_pages);
323 #if defined(__amd64__) || defined(__i386__) || defined(__arm__) || \
326 * Allocate a bitmap to indicate that a random physical page
327 * needs to be included in a minidump.
329 * The amd64 port needs this to indicate which direct map pages
330 * need to be dumped, via calls to dump_add_page()/dump_drop_page().
332 * However, i386 still needs this workspace internally within the
333 * minidump code. In theory, they are not needed on i386, but are
334 * included should the sf_buf code decide to use them.
337 for (i = 0; dump_avail[i + 1] != 0; i += 2)
338 if (dump_avail[i + 1] > last_pa)
339 last_pa = dump_avail[i + 1];
340 page_range = last_pa / PAGE_SIZE;
341 vm_page_dump_size = round_page(roundup2(page_range, NBBY) / NBBY);
342 new_end -= vm_page_dump_size;
343 vm_page_dump = (void *)(uintptr_t)pmap_map(&vaddr, new_end,
344 new_end + vm_page_dump_size, VM_PROT_READ | VM_PROT_WRITE);
345 bzero((void *)vm_page_dump, vm_page_dump_size);
349 * Request that the physical pages underlying the message buffer be
350 * included in a crash dump. Since the message buffer is accessed
351 * through the direct map, they are not automatically included.
353 pa = DMAP_TO_PHYS((vm_offset_t)msgbufp->msg_ptr);
354 last_pa = pa + round_page(msgbufsize);
355 while (pa < last_pa) {
361 * Compute the number of pages of memory that will be available for
362 * use (taking into account the overhead of a page structure per
365 first_page = low_water / PAGE_SIZE;
366 #ifdef VM_PHYSSEG_SPARSE
368 for (i = 0; phys_avail[i + 1] != 0; i += 2)
369 page_range += atop(phys_avail[i + 1] - phys_avail[i]);
370 #elif defined(VM_PHYSSEG_DENSE)
371 page_range = high_water / PAGE_SIZE - first_page;
373 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
378 * Reserve an unmapped guard page to trap access to vm_page_array[-1].
383 * Initialize the mem entry structures now, and put them in the free
386 new_end = trunc_page(end - page_range * sizeof(struct vm_page));
387 mapped = pmap_map(&vaddr, new_end, end,
388 VM_PROT_READ | VM_PROT_WRITE);
389 vm_page_array = (vm_page_t) mapped;
390 #if VM_NRESERVLEVEL > 0
392 * Allocate memory for the reservation management system's data
395 new_end = vm_reserv_startup(&vaddr, new_end, high_water);
397 #if defined(__amd64__) || defined(__mips__)
399 * pmap_map on amd64 and mips can come out of the direct-map, not kvm
400 * like i386, so the pages must be tracked for a crashdump to include
401 * this data. This includes the vm_page_array and the early UMA
404 for (pa = new_end; pa < phys_avail[biggestone + 1]; pa += PAGE_SIZE)
407 phys_avail[biggestone + 1] = new_end;
410 * Clear all of the page structures
412 bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page));
413 for (i = 0; i < page_range; i++)
414 vm_page_array[i].order = VM_NFREEORDER;
415 vm_page_array_size = page_range;
418 * Initialize the physical memory allocator.
423 * Add every available physical page that is not blacklisted to
426 cnt.v_page_count = 0;
427 cnt.v_free_count = 0;
428 list = getenv("vm.blacklist");
429 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
431 last_pa = phys_avail[i + 1];
432 while (pa < last_pa) {
434 vm_page_blacklist_lookup(list, pa))
435 printf("Skipping page with pa 0x%jx\n",
438 vm_phys_add_page(pa);
443 #if VM_NRESERVLEVEL > 0
445 * Initialize the reservation management system.
453 CTASSERT(offsetof(struct vm_page, aflags) % sizeof(uint32_t) == 0);
456 vm_page_aflag_set(vm_page_t m, uint8_t bits)
461 * The PGA_WRITEABLE flag can only be set if the page is managed and
462 * VPO_BUSY. Currently, this flag is only set by pmap_enter().
464 KASSERT((bits & PGA_WRITEABLE) == 0 ||
465 (m->oflags & (VPO_UNMANAGED | VPO_BUSY)) == VPO_BUSY,
466 ("PGA_WRITEABLE and !VPO_BUSY"));
469 * We want to use atomic updates for m->aflags, which is a
470 * byte wide. Not all architectures provide atomic operations
471 * on the single-byte destination. Punt and access the whole
472 * 4-byte word with an atomic update. Parallel non-atomic
473 * updates to the fields included in the update by proximity
474 * are handled properly by atomics.
476 addr = (void *)&m->aflags;
477 MPASS(((uintptr_t)addr & (sizeof(uint32_t) - 1)) == 0);
479 #if BYTE_ORDER == BIG_ENDIAN
482 atomic_set_32(addr, val);
486 vm_page_aflag_clear(vm_page_t m, uint8_t bits)
491 * The PGA_REFERENCED flag can only be cleared if the object
492 * containing the page is locked.
494 KASSERT((bits & PGA_REFERENCED) == 0 || VM_OBJECT_LOCKED(m->object),
495 ("PGA_REFERENCED and !VM_OBJECT_LOCKED"));
498 * See the comment in vm_page_aflag_set().
500 addr = (void *)&m->aflags;
501 MPASS(((uintptr_t)addr & (sizeof(uint32_t) - 1)) == 0);
503 #if BYTE_ORDER == BIG_ENDIAN
506 atomic_clear_32(addr, val);
510 vm_page_reference(vm_page_t m)
513 vm_page_aflag_set(m, PGA_REFERENCED);
517 vm_page_busy(vm_page_t m)
520 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
521 KASSERT((m->oflags & VPO_BUSY) == 0,
522 ("vm_page_busy: page already busy!!!"));
523 m->oflags |= VPO_BUSY;
529 * wakeup anyone waiting for the page.
532 vm_page_flash(vm_page_t m)
535 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
536 if (m->oflags & VPO_WANTED) {
537 m->oflags &= ~VPO_WANTED;
545 * clear the VPO_BUSY flag and wakeup anyone waiting for the
550 vm_page_wakeup(vm_page_t m)
553 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
554 KASSERT(m->oflags & VPO_BUSY, ("vm_page_wakeup: page not busy!!!"));
555 m->oflags &= ~VPO_BUSY;
560 vm_page_io_start(vm_page_t m)
563 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
568 vm_page_io_finish(vm_page_t m)
571 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
572 KASSERT(m->busy > 0, ("vm_page_io_finish: page %p is not busy", m));
579 * Keep page from being freed by the page daemon
580 * much of the same effect as wiring, except much lower
581 * overhead and should be used only for *very* temporary
582 * holding ("wiring").
585 vm_page_hold(vm_page_t mem)
588 vm_page_lock_assert(mem, MA_OWNED);
593 vm_page_unhold(vm_page_t mem)
596 vm_page_lock_assert(mem, MA_OWNED);
598 KASSERT(mem->hold_count >= 0, ("vm_page_unhold: hold count < 0!!!"));
599 if (mem->hold_count == 0 && mem->queue == PQ_HOLD)
600 vm_page_free_toq(mem);
604 * vm_page_unhold_pages:
606 * Unhold each of the pages that is referenced by the given array.
609 vm_page_unhold_pages(vm_page_t *ma, int count)
611 struct mtx *mtx, *new_mtx;
614 for (; count != 0; count--) {
616 * Avoid releasing and reacquiring the same page lock.
618 new_mtx = vm_page_lockptr(*ma);
619 if (mtx != new_mtx) {
633 PHYS_TO_VM_PAGE(vm_paddr_t pa)
637 #ifdef VM_PHYSSEG_SPARSE
638 m = vm_phys_paddr_to_vm_page(pa);
640 m = vm_phys_fictitious_to_vm_page(pa);
642 #elif defined(VM_PHYSSEG_DENSE)
646 if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
647 m = &vm_page_array[pi - first_page];
650 return (vm_phys_fictitious_to_vm_page(pa));
652 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
659 * Create a fictitious page with the specified physical address and
660 * memory attribute. The memory attribute is the only the machine-
661 * dependent aspect of a fictitious page that must be initialized.
664 vm_page_getfake(vm_paddr_t paddr, vm_memattr_t memattr)
668 m = uma_zalloc(fakepg_zone, M_WAITOK | M_ZERO);
669 vm_page_initfake(m, paddr, memattr);
674 vm_page_initfake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
677 if ((m->flags & PG_FICTITIOUS) != 0) {
679 * The page's memattr might have changed since the
680 * previous initialization. Update the pmap to the
685 m->phys_addr = paddr;
687 /* Fictitious pages don't use "segind". */
688 m->flags = PG_FICTITIOUS;
689 /* Fictitious pages don't use "order" or "pool". */
690 m->oflags = VPO_BUSY | VPO_UNMANAGED;
693 pmap_page_set_memattr(m, memattr);
699 * Release a fictitious page.
702 vm_page_putfake(vm_page_t m)
705 KASSERT((m->oflags & VPO_UNMANAGED) != 0, ("managed %p", m));
706 KASSERT((m->flags & PG_FICTITIOUS) != 0,
707 ("vm_page_putfake: bad page %p", m));
708 uma_zfree(fakepg_zone, m);
712 * vm_page_updatefake:
714 * Update the given fictitious page to the specified physical address and
718 vm_page_updatefake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
721 KASSERT((m->flags & PG_FICTITIOUS) != 0,
722 ("vm_page_updatefake: bad page %p", m));
723 m->phys_addr = paddr;
724 pmap_page_set_memattr(m, memattr);
733 vm_page_free(vm_page_t m)
736 m->flags &= ~PG_ZERO;
743 * Free a page to the zerod-pages queue
746 vm_page_free_zero(vm_page_t m)
754 * Unbusy and handle the page queueing for a page from the VOP_GETPAGES()
755 * array which is not the request page.
758 vm_page_readahead_finish(vm_page_t m)
763 * Since the page is not the requested page, whether
764 * it should be activated or deactivated is not
765 * obvious. Empirical results have shown that
766 * deactivating the page is usually the best choice,
767 * unless the page is wanted by another thread.
769 if (m->oflags & VPO_WANTED) {
775 vm_page_deactivate(m);
781 * Free the completely invalid page. Such page state
782 * occurs due to the short read operation which did
783 * not covered our page at all, or in case when a read
795 * Sleep and release the page and page queues locks.
797 * The object containing the given page must be locked.
800 vm_page_sleep(vm_page_t m, const char *msg)
803 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
804 if (mtx_owned(&vm_page_queue_mtx))
805 vm_page_unlock_queues();
806 if (mtx_owned(vm_page_lockptr(m)))
810 * It's possible that while we sleep, the page will get
811 * unbusied and freed. If we are holding the object
812 * lock, we will assume we hold a reference to the object
813 * such that even if m->object changes, we can re-lock
816 m->oflags |= VPO_WANTED;
817 msleep(m, VM_OBJECT_MTX(m->object), PVM, msg, 0);
823 * Set all bits in the page's dirty field.
825 * The object containing the specified page must be locked if the
826 * call is made from the machine-independent layer.
828 * See vm_page_clear_dirty_mask().
831 vm_page_dirty(vm_page_t m)
834 KASSERT((m->flags & PG_CACHED) == 0,
835 ("vm_page_dirty: page in cache!"));
836 KASSERT(!VM_PAGE_IS_FREE(m),
837 ("vm_page_dirty: page is free!"));
838 KASSERT(m->valid == VM_PAGE_BITS_ALL,
839 ("vm_page_dirty: page is invalid!"));
840 m->dirty = VM_PAGE_BITS_ALL;
846 * Implements Sleator and Tarjan's top-down splay algorithm. Returns
847 * the vm_page containing the given pindex. If, however, that
848 * pindex is not found in the vm_object, returns a vm_page that is
849 * adjacent to the pindex, coming before or after it.
852 vm_page_splay(vm_pindex_t pindex, vm_page_t root)
854 struct vm_page dummy;
855 vm_page_t lefttreemax, righttreemin, y;
859 lefttreemax = righttreemin = &dummy;
861 if (pindex < root->pindex) {
862 if ((y = root->left) == NULL)
864 if (pindex < y->pindex) {
866 root->left = y->right;
869 if ((y = root->left) == NULL)
872 /* Link into the new root's right tree. */
873 righttreemin->left = root;
875 } else if (pindex > root->pindex) {
876 if ((y = root->right) == NULL)
878 if (pindex > y->pindex) {
880 root->right = y->left;
883 if ((y = root->right) == NULL)
886 /* Link into the new root's left tree. */
887 lefttreemax->right = root;
892 /* Assemble the new root. */
893 lefttreemax->right = root->left;
894 righttreemin->left = root->right;
895 root->left = dummy.right;
896 root->right = dummy.left;
901 * vm_page_insert: [ internal use only ]
903 * Inserts the given mem entry into the object and object list.
905 * The pagetables are not updated but will presumably fault the page
906 * in if necessary, or if a kernel page the caller will at some point
907 * enter the page into the kernel's pmap. We are not allowed to sleep
908 * here so we *can't* do this anyway.
910 * The object must be locked.
913 vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex)
917 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
918 if (m->object != NULL)
919 panic("vm_page_insert: page already inserted");
922 * Record the object/offset pair in this page
928 * Now link into the object's ordered list of backed pages.
934 TAILQ_INSERT_TAIL(&object->memq, m, listq);
936 root = vm_page_splay(pindex, root);
937 if (pindex < root->pindex) {
938 m->left = root->left;
941 TAILQ_INSERT_BEFORE(root, m, listq);
942 } else if (pindex == root->pindex)
943 panic("vm_page_insert: offset already allocated");
945 m->right = root->right;
948 TAILQ_INSERT_AFTER(&object->memq, root, m, listq);
954 * Show that the object has one more resident page.
956 object->resident_page_count++;
959 * Hold the vnode until the last page is released.
961 if (object->resident_page_count == 1 && object->type == OBJT_VNODE)
962 vhold(object->handle);
965 * Since we are inserting a new and possibly dirty page,
966 * update the object's OBJ_MIGHTBEDIRTY flag.
968 if (pmap_page_is_write_mapped(m))
969 vm_object_set_writeable_dirty(object);
975 * Removes the given mem entry from the object/offset-page
976 * table and the object page list, but do not invalidate/terminate
979 * The underlying pmap entry (if any) is NOT removed here.
981 * The object must be locked. The page must be locked if it is managed.
984 vm_page_remove(vm_page_t m)
987 vm_page_t next, prev, root;
989 if ((m->oflags & VPO_UNMANAGED) == 0)
990 vm_page_lock_assert(m, MA_OWNED);
991 if ((object = m->object) == NULL)
993 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
994 if (m->oflags & VPO_BUSY) {
995 m->oflags &= ~VPO_BUSY;
1000 * Now remove from the object's list of backed pages.
1002 if ((next = TAILQ_NEXT(m, listq)) != NULL && next->left == m) {
1004 * Since the page's successor in the list is also its parent
1005 * in the tree, its right subtree must be empty.
1007 next->left = m->left;
1008 KASSERT(m->right == NULL,
1009 ("vm_page_remove: page %p has right child", m));
1010 } else if ((prev = TAILQ_PREV(m, pglist, listq)) != NULL &&
1013 * Since the page's predecessor in the list is also its parent
1014 * in the tree, its left subtree must be empty.
1016 KASSERT(m->left == NULL,
1017 ("vm_page_remove: page %p has left child", m));
1018 prev->right = m->right;
1020 if (m != object->root)
1021 vm_page_splay(m->pindex, object->root);
1022 if (m->left == NULL)
1024 else if (m->right == NULL)
1028 * Move the page's successor to the root, because
1029 * pages are usually removed in ascending order.
1031 if (m->right != next)
1032 vm_page_splay(m->pindex, m->right);
1033 next->left = m->left;
1036 object->root = root;
1038 TAILQ_REMOVE(&object->memq, m, listq);
1041 * And show that the object has one fewer resident page.
1043 object->resident_page_count--;
1046 * The vnode may now be recycled.
1048 if (object->resident_page_count == 0 && object->type == OBJT_VNODE)
1049 vdrop(object->handle);
1057 * Returns the page associated with the object/offset
1058 * pair specified; if none is found, NULL is returned.
1060 * The object must be locked.
1063 vm_page_lookup(vm_object_t object, vm_pindex_t pindex)
1067 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1068 if ((m = object->root) != NULL && m->pindex != pindex) {
1069 m = vm_page_splay(pindex, m);
1070 if ((object->root = m)->pindex != pindex)
1077 * vm_page_find_least:
1079 * Returns the page associated with the object with least pindex
1080 * greater than or equal to the parameter pindex, or NULL.
1082 * The object must be locked.
1085 vm_page_find_least(vm_object_t object, vm_pindex_t pindex)
1089 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1090 if ((m = TAILQ_FIRST(&object->memq)) != NULL) {
1091 if (m->pindex < pindex) {
1092 m = vm_page_splay(pindex, object->root);
1093 if ((object->root = m)->pindex < pindex)
1094 m = TAILQ_NEXT(m, listq);
1101 * Returns the given page's successor (by pindex) within the object if it is
1102 * resident; if none is found, NULL is returned.
1104 * The object must be locked.
1107 vm_page_next(vm_page_t m)
1111 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1112 if ((next = TAILQ_NEXT(m, listq)) != NULL &&
1113 next->pindex != m->pindex + 1)
1119 * Returns the given page's predecessor (by pindex) within the object if it is
1120 * resident; if none is found, NULL is returned.
1122 * The object must be locked.
1125 vm_page_prev(vm_page_t m)
1129 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1130 if ((prev = TAILQ_PREV(m, pglist, listq)) != NULL &&
1131 prev->pindex != m->pindex - 1)
1139 * Move the given memory entry from its
1140 * current object to the specified target object/offset.
1142 * Note: swap associated with the page must be invalidated by the move. We
1143 * have to do this for several reasons: (1) we aren't freeing the
1144 * page, (2) we are dirtying the page, (3) the VM system is probably
1145 * moving the page from object A to B, and will then later move
1146 * the backing store from A to B and we can't have a conflict.
1148 * Note: we *always* dirty the page. It is necessary both for the
1149 * fact that we moved it, and because we may be invalidating
1150 * swap. If the page is on the cache, we have to deactivate it
1151 * or vm_page_dirty() will panic. Dirty pages are not allowed
1154 * The objects must be locked. The page must be locked if it is managed.
1157 vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex)
1161 vm_page_insert(m, new_object, new_pindex);
1166 * Convert all of the given object's cached pages that have a
1167 * pindex within the given range into free pages. If the value
1168 * zero is given for "end", then the range's upper bound is
1169 * infinity. If the given object is backed by a vnode and it
1170 * transitions from having one or more cached pages to none, the
1171 * vnode's hold count is reduced.
1174 vm_page_cache_free(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1176 vm_page_t m, m_next;
1179 mtx_lock(&vm_page_queue_free_mtx);
1180 if (__predict_false(object->cache == NULL)) {
1181 mtx_unlock(&vm_page_queue_free_mtx);
1184 m = object->cache = vm_page_splay(start, object->cache);
1185 if (m->pindex < start) {
1186 if (m->right == NULL)
1189 m_next = vm_page_splay(start, m->right);
1192 m = object->cache = m_next;
1197 * At this point, "m" is either (1) a reference to the page
1198 * with the least pindex that is greater than or equal to
1199 * "start" or (2) NULL.
1201 for (; m != NULL && (m->pindex < end || end == 0); m = m_next) {
1203 * Find "m"'s successor and remove "m" from the
1206 if (m->right == NULL) {
1207 object->cache = m->left;
1210 m_next = vm_page_splay(start, m->right);
1211 m_next->left = m->left;
1212 object->cache = m_next;
1214 /* Convert "m" to a free page. */
1217 /* Clear PG_CACHED and set PG_FREE. */
1218 m->flags ^= PG_CACHED | PG_FREE;
1219 KASSERT((m->flags & (PG_CACHED | PG_FREE)) == PG_FREE,
1220 ("vm_page_cache_free: page %p has inconsistent flags", m));
1221 cnt.v_cache_count--;
1224 empty = object->cache == NULL;
1225 mtx_unlock(&vm_page_queue_free_mtx);
1226 if (object->type == OBJT_VNODE && empty)
1227 vdrop(object->handle);
1231 * Returns the cached page that is associated with the given
1232 * object and offset. If, however, none exists, returns NULL.
1234 * The free page queue must be locked.
1236 static inline vm_page_t
1237 vm_page_cache_lookup(vm_object_t object, vm_pindex_t pindex)
1241 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1242 if ((m = object->cache) != NULL && m->pindex != pindex) {
1243 m = vm_page_splay(pindex, m);
1244 if ((object->cache = m)->pindex != pindex)
1251 * Remove the given cached page from its containing object's
1252 * collection of cached pages.
1254 * The free page queue must be locked.
1257 vm_page_cache_remove(vm_page_t m)
1262 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1263 KASSERT((m->flags & PG_CACHED) != 0,
1264 ("vm_page_cache_remove: page %p is not cached", m));
1266 if (m != object->cache) {
1267 root = vm_page_splay(m->pindex, object->cache);
1269 ("vm_page_cache_remove: page %p is not cached in object %p",
1272 if (m->left == NULL)
1274 else if (m->right == NULL)
1277 root = vm_page_splay(m->pindex, m->left);
1278 root->right = m->right;
1280 object->cache = root;
1282 cnt.v_cache_count--;
1286 * Transfer all of the cached pages with offset greater than or
1287 * equal to 'offidxstart' from the original object's cache to the
1288 * new object's cache. However, any cached pages with offset
1289 * greater than or equal to the new object's size are kept in the
1290 * original object. Initially, the new object's cache must be
1291 * empty. Offset 'offidxstart' in the original object must
1292 * correspond to offset zero in the new object.
1294 * The new object must be locked.
1297 vm_page_cache_transfer(vm_object_t orig_object, vm_pindex_t offidxstart,
1298 vm_object_t new_object)
1300 vm_page_t m, m_next;
1303 * Insertion into an object's collection of cached pages
1304 * requires the object to be locked. In contrast, removal does
1307 VM_OBJECT_LOCK_ASSERT(new_object, MA_OWNED);
1308 KASSERT(new_object->cache == NULL,
1309 ("vm_page_cache_transfer: object %p has cached pages",
1311 mtx_lock(&vm_page_queue_free_mtx);
1312 if ((m = orig_object->cache) != NULL) {
1314 * Transfer all of the pages with offset greater than or
1315 * equal to 'offidxstart' from the original object's
1316 * cache to the new object's cache.
1318 m = vm_page_splay(offidxstart, m);
1319 if (m->pindex < offidxstart) {
1320 orig_object->cache = m;
1321 new_object->cache = m->right;
1324 orig_object->cache = m->left;
1325 new_object->cache = m;
1328 while ((m = new_object->cache) != NULL) {
1329 if ((m->pindex - offidxstart) >= new_object->size) {
1331 * Return all of the cached pages with
1332 * offset greater than or equal to the
1333 * new object's size to the original
1336 new_object->cache = m->left;
1337 m->left = orig_object->cache;
1338 orig_object->cache = m;
1341 m_next = vm_page_splay(m->pindex, m->right);
1342 /* Update the page's object and offset. */
1343 m->object = new_object;
1344 m->pindex -= offidxstart;
1349 new_object->cache = m_next;
1351 KASSERT(new_object->cache == NULL ||
1352 new_object->type == OBJT_SWAP,
1353 ("vm_page_cache_transfer: object %p's type is incompatible"
1354 " with cached pages", new_object));
1356 mtx_unlock(&vm_page_queue_free_mtx);
1360 * Returns TRUE if a cached page is associated with the given object and
1361 * offset, and FALSE otherwise.
1363 * The object must be locked.
1366 vm_page_is_cached(vm_object_t object, vm_pindex_t pindex)
1371 * Insertion into an object's collection of cached pages requires the
1372 * object to be locked. Therefore, if the object is locked and the
1373 * object's collection is empty, there is no need to acquire the free
1374 * page queues lock in order to prove that the specified page doesn't
1377 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1378 if (object->cache == NULL)
1380 mtx_lock(&vm_page_queue_free_mtx);
1381 m = vm_page_cache_lookup(object, pindex);
1382 mtx_unlock(&vm_page_queue_free_mtx);
1389 * Allocate and return a memory cell associated
1390 * with this VM object/offset pair.
1392 * The caller must always specify an allocation class.
1394 * allocation classes:
1395 * VM_ALLOC_NORMAL normal process request
1396 * VM_ALLOC_SYSTEM system *really* needs a page
1397 * VM_ALLOC_INTERRUPT interrupt time request
1399 * optional allocation flags:
1400 * VM_ALLOC_ZERO prefer a zeroed page
1401 * VM_ALLOC_WIRED wire the allocated page
1402 * VM_ALLOC_NOOBJ page is not associated with a vm object
1403 * VM_ALLOC_NOBUSY do not set the page busy
1404 * VM_ALLOC_IFCACHED return page only if it is cached
1405 * VM_ALLOC_IFNOTCACHED return NULL, do not reactivate if the page
1408 * This routine may not sleep.
1411 vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req)
1413 struct vnode *vp = NULL;
1414 vm_object_t m_object;
1416 int flags, page_req;
1418 if ((req & VM_ALLOC_NOOBJ) == 0) {
1419 KASSERT(object != NULL,
1420 ("vm_page_alloc: NULL object."));
1421 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1424 page_req = req & VM_ALLOC_CLASS_MASK;
1427 * The pager is allowed to eat deeper into the free page list.
1429 if ((curproc == pageproc) && (page_req != VM_ALLOC_INTERRUPT))
1430 page_req = VM_ALLOC_SYSTEM;
1432 mtx_lock(&vm_page_queue_free_mtx);
1433 if (cnt.v_free_count + cnt.v_cache_count > cnt.v_free_reserved ||
1434 (page_req == VM_ALLOC_SYSTEM &&
1435 cnt.v_free_count + cnt.v_cache_count > cnt.v_interrupt_free_min) ||
1436 (page_req == VM_ALLOC_INTERRUPT &&
1437 cnt.v_free_count + cnt.v_cache_count > 0)) {
1439 * Allocate from the free queue if the number of free pages
1440 * exceeds the minimum for the request class.
1442 if (object != NULL &&
1443 (m = vm_page_cache_lookup(object, pindex)) != NULL) {
1444 if ((req & VM_ALLOC_IFNOTCACHED) != 0) {
1445 mtx_unlock(&vm_page_queue_free_mtx);
1448 if (vm_phys_unfree_page(m))
1449 vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m, 0);
1450 #if VM_NRESERVLEVEL > 0
1451 else if (!vm_reserv_reactivate_page(m))
1455 panic("vm_page_alloc: cache page %p is missing"
1456 " from the free queue", m);
1457 } else if ((req & VM_ALLOC_IFCACHED) != 0) {
1458 mtx_unlock(&vm_page_queue_free_mtx);
1460 #if VM_NRESERVLEVEL > 0
1461 } else if (object == NULL || object->type == OBJT_DEVICE ||
1462 object->type == OBJT_SG ||
1463 (object->flags & OBJ_COLORED) == 0 ||
1464 (m = vm_reserv_alloc_page(object, pindex)) == NULL) {
1468 m = vm_phys_alloc_pages(object != NULL ?
1469 VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT, 0);
1470 #if VM_NRESERVLEVEL > 0
1471 if (m == NULL && vm_reserv_reclaim_inactive()) {
1472 m = vm_phys_alloc_pages(object != NULL ?
1473 VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT,
1480 * Not allocatable, give up.
1482 mtx_unlock(&vm_page_queue_free_mtx);
1483 atomic_add_int(&vm_pageout_deficit,
1484 MAX((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1));
1485 pagedaemon_wakeup();
1490 * At this point we had better have found a good page.
1493 KASSERT(m != NULL, ("vm_page_alloc: missing page"));
1494 KASSERT(m->queue == PQ_NONE,
1495 ("vm_page_alloc: page %p has unexpected queue %d", m, m->queue));
1496 KASSERT(m->wire_count == 0, ("vm_page_alloc: page %p is wired", m));
1497 KASSERT(m->hold_count == 0, ("vm_page_alloc: page %p is held", m));
1498 KASSERT(m->busy == 0, ("vm_page_alloc: page %p is busy", m));
1499 KASSERT(m->dirty == 0, ("vm_page_alloc: page %p is dirty", m));
1500 KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT,
1501 ("vm_page_alloc: page %p has unexpected memattr %d", m,
1502 pmap_page_get_memattr(m)));
1503 if ((m->flags & PG_CACHED) != 0) {
1504 KASSERT(m->valid != 0,
1505 ("vm_page_alloc: cached page %p is invalid", m));
1506 if (m->object == object && m->pindex == pindex)
1507 cnt.v_reactivated++;
1510 m_object = m->object;
1511 vm_page_cache_remove(m);
1512 if (m_object->type == OBJT_VNODE && m_object->cache == NULL)
1513 vp = m_object->handle;
1515 KASSERT(VM_PAGE_IS_FREE(m),
1516 ("vm_page_alloc: page %p is not free", m));
1517 KASSERT(m->valid == 0,
1518 ("vm_page_alloc: free page %p is valid", m));
1523 * Only the PG_ZERO flag is inherited. The PG_CACHED or PG_FREE flag
1524 * must be cleared before the free page queues lock is released.
1527 if (req & VM_ALLOC_NODUMP)
1529 if (m->flags & PG_ZERO) {
1530 vm_page_zero_count--;
1531 if (req & VM_ALLOC_ZERO)
1535 mtx_unlock(&vm_page_queue_free_mtx);
1537 if (object == NULL || object->type == OBJT_PHYS)
1538 m->oflags = VPO_UNMANAGED;
1541 if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ)) == 0)
1542 m->oflags |= VPO_BUSY;
1543 if (req & VM_ALLOC_WIRED) {
1545 * The page lock is not required for wiring a page until that
1546 * page is inserted into the object.
1548 atomic_add_int(&cnt.v_wire_count, 1);
1553 if (object != NULL) {
1554 /* Ignore device objects; the pager sets "memattr" for them. */
1555 if (object->memattr != VM_MEMATTR_DEFAULT &&
1556 object->type != OBJT_DEVICE && object->type != OBJT_SG)
1557 pmap_page_set_memattr(m, object->memattr);
1558 vm_page_insert(m, object, pindex);
1563 * The following call to vdrop() must come after the above call
1564 * to vm_page_insert() in case both affect the same object and
1565 * vnode. Otherwise, the affected vnode's hold count could
1566 * temporarily become zero.
1572 * Don't wakeup too often - wakeup the pageout daemon when
1573 * we would be nearly out of memory.
1575 if (vm_paging_needed())
1576 pagedaemon_wakeup();
1582 * Initialize a page that has been freshly dequeued from a freelist.
1583 * The caller has to drop the vnode returned, if it is not NULL.
1585 * To be called with vm_page_queue_free_mtx held.
1588 vm_page_alloc_init(vm_page_t m)
1591 vm_object_t m_object;
1593 KASSERT(m->queue == PQ_NONE,
1594 ("vm_page_alloc_init: page %p has unexpected queue %d",
1596 KASSERT(m->wire_count == 0,
1597 ("vm_page_alloc_init: page %p is wired", m));
1598 KASSERT(m->hold_count == 0,
1599 ("vm_page_alloc_init: page %p is held", m));
1600 KASSERT(m->busy == 0,
1601 ("vm_page_alloc_init: page %p is busy", m));
1602 KASSERT(m->dirty == 0,
1603 ("vm_page_alloc_init: page %p is dirty", m));
1604 KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT,
1605 ("vm_page_alloc_init: page %p has unexpected memattr %d",
1606 m, pmap_page_get_memattr(m)));
1607 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1609 if ((m->flags & PG_CACHED) != 0) {
1611 m_object = m->object;
1612 vm_page_cache_remove(m);
1613 if (m_object->type == OBJT_VNODE &&
1614 m_object->cache == NULL)
1615 drop = m_object->handle;
1617 KASSERT(VM_PAGE_IS_FREE(m),
1618 ("vm_page_alloc_init: page %p is not free", m));
1619 KASSERT(m->valid == 0,
1620 ("vm_page_alloc_init: free page %p is valid", m));
1623 if (m->flags & PG_ZERO)
1624 vm_page_zero_count--;
1625 /* Don't clear the PG_ZERO flag; we'll need it later. */
1626 m->flags &= PG_ZERO;
1628 m->oflags = VPO_UNMANAGED;
1629 /* Unmanaged pages don't use "act_count". */
1634 * vm_page_alloc_freelist:
1636 * Allocate a page from the specified freelist.
1637 * Only the ALLOC_CLASS values in req are honored, other request flags
1641 vm_page_alloc_freelist(int flind, int req)
1648 page_req = req & VM_ALLOC_CLASS_MASK;
1649 mtx_lock(&vm_page_queue_free_mtx);
1651 * Do not allocate reserved pages unless the req has asked for it.
1653 if (cnt.v_free_count + cnt.v_cache_count > cnt.v_free_reserved ||
1654 (page_req == VM_ALLOC_SYSTEM &&
1655 cnt.v_free_count + cnt.v_cache_count > cnt.v_interrupt_free_min) ||
1656 (page_req == VM_ALLOC_INTERRUPT &&
1657 cnt.v_free_count + cnt.v_cache_count > 0)) {
1658 m = vm_phys_alloc_freelist_pages(flind, VM_FREEPOOL_DIRECT, 0);
1661 mtx_unlock(&vm_page_queue_free_mtx);
1664 drop = vm_page_alloc_init(m);
1665 mtx_unlock(&vm_page_queue_free_mtx);
1672 * vm_wait: (also see VM_WAIT macro)
1674 * Sleep until free pages are available for allocation.
1675 * - Called in various places before memory allocations.
1681 mtx_lock(&vm_page_queue_free_mtx);
1682 if (curproc == pageproc) {
1683 vm_pageout_pages_needed = 1;
1684 msleep(&vm_pageout_pages_needed, &vm_page_queue_free_mtx,
1685 PDROP | PSWP, "VMWait", 0);
1687 if (!vm_pages_needed) {
1688 vm_pages_needed = 1;
1689 wakeup(&vm_pages_needed);
1691 msleep(&cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PVM,
1697 * vm_waitpfault: (also see VM_WAITPFAULT macro)
1699 * Sleep until free pages are available for allocation.
1700 * - Called only in vm_fault so that processes page faulting
1701 * can be easily tracked.
1702 * - Sleeps at a lower priority than vm_wait() so that vm_wait()ing
1703 * processes will be able to grab memory first. Do not change
1704 * this balance without careful testing first.
1710 mtx_lock(&vm_page_queue_free_mtx);
1711 if (!vm_pages_needed) {
1712 vm_pages_needed = 1;
1713 wakeup(&vm_pages_needed);
1715 msleep(&cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PUSER,
1722 * Move the given page to the tail of its present page queue.
1724 * The page queues must be locked.
1727 vm_page_requeue(vm_page_t m)
1729 struct vpgqueues *vpq;
1732 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1734 KASSERT(queue != PQ_NONE,
1735 ("vm_page_requeue: page %p is not queued", m));
1736 vpq = &vm_page_queues[queue];
1737 TAILQ_REMOVE(&vpq->pl, m, pageq);
1738 TAILQ_INSERT_TAIL(&vpq->pl, m, pageq);
1742 * vm_page_queue_remove:
1744 * Remove the given page from the specified queue.
1746 * The page and page queues must be locked.
1748 static __inline void
1749 vm_page_queue_remove(int queue, vm_page_t m)
1751 struct vpgqueues *pq;
1753 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1754 vm_page_lock_assert(m, MA_OWNED);
1755 pq = &vm_page_queues[queue];
1756 TAILQ_REMOVE(&pq->pl, m, pageq);
1763 * Remove a page from its queue.
1765 * The given page must be locked.
1768 vm_pageq_remove(vm_page_t m)
1772 vm_page_lock_assert(m, MA_OWNED);
1773 if ((queue = m->queue) != PQ_NONE) {
1774 vm_page_lock_queues();
1776 vm_page_queue_remove(queue, m);
1777 vm_page_unlock_queues();
1784 * Add the given page to the specified queue.
1786 * The page queues must be locked.
1789 vm_page_enqueue(int queue, vm_page_t m)
1791 struct vpgqueues *vpq;
1793 vpq = &vm_page_queues[queue];
1795 TAILQ_INSERT_TAIL(&vpq->pl, m, pageq);
1802 * Put the specified page on the active list (if appropriate).
1803 * Ensure that act_count is at least ACT_INIT but do not otherwise
1806 * The page must be locked.
1809 vm_page_activate(vm_page_t m)
1813 vm_page_lock_assert(m, MA_OWNED);
1814 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1815 if ((queue = m->queue) != PQ_ACTIVE) {
1816 if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) {
1817 if (m->act_count < ACT_INIT)
1818 m->act_count = ACT_INIT;
1819 vm_page_lock_queues();
1820 if (queue != PQ_NONE)
1821 vm_page_queue_remove(queue, m);
1822 vm_page_enqueue(PQ_ACTIVE, m);
1823 vm_page_unlock_queues();
1825 KASSERT(queue == PQ_NONE,
1826 ("vm_page_activate: wired page %p is queued", m));
1828 if (m->act_count < ACT_INIT)
1829 m->act_count = ACT_INIT;
1834 * vm_page_free_wakeup:
1836 * Helper routine for vm_page_free_toq() and vm_page_cache(). This
1837 * routine is called when a page has been added to the cache or free
1840 * The page queues must be locked.
1843 vm_page_free_wakeup(void)
1846 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1848 * if pageout daemon needs pages, then tell it that there are
1851 if (vm_pageout_pages_needed &&
1852 cnt.v_cache_count + cnt.v_free_count >= cnt.v_pageout_free_min) {
1853 wakeup(&vm_pageout_pages_needed);
1854 vm_pageout_pages_needed = 0;
1857 * wakeup processes that are waiting on memory if we hit a
1858 * high water mark. And wakeup scheduler process if we have
1859 * lots of memory. this process will swapin processes.
1861 if (vm_pages_needed && !vm_page_count_min()) {
1862 vm_pages_needed = 0;
1863 wakeup(&cnt.v_free_count);
1870 * Returns the given page to the free list,
1871 * disassociating it with any VM object.
1873 * The object must be locked. The page must be locked if it is managed.
1876 vm_page_free_toq(vm_page_t m)
1879 if ((m->oflags & VPO_UNMANAGED) == 0) {
1880 vm_page_lock_assert(m, MA_OWNED);
1881 KASSERT(!pmap_page_is_mapped(m),
1882 ("vm_page_free_toq: freeing mapped page %p", m));
1884 PCPU_INC(cnt.v_tfree);
1886 if (VM_PAGE_IS_FREE(m))
1887 panic("vm_page_free: freeing free page %p", m);
1888 else if (m->busy != 0)
1889 panic("vm_page_free: freeing busy page %p", m);
1892 * Unqueue, then remove page. Note that we cannot destroy
1893 * the page here because we do not want to call the pager's
1894 * callback routine until after we've put the page on the
1895 * appropriate free queue.
1897 if ((m->oflags & VPO_UNMANAGED) == 0)
1902 * If fictitious remove object association and
1903 * return, otherwise delay object association removal.
1905 if ((m->flags & PG_FICTITIOUS) != 0) {
1912 if (m->wire_count != 0)
1913 panic("vm_page_free: freeing wired page %p", m);
1914 if (m->hold_count != 0) {
1915 m->flags &= ~PG_ZERO;
1916 vm_page_lock_queues();
1917 vm_page_enqueue(PQ_HOLD, m);
1918 vm_page_unlock_queues();
1921 * Restore the default memory attribute to the page.
1923 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT)
1924 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT);
1927 * Insert the page into the physical memory allocator's
1928 * cache/free page queues.
1930 mtx_lock(&vm_page_queue_free_mtx);
1931 m->flags |= PG_FREE;
1933 #if VM_NRESERVLEVEL > 0
1934 if (!vm_reserv_free_page(m))
1938 vm_phys_free_pages(m, 0);
1939 if ((m->flags & PG_ZERO) != 0)
1940 ++vm_page_zero_count;
1942 vm_page_zero_idle_wakeup();
1943 vm_page_free_wakeup();
1944 mtx_unlock(&vm_page_queue_free_mtx);
1951 * Mark this page as wired down by yet
1952 * another map, removing it from paging queues
1955 * If the page is fictitious, then its wire count must remain one.
1957 * The page must be locked.
1960 vm_page_wire(vm_page_t m)
1964 * Only bump the wire statistics if the page is not already wired,
1965 * and only unqueue the page if it is on some queue (if it is unmanaged
1966 * it is already off the queues).
1968 vm_page_lock_assert(m, MA_OWNED);
1969 if ((m->flags & PG_FICTITIOUS) != 0) {
1970 KASSERT(m->wire_count == 1,
1971 ("vm_page_wire: fictitious page %p's wire count isn't one",
1975 if (m->wire_count == 0) {
1976 if ((m->oflags & VPO_UNMANAGED) == 0)
1978 atomic_add_int(&cnt.v_wire_count, 1);
1981 KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m));
1987 * Release one wiring of the specified page, potentially enabling it to be
1988 * paged again. If paging is enabled, then the value of the parameter
1989 * "activate" determines to which queue the page is added. If "activate" is
1990 * non-zero, then the page is added to the active queue. Otherwise, it is
1991 * added to the inactive queue.
1993 * However, unless the page belongs to an object, it is not enqueued because
1994 * it cannot be paged out.
1996 * If a page is fictitious, then its wire count must alway be one.
1998 * A managed page must be locked.
2001 vm_page_unwire(vm_page_t m, int activate)
2004 if ((m->oflags & VPO_UNMANAGED) == 0)
2005 vm_page_lock_assert(m, MA_OWNED);
2006 if ((m->flags & PG_FICTITIOUS) != 0) {
2007 KASSERT(m->wire_count == 1,
2008 ("vm_page_unwire: fictitious page %p's wire count isn't one", m));
2011 if (m->wire_count > 0) {
2013 if (m->wire_count == 0) {
2014 atomic_subtract_int(&cnt.v_wire_count, 1);
2015 if ((m->oflags & VPO_UNMANAGED) != 0 ||
2019 m->flags &= ~PG_WINATCFLS;
2020 vm_page_lock_queues();
2021 vm_page_enqueue(activate ? PQ_ACTIVE : PQ_INACTIVE, m);
2022 vm_page_unlock_queues();
2025 panic("vm_page_unwire: page %p's wire count is zero", m);
2029 * Move the specified page to the inactive queue.
2031 * Many pages placed on the inactive queue should actually go
2032 * into the cache, but it is difficult to figure out which. What
2033 * we do instead, if the inactive target is well met, is to put
2034 * clean pages at the head of the inactive queue instead of the tail.
2035 * This will cause them to be moved to the cache more quickly and
2036 * if not actively re-referenced, reclaimed more quickly. If we just
2037 * stick these pages at the end of the inactive queue, heavy filesystem
2038 * meta-data accesses can cause an unnecessary paging load on memory bound
2039 * processes. This optimization causes one-time-use metadata to be
2040 * reused more quickly.
2042 * Normally athead is 0 resulting in LRU operation. athead is set
2043 * to 1 if we want this page to be 'as if it were placed in the cache',
2044 * except without unmapping it from the process address space.
2046 * The page must be locked.
2049 _vm_page_deactivate(vm_page_t m, int athead)
2053 vm_page_lock_assert(m, MA_OWNED);
2056 * Ignore if already inactive.
2058 if ((queue = m->queue) == PQ_INACTIVE)
2060 if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) {
2061 m->flags &= ~PG_WINATCFLS;
2062 vm_page_lock_queues();
2063 if (queue != PQ_NONE)
2064 vm_page_queue_remove(queue, m);
2066 TAILQ_INSERT_HEAD(&vm_page_queues[PQ_INACTIVE].pl, m,
2069 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m,
2071 m->queue = PQ_INACTIVE;
2072 cnt.v_inactive_count++;
2073 vm_page_unlock_queues();
2078 * Move the specified page to the inactive queue.
2080 * The page must be locked.
2083 vm_page_deactivate(vm_page_t m)
2086 _vm_page_deactivate(m, 0);
2090 * vm_page_try_to_cache:
2092 * Returns 0 on failure, 1 on success
2095 vm_page_try_to_cache(vm_page_t m)
2098 vm_page_lock_assert(m, MA_OWNED);
2099 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2100 if (m->dirty || m->hold_count || m->busy || m->wire_count ||
2101 (m->oflags & (VPO_BUSY | VPO_UNMANAGED)) != 0)
2111 * vm_page_try_to_free()
2113 * Attempt to free the page. If we cannot free it, we do nothing.
2114 * 1 is returned on success, 0 on failure.
2117 vm_page_try_to_free(vm_page_t m)
2120 vm_page_lock_assert(m, MA_OWNED);
2121 if (m->object != NULL)
2122 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2123 if (m->dirty || m->hold_count || m->busy || m->wire_count ||
2124 (m->oflags & (VPO_BUSY | VPO_UNMANAGED)) != 0)
2136 * Put the specified page onto the page cache queue (if appropriate).
2138 * The object and page must be locked.
2141 vm_page_cache(vm_page_t m)
2144 vm_page_t next, prev, root;
2146 vm_page_lock_assert(m, MA_OWNED);
2148 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2149 if ((m->oflags & (VPO_UNMANAGED | VPO_BUSY)) || m->busy ||
2150 m->hold_count || m->wire_count)
2151 panic("vm_page_cache: attempting to cache busy page");
2154 panic("vm_page_cache: page %p is dirty", m);
2155 if (m->valid == 0 || object->type == OBJT_DEFAULT ||
2156 (object->type == OBJT_SWAP &&
2157 !vm_pager_has_page(object, m->pindex, NULL, NULL))) {
2159 * Hypothesis: A cache-elgible page belonging to a
2160 * default object or swap object but without a backing
2161 * store must be zero filled.
2166 KASSERT((m->flags & PG_CACHED) == 0,
2167 ("vm_page_cache: page %p is already cached", m));
2168 PCPU_INC(cnt.v_tcached);
2171 * Remove the page from the paging queues.
2176 * Remove the page from the object's collection of resident
2179 if ((next = TAILQ_NEXT(m, listq)) != NULL && next->left == m) {
2181 * Since the page's successor in the list is also its parent
2182 * in the tree, its right subtree must be empty.
2184 next->left = m->left;
2185 KASSERT(m->right == NULL,
2186 ("vm_page_cache: page %p has right child", m));
2187 } else if ((prev = TAILQ_PREV(m, pglist, listq)) != NULL &&
2190 * Since the page's predecessor in the list is also its parent
2191 * in the tree, its left subtree must be empty.
2193 KASSERT(m->left == NULL,
2194 ("vm_page_cache: page %p has left child", m));
2195 prev->right = m->right;
2197 if (m != object->root)
2198 vm_page_splay(m->pindex, object->root);
2199 if (m->left == NULL)
2201 else if (m->right == NULL)
2205 * Move the page's successor to the root, because
2206 * pages are usually removed in ascending order.
2208 if (m->right != next)
2209 vm_page_splay(m->pindex, m->right);
2210 next->left = m->left;
2213 object->root = root;
2215 TAILQ_REMOVE(&object->memq, m, listq);
2216 object->resident_page_count--;
2219 * Restore the default memory attribute to the page.
2221 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT)
2222 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT);
2225 * Insert the page into the object's collection of cached pages
2226 * and the physical memory allocator's cache/free page queues.
2228 m->flags &= ~PG_ZERO;
2229 mtx_lock(&vm_page_queue_free_mtx);
2230 m->flags |= PG_CACHED;
2231 cnt.v_cache_count++;
2232 root = object->cache;
2237 root = vm_page_splay(m->pindex, root);
2238 if (m->pindex < root->pindex) {
2239 m->left = root->left;
2242 } else if (__predict_false(m->pindex == root->pindex))
2243 panic("vm_page_cache: offset already cached");
2245 m->right = root->right;
2251 #if VM_NRESERVLEVEL > 0
2252 if (!vm_reserv_free_page(m)) {
2256 vm_phys_set_pool(VM_FREEPOOL_CACHE, m, 0);
2257 vm_phys_free_pages(m, 0);
2259 vm_page_free_wakeup();
2260 mtx_unlock(&vm_page_queue_free_mtx);
2263 * Increment the vnode's hold count if this is the object's only
2264 * cached page. Decrement the vnode's hold count if this was
2265 * the object's only resident page.
2267 if (object->type == OBJT_VNODE) {
2268 if (root == NULL && object->resident_page_count != 0)
2269 vhold(object->handle);
2270 else if (root != NULL && object->resident_page_count == 0)
2271 vdrop(object->handle);
2278 * Cache, deactivate, or do nothing as appropriate. This routine
2279 * is typically used by madvise() MADV_DONTNEED.
2281 * Generally speaking we want to move the page into the cache so
2282 * it gets reused quickly. However, this can result in a silly syndrome
2283 * due to the page recycling too quickly. Small objects will not be
2284 * fully cached. On the otherhand, if we move the page to the inactive
2285 * queue we wind up with a problem whereby very large objects
2286 * unnecessarily blow away our inactive and cache queues.
2288 * The solution is to move the pages based on a fixed weighting. We
2289 * either leave them alone, deactivate them, or move them to the cache,
2290 * where moving them to the cache has the highest weighting.
2291 * By forcing some pages into other queues we eventually force the
2292 * system to balance the queues, potentially recovering other unrelated
2293 * space from active. The idea is to not force this to happen too
2296 * The object and page must be locked.
2299 vm_page_dontneed(vm_page_t m)
2304 vm_page_lock_assert(m, MA_OWNED);
2305 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2306 dnw = PCPU_GET(dnweight);
2310 * Occasionally leave the page alone.
2312 if ((dnw & 0x01F0) == 0 || m->queue == PQ_INACTIVE) {
2313 if (m->act_count >= ACT_INIT)
2319 * Clear any references to the page. Otherwise, the page daemon will
2320 * immediately reactivate the page.
2322 * Perform the pmap_clear_reference() first. Otherwise, a concurrent
2323 * pmap operation, such as pmap_remove(), could clear a reference in
2324 * the pmap and set PGA_REFERENCED on the page before the
2325 * pmap_clear_reference() had completed. Consequently, the page would
2326 * appear referenced based upon an old reference that occurred before
2327 * this function ran.
2329 pmap_clear_reference(m);
2330 vm_page_aflag_clear(m, PGA_REFERENCED);
2332 if (m->dirty == 0 && pmap_is_modified(m))
2335 if (m->dirty || (dnw & 0x0070) == 0) {
2337 * Deactivate the page 3 times out of 32.
2342 * Cache the page 28 times out of every 32. Note that
2343 * the page is deactivated instead of cached, but placed
2344 * at the head of the queue instead of the tail.
2348 _vm_page_deactivate(m, head);
2352 * Grab a page, waiting until we are waken up due to the page
2353 * changing state. We keep on waiting, if the page continues
2354 * to be in the object. If the page doesn't exist, first allocate it
2355 * and then conditionally zero it.
2357 * The caller must always specify the VM_ALLOC_RETRY flag. This is intended
2358 * to facilitate its eventual removal.
2360 * This routine may sleep.
2362 * The object must be locked on entry. The lock will, however, be released
2363 * and reacquired if the routine sleeps.
2366 vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags)
2370 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2371 KASSERT((allocflags & VM_ALLOC_RETRY) != 0,
2372 ("vm_page_grab: VM_ALLOC_RETRY is required"));
2374 if ((m = vm_page_lookup(object, pindex)) != NULL) {
2375 if ((m->oflags & VPO_BUSY) != 0 ||
2376 ((allocflags & VM_ALLOC_IGN_SBUSY) == 0 && m->busy != 0)) {
2378 * Reference the page before unlocking and
2379 * sleeping so that the page daemon is less
2380 * likely to reclaim it.
2382 vm_page_aflag_set(m, PGA_REFERENCED);
2383 vm_page_sleep(m, "pgrbwt");
2386 if ((allocflags & VM_ALLOC_WIRED) != 0) {
2391 if ((allocflags & VM_ALLOC_NOBUSY) == 0)
2396 m = vm_page_alloc(object, pindex, allocflags & ~(VM_ALLOC_RETRY |
2397 VM_ALLOC_IGN_SBUSY));
2399 VM_OBJECT_UNLOCK(object);
2401 VM_OBJECT_LOCK(object);
2403 } else if (m->valid != 0)
2405 if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0)
2411 * Mapping function for valid or dirty bits in a page.
2413 * Inputs are required to range within a page.
2416 vm_page_bits(int base, int size)
2422 base + size <= PAGE_SIZE,
2423 ("vm_page_bits: illegal base/size %d/%d", base, size)
2426 if (size == 0) /* handle degenerate case */
2429 first_bit = base >> DEV_BSHIFT;
2430 last_bit = (base + size - 1) >> DEV_BSHIFT;
2432 return (((vm_page_bits_t)2 << last_bit) -
2433 ((vm_page_bits_t)1 << first_bit));
2437 * vm_page_set_valid:
2439 * Sets portions of a page valid. The arguments are expected
2440 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
2441 * of any partial chunks touched by the range. The invalid portion of
2442 * such chunks will be zeroed.
2444 * (base + size) must be less then or equal to PAGE_SIZE.
2447 vm_page_set_valid(vm_page_t m, int base, int size)
2451 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2452 if (size == 0) /* handle degenerate case */
2456 * If the base is not DEV_BSIZE aligned and the valid
2457 * bit is clear, we have to zero out a portion of the
2460 if ((frag = base & ~(DEV_BSIZE - 1)) != base &&
2461 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0)
2462 pmap_zero_page_area(m, frag, base - frag);
2465 * If the ending offset is not DEV_BSIZE aligned and the
2466 * valid bit is clear, we have to zero out a portion of
2469 endoff = base + size;
2470 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff &&
2471 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0)
2472 pmap_zero_page_area(m, endoff,
2473 DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
2476 * Assert that no previously invalid block that is now being validated
2479 KASSERT((~m->valid & vm_page_bits(base, size) & m->dirty) == 0,
2480 ("vm_page_set_valid: page %p is dirty", m));
2483 * Set valid bits inclusive of any overlap.
2485 m->valid |= vm_page_bits(base, size);
2489 * Clear the given bits from the specified page's dirty field.
2491 static __inline void
2492 vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits)
2495 #if PAGE_SIZE < 16384
2500 * If the object is locked and the page is neither VPO_BUSY nor
2501 * write mapped, then the page's dirty field cannot possibly be
2502 * set by a concurrent pmap operation.
2504 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2505 if ((m->oflags & VPO_BUSY) == 0 && !pmap_page_is_write_mapped(m))
2506 m->dirty &= ~pagebits;
2509 * The pmap layer can call vm_page_dirty() without
2510 * holding a distinguished lock. The combination of
2511 * the object's lock and an atomic operation suffice
2512 * to guarantee consistency of the page dirty field.
2514 * For PAGE_SIZE == 32768 case, compiler already
2515 * properly aligns the dirty field, so no forcible
2516 * alignment is needed. Only require existence of
2517 * atomic_clear_64 when page size is 32768.
2519 addr = (uintptr_t)&m->dirty;
2520 #if PAGE_SIZE == 32768
2521 atomic_clear_64((uint64_t *)addr, pagebits);
2522 #elif PAGE_SIZE == 16384
2523 atomic_clear_32((uint32_t *)addr, pagebits);
2524 #else /* PAGE_SIZE <= 8192 */
2526 * Use a trick to perform a 32-bit atomic on the
2527 * containing aligned word, to not depend on the existence
2528 * of atomic_clear_{8, 16}.
2530 shift = addr & (sizeof(uint32_t) - 1);
2531 #if BYTE_ORDER == BIG_ENDIAN
2532 shift = (sizeof(uint32_t) - sizeof(m->dirty) - shift) * NBBY;
2536 addr &= ~(sizeof(uint32_t) - 1);
2537 atomic_clear_32((uint32_t *)addr, pagebits << shift);
2538 #endif /* PAGE_SIZE */
2543 * vm_page_set_validclean:
2545 * Sets portions of a page valid and clean. The arguments are expected
2546 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
2547 * of any partial chunks touched by the range. The invalid portion of
2548 * such chunks will be zero'd.
2550 * (base + size) must be less then or equal to PAGE_SIZE.
2553 vm_page_set_validclean(vm_page_t m, int base, int size)
2555 vm_page_bits_t oldvalid, pagebits;
2558 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2559 if (size == 0) /* handle degenerate case */
2563 * If the base is not DEV_BSIZE aligned and the valid
2564 * bit is clear, we have to zero out a portion of the
2567 if ((frag = base & ~(DEV_BSIZE - 1)) != base &&
2568 (m->valid & ((vm_page_bits_t)1 << (base >> DEV_BSHIFT))) == 0)
2569 pmap_zero_page_area(m, frag, base - frag);
2572 * If the ending offset is not DEV_BSIZE aligned and the
2573 * valid bit is clear, we have to zero out a portion of
2576 endoff = base + size;
2577 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff &&
2578 (m->valid & ((vm_page_bits_t)1 << (endoff >> DEV_BSHIFT))) == 0)
2579 pmap_zero_page_area(m, endoff,
2580 DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
2583 * Set valid, clear dirty bits. If validating the entire
2584 * page we can safely clear the pmap modify bit. We also
2585 * use this opportunity to clear the VPO_NOSYNC flag. If a process
2586 * takes a write fault on a MAP_NOSYNC memory area the flag will
2589 * We set valid bits inclusive of any overlap, but we can only
2590 * clear dirty bits for DEV_BSIZE chunks that are fully within
2593 oldvalid = m->valid;
2594 pagebits = vm_page_bits(base, size);
2595 m->valid |= pagebits;
2597 if ((frag = base & (DEV_BSIZE - 1)) != 0) {
2598 frag = DEV_BSIZE - frag;
2604 pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1));
2606 if (base == 0 && size == PAGE_SIZE) {
2608 * The page can only be modified within the pmap if it is
2609 * mapped, and it can only be mapped if it was previously
2612 if (oldvalid == VM_PAGE_BITS_ALL)
2614 * Perform the pmap_clear_modify() first. Otherwise,
2615 * a concurrent pmap operation, such as
2616 * pmap_protect(), could clear a modification in the
2617 * pmap and set the dirty field on the page before
2618 * pmap_clear_modify() had begun and after the dirty
2619 * field was cleared here.
2621 pmap_clear_modify(m);
2623 m->oflags &= ~VPO_NOSYNC;
2624 } else if (oldvalid != VM_PAGE_BITS_ALL)
2625 m->dirty &= ~pagebits;
2627 vm_page_clear_dirty_mask(m, pagebits);
2631 vm_page_clear_dirty(vm_page_t m, int base, int size)
2634 vm_page_clear_dirty_mask(m, vm_page_bits(base, size));
2638 * vm_page_set_invalid:
2640 * Invalidates DEV_BSIZE'd chunks within a page. Both the
2641 * valid and dirty bits for the effected areas are cleared.
2644 vm_page_set_invalid(vm_page_t m, int base, int size)
2646 vm_page_bits_t bits;
2648 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2649 KASSERT((m->oflags & VPO_BUSY) == 0,
2650 ("vm_page_set_invalid: page %p is busy", m));
2651 bits = vm_page_bits(base, size);
2652 if (m->valid == VM_PAGE_BITS_ALL && bits != 0)
2654 KASSERT(!pmap_page_is_mapped(m),
2655 ("vm_page_set_invalid: page %p is mapped", m));
2661 * vm_page_zero_invalid()
2663 * The kernel assumes that the invalid portions of a page contain
2664 * garbage, but such pages can be mapped into memory by user code.
2665 * When this occurs, we must zero out the non-valid portions of the
2666 * page so user code sees what it expects.
2668 * Pages are most often semi-valid when the end of a file is mapped
2669 * into memory and the file's size is not page aligned.
2672 vm_page_zero_invalid(vm_page_t m, boolean_t setvalid)
2677 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2679 * Scan the valid bits looking for invalid sections that
2680 * must be zerod. Invalid sub-DEV_BSIZE'd areas ( where the
2681 * valid bit may be set ) have already been zerod by
2682 * vm_page_set_validclean().
2684 for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) {
2685 if (i == (PAGE_SIZE / DEV_BSIZE) ||
2686 (m->valid & ((vm_page_bits_t)1 << i))) {
2688 pmap_zero_page_area(m,
2689 b << DEV_BSHIFT, (i - b) << DEV_BSHIFT);
2696 * setvalid is TRUE when we can safely set the zero'd areas
2697 * as being valid. We can do this if there are no cache consistancy
2698 * issues. e.g. it is ok to do with UFS, but not ok to do with NFS.
2701 m->valid = VM_PAGE_BITS_ALL;
2707 * Is (partial) page valid? Note that the case where size == 0
2708 * will return FALSE in the degenerate case where the page is
2709 * entirely invalid, and TRUE otherwise.
2712 vm_page_is_valid(vm_page_t m, int base, int size)
2714 vm_page_bits_t bits;
2716 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2717 bits = vm_page_bits(base, size);
2718 if (m->valid && ((m->valid & bits) == bits))
2725 * Set the page's dirty bits if the page is modified.
2728 vm_page_test_dirty(vm_page_t m)
2731 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2732 if (m->dirty != VM_PAGE_BITS_ALL && pmap_is_modified(m))
2737 vm_page_lock_KBI(vm_page_t m, const char *file, int line)
2740 mtx_lock_flags_(vm_page_lockptr(m), 0, file, line);
2744 vm_page_unlock_KBI(vm_page_t m, const char *file, int line)
2747 mtx_unlock_flags_(vm_page_lockptr(m), 0, file, line);
2751 vm_page_trylock_KBI(vm_page_t m, const char *file, int line)
2754 return (mtx_trylock_flags_(vm_page_lockptr(m), 0, file, line));
2757 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
2759 vm_page_lock_assert_KBI(vm_page_t m, int a, const char *file, int line)
2762 mtx_assert_(vm_page_lockptr(m), a, file, line);
2766 int so_zerocp_fullpage = 0;
2769 * Replace the given page with a copy. The copied page assumes
2770 * the portion of the given page's "wire_count" that is not the
2771 * responsibility of this copy-on-write mechanism.
2773 * The object containing the given page must have a non-zero
2774 * paging-in-progress count and be locked.
2777 vm_page_cowfault(vm_page_t m)
2783 mtx_assert(&vm_page_queue_mtx, MA_NOTOWNED);
2784 vm_page_lock_assert(m, MA_OWNED);
2786 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2787 KASSERT(object->paging_in_progress != 0,
2788 ("vm_page_cowfault: object %p's paging-in-progress count is zero.",
2795 mnew = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL | VM_ALLOC_NOBUSY);
2797 vm_page_insert(m, object, pindex);
2799 VM_OBJECT_UNLOCK(object);
2801 VM_OBJECT_LOCK(object);
2802 if (m == vm_page_lookup(object, pindex)) {
2807 * Page disappeared during the wait.
2815 * check to see if we raced with an xmit complete when
2816 * waiting to allocate a page. If so, put things back
2822 vm_page_unlock(mnew);
2823 vm_page_insert(m, object, pindex);
2824 } else { /* clear COW & copy page */
2825 if (!so_zerocp_fullpage)
2826 pmap_copy_page(m, mnew);
2827 mnew->valid = VM_PAGE_BITS_ALL;
2828 vm_page_dirty(mnew);
2829 mnew->wire_count = m->wire_count - m->cow;
2830 m->wire_count = m->cow;
2836 vm_page_cowclear(vm_page_t m)
2839 vm_page_lock_assert(m, MA_OWNED);
2843 * let vm_fault add back write permission lazily
2847 * sf_buf_free() will free the page, so we needn't do it here
2852 vm_page_cowsetup(vm_page_t m)
2855 vm_page_lock_assert(m, MA_OWNED);
2856 if ((m->flags & PG_FICTITIOUS) != 0 ||
2857 (m->oflags & VPO_UNMANAGED) != 0 ||
2858 m->cow == USHRT_MAX - 1 || !VM_OBJECT_TRYLOCK(m->object))
2861 pmap_remove_write(m);
2862 VM_OBJECT_UNLOCK(m->object);
2868 vm_page_object_lock_assert(vm_page_t m)
2872 * Certain of the page's fields may only be modified by the
2873 * holder of the containing object's lock or the setter of the
2874 * page's VPO_BUSY flag. Unfortunately, the setter of the
2875 * VPO_BUSY flag is not recorded, and thus cannot be checked
2878 if (m->object != NULL && (m->oflags & VPO_BUSY) == 0)
2879 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2883 #include "opt_ddb.h"
2885 #include <sys/kernel.h>
2887 #include <ddb/ddb.h>
2889 DB_SHOW_COMMAND(page, vm_page_print_page_info)
2891 db_printf("cnt.v_free_count: %d\n", cnt.v_free_count);
2892 db_printf("cnt.v_cache_count: %d\n", cnt.v_cache_count);
2893 db_printf("cnt.v_inactive_count: %d\n", cnt.v_inactive_count);
2894 db_printf("cnt.v_active_count: %d\n", cnt.v_active_count);
2895 db_printf("cnt.v_wire_count: %d\n", cnt.v_wire_count);
2896 db_printf("cnt.v_free_reserved: %d\n", cnt.v_free_reserved);
2897 db_printf("cnt.v_free_min: %d\n", cnt.v_free_min);
2898 db_printf("cnt.v_free_target: %d\n", cnt.v_free_target);
2899 db_printf("cnt.v_cache_min: %d\n", cnt.v_cache_min);
2900 db_printf("cnt.v_inactive_target: %d\n", cnt.v_inactive_target);
2903 DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info)
2906 db_printf("PQ_FREE:");
2907 db_printf(" %d", cnt.v_free_count);
2910 db_printf("PQ_CACHE:");
2911 db_printf(" %d", cnt.v_cache_count);
2914 db_printf("PQ_ACTIVE: %d, PQ_INACTIVE: %d\n",
2915 *vm_page_queues[PQ_ACTIVE].cnt,
2916 *vm_page_queues[PQ_INACTIVE].cnt);
2919 DB_SHOW_COMMAND(pginfo, vm_page_print_pginfo)
2925 db_printf("show pginfo addr\n");
2929 phys = strchr(modif, 'p') != NULL;
2931 m = PHYS_TO_VM_PAGE(addr);
2933 m = (vm_page_t)addr;
2935 "page %p obj %p pidx 0x%jx phys 0x%jx q %d hold %d wire %d\n"
2936 " af 0x%x of 0x%x f 0x%x act %d busy %d valid 0x%x dirty 0x%x\n",
2937 m, m->object, (uintmax_t)m->pindex, (uintmax_t)m->phys_addr,
2938 m->queue, m->hold_count, m->wire_count, m->aflags, m->oflags,
2939 m->flags, m->act_count, m->busy, m->valid, m->dirty);