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