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