2 * Copyright (c) 2002-2006 Rice University
3 * Copyright (c) 2007 Alan L. Cox <alc@cs.rice.edu>
6 * This software was developed for the FreeBSD Project by Alan L. Cox,
7 * Olivier Crameri, Peter Druschel, Sitaram Iyer, and Juan Navarro.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
28 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Physical memory system implementation
35 * Any external functions defined by this module are only to be used by the
36 * virtual memory system.
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
46 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/mutex.h>
54 #include <sys/queue.h>
56 #include <sys/sysctl.h>
57 #include <sys/vmmeter.h>
62 #include <vm/vm_param.h>
63 #include <vm/vm_kern.h>
64 #include <vm/vm_object.h>
65 #include <vm/vm_page.h>
66 #include <vm/vm_phys.h>
68 _Static_assert(sizeof(long) * NBBY >= VM_PHYSSEG_MAX,
69 "Too many physsegs.");
71 struct mem_affinity *mem_affinity;
75 struct vm_phys_seg vm_phys_segs[VM_PHYSSEG_MAX];
78 #define VM_PHYS_FICTITIOUS_NSEGS 8
79 static struct vm_phys_fictitious_seg {
83 } vm_phys_fictitious_segs[VM_PHYS_FICTITIOUS_NSEGS];
84 static struct mtx vm_phys_fictitious_reg_mtx;
85 MALLOC_DEFINE(M_FICT_PAGES, "vm_fictitious", "Fictitious VM pages");
87 static struct vm_freelist
88 vm_phys_free_queues[MAXMEMDOM][VM_NFREELIST][VM_NFREEPOOL][VM_NFREEORDER];
90 static int vm_nfreelists = VM_FREELIST_DEFAULT + 1;
92 static int cnt_prezero;
93 SYSCTL_INT(_vm_stats_misc, OID_AUTO, cnt_prezero, CTLFLAG_RD,
94 &cnt_prezero, 0, "The number of physical pages prezeroed at idle time");
96 static int sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS);
97 SYSCTL_OID(_vm, OID_AUTO, phys_free, CTLTYPE_STRING | CTLFLAG_RD,
98 NULL, 0, sysctl_vm_phys_free, "A", "Phys Free Info");
100 static int sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS);
101 SYSCTL_OID(_vm, OID_AUTO, phys_segs, CTLTYPE_STRING | CTLFLAG_RD,
102 NULL, 0, sysctl_vm_phys_segs, "A", "Phys Seg Info");
104 SYSCTL_INT(_vm, OID_AUTO, ndomains, CTLFLAG_RD,
105 &vm_ndomains, 0, "Number of physical memory domains available.");
107 static vm_page_t vm_phys_alloc_domain_pages(int domain, int flind, int pool,
109 static void _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int flind,
111 static void vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int flind);
112 static int vm_phys_paddr_to_segind(vm_paddr_t pa);
113 static void vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl,
117 vm_rr_selectdomain(void)
125 td->td_dom_rr_idx %= vm_ndomains;
126 return (td->td_dom_rr_idx);
133 vm_phys_domain_intersects(long mask, vm_paddr_t low, vm_paddr_t high)
135 struct vm_phys_seg *s;
138 while ((idx = ffsl(mask)) != 0) {
139 idx--; /* ffsl counts from 1 */
140 mask &= ~(1UL << idx);
141 s = &vm_phys_segs[idx];
142 if (low < s->end && high > s->start)
149 * Outputs the state of the physical memory allocator, specifically,
150 * the amount of physical memory in each free list.
153 sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS)
156 struct vm_freelist *fl;
157 int dom, error, flind, oind, pind;
159 error = sysctl_wire_old_buffer(req, 0);
162 sbuf_new_for_sysctl(&sbuf, NULL, 128 * vm_ndomains, req);
163 for (dom = 0; dom < vm_ndomains; dom++) {
164 sbuf_printf(&sbuf,"\nDOMAIN %d:\n", dom);
165 for (flind = 0; flind < vm_nfreelists; flind++) {
166 sbuf_printf(&sbuf, "\nFREE LIST %d:\n"
167 "\n ORDER (SIZE) | NUMBER"
169 for (pind = 0; pind < VM_NFREEPOOL; pind++)
170 sbuf_printf(&sbuf, " | POOL %d", pind);
171 sbuf_printf(&sbuf, "\n-- ");
172 for (pind = 0; pind < VM_NFREEPOOL; pind++)
173 sbuf_printf(&sbuf, "-- -- ");
174 sbuf_printf(&sbuf, "--\n");
175 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
176 sbuf_printf(&sbuf, " %2d (%6dK)", oind,
177 1 << (PAGE_SHIFT - 10 + oind));
178 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
179 fl = vm_phys_free_queues[dom][flind][pind];
180 sbuf_printf(&sbuf, " | %6d",
183 sbuf_printf(&sbuf, "\n");
187 error = sbuf_finish(&sbuf);
193 * Outputs the set of physical memory segments.
196 sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS)
199 struct vm_phys_seg *seg;
202 error = sysctl_wire_old_buffer(req, 0);
205 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
206 for (segind = 0; segind < vm_phys_nsegs; segind++) {
207 sbuf_printf(&sbuf, "\nSEGMENT %d:\n\n", segind);
208 seg = &vm_phys_segs[segind];
209 sbuf_printf(&sbuf, "start: %#jx\n",
210 (uintmax_t)seg->start);
211 sbuf_printf(&sbuf, "end: %#jx\n",
212 (uintmax_t)seg->end);
213 sbuf_printf(&sbuf, "domain: %d\n", seg->domain);
214 sbuf_printf(&sbuf, "free list: %p\n", seg->free_queues);
216 error = sbuf_finish(&sbuf);
222 vm_freelist_add(struct vm_freelist *fl, vm_page_t m, int order, int tail)
227 TAILQ_INSERT_TAIL(&fl[order].pl, m, plinks.q);
229 TAILQ_INSERT_HEAD(&fl[order].pl, m, plinks.q);
234 vm_freelist_rem(struct vm_freelist *fl, vm_page_t m, int order)
237 TAILQ_REMOVE(&fl[order].pl, m, plinks.q);
239 m->order = VM_NFREEORDER;
243 * Create a physical memory segment.
246 _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int flind, int domain)
248 struct vm_phys_seg *seg;
249 #ifdef VM_PHYSSEG_SPARSE
254 for (segind = 0; segind < vm_phys_nsegs; segind++) {
255 seg = &vm_phys_segs[segind];
256 pages += atop(seg->end - seg->start);
259 KASSERT(vm_phys_nsegs < VM_PHYSSEG_MAX,
260 ("vm_phys_create_seg: increase VM_PHYSSEG_MAX"));
261 KASSERT(domain < vm_ndomains,
262 ("vm_phys_create_seg: invalid domain provided"));
263 seg = &vm_phys_segs[vm_phys_nsegs++];
266 seg->domain = domain;
267 #ifdef VM_PHYSSEG_SPARSE
268 seg->first_page = &vm_page_array[pages];
270 seg->first_page = PHYS_TO_VM_PAGE(start);
272 seg->free_queues = &vm_phys_free_queues[domain][flind];
276 vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int flind)
280 if (mem_affinity == NULL) {
281 _vm_phys_create_seg(start, end, flind, 0);
286 if (mem_affinity[i].end == 0)
287 panic("Reached end of affinity info");
288 if (mem_affinity[i].end <= start)
290 if (mem_affinity[i].start > start)
291 panic("No affinity info for start %jx",
293 if (mem_affinity[i].end >= end) {
294 _vm_phys_create_seg(start, end, flind,
295 mem_affinity[i].domain);
298 _vm_phys_create_seg(start, mem_affinity[i].end, flind,
299 mem_affinity[i].domain);
300 start = mem_affinity[i].end;
305 * Initialize the physical memory allocator.
310 struct vm_freelist *fl;
311 int dom, flind, i, oind, pind;
313 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
314 #ifdef VM_FREELIST_ISADMA
315 if (phys_avail[i] < 16777216) {
316 if (phys_avail[i + 1] > 16777216) {
317 vm_phys_create_seg(phys_avail[i], 16777216,
319 vm_phys_create_seg(16777216, phys_avail[i + 1],
320 VM_FREELIST_DEFAULT);
322 vm_phys_create_seg(phys_avail[i],
323 phys_avail[i + 1], VM_FREELIST_ISADMA);
325 if (VM_FREELIST_ISADMA >= vm_nfreelists)
326 vm_nfreelists = VM_FREELIST_ISADMA + 1;
329 #ifdef VM_FREELIST_HIGHMEM
330 if (phys_avail[i + 1] > VM_HIGHMEM_ADDRESS) {
331 if (phys_avail[i] < VM_HIGHMEM_ADDRESS) {
332 vm_phys_create_seg(phys_avail[i],
333 VM_HIGHMEM_ADDRESS, VM_FREELIST_DEFAULT);
334 vm_phys_create_seg(VM_HIGHMEM_ADDRESS,
335 phys_avail[i + 1], VM_FREELIST_HIGHMEM);
337 vm_phys_create_seg(phys_avail[i],
338 phys_avail[i + 1], VM_FREELIST_HIGHMEM);
340 if (VM_FREELIST_HIGHMEM >= vm_nfreelists)
341 vm_nfreelists = VM_FREELIST_HIGHMEM + 1;
344 vm_phys_create_seg(phys_avail[i], phys_avail[i + 1],
345 VM_FREELIST_DEFAULT);
347 for (dom = 0; dom < vm_ndomains; dom++) {
348 for (flind = 0; flind < vm_nfreelists; flind++) {
349 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
350 fl = vm_phys_free_queues[dom][flind][pind];
351 for (oind = 0; oind < VM_NFREEORDER; oind++)
352 TAILQ_INIT(&fl[oind].pl);
356 mtx_init(&vm_phys_fictitious_reg_mtx, "vmfctr", NULL, MTX_DEF);
360 * Split a contiguous, power of two-sized set of physical pages.
363 vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl, int order)
367 while (oind > order) {
369 m_buddy = &m[1 << oind];
370 KASSERT(m_buddy->order == VM_NFREEORDER,
371 ("vm_phys_split_pages: page %p has unexpected order %d",
372 m_buddy, m_buddy->order));
373 vm_freelist_add(fl, m_buddy, oind, 0);
378 * Initialize a physical page and add it to the free lists.
381 vm_phys_add_page(vm_paddr_t pa)
384 struct vm_domain *vmd;
387 m = vm_phys_paddr_to_vm_page(pa);
390 m->segind = vm_phys_paddr_to_segind(pa);
391 vmd = vm_phys_domain(m);
392 vmd->vmd_page_count++;
393 vmd->vmd_segs |= 1UL << m->segind;
395 KASSERT(m->order == VM_NFREEORDER,
396 ("vm_phys_add_page: page %p has unexpected order %d",
398 m->pool = VM_FREEPOOL_DEFAULT;
400 mtx_lock(&vm_page_queue_free_mtx);
401 vm_phys_freecnt_adj(m, 1);
402 vm_phys_free_pages(m, 0);
403 mtx_unlock(&vm_page_queue_free_mtx);
407 * Allocate a contiguous, power of two-sized set of physical pages
408 * from the free lists.
410 * The free page queues must be locked.
413 vm_phys_alloc_pages(int pool, int order)
416 int dom, domain, flind;
418 KASSERT(pool < VM_NFREEPOOL,
419 ("vm_phys_alloc_pages: pool %d is out of range", pool));
420 KASSERT(order < VM_NFREEORDER,
421 ("vm_phys_alloc_pages: order %d is out of range", order));
423 for (dom = 0; dom < vm_ndomains; dom++) {
424 domain = vm_rr_selectdomain();
425 for (flind = 0; flind < vm_nfreelists; flind++) {
426 m = vm_phys_alloc_domain_pages(domain, flind, pool,
436 * Find and dequeue a free page on the given free list, with the
437 * specified pool and order
440 vm_phys_alloc_freelist_pages(int flind, int pool, int order)
445 KASSERT(flind < VM_NFREELIST,
446 ("vm_phys_alloc_freelist_pages: freelist %d is out of range", flind));
447 KASSERT(pool < VM_NFREEPOOL,
448 ("vm_phys_alloc_freelist_pages: pool %d is out of range", pool));
449 KASSERT(order < VM_NFREEORDER,
450 ("vm_phys_alloc_freelist_pages: order %d is out of range", order));
452 for (dom = 0; dom < vm_ndomains; dom++) {
453 domain = vm_rr_selectdomain();
454 m = vm_phys_alloc_domain_pages(domain, flind, pool, order);
462 vm_phys_alloc_domain_pages(int domain, int flind, int pool, int order)
464 struct vm_freelist *fl;
465 struct vm_freelist *alt;
469 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
470 fl = &vm_phys_free_queues[domain][flind][pool][0];
471 for (oind = order; oind < VM_NFREEORDER; oind++) {
472 m = TAILQ_FIRST(&fl[oind].pl);
474 vm_freelist_rem(fl, m, oind);
475 vm_phys_split_pages(m, oind, fl, order);
481 * The given pool was empty. Find the largest
482 * contiguous, power-of-two-sized set of pages in any
483 * pool. Transfer these pages to the given pool, and
484 * use them to satisfy the allocation.
486 for (oind = VM_NFREEORDER - 1; oind >= order; oind--) {
487 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
488 alt = &vm_phys_free_queues[domain][flind][pind][0];
489 m = TAILQ_FIRST(&alt[oind].pl);
491 vm_freelist_rem(alt, m, oind);
492 vm_phys_set_pool(pool, m, oind);
493 vm_phys_split_pages(m, oind, fl, order);
502 * Find the vm_page corresponding to the given physical address.
505 vm_phys_paddr_to_vm_page(vm_paddr_t pa)
507 struct vm_phys_seg *seg;
510 for (segind = 0; segind < vm_phys_nsegs; segind++) {
511 seg = &vm_phys_segs[segind];
512 if (pa >= seg->start && pa < seg->end)
513 return (&seg->first_page[atop(pa - seg->start)]);
519 vm_phys_fictitious_to_vm_page(vm_paddr_t pa)
521 struct vm_phys_fictitious_seg *seg;
526 for (segind = 0; segind < VM_PHYS_FICTITIOUS_NSEGS; segind++) {
527 seg = &vm_phys_fictitious_segs[segind];
528 if (pa >= seg->start && pa < seg->end) {
529 m = &seg->first_page[atop(pa - seg->start)];
530 KASSERT((m->flags & PG_FICTITIOUS) != 0,
531 ("%p not fictitious", m));
539 vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
540 vm_memattr_t memattr)
542 struct vm_phys_fictitious_seg *seg;
546 #ifdef VM_PHYSSEG_DENSE
551 page_count = (end - start) / PAGE_SIZE;
553 #ifdef VM_PHYSSEG_DENSE
555 if (pi >= first_page && pi < vm_page_array_size + first_page) {
556 if (atop(end) >= vm_page_array_size + first_page)
558 fp = &vm_page_array[pi - first_page];
563 fp = malloc(page_count * sizeof(struct vm_page), M_FICT_PAGES,
565 #ifdef VM_PHYSSEG_DENSE
569 for (i = 0; i < page_count; i++) {
570 vm_page_initfake(&fp[i], start + PAGE_SIZE * i, memattr);
571 fp[i].oflags &= ~VPO_UNMANAGED;
572 fp[i].busy_lock = VPB_UNBUSIED;
574 mtx_lock(&vm_phys_fictitious_reg_mtx);
575 for (segind = 0; segind < VM_PHYS_FICTITIOUS_NSEGS; segind++) {
576 seg = &vm_phys_fictitious_segs[segind];
577 if (seg->start == 0 && seg->end == 0) {
580 seg->first_page = fp;
581 mtx_unlock(&vm_phys_fictitious_reg_mtx);
585 mtx_unlock(&vm_phys_fictitious_reg_mtx);
586 #ifdef VM_PHYSSEG_DENSE
589 free(fp, M_FICT_PAGES);
594 vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end)
596 struct vm_phys_fictitious_seg *seg;
599 #ifdef VM_PHYSSEG_DENSE
603 #ifdef VM_PHYSSEG_DENSE
607 mtx_lock(&vm_phys_fictitious_reg_mtx);
608 for (segind = 0; segind < VM_PHYS_FICTITIOUS_NSEGS; segind++) {
609 seg = &vm_phys_fictitious_segs[segind];
610 if (seg->start == start && seg->end == end) {
611 seg->start = seg->end = 0;
612 fp = seg->first_page;
613 seg->first_page = NULL;
614 mtx_unlock(&vm_phys_fictitious_reg_mtx);
615 #ifdef VM_PHYSSEG_DENSE
616 if (pi < first_page || atop(end) >= vm_page_array_size)
618 free(fp, M_FICT_PAGES);
622 mtx_unlock(&vm_phys_fictitious_reg_mtx);
623 KASSERT(0, ("Unregistering not registered fictitious range"));
627 * Find the segment containing the given physical address.
630 vm_phys_paddr_to_segind(vm_paddr_t pa)
632 struct vm_phys_seg *seg;
635 for (segind = 0; segind < vm_phys_nsegs; segind++) {
636 seg = &vm_phys_segs[segind];
637 if (pa >= seg->start && pa < seg->end)
640 panic("vm_phys_paddr_to_segind: paddr %#jx is not in any segment" ,
645 * Free a contiguous, power of two-sized set of physical pages.
647 * The free page queues must be locked.
650 vm_phys_free_pages(vm_page_t m, int order)
652 struct vm_freelist *fl;
653 struct vm_phys_seg *seg;
657 KASSERT(m->order == VM_NFREEORDER,
658 ("vm_phys_free_pages: page %p has unexpected order %d",
660 KASSERT(m->pool < VM_NFREEPOOL,
661 ("vm_phys_free_pages: page %p has unexpected pool %d",
663 KASSERT(order < VM_NFREEORDER,
664 ("vm_phys_free_pages: order %d is out of range", order));
665 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
666 seg = &vm_phys_segs[m->segind];
667 if (order < VM_NFREEORDER - 1) {
668 pa = VM_PAGE_TO_PHYS(m);
670 pa ^= ((vm_paddr_t)1 << (PAGE_SHIFT + order));
671 if (pa < seg->start || pa >= seg->end)
673 m_buddy = &seg->first_page[atop(pa - seg->start)];
674 if (m_buddy->order != order)
676 fl = (*seg->free_queues)[m_buddy->pool];
677 vm_freelist_rem(fl, m_buddy, order);
678 if (m_buddy->pool != m->pool)
679 vm_phys_set_pool(m->pool, m_buddy, order);
681 pa &= ~(((vm_paddr_t)1 << (PAGE_SHIFT + order)) - 1);
682 m = &seg->first_page[atop(pa - seg->start)];
683 } while (order < VM_NFREEORDER - 1);
685 fl = (*seg->free_queues)[m->pool];
686 vm_freelist_add(fl, m, order, 1);
690 * Free a contiguous, arbitrarily sized set of physical pages.
692 * The free page queues must be locked.
695 vm_phys_free_contig(vm_page_t m, u_long npages)
701 * Avoid unnecessary coalescing by freeing the pages in the largest
702 * possible power-of-two-sized subsets.
704 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
705 for (;; npages -= n) {
707 * Unsigned "min" is used here so that "order" is assigned
708 * "VM_NFREEORDER - 1" when "m"'s physical address is zero
709 * or the low-order bits of its physical address are zero
710 * because the size of a physical address exceeds the size of
713 order = min(ffsl(VM_PAGE_TO_PHYS(m) >> PAGE_SHIFT) - 1,
718 vm_phys_free_pages(m, order);
721 /* The residual "npages" is less than "1 << (VM_NFREEORDER - 1)". */
722 for (; npages > 0; npages -= n) {
723 order = flsl(npages) - 1;
725 vm_phys_free_pages(m, order);
731 * Set the pool for a contiguous, power of two-sized set of physical pages.
734 vm_phys_set_pool(int pool, vm_page_t m, int order)
738 for (m_tmp = m; m_tmp < &m[1 << order]; m_tmp++)
743 * Search for the given physical page "m" in the free lists. If the search
744 * succeeds, remove "m" from the free lists and return TRUE. Otherwise, return
745 * FALSE, indicating that "m" is not in the free lists.
747 * The free page queues must be locked.
750 vm_phys_unfree_page(vm_page_t m)
752 struct vm_freelist *fl;
753 struct vm_phys_seg *seg;
754 vm_paddr_t pa, pa_half;
755 vm_page_t m_set, m_tmp;
758 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
761 * First, find the contiguous, power of two-sized set of free
762 * physical pages containing the given physical page "m" and
763 * assign it to "m_set".
765 seg = &vm_phys_segs[m->segind];
766 for (m_set = m, order = 0; m_set->order == VM_NFREEORDER &&
767 order < VM_NFREEORDER - 1; ) {
769 pa = m->phys_addr & (~(vm_paddr_t)0 << (PAGE_SHIFT + order));
770 if (pa >= seg->start)
771 m_set = &seg->first_page[atop(pa - seg->start)];
775 if (m_set->order < order)
777 if (m_set->order == VM_NFREEORDER)
779 KASSERT(m_set->order < VM_NFREEORDER,
780 ("vm_phys_unfree_page: page %p has unexpected order %d",
781 m_set, m_set->order));
784 * Next, remove "m_set" from the free lists. Finally, extract
785 * "m" from "m_set" using an iterative algorithm: While "m_set"
786 * is larger than a page, shrink "m_set" by returning the half
787 * of "m_set" that does not contain "m" to the free lists.
789 fl = (*seg->free_queues)[m_set->pool];
790 order = m_set->order;
791 vm_freelist_rem(fl, m_set, order);
794 pa_half = m_set->phys_addr ^ (1 << (PAGE_SHIFT + order));
795 if (m->phys_addr < pa_half)
796 m_tmp = &seg->first_page[atop(pa_half - seg->start)];
799 m_set = &seg->first_page[atop(pa_half - seg->start)];
801 vm_freelist_add(fl, m_tmp, order, 0);
803 KASSERT(m_set == m, ("vm_phys_unfree_page: fatal inconsistency"));
808 * Try to zero one physical page. Used by an idle priority thread.
811 vm_phys_zero_pages_idle(void)
813 static struct vm_freelist *fl;
814 static int flind, oind, pind;
818 domain = vm_rr_selectdomain();
819 fl = vm_phys_free_queues[domain][0][0];
820 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
822 TAILQ_FOREACH_REVERSE(m, &fl[oind].pl, pglist, plinks.q) {
823 for (m_tmp = m; m_tmp < &m[1 << oind]; m_tmp++) {
824 if ((m_tmp->flags & (PG_CACHED | PG_ZERO)) == 0) {
825 vm_phys_unfree_page(m_tmp);
826 vm_phys_freecnt_adj(m, -1);
827 mtx_unlock(&vm_page_queue_free_mtx);
828 pmap_zero_page_idle(m_tmp);
829 m_tmp->flags |= PG_ZERO;
830 mtx_lock(&vm_page_queue_free_mtx);
831 vm_phys_freecnt_adj(m, 1);
832 vm_phys_free_pages(m_tmp, 0);
833 vm_page_zero_count++;
840 if (oind == VM_NFREEORDER) {
843 if (pind == VM_NFREEPOOL) {
846 if (flind == vm_nfreelists)
849 fl = vm_phys_free_queues[domain][flind][pind];
855 * Allocate a contiguous set of physical pages of the given size
856 * "npages" from the free lists. All of the physical pages must be at
857 * or above the given physical address "low" and below the given
858 * physical address "high". The given value "alignment" determines the
859 * alignment of the first physical page in the set. If the given value
860 * "boundary" is non-zero, then the set of physical pages cannot cross
861 * any physical address boundary that is a multiple of that value. Both
862 * "alignment" and "boundary" must be a power of two.
865 vm_phys_alloc_contig(u_long npages, vm_paddr_t low, vm_paddr_t high,
866 u_long alignment, vm_paddr_t boundary)
868 struct vm_freelist *fl;
869 struct vm_phys_seg *seg;
870 vm_paddr_t pa, pa_last, size;
873 int dom, domain, flind, oind, order, pind;
875 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
876 size = npages << PAGE_SHIFT;
878 ("vm_phys_alloc_contig: size must not be 0"));
879 KASSERT((alignment & (alignment - 1)) == 0,
880 ("vm_phys_alloc_contig: alignment must be a power of 2"));
881 KASSERT((boundary & (boundary - 1)) == 0,
882 ("vm_phys_alloc_contig: boundary must be a power of 2"));
883 /* Compute the queue that is the best fit for npages. */
884 for (order = 0; (1 << order) < npages; order++);
887 domain = vm_rr_selectdomain();
888 for (flind = 0; flind < vm_nfreelists; flind++) {
889 for (oind = min(order, VM_NFREEORDER - 1); oind < VM_NFREEORDER; oind++) {
890 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
891 fl = &vm_phys_free_queues[domain][flind][pind][0];
892 TAILQ_FOREACH(m_ret, &fl[oind].pl, plinks.q) {
894 * A free list may contain physical pages
895 * from one or more segments.
897 seg = &vm_phys_segs[m_ret->segind];
898 if (seg->start > high ||
903 * Is the size of this allocation request
904 * larger than the largest block size?
906 if (order >= VM_NFREEORDER) {
908 * Determine if a sufficient number
909 * of subsequent blocks to satisfy
910 * the allocation request are free.
912 pa = VM_PAGE_TO_PHYS(m_ret);
915 pa += 1 << (PAGE_SHIFT + VM_NFREEORDER - 1);
918 if (pa < seg->start ||
921 m = &seg->first_page[atop(pa - seg->start)];
922 if (m->order != VM_NFREEORDER - 1)
925 /* If not, continue to the next block. */
931 * Determine if the blocks are within the given range,
932 * satisfy the given alignment, and do not cross the
935 pa = VM_PAGE_TO_PHYS(m_ret);
938 (pa & (alignment - 1)) == 0 &&
939 ((pa ^ (pa + size - 1)) & ~(boundary - 1)) == 0)
945 if (++dom < vm_ndomains)
949 for (m = m_ret; m < &m_ret[npages]; m = &m[1 << oind]) {
950 fl = (*seg->free_queues)[m->pool];
951 vm_freelist_rem(fl, m, m->order);
953 if (m_ret->pool != VM_FREEPOOL_DEFAULT)
954 vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m_ret, oind);
955 fl = (*seg->free_queues)[m_ret->pool];
956 vm_phys_split_pages(m_ret, oind, fl, order);
957 /* Return excess pages to the free lists. */
958 npages_end = roundup2(npages, 1 << imin(oind, order));
959 if (npages < npages_end)
960 vm_phys_free_contig(&m_ret[npages], npages_end - npages);
966 * Show the number of physical pages in each of the free lists.
968 DB_SHOW_COMMAND(freepages, db_show_freepages)
970 struct vm_freelist *fl;
971 int flind, oind, pind, dom;
973 for (dom = 0; dom < vm_ndomains; dom++) {
974 db_printf("DOMAIN: %d\n", dom);
975 for (flind = 0; flind < vm_nfreelists; flind++) {
976 db_printf("FREE LIST %d:\n"
977 "\n ORDER (SIZE) | NUMBER"
979 for (pind = 0; pind < VM_NFREEPOOL; pind++)
980 db_printf(" | POOL %d", pind);
982 for (pind = 0; pind < VM_NFREEPOOL; pind++)
985 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
986 db_printf(" %2.2d (%6.6dK)", oind,
987 1 << (PAGE_SHIFT - 10 + oind));
988 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
989 fl = vm_phys_free_queues[dom][flind][pind];
990 db_printf(" | %6.6d", fl[oind].lcnt);