1 /* $NetBSD: uvm_page.c,v 1.67 2001/09/15 20:36:46 chs 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. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by Charles D. Cranor, 23 * Washington University, the University of California, Berkeley and 24 * its contributors. 25 * 4. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * @(#)vm_page.c 8.3 (Berkeley) 3/21/94 42 * from: Id: uvm_page.c,v 1.1.2.18 1998/02/06 05:24:42 chs Exp 43 * 44 * 45 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 46 * All rights reserved. 47 * 48 * Permission to use, copy, modify and distribute this software and 49 * its documentation is hereby granted, provided that both the copyright 50 * notice and this permission notice appear in all copies of the 51 * software, derivative works or modified versions, and any portions 52 * thereof, and that both notices appear in supporting documentation. 53 * 54 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 55 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 56 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 57 * 58 * Carnegie Mellon requests users of this software to return to 59 * 60 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 61 * School of Computer Science 62 * Carnegie Mellon University 63 * Pittsburgh PA 15213-3890 64 * 65 * any improvements or extensions that they make and grant Carnegie the 66 * rights to redistribute these changes. 67 */ 68 69 /* 70 * uvm_page.c: page ops. 71 */ 72 73 #include "opt_uvmhist.h" 74 75 #include <sys/param.h> 76 #include <sys/systm.h> 77 #include <sys/malloc.h> 78 #include <sys/sched.h> 79 #include <sys/kernel.h> 80 #include <sys/vnode.h> 81 82 #define UVM_PAGE /* pull in uvm_page.h functions */ 83 #include <uvm/uvm.h> 84 85 /* 86 * global vars... XXXCDC: move to uvm. structure. 87 */ 88 89 /* 90 * physical memory config is stored in vm_physmem. 91 */ 92 93 struct vm_physseg vm_physmem[VM_PHYSSEG_MAX]; /* XXXCDC: uvm.physmem */ 94 int vm_nphysseg = 0; /* XXXCDC: uvm.nphysseg */ 95 96 /* 97 * Some supported CPUs in a given architecture don't support all 98 * of the things necessary to do idle page zero'ing efficiently. 99 * We therefore provide a way to disable it from machdep code here. 100 */ 101 /* 102 * XXX disabled until we can find a way to do this without causing 103 * problems for either cpu caches or DMA latency. 104 */ 105 boolean_t vm_page_zero_enable = FALSE; 106 107 /* 108 * local variables 109 */ 110 111 /* 112 * these variables record the values returned by vm_page_bootstrap, 113 * for debugging purposes. The implementation of uvm_pageboot_alloc 114 * and pmap_startup here also uses them internally. 115 */ 116 117 static vaddr_t virtual_space_start; 118 static vaddr_t virtual_space_end; 119 120 /* 121 * we use a hash table with only one bucket during bootup. we will 122 * later rehash (resize) the hash table once the allocator is ready. 123 * we static allocate the one bootstrap bucket below... 124 */ 125 126 static struct pglist uvm_bootbucket; 127 128 /* 129 * we allocate an initial number of page colors in uvm_page_init(), 130 * and remember them. We may re-color pages as cache sizes are 131 * discovered during the autoconfiguration phase. But we can never 132 * free the initial set of buckets, since they are allocated using 133 * uvm_pageboot_alloc(). 134 */ 135 136 static boolean_t have_recolored_pages /* = FALSE */; 137 138 /* 139 * local prototypes 140 */ 141 142 static void uvm_pageinsert __P((struct vm_page *)); 143 static void uvm_pageremove __P((struct vm_page *)); 144 145 /* 146 * inline functions 147 */ 148 149 /* 150 * uvm_pageinsert: insert a page in the object and the hash table 151 * 152 * => caller must lock object 153 * => caller must lock page queues 154 * => call should have already set pg's object and offset pointers 155 * and bumped the version counter 156 */ 157 158 __inline static void 159 uvm_pageinsert(pg) 160 struct vm_page *pg; 161 { 162 struct pglist *buck; 163 struct uvm_object *uobj = pg->uobject; 164 165 KASSERT((pg->flags & PG_TABLED) == 0); 166 buck = &uvm.page_hash[uvm_pagehash(uobj, pg->offset)]; 167 simple_lock(&uvm.hashlock); 168 TAILQ_INSERT_TAIL(buck, pg, hashq); 169 simple_unlock(&uvm.hashlock); 170 171 TAILQ_INSERT_TAIL(&uobj->memq, pg, listq); 172 pg->flags |= PG_TABLED; 173 uobj->uo_npages++; 174 } 175 176 /* 177 * uvm_page_remove: remove page from object and hash 178 * 179 * => caller must lock object 180 * => caller must lock page queues 181 */ 182 183 static __inline void 184 uvm_pageremove(pg) 185 struct vm_page *pg; 186 { 187 struct pglist *buck; 188 struct uvm_object *uobj = pg->uobject; 189 190 KASSERT(pg->flags & PG_TABLED); 191 buck = &uvm.page_hash[uvm_pagehash(uobj ,pg->offset)]; 192 simple_lock(&uvm.hashlock); 193 TAILQ_REMOVE(buck, pg, hashq); 194 simple_unlock(&uvm.hashlock); 195 196 if (UVM_OBJ_IS_VTEXT(uobj)) { 197 uvmexp.vtextpages--; 198 } else if (UVM_OBJ_IS_VNODE(uobj)) { 199 uvmexp.vnodepages--; 200 } 201 202 /* object should be locked */ 203 uobj->uo_npages--; 204 TAILQ_REMOVE(&uobj->memq, pg, listq); 205 pg->flags &= ~PG_TABLED; 206 pg->uobject = NULL; 207 } 208 209 static void 210 uvm_page_init_buckets(struct pgfreelist *pgfl) 211 { 212 int color, i; 213 214 for (color = 0; color < uvmexp.ncolors; color++) { 215 for (i = 0; i < PGFL_NQUEUES; i++) { 216 TAILQ_INIT(&pgfl->pgfl_buckets[ 217 color].pgfl_queues[i]); 218 } 219 } 220 } 221 222 /* 223 * uvm_page_init: init the page system. called from uvm_init(). 224 * 225 * => we return the range of kernel virtual memory in kvm_startp/kvm_endp 226 */ 227 228 void 229 uvm_page_init(kvm_startp, kvm_endp) 230 vaddr_t *kvm_startp, *kvm_endp; 231 { 232 vsize_t freepages, pagecount, bucketcount, n; 233 struct pgflbucket *bucketarray; 234 struct vm_page *pagearray; 235 int lcv, i; 236 paddr_t paddr; 237 238 /* 239 * init the page queues and page queue locks, except the free 240 * list; we allocate that later (with the initial vm_page 241 * structures). 242 */ 243 244 TAILQ_INIT(&uvm.page_active); 245 TAILQ_INIT(&uvm.page_inactive); 246 simple_lock_init(&uvm.pageqlock); 247 simple_lock_init(&uvm.fpageqlock); 248 249 /* 250 * init the <obj,offset> => <page> hash table. for now 251 * we just have one bucket (the bootstrap bucket). later on we 252 * will allocate new buckets as we dynamically resize the hash table. 253 */ 254 255 uvm.page_nhash = 1; /* 1 bucket */ 256 uvm.page_hashmask = 0; /* mask for hash function */ 257 uvm.page_hash = &uvm_bootbucket; /* install bootstrap bucket */ 258 TAILQ_INIT(uvm.page_hash); /* init hash table */ 259 simple_lock_init(&uvm.hashlock); /* init hash table lock */ 260 261 /* 262 * allocate vm_page structures. 263 */ 264 265 /* 266 * sanity check: 267 * before calling this function the MD code is expected to register 268 * some free RAM with the uvm_page_physload() function. our job 269 * now is to allocate vm_page structures for this memory. 270 */ 271 272 if (vm_nphysseg == 0) 273 panic("uvm_page_bootstrap: no memory pre-allocated"); 274 275 /* 276 * first calculate the number of free pages... 277 * 278 * note that we use start/end rather than avail_start/avail_end. 279 * this allows us to allocate extra vm_page structures in case we 280 * want to return some memory to the pool after booting. 281 */ 282 283 freepages = 0; 284 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) 285 freepages += (vm_physmem[lcv].end - vm_physmem[lcv].start); 286 287 /* 288 * Let MD code initialize the number of colors, or default 289 * to 1 color if MD code doesn't care. 290 */ 291 if (uvmexp.ncolors == 0) 292 uvmexp.ncolors = 1; 293 uvmexp.colormask = uvmexp.ncolors - 1; 294 295 /* 296 * we now know we have (PAGE_SIZE * freepages) bytes of memory we can 297 * use. for each page of memory we use we need a vm_page structure. 298 * thus, the total number of pages we can use is the total size of 299 * the memory divided by the PAGE_SIZE plus the size of the vm_page 300 * structure. we add one to freepages as a fudge factor to avoid 301 * truncation errors (since we can only allocate in terms of whole 302 * pages). 303 */ 304 305 bucketcount = uvmexp.ncolors * VM_NFREELIST; 306 pagecount = ((freepages + 1) << PAGE_SHIFT) / 307 (PAGE_SIZE + sizeof(struct vm_page)); 308 309 bucketarray = (void *)uvm_pageboot_alloc((bucketcount * 310 sizeof(struct pgflbucket)) + (pagecount * 311 sizeof(struct vm_page))); 312 pagearray = (struct vm_page *)(bucketarray + bucketcount); 313 314 for (lcv = 0; lcv < VM_NFREELIST; lcv++) { 315 uvm.page_free[lcv].pgfl_buckets = 316 (bucketarray + (lcv * uvmexp.ncolors)); 317 uvm_page_init_buckets(&uvm.page_free[lcv]); 318 } 319 memset(pagearray, 0, pagecount * sizeof(struct vm_page)); 320 321 /* 322 * init the vm_page structures and put them in the correct place. 323 */ 324 325 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) { 326 n = vm_physmem[lcv].end - vm_physmem[lcv].start; 327 328 /* set up page array pointers */ 329 vm_physmem[lcv].pgs = pagearray; 330 pagearray += n; 331 pagecount -= n; 332 vm_physmem[lcv].lastpg = vm_physmem[lcv].pgs + (n - 1); 333 334 /* init and free vm_pages (we've already zeroed them) */ 335 paddr = ptoa(vm_physmem[lcv].start); 336 for (i = 0 ; i < n ; i++, paddr += PAGE_SIZE) { 337 vm_physmem[lcv].pgs[i].phys_addr = paddr; 338 #ifdef __HAVE_VM_PAGE_MD 339 VM_MDPAGE_INIT(&vm_physmem[lcv].pgs[i]); 340 #endif 341 if (atop(paddr) >= vm_physmem[lcv].avail_start && 342 atop(paddr) <= vm_physmem[lcv].avail_end) { 343 uvmexp.npages++; 344 /* add page to free pool */ 345 uvm_pagefree(&vm_physmem[lcv].pgs[i]); 346 } 347 } 348 } 349 350 /* 351 * pass up the values of virtual_space_start and 352 * virtual_space_end (obtained by uvm_pageboot_alloc) to the upper 353 * layers of the VM. 354 */ 355 356 *kvm_startp = round_page(virtual_space_start); 357 *kvm_endp = trunc_page(virtual_space_end); 358 359 /* 360 * init locks for kernel threads 361 */ 362 363 simple_lock_init(&uvm.pagedaemon_lock); 364 simple_lock_init(&uvm.aiodoned_lock); 365 366 /* 367 * init various thresholds. 368 * XXXCDC - values may need adjusting 369 */ 370 371 uvmexp.reserve_pagedaemon = 1; 372 uvmexp.reserve_kernel = 5; 373 uvmexp.anonminpct = 10; 374 uvmexp.vnodeminpct = 10; 375 uvmexp.vtextminpct = 5; 376 uvmexp.anonmin = uvmexp.anonminpct * 256 / 100; 377 uvmexp.vnodemin = uvmexp.vnodeminpct * 256 / 100; 378 uvmexp.vtextmin = uvmexp.vtextminpct * 256 / 100; 379 380 /* 381 * determine if we should zero pages in the idle loop. 382 */ 383 384 uvm.page_idle_zero = vm_page_zero_enable; 385 386 /* 387 * done! 388 */ 389 390 uvm.page_init_done = TRUE; 391 } 392 393 /* 394 * uvm_setpagesize: set the page size 395 * 396 * => sets page_shift and page_mask from uvmexp.pagesize. 397 */ 398 399 void 400 uvm_setpagesize() 401 { 402 if (uvmexp.pagesize == 0) 403 uvmexp.pagesize = DEFAULT_PAGE_SIZE; 404 uvmexp.pagemask = uvmexp.pagesize - 1; 405 if ((uvmexp.pagemask & uvmexp.pagesize) != 0) 406 panic("uvm_setpagesize: page size not a power of two"); 407 for (uvmexp.pageshift = 0; ; uvmexp.pageshift++) 408 if ((1 << uvmexp.pageshift) == uvmexp.pagesize) 409 break; 410 } 411 412 /* 413 * uvm_pageboot_alloc: steal memory from physmem for bootstrapping 414 */ 415 416 vaddr_t 417 uvm_pageboot_alloc(size) 418 vsize_t size; 419 { 420 static boolean_t initialized = FALSE; 421 vaddr_t addr; 422 #if !defined(PMAP_STEAL_MEMORY) 423 vaddr_t vaddr; 424 paddr_t paddr; 425 #endif 426 427 /* 428 * on first call to this function, initialize ourselves. 429 */ 430 if (initialized == FALSE) { 431 pmap_virtual_space(&virtual_space_start, &virtual_space_end); 432 433 /* round it the way we like it */ 434 virtual_space_start = round_page(virtual_space_start); 435 virtual_space_end = trunc_page(virtual_space_end); 436 437 initialized = TRUE; 438 } 439 440 /* round to page size */ 441 size = round_page(size); 442 443 #if defined(PMAP_STEAL_MEMORY) 444 445 /* 446 * defer bootstrap allocation to MD code (it may want to allocate 447 * from a direct-mapped segment). pmap_steal_memory should adjust 448 * virtual_space_start/virtual_space_end if necessary. 449 */ 450 451 addr = pmap_steal_memory(size, &virtual_space_start, 452 &virtual_space_end); 453 454 return(addr); 455 456 #else /* !PMAP_STEAL_MEMORY */ 457 458 /* 459 * allocate virtual memory for this request 460 */ 461 if (virtual_space_start == virtual_space_end || 462 (virtual_space_end - virtual_space_start) < size) 463 panic("uvm_pageboot_alloc: out of virtual space"); 464 465 addr = virtual_space_start; 466 467 #ifdef PMAP_GROWKERNEL 468 /* 469 * If the kernel pmap can't map the requested space, 470 * then allocate more resources for it. 471 */ 472 if (uvm_maxkaddr < (addr + size)) { 473 uvm_maxkaddr = pmap_growkernel(addr + size); 474 if (uvm_maxkaddr < (addr + size)) 475 panic("uvm_pageboot_alloc: pmap_growkernel() failed"); 476 } 477 #endif 478 479 virtual_space_start += size; 480 481 /* 482 * allocate and mapin physical pages to back new virtual pages 483 */ 484 485 for (vaddr = round_page(addr) ; vaddr < addr + size ; 486 vaddr += PAGE_SIZE) { 487 488 if (!uvm_page_physget(&paddr)) 489 panic("uvm_pageboot_alloc: out of memory"); 490 491 /* 492 * Note this memory is no longer managed, so using 493 * pmap_kenter is safe. 494 */ 495 pmap_kenter_pa(vaddr, paddr, VM_PROT_READ|VM_PROT_WRITE); 496 } 497 pmap_update(pmap_kernel()); 498 return(addr); 499 #endif /* PMAP_STEAL_MEMORY */ 500 } 501 502 #if !defined(PMAP_STEAL_MEMORY) 503 /* 504 * uvm_page_physget: "steal" one page from the vm_physmem structure. 505 * 506 * => attempt to allocate it off the end of a segment in which the "avail" 507 * values match the start/end values. if we can't do that, then we 508 * will advance both values (making them equal, and removing some 509 * vm_page structures from the non-avail area). 510 * => return false if out of memory. 511 */ 512 513 /* subroutine: try to allocate from memory chunks on the specified freelist */ 514 static boolean_t uvm_page_physget_freelist __P((paddr_t *, int)); 515 516 static boolean_t 517 uvm_page_physget_freelist(paddrp, freelist) 518 paddr_t *paddrp; 519 int freelist; 520 { 521 int lcv, x; 522 523 /* pass 1: try allocating from a matching end */ 524 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST) 525 for (lcv = vm_nphysseg - 1 ; lcv >= 0 ; lcv--) 526 #else 527 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) 528 #endif 529 { 530 531 if (uvm.page_init_done == TRUE) 532 panic("uvm_page_physget: called _after_ bootstrap"); 533 534 if (vm_physmem[lcv].free_list != freelist) 535 continue; 536 537 /* try from front */ 538 if (vm_physmem[lcv].avail_start == vm_physmem[lcv].start && 539 vm_physmem[lcv].avail_start < vm_physmem[lcv].avail_end) { 540 *paddrp = ptoa(vm_physmem[lcv].avail_start); 541 vm_physmem[lcv].avail_start++; 542 vm_physmem[lcv].start++; 543 /* nothing left? nuke it */ 544 if (vm_physmem[lcv].avail_start == 545 vm_physmem[lcv].end) { 546 if (vm_nphysseg == 1) 547 panic("vum_page_physget: out of memory!"); 548 vm_nphysseg--; 549 for (x = lcv ; x < vm_nphysseg ; x++) 550 /* structure copy */ 551 vm_physmem[x] = vm_physmem[x+1]; 552 } 553 return (TRUE); 554 } 555 556 /* try from rear */ 557 if (vm_physmem[lcv].avail_end == vm_physmem[lcv].end && 558 vm_physmem[lcv].avail_start < vm_physmem[lcv].avail_end) { 559 *paddrp = ptoa(vm_physmem[lcv].avail_end - 1); 560 vm_physmem[lcv].avail_end--; 561 vm_physmem[lcv].end--; 562 /* nothing left? nuke it */ 563 if (vm_physmem[lcv].avail_end == 564 vm_physmem[lcv].start) { 565 if (vm_nphysseg == 1) 566 panic("uvm_page_physget: out of memory!"); 567 vm_nphysseg--; 568 for (x = lcv ; x < vm_nphysseg ; x++) 569 /* structure copy */ 570 vm_physmem[x] = vm_physmem[x+1]; 571 } 572 return (TRUE); 573 } 574 } 575 576 /* pass2: forget about matching ends, just allocate something */ 577 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST) 578 for (lcv = vm_nphysseg - 1 ; lcv >= 0 ; lcv--) 579 #else 580 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) 581 #endif 582 { 583 584 /* any room in this bank? */ 585 if (vm_physmem[lcv].avail_start >= vm_physmem[lcv].avail_end) 586 continue; /* nope */ 587 588 *paddrp = ptoa(vm_physmem[lcv].avail_start); 589 vm_physmem[lcv].avail_start++; 590 /* truncate! */ 591 vm_physmem[lcv].start = vm_physmem[lcv].avail_start; 592 593 /* nothing left? nuke it */ 594 if (vm_physmem[lcv].avail_start == vm_physmem[lcv].end) { 595 if (vm_nphysseg == 1) 596 panic("uvm_page_physget: out of memory!"); 597 vm_nphysseg--; 598 for (x = lcv ; x < vm_nphysseg ; x++) 599 /* structure copy */ 600 vm_physmem[x] = vm_physmem[x+1]; 601 } 602 return (TRUE); 603 } 604 605 return (FALSE); /* whoops! */ 606 } 607 608 boolean_t 609 uvm_page_physget(paddrp) 610 paddr_t *paddrp; 611 { 612 int i; 613 614 /* try in the order of freelist preference */ 615 for (i = 0; i < VM_NFREELIST; i++) 616 if (uvm_page_physget_freelist(paddrp, i) == TRUE) 617 return (TRUE); 618 return (FALSE); 619 } 620 #endif /* PMAP_STEAL_MEMORY */ 621 622 /* 623 * uvm_page_physload: load physical memory into VM system 624 * 625 * => all args are PFs 626 * => all pages in start/end get vm_page structures 627 * => areas marked by avail_start/avail_end get added to the free page pool 628 * => we are limited to VM_PHYSSEG_MAX physical memory segments 629 */ 630 631 void 632 uvm_page_physload(start, end, avail_start, avail_end, free_list) 633 paddr_t start, end, avail_start, avail_end; 634 int free_list; 635 { 636 int preload, lcv; 637 psize_t npages; 638 struct vm_page *pgs; 639 struct vm_physseg *ps; 640 641 if (uvmexp.pagesize == 0) 642 panic("uvm_page_physload: page size not set!"); 643 if (free_list >= VM_NFREELIST || free_list < VM_FREELIST_DEFAULT) 644 panic("uvm_page_physload: bad free list %d\n", free_list); 645 if (start >= end) 646 panic("uvm_page_physload: start >= end"); 647 648 /* 649 * do we have room? 650 */ 651 652 if (vm_nphysseg == VM_PHYSSEG_MAX) { 653 printf("uvm_page_physload: unable to load physical memory " 654 "segment\n"); 655 printf("\t%d segments allocated, ignoring 0x%llx -> 0x%llx\n", 656 VM_PHYSSEG_MAX, (long long)start, (long long)end); 657 printf("\tincrease VM_PHYSSEG_MAX\n"); 658 return; 659 } 660 661 /* 662 * check to see if this is a "preload" (i.e. uvm_mem_init hasn't been 663 * called yet, so malloc is not available). 664 */ 665 666 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) { 667 if (vm_physmem[lcv].pgs) 668 break; 669 } 670 preload = (lcv == vm_nphysseg); 671 672 /* 673 * if VM is already running, attempt to malloc() vm_page structures 674 */ 675 676 if (!preload) { 677 #if defined(VM_PHYSSEG_NOADD) 678 panic("uvm_page_physload: tried to add RAM after vm_mem_init"); 679 #else 680 /* XXXCDC: need some sort of lockout for this case */ 681 paddr_t paddr; 682 npages = end - start; /* # of pages */ 683 pgs = malloc(sizeof(struct vm_page) * npages, 684 M_VMPAGE, M_NOWAIT); 685 if (pgs == NULL) { 686 printf("uvm_page_physload: can not malloc vm_page " 687 "structs for segment\n"); 688 printf("\tignoring 0x%lx -> 0x%lx\n", start, end); 689 return; 690 } 691 /* zero data, init phys_addr and free_list, and free pages */ 692 memset(pgs, 0, sizeof(struct vm_page) * npages); 693 for (lcv = 0, paddr = ptoa(start) ; 694 lcv < npages ; lcv++, paddr += PAGE_SIZE) { 695 pgs[lcv].phys_addr = paddr; 696 pgs[lcv].free_list = free_list; 697 if (atop(paddr) >= avail_start && 698 atop(paddr) <= avail_end) 699 uvm_pagefree(&pgs[lcv]); 700 } 701 /* XXXCDC: incomplete: need to update uvmexp.free, what else? */ 702 /* XXXCDC: need hook to tell pmap to rebuild pv_list, etc... */ 703 #endif 704 } else { 705 pgs = NULL; 706 npages = 0; 707 } 708 709 /* 710 * now insert us in the proper place in vm_physmem[] 711 */ 712 713 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_RANDOM) 714 /* random: put it at the end (easy!) */ 715 ps = &vm_physmem[vm_nphysseg]; 716 #elif (VM_PHYSSEG_STRAT == VM_PSTRAT_BSEARCH) 717 { 718 int x; 719 /* sort by address for binary search */ 720 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) 721 if (start < vm_physmem[lcv].start) 722 break; 723 ps = &vm_physmem[lcv]; 724 /* move back other entries, if necessary ... */ 725 for (x = vm_nphysseg ; x > lcv ; x--) 726 /* structure copy */ 727 vm_physmem[x] = vm_physmem[x - 1]; 728 } 729 #elif (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST) 730 { 731 int x; 732 /* sort by largest segment first */ 733 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) 734 if ((end - start) > 735 (vm_physmem[lcv].end - vm_physmem[lcv].start)) 736 break; 737 ps = &vm_physmem[lcv]; 738 /* move back other entries, if necessary ... */ 739 for (x = vm_nphysseg ; x > lcv ; x--) 740 /* structure copy */ 741 vm_physmem[x] = vm_physmem[x - 1]; 742 } 743 #else 744 panic("uvm_page_physload: unknown physseg strategy selected!"); 745 #endif 746 747 ps->start = start; 748 ps->end = end; 749 ps->avail_start = avail_start; 750 ps->avail_end = avail_end; 751 if (preload) { 752 ps->pgs = NULL; 753 } else { 754 ps->pgs = pgs; 755 ps->lastpg = pgs + npages - 1; 756 } 757 ps->free_list = free_list; 758 vm_nphysseg++; 759 760 if (!preload) 761 uvm_page_rehash(); 762 } 763 764 /* 765 * uvm_page_rehash: reallocate hash table based on number of free pages. 766 */ 767 768 void 769 uvm_page_rehash() 770 { 771 int freepages, lcv, bucketcount, oldcount; 772 struct pglist *newbuckets, *oldbuckets; 773 struct vm_page *pg; 774 size_t newsize, oldsize; 775 776 /* 777 * compute number of pages that can go in the free pool 778 */ 779 780 freepages = 0; 781 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) 782 freepages += 783 (vm_physmem[lcv].avail_end - vm_physmem[lcv].avail_start); 784 785 /* 786 * compute number of buckets needed for this number of pages 787 */ 788 789 bucketcount = 1; 790 while (bucketcount < freepages) 791 bucketcount = bucketcount * 2; 792 793 /* 794 * compute the size of the current table and new table. 795 */ 796 797 oldbuckets = uvm.page_hash; 798 oldcount = uvm.page_nhash; 799 oldsize = round_page(sizeof(struct pglist) * oldcount); 800 newsize = round_page(sizeof(struct pglist) * bucketcount); 801 802 /* 803 * allocate the new buckets 804 */ 805 806 newbuckets = (struct pglist *) uvm_km_alloc(kernel_map, newsize); 807 if (newbuckets == NULL) { 808 printf("uvm_page_physrehash: WARNING: could not grow page " 809 "hash table\n"); 810 return; 811 } 812 for (lcv = 0 ; lcv < bucketcount ; lcv++) 813 TAILQ_INIT(&newbuckets[lcv]); 814 815 /* 816 * now replace the old buckets with the new ones and rehash everything 817 */ 818 819 simple_lock(&uvm.hashlock); 820 uvm.page_hash = newbuckets; 821 uvm.page_nhash = bucketcount; 822 uvm.page_hashmask = bucketcount - 1; /* power of 2 */ 823 824 /* ... and rehash */ 825 for (lcv = 0 ; lcv < oldcount ; lcv++) { 826 while ((pg = oldbuckets[lcv].tqh_first) != NULL) { 827 TAILQ_REMOVE(&oldbuckets[lcv], pg, hashq); 828 TAILQ_INSERT_TAIL( 829 &uvm.page_hash[uvm_pagehash(pg->uobject, pg->offset)], 830 pg, hashq); 831 } 832 } 833 simple_unlock(&uvm.hashlock); 834 835 /* 836 * free old bucket array if is not the boot-time table 837 */ 838 839 if (oldbuckets != &uvm_bootbucket) 840 uvm_km_free(kernel_map, (vaddr_t) oldbuckets, oldsize); 841 } 842 843 /* 844 * uvm_page_recolor: Recolor the pages if the new bucket count is 845 * larger than the old one. 846 */ 847 848 void 849 uvm_page_recolor(int newncolors) 850 { 851 struct pgflbucket *bucketarray, *oldbucketarray; 852 struct pgfreelist pgfl; 853 struct vm_page *pg; 854 vsize_t bucketcount; 855 int s, lcv, color, i, ocolors; 856 857 if (newncolors <= uvmexp.ncolors) 858 return; 859 860 bucketcount = newncolors * VM_NFREELIST; 861 bucketarray = malloc(bucketcount * sizeof(struct pgflbucket), 862 M_VMPAGE, M_NOWAIT); 863 if (bucketarray == NULL) { 864 printf("WARNING: unable to allocate %ld page color buckets\n", 865 (long) bucketcount); 866 return; 867 } 868 869 s = uvm_lock_fpageq(); 870 871 /* Make sure we should still do this. */ 872 if (newncolors <= uvmexp.ncolors) { 873 uvm_unlock_fpageq(s); 874 free(bucketarray, M_VMPAGE); 875 return; 876 } 877 878 oldbucketarray = uvm.page_free[0].pgfl_buckets; 879 ocolors = uvmexp.ncolors; 880 881 uvmexp.ncolors = newncolors; 882 uvmexp.colormask = uvmexp.ncolors - 1; 883 884 for (lcv = 0; lcv < VM_NFREELIST; lcv++) { 885 pgfl.pgfl_buckets = (bucketarray + (lcv * newncolors)); 886 uvm_page_init_buckets(&pgfl); 887 for (color = 0; color < ocolors; color++) { 888 for (i = 0; i < PGFL_NQUEUES; i++) { 889 while ((pg = TAILQ_FIRST(&uvm.page_free[ 890 lcv].pgfl_buckets[color].pgfl_queues[i])) 891 != NULL) { 892 TAILQ_REMOVE(&uvm.page_free[ 893 lcv].pgfl_buckets[ 894 color].pgfl_queues[i], pg, pageq); 895 TAILQ_INSERT_TAIL(&pgfl.pgfl_buckets[ 896 VM_PGCOLOR_BUCKET(pg)].pgfl_queues[ 897 i], pg, pageq); 898 } 899 } 900 } 901 uvm.page_free[lcv].pgfl_buckets = pgfl.pgfl_buckets; 902 } 903 904 if (have_recolored_pages) { 905 uvm_unlock_fpageq(s); 906 free(oldbucketarray, M_VMPAGE); 907 return; 908 } 909 910 have_recolored_pages = TRUE; 911 uvm_unlock_fpageq(s); 912 } 913 914 /* 915 * uvm_pagealloc_pgfl: helper routine for uvm_pagealloc_strat 916 */ 917 918 static __inline struct vm_page * 919 uvm_pagealloc_pgfl(struct pgfreelist *pgfl, int try1, int try2, 920 unsigned int *trycolorp) 921 { 922 struct pglist *freeq; 923 struct vm_page *pg; 924 int color, trycolor = *trycolorp; 925 926 color = trycolor; 927 do { 928 if ((pg = TAILQ_FIRST((freeq = 929 &pgfl->pgfl_buckets[color].pgfl_queues[try1]))) != NULL) 930 goto gotit; 931 if ((pg = TAILQ_FIRST((freeq = 932 &pgfl->pgfl_buckets[color].pgfl_queues[try2]))) != NULL) 933 goto gotit; 934 color = (color + 1) & uvmexp.colormask; 935 } while (color != trycolor); 936 937 return (NULL); 938 939 gotit: 940 TAILQ_REMOVE(freeq, pg, pageq); 941 uvmexp.free--; 942 943 /* update zero'd page count */ 944 if (pg->flags & PG_ZERO) 945 uvmexp.zeropages--; 946 947 if (color == trycolor) 948 uvmexp.colorhit++; 949 else { 950 uvmexp.colormiss++; 951 *trycolorp = color; 952 } 953 954 return (pg); 955 } 956 957 /* 958 * uvm_pagealloc_strat: allocate vm_page from a particular free list. 959 * 960 * => return null if no pages free 961 * => wake up pagedaemon if number of free pages drops below low water mark 962 * => if obj != NULL, obj must be locked (to put in hash) 963 * => if anon != NULL, anon must be locked (to put in anon) 964 * => only one of obj or anon can be non-null 965 * => caller must activate/deactivate page if it is not wired. 966 * => free_list is ignored if strat == UVM_PGA_STRAT_NORMAL. 967 * => policy decision: it is more important to pull a page off of the 968 * appropriate priority free list than it is to get a zero'd or 969 * unknown contents page. This is because we live with the 970 * consequences of a bad free list decision for the entire 971 * lifetime of the page, e.g. if the page comes from memory that 972 * is slower to access. 973 */ 974 975 struct vm_page * 976 uvm_pagealloc_strat(obj, off, anon, flags, strat, free_list) 977 struct uvm_object *obj; 978 voff_t off; 979 int flags; 980 struct vm_anon *anon; 981 int strat, free_list; 982 { 983 int lcv, try1, try2, s, zeroit = 0, color; 984 struct vm_page *pg; 985 boolean_t use_reserve; 986 987 KASSERT(obj == NULL || anon == NULL); 988 KASSERT(off == trunc_page(off)); 989 LOCK_ASSERT(obj == NULL || simple_lock_held(&obj->vmobjlock)); 990 LOCK_ASSERT(anon == NULL || simple_lock_held(&anon->an_lock)); 991 992 s = uvm_lock_fpageq(); 993 994 /* 995 * This implements a global round-robin page coloring 996 * algorithm. 997 * 998 * XXXJRT: Should we make the `nextcolor' per-cpu? 999 * XXXJRT: What about virtually-indexed caches? 1000 */ 1001 1002 color = uvm.page_free_nextcolor; 1003 1004 /* 1005 * check to see if we need to generate some free pages waking 1006 * the pagedaemon. 1007 */ 1008 1009 UVM_KICK_PDAEMON(); 1010 1011 /* 1012 * fail if any of these conditions is true: 1013 * [1] there really are no free pages, or 1014 * [2] only kernel "reserved" pages remain and 1015 * the page isn't being allocated to a kernel object. 1016 * [3] only pagedaemon "reserved" pages remain and 1017 * the requestor isn't the pagedaemon. 1018 */ 1019 1020 use_reserve = (flags & UVM_PGA_USERESERVE) || 1021 (obj && UVM_OBJ_IS_KERN_OBJECT(obj)); 1022 if ((uvmexp.free <= uvmexp.reserve_kernel && !use_reserve) || 1023 (uvmexp.free <= uvmexp.reserve_pagedaemon && 1024 !(use_reserve && curproc == uvm.pagedaemon_proc))) 1025 goto fail; 1026 1027 #if PGFL_NQUEUES != 2 1028 #error uvm_pagealloc_strat needs to be updated 1029 #endif 1030 1031 /* 1032 * If we want a zero'd page, try the ZEROS queue first, otherwise 1033 * we try the UNKNOWN queue first. 1034 */ 1035 if (flags & UVM_PGA_ZERO) { 1036 try1 = PGFL_ZEROS; 1037 try2 = PGFL_UNKNOWN; 1038 } else { 1039 try1 = PGFL_UNKNOWN; 1040 try2 = PGFL_ZEROS; 1041 } 1042 1043 again: 1044 switch (strat) { 1045 case UVM_PGA_STRAT_NORMAL: 1046 /* Check all freelists in descending priority order. */ 1047 for (lcv = 0; lcv < VM_NFREELIST; lcv++) { 1048 pg = uvm_pagealloc_pgfl(&uvm.page_free[lcv], 1049 try1, try2, &color); 1050 if (pg != NULL) 1051 goto gotit; 1052 } 1053 1054 /* No pages free! */ 1055 goto fail; 1056 1057 case UVM_PGA_STRAT_ONLY: 1058 case UVM_PGA_STRAT_FALLBACK: 1059 /* Attempt to allocate from the specified free list. */ 1060 KASSERT(free_list >= 0 && free_list < VM_NFREELIST); 1061 pg = uvm_pagealloc_pgfl(&uvm.page_free[free_list], 1062 try1, try2, &color); 1063 if (pg != NULL) 1064 goto gotit; 1065 1066 /* Fall back, if possible. */ 1067 if (strat == UVM_PGA_STRAT_FALLBACK) { 1068 strat = UVM_PGA_STRAT_NORMAL; 1069 goto again; 1070 } 1071 1072 /* No pages free! */ 1073 goto fail; 1074 1075 default: 1076 panic("uvm_pagealloc_strat: bad strat %d", strat); 1077 /* NOTREACHED */ 1078 } 1079 1080 gotit: 1081 /* 1082 * We now know which color we actually allocated from; set 1083 * the next color accordingly. 1084 */ 1085 1086 uvm.page_free_nextcolor = (color + 1) & uvmexp.colormask; 1087 1088 /* 1089 * update allocation statistics and remember if we have to 1090 * zero the page 1091 */ 1092 1093 if (flags & UVM_PGA_ZERO) { 1094 if (pg->flags & PG_ZERO) { 1095 uvmexp.pga_zerohit++; 1096 zeroit = 0; 1097 } else { 1098 uvmexp.pga_zeromiss++; 1099 zeroit = 1; 1100 } 1101 } 1102 uvm_unlock_fpageq(s); 1103 1104 pg->offset = off; 1105 pg->uobject = obj; 1106 pg->uanon = anon; 1107 pg->flags = PG_BUSY|PG_CLEAN|PG_FAKE; 1108 if (anon) { 1109 anon->u.an_page = pg; 1110 pg->pqflags = PQ_ANON; 1111 uvmexp.anonpages++; 1112 } else { 1113 if (obj) { 1114 uvm_pageinsert(pg); 1115 } 1116 pg->pqflags = 0; 1117 } 1118 #if defined(UVM_PAGE_TRKOWN) 1119 pg->owner_tag = NULL; 1120 #endif 1121 UVM_PAGE_OWN(pg, "new alloc"); 1122 1123 if (flags & UVM_PGA_ZERO) { 1124 /* 1125 * A zero'd page is not clean. If we got a page not already 1126 * zero'd, then we have to zero it ourselves. 1127 */ 1128 pg->flags &= ~PG_CLEAN; 1129 if (zeroit) 1130 pmap_zero_page(VM_PAGE_TO_PHYS(pg)); 1131 } 1132 1133 return(pg); 1134 1135 fail: 1136 uvm_unlock_fpageq(s); 1137 return (NULL); 1138 } 1139 1140 /* 1141 * uvm_pagerealloc: reallocate a page from one object to another 1142 * 1143 * => both objects must be locked 1144 */ 1145 1146 void 1147 uvm_pagerealloc(pg, newobj, newoff) 1148 struct vm_page *pg; 1149 struct uvm_object *newobj; 1150 voff_t newoff; 1151 { 1152 /* 1153 * remove it from the old object 1154 */ 1155 1156 if (pg->uobject) { 1157 uvm_pageremove(pg); 1158 } 1159 1160 /* 1161 * put it in the new object 1162 */ 1163 1164 if (newobj) { 1165 pg->uobject = newobj; 1166 pg->offset = newoff; 1167 uvm_pageinsert(pg); 1168 } 1169 } 1170 1171 /* 1172 * uvm_pagefree: free page 1173 * 1174 * => erase page's identity (i.e. remove from hash/object) 1175 * => put page on free list 1176 * => caller must lock owning object (either anon or uvm_object) 1177 * => caller must lock page queues 1178 * => assumes all valid mappings of pg are gone 1179 */ 1180 1181 void 1182 uvm_pagefree(pg) 1183 struct vm_page *pg; 1184 { 1185 int s; 1186 1187 KASSERT((pg->flags & PG_PAGEOUT) == 0); 1188 LOCK_ASSERT(simple_lock_held(&uvm.pageqlock) || 1189 (pg->pqflags & (PQ_ACTIVE|PQ_INACTIVE)) == 0); 1190 1191 #ifdef DEBUG 1192 if (pg->uobject == (void *)0xdeadbeef && 1193 pg->uanon == (void *)0xdeadbeef) { 1194 panic("uvm_pagefree: freeing free page %p\n", pg); 1195 } 1196 #endif 1197 1198 /* 1199 * if the page is loaned, resolve the loan instead of freeing. 1200 */ 1201 1202 if (pg->loan_count) { 1203 1204 /* 1205 * if the page is owned by an anon then we just want to 1206 * drop ownership and return. the kernel will free the page 1207 * when it is done with it. if the page is owned by an object, 1208 * mark it clean for the benefit of possible anon owners 1209 * and mark it as anon-owned if there current anon loanees. 1210 */ 1211 1212 if (pg->pqflags & PQ_ANON) { 1213 pg->pqflags &= ~PQ_ANON; 1214 pg->uanon = NULL; 1215 } else if (pg->flags & PG_TABLED) { 1216 pg->flags &= ~PG_CLEAN; 1217 if (pg->uanon) { 1218 pg->pqflags |= PQ_ANON; 1219 } 1220 } 1221 return; 1222 } 1223 1224 /* 1225 * remove page from its object or anon. 1226 * adjust swpgonly if the page is swap-backed. 1227 */ 1228 1229 if (pg->flags & PG_TABLED) { 1230 if (pg->pqflags & PQ_AOBJ && 1231 uao_find_swslot(pg->uobject, 1232 pg->offset >> PAGE_SHIFT) != 0) { 1233 simple_lock(&uvm.swap_data_lock); 1234 uvmexp.swpgonly++; 1235 simple_unlock(&uvm.swap_data_lock); 1236 } 1237 uvm_pageremove(pg); 1238 } else if (pg->pqflags & PQ_ANON) { 1239 if (pg->uanon->an_swslot) { 1240 simple_lock(&uvm.swap_data_lock); 1241 uvmexp.swpgonly++; 1242 simple_unlock(&uvm.swap_data_lock); 1243 } 1244 pg->uanon->u.an_page = NULL; 1245 } 1246 1247 /* 1248 * now remove the page from the queues 1249 */ 1250 1251 uvm_pagedequeue(pg); 1252 1253 /* 1254 * if the page was wired, unwire it now. 1255 */ 1256 1257 if (pg->wire_count) { 1258 pg->wire_count = 0; 1259 uvmexp.wired--; 1260 } 1261 if (pg->pqflags & PQ_ANON) { 1262 uvmexp.anonpages--; 1263 } 1264 1265 /* 1266 * and put on free queue 1267 */ 1268 1269 pg->flags &= ~PG_ZERO; 1270 1271 s = uvm_lock_fpageq(); 1272 TAILQ_INSERT_TAIL(&uvm.page_free[ 1273 uvm_page_lookup_freelist(pg)].pgfl_buckets[ 1274 VM_PGCOLOR_BUCKET(pg)].pgfl_queues[PGFL_UNKNOWN], pg, pageq); 1275 pg->pqflags = PQ_FREE; 1276 #ifdef DEBUG 1277 pg->uobject = (void *)0xdeadbeef; 1278 pg->offset = 0xdeadbeef; 1279 pg->uanon = (void *)0xdeadbeef; 1280 #endif 1281 uvmexp.free++; 1282 1283 if (uvmexp.zeropages < UVM_PAGEZERO_TARGET) 1284 uvm.page_idle_zero = vm_page_zero_enable; 1285 1286 uvm_unlock_fpageq(s); 1287 } 1288 1289 /* 1290 * uvm_page_unbusy: unbusy an array of pages. 1291 * 1292 * => pages must either all belong to the same object, or all belong to anons. 1293 * => if pages are object-owned, object must be locked. 1294 * => if pages are anon-owned, anons must be locked. 1295 */ 1296 1297 void 1298 uvm_page_unbusy(pgs, npgs) 1299 struct vm_page **pgs; 1300 int npgs; 1301 { 1302 struct vm_page *pg; 1303 int i; 1304 UVMHIST_FUNC("uvm_page_unbusy"); UVMHIST_CALLED(ubchist); 1305 1306 for (i = 0; i < npgs; i++) { 1307 pg = pgs[i]; 1308 if (pg == NULL) { 1309 continue; 1310 } 1311 if (pg->flags & PG_WANTED) { 1312 wakeup(pg); 1313 } 1314 if (pg->flags & PG_RELEASED) { 1315 UVMHIST_LOG(ubchist, "releasing pg %p", pg,0,0,0); 1316 pg->flags &= ~PG_RELEASED; 1317 uvm_pagefree(pg); 1318 } else { 1319 UVMHIST_LOG(ubchist, "unbusying pg %p", pg,0,0,0); 1320 pg->flags &= ~(PG_WANTED|PG_BUSY); 1321 UVM_PAGE_OWN(pg, NULL); 1322 } 1323 } 1324 } 1325 1326 #if defined(UVM_PAGE_TRKOWN) 1327 /* 1328 * uvm_page_own: set or release page ownership 1329 * 1330 * => this is a debugging function that keeps track of who sets PG_BUSY 1331 * and where they do it. it can be used to track down problems 1332 * such a process setting "PG_BUSY" and never releasing it. 1333 * => page's object [if any] must be locked 1334 * => if "tag" is NULL then we are releasing page ownership 1335 */ 1336 void 1337 uvm_page_own(pg, tag) 1338 struct vm_page *pg; 1339 char *tag; 1340 { 1341 KASSERT((pg->flags & (PG_PAGEOUT|PG_RELEASED)) == 0); 1342 1343 /* gain ownership? */ 1344 if (tag) { 1345 if (pg->owner_tag) { 1346 printf("uvm_page_own: page %p already owned " 1347 "by proc %d [%s]\n", pg, 1348 pg->owner, pg->owner_tag); 1349 panic("uvm_page_own"); 1350 } 1351 pg->owner = (curproc) ? curproc->p_pid : (pid_t) -1; 1352 pg->owner_tag = tag; 1353 return; 1354 } 1355 1356 /* drop ownership */ 1357 if (pg->owner_tag == NULL) { 1358 printf("uvm_page_own: dropping ownership of an non-owned " 1359 "page (%p)\n", pg); 1360 panic("uvm_page_own"); 1361 } 1362 pg->owner_tag = NULL; 1363 KASSERT((pg->uanon == NULL && pg->uobject == NULL) || 1364 pg->uobject == uvm.kernel_object || 1365 pg->wire_count || (pg->pqflags & (PQ_ACTIVE|PQ_INACTIVE))); 1366 return; 1367 } 1368 #endif 1369 1370 /* 1371 * uvm_pageidlezero: zero free pages while the system is idle. 1372 * 1373 * => try to complete one color bucket at a time, to reduce our impact 1374 * on the CPU cache. 1375 * => we loop until we either reach the target or whichqs indicates that 1376 * there is a process ready to run. 1377 */ 1378 void 1379 uvm_pageidlezero() 1380 { 1381 struct vm_page *pg; 1382 struct pgfreelist *pgfl; 1383 int free_list, s, firstbucket; 1384 static int nextbucket; 1385 1386 s = uvm_lock_fpageq(); 1387 firstbucket = nextbucket; 1388 do { 1389 if (sched_whichqs != 0) { 1390 uvm_unlock_fpageq(s); 1391 return; 1392 } 1393 if (uvmexp.zeropages >= UVM_PAGEZERO_TARGET) { 1394 uvm.page_idle_zero = FALSE; 1395 uvm_unlock_fpageq(s); 1396 return; 1397 } 1398 for (free_list = 0; free_list < VM_NFREELIST; free_list++) { 1399 pgfl = &uvm.page_free[free_list]; 1400 while ((pg = TAILQ_FIRST(&pgfl->pgfl_buckets[ 1401 nextbucket].pgfl_queues[PGFL_UNKNOWN])) != NULL) { 1402 if (sched_whichqs != 0) { 1403 uvm_unlock_fpageq(s); 1404 return; 1405 } 1406 1407 TAILQ_REMOVE(&pgfl->pgfl_buckets[ 1408 nextbucket].pgfl_queues[PGFL_UNKNOWN], 1409 pg, pageq); 1410 uvmexp.free--; 1411 uvm_unlock_fpageq(s); 1412 #ifdef PMAP_PAGEIDLEZERO 1413 if (!PMAP_PAGEIDLEZERO(VM_PAGE_TO_PHYS(pg))) { 1414 1415 /* 1416 * The machine-dependent code detected 1417 * some reason for us to abort zeroing 1418 * pages, probably because there is a 1419 * process now ready to run. 1420 */ 1421 1422 s = uvm_lock_fpageq(); 1423 TAILQ_INSERT_HEAD(&pgfl->pgfl_buckets[ 1424 nextbucket].pgfl_queues[ 1425 PGFL_UNKNOWN], pg, pageq); 1426 uvmexp.free++; 1427 uvmexp.zeroaborts++; 1428 uvm_unlock_fpageq(s); 1429 return; 1430 } 1431 #else 1432 pmap_zero_page(VM_PAGE_TO_PHYS(pg)); 1433 #endif /* PMAP_PAGEIDLEZERO */ 1434 pg->flags |= PG_ZERO; 1435 1436 s = uvm_lock_fpageq(); 1437 TAILQ_INSERT_HEAD(&pgfl->pgfl_buckets[ 1438 nextbucket].pgfl_queues[PGFL_ZEROS], 1439 pg, pageq); 1440 uvmexp.free++; 1441 uvmexp.zeropages++; 1442 } 1443 } 1444 nextbucket = (nextbucket + 1) & uvmexp.colormask; 1445 } while (nextbucket != firstbucket); 1446 uvm_unlock_fpageq(s); 1447 } 1448