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 #define VM_PHYS_FICTITIOUS_NSEGS 8
91 static struct vm_phys_fictitious_seg {
95 } vm_phys_fictitious_segs[VM_PHYS_FICTITIOUS_NSEGS];
96 static struct mtx vm_phys_fictitious_reg_mtx;
97 MALLOC_DEFINE(M_FICT_PAGES, "", "");
99 static struct vm_freelist
100 vm_phys_free_queues[VM_RAW_NFREELIST][VM_NFREEPOOL][VM_NFREEORDER];
101 static struct vm_freelist
102 (*vm_phys_lookup_lists[VM_NDOMAIN][VM_RAW_NFREELIST])[VM_NFREEPOOL][VM_NFREEORDER];
104 static int vm_nfreelists = VM_FREELIST_DEFAULT + 1;
106 static int cnt_prezero;
107 SYSCTL_INT(_vm_stats_misc, OID_AUTO, cnt_prezero, CTLFLAG_RD,
108 &cnt_prezero, 0, "The number of physical pages prezeroed at idle time");
110 static int sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS);
111 SYSCTL_OID(_vm, OID_AUTO, phys_free, CTLTYPE_STRING | CTLFLAG_RD,
112 NULL, 0, sysctl_vm_phys_free, "A", "Phys Free Info");
114 static int sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS);
115 SYSCTL_OID(_vm, OID_AUTO, phys_segs, CTLTYPE_STRING | CTLFLAG_RD,
116 NULL, 0, sysctl_vm_phys_segs, "A", "Phys Seg Info");
119 static int sysctl_vm_phys_lookup_lists(SYSCTL_HANDLER_ARGS);
120 SYSCTL_OID(_vm, OID_AUTO, phys_lookup_lists, CTLTYPE_STRING | CTLFLAG_RD,
121 NULL, 0, sysctl_vm_phys_lookup_lists, "A", "Phys Lookup Lists");
124 static void _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int flind,
126 static void vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int flind);
127 static int vm_phys_paddr_to_segind(vm_paddr_t pa);
128 static void vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl,
132 * Outputs the state of the physical memory allocator, specifically,
133 * the amount of physical memory in each free list.
136 sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS)
139 struct vm_freelist *fl;
140 int error, flind, oind, pind;
142 error = sysctl_wire_old_buffer(req, 0);
145 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
146 for (flind = 0; flind < vm_nfreelists; flind++) {
147 sbuf_printf(&sbuf, "\nFREE LIST %d:\n"
148 "\n ORDER (SIZE) | NUMBER"
150 for (pind = 0; pind < VM_NFREEPOOL; pind++)
151 sbuf_printf(&sbuf, " | POOL %d", pind);
152 sbuf_printf(&sbuf, "\n-- ");
153 for (pind = 0; pind < VM_NFREEPOOL; pind++)
154 sbuf_printf(&sbuf, "-- -- ");
155 sbuf_printf(&sbuf, "--\n");
156 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
157 sbuf_printf(&sbuf, " %2d (%6dK)", oind,
158 1 << (PAGE_SHIFT - 10 + oind));
159 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
160 fl = vm_phys_free_queues[flind][pind];
161 sbuf_printf(&sbuf, " | %6d", fl[oind].lcnt);
163 sbuf_printf(&sbuf, "\n");
166 error = sbuf_finish(&sbuf);
172 * Outputs the set of physical memory segments.
175 sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS)
178 struct vm_phys_seg *seg;
181 error = sysctl_wire_old_buffer(req, 0);
184 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
185 for (segind = 0; segind < vm_phys_nsegs; segind++) {
186 sbuf_printf(&sbuf, "\nSEGMENT %d:\n\n", segind);
187 seg = &vm_phys_segs[segind];
188 sbuf_printf(&sbuf, "start: %#jx\n",
189 (uintmax_t)seg->start);
190 sbuf_printf(&sbuf, "end: %#jx\n",
191 (uintmax_t)seg->end);
192 sbuf_printf(&sbuf, "domain: %d\n", seg->domain);
193 sbuf_printf(&sbuf, "free list: %p\n", seg->free_queues);
195 error = sbuf_finish(&sbuf);
202 * Outputs the set of free list lookup lists.
205 sysctl_vm_phys_lookup_lists(SYSCTL_HANDLER_ARGS)
208 int domain, error, flind, ndomains;
210 error = sysctl_wire_old_buffer(req, 0);
213 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
214 ndomains = vm_nfreelists - VM_NFREELIST + 1;
215 for (domain = 0; domain < ndomains; domain++) {
216 sbuf_printf(&sbuf, "\nDOMAIN %d:\n\n", domain);
217 for (flind = 0; flind < vm_nfreelists; flind++)
218 sbuf_printf(&sbuf, " [%d]:\t%p\n", flind,
219 vm_phys_lookup_lists[domain][flind]);
221 error = sbuf_finish(&sbuf);
228 * Create a physical memory segment.
231 _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int flind, int domain)
233 struct vm_phys_seg *seg;
234 #ifdef VM_PHYSSEG_SPARSE
239 for (segind = 0; segind < vm_phys_nsegs; segind++) {
240 seg = &vm_phys_segs[segind];
241 pages += atop(seg->end - seg->start);
244 KASSERT(vm_phys_nsegs < VM_PHYSSEG_MAX,
245 ("vm_phys_create_seg: increase VM_PHYSSEG_MAX"));
246 seg = &vm_phys_segs[vm_phys_nsegs++];
249 seg->domain = domain;
250 #ifdef VM_PHYSSEG_SPARSE
251 seg->first_page = &vm_page_array[pages];
253 seg->first_page = PHYS_TO_VM_PAGE(start);
256 if (flind == VM_FREELIST_DEFAULT && domain != 0) {
257 flind = VM_NFREELIST + (domain - 1);
258 if (flind >= vm_nfreelists)
259 vm_nfreelists = flind + 1;
262 seg->free_queues = &vm_phys_free_queues[flind];
266 vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int flind)
270 if (mem_affinity == NULL) {
271 _vm_phys_create_seg(start, end, flind, 0);
276 if (mem_affinity[i].end == 0)
277 panic("Reached end of affinity info");
278 if (mem_affinity[i].end <= start)
280 if (mem_affinity[i].start > start)
281 panic("No affinity info for start %jx",
283 if (mem_affinity[i].end >= end) {
284 _vm_phys_create_seg(start, end, flind,
285 mem_affinity[i].domain);
288 _vm_phys_create_seg(start, mem_affinity[i].end, flind,
289 mem_affinity[i].domain);
290 start = mem_affinity[i].end;
295 * Initialize the physical memory allocator.
300 struct vm_freelist *fl;
301 int flind, i, oind, pind;
306 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
307 #ifdef VM_FREELIST_ISADMA
308 if (phys_avail[i] < 16777216) {
309 if (phys_avail[i + 1] > 16777216) {
310 vm_phys_create_seg(phys_avail[i], 16777216,
312 vm_phys_create_seg(16777216, phys_avail[i + 1],
313 VM_FREELIST_DEFAULT);
315 vm_phys_create_seg(phys_avail[i],
316 phys_avail[i + 1], VM_FREELIST_ISADMA);
318 if (VM_FREELIST_ISADMA >= vm_nfreelists)
319 vm_nfreelists = VM_FREELIST_ISADMA + 1;
322 #ifdef VM_FREELIST_HIGHMEM
323 if (phys_avail[i + 1] > VM_HIGHMEM_ADDRESS) {
324 if (phys_avail[i] < VM_HIGHMEM_ADDRESS) {
325 vm_phys_create_seg(phys_avail[i],
326 VM_HIGHMEM_ADDRESS, VM_FREELIST_DEFAULT);
327 vm_phys_create_seg(VM_HIGHMEM_ADDRESS,
328 phys_avail[i + 1], VM_FREELIST_HIGHMEM);
330 vm_phys_create_seg(phys_avail[i],
331 phys_avail[i + 1], VM_FREELIST_HIGHMEM);
333 if (VM_FREELIST_HIGHMEM >= vm_nfreelists)
334 vm_nfreelists = VM_FREELIST_HIGHMEM + 1;
337 vm_phys_create_seg(phys_avail[i], phys_avail[i + 1],
338 VM_FREELIST_DEFAULT);
340 for (flind = 0; flind < vm_nfreelists; flind++) {
341 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
342 fl = vm_phys_free_queues[flind][pind];
343 for (oind = 0; oind < VM_NFREEORDER; oind++)
344 TAILQ_INIT(&fl[oind].pl);
349 * Build a free list lookup list for each domain. All of the
350 * memory domain lists are inserted at the VM_FREELIST_DEFAULT
351 * index in a round-robin order starting with the current
354 ndomains = vm_nfreelists - VM_NFREELIST + 1;
355 for (flind = 0; flind < VM_FREELIST_DEFAULT; flind++)
356 for (i = 0; i < ndomains; i++)
357 vm_phys_lookup_lists[i][flind] =
358 &vm_phys_free_queues[flind];
359 for (i = 0; i < ndomains; i++)
360 for (j = 0; j < ndomains; j++) {
361 flind = (i + j) % ndomains;
363 flind = VM_FREELIST_DEFAULT;
365 flind += VM_NFREELIST - 1;
366 vm_phys_lookup_lists[i][VM_FREELIST_DEFAULT + j] =
367 &vm_phys_free_queues[flind];
369 for (flind = VM_FREELIST_DEFAULT + 1; flind < VM_NFREELIST;
371 for (i = 0; i < ndomains; i++)
372 vm_phys_lookup_lists[i][flind + ndomains - 1] =
373 &vm_phys_free_queues[flind];
375 for (flind = 0; flind < vm_nfreelists; flind++)
376 vm_phys_lookup_lists[0][flind] = &vm_phys_free_queues[flind];
379 mtx_init(&vm_phys_fictitious_reg_mtx, "vmfctr", NULL, MTX_DEF);
383 * Split a contiguous, power of two-sized set of physical pages.
386 vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl, int order)
390 while (oind > order) {
392 m_buddy = &m[1 << oind];
393 KASSERT(m_buddy->order == VM_NFREEORDER,
394 ("vm_phys_split_pages: page %p has unexpected order %d",
395 m_buddy, m_buddy->order));
396 m_buddy->order = oind;
397 TAILQ_INSERT_HEAD(&fl[oind].pl, m_buddy, pageq);
403 * Initialize a physical page and add it to the free lists.
406 vm_phys_add_page(vm_paddr_t pa)
411 m = vm_phys_paddr_to_vm_page(pa);
414 m->segind = vm_phys_paddr_to_segind(pa);
416 KASSERT(m->order == VM_NFREEORDER,
417 ("vm_phys_add_page: page %p has unexpected order %d",
419 m->pool = VM_FREEPOOL_DEFAULT;
421 mtx_lock(&vm_page_queue_free_mtx);
423 vm_phys_free_pages(m, 0);
424 mtx_unlock(&vm_page_queue_free_mtx);
428 * Allocate a contiguous, power of two-sized set of physical pages
429 * from the free lists.
431 * The free page queues must be locked.
434 vm_phys_alloc_pages(int pool, int order)
439 for (flind = 0; flind < vm_nfreelists; flind++) {
440 m = vm_phys_alloc_freelist_pages(flind, pool, order);
448 * Find and dequeue a free page on the given free list, with the
449 * specified pool and order
452 vm_phys_alloc_freelist_pages(int flind, int pool, int order)
454 struct vm_freelist *fl;
455 struct vm_freelist *alt;
456 int domain, oind, pind;
459 KASSERT(flind < VM_NFREELIST,
460 ("vm_phys_alloc_freelist_pages: freelist %d is out of range", flind));
461 KASSERT(pool < VM_NFREEPOOL,
462 ("vm_phys_alloc_freelist_pages: pool %d is out of range", pool));
463 KASSERT(order < VM_NFREEORDER,
464 ("vm_phys_alloc_freelist_pages: order %d is out of range", order));
467 domain = PCPU_GET(domain);
471 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
472 fl = (*vm_phys_lookup_lists[domain][flind])[pool];
473 for (oind = order; oind < VM_NFREEORDER; oind++) {
474 m = TAILQ_FIRST(&fl[oind].pl);
476 TAILQ_REMOVE(&fl[oind].pl, m, pageq);
478 m->order = VM_NFREEORDER;
479 vm_phys_split_pages(m, oind, fl, order);
485 * The given pool was empty. Find the largest
486 * contiguous, power-of-two-sized set of pages in any
487 * pool. Transfer these pages to the given pool, and
488 * use them to satisfy the allocation.
490 for (oind = VM_NFREEORDER - 1; oind >= order; oind--) {
491 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
492 alt = (*vm_phys_lookup_lists[domain][flind])[pind];
493 m = TAILQ_FIRST(&alt[oind].pl);
495 TAILQ_REMOVE(&alt[oind].pl, m, pageq);
497 m->order = VM_NFREEORDER;
498 vm_phys_set_pool(pool, m, oind);
499 vm_phys_split_pages(m, oind, fl, order);
508 * Find the vm_page corresponding to the given physical address.
511 vm_phys_paddr_to_vm_page(vm_paddr_t pa)
513 struct vm_phys_seg *seg;
516 for (segind = 0; segind < vm_phys_nsegs; segind++) {
517 seg = &vm_phys_segs[segind];
518 if (pa >= seg->start && pa < seg->end)
519 return (&seg->first_page[atop(pa - seg->start)]);
525 vm_phys_fictitious_to_vm_page(vm_paddr_t pa)
527 struct vm_phys_fictitious_seg *seg;
532 for (segind = 0; segind < VM_PHYS_FICTITIOUS_NSEGS; segind++) {
533 seg = &vm_phys_fictitious_segs[segind];
534 if (pa >= seg->start && pa < seg->end) {
535 m = &seg->first_page[atop(pa - seg->start)];
536 KASSERT((m->flags & PG_FICTITIOUS) != 0,
537 ("%p not fictitious", m));
545 vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
546 vm_memattr_t memattr)
548 struct vm_phys_fictitious_seg *seg;
552 #ifdef VM_PHYSSEG_DENSE
557 page_count = (end - start) / PAGE_SIZE;
559 #ifdef VM_PHYSSEG_DENSE
561 if (pi >= first_page && atop(end) < vm_page_array_size) {
562 fp = &vm_page_array[pi - first_page];
567 fp = malloc(page_count * sizeof(struct vm_page), M_FICT_PAGES,
569 #ifdef VM_PHYSSEG_DENSE
573 for (i = 0; i < page_count; i++) {
574 vm_page_initfake(&fp[i], start + PAGE_SIZE * i, memattr);
575 pmap_page_init(&fp[i]);
576 fp[i].oflags &= ~(VPO_BUSY | VPO_UNMANAGED);
578 mtx_lock(&vm_phys_fictitious_reg_mtx);
579 for (segind = 0; segind < VM_PHYS_FICTITIOUS_NSEGS; segind++) {
580 seg = &vm_phys_fictitious_segs[segind];
581 if (seg->start == 0 && seg->end == 0) {
584 seg->first_page = fp;
585 mtx_unlock(&vm_phys_fictitious_reg_mtx);
589 mtx_unlock(&vm_phys_fictitious_reg_mtx);
590 #ifdef VM_PHYSSEG_DENSE
593 free(fp, M_FICT_PAGES);
598 vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end)
600 struct vm_phys_fictitious_seg *seg;
603 #ifdef VM_PHYSSEG_DENSE
607 #ifdef VM_PHYSSEG_DENSE
611 mtx_lock(&vm_phys_fictitious_reg_mtx);
612 for (segind = 0; segind < VM_PHYS_FICTITIOUS_NSEGS; segind++) {
613 seg = &vm_phys_fictitious_segs[segind];
614 if (seg->start == start && seg->end == end) {
615 seg->start = seg->end = 0;
616 fp = seg->first_page;
617 seg->first_page = NULL;
618 mtx_unlock(&vm_phys_fictitious_reg_mtx);
619 #ifdef VM_PHYSSEG_DENSE
620 if (pi < first_page || atop(end) >= vm_page_array_size)
622 free(fp, M_FICT_PAGES);
626 mtx_unlock(&vm_phys_fictitious_reg_mtx);
627 KASSERT(0, ("Unregistering not registered fictitious range"));
631 * Find the segment containing the given physical address.
634 vm_phys_paddr_to_segind(vm_paddr_t pa)
636 struct vm_phys_seg *seg;
639 for (segind = 0; segind < vm_phys_nsegs; segind++) {
640 seg = &vm_phys_segs[segind];
641 if (pa >= seg->start && pa < seg->end)
644 panic("vm_phys_paddr_to_segind: paddr %#jx is not in any segment" ,
649 * Free a contiguous, power of two-sized set of physical pages.
651 * The free page queues must be locked.
654 vm_phys_free_pages(vm_page_t m, int order)
656 struct vm_freelist *fl;
657 struct vm_phys_seg *seg;
661 KASSERT(m->order == VM_NFREEORDER,
662 ("vm_phys_free_pages: page %p has unexpected order %d",
664 KASSERT(m->pool < VM_NFREEPOOL,
665 ("vm_phys_free_pages: page %p has unexpected pool %d",
667 KASSERT(order < VM_NFREEORDER,
668 ("vm_phys_free_pages: order %d is out of range", order));
669 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
670 seg = &vm_phys_segs[m->segind];
671 if (order < VM_NFREEORDER - 1) {
672 pa = VM_PAGE_TO_PHYS(m);
674 pa ^= ((vm_paddr_t)1 << (PAGE_SHIFT + order));
675 if (pa < seg->start || pa >= seg->end)
677 m_buddy = &seg->first_page[atop(pa - seg->start)];
678 if (m_buddy->order != order)
680 fl = (*seg->free_queues)[m_buddy->pool];
681 TAILQ_REMOVE(&fl[order].pl, m_buddy, pageq);
683 m_buddy->order = VM_NFREEORDER;
684 if (m_buddy->pool != m->pool)
685 vm_phys_set_pool(m->pool, m_buddy, order);
687 pa &= ~(((vm_paddr_t)1 << (PAGE_SHIFT + order)) - 1);
688 m = &seg->first_page[atop(pa - seg->start)];
689 } while (order < VM_NFREEORDER - 1);
692 fl = (*seg->free_queues)[m->pool];
693 TAILQ_INSERT_TAIL(&fl[order].pl, m, pageq);
698 * Free a contiguous, arbitrarily sized set of physical pages.
700 * The free page queues must be locked.
703 vm_phys_free_contig(vm_page_t m, u_long npages)
709 * Avoid unnecessary coalescing by freeing the pages in the largest
710 * possible power-of-two-sized subsets.
712 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
713 for (;; npages -= n) {
715 * Unsigned "min" is used here so that "order" is assigned
716 * "VM_NFREEORDER - 1" when "m"'s physical address is zero
717 * or the low-order bits of its physical address are zero
718 * because the size of a physical address exceeds the size of
721 order = min(ffsl(VM_PAGE_TO_PHYS(m) >> PAGE_SHIFT) - 1,
726 vm_phys_free_pages(m, order);
729 /* The residual "npages" is less than "1 << (VM_NFREEORDER - 1)". */
730 for (; npages > 0; npages -= n) {
731 order = flsl(npages) - 1;
733 vm_phys_free_pages(m, order);
739 * Set the pool for a contiguous, power of two-sized set of physical pages.
742 vm_phys_set_pool(int pool, vm_page_t m, int order)
746 for (m_tmp = m; m_tmp < &m[1 << order]; m_tmp++)
751 * Search for the given physical page "m" in the free lists. If the search
752 * succeeds, remove "m" from the free lists and return TRUE. Otherwise, return
753 * FALSE, indicating that "m" is not in the free lists.
755 * The free page queues must be locked.
758 vm_phys_unfree_page(vm_page_t m)
760 struct vm_freelist *fl;
761 struct vm_phys_seg *seg;
762 vm_paddr_t pa, pa_half;
763 vm_page_t m_set, m_tmp;
766 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
769 * First, find the contiguous, power of two-sized set of free
770 * physical pages containing the given physical page "m" and
771 * assign it to "m_set".
773 seg = &vm_phys_segs[m->segind];
774 for (m_set = m, order = 0; m_set->order == VM_NFREEORDER &&
775 order < VM_NFREEORDER - 1; ) {
777 pa = m->phys_addr & (~(vm_paddr_t)0 << (PAGE_SHIFT + order));
778 if (pa >= seg->start)
779 m_set = &seg->first_page[atop(pa - seg->start)];
783 if (m_set->order < order)
785 if (m_set->order == VM_NFREEORDER)
787 KASSERT(m_set->order < VM_NFREEORDER,
788 ("vm_phys_unfree_page: page %p has unexpected order %d",
789 m_set, m_set->order));
792 * Next, remove "m_set" from the free lists. Finally, extract
793 * "m" from "m_set" using an iterative algorithm: While "m_set"
794 * is larger than a page, shrink "m_set" by returning the half
795 * of "m_set" that does not contain "m" to the free lists.
797 fl = (*seg->free_queues)[m_set->pool];
798 order = m_set->order;
799 TAILQ_REMOVE(&fl[order].pl, m_set, pageq);
801 m_set->order = VM_NFREEORDER;
804 pa_half = m_set->phys_addr ^ (1 << (PAGE_SHIFT + order));
805 if (m->phys_addr < pa_half)
806 m_tmp = &seg->first_page[atop(pa_half - seg->start)];
809 m_set = &seg->first_page[atop(pa_half - seg->start)];
811 m_tmp->order = order;
812 TAILQ_INSERT_HEAD(&fl[order].pl, m_tmp, pageq);
815 KASSERT(m_set == m, ("vm_phys_unfree_page: fatal inconsistency"));
820 * Try to zero one physical page. Used by an idle priority thread.
823 vm_phys_zero_pages_idle(void)
825 static struct vm_freelist *fl = vm_phys_free_queues[0][0];
826 static int flind, oind, pind;
829 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
831 TAILQ_FOREACH_REVERSE(m, &fl[oind].pl, pglist, pageq) {
832 for (m_tmp = m; m_tmp < &m[1 << oind]; m_tmp++) {
833 if ((m_tmp->flags & (PG_CACHED | PG_ZERO)) == 0) {
834 vm_phys_unfree_page(m_tmp);
836 mtx_unlock(&vm_page_queue_free_mtx);
837 pmap_zero_page_idle(m_tmp);
838 m_tmp->flags |= PG_ZERO;
839 mtx_lock(&vm_page_queue_free_mtx);
841 vm_phys_free_pages(m_tmp, 0);
842 vm_page_zero_count++;
849 if (oind == VM_NFREEORDER) {
852 if (pind == VM_NFREEPOOL) {
855 if (flind == vm_nfreelists)
858 fl = vm_phys_free_queues[flind][pind];
864 * Allocate a contiguous set of physical pages of the given size
865 * "npages" from the free lists. All of the physical pages must be at
866 * or above the given physical address "low" and below the given
867 * physical address "high". The given value "alignment" determines the
868 * alignment of the first physical page in the set. If the given value
869 * "boundary" is non-zero, then the set of physical pages cannot cross
870 * any physical address boundary that is a multiple of that value. Both
871 * "alignment" and "boundary" must be a power of two.
874 vm_phys_alloc_contig(u_long npages, vm_paddr_t low, vm_paddr_t high,
875 u_long alignment, vm_paddr_t boundary)
877 struct vm_freelist *fl;
878 struct vm_phys_seg *seg;
879 vm_paddr_t pa, pa_last, size;
882 int domain, flind, oind, order, pind;
884 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
886 domain = PCPU_GET(domain);
890 size = npages << PAGE_SHIFT;
892 ("vm_phys_alloc_contig: size must not be 0"));
893 KASSERT((alignment & (alignment - 1)) == 0,
894 ("vm_phys_alloc_contig: alignment must be a power of 2"));
895 KASSERT((boundary & (boundary - 1)) == 0,
896 ("vm_phys_alloc_contig: boundary must be a power of 2"));
897 /* Compute the queue that is the best fit for npages. */
898 for (order = 0; (1 << order) < npages; order++);
899 for (flind = 0; flind < vm_nfreelists; flind++) {
900 for (oind = min(order, VM_NFREEORDER - 1); oind < VM_NFREEORDER; oind++) {
901 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
902 fl = (*vm_phys_lookup_lists[domain][flind])
904 TAILQ_FOREACH(m_ret, &fl[oind].pl, pageq) {
906 * A free list may contain physical pages
907 * from one or more segments.
909 seg = &vm_phys_segs[m_ret->segind];
910 if (seg->start > high ||
915 * Is the size of this allocation request
916 * larger than the largest block size?
918 if (order >= VM_NFREEORDER) {
920 * Determine if a sufficient number
921 * of subsequent blocks to satisfy
922 * the allocation request are free.
924 pa = VM_PAGE_TO_PHYS(m_ret);
927 pa += 1 << (PAGE_SHIFT + VM_NFREEORDER - 1);
930 if (pa < seg->start ||
933 m = &seg->first_page[atop(pa - seg->start)];
934 if (m->order != VM_NFREEORDER - 1)
937 /* If not, continue to the next block. */
943 * Determine if the blocks are within the given range,
944 * satisfy the given alignment, and do not cross the
947 pa = VM_PAGE_TO_PHYS(m_ret);
950 (pa & (alignment - 1)) == 0 &&
951 ((pa ^ (pa + size - 1)) & ~(boundary - 1)) == 0)
959 for (m = m_ret; m < &m_ret[npages]; m = &m[1 << oind]) {
960 fl = (*seg->free_queues)[m->pool];
961 TAILQ_REMOVE(&fl[m->order].pl, m, pageq);
963 m->order = VM_NFREEORDER;
965 if (m_ret->pool != VM_FREEPOOL_DEFAULT)
966 vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m_ret, oind);
967 fl = (*seg->free_queues)[m_ret->pool];
968 vm_phys_split_pages(m_ret, oind, fl, order);
969 /* Return excess pages to the free lists. */
970 npages_end = roundup2(npages, 1 << imin(oind, order));
971 if (npages < npages_end)
972 vm_phys_free_contig(&m_ret[npages], npages_end - npages);
978 * Show the number of physical pages in each of the free lists.
980 DB_SHOW_COMMAND(freepages, db_show_freepages)
982 struct vm_freelist *fl;
983 int flind, oind, pind;
985 for (flind = 0; flind < vm_nfreelists; flind++) {
986 db_printf("FREE LIST %d:\n"
987 "\n ORDER (SIZE) | NUMBER"
989 for (pind = 0; pind < VM_NFREEPOOL; pind++)
990 db_printf(" | POOL %d", pind);
992 for (pind = 0; pind < VM_NFREEPOOL; pind++)
995 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
996 db_printf(" %2.2d (%6.6dK)", oind,
997 1 << (PAGE_SHIFT - 10 + oind));
998 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
999 fl = vm_phys_free_queues[flind][pind];
1000 db_printf(" | %6.6d", fl[oind].lcnt);