uvm_page.c revision 1.77 1 /* $NetBSD: uvm_page.c,v 1.77 2002/06/19 17:01:18 wrstuden 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.77 2002/06/19 17:01:18 wrstuden 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 if (uvm.page_init_done == FALSE) {
870 uvmexp.ncolors = newncolors;
871 return;
872 }
873
874 bucketcount = newncolors * VM_NFREELIST;
875 bucketarray = malloc(bucketcount * sizeof(struct pgflbucket),
876 M_VMPAGE, M_NOWAIT);
877 if (bucketarray == NULL) {
878 printf("WARNING: unable to allocate %ld page color buckets\n",
879 (long) bucketcount);
880 return;
881 }
882
883 s = uvm_lock_fpageq();
884
885 /* Make sure we should still do this. */
886 if (newncolors <= uvmexp.ncolors) {
887 uvm_unlock_fpageq(s);
888 free(bucketarray, M_VMPAGE);
889 return;
890 }
891
892 oldbucketarray = uvm.page_free[0].pgfl_buckets;
893 ocolors = uvmexp.ncolors;
894
895 uvmexp.ncolors = newncolors;
896 uvmexp.colormask = uvmexp.ncolors - 1;
897
898 for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
899 pgfl.pgfl_buckets = (bucketarray + (lcv * newncolors));
900 uvm_page_init_buckets(&pgfl);
901 for (color = 0; color < ocolors; color++) {
902 for (i = 0; i < PGFL_NQUEUES; i++) {
903 while ((pg = TAILQ_FIRST(&uvm.page_free[
904 lcv].pgfl_buckets[color].pgfl_queues[i]))
905 != NULL) {
906 TAILQ_REMOVE(&uvm.page_free[
907 lcv].pgfl_buckets[
908 color].pgfl_queues[i], pg, pageq);
909 TAILQ_INSERT_TAIL(&pgfl.pgfl_buckets[
910 VM_PGCOLOR_BUCKET(pg)].pgfl_queues[
911 i], pg, pageq);
912 }
913 }
914 }
915 uvm.page_free[lcv].pgfl_buckets = pgfl.pgfl_buckets;
916 }
917
918 if (have_recolored_pages) {
919 uvm_unlock_fpageq(s);
920 free(oldbucketarray, M_VMPAGE);
921 return;
922 }
923
924 have_recolored_pages = TRUE;
925 uvm_unlock_fpageq(s);
926 }
927
928 /*
929 * uvm_pagealloc_pgfl: helper routine for uvm_pagealloc_strat
930 */
931
932 static __inline struct vm_page *
933 uvm_pagealloc_pgfl(struct pgfreelist *pgfl, int try1, int try2,
934 int *trycolorp)
935 {
936 struct pglist *freeq;
937 struct vm_page *pg;
938 int color, trycolor = *trycolorp;
939
940 color = trycolor;
941 do {
942 if ((pg = TAILQ_FIRST((freeq =
943 &pgfl->pgfl_buckets[color].pgfl_queues[try1]))) != NULL)
944 goto gotit;
945 if ((pg = TAILQ_FIRST((freeq =
946 &pgfl->pgfl_buckets[color].pgfl_queues[try2]))) != NULL)
947 goto gotit;
948 color = (color + 1) & uvmexp.colormask;
949 } while (color != trycolor);
950
951 return (NULL);
952
953 gotit:
954 TAILQ_REMOVE(freeq, pg, pageq);
955 uvmexp.free--;
956
957 /* update zero'd page count */
958 if (pg->flags & PG_ZERO)
959 uvmexp.zeropages--;
960
961 if (color == trycolor)
962 uvmexp.colorhit++;
963 else {
964 uvmexp.colormiss++;
965 *trycolorp = color;
966 }
967
968 return (pg);
969 }
970
971 /*
972 * uvm_pagealloc_strat: allocate vm_page from a particular free list.
973 *
974 * => return null if no pages free
975 * => wake up pagedaemon if number of free pages drops below low water mark
976 * => if obj != NULL, obj must be locked (to put in hash)
977 * => if anon != NULL, anon must be locked (to put in anon)
978 * => only one of obj or anon can be non-null
979 * => caller must activate/deactivate page if it is not wired.
980 * => free_list is ignored if strat == UVM_PGA_STRAT_NORMAL.
981 * => policy decision: it is more important to pull a page off of the
982 * appropriate priority free list than it is to get a zero'd or
983 * unknown contents page. This is because we live with the
984 * consequences of a bad free list decision for the entire
985 * lifetime of the page, e.g. if the page comes from memory that
986 * is slower to access.
987 */
988
989 struct vm_page *
990 uvm_pagealloc_strat(obj, off, anon, flags, strat, free_list)
991 struct uvm_object *obj;
992 voff_t off;
993 int flags;
994 struct vm_anon *anon;
995 int strat, free_list;
996 {
997 int lcv, try1, try2, s, zeroit = 0, color;
998 struct vm_page *pg;
999 boolean_t use_reserve;
1000
1001 KASSERT(obj == NULL || anon == NULL);
1002 KASSERT(off == trunc_page(off));
1003 LOCK_ASSERT(obj == NULL || simple_lock_held(&obj->vmobjlock));
1004 LOCK_ASSERT(anon == NULL || simple_lock_held(&anon->an_lock));
1005
1006 s = uvm_lock_fpageq();
1007
1008 /*
1009 * This implements a global round-robin page coloring
1010 * algorithm.
1011 *
1012 * XXXJRT: Should we make the `nextcolor' per-cpu?
1013 * XXXJRT: What about virtually-indexed caches?
1014 */
1015
1016 color = uvm.page_free_nextcolor;
1017
1018 /*
1019 * check to see if we need to generate some free pages waking
1020 * the pagedaemon.
1021 */
1022
1023 UVM_KICK_PDAEMON();
1024
1025 /*
1026 * fail if any of these conditions is true:
1027 * [1] there really are no free pages, or
1028 * [2] only kernel "reserved" pages remain and
1029 * the page isn't being allocated to a kernel object.
1030 * [3] only pagedaemon "reserved" pages remain and
1031 * the requestor isn't the pagedaemon.
1032 */
1033
1034 use_reserve = (flags & UVM_PGA_USERESERVE) ||
1035 (obj && UVM_OBJ_IS_KERN_OBJECT(obj));
1036 if ((uvmexp.free <= uvmexp.reserve_kernel && !use_reserve) ||
1037 (uvmexp.free <= uvmexp.reserve_pagedaemon &&
1038 !(use_reserve && curproc == uvm.pagedaemon_proc)))
1039 goto fail;
1040
1041 #if PGFL_NQUEUES != 2
1042 #error uvm_pagealloc_strat needs to be updated
1043 #endif
1044
1045 /*
1046 * If we want a zero'd page, try the ZEROS queue first, otherwise
1047 * we try the UNKNOWN queue first.
1048 */
1049 if (flags & UVM_PGA_ZERO) {
1050 try1 = PGFL_ZEROS;
1051 try2 = PGFL_UNKNOWN;
1052 } else {
1053 try1 = PGFL_UNKNOWN;
1054 try2 = PGFL_ZEROS;
1055 }
1056
1057 again:
1058 switch (strat) {
1059 case UVM_PGA_STRAT_NORMAL:
1060 /* Check all freelists in descending priority order. */
1061 for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
1062 pg = uvm_pagealloc_pgfl(&uvm.page_free[lcv],
1063 try1, try2, &color);
1064 if (pg != NULL)
1065 goto gotit;
1066 }
1067
1068 /* No pages free! */
1069 goto fail;
1070
1071 case UVM_PGA_STRAT_ONLY:
1072 case UVM_PGA_STRAT_FALLBACK:
1073 /* Attempt to allocate from the specified free list. */
1074 KASSERT(free_list >= 0 && free_list < VM_NFREELIST);
1075 pg = uvm_pagealloc_pgfl(&uvm.page_free[free_list],
1076 try1, try2, &color);
1077 if (pg != NULL)
1078 goto gotit;
1079
1080 /* Fall back, if possible. */
1081 if (strat == UVM_PGA_STRAT_FALLBACK) {
1082 strat = UVM_PGA_STRAT_NORMAL;
1083 goto again;
1084 }
1085
1086 /* No pages free! */
1087 goto fail;
1088
1089 default:
1090 panic("uvm_pagealloc_strat: bad strat %d", strat);
1091 /* NOTREACHED */
1092 }
1093
1094 gotit:
1095 /*
1096 * We now know which color we actually allocated from; set
1097 * the next color accordingly.
1098 */
1099
1100 uvm.page_free_nextcolor = (color + 1) & uvmexp.colormask;
1101
1102 /*
1103 * update allocation statistics and remember if we have to
1104 * zero the page
1105 */
1106
1107 if (flags & UVM_PGA_ZERO) {
1108 if (pg->flags & PG_ZERO) {
1109 uvmexp.pga_zerohit++;
1110 zeroit = 0;
1111 } else {
1112 uvmexp.pga_zeromiss++;
1113 zeroit = 1;
1114 }
1115 }
1116 uvm_unlock_fpageq(s);
1117
1118 pg->offset = off;
1119 pg->uobject = obj;
1120 pg->uanon = anon;
1121 pg->flags = PG_BUSY|PG_CLEAN|PG_FAKE;
1122 if (anon) {
1123 anon->u.an_page = pg;
1124 pg->pqflags = PQ_ANON;
1125 uvmexp.anonpages++;
1126 } else {
1127 if (obj) {
1128 uvm_pageinsert(pg);
1129 }
1130 pg->pqflags = 0;
1131 }
1132 #if defined(UVM_PAGE_TRKOWN)
1133 pg->owner_tag = NULL;
1134 #endif
1135 UVM_PAGE_OWN(pg, "new alloc");
1136
1137 if (flags & UVM_PGA_ZERO) {
1138 /*
1139 * A zero'd page is not clean. If we got a page not already
1140 * zero'd, then we have to zero it ourselves.
1141 */
1142 pg->flags &= ~PG_CLEAN;
1143 if (zeroit)
1144 pmap_zero_page(VM_PAGE_TO_PHYS(pg));
1145 }
1146
1147 return(pg);
1148
1149 fail:
1150 uvm_unlock_fpageq(s);
1151 return (NULL);
1152 }
1153
1154 /*
1155 * uvm_pagerealloc: reallocate a page from one object to another
1156 *
1157 * => both objects must be locked
1158 */
1159
1160 void
1161 uvm_pagerealloc(pg, newobj, newoff)
1162 struct vm_page *pg;
1163 struct uvm_object *newobj;
1164 voff_t newoff;
1165 {
1166 /*
1167 * remove it from the old object
1168 */
1169
1170 if (pg->uobject) {
1171 uvm_pageremove(pg);
1172 }
1173
1174 /*
1175 * put it in the new object
1176 */
1177
1178 if (newobj) {
1179 pg->uobject = newobj;
1180 pg->offset = newoff;
1181 uvm_pageinsert(pg);
1182 }
1183 }
1184
1185 /*
1186 * uvm_pagefree: free page
1187 *
1188 * => erase page's identity (i.e. remove from hash/object)
1189 * => put page on free list
1190 * => caller must lock owning object (either anon or uvm_object)
1191 * => caller must lock page queues
1192 * => assumes all valid mappings of pg are gone
1193 */
1194
1195 void
1196 uvm_pagefree(pg)
1197 struct vm_page *pg;
1198 {
1199 int s;
1200
1201 KASSERT((pg->flags & PG_PAGEOUT) == 0);
1202 LOCK_ASSERT(simple_lock_held(&uvm.pageqlock) ||
1203 (pg->pqflags & (PQ_ACTIVE|PQ_INACTIVE)) == 0);
1204 LOCK_ASSERT(pg->uobject == NULL ||
1205 simple_lock_held(&pg->uobject->vmobjlock));
1206 LOCK_ASSERT(pg->uobject != NULL || pg->uanon == NULL ||
1207 simple_lock_held(&pg->uanon->an_lock));
1208
1209 #ifdef DEBUG
1210 if (pg->uobject == (void *)0xdeadbeef &&
1211 pg->uanon == (void *)0xdeadbeef) {
1212 panic("uvm_pagefree: freeing free page %p\n", pg);
1213 }
1214 #endif
1215
1216 /*
1217 * if the page is loaned, resolve the loan instead of freeing.
1218 */
1219
1220 if (pg->loan_count) {
1221 KASSERT(pg->wire_count == 0);
1222
1223 /*
1224 * if the page is owned by an anon then we just want to
1225 * drop anon ownership. the kernel will free the page when
1226 * it is done with it. if the page is owned by an object,
1227 * remove it from the object and mark it dirty for the benefit
1228 * of possible anon owners.
1229 *
1230 * regardless of previous ownership, wakeup any waiters,
1231 * unbusy the page, and we're done.
1232 */
1233
1234 if (pg->uobject != NULL) {
1235 uvm_pageremove(pg);
1236 pg->flags &= ~PG_CLEAN;
1237 } else if (pg->uanon != NULL) {
1238 if ((pg->pqflags & PQ_ANON) == 0) {
1239 pg->loan_count--;
1240 } else {
1241 pg->pqflags &= ~PQ_ANON;
1242 }
1243 pg->uanon = NULL;
1244 }
1245 if (pg->flags & PG_WANTED) {
1246 wakeup(pg);
1247 }
1248 pg->flags &= ~(PG_WANTED|PG_BUSY|PG_RELEASED);
1249 #ifdef UVM_PAGE_TRKOWN
1250 pg->owner_tag = NULL;
1251 #endif
1252 if (pg->loan_count) {
1253 uvm_pagedequeue(pg);
1254 return;
1255 }
1256 }
1257
1258 /*
1259 * remove page from its object or anon.
1260 */
1261
1262 if (pg->uobject != NULL) {
1263 uvm_pageremove(pg);
1264 } else if (pg->uanon != NULL) {
1265 pg->uanon->u.an_page = NULL;
1266 uvmexp.anonpages--;
1267 }
1268
1269 /*
1270 * now remove the page from the queues.
1271 */
1272
1273 uvm_pagedequeue(pg);
1274
1275 /*
1276 * if the page was wired, unwire it now.
1277 */
1278
1279 if (pg->wire_count) {
1280 pg->wire_count = 0;
1281 uvmexp.wired--;
1282 }
1283
1284 /*
1285 * and put on free queue
1286 */
1287
1288 pg->flags &= ~PG_ZERO;
1289
1290 s = uvm_lock_fpageq();
1291 TAILQ_INSERT_TAIL(&uvm.page_free[
1292 uvm_page_lookup_freelist(pg)].pgfl_buckets[
1293 VM_PGCOLOR_BUCKET(pg)].pgfl_queues[PGFL_UNKNOWN], pg, pageq);
1294 pg->pqflags = PQ_FREE;
1295 #ifdef DEBUG
1296 pg->uobject = (void *)0xdeadbeef;
1297 pg->offset = 0xdeadbeef;
1298 pg->uanon = (void *)0xdeadbeef;
1299 #endif
1300 uvmexp.free++;
1301
1302 if (uvmexp.zeropages < UVM_PAGEZERO_TARGET)
1303 uvm.page_idle_zero = vm_page_zero_enable;
1304
1305 uvm_unlock_fpageq(s);
1306 }
1307
1308 /*
1309 * uvm_page_unbusy: unbusy an array of pages.
1310 *
1311 * => pages must either all belong to the same object, or all belong to anons.
1312 * => if pages are object-owned, object must be locked.
1313 * => if pages are anon-owned, anons must be locked.
1314 * => caller must lock page queues if pages may be released.
1315 */
1316
1317 void
1318 uvm_page_unbusy(pgs, npgs)
1319 struct vm_page **pgs;
1320 int npgs;
1321 {
1322 struct vm_page *pg;
1323 int i;
1324 UVMHIST_FUNC("uvm_page_unbusy"); UVMHIST_CALLED(ubchist);
1325
1326 for (i = 0; i < npgs; i++) {
1327 pg = pgs[i];
1328 if (pg == NULL) {
1329 continue;
1330 }
1331 if (pg->flags & PG_WANTED) {
1332 wakeup(pg);
1333 }
1334 if (pg->flags & PG_RELEASED) {
1335 UVMHIST_LOG(ubchist, "releasing pg %p", pg,0,0,0);
1336 pg->flags &= ~PG_RELEASED;
1337 uvm_pagefree(pg);
1338 } else {
1339 UVMHIST_LOG(ubchist, "unbusying pg %p", pg,0,0,0);
1340 pg->flags &= ~(PG_WANTED|PG_BUSY);
1341 UVM_PAGE_OWN(pg, NULL);
1342 }
1343 }
1344 }
1345
1346 #if defined(UVM_PAGE_TRKOWN)
1347 /*
1348 * uvm_page_own: set or release page ownership
1349 *
1350 * => this is a debugging function that keeps track of who sets PG_BUSY
1351 * and where they do it. it can be used to track down problems
1352 * such a process setting "PG_BUSY" and never releasing it.
1353 * => page's object [if any] must be locked
1354 * => if "tag" is NULL then we are releasing page ownership
1355 */
1356 void
1357 uvm_page_own(pg, tag)
1358 struct vm_page *pg;
1359 char *tag;
1360 {
1361 KASSERT((pg->flags & (PG_PAGEOUT|PG_RELEASED)) == 0);
1362
1363 /* gain ownership? */
1364 if (tag) {
1365 if (pg->owner_tag) {
1366 printf("uvm_page_own: page %p already owned "
1367 "by proc %d [%s]\n", pg,
1368 pg->owner, pg->owner_tag);
1369 panic("uvm_page_own");
1370 }
1371 pg->owner = (curproc) ? curproc->p_pid : (pid_t) -1;
1372 pg->owner_tag = tag;
1373 return;
1374 }
1375
1376 /* drop ownership */
1377 if (pg->owner_tag == NULL) {
1378 printf("uvm_page_own: dropping ownership of an non-owned "
1379 "page (%p)\n", pg);
1380 panic("uvm_page_own");
1381 }
1382 KASSERT((pg->pqflags & (PQ_ACTIVE|PQ_INACTIVE)) ||
1383 (pg->uanon == NULL && pg->uobject == NULL) ||
1384 pg->uobject == uvm.kernel_object ||
1385 pg->wire_count > 0 ||
1386 (pg->loan_count == 1 && pg->uanon == NULL) ||
1387 pg->loan_count > 1);
1388 pg->owner_tag = NULL;
1389 }
1390 #endif
1391
1392 /*
1393 * uvm_pageidlezero: zero free pages while the system is idle.
1394 *
1395 * => try to complete one color bucket at a time, to reduce our impact
1396 * on the CPU cache.
1397 * => we loop until we either reach the target or whichqs indicates that
1398 * there is a process ready to run.
1399 */
1400 void
1401 uvm_pageidlezero()
1402 {
1403 struct vm_page *pg;
1404 struct pgfreelist *pgfl;
1405 int free_list, s, firstbucket;
1406 static int nextbucket;
1407
1408 s = uvm_lock_fpageq();
1409 firstbucket = nextbucket;
1410 do {
1411 if (sched_whichqs != 0) {
1412 uvm_unlock_fpageq(s);
1413 return;
1414 }
1415 if (uvmexp.zeropages >= UVM_PAGEZERO_TARGET) {
1416 uvm.page_idle_zero = FALSE;
1417 uvm_unlock_fpageq(s);
1418 return;
1419 }
1420 for (free_list = 0; free_list < VM_NFREELIST; free_list++) {
1421 pgfl = &uvm.page_free[free_list];
1422 while ((pg = TAILQ_FIRST(&pgfl->pgfl_buckets[
1423 nextbucket].pgfl_queues[PGFL_UNKNOWN])) != NULL) {
1424 if (sched_whichqs != 0) {
1425 uvm_unlock_fpageq(s);
1426 return;
1427 }
1428
1429 TAILQ_REMOVE(&pgfl->pgfl_buckets[
1430 nextbucket].pgfl_queues[PGFL_UNKNOWN],
1431 pg, pageq);
1432 uvmexp.free--;
1433 uvm_unlock_fpageq(s);
1434 #ifdef PMAP_PAGEIDLEZERO
1435 if (!PMAP_PAGEIDLEZERO(VM_PAGE_TO_PHYS(pg))) {
1436
1437 /*
1438 * The machine-dependent code detected
1439 * some reason for us to abort zeroing
1440 * pages, probably because there is a
1441 * process now ready to run.
1442 */
1443
1444 s = uvm_lock_fpageq();
1445 TAILQ_INSERT_HEAD(&pgfl->pgfl_buckets[
1446 nextbucket].pgfl_queues[
1447 PGFL_UNKNOWN], pg, pageq);
1448 uvmexp.free++;
1449 uvmexp.zeroaborts++;
1450 uvm_unlock_fpageq(s);
1451 return;
1452 }
1453 #else
1454 pmap_zero_page(VM_PAGE_TO_PHYS(pg));
1455 #endif /* PMAP_PAGEIDLEZERO */
1456 pg->flags |= PG_ZERO;
1457
1458 s = uvm_lock_fpageq();
1459 TAILQ_INSERT_HEAD(&pgfl->pgfl_buckets[
1460 nextbucket].pgfl_queues[PGFL_ZEROS],
1461 pg, pageq);
1462 uvmexp.free++;
1463 uvmexp.zeropages++;
1464 }
1465 }
1466 nextbucket = (nextbucket + 1) & uvmexp.colormask;
1467 } while (nextbucket != firstbucket);
1468 uvm_unlock_fpageq(s);
1469 }
1470