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