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