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