x86: add tizen_qemu_x86_defconfig & tizen_qemu_x86_64_defconfig
[platform/kernel/linux-rpi.git] / mm / vmalloc.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Copyright (C) 1993  Linus Torvalds
4  *  Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
5  *  SMP-safe vmalloc/vfree/ioremap, Tigran Aivazian <tigran@veritas.com>, May 2000
6  *  Major rework to support vmap/vunmap, Christoph Hellwig, SGI, August 2002
7  *  Numa awareness, Christoph Lameter, SGI, June 2005
8  *  Improving global KVA allocator, Uladzislau Rezki, Sony, May 2019
9  */
10
11 #include <linux/vmalloc.h>
12 #include <linux/mm.h>
13 #include <linux/module.h>
14 #include <linux/highmem.h>
15 #include <linux/sched/signal.h>
16 #include <linux/slab.h>
17 #include <linux/spinlock.h>
18 #include <linux/interrupt.h>
19 #include <linux/proc_fs.h>
20 #include <linux/seq_file.h>
21 #include <linux/set_memory.h>
22 #include <linux/debugobjects.h>
23 #include <linux/kallsyms.h>
24 #include <linux/list.h>
25 #include <linux/notifier.h>
26 #include <linux/rbtree.h>
27 #include <linux/xarray.h>
28 #include <linux/io.h>
29 #include <linux/rcupdate.h>
30 #include <linux/pfn.h>
31 #include <linux/kmemleak.h>
32 #include <linux/atomic.h>
33 #include <linux/compiler.h>
34 #include <linux/llist.h>
35 #include <linux/bitops.h>
36 #include <linux/rbtree_augmented.h>
37 #include <linux/overflow.h>
38 #include <linux/pgtable.h>
39 #include <linux/uaccess.h>
40 #include <linux/hugetlb.h>
41 #include <asm/tlbflush.h>
42 #include <asm/shmparam.h>
43
44 #include "internal.h"
45 #include "pgalloc-track.h"
46
47 #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
48 static unsigned int __ro_after_init ioremap_max_page_shift = BITS_PER_LONG - 1;
49
50 static int __init set_nohugeiomap(char *str)
51 {
52         ioremap_max_page_shift = PAGE_SHIFT;
53         return 0;
54 }
55 early_param("nohugeiomap", set_nohugeiomap);
56 #else /* CONFIG_HAVE_ARCH_HUGE_VMAP */
57 static const unsigned int ioremap_max_page_shift = PAGE_SHIFT;
58 #endif  /* CONFIG_HAVE_ARCH_HUGE_VMAP */
59
60 #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
61 static bool __ro_after_init vmap_allow_huge = true;
62
63 static int __init set_nohugevmalloc(char *str)
64 {
65         vmap_allow_huge = false;
66         return 0;
67 }
68 early_param("nohugevmalloc", set_nohugevmalloc);
69 #else /* CONFIG_HAVE_ARCH_HUGE_VMALLOC */
70 static const bool vmap_allow_huge = false;
71 #endif  /* CONFIG_HAVE_ARCH_HUGE_VMALLOC */
72
73 bool is_vmalloc_addr(const void *x)
74 {
75         unsigned long addr = (unsigned long)x;
76
77         return addr >= VMALLOC_START && addr < VMALLOC_END;
78 }
79 EXPORT_SYMBOL(is_vmalloc_addr);
80
81 struct vfree_deferred {
82         struct llist_head list;
83         struct work_struct wq;
84 };
85 static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred);
86
87 static void __vunmap(const void *, int);
88
89 static void free_work(struct work_struct *w)
90 {
91         struct vfree_deferred *p = container_of(w, struct vfree_deferred, wq);
92         struct llist_node *t, *llnode;
93
94         llist_for_each_safe(llnode, t, llist_del_all(&p->list))
95                 __vunmap((void *)llnode, 1);
96 }
97
98 /*** Page table manipulation functions ***/
99 static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
100                         phys_addr_t phys_addr, pgprot_t prot,
101                         unsigned int max_page_shift, pgtbl_mod_mask *mask)
102 {
103         pte_t *pte;
104         u64 pfn;
105         unsigned long size = PAGE_SIZE;
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));
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
126                 set_pte_at(&init_mm, addr, pte, pfn_pte(pfn, prot));
127                 pfn++;
128         } while (pte += PFN_DOWN(size), addr += size, addr != end);
129         *mask |= PGTBL_PTE_MODIFIED;
130         return 0;
131 }
132
133 static 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
158 static 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
177                 if (vmap_pte_range(pmd, addr, next, phys_addr, prot, max_page_shift, mask))
178                         return -ENOMEM;
179         } while (pmd++, phys_addr += (next - addr), addr = next, addr != end);
180         return 0;
181 }
182
183 static 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
208 static 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
234 static 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
259 static 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
285 static int vmap_range_noflush(unsigned long addr, unsigned long end,
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
308         if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
309                 arch_sync_kernel_mappings(start, end);
310
311         return err;
312 }
313
314 int ioremap_page_range(unsigned long addr, unsigned long end,
315                 phys_addr_t phys_addr, pgprot_t prot)
316 {
317         int err;
318
319         err = vmap_range_noflush(addr, end, phys_addr, pgprot_nx(prot),
320                                  ioremap_max_page_shift);
321         flush_cache_vmap(addr, end);
322         return err;
323 }
324
325 static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
326                              pgtbl_mod_mask *mask)
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);
335         *mask |= PGTBL_PTE_MODIFIED;
336 }
337
338 static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,
339                              pgtbl_mod_mask *mask)
340 {
341         pmd_t *pmd;
342         unsigned long next;
343         int cleared;
344
345         pmd = pmd_offset(pud, addr);
346         do {
347                 next = pmd_addr_end(addr, end);
348
349                 cleared = pmd_clear_huge(pmd);
350                 if (cleared || pmd_bad(*pmd))
351                         *mask |= PGTBL_PMD_MODIFIED;
352
353                 if (cleared)
354                         continue;
355                 if (pmd_none_or_clear_bad(pmd))
356                         continue;
357                 vunmap_pte_range(pmd, addr, next, mask);
358
359                 cond_resched();
360         } while (pmd++, addr = next, addr != end);
361 }
362
363 static void vunmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end,
364                              pgtbl_mod_mask *mask)
365 {
366         pud_t *pud;
367         unsigned long next;
368         int cleared;
369
370         pud = pud_offset(p4d, addr);
371         do {
372                 next = pud_addr_end(addr, end);
373
374                 cleared = pud_clear_huge(pud);
375                 if (cleared || pud_bad(*pud))
376                         *mask |= PGTBL_PUD_MODIFIED;
377
378                 if (cleared)
379                         continue;
380                 if (pud_none_or_clear_bad(pud))
381                         continue;
382                 vunmap_pmd_range(pud, addr, next, mask);
383         } while (pud++, addr = next, addr != end);
384 }
385
386 static void vunmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,
387                              pgtbl_mod_mask *mask)
388 {
389         p4d_t *p4d;
390         unsigned long next;
391         int cleared;
392
393         p4d = p4d_offset(pgd, addr);
394         do {
395                 next = p4d_addr_end(addr, end);
396
397                 cleared = p4d_clear_huge(p4d);
398                 if (cleared || p4d_bad(*p4d))
399                         *mask |= PGTBL_P4D_MODIFIED;
400
401                 if (cleared)
402                         continue;
403                 if (p4d_none_or_clear_bad(p4d))
404                         continue;
405                 vunmap_pud_range(p4d, addr, next, mask);
406         } while (p4d++, addr = next, addr != end);
407 }
408
409 /*
410  * vunmap_range_noflush is similar to vunmap_range, but does not
411  * flush caches or TLBs.
412  *
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).
418  *
419  * This is an internal function only. Do not use outside mm/.
420  */
421 void vunmap_range_noflush(unsigned long start, unsigned long end)
422 {
423         unsigned long next;
424         pgd_t *pgd;
425         unsigned long addr = start;
426         pgtbl_mod_mask mask = 0;
427
428         BUG_ON(addr >= end);
429         pgd = pgd_offset_k(addr);
430         do {
431                 next = pgd_addr_end(addr, end);
432                 if (pgd_bad(*pgd))
433                         mask |= PGTBL_PGD_MODIFIED;
434                 if (pgd_none_or_clear_bad(pgd))
435                         continue;
436                 vunmap_p4d_range(pgd, addr, next, &mask);
437         } while (pgd++, addr = next, addr != end);
438
439         if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
440                 arch_sync_kernel_mappings(start, end);
441 }
442
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  */
452 void 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
459 static int vmap_pages_pte_range(pmd_t *pmd, unsigned long addr,
460                 unsigned long end, pgprot_t prot, struct page **pages, int *nr,
461                 pgtbl_mod_mask *mask)
462 {
463         pte_t *pte;
464
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
470         pte = pte_alloc_kernel_track(pmd, addr, mask);
471         if (!pte)
472                 return -ENOMEM;
473         do {
474                 struct page *page = pages[*nr];
475
476                 if (WARN_ON(!pte_none(*pte)))
477                         return -EBUSY;
478                 if (WARN_ON(!page))
479                         return -ENOMEM;
480                 set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
481                 (*nr)++;
482         } while (pte++, addr += PAGE_SIZE, addr != end);
483         *mask |= PGTBL_PTE_MODIFIED;
484         return 0;
485 }
486
487 static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr,
488                 unsigned long end, pgprot_t prot, struct page **pages, int *nr,
489                 pgtbl_mod_mask *mask)
490 {
491         pmd_t *pmd;
492         unsigned long next;
493
494         pmd = pmd_alloc_track(&init_mm, pud, addr, mask);
495         if (!pmd)
496                 return -ENOMEM;
497         do {
498                 next = pmd_addr_end(addr, end);
499                 if (vmap_pages_pte_range(pmd, addr, next, prot, pages, nr, mask))
500                         return -ENOMEM;
501         } while (pmd++, addr = next, addr != end);
502         return 0;
503 }
504
505 static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr,
506                 unsigned long end, pgprot_t prot, struct page **pages, int *nr,
507                 pgtbl_mod_mask *mask)
508 {
509         pud_t *pud;
510         unsigned long next;
511
512         pud = pud_alloc_track(&init_mm, p4d, addr, mask);
513         if (!pud)
514                 return -ENOMEM;
515         do {
516                 next = pud_addr_end(addr, end);
517                 if (vmap_pages_pmd_range(pud, addr, next, prot, pages, nr, mask))
518                         return -ENOMEM;
519         } while (pud++, addr = next, addr != end);
520         return 0;
521 }
522
523 static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr,
524                 unsigned long end, pgprot_t prot, struct page **pages, int *nr,
525                 pgtbl_mod_mask *mask)
526 {
527         p4d_t *p4d;
528         unsigned long next;
529
530         p4d = p4d_alloc_track(&init_mm, pgd, addr, mask);
531         if (!p4d)
532                 return -ENOMEM;
533         do {
534                 next = p4d_addr_end(addr, end);
535                 if (vmap_pages_pud_range(p4d, addr, next, prot, pages, nr, mask))
536                         return -ENOMEM;
537         } while (p4d++, addr = next, addr != end);
538         return 0;
539 }
540
541 static int vmap_small_pages_range_noflush(unsigned long addr, unsigned long end,
542                 pgprot_t prot, struct page **pages)
543 {
544         unsigned long start = addr;
545         pgd_t *pgd;
546         unsigned long next;
547         int err = 0;
548         int nr = 0;
549         pgtbl_mod_mask mask = 0;
550
551         BUG_ON(addr >= end);
552         pgd = pgd_offset_k(addr);
553         do {
554                 next = pgd_addr_end(addr, end);
555                 if (pgd_bad(*pgd))
556                         mask |= PGTBL_PGD_MODIFIED;
557                 err = vmap_pages_p4d_range(pgd, addr, next, prot, pages, &nr, &mask);
558                 if (err)
559                         return err;
560         } while (pgd++, addr = next, addr != end);
561
562         if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
563                 arch_sync_kernel_mappings(start, end);
564
565         return 0;
566 }
567
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  */
577 int vmap_pages_range_noflush(unsigned long addr, unsigned long end,
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
603 /**
604  * vmap_pages_range - map pages to a kernel virtual address
605  * @addr: start of the VM area to map
606  * @end: end of the VM area to map (non-inclusive)
607  * @prot: page protection flags to use
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.
611  *
612  * RETURNS:
613  * 0 on success, -errno on failure.
614  */
615 static int vmap_pages_range(unsigned long addr, unsigned long end,
616                 pgprot_t prot, struct page **pages, unsigned int page_shift)
617 {
618         int err;
619
620         err = vmap_pages_range_noflush(addr, end, prot, pages, page_shift);
621         flush_cache_vmap(addr, end);
622         return err;
623 }
624
625 int is_vmalloc_or_module_addr(const void *x)
626 {
627         /*
628          * ARM, x86-64 and sparc64 put modules in a special place,
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
640 /*
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.
644  */
645 struct page *vmalloc_to_page(const void *vmalloc_addr)
646 {
647         unsigned long addr = (unsigned long) vmalloc_addr;
648         struct page *page = NULL;
649         pgd_t *pgd = pgd_offset_k(addr);
650         p4d_t *p4d;
651         pud_t *pud;
652         pmd_t *pmd;
653         pte_t *ptep, pte;
654
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          */
659         VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr));
660
661         if (pgd_none(*pgd))
662                 return NULL;
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
668         p4d = p4d_offset(pgd, addr);
669         if (p4d_none(*p4d))
670                 return NULL;
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;
675
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)))
682                 return NULL;
683
684         pmd = pmd_offset(pud, addr);
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)))
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);
697
698         return page;
699 }
700 EXPORT_SYMBOL(vmalloc_to_page);
701
702 /*
703  * Map a vmalloc()-space virtual address to the physical page frame number.
704  */
705 unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
706 {
707         return page_to_pfn(vmalloc_to_page(vmalloc_addr));
708 }
709 EXPORT_SYMBOL(vmalloc_to_pfn);
710
711
712 /*** Global kva allocator ***/
713
714 #define DEBUG_AUGMENT_PROPAGATE_CHECK 0
715 #define DEBUG_AUGMENT_LOWEST_MATCH_CHECK 0
716
717
718 static DEFINE_SPINLOCK(vmap_area_lock);
719 static DEFINE_SPINLOCK(free_vmap_area_lock);
720 /* Export for kexec only */
721 LIST_HEAD(vmap_area_list);
722 static struct rb_root vmap_area_root = RB_ROOT;
723 static bool vmap_initialized __read_mostly;
724
725 static struct rb_root purge_vmap_area_root = RB_ROOT;
726 static LIST_HEAD(purge_vmap_area_list);
727 static DEFINE_SPINLOCK(purge_vmap_area_lock);
728
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  */
735 static 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  */
741 static 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  */
753 static struct rb_root free_vmap_area_root = RB_ROOT;
754
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  */
760 static DEFINE_PER_CPU(struct vmap_area *, ne_fit_preload_node);
761
762 static __always_inline unsigned long
763 va_size(struct vmap_area *va)
764 {
765         return (va->va_end - va->va_start);
766 }
767
768 static __always_inline unsigned long
769 get_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 }
776
777 /*
778  * Gets called when remove the node and rotate.
779  */
780 static __always_inline unsigned long
781 compute_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
788 RB_DECLARE_CALLBACKS_MAX(static, free_vmap_area_rb_augment_cb,
789         struct vmap_area, rb_node, unsigned long, subtree_max_size, va_size)
790
791 static void purge_vmap_area_lazy(void);
792 static BLOCKING_NOTIFIER_HEAD(vmap_notify_list);
793 static unsigned long lazy_max_pages(void);
794
795 static atomic_long_t nr_vmalloc_pages;
796
797 unsigned long vmalloc_nr_pages(void)
798 {
799         return atomic_long_read(&nr_vmalloc_pages);
800 }
801
802 static 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
824 static struct vmap_area *__find_vmap_area(unsigned long addr)
825 {
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;
834                 else if (addr >= va->va_end)
835                         n = n->rb_right;
836                 else
837                         return va;
838         }
839
840         return NULL;
841 }
842
843 /*
844  * This function returns back addresses of parent node
845  * and its left or right link for further processing.
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.
850  */
851 static __always_inline struct rb_node **
852 find_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         }
868
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);
876
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;
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                 }
894         } while (*link);
895
896         *parent = &tmp_va->rb_node;
897         return link;
898 }
899
900 static __always_inline struct list_head *
901 get_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
918 static __always_inline void
919 link_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;
930         }
931
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         }
952
953         /* Address-sort this list */
954         list_add(&va->list, head);
955 }
956
957 static __always_inline void
958 unlink_va(struct vmap_area *va, struct rb_root *root)
959 {
960         if (WARN_ON(RB_EMPTY_NODE(&va->rb_node)))
961                 return;
962
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);
971 }
972
973 #if DEBUG_AUGMENT_PROPAGATE_CHECK
974 static void
975 augment_tree_propagate_check(void)
976 {
977         struct vmap_area *va;
978         unsigned long computed_size;
979
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);
985         }
986 }
987 #endif
988
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  */
1016 static __always_inline void
1017 augment_tree_propagate_from(struct vmap_area *va)
1018 {
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);
1025
1026 #if DEBUG_AUGMENT_PROPAGATE_CHECK
1027         augment_tree_propagate_check();
1028 #endif
1029 }
1030
1031 static void
1032 insert_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);
1039         if (link)
1040                 link_va(va, root, parent, link, head);
1041 }
1042
1043 static void
1044 insert_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
1056         if (link) {
1057                 link_va(va, root, parent, link, head);
1058                 augment_tree_propagate_from(va);
1059         }
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.
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.
1072  */
1073 static __always_inline struct vmap_area *
1074 merge_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);
1088         if (!link)
1089                 return NULL;
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
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) {
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                          */
1136                         if (merged)
1137                                 unlink_va(va, root);
1138
1139                         sibling->va_end = va->va_end;
1140
1141                         /* Free vmap_area object. */
1142                         kmem_cache_free(vmap_area_cachep, va);
1143
1144                         /* Point to the new merged area. */
1145                         va = sibling;
1146                         merged = true;
1147                 }
1148         }
1149
1150 insert:
1151         if (!merged)
1152                 link_va(va, root, parent, link, head);
1153
1154         return va;
1155 }
1156
1157 static __always_inline struct vmap_area *
1158 merge_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
1165         return va;
1166 }
1167
1168 static __always_inline bool
1169 is_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  */
1192 static __always_inline struct vmap_area *
1193 find_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                         /*
1227                          * OK. We roll back and find the first right sub-tree,
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
1248 #if DEBUG_AUGMENT_LOWEST_MATCH_CHECK
1249 #include <linux/random.h>
1250
1251 static struct vmap_area *
1252 find_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
1267 static void
1268 find_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
1286 enum 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
1294 static __always_inline enum fit_type
1295 classify_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
1320 static __always_inline int
1321 adjust_va_to_fit_type(struct vmap_area *va,
1322         unsigned long nva_start_addr, unsigned long size,
1323         enum fit_type type)
1324 {
1325         struct vmap_area *lva = NULL;
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                  */
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.
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.
1389                          */
1390                         lva = kmem_cache_alloc(vmap_area_cachep, GFP_NOWAIT);
1391                         if (!lva)
1392                                 return -1;
1393                 }
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
1412                 if (lva)        /* type == NE_FIT_TYPE */
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  */
1424 static __always_inline unsigned long
1425 __alloc_vmap_area(unsigned long size, unsigned long align,
1426         unsigned long vstart, unsigned long vend)
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
1456 #if DEBUG_AUGMENT_LOWEST_MATCH_CHECK
1457         find_vmap_lowest_match_check(size);
1458 #endif
1459
1460         return nva_start_addr;
1461 }
1462
1463 /*
1464  * Free a region of KVA allocated by alloc_vmap_area
1465  */
1466 static 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);
1479         merge_or_add_vmap_area_augment(va, &free_vmap_area_root, &free_vmap_area_list);
1480         spin_unlock(&free_vmap_area_lock);
1481 }
1482
1483 static inline void
1484 preload_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
1506 /*
1507  * Allocate a region of KVA of the specified size and alignment, within the
1508  * vstart and vend.
1509  */
1510 static 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 {
1515         struct vmap_area *va;
1516         unsigned long freed;
1517         unsigned long addr;
1518         int purged = 0;
1519         int ret;
1520
1521         BUG_ON(!size);
1522         BUG_ON(offset_in_page(size));
1523         BUG_ON(!is_power_of_2(align));
1524
1525         if (unlikely(!vmap_initialized))
1526                 return ERR_PTR(-EBUSY);
1527
1528         might_sleep();
1529         gfp_mask = gfp_mask & GFP_RECLAIM_MASK;
1530
1531         va = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node);
1532         if (unlikely(!va))
1533                 return ERR_PTR(-ENOMEM);
1534
1535         /*
1536          * Only scan the relevant parts containing pointers to other objects
1537          * to avoid false negatives.
1538          */
1539         kmemleak_scan_area(&va->rb_node, SIZE_MAX, gfp_mask);
1540
1541 retry:
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);
1545
1546         /*
1547          * If an allocation fails, the "vend" address is
1548          * returned. Therefore trigger the overflow path.
1549          */
1550         if (unlikely(addr == vend))
1551                 goto overflow;
1552
1553         va->va_start = addr;
1554         va->va_end = addr + size;
1555         va->vm = NULL;
1556
1557         spin_lock(&vmap_area_lock);
1558         insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
1559         spin_unlock(&vmap_area_lock);
1560
1561         BUG_ON(!IS_ALIGNED(va->va_start, align));
1562         BUG_ON(va->va_start < vstart);
1563         BUG_ON(va->va_end > vend);
1564
1565         ret = kasan_populate_vmalloc(addr, size);
1566         if (ret) {
1567                 free_vmap_area(va);
1568                 return ERR_PTR(ret);
1569         }
1570
1571         return va;
1572
1573 overflow:
1574         if (!purged) {
1575                 purge_vmap_area_lazy();
1576                 purged = 1;
1577                 goto retry;
1578         }
1579
1580         freed = 0;
1581         blocking_notifier_call_chain(&vmap_notify_list, 0, &freed);
1582
1583         if (freed > 0) {
1584                 purged = 0;
1585                 goto retry;
1586         }
1587
1588         if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit())
1589                 pr_warn("vmap allocation for size %lu failed: use vmalloc=<size> to increase size\n",
1590                         size);
1591
1592         kmem_cache_free(vmap_area_cachep, va);
1593         return ERR_PTR(-EBUSY);
1594 }
1595
1596 int register_vmap_purge_notifier(struct notifier_block *nb)
1597 {
1598         return blocking_notifier_chain_register(&vmap_notify_list, nb);
1599 }
1600 EXPORT_SYMBOL_GPL(register_vmap_purge_notifier);
1601
1602 int unregister_vmap_purge_notifier(struct notifier_block *nb)
1603 {
1604         return blocking_notifier_chain_unregister(&vmap_notify_list, nb);
1605 }
1606 EXPORT_SYMBOL_GPL(unregister_vmap_purge_notifier);
1607
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  */
1624 static 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
1633 static atomic_long_t vmap_lazy_nr = ATOMIC_LONG_INIT(0);
1634
1635 /*
1636  * Serialize vmap purging.  There is no actual critical section protected
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  */
1640 static DEFINE_MUTEX(vmap_purge_lock);
1641
1642 /* for per-CPU blocks */
1643 static void purge_fragmented_blocks_allcpus(void);
1644
1645 #ifdef CONFIG_X86_64
1646 /*
1647  * called before a call to iounmap() if the caller wants vm_area_struct's
1648  * immediately freed.
1649  */
1650 void set_iounmap_nonlazy(void)
1651 {
1652         atomic_long_set(&vmap_lazy_nr, lazy_max_pages()+1);
1653 }
1654 #endif /* CONFIG_X86_64 */
1655
1656 /*
1657  * Purges all lazily-freed vmap areas.
1658  */
1659 static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end)
1660 {
1661         unsigned long resched_threshold;
1662         struct list_head local_pure_list;
1663         struct vmap_area *va, *n_va;
1664
1665         lockdep_assert_held(&vmap_purge_lock);
1666
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)))
1673                 return false;
1674
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);
1682
1683         flush_tlb_kernel_range(start, end);
1684         resched_threshold = lazy_max_pages() << 1;
1685
1686         spin_lock(&free_vmap_area_lock);
1687         list_for_each_entry_safe(va, n_va, &local_pure_list, list) {
1688                 unsigned long nr = (va->va_end - va->va_start) >> PAGE_SHIFT;
1689                 unsigned long orig_start = va->va_start;
1690                 unsigned long orig_end = va->va_end;
1691
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                  */
1697                 va = merge_or_add_vmap_area_augment(va, &free_vmap_area_root,
1698                                 &free_vmap_area_list);
1699
1700                 if (!va)
1701                         continue;
1702
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);
1706
1707                 atomic_long_sub(nr, &vmap_lazy_nr);
1708
1709                 if (atomic_long_read(&vmap_lazy_nr) < resched_threshold)
1710                         cond_resched_lock(&free_vmap_area_lock);
1711         }
1712         spin_unlock(&free_vmap_area_lock);
1713         return true;
1714 }
1715
1716 /*
1717  * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
1718  * is already purging.
1719  */
1720 static void try_purge_vmap_area_lazy(void)
1721 {
1722         if (mutex_trylock(&vmap_purge_lock)) {
1723                 __purge_vmap_area_lazy(ULONG_MAX, 0);
1724                 mutex_unlock(&vmap_purge_lock);
1725         }
1726 }
1727
1728 /*
1729  * Kick off a purge of the outstanding lazy areas.
1730  */
1731 static void purge_vmap_area_lazy(void)
1732 {
1733         mutex_lock(&vmap_purge_lock);
1734         purge_fragmented_blocks_allcpus();
1735         __purge_vmap_area_lazy(ULONG_MAX, 0);
1736         mutex_unlock(&vmap_purge_lock);
1737 }
1738
1739 /*
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.
1743  */
1744 static void free_vmap_area_noflush(struct vmap_area *va)
1745 {
1746         unsigned long nr_lazy;
1747
1748         spin_lock(&vmap_area_lock);
1749         unlink_va(va, &vmap_area_root);
1750         spin_unlock(&vmap_area_lock);
1751
1752         nr_lazy = atomic_long_add_return((va->va_end - va->va_start) >>
1753                                 PAGE_SHIFT, &vmap_lazy_nr);
1754
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);
1762
1763         /* After this point, we may free va at any time */
1764         if (unlikely(nr_lazy > lazy_max_pages()))
1765                 try_purge_vmap_area_lazy();
1766 }
1767
1768 /*
1769  * Free and unmap a vmap area
1770  */
1771 static void free_unmap_vmap_area(struct vmap_area *va)
1772 {
1773         flush_cache_vunmap(va->va_start, va->va_end);
1774         vunmap_range_noflush(va->va_start, va->va_end);
1775         if (debug_pagealloc_enabled_static())
1776                 flush_tlb_kernel_range(va->va_start, va->va_end);
1777
1778         free_vmap_area_noflush(va);
1779 }
1780
1781 static 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
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() */
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))
1819
1820 #define VMAP_BLOCK_SIZE         (VMAP_BBMAP_BITS * PAGE_SIZE)
1821
1822 struct vmap_block_queue {
1823         spinlock_t lock;
1824         struct list_head free;
1825 };
1826
1827 struct vmap_block {
1828         spinlock_t lock;
1829         struct vmap_area *va;
1830         unsigned long free, dirty;
1831         unsigned long dirty_min, dirty_max; /*< dirty range */
1832         struct list_head free_list;
1833         struct rcu_head rcu_head;
1834         struct list_head purge;
1835 };
1836
1837 /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
1838 static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
1839
1840 /*
1841  * XArray of vmap blocks, indexed by address, to quickly find a vmap block
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  */
1845 static DEFINE_XARRAY(vmap_blocks);
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
1854 static 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
1861 static 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  *
1876  * Return: virtual address in a newly allocated block or ERR_PTR(-errno)
1877  */
1878 static void *new_vmap_block(unsigned int order, gfp_t gfp_mask)
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;
1885         void *vaddr;
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);
1897         if (IS_ERR(va)) {
1898                 kfree(vb);
1899                 return ERR_CAST(va);
1900         }
1901
1902         vaddr = vmap_block_vaddr(va->va_start, 0);
1903         spin_lock_init(&vb->lock);
1904         vb->va = va;
1905         /* At least something should be left free */
1906         BUG_ON(VMAP_BBMAP_BITS <= (1UL << order));
1907         vb->free = VMAP_BBMAP_BITS - (1UL << order);
1908         vb->dirty = 0;
1909         vb->dirty_min = VMAP_BBMAP_BITS;
1910         vb->dirty_max = 0;
1911         INIT_LIST_HEAD(&vb->free_list);
1912
1913         vb_idx = addr_to_vb_idx(va->va_start);
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         }
1920
1921         get_cpu_light();
1922         vbq = this_cpu_ptr(&vmap_block_queue);
1923         spin_lock(&vbq->lock);
1924         list_add_tail_rcu(&vb->free_list, &vbq->free);
1925         spin_unlock(&vbq->lock);
1926         put_cpu_light();
1927
1928         return vaddr;
1929 }
1930
1931 static void free_vmap_block(struct vmap_block *vb)
1932 {
1933         struct vmap_block *tmp;
1934
1935         tmp = xa_erase(&vmap_blocks, addr_to_vb_idx(vb->va->va_start));
1936         BUG_ON(tmp != vb);
1937
1938         free_vmap_area_noflush(vb->va);
1939         kfree_rcu(vb, rcu_head);
1940 }
1941
1942 static void purge_fragmented_blocks(int cpu)
1943 {
1944         LIST_HEAD(purge);
1945         struct vmap_block *vb;
1946         struct vmap_block *n_vb;
1947         struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
1948
1949         rcu_read_lock();
1950         list_for_each_entry_rcu(vb, &vbq->free, free_list) {
1951
1952                 if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS))
1953                         continue;
1954
1955                 spin_lock(&vb->lock);
1956                 if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) {
1957                         vb->free = 0; /* prevent further allocs after releasing lock */
1958                         vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */
1959                         vb->dirty_min = 0;
1960                         vb->dirty_max = VMAP_BBMAP_BITS;
1961                         spin_lock(&vbq->lock);
1962                         list_del_rcu(&vb->free_list);
1963                         spin_unlock(&vbq->lock);
1964                         spin_unlock(&vb->lock);
1965                         list_add_tail(&vb->purge, &purge);
1966                 } else
1967                         spin_unlock(&vb->lock);
1968         }
1969         rcu_read_unlock();
1970
1971         list_for_each_entry_safe(vb, n_vb, &purge, purge) {
1972                 list_del(&vb->purge);
1973                 free_vmap_block(vb);
1974         }
1975 }
1976
1977 static void purge_fragmented_blocks_allcpus(void)
1978 {
1979         int cpu;
1980
1981         for_each_possible_cpu(cpu)
1982                 purge_fragmented_blocks(cpu);
1983 }
1984
1985 static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
1986 {
1987         struct vmap_block_queue *vbq;
1988         struct vmap_block *vb;
1989         void *vaddr = NULL;
1990         unsigned int order;
1991
1992         BUG_ON(offset_in_page(size));
1993         BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
1994         if (WARN_ON(size == 0)) {
1995                 /*
1996                  * Allocating 0 bytes isn't what caller wants since
1997                  * get_order(0) returns funny result. Just warn and terminate
1998                  * early.
1999                  */
2000                 return NULL;
2001         }
2002         order = get_order(size);
2003
2004         rcu_read_lock();
2005         get_cpu_light();
2006         vbq = this_cpu_ptr(&vmap_block_queue);
2007         list_for_each_entry_rcu(vb, &vbq->free, free_list) {
2008                 unsigned long pages_off;
2009
2010                 spin_lock(&vb->lock);
2011                 if (vb->free < (1UL << order)) {
2012                         spin_unlock(&vb->lock);
2013                         continue;
2014                 }
2015
2016                 pages_off = VMAP_BBMAP_BITS - vb->free;
2017                 vaddr = vmap_block_vaddr(vb->va->va_start, pages_off);
2018                 vb->free -= 1UL << order;
2019                 if (vb->free == 0) {
2020                         spin_lock(&vbq->lock);
2021                         list_del_rcu(&vb->free_list);
2022                         spin_unlock(&vbq->lock);
2023                 }
2024
2025                 spin_unlock(&vb->lock);
2026                 break;
2027         }
2028
2029         put_cpu_light();
2030         rcu_read_unlock();
2031
2032         /* Allocate new block if nothing was found */
2033         if (!vaddr)
2034                 vaddr = new_vmap_block(order, gfp_mask);
2035
2036         return vaddr;
2037 }
2038
2039 static void vb_free(unsigned long addr, unsigned long size)
2040 {
2041         unsigned long offset;
2042         unsigned int order;
2043         struct vmap_block *vb;
2044
2045         BUG_ON(offset_in_page(size));
2046         BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
2047
2048         flush_cache_vunmap(addr, addr + size);
2049
2050         order = get_order(size);
2051         offset = (addr & (VMAP_BLOCK_SIZE - 1)) >> PAGE_SHIFT;
2052         vb = xa_load(&vmap_blocks, addr_to_vb_idx(addr));
2053
2054         vunmap_range_noflush(addr, addr + size);
2055
2056         if (debug_pagealloc_enabled_static())
2057                 flush_tlb_kernel_range(addr, addr + size);
2058
2059         spin_lock(&vb->lock);
2060
2061         /* Expand dirty range */
2062         vb->dirty_min = min(vb->dirty_min, offset);
2063         vb->dirty_max = max(vb->dirty_max, offset + (1UL << order));
2064
2065         vb->dirty += 1UL << order;
2066         if (vb->dirty == VMAP_BBMAP_BITS) {
2067                 BUG_ON(vb->free);
2068                 spin_unlock(&vb->lock);
2069                 free_vmap_block(vb);
2070         } else
2071                 spin_unlock(&vb->lock);
2072 }
2073
2074 static void _vm_unmap_aliases(unsigned long start, unsigned long end, int flush)
2075 {
2076         int cpu;
2077
2078         if (unlikely(!vmap_initialized))
2079                 return;
2080
2081         might_sleep();
2082
2083         for_each_possible_cpu(cpu) {
2084                 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
2085                 struct vmap_block *vb;
2086
2087                 rcu_read_lock();
2088                 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
2089                         spin_lock(&vb->lock);
2090                         if (vb->dirty && vb->dirty != VMAP_BBMAP_BITS) {
2091                                 unsigned long va_start = vb->va->va_start;
2092                                 unsigned long s, e;
2093
2094                                 s = va_start + (vb->dirty_min << PAGE_SHIFT);
2095                                 e = va_start + (vb->dirty_max << PAGE_SHIFT);
2096
2097                                 start = min(s, start);
2098                                 end   = max(e, end);
2099
2100                                 flush = 1;
2101                         }
2102                         spin_unlock(&vb->lock);
2103                 }
2104                 rcu_read_unlock();
2105         }
2106
2107         mutex_lock(&vmap_purge_lock);
2108         purge_fragmented_blocks_allcpus();
2109         if (!__purge_vmap_area_lazy(start, end) && flush)
2110                 flush_tlb_kernel_range(start, end);
2111         mutex_unlock(&vmap_purge_lock);
2112 }
2113
2114 /**
2115  * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
2116  *
2117  * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
2118  * to amortize TLB flushing overheads. What this means is that any page you
2119  * have now, may, in a former life, have been mapped into kernel virtual
2120  * address by the vmap layer and so there might be some CPUs with TLB entries
2121  * still referencing that page (additional to the regular 1:1 kernel mapping).
2122  *
2123  * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
2124  * be sure that none of the pages we have control over will have any aliases
2125  * from the vmap layer.
2126  */
2127 void vm_unmap_aliases(void)
2128 {
2129         unsigned long start = ULONG_MAX, end = 0;
2130         int flush = 0;
2131
2132         _vm_unmap_aliases(start, end, flush);
2133 }
2134 EXPORT_SYMBOL_GPL(vm_unmap_aliases);
2135
2136 /**
2137  * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
2138  * @mem: the pointer returned by vm_map_ram
2139  * @count: the count passed to that vm_map_ram call (cannot unmap partial)
2140  */
2141 void vm_unmap_ram(const void *mem, unsigned int count)
2142 {
2143         unsigned long size = (unsigned long)count << PAGE_SHIFT;
2144         unsigned long addr = (unsigned long)mem;
2145         struct vmap_area *va;
2146
2147         might_sleep();
2148         BUG_ON(!addr);
2149         BUG_ON(addr < VMALLOC_START);
2150         BUG_ON(addr > VMALLOC_END);
2151         BUG_ON(!PAGE_ALIGNED(addr));
2152
2153         kasan_poison_vmalloc(mem, size);
2154
2155         if (likely(count <= VMAP_MAX_ALLOC)) {
2156                 debug_check_no_locks_freed(mem, size);
2157                 vb_free(addr, size);
2158                 return;
2159         }
2160
2161         va = find_vmap_area(addr);
2162         BUG_ON(!va);
2163         debug_check_no_locks_freed((void *)va->va_start,
2164                                     (va->va_end - va->va_start));
2165         free_unmap_vmap_area(va);
2166 }
2167 EXPORT_SYMBOL(vm_unmap_ram);
2168
2169 /**
2170  * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
2171  * @pages: an array of pointers to the pages to be mapped
2172  * @count: number of pages
2173  * @node: prefer to allocate data structures on this node
2174  *
2175  * If you use this function for less than VMAP_MAX_ALLOC pages, it could be
2176  * faster than vmap so it's good.  But if you mix long-life and short-life
2177  * objects with vm_map_ram(), it could consume lots of address space through
2178  * fragmentation (especially on a 32bit machine).  You could see failures in
2179  * the end.  Please use this function for short-lived objects.
2180  *
2181  * Returns: a pointer to the address that has been mapped, or %NULL on failure
2182  */
2183 void *vm_map_ram(struct page **pages, unsigned int count, int node)
2184 {
2185         unsigned long size = (unsigned long)count << PAGE_SHIFT;
2186         unsigned long addr;
2187         void *mem;
2188
2189         if (likely(count <= VMAP_MAX_ALLOC)) {
2190                 mem = vb_alloc(size, GFP_KERNEL);
2191                 if (IS_ERR(mem))
2192                         return NULL;
2193                 addr = (unsigned long)mem;
2194         } else {
2195                 struct vmap_area *va;
2196                 va = alloc_vmap_area(size, PAGE_SIZE,
2197                                 VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
2198                 if (IS_ERR(va))
2199                         return NULL;
2200
2201                 addr = va->va_start;
2202                 mem = (void *)addr;
2203         }
2204
2205         kasan_unpoison_vmalloc(mem, size);
2206
2207         if (vmap_pages_range(addr, addr + size, PAGE_KERNEL,
2208                                 pages, PAGE_SHIFT) < 0) {
2209                 vm_unmap_ram(mem, count);
2210                 return NULL;
2211         }
2212
2213         return mem;
2214 }
2215 EXPORT_SYMBOL(vm_map_ram);
2216
2217 static struct vm_struct *vmlist __initdata;
2218
2219 static inline unsigned int vm_area_page_order(struct vm_struct *vm)
2220 {
2221 #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
2222         return vm->page_order;
2223 #else
2224         return 0;
2225 #endif
2226 }
2227
2228 static inline void set_vm_area_page_order(struct vm_struct *vm, unsigned int order)
2229 {
2230 #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
2231         vm->page_order = order;
2232 #else
2233         BUG_ON(order != 0);
2234 #endif
2235 }
2236
2237 /**
2238  * vm_area_add_early - add vmap area early during boot
2239  * @vm: vm_struct to add
2240  *
2241  * This function is used to add fixed kernel vm area to vmlist before
2242  * vmalloc_init() is called.  @vm->addr, @vm->size, and @vm->flags
2243  * should contain proper values and the other fields should be zero.
2244  *
2245  * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
2246  */
2247 void __init vm_area_add_early(struct vm_struct *vm)
2248 {
2249         struct vm_struct *tmp, **p;
2250
2251         BUG_ON(vmap_initialized);
2252         for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
2253                 if (tmp->addr >= vm->addr) {
2254                         BUG_ON(tmp->addr < vm->addr + vm->size);
2255                         break;
2256                 } else
2257                         BUG_ON(tmp->addr + tmp->size > vm->addr);
2258         }
2259         vm->next = *p;
2260         *p = vm;
2261 }
2262
2263 /**
2264  * vm_area_register_early - register vmap area early during boot
2265  * @vm: vm_struct to register
2266  * @align: requested alignment
2267  *
2268  * This function is used to register kernel vm area before
2269  * vmalloc_init() is called.  @vm->size and @vm->flags should contain
2270  * proper values on entry and other fields should be zero.  On return,
2271  * vm->addr contains the allocated address.
2272  *
2273  * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
2274  */
2275 void __init vm_area_register_early(struct vm_struct *vm, size_t align)
2276 {
2277         static size_t vm_init_off __initdata;
2278         unsigned long addr;
2279
2280         addr = ALIGN(VMALLOC_START + vm_init_off, align);
2281         vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START;
2282
2283         vm->addr = (void *)addr;
2284
2285         vm_area_add_early(vm);
2286 }
2287
2288 static void vmap_init_free_space(void)
2289 {
2290         unsigned long vmap_start = 1;
2291         const unsigned long vmap_end = ULONG_MAX;
2292         struct vmap_area *busy, *free;
2293
2294         /*
2295          *     B     F     B     B     B     F
2296          * -|-----|.....|-----|-----|-----|.....|-
2297          *  |           The KVA space           |
2298          *  |<--------------------------------->|
2299          */
2300         list_for_each_entry(busy, &vmap_area_list, list) {
2301                 if (busy->va_start - vmap_start > 0) {
2302                         free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
2303                         if (!WARN_ON_ONCE(!free)) {
2304                                 free->va_start = vmap_start;
2305                                 free->va_end = busy->va_start;
2306
2307                                 insert_vmap_area_augment(free, NULL,
2308                                         &free_vmap_area_root,
2309                                                 &free_vmap_area_list);
2310                         }
2311                 }
2312
2313                 vmap_start = busy->va_end;
2314         }
2315
2316         if (vmap_end - vmap_start > 0) {
2317                 free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
2318                 if (!WARN_ON_ONCE(!free)) {
2319                         free->va_start = vmap_start;
2320                         free->va_end = vmap_end;
2321
2322                         insert_vmap_area_augment(free, NULL,
2323                                 &free_vmap_area_root,
2324                                         &free_vmap_area_list);
2325                 }
2326         }
2327 }
2328
2329 void __init vmalloc_init(void)
2330 {
2331         struct vmap_area *va;
2332         struct vm_struct *tmp;
2333         int i;
2334
2335         /*
2336          * Create the cache for vmap_area objects.
2337          */
2338         vmap_area_cachep = KMEM_CACHE(vmap_area, SLAB_PANIC);
2339
2340         for_each_possible_cpu(i) {
2341                 struct vmap_block_queue *vbq;
2342                 struct vfree_deferred *p;
2343
2344                 vbq = &per_cpu(vmap_block_queue, i);
2345                 spin_lock_init(&vbq->lock);
2346                 INIT_LIST_HEAD(&vbq->free);
2347                 p = &per_cpu(vfree_deferred, i);
2348                 init_llist_head(&p->list);
2349                 INIT_WORK(&p->wq, free_work);
2350         }
2351
2352         /* Import existing vmlist entries. */
2353         for (tmp = vmlist; tmp; tmp = tmp->next) {
2354                 va = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
2355                 if (WARN_ON_ONCE(!va))
2356                         continue;
2357
2358                 va->va_start = (unsigned long)tmp->addr;
2359                 va->va_end = va->va_start + tmp->size;
2360                 va->vm = tmp;
2361                 insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
2362         }
2363
2364         /*
2365          * Now we can initialize a free vmap space.
2366          */
2367         vmap_init_free_space();
2368         vmap_initialized = true;
2369 }
2370
2371 static inline void setup_vmalloc_vm_locked(struct vm_struct *vm,
2372         struct vmap_area *va, unsigned long flags, const void *caller)
2373 {
2374         vm->flags = flags;
2375         vm->addr = (void *)va->va_start;
2376         vm->size = va->va_end - va->va_start;
2377         vm->caller = caller;
2378         va->vm = vm;
2379 }
2380
2381 static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
2382                               unsigned long flags, const void *caller)
2383 {
2384         spin_lock(&vmap_area_lock);
2385         setup_vmalloc_vm_locked(vm, va, flags, caller);
2386         spin_unlock(&vmap_area_lock);
2387 }
2388
2389 static void clear_vm_uninitialized_flag(struct vm_struct *vm)
2390 {
2391         /*
2392          * Before removing VM_UNINITIALIZED,
2393          * we should make sure that vm has proper values.
2394          * Pair with smp_rmb() in show_numa_info().
2395          */
2396         smp_wmb();
2397         vm->flags &= ~VM_UNINITIALIZED;
2398 }
2399
2400 static struct vm_struct *__get_vm_area_node(unsigned long size,
2401                 unsigned long align, unsigned long shift, unsigned long flags,
2402                 unsigned long start, unsigned long end, int node,
2403                 gfp_t gfp_mask, const void *caller)
2404 {
2405         struct vmap_area *va;
2406         struct vm_struct *area;
2407         unsigned long requested_size = size;
2408
2409         BUG_ON(in_interrupt());
2410         size = ALIGN(size, 1ul << shift);
2411         if (unlikely(!size))
2412                 return NULL;
2413
2414         if (flags & VM_IOREMAP)
2415                 align = 1ul << clamp_t(int, get_count_order_long(size),
2416                                        PAGE_SHIFT, IOREMAP_MAX_ORDER);
2417
2418         area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
2419         if (unlikely(!area))
2420                 return NULL;
2421
2422         if (!(flags & VM_NO_GUARD))
2423                 size += PAGE_SIZE;
2424
2425         va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
2426         if (IS_ERR(va)) {
2427                 kfree(area);
2428                 return NULL;
2429         }
2430
2431         kasan_unpoison_vmalloc((void *)va->va_start, requested_size);
2432
2433         setup_vmalloc_vm(area, va, flags, caller);
2434
2435         return area;
2436 }
2437
2438 struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
2439                                        unsigned long start, unsigned long end,
2440                                        const void *caller)
2441 {
2442         return __get_vm_area_node(size, 1, PAGE_SHIFT, flags, start, end,
2443                                   NUMA_NO_NODE, GFP_KERNEL, caller);
2444 }
2445
2446 /**
2447  * get_vm_area - reserve a contiguous kernel virtual area
2448  * @size:        size of the area
2449  * @flags:       %VM_IOREMAP for I/O mappings or VM_ALLOC
2450  *
2451  * Search an area of @size in the kernel virtual mapping area,
2452  * and reserved it for out purposes.  Returns the area descriptor
2453  * on success or %NULL on failure.
2454  *
2455  * Return: the area descriptor on success or %NULL on failure.
2456  */
2457 struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
2458 {
2459         return __get_vm_area_node(size, 1, PAGE_SHIFT, flags,
2460                                   VMALLOC_START, VMALLOC_END,
2461                                   NUMA_NO_NODE, GFP_KERNEL,
2462                                   __builtin_return_address(0));
2463 }
2464
2465 struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
2466                                 const void *caller)
2467 {
2468         return __get_vm_area_node(size, 1, PAGE_SHIFT, flags,
2469                                   VMALLOC_START, VMALLOC_END,
2470                                   NUMA_NO_NODE, GFP_KERNEL, caller);
2471 }
2472
2473 /**
2474  * find_vm_area - find a continuous kernel virtual area
2475  * @addr:         base address
2476  *
2477  * Search for the kernel VM area starting at @addr, and return it.
2478  * It is up to the caller to do all required locking to keep the returned
2479  * pointer valid.
2480  *
2481  * Return: the area descriptor on success or %NULL on failure.
2482  */
2483 struct vm_struct *find_vm_area(const void *addr)
2484 {
2485         struct vmap_area *va;
2486
2487         va = find_vmap_area((unsigned long)addr);
2488         if (!va)
2489                 return NULL;
2490
2491         return va->vm;
2492 }
2493
2494 /**
2495  * remove_vm_area - find and remove a continuous kernel virtual area
2496  * @addr:           base address
2497  *
2498  * Search for the kernel VM area starting at @addr, and remove it.
2499  * This function returns the found VM area, but using it is NOT safe
2500  * on SMP machines, except for its size or flags.
2501  *
2502  * Return: the area descriptor on success or %NULL on failure.
2503  */
2504 struct vm_struct *remove_vm_area(const void *addr)
2505 {
2506         struct vmap_area *va;
2507
2508         might_sleep();
2509
2510         spin_lock(&vmap_area_lock);
2511         va = __find_vmap_area((unsigned long)addr);
2512         if (va && va->vm) {
2513                 struct vm_struct *vm = va->vm;
2514
2515                 va->vm = NULL;
2516                 spin_unlock(&vmap_area_lock);
2517
2518                 kasan_free_shadow(vm);
2519                 free_unmap_vmap_area(va);
2520
2521                 return vm;
2522         }
2523
2524         spin_unlock(&vmap_area_lock);
2525         return NULL;
2526 }
2527
2528 static inline void set_area_direct_map(const struct vm_struct *area,
2529                                        int (*set_direct_map)(struct page *page))
2530 {
2531         int i;
2532
2533         /* HUGE_VMALLOC passes small pages to set_direct_map */
2534         for (i = 0; i < area->nr_pages; i++)
2535                 if (page_address(area->pages[i]))
2536                         set_direct_map(area->pages[i]);
2537 }
2538
2539 /* Handle removing and resetting vm mappings related to the vm_struct. */
2540 static void vm_remove_mappings(struct vm_struct *area, int deallocate_pages)
2541 {
2542         unsigned long start = ULONG_MAX, end = 0;
2543         unsigned int page_order = vm_area_page_order(area);
2544         int flush_reset = area->flags & VM_FLUSH_RESET_PERMS;
2545         int flush_dmap = 0;
2546         int i;
2547
2548         remove_vm_area(area->addr);
2549
2550         /* If this is not VM_FLUSH_RESET_PERMS memory, no need for the below. */
2551         if (!flush_reset)
2552                 return;
2553
2554         /*
2555          * If not deallocating pages, just do the flush of the VM area and
2556          * return.
2557          */
2558         if (!deallocate_pages) {
2559                 vm_unmap_aliases();
2560                 return;
2561         }
2562
2563         /*
2564          * If execution gets here, flush the vm mapping and reset the direct
2565          * map. Find the start and end range of the direct mappings to make sure
2566          * the vm_unmap_aliases() flush includes the direct map.
2567          */
2568         for (i = 0; i < area->nr_pages; i += 1U << page_order) {
2569                 unsigned long addr = (unsigned long)page_address(area->pages[i]);
2570                 if (addr) {
2571                         unsigned long page_size;
2572
2573                         page_size = PAGE_SIZE << page_order;
2574                         start = min(addr, start);
2575                         end = max(addr + page_size, end);
2576                         flush_dmap = 1;
2577                 }
2578         }
2579
2580         /*
2581          * Set direct map to something invalid so that it won't be cached if
2582          * there are any accesses after the TLB flush, then flush the TLB and
2583          * reset the direct map permissions to the default.
2584          */
2585         set_area_direct_map(area, set_direct_map_invalid_noflush);
2586         _vm_unmap_aliases(start, end, flush_dmap);
2587         set_area_direct_map(area, set_direct_map_default_noflush);
2588 }
2589
2590 static void __vunmap(const void *addr, int deallocate_pages)
2591 {
2592         struct vm_struct *area;
2593
2594         if (!addr)
2595                 return;
2596
2597         if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n",
2598                         addr))
2599                 return;
2600
2601         area = find_vm_area(addr);
2602         if (unlikely(!area)) {
2603                 WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
2604                                 addr);
2605                 return;
2606         }
2607
2608         debug_check_no_locks_freed(area->addr, get_vm_area_size(area));
2609         debug_check_no_obj_freed(area->addr, get_vm_area_size(area));
2610
2611         kasan_poison_vmalloc(area->addr, get_vm_area_size(area));
2612
2613         vm_remove_mappings(area, deallocate_pages);
2614
2615         if (deallocate_pages) {
2616                 unsigned int page_order = vm_area_page_order(area);
2617                 int i;
2618
2619                 for (i = 0; i < area->nr_pages; i += 1U << page_order) {
2620                         struct page *page = area->pages[i];
2621
2622                         BUG_ON(!page);
2623                         __free_pages(page, page_order);
2624                         cond_resched();
2625                 }
2626                 atomic_long_sub(area->nr_pages, &nr_vmalloc_pages);
2627
2628                 kvfree(area->pages);
2629         }
2630
2631         kfree(area);
2632 }
2633
2634 static inline void __vfree_deferred(const void *addr)
2635 {
2636         /*
2637          * Use raw_cpu_ptr() because this can be called from preemptible
2638          * context. Preemption is absolutely fine here, because the llist_add()
2639          * implementation is lockless, so it works even if we are adding to
2640          * another cpu's list. schedule_work() should be fine with this too.
2641          */
2642         struct vfree_deferred *p = raw_cpu_ptr(&vfree_deferred);
2643
2644         if (llist_add((struct llist_node *)addr, &p->list))
2645                 schedule_work(&p->wq);
2646 }
2647
2648 /**
2649  * vfree_atomic - release memory allocated by vmalloc()
2650  * @addr:         memory base address
2651  *
2652  * This one is just like vfree() but can be called in any atomic context
2653  * except NMIs.
2654  */
2655 void vfree_atomic(const void *addr)
2656 {
2657         BUG_ON(in_nmi());
2658
2659         kmemleak_free(addr);
2660
2661         if (!addr)
2662                 return;
2663         __vfree_deferred(addr);
2664 }
2665
2666 static void __vfree(const void *addr)
2667 {
2668         if (unlikely(in_interrupt()))
2669                 __vfree_deferred(addr);
2670         else
2671                 __vunmap(addr, 1);
2672 }
2673
2674 /**
2675  * vfree - Release memory allocated by vmalloc()
2676  * @addr:  Memory base address
2677  *
2678  * Free the virtually continuous memory area starting at @addr, as obtained
2679  * from one of the vmalloc() family of APIs.  This will usually also free the
2680  * physical memory underlying the virtual allocation, but that memory is
2681  * reference counted, so it will not be freed until the last user goes away.
2682  *
2683  * If @addr is NULL, no operation is performed.
2684  *
2685  * Context:
2686  * May sleep if called *not* from interrupt context.
2687  * Must not be called in NMI context (strictly speaking, it could be
2688  * if we have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling
2689  * conventions for vfree() arch-dependent would be a really bad idea).
2690  */
2691 void vfree(const void *addr)
2692 {
2693         BUG_ON(in_nmi());
2694
2695         kmemleak_free(addr);
2696
2697         might_sleep_if(!in_interrupt());
2698
2699         if (!addr)
2700                 return;
2701
2702         __vfree(addr);
2703 }
2704 EXPORT_SYMBOL(vfree);
2705
2706 /**
2707  * vunmap - release virtual mapping obtained by vmap()
2708  * @addr:   memory base address
2709  *
2710  * Free the virtually contiguous memory area starting at @addr,
2711  * which was created from the page array passed to vmap().
2712  *
2713  * Must not be called in interrupt context.
2714  */
2715 void vunmap(const void *addr)
2716 {
2717         BUG_ON(in_interrupt());
2718         might_sleep();
2719         if (addr)
2720                 __vunmap(addr, 0);
2721 }
2722 EXPORT_SYMBOL(vunmap);
2723
2724 /**
2725  * vmap - map an array of pages into virtually contiguous space
2726  * @pages: array of page pointers
2727  * @count: number of pages to map
2728  * @flags: vm_area->flags
2729  * @prot: page protection for the mapping
2730  *
2731  * Maps @count pages from @pages into contiguous kernel virtual space.
2732  * If @flags contains %VM_MAP_PUT_PAGES the ownership of the pages array itself
2733  * (which must be kmalloc or vmalloc memory) and one reference per pages in it
2734  * are transferred from the caller to vmap(), and will be freed / dropped when
2735  * vfree() is called on the return value.
2736  *
2737  * Return: the address of the area or %NULL on failure
2738  */
2739 void *vmap(struct page **pages, unsigned int count,
2740            unsigned long flags, pgprot_t prot)
2741 {
2742         struct vm_struct *area;
2743         unsigned long addr;
2744         unsigned long size;             /* In bytes */
2745
2746         might_sleep();
2747
2748         if (count > totalram_pages())
2749                 return NULL;
2750
2751         size = (unsigned long)count << PAGE_SHIFT;
2752         area = get_vm_area_caller(size, flags, __builtin_return_address(0));
2753         if (!area)
2754                 return NULL;
2755
2756         addr = (unsigned long)area->addr;
2757         if (vmap_pages_range(addr, addr + size, pgprot_nx(prot),
2758                                 pages, PAGE_SHIFT) < 0) {
2759                 vunmap(area->addr);
2760                 return NULL;
2761         }
2762
2763         if (flags & VM_MAP_PUT_PAGES) {
2764                 area->pages = pages;
2765                 area->nr_pages = count;
2766         }
2767         return area->addr;
2768 }
2769 EXPORT_SYMBOL(vmap);
2770
2771 #ifdef CONFIG_VMAP_PFN
2772 struct vmap_pfn_data {
2773         unsigned long   *pfns;
2774         pgprot_t        prot;
2775         unsigned int    idx;
2776 };
2777
2778 static int vmap_pfn_apply(pte_t *pte, unsigned long addr, void *private)
2779 {
2780         struct vmap_pfn_data *data = private;
2781
2782         if (WARN_ON_ONCE(pfn_valid(data->pfns[data->idx])))
2783                 return -EINVAL;
2784         *pte = pte_mkspecial(pfn_pte(data->pfns[data->idx++], data->prot));
2785         return 0;
2786 }
2787
2788 /**
2789  * vmap_pfn - map an array of PFNs into virtually contiguous space
2790  * @pfns: array of PFNs
2791  * @count: number of pages to map
2792  * @prot: page protection for the mapping
2793  *
2794  * Maps @count PFNs from @pfns into contiguous kernel virtual space and returns
2795  * the start address of the mapping.
2796  */
2797 void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot)
2798 {
2799         struct vmap_pfn_data data = { .pfns = pfns, .prot = pgprot_nx(prot) };
2800         struct vm_struct *area;
2801
2802         area = get_vm_area_caller(count * PAGE_SIZE, VM_IOREMAP,
2803                         __builtin_return_address(0));
2804         if (!area)
2805                 return NULL;
2806         if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
2807                         count * PAGE_SIZE, vmap_pfn_apply, &data)) {
2808                 free_vm_area(area);
2809                 return NULL;
2810         }
2811         return area->addr;
2812 }
2813 EXPORT_SYMBOL_GPL(vmap_pfn);
2814 #endif /* CONFIG_VMAP_PFN */
2815
2816 static inline unsigned int
2817 vm_area_alloc_pages(gfp_t gfp, int nid,
2818                 unsigned int order, unsigned int nr_pages, struct page **pages)
2819 {
2820         unsigned int nr_allocated = 0;
2821         struct page *page;
2822         int i;
2823
2824         /*
2825          * For order-0 pages we make use of bulk allocator, if
2826          * the page array is partly or not at all populated due
2827          * to fails, fallback to a single page allocator that is
2828          * more permissive.
2829          */
2830         if (!order && nid != NUMA_NO_NODE) {
2831                 while (nr_allocated < nr_pages) {
2832                         unsigned int nr, nr_pages_request;
2833
2834                         /*
2835                          * A maximum allowed request is hard-coded and is 100
2836                          * pages per call. That is done in order to prevent a
2837                          * long preemption off scenario in the bulk-allocator
2838                          * so the range is [1:100].
2839                          */
2840                         nr_pages_request = min(100U, nr_pages - nr_allocated);
2841
2842                         nr = alloc_pages_bulk_array_node(gfp, nid,
2843                                 nr_pages_request, pages + nr_allocated);
2844
2845                         nr_allocated += nr;
2846                         cond_resched();
2847
2848                         /*
2849                          * If zero or pages were obtained partly,
2850                          * fallback to a single page allocator.
2851                          */
2852                         if (nr != nr_pages_request)
2853                                 break;
2854                 }
2855         } else if (order)
2856                 /*
2857                  * Compound pages required for remap_vmalloc_page if
2858                  * high-order pages.
2859                  */
2860                 gfp |= __GFP_COMP;
2861
2862         /* High-order pages or fallback path if "bulk" fails. */
2863
2864         while (nr_allocated < nr_pages) {
2865                 if (nid == NUMA_NO_NODE)
2866                         page = alloc_pages(gfp, order);
2867                 else
2868                         page = alloc_pages_node(nid, gfp, order);
2869                 if (unlikely(!page))
2870                         break;
2871
2872                 /*
2873                  * Careful, we allocate and map page-order pages, but
2874                  * tracking is done per PAGE_SIZE page so as to keep the
2875                  * vm_struct APIs independent of the physical/mapped size.
2876                  */
2877                 for (i = 0; i < (1U << order); i++)
2878                         pages[nr_allocated + i] = page + i;
2879
2880                 cond_resched();
2881                 nr_allocated += 1U << order;
2882         }
2883
2884         return nr_allocated;
2885 }
2886
2887 static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
2888                                  pgprot_t prot, unsigned int page_shift,
2889                                  int node)
2890 {
2891         const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
2892         unsigned long addr = (unsigned long)area->addr;
2893         unsigned long size = get_vm_area_size(area);
2894         unsigned long array_size;
2895         unsigned int nr_small_pages = size >> PAGE_SHIFT;
2896         unsigned int page_order;
2897
2898         array_size = (unsigned long)nr_small_pages * sizeof(struct page *);
2899         gfp_mask |= __GFP_NOWARN;
2900         if (!(gfp_mask & (GFP_DMA | GFP_DMA32)))
2901                 gfp_mask |= __GFP_HIGHMEM;
2902
2903         /* Please note that the recursion is strictly bounded. */
2904         if (array_size > PAGE_SIZE) {
2905                 area->pages = __vmalloc_node(array_size, 1, nested_gfp, node,
2906                                         area->caller);
2907         } else {
2908                 area->pages = kmalloc_node(array_size, nested_gfp, node);
2909         }
2910
2911         if (!area->pages) {
2912                 warn_alloc(gfp_mask, NULL,
2913                         "vmalloc error: size %lu, failed to allocated page array size %lu",
2914                         nr_small_pages * PAGE_SIZE, array_size);
2915                 free_vm_area(area);
2916                 return NULL;
2917         }
2918
2919         set_vm_area_page_order(area, page_shift - PAGE_SHIFT);
2920         page_order = vm_area_page_order(area);
2921
2922         area->nr_pages = vm_area_alloc_pages(gfp_mask, node,
2923                 page_order, nr_small_pages, area->pages);
2924
2925         atomic_long_add(area->nr_pages, &nr_vmalloc_pages);
2926
2927         /*
2928          * If not enough pages were obtained to accomplish an
2929          * allocation request, free them via __vfree() if any.
2930          */
2931         if (area->nr_pages != nr_small_pages) {
2932                 warn_alloc(gfp_mask, NULL,
2933                         "vmalloc error: size %lu, page order %u, failed to allocate pages",
2934                         area->nr_pages * PAGE_SIZE, page_order);
2935                 goto fail;
2936         }
2937
2938         if (vmap_pages_range(addr, addr + size, prot, area->pages,
2939                         page_shift) < 0) {
2940                 warn_alloc(gfp_mask, NULL,
2941                         "vmalloc error: size %lu, failed to map pages",
2942                         area->nr_pages * PAGE_SIZE);
2943                 goto fail;
2944         }
2945
2946         return area->addr;
2947
2948 fail:
2949         __vfree(area->addr);
2950         return NULL;
2951 }
2952
2953 /**
2954  * __vmalloc_node_range - allocate virtually contiguous memory
2955  * @size:                 allocation size
2956  * @align:                desired alignment
2957  * @start:                vm area range start
2958  * @end:                  vm area range end
2959  * @gfp_mask:             flags for the page level allocator
2960  * @prot:                 protection mask for the allocated pages
2961  * @vm_flags:             additional vm area flags (e.g. %VM_NO_GUARD)
2962  * @node:                 node to use for allocation or NUMA_NO_NODE
2963  * @caller:               caller's return address
2964  *
2965  * Allocate enough pages to cover @size from the page level
2966  * allocator with @gfp_mask flags.  Map them into contiguous
2967  * kernel virtual space, using a pagetable protection of @prot.
2968  *
2969  * Return: the address of the area or %NULL on failure
2970  */
2971 void *__vmalloc_node_range(unsigned long size, unsigned long align,
2972                         unsigned long start, unsigned long end, gfp_t gfp_mask,
2973                         pgprot_t prot, unsigned long vm_flags, int node,
2974                         const void *caller)
2975 {
2976         struct vm_struct *area;
2977         void *addr;
2978         unsigned long real_size = size;
2979         unsigned long real_align = align;
2980         unsigned int shift = PAGE_SHIFT;
2981
2982         if (WARN_ON_ONCE(!size))
2983                 return NULL;
2984
2985         if ((size >> PAGE_SHIFT) > totalram_pages()) {
2986                 warn_alloc(gfp_mask, NULL,
2987                         "vmalloc error: size %lu, exceeds total pages",
2988                         real_size);
2989                 return NULL;
2990         }
2991
2992         if (vmap_allow_huge && !(vm_flags & VM_NO_HUGE_VMAP)) {
2993                 unsigned long size_per_node;
2994
2995                 /*
2996                  * Try huge pages. Only try for PAGE_KERNEL allocations,
2997                  * others like modules don't yet expect huge pages in
2998                  * their allocations due to apply_to_page_range not
2999                  * supporting them.
3000                  */
3001
3002                 size_per_node = size;
3003                 if (node == NUMA_NO_NODE)
3004                         size_per_node /= num_online_nodes();
3005                 if (arch_vmap_pmd_supported(prot) && size_per_node >= PMD_SIZE)
3006                         shift = PMD_SHIFT;
3007                 else
3008                         shift = arch_vmap_pte_supported_shift(size_per_node);
3009
3010                 align = max(real_align, 1UL << shift);
3011                 size = ALIGN(real_size, 1UL << shift);
3012         }
3013
3014 again:
3015         area = __get_vm_area_node(real_size, align, shift, VM_ALLOC |
3016                                   VM_UNINITIALIZED | vm_flags, start, end, node,
3017                                   gfp_mask, caller);
3018         if (!area) {
3019                 warn_alloc(gfp_mask, NULL,
3020                         "vmalloc error: size %lu, vm_struct allocation failed",
3021                         real_size);
3022                 goto fail;
3023         }
3024
3025         addr = __vmalloc_area_node(area, gfp_mask, prot, shift, node);
3026         if (!addr)
3027                 goto fail;
3028
3029         /*
3030          * In this function, newly allocated vm_struct has VM_UNINITIALIZED
3031          * flag. It means that vm_struct is not fully initialized.
3032          * Now, it is fully initialized, so remove this flag here.
3033          */
3034         clear_vm_uninitialized_flag(area);
3035
3036         size = PAGE_ALIGN(size);
3037         if (!(vm_flags & VM_DEFER_KMEMLEAK))
3038                 kmemleak_vmalloc(area, size, gfp_mask);
3039
3040         return addr;
3041
3042 fail:
3043         if (shift > PAGE_SHIFT) {
3044                 shift = PAGE_SHIFT;
3045                 align = real_align;
3046                 size = real_size;
3047                 goto again;
3048         }
3049
3050         return NULL;
3051 }
3052
3053 /**
3054  * __vmalloc_node - allocate virtually contiguous memory
3055  * @size:           allocation size
3056  * @align:          desired alignment
3057  * @gfp_mask:       flags for the page level allocator
3058  * @node:           node to use for allocation or NUMA_NO_NODE
3059  * @caller:         caller's return address
3060  *
3061  * Allocate enough pages to cover @size from the page level allocator with
3062  * @gfp_mask flags.  Map them into contiguous kernel virtual space.
3063  *
3064  * Reclaim modifiers in @gfp_mask - __GFP_NORETRY, __GFP_RETRY_MAYFAIL
3065  * and __GFP_NOFAIL are not supported
3066  *
3067  * Any use of gfp flags outside of GFP_KERNEL should be consulted
3068  * with mm people.
3069  *
3070  * Return: pointer to the allocated memory or %NULL on error
3071  */
3072 void *__vmalloc_node(unsigned long size, unsigned long align,
3073                             gfp_t gfp_mask, int node, const void *caller)
3074 {
3075         return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
3076                                 gfp_mask, PAGE_KERNEL, 0, node, caller);
3077 }
3078 /*
3079  * This is only for performance analysis of vmalloc and stress purpose.
3080  * It is required by vmalloc test module, therefore do not use it other
3081  * than that.
3082  */
3083 #ifdef CONFIG_TEST_VMALLOC_MODULE
3084 EXPORT_SYMBOL_GPL(__vmalloc_node);
3085 #endif
3086
3087 void *__vmalloc(unsigned long size, gfp_t gfp_mask)
3088 {
3089         return __vmalloc_node(size, 1, gfp_mask, NUMA_NO_NODE,
3090                                 __builtin_return_address(0));
3091 }
3092 EXPORT_SYMBOL(__vmalloc);
3093
3094 /**
3095  * vmalloc - allocate virtually contiguous memory
3096  * @size:    allocation size
3097  *
3098  * Allocate enough pages to cover @size from the page level
3099  * allocator and map them into contiguous kernel virtual space.
3100  *
3101  * For tight control over page level allocator and protection flags
3102  * use __vmalloc() instead.
3103  *
3104  * Return: pointer to the allocated memory or %NULL on error
3105  */
3106 void *vmalloc(unsigned long size)
3107 {
3108         return __vmalloc_node(size, 1, GFP_KERNEL, NUMA_NO_NODE,
3109                                 __builtin_return_address(0));
3110 }
3111 EXPORT_SYMBOL(vmalloc);
3112
3113 /**
3114  * vmalloc_no_huge - allocate virtually contiguous memory using small pages
3115  * @size:    allocation size
3116  *
3117  * Allocate enough non-huge pages to cover @size from the page level
3118  * allocator and map them into contiguous kernel virtual space.
3119  *
3120  * Return: pointer to the allocated memory or %NULL on error
3121  */
3122 void *vmalloc_no_huge(unsigned long size)
3123 {
3124         return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END,
3125                                     GFP_KERNEL, PAGE_KERNEL, VM_NO_HUGE_VMAP,
3126                                     NUMA_NO_NODE, __builtin_return_address(0));
3127 }
3128 EXPORT_SYMBOL(vmalloc_no_huge);
3129
3130 /**
3131  * vzalloc - allocate virtually contiguous memory with zero fill
3132  * @size:    allocation size
3133  *
3134  * Allocate enough pages to cover @size from the page level
3135  * allocator and map them into contiguous kernel virtual space.
3136  * The memory allocated is set to zero.
3137  *
3138  * For tight control over page level allocator and protection flags
3139  * use __vmalloc() instead.
3140  *
3141  * Return: pointer to the allocated memory or %NULL on error
3142  */
3143 void *vzalloc(unsigned long size)
3144 {
3145         return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_ZERO, NUMA_NO_NODE,
3146                                 __builtin_return_address(0));
3147 }
3148 EXPORT_SYMBOL(vzalloc);
3149
3150 /**
3151  * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
3152  * @size: allocation size
3153  *
3154  * The resulting memory area is zeroed so it can be mapped to userspace
3155  * without leaking data.
3156  *
3157  * Return: pointer to the allocated memory or %NULL on error
3158  */
3159 void *vmalloc_user(unsigned long size)
3160 {
3161         return __vmalloc_node_range(size, SHMLBA,  VMALLOC_START, VMALLOC_END,
3162                                     GFP_KERNEL | __GFP_ZERO, PAGE_KERNEL,
3163                                     VM_USERMAP, NUMA_NO_NODE,
3164                                     __builtin_return_address(0));
3165 }
3166 EXPORT_SYMBOL(vmalloc_user);
3167
3168 /**
3169  * vmalloc_node - allocate memory on a specific node
3170  * @size:         allocation size
3171  * @node:         numa node
3172  *
3173  * Allocate enough pages to cover @size from the page level
3174  * allocator and map them into contiguous kernel virtual space.
3175  *
3176  * For tight control over page level allocator and protection flags
3177  * use __vmalloc() instead.
3178  *
3179  * Return: pointer to the allocated memory or %NULL on error
3180  */
3181 void *vmalloc_node(unsigned long size, int node)
3182 {
3183         return __vmalloc_node(size, 1, GFP_KERNEL, node,
3184                         __builtin_return_address(0));
3185 }
3186 EXPORT_SYMBOL(vmalloc_node);
3187
3188 /**
3189  * vzalloc_node - allocate memory on a specific node with zero fill
3190  * @size:       allocation size
3191  * @node:       numa node
3192  *
3193  * Allocate enough pages to cover @size from the page level
3194  * allocator and map them into contiguous kernel virtual space.
3195  * The memory allocated is set to zero.
3196  *
3197  * Return: pointer to the allocated memory or %NULL on error
3198  */
3199 void *vzalloc_node(unsigned long size, int node)
3200 {
3201         return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_ZERO, node,
3202                                 __builtin_return_address(0));
3203 }
3204 EXPORT_SYMBOL(vzalloc_node);
3205
3206 #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
3207 #define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL)
3208 #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
3209 #define GFP_VMALLOC32 (GFP_DMA | GFP_KERNEL)
3210 #else
3211 /*
3212  * 64b systems should always have either DMA or DMA32 zones. For others
3213  * GFP_DMA32 should do the right thing and use the normal zone.
3214  */
3215 #define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL)
3216 #endif
3217
3218 /**
3219  * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
3220  * @size:       allocation size
3221  *
3222  * Allocate enough 32bit PA addressable pages to cover @size from the
3223  * page level allocator and map them into contiguous kernel virtual space.
3224  *
3225  * Return: pointer to the allocated memory or %NULL on error
3226  */
3227 void *vmalloc_32(unsigned long size)
3228 {
3229         return __vmalloc_node(size, 1, GFP_VMALLOC32, NUMA_NO_NODE,
3230                         __builtin_return_address(0));
3231 }
3232 EXPORT_SYMBOL(vmalloc_32);
3233
3234 /**
3235  * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
3236  * @size:            allocation size
3237  *
3238  * The resulting memory area is 32bit addressable and zeroed so it can be
3239  * mapped to userspace without leaking data.
3240  *
3241  * Return: pointer to the allocated memory or %NULL on error
3242  */
3243 void *vmalloc_32_user(unsigned long size)
3244 {
3245         return __vmalloc_node_range(size, SHMLBA,  VMALLOC_START, VMALLOC_END,
3246                                     GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
3247                                     VM_USERMAP, NUMA_NO_NODE,
3248                                     __builtin_return_address(0));
3249 }
3250 EXPORT_SYMBOL(vmalloc_32_user);
3251
3252 /*
3253  * small helper routine , copy contents to buf from addr.
3254  * If the page is not present, fill zero.
3255  */
3256
3257 static int aligned_vread(char *buf, char *addr, unsigned long count)
3258 {
3259         struct page *p;
3260         int copied = 0;
3261
3262         while (count) {
3263                 unsigned long offset, length;
3264
3265                 offset = offset_in_page(addr);
3266                 length = PAGE_SIZE - offset;
3267                 if (length > count)
3268                         length = count;
3269                 p = vmalloc_to_page(addr);
3270                 /*
3271                  * To do safe access to this _mapped_ area, we need
3272                  * lock. But adding lock here means that we need to add
3273                  * overhead of vmalloc()/vfree() calls for this _debug_
3274                  * interface, rarely used. Instead of that, we'll use
3275                  * kmap() and get small overhead in this access function.
3276                  */
3277                 if (p) {
3278                         /* We can expect USER0 is not used -- see vread() */
3279                         void *map = kmap_atomic(p);
3280                         memcpy(buf, map + offset, length);
3281                         kunmap_atomic(map);
3282                 } else
3283                         memset(buf, 0, length);
3284
3285                 addr += length;
3286                 buf += length;
3287                 copied += length;
3288                 count -= length;
3289         }
3290         return copied;
3291 }
3292
3293 /**
3294  * vread() - read vmalloc area in a safe way.
3295  * @buf:     buffer for reading data
3296  * @addr:    vm address.
3297  * @count:   number of bytes to be read.
3298  *
3299  * This function checks that addr is a valid vmalloc'ed area, and
3300  * copy data from that area to a given buffer. If the given memory range
3301  * of [addr...addr+count) includes some valid address, data is copied to
3302  * proper area of @buf. If there are memory holes, they'll be zero-filled.
3303  * IOREMAP area is treated as memory hole and no copy is done.
3304  *
3305  * If [addr...addr+count) doesn't includes any intersects with alive
3306  * vm_struct area, returns 0. @buf should be kernel's buffer.
3307  *
3308  * Note: In usual ops, vread() is never necessary because the caller
3309  * should know vmalloc() area is valid and can use memcpy().
3310  * This is for routines which have to access vmalloc area without
3311  * any information, as /proc/kcore.
3312  *
3313  * Return: number of bytes for which addr and buf should be increased
3314  * (same number as @count) or %0 if [addr...addr+count) doesn't
3315  * include any intersection with valid vmalloc area
3316  */
3317 long vread(char *buf, char *addr, unsigned long count)
3318 {
3319         struct vmap_area *va;
3320         struct vm_struct *vm;
3321         char *vaddr, *buf_start = buf;
3322         unsigned long buflen = count;
3323         unsigned long n;
3324
3325         /* Don't allow overflow */
3326         if ((unsigned long) addr + count < count)
3327                 count = -(unsigned long) addr;
3328
3329         spin_lock(&vmap_area_lock);
3330         va = find_vmap_area_exceed_addr((unsigned long)addr);
3331         if (!va)
3332                 goto finished;
3333
3334         /* no intersects with alive vmap_area */
3335         if ((unsigned long)addr + count <= va->va_start)
3336                 goto finished;
3337
3338         list_for_each_entry_from(va, &vmap_area_list, list) {
3339                 if (!count)
3340                         break;
3341
3342                 if (!va->vm)
3343                         continue;
3344
3345                 vm = va->vm;
3346                 vaddr = (char *) vm->addr;
3347                 if (addr >= vaddr + get_vm_area_size(vm))
3348                         continue;
3349                 while (addr < vaddr) {
3350                         if (count == 0)
3351                                 goto finished;
3352                         *buf = '\0';
3353                         buf++;
3354                         addr++;
3355                         count--;
3356                 }
3357                 n = vaddr + get_vm_area_size(vm) - addr;
3358                 if (n > count)
3359                         n = count;
3360                 if (!(vm->flags & VM_IOREMAP))
3361                         aligned_vread(buf, addr, n);
3362                 else /* IOREMAP area is treated as memory hole */
3363                         memset(buf, 0, n);
3364                 buf += n;
3365                 addr += n;
3366                 count -= n;
3367         }
3368 finished:
3369         spin_unlock(&vmap_area_lock);
3370
3371         if (buf == buf_start)
3372                 return 0;
3373         /* zero-fill memory holes */
3374         if (buf != buf_start + buflen)
3375                 memset(buf, 0, buflen - (buf - buf_start));
3376
3377         return buflen;
3378 }
3379
3380 /**
3381  * remap_vmalloc_range_partial - map vmalloc pages to userspace
3382  * @vma:                vma to cover
3383  * @uaddr:              target user address to start at
3384  * @kaddr:              virtual address of vmalloc kernel memory
3385  * @pgoff:              offset from @kaddr to start at
3386  * @size:               size of map area
3387  *
3388  * Returns:     0 for success, -Exxx on failure
3389  *
3390  * This function checks that @kaddr is a valid vmalloc'ed area,
3391  * and that it is big enough to cover the range starting at
3392  * @uaddr in @vma. Will return failure if that criteria isn't
3393  * met.
3394  *
3395  * Similar to remap_pfn_range() (see mm/memory.c)
3396  */
3397 int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr,
3398                                 void *kaddr, unsigned long pgoff,
3399                                 unsigned long size)
3400 {
3401         struct vm_struct *area;
3402         unsigned long off;
3403         unsigned long end_index;
3404
3405         if (check_shl_overflow(pgoff, PAGE_SHIFT, &off))
3406                 return -EINVAL;
3407
3408         size = PAGE_ALIGN(size);
3409
3410         if (!PAGE_ALIGNED(uaddr) || !PAGE_ALIGNED(kaddr))
3411                 return -EINVAL;
3412
3413         area = find_vm_area(kaddr);
3414         if (!area)
3415                 return -EINVAL;
3416
3417         if (!(area->flags & (VM_USERMAP | VM_DMA_COHERENT)))
3418                 return -EINVAL;
3419
3420         if (check_add_overflow(size, off, &end_index) ||
3421             end_index > get_vm_area_size(area))
3422                 return -EINVAL;
3423         kaddr += off;
3424
3425         do {
3426                 struct page *page = vmalloc_to_page(kaddr);
3427                 int ret;
3428
3429                 ret = vm_insert_page(vma, uaddr, page);
3430                 if (ret)
3431                         return ret;
3432
3433                 uaddr += PAGE_SIZE;
3434                 kaddr += PAGE_SIZE;
3435                 size -= PAGE_SIZE;
3436         } while (size > 0);
3437
3438         vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3439
3440         return 0;
3441 }
3442
3443 /**
3444  * remap_vmalloc_range - map vmalloc pages to userspace
3445  * @vma:                vma to cover (map full range of vma)
3446  * @addr:               vmalloc memory
3447  * @pgoff:              number of pages into addr before first page to map
3448  *
3449  * Returns:     0 for success, -Exxx on failure
3450  *
3451  * This function checks that addr is a valid vmalloc'ed area, and
3452  * that it is big enough to cover the vma. Will return failure if
3453  * that criteria isn't met.
3454  *
3455  * Similar to remap_pfn_range() (see mm/memory.c)
3456  */
3457 int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
3458                                                 unsigned long pgoff)
3459 {
3460         return remap_vmalloc_range_partial(vma, vma->vm_start,
3461                                            addr, pgoff,
3462                                            vma->vm_end - vma->vm_start);
3463 }
3464 EXPORT_SYMBOL(remap_vmalloc_range);
3465
3466 void free_vm_area(struct vm_struct *area)
3467 {
3468         struct vm_struct *ret;
3469         ret = remove_vm_area(area->addr);
3470         BUG_ON(ret != area);
3471         kfree(area);
3472 }
3473 EXPORT_SYMBOL_GPL(free_vm_area);
3474
3475 #ifdef CONFIG_SMP
3476 static struct vmap_area *node_to_va(struct rb_node *n)
3477 {
3478         return rb_entry_safe(n, struct vmap_area, rb_node);
3479 }
3480
3481 /**
3482  * pvm_find_va_enclose_addr - find the vmap_area @addr belongs to
3483  * @addr: target address
3484  *
3485  * Returns: vmap_area if it is found. If there is no such area
3486  *   the first highest(reverse order) vmap_area is returned
3487  *   i.e. va->va_start < addr && va->va_end < addr or NULL
3488  *   if there are no any areas before @addr.
3489  */
3490 static struct vmap_area *
3491 pvm_find_va_enclose_addr(unsigned long addr)
3492 {
3493         struct vmap_area *va, *tmp;
3494         struct rb_node *n;
3495
3496         n = free_vmap_area_root.rb_node;
3497         va = NULL;
3498
3499         while (n) {
3500                 tmp = rb_entry(n, struct vmap_area, rb_node);
3501                 if (tmp->va_start <= addr) {
3502                         va = tmp;
3503                         if (tmp->va_end >= addr)
3504                                 break;
3505
3506                         n = n->rb_right;
3507                 } else {
3508                         n = n->rb_left;
3509                 }
3510         }
3511
3512         return va;
3513 }
3514
3515 /**
3516  * pvm_determine_end_from_reverse - find the highest aligned address
3517  * of free block below VMALLOC_END
3518  * @va:
3519  *   in - the VA we start the search(reverse order);
3520  *   out - the VA with the highest aligned end address.
3521  * @align: alignment for required highest address
3522  *
3523  * Returns: determined end address within vmap_area
3524  */
3525 static unsigned long
3526 pvm_determine_end_from_reverse(struct vmap_area **va, unsigned long align)
3527 {
3528         unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
3529         unsigned long addr;
3530
3531         if (likely(*va)) {
3532                 list_for_each_entry_from_reverse((*va),
3533                                 &free_vmap_area_list, list) {
3534                         addr = min((*va)->va_end & ~(align - 1), vmalloc_end);
3535                         if ((*va)->va_start < addr)
3536                                 return addr;
3537                 }
3538         }
3539
3540         return 0;
3541 }
3542
3543 /**
3544  * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator
3545  * @offsets: array containing offset of each area
3546  * @sizes: array containing size of each area
3547  * @nr_vms: the number of areas to allocate
3548  * @align: alignment, all entries in @offsets and @sizes must be aligned to this
3549  *
3550  * Returns: kmalloc'd vm_struct pointer array pointing to allocated
3551  *          vm_structs on success, %NULL on failure
3552  *
3553  * Percpu allocator wants to use congruent vm areas so that it can
3554  * maintain the offsets among percpu areas.  This function allocates
3555  * congruent vmalloc areas for it with GFP_KERNEL.  These areas tend to
3556  * be scattered pretty far, distance between two areas easily going up
3557  * to gigabytes.  To avoid interacting with regular vmallocs, these
3558  * areas are allocated from top.
3559  *
3560  * Despite its complicated look, this allocator is rather simple. It
3561  * does everything top-down and scans free blocks from the end looking
3562  * for matching base. While scanning, if any of the areas do not fit the
3563  * base address is pulled down to fit the area. Scanning is repeated till
3564  * all the areas fit and then all necessary data structures are inserted
3565  * and the result is returned.
3566  */
3567 struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
3568                                      const size_t *sizes, int nr_vms,
3569                                      size_t align)
3570 {
3571         const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align);
3572         const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
3573         struct vmap_area **vas, *va;
3574         struct vm_struct **vms;
3575         int area, area2, last_area, term_area;
3576         unsigned long base, start, size, end, last_end, orig_start, orig_end;
3577         bool purged = false;
3578         enum fit_type type;
3579
3580         /* verify parameters and allocate data structures */
3581         BUG_ON(offset_in_page(align) || !is_power_of_2(align));
3582         for (last_area = 0, area = 0; area < nr_vms; area++) {
3583                 start = offsets[area];
3584                 end = start + sizes[area];
3585
3586                 /* is everything aligned properly? */
3587                 BUG_ON(!IS_ALIGNED(offsets[area], align));
3588                 BUG_ON(!IS_ALIGNED(sizes[area], align));
3589
3590                 /* detect the area with the highest address */
3591                 if (start > offsets[last_area])
3592                         last_area = area;
3593
3594                 for (area2 = area + 1; area2 < nr_vms; area2++) {
3595                         unsigned long start2 = offsets[area2];
3596                         unsigned long end2 = start2 + sizes[area2];
3597
3598                         BUG_ON(start2 < end && start < end2);
3599                 }
3600         }
3601         last_end = offsets[last_area] + sizes[last_area];
3602
3603         if (vmalloc_end - vmalloc_start < last_end) {
3604                 WARN_ON(true);
3605                 return NULL;
3606         }
3607
3608         vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL);
3609         vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL);
3610         if (!vas || !vms)
3611                 goto err_free2;
3612
3613         for (area = 0; area < nr_vms; area++) {
3614                 vas[area] = kmem_cache_zalloc(vmap_area_cachep, GFP_KERNEL);
3615                 vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL);
3616                 if (!vas[area] || !vms[area])
3617                         goto err_free;
3618         }
3619 retry:
3620         spin_lock(&free_vmap_area_lock);
3621
3622         /* start scanning - we scan from the top, begin with the last area */
3623         area = term_area = last_area;
3624         start = offsets[area];
3625         end = start + sizes[area];
3626
3627         va = pvm_find_va_enclose_addr(vmalloc_end);
3628         base = pvm_determine_end_from_reverse(&va, align) - end;
3629
3630         while (true) {
3631                 /*
3632                  * base might have underflowed, add last_end before
3633                  * comparing.
3634                  */
3635                 if (base + last_end < vmalloc_start + last_end)
3636                         goto overflow;
3637
3638                 /*
3639                  * Fitting base has not been found.
3640                  */
3641                 if (va == NULL)
3642                         goto overflow;
3643
3644                 /*
3645                  * If required width exceeds current VA block, move
3646                  * base downwards and then recheck.
3647                  */
3648                 if (base + end > va->va_end) {
3649                         base = pvm_determine_end_from_reverse(&va, align) - end;
3650                         term_area = area;
3651                         continue;
3652                 }
3653
3654                 /*
3655                  * If this VA does not fit, move base downwards and recheck.
3656                  */
3657                 if (base + start < va->va_start) {
3658                         va = node_to_va(rb_prev(&va->rb_node));
3659                         base = pvm_determine_end_from_reverse(&va, align) - end;
3660                         term_area = area;
3661                         continue;
3662                 }
3663
3664                 /*
3665                  * This area fits, move on to the previous one.  If
3666                  * the previous one is the terminal one, we're done.
3667                  */
3668                 area = (area + nr_vms - 1) % nr_vms;
3669                 if (area == term_area)
3670                         break;
3671
3672                 start = offsets[area];
3673                 end = start + sizes[area];
3674                 va = pvm_find_va_enclose_addr(base + end);
3675         }
3676
3677         /* we've found a fitting base, insert all va's */
3678         for (area = 0; area < nr_vms; area++) {
3679                 int ret;
3680
3681                 start = base + offsets[area];
3682                 size = sizes[area];
3683
3684                 va = pvm_find_va_enclose_addr(start);
3685                 if (WARN_ON_ONCE(va == NULL))
3686                         /* It is a BUG(), but trigger recovery instead. */
3687                         goto recovery;
3688
3689                 type = classify_va_fit_type(va, start, size);
3690                 if (WARN_ON_ONCE(type == NOTHING_FIT))
3691                         /* It is a BUG(), but trigger recovery instead. */
3692                         goto recovery;
3693
3694                 ret = adjust_va_to_fit_type(va, start, size, type);
3695                 if (unlikely(ret))
3696                         goto recovery;
3697
3698                 /* Allocated area. */
3699                 va = vas[area];
3700                 va->va_start = start;
3701                 va->va_end = start + size;
3702         }
3703
3704         spin_unlock(&free_vmap_area_lock);
3705
3706         /* populate the kasan shadow space */
3707         for (area = 0; area < nr_vms; area++) {
3708                 if (kasan_populate_vmalloc(vas[area]->va_start, sizes[area]))
3709                         goto err_free_shadow;
3710
3711                 kasan_unpoison_vmalloc((void *)vas[area]->va_start,
3712                                        sizes[area]);
3713         }
3714
3715         /* insert all vm's */
3716         spin_lock(&vmap_area_lock);
3717         for (area = 0; area < nr_vms; area++) {
3718                 insert_vmap_area(vas[area], &vmap_area_root, &vmap_area_list);
3719
3720                 setup_vmalloc_vm_locked(vms[area], vas[area], VM_ALLOC,
3721                                  pcpu_get_vm_areas);
3722         }
3723         spin_unlock(&vmap_area_lock);
3724
3725         kfree(vas);
3726         return vms;
3727
3728 recovery:
3729         /*
3730          * Remove previously allocated areas. There is no
3731          * need in removing these areas from the busy tree,
3732          * because they are inserted only on the final step
3733          * and when pcpu_get_vm_areas() is success.
3734          */
3735         while (area--) {
3736                 orig_start = vas[area]->va_start;
3737                 orig_end = vas[area]->va_end;
3738                 va = merge_or_add_vmap_area_augment(vas[area], &free_vmap_area_root,
3739                                 &free_vmap_area_list);
3740                 if (va)
3741                         kasan_release_vmalloc(orig_start, orig_end,
3742                                 va->va_start, va->va_end);
3743                 vas[area] = NULL;
3744         }
3745
3746 overflow:
3747         spin_unlock(&free_vmap_area_lock);
3748         if (!purged) {
3749                 purge_vmap_area_lazy();
3750                 purged = true;
3751
3752                 /* Before "retry", check if we recover. */
3753                 for (area = 0; area < nr_vms; area++) {
3754                         if (vas[area])
3755                                 continue;
3756
3757                         vas[area] = kmem_cache_zalloc(
3758                                 vmap_area_cachep, GFP_KERNEL);
3759                         if (!vas[area])
3760                                 goto err_free;
3761                 }
3762
3763                 goto retry;
3764         }
3765
3766 err_free:
3767         for (area = 0; area < nr_vms; area++) {
3768                 if (vas[area])
3769                         kmem_cache_free(vmap_area_cachep, vas[area]);
3770
3771                 kfree(vms[area]);
3772         }
3773 err_free2:
3774         kfree(vas);
3775         kfree(vms);
3776         return NULL;
3777
3778 err_free_shadow:
3779         spin_lock(&free_vmap_area_lock);
3780         /*
3781          * We release all the vmalloc shadows, even the ones for regions that
3782          * hadn't been successfully added. This relies on kasan_release_vmalloc
3783          * being able to tolerate this case.
3784          */
3785         for (area = 0; area < nr_vms; area++) {
3786                 orig_start = vas[area]->va_start;
3787                 orig_end = vas[area]->va_end;
3788                 va = merge_or_add_vmap_area_augment(vas[area], &free_vmap_area_root,
3789                                 &free_vmap_area_list);
3790                 if (va)
3791                         kasan_release_vmalloc(orig_start, orig_end,
3792                                 va->va_start, va->va_end);
3793                 vas[area] = NULL;
3794                 kfree(vms[area]);
3795         }
3796         spin_unlock(&free_vmap_area_lock);
3797         kfree(vas);
3798         kfree(vms);
3799         return NULL;
3800 }
3801
3802 /**
3803  * pcpu_free_vm_areas - free vmalloc areas for percpu allocator
3804  * @vms: vm_struct pointer array returned by pcpu_get_vm_areas()
3805  * @nr_vms: the number of allocated areas
3806  *
3807  * Free vm_structs and the array allocated by pcpu_get_vm_areas().
3808  */
3809 void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
3810 {
3811         int i;
3812
3813         for (i = 0; i < nr_vms; i++)
3814                 free_vm_area(vms[i]);
3815         kfree(vms);
3816 }
3817 #endif  /* CONFIG_SMP */
3818
3819 #ifdef CONFIG_PRINTK
3820 bool vmalloc_dump_obj(void *object)
3821 {
3822         struct vm_struct *vm;
3823         void *objp = (void *)PAGE_ALIGN((unsigned long)object);
3824
3825         vm = find_vm_area(objp);
3826         if (!vm)
3827                 return false;
3828         pr_cont(" %u-page vmalloc region starting at %#lx allocated at %pS\n",
3829                 vm->nr_pages, (unsigned long)vm->addr, vm->caller);
3830         return true;
3831 }
3832 #endif
3833
3834 #ifdef CONFIG_PROC_FS
3835 static void *s_start(struct seq_file *m, loff_t *pos)
3836         __acquires(&vmap_purge_lock)
3837         __acquires(&vmap_area_lock)
3838 {
3839         mutex_lock(&vmap_purge_lock);
3840         spin_lock(&vmap_area_lock);
3841
3842         return seq_list_start(&vmap_area_list, *pos);
3843 }
3844
3845 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
3846 {
3847         return seq_list_next(p, &vmap_area_list, pos);
3848 }
3849
3850 static void s_stop(struct seq_file *m, void *p)
3851         __releases(&vmap_area_lock)
3852         __releases(&vmap_purge_lock)
3853 {
3854         spin_unlock(&vmap_area_lock);
3855         mutex_unlock(&vmap_purge_lock);
3856 }
3857
3858 static void show_numa_info(struct seq_file *m, struct vm_struct *v)
3859 {
3860         if (IS_ENABLED(CONFIG_NUMA)) {
3861                 unsigned int nr, *counters = m->private;
3862
3863                 if (!counters)
3864                         return;
3865
3866                 if (v->flags & VM_UNINITIALIZED)
3867                         return;
3868                 /* Pair with smp_wmb() in clear_vm_uninitialized_flag() */
3869                 smp_rmb();
3870
3871                 memset(counters, 0, nr_node_ids * sizeof(unsigned int));
3872
3873                 for (nr = 0; nr < v->nr_pages; nr++)
3874                         counters[page_to_nid(v->pages[nr])]++;
3875
3876                 for_each_node_state(nr, N_HIGH_MEMORY)
3877                         if (counters[nr])
3878                                 seq_printf(m, " N%u=%u", nr, counters[nr]);
3879         }
3880 }
3881
3882 static void show_purge_info(struct seq_file *m)
3883 {
3884         struct vmap_area *va;
3885
3886         spin_lock(&purge_vmap_area_lock);
3887         list_for_each_entry(va, &purge_vmap_area_list, list) {
3888                 seq_printf(m, "0x%pK-0x%pK %7ld unpurged vm_area\n",
3889                         (void *)va->va_start, (void *)va->va_end,
3890                         va->va_end - va->va_start);
3891         }
3892         spin_unlock(&purge_vmap_area_lock);
3893 }
3894
3895 static int s_show(struct seq_file *m, void *p)
3896 {
3897         struct vmap_area *va;
3898         struct vm_struct *v;
3899
3900         va = list_entry(p, struct vmap_area, list);
3901
3902         /*
3903          * s_show can encounter race with remove_vm_area, !vm on behalf
3904          * of vmap area is being tear down or vm_map_ram allocation.
3905          */
3906         if (!va->vm) {
3907                 seq_printf(m, "0x%pK-0x%pK %7ld vm_map_ram\n",
3908                         (void *)va->va_start, (void *)va->va_end,
3909                         va->va_end - va->va_start);
3910
3911                 return 0;
3912         }
3913
3914         v = va->vm;
3915
3916         seq_printf(m, "0x%pK-0x%pK %7ld",
3917                 v->addr, v->addr + v->size, v->size);
3918
3919         if (v->caller)
3920                 seq_printf(m, " %pS", v->caller);
3921
3922         if (v->nr_pages)
3923                 seq_printf(m, " pages=%d", v->nr_pages);
3924
3925         if (v->phys_addr)
3926                 seq_printf(m, " phys=%pa", &v->phys_addr);
3927
3928         if (v->flags & VM_IOREMAP)
3929                 seq_puts(m, " ioremap");
3930
3931         if (v->flags & VM_ALLOC)
3932                 seq_puts(m, " vmalloc");
3933
3934         if (v->flags & VM_MAP)
3935                 seq_puts(m, " vmap");
3936
3937         if (v->flags & VM_USERMAP)
3938                 seq_puts(m, " user");
3939
3940         if (v->flags & VM_DMA_COHERENT)
3941                 seq_puts(m, " dma-coherent");
3942
3943         if (is_vmalloc_addr(v->pages))
3944                 seq_puts(m, " vpages");
3945
3946         show_numa_info(m, v);
3947         seq_putc(m, '\n');
3948
3949         /*
3950          * As a final step, dump "unpurged" areas.
3951          */
3952         if (list_is_last(&va->list, &vmap_area_list))
3953                 show_purge_info(m);
3954
3955         return 0;
3956 }
3957
3958 static const struct seq_operations vmalloc_op = {
3959         .start = s_start,
3960         .next = s_next,
3961         .stop = s_stop,
3962         .show = s_show,
3963 };
3964
3965 static int __init proc_vmalloc_init(void)
3966 {
3967         if (IS_ENABLED(CONFIG_NUMA))
3968                 proc_create_seq_private("vmallocinfo", 0400, NULL,
3969                                 &vmalloc_op,
3970                                 nr_node_ids * sizeof(unsigned int), NULL);
3971         else
3972                 proc_create_seq("vmallocinfo", 0400, NULL, &vmalloc_op);
3973         return 0;
3974 }
3975 module_init(proc_vmalloc_init);
3976
3977 #endif