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
35 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36 * All rights reserved.
38 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
40 * Permission to use, copy, modify and distribute this software and
41 * its documentation is hereby granted, provided that both the copyright
42 * notice and this permission notice appear in all copies of the
43 * software, derivative works or modified versions, and any portions
44 * thereof, and that both notices appear in supporting documentation.
46 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
50 * Carnegie Mellon requests users of this software to return to
52 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
53 * School of Computer Science
54 * Carnegie Mellon University
55 * Pittsburgh PA 15213-3890
57 * any improvements or extensions that they make and grant Carnegie the
58 * rights to redistribute these changes.
62 * Kernel memory management.
65 #include <sys/cdefs.h>
68 #include <sys/param.h>
69 #include <sys/systm.h>
71 #include <sys/domainset.h>
72 #include <sys/eventhandler.h>
73 #include <sys/kernel.h>
75 #include <sys/malloc.h>
78 #include <sys/rwlock.h>
80 #include <sys/sysctl.h>
82 #include <sys/vmmeter.h>
85 #include <vm/vm_param.h>
86 #include <vm/vm_domainset.h>
87 #include <vm/vm_kern.h>
89 #include <vm/vm_map.h>
90 #include <vm/vm_object.h>
91 #include <vm/vm_page.h>
92 #include <vm/vm_pageout.h>
93 #include <vm/vm_pagequeue.h>
94 #include <vm/vm_phys.h>
95 #include <vm/vm_radix.h>
96 #include <vm/vm_extern.h>
99 struct vm_map kernel_map_store;
100 struct vm_map exec_map_store;
101 struct vm_map pipe_map_store;
103 const void *zero_region;
104 CTASSERT((ZERO_REGION_SIZE & PAGE_MASK) == 0);
106 /* NB: Used by kernel debuggers. */
107 const u_long vm_maxuser_address = VM_MAXUSER_ADDRESS;
109 u_int exec_map_entry_size;
110 u_int exec_map_entries;
112 SYSCTL_ULONG(_vm, OID_AUTO, min_kernel_address, CTLFLAG_RD,
113 SYSCTL_NULL_ULONG_PTR, VM_MIN_KERNEL_ADDRESS, "Min kernel address");
115 SYSCTL_ULONG(_vm, OID_AUTO, max_kernel_address, CTLFLAG_RD,
117 &vm_max_kernel_address, 0,
119 SYSCTL_NULL_ULONG_PTR, VM_MAX_KERNEL_ADDRESS,
121 "Max kernel address");
123 #if VM_NRESERVLEVEL > 0
124 #define KVA_QUANTUM_SHIFT (VM_LEVEL_0_ORDER + PAGE_SHIFT)
126 /* On non-superpage architectures we want large import sizes. */
127 #define KVA_QUANTUM_SHIFT (8 + PAGE_SHIFT)
129 #define KVA_QUANTUM (1ul << KVA_QUANTUM_SHIFT)
130 #define KVA_NUMA_IMPORT_QUANTUM (KVA_QUANTUM * 128)
132 extern void uma_startup2(void);
137 * Allocate a virtual address range with no underlying object and
138 * no initial mapping to physical memory. Any mapping from this
139 * range to physical memory must be explicitly created prior to
140 * its use, typically with pmap_qenter(). Any attempt to create
141 * a mapping on demand through vm_fault() will result in a panic.
144 kva_alloc(vm_size_t size)
149 size = round_page(size);
150 if (vmem_xalloc(kernel_arena, size, 0, 0, 0, VMEM_ADDR_MIN,
151 VMEM_ADDR_MAX, M_BESTFIT | M_NOWAIT, &addr))
161 * Allocate a virtual address range as in kva_alloc where the base
162 * address is aligned to align.
165 kva_alloc_aligned(vm_size_t size, vm_size_t align)
170 size = round_page(size);
171 if (vmem_xalloc(kernel_arena, size, align, 0, 0, VMEM_ADDR_MIN,
172 VMEM_ADDR_MAX, M_BESTFIT | M_NOWAIT, &addr))
182 * Release a region of kernel virtual memory allocated
183 * with kva_alloc, and return the physical pages
184 * associated with that region.
186 * This routine may not block on kernel maps.
189 kva_free(vm_offset_t addr, vm_size_t size)
192 size = round_page(size);
193 vmem_xfree(kernel_arena, addr, size);
197 * Update sanitizer shadow state to reflect a new allocation. Force inlining to
198 * help make KMSAN origin tracking more precise.
200 static __always_inline void
201 kmem_alloc_san(vm_offset_t addr, vm_size_t size, vm_size_t asize, int flags)
203 if ((flags & M_ZERO) == 0) {
204 kmsan_mark((void *)addr, asize, KMSAN_STATE_UNINIT);
205 kmsan_orig((void *)addr, asize, KMSAN_TYPE_KMEM,
208 kmsan_mark((void *)addr, asize, KMSAN_STATE_INITED);
210 kasan_mark((void *)addr, size, asize, KASAN_KMEM_REDZONE);
214 kmem_alloc_contig_pages(vm_object_t object, vm_pindex_t pindex, int domain,
215 int pflags, u_long npages, vm_paddr_t low, vm_paddr_t high,
216 u_long alignment, vm_paddr_t boundary, vm_memattr_t memattr)
222 VM_OBJECT_ASSERT_WLOCKED(object);
224 wait = (pflags & VM_ALLOC_WAITOK) != 0;
225 reclaim = (pflags & VM_ALLOC_NORECLAIM) == 0;
226 pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL);
227 pflags |= VM_ALLOC_NOWAIT;
228 for (tries = wait ? 3 : 1;; tries--) {
229 m = vm_page_alloc_contig_domain(object, pindex, domain, pflags,
230 npages, low, high, alignment, boundary, memattr);
231 if (m != NULL || tries == 0 || !reclaim)
234 VM_OBJECT_WUNLOCK(object);
235 if (vm_page_reclaim_contig_domain(domain, pflags, npages,
236 low, high, alignment, boundary) == ENOMEM && wait)
237 vm_wait_domain(domain);
238 VM_OBJECT_WLOCK(object);
244 * Allocates a region from the kernel address map and physical pages
245 * within the specified address range to the kernel object. Creates a
246 * wired mapping from this region to these pages, and returns the
247 * region's starting virtual address. The allocated pages are not
248 * necessarily physically contiguous. If M_ZERO is specified through the
249 * given flags, then the pages are zeroed before they are mapped.
252 kmem_alloc_attr_domain(int domain, vm_size_t size, int flags, vm_paddr_t low,
253 vm_paddr_t high, vm_memattr_t memattr)
257 vm_offset_t addr, i, offset;
263 object = kernel_object;
264 asize = round_page(size);
265 vmem = vm_dom[domain].vmd_kernel_arena;
266 if (vmem_alloc(vmem, asize, M_BESTFIT | flags, &addr))
268 offset = addr - VM_MIN_KERNEL_ADDRESS;
269 pflags = malloc2vm_flags(flags) | VM_ALLOC_WIRED;
270 prot = (flags & M_EXEC) != 0 ? VM_PROT_ALL : VM_PROT_RW;
271 VM_OBJECT_WLOCK(object);
272 for (i = 0; i < asize; i += PAGE_SIZE) {
273 m = kmem_alloc_contig_pages(object, atop(offset + i),
274 domain, pflags, 1, low, high, PAGE_SIZE, 0, memattr);
276 VM_OBJECT_WUNLOCK(object);
277 kmem_unback(object, addr, i);
278 vmem_free(vmem, addr, asize);
281 KASSERT(vm_page_domain(m) == domain,
282 ("kmem_alloc_attr_domain: Domain mismatch %d != %d",
283 vm_page_domain(m), domain));
284 if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
287 pmap_enter(kernel_pmap, addr + i, m, prot,
288 prot | PMAP_ENTER_WIRED, 0);
290 VM_OBJECT_WUNLOCK(object);
291 kmem_alloc_san(addr, size, asize, flags);
292 return ((void *)addr);
296 kmem_alloc_attr(vm_size_t size, int flags, vm_paddr_t low, vm_paddr_t high,
297 vm_memattr_t memattr)
300 return (kmem_alloc_attr_domainset(DOMAINSET_RR(), size, flags, low,
305 kmem_alloc_attr_domainset(struct domainset *ds, vm_size_t size, int flags,
306 vm_paddr_t low, vm_paddr_t high, vm_memattr_t memattr)
308 struct vm_domainset_iter di;
316 vm_domainset_iter_policy_init(&di, ds, &domain, &flags);
318 addr = kmem_alloc_attr_domain(domain, size, flags, low, high,
322 if (start_segind == -1)
323 start_segind = vm_phys_lookup_segind(low);
324 if (vm_phys_find_range(bounds, start_segind, domain,
325 atop(round_page(size)), low, high) == -1) {
326 vm_domainset_iter_ignore(&di, domain);
328 } while (vm_domainset_iter_policy(&di, &domain) == 0);
334 * Allocates a region from the kernel address map and physically
335 * contiguous pages within the specified address range to the kernel
336 * object. Creates a wired mapping from this region to these pages, and
337 * returns the region's starting virtual address. If M_ZERO is specified
338 * through the given flags, then the pages are zeroed before they are
342 kmem_alloc_contig_domain(int domain, vm_size_t size, int flags, vm_paddr_t low,
343 vm_paddr_t high, u_long alignment, vm_paddr_t boundary,
344 vm_memattr_t memattr)
348 vm_offset_t addr, offset, tmp;
354 object = kernel_object;
355 asize = round_page(size);
356 vmem = vm_dom[domain].vmd_kernel_arena;
357 if (vmem_alloc(vmem, asize, flags | M_BESTFIT, &addr))
359 offset = addr - VM_MIN_KERNEL_ADDRESS;
360 pflags = malloc2vm_flags(flags) | VM_ALLOC_WIRED;
361 npages = atop(asize);
362 VM_OBJECT_WLOCK(object);
363 m = kmem_alloc_contig_pages(object, atop(offset), domain,
364 pflags, npages, low, high, alignment, boundary, memattr);
366 VM_OBJECT_WUNLOCK(object);
367 vmem_free(vmem, addr, asize);
370 KASSERT(vm_page_domain(m) == domain,
371 ("kmem_alloc_contig_domain: Domain mismatch %d != %d",
372 vm_page_domain(m), domain));
375 for (; m < end_m; m++) {
376 if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
379 pmap_enter(kernel_pmap, tmp, m, VM_PROT_RW,
380 VM_PROT_RW | PMAP_ENTER_WIRED, 0);
383 VM_OBJECT_WUNLOCK(object);
384 kmem_alloc_san(addr, size, asize, flags);
385 return ((void *)addr);
389 kmem_alloc_contig(vm_size_t size, int flags, vm_paddr_t low, vm_paddr_t high,
390 u_long alignment, vm_paddr_t boundary, vm_memattr_t memattr)
393 return (kmem_alloc_contig_domainset(DOMAINSET_RR(), size, flags, low,
394 high, alignment, boundary, memattr));
398 kmem_alloc_contig_domainset(struct domainset *ds, vm_size_t size, int flags,
399 vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary,
400 vm_memattr_t memattr)
402 struct vm_domainset_iter di;
410 vm_domainset_iter_policy_init(&di, ds, &domain, &flags);
412 addr = kmem_alloc_contig_domain(domain, size, flags, low, high,
413 alignment, boundary, memattr);
416 if (start_segind == -1)
417 start_segind = vm_phys_lookup_segind(low);
418 if (vm_phys_find_range(bounds, start_segind, domain,
419 atop(round_page(size)), low, high) == -1) {
420 vm_domainset_iter_ignore(&di, domain);
422 } while (vm_domainset_iter_policy(&di, &domain) == 0);
430 * Initializes a map to manage a subrange
431 * of the kernel virtual address space.
433 * Arguments are as follows:
435 * parent Map to take range from
436 * min, max Returned endpoints of map
437 * size Size of range to find
438 * superpage_align Request that min is superpage aligned
441 kmem_subinit(vm_map_t map, vm_map_t parent, vm_offset_t *min, vm_offset_t *max,
442 vm_size_t size, bool superpage_align)
446 size = round_page(size);
448 *min = vm_map_min(parent);
449 ret = vm_map_find(parent, NULL, 0, min, size, 0, superpage_align ?
450 VMFS_SUPER_SPACE : VMFS_ANY_SPACE, VM_PROT_ALL, VM_PROT_ALL,
452 if (ret != KERN_SUCCESS)
453 panic("kmem_subinit: bad status return of %d", ret);
455 vm_map_init(map, vm_map_pmap(parent), *min, *max);
456 if (vm_map_submap(parent, *min, *max, map) != KERN_SUCCESS)
457 panic("kmem_subinit: unable to change range to submap");
461 * kmem_malloc_domain:
463 * Allocate wired-down pages in the kernel's address space.
466 kmem_malloc_domain(int domain, vm_size_t size, int flags)
473 if (__predict_true((flags & M_EXEC) == 0))
474 arena = vm_dom[domain].vmd_kernel_arena;
476 arena = vm_dom[domain].vmd_kernel_rwx_arena;
477 asize = round_page(size);
478 if (vmem_alloc(arena, asize, flags | M_BESTFIT, &addr))
481 rv = kmem_back_domain(domain, kernel_object, addr, asize, flags);
482 if (rv != KERN_SUCCESS) {
483 vmem_free(arena, addr, asize);
486 kasan_mark((void *)addr, size, asize, KASAN_KMEM_REDZONE);
487 return ((void *)addr);
491 kmem_malloc(vm_size_t size, int flags)
496 p = kmem_malloc_domainset(DOMAINSET_RR(), size, flags);
502 kmem_malloc_domainset(struct domainset *ds, vm_size_t size, int flags)
504 struct vm_domainset_iter di;
508 vm_domainset_iter_policy_init(&di, ds, &domain, &flags);
510 addr = kmem_malloc_domain(domain, size, flags);
513 } while (vm_domainset_iter_policy(&di, &domain) == 0);
521 * Allocate physical pages from the specified domain for the specified
522 * virtual address range.
525 kmem_back_domain(int domain, vm_object_t object, vm_offset_t addr,
526 vm_size_t size, int flags)
528 vm_offset_t offset, i;
533 KASSERT(object == kernel_object,
534 ("kmem_back_domain: only supports kernel object."));
536 offset = addr - VM_MIN_KERNEL_ADDRESS;
537 pflags = malloc2vm_flags(flags) | VM_ALLOC_WIRED;
538 pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL);
539 if (flags & M_WAITOK)
540 pflags |= VM_ALLOC_WAITFAIL;
541 prot = (flags & M_EXEC) != 0 ? VM_PROT_ALL : VM_PROT_RW;
544 VM_OBJECT_WLOCK(object);
546 mpred = vm_radix_lookup_le(&object->rtree, atop(offset + i));
547 for (; i < size; i += PAGE_SIZE, mpred = m) {
548 m = vm_page_alloc_domain_after(object, atop(offset + i),
549 domain, pflags, mpred);
552 * Ran out of space, free everything up and return. Don't need
553 * to lock page queues here as we know that the pages we got
554 * aren't on any queues.
557 if ((flags & M_NOWAIT) == 0)
559 VM_OBJECT_WUNLOCK(object);
560 kmem_unback(object, addr, i);
561 return (KERN_NO_SPACE);
563 KASSERT(vm_page_domain(m) == domain,
564 ("kmem_back_domain: Domain mismatch %d != %d",
565 vm_page_domain(m), domain));
566 if (flags & M_ZERO && (m->flags & PG_ZERO) == 0)
568 KASSERT((m->oflags & VPO_UNMANAGED) != 0,
569 ("kmem_malloc: page %p is managed", m));
571 pmap_enter(kernel_pmap, addr + i, m, prot,
572 prot | PMAP_ENTER_WIRED, 0);
573 if (__predict_false((prot & VM_PROT_EXECUTE) != 0))
574 m->oflags |= VPO_KMEM_EXEC;
576 VM_OBJECT_WUNLOCK(object);
577 kmem_alloc_san(addr, size, size, flags);
578 return (KERN_SUCCESS);
584 * Allocate physical pages for the specified virtual address range.
587 kmem_back(vm_object_t object, vm_offset_t addr, vm_size_t size, int flags)
589 vm_offset_t end, next, start;
592 KASSERT(object == kernel_object,
593 ("kmem_back: only supports kernel object."));
595 for (start = addr, end = addr + size; addr < end; addr = next) {
597 * We must ensure that pages backing a given large virtual page
598 * all come from the same physical domain.
600 if (vm_ndomains > 1) {
601 domain = (addr >> KVA_QUANTUM_SHIFT) % vm_ndomains;
602 while (VM_DOMAIN_EMPTY(domain))
604 next = roundup2(addr + 1, KVA_QUANTUM);
605 if (next > end || next < start)
611 rv = kmem_back_domain(domain, object, addr, next - addr, flags);
612 if (rv != KERN_SUCCESS) {
613 kmem_unback(object, start, addr - start);
623 * Unmap and free the physical pages underlying the specified virtual
626 * A physical page must exist within the specified object at each index
627 * that is being unmapped.
630 _kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size)
634 vm_offset_t end, offset;
637 KASSERT(object == kernel_object,
638 ("kmem_unback: only supports kernel object."));
642 pmap_remove(kernel_pmap, addr, addr + size);
643 offset = addr - VM_MIN_KERNEL_ADDRESS;
645 VM_OBJECT_WLOCK(object);
646 m = vm_page_lookup(object, atop(offset));
647 domain = vm_page_domain(m);
648 if (__predict_true((m->oflags & VPO_KMEM_EXEC) == 0))
649 arena = vm_dom[domain].vmd_kernel_arena;
651 arena = vm_dom[domain].vmd_kernel_rwx_arena;
652 for (; offset < end; offset += PAGE_SIZE, m = next) {
653 next = vm_page_next(m);
654 vm_page_xbusy_claim(m);
655 vm_page_unwire_noq(m);
658 VM_OBJECT_WUNLOCK(object);
664 kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size)
667 (void)_kmem_unback(object, addr, size);
673 * Free memory allocated with kmem_malloc. The size must match the
674 * original allocation.
677 kmem_free(void *addr, vm_size_t size)
681 size = round_page(size);
682 kasan_mark(addr, size, size, 0);
683 arena = _kmem_unback(kernel_object, (uintptr_t)addr, size);
685 vmem_free(arena, (uintptr_t)addr, size);
691 * Allocates pageable memory from a sub-map of the kernel. If the submap
692 * has no room, the caller sleeps waiting for more memory in the submap.
694 * This routine may block.
697 kmap_alloc_wait(vm_map_t map, vm_size_t size)
701 size = round_page(size);
702 if (!swap_reserve(size))
707 * To make this work for more than one map, use the map's lock
708 * to lock out sleepers/wakers.
711 addr = vm_map_findspace(map, vm_map_min(map), size);
712 if (addr + size <= vm_map_max(map))
714 /* no space now; see if we can ever get space */
715 if (vm_map_max(map) - vm_map_min(map) < size) {
720 map->needs_wakeup = TRUE;
721 vm_map_unlock_and_wait(map, 0);
723 vm_map_insert(map, NULL, 0, addr, addr + size, VM_PROT_RW, VM_PROT_RW,
732 * Returns memory to a submap of the kernel, and wakes up any processes
733 * waiting for memory in that map.
736 kmap_free_wakeup(vm_map_t map, vm_offset_t addr, vm_size_t size)
740 (void) vm_map_delete(map, trunc_page(addr), round_page(addr + size));
741 if (map->needs_wakeup) {
742 map->needs_wakeup = FALSE;
749 kmem_init_zero_region(void)
755 * Map a single physical page of zeros to a larger virtual range.
756 * This requires less looping in places that want large amounts of
757 * zeros, while not using much more physical resources.
759 addr = kva_alloc(ZERO_REGION_SIZE);
760 m = vm_page_alloc_noobj(VM_ALLOC_WIRED | VM_ALLOC_ZERO);
761 for (i = 0; i < ZERO_REGION_SIZE; i += PAGE_SIZE)
762 pmap_qenter(addr + i, &m, 1);
763 pmap_protect(kernel_pmap, addr, addr + ZERO_REGION_SIZE, VM_PROT_READ);
765 zero_region = (const void *)addr;
769 * Import KVA from the kernel map into the kernel arena.
772 kva_import(void *unused, vmem_size_t size, int flags, vmem_addr_t *addrp)
778 KASSERT((size % KVA_QUANTUM) == 0,
779 ("kva_import: Size %jd is not a multiple of %d",
780 (intmax_t)size, (int)KVA_QUANTUM));
781 addr = vm_map_min(kernel_map);
782 result = vm_map_find(kernel_map, NULL, 0, &addr, size, 0,
783 VMFS_SUPER_SPACE, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
784 if (result != KERN_SUCCESS) {
796 * Import KVA from a parent arena into a per-domain arena. Imports must be
797 * KVA_QUANTUM-aligned and a multiple of KVA_QUANTUM in size.
800 kva_import_domain(void *arena, vmem_size_t size, int flags, vmem_addr_t *addrp)
803 KASSERT((size % KVA_QUANTUM) == 0,
804 ("kva_import_domain: Size %jd is not a multiple of %d",
805 (intmax_t)size, (int)KVA_QUANTUM));
806 return (vmem_xalloc(arena, size, KVA_QUANTUM, 0, 0, VMEM_ADDR_MIN,
807 VMEM_ADDR_MAX, flags, addrp));
813 * Create the kernel map; insert a mapping covering kernel text,
814 * data, bss, and all space allocated thus far (`boostrap' data). The
815 * new map will thus map the range between VM_MIN_KERNEL_ADDRESS and
816 * `start' as allocated, and the range between `start' and `end' as free.
817 * Create the kernel vmem arena and its per-domain children.
820 kmem_init(vm_offset_t start, vm_offset_t end)
825 vm_map_init(kernel_map, kernel_pmap, VM_MIN_KERNEL_ADDRESS, end);
826 kernel_map->system_map = 1;
827 vm_map_lock(kernel_map);
828 /* N.B.: cannot use kgdb to debug, starting with this assignment ... */
829 (void)vm_map_insert(kernel_map, NULL, 0,
833 VM_MIN_KERNEL_ADDRESS,
835 start, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
836 /* ... and ending with the completion of the above `insert' */
840 * Mark KVA used for the page array as allocated. Other platforms
841 * that handle vm_page_array allocation can simply adjust virtual_avail
844 (void)vm_map_insert(kernel_map, NULL, 0, (vm_offset_t)vm_page_array,
845 (vm_offset_t)vm_page_array + round_2mpage(vm_page_array_size *
846 sizeof(struct vm_page)),
847 VM_PROT_RW, VM_PROT_RW, MAP_NOFAULT);
849 vm_map_unlock(kernel_map);
852 * Use a large import quantum on NUMA systems. This helps minimize
853 * interleaving of superpages, reducing internal fragmentation within
854 * the per-domain arenas.
856 if (vm_ndomains > 1 && PMAP_HAS_DMAP)
857 quantum = KVA_NUMA_IMPORT_QUANTUM;
859 quantum = KVA_QUANTUM;
862 * Initialize the kernel_arena. This can grow on demand.
864 vmem_init(kernel_arena, "kernel arena", 0, 0, PAGE_SIZE, 0, 0);
865 vmem_set_import(kernel_arena, kva_import, NULL, NULL, quantum);
867 for (domain = 0; domain < vm_ndomains; domain++) {
869 * Initialize the per-domain arenas. These are used to color
870 * the KVA space in a way that ensures that virtual large pages
871 * are backed by memory from the same physical domain,
872 * maximizing the potential for superpage promotion.
874 vm_dom[domain].vmd_kernel_arena = vmem_create(
875 "kernel arena domain", 0, 0, PAGE_SIZE, 0, M_WAITOK);
876 vmem_set_import(vm_dom[domain].vmd_kernel_arena,
877 kva_import_domain, NULL, kernel_arena, quantum);
880 * In architectures with superpages, maintain separate arenas
881 * for allocations with permissions that differ from the
882 * "standard" read/write permissions used for kernel memory,
883 * so as not to inhibit superpage promotion.
885 * Use the base import quantum since this arena is rarely used.
887 #if VM_NRESERVLEVEL > 0
888 vm_dom[domain].vmd_kernel_rwx_arena = vmem_create(
889 "kernel rwx arena domain", 0, 0, PAGE_SIZE, 0, M_WAITOK);
890 vmem_set_import(vm_dom[domain].vmd_kernel_rwx_arena,
891 kva_import_domain, (vmem_release_t *)vmem_xfree,
892 kernel_arena, KVA_QUANTUM);
894 vm_dom[domain].vmd_kernel_rwx_arena =
895 vm_dom[domain].vmd_kernel_arena;
900 * This must be the very first call so that the virtual address
901 * space used for early allocations is properly marked used in
908 * kmem_bootstrap_free:
910 * Free pages backing preloaded data (e.g., kernel modules) to the
911 * system. Currently only supported on platforms that create a
912 * vm_phys segment for preloaded data.
915 kmem_bootstrap_free(vm_offset_t start, vm_size_t size)
917 #if defined(__i386__) || defined(__amd64__)
918 struct vm_domain *vmd;
923 end = trunc_page(start + size);
924 start = round_page(start);
928 * Preloaded files do not have execute permissions by default on amd64.
929 * Restore the default permissions to ensure that the direct map alias
932 pmap_change_prot(start, end - start, VM_PROT_RW);
934 for (va = start; va < end; va += PAGE_SIZE) {
935 pa = pmap_kextract(va);
936 m = PHYS_TO_VM_PAGE(pa);
938 vmd = vm_pagequeue_domain(m);
939 vm_domain_free_lock(vmd);
940 vm_phys_free_pages(m, 0);
941 vm_domain_free_unlock(vmd);
943 vm_domain_freecnt_inc(vmd, 1);
944 vm_cnt.v_page_count++;
946 pmap_remove(kernel_pmap, start, end);
947 (void)vmem_add(kernel_arena, start, end - start, M_WAITOK);
951 #ifdef PMAP_WANT_ACTIVE_CPUS_NAIVE
953 pmap_active_cpus(pmap_t pmap, cpuset_t *res)
962 td = cpuid_to_pcpu[c]->pc_curthread;
966 vm = vmspace_acquire_ref(p);
969 if (pmap == vmspace_pmap(vm))
977 * Allow userspace to directly trigger the VM drain routine for testing
981 debug_vm_lowmem(SYSCTL_HANDLER_ARGS)
986 error = sysctl_handle_int(oidp, &i, 0, req);
989 if ((i & ~(VM_LOW_KMEM | VM_LOW_PAGES)) != 0)
992 EVENTHANDLER_INVOKE(vm_lowmem, i);
995 SYSCTL_PROC(_debug, OID_AUTO, vm_lowmem,
996 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, 0, debug_vm_lowmem, "I",
997 "set to trigger vm_lowmem event with given flags");
1000 debug_uma_reclaim(SYSCTL_HANDLER_ARGS)
1005 error = sysctl_handle_int(oidp, &i, 0, req);
1006 if (error != 0 || req->newptr == NULL)
1008 if (i != UMA_RECLAIM_TRIM && i != UMA_RECLAIM_DRAIN &&
1009 i != UMA_RECLAIM_DRAIN_CPU)
1014 SYSCTL_PROC(_debug, OID_AUTO, uma_reclaim,
1015 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, 0, debug_uma_reclaim, "I",
1016 "set to generate request to reclaim uma caches");
1019 debug_uma_reclaim_domain(SYSCTL_HANDLER_ARGS)
1021 int domain, error, request;
1024 error = sysctl_handle_int(oidp, &request, 0, req);
1025 if (error != 0 || req->newptr == NULL)
1028 domain = request >> 4;
1030 if (request != UMA_RECLAIM_TRIM && request != UMA_RECLAIM_DRAIN &&
1031 request != UMA_RECLAIM_DRAIN_CPU)
1033 if (domain < 0 || domain >= vm_ndomains)
1035 uma_reclaim_domain(request, domain);
1038 SYSCTL_PROC(_debug, OID_AUTO, uma_reclaim_domain,
1039 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, 0,
1040 debug_uma_reclaim_domain, "I",