vm_page.c revision 128614
11556Srgrimes/* 21556Srgrimes * Copyright (c) 1991 Regents of the University of California. 31556Srgrimes * All rights reserved. 41556Srgrimes * 51556Srgrimes * This code is derived from software contributed to Berkeley by 61556Srgrimes * The Mach Operating System project at Carnegie-Mellon University. 71556Srgrimes * 81556Srgrimes * Redistribution and use in source and binary forms, with or without 91556Srgrimes * modification, are permitted provided that the following conditions 101556Srgrimes * are met: 111556Srgrimes * 1. Redistributions of source code must retain the above copyright 121556Srgrimes * notice, this list of conditions and the following disclaimer. 131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141556Srgrimes * notice, this list of conditions and the following disclaimer in the 151556Srgrimes * documentation and/or other materials provided with the distribution. 161556Srgrimes * 4. Neither the name of the University nor the names of its contributors 171556Srgrimes * may be used to endorse or promote products derived from this software 181556Srgrimes * without specific prior written permission. 191556Srgrimes * 201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301556Srgrimes * SUCH DAMAGE. 311556Srgrimes * 321556Srgrimes * from: @(#)vm_page.c 7.4 (Berkeley) 5/7/91 331556Srgrimes */ 341556Srgrimes 351556Srgrimes/* 361556Srgrimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 371556Srgrimes * All rights reserved. 3820420Ssteve * 391556Srgrimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 401556Srgrimes * 411556Srgrimes * Permission to use, copy, modify and distribute this software and 421556Srgrimes * its documentation is hereby granted, provided that both the copyright 431556Srgrimes * notice and this permission notice appear in all copies of the 4436049Scharnier * software, derivative works or modified versions, and any portions 4536049Scharnier * thereof, and that both notices appear in supporting documentation. 4636049Scharnier * 4736049Scharnier * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 4836049Scharnier * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 491556Srgrimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 501556Srgrimes * 511556Srgrimes * Carnegie Mellon requests users of this software to return to 521556Srgrimes * 531556Srgrimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 541556Srgrimes * School of Computer Science 5531664Seivind * Carnegie Mellon University 561556Srgrimes * Pittsburgh PA 15213-3890 571556Srgrimes * 581556Srgrimes * any improvements or extensions that they make and grant Carnegie the 591556Srgrimes * rights to redistribute these changes. 601556Srgrimes */ 611556Srgrimes 621556Srgrimes/* 631556Srgrimes * GENERAL RULES ON VM_PAGE MANIPULATION 641556Srgrimes * 651556Srgrimes * - a pageq mutex is required when adding or removing a page from a 661556Srgrimes * page queue (vm_page_queue[]), regardless of other mutexes or the 671556Srgrimes * busy state of a page. 681556Srgrimes * 691556Srgrimes * - a hash chain mutex is required when associating or disassociating 701556Srgrimes * a page from the VM PAGE CACHE hash table (vm_page_buckets), 711556Srgrimes * regardless of other mutexes or the busy state of a page. 721556Srgrimes * 731556Srgrimes * - either a hash chain mutex OR a busied page is required in order 741556Srgrimes * to modify the page flags. A hash chain mutex must be obtained in 751556Srgrimes * order to busy a page. A page's flags cannot be modified by a 761556Srgrimes * hash chain mutex if the page is marked busy. 771556Srgrimes * 781556Srgrimes * - The object memq mutex is held when inserting or removing 791556Srgrimes * pages from an object (vm_page_insert() or vm_page_remove()). This 801556Srgrimes * is different from the object's main mutex. 811556Srgrimes * 821556Srgrimes * Generally speaking, you have to be aware of side effects when running 831556Srgrimes * vm_page ops. A vm_page_lookup() will return with the hash chain 841556Srgrimes * locked, whether it was able to lookup the page or not. vm_page_free(), 8524348Simp * vm_page_cache(), vm_page_activate(), and a number of other routines 861556Srgrimes * will release the hash chain mutex for you. Intermediate manipulation 871556Srgrimes * routines such as vm_page_flag_set() expect the hash chain to be held 8814154Swosch * on entry and the hash chain will remain held on return. 8914166Swosch * 901556Srgrimes * pageq scanning can only occur with the pageq in question locked. 911556Srgrimes * We have a known bottleneck with the active queue, but the cache 921556Srgrimes * and free queues are actually arrays already. 9314166Swosch */ 941556Srgrimes 951556Srgrimes/* 961556Srgrimes * Resident memory management module. 971556Srgrimes */ 9814305Swosch 991556Srgrimes#include <sys/cdefs.h> 1001556Srgrimes__FBSDID("$FreeBSD: head/sys/vm/vm_page.c 128614 2004-04-24 21:36:23Z alc $"); 1011556Srgrimes 1021556Srgrimes#include <sys/param.h> 1031556Srgrimes#include <sys/systm.h> 1041556Srgrimes#include <sys/lock.h> 1051556Srgrimes#include <sys/malloc.h> 1061556Srgrimes#include <sys/mutex.h> 1071556Srgrimes#include <sys/proc.h> 1081556Srgrimes#include <sys/vmmeter.h> 1091556Srgrimes#include <sys/vnode.h> 1101556Srgrimes 1111556Srgrimes#include <vm/vm.h> 1121556Srgrimes#include <vm/vm_param.h> 1131556Srgrimes#include <vm/vm_kern.h> 1141556Srgrimes#include <vm/vm_object.h> 1151556Srgrimes#include <vm/vm_page.h> 1161556Srgrimes#include <vm/vm_pageout.h> 1171556Srgrimes#include <vm/vm_pager.h> 1181556Srgrimes#include <vm/vm_extern.h> 1191556Srgrimes#include <vm/uma.h> 1201556Srgrimes#include <vm/uma_int.h> 12111298Sbde 12211298Sbde/* 12311298Sbde * Associated with page of user-allocatable memory is a 12411298Sbde * page structure. 12511298Sbde */ 12611298Sbde 12711298Sbdestruct mtx vm_page_queue_mtx; 12811298Sbdestruct mtx vm_page_queue_free_mtx; 12911298Sbde 13011298Sbdevm_page_t vm_page_array = 0; 1311556Srgrimesint vm_page_array_size = 0; 1321556Srgrimeslong first_page = 0; 1331556Srgrimesint vm_page_zero_count = 0; 1341556Srgrimes 1351556Srgrimes/* 1361556Srgrimes * vm_set_page_size: 1371556Srgrimes * 1381556Srgrimes * Sets the page size, perhaps based upon the memory 1391556Srgrimes * size. Must be called before any use of page-size 1401556Srgrimes * dependent functions. 1411556Srgrimes */ 1421556Srgrimesvoid 1431556Srgrimesvm_set_page_size(void) 1441556Srgrimes{ 1451556Srgrimes if (cnt.v_page_size == 0) 1461556Srgrimes cnt.v_page_size = PAGE_SIZE; 1471556Srgrimes if (((cnt.v_page_size - 1) & cnt.v_page_size) != 0) 14829933Swosch panic("vm_set_page_size: page size not a power of two"); 1491556Srgrimes} 1501556Srgrimes 1511556Srgrimes/* 1521556Srgrimes * vm_page_startup: 1531556Srgrimes * 1541556Srgrimes * Initializes the resident memory module. 1551556Srgrimes * 1561556Srgrimes * Allocates memory for the page cells, and 15714166Swosch * for the object/offset-to-page hash table headers. 15814166Swosch * Each page cell is initialized and placed on the free list. 15914166Swosch */ 16014305Swoschvm_offset_t 16114305Swoschvm_page_startup(vm_offset_t vaddr) 16214166Swosch{ 16330106Swosch vm_offset_t mapped; 16430106Swosch vm_size_t npages; 1651556Srgrimes vm_paddr_t page_range; 1661556Srgrimes vm_paddr_t new_end; 16730106Swosch int i; 1681556Srgrimes vm_paddr_t pa; 1691556Srgrimes int nblocks; 1701556Srgrimes vm_paddr_t last_pa; 17130106Swosch 1721556Srgrimes /* the biggest memory array is the second group of pages */ 1731556Srgrimes vm_paddr_t end; 17430106Swosch vm_paddr_t biggestsize; 1751556Srgrimes int biggestone; 1761556Srgrimes 1771556Srgrimes vm_paddr_t total; 17829933Swosch vm_size_t bootpages; 17929933Swosch 18029933Swosch total = 0; 18130106Swosch biggestsize = 0; 18230106Swosch biggestone = 0; 1831556Srgrimes nblocks = 0; 18430106Swosch vaddr = round_page(vaddr); 1851556Srgrimes 1861556Srgrimes for (i = 0; phys_avail[i + 1]; i += 2) { 1871556Srgrimes phys_avail[i] = round_page(phys_avail[i]); 1881556Srgrimes phys_avail[i + 1] = trunc_page(phys_avail[i + 1]); 1891556Srgrimes } 19031664Seivind 19131664Seivind for (i = 0; phys_avail[i + 1]; i += 2) { 19231664Seivind vm_paddr_t size = phys_avail[i + 1] - phys_avail[i]; 19331664Seivind 19431664Seivind if (size > biggestsize) { 19531664Seivind biggestone = i; 19631664Seivind biggestsize = size; 19731664Seivind } 19831664Seivind ++nblocks; 19931664Seivind total += size; 20031664Seivind } 20131664Seivind 20231664Seivind end = phys_avail[biggestone+1]; 20331664Seivind 2041556Srgrimes /* 2051556Srgrimes * Initialize the locks. 2061556Srgrimes */ 2071556Srgrimes mtx_init(&vm_page_queue_mtx, "vm page queue mutex", NULL, MTX_DEF); 2081556Srgrimes mtx_init(&vm_page_queue_free_mtx, "vm page queue free mutex", NULL, 2091556Srgrimes MTX_SPIN); 2101556Srgrimes 2111556Srgrimes /* 2121556Srgrimes * Initialize the queue headers for the free queue, the active queue 2131556Srgrimes * and the inactive queue. 2141556Srgrimes */ 2151556Srgrimes vm_pageq_init(); 2161556Srgrimes 2171556Srgrimes /* 2181556Srgrimes * Allocate memory for use when boot strapping the kernel memory 2191556Srgrimes * allocator. 2201556Srgrimes */ 2211556Srgrimes bootpages = UMA_BOOT_PAGES * UMA_SLAB_SIZE; 2221556Srgrimes new_end = end - bootpages; 2231556Srgrimes new_end = trunc_page(new_end); 2241556Srgrimes mapped = pmap_map(&vaddr, new_end, end, 2251556Srgrimes VM_PROT_READ | VM_PROT_WRITE); 2261556Srgrimes bzero((caddr_t) mapped, end - new_end); 2271556Srgrimes uma_startup((caddr_t)mapped); 2281556Srgrimes 22923525Sguido /* 2301556Srgrimes * Compute the number of pages of memory that will be available for 2311556Srgrimes * use (taking into account the overhead of a page structure per 2321556Srgrimes * page). 2331556Srgrimes */ 2341556Srgrimes first_page = phys_avail[0] / PAGE_SIZE; 2351556Srgrimes page_range = phys_avail[(nblocks - 1) * 2 + 1] / PAGE_SIZE - first_page; 23623525Sguido npages = (total - (page_range * sizeof(struct vm_page)) - 23723525Sguido (end - new_end)) / PAGE_SIZE; 23823525Sguido end = new_end; 23923525Sguido 24023525Sguido /* 24123525Sguido * Reserve an unmapped guard page to trap access to vm_page_array[-1]. 24223525Sguido */ 24323525Sguido vaddr += PAGE_SIZE; 24423525Sguido 24523525Sguido /* 24623525Sguido * Initialize the mem entry structures now, and put them in the free 24723525Sguido * queue. 24823525Sguido */ 24923525Sguido new_end = trunc_page(end - page_range * sizeof(struct vm_page)); 2501556Srgrimes mapped = pmap_map(&vaddr, new_end, end, 2511556Srgrimes VM_PROT_READ | VM_PROT_WRITE); 2521556Srgrimes vm_page_array = (vm_page_t) mapped; 2531556Srgrimes phys_avail[biggestone + 1] = new_end; 2541556Srgrimes 2551556Srgrimes /* 2561556Srgrimes * Clear all of the page structures 2571556Srgrimes */ 2581556Srgrimes bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page)); 2591556Srgrimes vm_page_array_size = page_range; 2601556Srgrimes 2611556Srgrimes /* 2621556Srgrimes * Construct the free queue(s) in descending order (by physical 2631556Srgrimes * address) so that the first 16MB of physical memory is allocated 2641556Srgrimes * last rather than first. On large-memory machines, this avoids 2651556Srgrimes * the exhaustion of low physical memory before isa_dmainit has run. 2661556Srgrimes */ 2671556Srgrimes cnt.v_page_count = 0; 2681556Srgrimes cnt.v_free_count = 0; 26923525Sguido for (i = 0; phys_avail[i + 1] && npages > 0; i += 2) { 27023525Sguido pa = phys_avail[i]; 27123525Sguido last_pa = phys_avail[i + 1]; 27223525Sguido while (pa < last_pa && npages-- > 0) { 27323525Sguido vm_pageq_add_new_page(pa); 27423525Sguido pa += PAGE_SIZE; 27523525Sguido } 27623525Sguido } 27723525Sguido return (vaddr); 27823525Sguido} 27923525Sguido 2801556Srgrimesvoid 28123525Sguidovm_page_flag_set(vm_page_t m, unsigned short bits) 2821556Srgrimes{ 2831556Srgrimes 2841556Srgrimes mtx_assert(&vm_page_queue_mtx, MA_OWNED); 2851556Srgrimes m->flags |= bits; 2861556Srgrimes} 2871556Srgrimes 2881556Srgrimesvoid 2891556Srgrimesvm_page_flag_clear(vm_page_t m, unsigned short bits) 2901556Srgrimes{ 2911556Srgrimes 2921556Srgrimes mtx_assert(&vm_page_queue_mtx, MA_OWNED); 2931556Srgrimes m->flags &= ~bits; 2941556Srgrimes} 2951556Srgrimes 2961556Srgrimesvoid 2971556Srgrimesvm_page_busy(vm_page_t m) 2981556Srgrimes{ 2991556Srgrimes KASSERT((m->flags & PG_BUSY) == 0, 3001556Srgrimes ("vm_page_busy: page already busy!!!")); 3011556Srgrimes vm_page_flag_set(m, PG_BUSY); 3021556Srgrimes} 3031556Srgrimes 3041556Srgrimes/* 3051556Srgrimes * vm_page_flash: 3061556Srgrimes * 3071556Srgrimes * wakeup anyone waiting for the page. 3081556Srgrimes */ 3091556Srgrimesvoid 3101556Srgrimesvm_page_flash(vm_page_t m) 3111556Srgrimes{ 3121556Srgrimes if (m->flags & PG_WANTED) { 3131556Srgrimes vm_page_flag_clear(m, PG_WANTED); 3141556Srgrimes wakeup(m); 3151556Srgrimes } 3161556Srgrimes} 3171556Srgrimes 3181556Srgrimes/* 3191556Srgrimes * vm_page_wakeup: 3201556Srgrimes * 3211556Srgrimes * clear the PG_BUSY flag and wakeup anyone waiting for the 3221556Srgrimes * page. 3231556Srgrimes * 3241556Srgrimes */ 3251556Srgrimesvoid 3261556Srgrimesvm_page_wakeup(vm_page_t m) 3271556Srgrimes{ 3281556Srgrimes KASSERT(m->flags & PG_BUSY, ("vm_page_wakeup: page not busy!!!")); 3291556Srgrimes vm_page_flag_clear(m, PG_BUSY); 3301556Srgrimes vm_page_flash(m); 3311556Srgrimes} 3321556Srgrimes 3331556Srgrimesvoid 3341556Srgrimesvm_page_io_start(vm_page_t m) 3351556Srgrimes{ 3361556Srgrimes 3371556Srgrimes mtx_assert(&vm_page_queue_mtx, MA_OWNED); 3381556Srgrimes m->busy++; 3391556Srgrimes} 3401556Srgrimes 3411556Srgrimesvoid 3421556Srgrimesvm_page_io_finish(vm_page_t m) 3431556Srgrimes{ 3441556Srgrimes 3451556Srgrimes mtx_assert(&vm_page_queue_mtx, MA_OWNED); 3461556Srgrimes m->busy--; 3471556Srgrimes if (m->busy == 0) 3481556Srgrimes vm_page_flash(m); 34914305Swosch} 35030727Shelbig 35130727Shelbig/* 3521556Srgrimes * Keep page from being freed by the page daemon 3531556Srgrimes * much of the same effect as wiring, except much lower 354 * overhead and should be used only for *very* temporary 355 * holding ("wiring"). 356 */ 357void 358vm_page_hold(vm_page_t mem) 359{ 360 361 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 362 mem->hold_count++; 363} 364 365void 366vm_page_unhold(vm_page_t mem) 367{ 368 369 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 370 --mem->hold_count; 371 KASSERT(mem->hold_count >= 0, ("vm_page_unhold: hold count < 0!!!")); 372 if (mem->hold_count == 0 && mem->queue == PQ_HOLD) 373 vm_page_free_toq(mem); 374} 375 376/* 377 * vm_page_free: 378 * 379 * Free a page 380 * 381 * The clearing of PG_ZERO is a temporary safety until the code can be 382 * reviewed to determine that PG_ZERO is being properly cleared on 383 * write faults or maps. PG_ZERO was previously cleared in 384 * vm_page_alloc(). 385 */ 386void 387vm_page_free(vm_page_t m) 388{ 389 vm_page_flag_clear(m, PG_ZERO); 390 vm_page_free_toq(m); 391 vm_page_zero_idle_wakeup(); 392} 393 394/* 395 * vm_page_free_zero: 396 * 397 * Free a page to the zerod-pages queue 398 */ 399void 400vm_page_free_zero(vm_page_t m) 401{ 402 vm_page_flag_set(m, PG_ZERO); 403 vm_page_free_toq(m); 404} 405 406/* 407 * vm_page_sleep_if_busy: 408 * 409 * Sleep and release the page queues lock if PG_BUSY is set or, 410 * if also_m_busy is TRUE, busy is non-zero. Returns TRUE if the 411 * thread slept and the page queues lock was released. 412 * Otherwise, retains the page queues lock and returns FALSE. 413 */ 414int 415vm_page_sleep_if_busy(vm_page_t m, int also_m_busy, const char *msg) 416{ 417 int is_object_locked; 418 419 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 420 if ((m->flags & PG_BUSY) || (also_m_busy && m->busy)) { 421 vm_page_flag_set(m, PG_WANTED | PG_REFERENCED); 422 /* 423 * Remove mtx_owned() after vm_object locking is finished. 424 */ 425 if ((is_object_locked = m->object != NULL && 426 mtx_owned(&m->object->mtx))) 427 mtx_unlock(&m->object->mtx); 428 msleep(m, &vm_page_queue_mtx, PDROP | PVM, msg, 0); 429 if (is_object_locked) 430 mtx_lock(&m->object->mtx); 431 return (TRUE); 432 } 433 return (FALSE); 434} 435 436/* 437 * vm_page_dirty: 438 * 439 * make page all dirty 440 */ 441void 442vm_page_dirty(vm_page_t m) 443{ 444 KASSERT(m->queue - m->pc != PQ_CACHE, 445 ("vm_page_dirty: page in cache!")); 446 KASSERT(m->queue - m->pc != PQ_FREE, 447 ("vm_page_dirty: page is free!")); 448 m->dirty = VM_PAGE_BITS_ALL; 449} 450 451/* 452 * vm_page_splay: 453 * 454 * Implements Sleator and Tarjan's top-down splay algorithm. Returns 455 * the vm_page containing the given pindex. If, however, that 456 * pindex is not found in the vm_object, returns a vm_page that is 457 * adjacent to the pindex, coming before or after it. 458 */ 459vm_page_t 460vm_page_splay(vm_pindex_t pindex, vm_page_t root) 461{ 462 struct vm_page dummy; 463 vm_page_t lefttreemax, righttreemin, y; 464 465 if (root == NULL) 466 return (root); 467 lefttreemax = righttreemin = &dummy; 468 for (;; root = y) { 469 if (pindex < root->pindex) { 470 if ((y = root->left) == NULL) 471 break; 472 if (pindex < y->pindex) { 473 /* Rotate right. */ 474 root->left = y->right; 475 y->right = root; 476 root = y; 477 if ((y = root->left) == NULL) 478 break; 479 } 480 /* Link into the new root's right tree. */ 481 righttreemin->left = root; 482 righttreemin = root; 483 } else if (pindex > root->pindex) { 484 if ((y = root->right) == NULL) 485 break; 486 if (pindex > y->pindex) { 487 /* Rotate left. */ 488 root->right = y->left; 489 y->left = root; 490 root = y; 491 if ((y = root->right) == NULL) 492 break; 493 } 494 /* Link into the new root's left tree. */ 495 lefttreemax->right = root; 496 lefttreemax = root; 497 } else 498 break; 499 } 500 /* Assemble the new root. */ 501 lefttreemax->right = root->left; 502 righttreemin->left = root->right; 503 root->left = dummy.right; 504 root->right = dummy.left; 505 return (root); 506} 507 508/* 509 * vm_page_insert: [ internal use only ] 510 * 511 * Inserts the given mem entry into the object and object list. 512 * 513 * The pagetables are not updated but will presumably fault the page 514 * in if necessary, or if a kernel page the caller will at some point 515 * enter the page into the kernel's pmap. We are not allowed to block 516 * here so we *can't* do this anyway. 517 * 518 * The object and page must be locked, and must be splhigh. 519 * This routine may not block. 520 */ 521void 522vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex) 523{ 524 vm_page_t root; 525 526 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 527 if (m->object != NULL) 528 panic("vm_page_insert: page already inserted"); 529 530 /* 531 * Record the object/offset pair in this page 532 */ 533 m->object = object; 534 m->pindex = pindex; 535 536 /* 537 * Now link into the object's ordered list of backed pages. 538 */ 539 root = object->root; 540 if (root == NULL) { 541 m->left = NULL; 542 m->right = NULL; 543 TAILQ_INSERT_TAIL(&object->memq, m, listq); 544 } else { 545 root = vm_page_splay(pindex, root); 546 if (pindex < root->pindex) { 547 m->left = root->left; 548 m->right = root; 549 root->left = NULL; 550 TAILQ_INSERT_BEFORE(root, m, listq); 551 } else if (pindex == root->pindex) 552 panic("vm_page_insert: offset already allocated"); 553 else { 554 m->right = root->right; 555 m->left = root; 556 root->right = NULL; 557 TAILQ_INSERT_AFTER(&object->memq, root, m, listq); 558 } 559 } 560 object->root = m; 561 object->generation++; 562 563 /* 564 * show that the object has one more resident page. 565 */ 566 object->resident_page_count++; 567 568 /* 569 * Since we are inserting a new and possibly dirty page, 570 * update the object's OBJ_WRITEABLE and OBJ_MIGHTBEDIRTY flags. 571 */ 572 if (m->flags & PG_WRITEABLE) 573 vm_object_set_writeable_dirty(object); 574} 575 576/* 577 * vm_page_remove: 578 * NOTE: used by device pager as well -wfj 579 * 580 * Removes the given mem entry from the object/offset-page 581 * table and the object page list, but do not invalidate/terminate 582 * the backing store. 583 * 584 * The object and page must be locked, and at splhigh. 585 * The underlying pmap entry (if any) is NOT removed here. 586 * This routine may not block. 587 */ 588void 589vm_page_remove(vm_page_t m) 590{ 591 vm_object_t object; 592 vm_page_t root; 593 594 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 595 if (m->object == NULL) 596 return; 597#ifndef __alpha__ 598 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 599#endif 600 if ((m->flags & PG_BUSY) == 0) { 601 panic("vm_page_remove: page not busy"); 602 } 603 604 /* 605 * Basically destroy the page. 606 */ 607 vm_page_wakeup(m); 608 609 object = m->object; 610 611 /* 612 * Now remove from the object's list of backed pages. 613 */ 614 if (m != object->root) 615 vm_page_splay(m->pindex, object->root); 616 if (m->left == NULL) 617 root = m->right; 618 else { 619 root = vm_page_splay(m->pindex, m->left); 620 root->right = m->right; 621 } 622 object->root = root; 623 TAILQ_REMOVE(&object->memq, m, listq); 624 625 /* 626 * And show that the object has one fewer resident page. 627 */ 628 object->resident_page_count--; 629 object->generation++; 630 631 m->object = NULL; 632} 633 634/* 635 * vm_page_lookup: 636 * 637 * Returns the page associated with the object/offset 638 * pair specified; if none is found, NULL is returned. 639 * 640 * The object must be locked. 641 * This routine may not block. 642 * This is a critical path routine 643 */ 644vm_page_t 645vm_page_lookup(vm_object_t object, vm_pindex_t pindex) 646{ 647 vm_page_t m; 648 649 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 650 if ((m = object->root) != NULL && m->pindex != pindex) { 651 m = vm_page_splay(pindex, m); 652 if ((object->root = m)->pindex != pindex) 653 m = NULL; 654 } 655 return (m); 656} 657 658/* 659 * vm_page_rename: 660 * 661 * Move the given memory entry from its 662 * current object to the specified target object/offset. 663 * 664 * The object must be locked. 665 * This routine may not block. 666 * 667 * Note: this routine will raise itself to splvm(), the caller need not. 668 * 669 * Note: swap associated with the page must be invalidated by the move. We 670 * have to do this for several reasons: (1) we aren't freeing the 671 * page, (2) we are dirtying the page, (3) the VM system is probably 672 * moving the page from object A to B, and will then later move 673 * the backing store from A to B and we can't have a conflict. 674 * 675 * Note: we *always* dirty the page. It is necessary both for the 676 * fact that we moved it, and because we may be invalidating 677 * swap. If the page is on the cache, we have to deactivate it 678 * or vm_page_dirty() will panic. Dirty pages are not allowed 679 * on the cache. 680 */ 681void 682vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex) 683{ 684 int s; 685 686 s = splvm(); 687 vm_page_remove(m); 688 vm_page_insert(m, new_object, new_pindex); 689 if (m->queue - m->pc == PQ_CACHE) 690 vm_page_deactivate(m); 691 vm_page_dirty(m); 692 splx(s); 693} 694 695/* 696 * vm_page_select_cache: 697 * 698 * Find a page on the cache queue with color optimization. As pages 699 * might be found, but not applicable, they are deactivated. This 700 * keeps us from using potentially busy cached pages. 701 * 702 * This routine must be called at splvm(). 703 * This routine may not block. 704 */ 705vm_page_t 706vm_page_select_cache(int color) 707{ 708 vm_page_t m; 709 710 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 711 while (TRUE) { 712 m = vm_pageq_find(PQ_CACHE, color, FALSE); 713 if (m && ((m->flags & (PG_BUSY|PG_UNMANAGED)) || m->busy || 714 m->hold_count || m->wire_count || 715 (!VM_OBJECT_TRYLOCK(m->object) && 716 !VM_OBJECT_LOCKED(m->object)))) { 717 vm_page_deactivate(m); 718 continue; 719 } 720 return m; 721 } 722} 723 724/* 725 * vm_page_alloc: 726 * 727 * Allocate and return a memory cell associated 728 * with this VM object/offset pair. 729 * 730 * page_req classes: 731 * VM_ALLOC_NORMAL normal process request 732 * VM_ALLOC_SYSTEM system *really* needs a page 733 * VM_ALLOC_INTERRUPT interrupt time request 734 * VM_ALLOC_ZERO zero page 735 * 736 * This routine may not block. 737 * 738 * Additional special handling is required when called from an 739 * interrupt (VM_ALLOC_INTERRUPT). We are not allowed to mess with 740 * the page cache in this case. 741 */ 742vm_page_t 743vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req) 744{ 745 vm_object_t m_object; 746 vm_page_t m = NULL; 747 int color, flags, page_req, s; 748 749 page_req = req & VM_ALLOC_CLASS_MASK; 750 751 if ((req & VM_ALLOC_NOOBJ) == 0) { 752 KASSERT(object != NULL, 753 ("vm_page_alloc: NULL object.")); 754 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 755 color = (pindex + object->pg_color) & PQ_L2_MASK; 756 } else 757 color = pindex & PQ_L2_MASK; 758 759 /* 760 * The pager is allowed to eat deeper into the free page list. 761 */ 762 if ((curproc == pageproc) && (page_req != VM_ALLOC_INTERRUPT)) { 763 page_req = VM_ALLOC_SYSTEM; 764 }; 765 766 s = splvm(); 767loop: 768 mtx_lock_spin(&vm_page_queue_free_mtx); 769 if (cnt.v_free_count > cnt.v_free_reserved || 770 (page_req == VM_ALLOC_SYSTEM && 771 cnt.v_cache_count == 0 && 772 cnt.v_free_count > cnt.v_interrupt_free_min) || 773 (page_req == VM_ALLOC_INTERRUPT && cnt.v_free_count > 0)) { 774 /* 775 * Allocate from the free queue if the number of free pages 776 * exceeds the minimum for the request class. 777 */ 778 m = vm_pageq_find(PQ_FREE, color, (req & VM_ALLOC_ZERO) != 0); 779 } else if (page_req != VM_ALLOC_INTERRUPT) { 780 mtx_unlock_spin(&vm_page_queue_free_mtx); 781 /* 782 * Allocatable from cache (non-interrupt only). On success, 783 * we must free the page and try again, thus ensuring that 784 * cnt.v_*_free_min counters are replenished. 785 */ 786 vm_page_lock_queues(); 787 if ((m = vm_page_select_cache(color)) == NULL) { 788 vm_page_unlock_queues(); 789 splx(s); 790#if defined(DIAGNOSTIC) 791 if (cnt.v_cache_count > 0) 792 printf("vm_page_alloc(NORMAL): missing pages on cache queue: %d\n", cnt.v_cache_count); 793#endif 794 atomic_add_int(&vm_pageout_deficit, 1); 795 pagedaemon_wakeup(); 796 return (NULL); 797 } 798 KASSERT(m->dirty == 0, ("Found dirty cache page %p", m)); 799 m_object = m->object; 800 VM_OBJECT_LOCK_ASSERT(m_object, MA_OWNED); 801 vm_page_busy(m); 802 pmap_remove_all(m); 803 vm_page_free(m); 804 vm_page_unlock_queues(); 805 if (m_object != object) 806 VM_OBJECT_UNLOCK(m_object); 807 goto loop; 808 } else { 809 /* 810 * Not allocatable from cache from interrupt, give up. 811 */ 812 mtx_unlock_spin(&vm_page_queue_free_mtx); 813 splx(s); 814 atomic_add_int(&vm_pageout_deficit, 1); 815 pagedaemon_wakeup(); 816 return (NULL); 817 } 818 819 /* 820 * At this point we had better have found a good page. 821 */ 822 823 KASSERT( 824 m != NULL, 825 ("vm_page_alloc(): missing page on free queue\n") 826 ); 827 828 /* 829 * Remove from free queue 830 */ 831 832 vm_pageq_remove_nowakeup(m); 833 834 /* 835 * Initialize structure. Only the PG_ZERO flag is inherited. 836 */ 837 flags = PG_BUSY; 838 if (m->flags & PG_ZERO) { 839 vm_page_zero_count--; 840 if (req & VM_ALLOC_ZERO) 841 flags = PG_ZERO | PG_BUSY; 842 } 843 m->flags = flags; 844 if (req & VM_ALLOC_WIRED) { 845 atomic_add_int(&cnt.v_wire_count, 1); 846 m->wire_count = 1; 847 } else 848 m->wire_count = 0; 849 m->hold_count = 0; 850 m->act_count = 0; 851 m->busy = 0; 852 m->valid = 0; 853 KASSERT(m->dirty == 0, ("vm_page_alloc: free/cache page %p was dirty", m)); 854 mtx_unlock_spin(&vm_page_queue_free_mtx); 855 856 /* 857 * vm_page_insert() is safe prior to the splx(). Note also that 858 * inserting a page here does not insert it into the pmap (which 859 * could cause us to block allocating memory). We cannot block 860 * anywhere. 861 */ 862 if ((req & VM_ALLOC_NOOBJ) == 0) 863 vm_page_insert(m, object, pindex); 864 else 865 m->pindex = pindex; 866 867 /* 868 * Don't wakeup too often - wakeup the pageout daemon when 869 * we would be nearly out of memory. 870 */ 871 if (vm_paging_needed()) 872 pagedaemon_wakeup(); 873 874 splx(s); 875 return (m); 876} 877 878/* 879 * vm_wait: (also see VM_WAIT macro) 880 * 881 * Block until free pages are available for allocation 882 * - Called in various places before memory allocations. 883 */ 884void 885vm_wait(void) 886{ 887 int s; 888 889 s = splvm(); 890 vm_page_lock_queues(); 891 if (curproc == pageproc) { 892 vm_pageout_pages_needed = 1; 893 msleep(&vm_pageout_pages_needed, &vm_page_queue_mtx, 894 PDROP | PSWP, "VMWait", 0); 895 } else { 896 if (!vm_pages_needed) { 897 vm_pages_needed = 1; 898 wakeup(&vm_pages_needed); 899 } 900 msleep(&cnt.v_free_count, &vm_page_queue_mtx, PDROP | PVM, 901 "vmwait", 0); 902 } 903 splx(s); 904} 905 906/* 907 * vm_waitpfault: (also see VM_WAITPFAULT macro) 908 * 909 * Block until free pages are available for allocation 910 * - Called only in vm_fault so that processes page faulting 911 * can be easily tracked. 912 * - Sleeps at a lower priority than vm_wait() so that vm_wait()ing 913 * processes will be able to grab memory first. Do not change 914 * this balance without careful testing first. 915 */ 916void 917vm_waitpfault(void) 918{ 919 int s; 920 921 s = splvm(); 922 vm_page_lock_queues(); 923 if (!vm_pages_needed) { 924 vm_pages_needed = 1; 925 wakeup(&vm_pages_needed); 926 } 927 msleep(&cnt.v_free_count, &vm_page_queue_mtx, PDROP | PUSER, 928 "pfault", 0); 929 splx(s); 930} 931 932/* 933 * vm_page_activate: 934 * 935 * Put the specified page on the active list (if appropriate). 936 * Ensure that act_count is at least ACT_INIT but do not otherwise 937 * mess with it. 938 * 939 * The page queues must be locked. 940 * This routine may not block. 941 */ 942void 943vm_page_activate(vm_page_t m) 944{ 945 int s; 946 947 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 948 s = splvm(); 949 if (m->queue != PQ_ACTIVE) { 950 if ((m->queue - m->pc) == PQ_CACHE) 951 cnt.v_reactivated++; 952 vm_pageq_remove(m); 953 if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) { 954 if (m->act_count < ACT_INIT) 955 m->act_count = ACT_INIT; 956 vm_pageq_enqueue(PQ_ACTIVE, m); 957 } 958 } else { 959 if (m->act_count < ACT_INIT) 960 m->act_count = ACT_INIT; 961 } 962 splx(s); 963} 964 965/* 966 * vm_page_free_wakeup: 967 * 968 * Helper routine for vm_page_free_toq() and vm_page_cache(). This 969 * routine is called when a page has been added to the cache or free 970 * queues. 971 * 972 * This routine may not block. 973 * This routine must be called at splvm() 974 */ 975static __inline void 976vm_page_free_wakeup(void) 977{ 978 979 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 980 /* 981 * if pageout daemon needs pages, then tell it that there are 982 * some free. 983 */ 984 if (vm_pageout_pages_needed && 985 cnt.v_cache_count + cnt.v_free_count >= cnt.v_pageout_free_min) { 986 wakeup(&vm_pageout_pages_needed); 987 vm_pageout_pages_needed = 0; 988 } 989 /* 990 * wakeup processes that are waiting on memory if we hit a 991 * high water mark. And wakeup scheduler process if we have 992 * lots of memory. this process will swapin processes. 993 */ 994 if (vm_pages_needed && !vm_page_count_min()) { 995 vm_pages_needed = 0; 996 wakeup(&cnt.v_free_count); 997 } 998} 999 1000/* 1001 * vm_page_free_toq: 1002 * 1003 * Returns the given page to the PQ_FREE list, 1004 * disassociating it with any VM object. 1005 * 1006 * Object and page must be locked prior to entry. 1007 * This routine may not block. 1008 */ 1009 1010void 1011vm_page_free_toq(vm_page_t m) 1012{ 1013 int s; 1014 struct vpgqueues *pq; 1015 vm_object_t object = m->object; 1016 1017 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1018 s = splvm(); 1019 cnt.v_tfree++; 1020 1021 if (m->busy || ((m->queue - m->pc) == PQ_FREE)) { 1022 printf( 1023 "vm_page_free: pindex(%lu), busy(%d), PG_BUSY(%d), hold(%d)\n", 1024 (u_long)m->pindex, m->busy, (m->flags & PG_BUSY) ? 1 : 0, 1025 m->hold_count); 1026 if ((m->queue - m->pc) == PQ_FREE) 1027 panic("vm_page_free: freeing free page"); 1028 else 1029 panic("vm_page_free: freeing busy page"); 1030 } 1031 1032 /* 1033 * unqueue, then remove page. Note that we cannot destroy 1034 * the page here because we do not want to call the pager's 1035 * callback routine until after we've put the page on the 1036 * appropriate free queue. 1037 */ 1038 vm_pageq_remove_nowakeup(m); 1039 vm_page_remove(m); 1040 1041 /* 1042 * If fictitious remove object association and 1043 * return, otherwise delay object association removal. 1044 */ 1045 if ((m->flags & PG_FICTITIOUS) != 0) { 1046 splx(s); 1047 return; 1048 } 1049 1050 m->valid = 0; 1051 vm_page_undirty(m); 1052 1053 if (m->wire_count != 0) { 1054 if (m->wire_count > 1) { 1055 panic("vm_page_free: invalid wire count (%d), pindex: 0x%lx", 1056 m->wire_count, (long)m->pindex); 1057 } 1058 panic("vm_page_free: freeing wired page\n"); 1059 } 1060 1061 /* 1062 * If we've exhausted the object's resident pages we want to free 1063 * it up. 1064 */ 1065 if (object && 1066 (object->type == OBJT_VNODE) && 1067 ((object->flags & OBJ_DEAD) == 0) 1068 ) { 1069 struct vnode *vp = (struct vnode *)object->handle; 1070 1071 if (vp) { 1072 VI_LOCK(vp); 1073 if (VSHOULDFREE(vp)) 1074 vfree(vp); 1075 VI_UNLOCK(vp); 1076 } 1077 } 1078 1079 /* 1080 * Clear the UNMANAGED flag when freeing an unmanaged page. 1081 */ 1082 if (m->flags & PG_UNMANAGED) { 1083 m->flags &= ~PG_UNMANAGED; 1084 } 1085 1086 if (m->hold_count != 0) { 1087 m->flags &= ~PG_ZERO; 1088 m->queue = PQ_HOLD; 1089 } else 1090 m->queue = PQ_FREE + m->pc; 1091 pq = &vm_page_queues[m->queue]; 1092 mtx_lock_spin(&vm_page_queue_free_mtx); 1093 pq->lcnt++; 1094 ++(*pq->cnt); 1095 1096 /* 1097 * Put zero'd pages on the end ( where we look for zero'd pages 1098 * first ) and non-zerod pages at the head. 1099 */ 1100 if (m->flags & PG_ZERO) { 1101 TAILQ_INSERT_TAIL(&pq->pl, m, pageq); 1102 ++vm_page_zero_count; 1103 } else { 1104 TAILQ_INSERT_HEAD(&pq->pl, m, pageq); 1105 } 1106 mtx_unlock_spin(&vm_page_queue_free_mtx); 1107 vm_page_free_wakeup(); 1108 splx(s); 1109} 1110 1111/* 1112 * vm_page_unmanage: 1113 * 1114 * Prevent PV management from being done on the page. The page is 1115 * removed from the paging queues as if it were wired, and as a 1116 * consequence of no longer being managed the pageout daemon will not 1117 * touch it (since there is no way to locate the pte mappings for the 1118 * page). madvise() calls that mess with the pmap will also no longer 1119 * operate on the page. 1120 * 1121 * Beyond that the page is still reasonably 'normal'. Freeing the page 1122 * will clear the flag. 1123 * 1124 * This routine is used by OBJT_PHYS objects - objects using unswappable 1125 * physical memory as backing store rather then swap-backed memory and 1126 * will eventually be extended to support 4MB unmanaged physical 1127 * mappings. 1128 */ 1129void 1130vm_page_unmanage(vm_page_t m) 1131{ 1132 int s; 1133 1134 s = splvm(); 1135 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1136 if ((m->flags & PG_UNMANAGED) == 0) { 1137 if (m->wire_count == 0) 1138 vm_pageq_remove(m); 1139 } 1140 vm_page_flag_set(m, PG_UNMANAGED); 1141 splx(s); 1142} 1143 1144/* 1145 * vm_page_wire: 1146 * 1147 * Mark this page as wired down by yet 1148 * another map, removing it from paging queues 1149 * as necessary. 1150 * 1151 * The page queues must be locked. 1152 * This routine may not block. 1153 */ 1154void 1155vm_page_wire(vm_page_t m) 1156{ 1157 int s; 1158 1159 /* 1160 * Only bump the wire statistics if the page is not already wired, 1161 * and only unqueue the page if it is on some queue (if it is unmanaged 1162 * it is already off the queues). 1163 */ 1164 s = splvm(); 1165 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1166 if (m->wire_count == 0) { 1167 if ((m->flags & PG_UNMANAGED) == 0) 1168 vm_pageq_remove(m); 1169 atomic_add_int(&cnt.v_wire_count, 1); 1170 } 1171 m->wire_count++; 1172 KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m)); 1173 splx(s); 1174} 1175 1176/* 1177 * vm_page_unwire: 1178 * 1179 * Release one wiring of this page, potentially 1180 * enabling it to be paged again. 1181 * 1182 * Many pages placed on the inactive queue should actually go 1183 * into the cache, but it is difficult to figure out which. What 1184 * we do instead, if the inactive target is well met, is to put 1185 * clean pages at the head of the inactive queue instead of the tail. 1186 * This will cause them to be moved to the cache more quickly and 1187 * if not actively re-referenced, freed more quickly. If we just 1188 * stick these pages at the end of the inactive queue, heavy filesystem 1189 * meta-data accesses can cause an unnecessary paging load on memory bound 1190 * processes. This optimization causes one-time-use metadata to be 1191 * reused more quickly. 1192 * 1193 * BUT, if we are in a low-memory situation we have no choice but to 1194 * put clean pages on the cache queue. 1195 * 1196 * A number of routines use vm_page_unwire() to guarantee that the page 1197 * will go into either the inactive or active queues, and will NEVER 1198 * be placed in the cache - for example, just after dirtying a page. 1199 * dirty pages in the cache are not allowed. 1200 * 1201 * The page queues must be locked. 1202 * This routine may not block. 1203 */ 1204void 1205vm_page_unwire(vm_page_t m, int activate) 1206{ 1207 int s; 1208 1209 s = splvm(); 1210 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1211 if (m->wire_count > 0) { 1212 m->wire_count--; 1213 if (m->wire_count == 0) { 1214 atomic_subtract_int(&cnt.v_wire_count, 1); 1215 if (m->flags & PG_UNMANAGED) { 1216 ; 1217 } else if (activate) 1218 vm_pageq_enqueue(PQ_ACTIVE, m); 1219 else { 1220 vm_page_flag_clear(m, PG_WINATCFLS); 1221 vm_pageq_enqueue(PQ_INACTIVE, m); 1222 } 1223 } 1224 } else { 1225 panic("vm_page_unwire: invalid wire count: %d\n", m->wire_count); 1226 } 1227 splx(s); 1228} 1229 1230 1231/* 1232 * Move the specified page to the inactive queue. If the page has 1233 * any associated swap, the swap is deallocated. 1234 * 1235 * Normally athead is 0 resulting in LRU operation. athead is set 1236 * to 1 if we want this page to be 'as if it were placed in the cache', 1237 * except without unmapping it from the process address space. 1238 * 1239 * This routine may not block. 1240 */ 1241static __inline void 1242_vm_page_deactivate(vm_page_t m, int athead) 1243{ 1244 int s; 1245 1246 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1247 /* 1248 * Ignore if already inactive. 1249 */ 1250 if (m->queue == PQ_INACTIVE) 1251 return; 1252 1253 s = splvm(); 1254 if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) { 1255 if ((m->queue - m->pc) == PQ_CACHE) 1256 cnt.v_reactivated++; 1257 vm_page_flag_clear(m, PG_WINATCFLS); 1258 vm_pageq_remove(m); 1259 if (athead) 1260 TAILQ_INSERT_HEAD(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 1261 else 1262 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 1263 m->queue = PQ_INACTIVE; 1264 vm_page_queues[PQ_INACTIVE].lcnt++; 1265 cnt.v_inactive_count++; 1266 } 1267 splx(s); 1268} 1269 1270void 1271vm_page_deactivate(vm_page_t m) 1272{ 1273 _vm_page_deactivate(m, 0); 1274} 1275 1276/* 1277 * vm_page_try_to_cache: 1278 * 1279 * Returns 0 on failure, 1 on success 1280 */ 1281int 1282vm_page_try_to_cache(vm_page_t m) 1283{ 1284 1285 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1286 if (m->dirty || m->hold_count || m->busy || m->wire_count || 1287 (m->flags & (PG_BUSY|PG_UNMANAGED))) { 1288 return (0); 1289 } 1290 pmap_remove_all(m); 1291 if (m->dirty) 1292 return (0); 1293 vm_page_cache(m); 1294 return (1); 1295} 1296 1297/* 1298 * vm_page_try_to_free() 1299 * 1300 * Attempt to free the page. If we cannot free it, we do nothing. 1301 * 1 is returned on success, 0 on failure. 1302 */ 1303int 1304vm_page_try_to_free(vm_page_t m) 1305{ 1306 1307 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1308 if (m->object != NULL) 1309 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1310 if (m->dirty || m->hold_count || m->busy || m->wire_count || 1311 (m->flags & (PG_BUSY|PG_UNMANAGED))) { 1312 return (0); 1313 } 1314 pmap_remove_all(m); 1315 if (m->dirty) 1316 return (0); 1317 vm_page_busy(m); 1318 vm_page_free(m); 1319 return (1); 1320} 1321 1322/* 1323 * vm_page_cache 1324 * 1325 * Put the specified page onto the page cache queue (if appropriate). 1326 * 1327 * This routine may not block. 1328 */ 1329void 1330vm_page_cache(vm_page_t m) 1331{ 1332 int s; 1333 1334 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1335 if ((m->flags & (PG_BUSY|PG_UNMANAGED)) || m->busy || 1336 m->hold_count || m->wire_count) { 1337 printf("vm_page_cache: attempting to cache busy page\n"); 1338 return; 1339 } 1340 if ((m->queue - m->pc) == PQ_CACHE) 1341 return; 1342 1343 /* 1344 * Remove all pmaps and indicate that the page is not 1345 * writeable or mapped. 1346 */ 1347 pmap_remove_all(m); 1348 if (m->dirty != 0) { 1349 panic("vm_page_cache: caching a dirty page, pindex: %ld", 1350 (long)m->pindex); 1351 } 1352 s = splvm(); 1353 vm_pageq_remove_nowakeup(m); 1354 vm_pageq_enqueue(PQ_CACHE + m->pc, m); 1355 vm_page_free_wakeup(); 1356 splx(s); 1357} 1358 1359/* 1360 * vm_page_dontneed 1361 * 1362 * Cache, deactivate, or do nothing as appropriate. This routine 1363 * is typically used by madvise() MADV_DONTNEED. 1364 * 1365 * Generally speaking we want to move the page into the cache so 1366 * it gets reused quickly. However, this can result in a silly syndrome 1367 * due to the page recycling too quickly. Small objects will not be 1368 * fully cached. On the otherhand, if we move the page to the inactive 1369 * queue we wind up with a problem whereby very large objects 1370 * unnecessarily blow away our inactive and cache queues. 1371 * 1372 * The solution is to move the pages based on a fixed weighting. We 1373 * either leave them alone, deactivate them, or move them to the cache, 1374 * where moving them to the cache has the highest weighting. 1375 * By forcing some pages into other queues we eventually force the 1376 * system to balance the queues, potentially recovering other unrelated 1377 * space from active. The idea is to not force this to happen too 1378 * often. 1379 */ 1380void 1381vm_page_dontneed(vm_page_t m) 1382{ 1383 static int dnweight; 1384 int dnw; 1385 int head; 1386 1387 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1388 dnw = ++dnweight; 1389 1390 /* 1391 * occassionally leave the page alone 1392 */ 1393 if ((dnw & 0x01F0) == 0 || 1394 m->queue == PQ_INACTIVE || 1395 m->queue - m->pc == PQ_CACHE 1396 ) { 1397 if (m->act_count >= ACT_INIT) 1398 --m->act_count; 1399 return; 1400 } 1401 1402 if (m->dirty == 0 && pmap_is_modified(m)) 1403 vm_page_dirty(m); 1404 1405 if (m->dirty || (dnw & 0x0070) == 0) { 1406 /* 1407 * Deactivate the page 3 times out of 32. 1408 */ 1409 head = 0; 1410 } else { 1411 /* 1412 * Cache the page 28 times out of every 32. Note that 1413 * the page is deactivated instead of cached, but placed 1414 * at the head of the queue instead of the tail. 1415 */ 1416 head = 1; 1417 } 1418 _vm_page_deactivate(m, head); 1419} 1420 1421/* 1422 * Grab a page, waiting until we are waken up due to the page 1423 * changing state. We keep on waiting, if the page continues 1424 * to be in the object. If the page doesn't exist, first allocate it 1425 * and then conditionally zero it. 1426 * 1427 * This routine may block. 1428 */ 1429vm_page_t 1430vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags) 1431{ 1432 vm_page_t m; 1433 1434 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1435retrylookup: 1436 if ((m = vm_page_lookup(object, pindex)) != NULL) { 1437 vm_page_lock_queues(); 1438 if (m->busy || (m->flags & PG_BUSY)) { 1439 vm_page_flag_set(m, PG_WANTED | PG_REFERENCED); 1440 VM_OBJECT_UNLOCK(object); 1441 msleep(m, &vm_page_queue_mtx, PDROP | PVM, "pgrbwt", 0); 1442 VM_OBJECT_LOCK(object); 1443 if ((allocflags & VM_ALLOC_RETRY) == 0) 1444 return (NULL); 1445 goto retrylookup; 1446 } else { 1447 if (allocflags & VM_ALLOC_WIRED) 1448 vm_page_wire(m); 1449 vm_page_busy(m); 1450 vm_page_unlock_queues(); 1451 return (m); 1452 } 1453 } 1454 m = vm_page_alloc(object, pindex, allocflags & ~VM_ALLOC_RETRY); 1455 if (m == NULL) { 1456 VM_OBJECT_UNLOCK(object); 1457 VM_WAIT; 1458 VM_OBJECT_LOCK(object); 1459 if ((allocflags & VM_ALLOC_RETRY) == 0) 1460 return (NULL); 1461 goto retrylookup; 1462 } 1463 if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0) 1464 pmap_zero_page(m); 1465 return (m); 1466} 1467 1468/* 1469 * Mapping function for valid bits or for dirty bits in 1470 * a page. May not block. 1471 * 1472 * Inputs are required to range within a page. 1473 */ 1474__inline int 1475vm_page_bits(int base, int size) 1476{ 1477 int first_bit; 1478 int last_bit; 1479 1480 KASSERT( 1481 base + size <= PAGE_SIZE, 1482 ("vm_page_bits: illegal base/size %d/%d", base, size) 1483 ); 1484 1485 if (size == 0) /* handle degenerate case */ 1486 return (0); 1487 1488 first_bit = base >> DEV_BSHIFT; 1489 last_bit = (base + size - 1) >> DEV_BSHIFT; 1490 1491 return ((2 << last_bit) - (1 << first_bit)); 1492} 1493 1494/* 1495 * vm_page_set_validclean: 1496 * 1497 * Sets portions of a page valid and clean. The arguments are expected 1498 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive 1499 * of any partial chunks touched by the range. The invalid portion of 1500 * such chunks will be zero'd. 1501 * 1502 * This routine may not block. 1503 * 1504 * (base + size) must be less then or equal to PAGE_SIZE. 1505 */ 1506void 1507vm_page_set_validclean(vm_page_t m, int base, int size) 1508{ 1509 int pagebits; 1510 int frag; 1511 int endoff; 1512 1513 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1514 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1515 if (size == 0) /* handle degenerate case */ 1516 return; 1517 1518 /* 1519 * If the base is not DEV_BSIZE aligned and the valid 1520 * bit is clear, we have to zero out a portion of the 1521 * first block. 1522 */ 1523 if ((frag = base & ~(DEV_BSIZE - 1)) != base && 1524 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0) 1525 pmap_zero_page_area(m, frag, base - frag); 1526 1527 /* 1528 * If the ending offset is not DEV_BSIZE aligned and the 1529 * valid bit is clear, we have to zero out a portion of 1530 * the last block. 1531 */ 1532 endoff = base + size; 1533 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff && 1534 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0) 1535 pmap_zero_page_area(m, endoff, 1536 DEV_BSIZE - (endoff & (DEV_BSIZE - 1))); 1537 1538 /* 1539 * Set valid, clear dirty bits. If validating the entire 1540 * page we can safely clear the pmap modify bit. We also 1541 * use this opportunity to clear the PG_NOSYNC flag. If a process 1542 * takes a write fault on a MAP_NOSYNC memory area the flag will 1543 * be set again. 1544 * 1545 * We set valid bits inclusive of any overlap, but we can only 1546 * clear dirty bits for DEV_BSIZE chunks that are fully within 1547 * the range. 1548 */ 1549 pagebits = vm_page_bits(base, size); 1550 m->valid |= pagebits; 1551#if 0 /* NOT YET */ 1552 if ((frag = base & (DEV_BSIZE - 1)) != 0) { 1553 frag = DEV_BSIZE - frag; 1554 base += frag; 1555 size -= frag; 1556 if (size < 0) 1557 size = 0; 1558 } 1559 pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1)); 1560#endif 1561 m->dirty &= ~pagebits; 1562 if (base == 0 && size == PAGE_SIZE) { 1563 pmap_clear_modify(m); 1564 vm_page_flag_clear(m, PG_NOSYNC); 1565 } 1566} 1567 1568void 1569vm_page_clear_dirty(vm_page_t m, int base, int size) 1570{ 1571 1572 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1573 m->dirty &= ~vm_page_bits(base, size); 1574} 1575 1576/* 1577 * vm_page_set_invalid: 1578 * 1579 * Invalidates DEV_BSIZE'd chunks within a page. Both the 1580 * valid and dirty bits for the effected areas are cleared. 1581 * 1582 * May not block. 1583 */ 1584void 1585vm_page_set_invalid(vm_page_t m, int base, int size) 1586{ 1587 int bits; 1588 1589 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1590 bits = vm_page_bits(base, size); 1591 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1592 m->valid &= ~bits; 1593 m->dirty &= ~bits; 1594 m->object->generation++; 1595} 1596 1597/* 1598 * vm_page_zero_invalid() 1599 * 1600 * The kernel assumes that the invalid portions of a page contain 1601 * garbage, but such pages can be mapped into memory by user code. 1602 * When this occurs, we must zero out the non-valid portions of the 1603 * page so user code sees what it expects. 1604 * 1605 * Pages are most often semi-valid when the end of a file is mapped 1606 * into memory and the file's size is not page aligned. 1607 */ 1608void 1609vm_page_zero_invalid(vm_page_t m, boolean_t setvalid) 1610{ 1611 int b; 1612 int i; 1613 1614 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1615 /* 1616 * Scan the valid bits looking for invalid sections that 1617 * must be zerod. Invalid sub-DEV_BSIZE'd areas ( where the 1618 * valid bit may be set ) have already been zerod by 1619 * vm_page_set_validclean(). 1620 */ 1621 for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) { 1622 if (i == (PAGE_SIZE / DEV_BSIZE) || 1623 (m->valid & (1 << i)) 1624 ) { 1625 if (i > b) { 1626 pmap_zero_page_area(m, 1627 b << DEV_BSHIFT, (i - b) << DEV_BSHIFT); 1628 } 1629 b = i + 1; 1630 } 1631 } 1632 1633 /* 1634 * setvalid is TRUE when we can safely set the zero'd areas 1635 * as being valid. We can do this if there are no cache consistancy 1636 * issues. e.g. it is ok to do with UFS, but not ok to do with NFS. 1637 */ 1638 if (setvalid) 1639 m->valid = VM_PAGE_BITS_ALL; 1640} 1641 1642/* 1643 * vm_page_is_valid: 1644 * 1645 * Is (partial) page valid? Note that the case where size == 0 1646 * will return FALSE in the degenerate case where the page is 1647 * entirely invalid, and TRUE otherwise. 1648 * 1649 * May not block. 1650 */ 1651int 1652vm_page_is_valid(vm_page_t m, int base, int size) 1653{ 1654 int bits = vm_page_bits(base, size); 1655 1656 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1657 if (m->valid && ((m->valid & bits) == bits)) 1658 return 1; 1659 else 1660 return 0; 1661} 1662 1663/* 1664 * update dirty bits from pmap/mmu. May not block. 1665 */ 1666void 1667vm_page_test_dirty(vm_page_t m) 1668{ 1669 if ((m->dirty != VM_PAGE_BITS_ALL) && pmap_is_modified(m)) { 1670 vm_page_dirty(m); 1671 } 1672} 1673 1674int so_zerocp_fullpage = 0; 1675 1676void 1677vm_page_cowfault(vm_page_t m) 1678{ 1679 vm_page_t mnew; 1680 vm_object_t object; 1681 vm_pindex_t pindex; 1682 1683 object = m->object; 1684 pindex = m->pindex; 1685 vm_page_busy(m); 1686 1687 retry_alloc: 1688 vm_page_remove(m); 1689 /* 1690 * An interrupt allocation is requested because the page 1691 * queues lock is held. 1692 */ 1693 mnew = vm_page_alloc(object, pindex, VM_ALLOC_INTERRUPT); 1694 if (mnew == NULL) { 1695 vm_page_insert(m, object, pindex); 1696 vm_page_unlock_queues(); 1697 VM_OBJECT_UNLOCK(object); 1698 VM_WAIT; 1699 VM_OBJECT_LOCK(object); 1700 vm_page_lock_queues(); 1701 goto retry_alloc; 1702 } 1703 1704 if (m->cow == 0) { 1705 /* 1706 * check to see if we raced with an xmit complete when 1707 * waiting to allocate a page. If so, put things back 1708 * the way they were 1709 */ 1710 vm_page_busy(mnew); 1711 vm_page_free(mnew); 1712 vm_page_insert(m, object, pindex); 1713 } else { /* clear COW & copy page */ 1714 if (!so_zerocp_fullpage) 1715 pmap_copy_page(m, mnew); 1716 mnew->valid = VM_PAGE_BITS_ALL; 1717 vm_page_dirty(mnew); 1718 vm_page_flag_clear(mnew, PG_BUSY); 1719 } 1720} 1721 1722void 1723vm_page_cowclear(vm_page_t m) 1724{ 1725 1726 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1727 if (m->cow) { 1728 m->cow--; 1729 /* 1730 * let vm_fault add back write permission lazily 1731 */ 1732 } 1733 /* 1734 * sf_buf_free() will free the page, so we needn't do it here 1735 */ 1736} 1737 1738void 1739vm_page_cowsetup(vm_page_t m) 1740{ 1741 1742 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1743 m->cow++; 1744 pmap_page_protect(m, VM_PROT_READ); 1745} 1746 1747#include "opt_ddb.h" 1748#ifdef DDB 1749#include <sys/kernel.h> 1750 1751#include <ddb/ddb.h> 1752 1753DB_SHOW_COMMAND(page, vm_page_print_page_info) 1754{ 1755 db_printf("cnt.v_free_count: %d\n", cnt.v_free_count); 1756 db_printf("cnt.v_cache_count: %d\n", cnt.v_cache_count); 1757 db_printf("cnt.v_inactive_count: %d\n", cnt.v_inactive_count); 1758 db_printf("cnt.v_active_count: %d\n", cnt.v_active_count); 1759 db_printf("cnt.v_wire_count: %d\n", cnt.v_wire_count); 1760 db_printf("cnt.v_free_reserved: %d\n", cnt.v_free_reserved); 1761 db_printf("cnt.v_free_min: %d\n", cnt.v_free_min); 1762 db_printf("cnt.v_free_target: %d\n", cnt.v_free_target); 1763 db_printf("cnt.v_cache_min: %d\n", cnt.v_cache_min); 1764 db_printf("cnt.v_inactive_target: %d\n", cnt.v_inactive_target); 1765} 1766 1767DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info) 1768{ 1769 int i; 1770 db_printf("PQ_FREE:"); 1771 for (i = 0; i < PQ_L2_SIZE; i++) { 1772 db_printf(" %d", vm_page_queues[PQ_FREE + i].lcnt); 1773 } 1774 db_printf("\n"); 1775 1776 db_printf("PQ_CACHE:"); 1777 for (i = 0; i < PQ_L2_SIZE; i++) { 1778 db_printf(" %d", vm_page_queues[PQ_CACHE + i].lcnt); 1779 } 1780 db_printf("\n"); 1781 1782 db_printf("PQ_ACTIVE: %d, PQ_INACTIVE: %d\n", 1783 vm_page_queues[PQ_ACTIVE].lcnt, 1784 vm_page_queues[PQ_INACTIVE].lcnt); 1785} 1786#endif /* DDB */ 1787