1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 1993 Linus Torvalds 4 * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999 5 * SMP-safe vmalloc/vfree/ioremap, Tigran Aivazian <tigran@veritas.com>, May 2000 6 * Major rework to support vmap/vunmap, Christoph Hellwig, SGI, August 2002 7 * Numa awareness, Christoph Lameter, SGI, June 2005 8 * Improving global KVA allocator, Uladzislau Rezki, Sony, May 2019 9 */ 10 11 #include <linux/vmalloc.h> 12 #include <linux/mm.h> 13 #include <linux/module.h> 14 #include <linux/highmem.h> 15 #include <linux/sched/signal.h> 16 #include <linux/slab.h> 17 #include <linux/spinlock.h> 18 #include <linux/interrupt.h> 19 #include <linux/proc_fs.h> 20 #include <linux/seq_file.h> 21 #include <linux/set_memory.h> 22 #include <linux/debugobjects.h> 23 #include <linux/kallsyms.h> 24 #include <linux/list.h> 25 #include <linux/notifier.h> 26 #include <linux/rbtree.h> 27 #include <linux/xarray.h> 28 #include <linux/io.h> 29 #include <linux/rcupdate.h> 30 #include <linux/pfn.h> 31 #include <linux/kmemleak.h> 32 #include <linux/atomic.h> 33 #include <linux/compiler.h> 34 #include <linux/memcontrol.h> 35 #include <linux/llist.h> 36 #include <linux/uio.h> 37 #include <linux/bitops.h> 38 #include <linux/rbtree_augmented.h> 39 #include <linux/overflow.h> 40 #include <linux/pgtable.h> 41 #include <linux/hugetlb.h> 42 #include <linux/sched/mm.h> 43 #include <asm/tlbflush.h> 44 #include <asm/shmparam.h> 45 #include <linux/page_owner.h> 46 #include <linux/cleanup.h> 47 48 #define CREATE_TRACE_POINTS 49 #include <trace/events/vmalloc.h> 50 51 #include "internal.h" 52 #include "pgalloc-track.h" 53 54 #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP 55 static unsigned int __ro_after_init ioremap_max_page_shift = BITS_PER_LONG - 1; 56 57 static int __init set_nohugeiomap(char *str) 58 { 59 ioremap_max_page_shift = PAGE_SHIFT; 60 return 0; 61 } 62 early_param("nohugeiomap", set_nohugeiomap); 63 #else /* CONFIG_HAVE_ARCH_HUGE_VMAP */ 64 static const unsigned int ioremap_max_page_shift = PAGE_SHIFT; 65 #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */ 66 67 #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC 68 static bool __ro_after_init vmap_allow_huge = true; 69 70 static int __init set_nohugevmalloc(char *str) 71 { 72 vmap_allow_huge = false; 73 return 0; 74 } 75 early_param("nohugevmalloc", set_nohugevmalloc); 76 #else /* CONFIG_HAVE_ARCH_HUGE_VMALLOC */ 77 static const bool vmap_allow_huge = false; 78 #endif /* CONFIG_HAVE_ARCH_HUGE_VMALLOC */ 79 80 bool is_vmalloc_addr(const void *x) 81 { 82 unsigned long addr = (unsigned long)kasan_reset_tag(x); 83 84 return addr >= VMALLOC_START && addr < VMALLOC_END; 85 } 86 EXPORT_SYMBOL(is_vmalloc_addr); 87 88 struct vfree_deferred { 89 struct llist_head list; 90 struct work_struct wq; 91 }; 92 static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred); 93 94 /*** Page table manipulation functions ***/ 95 static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, 96 phys_addr_t phys_addr, pgprot_t prot, 97 unsigned int max_page_shift, pgtbl_mod_mask *mask) 98 { 99 pte_t *pte; 100 u64 pfn; 101 struct page *page; 102 unsigned long size = PAGE_SIZE; 103 104 if (WARN_ON_ONCE(!PAGE_ALIGNED(end - addr))) 105 return -EINVAL; 106 107 pfn = phys_addr >> PAGE_SHIFT; 108 pte = pte_alloc_kernel_track(pmd, addr, mask); 109 if (!pte) 110 return -ENOMEM; 111 112 lazy_mmu_mode_enable(); 113 114 do { 115 if (unlikely(!pte_none(ptep_get(pte)))) { 116 if (pfn_valid(pfn)) { 117 page = pfn_to_page(pfn); 118 dump_page(page, "remapping already mapped page"); 119 } 120 BUG(); 121 } 122 123 #ifdef CONFIG_HUGETLB_PAGE 124 size = arch_vmap_pte_range_map_size(addr, end, pfn, max_page_shift); 125 if (size != PAGE_SIZE) { 126 pte_t entry = pfn_pte(pfn, prot); 127 128 entry = arch_make_huge_pte(entry, ilog2(size), 0); 129 set_huge_pte_at(&init_mm, addr, pte, entry, size); 130 pfn += PFN_DOWN(size); 131 continue; 132 } 133 #endif 134 set_pte_at(&init_mm, addr, pte, pfn_pte(pfn, prot)); 135 pfn++; 136 } while (pte += PFN_DOWN(size), addr += size, addr != end); 137 138 lazy_mmu_mode_disable(); 139 *mask |= PGTBL_PTE_MODIFIED; 140 return 0; 141 } 142 143 static int vmap_try_huge_pmd(pmd_t *pmd, unsigned long addr, unsigned long end, 144 phys_addr_t phys_addr, pgprot_t prot, 145 unsigned int max_page_shift) 146 { 147 if (max_page_shift < PMD_SHIFT) 148 return 0; 149 150 if (!arch_vmap_pmd_supported(prot)) 151 return 0; 152 153 if ((end - addr) != PMD_SIZE) 154 return 0; 155 156 if (!IS_ALIGNED(addr, PMD_SIZE)) 157 return 0; 158 159 if (!IS_ALIGNED(phys_addr, PMD_SIZE)) 160 return 0; 161 162 if (!pmd_present(*pmd)) 163 return pmd_set_huge(pmd, phys_addr, prot); 164 165 /* 166 * Acquire the mmap read lock to exclude ptdump, which walks 167 * kernel page tables it does not own under the mmap write lock. 168 * 169 * Concurrent read lock holders are safe: each exclusively owns 170 * the range it operates on and cannot reach this page table. 171 */ 172 scoped_cond_guard(mmap_read_lock_try, return 0, &init_mm) { 173 if (!pmd_free_pte_page(pmd, addr)) 174 return 0; 175 return pmd_set_huge(pmd, phys_addr, prot); 176 } 177 } 178 179 static int vmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end, 180 phys_addr_t phys_addr, pgprot_t prot, 181 unsigned int max_page_shift, pgtbl_mod_mask *mask) 182 { 183 pmd_t *pmd; 184 unsigned long next; 185 int err = 0; 186 187 pmd = pmd_alloc_track(&init_mm, pud, addr, mask); 188 if (!pmd) 189 return -ENOMEM; 190 do { 191 next = pmd_addr_end(addr, end); 192 193 if (vmap_try_huge_pmd(pmd, addr, next, phys_addr, prot, 194 max_page_shift)) { 195 *mask |= PGTBL_PMD_MODIFIED; 196 continue; 197 } 198 199 err = vmap_pte_range(pmd, addr, next, phys_addr, prot, max_page_shift, mask); 200 if (err) 201 break; 202 } while (pmd++, phys_addr += (next - addr), addr = next, addr != end); 203 return err; 204 } 205 206 static int vmap_try_huge_pud(pud_t *pud, unsigned long addr, unsigned long end, 207 phys_addr_t phys_addr, pgprot_t prot, 208 unsigned int max_page_shift) 209 { 210 if (max_page_shift < PUD_SHIFT) 211 return 0; 212 213 if (!arch_vmap_pud_supported(prot)) 214 return 0; 215 216 if ((end - addr) != PUD_SIZE) 217 return 0; 218 219 if (!IS_ALIGNED(addr, PUD_SIZE)) 220 return 0; 221 222 if (!IS_ALIGNED(phys_addr, PUD_SIZE)) 223 return 0; 224 225 if (!pud_present(*pud)) 226 return pud_set_huge(pud, phys_addr, prot); 227 228 /* See comment in vmap_try_huge_pmd(). */ 229 scoped_cond_guard(mmap_read_lock_try, return 0, &init_mm) { 230 if (!pud_free_pmd_page(pud, addr)) 231 return 0; 232 return pud_set_huge(pud, phys_addr, prot); 233 } 234 } 235 236 static int vmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end, 237 phys_addr_t phys_addr, pgprot_t prot, 238 unsigned int max_page_shift, pgtbl_mod_mask *mask) 239 { 240 pud_t *pud; 241 unsigned long next; 242 int err = 0; 243 244 pud = pud_alloc_track(&init_mm, p4d, addr, mask); 245 if (!pud) 246 return -ENOMEM; 247 do { 248 next = pud_addr_end(addr, end); 249 250 if (vmap_try_huge_pud(pud, addr, next, phys_addr, prot, 251 max_page_shift)) { 252 *mask |= PGTBL_PUD_MODIFIED; 253 continue; 254 } 255 256 err = vmap_pmd_range(pud, addr, next, phys_addr, prot, max_page_shift, mask); 257 if (err) 258 break; 259 } while (pud++, phys_addr += (next - addr), addr = next, addr != end); 260 return err; 261 } 262 263 static int vmap_try_huge_p4d(p4d_t *p4d, unsigned long addr, unsigned long end, 264 phys_addr_t phys_addr, pgprot_t prot, 265 unsigned int max_page_shift) 266 { 267 if (max_page_shift < P4D_SHIFT) 268 return 0; 269 270 if (!arch_vmap_p4d_supported(prot)) 271 return 0; 272 273 if ((end - addr) != P4D_SIZE) 274 return 0; 275 276 if (!IS_ALIGNED(addr, P4D_SIZE)) 277 return 0; 278 279 if (!IS_ALIGNED(phys_addr, P4D_SIZE)) 280 return 0; 281 282 if (!p4d_present(*p4d)) 283 return p4d_set_huge(p4d, phys_addr, prot); 284 285 /* See comment in vmap_try_huge_pmd(). */ 286 scoped_cond_guard(mmap_read_lock_try, return 0, &init_mm) { 287 if (!p4d_free_pud_page(p4d, addr)) 288 return 0; 289 return p4d_set_huge(p4d, phys_addr, prot); 290 } 291 } 292 293 static int vmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end, 294 phys_addr_t phys_addr, pgprot_t prot, 295 unsigned int max_page_shift, pgtbl_mod_mask *mask) 296 { 297 p4d_t *p4d; 298 unsigned long next; 299 int err = 0; 300 301 p4d = p4d_alloc_track(&init_mm, pgd, addr, mask); 302 if (!p4d) 303 return -ENOMEM; 304 do { 305 next = p4d_addr_end(addr, end); 306 307 if (vmap_try_huge_p4d(p4d, addr, next, phys_addr, prot, 308 max_page_shift)) { 309 *mask |= PGTBL_P4D_MODIFIED; 310 continue; 311 } 312 313 err = vmap_pud_range(p4d, addr, next, phys_addr, prot, max_page_shift, mask); 314 if (err) 315 break; 316 } while (p4d++, phys_addr += (next - addr), addr = next, addr != end); 317 return err; 318 } 319 320 static int vmap_range_noflush(unsigned long addr, unsigned long end, 321 phys_addr_t phys_addr, pgprot_t prot, 322 unsigned int max_page_shift) 323 { 324 pgd_t *pgd; 325 unsigned long start; 326 unsigned long next; 327 int err; 328 pgtbl_mod_mask mask = 0; 329 330 /* 331 * Might allocate pagetables (for most archs a more precise annotation 332 * would be might_alloc(GFP_PGTABLE_KERNEL)). Also might shootdown TLB 333 * (requires IRQs enabled on x86). 334 */ 335 might_sleep(); 336 BUG_ON(addr >= end); 337 338 start = addr; 339 pgd = pgd_offset_k(addr); 340 do { 341 next = pgd_addr_end(addr, end); 342 err = vmap_p4d_range(pgd, addr, next, phys_addr, prot, 343 max_page_shift, &mask); 344 if (err) 345 break; 346 } while (pgd++, phys_addr += (next - addr), addr = next, addr != end); 347 348 if (mask & ARCH_PAGE_TABLE_SYNC_MASK) 349 arch_sync_kernel_mappings(start, end); 350 351 return err; 352 } 353 354 int vmap_page_range(unsigned long addr, unsigned long end, 355 phys_addr_t phys_addr, pgprot_t prot) 356 { 357 int err; 358 359 err = vmap_range_noflush(addr, end, phys_addr, pgprot_nx(prot), 360 ioremap_max_page_shift); 361 flush_cache_vmap(addr, end); 362 if (!err) 363 err = kmsan_ioremap_page_range(addr, end, phys_addr, prot, 364 ioremap_max_page_shift); 365 return err; 366 } 367 368 int ioremap_page_range(unsigned long addr, unsigned long end, 369 phys_addr_t phys_addr, pgprot_t prot) 370 { 371 struct vm_struct *area; 372 373 area = find_vm_area((void *)addr); 374 if (!area || !(area->flags & VM_IOREMAP)) { 375 WARN_ONCE(1, "vm_area at addr %lx is not marked as VM_IOREMAP\n", addr); 376 return -EINVAL; 377 } 378 if (addr != (unsigned long)area->addr || 379 (void *)end != area->addr + get_vm_area_size(area)) { 380 WARN_ONCE(1, "ioremap request [%lx,%lx) doesn't match vm_area [%lx, %lx)\n", 381 addr, end, (long)area->addr, 382 (long)area->addr + get_vm_area_size(area)); 383 return -ERANGE; 384 } 385 return vmap_page_range(addr, end, phys_addr, prot); 386 } 387 388 static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, 389 pgtbl_mod_mask *mask) 390 { 391 pte_t *pte; 392 pte_t ptent; 393 unsigned long size = PAGE_SIZE; 394 395 pte = pte_offset_kernel(pmd, addr); 396 lazy_mmu_mode_enable(); 397 398 do { 399 #ifdef CONFIG_HUGETLB_PAGE 400 size = arch_vmap_pte_range_unmap_size(addr, pte); 401 if (size != PAGE_SIZE) { 402 if (WARN_ON(!IS_ALIGNED(addr, size))) { 403 addr = ALIGN_DOWN(addr, size); 404 pte = PTR_ALIGN_DOWN(pte, sizeof(*pte) * (size >> PAGE_SHIFT)); 405 } 406 ptent = huge_ptep_get_and_clear(&init_mm, addr, pte, size); 407 if (WARN_ON(end - addr < size)) 408 size = end - addr; 409 } else 410 #endif 411 ptent = ptep_get_and_clear(&init_mm, addr, pte); 412 WARN_ON(!pte_none(ptent) && !pte_present(ptent)); 413 } while (pte += (size >> PAGE_SHIFT), addr += size, addr != end); 414 415 lazy_mmu_mode_disable(); 416 *mask |= PGTBL_PTE_MODIFIED; 417 } 418 419 static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end, 420 pgtbl_mod_mask *mask) 421 { 422 pmd_t *pmd; 423 unsigned long next; 424 int cleared; 425 426 pmd = pmd_offset(pud, addr); 427 do { 428 next = pmd_addr_end(addr, end); 429 430 cleared = pmd_clear_huge(pmd); 431 if (cleared || pmd_bad(*pmd)) 432 *mask |= PGTBL_PMD_MODIFIED; 433 434 if (cleared) { 435 WARN_ON(next - addr < PMD_SIZE); 436 continue; 437 } 438 if (pmd_none_or_clear_bad(pmd)) 439 continue; 440 vunmap_pte_range(pmd, addr, next, mask); 441 442 cond_resched(); 443 } while (pmd++, addr = next, addr != end); 444 } 445 446 static void vunmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end, 447 pgtbl_mod_mask *mask) 448 { 449 pud_t *pud; 450 unsigned long next; 451 int cleared; 452 453 pud = pud_offset(p4d, addr); 454 do { 455 next = pud_addr_end(addr, end); 456 457 cleared = pud_clear_huge(pud); 458 if (cleared || pud_bad(*pud)) 459 *mask |= PGTBL_PUD_MODIFIED; 460 461 if (cleared) { 462 WARN_ON(next - addr < PUD_SIZE); 463 continue; 464 } 465 if (pud_none_or_clear_bad(pud)) 466 continue; 467 vunmap_pmd_range(pud, addr, next, mask); 468 } while (pud++, addr = next, addr != end); 469 } 470 471 static void vunmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end, 472 pgtbl_mod_mask *mask) 473 { 474 p4d_t *p4d; 475 unsigned long next; 476 477 p4d = p4d_offset(pgd, addr); 478 do { 479 next = p4d_addr_end(addr, end); 480 481 p4d_clear_huge(p4d); 482 if (p4d_bad(*p4d)) 483 *mask |= PGTBL_P4D_MODIFIED; 484 485 if (p4d_none_or_clear_bad(p4d)) 486 continue; 487 vunmap_pud_range(p4d, addr, next, mask); 488 } while (p4d++, addr = next, addr != end); 489 } 490 491 /* 492 * vunmap_range_noflush is similar to vunmap_range, but does not 493 * flush caches or TLBs. 494 * 495 * The caller is responsible for calling flush_cache_vmap() before calling 496 * this function, and flush_tlb_kernel_range after it has returned 497 * successfully (and before the addresses are expected to cause a page fault 498 * or be re-mapped for something else, if TLB flushes are being delayed or 499 * coalesced). 500 * 501 * This is an internal function only. Do not use outside mm/. 502 */ 503 void __vunmap_range_noflush(unsigned long start, unsigned long end) 504 { 505 unsigned long next; 506 pgd_t *pgd; 507 unsigned long addr = start; 508 pgtbl_mod_mask mask = 0; 509 510 BUG_ON(addr >= end); 511 pgd = pgd_offset_k(addr); 512 do { 513 next = pgd_addr_end(addr, end); 514 if (pgd_bad(*pgd)) 515 mask |= PGTBL_PGD_MODIFIED; 516 if (pgd_none_or_clear_bad(pgd)) 517 continue; 518 vunmap_p4d_range(pgd, addr, next, &mask); 519 } while (pgd++, addr = next, addr != end); 520 521 if (mask & ARCH_PAGE_TABLE_SYNC_MASK) 522 arch_sync_kernel_mappings(start, end); 523 } 524 525 void vunmap_range_noflush(unsigned long start, unsigned long end) 526 { 527 kmsan_vunmap_range_noflush(start, end); 528 __vunmap_range_noflush(start, end); 529 } 530 531 /** 532 * vunmap_range - unmap kernel virtual addresses 533 * @addr: start of the VM area to unmap 534 * @end: end of the VM area to unmap (non-inclusive) 535 * 536 * Clears any present PTEs in the virtual address range, flushes TLBs and 537 * caches. Any subsequent access to the address before it has been re-mapped 538 * is a kernel bug. 539 */ 540 void vunmap_range(unsigned long addr, unsigned long end) 541 { 542 flush_cache_vunmap(addr, end); 543 vunmap_range_noflush(addr, end); 544 flush_tlb_kernel_range(addr, end); 545 } 546 547 static int vmap_pages_pte_range(pmd_t *pmd, unsigned long addr, 548 unsigned long end, pgprot_t prot, struct page **pages, int *nr, 549 pgtbl_mod_mask *mask) 550 { 551 int err = 0; 552 pte_t *pte; 553 554 /* 555 * nr is a running index into the array which helps higher level 556 * callers keep track of where we're up to. 557 */ 558 559 pte = pte_alloc_kernel_track(pmd, addr, mask); 560 if (!pte) 561 return -ENOMEM; 562 563 lazy_mmu_mode_enable(); 564 565 do { 566 struct page *page = pages[*nr]; 567 568 if (WARN_ON(!pte_none(ptep_get(pte)))) { 569 err = -EBUSY; 570 break; 571 } 572 if (WARN_ON(!page)) { 573 err = -ENOMEM; 574 break; 575 } 576 if (WARN_ON(!pfn_valid(page_to_pfn(page)))) { 577 err = -EINVAL; 578 break; 579 } 580 581 set_pte_at(&init_mm, addr, pte, mk_pte(page, prot)); 582 (*nr)++; 583 } while (pte++, addr += PAGE_SIZE, addr != end); 584 585 lazy_mmu_mode_disable(); 586 *mask |= PGTBL_PTE_MODIFIED; 587 588 return err; 589 } 590 591 static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr, 592 unsigned long end, pgprot_t prot, struct page **pages, int *nr, 593 pgtbl_mod_mask *mask) 594 { 595 pmd_t *pmd; 596 unsigned long next; 597 598 pmd = pmd_alloc_track(&init_mm, pud, addr, mask); 599 if (!pmd) 600 return -ENOMEM; 601 do { 602 next = pmd_addr_end(addr, end); 603 if (vmap_pages_pte_range(pmd, addr, next, prot, pages, nr, mask)) 604 return -ENOMEM; 605 } while (pmd++, addr = next, addr != end); 606 return 0; 607 } 608 609 static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr, 610 unsigned long end, pgprot_t prot, struct page **pages, int *nr, 611 pgtbl_mod_mask *mask) 612 { 613 pud_t *pud; 614 unsigned long next; 615 616 pud = pud_alloc_track(&init_mm, p4d, addr, mask); 617 if (!pud) 618 return -ENOMEM; 619 do { 620 next = pud_addr_end(addr, end); 621 if (vmap_pages_pmd_range(pud, addr, next, prot, pages, nr, mask)) 622 return -ENOMEM; 623 } while (pud++, addr = next, addr != end); 624 return 0; 625 } 626 627 static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr, 628 unsigned long end, pgprot_t prot, struct page **pages, int *nr, 629 pgtbl_mod_mask *mask) 630 { 631 p4d_t *p4d; 632 unsigned long next; 633 634 p4d = p4d_alloc_track(&init_mm, pgd, addr, mask); 635 if (!p4d) 636 return -ENOMEM; 637 do { 638 next = p4d_addr_end(addr, end); 639 if (vmap_pages_pud_range(p4d, addr, next, prot, pages, nr, mask)) 640 return -ENOMEM; 641 } while (p4d++, addr = next, addr != end); 642 return 0; 643 } 644 645 static int vmap_small_pages_range_noflush(unsigned long addr, unsigned long end, 646 pgprot_t prot, struct page **pages) 647 { 648 unsigned long start = addr; 649 pgd_t *pgd; 650 unsigned long next; 651 int err = 0; 652 int nr = 0; 653 pgtbl_mod_mask mask = 0; 654 655 BUG_ON(addr >= end); 656 pgd = pgd_offset_k(addr); 657 do { 658 next = pgd_addr_end(addr, end); 659 if (pgd_bad(*pgd)) 660 mask |= PGTBL_PGD_MODIFIED; 661 err = vmap_pages_p4d_range(pgd, addr, next, prot, pages, &nr, &mask); 662 if (err) 663 break; 664 } while (pgd++, addr = next, addr != end); 665 666 if (mask & ARCH_PAGE_TABLE_SYNC_MASK) 667 arch_sync_kernel_mappings(start, end); 668 669 return err; 670 } 671 672 /* 673 * vmap_pages_range_noflush is similar to vmap_pages_range, but does not 674 * flush caches. 675 * 676 * The caller is responsible for calling flush_cache_vmap() after this 677 * function returns successfully and before the addresses are accessed. 678 * 679 * This is an internal function only. Do not use outside mm/. 680 */ 681 int __vmap_pages_range_noflush(unsigned long addr, unsigned long end, 682 pgprot_t prot, struct page **pages, unsigned int page_shift) 683 { 684 unsigned int i, nr = (end - addr) >> PAGE_SHIFT; 685 686 WARN_ON(page_shift < PAGE_SHIFT); 687 688 if (!IS_ENABLED(CONFIG_HAVE_ARCH_HUGE_VMALLOC) || 689 page_shift == PAGE_SHIFT) 690 return vmap_small_pages_range_noflush(addr, end, prot, pages); 691 692 for (i = 0; i < nr; i += 1U << (page_shift - PAGE_SHIFT)) { 693 int err; 694 695 err = vmap_range_noflush(addr, addr + (1UL << page_shift), 696 page_to_phys(pages[i]), prot, 697 page_shift); 698 if (err) 699 return err; 700 701 addr += 1UL << page_shift; 702 } 703 704 return 0; 705 } 706 707 int vmap_pages_range_noflush(unsigned long addr, unsigned long end, 708 pgprot_t prot, struct page **pages, unsigned int page_shift, 709 gfp_t gfp_mask) 710 { 711 int ret = kmsan_vmap_pages_range_noflush(addr, end, prot, pages, 712 page_shift, gfp_mask); 713 714 if (ret) 715 return ret; 716 return __vmap_pages_range_noflush(addr, end, prot, pages, page_shift); 717 } 718 719 static int __vmap_pages_range(unsigned long addr, unsigned long end, 720 pgprot_t prot, struct page **pages, unsigned int page_shift, 721 gfp_t gfp_mask) 722 { 723 int err; 724 725 err = vmap_pages_range_noflush(addr, end, prot, pages, page_shift, gfp_mask); 726 flush_cache_vmap(addr, end); 727 return err; 728 } 729 730 /** 731 * vmap_pages_range - map pages to a kernel virtual address 732 * @addr: start of the VM area to map 733 * @end: end of the VM area to map (non-inclusive) 734 * @prot: page protection flags to use 735 * @pages: pages to map (always PAGE_SIZE pages) 736 * @page_shift: maximum shift that the pages may be mapped with, @pages must 737 * be aligned and contiguous up to at least this shift. 738 * 739 * RETURNS: 740 * 0 on success, -errno on failure. 741 */ 742 int vmap_pages_range(unsigned long addr, unsigned long end, 743 pgprot_t prot, struct page **pages, unsigned int page_shift) 744 { 745 return __vmap_pages_range(addr, end, prot, pages, page_shift, GFP_KERNEL); 746 } 747 748 static int check_sparse_vm_area(struct vm_struct *area, unsigned long start, 749 unsigned long end) 750 { 751 might_sleep(); 752 if (WARN_ON_ONCE(area->flags & VM_FLUSH_RESET_PERMS)) 753 return -EINVAL; 754 if (WARN_ON_ONCE(area->flags & VM_NO_GUARD)) 755 return -EINVAL; 756 if (WARN_ON_ONCE(!(area->flags & VM_SPARSE))) 757 return -EINVAL; 758 if ((end - start) >> PAGE_SHIFT > totalram_pages()) 759 return -E2BIG; 760 if (start < (unsigned long)area->addr || 761 (void *)end > area->addr + get_vm_area_size(area)) 762 return -ERANGE; 763 return 0; 764 } 765 766 /** 767 * vm_area_map_pages - map pages inside given sparse vm_area 768 * @area: vm_area 769 * @start: start address inside vm_area 770 * @end: end address inside vm_area 771 * @pages: pages to map (always PAGE_SIZE pages) 772 */ 773 int vm_area_map_pages(struct vm_struct *area, unsigned long start, 774 unsigned long end, struct page **pages) 775 { 776 int err; 777 778 err = check_sparse_vm_area(area, start, end); 779 if (err) 780 return err; 781 782 return vmap_pages_range(start, end, PAGE_KERNEL, pages, PAGE_SHIFT); 783 } 784 785 /** 786 * vm_area_unmap_pages - unmap pages inside given sparse vm_area 787 * @area: vm_area 788 * @start: start address inside vm_area 789 * @end: end address inside vm_area 790 */ 791 void vm_area_unmap_pages(struct vm_struct *area, unsigned long start, 792 unsigned long end) 793 { 794 if (check_sparse_vm_area(area, start, end)) 795 return; 796 797 vunmap_range(start, end); 798 } 799 800 int is_vmalloc_or_module_addr(const void *x) 801 { 802 /* 803 * ARM, x86-64 and sparc64 put modules in a special place, 804 * and fall back on vmalloc() if that fails. Others 805 * just put it in the vmalloc space. 806 */ 807 #if defined(CONFIG_EXECMEM) && defined(MODULES_VADDR) 808 unsigned long addr = (unsigned long)kasan_reset_tag(x); 809 if (addr >= MODULES_VADDR && addr < MODULES_END) 810 return 1; 811 #endif 812 return is_vmalloc_addr(x); 813 } 814 EXPORT_SYMBOL_GPL(is_vmalloc_or_module_addr); 815 816 /* 817 * Walk a vmap address to the struct page it maps. Huge vmap mappings will 818 * return the tail page that corresponds to the base page address, which 819 * matches small vmap mappings. 820 */ 821 struct page *vmalloc_to_page(const void *vmalloc_addr) 822 { 823 unsigned long addr = (unsigned long) vmalloc_addr; 824 struct page *page = NULL; 825 pgd_t *pgd = pgd_offset_k(addr); 826 p4d_t *p4d; 827 pud_t *pud; 828 pmd_t *pmd; 829 pte_t *ptep, pte; 830 831 /* 832 * XXX we might need to change this if we add VIRTUAL_BUG_ON for 833 * architectures that do not vmalloc module space 834 */ 835 VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr)); 836 837 if (pgd_none(*pgd)) 838 return NULL; 839 if (WARN_ON_ONCE(pgd_leaf(*pgd))) 840 return NULL; /* XXX: no allowance for huge pgd */ 841 if (WARN_ON_ONCE(pgd_bad(*pgd))) 842 return NULL; 843 844 p4d = p4d_offset(pgd, addr); 845 if (p4d_none(*p4d)) 846 return NULL; 847 if (p4d_leaf(*p4d)) 848 return p4d_page(*p4d) + ((addr & ~P4D_MASK) >> PAGE_SHIFT); 849 if (WARN_ON_ONCE(p4d_bad(*p4d))) 850 return NULL; 851 852 pud = pud_offset(p4d, addr); 853 if (pud_none(*pud)) 854 return NULL; 855 if (pud_leaf(*pud)) 856 return pud_page(*pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT); 857 if (WARN_ON_ONCE(pud_bad(*pud))) 858 return NULL; 859 860 pmd = pmd_offset(pud, addr); 861 if (pmd_none(*pmd)) 862 return NULL; 863 if (pmd_leaf(*pmd)) 864 return pmd_page(*pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT); 865 if (WARN_ON_ONCE(pmd_bad(*pmd))) 866 return NULL; 867 868 ptep = pte_offset_kernel(pmd, addr); 869 pte = ptep_get(ptep); 870 if (pte_present(pte)) 871 page = pte_page(pte); 872 873 return page; 874 } 875 EXPORT_SYMBOL(vmalloc_to_page); 876 877 /* 878 * Map a vmalloc()-space virtual address to the physical page frame number. 879 */ 880 unsigned long vmalloc_to_pfn(const void *vmalloc_addr) 881 { 882 return page_to_pfn(vmalloc_to_page(vmalloc_addr)); 883 } 884 EXPORT_SYMBOL(vmalloc_to_pfn); 885 886 887 /*** Global kva allocator ***/ 888 889 #define DEBUG_AUGMENT_PROPAGATE_CHECK 0 890 #define DEBUG_AUGMENT_LOWEST_MATCH_CHECK 0 891 892 893 static DEFINE_SPINLOCK(free_vmap_area_lock); 894 static bool vmap_initialized __read_mostly; 895 896 /* 897 * This kmem_cache is used for vmap_area objects. Instead of 898 * allocating from slab we reuse an object from this cache to 899 * make things faster. Especially in "no edge" splitting of 900 * free block. 901 */ 902 static struct kmem_cache *vmap_area_cachep; 903 904 /* 905 * This linked list is used in pair with free_vmap_area_root. 906 * It gives O(1) access to prev/next to perform fast coalescing. 907 */ 908 static LIST_HEAD(free_vmap_area_list); 909 910 /* 911 * This augment red-black tree represents the free vmap space. 912 * All vmap_area objects in this tree are sorted by va->va_start 913 * address. It is used for allocation and merging when a vmap 914 * object is released. 915 * 916 * Each vmap_area node contains a maximum available free block 917 * of its sub-tree, right or left. Therefore it is possible to 918 * find a lowest match of free area. 919 */ 920 static struct rb_root free_vmap_area_root = RB_ROOT; 921 922 /* 923 * Preload a CPU with one object for "no edge" split case. The 924 * aim is to get rid of allocations from the atomic context, thus 925 * to use more permissive allocation masks. 926 */ 927 static DEFINE_PER_CPU(struct vmap_area *, ne_fit_preload_node); 928 929 /* 930 * This structure defines a single, solid model where a list and 931 * rb-tree are part of one entity protected by the lock. Nodes are 932 * sorted in ascending order, thus for O(1) access to left/right 933 * neighbors a list is used as well as for sequential traversal. 934 */ 935 struct rb_list { 936 struct rb_root root; 937 struct list_head head; 938 spinlock_t lock; 939 }; 940 941 /* 942 * A fast size storage contains VAs up to 1M size. A pool consists 943 * of linked between each other ready to go VAs of certain sizes. 944 * An index in the pool-array corresponds to number of pages + 1. 945 */ 946 #define MAX_VA_SIZE_PAGES 256 947 948 struct vmap_pool { 949 struct list_head head; 950 unsigned long len; 951 }; 952 953 /* 954 * An effective vmap-node logic. Users make use of nodes instead 955 * of a global heap. It allows to balance an access and mitigate 956 * contention. 957 */ 958 static struct vmap_node { 959 /* Simple size segregated storage. */ 960 struct vmap_pool pool[MAX_VA_SIZE_PAGES]; 961 spinlock_t pool_lock; 962 bool skip_populate; 963 964 /* Bookkeeping data of this node. */ 965 struct rb_list busy; 966 struct rb_list lazy; 967 968 /* 969 * Ready-to-free areas. 970 */ 971 struct list_head purge_list; 972 struct work_struct purge_work; 973 unsigned long nr_purged; 974 } single; 975 976 /* 977 * Initial setup consists of one single node, i.e. a balancing 978 * is fully disabled. Later on, after vmap is initialized these 979 * parameters are updated based on a system capacity. 980 */ 981 static struct vmap_node *vmap_nodes = &single; 982 static __read_mostly unsigned int nr_vmap_nodes = 1; 983 static __read_mostly unsigned int vmap_zone_size = 1; 984 985 /* A simple iterator over all vmap-nodes. */ 986 #define for_each_vmap_node(vn) \ 987 for ((vn) = &vmap_nodes[0]; \ 988 (vn) < &vmap_nodes[nr_vmap_nodes]; (vn)++) 989 990 static inline unsigned int 991 addr_to_node_id(unsigned long addr) 992 { 993 return (addr / vmap_zone_size) % nr_vmap_nodes; 994 } 995 996 static inline struct vmap_node * 997 addr_to_node(unsigned long addr) 998 { 999 return &vmap_nodes[addr_to_node_id(addr)]; 1000 } 1001 1002 static inline struct vmap_node * 1003 id_to_node(unsigned int id) 1004 { 1005 return &vmap_nodes[id % nr_vmap_nodes]; 1006 } 1007 1008 static inline unsigned int 1009 node_to_id(struct vmap_node *node) 1010 { 1011 /* Pointer arithmetic. */ 1012 unsigned int id = node - vmap_nodes; 1013 1014 if (likely(id < nr_vmap_nodes)) 1015 return id; 1016 1017 WARN_ONCE(1, "An address 0x%p is out-of-bounds.\n", node); 1018 return 0; 1019 } 1020 1021 /* 1022 * We use the value 0 to represent "no node", that is why 1023 * an encoded value will be the node-id incremented by 1. 1024 * It is always greater then 0. A valid node_id which can 1025 * be encoded is [0:nr_vmap_nodes - 1]. If a passed node_id 1026 * is not valid 0 is returned. 1027 */ 1028 static unsigned int 1029 encode_vn_id(unsigned int node_id) 1030 { 1031 /* Can store U8_MAX [0:254] nodes. */ 1032 if (node_id < nr_vmap_nodes) 1033 return (node_id + 1) << BITS_PER_BYTE; 1034 1035 /* Warn and no node encoded. */ 1036 WARN_ONCE(1, "Encode wrong node id (%u)\n", node_id); 1037 return 0; 1038 } 1039 1040 /* 1041 * Returns an encoded node-id, the valid range is within 1042 * [0:nr_vmap_nodes-1] values. Otherwise nr_vmap_nodes is 1043 * returned if extracted data is wrong. 1044 */ 1045 static unsigned int 1046 decode_vn_id(unsigned int val) 1047 { 1048 unsigned int node_id = (val >> BITS_PER_BYTE) - 1; 1049 1050 /* Can store U8_MAX [0:254] nodes. */ 1051 if (node_id < nr_vmap_nodes) 1052 return node_id; 1053 1054 /* If it was _not_ zero, warn. */ 1055 WARN_ONCE(node_id != UINT_MAX, 1056 "Decode wrong node id (%d)\n", node_id); 1057 1058 return nr_vmap_nodes; 1059 } 1060 1061 static bool 1062 is_vn_id_valid(unsigned int node_id) 1063 { 1064 if (node_id < nr_vmap_nodes) 1065 return true; 1066 1067 return false; 1068 } 1069 1070 static __always_inline unsigned long 1071 va_size(struct vmap_area *va) 1072 { 1073 return (va->va_end - va->va_start); 1074 } 1075 1076 static __always_inline unsigned long 1077 get_subtree_max_size(struct rb_node *node) 1078 { 1079 struct vmap_area *va; 1080 1081 va = rb_entry_safe(node, struct vmap_area, rb_node); 1082 return va ? va->subtree_max_size : 0; 1083 } 1084 1085 RB_DECLARE_CALLBACKS_MAX(static, free_vmap_area_rb_augment_cb, 1086 struct vmap_area, rb_node, unsigned long, subtree_max_size, va_size) 1087 1088 static void reclaim_and_purge_vmap_areas(void); 1089 static BLOCKING_NOTIFIER_HEAD(vmap_notify_list); 1090 static void drain_vmap_area_work(struct work_struct *work); 1091 static DECLARE_WORK(drain_vmap_work, drain_vmap_area_work); 1092 1093 static __cacheline_aligned_in_smp atomic_long_t vmap_lazy_nr; 1094 1095 static struct vmap_area *__find_vmap_area(unsigned long addr, struct rb_root *root) 1096 { 1097 struct rb_node *n = root->rb_node; 1098 1099 addr = (unsigned long)kasan_reset_tag((void *)addr); 1100 1101 while (n) { 1102 struct vmap_area *va; 1103 1104 va = rb_entry(n, struct vmap_area, rb_node); 1105 if (addr < va->va_start) 1106 n = n->rb_left; 1107 else if (addr >= va->va_end) 1108 n = n->rb_right; 1109 else 1110 return va; 1111 } 1112 1113 return NULL; 1114 } 1115 1116 /* Look up the first VA which satisfies addr < va_end, NULL if none. */ 1117 static struct vmap_area * 1118 __find_vmap_area_exceed_addr(unsigned long addr, struct rb_root *root) 1119 { 1120 struct vmap_area *va = NULL; 1121 struct rb_node *n = root->rb_node; 1122 1123 addr = (unsigned long)kasan_reset_tag((void *)addr); 1124 1125 while (n) { 1126 struct vmap_area *tmp; 1127 1128 tmp = rb_entry(n, struct vmap_area, rb_node); 1129 if (tmp->va_end > addr) { 1130 va = tmp; 1131 if (tmp->va_start <= addr) 1132 break; 1133 1134 n = n->rb_left; 1135 } else 1136 n = n->rb_right; 1137 } 1138 1139 return va; 1140 } 1141 1142 /* 1143 * Returns a node where a first VA, that satisfies addr < va_end, resides. 1144 * If success, a node is locked. A user is responsible to unlock it when a 1145 * VA is no longer needed to be accessed. 1146 * 1147 * Returns NULL if nothing found. 1148 */ 1149 static struct vmap_node * 1150 find_vmap_area_exceed_addr_lock(unsigned long addr, struct vmap_area **va) 1151 { 1152 unsigned long va_start_lowest; 1153 struct vmap_node *vn; 1154 1155 repeat: 1156 va_start_lowest = 0; 1157 1158 for_each_vmap_node(vn) { 1159 spin_lock(&vn->busy.lock); 1160 *va = __find_vmap_area_exceed_addr(addr, &vn->busy.root); 1161 1162 if (*va) 1163 if (!va_start_lowest || (*va)->va_start < va_start_lowest) 1164 va_start_lowest = (*va)->va_start; 1165 spin_unlock(&vn->busy.lock); 1166 } 1167 1168 /* 1169 * Check if found VA exists, it might have gone away. In this case we 1170 * repeat the search because a VA has been removed concurrently and we 1171 * need to proceed to the next one, which is a rare case. 1172 */ 1173 if (va_start_lowest) { 1174 vn = addr_to_node(va_start_lowest); 1175 1176 spin_lock(&vn->busy.lock); 1177 *va = __find_vmap_area(va_start_lowest, &vn->busy.root); 1178 1179 if (*va) 1180 return vn; 1181 1182 spin_unlock(&vn->busy.lock); 1183 goto repeat; 1184 } 1185 1186 return NULL; 1187 } 1188 1189 /* 1190 * This function returns back addresses of parent node 1191 * and its left or right link for further processing. 1192 * 1193 * Otherwise NULL is returned. In that case all further 1194 * steps regarding inserting of conflicting overlap range 1195 * have to be declined and actually considered as a bug. 1196 */ 1197 static __always_inline struct rb_node ** 1198 find_va_links(struct vmap_area *va, 1199 struct rb_root *root, struct rb_node *from, 1200 struct rb_node **parent) 1201 { 1202 struct vmap_area *tmp_va; 1203 struct rb_node **link; 1204 1205 if (root) { 1206 link = &root->rb_node; 1207 if (unlikely(!*link)) { 1208 *parent = NULL; 1209 return link; 1210 } 1211 } else { 1212 link = &from; 1213 } 1214 1215 /* 1216 * Go to the bottom of the tree. When we hit the last point 1217 * we end up with parent rb_node and correct direction, i name 1218 * it link, where the new va->rb_node will be attached to. 1219 */ 1220 do { 1221 tmp_va = rb_entry(*link, struct vmap_area, rb_node); 1222 1223 /* 1224 * During the traversal we also do some sanity check. 1225 * Trigger the BUG() if there are sides(left/right) 1226 * or full overlaps. 1227 */ 1228 if (va->va_end <= tmp_va->va_start) 1229 link = &(*link)->rb_left; 1230 else if (va->va_start >= tmp_va->va_end) 1231 link = &(*link)->rb_right; 1232 else { 1233 WARN(1, "vmalloc bug: 0x%lx-0x%lx overlaps with 0x%lx-0x%lx\n", 1234 va->va_start, va->va_end, tmp_va->va_start, tmp_va->va_end); 1235 1236 return NULL; 1237 } 1238 } while (*link); 1239 1240 *parent = &tmp_va->rb_node; 1241 return link; 1242 } 1243 1244 static __always_inline struct list_head * 1245 get_va_next_sibling(struct rb_node *parent, struct rb_node **link) 1246 { 1247 struct list_head *list; 1248 1249 if (unlikely(!parent)) 1250 /* 1251 * The red-black tree where we try to find VA neighbors 1252 * before merging or inserting is empty, i.e. it means 1253 * there is no free vmap space. Normally it does not 1254 * happen but we handle this case anyway. 1255 */ 1256 return NULL; 1257 1258 list = &rb_entry(parent, struct vmap_area, rb_node)->list; 1259 return (&parent->rb_right == link ? list->next : list); 1260 } 1261 1262 static __always_inline void 1263 __link_va(struct vmap_area *va, struct rb_root *root, 1264 struct rb_node *parent, struct rb_node **link, 1265 struct list_head *head, bool augment) 1266 { 1267 /* 1268 * VA is still not in the list, but we can 1269 * identify its future previous list_head node. 1270 */ 1271 if (likely(parent)) { 1272 head = &rb_entry(parent, struct vmap_area, rb_node)->list; 1273 if (&parent->rb_right != link) 1274 head = head->prev; 1275 } 1276 1277 /* Insert to the rb-tree */ 1278 rb_link_node(&va->rb_node, parent, link); 1279 if (augment) { 1280 /* 1281 * Some explanation here. Just perform simple insertion 1282 * to the tree. We do not set va->subtree_max_size to 1283 * its current size before calling rb_insert_augmented(). 1284 * It is because we populate the tree from the bottom 1285 * to parent levels when the node _is_ in the tree. 1286 * 1287 * Therefore we set subtree_max_size to zero after insertion, 1288 * to let __augment_tree_propagate_from() puts everything to 1289 * the correct order later on. 1290 */ 1291 rb_insert_augmented(&va->rb_node, 1292 root, &free_vmap_area_rb_augment_cb); 1293 va->subtree_max_size = 0; 1294 } else { 1295 rb_insert_color(&va->rb_node, root); 1296 } 1297 1298 /* Address-sort this list */ 1299 list_add(&va->list, head); 1300 } 1301 1302 static __always_inline void 1303 link_va(struct vmap_area *va, struct rb_root *root, 1304 struct rb_node *parent, struct rb_node **link, 1305 struct list_head *head) 1306 { 1307 __link_va(va, root, parent, link, head, false); 1308 } 1309 1310 static __always_inline void 1311 link_va_augment(struct vmap_area *va, struct rb_root *root, 1312 struct rb_node *parent, struct rb_node **link, 1313 struct list_head *head) 1314 { 1315 __link_va(va, root, parent, link, head, true); 1316 } 1317 1318 static __always_inline void 1319 __unlink_va(struct vmap_area *va, struct rb_root *root, bool augment) 1320 { 1321 if (WARN_ON(RB_EMPTY_NODE(&va->rb_node))) 1322 return; 1323 1324 if (augment) 1325 rb_erase_augmented(&va->rb_node, 1326 root, &free_vmap_area_rb_augment_cb); 1327 else 1328 rb_erase(&va->rb_node, root); 1329 1330 list_del_init(&va->list); 1331 RB_CLEAR_NODE(&va->rb_node); 1332 } 1333 1334 static __always_inline void 1335 unlink_va(struct vmap_area *va, struct rb_root *root) 1336 { 1337 __unlink_va(va, root, false); 1338 } 1339 1340 static __always_inline void 1341 unlink_va_augment(struct vmap_area *va, struct rb_root *root) 1342 { 1343 __unlink_va(va, root, true); 1344 } 1345 1346 #if DEBUG_AUGMENT_PROPAGATE_CHECK 1347 /* 1348 * Gets called when remove the node and rotate. 1349 */ 1350 static __always_inline unsigned long 1351 compute_subtree_max_size(struct vmap_area *va) 1352 { 1353 return max3(va_size(va), 1354 get_subtree_max_size(va->rb_node.rb_left), 1355 get_subtree_max_size(va->rb_node.rb_right)); 1356 } 1357 1358 static void 1359 augment_tree_propagate_check(void) 1360 { 1361 struct vmap_area *va; 1362 unsigned long computed_size; 1363 1364 list_for_each_entry(va, &free_vmap_area_list, list) { 1365 computed_size = compute_subtree_max_size(va); 1366 if (computed_size != va->subtree_max_size) 1367 pr_emerg("tree is corrupted: %lu, %lu\n", 1368 va_size(va), va->subtree_max_size); 1369 } 1370 } 1371 #endif 1372 1373 /* 1374 * This function populates subtree_max_size from bottom to upper 1375 * levels starting from VA point. The propagation must be done 1376 * when VA size is modified by changing its va_start/va_end. Or 1377 * in case of newly inserting of VA to the tree. 1378 * 1379 * It means that __augment_tree_propagate_from() must be called: 1380 * - After VA has been inserted to the tree(free path); 1381 * - After VA has been shrunk(allocation path); 1382 * - After VA has been increased(merging path). 1383 * 1384 * Please note that, it does not mean that upper parent nodes 1385 * and their subtree_max_size are recalculated all the time up 1386 * to the root node. 1387 * 1388 * 4--8 1389 * /\ 1390 * / \ 1391 * / \ 1392 * 2--2 8--8 1393 * 1394 * For example if we modify the node 4, shrinking it to 2, then 1395 * no any modification is required. If we shrink the node 2 to 1 1396 * its subtree_max_size is updated only, and set to 1. If we shrink 1397 * the node 8 to 6, then its subtree_max_size is set to 6 and parent 1398 * node becomes 4--6. 1399 */ 1400 static __always_inline void 1401 augment_tree_propagate_from(struct vmap_area *va) 1402 { 1403 /* 1404 * Populate the tree from bottom towards the root until 1405 * the calculated maximum available size of checked node 1406 * is equal to its current one. 1407 */ 1408 free_vmap_area_rb_augment_cb_propagate(&va->rb_node, NULL); 1409 1410 #if DEBUG_AUGMENT_PROPAGATE_CHECK 1411 augment_tree_propagate_check(); 1412 #endif 1413 } 1414 1415 static void 1416 insert_vmap_area(struct vmap_area *va, 1417 struct rb_root *root, struct list_head *head) 1418 { 1419 struct rb_node **link; 1420 struct rb_node *parent; 1421 1422 link = find_va_links(va, root, NULL, &parent); 1423 if (link) 1424 link_va(va, root, parent, link, head); 1425 } 1426 1427 static void 1428 insert_vmap_area_augment(struct vmap_area *va, 1429 struct rb_node *from, struct rb_root *root, 1430 struct list_head *head) 1431 { 1432 struct rb_node **link; 1433 struct rb_node *parent; 1434 1435 if (from) 1436 link = find_va_links(va, NULL, from, &parent); 1437 else 1438 link = find_va_links(va, root, NULL, &parent); 1439 1440 if (link) { 1441 link_va_augment(va, root, parent, link, head); 1442 augment_tree_propagate_from(va); 1443 } 1444 } 1445 1446 /* 1447 * Merge de-allocated chunk of VA memory with previous 1448 * and next free blocks. If coalesce is not done a new 1449 * free area is inserted. If VA has been merged, it is 1450 * freed. 1451 * 1452 * Please note, it can return NULL in case of overlap 1453 * ranges, followed by WARN() report. Despite it is a 1454 * buggy behaviour, a system can be alive and keep 1455 * ongoing. 1456 */ 1457 static __always_inline struct vmap_area * 1458 __merge_or_add_vmap_area(struct vmap_area *va, 1459 struct rb_root *root, struct list_head *head, bool augment) 1460 { 1461 struct vmap_area *sibling; 1462 struct list_head *next; 1463 struct rb_node **link; 1464 struct rb_node *parent; 1465 bool merged = false; 1466 1467 /* 1468 * Find a place in the tree where VA potentially will be 1469 * inserted, unless it is merged with its sibling/siblings. 1470 */ 1471 link = find_va_links(va, root, NULL, &parent); 1472 if (!link) 1473 return NULL; 1474 1475 /* 1476 * Get next node of VA to check if merging can be done. 1477 */ 1478 next = get_va_next_sibling(parent, link); 1479 if (unlikely(next == NULL)) 1480 goto insert; 1481 1482 /* 1483 * start end 1484 * | | 1485 * |<------VA------>|<-----Next----->| 1486 * | | 1487 * start end 1488 */ 1489 if (next != head) { 1490 sibling = list_entry(next, struct vmap_area, list); 1491 if (sibling->va_start == va->va_end) { 1492 sibling->va_start = va->va_start; 1493 1494 /* Free vmap_area object. */ 1495 kmem_cache_free(vmap_area_cachep, va); 1496 1497 /* Point to the new merged area. */ 1498 va = sibling; 1499 merged = true; 1500 } 1501 } 1502 1503 /* 1504 * start end 1505 * | | 1506 * |<-----Prev----->|<------VA------>| 1507 * | | 1508 * start end 1509 */ 1510 if (next->prev != head) { 1511 sibling = list_entry(next->prev, struct vmap_area, list); 1512 if (sibling->va_end == va->va_start) { 1513 /* 1514 * If both neighbors are coalesced, it is important 1515 * to unlink the "next" node first, followed by merging 1516 * with "previous" one. Otherwise the tree might not be 1517 * fully populated if a sibling's augmented value is 1518 * "normalized" because of rotation operations. 1519 */ 1520 if (merged) 1521 __unlink_va(va, root, augment); 1522 1523 sibling->va_end = va->va_end; 1524 1525 /* Free vmap_area object. */ 1526 kmem_cache_free(vmap_area_cachep, va); 1527 1528 /* Point to the new merged area. */ 1529 va = sibling; 1530 merged = true; 1531 } 1532 } 1533 1534 insert: 1535 if (!merged) 1536 __link_va(va, root, parent, link, head, augment); 1537 1538 return va; 1539 } 1540 1541 static __always_inline struct vmap_area * 1542 merge_or_add_vmap_area(struct vmap_area *va, 1543 struct rb_root *root, struct list_head *head) 1544 { 1545 return __merge_or_add_vmap_area(va, root, head, false); 1546 } 1547 1548 static __always_inline struct vmap_area * 1549 merge_or_add_vmap_area_augment(struct vmap_area *va, 1550 struct rb_root *root, struct list_head *head) 1551 { 1552 va = __merge_or_add_vmap_area(va, root, head, true); 1553 if (va) 1554 augment_tree_propagate_from(va); 1555 1556 return va; 1557 } 1558 1559 static __always_inline bool 1560 is_within_this_va(struct vmap_area *va, unsigned long size, 1561 unsigned long align, unsigned long vstart) 1562 { 1563 unsigned long nva_start_addr; 1564 1565 if (va->va_start > vstart) 1566 nva_start_addr = ALIGN(va->va_start, align); 1567 else 1568 nva_start_addr = ALIGN(vstart, align); 1569 1570 /* Can be overflowed due to big size or alignment. */ 1571 if (nva_start_addr + size < nva_start_addr || 1572 nva_start_addr < vstart) 1573 return false; 1574 1575 return (nva_start_addr + size <= va->va_end); 1576 } 1577 1578 /* 1579 * Find the first free block(lowest start address) in the tree, 1580 * that will accomplish the request corresponding to passing 1581 * parameters. Please note, with an alignment bigger than PAGE_SIZE, 1582 * a search length is adjusted to account for worst case alignment 1583 * overhead. 1584 */ 1585 static __always_inline struct vmap_area * 1586 find_vmap_lowest_match(struct rb_root *root, unsigned long size, 1587 unsigned long align, unsigned long vstart, bool adjust_search_size) 1588 { 1589 struct vmap_area *va; 1590 struct rb_node *node; 1591 unsigned long length; 1592 1593 /* Start from the root. */ 1594 node = root->rb_node; 1595 1596 /* Adjust the search size for alignment overhead. */ 1597 length = adjust_search_size ? size + align - 1 : size; 1598 1599 while (node) { 1600 va = rb_entry(node, struct vmap_area, rb_node); 1601 1602 if (get_subtree_max_size(node->rb_left) >= length && 1603 vstart < va->va_start) { 1604 node = node->rb_left; 1605 } else { 1606 if (is_within_this_va(va, size, align, vstart)) 1607 return va; 1608 1609 /* 1610 * Does not make sense to go deeper towards the right 1611 * sub-tree if it does not have a free block that is 1612 * equal or bigger to the requested search length. 1613 */ 1614 if (get_subtree_max_size(node->rb_right) >= length) { 1615 node = node->rb_right; 1616 continue; 1617 } 1618 1619 /* 1620 * OK. We roll back and find the first right sub-tree, 1621 * that will satisfy the search criteria. It can happen 1622 * due to "vstart" restriction or an alignment overhead 1623 * that is bigger then PAGE_SIZE. 1624 */ 1625 while ((node = rb_parent(node))) { 1626 va = rb_entry(node, struct vmap_area, rb_node); 1627 if (is_within_this_va(va, size, align, vstart)) 1628 return va; 1629 1630 if (get_subtree_max_size(node->rb_right) >= length && 1631 vstart <= va->va_start) { 1632 /* 1633 * Shift the vstart forward. Please note, we update it with 1634 * parent's start address adding "1" because we do not want 1635 * to enter same sub-tree after it has already been checked 1636 * and no suitable free block found there. 1637 */ 1638 vstart = va->va_start + 1; 1639 node = node->rb_right; 1640 break; 1641 } 1642 } 1643 } 1644 } 1645 1646 return NULL; 1647 } 1648 1649 #if DEBUG_AUGMENT_LOWEST_MATCH_CHECK 1650 #include <linux/random.h> 1651 1652 static struct vmap_area * 1653 find_vmap_lowest_linear_match(struct list_head *head, unsigned long size, 1654 unsigned long align, unsigned long vstart) 1655 { 1656 struct vmap_area *va; 1657 1658 list_for_each_entry(va, head, list) { 1659 if (!is_within_this_va(va, size, align, vstart)) 1660 continue; 1661 1662 return va; 1663 } 1664 1665 return NULL; 1666 } 1667 1668 static void 1669 find_vmap_lowest_match_check(struct rb_root *root, struct list_head *head, 1670 unsigned long size, unsigned long align) 1671 { 1672 struct vmap_area *va_1, *va_2; 1673 unsigned long vstart; 1674 unsigned int rnd; 1675 1676 get_random_bytes(&rnd, sizeof(rnd)); 1677 vstart = VMALLOC_START + rnd; 1678 1679 va_1 = find_vmap_lowest_match(root, size, align, vstart, false); 1680 va_2 = find_vmap_lowest_linear_match(head, size, align, vstart); 1681 1682 if (va_1 != va_2) 1683 pr_emerg("not lowest: t: 0x%p, l: 0x%p, v: 0x%lx\n", 1684 va_1, va_2, vstart); 1685 } 1686 #endif 1687 1688 enum fit_type { 1689 NOTHING_FIT = 0, 1690 FL_FIT_TYPE = 1, /* full fit */ 1691 LE_FIT_TYPE = 2, /* left edge fit */ 1692 RE_FIT_TYPE = 3, /* right edge fit */ 1693 NE_FIT_TYPE = 4 /* no edge fit */ 1694 }; 1695 1696 static __always_inline enum fit_type 1697 classify_va_fit_type(struct vmap_area *va, 1698 unsigned long nva_start_addr, unsigned long size) 1699 { 1700 enum fit_type type; 1701 1702 /* Check if it is within VA. */ 1703 if (nva_start_addr < va->va_start || 1704 nva_start_addr + size > va->va_end) 1705 return NOTHING_FIT; 1706 1707 /* Now classify. */ 1708 if (va->va_start == nva_start_addr) { 1709 if (va->va_end == nva_start_addr + size) 1710 type = FL_FIT_TYPE; 1711 else 1712 type = LE_FIT_TYPE; 1713 } else if (va->va_end == nva_start_addr + size) { 1714 type = RE_FIT_TYPE; 1715 } else { 1716 type = NE_FIT_TYPE; 1717 } 1718 1719 return type; 1720 } 1721 1722 static __always_inline int 1723 va_clip(struct rb_root *root, struct list_head *head, 1724 struct vmap_area *va, unsigned long nva_start_addr, 1725 unsigned long size) 1726 { 1727 struct vmap_area *lva = NULL; 1728 enum fit_type type = classify_va_fit_type(va, nva_start_addr, size); 1729 1730 if (type == FL_FIT_TYPE) { 1731 /* 1732 * No need to split VA, it fully fits. 1733 * 1734 * | | 1735 * V NVA V 1736 * |---------------| 1737 */ 1738 unlink_va_augment(va, root); 1739 kmem_cache_free(vmap_area_cachep, va); 1740 } else if (type == LE_FIT_TYPE) { 1741 /* 1742 * Split left edge of fit VA. 1743 * 1744 * | | 1745 * V NVA V R 1746 * |-------|-------| 1747 */ 1748 va->va_start += size; 1749 } else if (type == RE_FIT_TYPE) { 1750 /* 1751 * Split right edge of fit VA. 1752 * 1753 * | | 1754 * L V NVA V 1755 * |-------|-------| 1756 */ 1757 va->va_end = nva_start_addr; 1758 } else if (type == NE_FIT_TYPE) { 1759 /* 1760 * Split no edge of fit VA. 1761 * 1762 * | | 1763 * L V NVA V R 1764 * |---|-------|---| 1765 */ 1766 lva = __this_cpu_xchg(ne_fit_preload_node, NULL); 1767 if (unlikely(!lva)) { 1768 /* 1769 * For percpu allocator we do not do any pre-allocation 1770 * and leave it as it is. The reason is it most likely 1771 * never ends up with NE_FIT_TYPE splitting. In case of 1772 * percpu allocations offsets and sizes are aligned to 1773 * fixed align request, i.e. RE_FIT_TYPE and FL_FIT_TYPE 1774 * are its main fitting cases. 1775 * 1776 * There are a few exceptions though, as an example it is 1777 * a first allocation (early boot up) when we have "one" 1778 * big free space that has to be split. 1779 * 1780 * Also we can hit this path in case of regular "vmap" 1781 * allocations, if "this" current CPU was not preloaded. 1782 * See the comment in alloc_vmap_area() why. If so, then 1783 * GFP_NOWAIT is used instead to get an extra object for 1784 * split purpose. That is rare and most time does not 1785 * occur. 1786 * 1787 * What happens if an allocation gets failed. Basically, 1788 * an "overflow" path is triggered to purge lazily freed 1789 * areas to free some memory, then, the "retry" path is 1790 * triggered to repeat one more time. See more details 1791 * in alloc_vmap_area() function. 1792 */ 1793 lva = kmem_cache_alloc(vmap_area_cachep, GFP_NOWAIT); 1794 if (!lva) 1795 return -ENOMEM; 1796 } 1797 1798 /* 1799 * Build the remainder. 1800 */ 1801 lva->va_start = va->va_start; 1802 lva->va_end = nva_start_addr; 1803 1804 /* 1805 * Shrink this VA to remaining size. 1806 */ 1807 va->va_start = nva_start_addr + size; 1808 } else { 1809 return -EINVAL; 1810 } 1811 1812 if (type != FL_FIT_TYPE) { 1813 augment_tree_propagate_from(va); 1814 1815 if (lva) /* type == NE_FIT_TYPE */ 1816 insert_vmap_area_augment(lva, &va->rb_node, root, head); 1817 } 1818 1819 return 0; 1820 } 1821 1822 static unsigned long 1823 va_alloc(struct vmap_area *va, 1824 struct rb_root *root, struct list_head *head, 1825 unsigned long size, unsigned long align, 1826 unsigned long vstart, unsigned long vend) 1827 { 1828 unsigned long nva_start_addr; 1829 int ret; 1830 1831 if (va->va_start > vstart) 1832 nva_start_addr = ALIGN(va->va_start, align); 1833 else 1834 nva_start_addr = ALIGN(vstart, align); 1835 1836 /* Check the "vend" restriction. */ 1837 if (nva_start_addr + size > vend) 1838 return -ERANGE; 1839 1840 /* Update the free vmap_area. */ 1841 ret = va_clip(root, head, va, nva_start_addr, size); 1842 if (WARN_ON_ONCE(ret)) 1843 return ret; 1844 1845 return nva_start_addr; 1846 } 1847 1848 /* 1849 * Returns a start address of the newly allocated area, if success. 1850 * Otherwise an error value is returned that indicates failure. 1851 */ 1852 static __always_inline unsigned long 1853 __alloc_vmap_area(struct rb_root *root, struct list_head *head, 1854 unsigned long size, unsigned long align, 1855 unsigned long vstart, unsigned long vend) 1856 { 1857 bool adjust_search_size = true; 1858 unsigned long nva_start_addr; 1859 struct vmap_area *va; 1860 1861 /* 1862 * Do not adjust when: 1863 * a) align <= PAGE_SIZE, because it does not make any sense. 1864 * All blocks(their start addresses) are at least PAGE_SIZE 1865 * aligned anyway; 1866 * b) a short range where a requested size corresponds to exactly 1867 * specified [vstart:vend] interval and an alignment > PAGE_SIZE. 1868 * With adjusted search length an allocation would not succeed. 1869 */ 1870 if (align <= PAGE_SIZE || (align > PAGE_SIZE && (vend - vstart) == size)) 1871 adjust_search_size = false; 1872 1873 va = find_vmap_lowest_match(root, size, align, vstart, adjust_search_size); 1874 if (unlikely(!va)) 1875 return -ENOENT; 1876 1877 nva_start_addr = va_alloc(va, root, head, size, align, vstart, vend); 1878 1879 #if DEBUG_AUGMENT_LOWEST_MATCH_CHECK 1880 if (!IS_ERR_VALUE(nva_start_addr)) 1881 find_vmap_lowest_match_check(root, head, size, align); 1882 #endif 1883 1884 return nva_start_addr; 1885 } 1886 1887 /* 1888 * Free a region of KVA allocated by alloc_vmap_area 1889 */ 1890 static void free_vmap_area(struct vmap_area *va) 1891 { 1892 struct vmap_node *vn = addr_to_node(va->va_start); 1893 1894 /* 1895 * Remove from the busy tree/list. 1896 */ 1897 spin_lock(&vn->busy.lock); 1898 unlink_va(va, &vn->busy.root); 1899 spin_unlock(&vn->busy.lock); 1900 1901 /* 1902 * Insert/Merge it back to the free tree/list. 1903 */ 1904 spin_lock(&free_vmap_area_lock); 1905 merge_or_add_vmap_area_augment(va, &free_vmap_area_root, &free_vmap_area_list); 1906 spin_unlock(&free_vmap_area_lock); 1907 } 1908 1909 static inline void 1910 preload_this_cpu_lock(spinlock_t *lock, gfp_t gfp_mask, int node) 1911 { 1912 struct vmap_area *va = NULL, *tmp; 1913 1914 /* 1915 * Preload this CPU with one extra vmap_area object. It is used 1916 * when fit type of free area is NE_FIT_TYPE. It guarantees that 1917 * a CPU that does an allocation is preloaded. 1918 * 1919 * We do it in non-atomic context, thus it allows us to use more 1920 * permissive allocation masks to be more stable under low memory 1921 * condition and high memory pressure. 1922 */ 1923 if (!this_cpu_read(ne_fit_preload_node)) 1924 va = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node); 1925 1926 spin_lock(lock); 1927 1928 tmp = NULL; 1929 if (va && !__this_cpu_try_cmpxchg(ne_fit_preload_node, &tmp, va)) 1930 kmem_cache_free(vmap_area_cachep, va); 1931 } 1932 1933 static struct vmap_pool * 1934 size_to_va_pool(struct vmap_node *vn, unsigned long size) 1935 { 1936 unsigned int idx = (size - 1) / PAGE_SIZE; 1937 1938 if (idx < MAX_VA_SIZE_PAGES) 1939 return &vn->pool[idx]; 1940 1941 return NULL; 1942 } 1943 1944 static bool 1945 node_pool_add_va(struct vmap_node *n, struct vmap_area *va) 1946 { 1947 struct vmap_pool *vp; 1948 1949 vp = size_to_va_pool(n, va_size(va)); 1950 if (!vp) 1951 return false; 1952 1953 spin_lock(&n->pool_lock); 1954 list_add(&va->list, &vp->head); 1955 WRITE_ONCE(vp->len, vp->len + 1); 1956 spin_unlock(&n->pool_lock); 1957 1958 return true; 1959 } 1960 1961 static struct vmap_area * 1962 node_pool_del_va(struct vmap_node *vn, unsigned long size, 1963 unsigned long align, unsigned long vstart, 1964 unsigned long vend) 1965 { 1966 struct vmap_area *va = NULL; 1967 struct vmap_pool *vp; 1968 int err = 0; 1969 1970 vp = size_to_va_pool(vn, size); 1971 if (!vp || list_empty(&vp->head)) 1972 return NULL; 1973 1974 spin_lock(&vn->pool_lock); 1975 if (!list_empty(&vp->head)) { 1976 va = list_first_entry(&vp->head, struct vmap_area, list); 1977 1978 if (IS_ALIGNED(va->va_start, align)) { 1979 /* 1980 * Do some sanity check and emit a warning 1981 * if one of below checks detects an error. 1982 */ 1983 err |= (va_size(va) != size); 1984 err |= (va->va_start < vstart); 1985 err |= (va->va_end > vend); 1986 1987 if (!WARN_ON_ONCE(err)) { 1988 list_del_init(&va->list); 1989 WRITE_ONCE(vp->len, vp->len - 1); 1990 } else { 1991 va = NULL; 1992 } 1993 } else { 1994 list_move_tail(&va->list, &vp->head); 1995 va = NULL; 1996 } 1997 } 1998 spin_unlock(&vn->pool_lock); 1999 2000 return va; 2001 } 2002 2003 static struct vmap_area * 2004 node_alloc(unsigned long size, unsigned long align, 2005 unsigned long vstart, unsigned long vend, 2006 unsigned long *addr, unsigned int *vn_id) 2007 { 2008 struct vmap_area *va; 2009 2010 *vn_id = 0; 2011 *addr = -EINVAL; 2012 2013 /* 2014 * Fallback to a global heap if not vmalloc or there 2015 * is only one node. 2016 */ 2017 if (vstart != VMALLOC_START || vend != VMALLOC_END || 2018 nr_vmap_nodes == 1) 2019 return NULL; 2020 2021 *vn_id = raw_smp_processor_id() % nr_vmap_nodes; 2022 va = node_pool_del_va(id_to_node(*vn_id), size, align, vstart, vend); 2023 *vn_id = encode_vn_id(*vn_id); 2024 2025 if (va) 2026 *addr = va->va_start; 2027 2028 return va; 2029 } 2030 2031 static inline void setup_vmalloc_vm(struct vm_struct *vm, 2032 struct vmap_area *va, unsigned long flags, const void *caller) 2033 { 2034 vm->flags = flags; 2035 vm->addr = (void *)va->va_start; 2036 vm->size = vm->requested_size = va_size(va); 2037 vm->caller = caller; 2038 va->vm = vm; 2039 } 2040 2041 /* 2042 * Allocate a region of KVA of the specified size and alignment, within the 2043 * vstart and vend. If vm is passed in, the two will also be bound. 2044 */ 2045 static struct vmap_area *alloc_vmap_area(unsigned long size, 2046 unsigned long align, 2047 unsigned long vstart, unsigned long vend, 2048 int node, gfp_t gfp_mask, 2049 unsigned long va_flags, struct vm_struct *vm) 2050 { 2051 struct vmap_node *vn; 2052 struct vmap_area *va; 2053 unsigned long freed; 2054 unsigned long addr; 2055 unsigned int vn_id; 2056 bool allow_block; 2057 int purged = 0; 2058 int ret; 2059 2060 if (unlikely(!size || offset_in_page(size) || !is_power_of_2(align))) 2061 return ERR_PTR(-EINVAL); 2062 2063 if (unlikely(!vmap_initialized)) 2064 return ERR_PTR(-EBUSY); 2065 2066 /* Only reclaim behaviour flags are relevant. */ 2067 gfp_mask = gfp_mask & GFP_RECLAIM_MASK; 2068 allow_block = gfpflags_allow_blocking(gfp_mask); 2069 might_sleep_if(allow_block); 2070 2071 /* 2072 * If a VA is obtained from a global heap(if it fails here) 2073 * it is anyway marked with this "vn_id" so it is returned 2074 * to this pool's node later. Such way gives a possibility 2075 * to populate pools based on users demand. 2076 * 2077 * On success a ready to go VA is returned. 2078 */ 2079 va = node_alloc(size, align, vstart, vend, &addr, &vn_id); 2080 if (!va) { 2081 va = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node); 2082 if (unlikely(!va)) 2083 return ERR_PTR(-ENOMEM); 2084 2085 /* 2086 * Only scan the relevant parts containing pointers to other objects 2087 * to avoid false negatives. 2088 */ 2089 kmemleak_scan_area(&va->rb_node, SIZE_MAX, gfp_mask); 2090 } 2091 2092 retry: 2093 if (IS_ERR_VALUE(addr)) { 2094 preload_this_cpu_lock(&free_vmap_area_lock, gfp_mask, node); 2095 addr = __alloc_vmap_area(&free_vmap_area_root, &free_vmap_area_list, 2096 size, align, vstart, vend); 2097 spin_unlock(&free_vmap_area_lock); 2098 2099 /* 2100 * This is not a fast path. Check if yielding is needed. This 2101 * is the only reschedule point in the vmalloc() path. 2102 */ 2103 if (allow_block) 2104 cond_resched(); 2105 } 2106 2107 trace_alloc_vmap_area(addr, size, align, vstart, vend, IS_ERR_VALUE(addr)); 2108 2109 /* 2110 * If an allocation fails, the error value is 2111 * returned. Therefore trigger the overflow path. 2112 */ 2113 if (IS_ERR_VALUE(addr)) { 2114 if (allow_block) 2115 goto overflow; 2116 2117 /* 2118 * We can not trigger any reclaim logic because 2119 * sleeping is not allowed, thus fail an allocation. 2120 */ 2121 goto out_free_va; 2122 } 2123 2124 va->va_start = addr; 2125 va->va_end = addr + size; 2126 va->vm = NULL; 2127 va->flags = (va_flags | vn_id); 2128 2129 if (vm) { 2130 vm->addr = (void *)va->va_start; 2131 vm->size = va_size(va); 2132 va->vm = vm; 2133 } 2134 2135 vn = addr_to_node(va->va_start); 2136 2137 spin_lock(&vn->busy.lock); 2138 insert_vmap_area(va, &vn->busy.root, &vn->busy.head); 2139 spin_unlock(&vn->busy.lock); 2140 2141 BUG_ON(!IS_ALIGNED(va->va_start, align)); 2142 BUG_ON(va->va_start < vstart); 2143 BUG_ON(va->va_end > vend); 2144 2145 ret = kasan_populate_vmalloc(addr, size, gfp_mask); 2146 if (ret) { 2147 free_vmap_area(va); 2148 return ERR_PTR(ret); 2149 } 2150 2151 return va; 2152 2153 overflow: 2154 if (!purged) { 2155 reclaim_and_purge_vmap_areas(); 2156 purged = 1; 2157 goto retry; 2158 } 2159 2160 freed = 0; 2161 blocking_notifier_call_chain(&vmap_notify_list, 0, &freed); 2162 2163 if (freed > 0) { 2164 purged = 0; 2165 goto retry; 2166 } 2167 2168 if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) 2169 pr_warn("vmalloc_node_range for size %lu failed: Address range restricted to %#lx - %#lx\n", 2170 size, vstart, vend); 2171 2172 out_free_va: 2173 kmem_cache_free(vmap_area_cachep, va); 2174 return ERR_PTR(-EBUSY); 2175 } 2176 2177 int register_vmap_purge_notifier(struct notifier_block *nb) 2178 { 2179 return blocking_notifier_chain_register(&vmap_notify_list, nb); 2180 } 2181 EXPORT_SYMBOL_GPL(register_vmap_purge_notifier); 2182 2183 int unregister_vmap_purge_notifier(struct notifier_block *nb) 2184 { 2185 return blocking_notifier_chain_unregister(&vmap_notify_list, nb); 2186 } 2187 EXPORT_SYMBOL_GPL(unregister_vmap_purge_notifier); 2188 2189 /* 2190 * lazy_max_pages is the maximum amount of virtual address space we gather up 2191 * before attempting to purge with a TLB flush. 2192 * 2193 * There is a tradeoff here: a larger number will cover more kernel page tables 2194 * and take slightly longer to purge, but it will linearly reduce the number of 2195 * global TLB flushes that must be performed. It would seem natural to scale 2196 * this number up linearly with the number of CPUs (because vmapping activity 2197 * could also scale linearly with the number of CPUs), however it is likely 2198 * that in practice, workloads might be constrained in other ways that mean 2199 * vmap activity will not scale linearly with CPUs. Also, I want to be 2200 * conservative and not introduce a big latency on huge systems, so go with 2201 * a less aggressive log scale. It will still be an improvement over the old 2202 * code, and it will be simple to change the scale factor if we find that it 2203 * becomes a problem on bigger systems. 2204 */ 2205 static unsigned long lazy_max_pages(void) 2206 { 2207 unsigned int log; 2208 2209 log = fls(num_online_cpus()); 2210 2211 return log * (32UL * 1024 * 1024 / PAGE_SIZE); 2212 } 2213 2214 /* 2215 * Serialize vmap purging. There is no actual critical section protected 2216 * by this lock, but we want to avoid concurrent calls for performance 2217 * reasons and to make the pcpu_get_vm_areas more deterministic. 2218 */ 2219 static DEFINE_MUTEX(vmap_purge_lock); 2220 2221 /* for per-CPU blocks */ 2222 static void purge_fragmented_blocks_allcpus(void); 2223 2224 static void 2225 reclaim_list_global(struct list_head *head) 2226 { 2227 struct vmap_area *va, *n; 2228 2229 if (list_empty(head)) 2230 return; 2231 2232 spin_lock(&free_vmap_area_lock); 2233 list_for_each_entry_safe(va, n, head, list) 2234 merge_or_add_vmap_area_augment(va, 2235 &free_vmap_area_root, &free_vmap_area_list); 2236 spin_unlock(&free_vmap_area_lock); 2237 } 2238 2239 static void 2240 decay_va_pool_node(struct vmap_node *vn, bool full_decay) 2241 { 2242 LIST_HEAD(decay_list); 2243 struct rb_root decay_root = RB_ROOT; 2244 struct vmap_area *va, *nva; 2245 unsigned long n_decay, pool_len; 2246 int i; 2247 2248 for (i = 0; i < MAX_VA_SIZE_PAGES; i++) { 2249 LIST_HEAD(tmp_list); 2250 2251 if (list_empty(&vn->pool[i].head)) 2252 continue; 2253 2254 /* Detach the pool, so no-one can access it. */ 2255 spin_lock(&vn->pool_lock); 2256 list_replace_init(&vn->pool[i].head, &tmp_list); 2257 spin_unlock(&vn->pool_lock); 2258 2259 pool_len = n_decay = vn->pool[i].len; 2260 WRITE_ONCE(vn->pool[i].len, 0); 2261 2262 /* Decay a pool by ~25% out of left objects. */ 2263 if (!full_decay) 2264 n_decay >>= 2; 2265 pool_len -= n_decay; 2266 2267 list_for_each_entry_safe(va, nva, &tmp_list, list) { 2268 if (!n_decay--) 2269 break; 2270 2271 list_del_init(&va->list); 2272 merge_or_add_vmap_area(va, &decay_root, &decay_list); 2273 } 2274 2275 /* 2276 * Attach the pool back if it has been partly decayed. 2277 * Please note, it is supposed that nobody(other contexts) 2278 * can populate the pool therefore a simple list replace 2279 * operation takes place here. 2280 */ 2281 if (!list_empty(&tmp_list)) { 2282 spin_lock(&vn->pool_lock); 2283 list_replace_init(&tmp_list, &vn->pool[i].head); 2284 WRITE_ONCE(vn->pool[i].len, pool_len); 2285 spin_unlock(&vn->pool_lock); 2286 } 2287 } 2288 2289 reclaim_list_global(&decay_list); 2290 } 2291 2292 #define KASAN_RELEASE_BATCH_SIZE 32 2293 2294 static void 2295 kasan_release_vmalloc_node(struct vmap_node *vn) 2296 { 2297 struct vmap_area *va; 2298 unsigned long start, end; 2299 unsigned int batch_count = 0; 2300 2301 start = list_first_entry(&vn->purge_list, struct vmap_area, list)->va_start; 2302 end = list_last_entry(&vn->purge_list, struct vmap_area, list)->va_end; 2303 2304 list_for_each_entry(va, &vn->purge_list, list) { 2305 if (is_vmalloc_or_module_addr((void *) va->va_start)) 2306 kasan_release_vmalloc(va->va_start, va->va_end, 2307 va->va_start, va->va_end, 2308 KASAN_VMALLOC_PAGE_RANGE); 2309 2310 if (need_resched() || (++batch_count >= KASAN_RELEASE_BATCH_SIZE)) { 2311 cond_resched(); 2312 batch_count = 0; 2313 } 2314 } 2315 2316 kasan_release_vmalloc(start, end, start, end, KASAN_VMALLOC_TLB_FLUSH); 2317 } 2318 2319 static void purge_vmap_node(struct work_struct *work) 2320 { 2321 struct vmap_node *vn = container_of(work, 2322 struct vmap_node, purge_work); 2323 unsigned long nr_purged_pages = 0; 2324 struct vmap_area *va, *n_va; 2325 LIST_HEAD(local_list); 2326 2327 if (IS_ENABLED(CONFIG_KASAN_VMALLOC)) 2328 kasan_release_vmalloc_node(vn); 2329 2330 vn->nr_purged = 0; 2331 2332 list_for_each_entry_safe(va, n_va, &vn->purge_list, list) { 2333 unsigned long nr = va_size(va) >> PAGE_SHIFT; 2334 unsigned int vn_id = decode_vn_id(va->flags); 2335 2336 list_del_init(&va->list); 2337 2338 nr_purged_pages += nr; 2339 vn->nr_purged++; 2340 2341 if (is_vn_id_valid(vn_id) && !vn->skip_populate) 2342 if (node_pool_add_va(vn, va)) 2343 continue; 2344 2345 /* Go back to global. */ 2346 list_add(&va->list, &local_list); 2347 } 2348 2349 atomic_long_sub(nr_purged_pages, &vmap_lazy_nr); 2350 2351 reclaim_list_global(&local_list); 2352 } 2353 2354 /* 2355 * Purges all lazily-freed vmap areas. 2356 */ 2357 static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end, 2358 bool full_pool_decay) 2359 { 2360 unsigned long nr_purged_areas = 0; 2361 unsigned int nr_purge_helpers; 2362 static cpumask_t purge_nodes; 2363 unsigned int nr_purge_nodes; 2364 struct vmap_node *vn; 2365 int i; 2366 2367 lockdep_assert_held(&vmap_purge_lock); 2368 2369 /* 2370 * Use cpumask to mark which node has to be processed. 2371 */ 2372 purge_nodes = CPU_MASK_NONE; 2373 2374 for_each_vmap_node(vn) { 2375 INIT_LIST_HEAD(&vn->purge_list); 2376 vn->skip_populate = full_pool_decay; 2377 decay_va_pool_node(vn, full_pool_decay); 2378 2379 if (RB_EMPTY_ROOT(&vn->lazy.root)) 2380 continue; 2381 2382 spin_lock(&vn->lazy.lock); 2383 WRITE_ONCE(vn->lazy.root.rb_node, NULL); 2384 list_replace_init(&vn->lazy.head, &vn->purge_list); 2385 spin_unlock(&vn->lazy.lock); 2386 2387 start = min(start, list_first_entry(&vn->purge_list, 2388 struct vmap_area, list)->va_start); 2389 2390 end = max(end, list_last_entry(&vn->purge_list, 2391 struct vmap_area, list)->va_end); 2392 2393 cpumask_set_cpu(node_to_id(vn), &purge_nodes); 2394 } 2395 2396 nr_purge_nodes = cpumask_weight(&purge_nodes); 2397 if (nr_purge_nodes > 0) { 2398 flush_tlb_kernel_range(start, end); 2399 2400 /* One extra worker is per a lazy_max_pages() full set minus one. */ 2401 nr_purge_helpers = atomic_long_read(&vmap_lazy_nr) / lazy_max_pages(); 2402 nr_purge_helpers = clamp(nr_purge_helpers, 1U, nr_purge_nodes) - 1; 2403 2404 for_each_cpu(i, &purge_nodes) { 2405 vn = &vmap_nodes[i]; 2406 2407 if (nr_purge_helpers > 0) { 2408 INIT_WORK(&vn->purge_work, purge_vmap_node); 2409 2410 if (cpumask_test_cpu(i, cpu_online_mask)) 2411 schedule_work_on(i, &vn->purge_work); 2412 else 2413 schedule_work(&vn->purge_work); 2414 2415 nr_purge_helpers--; 2416 } else { 2417 vn->purge_work.func = NULL; 2418 purge_vmap_node(&vn->purge_work); 2419 nr_purged_areas += vn->nr_purged; 2420 } 2421 } 2422 2423 for_each_cpu(i, &purge_nodes) { 2424 vn = &vmap_nodes[i]; 2425 2426 if (vn->purge_work.func) { 2427 flush_work(&vn->purge_work); 2428 nr_purged_areas += vn->nr_purged; 2429 } 2430 } 2431 } 2432 2433 trace_purge_vmap_area_lazy(start, end, nr_purged_areas); 2434 return nr_purged_areas > 0; 2435 } 2436 2437 /* 2438 * Reclaim vmap areas by purging fragmented blocks and purge_vmap_area_list. 2439 */ 2440 static void reclaim_and_purge_vmap_areas(void) 2441 2442 { 2443 mutex_lock(&vmap_purge_lock); 2444 purge_fragmented_blocks_allcpus(); 2445 __purge_vmap_area_lazy(ULONG_MAX, 0, true); 2446 mutex_unlock(&vmap_purge_lock); 2447 } 2448 2449 static void drain_vmap_area_work(struct work_struct *work) 2450 { 2451 mutex_lock(&vmap_purge_lock); 2452 __purge_vmap_area_lazy(ULONG_MAX, 0, false); 2453 mutex_unlock(&vmap_purge_lock); 2454 } 2455 2456 /* 2457 * Free a vmap area, caller ensuring that the area has been unmapped, 2458 * unlinked and flush_cache_vunmap had been called for the correct 2459 * range previously. 2460 */ 2461 static void free_vmap_area_noflush(struct vmap_area *va) 2462 { 2463 unsigned long nr_lazy_max = lazy_max_pages(); 2464 unsigned long va_start = va->va_start; 2465 unsigned int vn_id = decode_vn_id(va->flags); 2466 struct vmap_node *vn; 2467 unsigned long nr_lazy; 2468 2469 if (WARN_ON_ONCE(!list_empty(&va->list))) 2470 return; 2471 2472 nr_lazy = atomic_long_add_return_relaxed(va_size(va) >> PAGE_SHIFT, 2473 &vmap_lazy_nr); 2474 2475 /* 2476 * If it was request by a certain node we would like to 2477 * return it to that node, i.e. its pool for later reuse. 2478 */ 2479 vn = is_vn_id_valid(vn_id) ? 2480 id_to_node(vn_id):addr_to_node(va->va_start); 2481 2482 spin_lock(&vn->lazy.lock); 2483 insert_vmap_area(va, &vn->lazy.root, &vn->lazy.head); 2484 spin_unlock(&vn->lazy.lock); 2485 2486 trace_free_vmap_area_noflush(va_start, nr_lazy, nr_lazy_max); 2487 2488 /* After this point, we may free va at any time */ 2489 if (unlikely(nr_lazy > nr_lazy_max)) 2490 schedule_work(&drain_vmap_work); 2491 } 2492 2493 /* 2494 * Free and unmap a vmap area 2495 */ 2496 static void free_unmap_vmap_area(struct vmap_area *va) 2497 { 2498 flush_cache_vunmap(va->va_start, va->va_end); 2499 vunmap_range_noflush(va->va_start, va->va_end); 2500 if (debug_pagealloc_enabled_static()) 2501 flush_tlb_kernel_range(va->va_start, va->va_end); 2502 2503 free_vmap_area_noflush(va); 2504 } 2505 2506 struct vmap_area *find_vmap_area(unsigned long addr) 2507 { 2508 struct vmap_node *vn; 2509 struct vmap_area *va; 2510 int i, j; 2511 2512 if (unlikely(!vmap_initialized)) 2513 return NULL; 2514 2515 /* 2516 * An addr_to_node_id(addr) converts an address to a node index 2517 * where a VA is located. If VA spans several zones and passed 2518 * addr is not the same as va->va_start, what is not common, we 2519 * may need to scan extra nodes. See an example: 2520 * 2521 * <----va----> 2522 * -|-----|-----|-----|-----|- 2523 * 1 2 0 1 2524 * 2525 * VA resides in node 1 whereas it spans 1, 2 an 0. If passed 2526 * addr is within 2 or 0 nodes we should do extra work. 2527 */ 2528 i = j = addr_to_node_id(addr); 2529 do { 2530 vn = &vmap_nodes[i]; 2531 2532 spin_lock(&vn->busy.lock); 2533 va = __find_vmap_area(addr, &vn->busy.root); 2534 spin_unlock(&vn->busy.lock); 2535 2536 if (va) 2537 return va; 2538 } while ((i = (i + nr_vmap_nodes - 1) % nr_vmap_nodes) != j); 2539 2540 return NULL; 2541 } 2542 2543 static struct vmap_area *find_unlink_vmap_area(unsigned long addr) 2544 { 2545 struct vmap_node *vn; 2546 struct vmap_area *va; 2547 int i, j; 2548 2549 /* 2550 * Check the comment in the find_vmap_area() about the loop. 2551 */ 2552 i = j = addr_to_node_id(addr); 2553 do { 2554 vn = &vmap_nodes[i]; 2555 2556 spin_lock(&vn->busy.lock); 2557 va = __find_vmap_area(addr, &vn->busy.root); 2558 if (va) 2559 unlink_va(va, &vn->busy.root); 2560 spin_unlock(&vn->busy.lock); 2561 2562 if (va) 2563 return va; 2564 } while ((i = (i + nr_vmap_nodes - 1) % nr_vmap_nodes) != j); 2565 2566 return NULL; 2567 } 2568 2569 /*** Per cpu kva allocator ***/ 2570 2571 /* 2572 * vmap space is limited especially on 32 bit architectures. Ensure there is 2573 * room for at least 16 percpu vmap blocks per CPU. 2574 */ 2575 /* 2576 * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able 2577 * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess 2578 * instead (we just need a rough idea) 2579 */ 2580 #if BITS_PER_LONG == 32 2581 #define VMALLOC_SPACE (128UL*1024*1024) 2582 #else 2583 #define VMALLOC_SPACE (128UL*1024*1024*1024) 2584 #endif 2585 2586 #define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE) 2587 #define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */ 2588 #define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */ 2589 #define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2) 2590 #define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */ 2591 #define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */ 2592 #define VMAP_BBMAP_BITS \ 2593 VMAP_MIN(VMAP_BBMAP_BITS_MAX, \ 2594 VMAP_MAX(VMAP_BBMAP_BITS_MIN, \ 2595 VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16)) 2596 2597 #define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE) 2598 2599 /* 2600 * Purge threshold to prevent overeager purging of fragmented blocks for 2601 * regular operations: Purge if vb->free is less than 1/4 of the capacity. 2602 */ 2603 #define VMAP_PURGE_THRESHOLD (VMAP_BBMAP_BITS / 4) 2604 2605 #define VMAP_RAM 0x1 /* indicates vm_map_ram area*/ 2606 #define VMAP_BLOCK 0x2 /* mark out the vmap_block sub-type*/ 2607 #define VMAP_FLAGS_MASK 0x3 2608 2609 struct vmap_block_queue { 2610 spinlock_t lock; 2611 struct list_head free; 2612 2613 /* 2614 * An xarray requires an extra memory dynamically to 2615 * be allocated. If it is an issue, we can use rb-tree 2616 * instead. 2617 */ 2618 struct xarray vmap_blocks; 2619 }; 2620 2621 struct vmap_block { 2622 spinlock_t lock; 2623 struct vmap_area *va; 2624 unsigned long free, dirty; 2625 DECLARE_BITMAP(used_map, VMAP_BBMAP_BITS); 2626 unsigned long dirty_min, dirty_max; /*< dirty range */ 2627 struct list_head free_list; 2628 struct rcu_head rcu_head; 2629 struct list_head purge; 2630 unsigned int cpu; 2631 }; 2632 2633 /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */ 2634 static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue); 2635 2636 /* 2637 * In order to fast access to any "vmap_block" associated with a 2638 * specific address, we use a hash. 2639 * 2640 * A per-cpu vmap_block_queue is used in both ways, to serialize 2641 * an access to free block chains among CPUs(alloc path) and it 2642 * also acts as a vmap_block hash(alloc/free paths). It means we 2643 * overload it, since we already have the per-cpu array which is 2644 * used as a hash table. When used as a hash a 'cpu' passed to 2645 * per_cpu() is not actually a CPU but rather a hash index. 2646 * 2647 * A hash function is addr_to_vb_xa() which hashes any address 2648 * to a specific index(in a hash) it belongs to. This then uses a 2649 * per_cpu() macro to access an array with generated index. 2650 * 2651 * An example: 2652 * 2653 * CPU_1 CPU_2 CPU_0 2654 * | | | 2655 * V V V 2656 * 0 10 20 30 40 50 60 2657 * |------|------|------|------|------|------|...<vmap address space> 2658 * CPU0 CPU1 CPU2 CPU0 CPU1 CPU2 2659 * 2660 * - CPU_1 invokes vm_unmap_ram(6), 6 belongs to CPU0 zone, thus 2661 * it access: CPU0/INDEX0 -> vmap_blocks -> xa_lock; 2662 * 2663 * - CPU_2 invokes vm_unmap_ram(11), 11 belongs to CPU1 zone, thus 2664 * it access: CPU1/INDEX1 -> vmap_blocks -> xa_lock; 2665 * 2666 * - CPU_0 invokes vm_unmap_ram(20), 20 belongs to CPU2 zone, thus 2667 * it access: CPU2/INDEX2 -> vmap_blocks -> xa_lock. 2668 * 2669 * This technique almost always avoids lock contention on insert/remove, 2670 * however xarray spinlocks protect against any contention that remains. 2671 */ 2672 static struct xarray * 2673 addr_to_vb_xa(unsigned long addr) 2674 { 2675 int index = (addr / VMAP_BLOCK_SIZE) % nr_cpu_ids; 2676 2677 /* 2678 * Please note, nr_cpu_ids points on a highest set 2679 * possible bit, i.e. we never invoke cpumask_next() 2680 * if an index points on it which is nr_cpu_ids - 1. 2681 */ 2682 if (!cpu_possible(index)) 2683 index = cpumask_next(index, cpu_possible_mask); 2684 2685 return &per_cpu(vmap_block_queue, index).vmap_blocks; 2686 } 2687 2688 /* 2689 * We should probably have a fallback mechanism to allocate virtual memory 2690 * out of partially filled vmap blocks. However vmap block sizing should be 2691 * fairly reasonable according to the vmalloc size, so it shouldn't be a 2692 * big problem. 2693 */ 2694 2695 static unsigned long addr_to_vb_idx(unsigned long addr) 2696 { 2697 addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1); 2698 addr /= VMAP_BLOCK_SIZE; 2699 return addr; 2700 } 2701 2702 static void *vmap_block_vaddr(unsigned long va_start, unsigned long pages_off) 2703 { 2704 unsigned long addr; 2705 2706 addr = va_start + (pages_off << PAGE_SHIFT); 2707 BUG_ON(addr_to_vb_idx(addr) != addr_to_vb_idx(va_start)); 2708 return (void *)addr; 2709 } 2710 2711 /** 2712 * new_vmap_block - allocates new vmap_block and occupies 2^order pages in this 2713 * block. Of course pages number can't exceed VMAP_BBMAP_BITS 2714 * @order: how many 2^order pages should be occupied in newly allocated block 2715 * @gfp_mask: flags for the page level allocator 2716 * 2717 * Return: virtual address in a newly allocated block or ERR_PTR(-errno) 2718 */ 2719 static void *new_vmap_block(unsigned int order, gfp_t gfp_mask) 2720 { 2721 struct vmap_block_queue *vbq; 2722 struct vmap_block *vb; 2723 struct vmap_area *va; 2724 struct xarray *xa; 2725 unsigned long vb_idx; 2726 int node, err; 2727 void *vaddr; 2728 2729 node = numa_node_id(); 2730 2731 vb = kmalloc_node(sizeof(struct vmap_block), gfp_mask, node); 2732 if (unlikely(!vb)) 2733 return ERR_PTR(-ENOMEM); 2734 2735 va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE, 2736 VMALLOC_START, VMALLOC_END, 2737 node, gfp_mask, 2738 VMAP_RAM|VMAP_BLOCK, NULL); 2739 if (IS_ERR(va)) { 2740 kfree(vb); 2741 return ERR_CAST(va); 2742 } 2743 2744 vaddr = vmap_block_vaddr(va->va_start, 0); 2745 spin_lock_init(&vb->lock); 2746 vb->va = va; 2747 /* At least something should be left free */ 2748 BUG_ON(VMAP_BBMAP_BITS <= (1UL << order)); 2749 bitmap_zero(vb->used_map, VMAP_BBMAP_BITS); 2750 vb->free = VMAP_BBMAP_BITS - (1UL << order); 2751 vb->dirty = 0; 2752 vb->dirty_min = VMAP_BBMAP_BITS; 2753 vb->dirty_max = 0; 2754 bitmap_set(vb->used_map, 0, (1UL << order)); 2755 INIT_LIST_HEAD(&vb->free_list); 2756 vb->cpu = raw_smp_processor_id(); 2757 2758 xa = addr_to_vb_xa(va->va_start); 2759 vb_idx = addr_to_vb_idx(va->va_start); 2760 err = xa_insert(xa, vb_idx, vb, gfp_mask); 2761 if (err) { 2762 kfree(vb); 2763 free_vmap_area(va); 2764 return ERR_PTR(err); 2765 } 2766 /* 2767 * list_add_tail_rcu could happened in another core 2768 * rather than vb->cpu due to task migration, which 2769 * is safe as list_add_tail_rcu will ensure the list's 2770 * integrity together with list_for_each_rcu from read 2771 * side. 2772 */ 2773 vbq = per_cpu_ptr(&vmap_block_queue, vb->cpu); 2774 spin_lock(&vbq->lock); 2775 list_add_tail_rcu(&vb->free_list, &vbq->free); 2776 spin_unlock(&vbq->lock); 2777 2778 return vaddr; 2779 } 2780 2781 static void free_vmap_block(struct vmap_block *vb) 2782 { 2783 struct vmap_node *vn; 2784 struct vmap_block *tmp; 2785 struct xarray *xa; 2786 2787 xa = addr_to_vb_xa(vb->va->va_start); 2788 tmp = xa_erase(xa, addr_to_vb_idx(vb->va->va_start)); 2789 BUG_ON(tmp != vb); 2790 2791 vn = addr_to_node(vb->va->va_start); 2792 spin_lock(&vn->busy.lock); 2793 unlink_va(vb->va, &vn->busy.root); 2794 spin_unlock(&vn->busy.lock); 2795 2796 free_vmap_area_noflush(vb->va); 2797 kfree_rcu(vb, rcu_head); 2798 } 2799 2800 static bool purge_fragmented_block(struct vmap_block *vb, 2801 struct list_head *purge_list, bool force_purge) 2802 { 2803 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, vb->cpu); 2804 2805 if (vb->free + vb->dirty != VMAP_BBMAP_BITS || 2806 vb->dirty == VMAP_BBMAP_BITS) 2807 return false; 2808 2809 /* Don't overeagerly purge usable blocks unless requested */ 2810 if (!(force_purge || vb->free < VMAP_PURGE_THRESHOLD)) 2811 return false; 2812 2813 /* prevent further allocs after releasing lock */ 2814 WRITE_ONCE(vb->free, 0); 2815 /* prevent purging it again */ 2816 WRITE_ONCE(vb->dirty, VMAP_BBMAP_BITS); 2817 vb->dirty_min = 0; 2818 vb->dirty_max = VMAP_BBMAP_BITS; 2819 spin_lock(&vbq->lock); 2820 list_del_rcu(&vb->free_list); 2821 spin_unlock(&vbq->lock); 2822 list_add_tail(&vb->purge, purge_list); 2823 return true; 2824 } 2825 2826 static void free_purged_blocks(struct list_head *purge_list) 2827 { 2828 struct vmap_block *vb, *n_vb; 2829 2830 list_for_each_entry_safe(vb, n_vb, purge_list, purge) { 2831 list_del(&vb->purge); 2832 free_vmap_block(vb); 2833 } 2834 } 2835 2836 static void purge_fragmented_blocks(int cpu) 2837 { 2838 LIST_HEAD(purge); 2839 struct vmap_block *vb; 2840 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu); 2841 2842 rcu_read_lock(); 2843 list_for_each_entry_rcu(vb, &vbq->free, free_list) { 2844 unsigned long free = READ_ONCE(vb->free); 2845 unsigned long dirty = READ_ONCE(vb->dirty); 2846 2847 if (free + dirty != VMAP_BBMAP_BITS || 2848 dirty == VMAP_BBMAP_BITS) 2849 continue; 2850 2851 spin_lock(&vb->lock); 2852 purge_fragmented_block(vb, &purge, true); 2853 spin_unlock(&vb->lock); 2854 } 2855 rcu_read_unlock(); 2856 free_purged_blocks(&purge); 2857 } 2858 2859 static void purge_fragmented_blocks_allcpus(void) 2860 { 2861 int cpu; 2862 2863 for_each_possible_cpu(cpu) 2864 purge_fragmented_blocks(cpu); 2865 } 2866 2867 static void *vb_alloc(unsigned long size, gfp_t gfp_mask) 2868 { 2869 struct vmap_block_queue *vbq; 2870 struct vmap_block *vb; 2871 void *vaddr = NULL; 2872 unsigned int order; 2873 2874 BUG_ON(offset_in_page(size)); 2875 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC); 2876 if (WARN_ON(size == 0)) { 2877 /* 2878 * Allocating 0 bytes isn't what caller wants since 2879 * get_order(0) returns funny result. Just warn and terminate 2880 * early. 2881 */ 2882 return ERR_PTR(-EINVAL); 2883 } 2884 order = get_order(size); 2885 2886 rcu_read_lock(); 2887 vbq = raw_cpu_ptr(&vmap_block_queue); 2888 list_for_each_entry_rcu(vb, &vbq->free, free_list) { 2889 unsigned long pages_off; 2890 2891 if (READ_ONCE(vb->free) < (1UL << order)) 2892 continue; 2893 2894 spin_lock(&vb->lock); 2895 if (vb->free < (1UL << order)) { 2896 spin_unlock(&vb->lock); 2897 continue; 2898 } 2899 2900 pages_off = VMAP_BBMAP_BITS - vb->free; 2901 vaddr = vmap_block_vaddr(vb->va->va_start, pages_off); 2902 WRITE_ONCE(vb->free, vb->free - (1UL << order)); 2903 bitmap_set(vb->used_map, pages_off, (1UL << order)); 2904 if (vb->free == 0) { 2905 spin_lock(&vbq->lock); 2906 list_del_rcu(&vb->free_list); 2907 spin_unlock(&vbq->lock); 2908 } 2909 2910 spin_unlock(&vb->lock); 2911 break; 2912 } 2913 2914 rcu_read_unlock(); 2915 2916 /* Allocate new block if nothing was found */ 2917 if (!vaddr) 2918 vaddr = new_vmap_block(order, gfp_mask); 2919 2920 return vaddr; 2921 } 2922 2923 static void vb_free(unsigned long addr, unsigned long size) 2924 { 2925 unsigned long offset; 2926 unsigned int order; 2927 struct vmap_block *vb; 2928 struct xarray *xa; 2929 2930 BUG_ON(offset_in_page(size)); 2931 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC); 2932 2933 flush_cache_vunmap(addr, addr + size); 2934 2935 order = get_order(size); 2936 offset = (addr & (VMAP_BLOCK_SIZE - 1)) >> PAGE_SHIFT; 2937 2938 xa = addr_to_vb_xa(addr); 2939 vb = xa_load(xa, addr_to_vb_idx(addr)); 2940 2941 spin_lock(&vb->lock); 2942 bitmap_clear(vb->used_map, offset, (1UL << order)); 2943 spin_unlock(&vb->lock); 2944 2945 vunmap_range_noflush(addr, addr + size); 2946 2947 if (debug_pagealloc_enabled_static()) 2948 flush_tlb_kernel_range(addr, addr + size); 2949 2950 spin_lock(&vb->lock); 2951 2952 /* Expand the not yet TLB flushed dirty range */ 2953 vb->dirty_min = min(vb->dirty_min, offset); 2954 vb->dirty_max = max(vb->dirty_max, offset + (1UL << order)); 2955 2956 WRITE_ONCE(vb->dirty, vb->dirty + (1UL << order)); 2957 if (vb->dirty == VMAP_BBMAP_BITS) { 2958 BUG_ON(vb->free); 2959 spin_unlock(&vb->lock); 2960 free_vmap_block(vb); 2961 } else 2962 spin_unlock(&vb->lock); 2963 } 2964 2965 static void _vm_unmap_aliases(unsigned long start, unsigned long end, int flush) 2966 { 2967 LIST_HEAD(purge_list); 2968 int cpu; 2969 2970 if (unlikely(!vmap_initialized)) 2971 return; 2972 2973 mutex_lock(&vmap_purge_lock); 2974 2975 for_each_possible_cpu(cpu) { 2976 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu); 2977 struct vmap_block *vb; 2978 unsigned long idx; 2979 2980 rcu_read_lock(); 2981 xa_for_each(&vbq->vmap_blocks, idx, vb) { 2982 spin_lock(&vb->lock); 2983 2984 /* 2985 * Try to purge a fragmented block first. If it's 2986 * not purgeable, check whether there is dirty 2987 * space to be flushed. 2988 */ 2989 if (!purge_fragmented_block(vb, &purge_list, false) && 2990 vb->dirty_max && vb->dirty != VMAP_BBMAP_BITS) { 2991 unsigned long va_start = vb->va->va_start; 2992 unsigned long s, e; 2993 2994 s = va_start + (vb->dirty_min << PAGE_SHIFT); 2995 e = va_start + (vb->dirty_max << PAGE_SHIFT); 2996 2997 start = min(s, start); 2998 end = max(e, end); 2999 3000 /* Prevent that this is flushed again */ 3001 vb->dirty_min = VMAP_BBMAP_BITS; 3002 vb->dirty_max = 0; 3003 3004 flush = 1; 3005 } 3006 spin_unlock(&vb->lock); 3007 } 3008 rcu_read_unlock(); 3009 } 3010 free_purged_blocks(&purge_list); 3011 3012 if (!__purge_vmap_area_lazy(start, end, false) && flush) 3013 flush_tlb_kernel_range(start, end); 3014 mutex_unlock(&vmap_purge_lock); 3015 } 3016 3017 /** 3018 * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer 3019 * 3020 * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily 3021 * to amortize TLB flushing overheads. What this means is that any page you 3022 * have now, may, in a former life, have been mapped into kernel virtual 3023 * address by the vmap layer and so there might be some CPUs with TLB entries 3024 * still referencing that page (additional to the regular 1:1 kernel mapping). 3025 * 3026 * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can 3027 * be sure that none of the pages we have control over will have any aliases 3028 * from the vmap layer. 3029 */ 3030 void vm_unmap_aliases(void) 3031 { 3032 _vm_unmap_aliases(ULONG_MAX, 0, 0); 3033 } 3034 EXPORT_SYMBOL_GPL(vm_unmap_aliases); 3035 3036 /** 3037 * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram 3038 * @mem: the pointer returned by vm_map_ram 3039 * @count: the count passed to that vm_map_ram call (cannot unmap partial) 3040 */ 3041 void vm_unmap_ram(const void *mem, unsigned int count) 3042 { 3043 unsigned long size = (unsigned long)count << PAGE_SHIFT; 3044 unsigned long addr = (unsigned long)kasan_reset_tag(mem); 3045 struct vmap_area *va; 3046 3047 might_sleep(); 3048 BUG_ON(!addr); 3049 BUG_ON(addr < VMALLOC_START); 3050 BUG_ON(addr > VMALLOC_END); 3051 BUG_ON(!PAGE_ALIGNED(addr)); 3052 3053 kasan_poison_vmalloc(mem, size); 3054 3055 if (likely(count <= VMAP_MAX_ALLOC)) { 3056 debug_check_no_locks_freed(mem, size); 3057 vb_free(addr, size); 3058 return; 3059 } 3060 3061 va = find_unlink_vmap_area(addr); 3062 if (WARN_ON_ONCE(!va)) 3063 return; 3064 3065 debug_check_no_locks_freed((void *)va->va_start, va_size(va)); 3066 free_unmap_vmap_area(va); 3067 } 3068 EXPORT_SYMBOL(vm_unmap_ram); 3069 3070 /** 3071 * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space) 3072 * @pages: an array of pointers to the pages to be mapped 3073 * @count: number of pages 3074 * @node: prefer to allocate data structures on this node 3075 * 3076 * If you use this function for less than VMAP_MAX_ALLOC pages, it could be 3077 * faster than vmap so it's good. But if you mix long-life and short-life 3078 * objects with vm_map_ram(), it could consume lots of address space through 3079 * fragmentation (especially on a 32bit machine). You could see failures in 3080 * the end. Please use this function for short-lived objects. 3081 * 3082 * Returns: a pointer to the address that has been mapped, or %NULL on failure 3083 */ 3084 void *vm_map_ram(struct page **pages, unsigned int count, int node) 3085 { 3086 unsigned long size = (unsigned long)count << PAGE_SHIFT; 3087 unsigned long addr; 3088 void *mem; 3089 3090 if (likely(count <= VMAP_MAX_ALLOC)) { 3091 mem = vb_alloc(size, GFP_KERNEL); 3092 if (IS_ERR(mem)) 3093 return NULL; 3094 addr = (unsigned long)mem; 3095 } else { 3096 struct vmap_area *va; 3097 va = alloc_vmap_area(size, PAGE_SIZE, 3098 VMALLOC_START, VMALLOC_END, 3099 node, GFP_KERNEL, VMAP_RAM, 3100 NULL); 3101 if (IS_ERR(va)) 3102 return NULL; 3103 3104 addr = va->va_start; 3105 mem = (void *)addr; 3106 } 3107 3108 if (vmap_pages_range(addr, addr + size, PAGE_KERNEL, 3109 pages, PAGE_SHIFT) < 0) { 3110 vm_unmap_ram(mem, count); 3111 return NULL; 3112 } 3113 3114 /* 3115 * Mark the pages as accessible, now that they are mapped. 3116 * With hardware tag-based KASAN, marking is skipped for 3117 * non-VM_ALLOC mappings, see __kasan_unpoison_vmalloc(). 3118 */ 3119 mem = kasan_unpoison_vmalloc(mem, size, KASAN_VMALLOC_PROT_NORMAL); 3120 3121 return mem; 3122 } 3123 EXPORT_SYMBOL(vm_map_ram); 3124 3125 static struct vm_struct *vmlist __initdata; 3126 3127 static inline unsigned int vm_area_page_order(struct vm_struct *vm) 3128 { 3129 #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC 3130 return vm->page_order; 3131 #else 3132 return 0; 3133 #endif 3134 } 3135 3136 unsigned int get_vm_area_page_order(struct vm_struct *vm) 3137 { 3138 return vm_area_page_order(vm); 3139 } 3140 3141 static inline void set_vm_area_page_order(struct vm_struct *vm, unsigned int order) 3142 { 3143 #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC 3144 vm->page_order = order; 3145 #else 3146 BUG_ON(order != 0); 3147 #endif 3148 } 3149 3150 /** 3151 * vm_area_add_early - add vmap area early during boot 3152 * @vm: vm_struct to add 3153 * 3154 * This function is used to add fixed kernel vm area to vmlist before 3155 * vmalloc_init() is called. @vm->addr, @vm->size, and @vm->flags 3156 * should contain proper values and the other fields should be zero. 3157 * 3158 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING. 3159 */ 3160 void __init vm_area_add_early(struct vm_struct *vm) 3161 { 3162 struct vm_struct *tmp, **p; 3163 3164 BUG_ON(vmap_initialized); 3165 for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) { 3166 if (tmp->addr >= vm->addr) { 3167 BUG_ON(tmp->addr < vm->addr + vm->size); 3168 break; 3169 } else 3170 BUG_ON(tmp->addr + tmp->size > vm->addr); 3171 } 3172 vm->next = *p; 3173 *p = vm; 3174 } 3175 3176 /** 3177 * vm_area_register_early - register vmap area early during boot 3178 * @vm: vm_struct to register 3179 * @align: requested alignment 3180 * 3181 * This function is used to register kernel vm area before 3182 * vmalloc_init() is called. @vm->size and @vm->flags should contain 3183 * proper values on entry and other fields should be zero. On return, 3184 * vm->addr contains the allocated address. 3185 * 3186 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING. 3187 */ 3188 void __init vm_area_register_early(struct vm_struct *vm, size_t align) 3189 { 3190 unsigned long addr = ALIGN(VMALLOC_START, align); 3191 struct vm_struct *cur, **p; 3192 3193 BUG_ON(vmap_initialized); 3194 3195 for (p = &vmlist; (cur = *p) != NULL; p = &cur->next) { 3196 if ((unsigned long)cur->addr - addr >= vm->size) 3197 break; 3198 addr = ALIGN((unsigned long)cur->addr + cur->size, align); 3199 } 3200 3201 BUG_ON(addr > VMALLOC_END - vm->size); 3202 vm->addr = (void *)addr; 3203 vm->next = *p; 3204 *p = vm; 3205 kasan_populate_early_vm_area_shadow(vm->addr, vm->size); 3206 } 3207 3208 void clear_vm_uninitialized_flag(struct vm_struct *vm) 3209 { 3210 /* 3211 * Before removing VM_UNINITIALIZED, 3212 * we should make sure that vm has proper values. 3213 * Pair with smp_rmb() in vread_iter() and vmalloc_info_show(). 3214 */ 3215 smp_wmb(); 3216 vm->flags &= ~VM_UNINITIALIZED; 3217 } 3218 3219 struct vm_struct *__get_vm_area_node(unsigned long size, 3220 unsigned long align, unsigned long shift, unsigned long flags, 3221 unsigned long start, unsigned long end, int node, 3222 gfp_t gfp_mask, const void *caller) 3223 { 3224 struct vmap_area *va; 3225 struct vm_struct *area; 3226 unsigned long requested_size = size; 3227 3228 BUG_ON(in_nmi() || in_hardirq()); 3229 size = ALIGN(size, 1ul << shift); 3230 if (unlikely(!size)) 3231 return NULL; 3232 3233 if (flags & VM_IOREMAP) 3234 align = 1ul << clamp_t(int, get_count_order_long(size), 3235 PAGE_SHIFT, IOREMAP_MAX_ORDER); 3236 3237 area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node); 3238 if (unlikely(!area)) 3239 return NULL; 3240 3241 if (!(flags & VM_NO_GUARD)) 3242 size += PAGE_SIZE; 3243 3244 area->flags = flags; 3245 area->caller = caller; 3246 area->requested_size = requested_size; 3247 3248 va = alloc_vmap_area(size, align, start, end, node, gfp_mask, 0, area); 3249 if (IS_ERR(va)) { 3250 kfree(area); 3251 return NULL; 3252 } 3253 3254 /* 3255 * Mark pages for non-VM_ALLOC mappings as accessible. Do it now as a 3256 * best-effort approach, as they can be mapped outside of vmalloc code. 3257 * For VM_ALLOC mappings, the pages are marked as accessible after 3258 * getting mapped in __vmalloc_node_range(). 3259 * With hardware tag-based KASAN, marking is skipped for 3260 * non-VM_ALLOC mappings, see __kasan_unpoison_vmalloc(). 3261 */ 3262 if (!(flags & VM_ALLOC)) 3263 area->addr = kasan_unpoison_vmalloc(area->addr, requested_size, 3264 KASAN_VMALLOC_PROT_NORMAL); 3265 3266 return area; 3267 } 3268 3269 struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags, 3270 unsigned long start, unsigned long end, 3271 const void *caller) 3272 { 3273 return __get_vm_area_node(size, 1, PAGE_SHIFT, flags, start, end, 3274 NUMA_NO_NODE, GFP_KERNEL, caller); 3275 } 3276 3277 /** 3278 * get_vm_area - reserve a contiguous kernel virtual area 3279 * @size: size of the area 3280 * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC 3281 * 3282 * Search an area of @size in the kernel virtual mapping area, 3283 * and reserved it for out purposes. Returns the area descriptor 3284 * on success or %NULL on failure. 3285 * 3286 * Return: the area descriptor on success or %NULL on failure. 3287 */ 3288 struct vm_struct *get_vm_area(unsigned long size, unsigned long flags) 3289 { 3290 return __get_vm_area_node(size, 1, PAGE_SHIFT, flags, 3291 VMALLOC_START, VMALLOC_END, 3292 NUMA_NO_NODE, GFP_KERNEL, 3293 __builtin_return_address(0)); 3294 } 3295 3296 struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags, 3297 const void *caller) 3298 { 3299 return __get_vm_area_node(size, 1, PAGE_SHIFT, flags, 3300 VMALLOC_START, VMALLOC_END, 3301 NUMA_NO_NODE, GFP_KERNEL, caller); 3302 } 3303 3304 /** 3305 * find_vm_area - find a continuous kernel virtual area 3306 * @addr: base address 3307 * 3308 * Search for the kernel VM area starting at @addr, and return it. 3309 * It is up to the caller to do all required locking to keep the returned 3310 * pointer valid. 3311 * 3312 * Return: the area descriptor on success or %NULL on failure. 3313 */ 3314 struct vm_struct *find_vm_area(const void *addr) 3315 { 3316 struct vmap_area *va; 3317 3318 va = find_vmap_area((unsigned long)addr); 3319 if (!va) 3320 return NULL; 3321 3322 return va->vm; 3323 } 3324 3325 /** 3326 * remove_vm_area - find and remove a continuous kernel virtual area 3327 * @addr: base address 3328 * 3329 * Search for the kernel VM area starting at @addr, and remove it. 3330 * This function returns the found VM area, but using it is NOT safe 3331 * on SMP machines, except for its size or flags. 3332 * 3333 * Return: the area descriptor on success or %NULL on failure. 3334 */ 3335 struct vm_struct *remove_vm_area(const void *addr) 3336 { 3337 struct vmap_area *va; 3338 struct vm_struct *vm; 3339 3340 might_sleep(); 3341 3342 if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n", 3343 addr)) 3344 return NULL; 3345 3346 va = find_unlink_vmap_area((unsigned long)addr); 3347 if (!va || !va->vm) 3348 return NULL; 3349 vm = va->vm; 3350 3351 debug_check_no_locks_freed(vm->addr, get_vm_area_size(vm)); 3352 debug_check_no_obj_freed(vm->addr, get_vm_area_size(vm)); 3353 kasan_free_module_shadow(vm); 3354 kasan_poison_vmalloc(vm->addr, get_vm_area_size(vm)); 3355 3356 free_unmap_vmap_area(va); 3357 return vm; 3358 } 3359 3360 static inline void set_area_direct_map(const struct vm_struct *area, 3361 int (*set_direct_map)(struct page *page)) 3362 { 3363 int i; 3364 3365 /* HUGE_VMALLOC passes small pages to set_direct_map */ 3366 for (i = 0; i < area->nr_pages; i++) 3367 if (page_address(area->pages[i])) 3368 set_direct_map(area->pages[i]); 3369 } 3370 3371 /* 3372 * Flush the vm mapping and reset the direct map. 3373 */ 3374 static void vm_reset_perms(struct vm_struct *area) 3375 { 3376 unsigned long start = ULONG_MAX, end = 0; 3377 unsigned int page_order = vm_area_page_order(area); 3378 int flush_dmap = 0; 3379 int i; 3380 3381 /* 3382 * Find the start and end range of the direct mappings to make sure that 3383 * the vm_unmap_aliases() flush includes the direct map. 3384 */ 3385 for (i = 0; i < area->nr_pages; i += 1U << page_order) { 3386 unsigned long addr = (unsigned long)page_address(area->pages[i]); 3387 3388 if (addr) { 3389 unsigned long page_size; 3390 3391 page_size = PAGE_SIZE << page_order; 3392 start = min(addr, start); 3393 end = max(addr + page_size, end); 3394 flush_dmap = 1; 3395 } 3396 } 3397 3398 /* 3399 * Set direct map to something invalid so that it won't be cached if 3400 * there are any accesses after the TLB flush, then flush the TLB and 3401 * reset the direct map permissions to the default. 3402 */ 3403 set_area_direct_map(area, set_direct_map_invalid_noflush); 3404 _vm_unmap_aliases(start, end, flush_dmap); 3405 set_area_direct_map(area, set_direct_map_default_noflush); 3406 } 3407 3408 static void delayed_vfree_work(struct work_struct *w) 3409 { 3410 struct vfree_deferred *p = container_of(w, struct vfree_deferred, wq); 3411 struct llist_node *t, *llnode; 3412 3413 llist_for_each_safe(llnode, t, llist_del_all(&p->list)) 3414 vfree(llnode); 3415 } 3416 3417 /** 3418 * vfree_atomic - release memory allocated by vmalloc() 3419 * @addr: memory base address 3420 * 3421 * This one is just like vfree() but can be called in any atomic context 3422 * except NMIs. 3423 */ 3424 void vfree_atomic(const void *addr) 3425 { 3426 struct vfree_deferred *p = raw_cpu_ptr(&vfree_deferred); 3427 3428 BUG_ON(in_nmi()); 3429 kmemleak_free(addr); 3430 3431 /* 3432 * Use raw_cpu_ptr() because this can be called from preemptible 3433 * context. Preemption is absolutely fine here, because the llist_add() 3434 * implementation is lockless, so it works even if we are adding to 3435 * another cpu's list. schedule_work() should be fine with this too. 3436 */ 3437 if (addr && llist_add((struct llist_node *)addr, &p->list)) 3438 schedule_work(&p->wq); 3439 } 3440 3441 /* 3442 * vm_area_free_pages - free a range of pages from a vmalloc allocation 3443 * @vm: the vm_struct containing the pages 3444 * @start_idx: first page index to free (inclusive) 3445 * @end_idx: last page index to free (exclusive) 3446 * 3447 * Free pages [start_idx, end_idx) updating NR_VMALLOC stat accounting. 3448 * Freed vm->pages[] entries are set to NULL. 3449 * Caller is responsible for unmapping (vunmap_range) and KASAN 3450 * poisoning before calling this. 3451 */ 3452 static void vm_area_free_pages(struct vm_struct *vm, unsigned int start_idx, 3453 unsigned int end_idx) 3454 { 3455 unsigned int i; 3456 3457 if (!(vm->flags & VM_MAP_PUT_PAGES)) { 3458 for (i = start_idx; i < end_idx; i++) 3459 mod_lruvec_page_state(vm->pages[i], NR_VMALLOC, -1); 3460 } 3461 free_pages_bulk(vm->pages + start_idx, end_idx - start_idx); 3462 3463 for (i = start_idx; i < end_idx; i++) 3464 vm->pages[i] = NULL; 3465 } 3466 3467 /** 3468 * vfree - Release memory allocated by vmalloc() 3469 * @addr: Memory base address 3470 * 3471 * Free the virtually continuous memory area starting at @addr, as obtained 3472 * from one of the vmalloc() family of APIs. This will usually also free the 3473 * physical memory underlying the virtual allocation, but that memory is 3474 * reference counted, so it will not be freed until the last user goes away. 3475 * 3476 * If @addr is NULL, no operation is performed. 3477 * 3478 * Context: 3479 * May sleep if called *not* from interrupt context. 3480 * Must not be called in NMI context (strictly speaking, it could be 3481 * if we have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling 3482 * conventions for vfree() arch-dependent would be a really bad idea). 3483 */ 3484 void vfree(const void *addr) 3485 { 3486 struct vm_struct *vm; 3487 3488 if (unlikely(in_interrupt())) { 3489 vfree_atomic(addr); 3490 return; 3491 } 3492 3493 BUG_ON(in_nmi()); 3494 kmemleak_free(addr); 3495 might_sleep(); 3496 3497 if (!addr) 3498 return; 3499 3500 vm = remove_vm_area(addr); 3501 if (unlikely(!vm)) { 3502 WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n", 3503 addr); 3504 return; 3505 } 3506 3507 if (unlikely(vm->flags & VM_FLUSH_RESET_PERMS)) 3508 vm_reset_perms(vm); 3509 3510 vm_area_free_pages(vm, 0, vm->nr_pages); 3511 kvfree(vm->pages); 3512 kfree(vm); 3513 } 3514 EXPORT_SYMBOL(vfree); 3515 3516 /** 3517 * vunmap - release virtual mapping obtained by vmap() 3518 * @addr: memory base address 3519 * 3520 * Free the virtually contiguous memory area starting at @addr, 3521 * which was created from the page array passed to vmap(). 3522 * 3523 * Must not be called in interrupt context. 3524 */ 3525 void vunmap(const void *addr) 3526 { 3527 struct vm_struct *vm; 3528 3529 BUG_ON(in_interrupt()); 3530 might_sleep(); 3531 3532 if (!addr) 3533 return; 3534 vm = remove_vm_area(addr); 3535 if (unlikely(!vm)) { 3536 WARN(1, KERN_ERR "Trying to vunmap() nonexistent vm area (%p)\n", 3537 addr); 3538 return; 3539 } 3540 kfree(vm); 3541 } 3542 EXPORT_SYMBOL(vunmap); 3543 3544 /** 3545 * vmap - map an array of pages into virtually contiguous space 3546 * @pages: array of page pointers 3547 * @count: number of pages to map 3548 * @flags: vm_area->flags 3549 * @prot: page protection for the mapping 3550 * 3551 * Maps @count pages from @pages into contiguous kernel virtual space. 3552 * If @flags contains %VM_MAP_PUT_PAGES the ownership of the pages array itself 3553 * (which must be kmalloc or vmalloc memory) and one reference per pages in it 3554 * are transferred from the caller to vmap(), and will be freed / dropped when 3555 * vfree() is called on the return value. 3556 * 3557 * Return: the address of the area or %NULL on failure 3558 */ 3559 void *vmap(struct page **pages, unsigned int count, 3560 unsigned long flags, pgprot_t prot) 3561 { 3562 struct vm_struct *area; 3563 unsigned long addr; 3564 unsigned long size; /* In bytes */ 3565 3566 might_sleep(); 3567 3568 if (WARN_ON_ONCE(flags & VM_FLUSH_RESET_PERMS)) 3569 return NULL; 3570 3571 /* 3572 * Your top guard is someone else's bottom guard. Not having a top 3573 * guard compromises someone else's mappings too. 3574 */ 3575 if (WARN_ON_ONCE(flags & VM_NO_GUARD)) 3576 flags &= ~VM_NO_GUARD; 3577 3578 if (count > totalram_pages()) 3579 return NULL; 3580 3581 size = (unsigned long)count << PAGE_SHIFT; 3582 area = get_vm_area_caller(size, flags, __builtin_return_address(0)); 3583 if (!area) 3584 return NULL; 3585 3586 addr = (unsigned long)area->addr; 3587 if (vmap_pages_range(addr, addr + size, pgprot_nx(prot), 3588 pages, PAGE_SHIFT) < 0) { 3589 vunmap(area->addr); 3590 return NULL; 3591 } 3592 3593 if (flags & VM_MAP_PUT_PAGES) { 3594 area->pages = pages; 3595 area->nr_pages = count; 3596 } 3597 return area->addr; 3598 } 3599 EXPORT_SYMBOL(vmap); 3600 3601 #ifdef CONFIG_VMAP_PFN 3602 struct vmap_pfn_data { 3603 unsigned long *pfns; 3604 pgprot_t prot; 3605 unsigned int idx; 3606 }; 3607 3608 static int vmap_pfn_apply(pte_t *pte, unsigned long addr, void *private) 3609 { 3610 struct vmap_pfn_data *data = private; 3611 unsigned long pfn = data->pfns[data->idx]; 3612 pte_t ptent; 3613 3614 if (WARN_ON_ONCE(pfn_valid(pfn))) 3615 return -EINVAL; 3616 3617 ptent = pte_mkspecial(pfn_pte(pfn, data->prot)); 3618 set_pte_at(&init_mm, addr, pte, ptent); 3619 3620 data->idx++; 3621 return 0; 3622 } 3623 3624 /** 3625 * vmap_pfn - map an array of PFNs into virtually contiguous space 3626 * @pfns: array of PFNs 3627 * @count: number of pages to map 3628 * @prot: page protection for the mapping 3629 * 3630 * Maps @count PFNs from @pfns into contiguous kernel virtual space and returns 3631 * the start address of the mapping. 3632 */ 3633 void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot) 3634 { 3635 struct vmap_pfn_data data = { .pfns = pfns, .prot = pgprot_nx(prot) }; 3636 struct vm_struct *area; 3637 3638 area = get_vm_area_caller(count * PAGE_SIZE, VM_IOREMAP, 3639 __builtin_return_address(0)); 3640 if (!area) 3641 return NULL; 3642 if (apply_to_page_range(&init_mm, (unsigned long)area->addr, 3643 count * PAGE_SIZE, vmap_pfn_apply, &data)) { 3644 free_vm_area(area); 3645 return NULL; 3646 } 3647 3648 flush_cache_vmap((unsigned long)area->addr, 3649 (unsigned long)area->addr + count * PAGE_SIZE); 3650 3651 return area->addr; 3652 } 3653 EXPORT_SYMBOL_GPL(vmap_pfn); 3654 #endif /* CONFIG_VMAP_PFN */ 3655 3656 /* 3657 * Helper for vmalloc to adjust the gfp flags for certain allocations. 3658 */ 3659 static inline gfp_t vmalloc_gfp_adjust(gfp_t flags, const bool large) 3660 { 3661 flags |= __GFP_NOWARN; 3662 if (large) 3663 flags &= ~__GFP_NOFAIL; 3664 return flags; 3665 } 3666 3667 static inline unsigned int 3668 vm_area_alloc_pages(gfp_t gfp, int nid, 3669 unsigned int order, unsigned int nr_pages, struct page **pages) 3670 { 3671 unsigned int nr_allocated = 0; 3672 unsigned int nr_remaining = nr_pages; 3673 unsigned int max_attempt_order = MAX_PAGE_ORDER; 3674 struct page *page; 3675 int i; 3676 unsigned int large_order = ilog2(nr_remaining); 3677 gfp_t large_gfp = vmalloc_gfp_adjust(gfp, large_order) & ~__GFP_DIRECT_RECLAIM; 3678 3679 large_order = min(max_attempt_order, large_order); 3680 3681 /* 3682 * Initially, attempt to have the page allocator give us large order 3683 * pages. Do not attempt allocating smaller than order chunks since 3684 * __vmap_pages_range() expects physically contigous pages of exactly 3685 * order long chunks. 3686 */ 3687 while (large_order > order && nr_remaining) { 3688 if (nid == NUMA_NO_NODE) 3689 page = alloc_pages_noprof(large_gfp, large_order); 3690 else 3691 page = alloc_pages_node_noprof(nid, large_gfp, large_order); 3692 3693 if (unlikely(!page)) { 3694 max_attempt_order = --large_order; 3695 continue; 3696 } 3697 3698 mod_lruvec_page_state(page, NR_VMALLOC, 1 << large_order); 3699 3700 split_page(page, large_order); 3701 for (i = 0; i < (1U << large_order); i++) 3702 pages[nr_allocated + i] = page + i; 3703 3704 nr_allocated += 1U << large_order; 3705 nr_remaining = nr_pages - nr_allocated; 3706 3707 large_order = ilog2(nr_remaining); 3708 large_order = min(max_attempt_order, large_order); 3709 } 3710 3711 /* 3712 * For order-0 pages we make use of bulk allocator, if 3713 * the page array is partly or not at all populated due 3714 * to fails, fallback to a single page allocator that is 3715 * more permissive. 3716 */ 3717 if (!order) { 3718 while (nr_allocated < nr_pages) { 3719 unsigned int nr, nr_pages_request; 3720 int i; 3721 3722 /* 3723 * A maximum allowed request is hard-coded and is 100 3724 * pages per call. That is done in order to prevent a 3725 * long preemption off scenario in the bulk-allocator 3726 * so the range is [1:100]. 3727 */ 3728 nr_pages_request = min(100U, nr_pages - nr_allocated); 3729 3730 /* memory allocation should consider mempolicy, we can't 3731 * wrongly use nearest node when nid == NUMA_NO_NODE, 3732 * otherwise memory may be allocated in only one node, 3733 * but mempolicy wants to alloc memory by interleaving. 3734 */ 3735 if (IS_ENABLED(CONFIG_NUMA) && nid == NUMA_NO_NODE) 3736 nr = alloc_pages_bulk_mempolicy_noprof(gfp, 3737 nr_pages_request, 3738 pages + nr_allocated); 3739 else 3740 nr = alloc_pages_bulk_node_noprof(gfp, nid, 3741 nr_pages_request, 3742 pages + nr_allocated); 3743 3744 for (i = nr_allocated; i < nr_allocated + nr; i++) 3745 mod_lruvec_page_state(pages[i], NR_VMALLOC, 1); 3746 3747 nr_allocated += nr; 3748 3749 /* 3750 * If zero or pages were obtained partly, 3751 * fallback to a single page allocator. 3752 */ 3753 if (nr != nr_pages_request) 3754 break; 3755 } 3756 } 3757 3758 /* High-order pages or fallback path if "bulk" fails. */ 3759 while (nr_allocated < nr_pages) { 3760 if (!(gfp & __GFP_NOFAIL) && fatal_signal_pending(current)) 3761 break; 3762 3763 if (nid == NUMA_NO_NODE) 3764 page = alloc_pages_noprof(gfp, order); 3765 else 3766 page = alloc_pages_node_noprof(nid, gfp, order); 3767 3768 if (unlikely(!page)) 3769 break; 3770 3771 mod_lruvec_page_state(page, NR_VMALLOC, 1 << order); 3772 3773 /* 3774 * High-order allocations must be able to be treated as 3775 * independent small pages by callers (as they can with 3776 * small-page vmallocs). Some drivers do their own refcounting 3777 * on vmalloc_to_page() pages, some use page->mapping, 3778 * page->lru, etc. 3779 */ 3780 if (order) 3781 split_page(page, order); 3782 3783 /* 3784 * Careful, we allocate and map page-order pages, but 3785 * tracking is done per PAGE_SIZE page so as to keep the 3786 * vm_struct APIs independent of the physical/mapped size. 3787 */ 3788 for (i = 0; i < (1U << order); i++) 3789 pages[nr_allocated + i] = page + i; 3790 3791 nr_allocated += 1U << order; 3792 } 3793 3794 return nr_allocated; 3795 } 3796 3797 static LLIST_HEAD(pending_vm_area_cleanup); 3798 static void cleanup_vm_area_work(struct work_struct *work) 3799 { 3800 struct vm_struct *area, *tmp; 3801 struct llist_node *head; 3802 3803 head = llist_del_all(&pending_vm_area_cleanup); 3804 if (!head) 3805 return; 3806 3807 llist_for_each_entry_safe(area, tmp, head, llnode) { 3808 if (!area->pages) 3809 free_vm_area(area); 3810 else 3811 vfree(area->addr); 3812 } 3813 } 3814 3815 /* 3816 * Helper for __vmalloc_area_node() to defer cleanup 3817 * of partially initialized vm_struct in error paths. 3818 */ 3819 static DECLARE_WORK(cleanup_vm_area, cleanup_vm_area_work); 3820 static void defer_vm_area_cleanup(struct vm_struct *area) 3821 { 3822 if (llist_add(&area->llnode, &pending_vm_area_cleanup)) 3823 schedule_work(&cleanup_vm_area); 3824 } 3825 3826 /* 3827 * Page tables allocations ignore external GFP. Enforces it by 3828 * the memalloc scope API. It is used by vmalloc internals and 3829 * KASAN shadow population only. 3830 * 3831 * GFP to scope mapping: 3832 * 3833 * non-blocking (no __GFP_DIRECT_RECLAIM) - memalloc_noreclaim_save() 3834 * GFP_NOFS - memalloc_nofs_save() 3835 * GFP_NOIO - memalloc_noio_save() 3836 * __GFP_RETRY_MAYFAIL, __GFP_NORETRY - memalloc_noreclaim_save() 3837 * to prevent OOMs 3838 * 3839 * Returns a flag cookie to pair with restore. 3840 */ 3841 unsigned int 3842 memalloc_apply_gfp_scope(gfp_t gfp_mask) 3843 { 3844 unsigned int flags = 0; 3845 3846 if (!gfpflags_allow_blocking(gfp_mask) || 3847 (gfp_mask & (__GFP_RETRY_MAYFAIL | __GFP_NORETRY))) 3848 flags = memalloc_noreclaim_save(); 3849 else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO) 3850 flags = memalloc_nofs_save(); 3851 else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0) 3852 flags = memalloc_noio_save(); 3853 3854 /* 0 - no scope applied. */ 3855 return flags; 3856 } 3857 3858 void 3859 memalloc_restore_scope(unsigned int flags) 3860 { 3861 if (flags) 3862 memalloc_flags_restore(flags); 3863 } 3864 3865 static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, 3866 pgprot_t prot, unsigned int page_shift, 3867 int node) 3868 { 3869 const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO; 3870 bool nofail = gfp_mask & __GFP_NOFAIL; 3871 unsigned long addr = (unsigned long)area->addr; 3872 unsigned long size = get_vm_area_size(area); 3873 unsigned long array_size; 3874 unsigned int nr_small_pages = size >> PAGE_SHIFT; 3875 unsigned int page_order; 3876 unsigned int flags; 3877 int ret; 3878 3879 array_size = (unsigned long)nr_small_pages * sizeof(struct page *); 3880 3881 /* __GFP_NOFAIL and "noblock" flags are mutually exclusive. */ 3882 if (!gfpflags_allow_blocking(gfp_mask)) 3883 nofail = false; 3884 3885 if (!(gfp_mask & (GFP_DMA | GFP_DMA32))) 3886 gfp_mask |= __GFP_HIGHMEM; 3887 3888 /* Please note that the recursion is strictly bounded. */ 3889 if (array_size > PAGE_SIZE) { 3890 area->pages = __vmalloc_node_noprof(array_size, 1, nested_gfp, node, 3891 area->caller); 3892 } else { 3893 area->pages = kmalloc_node_noprof(array_size, nested_gfp, node); 3894 } 3895 3896 if (!area->pages) { 3897 warn_alloc(gfp_mask, NULL, 3898 "vmalloc error: size %lu, failed to allocated page array size %lu", 3899 nr_small_pages * PAGE_SIZE, array_size); 3900 goto fail; 3901 } 3902 3903 set_vm_area_page_order(area, page_shift - PAGE_SHIFT); 3904 page_order = vm_area_page_order(area); 3905 3906 /* 3907 * High-order nofail allocations are really expensive and 3908 * potentially dangerous (pre-mature OOM, disruptive reclaim 3909 * and compaction etc. 3910 * 3911 * Please note, the __vmalloc_node_range_noprof() falls-back 3912 * to order-0 pages if high-order attempt is unsuccessful. 3913 */ 3914 area->nr_pages = vm_area_alloc_pages( 3915 vmalloc_gfp_adjust(gfp_mask, page_order), node, 3916 page_order, nr_small_pages, area->pages); 3917 3918 /* 3919 * If not enough pages were obtained to accomplish an 3920 * allocation request, free them via vfree() if any. 3921 */ 3922 if (area->nr_pages != nr_small_pages) { 3923 /* 3924 * vm_area_alloc_pages() can fail due to insufficient memory but 3925 * also:- 3926 * 3927 * - a pending fatal signal 3928 * - insufficient huge page-order pages 3929 * 3930 * Since we always retry allocations at order-0 in the huge page 3931 * case a warning for either is spurious. 3932 */ 3933 if (!fatal_signal_pending(current) && page_order == 0) 3934 warn_alloc(gfp_mask, NULL, 3935 "vmalloc error: size %lu, failed to allocate pages", 3936 nr_small_pages * PAGE_SIZE); 3937 goto fail; 3938 } 3939 3940 /* 3941 * page tables allocations ignore external gfp mask, enforce it 3942 * by the scope API 3943 */ 3944 flags = memalloc_apply_gfp_scope(gfp_mask); 3945 do { 3946 ret = __vmap_pages_range(addr, addr + size, prot, area->pages, 3947 page_shift, nested_gfp); 3948 if (nofail && (ret < 0)) 3949 schedule_timeout_uninterruptible(1); 3950 } while (nofail && (ret < 0)); 3951 memalloc_restore_scope(flags); 3952 3953 if (ret < 0) { 3954 warn_alloc(gfp_mask, NULL, 3955 "vmalloc error: size %lu, failed to map pages", 3956 area->nr_pages * PAGE_SIZE); 3957 goto fail; 3958 } 3959 3960 return area->addr; 3961 3962 fail: 3963 defer_vm_area_cleanup(area); 3964 return NULL; 3965 } 3966 3967 /* 3968 * See __vmalloc_node_range() for a clear list of supported vmalloc flags. 3969 * This gfp lists all flags currently passed through vmalloc. Currently, 3970 * __GFP_ZERO is used by BPF and __GFP_NORETRY is used by percpu. Both drm 3971 * and BPF also use GFP_USER. Additionally, various users pass 3972 * GFP_KERNEL_ACCOUNT. Xfs uses __GFP_NOLOCKDEP. 3973 */ 3974 #define GFP_VMALLOC_SUPPORTED (GFP_KERNEL | GFP_ATOMIC | GFP_NOWAIT |\ 3975 __GFP_NOFAIL | __GFP_ZERO |\ 3976 __GFP_NORETRY | __GFP_RETRY_MAYFAIL |\ 3977 GFP_NOFS | GFP_NOIO | GFP_KERNEL_ACCOUNT |\ 3978 GFP_USER | __GFP_NOLOCKDEP | __GFP_SKIP_KASAN) 3979 3980 static gfp_t vmalloc_fix_flags(gfp_t flags) 3981 { 3982 gfp_t invalid_mask = flags & ~GFP_VMALLOC_SUPPORTED; 3983 3984 flags &= GFP_VMALLOC_SUPPORTED; 3985 WARN_ONCE(1, "Unexpected gfp: %#x (%pGg). Fixing up to gfp: %#x (%pGg). Fix your code!\n", 3986 invalid_mask, &invalid_mask, flags, &flags); 3987 return flags; 3988 } 3989 3990 /** 3991 * __vmalloc_node_range - allocate virtually contiguous memory 3992 * @size: allocation size 3993 * @align: desired alignment 3994 * @start: vm area range start 3995 * @end: vm area range end 3996 * @gfp_mask: flags for the page level allocator 3997 * @prot: protection mask for the allocated pages 3998 * @vm_flags: additional vm area flags (e.g. %VM_NO_GUARD) 3999 * @node: node to use for allocation or NUMA_NO_NODE 4000 * @caller: caller's return address 4001 * 4002 * Allocate enough pages to cover @size from the page level 4003 * allocator with @gfp_mask flags and map them into contiguous 4004 * virtual range with protection @prot. 4005 * 4006 * Supported GFP classes: %GFP_KERNEL, %GFP_ATOMIC, %GFP_NOWAIT, 4007 * %__GFP_RETRY_MAYFAIL, %__GFP_NORETRY, %GFP_NOFS and %GFP_NOIO. 4008 * Zone modifiers are not supported. 4009 * Please note %GFP_ATOMIC and %GFP_NOWAIT are supported only 4010 * by __vmalloc(). 4011 * 4012 * Retry modifiers: only %__GFP_NOFAIL is fully supported; 4013 * %__GFP_NORETRY and %__GFP_RETRY_MAYFAIL are supported with limitation, 4014 * i.e. page tables are allocated with NOWAIT semantic so they might fail 4015 * under moderate memory pressure. 4016 * 4017 * %__GFP_NOWARN can be used to suppress failure messages. 4018 * 4019 * %__GFP_SKIP_KASAN can be used to skip unpoisoning of mapped pages 4020 * (when prot=%PAGE_KERNEL). 4021 * 4022 * Can not be called from interrupt nor NMI contexts. 4023 * Return: the address of the area or %NULL on failure 4024 */ 4025 void *__vmalloc_node_range_noprof(unsigned long size, unsigned long align, 4026 unsigned long start, unsigned long end, gfp_t gfp_mask, 4027 pgprot_t prot, unsigned long vm_flags, int node, 4028 const void *caller) 4029 { 4030 struct vm_struct *area; 4031 void *ret; 4032 kasan_vmalloc_flags_t kasan_flags = KASAN_VMALLOC_NONE; 4033 unsigned long original_align = align; 4034 unsigned int shift = PAGE_SHIFT; 4035 bool skip_vmalloc_kasan = kasan_hw_tags_enabled() && (gfp_mask & __GFP_SKIP_KASAN); 4036 4037 if (WARN_ON_ONCE(!size)) 4038 return NULL; 4039 4040 if ((size >> PAGE_SHIFT) > totalram_pages()) { 4041 warn_alloc(gfp_mask, NULL, 4042 "vmalloc error: size %lu, exceeds total pages", 4043 size); 4044 return NULL; 4045 } 4046 4047 if (vmap_allow_huge && (vm_flags & VM_ALLOW_HUGE_VMAP)) { 4048 /* 4049 * Try huge pages. Only try for PAGE_KERNEL allocations, 4050 * others like modules don't yet expect huge pages in 4051 * their allocations due to apply_to_page_range not 4052 * supporting them. 4053 */ 4054 4055 if (arch_vmap_pmd_supported(prot) && size >= PMD_SIZE) 4056 shift = PMD_SHIFT; 4057 else 4058 shift = arch_vmap_pte_supported_shift(size); 4059 4060 align = max(original_align, 1UL << shift); 4061 } 4062 4063 again: 4064 area = __get_vm_area_node(size, align, shift, VM_ALLOC | 4065 VM_UNINITIALIZED | vm_flags, start, end, node, 4066 gfp_mask & ~__GFP_SKIP_KASAN, caller); 4067 if (!area) { 4068 bool nofail = gfp_mask & __GFP_NOFAIL; 4069 warn_alloc(gfp_mask, NULL, 4070 "vmalloc error: size %lu, vm_struct allocation failed%s", 4071 size, (nofail) ? ". Retrying." : ""); 4072 if (nofail) { 4073 schedule_timeout_uninterruptible(1); 4074 goto again; 4075 } 4076 goto fail; 4077 } 4078 4079 /* 4080 * Prepare arguments for __vmalloc_area_node() and 4081 * kasan_unpoison_vmalloc(). 4082 */ 4083 if (pgprot_val(prot) == pgprot_val(PAGE_KERNEL)) { 4084 if (kasan_hw_tags_enabled() && !skip_vmalloc_kasan) { 4085 /* 4086 * Modify protection bits to allow tagging. 4087 * This must be done before mapping. 4088 */ 4089 prot = arch_vmap_pgprot_tagged(prot); 4090 4091 /* 4092 * Skip page_alloc poisoning and zeroing for physical 4093 * pages backing VM_ALLOC mapping. Memory is instead 4094 * poisoned and zeroed by kasan_unpoison_vmalloc(). 4095 */ 4096 gfp_mask |= __GFP_SKIP_KASAN | __GFP_SKIP_ZERO; 4097 } 4098 4099 /* Take note that the mapping is PAGE_KERNEL. */ 4100 kasan_flags |= KASAN_VMALLOC_PROT_NORMAL; 4101 } 4102 4103 /* Allocate physical pages and map them into vmalloc space. */ 4104 ret = __vmalloc_area_node(area, gfp_mask, prot, shift, node); 4105 if (!ret) 4106 goto fail; 4107 4108 /* 4109 * Mark the pages as accessible, now that they are mapped. 4110 * The condition for setting KASAN_VMALLOC_INIT should complement the 4111 * one in post_alloc_hook() with regards to the __GFP_SKIP_ZERO check 4112 * to make sure that memory is initialized under the same conditions. 4113 * Tag-based KASAN modes only assign tags to normal non-executable 4114 * allocations, see __kasan_unpoison_vmalloc(). 4115 */ 4116 kasan_flags |= KASAN_VMALLOC_VM_ALLOC; 4117 if (!want_init_on_free() && want_init_on_alloc(gfp_mask) && 4118 (gfp_mask & __GFP_SKIP_ZERO)) 4119 kasan_flags |= KASAN_VMALLOC_INIT; 4120 /* KASAN_VMALLOC_PROT_NORMAL already set if required. */ 4121 if (!skip_vmalloc_kasan) 4122 area->addr = kasan_unpoison_vmalloc(area->addr, size, kasan_flags); 4123 4124 /* 4125 * In this function, newly allocated vm_struct has VM_UNINITIALIZED 4126 * flag. It means that vm_struct is not fully initialized. 4127 * Now, it is fully initialized, so remove this flag here. 4128 */ 4129 clear_vm_uninitialized_flag(area); 4130 4131 if (!(vm_flags & VM_DEFER_KMEMLEAK)) 4132 kmemleak_vmalloc(area, PAGE_ALIGN(size), gfp_mask); 4133 4134 return area->addr; 4135 4136 fail: 4137 if (shift > PAGE_SHIFT) { 4138 shift = PAGE_SHIFT; 4139 align = original_align; 4140 goto again; 4141 } 4142 4143 return NULL; 4144 } 4145 4146 /** 4147 * __vmalloc_node - allocate virtually contiguous memory 4148 * @size: allocation size 4149 * @align: desired alignment 4150 * @gfp_mask: flags for the page level allocator 4151 * @node: node to use for allocation or NUMA_NO_NODE 4152 * @caller: caller's return address 4153 * 4154 * Allocate enough pages to cover @size from the page level allocator with 4155 * @gfp_mask flags. Map them into contiguous kernel virtual space. 4156 * 4157 * Semantics of @gfp_mask (including reclaim/retry modifiers such as 4158 * __GFP_NOFAIL) are the same as in __vmalloc_node_range_noprof(). 4159 * 4160 * Return: pointer to the allocated memory or %NULL on error 4161 */ 4162 void *__vmalloc_node_noprof(unsigned long size, unsigned long align, 4163 gfp_t gfp_mask, int node, const void *caller) 4164 { 4165 return __vmalloc_node_range_noprof(size, align, VMALLOC_START, VMALLOC_END, 4166 gfp_mask, PAGE_KERNEL, 0, node, caller); 4167 } 4168 /* 4169 * This is only for performance analysis of vmalloc and stress purpose. 4170 * It is required by vmalloc test module, therefore do not use it other 4171 * than that. 4172 */ 4173 #ifdef CONFIG_TEST_VMALLOC_MODULE 4174 EXPORT_SYMBOL_GPL(__vmalloc_node_noprof); 4175 #endif 4176 4177 void *__vmalloc_noprof(unsigned long size, gfp_t gfp_mask) 4178 { 4179 if (unlikely(gfp_mask & ~GFP_VMALLOC_SUPPORTED)) 4180 gfp_mask = vmalloc_fix_flags(gfp_mask); 4181 return __vmalloc_node_noprof(size, 1, gfp_mask, NUMA_NO_NODE, 4182 __builtin_return_address(0)); 4183 } 4184 EXPORT_SYMBOL(__vmalloc_noprof); 4185 4186 /** 4187 * vmalloc - allocate virtually contiguous memory 4188 * @size: allocation size 4189 * 4190 * Allocate enough pages to cover @size from the page level 4191 * allocator and map them into contiguous kernel virtual space. 4192 * 4193 * For tight control over page level allocator and protection flags 4194 * use __vmalloc() instead. 4195 * 4196 * Return: pointer to the allocated memory or %NULL on error 4197 */ 4198 void *vmalloc_noprof(unsigned long size) 4199 { 4200 return __vmalloc_node_noprof(size, 1, GFP_KERNEL, NUMA_NO_NODE, 4201 __builtin_return_address(0)); 4202 } 4203 EXPORT_SYMBOL(vmalloc_noprof); 4204 4205 /** 4206 * vmalloc_huge_node - allocate virtually contiguous memory, allow huge pages 4207 * @size: allocation size 4208 * @gfp_mask: flags for the page level allocator 4209 * @node: node to use for allocation or NUMA_NO_NODE 4210 * 4211 * Allocate enough pages to cover @size from the page level 4212 * allocator and map them into contiguous kernel virtual space. 4213 * If @size is greater than or equal to PMD_SIZE, allow using 4214 * huge pages for the memory 4215 * 4216 * Return: pointer to the allocated memory or %NULL on error 4217 */ 4218 void *vmalloc_huge_node_noprof(unsigned long size, gfp_t gfp_mask, int node) 4219 { 4220 if (unlikely(gfp_mask & ~GFP_VMALLOC_SUPPORTED)) 4221 gfp_mask = vmalloc_fix_flags(gfp_mask); 4222 return __vmalloc_node_range_noprof(size, 1, VMALLOC_START, VMALLOC_END, 4223 gfp_mask, PAGE_KERNEL, VM_ALLOW_HUGE_VMAP, 4224 node, __builtin_return_address(0)); 4225 } 4226 EXPORT_SYMBOL_GPL(vmalloc_huge_node_noprof); 4227 4228 /** 4229 * vzalloc - allocate virtually contiguous memory with zero fill 4230 * @size: allocation size 4231 * 4232 * Allocate enough pages to cover @size from the page level 4233 * allocator and map them into contiguous kernel virtual space. 4234 * The memory allocated is set to zero. 4235 * 4236 * For tight control over page level allocator and protection flags 4237 * use __vmalloc() instead. 4238 * 4239 * Return: pointer to the allocated memory or %NULL on error 4240 */ 4241 void *vzalloc_noprof(unsigned long size) 4242 { 4243 return __vmalloc_node_noprof(size, 1, GFP_KERNEL | __GFP_ZERO, NUMA_NO_NODE, 4244 __builtin_return_address(0)); 4245 } 4246 EXPORT_SYMBOL(vzalloc_noprof); 4247 4248 /** 4249 * vmalloc_user - allocate zeroed virtually contiguous memory for userspace 4250 * @size: allocation size 4251 * 4252 * The resulting memory area is zeroed so it can be mapped to userspace 4253 * without leaking data. 4254 * 4255 * Return: pointer to the allocated memory or %NULL on error 4256 */ 4257 void *vmalloc_user_noprof(unsigned long size) 4258 { 4259 return __vmalloc_node_range_noprof(size, SHMLBA, VMALLOC_START, VMALLOC_END, 4260 GFP_KERNEL | __GFP_ZERO, PAGE_KERNEL, 4261 VM_USERMAP, NUMA_NO_NODE, 4262 __builtin_return_address(0)); 4263 } 4264 EXPORT_SYMBOL(vmalloc_user_noprof); 4265 4266 /** 4267 * vmalloc_node - allocate memory on a specific node 4268 * @size: allocation size 4269 * @node: numa node 4270 * 4271 * Allocate enough pages to cover @size from the page level 4272 * allocator and map them into contiguous kernel virtual space. 4273 * 4274 * For tight control over page level allocator and protection flags 4275 * use __vmalloc() instead. 4276 * 4277 * Return: pointer to the allocated memory or %NULL on error 4278 */ 4279 void *vmalloc_node_noprof(unsigned long size, int node) 4280 { 4281 return __vmalloc_node_noprof(size, 1, GFP_KERNEL, node, 4282 __builtin_return_address(0)); 4283 } 4284 EXPORT_SYMBOL(vmalloc_node_noprof); 4285 4286 /** 4287 * vzalloc_node - allocate memory on a specific node with zero fill 4288 * @size: allocation size 4289 * @node: numa node 4290 * 4291 * Allocate enough pages to cover @size from the page level 4292 * allocator and map them into contiguous kernel virtual space. 4293 * The memory allocated is set to zero. 4294 * 4295 * Return: pointer to the allocated memory or %NULL on error 4296 */ 4297 void *vzalloc_node_noprof(unsigned long size, int node) 4298 { 4299 return __vmalloc_node_noprof(size, 1, GFP_KERNEL | __GFP_ZERO, node, 4300 __builtin_return_address(0)); 4301 } 4302 EXPORT_SYMBOL(vzalloc_node_noprof); 4303 4304 /** 4305 * vrealloc_node_align - reallocate virtually contiguous memory; contents 4306 * remain unchanged 4307 * @p: object to reallocate memory for 4308 * @size: the size to reallocate 4309 * @align: requested alignment 4310 * @flags: the flags for the page level allocator 4311 * @nid: node number of the target node 4312 * 4313 * If @p is %NULL, vrealloc_XXX() behaves exactly like vmalloc_XXX(). If @size 4314 * is 0 and @p is not a %NULL pointer, the object pointed to is freed. 4315 * 4316 * If the caller wants the new memory to be on specific node *only*, 4317 * __GFP_THISNODE flag should be set, otherwise the function will try to avoid 4318 * reallocation and possibly disregard the specified @nid. 4319 * 4320 * If __GFP_ZERO logic is requested, callers must ensure that, starting with the 4321 * initial memory allocation, every subsequent call to this API for the same 4322 * memory allocation is flagged with __GFP_ZERO. Otherwise, it is possible that 4323 * __GFP_ZERO is not fully honored by this API. 4324 * 4325 * Requesting an alignment that is bigger than the alignment of the existing 4326 * allocation will fail. 4327 * 4328 * In any case, the contents of the object pointed to are preserved up to the 4329 * lesser of the new and old sizes. 4330 * 4331 * This function must not be called concurrently with itself or vfree() for the 4332 * same memory allocation. 4333 * 4334 * Return: pointer to the allocated memory; %NULL if @size is zero or in case of 4335 * failure 4336 */ 4337 void *vrealloc_node_align_noprof(const void *p, size_t size, unsigned long align, 4338 gfp_t flags, int nid) 4339 { 4340 struct vm_struct *vm = NULL; 4341 size_t alloced_size = 0; 4342 size_t old_size = 0; 4343 void *n; 4344 4345 if (!size) { 4346 vfree(p); 4347 return NULL; 4348 } 4349 4350 if (p) { 4351 vm = find_vm_area(p); 4352 if (unlikely(!vm)) { 4353 WARN(1, "Trying to vrealloc() nonexistent vm area (%p)\n", p); 4354 return NULL; 4355 } 4356 4357 alloced_size = get_vm_area_size(vm); 4358 old_size = vm->requested_size; 4359 if (WARN(alloced_size < old_size, 4360 "vrealloc() has mismatched area vs requested sizes (%p)\n", p)) 4361 return NULL; 4362 if (WARN(!IS_ALIGNED((unsigned long)p, align), 4363 "will not reallocate with a bigger alignment (0x%lx)\n", align)) 4364 return NULL; 4365 if (unlikely(flags & __GFP_THISNODE) && nid != NUMA_NO_NODE && 4366 nid != page_to_nid(vmalloc_to_page(p))) 4367 goto need_realloc; 4368 } else { 4369 /* 4370 * If p is NULL, vrealloc behaves exactly like vmalloc. 4371 * Skip the shrink and in-place grow paths. 4372 */ 4373 goto need_realloc; 4374 } 4375 4376 if (size <= old_size) { 4377 unsigned int new_nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT; 4378 4379 /* Zero out "freed" memory, potentially for future realloc. */ 4380 if (want_init_on_free() || want_init_on_alloc(flags)) 4381 memset((void *)p + size, 0, old_size - size); 4382 4383 /* 4384 * Free tail pages when shrink crosses a page boundary. 4385 * 4386 * Skip huge page allocations (page_order > 0) as partial 4387 * freeing would require splitting. 4388 * 4389 * Skip VM_FLUSH_RESET_PERMS, as direct-map permissions must 4390 * be reset before pages are returned to the allocator. 4391 * 4392 * Skip VM_USERMAP, as remap_vmalloc_range_partial() validates 4393 * mapping requests against the unchanged vm->size; freeing 4394 * tail pages would cause vmalloc_to_page() to return NULL for 4395 * the unmapped range. 4396 * 4397 * Skip if either GFP_NOFS or GFP_NOIO are used. 4398 * kmemleak_free_part() internally allocates with 4399 * GFP_KERNEL, which could trigger a recursive deadlock 4400 * if we are under filesystem or I/O reclaim. 4401 */ 4402 if (new_nr_pages < vm->nr_pages && !vm_area_page_order(vm) && 4403 !(vm->flags & (VM_FLUSH_RESET_PERMS | VM_USERMAP)) && 4404 gfp_has_io_fs(flags)) { 4405 unsigned long addr = (unsigned long)kasan_reset_tag(p); 4406 unsigned int old_nr_pages = vm->nr_pages; 4407 4408 /* 4409 * Use the node lock to synchronize with concurrent 4410 * readers (vmalloc_info_show). 4411 */ 4412 struct vmap_node *vn = addr_to_node(addr); 4413 4414 spin_lock(&vn->busy.lock); 4415 vm->nr_pages = new_nr_pages; 4416 spin_unlock(&vn->busy.lock); 4417 4418 /* Notify kmemleak of the reduced allocation size before unmapping. */ 4419 kmemleak_free_part( 4420 (void *)addr + ((unsigned long)new_nr_pages 4421 << PAGE_SHIFT), 4422 (unsigned long)(old_nr_pages - new_nr_pages) 4423 << PAGE_SHIFT); 4424 4425 vunmap_range(addr + ((unsigned long)new_nr_pages 4426 << PAGE_SHIFT), 4427 addr + ((unsigned long)old_nr_pages 4428 << PAGE_SHIFT)); 4429 4430 vm_area_free_pages(vm, new_nr_pages, old_nr_pages); 4431 } 4432 vm->requested_size = size; 4433 kasan_vrealloc(p, old_size, size); 4434 return (void *)p; 4435 } 4436 4437 /* 4438 * We already have the bytes available in the allocation; use them. 4439 */ 4440 if (size <= vm->nr_pages << PAGE_SHIFT) { 4441 /* 4442 * No need to zero memory here, as unused memory will have 4443 * already been zeroed at initial allocation time or during 4444 * realloc shrink time. 4445 */ 4446 vm->requested_size = size; 4447 kasan_vrealloc(p, old_size, size); 4448 return (void *)p; 4449 } 4450 4451 need_realloc: 4452 /* TODO: Grow the vm_area, i.e. allocate and map additional pages. */ 4453 n = __vmalloc_node_noprof(size, align, flags, nid, __builtin_return_address(0)); 4454 4455 if (!n) 4456 return NULL; 4457 4458 if (p) { 4459 memcpy(n, p, min(size, old_size)); 4460 vfree(p); 4461 } 4462 4463 return n; 4464 } 4465 EXPORT_SYMBOL(vrealloc_node_align_noprof); 4466 4467 #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32) 4468 #define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL) 4469 #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA) 4470 #define GFP_VMALLOC32 (GFP_DMA | GFP_KERNEL) 4471 #else 4472 /* 4473 * 64b systems should always have either DMA or DMA32 zones. For others 4474 * GFP_DMA32 should do the right thing and use the normal zone. 4475 */ 4476 #define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL) 4477 #endif 4478 4479 /** 4480 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable) 4481 * @size: allocation size 4482 * 4483 * Allocate enough 32bit PA addressable pages to cover @size from the 4484 * page level allocator and map them into contiguous kernel virtual space. 4485 * 4486 * Return: pointer to the allocated memory or %NULL on error 4487 */ 4488 void *vmalloc_32_noprof(unsigned long size) 4489 { 4490 return __vmalloc_node_noprof(size, 1, GFP_VMALLOC32, NUMA_NO_NODE, 4491 __builtin_return_address(0)); 4492 } 4493 EXPORT_SYMBOL(vmalloc_32_noprof); 4494 4495 /** 4496 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory 4497 * @size: allocation size 4498 * 4499 * The resulting memory area is 32bit addressable and zeroed so it can be 4500 * mapped to userspace without leaking data. 4501 * 4502 * Return: pointer to the allocated memory or %NULL on error 4503 */ 4504 void *vmalloc_32_user_noprof(unsigned long size) 4505 { 4506 return __vmalloc_node_range_noprof(size, SHMLBA, VMALLOC_START, VMALLOC_END, 4507 GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL, 4508 VM_USERMAP, NUMA_NO_NODE, 4509 __builtin_return_address(0)); 4510 } 4511 EXPORT_SYMBOL(vmalloc_32_user_noprof); 4512 4513 /* 4514 * Atomically zero bytes in the iterator. 4515 * 4516 * Returns the number of zeroed bytes. 4517 */ 4518 static size_t zero_iter(struct iov_iter *iter, size_t count) 4519 { 4520 size_t remains = count; 4521 4522 while (remains > 0) { 4523 size_t num, copied; 4524 4525 num = min_t(size_t, remains, PAGE_SIZE); 4526 copied = copy_page_to_iter_nofault(ZERO_PAGE(0), 0, num, iter); 4527 remains -= copied; 4528 4529 if (copied < num) 4530 break; 4531 } 4532 4533 return count - remains; 4534 } 4535 4536 /* 4537 * small helper routine, copy contents to iter from addr. 4538 * If the page is not present, fill zero. 4539 * 4540 * Returns the number of copied bytes. 4541 */ 4542 static size_t aligned_vread_iter(struct iov_iter *iter, 4543 const char *addr, size_t count) 4544 { 4545 size_t remains = count; 4546 struct page *page; 4547 4548 while (remains > 0) { 4549 unsigned long offset, length; 4550 size_t copied = 0; 4551 4552 offset = offset_in_page(addr); 4553 length = PAGE_SIZE - offset; 4554 if (length > remains) 4555 length = remains; 4556 page = vmalloc_to_page(addr); 4557 /* 4558 * To do safe access to this _mapped_ area, we need lock. But 4559 * adding lock here means that we need to add overhead of 4560 * vmalloc()/vfree() calls for this _debug_ interface, rarely 4561 * used. Instead of that, we'll use an local mapping via 4562 * copy_page_to_iter_nofault() and accept a small overhead in 4563 * this access function. 4564 */ 4565 if (page) 4566 copied = copy_page_to_iter_nofault(page, offset, 4567 length, iter); 4568 else 4569 copied = zero_iter(iter, length); 4570 4571 addr += copied; 4572 remains -= copied; 4573 4574 if (copied != length) 4575 break; 4576 } 4577 4578 return count - remains; 4579 } 4580 4581 /* 4582 * Read from a vm_map_ram region of memory. 4583 * 4584 * Returns the number of copied bytes. 4585 */ 4586 static size_t vmap_ram_vread_iter(struct iov_iter *iter, const char *addr, 4587 size_t count, unsigned long flags) 4588 { 4589 char *start; 4590 struct vmap_block *vb; 4591 struct xarray *xa; 4592 unsigned long offset; 4593 unsigned int rs, re; 4594 size_t remains, n; 4595 4596 /* 4597 * If it's area created by vm_map_ram() interface directly, but 4598 * not further subdividing and delegating management to vmap_block, 4599 * handle it here. 4600 */ 4601 if (!(flags & VMAP_BLOCK)) 4602 return aligned_vread_iter(iter, addr, count); 4603 4604 remains = count; 4605 4606 /* 4607 * Area is split into regions and tracked with vmap_block, read out 4608 * each region and zero fill the hole between regions. 4609 */ 4610 xa = addr_to_vb_xa((unsigned long) addr); 4611 vb = xa_load(xa, addr_to_vb_idx((unsigned long)addr)); 4612 if (!vb) 4613 goto finished_zero; 4614 4615 spin_lock(&vb->lock); 4616 if (bitmap_empty(vb->used_map, VMAP_BBMAP_BITS)) { 4617 spin_unlock(&vb->lock); 4618 goto finished_zero; 4619 } 4620 4621 for_each_set_bitrange(rs, re, vb->used_map, VMAP_BBMAP_BITS) { 4622 size_t copied; 4623 4624 if (remains == 0) 4625 goto finished; 4626 4627 start = vmap_block_vaddr(vb->va->va_start, rs); 4628 4629 if (addr < start) { 4630 size_t to_zero = min_t(size_t, start - addr, remains); 4631 size_t zeroed = zero_iter(iter, to_zero); 4632 4633 addr += zeroed; 4634 remains -= zeroed; 4635 4636 if (remains == 0 || zeroed != to_zero) 4637 goto finished; 4638 } 4639 4640 /*it could start reading from the middle of used region*/ 4641 offset = offset_in_page(addr); 4642 n = ((re - rs + 1) << PAGE_SHIFT) - offset; 4643 if (n > remains) 4644 n = remains; 4645 4646 copied = aligned_vread_iter(iter, start + offset, n); 4647 4648 addr += copied; 4649 remains -= copied; 4650 4651 if (copied != n) 4652 goto finished; 4653 } 4654 4655 spin_unlock(&vb->lock); 4656 4657 finished_zero: 4658 /* zero-fill the left dirty or free regions */ 4659 return count - remains + zero_iter(iter, remains); 4660 finished: 4661 /* We couldn't copy/zero everything */ 4662 spin_unlock(&vb->lock); 4663 return count - remains; 4664 } 4665 4666 /** 4667 * vread_iter() - read vmalloc area in a safe way to an iterator. 4668 * @iter: the iterator to which data should be written. 4669 * @addr: vm address. 4670 * @count: number of bytes to be read. 4671 * 4672 * This function checks that addr is a valid vmalloc'ed area, and 4673 * copies data from that area to a given iterator. If the given memory range 4674 * of [addr...addr+count) includes some valid address, data is copied to 4675 * proper area of @iter. If there are memory holes, they'll be zero-filled. 4676 * IOREMAP area is treated as memory hole and no copy is done. 4677 * 4678 * If [addr...addr+count) doesn't includes any intersects with alive 4679 * vm_struct area, returns 0. 4680 * 4681 * Note: In usual ops, vread_iter() is never necessary because the caller 4682 * should know vmalloc() area is valid and can use memcpy(). 4683 * This is for routines which have to access vmalloc area without 4684 * any information, as /proc/kcore. 4685 * 4686 * Return: number of bytes for which addr and iter should be advanced 4687 * (same number as @count) or %0 if [addr...addr+count) doesn't 4688 * include any intersection with valid vmalloc area 4689 */ 4690 long vread_iter(struct iov_iter *iter, const char *addr, size_t count) 4691 { 4692 struct vmap_node *vn; 4693 struct vmap_area *va; 4694 struct vm_struct *vm; 4695 char *vaddr; 4696 size_t n, size, flags, remains; 4697 unsigned long next; 4698 4699 addr = kasan_reset_tag(addr); 4700 4701 /* Don't allow overflow */ 4702 if ((unsigned long) addr + count < count) 4703 count = -(unsigned long) addr; 4704 4705 remains = count; 4706 4707 vn = find_vmap_area_exceed_addr_lock((unsigned long) addr, &va); 4708 if (!vn) 4709 goto finished_zero; 4710 4711 /* no intersects with alive vmap_area */ 4712 if ((unsigned long)addr + remains <= va->va_start) 4713 goto finished_zero; 4714 4715 do { 4716 size_t copied; 4717 4718 if (remains == 0) 4719 goto finished; 4720 4721 vm = va->vm; 4722 flags = va->flags & VMAP_FLAGS_MASK; 4723 /* 4724 * VMAP_BLOCK indicates a sub-type of vm_map_ram area, need 4725 * be set together with VMAP_RAM. 4726 */ 4727 WARN_ON(flags == VMAP_BLOCK); 4728 4729 if (!vm && !flags) 4730 goto next_va; 4731 4732 if (vm && (vm->flags & VM_UNINITIALIZED)) 4733 goto next_va; 4734 4735 /* Pair with smp_wmb() in clear_vm_uninitialized_flag() */ 4736 smp_rmb(); 4737 4738 vaddr = (char *) va->va_start; 4739 if (vm) 4740 /* 4741 * For VM_ALLOC areas, use nr_pages rather than 4742 * get_vm_area_size() because vrealloc() may shrink 4743 * the mapping without updating area->size. Other 4744 * mapping types (vmap, ioremap) don't set nr_pages. 4745 */ 4746 size = (vm->flags & VM_ALLOC && vm->nr_pages) ? 4747 (vm->nr_pages << PAGE_SHIFT) : 4748 get_vm_area_size(vm); 4749 else 4750 size = va_size(va); 4751 4752 if (addr >= vaddr + size) 4753 goto next_va; 4754 4755 if (addr < vaddr) { 4756 size_t to_zero = min_t(size_t, vaddr - addr, remains); 4757 size_t zeroed = zero_iter(iter, to_zero); 4758 4759 addr += zeroed; 4760 remains -= zeroed; 4761 4762 if (remains == 0 || zeroed != to_zero) 4763 goto finished; 4764 } 4765 4766 n = vaddr + size - addr; 4767 if (n > remains) 4768 n = remains; 4769 4770 if (flags & VMAP_RAM) 4771 copied = vmap_ram_vread_iter(iter, addr, n, flags); 4772 else if (!(vm && (vm->flags & (VM_IOREMAP | VM_SPARSE)))) 4773 copied = aligned_vread_iter(iter, addr, n); 4774 else /* IOREMAP | SPARSE area is treated as memory hole */ 4775 copied = zero_iter(iter, n); 4776 4777 addr += copied; 4778 remains -= copied; 4779 4780 if (copied != n) 4781 goto finished; 4782 4783 next_va: 4784 next = va->va_end; 4785 spin_unlock(&vn->busy.lock); 4786 } while ((vn = find_vmap_area_exceed_addr_lock(next, &va))); 4787 4788 finished_zero: 4789 if (vn) 4790 spin_unlock(&vn->busy.lock); 4791 4792 /* zero-fill memory holes */ 4793 return count - remains + zero_iter(iter, remains); 4794 finished: 4795 /* Nothing remains, or We couldn't copy/zero everything. */ 4796 if (vn) 4797 spin_unlock(&vn->busy.lock); 4798 4799 return count - remains; 4800 } 4801 4802 /** 4803 * remap_vmalloc_range_partial - map vmalloc pages to userspace 4804 * @vma: vma to cover 4805 * @uaddr: target user address to start at 4806 * @kaddr: virtual address of vmalloc kernel memory 4807 * @pgoff: offset from @kaddr to start at 4808 * @size: size of map area 4809 * 4810 * Returns: 0 for success, -Exxx on failure 4811 * 4812 * This function checks that @kaddr is a valid vmalloc'ed area, 4813 * and that it is big enough to cover the range starting at 4814 * @uaddr in @vma. Will return failure if that criteria isn't 4815 * met. 4816 * 4817 * Similar to remap_pfn_range() (see mm/memory.c) 4818 */ 4819 int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr, 4820 void *kaddr, unsigned long pgoff, 4821 unsigned long size) 4822 { 4823 struct vm_struct *area; 4824 unsigned long off; 4825 unsigned long end_index; 4826 4827 if (check_shl_overflow(pgoff, PAGE_SHIFT, &off)) 4828 return -EINVAL; 4829 4830 size = PAGE_ALIGN(size); 4831 4832 if (!PAGE_ALIGNED(uaddr) || !PAGE_ALIGNED(kaddr)) 4833 return -EINVAL; 4834 4835 area = find_vm_area(kaddr); 4836 if (!area) 4837 return -EINVAL; 4838 4839 if (!(area->flags & (VM_USERMAP | VM_DMA_COHERENT))) 4840 return -EINVAL; 4841 4842 if (check_add_overflow(size, off, &end_index) || 4843 end_index > get_vm_area_size(area)) 4844 return -EINVAL; 4845 kaddr += off; 4846 4847 do { 4848 struct page *page = vmalloc_to_page(kaddr); 4849 int ret; 4850 4851 ret = vm_insert_page(vma, uaddr, page); 4852 if (ret) 4853 return ret; 4854 4855 uaddr += PAGE_SIZE; 4856 kaddr += PAGE_SIZE; 4857 size -= PAGE_SIZE; 4858 } while (size > 0); 4859 4860 vm_flags_set(vma, VM_DONTEXPAND | VM_DONTDUMP); 4861 4862 return 0; 4863 } 4864 4865 /** 4866 * remap_vmalloc_range - map vmalloc pages to userspace 4867 * @vma: vma to cover (map full range of vma) 4868 * @addr: vmalloc memory 4869 * @pgoff: number of pages into addr before first page to map 4870 * 4871 * Returns: 0 for success, -Exxx on failure 4872 * 4873 * This function checks that addr is a valid vmalloc'ed area, and 4874 * that it is big enough to cover the vma. Will return failure if 4875 * that criteria isn't met. 4876 * 4877 * Similar to remap_pfn_range() (see mm/memory.c) 4878 */ 4879 int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, 4880 unsigned long pgoff) 4881 { 4882 return remap_vmalloc_range_partial(vma, vma->vm_start, 4883 addr, pgoff, 4884 vma->vm_end - vma->vm_start); 4885 } 4886 EXPORT_SYMBOL(remap_vmalloc_range); 4887 4888 void free_vm_area(struct vm_struct *area) 4889 { 4890 struct vm_struct *ret; 4891 ret = remove_vm_area(area->addr); 4892 BUG_ON(ret != area); 4893 kfree(area); 4894 } 4895 EXPORT_SYMBOL_GPL(free_vm_area); 4896 4897 #ifdef CONFIG_SMP 4898 static struct vmap_area *node_to_va(struct rb_node *n) 4899 { 4900 return rb_entry_safe(n, struct vmap_area, rb_node); 4901 } 4902 4903 /** 4904 * pvm_find_va_enclose_addr - find the vmap_area @addr belongs to 4905 * @addr: target address 4906 * 4907 * Returns: vmap_area if it is found. If there is no such area 4908 * the first highest(reverse order) vmap_area is returned 4909 * i.e. va->va_start < addr && va->va_end < addr or NULL 4910 * if there are no any areas before @addr. 4911 */ 4912 static struct vmap_area * 4913 pvm_find_va_enclose_addr(unsigned long addr) 4914 { 4915 struct vmap_area *va, *tmp; 4916 struct rb_node *n; 4917 4918 n = free_vmap_area_root.rb_node; 4919 va = NULL; 4920 4921 while (n) { 4922 tmp = rb_entry(n, struct vmap_area, rb_node); 4923 if (tmp->va_start <= addr) { 4924 va = tmp; 4925 if (tmp->va_end >= addr) 4926 break; 4927 4928 n = n->rb_right; 4929 } else { 4930 n = n->rb_left; 4931 } 4932 } 4933 4934 return va; 4935 } 4936 4937 /** 4938 * pvm_determine_end_from_reverse - find the highest aligned address 4939 * of free block below VMALLOC_END 4940 * @va: 4941 * in - the VA we start the search(reverse order); 4942 * out - the VA with the highest aligned end address. 4943 * @align: alignment for required highest address 4944 * 4945 * Returns: determined end address within vmap_area 4946 */ 4947 static unsigned long 4948 pvm_determine_end_from_reverse(struct vmap_area **va, unsigned long align) 4949 { 4950 unsigned long vmalloc_end = VMALLOC_END & ~(align - 1); 4951 unsigned long addr; 4952 4953 if (likely(*va)) { 4954 list_for_each_entry_from_reverse((*va), 4955 &free_vmap_area_list, list) { 4956 addr = min((*va)->va_end & ~(align - 1), vmalloc_end); 4957 if ((*va)->va_start < addr) 4958 return addr; 4959 } 4960 } 4961 4962 return 0; 4963 } 4964 4965 /** 4966 * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator 4967 * @offsets: array containing offset of each area 4968 * @sizes: array containing size of each area 4969 * @nr_vms: the number of areas to allocate 4970 * @align: alignment, all entries in @offsets and @sizes must be aligned to this 4971 * 4972 * Returns: kmalloc'd vm_struct pointer array pointing to allocated 4973 * vm_structs on success, %NULL on failure 4974 * 4975 * Percpu allocator wants to use congruent vm areas so that it can 4976 * maintain the offsets among percpu areas. This function allocates 4977 * congruent vmalloc areas for it with GFP_KERNEL. These areas tend to 4978 * be scattered pretty far, distance between two areas easily going up 4979 * to gigabytes. To avoid interacting with regular vmallocs, these 4980 * areas are allocated from top. 4981 * 4982 * Despite its complicated look, this allocator is rather simple. It 4983 * does everything top-down and scans free blocks from the end looking 4984 * for matching base. While scanning, if any of the areas do not fit the 4985 * base address is pulled down to fit the area. Scanning is repeated till 4986 * all the areas fit and then all necessary data structures are inserted 4987 * and the result is returned. 4988 */ 4989 struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets, 4990 const size_t *sizes, int nr_vms, 4991 size_t align) 4992 { 4993 const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align); 4994 const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1); 4995 struct vmap_area **vas, *va; 4996 struct vm_struct **vms; 4997 int area, area2, last_area, term_area; 4998 unsigned long base, start, size, end, last_end, orig_start, orig_end; 4999 bool purged = false; 5000 5001 /* verify parameters and allocate data structures */ 5002 BUG_ON(offset_in_page(align) || !is_power_of_2(align)); 5003 for (last_area = 0, area = 0; area < nr_vms; area++) { 5004 start = offsets[area]; 5005 end = start + sizes[area]; 5006 5007 /* is everything aligned properly? */ 5008 BUG_ON(!IS_ALIGNED(offsets[area], align)); 5009 BUG_ON(!IS_ALIGNED(sizes[area], align)); 5010 5011 /* detect the area with the highest address */ 5012 if (start > offsets[last_area]) 5013 last_area = area; 5014 5015 for (area2 = area + 1; area2 < nr_vms; area2++) { 5016 unsigned long start2 = offsets[area2]; 5017 unsigned long end2 = start2 + sizes[area2]; 5018 5019 BUG_ON(start2 < end && start < end2); 5020 } 5021 } 5022 last_end = offsets[last_area] + sizes[last_area]; 5023 5024 if (vmalloc_end - vmalloc_start < last_end) { 5025 WARN_ON(true); 5026 return NULL; 5027 } 5028 5029 vms = kzalloc_objs(vms[0], nr_vms); 5030 vas = kzalloc_objs(vas[0], nr_vms); 5031 if (!vas || !vms) 5032 goto err_free2; 5033 5034 for (area = 0; area < nr_vms; area++) { 5035 vas[area] = kmem_cache_zalloc(vmap_area_cachep, GFP_KERNEL); 5036 vms[area] = kzalloc_obj(struct vm_struct); 5037 if (!vas[area] || !vms[area]) 5038 goto err_free; 5039 } 5040 retry: 5041 spin_lock(&free_vmap_area_lock); 5042 5043 /* start scanning - we scan from the top, begin with the last area */ 5044 area = term_area = last_area; 5045 start = offsets[area]; 5046 end = start + sizes[area]; 5047 5048 va = pvm_find_va_enclose_addr(vmalloc_end); 5049 base = pvm_determine_end_from_reverse(&va, align) - end; 5050 5051 while (true) { 5052 /* 5053 * base might have underflowed, add last_end before 5054 * comparing. 5055 */ 5056 if (base + last_end < vmalloc_start + last_end) 5057 goto overflow; 5058 5059 /* 5060 * Fitting base has not been found. 5061 */ 5062 if (va == NULL) 5063 goto overflow; 5064 5065 /* 5066 * If required width exceeds current VA block, move 5067 * base downwards and then recheck. 5068 */ 5069 if (base + end > va->va_end) { 5070 base = pvm_determine_end_from_reverse(&va, align) - end; 5071 term_area = area; 5072 continue; 5073 } 5074 5075 /* 5076 * If this VA does not fit, move base downwards and recheck. 5077 */ 5078 if (base + start < va->va_start) { 5079 va = node_to_va(rb_prev(&va->rb_node)); 5080 base = pvm_determine_end_from_reverse(&va, align) - end; 5081 term_area = area; 5082 continue; 5083 } 5084 5085 /* 5086 * This area fits, move on to the previous one. If 5087 * the previous one is the terminal one, we're done. 5088 */ 5089 area = (area + nr_vms - 1) % nr_vms; 5090 if (area == term_area) 5091 break; 5092 5093 start = offsets[area]; 5094 end = start + sizes[area]; 5095 va = pvm_find_va_enclose_addr(base + end); 5096 } 5097 5098 /* we've found a fitting base, insert all va's */ 5099 for (area = 0; area < nr_vms; area++) { 5100 int ret; 5101 5102 start = base + offsets[area]; 5103 size = sizes[area]; 5104 5105 va = pvm_find_va_enclose_addr(start); 5106 if (WARN_ON_ONCE(va == NULL)) 5107 /* It is a BUG(), but trigger recovery instead. */ 5108 goto recovery; 5109 5110 ret = va_clip(&free_vmap_area_root, 5111 &free_vmap_area_list, va, start, size); 5112 if (WARN_ON_ONCE(unlikely(ret))) 5113 /* It is a BUG(), but trigger recovery instead. */ 5114 goto recovery; 5115 5116 /* Allocated area. */ 5117 va = vas[area]; 5118 va->va_start = start; 5119 va->va_end = start + size; 5120 } 5121 5122 spin_unlock(&free_vmap_area_lock); 5123 5124 /* populate the kasan shadow space */ 5125 for (area = 0; area < nr_vms; area++) { 5126 if (kasan_populate_vmalloc(vas[area]->va_start, sizes[area], GFP_KERNEL)) 5127 goto err_free_shadow; 5128 } 5129 5130 /* insert all vm's */ 5131 for (area = 0; area < nr_vms; area++) { 5132 struct vmap_node *vn = addr_to_node(vas[area]->va_start); 5133 5134 spin_lock(&vn->busy.lock); 5135 insert_vmap_area(vas[area], &vn->busy.root, &vn->busy.head); 5136 setup_vmalloc_vm(vms[area], vas[area], VM_ALLOC, 5137 pcpu_get_vm_areas); 5138 spin_unlock(&vn->busy.lock); 5139 } 5140 5141 /* 5142 * Mark allocated areas as accessible. Do it now as a best-effort 5143 * approach, as they can be mapped outside of vmalloc code. 5144 * With hardware tag-based KASAN, marking is skipped for 5145 * non-VM_ALLOC mappings, see __kasan_unpoison_vmalloc(). 5146 */ 5147 kasan_unpoison_vmap_areas(vms, nr_vms, KASAN_VMALLOC_PROT_NORMAL); 5148 5149 kfree(vas); 5150 return vms; 5151 5152 recovery: 5153 /* 5154 * Remove previously allocated areas. There is no 5155 * need in removing these areas from the busy tree, 5156 * because they are inserted only on the final step 5157 * and when pcpu_get_vm_areas() is success. 5158 */ 5159 while (area--) { 5160 orig_start = vas[area]->va_start; 5161 orig_end = vas[area]->va_end; 5162 va = merge_or_add_vmap_area_augment(vas[area], &free_vmap_area_root, 5163 &free_vmap_area_list); 5164 if (va) 5165 kasan_release_vmalloc(orig_start, orig_end, 5166 va->va_start, va->va_end, 5167 KASAN_VMALLOC_PAGE_RANGE | KASAN_VMALLOC_TLB_FLUSH); 5168 vas[area] = NULL; 5169 } 5170 5171 overflow: 5172 spin_unlock(&free_vmap_area_lock); 5173 if (!purged) { 5174 reclaim_and_purge_vmap_areas(); 5175 purged = true; 5176 5177 /* Before "retry", check if we recover. */ 5178 for (area = 0; area < nr_vms; area++) { 5179 if (vas[area]) 5180 continue; 5181 5182 vas[area] = kmem_cache_zalloc( 5183 vmap_area_cachep, GFP_KERNEL); 5184 if (!vas[area]) 5185 goto err_free; 5186 } 5187 5188 goto retry; 5189 } 5190 5191 err_free: 5192 for (area = 0; area < nr_vms; area++) { 5193 if (vas[area]) 5194 kmem_cache_free(vmap_area_cachep, vas[area]); 5195 5196 kfree(vms[area]); 5197 } 5198 err_free2: 5199 kfree(vas); 5200 kfree(vms); 5201 return NULL; 5202 5203 err_free_shadow: 5204 spin_lock(&free_vmap_area_lock); 5205 /* 5206 * We release all the vmalloc shadows, even the ones for regions that 5207 * hadn't been successfully added. This relies on kasan_release_vmalloc 5208 * being able to tolerate this case. 5209 */ 5210 for (area = 0; area < nr_vms; area++) { 5211 orig_start = vas[area]->va_start; 5212 orig_end = vas[area]->va_end; 5213 va = merge_or_add_vmap_area_augment(vas[area], &free_vmap_area_root, 5214 &free_vmap_area_list); 5215 if (va) 5216 kasan_release_vmalloc(orig_start, orig_end, 5217 va->va_start, va->va_end, 5218 KASAN_VMALLOC_PAGE_RANGE | KASAN_VMALLOC_TLB_FLUSH); 5219 vas[area] = NULL; 5220 kfree(vms[area]); 5221 } 5222 spin_unlock(&free_vmap_area_lock); 5223 kfree(vas); 5224 kfree(vms); 5225 return NULL; 5226 } 5227 5228 /** 5229 * pcpu_free_vm_areas - free vmalloc areas for percpu allocator 5230 * @vms: vm_struct pointer array returned by pcpu_get_vm_areas() 5231 * @nr_vms: the number of allocated areas 5232 * 5233 * Free vm_structs and the array allocated by pcpu_get_vm_areas(). 5234 */ 5235 void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms) 5236 { 5237 int i; 5238 5239 for (i = 0; i < nr_vms; i++) 5240 free_vm_area(vms[i]); 5241 kfree(vms); 5242 } 5243 #endif /* CONFIG_SMP */ 5244 5245 #ifdef CONFIG_PRINTK 5246 bool vmalloc_dump_obj(void *object) 5247 { 5248 const void *caller; 5249 struct vm_struct *vm; 5250 struct vmap_area *va; 5251 struct vmap_node *vn; 5252 unsigned long addr; 5253 unsigned int nr_pages; 5254 5255 addr = PAGE_ALIGN((unsigned long) object); 5256 vn = addr_to_node(addr); 5257 5258 if (!spin_trylock(&vn->busy.lock)) 5259 return false; 5260 5261 va = __find_vmap_area(addr, &vn->busy.root); 5262 if (!va || !va->vm) { 5263 spin_unlock(&vn->busy.lock); 5264 return false; 5265 } 5266 5267 vm = va->vm; 5268 addr = (unsigned long) vm->addr; 5269 caller = vm->caller; 5270 nr_pages = vm->nr_pages; 5271 spin_unlock(&vn->busy.lock); 5272 5273 pr_cont(" %u-page vmalloc region starting at %#lx allocated at %pS\n", 5274 nr_pages, addr, caller); 5275 5276 return true; 5277 } 5278 #endif 5279 5280 #ifdef CONFIG_PROC_FS 5281 5282 /* 5283 * Print number of pages allocated on each memory node. 5284 * 5285 * This function can only be called if CONFIG_NUMA is enabled 5286 * and VM_UNINITIALIZED bit in v->flags is disabled. 5287 */ 5288 static void show_numa_info(struct seq_file *m, struct vm_struct *v, 5289 unsigned int *counters) 5290 { 5291 unsigned int nr; 5292 unsigned int step = 1U << vm_area_page_order(v); 5293 5294 if (!counters) 5295 return; 5296 5297 memset(counters, 0, nr_node_ids * sizeof(unsigned int)); 5298 5299 for (nr = 0; nr < v->nr_pages; nr += step) 5300 counters[page_to_nid(v->pages[nr])] += step; 5301 for_each_node_state(nr, N_HIGH_MEMORY) 5302 if (counters[nr]) 5303 seq_printf(m, " N%u=%u", nr, counters[nr]); 5304 } 5305 5306 static void show_purge_info(struct seq_file *m) 5307 { 5308 struct vmap_node *vn; 5309 struct vmap_area *va; 5310 5311 for_each_vmap_node(vn) { 5312 spin_lock(&vn->lazy.lock); 5313 list_for_each_entry(va, &vn->lazy.head, list) { 5314 seq_printf(m, "0x%pK-0x%pK %7ld unpurged vm_area\n", 5315 (void *)va->va_start, (void *)va->va_end, 5316 va_size(va)); 5317 } 5318 spin_unlock(&vn->lazy.lock); 5319 } 5320 } 5321 5322 static int vmalloc_info_show(struct seq_file *m, void *p) 5323 { 5324 struct vmap_node *vn; 5325 struct vmap_area *va; 5326 struct vm_struct *v; 5327 unsigned int *counters; 5328 5329 if (IS_ENABLED(CONFIG_NUMA)) 5330 counters = kmalloc_array(nr_node_ids, sizeof(unsigned int), GFP_KERNEL); 5331 5332 for_each_vmap_node(vn) { 5333 spin_lock(&vn->busy.lock); 5334 list_for_each_entry(va, &vn->busy.head, list) { 5335 if (!va->vm) { 5336 if (va->flags & VMAP_RAM) 5337 seq_printf(m, "0x%pK-0x%pK %7ld vm_map_ram\n", 5338 (void *)va->va_start, (void *)va->va_end, 5339 va_size(va)); 5340 5341 continue; 5342 } 5343 5344 v = va->vm; 5345 if (v->flags & VM_UNINITIALIZED) 5346 continue; 5347 5348 /* Pair with smp_wmb() in clear_vm_uninitialized_flag() */ 5349 smp_rmb(); 5350 5351 seq_printf(m, "0x%pK-0x%pK %7ld", 5352 v->addr, v->addr + v->size, v->size); 5353 5354 if (v->caller) 5355 seq_printf(m, " %pS", v->caller); 5356 5357 if (v->nr_pages) 5358 seq_printf(m, " pages=%d", v->nr_pages); 5359 5360 if (v->phys_addr) 5361 seq_printf(m, " phys=%pa", &v->phys_addr); 5362 5363 if (v->flags & VM_IOREMAP) 5364 seq_puts(m, " ioremap"); 5365 5366 if (v->flags & VM_SPARSE) 5367 seq_puts(m, " sparse"); 5368 5369 if (v->flags & VM_ALLOC) 5370 seq_puts(m, " vmalloc"); 5371 5372 if (v->flags & VM_MAP) 5373 seq_puts(m, " vmap"); 5374 5375 if (v->flags & VM_USERMAP) 5376 seq_puts(m, " user"); 5377 5378 if (v->flags & VM_DMA_COHERENT) 5379 seq_puts(m, " dma-coherent"); 5380 5381 if (is_vmalloc_addr(v->pages)) 5382 seq_puts(m, " vpages"); 5383 5384 if (IS_ENABLED(CONFIG_NUMA)) 5385 show_numa_info(m, v, counters); 5386 5387 seq_putc(m, '\n'); 5388 } 5389 spin_unlock(&vn->busy.lock); 5390 } 5391 5392 /* 5393 * As a final step, dump "unpurged" areas. 5394 */ 5395 show_purge_info(m); 5396 if (IS_ENABLED(CONFIG_NUMA)) 5397 kfree(counters); 5398 return 0; 5399 } 5400 5401 static int __init proc_vmalloc_init(void) 5402 { 5403 proc_create_single("vmallocinfo", 0400, NULL, vmalloc_info_show); 5404 return 0; 5405 } 5406 module_init(proc_vmalloc_init); 5407 5408 #endif 5409 5410 static void __init vmap_init_free_space(void) 5411 { 5412 unsigned long vmap_start = 1; 5413 const unsigned long vmap_end = ULONG_MAX; 5414 struct vmap_area *free; 5415 struct vm_struct *busy; 5416 5417 /* 5418 * B F B B B F 5419 * -|-----|.....|-----|-----|-----|.....|- 5420 * | The KVA space | 5421 * |<--------------------------------->| 5422 */ 5423 for (busy = vmlist; busy; busy = busy->next) { 5424 if ((unsigned long) busy->addr - vmap_start > 0) { 5425 free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT); 5426 if (!WARN_ON_ONCE(!free)) { 5427 free->va_start = vmap_start; 5428 free->va_end = (unsigned long) busy->addr; 5429 5430 insert_vmap_area_augment(free, NULL, 5431 &free_vmap_area_root, 5432 &free_vmap_area_list); 5433 } 5434 } 5435 5436 vmap_start = (unsigned long) busy->addr + busy->size; 5437 } 5438 5439 if (vmap_end - vmap_start > 0) { 5440 free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT); 5441 if (!WARN_ON_ONCE(!free)) { 5442 free->va_start = vmap_start; 5443 free->va_end = vmap_end; 5444 5445 insert_vmap_area_augment(free, NULL, 5446 &free_vmap_area_root, 5447 &free_vmap_area_list); 5448 } 5449 } 5450 } 5451 5452 static void vmap_init_nodes(void) 5453 { 5454 struct vmap_node *vn; 5455 int i; 5456 5457 #if BITS_PER_LONG == 64 5458 /* 5459 * A high threshold of max nodes is fixed and bound to 128, 5460 * thus a scale factor is 1 for systems where number of cores 5461 * are less or equal to specified threshold. 5462 * 5463 * As for NUMA-aware notes. For bigger systems, for example 5464 * NUMA with multi-sockets, where we can end-up with thousands 5465 * of cores in total, a "sub-numa-clustering" should be added. 5466 * 5467 * In this case a NUMA domain is considered as a single entity 5468 * with dedicated sub-nodes in it which describe one group or 5469 * set of cores. Therefore a per-domain purging is supposed to 5470 * be added as well as a per-domain balancing. 5471 */ 5472 int n = clamp_t(unsigned int, num_possible_cpus(), 1, 128); 5473 5474 if (n > 1) { 5475 vn = kmalloc_objs(*vn, n, GFP_NOWAIT); 5476 if (vn) { 5477 /* Node partition is 16 pages. */ 5478 vmap_zone_size = (1 << 4) * PAGE_SIZE; 5479 nr_vmap_nodes = n; 5480 vmap_nodes = vn; 5481 } else { 5482 pr_err("Failed to allocate an array. Disable a node layer\n"); 5483 } 5484 } 5485 #endif 5486 5487 for_each_vmap_node(vn) { 5488 vn->busy.root = RB_ROOT; 5489 INIT_LIST_HEAD(&vn->busy.head); 5490 spin_lock_init(&vn->busy.lock); 5491 5492 vn->lazy.root = RB_ROOT; 5493 INIT_LIST_HEAD(&vn->lazy.head); 5494 spin_lock_init(&vn->lazy.lock); 5495 5496 for (i = 0; i < MAX_VA_SIZE_PAGES; i++) { 5497 INIT_LIST_HEAD(&vn->pool[i].head); 5498 WRITE_ONCE(vn->pool[i].len, 0); 5499 } 5500 5501 spin_lock_init(&vn->pool_lock); 5502 } 5503 } 5504 5505 static unsigned long 5506 vmap_node_shrink_count(struct shrinker *shrink, struct shrink_control *sc) 5507 { 5508 unsigned long count = 0; 5509 struct vmap_node *vn; 5510 int i; 5511 5512 for_each_vmap_node(vn) { 5513 for (i = 0; i < MAX_VA_SIZE_PAGES; i++) 5514 count += READ_ONCE(vn->pool[i].len); 5515 } 5516 5517 return count ? count : SHRINK_EMPTY; 5518 } 5519 5520 static unsigned long 5521 vmap_node_shrink_scan(struct shrinker *shrink, struct shrink_control *sc) 5522 { 5523 struct vmap_node *vn; 5524 5525 guard(mutex)(&vmap_purge_lock); 5526 for_each_vmap_node(vn) 5527 decay_va_pool_node(vn, true); 5528 5529 return SHRINK_STOP; 5530 } 5531 5532 void __init vmalloc_init(void) 5533 { 5534 struct shrinker *vmap_node_shrinker; 5535 struct vmap_area *va; 5536 struct vmap_node *vn; 5537 struct vm_struct *tmp; 5538 int i; 5539 5540 /* 5541 * Create the cache for vmap_area objects. 5542 */ 5543 vmap_area_cachep = KMEM_CACHE(vmap_area, SLAB_PANIC); 5544 5545 for_each_possible_cpu(i) { 5546 struct vmap_block_queue *vbq; 5547 struct vfree_deferred *p; 5548 5549 vbq = &per_cpu(vmap_block_queue, i); 5550 spin_lock_init(&vbq->lock); 5551 INIT_LIST_HEAD(&vbq->free); 5552 p = &per_cpu(vfree_deferred, i); 5553 init_llist_head(&p->list); 5554 INIT_WORK(&p->wq, delayed_vfree_work); 5555 xa_init(&vbq->vmap_blocks); 5556 } 5557 5558 /* 5559 * Setup nodes before importing vmlist. 5560 */ 5561 vmap_init_nodes(); 5562 5563 /* Import existing vmlist entries. */ 5564 for (tmp = vmlist; tmp; tmp = tmp->next) { 5565 va = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT); 5566 if (WARN_ON_ONCE(!va)) 5567 continue; 5568 5569 va->va_start = (unsigned long)tmp->addr; 5570 va->va_end = va->va_start + tmp->size; 5571 va->vm = tmp; 5572 5573 vn = addr_to_node(va->va_start); 5574 insert_vmap_area(va, &vn->busy.root, &vn->busy.head); 5575 } 5576 5577 /* 5578 * Now we can initialize a free vmap space. 5579 */ 5580 vmap_init_free_space(); 5581 vmap_initialized = true; 5582 5583 vmap_node_shrinker = shrinker_alloc(0, "vmap-node"); 5584 if (!vmap_node_shrinker) { 5585 pr_err("Failed to allocate vmap-node shrinker!\n"); 5586 return; 5587 } 5588 5589 vmap_node_shrinker->count_objects = vmap_node_shrink_count; 5590 vmap_node_shrinker->scan_objects = vmap_node_shrink_scan; 5591 shrinker_register(vmap_node_shrinker); 5592 } 5593