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