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$");
44 #include <sys/param.h>
45 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/mutex.h>
50 #include <sys/queue.h>
52 #include <sys/sysctl.h>
53 #include <sys/vmmeter.h>
58 #include <vm/vm_param.h>
59 #include <vm/vm_kern.h>
60 #include <vm/vm_object.h>
61 #include <vm/vm_page.h>
62 #include <vm/vm_phys.h>
65 * VM_FREELIST_DEFAULT is split into VM_NDOMAIN lists, one for each
66 * domain. These extra lists are stored at the end of the regular
67 * free lists starting with VM_NFREELIST.
69 #define VM_RAW_NFREELIST (VM_NFREELIST + VM_NDOMAIN - 1)
81 struct vm_freelist (*free_queues)[VM_NFREEPOOL][VM_NFREEORDER];
84 struct mem_affinity *mem_affinity;
86 static struct vm_phys_seg vm_phys_segs[VM_PHYSSEG_MAX];
88 static int vm_phys_nsegs;
90 static struct vm_freelist
91 vm_phys_free_queues[VM_RAW_NFREELIST][VM_NFREEPOOL][VM_NFREEORDER];
92 static struct vm_freelist
93 (*vm_phys_lookup_lists[VM_NDOMAIN][VM_RAW_NFREELIST])[VM_NFREEPOOL][VM_NFREEORDER];
95 static int vm_nfreelists = VM_FREELIST_DEFAULT + 1;
97 static int cnt_prezero;
98 SYSCTL_INT(_vm_stats_misc, OID_AUTO, cnt_prezero, CTLFLAG_RD,
99 &cnt_prezero, 0, "The number of physical pages prezeroed at idle time");
101 static int sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS);
102 SYSCTL_OID(_vm, OID_AUTO, phys_free, CTLTYPE_STRING | CTLFLAG_RD,
103 NULL, 0, sysctl_vm_phys_free, "A", "Phys Free Info");
105 static int sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS);
106 SYSCTL_OID(_vm, OID_AUTO, phys_segs, CTLTYPE_STRING | CTLFLAG_RD,
107 NULL, 0, sysctl_vm_phys_segs, "A", "Phys Seg Info");
110 static int sysctl_vm_phys_lookup_lists(SYSCTL_HANDLER_ARGS);
111 SYSCTL_OID(_vm, OID_AUTO, phys_lookup_lists, CTLTYPE_STRING | CTLFLAG_RD,
112 NULL, 0, sysctl_vm_phys_lookup_lists, "A", "Phys Lookup Lists");
115 static void _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int flind,
117 static void vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int flind);
118 static int vm_phys_paddr_to_segind(vm_paddr_t pa);
119 static void vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl,
123 * Outputs the state of the physical memory allocator, specifically,
124 * the amount of physical memory in each free list.
127 sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS)
130 struct vm_freelist *fl;
131 int error, flind, oind, pind;
133 error = sysctl_wire_old_buffer(req, 0);
136 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
137 for (flind = 0; flind < vm_nfreelists; flind++) {
138 sbuf_printf(&sbuf, "\nFREE LIST %d:\n"
139 "\n ORDER (SIZE) | NUMBER"
141 for (pind = 0; pind < VM_NFREEPOOL; pind++)
142 sbuf_printf(&sbuf, " | POOL %d", pind);
143 sbuf_printf(&sbuf, "\n-- ");
144 for (pind = 0; pind < VM_NFREEPOOL; pind++)
145 sbuf_printf(&sbuf, "-- -- ");
146 sbuf_printf(&sbuf, "--\n");
147 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
148 sbuf_printf(&sbuf, " %2d (%6dK)", oind,
149 1 << (PAGE_SHIFT - 10 + oind));
150 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
151 fl = vm_phys_free_queues[flind][pind];
152 sbuf_printf(&sbuf, " | %6d", fl[oind].lcnt);
154 sbuf_printf(&sbuf, "\n");
157 error = sbuf_finish(&sbuf);
163 * Outputs the set of physical memory segments.
166 sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS)
169 struct vm_phys_seg *seg;
172 error = sysctl_wire_old_buffer(req, 0);
175 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
176 for (segind = 0; segind < vm_phys_nsegs; segind++) {
177 sbuf_printf(&sbuf, "\nSEGMENT %d:\n\n", segind);
178 seg = &vm_phys_segs[segind];
179 sbuf_printf(&sbuf, "start: %#jx\n",
180 (uintmax_t)seg->start);
181 sbuf_printf(&sbuf, "end: %#jx\n",
182 (uintmax_t)seg->end);
183 sbuf_printf(&sbuf, "domain: %d\n", seg->domain);
184 sbuf_printf(&sbuf, "free list: %p\n", seg->free_queues);
186 error = sbuf_finish(&sbuf);
193 * Outputs the set of free list lookup lists.
196 sysctl_vm_phys_lookup_lists(SYSCTL_HANDLER_ARGS)
199 int domain, error, flind, ndomains;
201 error = sysctl_wire_old_buffer(req, 0);
204 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
205 ndomains = vm_nfreelists - VM_NFREELIST + 1;
206 for (domain = 0; domain < ndomains; domain++) {
207 sbuf_printf(&sbuf, "\nDOMAIN %d:\n\n", domain);
208 for (flind = 0; flind < vm_nfreelists; flind++)
209 sbuf_printf(&sbuf, " [%d]:\t%p\n", flind,
210 vm_phys_lookup_lists[domain][flind]);
212 error = sbuf_finish(&sbuf);
219 * Create a physical memory segment.
222 _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int flind, int domain)
224 struct vm_phys_seg *seg;
225 #ifdef VM_PHYSSEG_SPARSE
230 for (segind = 0; segind < vm_phys_nsegs; segind++) {
231 seg = &vm_phys_segs[segind];
232 pages += atop(seg->end - seg->start);
235 KASSERT(vm_phys_nsegs < VM_PHYSSEG_MAX,
236 ("vm_phys_create_seg: increase VM_PHYSSEG_MAX"));
237 seg = &vm_phys_segs[vm_phys_nsegs++];
240 seg->domain = domain;
241 #ifdef VM_PHYSSEG_SPARSE
242 seg->first_page = &vm_page_array[pages];
244 seg->first_page = PHYS_TO_VM_PAGE(start);
247 if (flind == VM_FREELIST_DEFAULT && domain != 0) {
248 flind = VM_NFREELIST + (domain - 1);
249 if (flind >= vm_nfreelists)
250 vm_nfreelists = flind + 1;
253 seg->free_queues = &vm_phys_free_queues[flind];
257 vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int flind)
261 if (mem_affinity == NULL) {
262 _vm_phys_create_seg(start, end, flind, 0);
267 if (mem_affinity[i].end == 0)
268 panic("Reached end of affinity info");
269 if (mem_affinity[i].end <= start)
271 if (mem_affinity[i].start > start)
272 panic("No affinity info for start %jx",
274 if (mem_affinity[i].end >= end) {
275 _vm_phys_create_seg(start, end, flind,
276 mem_affinity[i].domain);
279 _vm_phys_create_seg(start, mem_affinity[i].end, flind,
280 mem_affinity[i].domain);
281 start = mem_affinity[i].end;
286 * Initialize the physical memory allocator.
291 struct vm_freelist *fl;
292 int flind, i, oind, pind;
297 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
298 #ifdef VM_FREELIST_ISADMA
299 if (phys_avail[i] < 16777216) {
300 if (phys_avail[i + 1] > 16777216) {
301 vm_phys_create_seg(phys_avail[i], 16777216,
303 vm_phys_create_seg(16777216, phys_avail[i + 1],
304 VM_FREELIST_DEFAULT);
306 vm_phys_create_seg(phys_avail[i],
307 phys_avail[i + 1], VM_FREELIST_ISADMA);
309 if (VM_FREELIST_ISADMA >= vm_nfreelists)
310 vm_nfreelists = VM_FREELIST_ISADMA + 1;
313 #ifdef VM_FREELIST_HIGHMEM
314 if (phys_avail[i + 1] > VM_HIGHMEM_ADDRESS) {
315 if (phys_avail[i] < VM_HIGHMEM_ADDRESS) {
316 vm_phys_create_seg(phys_avail[i],
317 VM_HIGHMEM_ADDRESS, VM_FREELIST_DEFAULT);
318 vm_phys_create_seg(VM_HIGHMEM_ADDRESS,
319 phys_avail[i + 1], VM_FREELIST_HIGHMEM);
321 vm_phys_create_seg(phys_avail[i],
322 phys_avail[i + 1], VM_FREELIST_HIGHMEM);
324 if (VM_FREELIST_HIGHMEM >= vm_nfreelists)
325 vm_nfreelists = VM_FREELIST_HIGHMEM + 1;
328 vm_phys_create_seg(phys_avail[i], phys_avail[i + 1],
329 VM_FREELIST_DEFAULT);
331 for (flind = 0; flind < vm_nfreelists; flind++) {
332 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
333 fl = vm_phys_free_queues[flind][pind];
334 for (oind = 0; oind < VM_NFREEORDER; oind++)
335 TAILQ_INIT(&fl[oind].pl);
340 * Build a free list lookup list for each domain. All of the
341 * memory domain lists are inserted at the VM_FREELIST_DEFAULT
342 * index in a round-robin order starting with the current
345 ndomains = vm_nfreelists - VM_NFREELIST + 1;
346 for (flind = 0; flind < VM_FREELIST_DEFAULT; flind++)
347 for (i = 0; i < ndomains; i++)
348 vm_phys_lookup_lists[i][flind] =
349 &vm_phys_free_queues[flind];
350 for (i = 0; i < ndomains; i++)
351 for (j = 0; j < ndomains; j++) {
352 flind = (i + j) % ndomains;
354 flind = VM_FREELIST_DEFAULT;
356 flind += VM_NFREELIST - 1;
357 vm_phys_lookup_lists[i][VM_FREELIST_DEFAULT + j] =
358 &vm_phys_free_queues[flind];
360 for (flind = VM_FREELIST_DEFAULT + 1; flind < VM_NFREELIST;
362 for (i = 0; i < ndomains; i++)
363 vm_phys_lookup_lists[i][flind + ndomains - 1] =
364 &vm_phys_free_queues[flind];
366 for (flind = 0; flind < vm_nfreelists; flind++)
367 vm_phys_lookup_lists[0][flind] = &vm_phys_free_queues[flind];
372 * Split a contiguous, power of two-sized set of physical pages.
375 vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl, int order)
379 while (oind > order) {
381 m_buddy = &m[1 << oind];
382 KASSERT(m_buddy->order == VM_NFREEORDER,
383 ("vm_phys_split_pages: page %p has unexpected order %d",
384 m_buddy, m_buddy->order));
385 m_buddy->order = oind;
386 TAILQ_INSERT_HEAD(&fl[oind].pl, m_buddy, pageq);
392 * Initialize a physical page and add it to the free lists.
395 vm_phys_add_page(vm_paddr_t pa)
400 m = vm_phys_paddr_to_vm_page(pa);
403 m->segind = vm_phys_paddr_to_segind(pa);
405 KASSERT(m->order == VM_NFREEORDER,
406 ("vm_phys_add_page: page %p has unexpected order %d",
408 m->pool = VM_FREEPOOL_DEFAULT;
410 mtx_lock(&vm_page_queue_free_mtx);
412 vm_phys_free_pages(m, 0);
413 mtx_unlock(&vm_page_queue_free_mtx);
417 * Allocate a contiguous, power of two-sized set of physical pages
418 * from the free lists.
420 * The free page queues must be locked.
423 vm_phys_alloc_pages(int pool, int order)
428 for (flind = 0; flind < vm_nfreelists; flind++) {
429 m = vm_phys_alloc_freelist_pages(flind, pool, order);
437 * Find and dequeue a free page on the given free list, with the
438 * specified pool and order
441 vm_phys_alloc_freelist_pages(int flind, int pool, int order)
443 struct vm_freelist *fl;
444 struct vm_freelist *alt;
445 int domain, oind, pind;
448 KASSERT(flind < VM_NFREELIST,
449 ("vm_phys_alloc_freelist_pages: freelist %d is out of range", flind));
450 KASSERT(pool < VM_NFREEPOOL,
451 ("vm_phys_alloc_freelist_pages: pool %d is out of range", pool));
452 KASSERT(order < VM_NFREEORDER,
453 ("vm_phys_alloc_freelist_pages: order %d is out of range", order));
456 domain = PCPU_GET(domain);
460 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
461 fl = (*vm_phys_lookup_lists[domain][flind])[pool];
462 for (oind = order; oind < VM_NFREEORDER; oind++) {
463 m = TAILQ_FIRST(&fl[oind].pl);
465 TAILQ_REMOVE(&fl[oind].pl, m, pageq);
467 m->order = VM_NFREEORDER;
468 vm_phys_split_pages(m, oind, fl, order);
474 * The given pool was empty. Find the largest
475 * contiguous, power-of-two-sized set of pages in any
476 * pool. Transfer these pages to the given pool, and
477 * use them to satisfy the allocation.
479 for (oind = VM_NFREEORDER - 1; oind >= order; oind--) {
480 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
481 alt = (*vm_phys_lookup_lists[domain][flind])[pind];
482 m = TAILQ_FIRST(&alt[oind].pl);
484 TAILQ_REMOVE(&alt[oind].pl, m, pageq);
486 m->order = VM_NFREEORDER;
487 vm_phys_set_pool(pool, m, oind);
488 vm_phys_split_pages(m, oind, fl, order);
497 * Find the vm_page corresponding to the given physical address.
500 vm_phys_paddr_to_vm_page(vm_paddr_t pa)
502 struct vm_phys_seg *seg;
505 for (segind = 0; segind < vm_phys_nsegs; segind++) {
506 seg = &vm_phys_segs[segind];
507 if (pa >= seg->start && pa < seg->end)
508 return (&seg->first_page[atop(pa - seg->start)]);
514 * Find the segment containing the given physical address.
517 vm_phys_paddr_to_segind(vm_paddr_t pa)
519 struct vm_phys_seg *seg;
522 for (segind = 0; segind < vm_phys_nsegs; segind++) {
523 seg = &vm_phys_segs[segind];
524 if (pa >= seg->start && pa < seg->end)
527 panic("vm_phys_paddr_to_segind: paddr %#jx is not in any segment" ,
532 * Free a contiguous, power of two-sized set of physical pages.
534 * The free page queues must be locked.
537 vm_phys_free_pages(vm_page_t m, int order)
539 struct vm_freelist *fl;
540 struct vm_phys_seg *seg;
544 KASSERT(m->order == VM_NFREEORDER,
545 ("vm_phys_free_pages: page %p has unexpected order %d",
547 KASSERT(m->pool < VM_NFREEPOOL,
548 ("vm_phys_free_pages: page %p has unexpected pool %d",
550 KASSERT(order < VM_NFREEORDER,
551 ("vm_phys_free_pages: order %d is out of range", order));
552 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
553 seg = &vm_phys_segs[m->segind];
554 if (order < VM_NFREEORDER - 1) {
555 pa = VM_PAGE_TO_PHYS(m);
557 pa ^= ((vm_paddr_t)1 << (PAGE_SHIFT + order));
558 if (pa < seg->start || pa >= seg->end)
560 m_buddy = &seg->first_page[atop(pa - seg->start)];
561 if (m_buddy->order != order)
563 fl = (*seg->free_queues)[m_buddy->pool];
564 TAILQ_REMOVE(&fl[order].pl, m_buddy, pageq);
566 m_buddy->order = VM_NFREEORDER;
567 if (m_buddy->pool != m->pool)
568 vm_phys_set_pool(m->pool, m_buddy, order);
570 pa &= ~(((vm_paddr_t)1 << (PAGE_SHIFT + order)) - 1);
571 m = &seg->first_page[atop(pa - seg->start)];
572 } while (order < VM_NFREEORDER - 1);
575 fl = (*seg->free_queues)[m->pool];
576 TAILQ_INSERT_TAIL(&fl[order].pl, m, pageq);
581 * Free a contiguous, arbitrarily sized set of physical pages.
583 * The free page queues must be locked.
586 vm_phys_free_contig(vm_page_t m, u_long npages)
592 * Avoid unnecessary coalescing by freeing the pages in the largest
593 * possible power-of-two-sized subsets.
595 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
596 for (;; npages -= n) {
598 * Unsigned "min" is used here so that "order" is assigned
599 * "VM_NFREEORDER - 1" when "m"'s physical address is zero
600 * or the low-order bits of its physical address are zero
601 * because the size of a physical address exceeds the size of
604 order = min(ffsl(VM_PAGE_TO_PHYS(m) >> PAGE_SHIFT) - 1,
609 vm_phys_free_pages(m, order);
612 /* The residual "npages" is less than "1 << (VM_NFREEORDER - 1)". */
613 for (; npages > 0; npages -= n) {
614 order = flsl(npages) - 1;
616 vm_phys_free_pages(m, order);
622 * Set the pool for a contiguous, power of two-sized set of physical pages.
625 vm_phys_set_pool(int pool, vm_page_t m, int order)
629 for (m_tmp = m; m_tmp < &m[1 << order]; m_tmp++)
634 * Search for the given physical page "m" in the free lists. If the search
635 * succeeds, remove "m" from the free lists and return TRUE. Otherwise, return
636 * FALSE, indicating that "m" is not in the free lists.
638 * The free page queues must be locked.
641 vm_phys_unfree_page(vm_page_t m)
643 struct vm_freelist *fl;
644 struct vm_phys_seg *seg;
645 vm_paddr_t pa, pa_half;
646 vm_page_t m_set, m_tmp;
649 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
652 * First, find the contiguous, power of two-sized set of free
653 * physical pages containing the given physical page "m" and
654 * assign it to "m_set".
656 seg = &vm_phys_segs[m->segind];
657 for (m_set = m, order = 0; m_set->order == VM_NFREEORDER &&
658 order < VM_NFREEORDER - 1; ) {
660 pa = m->phys_addr & (~(vm_paddr_t)0 << (PAGE_SHIFT + order));
661 if (pa >= seg->start)
662 m_set = &seg->first_page[atop(pa - seg->start)];
666 if (m_set->order < order)
668 if (m_set->order == VM_NFREEORDER)
670 KASSERT(m_set->order < VM_NFREEORDER,
671 ("vm_phys_unfree_page: page %p has unexpected order %d",
672 m_set, m_set->order));
675 * Next, remove "m_set" from the free lists. Finally, extract
676 * "m" from "m_set" using an iterative algorithm: While "m_set"
677 * is larger than a page, shrink "m_set" by returning the half
678 * of "m_set" that does not contain "m" to the free lists.
680 fl = (*seg->free_queues)[m_set->pool];
681 order = m_set->order;
682 TAILQ_REMOVE(&fl[order].pl, m_set, pageq);
684 m_set->order = VM_NFREEORDER;
687 pa_half = m_set->phys_addr ^ (1 << (PAGE_SHIFT + order));
688 if (m->phys_addr < pa_half)
689 m_tmp = &seg->first_page[atop(pa_half - seg->start)];
692 m_set = &seg->first_page[atop(pa_half - seg->start)];
694 m_tmp->order = order;
695 TAILQ_INSERT_HEAD(&fl[order].pl, m_tmp, pageq);
698 KASSERT(m_set == m, ("vm_phys_unfree_page: fatal inconsistency"));
703 * Try to zero one physical page. Used by an idle priority thread.
706 vm_phys_zero_pages_idle(void)
708 static struct vm_freelist *fl = vm_phys_free_queues[0][0];
709 static int flind, oind, pind;
712 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
714 TAILQ_FOREACH_REVERSE(m, &fl[oind].pl, pglist, pageq) {
715 for (m_tmp = m; m_tmp < &m[1 << oind]; m_tmp++) {
716 if ((m_tmp->flags & (PG_CACHED | PG_ZERO)) == 0) {
717 vm_phys_unfree_page(m_tmp);
719 mtx_unlock(&vm_page_queue_free_mtx);
720 pmap_zero_page_idle(m_tmp);
721 m_tmp->flags |= PG_ZERO;
722 mtx_lock(&vm_page_queue_free_mtx);
724 vm_phys_free_pages(m_tmp, 0);
725 vm_page_zero_count++;
732 if (oind == VM_NFREEORDER) {
735 if (pind == VM_NFREEPOOL) {
738 if (flind == vm_nfreelists)
741 fl = vm_phys_free_queues[flind][pind];
747 * Allocate a contiguous set of physical pages of the given size
748 * "npages" from the free lists. All of the physical pages must be at
749 * or above the given physical address "low" and below the given
750 * physical address "high". The given value "alignment" determines the
751 * alignment of the first physical page in the set. If the given value
752 * "boundary" is non-zero, then the set of physical pages cannot cross
753 * any physical address boundary that is a multiple of that value. Both
754 * "alignment" and "boundary" must be a power of two.
757 vm_phys_alloc_contig(u_long npages, vm_paddr_t low, vm_paddr_t high,
758 u_long alignment, vm_paddr_t boundary)
760 struct vm_freelist *fl;
761 struct vm_phys_seg *seg;
762 vm_paddr_t pa, pa_last, size;
765 int domain, flind, oind, order, pind;
767 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
769 domain = PCPU_GET(domain);
773 size = npages << PAGE_SHIFT;
775 ("vm_phys_alloc_contig: size must not be 0"));
776 KASSERT((alignment & (alignment - 1)) == 0,
777 ("vm_phys_alloc_contig: alignment must be a power of 2"));
778 KASSERT((boundary & (boundary - 1)) == 0,
779 ("vm_phys_alloc_contig: boundary must be a power of 2"));
780 /* Compute the queue that is the best fit for npages. */
781 for (order = 0; (1 << order) < npages; order++);
782 for (flind = 0; flind < vm_nfreelists; flind++) {
783 for (oind = min(order, VM_NFREEORDER - 1); oind < VM_NFREEORDER; oind++) {
784 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
785 fl = (*vm_phys_lookup_lists[domain][flind])
787 TAILQ_FOREACH(m_ret, &fl[oind].pl, pageq) {
789 * A free list may contain physical pages
790 * from one or more segments.
792 seg = &vm_phys_segs[m_ret->segind];
793 if (seg->start > high ||
798 * Is the size of this allocation request
799 * larger than the largest block size?
801 if (order >= VM_NFREEORDER) {
803 * Determine if a sufficient number
804 * of subsequent blocks to satisfy
805 * the allocation request are free.
807 pa = VM_PAGE_TO_PHYS(m_ret);
810 pa += 1 << (PAGE_SHIFT + VM_NFREEORDER - 1);
813 if (pa < seg->start ||
816 m = &seg->first_page[atop(pa - seg->start)];
817 if (m->order != VM_NFREEORDER - 1)
820 /* If not, continue to the next block. */
826 * Determine if the blocks are within the given range,
827 * satisfy the given alignment, and do not cross the
830 pa = VM_PAGE_TO_PHYS(m_ret);
833 (pa & (alignment - 1)) == 0 &&
834 ((pa ^ (pa + size - 1)) & ~(boundary - 1)) == 0)
842 for (m = m_ret; m < &m_ret[npages]; m = &m[1 << oind]) {
843 fl = (*seg->free_queues)[m->pool];
844 TAILQ_REMOVE(&fl[m->order].pl, m, pageq);
846 m->order = VM_NFREEORDER;
848 if (m_ret->pool != VM_FREEPOOL_DEFAULT)
849 vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m_ret, oind);
850 fl = (*seg->free_queues)[m_ret->pool];
851 vm_phys_split_pages(m_ret, oind, fl, order);
852 /* Return excess pages to the free lists. */
853 npages_end = roundup2(npages, 1 << imin(oind, order));
854 if (npages < npages_end)
855 vm_phys_free_contig(&m_ret[npages], npages_end - npages);
861 * Show the number of physical pages in each of the free lists.
863 DB_SHOW_COMMAND(freepages, db_show_freepages)
865 struct vm_freelist *fl;
866 int flind, oind, pind;
868 for (flind = 0; flind < vm_nfreelists; flind++) {
869 db_printf("FREE LIST %d:\n"
870 "\n ORDER (SIZE) | NUMBER"
872 for (pind = 0; pind < VM_NFREEPOOL; pind++)
873 db_printf(" | POOL %d", pind);
875 for (pind = 0; pind < VM_NFREEPOOL; pind++)
878 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
879 db_printf(" %2.2d (%6.6dK)", oind,
880 1 << (PAGE_SHIFT - 10 + oind));
881 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
882 fl = vm_phys_free_queues[flind][pind];
883 db_printf(" | %6.6d", fl[oind].lcnt);