vm_page.c revision 116226
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 116226 2003-06-11 23:50:51Z obrien $"); 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. 210 */ 211 mtx_init(&vm_page_queue_mtx, "vm page queue mutex", NULL, MTX_DEF); 212 mtx_init(&vm_page_queue_free_mtx, "vm page queue free mutex", NULL, 213 MTX_SPIN); 214 215 /* 216 * Initialize the queue headers for the free queue, the active queue 217 * and the inactive queue. 218 */ 219 vm_pageq_init(); 220 221 /* 222 * Allocate memory for use when boot strapping the kernel memory 223 * allocator. 224 */ 225 bootpages = UMA_BOOT_PAGES * UMA_SLAB_SIZE; 226 new_end = end - bootpages; 227 new_end = trunc_page(new_end); 228 mapped = pmap_map(&vaddr, new_end, end, 229 VM_PROT_READ | VM_PROT_WRITE); 230 bzero((caddr_t) mapped, end - new_end); 231 uma_startup((caddr_t)mapped); 232 233 /* 234 * Compute the number of pages of memory that will be available for 235 * use (taking into account the overhead of a page structure per 236 * page). 237 */ 238 first_page = phys_avail[0] / PAGE_SIZE; 239 page_range = phys_avail[(nblocks - 1) * 2 + 1] / PAGE_SIZE - first_page; 240 npages = (total - (page_range * sizeof(struct vm_page)) - 241 (end - new_end)) / PAGE_SIZE; 242 end = new_end; 243 244 /* 245 * Initialize the mem entry structures now, and put them in the free 246 * queue. 247 */ 248 new_end = trunc_page(end - page_range * sizeof(struct vm_page)); 249 mapped = pmap_map(&vaddr, new_end, end, 250 VM_PROT_READ | VM_PROT_WRITE); 251 vm_page_array = (vm_page_t) mapped; 252 phys_avail[biggestone + 1] = new_end; 253 254 /* 255 * Clear all of the page structures 256 */ 257 bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page)); 258 vm_page_array_size = page_range; 259 260 /* 261 * Construct the free queue(s) in descending order (by physical 262 * address) so that the first 16MB of physical memory is allocated 263 * last rather than first. On large-memory machines, this avoids 264 * the exhaustion of low physical memory before isa_dmainit has run. 265 */ 266 cnt.v_page_count = 0; 267 cnt.v_free_count = 0; 268 for (i = 0; phys_avail[i + 1] && npages > 0; i += 2) { 269 pa = phys_avail[i]; 270 last_pa = phys_avail[i + 1]; 271 while (pa < last_pa && npages-- > 0) { 272 vm_pageq_add_new_page(pa); 273 pa += PAGE_SIZE; 274 } 275 } 276 return (vaddr); 277} 278 279void 280vm_page_flag_set(vm_page_t m, unsigned short bits) 281{ 282 283 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 284 m->flags |= bits; 285} 286 287void 288vm_page_flag_clear(vm_page_t m, unsigned short bits) 289{ 290 291 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 292 m->flags &= ~bits; 293} 294 295void 296vm_page_busy(vm_page_t m) 297{ 298 KASSERT((m->flags & PG_BUSY) == 0, 299 ("vm_page_busy: page already busy!!!")); 300 vm_page_flag_set(m, PG_BUSY); 301} 302 303/* 304 * vm_page_flash: 305 * 306 * wakeup anyone waiting for the page. 307 */ 308void 309vm_page_flash(vm_page_t m) 310{ 311 if (m->flags & PG_WANTED) { 312 vm_page_flag_clear(m, PG_WANTED); 313 wakeup(m); 314 } 315} 316 317/* 318 * vm_page_wakeup: 319 * 320 * clear the PG_BUSY flag and wakeup anyone waiting for the 321 * page. 322 * 323 */ 324void 325vm_page_wakeup(vm_page_t m) 326{ 327 KASSERT(m->flags & PG_BUSY, ("vm_page_wakeup: page not busy!!!")); 328 vm_page_flag_clear(m, PG_BUSY); 329 vm_page_flash(m); 330} 331 332void 333vm_page_io_start(vm_page_t m) 334{ 335 336 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 337 m->busy++; 338} 339 340void 341vm_page_io_finish(vm_page_t m) 342{ 343 344 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 345 m->busy--; 346 if (m->busy == 0) 347 vm_page_flash(m); 348} 349 350/* 351 * Keep page from being freed by the page daemon 352 * much of the same effect as wiring, except much lower 353 * overhead and should be used only for *very* temporary 354 * holding ("wiring"). 355 */ 356void 357vm_page_hold(vm_page_t mem) 358{ 359 360 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 361 mem->hold_count++; 362} 363 364void 365vm_page_unhold(vm_page_t mem) 366{ 367 368 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 369 --mem->hold_count; 370 KASSERT(mem->hold_count >= 0, ("vm_page_unhold: hold count < 0!!!")); 371 if (mem->hold_count == 0 && mem->queue == PQ_HOLD) 372 vm_page_free_toq(mem); 373} 374 375/* 376 * vm_page_copy: 377 * 378 * Copy one page to another 379 */ 380void 381vm_page_copy(vm_page_t src_m, vm_page_t dest_m) 382{ 383 pmap_copy_page(src_m, dest_m); 384 dest_m->valid = VM_PAGE_BITS_ALL; 385} 386 387/* 388 * vm_page_free: 389 * 390 * Free a page 391 * 392 * The clearing of PG_ZERO is a temporary safety until the code can be 393 * reviewed to determine that PG_ZERO is being properly cleared on 394 * write faults or maps. PG_ZERO was previously cleared in 395 * vm_page_alloc(). 396 */ 397void 398vm_page_free(vm_page_t m) 399{ 400 vm_page_flag_clear(m, PG_ZERO); 401 vm_page_free_toq(m); 402 vm_page_zero_idle_wakeup(); 403} 404 405/* 406 * vm_page_free_zero: 407 * 408 * Free a page to the zerod-pages queue 409 */ 410void 411vm_page_free_zero(vm_page_t m) 412{ 413 vm_page_flag_set(m, PG_ZERO); 414 vm_page_free_toq(m); 415} 416 417/* 418 * vm_page_sleep_if_busy: 419 * 420 * Sleep and release the page queues lock if PG_BUSY is set or, 421 * if also_m_busy is TRUE, busy is non-zero. Returns TRUE if the 422 * thread slept and the page queues lock was released. 423 * Otherwise, retains the page queues lock and returns FALSE. 424 */ 425int 426vm_page_sleep_if_busy(vm_page_t m, int also_m_busy, const char *msg) 427{ 428 int is_object_locked; 429 430 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 431 if ((m->flags & PG_BUSY) || (also_m_busy && m->busy)) { 432 vm_page_flag_set(m, PG_WANTED | PG_REFERENCED); 433 /* 434 * Remove mtx_owned() after vm_object locking is finished. 435 */ 436 if ((is_object_locked = m->object != NULL && 437 mtx_owned(&m->object->mtx))) 438 mtx_unlock(&m->object->mtx); 439 msleep(m, &vm_page_queue_mtx, PDROP | PVM, msg, 0); 440 if (is_object_locked) 441 mtx_lock(&m->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, and must be splhigh. 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 if (!VM_OBJECT_LOCKED(object)) 538 GIANT_REQUIRED; 539 if (m->object != NULL) 540 panic("vm_page_insert: already inserted"); 541 542 /* 543 * Record the object/offset pair in this page 544 */ 545 m->object = object; 546 m->pindex = pindex; 547 548 /* 549 * Now link into the object's ordered list of backed pages. 550 */ 551 root = object->root; 552 if (root == NULL) { 553 m->left = NULL; 554 m->right = NULL; 555 TAILQ_INSERT_TAIL(&object->memq, m, listq); 556 } else { 557 root = vm_page_splay(pindex, root); 558 if (pindex < root->pindex) { 559 m->left = root->left; 560 m->right = root; 561 root->left = NULL; 562 TAILQ_INSERT_BEFORE(root, m, listq); 563 } else { 564 m->right = root->right; 565 m->left = root; 566 root->right = NULL; 567 TAILQ_INSERT_AFTER(&object->memq, root, m, listq); 568 } 569 } 570 object->root = m; 571 object->generation++; 572 573 /* 574 * show that the object has one more resident page. 575 */ 576 object->resident_page_count++; 577 578 /* 579 * Since we are inserting a new and possibly dirty page, 580 * update the object's OBJ_WRITEABLE and OBJ_MIGHTBEDIRTY flags. 581 */ 582 if (m->flags & PG_WRITEABLE) 583 vm_object_set_writeable_dirty(object); 584} 585 586/* 587 * vm_page_remove: 588 * NOTE: used by device pager as well -wfj 589 * 590 * Removes the given mem entry from the object/offset-page 591 * table and the object page list, but do not invalidate/terminate 592 * the backing store. 593 * 594 * The object and page must be locked, and at splhigh. 595 * The underlying pmap entry (if any) is NOT removed here. 596 * This routine may not block. 597 */ 598void 599vm_page_remove(vm_page_t m) 600{ 601 vm_object_t object; 602 vm_page_t root; 603 604 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 605 if (m->object == NULL) 606 return; 607 if (!VM_OBJECT_LOCKED(m->object)) 608 GIANT_REQUIRED; 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 if (!VM_OBJECT_LOCKED(object)) 659 GIANT_REQUIRED; 660 m = vm_page_splay(pindex, object->root); 661 if ((object->root = m) != NULL && m->pindex != pindex) 662 m = NULL; 663 return (m); 664} 665 666/* 667 * vm_page_rename: 668 * 669 * Move the given memory entry from its 670 * current object to the specified target object/offset. 671 * 672 * The object must be locked. 673 * This routine may not block. 674 * 675 * Note: this routine will raise itself to splvm(), the caller need not. 676 * 677 * Note: swap associated with the page must be invalidated by the move. We 678 * have to do this for several reasons: (1) we aren't freeing the 679 * page, (2) we are dirtying the page, (3) the VM system is probably 680 * moving the page from object A to B, and will then later move 681 * the backing store from A to B and we can't have a conflict. 682 * 683 * Note: we *always* dirty the page. It is necessary both for the 684 * fact that we moved it, and because we may be invalidating 685 * swap. If the page is on the cache, we have to deactivate it 686 * or vm_page_dirty() will panic. Dirty pages are not allowed 687 * on the cache. 688 */ 689void 690vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex) 691{ 692 int s; 693 694 s = splvm(); 695 vm_page_remove(m); 696 vm_page_insert(m, new_object, new_pindex); 697 if (m->queue - m->pc == PQ_CACHE) 698 vm_page_deactivate(m); 699 vm_page_dirty(m); 700 splx(s); 701} 702 703/* 704 * vm_page_select_cache: 705 * 706 * Find a page on the cache queue with color optimization. As pages 707 * might be found, but not applicable, they are deactivated. This 708 * keeps us from using potentially busy cached pages. 709 * 710 * This routine must be called at splvm(). 711 * This routine may not block. 712 */ 713static vm_page_t 714vm_page_select_cache(vm_pindex_t color) 715{ 716 vm_page_t m; 717 718 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 719 while (TRUE) { 720 m = vm_pageq_find(PQ_CACHE, color & PQ_L2_MASK, FALSE); 721 if (m && ((m->flags & (PG_BUSY|PG_UNMANAGED)) || m->busy || 722 m->hold_count || m->wire_count)) { 723 vm_page_deactivate(m); 724 continue; 725 } 726 return m; 727 } 728} 729 730/* 731 * vm_page_select_free: 732 * 733 * Find a free or zero page, with specified preference. 734 * 735 * This routine must be called at splvm(). 736 * This routine may not block. 737 */ 738static __inline vm_page_t 739vm_page_select_free(vm_pindex_t color, boolean_t prefer_zero) 740{ 741 vm_page_t m; 742 743 m = vm_pageq_find(PQ_FREE, color & PQ_L2_MASK, prefer_zero); 744 return (m); 745} 746 747/* 748 * vm_page_alloc: 749 * 750 * Allocate and return a memory cell associated 751 * with this VM object/offset pair. 752 * 753 * page_req classes: 754 * VM_ALLOC_NORMAL normal process request 755 * VM_ALLOC_SYSTEM system *really* needs a page 756 * VM_ALLOC_INTERRUPT interrupt time request 757 * VM_ALLOC_ZERO zero page 758 * 759 * This routine may not block. 760 * 761 * Additional special handling is required when called from an 762 * interrupt (VM_ALLOC_INTERRUPT). We are not allowed to mess with 763 * the page cache in this case. 764 */ 765vm_page_t 766vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req) 767{ 768 vm_page_t m = NULL; 769 vm_pindex_t color; 770 int flags, page_req, s; 771 772 page_req = req & VM_ALLOC_CLASS_MASK; 773 774 if ((req & VM_ALLOC_NOOBJ) == 0) { 775 KASSERT(object != NULL, 776 ("vm_page_alloc: NULL object.")); 777 KASSERT(!vm_page_lookup(object, pindex), 778 ("vm_page_alloc: page already allocated")); 779 color = pindex + object->pg_color; 780 } else 781 color = pindex; 782 783 /* 784 * The pager is allowed to eat deeper into the free page list. 785 */ 786 if ((curproc == pageproc) && (page_req != VM_ALLOC_INTERRUPT)) { 787 page_req = VM_ALLOC_SYSTEM; 788 }; 789 790 s = splvm(); 791loop: 792 mtx_lock_spin(&vm_page_queue_free_mtx); 793 if (cnt.v_free_count > cnt.v_free_reserved || 794 (page_req == VM_ALLOC_SYSTEM && 795 cnt.v_cache_count == 0 && 796 cnt.v_free_count > cnt.v_interrupt_free_min) || 797 (page_req == VM_ALLOC_INTERRUPT && cnt.v_free_count > 0)) { 798 /* 799 * Allocate from the free queue if the number of free pages 800 * exceeds the minimum for the request class. 801 */ 802 m = vm_page_select_free(color, (req & VM_ALLOC_ZERO) != 0); 803 } else if (page_req != VM_ALLOC_INTERRUPT) { 804 mtx_unlock_spin(&vm_page_queue_free_mtx); 805 /* 806 * Allocatable from cache (non-interrupt only). On success, 807 * we must free the page and try again, thus ensuring that 808 * cnt.v_*_free_min counters are replenished. 809 */ 810 vm_page_lock_queues(); 811 if ((m = vm_page_select_cache(color)) == NULL) { 812 vm_page_unlock_queues(); 813 splx(s); 814#if defined(DIAGNOSTIC) 815 if (cnt.v_cache_count > 0) 816 printf("vm_page_alloc(NORMAL): missing pages on cache queue: %d\n", cnt.v_cache_count); 817#endif 818 atomic_add_int(&vm_pageout_deficit, 1); 819 pagedaemon_wakeup(); 820 return (NULL); 821 } 822 KASSERT(m->dirty == 0, ("Found dirty cache page %p", m)); 823 vm_page_busy(m); 824 pmap_remove_all(m); 825 vm_page_free(m); 826 vm_page_unlock_queues(); 827 goto loop; 828 } else { 829 /* 830 * Not allocatable from cache from interrupt, give up. 831 */ 832 mtx_unlock_spin(&vm_page_queue_free_mtx); 833 splx(s); 834 atomic_add_int(&vm_pageout_deficit, 1); 835 pagedaemon_wakeup(); 836 return (NULL); 837 } 838 839 /* 840 * At this point we had better have found a good page. 841 */ 842 843 KASSERT( 844 m != NULL, 845 ("vm_page_alloc(): missing page on free queue\n") 846 ); 847 848 /* 849 * Remove from free queue 850 */ 851 852 vm_pageq_remove_nowakeup(m); 853 854 /* 855 * Initialize structure. Only the PG_ZERO flag is inherited. 856 */ 857 flags = PG_BUSY; 858 if (m->flags & PG_ZERO) { 859 vm_page_zero_count--; 860 if (req & VM_ALLOC_ZERO) 861 flags = PG_ZERO | PG_BUSY; 862 } 863 m->flags = flags; 864 if (req & VM_ALLOC_WIRED) { 865 atomic_add_int(&cnt.v_wire_count, 1); 866 m->wire_count = 1; 867 } else 868 m->wire_count = 0; 869 m->hold_count = 0; 870 m->act_count = 0; 871 m->busy = 0; 872 m->valid = 0; 873 KASSERT(m->dirty == 0, ("vm_page_alloc: free/cache page %p was dirty", m)); 874 mtx_unlock_spin(&vm_page_queue_free_mtx); 875 876 /* 877 * vm_page_insert() is safe prior to the splx(). Note also that 878 * inserting a page here does not insert it into the pmap (which 879 * could cause us to block allocating memory). We cannot block 880 * anywhere. 881 */ 882 if ((req & VM_ALLOC_NOOBJ) == 0) 883 vm_page_insert(m, object, pindex); 884 885 /* 886 * Don't wakeup too often - wakeup the pageout daemon when 887 * we would be nearly out of memory. 888 */ 889 if (vm_paging_needed()) 890 pagedaemon_wakeup(); 891 892 splx(s); 893 return (m); 894} 895 896/* 897 * vm_wait: (also see VM_WAIT macro) 898 * 899 * Block until free pages are available for allocation 900 * - Called in various places before memory allocations. 901 */ 902void 903vm_wait(void) 904{ 905 int s; 906 907 s = splvm(); 908 vm_page_lock_queues(); 909 if (curproc == pageproc) { 910 vm_pageout_pages_needed = 1; 911 msleep(&vm_pageout_pages_needed, &vm_page_queue_mtx, 912 PDROP | PSWP, "VMWait", 0); 913 } else { 914 if (!vm_pages_needed) { 915 vm_pages_needed = 1; 916 wakeup(&vm_pages_needed); 917 } 918 msleep(&cnt.v_free_count, &vm_page_queue_mtx, PDROP | PVM, 919 "vmwait", 0); 920 } 921 splx(s); 922} 923 924/* 925 * vm_waitpfault: (also see VM_WAITPFAULT macro) 926 * 927 * Block until free pages are available for allocation 928 * - Called only in vm_fault so that processes page faulting 929 * can be easily tracked. 930 * - Sleeps at a lower priority than vm_wait() so that vm_wait()ing 931 * processes will be able to grab memory first. Do not change 932 * this balance without careful testing first. 933 */ 934void 935vm_waitpfault(void) 936{ 937 int s; 938 939 s = splvm(); 940 vm_page_lock_queues(); 941 if (!vm_pages_needed) { 942 vm_pages_needed = 1; 943 wakeup(&vm_pages_needed); 944 } 945 msleep(&cnt.v_free_count, &vm_page_queue_mtx, PDROP | PUSER, 946 "pfault", 0); 947 splx(s); 948} 949 950/* 951 * vm_page_activate: 952 * 953 * Put the specified page on the active list (if appropriate). 954 * Ensure that act_count is at least ACT_INIT but do not otherwise 955 * mess with it. 956 * 957 * The page queues must be locked. 958 * This routine may not block. 959 */ 960void 961vm_page_activate(vm_page_t m) 962{ 963 int s; 964 965 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 966 s = splvm(); 967 if (m->queue != PQ_ACTIVE) { 968 if ((m->queue - m->pc) == PQ_CACHE) 969 cnt.v_reactivated++; 970 vm_pageq_remove(m); 971 if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) { 972 if (m->act_count < ACT_INIT) 973 m->act_count = ACT_INIT; 974 vm_pageq_enqueue(PQ_ACTIVE, m); 975 } 976 } else { 977 if (m->act_count < ACT_INIT) 978 m->act_count = ACT_INIT; 979 } 980 splx(s); 981} 982 983/* 984 * vm_page_free_wakeup: 985 * 986 * Helper routine for vm_page_free_toq() and vm_page_cache(). This 987 * routine is called when a page has been added to the cache or free 988 * queues. 989 * 990 * This routine may not block. 991 * This routine must be called at splvm() 992 */ 993static __inline void 994vm_page_free_wakeup(void) 995{ 996 997 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 998 /* 999 * if pageout daemon needs pages, then tell it that there are 1000 * some free. 1001 */ 1002 if (vm_pageout_pages_needed && 1003 cnt.v_cache_count + cnt.v_free_count >= cnt.v_pageout_free_min) { 1004 wakeup(&vm_pageout_pages_needed); 1005 vm_pageout_pages_needed = 0; 1006 } 1007 /* 1008 * wakeup processes that are waiting on memory if we hit a 1009 * high water mark. And wakeup scheduler process if we have 1010 * lots of memory. this process will swapin processes. 1011 */ 1012 if (vm_pages_needed && !vm_page_count_min()) { 1013 vm_pages_needed = 0; 1014 wakeup(&cnt.v_free_count); 1015 } 1016} 1017 1018/* 1019 * vm_page_free_toq: 1020 * 1021 * Returns the given page to the PQ_FREE list, 1022 * disassociating it with any VM object. 1023 * 1024 * Object and page must be locked prior to entry. 1025 * This routine may not block. 1026 */ 1027 1028void 1029vm_page_free_toq(vm_page_t m) 1030{ 1031 int s; 1032 struct vpgqueues *pq; 1033 vm_object_t object = m->object; 1034 1035 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1036 s = splvm(); 1037 cnt.v_tfree++; 1038 1039 if (m->busy || ((m->queue - m->pc) == PQ_FREE)) { 1040 printf( 1041 "vm_page_free: pindex(%lu), busy(%d), PG_BUSY(%d), hold(%d)\n", 1042 (u_long)m->pindex, m->busy, (m->flags & PG_BUSY) ? 1 : 0, 1043 m->hold_count); 1044 if ((m->queue - m->pc) == PQ_FREE) 1045 panic("vm_page_free: freeing free page"); 1046 else 1047 panic("vm_page_free: freeing busy page"); 1048 } 1049 1050 /* 1051 * unqueue, then remove page. Note that we cannot destroy 1052 * the page here because we do not want to call the pager's 1053 * callback routine until after we've put the page on the 1054 * appropriate free queue. 1055 */ 1056 vm_pageq_remove_nowakeup(m); 1057 vm_page_remove(m); 1058 1059 /* 1060 * If fictitious remove object association and 1061 * return, otherwise delay object association removal. 1062 */ 1063 if ((m->flags & PG_FICTITIOUS) != 0) { 1064 splx(s); 1065 return; 1066 } 1067 1068 m->valid = 0; 1069 vm_page_undirty(m); 1070 1071 if (m->wire_count != 0) { 1072 if (m->wire_count > 1) { 1073 panic("vm_page_free: invalid wire count (%d), pindex: 0x%lx", 1074 m->wire_count, (long)m->pindex); 1075 } 1076 panic("vm_page_free: freeing wired page\n"); 1077 } 1078 1079 /* 1080 * If we've exhausted the object's resident pages we want to free 1081 * it up. 1082 */ 1083 if (object && 1084 (object->type == OBJT_VNODE) && 1085 ((object->flags & OBJ_DEAD) == 0) 1086 ) { 1087 struct vnode *vp = (struct vnode *)object->handle; 1088 1089 if (vp) { 1090 VI_LOCK(vp); 1091 if (VSHOULDFREE(vp)) 1092 vfree(vp); 1093 VI_UNLOCK(vp); 1094 } 1095 } 1096 1097 /* 1098 * Clear the UNMANAGED flag when freeing an unmanaged page. 1099 */ 1100 if (m->flags & PG_UNMANAGED) { 1101 m->flags &= ~PG_UNMANAGED; 1102 } 1103 1104 if (m->hold_count != 0) { 1105 m->flags &= ~PG_ZERO; 1106 m->queue = PQ_HOLD; 1107 } else 1108 m->queue = PQ_FREE + m->pc; 1109 pq = &vm_page_queues[m->queue]; 1110 mtx_lock_spin(&vm_page_queue_free_mtx); 1111 pq->lcnt++; 1112 ++(*pq->cnt); 1113 1114 /* 1115 * Put zero'd pages on the end ( where we look for zero'd pages 1116 * first ) and non-zerod pages at the head. 1117 */ 1118 if (m->flags & PG_ZERO) { 1119 TAILQ_INSERT_TAIL(&pq->pl, m, pageq); 1120 ++vm_page_zero_count; 1121 } else { 1122 TAILQ_INSERT_HEAD(&pq->pl, m, pageq); 1123 } 1124 mtx_unlock_spin(&vm_page_queue_free_mtx); 1125 vm_page_free_wakeup(); 1126 splx(s); 1127} 1128 1129/* 1130 * vm_page_unmanage: 1131 * 1132 * Prevent PV management from being done on the page. The page is 1133 * removed from the paging queues as if it were wired, and as a 1134 * consequence of no longer being managed the pageout daemon will not 1135 * touch it (since there is no way to locate the pte mappings for the 1136 * page). madvise() calls that mess with the pmap will also no longer 1137 * operate on the page. 1138 * 1139 * Beyond that the page is still reasonably 'normal'. Freeing the page 1140 * will clear the flag. 1141 * 1142 * This routine is used by OBJT_PHYS objects - objects using unswappable 1143 * physical memory as backing store rather then swap-backed memory and 1144 * will eventually be extended to support 4MB unmanaged physical 1145 * mappings. 1146 */ 1147void 1148vm_page_unmanage(vm_page_t m) 1149{ 1150 int s; 1151 1152 s = splvm(); 1153 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1154 if ((m->flags & PG_UNMANAGED) == 0) { 1155 if (m->wire_count == 0) 1156 vm_pageq_remove(m); 1157 } 1158 vm_page_flag_set(m, PG_UNMANAGED); 1159 splx(s); 1160} 1161 1162/* 1163 * vm_page_wire: 1164 * 1165 * Mark this page as wired down by yet 1166 * another map, removing it from paging queues 1167 * as necessary. 1168 * 1169 * The page queues must be locked. 1170 * This routine may not block. 1171 */ 1172void 1173vm_page_wire(vm_page_t m) 1174{ 1175 int s; 1176 1177 /* 1178 * Only bump the wire statistics if the page is not already wired, 1179 * and only unqueue the page if it is on some queue (if it is unmanaged 1180 * it is already off the queues). 1181 */ 1182 s = splvm(); 1183 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1184 if (m->wire_count == 0) { 1185 if ((m->flags & PG_UNMANAGED) == 0) 1186 vm_pageq_remove(m); 1187 atomic_add_int(&cnt.v_wire_count, 1); 1188 } 1189 m->wire_count++; 1190 KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m)); 1191 splx(s); 1192} 1193 1194/* 1195 * vm_page_unwire: 1196 * 1197 * Release one wiring of this page, potentially 1198 * enabling it to be paged again. 1199 * 1200 * Many pages placed on the inactive queue should actually go 1201 * into the cache, but it is difficult to figure out which. What 1202 * we do instead, if the inactive target is well met, is to put 1203 * clean pages at the head of the inactive queue instead of the tail. 1204 * This will cause them to be moved to the cache more quickly and 1205 * if not actively re-referenced, freed more quickly. If we just 1206 * stick these pages at the end of the inactive queue, heavy filesystem 1207 * meta-data accesses can cause an unnecessary paging load on memory bound 1208 * processes. This optimization causes one-time-use metadata to be 1209 * reused more quickly. 1210 * 1211 * BUT, if we are in a low-memory situation we have no choice but to 1212 * put clean pages on the cache queue. 1213 * 1214 * A number of routines use vm_page_unwire() to guarantee that the page 1215 * will go into either the inactive or active queues, and will NEVER 1216 * be placed in the cache - for example, just after dirtying a page. 1217 * dirty pages in the cache are not allowed. 1218 * 1219 * The page queues must be locked. 1220 * This routine may not block. 1221 */ 1222void 1223vm_page_unwire(vm_page_t m, int activate) 1224{ 1225 int s; 1226 1227 s = splvm(); 1228 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1229 if (m->wire_count > 0) { 1230 m->wire_count--; 1231 if (m->wire_count == 0) { 1232 atomic_subtract_int(&cnt.v_wire_count, 1); 1233 if (m->flags & PG_UNMANAGED) { 1234 ; 1235 } else if (activate) 1236 vm_pageq_enqueue(PQ_ACTIVE, m); 1237 else { 1238 vm_page_flag_clear(m, PG_WINATCFLS); 1239 vm_pageq_enqueue(PQ_INACTIVE, m); 1240 } 1241 } 1242 } else { 1243 panic("vm_page_unwire: invalid wire count: %d\n", m->wire_count); 1244 } 1245 splx(s); 1246} 1247 1248 1249/* 1250 * Move the specified page to the inactive queue. If the page has 1251 * any associated swap, the swap is deallocated. 1252 * 1253 * Normally athead is 0 resulting in LRU operation. athead is set 1254 * to 1 if we want this page to be 'as if it were placed in the cache', 1255 * except without unmapping it from the process address space. 1256 * 1257 * This routine may not block. 1258 */ 1259static __inline void 1260_vm_page_deactivate(vm_page_t m, int athead) 1261{ 1262 int s; 1263 1264 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1265 /* 1266 * Ignore if already inactive. 1267 */ 1268 if (m->queue == PQ_INACTIVE) 1269 return; 1270 1271 s = splvm(); 1272 if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) { 1273 if ((m->queue - m->pc) == PQ_CACHE) 1274 cnt.v_reactivated++; 1275 vm_page_flag_clear(m, PG_WINATCFLS); 1276 vm_pageq_remove(m); 1277 if (athead) 1278 TAILQ_INSERT_HEAD(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 1279 else 1280 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 1281 m->queue = PQ_INACTIVE; 1282 vm_page_queues[PQ_INACTIVE].lcnt++; 1283 cnt.v_inactive_count++; 1284 } 1285 splx(s); 1286} 1287 1288void 1289vm_page_deactivate(vm_page_t m) 1290{ 1291 _vm_page_deactivate(m, 0); 1292} 1293 1294/* 1295 * vm_page_try_to_cache: 1296 * 1297 * Returns 0 on failure, 1 on success 1298 */ 1299int 1300vm_page_try_to_cache(vm_page_t m) 1301{ 1302 1303 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1304 if (m->dirty || m->hold_count || m->busy || m->wire_count || 1305 (m->flags & (PG_BUSY|PG_UNMANAGED))) { 1306 return (0); 1307 } 1308 vm_page_test_dirty(m); 1309 if (m->dirty) 1310 return (0); 1311 vm_page_cache(m); 1312 return (1); 1313} 1314 1315/* 1316 * vm_page_try_to_free() 1317 * 1318 * Attempt to free the page. If we cannot free it, we do nothing. 1319 * 1 is returned on success, 0 on failure. 1320 */ 1321int 1322vm_page_try_to_free(vm_page_t m) 1323{ 1324 1325 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1326 if (m->dirty || m->hold_count || m->busy || m->wire_count || 1327 (m->flags & (PG_BUSY|PG_UNMANAGED))) { 1328 return (0); 1329 } 1330 vm_page_test_dirty(m); 1331 if (m->dirty) 1332 return (0); 1333 vm_page_busy(m); 1334 pmap_remove_all(m); 1335 vm_page_free(m); 1336 return (1); 1337} 1338 1339/* 1340 * vm_page_cache 1341 * 1342 * Put the specified page onto the page cache queue (if appropriate). 1343 * 1344 * This routine may not block. 1345 */ 1346void 1347vm_page_cache(vm_page_t m) 1348{ 1349 int s; 1350 1351 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1352 if ((m->flags & (PG_BUSY|PG_UNMANAGED)) || m->busy || m->wire_count) { 1353 printf("vm_page_cache: attempting to cache busy page\n"); 1354 return; 1355 } 1356 if ((m->queue - m->pc) == PQ_CACHE) 1357 return; 1358 1359 /* 1360 * Remove all pmaps and indicate that the page is not 1361 * writeable or mapped. 1362 */ 1363 pmap_remove_all(m); 1364 if (m->dirty != 0) { 1365 panic("vm_page_cache: caching a dirty page, pindex: %ld", 1366 (long)m->pindex); 1367 } 1368 s = splvm(); 1369 vm_pageq_remove_nowakeup(m); 1370 vm_pageq_enqueue(PQ_CACHE + m->pc, m); 1371 vm_page_free_wakeup(); 1372 splx(s); 1373} 1374 1375/* 1376 * vm_page_dontneed 1377 * 1378 * Cache, deactivate, or do nothing as appropriate. This routine 1379 * is typically used by madvise() MADV_DONTNEED. 1380 * 1381 * Generally speaking we want to move the page into the cache so 1382 * it gets reused quickly. However, this can result in a silly syndrome 1383 * due to the page recycling too quickly. Small objects will not be 1384 * fully cached. On the otherhand, if we move the page to the inactive 1385 * queue we wind up with a problem whereby very large objects 1386 * unnecessarily blow away our inactive and cache queues. 1387 * 1388 * The solution is to move the pages based on a fixed weighting. We 1389 * either leave them alone, deactivate them, or move them to the cache, 1390 * where moving them to the cache has the highest weighting. 1391 * By forcing some pages into other queues we eventually force the 1392 * system to balance the queues, potentially recovering other unrelated 1393 * space from active. The idea is to not force this to happen too 1394 * often. 1395 */ 1396void 1397vm_page_dontneed(vm_page_t m) 1398{ 1399 static int dnweight; 1400 int dnw; 1401 int head; 1402 1403 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1404 dnw = ++dnweight; 1405 1406 /* 1407 * occassionally leave the page alone 1408 */ 1409 if ((dnw & 0x01F0) == 0 || 1410 m->queue == PQ_INACTIVE || 1411 m->queue - m->pc == PQ_CACHE 1412 ) { 1413 if (m->act_count >= ACT_INIT) 1414 --m->act_count; 1415 return; 1416 } 1417 1418 if (m->dirty == 0) 1419 vm_page_test_dirty(m); 1420 1421 if (m->dirty || (dnw & 0x0070) == 0) { 1422 /* 1423 * Deactivate the page 3 times out of 32. 1424 */ 1425 head = 0; 1426 } else { 1427 /* 1428 * Cache the page 28 times out of every 32. Note that 1429 * the page is deactivated instead of cached, but placed 1430 * at the head of the queue instead of the tail. 1431 */ 1432 head = 1; 1433 } 1434 _vm_page_deactivate(m, head); 1435} 1436 1437/* 1438 * Grab a page, waiting until we are waken up due to the page 1439 * changing state. We keep on waiting, if the page continues 1440 * to be in the object. If the page doesn't exist, allocate it. 1441 * 1442 * This routine may block. 1443 */ 1444vm_page_t 1445vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags) 1446{ 1447 vm_page_t m; 1448 int s, generation, is_object_locked; 1449 1450 /* 1451 * Remove is_object_locked after vm_object locking is finished. 1452 */ 1453 if (!(is_object_locked = VM_OBJECT_LOCKED(object))) 1454 GIANT_REQUIRED; 1455retrylookup: 1456 if ((m = vm_page_lookup(object, pindex)) != NULL) { 1457 vm_page_lock_queues(); 1458 if (m->busy || (m->flags & PG_BUSY)) { 1459 generation = object->generation; 1460 1461 s = splvm(); 1462 while ((object->generation == generation) && 1463 (m->busy || (m->flags & PG_BUSY))) { 1464 vm_page_flag_set(m, PG_WANTED | PG_REFERENCED); 1465 if (is_object_locked) 1466 VM_OBJECT_UNLOCK(object); 1467 msleep(m, &vm_page_queue_mtx, PDROP | PVM, "pgrbwt", 0); 1468 if (is_object_locked) 1469 VM_OBJECT_LOCK(object); 1470 if ((allocflags & VM_ALLOC_RETRY) == 0) { 1471 splx(s); 1472 return NULL; 1473 } 1474 vm_page_lock_queues(); 1475 } 1476 vm_page_unlock_queues(); 1477 splx(s); 1478 goto retrylookup; 1479 } else { 1480 if (allocflags & VM_ALLOC_WIRED) 1481 vm_page_wire(m); 1482 vm_page_busy(m); 1483 vm_page_unlock_queues(); 1484 return m; 1485 } 1486 } 1487 1488 m = vm_page_alloc(object, pindex, allocflags & ~VM_ALLOC_RETRY); 1489 if (m == NULL) { 1490 if (is_object_locked) 1491 VM_OBJECT_UNLOCK(object); 1492 VM_WAIT; 1493 if (is_object_locked) 1494 VM_OBJECT_LOCK(object); 1495 if ((allocflags & VM_ALLOC_RETRY) == 0) 1496 return NULL; 1497 goto retrylookup; 1498 } 1499 1500 return m; 1501} 1502 1503/* 1504 * Mapping function for valid bits or for dirty bits in 1505 * a page. May not block. 1506 * 1507 * Inputs are required to range within a page. 1508 */ 1509__inline int 1510vm_page_bits(int base, int size) 1511{ 1512 int first_bit; 1513 int last_bit; 1514 1515 KASSERT( 1516 base + size <= PAGE_SIZE, 1517 ("vm_page_bits: illegal base/size %d/%d", base, size) 1518 ); 1519 1520 if (size == 0) /* handle degenerate case */ 1521 return (0); 1522 1523 first_bit = base >> DEV_BSHIFT; 1524 last_bit = (base + size - 1) >> DEV_BSHIFT; 1525 1526 return ((2 << last_bit) - (1 << first_bit)); 1527} 1528 1529/* 1530 * vm_page_set_validclean: 1531 * 1532 * Sets portions of a page valid and clean. The arguments are expected 1533 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive 1534 * of any partial chunks touched by the range. The invalid portion of 1535 * such chunks will be zero'd. 1536 * 1537 * This routine may not block. 1538 * 1539 * (base + size) must be less then or equal to PAGE_SIZE. 1540 */ 1541void 1542vm_page_set_validclean(vm_page_t m, int base, int size) 1543{ 1544 int pagebits; 1545 int frag; 1546 int endoff; 1547 1548 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1549 if (size == 0) /* handle degenerate case */ 1550 return; 1551 1552 /* 1553 * If the base is not DEV_BSIZE aligned and the valid 1554 * bit is clear, we have to zero out a portion of the 1555 * first block. 1556 */ 1557 if ((frag = base & ~(DEV_BSIZE - 1)) != base && 1558 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0) 1559 pmap_zero_page_area(m, frag, base - frag); 1560 1561 /* 1562 * If the ending offset is not DEV_BSIZE aligned and the 1563 * valid bit is clear, we have to zero out a portion of 1564 * the last block. 1565 */ 1566 endoff = base + size; 1567 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff && 1568 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0) 1569 pmap_zero_page_area(m, endoff, 1570 DEV_BSIZE - (endoff & (DEV_BSIZE - 1))); 1571 1572 /* 1573 * Set valid, clear dirty bits. If validating the entire 1574 * page we can safely clear the pmap modify bit. We also 1575 * use this opportunity to clear the PG_NOSYNC flag. If a process 1576 * takes a write fault on a MAP_NOSYNC memory area the flag will 1577 * be set again. 1578 * 1579 * We set valid bits inclusive of any overlap, but we can only 1580 * clear dirty bits for DEV_BSIZE chunks that are fully within 1581 * the range. 1582 */ 1583 pagebits = vm_page_bits(base, size); 1584 m->valid |= pagebits; 1585#if 0 /* NOT YET */ 1586 if ((frag = base & (DEV_BSIZE - 1)) != 0) { 1587 frag = DEV_BSIZE - frag; 1588 base += frag; 1589 size -= frag; 1590 if (size < 0) 1591 size = 0; 1592 } 1593 pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1)); 1594#endif 1595 m->dirty &= ~pagebits; 1596 if (base == 0 && size == PAGE_SIZE) { 1597 pmap_clear_modify(m); 1598 vm_page_flag_clear(m, PG_NOSYNC); 1599 } 1600} 1601 1602#if 0 1603 1604void 1605vm_page_set_dirty(vm_page_t m, int base, int size) 1606{ 1607 m->dirty |= vm_page_bits(base, size); 1608} 1609 1610#endif 1611 1612void 1613vm_page_clear_dirty(vm_page_t m, int base, int size) 1614{ 1615 GIANT_REQUIRED; 1616 m->dirty &= ~vm_page_bits(base, size); 1617} 1618 1619/* 1620 * vm_page_set_invalid: 1621 * 1622 * Invalidates DEV_BSIZE'd chunks within a page. Both the 1623 * valid and dirty bits for the effected areas are cleared. 1624 * 1625 * May not block. 1626 */ 1627void 1628vm_page_set_invalid(vm_page_t m, int base, int size) 1629{ 1630 int bits; 1631 1632 GIANT_REQUIRED; 1633 bits = vm_page_bits(base, size); 1634 m->valid &= ~bits; 1635 m->dirty &= ~bits; 1636 m->object->generation++; 1637} 1638 1639/* 1640 * vm_page_zero_invalid() 1641 * 1642 * The kernel assumes that the invalid portions of a page contain 1643 * garbage, but such pages can be mapped into memory by user code. 1644 * When this occurs, we must zero out the non-valid portions of the 1645 * page so user code sees what it expects. 1646 * 1647 * Pages are most often semi-valid when the end of a file is mapped 1648 * into memory and the file's size is not page aligned. 1649 */ 1650void 1651vm_page_zero_invalid(vm_page_t m, boolean_t setvalid) 1652{ 1653 int b; 1654 int i; 1655 1656 /* 1657 * Scan the valid bits looking for invalid sections that 1658 * must be zerod. Invalid sub-DEV_BSIZE'd areas ( where the 1659 * valid bit may be set ) have already been zerod by 1660 * vm_page_set_validclean(). 1661 */ 1662 for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) { 1663 if (i == (PAGE_SIZE / DEV_BSIZE) || 1664 (m->valid & (1 << i)) 1665 ) { 1666 if (i > b) { 1667 pmap_zero_page_area(m, 1668 b << DEV_BSHIFT, (i - b) << DEV_BSHIFT); 1669 } 1670 b = i + 1; 1671 } 1672 } 1673 1674 /* 1675 * setvalid is TRUE when we can safely set the zero'd areas 1676 * as being valid. We can do this if there are no cache consistancy 1677 * issues. e.g. it is ok to do with UFS, but not ok to do with NFS. 1678 */ 1679 if (setvalid) 1680 m->valid = VM_PAGE_BITS_ALL; 1681} 1682 1683/* 1684 * vm_page_is_valid: 1685 * 1686 * Is (partial) page valid? Note that the case where size == 0 1687 * will return FALSE in the degenerate case where the page is 1688 * entirely invalid, and TRUE otherwise. 1689 * 1690 * May not block. 1691 */ 1692int 1693vm_page_is_valid(vm_page_t m, int base, int size) 1694{ 1695 int bits = vm_page_bits(base, size); 1696 1697 if (m->valid && ((m->valid & bits) == bits)) 1698 return 1; 1699 else 1700 return 0; 1701} 1702 1703/* 1704 * update dirty bits from pmap/mmu. May not block. 1705 */ 1706void 1707vm_page_test_dirty(vm_page_t m) 1708{ 1709 if ((m->dirty != VM_PAGE_BITS_ALL) && pmap_is_modified(m)) { 1710 vm_page_dirty(m); 1711 } 1712} 1713 1714int so_zerocp_fullpage = 0; 1715 1716void 1717vm_page_cowfault(vm_page_t m) 1718{ 1719 vm_page_t mnew; 1720 vm_object_t object; 1721 vm_pindex_t pindex; 1722 1723 object = m->object; 1724 pindex = m->pindex; 1725 vm_page_busy(m); 1726 1727 retry_alloc: 1728 vm_page_remove(m); 1729 /* 1730 * An interrupt allocation is requested because the page 1731 * queues lock is held. 1732 */ 1733 mnew = vm_page_alloc(object, pindex, VM_ALLOC_INTERRUPT); 1734 if (mnew == NULL) { 1735 vm_page_insert(m, object, pindex); 1736 vm_page_unlock_queues(); 1737 VM_WAIT; 1738 vm_page_lock_queues(); 1739 goto retry_alloc; 1740 } 1741 1742 if (m->cow == 0) { 1743 /* 1744 * check to see if we raced with an xmit complete when 1745 * waiting to allocate a page. If so, put things back 1746 * the way they were 1747 */ 1748 vm_page_busy(mnew); 1749 vm_page_free(mnew); 1750 vm_page_insert(m, object, pindex); 1751 } else { /* clear COW & copy page */ 1752 if (so_zerocp_fullpage) { 1753 mnew->valid = VM_PAGE_BITS_ALL; 1754 } else { 1755 vm_page_copy(m, mnew); 1756 } 1757 vm_page_dirty(mnew); 1758 vm_page_flag_clear(mnew, PG_BUSY); 1759 } 1760} 1761 1762void 1763vm_page_cowclear(vm_page_t m) 1764{ 1765 1766 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1767 if (m->cow) { 1768 m->cow--; 1769 /* 1770 * let vm_fault add back write permission lazily 1771 */ 1772 } 1773 /* 1774 * sf_buf_free() will free the page, so we needn't do it here 1775 */ 1776} 1777 1778void 1779vm_page_cowsetup(vm_page_t m) 1780{ 1781 1782 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1783 m->cow++; 1784 pmap_page_protect(m, VM_PROT_READ); 1785} 1786 1787#include "opt_ddb.h" 1788#ifdef DDB 1789#include <sys/kernel.h> 1790 1791#include <ddb/ddb.h> 1792 1793DB_SHOW_COMMAND(page, vm_page_print_page_info) 1794{ 1795 db_printf("cnt.v_free_count: %d\n", cnt.v_free_count); 1796 db_printf("cnt.v_cache_count: %d\n", cnt.v_cache_count); 1797 db_printf("cnt.v_inactive_count: %d\n", cnt.v_inactive_count); 1798 db_printf("cnt.v_active_count: %d\n", cnt.v_active_count); 1799 db_printf("cnt.v_wire_count: %d\n", cnt.v_wire_count); 1800 db_printf("cnt.v_free_reserved: %d\n", cnt.v_free_reserved); 1801 db_printf("cnt.v_free_min: %d\n", cnt.v_free_min); 1802 db_printf("cnt.v_free_target: %d\n", cnt.v_free_target); 1803 db_printf("cnt.v_cache_min: %d\n", cnt.v_cache_min); 1804 db_printf("cnt.v_inactive_target: %d\n", cnt.v_inactive_target); 1805} 1806 1807DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info) 1808{ 1809 int i; 1810 db_printf("PQ_FREE:"); 1811 for (i = 0; i < PQ_L2_SIZE; i++) { 1812 db_printf(" %d", vm_page_queues[PQ_FREE + i].lcnt); 1813 } 1814 db_printf("\n"); 1815 1816 db_printf("PQ_CACHE:"); 1817 for (i = 0; i < PQ_L2_SIZE; i++) { 1818 db_printf(" %d", vm_page_queues[PQ_CACHE + i].lcnt); 1819 } 1820 db_printf("\n"); 1821 1822 db_printf("PQ_ACTIVE: %d, PQ_INACTIVE: %d\n", 1823 vm_page_queues[PQ_ACTIVE].lcnt, 1824 vm_page_queues[PQ_INACTIVE].lcnt); 1825} 1826#endif /* DDB */ 1827