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 page queue lock is required when adding or removing a page from a 67 * page queue regardless of other locks or the busy state of a page. 68 * 69 * * In general, no thread besides the page daemon can acquire or 70 * hold more than one page queue lock at a time. 71 * 72 * * The page daemon can acquire and hold any pair of page queue 73 * locks in any order. 74 * 75 * - The object lock is required when inserting or removing 76 * pages from an object (vm_page_insert() or vm_page_remove()). 77 * 78 */ 79 80 /* 81 * Resident memory management module. 82 */ 83 84 #include <sys/cdefs.h> 85 __FBSDID("$FreeBSD$"); 86 87 #include "opt_vm.h" 88 89 #include <sys/param.h> 90 #include <sys/systm.h> 91 #include <sys/lock.h> 92 #include <sys/kernel.h> 93 #include <sys/limits.h> 94 #include <sys/linker.h> 95 #include <sys/malloc.h> 96 #include <sys/mman.h> 97 #include <sys/msgbuf.h> 98 #include <sys/mutex.h> 99 #include <sys/proc.h> 100 #include <sys/rwlock.h> 101 #include <sys/sbuf.h> 102 #include <sys/smp.h> 103 #include <sys/sysctl.h> 104 #include <sys/vmmeter.h> 105 #include <sys/vnode.h> 106 107 #include <vm/vm.h> 108 #include <vm/pmap.h> 109 #include <vm/vm_param.h> 110 #include <vm/vm_kern.h> 111 #include <vm/vm_object.h> 112 #include <vm/vm_page.h> 113 #include <vm/vm_pageout.h> 114 #include <vm/vm_pager.h> 115 #include <vm/vm_phys.h> 116 #include <vm/vm_radix.h> 117 #include <vm/vm_reserv.h> 118 #include <vm/vm_extern.h> 119 #include <vm/uma.h> 120 #include <vm/uma_int.h> 121 122 #include <machine/md_var.h> 123 124 /* 125 * Associated with page of user-allocatable memory is a 126 * page structure. 127 */ 128 129 struct vm_domain vm_dom[MAXMEMDOM]; 130 struct mtx_padalign vm_page_queue_free_mtx; 131 132 struct mtx_padalign pa_lock[PA_LOCK_COUNT]; 133 134 vm_page_t vm_page_array; 135 long vm_page_array_size; 136 long first_page; 137 int vm_page_zero_count; 138 139 static int boot_pages = UMA_BOOT_PAGES; 140 SYSCTL_INT(_vm, OID_AUTO, boot_pages, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, 141 &boot_pages, 0, 142 "number of pages allocated for bootstrapping the VM system"); 143 144 static int pa_tryrelock_restart; 145 SYSCTL_INT(_vm, OID_AUTO, tryrelock_restart, CTLFLAG_RD, 146 &pa_tryrelock_restart, 0, "Number of tryrelock restarts"); 147 148 static TAILQ_HEAD(, vm_page) blacklist_head; 149 static int sysctl_vm_page_blacklist(SYSCTL_HANDLER_ARGS); 150 SYSCTL_PROC(_vm, OID_AUTO, page_blacklist, CTLTYPE_STRING | CTLFLAG_RD | 151 CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_page_blacklist, "A", "Blacklist pages"); 152 153 /* Is the page daemon waiting for free pages? */ 154 static int vm_pageout_pages_needed; 155 156 static uma_zone_t fakepg_zone; 157 158 static struct vnode *vm_page_alloc_init(vm_page_t m); 159 static void vm_page_cache_turn_free(vm_page_t m); 160 static void vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits); 161 static void vm_page_enqueue(uint8_t queue, vm_page_t m); 162 static void vm_page_free_wakeup(void); 163 static void vm_page_init_fakepg(void *dummy); 164 static int vm_page_insert_after(vm_page_t m, vm_object_t object, 165 vm_pindex_t pindex, vm_page_t mpred); 166 static void vm_page_insert_radixdone(vm_page_t m, vm_object_t object, 167 vm_page_t mpred); 168 static int vm_page_reclaim_run(int req_class, u_long npages, vm_page_t m_run, 169 vm_paddr_t high); 170 171 SYSINIT(vm_page, SI_SUB_VM, SI_ORDER_SECOND, vm_page_init_fakepg, NULL); 172 173 static void 174 vm_page_init_fakepg(void *dummy) 175 { 176 177 fakepg_zone = uma_zcreate("fakepg", sizeof(struct vm_page), NULL, NULL, 178 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE | UMA_ZONE_VM); 179 } 180 181 /* Make sure that u_long is at least 64 bits when PAGE_SIZE is 32K. */ 182 #if PAGE_SIZE == 32768 183 #ifdef CTASSERT 184 CTASSERT(sizeof(u_long) >= 8); 185 #endif 186 #endif 187 188 /* 189 * Try to acquire a physical address lock while a pmap is locked. If we 190 * fail to trylock we unlock and lock the pmap directly and cache the 191 * locked pa in *locked. The caller should then restart their loop in case 192 * the virtual to physical mapping has changed. 193 */ 194 int 195 vm_page_pa_tryrelock(pmap_t pmap, vm_paddr_t pa, vm_paddr_t *locked) 196 { 197 vm_paddr_t lockpa; 198 199 lockpa = *locked; 200 *locked = pa; 201 if (lockpa) { 202 PA_LOCK_ASSERT(lockpa, MA_OWNED); 203 if (PA_LOCKPTR(pa) == PA_LOCKPTR(lockpa)) 204 return (0); 205 PA_UNLOCK(lockpa); 206 } 207 if (PA_TRYLOCK(pa)) 208 return (0); 209 PMAP_UNLOCK(pmap); 210 atomic_add_int(&pa_tryrelock_restart, 1); 211 PA_LOCK(pa); 212 PMAP_LOCK(pmap); 213 return (EAGAIN); 214 } 215 216 /* 217 * vm_set_page_size: 218 * 219 * Sets the page size, perhaps based upon the memory 220 * size. Must be called before any use of page-size 221 * dependent functions. 222 */ 223 void 224 vm_set_page_size(void) 225 { 226 if (vm_cnt.v_page_size == 0) 227 vm_cnt.v_page_size = PAGE_SIZE; 228 if (((vm_cnt.v_page_size - 1) & vm_cnt.v_page_size) != 0) 229 panic("vm_set_page_size: page size not a power of two"); 230 } 231 232 /* 233 * vm_page_blacklist_next: 234 * 235 * Find the next entry in the provided string of blacklist 236 * addresses. Entries are separated by space, comma, or newline. 237 * If an invalid integer is encountered then the rest of the 238 * string is skipped. Updates the list pointer to the next 239 * character, or NULL if the string is exhausted or invalid. 240 */ 241 static vm_paddr_t 242 vm_page_blacklist_next(char **list, char *end) 243 { 244 vm_paddr_t bad; 245 char *cp, *pos; 246 247 if (list == NULL || *list == NULL) 248 return (0); 249 if (**list =='\0') { 250 *list = NULL; 251 return (0); 252 } 253 254 /* 255 * If there's no end pointer then the buffer is coming from 256 * the kenv and we know it's null-terminated. 257 */ 258 if (end == NULL) 259 end = *list + strlen(*list); 260 261 /* Ensure that strtoq() won't walk off the end */ 262 if (*end != '\0') { 263 if (*end == '\n' || *end == ' ' || *end == ',') 264 *end = '\0'; 265 else { 266 printf("Blacklist not terminated, skipping\n"); 267 *list = NULL; 268 return (0); 269 } 270 } 271 272 for (pos = *list; *pos != '\0'; pos = cp) { 273 bad = strtoq(pos, &cp, 0); 274 if (*cp == '\0' || *cp == ' ' || *cp == ',' || *cp == '\n') { 275 if (bad == 0) { 276 if (++cp < end) 277 continue; 278 else 279 break; 280 } 281 } else 282 break; 283 if (*cp == '\0' || ++cp >= end) 284 *list = NULL; 285 else 286 *list = cp; 287 return (trunc_page(bad)); 288 } 289 printf("Garbage in RAM blacklist, skipping\n"); 290 *list = NULL; 291 return (0); 292 } 293 294 /* 295 * vm_page_blacklist_check: 296 * 297 * Iterate through the provided string of blacklist addresses, pulling 298 * each entry out of the physical allocator free list and putting it 299 * onto a list for reporting via the vm.page_blacklist sysctl. 300 */ 301 static void 302 vm_page_blacklist_check(char *list, char *end) 303 { 304 vm_paddr_t pa; 305 vm_page_t m; 306 char *next; 307 int ret; 308 309 next = list; 310 while (next != NULL) { 311 if ((pa = vm_page_blacklist_next(&next, end)) == 0) 312 continue; 313 m = vm_phys_paddr_to_vm_page(pa); 314 if (m == NULL) 315 continue; 316 mtx_lock(&vm_page_queue_free_mtx); 317 ret = vm_phys_unfree_page(m); 318 mtx_unlock(&vm_page_queue_free_mtx); 319 if (ret == TRUE) { 320 TAILQ_INSERT_TAIL(&blacklist_head, m, listq); 321 if (bootverbose) 322 printf("Skipping page with pa 0x%jx\n", 323 (uintmax_t)pa); 324 } 325 } 326 } 327 328 /* 329 * vm_page_blacklist_load: 330 * 331 * Search for a special module named "ram_blacklist". It'll be a 332 * plain text file provided by the user via the loader directive 333 * of the same name. 334 */ 335 static void 336 vm_page_blacklist_load(char **list, char **end) 337 { 338 void *mod; 339 u_char *ptr; 340 u_int len; 341 342 mod = NULL; 343 ptr = NULL; 344 345 mod = preload_search_by_type("ram_blacklist"); 346 if (mod != NULL) { 347 ptr = preload_fetch_addr(mod); 348 len = preload_fetch_size(mod); 349 } 350 *list = ptr; 351 if (ptr != NULL) 352 *end = ptr + len; 353 else 354 *end = NULL; 355 return; 356 } 357 358 static int 359 sysctl_vm_page_blacklist(SYSCTL_HANDLER_ARGS) 360 { 361 vm_page_t m; 362 struct sbuf sbuf; 363 int error, first; 364 365 first = 1; 366 error = sysctl_wire_old_buffer(req, 0); 367 if (error != 0) 368 return (error); 369 sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 370 TAILQ_FOREACH(m, &blacklist_head, listq) { 371 sbuf_printf(&sbuf, "%s%#jx", first ? "" : ",", 372 (uintmax_t)m->phys_addr); 373 first = 0; 374 } 375 error = sbuf_finish(&sbuf); 376 sbuf_delete(&sbuf); 377 return (error); 378 } 379 380 static void 381 vm_page_domain_init(struct vm_domain *vmd) 382 { 383 struct vm_pagequeue *pq; 384 int i; 385 386 *__DECONST(char **, &vmd->vmd_pagequeues[PQ_INACTIVE].pq_name) = 387 "vm inactive pagequeue"; 388 *__DECONST(u_int **, &vmd->vmd_pagequeues[PQ_INACTIVE].pq_vcnt) = 389 &vm_cnt.v_inactive_count; 390 *__DECONST(char **, &vmd->vmd_pagequeues[PQ_ACTIVE].pq_name) = 391 "vm active pagequeue"; 392 *__DECONST(u_int **, &vmd->vmd_pagequeues[PQ_ACTIVE].pq_vcnt) = 393 &vm_cnt.v_active_count; 394 vmd->vmd_page_count = 0; 395 vmd->vmd_free_count = 0; 396 vmd->vmd_segs = 0; 397 vmd->vmd_oom = FALSE; 398 vmd->vmd_pass = 0; 399 for (i = 0; i < PQ_COUNT; i++) { 400 pq = &vmd->vmd_pagequeues[i]; 401 TAILQ_INIT(&pq->pq_pl); 402 mtx_init(&pq->pq_mutex, pq->pq_name, "vm pagequeue", 403 MTX_DEF | MTX_DUPOK); 404 } 405 } 406 407 /* 408 * vm_page_startup: 409 * 410 * Initializes the resident memory module. 411 * 412 * Allocates memory for the page cells, and 413 * for the object/offset-to-page hash table headers. 414 * Each page cell is initialized and placed on the free list. 415 */ 416 vm_offset_t 417 vm_page_startup(vm_offset_t vaddr) 418 { 419 vm_offset_t mapped; 420 vm_paddr_t page_range; 421 vm_paddr_t new_end; 422 int i; 423 vm_paddr_t pa; 424 vm_paddr_t last_pa; 425 char *list, *listend; 426 vm_paddr_t end; 427 vm_paddr_t biggestsize; 428 vm_paddr_t low_water, high_water; 429 int biggestone; 430 int pages_per_zone; 431 432 biggestsize = 0; 433 biggestone = 0; 434 vaddr = round_page(vaddr); 435 436 for (i = 0; phys_avail[i + 1]; i += 2) { 437 phys_avail[i] = round_page(phys_avail[i]); 438 phys_avail[i + 1] = trunc_page(phys_avail[i + 1]); 439 } 440 441 low_water = phys_avail[0]; 442 high_water = phys_avail[1]; 443 444 for (i = 0; i < vm_phys_nsegs; i++) { 445 if (vm_phys_segs[i].start < low_water) 446 low_water = vm_phys_segs[i].start; 447 if (vm_phys_segs[i].end > high_water) 448 high_water = vm_phys_segs[i].end; 449 } 450 for (i = 0; phys_avail[i + 1]; i += 2) { 451 vm_paddr_t size = phys_avail[i + 1] - phys_avail[i]; 452 453 if (size > biggestsize) { 454 biggestone = i; 455 biggestsize = size; 456 } 457 if (phys_avail[i] < low_water) 458 low_water = phys_avail[i]; 459 if (phys_avail[i + 1] > high_water) 460 high_water = phys_avail[i + 1]; 461 } 462 463 end = phys_avail[biggestone+1]; 464 465 /* 466 * Initialize the page and queue locks. 467 */ 468 mtx_init(&vm_page_queue_free_mtx, "vm page free queue", NULL, MTX_DEF); 469 for (i = 0; i < PA_LOCK_COUNT; i++) 470 mtx_init(&pa_lock[i], "vm page", NULL, MTX_DEF); 471 for (i = 0; i < vm_ndomains; i++) 472 vm_page_domain_init(&vm_dom[i]); 473 474 /* 475 * Almost all of the pages needed for boot strapping UMA are used 476 * for zone structures, so if the number of CPUs results in those 477 * structures taking more than one page each, we set aside more pages 478 * in proportion to the zone structure size. 479 */ 480 pages_per_zone = howmany(sizeof(struct uma_zone) + 481 sizeof(struct uma_cache) * (mp_maxid + 1), UMA_SLAB_SIZE); 482 if (pages_per_zone > 1) { 483 /* Reserve more pages so that we don't run out. */ 484 boot_pages = UMA_BOOT_PAGES_ZONES * pages_per_zone; 485 } 486 487 /* 488 * Allocate memory for use when boot strapping the kernel memory 489 * allocator. 490 * 491 * CTFLAG_RDTUN doesn't work during the early boot process, so we must 492 * manually fetch the value. 493 */ 494 TUNABLE_INT_FETCH("vm.boot_pages", &boot_pages); 495 new_end = end - (boot_pages * UMA_SLAB_SIZE); 496 new_end = trunc_page(new_end); 497 mapped = pmap_map(&vaddr, new_end, end, 498 VM_PROT_READ | VM_PROT_WRITE); 499 bzero((void *)mapped, end - new_end); 500 uma_startup((void *)mapped, boot_pages); 501 502 #if defined(__aarch64__) || defined(__amd64__) || defined(__arm__) || \ 503 defined(__i386__) || defined(__mips__) 504 /* 505 * Allocate a bitmap to indicate that a random physical page 506 * needs to be included in a minidump. 507 * 508 * The amd64 port needs this to indicate which direct map pages 509 * need to be dumped, via calls to dump_add_page()/dump_drop_page(). 510 * 511 * However, i386 still needs this workspace internally within the 512 * minidump code. In theory, they are not needed on i386, but are 513 * included should the sf_buf code decide to use them. 514 */ 515 last_pa = 0; 516 for (i = 0; dump_avail[i + 1] != 0; i += 2) 517 if (dump_avail[i + 1] > last_pa) 518 last_pa = dump_avail[i + 1]; 519 page_range = last_pa / PAGE_SIZE; 520 vm_page_dump_size = round_page(roundup2(page_range, NBBY) / NBBY); 521 new_end -= vm_page_dump_size; 522 vm_page_dump = (void *)(uintptr_t)pmap_map(&vaddr, new_end, 523 new_end + vm_page_dump_size, VM_PROT_READ | VM_PROT_WRITE); 524 bzero((void *)vm_page_dump, vm_page_dump_size); 525 #endif 526 #ifdef __amd64__ 527 /* 528 * Request that the physical pages underlying the message buffer be 529 * included in a crash dump. Since the message buffer is accessed 530 * through the direct map, they are not automatically included. 531 */ 532 pa = DMAP_TO_PHYS((vm_offset_t)msgbufp->msg_ptr); 533 last_pa = pa + round_page(msgbufsize); 534 while (pa < last_pa) { 535 dump_add_page(pa); 536 pa += PAGE_SIZE; 537 } 538 #endif 539 /* 540 * Compute the number of pages of memory that will be available for 541 * use (taking into account the overhead of a page structure per 542 * page). 543 */ 544 first_page = low_water / PAGE_SIZE; 545 #ifdef VM_PHYSSEG_SPARSE 546 page_range = 0; 547 for (i = 0; i < vm_phys_nsegs; i++) { 548 page_range += atop(vm_phys_segs[i].end - 549 vm_phys_segs[i].start); 550 } 551 for (i = 0; phys_avail[i + 1] != 0; i += 2) 552 page_range += atop(phys_avail[i + 1] - phys_avail[i]); 553 #elif defined(VM_PHYSSEG_DENSE) 554 page_range = high_water / PAGE_SIZE - first_page; 555 #else 556 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined." 557 #endif 558 end = new_end; 559 560 /* 561 * Reserve an unmapped guard page to trap access to vm_page_array[-1]. 562 */ 563 vaddr += PAGE_SIZE; 564 565 /* 566 * Initialize the mem entry structures now, and put them in the free 567 * queue. 568 */ 569 new_end = trunc_page(end - page_range * sizeof(struct vm_page)); 570 mapped = pmap_map(&vaddr, new_end, end, 571 VM_PROT_READ | VM_PROT_WRITE); 572 vm_page_array = (vm_page_t) mapped; 573 #if VM_NRESERVLEVEL > 0 574 /* 575 * Allocate memory for the reservation management system's data 576 * structures. 577 */ 578 new_end = vm_reserv_startup(&vaddr, new_end, high_water); 579 #endif 580 #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__) 581 /* 582 * pmap_map on arm64, amd64, and mips can come out of the direct-map, 583 * not kvm like i386, so the pages must be tracked for a crashdump to 584 * include this data. This includes the vm_page_array and the early 585 * UMA bootstrap pages. 586 */ 587 for (pa = new_end; pa < phys_avail[biggestone + 1]; pa += PAGE_SIZE) 588 dump_add_page(pa); 589 #endif 590 phys_avail[biggestone + 1] = new_end; 591 592 /* 593 * Add physical memory segments corresponding to the available 594 * physical pages. 595 */ 596 for (i = 0; phys_avail[i + 1] != 0; i += 2) 597 vm_phys_add_seg(phys_avail[i], phys_avail[i + 1]); 598 599 /* 600 * Clear all of the page structures 601 */ 602 bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page)); 603 for (i = 0; i < page_range; i++) 604 vm_page_array[i].order = VM_NFREEORDER; 605 vm_page_array_size = page_range; 606 607 /* 608 * Initialize the physical memory allocator. 609 */ 610 vm_phys_init(); 611 612 /* 613 * Add every available physical page that is not blacklisted to 614 * the free lists. 615 */ 616 vm_cnt.v_page_count = 0; 617 vm_cnt.v_free_count = 0; 618 for (i = 0; phys_avail[i + 1] != 0; i += 2) { 619 pa = phys_avail[i]; 620 last_pa = phys_avail[i + 1]; 621 while (pa < last_pa) { 622 vm_phys_add_page(pa); 623 pa += PAGE_SIZE; 624 } 625 } 626 627 TAILQ_INIT(&blacklist_head); 628 vm_page_blacklist_load(&list, &listend); 629 vm_page_blacklist_check(list, listend); 630 631 list = kern_getenv("vm.blacklist"); 632 vm_page_blacklist_check(list, NULL); 633 634 freeenv(list); 635 #if VM_NRESERVLEVEL > 0 636 /* 637 * Initialize the reservation management system. 638 */ 639 vm_reserv_init(); 640 #endif 641 return (vaddr); 642 } 643 644 void 645 vm_page_reference(vm_page_t m) 646 { 647 648 vm_page_aflag_set(m, PGA_REFERENCED); 649 } 650 651 /* 652 * vm_page_busy_downgrade: 653 * 654 * Downgrade an exclusive busy page into a single shared busy page. 655 */ 656 void 657 vm_page_busy_downgrade(vm_page_t m) 658 { 659 u_int x; 660 661 vm_page_assert_xbusied(m); 662 663 for (;;) { 664 x = m->busy_lock; 665 x &= VPB_BIT_WAITERS; 666 if (atomic_cmpset_rel_int(&m->busy_lock, 667 VPB_SINGLE_EXCLUSIVER | x, VPB_SHARERS_WORD(1) | x)) 668 break; 669 } 670 } 671 672 /* 673 * vm_page_sbusied: 674 * 675 * Return a positive value if the page is shared busied, 0 otherwise. 676 */ 677 int 678 vm_page_sbusied(vm_page_t m) 679 { 680 u_int x; 681 682 x = m->busy_lock; 683 return ((x & VPB_BIT_SHARED) != 0 && x != VPB_UNBUSIED); 684 } 685 686 /* 687 * vm_page_sunbusy: 688 * 689 * Shared unbusy a page. 690 */ 691 void 692 vm_page_sunbusy(vm_page_t m) 693 { 694 u_int x; 695 696 vm_page_assert_sbusied(m); 697 698 for (;;) { 699 x = m->busy_lock; 700 if (VPB_SHARERS(x) > 1) { 701 if (atomic_cmpset_int(&m->busy_lock, x, 702 x - VPB_ONE_SHARER)) 703 break; 704 continue; 705 } 706 if ((x & VPB_BIT_WAITERS) == 0) { 707 KASSERT(x == VPB_SHARERS_WORD(1), 708 ("vm_page_sunbusy: invalid lock state")); 709 if (atomic_cmpset_int(&m->busy_lock, 710 VPB_SHARERS_WORD(1), VPB_UNBUSIED)) 711 break; 712 continue; 713 } 714 KASSERT(x == (VPB_SHARERS_WORD(1) | VPB_BIT_WAITERS), 715 ("vm_page_sunbusy: invalid lock state for waiters")); 716 717 vm_page_lock(m); 718 if (!atomic_cmpset_int(&m->busy_lock, x, VPB_UNBUSIED)) { 719 vm_page_unlock(m); 720 continue; 721 } 722 wakeup(m); 723 vm_page_unlock(m); 724 break; 725 } 726 } 727 728 /* 729 * vm_page_busy_sleep: 730 * 731 * Sleep and release the page lock, using the page pointer as wchan. 732 * This is used to implement the hard-path of busying mechanism. 733 * 734 * The given page must be locked. 735 */ 736 void 737 vm_page_busy_sleep(vm_page_t m, const char *wmesg) 738 { 739 u_int x; 740 741 vm_page_lock_assert(m, MA_OWNED); 742 743 x = m->busy_lock; 744 if (x == VPB_UNBUSIED) { 745 vm_page_unlock(m); 746 return; 747 } 748 if ((x & VPB_BIT_WAITERS) == 0 && 749 !atomic_cmpset_int(&m->busy_lock, x, x | VPB_BIT_WAITERS)) { 750 vm_page_unlock(m); 751 return; 752 } 753 msleep(m, vm_page_lockptr(m), PVM | PDROP, wmesg, 0); 754 } 755 756 /* 757 * vm_page_trysbusy: 758 * 759 * Try to shared busy a page. 760 * If the operation succeeds 1 is returned otherwise 0. 761 * The operation never sleeps. 762 */ 763 int 764 vm_page_trysbusy(vm_page_t m) 765 { 766 u_int x; 767 768 for (;;) { 769 x = m->busy_lock; 770 if ((x & VPB_BIT_SHARED) == 0) 771 return (0); 772 if (atomic_cmpset_acq_int(&m->busy_lock, x, x + VPB_ONE_SHARER)) 773 return (1); 774 } 775 } 776 777 static void 778 vm_page_xunbusy_locked(vm_page_t m) 779 { 780 781 vm_page_assert_xbusied(m); 782 vm_page_assert_locked(m); 783 784 atomic_store_rel_int(&m->busy_lock, VPB_UNBUSIED); 785 /* There is a waiter, do wakeup() instead of vm_page_flash(). */ 786 wakeup(m); 787 } 788 789 static void 790 vm_page_xunbusy_maybelocked(vm_page_t m) 791 { 792 bool lockacq; 793 794 vm_page_assert_xbusied(m); 795 796 /* 797 * Fast path for unbusy. If it succeeds, we know that there 798 * are no waiters, so we do not need a wakeup. 799 */ 800 if (atomic_cmpset_rel_int(&m->busy_lock, VPB_SINGLE_EXCLUSIVER, 801 VPB_UNBUSIED)) 802 return; 803 804 lockacq = !mtx_owned(vm_page_lockptr(m)); 805 if (lockacq) 806 vm_page_lock(m); 807 vm_page_xunbusy_locked(m); 808 if (lockacq) 809 vm_page_unlock(m); 810 } 811 812 /* 813 * vm_page_xunbusy_hard: 814 * 815 * Called after the first try the exclusive unbusy of a page failed. 816 * It is assumed that the waiters bit is on. 817 */ 818 void 819 vm_page_xunbusy_hard(vm_page_t m) 820 { 821 822 vm_page_assert_xbusied(m); 823 824 vm_page_lock(m); 825 vm_page_xunbusy_locked(m); 826 vm_page_unlock(m); 827 } 828 829 /* 830 * vm_page_flash: 831 * 832 * Wakeup anyone waiting for the page. 833 * The ownership bits do not change. 834 * 835 * The given page must be locked. 836 */ 837 void 838 vm_page_flash(vm_page_t m) 839 { 840 u_int x; 841 842 vm_page_lock_assert(m, MA_OWNED); 843 844 for (;;) { 845 x = m->busy_lock; 846 if ((x & VPB_BIT_WAITERS) == 0) 847 return; 848 if (atomic_cmpset_int(&m->busy_lock, x, 849 x & (~VPB_BIT_WAITERS))) 850 break; 851 } 852 wakeup(m); 853 } 854 855 /* 856 * Keep page from being freed by the page daemon 857 * much of the same effect as wiring, except much lower 858 * overhead and should be used only for *very* temporary 859 * holding ("wiring"). 860 */ 861 void 862 vm_page_hold(vm_page_t mem) 863 { 864 865 vm_page_lock_assert(mem, MA_OWNED); 866 mem->hold_count++; 867 } 868 869 void 870 vm_page_unhold(vm_page_t mem) 871 { 872 873 vm_page_lock_assert(mem, MA_OWNED); 874 KASSERT(mem->hold_count >= 1, ("vm_page_unhold: hold count < 0!!!")); 875 --mem->hold_count; 876 if (mem->hold_count == 0 && (mem->flags & PG_UNHOLDFREE) != 0) 877 vm_page_free_toq(mem); 878 } 879 880 /* 881 * vm_page_unhold_pages: 882 * 883 * Unhold each of the pages that is referenced by the given array. 884 */ 885 void 886 vm_page_unhold_pages(vm_page_t *ma, int count) 887 { 888 struct mtx *mtx, *new_mtx; 889 890 mtx = NULL; 891 for (; count != 0; count--) { 892 /* 893 * Avoid releasing and reacquiring the same page lock. 894 */ 895 new_mtx = vm_page_lockptr(*ma); 896 if (mtx != new_mtx) { 897 if (mtx != NULL) 898 mtx_unlock(mtx); 899 mtx = new_mtx; 900 mtx_lock(mtx); 901 } 902 vm_page_unhold(*ma); 903 ma++; 904 } 905 if (mtx != NULL) 906 mtx_unlock(mtx); 907 } 908 909 vm_page_t 910 PHYS_TO_VM_PAGE(vm_paddr_t pa) 911 { 912 vm_page_t m; 913 914 #ifdef VM_PHYSSEG_SPARSE 915 m = vm_phys_paddr_to_vm_page(pa); 916 if (m == NULL) 917 m = vm_phys_fictitious_to_vm_page(pa); 918 return (m); 919 #elif defined(VM_PHYSSEG_DENSE) 920 long pi; 921 922 pi = atop(pa); 923 if (pi >= first_page && (pi - first_page) < vm_page_array_size) { 924 m = &vm_page_array[pi - first_page]; 925 return (m); 926 } 927 return (vm_phys_fictitious_to_vm_page(pa)); 928 #else 929 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined." 930 #endif 931 } 932 933 /* 934 * vm_page_getfake: 935 * 936 * Create a fictitious page with the specified physical address and 937 * memory attribute. The memory attribute is the only the machine- 938 * dependent aspect of a fictitious page that must be initialized. 939 */ 940 vm_page_t 941 vm_page_getfake(vm_paddr_t paddr, vm_memattr_t memattr) 942 { 943 vm_page_t m; 944 945 m = uma_zalloc(fakepg_zone, M_WAITOK | M_ZERO); 946 vm_page_initfake(m, paddr, memattr); 947 return (m); 948 } 949 950 void 951 vm_page_initfake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr) 952 { 953 954 if ((m->flags & PG_FICTITIOUS) != 0) { 955 /* 956 * The page's memattr might have changed since the 957 * previous initialization. Update the pmap to the 958 * new memattr. 959 */ 960 goto memattr; 961 } 962 m->phys_addr = paddr; 963 m->queue = PQ_NONE; 964 /* Fictitious pages don't use "segind". */ 965 m->flags = PG_FICTITIOUS; 966 /* Fictitious pages don't use "order" or "pool". */ 967 m->oflags = VPO_UNMANAGED; 968 m->busy_lock = VPB_SINGLE_EXCLUSIVER; 969 m->wire_count = 1; 970 pmap_page_init(m); 971 memattr: 972 pmap_page_set_memattr(m, memattr); 973 } 974 975 /* 976 * vm_page_putfake: 977 * 978 * Release a fictitious page. 979 */ 980 void 981 vm_page_putfake(vm_page_t m) 982 { 983 984 KASSERT((m->oflags & VPO_UNMANAGED) != 0, ("managed %p", m)); 985 KASSERT((m->flags & PG_FICTITIOUS) != 0, 986 ("vm_page_putfake: bad page %p", m)); 987 uma_zfree(fakepg_zone, m); 988 } 989 990 /* 991 * vm_page_updatefake: 992 * 993 * Update the given fictitious page to the specified physical address and 994 * memory attribute. 995 */ 996 void 997 vm_page_updatefake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr) 998 { 999 1000 KASSERT((m->flags & PG_FICTITIOUS) != 0, 1001 ("vm_page_updatefake: bad page %p", m)); 1002 m->phys_addr = paddr; 1003 pmap_page_set_memattr(m, memattr); 1004 } 1005 1006 /* 1007 * vm_page_free: 1008 * 1009 * Free a page. 1010 */ 1011 void 1012 vm_page_free(vm_page_t m) 1013 { 1014 1015 m->flags &= ~PG_ZERO; 1016 vm_page_free_toq(m); 1017 } 1018 1019 /* 1020 * vm_page_free_zero: 1021 * 1022 * Free a page to the zerod-pages queue 1023 */ 1024 void 1025 vm_page_free_zero(vm_page_t m) 1026 { 1027 1028 m->flags |= PG_ZERO; 1029 vm_page_free_toq(m); 1030 } 1031 1032 /* 1033 * Unbusy and handle the page queueing for a page from the VOP_GETPAGES() 1034 * array which was optionally read ahead or behind. 1035 */ 1036 void 1037 vm_page_readahead_finish(vm_page_t m) 1038 { 1039 1040 /* We shouldn't put invalid pages on queues. */ 1041 KASSERT(m->valid != 0, ("%s: %p is invalid", __func__, m)); 1042 1043 /* 1044 * Since the page is not the actually needed one, whether it should 1045 * be activated or deactivated is not obvious. Empirical results 1046 * have shown that deactivating the page is usually the best choice, 1047 * unless the page is wanted by another thread. 1048 */ 1049 vm_page_lock(m); 1050 if ((m->busy_lock & VPB_BIT_WAITERS) != 0) 1051 vm_page_activate(m); 1052 else 1053 vm_page_deactivate(m); 1054 vm_page_unlock(m); 1055 vm_page_xunbusy(m); 1056 } 1057 1058 /* 1059 * vm_page_sleep_if_busy: 1060 * 1061 * Sleep and release the page queues lock if the page is busied. 1062 * Returns TRUE if the thread slept. 1063 * 1064 * The given page must be unlocked and object containing it must 1065 * be locked. 1066 */ 1067 int 1068 vm_page_sleep_if_busy(vm_page_t m, const char *msg) 1069 { 1070 vm_object_t obj; 1071 1072 vm_page_lock_assert(m, MA_NOTOWNED); 1073 VM_OBJECT_ASSERT_WLOCKED(m->object); 1074 1075 if (vm_page_busied(m)) { 1076 /* 1077 * The page-specific object must be cached because page 1078 * identity can change during the sleep, causing the 1079 * re-lock of a different object. 1080 * It is assumed that a reference to the object is already 1081 * held by the callers. 1082 */ 1083 obj = m->object; 1084 vm_page_lock(m); 1085 VM_OBJECT_WUNLOCK(obj); 1086 vm_page_busy_sleep(m, msg); 1087 VM_OBJECT_WLOCK(obj); 1088 return (TRUE); 1089 } 1090 return (FALSE); 1091 } 1092 1093 /* 1094 * vm_page_dirty_KBI: [ internal use only ] 1095 * 1096 * Set all bits in the page's dirty field. 1097 * 1098 * The object containing the specified page must be locked if the 1099 * call is made from the machine-independent layer. 1100 * 1101 * See vm_page_clear_dirty_mask(). 1102 * 1103 * This function should only be called by vm_page_dirty(). 1104 */ 1105 void 1106 vm_page_dirty_KBI(vm_page_t m) 1107 { 1108 1109 /* These assertions refer to this operation by its public name. */ 1110 KASSERT((m->flags & PG_CACHED) == 0, 1111 ("vm_page_dirty: page in cache!")); 1112 KASSERT(m->valid == VM_PAGE_BITS_ALL, 1113 ("vm_page_dirty: page is invalid!")); 1114 m->dirty = VM_PAGE_BITS_ALL; 1115 } 1116 1117 /* 1118 * vm_page_insert: [ internal use only ] 1119 * 1120 * Inserts the given mem entry into the object and object list. 1121 * 1122 * The object must be locked. 1123 */ 1124 int 1125 vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex) 1126 { 1127 vm_page_t mpred; 1128 1129 VM_OBJECT_ASSERT_WLOCKED(object); 1130 mpred = vm_radix_lookup_le(&object->rtree, pindex); 1131 return (vm_page_insert_after(m, object, pindex, mpred)); 1132 } 1133 1134 /* 1135 * vm_page_insert_after: 1136 * 1137 * Inserts the page "m" into the specified object at offset "pindex". 1138 * 1139 * The page "mpred" must immediately precede the offset "pindex" within 1140 * the specified object. 1141 * 1142 * The object must be locked. 1143 */ 1144 static int 1145 vm_page_insert_after(vm_page_t m, vm_object_t object, vm_pindex_t pindex, 1146 vm_page_t mpred) 1147 { 1148 vm_page_t msucc; 1149 1150 VM_OBJECT_ASSERT_WLOCKED(object); 1151 KASSERT(m->object == NULL, 1152 ("vm_page_insert_after: page already inserted")); 1153 if (mpred != NULL) { 1154 KASSERT(mpred->object == object, 1155 ("vm_page_insert_after: object doesn't contain mpred")); 1156 KASSERT(mpred->pindex < pindex, 1157 ("vm_page_insert_after: mpred doesn't precede pindex")); 1158 msucc = TAILQ_NEXT(mpred, listq); 1159 } else 1160 msucc = TAILQ_FIRST(&object->memq); 1161 if (msucc != NULL) 1162 KASSERT(msucc->pindex > pindex, 1163 ("vm_page_insert_after: msucc doesn't succeed pindex")); 1164 1165 /* 1166 * Record the object/offset pair in this page 1167 */ 1168 m->object = object; 1169 m->pindex = pindex; 1170 1171 /* 1172 * Now link into the object's ordered list of backed pages. 1173 */ 1174 if (vm_radix_insert(&object->rtree, m)) { 1175 m->object = NULL; 1176 m->pindex = 0; 1177 return (1); 1178 } 1179 vm_page_insert_radixdone(m, object, mpred); 1180 return (0); 1181 } 1182 1183 /* 1184 * vm_page_insert_radixdone: 1185 * 1186 * Complete page "m" insertion into the specified object after the 1187 * radix trie hooking. 1188 * 1189 * The page "mpred" must precede the offset "m->pindex" within the 1190 * specified object. 1191 * 1192 * The object must be locked. 1193 */ 1194 static void 1195 vm_page_insert_radixdone(vm_page_t m, vm_object_t object, vm_page_t mpred) 1196 { 1197 1198 VM_OBJECT_ASSERT_WLOCKED(object); 1199 KASSERT(object != NULL && m->object == object, 1200 ("vm_page_insert_radixdone: page %p has inconsistent object", m)); 1201 if (mpred != NULL) { 1202 KASSERT(mpred->object == object, 1203 ("vm_page_insert_after: object doesn't contain mpred")); 1204 KASSERT(mpred->pindex < m->pindex, 1205 ("vm_page_insert_after: mpred doesn't precede pindex")); 1206 } 1207 1208 if (mpred != NULL) 1209 TAILQ_INSERT_AFTER(&object->memq, mpred, m, listq); 1210 else 1211 TAILQ_INSERT_HEAD(&object->memq, m, listq); 1212 1213 /* 1214 * Show that the object has one more resident page. 1215 */ 1216 object->resident_page_count++; 1217 1218 /* 1219 * Hold the vnode until the last page is released. 1220 */ 1221 if (object->resident_page_count == 1 && object->type == OBJT_VNODE) 1222 vhold(object->handle); 1223 1224 /* 1225 * Since we are inserting a new and possibly dirty page, 1226 * update the object's OBJ_MIGHTBEDIRTY flag. 1227 */ 1228 if (pmap_page_is_write_mapped(m)) 1229 vm_object_set_writeable_dirty(object); 1230 } 1231 1232 /* 1233 * vm_page_remove: 1234 * 1235 * Removes the given mem entry from the object/offset-page 1236 * table and the object page list, but do not invalidate/terminate 1237 * the backing store. 1238 * 1239 * The object must be locked. The page must be locked if it is managed. 1240 */ 1241 void 1242 vm_page_remove(vm_page_t m) 1243 { 1244 vm_object_t object; 1245 1246 if ((m->oflags & VPO_UNMANAGED) == 0) 1247 vm_page_assert_locked(m); 1248 if ((object = m->object) == NULL) 1249 return; 1250 VM_OBJECT_ASSERT_WLOCKED(object); 1251 if (vm_page_xbusied(m)) 1252 vm_page_xunbusy_maybelocked(m); 1253 1254 /* 1255 * Now remove from the object's list of backed pages. 1256 */ 1257 vm_radix_remove(&object->rtree, m->pindex); 1258 TAILQ_REMOVE(&object->memq, m, listq); 1259 1260 /* 1261 * And show that the object has one fewer resident page. 1262 */ 1263 object->resident_page_count--; 1264 1265 /* 1266 * The vnode may now be recycled. 1267 */ 1268 if (object->resident_page_count == 0 && object->type == OBJT_VNODE) 1269 vdrop(object->handle); 1270 1271 m->object = NULL; 1272 } 1273 1274 /* 1275 * vm_page_lookup: 1276 * 1277 * Returns the page associated with the object/offset 1278 * pair specified; if none is found, NULL is returned. 1279 * 1280 * The object must be locked. 1281 */ 1282 vm_page_t 1283 vm_page_lookup(vm_object_t object, vm_pindex_t pindex) 1284 { 1285 1286 VM_OBJECT_ASSERT_LOCKED(object); 1287 return (vm_radix_lookup(&object->rtree, pindex)); 1288 } 1289 1290 /* 1291 * vm_page_find_least: 1292 * 1293 * Returns the page associated with the object with least pindex 1294 * greater than or equal to the parameter pindex, or NULL. 1295 * 1296 * The object must be locked. 1297 */ 1298 vm_page_t 1299 vm_page_find_least(vm_object_t object, vm_pindex_t pindex) 1300 { 1301 vm_page_t m; 1302 1303 VM_OBJECT_ASSERT_LOCKED(object); 1304 if ((m = TAILQ_FIRST(&object->memq)) != NULL && m->pindex < pindex) 1305 m = vm_radix_lookup_ge(&object->rtree, pindex); 1306 return (m); 1307 } 1308 1309 /* 1310 * Returns the given page's successor (by pindex) within the object if it is 1311 * resident; if none is found, NULL is returned. 1312 * 1313 * The object must be locked. 1314 */ 1315 vm_page_t 1316 vm_page_next(vm_page_t m) 1317 { 1318 vm_page_t next; 1319 1320 VM_OBJECT_ASSERT_LOCKED(m->object); 1321 if ((next = TAILQ_NEXT(m, listq)) != NULL && 1322 next->pindex != m->pindex + 1) 1323 next = NULL; 1324 return (next); 1325 } 1326 1327 /* 1328 * Returns the given page's predecessor (by pindex) within the object if it is 1329 * resident; if none is found, NULL is returned. 1330 * 1331 * The object must be locked. 1332 */ 1333 vm_page_t 1334 vm_page_prev(vm_page_t m) 1335 { 1336 vm_page_t prev; 1337 1338 VM_OBJECT_ASSERT_LOCKED(m->object); 1339 if ((prev = TAILQ_PREV(m, pglist, listq)) != NULL && 1340 prev->pindex != m->pindex - 1) 1341 prev = NULL; 1342 return (prev); 1343 } 1344 1345 /* 1346 * Uses the page mnew as a replacement for an existing page at index 1347 * pindex which must be already present in the object. 1348 * 1349 * The existing page must not be on a paging queue. 1350 */ 1351 vm_page_t 1352 vm_page_replace(vm_page_t mnew, vm_object_t object, vm_pindex_t pindex) 1353 { 1354 vm_page_t mold; 1355 1356 VM_OBJECT_ASSERT_WLOCKED(object); 1357 KASSERT(mnew->object == NULL, 1358 ("vm_page_replace: page already in object")); 1359 1360 /* 1361 * This function mostly follows vm_page_insert() and 1362 * vm_page_remove() without the radix, object count and vnode 1363 * dance. Double check such functions for more comments. 1364 */ 1365 1366 mnew->object = object; 1367 mnew->pindex = pindex; 1368 mold = vm_radix_replace(&object->rtree, mnew); 1369 KASSERT(mold->queue == PQ_NONE, 1370 ("vm_page_replace: mold is on a paging queue")); 1371 1372 /* Keep the resident page list in sorted order. */ 1373 TAILQ_INSERT_AFTER(&object->memq, mold, mnew, listq); 1374 TAILQ_REMOVE(&object->memq, mold, listq); 1375 1376 mold->object = NULL; 1377 vm_page_xunbusy_maybelocked(mold); 1378 1379 /* 1380 * The object's resident_page_count does not change because we have 1381 * swapped one page for another, but OBJ_MIGHTBEDIRTY. 1382 */ 1383 if (pmap_page_is_write_mapped(mnew)) 1384 vm_object_set_writeable_dirty(object); 1385 return (mold); 1386 } 1387 1388 /* 1389 * vm_page_rename: 1390 * 1391 * Move the given memory entry from its 1392 * current object to the specified target object/offset. 1393 * 1394 * Note: swap associated with the page must be invalidated by the move. We 1395 * have to do this for several reasons: (1) we aren't freeing the 1396 * page, (2) we are dirtying the page, (3) the VM system is probably 1397 * moving the page from object A to B, and will then later move 1398 * the backing store from A to B and we can't have a conflict. 1399 * 1400 * Note: we *always* dirty the page. It is necessary both for the 1401 * fact that we moved it, and because we may be invalidating 1402 * swap. If the page is on the cache, we have to deactivate it 1403 * or vm_page_dirty() will panic. Dirty pages are not allowed 1404 * on the cache. 1405 * 1406 * The objects must be locked. 1407 */ 1408 int 1409 vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex) 1410 { 1411 vm_page_t mpred; 1412 vm_pindex_t opidx; 1413 1414 VM_OBJECT_ASSERT_WLOCKED(new_object); 1415 1416 mpred = vm_radix_lookup_le(&new_object->rtree, new_pindex); 1417 KASSERT(mpred == NULL || mpred->pindex != new_pindex, 1418 ("vm_page_rename: pindex already renamed")); 1419 1420 /* 1421 * Create a custom version of vm_page_insert() which does not depend 1422 * by m_prev and can cheat on the implementation aspects of the 1423 * function. 1424 */ 1425 opidx = m->pindex; 1426 m->pindex = new_pindex; 1427 if (vm_radix_insert(&new_object->rtree, m)) { 1428 m->pindex = opidx; 1429 return (1); 1430 } 1431 1432 /* 1433 * The operation cannot fail anymore. The removal must happen before 1434 * the listq iterator is tainted. 1435 */ 1436 m->pindex = opidx; 1437 vm_page_lock(m); 1438 vm_page_remove(m); 1439 1440 /* Return back to the new pindex to complete vm_page_insert(). */ 1441 m->pindex = new_pindex; 1442 m->object = new_object; 1443 vm_page_unlock(m); 1444 vm_page_insert_radixdone(m, new_object, mpred); 1445 vm_page_dirty(m); 1446 return (0); 1447 } 1448 1449 /* 1450 * Convert all of the given object's cached pages that have a 1451 * pindex within the given range into free pages. If the value 1452 * zero is given for "end", then the range's upper bound is 1453 * infinity. If the given object is backed by a vnode and it 1454 * transitions from having one or more cached pages to none, the 1455 * vnode's hold count is reduced. 1456 */ 1457 void 1458 vm_page_cache_free(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 1459 { 1460 vm_page_t m; 1461 boolean_t empty; 1462 1463 mtx_lock(&vm_page_queue_free_mtx); 1464 if (__predict_false(vm_radix_is_empty(&object->cache))) { 1465 mtx_unlock(&vm_page_queue_free_mtx); 1466 return; 1467 } 1468 while ((m = vm_radix_lookup_ge(&object->cache, start)) != NULL) { 1469 if (end != 0 && m->pindex >= end) 1470 break; 1471 vm_radix_remove(&object->cache, m->pindex); 1472 vm_page_cache_turn_free(m); 1473 } 1474 empty = vm_radix_is_empty(&object->cache); 1475 mtx_unlock(&vm_page_queue_free_mtx); 1476 if (object->type == OBJT_VNODE && empty) 1477 vdrop(object->handle); 1478 } 1479 1480 /* 1481 * Returns the cached page that is associated with the given 1482 * object and offset. If, however, none exists, returns NULL. 1483 * 1484 * The free page queue must be locked. 1485 */ 1486 static inline vm_page_t 1487 vm_page_cache_lookup(vm_object_t object, vm_pindex_t pindex) 1488 { 1489 1490 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 1491 return (vm_radix_lookup(&object->cache, pindex)); 1492 } 1493 1494 /* 1495 * Remove the given cached page from its containing object's 1496 * collection of cached pages. 1497 * 1498 * The free page queue must be locked. 1499 */ 1500 static void 1501 vm_page_cache_remove(vm_page_t m) 1502 { 1503 1504 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 1505 KASSERT((m->flags & PG_CACHED) != 0, 1506 ("vm_page_cache_remove: page %p is not cached", m)); 1507 vm_radix_remove(&m->object->cache, m->pindex); 1508 m->object = NULL; 1509 vm_cnt.v_cache_count--; 1510 } 1511 1512 /* 1513 * Transfer all of the cached pages with offset greater than or 1514 * equal to 'offidxstart' from the original object's cache to the 1515 * new object's cache. However, any cached pages with offset 1516 * greater than or equal to the new object's size are kept in the 1517 * original object. Initially, the new object's cache must be 1518 * empty. Offset 'offidxstart' in the original object must 1519 * correspond to offset zero in the new object. 1520 * 1521 * The new object must be locked. 1522 */ 1523 void 1524 vm_page_cache_transfer(vm_object_t orig_object, vm_pindex_t offidxstart, 1525 vm_object_t new_object) 1526 { 1527 vm_page_t m; 1528 1529 /* 1530 * Insertion into an object's collection of cached pages 1531 * requires the object to be locked. In contrast, removal does 1532 * not. 1533 */ 1534 VM_OBJECT_ASSERT_WLOCKED(new_object); 1535 KASSERT(vm_radix_is_empty(&new_object->cache), 1536 ("vm_page_cache_transfer: object %p has cached pages", 1537 new_object)); 1538 mtx_lock(&vm_page_queue_free_mtx); 1539 while ((m = vm_radix_lookup_ge(&orig_object->cache, 1540 offidxstart)) != NULL) { 1541 /* 1542 * Transfer all of the pages with offset greater than or 1543 * equal to 'offidxstart' from the original object's 1544 * cache to the new object's cache. 1545 */ 1546 if ((m->pindex - offidxstart) >= new_object->size) 1547 break; 1548 vm_radix_remove(&orig_object->cache, m->pindex); 1549 /* Update the page's object and offset. */ 1550 m->object = new_object; 1551 m->pindex -= offidxstart; 1552 if (vm_radix_insert(&new_object->cache, m)) 1553 vm_page_cache_turn_free(m); 1554 } 1555 mtx_unlock(&vm_page_queue_free_mtx); 1556 } 1557 1558 /* 1559 * Returns TRUE if a cached page is associated with the given object and 1560 * offset, and FALSE otherwise. 1561 * 1562 * The object must be locked. 1563 */ 1564 boolean_t 1565 vm_page_is_cached(vm_object_t object, vm_pindex_t pindex) 1566 { 1567 vm_page_t m; 1568 1569 /* 1570 * Insertion into an object's collection of cached pages requires the 1571 * object to be locked. Therefore, if the object is locked and the 1572 * object's collection is empty, there is no need to acquire the free 1573 * page queues lock in order to prove that the specified page doesn't 1574 * exist. 1575 */ 1576 VM_OBJECT_ASSERT_WLOCKED(object); 1577 if (__predict_true(vm_object_cache_is_empty(object))) 1578 return (FALSE); 1579 mtx_lock(&vm_page_queue_free_mtx); 1580 m = vm_page_cache_lookup(object, pindex); 1581 mtx_unlock(&vm_page_queue_free_mtx); 1582 return (m != NULL); 1583 } 1584 1585 /* 1586 * vm_page_alloc: 1587 * 1588 * Allocate and return a page that is associated with the specified 1589 * object and offset pair. By default, this page is exclusive busied. 1590 * 1591 * The caller must always specify an allocation class. 1592 * 1593 * allocation classes: 1594 * VM_ALLOC_NORMAL normal process request 1595 * VM_ALLOC_SYSTEM system *really* needs a page 1596 * VM_ALLOC_INTERRUPT interrupt time request 1597 * 1598 * optional allocation flags: 1599 * VM_ALLOC_COUNT(number) the number of additional pages that the caller 1600 * intends to allocate 1601 * VM_ALLOC_IFCACHED return page only if it is cached 1602 * VM_ALLOC_IFNOTCACHED return NULL, do not reactivate if the page 1603 * is cached 1604 * VM_ALLOC_NOBUSY do not exclusive busy the page 1605 * VM_ALLOC_NODUMP do not include the page in a kernel core dump 1606 * VM_ALLOC_NOOBJ page is not associated with an object and 1607 * should not be exclusive busy 1608 * VM_ALLOC_SBUSY shared busy the allocated page 1609 * VM_ALLOC_WIRED wire the allocated page 1610 * VM_ALLOC_ZERO prefer a zeroed page 1611 * 1612 * This routine may not sleep. 1613 */ 1614 vm_page_t 1615 vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req) 1616 { 1617 struct vnode *vp = NULL; 1618 vm_object_t m_object; 1619 vm_page_t m, mpred; 1620 int flags, req_class; 1621 1622 mpred = 0; /* XXX: pacify gcc */ 1623 KASSERT((object != NULL) == ((req & VM_ALLOC_NOOBJ) == 0) && 1624 (object != NULL || (req & VM_ALLOC_SBUSY) == 0) && 1625 ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) != 1626 (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)), 1627 ("vm_page_alloc: inconsistent object(%p)/req(%x)", (void *)object, 1628 req)); 1629 if (object != NULL) 1630 VM_OBJECT_ASSERT_WLOCKED(object); 1631 1632 req_class = req & VM_ALLOC_CLASS_MASK; 1633 1634 /* 1635 * The page daemon is allowed to dig deeper into the free page list. 1636 */ 1637 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT) 1638 req_class = VM_ALLOC_SYSTEM; 1639 1640 if (object != NULL) { 1641 mpred = vm_radix_lookup_le(&object->rtree, pindex); 1642 KASSERT(mpred == NULL || mpred->pindex != pindex, 1643 ("vm_page_alloc: pindex already allocated")); 1644 } 1645 1646 /* 1647 * The page allocation request can came from consumers which already 1648 * hold the free page queue mutex, like vm_page_insert() in 1649 * vm_page_cache(). 1650 */ 1651 mtx_lock_flags(&vm_page_queue_free_mtx, MTX_RECURSE); 1652 if (vm_cnt.v_free_count + vm_cnt.v_cache_count > vm_cnt.v_free_reserved || 1653 (req_class == VM_ALLOC_SYSTEM && 1654 vm_cnt.v_free_count + vm_cnt.v_cache_count > vm_cnt.v_interrupt_free_min) || 1655 (req_class == VM_ALLOC_INTERRUPT && 1656 vm_cnt.v_free_count + vm_cnt.v_cache_count > 0)) { 1657 /* 1658 * Allocate from the free queue if the number of free pages 1659 * exceeds the minimum for the request class. 1660 */ 1661 if (object != NULL && 1662 (m = vm_page_cache_lookup(object, pindex)) != NULL) { 1663 if ((req & VM_ALLOC_IFNOTCACHED) != 0) { 1664 mtx_unlock(&vm_page_queue_free_mtx); 1665 return (NULL); 1666 } 1667 if (vm_phys_unfree_page(m)) 1668 vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m, 0); 1669 #if VM_NRESERVLEVEL > 0 1670 else if (!vm_reserv_reactivate_page(m)) 1671 #else 1672 else 1673 #endif 1674 panic("vm_page_alloc: cache page %p is missing" 1675 " from the free queue", m); 1676 } else if ((req & VM_ALLOC_IFCACHED) != 0) { 1677 mtx_unlock(&vm_page_queue_free_mtx); 1678 return (NULL); 1679 #if VM_NRESERVLEVEL > 0 1680 } else if (object == NULL || (object->flags & (OBJ_COLORED | 1681 OBJ_FICTITIOUS)) != OBJ_COLORED || (m = 1682 vm_reserv_alloc_page(object, pindex, mpred)) == NULL) { 1683 #else 1684 } else { 1685 #endif 1686 m = vm_phys_alloc_pages(object != NULL ? 1687 VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT, 0); 1688 #if VM_NRESERVLEVEL > 0 1689 if (m == NULL && vm_reserv_reclaim_inactive()) { 1690 m = vm_phys_alloc_pages(object != NULL ? 1691 VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT, 1692 0); 1693 } 1694 #endif 1695 } 1696 } else { 1697 /* 1698 * Not allocatable, give up. 1699 */ 1700 mtx_unlock(&vm_page_queue_free_mtx); 1701 atomic_add_int(&vm_pageout_deficit, 1702 max((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1)); 1703 pagedaemon_wakeup(); 1704 return (NULL); 1705 } 1706 1707 /* 1708 * At this point we had better have found a good page. 1709 */ 1710 KASSERT(m != NULL, ("vm_page_alloc: missing page")); 1711 KASSERT(m->queue == PQ_NONE, 1712 ("vm_page_alloc: page %p has unexpected queue %d", m, m->queue)); 1713 KASSERT(m->wire_count == 0, ("vm_page_alloc: page %p is wired", m)); 1714 KASSERT(m->hold_count == 0, ("vm_page_alloc: page %p is held", m)); 1715 KASSERT(!vm_page_busied(m), ("vm_page_alloc: page %p is busy", m)); 1716 KASSERT(m->dirty == 0, ("vm_page_alloc: page %p is dirty", m)); 1717 KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT, 1718 ("vm_page_alloc: page %p has unexpected memattr %d", m, 1719 pmap_page_get_memattr(m))); 1720 if ((m->flags & PG_CACHED) != 0) { 1721 KASSERT((m->flags & PG_ZERO) == 0, 1722 ("vm_page_alloc: cached page %p is PG_ZERO", m)); 1723 KASSERT(m->valid != 0, 1724 ("vm_page_alloc: cached page %p is invalid", m)); 1725 if (m->object == object && m->pindex == pindex) 1726 vm_cnt.v_reactivated++; 1727 else 1728 m->valid = 0; 1729 m_object = m->object; 1730 vm_page_cache_remove(m); 1731 if (m_object->type == OBJT_VNODE && 1732 vm_object_cache_is_empty(m_object)) 1733 vp = m_object->handle; 1734 } else { 1735 KASSERT(m->valid == 0, 1736 ("vm_page_alloc: free page %p is valid", m)); 1737 vm_phys_freecnt_adj(m, -1); 1738 if ((m->flags & PG_ZERO) != 0) 1739 vm_page_zero_count--; 1740 } 1741 mtx_unlock(&vm_page_queue_free_mtx); 1742 1743 /* 1744 * Initialize the page. Only the PG_ZERO flag is inherited. 1745 */ 1746 flags = 0; 1747 if ((req & VM_ALLOC_ZERO) != 0) 1748 flags = PG_ZERO; 1749 flags &= m->flags; 1750 if ((req & VM_ALLOC_NODUMP) != 0) 1751 flags |= PG_NODUMP; 1752 m->flags = flags; 1753 m->aflags = 0; 1754 m->oflags = object == NULL || (object->flags & OBJ_UNMANAGED) != 0 ? 1755 VPO_UNMANAGED : 0; 1756 m->busy_lock = VPB_UNBUSIED; 1757 if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ | VM_ALLOC_SBUSY)) == 0) 1758 m->busy_lock = VPB_SINGLE_EXCLUSIVER; 1759 if ((req & VM_ALLOC_SBUSY) != 0) 1760 m->busy_lock = VPB_SHARERS_WORD(1); 1761 if (req & VM_ALLOC_WIRED) { 1762 /* 1763 * The page lock is not required for wiring a page until that 1764 * page is inserted into the object. 1765 */ 1766 atomic_add_int(&vm_cnt.v_wire_count, 1); 1767 m->wire_count = 1; 1768 } 1769 m->act_count = 0; 1770 1771 if (object != NULL) { 1772 if (vm_page_insert_after(m, object, pindex, mpred)) { 1773 /* See the comment below about hold count. */ 1774 if (vp != NULL) 1775 vdrop(vp); 1776 pagedaemon_wakeup(); 1777 if (req & VM_ALLOC_WIRED) { 1778 atomic_subtract_int(&vm_cnt.v_wire_count, 1); 1779 m->wire_count = 0; 1780 } 1781 m->object = NULL; 1782 m->oflags = VPO_UNMANAGED; 1783 m->busy_lock = VPB_UNBUSIED; 1784 vm_page_free(m); 1785 return (NULL); 1786 } 1787 1788 /* Ignore device objects; the pager sets "memattr" for them. */ 1789 if (object->memattr != VM_MEMATTR_DEFAULT && 1790 (object->flags & OBJ_FICTITIOUS) == 0) 1791 pmap_page_set_memattr(m, object->memattr); 1792 } else 1793 m->pindex = pindex; 1794 1795 /* 1796 * The following call to vdrop() must come after the above call 1797 * to vm_page_insert() in case both affect the same object and 1798 * vnode. Otherwise, the affected vnode's hold count could 1799 * temporarily become zero. 1800 */ 1801 if (vp != NULL) 1802 vdrop(vp); 1803 1804 /* 1805 * Don't wakeup too often - wakeup the pageout daemon when 1806 * we would be nearly out of memory. 1807 */ 1808 if (vm_paging_needed()) 1809 pagedaemon_wakeup(); 1810 1811 return (m); 1812 } 1813 1814 static void 1815 vm_page_alloc_contig_vdrop(struct spglist *lst) 1816 { 1817 1818 while (!SLIST_EMPTY(lst)) { 1819 vdrop((struct vnode *)SLIST_FIRST(lst)-> plinks.s.pv); 1820 SLIST_REMOVE_HEAD(lst, plinks.s.ss); 1821 } 1822 } 1823 1824 /* 1825 * vm_page_alloc_contig: 1826 * 1827 * Allocate a contiguous set of physical pages of the given size "npages" 1828 * from the free lists. All of the physical pages must be at or above 1829 * the given physical address "low" and below the given physical address 1830 * "high". The given value "alignment" determines the alignment of the 1831 * first physical page in the set. If the given value "boundary" is 1832 * non-zero, then the set of physical pages cannot cross any physical 1833 * address boundary that is a multiple of that value. Both "alignment" 1834 * and "boundary" must be a power of two. 1835 * 1836 * If the specified memory attribute, "memattr", is VM_MEMATTR_DEFAULT, 1837 * then the memory attribute setting for the physical pages is configured 1838 * to the object's memory attribute setting. Otherwise, the memory 1839 * attribute setting for the physical pages is configured to "memattr", 1840 * overriding the object's memory attribute setting. However, if the 1841 * object's memory attribute setting is not VM_MEMATTR_DEFAULT, then the 1842 * memory attribute setting for the physical pages cannot be configured 1843 * to VM_MEMATTR_DEFAULT. 1844 * 1845 * The caller must always specify an allocation class. 1846 * 1847 * allocation classes: 1848 * VM_ALLOC_NORMAL normal process request 1849 * VM_ALLOC_SYSTEM system *really* needs a page 1850 * VM_ALLOC_INTERRUPT interrupt time request 1851 * 1852 * optional allocation flags: 1853 * VM_ALLOC_NOBUSY do not exclusive busy the page 1854 * VM_ALLOC_NODUMP do not include the page in a kernel core dump 1855 * VM_ALLOC_NOOBJ page is not associated with an object and 1856 * should not be exclusive busy 1857 * VM_ALLOC_SBUSY shared busy the allocated page 1858 * VM_ALLOC_WIRED wire the allocated page 1859 * VM_ALLOC_ZERO prefer a zeroed page 1860 * 1861 * This routine may not sleep. 1862 */ 1863 vm_page_t 1864 vm_page_alloc_contig(vm_object_t object, vm_pindex_t pindex, int req, 1865 u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment, 1866 vm_paddr_t boundary, vm_memattr_t memattr) 1867 { 1868 struct vnode *drop; 1869 struct spglist deferred_vdrop_list; 1870 vm_page_t m, m_tmp, m_ret; 1871 u_int flags; 1872 int req_class; 1873 1874 KASSERT((object != NULL) == ((req & VM_ALLOC_NOOBJ) == 0) && 1875 (object != NULL || (req & VM_ALLOC_SBUSY) == 0) && 1876 ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) != 1877 (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)), 1878 ("vm_page_alloc: inconsistent object(%p)/req(%x)", (void *)object, 1879 req)); 1880 if (object != NULL) { 1881 VM_OBJECT_ASSERT_WLOCKED(object); 1882 KASSERT(object->type == OBJT_PHYS, 1883 ("vm_page_alloc_contig: object %p isn't OBJT_PHYS", 1884 object)); 1885 } 1886 KASSERT(npages > 0, ("vm_page_alloc_contig: npages is zero")); 1887 req_class = req & VM_ALLOC_CLASS_MASK; 1888 1889 /* 1890 * The page daemon is allowed to dig deeper into the free page list. 1891 */ 1892 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT) 1893 req_class = VM_ALLOC_SYSTEM; 1894 1895 SLIST_INIT(&deferred_vdrop_list); 1896 mtx_lock(&vm_page_queue_free_mtx); 1897 if (vm_cnt.v_free_count + vm_cnt.v_cache_count >= npages + 1898 vm_cnt.v_free_reserved || (req_class == VM_ALLOC_SYSTEM && 1899 vm_cnt.v_free_count + vm_cnt.v_cache_count >= npages + 1900 vm_cnt.v_interrupt_free_min) || (req_class == VM_ALLOC_INTERRUPT && 1901 vm_cnt.v_free_count + vm_cnt.v_cache_count >= npages)) { 1902 #if VM_NRESERVLEVEL > 0 1903 retry: 1904 if (object == NULL || (object->flags & OBJ_COLORED) == 0 || 1905 (m_ret = vm_reserv_alloc_contig(object, pindex, npages, 1906 low, high, alignment, boundary)) == NULL) 1907 #endif 1908 m_ret = vm_phys_alloc_contig(npages, low, high, 1909 alignment, boundary); 1910 } else { 1911 mtx_unlock(&vm_page_queue_free_mtx); 1912 atomic_add_int(&vm_pageout_deficit, npages); 1913 pagedaemon_wakeup(); 1914 return (NULL); 1915 } 1916 if (m_ret != NULL) 1917 for (m = m_ret; m < &m_ret[npages]; m++) { 1918 drop = vm_page_alloc_init(m); 1919 if (drop != NULL) { 1920 /* 1921 * Enqueue the vnode for deferred vdrop(). 1922 */ 1923 m->plinks.s.pv = drop; 1924 SLIST_INSERT_HEAD(&deferred_vdrop_list, m, 1925 plinks.s.ss); 1926 } 1927 } 1928 else { 1929 #if VM_NRESERVLEVEL > 0 1930 if (vm_reserv_reclaim_contig(npages, low, high, alignment, 1931 boundary)) 1932 goto retry; 1933 #endif 1934 } 1935 mtx_unlock(&vm_page_queue_free_mtx); 1936 if (m_ret == NULL) 1937 return (NULL); 1938 1939 /* 1940 * Initialize the pages. Only the PG_ZERO flag is inherited. 1941 */ 1942 flags = 0; 1943 if ((req & VM_ALLOC_ZERO) != 0) 1944 flags = PG_ZERO; 1945 if ((req & VM_ALLOC_NODUMP) != 0) 1946 flags |= PG_NODUMP; 1947 if ((req & VM_ALLOC_WIRED) != 0) 1948 atomic_add_int(&vm_cnt.v_wire_count, npages); 1949 if (object != NULL) { 1950 if (object->memattr != VM_MEMATTR_DEFAULT && 1951 memattr == VM_MEMATTR_DEFAULT) 1952 memattr = object->memattr; 1953 } 1954 for (m = m_ret; m < &m_ret[npages]; m++) { 1955 m->aflags = 0; 1956 m->flags = (m->flags | PG_NODUMP) & flags; 1957 m->busy_lock = VPB_UNBUSIED; 1958 if (object != NULL) { 1959 if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) == 0) 1960 m->busy_lock = VPB_SINGLE_EXCLUSIVER; 1961 if ((req & VM_ALLOC_SBUSY) != 0) 1962 m->busy_lock = VPB_SHARERS_WORD(1); 1963 } 1964 if ((req & VM_ALLOC_WIRED) != 0) 1965 m->wire_count = 1; 1966 /* Unmanaged pages don't use "act_count". */ 1967 m->oflags = VPO_UNMANAGED; 1968 if (object != NULL) { 1969 if (vm_page_insert(m, object, pindex)) { 1970 vm_page_alloc_contig_vdrop( 1971 &deferred_vdrop_list); 1972 if (vm_paging_needed()) 1973 pagedaemon_wakeup(); 1974 if ((req & VM_ALLOC_WIRED) != 0) 1975 atomic_subtract_int(&vm_cnt.v_wire_count, 1976 npages); 1977 for (m_tmp = m, m = m_ret; 1978 m < &m_ret[npages]; m++) { 1979 if ((req & VM_ALLOC_WIRED) != 0) 1980 m->wire_count = 0; 1981 if (m >= m_tmp) { 1982 m->object = NULL; 1983 m->oflags |= VPO_UNMANAGED; 1984 } 1985 m->busy_lock = VPB_UNBUSIED; 1986 vm_page_free(m); 1987 } 1988 return (NULL); 1989 } 1990 } else 1991 m->pindex = pindex; 1992 if (memattr != VM_MEMATTR_DEFAULT) 1993 pmap_page_set_memattr(m, memattr); 1994 pindex++; 1995 } 1996 vm_page_alloc_contig_vdrop(&deferred_vdrop_list); 1997 if (vm_paging_needed()) 1998 pagedaemon_wakeup(); 1999 return (m_ret); 2000 } 2001 2002 /* 2003 * Initialize a page that has been freshly dequeued from a freelist. 2004 * The caller has to drop the vnode returned, if it is not NULL. 2005 * 2006 * This function may only be used to initialize unmanaged pages. 2007 * 2008 * To be called with vm_page_queue_free_mtx held. 2009 */ 2010 static struct vnode * 2011 vm_page_alloc_init(vm_page_t m) 2012 { 2013 struct vnode *drop; 2014 vm_object_t m_object; 2015 2016 KASSERT(m->queue == PQ_NONE, 2017 ("vm_page_alloc_init: page %p has unexpected queue %d", 2018 m, m->queue)); 2019 KASSERT(m->wire_count == 0, 2020 ("vm_page_alloc_init: page %p is wired", m)); 2021 KASSERT(m->hold_count == 0, 2022 ("vm_page_alloc_init: page %p is held", m)); 2023 KASSERT(!vm_page_busied(m), 2024 ("vm_page_alloc_init: page %p is busy", m)); 2025 KASSERT(m->dirty == 0, 2026 ("vm_page_alloc_init: page %p is dirty", m)); 2027 KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT, 2028 ("vm_page_alloc_init: page %p has unexpected memattr %d", 2029 m, pmap_page_get_memattr(m))); 2030 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 2031 drop = NULL; 2032 if ((m->flags & PG_CACHED) != 0) { 2033 KASSERT((m->flags & PG_ZERO) == 0, 2034 ("vm_page_alloc_init: cached page %p is PG_ZERO", m)); 2035 m->valid = 0; 2036 m_object = m->object; 2037 vm_page_cache_remove(m); 2038 if (m_object->type == OBJT_VNODE && 2039 vm_object_cache_is_empty(m_object)) 2040 drop = m_object->handle; 2041 } else { 2042 KASSERT(m->valid == 0, 2043 ("vm_page_alloc_init: free page %p is valid", m)); 2044 vm_phys_freecnt_adj(m, -1); 2045 if ((m->flags & PG_ZERO) != 0) 2046 vm_page_zero_count--; 2047 } 2048 return (drop); 2049 } 2050 2051 /* 2052 * vm_page_alloc_freelist: 2053 * 2054 * Allocate a physical page from the specified free page list. 2055 * 2056 * The caller must always specify an allocation class. 2057 * 2058 * allocation classes: 2059 * VM_ALLOC_NORMAL normal process request 2060 * VM_ALLOC_SYSTEM system *really* needs a page 2061 * VM_ALLOC_INTERRUPT interrupt time request 2062 * 2063 * optional allocation flags: 2064 * VM_ALLOC_COUNT(number) the number of additional pages that the caller 2065 * intends to allocate 2066 * VM_ALLOC_WIRED wire the allocated page 2067 * VM_ALLOC_ZERO prefer a zeroed page 2068 * 2069 * This routine may not sleep. 2070 */ 2071 vm_page_t 2072 vm_page_alloc_freelist(int flind, int req) 2073 { 2074 struct vnode *drop; 2075 vm_page_t m; 2076 u_int flags; 2077 int req_class; 2078 2079 req_class = req & VM_ALLOC_CLASS_MASK; 2080 2081 /* 2082 * The page daemon is allowed to dig deeper into the free page list. 2083 */ 2084 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT) 2085 req_class = VM_ALLOC_SYSTEM; 2086 2087 /* 2088 * Do not allocate reserved pages unless the req has asked for it. 2089 */ 2090 mtx_lock_flags(&vm_page_queue_free_mtx, MTX_RECURSE); 2091 if (vm_cnt.v_free_count + vm_cnt.v_cache_count > vm_cnt.v_free_reserved || 2092 (req_class == VM_ALLOC_SYSTEM && 2093 vm_cnt.v_free_count + vm_cnt.v_cache_count > vm_cnt.v_interrupt_free_min) || 2094 (req_class == VM_ALLOC_INTERRUPT && 2095 vm_cnt.v_free_count + vm_cnt.v_cache_count > 0)) 2096 m = vm_phys_alloc_freelist_pages(flind, VM_FREEPOOL_DIRECT, 0); 2097 else { 2098 mtx_unlock(&vm_page_queue_free_mtx); 2099 atomic_add_int(&vm_pageout_deficit, 2100 max((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1)); 2101 pagedaemon_wakeup(); 2102 return (NULL); 2103 } 2104 if (m == NULL) { 2105 mtx_unlock(&vm_page_queue_free_mtx); 2106 return (NULL); 2107 } 2108 drop = vm_page_alloc_init(m); 2109 mtx_unlock(&vm_page_queue_free_mtx); 2110 2111 /* 2112 * Initialize the page. Only the PG_ZERO flag is inherited. 2113 */ 2114 m->aflags = 0; 2115 flags = 0; 2116 if ((req & VM_ALLOC_ZERO) != 0) 2117 flags = PG_ZERO; 2118 m->flags &= flags; 2119 if ((req & VM_ALLOC_WIRED) != 0) { 2120 /* 2121 * The page lock is not required for wiring a page that does 2122 * not belong to an object. 2123 */ 2124 atomic_add_int(&vm_cnt.v_wire_count, 1); 2125 m->wire_count = 1; 2126 } 2127 /* Unmanaged pages don't use "act_count". */ 2128 m->oflags = VPO_UNMANAGED; 2129 if (drop != NULL) 2130 vdrop(drop); 2131 if (vm_paging_needed()) 2132 pagedaemon_wakeup(); 2133 return (m); 2134 } 2135 2136 #define VPSC_ANY 0 /* No restrictions. */ 2137 #define VPSC_NORESERV 1 /* Skip reservations; implies VPSC_NOSUPER. */ 2138 #define VPSC_NOSUPER 2 /* Skip superpages. */ 2139 2140 /* 2141 * vm_page_scan_contig: 2142 * 2143 * Scan vm_page_array[] between the specified entries "m_start" and 2144 * "m_end" for a run of contiguous physical pages that satisfy the 2145 * specified conditions, and return the lowest page in the run. The 2146 * specified "alignment" determines the alignment of the lowest physical 2147 * page in the run. If the specified "boundary" is non-zero, then the 2148 * run of physical pages cannot span a physical address that is a 2149 * multiple of "boundary". 2150 * 2151 * "m_end" is never dereferenced, so it need not point to a vm_page 2152 * structure within vm_page_array[]. 2153 * 2154 * "npages" must be greater than zero. "m_start" and "m_end" must not 2155 * span a hole (or discontiguity) in the physical address space. Both 2156 * "alignment" and "boundary" must be a power of two. 2157 */ 2158 vm_page_t 2159 vm_page_scan_contig(u_long npages, vm_page_t m_start, vm_page_t m_end, 2160 u_long alignment, vm_paddr_t boundary, int options) 2161 { 2162 struct mtx *m_mtx, *new_mtx; 2163 vm_object_t object; 2164 vm_paddr_t pa; 2165 vm_page_t m, m_run; 2166 #if VM_NRESERVLEVEL > 0 2167 int level; 2168 #endif 2169 int m_inc, order, run_ext, run_len; 2170 2171 KASSERT(npages > 0, ("npages is 0")); 2172 KASSERT(powerof2(alignment), ("alignment is not a power of 2")); 2173 KASSERT(powerof2(boundary), ("boundary is not a power of 2")); 2174 m_run = NULL; 2175 run_len = 0; 2176 m_mtx = NULL; 2177 for (m = m_start; m < m_end && run_len < npages; m += m_inc) { 2178 KASSERT((m->flags & (PG_FICTITIOUS | PG_MARKER)) == 0, 2179 ("page %p is PG_FICTITIOUS or PG_MARKER", m)); 2180 2181 /* 2182 * If the current page would be the start of a run, check its 2183 * physical address against the end, alignment, and boundary 2184 * conditions. If it doesn't satisfy these conditions, either 2185 * terminate the scan or advance to the next page that 2186 * satisfies the failed condition. 2187 */ 2188 if (run_len == 0) { 2189 KASSERT(m_run == NULL, ("m_run != NULL")); 2190 if (m + npages > m_end) 2191 break; 2192 pa = VM_PAGE_TO_PHYS(m); 2193 if ((pa & (alignment - 1)) != 0) { 2194 m_inc = atop(roundup2(pa, alignment) - pa); 2195 continue; 2196 } 2197 if (rounddown2(pa ^ (pa + ptoa(npages) - 1), 2198 boundary) != 0) { 2199 m_inc = atop(roundup2(pa, boundary) - pa); 2200 continue; 2201 } 2202 } else 2203 KASSERT(m_run != NULL, ("m_run == NULL")); 2204 2205 /* 2206 * Avoid releasing and reacquiring the same page lock. 2207 */ 2208 new_mtx = vm_page_lockptr(m); 2209 if (m_mtx != new_mtx) { 2210 if (m_mtx != NULL) 2211 mtx_unlock(m_mtx); 2212 m_mtx = new_mtx; 2213 mtx_lock(m_mtx); 2214 } 2215 m_inc = 1; 2216 retry: 2217 if (m->wire_count != 0 || m->hold_count != 0) 2218 run_ext = 0; 2219 #if VM_NRESERVLEVEL > 0 2220 else if ((level = vm_reserv_level(m)) >= 0 && 2221 (options & VPSC_NORESERV) != 0) { 2222 run_ext = 0; 2223 /* Advance to the end of the reservation. */ 2224 pa = VM_PAGE_TO_PHYS(m); 2225 m_inc = atop(roundup2(pa + 1, vm_reserv_size(level)) - 2226 pa); 2227 } 2228 #endif 2229 else if ((object = m->object) != NULL) { 2230 /* 2231 * The page is considered eligible for relocation if 2232 * and only if it could be laundered or reclaimed by 2233 * the page daemon. 2234 */ 2235 if (!VM_OBJECT_TRYRLOCK(object)) { 2236 mtx_unlock(m_mtx); 2237 VM_OBJECT_RLOCK(object); 2238 mtx_lock(m_mtx); 2239 if (m->object != object) { 2240 /* 2241 * The page may have been freed. 2242 */ 2243 VM_OBJECT_RUNLOCK(object); 2244 goto retry; 2245 } else if (m->wire_count != 0 || 2246 m->hold_count != 0) { 2247 run_ext = 0; 2248 goto unlock; 2249 } 2250 } 2251 KASSERT((m->flags & PG_UNHOLDFREE) == 0, 2252 ("page %p is PG_UNHOLDFREE", m)); 2253 /* Don't care: PG_NODUMP, PG_WINATCFLS, PG_ZERO. */ 2254 if (object->type != OBJT_DEFAULT && 2255 object->type != OBJT_SWAP && 2256 object->type != OBJT_VNODE) 2257 run_ext = 0; 2258 else if ((m->flags & PG_CACHED) != 0 || 2259 m != vm_page_lookup(object, m->pindex)) { 2260 /* 2261 * The page is cached or recently converted 2262 * from cached to free. 2263 */ 2264 #if VM_NRESERVLEVEL > 0 2265 if (level >= 0) { 2266 /* 2267 * The page is reserved. Extend the 2268 * current run by one page. 2269 */ 2270 run_ext = 1; 2271 } else 2272 #endif 2273 if ((order = m->order) < VM_NFREEORDER) { 2274 /* 2275 * The page is enqueued in the 2276 * physical memory allocator's cache/ 2277 * free page queues. Moreover, it is 2278 * the first page in a power-of-two- 2279 * sized run of contiguous cache/free 2280 * pages. Add these pages to the end 2281 * of the current run, and jump 2282 * ahead. 2283 */ 2284 run_ext = 1 << order; 2285 m_inc = 1 << order; 2286 } else 2287 run_ext = 0; 2288 #if VM_NRESERVLEVEL > 0 2289 } else if ((options & VPSC_NOSUPER) != 0 && 2290 (level = vm_reserv_level_iffullpop(m)) >= 0) { 2291 run_ext = 0; 2292 /* Advance to the end of the superpage. */ 2293 pa = VM_PAGE_TO_PHYS(m); 2294 m_inc = atop(roundup2(pa + 1, 2295 vm_reserv_size(level)) - pa); 2296 #endif 2297 } else if (object->memattr == VM_MEMATTR_DEFAULT && 2298 m->queue != PQ_NONE && !vm_page_busied(m)) { 2299 /* 2300 * The page is allocated but eligible for 2301 * relocation. Extend the current run by one 2302 * page. 2303 */ 2304 KASSERT(pmap_page_get_memattr(m) == 2305 VM_MEMATTR_DEFAULT, 2306 ("page %p has an unexpected memattr", m)); 2307 KASSERT((m->oflags & (VPO_SWAPINPROG | 2308 VPO_SWAPSLEEP | VPO_UNMANAGED)) == 0, 2309 ("page %p has unexpected oflags", m)); 2310 /* Don't care: VPO_NOSYNC. */ 2311 run_ext = 1; 2312 } else 2313 run_ext = 0; 2314 unlock: 2315 VM_OBJECT_RUNLOCK(object); 2316 #if VM_NRESERVLEVEL > 0 2317 } else if (level >= 0) { 2318 /* 2319 * The page is reserved but not yet allocated. In 2320 * other words, it is still cached or free. Extend 2321 * the current run by one page. 2322 */ 2323 run_ext = 1; 2324 #endif 2325 } else if ((order = m->order) < VM_NFREEORDER) { 2326 /* 2327 * The page is enqueued in the physical memory 2328 * allocator's cache/free page queues. Moreover, it 2329 * is the first page in a power-of-two-sized run of 2330 * contiguous cache/free pages. Add these pages to 2331 * the end of the current run, and jump ahead. 2332 */ 2333 run_ext = 1 << order; 2334 m_inc = 1 << order; 2335 } else { 2336 /* 2337 * Skip the page for one of the following reasons: (1) 2338 * It is enqueued in the physical memory allocator's 2339 * cache/free page queues. However, it is not the 2340 * first page in a run of contiguous cache/free pages. 2341 * (This case rarely occurs because the scan is 2342 * performed in ascending order.) (2) It is not 2343 * reserved, and it is transitioning from free to 2344 * allocated. (Conversely, the transition from 2345 * allocated to free for managed pages is blocked by 2346 * the page lock.) (3) It is allocated but not 2347 * contained by an object and not wired, e.g., 2348 * allocated by Xen's balloon driver. 2349 */ 2350 run_ext = 0; 2351 } 2352 2353 /* 2354 * Extend or reset the current run of pages. 2355 */ 2356 if (run_ext > 0) { 2357 if (run_len == 0) 2358 m_run = m; 2359 run_len += run_ext; 2360 } else { 2361 if (run_len > 0) { 2362 m_run = NULL; 2363 run_len = 0; 2364 } 2365 } 2366 } 2367 if (m_mtx != NULL) 2368 mtx_unlock(m_mtx); 2369 if (run_len >= npages) 2370 return (m_run); 2371 return (NULL); 2372 } 2373 2374 /* 2375 * vm_page_reclaim_run: 2376 * 2377 * Try to relocate each of the allocated virtual pages within the 2378 * specified run of physical pages to a new physical address. Free the 2379 * physical pages underlying the relocated virtual pages. A virtual page 2380 * is relocatable if and only if it could be laundered or reclaimed by 2381 * the page daemon. Whenever possible, a virtual page is relocated to a 2382 * physical address above "high". 2383 * 2384 * Returns 0 if every physical page within the run was already free or 2385 * just freed by a successful relocation. Otherwise, returns a non-zero 2386 * value indicating why the last attempt to relocate a virtual page was 2387 * unsuccessful. 2388 * 2389 * "req_class" must be an allocation class. 2390 */ 2391 static int 2392 vm_page_reclaim_run(int req_class, u_long npages, vm_page_t m_run, 2393 vm_paddr_t high) 2394 { 2395 struct mtx *m_mtx, *new_mtx; 2396 struct spglist free; 2397 vm_object_t object; 2398 vm_paddr_t pa; 2399 vm_page_t m, m_end, m_new; 2400 int error, order, req; 2401 2402 KASSERT((req_class & VM_ALLOC_CLASS_MASK) == req_class, 2403 ("req_class is not an allocation class")); 2404 SLIST_INIT(&free); 2405 error = 0; 2406 m = m_run; 2407 m_end = m_run + npages; 2408 m_mtx = NULL; 2409 for (; error == 0 && m < m_end; m++) { 2410 KASSERT((m->flags & (PG_FICTITIOUS | PG_MARKER)) == 0, 2411 ("page %p is PG_FICTITIOUS or PG_MARKER", m)); 2412 2413 /* 2414 * Avoid releasing and reacquiring the same page lock. 2415 */ 2416 new_mtx = vm_page_lockptr(m); 2417 if (m_mtx != new_mtx) { 2418 if (m_mtx != NULL) 2419 mtx_unlock(m_mtx); 2420 m_mtx = new_mtx; 2421 mtx_lock(m_mtx); 2422 } 2423 retry: 2424 if (m->wire_count != 0 || m->hold_count != 0) 2425 error = EBUSY; 2426 else if ((object = m->object) != NULL) { 2427 /* 2428 * The page is relocated if and only if it could be 2429 * laundered or reclaimed by the page daemon. 2430 */ 2431 if (!VM_OBJECT_TRYWLOCK(object)) { 2432 mtx_unlock(m_mtx); 2433 VM_OBJECT_WLOCK(object); 2434 mtx_lock(m_mtx); 2435 if (m->object != object) { 2436 /* 2437 * The page may have been freed. 2438 */ 2439 VM_OBJECT_WUNLOCK(object); 2440 goto retry; 2441 } else if (m->wire_count != 0 || 2442 m->hold_count != 0) { 2443 error = EBUSY; 2444 goto unlock; 2445 } 2446 } 2447 KASSERT((m->flags & PG_UNHOLDFREE) == 0, 2448 ("page %p is PG_UNHOLDFREE", m)); 2449 /* Don't care: PG_NODUMP, PG_WINATCFLS, PG_ZERO. */ 2450 if (object->type != OBJT_DEFAULT && 2451 object->type != OBJT_SWAP && 2452 object->type != OBJT_VNODE) 2453 error = EINVAL; 2454 else if ((m->flags & PG_CACHED) != 0 || 2455 m != vm_page_lookup(object, m->pindex)) { 2456 /* 2457 * The page is cached or recently converted 2458 * from cached to free. 2459 */ 2460 VM_OBJECT_WUNLOCK(object); 2461 goto cached; 2462 } else if (object->memattr != VM_MEMATTR_DEFAULT) 2463 error = EINVAL; 2464 else if (m->queue != PQ_NONE && !vm_page_busied(m)) { 2465 KASSERT(pmap_page_get_memattr(m) == 2466 VM_MEMATTR_DEFAULT, 2467 ("page %p has an unexpected memattr", m)); 2468 KASSERT((m->oflags & (VPO_SWAPINPROG | 2469 VPO_SWAPSLEEP | VPO_UNMANAGED)) == 0, 2470 ("page %p has unexpected oflags", m)); 2471 /* Don't care: VPO_NOSYNC. */ 2472 if (m->valid != 0) { 2473 /* 2474 * First, try to allocate a new page 2475 * that is above "high". Failing 2476 * that, try to allocate a new page 2477 * that is below "m_run". Allocate 2478 * the new page between the end of 2479 * "m_run" and "high" only as a last 2480 * resort. 2481 */ 2482 req = req_class | VM_ALLOC_NOOBJ; 2483 if ((m->flags & PG_NODUMP) != 0) 2484 req |= VM_ALLOC_NODUMP; 2485 if (trunc_page(high) != 2486 ~(vm_paddr_t)PAGE_MASK) { 2487 m_new = vm_page_alloc_contig( 2488 NULL, 0, req, 1, 2489 round_page(high), 2490 ~(vm_paddr_t)0, 2491 PAGE_SIZE, 0, 2492 VM_MEMATTR_DEFAULT); 2493 } else 2494 m_new = NULL; 2495 if (m_new == NULL) { 2496 pa = VM_PAGE_TO_PHYS(m_run); 2497 m_new = vm_page_alloc_contig( 2498 NULL, 0, req, 1, 2499 0, pa - 1, PAGE_SIZE, 0, 2500 VM_MEMATTR_DEFAULT); 2501 } 2502 if (m_new == NULL) { 2503 pa += ptoa(npages); 2504 m_new = vm_page_alloc_contig( 2505 NULL, 0, req, 1, 2506 pa, high, PAGE_SIZE, 0, 2507 VM_MEMATTR_DEFAULT); 2508 } 2509 if (m_new == NULL) { 2510 error = ENOMEM; 2511 goto unlock; 2512 } 2513 KASSERT(m_new->wire_count == 0, 2514 ("page %p is wired", m)); 2515 2516 /* 2517 * Replace "m" with the new page. For 2518 * vm_page_replace(), "m" must be busy 2519 * and dequeued. Finally, change "m" 2520 * as if vm_page_free() was called. 2521 */ 2522 if (object->ref_count != 0) 2523 pmap_remove_all(m); 2524 m_new->aflags = m->aflags; 2525 KASSERT(m_new->oflags == VPO_UNMANAGED, 2526 ("page %p is managed", m)); 2527 m_new->oflags = m->oflags & VPO_NOSYNC; 2528 pmap_copy_page(m, m_new); 2529 m_new->valid = m->valid; 2530 m_new->dirty = m->dirty; 2531 m->flags &= ~PG_ZERO; 2532 vm_page_xbusy(m); 2533 vm_page_remque(m); 2534 vm_page_replace_checked(m_new, object, 2535 m->pindex, m); 2536 m->valid = 0; 2537 vm_page_undirty(m); 2538 2539 /* 2540 * The new page must be deactivated 2541 * before the object is unlocked. 2542 */ 2543 new_mtx = vm_page_lockptr(m_new); 2544 if (m_mtx != new_mtx) { 2545 mtx_unlock(m_mtx); 2546 m_mtx = new_mtx; 2547 mtx_lock(m_mtx); 2548 } 2549 vm_page_deactivate(m_new); 2550 } else { 2551 m->flags &= ~PG_ZERO; 2552 vm_page_remque(m); 2553 vm_page_remove(m); 2554 KASSERT(m->dirty == 0, 2555 ("page %p is dirty", m)); 2556 } 2557 SLIST_INSERT_HEAD(&free, m, plinks.s.ss); 2558 } else 2559 error = EBUSY; 2560 unlock: 2561 VM_OBJECT_WUNLOCK(object); 2562 } else { 2563 cached: 2564 mtx_lock(&vm_page_queue_free_mtx); 2565 order = m->order; 2566 if (order < VM_NFREEORDER) { 2567 /* 2568 * The page is enqueued in the physical memory 2569 * allocator's cache/free page queues. 2570 * Moreover, it is the first page in a power- 2571 * of-two-sized run of contiguous cache/free 2572 * pages. Jump ahead to the last page within 2573 * that run, and continue from there. 2574 */ 2575 m += (1 << order) - 1; 2576 } 2577 #if VM_NRESERVLEVEL > 0 2578 else if (vm_reserv_is_page_free(m)) 2579 order = 0; 2580 #endif 2581 mtx_unlock(&vm_page_queue_free_mtx); 2582 if (order == VM_NFREEORDER) 2583 error = EINVAL; 2584 } 2585 } 2586 if (m_mtx != NULL) 2587 mtx_unlock(m_mtx); 2588 if ((m = SLIST_FIRST(&free)) != NULL) { 2589 mtx_lock(&vm_page_queue_free_mtx); 2590 do { 2591 SLIST_REMOVE_HEAD(&free, plinks.s.ss); 2592 vm_phys_freecnt_adj(m, 1); 2593 #if VM_NRESERVLEVEL > 0 2594 if (!vm_reserv_free_page(m)) 2595 #else 2596 if (true) 2597 #endif 2598 vm_phys_free_pages(m, 0); 2599 } while ((m = SLIST_FIRST(&free)) != NULL); 2600 vm_page_zero_idle_wakeup(); 2601 vm_page_free_wakeup(); 2602 mtx_unlock(&vm_page_queue_free_mtx); 2603 } 2604 return (error); 2605 } 2606 2607 #define NRUNS 16 2608 2609 CTASSERT(powerof2(NRUNS)); 2610 2611 #define RUN_INDEX(count) ((count) & (NRUNS - 1)) 2612 2613 #define MIN_RECLAIM 8 2614 2615 /* 2616 * vm_page_reclaim_contig: 2617 * 2618 * Reclaim allocated, contiguous physical memory satisfying the specified 2619 * conditions by relocating the virtual pages using that physical memory. 2620 * Returns true if reclamation is successful and false otherwise. Since 2621 * relocation requires the allocation of physical pages, reclamation may 2622 * fail due to a shortage of cache/free pages. When reclamation fails, 2623 * callers are expected to perform VM_WAIT before retrying a failed 2624 * allocation operation, e.g., vm_page_alloc_contig(). 2625 * 2626 * The caller must always specify an allocation class through "req". 2627 * 2628 * allocation classes: 2629 * VM_ALLOC_NORMAL normal process request 2630 * VM_ALLOC_SYSTEM system *really* needs a page 2631 * VM_ALLOC_INTERRUPT interrupt time request 2632 * 2633 * The optional allocation flags are ignored. 2634 * 2635 * "npages" must be greater than zero. Both "alignment" and "boundary" 2636 * must be a power of two. 2637 */ 2638 bool 2639 vm_page_reclaim_contig(int req, u_long npages, vm_paddr_t low, vm_paddr_t high, 2640 u_long alignment, vm_paddr_t boundary) 2641 { 2642 vm_paddr_t curr_low; 2643 vm_page_t m_run, m_runs[NRUNS]; 2644 u_long count, reclaimed; 2645 int error, i, options, req_class; 2646 2647 KASSERT(npages > 0, ("npages is 0")); 2648 KASSERT(powerof2(alignment), ("alignment is not a power of 2")); 2649 KASSERT(powerof2(boundary), ("boundary is not a power of 2")); 2650 req_class = req & VM_ALLOC_CLASS_MASK; 2651 2652 /* 2653 * The page daemon is allowed to dig deeper into the free page list. 2654 */ 2655 if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT) 2656 req_class = VM_ALLOC_SYSTEM; 2657 2658 /* 2659 * Return if the number of cached and free pages cannot satisfy the 2660 * requested allocation. 2661 */ 2662 count = vm_cnt.v_free_count + vm_cnt.v_cache_count; 2663 if (count < npages + vm_cnt.v_free_reserved || (count < npages + 2664 vm_cnt.v_interrupt_free_min && req_class == VM_ALLOC_SYSTEM) || 2665 (count < npages && req_class == VM_ALLOC_INTERRUPT)) 2666 return (false); 2667 2668 /* 2669 * Scan up to three times, relaxing the restrictions ("options") on 2670 * the reclamation of reservations and superpages each time. 2671 */ 2672 for (options = VPSC_NORESERV;;) { 2673 /* 2674 * Find the highest runs that satisfy the given constraints 2675 * and restrictions, and record them in "m_runs". 2676 */ 2677 curr_low = low; 2678 count = 0; 2679 for (;;) { 2680 m_run = vm_phys_scan_contig(npages, curr_low, high, 2681 alignment, boundary, options); 2682 if (m_run == NULL) 2683 break; 2684 curr_low = VM_PAGE_TO_PHYS(m_run) + ptoa(npages); 2685 m_runs[RUN_INDEX(count)] = m_run; 2686 count++; 2687 } 2688 2689 /* 2690 * Reclaim the highest runs in LIFO (descending) order until 2691 * the number of reclaimed pages, "reclaimed", is at least 2692 * MIN_RECLAIM. Reset "reclaimed" each time because each 2693 * reclamation is idempotent, and runs will (likely) recur 2694 * from one scan to the next as restrictions are relaxed. 2695 */ 2696 reclaimed = 0; 2697 for (i = 0; count > 0 && i < NRUNS; i++) { 2698 count--; 2699 m_run = m_runs[RUN_INDEX(count)]; 2700 error = vm_page_reclaim_run(req_class, npages, m_run, 2701 high); 2702 if (error == 0) { 2703 reclaimed += npages; 2704 if (reclaimed >= MIN_RECLAIM) 2705 return (true); 2706 } 2707 } 2708 2709 /* 2710 * Either relax the restrictions on the next scan or return if 2711 * the last scan had no restrictions. 2712 */ 2713 if (options == VPSC_NORESERV) 2714 options = VPSC_NOSUPER; 2715 else if (options == VPSC_NOSUPER) 2716 options = VPSC_ANY; 2717 else if (options == VPSC_ANY) 2718 return (reclaimed != 0); 2719 } 2720 } 2721 2722 /* 2723 * vm_wait: (also see VM_WAIT macro) 2724 * 2725 * Sleep until free pages are available for allocation. 2726 * - Called in various places before memory allocations. 2727 */ 2728 void 2729 vm_wait(void) 2730 { 2731 2732 mtx_lock(&vm_page_queue_free_mtx); 2733 if (curproc == pageproc) { 2734 vm_pageout_pages_needed = 1; 2735 msleep(&vm_pageout_pages_needed, &vm_page_queue_free_mtx, 2736 PDROP | PSWP, "VMWait", 0); 2737 } else { 2738 if (!vm_pageout_wanted) { 2739 vm_pageout_wanted = true; 2740 wakeup(&vm_pageout_wanted); 2741 } 2742 vm_pages_needed = true; 2743 msleep(&vm_cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PVM, 2744 "vmwait", 0); 2745 } 2746 } 2747 2748 /* 2749 * vm_waitpfault: (also see VM_WAITPFAULT macro) 2750 * 2751 * Sleep until free pages are available for allocation. 2752 * - Called only in vm_fault so that processes page faulting 2753 * can be easily tracked. 2754 * - Sleeps at a lower priority than vm_wait() so that vm_wait()ing 2755 * processes will be able to grab memory first. Do not change 2756 * this balance without careful testing first. 2757 */ 2758 void 2759 vm_waitpfault(void) 2760 { 2761 2762 mtx_lock(&vm_page_queue_free_mtx); 2763 if (!vm_pageout_wanted) { 2764 vm_pageout_wanted = true; 2765 wakeup(&vm_pageout_wanted); 2766 } 2767 vm_pages_needed = true; 2768 msleep(&vm_cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PUSER, 2769 "pfault", 0); 2770 } 2771 2772 struct vm_pagequeue * 2773 vm_page_pagequeue(vm_page_t m) 2774 { 2775 2776 return (&vm_phys_domain(m)->vmd_pagequeues[m->queue]); 2777 } 2778 2779 /* 2780 * vm_page_dequeue: 2781 * 2782 * Remove the given page from its current page queue. 2783 * 2784 * The page must be locked. 2785 */ 2786 void 2787 vm_page_dequeue(vm_page_t m) 2788 { 2789 struct vm_pagequeue *pq; 2790 2791 vm_page_assert_locked(m); 2792 KASSERT(m->queue < PQ_COUNT, ("vm_page_dequeue: page %p is not queued", 2793 m)); 2794 pq = vm_page_pagequeue(m); 2795 vm_pagequeue_lock(pq); 2796 m->queue = PQ_NONE; 2797 TAILQ_REMOVE(&pq->pq_pl, m, plinks.q); 2798 vm_pagequeue_cnt_dec(pq); 2799 vm_pagequeue_unlock(pq); 2800 } 2801 2802 /* 2803 * vm_page_dequeue_locked: 2804 * 2805 * Remove the given page from its current page queue. 2806 * 2807 * The page and page queue must be locked. 2808 */ 2809 void 2810 vm_page_dequeue_locked(vm_page_t m) 2811 { 2812 struct vm_pagequeue *pq; 2813 2814 vm_page_lock_assert(m, MA_OWNED); 2815 pq = vm_page_pagequeue(m); 2816 vm_pagequeue_assert_locked(pq); 2817 m->queue = PQ_NONE; 2818 TAILQ_REMOVE(&pq->pq_pl, m, plinks.q); 2819 vm_pagequeue_cnt_dec(pq); 2820 } 2821 2822 /* 2823 * vm_page_enqueue: 2824 * 2825 * Add the given page to the specified page queue. 2826 * 2827 * The page must be locked. 2828 */ 2829 static void 2830 vm_page_enqueue(uint8_t queue, vm_page_t m) 2831 { 2832 struct vm_pagequeue *pq; 2833 2834 vm_page_lock_assert(m, MA_OWNED); 2835 KASSERT(queue < PQ_COUNT, 2836 ("vm_page_enqueue: invalid queue %u request for page %p", 2837 queue, m)); 2838 pq = &vm_phys_domain(m)->vmd_pagequeues[queue]; 2839 vm_pagequeue_lock(pq); 2840 m->queue = queue; 2841 TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q); 2842 vm_pagequeue_cnt_inc(pq); 2843 vm_pagequeue_unlock(pq); 2844 } 2845 2846 /* 2847 * vm_page_requeue: 2848 * 2849 * Move the given page to the tail of its current page queue. 2850 * 2851 * The page must be locked. 2852 */ 2853 void 2854 vm_page_requeue(vm_page_t m) 2855 { 2856 struct vm_pagequeue *pq; 2857 2858 vm_page_lock_assert(m, MA_OWNED); 2859 KASSERT(m->queue != PQ_NONE, 2860 ("vm_page_requeue: page %p is not queued", m)); 2861 pq = vm_page_pagequeue(m); 2862 vm_pagequeue_lock(pq); 2863 TAILQ_REMOVE(&pq->pq_pl, m, plinks.q); 2864 TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q); 2865 vm_pagequeue_unlock(pq); 2866 } 2867 2868 /* 2869 * vm_page_requeue_locked: 2870 * 2871 * Move the given page to the tail of its current page queue. 2872 * 2873 * The page queue must be locked. 2874 */ 2875 void 2876 vm_page_requeue_locked(vm_page_t m) 2877 { 2878 struct vm_pagequeue *pq; 2879 2880 KASSERT(m->queue != PQ_NONE, 2881 ("vm_page_requeue_locked: page %p is not queued", m)); 2882 pq = vm_page_pagequeue(m); 2883 vm_pagequeue_assert_locked(pq); 2884 TAILQ_REMOVE(&pq->pq_pl, m, plinks.q); 2885 TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q); 2886 } 2887 2888 /* 2889 * vm_page_activate: 2890 * 2891 * Put the specified page on the active list (if appropriate). 2892 * Ensure that act_count is at least ACT_INIT but do not otherwise 2893 * mess with it. 2894 * 2895 * The page must be locked. 2896 */ 2897 void 2898 vm_page_activate(vm_page_t m) 2899 { 2900 int queue; 2901 2902 vm_page_lock_assert(m, MA_OWNED); 2903 if ((queue = m->queue) != PQ_ACTIVE) { 2904 if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) { 2905 if (m->act_count < ACT_INIT) 2906 m->act_count = ACT_INIT; 2907 if (queue != PQ_NONE) 2908 vm_page_dequeue(m); 2909 vm_page_enqueue(PQ_ACTIVE, m); 2910 } else 2911 KASSERT(queue == PQ_NONE, 2912 ("vm_page_activate: wired page %p is queued", m)); 2913 } else { 2914 if (m->act_count < ACT_INIT) 2915 m->act_count = ACT_INIT; 2916 } 2917 } 2918 2919 /* 2920 * vm_page_free_wakeup: 2921 * 2922 * Helper routine for vm_page_free_toq() and vm_page_cache(). This 2923 * routine is called when a page has been added to the cache or free 2924 * queues. 2925 * 2926 * The page queues must be locked. 2927 */ 2928 static inline void 2929 vm_page_free_wakeup(void) 2930 { 2931 2932 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 2933 /* 2934 * if pageout daemon needs pages, then tell it that there are 2935 * some free. 2936 */ 2937 if (vm_pageout_pages_needed && 2938 vm_cnt.v_cache_count + vm_cnt.v_free_count >= vm_cnt.v_pageout_free_min) { 2939 wakeup(&vm_pageout_pages_needed); 2940 vm_pageout_pages_needed = 0; 2941 } 2942 /* 2943 * wakeup processes that are waiting on memory if we hit a 2944 * high water mark. And wakeup scheduler process if we have 2945 * lots of memory. this process will swapin processes. 2946 */ 2947 if (vm_pages_needed && !vm_page_count_min()) { 2948 vm_pages_needed = false; 2949 wakeup(&vm_cnt.v_free_count); 2950 } 2951 } 2952 2953 /* 2954 * Turn a cached page into a free page, by changing its attributes. 2955 * Keep the statistics up-to-date. 2956 * 2957 * The free page queue must be locked. 2958 */ 2959 static void 2960 vm_page_cache_turn_free(vm_page_t m) 2961 { 2962 2963 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 2964 2965 m->object = NULL; 2966 m->valid = 0; 2967 KASSERT((m->flags & PG_CACHED) != 0, 2968 ("vm_page_cache_turn_free: page %p is not cached", m)); 2969 m->flags &= ~PG_CACHED; 2970 vm_cnt.v_cache_count--; 2971 vm_phys_freecnt_adj(m, 1); 2972 } 2973 2974 /* 2975 * vm_page_free_toq: 2976 * 2977 * Returns the given page to the free list, 2978 * disassociating it with any VM object. 2979 * 2980 * The object must be locked. The page must be locked if it is managed. 2981 */ 2982 void 2983 vm_page_free_toq(vm_page_t m) 2984 { 2985 2986 if ((m->oflags & VPO_UNMANAGED) == 0) { 2987 vm_page_lock_assert(m, MA_OWNED); 2988 KASSERT(!pmap_page_is_mapped(m), 2989 ("vm_page_free_toq: freeing mapped page %p", m)); 2990 } else 2991 KASSERT(m->queue == PQ_NONE, 2992 ("vm_page_free_toq: unmanaged page %p is queued", m)); 2993 PCPU_INC(cnt.v_tfree); 2994 2995 if (vm_page_sbusied(m)) 2996 panic("vm_page_free: freeing busy page %p", m); 2997 2998 /* 2999 * Unqueue, then remove page. Note that we cannot destroy 3000 * the page here because we do not want to call the pager's 3001 * callback routine until after we've put the page on the 3002 * appropriate free queue. 3003 */ 3004 vm_page_remque(m); 3005 vm_page_remove(m); 3006 3007 /* 3008 * If fictitious remove object association and 3009 * return, otherwise delay object association removal. 3010 */ 3011 if ((m->flags & PG_FICTITIOUS) != 0) { 3012 return; 3013 } 3014 3015 m->valid = 0; 3016 vm_page_undirty(m); 3017 3018 if (m->wire_count != 0) 3019 panic("vm_page_free: freeing wired page %p", m); 3020 if (m->hold_count != 0) { 3021 m->flags &= ~PG_ZERO; 3022 KASSERT((m->flags & PG_UNHOLDFREE) == 0, 3023 ("vm_page_free: freeing PG_UNHOLDFREE page %p", m)); 3024 m->flags |= PG_UNHOLDFREE; 3025 } else { 3026 /* 3027 * Restore the default memory attribute to the page. 3028 */ 3029 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT) 3030 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT); 3031 3032 /* 3033 * Insert the page into the physical memory allocator's 3034 * cache/free page queues. 3035 */ 3036 mtx_lock(&vm_page_queue_free_mtx); 3037 vm_phys_freecnt_adj(m, 1); 3038 #if VM_NRESERVLEVEL > 0 3039 if (!vm_reserv_free_page(m)) 3040 #else 3041 if (TRUE) 3042 #endif 3043 vm_phys_free_pages(m, 0); 3044 if ((m->flags & PG_ZERO) != 0) 3045 ++vm_page_zero_count; 3046 else 3047 vm_page_zero_idle_wakeup(); 3048 vm_page_free_wakeup(); 3049 mtx_unlock(&vm_page_queue_free_mtx); 3050 } 3051 } 3052 3053 /* 3054 * vm_page_wire: 3055 * 3056 * Mark this page as wired down by yet 3057 * another map, removing it from paging queues 3058 * as necessary. 3059 * 3060 * If the page is fictitious, then its wire count must remain one. 3061 * 3062 * The page must be locked. 3063 */ 3064 void 3065 vm_page_wire(vm_page_t m) 3066 { 3067 3068 /* 3069 * Only bump the wire statistics if the page is not already wired, 3070 * and only unqueue the page if it is on some queue (if it is unmanaged 3071 * it is already off the queues). 3072 */ 3073 vm_page_lock_assert(m, MA_OWNED); 3074 if ((m->flags & PG_FICTITIOUS) != 0) { 3075 KASSERT(m->wire_count == 1, 3076 ("vm_page_wire: fictitious page %p's wire count isn't one", 3077 m)); 3078 return; 3079 } 3080 if (m->wire_count == 0) { 3081 KASSERT((m->oflags & VPO_UNMANAGED) == 0 || 3082 m->queue == PQ_NONE, 3083 ("vm_page_wire: unmanaged page %p is queued", m)); 3084 vm_page_remque(m); 3085 atomic_add_int(&vm_cnt.v_wire_count, 1); 3086 } 3087 m->wire_count++; 3088 KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m)); 3089 } 3090 3091 /* 3092 * vm_page_unwire: 3093 * 3094 * Release one wiring of the specified page, potentially allowing it to be 3095 * paged out. Returns TRUE if the number of wirings transitions to zero and 3096 * FALSE otherwise. 3097 * 3098 * Only managed pages belonging to an object can be paged out. If the number 3099 * of wirings transitions to zero and the page is eligible for page out, then 3100 * the page is added to the specified paging queue (unless PQ_NONE is 3101 * specified). 3102 * 3103 * If a page is fictitious, then its wire count must always be one. 3104 * 3105 * A managed page must be locked. 3106 */ 3107 boolean_t 3108 vm_page_unwire(vm_page_t m, uint8_t queue) 3109 { 3110 3111 KASSERT(queue < PQ_COUNT || queue == PQ_NONE, 3112 ("vm_page_unwire: invalid queue %u request for page %p", 3113 queue, m)); 3114 if ((m->oflags & VPO_UNMANAGED) == 0) 3115 vm_page_assert_locked(m); 3116 if ((m->flags & PG_FICTITIOUS) != 0) { 3117 KASSERT(m->wire_count == 1, 3118 ("vm_page_unwire: fictitious page %p's wire count isn't one", m)); 3119 return (FALSE); 3120 } 3121 if (m->wire_count > 0) { 3122 m->wire_count--; 3123 if (m->wire_count == 0) { 3124 atomic_subtract_int(&vm_cnt.v_wire_count, 1); 3125 if ((m->oflags & VPO_UNMANAGED) == 0 && 3126 m->object != NULL && queue != PQ_NONE) { 3127 if (queue == PQ_INACTIVE) 3128 m->flags &= ~PG_WINATCFLS; 3129 vm_page_enqueue(queue, m); 3130 } 3131 return (TRUE); 3132 } else 3133 return (FALSE); 3134 } else 3135 panic("vm_page_unwire: page %p's wire count is zero", m); 3136 } 3137 3138 /* 3139 * Move the specified page to the inactive queue. 3140 * 3141 * Many pages placed on the inactive queue should actually go 3142 * into the cache, but it is difficult to figure out which. What 3143 * we do instead, if the inactive target is well met, is to put 3144 * clean pages at the head of the inactive queue instead of the tail. 3145 * This will cause them to be moved to the cache more quickly and 3146 * if not actively re-referenced, reclaimed more quickly. If we just 3147 * stick these pages at the end of the inactive queue, heavy filesystem 3148 * meta-data accesses can cause an unnecessary paging load on memory bound 3149 * processes. This optimization causes one-time-use metadata to be 3150 * reused more quickly. 3151 * 3152 * Normally noreuse is FALSE, resulting in LRU operation. noreuse is set 3153 * to TRUE if we want this page to be 'as if it were placed in the cache', 3154 * except without unmapping it from the process address space. In 3155 * practice this is implemented by inserting the page at the head of the 3156 * queue, using a marker page to guide FIFO insertion ordering. 3157 * 3158 * The page must be locked. 3159 */ 3160 static inline void 3161 _vm_page_deactivate(vm_page_t m, boolean_t noreuse) 3162 { 3163 struct vm_pagequeue *pq; 3164 int queue; 3165 3166 vm_page_assert_locked(m); 3167 3168 /* 3169 * Ignore if the page is already inactive, unless it is unlikely to be 3170 * reactivated. 3171 */ 3172 if ((queue = m->queue) == PQ_INACTIVE && !noreuse) 3173 return; 3174 if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) { 3175 pq = &vm_phys_domain(m)->vmd_pagequeues[PQ_INACTIVE]; 3176 /* Avoid multiple acquisitions of the inactive queue lock. */ 3177 if (queue == PQ_INACTIVE) { 3178 vm_pagequeue_lock(pq); 3179 vm_page_dequeue_locked(m); 3180 } else { 3181 if (queue != PQ_NONE) 3182 vm_page_dequeue(m); 3183 m->flags &= ~PG_WINATCFLS; 3184 vm_pagequeue_lock(pq); 3185 } 3186 m->queue = PQ_INACTIVE; 3187 if (noreuse) 3188 TAILQ_INSERT_BEFORE(&vm_phys_domain(m)->vmd_inacthead, 3189 m, plinks.q); 3190 else 3191 TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q); 3192 vm_pagequeue_cnt_inc(pq); 3193 vm_pagequeue_unlock(pq); 3194 } 3195 } 3196 3197 /* 3198 * Move the specified page to the inactive queue. 3199 * 3200 * The page must be locked. 3201 */ 3202 void 3203 vm_page_deactivate(vm_page_t m) 3204 { 3205 3206 _vm_page_deactivate(m, FALSE); 3207 } 3208 3209 /* 3210 * Move the specified page to the inactive queue with the expectation 3211 * that it is unlikely to be reused. 3212 * 3213 * The page must be locked. 3214 */ 3215 void 3216 vm_page_deactivate_noreuse(vm_page_t m) 3217 { 3218 3219 _vm_page_deactivate(m, TRUE); 3220 } 3221 3222 /* 3223 * vm_page_try_to_cache: 3224 * 3225 * Returns 0 on failure, 1 on success 3226 */ 3227 int 3228 vm_page_try_to_cache(vm_page_t m) 3229 { 3230 3231 vm_page_lock_assert(m, MA_OWNED); 3232 VM_OBJECT_ASSERT_WLOCKED(m->object); 3233 if (m->dirty || m->hold_count || m->wire_count || 3234 (m->oflags & VPO_UNMANAGED) != 0 || vm_page_busied(m)) 3235 return (0); 3236 pmap_remove_all(m); 3237 if (m->dirty) 3238 return (0); 3239 vm_page_cache(m); 3240 return (1); 3241 } 3242 3243 /* 3244 * vm_page_try_to_free() 3245 * 3246 * Attempt to free the page. If we cannot free it, we do nothing. 3247 * 1 is returned on success, 0 on failure. 3248 */ 3249 int 3250 vm_page_try_to_free(vm_page_t m) 3251 { 3252 3253 vm_page_lock_assert(m, MA_OWNED); 3254 if (m->object != NULL) 3255 VM_OBJECT_ASSERT_WLOCKED(m->object); 3256 if (m->dirty || m->hold_count || m->wire_count || 3257 (m->oflags & VPO_UNMANAGED) != 0 || vm_page_busied(m)) 3258 return (0); 3259 pmap_remove_all(m); 3260 if (m->dirty) 3261 return (0); 3262 vm_page_free(m); 3263 return (1); 3264 } 3265 3266 /* 3267 * vm_page_cache 3268 * 3269 * Put the specified page onto the page cache queue (if appropriate). 3270 * 3271 * The object and page must be locked. 3272 */ 3273 void 3274 vm_page_cache(vm_page_t m) 3275 { 3276 vm_object_t object; 3277 boolean_t cache_was_empty; 3278 3279 vm_page_lock_assert(m, MA_OWNED); 3280 object = m->object; 3281 VM_OBJECT_ASSERT_WLOCKED(object); 3282 if (vm_page_busied(m) || (m->oflags & VPO_UNMANAGED) || 3283 m->hold_count || m->wire_count) 3284 panic("vm_page_cache: attempting to cache busy page"); 3285 KASSERT(!pmap_page_is_mapped(m), 3286 ("vm_page_cache: page %p is mapped", m)); 3287 KASSERT(m->dirty == 0, ("vm_page_cache: page %p is dirty", m)); 3288 if (m->valid == 0 || object->type == OBJT_DEFAULT || 3289 (object->type == OBJT_SWAP && 3290 !vm_pager_has_page(object, m->pindex, NULL, NULL))) { 3291 /* 3292 * Hypothesis: A cache-eligible page belonging to a 3293 * default object or swap object but without a backing 3294 * store must be zero filled. 3295 */ 3296 vm_page_free(m); 3297 return; 3298 } 3299 KASSERT((m->flags & PG_CACHED) == 0, 3300 ("vm_page_cache: page %p is already cached", m)); 3301 3302 /* 3303 * Remove the page from the paging queues. 3304 */ 3305 vm_page_remque(m); 3306 3307 /* 3308 * Remove the page from the object's collection of resident 3309 * pages. 3310 */ 3311 vm_radix_remove(&object->rtree, m->pindex); 3312 TAILQ_REMOVE(&object->memq, m, listq); 3313 object->resident_page_count--; 3314 3315 /* 3316 * Restore the default memory attribute to the page. 3317 */ 3318 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT) 3319 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT); 3320 3321 /* 3322 * Insert the page into the object's collection of cached pages 3323 * and the physical memory allocator's cache/free page queues. 3324 */ 3325 m->flags &= ~PG_ZERO; 3326 mtx_lock(&vm_page_queue_free_mtx); 3327 cache_was_empty = vm_radix_is_empty(&object->cache); 3328 if (vm_radix_insert(&object->cache, m)) { 3329 mtx_unlock(&vm_page_queue_free_mtx); 3330 if (object->type == OBJT_VNODE && 3331 object->resident_page_count == 0) 3332 vdrop(object->handle); 3333 m->object = NULL; 3334 vm_page_free(m); 3335 return; 3336 } 3337 3338 /* 3339 * The above call to vm_radix_insert() could reclaim the one pre- 3340 * existing cached page from this object, resulting in a call to 3341 * vdrop(). 3342 */ 3343 if (!cache_was_empty) 3344 cache_was_empty = vm_radix_is_singleton(&object->cache); 3345 3346 m->flags |= PG_CACHED; 3347 vm_cnt.v_cache_count++; 3348 PCPU_INC(cnt.v_tcached); 3349 #if VM_NRESERVLEVEL > 0 3350 if (!vm_reserv_free_page(m)) { 3351 #else 3352 if (TRUE) { 3353 #endif 3354 vm_phys_free_pages(m, 0); 3355 } 3356 vm_page_free_wakeup(); 3357 mtx_unlock(&vm_page_queue_free_mtx); 3358 3359 /* 3360 * Increment the vnode's hold count if this is the object's only 3361 * cached page. Decrement the vnode's hold count if this was 3362 * the object's only resident page. 3363 */ 3364 if (object->type == OBJT_VNODE) { 3365 if (cache_was_empty && object->resident_page_count != 0) 3366 vhold(object->handle); 3367 else if (!cache_was_empty && object->resident_page_count == 0) 3368 vdrop(object->handle); 3369 } 3370 } 3371 3372 /* 3373 * vm_page_advise 3374 * 3375 * Deactivate or do nothing, as appropriate. 3376 * 3377 * The object and page must be locked. 3378 */ 3379 void 3380 vm_page_advise(vm_page_t m, int advice) 3381 { 3382 3383 vm_page_assert_locked(m); 3384 VM_OBJECT_ASSERT_WLOCKED(m->object); 3385 if (advice == MADV_FREE) 3386 /* 3387 * Mark the page clean. This will allow the page to be freed 3388 * up by the system. However, such pages are often reused 3389 * quickly by malloc() so we do not do anything that would 3390 * cause a page fault if we can help it. 3391 * 3392 * Specifically, we do not try to actually free the page now 3393 * nor do we try to put it in the cache (which would cause a 3394 * page fault on reuse). 3395 * 3396 * But we do make the page as freeable as we can without 3397 * actually taking the step of unmapping it. 3398 */ 3399 vm_page_undirty(m); 3400 else if (advice != MADV_DONTNEED) 3401 return; 3402 3403 /* 3404 * Clear any references to the page. Otherwise, the page daemon will 3405 * immediately reactivate the page. 3406 */ 3407 vm_page_aflag_clear(m, PGA_REFERENCED); 3408 3409 if (advice != MADV_FREE && m->dirty == 0 && pmap_is_modified(m)) 3410 vm_page_dirty(m); 3411 3412 /* 3413 * Place clean pages near the head of the inactive queue rather than 3414 * the tail, thus defeating the queue's LRU operation and ensuring that 3415 * the page will be reused quickly. Dirty pages are given a chance to 3416 * cycle once through the inactive queue before becoming eligible for 3417 * laundering. 3418 */ 3419 _vm_page_deactivate(m, m->dirty == 0); 3420 } 3421 3422 /* 3423 * Grab a page, waiting until we are waken up due to the page 3424 * changing state. We keep on waiting, if the page continues 3425 * to be in the object. If the page doesn't exist, first allocate it 3426 * and then conditionally zero it. 3427 * 3428 * This routine may sleep. 3429 * 3430 * The object must be locked on entry. The lock will, however, be released 3431 * and reacquired if the routine sleeps. 3432 */ 3433 vm_page_t 3434 vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags) 3435 { 3436 vm_page_t m; 3437 int sleep; 3438 3439 VM_OBJECT_ASSERT_WLOCKED(object); 3440 KASSERT((allocflags & VM_ALLOC_SBUSY) == 0 || 3441 (allocflags & VM_ALLOC_IGN_SBUSY) != 0, 3442 ("vm_page_grab: VM_ALLOC_SBUSY/VM_ALLOC_IGN_SBUSY mismatch")); 3443 retrylookup: 3444 if ((m = vm_page_lookup(object, pindex)) != NULL) { 3445 sleep = (allocflags & VM_ALLOC_IGN_SBUSY) != 0 ? 3446 vm_page_xbusied(m) : vm_page_busied(m); 3447 if (sleep) { 3448 if ((allocflags & VM_ALLOC_NOWAIT) != 0) 3449 return (NULL); 3450 /* 3451 * Reference the page before unlocking and 3452 * sleeping so that the page daemon is less 3453 * likely to reclaim it. 3454 */ 3455 vm_page_aflag_set(m, PGA_REFERENCED); 3456 vm_page_lock(m); 3457 VM_OBJECT_WUNLOCK(object); 3458 vm_page_busy_sleep(m, "pgrbwt"); 3459 VM_OBJECT_WLOCK(object); 3460 goto retrylookup; 3461 } else { 3462 if ((allocflags & VM_ALLOC_WIRED) != 0) { 3463 vm_page_lock(m); 3464 vm_page_wire(m); 3465 vm_page_unlock(m); 3466 } 3467 if ((allocflags & 3468 (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) == 0) 3469 vm_page_xbusy(m); 3470 if ((allocflags & VM_ALLOC_SBUSY) != 0) 3471 vm_page_sbusy(m); 3472 return (m); 3473 } 3474 } 3475 m = vm_page_alloc(object, pindex, allocflags); 3476 if (m == NULL) { 3477 if ((allocflags & VM_ALLOC_NOWAIT) != 0) 3478 return (NULL); 3479 VM_OBJECT_WUNLOCK(object); 3480 VM_WAIT; 3481 VM_OBJECT_WLOCK(object); 3482 goto retrylookup; 3483 } else if (m->valid != 0) 3484 return (m); 3485 if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0) 3486 pmap_zero_page(m); 3487 return (m); 3488 } 3489 3490 /* 3491 * Mapping function for valid or dirty bits in a page. 3492 * 3493 * Inputs are required to range within a page. 3494 */ 3495 vm_page_bits_t 3496 vm_page_bits(int base, int size) 3497 { 3498 int first_bit; 3499 int last_bit; 3500 3501 KASSERT( 3502 base + size <= PAGE_SIZE, 3503 ("vm_page_bits: illegal base/size %d/%d", base, size) 3504 ); 3505 3506 if (size == 0) /* handle degenerate case */ 3507 return (0); 3508 3509 first_bit = base >> DEV_BSHIFT; 3510 last_bit = (base + size - 1) >> DEV_BSHIFT; 3511 3512 return (((vm_page_bits_t)2 << last_bit) - 3513 ((vm_page_bits_t)1 << first_bit)); 3514 } 3515 3516 /* 3517 * vm_page_set_valid_range: 3518 * 3519 * Sets portions of a page valid. The arguments are expected 3520 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive 3521 * of any partial chunks touched by the range. The invalid portion of 3522 * such chunks will be zeroed. 3523 * 3524 * (base + size) must be less then or equal to PAGE_SIZE. 3525 */ 3526 void 3527 vm_page_set_valid_range(vm_page_t m, int base, int size) 3528 { 3529 int endoff, frag; 3530 3531 VM_OBJECT_ASSERT_WLOCKED(m->object); 3532 if (size == 0) /* handle degenerate case */ 3533 return; 3534 3535 /* 3536 * If the base is not DEV_BSIZE aligned and the valid 3537 * bit is clear, we have to zero out a portion of the 3538 * first block. 3539 */ 3540 if ((frag = rounddown2(base, DEV_BSIZE)) != base && 3541 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0) 3542 pmap_zero_page_area(m, frag, base - frag); 3543 3544 /* 3545 * If the ending offset is not DEV_BSIZE aligned and the 3546 * valid bit is clear, we have to zero out a portion of 3547 * the last block. 3548 */ 3549 endoff = base + size; 3550 if ((frag = rounddown2(endoff, DEV_BSIZE)) != endoff && 3551 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0) 3552 pmap_zero_page_area(m, endoff, 3553 DEV_BSIZE - (endoff & (DEV_BSIZE - 1))); 3554 3555 /* 3556 * Assert that no previously invalid block that is now being validated 3557 * is already dirty. 3558 */ 3559 KASSERT((~m->valid & vm_page_bits(base, size) & m->dirty) == 0, 3560 ("vm_page_set_valid_range: page %p is dirty", m)); 3561 3562 /* 3563 * Set valid bits inclusive of any overlap. 3564 */ 3565 m->valid |= vm_page_bits(base, size); 3566 } 3567 3568 /* 3569 * Clear the given bits from the specified page's dirty field. 3570 */ 3571 static __inline void 3572 vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits) 3573 { 3574 uintptr_t addr; 3575 #if PAGE_SIZE < 16384 3576 int shift; 3577 #endif 3578 3579 /* 3580 * If the object is locked and the page is neither exclusive busy nor 3581 * write mapped, then the page's dirty field cannot possibly be 3582 * set by a concurrent pmap operation. 3583 */ 3584 VM_OBJECT_ASSERT_WLOCKED(m->object); 3585 if (!vm_page_xbusied(m) && !pmap_page_is_write_mapped(m)) 3586 m->dirty &= ~pagebits; 3587 else { 3588 /* 3589 * The pmap layer can call vm_page_dirty() without 3590 * holding a distinguished lock. The combination of 3591 * the object's lock and an atomic operation suffice 3592 * to guarantee consistency of the page dirty field. 3593 * 3594 * For PAGE_SIZE == 32768 case, compiler already 3595 * properly aligns the dirty field, so no forcible 3596 * alignment is needed. Only require existence of 3597 * atomic_clear_64 when page size is 32768. 3598 */ 3599 addr = (uintptr_t)&m->dirty; 3600 #if PAGE_SIZE == 32768 3601 atomic_clear_64((uint64_t *)addr, pagebits); 3602 #elif PAGE_SIZE == 16384 3603 atomic_clear_32((uint32_t *)addr, pagebits); 3604 #else /* PAGE_SIZE <= 8192 */ 3605 /* 3606 * Use a trick to perform a 32-bit atomic on the 3607 * containing aligned word, to not depend on the existence 3608 * of atomic_clear_{8, 16}. 3609 */ 3610 shift = addr & (sizeof(uint32_t) - 1); 3611 #if BYTE_ORDER == BIG_ENDIAN 3612 shift = (sizeof(uint32_t) - sizeof(m->dirty) - shift) * NBBY; 3613 #else 3614 shift *= NBBY; 3615 #endif 3616 addr &= ~(sizeof(uint32_t) - 1); 3617 atomic_clear_32((uint32_t *)addr, pagebits << shift); 3618 #endif /* PAGE_SIZE */ 3619 } 3620 } 3621 3622 /* 3623 * vm_page_set_validclean: 3624 * 3625 * Sets portions of a page valid and clean. The arguments are expected 3626 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive 3627 * of any partial chunks touched by the range. The invalid portion of 3628 * such chunks will be zero'd. 3629 * 3630 * (base + size) must be less then or equal to PAGE_SIZE. 3631 */ 3632 void 3633 vm_page_set_validclean(vm_page_t m, int base, int size) 3634 { 3635 vm_page_bits_t oldvalid, pagebits; 3636 int endoff, frag; 3637 3638 VM_OBJECT_ASSERT_WLOCKED(m->object); 3639 if (size == 0) /* handle degenerate case */ 3640 return; 3641 3642 /* 3643 * If the base is not DEV_BSIZE aligned and the valid 3644 * bit is clear, we have to zero out a portion of the 3645 * first block. 3646 */ 3647 if ((frag = rounddown2(base, DEV_BSIZE)) != base && 3648 (m->valid & ((vm_page_bits_t)1 << (base >> DEV_BSHIFT))) == 0) 3649 pmap_zero_page_area(m, frag, base - frag); 3650 3651 /* 3652 * If the ending offset is not DEV_BSIZE aligned and the 3653 * valid bit is clear, we have to zero out a portion of 3654 * the last block. 3655 */ 3656 endoff = base + size; 3657 if ((frag = rounddown2(endoff, DEV_BSIZE)) != endoff && 3658 (m->valid & ((vm_page_bits_t)1 << (endoff >> DEV_BSHIFT))) == 0) 3659 pmap_zero_page_area(m, endoff, 3660 DEV_BSIZE - (endoff & (DEV_BSIZE - 1))); 3661 3662 /* 3663 * Set valid, clear dirty bits. If validating the entire 3664 * page we can safely clear the pmap modify bit. We also 3665 * use this opportunity to clear the VPO_NOSYNC flag. If a process 3666 * takes a write fault on a MAP_NOSYNC memory area the flag will 3667 * be set again. 3668 * 3669 * We set valid bits inclusive of any overlap, but we can only 3670 * clear dirty bits for DEV_BSIZE chunks that are fully within 3671 * the range. 3672 */ 3673 oldvalid = m->valid; 3674 pagebits = vm_page_bits(base, size); 3675 m->valid |= pagebits; 3676 #if 0 /* NOT YET */ 3677 if ((frag = base & (DEV_BSIZE - 1)) != 0) { 3678 frag = DEV_BSIZE - frag; 3679 base += frag; 3680 size -= frag; 3681 if (size < 0) 3682 size = 0; 3683 } 3684 pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1)); 3685 #endif 3686 if (base == 0 && size == PAGE_SIZE) { 3687 /* 3688 * The page can only be modified within the pmap if it is 3689 * mapped, and it can only be mapped if it was previously 3690 * fully valid. 3691 */ 3692 if (oldvalid == VM_PAGE_BITS_ALL) 3693 /* 3694 * Perform the pmap_clear_modify() first. Otherwise, 3695 * a concurrent pmap operation, such as 3696 * pmap_protect(), could clear a modification in the 3697 * pmap and set the dirty field on the page before 3698 * pmap_clear_modify() had begun and after the dirty 3699 * field was cleared here. 3700 */ 3701 pmap_clear_modify(m); 3702 m->dirty = 0; 3703 m->oflags &= ~VPO_NOSYNC; 3704 } else if (oldvalid != VM_PAGE_BITS_ALL) 3705 m->dirty &= ~pagebits; 3706 else 3707 vm_page_clear_dirty_mask(m, pagebits); 3708 } 3709 3710 void 3711 vm_page_clear_dirty(vm_page_t m, int base, int size) 3712 { 3713 3714 vm_page_clear_dirty_mask(m, vm_page_bits(base, size)); 3715 } 3716 3717 /* 3718 * vm_page_set_invalid: 3719 * 3720 * Invalidates DEV_BSIZE'd chunks within a page. Both the 3721 * valid and dirty bits for the effected areas are cleared. 3722 */ 3723 void 3724 vm_page_set_invalid(vm_page_t m, int base, int size) 3725 { 3726 vm_page_bits_t bits; 3727 vm_object_t object; 3728 3729 object = m->object; 3730 VM_OBJECT_ASSERT_WLOCKED(object); 3731 if (object->type == OBJT_VNODE && base == 0 && IDX_TO_OFF(m->pindex) + 3732 size >= object->un_pager.vnp.vnp_size) 3733 bits = VM_PAGE_BITS_ALL; 3734 else 3735 bits = vm_page_bits(base, size); 3736 if (object->ref_count != 0 && m->valid == VM_PAGE_BITS_ALL && 3737 bits != 0) 3738 pmap_remove_all(m); 3739 KASSERT((bits == 0 && m->valid == VM_PAGE_BITS_ALL) || 3740 !pmap_page_is_mapped(m), 3741 ("vm_page_set_invalid: page %p is mapped", m)); 3742 m->valid &= ~bits; 3743 m->dirty &= ~bits; 3744 } 3745 3746 /* 3747 * vm_page_zero_invalid() 3748 * 3749 * The kernel assumes that the invalid portions of a page contain 3750 * garbage, but such pages can be mapped into memory by user code. 3751 * When this occurs, we must zero out the non-valid portions of the 3752 * page so user code sees what it expects. 3753 * 3754 * Pages are most often semi-valid when the end of a file is mapped 3755 * into memory and the file's size is not page aligned. 3756 */ 3757 void 3758 vm_page_zero_invalid(vm_page_t m, boolean_t setvalid) 3759 { 3760 int b; 3761 int i; 3762 3763 VM_OBJECT_ASSERT_WLOCKED(m->object); 3764 /* 3765 * Scan the valid bits looking for invalid sections that 3766 * must be zeroed. Invalid sub-DEV_BSIZE'd areas ( where the 3767 * valid bit may be set ) have already been zeroed by 3768 * vm_page_set_validclean(). 3769 */ 3770 for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) { 3771 if (i == (PAGE_SIZE / DEV_BSIZE) || 3772 (m->valid & ((vm_page_bits_t)1 << i))) { 3773 if (i > b) { 3774 pmap_zero_page_area(m, 3775 b << DEV_BSHIFT, (i - b) << DEV_BSHIFT); 3776 } 3777 b = i + 1; 3778 } 3779 } 3780 3781 /* 3782 * setvalid is TRUE when we can safely set the zero'd areas 3783 * as being valid. We can do this if there are no cache consistancy 3784 * issues. e.g. it is ok to do with UFS, but not ok to do with NFS. 3785 */ 3786 if (setvalid) 3787 m->valid = VM_PAGE_BITS_ALL; 3788 } 3789 3790 /* 3791 * vm_page_is_valid: 3792 * 3793 * Is (partial) page valid? Note that the case where size == 0 3794 * will return FALSE in the degenerate case where the page is 3795 * entirely invalid, and TRUE otherwise. 3796 */ 3797 int 3798 vm_page_is_valid(vm_page_t m, int base, int size) 3799 { 3800 vm_page_bits_t bits; 3801 3802 VM_OBJECT_ASSERT_LOCKED(m->object); 3803 bits = vm_page_bits(base, size); 3804 return (m->valid != 0 && (m->valid & bits) == bits); 3805 } 3806 3807 /* 3808 * vm_page_ps_is_valid: 3809 * 3810 * Returns TRUE if the entire (super)page is valid and FALSE otherwise. 3811 */ 3812 boolean_t 3813 vm_page_ps_is_valid(vm_page_t m) 3814 { 3815 int i, npages; 3816 3817 VM_OBJECT_ASSERT_LOCKED(m->object); 3818 npages = atop(pagesizes[m->psind]); 3819 3820 /* 3821 * The physically contiguous pages that make up a superpage, i.e., a 3822 * page with a page size index ("psind") greater than zero, will 3823 * occupy adjacent entries in vm_page_array[]. 3824 */ 3825 for (i = 0; i < npages; i++) { 3826 if (m[i].valid != VM_PAGE_BITS_ALL) 3827 return (FALSE); 3828 } 3829 return (TRUE); 3830 } 3831 3832 /* 3833 * Set the page's dirty bits if the page is modified. 3834 */ 3835 void 3836 vm_page_test_dirty(vm_page_t m) 3837 { 3838 3839 VM_OBJECT_ASSERT_WLOCKED(m->object); 3840 if (m->dirty != VM_PAGE_BITS_ALL && pmap_is_modified(m)) 3841 vm_page_dirty(m); 3842 } 3843 3844 void 3845 vm_page_lock_KBI(vm_page_t m, const char *file, int line) 3846 { 3847 3848 mtx_lock_flags_(vm_page_lockptr(m), 0, file, line); 3849 } 3850 3851 void 3852 vm_page_unlock_KBI(vm_page_t m, const char *file, int line) 3853 { 3854 3855 mtx_unlock_flags_(vm_page_lockptr(m), 0, file, line); 3856 } 3857 3858 int 3859 vm_page_trylock_KBI(vm_page_t m, const char *file, int line) 3860 { 3861 3862 return (mtx_trylock_flags_(vm_page_lockptr(m), 0, file, line)); 3863 } 3864 3865 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT) 3866 void 3867 vm_page_assert_locked_KBI(vm_page_t m, const char *file, int line) 3868 { 3869 3870 vm_page_lock_assert_KBI(m, MA_OWNED, file, line); 3871 } 3872 3873 void 3874 vm_page_lock_assert_KBI(vm_page_t m, int a, const char *file, int line) 3875 { 3876 3877 mtx_assert_(vm_page_lockptr(m), a, file, line); 3878 } 3879 #endif 3880 3881 #ifdef INVARIANTS 3882 void 3883 vm_page_object_lock_assert(vm_page_t m) 3884 { 3885 3886 /* 3887 * Certain of the page's fields may only be modified by the 3888 * holder of the containing object's lock or the exclusive busy. 3889 * holder. Unfortunately, the holder of the write busy is 3890 * not recorded, and thus cannot be checked here. 3891 */ 3892 if (m->object != NULL && !vm_page_xbusied(m)) 3893 VM_OBJECT_ASSERT_WLOCKED(m->object); 3894 } 3895 3896 void 3897 vm_page_assert_pga_writeable(vm_page_t m, uint8_t bits) 3898 { 3899 3900 if ((bits & PGA_WRITEABLE) == 0) 3901 return; 3902 3903 /* 3904 * The PGA_WRITEABLE flag can only be set if the page is 3905 * managed, is exclusively busied or the object is locked. 3906 * Currently, this flag is only set by pmap_enter(). 3907 */ 3908 KASSERT((m->oflags & VPO_UNMANAGED) == 0, 3909 ("PGA_WRITEABLE on unmanaged page")); 3910 if (!vm_page_xbusied(m)) 3911 VM_OBJECT_ASSERT_LOCKED(m->object); 3912 } 3913 #endif 3914 3915 #include "opt_ddb.h" 3916 #ifdef DDB 3917 #include <sys/kernel.h> 3918 3919 #include <ddb/ddb.h> 3920 3921 DB_SHOW_COMMAND(page, vm_page_print_page_info) 3922 { 3923 db_printf("vm_cnt.v_free_count: %d\n", vm_cnt.v_free_count); 3924 db_printf("vm_cnt.v_cache_count: %d\n", vm_cnt.v_cache_count); 3925 db_printf("vm_cnt.v_inactive_count: %d\n", vm_cnt.v_inactive_count); 3926 db_printf("vm_cnt.v_active_count: %d\n", vm_cnt.v_active_count); 3927 db_printf("vm_cnt.v_wire_count: %d\n", vm_cnt.v_wire_count); 3928 db_printf("vm_cnt.v_free_reserved: %d\n", vm_cnt.v_free_reserved); 3929 db_printf("vm_cnt.v_free_min: %d\n", vm_cnt.v_free_min); 3930 db_printf("vm_cnt.v_free_target: %d\n", vm_cnt.v_free_target); 3931 db_printf("vm_cnt.v_inactive_target: %d\n", vm_cnt.v_inactive_target); 3932 } 3933 3934 DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info) 3935 { 3936 int dom; 3937 3938 db_printf("pq_free %d pq_cache %d\n", 3939 vm_cnt.v_free_count, vm_cnt.v_cache_count); 3940 for (dom = 0; dom < vm_ndomains; dom++) { 3941 db_printf( 3942 "dom %d page_cnt %d free %d pq_act %d pq_inact %d pass %d\n", 3943 dom, 3944 vm_dom[dom].vmd_page_count, 3945 vm_dom[dom].vmd_free_count, 3946 vm_dom[dom].vmd_pagequeues[PQ_ACTIVE].pq_cnt, 3947 vm_dom[dom].vmd_pagequeues[PQ_INACTIVE].pq_cnt, 3948 vm_dom[dom].vmd_pass); 3949 } 3950 } 3951 3952 DB_SHOW_COMMAND(pginfo, vm_page_print_pginfo) 3953 { 3954 vm_page_t m; 3955 boolean_t phys; 3956 3957 if (!have_addr) { 3958 db_printf("show pginfo addr\n"); 3959 return; 3960 } 3961 3962 phys = strchr(modif, 'p') != NULL; 3963 if (phys) 3964 m = PHYS_TO_VM_PAGE(addr); 3965 else 3966 m = (vm_page_t)addr; 3967 db_printf( 3968 "page %p obj %p pidx 0x%jx phys 0x%jx q %d hold %d wire %d\n" 3969 " af 0x%x of 0x%x f 0x%x act %d busy %x valid 0x%x dirty 0x%x\n", 3970 m, m->object, (uintmax_t)m->pindex, (uintmax_t)m->phys_addr, 3971 m->queue, m->hold_count, m->wire_count, m->aflags, m->oflags, 3972 m->flags, m->act_count, m->busy_lock, m->valid, m->dirty); 3973 } 3974 #endif /* DDB */ 3975