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