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