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