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.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/mutex.h>
43 #include <sys/queue.h>
45 #include <sys/sysctl.h>
46 #include <sys/vmmeter.h>
47 #include <sys/vnode.h>
52 #include <vm/vm_param.h>
53 #include <vm/vm_kern.h>
54 #include <vm/vm_object.h>
55 #include <vm/vm_page.h>
56 #include <vm/vm_phys.h>
57 #include <vm/vm_reserv.h>
60 * VM_FREELIST_DEFAULT is split into VM_NDOMAIN lists, one for each
61 * domain. These extra lists are stored at the end of the regular
62 * free lists starting with VM_NFREELIST.
64 #define VM_RAW_NFREELIST (VM_NFREELIST + VM_NDOMAIN - 1)
76 struct vm_freelist (*free_queues)[VM_NFREEPOOL][VM_NFREEORDER];
79 struct mem_affinity *mem_affinity;
81 static struct vm_phys_seg vm_phys_segs[VM_PHYSSEG_MAX];
83 static int vm_phys_nsegs;
85 static struct vm_freelist
86 vm_phys_free_queues[VM_RAW_NFREELIST][VM_NFREEPOOL][VM_NFREEORDER];
87 static struct vm_freelist
88 (*vm_phys_lookup_lists[VM_NDOMAIN][VM_RAW_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");
105 static int sysctl_vm_phys_lookup_lists(SYSCTL_HANDLER_ARGS);
106 SYSCTL_OID(_vm, OID_AUTO, phys_lookup_lists, CTLTYPE_STRING | CTLFLAG_RD,
107 NULL, 0, sysctl_vm_phys_lookup_lists, "A", "Phys Lookup Lists");
110 static void _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int flind,
112 static void vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int flind);
113 static int vm_phys_paddr_to_segind(vm_paddr_t pa);
114 static void vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl,
118 * Outputs the state of the physical memory allocator, specifically,
119 * the amount of physical memory in each free list.
122 sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS)
125 struct vm_freelist *fl;
127 const int cbufsize = vm_nfreelists*(VM_NFREEORDER + 1)*81;
128 int error, flind, oind, pind;
130 cbuf = malloc(cbufsize, M_TEMP, M_WAITOK | M_ZERO);
131 sbuf_new(&sbuf, cbuf, cbufsize, SBUF_FIXEDLEN);
132 for (flind = 0; flind < vm_nfreelists; flind++) {
133 sbuf_printf(&sbuf, "\nFREE LIST %d:\n"
134 "\n ORDER (SIZE) | NUMBER"
136 for (pind = 0; pind < VM_NFREEPOOL; pind++)
137 sbuf_printf(&sbuf, " | POOL %d", pind);
138 sbuf_printf(&sbuf, "\n-- ");
139 for (pind = 0; pind < VM_NFREEPOOL; pind++)
140 sbuf_printf(&sbuf, "-- -- ");
141 sbuf_printf(&sbuf, "--\n");
142 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
143 sbuf_printf(&sbuf, " %2.2d (%6.6dK)", oind,
144 1 << (PAGE_SHIFT - 10 + oind));
145 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
146 fl = vm_phys_free_queues[flind][pind];
147 sbuf_printf(&sbuf, " | %6.6d", fl[oind].lcnt);
149 sbuf_printf(&sbuf, "\n");
153 error = SYSCTL_OUT(req, sbuf_data(&sbuf), sbuf_len(&sbuf));
160 * Outputs the set of physical memory segments.
163 sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS)
166 struct vm_phys_seg *seg;
168 const int cbufsize = VM_PHYSSEG_MAX*(VM_NFREEORDER + 1)*81;
171 cbuf = malloc(cbufsize, M_TEMP, M_WAITOK | M_ZERO);
172 sbuf_new(&sbuf, cbuf, cbufsize, SBUF_FIXEDLEN);
173 for (segind = 0; segind < vm_phys_nsegs; segind++) {
174 sbuf_printf(&sbuf, "\nSEGMENT %d:\n\n", segind);
175 seg = &vm_phys_segs[segind];
176 sbuf_printf(&sbuf, "start: %#jx\n",
177 (uintmax_t)seg->start);
178 sbuf_printf(&sbuf, "end: %#jx\n",
179 (uintmax_t)seg->end);
180 sbuf_printf(&sbuf, "domain: %d\n", seg->domain);
181 sbuf_printf(&sbuf, "free list: %p\n", seg->free_queues);
184 error = SYSCTL_OUT(req, sbuf_data(&sbuf), sbuf_len(&sbuf));
192 * Outputs the set of free list lookup lists.
195 sysctl_vm_phys_lookup_lists(SYSCTL_HANDLER_ARGS)
199 const int cbufsize = (vm_nfreelists + 1) * VM_NDOMAIN * 81;
200 int domain, error, flind, ndomains;
202 ndomains = vm_nfreelists - VM_NFREELIST + 1;
203 cbuf = malloc(cbufsize, M_TEMP, M_WAITOK | M_ZERO);
204 sbuf_new(&sbuf, cbuf, cbufsize, SBUF_FIXEDLEN);
205 for (domain = 0; domain < ndomains; domain++) {
206 sbuf_printf(&sbuf, "\nDOMAIN %d:\n\n", domain);
207 for (flind = 0; flind < vm_nfreelists; flind++)
208 sbuf_printf(&sbuf, " [%d]:\t%p\n", flind,
209 vm_phys_lookup_lists[domain][flind]);
212 error = SYSCTL_OUT(req, sbuf_data(&sbuf), sbuf_len(&sbuf));
220 * Create a physical memory segment.
223 _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int flind, int domain)
225 struct vm_phys_seg *seg;
226 #ifdef VM_PHYSSEG_SPARSE
231 for (segind = 0; segind < vm_phys_nsegs; segind++) {
232 seg = &vm_phys_segs[segind];
233 pages += atop(seg->end - seg->start);
236 KASSERT(vm_phys_nsegs < VM_PHYSSEG_MAX,
237 ("vm_phys_create_seg: increase VM_PHYSSEG_MAX"));
238 seg = &vm_phys_segs[vm_phys_nsegs++];
241 seg->domain = domain;
242 #ifdef VM_PHYSSEG_SPARSE
243 seg->first_page = &vm_page_array[pages];
245 seg->first_page = PHYS_TO_VM_PAGE(start);
248 if (flind == VM_FREELIST_DEFAULT && domain != 0) {
249 flind = VM_NFREELIST + (domain - 1);
250 if (flind >= vm_nfreelists)
251 vm_nfreelists = flind + 1;
254 seg->free_queues = &vm_phys_free_queues[flind];
258 vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int flind)
262 if (mem_affinity == NULL) {
263 _vm_phys_create_seg(start, end, flind, 0);
268 if (mem_affinity[i].end == 0)
269 panic("Reached end of affinity info");
270 if (mem_affinity[i].end <= start)
272 if (mem_affinity[i].start > start)
273 panic("No affinity info for start %jx",
275 if (mem_affinity[i].end >= end) {
276 _vm_phys_create_seg(start, end, flind,
277 mem_affinity[i].domain);
280 _vm_phys_create_seg(start, mem_affinity[i].end, flind,
281 mem_affinity[i].domain);
282 start = mem_affinity[i].end;
287 * Initialize the physical memory allocator.
292 struct vm_freelist *fl;
293 int flind, i, oind, pind;
298 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
299 #ifdef VM_FREELIST_ISADMA
300 if (phys_avail[i] < 16777216) {
301 if (phys_avail[i + 1] > 16777216) {
302 vm_phys_create_seg(phys_avail[i], 16777216,
304 vm_phys_create_seg(16777216, phys_avail[i + 1],
305 VM_FREELIST_DEFAULT);
307 vm_phys_create_seg(phys_avail[i],
308 phys_avail[i + 1], VM_FREELIST_ISADMA);
310 if (VM_FREELIST_ISADMA >= vm_nfreelists)
311 vm_nfreelists = VM_FREELIST_ISADMA + 1;
314 #ifdef VM_FREELIST_HIGHMEM
315 if (phys_avail[i + 1] > VM_HIGHMEM_ADDRESS) {
316 if (phys_avail[i] < VM_HIGHMEM_ADDRESS) {
317 vm_phys_create_seg(phys_avail[i],
318 VM_HIGHMEM_ADDRESS, VM_FREELIST_DEFAULT);
319 vm_phys_create_seg(VM_HIGHMEM_ADDRESS,
320 phys_avail[i + 1], VM_FREELIST_HIGHMEM);
322 vm_phys_create_seg(phys_avail[i],
323 phys_avail[i + 1], VM_FREELIST_HIGHMEM);
325 if (VM_FREELIST_HIGHMEM >= vm_nfreelists)
326 vm_nfreelists = VM_FREELIST_HIGHMEM + 1;
329 vm_phys_create_seg(phys_avail[i], phys_avail[i + 1],
330 VM_FREELIST_DEFAULT);
332 for (flind = 0; flind < vm_nfreelists; flind++) {
333 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
334 fl = vm_phys_free_queues[flind][pind];
335 for (oind = 0; oind < VM_NFREEORDER; oind++)
336 TAILQ_INIT(&fl[oind].pl);
341 * Build a free list lookup list for each domain. All of the
342 * memory domain lists are inserted at the VM_FREELIST_DEFAULT
343 * index in a round-robin order starting with the current
346 ndomains = vm_nfreelists - VM_NFREELIST + 1;
347 for (flind = 0; flind < VM_FREELIST_DEFAULT; flind++)
348 for (i = 0; i < ndomains; i++)
349 vm_phys_lookup_lists[i][flind] =
350 &vm_phys_free_queues[flind];
351 for (i = 0; i < ndomains; i++)
352 for (j = 0; j < ndomains; j++) {
353 flind = (i + j) % ndomains;
355 flind = VM_FREELIST_DEFAULT;
357 flind += VM_NFREELIST - 1;
358 vm_phys_lookup_lists[i][VM_FREELIST_DEFAULT + j] =
359 &vm_phys_free_queues[flind];
361 for (flind = VM_FREELIST_DEFAULT + 1; flind < VM_NFREELIST;
363 for (i = 0; i < ndomains; i++)
364 vm_phys_lookup_lists[i][flind + ndomains - 1] =
365 &vm_phys_free_queues[flind];
367 for (flind = 0; flind < vm_nfreelists; flind++)
368 vm_phys_lookup_lists[0][flind] = &vm_phys_free_queues[flind];
373 * Split a contiguous, power of two-sized set of physical pages.
376 vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl, int order)
380 while (oind > order) {
382 m_buddy = &m[1 << oind];
383 KASSERT(m_buddy->order == VM_NFREEORDER,
384 ("vm_phys_split_pages: page %p has unexpected order %d",
385 m_buddy, m_buddy->order));
386 m_buddy->order = oind;
387 TAILQ_INSERT_HEAD(&fl[oind].pl, m_buddy, pageq);
393 * Initialize a physical page and add it to the free lists.
396 vm_phys_add_page(vm_paddr_t pa)
401 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 * Allocate physical memory from phys_avail[].
500 vm_phys_bootstrap_alloc(vm_size_t size, unsigned long alignment)
505 size = round_page(size);
506 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
507 if (phys_avail[i + 1] - phys_avail[i] < size)
510 phys_avail[i] += size;
513 panic("vm_phys_bootstrap_alloc");
517 * Find the vm_page corresponding to the given physical address.
520 vm_phys_paddr_to_vm_page(vm_paddr_t pa)
522 struct vm_phys_seg *seg;
525 for (segind = 0; segind < vm_phys_nsegs; segind++) {
526 seg = &vm_phys_segs[segind];
527 if (pa >= seg->start && pa < seg->end)
528 return (&seg->first_page[atop(pa - seg->start)]);
534 * Find the segment containing the given physical address.
537 vm_phys_paddr_to_segind(vm_paddr_t pa)
539 struct vm_phys_seg *seg;
542 for (segind = 0; segind < vm_phys_nsegs; segind++) {
543 seg = &vm_phys_segs[segind];
544 if (pa >= seg->start && pa < seg->end)
547 panic("vm_phys_paddr_to_segind: paddr %#jx is not in any segment" ,
552 * Free a contiguous, power of two-sized set of physical pages.
554 * The free page queues must be locked.
557 vm_phys_free_pages(vm_page_t m, int order)
559 struct vm_freelist *fl;
560 struct vm_phys_seg *seg;
561 vm_paddr_t pa, pa_buddy;
564 KASSERT(m->order == VM_NFREEORDER,
565 ("vm_phys_free_pages: page %p has unexpected order %d",
567 KASSERT(m->pool < VM_NFREEPOOL,
568 ("vm_phys_free_pages: page %p has unexpected pool %d",
570 KASSERT(order < VM_NFREEORDER,
571 ("vm_phys_free_pages: order %d is out of range", order));
572 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
573 pa = VM_PAGE_TO_PHYS(m);
574 seg = &vm_phys_segs[m->segind];
575 while (order < VM_NFREEORDER - 1) {
576 pa_buddy = pa ^ (1 << (PAGE_SHIFT + order));
577 if (pa_buddy < seg->start ||
578 pa_buddy >= seg->end)
580 m_buddy = &seg->first_page[atop(pa_buddy - seg->start)];
581 if (m_buddy->order != order)
583 fl = (*seg->free_queues)[m_buddy->pool];
584 TAILQ_REMOVE(&fl[m_buddy->order].pl, m_buddy, pageq);
585 fl[m_buddy->order].lcnt--;
586 m_buddy->order = VM_NFREEORDER;
587 if (m_buddy->pool != m->pool)
588 vm_phys_set_pool(m->pool, m_buddy, order);
590 pa &= ~((1 << (PAGE_SHIFT + order)) - 1);
591 m = &seg->first_page[atop(pa - seg->start)];
594 fl = (*seg->free_queues)[m->pool];
595 TAILQ_INSERT_TAIL(&fl[order].pl, m, pageq);
600 * Set the pool for a contiguous, power of two-sized set of physical pages.
603 vm_phys_set_pool(int pool, vm_page_t m, int order)
607 for (m_tmp = m; m_tmp < &m[1 << order]; m_tmp++)
612 * Search for the given physical page "m" in the free lists. If the search
613 * succeeds, remove "m" from the free lists and return TRUE. Otherwise, return
614 * FALSE, indicating that "m" is not in the free lists.
616 * The free page queues must be locked.
619 vm_phys_unfree_page(vm_page_t m)
621 struct vm_freelist *fl;
622 struct vm_phys_seg *seg;
623 vm_paddr_t pa, pa_half;
624 vm_page_t m_set, m_tmp;
627 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
630 * First, find the contiguous, power of two-sized set of free
631 * physical pages containing the given physical page "m" and
632 * assign it to "m_set".
634 seg = &vm_phys_segs[m->segind];
635 for (m_set = m, order = 0; m_set->order == VM_NFREEORDER &&
636 order < VM_NFREEORDER - 1; ) {
638 pa = m->phys_addr & (~(vm_paddr_t)0 << (PAGE_SHIFT + order));
639 if (pa >= seg->start)
640 m_set = &seg->first_page[atop(pa - seg->start)];
644 if (m_set->order < order)
646 if (m_set->order == VM_NFREEORDER)
648 KASSERT(m_set->order < VM_NFREEORDER,
649 ("vm_phys_unfree_page: page %p has unexpected order %d",
650 m_set, m_set->order));
653 * Next, remove "m_set" from the free lists. Finally, extract
654 * "m" from "m_set" using an iterative algorithm: While "m_set"
655 * is larger than a page, shrink "m_set" by returning the half
656 * of "m_set" that does not contain "m" to the free lists.
658 fl = (*seg->free_queues)[m_set->pool];
659 order = m_set->order;
660 TAILQ_REMOVE(&fl[order].pl, m_set, pageq);
662 m_set->order = VM_NFREEORDER;
665 pa_half = m_set->phys_addr ^ (1 << (PAGE_SHIFT + order));
666 if (m->phys_addr < pa_half)
667 m_tmp = &seg->first_page[atop(pa_half - seg->start)];
670 m_set = &seg->first_page[atop(pa_half - seg->start)];
672 m_tmp->order = order;
673 TAILQ_INSERT_HEAD(&fl[order].pl, m_tmp, pageq);
676 KASSERT(m_set == m, ("vm_phys_unfree_page: fatal inconsistency"));
681 * Try to zero one physical page. Used by an idle priority thread.
684 vm_phys_zero_pages_idle(void)
686 static struct vm_freelist *fl = vm_phys_free_queues[0][0];
687 static int flind, oind, pind;
690 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
692 TAILQ_FOREACH_REVERSE(m, &fl[oind].pl, pglist, pageq) {
693 for (m_tmp = m; m_tmp < &m[1 << oind]; m_tmp++) {
694 if ((m_tmp->flags & (PG_CACHED | PG_ZERO)) == 0) {
695 vm_phys_unfree_page(m_tmp);
697 mtx_unlock(&vm_page_queue_free_mtx);
698 pmap_zero_page_idle(m_tmp);
699 m_tmp->flags |= PG_ZERO;
700 mtx_lock(&vm_page_queue_free_mtx);
702 vm_phys_free_pages(m_tmp, 0);
703 vm_page_zero_count++;
710 if (oind == VM_NFREEORDER) {
713 if (pind == VM_NFREEPOOL) {
716 if (flind == vm_nfreelists)
719 fl = vm_phys_free_queues[flind][pind];
725 * Allocate a contiguous set of physical pages of the given size
726 * "npages" from the free lists. All of the physical pages must be at
727 * or above the given physical address "low" and below the given
728 * physical address "high". The given value "alignment" determines the
729 * alignment of the first physical page in the set. If the given value
730 * "boundary" is non-zero, then the set of physical pages cannot cross
731 * any physical address boundary that is a multiple of that value. Both
732 * "alignment" and "boundary" must be a power of two.
735 vm_phys_alloc_contig(unsigned long npages, vm_paddr_t low, vm_paddr_t high,
736 unsigned long alignment, unsigned long boundary)
738 struct vm_freelist *fl;
739 struct vm_phys_seg *seg;
741 vm_paddr_t pa, pa_last, size;
742 vm_page_t deferred_vdrop_list, m, m_ret;
743 int domain, flind, i, oind, order, pind;
746 domain = PCPU_GET(domain);
750 size = npages << PAGE_SHIFT;
752 ("vm_phys_alloc_contig: size must not be 0"));
753 KASSERT((alignment & (alignment - 1)) == 0,
754 ("vm_phys_alloc_contig: alignment must be a power of 2"));
755 KASSERT((boundary & (boundary - 1)) == 0,
756 ("vm_phys_alloc_contig: boundary must be a power of 2"));
757 deferred_vdrop_list = NULL;
758 /* Compute the queue that is the best fit for npages. */
759 for (order = 0; (1 << order) < npages; order++);
760 mtx_lock(&vm_page_queue_free_mtx);
761 #if VM_NRESERVLEVEL > 0
764 for (flind = 0; flind < vm_nfreelists; flind++) {
765 for (oind = min(order, VM_NFREEORDER - 1); oind < VM_NFREEORDER; oind++) {
766 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
767 fl = (*vm_phys_lookup_lists[domain][flind])
769 TAILQ_FOREACH(m_ret, &fl[oind].pl, pageq) {
771 * A free list may contain physical pages
772 * from one or more segments.
774 seg = &vm_phys_segs[m_ret->segind];
775 if (seg->start > high ||
780 * Is the size of this allocation request
781 * larger than the largest block size?
783 if (order >= VM_NFREEORDER) {
785 * Determine if a sufficient number
786 * of subsequent blocks to satisfy
787 * the allocation request are free.
789 pa = VM_PAGE_TO_PHYS(m_ret);
792 pa += 1 << (PAGE_SHIFT + VM_NFREEORDER - 1);
795 if (pa < seg->start ||
798 m = &seg->first_page[atop(pa - seg->start)];
799 if (m->order != VM_NFREEORDER - 1)
802 /* If not, continue to the next block. */
808 * Determine if the blocks are within the given range,
809 * satisfy the given alignment, and do not cross the
812 pa = VM_PAGE_TO_PHYS(m_ret);
815 (pa & (alignment - 1)) == 0 &&
816 ((pa ^ (pa + size - 1)) & ~(boundary - 1)) == 0)
822 #if VM_NRESERVLEVEL > 0
823 if (vm_reserv_reclaim_contig(size, low, high, alignment, boundary))
826 mtx_unlock(&vm_page_queue_free_mtx);
829 for (m = m_ret; m < &m_ret[npages]; m = &m[1 << oind]) {
830 fl = (*seg->free_queues)[m->pool];
831 TAILQ_REMOVE(&fl[m->order].pl, m, pageq);
833 m->order = VM_NFREEORDER;
835 if (m_ret->pool != VM_FREEPOOL_DEFAULT)
836 vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m_ret, oind);
837 fl = (*seg->free_queues)[m_ret->pool];
838 vm_phys_split_pages(m_ret, oind, fl, order);
839 for (i = 0; i < npages; i++) {
841 vp = vm_page_alloc_init(m);
844 * Enqueue the vnode for deferred vdrop().
846 * Unmanaged pages don't use "pageq", so it
847 * can be safely abused to construct a short-
848 * lived queue of vnodes.
850 m->pageq.tqe_prev = (void *)vp;
851 m->pageq.tqe_next = deferred_vdrop_list;
852 deferred_vdrop_list = m;
855 for (; i < roundup2(npages, 1 << imin(oind, order)); i++) {
857 KASSERT(m->order == VM_NFREEORDER,
858 ("vm_phys_alloc_contig: page %p has unexpected order %d",
860 vm_phys_free_pages(m, 0);
862 mtx_unlock(&vm_page_queue_free_mtx);
863 while (deferred_vdrop_list != NULL) {
864 vdrop((struct vnode *)deferred_vdrop_list->pageq.tqe_prev);
865 deferred_vdrop_list = deferred_vdrop_list->pageq.tqe_next;
872 * Show the number of physical pages in each of the free lists.
874 DB_SHOW_COMMAND(freepages, db_show_freepages)
876 struct vm_freelist *fl;
877 int flind, oind, pind;
879 for (flind = 0; flind < vm_nfreelists; flind++) {
880 db_printf("FREE LIST %d:\n"
881 "\n ORDER (SIZE) | NUMBER"
883 for (pind = 0; pind < VM_NFREEPOOL; pind++)
884 db_printf(" | POOL %d", pind);
886 for (pind = 0; pind < VM_NFREEPOOL; pind++)
889 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
890 db_printf(" %2.2d (%6.6dK)", oind,
891 1 << (PAGE_SHIFT - 10 + oind));
892 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
893 fl = vm_phys_free_queues[flind][pind];
894 db_printf(" | %6.6d", fl[oind].lcnt);