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