vm_page.c revision 124261
1/* 2 * Copyright (c) 1991 Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * The Mach Operating System project at Carnegie-Mellon University. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * from: @(#)vm_page.c 7.4 (Berkeley) 5/7/91 37 */ 38 39/* 40 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 41 * All rights reserved. 42 * 43 * Authors: Avadis Tevanian, Jr., Michael Wayne Young 44 * 45 * Permission to use, copy, modify and distribute this software and 46 * its documentation is hereby granted, provided that both the copyright 47 * notice and this permission notice appear in all copies of the 48 * software, derivative works or modified versions, and any portions 49 * thereof, and that both notices appear in supporting documentation. 50 * 51 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 52 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 53 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 54 * 55 * Carnegie Mellon requests users of this software to return to 56 * 57 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 58 * School of Computer Science 59 * Carnegie Mellon University 60 * Pittsburgh PA 15213-3890 61 * 62 * any improvements or extensions that they make and grant Carnegie the 63 * rights to redistribute these changes. 64 */ 65 66/* 67 * GENERAL RULES ON VM_PAGE MANIPULATION 68 * 69 * - a pageq mutex is required when adding or removing a page from a 70 * page queue (vm_page_queue[]), regardless of other mutexes or the 71 * busy state of a page. 72 * 73 * - a hash chain mutex is required when associating or disassociating 74 * a page from the VM PAGE CACHE hash table (vm_page_buckets), 75 * regardless of other mutexes or the busy state of a page. 76 * 77 * - either a hash chain mutex OR a busied page is required in order 78 * to modify the page flags. A hash chain mutex must be obtained in 79 * order to busy a page. A page's flags cannot be modified by a 80 * hash chain mutex if the page is marked busy. 81 * 82 * - The object memq mutex is held when inserting or removing 83 * pages from an object (vm_page_insert() or vm_page_remove()). This 84 * is different from the object's main mutex. 85 * 86 * Generally speaking, you have to be aware of side effects when running 87 * vm_page ops. A vm_page_lookup() will return with the hash chain 88 * locked, whether it was able to lookup the page or not. vm_page_free(), 89 * vm_page_cache(), vm_page_activate(), and a number of other routines 90 * will release the hash chain mutex for you. Intermediate manipulation 91 * routines such as vm_page_flag_set() expect the hash chain to be held 92 * on entry and the hash chain will remain held on return. 93 * 94 * pageq scanning can only occur with the pageq in question locked. 95 * We have a known bottleneck with the active queue, but the cache 96 * and free queues are actually arrays already. 97 */ 98 99/* 100 * Resident memory management module. 101 */ 102 103#include <sys/cdefs.h> 104__FBSDID("$FreeBSD: head/sys/vm/vm_page.c 124261 2004-01-08 20:48:26Z alc $"); 105 106#include <sys/param.h> 107#include <sys/systm.h> 108#include <sys/lock.h> 109#include <sys/malloc.h> 110#include <sys/mutex.h> 111#include <sys/proc.h> 112#include <sys/vmmeter.h> 113#include <sys/vnode.h> 114 115#include <vm/vm.h> 116#include <vm/vm_param.h> 117#include <vm/vm_kern.h> 118#include <vm/vm_object.h> 119#include <vm/vm_page.h> 120#include <vm/vm_pageout.h> 121#include <vm/vm_pager.h> 122#include <vm/vm_extern.h> 123#include <vm/uma.h> 124#include <vm/uma_int.h> 125 126/* 127 * Associated with page of user-allocatable memory is a 128 * page structure. 129 */ 130 131struct mtx vm_page_queue_mtx; 132struct mtx vm_page_queue_free_mtx; 133 134vm_page_t vm_page_array = 0; 135int vm_page_array_size = 0; 136long first_page = 0; 137int vm_page_zero_count = 0; 138 139/* 140 * vm_set_page_size: 141 * 142 * Sets the page size, perhaps based upon the memory 143 * size. Must be called before any use of page-size 144 * dependent functions. 145 */ 146void 147vm_set_page_size(void) 148{ 149 if (cnt.v_page_size == 0) 150 cnt.v_page_size = PAGE_SIZE; 151 if (((cnt.v_page_size - 1) & cnt.v_page_size) != 0) 152 panic("vm_set_page_size: page size not a power of two"); 153} 154 155/* 156 * vm_page_startup: 157 * 158 * Initializes the resident memory module. 159 * 160 * Allocates memory for the page cells, and 161 * for the object/offset-to-page hash table headers. 162 * Each page cell is initialized and placed on the free list. 163 */ 164vm_offset_t 165vm_page_startup(vm_offset_t starta, vm_offset_t enda, vm_offset_t vaddr) 166{ 167 vm_offset_t mapped; 168 vm_size_t npages; 169 vm_paddr_t page_range; 170 vm_paddr_t new_end; 171 int i; 172 vm_paddr_t pa; 173 int nblocks; 174 vm_paddr_t last_pa; 175 176 /* the biggest memory array is the second group of pages */ 177 vm_paddr_t end; 178 vm_paddr_t biggestsize; 179 int biggestone; 180 181 vm_paddr_t total; 182 vm_size_t bootpages; 183 184 total = 0; 185 biggestsize = 0; 186 biggestone = 0; 187 nblocks = 0; 188 vaddr = round_page(vaddr); 189 190 for (i = 0; phys_avail[i + 1]; i += 2) { 191 phys_avail[i] = round_page(phys_avail[i]); 192 phys_avail[i + 1] = trunc_page(phys_avail[i + 1]); 193 } 194 195 for (i = 0; phys_avail[i + 1]; i += 2) { 196 vm_paddr_t size = phys_avail[i + 1] - phys_avail[i]; 197 198 if (size > biggestsize) { 199 biggestone = i; 200 biggestsize = size; 201 } 202 ++nblocks; 203 total += size; 204 } 205 206 end = phys_avail[biggestone+1]; 207 208 /* 209 * Initialize the locks. Recursive acquisition of the vm page 210 * queue free mutex begins in contigmalloc1(). 211 */ 212 mtx_init(&vm_page_queue_mtx, "vm page queue mutex", NULL, MTX_DEF); 213 mtx_init(&vm_page_queue_free_mtx, "vm page queue free mutex", NULL, 214 MTX_RECURSE | MTX_SPIN); 215 216 /* 217 * Initialize the queue headers for the free queue, the active queue 218 * and the inactive queue. 219 */ 220 vm_pageq_init(); 221 222 /* 223 * Allocate memory for use when boot strapping the kernel memory 224 * allocator. 225 */ 226 bootpages = UMA_BOOT_PAGES * UMA_SLAB_SIZE; 227 new_end = end - bootpages; 228 new_end = trunc_page(new_end); 229 mapped = pmap_map(&vaddr, new_end, end, 230 VM_PROT_READ | VM_PROT_WRITE); 231 bzero((caddr_t) mapped, end - new_end); 232 uma_startup((caddr_t)mapped); 233 234 /* 235 * Compute the number of pages of memory that will be available for 236 * use (taking into account the overhead of a page structure per 237 * page). 238 */ 239 first_page = phys_avail[0] / PAGE_SIZE; 240 page_range = phys_avail[(nblocks - 1) * 2 + 1] / PAGE_SIZE - first_page; 241 npages = (total - (page_range * sizeof(struct vm_page)) - 242 (end - new_end)) / PAGE_SIZE; 243 end = new_end; 244 245 /* 246 * Reserve an unmapped guard page to trap access to vm_page_array[-1]. 247 */ 248 vaddr += PAGE_SIZE; 249 250 /* 251 * Initialize the mem entry structures now, and put them in the free 252 * queue. 253 */ 254 new_end = trunc_page(end - page_range * sizeof(struct vm_page)); 255 mapped = pmap_map(&vaddr, new_end, end, 256 VM_PROT_READ | VM_PROT_WRITE); 257 vm_page_array = (vm_page_t) mapped; 258 phys_avail[biggestone + 1] = new_end; 259 260 /* 261 * Clear all of the page structures 262 */ 263 bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page)); 264 vm_page_array_size = page_range; 265 266 /* 267 * Construct the free queue(s) in descending order (by physical 268 * address) so that the first 16MB of physical memory is allocated 269 * last rather than first. On large-memory machines, this avoids 270 * the exhaustion of low physical memory before isa_dmainit has run. 271 */ 272 cnt.v_page_count = 0; 273 cnt.v_free_count = 0; 274 for (i = 0; phys_avail[i + 1] && npages > 0; i += 2) { 275 pa = phys_avail[i]; 276 last_pa = phys_avail[i + 1]; 277 while (pa < last_pa && npages-- > 0) { 278 vm_pageq_add_new_page(pa); 279 pa += PAGE_SIZE; 280 } 281 } 282 return (vaddr); 283} 284 285void 286vm_page_flag_set(vm_page_t m, unsigned short bits) 287{ 288 289 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 290 m->flags |= bits; 291} 292 293void 294vm_page_flag_clear(vm_page_t m, unsigned short bits) 295{ 296 297 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 298 m->flags &= ~bits; 299} 300 301void 302vm_page_busy(vm_page_t m) 303{ 304 KASSERT((m->flags & PG_BUSY) == 0, 305 ("vm_page_busy: page already busy!!!")); 306 vm_page_flag_set(m, PG_BUSY); 307} 308 309/* 310 * vm_page_flash: 311 * 312 * wakeup anyone waiting for the page. 313 */ 314void 315vm_page_flash(vm_page_t m) 316{ 317 if (m->flags & PG_WANTED) { 318 vm_page_flag_clear(m, PG_WANTED); 319 wakeup(m); 320 } 321} 322 323/* 324 * vm_page_wakeup: 325 * 326 * clear the PG_BUSY flag and wakeup anyone waiting for the 327 * page. 328 * 329 */ 330void 331vm_page_wakeup(vm_page_t m) 332{ 333 KASSERT(m->flags & PG_BUSY, ("vm_page_wakeup: page not busy!!!")); 334 vm_page_flag_clear(m, PG_BUSY); 335 vm_page_flash(m); 336} 337 338void 339vm_page_io_start(vm_page_t m) 340{ 341 342 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 343 m->busy++; 344} 345 346void 347vm_page_io_finish(vm_page_t m) 348{ 349 350 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 351 m->busy--; 352 if (m->busy == 0) 353 vm_page_flash(m); 354} 355 356/* 357 * Keep page from being freed by the page daemon 358 * much of the same effect as wiring, except much lower 359 * overhead and should be used only for *very* temporary 360 * holding ("wiring"). 361 */ 362void 363vm_page_hold(vm_page_t mem) 364{ 365 366 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 367 mem->hold_count++; 368} 369 370void 371vm_page_unhold(vm_page_t mem) 372{ 373 374 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 375 --mem->hold_count; 376 KASSERT(mem->hold_count >= 0, ("vm_page_unhold: hold count < 0!!!")); 377 if (mem->hold_count == 0 && mem->queue == PQ_HOLD) 378 vm_page_free_toq(mem); 379} 380 381/* 382 * vm_page_free: 383 * 384 * Free a page 385 * 386 * The clearing of PG_ZERO is a temporary safety until the code can be 387 * reviewed to determine that PG_ZERO is being properly cleared on 388 * write faults or maps. PG_ZERO was previously cleared in 389 * vm_page_alloc(). 390 */ 391void 392vm_page_free(vm_page_t m) 393{ 394 vm_page_flag_clear(m, PG_ZERO); 395 vm_page_free_toq(m); 396 vm_page_zero_idle_wakeup(); 397} 398 399/* 400 * vm_page_free_zero: 401 * 402 * Free a page to the zerod-pages queue 403 */ 404void 405vm_page_free_zero(vm_page_t m) 406{ 407 vm_page_flag_set(m, PG_ZERO); 408 vm_page_free_toq(m); 409} 410 411/* 412 * vm_page_sleep_if_busy: 413 * 414 * Sleep and release the page queues lock if PG_BUSY is set or, 415 * if also_m_busy is TRUE, busy is non-zero. Returns TRUE if the 416 * thread slept and the page queues lock was released. 417 * Otherwise, retains the page queues lock and returns FALSE. 418 */ 419int 420vm_page_sleep_if_busy(vm_page_t m, int also_m_busy, const char *msg) 421{ 422 int is_object_locked; 423 424 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 425 if ((m->flags & PG_BUSY) || (also_m_busy && m->busy)) { 426 vm_page_flag_set(m, PG_WANTED | PG_REFERENCED); 427 /* 428 * Remove mtx_owned() after vm_object locking is finished. 429 */ 430 if ((is_object_locked = m->object != NULL && 431 mtx_owned(&m->object->mtx))) 432 mtx_unlock(&m->object->mtx); 433 msleep(m, &vm_page_queue_mtx, PDROP | PVM, msg, 0); 434 if (is_object_locked) 435 mtx_lock(&m->object->mtx); 436 return (TRUE); 437 } 438 return (FALSE); 439} 440 441/* 442 * vm_page_dirty: 443 * 444 * make page all dirty 445 */ 446void 447vm_page_dirty(vm_page_t m) 448{ 449 KASSERT(m->queue - m->pc != PQ_CACHE, 450 ("vm_page_dirty: page in cache!")); 451 KASSERT(m->queue - m->pc != PQ_FREE, 452 ("vm_page_dirty: page is free!")); 453 m->dirty = VM_PAGE_BITS_ALL; 454} 455 456/* 457 * vm_page_splay: 458 * 459 * Implements Sleator and Tarjan's top-down splay algorithm. Returns 460 * the vm_page containing the given pindex. If, however, that 461 * pindex is not found in the vm_object, returns a vm_page that is 462 * adjacent to the pindex, coming before or after it. 463 */ 464vm_page_t 465vm_page_splay(vm_pindex_t pindex, vm_page_t root) 466{ 467 struct vm_page dummy; 468 vm_page_t lefttreemax, righttreemin, y; 469 470 if (root == NULL) 471 return (root); 472 lefttreemax = righttreemin = &dummy; 473 for (;; root = y) { 474 if (pindex < root->pindex) { 475 if ((y = root->left) == NULL) 476 break; 477 if (pindex < y->pindex) { 478 /* Rotate right. */ 479 root->left = y->right; 480 y->right = root; 481 root = y; 482 if ((y = root->left) == NULL) 483 break; 484 } 485 /* Link into the new root's right tree. */ 486 righttreemin->left = root; 487 righttreemin = root; 488 } else if (pindex > root->pindex) { 489 if ((y = root->right) == NULL) 490 break; 491 if (pindex > y->pindex) { 492 /* Rotate left. */ 493 root->right = y->left; 494 y->left = root; 495 root = y; 496 if ((y = root->right) == NULL) 497 break; 498 } 499 /* Link into the new root's left tree. */ 500 lefttreemax->right = root; 501 lefttreemax = root; 502 } else 503 break; 504 } 505 /* Assemble the new root. */ 506 lefttreemax->right = root->left; 507 righttreemin->left = root->right; 508 root->left = dummy.right; 509 root->right = dummy.left; 510 return (root); 511} 512 513/* 514 * vm_page_insert: [ internal use only ] 515 * 516 * Inserts the given mem entry into the object and object list. 517 * 518 * The pagetables are not updated but will presumably fault the page 519 * in if necessary, or if a kernel page the caller will at some point 520 * enter the page into the kernel's pmap. We are not allowed to block 521 * here so we *can't* do this anyway. 522 * 523 * The object and page must be locked, and must be splhigh. 524 * This routine may not block. 525 */ 526void 527vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex) 528{ 529 vm_page_t root; 530 531 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 532 if (m->object != NULL) 533 panic("vm_page_insert: page already inserted"); 534 535 /* 536 * Record the object/offset pair in this page 537 */ 538 m->object = object; 539 m->pindex = pindex; 540 541 /* 542 * Now link into the object's ordered list of backed pages. 543 */ 544 root = object->root; 545 if (root == NULL) { 546 m->left = NULL; 547 m->right = NULL; 548 TAILQ_INSERT_TAIL(&object->memq, m, listq); 549 } else { 550 root = vm_page_splay(pindex, root); 551 if (pindex < root->pindex) { 552 m->left = root->left; 553 m->right = root; 554 root->left = NULL; 555 TAILQ_INSERT_BEFORE(root, m, listq); 556 } else if (pindex == root->pindex) 557 panic("vm_page_insert: offset already allocated"); 558 else { 559 m->right = root->right; 560 m->left = root; 561 root->right = NULL; 562 TAILQ_INSERT_AFTER(&object->memq, root, m, listq); 563 } 564 } 565 object->root = m; 566 object->generation++; 567 568 /* 569 * show that the object has one more resident page. 570 */ 571 object->resident_page_count++; 572 573 /* 574 * Since we are inserting a new and possibly dirty page, 575 * update the object's OBJ_WRITEABLE and OBJ_MIGHTBEDIRTY flags. 576 */ 577 if (m->flags & PG_WRITEABLE) 578 vm_object_set_writeable_dirty(object); 579} 580 581/* 582 * vm_page_remove: 583 * NOTE: used by device pager as well -wfj 584 * 585 * Removes the given mem entry from the object/offset-page 586 * table and the object page list, but do not invalidate/terminate 587 * the backing store. 588 * 589 * The object and page must be locked, and at splhigh. 590 * The underlying pmap entry (if any) is NOT removed here. 591 * This routine may not block. 592 */ 593void 594vm_page_remove(vm_page_t m) 595{ 596 vm_object_t object; 597 vm_page_t root; 598 599 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 600 if (m->object == NULL) 601 return; 602#ifndef __alpha__ 603 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 604#endif 605 if ((m->flags & PG_BUSY) == 0) { 606 panic("vm_page_remove: page not busy"); 607 } 608 609 /* 610 * Basically destroy the page. 611 */ 612 vm_page_wakeup(m); 613 614 object = m->object; 615 616 /* 617 * Now remove from the object's list of backed pages. 618 */ 619 if (m != object->root) 620 vm_page_splay(m->pindex, object->root); 621 if (m->left == NULL) 622 root = m->right; 623 else { 624 root = vm_page_splay(m->pindex, m->left); 625 root->right = m->right; 626 } 627 object->root = root; 628 TAILQ_REMOVE(&object->memq, m, listq); 629 630 /* 631 * And show that the object has one fewer resident page. 632 */ 633 object->resident_page_count--; 634 object->generation++; 635 636 m->object = NULL; 637} 638 639/* 640 * vm_page_lookup: 641 * 642 * Returns the page associated with the object/offset 643 * pair specified; if none is found, NULL is returned. 644 * 645 * The object must be locked. 646 * This routine may not block. 647 * This is a critical path routine 648 */ 649vm_page_t 650vm_page_lookup(vm_object_t object, vm_pindex_t pindex) 651{ 652 vm_page_t m; 653 654 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 655 if ((m = object->root) != NULL && m->pindex != pindex) { 656 m = vm_page_splay(pindex, m); 657 if ((object->root = m)->pindex != pindex) 658 m = NULL; 659 } 660 return (m); 661} 662 663/* 664 * vm_page_rename: 665 * 666 * Move the given memory entry from its 667 * current object to the specified target object/offset. 668 * 669 * The object must be locked. 670 * This routine may not block. 671 * 672 * Note: this routine will raise itself to splvm(), the caller need not. 673 * 674 * Note: swap associated with the page must be invalidated by the move. We 675 * have to do this for several reasons: (1) we aren't freeing the 676 * page, (2) we are dirtying the page, (3) the VM system is probably 677 * moving the page from object A to B, and will then later move 678 * the backing store from A to B and we can't have a conflict. 679 * 680 * Note: we *always* dirty the page. It is necessary both for the 681 * fact that we moved it, and because we may be invalidating 682 * swap. If the page is on the cache, we have to deactivate it 683 * or vm_page_dirty() will panic. Dirty pages are not allowed 684 * on the cache. 685 */ 686void 687vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex) 688{ 689 int s; 690 691 s = splvm(); 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 splx(s); 698} 699 700/* 701 * vm_page_select_cache: 702 * 703 * Find a page on the cache queue with color optimization. As pages 704 * might be found, but not applicable, they are deactivated. This 705 * keeps us from using potentially busy cached pages. 706 * 707 * This routine must be called at splvm(). 708 * This routine may not block. 709 */ 710vm_page_t 711vm_page_select_cache(int color) 712{ 713 vm_page_t m; 714 715 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 716 while (TRUE) { 717 m = vm_pageq_find(PQ_CACHE, color, FALSE); 718 if (m && ((m->flags & (PG_BUSY|PG_UNMANAGED)) || m->busy || 719 m->hold_count || m->wire_count || 720 (!VM_OBJECT_TRYLOCK(m->object) && 721 !VM_OBJECT_LOCKED(m->object)))) { 722 vm_page_deactivate(m); 723 continue; 724 } 725 return m; 726 } 727} 728 729/* 730 * vm_page_alloc: 731 * 732 * Allocate and return a memory cell associated 733 * with this VM object/offset pair. 734 * 735 * page_req classes: 736 * VM_ALLOC_NORMAL normal process request 737 * VM_ALLOC_SYSTEM system *really* needs a page 738 * VM_ALLOC_INTERRUPT interrupt time request 739 * VM_ALLOC_ZERO zero page 740 * 741 * This routine may not block. 742 * 743 * Additional special handling is required when called from an 744 * interrupt (VM_ALLOC_INTERRUPT). We are not allowed to mess with 745 * the page cache in this case. 746 */ 747vm_page_t 748vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req) 749{ 750 vm_object_t m_object; 751 vm_page_t m = NULL; 752 int color, flags, page_req, s; 753 754 page_req = req & VM_ALLOC_CLASS_MASK; 755 756 if ((req & VM_ALLOC_NOOBJ) == 0) { 757 KASSERT(object != NULL, 758 ("vm_page_alloc: NULL object.")); 759 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 760 color = (pindex + object->pg_color) & PQ_L2_MASK; 761 } else 762 color = pindex & PQ_L2_MASK; 763 764 /* 765 * The pager is allowed to eat deeper into the free page list. 766 */ 767 if ((curproc == pageproc) && (page_req != VM_ALLOC_INTERRUPT)) { 768 page_req = VM_ALLOC_SYSTEM; 769 }; 770 771 s = splvm(); 772loop: 773 mtx_lock_spin(&vm_page_queue_free_mtx); 774 if (cnt.v_free_count > cnt.v_free_reserved || 775 (page_req == VM_ALLOC_SYSTEM && 776 cnt.v_cache_count == 0 && 777 cnt.v_free_count > cnt.v_interrupt_free_min) || 778 (page_req == VM_ALLOC_INTERRUPT && cnt.v_free_count > 0)) { 779 /* 780 * Allocate from the free queue if the number of free pages 781 * exceeds the minimum for the request class. 782 */ 783 m = vm_pageq_find(PQ_FREE, color, (req & VM_ALLOC_ZERO) != 0); 784 } else if (page_req != VM_ALLOC_INTERRUPT) { 785 mtx_unlock_spin(&vm_page_queue_free_mtx); 786 /* 787 * Allocatable from cache (non-interrupt only). On success, 788 * we must free the page and try again, thus ensuring that 789 * cnt.v_*_free_min counters are replenished. 790 */ 791 vm_page_lock_queues(); 792 if ((m = vm_page_select_cache(color)) == NULL) { 793 vm_page_unlock_queues(); 794 splx(s); 795#if defined(DIAGNOSTIC) 796 if (cnt.v_cache_count > 0) 797 printf("vm_page_alloc(NORMAL): missing pages on cache queue: %d\n", cnt.v_cache_count); 798#endif 799 atomic_add_int(&vm_pageout_deficit, 1); 800 pagedaemon_wakeup(); 801 return (NULL); 802 } 803 KASSERT(m->dirty == 0, ("Found dirty cache page %p", m)); 804 m_object = m->object; 805 VM_OBJECT_LOCK_ASSERT(m_object, MA_OWNED); 806 vm_page_busy(m); 807 pmap_remove_all(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 splx(s); 819 atomic_add_int(&vm_pageout_deficit, 1); 820 pagedaemon_wakeup(); 821 return (NULL); 822 } 823 824 /* 825 * At this point we had better have found a good page. 826 */ 827 828 KASSERT( 829 m != NULL, 830 ("vm_page_alloc(): missing page on free queue\n") 831 ); 832 833 /* 834 * Remove from free queue 835 */ 836 837 vm_pageq_remove_nowakeup(m); 838 839 /* 840 * Initialize structure. Only the PG_ZERO flag is inherited. 841 */ 842 flags = PG_BUSY; 843 if (m->flags & PG_ZERO) { 844 vm_page_zero_count--; 845 if (req & VM_ALLOC_ZERO) 846 flags = PG_ZERO | PG_BUSY; 847 } 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 /* 862 * vm_page_insert() is safe prior to the splx(). Note also that 863 * inserting a page here does not insert it into the pmap (which 864 * could cause us to block allocating memory). We cannot block 865 * anywhere. 866 */ 867 if ((req & VM_ALLOC_NOOBJ) == 0) 868 vm_page_insert(m, object, pindex); 869 else 870 m->pindex = pindex; 871 872 /* 873 * Don't wakeup too often - wakeup the pageout daemon when 874 * we would be nearly out of memory. 875 */ 876 if (vm_paging_needed()) 877 pagedaemon_wakeup(); 878 879 splx(s); 880 return (m); 881} 882 883/* 884 * vm_wait: (also see VM_WAIT macro) 885 * 886 * Block until free pages are available for allocation 887 * - Called in various places before memory allocations. 888 */ 889void 890vm_wait(void) 891{ 892 int s; 893 894 s = splvm(); 895 vm_page_lock_queues(); 896 if (curproc == pageproc) { 897 vm_pageout_pages_needed = 1; 898 msleep(&vm_pageout_pages_needed, &vm_page_queue_mtx, 899 PDROP | PSWP, "VMWait", 0); 900 } else { 901 if (!vm_pages_needed) { 902 vm_pages_needed = 1; 903 wakeup(&vm_pages_needed); 904 } 905 msleep(&cnt.v_free_count, &vm_page_queue_mtx, PDROP | PVM, 906 "vmwait", 0); 907 } 908 splx(s); 909} 910 911/* 912 * vm_waitpfault: (also see VM_WAITPFAULT macro) 913 * 914 * Block until free pages are available for allocation 915 * - Called only in vm_fault so that processes page faulting 916 * can be easily tracked. 917 * - Sleeps at a lower priority than vm_wait() so that vm_wait()ing 918 * processes will be able to grab memory first. Do not change 919 * this balance without careful testing first. 920 */ 921void 922vm_waitpfault(void) 923{ 924 int s; 925 926 s = splvm(); 927 vm_page_lock_queues(); 928 if (!vm_pages_needed) { 929 vm_pages_needed = 1; 930 wakeup(&vm_pages_needed); 931 } 932 msleep(&cnt.v_free_count, &vm_page_queue_mtx, PDROP | PUSER, 933 "pfault", 0); 934 splx(s); 935} 936 937/* 938 * vm_page_activate: 939 * 940 * Put the specified page on the active list (if appropriate). 941 * Ensure that act_count is at least ACT_INIT but do not otherwise 942 * mess with it. 943 * 944 * The page queues must be locked. 945 * This routine may not block. 946 */ 947void 948vm_page_activate(vm_page_t m) 949{ 950 int s; 951 952 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 953 s = splvm(); 954 if (m->queue != PQ_ACTIVE) { 955 if ((m->queue - m->pc) == PQ_CACHE) 956 cnt.v_reactivated++; 957 vm_pageq_remove(m); 958 if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) { 959 if (m->act_count < ACT_INIT) 960 m->act_count = ACT_INIT; 961 vm_pageq_enqueue(PQ_ACTIVE, m); 962 } 963 } else { 964 if (m->act_count < ACT_INIT) 965 m->act_count = ACT_INIT; 966 } 967 splx(s); 968} 969 970/* 971 * vm_page_free_wakeup: 972 * 973 * Helper routine for vm_page_free_toq() and vm_page_cache(). This 974 * routine is called when a page has been added to the cache or free 975 * queues. 976 * 977 * This routine may not block. 978 * This routine must be called at splvm() 979 */ 980static __inline void 981vm_page_free_wakeup(void) 982{ 983 984 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 985 /* 986 * if pageout daemon needs pages, then tell it that there are 987 * some free. 988 */ 989 if (vm_pageout_pages_needed && 990 cnt.v_cache_count + cnt.v_free_count >= cnt.v_pageout_free_min) { 991 wakeup(&vm_pageout_pages_needed); 992 vm_pageout_pages_needed = 0; 993 } 994 /* 995 * wakeup processes that are waiting on memory if we hit a 996 * high water mark. And wakeup scheduler process if we have 997 * lots of memory. this process will swapin processes. 998 */ 999 if (vm_pages_needed && !vm_page_count_min()) { 1000 vm_pages_needed = 0; 1001 wakeup(&cnt.v_free_count); 1002 } 1003} 1004 1005/* 1006 * vm_page_free_toq: 1007 * 1008 * Returns the given page to the PQ_FREE list, 1009 * disassociating it with any VM object. 1010 * 1011 * Object and page must be locked prior to entry. 1012 * This routine may not block. 1013 */ 1014 1015void 1016vm_page_free_toq(vm_page_t m) 1017{ 1018 int s; 1019 struct vpgqueues *pq; 1020 vm_object_t object = m->object; 1021 1022 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1023 s = splvm(); 1024 cnt.v_tfree++; 1025 1026 if (m->busy || ((m->queue - m->pc) == PQ_FREE)) { 1027 printf( 1028 "vm_page_free: pindex(%lu), busy(%d), PG_BUSY(%d), hold(%d)\n", 1029 (u_long)m->pindex, m->busy, (m->flags & PG_BUSY) ? 1 : 0, 1030 m->hold_count); 1031 if ((m->queue - m->pc) == PQ_FREE) 1032 panic("vm_page_free: freeing free page"); 1033 else 1034 panic("vm_page_free: freeing busy page"); 1035 } 1036 1037 /* 1038 * unqueue, then remove page. Note that we cannot destroy 1039 * the page here because we do not want to call the pager's 1040 * callback routine until after we've put the page on the 1041 * appropriate free queue. 1042 */ 1043 vm_pageq_remove_nowakeup(m); 1044 vm_page_remove(m); 1045 1046 /* 1047 * If fictitious remove object association and 1048 * return, otherwise delay object association removal. 1049 */ 1050 if ((m->flags & PG_FICTITIOUS) != 0) { 1051 splx(s); 1052 return; 1053 } 1054 1055 m->valid = 0; 1056 vm_page_undirty(m); 1057 1058 if (m->wire_count != 0) { 1059 if (m->wire_count > 1) { 1060 panic("vm_page_free: invalid wire count (%d), pindex: 0x%lx", 1061 m->wire_count, (long)m->pindex); 1062 } 1063 panic("vm_page_free: freeing wired page\n"); 1064 } 1065 1066 /* 1067 * If we've exhausted the object's resident pages we want to free 1068 * it up. 1069 */ 1070 if (object && 1071 (object->type == OBJT_VNODE) && 1072 ((object->flags & OBJ_DEAD) == 0) 1073 ) { 1074 struct vnode *vp = (struct vnode *)object->handle; 1075 1076 if (vp) { 1077 VI_LOCK(vp); 1078 if (VSHOULDFREE(vp)) 1079 vfree(vp); 1080 VI_UNLOCK(vp); 1081 } 1082 } 1083 1084 /* 1085 * Clear the UNMANAGED flag when freeing an unmanaged page. 1086 */ 1087 if (m->flags & PG_UNMANAGED) { 1088 m->flags &= ~PG_UNMANAGED; 1089 } 1090 1091 if (m->hold_count != 0) { 1092 m->flags &= ~PG_ZERO; 1093 m->queue = PQ_HOLD; 1094 } else 1095 m->queue = PQ_FREE + m->pc; 1096 pq = &vm_page_queues[m->queue]; 1097 mtx_lock_spin(&vm_page_queue_free_mtx); 1098 pq->lcnt++; 1099 ++(*pq->cnt); 1100 1101 /* 1102 * Put zero'd pages on the end ( where we look for zero'd pages 1103 * first ) and non-zerod pages at the head. 1104 */ 1105 if (m->flags & PG_ZERO) { 1106 TAILQ_INSERT_TAIL(&pq->pl, m, pageq); 1107 ++vm_page_zero_count; 1108 } else { 1109 TAILQ_INSERT_HEAD(&pq->pl, m, pageq); 1110 } 1111 mtx_unlock_spin(&vm_page_queue_free_mtx); 1112 vm_page_free_wakeup(); 1113 splx(s); 1114} 1115 1116/* 1117 * vm_page_unmanage: 1118 * 1119 * Prevent PV management from being done on the page. The page is 1120 * removed from the paging queues as if it were wired, and as a 1121 * consequence of no longer being managed the pageout daemon will not 1122 * touch it (since there is no way to locate the pte mappings for the 1123 * page). madvise() calls that mess with the pmap will also no longer 1124 * operate on the page. 1125 * 1126 * Beyond that the page is still reasonably 'normal'. Freeing the page 1127 * will clear the flag. 1128 * 1129 * This routine is used by OBJT_PHYS objects - objects using unswappable 1130 * physical memory as backing store rather then swap-backed memory and 1131 * will eventually be extended to support 4MB unmanaged physical 1132 * mappings. 1133 */ 1134void 1135vm_page_unmanage(vm_page_t m) 1136{ 1137 int s; 1138 1139 s = splvm(); 1140 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1141 if ((m->flags & PG_UNMANAGED) == 0) { 1142 if (m->wire_count == 0) 1143 vm_pageq_remove(m); 1144 } 1145 vm_page_flag_set(m, PG_UNMANAGED); 1146 splx(s); 1147} 1148 1149/* 1150 * vm_page_wire: 1151 * 1152 * Mark this page as wired down by yet 1153 * another map, removing it from paging queues 1154 * as necessary. 1155 * 1156 * The page queues must be locked. 1157 * This routine may not block. 1158 */ 1159void 1160vm_page_wire(vm_page_t m) 1161{ 1162 int s; 1163 1164 /* 1165 * Only bump the wire statistics if the page is not already wired, 1166 * and only unqueue the page if it is on some queue (if it is unmanaged 1167 * it is already off the queues). 1168 */ 1169 s = splvm(); 1170 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1171 if (m->wire_count == 0) { 1172 if ((m->flags & PG_UNMANAGED) == 0) 1173 vm_pageq_remove(m); 1174 atomic_add_int(&cnt.v_wire_count, 1); 1175 } 1176 m->wire_count++; 1177 KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m)); 1178 splx(s); 1179} 1180 1181/* 1182 * vm_page_unwire: 1183 * 1184 * Release one wiring of this page, potentially 1185 * enabling it to be paged again. 1186 * 1187 * Many pages placed on the inactive queue should actually go 1188 * into the cache, but it is difficult to figure out which. What 1189 * we do instead, if the inactive target is well met, is to put 1190 * clean pages at the head of the inactive queue instead of the tail. 1191 * This will cause them to be moved to the cache more quickly and 1192 * if not actively re-referenced, freed more quickly. If we just 1193 * stick these pages at the end of the inactive queue, heavy filesystem 1194 * meta-data accesses can cause an unnecessary paging load on memory bound 1195 * processes. This optimization causes one-time-use metadata to be 1196 * reused more quickly. 1197 * 1198 * BUT, if we are in a low-memory situation we have no choice but to 1199 * put clean pages on the cache queue. 1200 * 1201 * A number of routines use vm_page_unwire() to guarantee that the page 1202 * will go into either the inactive or active queues, and will NEVER 1203 * be placed in the cache - for example, just after dirtying a page. 1204 * dirty pages in the cache are not allowed. 1205 * 1206 * The page queues must be locked. 1207 * This routine may not block. 1208 */ 1209void 1210vm_page_unwire(vm_page_t m, int activate) 1211{ 1212 int s; 1213 1214 s = splvm(); 1215 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1216 if (m->wire_count > 0) { 1217 m->wire_count--; 1218 if (m->wire_count == 0) { 1219 atomic_subtract_int(&cnt.v_wire_count, 1); 1220 if (m->flags & PG_UNMANAGED) { 1221 ; 1222 } else if (activate) 1223 vm_pageq_enqueue(PQ_ACTIVE, m); 1224 else { 1225 vm_page_flag_clear(m, PG_WINATCFLS); 1226 vm_pageq_enqueue(PQ_INACTIVE, m); 1227 } 1228 } 1229 } else { 1230 panic("vm_page_unwire: invalid wire count: %d\n", m->wire_count); 1231 } 1232 splx(s); 1233} 1234 1235 1236/* 1237 * Move the specified page to the inactive queue. If the page has 1238 * any associated swap, the swap is deallocated. 1239 * 1240 * Normally athead is 0 resulting in LRU operation. athead is set 1241 * to 1 if we want this page to be 'as if it were placed in the cache', 1242 * except without unmapping it from the process address space. 1243 * 1244 * This routine may not block. 1245 */ 1246static __inline void 1247_vm_page_deactivate(vm_page_t m, int athead) 1248{ 1249 int s; 1250 1251 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1252 /* 1253 * Ignore if already inactive. 1254 */ 1255 if (m->queue == PQ_INACTIVE) 1256 return; 1257 1258 s = splvm(); 1259 if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) { 1260 if ((m->queue - m->pc) == PQ_CACHE) 1261 cnt.v_reactivated++; 1262 vm_page_flag_clear(m, PG_WINATCFLS); 1263 vm_pageq_remove(m); 1264 if (athead) 1265 TAILQ_INSERT_HEAD(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 1266 else 1267 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 1268 m->queue = PQ_INACTIVE; 1269 vm_page_queues[PQ_INACTIVE].lcnt++; 1270 cnt.v_inactive_count++; 1271 } 1272 splx(s); 1273} 1274 1275void 1276vm_page_deactivate(vm_page_t m) 1277{ 1278 _vm_page_deactivate(m, 0); 1279} 1280 1281/* 1282 * vm_page_try_to_cache: 1283 * 1284 * Returns 0 on failure, 1 on success 1285 */ 1286int 1287vm_page_try_to_cache(vm_page_t m) 1288{ 1289 1290 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1291 if (m->dirty || m->hold_count || m->busy || m->wire_count || 1292 (m->flags & (PG_BUSY|PG_UNMANAGED))) { 1293 return (0); 1294 } 1295 vm_page_test_dirty(m); 1296 if (m->dirty) 1297 return (0); 1298 vm_page_cache(m); 1299 return (1); 1300} 1301 1302/* 1303 * vm_page_try_to_free() 1304 * 1305 * Attempt to free the page. If we cannot free it, we do nothing. 1306 * 1 is returned on success, 0 on failure. 1307 */ 1308int 1309vm_page_try_to_free(vm_page_t m) 1310{ 1311 1312 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1313 if (m->object != NULL) 1314 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1315 if (m->dirty || m->hold_count || m->busy || m->wire_count || 1316 (m->flags & (PG_BUSY|PG_UNMANAGED))) { 1317 return (0); 1318 } 1319 vm_page_test_dirty(m); 1320 if (m->dirty) 1321 return (0); 1322 vm_page_busy(m); 1323 pmap_remove_all(m); 1324 vm_page_free(m); 1325 return (1); 1326} 1327 1328/* 1329 * vm_page_cache 1330 * 1331 * Put the specified page onto the page cache queue (if appropriate). 1332 * 1333 * This routine may not block. 1334 */ 1335void 1336vm_page_cache(vm_page_t m) 1337{ 1338 int s; 1339 1340 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1341 if ((m->flags & (PG_BUSY|PG_UNMANAGED)) || m->busy || 1342 m->hold_count || m->wire_count) { 1343 printf("vm_page_cache: attempting to cache busy page\n"); 1344 return; 1345 } 1346 if ((m->queue - m->pc) == PQ_CACHE) 1347 return; 1348 1349 /* 1350 * Remove all pmaps and indicate that the page is not 1351 * writeable or mapped. 1352 */ 1353 pmap_remove_all(m); 1354 if (m->dirty != 0) { 1355 panic("vm_page_cache: caching a dirty page, pindex: %ld", 1356 (long)m->pindex); 1357 } 1358 s = splvm(); 1359 vm_pageq_remove_nowakeup(m); 1360 vm_pageq_enqueue(PQ_CACHE + m->pc, m); 1361 vm_page_free_wakeup(); 1362 splx(s); 1363} 1364 1365/* 1366 * vm_page_dontneed 1367 * 1368 * Cache, deactivate, or do nothing as appropriate. This routine 1369 * is typically used by madvise() MADV_DONTNEED. 1370 * 1371 * Generally speaking we want to move the page into the cache so 1372 * it gets reused quickly. However, this can result in a silly syndrome 1373 * due to the page recycling too quickly. Small objects will not be 1374 * fully cached. On the otherhand, if we move the page to the inactive 1375 * queue we wind up with a problem whereby very large objects 1376 * unnecessarily blow away our inactive and cache queues. 1377 * 1378 * The solution is to move the pages based on a fixed weighting. We 1379 * either leave them alone, deactivate them, or move them to the cache, 1380 * where moving them to the cache has the highest weighting. 1381 * By forcing some pages into other queues we eventually force the 1382 * system to balance the queues, potentially recovering other unrelated 1383 * space from active. The idea is to not force this to happen too 1384 * often. 1385 */ 1386void 1387vm_page_dontneed(vm_page_t m) 1388{ 1389 static int dnweight; 1390 int dnw; 1391 int head; 1392 1393 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1394 dnw = ++dnweight; 1395 1396 /* 1397 * occassionally leave the page alone 1398 */ 1399 if ((dnw & 0x01F0) == 0 || 1400 m->queue == PQ_INACTIVE || 1401 m->queue - m->pc == PQ_CACHE 1402 ) { 1403 if (m->act_count >= ACT_INIT) 1404 --m->act_count; 1405 return; 1406 } 1407 1408 if (m->dirty == 0) 1409 vm_page_test_dirty(m); 1410 1411 if (m->dirty || (dnw & 0x0070) == 0) { 1412 /* 1413 * Deactivate the page 3 times out of 32. 1414 */ 1415 head = 0; 1416 } else { 1417 /* 1418 * Cache the page 28 times out of every 32. Note that 1419 * the page is deactivated instead of cached, but placed 1420 * at the head of the queue instead of the tail. 1421 */ 1422 head = 1; 1423 } 1424 _vm_page_deactivate(m, head); 1425} 1426 1427/* 1428 * Grab a page, waiting until we are waken up due to the page 1429 * changing state. We keep on waiting, if the page continues 1430 * to be in the object. If the page doesn't exist, allocate it. 1431 * 1432 * This routine may block. 1433 */ 1434vm_page_t 1435vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags) 1436{ 1437 vm_page_t m; 1438 1439 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1440retrylookup: 1441 if ((m = vm_page_lookup(object, pindex)) != NULL) { 1442 vm_page_lock_queues(); 1443 if (m->busy || (m->flags & PG_BUSY)) { 1444 vm_page_flag_set(m, PG_WANTED | PG_REFERENCED); 1445 VM_OBJECT_UNLOCK(object); 1446 msleep(m, &vm_page_queue_mtx, PDROP | PVM, "pgrbwt", 0); 1447 VM_OBJECT_LOCK(object); 1448 if ((allocflags & VM_ALLOC_RETRY) == 0) 1449 return (NULL); 1450 goto retrylookup; 1451 } else { 1452 if (allocflags & VM_ALLOC_WIRED) 1453 vm_page_wire(m); 1454 vm_page_busy(m); 1455 vm_page_unlock_queues(); 1456 return m; 1457 } 1458 } 1459 1460 m = vm_page_alloc(object, pindex, allocflags & ~VM_ALLOC_RETRY); 1461 if (m == NULL) { 1462 VM_OBJECT_UNLOCK(object); 1463 VM_WAIT; 1464 VM_OBJECT_LOCK(object); 1465 if ((allocflags & VM_ALLOC_RETRY) == 0) 1466 return NULL; 1467 goto retrylookup; 1468 } 1469 1470 return m; 1471} 1472 1473/* 1474 * Mapping function for valid bits or for dirty bits in 1475 * a page. May not block. 1476 * 1477 * Inputs are required to range within a page. 1478 */ 1479__inline int 1480vm_page_bits(int base, int size) 1481{ 1482 int first_bit; 1483 int last_bit; 1484 1485 KASSERT( 1486 base + size <= PAGE_SIZE, 1487 ("vm_page_bits: illegal base/size %d/%d", base, size) 1488 ); 1489 1490 if (size == 0) /* handle degenerate case */ 1491 return (0); 1492 1493 first_bit = base >> DEV_BSHIFT; 1494 last_bit = (base + size - 1) >> DEV_BSHIFT; 1495 1496 return ((2 << last_bit) - (1 << first_bit)); 1497} 1498 1499/* 1500 * vm_page_set_validclean: 1501 * 1502 * Sets portions of a page valid and clean. The arguments are expected 1503 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive 1504 * of any partial chunks touched by the range. The invalid portion of 1505 * such chunks will be zero'd. 1506 * 1507 * This routine may not block. 1508 * 1509 * (base + size) must be less then or equal to PAGE_SIZE. 1510 */ 1511void 1512vm_page_set_validclean(vm_page_t m, int base, int size) 1513{ 1514 int pagebits; 1515 int frag; 1516 int endoff; 1517 1518 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1519 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1520 if (size == 0) /* handle degenerate case */ 1521 return; 1522 1523 /* 1524 * If the base is not DEV_BSIZE aligned and the valid 1525 * bit is clear, we have to zero out a portion of the 1526 * first block. 1527 */ 1528 if ((frag = base & ~(DEV_BSIZE - 1)) != base && 1529 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0) 1530 pmap_zero_page_area(m, frag, base - frag); 1531 1532 /* 1533 * If the ending offset is not DEV_BSIZE aligned and the 1534 * valid bit is clear, we have to zero out a portion of 1535 * the last block. 1536 */ 1537 endoff = base + size; 1538 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff && 1539 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0) 1540 pmap_zero_page_area(m, endoff, 1541 DEV_BSIZE - (endoff & (DEV_BSIZE - 1))); 1542 1543 /* 1544 * Set valid, clear dirty bits. If validating the entire 1545 * page we can safely clear the pmap modify bit. We also 1546 * use this opportunity to clear the PG_NOSYNC flag. If a process 1547 * takes a write fault on a MAP_NOSYNC memory area the flag will 1548 * be set again. 1549 * 1550 * We set valid bits inclusive of any overlap, but we can only 1551 * clear dirty bits for DEV_BSIZE chunks that are fully within 1552 * the range. 1553 */ 1554 pagebits = vm_page_bits(base, size); 1555 m->valid |= pagebits; 1556#if 0 /* NOT YET */ 1557 if ((frag = base & (DEV_BSIZE - 1)) != 0) { 1558 frag = DEV_BSIZE - frag; 1559 base += frag; 1560 size -= frag; 1561 if (size < 0) 1562 size = 0; 1563 } 1564 pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1)); 1565#endif 1566 m->dirty &= ~pagebits; 1567 if (base == 0 && size == PAGE_SIZE) { 1568 pmap_clear_modify(m); 1569 vm_page_flag_clear(m, PG_NOSYNC); 1570 } 1571} 1572 1573void 1574vm_page_clear_dirty(vm_page_t m, int base, int size) 1575{ 1576 1577 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1578 m->dirty &= ~vm_page_bits(base, size); 1579} 1580 1581/* 1582 * vm_page_set_invalid: 1583 * 1584 * Invalidates DEV_BSIZE'd chunks within a page. Both the 1585 * valid and dirty bits for the effected areas are cleared. 1586 * 1587 * May not block. 1588 */ 1589void 1590vm_page_set_invalid(vm_page_t m, int base, int size) 1591{ 1592 int bits; 1593 1594 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1595 bits = vm_page_bits(base, size); 1596 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1597 m->valid &= ~bits; 1598 m->dirty &= ~bits; 1599 m->object->generation++; 1600} 1601 1602/* 1603 * vm_page_zero_invalid() 1604 * 1605 * The kernel assumes that the invalid portions of a page contain 1606 * garbage, but such pages can be mapped into memory by user code. 1607 * When this occurs, we must zero out the non-valid portions of the 1608 * page so user code sees what it expects. 1609 * 1610 * Pages are most often semi-valid when the end of a file is mapped 1611 * into memory and the file's size is not page aligned. 1612 */ 1613void 1614vm_page_zero_invalid(vm_page_t m, boolean_t setvalid) 1615{ 1616 int b; 1617 int i; 1618 1619 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1620 /* 1621 * Scan the valid bits looking for invalid sections that 1622 * must be zerod. Invalid sub-DEV_BSIZE'd areas ( where the 1623 * valid bit may be set ) have already been zerod by 1624 * vm_page_set_validclean(). 1625 */ 1626 for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) { 1627 if (i == (PAGE_SIZE / DEV_BSIZE) || 1628 (m->valid & (1 << i)) 1629 ) { 1630 if (i > b) { 1631 pmap_zero_page_area(m, 1632 b << DEV_BSHIFT, (i - b) << DEV_BSHIFT); 1633 } 1634 b = i + 1; 1635 } 1636 } 1637 1638 /* 1639 * setvalid is TRUE when we can safely set the zero'd areas 1640 * as being valid. We can do this if there are no cache consistancy 1641 * issues. e.g. it is ok to do with UFS, but not ok to do with NFS. 1642 */ 1643 if (setvalid) 1644 m->valid = VM_PAGE_BITS_ALL; 1645} 1646 1647/* 1648 * vm_page_is_valid: 1649 * 1650 * Is (partial) page valid? Note that the case where size == 0 1651 * will return FALSE in the degenerate case where the page is 1652 * entirely invalid, and TRUE otherwise. 1653 * 1654 * May not block. 1655 */ 1656int 1657vm_page_is_valid(vm_page_t m, int base, int size) 1658{ 1659 int bits = vm_page_bits(base, size); 1660 1661 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1662 if (m->valid && ((m->valid & bits) == bits)) 1663 return 1; 1664 else 1665 return 0; 1666} 1667 1668/* 1669 * update dirty bits from pmap/mmu. May not block. 1670 */ 1671void 1672vm_page_test_dirty(vm_page_t m) 1673{ 1674 if ((m->dirty != VM_PAGE_BITS_ALL) && pmap_is_modified(m)) { 1675 vm_page_dirty(m); 1676 } 1677} 1678 1679int so_zerocp_fullpage = 0; 1680 1681void 1682vm_page_cowfault(vm_page_t m) 1683{ 1684 vm_page_t mnew; 1685 vm_object_t object; 1686 vm_pindex_t pindex; 1687 1688 object = m->object; 1689 pindex = m->pindex; 1690 vm_page_busy(m); 1691 1692 retry_alloc: 1693 vm_page_remove(m); 1694 /* 1695 * An interrupt allocation is requested because the page 1696 * queues lock is held. 1697 */ 1698 mnew = vm_page_alloc(object, pindex, VM_ALLOC_INTERRUPT); 1699 if (mnew == NULL) { 1700 vm_page_insert(m, object, pindex); 1701 vm_page_unlock_queues(); 1702 VM_OBJECT_UNLOCK(object); 1703 VM_WAIT; 1704 VM_OBJECT_LOCK(object); 1705 vm_page_lock_queues(); 1706 goto retry_alloc; 1707 } 1708 1709 if (m->cow == 0) { 1710 /* 1711 * check to see if we raced with an xmit complete when 1712 * waiting to allocate a page. If so, put things back 1713 * the way they were 1714 */ 1715 vm_page_busy(mnew); 1716 vm_page_free(mnew); 1717 vm_page_insert(m, object, pindex); 1718 } else { /* clear COW & copy page */ 1719 if (!so_zerocp_fullpage) 1720 pmap_copy_page(m, mnew); 1721 mnew->valid = VM_PAGE_BITS_ALL; 1722 vm_page_dirty(mnew); 1723 vm_page_flag_clear(mnew, PG_BUSY); 1724 } 1725} 1726 1727void 1728vm_page_cowclear(vm_page_t m) 1729{ 1730 1731 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1732 if (m->cow) { 1733 m->cow--; 1734 /* 1735 * let vm_fault add back write permission lazily 1736 */ 1737 } 1738 /* 1739 * sf_buf_free() will free the page, so we needn't do it here 1740 */ 1741} 1742 1743void 1744vm_page_cowsetup(vm_page_t m) 1745{ 1746 1747 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1748 m->cow++; 1749 pmap_page_protect(m, VM_PROT_READ); 1750} 1751 1752#include "opt_ddb.h" 1753#ifdef DDB 1754#include <sys/kernel.h> 1755 1756#include <ddb/ddb.h> 1757 1758DB_SHOW_COMMAND(page, vm_page_print_page_info) 1759{ 1760 db_printf("cnt.v_free_count: %d\n", cnt.v_free_count); 1761 db_printf("cnt.v_cache_count: %d\n", cnt.v_cache_count); 1762 db_printf("cnt.v_inactive_count: %d\n", cnt.v_inactive_count); 1763 db_printf("cnt.v_active_count: %d\n", cnt.v_active_count); 1764 db_printf("cnt.v_wire_count: %d\n", cnt.v_wire_count); 1765 db_printf("cnt.v_free_reserved: %d\n", cnt.v_free_reserved); 1766 db_printf("cnt.v_free_min: %d\n", cnt.v_free_min); 1767 db_printf("cnt.v_free_target: %d\n", cnt.v_free_target); 1768 db_printf("cnt.v_cache_min: %d\n", cnt.v_cache_min); 1769 db_printf("cnt.v_inactive_target: %d\n", cnt.v_inactive_target); 1770} 1771 1772DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info) 1773{ 1774 int i; 1775 db_printf("PQ_FREE:"); 1776 for (i = 0; i < PQ_L2_SIZE; i++) { 1777 db_printf(" %d", vm_page_queues[PQ_FREE + i].lcnt); 1778 } 1779 db_printf("\n"); 1780 1781 db_printf("PQ_CACHE:"); 1782 for (i = 0; i < PQ_L2_SIZE; i++) { 1783 db_printf(" %d", vm_page_queues[PQ_CACHE + i].lcnt); 1784 } 1785 db_printf("\n"); 1786 1787 db_printf("PQ_ACTIVE: %d, PQ_INACTIVE: %d\n", 1788 vm_page_queues[PQ_ACTIVE].lcnt, 1789 vm_page_queues[PQ_INACTIVE].lcnt); 1790} 1791#endif /* DDB */ 1792