1 /*- 2 * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU) 3 * 4 * Copyright (c) 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * The Mach Operating System project at Carnegie-Mellon University. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 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. 21 * 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 32 * SUCH DAMAGE. 33 * 34 * 35 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 36 * All rights reserved. 37 * 38 * Authors: Avadis Tevanian, Jr., Michael Wayne Young 39 * 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. 45 * 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. 49 * 50 * Carnegie Mellon requests users of this software to return to 51 * 52 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 53 * School of Computer Science 54 * Carnegie Mellon University 55 * Pittsburgh PA 15213-3890 56 * 57 * any improvements or extensions that they make and grant Carnegie the 58 * rights to redistribute these changes. 59 */ 60 61 /* 62 * Kernel memory management. 63 */ 64 65 #include <sys/cdefs.h> 66 #include "opt_vm.h" 67 68 #include <sys/param.h> 69 #include <sys/systm.h> 70 #include <sys/asan.h> 71 #include <sys/domainset.h> 72 #include <sys/eventhandler.h> 73 #include <sys/kernel.h> 74 #include <sys/lock.h> 75 #include <sys/malloc.h> 76 #include <sys/msan.h> 77 #include <sys/proc.h> 78 #include <sys/rwlock.h> 79 #include <sys/smp.h> 80 #include <sys/sysctl.h> 81 #include <sys/vmem.h> 82 #include <sys/vmmeter.h> 83 84 #include <vm/vm.h> 85 #include <vm/vm_param.h> 86 #include <vm/vm_domainset.h> 87 #include <vm/vm_kern.h> 88 #include <vm/pmap.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> 97 #include <vm/uma.h> 98 99 struct vm_map kernel_map_store; 100 struct vm_map exec_map_store; 101 struct vm_map pipe_map_store; 102 103 const void *zero_region; 104 CTASSERT((ZERO_REGION_SIZE & PAGE_MASK) == 0); 105 106 /* NB: Used by kernel debuggers. */ 107 const u_long vm_maxuser_address = VM_MAXUSER_ADDRESS; 108 109 u_int exec_map_entry_size; 110 u_int exec_map_entries; 111 112 SYSCTL_ULONG(_vm, OID_AUTO, min_kernel_address, CTLFLAG_RD, 113 SYSCTL_NULL_ULONG_PTR, VM_MIN_KERNEL_ADDRESS, "Min kernel address"); 114 115 SYSCTL_ULONG(_vm, OID_AUTO, max_kernel_address, CTLFLAG_RD, 116 #if defined(__arm__) 117 &vm_max_kernel_address, 0, 118 #else 119 SYSCTL_NULL_ULONG_PTR, VM_MAX_KERNEL_ADDRESS, 120 #endif 121 "Max kernel address"); 122 123 #if VM_NRESERVLEVEL > 0 124 #define KVA_QUANTUM_SHIFT (VM_LEVEL_0_ORDER + PAGE_SHIFT) 125 #else 126 /* On non-superpage architectures we want large import sizes. */ 127 #define KVA_QUANTUM_SHIFT (8 + PAGE_SHIFT) 128 #endif 129 #define KVA_QUANTUM (1ul << KVA_QUANTUM_SHIFT) 130 #define KVA_NUMA_IMPORT_QUANTUM (KVA_QUANTUM * 128) 131 132 extern void uma_startup2(void); 133 134 /* 135 * kva_alloc: 136 * 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. 142 */ 143 vm_offset_t 144 kva_alloc(vm_size_t size) 145 { 146 vm_offset_t addr; 147 148 TSENTER(); 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)) 152 return (0); 153 TSEXIT(); 154 155 return (addr); 156 } 157 158 /* 159 * kva_alloc_aligned: 160 * 161 * Allocate a virtual address range as in kva_alloc where the base 162 * address is aligned to align. 163 */ 164 vm_offset_t 165 kva_alloc_aligned(vm_size_t size, vm_size_t align) 166 { 167 vm_offset_t addr; 168 169 TSENTER(); 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)) 173 return (0); 174 TSEXIT(); 175 176 return (addr); 177 } 178 179 /* 180 * kva_free: 181 * 182 * Release a region of kernel virtual memory allocated 183 * with kva_alloc, and return the physical pages 184 * associated with that region. 185 * 186 * This routine may not block on kernel maps. 187 */ 188 void 189 kva_free(vm_offset_t addr, vm_size_t size) 190 { 191 192 size = round_page(size); 193 vmem_xfree(kernel_arena, addr, size); 194 } 195 196 /* 197 * Update sanitizer shadow state to reflect a new allocation. Force inlining to 198 * help make KMSAN origin tracking more precise. 199 */ 200 static __always_inline void 201 kmem_alloc_san(vm_offset_t addr, vm_size_t size, vm_size_t asize, int flags) 202 { 203 if ((flags & M_ZERO) == 0) { 204 kmsan_mark((void *)addr, asize, KMSAN_STATE_UNINIT); 205 kmsan_orig((void *)addr, asize, KMSAN_TYPE_KMEM, 206 KMSAN_RET_ADDR); 207 } else { 208 kmsan_mark((void *)addr, asize, KMSAN_STATE_INITED); 209 } 210 kasan_mark((void *)addr, size, asize, KASAN_KMEM_REDZONE); 211 } 212 213 static vm_page_t 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) 217 { 218 vm_page_t m; 219 int tries; 220 bool wait, reclaim; 221 222 VM_OBJECT_ASSERT_WLOCKED(object); 223 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) 232 break; 233 234 VM_OBJECT_WUNLOCK(object); 235 if (!vm_page_reclaim_contig_domain(domain, pflags, npages, 236 low, high, alignment, boundary) && wait) 237 vm_wait_domain(domain); 238 VM_OBJECT_WLOCK(object); 239 } 240 return (m); 241 } 242 243 /* 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. 250 */ 251 static void * 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) 254 { 255 vmem_t *vmem; 256 vm_object_t object; 257 vm_offset_t addr, i, offset; 258 vm_page_t m; 259 vm_size_t asize; 260 int pflags; 261 vm_prot_t prot; 262 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)) 267 return (0); 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); 275 if (m == NULL) { 276 VM_OBJECT_WUNLOCK(object); 277 kmem_unback(object, addr, i); 278 vmem_free(vmem, addr, asize); 279 return (0); 280 } 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) 285 pmap_zero_page(m); 286 vm_page_valid(m); 287 pmap_enter(kernel_pmap, addr + i, m, prot, 288 prot | PMAP_ENTER_WIRED, 0); 289 } 290 VM_OBJECT_WUNLOCK(object); 291 kmem_alloc_san(addr, size, asize, flags); 292 return ((void *)addr); 293 } 294 295 void * 296 kmem_alloc_attr(vm_size_t size, int flags, vm_paddr_t low, vm_paddr_t high, 297 vm_memattr_t memattr) 298 { 299 300 return (kmem_alloc_attr_domainset(DOMAINSET_RR(), size, flags, low, 301 high, memattr)); 302 } 303 304 void * 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) 307 { 308 struct vm_domainset_iter di; 309 void *addr; 310 int domain; 311 312 vm_domainset_iter_policy_init(&di, ds, &domain, &flags); 313 do { 314 addr = kmem_alloc_attr_domain(domain, size, flags, low, high, 315 memattr); 316 if (addr != NULL) 317 break; 318 } while (vm_domainset_iter_policy(&di, &domain) == 0); 319 320 return (addr); 321 } 322 323 /* 324 * Allocates a region from the kernel address map and physically 325 * contiguous pages within the specified address range to the kernel 326 * object. Creates a wired mapping from this region to these pages, and 327 * returns the region's starting virtual address. If M_ZERO is specified 328 * through the given flags, then the pages are zeroed before they are 329 * mapped. 330 */ 331 static void * 332 kmem_alloc_contig_domain(int domain, vm_size_t size, int flags, vm_paddr_t low, 333 vm_paddr_t high, u_long alignment, vm_paddr_t boundary, 334 vm_memattr_t memattr) 335 { 336 vmem_t *vmem; 337 vm_object_t object; 338 vm_offset_t addr, offset, tmp; 339 vm_page_t end_m, m; 340 vm_size_t asize; 341 u_long npages; 342 int pflags; 343 344 object = kernel_object; 345 asize = round_page(size); 346 vmem = vm_dom[domain].vmd_kernel_arena; 347 if (vmem_alloc(vmem, asize, flags | M_BESTFIT, &addr)) 348 return (NULL); 349 offset = addr - VM_MIN_KERNEL_ADDRESS; 350 pflags = malloc2vm_flags(flags) | VM_ALLOC_WIRED; 351 npages = atop(asize); 352 VM_OBJECT_WLOCK(object); 353 m = kmem_alloc_contig_pages(object, atop(offset), domain, 354 pflags, npages, low, high, alignment, boundary, memattr); 355 if (m == NULL) { 356 VM_OBJECT_WUNLOCK(object); 357 vmem_free(vmem, addr, asize); 358 return (NULL); 359 } 360 KASSERT(vm_page_domain(m) == domain, 361 ("kmem_alloc_contig_domain: Domain mismatch %d != %d", 362 vm_page_domain(m), domain)); 363 end_m = m + npages; 364 tmp = addr; 365 for (; m < end_m; m++) { 366 if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0) 367 pmap_zero_page(m); 368 vm_page_valid(m); 369 pmap_enter(kernel_pmap, tmp, m, VM_PROT_RW, 370 VM_PROT_RW | PMAP_ENTER_WIRED, 0); 371 tmp += PAGE_SIZE; 372 } 373 VM_OBJECT_WUNLOCK(object); 374 kmem_alloc_san(addr, size, asize, flags); 375 return ((void *)addr); 376 } 377 378 void * 379 kmem_alloc_contig(vm_size_t size, int flags, vm_paddr_t low, vm_paddr_t high, 380 u_long alignment, vm_paddr_t boundary, vm_memattr_t memattr) 381 { 382 383 return (kmem_alloc_contig_domainset(DOMAINSET_RR(), size, flags, low, 384 high, alignment, boundary, memattr)); 385 } 386 387 void * 388 kmem_alloc_contig_domainset(struct domainset *ds, vm_size_t size, int flags, 389 vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary, 390 vm_memattr_t memattr) 391 { 392 struct vm_domainset_iter di; 393 void *addr; 394 int domain; 395 396 vm_domainset_iter_policy_init(&di, ds, &domain, &flags); 397 do { 398 addr = kmem_alloc_contig_domain(domain, size, flags, low, high, 399 alignment, boundary, memattr); 400 if (addr != NULL) 401 break; 402 } while (vm_domainset_iter_policy(&di, &domain) == 0); 403 404 return (addr); 405 } 406 407 /* 408 * kmem_subinit: 409 * 410 * Initializes a map to manage a subrange 411 * of the kernel virtual address space. 412 * 413 * Arguments are as follows: 414 * 415 * parent Map to take range from 416 * min, max Returned endpoints of map 417 * size Size of range to find 418 * superpage_align Request that min is superpage aligned 419 */ 420 void 421 kmem_subinit(vm_map_t map, vm_map_t parent, vm_offset_t *min, vm_offset_t *max, 422 vm_size_t size, bool superpage_align) 423 { 424 int ret; 425 426 size = round_page(size); 427 428 *min = vm_map_min(parent); 429 ret = vm_map_find(parent, NULL, 0, min, size, 0, superpage_align ? 430 VMFS_SUPER_SPACE : VMFS_ANY_SPACE, VM_PROT_ALL, VM_PROT_ALL, 431 MAP_ACC_NO_CHARGE); 432 if (ret != KERN_SUCCESS) 433 panic("kmem_subinit: bad status return of %d", ret); 434 *max = *min + size; 435 vm_map_init(map, vm_map_pmap(parent), *min, *max); 436 if (vm_map_submap(parent, *min, *max, map) != KERN_SUCCESS) 437 panic("kmem_subinit: unable to change range to submap"); 438 } 439 440 /* 441 * kmem_malloc_domain: 442 * 443 * Allocate wired-down pages in the kernel's address space. 444 */ 445 static void * 446 kmem_malloc_domain(int domain, vm_size_t size, int flags) 447 { 448 vmem_t *arena; 449 vm_offset_t addr; 450 vm_size_t asize; 451 int rv; 452 453 if (__predict_true((flags & M_EXEC) == 0)) 454 arena = vm_dom[domain].vmd_kernel_arena; 455 else 456 arena = vm_dom[domain].vmd_kernel_rwx_arena; 457 asize = round_page(size); 458 if (vmem_alloc(arena, asize, flags | M_BESTFIT, &addr)) 459 return (0); 460 461 rv = kmem_back_domain(domain, kernel_object, addr, asize, flags); 462 if (rv != KERN_SUCCESS) { 463 vmem_free(arena, addr, asize); 464 return (0); 465 } 466 kasan_mark((void *)addr, size, asize, KASAN_KMEM_REDZONE); 467 return ((void *)addr); 468 } 469 470 void * 471 kmem_malloc(vm_size_t size, int flags) 472 { 473 void * p; 474 475 TSENTER(); 476 p = kmem_malloc_domainset(DOMAINSET_RR(), size, flags); 477 TSEXIT(); 478 return (p); 479 } 480 481 void * 482 kmem_malloc_domainset(struct domainset *ds, vm_size_t size, int flags) 483 { 484 struct vm_domainset_iter di; 485 void *addr; 486 int domain; 487 488 vm_domainset_iter_policy_init(&di, ds, &domain, &flags); 489 do { 490 addr = kmem_malloc_domain(domain, size, flags); 491 if (addr != NULL) 492 break; 493 } while (vm_domainset_iter_policy(&di, &domain) == 0); 494 495 return (addr); 496 } 497 498 /* 499 * kmem_back_domain: 500 * 501 * Allocate physical pages from the specified domain for the specified 502 * virtual address range. 503 */ 504 int 505 kmem_back_domain(int domain, vm_object_t object, vm_offset_t addr, 506 vm_size_t size, int flags) 507 { 508 vm_offset_t offset, i; 509 vm_page_t m, mpred; 510 vm_prot_t prot; 511 int pflags; 512 513 KASSERT(object == kernel_object, 514 ("kmem_back_domain: only supports kernel object.")); 515 516 offset = addr - VM_MIN_KERNEL_ADDRESS; 517 pflags = malloc2vm_flags(flags) | VM_ALLOC_WIRED; 518 pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL); 519 if (flags & M_WAITOK) 520 pflags |= VM_ALLOC_WAITFAIL; 521 prot = (flags & M_EXEC) != 0 ? VM_PROT_ALL : VM_PROT_RW; 522 523 i = 0; 524 VM_OBJECT_WLOCK(object); 525 retry: 526 mpred = vm_radix_lookup_le(&object->rtree, atop(offset + i)); 527 for (; i < size; i += PAGE_SIZE, mpred = m) { 528 m = vm_page_alloc_domain_after(object, atop(offset + i), 529 domain, pflags, mpred); 530 531 /* 532 * Ran out of space, free everything up and return. Don't need 533 * to lock page queues here as we know that the pages we got 534 * aren't on any queues. 535 */ 536 if (m == NULL) { 537 if ((flags & M_NOWAIT) == 0) 538 goto retry; 539 VM_OBJECT_WUNLOCK(object); 540 kmem_unback(object, addr, i); 541 return (KERN_NO_SPACE); 542 } 543 KASSERT(vm_page_domain(m) == domain, 544 ("kmem_back_domain: Domain mismatch %d != %d", 545 vm_page_domain(m), domain)); 546 if (flags & M_ZERO && (m->flags & PG_ZERO) == 0) 547 pmap_zero_page(m); 548 KASSERT((m->oflags & VPO_UNMANAGED) != 0, 549 ("kmem_malloc: page %p is managed", m)); 550 vm_page_valid(m); 551 pmap_enter(kernel_pmap, addr + i, m, prot, 552 prot | PMAP_ENTER_WIRED, 0); 553 if (__predict_false((prot & VM_PROT_EXECUTE) != 0)) 554 m->oflags |= VPO_KMEM_EXEC; 555 } 556 VM_OBJECT_WUNLOCK(object); 557 kmem_alloc_san(addr, size, size, flags); 558 return (KERN_SUCCESS); 559 } 560 561 /* 562 * kmem_back: 563 * 564 * Allocate physical pages for the specified virtual address range. 565 */ 566 int 567 kmem_back(vm_object_t object, vm_offset_t addr, vm_size_t size, int flags) 568 { 569 vm_offset_t end, next, start; 570 int domain, rv; 571 572 KASSERT(object == kernel_object, 573 ("kmem_back: only supports kernel object.")); 574 575 for (start = addr, end = addr + size; addr < end; addr = next) { 576 /* 577 * We must ensure that pages backing a given large virtual page 578 * all come from the same physical domain. 579 */ 580 if (vm_ndomains > 1) { 581 domain = (addr >> KVA_QUANTUM_SHIFT) % vm_ndomains; 582 while (VM_DOMAIN_EMPTY(domain)) 583 domain++; 584 next = roundup2(addr + 1, KVA_QUANTUM); 585 if (next > end || next < start) 586 next = end; 587 } else { 588 domain = 0; 589 next = end; 590 } 591 rv = kmem_back_domain(domain, object, addr, next - addr, flags); 592 if (rv != KERN_SUCCESS) { 593 kmem_unback(object, start, addr - start); 594 break; 595 } 596 } 597 return (rv); 598 } 599 600 /* 601 * kmem_unback: 602 * 603 * Unmap and free the physical pages underlying the specified virtual 604 * address range. 605 * 606 * A physical page must exist within the specified object at each index 607 * that is being unmapped. 608 */ 609 static struct vmem * 610 _kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size) 611 { 612 struct vmem *arena; 613 vm_page_t m, next; 614 vm_offset_t end, offset; 615 int domain; 616 617 KASSERT(object == kernel_object, 618 ("kmem_unback: only supports kernel object.")); 619 620 if (size == 0) 621 return (NULL); 622 pmap_remove(kernel_pmap, addr, addr + size); 623 offset = addr - VM_MIN_KERNEL_ADDRESS; 624 end = offset + size; 625 VM_OBJECT_WLOCK(object); 626 m = vm_page_lookup(object, atop(offset)); 627 domain = vm_page_domain(m); 628 if (__predict_true((m->oflags & VPO_KMEM_EXEC) == 0)) 629 arena = vm_dom[domain].vmd_kernel_arena; 630 else 631 arena = vm_dom[domain].vmd_kernel_rwx_arena; 632 for (; offset < end; offset += PAGE_SIZE, m = next) { 633 next = vm_page_next(m); 634 vm_page_xbusy_claim(m); 635 vm_page_unwire_noq(m); 636 vm_page_free(m); 637 } 638 VM_OBJECT_WUNLOCK(object); 639 640 return (arena); 641 } 642 643 void 644 kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size) 645 { 646 647 (void)_kmem_unback(object, addr, size); 648 } 649 650 /* 651 * kmem_free: 652 * 653 * Free memory allocated with kmem_malloc. The size must match the 654 * original allocation. 655 */ 656 void 657 kmem_free(void *addr, vm_size_t size) 658 { 659 struct vmem *arena; 660 661 size = round_page(size); 662 kasan_mark(addr, size, size, 0); 663 arena = _kmem_unback(kernel_object, (uintptr_t)addr, size); 664 if (arena != NULL) 665 vmem_free(arena, (uintptr_t)addr, size); 666 } 667 668 /* 669 * kmap_alloc_wait: 670 * 671 * Allocates pageable memory from a sub-map of the kernel. If the submap 672 * has no room, the caller sleeps waiting for more memory in the submap. 673 * 674 * This routine may block. 675 */ 676 vm_offset_t 677 kmap_alloc_wait(vm_map_t map, vm_size_t size) 678 { 679 vm_offset_t addr; 680 681 size = round_page(size); 682 if (!swap_reserve(size)) 683 return (0); 684 685 for (;;) { 686 /* 687 * To make this work for more than one map, use the map's lock 688 * to lock out sleepers/wakers. 689 */ 690 vm_map_lock(map); 691 addr = vm_map_findspace(map, vm_map_min(map), size); 692 if (addr + size <= vm_map_max(map)) 693 break; 694 /* no space now; see if we can ever get space */ 695 if (vm_map_max(map) - vm_map_min(map) < size) { 696 vm_map_unlock(map); 697 swap_release(size); 698 return (0); 699 } 700 map->needs_wakeup = TRUE; 701 vm_map_unlock_and_wait(map, 0); 702 } 703 vm_map_insert(map, NULL, 0, addr, addr + size, VM_PROT_RW, VM_PROT_RW, 704 MAP_ACC_CHARGED); 705 vm_map_unlock(map); 706 return (addr); 707 } 708 709 /* 710 * kmap_free_wakeup: 711 * 712 * Returns memory to a submap of the kernel, and wakes up any processes 713 * waiting for memory in that map. 714 */ 715 void 716 kmap_free_wakeup(vm_map_t map, vm_offset_t addr, vm_size_t size) 717 { 718 719 vm_map_lock(map); 720 (void) vm_map_delete(map, trunc_page(addr), round_page(addr + size)); 721 if (map->needs_wakeup) { 722 map->needs_wakeup = FALSE; 723 vm_map_wakeup(map); 724 } 725 vm_map_unlock(map); 726 } 727 728 void 729 kmem_init_zero_region(void) 730 { 731 vm_offset_t addr, i; 732 vm_page_t m; 733 734 /* 735 * Map a single physical page of zeros to a larger virtual range. 736 * This requires less looping in places that want large amounts of 737 * zeros, while not using much more physical resources. 738 */ 739 addr = kva_alloc(ZERO_REGION_SIZE); 740 m = vm_page_alloc_noobj(VM_ALLOC_WIRED | VM_ALLOC_ZERO); 741 for (i = 0; i < ZERO_REGION_SIZE; i += PAGE_SIZE) 742 pmap_qenter(addr + i, &m, 1); 743 pmap_protect(kernel_pmap, addr, addr + ZERO_REGION_SIZE, VM_PROT_READ); 744 745 zero_region = (const void *)addr; 746 } 747 748 /* 749 * Import KVA from the kernel map into the kernel arena. 750 */ 751 static int 752 kva_import(void *unused, vmem_size_t size, int flags, vmem_addr_t *addrp) 753 { 754 vm_offset_t addr; 755 int result; 756 757 TSENTER(); 758 KASSERT((size % KVA_QUANTUM) == 0, 759 ("kva_import: Size %jd is not a multiple of %d", 760 (intmax_t)size, (int)KVA_QUANTUM)); 761 addr = vm_map_min(kernel_map); 762 result = vm_map_find(kernel_map, NULL, 0, &addr, size, 0, 763 VMFS_SUPER_SPACE, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT); 764 if (result != KERN_SUCCESS) { 765 TSEXIT(); 766 return (ENOMEM); 767 } 768 769 *addrp = addr; 770 771 TSEXIT(); 772 return (0); 773 } 774 775 /* 776 * Import KVA from a parent arena into a per-domain arena. Imports must be 777 * KVA_QUANTUM-aligned and a multiple of KVA_QUANTUM in size. 778 */ 779 static int 780 kva_import_domain(void *arena, vmem_size_t size, int flags, vmem_addr_t *addrp) 781 { 782 783 KASSERT((size % KVA_QUANTUM) == 0, 784 ("kva_import_domain: Size %jd is not a multiple of %d", 785 (intmax_t)size, (int)KVA_QUANTUM)); 786 return (vmem_xalloc(arena, size, KVA_QUANTUM, 0, 0, VMEM_ADDR_MIN, 787 VMEM_ADDR_MAX, flags, addrp)); 788 } 789 790 /* 791 * kmem_init: 792 * 793 * Create the kernel map; insert a mapping covering kernel text, 794 * data, bss, and all space allocated thus far (`boostrap' data). The 795 * new map will thus map the range between VM_MIN_KERNEL_ADDRESS and 796 * `start' as allocated, and the range between `start' and `end' as free. 797 * Create the kernel vmem arena and its per-domain children. 798 */ 799 void 800 kmem_init(vm_offset_t start, vm_offset_t end) 801 { 802 vm_size_t quantum; 803 int domain; 804 805 vm_map_init(kernel_map, kernel_pmap, VM_MIN_KERNEL_ADDRESS, end); 806 kernel_map->system_map = 1; 807 vm_map_lock(kernel_map); 808 /* N.B.: cannot use kgdb to debug, starting with this assignment ... */ 809 (void)vm_map_insert(kernel_map, NULL, 0, 810 #ifdef __amd64__ 811 KERNBASE, 812 #else 813 VM_MIN_KERNEL_ADDRESS, 814 #endif 815 start, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT); 816 /* ... and ending with the completion of the above `insert' */ 817 818 #ifdef __amd64__ 819 /* 820 * Mark KVA used for the page array as allocated. Other platforms 821 * that handle vm_page_array allocation can simply adjust virtual_avail 822 * instead. 823 */ 824 (void)vm_map_insert(kernel_map, NULL, 0, (vm_offset_t)vm_page_array, 825 (vm_offset_t)vm_page_array + round_2mpage(vm_page_array_size * 826 sizeof(struct vm_page)), 827 VM_PROT_RW, VM_PROT_RW, MAP_NOFAULT); 828 #endif 829 vm_map_unlock(kernel_map); 830 831 /* 832 * Use a large import quantum on NUMA systems. This helps minimize 833 * interleaving of superpages, reducing internal fragmentation within 834 * the per-domain arenas. 835 */ 836 if (vm_ndomains > 1 && PMAP_HAS_DMAP) 837 quantum = KVA_NUMA_IMPORT_QUANTUM; 838 else 839 quantum = KVA_QUANTUM; 840 841 /* 842 * Initialize the kernel_arena. This can grow on demand. 843 */ 844 vmem_init(kernel_arena, "kernel arena", 0, 0, PAGE_SIZE, 0, 0); 845 vmem_set_import(kernel_arena, kva_import, NULL, NULL, quantum); 846 847 for (domain = 0; domain < vm_ndomains; domain++) { 848 /* 849 * Initialize the per-domain arenas. These are used to color 850 * the KVA space in a way that ensures that virtual large pages 851 * are backed by memory from the same physical domain, 852 * maximizing the potential for superpage promotion. 853 */ 854 vm_dom[domain].vmd_kernel_arena = vmem_create( 855 "kernel arena domain", 0, 0, PAGE_SIZE, 0, M_WAITOK); 856 vmem_set_import(vm_dom[domain].vmd_kernel_arena, 857 kva_import_domain, NULL, kernel_arena, quantum); 858 859 /* 860 * In architectures with superpages, maintain separate arenas 861 * for allocations with permissions that differ from the 862 * "standard" read/write permissions used for kernel memory, 863 * so as not to inhibit superpage promotion. 864 * 865 * Use the base import quantum since this arena is rarely used. 866 */ 867 #if VM_NRESERVLEVEL > 0 868 vm_dom[domain].vmd_kernel_rwx_arena = vmem_create( 869 "kernel rwx arena domain", 0, 0, PAGE_SIZE, 0, M_WAITOK); 870 vmem_set_import(vm_dom[domain].vmd_kernel_rwx_arena, 871 kva_import_domain, (vmem_release_t *)vmem_xfree, 872 kernel_arena, KVA_QUANTUM); 873 #else 874 vm_dom[domain].vmd_kernel_rwx_arena = 875 vm_dom[domain].vmd_kernel_arena; 876 #endif 877 } 878 879 /* 880 * This must be the very first call so that the virtual address 881 * space used for early allocations is properly marked used in 882 * the map. 883 */ 884 uma_startup2(); 885 } 886 887 /* 888 * kmem_bootstrap_free: 889 * 890 * Free pages backing preloaded data (e.g., kernel modules) to the 891 * system. Currently only supported on platforms that create a 892 * vm_phys segment for preloaded data. 893 */ 894 void 895 kmem_bootstrap_free(vm_offset_t start, vm_size_t size) 896 { 897 #if defined(__i386__) || defined(__amd64__) 898 struct vm_domain *vmd; 899 vm_offset_t end, va; 900 vm_paddr_t pa; 901 vm_page_t m; 902 903 end = trunc_page(start + size); 904 start = round_page(start); 905 906 #ifdef __amd64__ 907 /* 908 * Preloaded files do not have execute permissions by default on amd64. 909 * Restore the default permissions to ensure that the direct map alias 910 * is updated. 911 */ 912 pmap_change_prot(start, end - start, VM_PROT_RW); 913 #endif 914 for (va = start; va < end; va += PAGE_SIZE) { 915 pa = pmap_kextract(va); 916 m = PHYS_TO_VM_PAGE(pa); 917 918 vmd = vm_pagequeue_domain(m); 919 vm_domain_free_lock(vmd); 920 vm_phys_free_pages(m, 0); 921 vm_domain_free_unlock(vmd); 922 923 vm_domain_freecnt_inc(vmd, 1); 924 vm_cnt.v_page_count++; 925 } 926 pmap_remove(kernel_pmap, start, end); 927 (void)vmem_add(kernel_arena, start, end - start, M_WAITOK); 928 #endif 929 } 930 931 #ifdef PMAP_WANT_ACTIVE_CPUS_NAIVE 932 void 933 pmap_active_cpus(pmap_t pmap, cpuset_t *res) 934 { 935 struct thread *td; 936 struct proc *p; 937 struct vmspace *vm; 938 int c; 939 940 CPU_ZERO(res); 941 CPU_FOREACH(c) { 942 td = cpuid_to_pcpu[c]->pc_curthread; 943 p = td->td_proc; 944 if (p == NULL) 945 continue; 946 vm = vmspace_acquire_ref(p); 947 if (vm == NULL) 948 continue; 949 if (pmap == vmspace_pmap(vm)) 950 CPU_SET(c, res); 951 vmspace_free(vm); 952 } 953 } 954 #endif 955 956 /* 957 * Allow userspace to directly trigger the VM drain routine for testing 958 * purposes. 959 */ 960 static int 961 debug_vm_lowmem(SYSCTL_HANDLER_ARGS) 962 { 963 int error, i; 964 965 i = 0; 966 error = sysctl_handle_int(oidp, &i, 0, req); 967 if (error != 0) 968 return (error); 969 if ((i & ~(VM_LOW_KMEM | VM_LOW_PAGES)) != 0) 970 return (EINVAL); 971 if (i != 0) 972 EVENTHANDLER_INVOKE(vm_lowmem, i); 973 return (0); 974 } 975 SYSCTL_PROC(_debug, OID_AUTO, vm_lowmem, 976 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, 0, debug_vm_lowmem, "I", 977 "set to trigger vm_lowmem event with given flags"); 978 979 static int 980 debug_uma_reclaim(SYSCTL_HANDLER_ARGS) 981 { 982 int error, i; 983 984 i = 0; 985 error = sysctl_handle_int(oidp, &i, 0, req); 986 if (error != 0 || req->newptr == NULL) 987 return (error); 988 if (i != UMA_RECLAIM_TRIM && i != UMA_RECLAIM_DRAIN && 989 i != UMA_RECLAIM_DRAIN_CPU) 990 return (EINVAL); 991 uma_reclaim(i); 992 return (0); 993 } 994 SYSCTL_PROC(_debug, OID_AUTO, uma_reclaim, 995 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, 0, debug_uma_reclaim, "I", 996 "set to generate request to reclaim uma caches"); 997 998 static int 999 debug_uma_reclaim_domain(SYSCTL_HANDLER_ARGS) 1000 { 1001 int domain, error, request; 1002 1003 request = 0; 1004 error = sysctl_handle_int(oidp, &request, 0, req); 1005 if (error != 0 || req->newptr == NULL) 1006 return (error); 1007 1008 domain = request >> 4; 1009 request &= 0xf; 1010 if (request != UMA_RECLAIM_TRIM && request != UMA_RECLAIM_DRAIN && 1011 request != UMA_RECLAIM_DRAIN_CPU) 1012 return (EINVAL); 1013 if (domain < 0 || domain >= vm_ndomains) 1014 return (EINVAL); 1015 uma_reclaim_domain(request, domain); 1016 return (0); 1017 } 1018 SYSCTL_PROC(_debug, OID_AUTO, uma_reclaim_domain, 1019 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, 0, 1020 debug_uma_reclaim_domain, "I", 1021 ""); 1022