1 /* $NetBSD: uvm_page.c,v 1.194 2017/10/28 00:37:13 pgoyette Exp $ */ 2 3 /* 4 * Copyright (c) 1997 Charles D. Cranor and Washington University. 5 * Copyright (c) 1991, 1993, The Regents of the University of California. 6 * 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to Berkeley by 10 * The Mach Operating System project at Carnegie-Mellon University. 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 * @(#)vm_page.c 8.3 (Berkeley) 3/21/94 37 * from: Id: uvm_page.c,v 1.1.2.18 1998/02/06 05:24:42 chs Exp 38 * 39 * 40 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 41 * All rights reserved. 42 * 43 * Permission to use, copy, modify and distribute this software and 44 * its documentation is hereby granted, provided that both the copyright 45 * notice and this permission notice appear in all copies of the 46 * software, derivative works or modified versions, and any portions 47 * thereof, and that both notices appear in supporting documentation. 48 * 49 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 50 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 51 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 52 * 53 * Carnegie Mellon requests users of this software to return to 54 * 55 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 56 * School of Computer Science 57 * Carnegie Mellon University 58 * Pittsburgh PA 15213-3890 59 * 60 * any improvements or extensions that they make and grant Carnegie the 61 * rights to redistribute these changes. 62 */ 63 64 /* 65 * uvm_page.c: page ops. 66 */ 67 68 #include <sys/cdefs.h> 69 __KERNEL_RCSID(0, "$NetBSD: uvm_page.c,v 1.194 2017/10/28 00:37:13 pgoyette Exp $"); 70 71 #include "opt_ddb.h" 72 #include "opt_uvm.h" 73 #include "opt_uvmhist.h" 74 #include "opt_readahead.h" 75 76 #include <sys/param.h> 77 #include <sys/systm.h> 78 #include <sys/sched.h> 79 #include <sys/kernel.h> 80 #include <sys/vnode.h> 81 #include <sys/proc.h> 82 #include <sys/atomic.h> 83 #include <sys/cpu.h> 84 #include <sys/extent.h> 85 86 #include <uvm/uvm.h> 87 #include <uvm/uvm_ddb.h> 88 #include <uvm/uvm_pdpolicy.h> 89 90 /* 91 * Some supported CPUs in a given architecture don't support all 92 * of the things necessary to do idle page zero'ing efficiently. 93 * We therefore provide a way to enable it from machdep code here. 94 */ 95 bool vm_page_zero_enable = false; 96 97 /* 98 * number of pages per-CPU to reserve for the kernel. 99 */ 100 #ifndef UVM_RESERVED_PAGES_PER_CPU 101 #define UVM_RESERVED_PAGES_PER_CPU 5 102 #endif 103 int vm_page_reserve_kernel = UVM_RESERVED_PAGES_PER_CPU; 104 105 /* 106 * physical memory size; 107 */ 108 psize_t physmem; 109 110 /* 111 * local variables 112 */ 113 114 /* 115 * these variables record the values returned by vm_page_bootstrap, 116 * for debugging purposes. The implementation of uvm_pageboot_alloc 117 * and pmap_startup here also uses them internally. 118 */ 119 120 static vaddr_t virtual_space_start; 121 static vaddr_t virtual_space_end; 122 123 /* 124 * we allocate an initial number of page colors in uvm_page_init(), 125 * and remember them. We may re-color pages as cache sizes are 126 * discovered during the autoconfiguration phase. But we can never 127 * free the initial set of buckets, since they are allocated using 128 * uvm_pageboot_alloc(). 129 */ 130 131 static size_t recolored_pages_memsize /* = 0 */; 132 133 #ifdef DEBUG 134 vaddr_t uvm_zerocheckkva; 135 #endif /* DEBUG */ 136 137 /* 138 * These functions are reserved for uvm(9) internal use and are not 139 * exported in the header file uvm_physseg.h 140 * 141 * Thus they are redefined here. 142 */ 143 void uvm_physseg_init_seg(uvm_physseg_t, struct vm_page *); 144 void uvm_physseg_seg_chomp_slab(uvm_physseg_t, struct vm_page *, size_t); 145 146 /* returns a pgs array */ 147 struct vm_page *uvm_physseg_seg_alloc_from_slab(uvm_physseg_t, size_t); 148 149 /* 150 * local prototypes 151 */ 152 153 static void uvm_pageinsert(struct uvm_object *, struct vm_page *); 154 static void uvm_pageremove(struct uvm_object *, struct vm_page *); 155 156 /* 157 * per-object tree of pages 158 */ 159 160 static signed int 161 uvm_page_compare_nodes(void *ctx, const void *n1, const void *n2) 162 { 163 const struct vm_page *pg1 = n1; 164 const struct vm_page *pg2 = n2; 165 const voff_t a = pg1->offset; 166 const voff_t b = pg2->offset; 167 168 if (a < b) 169 return -1; 170 if (a > b) 171 return 1; 172 return 0; 173 } 174 175 static signed int 176 uvm_page_compare_key(void *ctx, const void *n, const void *key) 177 { 178 const struct vm_page *pg = n; 179 const voff_t a = pg->offset; 180 const voff_t b = *(const voff_t *)key; 181 182 if (a < b) 183 return -1; 184 if (a > b) 185 return 1; 186 return 0; 187 } 188 189 const rb_tree_ops_t uvm_page_tree_ops = { 190 .rbto_compare_nodes = uvm_page_compare_nodes, 191 .rbto_compare_key = uvm_page_compare_key, 192 .rbto_node_offset = offsetof(struct vm_page, rb_node), 193 .rbto_context = NULL 194 }; 195 196 /* 197 * inline functions 198 */ 199 200 /* 201 * uvm_pageinsert: insert a page in the object. 202 * 203 * => caller must lock object 204 * => caller must lock page queues 205 * => call should have already set pg's object and offset pointers 206 * and bumped the version counter 207 */ 208 209 static inline void 210 uvm_pageinsert_list(struct uvm_object *uobj, struct vm_page *pg, 211 struct vm_page *where) 212 { 213 214 KASSERT(uobj == pg->uobject); 215 KASSERT(mutex_owned(uobj->vmobjlock)); 216 KASSERT((pg->flags & PG_TABLED) == 0); 217 KASSERT(where == NULL || (where->flags & PG_TABLED)); 218 KASSERT(where == NULL || (where->uobject == uobj)); 219 220 if (UVM_OBJ_IS_VNODE(uobj)) { 221 if (uobj->uo_npages == 0) { 222 struct vnode *vp = (struct vnode *)uobj; 223 224 vholdl(vp); 225 } 226 if (UVM_OBJ_IS_VTEXT(uobj)) { 227 atomic_inc_uint(&uvmexp.execpages); 228 } else { 229 atomic_inc_uint(&uvmexp.filepages); 230 } 231 } else if (UVM_OBJ_IS_AOBJ(uobj)) { 232 atomic_inc_uint(&uvmexp.anonpages); 233 } 234 235 if (where) 236 TAILQ_INSERT_AFTER(&uobj->memq, where, pg, listq.queue); 237 else 238 TAILQ_INSERT_TAIL(&uobj->memq, pg, listq.queue); 239 pg->flags |= PG_TABLED; 240 uobj->uo_npages++; 241 } 242 243 244 static inline void 245 uvm_pageinsert_tree(struct uvm_object *uobj, struct vm_page *pg) 246 { 247 struct vm_page *ret __diagused; 248 249 KASSERT(uobj == pg->uobject); 250 ret = rb_tree_insert_node(&uobj->rb_tree, pg); 251 KASSERT(ret == pg); 252 } 253 254 static inline void 255 uvm_pageinsert(struct uvm_object *uobj, struct vm_page *pg) 256 { 257 258 KDASSERT(uobj != NULL); 259 uvm_pageinsert_tree(uobj, pg); 260 uvm_pageinsert_list(uobj, pg, NULL); 261 } 262 263 /* 264 * uvm_page_remove: remove page from object. 265 * 266 * => caller must lock object 267 * => caller must lock page queues 268 */ 269 270 static inline void 271 uvm_pageremove_list(struct uvm_object *uobj, struct vm_page *pg) 272 { 273 274 KASSERT(uobj == pg->uobject); 275 KASSERT(mutex_owned(uobj->vmobjlock)); 276 KASSERT(pg->flags & PG_TABLED); 277 278 if (UVM_OBJ_IS_VNODE(uobj)) { 279 if (uobj->uo_npages == 1) { 280 struct vnode *vp = (struct vnode *)uobj; 281 282 holdrelel(vp); 283 } 284 if (UVM_OBJ_IS_VTEXT(uobj)) { 285 atomic_dec_uint(&uvmexp.execpages); 286 } else { 287 atomic_dec_uint(&uvmexp.filepages); 288 } 289 } else if (UVM_OBJ_IS_AOBJ(uobj)) { 290 atomic_dec_uint(&uvmexp.anonpages); 291 } 292 293 /* object should be locked */ 294 uobj->uo_npages--; 295 TAILQ_REMOVE(&uobj->memq, pg, listq.queue); 296 pg->flags &= ~PG_TABLED; 297 pg->uobject = NULL; 298 } 299 300 static inline void 301 uvm_pageremove_tree(struct uvm_object *uobj, struct vm_page *pg) 302 { 303 304 KASSERT(uobj == pg->uobject); 305 rb_tree_remove_node(&uobj->rb_tree, pg); 306 } 307 308 static inline void 309 uvm_pageremove(struct uvm_object *uobj, struct vm_page *pg) 310 { 311 312 KDASSERT(uobj != NULL); 313 uvm_pageremove_tree(uobj, pg); 314 uvm_pageremove_list(uobj, pg); 315 } 316 317 static void 318 uvm_page_init_buckets(struct pgfreelist *pgfl) 319 { 320 int color, i; 321 322 for (color = 0; color < uvmexp.ncolors; color++) { 323 for (i = 0; i < PGFL_NQUEUES; i++) { 324 LIST_INIT(&pgfl->pgfl_buckets[color].pgfl_queues[i]); 325 } 326 } 327 } 328 329 /* 330 * uvm_page_init: init the page system. called from uvm_init(). 331 * 332 * => we return the range of kernel virtual memory in kvm_startp/kvm_endp 333 */ 334 335 void 336 uvm_page_init(vaddr_t *kvm_startp, vaddr_t *kvm_endp) 337 { 338 static struct uvm_cpu boot_cpu; 339 psize_t freepages, pagecount, bucketcount, n; 340 struct pgflbucket *bucketarray, *cpuarray; 341 struct vm_page *pagearray; 342 uvm_physseg_t bank; 343 int lcv; 344 345 KASSERT(ncpu <= 1); 346 CTASSERT(sizeof(pagearray->offset) >= sizeof(struct uvm_cpu *)); 347 348 /* 349 * init the page queues and page queue locks, except the free 350 * list; we allocate that later (with the initial vm_page 351 * structures). 352 */ 353 354 uvm.cpus[0] = &boot_cpu; 355 curcpu()->ci_data.cpu_uvm = &boot_cpu; 356 uvmpdpol_init(); 357 mutex_init(&uvm_pageqlock, MUTEX_DRIVER, IPL_NONE); 358 mutex_init(&uvm_fpageqlock, MUTEX_DRIVER, IPL_VM); 359 360 /* 361 * allocate vm_page structures. 362 */ 363 364 /* 365 * sanity check: 366 * before calling this function the MD code is expected to register 367 * some free RAM with the uvm_page_physload() function. our job 368 * now is to allocate vm_page structures for this memory. 369 */ 370 371 if (uvm_physseg_get_last() == UVM_PHYSSEG_TYPE_INVALID) 372 panic("uvm_page_bootstrap: no memory pre-allocated"); 373 374 /* 375 * first calculate the number of free pages... 376 * 377 * note that we use start/end rather than avail_start/avail_end. 378 * this allows us to allocate extra vm_page structures in case we 379 * want to return some memory to the pool after booting. 380 */ 381 382 freepages = 0; 383 384 for (bank = uvm_physseg_get_first(); 385 uvm_physseg_valid_p(bank) ; 386 bank = uvm_physseg_get_next(bank)) { 387 freepages += (uvm_physseg_get_end(bank) - uvm_physseg_get_start(bank)); 388 } 389 390 /* 391 * Let MD code initialize the number of colors, or default 392 * to 1 color if MD code doesn't care. 393 */ 394 if (uvmexp.ncolors == 0) 395 uvmexp.ncolors = 1; 396 uvmexp.colormask = uvmexp.ncolors - 1; 397 KASSERT((uvmexp.colormask & uvmexp.ncolors) == 0); 398 399 /* 400 * we now know we have (PAGE_SIZE * freepages) bytes of memory we can 401 * use. for each page of memory we use we need a vm_page structure. 402 * thus, the total number of pages we can use is the total size of 403 * the memory divided by the PAGE_SIZE plus the size of the vm_page 404 * structure. we add one to freepages as a fudge factor to avoid 405 * truncation errors (since we can only allocate in terms of whole 406 * pages). 407 */ 408 409 bucketcount = uvmexp.ncolors * VM_NFREELIST; 410 pagecount = ((freepages + 1) << PAGE_SHIFT) / 411 (PAGE_SIZE + sizeof(struct vm_page)); 412 413 bucketarray = (void *)uvm_pageboot_alloc((bucketcount * 414 sizeof(struct pgflbucket) * 2) + (pagecount * 415 sizeof(struct vm_page))); 416 cpuarray = bucketarray + bucketcount; 417 pagearray = (struct vm_page *)(bucketarray + bucketcount * 2); 418 419 for (lcv = 0; lcv < VM_NFREELIST; lcv++) { 420 uvm.page_free[lcv].pgfl_buckets = 421 (bucketarray + (lcv * uvmexp.ncolors)); 422 uvm_page_init_buckets(&uvm.page_free[lcv]); 423 uvm.cpus[0]->page_free[lcv].pgfl_buckets = 424 (cpuarray + (lcv * uvmexp.ncolors)); 425 uvm_page_init_buckets(&uvm.cpus[0]->page_free[lcv]); 426 } 427 memset(pagearray, 0, pagecount * sizeof(struct vm_page)); 428 429 /* 430 * init the vm_page structures and put them in the correct place. 431 */ 432 /* First init the extent */ 433 434 for (bank = uvm_physseg_get_first(), 435 uvm_physseg_seg_chomp_slab(bank, pagearray, pagecount); 436 uvm_physseg_valid_p(bank); 437 bank = uvm_physseg_get_next(bank)) { 438 439 n = uvm_physseg_get_end(bank) - uvm_physseg_get_start(bank); 440 uvm_physseg_seg_alloc_from_slab(bank, n); 441 uvm_physseg_init_seg(bank, pagearray); 442 443 /* set up page array pointers */ 444 pagearray += n; 445 pagecount -= n; 446 } 447 448 /* 449 * pass up the values of virtual_space_start and 450 * virtual_space_end (obtained by uvm_pageboot_alloc) to the upper 451 * layers of the VM. 452 */ 453 454 *kvm_startp = round_page(virtual_space_start); 455 *kvm_endp = trunc_page(virtual_space_end); 456 #ifdef DEBUG 457 /* 458 * steal kva for uvm_pagezerocheck(). 459 */ 460 uvm_zerocheckkva = *kvm_startp; 461 *kvm_startp += PAGE_SIZE; 462 #endif /* DEBUG */ 463 464 /* 465 * init various thresholds. 466 */ 467 468 uvmexp.reserve_pagedaemon = 1; 469 uvmexp.reserve_kernel = vm_page_reserve_kernel; 470 471 /* 472 * determine if we should zero pages in the idle loop. 473 */ 474 475 uvm.cpus[0]->page_idle_zero = vm_page_zero_enable; 476 477 /* 478 * done! 479 */ 480 481 uvm.page_init_done = true; 482 } 483 484 /* 485 * uvm_setpagesize: set the page size 486 * 487 * => sets page_shift and page_mask from uvmexp.pagesize. 488 */ 489 490 void 491 uvm_setpagesize(void) 492 { 493 494 /* 495 * If uvmexp.pagesize is 0 at this point, we expect PAGE_SIZE 496 * to be a constant (indicated by being a non-zero value). 497 */ 498 if (uvmexp.pagesize == 0) { 499 if (PAGE_SIZE == 0) 500 panic("uvm_setpagesize: uvmexp.pagesize not set"); 501 uvmexp.pagesize = PAGE_SIZE; 502 } 503 uvmexp.pagemask = uvmexp.pagesize - 1; 504 if ((uvmexp.pagemask & uvmexp.pagesize) != 0) 505 panic("uvm_setpagesize: page size %u (%#x) not a power of two", 506 uvmexp.pagesize, uvmexp.pagesize); 507 for (uvmexp.pageshift = 0; ; uvmexp.pageshift++) 508 if ((1 << uvmexp.pageshift) == uvmexp.pagesize) 509 break; 510 } 511 512 /* 513 * uvm_pageboot_alloc: steal memory from physmem for bootstrapping 514 */ 515 516 vaddr_t 517 uvm_pageboot_alloc(vsize_t size) 518 { 519 static bool initialized = false; 520 vaddr_t addr; 521 #if !defined(PMAP_STEAL_MEMORY) 522 vaddr_t vaddr; 523 paddr_t paddr; 524 #endif 525 526 /* 527 * on first call to this function, initialize ourselves. 528 */ 529 if (initialized == false) { 530 pmap_virtual_space(&virtual_space_start, &virtual_space_end); 531 532 /* round it the way we like it */ 533 virtual_space_start = round_page(virtual_space_start); 534 virtual_space_end = trunc_page(virtual_space_end); 535 536 initialized = true; 537 } 538 539 /* round to page size */ 540 size = round_page(size); 541 542 #if defined(PMAP_STEAL_MEMORY) 543 544 /* 545 * defer bootstrap allocation to MD code (it may want to allocate 546 * from a direct-mapped segment). pmap_steal_memory should adjust 547 * virtual_space_start/virtual_space_end if necessary. 548 */ 549 550 addr = pmap_steal_memory(size, &virtual_space_start, 551 &virtual_space_end); 552 553 return(addr); 554 555 #else /* !PMAP_STEAL_MEMORY */ 556 557 /* 558 * allocate virtual memory for this request 559 */ 560 if (virtual_space_start == virtual_space_end || 561 (virtual_space_end - virtual_space_start) < size) 562 panic("uvm_pageboot_alloc: out of virtual space"); 563 564 addr = virtual_space_start; 565 566 #ifdef PMAP_GROWKERNEL 567 /* 568 * If the kernel pmap can't map the requested space, 569 * then allocate more resources for it. 570 */ 571 if (uvm_maxkaddr < (addr + size)) { 572 uvm_maxkaddr = pmap_growkernel(addr + size); 573 if (uvm_maxkaddr < (addr + size)) 574 panic("uvm_pageboot_alloc: pmap_growkernel() failed"); 575 } 576 #endif 577 578 virtual_space_start += size; 579 580 /* 581 * allocate and mapin physical pages to back new virtual pages 582 */ 583 584 for (vaddr = round_page(addr) ; vaddr < addr + size ; 585 vaddr += PAGE_SIZE) { 586 587 if (!uvm_page_physget(&paddr)) 588 panic("uvm_pageboot_alloc: out of memory"); 589 590 /* 591 * Note this memory is no longer managed, so using 592 * pmap_kenter is safe. 593 */ 594 pmap_kenter_pa(vaddr, paddr, VM_PROT_READ|VM_PROT_WRITE, 0); 595 } 596 pmap_update(pmap_kernel()); 597 return(addr); 598 #endif /* PMAP_STEAL_MEMORY */ 599 } 600 601 #if !defined(PMAP_STEAL_MEMORY) 602 /* 603 * uvm_page_physget: "steal" one page from the vm_physmem structure. 604 * 605 * => attempt to allocate it off the end of a segment in which the "avail" 606 * values match the start/end values. if we can't do that, then we 607 * will advance both values (making them equal, and removing some 608 * vm_page structures from the non-avail area). 609 * => return false if out of memory. 610 */ 611 612 /* subroutine: try to allocate from memory chunks on the specified freelist */ 613 static bool uvm_page_physget_freelist(paddr_t *, int); 614 615 static bool 616 uvm_page_physget_freelist(paddr_t *paddrp, int freelist) 617 { 618 uvm_physseg_t lcv; 619 620 /* pass 1: try allocating from a matching end */ 621 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST) 622 for (lcv = uvm_physseg_get_last(); uvm_physseg_valid_p(lcv); lcv = uvm_physseg_get_prev(lcv)) 623 #else 624 for (lcv = uvm_physseg_get_first(); uvm_physseg_valid_p(lcv); lcv = uvm_physseg_get_next(lcv)) 625 #endif 626 { 627 if (uvm.page_init_done == true) 628 panic("uvm_page_physget: called _after_ bootstrap"); 629 630 /* Try to match at front or back on unused segment */ 631 if (uvm_page_physunload(lcv, freelist, paddrp) == false) { 632 if (paddrp == NULL) /* freelist fail, try next */ 633 continue; 634 } else 635 return true; 636 } 637 638 /* pass2: forget about matching ends, just allocate something */ 639 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST) 640 for (lcv = uvm_physseg_get_last(); uvm_physseg_valid_p(lcv); lcv = uvm_physseg_get_prev(lcv)) 641 #else 642 for (lcv = uvm_physseg_get_first(); uvm_physseg_valid_p(lcv); lcv = uvm_physseg_get_next(lcv)) 643 #endif 644 { 645 /* Try the front regardless. */ 646 if (uvm_page_physunload_force(lcv, freelist, paddrp) == false) { 647 if (paddrp == NULL) /* freelist fail, try next */ 648 continue; 649 } else 650 return true; 651 } 652 return false; 653 } 654 655 bool 656 uvm_page_physget(paddr_t *paddrp) 657 { 658 int i; 659 660 /* try in the order of freelist preference */ 661 for (i = 0; i < VM_NFREELIST; i++) 662 if (uvm_page_physget_freelist(paddrp, i) == true) 663 return (true); 664 return (false); 665 } 666 #endif /* PMAP_STEAL_MEMORY */ 667 668 /* 669 * PHYS_TO_VM_PAGE: find vm_page for a PA. used by MI code to get vm_pages 670 * back from an I/O mapping (ugh!). used in some MD code as well. 671 */ 672 struct vm_page * 673 uvm_phys_to_vm_page(paddr_t pa) 674 { 675 paddr_t pf = atop(pa); 676 paddr_t off; 677 uvm_physseg_t upm; 678 679 upm = uvm_physseg_find(pf, &off); 680 if (upm != UVM_PHYSSEG_TYPE_INVALID) 681 return uvm_physseg_get_pg(upm, off); 682 return(NULL); 683 } 684 685 paddr_t 686 uvm_vm_page_to_phys(const struct vm_page *pg) 687 { 688 689 return pg->phys_addr; 690 } 691 692 /* 693 * uvm_page_recolor: Recolor the pages if the new bucket count is 694 * larger than the old one. 695 */ 696 697 void 698 uvm_page_recolor(int newncolors) 699 { 700 struct pgflbucket *bucketarray, *cpuarray, *oldbucketarray; 701 struct pgfreelist gpgfl, pgfl; 702 struct vm_page *pg; 703 vsize_t bucketcount; 704 size_t bucketmemsize, oldbucketmemsize; 705 int color, i, ocolors; 706 int lcv; 707 struct uvm_cpu *ucpu; 708 709 KASSERT(((newncolors - 1) & newncolors) == 0); 710 711 if (newncolors <= uvmexp.ncolors) 712 return; 713 714 if (uvm.page_init_done == false) { 715 uvmexp.ncolors = newncolors; 716 return; 717 } 718 719 bucketcount = newncolors * VM_NFREELIST; 720 bucketmemsize = bucketcount * sizeof(struct pgflbucket) * 2; 721 bucketarray = kmem_alloc(bucketmemsize, KM_SLEEP); 722 cpuarray = bucketarray + bucketcount; 723 724 mutex_spin_enter(&uvm_fpageqlock); 725 726 /* Make sure we should still do this. */ 727 if (newncolors <= uvmexp.ncolors) { 728 mutex_spin_exit(&uvm_fpageqlock); 729 kmem_free(bucketarray, bucketmemsize); 730 return; 731 } 732 733 oldbucketarray = uvm.page_free[0].pgfl_buckets; 734 ocolors = uvmexp.ncolors; 735 736 uvmexp.ncolors = newncolors; 737 uvmexp.colormask = uvmexp.ncolors - 1; 738 739 ucpu = curcpu()->ci_data.cpu_uvm; 740 for (lcv = 0; lcv < VM_NFREELIST; lcv++) { 741 gpgfl.pgfl_buckets = (bucketarray + (lcv * newncolors)); 742 pgfl.pgfl_buckets = (cpuarray + (lcv * uvmexp.ncolors)); 743 uvm_page_init_buckets(&gpgfl); 744 uvm_page_init_buckets(&pgfl); 745 for (color = 0; color < ocolors; color++) { 746 for (i = 0; i < PGFL_NQUEUES; i++) { 747 while ((pg = LIST_FIRST(&uvm.page_free[ 748 lcv].pgfl_buckets[color].pgfl_queues[i])) 749 != NULL) { 750 LIST_REMOVE(pg, pageq.list); /* global */ 751 LIST_REMOVE(pg, listq.list); /* cpu */ 752 LIST_INSERT_HEAD(&gpgfl.pgfl_buckets[ 753 VM_PGCOLOR_BUCKET(pg)].pgfl_queues[ 754 i], pg, pageq.list); 755 LIST_INSERT_HEAD(&pgfl.pgfl_buckets[ 756 VM_PGCOLOR_BUCKET(pg)].pgfl_queues[ 757 i], pg, listq.list); 758 } 759 } 760 } 761 uvm.page_free[lcv].pgfl_buckets = gpgfl.pgfl_buckets; 762 ucpu->page_free[lcv].pgfl_buckets = pgfl.pgfl_buckets; 763 } 764 765 oldbucketmemsize = recolored_pages_memsize; 766 767 recolored_pages_memsize = bucketmemsize; 768 mutex_spin_exit(&uvm_fpageqlock); 769 770 if (oldbucketmemsize) { 771 kmem_free(oldbucketarray, recolored_pages_memsize); 772 } 773 774 /* 775 * this calls uvm_km_alloc() which may want to hold 776 * uvm_fpageqlock. 777 */ 778 uvm_pager_realloc_emerg(); 779 } 780 781 /* 782 * uvm_cpu_attach: initialize per-CPU data structures. 783 */ 784 785 void 786 uvm_cpu_attach(struct cpu_info *ci) 787 { 788 struct pgflbucket *bucketarray; 789 struct pgfreelist pgfl; 790 struct uvm_cpu *ucpu; 791 vsize_t bucketcount; 792 int lcv; 793 794 if (CPU_IS_PRIMARY(ci)) { 795 /* Already done in uvm_page_init(). */ 796 goto attachrnd; 797 } 798 799 /* Add more reserve pages for this CPU. */ 800 uvmexp.reserve_kernel += vm_page_reserve_kernel; 801 802 /* Configure this CPU's free lists. */ 803 bucketcount = uvmexp.ncolors * VM_NFREELIST; 804 bucketarray = kmem_alloc(bucketcount * sizeof(struct pgflbucket), 805 KM_SLEEP); 806 ucpu = kmem_zalloc(sizeof(*ucpu), KM_SLEEP); 807 uvm.cpus[cpu_index(ci)] = ucpu; 808 ci->ci_data.cpu_uvm = ucpu; 809 for (lcv = 0; lcv < VM_NFREELIST; lcv++) { 810 pgfl.pgfl_buckets = (bucketarray + (lcv * uvmexp.ncolors)); 811 uvm_page_init_buckets(&pgfl); 812 ucpu->page_free[lcv].pgfl_buckets = pgfl.pgfl_buckets; 813 } 814 815 attachrnd: 816 /* 817 * Attach RNG source for this CPU's VM events 818 */ 819 rnd_attach_source(&uvm.cpus[cpu_index(ci)]->rs, 820 ci->ci_data.cpu_name, RND_TYPE_VM, 821 RND_FLAG_COLLECT_TIME|RND_FLAG_COLLECT_VALUE| 822 RND_FLAG_ESTIMATE_VALUE); 823 824 } 825 826 /* 827 * uvm_pagealloc_pgfl: helper routine for uvm_pagealloc_strat 828 */ 829 830 static struct vm_page * 831 uvm_pagealloc_pgfl(struct uvm_cpu *ucpu, int flist, int try1, int try2, 832 int *trycolorp) 833 { 834 struct pgflist *freeq; 835 struct vm_page *pg; 836 int color, trycolor = *trycolorp; 837 struct pgfreelist *gpgfl, *pgfl; 838 839 KASSERT(mutex_owned(&uvm_fpageqlock)); 840 841 color = trycolor; 842 pgfl = &ucpu->page_free[flist]; 843 gpgfl = &uvm.page_free[flist]; 844 do { 845 /* cpu, try1 */ 846 if ((pg = LIST_FIRST((freeq = 847 &pgfl->pgfl_buckets[color].pgfl_queues[try1]))) != NULL) { 848 KASSERT(pg->pqflags & PQ_FREE); 849 KASSERT(try1 == PGFL_ZEROS || !(pg->flags & PG_ZERO)); 850 KASSERT(try1 == PGFL_UNKNOWN || (pg->flags & PG_ZERO)); 851 KASSERT(ucpu == VM_FREE_PAGE_TO_CPU(pg)); 852 VM_FREE_PAGE_TO_CPU(pg)->pages[try1]--; 853 uvmexp.cpuhit++; 854 goto gotit; 855 } 856 /* global, try1 */ 857 if ((pg = LIST_FIRST((freeq = 858 &gpgfl->pgfl_buckets[color].pgfl_queues[try1]))) != NULL) { 859 KASSERT(pg->pqflags & PQ_FREE); 860 KASSERT(try1 == PGFL_ZEROS || !(pg->flags & PG_ZERO)); 861 KASSERT(try1 == PGFL_UNKNOWN || (pg->flags & PG_ZERO)); 862 KASSERT(ucpu != VM_FREE_PAGE_TO_CPU(pg)); 863 VM_FREE_PAGE_TO_CPU(pg)->pages[try1]--; 864 uvmexp.cpumiss++; 865 goto gotit; 866 } 867 /* cpu, try2 */ 868 if ((pg = LIST_FIRST((freeq = 869 &pgfl->pgfl_buckets[color].pgfl_queues[try2]))) != NULL) { 870 KASSERT(pg->pqflags & PQ_FREE); 871 KASSERT(try2 == PGFL_ZEROS || !(pg->flags & PG_ZERO)); 872 KASSERT(try2 == PGFL_UNKNOWN || (pg->flags & PG_ZERO)); 873 KASSERT(ucpu == VM_FREE_PAGE_TO_CPU(pg)); 874 VM_FREE_PAGE_TO_CPU(pg)->pages[try2]--; 875 uvmexp.cpuhit++; 876 goto gotit; 877 } 878 /* global, try2 */ 879 if ((pg = LIST_FIRST((freeq = 880 &gpgfl->pgfl_buckets[color].pgfl_queues[try2]))) != NULL) { 881 KASSERT(pg->pqflags & PQ_FREE); 882 KASSERT(try2 == PGFL_ZEROS || !(pg->flags & PG_ZERO)); 883 KASSERT(try2 == PGFL_UNKNOWN || (pg->flags & PG_ZERO)); 884 KASSERT(ucpu != VM_FREE_PAGE_TO_CPU(pg)); 885 VM_FREE_PAGE_TO_CPU(pg)->pages[try2]--; 886 uvmexp.cpumiss++; 887 goto gotit; 888 } 889 color = (color + 1) & uvmexp.colormask; 890 } while (color != trycolor); 891 892 return (NULL); 893 894 gotit: 895 LIST_REMOVE(pg, pageq.list); /* global list */ 896 LIST_REMOVE(pg, listq.list); /* per-cpu list */ 897 uvmexp.free--; 898 899 /* update zero'd page count */ 900 if (pg->flags & PG_ZERO) 901 uvmexp.zeropages--; 902 903 if (color == trycolor) 904 uvmexp.colorhit++; 905 else { 906 uvmexp.colormiss++; 907 *trycolorp = color; 908 } 909 910 return (pg); 911 } 912 913 /* 914 * uvm_pagealloc_strat: allocate vm_page from a particular free list. 915 * 916 * => return null if no pages free 917 * => wake up pagedaemon if number of free pages drops below low water mark 918 * => if obj != NULL, obj must be locked (to put in obj's tree) 919 * => if anon != NULL, anon must be locked (to put in anon) 920 * => only one of obj or anon can be non-null 921 * => caller must activate/deactivate page if it is not wired. 922 * => free_list is ignored if strat == UVM_PGA_STRAT_NORMAL. 923 * => policy decision: it is more important to pull a page off of the 924 * appropriate priority free list than it is to get a zero'd or 925 * unknown contents page. This is because we live with the 926 * consequences of a bad free list decision for the entire 927 * lifetime of the page, e.g. if the page comes from memory that 928 * is slower to access. 929 */ 930 931 struct vm_page * 932 uvm_pagealloc_strat(struct uvm_object *obj, voff_t off, struct vm_anon *anon, 933 int flags, int strat, int free_list) 934 { 935 int try1, try2, zeroit = 0, color; 936 int lcv; 937 struct uvm_cpu *ucpu; 938 struct vm_page *pg; 939 lwp_t *l; 940 941 KASSERT(obj == NULL || anon == NULL); 942 KASSERT(anon == NULL || (flags & UVM_FLAG_COLORMATCH) || off == 0); 943 KASSERT(off == trunc_page(off)); 944 KASSERT(obj == NULL || mutex_owned(obj->vmobjlock)); 945 KASSERT(anon == NULL || anon->an_lock == NULL || 946 mutex_owned(anon->an_lock)); 947 948 mutex_spin_enter(&uvm_fpageqlock); 949 950 /* 951 * This implements a global round-robin page coloring 952 * algorithm. 953 */ 954 955 ucpu = curcpu()->ci_data.cpu_uvm; 956 if (flags & UVM_FLAG_COLORMATCH) { 957 color = atop(off) & uvmexp.colormask; 958 } else { 959 color = ucpu->page_free_nextcolor; 960 } 961 962 /* 963 * check to see if we need to generate some free pages waking 964 * the pagedaemon. 965 */ 966 967 uvm_kick_pdaemon(); 968 969 /* 970 * fail if any of these conditions is true: 971 * [1] there really are no free pages, or 972 * [2] only kernel "reserved" pages remain and 973 * reserved pages have not been requested. 974 * [3] only pagedaemon "reserved" pages remain and 975 * the requestor isn't the pagedaemon. 976 * we make kernel reserve pages available if called by a 977 * kernel thread or a realtime thread. 978 */ 979 l = curlwp; 980 if (__predict_true(l != NULL) && lwp_eprio(l) >= PRI_KTHREAD) { 981 flags |= UVM_PGA_USERESERVE; 982 } 983 if ((uvmexp.free <= uvmexp.reserve_kernel && 984 (flags & UVM_PGA_USERESERVE) == 0) || 985 (uvmexp.free <= uvmexp.reserve_pagedaemon && 986 curlwp != uvm.pagedaemon_lwp)) 987 goto fail; 988 989 #if PGFL_NQUEUES != 2 990 #error uvm_pagealloc_strat needs to be updated 991 #endif 992 993 /* 994 * If we want a zero'd page, try the ZEROS queue first, otherwise 995 * we try the UNKNOWN queue first. 996 */ 997 if (flags & UVM_PGA_ZERO) { 998 try1 = PGFL_ZEROS; 999 try2 = PGFL_UNKNOWN; 1000 } else { 1001 try1 = PGFL_UNKNOWN; 1002 try2 = PGFL_ZEROS; 1003 } 1004 1005 again: 1006 switch (strat) { 1007 case UVM_PGA_STRAT_NORMAL: 1008 /* Check freelists: descending priority (ascending id) order */ 1009 for (lcv = 0; lcv < VM_NFREELIST; lcv++) { 1010 pg = uvm_pagealloc_pgfl(ucpu, lcv, 1011 try1, try2, &color); 1012 if (pg != NULL) 1013 goto gotit; 1014 } 1015 1016 /* No pages free! */ 1017 goto fail; 1018 1019 case UVM_PGA_STRAT_ONLY: 1020 case UVM_PGA_STRAT_FALLBACK: 1021 /* Attempt to allocate from the specified free list. */ 1022 KASSERT(free_list >= 0 && free_list < VM_NFREELIST); 1023 pg = uvm_pagealloc_pgfl(ucpu, free_list, 1024 try1, try2, &color); 1025 if (pg != NULL) 1026 goto gotit; 1027 1028 /* Fall back, if possible. */ 1029 if (strat == UVM_PGA_STRAT_FALLBACK) { 1030 strat = UVM_PGA_STRAT_NORMAL; 1031 goto again; 1032 } 1033 1034 /* No pages free! */ 1035 goto fail; 1036 1037 default: 1038 panic("uvm_pagealloc_strat: bad strat %d", strat); 1039 /* NOTREACHED */ 1040 } 1041 1042 gotit: 1043 /* 1044 * We now know which color we actually allocated from; set 1045 * the next color accordingly. 1046 */ 1047 1048 ucpu->page_free_nextcolor = (color + 1) & uvmexp.colormask; 1049 1050 /* 1051 * update allocation statistics and remember if we have to 1052 * zero the page 1053 */ 1054 1055 if (flags & UVM_PGA_ZERO) { 1056 if (pg->flags & PG_ZERO) { 1057 uvmexp.pga_zerohit++; 1058 zeroit = 0; 1059 } else { 1060 uvmexp.pga_zeromiss++; 1061 zeroit = 1; 1062 } 1063 if (ucpu->pages[PGFL_ZEROS] < ucpu->pages[PGFL_UNKNOWN]) { 1064 ucpu->page_idle_zero = vm_page_zero_enable; 1065 } 1066 } 1067 KASSERT(pg->pqflags == PQ_FREE); 1068 1069 pg->offset = off; 1070 pg->uobject = obj; 1071 pg->uanon = anon; 1072 pg->flags = PG_BUSY|PG_CLEAN|PG_FAKE; 1073 if (anon) { 1074 anon->an_page = pg; 1075 pg->pqflags = PQ_ANON; 1076 atomic_inc_uint(&uvmexp.anonpages); 1077 } else { 1078 if (obj) { 1079 uvm_pageinsert(obj, pg); 1080 } 1081 pg->pqflags = 0; 1082 } 1083 mutex_spin_exit(&uvm_fpageqlock); 1084 1085 #if defined(UVM_PAGE_TRKOWN) 1086 pg->owner_tag = NULL; 1087 #endif 1088 UVM_PAGE_OWN(pg, "new alloc"); 1089 1090 if (flags & UVM_PGA_ZERO) { 1091 /* 1092 * A zero'd page is not clean. If we got a page not already 1093 * zero'd, then we have to zero it ourselves. 1094 */ 1095 pg->flags &= ~PG_CLEAN; 1096 if (zeroit) 1097 pmap_zero_page(VM_PAGE_TO_PHYS(pg)); 1098 } 1099 1100 return(pg); 1101 1102 fail: 1103 mutex_spin_exit(&uvm_fpageqlock); 1104 return (NULL); 1105 } 1106 1107 /* 1108 * uvm_pagereplace: replace a page with another 1109 * 1110 * => object must be locked 1111 */ 1112 1113 void 1114 uvm_pagereplace(struct vm_page *oldpg, struct vm_page *newpg) 1115 { 1116 struct uvm_object *uobj = oldpg->uobject; 1117 1118 KASSERT((oldpg->flags & PG_TABLED) != 0); 1119 KASSERT(uobj != NULL); 1120 KASSERT((newpg->flags & PG_TABLED) == 0); 1121 KASSERT(newpg->uobject == NULL); 1122 KASSERT(mutex_owned(uobj->vmobjlock)); 1123 1124 newpg->uobject = uobj; 1125 newpg->offset = oldpg->offset; 1126 1127 uvm_pageremove_tree(uobj, oldpg); 1128 uvm_pageinsert_tree(uobj, newpg); 1129 uvm_pageinsert_list(uobj, newpg, oldpg); 1130 uvm_pageremove_list(uobj, oldpg); 1131 } 1132 1133 /* 1134 * uvm_pagerealloc: reallocate a page from one object to another 1135 * 1136 * => both objects must be locked 1137 */ 1138 1139 void 1140 uvm_pagerealloc(struct vm_page *pg, struct uvm_object *newobj, voff_t newoff) 1141 { 1142 /* 1143 * remove it from the old object 1144 */ 1145 1146 if (pg->uobject) { 1147 uvm_pageremove(pg->uobject, pg); 1148 } 1149 1150 /* 1151 * put it in the new object 1152 */ 1153 1154 if (newobj) { 1155 pg->uobject = newobj; 1156 pg->offset = newoff; 1157 uvm_pageinsert(newobj, pg); 1158 } 1159 } 1160 1161 #ifdef DEBUG 1162 /* 1163 * check if page is zero-filled 1164 * 1165 * - called with free page queue lock held. 1166 */ 1167 void 1168 uvm_pagezerocheck(struct vm_page *pg) 1169 { 1170 int *p, *ep; 1171 1172 KASSERT(uvm_zerocheckkva != 0); 1173 KASSERT(mutex_owned(&uvm_fpageqlock)); 1174 1175 /* 1176 * XXX assuming pmap_kenter_pa and pmap_kremove never call 1177 * uvm page allocator. 1178 * 1179 * it might be better to have "CPU-local temporary map" pmap interface. 1180 */ 1181 pmap_kenter_pa(uvm_zerocheckkva, VM_PAGE_TO_PHYS(pg), VM_PROT_READ, 0); 1182 p = (int *)uvm_zerocheckkva; 1183 ep = (int *)((char *)p + PAGE_SIZE); 1184 pmap_update(pmap_kernel()); 1185 while (p < ep) { 1186 if (*p != 0) 1187 panic("PG_ZERO page isn't zero-filled"); 1188 p++; 1189 } 1190 pmap_kremove(uvm_zerocheckkva, PAGE_SIZE); 1191 /* 1192 * pmap_update() is not necessary here because no one except us 1193 * uses this VA. 1194 */ 1195 } 1196 #endif /* DEBUG */ 1197 1198 /* 1199 * uvm_pagefree: free page 1200 * 1201 * => erase page's identity (i.e. remove from object) 1202 * => put page on free list 1203 * => caller must lock owning object (either anon or uvm_object) 1204 * => caller must lock page queues 1205 * => assumes all valid mappings of pg are gone 1206 */ 1207 1208 void 1209 uvm_pagefree(struct vm_page *pg) 1210 { 1211 struct pgflist *pgfl; 1212 struct uvm_cpu *ucpu; 1213 int index, color, queue; 1214 bool iszero; 1215 1216 #ifdef DEBUG 1217 if (pg->uobject == (void *)0xdeadbeef && 1218 pg->uanon == (void *)0xdeadbeef) { 1219 panic("uvm_pagefree: freeing free page %p", pg); 1220 } 1221 #endif /* DEBUG */ 1222 1223 KASSERT((pg->flags & PG_PAGEOUT) == 0); 1224 KASSERT(!(pg->pqflags & PQ_FREE)); 1225 //KASSERT(mutex_owned(&uvm_pageqlock) || !uvmpdpol_pageisqueued_p(pg)); 1226 KASSERT(pg->uobject == NULL || mutex_owned(pg->uobject->vmobjlock)); 1227 KASSERT(pg->uobject != NULL || pg->uanon == NULL || 1228 mutex_owned(pg->uanon->an_lock)); 1229 1230 /* 1231 * if the page is loaned, resolve the loan instead of freeing. 1232 */ 1233 1234 if (pg->loan_count) { 1235 KASSERT(pg->wire_count == 0); 1236 1237 /* 1238 * if the page is owned by an anon then we just want to 1239 * drop anon ownership. the kernel will free the page when 1240 * it is done with it. if the page is owned by an object, 1241 * remove it from the object and mark it dirty for the benefit 1242 * of possible anon owners. 1243 * 1244 * regardless of previous ownership, wakeup any waiters, 1245 * unbusy the page, and we're done. 1246 */ 1247 1248 if (pg->uobject != NULL) { 1249 uvm_pageremove(pg->uobject, pg); 1250 pg->flags &= ~PG_CLEAN; 1251 } else if (pg->uanon != NULL) { 1252 if ((pg->pqflags & PQ_ANON) == 0) { 1253 pg->loan_count--; 1254 } else { 1255 pg->pqflags &= ~PQ_ANON; 1256 atomic_dec_uint(&uvmexp.anonpages); 1257 } 1258 pg->uanon->an_page = NULL; 1259 pg->uanon = NULL; 1260 } 1261 if (pg->flags & PG_WANTED) { 1262 wakeup(pg); 1263 } 1264 pg->flags &= ~(PG_WANTED|PG_BUSY|PG_RELEASED|PG_PAGER1); 1265 #ifdef UVM_PAGE_TRKOWN 1266 pg->owner_tag = NULL; 1267 #endif 1268 if (pg->loan_count) { 1269 KASSERT(pg->uobject == NULL); 1270 if (pg->uanon == NULL) { 1271 KASSERT(mutex_owned(&uvm_pageqlock)); 1272 uvm_pagedequeue(pg); 1273 } 1274 return; 1275 } 1276 } 1277 1278 /* 1279 * remove page from its object or anon. 1280 */ 1281 1282 if (pg->uobject != NULL) { 1283 uvm_pageremove(pg->uobject, pg); 1284 } else if (pg->uanon != NULL) { 1285 pg->uanon->an_page = NULL; 1286 atomic_dec_uint(&uvmexp.anonpages); 1287 } 1288 1289 /* 1290 * now remove the page from the queues. 1291 */ 1292 if (uvmpdpol_pageisqueued_p(pg)) { 1293 KASSERT(mutex_owned(&uvm_pageqlock)); 1294 uvm_pagedequeue(pg); 1295 } 1296 1297 /* 1298 * if the page was wired, unwire it now. 1299 */ 1300 1301 if (pg->wire_count) { 1302 pg->wire_count = 0; 1303 uvmexp.wired--; 1304 } 1305 1306 /* 1307 * and put on free queue 1308 */ 1309 1310 iszero = (pg->flags & PG_ZERO); 1311 index = uvm_page_lookup_freelist(pg); 1312 color = VM_PGCOLOR_BUCKET(pg); 1313 queue = (iszero ? PGFL_ZEROS : PGFL_UNKNOWN); 1314 1315 #ifdef DEBUG 1316 pg->uobject = (void *)0xdeadbeef; 1317 pg->uanon = (void *)0xdeadbeef; 1318 #endif 1319 1320 mutex_spin_enter(&uvm_fpageqlock); 1321 pg->pqflags = PQ_FREE; 1322 1323 #ifdef DEBUG 1324 if (iszero) 1325 uvm_pagezerocheck(pg); 1326 #endif /* DEBUG */ 1327 1328 1329 /* global list */ 1330 pgfl = &uvm.page_free[index].pgfl_buckets[color].pgfl_queues[queue]; 1331 LIST_INSERT_HEAD(pgfl, pg, pageq.list); 1332 uvmexp.free++; 1333 if (iszero) { 1334 uvmexp.zeropages++; 1335 } 1336 1337 /* per-cpu list */ 1338 ucpu = curcpu()->ci_data.cpu_uvm; 1339 pg->offset = (uintptr_t)ucpu; 1340 pgfl = &ucpu->page_free[index].pgfl_buckets[color].pgfl_queues[queue]; 1341 LIST_INSERT_HEAD(pgfl, pg, listq.list); 1342 ucpu->pages[queue]++; 1343 if (ucpu->pages[PGFL_ZEROS] < ucpu->pages[PGFL_UNKNOWN]) { 1344 ucpu->page_idle_zero = vm_page_zero_enable; 1345 } 1346 1347 mutex_spin_exit(&uvm_fpageqlock); 1348 } 1349 1350 /* 1351 * uvm_page_unbusy: unbusy an array of pages. 1352 * 1353 * => pages must either all belong to the same object, or all belong to anons. 1354 * => if pages are object-owned, object must be locked. 1355 * => if pages are anon-owned, anons must be locked. 1356 * => caller must lock page queues if pages may be released. 1357 * => caller must make sure that anon-owned pages are not PG_RELEASED. 1358 */ 1359 1360 void 1361 uvm_page_unbusy(struct vm_page **pgs, int npgs) 1362 { 1363 struct vm_page *pg; 1364 int i; 1365 UVMHIST_FUNC("uvm_page_unbusy"); UVMHIST_CALLED(ubchist); 1366 1367 for (i = 0; i < npgs; i++) { 1368 pg = pgs[i]; 1369 if (pg == NULL || pg == PGO_DONTCARE) { 1370 continue; 1371 } 1372 1373 KASSERT(uvm_page_locked_p(pg)); 1374 KASSERT(pg->flags & PG_BUSY); 1375 KASSERT((pg->flags & PG_PAGEOUT) == 0); 1376 if (pg->flags & PG_WANTED) { 1377 wakeup(pg); 1378 } 1379 if (pg->flags & PG_RELEASED) { 1380 UVMHIST_LOG(ubchist, "releasing pg %#jx", 1381 (uintptr_t)pg, 0, 0, 0); 1382 KASSERT(pg->uobject != NULL || 1383 (pg->uanon != NULL && pg->uanon->an_ref > 0)); 1384 pg->flags &= ~PG_RELEASED; 1385 uvm_pagefree(pg); 1386 } else { 1387 UVMHIST_LOG(ubchist, "unbusying pg %#jx", 1388 (uintptr_t)pg, 0, 0, 0); 1389 KASSERT((pg->flags & PG_FAKE) == 0); 1390 pg->flags &= ~(PG_WANTED|PG_BUSY); 1391 UVM_PAGE_OWN(pg, NULL); 1392 } 1393 } 1394 } 1395 1396 #if defined(UVM_PAGE_TRKOWN) 1397 /* 1398 * uvm_page_own: set or release page ownership 1399 * 1400 * => this is a debugging function that keeps track of who sets PG_BUSY 1401 * and where they do it. it can be used to track down problems 1402 * such a process setting "PG_BUSY" and never releasing it. 1403 * => page's object [if any] must be locked 1404 * => if "tag" is NULL then we are releasing page ownership 1405 */ 1406 void 1407 uvm_page_own(struct vm_page *pg, const char *tag) 1408 { 1409 1410 KASSERT((pg->flags & (PG_PAGEOUT|PG_RELEASED)) == 0); 1411 KASSERT((pg->flags & PG_WANTED) == 0); 1412 KASSERT(uvm_page_locked_p(pg)); 1413 1414 /* gain ownership? */ 1415 if (tag) { 1416 KASSERT((pg->flags & PG_BUSY) != 0); 1417 if (pg->owner_tag) { 1418 printf("uvm_page_own: page %p already owned " 1419 "by proc %d [%s]\n", pg, 1420 pg->owner, pg->owner_tag); 1421 panic("uvm_page_own"); 1422 } 1423 pg->owner = curproc->p_pid; 1424 pg->lowner = curlwp->l_lid; 1425 pg->owner_tag = tag; 1426 return; 1427 } 1428 1429 /* drop ownership */ 1430 KASSERT((pg->flags & PG_BUSY) == 0); 1431 if (pg->owner_tag == NULL) { 1432 printf("uvm_page_own: dropping ownership of an non-owned " 1433 "page (%p)\n", pg); 1434 panic("uvm_page_own"); 1435 } 1436 if (!uvmpdpol_pageisqueued_p(pg)) { 1437 KASSERT((pg->uanon == NULL && pg->uobject == NULL) || 1438 pg->wire_count > 0); 1439 } else { 1440 KASSERT(pg->wire_count == 0); 1441 } 1442 pg->owner_tag = NULL; 1443 } 1444 #endif 1445 1446 /* 1447 * uvm_pageidlezero: zero free pages while the system is idle. 1448 * 1449 * => try to complete one color bucket at a time, to reduce our impact 1450 * on the CPU cache. 1451 * => we loop until we either reach the target or there is a lwp ready 1452 * to run, or MD code detects a reason to break early. 1453 */ 1454 void 1455 uvm_pageidlezero(void) 1456 { 1457 struct vm_page *pg; 1458 struct pgfreelist *pgfl, *gpgfl; 1459 struct uvm_cpu *ucpu; 1460 int free_list, firstbucket, nextbucket; 1461 bool lcont = false; 1462 1463 ucpu = curcpu()->ci_data.cpu_uvm; 1464 if (!ucpu->page_idle_zero || 1465 ucpu->pages[PGFL_UNKNOWN] < uvmexp.ncolors) { 1466 ucpu->page_idle_zero = false; 1467 return; 1468 } 1469 if (!mutex_tryenter(&uvm_fpageqlock)) { 1470 /* Contention: let other CPUs to use the lock. */ 1471 return; 1472 } 1473 firstbucket = ucpu->page_free_nextcolor; 1474 nextbucket = firstbucket; 1475 do { 1476 for (free_list = 0; free_list < VM_NFREELIST; free_list++) { 1477 if (sched_curcpu_runnable_p()) { 1478 goto quit; 1479 } 1480 pgfl = &ucpu->page_free[free_list]; 1481 gpgfl = &uvm.page_free[free_list]; 1482 while ((pg = LIST_FIRST(&pgfl->pgfl_buckets[ 1483 nextbucket].pgfl_queues[PGFL_UNKNOWN])) != NULL) { 1484 if (lcont || sched_curcpu_runnable_p()) { 1485 goto quit; 1486 } 1487 LIST_REMOVE(pg, pageq.list); /* global list */ 1488 LIST_REMOVE(pg, listq.list); /* per-cpu list */ 1489 ucpu->pages[PGFL_UNKNOWN]--; 1490 uvmexp.free--; 1491 KASSERT(pg->pqflags == PQ_FREE); 1492 pg->pqflags = 0; 1493 mutex_spin_exit(&uvm_fpageqlock); 1494 #ifdef PMAP_PAGEIDLEZERO 1495 if (!PMAP_PAGEIDLEZERO(VM_PAGE_TO_PHYS(pg))) { 1496 1497 /* 1498 * The machine-dependent code detected 1499 * some reason for us to abort zeroing 1500 * pages, probably because there is a 1501 * process now ready to run. 1502 */ 1503 1504 mutex_spin_enter(&uvm_fpageqlock); 1505 pg->pqflags = PQ_FREE; 1506 LIST_INSERT_HEAD(&gpgfl->pgfl_buckets[ 1507 nextbucket].pgfl_queues[ 1508 PGFL_UNKNOWN], pg, pageq.list); 1509 LIST_INSERT_HEAD(&pgfl->pgfl_buckets[ 1510 nextbucket].pgfl_queues[ 1511 PGFL_UNKNOWN], pg, listq.list); 1512 ucpu->pages[PGFL_UNKNOWN]++; 1513 uvmexp.free++; 1514 uvmexp.zeroaborts++; 1515 goto quit; 1516 } 1517 #else 1518 pmap_zero_page(VM_PAGE_TO_PHYS(pg)); 1519 #endif /* PMAP_PAGEIDLEZERO */ 1520 pg->flags |= PG_ZERO; 1521 1522 if (!mutex_tryenter(&uvm_fpageqlock)) { 1523 lcont = true; 1524 mutex_spin_enter(&uvm_fpageqlock); 1525 } else { 1526 lcont = false; 1527 } 1528 pg->pqflags = PQ_FREE; 1529 LIST_INSERT_HEAD(&gpgfl->pgfl_buckets[ 1530 nextbucket].pgfl_queues[PGFL_ZEROS], 1531 pg, pageq.list); 1532 LIST_INSERT_HEAD(&pgfl->pgfl_buckets[ 1533 nextbucket].pgfl_queues[PGFL_ZEROS], 1534 pg, listq.list); 1535 ucpu->pages[PGFL_ZEROS]++; 1536 uvmexp.free++; 1537 uvmexp.zeropages++; 1538 } 1539 } 1540 if (ucpu->pages[PGFL_UNKNOWN] < uvmexp.ncolors) { 1541 break; 1542 } 1543 nextbucket = (nextbucket + 1) & uvmexp.colormask; 1544 } while (nextbucket != firstbucket); 1545 ucpu->page_idle_zero = false; 1546 quit: 1547 mutex_spin_exit(&uvm_fpageqlock); 1548 } 1549 1550 /* 1551 * uvm_pagelookup: look up a page 1552 * 1553 * => caller should lock object to keep someone from pulling the page 1554 * out from under it 1555 */ 1556 1557 struct vm_page * 1558 uvm_pagelookup(struct uvm_object *obj, voff_t off) 1559 { 1560 struct vm_page *pg; 1561 1562 KASSERT(mutex_owned(obj->vmobjlock)); 1563 1564 pg = rb_tree_find_node(&obj->rb_tree, &off); 1565 1566 KASSERT(pg == NULL || obj->uo_npages != 0); 1567 KASSERT(pg == NULL || (pg->flags & (PG_RELEASED|PG_PAGEOUT)) == 0 || 1568 (pg->flags & PG_BUSY) != 0); 1569 return pg; 1570 } 1571 1572 /* 1573 * uvm_pagewire: wire the page, thus removing it from the daemon's grasp 1574 * 1575 * => caller must lock page queues 1576 */ 1577 1578 void 1579 uvm_pagewire(struct vm_page *pg) 1580 { 1581 KASSERT(mutex_owned(&uvm_pageqlock)); 1582 #if defined(READAHEAD_STATS) 1583 if ((pg->pqflags & PQ_READAHEAD) != 0) { 1584 uvm_ra_hit.ev_count++; 1585 pg->pqflags &= ~PQ_READAHEAD; 1586 } 1587 #endif /* defined(READAHEAD_STATS) */ 1588 if (pg->wire_count == 0) { 1589 uvm_pagedequeue(pg); 1590 uvmexp.wired++; 1591 } 1592 pg->wire_count++; 1593 } 1594 1595 /* 1596 * uvm_pageunwire: unwire the page. 1597 * 1598 * => activate if wire count goes to zero. 1599 * => caller must lock page queues 1600 */ 1601 1602 void 1603 uvm_pageunwire(struct vm_page *pg) 1604 { 1605 KASSERT(mutex_owned(&uvm_pageqlock)); 1606 pg->wire_count--; 1607 if (pg->wire_count == 0) { 1608 uvm_pageactivate(pg); 1609 uvmexp.wired--; 1610 } 1611 } 1612 1613 /* 1614 * uvm_pagedeactivate: deactivate page 1615 * 1616 * => caller must lock page queues 1617 * => caller must check to make sure page is not wired 1618 * => object that page belongs to must be locked (so we can adjust pg->flags) 1619 * => caller must clear the reference on the page before calling 1620 */ 1621 1622 void 1623 uvm_pagedeactivate(struct vm_page *pg) 1624 { 1625 1626 KASSERT(mutex_owned(&uvm_pageqlock)); 1627 KASSERT(uvm_page_locked_p(pg)); 1628 KASSERT(pg->wire_count != 0 || uvmpdpol_pageisqueued_p(pg)); 1629 uvmpdpol_pagedeactivate(pg); 1630 } 1631 1632 /* 1633 * uvm_pageactivate: activate page 1634 * 1635 * => caller must lock page queues 1636 */ 1637 1638 void 1639 uvm_pageactivate(struct vm_page *pg) 1640 { 1641 1642 KASSERT(mutex_owned(&uvm_pageqlock)); 1643 KASSERT(uvm_page_locked_p(pg)); 1644 #if defined(READAHEAD_STATS) 1645 if ((pg->pqflags & PQ_READAHEAD) != 0) { 1646 uvm_ra_hit.ev_count++; 1647 pg->pqflags &= ~PQ_READAHEAD; 1648 } 1649 #endif /* defined(READAHEAD_STATS) */ 1650 if (pg->wire_count != 0) { 1651 return; 1652 } 1653 uvmpdpol_pageactivate(pg); 1654 } 1655 1656 /* 1657 * uvm_pagedequeue: remove a page from any paging queue 1658 */ 1659 1660 void 1661 uvm_pagedequeue(struct vm_page *pg) 1662 { 1663 1664 if (uvmpdpol_pageisqueued_p(pg)) { 1665 KASSERT(mutex_owned(&uvm_pageqlock)); 1666 } 1667 1668 uvmpdpol_pagedequeue(pg); 1669 } 1670 1671 /* 1672 * uvm_pageenqueue: add a page to a paging queue without activating. 1673 * used where a page is not really demanded (yet). eg. read-ahead 1674 */ 1675 1676 void 1677 uvm_pageenqueue(struct vm_page *pg) 1678 { 1679 1680 KASSERT(mutex_owned(&uvm_pageqlock)); 1681 if (pg->wire_count != 0) { 1682 return; 1683 } 1684 uvmpdpol_pageenqueue(pg); 1685 } 1686 1687 /* 1688 * uvm_pagezero: zero fill a page 1689 * 1690 * => if page is part of an object then the object should be locked 1691 * to protect pg->flags. 1692 */ 1693 1694 void 1695 uvm_pagezero(struct vm_page *pg) 1696 { 1697 pg->flags &= ~PG_CLEAN; 1698 pmap_zero_page(VM_PAGE_TO_PHYS(pg)); 1699 } 1700 1701 /* 1702 * uvm_pagecopy: copy a page 1703 * 1704 * => if page is part of an object then the object should be locked 1705 * to protect pg->flags. 1706 */ 1707 1708 void 1709 uvm_pagecopy(struct vm_page *src, struct vm_page *dst) 1710 { 1711 1712 dst->flags &= ~PG_CLEAN; 1713 pmap_copy_page(VM_PAGE_TO_PHYS(src), VM_PAGE_TO_PHYS(dst)); 1714 } 1715 1716 /* 1717 * uvm_pageismanaged: test it see that a page (specified by PA) is managed. 1718 */ 1719 1720 bool 1721 uvm_pageismanaged(paddr_t pa) 1722 { 1723 1724 return (uvm_physseg_find(atop(pa), NULL) != UVM_PHYSSEG_TYPE_INVALID); 1725 } 1726 1727 /* 1728 * uvm_page_lookup_freelist: look up the free list for the specified page 1729 */ 1730 1731 int 1732 uvm_page_lookup_freelist(struct vm_page *pg) 1733 { 1734 uvm_physseg_t upm; 1735 1736 upm = uvm_physseg_find(atop(VM_PAGE_TO_PHYS(pg)), NULL); 1737 KASSERT(upm != UVM_PHYSSEG_TYPE_INVALID); 1738 return uvm_physseg_get_free_list(upm); 1739 } 1740 1741 /* 1742 * uvm_page_locked_p: return true if object associated with page is 1743 * locked. this is a weak check for runtime assertions only. 1744 */ 1745 1746 bool 1747 uvm_page_locked_p(struct vm_page *pg) 1748 { 1749 1750 if (pg->uobject != NULL) { 1751 return mutex_owned(pg->uobject->vmobjlock); 1752 } 1753 if (pg->uanon != NULL) { 1754 return mutex_owned(pg->uanon->an_lock); 1755 } 1756 return true; 1757 } 1758 1759 #if defined(DDB) || defined(DEBUGPRINT) 1760 1761 /* 1762 * uvm_page_printit: actually print the page 1763 */ 1764 1765 static const char page_flagbits[] = UVM_PGFLAGBITS; 1766 static const char page_pqflagbits[] = UVM_PQFLAGBITS; 1767 1768 void 1769 uvm_page_printit(struct vm_page *pg, bool full, 1770 void (*pr)(const char *, ...)) 1771 { 1772 struct vm_page *tpg; 1773 struct uvm_object *uobj; 1774 struct pgflist *pgl; 1775 char pgbuf[128]; 1776 char pqbuf[128]; 1777 1778 (*pr)("PAGE %p:\n", pg); 1779 snprintb(pgbuf, sizeof(pgbuf), page_flagbits, pg->flags); 1780 snprintb(pqbuf, sizeof(pqbuf), page_pqflagbits, pg->pqflags); 1781 (*pr)(" flags=%s, pqflags=%s, wire_count=%d, pa=0x%lx\n", 1782 pgbuf, pqbuf, pg->wire_count, (long)VM_PAGE_TO_PHYS(pg)); 1783 (*pr)(" uobject=%p, uanon=%p, offset=0x%llx loan_count=%d\n", 1784 pg->uobject, pg->uanon, (long long)pg->offset, pg->loan_count); 1785 #if defined(UVM_PAGE_TRKOWN) 1786 if (pg->flags & PG_BUSY) 1787 (*pr)(" owning process = %d, tag=%s\n", 1788 pg->owner, pg->owner_tag); 1789 else 1790 (*pr)(" page not busy, no owner\n"); 1791 #else 1792 (*pr)(" [page ownership tracking disabled]\n"); 1793 #endif 1794 1795 if (!full) 1796 return; 1797 1798 /* cross-verify object/anon */ 1799 if ((pg->pqflags & PQ_FREE) == 0) { 1800 if (pg->pqflags & PQ_ANON) { 1801 if (pg->uanon == NULL || pg->uanon->an_page != pg) 1802 (*pr)(" >>> ANON DOES NOT POINT HERE <<< (%p)\n", 1803 (pg->uanon) ? pg->uanon->an_page : NULL); 1804 else 1805 (*pr)(" anon backpointer is OK\n"); 1806 } else { 1807 uobj = pg->uobject; 1808 if (uobj) { 1809 (*pr)(" checking object list\n"); 1810 TAILQ_FOREACH(tpg, &uobj->memq, listq.queue) { 1811 if (tpg == pg) { 1812 break; 1813 } 1814 } 1815 if (tpg) 1816 (*pr)(" page found on object list\n"); 1817 else 1818 (*pr)(" >>> PAGE NOT FOUND ON OBJECT LIST! <<<\n"); 1819 } 1820 } 1821 } 1822 1823 /* cross-verify page queue */ 1824 if (pg->pqflags & PQ_FREE) { 1825 int fl = uvm_page_lookup_freelist(pg); 1826 int color = VM_PGCOLOR_BUCKET(pg); 1827 pgl = &uvm.page_free[fl].pgfl_buckets[color].pgfl_queues[ 1828 ((pg)->flags & PG_ZERO) ? PGFL_ZEROS : PGFL_UNKNOWN]; 1829 } else { 1830 pgl = NULL; 1831 } 1832 1833 if (pgl) { 1834 (*pr)(" checking pageq list\n"); 1835 LIST_FOREACH(tpg, pgl, pageq.list) { 1836 if (tpg == pg) { 1837 break; 1838 } 1839 } 1840 if (tpg) 1841 (*pr)(" page found on pageq list\n"); 1842 else 1843 (*pr)(" >>> PAGE NOT FOUND ON PAGEQ LIST! <<<\n"); 1844 } 1845 } 1846 1847 /* 1848 * uvm_pages_printthem - print a summary of all managed pages 1849 */ 1850 1851 void 1852 uvm_page_printall(void (*pr)(const char *, ...)) 1853 { 1854 uvm_physseg_t i; 1855 paddr_t pfn; 1856 struct vm_page *pg; 1857 1858 (*pr)("%18s %4s %4s %18s %18s" 1859 #ifdef UVM_PAGE_TRKOWN 1860 " OWNER" 1861 #endif 1862 "\n", "PAGE", "FLAG", "PQ", "UOBJECT", "UANON"); 1863 for (i = uvm_physseg_get_first(); 1864 uvm_physseg_valid_p(i); 1865 i = uvm_physseg_get_next(i)) { 1866 for (pfn = uvm_physseg_get_start(i); 1867 pfn < uvm_physseg_get_end(i); 1868 pfn++) { 1869 pg = PHYS_TO_VM_PAGE(ptoa(pfn)); 1870 1871 (*pr)("%18p %04x %04x %18p %18p", 1872 pg, pg->flags, pg->pqflags, pg->uobject, 1873 pg->uanon); 1874 #ifdef UVM_PAGE_TRKOWN 1875 if (pg->flags & PG_BUSY) 1876 (*pr)(" %d [%s]", pg->owner, pg->owner_tag); 1877 #endif 1878 (*pr)("\n"); 1879 } 1880 } 1881 } 1882 1883 #endif /* DDB || DEBUGPRINT */ 1884