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