OSDN Git Service

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