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