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