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