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