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