1 /* 2 * Copyright (c) 2003-2019 The DragonFly Project. All rights reserved. 3 * Copyright (c) 1991 Regents of the University of California. 4 * 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 * This code is derived from software contributed to The DragonFly Project 10 * by Matthew Dillon <dillon@backplane.com> 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * from: @(#)vm_page.c 7.4 (Berkeley) 5/7/91 37 * $FreeBSD: src/sys/vm/vm_page.c,v 1.147.2.18 2002/03/10 05:03:19 alc Exp $ 38 */ 39 40 /* 41 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 42 * All rights reserved. 43 * 44 * Authors: Avadis Tevanian, Jr., Michael Wayne Young 45 * 46 * Permission to use, copy, modify and distribute this software and 47 * its documentation is hereby granted, provided that both the copyright 48 * notice and this permission notice appear in all copies of the 49 * software, derivative works or modified versions, and any portions 50 * thereof, and that both notices appear in supporting documentation. 51 * 52 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 53 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 54 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 55 * 56 * Carnegie Mellon requests users of this software to return to 57 * 58 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 59 * School of Computer Science 60 * Carnegie Mellon University 61 * Pittsburgh PA 15213-3890 62 * 63 * any improvements or extensions that they make and grant Carnegie the 64 * rights to redistribute these changes. 65 */ 66 /* 67 * Resident memory management module. The module manipulates 'VM pages'. 68 * A VM page is the core building block for memory management. 69 */ 70 71 #include <sys/param.h> 72 #include <sys/systm.h> 73 #include <sys/malloc.h> 74 #include <sys/proc.h> 75 #include <sys/vmmeter.h> 76 #include <sys/vnode.h> 77 #include <sys/kernel.h> 78 #include <sys/alist.h> 79 #include <sys/sysctl.h> 80 #include <sys/cpu_topology.h> 81 82 #include <vm/vm.h> 83 #include <vm/vm_param.h> 84 #include <sys/lock.h> 85 #include <vm/vm_kern.h> 86 #include <vm/pmap.h> 87 #include <vm/vm_map.h> 88 #include <vm/vm_object.h> 89 #include <vm/vm_page.h> 90 #include <vm/vm_pageout.h> 91 #include <vm/vm_pager.h> 92 #include <vm/vm_extern.h> 93 #include <vm/swap_pager.h> 94 95 #include <machine/inttypes.h> 96 #include <machine/md_var.h> 97 #include <machine/specialreg.h> 98 #include <machine/bus_dma.h> 99 100 #include <vm/vm_page2.h> 101 #include <sys/spinlock2.h> 102 103 struct vm_page_hash_elm { 104 vm_page_t m; 105 int ticks; 106 int unused01; 107 }; 108 109 #define VM_PAGE_HASH_SET 4 /* power of 2, set-assoc */ 110 #define VM_PAGE_HASH_MAX (16 * 1024 * 1024) /* power of 2, max size */ 111 112 /* 113 * SET - Minimum required set associative size, must be a power of 2. We 114 * want this to match or exceed the set-associativeness of the cpu, 115 * up to a reasonable limit (we will use 16). 116 */ 117 __read_mostly static int set_assoc_mask = 16 - 1; 118 119 static void vm_page_queue_init(void); 120 static void vm_page_free_wakeup(void); 121 static vm_page_t vm_page_select_cache(u_short pg_color); 122 static vm_page_t _vm_page_list_find_wide(int basequeue, int index, int *lastp); 123 static vm_page_t _vm_page_list_find2_wide(int bq1, int bq2, int index, 124 int *lastp1, int *lastp); 125 static void _vm_page_deactivate_locked(vm_page_t m, int athead); 126 static void vm_numa_add_topology_mem(cpu_node_t *cpup, int physid, long bytes); 127 128 /* 129 * Array of tailq lists 130 */ 131 struct vpgqueues vm_page_queues[PQ_COUNT]; 132 133 static volatile int vm_pages_waiting; 134 static struct alist vm_contig_alist; 135 static struct almeta vm_contig_ameta[ALIST_RECORDS_65536]; 136 static struct spinlock vm_contig_spin = SPINLOCK_INITIALIZER(&vm_contig_spin, "vm_contig_spin"); 137 138 __read_mostly static int vm_page_hash_vnode_only; 139 __read_mostly static int vm_page_hash_size; 140 __read_mostly static struct vm_page_hash_elm *vm_page_hash; 141 142 static u_long vm_dma_reserved = 0; 143 TUNABLE_ULONG("vm.dma_reserved", &vm_dma_reserved); 144 SYSCTL_ULONG(_vm, OID_AUTO, dma_reserved, CTLFLAG_RD, &vm_dma_reserved, 0, 145 "Memory reserved for DMA"); 146 SYSCTL_UINT(_vm, OID_AUTO, dma_free_pages, CTLFLAG_RD, 147 &vm_contig_alist.bl_free, 0, "Memory reserved for DMA"); 148 149 SYSCTL_INT(_vm, OID_AUTO, page_hash_vnode_only, CTLFLAG_RW, 150 &vm_page_hash_vnode_only, 0, "Only hash vnode pages"); 151 #if 0 152 static int vm_page_hash_debug; 153 SYSCTL_INT(_vm, OID_AUTO, page_hash_debug, CTLFLAG_RW, 154 &vm_page_hash_debug, 0, "Only hash vnode pages"); 155 #endif 156 157 static int vm_contig_verbose = 0; 158 TUNABLE_INT("vm.contig_verbose", &vm_contig_verbose); 159 160 RB_GENERATE2(vm_page_rb_tree, vm_page, rb_entry, rb_vm_page_compare, 161 vm_pindex_t, pindex); 162 163 static void 164 vm_page_queue_init(void) 165 { 166 int i; 167 168 for (i = 0; i < PQ_L2_SIZE; i++) 169 vm_page_queues[PQ_FREE+i].cnt_offset = 170 offsetof(struct vmstats, v_free_count); 171 for (i = 0; i < PQ_L2_SIZE; i++) 172 vm_page_queues[PQ_CACHE+i].cnt_offset = 173 offsetof(struct vmstats, v_cache_count); 174 for (i = 0; i < PQ_L2_SIZE; i++) 175 vm_page_queues[PQ_INACTIVE+i].cnt_offset = 176 offsetof(struct vmstats, v_inactive_count); 177 for (i = 0; i < PQ_L2_SIZE; i++) 178 vm_page_queues[PQ_ACTIVE+i].cnt_offset = 179 offsetof(struct vmstats, v_active_count); 180 for (i = 0; i < PQ_L2_SIZE; i++) 181 vm_page_queues[PQ_HOLD+i].cnt_offset = 182 offsetof(struct vmstats, v_active_count); 183 /* PQ_NONE has no queue */ 184 185 for (i = 0; i < PQ_COUNT; i++) { 186 vm_page_queues[i].lastq = -1; 187 TAILQ_INIT(&vm_page_queues[i].pl); 188 spin_init(&vm_page_queues[i].spin, "vm_page_queue_init"); 189 } 190 } 191 192 /* 193 * note: place in initialized data section? Is this necessary? 194 */ 195 vm_pindex_t first_page = 0; 196 vm_pindex_t vm_page_array_size = 0; 197 vm_page_t vm_page_array = NULL; 198 vm_paddr_t vm_low_phys_reserved; 199 200 /* 201 * (low level boot) 202 * 203 * Sets the page size, perhaps based upon the memory size. 204 * Must be called before any use of page-size dependent functions. 205 */ 206 void 207 vm_set_page_size(void) 208 { 209 if (vmstats.v_page_size == 0) 210 vmstats.v_page_size = PAGE_SIZE; 211 if (((vmstats.v_page_size - 1) & vmstats.v_page_size) != 0) 212 panic("vm_set_page_size: page size not a power of two"); 213 } 214 215 /* 216 * (low level boot) 217 * 218 * Add a new page to the freelist for use by the system. New pages 219 * are added to both the head and tail of the associated free page 220 * queue in a bottom-up fashion, so both zero'd and non-zero'd page 221 * requests pull 'recent' adds (higher physical addresses) first. 222 * 223 * Beware that the page zeroing daemon will also be running soon after 224 * boot, moving pages from the head to the tail of the PQ_FREE queues. 225 * 226 * Must be called in a critical section. 227 */ 228 static void 229 vm_add_new_page(vm_paddr_t pa, int *badcountp) 230 { 231 struct vpgqueues *vpq; 232 vm_page_t m; 233 234 m = PHYS_TO_VM_PAGE(pa); 235 236 /* 237 * Make sure it isn't a duplicate (due to BIOS page range overlaps, 238 * which we consider bugs... but don't crash). Note that m->phys_addr 239 * is pre-initialized, so use m->queue as a check. 240 */ 241 if (m->queue) { 242 if (*badcountp < 10) { 243 kprintf("vm_add_new_page: duplicate pa %016jx\n", 244 (intmax_t)pa); 245 ++*badcountp; 246 } else if (*badcountp == 10) { 247 kprintf("vm_add_new_page: duplicate pa (many more)\n"); 248 ++*badcountp; 249 } 250 return; 251 } 252 253 m->phys_addr = pa; 254 m->flags = 0; 255 m->pat_mode = PAT_WRITE_BACK; 256 m->pc = (pa >> PAGE_SHIFT); 257 258 /* 259 * Twist for cpu localization in addition to page coloring, so 260 * different cpus selecting by m->queue get different page colors. 261 */ 262 m->pc ^= ((pa >> PAGE_SHIFT) / PQ_L2_SIZE); 263 m->pc ^= ((pa >> PAGE_SHIFT) / (PQ_L2_SIZE * PQ_L2_SIZE)); 264 m->pc &= PQ_L2_MASK; 265 266 /* 267 * Reserve a certain number of contiguous low memory pages for 268 * contigmalloc() to use. 269 * 270 * Even though these pages represent real ram and can be 271 * reverse-mapped, we set PG_FICTITIOUS and PG_UNQUEUED 272 * because their use is special-cased. 273 * 274 * WARNING! Once PG_FICTITIOUS is set, vm_page_wire*() 275 * and vm_page_unwire*() calls have no effect. 276 */ 277 if (pa < vm_low_phys_reserved) { 278 atomic_add_long(&vmstats.v_page_count, 1); 279 atomic_add_long(&vmstats.v_dma_pages, 1); 280 m->flags |= PG_FICTITIOUS | PG_UNQUEUED; 281 m->queue = PQ_NONE; 282 m->wire_count = 1; 283 atomic_add_long(&vmstats.v_wire_count, 1); 284 alist_free(&vm_contig_alist, pa >> PAGE_SHIFT, 1); 285 return; 286 } 287 288 /* 289 * General page 290 */ 291 m->queue = m->pc + PQ_FREE; 292 KKASSERT(m->dirty == 0); 293 294 atomic_add_long(&vmstats.v_page_count, 1); 295 atomic_add_long(&vmstats.v_free_count, 1); 296 vpq = &vm_page_queues[m->queue]; 297 TAILQ_INSERT_HEAD(&vpq->pl, m, pageq); 298 ++vpq->lcnt; 299 } 300 301 /* 302 * (low level boot) 303 * 304 * Initializes the resident memory module. 305 * 306 * Preallocates memory for critical VM structures and arrays prior to 307 * kernel_map becoming available. 308 * 309 * Memory is allocated from (virtual2_start, virtual2_end) if available, 310 * otherwise memory is allocated from (virtual_start, virtual_end). 311 * 312 * On x86-64 (virtual_start, virtual_end) is only 2GB and may not be 313 * large enough to hold vm_page_array & other structures for machines with 314 * large amounts of ram, so we want to use virtual2* when available. 315 */ 316 void 317 vm_page_startup(void) 318 { 319 vm_offset_t vaddr = virtual2_start ? virtual2_start : virtual_start; 320 vm_offset_t mapped; 321 vm_pindex_t npages; 322 vm_paddr_t page_range; 323 vm_paddr_t new_end; 324 int i; 325 vm_paddr_t pa; 326 vm_paddr_t last_pa; 327 vm_paddr_t end; 328 vm_paddr_t biggestone, biggestsize; 329 vm_paddr_t total; 330 vm_page_t m; 331 int badcount; 332 333 total = 0; 334 badcount = 0; 335 biggestsize = 0; 336 biggestone = 0; 337 vaddr = round_page(vaddr); 338 339 /* 340 * Make sure ranges are page-aligned. 341 */ 342 for (i = 0; phys_avail[i].phys_end; ++i) { 343 phys_avail[i].phys_beg = round_page64(phys_avail[i].phys_beg); 344 phys_avail[i].phys_end = trunc_page64(phys_avail[i].phys_end); 345 if (phys_avail[i].phys_end < phys_avail[i].phys_beg) 346 phys_avail[i].phys_end = phys_avail[i].phys_beg; 347 } 348 349 /* 350 * Locate largest block 351 */ 352 for (i = 0; phys_avail[i].phys_end; ++i) { 353 vm_paddr_t size = phys_avail[i].phys_end - 354 phys_avail[i].phys_beg; 355 356 if (size > biggestsize) { 357 biggestone = i; 358 biggestsize = size; 359 } 360 total += size; 361 } 362 --i; /* adjust to last entry for use down below */ 363 364 end = phys_avail[biggestone].phys_end; 365 end = trunc_page(end); 366 367 /* 368 * Initialize the queue headers for the free queue, the active queue 369 * and the inactive queue. 370 */ 371 vm_page_queue_init(); 372 373 #if !defined(_KERNEL_VIRTUAL) 374 /* 375 * VKERNELs don't support minidumps and as such don't need 376 * vm_page_dump 377 * 378 * Allocate a bitmap to indicate that a random physical page 379 * needs to be included in a minidump. 380 * 381 * The amd64 port needs this to indicate which direct map pages 382 * need to be dumped, via calls to dump_add_page()/dump_drop_page(). 383 * 384 * However, x86 still needs this workspace internally within the 385 * minidump code. In theory, they are not needed on x86, but are 386 * included should the sf_buf code decide to use them. 387 */ 388 page_range = phys_avail[i].phys_end / PAGE_SIZE; 389 vm_page_dump_size = round_page(roundup2(page_range, NBBY) / NBBY); 390 end -= vm_page_dump_size; 391 vm_page_dump = (void *)pmap_map(&vaddr, end, end + vm_page_dump_size, 392 VM_PROT_READ | VM_PROT_WRITE); 393 bzero((void *)vm_page_dump, vm_page_dump_size); 394 #endif 395 /* 396 * Compute the number of pages of memory that will be available for 397 * use (taking into account the overhead of a page structure per 398 * page). 399 */ 400 first_page = phys_avail[0].phys_beg / PAGE_SIZE; 401 page_range = phys_avail[i].phys_end / PAGE_SIZE - first_page; 402 npages = (total - (page_range * sizeof(struct vm_page))) / PAGE_SIZE; 403 404 #ifndef _KERNEL_VIRTUAL 405 /* 406 * (only applies to real kernels) 407 * 408 * Reserve a large amount of low memory for potential 32-bit DMA 409 * space allocations. Once device initialization is complete we 410 * release most of it, but keep (vm_dma_reserved) memory reserved 411 * for later use. Typically for X / graphics. Through trial and 412 * error we find that GPUs usually requires ~60-100MB or so. 413 * 414 * By default, 128M is left in reserve on machines with 2G+ of ram. 415 */ 416 vm_low_phys_reserved = (vm_paddr_t)65536 << PAGE_SHIFT; 417 if (vm_low_phys_reserved > total / 4) 418 vm_low_phys_reserved = total / 4; 419 if (vm_dma_reserved == 0) { 420 vm_dma_reserved = 128 * 1024 * 1024; /* 128MB */ 421 if (vm_dma_reserved > total / 16) 422 vm_dma_reserved = total / 16; 423 } 424 #endif 425 alist_init(&vm_contig_alist, 65536, vm_contig_ameta, 426 ALIST_RECORDS_65536); 427 428 /* 429 * Initialize the mem entry structures now, and put them in the free 430 * queue. 431 */ 432 if (bootverbose && ctob(physmem) >= 400LL*1024*1024*1024) 433 kprintf("initializing vm_page_array "); 434 new_end = trunc_page(end - page_range * sizeof(struct vm_page)); 435 mapped = pmap_map(&vaddr, new_end, end, VM_PROT_READ | VM_PROT_WRITE); 436 vm_page_array = (vm_page_t)mapped; 437 438 #if defined(__x86_64__) && !defined(_KERNEL_VIRTUAL) 439 /* 440 * since pmap_map on amd64 returns stuff out of a direct-map region, 441 * we have to manually add these pages to the minidump tracking so 442 * that they can be dumped, including the vm_page_array. 443 */ 444 for (pa = new_end; 445 pa < phys_avail[biggestone].phys_end; 446 pa += PAGE_SIZE) { 447 dump_add_page(pa); 448 } 449 #endif 450 451 /* 452 * Clear all of the page structures, run basic initialization so 453 * PHYS_TO_VM_PAGE() operates properly even on pages not in the 454 * map. 455 */ 456 bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page)); 457 vm_page_array_size = page_range; 458 if (bootverbose && ctob(physmem) >= 400LL*1024*1024*1024) 459 kprintf("size = 0x%zx\n", vm_page_array_size); 460 461 m = &vm_page_array[0]; 462 pa = ptoa(first_page); 463 for (i = 0; i < page_range; ++i) { 464 spin_init(&m->spin, "vm_page"); 465 m->phys_addr = pa; 466 pa += PAGE_SIZE; 467 ++m; 468 } 469 470 /* 471 * Construct the free queue(s) in ascending order (by physical 472 * address) so that the first 16MB of physical memory is allocated 473 * last rather than first. On large-memory machines, this avoids 474 * the exhaustion of low physical memory before isa_dma_init has run. 475 */ 476 vmstats.v_page_count = 0; 477 vmstats.v_free_count = 0; 478 for (i = 0; phys_avail[i].phys_end && npages > 0; ++i) { 479 pa = phys_avail[i].phys_beg; 480 if (i == biggestone) 481 last_pa = new_end; 482 else 483 last_pa = phys_avail[i].phys_end; 484 while (pa < last_pa && npages-- > 0) { 485 vm_add_new_page(pa, &badcount); 486 pa += PAGE_SIZE; 487 } 488 } 489 if (virtual2_start) 490 virtual2_start = vaddr; 491 else 492 virtual_start = vaddr; 493 mycpu->gd_vmstats = vmstats; 494 } 495 496 /* 497 * (called from early boot only) 498 * 499 * Reorganize VM pages based on numa data. May be called as many times as 500 * necessary. Will reorganize the vm_page_t page color and related queue(s) 501 * to allow vm_page_alloc() to choose pages based on socket affinity. 502 * 503 * NOTE: This function is only called while we are still in UP mode, so 504 * we only need a critical section to protect the queues (which 505 * saves a lot of time, there are likely a ton of pages). 506 */ 507 void 508 vm_numa_organize(vm_paddr_t ran_beg, vm_paddr_t bytes, int physid) 509 { 510 vm_paddr_t scan_beg; 511 vm_paddr_t scan_end; 512 vm_paddr_t ran_end; 513 struct vpgqueues *vpq; 514 vm_page_t m; 515 vm_page_t mend; 516 int socket_mod; 517 int socket_value; 518 int i; 519 520 /* 521 * Check if no physical information, or there was only one socket 522 * (so don't waste time doing nothing!). 523 */ 524 if (cpu_topology_phys_ids <= 1 || 525 cpu_topology_core_ids == 0) { 526 return; 527 } 528 529 /* 530 * Setup for our iteration. Note that ACPI may iterate CPU 531 * sockets starting at 0 or 1 or some other number. The 532 * cpu_topology code mod's it against the socket count. 533 */ 534 ran_end = ran_beg + bytes; 535 536 socket_mod = PQ_L2_SIZE / cpu_topology_phys_ids; 537 socket_value = (physid % cpu_topology_phys_ids) * socket_mod; 538 mend = &vm_page_array[vm_page_array_size]; 539 540 crit_enter(); 541 542 /* 543 * Adjust cpu_topology's phys_mem parameter 544 */ 545 if (root_cpu_node) 546 vm_numa_add_topology_mem(root_cpu_node, physid, (long)bytes); 547 548 /* 549 * Adjust vm_page->pc and requeue all affected pages. The 550 * allocator will then be able to localize memory allocations 551 * to some degree. 552 */ 553 for (i = 0; phys_avail[i].phys_end; ++i) { 554 scan_beg = phys_avail[i].phys_beg; 555 scan_end = phys_avail[i].phys_end; 556 if (scan_end <= ran_beg) 557 continue; 558 if (scan_beg >= ran_end) 559 continue; 560 if (scan_beg < ran_beg) 561 scan_beg = ran_beg; 562 if (scan_end > ran_end) 563 scan_end = ran_end; 564 if (atop(scan_end) > first_page + vm_page_array_size) 565 scan_end = ptoa(first_page + vm_page_array_size); 566 567 m = PHYS_TO_VM_PAGE(scan_beg); 568 while (scan_beg < scan_end) { 569 KKASSERT(m < mend); 570 if (m->queue != PQ_NONE) { 571 vpq = &vm_page_queues[m->queue]; 572 TAILQ_REMOVE(&vpq->pl, m, pageq); 573 --vpq->lcnt; 574 /* queue doesn't change, no need to adj cnt */ 575 m->queue -= m->pc; 576 m->pc %= socket_mod; 577 m->pc += socket_value; 578 m->pc &= PQ_L2_MASK; 579 m->queue += m->pc; 580 vpq = &vm_page_queues[m->queue]; 581 TAILQ_INSERT_HEAD(&vpq->pl, m, pageq); 582 ++vpq->lcnt; 583 /* queue doesn't change, no need to adj cnt */ 584 } else { 585 m->pc %= socket_mod; 586 m->pc += socket_value; 587 m->pc &= PQ_L2_MASK; 588 } 589 scan_beg += PAGE_SIZE; 590 ++m; 591 } 592 } 593 594 crit_exit(); 595 } 596 597 /* 598 * (called from early boot only) 599 * 600 * Don't allow the NUMA organization to leave vm_page_queues[] nodes 601 * completely empty for a logical cpu. Doing so would force allocations 602 * on that cpu to always borrow from a nearby cpu, create unnecessary 603 * contention, and cause vm_page_alloc() to iterate more queues and run more 604 * slowly. 605 * 606 * This situation can occur when memory sticks are not entirely populated, 607 * populated at different densities, or in naturally assymetric systems 608 * such as the 2990WX. There could very well be many vm_page_queues[] 609 * entries with *NO* pages assigned to them. 610 * 611 * Fixing this up ensures that each logical CPU has roughly the same 612 * sized memory pool, and more importantly ensures that logical CPUs 613 * do not wind up with an empty memory pool. 614 * 615 * At them moment we just iterate the other queues and borrow pages, 616 * moving them into the queues for cpus with severe deficits even though 617 * the memory might not be local to those cpus. I am not doing this in 618 * a 'smart' way, its effectively UMA style (sorta, since its page-by-page 619 * whereas real UMA typically exchanges address bits 8-10 with high address 620 * bits). But it works extremely well and gives us fairly good deterministic 621 * results on the cpu cores associated with these secondary nodes. 622 */ 623 void 624 vm_numa_organize_finalize(void) 625 { 626 struct vpgqueues *vpq; 627 vm_page_t m; 628 long lcnt_lo; 629 long lcnt_hi; 630 int iter; 631 int i; 632 int scale_lim; 633 634 crit_enter(); 635 636 /* 637 * Machines might not use an exact power of 2 for phys_ids, 638 * core_ids, ht_ids, etc. This can slightly reduce the actual 639 * range of indices in vm_page_queues[] that are nominally used. 640 */ 641 if (cpu_topology_ht_ids) { 642 scale_lim = PQ_L2_SIZE / cpu_topology_phys_ids; 643 scale_lim = scale_lim / cpu_topology_core_ids; 644 scale_lim = scale_lim / cpu_topology_ht_ids; 645 scale_lim = scale_lim * cpu_topology_ht_ids; 646 scale_lim = scale_lim * cpu_topology_core_ids; 647 scale_lim = scale_lim * cpu_topology_phys_ids; 648 } else { 649 scale_lim = PQ_L2_SIZE; 650 } 651 652 /* 653 * Calculate an average, set hysteresis for balancing from 654 * 10% below the average to the average. 655 */ 656 lcnt_hi = 0; 657 for (i = 0; i < scale_lim; ++i) { 658 lcnt_hi += vm_page_queues[i].lcnt; 659 } 660 lcnt_hi /= scale_lim; 661 lcnt_lo = lcnt_hi - lcnt_hi / 10; 662 663 kprintf("vm_page: avg %ld pages per queue, %d queues\n", 664 lcnt_hi, scale_lim); 665 666 iter = 0; 667 for (i = 0; i < scale_lim; ++i) { 668 vpq = &vm_page_queues[PQ_FREE + i]; 669 while (vpq->lcnt < lcnt_lo) { 670 struct vpgqueues *vptmp; 671 672 iter = (iter + 1) & PQ_L2_MASK; 673 vptmp = &vm_page_queues[PQ_FREE + iter]; 674 if (vptmp->lcnt < lcnt_hi) 675 continue; 676 m = TAILQ_FIRST(&vptmp->pl); 677 KKASSERT(m->queue == PQ_FREE + iter); 678 TAILQ_REMOVE(&vptmp->pl, m, pageq); 679 --vptmp->lcnt; 680 /* queue doesn't change, no need to adj cnt */ 681 m->queue -= m->pc; 682 m->pc = i; 683 m->queue += m->pc; 684 TAILQ_INSERT_HEAD(&vpq->pl, m, pageq); 685 ++vpq->lcnt; 686 } 687 } 688 crit_exit(); 689 } 690 691 static 692 void 693 vm_numa_add_topology_mem(cpu_node_t *cpup, int physid, long bytes) 694 { 695 int cpuid; 696 int i; 697 698 switch(cpup->type) { 699 case PACKAGE_LEVEL: 700 cpup->phys_mem += bytes; 701 break; 702 case CHIP_LEVEL: 703 /* 704 * All members should have the same chipid, so we only need 705 * to pull out one member. 706 */ 707 if (CPUMASK_TESTNZERO(cpup->members)) { 708 cpuid = BSFCPUMASK(cpup->members); 709 if (physid == 710 get_chip_ID_from_APICID(CPUID_TO_APICID(cpuid))) { 711 cpup->phys_mem += bytes; 712 } 713 } 714 break; 715 case CORE_LEVEL: 716 case THREAD_LEVEL: 717 /* 718 * Just inherit from the parent node 719 */ 720 cpup->phys_mem = cpup->parent_node->phys_mem; 721 break; 722 } 723 for (i = 0; i < MAXCPU && cpup->child_node[i]; ++i) 724 vm_numa_add_topology_mem(cpup->child_node[i], physid, bytes); 725 } 726 727 /* 728 * We tended to reserve a ton of memory for contigmalloc(). Now that most 729 * drivers have initialized we want to return most the remaining free 730 * reserve back to the VM page queues so they can be used for normal 731 * allocations. 732 * 733 * We leave vm_dma_reserved bytes worth of free pages in the reserve pool. 734 */ 735 static void 736 vm_page_startup_finish(void *dummy __unused) 737 { 738 alist_blk_t blk; 739 alist_blk_t rblk; 740 alist_blk_t count; 741 alist_blk_t xcount; 742 alist_blk_t bfree; 743 vm_page_t m; 744 struct vm_page_hash_elm *mp; 745 int mask; 746 747 /* 748 * Set the set_assoc_mask based on the fitted number of CPUs. 749 * This is a mask, so we subject 1. 750 * 751 * w/PQ_L2_SIZE = 1024, Don't let the associativity drop below 8. 752 * So if we have 256 CPUs, two hyper-threads will wind up sharing. 753 * 754 * The maximum is PQ_L2_SIZE. However, we limit the starting 755 * maximum to 16 (mask = 15) in order to improve the cache locality 756 * of related kernel data structures. 757 */ 758 mask = PQ_L2_SIZE / ncpus_fit - 1; 759 if (mask < 7) /* minimum is 8-way w/256 CPU threads */ 760 mask = 7; 761 if (mask < 15) 762 mask = 15; 763 cpu_ccfence(); 764 set_assoc_mask = mask; 765 766 /* 767 * Return part of the initial reserve back to the system 768 */ 769 spin_lock(&vm_contig_spin); 770 for (;;) { 771 bfree = alist_free_info(&vm_contig_alist, &blk, &count); 772 if (bfree <= vm_dma_reserved / PAGE_SIZE) 773 break; 774 if (count == 0) 775 break; 776 777 /* 778 * Figure out how much of the initial reserve we have to 779 * free in order to reach our target. 780 */ 781 bfree -= vm_dma_reserved / PAGE_SIZE; 782 if (count > bfree) { 783 blk += count - bfree; 784 count = bfree; 785 } 786 787 /* 788 * Calculate the nearest power of 2 <= count. 789 */ 790 for (xcount = 1; xcount <= count; xcount <<= 1) 791 ; 792 xcount >>= 1; 793 blk += count - xcount; 794 count = xcount; 795 796 /* 797 * Allocate the pages from the alist, then free them to 798 * the normal VM page queues. 799 * 800 * Pages allocated from the alist are wired. We have to 801 * busy, unwire, and free them. We must also adjust 802 * vm_low_phys_reserved before freeing any pages to prevent 803 * confusion. 804 */ 805 rblk = alist_alloc(&vm_contig_alist, blk, count); 806 if (rblk != blk) { 807 kprintf("vm_page_startup_finish: Unable to return " 808 "dma space @0x%08x/%d -> 0x%08x\n", 809 blk, count, rblk); 810 break; 811 } 812 atomic_add_long(&vmstats.v_dma_pages, -(long)count); 813 spin_unlock(&vm_contig_spin); 814 815 m = PHYS_TO_VM_PAGE((vm_paddr_t)blk << PAGE_SHIFT); 816 vm_low_phys_reserved = VM_PAGE_TO_PHYS(m); 817 while (count) { 818 vm_page_flag_clear(m, PG_FICTITIOUS | PG_UNQUEUED); 819 vm_page_busy_wait(m, FALSE, "cpgfr"); 820 vm_page_unwire(m, 0); 821 vm_page_free(m); 822 --count; 823 ++m; 824 } 825 spin_lock(&vm_contig_spin); 826 } 827 spin_unlock(&vm_contig_spin); 828 829 /* 830 * Print out how much DMA space drivers have already allocated and 831 * how much is left over. 832 */ 833 kprintf("DMA space used: %jdk, remaining available: %jdk\n", 834 (intmax_t)(vmstats.v_dma_pages - vm_contig_alist.bl_free) * 835 (PAGE_SIZE / 1024), 836 (intmax_t)vm_contig_alist.bl_free * (PAGE_SIZE / 1024)); 837 838 /* 839 * Power of 2 840 */ 841 vm_page_hash_size = 4096; 842 while (vm_page_hash_size < (vm_page_array_size / 16)) 843 vm_page_hash_size <<= 1; 844 if (vm_page_hash_size > VM_PAGE_HASH_MAX) 845 vm_page_hash_size = VM_PAGE_HASH_MAX; 846 847 /* 848 * hash table for vm_page_lookup_quick() 849 */ 850 mp = (void *)kmem_alloc3(&kernel_map, 851 vm_page_hash_size * sizeof(*vm_page_hash), 852 VM_SUBSYS_VMPGHASH, KM_CPU(0)); 853 bzero(mp, vm_page_hash_size * sizeof(*mp)); 854 cpu_sfence(); 855 vm_page_hash = mp; 856 } 857 SYSINIT(vm_pgend, SI_SUB_PROC0_POST, SI_ORDER_ANY, 858 vm_page_startup_finish, NULL); 859 860 861 /* 862 * Scan comparison function for Red-Black tree scans. An inclusive 863 * (start,end) is expected. Other fields are not used. 864 */ 865 int 866 rb_vm_page_scancmp(struct vm_page *p, void *data) 867 { 868 struct rb_vm_page_scan_info *info = data; 869 870 if (p->pindex < info->start_pindex) 871 return(-1); 872 if (p->pindex > info->end_pindex) 873 return(1); 874 return(0); 875 } 876 877 int 878 rb_vm_page_compare(struct vm_page *p1, struct vm_page *p2) 879 { 880 if (p1->pindex < p2->pindex) 881 return(-1); 882 if (p1->pindex > p2->pindex) 883 return(1); 884 return(0); 885 } 886 887 void 888 vm_page_init(vm_page_t m) 889 { 890 /* do nothing for now. Called from pmap_page_init() */ 891 } 892 893 /* 894 * Each page queue has its own spin lock, which is fairly optimal for 895 * allocating and freeing pages at least. 896 * 897 * The caller must hold the vm_page_spin_lock() before locking a vm_page's 898 * queue spinlock via this function. Also note that m->queue cannot change 899 * unless both the page and queue are locked. 900 */ 901 static __inline 902 void 903 _vm_page_queue_spin_lock(vm_page_t m) 904 { 905 u_short queue; 906 907 queue = m->queue; 908 if (queue != PQ_NONE) { 909 spin_lock(&vm_page_queues[queue].spin); 910 KKASSERT(queue == m->queue); 911 } 912 } 913 914 static __inline 915 void 916 _vm_page_queue_spin_unlock(vm_page_t m) 917 { 918 u_short queue; 919 920 queue = m->queue; 921 cpu_ccfence(); 922 if (queue != PQ_NONE) 923 spin_unlock(&vm_page_queues[queue].spin); 924 } 925 926 static __inline 927 void 928 _vm_page_queues_spin_lock(u_short queue) 929 { 930 cpu_ccfence(); 931 if (queue != PQ_NONE) 932 spin_lock(&vm_page_queues[queue].spin); 933 } 934 935 936 static __inline 937 void 938 _vm_page_queues_spin_unlock(u_short queue) 939 { 940 cpu_ccfence(); 941 if (queue != PQ_NONE) 942 spin_unlock(&vm_page_queues[queue].spin); 943 } 944 945 void 946 vm_page_queue_spin_lock(vm_page_t m) 947 { 948 _vm_page_queue_spin_lock(m); 949 } 950 951 void 952 vm_page_queues_spin_lock(u_short queue) 953 { 954 _vm_page_queues_spin_lock(queue); 955 } 956 957 void 958 vm_page_queue_spin_unlock(vm_page_t m) 959 { 960 _vm_page_queue_spin_unlock(m); 961 } 962 963 void 964 vm_page_queues_spin_unlock(u_short queue) 965 { 966 _vm_page_queues_spin_unlock(queue); 967 } 968 969 /* 970 * This locks the specified vm_page and its queue in the proper order 971 * (page first, then queue). The queue may change so the caller must 972 * recheck on return. 973 */ 974 static __inline 975 void 976 _vm_page_and_queue_spin_lock(vm_page_t m) 977 { 978 vm_page_spin_lock(m); 979 _vm_page_queue_spin_lock(m); 980 } 981 982 static __inline 983 void 984 _vm_page_and_queue_spin_unlock(vm_page_t m) 985 { 986 _vm_page_queues_spin_unlock(m->queue); 987 vm_page_spin_unlock(m); 988 } 989 990 void 991 vm_page_and_queue_spin_unlock(vm_page_t m) 992 { 993 _vm_page_and_queue_spin_unlock(m); 994 } 995 996 void 997 vm_page_and_queue_spin_lock(vm_page_t m) 998 { 999 _vm_page_and_queue_spin_lock(m); 1000 } 1001 1002 /* 1003 * Helper function removes vm_page from its current queue. 1004 * Returns the base queue the page used to be on. 1005 * 1006 * The vm_page and the queue must be spinlocked. 1007 * This function will unlock the queue but leave the page spinlocked. 1008 */ 1009 static __inline u_short 1010 _vm_page_rem_queue_spinlocked(vm_page_t m) 1011 { 1012 struct vpgqueues *pq; 1013 u_short queue; 1014 u_short oqueue; 1015 long *cnt_adj; 1016 long *cnt_gd; 1017 1018 queue = m->queue; 1019 if (queue != PQ_NONE) { 1020 pq = &vm_page_queues[queue]; 1021 TAILQ_REMOVE(&pq->pl, m, pageq); 1022 1023 /* 1024 * Primarily adjust our pcpu stats for rollup, which is 1025 * (mycpu->gd_vmstats_adj + offset). This is normally 1026 * synchronized on every hardclock(). 1027 * 1028 * However, in order for the nominal low-memory algorithms 1029 * to work properly if the unsynchronized adjustment gets 1030 * too negative and might trigger the pageout daemon, we 1031 * immediately synchronize with the global structure. 1032 * 1033 * The idea here is to reduce unnecessary SMP cache mastership 1034 * changes in the global vmstats, which can be particularly 1035 * bad in multi-socket systems. 1036 * 1037 * WARNING! In systems with low amounts of memory the 1038 * vm_paging_needed(-1024 * ncpus) test could 1039 * wind up testing a value above the paging target, 1040 * meaning it would almost always return TRUE. In 1041 * that situation we synchronize every time the 1042 * cumulative adjustment falls below -1024. 1043 */ 1044 cnt_adj = (long *)((char *)&mycpu->gd_vmstats_adj + 1045 pq->cnt_offset); 1046 cnt_gd = (long *)((char *)&mycpu->gd_vmstats + 1047 pq->cnt_offset); 1048 atomic_add_long(cnt_adj, -1); 1049 atomic_add_long(cnt_gd, -1); 1050 1051 if (*cnt_adj < -1024 && vm_paging_needed(-1024 * ncpus)) { 1052 u_long copy = atomic_swap_long(cnt_adj, 0); 1053 cnt_adj = (long *)((char *)&vmstats + pq->cnt_offset); 1054 atomic_add_long(cnt_adj, copy); 1055 } 1056 pq->lcnt--; 1057 m->queue = PQ_NONE; 1058 oqueue = queue; 1059 queue -= m->pc; 1060 vm_page_queues_spin_unlock(oqueue); /* intended */ 1061 } 1062 return queue; 1063 } 1064 1065 /* 1066 * Helper function places the vm_page on the specified queue. Generally 1067 * speaking only PQ_FREE pages are placed at the head, to allow them to 1068 * be allocated sooner rather than later on the assumption that they 1069 * are cache-hot. 1070 * 1071 * The vm_page must be spinlocked. 1072 * The vm_page must NOT be FICTITIOUS (that would be a disaster) 1073 * This function will return with both the page and the queue locked. 1074 */ 1075 static __inline void 1076 _vm_page_add_queue_spinlocked(vm_page_t m, u_short queue, int athead) 1077 { 1078 struct vpgqueues *pq; 1079 u_long *cnt_adj; 1080 u_long *cnt_gd; 1081 1082 KKASSERT(m->queue == PQ_NONE && 1083 (m->flags & (PG_FICTITIOUS | PG_UNQUEUED)) == 0); 1084 1085 if (queue != PQ_NONE) { 1086 vm_page_queues_spin_lock(queue); 1087 pq = &vm_page_queues[queue]; 1088 ++pq->lcnt; 1089 1090 /* 1091 * Adjust our pcpu stats. If a system entity really needs 1092 * to incorporate the count it will call vmstats_rollup() 1093 * to roll it all up into the global vmstats strufture. 1094 */ 1095 cnt_adj = (long *)((char *)&mycpu->gd_vmstats_adj + 1096 pq->cnt_offset); 1097 cnt_gd = (long *)((char *)&mycpu->gd_vmstats + 1098 pq->cnt_offset); 1099 atomic_add_long(cnt_adj, 1); 1100 atomic_add_long(cnt_gd, 1); 1101 1102 /* 1103 * PQ_FREE is always handled LIFO style to try to provide 1104 * cache-hot pages to programs. 1105 */ 1106 m->queue = queue; 1107 if (queue - m->pc == PQ_FREE) { 1108 TAILQ_INSERT_HEAD(&pq->pl, m, pageq); 1109 } else if (athead) { 1110 TAILQ_INSERT_HEAD(&pq->pl, m, pageq); 1111 } else { 1112 TAILQ_INSERT_TAIL(&pq->pl, m, pageq); 1113 } 1114 /* leave the queue spinlocked */ 1115 } 1116 } 1117 1118 /* 1119 * Wait until page is no longer BUSY. If also_m_busy is TRUE we wait 1120 * until the page is no longer BUSY or SBUSY (busy_count field is 0). 1121 * 1122 * Returns TRUE if it had to sleep, FALSE if we did not. Only one sleep 1123 * call will be made before returning. 1124 * 1125 * This function does NOT busy the page and on return the page is not 1126 * guaranteed to be available. 1127 */ 1128 void 1129 vm_page_sleep_busy(vm_page_t m, int also_m_busy, const char *msg) 1130 { 1131 u_int32_t busy_count; 1132 1133 for (;;) { 1134 busy_count = m->busy_count; 1135 cpu_ccfence(); 1136 1137 if ((busy_count & PBUSY_LOCKED) == 0 && 1138 (also_m_busy == 0 || (busy_count & PBUSY_MASK) == 0)) { 1139 break; 1140 } 1141 tsleep_interlock(m, 0); 1142 if (atomic_cmpset_int(&m->busy_count, busy_count, 1143 busy_count | PBUSY_WANTED)) { 1144 atomic_set_int(&m->flags, PG_REFERENCED); 1145 tsleep(m, PINTERLOCKED, msg, 0); 1146 break; 1147 } 1148 } 1149 } 1150 1151 /* 1152 * This calculates and returns a page color given an optional VM object and 1153 * either a pindex or an iterator. We attempt to return a cpu-localized 1154 * pg_color that is still roughly 16-way set-associative. The CPU topology 1155 * is used if it was probed. 1156 * 1157 * The caller may use the returned value to index into e.g. PQ_FREE when 1158 * allocating a page in order to nominally obtain pages that are hopefully 1159 * already localized to the requesting cpu. This function is not able to 1160 * provide any sort of guarantee of this, but does its best to improve 1161 * hardware cache management performance. 1162 * 1163 * WARNING! The caller must mask the returned value with PQ_L2_MASK. 1164 */ 1165 u_short 1166 vm_get_pg_color(int cpuid, vm_object_t object, vm_pindex_t pindex) 1167 { 1168 u_short pg_color; 1169 int object_pg_color; 1170 1171 /* 1172 * WARNING! cpu_topology_core_ids might not be a power of two. 1173 * We also shouldn't make assumptions about 1174 * cpu_topology_phys_ids either. 1175 * 1176 * WARNING! ncpus might not be known at this time (during early 1177 * boot), and might be set to 1. 1178 * 1179 * General format: [phys_id][core_id][cpuid][set-associativity] 1180 * (but uses modulo, so not necessarily precise bit masks) 1181 */ 1182 object_pg_color = object ? object->pg_color : 0; 1183 1184 if (cpu_topology_ht_ids) { 1185 int phys_id; 1186 int core_id; 1187 int ht_id; 1188 int physcale; 1189 int grpscale; 1190 int cpuscale; 1191 1192 /* 1193 * Translate cpuid to socket, core, and hyperthread id. 1194 */ 1195 phys_id = get_cpu_phys_id(cpuid); 1196 core_id = get_cpu_core_id(cpuid); 1197 ht_id = get_cpu_ht_id(cpuid); 1198 1199 /* 1200 * Calculate pg_color for our array index. 1201 * 1202 * physcale - socket multiplier. 1203 * grpscale - core multiplier (cores per socket) 1204 * cpu* - cpus per core 1205 * 1206 * WARNING! In early boot, ncpus has not yet been 1207 * initialized and may be set to (1). 1208 * 1209 * WARNING! physcale must match the organization that 1210 * vm_numa_organize() creates to ensure that 1211 * we properly localize allocations to the 1212 * requested cpuid. 1213 */ 1214 physcale = PQ_L2_SIZE / cpu_topology_phys_ids; 1215 grpscale = physcale / cpu_topology_core_ids; 1216 cpuscale = grpscale / cpu_topology_ht_ids; 1217 1218 pg_color = phys_id * physcale; 1219 pg_color += core_id * grpscale; 1220 pg_color += ht_id * cpuscale; 1221 pg_color += (pindex + object_pg_color) % cpuscale; 1222 1223 #if 0 1224 if (grpsize >= 8) { 1225 pg_color += (pindex + object_pg_color) % grpsize; 1226 } else { 1227 if (grpsize <= 2) { 1228 grpsize = 8; 1229 } else { 1230 /* 3->9, 4->8, 5->10, 6->12, 7->14 */ 1231 grpsize += grpsize; 1232 if (grpsize < 8) 1233 grpsize += grpsize; 1234 } 1235 pg_color += (pindex + object_pg_color) % grpsize; 1236 } 1237 #endif 1238 } else { 1239 /* 1240 * Unknown topology, distribute things evenly. 1241 * 1242 * WARNING! In early boot, ncpus has not yet been 1243 * initialized and may be set to (1). 1244 */ 1245 int cpuscale; 1246 1247 cpuscale = PQ_L2_SIZE / ncpus; 1248 1249 pg_color = cpuid * cpuscale; 1250 pg_color += (pindex + object_pg_color) % cpuscale; 1251 } 1252 return (pg_color & PQ_L2_MASK); 1253 } 1254 1255 /* 1256 * Wait until BUSY can be set, then set it. If also_m_busy is TRUE we 1257 * also wait for m->busy_count to become 0 before setting PBUSY_LOCKED. 1258 */ 1259 void 1260 VM_PAGE_DEBUG_EXT(vm_page_busy_wait)(vm_page_t m, 1261 int also_m_busy, const char *msg 1262 VM_PAGE_DEBUG_ARGS) 1263 { 1264 u_int32_t busy_count; 1265 1266 for (;;) { 1267 busy_count = m->busy_count; 1268 cpu_ccfence(); 1269 if (busy_count & PBUSY_LOCKED) { 1270 tsleep_interlock(m, 0); 1271 if (atomic_cmpset_int(&m->busy_count, busy_count, 1272 busy_count | PBUSY_WANTED)) { 1273 atomic_set_int(&m->flags, PG_REFERENCED); 1274 tsleep(m, PINTERLOCKED, msg, 0); 1275 } 1276 } else if (also_m_busy && busy_count) { 1277 tsleep_interlock(m, 0); 1278 if (atomic_cmpset_int(&m->busy_count, busy_count, 1279 busy_count | PBUSY_WANTED)) { 1280 atomic_set_int(&m->flags, PG_REFERENCED); 1281 tsleep(m, PINTERLOCKED, msg, 0); 1282 } 1283 } else { 1284 if (atomic_cmpset_int(&m->busy_count, busy_count, 1285 busy_count | PBUSY_LOCKED)) { 1286 #ifdef VM_PAGE_DEBUG 1287 m->busy_func = func; 1288 m->busy_line = lineno; 1289 #endif 1290 break; 1291 } 1292 } 1293 } 1294 } 1295 1296 /* 1297 * Attempt to set BUSY. If also_m_busy is TRUE we only succeed if 1298 * m->busy_count is also 0. 1299 * 1300 * Returns non-zero on failure. 1301 */ 1302 int 1303 VM_PAGE_DEBUG_EXT(vm_page_busy_try)(vm_page_t m, int also_m_busy 1304 VM_PAGE_DEBUG_ARGS) 1305 { 1306 u_int32_t busy_count; 1307 1308 for (;;) { 1309 busy_count = m->busy_count; 1310 cpu_ccfence(); 1311 if (busy_count & PBUSY_LOCKED) 1312 return TRUE; 1313 if (also_m_busy && (busy_count & PBUSY_MASK) != 0) 1314 return TRUE; 1315 if (atomic_cmpset_int(&m->busy_count, busy_count, 1316 busy_count | PBUSY_LOCKED)) { 1317 #ifdef VM_PAGE_DEBUG 1318 m->busy_func = func; 1319 m->busy_line = lineno; 1320 #endif 1321 return FALSE; 1322 } 1323 } 1324 } 1325 1326 /* 1327 * Clear the BUSY flag and return non-zero to indicate to the caller 1328 * that a wakeup() should be performed. 1329 * 1330 * (inline version) 1331 */ 1332 static __inline 1333 int 1334 _vm_page_wakeup(vm_page_t m) 1335 { 1336 u_int32_t busy_count; 1337 1338 busy_count = m->busy_count; 1339 cpu_ccfence(); 1340 for (;;) { 1341 if (atomic_fcmpset_int(&m->busy_count, &busy_count, 1342 busy_count & 1343 ~(PBUSY_LOCKED | PBUSY_WANTED))) { 1344 return((int)(busy_count & PBUSY_WANTED)); 1345 } 1346 } 1347 /* not reached */ 1348 } 1349 1350 /* 1351 * Clear the BUSY flag and wakeup anyone waiting for the page. This 1352 * is typically the last call you make on a page before moving onto 1353 * other things. 1354 */ 1355 void 1356 vm_page_wakeup(vm_page_t m) 1357 { 1358 KASSERT(m->busy_count & PBUSY_LOCKED, 1359 ("vm_page_wakeup: page not busy!!!")); 1360 if (_vm_page_wakeup(m)) 1361 wakeup(m); 1362 } 1363 1364 /* 1365 * Hold a page, preventing reuse. This is typically only called on pages 1366 * in a known state (either held busy, special, or interlocked in some 1367 * manner). Holding a page does not ensure that it remains valid, it only 1368 * prevents reuse. The page must not already be on the FREE queue or in 1369 * any danger of being moved to the FREE queue concurrent with this call. 1370 * 1371 * Other parts of the system can still disassociate the page from its object 1372 * and attempt to free it, or perform read or write I/O on it and/or otherwise 1373 * manipulate the page, but if the page is held the VM system will leave the 1374 * page and its data intact and not cycle it through the FREE queue until 1375 * the last hold has been released. 1376 * 1377 * (see vm_page_wire() if you want to prevent the page from being 1378 * disassociated from its object too). 1379 */ 1380 void 1381 vm_page_hold(vm_page_t m) 1382 { 1383 atomic_add_int(&m->hold_count, 1); 1384 KKASSERT(m->queue - m->pc != PQ_FREE); 1385 } 1386 1387 /* 1388 * The opposite of vm_page_hold(). If the page is on the HOLD queue 1389 * it was freed while held and must be moved back to the FREE queue. 1390 * 1391 * To avoid racing against vm_page_free*() we must re-test conditions 1392 * after obtaining the spin-lock. The initial test can also race a 1393 * vm_page_free*() that is in the middle of moving a page to PQ_HOLD, 1394 * leaving the page on PQ_HOLD with hold_count == 0. Rather than 1395 * throw a spin-lock in the critical path, we rely on the pageout 1396 * daemon to clean-up these loose ends. 1397 * 1398 * More critically, the 'easy movement' between queues without busying 1399 * a vm_page is only allowed for PQ_FREE<->PQ_HOLD. 1400 */ 1401 void 1402 vm_page_unhold(vm_page_t m) 1403 { 1404 KASSERT(m->hold_count > 0 && m->queue - m->pc != PQ_FREE, 1405 ("vm_page_unhold: pg %p illegal hold_count (%d) or " 1406 "on FREE queue (%d)", 1407 m, m->hold_count, m->queue - m->pc)); 1408 1409 if (atomic_fetchadd_int(&m->hold_count, -1) == 1 && 1410 m->queue - m->pc == PQ_HOLD) { 1411 vm_page_spin_lock(m); 1412 if (m->hold_count == 0 && m->queue - m->pc == PQ_HOLD) { 1413 _vm_page_queue_spin_lock(m); 1414 _vm_page_rem_queue_spinlocked(m); 1415 _vm_page_add_queue_spinlocked(m, PQ_FREE + m->pc, 1); 1416 _vm_page_queue_spin_unlock(m); 1417 } 1418 vm_page_spin_unlock(m); 1419 } 1420 } 1421 1422 /* 1423 * Create a fictitious page with the specified physical address and 1424 * memory attribute. The memory attribute is the only the machine- 1425 * dependent aspect of a fictitious page that must be initialized. 1426 */ 1427 void 1428 vm_page_initfake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr) 1429 { 1430 /* 1431 * The page's memattr might have changed since the 1432 * previous initialization. Update the pmap to the 1433 * new memattr. 1434 */ 1435 if ((m->flags & PG_FICTITIOUS) != 0) 1436 goto memattr; 1437 m->phys_addr = paddr; 1438 m->queue = PQ_NONE; 1439 /* Fictitious pages don't use "segind". */ 1440 /* Fictitious pages don't use "order" or "pool". */ 1441 m->flags = PG_FICTITIOUS | PG_UNQUEUED; 1442 m->busy_count = PBUSY_LOCKED; 1443 m->wire_count = 1; 1444 spin_init(&m->spin, "fake_page"); 1445 pmap_page_init(m); 1446 memattr: 1447 pmap_page_set_memattr(m, memattr); 1448 } 1449 1450 /* 1451 * Inserts the given vm_page into the object and object list. 1452 * 1453 * The pagetables are not updated but will presumably fault the page 1454 * in if necessary, or if a kernel page the caller will at some point 1455 * enter the page into the kernel's pmap. We are not allowed to block 1456 * here so we *can't* do this anyway. 1457 * 1458 * This routine may not block. 1459 * This routine must be called with the vm_object held. 1460 * This routine must be called with a critical section held. 1461 * 1462 * This routine returns TRUE if the page was inserted into the object 1463 * successfully, and FALSE if the page already exists in the object. 1464 */ 1465 int 1466 vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex) 1467 { 1468 ASSERT_LWKT_TOKEN_HELD_EXCL(vm_object_token(object)); 1469 if (m->object != NULL) 1470 panic("vm_page_insert: already inserted"); 1471 1472 atomic_add_int(&object->generation, 1); 1473 1474 /* 1475 * Associate the VM page with an (object, offset). 1476 * 1477 * The vm_page spin lock is required for interactions with the pmap. 1478 * XXX vm_page_spin_lock() might not be needed for this any more. 1479 */ 1480 vm_page_spin_lock(m); 1481 m->object = object; 1482 m->pindex = pindex; 1483 if (vm_page_rb_tree_RB_INSERT(&object->rb_memq, m)) { 1484 m->object = NULL; 1485 m->pindex = 0; 1486 vm_page_spin_unlock(m); 1487 return FALSE; 1488 } 1489 ++object->resident_page_count; 1490 ++mycpu->gd_vmtotal.t_rm; 1491 vm_page_spin_unlock(m); 1492 1493 /* 1494 * Since we are inserting a new and possibly dirty page, 1495 * update the object's OBJ_WRITEABLE and OBJ_MIGHTBEDIRTY flags. 1496 */ 1497 if ((m->valid & m->dirty) || 1498 (m->flags & (PG_WRITEABLE | PG_NEED_COMMIT))) 1499 vm_object_set_writeable_dirty(object); 1500 1501 /* 1502 * Checks for a swap assignment and sets PG_SWAPPED if appropriate. 1503 */ 1504 swap_pager_page_inserted(m); 1505 return TRUE; 1506 } 1507 1508 /* 1509 * Removes the given vm_page_t from the (object,index) table 1510 * 1511 * The page must be BUSY and will remain BUSY on return. 1512 * No other requirements. 1513 * 1514 * NOTE: FreeBSD side effect was to unbusy the page on return. We leave 1515 * it busy. 1516 * 1517 * NOTE: Caller is responsible for any pmap disposition prior to the 1518 * rename (as the pmap code will not be able to find the entries 1519 * once the object has been disassociated). The caller may choose 1520 * to leave the pmap association intact if this routine is being 1521 * called as part of a rename between shadowed objects. 1522 * 1523 * This routine may not block. 1524 */ 1525 void 1526 vm_page_remove(vm_page_t m) 1527 { 1528 vm_object_t object; 1529 1530 if (m->object == NULL) { 1531 return; 1532 } 1533 1534 if ((m->busy_count & PBUSY_LOCKED) == 0) 1535 panic("vm_page_remove: page not busy"); 1536 1537 object = m->object; 1538 1539 vm_object_hold(object); 1540 1541 /* 1542 * Remove the page from the object and update the object. 1543 * 1544 * The vm_page spin lock is required for interactions with the pmap. 1545 * XXX vm_page_spin_lock() might not be needed for this any more. 1546 */ 1547 vm_page_spin_lock(m); 1548 vm_page_rb_tree_RB_REMOVE(&object->rb_memq, m); 1549 --object->resident_page_count; 1550 --mycpu->gd_vmtotal.t_rm; 1551 m->object = NULL; 1552 atomic_add_int(&object->generation, 1); 1553 vm_page_spin_unlock(m); 1554 1555 vm_object_drop(object); 1556 } 1557 1558 /* 1559 * Calculate the hash position for the vm_page hash heuristic. 1560 * 1561 * Mask by ~3 to offer 4-way set-assoc 1562 */ 1563 static __inline 1564 struct vm_page_hash_elm * 1565 vm_page_hash_hash(vm_object_t object, vm_pindex_t pindex) 1566 { 1567 size_t hi; 1568 1569 /* mix it up */ 1570 hi = (intptr_t)object ^ object->pg_color ^ pindex; 1571 hi += object->pg_color * pindex; 1572 hi = hi ^ (hi >> 20); 1573 hi &= vm_page_hash_size - 1; /* bounds */ 1574 hi &= ~(VM_PAGE_HASH_SET - 1); /* set-assoc */ 1575 return (&vm_page_hash[hi]); 1576 } 1577 1578 /* 1579 * Heuristical page lookup that does not require any locks. Returns 1580 * a soft-busied page on success, NULL on failure. 1581 * 1582 * Caller must lookup the page the slow way if NULL is returned. 1583 */ 1584 vm_page_t 1585 vm_page_hash_get(vm_object_t object, vm_pindex_t pindex) 1586 { 1587 struct vm_page_hash_elm *mp; 1588 vm_page_t m; 1589 int i; 1590 1591 if (vm_page_hash == NULL) 1592 return NULL; 1593 mp = vm_page_hash_hash(object, pindex); 1594 for (i = 0; i < VM_PAGE_HASH_SET; ++i) { 1595 m = mp[i].m; 1596 cpu_ccfence(); 1597 if (m == NULL) 1598 continue; 1599 if (m->object != object || m->pindex != pindex) 1600 continue; 1601 if (vm_page_sbusy_try(m)) 1602 continue; 1603 if (m->object == object && m->pindex == pindex) { 1604 /* 1605 * On-match optimization - do not update ticks 1606 * unless we have to (reduce cache coherency traffic) 1607 */ 1608 if (mp[i].ticks != ticks) 1609 mp[i].ticks = ticks; 1610 return m; 1611 } 1612 vm_page_sbusy_drop(m); 1613 } 1614 return NULL; 1615 } 1616 1617 /* 1618 * Enter page onto vm_page_hash[]. This is a heuristic, SMP collisions 1619 * are allowed. 1620 */ 1621 static __inline 1622 void 1623 vm_page_hash_enter(vm_page_t m) 1624 { 1625 struct vm_page_hash_elm *mp; 1626 struct vm_page_hash_elm *best; 1627 int i; 1628 1629 /* 1630 * Only enter type-stable vm_pages with well-shared objects. 1631 */ 1632 if (vm_page_hash == NULL || 1633 m < &vm_page_array[0] || 1634 m >= &vm_page_array[vm_page_array_size]) 1635 return; 1636 if (m->object == NULL) 1637 return; 1638 #if 0 1639 /* 1640 * Disabled at the moment, there are some degenerate conditions 1641 * with often-exec'd programs that get ignored. In particular, 1642 * the kernel's elf loader does a vn_rdwr() on the first page of 1643 * a binary. 1644 */ 1645 if (m->object->ref_count <= 2 || (m->object->flags & OBJ_ONEMAPPING)) 1646 return; 1647 #endif 1648 if (vm_page_hash_vnode_only && m->object->type != OBJT_VNODE) 1649 return; 1650 1651 /* 1652 * Find best entry 1653 */ 1654 mp = vm_page_hash_hash(m->object, m->pindex); 1655 best = mp; 1656 for (i = 0; i < VM_PAGE_HASH_SET; ++i) { 1657 if (mp[i].m == m) { 1658 /* 1659 * On-match optimization - do not update ticks 1660 * unless we have to (reduce cache coherency traffic) 1661 */ 1662 if (mp[i].ticks != ticks) 1663 mp[i].ticks = ticks; 1664 return; 1665 } 1666 1667 /* 1668 * The best choice is the oldest entry. 1669 * 1670 * Also check for a field overflow, using -1 instead of 0 1671 * to deal with SMP races on accessing the 'ticks' global. 1672 */ 1673 if ((ticks - best->ticks) < (ticks - mp[i].ticks) || 1674 (int)(ticks - mp[i].ticks) < -1) { 1675 best = &mp[i]; 1676 } 1677 } 1678 best->m = m; 1679 best->ticks = ticks; 1680 } 1681 1682 /* 1683 * Locate and return the page at (object, pindex), or NULL if the 1684 * page could not be found. 1685 * 1686 * The caller must hold the vm_object token. 1687 */ 1688 vm_page_t 1689 vm_page_lookup(vm_object_t object, vm_pindex_t pindex) 1690 { 1691 vm_page_t m; 1692 1693 /* 1694 * Search the hash table for this object/offset pair 1695 */ 1696 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object)); 1697 m = vm_page_rb_tree_RB_LOOKUP(&object->rb_memq, pindex); 1698 if (m) { 1699 KKASSERT(m->object == object && m->pindex == pindex); 1700 vm_page_hash_enter(m); 1701 } 1702 return(m); 1703 } 1704 1705 vm_page_t 1706 VM_PAGE_DEBUG_EXT(vm_page_lookup_busy_wait)(struct vm_object *object, 1707 vm_pindex_t pindex, 1708 int also_m_busy, const char *msg 1709 VM_PAGE_DEBUG_ARGS) 1710 { 1711 u_int32_t busy_count; 1712 vm_page_t m; 1713 1714 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object)); 1715 m = vm_page_rb_tree_RB_LOOKUP(&object->rb_memq, pindex); 1716 while (m) { 1717 KKASSERT(m->object == object && m->pindex == pindex); 1718 busy_count = m->busy_count; 1719 cpu_ccfence(); 1720 if (busy_count & PBUSY_LOCKED) { 1721 tsleep_interlock(m, 0); 1722 if (atomic_cmpset_int(&m->busy_count, busy_count, 1723 busy_count | PBUSY_WANTED)) { 1724 atomic_set_int(&m->flags, PG_REFERENCED); 1725 tsleep(m, PINTERLOCKED, msg, 0); 1726 m = vm_page_rb_tree_RB_LOOKUP(&object->rb_memq, 1727 pindex); 1728 } 1729 } else if (also_m_busy && busy_count) { 1730 tsleep_interlock(m, 0); 1731 if (atomic_cmpset_int(&m->busy_count, busy_count, 1732 busy_count | PBUSY_WANTED)) { 1733 atomic_set_int(&m->flags, PG_REFERENCED); 1734 tsleep(m, PINTERLOCKED, msg, 0); 1735 m = vm_page_rb_tree_RB_LOOKUP(&object->rb_memq, 1736 pindex); 1737 } 1738 } else if (atomic_cmpset_int(&m->busy_count, busy_count, 1739 busy_count | PBUSY_LOCKED)) { 1740 #ifdef VM_PAGE_DEBUG 1741 m->busy_func = func; 1742 m->busy_line = lineno; 1743 #endif 1744 vm_page_hash_enter(m); 1745 break; 1746 } 1747 } 1748 return m; 1749 } 1750 1751 /* 1752 * Attempt to lookup and busy a page. 1753 * 1754 * Returns NULL if the page could not be found 1755 * 1756 * Returns a vm_page and error == TRUE if the page exists but could not 1757 * be busied. 1758 * 1759 * Returns a vm_page and error == FALSE on success. 1760 */ 1761 vm_page_t 1762 VM_PAGE_DEBUG_EXT(vm_page_lookup_busy_try)(struct vm_object *object, 1763 vm_pindex_t pindex, 1764 int also_m_busy, int *errorp 1765 VM_PAGE_DEBUG_ARGS) 1766 { 1767 u_int32_t busy_count; 1768 vm_page_t m; 1769 1770 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object)); 1771 m = vm_page_rb_tree_RB_LOOKUP(&object->rb_memq, pindex); 1772 *errorp = FALSE; 1773 while (m) { 1774 KKASSERT(m->object == object && m->pindex == pindex); 1775 busy_count = m->busy_count; 1776 cpu_ccfence(); 1777 if (busy_count & PBUSY_LOCKED) { 1778 *errorp = TRUE; 1779 break; 1780 } 1781 if (also_m_busy && busy_count) { 1782 *errorp = TRUE; 1783 break; 1784 } 1785 if (atomic_cmpset_int(&m->busy_count, busy_count, 1786 busy_count | PBUSY_LOCKED)) { 1787 #ifdef VM_PAGE_DEBUG 1788 m->busy_func = func; 1789 m->busy_line = lineno; 1790 #endif 1791 vm_page_hash_enter(m); 1792 break; 1793 } 1794 } 1795 return m; 1796 } 1797 1798 /* 1799 * Returns a page that is only soft-busied for use by the caller in 1800 * a read-only fashion. Returns NULL if the page could not be found, 1801 * the soft busy could not be obtained, or the page data is invalid. 1802 * 1803 * XXX Doesn't handle PG_FICTITIOUS pages at the moment, but there is 1804 * no reason why we couldn't. 1805 */ 1806 vm_page_t 1807 vm_page_lookup_sbusy_try(struct vm_object *object, vm_pindex_t pindex, 1808 int pgoff, int pgbytes) 1809 { 1810 vm_page_t m; 1811 1812 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object)); 1813 m = vm_page_rb_tree_RB_LOOKUP(&object->rb_memq, pindex); 1814 if (m) { 1815 if ((m->valid != VM_PAGE_BITS_ALL && 1816 !vm_page_is_valid(m, pgoff, pgbytes)) || 1817 (m->flags & PG_FICTITIOUS)) { 1818 m = NULL; 1819 } else if (vm_page_sbusy_try(m)) { 1820 m = NULL; 1821 } else if ((m->valid != VM_PAGE_BITS_ALL && 1822 !vm_page_is_valid(m, pgoff, pgbytes)) || 1823 (m->flags & PG_FICTITIOUS)) { 1824 vm_page_sbusy_drop(m); 1825 m = NULL; 1826 } else { 1827 vm_page_hash_enter(m); 1828 } 1829 } 1830 return m; 1831 } 1832 1833 /* 1834 * Caller must hold the related vm_object 1835 */ 1836 vm_page_t 1837 vm_page_next(vm_page_t m) 1838 { 1839 vm_page_t next; 1840 1841 next = vm_page_rb_tree_RB_NEXT(m); 1842 if (next && next->pindex != m->pindex + 1) 1843 next = NULL; 1844 return (next); 1845 } 1846 1847 /* 1848 * vm_page_rename() 1849 * 1850 * Move the given vm_page from its current object to the specified 1851 * target object/offset. The page must be busy and will remain so 1852 * on return. 1853 * 1854 * new_object must be held. 1855 * This routine might block. XXX ? 1856 * 1857 * NOTE: Swap associated with the page must be invalidated by the move. We 1858 * have to do this for several reasons: (1) we aren't freeing the 1859 * page, (2) we are dirtying the page, (3) the VM system is probably 1860 * moving the page from object A to B, and will then later move 1861 * the backing store from A to B and we can't have a conflict. 1862 * 1863 * NOTE: We *always* dirty the page. It is necessary both for the 1864 * fact that we moved it, and because we may be invalidating 1865 * swap. If the page is on the cache, we have to deactivate it 1866 * or vm_page_dirty() will panic. Dirty pages are not allowed 1867 * on the cache. 1868 * 1869 * NOTE: Caller is responsible for any pmap disposition prior to the 1870 * rename (as the pmap code will not be able to find the entries 1871 * once the object has been disassociated or changed). Nominally 1872 * the caller is moving a page between shadowed objects and so the 1873 * pmap association is retained without having to remove the page 1874 * from it. 1875 */ 1876 void 1877 vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex) 1878 { 1879 KKASSERT(m->busy_count & PBUSY_LOCKED); 1880 ASSERT_LWKT_TOKEN_HELD_EXCL(vm_object_token(new_object)); 1881 if (m->object) { 1882 ASSERT_LWKT_TOKEN_HELD_EXCL(vm_object_token(m->object)); 1883 vm_page_remove(m); 1884 } 1885 if (vm_page_insert(m, new_object, new_pindex) == FALSE) { 1886 panic("vm_page_rename: target exists (%p,%"PRIu64")", 1887 new_object, new_pindex); 1888 } 1889 if (m->queue - m->pc == PQ_CACHE) 1890 vm_page_deactivate(m); 1891 vm_page_dirty(m); 1892 } 1893 1894 /* 1895 * vm_page_unqueue() without any wakeup. This routine is used when a page 1896 * is to remain BUSYied by the caller. 1897 * 1898 * This routine may not block. 1899 */ 1900 void 1901 vm_page_unqueue_nowakeup(vm_page_t m) 1902 { 1903 vm_page_and_queue_spin_lock(m); 1904 (void)_vm_page_rem_queue_spinlocked(m); 1905 vm_page_spin_unlock(m); 1906 } 1907 1908 /* 1909 * vm_page_unqueue() - Remove a page from its queue, wakeup the pagedemon 1910 * if necessary. 1911 * 1912 * This routine may not block. 1913 */ 1914 void 1915 vm_page_unqueue(vm_page_t m) 1916 { 1917 u_short queue; 1918 1919 vm_page_and_queue_spin_lock(m); 1920 queue = _vm_page_rem_queue_spinlocked(m); 1921 if (queue == PQ_FREE || queue == PQ_CACHE) { 1922 vm_page_spin_unlock(m); 1923 pagedaemon_wakeup(); 1924 } else { 1925 vm_page_spin_unlock(m); 1926 } 1927 } 1928 1929 /* 1930 * vm_page_list_find() 1931 * 1932 * Find a page on the specified queue with color optimization. 1933 * 1934 * The page coloring optimization attempts to locate a page that does 1935 * not overload other nearby pages in the object in the cpu's L1 or L2 1936 * caches. We need this optimization because cpu caches tend to be 1937 * physical caches, while object spaces tend to be virtual. 1938 * 1939 * The page coloring optimization also, very importantly, tries to localize 1940 * memory to cpus and physical sockets. 1941 * 1942 * Each PQ_FREE and PQ_CACHE color queue has its own spinlock and the 1943 * algorithm is adjusted to localize allocations on a per-core basis. 1944 * This is done by 'twisting' the colors. 1945 * 1946 * The page is returned spinlocked and removed from its queue (it will 1947 * be on PQ_NONE), or NULL. The page is not BUSY'd. The caller 1948 * is responsible for dealing with the busy-page case (usually by 1949 * deactivating the page and looping). 1950 * 1951 * NOTE: This routine is carefully inlined. A non-inlined version 1952 * is available for outside callers but the only critical path is 1953 * from within this source file. 1954 * 1955 * NOTE: This routine assumes that the vm_pages found in PQ_CACHE and PQ_FREE 1956 * represent stable storage, allowing us to order our locks vm_page 1957 * first, then queue. 1958 */ 1959 static __inline 1960 vm_page_t 1961 _vm_page_list_find(int basequeue, int index) 1962 { 1963 struct vpgqueues *pq; 1964 vm_page_t m; 1965 1966 index &= PQ_L2_MASK; 1967 pq = &vm_page_queues[basequeue + index]; 1968 1969 /* 1970 * Try this cpu's colored queue first. Test for a page unlocked, 1971 * then lock the queue and locate a page. Note that the lock order 1972 * is reversed, but we do not want to dwadle on the page spinlock 1973 * anyway as it is held significantly longer than the queue spinlock. 1974 */ 1975 if (TAILQ_FIRST(&pq->pl)) { 1976 spin_lock(&pq->spin); 1977 TAILQ_FOREACH(m, &pq->pl, pageq) { 1978 if (spin_trylock(&m->spin) == 0) 1979 continue; 1980 KKASSERT(m->queue == basequeue + index); 1981 pq->lastq = -1; 1982 return(m); 1983 } 1984 spin_unlock(&pq->spin); 1985 } 1986 1987 m = _vm_page_list_find_wide(basequeue, index, &pq->lastq); 1988 1989 return(m); 1990 } 1991 1992 /* 1993 * If we could not find the page in the desired queue try to find it in 1994 * a nearby (NUMA-aware) queue, spreading out as we go. 1995 */ 1996 static vm_page_t 1997 _vm_page_list_find_wide(int basequeue, int index, int *lastp) 1998 { 1999 struct vpgqueues *pq; 2000 vm_page_t m = NULL; 2001 int pqmask = set_assoc_mask >> 1; 2002 int pqi; 2003 int range; 2004 int skip_start; 2005 int skip_next; 2006 int count; 2007 2008 /* 2009 * Avoid re-searching empty queues over and over again skip to 2010 * pq->last if appropriate. 2011 */ 2012 if (*lastp >= 0) 2013 index = *lastp; 2014 2015 index &= PQ_L2_MASK; 2016 pq = &vm_page_queues[basequeue]; 2017 count = 0; 2018 skip_start = -1; 2019 skip_next = -1; 2020 2021 /* 2022 * Run local sets of 16, 32, 64, 128, up to the entire queue if all 2023 * else fails (PQ_L2_MASK). 2024 * 2025 * pqmask is a mask, 15, 31, 63, etc. 2026 * 2027 * Test each queue unlocked first, then lock the queue and locate 2028 * a page. Note that the lock order is reversed, but we do not want 2029 * to dwadle on the page spinlock anyway as it is held significantly 2030 * longer than the queue spinlock. 2031 */ 2032 do { 2033 pqmask = (pqmask << 1) | 1; 2034 2035 pqi = index; 2036 range = pqmask + 1; 2037 2038 while (range > 0) { 2039 if (pqi >= skip_start && pqi < skip_next) { 2040 range -= skip_next - pqi; 2041 pqi = (pqi & ~pqmask) | (skip_next & pqmask); 2042 } 2043 if (range > 0 && TAILQ_FIRST(&pq[pqi].pl)) { 2044 spin_lock(&pq[pqi].spin); 2045 TAILQ_FOREACH(m, &pq[pqi].pl, pageq) { 2046 if (spin_trylock(&m->spin) == 0) 2047 continue; 2048 KKASSERT(m->queue == basequeue + pqi); 2049 2050 /* 2051 * If we had to wander too far, set 2052 * *lastp to skip past empty queues. 2053 */ 2054 if (count >= 8) 2055 *lastp = pqi & PQ_L2_MASK; 2056 return(m); 2057 } 2058 spin_unlock(&pq[pqi].spin); 2059 } 2060 --range; 2061 ++count; 2062 pqi = (pqi & ~pqmask) | ((pqi + 1) & pqmask); 2063 } 2064 skip_start = pqi & ~pqmask; 2065 skip_next = (pqi | pqmask) + 1; 2066 } while (pqmask != PQ_L2_MASK); 2067 2068 return(m); 2069 } 2070 2071 static __inline 2072 vm_page_t 2073 _vm_page_list_find2(int bq1, int bq2, int index) 2074 { 2075 struct vpgqueues *pq1; 2076 struct vpgqueues *pq2; 2077 vm_page_t m; 2078 2079 index &= PQ_L2_MASK; 2080 pq1 = &vm_page_queues[bq1 + index]; 2081 pq2 = &vm_page_queues[bq2 + index]; 2082 2083 /* 2084 * Try this cpu's colored queue first. Test for a page unlocked, 2085 * then lock the queue and locate a page. Note that the lock order 2086 * is reversed, but we do not want to dwadle on the page spinlock 2087 * anyway as it is held significantly longer than the queue spinlock. 2088 */ 2089 if (TAILQ_FIRST(&pq1->pl)) { 2090 spin_lock(&pq1->spin); 2091 TAILQ_FOREACH(m, &pq1->pl, pageq) { 2092 if (spin_trylock(&m->spin) == 0) 2093 continue; 2094 KKASSERT(m->queue == bq1 + index); 2095 pq1->lastq = -1; 2096 pq2->lastq = -1; 2097 return(m); 2098 } 2099 spin_unlock(&pq1->spin); 2100 } 2101 2102 m = _vm_page_list_find2_wide(bq1, bq2, index, &pq1->lastq, &pq2->lastq); 2103 2104 return(m); 2105 } 2106 2107 2108 /* 2109 * This version checks two queues at the same time, widening its search 2110 * as we progress. prefering basequeue1 2111 * and starting on basequeue2 after exhausting the first set. The idea 2112 * is to try to stay localized to the cpu. 2113 */ 2114 static vm_page_t 2115 _vm_page_list_find2_wide(int basequeue1, int basequeue2, int index, 2116 int *lastp1, int *lastp2) 2117 { 2118 struct vpgqueues *pq1; 2119 struct vpgqueues *pq2; 2120 vm_page_t m = NULL; 2121 int pqmask1, pqmask2; 2122 int pqi; 2123 int range; 2124 int skip_start1, skip_start2; 2125 int skip_next1, skip_next2; 2126 int count1, count2; 2127 2128 /* 2129 * Avoid re-searching empty queues over and over again skip to 2130 * pq->last if appropriate. 2131 */ 2132 if (*lastp1 >= 0) 2133 index = *lastp1; 2134 2135 index &= PQ_L2_MASK; 2136 2137 pqmask1 = set_assoc_mask >> 1; 2138 pq1 = &vm_page_queues[basequeue1]; 2139 count1 = 0; 2140 skip_start1 = -1; 2141 skip_next1 = -1; 2142 2143 pqmask2 = set_assoc_mask >> 1; 2144 pq2 = &vm_page_queues[basequeue2]; 2145 count2 = 0; 2146 skip_start2 = -1; 2147 skip_next2 = -1; 2148 2149 /* 2150 * Run local sets of 16, 32, 64, 128, up to the entire queue if all 2151 * else fails (PQ_L2_MASK). 2152 * 2153 * pqmask is a mask, 15, 31, 63, etc. 2154 * 2155 * Test each queue unlocked first, then lock the queue and locate 2156 * a page. Note that the lock order is reversed, but we do not want 2157 * to dwadle on the page spinlock anyway as it is held significantly 2158 * longer than the queue spinlock. 2159 */ 2160 do { 2161 if (pqmask1 == PQ_L2_MASK) 2162 goto skip2; 2163 2164 pqmask1 = (pqmask1 << 1) | 1; 2165 pqi = index; 2166 range = pqmask1 + 1; 2167 2168 while (range > 0) { 2169 if (pqi >= skip_start1 && pqi < skip_next1) { 2170 range -= skip_next1 - pqi; 2171 pqi = (pqi & ~pqmask1) | (skip_next1 & pqmask1); 2172 } 2173 if (range > 0 && TAILQ_FIRST(&pq1[pqi].pl)) { 2174 spin_lock(&pq1[pqi].spin); 2175 TAILQ_FOREACH(m, &pq1[pqi].pl, pageq) { 2176 if (spin_trylock(&m->spin) == 0) 2177 continue; 2178 KKASSERT(m->queue == basequeue1 + pqi); 2179 2180 /* 2181 * If we had to wander too far, set 2182 * *lastp to skip past empty queues. 2183 */ 2184 if (count1 >= 8) 2185 *lastp1 = pqi & PQ_L2_MASK; 2186 return(m); 2187 } 2188 spin_unlock(&pq1[pqi].spin); 2189 } 2190 --range; 2191 ++count1; 2192 pqi = (pqi & ~pqmask1) | ((pqi + 1) & pqmask1); 2193 } 2194 skip_start1 = pqi & ~pqmask1; 2195 skip_next1 = (pqi | pqmask1) + 1; 2196 skip2: 2197 if (pqmask1 < ((set_assoc_mask << 1) | 1)) 2198 continue; 2199 2200 pqmask2 = (pqmask2 << 1) | 1; 2201 pqi = index; 2202 range = pqmask2 + 1; 2203 2204 while (range > 0) { 2205 if (pqi >= skip_start2 && pqi < skip_next2) { 2206 range -= skip_next2 - pqi; 2207 pqi = (pqi & ~pqmask2) | (skip_next2 & pqmask2); 2208 } 2209 if (range > 0 && TAILQ_FIRST(&pq2[pqi].pl)) { 2210 spin_lock(&pq2[pqi].spin); 2211 TAILQ_FOREACH(m, &pq2[pqi].pl, pageq) { 2212 if (spin_trylock(&m->spin) == 0) 2213 continue; 2214 KKASSERT(m->queue == basequeue2 + pqi); 2215 2216 /* 2217 * If we had to wander too far, set 2218 * *lastp to skip past empty queues. 2219 */ 2220 if (count2 >= 8) 2221 *lastp2 = pqi & PQ_L2_MASK; 2222 return(m); 2223 } 2224 spin_unlock(&pq2[pqi].spin); 2225 } 2226 --range; 2227 ++count2; 2228 pqi = (pqi & ~pqmask2) | ((pqi + 1) & pqmask2); 2229 } 2230 skip_start2 = pqi & ~pqmask2; 2231 skip_next2 = (pqi | pqmask2) + 1; 2232 } while (pqmask1 != PQ_L2_MASK && pqmask2 != PQ_L2_MASK); 2233 2234 return(m); 2235 } 2236 2237 /* 2238 * Returns a vm_page candidate for allocation. The page is not busied so 2239 * it can move around. The caller must busy the page (and typically 2240 * deactivate it if it cannot be busied!) 2241 * 2242 * Returns a spinlocked vm_page that has been removed from its queue. 2243 * (note that _vm_page_list_find() does not remove the page from its 2244 * queue). 2245 */ 2246 vm_page_t 2247 vm_page_list_find(int basequeue, int index) 2248 { 2249 vm_page_t m; 2250 2251 m = _vm_page_list_find(basequeue, index); 2252 if (m) 2253 _vm_page_rem_queue_spinlocked(m); 2254 return m; 2255 } 2256 2257 /* 2258 * Find a page on the cache queue with color optimization, remove it 2259 * from the queue, and busy it. The returned page will not be spinlocked. 2260 * 2261 * A candidate failure will be deactivated. Candidates can fail due to 2262 * being busied by someone else, in which case they will be deactivated. 2263 * 2264 * This routine may not block. 2265 * 2266 */ 2267 static vm_page_t 2268 vm_page_select_cache(u_short pg_color) 2269 { 2270 vm_page_t m; 2271 2272 for (;;) { 2273 m = _vm_page_list_find(PQ_CACHE, pg_color); 2274 if (m == NULL) 2275 break; 2276 /* 2277 * (m) has been spinlocked 2278 */ 2279 _vm_page_rem_queue_spinlocked(m); 2280 if (vm_page_busy_try(m, TRUE)) { 2281 _vm_page_deactivate_locked(m, 0); 2282 vm_page_spin_unlock(m); 2283 } else { 2284 /* 2285 * We successfully busied the page 2286 */ 2287 if ((m->flags & PG_NEED_COMMIT) == 0 && 2288 m->hold_count == 0 && 2289 m->wire_count == 0 && 2290 (m->dirty & m->valid) == 0) { 2291 vm_page_spin_unlock(m); 2292 KKASSERT((m->flags & PG_UNQUEUED) == 0); 2293 pagedaemon_wakeup(); 2294 return(m); 2295 } 2296 2297 /* 2298 * The page cannot be recycled, deactivate it. 2299 */ 2300 _vm_page_deactivate_locked(m, 0); 2301 if (_vm_page_wakeup(m)) { 2302 vm_page_spin_unlock(m); 2303 wakeup(m); 2304 } else { 2305 vm_page_spin_unlock(m); 2306 } 2307 } 2308 } 2309 return (m); 2310 } 2311 2312 /* 2313 * Find a free page. We attempt to inline the nominal case and fall back 2314 * to _vm_page_select_free() otherwise. A busied page is removed from 2315 * the queue and returned. 2316 * 2317 * This routine may not block. 2318 */ 2319 static __inline vm_page_t 2320 vm_page_select_free(u_short pg_color) 2321 { 2322 vm_page_t m; 2323 2324 for (;;) { 2325 m = _vm_page_list_find(PQ_FREE, pg_color); 2326 if (m == NULL) 2327 break; 2328 _vm_page_rem_queue_spinlocked(m); 2329 if (vm_page_busy_try(m, TRUE)) { 2330 /* 2331 * Various mechanisms such as a pmap_collect can 2332 * result in a busy page on the free queue. We 2333 * have to move the page out of the way so we can 2334 * retry the allocation. If the other thread is not 2335 * allocating the page then m->valid will remain 0 and 2336 * the pageout daemon will free the page later on. 2337 * 2338 * Since we could not busy the page, however, we 2339 * cannot make assumptions as to whether the page 2340 * will be allocated by the other thread or not, 2341 * so all we can do is deactivate it to move it out 2342 * of the way. In particular, if the other thread 2343 * wires the page it may wind up on the inactive 2344 * queue and the pageout daemon will have to deal 2345 * with that case too. 2346 */ 2347 _vm_page_deactivate_locked(m, 0); 2348 vm_page_spin_unlock(m); 2349 } else { 2350 /* 2351 * Theoretically if we are able to busy the page 2352 * atomic with the queue removal (using the vm_page 2353 * lock) nobody else should have been able to mess 2354 * with the page before us. 2355 * 2356 * Assert the page state. Note that even though 2357 * wiring doesn't adjust queues, a page on the free 2358 * queue should never be wired at this point. 2359 */ 2360 KKASSERT((m->flags & (PG_UNQUEUED | 2361 PG_NEED_COMMIT)) == 0); 2362 KASSERT(m->hold_count == 0, 2363 ("m->hold_count is not zero " 2364 "pg %p q=%d flags=%08x hold=%d wire=%d", 2365 m, m->queue, m->flags, 2366 m->hold_count, m->wire_count)); 2367 KKASSERT(m->wire_count == 0); 2368 vm_page_spin_unlock(m); 2369 pagedaemon_wakeup(); 2370 2371 /* return busied and removed page */ 2372 return(m); 2373 } 2374 } 2375 return(m); 2376 } 2377 2378 static __inline vm_page_t 2379 vm_page_select_free_or_cache(u_short pg_color, int *fromcachep) 2380 { 2381 vm_page_t m; 2382 2383 *fromcachep = 0; 2384 for (;;) { 2385 m = _vm_page_list_find2(PQ_FREE, PQ_CACHE, pg_color); 2386 if (m == NULL) 2387 break; 2388 if (vm_page_busy_try(m, TRUE)) { 2389 _vm_page_rem_queue_spinlocked(m); 2390 _vm_page_deactivate_locked(m, 0); 2391 vm_page_spin_unlock(m); 2392 } else if (m->queue - m->pc == PQ_FREE) { 2393 /* 2394 * We successfully busied the page, PQ_FREE case 2395 */ 2396 _vm_page_rem_queue_spinlocked(m); 2397 KKASSERT((m->flags & (PG_UNQUEUED | 2398 PG_NEED_COMMIT)) == 0); 2399 KASSERT(m->hold_count == 0, 2400 ("m->hold_count is not zero " 2401 "pg %p q=%d flags=%08x hold=%d wire=%d", 2402 m, m->queue, m->flags, 2403 m->hold_count, m->wire_count)); 2404 KKASSERT(m->wire_count == 0); 2405 vm_page_spin_unlock(m); 2406 pagedaemon_wakeup(); 2407 2408 /* return busied and removed page */ 2409 return(m); 2410 } else { 2411 /* 2412 * We successfully busied the page, PQ_CACHE case 2413 */ 2414 _vm_page_rem_queue_spinlocked(m); 2415 if ((m->flags & PG_NEED_COMMIT) == 0 && 2416 m->hold_count == 0 && 2417 m->wire_count == 0 && 2418 (m->dirty & m->valid) == 0) { 2419 vm_page_spin_unlock(m); 2420 KKASSERT((m->flags & PG_UNQUEUED) == 0); 2421 pagedaemon_wakeup(); 2422 *fromcachep = 1; 2423 return(m); 2424 } 2425 2426 /* 2427 * The page cannot be recycled, deactivate it. 2428 */ 2429 _vm_page_deactivate_locked(m, 0); 2430 if (_vm_page_wakeup(m)) { 2431 vm_page_spin_unlock(m); 2432 wakeup(m); 2433 } else { 2434 vm_page_spin_unlock(m); 2435 } 2436 } 2437 } 2438 return(m); 2439 } 2440 2441 /* 2442 * vm_page_alloc() 2443 * 2444 * Allocate and return a memory cell associated with this VM object/offset 2445 * pair. If object is NULL an unassociated page will be allocated. 2446 * 2447 * The returned page will be busied and removed from its queues. This 2448 * routine can block and may return NULL if a race occurs and the page 2449 * is found to already exist at the specified (object, pindex). 2450 * 2451 * VM_ALLOC_NORMAL allow use of cache pages, nominal free drain 2452 * VM_ALLOC_QUICK like normal but cannot use cache 2453 * VM_ALLOC_SYSTEM greater free drain 2454 * VM_ALLOC_INTERRUPT allow free list to be completely drained 2455 * VM_ALLOC_ZERO advisory request for pre-zero'd page only 2456 * VM_ALLOC_FORCE_ZERO advisory request for pre-zero'd page only 2457 * VM_ALLOC_NULL_OK ok to return NULL on insertion collision 2458 * (see vm_page_grab()) 2459 * VM_ALLOC_USE_GD ok to use per-gd cache 2460 * 2461 * VM_ALLOC_CPU(n) allocate using specified cpu localization 2462 * 2463 * The object must be held if not NULL 2464 * This routine may not block 2465 * 2466 * Additional special handling is required when called from an interrupt 2467 * (VM_ALLOC_INTERRUPT). We are not allowed to mess with the page cache 2468 * in this case. 2469 */ 2470 vm_page_t 2471 vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int page_req) 2472 { 2473 globaldata_t gd; 2474 vm_object_t obj; 2475 vm_page_t m; 2476 u_short pg_color; 2477 int cpuid_local; 2478 int fromcache; 2479 2480 #if 0 2481 /* 2482 * Special per-cpu free VM page cache. The pages are pre-busied 2483 * and pre-zerod for us. 2484 */ 2485 if (gd->gd_vmpg_count && (page_req & VM_ALLOC_USE_GD)) { 2486 crit_enter_gd(gd); 2487 if (gd->gd_vmpg_count) { 2488 m = gd->gd_vmpg_array[--gd->gd_vmpg_count]; 2489 crit_exit_gd(gd); 2490 goto done; 2491 } 2492 crit_exit_gd(gd); 2493 } 2494 #endif 2495 m = NULL; 2496 2497 /* 2498 * CPU LOCALIZATION 2499 * 2500 * CPU localization algorithm. Break the page queues up by physical 2501 * id and core id (note that two cpu threads will have the same core 2502 * id, and core_id != gd_cpuid). 2503 * 2504 * This is nowhere near perfect, for example the last pindex in a 2505 * subgroup will overflow into the next cpu or package. But this 2506 * should get us good page reuse locality in heavy mixed loads. 2507 * 2508 * (may be executed before the APs are started, so other GDs might 2509 * not exist!) 2510 */ 2511 if (page_req & VM_ALLOC_CPU_SPEC) 2512 cpuid_local = VM_ALLOC_GETCPU(page_req); 2513 else 2514 cpuid_local = mycpu->gd_cpuid; 2515 2516 pg_color = vm_get_pg_color(cpuid_local, object, pindex); 2517 2518 KKASSERT(page_req & 2519 (VM_ALLOC_NORMAL|VM_ALLOC_QUICK| 2520 VM_ALLOC_INTERRUPT|VM_ALLOC_SYSTEM)); 2521 2522 /* 2523 * Certain system threads (pageout daemon, buf_daemon's) are 2524 * allowed to eat deeper into the free page list. 2525 */ 2526 if (curthread->td_flags & TDF_SYSTHREAD) 2527 page_req |= VM_ALLOC_SYSTEM; 2528 2529 /* 2530 * Impose various limitations. Note that the v_free_reserved test 2531 * must match the opposite of vm_page_count_target() to avoid 2532 * livelocks, be careful. 2533 */ 2534 loop: 2535 gd = mycpu; 2536 if (gd->gd_vmstats.v_free_count >= gd->gd_vmstats.v_free_reserved || 2537 ((page_req & VM_ALLOC_INTERRUPT) && 2538 gd->gd_vmstats.v_free_count > 0) || 2539 ((page_req & VM_ALLOC_SYSTEM) && 2540 gd->gd_vmstats.v_cache_count == 0 && 2541 gd->gd_vmstats.v_free_count > 2542 gd->gd_vmstats.v_interrupt_free_min) 2543 ) { 2544 /* 2545 * The free queue has sufficient free pages to take one out. 2546 * 2547 * However, if the free queue is strained the scan may widen 2548 * to the entire queue and cause a great deal of SMP 2549 * contention, so we use a double-queue-scan if we can 2550 * to avoid this. 2551 */ 2552 if (page_req & VM_ALLOC_NORMAL) { 2553 m = vm_page_select_free_or_cache(pg_color, &fromcache); 2554 if (m && fromcache) 2555 goto found_cache; 2556 } else { 2557 m = vm_page_select_free(pg_color); 2558 } 2559 } else if (page_req & VM_ALLOC_NORMAL) { 2560 /* 2561 * Allocatable from the cache (non-interrupt only). On 2562 * success, we must free the page and try again, thus 2563 * ensuring that vmstats.v_*_free_min counters are replenished. 2564 */ 2565 #ifdef INVARIANTS 2566 if (curthread->td_preempted) { 2567 kprintf("vm_page_alloc(): warning, attempt to allocate" 2568 " cache page from preempting interrupt\n"); 2569 m = NULL; 2570 } else { 2571 m = vm_page_select_cache(pg_color); 2572 } 2573 #else 2574 m = vm_page_select_cache(pg_color); 2575 #endif 2576 /* 2577 * On success move the page into the free queue and loop. 2578 * 2579 * Only do this if we can safely acquire the vm_object lock, 2580 * because this is effectively a random page and the caller 2581 * might be holding the lock shared, we don't want to 2582 * deadlock. 2583 */ 2584 if (m != NULL) { 2585 found_cache: 2586 KASSERT(m->dirty == 0, 2587 ("Found dirty cache page %p", m)); 2588 if ((obj = m->object) != NULL) { 2589 if (vm_object_hold_try(obj)) { 2590 vm_page_protect(m, VM_PROT_NONE); 2591 vm_page_free(m); 2592 /* m->object NULL here */ 2593 vm_object_drop(obj); 2594 } else { 2595 vm_page_deactivate(m); 2596 vm_page_wakeup(m); 2597 } 2598 } else { 2599 vm_page_protect(m, VM_PROT_NONE); 2600 vm_page_free(m); 2601 } 2602 goto loop; 2603 } 2604 2605 /* 2606 * On failure return NULL 2607 */ 2608 atomic_add_int(&vm_pageout_deficit, 1); 2609 pagedaemon_wakeup(); 2610 return (NULL); 2611 } else { 2612 /* 2613 * No pages available, wakeup the pageout daemon and give up. 2614 */ 2615 atomic_add_int(&vm_pageout_deficit, 1); 2616 pagedaemon_wakeup(); 2617 return (NULL); 2618 } 2619 2620 /* 2621 * v_free_count can race so loop if we don't find the expected 2622 * page. 2623 */ 2624 if (m == NULL) { 2625 vmstats_rollup(); 2626 goto loop; 2627 } 2628 2629 /* 2630 * Good page found. The page has already been busied for us and 2631 * removed from its queues. 2632 */ 2633 KASSERT(m->dirty == 0, 2634 ("vm_page_alloc: free/cache page %p was dirty", m)); 2635 KKASSERT(m->queue == PQ_NONE); 2636 2637 #if 0 2638 done: 2639 #endif 2640 /* 2641 * Initialize the structure, inheriting some flags but clearing 2642 * all the rest. The page has already been busied for us. 2643 */ 2644 vm_page_flag_clear(m, ~PG_KEEP_NEWPAGE_MASK); 2645 2646 KKASSERT(m->wire_count == 0); 2647 KKASSERT((m->busy_count & PBUSY_MASK) == 0); 2648 m->act_count = 0; 2649 m->valid = 0; 2650 2651 /* 2652 * Caller must be holding the object lock (asserted by 2653 * vm_page_insert()). 2654 * 2655 * NOTE: Inserting a page here does not insert it into any pmaps 2656 * (which could cause us to block allocating memory). 2657 * 2658 * NOTE: If no object an unassociated page is allocated, m->pindex 2659 * can be used by the caller for any purpose. 2660 */ 2661 if (object) { 2662 if (vm_page_insert(m, object, pindex) == FALSE) { 2663 vm_page_free(m); 2664 if ((page_req & VM_ALLOC_NULL_OK) == 0) 2665 panic("PAGE RACE %p[%ld]/%p", 2666 object, (long)pindex, m); 2667 m = NULL; 2668 } 2669 } else { 2670 m->pindex = pindex; 2671 } 2672 2673 /* 2674 * Don't wakeup too often - wakeup the pageout daemon when 2675 * we would be nearly out of memory. 2676 */ 2677 pagedaemon_wakeup(); 2678 2679 /* 2680 * A BUSY page is returned. 2681 */ 2682 return (m); 2683 } 2684 2685 /* 2686 * Returns number of pages available in our DMA memory reserve 2687 * (adjusted with vm.dma_reserved=<value>m in /boot/loader.conf) 2688 */ 2689 vm_size_t 2690 vm_contig_avail_pages(void) 2691 { 2692 alist_blk_t blk; 2693 alist_blk_t count; 2694 alist_blk_t bfree; 2695 spin_lock(&vm_contig_spin); 2696 bfree = alist_free_info(&vm_contig_alist, &blk, &count); 2697 spin_unlock(&vm_contig_spin); 2698 2699 return bfree; 2700 } 2701 2702 /* 2703 * Attempt to allocate contiguous physical memory with the specified 2704 * requirements. 2705 */ 2706 vm_page_t 2707 vm_page_alloc_contig(vm_paddr_t low, vm_paddr_t high, 2708 unsigned long alignment, unsigned long boundary, 2709 unsigned long size, vm_memattr_t memattr) 2710 { 2711 alist_blk_t blk; 2712 vm_page_t m; 2713 vm_pindex_t i; 2714 #if 0 2715 static vm_pindex_t contig_rover; 2716 #endif 2717 2718 alignment >>= PAGE_SHIFT; 2719 if (alignment == 0) 2720 alignment = 1; 2721 boundary >>= PAGE_SHIFT; 2722 if (boundary == 0) 2723 boundary = 1; 2724 size = (size + PAGE_MASK) >> PAGE_SHIFT; 2725 2726 #if 0 2727 /* 2728 * Disabled temporarily until we find a solution for DRM (a flag 2729 * to always use the free space reserve, for performance). 2730 */ 2731 if (high == BUS_SPACE_MAXADDR && alignment <= PAGE_SIZE && 2732 boundary <= PAGE_SIZE && size == 1 && 2733 memattr == VM_MEMATTR_DEFAULT) { 2734 /* 2735 * Any page will work, use vm_page_alloc() 2736 * (e.g. when used from kmem_alloc_attr()) 2737 */ 2738 m = vm_page_alloc(NULL, (contig_rover++) & 0x7FFFFFFF, 2739 VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM | 2740 VM_ALLOC_INTERRUPT); 2741 m->valid = VM_PAGE_BITS_ALL; 2742 vm_page_wire(m); 2743 vm_page_wakeup(m); 2744 } else 2745 #endif 2746 { 2747 /* 2748 * Use the low-memory dma reserve 2749 */ 2750 spin_lock(&vm_contig_spin); 2751 blk = alist_alloc(&vm_contig_alist, 0, size); 2752 if (blk == ALIST_BLOCK_NONE) { 2753 spin_unlock(&vm_contig_spin); 2754 if (bootverbose) { 2755 kprintf("vm_page_alloc_contig: %ldk nospace\n", 2756 (size << PAGE_SHIFT) / 1024); 2757 print_backtrace(5); 2758 } 2759 return(NULL); 2760 } 2761 if (high && ((vm_paddr_t)(blk + size) << PAGE_SHIFT) > high) { 2762 alist_free(&vm_contig_alist, blk, size); 2763 spin_unlock(&vm_contig_spin); 2764 if (bootverbose) { 2765 kprintf("vm_page_alloc_contig: %ldk high " 2766 "%016jx failed\n", 2767 (size << PAGE_SHIFT) / 1024, 2768 (intmax_t)high); 2769 } 2770 return(NULL); 2771 } 2772 spin_unlock(&vm_contig_spin); 2773 2774 /* 2775 * Base vm_page_t of range 2776 */ 2777 m = PHYS_TO_VM_PAGE((vm_paddr_t)blk << PAGE_SHIFT); 2778 } 2779 if (vm_contig_verbose) { 2780 kprintf("vm_page_alloc_contig: %016jx/%ldk " 2781 "(%016jx-%016jx al=%lu bo=%lu pgs=%lu attr=%d\n", 2782 (intmax_t)m->phys_addr, 2783 (size << PAGE_SHIFT) / 1024, 2784 low, high, alignment, boundary, size, memattr); 2785 } 2786 if (memattr != VM_MEMATTR_DEFAULT) { 2787 for (i = 0; i < size; ++i) { 2788 KKASSERT(m[i].flags & PG_FICTITIOUS); 2789 pmap_page_set_memattr(&m[i], memattr); 2790 } 2791 } 2792 return m; 2793 } 2794 2795 /* 2796 * Free contiguously allocated pages. The pages will be wired but not busy. 2797 * When freeing to the alist we leave them wired and not busy. 2798 */ 2799 void 2800 vm_page_free_contig(vm_page_t m, unsigned long size) 2801 { 2802 vm_paddr_t pa = VM_PAGE_TO_PHYS(m); 2803 vm_pindex_t start = pa >> PAGE_SHIFT; 2804 vm_pindex_t pages = (size + PAGE_MASK) >> PAGE_SHIFT; 2805 2806 if (vm_contig_verbose) { 2807 kprintf("vm_page_free_contig: %016jx/%ldk\n", 2808 (intmax_t)pa, size / 1024); 2809 } 2810 if (pa < vm_low_phys_reserved) { 2811 /* 2812 * Just assert check the first page for convenience. 2813 */ 2814 KKASSERT(m->wire_count == 1); 2815 KKASSERT(m->flags & PG_FICTITIOUS); 2816 KKASSERT(pa + size <= vm_low_phys_reserved); 2817 spin_lock(&vm_contig_spin); 2818 alist_free(&vm_contig_alist, start, pages); 2819 spin_unlock(&vm_contig_spin); 2820 } else { 2821 while (pages) { 2822 /* XXX FUTURE, maybe (pair with vm_pg_contig_alloc()) */ 2823 /*vm_page_flag_clear(m, PG_FICTITIOUS | PG_UNQUEUED);*/ 2824 vm_page_busy_wait(m, FALSE, "cpgfr"); 2825 vm_page_unwire(m, 0); 2826 vm_page_free(m); 2827 --pages; 2828 ++m; 2829 } 2830 2831 } 2832 } 2833 2834 2835 /* 2836 * Wait for sufficient free memory for nominal heavy memory use kernel 2837 * operations. 2838 * 2839 * WARNING! Be sure never to call this in any vm_pageout code path, which 2840 * will trivially deadlock the system. 2841 */ 2842 void 2843 vm_wait_nominal(void) 2844 { 2845 while (vm_page_count_min(0)) 2846 vm_wait(0); 2847 } 2848 2849 /* 2850 * Test if vm_wait_nominal() would block. 2851 */ 2852 int 2853 vm_test_nominal(void) 2854 { 2855 if (vm_page_count_min(0)) 2856 return(1); 2857 return(0); 2858 } 2859 2860 /* 2861 * Block until free pages are available for allocation, called in various 2862 * places before memory allocations. 2863 * 2864 * The caller may loop if vm_page_count_min() == FALSE so we cannot be 2865 * more generous then that. 2866 */ 2867 void 2868 vm_wait(int timo) 2869 { 2870 /* 2871 * never wait forever 2872 */ 2873 if (timo == 0) 2874 timo = hz; 2875 lwkt_gettoken(&vm_token); 2876 2877 if (curthread == pagethread || 2878 curthread == emergpager) { 2879 /* 2880 * The pageout daemon itself needs pages, this is bad. 2881 */ 2882 if (vm_page_count_min(0)) { 2883 vm_pageout_pages_needed = 1; 2884 tsleep(&vm_pageout_pages_needed, 0, "VMWait", timo); 2885 } 2886 } else { 2887 /* 2888 * Wakeup the pageout daemon if necessary and wait. 2889 * 2890 * Do not wait indefinitely for the target to be reached, 2891 * as load might prevent it from being reached any time soon. 2892 * But wait a little to try to slow down page allocations 2893 * and to give more important threads (the pagedaemon) 2894 * allocation priority. 2895 */ 2896 if (vm_page_count_target()) { 2897 if (vm_pages_needed <= 1) { 2898 ++vm_pages_needed; 2899 wakeup(&vm_pages_needed); 2900 } 2901 ++vm_pages_waiting; /* SMP race ok */ 2902 tsleep(&vmstats.v_free_count, 0, "vmwait", timo); 2903 } 2904 } 2905 lwkt_reltoken(&vm_token); 2906 } 2907 2908 /* 2909 * Block until free pages are available for allocation 2910 * 2911 * Called only from vm_fault so that processes page faulting can be 2912 * easily tracked. 2913 */ 2914 void 2915 vm_wait_pfault(void) 2916 { 2917 /* 2918 * Wakeup the pageout daemon if necessary and wait. 2919 * 2920 * Do not wait indefinitely for the target to be reached, 2921 * as load might prevent it from being reached any time soon. 2922 * But wait a little to try to slow down page allocations 2923 * and to give more important threads (the pagedaemon) 2924 * allocation priority. 2925 */ 2926 if (vm_page_count_min(0)) { 2927 lwkt_gettoken(&vm_token); 2928 while (vm_page_count_severe()) { 2929 if (vm_page_count_target()) { 2930 thread_t td; 2931 2932 if (vm_pages_needed <= 1) { 2933 ++vm_pages_needed; 2934 wakeup(&vm_pages_needed); 2935 } 2936 ++vm_pages_waiting; /* SMP race ok */ 2937 tsleep(&vmstats.v_free_count, 0, "pfault", hz); 2938 2939 /* 2940 * Do not stay stuck in the loop if the system is trying 2941 * to kill the process. 2942 */ 2943 td = curthread; 2944 if (td->td_proc && (td->td_proc->p_flags & P_LOWMEMKILL)) 2945 break; 2946 } 2947 } 2948 lwkt_reltoken(&vm_token); 2949 } 2950 } 2951 2952 /* 2953 * Put the specified page on the active list (if appropriate). Ensure 2954 * that act_count is at least ACT_INIT but do not otherwise mess with it. 2955 * 2956 * The caller should be holding the page busied ? XXX 2957 * This routine may not block. 2958 * 2959 * It is ok if the page is wired (so buffer cache operations don't have 2960 * to mess with the page queues). 2961 */ 2962 void 2963 vm_page_activate(vm_page_t m) 2964 { 2965 u_short oqueue; 2966 2967 /* 2968 * If already active or inappropriate, just set act_count and 2969 * return. We don't have to spin-lock the page. 2970 */ 2971 if (m->queue - m->pc == PQ_ACTIVE || 2972 (m->flags & (PG_FICTITIOUS | PG_UNQUEUED))) { 2973 if (m->act_count < ACT_INIT) 2974 m->act_count = ACT_INIT; 2975 return; 2976 } 2977 2978 vm_page_spin_lock(m); 2979 if (m->queue - m->pc != PQ_ACTIVE && 2980 (m->flags & (PG_FICTITIOUS | PG_UNQUEUED)) == 0) { 2981 _vm_page_queue_spin_lock(m); 2982 oqueue = _vm_page_rem_queue_spinlocked(m); 2983 /* page is left spinlocked, queue is unlocked */ 2984 2985 if (oqueue == PQ_CACHE) 2986 mycpu->gd_cnt.v_reactivated++; 2987 if (m->act_count < ACT_INIT) 2988 m->act_count = ACT_INIT; 2989 _vm_page_add_queue_spinlocked(m, PQ_ACTIVE + m->pc, 0); 2990 _vm_page_and_queue_spin_unlock(m); 2991 if (oqueue == PQ_CACHE || oqueue == PQ_FREE) 2992 pagedaemon_wakeup(); 2993 } else { 2994 if (m->act_count < ACT_INIT) 2995 m->act_count = ACT_INIT; 2996 vm_page_spin_unlock(m); 2997 } 2998 } 2999 3000 void 3001 vm_page_soft_activate(vm_page_t m) 3002 { 3003 if (m->queue - m->pc == PQ_ACTIVE || 3004 (m->flags & (PG_FICTITIOUS | PG_UNQUEUED))) { 3005 if (m->act_count < ACT_INIT) 3006 m->act_count = ACT_INIT; 3007 } else { 3008 vm_page_activate(m); 3009 } 3010 } 3011 3012 /* 3013 * Helper routine for vm_page_free_toq() and vm_page_cache(). This 3014 * routine is called when a page has been added to the cache or free 3015 * queues. 3016 * 3017 * This routine may not block. 3018 */ 3019 static __inline void 3020 vm_page_free_wakeup(void) 3021 { 3022 globaldata_t gd = mycpu; 3023 3024 /* 3025 * If the pageout daemon itself needs pages, then tell it that 3026 * there are some free. 3027 */ 3028 if (vm_pageout_pages_needed && 3029 gd->gd_vmstats.v_cache_count + gd->gd_vmstats.v_free_count >= 3030 gd->gd_vmstats.v_pageout_free_min 3031 ) { 3032 vm_pageout_pages_needed = 0; 3033 wakeup(&vm_pageout_pages_needed); 3034 } 3035 3036 /* 3037 * Wakeup processes that are waiting on memory. 3038 * 3039 * Generally speaking we want to wakeup stuck processes as soon as 3040 * possible. !vm_page_count_min(0) is the absolute minimum point 3041 * where we can do this. Wait a bit longer to reduce degenerate 3042 * re-blocking (vm_page_free_hysteresis). The target check is just 3043 * to make sure the min-check w/hysteresis does not exceed the 3044 * normal target. 3045 */ 3046 if (vm_pages_waiting) { 3047 if (!vm_page_count_min(vm_page_free_hysteresis) || 3048 !vm_page_count_target()) { 3049 vm_pages_waiting = 0; 3050 wakeup(&vmstats.v_free_count); 3051 ++mycpu->gd_cnt.v_ppwakeups; 3052 } 3053 #if 0 3054 if (!vm_page_count_target()) { 3055 /* 3056 * Plenty of pages are free, wakeup everyone. 3057 */ 3058 vm_pages_waiting = 0; 3059 wakeup(&vmstats.v_free_count); 3060 ++mycpu->gd_cnt.v_ppwakeups; 3061 } else if (!vm_page_count_min(0)) { 3062 /* 3063 * Some pages are free, wakeup someone. 3064 */ 3065 int wcount = vm_pages_waiting; 3066 if (wcount > 0) 3067 --wcount; 3068 vm_pages_waiting = wcount; 3069 wakeup_one(&vmstats.v_free_count); 3070 ++mycpu->gd_cnt.v_ppwakeups; 3071 } 3072 #endif 3073 } 3074 } 3075 3076 /* 3077 * Returns the given page to the PQ_FREE or PQ_HOLD list and disassociates 3078 * it from its VM object. 3079 * 3080 * The vm_page must be BUSY on entry. BUSY will be released on 3081 * return (the page will have been freed). 3082 */ 3083 void 3084 vm_page_free_toq(vm_page_t m) 3085 { 3086 mycpu->gd_cnt.v_tfree++; 3087 if (m->flags & (PG_MAPPED | PG_WRITEABLE)) 3088 pmap_mapped_sync(m); 3089 KKASSERT((m->flags & PG_MAPPED) == 0); 3090 KKASSERT(m->busy_count & PBUSY_LOCKED); 3091 3092 if ((m->busy_count & PBUSY_MASK) || ((m->queue - m->pc) == PQ_FREE)) { 3093 kprintf("vm_page_free: pindex(%lu), busy %08x, " 3094 "hold(%d)\n", 3095 (u_long)m->pindex, m->busy_count, m->hold_count); 3096 if ((m->queue - m->pc) == PQ_FREE) 3097 panic("vm_page_free: freeing free page"); 3098 else 3099 panic("vm_page_free: freeing busy page"); 3100 } 3101 3102 /* 3103 * Remove from object, spinlock the page and its queues and 3104 * remove from any queue. No queue spinlock will be held 3105 * after this section (because the page was removed from any 3106 * queue). 3107 */ 3108 vm_page_remove(m); 3109 3110 /* 3111 * No further management of fictitious pages occurs beyond object 3112 * and queue removal. 3113 */ 3114 if ((m->flags & PG_FICTITIOUS) != 0) { 3115 KKASSERT(m->queue == PQ_NONE); 3116 vm_page_wakeup(m); 3117 return; 3118 } 3119 vm_page_and_queue_spin_lock(m); 3120 _vm_page_rem_queue_spinlocked(m); 3121 3122 m->valid = 0; 3123 vm_page_undirty(m); 3124 3125 if (m->wire_count != 0) { 3126 if (m->wire_count > 1) { 3127 panic( 3128 "vm_page_free: invalid wire count (%d), pindex: 0x%lx", 3129 m->wire_count, (long)m->pindex); 3130 } 3131 panic("vm_page_free: freeing wired page"); 3132 } 3133 3134 if (!MD_PAGE_FREEABLE(m)) 3135 panic("vm_page_free: page %p is still mapped!", m); 3136 3137 /* 3138 * Clear the PG_NEED_COMMIT and the PG_UNQUEUED flags. The 3139 * page returns to normal operation and will be placed in 3140 * the PQ_HOLD or PQ_FREE queue. 3141 */ 3142 vm_page_flag_clear(m, PG_NEED_COMMIT | PG_UNQUEUED); 3143 3144 if (m->hold_count != 0) { 3145 _vm_page_add_queue_spinlocked(m, PQ_HOLD + m->pc, 0); 3146 } else { 3147 _vm_page_add_queue_spinlocked(m, PQ_FREE + m->pc, 1); 3148 } 3149 3150 /* 3151 * This sequence allows us to clear BUSY while still holding 3152 * its spin lock, which reduces contention vs allocators. We 3153 * must not leave the queue locked or _vm_page_wakeup() may 3154 * deadlock. 3155 */ 3156 _vm_page_queue_spin_unlock(m); 3157 if (_vm_page_wakeup(m)) { 3158 vm_page_spin_unlock(m); 3159 wakeup(m); 3160 } else { 3161 vm_page_spin_unlock(m); 3162 } 3163 vm_page_free_wakeup(); 3164 } 3165 3166 /* 3167 * Mark this page as wired down by yet another map. We do not adjust the 3168 * queue the page is on, it will be checked for wiring as-needed. 3169 * 3170 * This function has no effect on fictitious pages. 3171 * 3172 * Caller must be holding the page busy. 3173 */ 3174 void 3175 vm_page_wire(vm_page_t m) 3176 { 3177 KKASSERT(m->busy_count & PBUSY_LOCKED); 3178 if ((m->flags & PG_FICTITIOUS) == 0) { 3179 if (atomic_fetchadd_int(&m->wire_count, 1) == 0) { 3180 atomic_add_long(&mycpu->gd_vmstats_adj.v_wire_count, 1); 3181 } 3182 KASSERT(m->wire_count != 0, 3183 ("vm_page_wire: wire_count overflow m=%p", m)); 3184 } 3185 } 3186 3187 /* 3188 * Release one wiring of this page, potentially enabling it to be paged again. 3189 * 3190 * Note that wired pages are no longer unconditionally removed from the 3191 * paging queues, so the page may already be on a queue. Move the page 3192 * to the desired queue if necessary. 3193 * 3194 * Many pages placed on the inactive queue should actually go 3195 * into the cache, but it is difficult to figure out which. What 3196 * we do instead, if the inactive target is well met, is to put 3197 * clean pages at the head of the inactive queue instead of the tail. 3198 * This will cause them to be moved to the cache more quickly and 3199 * if not actively re-referenced, freed more quickly. If we just 3200 * stick these pages at the end of the inactive queue, heavy filesystem 3201 * meta-data accesses can cause an unnecessary paging load on memory bound 3202 * processes. This optimization causes one-time-use metadata to be 3203 * reused more quickly. 3204 * 3205 * Pages marked PG_NEED_COMMIT are always activated and never placed on 3206 * the inactive queue. This helps the pageout daemon determine memory 3207 * pressure and act on out-of-memory situations more quickly. 3208 * 3209 * BUT, if we are in a low-memory situation we have no choice but to 3210 * put clean pages on the cache queue. 3211 * 3212 * A number of routines use vm_page_unwire() to guarantee that the page 3213 * will go into either the inactive or active queues, and will NEVER 3214 * be placed in the cache - for example, just after dirtying a page. 3215 * dirty pages in the cache are not allowed. 3216 * 3217 * PG_FICTITIOUS or PG_UNQUEUED pages are never moved to any queue, and 3218 * the wire_count will not be adjusted in any way for a PG_FICTITIOUS 3219 * page. 3220 * 3221 * This routine may not block. 3222 */ 3223 void 3224 vm_page_unwire(vm_page_t m, int activate) 3225 { 3226 KKASSERT(m->busy_count & PBUSY_LOCKED); 3227 if (m->flags & PG_FICTITIOUS) { 3228 /* do nothing */ 3229 } else if ((int)m->wire_count <= 0) { 3230 panic("vm_page_unwire: invalid wire count: %d", m->wire_count); 3231 } else { 3232 if (atomic_fetchadd_int(&m->wire_count, -1) == 1) { 3233 atomic_add_long(&mycpu->gd_vmstats_adj.v_wire_count,-1); 3234 if (m->flags & PG_UNQUEUED) { 3235 ; 3236 } else if (activate || (m->flags & PG_NEED_COMMIT)) { 3237 vm_page_activate(m); 3238 } else { 3239 vm_page_deactivate(m); 3240 } 3241 } 3242 } 3243 } 3244 3245 /* 3246 * Move the specified page to the inactive queue. 3247 * 3248 * Normally athead is 0 resulting in LRU operation. athead is set 3249 * to 1 if we want this page to be 'as if it were placed in the cache', 3250 * except without unmapping it from the process address space. 3251 * 3252 * vm_page's spinlock must be held on entry and will remain held on return. 3253 * This routine may not block. The caller does not have to hold the page 3254 * busied but should have some sort of interlock on its validity. 3255 * 3256 * It is ok if the page is wired (so buffer cache operations don't have 3257 * to mess with the page queues). 3258 */ 3259 static void 3260 _vm_page_deactivate_locked(vm_page_t m, int athead) 3261 { 3262 u_short oqueue; 3263 3264 /* 3265 * Ignore if already inactive. 3266 */ 3267 if (m->queue - m->pc == PQ_INACTIVE || 3268 (m->flags & (PG_FICTITIOUS | PG_UNQUEUED))) { 3269 return; 3270 } 3271 3272 _vm_page_queue_spin_lock(m); 3273 oqueue = _vm_page_rem_queue_spinlocked(m); 3274 3275 if ((m->flags & (PG_FICTITIOUS | PG_UNQUEUED)) == 0) { 3276 if (oqueue == PQ_CACHE) 3277 mycpu->gd_cnt.v_reactivated++; 3278 vm_page_flag_clear(m, PG_WINATCFLS); 3279 _vm_page_add_queue_spinlocked(m, PQ_INACTIVE + m->pc, athead); 3280 if (athead == 0) { 3281 atomic_add_long( 3282 &vm_page_queues[PQ_INACTIVE + m->pc].adds, 1); 3283 } 3284 } 3285 /* NOTE: PQ_NONE if condition not taken */ 3286 _vm_page_queue_spin_unlock(m); 3287 /* leaves vm_page spinlocked */ 3288 } 3289 3290 /* 3291 * Attempt to deactivate a page. 3292 * 3293 * No requirements. We can pre-filter before getting the spinlock. 3294 * 3295 * It is ok if the page is wired (so buffer cache operations don't have 3296 * to mess with the page queues). 3297 */ 3298 void 3299 vm_page_deactivate(vm_page_t m) 3300 { 3301 if (m->queue - m->pc != PQ_INACTIVE && 3302 (m->flags & (PG_FICTITIOUS | PG_UNQUEUED)) == 0) { 3303 vm_page_spin_lock(m); 3304 _vm_page_deactivate_locked(m, 0); 3305 vm_page_spin_unlock(m); 3306 } 3307 } 3308 3309 void 3310 vm_page_deactivate_locked(vm_page_t m) 3311 { 3312 _vm_page_deactivate_locked(m, 0); 3313 } 3314 3315 /* 3316 * Attempt to move a busied page to PQ_CACHE, then unconditionally unbusy it. 3317 * 3318 * This function returns non-zero if it successfully moved the page to 3319 * PQ_CACHE. 3320 * 3321 * This function unconditionally unbusies the page on return. 3322 */ 3323 int 3324 vm_page_try_to_cache(vm_page_t m) 3325 { 3326 /* 3327 * Shortcut if we obviously cannot move the page, or if the 3328 * page is already on the cache queue, or it is ficitious. 3329 * 3330 * Never allow a wired page into the cache. 3331 */ 3332 if (m->dirty || m->hold_count || m->wire_count || 3333 m->queue - m->pc == PQ_CACHE || 3334 (m->flags & (PG_UNQUEUED | PG_NEED_COMMIT | PG_FICTITIOUS))) { 3335 vm_page_wakeup(m); 3336 return(0); 3337 } 3338 3339 /* 3340 * Page busied by us and no longer spinlocked. Dirty pages cannot 3341 * be moved to the cache, but can be deactivated. However, users 3342 * of this function want to move pages closer to the cache so we 3343 * only deactivate it if it is in PQ_ACTIVE. We do not re-deactivate. 3344 */ 3345 vm_page_test_dirty(m); 3346 if (m->dirty || (m->flags & PG_NEED_COMMIT)) { 3347 if (m->queue - m->pc == PQ_ACTIVE) 3348 vm_page_deactivate(m); 3349 vm_page_wakeup(m); 3350 return(0); 3351 } 3352 vm_page_cache(m); 3353 return(1); 3354 } 3355 3356 /* 3357 * Attempt to free the page. If we cannot free it, we do nothing. 3358 * 1 is returned on success, 0 on failure. 3359 * 3360 * The page can be in any state, including already being on the free 3361 * queue. Check to see if it really can be freed. Note that we disallow 3362 * this ad-hoc operation if the page is flagged PG_UNQUEUED. 3363 * 3364 * Caller provides an unlocked/non-busied page. 3365 * No requirements. 3366 */ 3367 int 3368 vm_page_try_to_free(vm_page_t m) 3369 { 3370 if (vm_page_busy_try(m, TRUE)) 3371 return(0); 3372 3373 if (m->dirty || /* can't free if it is dirty */ 3374 m->hold_count || /* or held (XXX may be wrong) */ 3375 m->wire_count || /* or wired */ 3376 (m->flags & (PG_UNQUEUED | /* or unqueued */ 3377 PG_NEED_COMMIT | /* or needs a commit */ 3378 PG_FICTITIOUS)) || /* or is fictitious */ 3379 m->queue - m->pc == PQ_FREE || /* already on PQ_FREE */ 3380 m->queue - m->pc == PQ_HOLD) { /* already on PQ_HOLD */ 3381 vm_page_wakeup(m); 3382 return(0); 3383 } 3384 3385 /* 3386 * We can probably free the page. 3387 * 3388 * Page busied by us and no longer spinlocked. Dirty pages will 3389 * not be freed by this function. We have to re-test the 3390 * dirty bit after cleaning out the pmaps. 3391 */ 3392 vm_page_test_dirty(m); 3393 if (m->dirty || (m->flags & PG_NEED_COMMIT)) { 3394 vm_page_wakeup(m); 3395 return(0); 3396 } 3397 vm_page_protect(m, VM_PROT_NONE); 3398 if (m->dirty || (m->flags & PG_NEED_COMMIT)) { 3399 vm_page_wakeup(m); 3400 return(0); 3401 } 3402 vm_page_free(m); 3403 return(1); 3404 } 3405 3406 /* 3407 * vm_page_cache 3408 * 3409 * Put the specified page onto the page cache queue (if appropriate). 3410 * 3411 * The page must be busy, and this routine will release the busy and 3412 * possibly even free the page. 3413 */ 3414 void 3415 vm_page_cache(vm_page_t m) 3416 { 3417 /* 3418 * Not suitable for the cache 3419 */ 3420 if ((m->flags & (PG_UNQUEUED | PG_NEED_COMMIT | PG_FICTITIOUS)) || 3421 (m->busy_count & PBUSY_MASK) || 3422 m->wire_count || m->hold_count) { 3423 vm_page_wakeup(m); 3424 return; 3425 } 3426 3427 /* 3428 * Already in the cache (and thus not mapped) 3429 */ 3430 if ((m->queue - m->pc) == PQ_CACHE) { 3431 if (m->flags & (PG_MAPPED | PG_WRITEABLE)) 3432 pmap_mapped_sync(m); 3433 KKASSERT((m->flags & PG_MAPPED) == 0); 3434 vm_page_wakeup(m); 3435 return; 3436 } 3437 3438 #if 0 3439 /* 3440 * REMOVED - it is possible for dirty to get set at any time as 3441 * long as the page is still mapped and writeable. 3442 * 3443 * Caller is required to test m->dirty, but note that the act of 3444 * removing the page from its maps can cause it to become dirty 3445 * on an SMP system due to another cpu running in usermode. 3446 */ 3447 if (m->dirty) { 3448 panic("vm_page_cache: caching a dirty page, pindex: %ld", 3449 (long)m->pindex); 3450 } 3451 #endif 3452 3453 /* 3454 * Remove all pmaps and indicate that the page is not 3455 * writeable or mapped. Our vm_page_protect() call may 3456 * have blocked (especially w/ VM_PROT_NONE), so recheck 3457 * everything. 3458 */ 3459 vm_page_protect(m, VM_PROT_NONE); 3460 pmap_mapped_sync(m); 3461 if ((m->flags & (PG_UNQUEUED | PG_MAPPED)) || 3462 (m->busy_count & PBUSY_MASK) || 3463 m->wire_count || m->hold_count) { 3464 vm_page_wakeup(m); 3465 } else if (m->dirty || (m->flags & PG_NEED_COMMIT)) { 3466 vm_page_deactivate(m); 3467 vm_page_wakeup(m); 3468 } else { 3469 _vm_page_and_queue_spin_lock(m); 3470 _vm_page_rem_queue_spinlocked(m); 3471 _vm_page_add_queue_spinlocked(m, PQ_CACHE + m->pc, 0); 3472 _vm_page_and_queue_spin_unlock(m); 3473 vm_page_wakeup(m); 3474 vm_page_free_wakeup(); 3475 } 3476 } 3477 3478 /* 3479 * vm_page_dontneed() 3480 * 3481 * Cache, deactivate, or do nothing as appropriate. This routine 3482 * is typically used by madvise() MADV_DONTNEED. 3483 * 3484 * Generally speaking we want to move the page into the cache so 3485 * it gets reused quickly. However, this can result in a silly syndrome 3486 * due to the page recycling too quickly. Small objects will not be 3487 * fully cached. On the otherhand, if we move the page to the inactive 3488 * queue we wind up with a problem whereby very large objects 3489 * unnecessarily blow away our inactive and cache queues. 3490 * 3491 * The solution is to move the pages based on a fixed weighting. We 3492 * either leave them alone, deactivate them, or move them to the cache, 3493 * where moving them to the cache has the highest weighting. 3494 * By forcing some pages into other queues we eventually force the 3495 * system to balance the queues, potentially recovering other unrelated 3496 * space from active. The idea is to not force this to happen too 3497 * often. 3498 * 3499 * The page must be busied. 3500 */ 3501 void 3502 vm_page_dontneed(vm_page_t m) 3503 { 3504 static int dnweight; 3505 int dnw; 3506 int head; 3507 3508 dnw = ++dnweight; 3509 3510 /* 3511 * occassionally leave the page alone 3512 */ 3513 if ((dnw & 0x01F0) == 0 || 3514 m->queue - m->pc == PQ_INACTIVE || 3515 m->queue - m->pc == PQ_CACHE 3516 ) { 3517 if (m->act_count >= ACT_INIT) 3518 --m->act_count; 3519 return; 3520 } 3521 3522 /* 3523 * If vm_page_dontneed() is inactivating a page, it must clear 3524 * the referenced flag; otherwise the pagedaemon will see references 3525 * on the page in the inactive queue and reactivate it. Until the 3526 * page can move to the cache queue, madvise's job is not done. 3527 */ 3528 vm_page_flag_clear(m, PG_REFERENCED); 3529 pmap_clear_reference(m); 3530 3531 if (m->dirty == 0) 3532 vm_page_test_dirty(m); 3533 3534 if (m->dirty || (dnw & 0x0070) == 0) { 3535 /* 3536 * Deactivate the page 3 times out of 32. 3537 */ 3538 head = 0; 3539 } else { 3540 /* 3541 * Cache the page 28 times out of every 32. Note that 3542 * the page is deactivated instead of cached, but placed 3543 * at the head of the queue instead of the tail. 3544 */ 3545 head = 1; 3546 } 3547 vm_page_spin_lock(m); 3548 _vm_page_deactivate_locked(m, head); 3549 vm_page_spin_unlock(m); 3550 } 3551 3552 /* 3553 * These routines manipulate the 'soft busy' count for a page. A soft busy 3554 * is almost like a hard BUSY except that it allows certain compatible 3555 * operations to occur on the page while it is busy. For example, a page 3556 * undergoing a write can still be mapped read-only. 3557 * 3558 * We also use soft-busy to quickly pmap_enter shared read-only pages 3559 * without having to hold the page locked. 3560 * 3561 * The soft-busy count can be > 1 in situations where multiple threads 3562 * are pmap_enter()ing the same page simultaneously, or when two buffer 3563 * cache buffers overlap the same page. 3564 * 3565 * The caller must hold the page BUSY when making these two calls. 3566 */ 3567 void 3568 vm_page_io_start(vm_page_t m) 3569 { 3570 uint32_t ocount; 3571 3572 ocount = atomic_fetchadd_int(&m->busy_count, 1); 3573 KKASSERT(ocount & PBUSY_LOCKED); 3574 } 3575 3576 void 3577 vm_page_io_finish(vm_page_t m) 3578 { 3579 uint32_t ocount; 3580 3581 ocount = atomic_fetchadd_int(&m->busy_count, -1); 3582 KKASSERT(ocount & PBUSY_MASK); 3583 #if 0 3584 if (((ocount - 1) & (PBUSY_LOCKED | PBUSY_MASK)) == 0) 3585 wakeup(m); 3586 #endif 3587 } 3588 3589 /* 3590 * Attempt to soft-busy a page. The page must not be PBUSY_LOCKED. 3591 * 3592 * We can't use fetchadd here because we might race a hard-busy and the 3593 * page freeing code asserts on a non-zero soft-busy count (even if only 3594 * temporary). 3595 * 3596 * Returns 0 on success, non-zero on failure. 3597 */ 3598 int 3599 vm_page_sbusy_try(vm_page_t m) 3600 { 3601 uint32_t ocount; 3602 3603 for (;;) { 3604 ocount = m->busy_count; 3605 cpu_ccfence(); 3606 if (ocount & PBUSY_LOCKED) 3607 return 1; 3608 if (atomic_cmpset_int(&m->busy_count, ocount, ocount + 1)) 3609 break; 3610 } 3611 return 0; 3612 #if 0 3613 if (m->busy_count & PBUSY_LOCKED) 3614 return 1; 3615 ocount = atomic_fetchadd_int(&m->busy_count, 1); 3616 if (ocount & PBUSY_LOCKED) { 3617 vm_page_sbusy_drop(m); 3618 return 1; 3619 } 3620 return 0; 3621 #endif 3622 } 3623 3624 /* 3625 * Indicate that a clean VM page requires a filesystem commit and cannot 3626 * be reused. Used by tmpfs. 3627 */ 3628 void 3629 vm_page_need_commit(vm_page_t m) 3630 { 3631 vm_page_flag_set(m, PG_NEED_COMMIT); 3632 vm_object_set_writeable_dirty(m->object); 3633 } 3634 3635 void 3636 vm_page_clear_commit(vm_page_t m) 3637 { 3638 vm_page_flag_clear(m, PG_NEED_COMMIT); 3639 } 3640 3641 /* 3642 * Grab a page, blocking if it is busy and allocating a page if necessary. 3643 * A busy page is returned or NULL. The page may or may not be valid and 3644 * might not be on a queue (the caller is responsible for the disposition of 3645 * the page). 3646 * 3647 * If VM_ALLOC_ZERO is specified and the grab must allocate a new page, the 3648 * page will be zero'd and marked valid. 3649 * 3650 * If VM_ALLOC_FORCE_ZERO is specified the page will be zero'd and marked 3651 * valid even if it already exists. 3652 * 3653 * If VM_ALLOC_RETRY is specified this routine will never return NULL. Also 3654 * note that VM_ALLOC_NORMAL must be specified if VM_ALLOC_RETRY is specified. 3655 * VM_ALLOC_NULL_OK is implied when VM_ALLOC_RETRY is specified. 3656 * 3657 * This routine may block, but if VM_ALLOC_RETRY is not set then NULL is 3658 * always returned if we had blocked. 3659 * 3660 * This routine may not be called from an interrupt. 3661 * 3662 * No other requirements. 3663 */ 3664 vm_page_t 3665 vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags) 3666 { 3667 vm_page_t m; 3668 int error; 3669 int shared = 1; 3670 3671 KKASSERT(allocflags & 3672 (VM_ALLOC_NORMAL|VM_ALLOC_INTERRUPT|VM_ALLOC_SYSTEM)); 3673 vm_object_hold_shared(object); 3674 for (;;) { 3675 m = vm_page_lookup_busy_try(object, pindex, TRUE, &error); 3676 if (error) { 3677 vm_page_sleep_busy(m, TRUE, "pgrbwt"); 3678 if ((allocflags & VM_ALLOC_RETRY) == 0) { 3679 m = NULL; 3680 break; 3681 } 3682 /* retry */ 3683 } else if (m == NULL) { 3684 if (shared) { 3685 vm_object_upgrade(object); 3686 shared = 0; 3687 } 3688 if (allocflags & VM_ALLOC_RETRY) 3689 allocflags |= VM_ALLOC_NULL_OK; 3690 m = vm_page_alloc(object, pindex, 3691 allocflags & ~VM_ALLOC_RETRY); 3692 if (m) 3693 break; 3694 vm_wait(0); 3695 if ((allocflags & VM_ALLOC_RETRY) == 0) 3696 goto failed; 3697 } else { 3698 /* m found */ 3699 break; 3700 } 3701 } 3702 3703 /* 3704 * If VM_ALLOC_ZERO an invalid page will be zero'd and set valid. 3705 * 3706 * If VM_ALLOC_FORCE_ZERO the page is unconditionally zero'd and set 3707 * valid even if already valid. 3708 * 3709 * NOTE! We have removed all of the PG_ZERO optimizations and also 3710 * removed the idle zeroing code. These optimizations actually 3711 * slow things down on modern cpus because the zerod area is 3712 * likely uncached, placing a memory-access burden on the 3713 * accesors taking the fault. 3714 * 3715 * By always zeroing the page in-line with the fault, no 3716 * dynamic ram reads are needed and the caches are hot, ready 3717 * for userland to access the memory. 3718 */ 3719 if (m->valid == 0) { 3720 if (allocflags & (VM_ALLOC_ZERO | VM_ALLOC_FORCE_ZERO)) { 3721 pmap_zero_page(VM_PAGE_TO_PHYS(m)); 3722 m->valid = VM_PAGE_BITS_ALL; 3723 } 3724 } else if (allocflags & VM_ALLOC_FORCE_ZERO) { 3725 pmap_zero_page(VM_PAGE_TO_PHYS(m)); 3726 m->valid = VM_PAGE_BITS_ALL; 3727 } 3728 failed: 3729 vm_object_drop(object); 3730 return(m); 3731 } 3732 3733 /* 3734 * Mapping function for valid bits or for dirty bits in 3735 * a page. May not block. 3736 * 3737 * Inputs are required to range within a page. 3738 * 3739 * No requirements. 3740 * Non blocking. 3741 */ 3742 int 3743 vm_page_bits(int base, int size) 3744 { 3745 int first_bit; 3746 int last_bit; 3747 3748 KASSERT( 3749 base + size <= PAGE_SIZE, 3750 ("vm_page_bits: illegal base/size %d/%d", base, size) 3751 ); 3752 3753 if (size == 0) /* handle degenerate case */ 3754 return(0); 3755 3756 first_bit = base >> DEV_BSHIFT; 3757 last_bit = (base + size - 1) >> DEV_BSHIFT; 3758 3759 return ((2 << last_bit) - (1 << first_bit)); 3760 } 3761 3762 /* 3763 * Sets portions of a page valid and clean. The arguments are expected 3764 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive 3765 * of any partial chunks touched by the range. The invalid portion of 3766 * such chunks will be zero'd. 3767 * 3768 * NOTE: When truncating a buffer vnode_pager_setsize() will automatically 3769 * align base to DEV_BSIZE so as not to mark clean a partially 3770 * truncated device block. Otherwise the dirty page status might be 3771 * lost. 3772 * 3773 * This routine may not block. 3774 * 3775 * (base + size) must be less then or equal to PAGE_SIZE. 3776 */ 3777 static void 3778 _vm_page_zero_valid(vm_page_t m, int base, int size) 3779 { 3780 int frag; 3781 int endoff; 3782 3783 if (size == 0) /* handle degenerate case */ 3784 return; 3785 3786 /* 3787 * If the base is not DEV_BSIZE aligned and the valid 3788 * bit is clear, we have to zero out a portion of the 3789 * first block. 3790 */ 3791 3792 if ((frag = rounddown2(base, DEV_BSIZE)) != base && 3793 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0 3794 ) { 3795 pmap_zero_page_area( 3796 VM_PAGE_TO_PHYS(m), 3797 frag, 3798 base - frag 3799 ); 3800 } 3801 3802 /* 3803 * If the ending offset is not DEV_BSIZE aligned and the 3804 * valid bit is clear, we have to zero out a portion of 3805 * the last block. 3806 */ 3807 3808 endoff = base + size; 3809 3810 if ((frag = rounddown2(endoff, DEV_BSIZE)) != endoff && 3811 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0 3812 ) { 3813 pmap_zero_page_area( 3814 VM_PAGE_TO_PHYS(m), 3815 endoff, 3816 DEV_BSIZE - (endoff & (DEV_BSIZE - 1)) 3817 ); 3818 } 3819 } 3820 3821 /* 3822 * Set valid, clear dirty bits. If validating the entire 3823 * page we can safely clear the pmap modify bit. We also 3824 * use this opportunity to clear the PG_NOSYNC flag. If a process 3825 * takes a write fault on a MAP_NOSYNC memory area the flag will 3826 * be set again. 3827 * 3828 * We set valid bits inclusive of any overlap, but we can only 3829 * clear dirty bits for DEV_BSIZE chunks that are fully within 3830 * the range. 3831 * 3832 * Page must be busied? 3833 * No other requirements. 3834 */ 3835 void 3836 vm_page_set_valid(vm_page_t m, int base, int size) 3837 { 3838 _vm_page_zero_valid(m, base, size); 3839 m->valid |= vm_page_bits(base, size); 3840 } 3841 3842 3843 /* 3844 * Set valid bits and clear dirty bits. 3845 * 3846 * Page must be busied by caller. 3847 * 3848 * NOTE: This function does not clear the pmap modified bit. 3849 * Also note that e.g. NFS may use a byte-granular base 3850 * and size. 3851 * 3852 * No other requirements. 3853 */ 3854 void 3855 vm_page_set_validclean(vm_page_t m, int base, int size) 3856 { 3857 int pagebits; 3858 3859 _vm_page_zero_valid(m, base, size); 3860 pagebits = vm_page_bits(base, size); 3861 m->valid |= pagebits; 3862 m->dirty &= ~pagebits; 3863 if (base == 0 && size == PAGE_SIZE) { 3864 /*pmap_clear_modify(m);*/ 3865 vm_page_flag_clear(m, PG_NOSYNC); 3866 } 3867 } 3868 3869 /* 3870 * Set valid & dirty. Used by buwrite() 3871 * 3872 * Page must be busied by caller. 3873 */ 3874 void 3875 vm_page_set_validdirty(vm_page_t m, int base, int size) 3876 { 3877 int pagebits; 3878 3879 pagebits = vm_page_bits(base, size); 3880 m->valid |= pagebits; 3881 m->dirty |= pagebits; 3882 if (m->object) 3883 vm_object_set_writeable_dirty(m->object); 3884 } 3885 3886 /* 3887 * Clear dirty bits. 3888 * 3889 * NOTE: This function does not clear the pmap modified bit. 3890 * Also note that e.g. NFS may use a byte-granular base 3891 * and size. 3892 * 3893 * Page must be busied? 3894 * No other requirements. 3895 */ 3896 void 3897 vm_page_clear_dirty(vm_page_t m, int base, int size) 3898 { 3899 m->dirty &= ~vm_page_bits(base, size); 3900 if (base == 0 && size == PAGE_SIZE) { 3901 /*pmap_clear_modify(m);*/ 3902 vm_page_flag_clear(m, PG_NOSYNC); 3903 } 3904 } 3905 3906 /* 3907 * Make the page all-dirty. 3908 * 3909 * Also make sure the related object and vnode reflect the fact that the 3910 * object may now contain a dirty page. 3911 * 3912 * Page must be busied? 3913 * No other requirements. 3914 */ 3915 void 3916 vm_page_dirty(vm_page_t m) 3917 { 3918 #ifdef INVARIANTS 3919 int pqtype = m->queue - m->pc; 3920 #endif 3921 KASSERT(pqtype != PQ_CACHE && pqtype != PQ_FREE, 3922 ("vm_page_dirty: page in free/cache queue!")); 3923 if (m->dirty != VM_PAGE_BITS_ALL) { 3924 m->dirty = VM_PAGE_BITS_ALL; 3925 if (m->object) 3926 vm_object_set_writeable_dirty(m->object); 3927 } 3928 } 3929 3930 /* 3931 * Invalidates DEV_BSIZE'd chunks within a page. Both the 3932 * valid and dirty bits for the effected areas are cleared. 3933 * 3934 * Page must be busied? 3935 * Does not block. 3936 * No other requirements. 3937 */ 3938 void 3939 vm_page_set_invalid(vm_page_t m, int base, int size) 3940 { 3941 int bits; 3942 3943 bits = vm_page_bits(base, size); 3944 m->valid &= ~bits; 3945 m->dirty &= ~bits; 3946 atomic_add_int(&m->object->generation, 1); 3947 } 3948 3949 /* 3950 * The kernel assumes that the invalid portions of a page contain 3951 * garbage, but such pages can be mapped into memory by user code. 3952 * When this occurs, we must zero out the non-valid portions of the 3953 * page so user code sees what it expects. 3954 * 3955 * Pages are most often semi-valid when the end of a file is mapped 3956 * into memory and the file's size is not page aligned. 3957 * 3958 * Page must be busied? 3959 * No other requirements. 3960 */ 3961 void 3962 vm_page_zero_invalid(vm_page_t m, boolean_t setvalid) 3963 { 3964 int b; 3965 int i; 3966 3967 /* 3968 * Scan the valid bits looking for invalid sections that 3969 * must be zerod. Invalid sub-DEV_BSIZE'd areas ( where the 3970 * valid bit may be set ) have already been zerod by 3971 * vm_page_set_validclean(). 3972 */ 3973 for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) { 3974 if (i == (PAGE_SIZE / DEV_BSIZE) || 3975 (m->valid & (1 << i)) 3976 ) { 3977 if (i > b) { 3978 pmap_zero_page_area( 3979 VM_PAGE_TO_PHYS(m), 3980 b << DEV_BSHIFT, 3981 (i - b) << DEV_BSHIFT 3982 ); 3983 } 3984 b = i + 1; 3985 } 3986 } 3987 3988 /* 3989 * setvalid is TRUE when we can safely set the zero'd areas 3990 * as being valid. We can do this if there are no cache consistency 3991 * issues. e.g. it is ok to do with UFS, but not ok to do with NFS. 3992 */ 3993 if (setvalid) 3994 m->valid = VM_PAGE_BITS_ALL; 3995 } 3996 3997 /* 3998 * Is a (partial) page valid? Note that the case where size == 0 3999 * will return FALSE in the degenerate case where the page is entirely 4000 * invalid, and TRUE otherwise. 4001 * 4002 * Does not block. 4003 * No other requirements. 4004 */ 4005 int 4006 vm_page_is_valid(vm_page_t m, int base, int size) 4007 { 4008 int bits = vm_page_bits(base, size); 4009 4010 if (m->valid && ((m->valid & bits) == bits)) 4011 return 1; 4012 else 4013 return 0; 4014 } 4015 4016 /* 4017 * Update dirty bits from pmap/mmu. May not block. 4018 * 4019 * Caller must hold the page busy 4020 * 4021 * WARNING! Unless the page has been unmapped, this function only 4022 * provides a likely dirty status. 4023 */ 4024 void 4025 vm_page_test_dirty(vm_page_t m) 4026 { 4027 if (m->dirty != VM_PAGE_BITS_ALL && pmap_is_modified(m)) { 4028 vm_page_dirty(m); 4029 } 4030 } 4031 4032 #include "opt_ddb.h" 4033 #ifdef DDB 4034 #include <ddb/ddb.h> 4035 4036 DB_SHOW_COMMAND(page, vm_page_print_page_info) 4037 { 4038 db_printf("vmstats.v_free_count: %ld\n", vmstats.v_free_count); 4039 db_printf("vmstats.v_cache_count: %ld\n", vmstats.v_cache_count); 4040 db_printf("vmstats.v_inactive_count: %ld\n", vmstats.v_inactive_count); 4041 db_printf("vmstats.v_active_count: %ld\n", vmstats.v_active_count); 4042 db_printf("vmstats.v_wire_count: %ld\n", vmstats.v_wire_count); 4043 db_printf("vmstats.v_free_reserved: %ld\n", vmstats.v_free_reserved); 4044 db_printf("vmstats.v_free_min: %ld\n", vmstats.v_free_min); 4045 db_printf("vmstats.v_free_target: %ld\n", vmstats.v_free_target); 4046 db_printf("vmstats.v_cache_min: %ld\n", vmstats.v_cache_min); 4047 db_printf("vmstats.v_inactive_target: %ld\n", 4048 vmstats.v_inactive_target); 4049 } 4050 4051 DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info) 4052 { 4053 int i; 4054 db_printf("PQ_FREE:"); 4055 for (i = 0; i < PQ_L2_SIZE; i++) { 4056 db_printf(" %ld", vm_page_queues[PQ_FREE + i].lcnt); 4057 } 4058 db_printf("\n"); 4059 4060 db_printf("PQ_CACHE:"); 4061 for(i = 0; i < PQ_L2_SIZE; i++) { 4062 db_printf(" %ld", vm_page_queues[PQ_CACHE + i].lcnt); 4063 } 4064 db_printf("\n"); 4065 4066 db_printf("PQ_ACTIVE:"); 4067 for(i = 0; i < PQ_L2_SIZE; i++) { 4068 db_printf(" %ld", vm_page_queues[PQ_ACTIVE + i].lcnt); 4069 } 4070 db_printf("\n"); 4071 4072 db_printf("PQ_INACTIVE:"); 4073 for(i = 0; i < PQ_L2_SIZE; i++) { 4074 db_printf(" %ld", vm_page_queues[PQ_INACTIVE + i].lcnt); 4075 } 4076 db_printf("\n"); 4077 } 4078 #endif /* DDB */ 4079