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