The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/vm/vm_page.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1991 Regents of the University of California.
    3  * All rights reserved.
    4  * Copyright (c) 1998 Matthew Dillon.  All Rights Reserved.
    5  *
    6  * This code is derived from software contributed to Berkeley by
    7  * The Mach Operating System project at Carnegie-Mellon University.
    8  *
    9  * Redistribution and use in source and binary forms, with or without
   10  * modification, are permitted provided that the following conditions
   11  * are met:
   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.
   20  *
   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
   31  * SUCH DAMAGE.
   32  *
   33  *      from: @(#)vm_page.c     7.4 (Berkeley) 5/7/91
   34  */
   35 
   36 /*-
   37  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
   38  * All rights reserved.
   39  *
   40  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
   41  *
   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.
   47  *
   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.
   51  *
   52  * Carnegie Mellon requests users of this software to return to
   53  *
   54  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
   55  *  School of Computer Science
   56  *  Carnegie Mellon University
   57  *  Pittsburgh PA 15213-3890
   58  *
   59  * any improvements or extensions that they make and grant Carnegie the
   60  * rights to redistribute these changes.
   61  */
   62 
   63 /*
   64  *                      GENERAL RULES ON VM_PAGE MANIPULATION
   65  *
   66  *      - A page queue lock is required when adding or removing a page from a
   67  *        page queue regardless of other locks or the busy state of a page.
   68  *
   69  *              * In general, no thread besides the page daemon can acquire or
   70  *                hold more than one page queue lock at a time.
   71  *
   72  *              * The page daemon can acquire and hold any pair of page queue
   73  *                locks in any order.
   74  *
   75  *      - The object lock is required when inserting or removing
   76  *        pages from an object (vm_page_insert() or vm_page_remove()).
   77  *
   78  */
   79 
   80 /*
   81  *      Resident memory management module.
   82  */
   83 
   84 #include <sys/cdefs.h>
   85 __FBSDID("$FreeBSD: releng/11.1/sys/vm/vm_page.c 337828 2018-08-15 02:30:11Z delphij $");
   86 
   87 #include "opt_vm.h"
   88 
   89 #include <sys/param.h>
   90 #include <sys/systm.h>
   91 #include <sys/lock.h>
   92 #include <sys/kernel.h>
   93 #include <sys/limits.h>
   94 #include <sys/linker.h>
   95 #include <sys/malloc.h>
   96 #include <sys/mman.h>
   97 #include <sys/msgbuf.h>
   98 #include <sys/mutex.h>
   99 #include <sys/proc.h>
  100 #include <sys/rwlock.h>
  101 #include <sys/sbuf.h>
  102 #include <sys/smp.h>
  103 #include <sys/sysctl.h>
  104 #include <sys/vmmeter.h>
  105 #include <sys/vnode.h>
  106 
  107 #include <vm/vm.h>
  108 #include <vm/pmap.h>
  109 #include <vm/vm_param.h>
  110 #include <vm/vm_kern.h>
  111 #include <vm/vm_object.h>
  112 #include <vm/vm_page.h>
  113 #include <vm/vm_pageout.h>
  114 #include <vm/vm_pager.h>
  115 #include <vm/vm_phys.h>
  116 #include <vm/vm_radix.h>
  117 #include <vm/vm_reserv.h>
  118 #include <vm/vm_extern.h>
  119 #include <vm/uma.h>
  120 #include <vm/uma_int.h>
  121 
  122 #include <machine/md_var.h>
  123 
  124 /*
  125  *      Associated with page of user-allocatable memory is a
  126  *      page structure.
  127  */
  128 
  129 struct vm_domain vm_dom[MAXMEMDOM];
  130 struct mtx_padalign vm_page_queue_free_mtx;
  131 
  132 struct mtx_padalign pa_lock[PA_LOCK_COUNT];
  133 
  134 vm_page_t vm_page_array;
  135 long vm_page_array_size;
  136 long first_page;
  137 int vm_page_zero_count;
  138 
  139 static int boot_pages = UMA_BOOT_PAGES;
  140 SYSCTL_INT(_vm, OID_AUTO, boot_pages, CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
  141     &boot_pages, 0,
  142     "number of pages allocated for bootstrapping the VM system");
  143 
  144 static int pa_tryrelock_restart;
  145 SYSCTL_INT(_vm, OID_AUTO, tryrelock_restart, CTLFLAG_RD,
  146     &pa_tryrelock_restart, 0, "Number of tryrelock restarts");
  147 
  148 static TAILQ_HEAD(, vm_page) blacklist_head;
  149 static int sysctl_vm_page_blacklist(SYSCTL_HANDLER_ARGS);
  150 SYSCTL_PROC(_vm, OID_AUTO, page_blacklist, CTLTYPE_STRING | CTLFLAG_RD |
  151     CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_page_blacklist, "A", "Blacklist pages");
  152 
  153 /* Is the page daemon waiting for free pages? */
  154 static int vm_pageout_pages_needed;
  155 
  156 static uma_zone_t fakepg_zone;
  157 
  158 static void vm_page_alloc_check(vm_page_t m);
  159 static void vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits);
  160 static void vm_page_enqueue(uint8_t queue, vm_page_t m);
  161 static void vm_page_free_wakeup(void);
  162 static void vm_page_init_fakepg(void *dummy);
  163 static int vm_page_insert_after(vm_page_t m, vm_object_t object,
  164     vm_pindex_t pindex, vm_page_t mpred);
  165 static void vm_page_insert_radixdone(vm_page_t m, vm_object_t object,
  166     vm_page_t mpred);
  167 static int vm_page_reclaim_run(int req_class, u_long npages, vm_page_t m_run,
  168     vm_paddr_t high);
  169 
  170 SYSINIT(vm_page, SI_SUB_VM, SI_ORDER_SECOND, vm_page_init_fakepg, NULL);
  171 
  172 static void
  173 vm_page_init_fakepg(void *dummy)
  174 {
  175 
  176         fakepg_zone = uma_zcreate("fakepg", sizeof(struct vm_page), NULL, NULL,
  177             NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE | UMA_ZONE_VM);
  178 }
  179 
  180 /* Make sure that u_long is at least 64 bits when PAGE_SIZE is 32K. */
  181 #if PAGE_SIZE == 32768
  182 #ifdef CTASSERT
  183 CTASSERT(sizeof(u_long) >= 8);
  184 #endif
  185 #endif
  186 
  187 /*
  188  * Try to acquire a physical address lock while a pmap is locked.  If we
  189  * fail to trylock we unlock and lock the pmap directly and cache the
  190  * locked pa in *locked.  The caller should then restart their loop in case
  191  * the virtual to physical mapping has changed.
  192  */
  193 int
  194 vm_page_pa_tryrelock(pmap_t pmap, vm_paddr_t pa, vm_paddr_t *locked)
  195 {
  196         vm_paddr_t lockpa;
  197 
  198         lockpa = *locked;
  199         *locked = pa;
  200         if (lockpa) {
  201                 PA_LOCK_ASSERT(lockpa, MA_OWNED);
  202                 if (PA_LOCKPTR(pa) == PA_LOCKPTR(lockpa))
  203                         return (0);
  204                 PA_UNLOCK(lockpa);
  205         }
  206         if (PA_TRYLOCK(pa))
  207                 return (0);
  208         PMAP_UNLOCK(pmap);
  209         atomic_add_int(&pa_tryrelock_restart, 1);
  210         PA_LOCK(pa);
  211         PMAP_LOCK(pmap);
  212         return (EAGAIN);
  213 }
  214 
  215 /*
  216  *      vm_set_page_size:
  217  *
  218  *      Sets the page size, perhaps based upon the memory
  219  *      size.  Must be called before any use of page-size
  220  *      dependent functions.
  221  */
  222 void
  223 vm_set_page_size(void)
  224 {
  225         if (vm_cnt.v_page_size == 0)
  226                 vm_cnt.v_page_size = PAGE_SIZE;
  227         if (((vm_cnt.v_page_size - 1) & vm_cnt.v_page_size) != 0)
  228                 panic("vm_set_page_size: page size not a power of two");
  229 }
  230 
  231 /*
  232  *      vm_page_blacklist_next:
  233  *
  234  *      Find the next entry in the provided string of blacklist
  235  *      addresses.  Entries are separated by space, comma, or newline.
  236  *      If an invalid integer is encountered then the rest of the
  237  *      string is skipped.  Updates the list pointer to the next
  238  *      character, or NULL if the string is exhausted or invalid.
  239  */
  240 static vm_paddr_t
  241 vm_page_blacklist_next(char **list, char *end)
  242 {
  243         vm_paddr_t bad;
  244         char *cp, *pos;
  245 
  246         if (list == NULL || *list == NULL)
  247                 return (0);
  248         if (**list =='\0') {
  249                 *list = NULL;
  250                 return (0);
  251         }
  252 
  253         /*
  254          * If there's no end pointer then the buffer is coming from
  255          * the kenv and we know it's null-terminated.
  256          */
  257         if (end == NULL)
  258                 end = *list + strlen(*list);
  259 
  260         /* Ensure that strtoq() won't walk off the end */
  261         if (*end != '\0') {
  262                 if (*end == '\n' || *end == ' ' || *end  == ',')
  263                         *end = '\0';
  264                 else {
  265                         printf("Blacklist not terminated, skipping\n");
  266                         *list = NULL;
  267                         return (0);
  268                 }
  269         }
  270 
  271         for (pos = *list; *pos != '\0'; pos = cp) {
  272                 bad = strtoq(pos, &cp, 0);
  273                 if (*cp == '\0' || *cp == ' ' || *cp == ',' || *cp == '\n') {
  274                         if (bad == 0) {
  275                                 if (++cp < end)
  276                                         continue;
  277                                 else
  278                                         break;
  279                         }
  280                 } else
  281                         break;
  282                 if (*cp == '\0' || ++cp >= end)
  283                         *list = NULL;
  284                 else
  285                         *list = cp;
  286                 return (trunc_page(bad));
  287         }
  288         printf("Garbage in RAM blacklist, skipping\n");
  289         *list = NULL;
  290         return (0);
  291 }
  292 
  293 bool
  294 vm_page_blacklist_add(vm_paddr_t pa, bool verbose)
  295 {
  296         vm_page_t m;
  297         int ret;
  298 
  299         m = vm_phys_paddr_to_vm_page(pa);
  300         if (m == NULL)
  301                 return (true); /* page does not exist, no failure */
  302 
  303         mtx_lock(&vm_page_queue_free_mtx);
  304         ret = vm_phys_unfree_page(m);
  305         mtx_unlock(&vm_page_queue_free_mtx);
  306         if (ret) {
  307                 TAILQ_INSERT_TAIL(&blacklist_head, m, listq);
  308                 if (verbose)
  309                         printf("Skipping page with pa 0x%jx\n", (uintmax_t)pa);
  310         }
  311         return (ret);
  312 }
  313 
  314 /*
  315  *      vm_page_blacklist_check:
  316  *
  317  *      Iterate through the provided string of blacklist addresses, pulling
  318  *      each entry out of the physical allocator free list and putting it
  319  *      onto a list for reporting via the vm.page_blacklist sysctl.
  320  */
  321 static void
  322 vm_page_blacklist_check(char *list, char *end)
  323 {
  324         vm_paddr_t pa;
  325         char *next;
  326 
  327         next = list;
  328         while (next != NULL) {
  329                 if ((pa = vm_page_blacklist_next(&next, end)) == 0)
  330                         continue;
  331                 vm_page_blacklist_add(pa, bootverbose);
  332         }
  333 }
  334 
  335 /*
  336  *      vm_page_blacklist_load:
  337  *
  338  *      Search for a special module named "ram_blacklist".  It'll be a
  339  *      plain text file provided by the user via the loader directive
  340  *      of the same name.
  341  */
  342 static void
  343 vm_page_blacklist_load(char **list, char **end)
  344 {
  345         void *mod;
  346         u_char *ptr;
  347         u_int len;
  348 
  349         mod = NULL;
  350         ptr = NULL;
  351 
  352         mod = preload_search_by_type("ram_blacklist");
  353         if (mod != NULL) {
  354                 ptr = preload_fetch_addr(mod);
  355                 len = preload_fetch_size(mod);
  356         }
  357         *list = ptr;
  358         if (ptr != NULL)
  359                 *end = ptr + len;
  360         else
  361                 *end = NULL;
  362         return;
  363 }
  364 
  365 static int
  366 sysctl_vm_page_blacklist(SYSCTL_HANDLER_ARGS)
  367 {
  368         vm_page_t m;
  369         struct sbuf sbuf;
  370         int error, first;
  371 
  372         first = 1;
  373         error = sysctl_wire_old_buffer(req, 0);
  374         if (error != 0)
  375                 return (error);
  376         sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
  377         TAILQ_FOREACH(m, &blacklist_head, listq) {
  378                 sbuf_printf(&sbuf, "%s%#jx", first ? "" : ",",
  379                     (uintmax_t)m->phys_addr);
  380                 first = 0;
  381         }
  382         error = sbuf_finish(&sbuf);
  383         sbuf_delete(&sbuf);
  384         return (error);
  385 }
  386 
  387 static void
  388 vm_page_domain_init(struct vm_domain *vmd)
  389 {
  390         struct vm_pagequeue *pq;
  391         int i;
  392 
  393         *__DECONST(char **, &vmd->vmd_pagequeues[PQ_INACTIVE].pq_name) =
  394             "vm inactive pagequeue";
  395         *__DECONST(u_int **, &vmd->vmd_pagequeues[PQ_INACTIVE].pq_vcnt) =
  396             &vm_cnt.v_inactive_count;
  397         *__DECONST(char **, &vmd->vmd_pagequeues[PQ_ACTIVE].pq_name) =
  398             "vm active pagequeue";
  399         *__DECONST(u_int **, &vmd->vmd_pagequeues[PQ_ACTIVE].pq_vcnt) =
  400             &vm_cnt.v_active_count;
  401         *__DECONST(char **, &vmd->vmd_pagequeues[PQ_LAUNDRY].pq_name) =
  402             "vm laundry pagequeue";
  403         *__DECONST(int **, &vmd->vmd_pagequeues[PQ_LAUNDRY].pq_vcnt) =
  404             &vm_cnt.v_laundry_count;
  405         vmd->vmd_page_count = 0;
  406         vmd->vmd_free_count = 0;
  407         vmd->vmd_segs = 0;
  408         vmd->vmd_oom = FALSE;
  409         for (i = 0; i < PQ_COUNT; i++) {
  410                 pq = &vmd->vmd_pagequeues[i];
  411                 TAILQ_INIT(&pq->pq_pl);
  412                 mtx_init(&pq->pq_mutex, pq->pq_name, "vm pagequeue",
  413                     MTX_DEF | MTX_DUPOK);
  414         }
  415 }
  416 
  417 /*
  418  *      vm_page_startup:
  419  *
  420  *      Initializes the resident memory module.  Allocates physical memory for
  421  *      bootstrapping UMA and some data structures that are used to manage
  422  *      physical pages.  Initializes these structures, and populates the free
  423  *      page queues.
  424  */
  425 vm_offset_t
  426 vm_page_startup(vm_offset_t vaddr)
  427 {
  428         vm_offset_t mapped;
  429         vm_paddr_t high_avail, low_avail, page_range, size;
  430         vm_paddr_t new_end;
  431         int i;
  432         vm_paddr_t pa;
  433         vm_paddr_t last_pa;
  434         char *list, *listend;
  435         vm_paddr_t end;
  436         vm_paddr_t biggestsize;
  437         int biggestone;
  438         int pages_per_zone;
  439 
  440         biggestsize = 0;
  441         biggestone = 0;
  442         vaddr = round_page(vaddr);
  443 
  444         for (i = 0; phys_avail[i + 1]; i += 2) {
  445                 phys_avail[i] = round_page(phys_avail[i]);
  446                 phys_avail[i + 1] = trunc_page(phys_avail[i + 1]);
  447         }
  448         for (i = 0; phys_avail[i + 1]; i += 2) {
  449                 size = phys_avail[i + 1] - phys_avail[i];
  450                 if (size > biggestsize) {
  451                         biggestone = i;
  452                         biggestsize = size;
  453                 }
  454         }
  455 
  456         end = phys_avail[biggestone+1];
  457 
  458         /*
  459          * Initialize the page and queue locks.
  460          */
  461         mtx_init(&vm_page_queue_free_mtx, "vm page free queue", NULL, MTX_DEF);
  462         for (i = 0; i < PA_LOCK_COUNT; i++)
  463                 mtx_init(&pa_lock[i], "vm page", NULL, MTX_DEF);
  464         for (i = 0; i < vm_ndomains; i++)
  465                 vm_page_domain_init(&vm_dom[i]);
  466 
  467         /*
  468          * Almost all of the pages needed for bootstrapping UMA are used
  469          * for zone structures, so if the number of CPUs results in those
  470          * structures taking more than one page each, we set aside more pages
  471          * in proportion to the zone structure size.
  472          */
  473         pages_per_zone = howmany(sizeof(struct uma_zone) +
  474             sizeof(struct uma_cache) * (mp_maxid + 1), UMA_SLAB_SIZE);
  475         if (pages_per_zone > 1) {
  476                 /* Reserve more pages so that we don't run out. */
  477                 boot_pages = UMA_BOOT_PAGES_ZONES * pages_per_zone;
  478         }
  479 
  480         /*
  481          * Allocate memory for use when boot strapping the kernel memory
  482          * allocator.
  483          *
  484          * CTFLAG_RDTUN doesn't work during the early boot process, so we must
  485          * manually fetch the value.
  486          */
  487         TUNABLE_INT_FETCH("vm.boot_pages", &boot_pages);
  488         new_end = end - (boot_pages * UMA_SLAB_SIZE);
  489         new_end = trunc_page(new_end);
  490         mapped = pmap_map(&vaddr, new_end, end,
  491             VM_PROT_READ | VM_PROT_WRITE);
  492         bzero((void *)mapped, end - new_end);
  493         uma_startup((void *)mapped, boot_pages);
  494 
  495 #if defined(__aarch64__) || defined(__amd64__) || defined(__arm__) || \
  496     defined(__i386__) || defined(__mips__)
  497         /*
  498          * Allocate a bitmap to indicate that a random physical page
  499          * needs to be included in a minidump.
  500          *
  501          * The amd64 port needs this to indicate which direct map pages
  502          * need to be dumped, via calls to dump_add_page()/dump_drop_page().
  503          *
  504          * However, i386 still needs this workspace internally within the
  505          * minidump code.  In theory, they are not needed on i386, but are
  506          * included should the sf_buf code decide to use them.
  507          */
  508         last_pa = 0;
  509         for (i = 0; dump_avail[i + 1] != 0; i += 2)
  510                 if (dump_avail[i + 1] > last_pa)
  511                         last_pa = dump_avail[i + 1];
  512         page_range = last_pa / PAGE_SIZE;
  513         vm_page_dump_size = round_page(roundup2(page_range, NBBY) / NBBY);
  514         new_end -= vm_page_dump_size;
  515         vm_page_dump = (void *)(uintptr_t)pmap_map(&vaddr, new_end,
  516             new_end + vm_page_dump_size, VM_PROT_READ | VM_PROT_WRITE);
  517         bzero((void *)vm_page_dump, vm_page_dump_size);
  518 #endif
  519 #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__)
  520         /*
  521          * Include the UMA bootstrap pages and vm_page_dump in a crash dump.
  522          * When pmap_map() uses the direct map, they are not automatically 
  523          * included.
  524          */
  525         for (pa = new_end; pa < end; pa += PAGE_SIZE)
  526                 dump_add_page(pa);
  527 #endif
  528         phys_avail[biggestone + 1] = new_end;
  529 #ifdef __amd64__
  530         /*
  531          * Request that the physical pages underlying the message buffer be
  532          * included in a crash dump.  Since the message buffer is accessed
  533          * through the direct map, they are not automatically included.
  534          */
  535         pa = DMAP_TO_PHYS((vm_offset_t)msgbufp->msg_ptr);
  536         last_pa = pa + round_page(msgbufsize);
  537         while (pa < last_pa) {
  538                 dump_add_page(pa);
  539                 pa += PAGE_SIZE;
  540         }
  541 #endif
  542         /*
  543          * Compute the number of pages of memory that will be available for
  544          * use, taking into account the overhead of a page structure per page.
  545          * In other words, solve
  546          *      "available physical memory" - round_page(page_range *
  547          *          sizeof(struct vm_page)) = page_range * PAGE_SIZE 
  548          * for page_range.  
  549          */
  550         low_avail = phys_avail[0];
  551         high_avail = phys_avail[1];
  552         for (i = 0; i < vm_phys_nsegs; i++) {
  553                 if (vm_phys_segs[i].start < low_avail)
  554                         low_avail = vm_phys_segs[i].start;
  555                 if (vm_phys_segs[i].end > high_avail)
  556                         high_avail = vm_phys_segs[i].end;
  557         }
  558         /* Skip the first chunk.  It is already accounted for. */
  559         for (i = 2; phys_avail[i + 1] != 0; i += 2) {
  560                 if (phys_avail[i] < low_avail)
  561                         low_avail = phys_avail[i];
  562                 if (phys_avail[i + 1] > high_avail)
  563                         high_avail = phys_avail[i + 1];
  564         }
  565         first_page = low_avail / PAGE_SIZE;
  566 #ifdef VM_PHYSSEG_SPARSE
  567         size = 0;
  568         for (i = 0; i < vm_phys_nsegs; i++)
  569                 size += vm_phys_segs[i].end - vm_phys_segs[i].start;
  570         for (i = 0; phys_avail[i + 1] != 0; i += 2)
  571                 size += phys_avail[i + 1] - phys_avail[i];
  572 #elif defined(VM_PHYSSEG_DENSE)
  573         size = high_avail - low_avail;
  574 #else
  575 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
  576 #endif
  577 
  578 #ifdef VM_PHYSSEG_DENSE
  579         /*
  580          * In the VM_PHYSSEG_DENSE case, the number of pages can account for
  581          * the overhead of a page structure per page only if vm_page_array is
  582          * allocated from the last physical memory chunk.  Otherwise, we must
  583          * allocate page structures representing the physical memory
  584          * underlying vm_page_array, even though they will not be used.
  585          */
  586         if (new_end != high_avail)
  587                 page_range = size / PAGE_SIZE;
  588         else
  589 #endif
  590         {
  591                 page_range = size / (PAGE_SIZE + sizeof(struct vm_page));
  592 
  593                 /*
  594                  * If the partial bytes remaining are large enough for
  595                  * a page (PAGE_SIZE) without a corresponding
  596                  * 'struct vm_page', then new_end will contain an
  597                  * extra page after subtracting the length of the VM
  598                  * page array.  Compensate by subtracting an extra
  599                  * page from new_end.
  600                  */
  601                 if (size % (PAGE_SIZE + sizeof(struct vm_page)) >= PAGE_SIZE) {
  602                         if (new_end == high_avail)
  603                                 high_avail -= PAGE_SIZE;
  604                         new_end -= PAGE_SIZE;
  605                 }
  606         }
  607         end = new_end;
  608 
  609         /*
  610          * Reserve an unmapped guard page to trap access to vm_page_array[-1].
  611          * However, because this page is allocated from KVM, out-of-bounds
  612          * accesses using the direct map will not be trapped.
  613          */
  614         vaddr += PAGE_SIZE;
  615 
  616         /*
  617          * Allocate physical memory for the page structures, and map it.
  618          */
  619         new_end = trunc_page(end - page_range * sizeof(struct vm_page));
  620         mapped = pmap_map(&vaddr, new_end, end,
  621             VM_PROT_READ | VM_PROT_WRITE);
  622         vm_page_array = (vm_page_t) mapped;
  623 #if VM_NRESERVLEVEL > 0
  624         /*
  625          * Allocate physical memory for the reservation management system's
  626          * data structures, and map it.
  627          */
  628         if (high_avail == end)
  629                 high_avail = new_end;
  630         new_end = vm_reserv_startup(&vaddr, new_end, high_avail);
  631 #endif
  632 #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__)
  633         /*
  634          * Include vm_page_array and vm_reserv_array in a crash dump.
  635          */
  636         for (pa = new_end; pa < end; pa += PAGE_SIZE)
  637                 dump_add_page(pa);
  638 #endif
  639         phys_avail[biggestone + 1] = new_end;
  640 
  641         /*
  642          * Add physical memory segments corresponding to the available
  643          * physical pages.
  644          */
  645         for (i = 0; phys_avail[i + 1] != 0; i += 2)
  646                 vm_phys_add_seg(phys_avail[i], phys_avail[i + 1]);
  647 
  648         /*
  649          * Clear all of the page structures
  650          */
  651         bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page));
  652         for (i = 0; i < page_range; i++)
  653                 vm_page_array[i].order = VM_NFREEORDER;
  654         vm_page_array_size = page_range;
  655 
  656         /*
  657          * Initialize the physical memory allocator.
  658          */
  659         vm_phys_init();
  660 
  661         /*
  662          * Add every available physical page that is not blacklisted to
  663          * the free lists.
  664          */
  665         vm_cnt.v_page_count = 0;
  666         vm_cnt.v_free_count = 0;
  667         for (i = 0; phys_avail[i + 1] != 0; i += 2) {
  668                 pa = phys_avail[i];
  669                 last_pa = phys_avail[i + 1];
  670                 while (pa < last_pa) {
  671                         vm_phys_add_page(pa);
  672                         pa += PAGE_SIZE;
  673                 }
  674         }
  675 
  676         TAILQ_INIT(&blacklist_head);
  677         vm_page_blacklist_load(&list, &listend);
  678         vm_page_blacklist_check(list, listend);
  679 
  680         list = kern_getenv("vm.blacklist");
  681         vm_page_blacklist_check(list, NULL);
  682 
  683         freeenv(list);
  684 #if VM_NRESERVLEVEL > 0
  685         /*
  686          * Initialize the reservation management system.
  687          */
  688         vm_reserv_init();
  689 #endif
  690         return (vaddr);
  691 }
  692 
  693 void
  694 vm_page_reference(vm_page_t m)
  695 {
  696 
  697         vm_page_aflag_set(m, PGA_REFERENCED);
  698 }
  699 
  700 /*
  701  *      vm_page_busy_downgrade:
  702  *
  703  *      Downgrade an exclusive busy page into a single shared busy page.
  704  */
  705 void
  706 vm_page_busy_downgrade(vm_page_t m)
  707 {
  708         u_int x;
  709         bool locked;
  710 
  711         vm_page_assert_xbusied(m);
  712         locked = mtx_owned(vm_page_lockptr(m));
  713 
  714         for (;;) {
  715                 x = m->busy_lock;
  716                 x &= VPB_BIT_WAITERS;
  717                 if (x != 0 && !locked)
  718                         vm_page_lock(m);
  719                 if (atomic_cmpset_rel_int(&m->busy_lock,
  720                     VPB_SINGLE_EXCLUSIVER | x, VPB_SHARERS_WORD(1)))
  721                         break;
  722                 if (x != 0 && !locked)
  723                         vm_page_unlock(m);
  724         }
  725         if (x != 0) {
  726                 wakeup(m);
  727                 if (!locked)
  728                         vm_page_unlock(m);
  729         }
  730 }
  731 
  732 /*
  733  *      vm_page_sbusied:
  734  *
  735  *      Return a positive value if the page is shared busied, 0 otherwise.
  736  */
  737 int
  738 vm_page_sbusied(vm_page_t m)
  739 {
  740         u_int x;
  741 
  742         x = m->busy_lock;
  743         return ((x & VPB_BIT_SHARED) != 0 && x != VPB_UNBUSIED);
  744 }
  745 
  746 /*
  747  *      vm_page_sunbusy:
  748  *
  749  *      Shared unbusy a page.
  750  */
  751 void
  752 vm_page_sunbusy(vm_page_t m)
  753 {
  754         u_int x;
  755 
  756         vm_page_assert_sbusied(m);
  757 
  758         for (;;) {
  759                 x = m->busy_lock;
  760                 if (VPB_SHARERS(x) > 1) {
  761                         if (atomic_cmpset_int(&m->busy_lock, x,
  762                             x - VPB_ONE_SHARER))
  763                                 break;
  764                         continue;
  765                 }
  766                 if ((x & VPB_BIT_WAITERS) == 0) {
  767                         KASSERT(x == VPB_SHARERS_WORD(1),
  768                             ("vm_page_sunbusy: invalid lock state"));
  769                         if (atomic_cmpset_int(&m->busy_lock,
  770                             VPB_SHARERS_WORD(1), VPB_UNBUSIED))
  771                                 break;
  772                         continue;
  773                 }
  774                 KASSERT(x == (VPB_SHARERS_WORD(1) | VPB_BIT_WAITERS),
  775                     ("vm_page_sunbusy: invalid lock state for waiters"));
  776 
  777                 vm_page_lock(m);
  778                 if (!atomic_cmpset_int(&m->busy_lock, x, VPB_UNBUSIED)) {
  779                         vm_page_unlock(m);
  780                         continue;
  781                 }
  782                 wakeup(m);
  783                 vm_page_unlock(m);
  784                 break;
  785         }
  786 }
  787 
  788 /*
  789  *      vm_page_busy_sleep:
  790  *
  791  *      Sleep and release the page lock, using the page pointer as wchan.
  792  *      This is used to implement the hard-path of busying mechanism.
  793  *
  794  *      The given page must be locked.
  795  *
  796  *      If nonshared is true, sleep only if the page is xbusy.
  797  */
  798 void
  799 vm_page_busy_sleep(vm_page_t m, const char *wmesg, bool nonshared)
  800 {
  801         u_int x;
  802 
  803         vm_page_assert_locked(m);
  804 
  805         x = m->busy_lock;
  806         if (x == VPB_UNBUSIED || (nonshared && (x & VPB_BIT_SHARED) != 0) ||
  807             ((x & VPB_BIT_WAITERS) == 0 &&
  808             !atomic_cmpset_int(&m->busy_lock, x, x | VPB_BIT_WAITERS))) {
  809                 vm_page_unlock(m);
  810                 return;
  811         }
  812         msleep(m, vm_page_lockptr(m), PVM | PDROP, wmesg, 0);
  813 }
  814 
  815 /*
  816  *      vm_page_trysbusy:
  817  *
  818  *      Try to shared busy a page.
  819  *      If the operation succeeds 1 is returned otherwise 0.
  820  *      The operation never sleeps.
  821  */
  822 int
  823 vm_page_trysbusy(vm_page_t m)
  824 {
  825         u_int x;
  826 
  827         for (;;) {
  828                 x = m->busy_lock;
  829                 if ((x & VPB_BIT_SHARED) == 0)
  830                         return (0);
  831                 if (atomic_cmpset_acq_int(&m->busy_lock, x, x + VPB_ONE_SHARER))
  832                         return (1);
  833         }
  834 }
  835 
  836 static void
  837 vm_page_xunbusy_locked(vm_page_t m)
  838 {
  839 
  840         vm_page_assert_xbusied(m);
  841         vm_page_assert_locked(m);
  842 
  843         atomic_store_rel_int(&m->busy_lock, VPB_UNBUSIED);
  844         /* There is a waiter, do wakeup() instead of vm_page_flash(). */
  845         wakeup(m);
  846 }
  847 
  848 void
  849 vm_page_xunbusy_maybelocked(vm_page_t m)
  850 {
  851         bool lockacq;
  852 
  853         vm_page_assert_xbusied(m);
  854 
  855         /*
  856          * Fast path for unbusy.  If it succeeds, we know that there
  857          * are no waiters, so we do not need a wakeup.
  858          */
  859         if (atomic_cmpset_rel_int(&m->busy_lock, VPB_SINGLE_EXCLUSIVER,
  860             VPB_UNBUSIED))
  861                 return;
  862 
  863         lockacq = !mtx_owned(vm_page_lockptr(m));
  864         if (lockacq)
  865                 vm_page_lock(m);
  866         vm_page_xunbusy_locked(m);
  867         if (lockacq)
  868                 vm_page_unlock(m);
  869 }
  870 
  871 /*
  872  *      vm_page_xunbusy_hard:
  873  *
  874  *      Called after the first try the exclusive unbusy of a page failed.
  875  *      It is assumed that the waiters bit is on.
  876  */
  877 void
  878 vm_page_xunbusy_hard(vm_page_t m)
  879 {
  880 
  881         vm_page_assert_xbusied(m);
  882 
  883         vm_page_lock(m);
  884         vm_page_xunbusy_locked(m);
  885         vm_page_unlock(m);
  886 }
  887 
  888 /*
  889  *      vm_page_flash:
  890  *
  891  *      Wakeup anyone waiting for the page.
  892  *      The ownership bits do not change.
  893  *
  894  *      The given page must be locked.
  895  */
  896 void
  897 vm_page_flash(vm_page_t m)
  898 {
  899         u_int x;
  900 
  901         vm_page_lock_assert(m, MA_OWNED);
  902 
  903         for (;;) {
  904                 x = m->busy_lock;
  905                 if ((x & VPB_BIT_WAITERS) == 0)
  906                         return;
  907                 if (atomic_cmpset_int(&m->busy_lock, x,
  908                     x & (~VPB_BIT_WAITERS)))
  909                         break;
  910         }
  911         wakeup(m);
  912 }
  913 
  914 /*
  915  * Keep page from being freed by the page daemon
  916  * much of the same effect as wiring, except much lower
  917  * overhead and should be used only for *very* temporary
  918  * holding ("wiring").
  919  */
  920 void
  921 vm_page_hold(vm_page_t mem)
  922 {
  923 
  924         vm_page_lock_assert(mem, MA_OWNED);
  925         mem->hold_count++;
  926 }
  927 
  928 void
  929 vm_page_unhold(vm_page_t mem)
  930 {
  931 
  932         vm_page_lock_assert(mem, MA_OWNED);
  933         KASSERT(mem->hold_count >= 1, ("vm_page_unhold: hold count < 0!!!"));
  934         --mem->hold_count;
  935         if (mem->hold_count == 0 && (mem->flags & PG_UNHOLDFREE) != 0)
  936                 vm_page_free_toq(mem);
  937 }
  938 
  939 /*
  940  *      vm_page_unhold_pages:
  941  *
  942  *      Unhold each of the pages that is referenced by the given array.
  943  */
  944 void
  945 vm_page_unhold_pages(vm_page_t *ma, int count)
  946 {
  947         struct mtx *mtx, *new_mtx;
  948 
  949         mtx = NULL;
  950         for (; count != 0; count--) {
  951                 /*
  952                  * Avoid releasing and reacquiring the same page lock.
  953                  */
  954                 new_mtx = vm_page_lockptr(*ma);
  955                 if (mtx != new_mtx) {
  956                         if (mtx != NULL)
  957                                 mtx_unlock(mtx);
  958                         mtx = new_mtx;
  959                         mtx_lock(mtx);
  960                 }
  961                 vm_page_unhold(*ma);
  962                 ma++;
  963         }
  964         if (mtx != NULL)
  965                 mtx_unlock(mtx);
  966 }
  967 
  968 vm_page_t
  969 PHYS_TO_VM_PAGE(vm_paddr_t pa)
  970 {
  971         vm_page_t m;
  972 
  973 #ifdef VM_PHYSSEG_SPARSE
  974         m = vm_phys_paddr_to_vm_page(pa);
  975         if (m == NULL)
  976                 m = vm_phys_fictitious_to_vm_page(pa);
  977         return (m);
  978 #elif defined(VM_PHYSSEG_DENSE)
  979         long pi;
  980 
  981         pi = atop(pa);
  982         if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
  983                 m = &vm_page_array[pi - first_page];
  984                 return (m);
  985         }
  986         return (vm_phys_fictitious_to_vm_page(pa));
  987 #else
  988 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
  989 #endif
  990 }
  991 
  992 /*
  993  *      vm_page_getfake:
  994  *
  995  *      Create a fictitious page with the specified physical address and
  996  *      memory attribute.  The memory attribute is the only the machine-
  997  *      dependent aspect of a fictitious page that must be initialized.
  998  */
  999 vm_page_t
 1000 vm_page_getfake(vm_paddr_t paddr, vm_memattr_t memattr)
 1001 {
 1002         vm_page_t m;
 1003 
 1004         m = uma_zalloc(fakepg_zone, M_WAITOK | M_ZERO);
 1005         vm_page_initfake(m, paddr, memattr);
 1006         return (m);
 1007 }
 1008 
 1009 void
 1010 vm_page_initfake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
 1011 {
 1012 
 1013         if ((m->flags & PG_FICTITIOUS) != 0) {
 1014                 /*
 1015                  * The page's memattr might have changed since the
 1016                  * previous initialization.  Update the pmap to the
 1017                  * new memattr.
 1018                  */
 1019                 goto memattr;
 1020         }
 1021         m->phys_addr = paddr;
 1022         m->queue = PQ_NONE;
 1023         /* Fictitious pages don't use "segind". */
 1024         m->flags = PG_FICTITIOUS;
 1025         /* Fictitious pages don't use "order" or "pool". */
 1026         m->oflags = VPO_UNMANAGED;
 1027         m->busy_lock = VPB_SINGLE_EXCLUSIVER;
 1028         m->wire_count = 1;
 1029         pmap_page_init(m);
 1030 memattr:
 1031         pmap_page_set_memattr(m, memattr);
 1032 }
 1033 
 1034 /*
 1035  *      vm_page_putfake:
 1036  *
 1037  *      Release a fictitious page.
 1038  */
 1039 void
 1040 vm_page_putfake(vm_page_t m)
 1041 {
 1042 
 1043         KASSERT((m->oflags & VPO_UNMANAGED) != 0, ("managed %p", m));
 1044         KASSERT((m->flags & PG_FICTITIOUS) != 0,
 1045             ("vm_page_putfake: bad page %p", m));
 1046         uma_zfree(fakepg_zone, m);
 1047 }
 1048 
 1049 /*
 1050  *      vm_page_updatefake:
 1051  *
 1052  *      Update the given fictitious page to the specified physical address and
 1053  *      memory attribute.
 1054  */
 1055 void
 1056 vm_page_updatefake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
 1057 {
 1058 
 1059         KASSERT((m->flags & PG_FICTITIOUS) != 0,
 1060             ("vm_page_updatefake: bad page %p", m));
 1061         m->phys_addr = paddr;
 1062         pmap_page_set_memattr(m, memattr);
 1063 }
 1064 
 1065 /*
 1066  *      vm_page_free:
 1067  *
 1068  *      Free a page.
 1069  */
 1070 void
 1071 vm_page_free(vm_page_t m)
 1072 {
 1073 
 1074         m->flags &= ~PG_ZERO;
 1075         vm_page_free_toq(m);
 1076 }
 1077 
 1078 /*
 1079  *      vm_page_free_zero:
 1080  *
 1081  *      Free a page to the zerod-pages queue
 1082  */
 1083 void
 1084 vm_page_free_zero(vm_page_t m)
 1085 {
 1086 
 1087         m->flags |= PG_ZERO;
 1088         vm_page_free_toq(m);
 1089 }
 1090 
 1091 /*
 1092  * Unbusy and handle the page queueing for a page from a getpages request that
 1093  * was optionally read ahead or behind.
 1094  */
 1095 void
 1096 vm_page_readahead_finish(vm_page_t m)
 1097 {
 1098 
 1099         /* We shouldn't put invalid pages on queues. */
 1100         KASSERT(m->valid != 0, ("%s: %p is invalid", __func__, m));
 1101 
 1102         /*
 1103          * Since the page is not the actually needed one, whether it should
 1104          * be activated or deactivated is not obvious.  Empirical results
 1105          * have shown that deactivating the page is usually the best choice,
 1106          * unless the page is wanted by another thread.
 1107          */
 1108         vm_page_lock(m);
 1109         if ((m->busy_lock & VPB_BIT_WAITERS) != 0)
 1110                 vm_page_activate(m);
 1111         else
 1112                 vm_page_deactivate(m);
 1113         vm_page_unlock(m);
 1114         vm_page_xunbusy(m);
 1115 }
 1116 
 1117 /*
 1118  *      vm_page_sleep_if_busy:
 1119  *
 1120  *      Sleep and release the page queues lock if the page is busied.
 1121  *      Returns TRUE if the thread slept.
 1122  *
 1123  *      The given page must be unlocked and object containing it must
 1124  *      be locked.
 1125  */
 1126 int
 1127 vm_page_sleep_if_busy(vm_page_t m, const char *msg)
 1128 {
 1129         vm_object_t obj;
 1130 
 1131         vm_page_lock_assert(m, MA_NOTOWNED);
 1132         VM_OBJECT_ASSERT_WLOCKED(m->object);
 1133 
 1134         if (vm_page_busied(m)) {
 1135                 /*
 1136                  * The page-specific object must be cached because page
 1137                  * identity can change during the sleep, causing the
 1138                  * re-lock of a different object.
 1139                  * It is assumed that a reference to the object is already
 1140                  * held by the callers.
 1141                  */
 1142                 obj = m->object;
 1143                 vm_page_lock(m);
 1144                 VM_OBJECT_WUNLOCK(obj);
 1145                 vm_page_busy_sleep(m, msg, false);
 1146                 VM_OBJECT_WLOCK(obj);
 1147                 return (TRUE);
 1148         }
 1149         return (FALSE);
 1150 }
 1151 
 1152 /*
 1153  *      vm_page_dirty_KBI:              [ internal use only ]
 1154  *
 1155  *      Set all bits in the page's dirty field.
 1156  *
 1157  *      The object containing the specified page must be locked if the
 1158  *      call is made from the machine-independent layer.
 1159  *
 1160  *      See vm_page_clear_dirty_mask().
 1161  *
 1162  *      This function should only be called by vm_page_dirty().
 1163  */
 1164 void
 1165 vm_page_dirty_KBI(vm_page_t m)
 1166 {
 1167 
 1168         /* Refer to this operation by its public name. */
 1169         KASSERT(m->valid == VM_PAGE_BITS_ALL,
 1170             ("vm_page_dirty: page is invalid!"));
 1171         m->dirty = VM_PAGE_BITS_ALL;
 1172 }
 1173 
 1174 /*
 1175  *      vm_page_insert:         [ internal use only ]
 1176  *
 1177  *      Inserts the given mem entry into the object and object list.
 1178  *
 1179  *      The object must be locked.
 1180  */
 1181 int
 1182 vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex)
 1183 {
 1184         vm_page_t mpred;
 1185 
 1186         VM_OBJECT_ASSERT_WLOCKED(object);
 1187         mpred = vm_radix_lookup_le(&object->rtree, pindex);
 1188         return (vm_page_insert_after(m, object, pindex, mpred));
 1189 }
 1190 
 1191 /*
 1192  *      vm_page_insert_after:
 1193  *
 1194  *      Inserts the page "m" into the specified object at offset "pindex".
 1195  *
 1196  *      The page "mpred" must immediately precede the offset "pindex" within
 1197  *      the specified object.
 1198  *
 1199  *      The object must be locked.
 1200  */
 1201 static int
 1202 vm_page_insert_after(vm_page_t m, vm_object_t object, vm_pindex_t pindex,
 1203     vm_page_t mpred)
 1204 {
 1205         vm_page_t msucc;
 1206 
 1207         VM_OBJECT_ASSERT_WLOCKED(object);
 1208         KASSERT(m->object == NULL,
 1209             ("vm_page_insert_after: page already inserted"));
 1210         if (mpred != NULL) {
 1211                 KASSERT(mpred->object == object,
 1212                     ("vm_page_insert_after: object doesn't contain mpred"));
 1213                 KASSERT(mpred->pindex < pindex,
 1214                     ("vm_page_insert_after: mpred doesn't precede pindex"));
 1215                 msucc = TAILQ_NEXT(mpred, listq);
 1216         } else
 1217                 msucc = TAILQ_FIRST(&object->memq);
 1218         if (msucc != NULL)
 1219                 KASSERT(msucc->pindex > pindex,
 1220                     ("vm_page_insert_after: msucc doesn't succeed pindex"));
 1221 
 1222         /*
 1223          * Record the object/offset pair in this page
 1224          */
 1225         m->object = object;
 1226         m->pindex = pindex;
 1227 
 1228         /*
 1229          * Now link into the object's ordered list of backed pages.
 1230          */
 1231         if (vm_radix_insert(&object->rtree, m)) {
 1232                 m->object = NULL;
 1233                 m->pindex = 0;
 1234                 return (1);
 1235         }
 1236         vm_page_insert_radixdone(m, object, mpred);
 1237         return (0);
 1238 }
 1239 
 1240 /*
 1241  *      vm_page_insert_radixdone:
 1242  *
 1243  *      Complete page "m" insertion into the specified object after the
 1244  *      radix trie hooking.
 1245  *
 1246  *      The page "mpred" must precede the offset "m->pindex" within the
 1247  *      specified object.
 1248  *
 1249  *      The object must be locked.
 1250  */
 1251 static void
 1252 vm_page_insert_radixdone(vm_page_t m, vm_object_t object, vm_page_t mpred)
 1253 {
 1254 
 1255         VM_OBJECT_ASSERT_WLOCKED(object);
 1256         KASSERT(object != NULL && m->object == object,
 1257             ("vm_page_insert_radixdone: page %p has inconsistent object", m));
 1258         if (mpred != NULL) {
 1259                 KASSERT(mpred->object == object,
 1260                     ("vm_page_insert_after: object doesn't contain mpred"));
 1261                 KASSERT(mpred->pindex < m->pindex,
 1262                     ("vm_page_insert_after: mpred doesn't precede pindex"));
 1263         }
 1264 
 1265         if (mpred != NULL)
 1266                 TAILQ_INSERT_AFTER(&object->memq, mpred, m, listq);
 1267         else
 1268                 TAILQ_INSERT_HEAD(&object->memq, m, listq);
 1269 
 1270         /*
 1271          * Show that the object has one more resident page.
 1272          */
 1273         object->resident_page_count++;
 1274 
 1275         /*
 1276          * Hold the vnode until the last page is released.
 1277          */
 1278         if (object->resident_page_count == 1 && object->type == OBJT_VNODE)
 1279                 vhold(object->handle);
 1280 
 1281         /*
 1282          * Since we are inserting a new and possibly dirty page,
 1283          * update the object's OBJ_MIGHTBEDIRTY flag.
 1284          */
 1285         if (pmap_page_is_write_mapped(m))
 1286                 vm_object_set_writeable_dirty(object);
 1287 }
 1288 
 1289 /*
 1290  *      vm_page_remove:
 1291  *
 1292  *      Removes the specified page from its containing object, but does not
 1293  *      invalidate any backing storage.
 1294  *
 1295  *      The object must be locked.  The page must be locked if it is managed.
 1296  */
 1297 void
 1298 vm_page_remove(vm_page_t m)
 1299 {
 1300         vm_object_t object;
 1301         vm_page_t mrem;
 1302 
 1303         if ((m->oflags & VPO_UNMANAGED) == 0)
 1304                 vm_page_assert_locked(m);
 1305         if ((object = m->object) == NULL)
 1306                 return;
 1307         VM_OBJECT_ASSERT_WLOCKED(object);
 1308         if (vm_page_xbusied(m))
 1309                 vm_page_xunbusy_maybelocked(m);
 1310         mrem = vm_radix_remove(&object->rtree, m->pindex);
 1311         KASSERT(mrem == m, ("removed page %p, expected page %p", mrem, m));
 1312 
 1313         /*
 1314          * Now remove from the object's list of backed pages.
 1315          */
 1316         TAILQ_REMOVE(&object->memq, m, listq);
 1317 
 1318         /*
 1319          * And show that the object has one fewer resident page.
 1320          */
 1321         object->resident_page_count--;
 1322 
 1323         /*
 1324          * The vnode may now be recycled.
 1325          */
 1326         if (object->resident_page_count == 0 && object->type == OBJT_VNODE)
 1327                 vdrop(object->handle);
 1328 
 1329         m->object = NULL;
 1330 }
 1331 
 1332 /*
 1333  *      vm_page_lookup:
 1334  *
 1335  *      Returns the page associated with the object/offset
 1336  *      pair specified; if none is found, NULL is returned.
 1337  *
 1338  *      The object must be locked.
 1339  */
 1340 vm_page_t
 1341 vm_page_lookup(vm_object_t object, vm_pindex_t pindex)
 1342 {
 1343 
 1344         VM_OBJECT_ASSERT_LOCKED(object);
 1345         return (vm_radix_lookup(&object->rtree, pindex));
 1346 }
 1347 
 1348 /*
 1349  *      vm_page_find_least:
 1350  *
 1351  *      Returns the page associated with the object with least pindex
 1352  *      greater than or equal to the parameter pindex, or NULL.
 1353  *
 1354  *      The object must be locked.
 1355  */
 1356 vm_page_t
 1357 vm_page_find_least(vm_object_t object, vm_pindex_t pindex)
 1358 {
 1359         vm_page_t m;
 1360 
 1361         VM_OBJECT_ASSERT_LOCKED(object);
 1362         if ((m = TAILQ_FIRST(&object->memq)) != NULL && m->pindex < pindex)
 1363                 m = vm_radix_lookup_ge(&object->rtree, pindex);
 1364         return (m);
 1365 }
 1366 
 1367 /*
 1368  * Returns the given page's successor (by pindex) within the object if it is
 1369  * resident; if none is found, NULL is returned.
 1370  *
 1371  * The object must be locked.
 1372  */
 1373 vm_page_t
 1374 vm_page_next(vm_page_t m)
 1375 {
 1376         vm_page_t next;
 1377 
 1378         VM_OBJECT_ASSERT_LOCKED(m->object);
 1379         if ((next = TAILQ_NEXT(m, listq)) != NULL) {
 1380                 MPASS(next->object == m->object);
 1381                 if (next->pindex != m->pindex + 1)
 1382                         next = NULL;
 1383         }
 1384         return (next);
 1385 }
 1386 
 1387 /*
 1388  * Returns the given page's predecessor (by pindex) within the object if it is
 1389  * resident; if none is found, NULL is returned.
 1390  *
 1391  * The object must be locked.
 1392  */
 1393 vm_page_t
 1394 vm_page_prev(vm_page_t m)
 1395 {
 1396         vm_page_t prev;
 1397 
 1398         VM_OBJECT_ASSERT_LOCKED(m->object);
 1399         if ((prev = TAILQ_PREV(m, pglist, listq)) != NULL) {
 1400                 MPASS(prev->object == m->object);
 1401                 if (prev->pindex != m->pindex - 1)
 1402                         prev = NULL;
 1403         }
 1404         return (prev);
 1405 }
 1406 
 1407 /*
 1408  * Uses the page mnew as a replacement for an existing page at index
 1409  * pindex which must be already present in the object.
 1410  *
 1411  * The existing page must not be on a paging queue.
 1412  */
 1413 vm_page_t
 1414 vm_page_replace(vm_page_t mnew, vm_object_t object, vm_pindex_t pindex)
 1415 {
 1416         vm_page_t mold;
 1417 
 1418         VM_OBJECT_ASSERT_WLOCKED(object);
 1419         KASSERT(mnew->object == NULL,
 1420             ("vm_page_replace: page already in object"));
 1421 
 1422         /*
 1423          * This function mostly follows vm_page_insert() and
 1424          * vm_page_remove() without the radix, object count and vnode
 1425          * dance.  Double check such functions for more comments.
 1426          */
 1427 
 1428         mnew->object = object;
 1429         mnew->pindex = pindex;
 1430         mold = vm_radix_replace(&object->rtree, mnew);
 1431         KASSERT(mold->queue == PQ_NONE,
 1432             ("vm_page_replace: mold is on a paging queue"));
 1433 
 1434         /* Keep the resident page list in sorted order. */
 1435         TAILQ_INSERT_AFTER(&object->memq, mold, mnew, listq);
 1436         TAILQ_REMOVE(&object->memq, mold, listq);
 1437 
 1438         mold->object = NULL;
 1439         vm_page_xunbusy_maybelocked(mold);
 1440 
 1441         /*
 1442          * The object's resident_page_count does not change because we have
 1443          * swapped one page for another, but OBJ_MIGHTBEDIRTY.
 1444          */
 1445         if (pmap_page_is_write_mapped(mnew))
 1446                 vm_object_set_writeable_dirty(object);
 1447         return (mold);
 1448 }
 1449 
 1450 /*
 1451  *      vm_page_rename:
 1452  *
 1453  *      Move the given memory entry from its
 1454  *      current object to the specified target object/offset.
 1455  *
 1456  *      Note: swap associated with the page must be invalidated by the move.  We
 1457  *            have to do this for several reasons:  (1) we aren't freeing the
 1458  *            page, (2) we are dirtying the page, (3) the VM system is probably
 1459  *            moving the page from object A to B, and will then later move
 1460  *            the backing store from A to B and we can't have a conflict.
 1461  *
 1462  *      Note: we *always* dirty the page.  It is necessary both for the
 1463  *            fact that we moved it, and because we may be invalidating
 1464  *            swap.
 1465  *
 1466  *      The objects must be locked.
 1467  */
 1468 int
 1469 vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex)
 1470 {
 1471         vm_page_t mpred;
 1472         vm_pindex_t opidx;
 1473 
 1474         VM_OBJECT_ASSERT_WLOCKED(new_object);
 1475 
 1476         mpred = vm_radix_lookup_le(&new_object->rtree, new_pindex);
 1477         KASSERT(mpred == NULL || mpred->pindex != new_pindex,
 1478             ("vm_page_rename: pindex already renamed"));
 1479 
 1480         /*
 1481          * Create a custom version of vm_page_insert() which does not depend
 1482          * by m_prev and can cheat on the implementation aspects of the
 1483          * function.
 1484          */
 1485         opidx = m->pindex;
 1486         m->pindex = new_pindex;
 1487         if (vm_radix_insert(&new_object->rtree, m)) {
 1488                 m->pindex = opidx;
 1489                 return (1);
 1490         }
 1491 
 1492         /*
 1493          * The operation cannot fail anymore.  The removal must happen before
 1494          * the listq iterator is tainted.
 1495          */
 1496         m->pindex = opidx;
 1497         vm_page_lock(m);
 1498         vm_page_remove(m);
 1499 
 1500         /* Return back to the new pindex to complete vm_page_insert(). */
 1501         m->pindex = new_pindex;
 1502         m->object = new_object;
 1503         vm_page_unlock(m);
 1504         vm_page_insert_radixdone(m, new_object, mpred);
 1505         vm_page_dirty(m);
 1506         return (0);
 1507 }
 1508 
 1509 /*
 1510  *      vm_page_alloc:
 1511  *
 1512  *      Allocate and return a page that is associated with the specified
 1513  *      object and offset pair.  By default, this page is exclusive busied.
 1514  *
 1515  *      The caller must always specify an allocation class.
 1516  *
 1517  *      allocation classes:
 1518  *      VM_ALLOC_NORMAL         normal process request
 1519  *      VM_ALLOC_SYSTEM         system *really* needs a page
 1520  *      VM_ALLOC_INTERRUPT      interrupt time request
 1521  *
 1522  *      optional allocation flags:
 1523  *      VM_ALLOC_COUNT(number)  the number of additional pages that the caller
 1524  *                              intends to allocate
 1525  *      VM_ALLOC_NOBUSY         do not exclusive busy the page
 1526  *      VM_ALLOC_NODUMP         do not include the page in a kernel core dump
 1527  *      VM_ALLOC_NOOBJ          page is not associated with an object and
 1528  *                              should not be exclusive busy
 1529  *      VM_ALLOC_SBUSY          shared busy the allocated page
 1530  *      VM_ALLOC_WIRED          wire the allocated page
 1531  *      VM_ALLOC_ZERO           prefer a zeroed page
 1532  *
 1533  *      This routine may not sleep.
 1534  */
 1535 vm_page_t
 1536 vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req)
 1537 {
 1538         vm_page_t m, mpred;
 1539         int flags, req_class;
 1540 
 1541         mpred = NULL;   /* XXX: pacify gcc */
 1542         KASSERT((object != NULL) == ((req & VM_ALLOC_NOOBJ) == 0) &&
 1543             (object != NULL || (req & VM_ALLOC_SBUSY) == 0) &&
 1544             ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) !=
 1545             (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)),
 1546             ("vm_page_alloc: inconsistent object(%p)/req(%x)", object, req));
 1547         if (object != NULL)
 1548                 VM_OBJECT_ASSERT_WLOCKED(object);
 1549 
 1550         if (__predict_false((req & VM_ALLOC_IFCACHED) != 0))
 1551                 return (NULL);
 1552 
 1553         req_class = req & VM_ALLOC_CLASS_MASK;
 1554 
 1555         /*
 1556          * The page daemon is allowed to dig deeper into the free page list.
 1557          */
 1558         if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
 1559                 req_class = VM_ALLOC_SYSTEM;
 1560 
 1561         if (object != NULL) {
 1562                 mpred = vm_radix_lookup_le(&object->rtree, pindex);
 1563                 KASSERT(mpred == NULL || mpred->pindex != pindex,
 1564                    ("vm_page_alloc: pindex already allocated"));
 1565         }
 1566 
 1567         /*
 1568          * Allocate a page if the number of free pages exceeds the minimum
 1569          * for the request class.
 1570          */
 1571         mtx_lock(&vm_page_queue_free_mtx);
 1572         if (vm_cnt.v_free_count > vm_cnt.v_free_reserved ||
 1573             (req_class == VM_ALLOC_SYSTEM &&
 1574             vm_cnt.v_free_count > vm_cnt.v_interrupt_free_min) ||
 1575             (req_class == VM_ALLOC_INTERRUPT &&
 1576             vm_cnt.v_free_count > 0)) {
 1577                 /*
 1578                  * Can we allocate the page from a reservation?
 1579                  */
 1580 #if VM_NRESERVLEVEL > 0
 1581                 if (object == NULL || (object->flags & (OBJ_COLORED |
 1582                     OBJ_FICTITIOUS)) != OBJ_COLORED || (m =
 1583                     vm_reserv_alloc_page(object, pindex, mpred)) == NULL)
 1584 #endif
 1585                 {
 1586                         /*
 1587                          * If not, allocate it from the free page queues.
 1588                          */
 1589                         m = vm_phys_alloc_pages(object != NULL ?
 1590                             VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT, 0);
 1591 #if VM_NRESERVLEVEL > 0
 1592                         if (m == NULL && vm_reserv_reclaim_inactive()) {
 1593                                 m = vm_phys_alloc_pages(object != NULL ?
 1594                                     VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT,
 1595                                     0);
 1596                         }
 1597 #endif
 1598                 }
 1599         } else {
 1600                 /*
 1601                  * Not allocatable, give up.
 1602                  */
 1603                 mtx_unlock(&vm_page_queue_free_mtx);
 1604                 atomic_add_int(&vm_pageout_deficit,
 1605                     max((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1));
 1606                 pagedaemon_wakeup();
 1607                 return (NULL);
 1608         }
 1609 
 1610         /*
 1611          *  At this point we had better have found a good page.
 1612          */
 1613         KASSERT(m != NULL, ("vm_page_alloc: missing page"));
 1614         vm_phys_freecnt_adj(m, -1);
 1615         if ((m->flags & PG_ZERO) != 0)
 1616                 vm_page_zero_count--;
 1617         mtx_unlock(&vm_page_queue_free_mtx);
 1618         vm_page_alloc_check(m);
 1619 
 1620         /*
 1621          * Initialize the page.  Only the PG_ZERO flag is inherited.
 1622          */
 1623         flags = 0;
 1624         if ((req & VM_ALLOC_ZERO) != 0)
 1625                 flags = PG_ZERO;
 1626         flags &= m->flags;
 1627         if ((req & VM_ALLOC_NODUMP) != 0)
 1628                 flags |= PG_NODUMP;
 1629         m->flags = flags;
 1630         m->aflags = 0;
 1631         m->oflags = object == NULL || (object->flags & OBJ_UNMANAGED) != 0 ?
 1632             VPO_UNMANAGED : 0;
 1633         m->busy_lock = VPB_UNBUSIED;
 1634         if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ | VM_ALLOC_SBUSY)) == 0)
 1635                 m->busy_lock = VPB_SINGLE_EXCLUSIVER;
 1636         if ((req & VM_ALLOC_SBUSY) != 0)
 1637                 m->busy_lock = VPB_SHARERS_WORD(1);
 1638         if (req & VM_ALLOC_WIRED) {
 1639                 /*
 1640                  * The page lock is not required for wiring a page until that
 1641                  * page is inserted into the object.
 1642                  */
 1643                 atomic_add_int(&vm_cnt.v_wire_count, 1);
 1644                 m->wire_count = 1;
 1645         }
 1646         m->act_count = 0;
 1647 
 1648         if (object != NULL) {
 1649                 if (vm_page_insert_after(m, object, pindex, mpred)) {
 1650                         pagedaemon_wakeup();
 1651                         if (req & VM_ALLOC_WIRED) {
 1652                                 atomic_subtract_int(&vm_cnt.v_wire_count, 1);
 1653                                 m->wire_count = 0;
 1654                         }
 1655                         KASSERT(m->object == NULL, ("page %p has object", m));
 1656                         m->oflags = VPO_UNMANAGED;
 1657                         m->busy_lock = VPB_UNBUSIED;
 1658                         /* Don't change PG_ZERO. */
 1659                         vm_page_free_toq(m);
 1660                         return (NULL);
 1661                 }
 1662 
 1663                 /* Ignore device objects; the pager sets "memattr" for them. */
 1664                 if (object->memattr != VM_MEMATTR_DEFAULT &&
 1665                     (object->flags & OBJ_FICTITIOUS) == 0)
 1666                         pmap_page_set_memattr(m, object->memattr);
 1667         } else
 1668                 m->pindex = pindex;
 1669 
 1670         /*
 1671          * Don't wakeup too often - wakeup the pageout daemon when
 1672          * we would be nearly out of memory.
 1673          */
 1674         if (vm_paging_needed())
 1675                 pagedaemon_wakeup();
 1676 
 1677         return (m);
 1678 }
 1679 
 1680 /*
 1681  *      vm_page_alloc_contig:
 1682  *
 1683  *      Allocate a contiguous set of physical pages of the given size "npages"
 1684  *      from the free lists.  All of the physical pages must be at or above
 1685  *      the given physical address "low" and below the given physical address
 1686  *      "high".  The given value "alignment" determines the alignment of the
 1687  *      first physical page in the set.  If the given value "boundary" is
 1688  *      non-zero, then the set of physical pages cannot cross any physical
 1689  *      address boundary that is a multiple of that value.  Both "alignment"
 1690  *      and "boundary" must be a power of two.
 1691  *
 1692  *      If the specified memory attribute, "memattr", is VM_MEMATTR_DEFAULT,
 1693  *      then the memory attribute setting for the physical pages is configured
 1694  *      to the object's memory attribute setting.  Otherwise, the memory
 1695  *      attribute setting for the physical pages is configured to "memattr",
 1696  *      overriding the object's memory attribute setting.  However, if the
 1697  *      object's memory attribute setting is not VM_MEMATTR_DEFAULT, then the
 1698  *      memory attribute setting for the physical pages cannot be configured
 1699  *      to VM_MEMATTR_DEFAULT.
 1700  *
 1701  *      The specified object may not contain fictitious pages.
 1702  *
 1703  *      The caller must always specify an allocation class.
 1704  *
 1705  *      allocation classes:
 1706  *      VM_ALLOC_NORMAL         normal process request
 1707  *      VM_ALLOC_SYSTEM         system *really* needs a page
 1708  *      VM_ALLOC_INTERRUPT      interrupt time request
 1709  *
 1710  *      optional allocation flags:
 1711  *      VM_ALLOC_NOBUSY         do not exclusive busy the page
 1712  *      VM_ALLOC_NODUMP         do not include the page in a kernel core dump
 1713  *      VM_ALLOC_NOOBJ          page is not associated with an object and
 1714  *                              should not be exclusive busy
 1715  *      VM_ALLOC_SBUSY          shared busy the allocated page
 1716  *      VM_ALLOC_WIRED          wire the allocated page
 1717  *      VM_ALLOC_ZERO           prefer a zeroed page
 1718  *
 1719  *      This routine may not sleep.
 1720  */
 1721 vm_page_t
 1722 vm_page_alloc_contig(vm_object_t object, vm_pindex_t pindex, int req,
 1723     u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
 1724     vm_paddr_t boundary, vm_memattr_t memattr)
 1725 {
 1726         vm_page_t m, m_ret, mpred;
 1727         u_int busy_lock, flags, oflags;
 1728         int req_class;
 1729 
 1730         mpred = NULL;   /* XXX: pacify gcc */
 1731         KASSERT((object != NULL) == ((req & VM_ALLOC_NOOBJ) == 0) &&
 1732             (object != NULL || (req & VM_ALLOC_SBUSY) == 0) &&
 1733             ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) !=
 1734             (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)),
 1735             ("vm_page_alloc_contig: inconsistent object(%p)/req(%x)", object,
 1736             req));
 1737         if (object != NULL) {
 1738                 VM_OBJECT_ASSERT_WLOCKED(object);
 1739                 KASSERT((object->flags & OBJ_FICTITIOUS) == 0,
 1740                     ("vm_page_alloc_contig: object %p has fictitious pages",
 1741                     object));
 1742         }
 1743         KASSERT(npages > 0, ("vm_page_alloc_contig: npages is zero"));
 1744         req_class = req & VM_ALLOC_CLASS_MASK;
 1745 
 1746         /*
 1747          * The page daemon is allowed to dig deeper into the free page list.
 1748          */
 1749         if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
 1750                 req_class = VM_ALLOC_SYSTEM;
 1751 
 1752         if (object != NULL) {
 1753                 mpred = vm_radix_lookup_le(&object->rtree, pindex);
 1754                 KASSERT(mpred == NULL || mpred->pindex != pindex,
 1755                     ("vm_page_alloc_contig: pindex already allocated"));
 1756         }
 1757 
 1758         /*
 1759          * Can we allocate the pages without the number of free pages falling
 1760          * below the lower bound for the allocation class?
 1761          */
 1762         mtx_lock(&vm_page_queue_free_mtx);
 1763         if (vm_cnt.v_free_count >= npages + vm_cnt.v_free_reserved ||
 1764             (req_class == VM_ALLOC_SYSTEM &&
 1765             vm_cnt.v_free_count >= npages + vm_cnt.v_interrupt_free_min) ||
 1766             (req_class == VM_ALLOC_INTERRUPT &&
 1767             vm_cnt.v_free_count >= npages)) {
 1768                 /*
 1769                  * Can we allocate the pages from a reservation?
 1770                  */
 1771 #if VM_NRESERVLEVEL > 0
 1772 retry:
 1773                 if (object == NULL || (object->flags & OBJ_COLORED) == 0 ||
 1774                     (m_ret = vm_reserv_alloc_contig(object, pindex, npages,
 1775                     low, high, alignment, boundary, mpred)) == NULL)
 1776 #endif
 1777                         /*
 1778                          * If not, allocate them from the free page queues.
 1779                          */
 1780                         m_ret = vm_phys_alloc_contig(npages, low, high,
 1781                             alignment, boundary);
 1782         } else {
 1783                 mtx_unlock(&vm_page_queue_free_mtx);
 1784                 atomic_add_int(&vm_pageout_deficit, npages);
 1785                 pagedaemon_wakeup();
 1786                 return (NULL);
 1787         }
 1788         if (m_ret != NULL) {
 1789                 vm_phys_freecnt_adj(m_ret, -npages);
 1790                 for (m = m_ret; m < &m_ret[npages]; m++)
 1791                         if ((m->flags & PG_ZERO) != 0)
 1792                                 vm_page_zero_count--;
 1793         } else {
 1794 #if VM_NRESERVLEVEL > 0
 1795                 if (vm_reserv_reclaim_contig(npages, low, high, alignment,
 1796                     boundary))
 1797                         goto retry;
 1798 #endif
 1799         }
 1800         mtx_unlock(&vm_page_queue_free_mtx);
 1801         if (m_ret == NULL)
 1802                 return (NULL);
 1803         for (m = m_ret; m < &m_ret[npages]; m++)
 1804                 vm_page_alloc_check(m);
 1805 
 1806         /*
 1807          * Initialize the pages.  Only the PG_ZERO flag is inherited.
 1808          */
 1809         flags = 0;
 1810         if ((req & VM_ALLOC_ZERO) != 0)
 1811                 flags = PG_ZERO;
 1812         if ((req & VM_ALLOC_NODUMP) != 0)
 1813                 flags |= PG_NODUMP;
 1814         oflags = object == NULL || (object->flags & OBJ_UNMANAGED) != 0 ?
 1815             VPO_UNMANAGED : 0;
 1816         busy_lock = VPB_UNBUSIED;
 1817         if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ | VM_ALLOC_SBUSY)) == 0)
 1818                 busy_lock = VPB_SINGLE_EXCLUSIVER;
 1819         if ((req & VM_ALLOC_SBUSY) != 0)
 1820                 busy_lock = VPB_SHARERS_WORD(1);
 1821         if ((req & VM_ALLOC_WIRED) != 0)
 1822                 atomic_add_int(&vm_cnt.v_wire_count, npages);
 1823         if (object != NULL) {
 1824                 if (object->memattr != VM_MEMATTR_DEFAULT &&
 1825                     memattr == VM_MEMATTR_DEFAULT)
 1826                         memattr = object->memattr;
 1827         }
 1828         for (m = m_ret; m < &m_ret[npages]; m++) {
 1829                 m->aflags = 0;
 1830                 m->flags = (m->flags | PG_NODUMP) & flags;
 1831                 m->busy_lock = busy_lock;
 1832                 if ((req & VM_ALLOC_WIRED) != 0)
 1833                         m->wire_count = 1;
 1834                 m->act_count = 0;
 1835                 m->oflags = oflags;
 1836                 if (object != NULL) {
 1837                         if (vm_page_insert_after(m, object, pindex, mpred)) {
 1838                                 pagedaemon_wakeup();
 1839                                 if ((req & VM_ALLOC_WIRED) != 0)
 1840                                         atomic_subtract_int(
 1841                                             &vm_cnt.v_wire_count, npages);
 1842                                 KASSERT(m->object == NULL,
 1843                                     ("page %p has object", m));
 1844                                 mpred = m;
 1845                                 for (m = m_ret; m < &m_ret[npages]; m++) {
 1846                                         if (m <= mpred &&
 1847                                             (req & VM_ALLOC_WIRED) != 0)
 1848                                                 m->wire_count = 0;
 1849                                         m->oflags = VPO_UNMANAGED;
 1850                                         m->busy_lock = VPB_UNBUSIED;
 1851                                         /* Don't change PG_ZERO. */
 1852                                         vm_page_free_toq(m);
 1853                                 }
 1854                                 return (NULL);
 1855                         }
 1856                         mpred = m;
 1857                 } else
 1858                         m->pindex = pindex;
 1859                 if (memattr != VM_MEMATTR_DEFAULT)
 1860                         pmap_page_set_memattr(m, memattr);
 1861                 pindex++;
 1862         }
 1863         if (vm_paging_needed())
 1864                 pagedaemon_wakeup();
 1865         return (m_ret);
 1866 }
 1867 
 1868 /*
 1869  * Check a page that has been freshly dequeued from a freelist.
 1870  */
 1871 static void
 1872 vm_page_alloc_check(vm_page_t m)
 1873 {
 1874 
 1875         KASSERT(m->object == NULL, ("page %p has object", m));
 1876         KASSERT(m->queue == PQ_NONE,
 1877             ("page %p has unexpected queue %d", m, m->queue));
 1878         KASSERT(m->wire_count == 0, ("page %p is wired", m));
 1879         KASSERT(m->hold_count == 0, ("page %p is held", m));
 1880         KASSERT(!vm_page_busied(m), ("page %p is busy", m));
 1881         KASSERT(m->dirty == 0, ("page %p is dirty", m));
 1882         KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT,
 1883             ("page %p has unexpected memattr %d",
 1884             m, pmap_page_get_memattr(m)));
 1885         KASSERT(m->valid == 0, ("free page %p is valid", m));
 1886 }
 1887 
 1888 /*
 1889  *      vm_page_alloc_freelist:
 1890  *
 1891  *      Allocate a physical page from the specified free page list.
 1892  *
 1893  *      The caller must always specify an allocation class.
 1894  *
 1895  *      allocation classes:
 1896  *      VM_ALLOC_NORMAL         normal process request
 1897  *      VM_ALLOC_SYSTEM         system *really* needs a page
 1898  *      VM_ALLOC_INTERRUPT      interrupt time request
 1899  *
 1900  *      optional allocation flags:
 1901  *      VM_ALLOC_COUNT(number)  the number of additional pages that the caller
 1902  *                              intends to allocate
 1903  *      VM_ALLOC_WIRED          wire the allocated page
 1904  *      VM_ALLOC_ZERO           prefer a zeroed page
 1905  *
 1906  *      This routine may not sleep.
 1907  */
 1908 vm_page_t
 1909 vm_page_alloc_freelist(int flind, int req)
 1910 {
 1911         vm_page_t m;
 1912         u_int flags;
 1913         int req_class;
 1914 
 1915         req_class = req & VM_ALLOC_CLASS_MASK;
 1916 
 1917         /*
 1918          * The page daemon is allowed to dig deeper into the free page list.
 1919          */
 1920         if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
 1921                 req_class = VM_ALLOC_SYSTEM;
 1922 
 1923         /*
 1924          * Do not allocate reserved pages unless the req has asked for it.
 1925          */
 1926         mtx_lock(&vm_page_queue_free_mtx);
 1927         if (vm_cnt.v_free_count > vm_cnt.v_free_reserved ||
 1928             (req_class == VM_ALLOC_SYSTEM &&
 1929             vm_cnt.v_free_count > vm_cnt.v_interrupt_free_min) ||
 1930             (req_class == VM_ALLOC_INTERRUPT &&
 1931             vm_cnt.v_free_count > 0))
 1932                 m = vm_phys_alloc_freelist_pages(flind, VM_FREEPOOL_DIRECT, 0);
 1933         else {
 1934                 mtx_unlock(&vm_page_queue_free_mtx);
 1935                 atomic_add_int(&vm_pageout_deficit,
 1936                     max((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1));
 1937                 pagedaemon_wakeup();
 1938                 return (NULL);
 1939         }
 1940         if (m == NULL) {
 1941                 mtx_unlock(&vm_page_queue_free_mtx);
 1942                 return (NULL);
 1943         }
 1944         vm_phys_freecnt_adj(m, -1);
 1945         if ((m->flags & PG_ZERO) != 0)
 1946                 vm_page_zero_count--;
 1947         mtx_unlock(&vm_page_queue_free_mtx);
 1948         vm_page_alloc_check(m);
 1949 
 1950         /*
 1951          * Initialize the page.  Only the PG_ZERO flag is inherited.
 1952          */
 1953         m->aflags = 0;
 1954         flags = 0;
 1955         if ((req & VM_ALLOC_ZERO) != 0)
 1956                 flags = PG_ZERO;
 1957         m->flags &= flags;
 1958         if ((req & VM_ALLOC_WIRED) != 0) {
 1959                 /*
 1960                  * The page lock is not required for wiring a page that does
 1961                  * not belong to an object.
 1962                  */
 1963                 atomic_add_int(&vm_cnt.v_wire_count, 1);
 1964                 m->wire_count = 1;
 1965         }
 1966         /* Unmanaged pages don't use "act_count". */
 1967         m->oflags = VPO_UNMANAGED;
 1968         if (vm_paging_needed())
 1969                 pagedaemon_wakeup();
 1970         return (m);
 1971 }
 1972 
 1973 #define VPSC_ANY        0       /* No restrictions. */
 1974 #define VPSC_NORESERV   1       /* Skip reservations; implies VPSC_NOSUPER. */
 1975 #define VPSC_NOSUPER    2       /* Skip superpages. */
 1976 
 1977 /*
 1978  *      vm_page_scan_contig:
 1979  *
 1980  *      Scan vm_page_array[] between the specified entries "m_start" and
 1981  *      "m_end" for a run of contiguous physical pages that satisfy the
 1982  *      specified conditions, and return the lowest page in the run.  The
 1983  *      specified "alignment" determines the alignment of the lowest physical
 1984  *      page in the run.  If the specified "boundary" is non-zero, then the
 1985  *      run of physical pages cannot span a physical address that is a
 1986  *      multiple of "boundary".
 1987  *
 1988  *      "m_end" is never dereferenced, so it need not point to a vm_page
 1989  *      structure within vm_page_array[].
 1990  *
 1991  *      "npages" must be greater than zero.  "m_start" and "m_end" must not
 1992  *      span a hole (or discontiguity) in the physical address space.  Both
 1993  *      "alignment" and "boundary" must be a power of two.
 1994  */
 1995 vm_page_t
 1996 vm_page_scan_contig(u_long npages, vm_page_t m_start, vm_page_t m_end,
 1997     u_long alignment, vm_paddr_t boundary, int options)
 1998 {
 1999         struct mtx *m_mtx, *new_mtx;
 2000         vm_object_t object;
 2001         vm_paddr_t pa;
 2002         vm_page_t m, m_run;
 2003 #if VM_NRESERVLEVEL > 0
 2004         int level;
 2005 #endif
 2006         int m_inc, order, run_ext, run_len;
 2007 
 2008         KASSERT(npages > 0, ("npages is 0"));
 2009         KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
 2010         KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
 2011         m_run = NULL;
 2012         run_len = 0;
 2013         m_mtx = NULL;
 2014         for (m = m_start; m < m_end && run_len < npages; m += m_inc) {
 2015                 KASSERT((m->flags & (PG_FICTITIOUS | PG_MARKER)) == 0,
 2016                     ("page %p is PG_FICTITIOUS or PG_MARKER", m));
 2017 
 2018                 /*
 2019                  * If the current page would be the start of a run, check its
 2020                  * physical address against the end, alignment, and boundary
 2021                  * conditions.  If it doesn't satisfy these conditions, either
 2022                  * terminate the scan or advance to the next page that
 2023                  * satisfies the failed condition.
 2024                  */
 2025                 if (run_len == 0) {
 2026                         KASSERT(m_run == NULL, ("m_run != NULL"));
 2027                         if (m + npages > m_end)
 2028                                 break;
 2029                         pa = VM_PAGE_TO_PHYS(m);
 2030                         if ((pa & (alignment - 1)) != 0) {
 2031                                 m_inc = atop(roundup2(pa, alignment) - pa);
 2032                                 continue;
 2033                         }
 2034                         if (rounddown2(pa ^ (pa + ptoa(npages) - 1),
 2035                             boundary) != 0) {
 2036                                 m_inc = atop(roundup2(pa, boundary) - pa);
 2037                                 continue;
 2038                         }
 2039                 } else
 2040                         KASSERT(m_run != NULL, ("m_run == NULL"));
 2041 
 2042                 /*
 2043                  * Avoid releasing and reacquiring the same page lock.
 2044                  */
 2045                 new_mtx = vm_page_lockptr(m);
 2046                 if (m_mtx != new_mtx) {
 2047                         if (m_mtx != NULL)
 2048                                 mtx_unlock(m_mtx);
 2049                         m_mtx = new_mtx;
 2050                         mtx_lock(m_mtx);
 2051                 }
 2052                 m_inc = 1;
 2053 retry:
 2054                 if (m->wire_count != 0 || m->hold_count != 0)
 2055                         run_ext = 0;
 2056 #if VM_NRESERVLEVEL > 0
 2057                 else if ((level = vm_reserv_level(m)) >= 0 &&
 2058                     (options & VPSC_NORESERV) != 0) {
 2059                         run_ext = 0;
 2060                         /* Advance to the end of the reservation. */
 2061                         pa = VM_PAGE_TO_PHYS(m);
 2062                         m_inc = atop(roundup2(pa + 1, vm_reserv_size(level)) -
 2063                             pa);
 2064                 }
 2065 #endif
 2066                 else if ((object = m->object) != NULL) {
 2067                         /*
 2068                          * The page is considered eligible for relocation if
 2069                          * and only if it could be laundered or reclaimed by
 2070                          * the page daemon.
 2071                          */
 2072                         if (!VM_OBJECT_TRYRLOCK(object)) {
 2073                                 mtx_unlock(m_mtx);
 2074                                 VM_OBJECT_RLOCK(object);
 2075                                 mtx_lock(m_mtx);
 2076                                 if (m->object != object) {
 2077                                         /*
 2078                                          * The page may have been freed.
 2079                                          */
 2080                                         VM_OBJECT_RUNLOCK(object);
 2081                                         goto retry;
 2082                                 } else if (m->wire_count != 0 ||
 2083                                     m->hold_count != 0) {
 2084                                         run_ext = 0;
 2085                                         goto unlock;
 2086                                 }
 2087                         }
 2088                         KASSERT((m->flags & PG_UNHOLDFREE) == 0,
 2089                             ("page %p is PG_UNHOLDFREE", m));
 2090                         /* Don't care: PG_NODUMP, PG_ZERO. */
 2091                         if (object->type != OBJT_DEFAULT &&
 2092                             object->type != OBJT_SWAP &&
 2093                             object->type != OBJT_VNODE) {
 2094                                 run_ext = 0;
 2095 #if VM_NRESERVLEVEL > 0
 2096                         } else if ((options & VPSC_NOSUPER) != 0 &&
 2097                             (level = vm_reserv_level_iffullpop(m)) >= 0) {
 2098                                 run_ext = 0;
 2099                                 /* Advance to the end of the superpage. */
 2100                                 pa = VM_PAGE_TO_PHYS(m);
 2101                                 m_inc = atop(roundup2(pa + 1,
 2102                                     vm_reserv_size(level)) - pa);
 2103 #endif
 2104                         } else if (object->memattr == VM_MEMATTR_DEFAULT &&
 2105                             m->queue != PQ_NONE && !vm_page_busied(m)) {
 2106                                 /*
 2107                                  * The page is allocated but eligible for
 2108                                  * relocation.  Extend the current run by one
 2109                                  * page.
 2110                                  */
 2111                                 KASSERT(pmap_page_get_memattr(m) ==
 2112                                     VM_MEMATTR_DEFAULT,
 2113                                     ("page %p has an unexpected memattr", m));
 2114                                 KASSERT((m->oflags & (VPO_SWAPINPROG |
 2115                                     VPO_SWAPSLEEP | VPO_UNMANAGED)) == 0,
 2116                                     ("page %p has unexpected oflags", m));
 2117                                 /* Don't care: VPO_NOSYNC. */
 2118                                 run_ext = 1;
 2119                         } else
 2120                                 run_ext = 0;
 2121 unlock:
 2122                         VM_OBJECT_RUNLOCK(object);
 2123 #if VM_NRESERVLEVEL > 0
 2124                 } else if (level >= 0) {
 2125                         /*
 2126                          * The page is reserved but not yet allocated.  In
 2127                          * other words, it is still free.  Extend the current
 2128                          * run by one page.
 2129                          */
 2130                         run_ext = 1;
 2131 #endif
 2132                 } else if ((order = m->order) < VM_NFREEORDER) {
 2133                         /*
 2134                          * The page is enqueued in the physical memory
 2135                          * allocator's free page queues.  Moreover, it is the
 2136                          * first page in a power-of-two-sized run of
 2137                          * contiguous free pages.  Add these pages to the end
 2138                          * of the current run, and jump ahead.
 2139                          */
 2140                         run_ext = 1 << order;
 2141                         m_inc = 1 << order;
 2142                 } else {
 2143                         /*
 2144                          * Skip the page for one of the following reasons: (1)
 2145                          * It is enqueued in the physical memory allocator's
 2146                          * free page queues.  However, it is not the first
 2147                          * page in a run of contiguous free pages.  (This case
 2148                          * rarely occurs because the scan is performed in
 2149                          * ascending order.) (2) It is not reserved, and it is
 2150                          * transitioning from free to allocated.  (Conversely,
 2151                          * the transition from allocated to free for managed
 2152                          * pages is blocked by the page lock.) (3) It is
 2153                          * allocated but not contained by an object and not
 2154                          * wired, e.g., allocated by Xen's balloon driver.
 2155                          */
 2156                         run_ext = 0;
 2157                 }
 2158 
 2159                 /*
 2160                  * Extend or reset the current run of pages.
 2161                  */
 2162                 if (run_ext > 0) {
 2163                         if (run_len == 0)
 2164                                 m_run = m;
 2165                         run_len += run_ext;
 2166                 } else {
 2167                         if (run_len > 0) {
 2168                                 m_run = NULL;
 2169                                 run_len = 0;
 2170                         }
 2171                 }
 2172         }
 2173         if (m_mtx != NULL)
 2174                 mtx_unlock(m_mtx);
 2175         if (run_len >= npages)
 2176                 return (m_run);
 2177         return (NULL);
 2178 }
 2179 
 2180 /*
 2181  *      vm_page_reclaim_run:
 2182  *
 2183  *      Try to relocate each of the allocated virtual pages within the
 2184  *      specified run of physical pages to a new physical address.  Free the
 2185  *      physical pages underlying the relocated virtual pages.  A virtual page
 2186  *      is relocatable if and only if it could be laundered or reclaimed by
 2187  *      the page daemon.  Whenever possible, a virtual page is relocated to a
 2188  *      physical address above "high".
 2189  *
 2190  *      Returns 0 if every physical page within the run was already free or
 2191  *      just freed by a successful relocation.  Otherwise, returns a non-zero
 2192  *      value indicating why the last attempt to relocate a virtual page was
 2193  *      unsuccessful.
 2194  *
 2195  *      "req_class" must be an allocation class.
 2196  */
 2197 static int
 2198 vm_page_reclaim_run(int req_class, u_long npages, vm_page_t m_run,
 2199     vm_paddr_t high)
 2200 {
 2201         struct mtx *m_mtx, *new_mtx;
 2202         struct spglist free;
 2203         vm_object_t object;
 2204         vm_paddr_t pa;
 2205         vm_page_t m, m_end, m_new;
 2206         int error, order, req;
 2207 
 2208         KASSERT((req_class & VM_ALLOC_CLASS_MASK) == req_class,
 2209             ("req_class is not an allocation class"));
 2210         SLIST_INIT(&free);
 2211         error = 0;
 2212         m = m_run;
 2213         m_end = m_run + npages;
 2214         m_mtx = NULL;
 2215         for (; error == 0 && m < m_end; m++) {
 2216                 KASSERT((m->flags & (PG_FICTITIOUS | PG_MARKER)) == 0,
 2217                     ("page %p is PG_FICTITIOUS or PG_MARKER", m));
 2218 
 2219                 /*
 2220                  * Avoid releasing and reacquiring the same page lock.
 2221                  */
 2222                 new_mtx = vm_page_lockptr(m);
 2223                 if (m_mtx != new_mtx) {
 2224                         if (m_mtx != NULL)
 2225                                 mtx_unlock(m_mtx);
 2226                         m_mtx = new_mtx;
 2227                         mtx_lock(m_mtx);
 2228                 }
 2229 retry:
 2230                 if (m->wire_count != 0 || m->hold_count != 0)
 2231                         error = EBUSY;
 2232                 else if ((object = m->object) != NULL) {
 2233                         /*
 2234                          * The page is relocated if and only if it could be
 2235                          * laundered or reclaimed by the page daemon.
 2236                          */
 2237                         if (!VM_OBJECT_TRYWLOCK(object)) {
 2238                                 mtx_unlock(m_mtx);
 2239                                 VM_OBJECT_WLOCK(object);
 2240                                 mtx_lock(m_mtx);
 2241                                 if (m->object != object) {
 2242                                         /*
 2243                                          * The page may have been freed.
 2244                                          */
 2245                                         VM_OBJECT_WUNLOCK(object);
 2246                                         goto retry;
 2247                                 } else if (m->wire_count != 0 ||
 2248                                     m->hold_count != 0) {
 2249                                         error = EBUSY;
 2250                                         goto unlock;
 2251                                 }
 2252                         }
 2253                         KASSERT((m->flags & PG_UNHOLDFREE) == 0,
 2254                             ("page %p is PG_UNHOLDFREE", m));
 2255                         /* Don't care: PG_NODUMP, PG_ZERO. */
 2256                         if (object->type != OBJT_DEFAULT &&
 2257                             object->type != OBJT_SWAP &&
 2258                             object->type != OBJT_VNODE)
 2259                                 error = EINVAL;
 2260                         else if (object->memattr != VM_MEMATTR_DEFAULT)
 2261                                 error = EINVAL;
 2262                         else if (m->queue != PQ_NONE && !vm_page_busied(m)) {
 2263                                 KASSERT(pmap_page_get_memattr(m) ==
 2264                                     VM_MEMATTR_DEFAULT,
 2265                                     ("page %p has an unexpected memattr", m));
 2266                                 KASSERT((m->oflags & (VPO_SWAPINPROG |
 2267                                     VPO_SWAPSLEEP | VPO_UNMANAGED)) == 0,
 2268                                     ("page %p has unexpected oflags", m));
 2269                                 /* Don't care: VPO_NOSYNC. */
 2270                                 if (m->valid != 0) {
 2271                                         /*
 2272                                          * First, try to allocate a new page
 2273                                          * that is above "high".  Failing
 2274                                          * that, try to allocate a new page
 2275                                          * that is below "m_run".  Allocate
 2276                                          * the new page between the end of
 2277                                          * "m_run" and "high" only as a last
 2278                                          * resort.
 2279                                          */
 2280                                         req = req_class | VM_ALLOC_NOOBJ;
 2281                                         if ((m->flags & PG_NODUMP) != 0)
 2282                                                 req |= VM_ALLOC_NODUMP;
 2283                                         if (trunc_page(high) !=
 2284                                             ~(vm_paddr_t)PAGE_MASK) {
 2285                                                 m_new = vm_page_alloc_contig(
 2286                                                     NULL, 0, req, 1,
 2287                                                     round_page(high),
 2288                                                     ~(vm_paddr_t)0,
 2289                                                     PAGE_SIZE, 0,
 2290                                                     VM_MEMATTR_DEFAULT);
 2291                                         } else
 2292                                                 m_new = NULL;
 2293                                         if (m_new == NULL) {
 2294                                                 pa = VM_PAGE_TO_PHYS(m_run);
 2295                                                 m_new = vm_page_alloc_contig(
 2296                                                     NULL, 0, req, 1,
 2297                                                     0, pa - 1, PAGE_SIZE, 0,
 2298                                                     VM_MEMATTR_DEFAULT);
 2299                                         }
 2300                                         if (m_new == NULL) {
 2301                                                 pa += ptoa(npages);
 2302                                                 m_new = vm_page_alloc_contig(
 2303                                                     NULL, 0, req, 1,
 2304                                                     pa, high, PAGE_SIZE, 0,
 2305                                                     VM_MEMATTR_DEFAULT);
 2306                                         }
 2307                                         if (m_new == NULL) {
 2308                                                 error = ENOMEM;
 2309                                                 goto unlock;
 2310                                         }
 2311                                         KASSERT(m_new->wire_count == 0,
 2312                                             ("page %p is wired", m));
 2313 
 2314                                         /*
 2315                                          * Replace "m" with the new page.  For
 2316                                          * vm_page_replace(), "m" must be busy
 2317                                          * and dequeued.  Finally, change "m"
 2318                                          * as if vm_page_free() was called.
 2319                                          */
 2320                                         if (object->ref_count != 0)
 2321                                                 pmap_remove_all(m);
 2322                                         m_new->aflags = m->aflags;
 2323                                         KASSERT(m_new->oflags == VPO_UNMANAGED,
 2324                                             ("page %p is managed", m));
 2325                                         m_new->oflags = m->oflags & VPO_NOSYNC;
 2326                                         pmap_copy_page(m, m_new);
 2327                                         m_new->valid = m->valid;
 2328                                         m_new->dirty = m->dirty;
 2329                                         m->flags &= ~PG_ZERO;
 2330                                         vm_page_xbusy(m);
 2331                                         vm_page_remque(m);
 2332                                         vm_page_replace_checked(m_new, object,
 2333                                             m->pindex, m);
 2334                                         m->valid = 0;
 2335                                         vm_page_undirty(m);
 2336 
 2337                                         /*
 2338                                          * The new page must be deactivated
 2339                                          * before the object is unlocked.
 2340                                          */
 2341                                         new_mtx = vm_page_lockptr(m_new);
 2342                                         if (m_mtx != new_mtx) {
 2343                                                 mtx_unlock(m_mtx);
 2344                                                 m_mtx = new_mtx;
 2345                                                 mtx_lock(m_mtx);
 2346                                         }
 2347                                         vm_page_deactivate(m_new);
 2348                                 } else {
 2349                                         m->flags &= ~PG_ZERO;
 2350                                         vm_page_remque(m);
 2351                                         vm_page_remove(m);
 2352                                         KASSERT(m->dirty == 0,
 2353                                             ("page %p is dirty", m));
 2354                                 }
 2355                                 SLIST_INSERT_HEAD(&free, m, plinks.s.ss);
 2356                         } else
 2357                                 error = EBUSY;
 2358 unlock:
 2359                         VM_OBJECT_WUNLOCK(object);
 2360                 } else {
 2361                         mtx_lock(&vm_page_queue_free_mtx);
 2362                         order = m->order;
 2363                         if (order < VM_NFREEORDER) {
 2364                                 /*
 2365                                  * The page is enqueued in the physical memory
 2366                                  * allocator's free page queues.  Moreover, it
 2367                                  * is the first page in a power-of-two-sized
 2368                                  * run of contiguous free pages.  Jump ahead
 2369                                  * to the last page within that run, and
 2370                                  * continue from there.
 2371                                  */
 2372                                 m += (1 << order) - 1;
 2373                         }
 2374 #if VM_NRESERVLEVEL > 0
 2375                         else if (vm_reserv_is_page_free(m))
 2376                                 order = 0;
 2377 #endif
 2378                         mtx_unlock(&vm_page_queue_free_mtx);
 2379                         if (order == VM_NFREEORDER)
 2380                                 error = EINVAL;
 2381                 }
 2382         }
 2383         if (m_mtx != NULL)
 2384                 mtx_unlock(m_mtx);
 2385         if ((m = SLIST_FIRST(&free)) != NULL) {
 2386                 mtx_lock(&vm_page_queue_free_mtx);
 2387                 do {
 2388                         SLIST_REMOVE_HEAD(&free, plinks.s.ss);
 2389                         vm_phys_freecnt_adj(m, 1);
 2390 #if VM_NRESERVLEVEL > 0
 2391                         if (!vm_reserv_free_page(m))
 2392 #else
 2393                         if (true)
 2394 #endif
 2395                                 vm_phys_free_pages(m, 0);
 2396                 } while ((m = SLIST_FIRST(&free)) != NULL);
 2397                 vm_page_zero_idle_wakeup();
 2398                 vm_page_free_wakeup();
 2399                 mtx_unlock(&vm_page_queue_free_mtx);
 2400         }
 2401         return (error);
 2402 }
 2403 
 2404 #define NRUNS   16
 2405 
 2406 CTASSERT(powerof2(NRUNS));
 2407 
 2408 #define RUN_INDEX(count)        ((count) & (NRUNS - 1))
 2409 
 2410 #define MIN_RECLAIM     8
 2411 
 2412 /*
 2413  *      vm_page_reclaim_contig:
 2414  *
 2415  *      Reclaim allocated, contiguous physical memory satisfying the specified
 2416  *      conditions by relocating the virtual pages using that physical memory.
 2417  *      Returns true if reclamation is successful and false otherwise.  Since
 2418  *      relocation requires the allocation of physical pages, reclamation may
 2419  *      fail due to a shortage of free pages.  When reclamation fails, callers
 2420  *      are expected to perform VM_WAIT before retrying a failed allocation
 2421  *      operation, e.g., vm_page_alloc_contig().
 2422  *
 2423  *      The caller must always specify an allocation class through "req".
 2424  *
 2425  *      allocation classes:
 2426  *      VM_ALLOC_NORMAL         normal process request
 2427  *      VM_ALLOC_SYSTEM         system *really* needs a page
 2428  *      VM_ALLOC_INTERRUPT      interrupt time request
 2429  *
 2430  *      The optional allocation flags are ignored.
 2431  *
 2432  *      "npages" must be greater than zero.  Both "alignment" and "boundary"
 2433  *      must be a power of two.
 2434  */
 2435 bool
 2436 vm_page_reclaim_contig(int req, u_long npages, vm_paddr_t low, vm_paddr_t high,
 2437     u_long alignment, vm_paddr_t boundary)
 2438 {
 2439         vm_paddr_t curr_low;
 2440         vm_page_t m_run, m_runs[NRUNS];
 2441         u_long count, reclaimed;
 2442         int error, i, options, req_class;
 2443 
 2444         KASSERT(npages > 0, ("npages is 0"));
 2445         KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
 2446         KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
 2447         req_class = req & VM_ALLOC_CLASS_MASK;
 2448 
 2449         /*
 2450          * The page daemon is allowed to dig deeper into the free page list.
 2451          */
 2452         if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
 2453                 req_class = VM_ALLOC_SYSTEM;
 2454 
 2455         /*
 2456          * Return if the number of free pages cannot satisfy the requested
 2457          * allocation.
 2458          */
 2459         count = vm_cnt.v_free_count;
 2460         if (count < npages + vm_cnt.v_free_reserved || (count < npages +
 2461             vm_cnt.v_interrupt_free_min && req_class == VM_ALLOC_SYSTEM) ||
 2462             (count < npages && req_class == VM_ALLOC_INTERRUPT))
 2463                 return (false);
 2464 
 2465         /*
 2466          * Scan up to three times, relaxing the restrictions ("options") on
 2467          * the reclamation of reservations and superpages each time.
 2468          */
 2469         for (options = VPSC_NORESERV;;) {
 2470                 /*
 2471                  * Find the highest runs that satisfy the given constraints
 2472                  * and restrictions, and record them in "m_runs".
 2473                  */
 2474                 curr_low = low;
 2475                 count = 0;
 2476                 for (;;) {
 2477                         m_run = vm_phys_scan_contig(npages, curr_low, high,
 2478                             alignment, boundary, options);
 2479                         if (m_run == NULL)
 2480                                 break;
 2481                         curr_low = VM_PAGE_TO_PHYS(m_run) + ptoa(npages);
 2482                         m_runs[RUN_INDEX(count)] = m_run;
 2483                         count++;
 2484                 }
 2485 
 2486                 /*
 2487                  * Reclaim the highest runs in LIFO (descending) order until
 2488                  * the number of reclaimed pages, "reclaimed", is at least
 2489                  * MIN_RECLAIM.  Reset "reclaimed" each time because each
 2490                  * reclamation is idempotent, and runs will (likely) recur
 2491                  * from one scan to the next as restrictions are relaxed.
 2492                  */
 2493                 reclaimed = 0;
 2494                 for (i = 0; count > 0 && i < NRUNS; i++) {
 2495                         count--;
 2496                         m_run = m_runs[RUN_INDEX(count)];
 2497                         error = vm_page_reclaim_run(req_class, npages, m_run,
 2498                             high);
 2499                         if (error == 0) {
 2500                                 reclaimed += npages;
 2501                                 if (reclaimed >= MIN_RECLAIM)
 2502                                         return (true);
 2503                         }
 2504                 }
 2505 
 2506                 /*
 2507                  * Either relax the restrictions on the next scan or return if
 2508                  * the last scan had no restrictions.
 2509                  */
 2510                 if (options == VPSC_NORESERV)
 2511                         options = VPSC_NOSUPER;
 2512                 else if (options == VPSC_NOSUPER)
 2513                         options = VPSC_ANY;
 2514                 else if (options == VPSC_ANY)
 2515                         return (reclaimed != 0);
 2516         }
 2517 }
 2518 
 2519 /*
 2520  *      vm_wait:        (also see VM_WAIT macro)
 2521  *
 2522  *      Sleep until free pages are available for allocation.
 2523  *      - Called in various places before memory allocations.
 2524  */
 2525 void
 2526 vm_wait(void)
 2527 {
 2528 
 2529         mtx_lock(&vm_page_queue_free_mtx);
 2530         if (curproc == pageproc) {
 2531                 vm_pageout_pages_needed = 1;
 2532                 msleep(&vm_pageout_pages_needed, &vm_page_queue_free_mtx,
 2533                     PDROP | PSWP, "VMWait", 0);
 2534         } else {
 2535                 if (__predict_false(pageproc == NULL))
 2536                         panic("vm_wait in early boot");
 2537                 if (!vm_pageout_wanted) {
 2538                         vm_pageout_wanted = true;
 2539                         wakeup(&vm_pageout_wanted);
 2540                 }
 2541                 vm_pages_needed = true;
 2542                 msleep(&vm_cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PVM,
 2543                     "vmwait", 0);
 2544         }
 2545 }
 2546 
 2547 /*
 2548  *      vm_waitpfault:  (also see VM_WAITPFAULT macro)
 2549  *
 2550  *      Sleep until free pages are available for allocation.
 2551  *      - Called only in vm_fault so that processes page faulting
 2552  *        can be easily tracked.
 2553  *      - Sleeps at a lower priority than vm_wait() so that vm_wait()ing
 2554  *        processes will be able to grab memory first.  Do not change
 2555  *        this balance without careful testing first.
 2556  */
 2557 void
 2558 vm_waitpfault(void)
 2559 {
 2560 
 2561         mtx_lock(&vm_page_queue_free_mtx);
 2562         if (!vm_pageout_wanted) {
 2563                 vm_pageout_wanted = true;
 2564                 wakeup(&vm_pageout_wanted);
 2565         }
 2566         vm_pages_needed = true;
 2567         msleep(&vm_cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PUSER,
 2568             "pfault", 0);
 2569 }
 2570 
 2571 struct vm_pagequeue *
 2572 vm_page_pagequeue(vm_page_t m)
 2573 {
 2574 
 2575         if (vm_page_in_laundry(m))
 2576                 return (&vm_dom[0].vmd_pagequeues[m->queue]);
 2577         else
 2578                 return (&vm_phys_domain(m)->vmd_pagequeues[m->queue]);
 2579 }
 2580 
 2581 /*
 2582  *      vm_page_dequeue:
 2583  *
 2584  *      Remove the given page from its current page queue.
 2585  *
 2586  *      The page must be locked.
 2587  */
 2588 void
 2589 vm_page_dequeue(vm_page_t m)
 2590 {
 2591         struct vm_pagequeue *pq;
 2592 
 2593         vm_page_assert_locked(m);
 2594         KASSERT(m->queue < PQ_COUNT, ("vm_page_dequeue: page %p is not queued",
 2595             m));
 2596         pq = vm_page_pagequeue(m);
 2597         vm_pagequeue_lock(pq);
 2598         m->queue = PQ_NONE;
 2599         TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
 2600         vm_pagequeue_cnt_dec(pq);
 2601         vm_pagequeue_unlock(pq);
 2602 }
 2603 
 2604 /*
 2605  *      vm_page_dequeue_locked:
 2606  *
 2607  *      Remove the given page from its current page queue.
 2608  *
 2609  *      The page and page queue must be locked.
 2610  */
 2611 void
 2612 vm_page_dequeue_locked(vm_page_t m)
 2613 {
 2614         struct vm_pagequeue *pq;
 2615 
 2616         vm_page_lock_assert(m, MA_OWNED);
 2617         pq = vm_page_pagequeue(m);
 2618         vm_pagequeue_assert_locked(pq);
 2619         m->queue = PQ_NONE;
 2620         TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
 2621         vm_pagequeue_cnt_dec(pq);
 2622 }
 2623 
 2624 /*
 2625  *      vm_page_enqueue:
 2626  *
 2627  *      Add the given page to the specified page queue.
 2628  *
 2629  *      The page must be locked.
 2630  */
 2631 static void
 2632 vm_page_enqueue(uint8_t queue, vm_page_t m)
 2633 {
 2634         struct vm_pagequeue *pq;
 2635 
 2636         vm_page_lock_assert(m, MA_OWNED);
 2637         KASSERT(queue < PQ_COUNT,
 2638             ("vm_page_enqueue: invalid queue %u request for page %p",
 2639             queue, m));
 2640         if (queue == PQ_LAUNDRY)
 2641                 pq = &vm_dom[0].vmd_pagequeues[queue];
 2642         else
 2643                 pq = &vm_phys_domain(m)->vmd_pagequeues[queue];
 2644         vm_pagequeue_lock(pq);
 2645         m->queue = queue;
 2646         TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
 2647         vm_pagequeue_cnt_inc(pq);
 2648         vm_pagequeue_unlock(pq);
 2649 }
 2650 
 2651 /*
 2652  *      vm_page_requeue:
 2653  *
 2654  *      Move the given page to the tail of its current page queue.
 2655  *
 2656  *      The page must be locked.
 2657  */
 2658 void
 2659 vm_page_requeue(vm_page_t m)
 2660 {
 2661         struct vm_pagequeue *pq;
 2662 
 2663         vm_page_lock_assert(m, MA_OWNED);
 2664         KASSERT(m->queue != PQ_NONE,
 2665             ("vm_page_requeue: page %p is not queued", m));
 2666         pq = vm_page_pagequeue(m);
 2667         vm_pagequeue_lock(pq);
 2668         TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
 2669         TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
 2670         vm_pagequeue_unlock(pq);
 2671 }
 2672 
 2673 /*
 2674  *      vm_page_requeue_locked:
 2675  *
 2676  *      Move the given page to the tail of its current page queue.
 2677  *
 2678  *      The page queue must be locked.
 2679  */
 2680 void
 2681 vm_page_requeue_locked(vm_page_t m)
 2682 {
 2683         struct vm_pagequeue *pq;
 2684 
 2685         KASSERT(m->queue != PQ_NONE,
 2686             ("vm_page_requeue_locked: page %p is not queued", m));
 2687         pq = vm_page_pagequeue(m);
 2688         vm_pagequeue_assert_locked(pq);
 2689         TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
 2690         TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
 2691 }
 2692 
 2693 /*
 2694  *      vm_page_activate:
 2695  *
 2696  *      Put the specified page on the active list (if appropriate).
 2697  *      Ensure that act_count is at least ACT_INIT but do not otherwise
 2698  *      mess with it.
 2699  *
 2700  *      The page must be locked.
 2701  */
 2702 void
 2703 vm_page_activate(vm_page_t m)
 2704 {
 2705         int queue;
 2706 
 2707         vm_page_lock_assert(m, MA_OWNED);
 2708         if ((queue = m->queue) != PQ_ACTIVE) {
 2709                 if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) {
 2710                         if (m->act_count < ACT_INIT)
 2711                                 m->act_count = ACT_INIT;
 2712                         if (queue != PQ_NONE)
 2713                                 vm_page_dequeue(m);
 2714                         vm_page_enqueue(PQ_ACTIVE, m);
 2715                 } else
 2716                         KASSERT(queue == PQ_NONE,
 2717                             ("vm_page_activate: wired page %p is queued", m));
 2718         } else {
 2719                 if (m->act_count < ACT_INIT)
 2720                         m->act_count = ACT_INIT;
 2721         }
 2722 }
 2723 
 2724 /*
 2725  *      vm_page_free_wakeup:
 2726  *
 2727  *      Helper routine for vm_page_free_toq().  This routine is called
 2728  *      when a page is added to the free queues.
 2729  *
 2730  *      The page queues must be locked.
 2731  */
 2732 static inline void
 2733 vm_page_free_wakeup(void)
 2734 {
 2735 
 2736         mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
 2737         /*
 2738          * if pageout daemon needs pages, then tell it that there are
 2739          * some free.
 2740          */
 2741         if (vm_pageout_pages_needed &&
 2742             vm_cnt.v_free_count >= vm_cnt.v_pageout_free_min) {
 2743                 wakeup(&vm_pageout_pages_needed);
 2744                 vm_pageout_pages_needed = 0;
 2745         }
 2746         /*
 2747          * wakeup processes that are waiting on memory if we hit a
 2748          * high water mark. And wakeup scheduler process if we have
 2749          * lots of memory. this process will swapin processes.
 2750          */
 2751         if (vm_pages_needed && !vm_page_count_min()) {
 2752                 vm_pages_needed = false;
 2753                 wakeup(&vm_cnt.v_free_count);
 2754         }
 2755 }
 2756 
 2757 /*
 2758  *      vm_page_free_toq:
 2759  *
 2760  *      Returns the given page to the free list,
 2761  *      disassociating it with any VM object.
 2762  *
 2763  *      The object must be locked.  The page must be locked if it is managed.
 2764  */
 2765 void
 2766 vm_page_free_toq(vm_page_t m)
 2767 {
 2768 
 2769         if ((m->oflags & VPO_UNMANAGED) == 0) {
 2770                 vm_page_lock_assert(m, MA_OWNED);
 2771                 KASSERT(!pmap_page_is_mapped(m),
 2772                     ("vm_page_free_toq: freeing mapped page %p", m));
 2773         } else
 2774                 KASSERT(m->queue == PQ_NONE,
 2775                     ("vm_page_free_toq: unmanaged page %p is queued", m));
 2776         PCPU_INC(cnt.v_tfree);
 2777 
 2778         if (vm_page_sbusied(m))
 2779                 panic("vm_page_free: freeing busy page %p", m);
 2780 
 2781         /*
 2782          * Unqueue, then remove page.  Note that we cannot destroy
 2783          * the page here because we do not want to call the pager's
 2784          * callback routine until after we've put the page on the
 2785          * appropriate free queue.
 2786          */
 2787         vm_page_remque(m);
 2788         vm_page_remove(m);
 2789 
 2790         /*
 2791          * If fictitious remove object association and
 2792          * return, otherwise delay object association removal.
 2793          */
 2794         if ((m->flags & PG_FICTITIOUS) != 0) {
 2795                 return;
 2796         }
 2797 
 2798         m->valid = 0;
 2799         vm_page_undirty(m);
 2800 
 2801         if (m->wire_count != 0)
 2802                 panic("vm_page_free: freeing wired page %p", m);
 2803         if (m->hold_count != 0) {
 2804                 m->flags &= ~PG_ZERO;
 2805                 KASSERT((m->flags & PG_UNHOLDFREE) == 0,
 2806                     ("vm_page_free: freeing PG_UNHOLDFREE page %p", m));
 2807                 m->flags |= PG_UNHOLDFREE;
 2808         } else {
 2809                 /*
 2810                  * Restore the default memory attribute to the page.
 2811                  */
 2812                 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT)
 2813                         pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT);
 2814 
 2815                 /*
 2816                  * Insert the page into the physical memory allocator's free
 2817                  * page queues.
 2818                  */
 2819                 mtx_lock(&vm_page_queue_free_mtx);
 2820                 vm_phys_freecnt_adj(m, 1);
 2821 #if VM_NRESERVLEVEL > 0
 2822                 if (!vm_reserv_free_page(m))
 2823 #else
 2824                 if (TRUE)
 2825 #endif
 2826                         vm_phys_free_pages(m, 0);
 2827                 if ((m->flags & PG_ZERO) != 0)
 2828                         ++vm_page_zero_count;
 2829                 else
 2830                         vm_page_zero_idle_wakeup();
 2831                 vm_page_free_wakeup();
 2832                 mtx_unlock(&vm_page_queue_free_mtx);
 2833         }
 2834 }
 2835 
 2836 /*
 2837  *      vm_page_wire:
 2838  *
 2839  *      Mark this page as wired down by yet
 2840  *      another map, removing it from paging queues
 2841  *      as necessary.
 2842  *
 2843  *      If the page is fictitious, then its wire count must remain one.
 2844  *
 2845  *      The page must be locked.
 2846  */
 2847 void
 2848 vm_page_wire(vm_page_t m)
 2849 {
 2850 
 2851         /*
 2852          * Only bump the wire statistics if the page is not already wired,
 2853          * and only unqueue the page if it is on some queue (if it is unmanaged
 2854          * it is already off the queues).
 2855          */
 2856         vm_page_lock_assert(m, MA_OWNED);
 2857         if ((m->flags & PG_FICTITIOUS) != 0) {
 2858                 KASSERT(m->wire_count == 1,
 2859                     ("vm_page_wire: fictitious page %p's wire count isn't one",
 2860                     m));
 2861                 return;
 2862         }
 2863         if (m->wire_count == 0) {
 2864                 KASSERT((m->oflags & VPO_UNMANAGED) == 0 ||
 2865                     m->queue == PQ_NONE,
 2866                     ("vm_page_wire: unmanaged page %p is queued", m));
 2867                 vm_page_remque(m);
 2868                 atomic_add_int(&vm_cnt.v_wire_count, 1);
 2869         }
 2870         m->wire_count++;
 2871         KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m));
 2872 }
 2873 
 2874 /*
 2875  * vm_page_unwire:
 2876  *
 2877  * Release one wiring of the specified page, potentially allowing it to be
 2878  * paged out.  Returns TRUE if the number of wirings transitions to zero and
 2879  * FALSE otherwise.
 2880  *
 2881  * Only managed pages belonging to an object can be paged out.  If the number
 2882  * of wirings transitions to zero and the page is eligible for page out, then
 2883  * the page is added to the specified paging queue (unless PQ_NONE is
 2884  * specified).
 2885  *
 2886  * If a page is fictitious, then its wire count must always be one.
 2887  *
 2888  * A managed page must be locked.
 2889  */
 2890 boolean_t
 2891 vm_page_unwire(vm_page_t m, uint8_t queue)
 2892 {
 2893 
 2894         KASSERT(queue < PQ_COUNT || queue == PQ_NONE,
 2895             ("vm_page_unwire: invalid queue %u request for page %p",
 2896             queue, m));
 2897         if ((m->oflags & VPO_UNMANAGED) == 0)
 2898                 vm_page_assert_locked(m);
 2899         if ((m->flags & PG_FICTITIOUS) != 0) {
 2900                 KASSERT(m->wire_count == 1,
 2901             ("vm_page_unwire: fictitious page %p's wire count isn't one", m));
 2902                 return (FALSE);
 2903         }
 2904         if (m->wire_count > 0) {
 2905                 m->wire_count--;
 2906                 if (m->wire_count == 0) {
 2907                         atomic_subtract_int(&vm_cnt.v_wire_count, 1);
 2908                         if ((m->oflags & VPO_UNMANAGED) == 0 &&
 2909                             m->object != NULL && queue != PQ_NONE)
 2910                                 vm_page_enqueue(queue, m);
 2911                         return (TRUE);
 2912                 } else
 2913                         return (FALSE);
 2914         } else
 2915                 panic("vm_page_unwire: page %p's wire count is zero", m);
 2916 }
 2917 
 2918 /*
 2919  * Move the specified page to the inactive queue.
 2920  *
 2921  * Normally, "noreuse" is FALSE, resulting in LRU ordering of the inactive
 2922  * queue.  However, setting "noreuse" to TRUE will accelerate the specified
 2923  * page's reclamation, but it will not unmap the page from any address space.
 2924  * This is implemented by inserting the page near the head of the inactive
 2925  * queue, using a marker page to guide FIFO insertion ordering.
 2926  *
 2927  * The page must be locked.
 2928  */
 2929 static inline void
 2930 _vm_page_deactivate(vm_page_t m, boolean_t noreuse)
 2931 {
 2932         struct vm_pagequeue *pq;
 2933         int queue;
 2934 
 2935         vm_page_assert_locked(m);
 2936 
 2937         /*
 2938          * Ignore if the page is already inactive, unless it is unlikely to be
 2939          * reactivated.
 2940          */
 2941         if ((queue = m->queue) == PQ_INACTIVE && !noreuse)
 2942                 return;
 2943         if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) {
 2944                 pq = &vm_phys_domain(m)->vmd_pagequeues[PQ_INACTIVE];
 2945                 /* Avoid multiple acquisitions of the inactive queue lock. */
 2946                 if (queue == PQ_INACTIVE) {
 2947                         vm_pagequeue_lock(pq);
 2948                         vm_page_dequeue_locked(m);
 2949                 } else {
 2950                         if (queue != PQ_NONE)
 2951                                 vm_page_dequeue(m);
 2952                         vm_pagequeue_lock(pq);
 2953                 }
 2954                 m->queue = PQ_INACTIVE;
 2955                 if (noreuse)
 2956                         TAILQ_INSERT_BEFORE(&vm_phys_domain(m)->vmd_inacthead,
 2957                             m, plinks.q);
 2958                 else
 2959                         TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
 2960                 vm_pagequeue_cnt_inc(pq);
 2961                 vm_pagequeue_unlock(pq);
 2962         }
 2963 }
 2964 
 2965 /*
 2966  * Move the specified page to the inactive queue.
 2967  *
 2968  * The page must be locked.
 2969  */
 2970 void
 2971 vm_page_deactivate(vm_page_t m)
 2972 {
 2973 
 2974         _vm_page_deactivate(m, FALSE);
 2975 }
 2976 
 2977 /*
 2978  * Move the specified page to the inactive queue with the expectation
 2979  * that it is unlikely to be reused.
 2980  *
 2981  * The page must be locked.
 2982  */
 2983 void
 2984 vm_page_deactivate_noreuse(vm_page_t m)
 2985 {
 2986 
 2987         _vm_page_deactivate(m, TRUE);
 2988 }
 2989 
 2990 /*
 2991  * vm_page_launder
 2992  *
 2993  *      Put a page in the laundry.
 2994  */
 2995 void
 2996 vm_page_launder(vm_page_t m)
 2997 {
 2998         int queue;
 2999 
 3000         vm_page_assert_locked(m);
 3001         if ((queue = m->queue) != PQ_LAUNDRY) {
 3002                 if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) {
 3003                         if (queue != PQ_NONE)
 3004                                 vm_page_dequeue(m);
 3005                         vm_page_enqueue(PQ_LAUNDRY, m);
 3006                 } else
 3007                         KASSERT(queue == PQ_NONE,
 3008                             ("wired page %p is queued", m));
 3009         }
 3010 }
 3011 
 3012 /*
 3013  * vm_page_try_to_free()
 3014  *
 3015  *      Attempt to free the page.  If we cannot free it, we do nothing.
 3016  *      1 is returned on success, 0 on failure.
 3017  */
 3018 int
 3019 vm_page_try_to_free(vm_page_t m)
 3020 {
 3021 
 3022         vm_page_lock_assert(m, MA_OWNED);
 3023         if (m->object != NULL)
 3024                 VM_OBJECT_ASSERT_WLOCKED(m->object);
 3025         if (m->dirty || m->hold_count || m->wire_count ||
 3026             (m->oflags & VPO_UNMANAGED) != 0 || vm_page_busied(m))
 3027                 return (0);
 3028         pmap_remove_all(m);
 3029         if (m->dirty)
 3030                 return (0);
 3031         vm_page_free(m);
 3032         return (1);
 3033 }
 3034 
 3035 /*
 3036  * vm_page_advise
 3037  *
 3038  *      Deactivate or do nothing, as appropriate.
 3039  *
 3040  *      The object and page must be locked.
 3041  */
 3042 void
 3043 vm_page_advise(vm_page_t m, int advice)
 3044 {
 3045 
 3046         vm_page_assert_locked(m);
 3047         VM_OBJECT_ASSERT_WLOCKED(m->object);
 3048         if (advice == MADV_FREE)
 3049                 /*
 3050                  * Mark the page clean.  This will allow the page to be freed
 3051                  * without first paging it out.  MADV_FREE pages are often
 3052                  * quickly reused by malloc(3), so we do not do anything that
 3053                  * would result in a page fault on a later access.
 3054                  */
 3055                 vm_page_undirty(m);
 3056         else if (advice != MADV_DONTNEED)
 3057                 return;
 3058 
 3059         /*
 3060          * Clear any references to the page.  Otherwise, the page daemon will
 3061          * immediately reactivate the page.
 3062          */
 3063         vm_page_aflag_clear(m, PGA_REFERENCED);
 3064 
 3065         if (advice != MADV_FREE && m->dirty == 0 && pmap_is_modified(m))
 3066                 vm_page_dirty(m);
 3067 
 3068         /*
 3069          * Place clean pages near the head of the inactive queue rather than
 3070          * the tail, thus defeating the queue's LRU operation and ensuring that
 3071          * the page will be reused quickly.  Dirty pages not already in the
 3072          * laundry are moved there.
 3073          */
 3074         if (m->dirty == 0)
 3075                 vm_page_deactivate_noreuse(m);
 3076         else
 3077                 vm_page_launder(m);
 3078 }
 3079 
 3080 /*
 3081  * Grab a page, waiting until we are waken up due to the page
 3082  * changing state.  We keep on waiting, if the page continues
 3083  * to be in the object.  If the page doesn't exist, first allocate it
 3084  * and then conditionally zero it.
 3085  *
 3086  * This routine may sleep.
 3087  *
 3088  * The object must be locked on entry.  The lock will, however, be released
 3089  * and reacquired if the routine sleeps.
 3090  */
 3091 vm_page_t
 3092 vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags)
 3093 {
 3094         vm_page_t m;
 3095         int sleep;
 3096 
 3097         VM_OBJECT_ASSERT_WLOCKED(object);
 3098         KASSERT((allocflags & VM_ALLOC_SBUSY) == 0 ||
 3099             (allocflags & VM_ALLOC_IGN_SBUSY) != 0,
 3100             ("vm_page_grab: VM_ALLOC_SBUSY/VM_ALLOC_IGN_SBUSY mismatch"));
 3101 retrylookup:
 3102         if ((m = vm_page_lookup(object, pindex)) != NULL) {
 3103                 sleep = (allocflags & VM_ALLOC_IGN_SBUSY) != 0 ?
 3104                     vm_page_xbusied(m) : vm_page_busied(m);
 3105                 if (sleep) {
 3106                         if ((allocflags & VM_ALLOC_NOWAIT) != 0)
 3107                                 return (NULL);
 3108                         /*
 3109                          * Reference the page before unlocking and
 3110                          * sleeping so that the page daemon is less
 3111                          * likely to reclaim it.
 3112                          */
 3113                         vm_page_aflag_set(m, PGA_REFERENCED);
 3114                         vm_page_lock(m);
 3115                         VM_OBJECT_WUNLOCK(object);
 3116                         vm_page_busy_sleep(m, "pgrbwt", (allocflags &
 3117                             VM_ALLOC_IGN_SBUSY) != 0);
 3118                         VM_OBJECT_WLOCK(object);
 3119                         goto retrylookup;
 3120                 } else {
 3121                         if ((allocflags & VM_ALLOC_WIRED) != 0) {
 3122                                 vm_page_lock(m);
 3123                                 vm_page_wire(m);
 3124                                 vm_page_unlock(m);
 3125                         }
 3126                         if ((allocflags &
 3127                             (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) == 0)
 3128                                 vm_page_xbusy(m);
 3129                         if ((allocflags & VM_ALLOC_SBUSY) != 0)
 3130                                 vm_page_sbusy(m);
 3131                         return (m);
 3132                 }
 3133         }
 3134         m = vm_page_alloc(object, pindex, allocflags);
 3135         if (m == NULL) {
 3136                 if ((allocflags & VM_ALLOC_NOWAIT) != 0)
 3137                         return (NULL);
 3138                 VM_OBJECT_WUNLOCK(object);
 3139                 VM_WAIT;
 3140                 VM_OBJECT_WLOCK(object);
 3141                 goto retrylookup;
 3142         }
 3143         if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0)
 3144                 pmap_zero_page(m);
 3145         return (m);
 3146 }
 3147 
 3148 /*
 3149  * Mapping function for valid or dirty bits in a page.
 3150  *
 3151  * Inputs are required to range within a page.
 3152  */
 3153 vm_page_bits_t
 3154 vm_page_bits(int base, int size)
 3155 {
 3156         int first_bit;
 3157         int last_bit;
 3158 
 3159         KASSERT(
 3160             base + size <= PAGE_SIZE,
 3161             ("vm_page_bits: illegal base/size %d/%d", base, size)
 3162         );
 3163 
 3164         if (size == 0)          /* handle degenerate case */
 3165                 return (0);
 3166 
 3167         first_bit = base >> DEV_BSHIFT;
 3168         last_bit = (base + size - 1) >> DEV_BSHIFT;
 3169 
 3170         return (((vm_page_bits_t)2 << last_bit) -
 3171             ((vm_page_bits_t)1 << first_bit));
 3172 }
 3173 
 3174 /*
 3175  *      vm_page_set_valid_range:
 3176  *
 3177  *      Sets portions of a page valid.  The arguments are expected
 3178  *      to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
 3179  *      of any partial chunks touched by the range.  The invalid portion of
 3180  *      such chunks will be zeroed.
 3181  *
 3182  *      (base + size) must be less then or equal to PAGE_SIZE.
 3183  */
 3184 void
 3185 vm_page_set_valid_range(vm_page_t m, int base, int size)
 3186 {
 3187         int endoff, frag;
 3188 
 3189         VM_OBJECT_ASSERT_WLOCKED(m->object);
 3190         if (size == 0)  /* handle degenerate case */
 3191                 return;
 3192 
 3193         /*
 3194          * If the base is not DEV_BSIZE aligned and the valid
 3195          * bit is clear, we have to zero out a portion of the
 3196          * first block.
 3197          */
 3198         if ((frag = rounddown2(base, DEV_BSIZE)) != base &&
 3199             (m->valid & (1 << (base >> DEV_BSHIFT))) == 0)
 3200                 pmap_zero_page_area(m, frag, base - frag);
 3201 
 3202         /*
 3203          * If the ending offset is not DEV_BSIZE aligned and the
 3204          * valid bit is clear, we have to zero out a portion of
 3205          * the last block.
 3206          */
 3207         endoff = base + size;
 3208         if ((frag = rounddown2(endoff, DEV_BSIZE)) != endoff &&
 3209             (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0)
 3210                 pmap_zero_page_area(m, endoff,
 3211                     DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
 3212 
 3213         /*
 3214          * Assert that no previously invalid block that is now being validated
 3215          * is already dirty.
 3216          */
 3217         KASSERT((~m->valid & vm_page_bits(base, size) & m->dirty) == 0,
 3218             ("vm_page_set_valid_range: page %p is dirty", m));
 3219 
 3220         /*
 3221          * Set valid bits inclusive of any overlap.
 3222          */
 3223         m->valid |= vm_page_bits(base, size);
 3224 }
 3225 
 3226 /*
 3227  * Clear the given bits from the specified page's dirty field.
 3228  */
 3229 static __inline void
 3230 vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits)
 3231 {
 3232         uintptr_t addr;
 3233 #if PAGE_SIZE < 16384
 3234         int shift;
 3235 #endif
 3236 
 3237         /*
 3238          * If the object is locked and the page is neither exclusive busy nor
 3239          * write mapped, then the page's dirty field cannot possibly be
 3240          * set by a concurrent pmap operation.
 3241          */
 3242         VM_OBJECT_ASSERT_WLOCKED(m->object);
 3243         if (!vm_page_xbusied(m) && !pmap_page_is_write_mapped(m))
 3244                 m->dirty &= ~pagebits;
 3245         else {
 3246                 /*
 3247                  * The pmap layer can call vm_page_dirty() without
 3248                  * holding a distinguished lock.  The combination of
 3249                  * the object's lock and an atomic operation suffice
 3250                  * to guarantee consistency of the page dirty field.
 3251                  *
 3252                  * For PAGE_SIZE == 32768 case, compiler already
 3253                  * properly aligns the dirty field, so no forcible
 3254                  * alignment is needed. Only require existence of
 3255                  * atomic_clear_64 when page size is 32768.
 3256                  */
 3257                 addr = (uintptr_t)&m->dirty;
 3258 #if PAGE_SIZE == 32768
 3259                 atomic_clear_64((uint64_t *)addr, pagebits);
 3260 #elif PAGE_SIZE == 16384
 3261                 atomic_clear_32((uint32_t *)addr, pagebits);
 3262 #else           /* PAGE_SIZE <= 8192 */
 3263                 /*
 3264                  * Use a trick to perform a 32-bit atomic on the
 3265                  * containing aligned word, to not depend on the existence
 3266                  * of atomic_clear_{8, 16}.
 3267                  */
 3268                 shift = addr & (sizeof(uint32_t) - 1);
 3269 #if BYTE_ORDER == BIG_ENDIAN
 3270                 shift = (sizeof(uint32_t) - sizeof(m->dirty) - shift) * NBBY;
 3271 #else
 3272                 shift *= NBBY;
 3273 #endif
 3274                 addr &= ~(sizeof(uint32_t) - 1);
 3275                 atomic_clear_32((uint32_t *)addr, pagebits << shift);
 3276 #endif          /* PAGE_SIZE */
 3277         }
 3278 }
 3279 
 3280 /*
 3281  *      vm_page_set_validclean:
 3282  *
 3283  *      Sets portions of a page valid and clean.  The arguments are expected
 3284  *      to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
 3285  *      of any partial chunks touched by the range.  The invalid portion of
 3286  *      such chunks will be zero'd.
 3287  *
 3288  *      (base + size) must be less then or equal to PAGE_SIZE.
 3289  */
 3290 void
 3291 vm_page_set_validclean(vm_page_t m, int base, int size)
 3292 {
 3293         vm_page_bits_t oldvalid, pagebits;
 3294         int endoff, frag;
 3295 
 3296         VM_OBJECT_ASSERT_WLOCKED(m->object);
 3297         if (size == 0)  /* handle degenerate case */
 3298                 return;
 3299 
 3300         /*
 3301          * If the base is not DEV_BSIZE aligned and the valid
 3302          * bit is clear, we have to zero out a portion of the
 3303          * first block.
 3304          */
 3305         if ((frag = rounddown2(base, DEV_BSIZE)) != base &&
 3306             (m->valid & ((vm_page_bits_t)1 << (base >> DEV_BSHIFT))) == 0)
 3307                 pmap_zero_page_area(m, frag, base - frag);
 3308 
 3309         /*
 3310          * If the ending offset is not DEV_BSIZE aligned and the
 3311          * valid bit is clear, we have to zero out a portion of
 3312          * the last block.
 3313          */
 3314         endoff = base + size;
 3315         if ((frag = rounddown2(endoff, DEV_BSIZE)) != endoff &&
 3316             (m->valid & ((vm_page_bits_t)1 << (endoff >> DEV_BSHIFT))) == 0)
 3317                 pmap_zero_page_area(m, endoff,
 3318                     DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
 3319 
 3320         /*
 3321          * Set valid, clear dirty bits.  If validating the entire
 3322          * page we can safely clear the pmap modify bit.  We also
 3323          * use this opportunity to clear the VPO_NOSYNC flag.  If a process
 3324          * takes a write fault on a MAP_NOSYNC memory area the flag will
 3325          * be set again.
 3326          *
 3327          * We set valid bits inclusive of any overlap, but we can only
 3328          * clear dirty bits for DEV_BSIZE chunks that are fully within
 3329          * the range.
 3330          */
 3331         oldvalid = m->valid;
 3332         pagebits = vm_page_bits(base, size);
 3333         m->valid |= pagebits;
 3334 #if 0   /* NOT YET */
 3335         if ((frag = base & (DEV_BSIZE - 1)) != 0) {
 3336                 frag = DEV_BSIZE - frag;
 3337                 base += frag;
 3338                 size -= frag;
 3339                 if (size < 0)
 3340                         size = 0;
 3341         }
 3342         pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1));
 3343 #endif
 3344         if (base == 0 && size == PAGE_SIZE) {
 3345                 /*
 3346                  * The page can only be modified within the pmap if it is
 3347                  * mapped, and it can only be mapped if it was previously
 3348                  * fully valid.
 3349                  */
 3350                 if (oldvalid == VM_PAGE_BITS_ALL)
 3351                         /*
 3352                          * Perform the pmap_clear_modify() first.  Otherwise,
 3353                          * a concurrent pmap operation, such as
 3354                          * pmap_protect(), could clear a modification in the
 3355                          * pmap and set the dirty field on the page before
 3356                          * pmap_clear_modify() had begun and after the dirty
 3357                          * field was cleared here.
 3358                          */
 3359                         pmap_clear_modify(m);
 3360                 m->dirty = 0;
 3361                 m->oflags &= ~VPO_NOSYNC;
 3362         } else if (oldvalid != VM_PAGE_BITS_ALL)
 3363                 m->dirty &= ~pagebits;
 3364         else
 3365                 vm_page_clear_dirty_mask(m, pagebits);
 3366 }
 3367 
 3368 void
 3369 vm_page_clear_dirty(vm_page_t m, int base, int size)
 3370 {
 3371 
 3372         vm_page_clear_dirty_mask(m, vm_page_bits(base, size));
 3373 }
 3374 
 3375 /*
 3376  *      vm_page_set_invalid:
 3377  *
 3378  *      Invalidates DEV_BSIZE'd chunks within a page.  Both the
 3379  *      valid and dirty bits for the effected areas are cleared.
 3380  */
 3381 void
 3382 vm_page_set_invalid(vm_page_t m, int base, int size)
 3383 {
 3384         vm_page_bits_t bits;
 3385         vm_object_t object;
 3386 
 3387         object = m->object;
 3388         VM_OBJECT_ASSERT_WLOCKED(object);
 3389         if (object->type == OBJT_VNODE && base == 0 && IDX_TO_OFF(m->pindex) +
 3390             size >= object->un_pager.vnp.vnp_size)
 3391                 bits = VM_PAGE_BITS_ALL;
 3392         else
 3393                 bits = vm_page_bits(base, size);
 3394         if (object->ref_count != 0 && m->valid == VM_PAGE_BITS_ALL &&
 3395             bits != 0)
 3396                 pmap_remove_all(m);
 3397         KASSERT((bits == 0 && m->valid == VM_PAGE_BITS_ALL) ||
 3398             !pmap_page_is_mapped(m),
 3399             ("vm_page_set_invalid: page %p is mapped", m));
 3400         m->valid &= ~bits;
 3401         m->dirty &= ~bits;
 3402 }
 3403 
 3404 /*
 3405  * vm_page_zero_invalid()
 3406  *
 3407  *      The kernel assumes that the invalid portions of a page contain
 3408  *      garbage, but such pages can be mapped into memory by user code.
 3409  *      When this occurs, we must zero out the non-valid portions of the
 3410  *      page so user code sees what it expects.
 3411  *
 3412  *      Pages are most often semi-valid when the end of a file is mapped
 3413  *      into memory and the file's size is not page aligned.
 3414  */
 3415 void
 3416 vm_page_zero_invalid(vm_page_t m, boolean_t setvalid)
 3417 {
 3418         int b;
 3419         int i;
 3420 
 3421         VM_OBJECT_ASSERT_WLOCKED(m->object);
 3422         /*
 3423          * Scan the valid bits looking for invalid sections that
 3424          * must be zeroed.  Invalid sub-DEV_BSIZE'd areas ( where the
 3425          * valid bit may be set ) have already been zeroed by
 3426          * vm_page_set_validclean().
 3427          */
 3428         for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) {
 3429                 if (i == (PAGE_SIZE / DEV_BSIZE) ||
 3430                     (m->valid & ((vm_page_bits_t)1 << i))) {
 3431                         if (i > b) {
 3432                                 pmap_zero_page_area(m,
 3433                                     b << DEV_BSHIFT, (i - b) << DEV_BSHIFT);
 3434                         }
 3435                         b = i + 1;
 3436                 }
 3437         }
 3438 
 3439         /*
 3440          * setvalid is TRUE when we can safely set the zero'd areas
 3441          * as being valid.  We can do this if there are no cache consistancy
 3442          * issues.  e.g. it is ok to do with UFS, but not ok to do with NFS.
 3443          */
 3444         if (setvalid)
 3445                 m->valid = VM_PAGE_BITS_ALL;
 3446 }
 3447 
 3448 /*
 3449  *      vm_page_is_valid:
 3450  *
 3451  *      Is (partial) page valid?  Note that the case where size == 0
 3452  *      will return FALSE in the degenerate case where the page is
 3453  *      entirely invalid, and TRUE otherwise.
 3454  */
 3455 int
 3456 vm_page_is_valid(vm_page_t m, int base, int size)
 3457 {
 3458         vm_page_bits_t bits;
 3459 
 3460         VM_OBJECT_ASSERT_LOCKED(m->object);
 3461         bits = vm_page_bits(base, size);
 3462         return (m->valid != 0 && (m->valid & bits) == bits);
 3463 }
 3464 
 3465 /*
 3466  *      vm_page_ps_is_valid:
 3467  *
 3468  *      Returns TRUE if the entire (super)page is valid and FALSE otherwise.
 3469  */
 3470 boolean_t
 3471 vm_page_ps_is_valid(vm_page_t m)
 3472 {
 3473         int i, npages;
 3474 
 3475         VM_OBJECT_ASSERT_LOCKED(m->object);
 3476         npages = atop(pagesizes[m->psind]);
 3477 
 3478         /*
 3479          * The physically contiguous pages that make up a superpage, i.e., a
 3480          * page with a page size index ("psind") greater than zero, will
 3481          * occupy adjacent entries in vm_page_array[].
 3482          */
 3483         for (i = 0; i < npages; i++) {
 3484                 if (m[i].valid != VM_PAGE_BITS_ALL)
 3485                         return (FALSE);
 3486         }
 3487         return (TRUE);
 3488 }
 3489 
 3490 /*
 3491  * Set the page's dirty bits if the page is modified.
 3492  */
 3493 void
 3494 vm_page_test_dirty(vm_page_t m)
 3495 {
 3496 
 3497         VM_OBJECT_ASSERT_WLOCKED(m->object);
 3498         if (m->dirty != VM_PAGE_BITS_ALL && pmap_is_modified(m))
 3499                 vm_page_dirty(m);
 3500 }
 3501 
 3502 void
 3503 vm_page_lock_KBI(vm_page_t m, const char *file, int line)
 3504 {
 3505 
 3506         mtx_lock_flags_(vm_page_lockptr(m), 0, file, line);
 3507 }
 3508 
 3509 void
 3510 vm_page_unlock_KBI(vm_page_t m, const char *file, int line)
 3511 {
 3512 
 3513         mtx_unlock_flags_(vm_page_lockptr(m), 0, file, line);
 3514 }
 3515 
 3516 int
 3517 vm_page_trylock_KBI(vm_page_t m, const char *file, int line)
 3518 {
 3519 
 3520         return (mtx_trylock_flags_(vm_page_lockptr(m), 0, file, line));
 3521 }
 3522 
 3523 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
 3524 void
 3525 vm_page_assert_locked_KBI(vm_page_t m, const char *file, int line)
 3526 {
 3527 
 3528         vm_page_lock_assert_KBI(m, MA_OWNED, file, line);
 3529 }
 3530 
 3531 void
 3532 vm_page_lock_assert_KBI(vm_page_t m, int a, const char *file, int line)
 3533 {
 3534 
 3535         mtx_assert_(vm_page_lockptr(m), a, file, line);
 3536 }
 3537 #endif
 3538 
 3539 #ifdef INVARIANTS
 3540 void
 3541 vm_page_object_lock_assert(vm_page_t m)
 3542 {
 3543 
 3544         /*
 3545          * Certain of the page's fields may only be modified by the
 3546          * holder of the containing object's lock or the exclusive busy.
 3547          * holder.  Unfortunately, the holder of the write busy is
 3548          * not recorded, and thus cannot be checked here.
 3549          */
 3550         if (m->object != NULL && !vm_page_xbusied(m))
 3551                 VM_OBJECT_ASSERT_WLOCKED(m->object);
 3552 }
 3553 
 3554 void
 3555 vm_page_assert_pga_writeable(vm_page_t m, uint8_t bits)
 3556 {
 3557 
 3558         if ((bits & PGA_WRITEABLE) == 0)
 3559                 return;
 3560 
 3561         /*
 3562          * The PGA_WRITEABLE flag can only be set if the page is
 3563          * managed, is exclusively busied or the object is locked.
 3564          * Currently, this flag is only set by pmap_enter().
 3565          */
 3566         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
 3567             ("PGA_WRITEABLE on unmanaged page"));
 3568         if (!vm_page_xbusied(m))
 3569                 VM_OBJECT_ASSERT_LOCKED(m->object);
 3570 }
 3571 #endif
 3572 
 3573 #include "opt_ddb.h"
 3574 #ifdef DDB
 3575 #include <sys/kernel.h>
 3576 
 3577 #include <ddb/ddb.h>
 3578 
 3579 DB_SHOW_COMMAND(page, vm_page_print_page_info)
 3580 {
 3581 
 3582         db_printf("vm_cnt.v_free_count: %d\n", vm_cnt.v_free_count);
 3583         db_printf("vm_cnt.v_inactive_count: %d\n", vm_cnt.v_inactive_count);
 3584         db_printf("vm_cnt.v_active_count: %d\n", vm_cnt.v_active_count);
 3585         db_printf("vm_cnt.v_laundry_count: %d\n", vm_cnt.v_laundry_count);
 3586         db_printf("vm_cnt.v_wire_count: %d\n", vm_cnt.v_wire_count);
 3587         db_printf("vm_cnt.v_free_reserved: %d\n", vm_cnt.v_free_reserved);
 3588         db_printf("vm_cnt.v_free_min: %d\n", vm_cnt.v_free_min);
 3589         db_printf("vm_cnt.v_free_target: %d\n", vm_cnt.v_free_target);
 3590         db_printf("vm_cnt.v_inactive_target: %d\n", vm_cnt.v_inactive_target);
 3591 }
 3592 
 3593 DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info)
 3594 {
 3595         int dom;
 3596 
 3597         db_printf("pq_free %d\n", vm_cnt.v_free_count);
 3598         for (dom = 0; dom < vm_ndomains; dom++) {
 3599                 db_printf(
 3600             "dom %d page_cnt %d free %d pq_act %d pq_inact %d pq_laund %d\n",
 3601                     dom,
 3602                     vm_dom[dom].vmd_page_count,
 3603                     vm_dom[dom].vmd_free_count,
 3604                     vm_dom[dom].vmd_pagequeues[PQ_ACTIVE].pq_cnt,
 3605                     vm_dom[dom].vmd_pagequeues[PQ_INACTIVE].pq_cnt,
 3606                     vm_dom[dom].vmd_pagequeues[PQ_LAUNDRY].pq_cnt);
 3607         }
 3608 }
 3609 
 3610 DB_SHOW_COMMAND(pginfo, vm_page_print_pginfo)
 3611 {
 3612         vm_page_t m;
 3613         boolean_t phys;
 3614 
 3615         if (!have_addr) {
 3616                 db_printf("show pginfo addr\n");
 3617                 return;
 3618         }
 3619 
 3620         phys = strchr(modif, 'p') != NULL;
 3621         if (phys)
 3622                 m = PHYS_TO_VM_PAGE(addr);
 3623         else
 3624                 m = (vm_page_t)addr;
 3625         db_printf(
 3626     "page %p obj %p pidx 0x%jx phys 0x%jx q %d hold %d wire %d\n"
 3627     "  af 0x%x of 0x%x f 0x%x act %d busy %x valid 0x%x dirty 0x%x\n",
 3628             m, m->object, (uintmax_t)m->pindex, (uintmax_t)m->phys_addr,
 3629             m->queue, m->hold_count, m->wire_count, m->aflags, m->oflags,
 3630             m->flags, m->act_count, m->busy_lock, m->valid, m->dirty);
 3631 }
 3632 #endif /* DDB */

Cache object: 4bbc3e0a4ea638976999fd56ff299a3c


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.