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