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 mutex is held 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/sysctl.h>
100 #include <sys/vmmeter.h>
101 #include <sys/vnode.h>
105 #include <vm/vm_param.h>
106 #include <vm/vm_kern.h>
107 #include <vm/vm_object.h>
108 #include <vm/vm_page.h>
109 #include <vm/vm_pageout.h>
110 #include <vm/vm_pager.h>
111 #include <vm/vm_phys.h>
112 #include <vm/vm_radix.h>
113 #include <vm/vm_reserv.h>
114 #include <vm/vm_extern.h>
116 #include <vm/uma_int.h>
118 #include <machine/md_var.h>
121 * Associated with page of user-allocatable memory is a
125 struct vm_pagequeue vm_pagequeues[PQ_COUNT] = {
127 .pq_pl = TAILQ_HEAD_INITIALIZER(
128 vm_pagequeues[PQ_INACTIVE].pq_pl),
129 .pq_cnt = &cnt.v_inactive_count,
130 .pq_name = "vm inactive pagequeue"
133 .pq_pl = TAILQ_HEAD_INITIALIZER(
134 vm_pagequeues[PQ_ACTIVE].pq_pl),
135 .pq_cnt = &cnt.v_active_count,
136 .pq_name = "vm active pagequeue"
139 struct mtx_padalign vm_page_queue_free_mtx;
141 struct mtx_padalign pa_lock[PA_LOCK_COUNT];
143 vm_page_t vm_page_array;
144 long vm_page_array_size;
146 int vm_page_zero_count;
148 static int boot_pages = UMA_BOOT_PAGES;
149 TUNABLE_INT("vm.boot_pages", &boot_pages);
150 SYSCTL_INT(_vm, OID_AUTO, boot_pages, CTLFLAG_RD, &boot_pages, 0,
151 "number of pages allocated for bootstrapping the VM system");
153 static int pa_tryrelock_restart;
154 SYSCTL_INT(_vm, OID_AUTO, tryrelock_restart, CTLFLAG_RD,
155 &pa_tryrelock_restart, 0, "Number of tryrelock restarts");
157 static uma_zone_t fakepg_zone;
159 static struct vnode *vm_page_alloc_init(vm_page_t m);
160 static void vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits);
161 static void vm_page_enqueue(int queue, vm_page_t m);
162 static void vm_page_init_fakepg(void *dummy);
164 SYSINIT(vm_page, SI_SUB_VM, SI_ORDER_SECOND, vm_page_init_fakepg, NULL);
167 vm_page_init_fakepg(void *dummy)
170 fakepg_zone = uma_zcreate("fakepg", sizeof(struct vm_page), NULL, NULL,
171 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE | UMA_ZONE_VM);
174 /* Make sure that u_long is at least 64 bits when PAGE_SIZE is 32K. */
175 #if PAGE_SIZE == 32768
177 CTASSERT(sizeof(u_long) >= 8);
182 * Try to acquire a physical address lock while a pmap is locked. If we
183 * fail to trylock we unlock and lock the pmap directly and cache the
184 * locked pa in *locked. The caller should then restart their loop in case
185 * the virtual to physical mapping has changed.
188 vm_page_pa_tryrelock(pmap_t pmap, vm_paddr_t pa, vm_paddr_t *locked)
195 PA_LOCK_ASSERT(lockpa, MA_OWNED);
196 if (PA_LOCKPTR(pa) == PA_LOCKPTR(lockpa))
203 atomic_add_int(&pa_tryrelock_restart, 1);
212 * Sets the page size, perhaps based upon the memory
213 * size. Must be called before any use of page-size
214 * dependent functions.
217 vm_set_page_size(void)
219 if (cnt.v_page_size == 0)
220 cnt.v_page_size = PAGE_SIZE;
221 if (((cnt.v_page_size - 1) & cnt.v_page_size) != 0)
222 panic("vm_set_page_size: page size not a power of two");
226 * vm_page_blacklist_lookup:
228 * See if a physical address in this page has been listed
229 * in the blacklist tunable. Entries in the tunable are
230 * separated by spaces or commas. If an invalid integer is
231 * encountered then the rest of the string is skipped.
234 vm_page_blacklist_lookup(char *list, vm_paddr_t pa)
239 for (pos = list; *pos != '\0'; pos = cp) {
240 bad = strtoq(pos, &cp, 0);
242 if (*cp == ' ' || *cp == ',') {
249 if (pa == trunc_page(bad))
258 * Initializes the resident memory module.
260 * Allocates memory for the page cells, and
261 * for the object/offset-to-page hash table headers.
262 * Each page cell is initialized and placed on the free list.
265 vm_page_startup(vm_offset_t vaddr)
268 vm_paddr_t page_range;
275 /* the biggest memory array is the second group of pages */
277 vm_paddr_t biggestsize;
278 vm_paddr_t low_water, high_water;
283 vaddr = round_page(vaddr);
285 for (i = 0; phys_avail[i + 1]; i += 2) {
286 phys_avail[i] = round_page(phys_avail[i]);
287 phys_avail[i + 1] = trunc_page(phys_avail[i + 1]);
290 low_water = phys_avail[0];
291 high_water = phys_avail[1];
293 for (i = 0; phys_avail[i + 1]; i += 2) {
294 vm_paddr_t size = phys_avail[i + 1] - phys_avail[i];
296 if (size > biggestsize) {
300 if (phys_avail[i] < low_water)
301 low_water = phys_avail[i];
302 if (phys_avail[i + 1] > high_water)
303 high_water = phys_avail[i + 1];
310 end = phys_avail[biggestone+1];
313 * Initialize the page and queue locks.
315 mtx_init(&vm_page_queue_free_mtx, "vm page free queue", NULL, MTX_DEF);
316 for (i = 0; i < PA_LOCK_COUNT; i++)
317 mtx_init(&pa_lock[i], "vm page", NULL, MTX_DEF);
318 for (i = 0; i < PQ_COUNT; i++)
319 vm_pagequeue_init_lock(&vm_pagequeues[i]);
322 * Allocate memory for use when boot strapping the kernel memory
325 new_end = end - (boot_pages * UMA_SLAB_SIZE);
326 new_end = trunc_page(new_end);
327 mapped = pmap_map(&vaddr, new_end, end,
328 VM_PROT_READ | VM_PROT_WRITE);
329 bzero((void *)mapped, end - new_end);
330 uma_startup((void *)mapped, boot_pages);
332 #if defined(__amd64__) || defined(__i386__) || defined(__arm__) || \
335 * Allocate a bitmap to indicate that a random physical page
336 * needs to be included in a minidump.
338 * The amd64 port needs this to indicate which direct map pages
339 * need to be dumped, via calls to dump_add_page()/dump_drop_page().
341 * However, i386 still needs this workspace internally within the
342 * minidump code. In theory, they are not needed on i386, but are
343 * included should the sf_buf code decide to use them.
346 for (i = 0; dump_avail[i + 1] != 0; i += 2)
347 if (dump_avail[i + 1] > last_pa)
348 last_pa = dump_avail[i + 1];
349 page_range = last_pa / PAGE_SIZE;
350 vm_page_dump_size = round_page(roundup2(page_range, NBBY) / NBBY);
351 new_end -= vm_page_dump_size;
352 vm_page_dump = (void *)(uintptr_t)pmap_map(&vaddr, new_end,
353 new_end + vm_page_dump_size, VM_PROT_READ | VM_PROT_WRITE);
354 bzero((void *)vm_page_dump, vm_page_dump_size);
358 * Request that the physical pages underlying the message buffer be
359 * included in a crash dump. Since the message buffer is accessed
360 * through the direct map, they are not automatically included.
362 pa = DMAP_TO_PHYS((vm_offset_t)msgbufp->msg_ptr);
363 last_pa = pa + round_page(msgbufsize);
364 while (pa < last_pa) {
370 * Compute the number of pages of memory that will be available for
371 * use (taking into account the overhead of a page structure per
374 first_page = low_water / PAGE_SIZE;
375 #ifdef VM_PHYSSEG_SPARSE
377 for (i = 0; phys_avail[i + 1] != 0; i += 2)
378 page_range += atop(phys_avail[i + 1] - phys_avail[i]);
379 #elif defined(VM_PHYSSEG_DENSE)
380 page_range = high_water / PAGE_SIZE - first_page;
382 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
387 * Reserve an unmapped guard page to trap access to vm_page_array[-1].
392 * Initialize the mem entry structures now, and put them in the free
395 new_end = trunc_page(end - page_range * sizeof(struct vm_page));
396 mapped = pmap_map(&vaddr, new_end, end,
397 VM_PROT_READ | VM_PROT_WRITE);
398 vm_page_array = (vm_page_t) mapped;
399 #if VM_NRESERVLEVEL > 0
401 * Allocate memory for the reservation management system's data
404 new_end = vm_reserv_startup(&vaddr, new_end, high_water);
406 #if defined(__amd64__) || defined(__mips__)
408 * pmap_map on amd64 and mips can come out of the direct-map, not kvm
409 * like i386, so the pages must be tracked for a crashdump to include
410 * this data. This includes the vm_page_array and the early UMA
413 for (pa = new_end; pa < phys_avail[biggestone + 1]; pa += PAGE_SIZE)
416 phys_avail[biggestone + 1] = new_end;
419 * Clear all of the page structures
421 bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page));
422 for (i = 0; i < page_range; i++)
423 vm_page_array[i].order = VM_NFREEORDER;
424 vm_page_array_size = page_range;
427 * Initialize the physical memory allocator.
432 * Add every available physical page that is not blacklisted to
435 cnt.v_page_count = 0;
436 cnt.v_free_count = 0;
437 list = getenv("vm.blacklist");
438 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
440 last_pa = phys_avail[i + 1];
441 while (pa < last_pa) {
443 vm_page_blacklist_lookup(list, pa))
444 printf("Skipping page with pa 0x%jx\n",
447 vm_phys_add_page(pa);
452 #if VM_NRESERVLEVEL > 0
454 * Initialize the reservation management system.
462 vm_page_reference(vm_page_t m)
465 vm_page_aflag_set(m, PGA_REFERENCED);
469 vm_page_busy(vm_page_t m)
472 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
473 KASSERT((m->oflags & VPO_BUSY) == 0,
474 ("vm_page_busy: page already busy!!!"));
475 m->oflags |= VPO_BUSY;
481 * wakeup anyone waiting for the page.
484 vm_page_flash(vm_page_t m)
487 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
488 if (m->oflags & VPO_WANTED) {
489 m->oflags &= ~VPO_WANTED;
497 * clear the VPO_BUSY flag and wakeup anyone waiting for the
502 vm_page_wakeup(vm_page_t m)
505 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
506 KASSERT(m->oflags & VPO_BUSY, ("vm_page_wakeup: page not busy!!!"));
507 m->oflags &= ~VPO_BUSY;
512 vm_page_io_start(vm_page_t m)
515 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
520 vm_page_io_finish(vm_page_t m)
523 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
524 KASSERT(m->busy > 0, ("vm_page_io_finish: page %p is not busy", m));
531 * Keep page from being freed by the page daemon
532 * much of the same effect as wiring, except much lower
533 * overhead and should be used only for *very* temporary
534 * holding ("wiring").
537 vm_page_hold(vm_page_t mem)
540 vm_page_lock_assert(mem, MA_OWNED);
545 vm_page_unhold(vm_page_t mem)
548 vm_page_lock_assert(mem, MA_OWNED);
550 KASSERT(mem->hold_count >= 0, ("vm_page_unhold: hold count < 0!!!"));
551 if (mem->hold_count == 0 && (mem->flags & PG_UNHOLDFREE) != 0)
552 vm_page_free_toq(mem);
556 * vm_page_unhold_pages:
558 * Unhold each of the pages that is referenced by the given array.
561 vm_page_unhold_pages(vm_page_t *ma, int count)
563 struct mtx *mtx, *new_mtx;
566 for (; count != 0; count--) {
568 * Avoid releasing and reacquiring the same page lock.
570 new_mtx = vm_page_lockptr(*ma);
571 if (mtx != new_mtx) {
585 PHYS_TO_VM_PAGE(vm_paddr_t pa)
589 #ifdef VM_PHYSSEG_SPARSE
590 m = vm_phys_paddr_to_vm_page(pa);
592 m = vm_phys_fictitious_to_vm_page(pa);
594 #elif defined(VM_PHYSSEG_DENSE)
598 if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
599 m = &vm_page_array[pi - first_page];
602 return (vm_phys_fictitious_to_vm_page(pa));
604 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
611 * Create a fictitious page with the specified physical address and
612 * memory attribute. The memory attribute is the only the machine-
613 * dependent aspect of a fictitious page that must be initialized.
616 vm_page_getfake(vm_paddr_t paddr, vm_memattr_t memattr)
620 m = uma_zalloc(fakepg_zone, M_WAITOK | M_ZERO);
621 vm_page_initfake(m, paddr, memattr);
626 vm_page_initfake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
629 if ((m->flags & PG_FICTITIOUS) != 0) {
631 * The page's memattr might have changed since the
632 * previous initialization. Update the pmap to the
637 m->phys_addr = paddr;
639 /* Fictitious pages don't use "segind". */
640 m->flags = PG_FICTITIOUS;
641 /* Fictitious pages don't use "order" or "pool". */
642 m->oflags = VPO_BUSY | VPO_UNMANAGED;
645 pmap_page_set_memattr(m, memattr);
651 * Release a fictitious page.
654 vm_page_putfake(vm_page_t m)
657 KASSERT((m->oflags & VPO_UNMANAGED) != 0, ("managed %p", m));
658 KASSERT((m->flags & PG_FICTITIOUS) != 0,
659 ("vm_page_putfake: bad page %p", m));
660 uma_zfree(fakepg_zone, m);
664 * vm_page_updatefake:
666 * Update the given fictitious page to the specified physical address and
670 vm_page_updatefake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
673 KASSERT((m->flags & PG_FICTITIOUS) != 0,
674 ("vm_page_updatefake: bad page %p", m));
675 m->phys_addr = paddr;
676 pmap_page_set_memattr(m, memattr);
685 vm_page_free(vm_page_t m)
688 m->flags &= ~PG_ZERO;
695 * Free a page to the zerod-pages queue
698 vm_page_free_zero(vm_page_t m)
706 * Unbusy and handle the page queueing for a page from the VOP_GETPAGES()
707 * array which is not the request page.
710 vm_page_readahead_finish(vm_page_t m)
715 * Since the page is not the requested page, whether
716 * it should be activated or deactivated is not
717 * obvious. Empirical results have shown that
718 * deactivating the page is usually the best choice,
719 * unless the page is wanted by another thread.
721 if (m->oflags & VPO_WANTED) {
727 vm_page_deactivate(m);
733 * Free the completely invalid page. Such page state
734 * occurs due to the short read operation which did
735 * not covered our page at all, or in case when a read
747 * Sleep and release the page lock.
749 * The object containing the given page must be locked.
752 vm_page_sleep(vm_page_t m, const char *msg)
755 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
756 if (mtx_owned(vm_page_lockptr(m)))
760 * It's possible that while we sleep, the page will get
761 * unbusied and freed. If we are holding the object
762 * lock, we will assume we hold a reference to the object
763 * such that even if m->object changes, we can re-lock
766 m->oflags |= VPO_WANTED;
767 VM_OBJECT_SLEEP(m->object, m, PVM, msg, 0);
771 * vm_page_dirty_KBI: [ internal use only ]
773 * Set all bits in the page's dirty field.
775 * The object containing the specified page must be locked if the
776 * call is made from the machine-independent layer.
778 * See vm_page_clear_dirty_mask().
780 * This function should only be called by vm_page_dirty().
783 vm_page_dirty_KBI(vm_page_t m)
786 /* These assertions refer to this operation by its public name. */
787 KASSERT((m->flags & PG_CACHED) == 0,
788 ("vm_page_dirty: page in cache!"));
789 KASSERT(!VM_PAGE_IS_FREE(m),
790 ("vm_page_dirty: page is free!"));
791 KASSERT(m->valid == VM_PAGE_BITS_ALL,
792 ("vm_page_dirty: page is invalid!"));
793 m->dirty = VM_PAGE_BITS_ALL;
797 * vm_page_insert: [ internal use only ]
799 * Inserts the given mem entry into the object and object list.
801 * The pagetables are not updated but will presumably fault the page
802 * in if necessary, or if a kernel page the caller will at some point
803 * enter the page into the kernel's pmap. We are not allowed to sleep
804 * here so we *can't* do this anyway.
806 * The object must be locked.
809 vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex)
813 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
814 if (m->object != NULL)
815 panic("vm_page_insert: page already inserted");
818 * Record the object/offset pair in this page
824 * Now link into the object's ordered list of backed pages.
826 if (object->resident_page_count == 0) {
827 TAILQ_INSERT_TAIL(&object->memq, m, listq);
829 neighbor = vm_radix_lookup_ge(&object->rtree, pindex);
830 if (neighbor != NULL) {
831 KASSERT(pindex < neighbor->pindex,
832 ("vm_page_insert: offset %ju not minor than %ju",
833 (uintmax_t)pindex, (uintmax_t)neighbor->pindex));
834 TAILQ_INSERT_BEFORE(neighbor, m, listq);
836 TAILQ_INSERT_TAIL(&object->memq, m, listq);
838 vm_radix_insert(&object->rtree, pindex, m);
841 * Show that the object has one more resident page.
843 object->resident_page_count++;
846 * Hold the vnode until the last page is released.
848 if (object->resident_page_count == 1 && object->type == OBJT_VNODE)
849 vhold(object->handle);
852 * Since we are inserting a new and possibly dirty page,
853 * update the object's OBJ_MIGHTBEDIRTY flag.
855 if (pmap_page_is_write_mapped(m))
856 vm_object_set_writeable_dirty(object);
862 * Removes the given mem entry from the object/offset-page
863 * table and the object page list, but do not invalidate/terminate
866 * The underlying pmap entry (if any) is NOT removed here.
868 * The object must be locked. The page must be locked if it is managed.
871 vm_page_remove(vm_page_t m)
875 if ((m->oflags & VPO_UNMANAGED) == 0)
876 vm_page_lock_assert(m, MA_OWNED);
877 if ((object = m->object) == NULL)
879 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
880 if (m->oflags & VPO_BUSY) {
881 m->oflags &= ~VPO_BUSY;
886 * Now remove from the object's list of backed pages.
888 vm_radix_remove(&object->rtree, m->pindex);
889 TAILQ_REMOVE(&object->memq, m, listq);
892 * And show that the object has one fewer resident page.
894 object->resident_page_count--;
897 * The vnode may now be recycled.
899 if (object->resident_page_count == 0 && object->type == OBJT_VNODE)
900 vdrop(object->handle);
908 * Returns the page associated with the object/offset
909 * pair specified; if none is found, NULL is returned.
911 * The object must be locked.
914 vm_page_lookup(vm_object_t object, vm_pindex_t pindex)
917 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
918 return (vm_radix_lookup(&object->rtree, pindex));
922 * vm_page_find_least:
924 * Returns the page associated with the object with least pindex
925 * greater than or equal to the parameter pindex, or NULL.
927 * The object must be locked.
930 vm_page_find_least(vm_object_t object, vm_pindex_t pindex)
934 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
935 if ((m = TAILQ_FIRST(&object->memq)) != NULL && m->pindex < pindex)
936 m = vm_radix_lookup_ge(&object->rtree, pindex);
941 * Returns the given page's successor (by pindex) within the object if it is
942 * resident; if none is found, NULL is returned.
944 * The object must be locked.
947 vm_page_next(vm_page_t m)
951 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
952 if ((next = TAILQ_NEXT(m, listq)) != NULL &&
953 next->pindex != m->pindex + 1)
959 * Returns the given page's predecessor (by pindex) within the object if it is
960 * resident; if none is found, NULL is returned.
962 * The object must be locked.
965 vm_page_prev(vm_page_t m)
969 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
970 if ((prev = TAILQ_PREV(m, pglist, listq)) != NULL &&
971 prev->pindex != m->pindex - 1)
979 * Move the given memory entry from its
980 * current object to the specified target object/offset.
982 * Note: swap associated with the page must be invalidated by the move. We
983 * have to do this for several reasons: (1) we aren't freeing the
984 * page, (2) we are dirtying the page, (3) the VM system is probably
985 * moving the page from object A to B, and will then later move
986 * the backing store from A to B and we can't have a conflict.
988 * Note: we *always* dirty the page. It is necessary both for the
989 * fact that we moved it, and because we may be invalidating
990 * swap. If the page is on the cache, we have to deactivate it
991 * or vm_page_dirty() will panic. Dirty pages are not allowed
994 * The objects must be locked. The page must be locked if it is managed.
997 vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex)
1001 vm_page_insert(m, new_object, new_pindex);
1006 * Convert all of the given object's cached pages that have a
1007 * pindex within the given range into free pages. If the value
1008 * zero is given for "end", then the range's upper bound is
1009 * infinity. If the given object is backed by a vnode and it
1010 * transitions from having one or more cached pages to none, the
1011 * vnode's hold count is reduced.
1014 vm_page_cache_free(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1019 mtx_lock(&vm_page_queue_free_mtx);
1020 if (vm_object_cache_is_empty(object)) {
1021 mtx_unlock(&vm_page_queue_free_mtx);
1024 while ((m = vm_radix_lookup_ge(&object->cache, start)) != NULL) {
1025 if (end != 0 && m->pindex >= end)
1027 vm_radix_remove(&object->cache, m->pindex);
1030 /* Clear PG_CACHED and set PG_FREE. */
1031 m->flags ^= PG_CACHED | PG_FREE;
1032 KASSERT((m->flags & (PG_CACHED | PG_FREE)) == PG_FREE,
1033 ("vm_page_cache_free: page %p has inconsistent flags", m));
1034 cnt.v_cache_count--;
1037 empty = vm_object_cache_is_empty(object);
1038 mtx_unlock(&vm_page_queue_free_mtx);
1039 if (object->type == OBJT_VNODE && empty)
1040 vdrop(object->handle);
1044 * Returns the cached page that is associated with the given
1045 * object and offset. If, however, none exists, returns NULL.
1047 * The free page queue must be locked.
1049 static inline vm_page_t
1050 vm_page_cache_lookup(vm_object_t object, vm_pindex_t pindex)
1053 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1054 return (vm_radix_lookup(&object->cache, pindex));
1058 * Remove the given cached page from its containing object's
1059 * collection of cached pages.
1061 * The free page queue must be locked.
1064 vm_page_cache_remove(vm_page_t m)
1067 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1068 KASSERT((m->flags & PG_CACHED) != 0,
1069 ("vm_page_cache_remove: page %p is not cached", m));
1070 vm_radix_remove(&m->object->cache, m->pindex);
1072 cnt.v_cache_count--;
1076 * Transfer all of the cached pages with offset greater than or
1077 * equal to 'offidxstart' from the original object's cache to the
1078 * new object's cache. However, any cached pages with offset
1079 * greater than or equal to the new object's size are kept in the
1080 * original object. Initially, the new object's cache must be
1081 * empty. Offset 'offidxstart' in the original object must
1082 * correspond to offset zero in the new object.
1084 * The new object must be locked.
1087 vm_page_cache_transfer(vm_object_t orig_object, vm_pindex_t offidxstart,
1088 vm_object_t new_object)
1093 * Insertion into an object's collection of cached pages
1094 * requires the object to be locked. In contrast, removal does
1097 VM_OBJECT_LOCK_ASSERT(new_object, MA_OWNED);
1098 KASSERT(vm_object_cache_is_empty(new_object),
1099 ("vm_page_cache_transfer: object %p has cached pages",
1101 mtx_lock(&vm_page_queue_free_mtx);
1102 while ((m = vm_radix_lookup_ge(&orig_object->cache,
1103 offidxstart)) != NULL) {
1105 * Transfer all of the pages with offset greater than or
1106 * equal to 'offidxstart' from the original object's
1107 * cache to the new object's cache.
1109 if ((m->pindex - offidxstart) >= new_object->size)
1111 vm_radix_remove(&orig_object->cache, m->pindex);
1112 vm_radix_insert(&new_object->cache, m->pindex - offidxstart, m);
1113 /* Update the page's object and offset. */
1114 m->object = new_object;
1115 m->pindex -= offidxstart;
1117 mtx_unlock(&vm_page_queue_free_mtx);
1121 * Returns TRUE if a cached page is associated with the given object and
1122 * offset, and FALSE otherwise.
1124 * The object must be locked.
1127 vm_page_is_cached(vm_object_t object, vm_pindex_t pindex)
1132 * Insertion into an object's collection of cached pages requires the
1133 * object to be locked. Therefore, if the object is locked and the
1134 * object's collection is empty, there is no need to acquire the free
1135 * page queues lock in order to prove that the specified page doesn't
1138 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1139 if (vm_object_cache_is_empty(object))
1141 mtx_lock(&vm_page_queue_free_mtx);
1142 m = vm_page_cache_lookup(object, pindex);
1143 mtx_unlock(&vm_page_queue_free_mtx);
1150 * Allocate and return a page that is associated with the specified
1151 * object and offset pair. By default, this page has the flag VPO_BUSY
1154 * The caller must always specify an allocation class.
1156 * allocation classes:
1157 * VM_ALLOC_NORMAL normal process request
1158 * VM_ALLOC_SYSTEM system *really* needs a page
1159 * VM_ALLOC_INTERRUPT interrupt time request
1161 * optional allocation flags:
1162 * VM_ALLOC_COUNT(number) the number of additional pages that the caller
1163 * intends to allocate
1164 * VM_ALLOC_IFCACHED return page only if it is cached
1165 * VM_ALLOC_IFNOTCACHED return NULL, do not reactivate if the page
1167 * VM_ALLOC_NOBUSY do not set the flag VPO_BUSY on the page
1168 * VM_ALLOC_NODUMP do not include the page in a kernel core dump
1169 * VM_ALLOC_NOOBJ page is not associated with an object and
1170 * should not have the flag VPO_BUSY set
1171 * VM_ALLOC_WIRED wire the allocated page
1172 * VM_ALLOC_ZERO prefer a zeroed page
1174 * This routine may not sleep.
1177 vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req)
1179 struct vnode *vp = NULL;
1180 vm_object_t m_object;
1182 int flags, req_class;
1184 KASSERT((object != NULL) == ((req & VM_ALLOC_NOOBJ) == 0),
1185 ("vm_page_alloc: inconsistent object/req"));
1187 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1189 req_class = req & VM_ALLOC_CLASS_MASK;
1192 * The page daemon is allowed to dig deeper into the free page list.
1194 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
1195 req_class = VM_ALLOC_SYSTEM;
1197 mtx_lock(&vm_page_queue_free_mtx);
1198 if (cnt.v_free_count + cnt.v_cache_count > cnt.v_free_reserved ||
1199 (req_class == VM_ALLOC_SYSTEM &&
1200 cnt.v_free_count + cnt.v_cache_count > cnt.v_interrupt_free_min) ||
1201 (req_class == VM_ALLOC_INTERRUPT &&
1202 cnt.v_free_count + cnt.v_cache_count > 0)) {
1204 * Allocate from the free queue if the number of free pages
1205 * exceeds the minimum for the request class.
1207 if (object != NULL &&
1208 (m = vm_page_cache_lookup(object, pindex)) != NULL) {
1209 if ((req & VM_ALLOC_IFNOTCACHED) != 0) {
1210 mtx_unlock(&vm_page_queue_free_mtx);
1213 if (vm_phys_unfree_page(m))
1214 vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m, 0);
1215 #if VM_NRESERVLEVEL > 0
1216 else if (!vm_reserv_reactivate_page(m))
1220 panic("vm_page_alloc: cache page %p is missing"
1221 " from the free queue", m);
1222 } else if ((req & VM_ALLOC_IFCACHED) != 0) {
1223 mtx_unlock(&vm_page_queue_free_mtx);
1225 #if VM_NRESERVLEVEL > 0
1226 } else if (object == NULL || (object->flags & (OBJ_COLORED |
1227 OBJ_FICTITIOUS)) != OBJ_COLORED ||
1228 (m = vm_reserv_alloc_page(object, pindex)) == NULL) {
1232 m = vm_phys_alloc_pages(object != NULL ?
1233 VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT, 0);
1234 #if VM_NRESERVLEVEL > 0
1235 if (m == NULL && vm_reserv_reclaim_inactive()) {
1236 m = vm_phys_alloc_pages(object != NULL ?
1237 VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT,
1244 * Not allocatable, give up.
1246 mtx_unlock(&vm_page_queue_free_mtx);
1247 atomic_add_int(&vm_pageout_deficit,
1248 max((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1));
1249 pagedaemon_wakeup();
1254 * At this point we had better have found a good page.
1256 KASSERT(m != NULL, ("vm_page_alloc: missing page"));
1257 KASSERT(m->queue == PQ_NONE,
1258 ("vm_page_alloc: page %p has unexpected queue %d", m, m->queue));
1259 KASSERT(m->wire_count == 0, ("vm_page_alloc: page %p is wired", m));
1260 KASSERT(m->hold_count == 0, ("vm_page_alloc: page %p is held", m));
1261 KASSERT(m->busy == 0, ("vm_page_alloc: page %p is busy", m));
1262 KASSERT(m->dirty == 0, ("vm_page_alloc: page %p is dirty", m));
1263 KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT,
1264 ("vm_page_alloc: page %p has unexpected memattr %d", m,
1265 pmap_page_get_memattr(m)));
1266 if ((m->flags & PG_CACHED) != 0) {
1267 KASSERT((m->flags & PG_ZERO) == 0,
1268 ("vm_page_alloc: cached page %p is PG_ZERO", m));
1269 KASSERT(m->valid != 0,
1270 ("vm_page_alloc: cached page %p is invalid", m));
1271 if (m->object == object && m->pindex == pindex)
1272 cnt.v_reactivated++;
1275 m_object = m->object;
1276 vm_page_cache_remove(m);
1277 if (m_object->type == OBJT_VNODE &&
1278 vm_object_cache_is_empty(m_object))
1279 vp = m_object->handle;
1281 KASSERT(VM_PAGE_IS_FREE(m),
1282 ("vm_page_alloc: page %p is not free", m));
1283 KASSERT(m->valid == 0,
1284 ("vm_page_alloc: free page %p is valid", m));
1289 * Only the PG_ZERO flag is inherited. The PG_CACHED or PG_FREE flag
1290 * must be cleared before the free page queues lock is released.
1293 if (m->flags & PG_ZERO) {
1294 vm_page_zero_count--;
1295 if (req & VM_ALLOC_ZERO)
1298 if (req & VM_ALLOC_NODUMP)
1301 mtx_unlock(&vm_page_queue_free_mtx);
1303 m->oflags = object == NULL || (object->flags & OBJ_UNMANAGED) != 0 ?
1305 if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ)) == 0)
1306 m->oflags |= VPO_BUSY;
1307 if (req & VM_ALLOC_WIRED) {
1309 * The page lock is not required for wiring a page until that
1310 * page is inserted into the object.
1312 atomic_add_int(&cnt.v_wire_count, 1);
1317 if (object != NULL) {
1318 /* Ignore device objects; the pager sets "memattr" for them. */
1319 if (object->memattr != VM_MEMATTR_DEFAULT &&
1320 (object->flags & OBJ_FICTITIOUS) == 0)
1321 pmap_page_set_memattr(m, object->memattr);
1322 vm_page_insert(m, object, pindex);
1327 * The following call to vdrop() must come after the above call
1328 * to vm_page_insert() in case both affect the same object and
1329 * vnode. Otherwise, the affected vnode's hold count could
1330 * temporarily become zero.
1336 * Don't wakeup too often - wakeup the pageout daemon when
1337 * we would be nearly out of memory.
1339 if (vm_paging_needed())
1340 pagedaemon_wakeup();
1346 * vm_page_alloc_contig:
1348 * Allocate a contiguous set of physical pages of the given size "npages"
1349 * from the free lists. All of the physical pages must be at or above
1350 * the given physical address "low" and below the given physical address
1351 * "high". The given value "alignment" determines the alignment of the
1352 * first physical page in the set. If the given value "boundary" is
1353 * non-zero, then the set of physical pages cannot cross any physical
1354 * address boundary that is a multiple of that value. Both "alignment"
1355 * and "boundary" must be a power of two.
1357 * If the specified memory attribute, "memattr", is VM_MEMATTR_DEFAULT,
1358 * then the memory attribute setting for the physical pages is configured
1359 * to the object's memory attribute setting. Otherwise, the memory
1360 * attribute setting for the physical pages is configured to "memattr",
1361 * overriding the object's memory attribute setting. However, if the
1362 * object's memory attribute setting is not VM_MEMATTR_DEFAULT, then the
1363 * memory attribute setting for the physical pages cannot be configured
1364 * to VM_MEMATTR_DEFAULT.
1366 * The caller must always specify an allocation class.
1368 * allocation classes:
1369 * VM_ALLOC_NORMAL normal process request
1370 * VM_ALLOC_SYSTEM system *really* needs a page
1371 * VM_ALLOC_INTERRUPT interrupt time request
1373 * optional allocation flags:
1374 * VM_ALLOC_NOBUSY do not set the flag VPO_BUSY on the page
1375 * VM_ALLOC_NOOBJ page is not associated with an object and
1376 * should not have the flag VPO_BUSY set
1377 * VM_ALLOC_WIRED wire the allocated page
1378 * VM_ALLOC_ZERO prefer a zeroed page
1380 * This routine may not sleep.
1383 vm_page_alloc_contig(vm_object_t object, vm_pindex_t pindex, int req,
1384 u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
1385 vm_paddr_t boundary, vm_memattr_t memattr)
1388 vm_page_t deferred_vdrop_list, m, m_ret;
1389 u_int flags, oflags;
1392 KASSERT((object != NULL) == ((req & VM_ALLOC_NOOBJ) == 0),
1393 ("vm_page_alloc_contig: inconsistent object/req"));
1394 if (object != NULL) {
1395 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1396 KASSERT(object->type == OBJT_PHYS,
1397 ("vm_page_alloc_contig: object %p isn't OBJT_PHYS",
1400 KASSERT(npages > 0, ("vm_page_alloc_contig: npages is zero"));
1401 req_class = req & VM_ALLOC_CLASS_MASK;
1404 * The page daemon is allowed to dig deeper into the free page list.
1406 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
1407 req_class = VM_ALLOC_SYSTEM;
1409 deferred_vdrop_list = NULL;
1410 mtx_lock(&vm_page_queue_free_mtx);
1411 if (cnt.v_free_count + cnt.v_cache_count >= npages +
1412 cnt.v_free_reserved || (req_class == VM_ALLOC_SYSTEM &&
1413 cnt.v_free_count + cnt.v_cache_count >= npages +
1414 cnt.v_interrupt_free_min) || (req_class == VM_ALLOC_INTERRUPT &&
1415 cnt.v_free_count + cnt.v_cache_count >= npages)) {
1416 #if VM_NRESERVLEVEL > 0
1418 if (object == NULL || (object->flags & OBJ_COLORED) == 0 ||
1419 (m_ret = vm_reserv_alloc_contig(object, pindex, npages,
1420 low, high, alignment, boundary)) == NULL)
1422 m_ret = vm_phys_alloc_contig(npages, low, high,
1423 alignment, boundary);
1425 mtx_unlock(&vm_page_queue_free_mtx);
1426 atomic_add_int(&vm_pageout_deficit, npages);
1427 pagedaemon_wakeup();
1431 for (m = m_ret; m < &m_ret[npages]; m++) {
1432 drop = vm_page_alloc_init(m);
1435 * Enqueue the vnode for deferred vdrop().
1437 * Once the pages are removed from the free
1438 * page list, "pageq" can be safely abused to
1439 * construct a short-lived list of vnodes.
1441 m->pageq.tqe_prev = (void *)drop;
1442 m->pageq.tqe_next = deferred_vdrop_list;
1443 deferred_vdrop_list = m;
1447 #if VM_NRESERVLEVEL > 0
1448 if (vm_reserv_reclaim_contig(npages, low, high, alignment,
1453 mtx_unlock(&vm_page_queue_free_mtx);
1458 * Initialize the pages. Only the PG_ZERO flag is inherited.
1461 if ((req & VM_ALLOC_ZERO) != 0)
1463 if ((req & VM_ALLOC_NODUMP) != 0)
1465 if ((req & VM_ALLOC_WIRED) != 0)
1466 atomic_add_int(&cnt.v_wire_count, npages);
1467 oflags = VPO_UNMANAGED;
1468 if (object != NULL) {
1469 if ((req & VM_ALLOC_NOBUSY) == 0)
1471 if (object->memattr != VM_MEMATTR_DEFAULT &&
1472 memattr == VM_MEMATTR_DEFAULT)
1473 memattr = object->memattr;
1475 for (m = m_ret; m < &m_ret[npages]; m++) {
1477 m->flags = (m->flags | PG_NODUMP) & flags;
1478 if ((req & VM_ALLOC_WIRED) != 0)
1480 /* Unmanaged pages don't use "act_count". */
1482 if (memattr != VM_MEMATTR_DEFAULT)
1483 pmap_page_set_memattr(m, memattr);
1485 vm_page_insert(m, object, pindex);
1490 while (deferred_vdrop_list != NULL) {
1491 vdrop((struct vnode *)deferred_vdrop_list->pageq.tqe_prev);
1492 deferred_vdrop_list = deferred_vdrop_list->pageq.tqe_next;
1494 if (vm_paging_needed())
1495 pagedaemon_wakeup();
1500 * Initialize a page that has been freshly dequeued from a freelist.
1501 * The caller has to drop the vnode returned, if it is not NULL.
1503 * This function may only be used to initialize unmanaged pages.
1505 * To be called with vm_page_queue_free_mtx held.
1507 static struct vnode *
1508 vm_page_alloc_init(vm_page_t m)
1511 vm_object_t m_object;
1513 KASSERT(m->queue == PQ_NONE,
1514 ("vm_page_alloc_init: page %p has unexpected queue %d",
1516 KASSERT(m->wire_count == 0,
1517 ("vm_page_alloc_init: page %p is wired", m));
1518 KASSERT(m->hold_count == 0,
1519 ("vm_page_alloc_init: page %p is held", m));
1520 KASSERT(m->busy == 0,
1521 ("vm_page_alloc_init: page %p is busy", m));
1522 KASSERT(m->dirty == 0,
1523 ("vm_page_alloc_init: page %p is dirty", m));
1524 KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT,
1525 ("vm_page_alloc_init: page %p has unexpected memattr %d",
1526 m, pmap_page_get_memattr(m)));
1527 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1529 if ((m->flags & PG_CACHED) != 0) {
1530 KASSERT((m->flags & PG_ZERO) == 0,
1531 ("vm_page_alloc_init: cached page %p is PG_ZERO", m));
1533 m_object = m->object;
1534 vm_page_cache_remove(m);
1535 if (m_object->type == OBJT_VNODE &&
1536 vm_object_cache_is_empty(m_object))
1537 drop = m_object->handle;
1539 KASSERT(VM_PAGE_IS_FREE(m),
1540 ("vm_page_alloc_init: page %p is not free", m));
1541 KASSERT(m->valid == 0,
1542 ("vm_page_alloc_init: free page %p is valid", m));
1544 if ((m->flags & PG_ZERO) != 0)
1545 vm_page_zero_count--;
1547 /* Don't clear the PG_ZERO flag; we'll need it later. */
1548 m->flags &= PG_ZERO;
1553 * vm_page_alloc_freelist:
1555 * Allocate a physical page from the specified free page list.
1557 * The caller must always specify an allocation class.
1559 * allocation classes:
1560 * VM_ALLOC_NORMAL normal process request
1561 * VM_ALLOC_SYSTEM system *really* needs a page
1562 * VM_ALLOC_INTERRUPT interrupt time request
1564 * optional allocation flags:
1565 * VM_ALLOC_COUNT(number) the number of additional pages that the caller
1566 * intends to allocate
1567 * VM_ALLOC_WIRED wire the allocated page
1568 * VM_ALLOC_ZERO prefer a zeroed page
1570 * This routine may not sleep.
1573 vm_page_alloc_freelist(int flind, int req)
1580 req_class = req & VM_ALLOC_CLASS_MASK;
1583 * The page daemon is allowed to dig deeper into the free page list.
1585 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
1586 req_class = VM_ALLOC_SYSTEM;
1589 * Do not allocate reserved pages unless the req has asked for it.
1591 mtx_lock(&vm_page_queue_free_mtx);
1592 if (cnt.v_free_count + cnt.v_cache_count > cnt.v_free_reserved ||
1593 (req_class == VM_ALLOC_SYSTEM &&
1594 cnt.v_free_count + cnt.v_cache_count > cnt.v_interrupt_free_min) ||
1595 (req_class == VM_ALLOC_INTERRUPT &&
1596 cnt.v_free_count + cnt.v_cache_count > 0))
1597 m = vm_phys_alloc_freelist_pages(flind, VM_FREEPOOL_DIRECT, 0);
1599 mtx_unlock(&vm_page_queue_free_mtx);
1600 atomic_add_int(&vm_pageout_deficit,
1601 max((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1));
1602 pagedaemon_wakeup();
1606 mtx_unlock(&vm_page_queue_free_mtx);
1609 drop = vm_page_alloc_init(m);
1610 mtx_unlock(&vm_page_queue_free_mtx);
1613 * Initialize the page. Only the PG_ZERO flag is inherited.
1617 if ((req & VM_ALLOC_ZERO) != 0)
1620 if ((req & VM_ALLOC_WIRED) != 0) {
1622 * The page lock is not required for wiring a page that does
1623 * not belong to an object.
1625 atomic_add_int(&cnt.v_wire_count, 1);
1628 /* Unmanaged pages don't use "act_count". */
1629 m->oflags = VPO_UNMANAGED;
1632 if (vm_paging_needed())
1633 pagedaemon_wakeup();
1638 * vm_wait: (also see VM_WAIT macro)
1640 * Sleep until free pages are available for allocation.
1641 * - Called in various places before memory allocations.
1647 mtx_lock(&vm_page_queue_free_mtx);
1648 if (curproc == pageproc) {
1649 vm_pageout_pages_needed = 1;
1650 msleep(&vm_pageout_pages_needed, &vm_page_queue_free_mtx,
1651 PDROP | PSWP, "VMWait", 0);
1653 if (!vm_pages_needed) {
1654 vm_pages_needed = 1;
1655 wakeup(&vm_pages_needed);
1657 msleep(&cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PVM,
1663 * vm_waitpfault: (also see VM_WAITPFAULT macro)
1665 * Sleep until free pages are available for allocation.
1666 * - Called only in vm_fault so that processes page faulting
1667 * can be easily tracked.
1668 * - Sleeps at a lower priority than vm_wait() so that vm_wait()ing
1669 * processes will be able to grab memory first. Do not change
1670 * this balance without careful testing first.
1676 mtx_lock(&vm_page_queue_free_mtx);
1677 if (!vm_pages_needed) {
1678 vm_pages_needed = 1;
1679 wakeup(&vm_pages_needed);
1681 msleep(&cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PUSER,
1688 * Remove the given page from its current page queue.
1690 * The page must be locked.
1693 vm_page_dequeue(vm_page_t m)
1695 struct vm_pagequeue *pq;
1697 vm_page_lock_assert(m, MA_OWNED);
1698 KASSERT(m->queue != PQ_NONE,
1699 ("vm_page_dequeue: page %p is not queued", m));
1700 pq = &vm_pagequeues[m->queue];
1701 vm_pagequeue_lock(pq);
1703 TAILQ_REMOVE(&pq->pq_pl, m, pageq);
1705 vm_pagequeue_unlock(pq);
1709 * vm_page_dequeue_locked:
1711 * Remove the given page from its current page queue.
1713 * The page and page queue must be locked.
1716 vm_page_dequeue_locked(vm_page_t m)
1718 struct vm_pagequeue *pq;
1720 vm_page_lock_assert(m, MA_OWNED);
1721 pq = &vm_pagequeues[m->queue];
1722 vm_pagequeue_assert_locked(pq);
1724 TAILQ_REMOVE(&pq->pq_pl, m, pageq);
1731 * Add the given page to the specified page queue.
1733 * The page must be locked.
1736 vm_page_enqueue(int queue, vm_page_t m)
1738 struct vm_pagequeue *pq;
1740 vm_page_lock_assert(m, MA_OWNED);
1741 pq = &vm_pagequeues[queue];
1742 vm_pagequeue_lock(pq);
1744 TAILQ_INSERT_TAIL(&pq->pq_pl, m, pageq);
1746 vm_pagequeue_unlock(pq);
1752 * Move the given page to the tail of its current page queue.
1754 * The page must be locked.
1757 vm_page_requeue(vm_page_t m)
1759 struct vm_pagequeue *pq;
1761 vm_page_lock_assert(m, MA_OWNED);
1762 KASSERT(m->queue != PQ_NONE,
1763 ("vm_page_requeue: page %p is not queued", m));
1764 pq = &vm_pagequeues[m->queue];
1765 vm_pagequeue_lock(pq);
1766 TAILQ_REMOVE(&pq->pq_pl, m, pageq);
1767 TAILQ_INSERT_TAIL(&pq->pq_pl, m, pageq);
1768 vm_pagequeue_unlock(pq);
1772 * vm_page_requeue_locked:
1774 * Move the given page to the tail of its current page queue.
1776 * The page queue must be locked.
1779 vm_page_requeue_locked(vm_page_t m)
1781 struct vm_pagequeue *pq;
1783 KASSERT(m->queue != PQ_NONE,
1784 ("vm_page_requeue_locked: page %p is not queued", m));
1785 pq = &vm_pagequeues[m->queue];
1786 vm_pagequeue_assert_locked(pq);
1787 TAILQ_REMOVE(&pq->pq_pl, m, pageq);
1788 TAILQ_INSERT_TAIL(&pq->pq_pl, m, pageq);
1794 * Put the specified page on the active list (if appropriate).
1795 * Ensure that act_count is at least ACT_INIT but do not otherwise
1798 * The page must be locked.
1801 vm_page_activate(vm_page_t m)
1805 vm_page_lock_assert(m, MA_OWNED);
1806 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1807 if ((queue = m->queue) != PQ_ACTIVE) {
1808 if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) {
1809 if (m->act_count < ACT_INIT)
1810 m->act_count = ACT_INIT;
1811 if (queue != PQ_NONE)
1813 vm_page_enqueue(PQ_ACTIVE, m);
1815 KASSERT(queue == PQ_NONE,
1816 ("vm_page_activate: wired page %p is queued", m));
1818 if (m->act_count < ACT_INIT)
1819 m->act_count = ACT_INIT;
1824 * vm_page_free_wakeup:
1826 * Helper routine for vm_page_free_toq() and vm_page_cache(). This
1827 * routine is called when a page has been added to the cache or free
1830 * The page queues must be locked.
1833 vm_page_free_wakeup(void)
1836 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1838 * if pageout daemon needs pages, then tell it that there are
1841 if (vm_pageout_pages_needed &&
1842 cnt.v_cache_count + cnt.v_free_count >= cnt.v_pageout_free_min) {
1843 wakeup(&vm_pageout_pages_needed);
1844 vm_pageout_pages_needed = 0;
1847 * wakeup processes that are waiting on memory if we hit a
1848 * high water mark. And wakeup scheduler process if we have
1849 * lots of memory. this process will swapin processes.
1851 if (vm_pages_needed && !vm_page_count_min()) {
1852 vm_pages_needed = 0;
1853 wakeup(&cnt.v_free_count);
1860 * Returns the given page to the free list,
1861 * disassociating it with any VM object.
1863 * The object must be locked. The page must be locked if it is managed.
1866 vm_page_free_toq(vm_page_t m)
1869 if ((m->oflags & VPO_UNMANAGED) == 0) {
1870 vm_page_lock_assert(m, MA_OWNED);
1871 KASSERT(!pmap_page_is_mapped(m),
1872 ("vm_page_free_toq: freeing mapped page %p", m));
1874 KASSERT(m->queue == PQ_NONE,
1875 ("vm_page_free_toq: unmanaged page %p is queued", m));
1876 PCPU_INC(cnt.v_tfree);
1878 if (VM_PAGE_IS_FREE(m))
1879 panic("vm_page_free: freeing free page %p", m);
1880 else if (m->busy != 0)
1881 panic("vm_page_free: freeing busy page %p", m);
1884 * Unqueue, then remove page. Note that we cannot destroy
1885 * the page here because we do not want to call the pager's
1886 * callback routine until after we've put the page on the
1887 * appropriate free queue.
1893 * If fictitious remove object association and
1894 * return, otherwise delay object association removal.
1896 if ((m->flags & PG_FICTITIOUS) != 0) {
1903 if (m->wire_count != 0)
1904 panic("vm_page_free: freeing wired page %p", m);
1905 if (m->hold_count != 0) {
1906 m->flags &= ~PG_ZERO;
1907 KASSERT((m->flags & PG_UNHOLDFREE) == 0,
1908 ("vm_page_free: freeing PG_UNHOLDFREE page %p", m));
1909 m->flags |= PG_UNHOLDFREE;
1912 * Restore the default memory attribute to the page.
1914 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT)
1915 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT);
1918 * Insert the page into the physical memory allocator's
1919 * cache/free page queues.
1921 mtx_lock(&vm_page_queue_free_mtx);
1922 m->flags |= PG_FREE;
1924 #if VM_NRESERVLEVEL > 0
1925 if (!vm_reserv_free_page(m))
1929 vm_phys_free_pages(m, 0);
1930 if ((m->flags & PG_ZERO) != 0)
1931 ++vm_page_zero_count;
1933 vm_page_zero_idle_wakeup();
1934 vm_page_free_wakeup();
1935 mtx_unlock(&vm_page_queue_free_mtx);
1942 * Mark this page as wired down by yet
1943 * another map, removing it from paging queues
1946 * If the page is fictitious, then its wire count must remain one.
1948 * The page must be locked.
1951 vm_page_wire(vm_page_t m)
1955 * Only bump the wire statistics if the page is not already wired,
1956 * and only unqueue the page if it is on some queue (if it is unmanaged
1957 * it is already off the queues).
1959 vm_page_lock_assert(m, MA_OWNED);
1960 if ((m->flags & PG_FICTITIOUS) != 0) {
1961 KASSERT(m->wire_count == 1,
1962 ("vm_page_wire: fictitious page %p's wire count isn't one",
1966 if (m->wire_count == 0) {
1967 KASSERT((m->oflags & VPO_UNMANAGED) == 0 ||
1968 m->queue == PQ_NONE,
1969 ("vm_page_wire: unmanaged page %p is queued", m));
1971 atomic_add_int(&cnt.v_wire_count, 1);
1974 KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m));
1980 * Release one wiring of the specified page, potentially enabling it to be
1981 * paged again. If paging is enabled, then the value of the parameter
1982 * "activate" determines to which queue the page is added. If "activate" is
1983 * non-zero, then the page is added to the active queue. Otherwise, it is
1984 * added to the inactive queue.
1986 * However, unless the page belongs to an object, it is not enqueued because
1987 * it cannot be paged out.
1989 * If a page is fictitious, then its wire count must alway be one.
1991 * A managed page must be locked.
1994 vm_page_unwire(vm_page_t m, int activate)
1997 if ((m->oflags & VPO_UNMANAGED) == 0)
1998 vm_page_lock_assert(m, MA_OWNED);
1999 if ((m->flags & PG_FICTITIOUS) != 0) {
2000 KASSERT(m->wire_count == 1,
2001 ("vm_page_unwire: fictitious page %p's wire count isn't one", m));
2004 if (m->wire_count > 0) {
2006 if (m->wire_count == 0) {
2007 atomic_subtract_int(&cnt.v_wire_count, 1);
2008 if ((m->oflags & VPO_UNMANAGED) != 0 ||
2012 m->flags &= ~PG_WINATCFLS;
2013 vm_page_enqueue(activate ? PQ_ACTIVE : PQ_INACTIVE, m);
2016 panic("vm_page_unwire: page %p's wire count is zero", m);
2020 * Move the specified page to the inactive queue.
2022 * Many pages placed on the inactive queue should actually go
2023 * into the cache, but it is difficult to figure out which. What
2024 * we do instead, if the inactive target is well met, is to put
2025 * clean pages at the head of the inactive queue instead of the tail.
2026 * This will cause them to be moved to the cache more quickly and
2027 * if not actively re-referenced, reclaimed more quickly. If we just
2028 * stick these pages at the end of the inactive queue, heavy filesystem
2029 * meta-data accesses can cause an unnecessary paging load on memory bound
2030 * processes. This optimization causes one-time-use metadata to be
2031 * reused more quickly.
2033 * Normally athead is 0 resulting in LRU operation. athead is set
2034 * to 1 if we want this page to be 'as if it were placed in the cache',
2035 * except without unmapping it from the process address space.
2037 * The page must be locked.
2040 _vm_page_deactivate(vm_page_t m, int athead)
2042 struct vm_pagequeue *pq;
2045 vm_page_lock_assert(m, MA_OWNED);
2048 * Ignore if already inactive.
2050 if ((queue = m->queue) == PQ_INACTIVE)
2052 if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) {
2053 if (queue != PQ_NONE)
2055 m->flags &= ~PG_WINATCFLS;
2056 pq = &vm_pagequeues[PQ_INACTIVE];
2057 vm_pagequeue_lock(pq);
2058 m->queue = PQ_INACTIVE;
2060 TAILQ_INSERT_HEAD(&pq->pq_pl, m, pageq);
2062 TAILQ_INSERT_TAIL(&pq->pq_pl, m, pageq);
2063 cnt.v_inactive_count++;
2064 vm_pagequeue_unlock(pq);
2069 * Move the specified page to the inactive queue.
2071 * The page must be locked.
2074 vm_page_deactivate(vm_page_t m)
2077 _vm_page_deactivate(m, 0);
2081 * vm_page_try_to_cache:
2083 * Returns 0 on failure, 1 on success
2086 vm_page_try_to_cache(vm_page_t m)
2089 vm_page_lock_assert(m, MA_OWNED);
2090 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2091 if (m->dirty || m->hold_count || m->busy || m->wire_count ||
2092 (m->oflags & (VPO_BUSY | VPO_UNMANAGED)) != 0)
2102 * vm_page_try_to_free()
2104 * Attempt to free the page. If we cannot free it, we do nothing.
2105 * 1 is returned on success, 0 on failure.
2108 vm_page_try_to_free(vm_page_t m)
2111 vm_page_lock_assert(m, MA_OWNED);
2112 if (m->object != NULL)
2113 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2114 if (m->dirty || m->hold_count || m->busy || m->wire_count ||
2115 (m->oflags & (VPO_BUSY | VPO_UNMANAGED)) != 0)
2127 * Put the specified page onto the page cache queue (if appropriate).
2129 * The object and page must be locked.
2132 vm_page_cache(vm_page_t m)
2135 int old_empty_cache;
2137 vm_page_lock_assert(m, MA_OWNED);
2139 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2140 if ((m->oflags & (VPO_UNMANAGED | VPO_BUSY)) || m->busy ||
2141 m->hold_count || m->wire_count)
2142 panic("vm_page_cache: attempting to cache busy page");
2143 KASSERT(!pmap_page_is_mapped(m),
2144 ("vm_page_cache: page %p is mapped", m));
2145 KASSERT(m->dirty == 0, ("vm_page_cache: page %p is dirty", m));
2146 if (m->valid == 0 || object->type == OBJT_DEFAULT ||
2147 (object->type == OBJT_SWAP &&
2148 !vm_pager_has_page(object, m->pindex, NULL, NULL))) {
2150 * Hypothesis: A cache-elgible page belonging to a
2151 * default object or swap object but without a backing
2152 * store must be zero filled.
2157 KASSERT((m->flags & PG_CACHED) == 0,
2158 ("vm_page_cache: page %p is already cached", m));
2159 PCPU_INC(cnt.v_tcached);
2162 * Remove the page from the paging queues.
2167 * Remove the page from the object's collection of resident
2170 vm_radix_remove(&object->rtree, m->pindex);
2171 TAILQ_REMOVE(&object->memq, m, listq);
2172 object->resident_page_count--;
2175 * Restore the default memory attribute to the page.
2177 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT)
2178 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT);
2181 * Insert the page into the object's collection of cached pages
2182 * and the physical memory allocator's cache/free page queues.
2184 m->flags &= ~PG_ZERO;
2185 mtx_lock(&vm_page_queue_free_mtx);
2186 m->flags |= PG_CACHED;
2187 old_empty_cache = vm_object_cache_is_empty(object);
2188 cnt.v_cache_count++;
2189 vm_radix_insert(&object->cache, m->pindex, m);
2190 #if VM_NRESERVLEVEL > 0
2191 if (!vm_reserv_free_page(m)) {
2195 vm_phys_set_pool(VM_FREEPOOL_CACHE, m, 0);
2196 vm_phys_free_pages(m, 0);
2198 vm_page_free_wakeup();
2199 mtx_unlock(&vm_page_queue_free_mtx);
2202 * Increment the vnode's hold count if this is the object's only
2203 * cached page. Decrement the vnode's hold count if this was
2204 * the object's only resident page.
2206 if (object->type == OBJT_VNODE) {
2207 if (old_empty_cache != 0 && object->resident_page_count != 0)
2208 vhold(object->handle);
2209 else if (old_empty_cache == 0 &&
2210 object->resident_page_count == 0)
2211 vdrop(object->handle);
2218 * Cache, deactivate, or do nothing as appropriate. This routine
2219 * is typically used by madvise() MADV_DONTNEED.
2221 * Generally speaking we want to move the page into the cache so
2222 * it gets reused quickly. However, this can result in a silly syndrome
2223 * due to the page recycling too quickly. Small objects will not be
2224 * fully cached. On the otherhand, if we move the page to the inactive
2225 * queue we wind up with a problem whereby very large objects
2226 * unnecessarily blow away our inactive and cache queues.
2228 * The solution is to move the pages based on a fixed weighting. We
2229 * either leave them alone, deactivate them, or move them to the cache,
2230 * where moving them to the cache has the highest weighting.
2231 * By forcing some pages into other queues we eventually force the
2232 * system to balance the queues, potentially recovering other unrelated
2233 * space from active. The idea is to not force this to happen too
2236 * The object and page must be locked.
2239 vm_page_dontneed(vm_page_t m)
2244 vm_page_lock_assert(m, MA_OWNED);
2245 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2246 dnw = PCPU_GET(dnweight);
2250 * Occasionally leave the page alone.
2252 if ((dnw & 0x01F0) == 0 || m->queue == PQ_INACTIVE) {
2253 if (m->act_count >= ACT_INIT)
2259 * Clear any references to the page. Otherwise, the page daemon will
2260 * immediately reactivate the page.
2262 * Perform the pmap_clear_reference() first. Otherwise, a concurrent
2263 * pmap operation, such as pmap_remove(), could clear a reference in
2264 * the pmap and set PGA_REFERENCED on the page before the
2265 * pmap_clear_reference() had completed. Consequently, the page would
2266 * appear referenced based upon an old reference that occurred before
2267 * this function ran.
2269 pmap_clear_reference(m);
2270 vm_page_aflag_clear(m, PGA_REFERENCED);
2272 if (m->dirty == 0 && pmap_is_modified(m))
2275 if (m->dirty || (dnw & 0x0070) == 0) {
2277 * Deactivate the page 3 times out of 32.
2282 * Cache the page 28 times out of every 32. Note that
2283 * the page is deactivated instead of cached, but placed
2284 * at the head of the queue instead of the tail.
2288 _vm_page_deactivate(m, head);
2292 * Grab a page, waiting until we are waken up due to the page
2293 * changing state. We keep on waiting, if the page continues
2294 * to be in the object. If the page doesn't exist, first allocate it
2295 * and then conditionally zero it.
2297 * The caller must always specify the VM_ALLOC_RETRY flag. This is intended
2298 * to facilitate its eventual removal.
2300 * This routine may sleep.
2302 * The object must be locked on entry. The lock will, however, be released
2303 * and reacquired if the routine sleeps.
2306 vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags)
2310 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2311 KASSERT((allocflags & VM_ALLOC_RETRY) != 0,
2312 ("vm_page_grab: VM_ALLOC_RETRY is required"));
2314 if ((m = vm_page_lookup(object, pindex)) != NULL) {
2315 if ((m->oflags & VPO_BUSY) != 0 ||
2316 ((allocflags & VM_ALLOC_IGN_SBUSY) == 0 && m->busy != 0)) {
2318 * Reference the page before unlocking and
2319 * sleeping so that the page daemon is less
2320 * likely to reclaim it.
2322 vm_page_aflag_set(m, PGA_REFERENCED);
2323 vm_page_sleep(m, "pgrbwt");
2326 if ((allocflags & VM_ALLOC_WIRED) != 0) {
2331 if ((allocflags & VM_ALLOC_NOBUSY) == 0)
2336 m = vm_page_alloc(object, pindex, allocflags & ~(VM_ALLOC_RETRY |
2337 VM_ALLOC_IGN_SBUSY));
2339 VM_OBJECT_UNLOCK(object);
2341 VM_OBJECT_LOCK(object);
2343 } else if (m->valid != 0)
2345 if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0)
2351 * Mapping function for valid or dirty bits in a page.
2353 * Inputs are required to range within a page.
2356 vm_page_bits(int base, int size)
2362 base + size <= PAGE_SIZE,
2363 ("vm_page_bits: illegal base/size %d/%d", base, size)
2366 if (size == 0) /* handle degenerate case */
2369 first_bit = base >> DEV_BSHIFT;
2370 last_bit = (base + size - 1) >> DEV_BSHIFT;
2372 return (((vm_page_bits_t)2 << last_bit) -
2373 ((vm_page_bits_t)1 << first_bit));
2377 * vm_page_set_valid_range:
2379 * Sets portions of a page valid. The arguments are expected
2380 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
2381 * of any partial chunks touched by the range. The invalid portion of
2382 * such chunks will be zeroed.
2384 * (base + size) must be less then or equal to PAGE_SIZE.
2387 vm_page_set_valid_range(vm_page_t m, int base, int size)
2391 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2392 if (size == 0) /* handle degenerate case */
2396 * If the base is not DEV_BSIZE aligned and the valid
2397 * bit is clear, we have to zero out a portion of the
2400 if ((frag = base & ~(DEV_BSIZE - 1)) != base &&
2401 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0)
2402 pmap_zero_page_area(m, frag, base - frag);
2405 * If the ending offset is not DEV_BSIZE aligned and the
2406 * valid bit is clear, we have to zero out a portion of
2409 endoff = base + size;
2410 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff &&
2411 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0)
2412 pmap_zero_page_area(m, endoff,
2413 DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
2416 * Assert that no previously invalid block that is now being validated
2419 KASSERT((~m->valid & vm_page_bits(base, size) & m->dirty) == 0,
2420 ("vm_page_set_valid_range: page %p is dirty", m));
2423 * Set valid bits inclusive of any overlap.
2425 m->valid |= vm_page_bits(base, size);
2429 * Clear the given bits from the specified page's dirty field.
2431 static __inline void
2432 vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits)
2435 #if PAGE_SIZE < 16384
2440 * If the object is locked and the page is neither VPO_BUSY nor
2441 * write mapped, then the page's dirty field cannot possibly be
2442 * set by a concurrent pmap operation.
2444 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2445 if ((m->oflags & VPO_BUSY) == 0 && !pmap_page_is_write_mapped(m))
2446 m->dirty &= ~pagebits;
2449 * The pmap layer can call vm_page_dirty() without
2450 * holding a distinguished lock. The combination of
2451 * the object's lock and an atomic operation suffice
2452 * to guarantee consistency of the page dirty field.
2454 * For PAGE_SIZE == 32768 case, compiler already
2455 * properly aligns the dirty field, so no forcible
2456 * alignment is needed. Only require existence of
2457 * atomic_clear_64 when page size is 32768.
2459 addr = (uintptr_t)&m->dirty;
2460 #if PAGE_SIZE == 32768
2461 atomic_clear_64((uint64_t *)addr, pagebits);
2462 #elif PAGE_SIZE == 16384
2463 atomic_clear_32((uint32_t *)addr, pagebits);
2464 #else /* PAGE_SIZE <= 8192 */
2466 * Use a trick to perform a 32-bit atomic on the
2467 * containing aligned word, to not depend on the existence
2468 * of atomic_clear_{8, 16}.
2470 shift = addr & (sizeof(uint32_t) - 1);
2471 #if BYTE_ORDER == BIG_ENDIAN
2472 shift = (sizeof(uint32_t) - sizeof(m->dirty) - shift) * NBBY;
2476 addr &= ~(sizeof(uint32_t) - 1);
2477 atomic_clear_32((uint32_t *)addr, pagebits << shift);
2478 #endif /* PAGE_SIZE */
2483 * vm_page_set_validclean:
2485 * Sets portions of a page valid and clean. The arguments are expected
2486 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
2487 * of any partial chunks touched by the range. The invalid portion of
2488 * such chunks will be zero'd.
2490 * (base + size) must be less then or equal to PAGE_SIZE.
2493 vm_page_set_validclean(vm_page_t m, int base, int size)
2495 vm_page_bits_t oldvalid, pagebits;
2498 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2499 if (size == 0) /* handle degenerate case */
2503 * If the base is not DEV_BSIZE aligned and the valid
2504 * bit is clear, we have to zero out a portion of the
2507 if ((frag = base & ~(DEV_BSIZE - 1)) != base &&
2508 (m->valid & ((vm_page_bits_t)1 << (base >> DEV_BSHIFT))) == 0)
2509 pmap_zero_page_area(m, frag, base - frag);
2512 * If the ending offset is not DEV_BSIZE aligned and the
2513 * valid bit is clear, we have to zero out a portion of
2516 endoff = base + size;
2517 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff &&
2518 (m->valid & ((vm_page_bits_t)1 << (endoff >> DEV_BSHIFT))) == 0)
2519 pmap_zero_page_area(m, endoff,
2520 DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
2523 * Set valid, clear dirty bits. If validating the entire
2524 * page we can safely clear the pmap modify bit. We also
2525 * use this opportunity to clear the VPO_NOSYNC flag. If a process
2526 * takes a write fault on a MAP_NOSYNC memory area the flag will
2529 * We set valid bits inclusive of any overlap, but we can only
2530 * clear dirty bits for DEV_BSIZE chunks that are fully within
2533 oldvalid = m->valid;
2534 pagebits = vm_page_bits(base, size);
2535 m->valid |= pagebits;
2537 if ((frag = base & (DEV_BSIZE - 1)) != 0) {
2538 frag = DEV_BSIZE - frag;
2544 pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1));
2546 if (base == 0 && size == PAGE_SIZE) {
2548 * The page can only be modified within the pmap if it is
2549 * mapped, and it can only be mapped if it was previously
2552 if (oldvalid == VM_PAGE_BITS_ALL)
2554 * Perform the pmap_clear_modify() first. Otherwise,
2555 * a concurrent pmap operation, such as
2556 * pmap_protect(), could clear a modification in the
2557 * pmap and set the dirty field on the page before
2558 * pmap_clear_modify() had begun and after the dirty
2559 * field was cleared here.
2561 pmap_clear_modify(m);
2563 m->oflags &= ~VPO_NOSYNC;
2564 } else if (oldvalid != VM_PAGE_BITS_ALL)
2565 m->dirty &= ~pagebits;
2567 vm_page_clear_dirty_mask(m, pagebits);
2571 vm_page_clear_dirty(vm_page_t m, int base, int size)
2574 vm_page_clear_dirty_mask(m, vm_page_bits(base, size));
2578 * vm_page_set_invalid:
2580 * Invalidates DEV_BSIZE'd chunks within a page. Both the
2581 * valid and dirty bits for the effected areas are cleared.
2584 vm_page_set_invalid(vm_page_t m, int base, int size)
2586 vm_page_bits_t bits;
2588 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2589 KASSERT((m->oflags & VPO_BUSY) == 0,
2590 ("vm_page_set_invalid: page %p is busy", m));
2591 bits = vm_page_bits(base, size);
2592 if (m->valid == VM_PAGE_BITS_ALL && bits != 0)
2594 KASSERT(!pmap_page_is_mapped(m),
2595 ("vm_page_set_invalid: page %p is mapped", m));
2601 * vm_page_zero_invalid()
2603 * The kernel assumes that the invalid portions of a page contain
2604 * garbage, but such pages can be mapped into memory by user code.
2605 * When this occurs, we must zero out the non-valid portions of the
2606 * page so user code sees what it expects.
2608 * Pages are most often semi-valid when the end of a file is mapped
2609 * into memory and the file's size is not page aligned.
2612 vm_page_zero_invalid(vm_page_t m, boolean_t setvalid)
2617 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2619 * Scan the valid bits looking for invalid sections that
2620 * must be zerod. Invalid sub-DEV_BSIZE'd areas ( where the
2621 * valid bit may be set ) have already been zerod by
2622 * vm_page_set_validclean().
2624 for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) {
2625 if (i == (PAGE_SIZE / DEV_BSIZE) ||
2626 (m->valid & ((vm_page_bits_t)1 << i))) {
2628 pmap_zero_page_area(m,
2629 b << DEV_BSHIFT, (i - b) << DEV_BSHIFT);
2636 * setvalid is TRUE when we can safely set the zero'd areas
2637 * as being valid. We can do this if there are no cache consistancy
2638 * issues. e.g. it is ok to do with UFS, but not ok to do with NFS.
2641 m->valid = VM_PAGE_BITS_ALL;
2647 * Is (partial) page valid? Note that the case where size == 0
2648 * will return FALSE in the degenerate case where the page is
2649 * entirely invalid, and TRUE otherwise.
2652 vm_page_is_valid(vm_page_t m, int base, int size)
2654 vm_page_bits_t bits;
2656 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2657 bits = vm_page_bits(base, size);
2658 if (m->valid && ((m->valid & bits) == bits))
2665 * Set the page's dirty bits if the page is modified.
2668 vm_page_test_dirty(vm_page_t m)
2671 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2672 if (m->dirty != VM_PAGE_BITS_ALL && pmap_is_modified(m))
2677 vm_page_lock_KBI(vm_page_t m, const char *file, int line)
2680 mtx_lock_flags_(vm_page_lockptr(m), 0, file, line);
2684 vm_page_unlock_KBI(vm_page_t m, const char *file, int line)
2687 mtx_unlock_flags_(vm_page_lockptr(m), 0, file, line);
2691 vm_page_trylock_KBI(vm_page_t m, const char *file, int line)
2694 return (mtx_trylock_flags_(vm_page_lockptr(m), 0, file, line));
2697 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
2699 vm_page_lock_assert_KBI(vm_page_t m, int a, const char *file, int line)
2702 mtx_assert_(vm_page_lockptr(m), a, file, line);
2706 int so_zerocp_fullpage = 0;
2709 * Replace the given page with a copy. The copied page assumes
2710 * the portion of the given page's "wire_count" that is not the
2711 * responsibility of this copy-on-write mechanism.
2713 * The object containing the given page must have a non-zero
2714 * paging-in-progress count and be locked.
2717 vm_page_cowfault(vm_page_t m)
2723 vm_page_lock_assert(m, MA_OWNED);
2725 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2726 KASSERT(object->paging_in_progress != 0,
2727 ("vm_page_cowfault: object %p's paging-in-progress count is zero.",
2734 mnew = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL | VM_ALLOC_NOBUSY);
2736 vm_page_insert(m, object, pindex);
2738 VM_OBJECT_UNLOCK(object);
2740 VM_OBJECT_LOCK(object);
2741 if (m == vm_page_lookup(object, pindex)) {
2746 * Page disappeared during the wait.
2754 * check to see if we raced with an xmit complete when
2755 * waiting to allocate a page. If so, put things back
2761 vm_page_unlock(mnew);
2762 vm_page_insert(m, object, pindex);
2763 } else { /* clear COW & copy page */
2764 if (!so_zerocp_fullpage)
2765 pmap_copy_page(m, mnew);
2766 mnew->valid = VM_PAGE_BITS_ALL;
2767 vm_page_dirty(mnew);
2768 mnew->wire_count = m->wire_count - m->cow;
2769 m->wire_count = m->cow;
2775 vm_page_cowclear(vm_page_t m)
2778 vm_page_lock_assert(m, MA_OWNED);
2782 * let vm_fault add back write permission lazily
2786 * sf_buf_free() will free the page, so we needn't do it here
2791 vm_page_cowsetup(vm_page_t m)
2794 vm_page_lock_assert(m, MA_OWNED);
2795 if ((m->flags & PG_FICTITIOUS) != 0 ||
2796 (m->oflags & VPO_UNMANAGED) != 0 ||
2797 m->cow == USHRT_MAX - 1 || !VM_OBJECT_TRYLOCK(m->object))
2800 pmap_remove_write(m);
2801 VM_OBJECT_UNLOCK(m->object);
2807 vm_page_object_lock_assert(vm_page_t m)
2811 * Certain of the page's fields may only be modified by the
2812 * holder of the containing object's lock or the setter of the
2813 * page's VPO_BUSY flag. Unfortunately, the setter of the
2814 * VPO_BUSY flag is not recorded, and thus cannot be checked
2817 if (m->object != NULL && (m->oflags & VPO_BUSY) == 0)
2818 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2822 #include "opt_ddb.h"
2824 #include <sys/kernel.h>
2826 #include <ddb/ddb.h>
2828 DB_SHOW_COMMAND(page, vm_page_print_page_info)
2830 db_printf("cnt.v_free_count: %d\n", cnt.v_free_count);
2831 db_printf("cnt.v_cache_count: %d\n", cnt.v_cache_count);
2832 db_printf("cnt.v_inactive_count: %d\n", cnt.v_inactive_count);
2833 db_printf("cnt.v_active_count: %d\n", cnt.v_active_count);
2834 db_printf("cnt.v_wire_count: %d\n", cnt.v_wire_count);
2835 db_printf("cnt.v_free_reserved: %d\n", cnt.v_free_reserved);
2836 db_printf("cnt.v_free_min: %d\n", cnt.v_free_min);
2837 db_printf("cnt.v_free_target: %d\n", cnt.v_free_target);
2838 db_printf("cnt.v_cache_min: %d\n", cnt.v_cache_min);
2839 db_printf("cnt.v_inactive_target: %d\n", cnt.v_inactive_target);
2842 DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info)
2845 db_printf("PQ_FREE:");
2846 db_printf(" %d", cnt.v_free_count);
2849 db_printf("PQ_CACHE:");
2850 db_printf(" %d", cnt.v_cache_count);
2853 db_printf("PQ_ACTIVE: %d, PQ_INACTIVE: %d\n",
2854 *vm_pagequeues[PQ_ACTIVE].pq_cnt,
2855 *vm_pagequeues[PQ_INACTIVE].pq_cnt);