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