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 page queue lock is required when adding or removing a page from a
67 * page queue (vm_pagequeues[]), regardless of other locks or the
68 * busy state of a page.
70 * * In general, no thread besides the page daemon can acquire or
71 * hold more than one page queue lock at a time.
73 * * The page daemon can acquire and hold any pair of page queue
76 * - The object lock is required when inserting or removing
77 * pages from an object (vm_page_insert() or vm_page_remove()).
82 * Resident memory management module.
85 #include <sys/cdefs.h>
86 __FBSDID("$FreeBSD$");
90 #include <sys/param.h>
91 #include <sys/systm.h>
93 #include <sys/kernel.h>
94 #include <sys/limits.h>
95 #include <sys/malloc.h>
97 #include <sys/msgbuf.h>
98 #include <sys/mutex.h>
100 #include <sys/rwlock.h>
101 #include <sys/sysctl.h>
102 #include <sys/vmmeter.h>
103 #include <sys/vnode.h>
107 #include <vm/vm_param.h>
108 #include <vm/vm_kern.h>
109 #include <vm/vm_object.h>
110 #include <vm/vm_page.h>
111 #include <vm/vm_pageout.h>
112 #include <vm/vm_pager.h>
113 #include <vm/vm_phys.h>
114 #include <vm/vm_radix.h>
115 #include <vm/vm_reserv.h>
116 #include <vm/vm_extern.h>
118 #include <vm/uma_int.h>
120 #include <machine/md_var.h>
123 * Associated with page of user-allocatable memory is a
127 struct vm_pagequeue vm_pagequeues[PQ_COUNT] = {
129 .pq_pl = TAILQ_HEAD_INITIALIZER(
130 vm_pagequeues[PQ_INACTIVE].pq_pl),
131 .pq_cnt = &cnt.v_inactive_count,
132 .pq_name = "vm inactive pagequeue"
135 .pq_pl = TAILQ_HEAD_INITIALIZER(
136 vm_pagequeues[PQ_ACTIVE].pq_pl),
137 .pq_cnt = &cnt.v_active_count,
138 .pq_name = "vm active pagequeue"
141 struct mtx_padalign vm_page_queue_free_mtx;
143 struct mtx_padalign pa_lock[PA_LOCK_COUNT];
145 vm_page_t vm_page_array;
146 long vm_page_array_size;
148 int vm_page_zero_count;
150 static int boot_pages = UMA_BOOT_PAGES;
151 TUNABLE_INT("vm.boot_pages", &boot_pages);
152 SYSCTL_INT(_vm, OID_AUTO, boot_pages, CTLFLAG_RD, &boot_pages, 0,
153 "number of pages allocated for bootstrapping the VM system");
155 static int pa_tryrelock_restart;
156 SYSCTL_INT(_vm, OID_AUTO, tryrelock_restart, CTLFLAG_RD,
157 &pa_tryrelock_restart, 0, "Number of tryrelock restarts");
159 static uma_zone_t fakepg_zone;
161 static struct vnode *vm_page_alloc_init(vm_page_t m);
162 static void vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits);
163 static void vm_page_enqueue(int queue, vm_page_t m);
164 static void vm_page_init_fakepg(void *dummy);
165 static void vm_page_insert_after(vm_page_t m, vm_object_t object,
166 vm_pindex_t pindex, vm_page_t mpred);
168 SYSINIT(vm_page, SI_SUB_VM, SI_ORDER_SECOND, vm_page_init_fakepg, NULL);
171 vm_page_init_fakepg(void *dummy)
174 fakepg_zone = uma_zcreate("fakepg", sizeof(struct vm_page), NULL, NULL,
175 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE | UMA_ZONE_VM);
178 /* Make sure that u_long is at least 64 bits when PAGE_SIZE is 32K. */
179 #if PAGE_SIZE == 32768
181 CTASSERT(sizeof(u_long) >= 8);
186 * Try to acquire a physical address lock while a pmap is locked. If we
187 * fail to trylock we unlock and lock the pmap directly and cache the
188 * locked pa in *locked. The caller should then restart their loop in case
189 * the virtual to physical mapping has changed.
192 vm_page_pa_tryrelock(pmap_t pmap, vm_paddr_t pa, vm_paddr_t *locked)
199 PA_LOCK_ASSERT(lockpa, MA_OWNED);
200 if (PA_LOCKPTR(pa) == PA_LOCKPTR(lockpa))
207 atomic_add_int(&pa_tryrelock_restart, 1);
216 * Sets the page size, perhaps based upon the memory
217 * size. Must be called before any use of page-size
218 * dependent functions.
221 vm_set_page_size(void)
223 if (cnt.v_page_size == 0)
224 cnt.v_page_size = PAGE_SIZE;
225 if (((cnt.v_page_size - 1) & cnt.v_page_size) != 0)
226 panic("vm_set_page_size: page size not a power of two");
230 * vm_page_blacklist_lookup:
232 * See if a physical address in this page has been listed
233 * in the blacklist tunable. Entries in the tunable are
234 * separated by spaces or commas. If an invalid integer is
235 * encountered then the rest of the string is skipped.
238 vm_page_blacklist_lookup(char *list, vm_paddr_t pa)
243 for (pos = list; *pos != '\0'; pos = cp) {
244 bad = strtoq(pos, &cp, 0);
246 if (*cp == ' ' || *cp == ',') {
253 if (pa == trunc_page(bad))
262 * Initializes the resident memory module.
264 * Allocates memory for the page cells, and
265 * for the object/offset-to-page hash table headers.
266 * Each page cell is initialized and placed on the free list.
269 vm_page_startup(vm_offset_t vaddr)
272 vm_paddr_t page_range;
279 /* the biggest memory array is the second group of pages */
281 vm_paddr_t biggestsize;
282 vm_paddr_t low_water, high_water;
287 vaddr = round_page(vaddr);
289 for (i = 0; phys_avail[i + 1]; i += 2) {
290 phys_avail[i] = round_page(phys_avail[i]);
291 phys_avail[i + 1] = trunc_page(phys_avail[i + 1]);
294 low_water = phys_avail[0];
295 high_water = phys_avail[1];
297 for (i = 0; phys_avail[i + 1]; i += 2) {
298 vm_paddr_t size = phys_avail[i + 1] - phys_avail[i];
300 if (size > biggestsize) {
304 if (phys_avail[i] < low_water)
305 low_water = phys_avail[i];
306 if (phys_avail[i + 1] > high_water)
307 high_water = phys_avail[i + 1];
314 end = phys_avail[biggestone+1];
317 * Initialize the page and queue locks.
319 mtx_init(&vm_page_queue_free_mtx, "vm page free queue", NULL, MTX_DEF);
320 for (i = 0; i < PA_LOCK_COUNT; i++)
321 mtx_init(&pa_lock[i], "vm page", NULL, MTX_DEF);
322 for (i = 0; i < PQ_COUNT; i++)
323 vm_pagequeue_init_lock(&vm_pagequeues[i]);
326 * Allocate memory for use when boot strapping the kernel memory
329 new_end = end - (boot_pages * UMA_SLAB_SIZE);
330 new_end = trunc_page(new_end);
331 mapped = pmap_map(&vaddr, new_end, end,
332 VM_PROT_READ | VM_PROT_WRITE);
333 bzero((void *)mapped, end - new_end);
334 uma_startup((void *)mapped, boot_pages);
336 #if defined(__amd64__) || defined(__i386__) || defined(__arm__) || \
339 * Allocate a bitmap to indicate that a random physical page
340 * needs to be included in a minidump.
342 * The amd64 port needs this to indicate which direct map pages
343 * need to be dumped, via calls to dump_add_page()/dump_drop_page().
345 * However, i386 still needs this workspace internally within the
346 * minidump code. In theory, they are not needed on i386, but are
347 * included should the sf_buf code decide to use them.
350 for (i = 0; dump_avail[i + 1] != 0; i += 2)
351 if (dump_avail[i + 1] > last_pa)
352 last_pa = dump_avail[i + 1];
353 page_range = last_pa / PAGE_SIZE;
354 vm_page_dump_size = round_page(roundup2(page_range, NBBY) / NBBY);
355 new_end -= vm_page_dump_size;
356 vm_page_dump = (void *)(uintptr_t)pmap_map(&vaddr, new_end,
357 new_end + vm_page_dump_size, VM_PROT_READ | VM_PROT_WRITE);
358 bzero((void *)vm_page_dump, vm_page_dump_size);
362 * Request that the physical pages underlying the message buffer be
363 * included in a crash dump. Since the message buffer is accessed
364 * through the direct map, they are not automatically included.
366 pa = DMAP_TO_PHYS((vm_offset_t)msgbufp->msg_ptr);
367 last_pa = pa + round_page(msgbufsize);
368 while (pa < last_pa) {
374 * Compute the number of pages of memory that will be available for
375 * use (taking into account the overhead of a page structure per
378 first_page = low_water / PAGE_SIZE;
379 #ifdef VM_PHYSSEG_SPARSE
381 for (i = 0; phys_avail[i + 1] != 0; i += 2)
382 page_range += atop(phys_avail[i + 1] - phys_avail[i]);
383 #elif defined(VM_PHYSSEG_DENSE)
384 page_range = high_water / PAGE_SIZE - first_page;
386 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
391 * Reserve an unmapped guard page to trap access to vm_page_array[-1].
396 * Initialize the mem entry structures now, and put them in the free
399 new_end = trunc_page(end - page_range * sizeof(struct vm_page));
400 mapped = pmap_map(&vaddr, new_end, end,
401 VM_PROT_READ | VM_PROT_WRITE);
402 vm_page_array = (vm_page_t) mapped;
403 #if VM_NRESERVLEVEL > 0
405 * Allocate memory for the reservation management system's data
408 new_end = vm_reserv_startup(&vaddr, new_end, high_water);
410 #if defined(__amd64__) || defined(__mips__)
412 * pmap_map on amd64 and mips can come out of the direct-map, not kvm
413 * like i386, so the pages must be tracked for a crashdump to include
414 * this data. This includes the vm_page_array and the early UMA
417 for (pa = new_end; pa < phys_avail[biggestone + 1]; pa += PAGE_SIZE)
420 phys_avail[biggestone + 1] = new_end;
423 * Clear all of the page structures
425 bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page));
426 for (i = 0; i < page_range; i++)
427 vm_page_array[i].order = VM_NFREEORDER;
428 vm_page_array_size = page_range;
431 * Initialize the physical memory allocator.
436 * Add every available physical page that is not blacklisted to
439 cnt.v_page_count = 0;
440 cnt.v_free_count = 0;
441 list = getenv("vm.blacklist");
442 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
444 last_pa = phys_avail[i + 1];
445 while (pa < last_pa) {
447 vm_page_blacklist_lookup(list, pa))
448 printf("Skipping page with pa 0x%jx\n",
451 vm_phys_add_page(pa);
456 #if VM_NRESERVLEVEL > 0
458 * Initialize the reservation management system.
466 vm_page_reference(vm_page_t m)
469 vm_page_aflag_set(m, PGA_REFERENCED);
473 vm_page_busy(vm_page_t m)
476 VM_OBJECT_ASSERT_WLOCKED(m->object);
477 KASSERT((m->oflags & VPO_BUSY) == 0,
478 ("vm_page_busy: page already busy!!!"));
479 m->oflags |= VPO_BUSY;
485 * wakeup anyone waiting for the page.
488 vm_page_flash(vm_page_t m)
491 VM_OBJECT_ASSERT_WLOCKED(m->object);
492 if (m->oflags & VPO_WANTED) {
493 m->oflags &= ~VPO_WANTED;
501 * clear the VPO_BUSY flag and wakeup anyone waiting for the
506 vm_page_wakeup(vm_page_t m)
509 VM_OBJECT_ASSERT_WLOCKED(m->object);
510 KASSERT(m->oflags & VPO_BUSY, ("vm_page_wakeup: page not busy!!!"));
511 m->oflags &= ~VPO_BUSY;
516 vm_page_io_start(vm_page_t m)
519 VM_OBJECT_ASSERT_WLOCKED(m->object);
524 vm_page_io_finish(vm_page_t m)
527 VM_OBJECT_ASSERT_WLOCKED(m->object);
528 KASSERT(m->busy > 0, ("vm_page_io_finish: page %p is not busy", m));
535 * Keep page from being freed by the page daemon
536 * much of the same effect as wiring, except much lower
537 * overhead and should be used only for *very* temporary
538 * holding ("wiring").
541 vm_page_hold(vm_page_t mem)
544 vm_page_lock_assert(mem, MA_OWNED);
549 vm_page_unhold(vm_page_t mem)
552 vm_page_lock_assert(mem, MA_OWNED);
554 KASSERT(mem->hold_count >= 0, ("vm_page_unhold: hold count < 0!!!"));
555 if (mem->hold_count == 0 && (mem->flags & PG_UNHOLDFREE) != 0)
556 vm_page_free_toq(mem);
560 * vm_page_unhold_pages:
562 * Unhold each of the pages that is referenced by the given array.
565 vm_page_unhold_pages(vm_page_t *ma, int count)
567 struct mtx *mtx, *new_mtx;
570 for (; count != 0; count--) {
572 * Avoid releasing and reacquiring the same page lock.
574 new_mtx = vm_page_lockptr(*ma);
575 if (mtx != new_mtx) {
589 PHYS_TO_VM_PAGE(vm_paddr_t pa)
593 #ifdef VM_PHYSSEG_SPARSE
594 m = vm_phys_paddr_to_vm_page(pa);
596 m = vm_phys_fictitious_to_vm_page(pa);
598 #elif defined(VM_PHYSSEG_DENSE)
602 if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
603 m = &vm_page_array[pi - first_page];
606 return (vm_phys_fictitious_to_vm_page(pa));
608 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
615 * Create a fictitious page with the specified physical address and
616 * memory attribute. The memory attribute is the only the machine-
617 * dependent aspect of a fictitious page that must be initialized.
620 vm_page_getfake(vm_paddr_t paddr, vm_memattr_t memattr)
624 m = uma_zalloc(fakepg_zone, M_WAITOK | M_ZERO);
625 vm_page_initfake(m, paddr, memattr);
630 vm_page_initfake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
633 if ((m->flags & PG_FICTITIOUS) != 0) {
635 * The page's memattr might have changed since the
636 * previous initialization. Update the pmap to the
641 m->phys_addr = paddr;
643 /* Fictitious pages don't use "segind". */
644 m->flags = PG_FICTITIOUS;
645 /* Fictitious pages don't use "order" or "pool". */
646 m->oflags = VPO_BUSY | VPO_UNMANAGED;
649 pmap_page_set_memattr(m, memattr);
655 * Release a fictitious page.
658 vm_page_putfake(vm_page_t m)
661 KASSERT((m->oflags & VPO_UNMANAGED) != 0, ("managed %p", m));
662 KASSERT((m->flags & PG_FICTITIOUS) != 0,
663 ("vm_page_putfake: bad page %p", m));
664 uma_zfree(fakepg_zone, m);
668 * vm_page_updatefake:
670 * Update the given fictitious page to the specified physical address and
674 vm_page_updatefake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
677 KASSERT((m->flags & PG_FICTITIOUS) != 0,
678 ("vm_page_updatefake: bad page %p", m));
679 m->phys_addr = paddr;
680 pmap_page_set_memattr(m, memattr);
689 vm_page_free(vm_page_t m)
692 m->flags &= ~PG_ZERO;
699 * Free a page to the zerod-pages queue
702 vm_page_free_zero(vm_page_t m)
710 * Unbusy and handle the page queueing for a page from the VOP_GETPAGES()
711 * array which is not the request page.
714 vm_page_readahead_finish(vm_page_t m)
719 * Since the page is not the requested page, whether
720 * it should be activated or deactivated is not
721 * obvious. Empirical results have shown that
722 * deactivating the page is usually the best choice,
723 * unless the page is wanted by another thread.
725 if (m->oflags & VPO_WANTED) {
731 vm_page_deactivate(m);
737 * Free the completely invalid page. Such page state
738 * occurs due to the short read operation which did
739 * not covered our page at all, or in case when a read
751 * Sleep and release the page lock.
753 * The object containing the given page must be locked.
756 vm_page_sleep(vm_page_t m, const char *msg)
759 VM_OBJECT_ASSERT_WLOCKED(m->object);
760 if (mtx_owned(vm_page_lockptr(m)))
764 * It's possible that while we sleep, the page will get
765 * unbusied and freed. If we are holding the object
766 * lock, we will assume we hold a reference to the object
767 * such that even if m->object changes, we can re-lock
770 m->oflags |= VPO_WANTED;
771 VM_OBJECT_SLEEP(m->object, m, PVM, msg, 0);
775 * vm_page_dirty_KBI: [ internal use only ]
777 * Set all bits in the page's dirty field.
779 * The object containing the specified page must be locked if the
780 * call is made from the machine-independent layer.
782 * See vm_page_clear_dirty_mask().
784 * This function should only be called by vm_page_dirty().
787 vm_page_dirty_KBI(vm_page_t m)
790 /* These assertions refer to this operation by its public name. */
791 KASSERT((m->flags & PG_CACHED) == 0,
792 ("vm_page_dirty: page in cache!"));
793 KASSERT(!VM_PAGE_IS_FREE(m),
794 ("vm_page_dirty: page is free!"));
795 KASSERT(m->valid == VM_PAGE_BITS_ALL,
796 ("vm_page_dirty: page is invalid!"));
797 m->dirty = VM_PAGE_BITS_ALL;
801 * vm_page_insert: [ internal use only ]
803 * Inserts the given mem entry into the object and object list.
805 * The object must be locked.
808 vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex)
812 VM_OBJECT_ASSERT_WLOCKED(object);
813 mpred = vm_radix_lookup_le(&object->rtree, pindex);
814 vm_page_insert_after(m, object, pindex, mpred);
818 * vm_page_insert_after:
820 * Inserts the page "m" into the specified object at offset "pindex".
822 * The page "mpred" must immediately precede the offset "pindex" within
823 * the specified object.
825 * The object must be locked.
828 vm_page_insert_after(vm_page_t m, vm_object_t object, vm_pindex_t pindex,
833 VM_OBJECT_ASSERT_WLOCKED(object);
834 KASSERT(m->object == NULL,
835 ("vm_page_insert_after: page already inserted"));
837 KASSERT(mpred->object == object ||
838 (mpred->flags & PG_SLAB) != 0,
839 ("vm_page_insert_after: object doesn't contain mpred"));
840 KASSERT(mpred->pindex < pindex,
841 ("vm_page_insert_after: mpred doesn't precede pindex"));
842 msucc = TAILQ_NEXT(mpred, listq);
844 msucc = TAILQ_FIRST(&object->memq);
846 KASSERT(msucc->pindex > pindex,
847 ("vm_page_insert_after: msucc doesn't succeed pindex"));
850 * Record the object/offset pair in this page
856 * Now link into the object's ordered list of backed pages.
859 TAILQ_INSERT_AFTER(&object->memq, mpred, m, listq);
861 TAILQ_INSERT_HEAD(&object->memq, m, listq);
862 vm_radix_insert(&object->rtree, m);
865 * Show that the object has one more resident page.
867 object->resident_page_count++;
870 * Hold the vnode until the last page is released.
872 if (object->resident_page_count == 1 && object->type == OBJT_VNODE)
873 vhold(object->handle);
876 * Since we are inserting a new and possibly dirty page,
877 * update the object's OBJ_MIGHTBEDIRTY flag.
879 if (pmap_page_is_write_mapped(m))
880 vm_object_set_writeable_dirty(object);
886 * Removes the given mem entry from the object/offset-page
887 * table and the object page list, but do not invalidate/terminate
890 * The object must be locked. The page must be locked if it is managed.
893 vm_page_remove(vm_page_t m)
897 if ((m->oflags & VPO_UNMANAGED) == 0)
898 vm_page_lock_assert(m, MA_OWNED);
899 if ((object = m->object) == NULL)
901 VM_OBJECT_ASSERT_WLOCKED(object);
902 if (m->oflags & VPO_BUSY) {
903 m->oflags &= ~VPO_BUSY;
908 * Now remove from the object's list of backed pages.
910 vm_radix_remove(&object->rtree, m->pindex);
911 TAILQ_REMOVE(&object->memq, m, listq);
914 * And show that the object has one fewer resident page.
916 object->resident_page_count--;
919 * The vnode may now be recycled.
921 if (object->resident_page_count == 0 && object->type == OBJT_VNODE)
922 vdrop(object->handle);
930 * Returns the page associated with the object/offset
931 * pair specified; if none is found, NULL is returned.
933 * The object must be locked.
936 vm_page_lookup(vm_object_t object, vm_pindex_t pindex)
939 VM_OBJECT_ASSERT_LOCKED(object);
940 return (vm_radix_lookup(&object->rtree, pindex));
944 * vm_page_find_least:
946 * Returns the page associated with the object with least pindex
947 * greater than or equal to the parameter pindex, or NULL.
949 * The object must be locked.
952 vm_page_find_least(vm_object_t object, vm_pindex_t pindex)
956 VM_OBJECT_ASSERT_LOCKED(object);
957 if ((m = TAILQ_FIRST(&object->memq)) != NULL && m->pindex < pindex)
958 m = vm_radix_lookup_ge(&object->rtree, pindex);
963 * Returns the given page's successor (by pindex) within the object if it is
964 * resident; if none is found, NULL is returned.
966 * The object must be locked.
969 vm_page_next(vm_page_t m)
973 VM_OBJECT_ASSERT_WLOCKED(m->object);
974 if ((next = TAILQ_NEXT(m, listq)) != NULL &&
975 next->pindex != m->pindex + 1)
981 * Returns the given page's predecessor (by pindex) within the object if it is
982 * resident; if none is found, NULL is returned.
984 * The object must be locked.
987 vm_page_prev(vm_page_t m)
991 VM_OBJECT_ASSERT_WLOCKED(m->object);
992 if ((prev = TAILQ_PREV(m, pglist, listq)) != NULL &&
993 prev->pindex != m->pindex - 1)
1001 * Move the given memory entry from its
1002 * current object to the specified target object/offset.
1004 * Note: swap associated with the page must be invalidated by the move. We
1005 * have to do this for several reasons: (1) we aren't freeing the
1006 * page, (2) we are dirtying the page, (3) the VM system is probably
1007 * moving the page from object A to B, and will then later move
1008 * the backing store from A to B and we can't have a conflict.
1010 * Note: we *always* dirty the page. It is necessary both for the
1011 * fact that we moved it, and because we may be invalidating
1012 * swap. If the page is on the cache, we have to deactivate it
1013 * or vm_page_dirty() will panic. Dirty pages are not allowed
1016 * The objects must be locked. The page must be locked if it is managed.
1019 vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex)
1023 vm_page_insert(m, new_object, new_pindex);
1028 * Convert all of the given object's cached pages that have a
1029 * pindex within the given range into free pages. If the value
1030 * zero is given for "end", then the range's upper bound is
1031 * infinity. If the given object is backed by a vnode and it
1032 * transitions from having one or more cached pages to none, the
1033 * vnode's hold count is reduced.
1036 vm_page_cache_free(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1041 mtx_lock(&vm_page_queue_free_mtx);
1042 if (__predict_false(vm_radix_is_empty(&object->cache))) {
1043 mtx_unlock(&vm_page_queue_free_mtx);
1046 while ((m = vm_radix_lookup_ge(&object->cache, start)) != NULL) {
1047 if (end != 0 && m->pindex >= end)
1049 vm_radix_remove(&object->cache, m->pindex);
1052 /* Clear PG_CACHED and set PG_FREE. */
1053 m->flags ^= PG_CACHED | PG_FREE;
1054 KASSERT((m->flags & (PG_CACHED | PG_FREE)) == PG_FREE,
1055 ("vm_page_cache_free: page %p has inconsistent flags", m));
1056 cnt.v_cache_count--;
1059 empty = vm_radix_is_empty(&object->cache);
1060 mtx_unlock(&vm_page_queue_free_mtx);
1061 if (object->type == OBJT_VNODE && empty)
1062 vdrop(object->handle);
1066 * Returns the cached page that is associated with the given
1067 * object and offset. If, however, none exists, returns NULL.
1069 * The free page queue must be locked.
1071 static inline vm_page_t
1072 vm_page_cache_lookup(vm_object_t object, vm_pindex_t pindex)
1075 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1076 return (vm_radix_lookup(&object->cache, pindex));
1080 * Remove the given cached page from its containing object's
1081 * collection of cached pages.
1083 * The free page queue must be locked.
1086 vm_page_cache_remove(vm_page_t m)
1089 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1090 KASSERT((m->flags & PG_CACHED) != 0,
1091 ("vm_page_cache_remove: page %p is not cached", m));
1092 vm_radix_remove(&m->object->cache, m->pindex);
1094 cnt.v_cache_count--;
1098 * Transfer all of the cached pages with offset greater than or
1099 * equal to 'offidxstart' from the original object's cache to the
1100 * new object's cache. However, any cached pages with offset
1101 * greater than or equal to the new object's size are kept in the
1102 * original object. Initially, the new object's cache must be
1103 * empty. Offset 'offidxstart' in the original object must
1104 * correspond to offset zero in the new object.
1106 * The new object must be locked.
1109 vm_page_cache_transfer(vm_object_t orig_object, vm_pindex_t offidxstart,
1110 vm_object_t new_object)
1115 * Insertion into an object's collection of cached pages
1116 * requires the object to be locked. In contrast, removal does
1119 VM_OBJECT_ASSERT_WLOCKED(new_object);
1120 KASSERT(vm_radix_is_empty(&new_object->cache),
1121 ("vm_page_cache_transfer: object %p has cached pages",
1123 mtx_lock(&vm_page_queue_free_mtx);
1124 while ((m = vm_radix_lookup_ge(&orig_object->cache,
1125 offidxstart)) != NULL) {
1127 * Transfer all of the pages with offset greater than or
1128 * equal to 'offidxstart' from the original object's
1129 * cache to the new object's cache.
1131 if ((m->pindex - offidxstart) >= new_object->size)
1133 vm_radix_remove(&orig_object->cache, m->pindex);
1134 /* Update the page's object and offset. */
1135 m->object = new_object;
1136 m->pindex -= offidxstart;
1137 vm_radix_insert(&new_object->cache, m);
1139 mtx_unlock(&vm_page_queue_free_mtx);
1143 * Returns TRUE if a cached page is associated with the given object and
1144 * offset, and FALSE otherwise.
1146 * The object must be locked.
1149 vm_page_is_cached(vm_object_t object, vm_pindex_t pindex)
1154 * Insertion into an object's collection of cached pages requires the
1155 * object to be locked. Therefore, if the object is locked and the
1156 * object's collection is empty, there is no need to acquire the free
1157 * page queues lock in order to prove that the specified page doesn't
1160 VM_OBJECT_ASSERT_WLOCKED(object);
1161 if (__predict_true(vm_object_cache_is_empty(object)))
1163 mtx_lock(&vm_page_queue_free_mtx);
1164 m = vm_page_cache_lookup(object, pindex);
1165 mtx_unlock(&vm_page_queue_free_mtx);
1172 * Allocate and return a page that is associated with the specified
1173 * object and offset pair. By default, this page has the flag VPO_BUSY
1176 * The caller must always specify an allocation class.
1178 * allocation classes:
1179 * VM_ALLOC_NORMAL normal process request
1180 * VM_ALLOC_SYSTEM system *really* needs a page
1181 * VM_ALLOC_INTERRUPT interrupt time request
1183 * optional allocation flags:
1184 * VM_ALLOC_COUNT(number) the number of additional pages that the caller
1185 * intends to allocate
1186 * VM_ALLOC_IFCACHED return page only if it is cached
1187 * VM_ALLOC_IFNOTCACHED return NULL, do not reactivate if the page
1189 * VM_ALLOC_NOBUSY do not set the flag VPO_BUSY on the page
1190 * VM_ALLOC_NODUMP do not include the page in a kernel core dump
1191 * VM_ALLOC_NOOBJ page is not associated with an object and
1192 * should not have the flag VPO_BUSY set
1193 * VM_ALLOC_WIRED wire the allocated page
1194 * VM_ALLOC_ZERO prefer a zeroed page
1196 * This routine may not sleep.
1199 vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req)
1201 struct vnode *vp = NULL;
1202 vm_object_t m_object;
1204 int flags, req_class;
1206 mpred = 0; /* XXX: pacify gcc */
1207 KASSERT((object != NULL) == ((req & VM_ALLOC_NOOBJ) == 0),
1208 ("vm_page_alloc: inconsistent object/req"));
1210 VM_OBJECT_ASSERT_WLOCKED(object);
1212 req_class = req & VM_ALLOC_CLASS_MASK;
1215 * The page daemon is allowed to dig deeper into the free page list.
1217 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
1218 req_class = VM_ALLOC_SYSTEM;
1220 if (object != NULL) {
1221 mpred = vm_radix_lookup_le(&object->rtree, pindex);
1222 KASSERT(mpred == NULL || mpred->pindex != pindex,
1223 ("vm_page_alloc: pindex already allocated"));
1225 mtx_lock(&vm_page_queue_free_mtx);
1226 if (cnt.v_free_count + cnt.v_cache_count > cnt.v_free_reserved ||
1227 (req_class == VM_ALLOC_SYSTEM &&
1228 cnt.v_free_count + cnt.v_cache_count > cnt.v_interrupt_free_min) ||
1229 (req_class == VM_ALLOC_INTERRUPT &&
1230 cnt.v_free_count + cnt.v_cache_count > 0)) {
1232 * Allocate from the free queue if the number of free pages
1233 * exceeds the minimum for the request class.
1235 if (object != NULL &&
1236 (m = vm_page_cache_lookup(object, pindex)) != NULL) {
1237 if ((req & VM_ALLOC_IFNOTCACHED) != 0) {
1238 mtx_unlock(&vm_page_queue_free_mtx);
1241 if (vm_phys_unfree_page(m))
1242 vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m, 0);
1243 #if VM_NRESERVLEVEL > 0
1244 else if (!vm_reserv_reactivate_page(m))
1248 panic("vm_page_alloc: cache page %p is missing"
1249 " from the free queue", m);
1250 } else if ((req & VM_ALLOC_IFCACHED) != 0) {
1251 mtx_unlock(&vm_page_queue_free_mtx);
1253 #if VM_NRESERVLEVEL > 0
1254 } else if (object == NULL || (object->flags & (OBJ_COLORED |
1255 OBJ_FICTITIOUS)) != OBJ_COLORED || (m =
1256 vm_reserv_alloc_page(object, pindex, mpred)) == NULL) {
1260 m = vm_phys_alloc_pages(object != NULL ?
1261 VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT, 0);
1262 #if VM_NRESERVLEVEL > 0
1263 if (m == NULL && vm_reserv_reclaim_inactive()) {
1264 m = vm_phys_alloc_pages(object != NULL ?
1265 VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT,
1272 * Not allocatable, give up.
1274 mtx_unlock(&vm_page_queue_free_mtx);
1275 atomic_add_int(&vm_pageout_deficit,
1276 max((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1));
1277 pagedaemon_wakeup();
1282 * At this point we had better have found a good page.
1284 KASSERT(m != NULL, ("vm_page_alloc: missing page"));
1285 KASSERT(m->queue == PQ_NONE,
1286 ("vm_page_alloc: page %p has unexpected queue %d", m, m->queue));
1287 KASSERT(m->wire_count == 0, ("vm_page_alloc: page %p is wired", m));
1288 KASSERT(m->hold_count == 0, ("vm_page_alloc: page %p is held", m));
1289 KASSERT(m->busy == 0, ("vm_page_alloc: page %p is busy", m));
1290 KASSERT(m->dirty == 0, ("vm_page_alloc: page %p is dirty", m));
1291 KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT,
1292 ("vm_page_alloc: page %p has unexpected memattr %d", m,
1293 pmap_page_get_memattr(m)));
1294 if ((m->flags & PG_CACHED) != 0) {
1295 KASSERT((m->flags & PG_ZERO) == 0,
1296 ("vm_page_alloc: cached page %p is PG_ZERO", m));
1297 KASSERT(m->valid != 0,
1298 ("vm_page_alloc: cached page %p is invalid", m));
1299 if (m->object == object && m->pindex == pindex)
1300 cnt.v_reactivated++;
1303 m_object = m->object;
1304 vm_page_cache_remove(m);
1305 if (m_object->type == OBJT_VNODE &&
1306 vm_object_cache_is_empty(m_object))
1307 vp = m_object->handle;
1309 KASSERT(VM_PAGE_IS_FREE(m),
1310 ("vm_page_alloc: page %p is not free", m));
1311 KASSERT(m->valid == 0,
1312 ("vm_page_alloc: free page %p is valid", m));
1317 * Only the PG_ZERO flag is inherited. The PG_CACHED or PG_FREE flag
1318 * must be cleared before the free page queues lock is released.
1321 if (m->flags & PG_ZERO) {
1322 vm_page_zero_count--;
1323 if (req & VM_ALLOC_ZERO)
1326 if (req & VM_ALLOC_NODUMP)
1329 mtx_unlock(&vm_page_queue_free_mtx);
1331 m->oflags = object == NULL || (object->flags & OBJ_UNMANAGED) != 0 ?
1333 if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ)) == 0)
1334 m->oflags |= VPO_BUSY;
1335 if (req & VM_ALLOC_WIRED) {
1337 * The page lock is not required for wiring a page until that
1338 * page is inserted into the object.
1340 atomic_add_int(&cnt.v_wire_count, 1);
1345 if (object != NULL) {
1346 /* Ignore device objects; the pager sets "memattr" for them. */
1347 if (object->memattr != VM_MEMATTR_DEFAULT &&
1348 (object->flags & OBJ_FICTITIOUS) == 0)
1349 pmap_page_set_memattr(m, object->memattr);
1350 vm_page_insert_after(m, object, pindex, mpred);
1355 * The following call to vdrop() must come after the above call
1356 * to vm_page_insert() in case both affect the same object and
1357 * vnode. Otherwise, the affected vnode's hold count could
1358 * temporarily become zero.
1364 * Don't wakeup too often - wakeup the pageout daemon when
1365 * we would be nearly out of memory.
1367 if (vm_paging_needed())
1368 pagedaemon_wakeup();
1374 * vm_page_alloc_contig:
1376 * Allocate a contiguous set of physical pages of the given size "npages"
1377 * from the free lists. All of the physical pages must be at or above
1378 * the given physical address "low" and below the given physical address
1379 * "high". The given value "alignment" determines the alignment of the
1380 * first physical page in the set. If the given value "boundary" is
1381 * non-zero, then the set of physical pages cannot cross any physical
1382 * address boundary that is a multiple of that value. Both "alignment"
1383 * and "boundary" must be a power of two.
1385 * If the specified memory attribute, "memattr", is VM_MEMATTR_DEFAULT,
1386 * then the memory attribute setting for the physical pages is configured
1387 * to the object's memory attribute setting. Otherwise, the memory
1388 * attribute setting for the physical pages is configured to "memattr",
1389 * overriding the object's memory attribute setting. However, if the
1390 * object's memory attribute setting is not VM_MEMATTR_DEFAULT, then the
1391 * memory attribute setting for the physical pages cannot be configured
1392 * to VM_MEMATTR_DEFAULT.
1394 * The caller must always specify an allocation class.
1396 * allocation classes:
1397 * VM_ALLOC_NORMAL normal process request
1398 * VM_ALLOC_SYSTEM system *really* needs a page
1399 * VM_ALLOC_INTERRUPT interrupt time request
1401 * optional allocation flags:
1402 * VM_ALLOC_NOBUSY do not set the flag VPO_BUSY on the page
1403 * VM_ALLOC_NOOBJ page is not associated with an object and
1404 * should not have the flag VPO_BUSY set
1405 * VM_ALLOC_WIRED wire the allocated page
1406 * VM_ALLOC_ZERO prefer a zeroed page
1408 * This routine may not sleep.
1411 vm_page_alloc_contig(vm_object_t object, vm_pindex_t pindex, int req,
1412 u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
1413 vm_paddr_t boundary, vm_memattr_t memattr)
1416 vm_page_t deferred_vdrop_list, m, m_ret;
1417 u_int flags, oflags;
1420 KASSERT((object != NULL) == ((req & VM_ALLOC_NOOBJ) == 0),
1421 ("vm_page_alloc_contig: inconsistent object/req"));
1422 if (object != NULL) {
1423 VM_OBJECT_ASSERT_WLOCKED(object);
1424 KASSERT(object->type == OBJT_PHYS,
1425 ("vm_page_alloc_contig: object %p isn't OBJT_PHYS",
1428 KASSERT(npages > 0, ("vm_page_alloc_contig: npages is zero"));
1429 req_class = req & VM_ALLOC_CLASS_MASK;
1432 * The page daemon is allowed to dig deeper into the free page list.
1434 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
1435 req_class = VM_ALLOC_SYSTEM;
1437 deferred_vdrop_list = NULL;
1438 mtx_lock(&vm_page_queue_free_mtx);
1439 if (cnt.v_free_count + cnt.v_cache_count >= npages +
1440 cnt.v_free_reserved || (req_class == VM_ALLOC_SYSTEM &&
1441 cnt.v_free_count + cnt.v_cache_count >= npages +
1442 cnt.v_interrupt_free_min) || (req_class == VM_ALLOC_INTERRUPT &&
1443 cnt.v_free_count + cnt.v_cache_count >= npages)) {
1444 #if VM_NRESERVLEVEL > 0
1446 if (object == NULL || (object->flags & OBJ_COLORED) == 0 ||
1447 (m_ret = vm_reserv_alloc_contig(object, pindex, npages,
1448 low, high, alignment, boundary)) == NULL)
1450 m_ret = vm_phys_alloc_contig(npages, low, high,
1451 alignment, boundary);
1453 mtx_unlock(&vm_page_queue_free_mtx);
1454 atomic_add_int(&vm_pageout_deficit, npages);
1455 pagedaemon_wakeup();
1459 for (m = m_ret; m < &m_ret[npages]; m++) {
1460 drop = vm_page_alloc_init(m);
1463 * Enqueue the vnode for deferred vdrop().
1465 * Once the pages are removed from the free
1466 * page list, "pageq" can be safely abused to
1467 * construct a short-lived list of vnodes.
1469 m->pageq.tqe_prev = (void *)drop;
1470 m->pageq.tqe_next = deferred_vdrop_list;
1471 deferred_vdrop_list = m;
1475 #if VM_NRESERVLEVEL > 0
1476 if (vm_reserv_reclaim_contig(npages, low, high, alignment,
1481 mtx_unlock(&vm_page_queue_free_mtx);
1486 * Initialize the pages. Only the PG_ZERO flag is inherited.
1489 if ((req & VM_ALLOC_ZERO) != 0)
1491 if ((req & VM_ALLOC_NODUMP) != 0)
1493 if ((req & VM_ALLOC_WIRED) != 0)
1494 atomic_add_int(&cnt.v_wire_count, npages);
1495 oflags = VPO_UNMANAGED;
1496 if (object != NULL) {
1497 if ((req & VM_ALLOC_NOBUSY) == 0)
1499 if (object->memattr != VM_MEMATTR_DEFAULT &&
1500 memattr == VM_MEMATTR_DEFAULT)
1501 memattr = object->memattr;
1503 for (m = m_ret; m < &m_ret[npages]; m++) {
1505 m->flags = (m->flags | PG_NODUMP) & flags;
1506 if ((req & VM_ALLOC_WIRED) != 0)
1508 /* Unmanaged pages don't use "act_count". */
1510 if (memattr != VM_MEMATTR_DEFAULT)
1511 pmap_page_set_memattr(m, memattr);
1513 vm_page_insert(m, object, pindex);
1518 while (deferred_vdrop_list != NULL) {
1519 vdrop((struct vnode *)deferred_vdrop_list->pageq.tqe_prev);
1520 deferred_vdrop_list = deferred_vdrop_list->pageq.tqe_next;
1522 if (vm_paging_needed())
1523 pagedaemon_wakeup();
1528 * Initialize a page that has been freshly dequeued from a freelist.
1529 * The caller has to drop the vnode returned, if it is not NULL.
1531 * This function may only be used to initialize unmanaged pages.
1533 * To be called with vm_page_queue_free_mtx held.
1535 static struct vnode *
1536 vm_page_alloc_init(vm_page_t m)
1539 vm_object_t m_object;
1541 KASSERT(m->queue == PQ_NONE,
1542 ("vm_page_alloc_init: page %p has unexpected queue %d",
1544 KASSERT(m->wire_count == 0,
1545 ("vm_page_alloc_init: page %p is wired", m));
1546 KASSERT(m->hold_count == 0,
1547 ("vm_page_alloc_init: page %p is held", m));
1548 KASSERT(m->busy == 0,
1549 ("vm_page_alloc_init: page %p is busy", m));
1550 KASSERT(m->dirty == 0,
1551 ("vm_page_alloc_init: page %p is dirty", m));
1552 KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT,
1553 ("vm_page_alloc_init: page %p has unexpected memattr %d",
1554 m, pmap_page_get_memattr(m)));
1555 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1557 if ((m->flags & PG_CACHED) != 0) {
1558 KASSERT((m->flags & PG_ZERO) == 0,
1559 ("vm_page_alloc_init: cached page %p is PG_ZERO", m));
1561 m_object = m->object;
1562 vm_page_cache_remove(m);
1563 if (m_object->type == OBJT_VNODE &&
1564 vm_object_cache_is_empty(m_object))
1565 drop = m_object->handle;
1567 KASSERT(VM_PAGE_IS_FREE(m),
1568 ("vm_page_alloc_init: page %p is not free", m));
1569 KASSERT(m->valid == 0,
1570 ("vm_page_alloc_init: free page %p is valid", m));
1572 if ((m->flags & PG_ZERO) != 0)
1573 vm_page_zero_count--;
1575 /* Don't clear the PG_ZERO flag; we'll need it later. */
1576 m->flags &= PG_ZERO;
1581 * vm_page_alloc_freelist:
1583 * Allocate a physical page from the specified free page list.
1585 * The caller must always specify an allocation class.
1587 * allocation classes:
1588 * VM_ALLOC_NORMAL normal process request
1589 * VM_ALLOC_SYSTEM system *really* needs a page
1590 * VM_ALLOC_INTERRUPT interrupt time request
1592 * optional allocation flags:
1593 * VM_ALLOC_COUNT(number) the number of additional pages that the caller
1594 * intends to allocate
1595 * VM_ALLOC_WIRED wire the allocated page
1596 * VM_ALLOC_ZERO prefer a zeroed page
1598 * This routine may not sleep.
1601 vm_page_alloc_freelist(int flind, int req)
1608 req_class = req & VM_ALLOC_CLASS_MASK;
1611 * The page daemon is allowed to dig deeper into the free page list.
1613 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
1614 req_class = VM_ALLOC_SYSTEM;
1617 * Do not allocate reserved pages unless the req has asked for it.
1619 mtx_lock(&vm_page_queue_free_mtx);
1620 if (cnt.v_free_count + cnt.v_cache_count > cnt.v_free_reserved ||
1621 (req_class == VM_ALLOC_SYSTEM &&
1622 cnt.v_free_count + cnt.v_cache_count > cnt.v_interrupt_free_min) ||
1623 (req_class == VM_ALLOC_INTERRUPT &&
1624 cnt.v_free_count + cnt.v_cache_count > 0))
1625 m = vm_phys_alloc_freelist_pages(flind, VM_FREEPOOL_DIRECT, 0);
1627 mtx_unlock(&vm_page_queue_free_mtx);
1628 atomic_add_int(&vm_pageout_deficit,
1629 max((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1));
1630 pagedaemon_wakeup();
1634 mtx_unlock(&vm_page_queue_free_mtx);
1637 drop = vm_page_alloc_init(m);
1638 mtx_unlock(&vm_page_queue_free_mtx);
1641 * Initialize the page. Only the PG_ZERO flag is inherited.
1645 if ((req & VM_ALLOC_ZERO) != 0)
1648 if ((req & VM_ALLOC_WIRED) != 0) {
1650 * The page lock is not required for wiring a page that does
1651 * not belong to an object.
1653 atomic_add_int(&cnt.v_wire_count, 1);
1656 /* Unmanaged pages don't use "act_count". */
1657 m->oflags = VPO_UNMANAGED;
1660 if (vm_paging_needed())
1661 pagedaemon_wakeup();
1666 * vm_wait: (also see VM_WAIT macro)
1668 * Sleep until free pages are available for allocation.
1669 * - Called in various places before memory allocations.
1675 mtx_lock(&vm_page_queue_free_mtx);
1676 if (curproc == pageproc) {
1677 vm_pageout_pages_needed = 1;
1678 msleep(&vm_pageout_pages_needed, &vm_page_queue_free_mtx,
1679 PDROP | PSWP, "VMWait", 0);
1681 if (!vm_pages_needed) {
1682 vm_pages_needed = 1;
1683 wakeup(&vm_pages_needed);
1685 msleep(&cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PVM,
1691 * vm_waitpfault: (also see VM_WAITPFAULT macro)
1693 * Sleep until free pages are available for allocation.
1694 * - Called only in vm_fault so that processes page faulting
1695 * can be easily tracked.
1696 * - Sleeps at a lower priority than vm_wait() so that vm_wait()ing
1697 * processes will be able to grab memory first. Do not change
1698 * this balance without careful testing first.
1704 mtx_lock(&vm_page_queue_free_mtx);
1705 if (!vm_pages_needed) {
1706 vm_pages_needed = 1;
1707 wakeup(&vm_pages_needed);
1709 msleep(&cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PUSER,
1716 * Remove the given page from its current page queue.
1718 * The page must be locked.
1721 vm_page_dequeue(vm_page_t m)
1723 struct vm_pagequeue *pq;
1725 vm_page_lock_assert(m, MA_OWNED);
1726 KASSERT(m->queue != PQ_NONE,
1727 ("vm_page_dequeue: page %p is not queued", m));
1728 pq = &vm_pagequeues[m->queue];
1729 vm_pagequeue_lock(pq);
1731 TAILQ_REMOVE(&pq->pq_pl, m, pageq);
1733 vm_pagequeue_unlock(pq);
1737 * vm_page_dequeue_locked:
1739 * Remove the given page from its current page queue.
1741 * The page and page queue must be locked.
1744 vm_page_dequeue_locked(vm_page_t m)
1746 struct vm_pagequeue *pq;
1748 vm_page_lock_assert(m, MA_OWNED);
1749 pq = &vm_pagequeues[m->queue];
1750 vm_pagequeue_assert_locked(pq);
1752 TAILQ_REMOVE(&pq->pq_pl, m, pageq);
1759 * Add the given page to the specified page queue.
1761 * The page must be locked.
1764 vm_page_enqueue(int queue, vm_page_t m)
1766 struct vm_pagequeue *pq;
1768 vm_page_lock_assert(m, MA_OWNED);
1769 pq = &vm_pagequeues[queue];
1770 vm_pagequeue_lock(pq);
1772 TAILQ_INSERT_TAIL(&pq->pq_pl, m, pageq);
1774 vm_pagequeue_unlock(pq);
1780 * Move the given page to the tail of its current page queue.
1782 * The page must be locked.
1785 vm_page_requeue(vm_page_t m)
1787 struct vm_pagequeue *pq;
1789 vm_page_lock_assert(m, MA_OWNED);
1790 KASSERT(m->queue != PQ_NONE,
1791 ("vm_page_requeue: page %p is not queued", m));
1792 pq = &vm_pagequeues[m->queue];
1793 vm_pagequeue_lock(pq);
1794 TAILQ_REMOVE(&pq->pq_pl, m, pageq);
1795 TAILQ_INSERT_TAIL(&pq->pq_pl, m, pageq);
1796 vm_pagequeue_unlock(pq);
1800 * vm_page_requeue_locked:
1802 * Move the given page to the tail of its current page queue.
1804 * The page queue must be locked.
1807 vm_page_requeue_locked(vm_page_t m)
1809 struct vm_pagequeue *pq;
1811 KASSERT(m->queue != PQ_NONE,
1812 ("vm_page_requeue_locked: page %p is not queued", m));
1813 pq = &vm_pagequeues[m->queue];
1814 vm_pagequeue_assert_locked(pq);
1815 TAILQ_REMOVE(&pq->pq_pl, m, pageq);
1816 TAILQ_INSERT_TAIL(&pq->pq_pl, m, pageq);
1822 * Put the specified page on the active list (if appropriate).
1823 * Ensure that act_count is at least ACT_INIT but do not otherwise
1826 * The page must be locked.
1829 vm_page_activate(vm_page_t m)
1833 vm_page_lock_assert(m, MA_OWNED);
1834 if ((queue = m->queue) != PQ_ACTIVE) {
1835 if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) {
1836 if (m->act_count < ACT_INIT)
1837 m->act_count = ACT_INIT;
1838 if (queue != PQ_NONE)
1840 vm_page_enqueue(PQ_ACTIVE, m);
1842 KASSERT(queue == PQ_NONE,
1843 ("vm_page_activate: wired page %p is queued", m));
1845 if (m->act_count < ACT_INIT)
1846 m->act_count = ACT_INIT;
1851 * vm_page_free_wakeup:
1853 * Helper routine for vm_page_free_toq() and vm_page_cache(). This
1854 * routine is called when a page has been added to the cache or free
1857 * The page queues must be locked.
1860 vm_page_free_wakeup(void)
1863 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1865 * if pageout daemon needs pages, then tell it that there are
1868 if (vm_pageout_pages_needed &&
1869 cnt.v_cache_count + cnt.v_free_count >= cnt.v_pageout_free_min) {
1870 wakeup(&vm_pageout_pages_needed);
1871 vm_pageout_pages_needed = 0;
1874 * wakeup processes that are waiting on memory if we hit a
1875 * high water mark. And wakeup scheduler process if we have
1876 * lots of memory. this process will swapin processes.
1878 if (vm_pages_needed && !vm_page_count_min()) {
1879 vm_pages_needed = 0;
1880 wakeup(&cnt.v_free_count);
1887 * Returns the given page to the free list,
1888 * disassociating it with any VM object.
1890 * The object must be locked. The page must be locked if it is managed.
1893 vm_page_free_toq(vm_page_t m)
1896 if ((m->oflags & VPO_UNMANAGED) == 0) {
1897 vm_page_lock_assert(m, MA_OWNED);
1898 KASSERT(!pmap_page_is_mapped(m),
1899 ("vm_page_free_toq: freeing mapped page %p", m));
1901 KASSERT(m->queue == PQ_NONE,
1902 ("vm_page_free_toq: unmanaged page %p is queued", m));
1903 PCPU_INC(cnt.v_tfree);
1905 if (VM_PAGE_IS_FREE(m))
1906 panic("vm_page_free: freeing free page %p", m);
1907 else if (m->busy != 0)
1908 panic("vm_page_free: freeing busy page %p", m);
1911 * Unqueue, then remove page. Note that we cannot destroy
1912 * the page here because we do not want to call the pager's
1913 * callback routine until after we've put the page on the
1914 * appropriate free queue.
1920 * If fictitious remove object association and
1921 * return, otherwise delay object association removal.
1923 if ((m->flags & PG_FICTITIOUS) != 0) {
1930 if (m->wire_count != 0)
1931 panic("vm_page_free: freeing wired page %p", m);
1932 if (m->hold_count != 0) {
1933 m->flags &= ~PG_ZERO;
1934 KASSERT((m->flags & PG_UNHOLDFREE) == 0,
1935 ("vm_page_free: freeing PG_UNHOLDFREE page %p", m));
1936 m->flags |= PG_UNHOLDFREE;
1939 * Restore the default memory attribute to the page.
1941 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT)
1942 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT);
1945 * Insert the page into the physical memory allocator's
1946 * cache/free page queues.
1948 mtx_lock(&vm_page_queue_free_mtx);
1949 m->flags |= PG_FREE;
1951 #if VM_NRESERVLEVEL > 0
1952 if (!vm_reserv_free_page(m))
1956 vm_phys_free_pages(m, 0);
1957 if ((m->flags & PG_ZERO) != 0)
1958 ++vm_page_zero_count;
1960 vm_page_zero_idle_wakeup();
1961 vm_page_free_wakeup();
1962 mtx_unlock(&vm_page_queue_free_mtx);
1969 * Mark this page as wired down by yet
1970 * another map, removing it from paging queues
1973 * If the page is fictitious, then its wire count must remain one.
1975 * The page must be locked.
1978 vm_page_wire(vm_page_t m)
1982 * Only bump the wire statistics if the page is not already wired,
1983 * and only unqueue the page if it is on some queue (if it is unmanaged
1984 * it is already off the queues).
1986 vm_page_lock_assert(m, MA_OWNED);
1987 if ((m->flags & PG_FICTITIOUS) != 0) {
1988 KASSERT(m->wire_count == 1,
1989 ("vm_page_wire: fictitious page %p's wire count isn't one",
1993 if (m->wire_count == 0) {
1994 KASSERT((m->oflags & VPO_UNMANAGED) == 0 ||
1995 m->queue == PQ_NONE,
1996 ("vm_page_wire: unmanaged page %p is queued", m));
1998 atomic_add_int(&cnt.v_wire_count, 1);
2001 KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m));
2007 * Release one wiring of the specified page, potentially enabling it to be
2008 * paged again. If paging is enabled, then the value of the parameter
2009 * "activate" determines to which queue the page is added. If "activate" is
2010 * non-zero, then the page is added to the active queue. Otherwise, it is
2011 * added to the inactive queue.
2013 * However, unless the page belongs to an object, it is not enqueued because
2014 * it cannot be paged out.
2016 * If a page is fictitious, then its wire count must alway be one.
2018 * A managed page must be locked.
2021 vm_page_unwire(vm_page_t m, int activate)
2024 if ((m->oflags & VPO_UNMANAGED) == 0)
2025 vm_page_lock_assert(m, MA_OWNED);
2026 if ((m->flags & PG_FICTITIOUS) != 0) {
2027 KASSERT(m->wire_count == 1,
2028 ("vm_page_unwire: fictitious page %p's wire count isn't one", m));
2031 if (m->wire_count > 0) {
2033 if (m->wire_count == 0) {
2034 atomic_subtract_int(&cnt.v_wire_count, 1);
2035 if ((m->oflags & VPO_UNMANAGED) != 0 ||
2039 m->flags &= ~PG_WINATCFLS;
2040 vm_page_enqueue(activate ? PQ_ACTIVE : PQ_INACTIVE, m);
2043 panic("vm_page_unwire: page %p's wire count is zero", m);
2047 * Move the specified page to the inactive queue.
2049 * Many pages placed on the inactive queue should actually go
2050 * into the cache, but it is difficult to figure out which. What
2051 * we do instead, if the inactive target is well met, is to put
2052 * clean pages at the head of the inactive queue instead of the tail.
2053 * This will cause them to be moved to the cache more quickly and
2054 * if not actively re-referenced, reclaimed more quickly. If we just
2055 * stick these pages at the end of the inactive queue, heavy filesystem
2056 * meta-data accesses can cause an unnecessary paging load on memory bound
2057 * processes. This optimization causes one-time-use metadata to be
2058 * reused more quickly.
2060 * Normally athead is 0 resulting in LRU operation. athead is set
2061 * to 1 if we want this page to be 'as if it were placed in the cache',
2062 * except without unmapping it from the process address space.
2064 * The page must be locked.
2067 _vm_page_deactivate(vm_page_t m, int athead)
2069 struct vm_pagequeue *pq;
2072 vm_page_lock_assert(m, MA_OWNED);
2075 * Ignore if already inactive.
2077 if ((queue = m->queue) == PQ_INACTIVE)
2079 if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) {
2080 if (queue != PQ_NONE)
2082 m->flags &= ~PG_WINATCFLS;
2083 pq = &vm_pagequeues[PQ_INACTIVE];
2084 vm_pagequeue_lock(pq);
2085 m->queue = PQ_INACTIVE;
2087 TAILQ_INSERT_HEAD(&pq->pq_pl, m, pageq);
2089 TAILQ_INSERT_TAIL(&pq->pq_pl, m, pageq);
2090 cnt.v_inactive_count++;
2091 vm_pagequeue_unlock(pq);
2096 * Move the specified page to the inactive queue.
2098 * The page must be locked.
2101 vm_page_deactivate(vm_page_t m)
2104 _vm_page_deactivate(m, 0);
2108 * vm_page_try_to_cache:
2110 * Returns 0 on failure, 1 on success
2113 vm_page_try_to_cache(vm_page_t m)
2116 vm_page_lock_assert(m, MA_OWNED);
2117 VM_OBJECT_ASSERT_WLOCKED(m->object);
2118 if (m->dirty || m->hold_count || m->busy || m->wire_count ||
2119 (m->oflags & (VPO_BUSY | VPO_UNMANAGED)) != 0)
2129 * vm_page_try_to_free()
2131 * Attempt to free the page. If we cannot free it, we do nothing.
2132 * 1 is returned on success, 0 on failure.
2135 vm_page_try_to_free(vm_page_t m)
2138 vm_page_lock_assert(m, MA_OWNED);
2139 if (m->object != NULL)
2140 VM_OBJECT_ASSERT_WLOCKED(m->object);
2141 if (m->dirty || m->hold_count || m->busy || m->wire_count ||
2142 (m->oflags & (VPO_BUSY | VPO_UNMANAGED)) != 0)
2154 * Put the specified page onto the page cache queue (if appropriate).
2156 * The object and page must be locked.
2159 vm_page_cache(vm_page_t m)
2162 boolean_t cache_was_empty;
2164 vm_page_lock_assert(m, MA_OWNED);
2166 VM_OBJECT_ASSERT_WLOCKED(object);
2167 if ((m->oflags & (VPO_UNMANAGED | VPO_BUSY)) || m->busy ||
2168 m->hold_count || m->wire_count)
2169 panic("vm_page_cache: attempting to cache busy page");
2170 KASSERT(!pmap_page_is_mapped(m),
2171 ("vm_page_cache: page %p is mapped", m));
2172 KASSERT(m->dirty == 0, ("vm_page_cache: page %p is dirty", m));
2173 if (m->valid == 0 || object->type == OBJT_DEFAULT ||
2174 (object->type == OBJT_SWAP &&
2175 !vm_pager_has_page(object, m->pindex, NULL, NULL))) {
2177 * Hypothesis: A cache-elgible page belonging to a
2178 * default object or swap object but without a backing
2179 * store must be zero filled.
2184 KASSERT((m->flags & PG_CACHED) == 0,
2185 ("vm_page_cache: page %p is already cached", m));
2186 PCPU_INC(cnt.v_tcached);
2189 * Remove the page from the paging queues.
2194 * Remove the page from the object's collection of resident
2197 vm_radix_remove(&object->rtree, m->pindex);
2198 TAILQ_REMOVE(&object->memq, m, listq);
2199 object->resident_page_count--;
2202 * Restore the default memory attribute to the page.
2204 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT)
2205 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT);
2208 * Insert the page into the object's collection of cached pages
2209 * and the physical memory allocator's cache/free page queues.
2211 m->flags &= ~PG_ZERO;
2212 mtx_lock(&vm_page_queue_free_mtx);
2213 m->flags |= PG_CACHED;
2214 cnt.v_cache_count++;
2215 cache_was_empty = vm_radix_is_empty(&object->cache);
2216 vm_radix_insert(&object->cache, m);
2217 #if VM_NRESERVLEVEL > 0
2218 if (!vm_reserv_free_page(m)) {
2222 vm_phys_set_pool(VM_FREEPOOL_CACHE, m, 0);
2223 vm_phys_free_pages(m, 0);
2225 vm_page_free_wakeup();
2226 mtx_unlock(&vm_page_queue_free_mtx);
2229 * Increment the vnode's hold count if this is the object's only
2230 * cached page. Decrement the vnode's hold count if this was
2231 * the object's only resident page.
2233 if (object->type == OBJT_VNODE) {
2234 if (cache_was_empty && object->resident_page_count != 0)
2235 vhold(object->handle);
2236 else if (!cache_was_empty && object->resident_page_count == 0)
2237 vdrop(object->handle);
2244 * Cache, deactivate, or do nothing as appropriate. This routine
2245 * is used by madvise().
2247 * Generally speaking we want to move the page into the cache so
2248 * it gets reused quickly. However, this can result in a silly syndrome
2249 * due to the page recycling too quickly. Small objects will not be
2250 * fully cached. On the other hand, if we move the page to the inactive
2251 * queue we wind up with a problem whereby very large objects
2252 * unnecessarily blow away our inactive and cache queues.
2254 * The solution is to move the pages based on a fixed weighting. We
2255 * either leave them alone, deactivate them, or move them to the cache,
2256 * where moving them to the cache has the highest weighting.
2257 * By forcing some pages into other queues we eventually force the
2258 * system to balance the queues, potentially recovering other unrelated
2259 * space from active. The idea is to not force this to happen too
2262 * The object and page must be locked.
2265 vm_page_advise(vm_page_t m, int advice)
2269 vm_page_assert_locked(m);
2270 VM_OBJECT_ASSERT_WLOCKED(m->object);
2271 if (advice == MADV_FREE) {
2273 * Mark the page clean. This will allow the page to be freed
2274 * up by the system. However, such pages are often reused
2275 * quickly by malloc() so we do not do anything that would
2276 * cause a page fault if we can help it.
2278 * Specifically, we do not try to actually free the page now
2279 * nor do we try to put it in the cache (which would cause a
2280 * page fault on reuse).
2282 * But we do make the page is freeable as we can without
2283 * actually taking the step of unmapping it.
2285 pmap_clear_modify(m);
2288 } else if (advice != MADV_DONTNEED)
2290 dnw = PCPU_GET(dnweight);
2294 * Occasionally leave the page alone.
2296 if ((dnw & 0x01F0) == 0 || m->queue == PQ_INACTIVE) {
2297 if (m->act_count >= ACT_INIT)
2303 * Clear any references to the page. Otherwise, the page daemon will
2304 * immediately reactivate the page.
2306 * Perform the pmap_clear_reference() first. Otherwise, a concurrent
2307 * pmap operation, such as pmap_remove(), could clear a reference in
2308 * the pmap and set PGA_REFERENCED on the page before the
2309 * pmap_clear_reference() had completed. Consequently, the page would
2310 * appear referenced based upon an old reference that occurred before
2311 * this function ran.
2313 pmap_clear_reference(m);
2314 vm_page_aflag_clear(m, PGA_REFERENCED);
2316 if (advice != MADV_FREE && m->dirty == 0 && pmap_is_modified(m))
2319 if (m->dirty || (dnw & 0x0070) == 0) {
2321 * Deactivate the page 3 times out of 32.
2326 * Cache the page 28 times out of every 32. Note that
2327 * the page is deactivated instead of cached, but placed
2328 * at the head of the queue instead of the tail.
2332 _vm_page_deactivate(m, head);
2336 * Grab a page, waiting until we are waken up due to the page
2337 * changing state. We keep on waiting, if the page continues
2338 * to be in the object. If the page doesn't exist, first allocate it
2339 * and then conditionally zero it.
2341 * The caller must always specify the VM_ALLOC_RETRY flag. This is intended
2342 * to facilitate its eventual removal.
2344 * This routine may sleep.
2346 * The object must be locked on entry. The lock will, however, be released
2347 * and reacquired if the routine sleeps.
2350 vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags)
2354 VM_OBJECT_ASSERT_WLOCKED(object);
2355 KASSERT((allocflags & VM_ALLOC_RETRY) != 0,
2356 ("vm_page_grab: VM_ALLOC_RETRY is required"));
2358 if ((m = vm_page_lookup(object, pindex)) != NULL) {
2359 if ((m->oflags & VPO_BUSY) != 0 ||
2360 ((allocflags & VM_ALLOC_IGN_SBUSY) == 0 && m->busy != 0)) {
2362 * Reference the page before unlocking and
2363 * sleeping so that the page daemon is less
2364 * likely to reclaim it.
2366 vm_page_aflag_set(m, PGA_REFERENCED);
2367 vm_page_sleep(m, "pgrbwt");
2370 if ((allocflags & VM_ALLOC_WIRED) != 0) {
2375 if ((allocflags & VM_ALLOC_NOBUSY) == 0)
2380 m = vm_page_alloc(object, pindex, allocflags & ~(VM_ALLOC_RETRY |
2381 VM_ALLOC_IGN_SBUSY));
2383 VM_OBJECT_WUNLOCK(object);
2385 VM_OBJECT_WLOCK(object);
2387 } else if (m->valid != 0)
2389 if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0)
2395 * Mapping function for valid or dirty bits in a page.
2397 * Inputs are required to range within a page.
2400 vm_page_bits(int base, int size)
2406 base + size <= PAGE_SIZE,
2407 ("vm_page_bits: illegal base/size %d/%d", base, size)
2410 if (size == 0) /* handle degenerate case */
2413 first_bit = base >> DEV_BSHIFT;
2414 last_bit = (base + size - 1) >> DEV_BSHIFT;
2416 return (((vm_page_bits_t)2 << last_bit) -
2417 ((vm_page_bits_t)1 << first_bit));
2421 * vm_page_set_valid_range:
2423 * Sets portions of a page valid. The arguments are expected
2424 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
2425 * of any partial chunks touched by the range. The invalid portion of
2426 * such chunks will be zeroed.
2428 * (base + size) must be less then or equal to PAGE_SIZE.
2431 vm_page_set_valid_range(vm_page_t m, int base, int size)
2435 VM_OBJECT_ASSERT_WLOCKED(m->object);
2436 if (size == 0) /* handle degenerate case */
2440 * If the base is not DEV_BSIZE aligned and the valid
2441 * bit is clear, we have to zero out a portion of the
2444 if ((frag = base & ~(DEV_BSIZE - 1)) != base &&
2445 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0)
2446 pmap_zero_page_area(m, frag, base - frag);
2449 * If the ending offset is not DEV_BSIZE aligned and the
2450 * valid bit is clear, we have to zero out a portion of
2453 endoff = base + size;
2454 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff &&
2455 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0)
2456 pmap_zero_page_area(m, endoff,
2457 DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
2460 * Assert that no previously invalid block that is now being validated
2463 KASSERT((~m->valid & vm_page_bits(base, size) & m->dirty) == 0,
2464 ("vm_page_set_valid_range: page %p is dirty", m));
2467 * Set valid bits inclusive of any overlap.
2469 m->valid |= vm_page_bits(base, size);
2473 * Clear the given bits from the specified page's dirty field.
2475 static __inline void
2476 vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits)
2479 #if PAGE_SIZE < 16384
2484 * If the object is locked and the page is neither VPO_BUSY nor
2485 * write mapped, then the page's dirty field cannot possibly be
2486 * set by a concurrent pmap operation.
2488 VM_OBJECT_ASSERT_WLOCKED(m->object);
2489 if ((m->oflags & VPO_BUSY) == 0 && !pmap_page_is_write_mapped(m))
2490 m->dirty &= ~pagebits;
2493 * The pmap layer can call vm_page_dirty() without
2494 * holding a distinguished lock. The combination of
2495 * the object's lock and an atomic operation suffice
2496 * to guarantee consistency of the page dirty field.
2498 * For PAGE_SIZE == 32768 case, compiler already
2499 * properly aligns the dirty field, so no forcible
2500 * alignment is needed. Only require existence of
2501 * atomic_clear_64 when page size is 32768.
2503 addr = (uintptr_t)&m->dirty;
2504 #if PAGE_SIZE == 32768
2505 atomic_clear_64((uint64_t *)addr, pagebits);
2506 #elif PAGE_SIZE == 16384
2507 atomic_clear_32((uint32_t *)addr, pagebits);
2508 #else /* PAGE_SIZE <= 8192 */
2510 * Use a trick to perform a 32-bit atomic on the
2511 * containing aligned word, to not depend on the existence
2512 * of atomic_clear_{8, 16}.
2514 shift = addr & (sizeof(uint32_t) - 1);
2515 #if BYTE_ORDER == BIG_ENDIAN
2516 shift = (sizeof(uint32_t) - sizeof(m->dirty) - shift) * NBBY;
2520 addr &= ~(sizeof(uint32_t) - 1);
2521 atomic_clear_32((uint32_t *)addr, pagebits << shift);
2522 #endif /* PAGE_SIZE */
2527 * vm_page_set_validclean:
2529 * Sets portions of a page valid and clean. The arguments are expected
2530 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
2531 * of any partial chunks touched by the range. The invalid portion of
2532 * such chunks will be zero'd.
2534 * (base + size) must be less then or equal to PAGE_SIZE.
2537 vm_page_set_validclean(vm_page_t m, int base, int size)
2539 vm_page_bits_t oldvalid, pagebits;
2542 VM_OBJECT_ASSERT_WLOCKED(m->object);
2543 if (size == 0) /* handle degenerate case */
2547 * If the base is not DEV_BSIZE aligned and the valid
2548 * bit is clear, we have to zero out a portion of the
2551 if ((frag = base & ~(DEV_BSIZE - 1)) != base &&
2552 (m->valid & ((vm_page_bits_t)1 << (base >> DEV_BSHIFT))) == 0)
2553 pmap_zero_page_area(m, frag, base - frag);
2556 * If the ending offset is not DEV_BSIZE aligned and the
2557 * valid bit is clear, we have to zero out a portion of
2560 endoff = base + size;
2561 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff &&
2562 (m->valid & ((vm_page_bits_t)1 << (endoff >> DEV_BSHIFT))) == 0)
2563 pmap_zero_page_area(m, endoff,
2564 DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
2567 * Set valid, clear dirty bits. If validating the entire
2568 * page we can safely clear the pmap modify bit. We also
2569 * use this opportunity to clear the VPO_NOSYNC flag. If a process
2570 * takes a write fault on a MAP_NOSYNC memory area the flag will
2573 * We set valid bits inclusive of any overlap, but we can only
2574 * clear dirty bits for DEV_BSIZE chunks that are fully within
2577 oldvalid = m->valid;
2578 pagebits = vm_page_bits(base, size);
2579 m->valid |= pagebits;
2581 if ((frag = base & (DEV_BSIZE - 1)) != 0) {
2582 frag = DEV_BSIZE - frag;
2588 pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1));
2590 if (base == 0 && size == PAGE_SIZE) {
2592 * The page can only be modified within the pmap if it is
2593 * mapped, and it can only be mapped if it was previously
2596 if (oldvalid == VM_PAGE_BITS_ALL)
2598 * Perform the pmap_clear_modify() first. Otherwise,
2599 * a concurrent pmap operation, such as
2600 * pmap_protect(), could clear a modification in the
2601 * pmap and set the dirty field on the page before
2602 * pmap_clear_modify() had begun and after the dirty
2603 * field was cleared here.
2605 pmap_clear_modify(m);
2607 m->oflags &= ~VPO_NOSYNC;
2608 } else if (oldvalid != VM_PAGE_BITS_ALL)
2609 m->dirty &= ~pagebits;
2611 vm_page_clear_dirty_mask(m, pagebits);
2615 vm_page_clear_dirty(vm_page_t m, int base, int size)
2618 vm_page_clear_dirty_mask(m, vm_page_bits(base, size));
2622 * vm_page_set_invalid:
2624 * Invalidates DEV_BSIZE'd chunks within a page. Both the
2625 * valid and dirty bits for the effected areas are cleared.
2628 vm_page_set_invalid(vm_page_t m, int base, int size)
2630 vm_page_bits_t bits;
2632 VM_OBJECT_ASSERT_WLOCKED(m->object);
2633 KASSERT((m->oflags & VPO_BUSY) == 0,
2634 ("vm_page_set_invalid: page %p is busy", m));
2635 bits = vm_page_bits(base, size);
2636 if (m->valid == VM_PAGE_BITS_ALL && bits != 0)
2638 KASSERT(!pmap_page_is_mapped(m),
2639 ("vm_page_set_invalid: page %p is mapped", m));
2645 * vm_page_zero_invalid()
2647 * The kernel assumes that the invalid portions of a page contain
2648 * garbage, but such pages can be mapped into memory by user code.
2649 * When this occurs, we must zero out the non-valid portions of the
2650 * page so user code sees what it expects.
2652 * Pages are most often semi-valid when the end of a file is mapped
2653 * into memory and the file's size is not page aligned.
2656 vm_page_zero_invalid(vm_page_t m, boolean_t setvalid)
2661 VM_OBJECT_ASSERT_WLOCKED(m->object);
2663 * Scan the valid bits looking for invalid sections that
2664 * must be zerod. Invalid sub-DEV_BSIZE'd areas ( where the
2665 * valid bit may be set ) have already been zerod by
2666 * vm_page_set_validclean().
2668 for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) {
2669 if (i == (PAGE_SIZE / DEV_BSIZE) ||
2670 (m->valid & ((vm_page_bits_t)1 << i))) {
2672 pmap_zero_page_area(m,
2673 b << DEV_BSHIFT, (i - b) << DEV_BSHIFT);
2680 * setvalid is TRUE when we can safely set the zero'd areas
2681 * as being valid. We can do this if there are no cache consistancy
2682 * issues. e.g. it is ok to do with UFS, but not ok to do with NFS.
2685 m->valid = VM_PAGE_BITS_ALL;
2691 * Is (partial) page valid? Note that the case where size == 0
2692 * will return FALSE in the degenerate case where the page is
2693 * entirely invalid, and TRUE otherwise.
2696 vm_page_is_valid(vm_page_t m, int base, int size)
2698 vm_page_bits_t bits;
2700 VM_OBJECT_ASSERT_WLOCKED(m->object);
2701 bits = vm_page_bits(base, size);
2702 return (m->valid != 0 && (m->valid & bits) == bits);
2706 * Set the page's dirty bits if the page is modified.
2709 vm_page_test_dirty(vm_page_t m)
2712 VM_OBJECT_ASSERT_WLOCKED(m->object);
2713 if (m->dirty != VM_PAGE_BITS_ALL && pmap_is_modified(m))
2718 vm_page_lock_KBI(vm_page_t m, const char *file, int line)
2721 mtx_lock_flags_(vm_page_lockptr(m), 0, file, line);
2725 vm_page_unlock_KBI(vm_page_t m, const char *file, int line)
2728 mtx_unlock_flags_(vm_page_lockptr(m), 0, file, line);
2732 vm_page_trylock_KBI(vm_page_t m, const char *file, int line)
2735 return (mtx_trylock_flags_(vm_page_lockptr(m), 0, file, line));
2738 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
2740 vm_page_assert_locked_KBI(vm_page_t m, const char *file, int line)
2743 vm_page_lock_assert_KBI(m, MA_OWNED, file, line);
2747 vm_page_lock_assert_KBI(vm_page_t m, int a, const char *file, int line)
2750 mtx_assert_(vm_page_lockptr(m), a, file, line);
2754 int so_zerocp_fullpage = 0;
2757 * Replace the given page with a copy. The copied page assumes
2758 * the portion of the given page's "wire_count" that is not the
2759 * responsibility of this copy-on-write mechanism.
2761 * The object containing the given page must have a non-zero
2762 * paging-in-progress count and be locked.
2765 vm_page_cowfault(vm_page_t m)
2771 vm_page_lock_assert(m, MA_OWNED);
2773 VM_OBJECT_ASSERT_WLOCKED(object);
2774 KASSERT(object->paging_in_progress != 0,
2775 ("vm_page_cowfault: object %p's paging-in-progress count is zero.",
2782 mnew = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL | VM_ALLOC_NOBUSY);
2784 vm_page_insert(m, object, pindex);
2786 VM_OBJECT_WUNLOCK(object);
2788 VM_OBJECT_WLOCK(object);
2789 if (m == vm_page_lookup(object, pindex)) {
2794 * Page disappeared during the wait.
2802 * check to see if we raced with an xmit complete when
2803 * waiting to allocate a page. If so, put things back
2809 vm_page_unlock(mnew);
2810 vm_page_insert(m, object, pindex);
2811 } else { /* clear COW & copy page */
2812 if (!so_zerocp_fullpage)
2813 pmap_copy_page(m, mnew);
2814 mnew->valid = VM_PAGE_BITS_ALL;
2815 vm_page_dirty(mnew);
2816 mnew->wire_count = m->wire_count - m->cow;
2817 m->wire_count = m->cow;
2823 vm_page_cowclear(vm_page_t m)
2826 vm_page_lock_assert(m, MA_OWNED);
2830 * let vm_fault add back write permission lazily
2834 * sf_buf_free() will free the page, so we needn't do it here
2839 vm_page_cowsetup(vm_page_t m)
2842 vm_page_lock_assert(m, MA_OWNED);
2843 if ((m->flags & PG_FICTITIOUS) != 0 ||
2844 (m->oflags & VPO_UNMANAGED) != 0 ||
2845 m->cow == USHRT_MAX - 1 || !VM_OBJECT_TRYWLOCK(m->object))
2848 pmap_remove_write(m);
2849 VM_OBJECT_WUNLOCK(m->object);
2855 vm_page_object_lock_assert(vm_page_t m)
2859 * Certain of the page's fields may only be modified by the
2860 * holder of the containing object's lock or the setter of the
2861 * page's VPO_BUSY flag. Unfortunately, the setter of the
2862 * VPO_BUSY flag is not recorded, and thus cannot be checked
2865 if (m->object != NULL && (m->oflags & VPO_BUSY) == 0)
2866 VM_OBJECT_ASSERT_WLOCKED(m->object);
2870 #include "opt_ddb.h"
2872 #include <sys/kernel.h>
2874 #include <ddb/ddb.h>
2876 DB_SHOW_COMMAND(page, vm_page_print_page_info)
2878 db_printf("cnt.v_free_count: %d\n", cnt.v_free_count);
2879 db_printf("cnt.v_cache_count: %d\n", cnt.v_cache_count);
2880 db_printf("cnt.v_inactive_count: %d\n", cnt.v_inactive_count);
2881 db_printf("cnt.v_active_count: %d\n", cnt.v_active_count);
2882 db_printf("cnt.v_wire_count: %d\n", cnt.v_wire_count);
2883 db_printf("cnt.v_free_reserved: %d\n", cnt.v_free_reserved);
2884 db_printf("cnt.v_free_min: %d\n", cnt.v_free_min);
2885 db_printf("cnt.v_free_target: %d\n", cnt.v_free_target);
2886 db_printf("cnt.v_cache_min: %d\n", cnt.v_cache_min);
2887 db_printf("cnt.v_inactive_target: %d\n", cnt.v_inactive_target);
2890 DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info)
2893 db_printf("PQ_FREE:");
2894 db_printf(" %d", cnt.v_free_count);
2897 db_printf("PQ_CACHE:");
2898 db_printf(" %d", cnt.v_cache_count);
2901 db_printf("PQ_ACTIVE: %d, PQ_INACTIVE: %d\n",
2902 *vm_pagequeues[PQ_ACTIVE].pq_cnt,
2903 *vm_pagequeues[PQ_INACTIVE].pq_cnt);
2906 DB_SHOW_COMMAND(pginfo, vm_page_print_pginfo)
2912 db_printf("show pginfo addr\n");
2916 phys = strchr(modif, 'p') != NULL;
2918 m = PHYS_TO_VM_PAGE(addr);
2920 m = (vm_page_t)addr;
2922 "page %p obj %p pidx 0x%jx phys 0x%jx q %d hold %d wire %d\n"
2923 " af 0x%x of 0x%x f 0x%x act %d busy %d valid 0x%x dirty 0x%x\n",
2924 m, m->object, (uintmax_t)m->pindex, (uintmax_t)m->phys_addr,
2925 m->queue, m->hold_count, m->wire_count, m->aflags, m->oflags,
2926 m->flags, m->act_count, m->busy, m->valid, m->dirty);