1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2002-2006 Rice University
5 * Copyright (c) 2007 Alan L. Cox <alc@cs.rice.edu>
6 * All rights reserved.
7 *
8 * This software was developed for the FreeBSD Project by Alan L. Cox,
9 * Olivier Crameri, Peter Druschel, Sitaram Iyer, and Juan Navarro.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
30 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * Physical memory system implementation
36 *
37 * Any external functions defined by this module are only to be used by the
38 * virtual memory system.
39 */
40
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD: stable/12/sys/vm/vm_phys.c 373252 2023-10-19 14:28:16Z git2svn $");
43
44 #include "opt_ddb.h"
45 #include "opt_vm.h"
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/domainset.h>
50 #include <sys/lock.h>
51 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/mutex.h>
54 #include <sys/proc.h>
55 #include <sys/queue.h>
56 #include <sys/rwlock.h>
57 #include <sys/sbuf.h>
58 #include <sys/sysctl.h>
59 #include <sys/tree.h>
60 #include <sys/vmmeter.h>
61 #include <sys/seq.h>
62
63 #include <ddb/ddb.h>
64
65 #include <vm/vm.h>
66 #include <vm/vm_param.h>
67 #include <vm/vm_kern.h>
68 #include <vm/vm_object.h>
69 #include <vm/vm_page.h>
70 #include <vm/vm_phys.h>
71 #include <vm/vm_pagequeue.h>
72
73 _Static_assert(sizeof(long) * NBBY >= VM_PHYSSEG_MAX,
74 "Too many physsegs.");
75
76 #ifdef NUMA
77 struct mem_affinity __read_mostly *mem_affinity;
78 int __read_mostly *mem_locality;
79
80 static int numa_disabled;
81 static SYSCTL_NODE(_vm, OID_AUTO, numa, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
82 "NUMA options");
83 SYSCTL_INT(_vm_numa, OID_AUTO, disabled, CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
84 &numa_disabled, 0, "NUMA-awareness in the allocators is disabled");
85 #endif
86
87 int __read_mostly vm_ndomains = 1;
88 domainset_t __read_mostly all_domains = DOMAINSET_T_INITIALIZER(0x1);
89
90 struct vm_phys_seg __read_mostly vm_phys_segs[VM_PHYSSEG_MAX];
91 int __read_mostly vm_phys_nsegs;
92
93 struct vm_phys_fictitious_seg;
94 static int vm_phys_fictitious_cmp(struct vm_phys_fictitious_seg *,
95 struct vm_phys_fictitious_seg *);
96
97 RB_HEAD(fict_tree, vm_phys_fictitious_seg) vm_phys_fictitious_tree =
98 RB_INITIALIZER(_vm_phys_fictitious_tree);
99
100 struct vm_phys_fictitious_seg {
101 RB_ENTRY(vm_phys_fictitious_seg) node;
102 /* Memory region data */
103 vm_paddr_t start;
104 vm_paddr_t end;
105 vm_page_t first_page;
106 };
107
108 RB_GENERATE_STATIC(fict_tree, vm_phys_fictitious_seg, node,
109 vm_phys_fictitious_cmp);
110
111 static struct rwlock_padalign vm_phys_fictitious_reg_lock;
112 MALLOC_DEFINE(M_FICT_PAGES, "vm_fictitious", "Fictitious VM pages");
113
114 static struct vm_freelist __aligned(CACHE_LINE_SIZE)
115 vm_phys_free_queues[MAXMEMDOM][VM_NFREELIST][VM_NFREEPOOL]
116 [VM_NFREEORDER_MAX];
117
118 static int __read_mostly vm_nfreelists;
119
120 /*
121 * Provides the mapping from VM_FREELIST_* to free list indices (flind).
122 */
123 static int __read_mostly vm_freelist_to_flind[VM_NFREELIST];
124
125 CTASSERT(VM_FREELIST_DEFAULT == 0);
126
127 #ifdef VM_FREELIST_DMA32
128 #define VM_DMA32_BOUNDARY ((vm_paddr_t)1 << 32)
129 #endif
130
131 /*
132 * Enforce the assumptions made by vm_phys_add_seg() and vm_phys_init() about
133 * the ordering of the free list boundaries.
134 */
135 #if defined(VM_LOWMEM_BOUNDARY) && defined(VM_DMA32_BOUNDARY)
136 CTASSERT(VM_LOWMEM_BOUNDARY < VM_DMA32_BOUNDARY);
137 #endif
138
139 static int sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS);
140 SYSCTL_OID(_vm, OID_AUTO, phys_free,
141 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
142 sysctl_vm_phys_free, "A",
143 "Phys Free Info");
144
145 static int sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS);
146 SYSCTL_OID(_vm, OID_AUTO, phys_segs,
147 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
148 sysctl_vm_phys_segs, "A",
149 "Phys Seg Info");
150
151 #ifdef NUMA
152 static int sysctl_vm_phys_locality(SYSCTL_HANDLER_ARGS);
153 SYSCTL_OID(_vm, OID_AUTO, phys_locality,
154 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
155 sysctl_vm_phys_locality, "A",
156 "Phys Locality Info");
157 #endif
158
159 SYSCTL_INT(_vm, OID_AUTO, ndomains, CTLFLAG_RD,
160 &vm_ndomains, 0, "Number of physical memory domains available.");
161
162 static vm_page_t vm_phys_alloc_seg_contig(struct vm_phys_seg *seg,
163 u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
164 vm_paddr_t boundary);
165 static void _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int domain);
166 static void vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end);
167 static void vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl,
168 int order, int tail);
169
170 /*
171 * Red-black tree helpers for vm fictitious range management.
172 */
173 static inline int
vm_phys_fictitious_in_range(struct vm_phys_fictitious_seg * p,struct vm_phys_fictitious_seg * range)174 vm_phys_fictitious_in_range(struct vm_phys_fictitious_seg *p,
175 struct vm_phys_fictitious_seg *range)
176 {
177
178 KASSERT(range->start != 0 && range->end != 0,
179 ("Invalid range passed on search for vm_fictitious page"));
180 if (p->start >= range->end)
181 return (1);
182 if (p->start < range->start)
183 return (-1);
184
185 return (0);
186 }
187
188 static int
vm_phys_fictitious_cmp(struct vm_phys_fictitious_seg * p1,struct vm_phys_fictitious_seg * p2)189 vm_phys_fictitious_cmp(struct vm_phys_fictitious_seg *p1,
190 struct vm_phys_fictitious_seg *p2)
191 {
192
193 /* Check if this is a search for a page */
194 if (p1->end == 0)
195 return (vm_phys_fictitious_in_range(p1, p2));
196
197 KASSERT(p2->end != 0,
198 ("Invalid range passed as second parameter to vm fictitious comparison"));
199
200 /* Searching to add a new range */
201 if (p1->end <= p2->start)
202 return (-1);
203 if (p1->start >= p2->end)
204 return (1);
205
206 panic("Trying to add overlapping vm fictitious ranges:\n"
207 "[%#jx:%#jx] and [%#jx:%#jx]", (uintmax_t)p1->start,
208 (uintmax_t)p1->end, (uintmax_t)p2->start, (uintmax_t)p2->end);
209 }
210
211 int
vm_phys_domain_match(int prefer,vm_paddr_t low,vm_paddr_t high)212 vm_phys_domain_match(int prefer, vm_paddr_t low, vm_paddr_t high)
213 {
214 #ifdef NUMA
215 domainset_t mask;
216 int i;
217
218 if (vm_ndomains == 1 || mem_affinity == NULL)
219 return (0);
220
221 DOMAINSET_ZERO(&mask);
222 /*
223 * Check for any memory that overlaps low, high.
224 */
225 for (i = 0; mem_affinity[i].end != 0; i++)
226 if (mem_affinity[i].start <= high &&
227 mem_affinity[i].end >= low)
228 DOMAINSET_SET(mem_affinity[i].domain, &mask);
229 if (prefer != -1 && DOMAINSET_ISSET(prefer, &mask))
230 return (prefer);
231 if (DOMAINSET_EMPTY(&mask))
232 panic("vm_phys_domain_match: Impossible constraint");
233 return (DOMAINSET_FFS(&mask) - 1);
234 #else
235 return (0);
236 #endif
237 }
238
239 /*
240 * Outputs the state of the physical memory allocator, specifically,
241 * the amount of physical memory in each free list.
242 */
243 static int
sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS)244 sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS)
245 {
246 struct sbuf sbuf;
247 struct vm_freelist *fl;
248 int dom, error, flind, oind, pind;
249
250 error = sysctl_wire_old_buffer(req, 0);
251 if (error != 0)
252 return (error);
253 sbuf_new_for_sysctl(&sbuf, NULL, 128 * vm_ndomains, req);
254 for (dom = 0; dom < vm_ndomains; dom++) {
255 sbuf_printf(&sbuf,"\nDOMAIN %d:\n", dom);
256 for (flind = 0; flind < vm_nfreelists; flind++) {
257 sbuf_printf(&sbuf, "\nFREE LIST %d:\n"
258 "\n ORDER (SIZE) | NUMBER"
259 "\n ", flind);
260 for (pind = 0; pind < VM_NFREEPOOL; pind++)
261 sbuf_printf(&sbuf, " | POOL %d", pind);
262 sbuf_printf(&sbuf, "\n-- ");
263 for (pind = 0; pind < VM_NFREEPOOL; pind++)
264 sbuf_printf(&sbuf, "-- -- ");
265 sbuf_printf(&sbuf, "--\n");
266 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
267 sbuf_printf(&sbuf, " %2d (%6dK)", oind,
268 1 << (PAGE_SHIFT - 10 + oind));
269 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
270 fl = vm_phys_free_queues[dom][flind][pind];
271 sbuf_printf(&sbuf, " | %6d",
272 fl[oind].lcnt);
273 }
274 sbuf_printf(&sbuf, "\n");
275 }
276 }
277 }
278 error = sbuf_finish(&sbuf);
279 sbuf_delete(&sbuf);
280 return (error);
281 }
282
283 /*
284 * Outputs the set of physical memory segments.
285 */
286 static int
sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS)287 sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS)
288 {
289 struct sbuf sbuf;
290 struct vm_phys_seg *seg;
291 int error, segind;
292
293 error = sysctl_wire_old_buffer(req, 0);
294 if (error != 0)
295 return (error);
296 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
297 for (segind = 0; segind < vm_phys_nsegs; segind++) {
298 sbuf_printf(&sbuf, "\nSEGMENT %d:\n\n", segind);
299 seg = &vm_phys_segs[segind];
300 sbuf_printf(&sbuf, "start: %#jx\n",
301 (uintmax_t)seg->start);
302 sbuf_printf(&sbuf, "end: %#jx\n",
303 (uintmax_t)seg->end);
304 sbuf_printf(&sbuf, "domain: %d\n", seg->domain);
305 sbuf_printf(&sbuf, "free list: %p\n", seg->free_queues);
306 }
307 error = sbuf_finish(&sbuf);
308 sbuf_delete(&sbuf);
309 return (error);
310 }
311
312 /*
313 * Return affinity, or -1 if there's no affinity information.
314 */
315 int
vm_phys_mem_affinity(int f,int t)316 vm_phys_mem_affinity(int f, int t)
317 {
318
319 #ifdef NUMA
320 if (mem_locality == NULL)
321 return (-1);
322 if (f >= vm_ndomains || t >= vm_ndomains)
323 return (-1);
324 return (mem_locality[f * vm_ndomains + t]);
325 #else
326 return (-1);
327 #endif
328 }
329
330 #ifdef NUMA
331 /*
332 * Outputs the VM locality table.
333 */
334 static int
sysctl_vm_phys_locality(SYSCTL_HANDLER_ARGS)335 sysctl_vm_phys_locality(SYSCTL_HANDLER_ARGS)
336 {
337 struct sbuf sbuf;
338 int error, i, j;
339
340 error = sysctl_wire_old_buffer(req, 0);
341 if (error != 0)
342 return (error);
343 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
344
345 sbuf_printf(&sbuf, "\n");
346
347 for (i = 0; i < vm_ndomains; i++) {
348 sbuf_printf(&sbuf, "%d: ", i);
349 for (j = 0; j < vm_ndomains; j++) {
350 sbuf_printf(&sbuf, "%d ", vm_phys_mem_affinity(i, j));
351 }
352 sbuf_printf(&sbuf, "\n");
353 }
354 error = sbuf_finish(&sbuf);
355 sbuf_delete(&sbuf);
356 return (error);
357 }
358 #endif
359
360 static void
vm_freelist_add(struct vm_freelist * fl,vm_page_t m,int order,int tail)361 vm_freelist_add(struct vm_freelist *fl, vm_page_t m, int order, int tail)
362 {
363
364 m->order = order;
365 if (tail)
366 TAILQ_INSERT_TAIL(&fl[order].pl, m, listq);
367 else
368 TAILQ_INSERT_HEAD(&fl[order].pl, m, listq);
369 fl[order].lcnt++;
370 }
371
372 static void
vm_freelist_rem(struct vm_freelist * fl,vm_page_t m,int order)373 vm_freelist_rem(struct vm_freelist *fl, vm_page_t m, int order)
374 {
375
376 TAILQ_REMOVE(&fl[order].pl, m, listq);
377 fl[order].lcnt--;
378 m->order = VM_NFREEORDER;
379 }
380
381 /*
382 * Create a physical memory segment.
383 */
384 static void
_vm_phys_create_seg(vm_paddr_t start,vm_paddr_t end,int domain)385 _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int domain)
386 {
387 struct vm_phys_seg *seg;
388
389 KASSERT(vm_phys_nsegs < VM_PHYSSEG_MAX,
390 ("vm_phys_create_seg: increase VM_PHYSSEG_MAX"));
391 KASSERT(domain >= 0 && domain < vm_ndomains,
392 ("vm_phys_create_seg: invalid domain provided"));
393 seg = &vm_phys_segs[vm_phys_nsegs++];
394 while (seg > vm_phys_segs && (seg - 1)->start >= end) {
395 *seg = *(seg - 1);
396 seg--;
397 }
398 seg->start = start;
399 seg->end = end;
400 seg->domain = domain;
401 }
402
403 static void
vm_phys_create_seg(vm_paddr_t start,vm_paddr_t end)404 vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end)
405 {
406 #ifdef NUMA
407 int i;
408
409 if (mem_affinity == NULL) {
410 _vm_phys_create_seg(start, end, 0);
411 return;
412 }
413
414 for (i = 0;; i++) {
415 if (mem_affinity[i].end == 0)
416 panic("Reached end of affinity info");
417 if (mem_affinity[i].end <= start)
418 continue;
419 if (mem_affinity[i].start > start)
420 panic("No affinity info for start %jx",
421 (uintmax_t)start);
422 if (mem_affinity[i].end >= end) {
423 _vm_phys_create_seg(start, end,
424 mem_affinity[i].domain);
425 break;
426 }
427 _vm_phys_create_seg(start, mem_affinity[i].end,
428 mem_affinity[i].domain);
429 start = mem_affinity[i].end;
430 }
431 #else
432 _vm_phys_create_seg(start, end, 0);
433 #endif
434 }
435
436 /*
437 * Add a physical memory segment.
438 */
439 void
vm_phys_add_seg(vm_paddr_t start,vm_paddr_t end)440 vm_phys_add_seg(vm_paddr_t start, vm_paddr_t end)
441 {
442 vm_paddr_t paddr;
443
444 KASSERT((start & PAGE_MASK) == 0,
445 ("vm_phys_define_seg: start is not page aligned"));
446 KASSERT((end & PAGE_MASK) == 0,
447 ("vm_phys_define_seg: end is not page aligned"));
448
449 /*
450 * Split the physical memory segment if it spans two or more free
451 * list boundaries.
452 */
453 paddr = start;
454 #ifdef VM_FREELIST_LOWMEM
455 if (paddr < VM_LOWMEM_BOUNDARY && end > VM_LOWMEM_BOUNDARY) {
456 vm_phys_create_seg(paddr, VM_LOWMEM_BOUNDARY);
457 paddr = VM_LOWMEM_BOUNDARY;
458 }
459 #endif
460 #ifdef VM_FREELIST_DMA32
461 if (paddr < VM_DMA32_BOUNDARY && end > VM_DMA32_BOUNDARY) {
462 vm_phys_create_seg(paddr, VM_DMA32_BOUNDARY);
463 paddr = VM_DMA32_BOUNDARY;
464 }
465 #endif
466 vm_phys_create_seg(paddr, end);
467 }
468
469 /*
470 * Initialize the physical memory allocator.
471 *
472 * Requires that vm_page_array is initialized!
473 */
474 void
vm_phys_init(void)475 vm_phys_init(void)
476 {
477 struct vm_freelist *fl;
478 struct vm_phys_seg *end_seg, *prev_seg, *seg, *tmp_seg;
479 u_long npages;
480 int dom, flind, freelist, oind, pind, segind;
481
482 /*
483 * Compute the number of free lists, and generate the mapping from the
484 * manifest constants VM_FREELIST_* to the free list indices.
485 *
486 * Initially, the entries of vm_freelist_to_flind[] are set to either
487 * 0 or 1 to indicate which free lists should be created.
488 */
489 npages = 0;
490 for (segind = vm_phys_nsegs - 1; segind >= 0; segind--) {
491 seg = &vm_phys_segs[segind];
492 #ifdef VM_FREELIST_LOWMEM
493 if (seg->end <= VM_LOWMEM_BOUNDARY)
494 vm_freelist_to_flind[VM_FREELIST_LOWMEM] = 1;
495 else
496 #endif
497 #ifdef VM_FREELIST_DMA32
498 if (
499 #ifdef VM_DMA32_NPAGES_THRESHOLD
500 /*
501 * Create the DMA32 free list only if the amount of
502 * physical memory above physical address 4G exceeds the
503 * given threshold.
504 */
505 npages > VM_DMA32_NPAGES_THRESHOLD &&
506 #endif
507 seg->end <= VM_DMA32_BOUNDARY)
508 vm_freelist_to_flind[VM_FREELIST_DMA32] = 1;
509 else
510 #endif
511 {
512 npages += atop(seg->end - seg->start);
513 vm_freelist_to_flind[VM_FREELIST_DEFAULT] = 1;
514 }
515 }
516 /* Change each entry into a running total of the free lists. */
517 for (freelist = 1; freelist < VM_NFREELIST; freelist++) {
518 vm_freelist_to_flind[freelist] +=
519 vm_freelist_to_flind[freelist - 1];
520 }
521 vm_nfreelists = vm_freelist_to_flind[VM_NFREELIST - 1];
522 KASSERT(vm_nfreelists > 0, ("vm_phys_init: no free lists"));
523 /* Change each entry into a free list index. */
524 for (freelist = 0; freelist < VM_NFREELIST; freelist++)
525 vm_freelist_to_flind[freelist]--;
526
527 /*
528 * Initialize the first_page and free_queues fields of each physical
529 * memory segment.
530 */
531 #ifdef VM_PHYSSEG_SPARSE
532 npages = 0;
533 #endif
534 for (segind = 0; segind < vm_phys_nsegs; segind++) {
535 seg = &vm_phys_segs[segind];
536 #ifdef VM_PHYSSEG_SPARSE
537 seg->first_page = &vm_page_array[npages];
538 npages += atop(seg->end - seg->start);
539 #else
540 seg->first_page = PHYS_TO_VM_PAGE(seg->start);
541 #endif
542 #ifdef VM_FREELIST_LOWMEM
543 if (seg->end <= VM_LOWMEM_BOUNDARY) {
544 flind = vm_freelist_to_flind[VM_FREELIST_LOWMEM];
545 KASSERT(flind >= 0,
546 ("vm_phys_init: LOWMEM flind < 0"));
547 } else
548 #endif
549 #ifdef VM_FREELIST_DMA32
550 if (seg->end <= VM_DMA32_BOUNDARY) {
551 flind = vm_freelist_to_flind[VM_FREELIST_DMA32];
552 KASSERT(flind >= 0,
553 ("vm_phys_init: DMA32 flind < 0"));
554 } else
555 #endif
556 {
557 flind = vm_freelist_to_flind[VM_FREELIST_DEFAULT];
558 KASSERT(flind >= 0,
559 ("vm_phys_init: DEFAULT flind < 0"));
560 }
561 seg->free_queues = &vm_phys_free_queues[seg->domain][flind];
562 }
563
564 /*
565 * Coalesce physical memory segments that are contiguous and share the
566 * same per-domain free queues.
567 */
568 prev_seg = vm_phys_segs;
569 seg = &vm_phys_segs[1];
570 end_seg = &vm_phys_segs[vm_phys_nsegs];
571 while (seg < end_seg) {
572 if (prev_seg->end == seg->start &&
573 prev_seg->free_queues == seg->free_queues) {
574 prev_seg->end = seg->end;
575 KASSERT(prev_seg->domain == seg->domain,
576 ("vm_phys_init: free queues cannot span domains"));
577 vm_phys_nsegs--;
578 end_seg--;
579 for (tmp_seg = seg; tmp_seg < end_seg; tmp_seg++)
580 *tmp_seg = *(tmp_seg + 1);
581 } else {
582 prev_seg = seg;
583 seg++;
584 }
585 }
586
587 /*
588 * Initialize the free queues.
589 */
590 for (dom = 0; dom < vm_ndomains; dom++) {
591 for (flind = 0; flind < vm_nfreelists; flind++) {
592 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
593 fl = vm_phys_free_queues[dom][flind][pind];
594 for (oind = 0; oind < VM_NFREEORDER; oind++)
595 TAILQ_INIT(&fl[oind].pl);
596 }
597 }
598 }
599
600 rw_init(&vm_phys_fictitious_reg_lock, "vmfctr");
601 }
602
603 /*
604 * Register info about the NUMA topology of the system.
605 *
606 * Invoked by platform-dependent code prior to vm_phys_init().
607 */
608 void
vm_phys_register_domains(int ndomains,struct mem_affinity * affinity,int * locality)609 vm_phys_register_domains(int ndomains, struct mem_affinity *affinity,
610 int *locality)
611 {
612 #ifdef NUMA
613 int i;
614
615 /*
616 * For now the only override value that we support is 1, which
617 * effectively disables NUMA-awareness in the allocators.
618 */
619 TUNABLE_INT_FETCH("vm.numa.disabled", &numa_disabled);
620 if (numa_disabled)
621 ndomains = 1;
622
623 if (ndomains > 1) {
624 vm_ndomains = ndomains;
625 mem_affinity = affinity;
626 mem_locality = locality;
627 }
628
629 for (i = 0; i < vm_ndomains; i++)
630 DOMAINSET_SET(i, &all_domains);
631 #else
632 (void)ndomains;
633 (void)affinity;
634 (void)locality;
635 #endif
636 }
637
638 int
_vm_phys_domain(vm_paddr_t pa)639 _vm_phys_domain(vm_paddr_t pa)
640 {
641 #ifdef NUMA
642 int i;
643
644 if (vm_ndomains == 1 || mem_affinity == NULL)
645 return (0);
646
647 /*
648 * Check for any memory that overlaps.
649 */
650 for (i = 0; mem_affinity[i].end != 0; i++)
651 if (mem_affinity[i].start <= pa &&
652 mem_affinity[i].end >= pa)
653 return (mem_affinity[i].domain);
654 #endif
655 return (0);
656 }
657
658 /*
659 * Split a contiguous, power of two-sized set of physical pages.
660 *
661 * When this function is called by a page allocation function, the caller
662 * should request insertion at the head unless the order [order, oind) queues
663 * are known to be empty. The objective being to reduce the likelihood of
664 * long-term fragmentation by promoting contemporaneous allocation and
665 * (hopefully) deallocation.
666 */
667 static __inline void
vm_phys_split_pages(vm_page_t m,int oind,struct vm_freelist * fl,int order,int tail)668 vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl, int order,
669 int tail)
670 {
671 vm_page_t m_buddy;
672
673 while (oind > order) {
674 oind--;
675 m_buddy = &m[1 << oind];
676 KASSERT(m_buddy->order == VM_NFREEORDER,
677 ("vm_phys_split_pages: page %p has unexpected order %d",
678 m_buddy, m_buddy->order));
679 vm_freelist_add(fl, m_buddy, oind, tail);
680 }
681 }
682
683 /*
684 * Add the physical pages [m, m + npages) at the end of a power-of-two aligned
685 * and sized set to the specified free list.
686 *
687 * When this function is called by a page allocation function, the caller
688 * should request insertion at the head unless the lower-order queues are
689 * known to be empty. The objective being to reduce the likelihood of long-
690 * term fragmentation by promoting contemporaneous allocation and (hopefully)
691 * deallocation.
692 *
693 * The physical page m's buddy must not be free.
694 */
695 static void
vm_phys_enq_range(vm_page_t m,u_int npages,struct vm_freelist * fl,int tail)696 vm_phys_enq_range(vm_page_t m, u_int npages, struct vm_freelist *fl, int tail)
697 {
698 u_int n;
699 int order;
700
701 KASSERT(npages > 0, ("vm_phys_enq_range: npages is 0"));
702 KASSERT(((VM_PAGE_TO_PHYS(m) + npages * PAGE_SIZE) &
703 ((PAGE_SIZE << (fls(npages) - 1)) - 1)) == 0,
704 ("vm_phys_enq_range: page %p and npages %u are misaligned",
705 m, npages));
706 do {
707 KASSERT(m->order == VM_NFREEORDER,
708 ("vm_phys_enq_range: page %p has unexpected order %d",
709 m, m->order));
710 order = ffs(npages) - 1;
711 KASSERT(order < VM_NFREEORDER,
712 ("vm_phys_enq_range: order %d is out of range", order));
713 vm_freelist_add(fl, m, order, tail);
714 n = 1 << order;
715 m += n;
716 npages -= n;
717 } while (npages > 0);
718 }
719
720 /*
721 * Tries to allocate the specified number of pages from the specified pool
722 * within the specified domain. Returns the actual number of allocated pages
723 * and a pointer to each page through the array ma[].
724 *
725 * The returned pages may not be physically contiguous. However, in contrast
726 * to performing multiple, back-to-back calls to vm_phys_alloc_pages(..., 0),
727 * calling this function once to allocate the desired number of pages will
728 * avoid wasted time in vm_phys_split_pages().
729 *
730 * The free page queues for the specified domain must be locked.
731 */
732 int
vm_phys_alloc_npages(int domain,int pool,int npages,vm_page_t ma[])733 vm_phys_alloc_npages(int domain, int pool, int npages, vm_page_t ma[])
734 {
735 struct vm_freelist *alt, *fl;
736 vm_page_t m;
737 int avail, end, flind, freelist, i, need, oind, pind;
738
739 KASSERT(domain >= 0 && domain < vm_ndomains,
740 ("vm_phys_alloc_npages: domain %d is out of range", domain));
741 KASSERT(pool < VM_NFREEPOOL,
742 ("vm_phys_alloc_npages: pool %d is out of range", pool));
743 KASSERT(npages <= 1 << (VM_NFREEORDER - 1),
744 ("vm_phys_alloc_npages: npages %d is out of range", npages));
745 vm_domain_free_assert_locked(VM_DOMAIN(domain));
746 i = 0;
747 for (freelist = 0; freelist < VM_NFREELIST; freelist++) {
748 flind = vm_freelist_to_flind[freelist];
749 if (flind < 0)
750 continue;
751 fl = vm_phys_free_queues[domain][flind][pool];
752 for (oind = 0; oind < VM_NFREEORDER; oind++) {
753 while ((m = TAILQ_FIRST(&fl[oind].pl)) != NULL) {
754 vm_freelist_rem(fl, m, oind);
755 avail = 1 << oind;
756 need = imin(npages - i, avail);
757 for (end = i + need; i < end;)
758 ma[i++] = m++;
759 if (need < avail) {
760 /*
761 * Return excess pages to fl. Its
762 * order [0, oind) queues are empty.
763 */
764 vm_phys_enq_range(m, avail - need, fl,
765 1);
766 return (npages);
767 } else if (i == npages)
768 return (npages);
769 }
770 }
771 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
772 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
773 alt = vm_phys_free_queues[domain][flind][pind];
774 while ((m = TAILQ_FIRST(&alt[oind].pl)) !=
775 NULL) {
776 vm_freelist_rem(alt, m, oind);
777 vm_phys_set_pool(pool, m, oind);
778 avail = 1 << oind;
779 need = imin(npages - i, avail);
780 for (end = i + need; i < end;)
781 ma[i++] = m++;
782 if (need < avail) {
783 /*
784 * Return excess pages to fl.
785 * Its order [0, oind) queues
786 * are empty.
787 */
788 vm_phys_enq_range(m, avail -
789 need, fl, 1);
790 return (npages);
791 } else if (i == npages)
792 return (npages);
793 }
794 }
795 }
796 }
797 return (i);
798 }
799
800 /*
801 * Allocate a contiguous, power of two-sized set of physical pages
802 * from the free lists.
803 *
804 * The free page queues must be locked.
805 */
806 vm_page_t
vm_phys_alloc_pages(int domain,int pool,int order)807 vm_phys_alloc_pages(int domain, int pool, int order)
808 {
809 vm_page_t m;
810 int freelist;
811
812 for (freelist = 0; freelist < VM_NFREELIST; freelist++) {
813 m = vm_phys_alloc_freelist_pages(domain, freelist, pool, order);
814 if (m != NULL)
815 return (m);
816 }
817 return (NULL);
818 }
819
820 /*
821 * Allocate a contiguous, power of two-sized set of physical pages from the
822 * specified free list. The free list must be specified using one of the
823 * manifest constants VM_FREELIST_*.
824 *
825 * The free page queues must be locked.
826 */
827 vm_page_t
vm_phys_alloc_freelist_pages(int domain,int freelist,int pool,int order)828 vm_phys_alloc_freelist_pages(int domain, int freelist, int pool, int order)
829 {
830 struct vm_freelist *alt, *fl;
831 vm_page_t m;
832 int oind, pind, flind;
833
834 KASSERT(domain >= 0 && domain < vm_ndomains,
835 ("vm_phys_alloc_freelist_pages: domain %d is out of range",
836 domain));
837 KASSERT(freelist < VM_NFREELIST,
838 ("vm_phys_alloc_freelist_pages: freelist %d is out of range",
839 freelist));
840 KASSERT(pool < VM_NFREEPOOL,
841 ("vm_phys_alloc_freelist_pages: pool %d is out of range", pool));
842 KASSERT(order < VM_NFREEORDER,
843 ("vm_phys_alloc_freelist_pages: order %d is out of range", order));
844
845 flind = vm_freelist_to_flind[freelist];
846 /* Check if freelist is present */
847 if (flind < 0)
848 return (NULL);
849
850 vm_domain_free_assert_locked(VM_DOMAIN(domain));
851 fl = &vm_phys_free_queues[domain][flind][pool][0];
852 for (oind = order; oind < VM_NFREEORDER; oind++) {
853 m = TAILQ_FIRST(&fl[oind].pl);
854 if (m != NULL) {
855 vm_freelist_rem(fl, m, oind);
856 /* The order [order, oind) queues are empty. */
857 vm_phys_split_pages(m, oind, fl, order, 1);
858 return (m);
859 }
860 }
861
862 /*
863 * The given pool was empty. Find the largest
864 * contiguous, power-of-two-sized set of pages in any
865 * pool. Transfer these pages to the given pool, and
866 * use them to satisfy the allocation.
867 */
868 for (oind = VM_NFREEORDER - 1; oind >= order; oind--) {
869 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
870 alt = &vm_phys_free_queues[domain][flind][pind][0];
871 m = TAILQ_FIRST(&alt[oind].pl);
872 if (m != NULL) {
873 vm_freelist_rem(alt, m, oind);
874 vm_phys_set_pool(pool, m, oind);
875 /* The order [order, oind) queues are empty. */
876 vm_phys_split_pages(m, oind, fl, order, 1);
877 return (m);
878 }
879 }
880 }
881 return (NULL);
882 }
883
884 /*
885 * Find the vm_page corresponding to the given physical address.
886 */
887 vm_page_t
vm_phys_paddr_to_vm_page(vm_paddr_t pa)888 vm_phys_paddr_to_vm_page(vm_paddr_t pa)
889 {
890 struct vm_phys_seg *seg;
891 int segind;
892
893 for (segind = 0; segind < vm_phys_nsegs; segind++) {
894 seg = &vm_phys_segs[segind];
895 if (pa >= seg->start && pa < seg->end)
896 return (&seg->first_page[atop(pa - seg->start)]);
897 }
898 return (NULL);
899 }
900
901 vm_page_t
vm_phys_fictitious_to_vm_page(vm_paddr_t pa)902 vm_phys_fictitious_to_vm_page(vm_paddr_t pa)
903 {
904 struct vm_phys_fictitious_seg tmp, *seg;
905 vm_page_t m;
906
907 m = NULL;
908 tmp.start = pa;
909 tmp.end = 0;
910
911 rw_rlock(&vm_phys_fictitious_reg_lock);
912 seg = RB_FIND(fict_tree, &vm_phys_fictitious_tree, &tmp);
913 rw_runlock(&vm_phys_fictitious_reg_lock);
914 if (seg == NULL)
915 return (NULL);
916
917 m = &seg->first_page[atop(pa - seg->start)];
918 KASSERT((m->flags & PG_FICTITIOUS) != 0, ("%p not fictitious", m));
919
920 return (m);
921 }
922
923 static inline void
vm_phys_fictitious_init_range(vm_page_t range,vm_paddr_t start,long page_count,vm_memattr_t memattr)924 vm_phys_fictitious_init_range(vm_page_t range, vm_paddr_t start,
925 long page_count, vm_memattr_t memattr)
926 {
927 long i;
928
929 bzero(range, page_count * sizeof(*range));
930 for (i = 0; i < page_count; i++) {
931 vm_page_initfake(&range[i], start + PAGE_SIZE * i, memattr);
932 range[i].oflags &= ~VPO_UNMANAGED;
933 range[i].busy_lock = VPB_UNBUSIED;
934 }
935 }
936
937 int
vm_phys_fictitious_reg_range(vm_paddr_t start,vm_paddr_t end,vm_memattr_t memattr)938 vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
939 vm_memattr_t memattr)
940 {
941 struct vm_phys_fictitious_seg *seg;
942 vm_page_t fp;
943 long page_count;
944 #ifdef VM_PHYSSEG_DENSE
945 long pi, pe;
946 long dpage_count;
947 #endif
948
949 KASSERT(start < end,
950 ("Start of segment isn't less than end (start: %jx end: %jx)",
951 (uintmax_t)start, (uintmax_t)end));
952
953 page_count = (end - start) / PAGE_SIZE;
954
955 #ifdef VM_PHYSSEG_DENSE
956 pi = atop(start);
957 pe = atop(end);
958 if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
959 fp = &vm_page_array[pi - first_page];
960 if ((pe - first_page) > vm_page_array_size) {
961 /*
962 * We have a segment that starts inside
963 * of vm_page_array, but ends outside of it.
964 *
965 * Use vm_page_array pages for those that are
966 * inside of the vm_page_array range, and
967 * allocate the remaining ones.
968 */
969 dpage_count = vm_page_array_size - (pi - first_page);
970 vm_phys_fictitious_init_range(fp, start, dpage_count,
971 memattr);
972 page_count -= dpage_count;
973 start += ptoa(dpage_count);
974 goto alloc;
975 }
976 /*
977 * We can allocate the full range from vm_page_array,
978 * so there's no need to register the range in the tree.
979 */
980 vm_phys_fictitious_init_range(fp, start, page_count, memattr);
981 return (0);
982 } else if (pe > first_page && (pe - first_page) < vm_page_array_size) {
983 /*
984 * We have a segment that ends inside of vm_page_array,
985 * but starts outside of it.
986 */
987 fp = &vm_page_array[0];
988 dpage_count = pe - first_page;
989 vm_phys_fictitious_init_range(fp, ptoa(first_page), dpage_count,
990 memattr);
991 end -= ptoa(dpage_count);
992 page_count -= dpage_count;
993 goto alloc;
994 } else if (pi < first_page && pe > (first_page + vm_page_array_size)) {
995 /*
996 * Trying to register a fictitious range that expands before
997 * and after vm_page_array.
998 */
999 return (EINVAL);
1000 } else {
1001 alloc:
1002 #endif
1003 fp = malloc(page_count * sizeof(struct vm_page), M_FICT_PAGES,
1004 M_WAITOK);
1005 #ifdef VM_PHYSSEG_DENSE
1006 }
1007 #endif
1008 vm_phys_fictitious_init_range(fp, start, page_count, memattr);
1009
1010 seg = malloc(sizeof(*seg), M_FICT_PAGES, M_WAITOK | M_ZERO);
1011 seg->start = start;
1012 seg->end = end;
1013 seg->first_page = fp;
1014
1015 rw_wlock(&vm_phys_fictitious_reg_lock);
1016 RB_INSERT(fict_tree, &vm_phys_fictitious_tree, seg);
1017 rw_wunlock(&vm_phys_fictitious_reg_lock);
1018
1019 return (0);
1020 }
1021
1022 void
vm_phys_fictitious_unreg_range(vm_paddr_t start,vm_paddr_t end)1023 vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end)
1024 {
1025 struct vm_phys_fictitious_seg *seg, tmp;
1026 #ifdef VM_PHYSSEG_DENSE
1027 long pi, pe;
1028 #endif
1029
1030 KASSERT(start < end,
1031 ("Start of segment isn't less than end (start: %jx end: %jx)",
1032 (uintmax_t)start, (uintmax_t)end));
1033
1034 #ifdef VM_PHYSSEG_DENSE
1035 pi = atop(start);
1036 pe = atop(end);
1037 if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
1038 if ((pe - first_page) <= vm_page_array_size) {
1039 /*
1040 * This segment was allocated using vm_page_array
1041 * only, there's nothing to do since those pages
1042 * were never added to the tree.
1043 */
1044 return;
1045 }
1046 /*
1047 * We have a segment that starts inside
1048 * of vm_page_array, but ends outside of it.
1049 *
1050 * Calculate how many pages were added to the
1051 * tree and free them.
1052 */
1053 start = ptoa(first_page + vm_page_array_size);
1054 } else if (pe > first_page && (pe - first_page) < vm_page_array_size) {
1055 /*
1056 * We have a segment that ends inside of vm_page_array,
1057 * but starts outside of it.
1058 */
1059 end = ptoa(first_page);
1060 } else if (pi < first_page && pe > (first_page + vm_page_array_size)) {
1061 /* Since it's not possible to register such a range, panic. */
1062 panic(
1063 "Unregistering not registered fictitious range [%#jx:%#jx]",
1064 (uintmax_t)start, (uintmax_t)end);
1065 }
1066 #endif
1067 tmp.start = start;
1068 tmp.end = 0;
1069
1070 rw_wlock(&vm_phys_fictitious_reg_lock);
1071 seg = RB_FIND(fict_tree, &vm_phys_fictitious_tree, &tmp);
1072 if (seg->start != start || seg->end != end) {
1073 rw_wunlock(&vm_phys_fictitious_reg_lock);
1074 panic(
1075 "Unregistering not registered fictitious range [%#jx:%#jx]",
1076 (uintmax_t)start, (uintmax_t)end);
1077 }
1078 RB_REMOVE(fict_tree, &vm_phys_fictitious_tree, seg);
1079 rw_wunlock(&vm_phys_fictitious_reg_lock);
1080 free(seg->first_page, M_FICT_PAGES);
1081 free(seg, M_FICT_PAGES);
1082 }
1083
1084 /*
1085 * Free a contiguous, power of two-sized set of physical pages.
1086 *
1087 * The free page queues must be locked.
1088 */
1089 void
vm_phys_free_pages(vm_page_t m,int order)1090 vm_phys_free_pages(vm_page_t m, int order)
1091 {
1092 struct vm_freelist *fl;
1093 struct vm_phys_seg *seg;
1094 vm_paddr_t pa;
1095 vm_page_t m_buddy;
1096
1097 KASSERT(m->order == VM_NFREEORDER,
1098 ("vm_phys_free_pages: page %p has unexpected order %d",
1099 m, m->order));
1100 KASSERT(m->pool < VM_NFREEPOOL,
1101 ("vm_phys_free_pages: page %p has unexpected pool %d",
1102 m, m->pool));
1103 KASSERT(order < VM_NFREEORDER,
1104 ("vm_phys_free_pages: order %d is out of range", order));
1105 seg = &vm_phys_segs[m->segind];
1106 vm_domain_free_assert_locked(VM_DOMAIN(seg->domain));
1107 if (order < VM_NFREEORDER - 1) {
1108 pa = VM_PAGE_TO_PHYS(m);
1109 do {
1110 pa ^= ((vm_paddr_t)1 << (PAGE_SHIFT + order));
1111 if (pa < seg->start || pa >= seg->end)
1112 break;
1113 m_buddy = &seg->first_page[atop(pa - seg->start)];
1114 if (m_buddy->order != order)
1115 break;
1116 fl = (*seg->free_queues)[m_buddy->pool];
1117 vm_freelist_rem(fl, m_buddy, order);
1118 if (m_buddy->pool != m->pool)
1119 vm_phys_set_pool(m->pool, m_buddy, order);
1120 order++;
1121 pa &= ~(((vm_paddr_t)1 << (PAGE_SHIFT + order)) - 1);
1122 m = &seg->first_page[atop(pa - seg->start)];
1123 } while (order < VM_NFREEORDER - 1);
1124 }
1125 fl = (*seg->free_queues)[m->pool];
1126 vm_freelist_add(fl, m, order, 1);
1127 }
1128
1129 /*
1130 * Free a contiguous, arbitrarily sized set of physical pages.
1131 *
1132 * The free page queues must be locked.
1133 */
1134 void
vm_phys_free_contig(vm_page_t m,u_long npages)1135 vm_phys_free_contig(vm_page_t m, u_long npages)
1136 {
1137 u_int n;
1138 int order;
1139
1140 /*
1141 * Avoid unnecessary coalescing by freeing the pages in the largest
1142 * possible power-of-two-sized subsets.
1143 */
1144 vm_domain_free_assert_locked(vm_pagequeue_domain(m));
1145 for (;; npages -= n) {
1146 /*
1147 * Unsigned "min" is used here so that "order" is assigned
1148 * "VM_NFREEORDER - 1" when "m"'s physical address is zero
1149 * or the low-order bits of its physical address are zero
1150 * because the size of a physical address exceeds the size of
1151 * a long.
1152 */
1153 order = min(ffsl(VM_PAGE_TO_PHYS(m) >> PAGE_SHIFT) - 1,
1154 VM_NFREEORDER - 1);
1155 n = 1 << order;
1156 if (npages < n)
1157 break;
1158 vm_phys_free_pages(m, order);
1159 m += n;
1160 }
1161 /* The residual "npages" is less than "1 << (VM_NFREEORDER - 1)". */
1162 for (; npages > 0; npages -= n) {
1163 order = flsl(npages) - 1;
1164 n = 1 << order;
1165 vm_phys_free_pages(m, order);
1166 m += n;
1167 }
1168 }
1169
1170 /*
1171 * Scan physical memory between the specified addresses "low" and "high" for a
1172 * run of contiguous physical pages that satisfy the specified conditions, and
1173 * return the lowest page in the run. The specified "alignment" determines
1174 * the alignment of the lowest physical page in the run. If the specified
1175 * "boundary" is non-zero, then the run of physical pages cannot span a
1176 * physical address that is a multiple of "boundary".
1177 *
1178 * "npages" must be greater than zero. Both "alignment" and "boundary" must
1179 * be a power of two.
1180 */
1181 vm_page_t
vm_phys_scan_contig(int domain,u_long npages,vm_paddr_t low,vm_paddr_t high,u_long alignment,vm_paddr_t boundary,int options)1182 vm_phys_scan_contig(int domain, u_long npages, vm_paddr_t low, vm_paddr_t high,
1183 u_long alignment, vm_paddr_t boundary, int options)
1184 {
1185 vm_paddr_t pa_end;
1186 vm_page_t m_end, m_run, m_start;
1187 struct vm_phys_seg *seg;
1188 int segind;
1189
1190 KASSERT(npages > 0, ("npages is 0"));
1191 KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
1192 KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
1193 if (low >= high)
1194 return (NULL);
1195 for (segind = 0; segind < vm_phys_nsegs; segind++) {
1196 seg = &vm_phys_segs[segind];
1197 if (seg->domain != domain)
1198 continue;
1199 if (seg->start >= high)
1200 break;
1201 if (low >= seg->end)
1202 continue;
1203 if (low <= seg->start)
1204 m_start = seg->first_page;
1205 else
1206 m_start = &seg->first_page[atop(low - seg->start)];
1207 if (high < seg->end)
1208 pa_end = high;
1209 else
1210 pa_end = seg->end;
1211 if (pa_end - VM_PAGE_TO_PHYS(m_start) < ptoa(npages))
1212 continue;
1213 m_end = &seg->first_page[atop(pa_end - seg->start)];
1214 m_run = vm_page_scan_contig(npages, m_start, m_end,
1215 alignment, boundary, options);
1216 if (m_run != NULL)
1217 return (m_run);
1218 }
1219 return (NULL);
1220 }
1221
1222 /*
1223 * Set the pool for a contiguous, power of two-sized set of physical pages.
1224 */
1225 void
vm_phys_set_pool(int pool,vm_page_t m,int order)1226 vm_phys_set_pool(int pool, vm_page_t m, int order)
1227 {
1228 vm_page_t m_tmp;
1229
1230 for (m_tmp = m; m_tmp < &m[1 << order]; m_tmp++)
1231 m_tmp->pool = pool;
1232 }
1233
1234 /*
1235 * Search for the given physical page "m" in the free lists. If the search
1236 * succeeds, remove "m" from the free lists and return TRUE. Otherwise, return
1237 * FALSE, indicating that "m" is not in the free lists.
1238 *
1239 * The free page queues must be locked.
1240 */
1241 boolean_t
vm_phys_unfree_page(vm_page_t m)1242 vm_phys_unfree_page(vm_page_t m)
1243 {
1244 struct vm_freelist *fl;
1245 struct vm_phys_seg *seg;
1246 vm_paddr_t pa, pa_half;
1247 vm_page_t m_set, m_tmp;
1248 int order;
1249
1250 /*
1251 * First, find the contiguous, power of two-sized set of free
1252 * physical pages containing the given physical page "m" and
1253 * assign it to "m_set".
1254 */
1255 seg = &vm_phys_segs[m->segind];
1256 vm_domain_free_assert_locked(VM_DOMAIN(seg->domain));
1257 for (m_set = m, order = 0; m_set->order == VM_NFREEORDER &&
1258 order < VM_NFREEORDER - 1; ) {
1259 order++;
1260 pa = m->phys_addr & (~(vm_paddr_t)0 << (PAGE_SHIFT + order));
1261 if (pa >= seg->start)
1262 m_set = &seg->first_page[atop(pa - seg->start)];
1263 else
1264 return (FALSE);
1265 }
1266 if (m_set->order < order)
1267 return (FALSE);
1268 if (m_set->order == VM_NFREEORDER)
1269 return (FALSE);
1270 KASSERT(m_set->order < VM_NFREEORDER,
1271 ("vm_phys_unfree_page: page %p has unexpected order %d",
1272 m_set, m_set->order));
1273
1274 /*
1275 * Next, remove "m_set" from the free lists. Finally, extract
1276 * "m" from "m_set" using an iterative algorithm: While "m_set"
1277 * is larger than a page, shrink "m_set" by returning the half
1278 * of "m_set" that does not contain "m" to the free lists.
1279 */
1280 fl = (*seg->free_queues)[m_set->pool];
1281 order = m_set->order;
1282 vm_freelist_rem(fl, m_set, order);
1283 while (order > 0) {
1284 order--;
1285 pa_half = m_set->phys_addr ^ (1 << (PAGE_SHIFT + order));
1286 if (m->phys_addr < pa_half)
1287 m_tmp = &seg->first_page[atop(pa_half - seg->start)];
1288 else {
1289 m_tmp = m_set;
1290 m_set = &seg->first_page[atop(pa_half - seg->start)];
1291 }
1292 vm_freelist_add(fl, m_tmp, order, 0);
1293 }
1294 KASSERT(m_set == m, ("vm_phys_unfree_page: fatal inconsistency"));
1295 return (TRUE);
1296 }
1297
1298 /*
1299 * Allocate a contiguous set of physical pages of the given size
1300 * "npages" from the free lists. All of the physical pages must be at
1301 * or above the given physical address "low" and below the given
1302 * physical address "high". The given value "alignment" determines the
1303 * alignment of the first physical page in the set. If the given value
1304 * "boundary" is non-zero, then the set of physical pages cannot cross
1305 * any physical address boundary that is a multiple of that value. Both
1306 * "alignment" and "boundary" must be a power of two.
1307 */
1308 vm_page_t
vm_phys_alloc_contig(int domain,u_long npages,vm_paddr_t low,vm_paddr_t high,u_long alignment,vm_paddr_t boundary)1309 vm_phys_alloc_contig(int domain, u_long npages, vm_paddr_t low, vm_paddr_t high,
1310 u_long alignment, vm_paddr_t boundary)
1311 {
1312 vm_paddr_t pa_end, pa_start;
1313 vm_page_t m_run;
1314 struct vm_phys_seg *seg;
1315 int segind;
1316
1317 KASSERT(npages > 0, ("npages is 0"));
1318 KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
1319 KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
1320 vm_domain_free_assert_locked(VM_DOMAIN(domain));
1321 if (low >= high)
1322 return (NULL);
1323 m_run = NULL;
1324 for (segind = vm_phys_nsegs - 1; segind >= 0; segind--) {
1325 seg = &vm_phys_segs[segind];
1326 if (seg->start >= high || seg->domain != domain)
1327 continue;
1328 if (low >= seg->end)
1329 break;
1330 if (low <= seg->start)
1331 pa_start = seg->start;
1332 else
1333 pa_start = low;
1334 if (high < seg->end)
1335 pa_end = high;
1336 else
1337 pa_end = seg->end;
1338 if (pa_end - pa_start < ptoa(npages))
1339 continue;
1340 m_run = vm_phys_alloc_seg_contig(seg, npages, low, high,
1341 alignment, boundary);
1342 if (m_run != NULL)
1343 break;
1344 }
1345 return (m_run);
1346 }
1347
1348 /*
1349 * Allocate a run of contiguous physical pages from the free list for the
1350 * specified segment.
1351 */
1352 static vm_page_t
vm_phys_alloc_seg_contig(struct vm_phys_seg * seg,u_long npages,vm_paddr_t low,vm_paddr_t high,u_long alignment,vm_paddr_t boundary)1353 vm_phys_alloc_seg_contig(struct vm_phys_seg *seg, u_long npages,
1354 vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
1355 {
1356 struct vm_freelist *fl;
1357 vm_paddr_t pa, pa_end, size;
1358 vm_page_t m, m_ret;
1359 u_long npages_end;
1360 int oind, order, pind;
1361
1362 KASSERT(npages > 0, ("npages is 0"));
1363 KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
1364 KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
1365 vm_domain_free_assert_locked(VM_DOMAIN(seg->domain));
1366 /* Compute the queue that is the best fit for npages. */
1367 order = flsl(npages - 1);
1368 /* Search for a run satisfying the specified conditions. */
1369 size = npages << PAGE_SHIFT;
1370 for (oind = min(order, VM_NFREEORDER - 1); oind < VM_NFREEORDER;
1371 oind++) {
1372 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
1373 fl = (*seg->free_queues)[pind];
1374 TAILQ_FOREACH(m_ret, &fl[oind].pl, listq) {
1375 /*
1376 * Is the size of this allocation request
1377 * larger than the largest block size?
1378 */
1379 if (order >= VM_NFREEORDER) {
1380 /*
1381 * Determine if a sufficient number of
1382 * subsequent blocks to satisfy the
1383 * allocation request are free.
1384 */
1385 pa = VM_PAGE_TO_PHYS(m_ret);
1386 pa_end = pa + size;
1387 if (pa_end < pa)
1388 continue;
1389 for (;;) {
1390 pa += 1 << (PAGE_SHIFT +
1391 VM_NFREEORDER - 1);
1392 if (pa >= pa_end ||
1393 pa < seg->start ||
1394 pa >= seg->end)
1395 break;
1396 m = &seg->first_page[atop(pa -
1397 seg->start)];
1398 if (m->order != VM_NFREEORDER -
1399 1)
1400 break;
1401 }
1402 /* If not, go to the next block. */
1403 if (pa < pa_end)
1404 continue;
1405 }
1406
1407 /*
1408 * Determine if the blocks are within the
1409 * given range, satisfy the given alignment,
1410 * and do not cross the given boundary.
1411 */
1412 pa = VM_PAGE_TO_PHYS(m_ret);
1413 pa_end = pa + size;
1414 if (pa >= low && pa_end <= high &&
1415 (pa & (alignment - 1)) == 0 &&
1416 rounddown2(pa ^ (pa_end - 1), boundary) == 0)
1417 goto done;
1418 }
1419 }
1420 }
1421 return (NULL);
1422 done:
1423 for (m = m_ret; m < &m_ret[npages]; m = &m[1 << oind]) {
1424 fl = (*seg->free_queues)[m->pool];
1425 vm_freelist_rem(fl, m, oind);
1426 if (m->pool != VM_FREEPOOL_DEFAULT)
1427 vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m, oind);
1428 }
1429 /* Return excess pages to the free lists. */
1430 npages_end = roundup2(npages, 1 << oind);
1431 if (npages < npages_end) {
1432 fl = (*seg->free_queues)[VM_FREEPOOL_DEFAULT];
1433 vm_phys_enq_range(&m_ret[npages], npages_end - npages, fl, 0);
1434 }
1435 return (m_ret);
1436 }
1437
1438 #ifdef DDB
1439 /*
1440 * Show the number of physical pages in each of the free lists.
1441 */
DB_SHOW_COMMAND(freepages,db_show_freepages)1442 DB_SHOW_COMMAND(freepages, db_show_freepages)
1443 {
1444 struct vm_freelist *fl;
1445 int flind, oind, pind, dom;
1446
1447 for (dom = 0; dom < vm_ndomains; dom++) {
1448 db_printf("DOMAIN: %d\n", dom);
1449 for (flind = 0; flind < vm_nfreelists; flind++) {
1450 db_printf("FREE LIST %d:\n"
1451 "\n ORDER (SIZE) | NUMBER"
1452 "\n ", flind);
1453 for (pind = 0; pind < VM_NFREEPOOL; pind++)
1454 db_printf(" | POOL %d", pind);
1455 db_printf("\n-- ");
1456 for (pind = 0; pind < VM_NFREEPOOL; pind++)
1457 db_printf("-- -- ");
1458 db_printf("--\n");
1459 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
1460 db_printf(" %2.2d (%6.6dK)", oind,
1461 1 << (PAGE_SHIFT - 10 + oind));
1462 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
1463 fl = vm_phys_free_queues[dom][flind][pind];
1464 db_printf(" | %6.6d", fl[oind].lcnt);
1465 }
1466 db_printf("\n");
1467 }
1468 db_printf("\n");
1469 }
1470 db_printf("\n");
1471 }
1472 }
1473 #endif
1474