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