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