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