uvm_page.c revision 1.91 1 /* $NetBSD: uvm_page.c,v 1.91 2003/11/03 03:58:28 yamt 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.91 2003/11/03 03:58:28 yamt 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[
237 color].pgfl_queues[i]);
238 }
239 }
240 }
241
242 /*
243 * uvm_page_init: init the page system. called from uvm_init().
244 *
245 * => we return the range of kernel virtual memory in kvm_startp/kvm_endp
246 */
247
248 void
249 uvm_page_init(kvm_startp, kvm_endp)
250 vaddr_t *kvm_startp, *kvm_endp;
251 {
252 vsize_t freepages, pagecount, bucketcount, n;
253 struct pgflbucket *bucketarray;
254 struct vm_page *pagearray;
255 int lcv;
256 u_int i;
257 paddr_t paddr;
258
259 /*
260 * init the page queues and page queue locks, except the free
261 * list; we allocate that later (with the initial vm_page
262 * structures).
263 */
264
265 TAILQ_INIT(&uvm.page_active);
266 TAILQ_INIT(&uvm.page_inactive);
267 simple_lock_init(&uvm.pageqlock);
268 simple_lock_init(&uvm.fpageqlock);
269
270 /*
271 * init the <obj,offset> => <page> hash table. for now
272 * we just have one bucket (the bootstrap bucket). later on we
273 * will allocate new buckets as we dynamically resize the hash table.
274 */
275
276 uvm.page_nhash = 1; /* 1 bucket */
277 uvm.page_hashmask = 0; /* mask for hash function */
278 uvm.page_hash = &uvm_bootbucket; /* install bootstrap bucket */
279 TAILQ_INIT(uvm.page_hash); /* init hash table */
280 simple_lock_init(&uvm.hashlock); /* init hash table lock */
281
282 /*
283 * allocate vm_page structures.
284 */
285
286 /*
287 * sanity check:
288 * before calling this function the MD code is expected to register
289 * some free RAM with the uvm_page_physload() function. our job
290 * now is to allocate vm_page structures for this memory.
291 */
292
293 if (vm_nphysseg == 0)
294 panic("uvm_page_bootstrap: no memory pre-allocated");
295
296 /*
297 * first calculate the number of free pages...
298 *
299 * note that we use start/end rather than avail_start/avail_end.
300 * this allows us to allocate extra vm_page structures in case we
301 * want to return some memory to the pool after booting.
302 */
303
304 freepages = 0;
305 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
306 freepages += (vm_physmem[lcv].end - vm_physmem[lcv].start);
307
308 /*
309 * Let MD code initialize the number of colors, or default
310 * to 1 color if MD code doesn't care.
311 */
312 if (uvmexp.ncolors == 0)
313 uvmexp.ncolors = 1;
314 uvmexp.colormask = uvmexp.ncolors - 1;
315
316 /*
317 * we now know we have (PAGE_SIZE * freepages) bytes of memory we can
318 * use. for each page of memory we use we need a vm_page structure.
319 * thus, the total number of pages we can use is the total size of
320 * the memory divided by the PAGE_SIZE plus the size of the vm_page
321 * structure. we add one to freepages as a fudge factor to avoid
322 * truncation errors (since we can only allocate in terms of whole
323 * pages).
324 */
325
326 bucketcount = uvmexp.ncolors * VM_NFREELIST;
327 pagecount = ((freepages + 1) << PAGE_SHIFT) /
328 (PAGE_SIZE + sizeof(struct vm_page));
329
330 bucketarray = (void *)uvm_pageboot_alloc((bucketcount *
331 sizeof(struct pgflbucket)) + (pagecount *
332 sizeof(struct vm_page)));
333 pagearray = (struct vm_page *)(bucketarray + bucketcount);
334
335 for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
336 uvm.page_free[lcv].pgfl_buckets =
337 (bucketarray + (lcv * uvmexp.ncolors));
338 uvm_page_init_buckets(&uvm.page_free[lcv]);
339 }
340 memset(pagearray, 0, pagecount * sizeof(struct vm_page));
341
342 /*
343 * init the vm_page structures and put them in the correct place.
344 */
345
346 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) {
347 n = vm_physmem[lcv].end - vm_physmem[lcv].start;
348
349 /* set up page array pointers */
350 vm_physmem[lcv].pgs = pagearray;
351 pagearray += n;
352 pagecount -= n;
353 vm_physmem[lcv].lastpg = vm_physmem[lcv].pgs + (n - 1);
354
355 /* init and free vm_pages (we've already zeroed them) */
356 paddr = ptoa(vm_physmem[lcv].start);
357 for (i = 0 ; i < n ; i++, paddr += PAGE_SIZE) {
358 vm_physmem[lcv].pgs[i].phys_addr = paddr;
359 #ifdef __HAVE_VM_PAGE_MD
360 VM_MDPAGE_INIT(&vm_physmem[lcv].pgs[i]);
361 #endif
362 if (atop(paddr) >= vm_physmem[lcv].avail_start &&
363 atop(paddr) <= vm_physmem[lcv].avail_end) {
364 uvmexp.npages++;
365 /* add page to free pool */
366 uvm_pagefree(&vm_physmem[lcv].pgs[i]);
367 }
368 }
369 }
370
371 /*
372 * pass up the values of virtual_space_start and
373 * virtual_space_end (obtained by uvm_pageboot_alloc) to the upper
374 * layers of the VM.
375 */
376
377 *kvm_startp = round_page(virtual_space_start);
378 *kvm_endp = trunc_page(virtual_space_end);
379 #ifdef DEBUG
380 /*
381 * steal kva for uvm_pagezerocheck().
382 */
383 uvm_zerocheckkva = *kvm_startp;
384 *kvm_startp += PAGE_SIZE;
385 #endif /* DEBUG */
386
387 /*
388 * init locks for kernel threads
389 */
390
391 simple_lock_init(&uvm.pagedaemon_lock);
392 simple_lock_init(&uvm.aiodoned_lock);
393
394 /*
395 * init various thresholds.
396 */
397
398 uvmexp.reserve_pagedaemon = 1;
399 uvmexp.reserve_kernel = 5;
400 uvmexp.anonminpct = 10;
401 uvmexp.fileminpct = 10;
402 uvmexp.execminpct = 5;
403 uvmexp.anonmaxpct = 80;
404 uvmexp.filemaxpct = 50;
405 uvmexp.execmaxpct = 30;
406 uvmexp.anonmin = uvmexp.anonminpct * 256 / 100;
407 uvmexp.filemin = uvmexp.fileminpct * 256 / 100;
408 uvmexp.execmin = uvmexp.execminpct * 256 / 100;
409 uvmexp.anonmax = uvmexp.anonmaxpct * 256 / 100;
410 uvmexp.filemax = uvmexp.filemaxpct * 256 / 100;
411 uvmexp.execmax = uvmexp.execmaxpct * 256 / 100;
412
413 /*
414 * determine if we should zero pages in the idle loop.
415 */
416
417 uvm.page_idle_zero = vm_page_zero_enable;
418
419 /*
420 * done!
421 */
422
423 uvm.page_init_done = TRUE;
424 }
425
426 /*
427 * uvm_setpagesize: set the page size
428 *
429 * => sets page_shift and page_mask from uvmexp.pagesize.
430 */
431
432 void
433 uvm_setpagesize()
434 {
435
436 /*
437 * If uvmexp.pagesize is 0 at this point, we expect PAGE_SIZE
438 * to be a constant (indicated by being a non-zero value).
439 */
440 if (uvmexp.pagesize == 0) {
441 if (PAGE_SIZE == 0)
442 panic("uvm_setpagesize: uvmexp.pagesize not set");
443 uvmexp.pagesize = PAGE_SIZE;
444 }
445 uvmexp.pagemask = uvmexp.pagesize - 1;
446 if ((uvmexp.pagemask & uvmexp.pagesize) != 0)
447 panic("uvm_setpagesize: page size not a power of two");
448 for (uvmexp.pageshift = 0; ; uvmexp.pageshift++)
449 if ((1 << uvmexp.pageshift) == uvmexp.pagesize)
450 break;
451 }
452
453 /*
454 * uvm_pageboot_alloc: steal memory from physmem for bootstrapping
455 */
456
457 vaddr_t
458 uvm_pageboot_alloc(size)
459 vsize_t size;
460 {
461 static boolean_t initialized = FALSE;
462 vaddr_t addr;
463 #if !defined(PMAP_STEAL_MEMORY)
464 vaddr_t vaddr;
465 paddr_t paddr;
466 #endif
467
468 /*
469 * on first call to this function, initialize ourselves.
470 */
471 if (initialized == FALSE) {
472 pmap_virtual_space(&virtual_space_start, &virtual_space_end);
473
474 /* round it the way we like it */
475 virtual_space_start = round_page(virtual_space_start);
476 virtual_space_end = trunc_page(virtual_space_end);
477
478 initialized = TRUE;
479 }
480
481 /* round to page size */
482 size = round_page(size);
483
484 #if defined(PMAP_STEAL_MEMORY)
485
486 /*
487 * defer bootstrap allocation to MD code (it may want to allocate
488 * from a direct-mapped segment). pmap_steal_memory should adjust
489 * virtual_space_start/virtual_space_end if necessary.
490 */
491
492 addr = pmap_steal_memory(size, &virtual_space_start,
493 &virtual_space_end);
494
495 return(addr);
496
497 #else /* !PMAP_STEAL_MEMORY */
498
499 /*
500 * allocate virtual memory for this request
501 */
502 if (virtual_space_start == virtual_space_end ||
503 (virtual_space_end - virtual_space_start) < size)
504 panic("uvm_pageboot_alloc: out of virtual space");
505
506 addr = virtual_space_start;
507
508 #ifdef PMAP_GROWKERNEL
509 /*
510 * If the kernel pmap can't map the requested space,
511 * then allocate more resources for it.
512 */
513 if (uvm_maxkaddr < (addr + size)) {
514 uvm_maxkaddr = pmap_growkernel(addr + size);
515 if (uvm_maxkaddr < (addr + size))
516 panic("uvm_pageboot_alloc: pmap_growkernel() failed");
517 }
518 #endif
519
520 virtual_space_start += size;
521
522 /*
523 * allocate and mapin physical pages to back new virtual pages
524 */
525
526 for (vaddr = round_page(addr) ; vaddr < addr + size ;
527 vaddr += PAGE_SIZE) {
528
529 if (!uvm_page_physget(&paddr))
530 panic("uvm_pageboot_alloc: out of memory");
531
532 /*
533 * Note this memory is no longer managed, so using
534 * pmap_kenter is safe.
535 */
536 pmap_kenter_pa(vaddr, paddr, VM_PROT_READ|VM_PROT_WRITE);
537 }
538 pmap_update(pmap_kernel());
539 return(addr);
540 #endif /* PMAP_STEAL_MEMORY */
541 }
542
543 #if !defined(PMAP_STEAL_MEMORY)
544 /*
545 * uvm_page_physget: "steal" one page from the vm_physmem structure.
546 *
547 * => attempt to allocate it off the end of a segment in which the "avail"
548 * values match the start/end values. if we can't do that, then we
549 * will advance both values (making them equal, and removing some
550 * vm_page structures from the non-avail area).
551 * => return false if out of memory.
552 */
553
554 /* subroutine: try to allocate from memory chunks on the specified freelist */
555 static boolean_t uvm_page_physget_freelist __P((paddr_t *, int));
556
557 static boolean_t
558 uvm_page_physget_freelist(paddrp, freelist)
559 paddr_t *paddrp;
560 int freelist;
561 {
562 int lcv, x;
563
564 /* pass 1: try allocating from a matching end */
565 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
566 for (lcv = vm_nphysseg - 1 ; lcv >= 0 ; lcv--)
567 #else
568 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
569 #endif
570 {
571
572 if (uvm.page_init_done == TRUE)
573 panic("uvm_page_physget: called _after_ bootstrap");
574
575 if (vm_physmem[lcv].free_list != freelist)
576 continue;
577
578 /* try from front */
579 if (vm_physmem[lcv].avail_start == vm_physmem[lcv].start &&
580 vm_physmem[lcv].avail_start < vm_physmem[lcv].avail_end) {
581 *paddrp = ptoa(vm_physmem[lcv].avail_start);
582 vm_physmem[lcv].avail_start++;
583 vm_physmem[lcv].start++;
584 /* nothing left? nuke it */
585 if (vm_physmem[lcv].avail_start ==
586 vm_physmem[lcv].end) {
587 if (vm_nphysseg == 1)
588 panic("uvm_page_physget: out of memory!");
589 vm_nphysseg--;
590 for (x = lcv ; x < vm_nphysseg ; x++)
591 /* structure copy */
592 vm_physmem[x] = vm_physmem[x+1];
593 }
594 return (TRUE);
595 }
596
597 /* try from rear */
598 if (vm_physmem[lcv].avail_end == vm_physmem[lcv].end &&
599 vm_physmem[lcv].avail_start < vm_physmem[lcv].avail_end) {
600 *paddrp = ptoa(vm_physmem[lcv].avail_end - 1);
601 vm_physmem[lcv].avail_end--;
602 vm_physmem[lcv].end--;
603 /* nothing left? nuke it */
604 if (vm_physmem[lcv].avail_end ==
605 vm_physmem[lcv].start) {
606 if (vm_nphysseg == 1)
607 panic("uvm_page_physget: out of memory!");
608 vm_nphysseg--;
609 for (x = lcv ; x < vm_nphysseg ; x++)
610 /* structure copy */
611 vm_physmem[x] = vm_physmem[x+1];
612 }
613 return (TRUE);
614 }
615 }
616
617 /* pass2: forget about matching ends, just allocate something */
618 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
619 for (lcv = vm_nphysseg - 1 ; lcv >= 0 ; lcv--)
620 #else
621 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
622 #endif
623 {
624
625 /* any room in this bank? */
626 if (vm_physmem[lcv].avail_start >= vm_physmem[lcv].avail_end)
627 continue; /* nope */
628
629 *paddrp = ptoa(vm_physmem[lcv].avail_start);
630 vm_physmem[lcv].avail_start++;
631 /* truncate! */
632 vm_physmem[lcv].start = vm_physmem[lcv].avail_start;
633
634 /* nothing left? nuke it */
635 if (vm_physmem[lcv].avail_start == vm_physmem[lcv].end) {
636 if (vm_nphysseg == 1)
637 panic("uvm_page_physget: out of memory!");
638 vm_nphysseg--;
639 for (x = lcv ; x < vm_nphysseg ; x++)
640 /* structure copy */
641 vm_physmem[x] = vm_physmem[x+1];
642 }
643 return (TRUE);
644 }
645
646 return (FALSE); /* whoops! */
647 }
648
649 boolean_t
650 uvm_page_physget(paddrp)
651 paddr_t *paddrp;
652 {
653 int i;
654
655 /* try in the order of freelist preference */
656 for (i = 0; i < VM_NFREELIST; i++)
657 if (uvm_page_physget_freelist(paddrp, i) == TRUE)
658 return (TRUE);
659 return (FALSE);
660 }
661 #endif /* PMAP_STEAL_MEMORY */
662
663 /*
664 * uvm_page_physload: load physical memory into VM system
665 *
666 * => all args are PFs
667 * => all pages in start/end get vm_page structures
668 * => areas marked by avail_start/avail_end get added to the free page pool
669 * => we are limited to VM_PHYSSEG_MAX physical memory segments
670 */
671
672 void
673 uvm_page_physload(start, end, avail_start, avail_end, free_list)
674 paddr_t start, end, avail_start, avail_end;
675 int free_list;
676 {
677 int preload, lcv;
678 psize_t npages;
679 struct vm_page *pgs;
680 struct vm_physseg *ps;
681
682 if (uvmexp.pagesize == 0)
683 panic("uvm_page_physload: page size not set!");
684 if (free_list >= VM_NFREELIST || free_list < VM_FREELIST_DEFAULT)
685 panic("uvm_page_physload: bad free list %d", free_list);
686 if (start >= end)
687 panic("uvm_page_physload: start >= end");
688
689 /*
690 * do we have room?
691 */
692
693 if (vm_nphysseg == VM_PHYSSEG_MAX) {
694 printf("uvm_page_physload: unable to load physical memory "
695 "segment\n");
696 printf("\t%d segments allocated, ignoring 0x%llx -> 0x%llx\n",
697 VM_PHYSSEG_MAX, (long long)start, (long long)end);
698 printf("\tincrease VM_PHYSSEG_MAX\n");
699 return;
700 }
701
702 /*
703 * check to see if this is a "preload" (i.e. uvm_mem_init hasn't been
704 * called yet, so malloc is not available).
705 */
706
707 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) {
708 if (vm_physmem[lcv].pgs)
709 break;
710 }
711 preload = (lcv == vm_nphysseg);
712
713 /*
714 * if VM is already running, attempt to malloc() vm_page structures
715 */
716
717 if (!preload) {
718 #if defined(VM_PHYSSEG_NOADD)
719 panic("uvm_page_physload: tried to add RAM after vm_mem_init");
720 #else
721 /* XXXCDC: need some sort of lockout for this case */
722 paddr_t paddr;
723 npages = end - start; /* # of pages */
724 pgs = malloc(sizeof(struct vm_page) * npages,
725 M_VMPAGE, M_NOWAIT);
726 if (pgs == NULL) {
727 printf("uvm_page_physload: can not malloc vm_page "
728 "structs for segment\n");
729 printf("\tignoring 0x%lx -> 0x%lx\n", start, end);
730 return;
731 }
732 /* zero data, init phys_addr and free_list, and free pages */
733 memset(pgs, 0, sizeof(struct vm_page) * npages);
734 for (lcv = 0, paddr = ptoa(start) ;
735 lcv < npages ; lcv++, paddr += PAGE_SIZE) {
736 pgs[lcv].phys_addr = paddr;
737 pgs[lcv].free_list = free_list;
738 if (atop(paddr) >= avail_start &&
739 atop(paddr) <= avail_end)
740 uvm_pagefree(&pgs[lcv]);
741 }
742 /* XXXCDC: incomplete: need to update uvmexp.free, what else? */
743 /* XXXCDC: need hook to tell pmap to rebuild pv_list, etc... */
744 #endif
745 } else {
746 pgs = NULL;
747 npages = 0;
748 }
749
750 /*
751 * now insert us in the proper place in vm_physmem[]
752 */
753
754 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_RANDOM)
755 /* random: put it at the end (easy!) */
756 ps = &vm_physmem[vm_nphysseg];
757 #elif (VM_PHYSSEG_STRAT == VM_PSTRAT_BSEARCH)
758 {
759 int x;
760 /* sort by address for binary search */
761 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
762 if (start < vm_physmem[lcv].start)
763 break;
764 ps = &vm_physmem[lcv];
765 /* move back other entries, if necessary ... */
766 for (x = vm_nphysseg ; x > lcv ; x--)
767 /* structure copy */
768 vm_physmem[x] = vm_physmem[x - 1];
769 }
770 #elif (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
771 {
772 int x;
773 /* sort by largest segment first */
774 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
775 if ((end - start) >
776 (vm_physmem[lcv].end - vm_physmem[lcv].start))
777 break;
778 ps = &vm_physmem[lcv];
779 /* move back other entries, if necessary ... */
780 for (x = vm_nphysseg ; x > lcv ; x--)
781 /* structure copy */
782 vm_physmem[x] = vm_physmem[x - 1];
783 }
784 #else
785 panic("uvm_page_physload: unknown physseg strategy selected!");
786 #endif
787
788 ps->start = start;
789 ps->end = end;
790 ps->avail_start = avail_start;
791 ps->avail_end = avail_end;
792 if (preload) {
793 ps->pgs = NULL;
794 } else {
795 ps->pgs = pgs;
796 ps->lastpg = pgs + npages - 1;
797 }
798 ps->free_list = free_list;
799 vm_nphysseg++;
800
801 if (!preload)
802 uvm_page_rehash();
803 }
804
805 /*
806 * uvm_page_rehash: reallocate hash table based on number of free pages.
807 */
808
809 void
810 uvm_page_rehash()
811 {
812 int freepages, lcv, bucketcount, oldcount;
813 struct pglist *newbuckets, *oldbuckets;
814 struct vm_page *pg;
815 size_t newsize, oldsize;
816
817 /*
818 * compute number of pages that can go in the free pool
819 */
820
821 freepages = 0;
822 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
823 freepages +=
824 (vm_physmem[lcv].avail_end - vm_physmem[lcv].avail_start);
825
826 /*
827 * compute number of buckets needed for this number of pages
828 */
829
830 bucketcount = 1;
831 while (bucketcount < freepages)
832 bucketcount = bucketcount * 2;
833
834 /*
835 * compute the size of the current table and new table.
836 */
837
838 oldbuckets = uvm.page_hash;
839 oldcount = uvm.page_nhash;
840 oldsize = round_page(sizeof(struct pglist) * oldcount);
841 newsize = round_page(sizeof(struct pglist) * bucketcount);
842
843 /*
844 * allocate the new buckets
845 */
846
847 newbuckets = (struct pglist *) uvm_km_alloc(kernel_map, newsize);
848 if (newbuckets == NULL) {
849 printf("uvm_page_physrehash: WARNING: could not grow page "
850 "hash table\n");
851 return;
852 }
853 for (lcv = 0 ; lcv < bucketcount ; lcv++)
854 TAILQ_INIT(&newbuckets[lcv]);
855
856 /*
857 * now replace the old buckets with the new ones and rehash everything
858 */
859
860 simple_lock(&uvm.hashlock);
861 uvm.page_hash = newbuckets;
862 uvm.page_nhash = bucketcount;
863 uvm.page_hashmask = bucketcount - 1; /* power of 2 */
864
865 /* ... and rehash */
866 for (lcv = 0 ; lcv < oldcount ; lcv++) {
867 while ((pg = oldbuckets[lcv].tqh_first) != NULL) {
868 TAILQ_REMOVE(&oldbuckets[lcv], pg, hashq);
869 TAILQ_INSERT_TAIL(
870 &uvm.page_hash[uvm_pagehash(pg->uobject, pg->offset)],
871 pg, hashq);
872 }
873 }
874 simple_unlock(&uvm.hashlock);
875
876 /*
877 * free old bucket array if is not the boot-time table
878 */
879
880 if (oldbuckets != &uvm_bootbucket)
881 uvm_km_free(kernel_map, (vaddr_t) oldbuckets, oldsize);
882 }
883
884 /*
885 * uvm_page_recolor: Recolor the pages if the new bucket count is
886 * larger than the old one.
887 */
888
889 void
890 uvm_page_recolor(int newncolors)
891 {
892 struct pgflbucket *bucketarray, *oldbucketarray;
893 struct pgfreelist pgfl;
894 struct vm_page *pg;
895 vsize_t bucketcount;
896 int s, lcv, color, i, ocolors;
897
898 if (newncolors <= uvmexp.ncolors)
899 return;
900
901 if (uvm.page_init_done == FALSE) {
902 uvmexp.ncolors = newncolors;
903 return;
904 }
905
906 bucketcount = newncolors * VM_NFREELIST;
907 bucketarray = malloc(bucketcount * sizeof(struct pgflbucket),
908 M_VMPAGE, M_NOWAIT);
909 if (bucketarray == NULL) {
910 printf("WARNING: unable to allocate %ld page color buckets\n",
911 (long) bucketcount);
912 return;
913 }
914
915 s = uvm_lock_fpageq();
916
917 /* Make sure we should still do this. */
918 if (newncolors <= uvmexp.ncolors) {
919 uvm_unlock_fpageq(s);
920 free(bucketarray, M_VMPAGE);
921 return;
922 }
923
924 oldbucketarray = uvm.page_free[0].pgfl_buckets;
925 ocolors = uvmexp.ncolors;
926
927 uvmexp.ncolors = newncolors;
928 uvmexp.colormask = uvmexp.ncolors - 1;
929
930 for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
931 pgfl.pgfl_buckets = (bucketarray + (lcv * newncolors));
932 uvm_page_init_buckets(&pgfl);
933 for (color = 0; color < ocolors; color++) {
934 for (i = 0; i < PGFL_NQUEUES; i++) {
935 while ((pg = TAILQ_FIRST(&uvm.page_free[
936 lcv].pgfl_buckets[color].pgfl_queues[i]))
937 != NULL) {
938 TAILQ_REMOVE(&uvm.page_free[
939 lcv].pgfl_buckets[
940 color].pgfl_queues[i], pg, pageq);
941 TAILQ_INSERT_TAIL(&pgfl.pgfl_buckets[
942 VM_PGCOLOR_BUCKET(pg)].pgfl_queues[
943 i], pg, pageq);
944 }
945 }
946 }
947 uvm.page_free[lcv].pgfl_buckets = pgfl.pgfl_buckets;
948 }
949
950 if (have_recolored_pages) {
951 uvm_unlock_fpageq(s);
952 free(oldbucketarray, M_VMPAGE);
953 return;
954 }
955
956 have_recolored_pages = TRUE;
957 uvm_unlock_fpageq(s);
958 }
959
960 /*
961 * uvm_pagealloc_pgfl: helper routine for uvm_pagealloc_strat
962 */
963
964 static __inline struct vm_page *
965 uvm_pagealloc_pgfl(struct pgfreelist *pgfl, int try1, int try2,
966 int *trycolorp)
967 {
968 struct pglist *freeq;
969 struct vm_page *pg;
970 int color, trycolor = *trycolorp;
971
972 color = trycolor;
973 do {
974 if ((pg = TAILQ_FIRST((freeq =
975 &pgfl->pgfl_buckets[color].pgfl_queues[try1]))) != NULL)
976 goto gotit;
977 if ((pg = TAILQ_FIRST((freeq =
978 &pgfl->pgfl_buckets[color].pgfl_queues[try2]))) != NULL)
979 goto gotit;
980 color = (color + 1) & uvmexp.colormask;
981 } while (color != trycolor);
982
983 return (NULL);
984
985 gotit:
986 TAILQ_REMOVE(freeq, pg, pageq);
987 uvmexp.free--;
988
989 /* update zero'd page count */
990 if (pg->flags & PG_ZERO)
991 uvmexp.zeropages--;
992
993 if (color == trycolor)
994 uvmexp.colorhit++;
995 else {
996 uvmexp.colormiss++;
997 *trycolorp = color;
998 }
999
1000 return (pg);
1001 }
1002
1003 /*
1004 * uvm_pagealloc_strat: allocate vm_page from a particular free list.
1005 *
1006 * => return null if no pages free
1007 * => wake up pagedaemon if number of free pages drops below low water mark
1008 * => if obj != NULL, obj must be locked (to put in hash)
1009 * => if anon != NULL, anon must be locked (to put in anon)
1010 * => only one of obj or anon can be non-null
1011 * => caller must activate/deactivate page if it is not wired.
1012 * => free_list is ignored if strat == UVM_PGA_STRAT_NORMAL.
1013 * => policy decision: it is more important to pull a page off of the
1014 * appropriate priority free list than it is to get a zero'd or
1015 * unknown contents page. This is because we live with the
1016 * consequences of a bad free list decision for the entire
1017 * lifetime of the page, e.g. if the page comes from memory that
1018 * is slower to access.
1019 */
1020
1021 struct vm_page *
1022 uvm_pagealloc_strat(obj, off, anon, flags, strat, free_list)
1023 struct uvm_object *obj;
1024 voff_t off;
1025 int flags;
1026 struct vm_anon *anon;
1027 int strat, free_list;
1028 {
1029 int lcv, try1, try2, s, zeroit = 0, color;
1030 struct vm_page *pg;
1031 boolean_t use_reserve;
1032
1033 KASSERT(obj == NULL || anon == NULL);
1034 KASSERT(off == trunc_page(off));
1035 LOCK_ASSERT(obj == NULL || simple_lock_held(&obj->vmobjlock));
1036 LOCK_ASSERT(anon == NULL || simple_lock_held(&anon->an_lock));
1037
1038 s = uvm_lock_fpageq();
1039
1040 /*
1041 * This implements a global round-robin page coloring
1042 * algorithm.
1043 *
1044 * XXXJRT: Should we make the `nextcolor' per-cpu?
1045 * XXXJRT: What about virtually-indexed caches?
1046 */
1047
1048 color = uvm.page_free_nextcolor;
1049
1050 /*
1051 * check to see if we need to generate some free pages waking
1052 * the pagedaemon.
1053 */
1054
1055 UVM_KICK_PDAEMON();
1056
1057 /*
1058 * fail if any of these conditions is true:
1059 * [1] there really are no free pages, or
1060 * [2] only kernel "reserved" pages remain and
1061 * the page isn't being allocated to a kernel object.
1062 * [3] only pagedaemon "reserved" pages remain and
1063 * the requestor isn't the pagedaemon.
1064 */
1065
1066 use_reserve = (flags & UVM_PGA_USERESERVE) ||
1067 (obj && UVM_OBJ_IS_KERN_OBJECT(obj));
1068 if ((uvmexp.free <= uvmexp.reserve_kernel && !use_reserve) ||
1069 (uvmexp.free <= uvmexp.reserve_pagedaemon &&
1070 !(use_reserve && curproc == uvm.pagedaemon_proc)))
1071 goto fail;
1072
1073 #if PGFL_NQUEUES != 2
1074 #error uvm_pagealloc_strat needs to be updated
1075 #endif
1076
1077 /*
1078 * If we want a zero'd page, try the ZEROS queue first, otherwise
1079 * we try the UNKNOWN queue first.
1080 */
1081 if (flags & UVM_PGA_ZERO) {
1082 try1 = PGFL_ZEROS;
1083 try2 = PGFL_UNKNOWN;
1084 } else {
1085 try1 = PGFL_UNKNOWN;
1086 try2 = PGFL_ZEROS;
1087 }
1088
1089 again:
1090 switch (strat) {
1091 case UVM_PGA_STRAT_NORMAL:
1092 /* Check all freelists in descending priority order. */
1093 for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
1094 pg = uvm_pagealloc_pgfl(&uvm.page_free[lcv],
1095 try1, try2, &color);
1096 if (pg != NULL)
1097 goto gotit;
1098 }
1099
1100 /* No pages free! */
1101 goto fail;
1102
1103 case UVM_PGA_STRAT_ONLY:
1104 case UVM_PGA_STRAT_FALLBACK:
1105 /* Attempt to allocate from the specified free list. */
1106 KASSERT(free_list >= 0 && free_list < VM_NFREELIST);
1107 pg = uvm_pagealloc_pgfl(&uvm.page_free[free_list],
1108 try1, try2, &color);
1109 if (pg != NULL)
1110 goto gotit;
1111
1112 /* Fall back, if possible. */
1113 if (strat == UVM_PGA_STRAT_FALLBACK) {
1114 strat = UVM_PGA_STRAT_NORMAL;
1115 goto again;
1116 }
1117
1118 /* No pages free! */
1119 goto fail;
1120
1121 default:
1122 panic("uvm_pagealloc_strat: bad strat %d", strat);
1123 /* NOTREACHED */
1124 }
1125
1126 gotit:
1127 /*
1128 * We now know which color we actually allocated from; set
1129 * the next color accordingly.
1130 */
1131
1132 uvm.page_free_nextcolor = (color + 1) & uvmexp.colormask;
1133
1134 /*
1135 * update allocation statistics and remember if we have to
1136 * zero the page
1137 */
1138
1139 if (flags & UVM_PGA_ZERO) {
1140 if (pg->flags & PG_ZERO) {
1141 uvmexp.pga_zerohit++;
1142 zeroit = 0;
1143 } else {
1144 uvmexp.pga_zeromiss++;
1145 zeroit = 1;
1146 }
1147 }
1148 uvm_unlock_fpageq(s);
1149
1150 pg->offset = off;
1151 pg->uobject = obj;
1152 pg->uanon = anon;
1153 pg->flags = PG_BUSY|PG_CLEAN|PG_FAKE;
1154 if (anon) {
1155 anon->u.an_page = pg;
1156 pg->pqflags = PQ_ANON;
1157 uvmexp.anonpages++;
1158 } else {
1159 if (obj) {
1160 uvm_pageinsert(pg);
1161 }
1162 pg->pqflags = 0;
1163 }
1164 #if defined(UVM_PAGE_TRKOWN)
1165 pg->owner_tag = NULL;
1166 #endif
1167 UVM_PAGE_OWN(pg, "new alloc");
1168
1169 if (flags & UVM_PGA_ZERO) {
1170 /*
1171 * A zero'd page is not clean. If we got a page not already
1172 * zero'd, then we have to zero it ourselves.
1173 */
1174 pg->flags &= ~PG_CLEAN;
1175 if (zeroit)
1176 pmap_zero_page(VM_PAGE_TO_PHYS(pg));
1177 }
1178
1179 return(pg);
1180
1181 fail:
1182 uvm_unlock_fpageq(s);
1183 return (NULL);
1184 }
1185
1186 /*
1187 * uvm_pagerealloc: reallocate a page from one object to another
1188 *
1189 * => both objects must be locked
1190 */
1191
1192 void
1193 uvm_pagerealloc(pg, newobj, newoff)
1194 struct vm_page *pg;
1195 struct uvm_object *newobj;
1196 voff_t newoff;
1197 {
1198 /*
1199 * remove it from the old object
1200 */
1201
1202 if (pg->uobject) {
1203 uvm_pageremove(pg);
1204 }
1205
1206 /*
1207 * put it in the new object
1208 */
1209
1210 if (newobj) {
1211 pg->uobject = newobj;
1212 pg->offset = newoff;
1213 uvm_pageinsert(pg);
1214 }
1215 }
1216
1217 #ifdef DEBUG
1218 /*
1219 * check if page is zero-filled
1220 *
1221 * - called with free page queue lock held.
1222 */
1223 void
1224 uvm_pagezerocheck(struct vm_page *pg)
1225 {
1226 int *p, *ep;
1227
1228 KASSERT(uvm_zerocheckkva != 0);
1229 LOCK_ASSERT(simple_lock_held(&uvm.fpageqlock));
1230
1231 /*
1232 * XXX assuming pmap_kenter_pa and pmap_kremove never call
1233 * uvm page allocator.
1234 *
1235 * it might be better to have "cpu-local temporary map" pmap interface.
1236 */
1237 pmap_kenter_pa(uvm_zerocheckkva, VM_PAGE_TO_PHYS(pg), VM_PROT_READ);
1238 p = (int *)uvm_zerocheckkva;
1239 ep = (int *)((char *)p + PAGE_SIZE);
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