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