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