2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002-2006 Rice University
5 * Copyright (c) 2007 Alan L. Cox <alc@cs.rice.edu>
8 * This software was developed for the FreeBSD Project by Alan L. Cox,
9 * Olivier Crameri, Peter Druschel, Sitaram Iyer, and Juan Navarro.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
35 * Physical memory system implementation
37 * Any external functions defined by this module are only to be used by the
38 * virtual memory system.
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/domainset.h>
51 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/mutex.h>
55 #include <sys/queue.h>
56 #include <sys/rwlock.h>
58 #include <sys/sysctl.h>
60 #include <sys/vmmeter.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>
73 _Static_assert(sizeof(long) * NBBY >= VM_PHYSSEG_MAX,
74 "Too many physsegs.");
77 struct mem_affinity __read_mostly *mem_affinity;
78 int __read_mostly *mem_locality;
81 int __read_mostly vm_ndomains = 1;
82 domainset_t __read_mostly all_domains = DOMAINSET_T_INITIALIZER(0x1);
84 struct vm_phys_seg __read_mostly vm_phys_segs[VM_PHYSSEG_MAX];
85 int __read_mostly vm_phys_nsegs;
87 struct vm_phys_fictitious_seg;
88 static int vm_phys_fictitious_cmp(struct vm_phys_fictitious_seg *,
89 struct vm_phys_fictitious_seg *);
91 RB_HEAD(fict_tree, vm_phys_fictitious_seg) vm_phys_fictitious_tree =
92 RB_INITIALIZER(_vm_phys_fictitious_tree);
94 struct vm_phys_fictitious_seg {
95 RB_ENTRY(vm_phys_fictitious_seg) node;
96 /* Memory region data */
102 RB_GENERATE_STATIC(fict_tree, vm_phys_fictitious_seg, node,
103 vm_phys_fictitious_cmp);
105 static struct rwlock_padalign vm_phys_fictitious_reg_lock;
106 MALLOC_DEFINE(M_FICT_PAGES, "vm_fictitious", "Fictitious VM pages");
108 static struct vm_freelist __aligned(CACHE_LINE_SIZE)
109 vm_phys_free_queues[MAXMEMDOM][VM_NFREELIST][VM_NFREEPOOL]
112 static int __read_mostly vm_nfreelists;
115 * Provides the mapping from VM_FREELIST_* to free list indices (flind).
117 static int __read_mostly vm_freelist_to_flind[VM_NFREELIST];
119 CTASSERT(VM_FREELIST_DEFAULT == 0);
121 #ifdef VM_FREELIST_DMA32
122 #define VM_DMA32_BOUNDARY ((vm_paddr_t)1 << 32)
126 * Enforce the assumptions made by vm_phys_add_seg() and vm_phys_init() about
127 * the ordering of the free list boundaries.
129 #if defined(VM_LOWMEM_BOUNDARY) && defined(VM_DMA32_BOUNDARY)
130 CTASSERT(VM_LOWMEM_BOUNDARY < VM_DMA32_BOUNDARY);
133 static int sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS);
134 SYSCTL_OID(_vm, OID_AUTO, phys_free, CTLTYPE_STRING | CTLFLAG_RD,
135 NULL, 0, sysctl_vm_phys_free, "A", "Phys Free Info");
137 static int sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS);
138 SYSCTL_OID(_vm, OID_AUTO, phys_segs, CTLTYPE_STRING | CTLFLAG_RD,
139 NULL, 0, sysctl_vm_phys_segs, "A", "Phys Seg Info");
142 static int sysctl_vm_phys_locality(SYSCTL_HANDLER_ARGS);
143 SYSCTL_OID(_vm, OID_AUTO, phys_locality, CTLTYPE_STRING | CTLFLAG_RD,
144 NULL, 0, sysctl_vm_phys_locality, "A", "Phys Locality Info");
147 SYSCTL_INT(_vm, OID_AUTO, ndomains, CTLFLAG_RD,
148 &vm_ndomains, 0, "Number of physical memory domains available.");
150 static vm_page_t vm_phys_alloc_seg_contig(struct vm_phys_seg *seg,
151 u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
152 vm_paddr_t boundary);
153 static void _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int domain);
154 static void vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end);
155 static void vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl,
156 int order, int tail);
159 * Red-black tree helpers for vm fictitious range management.
162 vm_phys_fictitious_in_range(struct vm_phys_fictitious_seg *p,
163 struct vm_phys_fictitious_seg *range)
166 KASSERT(range->start != 0 && range->end != 0,
167 ("Invalid range passed on search for vm_fictitious page"));
168 if (p->start >= range->end)
170 if (p->start < range->start)
177 vm_phys_fictitious_cmp(struct vm_phys_fictitious_seg *p1,
178 struct vm_phys_fictitious_seg *p2)
181 /* Check if this is a search for a page */
183 return (vm_phys_fictitious_in_range(p1, p2));
185 KASSERT(p2->end != 0,
186 ("Invalid range passed as second parameter to vm fictitious comparison"));
188 /* Searching to add a new range */
189 if (p1->end <= p2->start)
191 if (p1->start >= p2->end)
194 panic("Trying to add overlapping vm fictitious ranges:\n"
195 "[%#jx:%#jx] and [%#jx:%#jx]", (uintmax_t)p1->start,
196 (uintmax_t)p1->end, (uintmax_t)p2->start, (uintmax_t)p2->end);
200 vm_phys_domain_match(int prefer, vm_paddr_t low, vm_paddr_t high)
206 if (vm_ndomains == 1 || mem_affinity == NULL)
209 DOMAINSET_ZERO(&mask);
211 * Check for any memory that overlaps low, high.
213 for (i = 0; mem_affinity[i].end != 0; i++)
214 if (mem_affinity[i].start <= high &&
215 mem_affinity[i].end >= low)
216 DOMAINSET_SET(mem_affinity[i].domain, &mask);
217 if (prefer != -1 && DOMAINSET_ISSET(prefer, &mask))
219 if (DOMAINSET_EMPTY(&mask))
220 panic("vm_phys_domain_match: Impossible constraint");
221 return (DOMAINSET_FFS(&mask) - 1);
228 * Outputs the state of the physical memory allocator, specifically,
229 * the amount of physical memory in each free list.
232 sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS)
235 struct vm_freelist *fl;
236 int dom, error, flind, oind, pind;
238 error = sysctl_wire_old_buffer(req, 0);
241 sbuf_new_for_sysctl(&sbuf, NULL, 128 * vm_ndomains, req);
242 for (dom = 0; dom < vm_ndomains; dom++) {
243 sbuf_printf(&sbuf,"\nDOMAIN %d:\n", dom);
244 for (flind = 0; flind < vm_nfreelists; flind++) {
245 sbuf_printf(&sbuf, "\nFREE LIST %d:\n"
246 "\n ORDER (SIZE) | NUMBER"
248 for (pind = 0; pind < VM_NFREEPOOL; pind++)
249 sbuf_printf(&sbuf, " | POOL %d", pind);
250 sbuf_printf(&sbuf, "\n-- ");
251 for (pind = 0; pind < VM_NFREEPOOL; pind++)
252 sbuf_printf(&sbuf, "-- -- ");
253 sbuf_printf(&sbuf, "--\n");
254 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
255 sbuf_printf(&sbuf, " %2d (%6dK)", oind,
256 1 << (PAGE_SHIFT - 10 + oind));
257 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
258 fl = vm_phys_free_queues[dom][flind][pind];
259 sbuf_printf(&sbuf, " | %6d",
262 sbuf_printf(&sbuf, "\n");
266 error = sbuf_finish(&sbuf);
272 * Outputs the set of physical memory segments.
275 sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS)
278 struct vm_phys_seg *seg;
281 error = sysctl_wire_old_buffer(req, 0);
284 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
285 for (segind = 0; segind < vm_phys_nsegs; segind++) {
286 sbuf_printf(&sbuf, "\nSEGMENT %d:\n\n", segind);
287 seg = &vm_phys_segs[segind];
288 sbuf_printf(&sbuf, "start: %#jx\n",
289 (uintmax_t)seg->start);
290 sbuf_printf(&sbuf, "end: %#jx\n",
291 (uintmax_t)seg->end);
292 sbuf_printf(&sbuf, "domain: %d\n", seg->domain);
293 sbuf_printf(&sbuf, "free list: %p\n", seg->free_queues);
295 error = sbuf_finish(&sbuf);
301 * Return affinity, or -1 if there's no affinity information.
304 vm_phys_mem_affinity(int f, int t)
308 if (mem_locality == NULL)
310 if (f >= vm_ndomains || t >= vm_ndomains)
312 return (mem_locality[f * vm_ndomains + t]);
320 * Outputs the VM locality table.
323 sysctl_vm_phys_locality(SYSCTL_HANDLER_ARGS)
328 error = sysctl_wire_old_buffer(req, 0);
331 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
333 sbuf_printf(&sbuf, "\n");
335 for (i = 0; i < vm_ndomains; i++) {
336 sbuf_printf(&sbuf, "%d: ", i);
337 for (j = 0; j < vm_ndomains; j++) {
338 sbuf_printf(&sbuf, "%d ", vm_phys_mem_affinity(i, j));
340 sbuf_printf(&sbuf, "\n");
342 error = sbuf_finish(&sbuf);
349 vm_freelist_add(struct vm_freelist *fl, vm_page_t m, int order, int tail)
354 TAILQ_INSERT_TAIL(&fl[order].pl, m, listq);
356 TAILQ_INSERT_HEAD(&fl[order].pl, m, listq);
361 vm_freelist_rem(struct vm_freelist *fl, vm_page_t m, int order)
364 TAILQ_REMOVE(&fl[order].pl, m, listq);
366 m->order = VM_NFREEORDER;
370 * Create a physical memory segment.
373 _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int domain)
375 struct vm_phys_seg *seg;
377 KASSERT(vm_phys_nsegs < VM_PHYSSEG_MAX,
378 ("vm_phys_create_seg: increase VM_PHYSSEG_MAX"));
379 KASSERT(domain >= 0 && domain < vm_ndomains,
380 ("vm_phys_create_seg: invalid domain provided"));
381 seg = &vm_phys_segs[vm_phys_nsegs++];
382 while (seg > vm_phys_segs && (seg - 1)->start >= end) {
388 seg->domain = domain;
392 vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end)
397 if (mem_affinity == NULL) {
398 _vm_phys_create_seg(start, end, 0);
403 if (mem_affinity[i].end == 0)
404 panic("Reached end of affinity info");
405 if (mem_affinity[i].end <= start)
407 if (mem_affinity[i].start > start)
408 panic("No affinity info for start %jx",
410 if (mem_affinity[i].end >= end) {
411 _vm_phys_create_seg(start, end,
412 mem_affinity[i].domain);
415 _vm_phys_create_seg(start, mem_affinity[i].end,
416 mem_affinity[i].domain);
417 start = mem_affinity[i].end;
420 _vm_phys_create_seg(start, end, 0);
425 * Add a physical memory segment.
428 vm_phys_add_seg(vm_paddr_t start, vm_paddr_t end)
432 KASSERT((start & PAGE_MASK) == 0,
433 ("vm_phys_define_seg: start is not page aligned"));
434 KASSERT((end & PAGE_MASK) == 0,
435 ("vm_phys_define_seg: end is not page aligned"));
438 * Split the physical memory segment if it spans two or more free
442 #ifdef VM_FREELIST_LOWMEM
443 if (paddr < VM_LOWMEM_BOUNDARY && end > VM_LOWMEM_BOUNDARY) {
444 vm_phys_create_seg(paddr, VM_LOWMEM_BOUNDARY);
445 paddr = VM_LOWMEM_BOUNDARY;
448 #ifdef VM_FREELIST_DMA32
449 if (paddr < VM_DMA32_BOUNDARY && end > VM_DMA32_BOUNDARY) {
450 vm_phys_create_seg(paddr, VM_DMA32_BOUNDARY);
451 paddr = VM_DMA32_BOUNDARY;
454 vm_phys_create_seg(paddr, end);
458 * Initialize the physical memory allocator.
460 * Requires that vm_page_array is initialized!
465 struct vm_freelist *fl;
466 struct vm_phys_seg *end_seg, *prev_seg, *seg, *tmp_seg;
468 int dom, flind, freelist, oind, pind, segind;
471 * Compute the number of free lists, and generate the mapping from the
472 * manifest constants VM_FREELIST_* to the free list indices.
474 * Initially, the entries of vm_freelist_to_flind[] are set to either
475 * 0 or 1 to indicate which free lists should be created.
478 for (segind = vm_phys_nsegs - 1; segind >= 0; segind--) {
479 seg = &vm_phys_segs[segind];
480 #ifdef VM_FREELIST_LOWMEM
481 if (seg->end <= VM_LOWMEM_BOUNDARY)
482 vm_freelist_to_flind[VM_FREELIST_LOWMEM] = 1;
485 #ifdef VM_FREELIST_DMA32
487 #ifdef VM_DMA32_NPAGES_THRESHOLD
489 * Create the DMA32 free list only if the amount of
490 * physical memory above physical address 4G exceeds the
493 npages > VM_DMA32_NPAGES_THRESHOLD &&
495 seg->end <= VM_DMA32_BOUNDARY)
496 vm_freelist_to_flind[VM_FREELIST_DMA32] = 1;
500 npages += atop(seg->end - seg->start);
501 vm_freelist_to_flind[VM_FREELIST_DEFAULT] = 1;
504 /* Change each entry into a running total of the free lists. */
505 for (freelist = 1; freelist < VM_NFREELIST; freelist++) {
506 vm_freelist_to_flind[freelist] +=
507 vm_freelist_to_flind[freelist - 1];
509 vm_nfreelists = vm_freelist_to_flind[VM_NFREELIST - 1];
510 KASSERT(vm_nfreelists > 0, ("vm_phys_init: no free lists"));
511 /* Change each entry into a free list index. */
512 for (freelist = 0; freelist < VM_NFREELIST; freelist++)
513 vm_freelist_to_flind[freelist]--;
516 * Initialize the first_page and free_queues fields of each physical
519 #ifdef VM_PHYSSEG_SPARSE
522 for (segind = 0; segind < vm_phys_nsegs; segind++) {
523 seg = &vm_phys_segs[segind];
524 #ifdef VM_PHYSSEG_SPARSE
525 seg->first_page = &vm_page_array[npages];
526 npages += atop(seg->end - seg->start);
528 seg->first_page = PHYS_TO_VM_PAGE(seg->start);
530 #ifdef VM_FREELIST_LOWMEM
531 if (seg->end <= VM_LOWMEM_BOUNDARY) {
532 flind = vm_freelist_to_flind[VM_FREELIST_LOWMEM];
534 ("vm_phys_init: LOWMEM flind < 0"));
537 #ifdef VM_FREELIST_DMA32
538 if (seg->end <= VM_DMA32_BOUNDARY) {
539 flind = vm_freelist_to_flind[VM_FREELIST_DMA32];
541 ("vm_phys_init: DMA32 flind < 0"));
545 flind = vm_freelist_to_flind[VM_FREELIST_DEFAULT];
547 ("vm_phys_init: DEFAULT flind < 0"));
549 seg->free_queues = &vm_phys_free_queues[seg->domain][flind];
553 * Coalesce physical memory segments that are contiguous and share the
554 * same per-domain free queues.
556 prev_seg = vm_phys_segs;
557 seg = &vm_phys_segs[1];
558 end_seg = &vm_phys_segs[vm_phys_nsegs];
559 while (seg < end_seg) {
560 if (prev_seg->end == seg->start &&
561 prev_seg->free_queues == seg->free_queues) {
562 prev_seg->end = seg->end;
563 KASSERT(prev_seg->domain == seg->domain,
564 ("vm_phys_init: free queues cannot span domains"));
567 for (tmp_seg = seg; tmp_seg < end_seg; tmp_seg++)
568 *tmp_seg = *(tmp_seg + 1);
576 * Initialize the free queues.
578 for (dom = 0; dom < vm_ndomains; dom++) {
579 for (flind = 0; flind < vm_nfreelists; flind++) {
580 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
581 fl = vm_phys_free_queues[dom][flind][pind];
582 for (oind = 0; oind < VM_NFREEORDER; oind++)
583 TAILQ_INIT(&fl[oind].pl);
588 rw_init(&vm_phys_fictitious_reg_lock, "vmfctr");
592 * Register info about the NUMA topology of the system.
594 * Invoked by platform-dependent code prior to vm_phys_init().
597 vm_phys_register_domains(int ndomains, struct mem_affinity *affinity,
604 * For now the only override value that we support is 1, which
605 * effectively disables NUMA-awareness in the allocators.
608 TUNABLE_INT_FETCH("vm.numa.disabled", &d);
613 vm_ndomains = ndomains;
614 mem_affinity = affinity;
615 mem_locality = locality;
618 for (i = 0; i < vm_ndomains; i++)
619 DOMAINSET_SET(i, &all_domains);
628 * Split a contiguous, power of two-sized set of physical pages.
630 * When this function is called by a page allocation function, the caller
631 * should request insertion at the head unless the order [order, oind) queues
632 * are known to be empty. The objective being to reduce the likelihood of
633 * long-term fragmentation by promoting contemporaneous allocation and
634 * (hopefully) deallocation.
637 vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl, int order,
642 while (oind > order) {
644 m_buddy = &m[1 << oind];
645 KASSERT(m_buddy->order == VM_NFREEORDER,
646 ("vm_phys_split_pages: page %p has unexpected order %d",
647 m_buddy, m_buddy->order));
648 vm_freelist_add(fl, m_buddy, oind, tail);
653 * Add the physical pages [m, m + npages) at the end of a power-of-two aligned
654 * and sized set to the specified free list.
656 * When this function is called by a page allocation function, the caller
657 * should request insertion at the head unless the lower-order queues are
658 * known to be empty. The objective being to reduce the likelihood of long-
659 * term fragmentation by promoting contemporaneous allocation and (hopefully)
662 * The physical page m's buddy must not be free.
665 vm_phys_enq_range(vm_page_t m, u_int npages, struct vm_freelist *fl, int tail)
670 KASSERT(npages > 0, ("vm_phys_enq_range: npages is 0"));
671 KASSERT(((VM_PAGE_TO_PHYS(m) + npages * PAGE_SIZE) &
672 ((PAGE_SIZE << (fls(npages) - 1)) - 1)) == 0,
673 ("vm_phys_enq_range: page %p and npages %u are misaligned",
676 KASSERT(m->order == VM_NFREEORDER,
677 ("vm_phys_enq_range: page %p has unexpected order %d",
679 order = ffs(npages) - 1;
680 KASSERT(order < VM_NFREEORDER,
681 ("vm_phys_enq_range: order %d is out of range", order));
682 vm_freelist_add(fl, m, order, tail);
686 } while (npages > 0);
690 * Tries to allocate the specified number of pages from the specified pool
691 * within the specified domain. Returns the actual number of allocated pages
692 * and a pointer to each page through the array ma[].
694 * The returned pages may not be physically contiguous. However, in contrast
695 * to performing multiple, back-to-back calls to vm_phys_alloc_pages(..., 0),
696 * calling this function once to allocate the desired number of pages will
697 * avoid wasted time in vm_phys_split_pages().
699 * The free page queues for the specified domain must be locked.
702 vm_phys_alloc_npages(int domain, int pool, int npages, vm_page_t ma[])
704 struct vm_freelist *alt, *fl;
706 int avail, end, flind, freelist, i, need, oind, pind;
708 KASSERT(domain >= 0 && domain < vm_ndomains,
709 ("vm_phys_alloc_npages: domain %d is out of range", domain));
710 KASSERT(pool < VM_NFREEPOOL,
711 ("vm_phys_alloc_npages: pool %d is out of range", pool));
712 KASSERT(npages <= 1 << (VM_NFREEORDER - 1),
713 ("vm_phys_alloc_npages: npages %d is out of range", npages));
714 vm_domain_free_assert_locked(VM_DOMAIN(domain));
716 for (freelist = 0; freelist < VM_NFREELIST; freelist++) {
717 flind = vm_freelist_to_flind[freelist];
720 fl = vm_phys_free_queues[domain][flind][pool];
721 for (oind = 0; oind < VM_NFREEORDER; oind++) {
722 while ((m = TAILQ_FIRST(&fl[oind].pl)) != NULL) {
723 vm_freelist_rem(fl, m, oind);
725 need = imin(npages - i, avail);
726 for (end = i + need; i < end;)
730 * Return excess pages to fl. Its
731 * order [0, oind) queues are empty.
733 vm_phys_enq_range(m, avail - need, fl,
736 } else if (i == npages)
740 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
741 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
742 alt = vm_phys_free_queues[domain][flind][pind];
743 while ((m = TAILQ_FIRST(&alt[oind].pl)) !=
745 vm_freelist_rem(alt, m, oind);
746 vm_phys_set_pool(pool, m, oind);
748 need = imin(npages - i, avail);
749 for (end = i + need; i < end;)
753 * Return excess pages to fl.
754 * Its order [0, oind) queues
757 vm_phys_enq_range(m, avail -
760 } else if (i == npages)
770 * Allocate a contiguous, power of two-sized set of physical pages
771 * from the free lists.
773 * The free page queues must be locked.
776 vm_phys_alloc_pages(int domain, int pool, int order)
781 for (freelist = 0; freelist < VM_NFREELIST; freelist++) {
782 m = vm_phys_alloc_freelist_pages(domain, freelist, pool, order);
790 * Allocate a contiguous, power of two-sized set of physical pages from the
791 * specified free list. The free list must be specified using one of the
792 * manifest constants VM_FREELIST_*.
794 * The free page queues must be locked.
797 vm_phys_alloc_freelist_pages(int domain, int freelist, int pool, int order)
799 struct vm_freelist *alt, *fl;
801 int oind, pind, flind;
803 KASSERT(domain >= 0 && domain < vm_ndomains,
804 ("vm_phys_alloc_freelist_pages: domain %d is out of range",
806 KASSERT(freelist < VM_NFREELIST,
807 ("vm_phys_alloc_freelist_pages: freelist %d is out of range",
809 KASSERT(pool < VM_NFREEPOOL,
810 ("vm_phys_alloc_freelist_pages: pool %d is out of range", pool));
811 KASSERT(order < VM_NFREEORDER,
812 ("vm_phys_alloc_freelist_pages: order %d is out of range", order));
814 flind = vm_freelist_to_flind[freelist];
815 /* Check if freelist is present */
819 vm_domain_free_assert_locked(VM_DOMAIN(domain));
820 fl = &vm_phys_free_queues[domain][flind][pool][0];
821 for (oind = order; oind < VM_NFREEORDER; oind++) {
822 m = TAILQ_FIRST(&fl[oind].pl);
824 vm_freelist_rem(fl, m, oind);
825 /* The order [order, oind) queues are empty. */
826 vm_phys_split_pages(m, oind, fl, order, 1);
832 * The given pool was empty. Find the largest
833 * contiguous, power-of-two-sized set of pages in any
834 * pool. Transfer these pages to the given pool, and
835 * use them to satisfy the allocation.
837 for (oind = VM_NFREEORDER - 1; oind >= order; oind--) {
838 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
839 alt = &vm_phys_free_queues[domain][flind][pind][0];
840 m = TAILQ_FIRST(&alt[oind].pl);
842 vm_freelist_rem(alt, m, oind);
843 vm_phys_set_pool(pool, m, oind);
844 /* The order [order, oind) queues are empty. */
845 vm_phys_split_pages(m, oind, fl, order, 1);
854 * Find the vm_page corresponding to the given physical address.
857 vm_phys_paddr_to_vm_page(vm_paddr_t pa)
859 struct vm_phys_seg *seg;
862 for (segind = 0; segind < vm_phys_nsegs; segind++) {
863 seg = &vm_phys_segs[segind];
864 if (pa >= seg->start && pa < seg->end)
865 return (&seg->first_page[atop(pa - seg->start)]);
871 vm_phys_fictitious_to_vm_page(vm_paddr_t pa)
873 struct vm_phys_fictitious_seg tmp, *seg;
880 rw_rlock(&vm_phys_fictitious_reg_lock);
881 seg = RB_FIND(fict_tree, &vm_phys_fictitious_tree, &tmp);
882 rw_runlock(&vm_phys_fictitious_reg_lock);
886 m = &seg->first_page[atop(pa - seg->start)];
887 KASSERT((m->flags & PG_FICTITIOUS) != 0, ("%p not fictitious", m));
893 vm_phys_fictitious_init_range(vm_page_t range, vm_paddr_t start,
894 long page_count, vm_memattr_t memattr)
898 bzero(range, page_count * sizeof(*range));
899 for (i = 0; i < page_count; i++) {
900 vm_page_initfake(&range[i], start + PAGE_SIZE * i, memattr);
901 range[i].oflags &= ~VPO_UNMANAGED;
902 range[i].busy_lock = VPB_UNBUSIED;
907 vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
908 vm_memattr_t memattr)
910 struct vm_phys_fictitious_seg *seg;
913 #ifdef VM_PHYSSEG_DENSE
919 ("Start of segment isn't less than end (start: %jx end: %jx)",
920 (uintmax_t)start, (uintmax_t)end));
922 page_count = (end - start) / PAGE_SIZE;
924 #ifdef VM_PHYSSEG_DENSE
927 if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
928 fp = &vm_page_array[pi - first_page];
929 if ((pe - first_page) > vm_page_array_size) {
931 * We have a segment that starts inside
932 * of vm_page_array, but ends outside of it.
934 * Use vm_page_array pages for those that are
935 * inside of the vm_page_array range, and
936 * allocate the remaining ones.
938 dpage_count = vm_page_array_size - (pi - first_page);
939 vm_phys_fictitious_init_range(fp, start, dpage_count,
941 page_count -= dpage_count;
942 start += ptoa(dpage_count);
946 * We can allocate the full range from vm_page_array,
947 * so there's no need to register the range in the tree.
949 vm_phys_fictitious_init_range(fp, start, page_count, memattr);
951 } else if (pe > first_page && (pe - first_page) < vm_page_array_size) {
953 * We have a segment that ends inside of vm_page_array,
954 * but starts outside of it.
956 fp = &vm_page_array[0];
957 dpage_count = pe - first_page;
958 vm_phys_fictitious_init_range(fp, ptoa(first_page), dpage_count,
960 end -= ptoa(dpage_count);
961 page_count -= dpage_count;
963 } else if (pi < first_page && pe > (first_page + vm_page_array_size)) {
965 * Trying to register a fictitious range that expands before
966 * and after vm_page_array.
972 fp = malloc(page_count * sizeof(struct vm_page), M_FICT_PAGES,
974 #ifdef VM_PHYSSEG_DENSE
977 vm_phys_fictitious_init_range(fp, start, page_count, memattr);
979 seg = malloc(sizeof(*seg), M_FICT_PAGES, M_WAITOK | M_ZERO);
982 seg->first_page = fp;
984 rw_wlock(&vm_phys_fictitious_reg_lock);
985 RB_INSERT(fict_tree, &vm_phys_fictitious_tree, seg);
986 rw_wunlock(&vm_phys_fictitious_reg_lock);
992 vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end)
994 struct vm_phys_fictitious_seg *seg, tmp;
995 #ifdef VM_PHYSSEG_DENSE
1000 ("Start of segment isn't less than end (start: %jx end: %jx)",
1001 (uintmax_t)start, (uintmax_t)end));
1003 #ifdef VM_PHYSSEG_DENSE
1006 if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
1007 if ((pe - first_page) <= vm_page_array_size) {
1009 * This segment was allocated using vm_page_array
1010 * only, there's nothing to do since those pages
1011 * were never added to the tree.
1016 * We have a segment that starts inside
1017 * of vm_page_array, but ends outside of it.
1019 * Calculate how many pages were added to the
1020 * tree and free them.
1022 start = ptoa(first_page + vm_page_array_size);
1023 } else if (pe > first_page && (pe - first_page) < vm_page_array_size) {
1025 * We have a segment that ends inside of vm_page_array,
1026 * but starts outside of it.
1028 end = ptoa(first_page);
1029 } else if (pi < first_page && pe > (first_page + vm_page_array_size)) {
1030 /* Since it's not possible to register such a range, panic. */
1032 "Unregistering not registered fictitious range [%#jx:%#jx]",
1033 (uintmax_t)start, (uintmax_t)end);
1039 rw_wlock(&vm_phys_fictitious_reg_lock);
1040 seg = RB_FIND(fict_tree, &vm_phys_fictitious_tree, &tmp);
1041 if (seg->start != start || seg->end != end) {
1042 rw_wunlock(&vm_phys_fictitious_reg_lock);
1044 "Unregistering not registered fictitious range [%#jx:%#jx]",
1045 (uintmax_t)start, (uintmax_t)end);
1047 RB_REMOVE(fict_tree, &vm_phys_fictitious_tree, seg);
1048 rw_wunlock(&vm_phys_fictitious_reg_lock);
1049 free(seg->first_page, M_FICT_PAGES);
1050 free(seg, M_FICT_PAGES);
1054 * Free a contiguous, power of two-sized set of physical pages.
1056 * The free page queues must be locked.
1059 vm_phys_free_pages(vm_page_t m, int order)
1061 struct vm_freelist *fl;
1062 struct vm_phys_seg *seg;
1066 KASSERT(m->order == VM_NFREEORDER,
1067 ("vm_phys_free_pages: page %p has unexpected order %d",
1069 KASSERT(m->pool < VM_NFREEPOOL,
1070 ("vm_phys_free_pages: page %p has unexpected pool %d",
1072 KASSERT(order < VM_NFREEORDER,
1073 ("vm_phys_free_pages: order %d is out of range", order));
1074 seg = &vm_phys_segs[m->segind];
1075 vm_domain_free_assert_locked(VM_DOMAIN(seg->domain));
1076 if (order < VM_NFREEORDER - 1) {
1077 pa = VM_PAGE_TO_PHYS(m);
1079 pa ^= ((vm_paddr_t)1 << (PAGE_SHIFT + order));
1080 if (pa < seg->start || pa >= seg->end)
1082 m_buddy = &seg->first_page[atop(pa - seg->start)];
1083 if (m_buddy->order != order)
1085 fl = (*seg->free_queues)[m_buddy->pool];
1086 vm_freelist_rem(fl, m_buddy, order);
1087 if (m_buddy->pool != m->pool)
1088 vm_phys_set_pool(m->pool, m_buddy, order);
1090 pa &= ~(((vm_paddr_t)1 << (PAGE_SHIFT + order)) - 1);
1091 m = &seg->first_page[atop(pa - seg->start)];
1092 } while (order < VM_NFREEORDER - 1);
1094 fl = (*seg->free_queues)[m->pool];
1095 vm_freelist_add(fl, m, order, 1);
1099 * Free a contiguous, arbitrarily sized set of physical pages.
1101 * The free page queues must be locked.
1104 vm_phys_free_contig(vm_page_t m, u_long npages)
1110 * Avoid unnecessary coalescing by freeing the pages in the largest
1111 * possible power-of-two-sized subsets.
1113 vm_domain_free_assert_locked(vm_pagequeue_domain(m));
1114 for (;; npages -= n) {
1116 * Unsigned "min" is used here so that "order" is assigned
1117 * "VM_NFREEORDER - 1" when "m"'s physical address is zero
1118 * or the low-order bits of its physical address are zero
1119 * because the size of a physical address exceeds the size of
1122 order = min(ffsl(VM_PAGE_TO_PHYS(m) >> PAGE_SHIFT) - 1,
1127 vm_phys_free_pages(m, order);
1130 /* The residual "npages" is less than "1 << (VM_NFREEORDER - 1)". */
1131 for (; npages > 0; npages -= n) {
1132 order = flsl(npages) - 1;
1134 vm_phys_free_pages(m, order);
1140 * Scan physical memory between the specified addresses "low" and "high" for a
1141 * run of contiguous physical pages that satisfy the specified conditions, and
1142 * return the lowest page in the run. The specified "alignment" determines
1143 * the alignment of the lowest physical page in the run. If the specified
1144 * "boundary" is non-zero, then the run of physical pages cannot span a
1145 * physical address that is a multiple of "boundary".
1147 * "npages" must be greater than zero. Both "alignment" and "boundary" must
1148 * be a power of two.
1151 vm_phys_scan_contig(int domain, u_long npages, vm_paddr_t low, vm_paddr_t high,
1152 u_long alignment, vm_paddr_t boundary, int options)
1155 vm_page_t m_end, m_run, m_start;
1156 struct vm_phys_seg *seg;
1159 KASSERT(npages > 0, ("npages is 0"));
1160 KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
1161 KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
1164 for (segind = 0; segind < vm_phys_nsegs; segind++) {
1165 seg = &vm_phys_segs[segind];
1166 if (seg->domain != domain)
1168 if (seg->start >= high)
1170 if (low >= seg->end)
1172 if (low <= seg->start)
1173 m_start = seg->first_page;
1175 m_start = &seg->first_page[atop(low - seg->start)];
1176 if (high < seg->end)
1180 if (pa_end - VM_PAGE_TO_PHYS(m_start) < ptoa(npages))
1182 m_end = &seg->first_page[atop(pa_end - seg->start)];
1183 m_run = vm_page_scan_contig(npages, m_start, m_end,
1184 alignment, boundary, options);
1192 * Set the pool for a contiguous, power of two-sized set of physical pages.
1195 vm_phys_set_pool(int pool, vm_page_t m, int order)
1199 for (m_tmp = m; m_tmp < &m[1 << order]; m_tmp++)
1204 * Search for the given physical page "m" in the free lists. If the search
1205 * succeeds, remove "m" from the free lists and return TRUE. Otherwise, return
1206 * FALSE, indicating that "m" is not in the free lists.
1208 * The free page queues must be locked.
1211 vm_phys_unfree_page(vm_page_t m)
1213 struct vm_freelist *fl;
1214 struct vm_phys_seg *seg;
1215 vm_paddr_t pa, pa_half;
1216 vm_page_t m_set, m_tmp;
1220 * First, find the contiguous, power of two-sized set of free
1221 * physical pages containing the given physical page "m" and
1222 * assign it to "m_set".
1224 seg = &vm_phys_segs[m->segind];
1225 vm_domain_free_assert_locked(VM_DOMAIN(seg->domain));
1226 for (m_set = m, order = 0; m_set->order == VM_NFREEORDER &&
1227 order < VM_NFREEORDER - 1; ) {
1229 pa = m->phys_addr & (~(vm_paddr_t)0 << (PAGE_SHIFT + order));
1230 if (pa >= seg->start)
1231 m_set = &seg->first_page[atop(pa - seg->start)];
1235 if (m_set->order < order)
1237 if (m_set->order == VM_NFREEORDER)
1239 KASSERT(m_set->order < VM_NFREEORDER,
1240 ("vm_phys_unfree_page: page %p has unexpected order %d",
1241 m_set, m_set->order));
1244 * Next, remove "m_set" from the free lists. Finally, extract
1245 * "m" from "m_set" using an iterative algorithm: While "m_set"
1246 * is larger than a page, shrink "m_set" by returning the half
1247 * of "m_set" that does not contain "m" to the free lists.
1249 fl = (*seg->free_queues)[m_set->pool];
1250 order = m_set->order;
1251 vm_freelist_rem(fl, m_set, order);
1254 pa_half = m_set->phys_addr ^ (1 << (PAGE_SHIFT + order));
1255 if (m->phys_addr < pa_half)
1256 m_tmp = &seg->first_page[atop(pa_half - seg->start)];
1259 m_set = &seg->first_page[atop(pa_half - seg->start)];
1261 vm_freelist_add(fl, m_tmp, order, 0);
1263 KASSERT(m_set == m, ("vm_phys_unfree_page: fatal inconsistency"));
1268 * Allocate a contiguous set of physical pages of the given size
1269 * "npages" from the free lists. All of the physical pages must be at
1270 * or above the given physical address "low" and below the given
1271 * physical address "high". The given value "alignment" determines the
1272 * alignment of the first physical page in the set. If the given value
1273 * "boundary" is non-zero, then the set of physical pages cannot cross
1274 * any physical address boundary that is a multiple of that value. Both
1275 * "alignment" and "boundary" must be a power of two.
1278 vm_phys_alloc_contig(int domain, u_long npages, vm_paddr_t low, vm_paddr_t high,
1279 u_long alignment, vm_paddr_t boundary)
1281 vm_paddr_t pa_end, pa_start;
1283 struct vm_phys_seg *seg;
1286 KASSERT(npages > 0, ("npages is 0"));
1287 KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
1288 KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
1289 vm_domain_free_assert_locked(VM_DOMAIN(domain));
1293 for (segind = vm_phys_nsegs - 1; segind >= 0; segind--) {
1294 seg = &vm_phys_segs[segind];
1295 if (seg->start >= high || seg->domain != domain)
1297 if (low >= seg->end)
1299 if (low <= seg->start)
1300 pa_start = seg->start;
1303 if (high < seg->end)
1307 if (pa_end - pa_start < ptoa(npages))
1309 m_run = vm_phys_alloc_seg_contig(seg, npages, low, high,
1310 alignment, boundary);
1318 * Allocate a run of contiguous physical pages from the free list for the
1319 * specified segment.
1322 vm_phys_alloc_seg_contig(struct vm_phys_seg *seg, u_long npages,
1323 vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
1325 struct vm_freelist *fl;
1326 vm_paddr_t pa, pa_end, size;
1329 int oind, order, pind;
1331 KASSERT(npages > 0, ("npages is 0"));
1332 KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
1333 KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
1334 vm_domain_free_assert_locked(VM_DOMAIN(seg->domain));
1335 /* Compute the queue that is the best fit for npages. */
1336 order = flsl(npages - 1);
1337 /* Search for a run satisfying the specified conditions. */
1338 size = npages << PAGE_SHIFT;
1339 for (oind = min(order, VM_NFREEORDER - 1); oind < VM_NFREEORDER;
1341 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
1342 fl = (*seg->free_queues)[pind];
1343 TAILQ_FOREACH(m_ret, &fl[oind].pl, listq) {
1345 * Is the size of this allocation request
1346 * larger than the largest block size?
1348 if (order >= VM_NFREEORDER) {
1350 * Determine if a sufficient number of
1351 * subsequent blocks to satisfy the
1352 * allocation request are free.
1354 pa = VM_PAGE_TO_PHYS(m_ret);
1359 pa += 1 << (PAGE_SHIFT +
1365 m = &seg->first_page[atop(pa -
1367 if (m->order != VM_NFREEORDER -
1371 /* If not, go to the next block. */
1377 * Determine if the blocks are within the
1378 * given range, satisfy the given alignment,
1379 * and do not cross the given boundary.
1381 pa = VM_PAGE_TO_PHYS(m_ret);
1383 if (pa >= low && pa_end <= high &&
1384 (pa & (alignment - 1)) == 0 &&
1385 rounddown2(pa ^ (pa_end - 1), boundary) == 0)
1392 for (m = m_ret; m < &m_ret[npages]; m = &m[1 << oind]) {
1393 fl = (*seg->free_queues)[m->pool];
1394 vm_freelist_rem(fl, m, oind);
1395 if (m->pool != VM_FREEPOOL_DEFAULT)
1396 vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m, oind);
1398 /* Return excess pages to the free lists. */
1399 npages_end = roundup2(npages, 1 << oind);
1400 if (npages < npages_end) {
1401 fl = (*seg->free_queues)[VM_FREEPOOL_DEFAULT];
1402 vm_phys_enq_range(&m_ret[npages], npages_end - npages, fl, 0);
1409 * Show the number of physical pages in each of the free lists.
1411 DB_SHOW_COMMAND(freepages, db_show_freepages)
1413 struct vm_freelist *fl;
1414 int flind, oind, pind, dom;
1416 for (dom = 0; dom < vm_ndomains; dom++) {
1417 db_printf("DOMAIN: %d\n", dom);
1418 for (flind = 0; flind < vm_nfreelists; flind++) {
1419 db_printf("FREE LIST %d:\n"
1420 "\n ORDER (SIZE) | NUMBER"
1422 for (pind = 0; pind < VM_NFREEPOOL; pind++)
1423 db_printf(" | POOL %d", pind);
1425 for (pind = 0; pind < VM_NFREEPOOL; pind++)
1426 db_printf("-- -- ");
1428 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
1429 db_printf(" %2.2d (%6.6dK)", oind,
1430 1 << (PAGE_SHIFT - 10 + oind));
1431 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
1432 fl = vm_phys_free_queues[dom][flind][pind];
1433 db_printf(" | %6.6d", fl[oind].lcnt);