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