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