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