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