2 * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU)
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * The Mach Operating System project at Carnegie-Mellon University.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * from: @(#)vm_kern.c 8.3 (Berkeley) 1/12/94
37 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
38 * All rights reserved.
40 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
42 * Permission to use, copy, modify and distribute this software and
43 * its documentation is hereby granted, provided that both the copyright
44 * notice and this permission notice appear in all copies of the
45 * software, derivative works or modified versions, and any portions
46 * thereof, and that both notices appear in supporting documentation.
48 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
50 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
52 * Carnegie Mellon requests users of this software to return to
54 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
55 * School of Computer Science
56 * Carnegie Mellon University
57 * Pittsburgh PA 15213-3890
59 * any improvements or extensions that they make and grant Carnegie the
60 * rights to redistribute these changes.
64 * Kernel memory management.
67 #include <sys/cdefs.h>
68 __FBSDID("$FreeBSD$");
72 #include <sys/param.h>
73 #include <sys/systm.h>
75 #include <sys/domainset.h>
76 #include <sys/eventhandler.h>
77 #include <sys/kernel.h>
79 #include <sys/malloc.h>
82 #include <sys/rwlock.h>
83 #include <sys/sysctl.h>
85 #include <sys/vmmeter.h>
88 #include <vm/vm_param.h>
89 #include <vm/vm_domainset.h>
90 #include <vm/vm_kern.h>
92 #include <vm/vm_map.h>
93 #include <vm/vm_object.h>
94 #include <vm/vm_page.h>
95 #include <vm/vm_pageout.h>
96 #include <vm/vm_pagequeue.h>
97 #include <vm/vm_phys.h>
98 #include <vm/vm_radix.h>
99 #include <vm/vm_extern.h>
102 struct vm_map kernel_map_store;
103 struct vm_map exec_map_store;
104 struct vm_map pipe_map_store;
106 const void *zero_region;
107 CTASSERT((ZERO_REGION_SIZE & PAGE_MASK) == 0);
109 /* NB: Used by kernel debuggers. */
110 const u_long vm_maxuser_address = VM_MAXUSER_ADDRESS;
112 u_int exec_map_entry_size;
113 u_int exec_map_entries;
115 SYSCTL_ULONG(_vm, OID_AUTO, min_kernel_address, CTLFLAG_RD,
116 SYSCTL_NULL_ULONG_PTR, VM_MIN_KERNEL_ADDRESS, "Min kernel address");
118 SYSCTL_ULONG(_vm, OID_AUTO, max_kernel_address, CTLFLAG_RD,
120 &vm_max_kernel_address, 0,
122 SYSCTL_NULL_ULONG_PTR, VM_MAX_KERNEL_ADDRESS,
124 "Max kernel address");
126 #if VM_NRESERVLEVEL > 0
127 #define KVA_QUANTUM_SHIFT (VM_LEVEL_0_ORDER + PAGE_SHIFT)
129 /* On non-superpage architectures we want large import sizes. */
130 #define KVA_QUANTUM_SHIFT (8 + PAGE_SHIFT)
132 #define KVA_QUANTUM (1ul << KVA_QUANTUM_SHIFT)
133 #define KVA_NUMA_IMPORT_QUANTUM (KVA_QUANTUM * 128)
135 extern void uma_startup2(void);
140 * Allocate a virtual address range with no underlying object and
141 * no initial mapping to physical memory. Any mapping from this
142 * range to physical memory must be explicitly created prior to
143 * its use, typically with pmap_qenter(). Any attempt to create
144 * a mapping on demand through vm_fault() will result in a panic.
147 kva_alloc(vm_size_t size)
152 size = round_page(size);
153 if (vmem_alloc(kernel_arena, size, M_BESTFIT | M_NOWAIT, &addr))
163 * Release a region of kernel virtual memory allocated
164 * with kva_alloc, and return the physical pages
165 * associated with that region.
167 * This routine may not block on kernel maps.
170 kva_free(vm_offset_t addr, vm_size_t size)
173 size = round_page(size);
174 vmem_free(kernel_arena, addr, size);
178 * Update sanitizer shadow state to reflect a new allocation. Force inlining to
179 * help make KMSAN origin tracking more precise.
181 static __always_inline void
182 kmem_alloc_san(vm_offset_t addr, vm_size_t size, vm_size_t asize, int flags)
184 if ((flags & M_ZERO) == 0) {
185 kmsan_mark((void *)addr, asize, KMSAN_STATE_UNINIT);
186 kmsan_orig((void *)addr, asize, KMSAN_TYPE_KMEM,
189 kmsan_mark((void *)addr, asize, KMSAN_STATE_INITED);
191 kasan_mark((void *)addr, size, asize, KASAN_KMEM_REDZONE);
195 kmem_alloc_contig_pages(vm_object_t object, vm_pindex_t pindex, int domain,
196 int pflags, u_long npages, vm_paddr_t low, vm_paddr_t high,
197 u_long alignment, vm_paddr_t boundary, vm_memattr_t memattr)
203 VM_OBJECT_ASSERT_WLOCKED(object);
205 wait = (pflags & VM_ALLOC_WAITOK) != 0;
206 reclaim = (pflags & VM_ALLOC_NORECLAIM) == 0;
207 pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL);
208 pflags |= VM_ALLOC_NOWAIT;
209 for (tries = wait ? 3 : 1;; tries--) {
210 m = vm_page_alloc_contig_domain(object, pindex, domain, pflags,
211 npages, low, high, alignment, boundary, memattr);
212 if (m != NULL || tries == 0 || !reclaim)
215 VM_OBJECT_WUNLOCK(object);
216 if (!vm_page_reclaim_contig_domain(domain, pflags, npages,
217 low, high, alignment, boundary) && wait)
218 vm_wait_domain(domain);
219 VM_OBJECT_WLOCK(object);
225 * Allocates a region from the kernel address map and physical pages
226 * within the specified address range to the kernel object. Creates a
227 * wired mapping from this region to these pages, and returns the
228 * region's starting virtual address. The allocated pages are not
229 * necessarily physically contiguous. If M_ZERO is specified through the
230 * given flags, then the pages are zeroed before they are mapped.
233 kmem_alloc_attr_domain(int domain, vm_size_t size, int flags, vm_paddr_t low,
234 vm_paddr_t high, vm_memattr_t memattr)
238 vm_offset_t addr, i, offset;
244 object = kernel_object;
245 asize = round_page(size);
246 vmem = vm_dom[domain].vmd_kernel_arena;
247 if (vmem_alloc(vmem, asize, M_BESTFIT | flags, &addr))
249 offset = addr - VM_MIN_KERNEL_ADDRESS;
250 pflags = malloc2vm_flags(flags) | VM_ALLOC_WIRED;
251 prot = (flags & M_EXEC) != 0 ? VM_PROT_ALL : VM_PROT_RW;
252 VM_OBJECT_WLOCK(object);
253 for (i = 0; i < asize; i += PAGE_SIZE) {
254 m = kmem_alloc_contig_pages(object, atop(offset + i),
255 domain, pflags, 1, low, high, PAGE_SIZE, 0, memattr);
257 VM_OBJECT_WUNLOCK(object);
258 kmem_unback(object, addr, i);
259 vmem_free(vmem, addr, asize);
262 KASSERT(vm_page_domain(m) == domain,
263 ("kmem_alloc_attr_domain: Domain mismatch %d != %d",
264 vm_page_domain(m), domain));
265 if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
268 pmap_enter(kernel_pmap, addr + i, m, prot,
269 prot | PMAP_ENTER_WIRED, 0);
271 VM_OBJECT_WUNLOCK(object);
272 kmem_alloc_san(addr, size, asize, flags);
273 return ((void *)addr);
277 kmem_alloc_attr(vm_size_t size, int flags, vm_paddr_t low, vm_paddr_t high,
278 vm_memattr_t memattr)
281 return (kmem_alloc_attr_domainset(DOMAINSET_RR(), size, flags, low,
286 kmem_alloc_attr_domainset(struct domainset *ds, vm_size_t size, int flags,
287 vm_paddr_t low, vm_paddr_t high, vm_memattr_t memattr)
289 struct vm_domainset_iter di;
293 vm_domainset_iter_policy_init(&di, ds, &domain, &flags);
295 addr = kmem_alloc_attr_domain(domain, size, flags, low, high,
299 } while (vm_domainset_iter_policy(&di, &domain) == 0);
305 * Allocates a region from the kernel address map and physically
306 * contiguous pages within the specified address range to the kernel
307 * object. Creates a wired mapping from this region to these pages, and
308 * returns the region's starting virtual address. If M_ZERO is specified
309 * through the given flags, then the pages are zeroed before they are
313 kmem_alloc_contig_domain(int domain, vm_size_t size, int flags, vm_paddr_t low,
314 vm_paddr_t high, u_long alignment, vm_paddr_t boundary,
315 vm_memattr_t memattr)
319 vm_offset_t addr, offset, tmp;
325 object = kernel_object;
326 asize = round_page(size);
327 vmem = vm_dom[domain].vmd_kernel_arena;
328 if (vmem_alloc(vmem, asize, flags | M_BESTFIT, &addr))
330 offset = addr - VM_MIN_KERNEL_ADDRESS;
331 pflags = malloc2vm_flags(flags) | VM_ALLOC_WIRED;
332 npages = atop(asize);
333 VM_OBJECT_WLOCK(object);
334 m = kmem_alloc_contig_pages(object, atop(offset), domain,
335 pflags, npages, low, high, alignment, boundary, memattr);
337 VM_OBJECT_WUNLOCK(object);
338 vmem_free(vmem, addr, asize);
341 KASSERT(vm_page_domain(m) == domain,
342 ("kmem_alloc_contig_domain: Domain mismatch %d != %d",
343 vm_page_domain(m), domain));
346 for (; m < end_m; m++) {
347 if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
350 pmap_enter(kernel_pmap, tmp, m, VM_PROT_RW,
351 VM_PROT_RW | PMAP_ENTER_WIRED, 0);
354 VM_OBJECT_WUNLOCK(object);
355 kmem_alloc_san(addr, size, asize, flags);
356 return ((void *)addr);
360 kmem_alloc_contig(vm_size_t size, int flags, vm_paddr_t low, vm_paddr_t high,
361 u_long alignment, vm_paddr_t boundary, vm_memattr_t memattr)
364 return (kmem_alloc_contig_domainset(DOMAINSET_RR(), size, flags, low,
365 high, alignment, boundary, memattr));
369 kmem_alloc_contig_domainset(struct domainset *ds, vm_size_t size, int flags,
370 vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary,
371 vm_memattr_t memattr)
373 struct vm_domainset_iter di;
377 vm_domainset_iter_policy_init(&di, ds, &domain, &flags);
379 addr = kmem_alloc_contig_domain(domain, size, flags, low, high,
380 alignment, boundary, memattr);
383 } while (vm_domainset_iter_policy(&di, &domain) == 0);
391 * Initializes a map to manage a subrange
392 * of the kernel virtual address space.
394 * Arguments are as follows:
396 * parent Map to take range from
397 * min, max Returned endpoints of map
398 * size Size of range to find
399 * superpage_align Request that min is superpage aligned
402 kmem_subinit(vm_map_t map, vm_map_t parent, vm_offset_t *min, vm_offset_t *max,
403 vm_size_t size, bool superpage_align)
407 size = round_page(size);
409 *min = vm_map_min(parent);
410 ret = vm_map_find(parent, NULL, 0, min, size, 0, superpage_align ?
411 VMFS_SUPER_SPACE : VMFS_ANY_SPACE, VM_PROT_ALL, VM_PROT_ALL,
413 if (ret != KERN_SUCCESS)
414 panic("kmem_subinit: bad status return of %d", ret);
416 vm_map_init(map, vm_map_pmap(parent), *min, *max);
417 if (vm_map_submap(parent, *min, *max, map) != KERN_SUCCESS)
418 panic("kmem_subinit: unable to change range to submap");
422 * kmem_malloc_domain:
424 * Allocate wired-down pages in the kernel's address space.
427 kmem_malloc_domain(int domain, vm_size_t size, int flags)
434 if (__predict_true((flags & M_EXEC) == 0))
435 arena = vm_dom[domain].vmd_kernel_arena;
437 arena = vm_dom[domain].vmd_kernel_rwx_arena;
438 asize = round_page(size);
439 if (vmem_alloc(arena, asize, flags | M_BESTFIT, &addr))
442 rv = kmem_back_domain(domain, kernel_object, addr, asize, flags);
443 if (rv != KERN_SUCCESS) {
444 vmem_free(arena, addr, asize);
447 kasan_mark((void *)addr, size, asize, KASAN_KMEM_REDZONE);
448 return ((void *)addr);
452 kmem_malloc(vm_size_t size, int flags)
457 p = kmem_malloc_domainset(DOMAINSET_RR(), size, flags);
463 kmem_malloc_domainset(struct domainset *ds, vm_size_t size, int flags)
465 struct vm_domainset_iter di;
469 vm_domainset_iter_policy_init(&di, ds, &domain, &flags);
471 addr = kmem_malloc_domain(domain, size, flags);
474 } while (vm_domainset_iter_policy(&di, &domain) == 0);
482 * Allocate physical pages from the specified domain for the specified
483 * virtual address range.
486 kmem_back_domain(int domain, vm_object_t object, vm_offset_t addr,
487 vm_size_t size, int flags)
489 vm_offset_t offset, i;
494 KASSERT(object == kernel_object,
495 ("kmem_back_domain: only supports kernel object."));
497 offset = addr - VM_MIN_KERNEL_ADDRESS;
498 pflags = malloc2vm_flags(flags) | VM_ALLOC_WIRED;
499 pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL);
500 if (flags & M_WAITOK)
501 pflags |= VM_ALLOC_WAITFAIL;
502 prot = (flags & M_EXEC) != 0 ? VM_PROT_ALL : VM_PROT_RW;
505 VM_OBJECT_WLOCK(object);
507 mpred = vm_radix_lookup_le(&object->rtree, atop(offset + i));
508 for (; i < size; i += PAGE_SIZE, mpred = m) {
509 m = vm_page_alloc_domain_after(object, atop(offset + i),
510 domain, pflags, mpred);
513 * Ran out of space, free everything up and return. Don't need
514 * to lock page queues here as we know that the pages we got
515 * aren't on any queues.
518 if ((flags & M_NOWAIT) == 0)
520 VM_OBJECT_WUNLOCK(object);
521 kmem_unback(object, addr, i);
522 return (KERN_NO_SPACE);
524 KASSERT(vm_page_domain(m) == domain,
525 ("kmem_back_domain: Domain mismatch %d != %d",
526 vm_page_domain(m), domain));
527 if (flags & M_ZERO && (m->flags & PG_ZERO) == 0)
529 KASSERT((m->oflags & VPO_UNMANAGED) != 0,
530 ("kmem_malloc: page %p is managed", m));
532 pmap_enter(kernel_pmap, addr + i, m, prot,
533 prot | PMAP_ENTER_WIRED, 0);
534 if (__predict_false((prot & VM_PROT_EXECUTE) != 0))
535 m->oflags |= VPO_KMEM_EXEC;
537 VM_OBJECT_WUNLOCK(object);
538 kmem_alloc_san(addr, size, size, flags);
539 return (KERN_SUCCESS);
545 * Allocate physical pages for the specified virtual address range.
548 kmem_back(vm_object_t object, vm_offset_t addr, vm_size_t size, int flags)
550 vm_offset_t end, next, start;
553 KASSERT(object == kernel_object,
554 ("kmem_back: only supports kernel object."));
556 for (start = addr, end = addr + size; addr < end; addr = next) {
558 * We must ensure that pages backing a given large virtual page
559 * all come from the same physical domain.
561 if (vm_ndomains > 1) {
562 domain = (addr >> KVA_QUANTUM_SHIFT) % vm_ndomains;
563 while (VM_DOMAIN_EMPTY(domain))
565 next = roundup2(addr + 1, KVA_QUANTUM);
566 if (next > end || next < start)
572 rv = kmem_back_domain(domain, object, addr, next - addr, flags);
573 if (rv != KERN_SUCCESS) {
574 kmem_unback(object, start, addr - start);
584 * Unmap and free the physical pages underlying the specified virtual
587 * A physical page must exist within the specified object at each index
588 * that is being unmapped.
591 _kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size)
595 vm_offset_t end, offset;
598 KASSERT(object == kernel_object,
599 ("kmem_unback: only supports kernel object."));
603 pmap_remove(kernel_pmap, addr, addr + size);
604 offset = addr - VM_MIN_KERNEL_ADDRESS;
606 VM_OBJECT_WLOCK(object);
607 m = vm_page_lookup(object, atop(offset));
608 domain = vm_page_domain(m);
609 if (__predict_true((m->oflags & VPO_KMEM_EXEC) == 0))
610 arena = vm_dom[domain].vmd_kernel_arena;
612 arena = vm_dom[domain].vmd_kernel_rwx_arena;
613 for (; offset < end; offset += PAGE_SIZE, m = next) {
614 next = vm_page_next(m);
615 vm_page_xbusy_claim(m);
616 vm_page_unwire_noq(m);
619 VM_OBJECT_WUNLOCK(object);
625 kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size)
628 (void)_kmem_unback(object, addr, size);
634 * Free memory allocated with kmem_malloc. The size must match the
635 * original allocation.
638 kmem_free(void *addr, vm_size_t size)
642 size = round_page(size);
643 kasan_mark(addr, size, size, 0);
644 arena = _kmem_unback(kernel_object, (uintptr_t)addr, size);
646 vmem_free(arena, (uintptr_t)addr, size);
652 * Allocates pageable memory from a sub-map of the kernel. If the submap
653 * has no room, the caller sleeps waiting for more memory in the submap.
655 * This routine may block.
658 kmap_alloc_wait(vm_map_t map, vm_size_t size)
662 size = round_page(size);
663 if (!swap_reserve(size))
668 * To make this work for more than one map, use the map's lock
669 * to lock out sleepers/wakers.
672 addr = vm_map_findspace(map, vm_map_min(map), size);
673 if (addr + size <= vm_map_max(map))
675 /* no space now; see if we can ever get space */
676 if (vm_map_max(map) - vm_map_min(map) < size) {
681 map->needs_wakeup = TRUE;
682 vm_map_unlock_and_wait(map, 0);
684 vm_map_insert(map, NULL, 0, addr, addr + size, VM_PROT_RW, VM_PROT_RW,
693 * Returns memory to a submap of the kernel, and wakes up any processes
694 * waiting for memory in that map.
697 kmap_free_wakeup(vm_map_t map, vm_offset_t addr, vm_size_t size)
701 (void) vm_map_delete(map, trunc_page(addr), round_page(addr + size));
702 if (map->needs_wakeup) {
703 map->needs_wakeup = FALSE;
710 kmem_init_zero_region(void)
716 * Map a single physical page of zeros to a larger virtual range.
717 * This requires less looping in places that want large amounts of
718 * zeros, while not using much more physical resources.
720 addr = kva_alloc(ZERO_REGION_SIZE);
721 m = vm_page_alloc_noobj(VM_ALLOC_WIRED | VM_ALLOC_ZERO);
722 for (i = 0; i < ZERO_REGION_SIZE; i += PAGE_SIZE)
723 pmap_qenter(addr + i, &m, 1);
724 pmap_protect(kernel_pmap, addr, addr + ZERO_REGION_SIZE, VM_PROT_READ);
726 zero_region = (const void *)addr;
730 * Import KVA from the kernel map into the kernel arena.
733 kva_import(void *unused, vmem_size_t size, int flags, vmem_addr_t *addrp)
739 KASSERT((size % KVA_QUANTUM) == 0,
740 ("kva_import: Size %jd is not a multiple of %d",
741 (intmax_t)size, (int)KVA_QUANTUM));
742 addr = vm_map_min(kernel_map);
743 result = vm_map_find(kernel_map, NULL, 0, &addr, size, 0,
744 VMFS_SUPER_SPACE, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
745 if (result != KERN_SUCCESS) {
757 * Import KVA from a parent arena into a per-domain arena. Imports must be
758 * KVA_QUANTUM-aligned and a multiple of KVA_QUANTUM in size.
761 kva_import_domain(void *arena, vmem_size_t size, int flags, vmem_addr_t *addrp)
764 KASSERT((size % KVA_QUANTUM) == 0,
765 ("kva_import_domain: Size %jd is not a multiple of %d",
766 (intmax_t)size, (int)KVA_QUANTUM));
767 return (vmem_xalloc(arena, size, KVA_QUANTUM, 0, 0, VMEM_ADDR_MIN,
768 VMEM_ADDR_MAX, flags, addrp));
774 * Create the kernel map; insert a mapping covering kernel text,
775 * data, bss, and all space allocated thus far (`boostrap' data). The
776 * new map will thus map the range between VM_MIN_KERNEL_ADDRESS and
777 * `start' as allocated, and the range between `start' and `end' as free.
778 * Create the kernel vmem arena and its per-domain children.
781 kmem_init(vm_offset_t start, vm_offset_t end)
786 vm_map_init(kernel_map, kernel_pmap, VM_MIN_KERNEL_ADDRESS, end);
787 kernel_map->system_map = 1;
788 vm_map_lock(kernel_map);
789 /* N.B.: cannot use kgdb to debug, starting with this assignment ... */
790 (void)vm_map_insert(kernel_map, NULL, 0,
794 VM_MIN_KERNEL_ADDRESS,
796 start, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
797 /* ... and ending with the completion of the above `insert' */
801 * Mark KVA used for the page array as allocated. Other platforms
802 * that handle vm_page_array allocation can simply adjust virtual_avail
805 (void)vm_map_insert(kernel_map, NULL, 0, (vm_offset_t)vm_page_array,
806 (vm_offset_t)vm_page_array + round_2mpage(vm_page_array_size *
807 sizeof(struct vm_page)),
808 VM_PROT_RW, VM_PROT_RW, MAP_NOFAULT);
810 vm_map_unlock(kernel_map);
813 * Use a large import quantum on NUMA systems. This helps minimize
814 * interleaving of superpages, reducing internal fragmentation within
815 * the per-domain arenas.
817 if (vm_ndomains > 1 && PMAP_HAS_DMAP)
818 quantum = KVA_NUMA_IMPORT_QUANTUM;
820 quantum = KVA_QUANTUM;
823 * Initialize the kernel_arena. This can grow on demand.
825 vmem_init(kernel_arena, "kernel arena", 0, 0, PAGE_SIZE, 0, 0);
826 vmem_set_import(kernel_arena, kva_import, NULL, NULL, quantum);
828 for (domain = 0; domain < vm_ndomains; domain++) {
830 * Initialize the per-domain arenas. These are used to color
831 * the KVA space in a way that ensures that virtual large pages
832 * are backed by memory from the same physical domain,
833 * maximizing the potential for superpage promotion.
835 vm_dom[domain].vmd_kernel_arena = vmem_create(
836 "kernel arena domain", 0, 0, PAGE_SIZE, 0, M_WAITOK);
837 vmem_set_import(vm_dom[domain].vmd_kernel_arena,
838 kva_import_domain, NULL, kernel_arena, quantum);
841 * In architectures with superpages, maintain separate arenas
842 * for allocations with permissions that differ from the
843 * "standard" read/write permissions used for kernel memory,
844 * so as not to inhibit superpage promotion.
846 * Use the base import quantum since this arena is rarely used.
848 #if VM_NRESERVLEVEL > 0
849 vm_dom[domain].vmd_kernel_rwx_arena = vmem_create(
850 "kernel rwx arena domain", 0, 0, PAGE_SIZE, 0, M_WAITOK);
851 vmem_set_import(vm_dom[domain].vmd_kernel_rwx_arena,
852 kva_import_domain, (vmem_release_t *)vmem_xfree,
853 kernel_arena, KVA_QUANTUM);
855 vm_dom[domain].vmd_kernel_rwx_arena =
856 vm_dom[domain].vmd_kernel_arena;
861 * This must be the very first call so that the virtual address
862 * space used for early allocations is properly marked used in
869 * kmem_bootstrap_free:
871 * Free pages backing preloaded data (e.g., kernel modules) to the
872 * system. Currently only supported on platforms that create a
873 * vm_phys segment for preloaded data.
876 kmem_bootstrap_free(vm_offset_t start, vm_size_t size)
878 #if defined(__i386__) || defined(__amd64__)
879 struct vm_domain *vmd;
884 end = trunc_page(start + size);
885 start = round_page(start);
889 * Preloaded files do not have execute permissions by default on amd64.
890 * Restore the default permissions to ensure that the direct map alias
893 pmap_change_prot(start, end - start, VM_PROT_RW);
895 for (va = start; va < end; va += PAGE_SIZE) {
896 pa = pmap_kextract(va);
897 m = PHYS_TO_VM_PAGE(pa);
899 vmd = vm_pagequeue_domain(m);
900 vm_domain_free_lock(vmd);
901 vm_phys_free_pages(m, 0);
902 vm_domain_free_unlock(vmd);
904 vm_domain_freecnt_inc(vmd, 1);
905 vm_cnt.v_page_count++;
907 pmap_remove(kernel_pmap, start, end);
908 (void)vmem_add(kernel_arena, start, end - start, M_WAITOK);
913 * Allow userspace to directly trigger the VM drain routine for testing
917 debug_vm_lowmem(SYSCTL_HANDLER_ARGS)
922 error = sysctl_handle_int(oidp, &i, 0, req);
925 if ((i & ~(VM_LOW_KMEM | VM_LOW_PAGES)) != 0)
928 EVENTHANDLER_INVOKE(vm_lowmem, i);
931 SYSCTL_PROC(_debug, OID_AUTO, vm_lowmem,
932 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, 0, debug_vm_lowmem, "I",
933 "set to trigger vm_lowmem event with given flags");
936 debug_uma_reclaim(SYSCTL_HANDLER_ARGS)
941 error = sysctl_handle_int(oidp, &i, 0, req);
942 if (error != 0 || req->newptr == NULL)
944 if (i != UMA_RECLAIM_TRIM && i != UMA_RECLAIM_DRAIN &&
945 i != UMA_RECLAIM_DRAIN_CPU)
950 SYSCTL_PROC(_debug, OID_AUTO, uma_reclaim,
951 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, 0, debug_uma_reclaim, "I",
952 "set to generate request to reclaim uma caches");
955 debug_uma_reclaim_domain(SYSCTL_HANDLER_ARGS)
957 int domain, error, request;
960 error = sysctl_handle_int(oidp, &request, 0, req);
961 if (error != 0 || req->newptr == NULL)
964 domain = request >> 4;
966 if (request != UMA_RECLAIM_TRIM && request != UMA_RECLAIM_DRAIN &&
967 request != UMA_RECLAIM_DRAIN_CPU)
969 if (domain < 0 || domain >= vm_ndomains)
971 uma_reclaim_domain(request, domain);
974 SYSCTL_PROC(_debug, OID_AUTO, uma_reclaim_domain,
975 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, 0,
976 debug_uma_reclaim_domain, "I",