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>
51 #include <sys/queue.h>
53 #include <sys/sysctl.h>
54 #include <sys/vmmeter.h>
59 #include <vm/vm_param.h>
60 #include <vm/vm_kern.h>
61 #include <vm/vm_object.h>
62 #include <vm/vm_page.h>
63 #include <vm/vm_phys.h>
66 * VM_FREELIST_DEFAULT is split into VM_NDOMAIN lists, one for each
67 * domain. These extra lists are stored at the end of the regular
68 * free lists starting with VM_NFREELIST.
70 #define VM_RAW_NFREELIST (VM_NFREELIST + VM_NDOMAIN - 1)
82 struct vm_freelist (*free_queues)[VM_NFREEPOOL][VM_NFREEORDER];
85 struct mem_affinity *mem_affinity;
87 static struct vm_phys_seg vm_phys_segs[VM_PHYSSEG_MAX];
89 static int vm_phys_nsegs;
91 #define VM_PHYS_FICTITIOUS_NSEGS 8
92 static struct vm_phys_fictitious_seg {
96 } vm_phys_fictitious_segs[VM_PHYS_FICTITIOUS_NSEGS];
97 static struct mtx vm_phys_fictitious_reg_mtx;
98 MALLOC_DEFINE(M_FICT_PAGES, "", "");
100 static struct vm_freelist
101 vm_phys_free_queues[VM_RAW_NFREELIST][VM_NFREEPOOL][VM_NFREEORDER];
102 static struct vm_freelist
103 (*vm_phys_lookup_lists[VM_NDOMAIN][VM_RAW_NFREELIST])[VM_NFREEPOOL][VM_NFREEORDER];
105 static int vm_nfreelists = VM_FREELIST_DEFAULT + 1;
107 static int cnt_prezero;
108 SYSCTL_INT(_vm_stats_misc, OID_AUTO, cnt_prezero, CTLFLAG_RD,
109 &cnt_prezero, 0, "The number of physical pages prezeroed at idle time");
111 static int sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS);
112 SYSCTL_OID(_vm, OID_AUTO, phys_free, CTLTYPE_STRING | CTLFLAG_RD,
113 NULL, 0, sysctl_vm_phys_free, "A", "Phys Free Info");
115 static int sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS);
116 SYSCTL_OID(_vm, OID_AUTO, phys_segs, CTLTYPE_STRING | CTLFLAG_RD,
117 NULL, 0, sysctl_vm_phys_segs, "A", "Phys Seg Info");
120 static int sysctl_vm_phys_lookup_lists(SYSCTL_HANDLER_ARGS);
121 SYSCTL_OID(_vm, OID_AUTO, phys_lookup_lists, CTLTYPE_STRING | CTLFLAG_RD,
122 NULL, 0, sysctl_vm_phys_lookup_lists, "A", "Phys Lookup Lists");
125 static void _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int flind,
127 static void vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int flind);
128 static int vm_phys_paddr_to_segind(vm_paddr_t pa);
129 static void vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl,
133 * Outputs the state of the physical memory allocator, specifically,
134 * the amount of physical memory in each free list.
137 sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS)
140 struct vm_freelist *fl;
141 int error, flind, oind, pind;
143 error = sysctl_wire_old_buffer(req, 0);
146 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
147 for (flind = 0; flind < vm_nfreelists; flind++) {
148 sbuf_printf(&sbuf, "\nFREE LIST %d:\n"
149 "\n ORDER (SIZE) | NUMBER"
151 for (pind = 0; pind < VM_NFREEPOOL; pind++)
152 sbuf_printf(&sbuf, " | POOL %d", pind);
153 sbuf_printf(&sbuf, "\n-- ");
154 for (pind = 0; pind < VM_NFREEPOOL; pind++)
155 sbuf_printf(&sbuf, "-- -- ");
156 sbuf_printf(&sbuf, "--\n");
157 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
158 sbuf_printf(&sbuf, " %2d (%6dK)", oind,
159 1 << (PAGE_SHIFT - 10 + oind));
160 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
161 fl = vm_phys_free_queues[flind][pind];
162 sbuf_printf(&sbuf, " | %6d", fl[oind].lcnt);
164 sbuf_printf(&sbuf, "\n");
167 error = sbuf_finish(&sbuf);
173 * Outputs the set of physical memory segments.
176 sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS)
179 struct vm_phys_seg *seg;
182 error = sysctl_wire_old_buffer(req, 0);
185 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
186 for (segind = 0; segind < vm_phys_nsegs; segind++) {
187 sbuf_printf(&sbuf, "\nSEGMENT %d:\n\n", segind);
188 seg = &vm_phys_segs[segind];
189 sbuf_printf(&sbuf, "start: %#jx\n",
190 (uintmax_t)seg->start);
191 sbuf_printf(&sbuf, "end: %#jx\n",
192 (uintmax_t)seg->end);
193 sbuf_printf(&sbuf, "domain: %d\n", seg->domain);
194 sbuf_printf(&sbuf, "free list: %p\n", seg->free_queues);
196 error = sbuf_finish(&sbuf);
203 * Outputs the set of free list lookup lists.
206 sysctl_vm_phys_lookup_lists(SYSCTL_HANDLER_ARGS)
209 int domain, error, flind, ndomains;
211 error = sysctl_wire_old_buffer(req, 0);
214 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
215 ndomains = vm_nfreelists - VM_NFREELIST + 1;
216 for (domain = 0; domain < ndomains; domain++) {
217 sbuf_printf(&sbuf, "\nDOMAIN %d:\n\n", domain);
218 for (flind = 0; flind < vm_nfreelists; flind++)
219 sbuf_printf(&sbuf, " [%d]:\t%p\n", flind,
220 vm_phys_lookup_lists[domain][flind]);
222 error = sbuf_finish(&sbuf);
229 * Create a physical memory segment.
232 _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int flind, int domain)
234 struct vm_phys_seg *seg;
235 #ifdef VM_PHYSSEG_SPARSE
240 for (segind = 0; segind < vm_phys_nsegs; segind++) {
241 seg = &vm_phys_segs[segind];
242 pages += atop(seg->end - seg->start);
245 KASSERT(vm_phys_nsegs < VM_PHYSSEG_MAX,
246 ("vm_phys_create_seg: increase VM_PHYSSEG_MAX"));
247 seg = &vm_phys_segs[vm_phys_nsegs++];
250 seg->domain = domain;
251 #ifdef VM_PHYSSEG_SPARSE
252 seg->first_page = &vm_page_array[pages];
254 seg->first_page = PHYS_TO_VM_PAGE(start);
257 if (flind == VM_FREELIST_DEFAULT && domain != 0) {
258 flind = VM_NFREELIST + (domain - 1);
259 if (flind >= vm_nfreelists)
260 vm_nfreelists = flind + 1;
263 seg->free_queues = &vm_phys_free_queues[flind];
267 vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int flind)
271 if (mem_affinity == NULL) {
272 _vm_phys_create_seg(start, end, flind, 0);
277 if (mem_affinity[i].end == 0)
278 panic("Reached end of affinity info");
279 if (mem_affinity[i].end <= start)
281 if (mem_affinity[i].start > start)
282 panic("No affinity info for start %jx",
284 if (mem_affinity[i].end >= end) {
285 _vm_phys_create_seg(start, end, flind,
286 mem_affinity[i].domain);
289 _vm_phys_create_seg(start, mem_affinity[i].end, flind,
290 mem_affinity[i].domain);
291 start = mem_affinity[i].end;
296 * Initialize the physical memory allocator.
301 struct vm_freelist *fl;
302 int flind, i, oind, pind;
307 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
308 #ifdef VM_FREELIST_ISADMA
309 if (phys_avail[i] < 16777216) {
310 if (phys_avail[i + 1] > 16777216) {
311 vm_phys_create_seg(phys_avail[i], 16777216,
313 vm_phys_create_seg(16777216, phys_avail[i + 1],
314 VM_FREELIST_DEFAULT);
316 vm_phys_create_seg(phys_avail[i],
317 phys_avail[i + 1], VM_FREELIST_ISADMA);
319 if (VM_FREELIST_ISADMA >= vm_nfreelists)
320 vm_nfreelists = VM_FREELIST_ISADMA + 1;
323 #ifdef VM_FREELIST_HIGHMEM
324 if (phys_avail[i + 1] > VM_HIGHMEM_ADDRESS) {
325 if (phys_avail[i] < VM_HIGHMEM_ADDRESS) {
326 vm_phys_create_seg(phys_avail[i],
327 VM_HIGHMEM_ADDRESS, VM_FREELIST_DEFAULT);
328 vm_phys_create_seg(VM_HIGHMEM_ADDRESS,
329 phys_avail[i + 1], VM_FREELIST_HIGHMEM);
331 vm_phys_create_seg(phys_avail[i],
332 phys_avail[i + 1], VM_FREELIST_HIGHMEM);
334 if (VM_FREELIST_HIGHMEM >= vm_nfreelists)
335 vm_nfreelists = VM_FREELIST_HIGHMEM + 1;
338 vm_phys_create_seg(phys_avail[i], phys_avail[i + 1],
339 VM_FREELIST_DEFAULT);
341 for (flind = 0; flind < vm_nfreelists; flind++) {
342 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
343 fl = vm_phys_free_queues[flind][pind];
344 for (oind = 0; oind < VM_NFREEORDER; oind++)
345 TAILQ_INIT(&fl[oind].pl);
350 * Build a free list lookup list for each domain. All of the
351 * memory domain lists are inserted at the VM_FREELIST_DEFAULT
352 * index in a round-robin order starting with the current
355 ndomains = vm_nfreelists - VM_NFREELIST + 1;
356 for (flind = 0; flind < VM_FREELIST_DEFAULT; flind++)
357 for (i = 0; i < ndomains; i++)
358 vm_phys_lookup_lists[i][flind] =
359 &vm_phys_free_queues[flind];
360 for (i = 0; i < ndomains; i++)
361 for (j = 0; j < ndomains; j++) {
362 flind = (i + j) % ndomains;
364 flind = VM_FREELIST_DEFAULT;
366 flind += VM_NFREELIST - 1;
367 vm_phys_lookup_lists[i][VM_FREELIST_DEFAULT + j] =
368 &vm_phys_free_queues[flind];
370 for (flind = VM_FREELIST_DEFAULT + 1; flind < VM_NFREELIST;
372 for (i = 0; i < ndomains; i++)
373 vm_phys_lookup_lists[i][flind + ndomains - 1] =
374 &vm_phys_free_queues[flind];
376 for (flind = 0; flind < vm_nfreelists; flind++)
377 vm_phys_lookup_lists[0][flind] = &vm_phys_free_queues[flind];
380 mtx_init(&vm_phys_fictitious_reg_mtx, "vmfctr", NULL, MTX_DEF);
384 * Split a contiguous, power of two-sized set of physical pages.
387 vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl, int order)
391 while (oind > order) {
393 m_buddy = &m[1 << oind];
394 KASSERT(m_buddy->order == VM_NFREEORDER,
395 ("vm_phys_split_pages: page %p has unexpected order %d",
396 m_buddy, m_buddy->order));
397 m_buddy->order = oind;
398 TAILQ_INSERT_HEAD(&fl[oind].pl, m_buddy, pageq);
404 * Initialize a physical page and add it to the free lists.
407 vm_phys_add_page(vm_paddr_t pa)
412 m = vm_phys_paddr_to_vm_page(pa);
415 m->segind = vm_phys_paddr_to_segind(pa);
417 KASSERT(m->order == VM_NFREEORDER,
418 ("vm_phys_add_page: page %p has unexpected order %d",
420 m->pool = VM_FREEPOOL_DEFAULT;
422 mtx_lock(&vm_page_queue_free_mtx);
424 vm_phys_free_pages(m, 0);
425 mtx_unlock(&vm_page_queue_free_mtx);
429 * Allocate a contiguous, power of two-sized set of physical pages
430 * from the free lists.
432 * The free page queues must be locked.
435 vm_phys_alloc_pages(int pool, int order)
440 for (flind = 0; flind < vm_nfreelists; flind++) {
441 m = vm_phys_alloc_freelist_pages(flind, pool, order);
449 * Find and dequeue a free page on the given free list, with the
450 * specified pool and order
453 vm_phys_alloc_freelist_pages(int flind, int pool, int order)
455 struct vm_freelist *fl;
456 struct vm_freelist *alt;
457 int domain, oind, pind;
460 KASSERT(flind < VM_NFREELIST,
461 ("vm_phys_alloc_freelist_pages: freelist %d is out of range", flind));
462 KASSERT(pool < VM_NFREEPOOL,
463 ("vm_phys_alloc_freelist_pages: pool %d is out of range", pool));
464 KASSERT(order < VM_NFREEORDER,
465 ("vm_phys_alloc_freelist_pages: order %d is out of range", order));
468 domain = PCPU_GET(domain);
472 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
473 fl = (*vm_phys_lookup_lists[domain][flind])[pool];
474 for (oind = order; oind < VM_NFREEORDER; oind++) {
475 m = TAILQ_FIRST(&fl[oind].pl);
477 TAILQ_REMOVE(&fl[oind].pl, m, pageq);
479 m->order = VM_NFREEORDER;
480 vm_phys_split_pages(m, oind, fl, order);
486 * The given pool was empty. Find the largest
487 * contiguous, power-of-two-sized set of pages in any
488 * pool. Transfer these pages to the given pool, and
489 * use them to satisfy the allocation.
491 for (oind = VM_NFREEORDER - 1; oind >= order; oind--) {
492 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
493 alt = (*vm_phys_lookup_lists[domain][flind])[pind];
494 m = TAILQ_FIRST(&alt[oind].pl);
496 TAILQ_REMOVE(&alt[oind].pl, m, pageq);
498 m->order = VM_NFREEORDER;
499 vm_phys_set_pool(pool, m, oind);
500 vm_phys_split_pages(m, oind, fl, order);
509 * Find the vm_page corresponding to the given physical address.
512 vm_phys_paddr_to_vm_page(vm_paddr_t pa)
514 struct vm_phys_seg *seg;
517 for (segind = 0; segind < vm_phys_nsegs; segind++) {
518 seg = &vm_phys_segs[segind];
519 if (pa >= seg->start && pa < seg->end)
520 return (&seg->first_page[atop(pa - seg->start)]);
526 vm_phys_fictitious_to_vm_page(vm_paddr_t pa)
528 struct vm_phys_fictitious_seg *seg;
533 for (segind = 0; segind < VM_PHYS_FICTITIOUS_NSEGS; segind++) {
534 seg = &vm_phys_fictitious_segs[segind];
535 if (pa >= seg->start && pa < seg->end) {
536 m = &seg->first_page[atop(pa - seg->start)];
537 KASSERT((m->flags & PG_FICTITIOUS) != 0,
538 ("%p not fictitious", m));
546 vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
547 vm_memattr_t memattr)
549 struct vm_phys_fictitious_seg *seg;
553 #ifdef VM_PHYSSEG_DENSE
558 page_count = (end - start) / PAGE_SIZE;
560 #ifdef VM_PHYSSEG_DENSE
562 if (pi >= first_page && atop(end) < vm_page_array_size) {
563 fp = &vm_page_array[pi - first_page];
568 fp = malloc(page_count * sizeof(struct vm_page), M_FICT_PAGES,
570 #ifdef VM_PHYSSEG_DENSE
574 for (i = 0; i < page_count; i++) {
575 vm_page_initfake(&fp[i], start + PAGE_SIZE * i, memattr);
576 pmap_page_init(&fp[i]);
577 fp[i].oflags &= ~(VPO_BUSY | VPO_UNMANAGED);
579 mtx_lock(&vm_phys_fictitious_reg_mtx);
580 for (segind = 0; segind < VM_PHYS_FICTITIOUS_NSEGS; segind++) {
581 seg = &vm_phys_fictitious_segs[segind];
582 if (seg->start == 0 && seg->end == 0) {
585 seg->first_page = fp;
586 mtx_unlock(&vm_phys_fictitious_reg_mtx);
590 mtx_unlock(&vm_phys_fictitious_reg_mtx);
591 #ifdef VM_PHYSSEG_DENSE
594 free(fp, M_FICT_PAGES);
599 vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end)
601 struct vm_phys_fictitious_seg *seg;
604 #ifdef VM_PHYSSEG_DENSE
608 #ifdef VM_PHYSSEG_DENSE
612 mtx_lock(&vm_phys_fictitious_reg_mtx);
613 for (segind = 0; segind < VM_PHYS_FICTITIOUS_NSEGS; segind++) {
614 seg = &vm_phys_fictitious_segs[segind];
615 if (seg->start == start && seg->end == end) {
616 seg->start = seg->end = 0;
617 fp = seg->first_page;
618 seg->first_page = NULL;
619 mtx_unlock(&vm_phys_fictitious_reg_mtx);
620 #ifdef VM_PHYSSEG_DENSE
621 if (pi < first_page || atop(end) >= vm_page_array_size)
623 free(fp, M_FICT_PAGES);
627 mtx_unlock(&vm_phys_fictitious_reg_mtx);
628 KASSERT(0, ("Unregistering not registered fictitious range"));
632 * Find the segment containing the given physical address.
635 vm_phys_paddr_to_segind(vm_paddr_t pa)
637 struct vm_phys_seg *seg;
640 for (segind = 0; segind < vm_phys_nsegs; segind++) {
641 seg = &vm_phys_segs[segind];
642 if (pa >= seg->start && pa < seg->end)
645 panic("vm_phys_paddr_to_segind: paddr %#jx is not in any segment" ,
650 * Free a contiguous, power of two-sized set of physical pages.
652 * The free page queues must be locked.
655 vm_phys_free_pages(vm_page_t m, int order)
657 struct vm_freelist *fl;
658 struct vm_phys_seg *seg;
662 KASSERT(m->order == VM_NFREEORDER,
663 ("vm_phys_free_pages: page %p has unexpected order %d",
665 KASSERT(m->pool < VM_NFREEPOOL,
666 ("vm_phys_free_pages: page %p has unexpected pool %d",
668 KASSERT(order < VM_NFREEORDER,
669 ("vm_phys_free_pages: order %d is out of range", order));
670 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
671 seg = &vm_phys_segs[m->segind];
672 if (order < VM_NFREEORDER - 1) {
673 pa = VM_PAGE_TO_PHYS(m);
675 pa ^= ((vm_paddr_t)1 << (PAGE_SHIFT + order));
676 if (pa < seg->start || pa >= seg->end)
678 m_buddy = &seg->first_page[atop(pa - seg->start)];
679 if (m_buddy->order != order)
681 fl = (*seg->free_queues)[m_buddy->pool];
682 TAILQ_REMOVE(&fl[order].pl, m_buddy, pageq);
684 m_buddy->order = VM_NFREEORDER;
685 if (m_buddy->pool != m->pool)
686 vm_phys_set_pool(m->pool, m_buddy, order);
688 pa &= ~(((vm_paddr_t)1 << (PAGE_SHIFT + order)) - 1);
689 m = &seg->first_page[atop(pa - seg->start)];
690 } while (order < VM_NFREEORDER - 1);
693 fl = (*seg->free_queues)[m->pool];
694 TAILQ_INSERT_TAIL(&fl[order].pl, m, pageq);
699 * Free a contiguous, arbitrarily sized set of physical pages.
701 * The free page queues must be locked.
704 vm_phys_free_contig(vm_page_t m, u_long npages)
710 * Avoid unnecessary coalescing by freeing the pages in the largest
711 * possible power-of-two-sized subsets.
713 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
714 for (;; npages -= n) {
716 * Unsigned "min" is used here so that "order" is assigned
717 * "VM_NFREEORDER - 1" when "m"'s physical address is zero
718 * or the low-order bits of its physical address are zero
719 * because the size of a physical address exceeds the size of
722 order = min(ffsl(VM_PAGE_TO_PHYS(m) >> PAGE_SHIFT) - 1,
727 vm_phys_free_pages(m, order);
730 /* The residual "npages" is less than "1 << (VM_NFREEORDER - 1)". */
731 for (; npages > 0; npages -= n) {
732 order = flsl(npages) - 1;
734 vm_phys_free_pages(m, order);
740 * Set the pool for a contiguous, power of two-sized set of physical pages.
743 vm_phys_set_pool(int pool, vm_page_t m, int order)
747 for (m_tmp = m; m_tmp < &m[1 << order]; m_tmp++)
752 * Search for the given physical page "m" in the free lists. If the search
753 * succeeds, remove "m" from the free lists and return TRUE. Otherwise, return
754 * FALSE, indicating that "m" is not in the free lists.
756 * The free page queues must be locked.
759 vm_phys_unfree_page(vm_page_t m)
761 struct vm_freelist *fl;
762 struct vm_phys_seg *seg;
763 vm_paddr_t pa, pa_half;
764 vm_page_t m_set, m_tmp;
767 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
770 * First, find the contiguous, power of two-sized set of free
771 * physical pages containing the given physical page "m" and
772 * assign it to "m_set".
774 seg = &vm_phys_segs[m->segind];
775 for (m_set = m, order = 0; m_set->order == VM_NFREEORDER &&
776 order < VM_NFREEORDER - 1; ) {
778 pa = m->phys_addr & (~(vm_paddr_t)0 << (PAGE_SHIFT + order));
779 if (pa >= seg->start)
780 m_set = &seg->first_page[atop(pa - seg->start)];
784 if (m_set->order < order)
786 if (m_set->order == VM_NFREEORDER)
788 KASSERT(m_set->order < VM_NFREEORDER,
789 ("vm_phys_unfree_page: page %p has unexpected order %d",
790 m_set, m_set->order));
793 * Next, remove "m_set" from the free lists. Finally, extract
794 * "m" from "m_set" using an iterative algorithm: While "m_set"
795 * is larger than a page, shrink "m_set" by returning the half
796 * of "m_set" that does not contain "m" to the free lists.
798 fl = (*seg->free_queues)[m_set->pool];
799 order = m_set->order;
800 TAILQ_REMOVE(&fl[order].pl, m_set, pageq);
802 m_set->order = VM_NFREEORDER;
805 pa_half = m_set->phys_addr ^ (1 << (PAGE_SHIFT + order));
806 if (m->phys_addr < pa_half)
807 m_tmp = &seg->first_page[atop(pa_half - seg->start)];
810 m_set = &seg->first_page[atop(pa_half - seg->start)];
812 m_tmp->order = order;
813 TAILQ_INSERT_HEAD(&fl[order].pl, m_tmp, pageq);
816 KASSERT(m_set == m, ("vm_phys_unfree_page: fatal inconsistency"));
821 * Try to zero one physical page. Used by an idle priority thread.
824 vm_phys_zero_pages_idle(void)
826 static struct vm_freelist *fl = vm_phys_free_queues[0][0];
827 static int flind, oind, pind;
830 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
832 TAILQ_FOREACH_REVERSE(m, &fl[oind].pl, pglist, pageq) {
833 for (m_tmp = m; m_tmp < &m[1 << oind]; m_tmp++) {
834 if ((m_tmp->flags & (PG_CACHED | PG_ZERO)) == 0) {
835 vm_phys_unfree_page(m_tmp);
837 mtx_unlock(&vm_page_queue_free_mtx);
838 pmap_zero_page_idle(m_tmp);
839 m_tmp->flags |= PG_ZERO;
840 mtx_lock(&vm_page_queue_free_mtx);
842 vm_phys_free_pages(m_tmp, 0);
843 vm_page_zero_count++;
850 if (oind == VM_NFREEORDER) {
853 if (pind == VM_NFREEPOOL) {
856 if (flind == vm_nfreelists)
859 fl = vm_phys_free_queues[flind][pind];
865 * Allocate a contiguous set of physical pages of the given size
866 * "npages" from the free lists. All of the physical pages must be at
867 * or above the given physical address "low" and below the given
868 * physical address "high". The given value "alignment" determines the
869 * alignment of the first physical page in the set. If the given value
870 * "boundary" is non-zero, then the set of physical pages cannot cross
871 * any physical address boundary that is a multiple of that value. Both
872 * "alignment" and "boundary" must be a power of two.
875 vm_phys_alloc_contig(u_long npages, vm_paddr_t low, vm_paddr_t high,
876 u_long alignment, vm_paddr_t boundary)
878 struct vm_freelist *fl;
879 struct vm_phys_seg *seg;
880 vm_paddr_t pa, pa_last, size;
883 int domain, flind, oind, order, pind;
885 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
887 domain = PCPU_GET(domain);
891 size = npages << PAGE_SHIFT;
893 ("vm_phys_alloc_contig: size must not be 0"));
894 KASSERT((alignment & (alignment - 1)) == 0,
895 ("vm_phys_alloc_contig: alignment must be a power of 2"));
896 KASSERT((boundary & (boundary - 1)) == 0,
897 ("vm_phys_alloc_contig: boundary must be a power of 2"));
898 /* Compute the queue that is the best fit for npages. */
899 for (order = 0; (1 << order) < npages; order++);
900 for (flind = 0; flind < vm_nfreelists; flind++) {
901 for (oind = min(order, VM_NFREEORDER - 1); oind < VM_NFREEORDER; oind++) {
902 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
903 fl = (*vm_phys_lookup_lists[domain][flind])
905 TAILQ_FOREACH(m_ret, &fl[oind].pl, pageq) {
907 * A free list may contain physical pages
908 * from one or more segments.
910 seg = &vm_phys_segs[m_ret->segind];
911 if (seg->start > high ||
916 * Is the size of this allocation request
917 * larger than the largest block size?
919 if (order >= VM_NFREEORDER) {
921 * Determine if a sufficient number
922 * of subsequent blocks to satisfy
923 * the allocation request are free.
925 pa = VM_PAGE_TO_PHYS(m_ret);
928 pa += 1 << (PAGE_SHIFT + VM_NFREEORDER - 1);
931 if (pa < seg->start ||
934 m = &seg->first_page[atop(pa - seg->start)];
935 if (m->order != VM_NFREEORDER - 1)
938 /* If not, continue to the next block. */
944 * Determine if the blocks are within the given range,
945 * satisfy the given alignment, and do not cross the
948 pa = VM_PAGE_TO_PHYS(m_ret);
951 (pa & (alignment - 1)) == 0 &&
952 ((pa ^ (pa + size - 1)) & ~(boundary - 1)) == 0)
960 for (m = m_ret; m < &m_ret[npages]; m = &m[1 << oind]) {
961 fl = (*seg->free_queues)[m->pool];
962 TAILQ_REMOVE(&fl[m->order].pl, m, pageq);
964 m->order = VM_NFREEORDER;
966 if (m_ret->pool != VM_FREEPOOL_DEFAULT)
967 vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m_ret, oind);
968 fl = (*seg->free_queues)[m_ret->pool];
969 vm_phys_split_pages(m_ret, oind, fl, order);
970 /* Return excess pages to the free lists. */
971 npages_end = roundup2(npages, 1 << imin(oind, order));
972 if (npages < npages_end)
973 vm_phys_free_contig(&m_ret[npages], npages_end - npages);
979 * Show the number of physical pages in each of the free lists.
981 DB_SHOW_COMMAND(freepages, db_show_freepages)
983 struct vm_freelist *fl;
984 int flind, oind, pind;
986 for (flind = 0; flind < vm_nfreelists; flind++) {
987 db_printf("FREE LIST %d:\n"
988 "\n ORDER (SIZE) | NUMBER"
990 for (pind = 0; pind < VM_NFREEPOOL; pind++)
991 db_printf(" | POOL %d", pind);
993 for (pind = 0; pind < VM_NFREEPOOL; pind++)
996 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
997 db_printf(" %2.2d (%6.6dK)", oind,
998 1 << (PAGE_SHIFT - 10 + oind));
999 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
1000 fl = vm_phys_free_queues[flind][pind];
1001 db_printf(" | %6.6d", fl[oind].lcnt);