mm/vmalloc.c: add flags to mark vm_map_ram area
[linux-2.6-block.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>
0f616be1 36#include <linux/bitops.h>
68ad4a33 37#include <linux/rbtree_augmented.h>
bdebd6a2 38#include <linux/overflow.h>
c0eb315a 39#include <linux/pgtable.h>
7c0f6ba6 40#include <linux/uaccess.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>
1da177e4 45
cf243da6
URS
46#define CREATE_TRACE_POINTS
47#include <trace/events/vmalloc.h>
48
dd56b046 49#include "internal.h"
2a681cfa 50#include "pgalloc-track.h"
dd56b046 51
82a70ce0
CH
52#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
53static unsigned int __ro_after_init ioremap_max_page_shift = BITS_PER_LONG - 1;
54
55static int __init set_nohugeiomap(char *str)
56{
57 ioremap_max_page_shift = PAGE_SHIFT;
58 return 0;
59}
60early_param("nohugeiomap", set_nohugeiomap);
61#else /* CONFIG_HAVE_ARCH_HUGE_VMAP */
62static const unsigned int ioremap_max_page_shift = PAGE_SHIFT;
63#endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */
64
121e6f32
NP
65#ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
66static bool __ro_after_init vmap_allow_huge = true;
67
68static int __init set_nohugevmalloc(char *str)
69{
70 vmap_allow_huge = false;
71 return 0;
72}
73early_param("nohugevmalloc", set_nohugevmalloc);
74#else /* CONFIG_HAVE_ARCH_HUGE_VMALLOC */
75static const bool vmap_allow_huge = false;
76#endif /* CONFIG_HAVE_ARCH_HUGE_VMALLOC */
77
186525bd
IM
78bool is_vmalloc_addr(const void *x)
79{
4aff1dc4 80 unsigned long addr = (unsigned long)kasan_reset_tag(x);
186525bd
IM
81
82 return addr >= VMALLOC_START && addr < VMALLOC_END;
83}
84EXPORT_SYMBOL(is_vmalloc_addr);
85
32fcfd40
AV
86struct vfree_deferred {
87 struct llist_head list;
88 struct work_struct wq;
89};
90static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred);
91
db64fe02 92/*** Page table manipulation functions ***/
5e9e3d77
NP
93static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
94 phys_addr_t phys_addr, pgprot_t prot,
f7ee1f13 95 unsigned int max_page_shift, pgtbl_mod_mask *mask)
5e9e3d77
NP
96{
97 pte_t *pte;
98 u64 pfn;
f7ee1f13 99 unsigned long size = PAGE_SIZE;
5e9e3d77
NP
100
101 pfn = phys_addr >> PAGE_SHIFT;
102 pte = pte_alloc_kernel_track(pmd, addr, mask);
103 if (!pte)
104 return -ENOMEM;
105 do {
106 BUG_ON(!pte_none(*pte));
f7ee1f13
CL
107
108#ifdef CONFIG_HUGETLB_PAGE
109 size = arch_vmap_pte_range_map_size(addr, end, pfn, max_page_shift);
110 if (size != PAGE_SIZE) {
111 pte_t entry = pfn_pte(pfn, prot);
112
f7ee1f13
CL
113 entry = arch_make_huge_pte(entry, ilog2(size), 0);
114 set_huge_pte_at(&init_mm, addr, pte, entry);
115 pfn += PFN_DOWN(size);
116 continue;
117 }
118#endif
5e9e3d77
NP
119 set_pte_at(&init_mm, addr, pte, pfn_pte(pfn, prot));
120 pfn++;
f7ee1f13 121 } while (pte += PFN_DOWN(size), addr += size, addr != end);
5e9e3d77
NP
122 *mask |= PGTBL_PTE_MODIFIED;
123 return 0;
124}
125
126static int vmap_try_huge_pmd(pmd_t *pmd, unsigned long addr, unsigned long end,
127 phys_addr_t phys_addr, pgprot_t prot,
128 unsigned int max_page_shift)
129{
130 if (max_page_shift < PMD_SHIFT)
131 return 0;
132
133 if (!arch_vmap_pmd_supported(prot))
134 return 0;
135
136 if ((end - addr) != PMD_SIZE)
137 return 0;
138
139 if (!IS_ALIGNED(addr, PMD_SIZE))
140 return 0;
141
142 if (!IS_ALIGNED(phys_addr, PMD_SIZE))
143 return 0;
144
145 if (pmd_present(*pmd) && !pmd_free_pte_page(pmd, addr))
146 return 0;
147
148 return pmd_set_huge(pmd, phys_addr, prot);
149}
150
151static int vmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,
152 phys_addr_t phys_addr, pgprot_t prot,
153 unsigned int max_page_shift, pgtbl_mod_mask *mask)
154{
155 pmd_t *pmd;
156 unsigned long next;
157
158 pmd = pmd_alloc_track(&init_mm, pud, addr, mask);
159 if (!pmd)
160 return -ENOMEM;
161 do {
162 next = pmd_addr_end(addr, end);
163
164 if (vmap_try_huge_pmd(pmd, addr, next, phys_addr, prot,
165 max_page_shift)) {
166 *mask |= PGTBL_PMD_MODIFIED;
167 continue;
168 }
169
f7ee1f13 170 if (vmap_pte_range(pmd, addr, next, phys_addr, prot, max_page_shift, mask))
5e9e3d77
NP
171 return -ENOMEM;
172 } while (pmd++, phys_addr += (next - addr), addr = next, addr != end);
173 return 0;
174}
175
176static int vmap_try_huge_pud(pud_t *pud, unsigned long addr, unsigned long end,
177 phys_addr_t phys_addr, pgprot_t prot,
178 unsigned int max_page_shift)
179{
180 if (max_page_shift < PUD_SHIFT)
181 return 0;
182
183 if (!arch_vmap_pud_supported(prot))
184 return 0;
185
186 if ((end - addr) != PUD_SIZE)
187 return 0;
188
189 if (!IS_ALIGNED(addr, PUD_SIZE))
190 return 0;
191
192 if (!IS_ALIGNED(phys_addr, PUD_SIZE))
193 return 0;
194
195 if (pud_present(*pud) && !pud_free_pmd_page(pud, addr))
196 return 0;
197
198 return pud_set_huge(pud, phys_addr, prot);
199}
200
201static int vmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end,
202 phys_addr_t phys_addr, pgprot_t prot,
203 unsigned int max_page_shift, pgtbl_mod_mask *mask)
204{
205 pud_t *pud;
206 unsigned long next;
207
208 pud = pud_alloc_track(&init_mm, p4d, addr, mask);
209 if (!pud)
210 return -ENOMEM;
211 do {
212 next = pud_addr_end(addr, end);
213
214 if (vmap_try_huge_pud(pud, addr, next, phys_addr, prot,
215 max_page_shift)) {
216 *mask |= PGTBL_PUD_MODIFIED;
217 continue;
218 }
219
220 if (vmap_pmd_range(pud, addr, next, phys_addr, prot,
221 max_page_shift, mask))
222 return -ENOMEM;
223 } while (pud++, phys_addr += (next - addr), addr = next, addr != end);
224 return 0;
225}
226
227static int vmap_try_huge_p4d(p4d_t *p4d, unsigned long addr, unsigned long end,
228 phys_addr_t phys_addr, pgprot_t prot,
229 unsigned int max_page_shift)
230{
231 if (max_page_shift < P4D_SHIFT)
232 return 0;
233
234 if (!arch_vmap_p4d_supported(prot))
235 return 0;
236
237 if ((end - addr) != P4D_SIZE)
238 return 0;
239
240 if (!IS_ALIGNED(addr, P4D_SIZE))
241 return 0;
242
243 if (!IS_ALIGNED(phys_addr, P4D_SIZE))
244 return 0;
245
246 if (p4d_present(*p4d) && !p4d_free_pud_page(p4d, addr))
247 return 0;
248
249 return p4d_set_huge(p4d, phys_addr, prot);
250}
251
252static int vmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,
253 phys_addr_t phys_addr, pgprot_t prot,
254 unsigned int max_page_shift, pgtbl_mod_mask *mask)
255{
256 p4d_t *p4d;
257 unsigned long next;
258
259 p4d = p4d_alloc_track(&init_mm, pgd, addr, mask);
260 if (!p4d)
261 return -ENOMEM;
262 do {
263 next = p4d_addr_end(addr, end);
264
265 if (vmap_try_huge_p4d(p4d, addr, next, phys_addr, prot,
266 max_page_shift)) {
267 *mask |= PGTBL_P4D_MODIFIED;
268 continue;
269 }
270
271 if (vmap_pud_range(p4d, addr, next, phys_addr, prot,
272 max_page_shift, mask))
273 return -ENOMEM;
274 } while (p4d++, phys_addr += (next - addr), addr = next, addr != end);
275 return 0;
276}
277
5d87510d 278static int vmap_range_noflush(unsigned long addr, unsigned long end,
5e9e3d77
NP
279 phys_addr_t phys_addr, pgprot_t prot,
280 unsigned int max_page_shift)
281{
282 pgd_t *pgd;
283 unsigned long start;
284 unsigned long next;
285 int err;
286 pgtbl_mod_mask mask = 0;
287
288 might_sleep();
289 BUG_ON(addr >= end);
290
291 start = addr;
292 pgd = pgd_offset_k(addr);
293 do {
294 next = pgd_addr_end(addr, end);
295 err = vmap_p4d_range(pgd, addr, next, phys_addr, prot,
296 max_page_shift, &mask);
297 if (err)
298 break;
299 } while (pgd++, phys_addr += (next - addr), addr = next, addr != end);
300
5e9e3d77
NP
301 if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
302 arch_sync_kernel_mappings(start, end);
303
304 return err;
305}
b221385b 306
82a70ce0
CH
307int ioremap_page_range(unsigned long addr, unsigned long end,
308 phys_addr_t phys_addr, pgprot_t prot)
5d87510d
NP
309{
310 int err;
311
8491502f 312 err = vmap_range_noflush(addr, end, phys_addr, pgprot_nx(prot),
82a70ce0 313 ioremap_max_page_shift);
5d87510d 314 flush_cache_vmap(addr, end);
b073d7f8
AP
315 if (!err)
316 kmsan_ioremap_page_range(addr, end, phys_addr, prot,
317 ioremap_max_page_shift);
5d87510d
NP
318 return err;
319}
320
2ba3e694
JR
321static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
322 pgtbl_mod_mask *mask)
1da177e4
LT
323{
324 pte_t *pte;
325
326 pte = pte_offset_kernel(pmd, addr);
327 do {
328 pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte);
329 WARN_ON(!pte_none(ptent) && !pte_present(ptent));
330 } while (pte++, addr += PAGE_SIZE, addr != end);
2ba3e694 331 *mask |= PGTBL_PTE_MODIFIED;
1da177e4
LT
332}
333
2ba3e694
JR
334static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,
335 pgtbl_mod_mask *mask)
1da177e4
LT
336{
337 pmd_t *pmd;
338 unsigned long next;
2ba3e694 339 int cleared;
1da177e4
LT
340
341 pmd = pmd_offset(pud, addr);
342 do {
343 next = pmd_addr_end(addr, end);
2ba3e694
JR
344
345 cleared = pmd_clear_huge(pmd);
346 if (cleared || pmd_bad(*pmd))
347 *mask |= PGTBL_PMD_MODIFIED;
348
349 if (cleared)
b9820d8f 350 continue;
1da177e4
LT
351 if (pmd_none_or_clear_bad(pmd))
352 continue;
2ba3e694 353 vunmap_pte_range(pmd, addr, next, mask);
e47110e9
AK
354
355 cond_resched();
1da177e4
LT
356 } while (pmd++, addr = next, addr != end);
357}
358
2ba3e694
JR
359static void vunmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end,
360 pgtbl_mod_mask *mask)
1da177e4
LT
361{
362 pud_t *pud;
363 unsigned long next;
2ba3e694 364 int cleared;
1da177e4 365
c2febafc 366 pud = pud_offset(p4d, addr);
1da177e4
LT
367 do {
368 next = pud_addr_end(addr, end);
2ba3e694
JR
369
370 cleared = pud_clear_huge(pud);
371 if (cleared || pud_bad(*pud))
372 *mask |= PGTBL_PUD_MODIFIED;
373
374 if (cleared)
b9820d8f 375 continue;
1da177e4
LT
376 if (pud_none_or_clear_bad(pud))
377 continue;
2ba3e694 378 vunmap_pmd_range(pud, addr, next, mask);
1da177e4
LT
379 } while (pud++, addr = next, addr != end);
380}
381
2ba3e694
JR
382static void vunmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,
383 pgtbl_mod_mask *mask)
c2febafc
KS
384{
385 p4d_t *p4d;
386 unsigned long next;
387
388 p4d = p4d_offset(pgd, addr);
389 do {
390 next = p4d_addr_end(addr, end);
2ba3e694 391
c8db8c26
L
392 p4d_clear_huge(p4d);
393 if (p4d_bad(*p4d))
2ba3e694
JR
394 *mask |= PGTBL_P4D_MODIFIED;
395
c2febafc
KS
396 if (p4d_none_or_clear_bad(p4d))
397 continue;
2ba3e694 398 vunmap_pud_range(p4d, addr, next, mask);
c2febafc
KS
399 } while (p4d++, addr = next, addr != end);
400}
401
4ad0ae8c
NP
402/*
403 * vunmap_range_noflush is similar to vunmap_range, but does not
404 * flush caches or TLBs.
b521c43f 405 *
4ad0ae8c
NP
406 * The caller is responsible for calling flush_cache_vmap() before calling
407 * this function, and flush_tlb_kernel_range after it has returned
408 * successfully (and before the addresses are expected to cause a page fault
409 * or be re-mapped for something else, if TLB flushes are being delayed or
410 * coalesced).
b521c43f 411 *
4ad0ae8c 412 * This is an internal function only. Do not use outside mm/.
b521c43f 413 */
b073d7f8 414void __vunmap_range_noflush(unsigned long start, unsigned long end)
1da177e4 415{
1da177e4 416 unsigned long next;
b521c43f 417 pgd_t *pgd;
2ba3e694
JR
418 unsigned long addr = start;
419 pgtbl_mod_mask mask = 0;
1da177e4
LT
420
421 BUG_ON(addr >= end);
422 pgd = pgd_offset_k(addr);
1da177e4
LT
423 do {
424 next = pgd_addr_end(addr, end);
2ba3e694
JR
425 if (pgd_bad(*pgd))
426 mask |= PGTBL_PGD_MODIFIED;
1da177e4
LT
427 if (pgd_none_or_clear_bad(pgd))
428 continue;
2ba3e694 429 vunmap_p4d_range(pgd, addr, next, &mask);
1da177e4 430 } while (pgd++, addr = next, addr != end);
2ba3e694
JR
431
432 if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
433 arch_sync_kernel_mappings(start, end);
1da177e4
LT
434}
435
b073d7f8
AP
436void vunmap_range_noflush(unsigned long start, unsigned long end)
437{
438 kmsan_vunmap_range_noflush(start, end);
439 __vunmap_range_noflush(start, end);
440}
441
4ad0ae8c
NP
442/**
443 * vunmap_range - unmap kernel virtual addresses
444 * @addr: start of the VM area to unmap
445 * @end: end of the VM area to unmap (non-inclusive)
446 *
447 * Clears any present PTEs in the virtual address range, flushes TLBs and
448 * caches. Any subsequent access to the address before it has been re-mapped
449 * is a kernel bug.
450 */
451void vunmap_range(unsigned long addr, unsigned long end)
452{
453 flush_cache_vunmap(addr, end);
454 vunmap_range_noflush(addr, end);
455 flush_tlb_kernel_range(addr, end);
456}
457
0a264884 458static int vmap_pages_pte_range(pmd_t *pmd, unsigned long addr,
2ba3e694
JR
459 unsigned long end, pgprot_t prot, struct page **pages, int *nr,
460 pgtbl_mod_mask *mask)
1da177e4
LT
461{
462 pte_t *pte;
463
db64fe02
NP
464 /*
465 * nr is a running index into the array which helps higher level
466 * callers keep track of where we're up to.
467 */
468
2ba3e694 469 pte = pte_alloc_kernel_track(pmd, addr, mask);
1da177e4
LT
470 if (!pte)
471 return -ENOMEM;
472 do {
db64fe02
NP
473 struct page *page = pages[*nr];
474
475 if (WARN_ON(!pte_none(*pte)))
476 return -EBUSY;
477 if (WARN_ON(!page))
1da177e4 478 return -ENOMEM;
4fcdcc12
YN
479 if (WARN_ON(!pfn_valid(page_to_pfn(page))))
480 return -EINVAL;
481
1da177e4 482 set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
db64fe02 483 (*nr)++;
1da177e4 484 } while (pte++, addr += PAGE_SIZE, addr != end);
2ba3e694 485 *mask |= PGTBL_PTE_MODIFIED;
1da177e4
LT
486 return 0;
487}
488
0a264884 489static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr,
2ba3e694
JR
490 unsigned long end, pgprot_t prot, struct page **pages, int *nr,
491 pgtbl_mod_mask *mask)
1da177e4
LT
492{
493 pmd_t *pmd;
494 unsigned long next;
495
2ba3e694 496 pmd = pmd_alloc_track(&init_mm, pud, addr, mask);
1da177e4
LT
497 if (!pmd)
498 return -ENOMEM;
499 do {
500 next = pmd_addr_end(addr, end);
0a264884 501 if (vmap_pages_pte_range(pmd, addr, next, prot, pages, nr, mask))
1da177e4
LT
502 return -ENOMEM;
503 } while (pmd++, addr = next, addr != end);
504 return 0;
505}
506
0a264884 507static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr,
2ba3e694
JR
508 unsigned long end, pgprot_t prot, struct page **pages, int *nr,
509 pgtbl_mod_mask *mask)
1da177e4
LT
510{
511 pud_t *pud;
512 unsigned long next;
513
2ba3e694 514 pud = pud_alloc_track(&init_mm, p4d, addr, mask);
1da177e4
LT
515 if (!pud)
516 return -ENOMEM;
517 do {
518 next = pud_addr_end(addr, end);
0a264884 519 if (vmap_pages_pmd_range(pud, addr, next, prot, pages, nr, mask))
1da177e4
LT
520 return -ENOMEM;
521 } while (pud++, addr = next, addr != end);
522 return 0;
523}
524
0a264884 525static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr,
2ba3e694
JR
526 unsigned long end, pgprot_t prot, struct page **pages, int *nr,
527 pgtbl_mod_mask *mask)
c2febafc
KS
528{
529 p4d_t *p4d;
530 unsigned long next;
531
2ba3e694 532 p4d = p4d_alloc_track(&init_mm, pgd, addr, mask);
c2febafc
KS
533 if (!p4d)
534 return -ENOMEM;
535 do {
536 next = p4d_addr_end(addr, end);
0a264884 537 if (vmap_pages_pud_range(p4d, addr, next, prot, pages, nr, mask))
c2febafc
KS
538 return -ENOMEM;
539 } while (p4d++, addr = next, addr != end);
540 return 0;
541}
542
121e6f32
NP
543static int vmap_small_pages_range_noflush(unsigned long addr, unsigned long end,
544 pgprot_t prot, struct page **pages)
1da177e4 545{
2ba3e694 546 unsigned long start = addr;
b521c43f 547 pgd_t *pgd;
121e6f32 548 unsigned long next;
db64fe02
NP
549 int err = 0;
550 int nr = 0;
2ba3e694 551 pgtbl_mod_mask mask = 0;
1da177e4
LT
552
553 BUG_ON(addr >= end);
554 pgd = pgd_offset_k(addr);
1da177e4
LT
555 do {
556 next = pgd_addr_end(addr, end);
2ba3e694
JR
557 if (pgd_bad(*pgd))
558 mask |= PGTBL_PGD_MODIFIED;
0a264884 559 err = vmap_pages_p4d_range(pgd, addr, next, prot, pages, &nr, &mask);
1da177e4 560 if (err)
bf88c8c8 561 return err;
1da177e4 562 } while (pgd++, addr = next, addr != end);
db64fe02 563
2ba3e694
JR
564 if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
565 arch_sync_kernel_mappings(start, end);
566
60bb4465 567 return 0;
1da177e4
LT
568}
569
b67177ec
NP
570/*
571 * vmap_pages_range_noflush is similar to vmap_pages_range, but does not
572 * flush caches.
573 *
574 * The caller is responsible for calling flush_cache_vmap() after this
575 * function returns successfully and before the addresses are accessed.
576 *
577 * This is an internal function only. Do not use outside mm/.
578 */
b073d7f8 579int __vmap_pages_range_noflush(unsigned long addr, unsigned long end,
121e6f32
NP
580 pgprot_t prot, struct page **pages, unsigned int page_shift)
581{
582 unsigned int i, nr = (end - addr) >> PAGE_SHIFT;
583
584 WARN_ON(page_shift < PAGE_SHIFT);
585
586 if (!IS_ENABLED(CONFIG_HAVE_ARCH_HUGE_VMALLOC) ||
587 page_shift == PAGE_SHIFT)
588 return vmap_small_pages_range_noflush(addr, end, prot, pages);
589
590 for (i = 0; i < nr; i += 1U << (page_shift - PAGE_SHIFT)) {
591 int err;
592
593 err = vmap_range_noflush(addr, addr + (1UL << page_shift),
08262ac5 594 page_to_phys(pages[i]), prot,
121e6f32
NP
595 page_shift);
596 if (err)
597 return err;
598
599 addr += 1UL << page_shift;
600 }
601
602 return 0;
603}
b073d7f8
AP
604
605int vmap_pages_range_noflush(unsigned long addr, unsigned long end,
606 pgprot_t prot, struct page **pages, unsigned int page_shift)
607{
608 kmsan_vmap_pages_range_noflush(addr, end, prot, pages, page_shift);
609 return __vmap_pages_range_noflush(addr, end, prot, pages, page_shift);
610}
121e6f32 611
121e6f32 612/**
b67177ec 613 * vmap_pages_range - map pages to a kernel virtual address
121e6f32 614 * @addr: start of the VM area to map
b67177ec 615 * @end: end of the VM area to map (non-inclusive)
121e6f32 616 * @prot: page protection flags to use
b67177ec
NP
617 * @pages: pages to map (always PAGE_SIZE pages)
618 * @page_shift: maximum shift that the pages may be mapped with, @pages must
619 * be aligned and contiguous up to at least this shift.
121e6f32
NP
620 *
621 * RETURNS:
622 * 0 on success, -errno on failure.
623 */
b67177ec
NP
624static int vmap_pages_range(unsigned long addr, unsigned long end,
625 pgprot_t prot, struct page **pages, unsigned int page_shift)
8fc48985 626{
b67177ec 627 int err;
8fc48985 628
b67177ec
NP
629 err = vmap_pages_range_noflush(addr, end, prot, pages, page_shift);
630 flush_cache_vmap(addr, end);
631 return err;
8fc48985
TH
632}
633
81ac3ad9 634int is_vmalloc_or_module_addr(const void *x)
73bdf0a6
LT
635{
636 /*
ab4f2ee1 637 * ARM, x86-64 and sparc64 put modules in a special place,
73bdf0a6
LT
638 * and fall back on vmalloc() if that fails. Others
639 * just put it in the vmalloc space.
640 */
641#if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
4aff1dc4 642 unsigned long addr = (unsigned long)kasan_reset_tag(x);
73bdf0a6
LT
643 if (addr >= MODULES_VADDR && addr < MODULES_END)
644 return 1;
645#endif
646 return is_vmalloc_addr(x);
647}
648
48667e7a 649/*
c0eb315a
NP
650 * Walk a vmap address to the struct page it maps. Huge vmap mappings will
651 * return the tail page that corresponds to the base page address, which
652 * matches small vmap mappings.
48667e7a 653 */
add688fb 654struct page *vmalloc_to_page(const void *vmalloc_addr)
48667e7a
CL
655{
656 unsigned long addr = (unsigned long) vmalloc_addr;
add688fb 657 struct page *page = NULL;
48667e7a 658 pgd_t *pgd = pgd_offset_k(addr);
c2febafc
KS
659 p4d_t *p4d;
660 pud_t *pud;
661 pmd_t *pmd;
662 pte_t *ptep, pte;
48667e7a 663
7aa413de
IM
664 /*
665 * XXX we might need to change this if we add VIRTUAL_BUG_ON for
666 * architectures that do not vmalloc module space
667 */
73bdf0a6 668 VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr));
59ea7463 669
c2febafc
KS
670 if (pgd_none(*pgd))
671 return NULL;
c0eb315a
NP
672 if (WARN_ON_ONCE(pgd_leaf(*pgd)))
673 return NULL; /* XXX: no allowance for huge pgd */
674 if (WARN_ON_ONCE(pgd_bad(*pgd)))
675 return NULL;
676
c2febafc
KS
677 p4d = p4d_offset(pgd, addr);
678 if (p4d_none(*p4d))
679 return NULL;
c0eb315a
NP
680 if (p4d_leaf(*p4d))
681 return p4d_page(*p4d) + ((addr & ~P4D_MASK) >> PAGE_SHIFT);
682 if (WARN_ON_ONCE(p4d_bad(*p4d)))
683 return NULL;
029c54b0 684
c0eb315a
NP
685 pud = pud_offset(p4d, addr);
686 if (pud_none(*pud))
687 return NULL;
688 if (pud_leaf(*pud))
689 return pud_page(*pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
690 if (WARN_ON_ONCE(pud_bad(*pud)))
c2febafc 691 return NULL;
c0eb315a 692
c2febafc 693 pmd = pmd_offset(pud, addr);
c0eb315a
NP
694 if (pmd_none(*pmd))
695 return NULL;
696 if (pmd_leaf(*pmd))
697 return pmd_page(*pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
698 if (WARN_ON_ONCE(pmd_bad(*pmd)))
c2febafc
KS
699 return NULL;
700
701 ptep = pte_offset_map(pmd, addr);
702 pte = *ptep;
703 if (pte_present(pte))
704 page = pte_page(pte);
705 pte_unmap(ptep);
c0eb315a 706
add688fb 707 return page;
48667e7a 708}
add688fb 709EXPORT_SYMBOL(vmalloc_to_page);
48667e7a
CL
710
711/*
add688fb 712 * Map a vmalloc()-space virtual address to the physical page frame number.
48667e7a 713 */
add688fb 714unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
48667e7a 715{
add688fb 716 return page_to_pfn(vmalloc_to_page(vmalloc_addr));
48667e7a 717}
add688fb 718EXPORT_SYMBOL(vmalloc_to_pfn);
48667e7a 719
db64fe02
NP
720
721/*** Global kva allocator ***/
722
bb850f4d 723#define DEBUG_AUGMENT_PROPAGATE_CHECK 0
a6cf4e0f 724#define DEBUG_AUGMENT_LOWEST_MATCH_CHECK 0
bb850f4d 725
db64fe02 726
db64fe02 727static DEFINE_SPINLOCK(vmap_area_lock);
e36176be 728static DEFINE_SPINLOCK(free_vmap_area_lock);
f1c4069e
JK
729/* Export for kexec only */
730LIST_HEAD(vmap_area_list);
89699605 731static struct rb_root vmap_area_root = RB_ROOT;
68ad4a33 732static bool vmap_initialized __read_mostly;
89699605 733
96e2db45
URS
734static struct rb_root purge_vmap_area_root = RB_ROOT;
735static LIST_HEAD(purge_vmap_area_list);
736static DEFINE_SPINLOCK(purge_vmap_area_lock);
737
68ad4a33
URS
738/*
739 * This kmem_cache is used for vmap_area objects. Instead of
740 * allocating from slab we reuse an object from this cache to
741 * make things faster. Especially in "no edge" splitting of
742 * free block.
743 */
744static struct kmem_cache *vmap_area_cachep;
745
746/*
747 * This linked list is used in pair with free_vmap_area_root.
748 * It gives O(1) access to prev/next to perform fast coalescing.
749 */
750static LIST_HEAD(free_vmap_area_list);
751
752/*
753 * This augment red-black tree represents the free vmap space.
754 * All vmap_area objects in this tree are sorted by va->va_start
755 * address. It is used for allocation and merging when a vmap
756 * object is released.
757 *
758 * Each vmap_area node contains a maximum available free block
759 * of its sub-tree, right or left. Therefore it is possible to
760 * find a lowest match of free area.
761 */
762static struct rb_root free_vmap_area_root = RB_ROOT;
763
82dd23e8
URS
764/*
765 * Preload a CPU with one object for "no edge" split case. The
766 * aim is to get rid of allocations from the atomic context, thus
767 * to use more permissive allocation masks.
768 */
769static DEFINE_PER_CPU(struct vmap_area *, ne_fit_preload_node);
770
68ad4a33
URS
771static __always_inline unsigned long
772va_size(struct vmap_area *va)
773{
774 return (va->va_end - va->va_start);
775}
776
777static __always_inline unsigned long
778get_subtree_max_size(struct rb_node *node)
779{
780 struct vmap_area *va;
781
782 va = rb_entry_safe(node, struct vmap_area, rb_node);
783 return va ? va->subtree_max_size : 0;
784}
89699605 785
315cc066
ML
786RB_DECLARE_CALLBACKS_MAX(static, free_vmap_area_rb_augment_cb,
787 struct vmap_area, rb_node, unsigned long, subtree_max_size, va_size)
68ad4a33
URS
788
789static void purge_vmap_area_lazy(void);
790static BLOCKING_NOTIFIER_HEAD(vmap_notify_list);
690467c8
URS
791static void drain_vmap_area_work(struct work_struct *work);
792static DECLARE_WORK(drain_vmap_work, drain_vmap_area_work);
db64fe02 793
97105f0a
RG
794static atomic_long_t nr_vmalloc_pages;
795
796unsigned long vmalloc_nr_pages(void)
797{
798 return atomic_long_read(&nr_vmalloc_pages);
799}
800
153090f2 801/* Look up the first VA which satisfies addr < va_end, NULL if none. */
f181234a
CW
802static struct vmap_area *find_vmap_area_exceed_addr(unsigned long addr)
803{
804 struct vmap_area *va = NULL;
805 struct rb_node *n = vmap_area_root.rb_node;
806
4aff1dc4
AK
807 addr = (unsigned long)kasan_reset_tag((void *)addr);
808
f181234a
CW
809 while (n) {
810 struct vmap_area *tmp;
811
812 tmp = rb_entry(n, struct vmap_area, rb_node);
813 if (tmp->va_end > addr) {
814 va = tmp;
815 if (tmp->va_start <= addr)
816 break;
817
818 n = n->rb_left;
819 } else
820 n = n->rb_right;
821 }
822
823 return va;
824}
825
899c6efe 826static struct vmap_area *__find_vmap_area(unsigned long addr, struct rb_root *root)
1da177e4 827{
899c6efe 828 struct rb_node *n = root->rb_node;
db64fe02 829
4aff1dc4
AK
830 addr = (unsigned long)kasan_reset_tag((void *)addr);
831
db64fe02
NP
832 while (n) {
833 struct vmap_area *va;
834
835 va = rb_entry(n, struct vmap_area, rb_node);
836 if (addr < va->va_start)
837 n = n->rb_left;
cef2ac3f 838 else if (addr >= va->va_end)
db64fe02
NP
839 n = n->rb_right;
840 else
841 return va;
842 }
843
844 return NULL;
845}
846
68ad4a33
URS
847/*
848 * This function returns back addresses of parent node
849 * and its left or right link for further processing.
9c801f61
URS
850 *
851 * Otherwise NULL is returned. In that case all further
852 * steps regarding inserting of conflicting overlap range
853 * have to be declined and actually considered as a bug.
68ad4a33
URS
854 */
855static __always_inline struct rb_node **
856find_va_links(struct vmap_area *va,
857 struct rb_root *root, struct rb_node *from,
858 struct rb_node **parent)
859{
860 struct vmap_area *tmp_va;
861 struct rb_node **link;
862
863 if (root) {
864 link = &root->rb_node;
865 if (unlikely(!*link)) {
866 *parent = NULL;
867 return link;
868 }
869 } else {
870 link = &from;
871 }
db64fe02 872
68ad4a33
URS
873 /*
874 * Go to the bottom of the tree. When we hit the last point
875 * we end up with parent rb_node and correct direction, i name
876 * it link, where the new va->rb_node will be attached to.
877 */
878 do {
879 tmp_va = rb_entry(*link, struct vmap_area, rb_node);
db64fe02 880
68ad4a33
URS
881 /*
882 * During the traversal we also do some sanity check.
883 * Trigger the BUG() if there are sides(left/right)
884 * or full overlaps.
885 */
753df96b 886 if (va->va_end <= tmp_va->va_start)
68ad4a33 887 link = &(*link)->rb_left;
753df96b 888 else if (va->va_start >= tmp_va->va_end)
68ad4a33 889 link = &(*link)->rb_right;
9c801f61
URS
890 else {
891 WARN(1, "vmalloc bug: 0x%lx-0x%lx overlaps with 0x%lx-0x%lx\n",
892 va->va_start, va->va_end, tmp_va->va_start, tmp_va->va_end);
893
894 return NULL;
895 }
68ad4a33
URS
896 } while (*link);
897
898 *parent = &tmp_va->rb_node;
899 return link;
900}
901
902static __always_inline struct list_head *
903get_va_next_sibling(struct rb_node *parent, struct rb_node **link)
904{
905 struct list_head *list;
906
907 if (unlikely(!parent))
908 /*
909 * The red-black tree where we try to find VA neighbors
910 * before merging or inserting is empty, i.e. it means
911 * there is no free vmap space. Normally it does not
912 * happen but we handle this case anyway.
913 */
914 return NULL;
915
916 list = &rb_entry(parent, struct vmap_area, rb_node)->list;
917 return (&parent->rb_right == link ? list->next : list);
918}
919
920static __always_inline void
8eb510db
URS
921__link_va(struct vmap_area *va, struct rb_root *root,
922 struct rb_node *parent, struct rb_node **link,
923 struct list_head *head, bool augment)
68ad4a33
URS
924{
925 /*
926 * VA is still not in the list, but we can
927 * identify its future previous list_head node.
928 */
929 if (likely(parent)) {
930 head = &rb_entry(parent, struct vmap_area, rb_node)->list;
931 if (&parent->rb_right != link)
932 head = head->prev;
db64fe02
NP
933 }
934
68ad4a33
URS
935 /* Insert to the rb-tree */
936 rb_link_node(&va->rb_node, parent, link);
8eb510db 937 if (augment) {
68ad4a33
URS
938 /*
939 * Some explanation here. Just perform simple insertion
940 * to the tree. We do not set va->subtree_max_size to
941 * its current size before calling rb_insert_augmented().
153090f2 942 * It is because we populate the tree from the bottom
68ad4a33
URS
943 * to parent levels when the node _is_ in the tree.
944 *
945 * Therefore we set subtree_max_size to zero after insertion,
946 * to let __augment_tree_propagate_from() puts everything to
947 * the correct order later on.
948 */
949 rb_insert_augmented(&va->rb_node,
950 root, &free_vmap_area_rb_augment_cb);
951 va->subtree_max_size = 0;
952 } else {
953 rb_insert_color(&va->rb_node, root);
954 }
db64fe02 955
68ad4a33
URS
956 /* Address-sort this list */
957 list_add(&va->list, head);
db64fe02
NP
958}
959
68ad4a33 960static __always_inline void
8eb510db
URS
961link_va(struct vmap_area *va, struct rb_root *root,
962 struct rb_node *parent, struct rb_node **link,
963 struct list_head *head)
964{
965 __link_va(va, root, parent, link, head, false);
966}
967
968static __always_inline void
969link_va_augment(struct vmap_area *va, struct rb_root *root,
970 struct rb_node *parent, struct rb_node **link,
971 struct list_head *head)
972{
973 __link_va(va, root, parent, link, head, true);
974}
975
976static __always_inline void
977__unlink_va(struct vmap_area *va, struct rb_root *root, bool augment)
68ad4a33 978{
460e42d1
URS
979 if (WARN_ON(RB_EMPTY_NODE(&va->rb_node)))
980 return;
db64fe02 981
8eb510db 982 if (augment)
460e42d1
URS
983 rb_erase_augmented(&va->rb_node,
984 root, &free_vmap_area_rb_augment_cb);
985 else
986 rb_erase(&va->rb_node, root);
987
5d7a7c54 988 list_del_init(&va->list);
460e42d1 989 RB_CLEAR_NODE(&va->rb_node);
68ad4a33
URS
990}
991
8eb510db
URS
992static __always_inline void
993unlink_va(struct vmap_area *va, struct rb_root *root)
994{
995 __unlink_va(va, root, false);
996}
997
998static __always_inline void
999unlink_va_augment(struct vmap_area *va, struct rb_root *root)
1000{
1001 __unlink_va(va, root, true);
1002}
1003
bb850f4d 1004#if DEBUG_AUGMENT_PROPAGATE_CHECK
c3385e84
JC
1005/*
1006 * Gets called when remove the node and rotate.
1007 */
1008static __always_inline unsigned long
1009compute_subtree_max_size(struct vmap_area *va)
1010{
1011 return max3(va_size(va),
1012 get_subtree_max_size(va->rb_node.rb_left),
1013 get_subtree_max_size(va->rb_node.rb_right));
1014}
1015
bb850f4d 1016static void
da27c9ed 1017augment_tree_propagate_check(void)
bb850f4d
URS
1018{
1019 struct vmap_area *va;
da27c9ed 1020 unsigned long computed_size;
bb850f4d 1021
da27c9ed
URS
1022 list_for_each_entry(va, &free_vmap_area_list, list) {
1023 computed_size = compute_subtree_max_size(va);
1024 if (computed_size != va->subtree_max_size)
1025 pr_emerg("tree is corrupted: %lu, %lu\n",
1026 va_size(va), va->subtree_max_size);
bb850f4d 1027 }
bb850f4d
URS
1028}
1029#endif
1030
68ad4a33
URS
1031/*
1032 * This function populates subtree_max_size from bottom to upper
1033 * levels starting from VA point. The propagation must be done
1034 * when VA size is modified by changing its va_start/va_end. Or
1035 * in case of newly inserting of VA to the tree.
1036 *
1037 * It means that __augment_tree_propagate_from() must be called:
1038 * - After VA has been inserted to the tree(free path);
1039 * - After VA has been shrunk(allocation path);
1040 * - After VA has been increased(merging path).
1041 *
1042 * Please note that, it does not mean that upper parent nodes
1043 * and their subtree_max_size are recalculated all the time up
1044 * to the root node.
1045 *
1046 * 4--8
1047 * /\
1048 * / \
1049 * / \
1050 * 2--2 8--8
1051 *
1052 * For example if we modify the node 4, shrinking it to 2, then
1053 * no any modification is required. If we shrink the node 2 to 1
1054 * its subtree_max_size is updated only, and set to 1. If we shrink
1055 * the node 8 to 6, then its subtree_max_size is set to 6 and parent
1056 * node becomes 4--6.
1057 */
1058static __always_inline void
1059augment_tree_propagate_from(struct vmap_area *va)
1060{
15ae144f
URS
1061 /*
1062 * Populate the tree from bottom towards the root until
1063 * the calculated maximum available size of checked node
1064 * is equal to its current one.
1065 */
1066 free_vmap_area_rb_augment_cb_propagate(&va->rb_node, NULL);
bb850f4d
URS
1067
1068#if DEBUG_AUGMENT_PROPAGATE_CHECK
da27c9ed 1069 augment_tree_propagate_check();
bb850f4d 1070#endif
68ad4a33
URS
1071}
1072
1073static void
1074insert_vmap_area(struct vmap_area *va,
1075 struct rb_root *root, struct list_head *head)
1076{
1077 struct rb_node **link;
1078 struct rb_node *parent;
1079
1080 link = find_va_links(va, root, NULL, &parent);
9c801f61
URS
1081 if (link)
1082 link_va(va, root, parent, link, head);
68ad4a33
URS
1083}
1084
1085static void
1086insert_vmap_area_augment(struct vmap_area *va,
1087 struct rb_node *from, struct rb_root *root,
1088 struct list_head *head)
1089{
1090 struct rb_node **link;
1091 struct rb_node *parent;
1092
1093 if (from)
1094 link = find_va_links(va, NULL, from, &parent);
1095 else
1096 link = find_va_links(va, root, NULL, &parent);
1097
9c801f61 1098 if (link) {
8eb510db 1099 link_va_augment(va, root, parent, link, head);
9c801f61
URS
1100 augment_tree_propagate_from(va);
1101 }
68ad4a33
URS
1102}
1103
1104/*
1105 * Merge de-allocated chunk of VA memory with previous
1106 * and next free blocks. If coalesce is not done a new
1107 * free area is inserted. If VA has been merged, it is
1108 * freed.
9c801f61
URS
1109 *
1110 * Please note, it can return NULL in case of overlap
1111 * ranges, followed by WARN() report. Despite it is a
1112 * buggy behaviour, a system can be alive and keep
1113 * ongoing.
68ad4a33 1114 */
3c5c3cfb 1115static __always_inline struct vmap_area *
8eb510db
URS
1116__merge_or_add_vmap_area(struct vmap_area *va,
1117 struct rb_root *root, struct list_head *head, bool augment)
68ad4a33
URS
1118{
1119 struct vmap_area *sibling;
1120 struct list_head *next;
1121 struct rb_node **link;
1122 struct rb_node *parent;
1123 bool merged = false;
1124
1125 /*
1126 * Find a place in the tree where VA potentially will be
1127 * inserted, unless it is merged with its sibling/siblings.
1128 */
1129 link = find_va_links(va, root, NULL, &parent);
9c801f61
URS
1130 if (!link)
1131 return NULL;
68ad4a33
URS
1132
1133 /*
1134 * Get next node of VA to check if merging can be done.
1135 */
1136 next = get_va_next_sibling(parent, link);
1137 if (unlikely(next == NULL))
1138 goto insert;
1139
1140 /*
1141 * start end
1142 * | |
1143 * |<------VA------>|<-----Next----->|
1144 * | |
1145 * start end
1146 */
1147 if (next != head) {
1148 sibling = list_entry(next, struct vmap_area, list);
1149 if (sibling->va_start == va->va_end) {
1150 sibling->va_start = va->va_start;
1151
68ad4a33
URS
1152 /* Free vmap_area object. */
1153 kmem_cache_free(vmap_area_cachep, va);
1154
1155 /* Point to the new merged area. */
1156 va = sibling;
1157 merged = true;
1158 }
1159 }
1160
1161 /*
1162 * start end
1163 * | |
1164 * |<-----Prev----->|<------VA------>|
1165 * | |
1166 * start end
1167 */
1168 if (next->prev != head) {
1169 sibling = list_entry(next->prev, struct vmap_area, list);
1170 if (sibling->va_end == va->va_start) {
5dd78640
URS
1171 /*
1172 * If both neighbors are coalesced, it is important
1173 * to unlink the "next" node first, followed by merging
1174 * with "previous" one. Otherwise the tree might not be
1175 * fully populated if a sibling's augmented value is
1176 * "normalized" because of rotation operations.
1177 */
54f63d9d 1178 if (merged)
8eb510db 1179 __unlink_va(va, root, augment);
68ad4a33 1180
5dd78640
URS
1181 sibling->va_end = va->va_end;
1182
68ad4a33
URS
1183 /* Free vmap_area object. */
1184 kmem_cache_free(vmap_area_cachep, va);
3c5c3cfb
DA
1185
1186 /* Point to the new merged area. */
1187 va = sibling;
1188 merged = true;
68ad4a33
URS
1189 }
1190 }
1191
1192insert:
5dd78640 1193 if (!merged)
8eb510db 1194 __link_va(va, root, parent, link, head, augment);
3c5c3cfb 1195
96e2db45
URS
1196 return va;
1197}
1198
8eb510db
URS
1199static __always_inline struct vmap_area *
1200merge_or_add_vmap_area(struct vmap_area *va,
1201 struct rb_root *root, struct list_head *head)
1202{
1203 return __merge_or_add_vmap_area(va, root, head, false);
1204}
1205
96e2db45
URS
1206static __always_inline struct vmap_area *
1207merge_or_add_vmap_area_augment(struct vmap_area *va,
1208 struct rb_root *root, struct list_head *head)
1209{
8eb510db 1210 va = __merge_or_add_vmap_area(va, root, head, true);
96e2db45
URS
1211 if (va)
1212 augment_tree_propagate_from(va);
1213
3c5c3cfb 1214 return va;
68ad4a33
URS
1215}
1216
1217static __always_inline bool
1218is_within_this_va(struct vmap_area *va, unsigned long size,
1219 unsigned long align, unsigned long vstart)
1220{
1221 unsigned long nva_start_addr;
1222
1223 if (va->va_start > vstart)
1224 nva_start_addr = ALIGN(va->va_start, align);
1225 else
1226 nva_start_addr = ALIGN(vstart, align);
1227
1228 /* Can be overflowed due to big size or alignment. */
1229 if (nva_start_addr + size < nva_start_addr ||
1230 nva_start_addr < vstart)
1231 return false;
1232
1233 return (nva_start_addr + size <= va->va_end);
1234}
1235
1236/*
1237 * Find the first free block(lowest start address) in the tree,
1238 * that will accomplish the request corresponding to passing
9333fe98
UR
1239 * parameters. Please note, with an alignment bigger than PAGE_SIZE,
1240 * a search length is adjusted to account for worst case alignment
1241 * overhead.
68ad4a33
URS
1242 */
1243static __always_inline struct vmap_area *
f9863be4
URS
1244find_vmap_lowest_match(struct rb_root *root, unsigned long size,
1245 unsigned long align, unsigned long vstart, bool adjust_search_size)
68ad4a33
URS
1246{
1247 struct vmap_area *va;
1248 struct rb_node *node;
9333fe98 1249 unsigned long length;
68ad4a33
URS
1250
1251 /* Start from the root. */
f9863be4 1252 node = root->rb_node;
68ad4a33 1253
9333fe98
UR
1254 /* Adjust the search size for alignment overhead. */
1255 length = adjust_search_size ? size + align - 1 : size;
1256
68ad4a33
URS
1257 while (node) {
1258 va = rb_entry(node, struct vmap_area, rb_node);
1259
9333fe98 1260 if (get_subtree_max_size(node->rb_left) >= length &&
68ad4a33
URS
1261 vstart < va->va_start) {
1262 node = node->rb_left;
1263 } else {
1264 if (is_within_this_va(va, size, align, vstart))
1265 return va;
1266
1267 /*
1268 * Does not make sense to go deeper towards the right
1269 * sub-tree if it does not have a free block that is
9333fe98 1270 * equal or bigger to the requested search length.
68ad4a33 1271 */
9333fe98 1272 if (get_subtree_max_size(node->rb_right) >= length) {
68ad4a33
URS
1273 node = node->rb_right;
1274 continue;
1275 }
1276
1277 /*
3806b041 1278 * OK. We roll back and find the first right sub-tree,
68ad4a33 1279 * that will satisfy the search criteria. It can happen
9f531973
URS
1280 * due to "vstart" restriction or an alignment overhead
1281 * that is bigger then PAGE_SIZE.
68ad4a33
URS
1282 */
1283 while ((node = rb_parent(node))) {
1284 va = rb_entry(node, struct vmap_area, rb_node);
1285 if (is_within_this_va(va, size, align, vstart))
1286 return va;
1287
9333fe98 1288 if (get_subtree_max_size(node->rb_right) >= length &&
68ad4a33 1289 vstart <= va->va_start) {
9f531973
URS
1290 /*
1291 * Shift the vstart forward. Please note, we update it with
1292 * parent's start address adding "1" because we do not want
1293 * to enter same sub-tree after it has already been checked
1294 * and no suitable free block found there.
1295 */
1296 vstart = va->va_start + 1;
68ad4a33
URS
1297 node = node->rb_right;
1298 break;
1299 }
1300 }
1301 }
1302 }
1303
1304 return NULL;
1305}
1306
a6cf4e0f
URS
1307#if DEBUG_AUGMENT_LOWEST_MATCH_CHECK
1308#include <linux/random.h>
1309
1310static struct vmap_area *
bd1264c3 1311find_vmap_lowest_linear_match(struct list_head *head, unsigned long size,
a6cf4e0f
URS
1312 unsigned long align, unsigned long vstart)
1313{
1314 struct vmap_area *va;
1315
bd1264c3 1316 list_for_each_entry(va, head, list) {
a6cf4e0f
URS
1317 if (!is_within_this_va(va, size, align, vstart))
1318 continue;
1319
1320 return va;
1321 }
1322
1323 return NULL;
1324}
1325
1326static void
bd1264c3
SL
1327find_vmap_lowest_match_check(struct rb_root *root, struct list_head *head,
1328 unsigned long size, unsigned long align)
a6cf4e0f
URS
1329{
1330 struct vmap_area *va_1, *va_2;
1331 unsigned long vstart;
1332 unsigned int rnd;
1333
1334 get_random_bytes(&rnd, sizeof(rnd));
1335 vstart = VMALLOC_START + rnd;
1336
bd1264c3
SL
1337 va_1 = find_vmap_lowest_match(root, size, align, vstart, false);
1338 va_2 = find_vmap_lowest_linear_match(head, size, align, vstart);
a6cf4e0f
URS
1339
1340 if (va_1 != va_2)
1341 pr_emerg("not lowest: t: 0x%p, l: 0x%p, v: 0x%lx\n",
1342 va_1, va_2, vstart);
1343}
1344#endif
1345
68ad4a33
URS
1346enum fit_type {
1347 NOTHING_FIT = 0,
1348 FL_FIT_TYPE = 1, /* full fit */
1349 LE_FIT_TYPE = 2, /* left edge fit */
1350 RE_FIT_TYPE = 3, /* right edge fit */
1351 NE_FIT_TYPE = 4 /* no edge fit */
1352};
1353
1354static __always_inline enum fit_type
1355classify_va_fit_type(struct vmap_area *va,
1356 unsigned long nva_start_addr, unsigned long size)
1357{
1358 enum fit_type type;
1359
1360 /* Check if it is within VA. */
1361 if (nva_start_addr < va->va_start ||
1362 nva_start_addr + size > va->va_end)
1363 return NOTHING_FIT;
1364
1365 /* Now classify. */
1366 if (va->va_start == nva_start_addr) {
1367 if (va->va_end == nva_start_addr + size)
1368 type = FL_FIT_TYPE;
1369 else
1370 type = LE_FIT_TYPE;
1371 } else if (va->va_end == nva_start_addr + size) {
1372 type = RE_FIT_TYPE;
1373 } else {
1374 type = NE_FIT_TYPE;
1375 }
1376
1377 return type;
1378}
1379
1380static __always_inline int
f9863be4
URS
1381adjust_va_to_fit_type(struct rb_root *root, struct list_head *head,
1382 struct vmap_area *va, unsigned long nva_start_addr,
1383 unsigned long size)
68ad4a33 1384{
2c929233 1385 struct vmap_area *lva = NULL;
1b23ff80 1386 enum fit_type type = classify_va_fit_type(va, nva_start_addr, size);
68ad4a33
URS
1387
1388 if (type == FL_FIT_TYPE) {
1389 /*
1390 * No need to split VA, it fully fits.
1391 *
1392 * | |
1393 * V NVA V
1394 * |---------------|
1395 */
f9863be4 1396 unlink_va_augment(va, root);
68ad4a33
URS
1397 kmem_cache_free(vmap_area_cachep, va);
1398 } else if (type == LE_FIT_TYPE) {
1399 /*
1400 * Split left edge of fit VA.
1401 *
1402 * | |
1403 * V NVA V R
1404 * |-------|-------|
1405 */
1406 va->va_start += size;
1407 } else if (type == RE_FIT_TYPE) {
1408 /*
1409 * Split right edge of fit VA.
1410 *
1411 * | |
1412 * L V NVA V
1413 * |-------|-------|
1414 */
1415 va->va_end = nva_start_addr;
1416 } else if (type == NE_FIT_TYPE) {
1417 /*
1418 * Split no edge of fit VA.
1419 *
1420 * | |
1421 * L V NVA V R
1422 * |---|-------|---|
1423 */
82dd23e8
URS
1424 lva = __this_cpu_xchg(ne_fit_preload_node, NULL);
1425 if (unlikely(!lva)) {
1426 /*
1427 * For percpu allocator we do not do any pre-allocation
1428 * and leave it as it is. The reason is it most likely
1429 * never ends up with NE_FIT_TYPE splitting. In case of
1430 * percpu allocations offsets and sizes are aligned to
1431 * fixed align request, i.e. RE_FIT_TYPE and FL_FIT_TYPE
1432 * are its main fitting cases.
1433 *
1434 * There are a few exceptions though, as an example it is
1435 * a first allocation (early boot up) when we have "one"
1436 * big free space that has to be split.
060650a2
URS
1437 *
1438 * Also we can hit this path in case of regular "vmap"
1439 * allocations, if "this" current CPU was not preloaded.
1440 * See the comment in alloc_vmap_area() why. If so, then
1441 * GFP_NOWAIT is used instead to get an extra object for
1442 * split purpose. That is rare and most time does not
1443 * occur.
1444 *
1445 * What happens if an allocation gets failed. Basically,
1446 * an "overflow" path is triggered to purge lazily freed
1447 * areas to free some memory, then, the "retry" path is
1448 * triggered to repeat one more time. See more details
1449 * in alloc_vmap_area() function.
82dd23e8
URS
1450 */
1451 lva = kmem_cache_alloc(vmap_area_cachep, GFP_NOWAIT);
1452 if (!lva)
1453 return -1;
1454 }
68ad4a33
URS
1455
1456 /*
1457 * Build the remainder.
1458 */
1459 lva->va_start = va->va_start;
1460 lva->va_end = nva_start_addr;
1461
1462 /*
1463 * Shrink this VA to remaining size.
1464 */
1465 va->va_start = nva_start_addr + size;
1466 } else {
1467 return -1;
1468 }
1469
1470 if (type != FL_FIT_TYPE) {
1471 augment_tree_propagate_from(va);
1472
2c929233 1473 if (lva) /* type == NE_FIT_TYPE */
f9863be4 1474 insert_vmap_area_augment(lva, &va->rb_node, root, head);
68ad4a33
URS
1475 }
1476
1477 return 0;
1478}
1479
1480/*
1481 * Returns a start address of the newly allocated area, if success.
1482 * Otherwise a vend is returned that indicates failure.
1483 */
1484static __always_inline unsigned long
f9863be4
URS
1485__alloc_vmap_area(struct rb_root *root, struct list_head *head,
1486 unsigned long size, unsigned long align,
cacca6ba 1487 unsigned long vstart, unsigned long vend)
68ad4a33 1488{
9333fe98 1489 bool adjust_search_size = true;
68ad4a33
URS
1490 unsigned long nva_start_addr;
1491 struct vmap_area *va;
68ad4a33
URS
1492 int ret;
1493
9333fe98
UR
1494 /*
1495 * Do not adjust when:
1496 * a) align <= PAGE_SIZE, because it does not make any sense.
1497 * All blocks(their start addresses) are at least PAGE_SIZE
1498 * aligned anyway;
1499 * b) a short range where a requested size corresponds to exactly
1500 * specified [vstart:vend] interval and an alignment > PAGE_SIZE.
1501 * With adjusted search length an allocation would not succeed.
1502 */
1503 if (align <= PAGE_SIZE || (align > PAGE_SIZE && (vend - vstart) == size))
1504 adjust_search_size = false;
1505
f9863be4 1506 va = find_vmap_lowest_match(root, size, align, vstart, adjust_search_size);
68ad4a33
URS
1507 if (unlikely(!va))
1508 return vend;
1509
1510 if (va->va_start > vstart)
1511 nva_start_addr = ALIGN(va->va_start, align);
1512 else
1513 nva_start_addr = ALIGN(vstart, align);
1514
1515 /* Check the "vend" restriction. */
1516 if (nva_start_addr + size > vend)
1517 return vend;
1518
68ad4a33 1519 /* Update the free vmap_area. */
f9863be4 1520 ret = adjust_va_to_fit_type(root, head, va, nva_start_addr, size);
1b23ff80 1521 if (WARN_ON_ONCE(ret))
68ad4a33
URS
1522 return vend;
1523
a6cf4e0f 1524#if DEBUG_AUGMENT_LOWEST_MATCH_CHECK
bd1264c3 1525 find_vmap_lowest_match_check(root, head, size, align);
a6cf4e0f
URS
1526#endif
1527
68ad4a33
URS
1528 return nva_start_addr;
1529}
4da56b99 1530
d98c9e83
AR
1531/*
1532 * Free a region of KVA allocated by alloc_vmap_area
1533 */
1534static void free_vmap_area(struct vmap_area *va)
1535{
1536 /*
1537 * Remove from the busy tree/list.
1538 */
1539 spin_lock(&vmap_area_lock);
1540 unlink_va(va, &vmap_area_root);
1541 spin_unlock(&vmap_area_lock);
1542
1543 /*
1544 * Insert/Merge it back to the free tree/list.
1545 */
1546 spin_lock(&free_vmap_area_lock);
96e2db45 1547 merge_or_add_vmap_area_augment(va, &free_vmap_area_root, &free_vmap_area_list);
d98c9e83
AR
1548 spin_unlock(&free_vmap_area_lock);
1549}
1550
187f8cc4
URS
1551static inline void
1552preload_this_cpu_lock(spinlock_t *lock, gfp_t gfp_mask, int node)
1553{
1554 struct vmap_area *va = NULL;
1555
1556 /*
1557 * Preload this CPU with one extra vmap_area object. It is used
1558 * when fit type of free area is NE_FIT_TYPE. It guarantees that
1559 * a CPU that does an allocation is preloaded.
1560 *
1561 * We do it in non-atomic context, thus it allows us to use more
1562 * permissive allocation masks to be more stable under low memory
1563 * condition and high memory pressure.
1564 */
1565 if (!this_cpu_read(ne_fit_preload_node))
1566 va = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node);
1567
1568 spin_lock(lock);
1569
1570 if (va && __this_cpu_cmpxchg(ne_fit_preload_node, NULL, va))
1571 kmem_cache_free(vmap_area_cachep, va);
1572}
1573
db64fe02
NP
1574/*
1575 * Allocate a region of KVA of the specified size and alignment, within the
1576 * vstart and vend.
1577 */
1578static struct vmap_area *alloc_vmap_area(unsigned long size,
1579 unsigned long align,
1580 unsigned long vstart, unsigned long vend,
869176a0
BH
1581 int node, gfp_t gfp_mask,
1582 unsigned long va_flags)
db64fe02 1583{
187f8cc4 1584 struct vmap_area *va;
12e376a6 1585 unsigned long freed;
1da177e4 1586 unsigned long addr;
db64fe02 1587 int purged = 0;
d98c9e83 1588 int ret;
db64fe02 1589
7e4a32c0
HL
1590 if (unlikely(!size || offset_in_page(size) || !is_power_of_2(align)))
1591 return ERR_PTR(-EINVAL);
db64fe02 1592
68ad4a33
URS
1593 if (unlikely(!vmap_initialized))
1594 return ERR_PTR(-EBUSY);
1595
5803ed29 1596 might_sleep();
f07116d7 1597 gfp_mask = gfp_mask & GFP_RECLAIM_MASK;
4da56b99 1598
f07116d7 1599 va = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node);
db64fe02
NP
1600 if (unlikely(!va))
1601 return ERR_PTR(-ENOMEM);
1602
7f88f88f
CM
1603 /*
1604 * Only scan the relevant parts containing pointers to other objects
1605 * to avoid false negatives.
1606 */
f07116d7 1607 kmemleak_scan_area(&va->rb_node, SIZE_MAX, gfp_mask);
7f88f88f 1608
db64fe02 1609retry:
187f8cc4 1610 preload_this_cpu_lock(&free_vmap_area_lock, gfp_mask, node);
f9863be4
URS
1611 addr = __alloc_vmap_area(&free_vmap_area_root, &free_vmap_area_list,
1612 size, align, vstart, vend);
187f8cc4 1613 spin_unlock(&free_vmap_area_lock);
89699605 1614
cf243da6
URS
1615 trace_alloc_vmap_area(addr, size, align, vstart, vend, addr == vend);
1616
afd07389 1617 /*
68ad4a33
URS
1618 * If an allocation fails, the "vend" address is
1619 * returned. Therefore trigger the overflow path.
afd07389 1620 */
68ad4a33 1621 if (unlikely(addr == vend))
89699605 1622 goto overflow;
db64fe02
NP
1623
1624 va->va_start = addr;
1625 va->va_end = addr + size;
688fcbfc 1626 va->vm = NULL;
869176a0 1627 va->flags = va_flags;
68ad4a33 1628
e36176be
URS
1629 spin_lock(&vmap_area_lock);
1630 insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
db64fe02
NP
1631 spin_unlock(&vmap_area_lock);
1632
61e16557 1633 BUG_ON(!IS_ALIGNED(va->va_start, align));
89699605
NP
1634 BUG_ON(va->va_start < vstart);
1635 BUG_ON(va->va_end > vend);
1636
d98c9e83
AR
1637 ret = kasan_populate_vmalloc(addr, size);
1638 if (ret) {
1639 free_vmap_area(va);
1640 return ERR_PTR(ret);
1641 }
1642
db64fe02 1643 return va;
89699605
NP
1644
1645overflow:
89699605
NP
1646 if (!purged) {
1647 purge_vmap_area_lazy();
1648 purged = 1;
1649 goto retry;
1650 }
4da56b99 1651
12e376a6
URS
1652 freed = 0;
1653 blocking_notifier_call_chain(&vmap_notify_list, 0, &freed);
1654
1655 if (freed > 0) {
1656 purged = 0;
1657 goto retry;
4da56b99
CW
1658 }
1659
03497d76 1660 if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit())
756a025f
JP
1661 pr_warn("vmap allocation for size %lu failed: use vmalloc=<size> to increase size\n",
1662 size);
68ad4a33
URS
1663
1664 kmem_cache_free(vmap_area_cachep, va);
89699605 1665 return ERR_PTR(-EBUSY);
db64fe02
NP
1666}
1667
4da56b99
CW
1668int register_vmap_purge_notifier(struct notifier_block *nb)
1669{
1670 return blocking_notifier_chain_register(&vmap_notify_list, nb);
1671}
1672EXPORT_SYMBOL_GPL(register_vmap_purge_notifier);
1673
1674int unregister_vmap_purge_notifier(struct notifier_block *nb)
1675{
1676 return blocking_notifier_chain_unregister(&vmap_notify_list, nb);
1677}
1678EXPORT_SYMBOL_GPL(unregister_vmap_purge_notifier);
1679
db64fe02
NP
1680/*
1681 * lazy_max_pages is the maximum amount of virtual address space we gather up
1682 * before attempting to purge with a TLB flush.
1683 *
1684 * There is a tradeoff here: a larger number will cover more kernel page tables
1685 * and take slightly longer to purge, but it will linearly reduce the number of
1686 * global TLB flushes that must be performed. It would seem natural to scale
1687 * this number up linearly with the number of CPUs (because vmapping activity
1688 * could also scale linearly with the number of CPUs), however it is likely
1689 * that in practice, workloads might be constrained in other ways that mean
1690 * vmap activity will not scale linearly with CPUs. Also, I want to be
1691 * conservative and not introduce a big latency on huge systems, so go with
1692 * a less aggressive log scale. It will still be an improvement over the old
1693 * code, and it will be simple to change the scale factor if we find that it
1694 * becomes a problem on bigger systems.
1695 */
1696static unsigned long lazy_max_pages(void)
1697{
1698 unsigned int log;
1699
1700 log = fls(num_online_cpus());
1701
1702 return log * (32UL * 1024 * 1024 / PAGE_SIZE);
1703}
1704
4d36e6f8 1705static atomic_long_t vmap_lazy_nr = ATOMIC_LONG_INIT(0);
db64fe02 1706
0574ecd1 1707/*
f0953a1b 1708 * Serialize vmap purging. There is no actual critical section protected
153090f2 1709 * by this lock, but we want to avoid concurrent calls for performance
0574ecd1
CH
1710 * reasons and to make the pcpu_get_vm_areas more deterministic.
1711 */
f9e09977 1712static DEFINE_MUTEX(vmap_purge_lock);
0574ecd1 1713
02b709df
NP
1714/* for per-CPU blocks */
1715static void purge_fragmented_blocks_allcpus(void);
1716
db64fe02
NP
1717/*
1718 * Purges all lazily-freed vmap areas.
db64fe02 1719 */
0574ecd1 1720static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end)
db64fe02 1721{
4d36e6f8 1722 unsigned long resched_threshold;
6030fd5f 1723 unsigned int num_purged_areas = 0;
baa468a6 1724 struct list_head local_purge_list;
96e2db45 1725 struct vmap_area *va, *n_va;
db64fe02 1726
0574ecd1 1727 lockdep_assert_held(&vmap_purge_lock);
02b709df 1728
96e2db45
URS
1729 spin_lock(&purge_vmap_area_lock);
1730 purge_vmap_area_root = RB_ROOT;
baa468a6 1731 list_replace_init(&purge_vmap_area_list, &local_purge_list);
96e2db45
URS
1732 spin_unlock(&purge_vmap_area_lock);
1733
baa468a6 1734 if (unlikely(list_empty(&local_purge_list)))
6030fd5f 1735 goto out;
68571be9 1736
96e2db45 1737 start = min(start,
baa468a6 1738 list_first_entry(&local_purge_list,
96e2db45
URS
1739 struct vmap_area, list)->va_start);
1740
1741 end = max(end,
baa468a6 1742 list_last_entry(&local_purge_list,
96e2db45 1743 struct vmap_area, list)->va_end);
db64fe02 1744
0574ecd1 1745 flush_tlb_kernel_range(start, end);
4d36e6f8 1746 resched_threshold = lazy_max_pages() << 1;
db64fe02 1747
e36176be 1748 spin_lock(&free_vmap_area_lock);
baa468a6 1749 list_for_each_entry_safe(va, n_va, &local_purge_list, list) {
4d36e6f8 1750 unsigned long nr = (va->va_end - va->va_start) >> PAGE_SHIFT;
3c5c3cfb
DA
1751 unsigned long orig_start = va->va_start;
1752 unsigned long orig_end = va->va_end;
763b218d 1753
dd3b8353
URS
1754 /*
1755 * Finally insert or merge lazily-freed area. It is
1756 * detached and there is no need to "unlink" it from
1757 * anything.
1758 */
96e2db45
URS
1759 va = merge_or_add_vmap_area_augment(va, &free_vmap_area_root,
1760 &free_vmap_area_list);
3c5c3cfb 1761
9c801f61
URS
1762 if (!va)
1763 continue;
1764
3c5c3cfb
DA
1765 if (is_vmalloc_or_module_addr((void *)orig_start))
1766 kasan_release_vmalloc(orig_start, orig_end,
1767 va->va_start, va->va_end);
dd3b8353 1768
4d36e6f8 1769 atomic_long_sub(nr, &vmap_lazy_nr);
6030fd5f 1770 num_purged_areas++;
68571be9 1771
4d36e6f8 1772 if (atomic_long_read(&vmap_lazy_nr) < resched_threshold)
e36176be 1773 cond_resched_lock(&free_vmap_area_lock);
763b218d 1774 }
e36176be 1775 spin_unlock(&free_vmap_area_lock);
6030fd5f
URS
1776
1777out:
1778 trace_purge_vmap_area_lazy(start, end, num_purged_areas);
1779 return num_purged_areas > 0;
db64fe02
NP
1780}
1781
1782/*
1783 * Kick off a purge of the outstanding lazy areas.
1784 */
1785static void purge_vmap_area_lazy(void)
1786{
f9e09977 1787 mutex_lock(&vmap_purge_lock);
0574ecd1
CH
1788 purge_fragmented_blocks_allcpus();
1789 __purge_vmap_area_lazy(ULONG_MAX, 0);
f9e09977 1790 mutex_unlock(&vmap_purge_lock);
db64fe02
NP
1791}
1792
690467c8
URS
1793static void drain_vmap_area_work(struct work_struct *work)
1794{
1795 unsigned long nr_lazy;
1796
1797 do {
1798 mutex_lock(&vmap_purge_lock);
1799 __purge_vmap_area_lazy(ULONG_MAX, 0);
1800 mutex_unlock(&vmap_purge_lock);
1801
1802 /* Recheck if further work is required. */
1803 nr_lazy = atomic_long_read(&vmap_lazy_nr);
1804 } while (nr_lazy > lazy_max_pages());
1805}
1806
db64fe02 1807/*
edd89818
URS
1808 * Free a vmap area, caller ensuring that the area has been unmapped,
1809 * unlinked and flush_cache_vunmap had been called for the correct
1810 * range previously.
db64fe02 1811 */
64141da5 1812static void free_vmap_area_noflush(struct vmap_area *va)
db64fe02 1813{
8c4196fe
URS
1814 unsigned long nr_lazy_max = lazy_max_pages();
1815 unsigned long va_start = va->va_start;
4d36e6f8 1816 unsigned long nr_lazy;
80c4bd7a 1817
edd89818
URS
1818 if (WARN_ON_ONCE(!list_empty(&va->list)))
1819 return;
dd3b8353 1820
4d36e6f8
URS
1821 nr_lazy = atomic_long_add_return((va->va_end - va->va_start) >>
1822 PAGE_SHIFT, &vmap_lazy_nr);
80c4bd7a 1823
96e2db45
URS
1824 /*
1825 * Merge or place it to the purge tree/list.
1826 */
1827 spin_lock(&purge_vmap_area_lock);
1828 merge_or_add_vmap_area(va,
1829 &purge_vmap_area_root, &purge_vmap_area_list);
1830 spin_unlock(&purge_vmap_area_lock);
80c4bd7a 1831
8c4196fe
URS
1832 trace_free_vmap_area_noflush(va_start, nr_lazy, nr_lazy_max);
1833
96e2db45 1834 /* After this point, we may free va at any time */
8c4196fe 1835 if (unlikely(nr_lazy > nr_lazy_max))
690467c8 1836 schedule_work(&drain_vmap_work);
db64fe02
NP
1837}
1838
b29acbdc
NP
1839/*
1840 * Free and unmap a vmap area
1841 */
1842static void free_unmap_vmap_area(struct vmap_area *va)
1843{
1844 flush_cache_vunmap(va->va_start, va->va_end);
4ad0ae8c 1845 vunmap_range_noflush(va->va_start, va->va_end);
8e57f8ac 1846 if (debug_pagealloc_enabled_static())
82a2e924
CP
1847 flush_tlb_kernel_range(va->va_start, va->va_end);
1848
c8eef01e 1849 free_vmap_area_noflush(va);
b29acbdc
NP
1850}
1851
993d0b28 1852struct vmap_area *find_vmap_area(unsigned long addr)
db64fe02
NP
1853{
1854 struct vmap_area *va;
1855
1856 spin_lock(&vmap_area_lock);
899c6efe 1857 va = __find_vmap_area(addr, &vmap_area_root);
db64fe02
NP
1858 spin_unlock(&vmap_area_lock);
1859
1860 return va;
1861}
1862
edd89818
URS
1863static struct vmap_area *find_unlink_vmap_area(unsigned long addr)
1864{
1865 struct vmap_area *va;
1866
1867 spin_lock(&vmap_area_lock);
1868 va = __find_vmap_area(addr, &vmap_area_root);
1869 if (va)
1870 unlink_va(va, &vmap_area_root);
1871 spin_unlock(&vmap_area_lock);
1872
1873 return va;
1874}
1875
db64fe02
NP
1876/*** Per cpu kva allocator ***/
1877
1878/*
1879 * vmap space is limited especially on 32 bit architectures. Ensure there is
1880 * room for at least 16 percpu vmap blocks per CPU.
1881 */
1882/*
1883 * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
1884 * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess
1885 * instead (we just need a rough idea)
1886 */
1887#if BITS_PER_LONG == 32
1888#define VMALLOC_SPACE (128UL*1024*1024)
1889#else
1890#define VMALLOC_SPACE (128UL*1024*1024*1024)
1891#endif
1892
1893#define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE)
1894#define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */
1895#define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */
1896#define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2)
1897#define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */
1898#define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */
f982f915
CL
1899#define VMAP_BBMAP_BITS \
1900 VMAP_MIN(VMAP_BBMAP_BITS_MAX, \
1901 VMAP_MAX(VMAP_BBMAP_BITS_MIN, \
1902 VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16))
db64fe02
NP
1903
1904#define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE)
1905
869176a0
BH
1906#define VMAP_RAM 0x1 /* indicates vm_map_ram area*/
1907#define VMAP_BLOCK 0x2 /* mark out the vmap_block sub-type*/
1908#define VMAP_FLAGS_MASK 0x3
1909
db64fe02
NP
1910struct vmap_block_queue {
1911 spinlock_t lock;
1912 struct list_head free;
db64fe02
NP
1913};
1914
1915struct vmap_block {
1916 spinlock_t lock;
1917 struct vmap_area *va;
db64fe02 1918 unsigned long free, dirty;
d76f9954 1919 DECLARE_BITMAP(used_map, VMAP_BBMAP_BITS);
7d61bfe8 1920 unsigned long dirty_min, dirty_max; /*< dirty range */
de560423
NP
1921 struct list_head free_list;
1922 struct rcu_head rcu_head;
02b709df 1923 struct list_head purge;
db64fe02
NP
1924};
1925
1926/* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
1927static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
1928
1929/*
0f14599c 1930 * XArray of vmap blocks, indexed by address, to quickly find a vmap block
db64fe02
NP
1931 * in the free path. Could get rid of this if we change the API to return a
1932 * "cookie" from alloc, to be passed to free. But no big deal yet.
1933 */
0f14599c 1934static DEFINE_XARRAY(vmap_blocks);
db64fe02
NP
1935
1936/*
1937 * We should probably have a fallback mechanism to allocate virtual memory
1938 * out of partially filled vmap blocks. However vmap block sizing should be
1939 * fairly reasonable according to the vmalloc size, so it shouldn't be a
1940 * big problem.
1941 */
1942
1943static unsigned long addr_to_vb_idx(unsigned long addr)
1944{
1945 addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
1946 addr /= VMAP_BLOCK_SIZE;
1947 return addr;
1948}
1949
cf725ce2
RP
1950static void *vmap_block_vaddr(unsigned long va_start, unsigned long pages_off)
1951{
1952 unsigned long addr;
1953
1954 addr = va_start + (pages_off << PAGE_SHIFT);
1955 BUG_ON(addr_to_vb_idx(addr) != addr_to_vb_idx(va_start));
1956 return (void *)addr;
1957}
1958
1959/**
1960 * new_vmap_block - allocates new vmap_block and occupies 2^order pages in this
1961 * block. Of course pages number can't exceed VMAP_BBMAP_BITS
1962 * @order: how many 2^order pages should be occupied in newly allocated block
1963 * @gfp_mask: flags for the page level allocator
1964 *
a862f68a 1965 * Return: virtual address in a newly allocated block or ERR_PTR(-errno)
cf725ce2
RP
1966 */
1967static void *new_vmap_block(unsigned int order, gfp_t gfp_mask)
db64fe02
NP
1968{
1969 struct vmap_block_queue *vbq;
1970 struct vmap_block *vb;
1971 struct vmap_area *va;
1972 unsigned long vb_idx;
1973 int node, err;
cf725ce2 1974 void *vaddr;
db64fe02
NP
1975
1976 node = numa_node_id();
1977
1978 vb = kmalloc_node(sizeof(struct vmap_block),
1979 gfp_mask & GFP_RECLAIM_MASK, node);
1980 if (unlikely(!vb))
1981 return ERR_PTR(-ENOMEM);
1982
1983 va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
1984 VMALLOC_START, VMALLOC_END,
869176a0
BH
1985 node, gfp_mask,
1986 VMAP_RAM|VMAP_BLOCK);
ddf9c6d4 1987 if (IS_ERR(va)) {
db64fe02 1988 kfree(vb);
e7d86340 1989 return ERR_CAST(va);
db64fe02
NP
1990 }
1991
cf725ce2 1992 vaddr = vmap_block_vaddr(va->va_start, 0);
db64fe02
NP
1993 spin_lock_init(&vb->lock);
1994 vb->va = va;
cf725ce2
RP
1995 /* At least something should be left free */
1996 BUG_ON(VMAP_BBMAP_BITS <= (1UL << order));
d76f9954 1997 bitmap_zero(vb->used_map, VMAP_BBMAP_BITS);
cf725ce2 1998 vb->free = VMAP_BBMAP_BITS - (1UL << order);
db64fe02 1999 vb->dirty = 0;
7d61bfe8
RP
2000 vb->dirty_min = VMAP_BBMAP_BITS;
2001 vb->dirty_max = 0;
d76f9954 2002 bitmap_set(vb->used_map, 0, (1UL << order));
db64fe02 2003 INIT_LIST_HEAD(&vb->free_list);
db64fe02
NP
2004
2005 vb_idx = addr_to_vb_idx(va->va_start);
0f14599c
MWO
2006 err = xa_insert(&vmap_blocks, vb_idx, vb, gfp_mask);
2007 if (err) {
2008 kfree(vb);
2009 free_vmap_area(va);
2010 return ERR_PTR(err);
2011 }
db64fe02 2012
3f804920 2013 vbq = raw_cpu_ptr(&vmap_block_queue);
db64fe02 2014 spin_lock(&vbq->lock);
68ac546f 2015 list_add_tail_rcu(&vb->free_list, &vbq->free);
db64fe02 2016 spin_unlock(&vbq->lock);
db64fe02 2017
cf725ce2 2018 return vaddr;
db64fe02
NP
2019}
2020
db64fe02
NP
2021static void free_vmap_block(struct vmap_block *vb)
2022{
2023 struct vmap_block *tmp;
db64fe02 2024
0f14599c 2025 tmp = xa_erase(&vmap_blocks, addr_to_vb_idx(vb->va->va_start));
db64fe02
NP
2026 BUG_ON(tmp != vb);
2027
edd89818
URS
2028 spin_lock(&vmap_area_lock);
2029 unlink_va(vb->va, &vmap_area_root);
2030 spin_unlock(&vmap_area_lock);
2031
64141da5 2032 free_vmap_area_noflush(vb->va);
22a3c7d1 2033 kfree_rcu(vb, rcu_head);
db64fe02
NP
2034}
2035
02b709df
NP
2036static void purge_fragmented_blocks(int cpu)
2037{
2038 LIST_HEAD(purge);
2039 struct vmap_block *vb;
2040 struct vmap_block *n_vb;
2041 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
2042
2043 rcu_read_lock();
2044 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
2045
2046 if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS))
2047 continue;
2048
2049 spin_lock(&vb->lock);
2050 if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) {
2051 vb->free = 0; /* prevent further allocs after releasing lock */
2052 vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */
7d61bfe8
RP
2053 vb->dirty_min = 0;
2054 vb->dirty_max = VMAP_BBMAP_BITS;
02b709df
NP
2055 spin_lock(&vbq->lock);
2056 list_del_rcu(&vb->free_list);
2057 spin_unlock(&vbq->lock);
2058 spin_unlock(&vb->lock);
2059 list_add_tail(&vb->purge, &purge);
2060 } else
2061 spin_unlock(&vb->lock);
2062 }
2063 rcu_read_unlock();
2064
2065 list_for_each_entry_safe(vb, n_vb, &purge, purge) {
2066 list_del(&vb->purge);
2067 free_vmap_block(vb);
2068 }
2069}
2070
02b709df
NP
2071static void purge_fragmented_blocks_allcpus(void)
2072{
2073 int cpu;
2074
2075 for_each_possible_cpu(cpu)
2076 purge_fragmented_blocks(cpu);
2077}
2078
db64fe02
NP
2079static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
2080{
2081 struct vmap_block_queue *vbq;
2082 struct vmap_block *vb;
cf725ce2 2083 void *vaddr = NULL;
db64fe02
NP
2084 unsigned int order;
2085
891c49ab 2086 BUG_ON(offset_in_page(size));
db64fe02 2087 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
aa91c4d8
JK
2088 if (WARN_ON(size == 0)) {
2089 /*
2090 * Allocating 0 bytes isn't what caller wants since
2091 * get_order(0) returns funny result. Just warn and terminate
2092 * early.
2093 */
2094 return NULL;
2095 }
db64fe02
NP
2096 order = get_order(size);
2097
db64fe02 2098 rcu_read_lock();
3f804920 2099 vbq = raw_cpu_ptr(&vmap_block_queue);
db64fe02 2100 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
cf725ce2 2101 unsigned long pages_off;
db64fe02
NP
2102
2103 spin_lock(&vb->lock);
cf725ce2
RP
2104 if (vb->free < (1UL << order)) {
2105 spin_unlock(&vb->lock);
2106 continue;
2107 }
02b709df 2108
cf725ce2
RP
2109 pages_off = VMAP_BBMAP_BITS - vb->free;
2110 vaddr = vmap_block_vaddr(vb->va->va_start, pages_off);
02b709df 2111 vb->free -= 1UL << order;
d76f9954 2112 bitmap_set(vb->used_map, pages_off, (1UL << order));
02b709df
NP
2113 if (vb->free == 0) {
2114 spin_lock(&vbq->lock);
2115 list_del_rcu(&vb->free_list);
2116 spin_unlock(&vbq->lock);
2117 }
cf725ce2 2118
02b709df
NP
2119 spin_unlock(&vb->lock);
2120 break;
db64fe02 2121 }
02b709df 2122
db64fe02
NP
2123 rcu_read_unlock();
2124
cf725ce2
RP
2125 /* Allocate new block if nothing was found */
2126 if (!vaddr)
2127 vaddr = new_vmap_block(order, gfp_mask);
db64fe02 2128
cf725ce2 2129 return vaddr;
db64fe02
NP
2130}
2131
78a0e8c4 2132static void vb_free(unsigned long addr, unsigned long size)
db64fe02
NP
2133{
2134 unsigned long offset;
db64fe02
NP
2135 unsigned int order;
2136 struct vmap_block *vb;
2137
891c49ab 2138 BUG_ON(offset_in_page(size));
db64fe02 2139 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
b29acbdc 2140
78a0e8c4 2141 flush_cache_vunmap(addr, addr + size);
b29acbdc 2142
db64fe02 2143 order = get_order(size);
78a0e8c4 2144 offset = (addr & (VMAP_BLOCK_SIZE - 1)) >> PAGE_SHIFT;
0f14599c 2145 vb = xa_load(&vmap_blocks, addr_to_vb_idx(addr));
d76f9954
BH
2146 spin_lock(&vb->lock);
2147 bitmap_clear(vb->used_map, offset, (1UL << order));
2148 spin_unlock(&vb->lock);
db64fe02 2149
4ad0ae8c 2150 vunmap_range_noflush(addr, addr + size);
64141da5 2151
8e57f8ac 2152 if (debug_pagealloc_enabled_static())
78a0e8c4 2153 flush_tlb_kernel_range(addr, addr + size);
82a2e924 2154
db64fe02 2155 spin_lock(&vb->lock);
7d61bfe8
RP
2156
2157 /* Expand dirty range */
2158 vb->dirty_min = min(vb->dirty_min, offset);
2159 vb->dirty_max = max(vb->dirty_max, offset + (1UL << order));
d086817d 2160
db64fe02
NP
2161 vb->dirty += 1UL << order;
2162 if (vb->dirty == VMAP_BBMAP_BITS) {
de560423 2163 BUG_ON(vb->free);
db64fe02
NP
2164 spin_unlock(&vb->lock);
2165 free_vmap_block(vb);
2166 } else
2167 spin_unlock(&vb->lock);
2168}
2169
868b104d 2170static void _vm_unmap_aliases(unsigned long start, unsigned long end, int flush)
db64fe02 2171{
db64fe02 2172 int cpu;
db64fe02 2173
9b463334
JF
2174 if (unlikely(!vmap_initialized))
2175 return;
2176
5803ed29
CH
2177 might_sleep();
2178
db64fe02
NP
2179 for_each_possible_cpu(cpu) {
2180 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
2181 struct vmap_block *vb;
2182
2183 rcu_read_lock();
2184 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
db64fe02 2185 spin_lock(&vb->lock);
ad216c03 2186 if (vb->dirty && vb->dirty != VMAP_BBMAP_BITS) {
7d61bfe8 2187 unsigned long va_start = vb->va->va_start;
db64fe02 2188 unsigned long s, e;
b136be5e 2189
7d61bfe8
RP
2190 s = va_start + (vb->dirty_min << PAGE_SHIFT);
2191 e = va_start + (vb->dirty_max << PAGE_SHIFT);
db64fe02 2192
7d61bfe8
RP
2193 start = min(s, start);
2194 end = max(e, end);
db64fe02 2195
7d61bfe8 2196 flush = 1;
db64fe02
NP
2197 }
2198 spin_unlock(&vb->lock);
2199 }
2200 rcu_read_unlock();
2201 }
2202
f9e09977 2203 mutex_lock(&vmap_purge_lock);
0574ecd1
CH
2204 purge_fragmented_blocks_allcpus();
2205 if (!__purge_vmap_area_lazy(start, end) && flush)
2206 flush_tlb_kernel_range(start, end);
f9e09977 2207 mutex_unlock(&vmap_purge_lock);
db64fe02 2208}
868b104d
RE
2209
2210/**
2211 * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
2212 *
2213 * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
2214 * to amortize TLB flushing overheads. What this means is that any page you
2215 * have now, may, in a former life, have been mapped into kernel virtual
2216 * address by the vmap layer and so there might be some CPUs with TLB entries
2217 * still referencing that page (additional to the regular 1:1 kernel mapping).
2218 *
2219 * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
2220 * be sure that none of the pages we have control over will have any aliases
2221 * from the vmap layer.
2222 */
2223void vm_unmap_aliases(void)
2224{
2225 unsigned long start = ULONG_MAX, end = 0;
2226 int flush = 0;
2227
2228 _vm_unmap_aliases(start, end, flush);
2229}
db64fe02
NP
2230EXPORT_SYMBOL_GPL(vm_unmap_aliases);
2231
2232/**
2233 * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
2234 * @mem: the pointer returned by vm_map_ram
2235 * @count: the count passed to that vm_map_ram call (cannot unmap partial)
2236 */
2237void vm_unmap_ram(const void *mem, unsigned int count)
2238{
65ee03c4 2239 unsigned long size = (unsigned long)count << PAGE_SHIFT;
4aff1dc4 2240 unsigned long addr = (unsigned long)kasan_reset_tag(mem);
9c3acf60 2241 struct vmap_area *va;
db64fe02 2242
5803ed29 2243 might_sleep();
db64fe02
NP
2244 BUG_ON(!addr);
2245 BUG_ON(addr < VMALLOC_START);
2246 BUG_ON(addr > VMALLOC_END);
a1c0b1a0 2247 BUG_ON(!PAGE_ALIGNED(addr));
db64fe02 2248
d98c9e83
AR
2249 kasan_poison_vmalloc(mem, size);
2250
9c3acf60 2251 if (likely(count <= VMAP_MAX_ALLOC)) {
05e3ff95 2252 debug_check_no_locks_freed(mem, size);
78a0e8c4 2253 vb_free(addr, size);
9c3acf60
CH
2254 return;
2255 }
2256
edd89818 2257 va = find_unlink_vmap_area(addr);
14687619
URS
2258 if (WARN_ON_ONCE(!va))
2259 return;
2260
05e3ff95
CP
2261 debug_check_no_locks_freed((void *)va->va_start,
2262 (va->va_end - va->va_start));
9c3acf60 2263 free_unmap_vmap_area(va);
db64fe02
NP
2264}
2265EXPORT_SYMBOL(vm_unmap_ram);
2266
2267/**
2268 * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
2269 * @pages: an array of pointers to the pages to be mapped
2270 * @count: number of pages
2271 * @node: prefer to allocate data structures on this node
e99c97ad 2272 *
36437638
GK
2273 * If you use this function for less than VMAP_MAX_ALLOC pages, it could be
2274 * faster than vmap so it's good. But if you mix long-life and short-life
2275 * objects with vm_map_ram(), it could consume lots of address space through
2276 * fragmentation (especially on a 32bit machine). You could see failures in
2277 * the end. Please use this function for short-lived objects.
2278 *
e99c97ad 2279 * Returns: a pointer to the address that has been mapped, or %NULL on failure
db64fe02 2280 */
d4efd79a 2281void *vm_map_ram(struct page **pages, unsigned int count, int node)
db64fe02 2282{
65ee03c4 2283 unsigned long size = (unsigned long)count << PAGE_SHIFT;
db64fe02
NP
2284 unsigned long addr;
2285 void *mem;
2286
2287 if (likely(count <= VMAP_MAX_ALLOC)) {
2288 mem = vb_alloc(size, GFP_KERNEL);
2289 if (IS_ERR(mem))
2290 return NULL;
2291 addr = (unsigned long)mem;
2292 } else {
2293 struct vmap_area *va;
2294 va = alloc_vmap_area(size, PAGE_SIZE,
869176a0
BH
2295 VMALLOC_START, VMALLOC_END,
2296 node, GFP_KERNEL, VMAP_RAM);
db64fe02
NP
2297 if (IS_ERR(va))
2298 return NULL;
2299
2300 addr = va->va_start;
2301 mem = (void *)addr;
2302 }
d98c9e83 2303
b67177ec
NP
2304 if (vmap_pages_range(addr, addr + size, PAGE_KERNEL,
2305 pages, PAGE_SHIFT) < 0) {
db64fe02
NP
2306 vm_unmap_ram(mem, count);
2307 return NULL;
2308 }
b67177ec 2309
23689e91
AK
2310 /*
2311 * Mark the pages as accessible, now that they are mapped.
2312 * With hardware tag-based KASAN, marking is skipped for
2313 * non-VM_ALLOC mappings, see __kasan_unpoison_vmalloc().
2314 */
f6e39794 2315 mem = kasan_unpoison_vmalloc(mem, size, KASAN_VMALLOC_PROT_NORMAL);
19f1c3ac 2316
db64fe02
NP
2317 return mem;
2318}
2319EXPORT_SYMBOL(vm_map_ram);
2320
4341fa45 2321static struct vm_struct *vmlist __initdata;
92eac168 2322
121e6f32
NP
2323static inline unsigned int vm_area_page_order(struct vm_struct *vm)
2324{
2325#ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
2326 return vm->page_order;
2327#else
2328 return 0;
2329#endif
2330}
2331
2332static inline void set_vm_area_page_order(struct vm_struct *vm, unsigned int order)
2333{
2334#ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
2335 vm->page_order = order;
2336#else
2337 BUG_ON(order != 0);
2338#endif
2339}
2340
be9b7335
NP
2341/**
2342 * vm_area_add_early - add vmap area early during boot
2343 * @vm: vm_struct to add
2344 *
2345 * This function is used to add fixed kernel vm area to vmlist before
2346 * vmalloc_init() is called. @vm->addr, @vm->size, and @vm->flags
2347 * should contain proper values and the other fields should be zero.
2348 *
2349 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
2350 */
2351void __init vm_area_add_early(struct vm_struct *vm)
2352{
2353 struct vm_struct *tmp, **p;
2354
2355 BUG_ON(vmap_initialized);
2356 for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
2357 if (tmp->addr >= vm->addr) {
2358 BUG_ON(tmp->addr < vm->addr + vm->size);
2359 break;
2360 } else
2361 BUG_ON(tmp->addr + tmp->size > vm->addr);
2362 }
2363 vm->next = *p;
2364 *p = vm;
2365}
2366
f0aa6617
TH
2367/**
2368 * vm_area_register_early - register vmap area early during boot
2369 * @vm: vm_struct to register
c0c0a293 2370 * @align: requested alignment
f0aa6617
TH
2371 *
2372 * This function is used to register kernel vm area before
2373 * vmalloc_init() is called. @vm->size and @vm->flags should contain
2374 * proper values on entry and other fields should be zero. On return,
2375 * vm->addr contains the allocated address.
2376 *
2377 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
2378 */
c0c0a293 2379void __init vm_area_register_early(struct vm_struct *vm, size_t align)
f0aa6617 2380{
0eb68437
KW
2381 unsigned long addr = ALIGN(VMALLOC_START, align);
2382 struct vm_struct *cur, **p;
c0c0a293 2383
0eb68437 2384 BUG_ON(vmap_initialized);
f0aa6617 2385
0eb68437
KW
2386 for (p = &vmlist; (cur = *p) != NULL; p = &cur->next) {
2387 if ((unsigned long)cur->addr - addr >= vm->size)
2388 break;
2389 addr = ALIGN((unsigned long)cur->addr + cur->size, align);
2390 }
f0aa6617 2391
0eb68437
KW
2392 BUG_ON(addr > VMALLOC_END - vm->size);
2393 vm->addr = (void *)addr;
2394 vm->next = *p;
2395 *p = vm;
3252b1d8 2396 kasan_populate_early_vm_area_shadow(vm->addr, vm->size);
f0aa6617
TH
2397}
2398
68ad4a33
URS
2399static void vmap_init_free_space(void)
2400{
2401 unsigned long vmap_start = 1;
2402 const unsigned long vmap_end = ULONG_MAX;
2403 struct vmap_area *busy, *free;
2404
2405 /*
2406 * B F B B B F
2407 * -|-----|.....|-----|-----|-----|.....|-
2408 * | The KVA space |
2409 * |<--------------------------------->|
2410 */
2411 list_for_each_entry(busy, &vmap_area_list, list) {
2412 if (busy->va_start - vmap_start > 0) {
2413 free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
2414 if (!WARN_ON_ONCE(!free)) {
2415 free->va_start = vmap_start;
2416 free->va_end = busy->va_start;
2417
2418 insert_vmap_area_augment(free, NULL,
2419 &free_vmap_area_root,
2420 &free_vmap_area_list);
2421 }
2422 }
2423
2424 vmap_start = busy->va_end;
2425 }
2426
2427 if (vmap_end - vmap_start > 0) {
2428 free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
2429 if (!WARN_ON_ONCE(!free)) {
2430 free->va_start = vmap_start;
2431 free->va_end = vmap_end;
2432
2433 insert_vmap_area_augment(free, NULL,
2434 &free_vmap_area_root,
2435 &free_vmap_area_list);
2436 }
2437 }
2438}
2439
e36176be
URS
2440static inline void setup_vmalloc_vm_locked(struct vm_struct *vm,
2441 struct vmap_area *va, unsigned long flags, const void *caller)
cf88c790 2442{
cf88c790
TH
2443 vm->flags = flags;
2444 vm->addr = (void *)va->va_start;
2445 vm->size = va->va_end - va->va_start;
2446 vm->caller = caller;
db1aecaf 2447 va->vm = vm;
e36176be
URS
2448}
2449
2450static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
2451 unsigned long flags, const void *caller)
2452{
2453 spin_lock(&vmap_area_lock);
2454 setup_vmalloc_vm_locked(vm, va, flags, caller);
c69480ad 2455 spin_unlock(&vmap_area_lock);
f5252e00 2456}
cf88c790 2457
20fc02b4 2458static void clear_vm_uninitialized_flag(struct vm_struct *vm)
f5252e00 2459{
d4033afd 2460 /*
20fc02b4 2461 * Before removing VM_UNINITIALIZED,
d4033afd
JK
2462 * we should make sure that vm has proper values.
2463 * Pair with smp_rmb() in show_numa_info().
2464 */
2465 smp_wmb();
20fc02b4 2466 vm->flags &= ~VM_UNINITIALIZED;
cf88c790
TH
2467}
2468
db64fe02 2469static struct vm_struct *__get_vm_area_node(unsigned long size,
7ca3027b
DA
2470 unsigned long align, unsigned long shift, unsigned long flags,
2471 unsigned long start, unsigned long end, int node,
2472 gfp_t gfp_mask, const void *caller)
db64fe02 2473{
0006526d 2474 struct vmap_area *va;
db64fe02 2475 struct vm_struct *area;
d98c9e83 2476 unsigned long requested_size = size;
1da177e4 2477
52fd24ca 2478 BUG_ON(in_interrupt());
7ca3027b 2479 size = ALIGN(size, 1ul << shift);
31be8309
OH
2480 if (unlikely(!size))
2481 return NULL;
1da177e4 2482
252e5c6e 2483 if (flags & VM_IOREMAP)
2484 align = 1ul << clamp_t(int, get_count_order_long(size),
2485 PAGE_SHIFT, IOREMAP_MAX_ORDER);
2486
cf88c790 2487 area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
1da177e4
LT
2488 if (unlikely(!area))
2489 return NULL;
2490
71394fe5
AR
2491 if (!(flags & VM_NO_GUARD))
2492 size += PAGE_SIZE;
1da177e4 2493
869176a0 2494 va = alloc_vmap_area(size, align, start, end, node, gfp_mask, 0);
db64fe02
NP
2495 if (IS_ERR(va)) {
2496 kfree(area);
2497 return NULL;
1da177e4 2498 }
1da177e4 2499
d98c9e83 2500 setup_vmalloc_vm(area, va, flags, caller);
3c5c3cfb 2501
19f1c3ac
AK
2502 /*
2503 * Mark pages for non-VM_ALLOC mappings as accessible. Do it now as a
2504 * best-effort approach, as they can be mapped outside of vmalloc code.
2505 * For VM_ALLOC mappings, the pages are marked as accessible after
2506 * getting mapped in __vmalloc_node_range().
23689e91
AK
2507 * With hardware tag-based KASAN, marking is skipped for
2508 * non-VM_ALLOC mappings, see __kasan_unpoison_vmalloc().
19f1c3ac
AK
2509 */
2510 if (!(flags & VM_ALLOC))
23689e91 2511 area->addr = kasan_unpoison_vmalloc(area->addr, requested_size,
f6e39794 2512 KASAN_VMALLOC_PROT_NORMAL);
1d96320f 2513
1da177e4 2514 return area;
1da177e4
LT
2515}
2516
c2968612
BH
2517struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
2518 unsigned long start, unsigned long end,
5e6cafc8 2519 const void *caller)
c2968612 2520{
7ca3027b
DA
2521 return __get_vm_area_node(size, 1, PAGE_SHIFT, flags, start, end,
2522 NUMA_NO_NODE, GFP_KERNEL, caller);
c2968612
BH
2523}
2524
1da177e4 2525/**
92eac168
MR
2526 * get_vm_area - reserve a contiguous kernel virtual area
2527 * @size: size of the area
2528 * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC
1da177e4 2529 *
92eac168
MR
2530 * Search an area of @size in the kernel virtual mapping area,
2531 * and reserved it for out purposes. Returns the area descriptor
2532 * on success or %NULL on failure.
a862f68a
MR
2533 *
2534 * Return: the area descriptor on success or %NULL on failure.
1da177e4
LT
2535 */
2536struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
2537{
7ca3027b
DA
2538 return __get_vm_area_node(size, 1, PAGE_SHIFT, flags,
2539 VMALLOC_START, VMALLOC_END,
00ef2d2f
DR
2540 NUMA_NO_NODE, GFP_KERNEL,
2541 __builtin_return_address(0));
23016969
CL
2542}
2543
2544struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
5e6cafc8 2545 const void *caller)
23016969 2546{
7ca3027b
DA
2547 return __get_vm_area_node(size, 1, PAGE_SHIFT, flags,
2548 VMALLOC_START, VMALLOC_END,
00ef2d2f 2549 NUMA_NO_NODE, GFP_KERNEL, caller);
1da177e4
LT
2550}
2551
e9da6e99 2552/**
92eac168
MR
2553 * find_vm_area - find a continuous kernel virtual area
2554 * @addr: base address
e9da6e99 2555 *
92eac168
MR
2556 * Search for the kernel VM area starting at @addr, and return it.
2557 * It is up to the caller to do all required locking to keep the returned
2558 * pointer valid.
a862f68a 2559 *
74640617 2560 * Return: the area descriptor on success or %NULL on failure.
e9da6e99
MS
2561 */
2562struct vm_struct *find_vm_area(const void *addr)
83342314 2563{
db64fe02 2564 struct vmap_area *va;
83342314 2565
db64fe02 2566 va = find_vmap_area((unsigned long)addr);
688fcbfc
PL
2567 if (!va)
2568 return NULL;
1da177e4 2569
688fcbfc 2570 return va->vm;
1da177e4
LT
2571}
2572
7856dfeb 2573/**
92eac168
MR
2574 * remove_vm_area - find and remove a continuous kernel virtual area
2575 * @addr: base address
7856dfeb 2576 *
92eac168
MR
2577 * Search for the kernel VM area starting at @addr, and remove it.
2578 * This function returns the found VM area, but using it is NOT safe
2579 * on SMP machines, except for its size or flags.
a862f68a 2580 *
74640617 2581 * Return: the area descriptor on success or %NULL on failure.
7856dfeb 2582 */
b3bdda02 2583struct vm_struct *remove_vm_area(const void *addr)
7856dfeb 2584{
75c59ce7
CH
2585 struct vmap_area *va;
2586 struct vm_struct *vm;
2587
5803ed29
CH
2588 might_sleep();
2589
17d3ef43
CH
2590 if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n",
2591 addr))
2592 return NULL;
2593
75c59ce7
CH
2594 va = find_unlink_vmap_area((unsigned long)addr);
2595 if (!va || !va->vm)
2596 return NULL;
2597 vm = va->vm;
17d3ef43
CH
2598
2599 debug_check_no_locks_freed(vm->addr, get_vm_area_size(vm));
2600 debug_check_no_obj_freed(vm->addr, get_vm_area_size(vm));
75c59ce7 2601 kasan_free_module_shadow(vm);
17d3ef43
CH
2602 kasan_poison_vmalloc(vm->addr, get_vm_area_size(vm));
2603
75c59ce7
CH
2604 free_unmap_vmap_area(va);
2605 return vm;
7856dfeb
AK
2606}
2607
868b104d
RE
2608static inline void set_area_direct_map(const struct vm_struct *area,
2609 int (*set_direct_map)(struct page *page))
2610{
2611 int i;
2612
121e6f32 2613 /* HUGE_VMALLOC passes small pages to set_direct_map */
868b104d
RE
2614 for (i = 0; i < area->nr_pages; i++)
2615 if (page_address(area->pages[i]))
2616 set_direct_map(area->pages[i]);
2617}
2618
9e5fa0ae
CH
2619/*
2620 * Flush the vm mapping and reset the direct map.
2621 */
2622static void vm_reset_perms(struct vm_struct *area)
868b104d 2623{
868b104d 2624 unsigned long start = ULONG_MAX, end = 0;
121e6f32 2625 unsigned int page_order = vm_area_page_order(area);
31e67340 2626 int flush_dmap = 0;
868b104d
RE
2627 int i;
2628
868b104d 2629 /*
9e5fa0ae 2630 * Find the start and end range of the direct mappings to make sure that
868b104d
RE
2631 * the vm_unmap_aliases() flush includes the direct map.
2632 */
121e6f32 2633 for (i = 0; i < area->nr_pages; i += 1U << page_order) {
8e41f872 2634 unsigned long addr = (unsigned long)page_address(area->pages[i]);
9e5fa0ae 2635
8e41f872 2636 if (addr) {
121e6f32
NP
2637 unsigned long page_size;
2638
2639 page_size = PAGE_SIZE << page_order;
868b104d 2640 start = min(addr, start);
121e6f32 2641 end = max(addr + page_size, end);
31e67340 2642 flush_dmap = 1;
868b104d
RE
2643 }
2644 }
2645
2646 /*
2647 * Set direct map to something invalid so that it won't be cached if
2648 * there are any accesses after the TLB flush, then flush the TLB and
2649 * reset the direct map permissions to the default.
2650 */
2651 set_area_direct_map(area, set_direct_map_invalid_noflush);
31e67340 2652 _vm_unmap_aliases(start, end, flush_dmap);
868b104d
RE
2653 set_area_direct_map(area, set_direct_map_default_noflush);
2654}
2655
208162f4
CH
2656static void delayed_vfree_work(struct work_struct *w)
2657{
2658 struct vfree_deferred *p = container_of(w, struct vfree_deferred, wq);
2659 struct llist_node *t, *llnode;
2660
2661 llist_for_each_safe(llnode, t, llist_del_all(&p->list))
5d3d31d6 2662 vfree(llnode);
208162f4
CH
2663}
2664
bf22e37a 2665/**
92eac168
MR
2666 * vfree_atomic - release memory allocated by vmalloc()
2667 * @addr: memory base address
bf22e37a 2668 *
92eac168
MR
2669 * This one is just like vfree() but can be called in any atomic context
2670 * except NMIs.
bf22e37a
AR
2671 */
2672void vfree_atomic(const void *addr)
2673{
01e2e839 2674 struct vfree_deferred *p = raw_cpu_ptr(&vfree_deferred);
bf22e37a 2675
01e2e839 2676 BUG_ON(in_nmi());
bf22e37a
AR
2677 kmemleak_free(addr);
2678
01e2e839
CH
2679 /*
2680 * Use raw_cpu_ptr() because this can be called from preemptible
2681 * context. Preemption is absolutely fine here, because the llist_add()
2682 * implementation is lockless, so it works even if we are adding to
2683 * another cpu's list. schedule_work() should be fine with this too.
2684 */
2685 if (addr && llist_add((struct llist_node *)addr, &p->list))
2686 schedule_work(&p->wq);
bf22e37a
AR
2687}
2688
1da177e4 2689/**
fa307474
MWO
2690 * vfree - Release memory allocated by vmalloc()
2691 * @addr: Memory base address
1da177e4 2692 *
fa307474
MWO
2693 * Free the virtually continuous memory area starting at @addr, as obtained
2694 * from one of the vmalloc() family of APIs. This will usually also free the
2695 * physical memory underlying the virtual allocation, but that memory is
2696 * reference counted, so it will not be freed until the last user goes away.
1da177e4 2697 *
fa307474 2698 * If @addr is NULL, no operation is performed.
c9fcee51 2699 *
fa307474 2700 * Context:
92eac168 2701 * May sleep if called *not* from interrupt context.
fa307474
MWO
2702 * Must not be called in NMI context (strictly speaking, it could be
2703 * if we have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling
f0953a1b 2704 * conventions for vfree() arch-dependent would be a really bad idea).
1da177e4 2705 */
b3bdda02 2706void vfree(const void *addr)
1da177e4 2707{
79311c1f
CH
2708 struct vm_struct *vm;
2709 int i;
2710
01e2e839
CH
2711 if (unlikely(in_interrupt())) {
2712 vfree_atomic(addr);
2713 return;
2714 }
89219d37 2715
01e2e839 2716 BUG_ON(in_nmi());
89219d37 2717 kmemleak_free(addr);
01e2e839 2718 might_sleep();
89219d37 2719
79311c1f
CH
2720 if (!addr)
2721 return;
2722
2723 vm = remove_vm_area(addr);
2724 if (unlikely(!vm)) {
2725 WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
2726 addr);
2727 return;
2728 }
2729
9e5fa0ae
CH
2730 if (unlikely(vm->flags & VM_FLUSH_RESET_PERMS))
2731 vm_reset_perms(vm);
79311c1f
CH
2732 for (i = 0; i < vm->nr_pages; i++) {
2733 struct page *page = vm->pages[i];
2734
2735 BUG_ON(!page);
2736 mod_memcg_page_state(page, MEMCG_VMALLOC, -1);
2737 /*
2738 * High-order allocs for huge vmallocs are split, so
2739 * can be freed as an array of order-0 allocations
2740 */
2741 __free_pages(page, 0);
2742 cond_resched();
2743 }
2744 atomic_long_sub(vm->nr_pages, &nr_vmalloc_pages);
2745 kvfree(vm->pages);
2746 kfree(vm);
1da177e4 2747}
1da177e4
LT
2748EXPORT_SYMBOL(vfree);
2749
2750/**
92eac168
MR
2751 * vunmap - release virtual mapping obtained by vmap()
2752 * @addr: memory base address
1da177e4 2753 *
92eac168
MR
2754 * Free the virtually contiguous memory area starting at @addr,
2755 * which was created from the page array passed to vmap().
1da177e4 2756 *
92eac168 2757 * Must not be called in interrupt context.
1da177e4 2758 */
b3bdda02 2759void vunmap(const void *addr)
1da177e4 2760{
79311c1f
CH
2761 struct vm_struct *vm;
2762
1da177e4 2763 BUG_ON(in_interrupt());
34754b69 2764 might_sleep();
79311c1f
CH
2765
2766 if (!addr)
2767 return;
2768 vm = remove_vm_area(addr);
2769 if (unlikely(!vm)) {
2770 WARN(1, KERN_ERR "Trying to vunmap() nonexistent vm area (%p)\n",
2771 addr);
2772 return;
2773 }
2774 kfree(vm);
1da177e4 2775}
1da177e4
LT
2776EXPORT_SYMBOL(vunmap);
2777
2778/**
92eac168
MR
2779 * vmap - map an array of pages into virtually contiguous space
2780 * @pages: array of page pointers
2781 * @count: number of pages to map
2782 * @flags: vm_area->flags
2783 * @prot: page protection for the mapping
2784 *
b944afc9
CH
2785 * Maps @count pages from @pages into contiguous kernel virtual space.
2786 * If @flags contains %VM_MAP_PUT_PAGES the ownership of the pages array itself
2787 * (which must be kmalloc or vmalloc memory) and one reference per pages in it
2788 * are transferred from the caller to vmap(), and will be freed / dropped when
2789 * vfree() is called on the return value.
a862f68a
MR
2790 *
2791 * Return: the address of the area or %NULL on failure
1da177e4
LT
2792 */
2793void *vmap(struct page **pages, unsigned int count,
92eac168 2794 unsigned long flags, pgprot_t prot)
1da177e4
LT
2795{
2796 struct vm_struct *area;
b67177ec 2797 unsigned long addr;
65ee03c4 2798 unsigned long size; /* In bytes */
1da177e4 2799
34754b69
PZ
2800 might_sleep();
2801
37f3605e
CH
2802 if (WARN_ON_ONCE(flags & VM_FLUSH_RESET_PERMS))
2803 return NULL;
2804
bd1a8fb2
PZ
2805 /*
2806 * Your top guard is someone else's bottom guard. Not having a top
2807 * guard compromises someone else's mappings too.
2808 */
2809 if (WARN_ON_ONCE(flags & VM_NO_GUARD))
2810 flags &= ~VM_NO_GUARD;
2811
ca79b0c2 2812 if (count > totalram_pages())
1da177e4
LT
2813 return NULL;
2814
65ee03c4
GJM
2815 size = (unsigned long)count << PAGE_SHIFT;
2816 area = get_vm_area_caller(size, flags, __builtin_return_address(0));
1da177e4
LT
2817 if (!area)
2818 return NULL;
23016969 2819
b67177ec
NP
2820 addr = (unsigned long)area->addr;
2821 if (vmap_pages_range(addr, addr + size, pgprot_nx(prot),
2822 pages, PAGE_SHIFT) < 0) {
1da177e4
LT
2823 vunmap(area->addr);
2824 return NULL;
2825 }
2826
c22ee528 2827 if (flags & VM_MAP_PUT_PAGES) {
b944afc9 2828 area->pages = pages;
c22ee528
ML
2829 area->nr_pages = count;
2830 }
1da177e4
LT
2831 return area->addr;
2832}
1da177e4
LT
2833EXPORT_SYMBOL(vmap);
2834
3e9a9e25
CH
2835#ifdef CONFIG_VMAP_PFN
2836struct vmap_pfn_data {
2837 unsigned long *pfns;
2838 pgprot_t prot;
2839 unsigned int idx;
2840};
2841
2842static int vmap_pfn_apply(pte_t *pte, unsigned long addr, void *private)
2843{
2844 struct vmap_pfn_data *data = private;
2845
2846 if (WARN_ON_ONCE(pfn_valid(data->pfns[data->idx])))
2847 return -EINVAL;
2848 *pte = pte_mkspecial(pfn_pte(data->pfns[data->idx++], data->prot));
2849 return 0;
2850}
2851
2852/**
2853 * vmap_pfn - map an array of PFNs into virtually contiguous space
2854 * @pfns: array of PFNs
2855 * @count: number of pages to map
2856 * @prot: page protection for the mapping
2857 *
2858 * Maps @count PFNs from @pfns into contiguous kernel virtual space and returns
2859 * the start address of the mapping.
2860 */
2861void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot)
2862{
2863 struct vmap_pfn_data data = { .pfns = pfns, .prot = pgprot_nx(prot) };
2864 struct vm_struct *area;
2865
2866 area = get_vm_area_caller(count * PAGE_SIZE, VM_IOREMAP,
2867 __builtin_return_address(0));
2868 if (!area)
2869 return NULL;
2870 if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
2871 count * PAGE_SIZE, vmap_pfn_apply, &data)) {
2872 free_vm_area(area);
2873 return NULL;
2874 }
2875 return area->addr;
2876}
2877EXPORT_SYMBOL_GPL(vmap_pfn);
2878#endif /* CONFIG_VMAP_PFN */
2879
12b9f873
UR
2880static inline unsigned int
2881vm_area_alloc_pages(gfp_t gfp, int nid,
343ab817 2882 unsigned int order, unsigned int nr_pages, struct page **pages)
12b9f873
UR
2883{
2884 unsigned int nr_allocated = 0;
ffb29b1c
CW
2885 struct page *page;
2886 int i;
12b9f873
UR
2887
2888 /*
2889 * For order-0 pages we make use of bulk allocator, if
2890 * the page array is partly or not at all populated due
2891 * to fails, fallback to a single page allocator that is
2892 * more permissive.
2893 */
c00b6b96 2894 if (!order) {
9376130c
MH
2895 gfp_t bulk_gfp = gfp & ~__GFP_NOFAIL;
2896
343ab817
URS
2897 while (nr_allocated < nr_pages) {
2898 unsigned int nr, nr_pages_request;
2899
2900 /*
2901 * A maximum allowed request is hard-coded and is 100
2902 * pages per call. That is done in order to prevent a
2903 * long preemption off scenario in the bulk-allocator
2904 * so the range is [1:100].
2905 */
2906 nr_pages_request = min(100U, nr_pages - nr_allocated);
2907
c00b6b96
CW
2908 /* memory allocation should consider mempolicy, we can't
2909 * wrongly use nearest node when nid == NUMA_NO_NODE,
2910 * otherwise memory may be allocated in only one node,
98af39d5 2911 * but mempolicy wants to alloc memory by interleaving.
c00b6b96
CW
2912 */
2913 if (IS_ENABLED(CONFIG_NUMA) && nid == NUMA_NO_NODE)
9376130c 2914 nr = alloc_pages_bulk_array_mempolicy(bulk_gfp,
c00b6b96
CW
2915 nr_pages_request,
2916 pages + nr_allocated);
2917
2918 else
9376130c 2919 nr = alloc_pages_bulk_array_node(bulk_gfp, nid,
c00b6b96
CW
2920 nr_pages_request,
2921 pages + nr_allocated);
343ab817
URS
2922
2923 nr_allocated += nr;
2924 cond_resched();
2925
2926 /*
2927 * If zero or pages were obtained partly,
2928 * fallback to a single page allocator.
2929 */
2930 if (nr != nr_pages_request)
2931 break;
2932 }
3b8000ae 2933 }
12b9f873
UR
2934
2935 /* High-order pages or fallback path if "bulk" fails. */
12b9f873 2936
ffb29b1c 2937 while (nr_allocated < nr_pages) {
dd544141
VA
2938 if (fatal_signal_pending(current))
2939 break;
2940
ffb29b1c
CW
2941 if (nid == NUMA_NO_NODE)
2942 page = alloc_pages(gfp, order);
2943 else
2944 page = alloc_pages_node(nid, gfp, order);
12b9f873
UR
2945 if (unlikely(!page))
2946 break;
3b8000ae
NP
2947 /*
2948 * Higher order allocations must be able to be treated as
2949 * indepdenent small pages by callers (as they can with
2950 * small-page vmallocs). Some drivers do their own refcounting
2951 * on vmalloc_to_page() pages, some use page->mapping,
2952 * page->lru, etc.
2953 */
2954 if (order)
2955 split_page(page, order);
12b9f873
UR
2956
2957 /*
2958 * Careful, we allocate and map page-order pages, but
2959 * tracking is done per PAGE_SIZE page so as to keep the
2960 * vm_struct APIs independent of the physical/mapped size.
2961 */
2962 for (i = 0; i < (1U << order); i++)
2963 pages[nr_allocated + i] = page + i;
2964
12e376a6 2965 cond_resched();
12b9f873
UR
2966 nr_allocated += 1U << order;
2967 }
2968
2969 return nr_allocated;
2970}
2971
e31d9eb5 2972static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
121e6f32
NP
2973 pgprot_t prot, unsigned int page_shift,
2974 int node)
1da177e4 2975{
930f036b 2976 const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
9376130c 2977 bool nofail = gfp_mask & __GFP_NOFAIL;
121e6f32
NP
2978 unsigned long addr = (unsigned long)area->addr;
2979 unsigned long size = get_vm_area_size(area);
34fe6537 2980 unsigned long array_size;
121e6f32
NP
2981 unsigned int nr_small_pages = size >> PAGE_SHIFT;
2982 unsigned int page_order;
451769eb
MH
2983 unsigned int flags;
2984 int ret;
1da177e4 2985
121e6f32 2986 array_size = (unsigned long)nr_small_pages * sizeof(struct page *);
80b1d8fd 2987
f255935b
CH
2988 if (!(gfp_mask & (GFP_DMA | GFP_DMA32)))
2989 gfp_mask |= __GFP_HIGHMEM;
1da177e4 2990
1da177e4 2991 /* Please note that the recursion is strictly bounded. */
8757d5fa 2992 if (array_size > PAGE_SIZE) {
5c1f4e69 2993 area->pages = __vmalloc_node(array_size, 1, nested_gfp, node,
f255935b 2994 area->caller);
286e1ea3 2995 } else {
5c1f4e69 2996 area->pages = kmalloc_node(array_size, nested_gfp, node);
286e1ea3 2997 }
7ea36242 2998
5c1f4e69 2999 if (!area->pages) {
c3d77172 3000 warn_alloc(gfp_mask, NULL,
f4bdfeaf
URS
3001 "vmalloc error: size %lu, failed to allocated page array size %lu",
3002 nr_small_pages * PAGE_SIZE, array_size);
cd61413b 3003 free_vm_area(area);
1da177e4
LT
3004 return NULL;
3005 }
1da177e4 3006
121e6f32 3007 set_vm_area_page_order(area, page_shift - PAGE_SHIFT);
121e6f32 3008 page_order = vm_area_page_order(area);
bf53d6f8 3009
c3d77172
URS
3010 area->nr_pages = vm_area_alloc_pages(gfp_mask | __GFP_NOWARN,
3011 node, page_order, nr_small_pages, area->pages);
5c1f4e69 3012
97105f0a 3013 atomic_long_add(area->nr_pages, &nr_vmalloc_pages);
4e5aa1f4 3014 if (gfp_mask & __GFP_ACCOUNT) {
3b8000ae 3015 int i;
4e5aa1f4 3016
3b8000ae
NP
3017 for (i = 0; i < area->nr_pages; i++)
3018 mod_memcg_page_state(area->pages[i], MEMCG_VMALLOC, 1);
4e5aa1f4 3019 }
1da177e4 3020
5c1f4e69
URS
3021 /*
3022 * If not enough pages were obtained to accomplish an
f41f036b 3023 * allocation request, free them via vfree() if any.
5c1f4e69
URS
3024 */
3025 if (area->nr_pages != nr_small_pages) {
c3d77172 3026 warn_alloc(gfp_mask, NULL,
f4bdfeaf 3027 "vmalloc error: size %lu, page order %u, failed to allocate pages",
5c1f4e69
URS
3028 area->nr_pages * PAGE_SIZE, page_order);
3029 goto fail;
3030 }
3031
451769eb
MH
3032 /*
3033 * page tables allocations ignore external gfp mask, enforce it
3034 * by the scope API
3035 */
3036 if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
3037 flags = memalloc_nofs_save();
3038 else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)
3039 flags = memalloc_noio_save();
3040
9376130c
MH
3041 do {
3042 ret = vmap_pages_range(addr, addr + size, prot, area->pages,
451769eb 3043 page_shift);
9376130c
MH
3044 if (nofail && (ret < 0))
3045 schedule_timeout_uninterruptible(1);
3046 } while (nofail && (ret < 0));
451769eb
MH
3047
3048 if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
3049 memalloc_nofs_restore(flags);
3050 else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)
3051 memalloc_noio_restore(flags);
3052
3053 if (ret < 0) {
c3d77172 3054 warn_alloc(gfp_mask, NULL,
f4bdfeaf
URS
3055 "vmalloc error: size %lu, failed to map pages",
3056 area->nr_pages * PAGE_SIZE);
1da177e4 3057 goto fail;
d70bec8c 3058 }
ed1f324c 3059
1da177e4
LT
3060 return area->addr;
3061
3062fail:
f41f036b 3063 vfree(area->addr);
1da177e4
LT
3064 return NULL;
3065}
3066
3067/**
92eac168
MR
3068 * __vmalloc_node_range - allocate virtually contiguous memory
3069 * @size: allocation size
3070 * @align: desired alignment
3071 * @start: vm area range start
3072 * @end: vm area range end
3073 * @gfp_mask: flags for the page level allocator
3074 * @prot: protection mask for the allocated pages
3075 * @vm_flags: additional vm area flags (e.g. %VM_NO_GUARD)
3076 * @node: node to use for allocation or NUMA_NO_NODE
3077 * @caller: caller's return address
3078 *
3079 * Allocate enough pages to cover @size from the page level
b7d90e7a 3080 * allocator with @gfp_mask flags. Please note that the full set of gfp
30d3f011
MH
3081 * flags are not supported. GFP_KERNEL, GFP_NOFS and GFP_NOIO are all
3082 * supported.
3083 * Zone modifiers are not supported. From the reclaim modifiers
3084 * __GFP_DIRECT_RECLAIM is required (aka GFP_NOWAIT is not supported)
3085 * and only __GFP_NOFAIL is supported (i.e. __GFP_NORETRY and
3086 * __GFP_RETRY_MAYFAIL are not supported).
3087 *
3088 * __GFP_NOWARN can be used to suppress failures messages.
b7d90e7a
MH
3089 *
3090 * Map them into contiguous kernel virtual space, using a pagetable
3091 * protection of @prot.
a862f68a
MR
3092 *
3093 * Return: the address of the area or %NULL on failure
1da177e4 3094 */
d0a21265
DR
3095void *__vmalloc_node_range(unsigned long size, unsigned long align,
3096 unsigned long start, unsigned long end, gfp_t gfp_mask,
cb9e3c29
AR
3097 pgprot_t prot, unsigned long vm_flags, int node,
3098 const void *caller)
1da177e4
LT
3099{
3100 struct vm_struct *area;
19f1c3ac 3101 void *ret;
f6e39794 3102 kasan_vmalloc_flags_t kasan_flags = KASAN_VMALLOC_NONE;
89219d37 3103 unsigned long real_size = size;
121e6f32
NP
3104 unsigned long real_align = align;
3105 unsigned int shift = PAGE_SHIFT;
1da177e4 3106
d70bec8c
NP
3107 if (WARN_ON_ONCE(!size))
3108 return NULL;
3109
3110 if ((size >> PAGE_SHIFT) > totalram_pages()) {
3111 warn_alloc(gfp_mask, NULL,
f4bdfeaf
URS
3112 "vmalloc error: size %lu, exceeds total pages",
3113 real_size);
d70bec8c 3114 return NULL;
121e6f32
NP
3115 }
3116
559089e0 3117 if (vmap_allow_huge && (vm_flags & VM_ALLOW_HUGE_VMAP)) {
121e6f32 3118 unsigned long size_per_node;
1da177e4 3119
121e6f32
NP
3120 /*
3121 * Try huge pages. Only try for PAGE_KERNEL allocations,
3122 * others like modules don't yet expect huge pages in
3123 * their allocations due to apply_to_page_range not
3124 * supporting them.
3125 */
3126
3127 size_per_node = size;
3128 if (node == NUMA_NO_NODE)
3129 size_per_node /= num_online_nodes();
3382bbee 3130 if (arch_vmap_pmd_supported(prot) && size_per_node >= PMD_SIZE)
121e6f32 3131 shift = PMD_SHIFT;
3382bbee
CL
3132 else
3133 shift = arch_vmap_pte_supported_shift(size_per_node);
3134
3135 align = max(real_align, 1UL << shift);
3136 size = ALIGN(real_size, 1UL << shift);
121e6f32
NP
3137 }
3138
3139again:
7ca3027b
DA
3140 area = __get_vm_area_node(real_size, align, shift, VM_ALLOC |
3141 VM_UNINITIALIZED | vm_flags, start, end, node,
3142 gfp_mask, caller);
d70bec8c 3143 if (!area) {
9376130c 3144 bool nofail = gfp_mask & __GFP_NOFAIL;
d70bec8c 3145 warn_alloc(gfp_mask, NULL,
9376130c
MH
3146 "vmalloc error: size %lu, vm_struct allocation failed%s",
3147 real_size, (nofail) ? ". Retrying." : "");
3148 if (nofail) {
3149 schedule_timeout_uninterruptible(1);
3150 goto again;
3151 }
de7d2b56 3152 goto fail;
d70bec8c 3153 }
1da177e4 3154
f6e39794
AK
3155 /*
3156 * Prepare arguments for __vmalloc_area_node() and
3157 * kasan_unpoison_vmalloc().
3158 */
3159 if (pgprot_val(prot) == pgprot_val(PAGE_KERNEL)) {
3160 if (kasan_hw_tags_enabled()) {
3161 /*
3162 * Modify protection bits to allow tagging.
3163 * This must be done before mapping.
3164 */
3165 prot = arch_vmap_pgprot_tagged(prot);
01d92c7f 3166
f6e39794
AK
3167 /*
3168 * Skip page_alloc poisoning and zeroing for physical
3169 * pages backing VM_ALLOC mapping. Memory is instead
3170 * poisoned and zeroed by kasan_unpoison_vmalloc().
3171 */
3172 gfp_mask |= __GFP_SKIP_KASAN_UNPOISON | __GFP_SKIP_ZERO;
3173 }
3174
3175 /* Take note that the mapping is PAGE_KERNEL. */
3176 kasan_flags |= KASAN_VMALLOC_PROT_NORMAL;
23689e91
AK
3177 }
3178
01d92c7f 3179 /* Allocate physical pages and map them into vmalloc space. */
19f1c3ac
AK
3180 ret = __vmalloc_area_node(area, gfp_mask, prot, shift, node);
3181 if (!ret)
121e6f32 3182 goto fail;
89219d37 3183
23689e91
AK
3184 /*
3185 * Mark the pages as accessible, now that they are mapped.
6c2f761d
AK
3186 * The condition for setting KASAN_VMALLOC_INIT should complement the
3187 * one in post_alloc_hook() with regards to the __GFP_SKIP_ZERO check
3188 * to make sure that memory is initialized under the same conditions.
f6e39794
AK
3189 * Tag-based KASAN modes only assign tags to normal non-executable
3190 * allocations, see __kasan_unpoison_vmalloc().
23689e91 3191 */
f6e39794 3192 kasan_flags |= KASAN_VMALLOC_VM_ALLOC;
6c2f761d
AK
3193 if (!want_init_on_free() && want_init_on_alloc(gfp_mask) &&
3194 (gfp_mask & __GFP_SKIP_ZERO))
23689e91 3195 kasan_flags |= KASAN_VMALLOC_INIT;
f6e39794 3196 /* KASAN_VMALLOC_PROT_NORMAL already set if required. */
23689e91 3197 area->addr = kasan_unpoison_vmalloc(area->addr, real_size, kasan_flags);
19f1c3ac 3198
f5252e00 3199 /*
20fc02b4
ZY
3200 * In this function, newly allocated vm_struct has VM_UNINITIALIZED
3201 * flag. It means that vm_struct is not fully initialized.
4341fa45 3202 * Now, it is fully initialized, so remove this flag here.
f5252e00 3203 */
20fc02b4 3204 clear_vm_uninitialized_flag(area);
f5252e00 3205
7ca3027b 3206 size = PAGE_ALIGN(size);
60115fa5
KW
3207 if (!(vm_flags & VM_DEFER_KMEMLEAK))
3208 kmemleak_vmalloc(area, size, gfp_mask);
89219d37 3209
19f1c3ac 3210 return area->addr;
de7d2b56
JP
3211
3212fail:
121e6f32
NP
3213 if (shift > PAGE_SHIFT) {
3214 shift = PAGE_SHIFT;
3215 align = real_align;
3216 size = real_size;
3217 goto again;
3218 }
3219
de7d2b56 3220 return NULL;
1da177e4
LT
3221}
3222
d0a21265 3223/**
92eac168
MR
3224 * __vmalloc_node - allocate virtually contiguous memory
3225 * @size: allocation size
3226 * @align: desired alignment
3227 * @gfp_mask: flags for the page level allocator
92eac168
MR
3228 * @node: node to use for allocation or NUMA_NO_NODE
3229 * @caller: caller's return address
a7c3e901 3230 *
f38fcb9c
CH
3231 * Allocate enough pages to cover @size from the page level allocator with
3232 * @gfp_mask flags. Map them into contiguous kernel virtual space.
a7c3e901 3233 *
92eac168
MR
3234 * Reclaim modifiers in @gfp_mask - __GFP_NORETRY, __GFP_RETRY_MAYFAIL
3235 * and __GFP_NOFAIL are not supported
a7c3e901 3236 *
92eac168
MR
3237 * Any use of gfp flags outside of GFP_KERNEL should be consulted
3238 * with mm people.
a862f68a
MR
3239 *
3240 * Return: pointer to the allocated memory or %NULL on error
d0a21265 3241 */
2b905948 3242void *__vmalloc_node(unsigned long size, unsigned long align,
f38fcb9c 3243 gfp_t gfp_mask, int node, const void *caller)
d0a21265
DR
3244{
3245 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
f38fcb9c 3246 gfp_mask, PAGE_KERNEL, 0, node, caller);
d0a21265 3247}
c3f896dc
CH
3248/*
3249 * This is only for performance analysis of vmalloc and stress purpose.
3250 * It is required by vmalloc test module, therefore do not use it other
3251 * than that.
3252 */
3253#ifdef CONFIG_TEST_VMALLOC_MODULE
3254EXPORT_SYMBOL_GPL(__vmalloc_node);
3255#endif
d0a21265 3256
88dca4ca 3257void *__vmalloc(unsigned long size, gfp_t gfp_mask)
930fc45a 3258{
f38fcb9c 3259 return __vmalloc_node(size, 1, gfp_mask, NUMA_NO_NODE,
23016969 3260 __builtin_return_address(0));
930fc45a 3261}
1da177e4
LT
3262EXPORT_SYMBOL(__vmalloc);
3263
3264/**
92eac168
MR
3265 * vmalloc - allocate virtually contiguous memory
3266 * @size: allocation size
3267 *
3268 * Allocate enough pages to cover @size from the page level
3269 * allocator and map them into contiguous kernel virtual space.
1da177e4 3270 *
92eac168
MR
3271 * For tight control over page level allocator and protection flags
3272 * use __vmalloc() instead.
a862f68a
MR
3273 *
3274 * Return: pointer to the allocated memory or %NULL on error
1da177e4
LT
3275 */
3276void *vmalloc(unsigned long size)
3277{
4d39d728
CH
3278 return __vmalloc_node(size, 1, GFP_KERNEL, NUMA_NO_NODE,
3279 __builtin_return_address(0));
1da177e4 3280}
1da177e4
LT
3281EXPORT_SYMBOL(vmalloc);
3282
15a64f5a 3283/**
559089e0
SL
3284 * vmalloc_huge - allocate virtually contiguous memory, allow huge pages
3285 * @size: allocation size
3286 * @gfp_mask: flags for the page level allocator
15a64f5a 3287 *
559089e0 3288 * Allocate enough pages to cover @size from the page level
15a64f5a 3289 * allocator and map them into contiguous kernel virtual space.
559089e0
SL
3290 * If @size is greater than or equal to PMD_SIZE, allow using
3291 * huge pages for the memory
15a64f5a
CI
3292 *
3293 * Return: pointer to the allocated memory or %NULL on error
3294 */
559089e0 3295void *vmalloc_huge(unsigned long size, gfp_t gfp_mask)
15a64f5a
CI
3296{
3297 return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END,
559089e0 3298 gfp_mask, PAGE_KERNEL, VM_ALLOW_HUGE_VMAP,
15a64f5a
CI
3299 NUMA_NO_NODE, __builtin_return_address(0));
3300}
559089e0 3301EXPORT_SYMBOL_GPL(vmalloc_huge);
15a64f5a 3302
e1ca7788 3303/**
92eac168
MR
3304 * vzalloc - allocate virtually contiguous memory with zero fill
3305 * @size: allocation size
3306 *
3307 * Allocate enough pages to cover @size from the page level
3308 * allocator and map them into contiguous kernel virtual space.
3309 * The memory allocated is set to zero.
3310 *
3311 * For tight control over page level allocator and protection flags
3312 * use __vmalloc() instead.
a862f68a
MR
3313 *
3314 * Return: pointer to the allocated memory or %NULL on error
e1ca7788
DY
3315 */
3316void *vzalloc(unsigned long size)
3317{
4d39d728
CH
3318 return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_ZERO, NUMA_NO_NODE,
3319 __builtin_return_address(0));
e1ca7788
DY
3320}
3321EXPORT_SYMBOL(vzalloc);
3322
83342314 3323/**
ead04089
REB
3324 * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
3325 * @size: allocation size
83342314 3326 *
ead04089
REB
3327 * The resulting memory area is zeroed so it can be mapped to userspace
3328 * without leaking data.
a862f68a
MR
3329 *
3330 * Return: pointer to the allocated memory or %NULL on error
83342314
NP
3331 */
3332void *vmalloc_user(unsigned long size)
3333{
bc84c535
RP
3334 return __vmalloc_node_range(size, SHMLBA, VMALLOC_START, VMALLOC_END,
3335 GFP_KERNEL | __GFP_ZERO, PAGE_KERNEL,
3336 VM_USERMAP, NUMA_NO_NODE,
3337 __builtin_return_address(0));
83342314
NP
3338}
3339EXPORT_SYMBOL(vmalloc_user);
3340
930fc45a 3341/**
92eac168
MR
3342 * vmalloc_node - allocate memory on a specific node
3343 * @size: allocation size
3344 * @node: numa node
930fc45a 3345 *
92eac168
MR
3346 * Allocate enough pages to cover @size from the page level
3347 * allocator and map them into contiguous kernel virtual space.
930fc45a 3348 *
92eac168
MR
3349 * For tight control over page level allocator and protection flags
3350 * use __vmalloc() instead.
a862f68a
MR
3351 *
3352 * Return: pointer to the allocated memory or %NULL on error
930fc45a
CL
3353 */
3354void *vmalloc_node(unsigned long size, int node)
3355{
f38fcb9c
CH
3356 return __vmalloc_node(size, 1, GFP_KERNEL, node,
3357 __builtin_return_address(0));
930fc45a
CL
3358}
3359EXPORT_SYMBOL(vmalloc_node);
3360
e1ca7788
DY
3361/**
3362 * vzalloc_node - allocate memory on a specific node with zero fill
3363 * @size: allocation size
3364 * @node: numa node
3365 *
3366 * Allocate enough pages to cover @size from the page level
3367 * allocator and map them into contiguous kernel virtual space.
3368 * The memory allocated is set to zero.
3369 *
a862f68a 3370 * Return: pointer to the allocated memory or %NULL on error
e1ca7788
DY
3371 */
3372void *vzalloc_node(unsigned long size, int node)
3373{
4d39d728
CH
3374 return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_ZERO, node,
3375 __builtin_return_address(0));
e1ca7788
DY
3376}
3377EXPORT_SYMBOL(vzalloc_node);
3378
0d08e0d3 3379#if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
698d0831 3380#define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL)
0d08e0d3 3381#elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
698d0831 3382#define GFP_VMALLOC32 (GFP_DMA | GFP_KERNEL)
0d08e0d3 3383#else
698d0831
MH
3384/*
3385 * 64b systems should always have either DMA or DMA32 zones. For others
3386 * GFP_DMA32 should do the right thing and use the normal zone.
3387 */
68d68ff6 3388#define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL)
0d08e0d3
AK
3389#endif
3390
1da177e4 3391/**
92eac168
MR
3392 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
3393 * @size: allocation size
1da177e4 3394 *
92eac168
MR
3395 * Allocate enough 32bit PA addressable pages to cover @size from the
3396 * page level allocator and map them into contiguous kernel virtual space.
a862f68a
MR
3397 *
3398 * Return: pointer to the allocated memory or %NULL on error
1da177e4
LT
3399 */
3400void *vmalloc_32(unsigned long size)
3401{
f38fcb9c
CH
3402 return __vmalloc_node(size, 1, GFP_VMALLOC32, NUMA_NO_NODE,
3403 __builtin_return_address(0));
1da177e4 3404}
1da177e4
LT
3405EXPORT_SYMBOL(vmalloc_32);
3406
83342314 3407/**
ead04089 3408 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
92eac168 3409 * @size: allocation size
ead04089
REB
3410 *
3411 * The resulting memory area is 32bit addressable and zeroed so it can be
3412 * mapped to userspace without leaking data.
a862f68a
MR
3413 *
3414 * Return: pointer to the allocated memory or %NULL on error
83342314
NP
3415 */
3416void *vmalloc_32_user(unsigned long size)
3417{
bc84c535
RP
3418 return __vmalloc_node_range(size, SHMLBA, VMALLOC_START, VMALLOC_END,
3419 GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
3420 VM_USERMAP, NUMA_NO_NODE,
3421 __builtin_return_address(0));
83342314
NP
3422}
3423EXPORT_SYMBOL(vmalloc_32_user);
3424
d0107eb0
KH
3425/*
3426 * small helper routine , copy contents to buf from addr.
3427 * If the page is not present, fill zero.
3428 */
3429
3430static int aligned_vread(char *buf, char *addr, unsigned long count)
3431{
3432 struct page *p;
3433 int copied = 0;
3434
3435 while (count) {
3436 unsigned long offset, length;
3437
891c49ab 3438 offset = offset_in_page(addr);
d0107eb0
KH
3439 length = PAGE_SIZE - offset;
3440 if (length > count)
3441 length = count;
3442 p = vmalloc_to_page(addr);
3443 /*
3444 * To do safe access to this _mapped_ area, we need
3445 * lock. But adding lock here means that we need to add
f0953a1b 3446 * overhead of vmalloc()/vfree() calls for this _debug_
d0107eb0
KH
3447 * interface, rarely used. Instead of that, we'll use
3448 * kmap() and get small overhead in this access function.
3449 */
3450 if (p) {
f7c8ce44 3451 /* We can expect USER0 is not used -- see vread() */
9b04c5fe 3452 void *map = kmap_atomic(p);
d0107eb0 3453 memcpy(buf, map + offset, length);
9b04c5fe 3454 kunmap_atomic(map);
d0107eb0
KH
3455 } else
3456 memset(buf, 0, length);
3457
3458 addr += length;
3459 buf += length;
3460 copied += length;
3461 count -= length;
3462 }
3463 return copied;
3464}
3465
d0107eb0 3466/**
92eac168
MR
3467 * vread() - read vmalloc area in a safe way.
3468 * @buf: buffer for reading data
3469 * @addr: vm address.
3470 * @count: number of bytes to be read.
3471 *
92eac168
MR
3472 * This function checks that addr is a valid vmalloc'ed area, and
3473 * copy data from that area to a given buffer. If the given memory range
3474 * of [addr...addr+count) includes some valid address, data is copied to
3475 * proper area of @buf. If there are memory holes, they'll be zero-filled.
3476 * IOREMAP area is treated as memory hole and no copy is done.
3477 *
3478 * If [addr...addr+count) doesn't includes any intersects with alive
3479 * vm_struct area, returns 0. @buf should be kernel's buffer.
3480 *
3481 * Note: In usual ops, vread() is never necessary because the caller
3482 * should know vmalloc() area is valid and can use memcpy().
3483 * This is for routines which have to access vmalloc area without
bbcd53c9 3484 * any information, as /proc/kcore.
a862f68a
MR
3485 *
3486 * Return: number of bytes for which addr and buf should be increased
3487 * (same number as @count) or %0 if [addr...addr+count) doesn't
3488 * include any intersection with valid vmalloc area
d0107eb0 3489 */
1da177e4
LT
3490long vread(char *buf, char *addr, unsigned long count)
3491{
e81ce85f
JK
3492 struct vmap_area *va;
3493 struct vm_struct *vm;
1da177e4 3494 char *vaddr, *buf_start = buf;
d0107eb0 3495 unsigned long buflen = count;
1da177e4
LT
3496 unsigned long n;
3497
4aff1dc4
AK
3498 addr = kasan_reset_tag(addr);
3499
1da177e4
LT
3500 /* Don't allow overflow */
3501 if ((unsigned long) addr + count < count)
3502 count = -(unsigned long) addr;
3503
e81ce85f 3504 spin_lock(&vmap_area_lock);
f181234a 3505 va = find_vmap_area_exceed_addr((unsigned long)addr);
f608788c
SD
3506 if (!va)
3507 goto finished;
f181234a
CW
3508
3509 /* no intersects with alive vmap_area */
3510 if ((unsigned long)addr + count <= va->va_start)
3511 goto finished;
3512
f608788c 3513 list_for_each_entry_from(va, &vmap_area_list, list) {
e81ce85f
JK
3514 if (!count)
3515 break;
3516
688fcbfc 3517 if (!va->vm)
e81ce85f
JK
3518 continue;
3519
3520 vm = va->vm;
3521 vaddr = (char *) vm->addr;
762216ab 3522 if (addr >= vaddr + get_vm_area_size(vm))
1da177e4
LT
3523 continue;
3524 while (addr < vaddr) {
3525 if (count == 0)
3526 goto finished;
3527 *buf = '\0';
3528 buf++;
3529 addr++;
3530 count--;
3531 }
762216ab 3532 n = vaddr + get_vm_area_size(vm) - addr;
d0107eb0
KH
3533 if (n > count)
3534 n = count;
e81ce85f 3535 if (!(vm->flags & VM_IOREMAP))
d0107eb0
KH
3536 aligned_vread(buf, addr, n);
3537 else /* IOREMAP area is treated as memory hole */
3538 memset(buf, 0, n);
3539 buf += n;
3540 addr += n;
3541 count -= n;
1da177e4
LT
3542 }
3543finished:
e81ce85f 3544 spin_unlock(&vmap_area_lock);
d0107eb0
KH
3545
3546 if (buf == buf_start)
3547 return 0;
3548 /* zero-fill memory holes */
3549 if (buf != buf_start + buflen)
3550 memset(buf, 0, buflen - (buf - buf_start));
3551
3552 return buflen;
1da177e4
LT
3553}
3554
83342314 3555/**
92eac168
MR
3556 * remap_vmalloc_range_partial - map vmalloc pages to userspace
3557 * @vma: vma to cover
3558 * @uaddr: target user address to start at
3559 * @kaddr: virtual address of vmalloc kernel memory
bdebd6a2 3560 * @pgoff: offset from @kaddr to start at
92eac168 3561 * @size: size of map area
7682486b 3562 *
92eac168 3563 * Returns: 0 for success, -Exxx on failure
83342314 3564 *
92eac168
MR
3565 * This function checks that @kaddr is a valid vmalloc'ed area,
3566 * and that it is big enough to cover the range starting at
3567 * @uaddr in @vma. Will return failure if that criteria isn't
3568 * met.
83342314 3569 *
92eac168 3570 * Similar to remap_pfn_range() (see mm/memory.c)
83342314 3571 */
e69e9d4a 3572int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr,
bdebd6a2
JH
3573 void *kaddr, unsigned long pgoff,
3574 unsigned long size)
83342314
NP
3575{
3576 struct vm_struct *area;
bdebd6a2
JH
3577 unsigned long off;
3578 unsigned long end_index;
3579
3580 if (check_shl_overflow(pgoff, PAGE_SHIFT, &off))
3581 return -EINVAL;
83342314 3582
e69e9d4a
HD
3583 size = PAGE_ALIGN(size);
3584
3585 if (!PAGE_ALIGNED(uaddr) || !PAGE_ALIGNED(kaddr))
83342314
NP
3586 return -EINVAL;
3587
e69e9d4a 3588 area = find_vm_area(kaddr);
83342314 3589 if (!area)
db64fe02 3590 return -EINVAL;
83342314 3591
fe9041c2 3592 if (!(area->flags & (VM_USERMAP | VM_DMA_COHERENT)))
db64fe02 3593 return -EINVAL;
83342314 3594
bdebd6a2
JH
3595 if (check_add_overflow(size, off, &end_index) ||
3596 end_index > get_vm_area_size(area))
db64fe02 3597 return -EINVAL;
bdebd6a2 3598 kaddr += off;
83342314 3599
83342314 3600 do {
e69e9d4a 3601 struct page *page = vmalloc_to_page(kaddr);
db64fe02
NP
3602 int ret;
3603
83342314
NP
3604 ret = vm_insert_page(vma, uaddr, page);
3605 if (ret)
3606 return ret;
3607
3608 uaddr += PAGE_SIZE;
e69e9d4a
HD
3609 kaddr += PAGE_SIZE;
3610 size -= PAGE_SIZE;
3611 } while (size > 0);
83342314 3612
1c71222e 3613 vm_flags_set(vma, VM_DONTEXPAND | VM_DONTDUMP);
83342314 3614
db64fe02 3615 return 0;
83342314 3616}
e69e9d4a
HD
3617
3618/**
92eac168
MR
3619 * remap_vmalloc_range - map vmalloc pages to userspace
3620 * @vma: vma to cover (map full range of vma)
3621 * @addr: vmalloc memory
3622 * @pgoff: number of pages into addr before first page to map
e69e9d4a 3623 *
92eac168 3624 * Returns: 0 for success, -Exxx on failure
e69e9d4a 3625 *
92eac168
MR
3626 * This function checks that addr is a valid vmalloc'ed area, and
3627 * that it is big enough to cover the vma. Will return failure if
3628 * that criteria isn't met.
e69e9d4a 3629 *
92eac168 3630 * Similar to remap_pfn_range() (see mm/memory.c)
e69e9d4a
HD
3631 */
3632int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
3633 unsigned long pgoff)
3634{
3635 return remap_vmalloc_range_partial(vma, vma->vm_start,
bdebd6a2 3636 addr, pgoff,
e69e9d4a
HD
3637 vma->vm_end - vma->vm_start);
3638}
83342314
NP
3639EXPORT_SYMBOL(remap_vmalloc_range);
3640
5f4352fb
JF
3641void free_vm_area(struct vm_struct *area)
3642{
3643 struct vm_struct *ret;
3644 ret = remove_vm_area(area->addr);
3645 BUG_ON(ret != area);
3646 kfree(area);
3647}
3648EXPORT_SYMBOL_GPL(free_vm_area);
a10aa579 3649
4f8b02b4 3650#ifdef CONFIG_SMP
ca23e405
TH
3651static struct vmap_area *node_to_va(struct rb_node *n)
3652{
4583e773 3653 return rb_entry_safe(n, struct vmap_area, rb_node);
ca23e405
TH
3654}
3655
3656/**
68ad4a33
URS
3657 * pvm_find_va_enclose_addr - find the vmap_area @addr belongs to
3658 * @addr: target address
ca23e405 3659 *
68ad4a33
URS
3660 * Returns: vmap_area if it is found. If there is no such area
3661 * the first highest(reverse order) vmap_area is returned
3662 * i.e. va->va_start < addr && va->va_end < addr or NULL
3663 * if there are no any areas before @addr.
ca23e405 3664 */
68ad4a33
URS
3665static struct vmap_area *
3666pvm_find_va_enclose_addr(unsigned long addr)
ca23e405 3667{
68ad4a33
URS
3668 struct vmap_area *va, *tmp;
3669 struct rb_node *n;
3670
3671 n = free_vmap_area_root.rb_node;
3672 va = NULL;
ca23e405
TH
3673
3674 while (n) {
68ad4a33
URS
3675 tmp = rb_entry(n, struct vmap_area, rb_node);
3676 if (tmp->va_start <= addr) {
3677 va = tmp;
3678 if (tmp->va_end >= addr)
3679 break;
3680
ca23e405 3681 n = n->rb_right;
68ad4a33
URS
3682 } else {
3683 n = n->rb_left;
3684 }
ca23e405
TH
3685 }
3686
68ad4a33 3687 return va;
ca23e405
TH
3688}
3689
3690/**
68ad4a33
URS
3691 * pvm_determine_end_from_reverse - find the highest aligned address
3692 * of free block below VMALLOC_END
3693 * @va:
3694 * in - the VA we start the search(reverse order);
3695 * out - the VA with the highest aligned end address.
799fa85d 3696 * @align: alignment for required highest address
ca23e405 3697 *
68ad4a33 3698 * Returns: determined end address within vmap_area
ca23e405 3699 */
68ad4a33
URS
3700static unsigned long
3701pvm_determine_end_from_reverse(struct vmap_area **va, unsigned long align)
ca23e405 3702{
68ad4a33 3703 unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
ca23e405
TH
3704 unsigned long addr;
3705
68ad4a33
URS
3706 if (likely(*va)) {
3707 list_for_each_entry_from_reverse((*va),
3708 &free_vmap_area_list, list) {
3709 addr = min((*va)->va_end & ~(align - 1), vmalloc_end);
3710 if ((*va)->va_start < addr)
3711 return addr;
3712 }
ca23e405
TH
3713 }
3714
68ad4a33 3715 return 0;
ca23e405
TH
3716}
3717
3718/**
3719 * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator
3720 * @offsets: array containing offset of each area
3721 * @sizes: array containing size of each area
3722 * @nr_vms: the number of areas to allocate
3723 * @align: alignment, all entries in @offsets and @sizes must be aligned to this
ca23e405
TH
3724 *
3725 * Returns: kmalloc'd vm_struct pointer array pointing to allocated
3726 * vm_structs on success, %NULL on failure
3727 *
3728 * Percpu allocator wants to use congruent vm areas so that it can
3729 * maintain the offsets among percpu areas. This function allocates
ec3f64fc
DR
3730 * congruent vmalloc areas for it with GFP_KERNEL. These areas tend to
3731 * be scattered pretty far, distance between two areas easily going up
3732 * to gigabytes. To avoid interacting with regular vmallocs, these
3733 * areas are allocated from top.
ca23e405 3734 *
68ad4a33
URS
3735 * Despite its complicated look, this allocator is rather simple. It
3736 * does everything top-down and scans free blocks from the end looking
3737 * for matching base. While scanning, if any of the areas do not fit the
3738 * base address is pulled down to fit the area. Scanning is repeated till
3739 * all the areas fit and then all necessary data structures are inserted
3740 * and the result is returned.
ca23e405
TH
3741 */
3742struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
3743 const size_t *sizes, int nr_vms,
ec3f64fc 3744 size_t align)
ca23e405
TH
3745{
3746 const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align);
3747 const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
68ad4a33 3748 struct vmap_area **vas, *va;
ca23e405
TH
3749 struct vm_struct **vms;
3750 int area, area2, last_area, term_area;
253a496d 3751 unsigned long base, start, size, end, last_end, orig_start, orig_end;
ca23e405
TH
3752 bool purged = false;
3753
ca23e405 3754 /* verify parameters and allocate data structures */
891c49ab 3755 BUG_ON(offset_in_page(align) || !is_power_of_2(align));
ca23e405
TH
3756 for (last_area = 0, area = 0; area < nr_vms; area++) {
3757 start = offsets[area];
3758 end = start + sizes[area];
3759
3760 /* is everything aligned properly? */
3761 BUG_ON(!IS_ALIGNED(offsets[area], align));
3762 BUG_ON(!IS_ALIGNED(sizes[area], align));
3763
3764 /* detect the area with the highest address */
3765 if (start > offsets[last_area])
3766 last_area = area;
3767
c568da28 3768 for (area2 = area + 1; area2 < nr_vms; area2++) {
ca23e405
TH
3769 unsigned long start2 = offsets[area2];
3770 unsigned long end2 = start2 + sizes[area2];
3771
c568da28 3772 BUG_ON(start2 < end && start < end2);
ca23e405
TH
3773 }
3774 }
3775 last_end = offsets[last_area] + sizes[last_area];
3776
3777 if (vmalloc_end - vmalloc_start < last_end) {
3778 WARN_ON(true);
3779 return NULL;
3780 }
3781
4d67d860
TM
3782 vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL);
3783 vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL);
ca23e405 3784 if (!vas || !vms)
f1db7afd 3785 goto err_free2;
ca23e405
TH
3786
3787 for (area = 0; area < nr_vms; area++) {
68ad4a33 3788 vas[area] = kmem_cache_zalloc(vmap_area_cachep, GFP_KERNEL);
ec3f64fc 3789 vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL);
ca23e405
TH
3790 if (!vas[area] || !vms[area])
3791 goto err_free;
3792 }
3793retry:
e36176be 3794 spin_lock(&free_vmap_area_lock);
ca23e405
TH
3795
3796 /* start scanning - we scan from the top, begin with the last area */
3797 area = term_area = last_area;
3798 start = offsets[area];
3799 end = start + sizes[area];
3800
68ad4a33
URS
3801 va = pvm_find_va_enclose_addr(vmalloc_end);
3802 base = pvm_determine_end_from_reverse(&va, align) - end;
ca23e405
TH
3803
3804 while (true) {
ca23e405
TH
3805 /*
3806 * base might have underflowed, add last_end before
3807 * comparing.
3808 */
68ad4a33
URS
3809 if (base + last_end < vmalloc_start + last_end)
3810 goto overflow;
ca23e405
TH
3811
3812 /*
68ad4a33 3813 * Fitting base has not been found.
ca23e405 3814 */
68ad4a33
URS
3815 if (va == NULL)
3816 goto overflow;
ca23e405 3817
5336e52c 3818 /*
d8cc323d 3819 * If required width exceeds current VA block, move
5336e52c
KS
3820 * base downwards and then recheck.
3821 */
3822 if (base + end > va->va_end) {
3823 base = pvm_determine_end_from_reverse(&va, align) - end;
3824 term_area = area;
3825 continue;
3826 }
3827
ca23e405 3828 /*
68ad4a33 3829 * If this VA does not fit, move base downwards and recheck.
ca23e405 3830 */
5336e52c 3831 if (base + start < va->va_start) {
68ad4a33
URS
3832 va = node_to_va(rb_prev(&va->rb_node));
3833 base = pvm_determine_end_from_reverse(&va, align) - end;
ca23e405
TH
3834 term_area = area;
3835 continue;
3836 }
3837
3838 /*
3839 * This area fits, move on to the previous one. If
3840 * the previous one is the terminal one, we're done.
3841 */
3842 area = (area + nr_vms - 1) % nr_vms;
3843 if (area == term_area)
3844 break;
68ad4a33 3845
ca23e405
TH
3846 start = offsets[area];
3847 end = start + sizes[area];
68ad4a33 3848 va = pvm_find_va_enclose_addr(base + end);
ca23e405 3849 }
68ad4a33 3850
ca23e405
TH
3851 /* we've found a fitting base, insert all va's */
3852 for (area = 0; area < nr_vms; area++) {
68ad4a33 3853 int ret;
ca23e405 3854
68ad4a33
URS
3855 start = base + offsets[area];
3856 size = sizes[area];
ca23e405 3857
68ad4a33
URS
3858 va = pvm_find_va_enclose_addr(start);
3859 if (WARN_ON_ONCE(va == NULL))
3860 /* It is a BUG(), but trigger recovery instead. */
3861 goto recovery;
3862
f9863be4
URS
3863 ret = adjust_va_to_fit_type(&free_vmap_area_root,
3864 &free_vmap_area_list,
3865 va, start, size);
1b23ff80 3866 if (WARN_ON_ONCE(unlikely(ret)))
68ad4a33
URS
3867 /* It is a BUG(), but trigger recovery instead. */
3868 goto recovery;
3869
68ad4a33
URS
3870 /* Allocated area. */
3871 va = vas[area];
3872 va->va_start = start;
3873 va->va_end = start + size;
68ad4a33 3874 }
ca23e405 3875
e36176be 3876 spin_unlock(&free_vmap_area_lock);
ca23e405 3877
253a496d
DA
3878 /* populate the kasan shadow space */
3879 for (area = 0; area < nr_vms; area++) {
3880 if (kasan_populate_vmalloc(vas[area]->va_start, sizes[area]))
3881 goto err_free_shadow;
253a496d
DA
3882 }
3883
ca23e405 3884 /* insert all vm's */
e36176be
URS
3885 spin_lock(&vmap_area_lock);
3886 for (area = 0; area < nr_vms; area++) {
3887 insert_vmap_area(vas[area], &vmap_area_root, &vmap_area_list);
3888
3889 setup_vmalloc_vm_locked(vms[area], vas[area], VM_ALLOC,
3645cb4a 3890 pcpu_get_vm_areas);
e36176be
URS
3891 }
3892 spin_unlock(&vmap_area_lock);
ca23e405 3893
19f1c3ac
AK
3894 /*
3895 * Mark allocated areas as accessible. Do it now as a best-effort
3896 * approach, as they can be mapped outside of vmalloc code.
23689e91
AK
3897 * With hardware tag-based KASAN, marking is skipped for
3898 * non-VM_ALLOC mappings, see __kasan_unpoison_vmalloc().
19f1c3ac 3899 */
1d96320f
AK
3900 for (area = 0; area < nr_vms; area++)
3901 vms[area]->addr = kasan_unpoison_vmalloc(vms[area]->addr,
f6e39794 3902 vms[area]->size, KASAN_VMALLOC_PROT_NORMAL);
1d96320f 3903
ca23e405
TH
3904 kfree(vas);
3905 return vms;
3906
68ad4a33 3907recovery:
e36176be
URS
3908 /*
3909 * Remove previously allocated areas. There is no
3910 * need in removing these areas from the busy tree,
3911 * because they are inserted only on the final step
3912 * and when pcpu_get_vm_areas() is success.
3913 */
68ad4a33 3914 while (area--) {
253a496d
DA
3915 orig_start = vas[area]->va_start;
3916 orig_end = vas[area]->va_end;
96e2db45
URS
3917 va = merge_or_add_vmap_area_augment(vas[area], &free_vmap_area_root,
3918 &free_vmap_area_list);
9c801f61
URS
3919 if (va)
3920 kasan_release_vmalloc(orig_start, orig_end,
3921 va->va_start, va->va_end);
68ad4a33
URS
3922 vas[area] = NULL;
3923 }
3924
3925overflow:
e36176be 3926 spin_unlock(&free_vmap_area_lock);
68ad4a33
URS
3927 if (!purged) {
3928 purge_vmap_area_lazy();
3929 purged = true;
3930
3931 /* Before "retry", check if we recover. */
3932 for (area = 0; area < nr_vms; area++) {
3933 if (vas[area])
3934 continue;
3935
3936 vas[area] = kmem_cache_zalloc(
3937 vmap_area_cachep, GFP_KERNEL);
3938 if (!vas[area])
3939 goto err_free;
3940 }
3941
3942 goto retry;
3943 }
3944
ca23e405
TH
3945err_free:
3946 for (area = 0; area < nr_vms; area++) {
68ad4a33
URS
3947 if (vas[area])
3948 kmem_cache_free(vmap_area_cachep, vas[area]);
3949
f1db7afd 3950 kfree(vms[area]);
ca23e405 3951 }
f1db7afd 3952err_free2:
ca23e405
TH
3953 kfree(vas);
3954 kfree(vms);
3955 return NULL;
253a496d
DA
3956
3957err_free_shadow:
3958 spin_lock(&free_vmap_area_lock);
3959 /*
3960 * We release all the vmalloc shadows, even the ones for regions that
3961 * hadn't been successfully added. This relies on kasan_release_vmalloc
3962 * being able to tolerate this case.
3963 */
3964 for (area = 0; area < nr_vms; area++) {
3965 orig_start = vas[area]->va_start;
3966 orig_end = vas[area]->va_end;
96e2db45
URS
3967 va = merge_or_add_vmap_area_augment(vas[area], &free_vmap_area_root,
3968 &free_vmap_area_list);
9c801f61
URS
3969 if (va)
3970 kasan_release_vmalloc(orig_start, orig_end,
3971 va->va_start, va->va_end);
253a496d
DA
3972 vas[area] = NULL;
3973 kfree(vms[area]);
3974 }
3975 spin_unlock(&free_vmap_area_lock);
3976 kfree(vas);
3977 kfree(vms);
3978 return NULL;
ca23e405
TH
3979}
3980
3981/**
3982 * pcpu_free_vm_areas - free vmalloc areas for percpu allocator
3983 * @vms: vm_struct pointer array returned by pcpu_get_vm_areas()
3984 * @nr_vms: the number of allocated areas
3985 *
3986 * Free vm_structs and the array allocated by pcpu_get_vm_areas().
3987 */
3988void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
3989{
3990 int i;
3991
3992 for (i = 0; i < nr_vms; i++)
3993 free_vm_area(vms[i]);
3994 kfree(vms);
3995}
4f8b02b4 3996#endif /* CONFIG_SMP */
a10aa579 3997
5bb1bb35 3998#ifdef CONFIG_PRINTK
98f18083
PM
3999bool vmalloc_dump_obj(void *object)
4000{
4001 struct vm_struct *vm;
4002 void *objp = (void *)PAGE_ALIGN((unsigned long)object);
4003
4004 vm = find_vm_area(objp);
4005 if (!vm)
4006 return false;
bd34dcd4
PM
4007 pr_cont(" %u-page vmalloc region starting at %#lx allocated at %pS\n",
4008 vm->nr_pages, (unsigned long)vm->addr, vm->caller);
98f18083
PM
4009 return true;
4010}
5bb1bb35 4011#endif
98f18083 4012
a10aa579
CL
4013#ifdef CONFIG_PROC_FS
4014static void *s_start(struct seq_file *m, loff_t *pos)
e36176be 4015 __acquires(&vmap_purge_lock)
d4033afd 4016 __acquires(&vmap_area_lock)
a10aa579 4017{
e36176be 4018 mutex_lock(&vmap_purge_lock);
d4033afd 4019 spin_lock(&vmap_area_lock);
e36176be 4020
3f500069 4021 return seq_list_start(&vmap_area_list, *pos);
a10aa579
CL
4022}
4023
4024static void *s_next(struct seq_file *m, void *p, loff_t *pos)
4025{
3f500069 4026 return seq_list_next(p, &vmap_area_list, pos);
a10aa579
CL
4027}
4028
4029static void s_stop(struct seq_file *m, void *p)
d4033afd 4030 __releases(&vmap_area_lock)
0a7dd4e9 4031 __releases(&vmap_purge_lock)
a10aa579 4032{
d4033afd 4033 spin_unlock(&vmap_area_lock);
0a7dd4e9 4034 mutex_unlock(&vmap_purge_lock);
a10aa579
CL
4035}
4036
a47a126a
ED
4037static void show_numa_info(struct seq_file *m, struct vm_struct *v)
4038{
e5adfffc 4039 if (IS_ENABLED(CONFIG_NUMA)) {
a47a126a 4040 unsigned int nr, *counters = m->private;
51e50b3a 4041 unsigned int step = 1U << vm_area_page_order(v);
a47a126a
ED
4042
4043 if (!counters)
4044 return;
4045
af12346c
WL
4046 if (v->flags & VM_UNINITIALIZED)
4047 return;
7e5b528b
DV
4048 /* Pair with smp_wmb() in clear_vm_uninitialized_flag() */
4049 smp_rmb();
af12346c 4050
a47a126a
ED
4051 memset(counters, 0, nr_node_ids * sizeof(unsigned int));
4052
51e50b3a
ED
4053 for (nr = 0; nr < v->nr_pages; nr += step)
4054 counters[page_to_nid(v->pages[nr])] += step;
a47a126a
ED
4055 for_each_node_state(nr, N_HIGH_MEMORY)
4056 if (counters[nr])
4057 seq_printf(m, " N%u=%u", nr, counters[nr]);
4058 }
4059}
4060
dd3b8353
URS
4061static void show_purge_info(struct seq_file *m)
4062{
dd3b8353
URS
4063 struct vmap_area *va;
4064
96e2db45
URS
4065 spin_lock(&purge_vmap_area_lock);
4066 list_for_each_entry(va, &purge_vmap_area_list, list) {
dd3b8353
URS
4067 seq_printf(m, "0x%pK-0x%pK %7ld unpurged vm_area\n",
4068 (void *)va->va_start, (void *)va->va_end,
4069 va->va_end - va->va_start);
4070 }
96e2db45 4071 spin_unlock(&purge_vmap_area_lock);
dd3b8353
URS
4072}
4073
a10aa579
CL
4074static int s_show(struct seq_file *m, void *p)
4075{
3f500069 4076 struct vmap_area *va;
d4033afd
JK
4077 struct vm_struct *v;
4078
3f500069 4079 va = list_entry(p, struct vmap_area, list);
4080
c2ce8c14 4081 /*
688fcbfc
PL
4082 * s_show can encounter race with remove_vm_area, !vm on behalf
4083 * of vmap area is being tear down or vm_map_ram allocation.
c2ce8c14 4084 */
688fcbfc 4085 if (!va->vm) {
dd3b8353 4086 seq_printf(m, "0x%pK-0x%pK %7ld vm_map_ram\n",
78c72746 4087 (void *)va->va_start, (void *)va->va_end,
dd3b8353 4088 va->va_end - va->va_start);
78c72746 4089
7cc7913e 4090 goto final;
78c72746 4091 }
d4033afd
JK
4092
4093 v = va->vm;
a10aa579 4094
45ec1690 4095 seq_printf(m, "0x%pK-0x%pK %7ld",
a10aa579
CL
4096 v->addr, v->addr + v->size, v->size);
4097
62c70bce
JP
4098 if (v->caller)
4099 seq_printf(m, " %pS", v->caller);
23016969 4100
a10aa579
CL
4101 if (v->nr_pages)
4102 seq_printf(m, " pages=%d", v->nr_pages);
4103
4104 if (v->phys_addr)
199eaa05 4105 seq_printf(m, " phys=%pa", &v->phys_addr);
a10aa579
CL
4106
4107 if (v->flags & VM_IOREMAP)
f4527c90 4108 seq_puts(m, " ioremap");
a10aa579
CL
4109
4110 if (v->flags & VM_ALLOC)
f4527c90 4111 seq_puts(m, " vmalloc");
a10aa579
CL
4112
4113 if (v->flags & VM_MAP)
f4527c90 4114 seq_puts(m, " vmap");
a10aa579
CL
4115
4116 if (v->flags & VM_USERMAP)
f4527c90 4117 seq_puts(m, " user");
a10aa579 4118
fe9041c2
CH
4119 if (v->flags & VM_DMA_COHERENT)
4120 seq_puts(m, " dma-coherent");
4121
244d63ee 4122 if (is_vmalloc_addr(v->pages))
f4527c90 4123 seq_puts(m, " vpages");
a10aa579 4124
a47a126a 4125 show_numa_info(m, v);
a10aa579 4126 seq_putc(m, '\n');
dd3b8353
URS
4127
4128 /*
96e2db45 4129 * As a final step, dump "unpurged" areas.
dd3b8353 4130 */
7cc7913e 4131final:
dd3b8353
URS
4132 if (list_is_last(&va->list, &vmap_area_list))
4133 show_purge_info(m);
4134
a10aa579
CL
4135 return 0;
4136}
4137
5f6a6a9c 4138static const struct seq_operations vmalloc_op = {
a10aa579
CL
4139 .start = s_start,
4140 .next = s_next,
4141 .stop = s_stop,
4142 .show = s_show,
4143};
5f6a6a9c 4144
5f6a6a9c
AD
4145static int __init proc_vmalloc_init(void)
4146{
fddda2b7 4147 if (IS_ENABLED(CONFIG_NUMA))
0825a6f9 4148 proc_create_seq_private("vmallocinfo", 0400, NULL,
44414d82
CH
4149 &vmalloc_op,
4150 nr_node_ids * sizeof(unsigned int), NULL);
fddda2b7 4151 else
0825a6f9 4152 proc_create_seq("vmallocinfo", 0400, NULL, &vmalloc_op);
5f6a6a9c
AD
4153 return 0;
4154}
4155module_init(proc_vmalloc_init);
db3808c1 4156
a10aa579 4157#endif
208162f4
CH
4158
4159void __init vmalloc_init(void)
4160{
4161 struct vmap_area *va;
4162 struct vm_struct *tmp;
4163 int i;
4164
4165 /*
4166 * Create the cache for vmap_area objects.
4167 */
4168 vmap_area_cachep = KMEM_CACHE(vmap_area, SLAB_PANIC);
4169
4170 for_each_possible_cpu(i) {
4171 struct vmap_block_queue *vbq;
4172 struct vfree_deferred *p;
4173
4174 vbq = &per_cpu(vmap_block_queue, i);
4175 spin_lock_init(&vbq->lock);
4176 INIT_LIST_HEAD(&vbq->free);
4177 p = &per_cpu(vfree_deferred, i);
4178 init_llist_head(&p->list);
4179 INIT_WORK(&p->wq, delayed_vfree_work);
4180 }
4181
4182 /* Import existing vmlist entries. */
4183 for (tmp = vmlist; tmp; tmp = tmp->next) {
4184 va = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
4185 if (WARN_ON_ONCE(!va))
4186 continue;
4187
4188 va->va_start = (unsigned long)tmp->addr;
4189 va->va_end = va->va_start + tmp->size;
4190 va->vm = tmp;
4191 insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
4192 }
4193
4194 /*
4195 * Now we can initialize a free vmap space.
4196 */
4197 vmap_init_free_space();
4198 vmap_initialized = true;
4199}