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>
96 #include <sys/msgbuf.h>
97 #include <sys/mutex.h>
99 #include <sys/rwlock.h>
100 #include <sys/sysctl.h>
101 #include <sys/vmmeter.h>
102 #include <sys/vnode.h>
106 #include <vm/vm_param.h>
107 #include <vm/vm_kern.h>
108 #include <vm/vm_object.h>
109 #include <vm/vm_page.h>
110 #include <vm/vm_pageout.h>
111 #include <vm/vm_pager.h>
112 #include <vm/vm_phys.h>
113 #include <vm/vm_radix.h>
114 #include <vm/vm_reserv.h>
115 #include <vm/vm_extern.h>
117 #include <vm/uma_int.h>
119 #include <machine/md_var.h>
122 * Associated with page of user-allocatable memory is a
126 struct vm_pagequeue vm_pagequeues[PQ_COUNT] = {
128 .pq_pl = TAILQ_HEAD_INITIALIZER(
129 vm_pagequeues[PQ_INACTIVE].pq_pl),
130 .pq_cnt = &cnt.v_inactive_count,
131 .pq_name = "vm inactive pagequeue"
134 .pq_pl = TAILQ_HEAD_INITIALIZER(
135 vm_pagequeues[PQ_ACTIVE].pq_pl),
136 .pq_cnt = &cnt.v_active_count,
137 .pq_name = "vm active pagequeue"
140 struct mtx_padalign vm_page_queue_free_mtx;
142 struct mtx_padalign pa_lock[PA_LOCK_COUNT];
144 vm_page_t vm_page_array;
145 long vm_page_array_size;
147 int vm_page_zero_count;
149 static int boot_pages = UMA_BOOT_PAGES;
150 TUNABLE_INT("vm.boot_pages", &boot_pages);
151 SYSCTL_INT(_vm, OID_AUTO, boot_pages, CTLFLAG_RD, &boot_pages, 0,
152 "number of pages allocated for bootstrapping the VM system");
154 static int pa_tryrelock_restart;
155 SYSCTL_INT(_vm, OID_AUTO, tryrelock_restart, CTLFLAG_RD,
156 &pa_tryrelock_restart, 0, "Number of tryrelock restarts");
158 static uma_zone_t fakepg_zone;
160 static struct vnode *vm_page_alloc_init(vm_page_t m);
161 static void vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits);
162 static void vm_page_enqueue(int queue, vm_page_t m);
163 static void vm_page_init_fakepg(void *dummy);
164 static void vm_page_insert_after(vm_page_t m, vm_object_t object,
165 vm_pindex_t pindex, vm_page_t mpred);
167 SYSINIT(vm_page, SI_SUB_VM, SI_ORDER_SECOND, vm_page_init_fakepg, NULL);
170 vm_page_init_fakepg(void *dummy)
173 fakepg_zone = uma_zcreate("fakepg", sizeof(struct vm_page), NULL, NULL,
174 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE | UMA_ZONE_VM);
177 /* Make sure that u_long is at least 64 bits when PAGE_SIZE is 32K. */
178 #if PAGE_SIZE == 32768
180 CTASSERT(sizeof(u_long) >= 8);
185 * Try to acquire a physical address lock while a pmap is locked. If we
186 * fail to trylock we unlock and lock the pmap directly and cache the
187 * locked pa in *locked. The caller should then restart their loop in case
188 * the virtual to physical mapping has changed.
191 vm_page_pa_tryrelock(pmap_t pmap, vm_paddr_t pa, vm_paddr_t *locked)
198 PA_LOCK_ASSERT(lockpa, MA_OWNED);
199 if (PA_LOCKPTR(pa) == PA_LOCKPTR(lockpa))
206 atomic_add_int(&pa_tryrelock_restart, 1);
215 * Sets the page size, perhaps based upon the memory
216 * size. Must be called before any use of page-size
217 * dependent functions.
220 vm_set_page_size(void)
222 if (cnt.v_page_size == 0)
223 cnt.v_page_size = PAGE_SIZE;
224 if (((cnt.v_page_size - 1) & cnt.v_page_size) != 0)
225 panic("vm_set_page_size: page size not a power of two");
229 * vm_page_blacklist_lookup:
231 * See if a physical address in this page has been listed
232 * in the blacklist tunable. Entries in the tunable are
233 * separated by spaces or commas. If an invalid integer is
234 * encountered then the rest of the string is skipped.
237 vm_page_blacklist_lookup(char *list, vm_paddr_t pa)
242 for (pos = list; *pos != '\0'; pos = cp) {
243 bad = strtoq(pos, &cp, 0);
245 if (*cp == ' ' || *cp == ',') {
252 if (pa == trunc_page(bad))
261 * Initializes the resident memory module.
263 * Allocates memory for the page cells, and
264 * for the object/offset-to-page hash table headers.
265 * Each page cell is initialized and placed on the free list.
268 vm_page_startup(vm_offset_t vaddr)
271 vm_paddr_t page_range;
278 /* the biggest memory array is the second group of pages */
280 vm_paddr_t biggestsize;
281 vm_paddr_t low_water, high_water;
286 vaddr = round_page(vaddr);
288 for (i = 0; phys_avail[i + 1]; i += 2) {
289 phys_avail[i] = round_page(phys_avail[i]);
290 phys_avail[i + 1] = trunc_page(phys_avail[i + 1]);
293 low_water = phys_avail[0];
294 high_water = phys_avail[1];
296 for (i = 0; phys_avail[i + 1]; i += 2) {
297 vm_paddr_t size = phys_avail[i + 1] - phys_avail[i];
299 if (size > biggestsize) {
303 if (phys_avail[i] < low_water)
304 low_water = phys_avail[i];
305 if (phys_avail[i + 1] > high_water)
306 high_water = phys_avail[i + 1];
313 end = phys_avail[biggestone+1];
316 * Initialize the page and queue locks.
318 mtx_init(&vm_page_queue_free_mtx, "vm page free queue", NULL, MTX_DEF);
319 for (i = 0; i < PA_LOCK_COUNT; i++)
320 mtx_init(&pa_lock[i], "vm page", NULL, MTX_DEF);
321 for (i = 0; i < PQ_COUNT; i++)
322 vm_pagequeue_init_lock(&vm_pagequeues[i]);
325 * Allocate memory for use when boot strapping the kernel memory
328 new_end = end - (boot_pages * UMA_SLAB_SIZE);
329 new_end = trunc_page(new_end);
330 mapped = pmap_map(&vaddr, new_end, end,
331 VM_PROT_READ | VM_PROT_WRITE);
332 bzero((void *)mapped, end - new_end);
333 uma_startup((void *)mapped, boot_pages);
335 #if defined(__amd64__) || defined(__i386__) || defined(__arm__) || \
338 * Allocate a bitmap to indicate that a random physical page
339 * needs to be included in a minidump.
341 * The amd64 port needs this to indicate which direct map pages
342 * need to be dumped, via calls to dump_add_page()/dump_drop_page().
344 * However, i386 still needs this workspace internally within the
345 * minidump code. In theory, they are not needed on i386, but are
346 * included should the sf_buf code decide to use them.
349 for (i = 0; dump_avail[i + 1] != 0; i += 2)
350 if (dump_avail[i + 1] > last_pa)
351 last_pa = dump_avail[i + 1];
352 page_range = last_pa / PAGE_SIZE;
353 vm_page_dump_size = round_page(roundup2(page_range, NBBY) / NBBY);
354 new_end -= vm_page_dump_size;
355 vm_page_dump = (void *)(uintptr_t)pmap_map(&vaddr, new_end,
356 new_end + vm_page_dump_size, VM_PROT_READ | VM_PROT_WRITE);
357 bzero((void *)vm_page_dump, vm_page_dump_size);
361 * Request that the physical pages underlying the message buffer be
362 * included in a crash dump. Since the message buffer is accessed
363 * through the direct map, they are not automatically included.
365 pa = DMAP_TO_PHYS((vm_offset_t)msgbufp->msg_ptr);
366 last_pa = pa + round_page(msgbufsize);
367 while (pa < last_pa) {
373 * Compute the number of pages of memory that will be available for
374 * use (taking into account the overhead of a page structure per
377 first_page = low_water / PAGE_SIZE;
378 #ifdef VM_PHYSSEG_SPARSE
380 for (i = 0; phys_avail[i + 1] != 0; i += 2)
381 page_range += atop(phys_avail[i + 1] - phys_avail[i]);
382 #elif defined(VM_PHYSSEG_DENSE)
383 page_range = high_water / PAGE_SIZE - first_page;
385 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
390 * Reserve an unmapped guard page to trap access to vm_page_array[-1].
395 * Initialize the mem entry structures now, and put them in the free
398 new_end = trunc_page(end - page_range * sizeof(struct vm_page));
399 mapped = pmap_map(&vaddr, new_end, end,
400 VM_PROT_READ | VM_PROT_WRITE);
401 vm_page_array = (vm_page_t) mapped;
402 #if VM_NRESERVLEVEL > 0
404 * Allocate memory for the reservation management system's data
407 new_end = vm_reserv_startup(&vaddr, new_end, high_water);
409 #if defined(__amd64__) || defined(__mips__)
411 * pmap_map on amd64 and mips can come out of the direct-map, not kvm
412 * like i386, so the pages must be tracked for a crashdump to include
413 * this data. This includes the vm_page_array and the early UMA
416 for (pa = new_end; pa < phys_avail[biggestone + 1]; pa += PAGE_SIZE)
419 phys_avail[biggestone + 1] = new_end;
422 * Clear all of the page structures
424 bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page));
425 for (i = 0; i < page_range; i++)
426 vm_page_array[i].order = VM_NFREEORDER;
427 vm_page_array_size = page_range;
430 * Initialize the physical memory allocator.
435 * Add every available physical page that is not blacklisted to
438 cnt.v_page_count = 0;
439 cnt.v_free_count = 0;
440 list = getenv("vm.blacklist");
441 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
443 last_pa = phys_avail[i + 1];
444 while (pa < last_pa) {
446 vm_page_blacklist_lookup(list, pa))
447 printf("Skipping page with pa 0x%jx\n",
450 vm_phys_add_page(pa);
455 #if VM_NRESERVLEVEL > 0
457 * Initialize the reservation management system.
465 vm_page_reference(vm_page_t m)
468 vm_page_aflag_set(m, PGA_REFERENCED);
472 vm_page_busy(vm_page_t m)
475 VM_OBJECT_ASSERT_WLOCKED(m->object);
476 KASSERT((m->oflags & VPO_BUSY) == 0,
477 ("vm_page_busy: page already busy!!!"));
478 m->oflags |= VPO_BUSY;
484 * wakeup anyone waiting for the page.
487 vm_page_flash(vm_page_t m)
490 VM_OBJECT_ASSERT_WLOCKED(m->object);
491 if (m->oflags & VPO_WANTED) {
492 m->oflags &= ~VPO_WANTED;
500 * clear the VPO_BUSY flag and wakeup anyone waiting for the
505 vm_page_wakeup(vm_page_t m)
508 VM_OBJECT_ASSERT_WLOCKED(m->object);
509 KASSERT(m->oflags & VPO_BUSY, ("vm_page_wakeup: page not busy!!!"));
510 m->oflags &= ~VPO_BUSY;
515 vm_page_io_start(vm_page_t m)
518 VM_OBJECT_ASSERT_WLOCKED(m->object);
523 vm_page_io_finish(vm_page_t m)
526 VM_OBJECT_ASSERT_WLOCKED(m->object);
527 KASSERT(m->busy > 0, ("vm_page_io_finish: page %p is not busy", m));
534 * Keep page from being freed by the page daemon
535 * much of the same effect as wiring, except much lower
536 * overhead and should be used only for *very* temporary
537 * holding ("wiring").
540 vm_page_hold(vm_page_t mem)
543 vm_page_lock_assert(mem, MA_OWNED);
548 vm_page_unhold(vm_page_t mem)
551 vm_page_lock_assert(mem, MA_OWNED);
553 KASSERT(mem->hold_count >= 0, ("vm_page_unhold: hold count < 0!!!"));
554 if (mem->hold_count == 0 && (mem->flags & PG_UNHOLDFREE) != 0)
555 vm_page_free_toq(mem);
559 * vm_page_unhold_pages:
561 * Unhold each of the pages that is referenced by the given array.
564 vm_page_unhold_pages(vm_page_t *ma, int count)
566 struct mtx *mtx, *new_mtx;
569 for (; count != 0; count--) {
571 * Avoid releasing and reacquiring the same page lock.
573 new_mtx = vm_page_lockptr(*ma);
574 if (mtx != new_mtx) {
588 PHYS_TO_VM_PAGE(vm_paddr_t pa)
592 #ifdef VM_PHYSSEG_SPARSE
593 m = vm_phys_paddr_to_vm_page(pa);
595 m = vm_phys_fictitious_to_vm_page(pa);
597 #elif defined(VM_PHYSSEG_DENSE)
601 if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
602 m = &vm_page_array[pi - first_page];
605 return (vm_phys_fictitious_to_vm_page(pa));
607 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
614 * Create a fictitious page with the specified physical address and
615 * memory attribute. The memory attribute is the only the machine-
616 * dependent aspect of a fictitious page that must be initialized.
619 vm_page_getfake(vm_paddr_t paddr, vm_memattr_t memattr)
623 m = uma_zalloc(fakepg_zone, M_WAITOK | M_ZERO);
624 vm_page_initfake(m, paddr, memattr);
629 vm_page_initfake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
632 if ((m->flags & PG_FICTITIOUS) != 0) {
634 * The page's memattr might have changed since the
635 * previous initialization. Update the pmap to the
640 m->phys_addr = paddr;
642 /* Fictitious pages don't use "segind". */
643 m->flags = PG_FICTITIOUS;
644 /* Fictitious pages don't use "order" or "pool". */
645 m->oflags = VPO_BUSY | VPO_UNMANAGED;
648 pmap_page_set_memattr(m, memattr);
654 * Release a fictitious page.
657 vm_page_putfake(vm_page_t m)
660 KASSERT((m->oflags & VPO_UNMANAGED) != 0, ("managed %p", m));
661 KASSERT((m->flags & PG_FICTITIOUS) != 0,
662 ("vm_page_putfake: bad page %p", m));
663 uma_zfree(fakepg_zone, m);
667 * vm_page_updatefake:
669 * Update the given fictitious page to the specified physical address and
673 vm_page_updatefake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
676 KASSERT((m->flags & PG_FICTITIOUS) != 0,
677 ("vm_page_updatefake: bad page %p", m));
678 m->phys_addr = paddr;
679 pmap_page_set_memattr(m, memattr);
688 vm_page_free(vm_page_t m)
691 m->flags &= ~PG_ZERO;
698 * Free a page to the zerod-pages queue
701 vm_page_free_zero(vm_page_t m)
709 * Unbusy and handle the page queueing for a page from the VOP_GETPAGES()
710 * array which is not the request page.
713 vm_page_readahead_finish(vm_page_t m)
718 * Since the page is not the requested page, whether
719 * it should be activated or deactivated is not
720 * obvious. Empirical results have shown that
721 * deactivating the page is usually the best choice,
722 * unless the page is wanted by another thread.
724 if (m->oflags & VPO_WANTED) {
730 vm_page_deactivate(m);
736 * Free the completely invalid page. Such page state
737 * occurs due to the short read operation which did
738 * not covered our page at all, or in case when a read
750 * Sleep and release the page lock.
752 * The object containing the given page must be locked.
755 vm_page_sleep(vm_page_t m, const char *msg)
758 VM_OBJECT_ASSERT_WLOCKED(m->object);
759 if (mtx_owned(vm_page_lockptr(m)))
763 * It's possible that while we sleep, the page will get
764 * unbusied and freed. If we are holding the object
765 * lock, we will assume we hold a reference to the object
766 * such that even if m->object changes, we can re-lock
769 m->oflags |= VPO_WANTED;
770 VM_OBJECT_SLEEP(m->object, m, PVM, msg, 0);
774 * vm_page_dirty_KBI: [ internal use only ]
776 * Set all bits in the page's dirty field.
778 * The object containing the specified page must be locked if the
779 * call is made from the machine-independent layer.
781 * See vm_page_clear_dirty_mask().
783 * This function should only be called by vm_page_dirty().
786 vm_page_dirty_KBI(vm_page_t m)
789 /* These assertions refer to this operation by its public name. */
790 KASSERT((m->flags & PG_CACHED) == 0,
791 ("vm_page_dirty: page in cache!"));
792 KASSERT(!VM_PAGE_IS_FREE(m),
793 ("vm_page_dirty: page is free!"));
794 KASSERT(m->valid == VM_PAGE_BITS_ALL,
795 ("vm_page_dirty: page is invalid!"));
796 m->dirty = VM_PAGE_BITS_ALL;
800 * vm_page_insert: [ internal use only ]
802 * Inserts the given mem entry into the object and object list.
804 * The object must be locked.
807 vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex)
811 VM_OBJECT_ASSERT_WLOCKED(object);
812 mpred = vm_radix_lookup_le(&object->rtree, pindex);
813 vm_page_insert_after(m, object, pindex, mpred);
817 * vm_page_insert_after:
819 * Inserts the page "m" into the specified object at offset "pindex".
821 * The page "mpred" must immediately precede the offset "pindex" within
822 * the specified object.
824 * The object must be locked.
827 vm_page_insert_after(vm_page_t m, vm_object_t object, vm_pindex_t pindex,
832 VM_OBJECT_ASSERT_WLOCKED(object);
833 KASSERT(m->object == NULL,
834 ("vm_page_insert_after: page already inserted"));
836 KASSERT(mpred->object == object ||
837 (mpred->flags & PG_SLAB) != 0,
838 ("vm_page_insert_after: object doesn't contain mpred"));
839 KASSERT(mpred->pindex < pindex,
840 ("vm_page_insert_after: mpred doesn't precede pindex"));
841 msucc = TAILQ_NEXT(mpred, listq);
843 msucc = TAILQ_FIRST(&object->memq);
845 KASSERT(msucc->pindex > pindex,
846 ("vm_page_insert_after: msucc doesn't succeed pindex"));
849 * Record the object/offset pair in this page
855 * Now link into the object's ordered list of backed pages.
858 TAILQ_INSERT_AFTER(&object->memq, mpred, m, listq);
860 TAILQ_INSERT_HEAD(&object->memq, m, listq);
861 vm_radix_insert(&object->rtree, m);
864 * Show that the object has one more resident page.
866 object->resident_page_count++;
869 * Hold the vnode until the last page is released.
871 if (object->resident_page_count == 1 && object->type == OBJT_VNODE)
872 vhold(object->handle);
875 * Since we are inserting a new and possibly dirty page,
876 * update the object's OBJ_MIGHTBEDIRTY flag.
878 if (pmap_page_is_write_mapped(m))
879 vm_object_set_writeable_dirty(object);
885 * Removes the given mem entry from the object/offset-page
886 * table and the object page list, but do not invalidate/terminate
889 * The object must be locked. The page must be locked if it is managed.
892 vm_page_remove(vm_page_t m)
896 if ((m->oflags & VPO_UNMANAGED) == 0)
897 vm_page_lock_assert(m, MA_OWNED);
898 if ((object = m->object) == NULL)
900 VM_OBJECT_ASSERT_WLOCKED(object);
901 if (m->oflags & VPO_BUSY) {
902 m->oflags &= ~VPO_BUSY;
907 * Now remove from the object's list of backed pages.
909 vm_radix_remove(&object->rtree, m->pindex);
910 TAILQ_REMOVE(&object->memq, m, listq);
913 * And show that the object has one fewer resident page.
915 object->resident_page_count--;
918 * The vnode may now be recycled.
920 if (object->resident_page_count == 0 && object->type == OBJT_VNODE)
921 vdrop(object->handle);
929 * Returns the page associated with the object/offset
930 * pair specified; if none is found, NULL is returned.
932 * The object must be locked.
935 vm_page_lookup(vm_object_t object, vm_pindex_t pindex)
938 VM_OBJECT_ASSERT_LOCKED(object);
939 return (vm_radix_lookup(&object->rtree, pindex));
943 * vm_page_find_least:
945 * Returns the page associated with the object with least pindex
946 * greater than or equal to the parameter pindex, or NULL.
948 * The object must be locked.
951 vm_page_find_least(vm_object_t object, vm_pindex_t pindex)
955 VM_OBJECT_ASSERT_LOCKED(object);
956 if ((m = TAILQ_FIRST(&object->memq)) != NULL && m->pindex < pindex)
957 m = vm_radix_lookup_ge(&object->rtree, pindex);
962 * Returns the given page's successor (by pindex) within the object if it is
963 * resident; if none is found, NULL is returned.
965 * The object must be locked.
968 vm_page_next(vm_page_t m)
972 VM_OBJECT_ASSERT_WLOCKED(m->object);
973 if ((next = TAILQ_NEXT(m, listq)) != NULL &&
974 next->pindex != m->pindex + 1)
980 * Returns the given page's predecessor (by pindex) within the object if it is
981 * resident; if none is found, NULL is returned.
983 * The object must be locked.
986 vm_page_prev(vm_page_t m)
990 VM_OBJECT_ASSERT_WLOCKED(m->object);
991 if ((prev = TAILQ_PREV(m, pglist, listq)) != NULL &&
992 prev->pindex != m->pindex - 1)
1000 * Move the given memory entry from its
1001 * current object to the specified target object/offset.
1003 * Note: swap associated with the page must be invalidated by the move. We
1004 * have to do this for several reasons: (1) we aren't freeing the
1005 * page, (2) we are dirtying the page, (3) the VM system is probably
1006 * moving the page from object A to B, and will then later move
1007 * the backing store from A to B and we can't have a conflict.
1009 * Note: we *always* dirty the page. It is necessary both for the
1010 * fact that we moved it, and because we may be invalidating
1011 * swap. If the page is on the cache, we have to deactivate it
1012 * or vm_page_dirty() will panic. Dirty pages are not allowed
1015 * The objects must be locked. The page must be locked if it is managed.
1018 vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex)
1022 vm_page_insert(m, new_object, new_pindex);
1027 * Convert all of the given object's cached pages that have a
1028 * pindex within the given range into free pages. If the value
1029 * zero is given for "end", then the range's upper bound is
1030 * infinity. If the given object is backed by a vnode and it
1031 * transitions from having one or more cached pages to none, the
1032 * vnode's hold count is reduced.
1035 vm_page_cache_free(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1040 mtx_lock(&vm_page_queue_free_mtx);
1041 if (__predict_false(vm_radix_is_empty(&object->cache))) {
1042 mtx_unlock(&vm_page_queue_free_mtx);
1045 while ((m = vm_radix_lookup_ge(&object->cache, start)) != NULL) {
1046 if (end != 0 && m->pindex >= end)
1048 vm_radix_remove(&object->cache, m->pindex);
1051 /* Clear PG_CACHED and set PG_FREE. */
1052 m->flags ^= PG_CACHED | PG_FREE;
1053 KASSERT((m->flags & (PG_CACHED | PG_FREE)) == PG_FREE,
1054 ("vm_page_cache_free: page %p has inconsistent flags", m));
1055 cnt.v_cache_count--;
1058 empty = vm_radix_is_empty(&object->cache);
1059 mtx_unlock(&vm_page_queue_free_mtx);
1060 if (object->type == OBJT_VNODE && empty)
1061 vdrop(object->handle);
1065 * Returns the cached page that is associated with the given
1066 * object and offset. If, however, none exists, returns NULL.
1068 * The free page queue must be locked.
1070 static inline vm_page_t
1071 vm_page_cache_lookup(vm_object_t object, vm_pindex_t pindex)
1074 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1075 return (vm_radix_lookup(&object->cache, pindex));
1079 * Remove the given cached page from its containing object's
1080 * collection of cached pages.
1082 * The free page queue must be locked.
1085 vm_page_cache_remove(vm_page_t m)
1088 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1089 KASSERT((m->flags & PG_CACHED) != 0,
1090 ("vm_page_cache_remove: page %p is not cached", m));
1091 vm_radix_remove(&m->object->cache, m->pindex);
1093 cnt.v_cache_count--;
1097 * Transfer all of the cached pages with offset greater than or
1098 * equal to 'offidxstart' from the original object's cache to the
1099 * new object's cache. However, any cached pages with offset
1100 * greater than or equal to the new object's size are kept in the
1101 * original object. Initially, the new object's cache must be
1102 * empty. Offset 'offidxstart' in the original object must
1103 * correspond to offset zero in the new object.
1105 * The new object must be locked.
1108 vm_page_cache_transfer(vm_object_t orig_object, vm_pindex_t offidxstart,
1109 vm_object_t new_object)
1114 * Insertion into an object's collection of cached pages
1115 * requires the object to be locked. In contrast, removal does
1118 VM_OBJECT_ASSERT_WLOCKED(new_object);
1119 KASSERT(vm_radix_is_empty(&new_object->cache),
1120 ("vm_page_cache_transfer: object %p has cached pages",
1122 mtx_lock(&vm_page_queue_free_mtx);
1123 while ((m = vm_radix_lookup_ge(&orig_object->cache,
1124 offidxstart)) != NULL) {
1126 * Transfer all of the pages with offset greater than or
1127 * equal to 'offidxstart' from the original object's
1128 * cache to the new object's cache.
1130 if ((m->pindex - offidxstart) >= new_object->size)
1132 vm_radix_remove(&orig_object->cache, m->pindex);
1133 /* Update the page's object and offset. */
1134 m->object = new_object;
1135 m->pindex -= offidxstart;
1136 vm_radix_insert(&new_object->cache, m);
1138 mtx_unlock(&vm_page_queue_free_mtx);
1142 * Returns TRUE if a cached page is associated with the given object and
1143 * offset, and FALSE otherwise.
1145 * The object must be locked.
1148 vm_page_is_cached(vm_object_t object, vm_pindex_t pindex)
1153 * Insertion into an object's collection of cached pages requires the
1154 * object to be locked. Therefore, if the object is locked and the
1155 * object's collection is empty, there is no need to acquire the free
1156 * page queues lock in order to prove that the specified page doesn't
1159 VM_OBJECT_ASSERT_WLOCKED(object);
1160 if (__predict_true(vm_object_cache_is_empty(object)))
1162 mtx_lock(&vm_page_queue_free_mtx);
1163 m = vm_page_cache_lookup(object, pindex);
1164 mtx_unlock(&vm_page_queue_free_mtx);
1171 * Allocate and return a page that is associated with the specified
1172 * object and offset pair. By default, this page has the flag VPO_BUSY
1175 * The caller must always specify an allocation class.
1177 * allocation classes:
1178 * VM_ALLOC_NORMAL normal process request
1179 * VM_ALLOC_SYSTEM system *really* needs a page
1180 * VM_ALLOC_INTERRUPT interrupt time request
1182 * optional allocation flags:
1183 * VM_ALLOC_COUNT(number) the number of additional pages that the caller
1184 * intends to allocate
1185 * VM_ALLOC_IFCACHED return page only if it is cached
1186 * VM_ALLOC_IFNOTCACHED return NULL, do not reactivate if the page
1188 * VM_ALLOC_NOBUSY do not set the flag VPO_BUSY on the page
1189 * VM_ALLOC_NODUMP do not include the page in a kernel core dump
1190 * VM_ALLOC_NOOBJ page is not associated with an object and
1191 * should not have the flag VPO_BUSY set
1192 * VM_ALLOC_WIRED wire the allocated page
1193 * VM_ALLOC_ZERO prefer a zeroed page
1195 * This routine may not sleep.
1198 vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req)
1200 struct vnode *vp = NULL;
1201 vm_object_t m_object;
1203 int flags, req_class;
1205 mpred = 0; /* XXX: pacify gcc */
1206 KASSERT((object != NULL) == ((req & VM_ALLOC_NOOBJ) == 0),
1207 ("vm_page_alloc: inconsistent object/req"));
1209 VM_OBJECT_ASSERT_WLOCKED(object);
1211 req_class = req & VM_ALLOC_CLASS_MASK;
1214 * The page daemon is allowed to dig deeper into the free page list.
1216 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
1217 req_class = VM_ALLOC_SYSTEM;
1219 if (object != NULL) {
1220 mpred = vm_radix_lookup_le(&object->rtree, pindex);
1221 KASSERT(mpred == NULL || mpred->pindex != pindex,
1222 ("vm_page_alloc: pindex already allocated"));
1224 mtx_lock(&vm_page_queue_free_mtx);
1225 if (cnt.v_free_count + cnt.v_cache_count > cnt.v_free_reserved ||
1226 (req_class == VM_ALLOC_SYSTEM &&
1227 cnt.v_free_count + cnt.v_cache_count > cnt.v_interrupt_free_min) ||
1228 (req_class == VM_ALLOC_INTERRUPT &&
1229 cnt.v_free_count + cnt.v_cache_count > 0)) {
1231 * Allocate from the free queue if the number of free pages
1232 * exceeds the minimum for the request class.
1234 if (object != NULL &&
1235 (m = vm_page_cache_lookup(object, pindex)) != NULL) {
1236 if ((req & VM_ALLOC_IFNOTCACHED) != 0) {
1237 mtx_unlock(&vm_page_queue_free_mtx);
1240 if (vm_phys_unfree_page(m))
1241 vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m, 0);
1242 #if VM_NRESERVLEVEL > 0
1243 else if (!vm_reserv_reactivate_page(m))
1247 panic("vm_page_alloc: cache page %p is missing"
1248 " from the free queue", m);
1249 } else if ((req & VM_ALLOC_IFCACHED) != 0) {
1250 mtx_unlock(&vm_page_queue_free_mtx);
1252 #if VM_NRESERVLEVEL > 0
1253 } else if (object == NULL || (object->flags & (OBJ_COLORED |
1254 OBJ_FICTITIOUS)) != OBJ_COLORED || (m =
1255 vm_reserv_alloc_page(object, pindex, mpred)) == NULL) {
1259 m = vm_phys_alloc_pages(object != NULL ?
1260 VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT, 0);
1261 #if VM_NRESERVLEVEL > 0
1262 if (m == NULL && vm_reserv_reclaim_inactive()) {
1263 m = vm_phys_alloc_pages(object != NULL ?
1264 VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT,
1271 * Not allocatable, give up.
1273 mtx_unlock(&vm_page_queue_free_mtx);
1274 atomic_add_int(&vm_pageout_deficit,
1275 max((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1));
1276 pagedaemon_wakeup();
1281 * At this point we had better have found a good page.
1283 KASSERT(m != NULL, ("vm_page_alloc: missing page"));
1284 KASSERT(m->queue == PQ_NONE,
1285 ("vm_page_alloc: page %p has unexpected queue %d", m, m->queue));
1286 KASSERT(m->wire_count == 0, ("vm_page_alloc: page %p is wired", m));
1287 KASSERT(m->hold_count == 0, ("vm_page_alloc: page %p is held", m));
1288 KASSERT(m->busy == 0, ("vm_page_alloc: page %p is busy", m));
1289 KASSERT(m->dirty == 0, ("vm_page_alloc: page %p is dirty", m));
1290 KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT,
1291 ("vm_page_alloc: page %p has unexpected memattr %d", m,
1292 pmap_page_get_memattr(m)));
1293 if ((m->flags & PG_CACHED) != 0) {
1294 KASSERT((m->flags & PG_ZERO) == 0,
1295 ("vm_page_alloc: cached page %p is PG_ZERO", m));
1296 KASSERT(m->valid != 0,
1297 ("vm_page_alloc: cached page %p is invalid", m));
1298 if (m->object == object && m->pindex == pindex)
1299 cnt.v_reactivated++;
1302 m_object = m->object;
1303 vm_page_cache_remove(m);
1304 if (m_object->type == OBJT_VNODE &&
1305 vm_object_cache_is_empty(m_object))
1306 vp = m_object->handle;
1308 KASSERT(VM_PAGE_IS_FREE(m),
1309 ("vm_page_alloc: page %p is not free", m));
1310 KASSERT(m->valid == 0,
1311 ("vm_page_alloc: free page %p is valid", m));
1316 * Only the PG_ZERO flag is inherited. The PG_CACHED or PG_FREE flag
1317 * must be cleared before the free page queues lock is released.
1320 if (m->flags & PG_ZERO) {
1321 vm_page_zero_count--;
1322 if (req & VM_ALLOC_ZERO)
1325 if (req & VM_ALLOC_NODUMP)
1328 mtx_unlock(&vm_page_queue_free_mtx);
1330 m->oflags = object == NULL || (object->flags & OBJ_UNMANAGED) != 0 ?
1332 if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ)) == 0)
1333 m->oflags |= VPO_BUSY;
1334 if (req & VM_ALLOC_WIRED) {
1336 * The page lock is not required for wiring a page until that
1337 * page is inserted into the object.
1339 atomic_add_int(&cnt.v_wire_count, 1);
1344 if (object != NULL) {
1345 /* Ignore device objects; the pager sets "memattr" for them. */
1346 if (object->memattr != VM_MEMATTR_DEFAULT &&
1347 (object->flags & OBJ_FICTITIOUS) == 0)
1348 pmap_page_set_memattr(m, object->memattr);
1349 vm_page_insert_after(m, object, pindex, mpred);
1354 * The following call to vdrop() must come after the above call
1355 * to vm_page_insert() in case both affect the same object and
1356 * vnode. Otherwise, the affected vnode's hold count could
1357 * temporarily become zero.
1363 * Don't wakeup too often - wakeup the pageout daemon when
1364 * we would be nearly out of memory.
1366 if (vm_paging_needed())
1367 pagedaemon_wakeup();
1373 * vm_page_alloc_contig:
1375 * Allocate a contiguous set of physical pages of the given size "npages"
1376 * from the free lists. All of the physical pages must be at or above
1377 * the given physical address "low" and below the given physical address
1378 * "high". The given value "alignment" determines the alignment of the
1379 * first physical page in the set. If the given value "boundary" is
1380 * non-zero, then the set of physical pages cannot cross any physical
1381 * address boundary that is a multiple of that value. Both "alignment"
1382 * and "boundary" must be a power of two.
1384 * If the specified memory attribute, "memattr", is VM_MEMATTR_DEFAULT,
1385 * then the memory attribute setting for the physical pages is configured
1386 * to the object's memory attribute setting. Otherwise, the memory
1387 * attribute setting for the physical pages is configured to "memattr",
1388 * overriding the object's memory attribute setting. However, if the
1389 * object's memory attribute setting is not VM_MEMATTR_DEFAULT, then the
1390 * memory attribute setting for the physical pages cannot be configured
1391 * to VM_MEMATTR_DEFAULT.
1393 * The caller must always specify an allocation class.
1395 * allocation classes:
1396 * VM_ALLOC_NORMAL normal process request
1397 * VM_ALLOC_SYSTEM system *really* needs a page
1398 * VM_ALLOC_INTERRUPT interrupt time request
1400 * optional allocation flags:
1401 * VM_ALLOC_NOBUSY do not set the flag VPO_BUSY on the page
1402 * VM_ALLOC_NOOBJ page is not associated with an object and
1403 * should not have the flag VPO_BUSY set
1404 * VM_ALLOC_WIRED wire the allocated page
1405 * VM_ALLOC_ZERO prefer a zeroed page
1407 * This routine may not sleep.
1410 vm_page_alloc_contig(vm_object_t object, vm_pindex_t pindex, int req,
1411 u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
1412 vm_paddr_t boundary, vm_memattr_t memattr)
1415 vm_page_t deferred_vdrop_list, m, m_ret;
1416 u_int flags, oflags;
1419 KASSERT((object != NULL) == ((req & VM_ALLOC_NOOBJ) == 0),
1420 ("vm_page_alloc_contig: inconsistent object/req"));
1421 if (object != NULL) {
1422 VM_OBJECT_ASSERT_WLOCKED(object);
1423 KASSERT(object->type == OBJT_PHYS,
1424 ("vm_page_alloc_contig: object %p isn't OBJT_PHYS",
1427 KASSERT(npages > 0, ("vm_page_alloc_contig: npages is zero"));
1428 req_class = req & VM_ALLOC_CLASS_MASK;
1431 * The page daemon is allowed to dig deeper into the free page list.
1433 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
1434 req_class = VM_ALLOC_SYSTEM;
1436 deferred_vdrop_list = NULL;
1437 mtx_lock(&vm_page_queue_free_mtx);
1438 if (cnt.v_free_count + cnt.v_cache_count >= npages +
1439 cnt.v_free_reserved || (req_class == VM_ALLOC_SYSTEM &&
1440 cnt.v_free_count + cnt.v_cache_count >= npages +
1441 cnt.v_interrupt_free_min) || (req_class == VM_ALLOC_INTERRUPT &&
1442 cnt.v_free_count + cnt.v_cache_count >= npages)) {
1443 #if VM_NRESERVLEVEL > 0
1445 if (object == NULL || (object->flags & OBJ_COLORED) == 0 ||
1446 (m_ret = vm_reserv_alloc_contig(object, pindex, npages,
1447 low, high, alignment, boundary)) == NULL)
1449 m_ret = vm_phys_alloc_contig(npages, low, high,
1450 alignment, boundary);
1452 mtx_unlock(&vm_page_queue_free_mtx);
1453 atomic_add_int(&vm_pageout_deficit, npages);
1454 pagedaemon_wakeup();
1458 for (m = m_ret; m < &m_ret[npages]; m++) {
1459 drop = vm_page_alloc_init(m);
1462 * Enqueue the vnode for deferred vdrop().
1464 * Once the pages are removed from the free
1465 * page list, "pageq" can be safely abused to
1466 * construct a short-lived list of vnodes.
1468 m->pageq.tqe_prev = (void *)drop;
1469 m->pageq.tqe_next = deferred_vdrop_list;
1470 deferred_vdrop_list = m;
1474 #if VM_NRESERVLEVEL > 0
1475 if (vm_reserv_reclaim_contig(npages, low, high, alignment,
1480 mtx_unlock(&vm_page_queue_free_mtx);
1485 * Initialize the pages. Only the PG_ZERO flag is inherited.
1488 if ((req & VM_ALLOC_ZERO) != 0)
1490 if ((req & VM_ALLOC_NODUMP) != 0)
1492 if ((req & VM_ALLOC_WIRED) != 0)
1493 atomic_add_int(&cnt.v_wire_count, npages);
1494 oflags = VPO_UNMANAGED;
1495 if (object != NULL) {
1496 if ((req & VM_ALLOC_NOBUSY) == 0)
1498 if (object->memattr != VM_MEMATTR_DEFAULT &&
1499 memattr == VM_MEMATTR_DEFAULT)
1500 memattr = object->memattr;
1502 for (m = m_ret; m < &m_ret[npages]; m++) {
1504 m->flags = (m->flags | PG_NODUMP) & flags;
1505 if ((req & VM_ALLOC_WIRED) != 0)
1507 /* Unmanaged pages don't use "act_count". */
1509 if (memattr != VM_MEMATTR_DEFAULT)
1510 pmap_page_set_memattr(m, memattr);
1512 vm_page_insert(m, object, pindex);
1517 while (deferred_vdrop_list != NULL) {
1518 vdrop((struct vnode *)deferred_vdrop_list->pageq.tqe_prev);
1519 deferred_vdrop_list = deferred_vdrop_list->pageq.tqe_next;
1521 if (vm_paging_needed())
1522 pagedaemon_wakeup();
1527 * Initialize a page that has been freshly dequeued from a freelist.
1528 * The caller has to drop the vnode returned, if it is not NULL.
1530 * This function may only be used to initialize unmanaged pages.
1532 * To be called with vm_page_queue_free_mtx held.
1534 static struct vnode *
1535 vm_page_alloc_init(vm_page_t m)
1538 vm_object_t m_object;
1540 KASSERT(m->queue == PQ_NONE,
1541 ("vm_page_alloc_init: page %p has unexpected queue %d",
1543 KASSERT(m->wire_count == 0,
1544 ("vm_page_alloc_init: page %p is wired", m));
1545 KASSERT(m->hold_count == 0,
1546 ("vm_page_alloc_init: page %p is held", m));
1547 KASSERT(m->busy == 0,
1548 ("vm_page_alloc_init: page %p is busy", m));
1549 KASSERT(m->dirty == 0,
1550 ("vm_page_alloc_init: page %p is dirty", m));
1551 KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT,
1552 ("vm_page_alloc_init: page %p has unexpected memattr %d",
1553 m, pmap_page_get_memattr(m)));
1554 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1556 if ((m->flags & PG_CACHED) != 0) {
1557 KASSERT((m->flags & PG_ZERO) == 0,
1558 ("vm_page_alloc_init: cached page %p is PG_ZERO", m));
1560 m_object = m->object;
1561 vm_page_cache_remove(m);
1562 if (m_object->type == OBJT_VNODE &&
1563 vm_object_cache_is_empty(m_object))
1564 drop = m_object->handle;
1566 KASSERT(VM_PAGE_IS_FREE(m),
1567 ("vm_page_alloc_init: page %p is not free", m));
1568 KASSERT(m->valid == 0,
1569 ("vm_page_alloc_init: free page %p is valid", m));
1571 if ((m->flags & PG_ZERO) != 0)
1572 vm_page_zero_count--;
1574 /* Don't clear the PG_ZERO flag; we'll need it later. */
1575 m->flags &= PG_ZERO;
1580 * vm_page_alloc_freelist:
1582 * Allocate a physical page from the specified free page list.
1584 * The caller must always specify an allocation class.
1586 * allocation classes:
1587 * VM_ALLOC_NORMAL normal process request
1588 * VM_ALLOC_SYSTEM system *really* needs a page
1589 * VM_ALLOC_INTERRUPT interrupt time request
1591 * optional allocation flags:
1592 * VM_ALLOC_COUNT(number) the number of additional pages that the caller
1593 * intends to allocate
1594 * VM_ALLOC_WIRED wire the allocated page
1595 * VM_ALLOC_ZERO prefer a zeroed page
1597 * This routine may not sleep.
1600 vm_page_alloc_freelist(int flind, int req)
1607 req_class = req & VM_ALLOC_CLASS_MASK;
1610 * The page daemon is allowed to dig deeper into the free page list.
1612 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
1613 req_class = VM_ALLOC_SYSTEM;
1616 * Do not allocate reserved pages unless the req has asked for it.
1618 mtx_lock(&vm_page_queue_free_mtx);
1619 if (cnt.v_free_count + cnt.v_cache_count > cnt.v_free_reserved ||
1620 (req_class == VM_ALLOC_SYSTEM &&
1621 cnt.v_free_count + cnt.v_cache_count > cnt.v_interrupt_free_min) ||
1622 (req_class == VM_ALLOC_INTERRUPT &&
1623 cnt.v_free_count + cnt.v_cache_count > 0))
1624 m = vm_phys_alloc_freelist_pages(flind, VM_FREEPOOL_DIRECT, 0);
1626 mtx_unlock(&vm_page_queue_free_mtx);
1627 atomic_add_int(&vm_pageout_deficit,
1628 max((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1));
1629 pagedaemon_wakeup();
1633 mtx_unlock(&vm_page_queue_free_mtx);
1636 drop = vm_page_alloc_init(m);
1637 mtx_unlock(&vm_page_queue_free_mtx);
1640 * Initialize the page. Only the PG_ZERO flag is inherited.
1644 if ((req & VM_ALLOC_ZERO) != 0)
1647 if ((req & VM_ALLOC_WIRED) != 0) {
1649 * The page lock is not required for wiring a page that does
1650 * not belong to an object.
1652 atomic_add_int(&cnt.v_wire_count, 1);
1655 /* Unmanaged pages don't use "act_count". */
1656 m->oflags = VPO_UNMANAGED;
1659 if (vm_paging_needed())
1660 pagedaemon_wakeup();
1665 * vm_wait: (also see VM_WAIT macro)
1667 * Sleep until free pages are available for allocation.
1668 * - Called in various places before memory allocations.
1674 mtx_lock(&vm_page_queue_free_mtx);
1675 if (curproc == pageproc) {
1676 vm_pageout_pages_needed = 1;
1677 msleep(&vm_pageout_pages_needed, &vm_page_queue_free_mtx,
1678 PDROP | PSWP, "VMWait", 0);
1680 if (!vm_pages_needed) {
1681 vm_pages_needed = 1;
1682 wakeup(&vm_pages_needed);
1684 msleep(&cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PVM,
1690 * vm_waitpfault: (also see VM_WAITPFAULT macro)
1692 * Sleep until free pages are available for allocation.
1693 * - Called only in vm_fault so that processes page faulting
1694 * can be easily tracked.
1695 * - Sleeps at a lower priority than vm_wait() so that vm_wait()ing
1696 * processes will be able to grab memory first. Do not change
1697 * this balance without careful testing first.
1703 mtx_lock(&vm_page_queue_free_mtx);
1704 if (!vm_pages_needed) {
1705 vm_pages_needed = 1;
1706 wakeup(&vm_pages_needed);
1708 msleep(&cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PUSER,
1715 * Remove the given page from its current page queue.
1717 * The page must be locked.
1720 vm_page_dequeue(vm_page_t m)
1722 struct vm_pagequeue *pq;
1724 vm_page_lock_assert(m, MA_OWNED);
1725 KASSERT(m->queue != PQ_NONE,
1726 ("vm_page_dequeue: page %p is not queued", m));
1727 pq = &vm_pagequeues[m->queue];
1728 vm_pagequeue_lock(pq);
1730 TAILQ_REMOVE(&pq->pq_pl, m, pageq);
1732 vm_pagequeue_unlock(pq);
1736 * vm_page_dequeue_locked:
1738 * Remove the given page from its current page queue.
1740 * The page and page queue must be locked.
1743 vm_page_dequeue_locked(vm_page_t m)
1745 struct vm_pagequeue *pq;
1747 vm_page_lock_assert(m, MA_OWNED);
1748 pq = &vm_pagequeues[m->queue];
1749 vm_pagequeue_assert_locked(pq);
1751 TAILQ_REMOVE(&pq->pq_pl, m, pageq);
1758 * Add the given page to the specified page queue.
1760 * The page must be locked.
1763 vm_page_enqueue(int queue, vm_page_t m)
1765 struct vm_pagequeue *pq;
1767 vm_page_lock_assert(m, MA_OWNED);
1768 pq = &vm_pagequeues[queue];
1769 vm_pagequeue_lock(pq);
1771 TAILQ_INSERT_TAIL(&pq->pq_pl, m, pageq);
1773 vm_pagequeue_unlock(pq);
1779 * Move the given page to the tail of its current page queue.
1781 * The page must be locked.
1784 vm_page_requeue(vm_page_t m)
1786 struct vm_pagequeue *pq;
1788 vm_page_lock_assert(m, MA_OWNED);
1789 KASSERT(m->queue != PQ_NONE,
1790 ("vm_page_requeue: page %p is not queued", m));
1791 pq = &vm_pagequeues[m->queue];
1792 vm_pagequeue_lock(pq);
1793 TAILQ_REMOVE(&pq->pq_pl, m, pageq);
1794 TAILQ_INSERT_TAIL(&pq->pq_pl, m, pageq);
1795 vm_pagequeue_unlock(pq);
1799 * vm_page_requeue_locked:
1801 * Move the given page to the tail of its current page queue.
1803 * The page queue must be locked.
1806 vm_page_requeue_locked(vm_page_t m)
1808 struct vm_pagequeue *pq;
1810 KASSERT(m->queue != PQ_NONE,
1811 ("vm_page_requeue_locked: page %p is not queued", m));
1812 pq = &vm_pagequeues[m->queue];
1813 vm_pagequeue_assert_locked(pq);
1814 TAILQ_REMOVE(&pq->pq_pl, m, pageq);
1815 TAILQ_INSERT_TAIL(&pq->pq_pl, m, pageq);
1821 * Put the specified page on the active list (if appropriate).
1822 * Ensure that act_count is at least ACT_INIT but do not otherwise
1825 * The page must be locked.
1828 vm_page_activate(vm_page_t m)
1832 vm_page_lock_assert(m, MA_OWNED);
1833 if ((queue = m->queue) != PQ_ACTIVE) {
1834 if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) {
1835 if (m->act_count < ACT_INIT)
1836 m->act_count = ACT_INIT;
1837 if (queue != PQ_NONE)
1839 vm_page_enqueue(PQ_ACTIVE, m);
1841 KASSERT(queue == PQ_NONE,
1842 ("vm_page_activate: wired page %p is queued", m));
1844 if (m->act_count < ACT_INIT)
1845 m->act_count = ACT_INIT;
1850 * vm_page_free_wakeup:
1852 * Helper routine for vm_page_free_toq() and vm_page_cache(). This
1853 * routine is called when a page has been added to the cache or free
1856 * The page queues must be locked.
1859 vm_page_free_wakeup(void)
1862 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1864 * if pageout daemon needs pages, then tell it that there are
1867 if (vm_pageout_pages_needed &&
1868 cnt.v_cache_count + cnt.v_free_count >= cnt.v_pageout_free_min) {
1869 wakeup(&vm_pageout_pages_needed);
1870 vm_pageout_pages_needed = 0;
1873 * wakeup processes that are waiting on memory if we hit a
1874 * high water mark. And wakeup scheduler process if we have
1875 * lots of memory. this process will swapin processes.
1877 if (vm_pages_needed && !vm_page_count_min()) {
1878 vm_pages_needed = 0;
1879 wakeup(&cnt.v_free_count);
1886 * Returns the given page to the free list,
1887 * disassociating it with any VM object.
1889 * The object must be locked. The page must be locked if it is managed.
1892 vm_page_free_toq(vm_page_t m)
1895 if ((m->oflags & VPO_UNMANAGED) == 0) {
1896 vm_page_lock_assert(m, MA_OWNED);
1897 KASSERT(!pmap_page_is_mapped(m),
1898 ("vm_page_free_toq: freeing mapped page %p", m));
1900 KASSERT(m->queue == PQ_NONE,
1901 ("vm_page_free_toq: unmanaged page %p is queued", m));
1902 PCPU_INC(cnt.v_tfree);
1904 if (VM_PAGE_IS_FREE(m))
1905 panic("vm_page_free: freeing free page %p", m);
1906 else if (m->busy != 0)
1907 panic("vm_page_free: freeing busy page %p", m);
1910 * Unqueue, then remove page. Note that we cannot destroy
1911 * the page here because we do not want to call the pager's
1912 * callback routine until after we've put the page on the
1913 * appropriate free queue.
1919 * If fictitious remove object association and
1920 * return, otherwise delay object association removal.
1922 if ((m->flags & PG_FICTITIOUS) != 0) {
1929 if (m->wire_count != 0)
1930 panic("vm_page_free: freeing wired page %p", m);
1931 if (m->hold_count != 0) {
1932 m->flags &= ~PG_ZERO;
1933 KASSERT((m->flags & PG_UNHOLDFREE) == 0,
1934 ("vm_page_free: freeing PG_UNHOLDFREE page %p", m));
1935 m->flags |= PG_UNHOLDFREE;
1938 * Restore the default memory attribute to the page.
1940 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT)
1941 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT);
1944 * Insert the page into the physical memory allocator's
1945 * cache/free page queues.
1947 mtx_lock(&vm_page_queue_free_mtx);
1948 m->flags |= PG_FREE;
1950 #if VM_NRESERVLEVEL > 0
1951 if (!vm_reserv_free_page(m))
1955 vm_phys_free_pages(m, 0);
1956 if ((m->flags & PG_ZERO) != 0)
1957 ++vm_page_zero_count;
1959 vm_page_zero_idle_wakeup();
1960 vm_page_free_wakeup();
1961 mtx_unlock(&vm_page_queue_free_mtx);
1968 * Mark this page as wired down by yet
1969 * another map, removing it from paging queues
1972 * If the page is fictitious, then its wire count must remain one.
1974 * The page must be locked.
1977 vm_page_wire(vm_page_t m)
1981 * Only bump the wire statistics if the page is not already wired,
1982 * and only unqueue the page if it is on some queue (if it is unmanaged
1983 * it is already off the queues).
1985 vm_page_lock_assert(m, MA_OWNED);
1986 if ((m->flags & PG_FICTITIOUS) != 0) {
1987 KASSERT(m->wire_count == 1,
1988 ("vm_page_wire: fictitious page %p's wire count isn't one",
1992 if (m->wire_count == 0) {
1993 KASSERT((m->oflags & VPO_UNMANAGED) == 0 ||
1994 m->queue == PQ_NONE,
1995 ("vm_page_wire: unmanaged page %p is queued", m));
1997 atomic_add_int(&cnt.v_wire_count, 1);
2000 KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m));
2006 * Release one wiring of the specified page, potentially enabling it to be
2007 * paged again. If paging is enabled, then the value of the parameter
2008 * "activate" determines to which queue the page is added. If "activate" is
2009 * non-zero, then the page is added to the active queue. Otherwise, it is
2010 * added to the inactive queue.
2012 * However, unless the page belongs to an object, it is not enqueued because
2013 * it cannot be paged out.
2015 * If a page is fictitious, then its wire count must alway be one.
2017 * A managed page must be locked.
2020 vm_page_unwire(vm_page_t m, int activate)
2023 if ((m->oflags & VPO_UNMANAGED) == 0)
2024 vm_page_lock_assert(m, MA_OWNED);
2025 if ((m->flags & PG_FICTITIOUS) != 0) {
2026 KASSERT(m->wire_count == 1,
2027 ("vm_page_unwire: fictitious page %p's wire count isn't one", m));
2030 if (m->wire_count > 0) {
2032 if (m->wire_count == 0) {
2033 atomic_subtract_int(&cnt.v_wire_count, 1);
2034 if ((m->oflags & VPO_UNMANAGED) != 0 ||
2038 m->flags &= ~PG_WINATCFLS;
2039 vm_page_enqueue(activate ? PQ_ACTIVE : PQ_INACTIVE, m);
2042 panic("vm_page_unwire: page %p's wire count is zero", m);
2046 * Move the specified page to the inactive queue.
2048 * Many pages placed on the inactive queue should actually go
2049 * into the cache, but it is difficult to figure out which. What
2050 * we do instead, if the inactive target is well met, is to put
2051 * clean pages at the head of the inactive queue instead of the tail.
2052 * This will cause them to be moved to the cache more quickly and
2053 * if not actively re-referenced, reclaimed more quickly. If we just
2054 * stick these pages at the end of the inactive queue, heavy filesystem
2055 * meta-data accesses can cause an unnecessary paging load on memory bound
2056 * processes. This optimization causes one-time-use metadata to be
2057 * reused more quickly.
2059 * Normally athead is 0 resulting in LRU operation. athead is set
2060 * to 1 if we want this page to be 'as if it were placed in the cache',
2061 * except without unmapping it from the process address space.
2063 * The page must be locked.
2066 _vm_page_deactivate(vm_page_t m, int athead)
2068 struct vm_pagequeue *pq;
2071 vm_page_lock_assert(m, MA_OWNED);
2074 * Ignore if already inactive.
2076 if ((queue = m->queue) == PQ_INACTIVE)
2078 if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) {
2079 if (queue != PQ_NONE)
2081 m->flags &= ~PG_WINATCFLS;
2082 pq = &vm_pagequeues[PQ_INACTIVE];
2083 vm_pagequeue_lock(pq);
2084 m->queue = PQ_INACTIVE;
2086 TAILQ_INSERT_HEAD(&pq->pq_pl, m, pageq);
2088 TAILQ_INSERT_TAIL(&pq->pq_pl, m, pageq);
2089 cnt.v_inactive_count++;
2090 vm_pagequeue_unlock(pq);
2095 * Move the specified page to the inactive queue.
2097 * The page must be locked.
2100 vm_page_deactivate(vm_page_t m)
2103 _vm_page_deactivate(m, 0);
2107 * vm_page_try_to_cache:
2109 * Returns 0 on failure, 1 on success
2112 vm_page_try_to_cache(vm_page_t m)
2115 vm_page_lock_assert(m, MA_OWNED);
2116 VM_OBJECT_ASSERT_WLOCKED(m->object);
2117 if (m->dirty || m->hold_count || m->busy || m->wire_count ||
2118 (m->oflags & (VPO_BUSY | VPO_UNMANAGED)) != 0)
2128 * vm_page_try_to_free()
2130 * Attempt to free the page. If we cannot free it, we do nothing.
2131 * 1 is returned on success, 0 on failure.
2134 vm_page_try_to_free(vm_page_t m)
2137 vm_page_lock_assert(m, MA_OWNED);
2138 if (m->object != NULL)
2139 VM_OBJECT_ASSERT_WLOCKED(m->object);
2140 if (m->dirty || m->hold_count || m->busy || m->wire_count ||
2141 (m->oflags & (VPO_BUSY | VPO_UNMANAGED)) != 0)
2153 * Put the specified page onto the page cache queue (if appropriate).
2155 * The object and page must be locked.
2158 vm_page_cache(vm_page_t m)
2161 boolean_t cache_was_empty;
2163 vm_page_lock_assert(m, MA_OWNED);
2165 VM_OBJECT_ASSERT_WLOCKED(object);
2166 if ((m->oflags & (VPO_UNMANAGED | VPO_BUSY)) || m->busy ||
2167 m->hold_count || m->wire_count)
2168 panic("vm_page_cache: attempting to cache busy page");
2169 KASSERT(!pmap_page_is_mapped(m),
2170 ("vm_page_cache: page %p is mapped", m));
2171 KASSERT(m->dirty == 0, ("vm_page_cache: page %p is dirty", m));
2172 if (m->valid == 0 || object->type == OBJT_DEFAULT ||
2173 (object->type == OBJT_SWAP &&
2174 !vm_pager_has_page(object, m->pindex, NULL, NULL))) {
2176 * Hypothesis: A cache-elgible page belonging to a
2177 * default object or swap object but without a backing
2178 * store must be zero filled.
2183 KASSERT((m->flags & PG_CACHED) == 0,
2184 ("vm_page_cache: page %p is already cached", m));
2185 PCPU_INC(cnt.v_tcached);
2188 * Remove the page from the paging queues.
2193 * Remove the page from the object's collection of resident
2196 vm_radix_remove(&object->rtree, m->pindex);
2197 TAILQ_REMOVE(&object->memq, m, listq);
2198 object->resident_page_count--;
2201 * Restore the default memory attribute to the page.
2203 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT)
2204 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT);
2207 * Insert the page into the object's collection of cached pages
2208 * and the physical memory allocator's cache/free page queues.
2210 m->flags &= ~PG_ZERO;
2211 mtx_lock(&vm_page_queue_free_mtx);
2212 m->flags |= PG_CACHED;
2213 cnt.v_cache_count++;
2214 cache_was_empty = vm_radix_is_empty(&object->cache);
2215 vm_radix_insert(&object->cache, m);
2216 #if VM_NRESERVLEVEL > 0
2217 if (!vm_reserv_free_page(m)) {
2221 vm_phys_set_pool(VM_FREEPOOL_CACHE, m, 0);
2222 vm_phys_free_pages(m, 0);
2224 vm_page_free_wakeup();
2225 mtx_unlock(&vm_page_queue_free_mtx);
2228 * Increment the vnode's hold count if this is the object's only
2229 * cached page. Decrement the vnode's hold count if this was
2230 * the object's only resident page.
2232 if (object->type == OBJT_VNODE) {
2233 if (cache_was_empty && object->resident_page_count != 0)
2234 vhold(object->handle);
2235 else if (!cache_was_empty && object->resident_page_count == 0)
2236 vdrop(object->handle);
2243 * Cache, deactivate, or do nothing as appropriate. This routine
2244 * is typically used by madvise() MADV_DONTNEED.
2246 * Generally speaking we want to move the page into the cache so
2247 * it gets reused quickly. However, this can result in a silly syndrome
2248 * due to the page recycling too quickly. Small objects will not be
2249 * fully cached. On the otherhand, if we move the page to the inactive
2250 * queue we wind up with a problem whereby very large objects
2251 * unnecessarily blow away our inactive and cache queues.
2253 * The solution is to move the pages based on a fixed weighting. We
2254 * either leave them alone, deactivate them, or move them to the cache,
2255 * where moving them to the cache has the highest weighting.
2256 * By forcing some pages into other queues we eventually force the
2257 * system to balance the queues, potentially recovering other unrelated
2258 * space from active. The idea is to not force this to happen too
2261 * The object and page must be locked.
2264 vm_page_dontneed(vm_page_t m)
2269 vm_page_lock_assert(m, MA_OWNED);
2270 VM_OBJECT_ASSERT_WLOCKED(m->object);
2271 dnw = PCPU_GET(dnweight);
2275 * Occasionally leave the page alone.
2277 if ((dnw & 0x01F0) == 0 || m->queue == PQ_INACTIVE) {
2278 if (m->act_count >= ACT_INIT)
2284 * Clear any references to the page. Otherwise, the page daemon will
2285 * immediately reactivate the page.
2287 * Perform the pmap_clear_reference() first. Otherwise, a concurrent
2288 * pmap operation, such as pmap_remove(), could clear a reference in
2289 * the pmap and set PGA_REFERENCED on the page before the
2290 * pmap_clear_reference() had completed. Consequently, the page would
2291 * appear referenced based upon an old reference that occurred before
2292 * this function ran.
2294 pmap_clear_reference(m);
2295 vm_page_aflag_clear(m, PGA_REFERENCED);
2297 if (m->dirty == 0 && pmap_is_modified(m))
2300 if (m->dirty || (dnw & 0x0070) == 0) {
2302 * Deactivate the page 3 times out of 32.
2307 * Cache the page 28 times out of every 32. Note that
2308 * the page is deactivated instead of cached, but placed
2309 * at the head of the queue instead of the tail.
2313 _vm_page_deactivate(m, head);
2317 * Grab a page, waiting until we are waken up due to the page
2318 * changing state. We keep on waiting, if the page continues
2319 * to be in the object. If the page doesn't exist, first allocate it
2320 * and then conditionally zero it.
2322 * The caller must always specify the VM_ALLOC_RETRY flag. This is intended
2323 * to facilitate its eventual removal.
2325 * This routine may sleep.
2327 * The object must be locked on entry. The lock will, however, be released
2328 * and reacquired if the routine sleeps.
2331 vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags)
2335 VM_OBJECT_ASSERT_WLOCKED(object);
2336 KASSERT((allocflags & VM_ALLOC_RETRY) != 0,
2337 ("vm_page_grab: VM_ALLOC_RETRY is required"));
2339 if ((m = vm_page_lookup(object, pindex)) != NULL) {
2340 if ((m->oflags & VPO_BUSY) != 0 ||
2341 ((allocflags & VM_ALLOC_IGN_SBUSY) == 0 && m->busy != 0)) {
2343 * Reference the page before unlocking and
2344 * sleeping so that the page daemon is less
2345 * likely to reclaim it.
2347 vm_page_aflag_set(m, PGA_REFERENCED);
2348 vm_page_sleep(m, "pgrbwt");
2351 if ((allocflags & VM_ALLOC_WIRED) != 0) {
2356 if ((allocflags & VM_ALLOC_NOBUSY) == 0)
2361 m = vm_page_alloc(object, pindex, allocflags & ~(VM_ALLOC_RETRY |
2362 VM_ALLOC_IGN_SBUSY));
2364 VM_OBJECT_WUNLOCK(object);
2366 VM_OBJECT_WLOCK(object);
2368 } else if (m->valid != 0)
2370 if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0)
2376 * Mapping function for valid or dirty bits in a page.
2378 * Inputs are required to range within a page.
2381 vm_page_bits(int base, int size)
2387 base + size <= PAGE_SIZE,
2388 ("vm_page_bits: illegal base/size %d/%d", base, size)
2391 if (size == 0) /* handle degenerate case */
2394 first_bit = base >> DEV_BSHIFT;
2395 last_bit = (base + size - 1) >> DEV_BSHIFT;
2397 return (((vm_page_bits_t)2 << last_bit) -
2398 ((vm_page_bits_t)1 << first_bit));
2402 * vm_page_set_valid_range:
2404 * Sets portions of a page valid. The arguments are expected
2405 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
2406 * of any partial chunks touched by the range. The invalid portion of
2407 * such chunks will be zeroed.
2409 * (base + size) must be less then or equal to PAGE_SIZE.
2412 vm_page_set_valid_range(vm_page_t m, int base, int size)
2416 VM_OBJECT_ASSERT_WLOCKED(m->object);
2417 if (size == 0) /* handle degenerate case */
2421 * If the base is not DEV_BSIZE aligned and the valid
2422 * bit is clear, we have to zero out a portion of the
2425 if ((frag = base & ~(DEV_BSIZE - 1)) != base &&
2426 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0)
2427 pmap_zero_page_area(m, frag, base - frag);
2430 * If the ending offset is not DEV_BSIZE aligned and the
2431 * valid bit is clear, we have to zero out a portion of
2434 endoff = base + size;
2435 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff &&
2436 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0)
2437 pmap_zero_page_area(m, endoff,
2438 DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
2441 * Assert that no previously invalid block that is now being validated
2444 KASSERT((~m->valid & vm_page_bits(base, size) & m->dirty) == 0,
2445 ("vm_page_set_valid_range: page %p is dirty", m));
2448 * Set valid bits inclusive of any overlap.
2450 m->valid |= vm_page_bits(base, size);
2454 * Clear the given bits from the specified page's dirty field.
2456 static __inline void
2457 vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits)
2460 #if PAGE_SIZE < 16384
2465 * If the object is locked and the page is neither VPO_BUSY nor
2466 * write mapped, then the page's dirty field cannot possibly be
2467 * set by a concurrent pmap operation.
2469 VM_OBJECT_ASSERT_WLOCKED(m->object);
2470 if ((m->oflags & VPO_BUSY) == 0 && !pmap_page_is_write_mapped(m))
2471 m->dirty &= ~pagebits;
2474 * The pmap layer can call vm_page_dirty() without
2475 * holding a distinguished lock. The combination of
2476 * the object's lock and an atomic operation suffice
2477 * to guarantee consistency of the page dirty field.
2479 * For PAGE_SIZE == 32768 case, compiler already
2480 * properly aligns the dirty field, so no forcible
2481 * alignment is needed. Only require existence of
2482 * atomic_clear_64 when page size is 32768.
2484 addr = (uintptr_t)&m->dirty;
2485 #if PAGE_SIZE == 32768
2486 atomic_clear_64((uint64_t *)addr, pagebits);
2487 #elif PAGE_SIZE == 16384
2488 atomic_clear_32((uint32_t *)addr, pagebits);
2489 #else /* PAGE_SIZE <= 8192 */
2491 * Use a trick to perform a 32-bit atomic on the
2492 * containing aligned word, to not depend on the existence
2493 * of atomic_clear_{8, 16}.
2495 shift = addr & (sizeof(uint32_t) - 1);
2496 #if BYTE_ORDER == BIG_ENDIAN
2497 shift = (sizeof(uint32_t) - sizeof(m->dirty) - shift) * NBBY;
2501 addr &= ~(sizeof(uint32_t) - 1);
2502 atomic_clear_32((uint32_t *)addr, pagebits << shift);
2503 #endif /* PAGE_SIZE */
2508 * vm_page_set_validclean:
2510 * Sets portions of a page valid and clean. The arguments are expected
2511 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
2512 * of any partial chunks touched by the range. The invalid portion of
2513 * such chunks will be zero'd.
2515 * (base + size) must be less then or equal to PAGE_SIZE.
2518 vm_page_set_validclean(vm_page_t m, int base, int size)
2520 vm_page_bits_t oldvalid, pagebits;
2523 VM_OBJECT_ASSERT_WLOCKED(m->object);
2524 if (size == 0) /* handle degenerate case */
2528 * If the base is not DEV_BSIZE aligned and the valid
2529 * bit is clear, we have to zero out a portion of the
2532 if ((frag = base & ~(DEV_BSIZE - 1)) != base &&
2533 (m->valid & ((vm_page_bits_t)1 << (base >> DEV_BSHIFT))) == 0)
2534 pmap_zero_page_area(m, frag, base - frag);
2537 * If the ending offset is not DEV_BSIZE aligned and the
2538 * valid bit is clear, we have to zero out a portion of
2541 endoff = base + size;
2542 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff &&
2543 (m->valid & ((vm_page_bits_t)1 << (endoff >> DEV_BSHIFT))) == 0)
2544 pmap_zero_page_area(m, endoff,
2545 DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
2548 * Set valid, clear dirty bits. If validating the entire
2549 * page we can safely clear the pmap modify bit. We also
2550 * use this opportunity to clear the VPO_NOSYNC flag. If a process
2551 * takes a write fault on a MAP_NOSYNC memory area the flag will
2554 * We set valid bits inclusive of any overlap, but we can only
2555 * clear dirty bits for DEV_BSIZE chunks that are fully within
2558 oldvalid = m->valid;
2559 pagebits = vm_page_bits(base, size);
2560 m->valid |= pagebits;
2562 if ((frag = base & (DEV_BSIZE - 1)) != 0) {
2563 frag = DEV_BSIZE - frag;
2569 pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1));
2571 if (base == 0 && size == PAGE_SIZE) {
2573 * The page can only be modified within the pmap if it is
2574 * mapped, and it can only be mapped if it was previously
2577 if (oldvalid == VM_PAGE_BITS_ALL)
2579 * Perform the pmap_clear_modify() first. Otherwise,
2580 * a concurrent pmap operation, such as
2581 * pmap_protect(), could clear a modification in the
2582 * pmap and set the dirty field on the page before
2583 * pmap_clear_modify() had begun and after the dirty
2584 * field was cleared here.
2586 pmap_clear_modify(m);
2588 m->oflags &= ~VPO_NOSYNC;
2589 } else if (oldvalid != VM_PAGE_BITS_ALL)
2590 m->dirty &= ~pagebits;
2592 vm_page_clear_dirty_mask(m, pagebits);
2596 vm_page_clear_dirty(vm_page_t m, int base, int size)
2599 vm_page_clear_dirty_mask(m, vm_page_bits(base, size));
2603 * vm_page_set_invalid:
2605 * Invalidates DEV_BSIZE'd chunks within a page. Both the
2606 * valid and dirty bits for the effected areas are cleared.
2609 vm_page_set_invalid(vm_page_t m, int base, int size)
2611 vm_page_bits_t bits;
2613 VM_OBJECT_ASSERT_WLOCKED(m->object);
2614 KASSERT((m->oflags & VPO_BUSY) == 0,
2615 ("vm_page_set_invalid: page %p is busy", m));
2616 bits = vm_page_bits(base, size);
2617 if (m->valid == VM_PAGE_BITS_ALL && bits != 0)
2619 KASSERT(!pmap_page_is_mapped(m),
2620 ("vm_page_set_invalid: page %p is mapped", m));
2626 * vm_page_zero_invalid()
2628 * The kernel assumes that the invalid portions of a page contain
2629 * garbage, but such pages can be mapped into memory by user code.
2630 * When this occurs, we must zero out the non-valid portions of the
2631 * page so user code sees what it expects.
2633 * Pages are most often semi-valid when the end of a file is mapped
2634 * into memory and the file's size is not page aligned.
2637 vm_page_zero_invalid(vm_page_t m, boolean_t setvalid)
2642 VM_OBJECT_ASSERT_WLOCKED(m->object);
2644 * Scan the valid bits looking for invalid sections that
2645 * must be zerod. Invalid sub-DEV_BSIZE'd areas ( where the
2646 * valid bit may be set ) have already been zerod by
2647 * vm_page_set_validclean().
2649 for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) {
2650 if (i == (PAGE_SIZE / DEV_BSIZE) ||
2651 (m->valid & ((vm_page_bits_t)1 << i))) {
2653 pmap_zero_page_area(m,
2654 b << DEV_BSHIFT, (i - b) << DEV_BSHIFT);
2661 * setvalid is TRUE when we can safely set the zero'd areas
2662 * as being valid. We can do this if there are no cache consistancy
2663 * issues. e.g. it is ok to do with UFS, but not ok to do with NFS.
2666 m->valid = VM_PAGE_BITS_ALL;
2672 * Is (partial) page valid? Note that the case where size == 0
2673 * will return FALSE in the degenerate case where the page is
2674 * entirely invalid, and TRUE otherwise.
2677 vm_page_is_valid(vm_page_t m, int base, int size)
2679 vm_page_bits_t bits;
2681 VM_OBJECT_ASSERT_WLOCKED(m->object);
2682 bits = vm_page_bits(base, size);
2683 return (m->valid != 0 && (m->valid & bits) == bits);
2687 * Set the page's dirty bits if the page is modified.
2690 vm_page_test_dirty(vm_page_t m)
2693 VM_OBJECT_ASSERT_WLOCKED(m->object);
2694 if (m->dirty != VM_PAGE_BITS_ALL && pmap_is_modified(m))
2699 vm_page_lock_KBI(vm_page_t m, const char *file, int line)
2702 mtx_lock_flags_(vm_page_lockptr(m), 0, file, line);
2706 vm_page_unlock_KBI(vm_page_t m, const char *file, int line)
2709 mtx_unlock_flags_(vm_page_lockptr(m), 0, file, line);
2713 vm_page_trylock_KBI(vm_page_t m, const char *file, int line)
2716 return (mtx_trylock_flags_(vm_page_lockptr(m), 0, file, line));
2719 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
2721 vm_page_assert_locked_KBI(vm_page_t m, const char *file, int line)
2724 vm_page_lock_assert_KBI(m, MA_OWNED, file, line);
2728 vm_page_lock_assert_KBI(vm_page_t m, int a, const char *file, int line)
2731 mtx_assert_(vm_page_lockptr(m), a, file, line);
2735 int so_zerocp_fullpage = 0;
2738 * Replace the given page with a copy. The copied page assumes
2739 * the portion of the given page's "wire_count" that is not the
2740 * responsibility of this copy-on-write mechanism.
2742 * The object containing the given page must have a non-zero
2743 * paging-in-progress count and be locked.
2746 vm_page_cowfault(vm_page_t m)
2752 vm_page_lock_assert(m, MA_OWNED);
2754 VM_OBJECT_ASSERT_WLOCKED(object);
2755 KASSERT(object->paging_in_progress != 0,
2756 ("vm_page_cowfault: object %p's paging-in-progress count is zero.",
2763 mnew = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL | VM_ALLOC_NOBUSY);
2765 vm_page_insert(m, object, pindex);
2767 VM_OBJECT_WUNLOCK(object);
2769 VM_OBJECT_WLOCK(object);
2770 if (m == vm_page_lookup(object, pindex)) {
2775 * Page disappeared during the wait.
2783 * check to see if we raced with an xmit complete when
2784 * waiting to allocate a page. If so, put things back
2790 vm_page_unlock(mnew);
2791 vm_page_insert(m, object, pindex);
2792 } else { /* clear COW & copy page */
2793 if (!so_zerocp_fullpage)
2794 pmap_copy_page(m, mnew);
2795 mnew->valid = VM_PAGE_BITS_ALL;
2796 vm_page_dirty(mnew);
2797 mnew->wire_count = m->wire_count - m->cow;
2798 m->wire_count = m->cow;
2804 vm_page_cowclear(vm_page_t m)
2807 vm_page_lock_assert(m, MA_OWNED);
2811 * let vm_fault add back write permission lazily
2815 * sf_buf_free() will free the page, so we needn't do it here
2820 vm_page_cowsetup(vm_page_t m)
2823 vm_page_lock_assert(m, MA_OWNED);
2824 if ((m->flags & PG_FICTITIOUS) != 0 ||
2825 (m->oflags & VPO_UNMANAGED) != 0 ||
2826 m->cow == USHRT_MAX - 1 || !VM_OBJECT_TRYWLOCK(m->object))
2829 pmap_remove_write(m);
2830 VM_OBJECT_WUNLOCK(m->object);
2836 vm_page_object_lock_assert(vm_page_t m)
2840 * Certain of the page's fields may only be modified by the
2841 * holder of the containing object's lock or the setter of the
2842 * page's VPO_BUSY flag. Unfortunately, the setter of the
2843 * VPO_BUSY flag is not recorded, and thus cannot be checked
2846 if (m->object != NULL && (m->oflags & VPO_BUSY) == 0)
2847 VM_OBJECT_ASSERT_WLOCKED(m->object);
2851 #include "opt_ddb.h"
2853 #include <sys/kernel.h>
2855 #include <ddb/ddb.h>
2857 DB_SHOW_COMMAND(page, vm_page_print_page_info)
2859 db_printf("cnt.v_free_count: %d\n", cnt.v_free_count);
2860 db_printf("cnt.v_cache_count: %d\n", cnt.v_cache_count);
2861 db_printf("cnt.v_inactive_count: %d\n", cnt.v_inactive_count);
2862 db_printf("cnt.v_active_count: %d\n", cnt.v_active_count);
2863 db_printf("cnt.v_wire_count: %d\n", cnt.v_wire_count);
2864 db_printf("cnt.v_free_reserved: %d\n", cnt.v_free_reserved);
2865 db_printf("cnt.v_free_min: %d\n", cnt.v_free_min);
2866 db_printf("cnt.v_free_target: %d\n", cnt.v_free_target);
2867 db_printf("cnt.v_cache_min: %d\n", cnt.v_cache_min);
2868 db_printf("cnt.v_inactive_target: %d\n", cnt.v_inactive_target);
2871 DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info)
2874 db_printf("PQ_FREE:");
2875 db_printf(" %d", cnt.v_free_count);
2878 db_printf("PQ_CACHE:");
2879 db_printf(" %d", cnt.v_cache_count);
2882 db_printf("PQ_ACTIVE: %d, PQ_INACTIVE: %d\n",
2883 *vm_pagequeues[PQ_ACTIVE].pq_cnt,
2884 *vm_pagequeues[PQ_INACTIVE].pq_cnt);
2887 DB_SHOW_COMMAND(pginfo, vm_page_print_pginfo)
2893 db_printf("show pginfo addr\n");
2897 phys = strchr(modif, 'p') != NULL;
2899 m = PHYS_TO_VM_PAGE(addr);
2901 m = (vm_page_t)addr;
2903 "page %p obj %p pidx 0x%jx phys 0x%jx q %d hold %d wire %d\n"
2904 " af 0x%x of 0x%x f 0x%x act %d busy %d valid 0x%x dirty 0x%x\n",
2905 m, m->object, (uintmax_t)m->pindex, (uintmax_t)m->phys_addr,
2906 m->queue, m->hold_count, m->wire_count, m->aflags, m->oflags,
2907 m->flags, m->act_count, m->busy, m->valid, m->dirty);