uvm_page.c revision 1.221 1 /* $NetBSD: uvm_page.c,v 1.221 2020/01/05 22:01:09 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.221 2020/01/05 22:01:09 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. For non-NUMA
810 * assign take physical page frame
811 * number and cache color into
812 * account. We do this to try and
813 * avoid defeating any memory
814 * interleaving in the hardware.
815 */
816 KASSERT(
817 uvm_page_get_bucket(pg) == ob);
818 KASSERT(fl ==
819 uvm_page_get_freelist(pg));
820 if (uvm.numa_alloc) {
821 nb = uvm_page_numa_lookup(pg);
822 } else {
823 nb = atop(VM_PAGE_TO_PHYS(pg))
824 / uvmexp.ncolors / 8
825 % newnbuckets;
826 }
827 uvm_page_set_bucket(pg, nb);
828 npgb = npgfl.pgfl_buckets[nb];
829 npgb->pgb_nfree++;
830 nc = VM_PGCOLOR(pg);
831 nhead = &npgb->pgb_colors[nc];
832 LIST_INSERT_HEAD(nhead, pg, pageq.list);
833 }
834 }
835 }
836 /* Install the new freelist. */
837 memcpy(&uvm.page_free[fl], &npgfl, sizeof(npgfl));
838 }
839
840 /* Unlock and free the old memory. */
841 oldbucketmemsize = recolored_pages_memsize;
842 oldbucketmem = recolored_pages_mem;
843 recolored_pages_memsize = bucketmemsize;
844 recolored_pages_mem = bucketmem;
845
846 uvm_pgfl_unlock();
847 uvm_pgflcache_resume();
848
849 if (oldbucketmemsize) {
850 kmem_free(oldbucketmem, oldbucketmemsize);
851 }
852
853 /*
854 * this calls uvm_km_alloc() which may want to hold
855 * uvm_freelist_lock.
856 */
857 uvm_pager_realloc_emerg();
858 }
859
860 /*
861 * uvm_page_recolor: Recolor the pages if the new color count is
862 * larger than the old one.
863 */
864
865 void
866 uvm_page_recolor(int newncolors)
867 {
868
869 uvm_page_redim(newncolors, uvm.bucketcount);
870 }
871
872 /*
873 * uvm_page_rebucket: Determine a bucket structure and redim the free
874 * lists to match.
875 */
876
877 void
878 uvm_page_rebucket(void)
879 {
880 u_int min_numa, max_numa, npackage, shift;
881 struct cpu_info *ci, *ci2, *ci3;
882 CPU_INFO_ITERATOR cii;
883
884 /*
885 * If we have more than one NUMA node, and the maximum NUMA node ID
886 * is less than PGFL_MAX_BUCKETS, then we'll use NUMA distribution
887 * for free pages. uvm_pagefree() will not reassign pages to a
888 * different bucket on free.
889 */
890 min_numa = (u_int)-1;
891 max_numa = 0;
892 for (CPU_INFO_FOREACH(cii, ci)) {
893 if (ci->ci_numa_id < min_numa) {
894 min_numa = ci->ci_numa_id;
895 }
896 if (ci->ci_numa_id > max_numa) {
897 max_numa = ci->ci_numa_id;
898 }
899 }
900 if (min_numa != max_numa && max_numa < PGFL_MAX_BUCKETS) {
901 #ifdef NUMA
902 /*
903 * We can do this, and it seems to work well, but until
904 * further experiments are done we'll stick with the cache
905 * locality strategy.
906 */
907 aprint_debug("UVM: using NUMA allocation scheme\n");
908 for (CPU_INFO_FOREACH(cii, ci)) {
909 ci->ci_data.cpu_uvm->pgflbucket = ci->ci_numa_id;
910 }
911 uvm.numa_alloc = true;
912 uvm_page_redim(uvmexp.ncolors, max_numa + 1);
913 return;
914 #endif
915 }
916
917 /*
918 * Otherwise we'll go with a scheme to maximise L2/L3 cache locality
919 * and minimise lock contention. Count the total number of CPU
920 * packages, and then try to distribute the buckets among CPU
921 * packages evenly. uvm_pagefree() will reassign pages to the
922 * freeing CPU's preferred bucket on free.
923 */
924 npackage = 0;
925 ci = curcpu();
926 ci2 = ci;
927 do {
928 npackage++;
929 ci2 = ci2->ci_sibling[CPUREL_PEER];
930 } while (ci2 != ci);
931
932 /*
933 * Figure out how to arrange the packages & buckets, and the total
934 * number of buckets we need. XXX 2 may not be the best factor.
935 */
936 for (shift = 0; npackage > PGFL_MAX_BUCKETS; shift++) {
937 npackage >>= 1;
938 }
939 uvm_page_redim(uvmexp.ncolors, npackage);
940
941 /*
942 * Now tell each CPU which bucket to use. In the outer loop, scroll
943 * through all CPU packages.
944 */
945 npackage = 0;
946 ci = curcpu();
947 ci2 = ci;
948 do {
949 /*
950 * In the inner loop, scroll through all CPUs in the package
951 * and assign the same bucket ID.
952 */
953 ci3 = ci2;
954 do {
955 ci3->ci_data.cpu_uvm->pgflbucket = npackage >> shift;
956 ci3 = ci3->ci_sibling[CPUREL_PACKAGE];
957 } while (ci3 != ci2);
958 npackage++;
959 ci2 = ci2->ci_sibling[CPUREL_PEER];
960 } while (ci2 != ci);
961
962 aprint_debug("UVM: using package allocation scheme, "
963 "%d package(s) per bucket\n", 1 << shift);
964 }
965
966 /*
967 * uvm_cpu_attach: initialize per-CPU data structures.
968 */
969
970 void
971 uvm_cpu_attach(struct cpu_info *ci)
972 {
973 struct uvm_cpu *ucpu;
974
975 /* Already done in uvm_page_init(). */
976 if (!CPU_IS_PRIMARY(ci)) {
977 /* Add more reserve pages for this CPU. */
978 uvmexp.reserve_kernel += vm_page_reserve_kernel;
979
980 /* Allocate per-CPU data structures. */
981 ucpu = kmem_zalloc(sizeof(struct uvm_cpu) + coherency_unit - 1,
982 KM_SLEEP);
983 ucpu = (struct uvm_cpu *)roundup2((uintptr_t)ucpu,
984 coherency_unit);
985 ci->ci_data.cpu_uvm = ucpu;
986 } else {
987 ucpu = ci->ci_data.cpu_uvm;
988 }
989
990 uvmpdpol_init_cpu(ucpu);
991
992 /*
993 * Attach RNG source for this CPU's VM events
994 */
995 rnd_attach_source(&ucpu->rs, ci->ci_data.cpu_name, RND_TYPE_VM,
996 RND_FLAG_COLLECT_TIME|RND_FLAG_COLLECT_VALUE|
997 RND_FLAG_ESTIMATE_VALUE);
998 }
999
1000 /*
1001 * uvm_availmem: fetch the total amount of free memory in pages. this can
1002 * have a detrimental effect on performance due to false sharing; don't call
1003 * unless needed.
1004 */
1005
1006 int
1007 uvm_availmem(void)
1008 {
1009 struct pgfreelist *pgfl;
1010 int fl, b, fpages;
1011
1012 fpages = 0;
1013 for (fl = 0; fl < VM_NFREELIST; fl++) {
1014 pgfl = &uvm.page_free[fl];
1015 for (b = 0; b < uvm.bucketcount; b++) {
1016 fpages += pgfl->pgfl_buckets[b]->pgb_nfree;
1017 }
1018 }
1019 return fpages;
1020 }
1021
1022 /*
1023 * uvm_pagealloc_pgb: helper routine that tries to allocate any color from a
1024 * specific freelist and specific bucket only.
1025 *
1026 * => must be at IPL_VM or higher to protect per-CPU data structures.
1027 */
1028
1029 static struct vm_page *
1030 uvm_pagealloc_pgb(struct uvm_cpu *ucpu, int f, int b, int *trycolorp, int flags)
1031 {
1032 int c, trycolor, colormask;
1033 struct pgflbucket *pgb;
1034 struct vm_page *pg;
1035 kmutex_t *lock;
1036 bool fill;
1037
1038 /*
1039 * Skip the bucket if empty, no lock needed. There could be many
1040 * empty freelists/buckets.
1041 */
1042 pgb = uvm.page_free[f].pgfl_buckets[b];
1043 if (pgb->pgb_nfree == 0) {
1044 return NULL;
1045 }
1046
1047 /* Skip bucket if low on memory. */
1048 lock = &uvm_freelist_locks[b].lock;
1049 mutex_spin_enter(lock);
1050 if (__predict_false(pgb->pgb_nfree <= uvmexp.reserve_kernel)) {
1051 if ((flags & UVM_PGA_USERESERVE) == 0 ||
1052 (pgb->pgb_nfree <= uvmexp.reserve_pagedaemon &&
1053 curlwp != uvm.pagedaemon_lwp)) {
1054 mutex_spin_exit(lock);
1055 return NULL;
1056 }
1057 fill = false;
1058 } else {
1059 fill = true;
1060 }
1061
1062 /* Try all page colors as needed. */
1063 c = trycolor = *trycolorp;
1064 colormask = uvmexp.colormask;
1065 do {
1066 pg = LIST_FIRST(&pgb->pgb_colors[c]);
1067 if (__predict_true(pg != NULL)) {
1068 /*
1069 * Got a free page! PG_FREE must be cleared under
1070 * lock because of uvm_pglistalloc().
1071 */
1072 LIST_REMOVE(pg, pageq.list);
1073 KASSERT(pg->flags & PG_FREE);
1074 pg->flags &= PG_ZERO;
1075 pgb->pgb_nfree--;
1076
1077 /*
1078 * While we have the bucket locked and our data
1079 * structures fresh in L1 cache, we have an ideal
1080 * opportunity to grab some pages for the freelist
1081 * cache without causing extra contention. Only do
1082 * so if we found pages in this CPU's preferred
1083 * bucket.
1084 */
1085 if (__predict_true(b == ucpu->pgflbucket && fill)) {
1086 uvm_pgflcache_fill(ucpu, f, b, c);
1087 }
1088 mutex_spin_exit(lock);
1089 KASSERT(uvm_page_get_bucket(pg) == b);
1090 CPU_COUNT(c == trycolor ?
1091 CPU_COUNT_COLORHIT : CPU_COUNT_COLORMISS, 1);
1092 CPU_COUNT(CPU_COUNT_CPUMISS, 1);
1093 *trycolorp = c;
1094 return pg;
1095 }
1096 c = (c + 1) & colormask;
1097 } while (c != trycolor);
1098 mutex_spin_exit(lock);
1099
1100 return NULL;
1101 }
1102
1103 /*
1104 * uvm_pagealloc_pgfl: helper routine for uvm_pagealloc_strat that allocates
1105 * any color from any bucket, in a specific freelist.
1106 *
1107 * => must be at IPL_VM or higher to protect per-CPU data structures.
1108 */
1109
1110 static struct vm_page *
1111 uvm_pagealloc_pgfl(struct uvm_cpu *ucpu, int f, int *trycolorp, int flags)
1112 {
1113 int b, trybucket, bucketcount;
1114 struct vm_page *pg;
1115
1116 /* Try for the exact thing in the per-CPU cache. */
1117 if ((pg = uvm_pgflcache_alloc(ucpu, f, *trycolorp)) != NULL) {
1118 CPU_COUNT(CPU_COUNT_CPUHIT, 1);
1119 CPU_COUNT(CPU_COUNT_COLORHIT, 1);
1120 return pg;
1121 }
1122
1123 /* Walk through all buckets, trying our preferred bucket first. */
1124 trybucket = ucpu->pgflbucket;
1125 b = trybucket;
1126 bucketcount = uvm.bucketcount;
1127 do {
1128 pg = uvm_pagealloc_pgb(ucpu, f, b, trycolorp, flags);
1129 if (pg != NULL) {
1130 return pg;
1131 }
1132 b = (b + 1 == bucketcount ? 0 : b + 1);
1133 } while (b != trybucket);
1134
1135 return NULL;
1136 }
1137
1138 /*
1139 * uvm_pagealloc_strat: allocate vm_page from a particular free list.
1140 *
1141 * => return null if no pages free
1142 * => wake up pagedaemon if number of free pages drops below low water mark
1143 * => if obj != NULL, obj must be locked (to put in obj's tree)
1144 * => if anon != NULL, anon must be locked (to put in anon)
1145 * => only one of obj or anon can be non-null
1146 * => caller must activate/deactivate page if it is not wired.
1147 * => free_list is ignored if strat == UVM_PGA_STRAT_NORMAL.
1148 * => policy decision: it is more important to pull a page off of the
1149 * appropriate priority free list than it is to get a zero'd or
1150 * unknown contents page. This is because we live with the
1151 * consequences of a bad free list decision for the entire
1152 * lifetime of the page, e.g. if the page comes from memory that
1153 * is slower to access.
1154 */
1155
1156 struct vm_page *
1157 uvm_pagealloc_strat(struct uvm_object *obj, voff_t off, struct vm_anon *anon,
1158 int flags, int strat, int free_list)
1159 {
1160 int zeroit = 0, color;
1161 int lcv, error, s;
1162 struct uvm_cpu *ucpu;
1163 struct vm_page *pg;
1164 lwp_t *l;
1165
1166 KASSERT(obj == NULL || anon == NULL);
1167 KASSERT(anon == NULL || (flags & UVM_FLAG_COLORMATCH) || off == 0);
1168 KASSERT(off == trunc_page(off));
1169 KASSERT(obj == NULL || mutex_owned(obj->vmobjlock));
1170 KASSERT(anon == NULL || anon->an_lock == NULL ||
1171 mutex_owned(anon->an_lock));
1172
1173 /*
1174 * This implements a global round-robin page coloring
1175 * algorithm.
1176 */
1177
1178 s = splvm();
1179 ucpu = curcpu()->ci_data.cpu_uvm;
1180 if (flags & UVM_FLAG_COLORMATCH) {
1181 color = atop(off) & uvmexp.colormask;
1182 } else {
1183 color = ucpu->pgflcolor;
1184 }
1185
1186 /*
1187 * fail if any of these conditions is true:
1188 * [1] there really are no free pages, or
1189 * [2] only kernel "reserved" pages remain and
1190 * reserved pages have not been requested.
1191 * [3] only pagedaemon "reserved" pages remain and
1192 * the requestor isn't the pagedaemon.
1193 * we make kernel reserve pages available if called by a
1194 * kernel thread or a realtime thread.
1195 */
1196 l = curlwp;
1197 if (__predict_true(l != NULL) && lwp_eprio(l) >= PRI_KTHREAD) {
1198 flags |= UVM_PGA_USERESERVE;
1199 }
1200
1201 /* If the allocator's running in NUMA mode, go with NUMA strategy. */
1202 if (uvm.numa_alloc && strat == UVM_PGA_STRAT_NORMAL) {
1203 strat = UVM_PGA_STRAT_NUMA;
1204 }
1205
1206 again:
1207 switch (strat) {
1208 case UVM_PGA_STRAT_NORMAL:
1209 /* Check freelists: descending priority (ascending id) order. */
1210 for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
1211 pg = uvm_pagealloc_pgfl(ucpu, lcv, &color, flags);
1212 if (pg != NULL) {
1213 goto gotit;
1214 }
1215 }
1216
1217 /* No pages free! Have pagedaemon free some memory. */
1218 splx(s);
1219 uvm_kick_pdaemon();
1220 return NULL;
1221
1222 case UVM_PGA_STRAT_ONLY:
1223 case UVM_PGA_STRAT_FALLBACK:
1224 /* Attempt to allocate from the specified free list. */
1225 KASSERT(free_list >= 0 && free_list < VM_NFREELIST);
1226 pg = uvm_pagealloc_pgfl(ucpu, free_list, &color, flags);
1227 if (pg != NULL) {
1228 goto gotit;
1229 }
1230
1231 /* Fall back, if possible. */
1232 if (strat == UVM_PGA_STRAT_FALLBACK) {
1233 strat = UVM_PGA_STRAT_NORMAL;
1234 goto again;
1235 }
1236
1237 /* No pages free! Have pagedaemon free some memory. */
1238 splx(s);
1239 uvm_kick_pdaemon();
1240 return NULL;
1241
1242 case UVM_PGA_STRAT_NUMA:
1243 /*
1244 * NUMA strategy: allocating from the correct bucket is more
1245 * important than observing freelist priority. Look only to
1246 * the current NUMA node; if that fails, we need to look to
1247 * other NUMA nodes, so retry with the normal strategy.
1248 */
1249 for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
1250 pg = uvm_pgflcache_alloc(ucpu, lcv, color);
1251 if (pg != NULL) {
1252 CPU_COUNT(CPU_COUNT_CPUHIT, 1);
1253 CPU_COUNT(CPU_COUNT_COLORHIT, 1);
1254 goto gotit;
1255 }
1256 pg = uvm_pagealloc_pgb(ucpu, lcv,
1257 ucpu->pgflbucket, &color, flags);
1258 if (pg != NULL) {
1259 goto gotit;
1260 }
1261 }
1262 strat = UVM_PGA_STRAT_NORMAL;
1263 goto again;
1264
1265 default:
1266 panic("uvm_pagealloc_strat: bad strat %d", strat);
1267 /* NOTREACHED */
1268 }
1269
1270 gotit:
1271 /*
1272 * We now know which color we actually allocated from; set
1273 * the next color accordingly.
1274 */
1275
1276 ucpu->pgflcolor = (color + 1) & uvmexp.colormask;
1277
1278 /*
1279 * while still at IPL_VM, update allocation statistics and remember
1280 * if we have to zero the page
1281 */
1282
1283 if (flags & UVM_PGA_ZERO) {
1284 if (pg->flags & PG_ZERO) {
1285 CPU_COUNT(CPU_COUNT_PGA_ZEROHIT, 1);
1286 zeroit = 0;
1287 } else {
1288 CPU_COUNT(CPU_COUNT_PGA_ZEROMISS, 1);
1289 zeroit = 1;
1290 }
1291 }
1292 if (pg->flags & PG_ZERO) {
1293 CPU_COUNT(CPU_COUNT_ZEROPAGES, -1);
1294 }
1295 if (anon) {
1296 CPU_COUNT(CPU_COUNT_ANONPAGES, 1);
1297 }
1298 splx(s);
1299 KASSERT((pg->flags & ~(PG_ZERO|PG_FREE)) == 0);
1300
1301 /*
1302 * assign the page to the object. as the page was free, we know
1303 * that pg->uobject and pg->uanon are NULL. we only need to take
1304 * the page's interlock if we are changing the values.
1305 */
1306 if (anon != NULL || obj != NULL) {
1307 mutex_enter(&pg->interlock);
1308 }
1309 pg->offset = off;
1310 pg->uobject = obj;
1311 pg->uanon = anon;
1312 KASSERT(uvm_page_owner_locked_p(pg));
1313 pg->flags = PG_BUSY|PG_CLEAN|PG_FAKE;
1314 if (anon) {
1315 anon->an_page = pg;
1316 pg->flags |= PG_ANON;
1317 mutex_exit(&pg->interlock);
1318 } else if (obj) {
1319 uvm_pageinsert_object(obj, pg);
1320 mutex_exit(&pg->interlock);
1321 error = uvm_pageinsert_tree(obj, pg);
1322 if (error != 0) {
1323 mutex_enter(&pg->interlock);
1324 uvm_pageremove_object(obj, pg);
1325 mutex_exit(&pg->interlock);
1326 uvm_pagefree(pg);
1327 return NULL;
1328 }
1329 }
1330
1331 #if defined(UVM_PAGE_TRKOWN)
1332 pg->owner_tag = NULL;
1333 #endif
1334 UVM_PAGE_OWN(pg, "new alloc");
1335
1336 if (flags & UVM_PGA_ZERO) {
1337 /*
1338 * A zero'd page is not clean. If we got a page not already
1339 * zero'd, then we have to zero it ourselves.
1340 */
1341 pg->flags &= ~PG_CLEAN;
1342 if (zeroit)
1343 pmap_zero_page(VM_PAGE_TO_PHYS(pg));
1344 }
1345
1346 return(pg);
1347 }
1348
1349 /*
1350 * uvm_pagereplace: replace a page with another
1351 *
1352 * => object must be locked
1353 * => page interlocks must be held
1354 */
1355
1356 void
1357 uvm_pagereplace(struct vm_page *oldpg, struct vm_page *newpg)
1358 {
1359 struct uvm_object *uobj = oldpg->uobject;
1360 struct vm_page *pg __diagused;
1361
1362 KASSERT((oldpg->flags & PG_TABLED) != 0);
1363 KASSERT(uobj != NULL);
1364 KASSERT((newpg->flags & PG_TABLED) == 0);
1365 KASSERT(newpg->uobject == NULL);
1366 KASSERT(mutex_owned(uobj->vmobjlock));
1367 KASSERT(mutex_owned(&oldpg->interlock));
1368 KASSERT(mutex_owned(&newpg->interlock));
1369
1370 newpg->offset = oldpg->offset;
1371 pg = radix_tree_replace_node(&uobj->uo_pages,
1372 newpg->offset >> PAGE_SHIFT, newpg);
1373 KASSERT(pg == oldpg);
1374
1375 newpg->uobject = uobj;
1376 uvm_pageinsert_object(uobj, newpg);
1377 uvm_pageremove_object(uobj, oldpg);
1378 }
1379
1380 /*
1381 * uvm_pagerealloc: reallocate a page from one object to another
1382 *
1383 * => both objects must be locked
1384 * => both interlocks must be held
1385 */
1386
1387 void
1388 uvm_pagerealloc(struct vm_page *pg, struct uvm_object *newobj, voff_t newoff)
1389 {
1390 /*
1391 * remove it from the old object
1392 */
1393
1394 if (pg->uobject) {
1395 uvm_pageremove_tree(pg->uobject, pg);
1396 uvm_pageremove_object(pg->uobject, pg);
1397 }
1398
1399 /*
1400 * put it in the new object
1401 */
1402
1403 if (newobj) {
1404 /*
1405 * XXX we have no in-tree users of this functionality
1406 */
1407 panic("uvm_pagerealloc: no impl");
1408 }
1409 }
1410
1411 #ifdef DEBUG
1412 /*
1413 * check if page is zero-filled
1414 */
1415 void
1416 uvm_pagezerocheck(struct vm_page *pg)
1417 {
1418 int *p, *ep;
1419
1420 KASSERT(uvm_zerocheckkva != 0);
1421
1422 /*
1423 * XXX assuming pmap_kenter_pa and pmap_kremove never call
1424 * uvm page allocator.
1425 *
1426 * it might be better to have "CPU-local temporary map" pmap interface.
1427 */
1428 pmap_kenter_pa(uvm_zerocheckkva, VM_PAGE_TO_PHYS(pg), VM_PROT_READ, 0);
1429 p = (int *)uvm_zerocheckkva;
1430 ep = (int *)((char *)p + PAGE_SIZE);
1431 pmap_update(pmap_kernel());
1432 while (p < ep) {
1433 if (*p != 0)
1434 panic("PG_ZERO page isn't zero-filled");
1435 p++;
1436 }
1437 pmap_kremove(uvm_zerocheckkva, PAGE_SIZE);
1438 /*
1439 * pmap_update() is not necessary here because no one except us
1440 * uses this VA.
1441 */
1442 }
1443 #endif /* DEBUG */
1444
1445 /*
1446 * uvm_pagefree: free page
1447 *
1448 * => erase page's identity (i.e. remove from object)
1449 * => put page on free list
1450 * => caller must lock owning object (either anon or uvm_object)
1451 * => assumes all valid mappings of pg are gone
1452 */
1453
1454 void
1455 uvm_pagefree(struct vm_page *pg)
1456 {
1457 struct pgfreelist *pgfl;
1458 struct pgflbucket *pgb;
1459 struct uvm_cpu *ucpu;
1460 kmutex_t *lock;
1461 int bucket, s;
1462 bool locked;
1463
1464 #ifdef DEBUG
1465 if (pg->uobject == (void *)0xdeadbeef &&
1466 pg->uanon == (void *)0xdeadbeef) {
1467 panic("uvm_pagefree: freeing free page %p", pg);
1468 }
1469 #endif /* DEBUG */
1470
1471 KASSERT((pg->flags & PG_PAGEOUT) == 0);
1472 KASSERT(!(pg->flags & PG_FREE));
1473 KASSERT(pg->uobject == NULL || mutex_owned(pg->uobject->vmobjlock));
1474 KASSERT(pg->uobject != NULL || pg->uanon == NULL ||
1475 mutex_owned(pg->uanon->an_lock));
1476
1477 /*
1478 * remove the page from the object's tree beore acquiring any page
1479 * interlocks: this can acquire locks to free radixtree nodes.
1480 */
1481 if (pg->uobject != NULL) {
1482 uvm_pageremove_tree(pg->uobject, pg);
1483 }
1484
1485 /*
1486 * if the page is loaned, resolve the loan instead of freeing.
1487 */
1488
1489 if (pg->loan_count) {
1490 KASSERT(pg->wire_count == 0);
1491
1492 /*
1493 * if the page is owned by an anon then we just want to
1494 * drop anon ownership. the kernel will free the page when
1495 * it is done with it. if the page is owned by an object,
1496 * remove it from the object and mark it dirty for the benefit
1497 * of possible anon owners.
1498 *
1499 * regardless of previous ownership, wakeup any waiters,
1500 * unbusy the page, and we're done.
1501 */
1502
1503 uvm_pagelock(pg);
1504 locked = true;
1505 if (pg->uobject != NULL) {
1506 uvm_pageremove_object(pg->uobject, pg);
1507 pg->flags &= ~PG_CLEAN;
1508 } else if (pg->uanon != NULL) {
1509 if ((pg->flags & PG_ANON) == 0) {
1510 pg->loan_count--;
1511 } else {
1512 pg->flags &= ~PG_ANON;
1513 cpu_count(CPU_COUNT_ANONPAGES, -1);
1514 }
1515 pg->uanon->an_page = NULL;
1516 pg->uanon = NULL;
1517 }
1518 if (pg->flags & PG_WANTED) {
1519 wakeup(pg);
1520 }
1521 pg->flags &= ~(PG_WANTED|PG_BUSY|PG_RELEASED|PG_PAGER1);
1522 #ifdef UVM_PAGE_TRKOWN
1523 pg->owner_tag = NULL;
1524 #endif
1525 if (pg->loan_count) {
1526 KASSERT(pg->uobject == NULL);
1527 if (pg->uanon == NULL) {
1528 uvm_pagedequeue(pg);
1529 }
1530 uvm_pageunlock(pg);
1531 return;
1532 }
1533 } else if (pg->uobject != NULL || pg->uanon != NULL ||
1534 pg->wire_count != 0) {
1535 uvm_pagelock(pg);
1536 locked = true;
1537 } else {
1538 locked = false;
1539 }
1540
1541 /*
1542 * remove page from its object or anon.
1543 */
1544 if (pg->uobject != NULL) {
1545 uvm_pageremove_object(pg->uobject, pg);
1546 } else if (pg->uanon != NULL) {
1547 pg->uanon->an_page = NULL;
1548 pg->uanon = NULL;
1549 cpu_count(CPU_COUNT_ANONPAGES, -1);
1550 }
1551
1552 /*
1553 * if the page was wired, unwire it now.
1554 */
1555
1556 if (pg->wire_count) {
1557 pg->wire_count = 0;
1558 atomic_dec_uint(&uvmexp.wired);
1559 }
1560 if (locked) {
1561 /*
1562 * now remove the page from the queues.
1563 */
1564 uvm_pagedequeue(pg);
1565 uvm_pageunlock(pg);
1566 } else {
1567 KASSERT(!uvmpdpol_pageisqueued_p(pg));
1568 }
1569
1570 /*
1571 * and put on free queue
1572 */
1573
1574 #ifdef DEBUG
1575 pg->uobject = (void *)0xdeadbeef;
1576 pg->uanon = (void *)0xdeadbeef;
1577 if (pg->flags & PG_ZERO)
1578 uvm_pagezerocheck(pg);
1579 #endif /* DEBUG */
1580
1581 /* Try to send the page to the per-CPU cache. */
1582 s = splvm();
1583 ucpu = curcpu()->ci_data.cpu_uvm;
1584 bucket = uvm_page_get_bucket(pg);
1585 if (bucket == ucpu->pgflbucket && uvm_pgflcache_free(ucpu, pg)) {
1586 splx(s);
1587 return;
1588 }
1589
1590 /* Didn't work. Never mind, send it to a global bucket. */
1591 pgfl = &uvm.page_free[uvm_page_get_freelist(pg)];
1592 pgb = pgfl->pgfl_buckets[bucket];
1593 lock = &uvm_freelist_locks[bucket].lock;
1594
1595 mutex_spin_enter(lock);
1596 /* PG_FREE must be set under lock because of uvm_pglistalloc(). */
1597 pg->flags = (pg->flags & PG_ZERO) | PG_FREE;
1598 LIST_INSERT_HEAD(&pgb->pgb_colors[VM_PGCOLOR(pg)], pg, pageq.list);
1599 pgb->pgb_nfree++;
1600 mutex_spin_exit(lock);
1601 splx(s);
1602 }
1603
1604 /*
1605 * uvm_page_unbusy: unbusy an array of pages.
1606 *
1607 * => pages must either all belong to the same object, or all belong to anons.
1608 * => if pages are object-owned, object must be locked.
1609 * => if pages are anon-owned, anons must be locked.
1610 * => caller must make sure that anon-owned pages are not PG_RELEASED.
1611 */
1612
1613 void
1614 uvm_page_unbusy(struct vm_page **pgs, int npgs)
1615 {
1616 struct vm_page *pg;
1617 int i;
1618 UVMHIST_FUNC("uvm_page_unbusy"); UVMHIST_CALLED(ubchist);
1619
1620 for (i = 0; i < npgs; i++) {
1621 pg = pgs[i];
1622 if (pg == NULL || pg == PGO_DONTCARE) {
1623 continue;
1624 }
1625
1626 KASSERT(uvm_page_owner_locked_p(pg));
1627 KASSERT(pg->flags & PG_BUSY);
1628 KASSERT((pg->flags & PG_PAGEOUT) == 0);
1629 if (pg->flags & PG_WANTED) {
1630 /* XXXAD thundering herd problem. */
1631 wakeup(pg);
1632 }
1633 if (pg->flags & PG_RELEASED) {
1634 UVMHIST_LOG(ubchist, "releasing pg %#jx",
1635 (uintptr_t)pg, 0, 0, 0);
1636 KASSERT(pg->uobject != NULL ||
1637 (pg->uanon != NULL && pg->uanon->an_ref > 0));
1638 pg->flags &= ~PG_RELEASED;
1639 uvm_pagefree(pg);
1640 } else {
1641 UVMHIST_LOG(ubchist, "unbusying pg %#jx",
1642 (uintptr_t)pg, 0, 0, 0);
1643 KASSERT((pg->flags & PG_FAKE) == 0);
1644 pg->flags &= ~(PG_WANTED|PG_BUSY);
1645 UVM_PAGE_OWN(pg, NULL);
1646 }
1647 }
1648 }
1649
1650 #if defined(UVM_PAGE_TRKOWN)
1651 /*
1652 * uvm_page_own: set or release page ownership
1653 *
1654 * => this is a debugging function that keeps track of who sets PG_BUSY
1655 * and where they do it. it can be used to track down problems
1656 * such a process setting "PG_BUSY" and never releasing it.
1657 * => page's object [if any] must be locked
1658 * => if "tag" is NULL then we are releasing page ownership
1659 */
1660 void
1661 uvm_page_own(struct vm_page *pg, const char *tag)
1662 {
1663
1664 KASSERT((pg->flags & (PG_PAGEOUT|PG_RELEASED)) == 0);
1665 KASSERT((pg->flags & PG_WANTED) == 0);
1666 KASSERT(uvm_page_owner_locked_p(pg));
1667
1668 /* gain ownership? */
1669 if (tag) {
1670 KASSERT((pg->flags & PG_BUSY) != 0);
1671 if (pg->owner_tag) {
1672 printf("uvm_page_own: page %p already owned "
1673 "by proc %d [%s]\n", pg,
1674 pg->owner, pg->owner_tag);
1675 panic("uvm_page_own");
1676 }
1677 pg->owner = curproc->p_pid;
1678 pg->lowner = curlwp->l_lid;
1679 pg->owner_tag = tag;
1680 return;
1681 }
1682
1683 /* drop ownership */
1684 KASSERT((pg->flags & PG_BUSY) == 0);
1685 if (pg->owner_tag == NULL) {
1686 printf("uvm_page_own: dropping ownership of an non-owned "
1687 "page (%p)\n", pg);
1688 panic("uvm_page_own");
1689 }
1690 pg->owner_tag = NULL;
1691 }
1692 #endif
1693
1694 /*
1695 * uvm_pageidlezero: zero free pages while the system is idle.
1696 */
1697 void
1698 uvm_pageidlezero(void)
1699 {
1700
1701 /*
1702 * Disabled for the moment. Previous strategy too cache heavy. In
1703 * the future we may experiment with zeroing the pages held in the
1704 * per-CPU cache (uvm_pgflcache).
1705 */
1706 }
1707
1708 /*
1709 * uvm_pagelookup: look up a page
1710 *
1711 * => caller should lock object to keep someone from pulling the page
1712 * out from under it
1713 */
1714
1715 struct vm_page *
1716 uvm_pagelookup(struct uvm_object *obj, voff_t off)
1717 {
1718 struct vm_page *pg;
1719
1720 /* No - used from DDB. KASSERT(mutex_owned(obj->vmobjlock)); */
1721
1722 pg = radix_tree_lookup_node(&obj->uo_pages, off >> PAGE_SHIFT);
1723
1724 KASSERT(pg == NULL || obj->uo_npages != 0);
1725 KASSERT(pg == NULL || (pg->flags & (PG_RELEASED|PG_PAGEOUT)) == 0 ||
1726 (pg->flags & PG_BUSY) != 0);
1727 return pg;
1728 }
1729
1730 /*
1731 * uvm_pagewire: wire the page, thus removing it from the daemon's grasp
1732 *
1733 * => caller must lock objects
1734 * => caller must hold pg->interlock
1735 */
1736
1737 void
1738 uvm_pagewire(struct vm_page *pg)
1739 {
1740
1741 KASSERT(uvm_page_owner_locked_p(pg));
1742 KASSERT(mutex_owned(&pg->interlock));
1743 #if defined(READAHEAD_STATS)
1744 if ((pg->flags & PG_READAHEAD) != 0) {
1745 uvm_ra_hit.ev_count++;
1746 pg->flags &= ~PG_READAHEAD;
1747 }
1748 #endif /* defined(READAHEAD_STATS) */
1749 if (pg->wire_count == 0) {
1750 uvm_pagedequeue(pg);
1751 atomic_inc_uint(&uvmexp.wired);
1752 }
1753 pg->wire_count++;
1754 KASSERT(pg->wire_count > 0); /* detect wraparound */
1755 }
1756
1757 /*
1758 * uvm_pageunwire: unwire the page.
1759 *
1760 * => activate if wire count goes to zero.
1761 * => caller must lock objects
1762 * => caller must hold pg->interlock
1763 */
1764
1765 void
1766 uvm_pageunwire(struct vm_page *pg)
1767 {
1768
1769 KASSERT(uvm_page_owner_locked_p(pg));
1770 KASSERT(pg->wire_count != 0);
1771 KASSERT(!uvmpdpol_pageisqueued_p(pg));
1772 KASSERT(mutex_owned(&pg->interlock));
1773 pg->wire_count--;
1774 if (pg->wire_count == 0) {
1775 uvm_pageactivate(pg);
1776 KASSERT(uvmexp.wired != 0);
1777 atomic_dec_uint(&uvmexp.wired);
1778 }
1779 }
1780
1781 /*
1782 * uvm_pagedeactivate: deactivate page
1783 *
1784 * => caller must lock objects
1785 * => caller must check to make sure page is not wired
1786 * => object that page belongs to must be locked (so we can adjust pg->flags)
1787 * => caller must clear the reference on the page before calling
1788 * => caller must hold pg->interlock
1789 */
1790
1791 void
1792 uvm_pagedeactivate(struct vm_page *pg)
1793 {
1794
1795 KASSERT(uvm_page_owner_locked_p(pg));
1796 KASSERT(mutex_owned(&pg->interlock));
1797 if (pg->wire_count == 0) {
1798 KASSERT(uvmpdpol_pageisqueued_p(pg));
1799 uvmpdpol_pagedeactivate(pg);
1800 }
1801 }
1802
1803 /*
1804 * uvm_pageactivate: activate page
1805 *
1806 * => caller must lock objects
1807 * => caller must hold pg->interlock
1808 */
1809
1810 void
1811 uvm_pageactivate(struct vm_page *pg)
1812 {
1813
1814 KASSERT(uvm_page_owner_locked_p(pg));
1815 KASSERT(mutex_owned(&pg->interlock));
1816 #if defined(READAHEAD_STATS)
1817 if ((pg->flags & PG_READAHEAD) != 0) {
1818 uvm_ra_hit.ev_count++;
1819 pg->flags &= ~PG_READAHEAD;
1820 }
1821 #endif /* defined(READAHEAD_STATS) */
1822 if (pg->wire_count == 0) {
1823 uvmpdpol_pageactivate(pg);
1824 }
1825 }
1826
1827 /*
1828 * uvm_pagedequeue: remove a page from any paging queue
1829 *
1830 * => caller must lock objects
1831 * => caller must hold pg->interlock
1832 */
1833 void
1834 uvm_pagedequeue(struct vm_page *pg)
1835 {
1836
1837 KASSERT(uvm_page_owner_locked_p(pg));
1838 KASSERT(mutex_owned(&pg->interlock));
1839 if (uvmpdpol_pageisqueued_p(pg)) {
1840 uvmpdpol_pagedequeue(pg);
1841 }
1842 }
1843
1844 /*
1845 * uvm_pageenqueue: add a page to a paging queue without activating.
1846 * used where a page is not really demanded (yet). eg. read-ahead
1847 *
1848 * => caller must lock objects
1849 * => caller must hold pg->interlock
1850 */
1851 void
1852 uvm_pageenqueue(struct vm_page *pg)
1853 {
1854
1855 KASSERT(uvm_page_owner_locked_p(pg));
1856 KASSERT(mutex_owned(&pg->interlock));
1857 if (pg->wire_count == 0 && !uvmpdpol_pageisqueued_p(pg)) {
1858 uvmpdpol_pageenqueue(pg);
1859 }
1860 }
1861
1862 /*
1863 * uvm_pagelock: acquire page interlock
1864 */
1865 void
1866 uvm_pagelock(struct vm_page *pg)
1867 {
1868
1869 mutex_enter(&pg->interlock);
1870 }
1871
1872 /*
1873 * uvm_pagelock2: acquire two page interlocks
1874 */
1875 void
1876 uvm_pagelock2(struct vm_page *pg1, struct vm_page *pg2)
1877 {
1878
1879 if (pg1 < pg2) {
1880 mutex_enter(&pg1->interlock);
1881 mutex_enter(&pg2->interlock);
1882 } else {
1883 mutex_enter(&pg2->interlock);
1884 mutex_enter(&pg1->interlock);
1885 }
1886 }
1887
1888 /*
1889 * uvm_pageunlock: release page interlock, and if a page replacement intent
1890 * is set on the page, pass it to uvmpdpol to make real.
1891 *
1892 * => caller must hold pg->interlock
1893 */
1894 void
1895 uvm_pageunlock(struct vm_page *pg)
1896 {
1897
1898 if ((pg->pqflags & PQ_INTENT_SET) == 0 ||
1899 (pg->pqflags & PQ_INTENT_QUEUED) != 0) {
1900 mutex_exit(&pg->interlock);
1901 return;
1902 }
1903 pg->pqflags |= PQ_INTENT_QUEUED;
1904 mutex_exit(&pg->interlock);
1905 uvmpdpol_pagerealize(pg);
1906 }
1907
1908 /*
1909 * uvm_pageunlock2: release two page interlocks, and for both pages if a
1910 * page replacement intent is set on the page, pass it to uvmpdpol to make
1911 * real.
1912 *
1913 * => caller must hold pg->interlock
1914 */
1915 void
1916 uvm_pageunlock2(struct vm_page *pg1, struct vm_page *pg2)
1917 {
1918
1919 if ((pg1->pqflags & PQ_INTENT_SET) == 0 ||
1920 (pg1->pqflags & PQ_INTENT_QUEUED) != 0) {
1921 mutex_exit(&pg1->interlock);
1922 pg1 = NULL;
1923 } else {
1924 pg1->pqflags |= PQ_INTENT_QUEUED;
1925 mutex_exit(&pg1->interlock);
1926 }
1927
1928 if ((pg2->pqflags & PQ_INTENT_SET) == 0 ||
1929 (pg2->pqflags & PQ_INTENT_QUEUED) != 0) {
1930 mutex_exit(&pg2->interlock);
1931 pg2 = NULL;
1932 } else {
1933 pg2->pqflags |= PQ_INTENT_QUEUED;
1934 mutex_exit(&pg2->interlock);
1935 }
1936
1937 if (pg1 != NULL) {
1938 uvmpdpol_pagerealize(pg1);
1939 }
1940 if (pg2 != NULL) {
1941 uvmpdpol_pagerealize(pg2);
1942 }
1943 }
1944
1945 /*
1946 * uvm_pagezero: zero fill a page
1947 *
1948 * => if page is part of an object then the object should be locked
1949 * to protect pg->flags.
1950 */
1951
1952 void
1953 uvm_pagezero(struct vm_page *pg)
1954 {
1955 pg->flags &= ~PG_CLEAN;
1956 pmap_zero_page(VM_PAGE_TO_PHYS(pg));
1957 }
1958
1959 /*
1960 * uvm_pagecopy: copy a page
1961 *
1962 * => if page is part of an object then the object should be locked
1963 * to protect pg->flags.
1964 */
1965
1966 void
1967 uvm_pagecopy(struct vm_page *src, struct vm_page *dst)
1968 {
1969
1970 dst->flags &= ~PG_CLEAN;
1971 pmap_copy_page(VM_PAGE_TO_PHYS(src), VM_PAGE_TO_PHYS(dst));
1972 }
1973
1974 /*
1975 * uvm_pageismanaged: test it see that a page (specified by PA) is managed.
1976 */
1977
1978 bool
1979 uvm_pageismanaged(paddr_t pa)
1980 {
1981
1982 return (uvm_physseg_find(atop(pa), NULL) != UVM_PHYSSEG_TYPE_INVALID);
1983 }
1984
1985 /*
1986 * uvm_page_lookup_freelist: look up the free list for the specified page
1987 */
1988
1989 int
1990 uvm_page_lookup_freelist(struct vm_page *pg)
1991 {
1992 uvm_physseg_t upm;
1993
1994 upm = uvm_physseg_find(atop(VM_PAGE_TO_PHYS(pg)), NULL);
1995 KASSERT(upm != UVM_PHYSSEG_TYPE_INVALID);
1996 return uvm_physseg_get_free_list(upm);
1997 }
1998
1999 /*
2000 * uvm_page_owner_locked_p: return true if object associated with page is
2001 * locked. this is a weak check for runtime assertions only.
2002 */
2003
2004 bool
2005 uvm_page_owner_locked_p(struct vm_page *pg)
2006 {
2007
2008 if (pg->uobject != NULL) {
2009 return mutex_owned(pg->uobject->vmobjlock);
2010 }
2011 if (pg->uanon != NULL) {
2012 return mutex_owned(pg->uanon->an_lock);
2013 }
2014 return true;
2015 }
2016
2017 #ifdef PMAP_DIRECT
2018 /*
2019 * Call pmap to translate physical address into a virtual and to run a callback
2020 * for it. Used to avoid actually mapping the pages, pmap most likely uses direct map
2021 * or equivalent.
2022 */
2023 int
2024 uvm_direct_process(struct vm_page **pgs, u_int npages, voff_t off, vsize_t len,
2025 int (*process)(void *, size_t, void *), void *arg)
2026 {
2027 int error = 0;
2028 paddr_t pa;
2029 size_t todo;
2030 voff_t pgoff = (off & PAGE_MASK);
2031 struct vm_page *pg;
2032
2033 KASSERT(npages > 0 && len > 0);
2034
2035 for (int i = 0; i < npages; i++) {
2036 pg = pgs[i];
2037
2038 KASSERT(len > 0);
2039
2040 /*
2041 * Caller is responsible for ensuring all the pages are
2042 * available.
2043 */
2044 KASSERT(pg != NULL && pg != PGO_DONTCARE);
2045
2046 pa = VM_PAGE_TO_PHYS(pg);
2047 todo = MIN(len, PAGE_SIZE - pgoff);
2048
2049 error = pmap_direct_process(pa, pgoff, todo, process, arg);
2050 if (error)
2051 break;
2052
2053 pgoff = 0;
2054 len -= todo;
2055 }
2056
2057 KASSERTMSG(error != 0 || len == 0, "len %lu != 0 for non-error", len);
2058 return error;
2059 }
2060 #endif /* PMAP_DIRECT */
2061
2062 #if defined(DDB) || defined(DEBUGPRINT)
2063
2064 /*
2065 * uvm_page_printit: actually print the page
2066 */
2067
2068 static const char page_flagbits[] = UVM_PGFLAGBITS;
2069
2070 void
2071 uvm_page_printit(struct vm_page *pg, bool full,
2072 void (*pr)(const char *, ...))
2073 {
2074 struct vm_page *tpg;
2075 struct uvm_object *uobj;
2076 struct pgflbucket *pgb;
2077 struct pgflist *pgl;
2078 char pgbuf[128];
2079
2080 (*pr)("PAGE %p:\n", pg);
2081 snprintb(pgbuf, sizeof(pgbuf), page_flagbits, pg->flags);
2082 (*pr)(" flags=%s, pqflags=%x, wire_count=%d, pa=0x%lx\n",
2083 pgbuf, pg->pqflags, pg->wire_count, (long)VM_PAGE_TO_PHYS(pg));
2084 (*pr)(" uobject=%p, uanon=%p, offset=0x%llx loan_count=%d\n",
2085 pg->uobject, pg->uanon, (long long)pg->offset, pg->loan_count);
2086 (*pr)(" bucket=%d freelist=%d\n",
2087 uvm_page_get_bucket(pg), uvm_page_get_freelist(pg));
2088 #if defined(UVM_PAGE_TRKOWN)
2089 if (pg->flags & PG_BUSY)
2090 (*pr)(" owning process = %d, tag=%s\n",
2091 pg->owner, pg->owner_tag);
2092 else
2093 (*pr)(" page not busy, no owner\n");
2094 #else
2095 (*pr)(" [page ownership tracking disabled]\n");
2096 #endif
2097
2098 if (!full)
2099 return;
2100
2101 /* cross-verify object/anon */
2102 if ((pg->flags & PG_FREE) == 0) {
2103 if (pg->flags & PG_ANON) {
2104 if (pg->uanon == NULL || pg->uanon->an_page != pg)
2105 (*pr)(" >>> ANON DOES NOT POINT HERE <<< (%p)\n",
2106 (pg->uanon) ? pg->uanon->an_page : NULL);
2107 else
2108 (*pr)(" anon backpointer is OK\n");
2109 } else {
2110 uobj = pg->uobject;
2111 if (uobj) {
2112 (*pr)(" checking object list\n");
2113 tpg = uvm_pagelookup(uobj, pg->offset);
2114 if (tpg)
2115 (*pr)(" page found on object list\n");
2116 else
2117 (*pr)(" >>> PAGE NOT FOUND ON OBJECT LIST! <<<\n");
2118 }
2119 }
2120 }
2121
2122 /* cross-verify page queue */
2123 if (pg->flags & PG_FREE) {
2124 int fl = uvm_page_get_freelist(pg);
2125 int b = uvm_page_get_bucket(pg);
2126 pgb = uvm.page_free[fl].pgfl_buckets[b];
2127 pgl = &pgb->pgb_colors[VM_PGCOLOR(pg)];
2128 (*pr)(" checking pageq list\n");
2129 LIST_FOREACH(tpg, pgl, pageq.list) {
2130 if (tpg == pg) {
2131 break;
2132 }
2133 }
2134 if (tpg)
2135 (*pr)(" page found on pageq list\n");
2136 else
2137 (*pr)(" >>> PAGE NOT FOUND ON PAGEQ LIST! <<<\n");
2138 }
2139 }
2140
2141 /*
2142 * uvm_page_printall - print a summary of all managed pages
2143 */
2144
2145 void
2146 uvm_page_printall(void (*pr)(const char *, ...))
2147 {
2148 uvm_physseg_t i;
2149 paddr_t pfn;
2150 struct vm_page *pg;
2151
2152 (*pr)("%18s %4s %4s %18s %18s"
2153 #ifdef UVM_PAGE_TRKOWN
2154 " OWNER"
2155 #endif
2156 "\n", "PAGE", "FLAG", "PQ", "UOBJECT", "UANON");
2157 for (i = uvm_physseg_get_first();
2158 uvm_physseg_valid_p(i);
2159 i = uvm_physseg_get_next(i)) {
2160 for (pfn = uvm_physseg_get_start(i);
2161 pfn < uvm_physseg_get_end(i);
2162 pfn++) {
2163 pg = PHYS_TO_VM_PAGE(ptoa(pfn));
2164
2165 (*pr)("%18p %04x %08x %18p %18p",
2166 pg, pg->flags, pg->pqflags, pg->uobject,
2167 pg->uanon);
2168 #ifdef UVM_PAGE_TRKOWN
2169 if (pg->flags & PG_BUSY)
2170 (*pr)(" %d [%s]", pg->owner, pg->owner_tag);
2171 #endif
2172 (*pr)("\n");
2173 }
2174 }
2175 }
2176
2177 /*
2178 * uvm_page_print_freelists - print a summary freelists
2179 */
2180
2181 void
2182 uvm_page_print_freelists(void (*pr)(const char *, ...))
2183 {
2184 struct pgfreelist *pgfl;
2185 struct pgflbucket *pgb;
2186 int fl, b, c;
2187
2188 (*pr)("There are %d freelists with %d buckets of %d colors.\n\n",
2189 VM_NFREELIST, uvm.bucketcount, uvmexp.ncolors);
2190
2191 for (fl = 0; fl < VM_NFREELIST; fl++) {
2192 pgfl = &uvm.page_free[fl];
2193 (*pr)("freelist(%d) @ %p\n", fl, pgfl);
2194 for (b = 0; b < uvm.bucketcount; b++) {
2195 pgb = uvm.page_free[fl].pgfl_buckets[b];
2196 (*pr)(" bucket(%d) @ %p, nfree = %d, lock @ %p:\n",
2197 b, pgb, pgb->pgb_nfree,
2198 &uvm_freelist_locks[b].lock);
2199 for (c = 0; c < uvmexp.ncolors; c++) {
2200 (*pr)(" color(%d) @ %p, ", c,
2201 &pgb->pgb_colors[c]);
2202 (*pr)("first page = %p\n",
2203 LIST_FIRST(&pgb->pgb_colors[c]));
2204 }
2205 }
2206 }
2207 }
2208
2209 #endif /* DDB || DEBUGPRINT */
2210