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