uvm_page.c revision 1.161 1 /* $NetBSD: uvm_page.c,v 1.161 2010/11/11 15:59:27 uebayasi Exp $ */
2
3 /*
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * Copyright (c) 1997 Charles D. Cranor and Washington University.
31 * Copyright (c) 1991, 1993, The Regents of the University of California.
32 *
33 * All rights reserved.
34 *
35 * This code is derived from software contributed to Berkeley by
36 * The Mach Operating System project at Carnegie-Mellon University.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by Charles D. Cranor,
49 * Washington University, the University of California, Berkeley and
50 * its contributors.
51 * 4. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 *
67 * @(#)vm_page.c 8.3 (Berkeley) 3/21/94
68 * from: Id: uvm_page.c,v 1.1.2.18 1998/02/06 05:24:42 chs Exp
69 *
70 *
71 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
72 * All rights reserved.
73 *
74 * Permission to use, copy, modify and distribute this software and
75 * its documentation is hereby granted, provided that both the copyright
76 * notice and this permission notice appear in all copies of the
77 * software, derivative works or modified versions, and any portions
78 * thereof, and that both notices appear in supporting documentation.
79 *
80 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
81 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
82 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
83 *
84 * Carnegie Mellon requests users of this software to return to
85 *
86 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
87 * School of Computer Science
88 * Carnegie Mellon University
89 * Pittsburgh PA 15213-3890
90 *
91 * any improvements or extensions that they make and grant Carnegie the
92 * rights to redistribute these changes.
93 */
94
95 /*
96 * uvm_page.c: page ops.
97 */
98
99 #include <sys/cdefs.h>
100 __KERNEL_RCSID(0, "$NetBSD: uvm_page.c,v 1.161 2010/11/11 15:59:27 uebayasi Exp $");
101
102 #include "opt_ddb.h"
103 #include "opt_uvmhist.h"
104 #include "opt_readahead.h"
105
106 #include <sys/param.h>
107 #include <sys/systm.h>
108 #include <sys/malloc.h>
109 #include <sys/sched.h>
110 #include <sys/kernel.h>
111 #include <sys/vnode.h>
112 #include <sys/proc.h>
113 #include <sys/atomic.h>
114 #include <sys/cpu.h>
115
116 #include <uvm/uvm.h>
117 #include <uvm/uvm_ddb.h>
118 #include <uvm/uvm_pdpolicy.h>
119
120 /*
121 * global vars... XXXCDC: move to uvm. structure.
122 */
123
124 /*
125 * physical memory config is stored in vm_physmem.
126 */
127
128 struct vm_physseg vm_physmem[VM_PHYSSEG_MAX]; /* XXXCDC: uvm.physmem */
129 int vm_nphysseg = 0; /* XXXCDC: uvm.nphysseg */
130 #define vm_nphysmem vm_nphysseg
131
132 /*
133 * Some supported CPUs in a given architecture don't support all
134 * of the things necessary to do idle page zero'ing efficiently.
135 * We therefore provide a way to enable it from machdep code here.
136 */
137 bool vm_page_zero_enable = false;
138
139 /*
140 * number of pages per-CPU to reserve for the kernel.
141 */
142 int vm_page_reserve_kernel = 5;
143
144 /*
145 * physical memory size;
146 */
147 int physmem;
148
149 /*
150 * local variables
151 */
152
153 /*
154 * these variables record the values returned by vm_page_bootstrap,
155 * for debugging purposes. The implementation of uvm_pageboot_alloc
156 * and pmap_startup here also uses them internally.
157 */
158
159 static vaddr_t virtual_space_start;
160 static vaddr_t virtual_space_end;
161
162 /*
163 * we allocate an initial number of page colors in uvm_page_init(),
164 * and remember them. We may re-color pages as cache sizes are
165 * discovered during the autoconfiguration phase. But we can never
166 * free the initial set of buckets, since they are allocated using
167 * uvm_pageboot_alloc().
168 */
169
170 static bool have_recolored_pages /* = false */;
171
172 MALLOC_DEFINE(M_VMPAGE, "VM page", "VM page");
173
174 #ifdef DEBUG
175 vaddr_t uvm_zerocheckkva;
176 #endif /* DEBUG */
177
178 /*
179 * local prototypes
180 */
181
182 static void uvm_pageinsert(struct uvm_object *, struct vm_page *);
183 static void uvm_pageremove(struct uvm_object *, struct vm_page *);
184
185 /*
186 * per-object tree of pages
187 */
188
189 static signed int
190 uvm_page_compare_nodes(void *ctx, const void *n1, const void *n2)
191 {
192 const struct vm_page *pg1 = n1;
193 const struct vm_page *pg2 = n2;
194 const voff_t a = pg1->offset;
195 const voff_t b = pg2->offset;
196
197 if (a < b)
198 return -1;
199 if (a > b)
200 return 1;
201 return 0;
202 }
203
204 static signed int
205 uvm_page_compare_key(void *ctx, const void *n, const void *key)
206 {
207 const struct vm_page *pg = n;
208 const voff_t a = pg->offset;
209 const voff_t b = *(const voff_t *)key;
210
211 if (a < b)
212 return -1;
213 if (a > b)
214 return 1;
215 return 0;
216 }
217
218 const rb_tree_ops_t uvm_page_tree_ops = {
219 .rbto_compare_nodes = uvm_page_compare_nodes,
220 .rbto_compare_key = uvm_page_compare_key,
221 .rbto_node_offset = offsetof(struct vm_page, rb_node),
222 .rbto_context = NULL
223 };
224
225 /*
226 * inline functions
227 */
228
229 /*
230 * uvm_pageinsert: insert a page in the object.
231 *
232 * => caller must lock object
233 * => caller must lock page queues
234 * => call should have already set pg's object and offset pointers
235 * and bumped the version counter
236 */
237
238 static inline void
239 uvm_pageinsert_list(struct uvm_object *uobj, struct vm_page *pg,
240 struct vm_page *where)
241 {
242
243 KASSERT(uobj == pg->uobject);
244 KASSERT(mutex_owned(&uobj->vmobjlock));
245 KASSERT((pg->flags & PG_TABLED) == 0);
246 KASSERT(where == NULL || (where->flags & PG_TABLED));
247 KASSERT(where == NULL || (where->uobject == uobj));
248
249 if (UVM_OBJ_IS_VNODE(uobj)) {
250 if (uobj->uo_npages == 0) {
251 struct vnode *vp = (struct vnode *)uobj;
252
253 vholdl(vp);
254 }
255 if (UVM_OBJ_IS_VTEXT(uobj)) {
256 atomic_inc_uint(&uvmexp.execpages);
257 } else {
258 atomic_inc_uint(&uvmexp.filepages);
259 }
260 } else if (UVM_OBJ_IS_AOBJ(uobj)) {
261 atomic_inc_uint(&uvmexp.anonpages);
262 }
263
264 if (where)
265 TAILQ_INSERT_AFTER(&uobj->memq, where, pg, listq.queue);
266 else
267 TAILQ_INSERT_TAIL(&uobj->memq, pg, listq.queue);
268 pg->flags |= PG_TABLED;
269 uobj->uo_npages++;
270 }
271
272
273 static inline void
274 uvm_pageinsert_tree(struct uvm_object *uobj, struct vm_page *pg)
275 {
276 struct vm_page *ret;
277
278 KASSERT(uobj == pg->uobject);
279 ret = rb_tree_insert_node(&uobj->rb_tree, pg);
280 KASSERT(ret == pg);
281 }
282
283 static inline void
284 uvm_pageinsert(struct uvm_object *uobj, struct vm_page *pg)
285 {
286
287 KDASSERT(uobj != NULL);
288 uvm_pageinsert_tree(uobj, pg);
289 uvm_pageinsert_list(uobj, pg, NULL);
290 }
291
292 /*
293 * uvm_page_remove: remove page from object.
294 *
295 * => caller must lock object
296 * => caller must lock page queues
297 */
298
299 static inline void
300 uvm_pageremove_list(struct uvm_object *uobj, struct vm_page *pg)
301 {
302
303 KASSERT(uobj == pg->uobject);
304 KASSERT(mutex_owned(&uobj->vmobjlock));
305 KASSERT(pg->flags & PG_TABLED);
306
307 if (UVM_OBJ_IS_VNODE(uobj)) {
308 if (uobj->uo_npages == 1) {
309 struct vnode *vp = (struct vnode *)uobj;
310
311 holdrelel(vp);
312 }
313 if (UVM_OBJ_IS_VTEXT(uobj)) {
314 atomic_dec_uint(&uvmexp.execpages);
315 } else {
316 atomic_dec_uint(&uvmexp.filepages);
317 }
318 } else if (UVM_OBJ_IS_AOBJ(uobj)) {
319 atomic_dec_uint(&uvmexp.anonpages);
320 }
321
322 /* object should be locked */
323 uobj->uo_npages--;
324 TAILQ_REMOVE(&uobj->memq, pg, listq.queue);
325 pg->flags &= ~PG_TABLED;
326 pg->uobject = NULL;
327 }
328
329 static inline void
330 uvm_pageremove_tree(struct uvm_object *uobj, struct vm_page *pg)
331 {
332
333 KASSERT(uobj == pg->uobject);
334 rb_tree_remove_node(&uobj->rb_tree, pg);
335 }
336
337 static inline void
338 uvm_pageremove(struct uvm_object *uobj, struct vm_page *pg)
339 {
340
341 KDASSERT(uobj != NULL);
342 uvm_pageremove_tree(uobj, pg);
343 uvm_pageremove_list(uobj, pg);
344 }
345
346 static void
347 uvm_page_init_buckets(struct pgfreelist *pgfl)
348 {
349 int color, i;
350
351 for (color = 0; color < uvmexp.ncolors; color++) {
352 for (i = 0; i < PGFL_NQUEUES; i++) {
353 LIST_INIT(&pgfl->pgfl_buckets[color].pgfl_queues[i]);
354 }
355 }
356 }
357
358 /*
359 * uvm_page_init: init the page system. called from uvm_init().
360 *
361 * => we return the range of kernel virtual memory in kvm_startp/kvm_endp
362 */
363
364 void
365 uvm_page_init(vaddr_t *kvm_startp, vaddr_t *kvm_endp)
366 {
367 static struct uvm_cpu boot_cpu;
368 psize_t freepages, pagecount, bucketcount, n;
369 struct pgflbucket *bucketarray, *cpuarray;
370 struct vm_physseg *seg;
371 struct vm_page *pagearray;
372 int lcv;
373 u_int i;
374 paddr_t paddr;
375
376 KASSERT(ncpu <= 1);
377 CTASSERT(sizeof(pagearray->offset) >= sizeof(struct uvm_cpu *));
378
379 /*
380 * init the page queues and page queue locks, except the free
381 * list; we allocate that later (with the initial vm_page
382 * structures).
383 */
384
385 uvm.cpus[0] = &boot_cpu;
386 curcpu()->ci_data.cpu_uvm = &boot_cpu;
387 uvm_reclaim_init();
388 uvmpdpol_init();
389 mutex_init(&uvm_pageqlock, MUTEX_DRIVER, IPL_NONE);
390 mutex_init(&uvm_fpageqlock, MUTEX_DRIVER, IPL_VM);
391
392 /*
393 * allocate vm_page structures.
394 */
395
396 /*
397 * sanity check:
398 * before calling this function the MD code is expected to register
399 * some free RAM with the uvm_page_physload() function. our job
400 * now is to allocate vm_page structures for this memory.
401 */
402
403 if (vm_nphysmem == 0)
404 panic("uvm_page_bootstrap: no memory pre-allocated");
405
406 /*
407 * first calculate the number of free pages...
408 *
409 * note that we use start/end rather than avail_start/avail_end.
410 * this allows us to allocate extra vm_page structures in case we
411 * want to return some memory to the pool after booting.
412 */
413
414 freepages = 0;
415 for (lcv = 0 ; lcv < vm_nphysmem ; lcv++) {
416 seg = VM_PHYSMEM_PTR(lcv);
417 freepages += (seg->end - seg->start);
418 }
419
420 /*
421 * Let MD code initialize the number of colors, or default
422 * to 1 color if MD code doesn't care.
423 */
424 if (uvmexp.ncolors == 0)
425 uvmexp.ncolors = 1;
426 uvmexp.colormask = uvmexp.ncolors - 1;
427
428 /*
429 * we now know we have (PAGE_SIZE * freepages) bytes of memory we can
430 * use. for each page of memory we use we need a vm_page structure.
431 * thus, the total number of pages we can use is the total size of
432 * the memory divided by the PAGE_SIZE plus the size of the vm_page
433 * structure. we add one to freepages as a fudge factor to avoid
434 * truncation errors (since we can only allocate in terms of whole
435 * pages).
436 */
437
438 bucketcount = uvmexp.ncolors * VM_NFREELIST;
439 pagecount = ((freepages + 1) << PAGE_SHIFT) /
440 (PAGE_SIZE + sizeof(struct vm_page));
441
442 bucketarray = (void *)uvm_pageboot_alloc((bucketcount *
443 sizeof(struct pgflbucket) * 2) + (pagecount *
444 sizeof(struct vm_page)));
445 cpuarray = bucketarray + bucketcount;
446 pagearray = (struct vm_page *)(bucketarray + bucketcount * 2);
447
448 for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
449 uvm.page_free[lcv].pgfl_buckets =
450 (bucketarray + (lcv * uvmexp.ncolors));
451 uvm_page_init_buckets(&uvm.page_free[lcv]);
452 uvm.cpus[0]->page_free[lcv].pgfl_buckets =
453 (cpuarray + (lcv * uvmexp.ncolors));
454 uvm_page_init_buckets(&uvm.cpus[0]->page_free[lcv]);
455 }
456 memset(pagearray, 0, pagecount * sizeof(struct vm_page));
457
458 /*
459 * init the vm_page structures and put them in the correct place.
460 */
461
462 for (lcv = 0 ; lcv < vm_nphysmem ; lcv++) {
463 seg = VM_PHYSMEM_PTR(lcv);
464 n = seg->end - seg->start;
465
466 /* set up page array pointers */
467 seg->pgs = pagearray;
468 pagearray += n;
469 pagecount -= n;
470 seg->lastpg = seg->pgs + n;
471
472 /* init and free vm_pages (we've already zeroed them) */
473 paddr = ctob(seg->start);
474 for (i = 0 ; i < n ; i++, paddr += PAGE_SIZE) {
475 seg->pgs[i].phys_addr = paddr;
476 #ifdef __HAVE_VM_PAGE_MD
477 VM_MDPAGE_INIT(&seg->pgs[i]);
478 #endif
479 if (atop(paddr) >= seg->avail_start &&
480 atop(paddr) <= seg->avail_end) {
481 uvmexp.npages++;
482 /* add page to free pool */
483 uvm_pagefree(&seg->pgs[i]);
484 }
485 }
486 }
487
488 /*
489 * pass up the values of virtual_space_start and
490 * virtual_space_end (obtained by uvm_pageboot_alloc) to the upper
491 * layers of the VM.
492 */
493
494 *kvm_startp = round_page(virtual_space_start);
495 *kvm_endp = trunc_page(virtual_space_end);
496 #ifdef DEBUG
497 /*
498 * steal kva for uvm_pagezerocheck().
499 */
500 uvm_zerocheckkva = *kvm_startp;
501 *kvm_startp += PAGE_SIZE;
502 #endif /* DEBUG */
503
504 /*
505 * init various thresholds.
506 */
507
508 uvmexp.reserve_pagedaemon = 1;
509 uvmexp.reserve_kernel = vm_page_reserve_kernel;
510
511 /*
512 * determine if we should zero pages in the idle loop.
513 */
514
515 uvm.cpus[0]->page_idle_zero = vm_page_zero_enable;
516
517 /*
518 * done!
519 */
520
521 uvm.page_init_done = true;
522 }
523
524 /*
525 * uvm_setpagesize: set the page size
526 *
527 * => sets page_shift and page_mask from uvmexp.pagesize.
528 */
529
530 void
531 uvm_setpagesize(void)
532 {
533
534 /*
535 * If uvmexp.pagesize is 0 at this point, we expect PAGE_SIZE
536 * to be a constant (indicated by being a non-zero value).
537 */
538 if (uvmexp.pagesize == 0) {
539 if (PAGE_SIZE == 0)
540 panic("uvm_setpagesize: uvmexp.pagesize not set");
541 uvmexp.pagesize = PAGE_SIZE;
542 }
543 uvmexp.pagemask = uvmexp.pagesize - 1;
544 if ((uvmexp.pagemask & uvmexp.pagesize) != 0)
545 panic("uvm_setpagesize: page size not a power of two");
546 for (uvmexp.pageshift = 0; ; uvmexp.pageshift++)
547 if ((1 << uvmexp.pageshift) == uvmexp.pagesize)
548 break;
549 }
550
551 /*
552 * uvm_pageboot_alloc: steal memory from physmem for bootstrapping
553 */
554
555 vaddr_t
556 uvm_pageboot_alloc(vsize_t size)
557 {
558 static bool initialized = false;
559 vaddr_t addr;
560 #if !defined(PMAP_STEAL_MEMORY)
561 vaddr_t vaddr;
562 paddr_t paddr;
563 #endif
564
565 /*
566 * on first call to this function, initialize ourselves.
567 */
568 if (initialized == false) {
569 pmap_virtual_space(&virtual_space_start, &virtual_space_end);
570
571 /* round it the way we like it */
572 virtual_space_start = round_page(virtual_space_start);
573 virtual_space_end = trunc_page(virtual_space_end);
574
575 initialized = true;
576 }
577
578 /* round to page size */
579 size = round_page(size);
580
581 #if defined(PMAP_STEAL_MEMORY)
582
583 /*
584 * defer bootstrap allocation to MD code (it may want to allocate
585 * from a direct-mapped segment). pmap_steal_memory should adjust
586 * virtual_space_start/virtual_space_end if necessary.
587 */
588
589 addr = pmap_steal_memory(size, &virtual_space_start,
590 &virtual_space_end);
591
592 return(addr);
593
594 #else /* !PMAP_STEAL_MEMORY */
595
596 /*
597 * allocate virtual memory for this request
598 */
599 if (virtual_space_start == virtual_space_end ||
600 (virtual_space_end - virtual_space_start) < size)
601 panic("uvm_pageboot_alloc: out of virtual space");
602
603 addr = virtual_space_start;
604
605 #ifdef PMAP_GROWKERNEL
606 /*
607 * If the kernel pmap can't map the requested space,
608 * then allocate more resources for it.
609 */
610 if (uvm_maxkaddr < (addr + size)) {
611 uvm_maxkaddr = pmap_growkernel(addr + size);
612 if (uvm_maxkaddr < (addr + size))
613 panic("uvm_pageboot_alloc: pmap_growkernel() failed");
614 }
615 #endif
616
617 virtual_space_start += size;
618
619 /*
620 * allocate and mapin physical pages to back new virtual pages
621 */
622
623 for (vaddr = round_page(addr) ; vaddr < addr + size ;
624 vaddr += PAGE_SIZE) {
625
626 if (!uvm_page_physget(&paddr))
627 panic("uvm_pageboot_alloc: out of memory");
628
629 /*
630 * Note this memory is no longer managed, so using
631 * pmap_kenter is safe.
632 */
633 pmap_kenter_pa(vaddr, paddr, VM_PROT_READ|VM_PROT_WRITE, 0);
634 }
635 pmap_update(pmap_kernel());
636 return(addr);
637 #endif /* PMAP_STEAL_MEMORY */
638 }
639
640 #if !defined(PMAP_STEAL_MEMORY)
641 /*
642 * uvm_page_physget: "steal" one page from the vm_physmem structure.
643 *
644 * => attempt to allocate it off the end of a segment in which the "avail"
645 * values match the start/end values. if we can't do that, then we
646 * will advance both values (making them equal, and removing some
647 * vm_page structures from the non-avail area).
648 * => return false if out of memory.
649 */
650
651 /* subroutine: try to allocate from memory chunks on the specified freelist */
652 static bool uvm_page_physget_freelist(paddr_t *, int);
653
654 static bool
655 uvm_page_physget_freelist(paddr_t *paddrp, int freelist)
656 {
657 struct vm_physseg *seg;
658 int lcv, x;
659
660 /* pass 1: try allocating from a matching end */
661 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
662 for (lcv = vm_nphysmem - 1 ; lcv >= 0 ; lcv--)
663 #else
664 for (lcv = 0 ; lcv < vm_nphysmem ; lcv++)
665 #endif
666 {
667 seg = VM_PHYSMEM_PTR(lcv);
668
669 if (uvm.page_init_done == true)
670 panic("uvm_page_physget: called _after_ bootstrap");
671
672 if (seg->free_list != freelist)
673 continue;
674
675 /* try from front */
676 if (seg->avail_start == seg->start &&
677 seg->avail_start < seg->avail_end) {
678 *paddrp = ctob(seg->avail_start);
679 seg->avail_start++;
680 seg->start++;
681 /* nothing left? nuke it */
682 if (seg->avail_start == seg->end) {
683 if (vm_nphysmem == 1)
684 panic("uvm_page_physget: out of memory!");
685 vm_nphysmem--;
686 for (x = lcv ; x < vm_nphysmem ; x++)
687 /* structure copy */
688 VM_PHYSMEM_PTR_SWAP(x, x + 1);
689 }
690 return (true);
691 }
692
693 /* try from rear */
694 if (seg->avail_end == seg->end &&
695 seg->avail_start < seg->avail_end) {
696 *paddrp = ctob(seg->avail_end - 1);
697 seg->avail_end--;
698 seg->end--;
699 /* nothing left? nuke it */
700 if (seg->avail_end == seg->start) {
701 if (vm_nphysmem == 1)
702 panic("uvm_page_physget: out of memory!");
703 vm_nphysmem--;
704 for (x = lcv ; x < vm_nphysmem ; x++)
705 /* structure copy */
706 VM_PHYSMEM_PTR_SWAP(x, x + 1);
707 }
708 return (true);
709 }
710 }
711
712 /* pass2: forget about matching ends, just allocate something */
713 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
714 for (lcv = vm_nphysmem - 1 ; lcv >= 0 ; lcv--)
715 #else
716 for (lcv = 0 ; lcv < vm_nphysmem ; lcv++)
717 #endif
718 {
719 seg = VM_PHYSMEM_PTR(lcv);
720
721 /* any room in this bank? */
722 if (seg->avail_start >= seg->avail_end)
723 continue; /* nope */
724
725 *paddrp = ctob(seg->avail_start);
726 seg->avail_start++;
727 /* truncate! */
728 seg->start = seg->avail_start;
729
730 /* nothing left? nuke it */
731 if (seg->avail_start == seg->end) {
732 if (vm_nphysmem == 1)
733 panic("uvm_page_physget: out of memory!");
734 vm_nphysmem--;
735 for (x = lcv ; x < vm_nphysmem ; x++)
736 /* structure copy */
737 VM_PHYSMEM_PTR_SWAP(x, x + 1);
738 }
739 return (true);
740 }
741
742 return (false); /* whoops! */
743 }
744
745 bool
746 uvm_page_physget(paddr_t *paddrp)
747 {
748 int i;
749
750 /* try in the order of freelist preference */
751 for (i = 0; i < VM_NFREELIST; i++)
752 if (uvm_page_physget_freelist(paddrp, i) == true)
753 return (true);
754 return (false);
755 }
756 #endif /* PMAP_STEAL_MEMORY */
757
758 /*
759 * uvm_page_physload: load physical memory into VM system
760 *
761 * => all args are PFs
762 * => all pages in start/end get vm_page structures
763 * => areas marked by avail_start/avail_end get added to the free page pool
764 * => we are limited to VM_PHYSSEG_MAX physical memory segments
765 */
766
767 void
768 uvm_page_physload(paddr_t start, paddr_t end, paddr_t avail_start,
769 paddr_t avail_end, int free_list)
770 {
771 int preload, lcv;
772 psize_t npages;
773 struct vm_page *pgs;
774 struct vm_physseg *ps;
775
776 if (uvmexp.pagesize == 0)
777 panic("uvm_page_physload: page size not set!");
778 if (free_list >= VM_NFREELIST || free_list < VM_FREELIST_DEFAULT)
779 panic("uvm_page_physload: bad free list %d", free_list);
780 if (start >= end)
781 panic("uvm_page_physload: start >= end");
782
783 /*
784 * do we have room?
785 */
786
787 if (vm_nphysmem == VM_PHYSSEG_MAX) {
788 printf("uvm_page_physload: unable to load physical memory "
789 "segment\n");
790 printf("\t%d segments allocated, ignoring 0x%llx -> 0x%llx\n",
791 VM_PHYSSEG_MAX, (long long)start, (long long)end);
792 printf("\tincrease VM_PHYSSEG_MAX\n");
793 return;
794 }
795
796 /*
797 * check to see if this is a "preload" (i.e. uvm_page_init hasn't been
798 * called yet, so malloc is not available).
799 */
800
801 for (lcv = 0 ; lcv < vm_nphysmem ; lcv++) {
802 if (VM_PHYSMEM_PTR(lcv)->pgs)
803 break;
804 }
805 preload = (lcv == vm_nphysmem);
806
807 /*
808 * if VM is already running, attempt to malloc() vm_page structures
809 */
810
811 if (!preload) {
812 panic("uvm_page_physload: tried to add RAM after vm_mem_init");
813 } else {
814 pgs = NULL;
815 npages = 0;
816 }
817
818 /*
819 * now insert us in the proper place in vm_physmem[]
820 */
821
822 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_RANDOM)
823 /* random: put it at the end (easy!) */
824 ps = VM_PHYSMEM_PTR(vm_nphysmem);
825 #elif (VM_PHYSSEG_STRAT == VM_PSTRAT_BSEARCH)
826 {
827 int x;
828 /* sort by address for binary search */
829 for (lcv = 0 ; lcv < vm_nphysmem ; lcv++)
830 if (start < VM_PHYSMEM_PTR(lcv)->start)
831 break;
832 ps = VM_PHYSMEM_PTR(lcv);
833 /* move back other entries, if necessary ... */
834 for (x = vm_nphysmem ; x > lcv ; x--)
835 /* structure copy */
836 VM_PHYSMEM_PTR_SWAP(x, x - 1);
837 }
838 #elif (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
839 {
840 int x;
841 /* sort by largest segment first */
842 for (lcv = 0 ; lcv < vm_nphysmem ; lcv++)
843 if ((end - start) >
844 (VM_PHYSMEM_PTR(lcv)->end - VM_PHYSMEM_PTR(lcv)->start))
845 break;
846 ps = VM_PHYSMEM_PTR(lcv);
847 /* move back other entries, if necessary ... */
848 for (x = vm_nphysmem ; x > lcv ; x--)
849 /* structure copy */
850 VM_PHYSMEM_PTR_SWAP(x, x - 1);
851 }
852 #else
853 panic("uvm_page_physload: unknown physseg strategy selected!");
854 #endif
855
856 ps->start = start;
857 ps->end = end;
858 ps->avail_start = avail_start;
859 ps->avail_end = avail_end;
860 if (preload) {
861 ps->pgs = NULL;
862 } else {
863 ps->pgs = pgs;
864 ps->lastpg = pgs + npages;
865 }
866 ps->free_list = free_list;
867 vm_nphysmem++;
868
869 if (!preload) {
870 uvmpdpol_reinit();
871 }
872 }
873
874 /*
875 * uvm_page_recolor: Recolor the pages if the new bucket count is
876 * larger than the old one.
877 */
878
879 void
880 uvm_page_recolor(int newncolors)
881 {
882 struct pgflbucket *bucketarray, *cpuarray, *oldbucketarray;
883 struct pgfreelist gpgfl, pgfl;
884 struct vm_page *pg;
885 vsize_t bucketcount;
886 int lcv, color, i, ocolors;
887 struct uvm_cpu *ucpu;
888
889 if (newncolors <= uvmexp.ncolors)
890 return;
891
892 if (uvm.page_init_done == false) {
893 uvmexp.ncolors = newncolors;
894 return;
895 }
896
897 bucketcount = newncolors * VM_NFREELIST;
898 bucketarray = malloc(bucketcount * sizeof(struct pgflbucket) * 2,
899 M_VMPAGE, M_NOWAIT);
900 cpuarray = bucketarray + bucketcount;
901 if (bucketarray == NULL) {
902 printf("WARNING: unable to allocate %ld page color buckets\n",
903 (long) bucketcount);
904 return;
905 }
906
907 mutex_spin_enter(&uvm_fpageqlock);
908
909 /* Make sure we should still do this. */
910 if (newncolors <= uvmexp.ncolors) {
911 mutex_spin_exit(&uvm_fpageqlock);
912 free(bucketarray, M_VMPAGE);
913 return;
914 }
915
916 oldbucketarray = uvm.page_free[0].pgfl_buckets;
917 ocolors = uvmexp.ncolors;
918
919 uvmexp.ncolors = newncolors;
920 uvmexp.colormask = uvmexp.ncolors - 1;
921
922 ucpu = curcpu()->ci_data.cpu_uvm;
923 for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
924 gpgfl.pgfl_buckets = (bucketarray + (lcv * newncolors));
925 pgfl.pgfl_buckets = (cpuarray + (lcv * uvmexp.ncolors));
926 uvm_page_init_buckets(&gpgfl);
927 uvm_page_init_buckets(&pgfl);
928 for (color = 0; color < ocolors; color++) {
929 for (i = 0; i < PGFL_NQUEUES; i++) {
930 while ((pg = LIST_FIRST(&uvm.page_free[
931 lcv].pgfl_buckets[color].pgfl_queues[i]))
932 != NULL) {
933 LIST_REMOVE(pg, pageq.list); /* global */
934 LIST_REMOVE(pg, listq.list); /* cpu */
935 LIST_INSERT_HEAD(&gpgfl.pgfl_buckets[
936 VM_PGCOLOR_BUCKET(pg)].pgfl_queues[
937 i], pg, pageq.list);
938 LIST_INSERT_HEAD(&pgfl.pgfl_buckets[
939 VM_PGCOLOR_BUCKET(pg)].pgfl_queues[
940 i], pg, listq.list);
941 }
942 }
943 }
944 uvm.page_free[lcv].pgfl_buckets = gpgfl.pgfl_buckets;
945 ucpu->page_free[lcv].pgfl_buckets = pgfl.pgfl_buckets;
946 }
947
948 if (have_recolored_pages) {
949 mutex_spin_exit(&uvm_fpageqlock);
950 free(oldbucketarray, M_VMPAGE);
951 return;
952 }
953
954 have_recolored_pages = true;
955 mutex_spin_exit(&uvm_fpageqlock);
956 }
957
958 /*
959 * uvm_cpu_attach: initialize per-CPU data structures.
960 */
961
962 void
963 uvm_cpu_attach(struct cpu_info *ci)
964 {
965 struct pgflbucket *bucketarray;
966 struct pgfreelist pgfl;
967 struct uvm_cpu *ucpu;
968 vsize_t bucketcount;
969 int lcv;
970
971 if (CPU_IS_PRIMARY(ci)) {
972 /* Already done in uvm_page_init(). */
973 return;
974 }
975
976 /* Add more reserve pages for this CPU. */
977 uvmexp.reserve_kernel += vm_page_reserve_kernel;
978
979 /* Configure this CPU's free lists. */
980 bucketcount = uvmexp.ncolors * VM_NFREELIST;
981 bucketarray = malloc(bucketcount * sizeof(struct pgflbucket),
982 M_VMPAGE, M_WAITOK);
983 ucpu = kmem_zalloc(sizeof(*ucpu), KM_SLEEP);
984 uvm.cpus[cpu_index(ci)] = ucpu;
985 ci->ci_data.cpu_uvm = ucpu;
986 for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
987 pgfl.pgfl_buckets = (bucketarray + (lcv * uvmexp.ncolors));
988 uvm_page_init_buckets(&pgfl);
989 ucpu->page_free[lcv].pgfl_buckets = pgfl.pgfl_buckets;
990 }
991 }
992
993 /*
994 * uvm_pagealloc_pgfl: helper routine for uvm_pagealloc_strat
995 */
996
997 static struct vm_page *
998 uvm_pagealloc_pgfl(struct uvm_cpu *ucpu, int flist, int try1, int try2,
999 int *trycolorp)
1000 {
1001 struct pgflist *freeq;
1002 struct vm_page *pg;
1003 int color, trycolor = *trycolorp;
1004 struct pgfreelist *gpgfl, *pgfl;
1005
1006 KASSERT(mutex_owned(&uvm_fpageqlock));
1007
1008 color = trycolor;
1009 pgfl = &ucpu->page_free[flist];
1010 gpgfl = &uvm.page_free[flist];
1011 do {
1012 /* cpu, try1 */
1013 if ((pg = LIST_FIRST((freeq =
1014 &pgfl->pgfl_buckets[color].pgfl_queues[try1]))) != NULL) {
1015 VM_FREE_PAGE_TO_CPU(pg)->pages[try1]--;
1016 uvmexp.cpuhit++;
1017 goto gotit;
1018 }
1019 /* global, try1 */
1020 if ((pg = LIST_FIRST((freeq =
1021 &gpgfl->pgfl_buckets[color].pgfl_queues[try1]))) != NULL) {
1022 VM_FREE_PAGE_TO_CPU(pg)->pages[try1]--;
1023 uvmexp.cpumiss++;
1024 goto gotit;
1025 }
1026 /* cpu, try2 */
1027 if ((pg = LIST_FIRST((freeq =
1028 &pgfl->pgfl_buckets[color].pgfl_queues[try2]))) != NULL) {
1029 VM_FREE_PAGE_TO_CPU(pg)->pages[try2]--;
1030 uvmexp.cpuhit++;
1031 goto gotit;
1032 }
1033 /* global, try2 */
1034 if ((pg = LIST_FIRST((freeq =
1035 &gpgfl->pgfl_buckets[color].pgfl_queues[try2]))) != NULL) {
1036 VM_FREE_PAGE_TO_CPU(pg)->pages[try2]--;
1037 uvmexp.cpumiss++;
1038 goto gotit;
1039 }
1040 color = (color + 1) & uvmexp.colormask;
1041 } while (color != trycolor);
1042
1043 return (NULL);
1044
1045 gotit:
1046 LIST_REMOVE(pg, pageq.list); /* global list */
1047 LIST_REMOVE(pg, listq.list); /* per-cpu list */
1048 uvmexp.free--;
1049
1050 /* update zero'd page count */
1051 if (pg->flags & PG_ZERO)
1052 uvmexp.zeropages--;
1053
1054 if (color == trycolor)
1055 uvmexp.colorhit++;
1056 else {
1057 uvmexp.colormiss++;
1058 *trycolorp = color;
1059 }
1060
1061 return (pg);
1062 }
1063
1064 /*
1065 * uvm_pagealloc_strat: allocate vm_page from a particular free list.
1066 *
1067 * => return null if no pages free
1068 * => wake up pagedaemon if number of free pages drops below low water mark
1069 * => if obj != NULL, obj must be locked (to put in obj's tree)
1070 * => if anon != NULL, anon must be locked (to put in anon)
1071 * => only one of obj or anon can be non-null
1072 * => caller must activate/deactivate page if it is not wired.
1073 * => free_list is ignored if strat == UVM_PGA_STRAT_NORMAL.
1074 * => policy decision: it is more important to pull a page off of the
1075 * appropriate priority free list than it is to get a zero'd or
1076 * unknown contents page. This is because we live with the
1077 * consequences of a bad free list decision for the entire
1078 * lifetime of the page, e.g. if the page comes from memory that
1079 * is slower to access.
1080 */
1081
1082 struct vm_page *
1083 uvm_pagealloc_strat(struct uvm_object *obj, voff_t off, struct vm_anon *anon,
1084 int flags, int strat, int free_list)
1085 {
1086 int lcv, try1, try2, zeroit = 0, color;
1087 struct uvm_cpu *ucpu;
1088 struct vm_page *pg;
1089 lwp_t *l;
1090
1091 KASSERT(obj == NULL || anon == NULL);
1092 KASSERT(anon == NULL || off == 0);
1093 KASSERT(off == trunc_page(off));
1094 KASSERT(obj == NULL || mutex_owned(&obj->vmobjlock));
1095 KASSERT(anon == NULL || mutex_owned(&anon->an_lock));
1096
1097 mutex_spin_enter(&uvm_fpageqlock);
1098
1099 /*
1100 * This implements a global round-robin page coloring
1101 * algorithm.
1102 *
1103 * XXXJRT: What about virtually-indexed caches?
1104 */
1105
1106 ucpu = curcpu()->ci_data.cpu_uvm;
1107 color = ucpu->page_free_nextcolor;
1108
1109 /*
1110 * check to see if we need to generate some free pages waking
1111 * the pagedaemon.
1112 */
1113
1114 uvm_kick_pdaemon();
1115
1116 /*
1117 * fail if any of these conditions is true:
1118 * [1] there really are no free pages, or
1119 * [2] only kernel "reserved" pages remain and
1120 * reserved pages have not been requested.
1121 * [3] only pagedaemon "reserved" pages remain and
1122 * the requestor isn't the pagedaemon.
1123 * we make kernel reserve pages available if called by a
1124 * kernel thread or a realtime thread.
1125 */
1126 l = curlwp;
1127 if (__predict_true(l != NULL) && lwp_eprio(l) >= PRI_KTHREAD) {
1128 flags |= UVM_PGA_USERESERVE;
1129 }
1130 if ((uvmexp.free <= uvmexp.reserve_kernel &&
1131 (flags & UVM_PGA_USERESERVE) == 0) ||
1132 (uvmexp.free <= uvmexp.reserve_pagedaemon &&
1133 curlwp != uvm.pagedaemon_lwp))
1134 goto fail;
1135
1136 #if PGFL_NQUEUES != 2
1137 #error uvm_pagealloc_strat needs to be updated
1138 #endif
1139
1140 /*
1141 * If we want a zero'd page, try the ZEROS queue first, otherwise
1142 * we try the UNKNOWN queue first.
1143 */
1144 if (flags & UVM_PGA_ZERO) {
1145 try1 = PGFL_ZEROS;
1146 try2 = PGFL_UNKNOWN;
1147 } else {
1148 try1 = PGFL_UNKNOWN;
1149 try2 = PGFL_ZEROS;
1150 }
1151
1152 again:
1153 switch (strat) {
1154 case UVM_PGA_STRAT_NORMAL:
1155 /* Check freelists: descending priority (ascending id) order */
1156 for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
1157 pg = uvm_pagealloc_pgfl(ucpu, lcv,
1158 try1, try2, &color);
1159 if (pg != NULL)
1160 goto gotit;
1161 }
1162
1163 /* No pages free! */
1164 goto fail;
1165
1166 case UVM_PGA_STRAT_ONLY:
1167 case UVM_PGA_STRAT_FALLBACK:
1168 /* Attempt to allocate from the specified free list. */
1169 KASSERT(free_list >= 0 && free_list < VM_NFREELIST);
1170 pg = uvm_pagealloc_pgfl(ucpu, free_list,
1171 try1, try2, &color);
1172 if (pg != NULL)
1173 goto gotit;
1174
1175 /* Fall back, if possible. */
1176 if (strat == UVM_PGA_STRAT_FALLBACK) {
1177 strat = UVM_PGA_STRAT_NORMAL;
1178 goto again;
1179 }
1180
1181 /* No pages free! */
1182 goto fail;
1183
1184 default:
1185 panic("uvm_pagealloc_strat: bad strat %d", strat);
1186 /* NOTREACHED */
1187 }
1188
1189 gotit:
1190 /*
1191 * We now know which color we actually allocated from; set
1192 * the next color accordingly.
1193 */
1194
1195 ucpu->page_free_nextcolor = (color + 1) & uvmexp.colormask;
1196
1197 /*
1198 * update allocation statistics and remember if we have to
1199 * zero the page
1200 */
1201
1202 if (flags & UVM_PGA_ZERO) {
1203 if (pg->flags & PG_ZERO) {
1204 uvmexp.pga_zerohit++;
1205 zeroit = 0;
1206 } else {
1207 uvmexp.pga_zeromiss++;
1208 zeroit = 1;
1209 }
1210 if (ucpu->pages[PGFL_ZEROS] < ucpu->pages[PGFL_UNKNOWN]) {
1211 ucpu->page_idle_zero = vm_page_zero_enable;
1212 }
1213 }
1214 KASSERT(pg->pqflags == PQ_FREE);
1215
1216 pg->offset = off;
1217 pg->uobject = obj;
1218 pg->uanon = anon;
1219 pg->flags = PG_BUSY|PG_CLEAN|PG_FAKE;
1220 if (anon) {
1221 anon->an_page = pg;
1222 pg->pqflags = PQ_ANON;
1223 atomic_inc_uint(&uvmexp.anonpages);
1224 } else {
1225 if (obj) {
1226 uvm_pageinsert(obj, pg);
1227 }
1228 pg->pqflags = 0;
1229 }
1230 mutex_spin_exit(&uvm_fpageqlock);
1231
1232 #if defined(UVM_PAGE_TRKOWN)
1233 pg->owner_tag = NULL;
1234 #endif
1235 UVM_PAGE_OWN(pg, "new alloc");
1236
1237 if (flags & UVM_PGA_ZERO) {
1238 /*
1239 * A zero'd page is not clean. If we got a page not already
1240 * zero'd, then we have to zero it ourselves.
1241 */
1242 pg->flags &= ~PG_CLEAN;
1243 if (zeroit)
1244 pmap_zero_page(VM_PAGE_TO_PHYS(pg));
1245 }
1246
1247 return(pg);
1248
1249 fail:
1250 mutex_spin_exit(&uvm_fpageqlock);
1251 return (NULL);
1252 }
1253
1254 /*
1255 * uvm_pagereplace: replace a page with another
1256 *
1257 * => object must be locked
1258 */
1259
1260 void
1261 uvm_pagereplace(struct vm_page *oldpg, struct vm_page *newpg)
1262 {
1263 struct uvm_object *uobj = oldpg->uobject;
1264
1265 KASSERT((oldpg->flags & PG_TABLED) != 0);
1266 KASSERT(uobj != NULL);
1267 KASSERT((newpg->flags & PG_TABLED) == 0);
1268 KASSERT(newpg->uobject == NULL);
1269 KASSERT(mutex_owned(&uobj->vmobjlock));
1270
1271 newpg->uobject = uobj;
1272 newpg->offset = oldpg->offset;
1273
1274 uvm_pageremove_tree(uobj, oldpg);
1275 uvm_pageinsert_tree(uobj, newpg);
1276 uvm_pageinsert_list(uobj, newpg, oldpg);
1277 uvm_pageremove_list(uobj, oldpg);
1278 }
1279
1280 /*
1281 * uvm_pagerealloc: reallocate a page from one object to another
1282 *
1283 * => both objects must be locked
1284 */
1285
1286 void
1287 uvm_pagerealloc(struct vm_page *pg, struct uvm_object *newobj, voff_t newoff)
1288 {
1289 /*
1290 * remove it from the old object
1291 */
1292
1293 if (pg->uobject) {
1294 uvm_pageremove(pg->uobject, pg);
1295 }
1296
1297 /*
1298 * put it in the new object
1299 */
1300
1301 if (newobj) {
1302 pg->uobject = newobj;
1303 pg->offset = newoff;
1304 uvm_pageinsert(newobj, pg);
1305 }
1306 }
1307
1308 #ifdef DEBUG
1309 /*
1310 * check if page is zero-filled
1311 *
1312 * - called with free page queue lock held.
1313 */
1314 void
1315 uvm_pagezerocheck(struct vm_page *pg)
1316 {
1317 int *p, *ep;
1318
1319 KASSERT(uvm_zerocheckkva != 0);
1320 KASSERT(mutex_owned(&uvm_fpageqlock));
1321
1322 /*
1323 * XXX assuming pmap_kenter_pa and pmap_kremove never call
1324 * uvm page allocator.
1325 *
1326 * it might be better to have "CPU-local temporary map" pmap interface.
1327 */
1328 pmap_kenter_pa(uvm_zerocheckkva, VM_PAGE_TO_PHYS(pg), VM_PROT_READ, 0);
1329 p = (int *)uvm_zerocheckkva;
1330 ep = (int *)((char *)p + PAGE_SIZE);
1331 pmap_update(pmap_kernel());
1332 while (p < ep) {
1333 if (*p != 0)
1334 panic("PG_ZERO page isn't zero-filled");
1335 p++;
1336 }
1337 pmap_kremove(uvm_zerocheckkva, PAGE_SIZE);
1338 /*
1339 * pmap_update() is not necessary here because no one except us
1340 * uses this VA.
1341 */
1342 }
1343 #endif /* DEBUG */
1344
1345 /*
1346 * uvm_pagefree: free page
1347 *
1348 * => erase page's identity (i.e. remove from object)
1349 * => put page on free list
1350 * => caller must lock owning object (either anon or uvm_object)
1351 * => caller must lock page queues
1352 * => assumes all valid mappings of pg are gone
1353 */
1354
1355 void
1356 uvm_pagefree(struct vm_page *pg)
1357 {
1358 struct pgflist *pgfl;
1359 struct uvm_cpu *ucpu;
1360 int index, color, queue;
1361 bool iszero;
1362
1363 #ifdef DEBUG
1364 if (pg->uobject == (void *)0xdeadbeef &&
1365 pg->uanon == (void *)0xdeadbeef) {
1366 panic("uvm_pagefree: freeing free page %p", pg);
1367 }
1368 #endif /* DEBUG */
1369
1370 KASSERT((pg->flags & PG_PAGEOUT) == 0);
1371 KASSERT(!(pg->pqflags & PQ_FREE));
1372 KASSERT(mutex_owned(&uvm_pageqlock) || !uvmpdpol_pageisqueued_p(pg));
1373 KASSERT(pg->uobject == NULL || mutex_owned(&pg->uobject->vmobjlock));
1374 KASSERT(pg->uobject != NULL || pg->uanon == NULL ||
1375 mutex_owned(&pg->uanon->an_lock));
1376
1377 /*
1378 * if the page is loaned, resolve the loan instead of freeing.
1379 */
1380
1381 if (pg->loan_count) {
1382 KASSERT(pg->wire_count == 0);
1383
1384 /*
1385 * if the page is owned by an anon then we just want to
1386 * drop anon ownership. the kernel will free the page when
1387 * it is done with it. if the page is owned by an object,
1388 * remove it from the object and mark it dirty for the benefit
1389 * of possible anon owners.
1390 *
1391 * regardless of previous ownership, wakeup any waiters,
1392 * unbusy the page, and we're done.
1393 */
1394
1395 if (pg->uobject != NULL) {
1396 uvm_pageremove(pg->uobject, pg);
1397 pg->flags &= ~PG_CLEAN;
1398 } else if (pg->uanon != NULL) {
1399 if ((pg->pqflags & PQ_ANON) == 0) {
1400 pg->loan_count--;
1401 } else {
1402 pg->pqflags &= ~PQ_ANON;
1403 atomic_dec_uint(&uvmexp.anonpages);
1404 }
1405 pg->uanon->an_page = NULL;
1406 pg->uanon = NULL;
1407 }
1408 if (pg->flags & PG_WANTED) {
1409 wakeup(pg);
1410 }
1411 pg->flags &= ~(PG_WANTED|PG_BUSY|PG_RELEASED|PG_PAGER1);
1412 #ifdef UVM_PAGE_TRKOWN
1413 pg->owner_tag = NULL;
1414 #endif
1415 if (pg->loan_count) {
1416 KASSERT(pg->uobject == NULL);
1417 if (pg->uanon == NULL) {
1418 uvm_pagedequeue(pg);
1419 }
1420 return;
1421 }
1422 }
1423
1424 /*
1425 * remove page from its object or anon.
1426 */
1427
1428 if (pg->uobject != NULL) {
1429 uvm_pageremove(pg->uobject, pg);
1430 } else if (pg->uanon != NULL) {
1431 pg->uanon->an_page = NULL;
1432 atomic_dec_uint(&uvmexp.anonpages);
1433 }
1434
1435 /*
1436 * now remove the page from the queues.
1437 */
1438
1439 uvm_pagedequeue(pg);
1440
1441 /*
1442 * if the page was wired, unwire it now.
1443 */
1444
1445 if (pg->wire_count) {
1446 pg->wire_count = 0;
1447 uvmexp.wired--;
1448 }
1449
1450 /*
1451 * and put on free queue
1452 */
1453
1454 iszero = (pg->flags & PG_ZERO);
1455 index = uvm_page_lookup_freelist(pg);
1456 color = VM_PGCOLOR_BUCKET(pg);
1457 queue = (iszero ? PGFL_ZEROS : PGFL_UNKNOWN);
1458
1459 #ifdef DEBUG
1460 pg->uobject = (void *)0xdeadbeef;
1461 pg->uanon = (void *)0xdeadbeef;
1462 #endif
1463
1464 mutex_spin_enter(&uvm_fpageqlock);
1465 pg->pqflags = PQ_FREE;
1466
1467 #ifdef DEBUG
1468 if (iszero)
1469 uvm_pagezerocheck(pg);
1470 #endif /* DEBUG */
1471
1472
1473 /* global list */
1474 pgfl = &uvm.page_free[index].pgfl_buckets[color].pgfl_queues[queue];
1475 LIST_INSERT_HEAD(pgfl, pg, pageq.list);
1476 uvmexp.free++;
1477 if (iszero) {
1478 uvmexp.zeropages++;
1479 }
1480
1481 /* per-cpu list */
1482 ucpu = curcpu()->ci_data.cpu_uvm;
1483 pg->offset = (uintptr_t)ucpu;
1484 pgfl = &ucpu->page_free[index].pgfl_buckets[color].pgfl_queues[queue];
1485 LIST_INSERT_HEAD(pgfl, pg, listq.list);
1486 ucpu->pages[queue]++;
1487 if (ucpu->pages[PGFL_ZEROS] < ucpu->pages[PGFL_UNKNOWN]) {
1488 ucpu->page_idle_zero = vm_page_zero_enable;
1489 }
1490
1491 mutex_spin_exit(&uvm_fpageqlock);
1492 }
1493
1494 /*
1495 * uvm_page_unbusy: unbusy an array of pages.
1496 *
1497 * => pages must either all belong to the same object, or all belong to anons.
1498 * => if pages are object-owned, object must be locked.
1499 * => if pages are anon-owned, anons must be locked.
1500 * => caller must lock page queues if pages may be released.
1501 * => caller must make sure that anon-owned pages are not PG_RELEASED.
1502 */
1503
1504 void
1505 uvm_page_unbusy(struct vm_page **pgs, int npgs)
1506 {
1507 struct vm_page *pg;
1508 int i;
1509 UVMHIST_FUNC("uvm_page_unbusy"); UVMHIST_CALLED(ubchist);
1510
1511 for (i = 0; i < npgs; i++) {
1512 pg = pgs[i];
1513 if (pg == NULL || pg == PGO_DONTCARE) {
1514 continue;
1515 }
1516
1517 KASSERT(pg->uobject == NULL ||
1518 mutex_owned(&pg->uobject->vmobjlock));
1519 KASSERT(pg->uobject != NULL ||
1520 (pg->uanon != NULL && mutex_owned(&pg->uanon->an_lock)));
1521
1522 KASSERT(pg->flags & PG_BUSY);
1523 KASSERT((pg->flags & PG_PAGEOUT) == 0);
1524 if (pg->flags & PG_WANTED) {
1525 wakeup(pg);
1526 }
1527 if (pg->flags & PG_RELEASED) {
1528 UVMHIST_LOG(ubchist, "releasing pg %p", pg,0,0,0);
1529 KASSERT(pg->uobject != NULL ||
1530 (pg->uanon != NULL && pg->uanon->an_ref > 0));
1531 pg->flags &= ~PG_RELEASED;
1532 uvm_pagefree(pg);
1533 } else {
1534 UVMHIST_LOG(ubchist, "unbusying pg %p", pg,0,0,0);
1535 KASSERT((pg->flags & PG_FAKE) == 0);
1536 pg->flags &= ~(PG_WANTED|PG_BUSY);
1537 UVM_PAGE_OWN(pg, NULL);
1538 }
1539 }
1540 }
1541
1542 #if defined(UVM_PAGE_TRKOWN)
1543 /*
1544 * uvm_page_own: set or release page ownership
1545 *
1546 * => this is a debugging function that keeps track of who sets PG_BUSY
1547 * and where they do it. it can be used to track down problems
1548 * such a process setting "PG_BUSY" and never releasing it.
1549 * => page's object [if any] must be locked
1550 * => if "tag" is NULL then we are releasing page ownership
1551 */
1552 void
1553 uvm_page_own(struct vm_page *pg, const char *tag)
1554 {
1555 struct uvm_object *uobj;
1556 struct vm_anon *anon;
1557
1558 KASSERT((pg->flags & (PG_PAGEOUT|PG_RELEASED)) == 0);
1559
1560 uobj = pg->uobject;
1561 anon = pg->uanon;
1562 if (uobj != NULL) {
1563 KASSERT(mutex_owned(&uobj->vmobjlock));
1564 } else if (anon != NULL) {
1565 KASSERT(mutex_owned(&anon->an_lock));
1566 }
1567
1568 KASSERT((pg->flags & PG_WANTED) == 0);
1569
1570 /* gain ownership? */
1571 if (tag) {
1572 KASSERT((pg->flags & PG_BUSY) != 0);
1573 if (pg->owner_tag) {
1574 printf("uvm_page_own: page %p already owned "
1575 "by proc %d [%s]\n", pg,
1576 pg->owner, pg->owner_tag);
1577 panic("uvm_page_own");
1578 }
1579 pg->owner = (curproc) ? curproc->p_pid : (pid_t) -1;
1580 pg->lowner = (curlwp) ? curlwp->l_lid : (lwpid_t) -1;
1581 pg->owner_tag = tag;
1582 return;
1583 }
1584
1585 /* drop ownership */
1586 KASSERT((pg->flags & PG_BUSY) == 0);
1587 if (pg->owner_tag == NULL) {
1588 printf("uvm_page_own: dropping ownership of an non-owned "
1589 "page (%p)\n", pg);
1590 panic("uvm_page_own");
1591 }
1592 if (!uvmpdpol_pageisqueued_p(pg)) {
1593 KASSERT((pg->uanon == NULL && pg->uobject == NULL) ||
1594 pg->wire_count > 0);
1595 } else {
1596 KASSERT(pg->wire_count == 0);
1597 }
1598 pg->owner_tag = NULL;
1599 }
1600 #endif
1601
1602 /*
1603 * uvm_pageidlezero: zero free pages while the system is idle.
1604 *
1605 * => try to complete one color bucket at a time, to reduce our impact
1606 * on the CPU cache.
1607 * => we loop until we either reach the target or there is a lwp ready
1608 * to run, or MD code detects a reason to break early.
1609 */
1610 void
1611 uvm_pageidlezero(void)
1612 {
1613 struct vm_page *pg;
1614 struct pgfreelist *pgfl, *gpgfl;
1615 struct uvm_cpu *ucpu;
1616 int free_list, firstbucket, nextbucket;
1617
1618 ucpu = curcpu()->ci_data.cpu_uvm;
1619 if (!ucpu->page_idle_zero ||
1620 ucpu->pages[PGFL_UNKNOWN] < uvmexp.ncolors) {
1621 ucpu->page_idle_zero = false;
1622 return;
1623 }
1624 mutex_enter(&uvm_fpageqlock);
1625 firstbucket = ucpu->page_free_nextcolor;
1626 nextbucket = firstbucket;
1627 do {
1628 for (free_list = 0; free_list < VM_NFREELIST; free_list++) {
1629 if (sched_curcpu_runnable_p()) {
1630 goto quit;
1631 }
1632 pgfl = &ucpu->page_free[free_list];
1633 gpgfl = &uvm.page_free[free_list];
1634 while ((pg = LIST_FIRST(&pgfl->pgfl_buckets[
1635 nextbucket].pgfl_queues[PGFL_UNKNOWN])) != NULL) {
1636 if (sched_curcpu_runnable_p()) {
1637 goto quit;
1638 }
1639 LIST_REMOVE(pg, pageq.list); /* global list */
1640 LIST_REMOVE(pg, listq.list); /* per-cpu list */
1641 ucpu->pages[PGFL_UNKNOWN]--;
1642 uvmexp.free--;
1643 KASSERT(pg->pqflags == PQ_FREE);
1644 pg->pqflags = 0;
1645 mutex_spin_exit(&uvm_fpageqlock);
1646 #ifdef PMAP_PAGEIDLEZERO
1647 if (!PMAP_PAGEIDLEZERO(VM_PAGE_TO_PHYS(pg))) {
1648
1649 /*
1650 * The machine-dependent code detected
1651 * some reason for us to abort zeroing
1652 * pages, probably because there is a
1653 * process now ready to run.
1654 */
1655
1656 mutex_spin_enter(&uvm_fpageqlock);
1657 pg->pqflags = PQ_FREE;
1658 LIST_INSERT_HEAD(&gpgfl->pgfl_buckets[
1659 nextbucket].pgfl_queues[
1660 PGFL_UNKNOWN], pg, pageq.list);
1661 LIST_INSERT_HEAD(&pgfl->pgfl_buckets[
1662 nextbucket].pgfl_queues[
1663 PGFL_UNKNOWN], pg, listq.list);
1664 ucpu->pages[PGFL_UNKNOWN]++;
1665 uvmexp.free++;
1666 uvmexp.zeroaborts++;
1667 goto quit;
1668 }
1669 #else
1670 pmap_zero_page(VM_PAGE_TO_PHYS(pg));
1671 #endif /* PMAP_PAGEIDLEZERO */
1672 pg->flags |= PG_ZERO;
1673
1674 mutex_spin_enter(&uvm_fpageqlock);
1675 pg->pqflags = PQ_FREE;
1676 LIST_INSERT_HEAD(&gpgfl->pgfl_buckets[
1677 nextbucket].pgfl_queues[PGFL_ZEROS],
1678 pg, pageq.list);
1679 LIST_INSERT_HEAD(&pgfl->pgfl_buckets[
1680 nextbucket].pgfl_queues[PGFL_ZEROS],
1681 pg, listq.list);
1682 ucpu->pages[PGFL_ZEROS]++;
1683 uvmexp.free++;
1684 uvmexp.zeropages++;
1685 }
1686 }
1687 if (ucpu->pages[PGFL_UNKNOWN] < uvmexp.ncolors) {
1688 break;
1689 }
1690 nextbucket = (nextbucket + 1) & uvmexp.colormask;
1691 } while (nextbucket != firstbucket);
1692 ucpu->page_idle_zero = false;
1693 quit:
1694 mutex_spin_exit(&uvm_fpageqlock);
1695 }
1696
1697 /*
1698 * uvm_pagelookup: look up a page
1699 *
1700 * => caller should lock object to keep someone from pulling the page
1701 * out from under it
1702 */
1703
1704 struct vm_page *
1705 uvm_pagelookup(struct uvm_object *obj, voff_t off)
1706 {
1707 struct vm_page *pg;
1708
1709 KASSERT(mutex_owned(&obj->vmobjlock));
1710
1711 pg = rb_tree_find_node(&obj->rb_tree, &off);
1712
1713 KASSERT(pg == NULL || obj->uo_npages != 0);
1714 KASSERT(pg == NULL || (pg->flags & (PG_RELEASED|PG_PAGEOUT)) == 0 ||
1715 (pg->flags & PG_BUSY) != 0);
1716 return pg;
1717 }
1718
1719 /*
1720 * uvm_pagewire: wire the page, thus removing it from the daemon's grasp
1721 *
1722 * => caller must lock page queues
1723 */
1724
1725 void
1726 uvm_pagewire(struct vm_page *pg)
1727 {
1728 KASSERT(mutex_owned(&uvm_pageqlock));
1729 #if defined(READAHEAD_STATS)
1730 if ((pg->pqflags & PQ_READAHEAD) != 0) {
1731 uvm_ra_hit.ev_count++;
1732 pg->pqflags &= ~PQ_READAHEAD;
1733 }
1734 #endif /* defined(READAHEAD_STATS) */
1735 if (pg->wire_count == 0) {
1736 uvm_pagedequeue(pg);
1737 uvmexp.wired++;
1738 }
1739 pg->wire_count++;
1740 }
1741
1742 /*
1743 * uvm_pageunwire: unwire the page.
1744 *
1745 * => activate if wire count goes to zero.
1746 * => caller must lock page queues
1747 */
1748
1749 void
1750 uvm_pageunwire(struct vm_page *pg)
1751 {
1752 KASSERT(mutex_owned(&uvm_pageqlock));
1753 pg->wire_count--;
1754 if (pg->wire_count == 0) {
1755 uvm_pageactivate(pg);
1756 uvmexp.wired--;
1757 }
1758 }
1759
1760 /*
1761 * uvm_pagedeactivate: deactivate page
1762 *
1763 * => caller must lock page queues
1764 * => caller must check to make sure page is not wired
1765 * => object that page belongs to must be locked (so we can adjust pg->flags)
1766 * => caller must clear the reference on the page before calling
1767 */
1768
1769 void
1770 uvm_pagedeactivate(struct vm_page *pg)
1771 {
1772
1773 KASSERT(mutex_owned(&uvm_pageqlock));
1774 KASSERT(pg->wire_count != 0 || uvmpdpol_pageisqueued_p(pg));
1775 uvmpdpol_pagedeactivate(pg);
1776 }
1777
1778 /*
1779 * uvm_pageactivate: activate page
1780 *
1781 * => caller must lock page queues
1782 */
1783
1784 void
1785 uvm_pageactivate(struct vm_page *pg)
1786 {
1787
1788 KASSERT(mutex_owned(&uvm_pageqlock));
1789 #if defined(READAHEAD_STATS)
1790 if ((pg->pqflags & PQ_READAHEAD) != 0) {
1791 uvm_ra_hit.ev_count++;
1792 pg->pqflags &= ~PQ_READAHEAD;
1793 }
1794 #endif /* defined(READAHEAD_STATS) */
1795 if (pg->wire_count != 0) {
1796 return;
1797 }
1798 uvmpdpol_pageactivate(pg);
1799 }
1800
1801 /*
1802 * uvm_pagedequeue: remove a page from any paging queue
1803 */
1804
1805 void
1806 uvm_pagedequeue(struct vm_page *pg)
1807 {
1808
1809 if (uvmpdpol_pageisqueued_p(pg)) {
1810 KASSERT(mutex_owned(&uvm_pageqlock));
1811 }
1812
1813 uvmpdpol_pagedequeue(pg);
1814 }
1815
1816 /*
1817 * uvm_pageenqueue: add a page to a paging queue without activating.
1818 * used where a page is not really demanded (yet). eg. read-ahead
1819 */
1820
1821 void
1822 uvm_pageenqueue(struct vm_page *pg)
1823 {
1824
1825 KASSERT(mutex_owned(&uvm_pageqlock));
1826 if (pg->wire_count != 0) {
1827 return;
1828 }
1829 uvmpdpol_pageenqueue(pg);
1830 }
1831
1832 /*
1833 * uvm_pagezero: zero fill a page
1834 *
1835 * => if page is part of an object then the object should be locked
1836 * to protect pg->flags.
1837 */
1838
1839 void
1840 uvm_pagezero(struct vm_page *pg)
1841 {
1842 pg->flags &= ~PG_CLEAN;
1843 pmap_zero_page(VM_PAGE_TO_PHYS(pg));
1844 }
1845
1846 /*
1847 * uvm_pagecopy: copy a page
1848 *
1849 * => if page is part of an object then the object should be locked
1850 * to protect pg->flags.
1851 */
1852
1853 void
1854 uvm_pagecopy(struct vm_page *src, struct vm_page *dst)
1855 {
1856
1857 dst->flags &= ~PG_CLEAN;
1858 pmap_copy_page(VM_PAGE_TO_PHYS(src), VM_PAGE_TO_PHYS(dst));
1859 }
1860
1861 /*
1862 * uvm_pageismanaged: test it see that a page (specified by PA) is managed.
1863 */
1864
1865 bool
1866 uvm_pageismanaged(paddr_t pa)
1867 {
1868
1869 return (vm_physseg_find(atop(pa), NULL) != -1);
1870 }
1871
1872 /*
1873 * uvm_page_lookup_freelist: look up the free list for the specified page
1874 */
1875
1876 int
1877 uvm_page_lookup_freelist(struct vm_page *pg)
1878 {
1879 int lcv;
1880
1881 lcv = vm_physseg_find(atop(VM_PAGE_TO_PHYS(pg)), NULL);
1882 KASSERT(lcv != -1);
1883 return (VM_PHYSMEM_PTR(lcv)->free_list);
1884 }
1885
1886 #if defined(DDB) || defined(DEBUGPRINT)
1887
1888 /*
1889 * uvm_page_printit: actually print the page
1890 */
1891
1892 static const char page_flagbits[] = UVM_PGFLAGBITS;
1893 static const char page_pqflagbits[] = UVM_PQFLAGBITS;
1894
1895 void
1896 uvm_page_printit(struct vm_page *pg, bool full,
1897 void (*pr)(const char *, ...))
1898 {
1899 struct vm_page *tpg;
1900 struct uvm_object *uobj;
1901 struct pgflist *pgl;
1902 char pgbuf[128];
1903 char pqbuf[128];
1904
1905 (*pr)("PAGE %p:\n", pg);
1906 snprintb(pgbuf, sizeof(pgbuf), page_flagbits, pg->flags);
1907 snprintb(pqbuf, sizeof(pqbuf), page_pqflagbits, pg->pqflags);
1908 (*pr)(" flags=%s, pqflags=%s, wire_count=%d, pa=0x%lx\n",
1909 pgbuf, pqbuf, pg->wire_count, (long)VM_PAGE_TO_PHYS(pg));
1910 (*pr)(" uobject=%p, uanon=%p, offset=0x%llx loan_count=%d\n",
1911 pg->uobject, pg->uanon, (long long)pg->offset, pg->loan_count);
1912 #if defined(UVM_PAGE_TRKOWN)
1913 if (pg->flags & PG_BUSY)
1914 (*pr)(" owning process = %d, tag=%s\n",
1915 pg->owner, pg->owner_tag);
1916 else
1917 (*pr)(" page not busy, no owner\n");
1918 #else
1919 (*pr)(" [page ownership tracking disabled]\n");
1920 #endif
1921
1922 if (!full)
1923 return;
1924
1925 /* cross-verify object/anon */
1926 if ((pg->pqflags & PQ_FREE) == 0) {
1927 if (pg->pqflags & PQ_ANON) {
1928 if (pg->uanon == NULL || pg->uanon->an_page != pg)
1929 (*pr)(" >>> ANON DOES NOT POINT HERE <<< (%p)\n",
1930 (pg->uanon) ? pg->uanon->an_page : NULL);
1931 else
1932 (*pr)(" anon backpointer is OK\n");
1933 } else {
1934 uobj = pg->uobject;
1935 if (uobj) {
1936 (*pr)(" checking object list\n");
1937 TAILQ_FOREACH(tpg, &uobj->memq, listq.queue) {
1938 if (tpg == pg) {
1939 break;
1940 }
1941 }
1942 if (tpg)
1943 (*pr)(" page found on object list\n");
1944 else
1945 (*pr)(" >>> PAGE NOT FOUND ON OBJECT LIST! <<<\n");
1946 }
1947 }
1948 }
1949
1950 /* cross-verify page queue */
1951 if (pg->pqflags & PQ_FREE) {
1952 int fl = uvm_page_lookup_freelist(pg);
1953 int color = VM_PGCOLOR_BUCKET(pg);
1954 pgl = &uvm.page_free[fl].pgfl_buckets[color].pgfl_queues[
1955 ((pg)->flags & PG_ZERO) ? PGFL_ZEROS : PGFL_UNKNOWN];
1956 } else {
1957 pgl = NULL;
1958 }
1959
1960 if (pgl) {
1961 (*pr)(" checking pageq list\n");
1962 LIST_FOREACH(tpg, pgl, pageq.list) {
1963 if (tpg == pg) {
1964 break;
1965 }
1966 }
1967 if (tpg)
1968 (*pr)(" page found on pageq list\n");
1969 else
1970 (*pr)(" >>> PAGE NOT FOUND ON PAGEQ LIST! <<<\n");
1971 }
1972 }
1973
1974 /*
1975 * uvm_pages_printthem - print a summary of all managed pages
1976 */
1977
1978 void
1979 uvm_page_printall(void (*pr)(const char *, ...))
1980 {
1981 unsigned i;
1982 struct vm_page *pg;
1983
1984 (*pr)("%18s %4s %4s %18s %18s"
1985 #ifdef UVM_PAGE_TRKOWN
1986 " OWNER"
1987 #endif
1988 "\n", "PAGE", "FLAG", "PQ", "UOBJECT", "UANON");
1989 for (i = 0; i < vm_nphysmem; i++) {
1990 for (pg = VM_PHYSMEM_PTR(i)->pgs; pg < VM_PHYSMEM_PTR(i)->lastpg; pg++) {
1991 (*pr)("%18p %04x %04x %18p %18p",
1992 pg, pg->flags, pg->pqflags, pg->uobject,
1993 pg->uanon);
1994 #ifdef UVM_PAGE_TRKOWN
1995 if (pg->flags & PG_BUSY)
1996 (*pr)(" %d [%s]", pg->owner, pg->owner_tag);
1997 #endif
1998 (*pr)("\n");
1999 }
2000 }
2001 }
2002
2003 #endif /* DDB || DEBUGPRINT */
2004