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