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, "vm_fictitious", "Fictitious VM 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 vm_page_t vm_phys_alloc_domain_pages(int domain, int flind, int pool,
126 static void _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int flind,
128 static void vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int flind);
129 static int vm_phys_paddr_to_segind(vm_paddr_t pa);
130 static void vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl,
134 * Outputs the state of the physical memory allocator, specifically,
135 * the amount of physical memory in each free list.
138 sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS)
141 struct vm_freelist *fl;
142 int error, flind, oind, pind;
144 error = sysctl_wire_old_buffer(req, 0);
147 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
148 for (flind = 0; flind < vm_nfreelists; flind++) {
149 sbuf_printf(&sbuf, "\nFREE LIST %d:\n"
150 "\n ORDER (SIZE) | NUMBER"
152 for (pind = 0; pind < VM_NFREEPOOL; pind++)
153 sbuf_printf(&sbuf, " | POOL %d", pind);
154 sbuf_printf(&sbuf, "\n-- ");
155 for (pind = 0; pind < VM_NFREEPOOL; pind++)
156 sbuf_printf(&sbuf, "-- -- ");
157 sbuf_printf(&sbuf, "--\n");
158 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
159 sbuf_printf(&sbuf, " %2d (%6dK)", oind,
160 1 << (PAGE_SHIFT - 10 + oind));
161 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
162 fl = vm_phys_free_queues[flind][pind];
163 sbuf_printf(&sbuf, " | %6d", fl[oind].lcnt);
165 sbuf_printf(&sbuf, "\n");
168 error = sbuf_finish(&sbuf);
174 * Outputs the set of physical memory segments.
177 sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS)
180 struct vm_phys_seg *seg;
183 error = sysctl_wire_old_buffer(req, 0);
186 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
187 for (segind = 0; segind < vm_phys_nsegs; segind++) {
188 sbuf_printf(&sbuf, "\nSEGMENT %d:\n\n", segind);
189 seg = &vm_phys_segs[segind];
190 sbuf_printf(&sbuf, "start: %#jx\n",
191 (uintmax_t)seg->start);
192 sbuf_printf(&sbuf, "end: %#jx\n",
193 (uintmax_t)seg->end);
194 sbuf_printf(&sbuf, "domain: %d\n", seg->domain);
195 sbuf_printf(&sbuf, "free list: %p\n", seg->free_queues);
197 error = sbuf_finish(&sbuf);
204 * Outputs the set of free list lookup lists.
207 sysctl_vm_phys_lookup_lists(SYSCTL_HANDLER_ARGS)
210 int domain, error, flind, ndomains;
212 error = sysctl_wire_old_buffer(req, 0);
215 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
216 ndomains = vm_nfreelists - VM_NFREELIST + 1;
217 for (domain = 0; domain < ndomains; domain++) {
218 sbuf_printf(&sbuf, "\nDOMAIN %d:\n\n", domain);
219 for (flind = 0; flind < vm_nfreelists; flind++)
220 sbuf_printf(&sbuf, " [%d]:\t%p\n", flind,
221 vm_phys_lookup_lists[domain][flind]);
223 error = sbuf_finish(&sbuf);
230 * Create a physical memory segment.
233 _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int flind, int domain)
235 struct vm_phys_seg *seg;
236 #ifdef VM_PHYSSEG_SPARSE
241 for (segind = 0; segind < vm_phys_nsegs; segind++) {
242 seg = &vm_phys_segs[segind];
243 pages += atop(seg->end - seg->start);
246 KASSERT(vm_phys_nsegs < VM_PHYSSEG_MAX,
247 ("vm_phys_create_seg: increase VM_PHYSSEG_MAX"));
248 seg = &vm_phys_segs[vm_phys_nsegs++];
251 seg->domain = domain;
252 #ifdef VM_PHYSSEG_SPARSE
253 seg->first_page = &vm_page_array[pages];
255 seg->first_page = PHYS_TO_VM_PAGE(start);
258 if (flind == VM_FREELIST_DEFAULT && domain != 0) {
259 flind = VM_NFREELIST + (domain - 1);
260 if (flind >= vm_nfreelists)
261 vm_nfreelists = flind + 1;
264 seg->free_queues = &vm_phys_free_queues[flind];
268 vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int flind)
272 if (mem_affinity == NULL) {
273 _vm_phys_create_seg(start, end, flind, 0);
278 if (mem_affinity[i].end == 0)
279 panic("Reached end of affinity info");
280 if (mem_affinity[i].end <= start)
282 if (mem_affinity[i].start > start)
283 panic("No affinity info for start %jx",
285 if (mem_affinity[i].end >= end) {
286 _vm_phys_create_seg(start, end, flind,
287 mem_affinity[i].domain);
290 _vm_phys_create_seg(start, mem_affinity[i].end, flind,
291 mem_affinity[i].domain);
292 start = mem_affinity[i].end;
297 * Initialize the physical memory allocator.
302 struct vm_freelist *fl;
303 int flind, i, oind, pind;
308 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
309 #ifdef VM_FREELIST_ISADMA
310 if (phys_avail[i] < 16777216) {
311 if (phys_avail[i + 1] > 16777216) {
312 vm_phys_create_seg(phys_avail[i], 16777216,
314 vm_phys_create_seg(16777216, phys_avail[i + 1],
315 VM_FREELIST_DEFAULT);
317 vm_phys_create_seg(phys_avail[i],
318 phys_avail[i + 1], VM_FREELIST_ISADMA);
320 if (VM_FREELIST_ISADMA >= vm_nfreelists)
321 vm_nfreelists = VM_FREELIST_ISADMA + 1;
324 #ifdef VM_FREELIST_HIGHMEM
325 if (phys_avail[i + 1] > VM_HIGHMEM_ADDRESS) {
326 if (phys_avail[i] < VM_HIGHMEM_ADDRESS) {
327 vm_phys_create_seg(phys_avail[i],
328 VM_HIGHMEM_ADDRESS, VM_FREELIST_DEFAULT);
329 vm_phys_create_seg(VM_HIGHMEM_ADDRESS,
330 phys_avail[i + 1], VM_FREELIST_HIGHMEM);
332 vm_phys_create_seg(phys_avail[i],
333 phys_avail[i + 1], VM_FREELIST_HIGHMEM);
335 if (VM_FREELIST_HIGHMEM >= vm_nfreelists)
336 vm_nfreelists = VM_FREELIST_HIGHMEM + 1;
339 vm_phys_create_seg(phys_avail[i], phys_avail[i + 1],
340 VM_FREELIST_DEFAULT);
342 for (flind = 0; flind < vm_nfreelists; flind++) {
343 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
344 fl = vm_phys_free_queues[flind][pind];
345 for (oind = 0; oind < VM_NFREEORDER; oind++)
346 TAILQ_INIT(&fl[oind].pl);
351 * Build a free list lookup list for each domain. All of the
352 * memory domain lists are inserted at the VM_FREELIST_DEFAULT
353 * index in a round-robin order starting with the current
356 ndomains = vm_nfreelists - VM_NFREELIST + 1;
357 for (flind = 0; flind < VM_FREELIST_DEFAULT; flind++)
358 for (i = 0; i < ndomains; i++)
359 vm_phys_lookup_lists[i][flind] =
360 &vm_phys_free_queues[flind];
361 for (i = 0; i < ndomains; i++)
362 for (j = 0; j < ndomains; j++) {
363 flind = (i + j) % ndomains;
365 flind = VM_FREELIST_DEFAULT;
367 flind += VM_NFREELIST - 1;
368 vm_phys_lookup_lists[i][VM_FREELIST_DEFAULT + j] =
369 &vm_phys_free_queues[flind];
371 for (flind = VM_FREELIST_DEFAULT + 1; flind < VM_NFREELIST;
373 for (i = 0; i < ndomains; i++)
374 vm_phys_lookup_lists[i][flind + ndomains - 1] =
375 &vm_phys_free_queues[flind];
377 for (flind = 0; flind < vm_nfreelists; flind++)
378 vm_phys_lookup_lists[0][flind] = &vm_phys_free_queues[flind];
381 mtx_init(&vm_phys_fictitious_reg_mtx, "vmfctr", NULL, MTX_DEF);
385 * Split a contiguous, power of two-sized set of physical pages.
388 vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl, int order)
392 while (oind > order) {
394 m_buddy = &m[1 << oind];
395 KASSERT(m_buddy->order == VM_NFREEORDER,
396 ("vm_phys_split_pages: page %p has unexpected order %d",
397 m_buddy, m_buddy->order));
398 m_buddy->order = oind;
399 TAILQ_INSERT_HEAD(&fl[oind].pl, m_buddy, pageq);
405 * Initialize a physical page and add it to the free lists.
408 vm_phys_add_page(vm_paddr_t pa)
413 m = vm_phys_paddr_to_vm_page(pa);
416 m->segind = vm_phys_paddr_to_segind(pa);
418 KASSERT(m->order == VM_NFREEORDER,
419 ("vm_phys_add_page: page %p has unexpected order %d",
421 m->pool = VM_FREEPOOL_DEFAULT;
423 mtx_lock(&vm_page_queue_free_mtx);
425 vm_phys_free_pages(m, 0);
426 mtx_unlock(&vm_page_queue_free_mtx);
430 * Allocate a contiguous, power of two-sized set of physical pages
431 * from the free lists.
433 * The free page queues must be locked.
436 vm_phys_alloc_pages(int pool, int order)
441 KASSERT(pool < VM_NFREEPOOL,
442 ("vm_phys_alloc_pages: pool %d is out of range", pool));
443 KASSERT(order < VM_NFREEORDER,
444 ("vm_phys_alloc_pages: order %d is out of range", order));
447 domain = PCPU_GET(domain);
451 for (flind = 0; flind < vm_nfreelists; flind++) {
452 m = vm_phys_alloc_domain_pages(domain, flind, pool, order);
460 * Find and dequeue a free page on the given free list, with the
461 * specified pool and order
464 vm_phys_alloc_freelist_pages(int flind, int pool, int order)
472 KASSERT(flind < VM_NFREELIST,
473 ("vm_phys_alloc_freelist_pages: freelist %d is out of range", flind));
474 KASSERT(pool < VM_NFREEPOOL,
475 ("vm_phys_alloc_freelist_pages: pool %d is out of range", pool));
476 KASSERT(order < VM_NFREEORDER,
477 ("vm_phys_alloc_freelist_pages: order %d is out of range", order));
481 * This routine expects to be called with a VM_FREELIST_* constant.
482 * On a system with multiple domains we need to adjust the flind
483 * appropriately. If it is for VM_FREELIST_DEFAULT we need to
484 * iterate over the per-domain lists.
486 domain = PCPU_GET(domain);
487 ndomains = vm_nfreelists - VM_NFREELIST + 1;
488 if (flind == VM_FREELIST_DEFAULT) {
490 for (i = 0; i < ndomains; i++, flind++) {
491 m = vm_phys_alloc_domain_pages(domain, flind, pool,
497 } else if (flind > VM_FREELIST_DEFAULT)
498 flind += ndomains - 1;
502 return (vm_phys_alloc_domain_pages(domain, flind, pool, order));
506 vm_phys_alloc_domain_pages(int domain, int flind, int pool, int order)
508 struct vm_freelist *fl;
509 struct vm_freelist *alt;
513 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
514 fl = (*vm_phys_lookup_lists[domain][flind])[pool];
515 for (oind = order; oind < VM_NFREEORDER; oind++) {
516 m = TAILQ_FIRST(&fl[oind].pl);
518 TAILQ_REMOVE(&fl[oind].pl, m, pageq);
520 m->order = VM_NFREEORDER;
521 vm_phys_split_pages(m, oind, fl, order);
527 * The given pool was empty. Find the largest
528 * contiguous, power-of-two-sized set of pages in any
529 * pool. Transfer these pages to the given pool, and
530 * use them to satisfy the allocation.
532 for (oind = VM_NFREEORDER - 1; oind >= order; oind--) {
533 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
534 alt = (*vm_phys_lookup_lists[domain][flind])[pind];
535 m = TAILQ_FIRST(&alt[oind].pl);
537 TAILQ_REMOVE(&alt[oind].pl, m, pageq);
539 m->order = VM_NFREEORDER;
540 vm_phys_set_pool(pool, m, oind);
541 vm_phys_split_pages(m, oind, fl, order);
550 * Find the vm_page corresponding to the given physical address.
553 vm_phys_paddr_to_vm_page(vm_paddr_t pa)
555 struct vm_phys_seg *seg;
558 for (segind = 0; segind < vm_phys_nsegs; segind++) {
559 seg = &vm_phys_segs[segind];
560 if (pa >= seg->start && pa < seg->end)
561 return (&seg->first_page[atop(pa - seg->start)]);
567 vm_phys_fictitious_to_vm_page(vm_paddr_t pa)
569 struct vm_phys_fictitious_seg *seg;
574 for (segind = 0; segind < VM_PHYS_FICTITIOUS_NSEGS; segind++) {
575 seg = &vm_phys_fictitious_segs[segind];
576 if (pa >= seg->start && pa < seg->end) {
577 m = &seg->first_page[atop(pa - seg->start)];
578 KASSERT((m->flags & PG_FICTITIOUS) != 0,
579 ("%p not fictitious", m));
587 vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
588 vm_memattr_t memattr)
590 struct vm_phys_fictitious_seg *seg;
594 #ifdef VM_PHYSSEG_DENSE
599 page_count = (end - start) / PAGE_SIZE;
601 #ifdef VM_PHYSSEG_DENSE
603 if (pi >= first_page && atop(end) < vm_page_array_size) {
604 fp = &vm_page_array[pi - first_page];
609 fp = malloc(page_count * sizeof(struct vm_page), M_FICT_PAGES,
611 #ifdef VM_PHYSSEG_DENSE
615 for (i = 0; i < page_count; i++) {
616 vm_page_initfake(&fp[i], start + PAGE_SIZE * i, memattr);
617 pmap_page_init(&fp[i]);
618 fp[i].oflags &= ~(VPO_BUSY | VPO_UNMANAGED);
620 mtx_lock(&vm_phys_fictitious_reg_mtx);
621 for (segind = 0; segind < VM_PHYS_FICTITIOUS_NSEGS; segind++) {
622 seg = &vm_phys_fictitious_segs[segind];
623 if (seg->start == 0 && seg->end == 0) {
626 seg->first_page = fp;
627 mtx_unlock(&vm_phys_fictitious_reg_mtx);
631 mtx_unlock(&vm_phys_fictitious_reg_mtx);
632 #ifdef VM_PHYSSEG_DENSE
635 free(fp, M_FICT_PAGES);
640 vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end)
642 struct vm_phys_fictitious_seg *seg;
645 #ifdef VM_PHYSSEG_DENSE
649 #ifdef VM_PHYSSEG_DENSE
653 mtx_lock(&vm_phys_fictitious_reg_mtx);
654 for (segind = 0; segind < VM_PHYS_FICTITIOUS_NSEGS; segind++) {
655 seg = &vm_phys_fictitious_segs[segind];
656 if (seg->start == start && seg->end == end) {
657 seg->start = seg->end = 0;
658 fp = seg->first_page;
659 seg->first_page = NULL;
660 mtx_unlock(&vm_phys_fictitious_reg_mtx);
661 #ifdef VM_PHYSSEG_DENSE
662 if (pi < first_page || atop(end) >= vm_page_array_size)
664 free(fp, M_FICT_PAGES);
668 mtx_unlock(&vm_phys_fictitious_reg_mtx);
669 KASSERT(0, ("Unregistering not registered fictitious range"));
673 * Find the segment containing the given physical address.
676 vm_phys_paddr_to_segind(vm_paddr_t pa)
678 struct vm_phys_seg *seg;
681 for (segind = 0; segind < vm_phys_nsegs; segind++) {
682 seg = &vm_phys_segs[segind];
683 if (pa >= seg->start && pa < seg->end)
686 panic("vm_phys_paddr_to_segind: paddr %#jx is not in any segment" ,
691 * Free a contiguous, power of two-sized set of physical pages.
693 * The free page queues must be locked.
696 vm_phys_free_pages(vm_page_t m, int order)
698 struct vm_freelist *fl;
699 struct vm_phys_seg *seg;
703 KASSERT(m->order == VM_NFREEORDER,
704 ("vm_phys_free_pages: page %p has unexpected order %d",
706 KASSERT(m->pool < VM_NFREEPOOL,
707 ("vm_phys_free_pages: page %p has unexpected pool %d",
709 KASSERT(order < VM_NFREEORDER,
710 ("vm_phys_free_pages: order %d is out of range", order));
711 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
712 seg = &vm_phys_segs[m->segind];
713 if (order < VM_NFREEORDER - 1) {
714 pa = VM_PAGE_TO_PHYS(m);
716 pa ^= ((vm_paddr_t)1 << (PAGE_SHIFT + order));
717 if (pa < seg->start || pa >= seg->end)
719 m_buddy = &seg->first_page[atop(pa - seg->start)];
720 if (m_buddy->order != order)
722 fl = (*seg->free_queues)[m_buddy->pool];
723 TAILQ_REMOVE(&fl[order].pl, m_buddy, pageq);
725 m_buddy->order = VM_NFREEORDER;
726 if (m_buddy->pool != m->pool)
727 vm_phys_set_pool(m->pool, m_buddy, order);
729 pa &= ~(((vm_paddr_t)1 << (PAGE_SHIFT + order)) - 1);
730 m = &seg->first_page[atop(pa - seg->start)];
731 } while (order < VM_NFREEORDER - 1);
734 fl = (*seg->free_queues)[m->pool];
735 TAILQ_INSERT_TAIL(&fl[order].pl, m, pageq);
740 * Free a contiguous, arbitrarily sized set of physical pages.
742 * The free page queues must be locked.
745 vm_phys_free_contig(vm_page_t m, u_long npages)
751 * Avoid unnecessary coalescing by freeing the pages in the largest
752 * possible power-of-two-sized subsets.
754 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
755 for (;; npages -= n) {
757 * Unsigned "min" is used here so that "order" is assigned
758 * "VM_NFREEORDER - 1" when "m"'s physical address is zero
759 * or the low-order bits of its physical address are zero
760 * because the size of a physical address exceeds the size of
763 order = min(ffsl(VM_PAGE_TO_PHYS(m) >> PAGE_SHIFT) - 1,
768 vm_phys_free_pages(m, order);
771 /* The residual "npages" is less than "1 << (VM_NFREEORDER - 1)". */
772 for (; npages > 0; npages -= n) {
773 order = flsl(npages) - 1;
775 vm_phys_free_pages(m, order);
781 * Set the pool for a contiguous, power of two-sized set of physical pages.
784 vm_phys_set_pool(int pool, vm_page_t m, int order)
788 for (m_tmp = m; m_tmp < &m[1 << order]; m_tmp++)
793 * Search for the given physical page "m" in the free lists. If the search
794 * succeeds, remove "m" from the free lists and return TRUE. Otherwise, return
795 * FALSE, indicating that "m" is not in the free lists.
797 * The free page queues must be locked.
800 vm_phys_unfree_page(vm_page_t m)
802 struct vm_freelist *fl;
803 struct vm_phys_seg *seg;
804 vm_paddr_t pa, pa_half;
805 vm_page_t m_set, m_tmp;
808 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
811 * First, find the contiguous, power of two-sized set of free
812 * physical pages containing the given physical page "m" and
813 * assign it to "m_set".
815 seg = &vm_phys_segs[m->segind];
816 for (m_set = m, order = 0; m_set->order == VM_NFREEORDER &&
817 order < VM_NFREEORDER - 1; ) {
819 pa = m->phys_addr & (~(vm_paddr_t)0 << (PAGE_SHIFT + order));
820 if (pa >= seg->start)
821 m_set = &seg->first_page[atop(pa - seg->start)];
825 if (m_set->order < order)
827 if (m_set->order == VM_NFREEORDER)
829 KASSERT(m_set->order < VM_NFREEORDER,
830 ("vm_phys_unfree_page: page %p has unexpected order %d",
831 m_set, m_set->order));
834 * Next, remove "m_set" from the free lists. Finally, extract
835 * "m" from "m_set" using an iterative algorithm: While "m_set"
836 * is larger than a page, shrink "m_set" by returning the half
837 * of "m_set" that does not contain "m" to the free lists.
839 fl = (*seg->free_queues)[m_set->pool];
840 order = m_set->order;
841 TAILQ_REMOVE(&fl[order].pl, m_set, pageq);
843 m_set->order = VM_NFREEORDER;
846 pa_half = m_set->phys_addr ^ (1 << (PAGE_SHIFT + order));
847 if (m->phys_addr < pa_half)
848 m_tmp = &seg->first_page[atop(pa_half - seg->start)];
851 m_set = &seg->first_page[atop(pa_half - seg->start)];
853 m_tmp->order = order;
854 TAILQ_INSERT_HEAD(&fl[order].pl, m_tmp, pageq);
857 KASSERT(m_set == m, ("vm_phys_unfree_page: fatal inconsistency"));
862 * Try to zero one physical page. Used by an idle priority thread.
865 vm_phys_zero_pages_idle(void)
867 static struct vm_freelist *fl = vm_phys_free_queues[0][0];
868 static int flind, oind, pind;
871 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
873 TAILQ_FOREACH_REVERSE(m, &fl[oind].pl, pglist, pageq) {
874 for (m_tmp = m; m_tmp < &m[1 << oind]; m_tmp++) {
875 if ((m_tmp->flags & (PG_CACHED | PG_ZERO)) == 0) {
876 vm_phys_unfree_page(m_tmp);
878 mtx_unlock(&vm_page_queue_free_mtx);
879 pmap_zero_page_idle(m_tmp);
880 m_tmp->flags |= PG_ZERO;
881 mtx_lock(&vm_page_queue_free_mtx);
883 vm_phys_free_pages(m_tmp, 0);
884 vm_page_zero_count++;
891 if (oind == VM_NFREEORDER) {
894 if (pind == VM_NFREEPOOL) {
897 if (flind == vm_nfreelists)
900 fl = vm_phys_free_queues[flind][pind];
906 * Allocate a contiguous set of physical pages of the given size
907 * "npages" from the free lists. All of the physical pages must be at
908 * or above the given physical address "low" and below the given
909 * physical address "high". The given value "alignment" determines the
910 * alignment of the first physical page in the set. If the given value
911 * "boundary" is non-zero, then the set of physical pages cannot cross
912 * any physical address boundary that is a multiple of that value. Both
913 * "alignment" and "boundary" must be a power of two.
916 vm_phys_alloc_contig(u_long npages, vm_paddr_t low, vm_paddr_t high,
917 u_long alignment, u_long boundary)
919 struct vm_freelist *fl;
920 struct vm_phys_seg *seg;
921 vm_paddr_t pa, pa_last, size;
924 int domain, flind, oind, order, pind;
926 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
928 domain = PCPU_GET(domain);
932 size = npages << PAGE_SHIFT;
934 ("vm_phys_alloc_contig: size must not be 0"));
935 KASSERT((alignment & (alignment - 1)) == 0,
936 ("vm_phys_alloc_contig: alignment must be a power of 2"));
937 KASSERT((boundary & (boundary - 1)) == 0,
938 ("vm_phys_alloc_contig: boundary must be a power of 2"));
939 /* Compute the queue that is the best fit for npages. */
940 for (order = 0; (1 << order) < npages; order++);
941 for (flind = 0; flind < vm_nfreelists; flind++) {
942 for (oind = min(order, VM_NFREEORDER - 1); oind < VM_NFREEORDER; oind++) {
943 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
944 fl = (*vm_phys_lookup_lists[domain][flind])
946 TAILQ_FOREACH(m_ret, &fl[oind].pl, pageq) {
948 * A free list may contain physical pages
949 * from one or more segments.
951 seg = &vm_phys_segs[m_ret->segind];
952 if (seg->start > high ||
957 * Is the size of this allocation request
958 * larger than the largest block size?
960 if (order >= VM_NFREEORDER) {
962 * Determine if a sufficient number
963 * of subsequent blocks to satisfy
964 * the allocation request are free.
966 pa = VM_PAGE_TO_PHYS(m_ret);
969 pa += 1 << (PAGE_SHIFT + VM_NFREEORDER - 1);
972 if (pa < seg->start ||
975 m = &seg->first_page[atop(pa - seg->start)];
976 if (m->order != VM_NFREEORDER - 1)
979 /* If not, continue to the next block. */
985 * Determine if the blocks are within the given range,
986 * satisfy the given alignment, and do not cross the
989 pa = VM_PAGE_TO_PHYS(m_ret);
992 (pa & (alignment - 1)) == 0 &&
993 ((pa ^ (pa + size - 1)) & ~(boundary - 1)) == 0)
1001 for (m = m_ret; m < &m_ret[npages]; m = &m[1 << oind]) {
1002 fl = (*seg->free_queues)[m->pool];
1003 TAILQ_REMOVE(&fl[m->order].pl, m, pageq);
1004 fl[m->order].lcnt--;
1005 m->order = VM_NFREEORDER;
1007 if (m_ret->pool != VM_FREEPOOL_DEFAULT)
1008 vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m_ret, oind);
1009 fl = (*seg->free_queues)[m_ret->pool];
1010 vm_phys_split_pages(m_ret, oind, fl, order);
1011 /* Return excess pages to the free lists. */
1012 npages_end = roundup2(npages, 1 << imin(oind, order));
1013 if (npages < npages_end)
1014 vm_phys_free_contig(&m_ret[npages], npages_end - npages);
1020 * Show the number of physical pages in each of the free lists.
1022 DB_SHOW_COMMAND(freepages, db_show_freepages)
1024 struct vm_freelist *fl;
1025 int flind, oind, pind;
1027 for (flind = 0; flind < vm_nfreelists; flind++) {
1028 db_printf("FREE LIST %d:\n"
1029 "\n ORDER (SIZE) | NUMBER"
1031 for (pind = 0; pind < VM_NFREEPOOL; pind++)
1032 db_printf(" | POOL %d", pind);
1034 for (pind = 0; pind < VM_NFREEPOOL; pind++)
1035 db_printf("-- -- ");
1037 for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
1038 db_printf(" %2.2d (%6.6dK)", oind,
1039 1 << (PAGE_SHIFT - 10 + oind));
1040 for (pind = 0; pind < VM_NFREEPOOL; pind++) {
1041 fl = vm_phys_free_queues[flind][pind];
1042 db_printf(" | %6.6d", fl[oind].lcnt);