vm_page.c revision 136627
178064Sume/* 262638Skris * Copyright (c) 1991 Regents of the University of California. 355505Sshin * All rights reserved. 455505Sshin * 555505Sshin * This code is derived from software contributed to Berkeley by 655505Sshin * The Mach Operating System project at Carnegie-Mellon University. 755505Sshin * 855505Sshin * Redistribution and use in source and binary forms, with or without 955505Sshin * modification, are permitted provided that the following conditions 1055505Sshin * are met: 1155505Sshin * 1. Redistributions of source code must retain the above copyright 1255505Sshin * notice, this list of conditions and the following disclaimer. 1355505Sshin * 2. Redistributions in binary form must reproduce the above copyright 1455505Sshin * notice, this list of conditions and the following disclaimer in the 1555505Sshin * documentation and/or other materials provided with the distribution. 1655505Sshin * 4. Neither the name of the University nor the names of its contributors 1755505Sshin * may be used to endorse or promote products derived from this software 1855505Sshin * without specific prior written permission. 1955505Sshin * 2055505Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2155505Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2255505Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2355505Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2455505Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2555505Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2655505Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2755505Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2855505Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2955505Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30141580Sru * SUCH DAMAGE. 3155505Sshin * 3269862Sru * from: @(#)vm_page.c 7.4 (Berkeley) 5/7/91 3355505Sshin */ 3479537Sru 3555505Sshin/* 3655505Sshin * Copyright (c) 1987, 1990 Carnegie-Mellon University. 3755505Sshin * All rights reserved. 3855505Sshin * 3955505Sshin * Authors: Avadis Tevanian, Jr., Michael Wayne Young 4055505Sshin * 4155505Sshin * Permission to use, copy, modify and distribute this software and 4255505Sshin * its documentation is hereby granted, provided that both the copyright 4355505Sshin * notice and this permission notice appear in all copies of the 4455505Sshin * software, derivative works or modified versions, and any portions 4555505Sshin * thereof, and that both notices appear in supporting documentation. 4655505Sshin * 4755505Sshin * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 4855505Sshin * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 4955505Sshin * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 5055505Sshin * 5155505Sshin * Carnegie Mellon requests users of this software to return to 5255505Sshin * 5355505Sshin * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 5455505Sshin * School of Computer Science 5555505Sshin * Carnegie Mellon University 5655505Sshin * Pittsburgh PA 15213-3890 5755505Sshin * 5855505Sshin * any improvements or extensions that they make and grant Carnegie the 5955505Sshin * rights to redistribute these changes. 6055505Sshin */ 6155505Sshin 6255505Sshin/* 6355505Sshin * GENERAL RULES ON VM_PAGE MANIPULATION 6455505Sshin * 6578064Sume * - a pageq mutex is required when adding or removing a page from a 6678064Sume * page queue (vm_page_queue[]), regardless of other mutexes or the 6755505Sshin * busy state of a page. 6855505Sshin * 6955505Sshin * - a hash chain mutex is required when associating or disassociating 7055505Sshin * a page from the VM PAGE CACHE hash table (vm_page_buckets), 7155505Sshin * regardless of other mutexes or the busy state of a page. 7255505Sshin * 7371898Sru * - either a hash chain mutex OR a busied page is required in order 7455505Sshin * to modify the page flags. A hash chain mutex must be obtained in 7555505Sshin * order to busy a page. A page's flags cannot be modified by a 7655505Sshin * hash chain mutex if the page is marked busy. 7755505Sshin * 7855505Sshin * - The object memq mutex is held when inserting or removing 7955505Sshin * pages from an object (vm_page_insert() or vm_page_remove()). This 8055505Sshin * is different from the object's main mutex. 8155505Sshin * 8255505Sshin * Generally speaking, you have to be aware of side effects when running 8355505Sshin * vm_page ops. A vm_page_lookup() will return with the hash chain 8455505Sshin * locked, whether it was able to lookup the page or not. vm_page_free(), 8555505Sshin * vm_page_cache(), vm_page_activate(), and a number of other routines 8655505Sshin * will release the hash chain mutex for you. Intermediate manipulation 8778064Sume * routines such as vm_page_flag_set() expect the hash chain to be held 8878064Sume * on entry and the hash chain will remain held on return. 8955505Sshin * 9055505Sshin * pageq scanning can only occur with the pageq in question locked. 9155505Sshin * We have a known bottleneck with the active queue, but the cache 9255505Sshin * and free queues are actually arrays already. 9355505Sshin */ 9455505Sshin 9555505Sshin/* 9655505Sshin * Resident memory management module. 9755505Sshin */ 9855505Sshin 9970403Sru#include <sys/cdefs.h> 10055505Sshin__FBSDID("$FreeBSD: head/sys/vm/vm_page.c 136627 2004-10-17 22:33:40Z alc $"); 10155505Sshin 10255505Sshin#include <sys/param.h> 10355505Sshin#include <sys/systm.h> 10455505Sshin#include <sys/lock.h> 10555505Sshin#include <sys/malloc.h> 10678064Sume#include <sys/mutex.h> 10755505Sshin#include <sys/proc.h> 10878064Sume#include <sys/vmmeter.h> 10978064Sume#include <sys/vnode.h> 11055505Sshin 11178064Sume#include <vm/vm.h> 11278064Sume#include <vm/vm_param.h> 11355505Sshin#include <vm/vm_kern.h> 11478064Sume#include <vm/vm_object.h> 11578064Sume#include <vm/vm_page.h> 11655505Sshin#include <vm/vm_pageout.h> 11778064Sume#include <vm/vm_pager.h> 11878064Sume#include <vm/vm_extern.h> 11955505Sshin#include <vm/uma.h> 12078064Sume#include <vm/uma_int.h> 12178064Sume 12255505Sshin/* 12378064Sume * Associated with page of user-allocatable memory is a 12478064Sume * page structure. 12555505Sshin */ 12679365Sru 12755505Sshinstruct mtx vm_page_queue_mtx; 12855505Sshinstruct mtx vm_page_queue_free_mtx; 12955505Sshin 13055505Sshinvm_page_t vm_page_array = 0; 13155505Sshinint vm_page_array_size = 0; 13255505Sshinlong first_page = 0; 13355505Sshinint vm_page_zero_count = 0; 13455505Sshin 13555505Sshin/* 13655505Sshin * vm_set_page_size: 13755505Sshin * 13855505Sshin * Sets the page size, perhaps based upon the memory 13978064Sume * size. Must be called before any use of page-size 14055505Sshin * dependent functions. 14178064Sume */ 14278064Sumevoid 14355505Sshinvm_set_page_size(void) 14478064Sume{ 14578064Sume if (cnt.v_page_size == 0) 14655505Sshin cnt.v_page_size = PAGE_SIZE; 14778064Sume if (((cnt.v_page_size - 1) & cnt.v_page_size) != 0) 14878064Sume panic("vm_set_page_size: page size not a power of two"); 14955505Sshin} 15078064Sume 15178064Sume/* 15255505Sshin * vm_page_startup: 15378064Sume * 15455505Sshin * Initializes the resident memory module. 15555505Sshin * 15655505Sshin * Allocates memory for the page cells, and 15755505Sshin * for the object/offset-to-page hash table headers. 15855505Sshin * Each page cell is initialized and placed on the free list. 15955505Sshin */ 16055505Sshinvm_offset_t 16155505Sshinvm_page_startup(vm_offset_t vaddr) 16255505Sshin{ 16355505Sshin vm_offset_t mapped; 16455505Sshin vm_size_t npages; 16555505Sshin vm_paddr_t page_range; 16655505Sshin vm_paddr_t new_end; 16755505Sshin int i; 16855505Sshin vm_paddr_t pa; 16955505Sshin int nblocks; 17055505Sshin vm_paddr_t last_pa; 17155505Sshin 17255505Sshin /* the biggest memory array is the second group of pages */ 17355505Sshin vm_paddr_t end; 17455505Sshin vm_paddr_t biggestsize; 17555505Sshin int biggestone; 17655505Sshin 17755505Sshin vm_paddr_t total; 17855505Sshin vm_size_t bootpages; 17955505Sshin 18055505Sshin total = 0; 18155505Sshin biggestsize = 0; 18255505Sshin biggestone = 0; 18355505Sshin nblocks = 0; 18455505Sshin vaddr = round_page(vaddr); 18555505Sshin 18655505Sshin for (i = 0; phys_avail[i + 1]; i += 2) { 18755505Sshin phys_avail[i] = round_page(phys_avail[i]); 18855505Sshin phys_avail[i + 1] = trunc_page(phys_avail[i + 1]); 18955505Sshin } 19055505Sshin 19178064Sume for (i = 0; phys_avail[i + 1]; i += 2) { 19255505Sshin vm_paddr_t size = phys_avail[i + 1] - phys_avail[i]; 19355505Sshin 19481462Sru if (size > biggestsize) { 19581462Sru biggestone = i; 19655505Sshin biggestsize = size; 19755505Sshin } 19855505Sshin ++nblocks; 19955505Sshin total += size; 20055505Sshin } 20155505Sshin 20255505Sshin end = phys_avail[biggestone+1]; 20355505Sshin 20455505Sshin /* 20555505Sshin * Initialize the locks. 20655505Sshin */ 20755505Sshin mtx_init(&vm_page_queue_mtx, "vm page queue mutex", NULL, MTX_DEF | 20878064Sume MTX_RECURSE); 20978064Sume mtx_init(&vm_page_queue_free_mtx, "vm page queue free mutex", NULL, 21055505Sshin MTX_SPIN); 21155505Sshin 21255505Sshin /* 21355505Sshin * Initialize the queue headers for the free queue, the active queue 21455505Sshin * and the inactive queue. 21555505Sshin */ 21655505Sshin vm_pageq_init(); 21755505Sshin 21855505Sshin /* 21955505Sshin * Allocate memory for use when boot strapping the kernel memory 22055505Sshin * allocator. 22178064Sume */ 22278064Sume bootpages = UMA_BOOT_PAGES * UMA_SLAB_SIZE; 22355505Sshin new_end = end - bootpages; 22478064Sume new_end = trunc_page(new_end); 225131500Sru mapped = pmap_map(&vaddr, new_end, end, 22678064Sume VM_PROT_READ | VM_PROT_WRITE); 22778064Sume bzero((caddr_t) mapped, end - new_end); 22855505Sshin uma_startup((caddr_t)mapped); 22978064Sume 230131500Sru /* 23178064Sume * Compute the number of pages of memory that will be available for 23255505Sshin * use (taking into account the overhead of a page structure per 23355505Sshin * page). 23478064Sume */ 23578064Sume first_page = phys_avail[0] / PAGE_SIZE; 23655505Sshin page_range = phys_avail[(nblocks - 1) * 2 + 1] / PAGE_SIZE - first_page; 23755505Sshin npages = (total - (page_range * sizeof(struct vm_page)) - 23878064Sume (end - new_end)) / PAGE_SIZE; 23978064Sume end = new_end; 24055505Sshin 24155505Sshin /* 24255505Sshin * Reserve an unmapped guard page to trap access to vm_page_array[-1]. 24378064Sume */ 24478064Sume vaddr += PAGE_SIZE; 24555505Sshin 24678064Sume /* 24778064Sume * Initialize the mem entry structures now, and put them in the free 24855505Sshin * queue. 24955505Sshin */ 25055505Sshin new_end = trunc_page(end - page_range * sizeof(struct vm_page)); 25178064Sume mapped = pmap_map(&vaddr, new_end, end, 25278064Sume VM_PROT_READ | VM_PROT_WRITE); 25355505Sshin vm_page_array = (vm_page_t) mapped; 25478064Sume phys_avail[biggestone + 1] = new_end; 25578064Sume 25655505Sshin /* 25755505Sshin * Clear all of the page structures 25855505Sshin */ 25955505Sshin bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page)); 26055505Sshin vm_page_array_size = page_range; 26155505Sshin 26278064Sume /* 26378064Sume * Construct the free queue(s) in descending order (by physical 26455505Sshin * address) so that the first 16MB of physical memory is allocated 26555505Sshin * last rather than first. On large-memory machines, this avoids 26655505Sshin * the exhaustion of low physical memory before isa_dma_init has run. 26755505Sshin */ 26855505Sshin cnt.v_page_count = 0; 26955505Sshin cnt.v_free_count = 0; 27055505Sshin for (i = 0; phys_avail[i + 1] && npages > 0; i += 2) { 27168854Sru pa = phys_avail[i]; 27255505Sshin last_pa = phys_avail[i + 1]; 27355505Sshin while (pa < last_pa && npages-- > 0) { 27478064Sume vm_pageq_add_new_page(pa); 27578064Sume pa += PAGE_SIZE; 27678064Sume } 27755505Sshin } 27878064Sume return (vaddr); 27955505Sshin} 28055505Sshin 28155505Sshinvoid 28278064Sumevm_page_flag_set(vm_page_t m, unsigned short bits) 28355505Sshin{ 28455505Sshin 28555505Sshin mtx_assert(&vm_page_queue_mtx, MA_OWNED); 28655505Sshin m->flags |= bits; 28778064Sume} 28855505Sshin 28970403Sruvoid 29055505Sshinvm_page_flag_clear(vm_page_t m, unsigned short bits) 291141846Sru{ 292141946Sru 29355505Sshin mtx_assert(&vm_page_queue_mtx, MA_OWNED); 29455505Sshin m->flags &= ~bits; 29555505Sshin} 29655505Sshin 29755505Sshinvoid 29855505Sshinvm_page_busy(vm_page_t m) 29978064Sume{ 30055505Sshin KASSERT((m->flags & PG_BUSY) == 0, 30170403Sru ("vm_page_busy: page already busy!!!")); 30255505Sshin vm_page_flag_set(m, PG_BUSY); 30355505Sshin} 30455505Sshin 30555505Sshin/* 30678064Sume * vm_page_flash: 30755505Sshin * 30855505Sshin * wakeup anyone waiting for the page. 30978064Sume */ 310108317Sschweikhvoid 31155505Sshinvm_page_flash(vm_page_t m) 31278064Sume{ 31378064Sume if (m->flags & PG_WANTED) { 31455505Sshin vm_page_flag_clear(m, PG_WANTED); 31555505Sshin wakeup(m); 31655505Sshin } 31755505Sshin} 31855505Sshin 31955505Sshin/* 32055505Sshin * vm_page_wakeup: 32170403Sru * 32255505Sshin * clear the PG_BUSY flag and wakeup anyone waiting for the 32355505Sshin * page. 32455505Sshin * 32555505Sshin */ 32655505Sshinvoid 32755505Sshinvm_page_wakeup(vm_page_t m) 32855505Sshin{ 32978064Sume KASSERT(m->flags & PG_BUSY, ("vm_page_wakeup: page not busy!!!")); 33055505Sshin vm_page_flag_clear(m, PG_BUSY); 33155505Sshin vm_page_flash(m); 33255505Sshin} 33378064Sume 33455505Sshinvoid 33555505Sshinvm_page_io_start(vm_page_t m) 33670403Sru{ 33755505Sshin 33878064Sume VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 33978064Sume mtx_assert(&vm_page_queue_mtx, MA_OWNED); 34055505Sshin m->busy++; 34178064Sume} 34255505Sshin 34355505Sshinvoid 34455505Sshinvm_page_io_finish(vm_page_t m) 34555505Sshin{ 34678064Sume 34755505Sshin VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 34870403Sru mtx_assert(&vm_page_queue_mtx, MA_OWNED); 34955505Sshin m->busy--; 35055505Sshin if (m->busy == 0) 35155505Sshin vm_page_flash(m); 35255505Sshin} 35355505Sshin 35455505Sshin/* 35555505Sshin * Keep page from being freed by the page daemon 35655505Sshin * much of the same effect as wiring, except much lower 35755505Sshin * overhead and should be used only for *very* temporary 35855505Sshin * holding ("wiring"). 35955505Sshin */ 36055505Sshinvoid 36155505Sshinvm_page_hold(vm_page_t mem) 36279366Sru{ 36379366Sru 36455505Sshin mtx_assert(&vm_page_queue_mtx, MA_OWNED); 36555505Sshin mem->hold_count++; 36655505Sshin} 36755505Sshin 36855505Sshinvoid 36955505Sshinvm_page_unhold(vm_page_t mem) 370{ 371 372 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 373 --mem->hold_count; 374 KASSERT(mem->hold_count >= 0, ("vm_page_unhold: hold count < 0!!!")); 375 if (mem->hold_count == 0 && mem->queue == PQ_HOLD) 376 vm_page_free_toq(mem); 377} 378 379/* 380 * vm_page_free: 381 * 382 * Free a page 383 * 384 * The clearing of PG_ZERO is a temporary safety until the code can be 385 * reviewed to determine that PG_ZERO is being properly cleared on 386 * write faults or maps. PG_ZERO was previously cleared in 387 * vm_page_alloc(). 388 */ 389void 390vm_page_free(vm_page_t m) 391{ 392 vm_page_flag_clear(m, PG_ZERO); 393 vm_page_free_toq(m); 394 vm_page_zero_idle_wakeup(); 395} 396 397/* 398 * vm_page_free_zero: 399 * 400 * Free a page to the zerod-pages queue 401 */ 402void 403vm_page_free_zero(vm_page_t m) 404{ 405 vm_page_flag_set(m, PG_ZERO); 406 vm_page_free_toq(m); 407} 408 409/* 410 * vm_page_sleep_if_busy: 411 * 412 * Sleep and release the page queues lock if PG_BUSY is set or, 413 * if also_m_busy is TRUE, busy is non-zero. Returns TRUE if the 414 * thread slept and the page queues lock was released. 415 * Otherwise, retains the page queues lock and returns FALSE. 416 */ 417int 418vm_page_sleep_if_busy(vm_page_t m, int also_m_busy, const char *msg) 419{ 420 vm_object_t object; 421 int is_object_locked; 422 423 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 424 if ((m->flags & PG_BUSY) || (also_m_busy && m->busy)) { 425 vm_page_flag_set(m, PG_WANTED | PG_REFERENCED); 426 /* 427 * It's possible that while we sleep, the page will get 428 * unbusied and freed. If we are holding the object 429 * lock, we will assume we hold a reference to the object 430 * such that even if m->object changes, we can re-lock 431 * it. 432 * 433 * Remove mtx_owned() after vm_object locking is finished. 434 */ 435 object = m->object; 436 if ((is_object_locked = object != NULL && 437 mtx_owned(&object->mtx))) 438 mtx_unlock(&object->mtx); 439 msleep(m, &vm_page_queue_mtx, PDROP | PVM, msg, 0); 440 if (is_object_locked) 441 mtx_lock(&object->mtx); 442 return (TRUE); 443 } 444 return (FALSE); 445} 446 447/* 448 * vm_page_dirty: 449 * 450 * make page all dirty 451 */ 452void 453vm_page_dirty(vm_page_t m) 454{ 455 KASSERT(m->queue - m->pc != PQ_CACHE, 456 ("vm_page_dirty: page in cache!")); 457 KASSERT(m->queue - m->pc != PQ_FREE, 458 ("vm_page_dirty: page is free!")); 459 m->dirty = VM_PAGE_BITS_ALL; 460} 461 462/* 463 * vm_page_splay: 464 * 465 * Implements Sleator and Tarjan's top-down splay algorithm. Returns 466 * the vm_page containing the given pindex. If, however, that 467 * pindex is not found in the vm_object, returns a vm_page that is 468 * adjacent to the pindex, coming before or after it. 469 */ 470vm_page_t 471vm_page_splay(vm_pindex_t pindex, vm_page_t root) 472{ 473 struct vm_page dummy; 474 vm_page_t lefttreemax, righttreemin, y; 475 476 if (root == NULL) 477 return (root); 478 lefttreemax = righttreemin = &dummy; 479 for (;; root = y) { 480 if (pindex < root->pindex) { 481 if ((y = root->left) == NULL) 482 break; 483 if (pindex < y->pindex) { 484 /* Rotate right. */ 485 root->left = y->right; 486 y->right = root; 487 root = y; 488 if ((y = root->left) == NULL) 489 break; 490 } 491 /* Link into the new root's right tree. */ 492 righttreemin->left = root; 493 righttreemin = root; 494 } else if (pindex > root->pindex) { 495 if ((y = root->right) == NULL) 496 break; 497 if (pindex > y->pindex) { 498 /* Rotate left. */ 499 root->right = y->left; 500 y->left = root; 501 root = y; 502 if ((y = root->right) == NULL) 503 break; 504 } 505 /* Link into the new root's left tree. */ 506 lefttreemax->right = root; 507 lefttreemax = root; 508 } else 509 break; 510 } 511 /* Assemble the new root. */ 512 lefttreemax->right = root->left; 513 righttreemin->left = root->right; 514 root->left = dummy.right; 515 root->right = dummy.left; 516 return (root); 517} 518 519/* 520 * vm_page_insert: [ internal use only ] 521 * 522 * Inserts the given mem entry into the object and object list. 523 * 524 * The pagetables are not updated but will presumably fault the page 525 * in if necessary, or if a kernel page the caller will at some point 526 * enter the page into the kernel's pmap. We are not allowed to block 527 * here so we *can't* do this anyway. 528 * 529 * The object and page must be locked. 530 * This routine may not block. 531 */ 532void 533vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex) 534{ 535 vm_page_t root; 536 537 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 538 if (m->object != NULL) 539 panic("vm_page_insert: page already inserted"); 540 541 /* 542 * Record the object/offset pair in this page 543 */ 544 m->object = object; 545 m->pindex = pindex; 546 547 /* 548 * Now link into the object's ordered list of backed pages. 549 */ 550 root = object->root; 551 if (root == NULL) { 552 m->left = NULL; 553 m->right = NULL; 554 TAILQ_INSERT_TAIL(&object->memq, m, listq); 555 } else { 556 root = vm_page_splay(pindex, root); 557 if (pindex < root->pindex) { 558 m->left = root->left; 559 m->right = root; 560 root->left = NULL; 561 TAILQ_INSERT_BEFORE(root, m, listq); 562 } else if (pindex == root->pindex) 563 panic("vm_page_insert: offset already allocated"); 564 else { 565 m->right = root->right; 566 m->left = root; 567 root->right = NULL; 568 TAILQ_INSERT_AFTER(&object->memq, root, m, listq); 569 } 570 } 571 object->root = m; 572 object->generation++; 573 574 /* 575 * show that the object has one more resident page. 576 */ 577 object->resident_page_count++; 578 579 /* 580 * Since we are inserting a new and possibly dirty page, 581 * update the object's OBJ_WRITEABLE and OBJ_MIGHTBEDIRTY flags. 582 */ 583 if (m->flags & PG_WRITEABLE) 584 vm_object_set_writeable_dirty(object); 585} 586 587/* 588 * vm_page_remove: 589 * NOTE: used by device pager as well -wfj 590 * 591 * Removes the given mem entry from the object/offset-page 592 * table and the object page list, but do not invalidate/terminate 593 * the backing store. 594 * 595 * The object and page must be locked. 596 * The underlying pmap entry (if any) is NOT removed here. 597 * This routine may not block. 598 */ 599void 600vm_page_remove(vm_page_t m) 601{ 602 vm_object_t object; 603 vm_page_t root; 604 605 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 606 if (m->object == NULL) 607 return; 608 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 609 if ((m->flags & PG_BUSY) == 0) { 610 panic("vm_page_remove: page not busy"); 611 } 612 613 /* 614 * Basically destroy the page. 615 */ 616 vm_page_wakeup(m); 617 618 object = m->object; 619 620 /* 621 * Now remove from the object's list of backed pages. 622 */ 623 if (m != object->root) 624 vm_page_splay(m->pindex, object->root); 625 if (m->left == NULL) 626 root = m->right; 627 else { 628 root = vm_page_splay(m->pindex, m->left); 629 root->right = m->right; 630 } 631 object->root = root; 632 TAILQ_REMOVE(&object->memq, m, listq); 633 634 /* 635 * And show that the object has one fewer resident page. 636 */ 637 object->resident_page_count--; 638 object->generation++; 639 640 m->object = NULL; 641} 642 643/* 644 * vm_page_lookup: 645 * 646 * Returns the page associated with the object/offset 647 * pair specified; if none is found, NULL is returned. 648 * 649 * The object must be locked. 650 * This routine may not block. 651 * This is a critical path routine 652 */ 653vm_page_t 654vm_page_lookup(vm_object_t object, vm_pindex_t pindex) 655{ 656 vm_page_t m; 657 658 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 659 if ((m = object->root) != NULL && m->pindex != pindex) { 660 m = vm_page_splay(pindex, m); 661 if ((object->root = m)->pindex != pindex) 662 m = NULL; 663 } 664 return (m); 665} 666 667/* 668 * vm_page_rename: 669 * 670 * Move the given memory entry from its 671 * current object to the specified target object/offset. 672 * 673 * The object must be locked. 674 * This routine may not block. 675 * 676 * Note: swap associated with the page must be invalidated by the move. We 677 * have to do this for several reasons: (1) we aren't freeing the 678 * page, (2) we are dirtying the page, (3) the VM system is probably 679 * moving the page from object A to B, and will then later move 680 * the backing store from A to B and we can't have a conflict. 681 * 682 * Note: we *always* dirty the page. It is necessary both for the 683 * fact that we moved it, and because we may be invalidating 684 * swap. If the page is on the cache, we have to deactivate it 685 * or vm_page_dirty() will panic. Dirty pages are not allowed 686 * on the cache. 687 */ 688void 689vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex) 690{ 691 692 vm_page_remove(m); 693 vm_page_insert(m, new_object, new_pindex); 694 if (m->queue - m->pc == PQ_CACHE) 695 vm_page_deactivate(m); 696 vm_page_dirty(m); 697} 698 699/* 700 * vm_page_select_cache: 701 * 702 * Find a page on the cache queue with color optimization. As pages 703 * might be found, but not applicable, they are deactivated. This 704 * keeps us from using potentially busy cached pages. 705 * 706 * This routine may not block. 707 */ 708vm_page_t 709vm_page_select_cache(int color) 710{ 711 vm_page_t m; 712 713 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 714 while ((m = vm_pageq_find(PQ_CACHE, color, FALSE)) != NULL) { 715 if ((m->flags & PG_BUSY) == 0 && m->busy == 0 && 716 m->hold_count == 0 && (VM_OBJECT_TRYLOCK(m->object) || 717 VM_OBJECT_LOCKED(m->object))) { 718 KASSERT(m->dirty == 0, 719 ("Found dirty cache page %p", m)); 720 KASSERT(!pmap_page_is_mapped(m), 721 ("Found mapped cache page %p", m)); 722 KASSERT((m->flags & PG_UNMANAGED) == 0, 723 ("Found unmanaged cache page %p", m)); 724 KASSERT(m->wire_count == 0, 725 ("Found wired cache page %p", m)); 726 break; 727 } 728 vm_page_deactivate(m); 729 } 730 return (m); 731} 732 733/* 734 * vm_page_alloc: 735 * 736 * Allocate and return a memory cell associated 737 * with this VM object/offset pair. 738 * 739 * page_req classes: 740 * VM_ALLOC_NORMAL normal process request 741 * VM_ALLOC_SYSTEM system *really* needs a page 742 * VM_ALLOC_INTERRUPT interrupt time request 743 * VM_ALLOC_ZERO zero page 744 * 745 * This routine may not block. 746 * 747 * Additional special handling is required when called from an 748 * interrupt (VM_ALLOC_INTERRUPT). We are not allowed to mess with 749 * the page cache in this case. 750 */ 751vm_page_t 752vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req) 753{ 754 vm_object_t m_object; 755 vm_page_t m = NULL; 756 int color, flags, page_req; 757 758 page_req = req & VM_ALLOC_CLASS_MASK; 759 760 if ((req & VM_ALLOC_NOOBJ) == 0) { 761 KASSERT(object != NULL, 762 ("vm_page_alloc: NULL object.")); 763 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 764 color = (pindex + object->pg_color) & PQ_L2_MASK; 765 } else 766 color = pindex & PQ_L2_MASK; 767 768 /* 769 * The pager is allowed to eat deeper into the free page list. 770 */ 771 if ((curproc == pageproc) && (page_req != VM_ALLOC_INTERRUPT)) { 772 page_req = VM_ALLOC_SYSTEM; 773 }; 774 775loop: 776 mtx_lock_spin(&vm_page_queue_free_mtx); 777 if (cnt.v_free_count > cnt.v_free_reserved || 778 (page_req == VM_ALLOC_SYSTEM && 779 cnt.v_cache_count == 0 && 780 cnt.v_free_count > cnt.v_interrupt_free_min) || 781 (page_req == VM_ALLOC_INTERRUPT && cnt.v_free_count > 0)) { 782 /* 783 * Allocate from the free queue if the number of free pages 784 * exceeds the minimum for the request class. 785 */ 786 m = vm_pageq_find(PQ_FREE, color, (req & VM_ALLOC_ZERO) != 0); 787 } else if (page_req != VM_ALLOC_INTERRUPT) { 788 mtx_unlock_spin(&vm_page_queue_free_mtx); 789 /* 790 * Allocatable from cache (non-interrupt only). On success, 791 * we must free the page and try again, thus ensuring that 792 * cnt.v_*_free_min counters are replenished. 793 */ 794 vm_page_lock_queues(); 795 if ((m = vm_page_select_cache(color)) == NULL) { 796#if defined(DIAGNOSTIC) 797 if (cnt.v_cache_count > 0) 798 printf("vm_page_alloc(NORMAL): missing pages on cache queue: %d\n", cnt.v_cache_count); 799#endif 800 vm_page_unlock_queues(); 801 atomic_add_int(&vm_pageout_deficit, 1); 802 pagedaemon_wakeup(); 803 return (NULL); 804 } 805 m_object = m->object; 806 VM_OBJECT_LOCK_ASSERT(m_object, MA_OWNED); 807 vm_page_busy(m); 808 vm_page_free(m); 809 vm_page_unlock_queues(); 810 if (m_object != object) 811 VM_OBJECT_UNLOCK(m_object); 812 goto loop; 813 } else { 814 /* 815 * Not allocatable from cache from interrupt, give up. 816 */ 817 mtx_unlock_spin(&vm_page_queue_free_mtx); 818 atomic_add_int(&vm_pageout_deficit, 1); 819 pagedaemon_wakeup(); 820 return (NULL); 821 } 822 823 /* 824 * At this point we had better have found a good page. 825 */ 826 827 KASSERT( 828 m != NULL, 829 ("vm_page_alloc(): missing page on free queue") 830 ); 831 832 /* 833 * Remove from free queue 834 */ 835 vm_pageq_remove_nowakeup(m); 836 837 /* 838 * Initialize structure. Only the PG_ZERO flag is inherited. 839 */ 840 flags = PG_BUSY; 841 if (m->flags & PG_ZERO) { 842 vm_page_zero_count--; 843 if (req & VM_ALLOC_ZERO) 844 flags = PG_ZERO | PG_BUSY; 845 } 846 if (req & VM_ALLOC_NOOBJ) 847 flags &= ~PG_BUSY; 848 m->flags = flags; 849 if (req & VM_ALLOC_WIRED) { 850 atomic_add_int(&cnt.v_wire_count, 1); 851 m->wire_count = 1; 852 } else 853 m->wire_count = 0; 854 m->hold_count = 0; 855 m->act_count = 0; 856 m->busy = 0; 857 m->valid = 0; 858 KASSERT(m->dirty == 0, ("vm_page_alloc: free/cache page %p was dirty", m)); 859 mtx_unlock_spin(&vm_page_queue_free_mtx); 860 861 if ((req & VM_ALLOC_NOOBJ) == 0) 862 vm_page_insert(m, object, pindex); 863 else 864 m->pindex = pindex; 865 866 /* 867 * Don't wakeup too often - wakeup the pageout daemon when 868 * we would be nearly out of memory. 869 */ 870 if (vm_paging_needed()) 871 pagedaemon_wakeup(); 872 873 return (m); 874} 875 876/* 877 * vm_wait: (also see VM_WAIT macro) 878 * 879 * Block until free pages are available for allocation 880 * - Called in various places before memory allocations. 881 */ 882void 883vm_wait(void) 884{ 885 886 vm_page_lock_queues(); 887 if (curproc == pageproc) { 888 vm_pageout_pages_needed = 1; 889 msleep(&vm_pageout_pages_needed, &vm_page_queue_mtx, 890 PDROP | PSWP, "VMWait", 0); 891 } else { 892 if (!vm_pages_needed) { 893 vm_pages_needed = 1; 894 wakeup(&vm_pages_needed); 895 } 896 msleep(&cnt.v_free_count, &vm_page_queue_mtx, PDROP | PVM, 897 "vmwait", 0); 898 } 899} 900 901/* 902 * vm_waitpfault: (also see VM_WAITPFAULT macro) 903 * 904 * Block until free pages are available for allocation 905 * - Called only in vm_fault so that processes page faulting 906 * can be easily tracked. 907 * - Sleeps at a lower priority than vm_wait() so that vm_wait()ing 908 * processes will be able to grab memory first. Do not change 909 * this balance without careful testing first. 910 */ 911void 912vm_waitpfault(void) 913{ 914 915 vm_page_lock_queues(); 916 if (!vm_pages_needed) { 917 vm_pages_needed = 1; 918 wakeup(&vm_pages_needed); 919 } 920 msleep(&cnt.v_free_count, &vm_page_queue_mtx, PDROP | PUSER, 921 "pfault", 0); 922} 923 924/* 925 * vm_page_activate: 926 * 927 * Put the specified page on the active list (if appropriate). 928 * Ensure that act_count is at least ACT_INIT but do not otherwise 929 * mess with it. 930 * 931 * The page queues must be locked. 932 * This routine may not block. 933 */ 934void 935vm_page_activate(vm_page_t m) 936{ 937 938 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 939 if (m->queue != PQ_ACTIVE) { 940 if ((m->queue - m->pc) == PQ_CACHE) 941 cnt.v_reactivated++; 942 vm_pageq_remove(m); 943 if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) { 944 if (m->act_count < ACT_INIT) 945 m->act_count = ACT_INIT; 946 vm_pageq_enqueue(PQ_ACTIVE, m); 947 } 948 } else { 949 if (m->act_count < ACT_INIT) 950 m->act_count = ACT_INIT; 951 } 952} 953 954/* 955 * vm_page_free_wakeup: 956 * 957 * Helper routine for vm_page_free_toq() and vm_page_cache(). This 958 * routine is called when a page has been added to the cache or free 959 * queues. 960 * 961 * The page queues must be locked. 962 * This routine may not block. 963 */ 964static __inline void 965vm_page_free_wakeup(void) 966{ 967 968 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 969 /* 970 * if pageout daemon needs pages, then tell it that there are 971 * some free. 972 */ 973 if (vm_pageout_pages_needed && 974 cnt.v_cache_count + cnt.v_free_count >= cnt.v_pageout_free_min) { 975 wakeup(&vm_pageout_pages_needed); 976 vm_pageout_pages_needed = 0; 977 } 978 /* 979 * wakeup processes that are waiting on memory if we hit a 980 * high water mark. And wakeup scheduler process if we have 981 * lots of memory. this process will swapin processes. 982 */ 983 if (vm_pages_needed && !vm_page_count_min()) { 984 vm_pages_needed = 0; 985 wakeup(&cnt.v_free_count); 986 } 987} 988 989/* 990 * vm_page_free_toq: 991 * 992 * Returns the given page to the PQ_FREE list, 993 * disassociating it with any VM object. 994 * 995 * Object and page must be locked prior to entry. 996 * This routine may not block. 997 */ 998 999void 1000vm_page_free_toq(vm_page_t m) 1001{ 1002 struct vpgqueues *pq; 1003 vm_object_t object = m->object; 1004 1005 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1006 cnt.v_tfree++; 1007 1008 if (m->busy || ((m->queue - m->pc) == PQ_FREE)) { 1009 printf( 1010 "vm_page_free: pindex(%lu), busy(%d), PG_BUSY(%d), hold(%d)\n", 1011 (u_long)m->pindex, m->busy, (m->flags & PG_BUSY) ? 1 : 0, 1012 m->hold_count); 1013 if ((m->queue - m->pc) == PQ_FREE) 1014 panic("vm_page_free: freeing free page"); 1015 else 1016 panic("vm_page_free: freeing busy page"); 1017 } 1018 1019 /* 1020 * unqueue, then remove page. Note that we cannot destroy 1021 * the page here because we do not want to call the pager's 1022 * callback routine until after we've put the page on the 1023 * appropriate free queue. 1024 */ 1025 vm_pageq_remove_nowakeup(m); 1026 vm_page_remove(m); 1027 1028 /* 1029 * If fictitious remove object association and 1030 * return, otherwise delay object association removal. 1031 */ 1032 if ((m->flags & PG_FICTITIOUS) != 0) { 1033 return; 1034 } 1035 1036 m->valid = 0; 1037 vm_page_undirty(m); 1038 1039 if (m->wire_count != 0) { 1040 if (m->wire_count > 1) { 1041 panic("vm_page_free: invalid wire count (%d), pindex: 0x%lx", 1042 m->wire_count, (long)m->pindex); 1043 } 1044 panic("vm_page_free: freeing wired page"); 1045 } 1046 1047 /* 1048 * If we've exhausted the object's resident pages we want to free 1049 * it up. 1050 */ 1051 if (object && 1052 (object->type == OBJT_VNODE) && 1053 ((object->flags & OBJ_DEAD) == 0) 1054 ) { 1055 struct vnode *vp = (struct vnode *)object->handle; 1056 1057 if (vp) { 1058 VI_LOCK(vp); 1059 if (VSHOULDFREE(vp)) 1060 vfree(vp); 1061 VI_UNLOCK(vp); 1062 } 1063 } 1064 1065 /* 1066 * Clear the UNMANAGED flag when freeing an unmanaged page. 1067 */ 1068 if (m->flags & PG_UNMANAGED) { 1069 m->flags &= ~PG_UNMANAGED; 1070 } 1071 1072 if (m->hold_count != 0) { 1073 m->flags &= ~PG_ZERO; 1074 m->queue = PQ_HOLD; 1075 } else 1076 m->queue = PQ_FREE + m->pc; 1077 pq = &vm_page_queues[m->queue]; 1078 mtx_lock_spin(&vm_page_queue_free_mtx); 1079 pq->lcnt++; 1080 ++(*pq->cnt); 1081 1082 /* 1083 * Put zero'd pages on the end ( where we look for zero'd pages 1084 * first ) and non-zerod pages at the head. 1085 */ 1086 if (m->flags & PG_ZERO) { 1087 TAILQ_INSERT_TAIL(&pq->pl, m, pageq); 1088 ++vm_page_zero_count; 1089 } else { 1090 TAILQ_INSERT_HEAD(&pq->pl, m, pageq); 1091 } 1092 mtx_unlock_spin(&vm_page_queue_free_mtx); 1093 vm_page_free_wakeup(); 1094} 1095 1096/* 1097 * vm_page_unmanage: 1098 * 1099 * Prevent PV management from being done on the page. The page is 1100 * removed from the paging queues as if it were wired, and as a 1101 * consequence of no longer being managed the pageout daemon will not 1102 * touch it (since there is no way to locate the pte mappings for the 1103 * page). madvise() calls that mess with the pmap will also no longer 1104 * operate on the page. 1105 * 1106 * Beyond that the page is still reasonably 'normal'. Freeing the page 1107 * will clear the flag. 1108 * 1109 * This routine is used by OBJT_PHYS objects - objects using unswappable 1110 * physical memory as backing store rather then swap-backed memory and 1111 * will eventually be extended to support 4MB unmanaged physical 1112 * mappings. 1113 */ 1114void 1115vm_page_unmanage(vm_page_t m) 1116{ 1117 1118 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1119 if ((m->flags & PG_UNMANAGED) == 0) { 1120 if (m->wire_count == 0) 1121 vm_pageq_remove(m); 1122 } 1123 vm_page_flag_set(m, PG_UNMANAGED); 1124} 1125 1126/* 1127 * vm_page_wire: 1128 * 1129 * Mark this page as wired down by yet 1130 * another map, removing it from paging queues 1131 * as necessary. 1132 * 1133 * The page queues must be locked. 1134 * This routine may not block. 1135 */ 1136void 1137vm_page_wire(vm_page_t m) 1138{ 1139 1140 /* 1141 * Only bump the wire statistics if the page is not already wired, 1142 * and only unqueue the page if it is on some queue (if it is unmanaged 1143 * it is already off the queues). 1144 */ 1145 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1146 if (m->flags & PG_FICTITIOUS) 1147 return; 1148 if (m->wire_count == 0) { 1149 if ((m->flags & PG_UNMANAGED) == 0) 1150 vm_pageq_remove(m); 1151 atomic_add_int(&cnt.v_wire_count, 1); 1152 } 1153 m->wire_count++; 1154 KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m)); 1155} 1156 1157/* 1158 * vm_page_unwire: 1159 * 1160 * Release one wiring of this page, potentially 1161 * enabling it to be paged again. 1162 * 1163 * Many pages placed on the inactive queue should actually go 1164 * into the cache, but it is difficult to figure out which. What 1165 * we do instead, if the inactive target is well met, is to put 1166 * clean pages at the head of the inactive queue instead of the tail. 1167 * This will cause them to be moved to the cache more quickly and 1168 * if not actively re-referenced, freed more quickly. If we just 1169 * stick these pages at the end of the inactive queue, heavy filesystem 1170 * meta-data accesses can cause an unnecessary paging load on memory bound 1171 * processes. This optimization causes one-time-use metadata to be 1172 * reused more quickly. 1173 * 1174 * BUT, if we are in a low-memory situation we have no choice but to 1175 * put clean pages on the cache queue. 1176 * 1177 * A number of routines use vm_page_unwire() to guarantee that the page 1178 * will go into either the inactive or active queues, and will NEVER 1179 * be placed in the cache - for example, just after dirtying a page. 1180 * dirty pages in the cache are not allowed. 1181 * 1182 * The page queues must be locked. 1183 * This routine may not block. 1184 */ 1185void 1186vm_page_unwire(vm_page_t m, int activate) 1187{ 1188 1189 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1190 if (m->flags & PG_FICTITIOUS) 1191 return; 1192 if (m->wire_count > 0) { 1193 m->wire_count--; 1194 if (m->wire_count == 0) { 1195 atomic_subtract_int(&cnt.v_wire_count, 1); 1196 if (m->flags & PG_UNMANAGED) { 1197 ; 1198 } else if (activate) 1199 vm_pageq_enqueue(PQ_ACTIVE, m); 1200 else { 1201 vm_page_flag_clear(m, PG_WINATCFLS); 1202 vm_pageq_enqueue(PQ_INACTIVE, m); 1203 } 1204 } 1205 } else { 1206 panic("vm_page_unwire: invalid wire count: %d", m->wire_count); 1207 } 1208} 1209 1210 1211/* 1212 * Move the specified page to the inactive queue. If the page has 1213 * any associated swap, the swap is deallocated. 1214 * 1215 * Normally athead is 0 resulting in LRU operation. athead is set 1216 * to 1 if we want this page to be 'as if it were placed in the cache', 1217 * except without unmapping it from the process address space. 1218 * 1219 * This routine may not block. 1220 */ 1221static __inline void 1222_vm_page_deactivate(vm_page_t m, int athead) 1223{ 1224 1225 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1226 1227 /* 1228 * Ignore if already inactive. 1229 */ 1230 if (m->queue == PQ_INACTIVE) 1231 return; 1232 if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) { 1233 if ((m->queue - m->pc) == PQ_CACHE) 1234 cnt.v_reactivated++; 1235 vm_page_flag_clear(m, PG_WINATCFLS); 1236 vm_pageq_remove(m); 1237 if (athead) 1238 TAILQ_INSERT_HEAD(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 1239 else 1240 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 1241 m->queue = PQ_INACTIVE; 1242 vm_page_queues[PQ_INACTIVE].lcnt++; 1243 cnt.v_inactive_count++; 1244 } 1245} 1246 1247void 1248vm_page_deactivate(vm_page_t m) 1249{ 1250 _vm_page_deactivate(m, 0); 1251} 1252 1253/* 1254 * vm_page_try_to_cache: 1255 * 1256 * Returns 0 on failure, 1 on success 1257 */ 1258int 1259vm_page_try_to_cache(vm_page_t m) 1260{ 1261 1262 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1263 if (m->dirty || m->hold_count || m->busy || m->wire_count || 1264 (m->flags & (PG_BUSY|PG_UNMANAGED))) { 1265 return (0); 1266 } 1267 pmap_remove_all(m); 1268 if (m->dirty) 1269 return (0); 1270 vm_page_cache(m); 1271 return (1); 1272} 1273 1274/* 1275 * vm_page_try_to_free() 1276 * 1277 * Attempt to free the page. If we cannot free it, we do nothing. 1278 * 1 is returned on success, 0 on failure. 1279 */ 1280int 1281vm_page_try_to_free(vm_page_t m) 1282{ 1283 1284 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1285 if (m->object != NULL) 1286 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1287 if (m->dirty || m->hold_count || m->busy || m->wire_count || 1288 (m->flags & (PG_BUSY|PG_UNMANAGED))) { 1289 return (0); 1290 } 1291 pmap_remove_all(m); 1292 if (m->dirty) 1293 return (0); 1294 vm_page_busy(m); 1295 vm_page_free(m); 1296 return (1); 1297} 1298 1299/* 1300 * vm_page_cache 1301 * 1302 * Put the specified page onto the page cache queue (if appropriate). 1303 * 1304 * This routine may not block. 1305 */ 1306void 1307vm_page_cache(vm_page_t m) 1308{ 1309 1310 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1311 if ((m->flags & (PG_BUSY|PG_UNMANAGED)) || m->busy || 1312 m->hold_count || m->wire_count) { 1313 printf("vm_page_cache: attempting to cache busy page\n"); 1314 return; 1315 } 1316 if ((m->queue - m->pc) == PQ_CACHE) 1317 return; 1318 1319 /* 1320 * Remove all pmaps and indicate that the page is not 1321 * writeable or mapped. 1322 */ 1323 pmap_remove_all(m); 1324 if (m->dirty != 0) { 1325 panic("vm_page_cache: caching a dirty page, pindex: %ld", 1326 (long)m->pindex); 1327 } 1328 vm_pageq_remove_nowakeup(m); 1329 vm_pageq_enqueue(PQ_CACHE + m->pc, m); 1330 vm_page_free_wakeup(); 1331} 1332 1333/* 1334 * vm_page_dontneed 1335 * 1336 * Cache, deactivate, or do nothing as appropriate. This routine 1337 * is typically used by madvise() MADV_DONTNEED. 1338 * 1339 * Generally speaking we want to move the page into the cache so 1340 * it gets reused quickly. However, this can result in a silly syndrome 1341 * due to the page recycling too quickly. Small objects will not be 1342 * fully cached. On the otherhand, if we move the page to the inactive 1343 * queue we wind up with a problem whereby very large objects 1344 * unnecessarily blow away our inactive and cache queues. 1345 * 1346 * The solution is to move the pages based on a fixed weighting. We 1347 * either leave them alone, deactivate them, or move them to the cache, 1348 * where moving them to the cache has the highest weighting. 1349 * By forcing some pages into other queues we eventually force the 1350 * system to balance the queues, potentially recovering other unrelated 1351 * space from active. The idea is to not force this to happen too 1352 * often. 1353 */ 1354void 1355vm_page_dontneed(vm_page_t m) 1356{ 1357 static int dnweight; 1358 int dnw; 1359 int head; 1360 1361 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1362 dnw = ++dnweight; 1363 1364 /* 1365 * occassionally leave the page alone 1366 */ 1367 if ((dnw & 0x01F0) == 0 || 1368 m->queue == PQ_INACTIVE || 1369 m->queue - m->pc == PQ_CACHE 1370 ) { 1371 if (m->act_count >= ACT_INIT) 1372 --m->act_count; 1373 return; 1374 } 1375 1376 if (m->dirty == 0 && pmap_is_modified(m)) 1377 vm_page_dirty(m); 1378 1379 if (m->dirty || (dnw & 0x0070) == 0) { 1380 /* 1381 * Deactivate the page 3 times out of 32. 1382 */ 1383 head = 0; 1384 } else { 1385 /* 1386 * Cache the page 28 times out of every 32. Note that 1387 * the page is deactivated instead of cached, but placed 1388 * at the head of the queue instead of the tail. 1389 */ 1390 head = 1; 1391 } 1392 _vm_page_deactivate(m, head); 1393} 1394 1395/* 1396 * Grab a page, waiting until we are waken up due to the page 1397 * changing state. We keep on waiting, if the page continues 1398 * to be in the object. If the page doesn't exist, first allocate it 1399 * and then conditionally zero it. 1400 * 1401 * This routine may block. 1402 */ 1403vm_page_t 1404vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags) 1405{ 1406 vm_page_t m; 1407 1408 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1409retrylookup: 1410 if ((m = vm_page_lookup(object, pindex)) != NULL) { 1411 vm_page_lock_queues(); 1412 if (m->busy || (m->flags & PG_BUSY)) { 1413 vm_page_flag_set(m, PG_WANTED | PG_REFERENCED); 1414 VM_OBJECT_UNLOCK(object); 1415 msleep(m, &vm_page_queue_mtx, PDROP | PVM, "pgrbwt", 0); 1416 VM_OBJECT_LOCK(object); 1417 if ((allocflags & VM_ALLOC_RETRY) == 0) 1418 return (NULL); 1419 goto retrylookup; 1420 } else { 1421 if (allocflags & VM_ALLOC_WIRED) 1422 vm_page_wire(m); 1423 vm_page_busy(m); 1424 vm_page_unlock_queues(); 1425 return (m); 1426 } 1427 } 1428 m = vm_page_alloc(object, pindex, allocflags & ~VM_ALLOC_RETRY); 1429 if (m == NULL) { 1430 VM_OBJECT_UNLOCK(object); 1431 VM_WAIT; 1432 VM_OBJECT_LOCK(object); 1433 if ((allocflags & VM_ALLOC_RETRY) == 0) 1434 return (NULL); 1435 goto retrylookup; 1436 } 1437 if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0) 1438 pmap_zero_page(m); 1439 return (m); 1440} 1441 1442/* 1443 * Mapping function for valid bits or for dirty bits in 1444 * a page. May not block. 1445 * 1446 * Inputs are required to range within a page. 1447 */ 1448__inline int 1449vm_page_bits(int base, int size) 1450{ 1451 int first_bit; 1452 int last_bit; 1453 1454 KASSERT( 1455 base + size <= PAGE_SIZE, 1456 ("vm_page_bits: illegal base/size %d/%d", base, size) 1457 ); 1458 1459 if (size == 0) /* handle degenerate case */ 1460 return (0); 1461 1462 first_bit = base >> DEV_BSHIFT; 1463 last_bit = (base + size - 1) >> DEV_BSHIFT; 1464 1465 return ((2 << last_bit) - (1 << first_bit)); 1466} 1467 1468/* 1469 * vm_page_set_validclean: 1470 * 1471 * Sets portions of a page valid and clean. The arguments are expected 1472 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive 1473 * of any partial chunks touched by the range. The invalid portion of 1474 * such chunks will be zero'd. 1475 * 1476 * This routine may not block. 1477 * 1478 * (base + size) must be less then or equal to PAGE_SIZE. 1479 */ 1480void 1481vm_page_set_validclean(vm_page_t m, int base, int size) 1482{ 1483 int pagebits; 1484 int frag; 1485 int endoff; 1486 1487 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1488 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1489 if (size == 0) /* handle degenerate case */ 1490 return; 1491 1492 /* 1493 * If the base is not DEV_BSIZE aligned and the valid 1494 * bit is clear, we have to zero out a portion of the 1495 * first block. 1496 */ 1497 if ((frag = base & ~(DEV_BSIZE - 1)) != base && 1498 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0) 1499 pmap_zero_page_area(m, frag, base - frag); 1500 1501 /* 1502 * If the ending offset is not DEV_BSIZE aligned and the 1503 * valid bit is clear, we have to zero out a portion of 1504 * the last block. 1505 */ 1506 endoff = base + size; 1507 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff && 1508 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0) 1509 pmap_zero_page_area(m, endoff, 1510 DEV_BSIZE - (endoff & (DEV_BSIZE - 1))); 1511 1512 /* 1513 * Set valid, clear dirty bits. If validating the entire 1514 * page we can safely clear the pmap modify bit. We also 1515 * use this opportunity to clear the PG_NOSYNC flag. If a process 1516 * takes a write fault on a MAP_NOSYNC memory area the flag will 1517 * be set again. 1518 * 1519 * We set valid bits inclusive of any overlap, but we can only 1520 * clear dirty bits for DEV_BSIZE chunks that are fully within 1521 * the range. 1522 */ 1523 pagebits = vm_page_bits(base, size); 1524 m->valid |= pagebits; 1525#if 0 /* NOT YET */ 1526 if ((frag = base & (DEV_BSIZE - 1)) != 0) { 1527 frag = DEV_BSIZE - frag; 1528 base += frag; 1529 size -= frag; 1530 if (size < 0) 1531 size = 0; 1532 } 1533 pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1)); 1534#endif 1535 m->dirty &= ~pagebits; 1536 if (base == 0 && size == PAGE_SIZE) { 1537 pmap_clear_modify(m); 1538 vm_page_flag_clear(m, PG_NOSYNC); 1539 } 1540} 1541 1542void 1543vm_page_clear_dirty(vm_page_t m, int base, int size) 1544{ 1545 1546 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1547 m->dirty &= ~vm_page_bits(base, size); 1548} 1549 1550/* 1551 * vm_page_set_invalid: 1552 * 1553 * Invalidates DEV_BSIZE'd chunks within a page. Both the 1554 * valid and dirty bits for the effected areas are cleared. 1555 * 1556 * May not block. 1557 */ 1558void 1559vm_page_set_invalid(vm_page_t m, int base, int size) 1560{ 1561 int bits; 1562 1563 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1564 bits = vm_page_bits(base, size); 1565 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1566 m->valid &= ~bits; 1567 m->dirty &= ~bits; 1568 m->object->generation++; 1569} 1570 1571/* 1572 * vm_page_zero_invalid() 1573 * 1574 * The kernel assumes that the invalid portions of a page contain 1575 * garbage, but such pages can be mapped into memory by user code. 1576 * When this occurs, we must zero out the non-valid portions of the 1577 * page so user code sees what it expects. 1578 * 1579 * Pages are most often semi-valid when the end of a file is mapped 1580 * into memory and the file's size is not page aligned. 1581 */ 1582void 1583vm_page_zero_invalid(vm_page_t m, boolean_t setvalid) 1584{ 1585 int b; 1586 int i; 1587 1588 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1589 /* 1590 * Scan the valid bits looking for invalid sections that 1591 * must be zerod. Invalid sub-DEV_BSIZE'd areas ( where the 1592 * valid bit may be set ) have already been zerod by 1593 * vm_page_set_validclean(). 1594 */ 1595 for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) { 1596 if (i == (PAGE_SIZE / DEV_BSIZE) || 1597 (m->valid & (1 << i)) 1598 ) { 1599 if (i > b) { 1600 pmap_zero_page_area(m, 1601 b << DEV_BSHIFT, (i - b) << DEV_BSHIFT); 1602 } 1603 b = i + 1; 1604 } 1605 } 1606 1607 /* 1608 * setvalid is TRUE when we can safely set the zero'd areas 1609 * as being valid. We can do this if there are no cache consistancy 1610 * issues. e.g. it is ok to do with UFS, but not ok to do with NFS. 1611 */ 1612 if (setvalid) 1613 m->valid = VM_PAGE_BITS_ALL; 1614} 1615 1616/* 1617 * vm_page_is_valid: 1618 * 1619 * Is (partial) page valid? Note that the case where size == 0 1620 * will return FALSE in the degenerate case where the page is 1621 * entirely invalid, and TRUE otherwise. 1622 * 1623 * May not block. 1624 */ 1625int 1626vm_page_is_valid(vm_page_t m, int base, int size) 1627{ 1628 int bits = vm_page_bits(base, size); 1629 1630 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1631 if (m->valid && ((m->valid & bits) == bits)) 1632 return 1; 1633 else 1634 return 0; 1635} 1636 1637/* 1638 * update dirty bits from pmap/mmu. May not block. 1639 */ 1640void 1641vm_page_test_dirty(vm_page_t m) 1642{ 1643 if ((m->dirty != VM_PAGE_BITS_ALL) && pmap_is_modified(m)) { 1644 vm_page_dirty(m); 1645 } 1646} 1647 1648int so_zerocp_fullpage = 0; 1649 1650void 1651vm_page_cowfault(vm_page_t m) 1652{ 1653 vm_page_t mnew; 1654 vm_object_t object; 1655 vm_pindex_t pindex; 1656 1657 object = m->object; 1658 pindex = m->pindex; 1659 vm_page_busy(m); 1660 1661 retry_alloc: 1662 vm_page_remove(m); 1663 mnew = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL); 1664 if (mnew == NULL) { 1665 vm_page_insert(m, object, pindex); 1666 vm_page_unlock_queues(); 1667 VM_OBJECT_UNLOCK(object); 1668 VM_WAIT; 1669 VM_OBJECT_LOCK(object); 1670 vm_page_lock_queues(); 1671 goto retry_alloc; 1672 } 1673 1674 if (m->cow == 0) { 1675 /* 1676 * check to see if we raced with an xmit complete when 1677 * waiting to allocate a page. If so, put things back 1678 * the way they were 1679 */ 1680 vm_page_busy(mnew); 1681 vm_page_free(mnew); 1682 vm_page_insert(m, object, pindex); 1683 } else { /* clear COW & copy page */ 1684 if (!so_zerocp_fullpage) 1685 pmap_copy_page(m, mnew); 1686 mnew->valid = VM_PAGE_BITS_ALL; 1687 vm_page_dirty(mnew); 1688 vm_page_flag_clear(mnew, PG_BUSY); 1689 } 1690} 1691 1692void 1693vm_page_cowclear(vm_page_t m) 1694{ 1695 1696 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1697 if (m->cow) { 1698 m->cow--; 1699 /* 1700 * let vm_fault add back write permission lazily 1701 */ 1702 } 1703 /* 1704 * sf_buf_free() will free the page, so we needn't do it here 1705 */ 1706} 1707 1708void 1709vm_page_cowsetup(vm_page_t m) 1710{ 1711 1712 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1713 m->cow++; 1714 pmap_page_protect(m, VM_PROT_READ); 1715} 1716 1717#include "opt_ddb.h" 1718#ifdef DDB 1719#include <sys/kernel.h> 1720 1721#include <ddb/ddb.h> 1722 1723DB_SHOW_COMMAND(page, vm_page_print_page_info) 1724{ 1725 db_printf("cnt.v_free_count: %d\n", cnt.v_free_count); 1726 db_printf("cnt.v_cache_count: %d\n", cnt.v_cache_count); 1727 db_printf("cnt.v_inactive_count: %d\n", cnt.v_inactive_count); 1728 db_printf("cnt.v_active_count: %d\n", cnt.v_active_count); 1729 db_printf("cnt.v_wire_count: %d\n", cnt.v_wire_count); 1730 db_printf("cnt.v_free_reserved: %d\n", cnt.v_free_reserved); 1731 db_printf("cnt.v_free_min: %d\n", cnt.v_free_min); 1732 db_printf("cnt.v_free_target: %d\n", cnt.v_free_target); 1733 db_printf("cnt.v_cache_min: %d\n", cnt.v_cache_min); 1734 db_printf("cnt.v_inactive_target: %d\n", cnt.v_inactive_target); 1735} 1736 1737DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info) 1738{ 1739 int i; 1740 db_printf("PQ_FREE:"); 1741 for (i = 0; i < PQ_L2_SIZE; i++) { 1742 db_printf(" %d", vm_page_queues[PQ_FREE + i].lcnt); 1743 } 1744 db_printf("\n"); 1745 1746 db_printf("PQ_CACHE:"); 1747 for (i = 0; i < PQ_L2_SIZE; i++) { 1748 db_printf(" %d", vm_page_queues[PQ_CACHE + i].lcnt); 1749 } 1750 db_printf("\n"); 1751 1752 db_printf("PQ_ACTIVE: %d, PQ_INACTIVE: %d\n", 1753 vm_page_queues[PQ_ACTIVE].lcnt, 1754 vm_page_queues[PQ_INACTIVE].lcnt); 1755} 1756#endif /* DDB */ 1757