1 /*- 2 * Copyright (c) 1991 Regents of the University of California. 3 * All rights reserved. 4 * Copyright (c) 1998 Matthew Dillon. All Rights Reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * The Mach Operating System project at Carnegie-Mellon University. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * from: @(#)vm_page.c 7.4 (Berkeley) 5/7/91 34 */ 35 36 /*- 37 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 38 * All rights reserved. 39 * 40 * Authors: Avadis Tevanian, Jr., Michael Wayne Young 41 * 42 * Permission to use, copy, modify and distribute this software and 43 * its documentation is hereby granted, provided that both the copyright 44 * notice and this permission notice appear in all copies of the 45 * software, derivative works or modified versions, and any portions 46 * thereof, and that both notices appear in supporting documentation. 47 * 48 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 49 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 50 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 51 * 52 * Carnegie Mellon requests users of this software to return to 53 * 54 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 55 * School of Computer Science 56 * Carnegie Mellon University 57 * Pittsburgh PA 15213-3890 58 * 59 * any improvements or extensions that they make and grant Carnegie the 60 * rights to redistribute these changes. 61 */ 62 63 /* 64 * GENERAL RULES ON VM_PAGE MANIPULATION 65 * 66 * - a pageq mutex is required when adding or removing a page from a 67 * page queue (vm_page_queue[]), regardless of other mutexes or the 68 * busy state of a page. 69 * 70 * - a hash chain mutex is required when associating or disassociating 71 * a page from the VM PAGE CACHE hash table (vm_page_buckets), 72 * regardless of other mutexes or the busy state of a page. 73 * 74 * - either a hash chain mutex OR a busied page is required in order 75 * to modify the page flags. A hash chain mutex must be obtained in 76 * order to busy a page. A page's flags cannot be modified by a 77 * hash chain mutex if the page is marked busy. 78 * 79 * - The object memq mutex is held when inserting or removing 80 * pages from an object (vm_page_insert() or vm_page_remove()). This 81 * is different from the object's main mutex. 82 * 83 * Generally speaking, you have to be aware of side effects when running 84 * vm_page ops. A vm_page_lookup() will return with the hash chain 85 * locked, whether it was able to lookup the page or not. vm_page_free(), 86 * vm_page_cache(), vm_page_activate(), and a number of other routines 87 * will release the hash chain mutex for you. Intermediate manipulation 88 * routines such as vm_page_flag_set() expect the hash chain to be held 89 * on entry and the hash chain will remain held on return. 90 * 91 * pageq scanning can only occur with the pageq in question locked. 92 * We have a known bottleneck with the active queue, but the cache 93 * and free queues are actually arrays already. 94 */ 95 96 /* 97 * Resident memory management module. 98 */ 99 100 #include <sys/cdefs.h> 101 __FBSDID("$FreeBSD$"); 102 103 #include "opt_vm.h" 104 105 #include <sys/param.h> 106 #include <sys/systm.h> 107 #include <sys/lock.h> 108 #include <sys/kernel.h> 109 #include <sys/limits.h> 110 #include <sys/malloc.h> 111 #include <sys/mutex.h> 112 #include <sys/proc.h> 113 #include <sys/sysctl.h> 114 #include <sys/vmmeter.h> 115 #include <sys/vnode.h> 116 117 #include <vm/vm.h> 118 #include <vm/vm_param.h> 119 #include <vm/vm_kern.h> 120 #include <vm/vm_object.h> 121 #include <vm/vm_page.h> 122 #include <vm/vm_pageout.h> 123 #include <vm/vm_pager.h> 124 #include <vm/vm_phys.h> 125 #include <vm/vm_reserv.h> 126 #include <vm/vm_extern.h> 127 #include <vm/uma.h> 128 #include <vm/uma_int.h> 129 130 #include <machine/md_var.h> 131 132 /* 133 * Associated with page of user-allocatable memory is a 134 * page structure. 135 */ 136 137 struct vpgqueues vm_page_queues[PQ_COUNT]; 138 struct mtx vm_page_queue_mtx; 139 struct mtx vm_page_queue_free_mtx; 140 141 vm_page_t vm_page_array = 0; 142 int vm_page_array_size = 0; 143 long first_page = 0; 144 int vm_page_zero_count = 0; 145 146 static int boot_pages = UMA_BOOT_PAGES; 147 TUNABLE_INT("vm.boot_pages", &boot_pages); 148 SYSCTL_INT(_vm, OID_AUTO, boot_pages, CTLFLAG_RD, &boot_pages, 0, 149 "number of pages allocated for bootstrapping the VM system"); 150 151 static void vm_page_enqueue(int queue, vm_page_t m); 152 153 /* Make sure that u_long is at least 64 bits when PAGE_SIZE is 32K. */ 154 #if PAGE_SIZE == 32768 155 #ifdef CTASSERT 156 CTASSERT(sizeof(u_long) >= 8); 157 #endif 158 #endif 159 160 /* 161 * vm_set_page_size: 162 * 163 * Sets the page size, perhaps based upon the memory 164 * size. Must be called before any use of page-size 165 * dependent functions. 166 */ 167 void 168 vm_set_page_size(void) 169 { 170 if (cnt.v_page_size == 0) 171 cnt.v_page_size = PAGE_SIZE; 172 if (((cnt.v_page_size - 1) & cnt.v_page_size) != 0) 173 panic("vm_set_page_size: page size not a power of two"); 174 } 175 176 /* 177 * vm_page_blacklist_lookup: 178 * 179 * See if a physical address in this page has been listed 180 * in the blacklist tunable. Entries in the tunable are 181 * separated by spaces or commas. If an invalid integer is 182 * encountered then the rest of the string is skipped. 183 */ 184 static int 185 vm_page_blacklist_lookup(char *list, vm_paddr_t pa) 186 { 187 vm_paddr_t bad; 188 char *cp, *pos; 189 190 for (pos = list; *pos != '\0'; pos = cp) { 191 bad = strtoq(pos, &cp, 0); 192 if (*cp != '\0') { 193 if (*cp == ' ' || *cp == ',') { 194 cp++; 195 if (cp == pos) 196 continue; 197 } else 198 break; 199 } 200 if (pa == trunc_page(bad)) 201 return (1); 202 } 203 return (0); 204 } 205 206 /* 207 * vm_page_startup: 208 * 209 * Initializes the resident memory module. 210 * 211 * Allocates memory for the page cells, and 212 * for the object/offset-to-page hash table headers. 213 * Each page cell is initialized and placed on the free list. 214 */ 215 vm_offset_t 216 vm_page_startup(vm_offset_t vaddr) 217 { 218 vm_offset_t mapped; 219 vm_paddr_t page_range; 220 vm_paddr_t new_end; 221 int i; 222 vm_paddr_t pa; 223 int nblocks; 224 vm_paddr_t last_pa; 225 char *list; 226 227 /* the biggest memory array is the second group of pages */ 228 vm_paddr_t end; 229 vm_paddr_t biggestsize; 230 vm_paddr_t low_water, high_water; 231 int biggestone; 232 233 biggestsize = 0; 234 biggestone = 0; 235 nblocks = 0; 236 vaddr = round_page(vaddr); 237 238 for (i = 0; phys_avail[i + 1]; i += 2) { 239 phys_avail[i] = round_page(phys_avail[i]); 240 phys_avail[i + 1] = trunc_page(phys_avail[i + 1]); 241 } 242 243 low_water = phys_avail[0]; 244 high_water = phys_avail[1]; 245 246 for (i = 0; phys_avail[i + 1]; i += 2) { 247 vm_paddr_t size = phys_avail[i + 1] - phys_avail[i]; 248 249 if (size > biggestsize) { 250 biggestone = i; 251 biggestsize = size; 252 } 253 if (phys_avail[i] < low_water) 254 low_water = phys_avail[i]; 255 if (phys_avail[i + 1] > high_water) 256 high_water = phys_avail[i + 1]; 257 ++nblocks; 258 } 259 260 #ifdef XEN 261 low_water = 0; 262 #endif 263 264 end = phys_avail[biggestone+1]; 265 266 /* 267 * Initialize the locks. 268 */ 269 mtx_init(&vm_page_queue_mtx, "vm page queue mutex", NULL, MTX_DEF | 270 MTX_RECURSE); 271 mtx_init(&vm_page_queue_free_mtx, "vm page queue free mutex", NULL, 272 MTX_DEF); 273 274 /* 275 * Initialize the queue headers for the hold queue, the active queue, 276 * and the inactive queue. 277 */ 278 for (i = 0; i < PQ_COUNT; i++) 279 TAILQ_INIT(&vm_page_queues[i].pl); 280 vm_page_queues[PQ_INACTIVE].cnt = &cnt.v_inactive_count; 281 vm_page_queues[PQ_ACTIVE].cnt = &cnt.v_active_count; 282 vm_page_queues[PQ_HOLD].cnt = &cnt.v_active_count; 283 284 /* 285 * Allocate memory for use when boot strapping the kernel memory 286 * allocator. 287 */ 288 new_end = end - (boot_pages * UMA_SLAB_SIZE); 289 new_end = trunc_page(new_end); 290 mapped = pmap_map(&vaddr, new_end, end, 291 VM_PROT_READ | VM_PROT_WRITE); 292 bzero((void *)mapped, end - new_end); 293 uma_startup((void *)mapped, boot_pages); 294 295 #if defined(__amd64__) || defined(__i386__) || defined(__arm__) 296 /* 297 * Allocate a bitmap to indicate that a random physical page 298 * needs to be included in a minidump. 299 * 300 * The amd64 port needs this to indicate which direct map pages 301 * need to be dumped, via calls to dump_add_page()/dump_drop_page(). 302 * 303 * However, i386 still needs this workspace internally within the 304 * minidump code. In theory, they are not needed on i386, but are 305 * included should the sf_buf code decide to use them. 306 */ 307 page_range = phys_avail[(nblocks - 1) * 2 + 1] / PAGE_SIZE; 308 vm_page_dump_size = round_page(roundup2(page_range, NBBY) / NBBY); 309 new_end -= vm_page_dump_size; 310 vm_page_dump = (void *)(uintptr_t)pmap_map(&vaddr, new_end, 311 new_end + vm_page_dump_size, VM_PROT_READ | VM_PROT_WRITE); 312 bzero((void *)vm_page_dump, vm_page_dump_size); 313 #endif 314 /* 315 * Compute the number of pages of memory that will be available for 316 * use (taking into account the overhead of a page structure per 317 * page). 318 */ 319 first_page = low_water / PAGE_SIZE; 320 #ifdef VM_PHYSSEG_SPARSE 321 page_range = 0; 322 for (i = 0; phys_avail[i + 1] != 0; i += 2) 323 page_range += atop(phys_avail[i + 1] - phys_avail[i]); 324 #elif defined(VM_PHYSSEG_DENSE) 325 page_range = high_water / PAGE_SIZE - first_page; 326 #else 327 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined." 328 #endif 329 end = new_end; 330 331 /* 332 * Reserve an unmapped guard page to trap access to vm_page_array[-1]. 333 */ 334 vaddr += PAGE_SIZE; 335 336 /* 337 * Initialize the mem entry structures now, and put them in the free 338 * queue. 339 */ 340 new_end = trunc_page(end - page_range * sizeof(struct vm_page)); 341 mapped = pmap_map(&vaddr, new_end, end, 342 VM_PROT_READ | VM_PROT_WRITE); 343 vm_page_array = (vm_page_t) mapped; 344 #if VM_NRESERVLEVEL > 0 345 /* 346 * Allocate memory for the reservation management system's data 347 * structures. 348 */ 349 new_end = vm_reserv_startup(&vaddr, new_end, high_water); 350 #endif 351 #ifdef __amd64__ 352 /* 353 * pmap_map on amd64 comes out of the direct-map, not kvm like i386, 354 * so the pages must be tracked for a crashdump to include this data. 355 * This includes the vm_page_array and the early UMA bootstrap pages. 356 */ 357 for (pa = new_end; pa < phys_avail[biggestone + 1]; pa += PAGE_SIZE) 358 dump_add_page(pa); 359 #endif 360 phys_avail[biggestone + 1] = new_end; 361 362 /* 363 * Clear all of the page structures 364 */ 365 bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page)); 366 for (i = 0; i < page_range; i++) 367 vm_page_array[i].order = VM_NFREEORDER; 368 vm_page_array_size = page_range; 369 370 /* 371 * Initialize the physical memory allocator. 372 */ 373 vm_phys_init(); 374 375 /* 376 * Add every available physical page that is not blacklisted to 377 * the free lists. 378 */ 379 cnt.v_page_count = 0; 380 cnt.v_free_count = 0; 381 list = getenv("vm.blacklist"); 382 for (i = 0; phys_avail[i + 1] != 0; i += 2) { 383 pa = phys_avail[i]; 384 last_pa = phys_avail[i + 1]; 385 while (pa < last_pa) { 386 if (list != NULL && 387 vm_page_blacklist_lookup(list, pa)) 388 printf("Skipping page with pa 0x%jx\n", 389 (uintmax_t)pa); 390 else 391 vm_phys_add_page(pa); 392 pa += PAGE_SIZE; 393 } 394 } 395 freeenv(list); 396 #if VM_NRESERVLEVEL > 0 397 /* 398 * Initialize the reservation management system. 399 */ 400 vm_reserv_init(); 401 #endif 402 return (vaddr); 403 } 404 405 void 406 vm_page_flag_set(vm_page_t m, unsigned short bits) 407 { 408 409 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 410 m->flags |= bits; 411 } 412 413 void 414 vm_page_flag_clear(vm_page_t m, unsigned short bits) 415 { 416 417 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 418 m->flags &= ~bits; 419 } 420 421 void 422 vm_page_busy(vm_page_t m) 423 { 424 425 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 426 KASSERT((m->oflags & VPO_BUSY) == 0, 427 ("vm_page_busy: page already busy!!!")); 428 m->oflags |= VPO_BUSY; 429 } 430 431 /* 432 * vm_page_flash: 433 * 434 * wakeup anyone waiting for the page. 435 */ 436 void 437 vm_page_flash(vm_page_t m) 438 { 439 440 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 441 if (m->oflags & VPO_WANTED) { 442 m->oflags &= ~VPO_WANTED; 443 wakeup(m); 444 } 445 } 446 447 /* 448 * vm_page_wakeup: 449 * 450 * clear the VPO_BUSY flag and wakeup anyone waiting for the 451 * page. 452 * 453 */ 454 void 455 vm_page_wakeup(vm_page_t m) 456 { 457 458 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 459 KASSERT(m->oflags & VPO_BUSY, ("vm_page_wakeup: page not busy!!!")); 460 m->oflags &= ~VPO_BUSY; 461 vm_page_flash(m); 462 } 463 464 void 465 vm_page_io_start(vm_page_t m) 466 { 467 468 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 469 m->busy++; 470 } 471 472 void 473 vm_page_io_finish(vm_page_t m) 474 { 475 476 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 477 m->busy--; 478 if (m->busy == 0) 479 vm_page_flash(m); 480 } 481 482 /* 483 * Keep page from being freed by the page daemon 484 * much of the same effect as wiring, except much lower 485 * overhead and should be used only for *very* temporary 486 * holding ("wiring"). 487 */ 488 void 489 vm_page_hold(vm_page_t mem) 490 { 491 492 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 493 mem->hold_count++; 494 } 495 496 void 497 vm_page_unhold(vm_page_t mem) 498 { 499 500 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 501 --mem->hold_count; 502 KASSERT(mem->hold_count >= 0, ("vm_page_unhold: hold count < 0!!!")); 503 if (mem->hold_count == 0 && VM_PAGE_INQUEUE2(mem, PQ_HOLD)) 504 vm_page_free_toq(mem); 505 } 506 507 /* 508 * vm_page_free: 509 * 510 * Free a page. 511 */ 512 void 513 vm_page_free(vm_page_t m) 514 { 515 516 m->flags &= ~PG_ZERO; 517 vm_page_free_toq(m); 518 } 519 520 /* 521 * vm_page_free_zero: 522 * 523 * Free a page to the zerod-pages queue 524 */ 525 void 526 vm_page_free_zero(vm_page_t m) 527 { 528 529 m->flags |= PG_ZERO; 530 vm_page_free_toq(m); 531 } 532 533 /* 534 * vm_page_sleep: 535 * 536 * Sleep and release the page queues lock. 537 * 538 * The object containing the given page must be locked. 539 */ 540 void 541 vm_page_sleep(vm_page_t m, const char *msg) 542 { 543 544 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 545 if (!mtx_owned(&vm_page_queue_mtx)) 546 vm_page_lock_queues(); 547 vm_page_flag_set(m, PG_REFERENCED); 548 vm_page_unlock_queues(); 549 550 /* 551 * It's possible that while we sleep, the page will get 552 * unbusied and freed. If we are holding the object 553 * lock, we will assume we hold a reference to the object 554 * such that even if m->object changes, we can re-lock 555 * it. 556 */ 557 m->oflags |= VPO_WANTED; 558 msleep(m, VM_OBJECT_MTX(m->object), PVM, msg, 0); 559 } 560 561 /* 562 * vm_page_dirty: 563 * 564 * make page all dirty 565 */ 566 void 567 vm_page_dirty(vm_page_t m) 568 { 569 570 KASSERT((m->flags & PG_CACHED) == 0, 571 ("vm_page_dirty: page in cache!")); 572 KASSERT(!VM_PAGE_IS_FREE(m), 573 ("vm_page_dirty: page is free!")); 574 KASSERT(m->valid == VM_PAGE_BITS_ALL, 575 ("vm_page_dirty: page is invalid!")); 576 m->dirty = VM_PAGE_BITS_ALL; 577 } 578 579 /* 580 * vm_page_splay: 581 * 582 * Implements Sleator and Tarjan's top-down splay algorithm. Returns 583 * the vm_page containing the given pindex. If, however, that 584 * pindex is not found in the vm_object, returns a vm_page that is 585 * adjacent to the pindex, coming before or after it. 586 */ 587 vm_page_t 588 vm_page_splay(vm_pindex_t pindex, vm_page_t root) 589 { 590 struct vm_page dummy; 591 vm_page_t lefttreemax, righttreemin, y; 592 593 if (root == NULL) 594 return (root); 595 lefttreemax = righttreemin = &dummy; 596 for (;; root = y) { 597 if (pindex < root->pindex) { 598 if ((y = root->left) == NULL) 599 break; 600 if (pindex < y->pindex) { 601 /* Rotate right. */ 602 root->left = y->right; 603 y->right = root; 604 root = y; 605 if ((y = root->left) == NULL) 606 break; 607 } 608 /* Link into the new root's right tree. */ 609 righttreemin->left = root; 610 righttreemin = root; 611 } else if (pindex > root->pindex) { 612 if ((y = root->right) == NULL) 613 break; 614 if (pindex > y->pindex) { 615 /* Rotate left. */ 616 root->right = y->left; 617 y->left = root; 618 root = y; 619 if ((y = root->right) == NULL) 620 break; 621 } 622 /* Link into the new root's left tree. */ 623 lefttreemax->right = root; 624 lefttreemax = root; 625 } else 626 break; 627 } 628 /* Assemble the new root. */ 629 lefttreemax->right = root->left; 630 righttreemin->left = root->right; 631 root->left = dummy.right; 632 root->right = dummy.left; 633 return (root); 634 } 635 636 /* 637 * vm_page_insert: [ internal use only ] 638 * 639 * Inserts the given mem entry into the object and object list. 640 * 641 * The pagetables are not updated but will presumably fault the page 642 * in if necessary, or if a kernel page the caller will at some point 643 * enter the page into the kernel's pmap. We are not allowed to block 644 * here so we *can't* do this anyway. 645 * 646 * The object and page must be locked. 647 * This routine may not block. 648 */ 649 void 650 vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex) 651 { 652 vm_page_t root; 653 654 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 655 if (m->object != NULL) 656 panic("vm_page_insert: page already inserted"); 657 658 /* 659 * Record the object/offset pair in this page 660 */ 661 m->object = object; 662 m->pindex = pindex; 663 664 /* 665 * Now link into the object's ordered list of backed pages. 666 */ 667 root = object->root; 668 if (root == NULL) { 669 m->left = NULL; 670 m->right = NULL; 671 TAILQ_INSERT_TAIL(&object->memq, m, listq); 672 } else { 673 root = vm_page_splay(pindex, root); 674 if (pindex < root->pindex) { 675 m->left = root->left; 676 m->right = root; 677 root->left = NULL; 678 TAILQ_INSERT_BEFORE(root, m, listq); 679 } else if (pindex == root->pindex) 680 panic("vm_page_insert: offset already allocated"); 681 else { 682 m->right = root->right; 683 m->left = root; 684 root->right = NULL; 685 TAILQ_INSERT_AFTER(&object->memq, root, m, listq); 686 } 687 } 688 object->root = m; 689 object->generation++; 690 691 /* 692 * show that the object has one more resident page. 693 */ 694 object->resident_page_count++; 695 /* 696 * Hold the vnode until the last page is released. 697 */ 698 if (object->resident_page_count == 1 && object->type == OBJT_VNODE) 699 vhold((struct vnode *)object->handle); 700 701 /* 702 * Since we are inserting a new and possibly dirty page, 703 * update the object's OBJ_MIGHTBEDIRTY flag. 704 */ 705 if (m->flags & PG_WRITEABLE) 706 vm_object_set_writeable_dirty(object); 707 } 708 709 /* 710 * vm_page_remove: 711 * NOTE: used by device pager as well -wfj 712 * 713 * Removes the given mem entry from the object/offset-page 714 * table and the object page list, but do not invalidate/terminate 715 * the backing store. 716 * 717 * The object and page must be locked. 718 * The underlying pmap entry (if any) is NOT removed here. 719 * This routine may not block. 720 */ 721 void 722 vm_page_remove(vm_page_t m) 723 { 724 vm_object_t object; 725 vm_page_t root; 726 727 if ((object = m->object) == NULL) 728 return; 729 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 730 if (m->oflags & VPO_BUSY) { 731 m->oflags &= ~VPO_BUSY; 732 vm_page_flash(m); 733 } 734 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 735 736 /* 737 * Now remove from the object's list of backed pages. 738 */ 739 if (m != object->root) 740 vm_page_splay(m->pindex, object->root); 741 if (m->left == NULL) 742 root = m->right; 743 else { 744 root = vm_page_splay(m->pindex, m->left); 745 root->right = m->right; 746 } 747 object->root = root; 748 TAILQ_REMOVE(&object->memq, m, listq); 749 750 /* 751 * And show that the object has one fewer resident page. 752 */ 753 object->resident_page_count--; 754 object->generation++; 755 /* 756 * The vnode may now be recycled. 757 */ 758 if (object->resident_page_count == 0 && object->type == OBJT_VNODE) 759 vdrop((struct vnode *)object->handle); 760 761 m->object = NULL; 762 } 763 764 /* 765 * vm_page_lookup: 766 * 767 * Returns the page associated with the object/offset 768 * pair specified; if none is found, NULL is returned. 769 * 770 * The object must be locked. 771 * This routine may not block. 772 * This is a critical path routine 773 */ 774 vm_page_t 775 vm_page_lookup(vm_object_t object, vm_pindex_t pindex) 776 { 777 vm_page_t m; 778 779 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 780 if ((m = object->root) != NULL && m->pindex != pindex) { 781 m = vm_page_splay(pindex, m); 782 if ((object->root = m)->pindex != pindex) 783 m = NULL; 784 } 785 return (m); 786 } 787 788 /* 789 * vm_page_rename: 790 * 791 * Move the given memory entry from its 792 * current object to the specified target object/offset. 793 * 794 * The object must be locked. 795 * This routine may not block. 796 * 797 * Note: swap associated with the page must be invalidated by the move. We 798 * have to do this for several reasons: (1) we aren't freeing the 799 * page, (2) we are dirtying the page, (3) the VM system is probably 800 * moving the page from object A to B, and will then later move 801 * the backing store from A to B and we can't have a conflict. 802 * 803 * Note: we *always* dirty the page. It is necessary both for the 804 * fact that we moved it, and because we may be invalidating 805 * swap. If the page is on the cache, we have to deactivate it 806 * or vm_page_dirty() will panic. Dirty pages are not allowed 807 * on the cache. 808 */ 809 void 810 vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex) 811 { 812 813 vm_page_remove(m); 814 vm_page_insert(m, new_object, new_pindex); 815 vm_page_dirty(m); 816 } 817 818 /* 819 * Convert all of the given object's cached pages that have a 820 * pindex within the given range into free pages. If the value 821 * zero is given for "end", then the range's upper bound is 822 * infinity. If the given object is backed by a vnode and it 823 * transitions from having one or more cached pages to none, the 824 * vnode's hold count is reduced. 825 */ 826 void 827 vm_page_cache_free(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 828 { 829 vm_page_t m, m_next; 830 boolean_t empty; 831 832 mtx_lock(&vm_page_queue_free_mtx); 833 if (__predict_false(object->cache == NULL)) { 834 mtx_unlock(&vm_page_queue_free_mtx); 835 return; 836 } 837 m = object->cache = vm_page_splay(start, object->cache); 838 if (m->pindex < start) { 839 if (m->right == NULL) 840 m = NULL; 841 else { 842 m_next = vm_page_splay(start, m->right); 843 m_next->left = m; 844 m->right = NULL; 845 m = object->cache = m_next; 846 } 847 } 848 849 /* 850 * At this point, "m" is either (1) a reference to the page 851 * with the least pindex that is greater than or equal to 852 * "start" or (2) NULL. 853 */ 854 for (; m != NULL && (m->pindex < end || end == 0); m = m_next) { 855 /* 856 * Find "m"'s successor and remove "m" from the 857 * object's cache. 858 */ 859 if (m->right == NULL) { 860 object->cache = m->left; 861 m_next = NULL; 862 } else { 863 m_next = vm_page_splay(start, m->right); 864 m_next->left = m->left; 865 object->cache = m_next; 866 } 867 /* Convert "m" to a free page. */ 868 m->object = NULL; 869 m->valid = 0; 870 /* Clear PG_CACHED and set PG_FREE. */ 871 m->flags ^= PG_CACHED | PG_FREE; 872 KASSERT((m->flags & (PG_CACHED | PG_FREE)) == PG_FREE, 873 ("vm_page_cache_free: page %p has inconsistent flags", m)); 874 cnt.v_cache_count--; 875 cnt.v_free_count++; 876 } 877 empty = object->cache == NULL; 878 mtx_unlock(&vm_page_queue_free_mtx); 879 if (object->type == OBJT_VNODE && empty) 880 vdrop(object->handle); 881 } 882 883 /* 884 * Returns the cached page that is associated with the given 885 * object and offset. If, however, none exists, returns NULL. 886 * 887 * The free page queue must be locked. 888 */ 889 static inline vm_page_t 890 vm_page_cache_lookup(vm_object_t object, vm_pindex_t pindex) 891 { 892 vm_page_t m; 893 894 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 895 if ((m = object->cache) != NULL && m->pindex != pindex) { 896 m = vm_page_splay(pindex, m); 897 if ((object->cache = m)->pindex != pindex) 898 m = NULL; 899 } 900 return (m); 901 } 902 903 /* 904 * Remove the given cached page from its containing object's 905 * collection of cached pages. 906 * 907 * The free page queue must be locked. 908 */ 909 void 910 vm_page_cache_remove(vm_page_t m) 911 { 912 vm_object_t object; 913 vm_page_t root; 914 915 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 916 KASSERT((m->flags & PG_CACHED) != 0, 917 ("vm_page_cache_remove: page %p is not cached", m)); 918 object = m->object; 919 if (m != object->cache) { 920 root = vm_page_splay(m->pindex, object->cache); 921 KASSERT(root == m, 922 ("vm_page_cache_remove: page %p is not cached in object %p", 923 m, object)); 924 } 925 if (m->left == NULL) 926 root = m->right; 927 else if (m->right == NULL) 928 root = m->left; 929 else { 930 root = vm_page_splay(m->pindex, m->left); 931 root->right = m->right; 932 } 933 object->cache = root; 934 m->object = NULL; 935 cnt.v_cache_count--; 936 } 937 938 /* 939 * Transfer all of the cached pages with offset greater than or 940 * equal to 'offidxstart' from the original object's cache to the 941 * new object's cache. However, any cached pages with offset 942 * greater than or equal to the new object's size are kept in the 943 * original object. Initially, the new object's cache must be 944 * empty. Offset 'offidxstart' in the original object must 945 * correspond to offset zero in the new object. 946 * 947 * The new object must be locked. 948 */ 949 void 950 vm_page_cache_transfer(vm_object_t orig_object, vm_pindex_t offidxstart, 951 vm_object_t new_object) 952 { 953 vm_page_t m, m_next; 954 955 /* 956 * Insertion into an object's collection of cached pages 957 * requires the object to be locked. In contrast, removal does 958 * not. 959 */ 960 VM_OBJECT_LOCK_ASSERT(new_object, MA_OWNED); 961 KASSERT(new_object->cache == NULL, 962 ("vm_page_cache_transfer: object %p has cached pages", 963 new_object)); 964 mtx_lock(&vm_page_queue_free_mtx); 965 if ((m = orig_object->cache) != NULL) { 966 /* 967 * Transfer all of the pages with offset greater than or 968 * equal to 'offidxstart' from the original object's 969 * cache to the new object's cache. 970 */ 971 m = vm_page_splay(offidxstart, m); 972 if (m->pindex < offidxstart) { 973 orig_object->cache = m; 974 new_object->cache = m->right; 975 m->right = NULL; 976 } else { 977 orig_object->cache = m->left; 978 new_object->cache = m; 979 m->left = NULL; 980 } 981 while ((m = new_object->cache) != NULL) { 982 if ((m->pindex - offidxstart) >= new_object->size) { 983 /* 984 * Return all of the cached pages with 985 * offset greater than or equal to the 986 * new object's size to the original 987 * object's cache. 988 */ 989 new_object->cache = m->left; 990 m->left = orig_object->cache; 991 orig_object->cache = m; 992 break; 993 } 994 m_next = vm_page_splay(m->pindex, m->right); 995 /* Update the page's object and offset. */ 996 m->object = new_object; 997 m->pindex -= offidxstart; 998 if (m_next == NULL) 999 break; 1000 m->right = NULL; 1001 m_next->left = m; 1002 new_object->cache = m_next; 1003 } 1004 KASSERT(new_object->cache == NULL || 1005 new_object->type == OBJT_SWAP, 1006 ("vm_page_cache_transfer: object %p's type is incompatible" 1007 " with cached pages", new_object)); 1008 } 1009 mtx_unlock(&vm_page_queue_free_mtx); 1010 } 1011 1012 /* 1013 * vm_page_alloc: 1014 * 1015 * Allocate and return a memory cell associated 1016 * with this VM object/offset pair. 1017 * 1018 * page_req classes: 1019 * VM_ALLOC_NORMAL normal process request 1020 * VM_ALLOC_SYSTEM system *really* needs a page 1021 * VM_ALLOC_INTERRUPT interrupt time request 1022 * VM_ALLOC_ZERO zero page 1023 * 1024 * This routine may not block. 1025 */ 1026 vm_page_t 1027 vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req) 1028 { 1029 struct vnode *vp = NULL; 1030 vm_object_t m_object; 1031 vm_page_t m; 1032 int flags, page_req; 1033 1034 page_req = req & VM_ALLOC_CLASS_MASK; 1035 KASSERT(curthread->td_intr_nesting_level == 0 || 1036 page_req == VM_ALLOC_INTERRUPT, 1037 ("vm_page_alloc(NORMAL|SYSTEM) in interrupt context")); 1038 1039 if ((req & VM_ALLOC_NOOBJ) == 0) { 1040 KASSERT(object != NULL, 1041 ("vm_page_alloc: NULL object.")); 1042 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1043 } 1044 1045 /* 1046 * The pager is allowed to eat deeper into the free page list. 1047 */ 1048 if ((curproc == pageproc) && (page_req != VM_ALLOC_INTERRUPT)) { 1049 page_req = VM_ALLOC_SYSTEM; 1050 }; 1051 1052 mtx_lock(&vm_page_queue_free_mtx); 1053 if (cnt.v_free_count + cnt.v_cache_count > cnt.v_free_reserved || 1054 (page_req == VM_ALLOC_SYSTEM && 1055 cnt.v_free_count + cnt.v_cache_count > cnt.v_interrupt_free_min) || 1056 (page_req == VM_ALLOC_INTERRUPT && 1057 cnt.v_free_count + cnt.v_cache_count > 0)) { 1058 /* 1059 * Allocate from the free queue if the number of free pages 1060 * exceeds the minimum for the request class. 1061 */ 1062 if (object != NULL && 1063 (m = vm_page_cache_lookup(object, pindex)) != NULL) { 1064 if ((req & VM_ALLOC_IFNOTCACHED) != 0) { 1065 mtx_unlock(&vm_page_queue_free_mtx); 1066 return (NULL); 1067 } 1068 if (vm_phys_unfree_page(m)) 1069 vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m, 0); 1070 #if VM_NRESERVLEVEL > 0 1071 else if (!vm_reserv_reactivate_page(m)) 1072 #else 1073 else 1074 #endif 1075 panic("vm_page_alloc: cache page %p is missing" 1076 " from the free queue", m); 1077 } else if ((req & VM_ALLOC_IFCACHED) != 0) { 1078 mtx_unlock(&vm_page_queue_free_mtx); 1079 return (NULL); 1080 #if VM_NRESERVLEVEL > 0 1081 } else if (object == NULL || object->type == OBJT_DEVICE || 1082 (object->flags & OBJ_COLORED) == 0 || 1083 (m = vm_reserv_alloc_page(object, pindex)) == NULL) { 1084 #else 1085 } else { 1086 #endif 1087 m = vm_phys_alloc_pages(object != NULL ? 1088 VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT, 0); 1089 #if VM_NRESERVLEVEL > 0 1090 if (m == NULL && vm_reserv_reclaim_inactive()) { 1091 m = vm_phys_alloc_pages(object != NULL ? 1092 VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT, 1093 0); 1094 } 1095 #endif 1096 } 1097 } else { 1098 /* 1099 * Not allocatable, give up. 1100 */ 1101 mtx_unlock(&vm_page_queue_free_mtx); 1102 atomic_add_int(&vm_pageout_deficit, 1); 1103 pagedaemon_wakeup(); 1104 return (NULL); 1105 } 1106 1107 /* 1108 * At this point we had better have found a good page. 1109 */ 1110 1111 KASSERT( 1112 m != NULL, 1113 ("vm_page_alloc(): missing page on free queue") 1114 ); 1115 if ((m->flags & PG_CACHED) != 0) { 1116 KASSERT(m->valid != 0, 1117 ("vm_page_alloc: cached page %p is invalid", m)); 1118 if (m->object == object && m->pindex == pindex) 1119 cnt.v_reactivated++; 1120 else 1121 m->valid = 0; 1122 m_object = m->object; 1123 vm_page_cache_remove(m); 1124 if (m_object->type == OBJT_VNODE && m_object->cache == NULL) 1125 vp = m_object->handle; 1126 } else { 1127 KASSERT(VM_PAGE_IS_FREE(m), 1128 ("vm_page_alloc: page %p is not free", m)); 1129 KASSERT(m->valid == 0, 1130 ("vm_page_alloc: free page %p is valid", m)); 1131 cnt.v_free_count--; 1132 } 1133 1134 /* 1135 * Initialize structure. Only the PG_ZERO flag is inherited. 1136 */ 1137 flags = 0; 1138 if (m->flags & PG_ZERO) { 1139 vm_page_zero_count--; 1140 if (req & VM_ALLOC_ZERO) 1141 flags = PG_ZERO; 1142 } 1143 if (object == NULL || object->type == OBJT_PHYS) 1144 flags |= PG_UNMANAGED; 1145 m->flags = flags; 1146 if (req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ)) 1147 m->oflags = 0; 1148 else 1149 m->oflags = VPO_BUSY; 1150 if (req & VM_ALLOC_WIRED) { 1151 atomic_add_int(&cnt.v_wire_count, 1); 1152 m->wire_count = 1; 1153 } else 1154 m->wire_count = 0; 1155 m->hold_count = 0; 1156 m->act_count = 0; 1157 m->busy = 0; 1158 KASSERT(m->dirty == 0, ("vm_page_alloc: free/cache page %p was dirty", m)); 1159 mtx_unlock(&vm_page_queue_free_mtx); 1160 1161 if ((req & VM_ALLOC_NOOBJ) == 0) 1162 vm_page_insert(m, object, pindex); 1163 else 1164 m->pindex = pindex; 1165 1166 /* 1167 * The following call to vdrop() must come after the above call 1168 * to vm_page_insert() in case both affect the same object and 1169 * vnode. Otherwise, the affected vnode's hold count could 1170 * temporarily become zero. 1171 */ 1172 if (vp != NULL) 1173 vdrop(vp); 1174 1175 /* 1176 * Don't wakeup too often - wakeup the pageout daemon when 1177 * we would be nearly out of memory. 1178 */ 1179 if (vm_paging_needed()) 1180 pagedaemon_wakeup(); 1181 1182 return (m); 1183 } 1184 1185 /* 1186 * vm_wait: (also see VM_WAIT macro) 1187 * 1188 * Block until free pages are available for allocation 1189 * - Called in various places before memory allocations. 1190 */ 1191 void 1192 vm_wait(void) 1193 { 1194 1195 mtx_lock(&vm_page_queue_free_mtx); 1196 if (curproc == pageproc) { 1197 vm_pageout_pages_needed = 1; 1198 msleep(&vm_pageout_pages_needed, &vm_page_queue_free_mtx, 1199 PDROP | PSWP, "VMWait", 0); 1200 } else { 1201 if (!vm_pages_needed) { 1202 vm_pages_needed = 1; 1203 wakeup(&vm_pages_needed); 1204 } 1205 msleep(&cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PVM, 1206 "vmwait", 0); 1207 } 1208 } 1209 1210 /* 1211 * vm_waitpfault: (also see VM_WAITPFAULT macro) 1212 * 1213 * Block until free pages are available for allocation 1214 * - Called only in vm_fault so that processes page faulting 1215 * can be easily tracked. 1216 * - Sleeps at a lower priority than vm_wait() so that vm_wait()ing 1217 * processes will be able to grab memory first. Do not change 1218 * this balance without careful testing first. 1219 */ 1220 void 1221 vm_waitpfault(void) 1222 { 1223 1224 mtx_lock(&vm_page_queue_free_mtx); 1225 if (!vm_pages_needed) { 1226 vm_pages_needed = 1; 1227 wakeup(&vm_pages_needed); 1228 } 1229 msleep(&cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PUSER, 1230 "pfault", 0); 1231 } 1232 1233 /* 1234 * vm_page_requeue: 1235 * 1236 * If the given page is contained within a page queue, move it to the tail 1237 * of that queue. 1238 * 1239 * The page queues must be locked. 1240 */ 1241 void 1242 vm_page_requeue(vm_page_t m) 1243 { 1244 int queue = VM_PAGE_GETQUEUE(m); 1245 struct vpgqueues *vpq; 1246 1247 if (queue != PQ_NONE) { 1248 vpq = &vm_page_queues[queue]; 1249 TAILQ_REMOVE(&vpq->pl, m, pageq); 1250 TAILQ_INSERT_TAIL(&vpq->pl, m, pageq); 1251 } 1252 } 1253 1254 /* 1255 * vm_pageq_remove: 1256 * 1257 * Remove a page from its queue. 1258 * 1259 * The queue containing the given page must be locked. 1260 * This routine may not block. 1261 */ 1262 void 1263 vm_pageq_remove(vm_page_t m) 1264 { 1265 int queue = VM_PAGE_GETQUEUE(m); 1266 struct vpgqueues *pq; 1267 1268 if (queue != PQ_NONE) { 1269 VM_PAGE_SETQUEUE2(m, PQ_NONE); 1270 pq = &vm_page_queues[queue]; 1271 TAILQ_REMOVE(&pq->pl, m, pageq); 1272 (*pq->cnt)--; 1273 } 1274 } 1275 1276 /* 1277 * vm_page_enqueue: 1278 * 1279 * Add the given page to the specified queue. 1280 * 1281 * The page queues must be locked. 1282 */ 1283 static void 1284 vm_page_enqueue(int queue, vm_page_t m) 1285 { 1286 struct vpgqueues *vpq; 1287 1288 vpq = &vm_page_queues[queue]; 1289 VM_PAGE_SETQUEUE2(m, queue); 1290 TAILQ_INSERT_TAIL(&vpq->pl, m, pageq); 1291 ++*vpq->cnt; 1292 } 1293 1294 /* 1295 * vm_page_activate: 1296 * 1297 * Put the specified page on the active list (if appropriate). 1298 * Ensure that act_count is at least ACT_INIT but do not otherwise 1299 * mess with it. 1300 * 1301 * The page queues must be locked. 1302 * This routine may not block. 1303 */ 1304 void 1305 vm_page_activate(vm_page_t m) 1306 { 1307 1308 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1309 if (VM_PAGE_GETKNOWNQUEUE2(m) != PQ_ACTIVE) { 1310 vm_pageq_remove(m); 1311 if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) { 1312 if (m->act_count < ACT_INIT) 1313 m->act_count = ACT_INIT; 1314 vm_page_enqueue(PQ_ACTIVE, m); 1315 } 1316 } else { 1317 if (m->act_count < ACT_INIT) 1318 m->act_count = ACT_INIT; 1319 } 1320 } 1321 1322 /* 1323 * vm_page_free_wakeup: 1324 * 1325 * Helper routine for vm_page_free_toq() and vm_page_cache(). This 1326 * routine is called when a page has been added to the cache or free 1327 * queues. 1328 * 1329 * The page queues must be locked. 1330 * This routine may not block. 1331 */ 1332 static inline void 1333 vm_page_free_wakeup(void) 1334 { 1335 1336 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 1337 /* 1338 * if pageout daemon needs pages, then tell it that there are 1339 * some free. 1340 */ 1341 if (vm_pageout_pages_needed && 1342 cnt.v_cache_count + cnt.v_free_count >= cnt.v_pageout_free_min) { 1343 wakeup(&vm_pageout_pages_needed); 1344 vm_pageout_pages_needed = 0; 1345 } 1346 /* 1347 * wakeup processes that are waiting on memory if we hit a 1348 * high water mark. And wakeup scheduler process if we have 1349 * lots of memory. this process will swapin processes. 1350 */ 1351 if (vm_pages_needed && !vm_page_count_min()) { 1352 vm_pages_needed = 0; 1353 wakeup(&cnt.v_free_count); 1354 } 1355 } 1356 1357 /* 1358 * vm_page_free_toq: 1359 * 1360 * Returns the given page to the free list, 1361 * disassociating it with any VM object. 1362 * 1363 * Object and page must be locked prior to entry. 1364 * This routine may not block. 1365 */ 1366 1367 void 1368 vm_page_free_toq(vm_page_t m) 1369 { 1370 1371 if (VM_PAGE_GETQUEUE(m) != PQ_NONE) 1372 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1373 KASSERT(!pmap_page_is_mapped(m), 1374 ("vm_page_free_toq: freeing mapped page %p", m)); 1375 PCPU_INC(cnt.v_tfree); 1376 1377 if (m->busy || VM_PAGE_IS_FREE(m)) { 1378 printf( 1379 "vm_page_free: pindex(%lu), busy(%d), VPO_BUSY(%d), hold(%d)\n", 1380 (u_long)m->pindex, m->busy, (m->oflags & VPO_BUSY) ? 1 : 0, 1381 m->hold_count); 1382 if (VM_PAGE_IS_FREE(m)) 1383 panic("vm_page_free: freeing free page"); 1384 else 1385 panic("vm_page_free: freeing busy page"); 1386 } 1387 1388 /* 1389 * unqueue, then remove page. Note that we cannot destroy 1390 * the page here because we do not want to call the pager's 1391 * callback routine until after we've put the page on the 1392 * appropriate free queue. 1393 */ 1394 vm_pageq_remove(m); 1395 vm_page_remove(m); 1396 1397 /* 1398 * If fictitious remove object association and 1399 * return, otherwise delay object association removal. 1400 */ 1401 if ((m->flags & PG_FICTITIOUS) != 0) { 1402 return; 1403 } 1404 1405 m->valid = 0; 1406 vm_page_undirty(m); 1407 1408 if (m->wire_count != 0) { 1409 if (m->wire_count > 1) { 1410 panic("vm_page_free: invalid wire count (%d), pindex: 0x%lx", 1411 m->wire_count, (long)m->pindex); 1412 } 1413 panic("vm_page_free: freeing wired page"); 1414 } 1415 if (m->hold_count != 0) { 1416 m->flags &= ~PG_ZERO; 1417 vm_page_enqueue(PQ_HOLD, m); 1418 } else { 1419 mtx_lock(&vm_page_queue_free_mtx); 1420 m->flags |= PG_FREE; 1421 cnt.v_free_count++; 1422 #if VM_NRESERVLEVEL > 0 1423 if (!vm_reserv_free_page(m)) 1424 #else 1425 if (TRUE) 1426 #endif 1427 vm_phys_free_pages(m, 0); 1428 if ((m->flags & PG_ZERO) != 0) 1429 ++vm_page_zero_count; 1430 else 1431 vm_page_zero_idle_wakeup(); 1432 vm_page_free_wakeup(); 1433 mtx_unlock(&vm_page_queue_free_mtx); 1434 } 1435 } 1436 1437 /* 1438 * vm_page_wire: 1439 * 1440 * Mark this page as wired down by yet 1441 * another map, removing it from paging queues 1442 * as necessary. 1443 * 1444 * The page queues must be locked. 1445 * This routine may not block. 1446 */ 1447 void 1448 vm_page_wire(vm_page_t m) 1449 { 1450 1451 /* 1452 * Only bump the wire statistics if the page is not already wired, 1453 * and only unqueue the page if it is on some queue (if it is unmanaged 1454 * it is already off the queues). 1455 */ 1456 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1457 if (m->flags & PG_FICTITIOUS) 1458 return; 1459 if (m->wire_count == 0) { 1460 if ((m->flags & PG_UNMANAGED) == 0) 1461 vm_pageq_remove(m); 1462 atomic_add_int(&cnt.v_wire_count, 1); 1463 } 1464 m->wire_count++; 1465 KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m)); 1466 } 1467 1468 /* 1469 * vm_page_unwire: 1470 * 1471 * Release one wiring of this page, potentially 1472 * enabling it to be paged again. 1473 * 1474 * Many pages placed on the inactive queue should actually go 1475 * into the cache, but it is difficult to figure out which. What 1476 * we do instead, if the inactive target is well met, is to put 1477 * clean pages at the head of the inactive queue instead of the tail. 1478 * This will cause them to be moved to the cache more quickly and 1479 * if not actively re-referenced, freed more quickly. If we just 1480 * stick these pages at the end of the inactive queue, heavy filesystem 1481 * meta-data accesses can cause an unnecessary paging load on memory bound 1482 * processes. This optimization causes one-time-use metadata to be 1483 * reused more quickly. 1484 * 1485 * BUT, if we are in a low-memory situation we have no choice but to 1486 * put clean pages on the cache queue. 1487 * 1488 * A number of routines use vm_page_unwire() to guarantee that the page 1489 * will go into either the inactive or active queues, and will NEVER 1490 * be placed in the cache - for example, just after dirtying a page. 1491 * dirty pages in the cache are not allowed. 1492 * 1493 * The page queues must be locked. 1494 * This routine may not block. 1495 */ 1496 void 1497 vm_page_unwire(vm_page_t m, int activate) 1498 { 1499 1500 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1501 if (m->flags & PG_FICTITIOUS) 1502 return; 1503 if (m->wire_count > 0) { 1504 m->wire_count--; 1505 if (m->wire_count == 0) { 1506 atomic_subtract_int(&cnt.v_wire_count, 1); 1507 if (m->flags & PG_UNMANAGED) { 1508 ; 1509 } else if (activate) 1510 vm_page_enqueue(PQ_ACTIVE, m); 1511 else { 1512 vm_page_flag_clear(m, PG_WINATCFLS); 1513 vm_page_enqueue(PQ_INACTIVE, m); 1514 } 1515 } 1516 } else { 1517 panic("vm_page_unwire: invalid wire count: %d", m->wire_count); 1518 } 1519 } 1520 1521 1522 /* 1523 * Move the specified page to the inactive queue. If the page has 1524 * any associated swap, the swap is deallocated. 1525 * 1526 * Normally athead is 0 resulting in LRU operation. athead is set 1527 * to 1 if we want this page to be 'as if it were placed in the cache', 1528 * except without unmapping it from the process address space. 1529 * 1530 * This routine may not block. 1531 */ 1532 static inline void 1533 _vm_page_deactivate(vm_page_t m, int athead) 1534 { 1535 1536 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1537 1538 /* 1539 * Ignore if already inactive. 1540 */ 1541 if (VM_PAGE_INQUEUE2(m, PQ_INACTIVE)) 1542 return; 1543 if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) { 1544 vm_page_flag_clear(m, PG_WINATCFLS); 1545 vm_pageq_remove(m); 1546 if (athead) 1547 TAILQ_INSERT_HEAD(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 1548 else 1549 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 1550 VM_PAGE_SETQUEUE2(m, PQ_INACTIVE); 1551 cnt.v_inactive_count++; 1552 } 1553 } 1554 1555 void 1556 vm_page_deactivate(vm_page_t m) 1557 { 1558 _vm_page_deactivate(m, 0); 1559 } 1560 1561 /* 1562 * vm_page_try_to_cache: 1563 * 1564 * Returns 0 on failure, 1 on success 1565 */ 1566 int 1567 vm_page_try_to_cache(vm_page_t m) 1568 { 1569 1570 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1571 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1572 if (m->dirty || m->hold_count || m->busy || m->wire_count || 1573 (m->oflags & VPO_BUSY) || (m->flags & PG_UNMANAGED)) { 1574 return (0); 1575 } 1576 pmap_remove_all(m); 1577 if (m->dirty) 1578 return (0); 1579 vm_page_cache(m); 1580 return (1); 1581 } 1582 1583 /* 1584 * vm_page_try_to_free() 1585 * 1586 * Attempt to free the page. If we cannot free it, we do nothing. 1587 * 1 is returned on success, 0 on failure. 1588 */ 1589 int 1590 vm_page_try_to_free(vm_page_t m) 1591 { 1592 1593 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1594 if (m->object != NULL) 1595 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1596 if (m->dirty || m->hold_count || m->busy || m->wire_count || 1597 (m->oflags & VPO_BUSY) || (m->flags & PG_UNMANAGED)) { 1598 return (0); 1599 } 1600 pmap_remove_all(m); 1601 if (m->dirty) 1602 return (0); 1603 vm_page_free(m); 1604 return (1); 1605 } 1606 1607 /* 1608 * vm_page_cache 1609 * 1610 * Put the specified page onto the page cache queue (if appropriate). 1611 * 1612 * This routine may not block. 1613 */ 1614 void 1615 vm_page_cache(vm_page_t m) 1616 { 1617 vm_object_t object; 1618 vm_page_t root; 1619 1620 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1621 object = m->object; 1622 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1623 if ((m->flags & PG_UNMANAGED) || (m->oflags & VPO_BUSY) || m->busy || 1624 m->hold_count || m->wire_count) { 1625 panic("vm_page_cache: attempting to cache busy page"); 1626 } 1627 pmap_remove_all(m); 1628 if (m->dirty != 0) 1629 panic("vm_page_cache: page %p is dirty", m); 1630 if (m->valid == 0 || object->type == OBJT_DEFAULT || 1631 (object->type == OBJT_SWAP && 1632 !vm_pager_has_page(object, m->pindex, NULL, NULL))) { 1633 /* 1634 * Hypothesis: A cache-elgible page belonging to a 1635 * default object or swap object but without a backing 1636 * store must be zero filled. 1637 */ 1638 vm_page_free(m); 1639 return; 1640 } 1641 KASSERT((m->flags & PG_CACHED) == 0, 1642 ("vm_page_cache: page %p is already cached", m)); 1643 cnt.v_tcached++; 1644 1645 /* 1646 * Remove the page from the paging queues. 1647 */ 1648 vm_pageq_remove(m); 1649 1650 /* 1651 * Remove the page from the object's collection of resident 1652 * pages. 1653 */ 1654 if (m != object->root) 1655 vm_page_splay(m->pindex, object->root); 1656 if (m->left == NULL) 1657 root = m->right; 1658 else { 1659 root = vm_page_splay(m->pindex, m->left); 1660 root->right = m->right; 1661 } 1662 object->root = root; 1663 TAILQ_REMOVE(&object->memq, m, listq); 1664 object->resident_page_count--; 1665 object->generation++; 1666 1667 /* 1668 * Insert the page into the object's collection of cached pages 1669 * and the physical memory allocator's cache/free page queues. 1670 */ 1671 vm_page_flag_clear(m, PG_ZERO); 1672 mtx_lock(&vm_page_queue_free_mtx); 1673 m->flags |= PG_CACHED; 1674 cnt.v_cache_count++; 1675 root = object->cache; 1676 if (root == NULL) { 1677 m->left = NULL; 1678 m->right = NULL; 1679 } else { 1680 root = vm_page_splay(m->pindex, root); 1681 if (m->pindex < root->pindex) { 1682 m->left = root->left; 1683 m->right = root; 1684 root->left = NULL; 1685 } else if (__predict_false(m->pindex == root->pindex)) 1686 panic("vm_page_cache: offset already cached"); 1687 else { 1688 m->right = root->right; 1689 m->left = root; 1690 root->right = NULL; 1691 } 1692 } 1693 object->cache = m; 1694 #if VM_NRESERVLEVEL > 0 1695 if (!vm_reserv_free_page(m)) { 1696 #else 1697 if (TRUE) { 1698 #endif 1699 vm_phys_set_pool(VM_FREEPOOL_CACHE, m, 0); 1700 vm_phys_free_pages(m, 0); 1701 } 1702 vm_page_free_wakeup(); 1703 mtx_unlock(&vm_page_queue_free_mtx); 1704 1705 /* 1706 * Increment the vnode's hold count if this is the object's only 1707 * cached page. Decrement the vnode's hold count if this was 1708 * the object's only resident page. 1709 */ 1710 if (object->type == OBJT_VNODE) { 1711 if (root == NULL && object->resident_page_count != 0) 1712 vhold(object->handle); 1713 else if (root != NULL && object->resident_page_count == 0) 1714 vdrop(object->handle); 1715 } 1716 } 1717 1718 /* 1719 * vm_page_dontneed 1720 * 1721 * Cache, deactivate, or do nothing as appropriate. This routine 1722 * is typically used by madvise() MADV_DONTNEED. 1723 * 1724 * Generally speaking we want to move the page into the cache so 1725 * it gets reused quickly. However, this can result in a silly syndrome 1726 * due to the page recycling too quickly. Small objects will not be 1727 * fully cached. On the otherhand, if we move the page to the inactive 1728 * queue we wind up with a problem whereby very large objects 1729 * unnecessarily blow away our inactive and cache queues. 1730 * 1731 * The solution is to move the pages based on a fixed weighting. We 1732 * either leave them alone, deactivate them, or move them to the cache, 1733 * where moving them to the cache has the highest weighting. 1734 * By forcing some pages into other queues we eventually force the 1735 * system to balance the queues, potentially recovering other unrelated 1736 * space from active. The idea is to not force this to happen too 1737 * often. 1738 */ 1739 void 1740 vm_page_dontneed(vm_page_t m) 1741 { 1742 static int dnweight; 1743 int dnw; 1744 int head; 1745 1746 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1747 dnw = ++dnweight; 1748 1749 /* 1750 * occassionally leave the page alone 1751 */ 1752 if ((dnw & 0x01F0) == 0 || 1753 VM_PAGE_INQUEUE2(m, PQ_INACTIVE)) { 1754 if (m->act_count >= ACT_INIT) 1755 --m->act_count; 1756 return; 1757 } 1758 1759 /* 1760 * Clear any references to the page. Otherwise, the page daemon will 1761 * immediately reactivate the page. 1762 */ 1763 vm_page_flag_clear(m, PG_REFERENCED); 1764 pmap_clear_reference(m); 1765 1766 if (m->dirty == 0 && pmap_is_modified(m)) 1767 vm_page_dirty(m); 1768 1769 if (m->dirty || (dnw & 0x0070) == 0) { 1770 /* 1771 * Deactivate the page 3 times out of 32. 1772 */ 1773 head = 0; 1774 } else { 1775 /* 1776 * Cache the page 28 times out of every 32. Note that 1777 * the page is deactivated instead of cached, but placed 1778 * at the head of the queue instead of the tail. 1779 */ 1780 head = 1; 1781 } 1782 _vm_page_deactivate(m, head); 1783 } 1784 1785 /* 1786 * Grab a page, waiting until we are waken up due to the page 1787 * changing state. We keep on waiting, if the page continues 1788 * to be in the object. If the page doesn't exist, first allocate it 1789 * and then conditionally zero it. 1790 * 1791 * This routine may block. 1792 */ 1793 vm_page_t 1794 vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags) 1795 { 1796 vm_page_t m; 1797 1798 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1799 retrylookup: 1800 if ((m = vm_page_lookup(object, pindex)) != NULL) { 1801 if (vm_page_sleep_if_busy(m, TRUE, "pgrbwt")) { 1802 if ((allocflags & VM_ALLOC_RETRY) == 0) 1803 return (NULL); 1804 goto retrylookup; 1805 } else { 1806 if ((allocflags & VM_ALLOC_WIRED) != 0) { 1807 vm_page_lock_queues(); 1808 vm_page_wire(m); 1809 vm_page_unlock_queues(); 1810 } 1811 if ((allocflags & VM_ALLOC_NOBUSY) == 0) 1812 vm_page_busy(m); 1813 return (m); 1814 } 1815 } 1816 m = vm_page_alloc(object, pindex, allocflags & ~VM_ALLOC_RETRY); 1817 if (m == NULL) { 1818 VM_OBJECT_UNLOCK(object); 1819 VM_WAIT; 1820 VM_OBJECT_LOCK(object); 1821 if ((allocflags & VM_ALLOC_RETRY) == 0) 1822 return (NULL); 1823 goto retrylookup; 1824 } else if (m->valid != 0) 1825 return (m); 1826 if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0) 1827 pmap_zero_page(m); 1828 return (m); 1829 } 1830 1831 /* 1832 * Mapping function for valid bits or for dirty bits in 1833 * a page. May not block. 1834 * 1835 * Inputs are required to range within a page. 1836 */ 1837 int 1838 vm_page_bits(int base, int size) 1839 { 1840 int first_bit; 1841 int last_bit; 1842 1843 KASSERT( 1844 base + size <= PAGE_SIZE, 1845 ("vm_page_bits: illegal base/size %d/%d", base, size) 1846 ); 1847 1848 if (size == 0) /* handle degenerate case */ 1849 return (0); 1850 1851 first_bit = base >> DEV_BSHIFT; 1852 last_bit = (base + size - 1) >> DEV_BSHIFT; 1853 1854 return ((2 << last_bit) - (1 << first_bit)); 1855 } 1856 1857 /* 1858 * vm_page_set_valid: 1859 * 1860 * Sets portions of a page valid. The arguments are expected 1861 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive 1862 * of any partial chunks touched by the range. The invalid portion of 1863 * such chunks will be zeroed. 1864 * 1865 * (base + size) must be less then or equal to PAGE_SIZE. 1866 */ 1867 void 1868 vm_page_set_valid(vm_page_t m, int base, int size) 1869 { 1870 int endoff, frag; 1871 1872 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1873 if (size == 0) /* handle degenerate case */ 1874 return; 1875 1876 /* 1877 * If the base is not DEV_BSIZE aligned and the valid 1878 * bit is clear, we have to zero out a portion of the 1879 * first block. 1880 */ 1881 if ((frag = base & ~(DEV_BSIZE - 1)) != base && 1882 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0) 1883 pmap_zero_page_area(m, frag, base - frag); 1884 1885 /* 1886 * If the ending offset is not DEV_BSIZE aligned and the 1887 * valid bit is clear, we have to zero out a portion of 1888 * the last block. 1889 */ 1890 endoff = base + size; 1891 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff && 1892 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0) 1893 pmap_zero_page_area(m, endoff, 1894 DEV_BSIZE - (endoff & (DEV_BSIZE - 1))); 1895 1896 /* 1897 * Assert that no previously invalid block that is now being validated 1898 * is already dirty. 1899 */ 1900 KASSERT((~m->valid & vm_page_bits(base, size) & m->dirty) == 0, 1901 ("vm_page_set_valid: page %p is dirty", m)); 1902 1903 /* 1904 * Set valid bits inclusive of any overlap. 1905 */ 1906 m->valid |= vm_page_bits(base, size); 1907 } 1908 1909 /* 1910 * vm_page_set_validclean: 1911 * 1912 * Sets portions of a page valid and clean. The arguments are expected 1913 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive 1914 * of any partial chunks touched by the range. The invalid portion of 1915 * such chunks will be zero'd. 1916 * 1917 * This routine may not block. 1918 * 1919 * (base + size) must be less then or equal to PAGE_SIZE. 1920 */ 1921 void 1922 vm_page_set_validclean(vm_page_t m, int base, int size) 1923 { 1924 int pagebits; 1925 int frag; 1926 int endoff; 1927 1928 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1929 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1930 if (size == 0) /* handle degenerate case */ 1931 return; 1932 1933 /* 1934 * If the base is not DEV_BSIZE aligned and the valid 1935 * bit is clear, we have to zero out a portion of the 1936 * first block. 1937 */ 1938 if ((frag = base & ~(DEV_BSIZE - 1)) != base && 1939 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0) 1940 pmap_zero_page_area(m, frag, base - frag); 1941 1942 /* 1943 * If the ending offset is not DEV_BSIZE aligned and the 1944 * valid bit is clear, we have to zero out a portion of 1945 * the last block. 1946 */ 1947 endoff = base + size; 1948 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff && 1949 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0) 1950 pmap_zero_page_area(m, endoff, 1951 DEV_BSIZE - (endoff & (DEV_BSIZE - 1))); 1952 1953 /* 1954 * Set valid, clear dirty bits. If validating the entire 1955 * page we can safely clear the pmap modify bit. We also 1956 * use this opportunity to clear the VPO_NOSYNC flag. If a process 1957 * takes a write fault on a MAP_NOSYNC memory area the flag will 1958 * be set again. 1959 * 1960 * We set valid bits inclusive of any overlap, but we can only 1961 * clear dirty bits for DEV_BSIZE chunks that are fully within 1962 * the range. 1963 */ 1964 pagebits = vm_page_bits(base, size); 1965 m->valid |= pagebits; 1966 #if 0 /* NOT YET */ 1967 if ((frag = base & (DEV_BSIZE - 1)) != 0) { 1968 frag = DEV_BSIZE - frag; 1969 base += frag; 1970 size -= frag; 1971 if (size < 0) 1972 size = 0; 1973 } 1974 pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1)); 1975 #endif 1976 m->dirty &= ~pagebits; 1977 if (base == 0 && size == PAGE_SIZE) { 1978 pmap_clear_modify(m); 1979 m->oflags &= ~VPO_NOSYNC; 1980 } 1981 } 1982 1983 void 1984 vm_page_clear_dirty(vm_page_t m, int base, int size) 1985 { 1986 1987 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1988 m->dirty &= ~vm_page_bits(base, size); 1989 } 1990 1991 /* 1992 * vm_page_set_invalid: 1993 * 1994 * Invalidates DEV_BSIZE'd chunks within a page. Both the 1995 * valid and dirty bits for the effected areas are cleared. 1996 * 1997 * May not block. 1998 */ 1999 void 2000 vm_page_set_invalid(vm_page_t m, int base, int size) 2001 { 2002 int bits; 2003 2004 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 2005 bits = vm_page_bits(base, size); 2006 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 2007 if (m->valid == VM_PAGE_BITS_ALL && bits != 0) 2008 pmap_remove_all(m); 2009 m->valid &= ~bits; 2010 m->dirty &= ~bits; 2011 m->object->generation++; 2012 } 2013 2014 /* 2015 * vm_page_zero_invalid() 2016 * 2017 * The kernel assumes that the invalid portions of a page contain 2018 * garbage, but such pages can be mapped into memory by user code. 2019 * When this occurs, we must zero out the non-valid portions of the 2020 * page so user code sees what it expects. 2021 * 2022 * Pages are most often semi-valid when the end of a file is mapped 2023 * into memory and the file's size is not page aligned. 2024 */ 2025 void 2026 vm_page_zero_invalid(vm_page_t m, boolean_t setvalid) 2027 { 2028 int b; 2029 int i; 2030 2031 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 2032 /* 2033 * Scan the valid bits looking for invalid sections that 2034 * must be zerod. Invalid sub-DEV_BSIZE'd areas ( where the 2035 * valid bit may be set ) have already been zerod by 2036 * vm_page_set_validclean(). 2037 */ 2038 for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) { 2039 if (i == (PAGE_SIZE / DEV_BSIZE) || 2040 (m->valid & (1 << i)) 2041 ) { 2042 if (i > b) { 2043 pmap_zero_page_area(m, 2044 b << DEV_BSHIFT, (i - b) << DEV_BSHIFT); 2045 } 2046 b = i + 1; 2047 } 2048 } 2049 2050 /* 2051 * setvalid is TRUE when we can safely set the zero'd areas 2052 * as being valid. We can do this if there are no cache consistancy 2053 * issues. e.g. it is ok to do with UFS, but not ok to do with NFS. 2054 */ 2055 if (setvalid) 2056 m->valid = VM_PAGE_BITS_ALL; 2057 } 2058 2059 /* 2060 * vm_page_is_valid: 2061 * 2062 * Is (partial) page valid? Note that the case where size == 0 2063 * will return FALSE in the degenerate case where the page is 2064 * entirely invalid, and TRUE otherwise. 2065 * 2066 * May not block. 2067 */ 2068 int 2069 vm_page_is_valid(vm_page_t m, int base, int size) 2070 { 2071 int bits = vm_page_bits(base, size); 2072 2073 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 2074 if (m->valid && ((m->valid & bits) == bits)) 2075 return 1; 2076 else 2077 return 0; 2078 } 2079 2080 /* 2081 * update dirty bits from pmap/mmu. May not block. 2082 */ 2083 void 2084 vm_page_test_dirty(vm_page_t m) 2085 { 2086 if ((m->dirty != VM_PAGE_BITS_ALL) && pmap_is_modified(m)) { 2087 vm_page_dirty(m); 2088 } 2089 } 2090 2091 int so_zerocp_fullpage = 0; 2092 2093 /* 2094 * Replace the given page with a copy. The copied page assumes 2095 * the portion of the given page's "wire_count" that is not the 2096 * responsibility of this copy-on-write mechanism. 2097 * 2098 * The object containing the given page must have a non-zero 2099 * paging-in-progress count and be locked. 2100 */ 2101 void 2102 vm_page_cowfault(vm_page_t m) 2103 { 2104 vm_page_t mnew; 2105 vm_object_t object; 2106 vm_pindex_t pindex; 2107 2108 object = m->object; 2109 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 2110 KASSERT(object->paging_in_progress != 0, 2111 ("vm_page_cowfault: object %p's paging-in-progress count is zero.", 2112 object)); 2113 pindex = m->pindex; 2114 2115 retry_alloc: 2116 pmap_remove_all(m); 2117 vm_page_remove(m); 2118 mnew = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL | VM_ALLOC_NOBUSY); 2119 if (mnew == NULL) { 2120 vm_page_insert(m, object, pindex); 2121 vm_page_unlock_queues(); 2122 VM_OBJECT_UNLOCK(object); 2123 VM_WAIT; 2124 VM_OBJECT_LOCK(object); 2125 if (m == vm_page_lookup(object, pindex)) { 2126 vm_page_lock_queues(); 2127 goto retry_alloc; 2128 } else { 2129 /* 2130 * Page disappeared during the wait. 2131 */ 2132 vm_page_lock_queues(); 2133 return; 2134 } 2135 } 2136 2137 if (m->cow == 0) { 2138 /* 2139 * check to see if we raced with an xmit complete when 2140 * waiting to allocate a page. If so, put things back 2141 * the way they were 2142 */ 2143 vm_page_free(mnew); 2144 vm_page_insert(m, object, pindex); 2145 } else { /* clear COW & copy page */ 2146 if (!so_zerocp_fullpage) 2147 pmap_copy_page(m, mnew); 2148 mnew->valid = VM_PAGE_BITS_ALL; 2149 vm_page_dirty(mnew); 2150 mnew->wire_count = m->wire_count - m->cow; 2151 m->wire_count = m->cow; 2152 } 2153 } 2154 2155 void 2156 vm_page_cowclear(vm_page_t m) 2157 { 2158 2159 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 2160 if (m->cow) { 2161 m->cow--; 2162 /* 2163 * let vm_fault add back write permission lazily 2164 */ 2165 } 2166 /* 2167 * sf_buf_free() will free the page, so we needn't do it here 2168 */ 2169 } 2170 2171 int 2172 vm_page_cowsetup(vm_page_t m) 2173 { 2174 2175 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 2176 if (m->cow == USHRT_MAX - 1) 2177 return (EBUSY); 2178 m->cow++; 2179 pmap_remove_write(m); 2180 return (0); 2181 } 2182 2183 #include "opt_ddb.h" 2184 #ifdef DDB 2185 #include <sys/kernel.h> 2186 2187 #include <ddb/ddb.h> 2188 2189 DB_SHOW_COMMAND(page, vm_page_print_page_info) 2190 { 2191 db_printf("cnt.v_free_count: %d\n", cnt.v_free_count); 2192 db_printf("cnt.v_cache_count: %d\n", cnt.v_cache_count); 2193 db_printf("cnt.v_inactive_count: %d\n", cnt.v_inactive_count); 2194 db_printf("cnt.v_active_count: %d\n", cnt.v_active_count); 2195 db_printf("cnt.v_wire_count: %d\n", cnt.v_wire_count); 2196 db_printf("cnt.v_free_reserved: %d\n", cnt.v_free_reserved); 2197 db_printf("cnt.v_free_min: %d\n", cnt.v_free_min); 2198 db_printf("cnt.v_free_target: %d\n", cnt.v_free_target); 2199 db_printf("cnt.v_cache_min: %d\n", cnt.v_cache_min); 2200 db_printf("cnt.v_inactive_target: %d\n", cnt.v_inactive_target); 2201 } 2202 2203 DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info) 2204 { 2205 2206 db_printf("PQ_FREE:"); 2207 db_printf(" %d", cnt.v_free_count); 2208 db_printf("\n"); 2209 2210 db_printf("PQ_CACHE:"); 2211 db_printf(" %d", cnt.v_cache_count); 2212 db_printf("\n"); 2213 2214 db_printf("PQ_ACTIVE: %d, PQ_INACTIVE: %d\n", 2215 *vm_page_queues[PQ_ACTIVE].cnt, 2216 *vm_page_queues[PQ_INACTIVE].cnt); 2217 } 2218 #endif /* DDB */ 2219