xref: /openbmc/linux/mm/vmalloc.c (revision 34fe653716b0d340bc26dd4823d2dbe00c57f849)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  Copyright (C) 1993  Linus Torvalds
41da177e4SLinus Torvalds  *  Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
51da177e4SLinus Torvalds  *  SMP-safe vmalloc/vfree/ioremap, Tigran Aivazian <tigran@veritas.com>, May 2000
61da177e4SLinus Torvalds  *  Major rework to support vmap/vunmap, Christoph Hellwig, SGI, August 2002
7930fc45aSChristoph Lameter  *  Numa awareness, Christoph Lameter, SGI, June 2005
8d758ffe6SUladzislau Rezki (Sony)  *  Improving global KVA allocator, Uladzislau Rezki, Sony, May 2019
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
11db64fe02SNick Piggin #include <linux/vmalloc.h>
121da177e4SLinus Torvalds #include <linux/mm.h>
131da177e4SLinus Torvalds #include <linux/module.h>
141da177e4SLinus Torvalds #include <linux/highmem.h>
15c3edc401SIngo Molnar #include <linux/sched/signal.h>
161da177e4SLinus Torvalds #include <linux/slab.h>
171da177e4SLinus Torvalds #include <linux/spinlock.h>
181da177e4SLinus Torvalds #include <linux/interrupt.h>
195f6a6a9cSAlexey Dobriyan #include <linux/proc_fs.h>
20a10aa579SChristoph Lameter #include <linux/seq_file.h>
21868b104dSRick Edgecombe #include <linux/set_memory.h>
223ac7fe5aSThomas Gleixner #include <linux/debugobjects.h>
2323016969SChristoph Lameter #include <linux/kallsyms.h>
24db64fe02SNick Piggin #include <linux/list.h>
254da56b99SChris Wilson #include <linux/notifier.h>
26db64fe02SNick Piggin #include <linux/rbtree.h>
270f14599cSMatthew Wilcox (Oracle) #include <linux/xarray.h>
28db64fe02SNick Piggin #include <linux/rcupdate.h>
29f0aa6617STejun Heo #include <linux/pfn.h>
3089219d37SCatalin Marinas #include <linux/kmemleak.h>
3160063497SArun Sharma #include <linux/atomic.h>
323b32123dSGideon Israel Dsouza #include <linux/compiler.h>
3332fcfd40SAl Viro #include <linux/llist.h>
340f616be1SToshi Kani #include <linux/bitops.h>
3568ad4a33SUladzislau Rezki (Sony) #include <linux/rbtree_augmented.h>
36bdebd6a2SJann Horn #include <linux/overflow.h>
373b32123dSGideon Israel Dsouza 
387c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
391da177e4SLinus Torvalds #include <asm/tlbflush.h>
402dca6999SDavid Miller #include <asm/shmparam.h>
411da177e4SLinus Torvalds 
42dd56b046SMel Gorman #include "internal.h"
432a681cfaSJoerg Roedel #include "pgalloc-track.h"
44dd56b046SMel Gorman 
45186525bdSIngo Molnar bool is_vmalloc_addr(const void *x)
46186525bdSIngo Molnar {
47186525bdSIngo Molnar 	unsigned long addr = (unsigned long)x;
48186525bdSIngo Molnar 
49186525bdSIngo Molnar 	return addr >= VMALLOC_START && addr < VMALLOC_END;
50186525bdSIngo Molnar }
51186525bdSIngo Molnar EXPORT_SYMBOL(is_vmalloc_addr);
52186525bdSIngo Molnar 
5332fcfd40SAl Viro struct vfree_deferred {
5432fcfd40SAl Viro 	struct llist_head list;
5532fcfd40SAl Viro 	struct work_struct wq;
5632fcfd40SAl Viro };
5732fcfd40SAl Viro static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred);
5832fcfd40SAl Viro 
5932fcfd40SAl Viro static void __vunmap(const void *, int);
6032fcfd40SAl Viro 
6132fcfd40SAl Viro static void free_work(struct work_struct *w)
6232fcfd40SAl Viro {
6332fcfd40SAl Viro 	struct vfree_deferred *p = container_of(w, struct vfree_deferred, wq);
64894e58c1SByungchul Park 	struct llist_node *t, *llnode;
65894e58c1SByungchul Park 
66894e58c1SByungchul Park 	llist_for_each_safe(llnode, t, llist_del_all(&p->list))
67894e58c1SByungchul Park 		__vunmap((void *)llnode, 1);
6832fcfd40SAl Viro }
6932fcfd40SAl Viro 
70db64fe02SNick Piggin /*** Page table manipulation functions ***/
71b221385bSAdrian Bunk 
722ba3e694SJoerg Roedel static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
732ba3e694SJoerg Roedel 			     pgtbl_mod_mask *mask)
741da177e4SLinus Torvalds {
751da177e4SLinus Torvalds 	pte_t *pte;
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 	pte = pte_offset_kernel(pmd, addr);
781da177e4SLinus Torvalds 	do {
791da177e4SLinus Torvalds 		pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte);
801da177e4SLinus Torvalds 		WARN_ON(!pte_none(ptent) && !pte_present(ptent));
811da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
822ba3e694SJoerg Roedel 	*mask |= PGTBL_PTE_MODIFIED;
831da177e4SLinus Torvalds }
841da177e4SLinus Torvalds 
852ba3e694SJoerg Roedel static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,
862ba3e694SJoerg Roedel 			     pgtbl_mod_mask *mask)
871da177e4SLinus Torvalds {
881da177e4SLinus Torvalds 	pmd_t *pmd;
891da177e4SLinus Torvalds 	unsigned long next;
902ba3e694SJoerg Roedel 	int cleared;
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
931da177e4SLinus Torvalds 	do {
941da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
952ba3e694SJoerg Roedel 
962ba3e694SJoerg Roedel 		cleared = pmd_clear_huge(pmd);
972ba3e694SJoerg Roedel 		if (cleared || pmd_bad(*pmd))
982ba3e694SJoerg Roedel 			*mask |= PGTBL_PMD_MODIFIED;
992ba3e694SJoerg Roedel 
1002ba3e694SJoerg Roedel 		if (cleared)
101b9820d8fSToshi Kani 			continue;
1021da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(pmd))
1031da177e4SLinus Torvalds 			continue;
1042ba3e694SJoerg Roedel 		vunmap_pte_range(pmd, addr, next, mask);
105e47110e9SAneesh Kumar K.V 
106e47110e9SAneesh Kumar K.V 		cond_resched();
1071da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
1081da177e4SLinus Torvalds }
1091da177e4SLinus Torvalds 
1102ba3e694SJoerg Roedel static void vunmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end,
1112ba3e694SJoerg Roedel 			     pgtbl_mod_mask *mask)
1121da177e4SLinus Torvalds {
1131da177e4SLinus Torvalds 	pud_t *pud;
1141da177e4SLinus Torvalds 	unsigned long next;
1152ba3e694SJoerg Roedel 	int cleared;
1161da177e4SLinus Torvalds 
117c2febafcSKirill A. Shutemov 	pud = pud_offset(p4d, addr);
1181da177e4SLinus Torvalds 	do {
1191da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
1202ba3e694SJoerg Roedel 
1212ba3e694SJoerg Roedel 		cleared = pud_clear_huge(pud);
1222ba3e694SJoerg Roedel 		if (cleared || pud_bad(*pud))
1232ba3e694SJoerg Roedel 			*mask |= PGTBL_PUD_MODIFIED;
1242ba3e694SJoerg Roedel 
1252ba3e694SJoerg Roedel 		if (cleared)
126b9820d8fSToshi Kani 			continue;
1271da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
1281da177e4SLinus Torvalds 			continue;
1292ba3e694SJoerg Roedel 		vunmap_pmd_range(pud, addr, next, mask);
1301da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
1332ba3e694SJoerg Roedel static void vunmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,
1342ba3e694SJoerg Roedel 			     pgtbl_mod_mask *mask)
135c2febafcSKirill A. Shutemov {
136c2febafcSKirill A. Shutemov 	p4d_t *p4d;
137c2febafcSKirill A. Shutemov 	unsigned long next;
1382ba3e694SJoerg Roedel 	int cleared;
139c2febafcSKirill A. Shutemov 
140c2febafcSKirill A. Shutemov 	p4d = p4d_offset(pgd, addr);
141c2febafcSKirill A. Shutemov 	do {
142c2febafcSKirill A. Shutemov 		next = p4d_addr_end(addr, end);
1432ba3e694SJoerg Roedel 
1442ba3e694SJoerg Roedel 		cleared = p4d_clear_huge(p4d);
1452ba3e694SJoerg Roedel 		if (cleared || p4d_bad(*p4d))
1462ba3e694SJoerg Roedel 			*mask |= PGTBL_P4D_MODIFIED;
1472ba3e694SJoerg Roedel 
1482ba3e694SJoerg Roedel 		if (cleared)
149c2febafcSKirill A. Shutemov 			continue;
150c2febafcSKirill A. Shutemov 		if (p4d_none_or_clear_bad(p4d))
151c2febafcSKirill A. Shutemov 			continue;
1522ba3e694SJoerg Roedel 		vunmap_pud_range(p4d, addr, next, mask);
153c2febafcSKirill A. Shutemov 	} while (p4d++, addr = next, addr != end);
154c2febafcSKirill A. Shutemov }
155c2febafcSKirill A. Shutemov 
156b521c43fSChristoph Hellwig /**
157b521c43fSChristoph Hellwig  * unmap_kernel_range_noflush - unmap kernel VM area
1582ba3e694SJoerg Roedel  * @start: start of the VM area to unmap
159b521c43fSChristoph Hellwig  * @size: size of the VM area to unmap
160b521c43fSChristoph Hellwig  *
161b521c43fSChristoph Hellwig  * Unmap PFN_UP(@size) pages at @addr.  The VM area @addr and @size specify
162b521c43fSChristoph Hellwig  * should have been allocated using get_vm_area() and its friends.
163b521c43fSChristoph Hellwig  *
164b521c43fSChristoph Hellwig  * NOTE:
165b521c43fSChristoph Hellwig  * This function does NOT do any cache flushing.  The caller is responsible
166b521c43fSChristoph Hellwig  * for calling flush_cache_vunmap() on to-be-mapped areas before calling this
167b521c43fSChristoph Hellwig  * function and flush_tlb_kernel_range() after.
168b521c43fSChristoph Hellwig  */
1692ba3e694SJoerg Roedel void unmap_kernel_range_noflush(unsigned long start, unsigned long size)
1701da177e4SLinus Torvalds {
1712ba3e694SJoerg Roedel 	unsigned long end = start + size;
1721da177e4SLinus Torvalds 	unsigned long next;
173b521c43fSChristoph Hellwig 	pgd_t *pgd;
1742ba3e694SJoerg Roedel 	unsigned long addr = start;
1752ba3e694SJoerg Roedel 	pgtbl_mod_mask mask = 0;
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	BUG_ON(addr >= end);
1781da177e4SLinus Torvalds 	pgd = pgd_offset_k(addr);
1791da177e4SLinus Torvalds 	do {
1801da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
1812ba3e694SJoerg Roedel 		if (pgd_bad(*pgd))
1822ba3e694SJoerg Roedel 			mask |= PGTBL_PGD_MODIFIED;
1831da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
1841da177e4SLinus Torvalds 			continue;
1852ba3e694SJoerg Roedel 		vunmap_p4d_range(pgd, addr, next, &mask);
1861da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
1872ba3e694SJoerg Roedel 
1882ba3e694SJoerg Roedel 	if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
1892ba3e694SJoerg Roedel 		arch_sync_kernel_mappings(start, end);
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds static int vmap_pte_range(pmd_t *pmd, unsigned long addr,
1932ba3e694SJoerg Roedel 		unsigned long end, pgprot_t prot, struct page **pages, int *nr,
1942ba3e694SJoerg Roedel 		pgtbl_mod_mask *mask)
1951da177e4SLinus Torvalds {
1961da177e4SLinus Torvalds 	pte_t *pte;
1971da177e4SLinus Torvalds 
198db64fe02SNick Piggin 	/*
199db64fe02SNick Piggin 	 * nr is a running index into the array which helps higher level
200db64fe02SNick Piggin 	 * callers keep track of where we're up to.
201db64fe02SNick Piggin 	 */
202db64fe02SNick Piggin 
2032ba3e694SJoerg Roedel 	pte = pte_alloc_kernel_track(pmd, addr, mask);
2041da177e4SLinus Torvalds 	if (!pte)
2051da177e4SLinus Torvalds 		return -ENOMEM;
2061da177e4SLinus Torvalds 	do {
207db64fe02SNick Piggin 		struct page *page = pages[*nr];
208db64fe02SNick Piggin 
209db64fe02SNick Piggin 		if (WARN_ON(!pte_none(*pte)))
210db64fe02SNick Piggin 			return -EBUSY;
211db64fe02SNick Piggin 		if (WARN_ON(!page))
2121da177e4SLinus Torvalds 			return -ENOMEM;
2131da177e4SLinus Torvalds 		set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
214db64fe02SNick Piggin 		(*nr)++;
2151da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
2162ba3e694SJoerg Roedel 	*mask |= PGTBL_PTE_MODIFIED;
2171da177e4SLinus Torvalds 	return 0;
2181da177e4SLinus Torvalds }
2191da177e4SLinus Torvalds 
220db64fe02SNick Piggin static int vmap_pmd_range(pud_t *pud, unsigned long addr,
2212ba3e694SJoerg Roedel 		unsigned long end, pgprot_t prot, struct page **pages, int *nr,
2222ba3e694SJoerg Roedel 		pgtbl_mod_mask *mask)
2231da177e4SLinus Torvalds {
2241da177e4SLinus Torvalds 	pmd_t *pmd;
2251da177e4SLinus Torvalds 	unsigned long next;
2261da177e4SLinus Torvalds 
2272ba3e694SJoerg Roedel 	pmd = pmd_alloc_track(&init_mm, pud, addr, mask);
2281da177e4SLinus Torvalds 	if (!pmd)
2291da177e4SLinus Torvalds 		return -ENOMEM;
2301da177e4SLinus Torvalds 	do {
2311da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
2322ba3e694SJoerg Roedel 		if (vmap_pte_range(pmd, addr, next, prot, pages, nr, mask))
2331da177e4SLinus Torvalds 			return -ENOMEM;
2341da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
2351da177e4SLinus Torvalds 	return 0;
2361da177e4SLinus Torvalds }
2371da177e4SLinus Torvalds 
238c2febafcSKirill A. Shutemov static int vmap_pud_range(p4d_t *p4d, unsigned long addr,
2392ba3e694SJoerg Roedel 		unsigned long end, pgprot_t prot, struct page **pages, int *nr,
2402ba3e694SJoerg Roedel 		pgtbl_mod_mask *mask)
2411da177e4SLinus Torvalds {
2421da177e4SLinus Torvalds 	pud_t *pud;
2431da177e4SLinus Torvalds 	unsigned long next;
2441da177e4SLinus Torvalds 
2452ba3e694SJoerg Roedel 	pud = pud_alloc_track(&init_mm, p4d, addr, mask);
2461da177e4SLinus Torvalds 	if (!pud)
2471da177e4SLinus Torvalds 		return -ENOMEM;
2481da177e4SLinus Torvalds 	do {
2491da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
2502ba3e694SJoerg Roedel 		if (vmap_pmd_range(pud, addr, next, prot, pages, nr, mask))
2511da177e4SLinus Torvalds 			return -ENOMEM;
2521da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
2531da177e4SLinus Torvalds 	return 0;
2541da177e4SLinus Torvalds }
2551da177e4SLinus Torvalds 
256c2febafcSKirill A. Shutemov static int vmap_p4d_range(pgd_t *pgd, unsigned long addr,
2572ba3e694SJoerg Roedel 		unsigned long end, pgprot_t prot, struct page **pages, int *nr,
2582ba3e694SJoerg Roedel 		pgtbl_mod_mask *mask)
259c2febafcSKirill A. Shutemov {
260c2febafcSKirill A. Shutemov 	p4d_t *p4d;
261c2febafcSKirill A. Shutemov 	unsigned long next;
262c2febafcSKirill A. Shutemov 
2632ba3e694SJoerg Roedel 	p4d = p4d_alloc_track(&init_mm, pgd, addr, mask);
264c2febafcSKirill A. Shutemov 	if (!p4d)
265c2febafcSKirill A. Shutemov 		return -ENOMEM;
266c2febafcSKirill A. Shutemov 	do {
267c2febafcSKirill A. Shutemov 		next = p4d_addr_end(addr, end);
2682ba3e694SJoerg Roedel 		if (vmap_pud_range(p4d, addr, next, prot, pages, nr, mask))
269c2febafcSKirill A. Shutemov 			return -ENOMEM;
270c2febafcSKirill A. Shutemov 	} while (p4d++, addr = next, addr != end);
271c2febafcSKirill A. Shutemov 	return 0;
272c2febafcSKirill A. Shutemov }
273c2febafcSKirill A. Shutemov 
274b521c43fSChristoph Hellwig /**
275b521c43fSChristoph Hellwig  * map_kernel_range_noflush - map kernel VM area with the specified pages
276b521c43fSChristoph Hellwig  * @addr: start of the VM area to map
277b521c43fSChristoph Hellwig  * @size: size of the VM area to map
278b521c43fSChristoph Hellwig  * @prot: page protection flags to use
279b521c43fSChristoph Hellwig  * @pages: pages to map
280db64fe02SNick Piggin  *
281b521c43fSChristoph Hellwig  * Map PFN_UP(@size) pages at @addr.  The VM area @addr and @size specify should
282b521c43fSChristoph Hellwig  * have been allocated using get_vm_area() and its friends.
283b521c43fSChristoph Hellwig  *
284b521c43fSChristoph Hellwig  * NOTE:
285b521c43fSChristoph Hellwig  * This function does NOT do any cache flushing.  The caller is responsible for
286b521c43fSChristoph Hellwig  * calling flush_cache_vmap() on to-be-mapped areas before calling this
287b521c43fSChristoph Hellwig  * function.
288b521c43fSChristoph Hellwig  *
289b521c43fSChristoph Hellwig  * RETURNS:
29060bb4465SChristoph Hellwig  * 0 on success, -errno on failure.
291db64fe02SNick Piggin  */
292b521c43fSChristoph Hellwig int map_kernel_range_noflush(unsigned long addr, unsigned long size,
293db64fe02SNick Piggin 			     pgprot_t prot, struct page **pages)
2941da177e4SLinus Torvalds {
2952ba3e694SJoerg Roedel 	unsigned long start = addr;
296b521c43fSChristoph Hellwig 	unsigned long end = addr + size;
2971da177e4SLinus Torvalds 	unsigned long next;
298b521c43fSChristoph Hellwig 	pgd_t *pgd;
299db64fe02SNick Piggin 	int err = 0;
300db64fe02SNick Piggin 	int nr = 0;
3012ba3e694SJoerg Roedel 	pgtbl_mod_mask mask = 0;
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	BUG_ON(addr >= end);
3041da177e4SLinus Torvalds 	pgd = pgd_offset_k(addr);
3051da177e4SLinus Torvalds 	do {
3061da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
3072ba3e694SJoerg Roedel 		if (pgd_bad(*pgd))
3082ba3e694SJoerg Roedel 			mask |= PGTBL_PGD_MODIFIED;
3092ba3e694SJoerg Roedel 		err = vmap_p4d_range(pgd, addr, next, prot, pages, &nr, &mask);
3101da177e4SLinus Torvalds 		if (err)
311bf88c8c8SFigo.zhang 			return err;
3121da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
313db64fe02SNick Piggin 
3142ba3e694SJoerg Roedel 	if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
3152ba3e694SJoerg Roedel 		arch_sync_kernel_mappings(start, end);
3162ba3e694SJoerg Roedel 
31760bb4465SChristoph Hellwig 	return 0;
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds 
320ed1f324cSChristoph Hellwig int map_kernel_range(unsigned long start, unsigned long size, pgprot_t prot,
321ed1f324cSChristoph Hellwig 		struct page **pages)
3228fc48985STejun Heo {
3238fc48985STejun Heo 	int ret;
3248fc48985STejun Heo 
325a29adb62SChristoph Hellwig 	ret = map_kernel_range_noflush(start, size, prot, pages);
326a29adb62SChristoph Hellwig 	flush_cache_vmap(start, start + size);
3278fc48985STejun Heo 	return ret;
3288fc48985STejun Heo }
3298fc48985STejun Heo 
33081ac3ad9SKAMEZAWA Hiroyuki int is_vmalloc_or_module_addr(const void *x)
33173bdf0a6SLinus Torvalds {
33273bdf0a6SLinus Torvalds 	/*
333ab4f2ee1SRussell King 	 * ARM, x86-64 and sparc64 put modules in a special place,
33473bdf0a6SLinus Torvalds 	 * and fall back on vmalloc() if that fails. Others
33573bdf0a6SLinus Torvalds 	 * just put it in the vmalloc space.
33673bdf0a6SLinus Torvalds 	 */
33773bdf0a6SLinus Torvalds #if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
33873bdf0a6SLinus Torvalds 	unsigned long addr = (unsigned long)x;
33973bdf0a6SLinus Torvalds 	if (addr >= MODULES_VADDR && addr < MODULES_END)
34073bdf0a6SLinus Torvalds 		return 1;
34173bdf0a6SLinus Torvalds #endif
34273bdf0a6SLinus Torvalds 	return is_vmalloc_addr(x);
34373bdf0a6SLinus Torvalds }
34473bdf0a6SLinus Torvalds 
34548667e7aSChristoph Lameter /*
346add688fbSmalc  * Walk a vmap address to the struct page it maps.
34748667e7aSChristoph Lameter  */
348add688fbSmalc struct page *vmalloc_to_page(const void *vmalloc_addr)
34948667e7aSChristoph Lameter {
35048667e7aSChristoph Lameter 	unsigned long addr = (unsigned long) vmalloc_addr;
351add688fbSmalc 	struct page *page = NULL;
35248667e7aSChristoph Lameter 	pgd_t *pgd = pgd_offset_k(addr);
353c2febafcSKirill A. Shutemov 	p4d_t *p4d;
354c2febafcSKirill A. Shutemov 	pud_t *pud;
355c2febafcSKirill A. Shutemov 	pmd_t *pmd;
356c2febafcSKirill A. Shutemov 	pte_t *ptep, pte;
35748667e7aSChristoph Lameter 
3587aa413deSIngo Molnar 	/*
3597aa413deSIngo Molnar 	 * XXX we might need to change this if we add VIRTUAL_BUG_ON for
3607aa413deSIngo Molnar 	 * architectures that do not vmalloc module space
3617aa413deSIngo Molnar 	 */
36273bdf0a6SLinus Torvalds 	VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr));
36359ea7463SJiri Slaby 
364c2febafcSKirill A. Shutemov 	if (pgd_none(*pgd))
365c2febafcSKirill A. Shutemov 		return NULL;
366c2febafcSKirill A. Shutemov 	p4d = p4d_offset(pgd, addr);
367c2febafcSKirill A. Shutemov 	if (p4d_none(*p4d))
368c2febafcSKirill A. Shutemov 		return NULL;
369c2febafcSKirill A. Shutemov 	pud = pud_offset(p4d, addr);
370029c54b0SArd Biesheuvel 
371029c54b0SArd Biesheuvel 	/*
372029c54b0SArd Biesheuvel 	 * Don't dereference bad PUD or PMD (below) entries. This will also
373029c54b0SArd Biesheuvel 	 * identify huge mappings, which we may encounter on architectures
374029c54b0SArd Biesheuvel 	 * that define CONFIG_HAVE_ARCH_HUGE_VMAP=y. Such regions will be
375029c54b0SArd Biesheuvel 	 * identified as vmalloc addresses by is_vmalloc_addr(), but are
376029c54b0SArd Biesheuvel 	 * not [unambiguously] associated with a struct page, so there is
377029c54b0SArd Biesheuvel 	 * no correct value to return for them.
378029c54b0SArd Biesheuvel 	 */
379029c54b0SArd Biesheuvel 	WARN_ON_ONCE(pud_bad(*pud));
380029c54b0SArd Biesheuvel 	if (pud_none(*pud) || pud_bad(*pud))
381c2febafcSKirill A. Shutemov 		return NULL;
382c2febafcSKirill A. Shutemov 	pmd = pmd_offset(pud, addr);
383029c54b0SArd Biesheuvel 	WARN_ON_ONCE(pmd_bad(*pmd));
384029c54b0SArd Biesheuvel 	if (pmd_none(*pmd) || pmd_bad(*pmd))
385c2febafcSKirill A. Shutemov 		return NULL;
386db64fe02SNick Piggin 
38748667e7aSChristoph Lameter 	ptep = pte_offset_map(pmd, addr);
38848667e7aSChristoph Lameter 	pte = *ptep;
38948667e7aSChristoph Lameter 	if (pte_present(pte))
390add688fbSmalc 		page = pte_page(pte);
39148667e7aSChristoph Lameter 	pte_unmap(ptep);
392add688fbSmalc 	return page;
393ece86e22SJianyu Zhan }
394ece86e22SJianyu Zhan EXPORT_SYMBOL(vmalloc_to_page);
395ece86e22SJianyu Zhan 
396add688fbSmalc /*
397add688fbSmalc  * Map a vmalloc()-space virtual address to the physical page frame number.
398add688fbSmalc  */
399add688fbSmalc unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
400add688fbSmalc {
401add688fbSmalc 	return page_to_pfn(vmalloc_to_page(vmalloc_addr));
402add688fbSmalc }
403add688fbSmalc EXPORT_SYMBOL(vmalloc_to_pfn);
404add688fbSmalc 
405db64fe02SNick Piggin 
406db64fe02SNick Piggin /*** Global kva allocator ***/
407db64fe02SNick Piggin 
408bb850f4dSUladzislau Rezki (Sony) #define DEBUG_AUGMENT_PROPAGATE_CHECK 0
409a6cf4e0fSUladzislau Rezki (Sony) #define DEBUG_AUGMENT_LOWEST_MATCH_CHECK 0
410bb850f4dSUladzislau Rezki (Sony) 
411db64fe02SNick Piggin 
412db64fe02SNick Piggin static DEFINE_SPINLOCK(vmap_area_lock);
413e36176beSUladzislau Rezki (Sony) static DEFINE_SPINLOCK(free_vmap_area_lock);
414f1c4069eSJoonsoo Kim /* Export for kexec only */
415f1c4069eSJoonsoo Kim LIST_HEAD(vmap_area_list);
41680c4bd7aSChris Wilson static LLIST_HEAD(vmap_purge_list);
41789699605SNick Piggin static struct rb_root vmap_area_root = RB_ROOT;
41868ad4a33SUladzislau Rezki (Sony) static bool vmap_initialized __read_mostly;
41989699605SNick Piggin 
42068ad4a33SUladzislau Rezki (Sony) /*
42168ad4a33SUladzislau Rezki (Sony)  * This kmem_cache is used for vmap_area objects. Instead of
42268ad4a33SUladzislau Rezki (Sony)  * allocating from slab we reuse an object from this cache to
42368ad4a33SUladzislau Rezki (Sony)  * make things faster. Especially in "no edge" splitting of
42468ad4a33SUladzislau Rezki (Sony)  * free block.
42568ad4a33SUladzislau Rezki (Sony)  */
42668ad4a33SUladzislau Rezki (Sony) static struct kmem_cache *vmap_area_cachep;
42789699605SNick Piggin 
42868ad4a33SUladzislau Rezki (Sony) /*
42968ad4a33SUladzislau Rezki (Sony)  * This linked list is used in pair with free_vmap_area_root.
43068ad4a33SUladzislau Rezki (Sony)  * It gives O(1) access to prev/next to perform fast coalescing.
43168ad4a33SUladzislau Rezki (Sony)  */
43268ad4a33SUladzislau Rezki (Sony) static LIST_HEAD(free_vmap_area_list);
43368ad4a33SUladzislau Rezki (Sony) 
43468ad4a33SUladzislau Rezki (Sony) /*
43568ad4a33SUladzislau Rezki (Sony)  * This augment red-black tree represents the free vmap space.
43668ad4a33SUladzislau Rezki (Sony)  * All vmap_area objects in this tree are sorted by va->va_start
43768ad4a33SUladzislau Rezki (Sony)  * address. It is used for allocation and merging when a vmap
43868ad4a33SUladzislau Rezki (Sony)  * object is released.
43968ad4a33SUladzislau Rezki (Sony)  *
44068ad4a33SUladzislau Rezki (Sony)  * Each vmap_area node contains a maximum available free block
44168ad4a33SUladzislau Rezki (Sony)  * of its sub-tree, right or left. Therefore it is possible to
44268ad4a33SUladzislau Rezki (Sony)  * find a lowest match of free area.
44368ad4a33SUladzislau Rezki (Sony)  */
44468ad4a33SUladzislau Rezki (Sony) static struct rb_root free_vmap_area_root = RB_ROOT;
44568ad4a33SUladzislau Rezki (Sony) 
44682dd23e8SUladzislau Rezki (Sony) /*
44782dd23e8SUladzislau Rezki (Sony)  * Preload a CPU with one object for "no edge" split case. The
44882dd23e8SUladzislau Rezki (Sony)  * aim is to get rid of allocations from the atomic context, thus
44982dd23e8SUladzislau Rezki (Sony)  * to use more permissive allocation masks.
45082dd23e8SUladzislau Rezki (Sony)  */
45182dd23e8SUladzislau Rezki (Sony) static DEFINE_PER_CPU(struct vmap_area *, ne_fit_preload_node);
45282dd23e8SUladzislau Rezki (Sony) 
45368ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long
45468ad4a33SUladzislau Rezki (Sony) va_size(struct vmap_area *va)
45568ad4a33SUladzislau Rezki (Sony) {
45668ad4a33SUladzislau Rezki (Sony) 	return (va->va_end - va->va_start);
45768ad4a33SUladzislau Rezki (Sony) }
45868ad4a33SUladzislau Rezki (Sony) 
45968ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long
46068ad4a33SUladzislau Rezki (Sony) get_subtree_max_size(struct rb_node *node)
46168ad4a33SUladzislau Rezki (Sony) {
46268ad4a33SUladzislau Rezki (Sony) 	struct vmap_area *va;
46368ad4a33SUladzislau Rezki (Sony) 
46468ad4a33SUladzislau Rezki (Sony) 	va = rb_entry_safe(node, struct vmap_area, rb_node);
46568ad4a33SUladzislau Rezki (Sony) 	return va ? va->subtree_max_size : 0;
46668ad4a33SUladzislau Rezki (Sony) }
46768ad4a33SUladzislau Rezki (Sony) 
46868ad4a33SUladzislau Rezki (Sony) /*
46968ad4a33SUladzislau Rezki (Sony)  * Gets called when remove the node and rotate.
47068ad4a33SUladzislau Rezki (Sony)  */
47168ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long
47268ad4a33SUladzislau Rezki (Sony) compute_subtree_max_size(struct vmap_area *va)
47368ad4a33SUladzislau Rezki (Sony) {
47468ad4a33SUladzislau Rezki (Sony) 	return max3(va_size(va),
47568ad4a33SUladzislau Rezki (Sony) 		get_subtree_max_size(va->rb_node.rb_left),
47668ad4a33SUladzislau Rezki (Sony) 		get_subtree_max_size(va->rb_node.rb_right));
47768ad4a33SUladzislau Rezki (Sony) }
47868ad4a33SUladzislau Rezki (Sony) 
479315cc066SMichel Lespinasse RB_DECLARE_CALLBACKS_MAX(static, free_vmap_area_rb_augment_cb,
480315cc066SMichel Lespinasse 	struct vmap_area, rb_node, unsigned long, subtree_max_size, va_size)
48168ad4a33SUladzislau Rezki (Sony) 
48268ad4a33SUladzislau Rezki (Sony) static void purge_vmap_area_lazy(void);
48368ad4a33SUladzislau Rezki (Sony) static BLOCKING_NOTIFIER_HEAD(vmap_notify_list);
48468ad4a33SUladzislau Rezki (Sony) static unsigned long lazy_max_pages(void);
485db64fe02SNick Piggin 
48697105f0aSRoman Gushchin static atomic_long_t nr_vmalloc_pages;
48797105f0aSRoman Gushchin 
48897105f0aSRoman Gushchin unsigned long vmalloc_nr_pages(void)
48997105f0aSRoman Gushchin {
49097105f0aSRoman Gushchin 	return atomic_long_read(&nr_vmalloc_pages);
49197105f0aSRoman Gushchin }
49297105f0aSRoman Gushchin 
493db64fe02SNick Piggin static struct vmap_area *__find_vmap_area(unsigned long addr)
4941da177e4SLinus Torvalds {
495db64fe02SNick Piggin 	struct rb_node *n = vmap_area_root.rb_node;
496db64fe02SNick Piggin 
497db64fe02SNick Piggin 	while (n) {
498db64fe02SNick Piggin 		struct vmap_area *va;
499db64fe02SNick Piggin 
500db64fe02SNick Piggin 		va = rb_entry(n, struct vmap_area, rb_node);
501db64fe02SNick Piggin 		if (addr < va->va_start)
502db64fe02SNick Piggin 			n = n->rb_left;
503cef2ac3fSHATAYAMA Daisuke 		else if (addr >= va->va_end)
504db64fe02SNick Piggin 			n = n->rb_right;
505db64fe02SNick Piggin 		else
506db64fe02SNick Piggin 			return va;
507db64fe02SNick Piggin 	}
508db64fe02SNick Piggin 
509db64fe02SNick Piggin 	return NULL;
510db64fe02SNick Piggin }
511db64fe02SNick Piggin 
51268ad4a33SUladzislau Rezki (Sony) /*
51368ad4a33SUladzislau Rezki (Sony)  * This function returns back addresses of parent node
51468ad4a33SUladzislau Rezki (Sony)  * and its left or right link for further processing.
5159c801f61SUladzislau Rezki (Sony)  *
5169c801f61SUladzislau Rezki (Sony)  * Otherwise NULL is returned. In that case all further
5179c801f61SUladzislau Rezki (Sony)  * steps regarding inserting of conflicting overlap range
5189c801f61SUladzislau Rezki (Sony)  * have to be declined and actually considered as a bug.
51968ad4a33SUladzislau Rezki (Sony)  */
52068ad4a33SUladzislau Rezki (Sony) static __always_inline struct rb_node **
52168ad4a33SUladzislau Rezki (Sony) find_va_links(struct vmap_area *va,
52268ad4a33SUladzislau Rezki (Sony) 	struct rb_root *root, struct rb_node *from,
52368ad4a33SUladzislau Rezki (Sony) 	struct rb_node **parent)
524db64fe02SNick Piggin {
525170168d0SNamhyung Kim 	struct vmap_area *tmp_va;
52668ad4a33SUladzislau Rezki (Sony) 	struct rb_node **link;
527db64fe02SNick Piggin 
52868ad4a33SUladzislau Rezki (Sony) 	if (root) {
52968ad4a33SUladzislau Rezki (Sony) 		link = &root->rb_node;
53068ad4a33SUladzislau Rezki (Sony) 		if (unlikely(!*link)) {
53168ad4a33SUladzislau Rezki (Sony) 			*parent = NULL;
53268ad4a33SUladzislau Rezki (Sony) 			return link;
53368ad4a33SUladzislau Rezki (Sony) 		}
53468ad4a33SUladzislau Rezki (Sony) 	} else {
53568ad4a33SUladzislau Rezki (Sony) 		link = &from;
53668ad4a33SUladzislau Rezki (Sony) 	}
53768ad4a33SUladzislau Rezki (Sony) 
53868ad4a33SUladzislau Rezki (Sony) 	/*
53968ad4a33SUladzislau Rezki (Sony) 	 * Go to the bottom of the tree. When we hit the last point
54068ad4a33SUladzislau Rezki (Sony) 	 * we end up with parent rb_node and correct direction, i name
54168ad4a33SUladzislau Rezki (Sony) 	 * it link, where the new va->rb_node will be attached to.
54268ad4a33SUladzislau Rezki (Sony) 	 */
54368ad4a33SUladzislau Rezki (Sony) 	do {
54468ad4a33SUladzislau Rezki (Sony) 		tmp_va = rb_entry(*link, struct vmap_area, rb_node);
54568ad4a33SUladzislau Rezki (Sony) 
54668ad4a33SUladzislau Rezki (Sony) 		/*
54768ad4a33SUladzislau Rezki (Sony) 		 * During the traversal we also do some sanity check.
54868ad4a33SUladzislau Rezki (Sony) 		 * Trigger the BUG() if there are sides(left/right)
54968ad4a33SUladzislau Rezki (Sony) 		 * or full overlaps.
55068ad4a33SUladzislau Rezki (Sony) 		 */
55168ad4a33SUladzislau Rezki (Sony) 		if (va->va_start < tmp_va->va_end &&
55268ad4a33SUladzislau Rezki (Sony) 				va->va_end <= tmp_va->va_start)
55368ad4a33SUladzislau Rezki (Sony) 			link = &(*link)->rb_left;
55468ad4a33SUladzislau Rezki (Sony) 		else if (va->va_end > tmp_va->va_start &&
55568ad4a33SUladzislau Rezki (Sony) 				va->va_start >= tmp_va->va_end)
55668ad4a33SUladzislau Rezki (Sony) 			link = &(*link)->rb_right;
5579c801f61SUladzislau Rezki (Sony) 		else {
5589c801f61SUladzislau Rezki (Sony) 			WARN(1, "vmalloc bug: 0x%lx-0x%lx overlaps with 0x%lx-0x%lx\n",
5599c801f61SUladzislau Rezki (Sony) 				va->va_start, va->va_end, tmp_va->va_start, tmp_va->va_end);
5609c801f61SUladzislau Rezki (Sony) 
5619c801f61SUladzislau Rezki (Sony) 			return NULL;
5629c801f61SUladzislau Rezki (Sony) 		}
56368ad4a33SUladzislau Rezki (Sony) 	} while (*link);
56468ad4a33SUladzislau Rezki (Sony) 
56568ad4a33SUladzislau Rezki (Sony) 	*parent = &tmp_va->rb_node;
56668ad4a33SUladzislau Rezki (Sony) 	return link;
567db64fe02SNick Piggin }
568db64fe02SNick Piggin 
56968ad4a33SUladzislau Rezki (Sony) static __always_inline struct list_head *
57068ad4a33SUladzislau Rezki (Sony) get_va_next_sibling(struct rb_node *parent, struct rb_node **link)
57168ad4a33SUladzislau Rezki (Sony) {
57268ad4a33SUladzislau Rezki (Sony) 	struct list_head *list;
573db64fe02SNick Piggin 
57468ad4a33SUladzislau Rezki (Sony) 	if (unlikely(!parent))
57568ad4a33SUladzislau Rezki (Sony) 		/*
57668ad4a33SUladzislau Rezki (Sony) 		 * The red-black tree where we try to find VA neighbors
57768ad4a33SUladzislau Rezki (Sony) 		 * before merging or inserting is empty, i.e. it means
57868ad4a33SUladzislau Rezki (Sony) 		 * there is no free vmap space. Normally it does not
57968ad4a33SUladzislau Rezki (Sony) 		 * happen but we handle this case anyway.
58068ad4a33SUladzislau Rezki (Sony) 		 */
58168ad4a33SUladzislau Rezki (Sony) 		return NULL;
58268ad4a33SUladzislau Rezki (Sony) 
58368ad4a33SUladzislau Rezki (Sony) 	list = &rb_entry(parent, struct vmap_area, rb_node)->list;
58468ad4a33SUladzislau Rezki (Sony) 	return (&parent->rb_right == link ? list->next : list);
585db64fe02SNick Piggin }
586db64fe02SNick Piggin 
58768ad4a33SUladzislau Rezki (Sony) static __always_inline void
58868ad4a33SUladzislau Rezki (Sony) link_va(struct vmap_area *va, struct rb_root *root,
58968ad4a33SUladzislau Rezki (Sony) 	struct rb_node *parent, struct rb_node **link, struct list_head *head)
59068ad4a33SUladzislau Rezki (Sony) {
59168ad4a33SUladzislau Rezki (Sony) 	/*
59268ad4a33SUladzislau Rezki (Sony) 	 * VA is still not in the list, but we can
59368ad4a33SUladzislau Rezki (Sony) 	 * identify its future previous list_head node.
59468ad4a33SUladzislau Rezki (Sony) 	 */
59568ad4a33SUladzislau Rezki (Sony) 	if (likely(parent)) {
59668ad4a33SUladzislau Rezki (Sony) 		head = &rb_entry(parent, struct vmap_area, rb_node)->list;
59768ad4a33SUladzislau Rezki (Sony) 		if (&parent->rb_right != link)
59868ad4a33SUladzislau Rezki (Sony) 			head = head->prev;
59968ad4a33SUladzislau Rezki (Sony) 	}
600db64fe02SNick Piggin 
60168ad4a33SUladzislau Rezki (Sony) 	/* Insert to the rb-tree */
60268ad4a33SUladzislau Rezki (Sony) 	rb_link_node(&va->rb_node, parent, link);
60368ad4a33SUladzislau Rezki (Sony) 	if (root == &free_vmap_area_root) {
60468ad4a33SUladzislau Rezki (Sony) 		/*
60568ad4a33SUladzislau Rezki (Sony) 		 * Some explanation here. Just perform simple insertion
60668ad4a33SUladzislau Rezki (Sony) 		 * to the tree. We do not set va->subtree_max_size to
60768ad4a33SUladzislau Rezki (Sony) 		 * its current size before calling rb_insert_augmented().
60868ad4a33SUladzislau Rezki (Sony) 		 * It is because of we populate the tree from the bottom
60968ad4a33SUladzislau Rezki (Sony) 		 * to parent levels when the node _is_ in the tree.
61068ad4a33SUladzislau Rezki (Sony) 		 *
61168ad4a33SUladzislau Rezki (Sony) 		 * Therefore we set subtree_max_size to zero after insertion,
61268ad4a33SUladzislau Rezki (Sony) 		 * to let __augment_tree_propagate_from() puts everything to
61368ad4a33SUladzislau Rezki (Sony) 		 * the correct order later on.
61468ad4a33SUladzislau Rezki (Sony) 		 */
61568ad4a33SUladzislau Rezki (Sony) 		rb_insert_augmented(&va->rb_node,
61668ad4a33SUladzislau Rezki (Sony) 			root, &free_vmap_area_rb_augment_cb);
61768ad4a33SUladzislau Rezki (Sony) 		va->subtree_max_size = 0;
61868ad4a33SUladzislau Rezki (Sony) 	} else {
61968ad4a33SUladzislau Rezki (Sony) 		rb_insert_color(&va->rb_node, root);
62068ad4a33SUladzislau Rezki (Sony) 	}
62168ad4a33SUladzislau Rezki (Sony) 
62268ad4a33SUladzislau Rezki (Sony) 	/* Address-sort this list */
62368ad4a33SUladzislau Rezki (Sony) 	list_add(&va->list, head);
62468ad4a33SUladzislau Rezki (Sony) }
62568ad4a33SUladzislau Rezki (Sony) 
62668ad4a33SUladzislau Rezki (Sony) static __always_inline void
62768ad4a33SUladzislau Rezki (Sony) unlink_va(struct vmap_area *va, struct rb_root *root)
62868ad4a33SUladzislau Rezki (Sony) {
629460e42d1SUladzislau Rezki (Sony) 	if (WARN_ON(RB_EMPTY_NODE(&va->rb_node)))
630460e42d1SUladzislau Rezki (Sony) 		return;
631460e42d1SUladzislau Rezki (Sony) 
63268ad4a33SUladzislau Rezki (Sony) 	if (root == &free_vmap_area_root)
63368ad4a33SUladzislau Rezki (Sony) 		rb_erase_augmented(&va->rb_node,
63468ad4a33SUladzislau Rezki (Sony) 			root, &free_vmap_area_rb_augment_cb);
63568ad4a33SUladzislau Rezki (Sony) 	else
63668ad4a33SUladzislau Rezki (Sony) 		rb_erase(&va->rb_node, root);
63768ad4a33SUladzislau Rezki (Sony) 
63868ad4a33SUladzislau Rezki (Sony) 	list_del(&va->list);
63968ad4a33SUladzislau Rezki (Sony) 	RB_CLEAR_NODE(&va->rb_node);
64068ad4a33SUladzislau Rezki (Sony) }
64168ad4a33SUladzislau Rezki (Sony) 
642bb850f4dSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_PROPAGATE_CHECK
643bb850f4dSUladzislau Rezki (Sony) static void
644da27c9edSUladzislau Rezki (Sony) augment_tree_propagate_check(void)
645bb850f4dSUladzislau Rezki (Sony) {
646bb850f4dSUladzislau Rezki (Sony) 	struct vmap_area *va;
647da27c9edSUladzislau Rezki (Sony) 	unsigned long computed_size;
648bb850f4dSUladzislau Rezki (Sony) 
649da27c9edSUladzislau Rezki (Sony) 	list_for_each_entry(va, &free_vmap_area_list, list) {
650da27c9edSUladzislau Rezki (Sony) 		computed_size = compute_subtree_max_size(va);
651da27c9edSUladzislau Rezki (Sony) 		if (computed_size != va->subtree_max_size)
652bb850f4dSUladzislau Rezki (Sony) 			pr_emerg("tree is corrupted: %lu, %lu\n",
653bb850f4dSUladzislau Rezki (Sony) 				va_size(va), va->subtree_max_size);
654bb850f4dSUladzislau Rezki (Sony) 	}
655bb850f4dSUladzislau Rezki (Sony) }
656bb850f4dSUladzislau Rezki (Sony) #endif
657bb850f4dSUladzislau Rezki (Sony) 
65868ad4a33SUladzislau Rezki (Sony) /*
65968ad4a33SUladzislau Rezki (Sony)  * This function populates subtree_max_size from bottom to upper
66068ad4a33SUladzislau Rezki (Sony)  * levels starting from VA point. The propagation must be done
66168ad4a33SUladzislau Rezki (Sony)  * when VA size is modified by changing its va_start/va_end. Or
66268ad4a33SUladzislau Rezki (Sony)  * in case of newly inserting of VA to the tree.
66368ad4a33SUladzislau Rezki (Sony)  *
66468ad4a33SUladzislau Rezki (Sony)  * It means that __augment_tree_propagate_from() must be called:
66568ad4a33SUladzislau Rezki (Sony)  * - After VA has been inserted to the tree(free path);
66668ad4a33SUladzislau Rezki (Sony)  * - After VA has been shrunk(allocation path);
66768ad4a33SUladzislau Rezki (Sony)  * - After VA has been increased(merging path).
66868ad4a33SUladzislau Rezki (Sony)  *
66968ad4a33SUladzislau Rezki (Sony)  * Please note that, it does not mean that upper parent nodes
67068ad4a33SUladzislau Rezki (Sony)  * and their subtree_max_size are recalculated all the time up
67168ad4a33SUladzislau Rezki (Sony)  * to the root node.
67268ad4a33SUladzislau Rezki (Sony)  *
67368ad4a33SUladzislau Rezki (Sony)  *       4--8
67468ad4a33SUladzislau Rezki (Sony)  *        /\
67568ad4a33SUladzislau Rezki (Sony)  *       /  \
67668ad4a33SUladzislau Rezki (Sony)  *      /    \
67768ad4a33SUladzislau Rezki (Sony)  *    2--2  8--8
67868ad4a33SUladzislau Rezki (Sony)  *
67968ad4a33SUladzislau Rezki (Sony)  * For example if we modify the node 4, shrinking it to 2, then
68068ad4a33SUladzislau Rezki (Sony)  * no any modification is required. If we shrink the node 2 to 1
68168ad4a33SUladzislau Rezki (Sony)  * its subtree_max_size is updated only, and set to 1. If we shrink
68268ad4a33SUladzislau Rezki (Sony)  * the node 8 to 6, then its subtree_max_size is set to 6 and parent
68368ad4a33SUladzislau Rezki (Sony)  * node becomes 4--6.
68468ad4a33SUladzislau Rezki (Sony)  */
68568ad4a33SUladzislau Rezki (Sony) static __always_inline void
68668ad4a33SUladzislau Rezki (Sony) augment_tree_propagate_from(struct vmap_area *va)
68768ad4a33SUladzislau Rezki (Sony) {
68868ad4a33SUladzislau Rezki (Sony) 	/*
68915ae144fSUladzislau Rezki (Sony) 	 * Populate the tree from bottom towards the root until
69015ae144fSUladzislau Rezki (Sony) 	 * the calculated maximum available size of checked node
69115ae144fSUladzislau Rezki (Sony) 	 * is equal to its current one.
69268ad4a33SUladzislau Rezki (Sony) 	 */
69315ae144fSUladzislau Rezki (Sony) 	free_vmap_area_rb_augment_cb_propagate(&va->rb_node, NULL);
694bb850f4dSUladzislau Rezki (Sony) 
695bb850f4dSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_PROPAGATE_CHECK
696da27c9edSUladzislau Rezki (Sony) 	augment_tree_propagate_check();
697bb850f4dSUladzislau Rezki (Sony) #endif
69868ad4a33SUladzislau Rezki (Sony) }
69968ad4a33SUladzislau Rezki (Sony) 
70068ad4a33SUladzislau Rezki (Sony) static void
70168ad4a33SUladzislau Rezki (Sony) insert_vmap_area(struct vmap_area *va,
70268ad4a33SUladzislau Rezki (Sony) 	struct rb_root *root, struct list_head *head)
70368ad4a33SUladzislau Rezki (Sony) {
70468ad4a33SUladzislau Rezki (Sony) 	struct rb_node **link;
70568ad4a33SUladzislau Rezki (Sony) 	struct rb_node *parent;
70668ad4a33SUladzislau Rezki (Sony) 
70768ad4a33SUladzislau Rezki (Sony) 	link = find_va_links(va, root, NULL, &parent);
7089c801f61SUladzislau Rezki (Sony) 	if (link)
70968ad4a33SUladzislau Rezki (Sony) 		link_va(va, root, parent, link, head);
71068ad4a33SUladzislau Rezki (Sony) }
71168ad4a33SUladzislau Rezki (Sony) 
71268ad4a33SUladzislau Rezki (Sony) static void
71368ad4a33SUladzislau Rezki (Sony) insert_vmap_area_augment(struct vmap_area *va,
71468ad4a33SUladzislau Rezki (Sony) 	struct rb_node *from, struct rb_root *root,
71568ad4a33SUladzislau Rezki (Sony) 	struct list_head *head)
71668ad4a33SUladzislau Rezki (Sony) {
71768ad4a33SUladzislau Rezki (Sony) 	struct rb_node **link;
71868ad4a33SUladzislau Rezki (Sony) 	struct rb_node *parent;
71968ad4a33SUladzislau Rezki (Sony) 
72068ad4a33SUladzislau Rezki (Sony) 	if (from)
72168ad4a33SUladzislau Rezki (Sony) 		link = find_va_links(va, NULL, from, &parent);
72268ad4a33SUladzislau Rezki (Sony) 	else
72368ad4a33SUladzislau Rezki (Sony) 		link = find_va_links(va, root, NULL, &parent);
72468ad4a33SUladzislau Rezki (Sony) 
7259c801f61SUladzislau Rezki (Sony) 	if (link) {
72668ad4a33SUladzislau Rezki (Sony) 		link_va(va, root, parent, link, head);
72768ad4a33SUladzislau Rezki (Sony) 		augment_tree_propagate_from(va);
72868ad4a33SUladzislau Rezki (Sony) 	}
7299c801f61SUladzislau Rezki (Sony) }
73068ad4a33SUladzislau Rezki (Sony) 
73168ad4a33SUladzislau Rezki (Sony) /*
73268ad4a33SUladzislau Rezki (Sony)  * Merge de-allocated chunk of VA memory with previous
73368ad4a33SUladzislau Rezki (Sony)  * and next free blocks. If coalesce is not done a new
73468ad4a33SUladzislau Rezki (Sony)  * free area is inserted. If VA has been merged, it is
73568ad4a33SUladzislau Rezki (Sony)  * freed.
7369c801f61SUladzislau Rezki (Sony)  *
7379c801f61SUladzislau Rezki (Sony)  * Please note, it can return NULL in case of overlap
7389c801f61SUladzislau Rezki (Sony)  * ranges, followed by WARN() report. Despite it is a
7399c801f61SUladzislau Rezki (Sony)  * buggy behaviour, a system can be alive and keep
7409c801f61SUladzislau Rezki (Sony)  * ongoing.
74168ad4a33SUladzislau Rezki (Sony)  */
7423c5c3cfbSDaniel Axtens static __always_inline struct vmap_area *
74368ad4a33SUladzislau Rezki (Sony) merge_or_add_vmap_area(struct vmap_area *va,
74468ad4a33SUladzislau Rezki (Sony) 	struct rb_root *root, struct list_head *head)
74568ad4a33SUladzislau Rezki (Sony) {
74668ad4a33SUladzislau Rezki (Sony) 	struct vmap_area *sibling;
74768ad4a33SUladzislau Rezki (Sony) 	struct list_head *next;
74868ad4a33SUladzislau Rezki (Sony) 	struct rb_node **link;
74968ad4a33SUladzislau Rezki (Sony) 	struct rb_node *parent;
75068ad4a33SUladzislau Rezki (Sony) 	bool merged = false;
75168ad4a33SUladzislau Rezki (Sony) 
75268ad4a33SUladzislau Rezki (Sony) 	/*
75368ad4a33SUladzislau Rezki (Sony) 	 * Find a place in the tree where VA potentially will be
75468ad4a33SUladzislau Rezki (Sony) 	 * inserted, unless it is merged with its sibling/siblings.
75568ad4a33SUladzislau Rezki (Sony) 	 */
75668ad4a33SUladzislau Rezki (Sony) 	link = find_va_links(va, root, NULL, &parent);
7579c801f61SUladzislau Rezki (Sony) 	if (!link)
7589c801f61SUladzislau Rezki (Sony) 		return NULL;
75968ad4a33SUladzislau Rezki (Sony) 
76068ad4a33SUladzislau Rezki (Sony) 	/*
76168ad4a33SUladzislau Rezki (Sony) 	 * Get next node of VA to check if merging can be done.
76268ad4a33SUladzislau Rezki (Sony) 	 */
76368ad4a33SUladzislau Rezki (Sony) 	next = get_va_next_sibling(parent, link);
76468ad4a33SUladzislau Rezki (Sony) 	if (unlikely(next == NULL))
76568ad4a33SUladzislau Rezki (Sony) 		goto insert;
76668ad4a33SUladzislau Rezki (Sony) 
76768ad4a33SUladzislau Rezki (Sony) 	/*
76868ad4a33SUladzislau Rezki (Sony) 	 * start            end
76968ad4a33SUladzislau Rezki (Sony) 	 * |                |
77068ad4a33SUladzislau Rezki (Sony) 	 * |<------VA------>|<-----Next----->|
77168ad4a33SUladzislau Rezki (Sony) 	 *                  |                |
77268ad4a33SUladzislau Rezki (Sony) 	 *                  start            end
77368ad4a33SUladzislau Rezki (Sony) 	 */
77468ad4a33SUladzislau Rezki (Sony) 	if (next != head) {
77568ad4a33SUladzislau Rezki (Sony) 		sibling = list_entry(next, struct vmap_area, list);
77668ad4a33SUladzislau Rezki (Sony) 		if (sibling->va_start == va->va_end) {
77768ad4a33SUladzislau Rezki (Sony) 			sibling->va_start = va->va_start;
77868ad4a33SUladzislau Rezki (Sony) 
77968ad4a33SUladzislau Rezki (Sony) 			/* Free vmap_area object. */
78068ad4a33SUladzislau Rezki (Sony) 			kmem_cache_free(vmap_area_cachep, va);
78168ad4a33SUladzislau Rezki (Sony) 
78268ad4a33SUladzislau Rezki (Sony) 			/* Point to the new merged area. */
78368ad4a33SUladzislau Rezki (Sony) 			va = sibling;
78468ad4a33SUladzislau Rezki (Sony) 			merged = true;
78568ad4a33SUladzislau Rezki (Sony) 		}
78668ad4a33SUladzislau Rezki (Sony) 	}
78768ad4a33SUladzislau Rezki (Sony) 
78868ad4a33SUladzislau Rezki (Sony) 	/*
78968ad4a33SUladzislau Rezki (Sony) 	 * start            end
79068ad4a33SUladzislau Rezki (Sony) 	 * |                |
79168ad4a33SUladzislau Rezki (Sony) 	 * |<-----Prev----->|<------VA------>|
79268ad4a33SUladzislau Rezki (Sony) 	 *                  |                |
79368ad4a33SUladzislau Rezki (Sony) 	 *                  start            end
79468ad4a33SUladzislau Rezki (Sony) 	 */
79568ad4a33SUladzislau Rezki (Sony) 	if (next->prev != head) {
79668ad4a33SUladzislau Rezki (Sony) 		sibling = list_entry(next->prev, struct vmap_area, list);
79768ad4a33SUladzislau Rezki (Sony) 		if (sibling->va_end == va->va_start) {
7985dd78640SUladzislau Rezki (Sony) 			/*
7995dd78640SUladzislau Rezki (Sony) 			 * If both neighbors are coalesced, it is important
8005dd78640SUladzislau Rezki (Sony) 			 * to unlink the "next" node first, followed by merging
8015dd78640SUladzislau Rezki (Sony) 			 * with "previous" one. Otherwise the tree might not be
8025dd78640SUladzislau Rezki (Sony) 			 * fully populated if a sibling's augmented value is
8035dd78640SUladzislau Rezki (Sony) 			 * "normalized" because of rotation operations.
8045dd78640SUladzislau Rezki (Sony) 			 */
80554f63d9dSUladzislau Rezki (Sony) 			if (merged)
80668ad4a33SUladzislau Rezki (Sony) 				unlink_va(va, root);
80768ad4a33SUladzislau Rezki (Sony) 
8085dd78640SUladzislau Rezki (Sony) 			sibling->va_end = va->va_end;
8095dd78640SUladzislau Rezki (Sony) 
81068ad4a33SUladzislau Rezki (Sony) 			/* Free vmap_area object. */
81168ad4a33SUladzislau Rezki (Sony) 			kmem_cache_free(vmap_area_cachep, va);
8123c5c3cfbSDaniel Axtens 
8133c5c3cfbSDaniel Axtens 			/* Point to the new merged area. */
8143c5c3cfbSDaniel Axtens 			va = sibling;
8153c5c3cfbSDaniel Axtens 			merged = true;
81668ad4a33SUladzislau Rezki (Sony) 		}
81768ad4a33SUladzislau Rezki (Sony) 	}
81868ad4a33SUladzislau Rezki (Sony) 
81968ad4a33SUladzislau Rezki (Sony) insert:
8205dd78640SUladzislau Rezki (Sony) 	if (!merged)
82168ad4a33SUladzislau Rezki (Sony) 		link_va(va, root, parent, link, head);
8223c5c3cfbSDaniel Axtens 
8235dd78640SUladzislau Rezki (Sony) 	/*
8245dd78640SUladzislau Rezki (Sony) 	 * Last step is to check and update the tree.
8255dd78640SUladzislau Rezki (Sony) 	 */
8265dd78640SUladzislau Rezki (Sony) 	augment_tree_propagate_from(va);
8273c5c3cfbSDaniel Axtens 	return va;
82868ad4a33SUladzislau Rezki (Sony) }
82968ad4a33SUladzislau Rezki (Sony) 
83068ad4a33SUladzislau Rezki (Sony) static __always_inline bool
83168ad4a33SUladzislau Rezki (Sony) is_within_this_va(struct vmap_area *va, unsigned long size,
83268ad4a33SUladzislau Rezki (Sony) 	unsigned long align, unsigned long vstart)
83368ad4a33SUladzislau Rezki (Sony) {
83468ad4a33SUladzislau Rezki (Sony) 	unsigned long nva_start_addr;
83568ad4a33SUladzislau Rezki (Sony) 
83668ad4a33SUladzislau Rezki (Sony) 	if (va->va_start > vstart)
83768ad4a33SUladzislau Rezki (Sony) 		nva_start_addr = ALIGN(va->va_start, align);
83868ad4a33SUladzislau Rezki (Sony) 	else
83968ad4a33SUladzislau Rezki (Sony) 		nva_start_addr = ALIGN(vstart, align);
84068ad4a33SUladzislau Rezki (Sony) 
84168ad4a33SUladzislau Rezki (Sony) 	/* Can be overflowed due to big size or alignment. */
84268ad4a33SUladzislau Rezki (Sony) 	if (nva_start_addr + size < nva_start_addr ||
84368ad4a33SUladzislau Rezki (Sony) 			nva_start_addr < vstart)
84468ad4a33SUladzislau Rezki (Sony) 		return false;
84568ad4a33SUladzislau Rezki (Sony) 
84668ad4a33SUladzislau Rezki (Sony) 	return (nva_start_addr + size <= va->va_end);
84768ad4a33SUladzislau Rezki (Sony) }
84868ad4a33SUladzislau Rezki (Sony) 
84968ad4a33SUladzislau Rezki (Sony) /*
85068ad4a33SUladzislau Rezki (Sony)  * Find the first free block(lowest start address) in the tree,
85168ad4a33SUladzislau Rezki (Sony)  * that will accomplish the request corresponding to passing
85268ad4a33SUladzislau Rezki (Sony)  * parameters.
85368ad4a33SUladzislau Rezki (Sony)  */
85468ad4a33SUladzislau Rezki (Sony) static __always_inline struct vmap_area *
85568ad4a33SUladzislau Rezki (Sony) find_vmap_lowest_match(unsigned long size,
85668ad4a33SUladzislau Rezki (Sony) 	unsigned long align, unsigned long vstart)
85768ad4a33SUladzislau Rezki (Sony) {
85868ad4a33SUladzislau Rezki (Sony) 	struct vmap_area *va;
85968ad4a33SUladzislau Rezki (Sony) 	struct rb_node *node;
86068ad4a33SUladzislau Rezki (Sony) 	unsigned long length;
86168ad4a33SUladzislau Rezki (Sony) 
86268ad4a33SUladzislau Rezki (Sony) 	/* Start from the root. */
86368ad4a33SUladzislau Rezki (Sony) 	node = free_vmap_area_root.rb_node;
86468ad4a33SUladzislau Rezki (Sony) 
86568ad4a33SUladzislau Rezki (Sony) 	/* Adjust the search size for alignment overhead. */
86668ad4a33SUladzislau Rezki (Sony) 	length = size + align - 1;
86768ad4a33SUladzislau Rezki (Sony) 
86868ad4a33SUladzislau Rezki (Sony) 	while (node) {
86968ad4a33SUladzislau Rezki (Sony) 		va = rb_entry(node, struct vmap_area, rb_node);
87068ad4a33SUladzislau Rezki (Sony) 
87168ad4a33SUladzislau Rezki (Sony) 		if (get_subtree_max_size(node->rb_left) >= length &&
87268ad4a33SUladzislau Rezki (Sony) 				vstart < va->va_start) {
87368ad4a33SUladzislau Rezki (Sony) 			node = node->rb_left;
87468ad4a33SUladzislau Rezki (Sony) 		} else {
87568ad4a33SUladzislau Rezki (Sony) 			if (is_within_this_va(va, size, align, vstart))
87668ad4a33SUladzislau Rezki (Sony) 				return va;
87768ad4a33SUladzislau Rezki (Sony) 
87868ad4a33SUladzislau Rezki (Sony) 			/*
87968ad4a33SUladzislau Rezki (Sony) 			 * Does not make sense to go deeper towards the right
88068ad4a33SUladzislau Rezki (Sony) 			 * sub-tree if it does not have a free block that is
88168ad4a33SUladzislau Rezki (Sony) 			 * equal or bigger to the requested search length.
88268ad4a33SUladzislau Rezki (Sony) 			 */
88368ad4a33SUladzislau Rezki (Sony) 			if (get_subtree_max_size(node->rb_right) >= length) {
88468ad4a33SUladzislau Rezki (Sony) 				node = node->rb_right;
88568ad4a33SUladzislau Rezki (Sony) 				continue;
88668ad4a33SUladzislau Rezki (Sony) 			}
88768ad4a33SUladzislau Rezki (Sony) 
88868ad4a33SUladzislau Rezki (Sony) 			/*
8893806b041SAndrew Morton 			 * OK. We roll back and find the first right sub-tree,
89068ad4a33SUladzislau Rezki (Sony) 			 * that will satisfy the search criteria. It can happen
89168ad4a33SUladzislau Rezki (Sony) 			 * only once due to "vstart" restriction.
89268ad4a33SUladzislau Rezki (Sony) 			 */
89368ad4a33SUladzislau Rezki (Sony) 			while ((node = rb_parent(node))) {
89468ad4a33SUladzislau Rezki (Sony) 				va = rb_entry(node, struct vmap_area, rb_node);
89568ad4a33SUladzislau Rezki (Sony) 				if (is_within_this_va(va, size, align, vstart))
89668ad4a33SUladzislau Rezki (Sony) 					return va;
89768ad4a33SUladzislau Rezki (Sony) 
89868ad4a33SUladzislau Rezki (Sony) 				if (get_subtree_max_size(node->rb_right) >= length &&
89968ad4a33SUladzislau Rezki (Sony) 						vstart <= va->va_start) {
90068ad4a33SUladzislau Rezki (Sony) 					node = node->rb_right;
90168ad4a33SUladzislau Rezki (Sony) 					break;
90268ad4a33SUladzislau Rezki (Sony) 				}
90368ad4a33SUladzislau Rezki (Sony) 			}
90468ad4a33SUladzislau Rezki (Sony) 		}
90568ad4a33SUladzislau Rezki (Sony) 	}
90668ad4a33SUladzislau Rezki (Sony) 
90768ad4a33SUladzislau Rezki (Sony) 	return NULL;
90868ad4a33SUladzislau Rezki (Sony) }
90968ad4a33SUladzislau Rezki (Sony) 
910a6cf4e0fSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_LOWEST_MATCH_CHECK
911a6cf4e0fSUladzislau Rezki (Sony) #include <linux/random.h>
912a6cf4e0fSUladzislau Rezki (Sony) 
913a6cf4e0fSUladzislau Rezki (Sony) static struct vmap_area *
914a6cf4e0fSUladzislau Rezki (Sony) find_vmap_lowest_linear_match(unsigned long size,
915a6cf4e0fSUladzislau Rezki (Sony) 	unsigned long align, unsigned long vstart)
916a6cf4e0fSUladzislau Rezki (Sony) {
917a6cf4e0fSUladzislau Rezki (Sony) 	struct vmap_area *va;
918a6cf4e0fSUladzislau Rezki (Sony) 
919a6cf4e0fSUladzislau Rezki (Sony) 	list_for_each_entry(va, &free_vmap_area_list, list) {
920a6cf4e0fSUladzislau Rezki (Sony) 		if (!is_within_this_va(va, size, align, vstart))
921a6cf4e0fSUladzislau Rezki (Sony) 			continue;
922a6cf4e0fSUladzislau Rezki (Sony) 
923a6cf4e0fSUladzislau Rezki (Sony) 		return va;
924a6cf4e0fSUladzislau Rezki (Sony) 	}
925a6cf4e0fSUladzislau Rezki (Sony) 
926a6cf4e0fSUladzislau Rezki (Sony) 	return NULL;
927a6cf4e0fSUladzislau Rezki (Sony) }
928a6cf4e0fSUladzislau Rezki (Sony) 
929a6cf4e0fSUladzislau Rezki (Sony) static void
930a6cf4e0fSUladzislau Rezki (Sony) find_vmap_lowest_match_check(unsigned long size)
931a6cf4e0fSUladzislau Rezki (Sony) {
932a6cf4e0fSUladzislau Rezki (Sony) 	struct vmap_area *va_1, *va_2;
933a6cf4e0fSUladzislau Rezki (Sony) 	unsigned long vstart;
934a6cf4e0fSUladzislau Rezki (Sony) 	unsigned int rnd;
935a6cf4e0fSUladzislau Rezki (Sony) 
936a6cf4e0fSUladzislau Rezki (Sony) 	get_random_bytes(&rnd, sizeof(rnd));
937a6cf4e0fSUladzislau Rezki (Sony) 	vstart = VMALLOC_START + rnd;
938a6cf4e0fSUladzislau Rezki (Sony) 
939a6cf4e0fSUladzislau Rezki (Sony) 	va_1 = find_vmap_lowest_match(size, 1, vstart);
940a6cf4e0fSUladzislau Rezki (Sony) 	va_2 = find_vmap_lowest_linear_match(size, 1, vstart);
941a6cf4e0fSUladzislau Rezki (Sony) 
942a6cf4e0fSUladzislau Rezki (Sony) 	if (va_1 != va_2)
943a6cf4e0fSUladzislau Rezki (Sony) 		pr_emerg("not lowest: t: 0x%p, l: 0x%p, v: 0x%lx\n",
944a6cf4e0fSUladzislau Rezki (Sony) 			va_1, va_2, vstart);
945a6cf4e0fSUladzislau Rezki (Sony) }
946a6cf4e0fSUladzislau Rezki (Sony) #endif
947a6cf4e0fSUladzislau Rezki (Sony) 
94868ad4a33SUladzislau Rezki (Sony) enum fit_type {
94968ad4a33SUladzislau Rezki (Sony) 	NOTHING_FIT = 0,
95068ad4a33SUladzislau Rezki (Sony) 	FL_FIT_TYPE = 1,	/* full fit */
95168ad4a33SUladzislau Rezki (Sony) 	LE_FIT_TYPE = 2,	/* left edge fit */
95268ad4a33SUladzislau Rezki (Sony) 	RE_FIT_TYPE = 3,	/* right edge fit */
95368ad4a33SUladzislau Rezki (Sony) 	NE_FIT_TYPE = 4		/* no edge fit */
95468ad4a33SUladzislau Rezki (Sony) };
95568ad4a33SUladzislau Rezki (Sony) 
95668ad4a33SUladzislau Rezki (Sony) static __always_inline enum fit_type
95768ad4a33SUladzislau Rezki (Sony) classify_va_fit_type(struct vmap_area *va,
95868ad4a33SUladzislau Rezki (Sony) 	unsigned long nva_start_addr, unsigned long size)
95968ad4a33SUladzislau Rezki (Sony) {
96068ad4a33SUladzislau Rezki (Sony) 	enum fit_type type;
96168ad4a33SUladzislau Rezki (Sony) 
96268ad4a33SUladzislau Rezki (Sony) 	/* Check if it is within VA. */
96368ad4a33SUladzislau Rezki (Sony) 	if (nva_start_addr < va->va_start ||
96468ad4a33SUladzislau Rezki (Sony) 			nva_start_addr + size > va->va_end)
96568ad4a33SUladzislau Rezki (Sony) 		return NOTHING_FIT;
96668ad4a33SUladzislau Rezki (Sony) 
96768ad4a33SUladzislau Rezki (Sony) 	/* Now classify. */
96868ad4a33SUladzislau Rezki (Sony) 	if (va->va_start == nva_start_addr) {
96968ad4a33SUladzislau Rezki (Sony) 		if (va->va_end == nva_start_addr + size)
97068ad4a33SUladzislau Rezki (Sony) 			type = FL_FIT_TYPE;
97168ad4a33SUladzislau Rezki (Sony) 		else
97268ad4a33SUladzislau Rezki (Sony) 			type = LE_FIT_TYPE;
97368ad4a33SUladzislau Rezki (Sony) 	} else if (va->va_end == nva_start_addr + size) {
97468ad4a33SUladzislau Rezki (Sony) 		type = RE_FIT_TYPE;
97568ad4a33SUladzislau Rezki (Sony) 	} else {
97668ad4a33SUladzislau Rezki (Sony) 		type = NE_FIT_TYPE;
97768ad4a33SUladzislau Rezki (Sony) 	}
97868ad4a33SUladzislau Rezki (Sony) 
97968ad4a33SUladzislau Rezki (Sony) 	return type;
98068ad4a33SUladzislau Rezki (Sony) }
98168ad4a33SUladzislau Rezki (Sony) 
98268ad4a33SUladzislau Rezki (Sony) static __always_inline int
98368ad4a33SUladzislau Rezki (Sony) adjust_va_to_fit_type(struct vmap_area *va,
98468ad4a33SUladzislau Rezki (Sony) 	unsigned long nva_start_addr, unsigned long size,
98568ad4a33SUladzislau Rezki (Sony) 	enum fit_type type)
98668ad4a33SUladzislau Rezki (Sony) {
9872c929233SArnd Bergmann 	struct vmap_area *lva = NULL;
98868ad4a33SUladzislau Rezki (Sony) 
98968ad4a33SUladzislau Rezki (Sony) 	if (type == FL_FIT_TYPE) {
99068ad4a33SUladzislau Rezki (Sony) 		/*
99168ad4a33SUladzislau Rezki (Sony) 		 * No need to split VA, it fully fits.
99268ad4a33SUladzislau Rezki (Sony) 		 *
99368ad4a33SUladzislau Rezki (Sony) 		 * |               |
99468ad4a33SUladzislau Rezki (Sony) 		 * V      NVA      V
99568ad4a33SUladzislau Rezki (Sony) 		 * |---------------|
99668ad4a33SUladzislau Rezki (Sony) 		 */
99768ad4a33SUladzislau Rezki (Sony) 		unlink_va(va, &free_vmap_area_root);
99868ad4a33SUladzislau Rezki (Sony) 		kmem_cache_free(vmap_area_cachep, va);
99968ad4a33SUladzislau Rezki (Sony) 	} else if (type == LE_FIT_TYPE) {
100068ad4a33SUladzislau Rezki (Sony) 		/*
100168ad4a33SUladzislau Rezki (Sony) 		 * Split left edge of fit VA.
100268ad4a33SUladzislau Rezki (Sony) 		 *
100368ad4a33SUladzislau Rezki (Sony) 		 * |       |
100468ad4a33SUladzislau Rezki (Sony) 		 * V  NVA  V   R
100568ad4a33SUladzislau Rezki (Sony) 		 * |-------|-------|
100668ad4a33SUladzislau Rezki (Sony) 		 */
100768ad4a33SUladzislau Rezki (Sony) 		va->va_start += size;
100868ad4a33SUladzislau Rezki (Sony) 	} else if (type == RE_FIT_TYPE) {
100968ad4a33SUladzislau Rezki (Sony) 		/*
101068ad4a33SUladzislau Rezki (Sony) 		 * Split right edge of fit VA.
101168ad4a33SUladzislau Rezki (Sony) 		 *
101268ad4a33SUladzislau Rezki (Sony) 		 *         |       |
101368ad4a33SUladzislau Rezki (Sony) 		 *     L   V  NVA  V
101468ad4a33SUladzislau Rezki (Sony) 		 * |-------|-------|
101568ad4a33SUladzislau Rezki (Sony) 		 */
101668ad4a33SUladzislau Rezki (Sony) 		va->va_end = nva_start_addr;
101768ad4a33SUladzislau Rezki (Sony) 	} else if (type == NE_FIT_TYPE) {
101868ad4a33SUladzislau Rezki (Sony) 		/*
101968ad4a33SUladzislau Rezki (Sony) 		 * Split no edge of fit VA.
102068ad4a33SUladzislau Rezki (Sony) 		 *
102168ad4a33SUladzislau Rezki (Sony) 		 *     |       |
102268ad4a33SUladzislau Rezki (Sony) 		 *   L V  NVA  V R
102368ad4a33SUladzislau Rezki (Sony) 		 * |---|-------|---|
102468ad4a33SUladzislau Rezki (Sony) 		 */
102582dd23e8SUladzislau Rezki (Sony) 		lva = __this_cpu_xchg(ne_fit_preload_node, NULL);
102682dd23e8SUladzislau Rezki (Sony) 		if (unlikely(!lva)) {
102782dd23e8SUladzislau Rezki (Sony) 			/*
102882dd23e8SUladzislau Rezki (Sony) 			 * For percpu allocator we do not do any pre-allocation
102982dd23e8SUladzislau Rezki (Sony) 			 * and leave it as it is. The reason is it most likely
103082dd23e8SUladzislau Rezki (Sony) 			 * never ends up with NE_FIT_TYPE splitting. In case of
103182dd23e8SUladzislau Rezki (Sony) 			 * percpu allocations offsets and sizes are aligned to
103282dd23e8SUladzislau Rezki (Sony) 			 * fixed align request, i.e. RE_FIT_TYPE and FL_FIT_TYPE
103382dd23e8SUladzislau Rezki (Sony) 			 * are its main fitting cases.
103482dd23e8SUladzislau Rezki (Sony) 			 *
103582dd23e8SUladzislau Rezki (Sony) 			 * There are a few exceptions though, as an example it is
103682dd23e8SUladzislau Rezki (Sony) 			 * a first allocation (early boot up) when we have "one"
103782dd23e8SUladzislau Rezki (Sony) 			 * big free space that has to be split.
1038060650a2SUladzislau Rezki (Sony) 			 *
1039060650a2SUladzislau Rezki (Sony) 			 * Also we can hit this path in case of regular "vmap"
1040060650a2SUladzislau Rezki (Sony) 			 * allocations, if "this" current CPU was not preloaded.
1041060650a2SUladzislau Rezki (Sony) 			 * See the comment in alloc_vmap_area() why. If so, then
1042060650a2SUladzislau Rezki (Sony) 			 * GFP_NOWAIT is used instead to get an extra object for
1043060650a2SUladzislau Rezki (Sony) 			 * split purpose. That is rare and most time does not
1044060650a2SUladzislau Rezki (Sony) 			 * occur.
1045060650a2SUladzislau Rezki (Sony) 			 *
1046060650a2SUladzislau Rezki (Sony) 			 * What happens if an allocation gets failed. Basically,
1047060650a2SUladzislau Rezki (Sony) 			 * an "overflow" path is triggered to purge lazily freed
1048060650a2SUladzislau Rezki (Sony) 			 * areas to free some memory, then, the "retry" path is
1049060650a2SUladzislau Rezki (Sony) 			 * triggered to repeat one more time. See more details
1050060650a2SUladzislau Rezki (Sony) 			 * in alloc_vmap_area() function.
105182dd23e8SUladzislau Rezki (Sony) 			 */
105268ad4a33SUladzislau Rezki (Sony) 			lva = kmem_cache_alloc(vmap_area_cachep, GFP_NOWAIT);
105382dd23e8SUladzislau Rezki (Sony) 			if (!lva)
105468ad4a33SUladzislau Rezki (Sony) 				return -1;
105582dd23e8SUladzislau Rezki (Sony) 		}
105668ad4a33SUladzislau Rezki (Sony) 
105768ad4a33SUladzislau Rezki (Sony) 		/*
105868ad4a33SUladzislau Rezki (Sony) 		 * Build the remainder.
105968ad4a33SUladzislau Rezki (Sony) 		 */
106068ad4a33SUladzislau Rezki (Sony) 		lva->va_start = va->va_start;
106168ad4a33SUladzislau Rezki (Sony) 		lva->va_end = nva_start_addr;
106268ad4a33SUladzislau Rezki (Sony) 
106368ad4a33SUladzislau Rezki (Sony) 		/*
106468ad4a33SUladzislau Rezki (Sony) 		 * Shrink this VA to remaining size.
106568ad4a33SUladzislau Rezki (Sony) 		 */
106668ad4a33SUladzislau Rezki (Sony) 		va->va_start = nva_start_addr + size;
106768ad4a33SUladzislau Rezki (Sony) 	} else {
106868ad4a33SUladzislau Rezki (Sony) 		return -1;
106968ad4a33SUladzislau Rezki (Sony) 	}
107068ad4a33SUladzislau Rezki (Sony) 
107168ad4a33SUladzislau Rezki (Sony) 	if (type != FL_FIT_TYPE) {
107268ad4a33SUladzislau Rezki (Sony) 		augment_tree_propagate_from(va);
107368ad4a33SUladzislau Rezki (Sony) 
10742c929233SArnd Bergmann 		if (lva)	/* type == NE_FIT_TYPE */
107568ad4a33SUladzislau Rezki (Sony) 			insert_vmap_area_augment(lva, &va->rb_node,
107668ad4a33SUladzislau Rezki (Sony) 				&free_vmap_area_root, &free_vmap_area_list);
107768ad4a33SUladzislau Rezki (Sony) 	}
107868ad4a33SUladzislau Rezki (Sony) 
107968ad4a33SUladzislau Rezki (Sony) 	return 0;
108068ad4a33SUladzislau Rezki (Sony) }
108168ad4a33SUladzislau Rezki (Sony) 
108268ad4a33SUladzislau Rezki (Sony) /*
108368ad4a33SUladzislau Rezki (Sony)  * Returns a start address of the newly allocated area, if success.
108468ad4a33SUladzislau Rezki (Sony)  * Otherwise a vend is returned that indicates failure.
108568ad4a33SUladzislau Rezki (Sony)  */
108668ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long
108768ad4a33SUladzislau Rezki (Sony) __alloc_vmap_area(unsigned long size, unsigned long align,
1088cacca6baSUladzislau Rezki (Sony) 	unsigned long vstart, unsigned long vend)
108968ad4a33SUladzislau Rezki (Sony) {
109068ad4a33SUladzislau Rezki (Sony) 	unsigned long nva_start_addr;
109168ad4a33SUladzislau Rezki (Sony) 	struct vmap_area *va;
109268ad4a33SUladzislau Rezki (Sony) 	enum fit_type type;
109368ad4a33SUladzislau Rezki (Sony) 	int ret;
109468ad4a33SUladzislau Rezki (Sony) 
109568ad4a33SUladzislau Rezki (Sony) 	va = find_vmap_lowest_match(size, align, vstart);
109668ad4a33SUladzislau Rezki (Sony) 	if (unlikely(!va))
109768ad4a33SUladzislau Rezki (Sony) 		return vend;
109868ad4a33SUladzislau Rezki (Sony) 
109968ad4a33SUladzislau Rezki (Sony) 	if (va->va_start > vstart)
110068ad4a33SUladzislau Rezki (Sony) 		nva_start_addr = ALIGN(va->va_start, align);
110168ad4a33SUladzislau Rezki (Sony) 	else
110268ad4a33SUladzislau Rezki (Sony) 		nva_start_addr = ALIGN(vstart, align);
110368ad4a33SUladzislau Rezki (Sony) 
110468ad4a33SUladzislau Rezki (Sony) 	/* Check the "vend" restriction. */
110568ad4a33SUladzislau Rezki (Sony) 	if (nva_start_addr + size > vend)
110668ad4a33SUladzislau Rezki (Sony) 		return vend;
110768ad4a33SUladzislau Rezki (Sony) 
110868ad4a33SUladzislau Rezki (Sony) 	/* Classify what we have found. */
110968ad4a33SUladzislau Rezki (Sony) 	type = classify_va_fit_type(va, nva_start_addr, size);
111068ad4a33SUladzislau Rezki (Sony) 	if (WARN_ON_ONCE(type == NOTHING_FIT))
111168ad4a33SUladzislau Rezki (Sony) 		return vend;
111268ad4a33SUladzislau Rezki (Sony) 
111368ad4a33SUladzislau Rezki (Sony) 	/* Update the free vmap_area. */
111468ad4a33SUladzislau Rezki (Sony) 	ret = adjust_va_to_fit_type(va, nva_start_addr, size, type);
111568ad4a33SUladzislau Rezki (Sony) 	if (ret)
111668ad4a33SUladzislau Rezki (Sony) 		return vend;
111768ad4a33SUladzislau Rezki (Sony) 
1118a6cf4e0fSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_LOWEST_MATCH_CHECK
1119a6cf4e0fSUladzislau Rezki (Sony) 	find_vmap_lowest_match_check(size);
1120a6cf4e0fSUladzislau Rezki (Sony) #endif
1121a6cf4e0fSUladzislau Rezki (Sony) 
112268ad4a33SUladzislau Rezki (Sony) 	return nva_start_addr;
112368ad4a33SUladzislau Rezki (Sony) }
11244da56b99SChris Wilson 
1125db64fe02SNick Piggin /*
1126d98c9e83SAndrey Ryabinin  * Free a region of KVA allocated by alloc_vmap_area
1127d98c9e83SAndrey Ryabinin  */
1128d98c9e83SAndrey Ryabinin static void free_vmap_area(struct vmap_area *va)
1129d98c9e83SAndrey Ryabinin {
1130d98c9e83SAndrey Ryabinin 	/*
1131d98c9e83SAndrey Ryabinin 	 * Remove from the busy tree/list.
1132d98c9e83SAndrey Ryabinin 	 */
1133d98c9e83SAndrey Ryabinin 	spin_lock(&vmap_area_lock);
1134d98c9e83SAndrey Ryabinin 	unlink_va(va, &vmap_area_root);
1135d98c9e83SAndrey Ryabinin 	spin_unlock(&vmap_area_lock);
1136d98c9e83SAndrey Ryabinin 
1137d98c9e83SAndrey Ryabinin 	/*
1138d98c9e83SAndrey Ryabinin 	 * Insert/Merge it back to the free tree/list.
1139d98c9e83SAndrey Ryabinin 	 */
1140d98c9e83SAndrey Ryabinin 	spin_lock(&free_vmap_area_lock);
1141d98c9e83SAndrey Ryabinin 	merge_or_add_vmap_area(va, &free_vmap_area_root, &free_vmap_area_list);
1142d98c9e83SAndrey Ryabinin 	spin_unlock(&free_vmap_area_lock);
1143d98c9e83SAndrey Ryabinin }
1144d98c9e83SAndrey Ryabinin 
1145d98c9e83SAndrey Ryabinin /*
1146db64fe02SNick Piggin  * Allocate a region of KVA of the specified size and alignment, within the
1147db64fe02SNick Piggin  * vstart and vend.
1148db64fe02SNick Piggin  */
1149db64fe02SNick Piggin static struct vmap_area *alloc_vmap_area(unsigned long size,
1150db64fe02SNick Piggin 				unsigned long align,
1151db64fe02SNick Piggin 				unsigned long vstart, unsigned long vend,
1152db64fe02SNick Piggin 				int node, gfp_t gfp_mask)
1153db64fe02SNick Piggin {
115482dd23e8SUladzislau Rezki (Sony) 	struct vmap_area *va, *pva;
11551da177e4SLinus Torvalds 	unsigned long addr;
1156db64fe02SNick Piggin 	int purged = 0;
1157d98c9e83SAndrey Ryabinin 	int ret;
1158db64fe02SNick Piggin 
11597766970cSNick Piggin 	BUG_ON(!size);
1160891c49abSAlexander Kuleshov 	BUG_ON(offset_in_page(size));
116189699605SNick Piggin 	BUG_ON(!is_power_of_2(align));
1162db64fe02SNick Piggin 
116368ad4a33SUladzislau Rezki (Sony) 	if (unlikely(!vmap_initialized))
116468ad4a33SUladzislau Rezki (Sony) 		return ERR_PTR(-EBUSY);
116568ad4a33SUladzislau Rezki (Sony) 
11665803ed29SChristoph Hellwig 	might_sleep();
1167f07116d7SUladzislau Rezki (Sony) 	gfp_mask = gfp_mask & GFP_RECLAIM_MASK;
11684da56b99SChris Wilson 
1169f07116d7SUladzislau Rezki (Sony) 	va = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node);
1170db64fe02SNick Piggin 	if (unlikely(!va))
1171db64fe02SNick Piggin 		return ERR_PTR(-ENOMEM);
1172db64fe02SNick Piggin 
11737f88f88fSCatalin Marinas 	/*
11747f88f88fSCatalin Marinas 	 * Only scan the relevant parts containing pointers to other objects
11757f88f88fSCatalin Marinas 	 * to avoid false negatives.
11767f88f88fSCatalin Marinas 	 */
1177f07116d7SUladzislau Rezki (Sony) 	kmemleak_scan_area(&va->rb_node, SIZE_MAX, gfp_mask);
11787f88f88fSCatalin Marinas 
1179db64fe02SNick Piggin retry:
118082dd23e8SUladzislau Rezki (Sony) 	/*
118181f1ba58SUladzislau Rezki (Sony) 	 * Preload this CPU with one extra vmap_area object. It is used
118281f1ba58SUladzislau Rezki (Sony) 	 * when fit type of free area is NE_FIT_TYPE. Please note, it
118381f1ba58SUladzislau Rezki (Sony) 	 * does not guarantee that an allocation occurs on a CPU that
118481f1ba58SUladzislau Rezki (Sony) 	 * is preloaded, instead we minimize the case when it is not.
118581f1ba58SUladzislau Rezki (Sony) 	 * It can happen because of cpu migration, because there is a
118681f1ba58SUladzislau Rezki (Sony) 	 * race until the below spinlock is taken.
118782dd23e8SUladzislau Rezki (Sony) 	 *
118882dd23e8SUladzislau Rezki (Sony) 	 * The preload is done in non-atomic context, thus it allows us
118982dd23e8SUladzislau Rezki (Sony) 	 * to use more permissive allocation masks to be more stable under
119081f1ba58SUladzislau Rezki (Sony) 	 * low memory condition and high memory pressure. In rare case,
119181f1ba58SUladzislau Rezki (Sony) 	 * if not preloaded, GFP_NOWAIT is used.
119282dd23e8SUladzislau Rezki (Sony) 	 *
119381f1ba58SUladzislau Rezki (Sony) 	 * Set "pva" to NULL here, because of "retry" path.
119482dd23e8SUladzislau Rezki (Sony) 	 */
119581f1ba58SUladzislau Rezki (Sony) 	pva = NULL;
119682dd23e8SUladzislau Rezki (Sony) 
119781f1ba58SUladzislau Rezki (Sony) 	if (!this_cpu_read(ne_fit_preload_node))
119881f1ba58SUladzislau Rezki (Sony) 		/*
119981f1ba58SUladzislau Rezki (Sony) 		 * Even if it fails we do not really care about that.
120081f1ba58SUladzislau Rezki (Sony) 		 * Just proceed as it is. If needed "overflow" path
120181f1ba58SUladzislau Rezki (Sony) 		 * will refill the cache we allocate from.
120281f1ba58SUladzislau Rezki (Sony) 		 */
1203f07116d7SUladzislau Rezki (Sony) 		pva = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node);
120482dd23e8SUladzislau Rezki (Sony) 
1205e36176beSUladzislau Rezki (Sony) 	spin_lock(&free_vmap_area_lock);
120681f1ba58SUladzislau Rezki (Sony) 
120781f1ba58SUladzislau Rezki (Sony) 	if (pva && __this_cpu_cmpxchg(ne_fit_preload_node, NULL, pva))
120881f1ba58SUladzislau Rezki (Sony) 		kmem_cache_free(vmap_area_cachep, pva);
120968ad4a33SUladzislau Rezki (Sony) 
121089699605SNick Piggin 	/*
121168ad4a33SUladzislau Rezki (Sony) 	 * If an allocation fails, the "vend" address is
121268ad4a33SUladzislau Rezki (Sony) 	 * returned. Therefore trigger the overflow path.
121389699605SNick Piggin 	 */
1214cacca6baSUladzislau Rezki (Sony) 	addr = __alloc_vmap_area(size, align, vstart, vend);
1215e36176beSUladzislau Rezki (Sony) 	spin_unlock(&free_vmap_area_lock);
1216e36176beSUladzislau Rezki (Sony) 
121768ad4a33SUladzislau Rezki (Sony) 	if (unlikely(addr == vend))
121889699605SNick Piggin 		goto overflow;
121989699605SNick Piggin 
122089699605SNick Piggin 	va->va_start = addr;
122189699605SNick Piggin 	va->va_end = addr + size;
1222688fcbfcSPengfei Li 	va->vm = NULL;
122368ad4a33SUladzislau Rezki (Sony) 
1224d98c9e83SAndrey Ryabinin 
1225e36176beSUladzislau Rezki (Sony) 	spin_lock(&vmap_area_lock);
1226e36176beSUladzislau Rezki (Sony) 	insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
122789699605SNick Piggin 	spin_unlock(&vmap_area_lock);
122889699605SNick Piggin 
122961e16557SWang Xiaoqiang 	BUG_ON(!IS_ALIGNED(va->va_start, align));
123089699605SNick Piggin 	BUG_ON(va->va_start < vstart);
123189699605SNick Piggin 	BUG_ON(va->va_end > vend);
123289699605SNick Piggin 
1233d98c9e83SAndrey Ryabinin 	ret = kasan_populate_vmalloc(addr, size);
1234d98c9e83SAndrey Ryabinin 	if (ret) {
1235d98c9e83SAndrey Ryabinin 		free_vmap_area(va);
1236d98c9e83SAndrey Ryabinin 		return ERR_PTR(ret);
1237d98c9e83SAndrey Ryabinin 	}
1238d98c9e83SAndrey Ryabinin 
123989699605SNick Piggin 	return va;
124089699605SNick Piggin 
12417766970cSNick Piggin overflow:
1242db64fe02SNick Piggin 	if (!purged) {
1243db64fe02SNick Piggin 		purge_vmap_area_lazy();
1244db64fe02SNick Piggin 		purged = 1;
1245db64fe02SNick Piggin 		goto retry;
1246db64fe02SNick Piggin 	}
12474da56b99SChris Wilson 
12484da56b99SChris Wilson 	if (gfpflags_allow_blocking(gfp_mask)) {
12494da56b99SChris Wilson 		unsigned long freed = 0;
12504da56b99SChris Wilson 		blocking_notifier_call_chain(&vmap_notify_list, 0, &freed);
12514da56b99SChris Wilson 		if (freed > 0) {
12524da56b99SChris Wilson 			purged = 0;
12534da56b99SChris Wilson 			goto retry;
12544da56b99SChris Wilson 		}
12554da56b99SChris Wilson 	}
12564da56b99SChris Wilson 
125703497d76SFlorian Fainelli 	if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit())
1258756a025fSJoe Perches 		pr_warn("vmap allocation for size %lu failed: use vmalloc=<size> to increase size\n",
1259756a025fSJoe Perches 			size);
126068ad4a33SUladzislau Rezki (Sony) 
126168ad4a33SUladzislau Rezki (Sony) 	kmem_cache_free(vmap_area_cachep, va);
1262db64fe02SNick Piggin 	return ERR_PTR(-EBUSY);
1263db64fe02SNick Piggin }
1264db64fe02SNick Piggin 
12654da56b99SChris Wilson int register_vmap_purge_notifier(struct notifier_block *nb)
12664da56b99SChris Wilson {
12674da56b99SChris Wilson 	return blocking_notifier_chain_register(&vmap_notify_list, nb);
12684da56b99SChris Wilson }
12694da56b99SChris Wilson EXPORT_SYMBOL_GPL(register_vmap_purge_notifier);
12704da56b99SChris Wilson 
12714da56b99SChris Wilson int unregister_vmap_purge_notifier(struct notifier_block *nb)
12724da56b99SChris Wilson {
12734da56b99SChris Wilson 	return blocking_notifier_chain_unregister(&vmap_notify_list, nb);
12744da56b99SChris Wilson }
12754da56b99SChris Wilson EXPORT_SYMBOL_GPL(unregister_vmap_purge_notifier);
12764da56b99SChris Wilson 
1277db64fe02SNick Piggin /*
1278db64fe02SNick Piggin  * lazy_max_pages is the maximum amount of virtual address space we gather up
1279db64fe02SNick Piggin  * before attempting to purge with a TLB flush.
1280db64fe02SNick Piggin  *
1281db64fe02SNick Piggin  * There is a tradeoff here: a larger number will cover more kernel page tables
1282db64fe02SNick Piggin  * and take slightly longer to purge, but it will linearly reduce the number of
1283db64fe02SNick Piggin  * global TLB flushes that must be performed. It would seem natural to scale
1284db64fe02SNick Piggin  * this number up linearly with the number of CPUs (because vmapping activity
1285db64fe02SNick Piggin  * could also scale linearly with the number of CPUs), however it is likely
1286db64fe02SNick Piggin  * that in practice, workloads might be constrained in other ways that mean
1287db64fe02SNick Piggin  * vmap activity will not scale linearly with CPUs. Also, I want to be
1288db64fe02SNick Piggin  * conservative and not introduce a big latency on huge systems, so go with
1289db64fe02SNick Piggin  * a less aggressive log scale. It will still be an improvement over the old
1290db64fe02SNick Piggin  * code, and it will be simple to change the scale factor if we find that it
1291db64fe02SNick Piggin  * becomes a problem on bigger systems.
1292db64fe02SNick Piggin  */
1293db64fe02SNick Piggin static unsigned long lazy_max_pages(void)
1294db64fe02SNick Piggin {
1295db64fe02SNick Piggin 	unsigned int log;
1296db64fe02SNick Piggin 
1297db64fe02SNick Piggin 	log = fls(num_online_cpus());
1298db64fe02SNick Piggin 
1299db64fe02SNick Piggin 	return log * (32UL * 1024 * 1024 / PAGE_SIZE);
1300db64fe02SNick Piggin }
1301db64fe02SNick Piggin 
13024d36e6f8SUladzislau Rezki (Sony) static atomic_long_t vmap_lazy_nr = ATOMIC_LONG_INIT(0);
1303db64fe02SNick Piggin 
13040574ecd1SChristoph Hellwig /*
13050574ecd1SChristoph Hellwig  * Serialize vmap purging.  There is no actual criticial section protected
13060574ecd1SChristoph Hellwig  * by this look, but we want to avoid concurrent calls for performance
13070574ecd1SChristoph Hellwig  * reasons and to make the pcpu_get_vm_areas more deterministic.
13080574ecd1SChristoph Hellwig  */
1309f9e09977SChristoph Hellwig static DEFINE_MUTEX(vmap_purge_lock);
13100574ecd1SChristoph Hellwig 
131102b709dfSNick Piggin /* for per-CPU blocks */
131202b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void);
131302b709dfSNick Piggin 
1314db64fe02SNick Piggin /*
13153ee48b6aSCliff Wickman  * called before a call to iounmap() if the caller wants vm_area_struct's
13163ee48b6aSCliff Wickman  * immediately freed.
13173ee48b6aSCliff Wickman  */
13183ee48b6aSCliff Wickman void set_iounmap_nonlazy(void)
13193ee48b6aSCliff Wickman {
13204d36e6f8SUladzislau Rezki (Sony) 	atomic_long_set(&vmap_lazy_nr, lazy_max_pages()+1);
13213ee48b6aSCliff Wickman }
13223ee48b6aSCliff Wickman 
13233ee48b6aSCliff Wickman /*
1324db64fe02SNick Piggin  * Purges all lazily-freed vmap areas.
1325db64fe02SNick Piggin  */
13260574ecd1SChristoph Hellwig static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end)
1327db64fe02SNick Piggin {
13284d36e6f8SUladzislau Rezki (Sony) 	unsigned long resched_threshold;
132980c4bd7aSChris Wilson 	struct llist_node *valist;
1330db64fe02SNick Piggin 	struct vmap_area *va;
1331cbb76676SVegard Nossum 	struct vmap_area *n_va;
1332db64fe02SNick Piggin 
13330574ecd1SChristoph Hellwig 	lockdep_assert_held(&vmap_purge_lock);
133402b709dfSNick Piggin 
133580c4bd7aSChris Wilson 	valist = llist_del_all(&vmap_purge_list);
133668571be9SUladzislau Rezki (Sony) 	if (unlikely(valist == NULL))
133768571be9SUladzislau Rezki (Sony) 		return false;
133868571be9SUladzislau Rezki (Sony) 
133968571be9SUladzislau Rezki (Sony) 	/*
134068571be9SUladzislau Rezki (Sony) 	 * TODO: to calculate a flush range without looping.
134168571be9SUladzislau Rezki (Sony) 	 * The list can be up to lazy_max_pages() elements.
134268571be9SUladzislau Rezki (Sony) 	 */
134380c4bd7aSChris Wilson 	llist_for_each_entry(va, valist, purge_list) {
13440574ecd1SChristoph Hellwig 		if (va->va_start < start)
13450574ecd1SChristoph Hellwig 			start = va->va_start;
13460574ecd1SChristoph Hellwig 		if (va->va_end > end)
13470574ecd1SChristoph Hellwig 			end = va->va_end;
1348db64fe02SNick Piggin 	}
1349db64fe02SNick Piggin 
13500574ecd1SChristoph Hellwig 	flush_tlb_kernel_range(start, end);
13514d36e6f8SUladzislau Rezki (Sony) 	resched_threshold = lazy_max_pages() << 1;
1352db64fe02SNick Piggin 
1353e36176beSUladzislau Rezki (Sony) 	spin_lock(&free_vmap_area_lock);
1354763b218dSJoel Fernandes 	llist_for_each_entry_safe(va, n_va, valist, purge_list) {
13554d36e6f8SUladzislau Rezki (Sony) 		unsigned long nr = (va->va_end - va->va_start) >> PAGE_SHIFT;
13563c5c3cfbSDaniel Axtens 		unsigned long orig_start = va->va_start;
13573c5c3cfbSDaniel Axtens 		unsigned long orig_end = va->va_end;
1358763b218dSJoel Fernandes 
1359dd3b8353SUladzislau Rezki (Sony) 		/*
1360dd3b8353SUladzislau Rezki (Sony) 		 * Finally insert or merge lazily-freed area. It is
1361dd3b8353SUladzislau Rezki (Sony) 		 * detached and there is no need to "unlink" it from
1362dd3b8353SUladzislau Rezki (Sony) 		 * anything.
1363dd3b8353SUladzislau Rezki (Sony) 		 */
13643c5c3cfbSDaniel Axtens 		va = merge_or_add_vmap_area(va, &free_vmap_area_root,
13653c5c3cfbSDaniel Axtens 					    &free_vmap_area_list);
13663c5c3cfbSDaniel Axtens 
13679c801f61SUladzislau Rezki (Sony) 		if (!va)
13689c801f61SUladzislau Rezki (Sony) 			continue;
13699c801f61SUladzislau Rezki (Sony) 
13703c5c3cfbSDaniel Axtens 		if (is_vmalloc_or_module_addr((void *)orig_start))
13713c5c3cfbSDaniel Axtens 			kasan_release_vmalloc(orig_start, orig_end,
13723c5c3cfbSDaniel Axtens 					      va->va_start, va->va_end);
1373dd3b8353SUladzislau Rezki (Sony) 
13744d36e6f8SUladzislau Rezki (Sony) 		atomic_long_sub(nr, &vmap_lazy_nr);
137568571be9SUladzislau Rezki (Sony) 
13764d36e6f8SUladzislau Rezki (Sony) 		if (atomic_long_read(&vmap_lazy_nr) < resched_threshold)
1377e36176beSUladzislau Rezki (Sony) 			cond_resched_lock(&free_vmap_area_lock);
1378763b218dSJoel Fernandes 	}
1379e36176beSUladzislau Rezki (Sony) 	spin_unlock(&free_vmap_area_lock);
13800574ecd1SChristoph Hellwig 	return true;
1381db64fe02SNick Piggin }
1382db64fe02SNick Piggin 
1383db64fe02SNick Piggin /*
1384496850e5SNick Piggin  * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
1385496850e5SNick Piggin  * is already purging.
1386496850e5SNick Piggin  */
1387496850e5SNick Piggin static void try_purge_vmap_area_lazy(void)
1388496850e5SNick Piggin {
1389f9e09977SChristoph Hellwig 	if (mutex_trylock(&vmap_purge_lock)) {
13900574ecd1SChristoph Hellwig 		__purge_vmap_area_lazy(ULONG_MAX, 0);
1391f9e09977SChristoph Hellwig 		mutex_unlock(&vmap_purge_lock);
13920574ecd1SChristoph Hellwig 	}
1393496850e5SNick Piggin }
1394496850e5SNick Piggin 
1395496850e5SNick Piggin /*
1396db64fe02SNick Piggin  * Kick off a purge of the outstanding lazy areas.
1397db64fe02SNick Piggin  */
1398db64fe02SNick Piggin static void purge_vmap_area_lazy(void)
1399db64fe02SNick Piggin {
1400f9e09977SChristoph Hellwig 	mutex_lock(&vmap_purge_lock);
14010574ecd1SChristoph Hellwig 	purge_fragmented_blocks_allcpus();
14020574ecd1SChristoph Hellwig 	__purge_vmap_area_lazy(ULONG_MAX, 0);
1403f9e09977SChristoph Hellwig 	mutex_unlock(&vmap_purge_lock);
1404db64fe02SNick Piggin }
1405db64fe02SNick Piggin 
1406db64fe02SNick Piggin /*
140764141da5SJeremy Fitzhardinge  * Free a vmap area, caller ensuring that the area has been unmapped
140864141da5SJeremy Fitzhardinge  * and flush_cache_vunmap had been called for the correct range
140964141da5SJeremy Fitzhardinge  * previously.
1410db64fe02SNick Piggin  */
141164141da5SJeremy Fitzhardinge static void free_vmap_area_noflush(struct vmap_area *va)
1412db64fe02SNick Piggin {
14134d36e6f8SUladzislau Rezki (Sony) 	unsigned long nr_lazy;
141480c4bd7aSChris Wilson 
1415dd3b8353SUladzislau Rezki (Sony) 	spin_lock(&vmap_area_lock);
1416dd3b8353SUladzislau Rezki (Sony) 	unlink_va(va, &vmap_area_root);
1417dd3b8353SUladzislau Rezki (Sony) 	spin_unlock(&vmap_area_lock);
1418dd3b8353SUladzislau Rezki (Sony) 
14194d36e6f8SUladzislau Rezki (Sony) 	nr_lazy = atomic_long_add_return((va->va_end - va->va_start) >>
14204d36e6f8SUladzislau Rezki (Sony) 				PAGE_SHIFT, &vmap_lazy_nr);
142180c4bd7aSChris Wilson 
142280c4bd7aSChris Wilson 	/* After this point, we may free va at any time */
142380c4bd7aSChris Wilson 	llist_add(&va->purge_list, &vmap_purge_list);
142480c4bd7aSChris Wilson 
142580c4bd7aSChris Wilson 	if (unlikely(nr_lazy > lazy_max_pages()))
1426496850e5SNick Piggin 		try_purge_vmap_area_lazy();
1427db64fe02SNick Piggin }
1428db64fe02SNick Piggin 
1429b29acbdcSNick Piggin /*
1430b29acbdcSNick Piggin  * Free and unmap a vmap area
1431b29acbdcSNick Piggin  */
1432b29acbdcSNick Piggin static void free_unmap_vmap_area(struct vmap_area *va)
1433b29acbdcSNick Piggin {
1434b29acbdcSNick Piggin 	flush_cache_vunmap(va->va_start, va->va_end);
1435855e57a1SChristoph Hellwig 	unmap_kernel_range_noflush(va->va_start, va->va_end - va->va_start);
14368e57f8acSVlastimil Babka 	if (debug_pagealloc_enabled_static())
143782a2e924SChintan Pandya 		flush_tlb_kernel_range(va->va_start, va->va_end);
143882a2e924SChintan Pandya 
1439c8eef01eSChristoph Hellwig 	free_vmap_area_noflush(va);
1440b29acbdcSNick Piggin }
1441b29acbdcSNick Piggin 
1442db64fe02SNick Piggin static struct vmap_area *find_vmap_area(unsigned long addr)
1443db64fe02SNick Piggin {
1444db64fe02SNick Piggin 	struct vmap_area *va;
1445db64fe02SNick Piggin 
1446db64fe02SNick Piggin 	spin_lock(&vmap_area_lock);
1447db64fe02SNick Piggin 	va = __find_vmap_area(addr);
1448db64fe02SNick Piggin 	spin_unlock(&vmap_area_lock);
1449db64fe02SNick Piggin 
1450db64fe02SNick Piggin 	return va;
1451db64fe02SNick Piggin }
1452db64fe02SNick Piggin 
1453db64fe02SNick Piggin /*** Per cpu kva allocator ***/
1454db64fe02SNick Piggin 
1455db64fe02SNick Piggin /*
1456db64fe02SNick Piggin  * vmap space is limited especially on 32 bit architectures. Ensure there is
1457db64fe02SNick Piggin  * room for at least 16 percpu vmap blocks per CPU.
1458db64fe02SNick Piggin  */
1459db64fe02SNick Piggin /*
1460db64fe02SNick Piggin  * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
1461db64fe02SNick Piggin  * to #define VMALLOC_SPACE		(VMALLOC_END-VMALLOC_START). Guess
1462db64fe02SNick Piggin  * instead (we just need a rough idea)
1463db64fe02SNick Piggin  */
1464db64fe02SNick Piggin #if BITS_PER_LONG == 32
1465db64fe02SNick Piggin #define VMALLOC_SPACE		(128UL*1024*1024)
1466db64fe02SNick Piggin #else
1467db64fe02SNick Piggin #define VMALLOC_SPACE		(128UL*1024*1024*1024)
1468db64fe02SNick Piggin #endif
1469db64fe02SNick Piggin 
1470db64fe02SNick Piggin #define VMALLOC_PAGES		(VMALLOC_SPACE / PAGE_SIZE)
1471db64fe02SNick Piggin #define VMAP_MAX_ALLOC		BITS_PER_LONG	/* 256K with 4K pages */
1472db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MAX	1024	/* 4MB with 4K pages */
1473db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MIN	(VMAP_MAX_ALLOC*2)
1474db64fe02SNick Piggin #define VMAP_MIN(x, y)		((x) < (y) ? (x) : (y)) /* can't use min() */
1475db64fe02SNick Piggin #define VMAP_MAX(x, y)		((x) > (y) ? (x) : (y)) /* can't use max() */
1476f982f915SClemens Ladisch #define VMAP_BBMAP_BITS		\
1477f982f915SClemens Ladisch 		VMAP_MIN(VMAP_BBMAP_BITS_MAX,	\
1478db64fe02SNick Piggin 		VMAP_MAX(VMAP_BBMAP_BITS_MIN,	\
1479f982f915SClemens Ladisch 			VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16))
1480db64fe02SNick Piggin 
1481db64fe02SNick Piggin #define VMAP_BLOCK_SIZE		(VMAP_BBMAP_BITS * PAGE_SIZE)
1482db64fe02SNick Piggin 
1483db64fe02SNick Piggin struct vmap_block_queue {
1484db64fe02SNick Piggin 	spinlock_t lock;
1485db64fe02SNick Piggin 	struct list_head free;
1486db64fe02SNick Piggin };
1487db64fe02SNick Piggin 
1488db64fe02SNick Piggin struct vmap_block {
1489db64fe02SNick Piggin 	spinlock_t lock;
1490db64fe02SNick Piggin 	struct vmap_area *va;
1491db64fe02SNick Piggin 	unsigned long free, dirty;
14927d61bfe8SRoman Pen 	unsigned long dirty_min, dirty_max; /*< dirty range */
1493db64fe02SNick Piggin 	struct list_head free_list;
1494db64fe02SNick Piggin 	struct rcu_head rcu_head;
149502b709dfSNick Piggin 	struct list_head purge;
1496db64fe02SNick Piggin };
1497db64fe02SNick Piggin 
1498db64fe02SNick Piggin /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
1499db64fe02SNick Piggin static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
1500db64fe02SNick Piggin 
1501db64fe02SNick Piggin /*
15020f14599cSMatthew Wilcox (Oracle)  * XArray of vmap blocks, indexed by address, to quickly find a vmap block
1503db64fe02SNick Piggin  * in the free path. Could get rid of this if we change the API to return a
1504db64fe02SNick Piggin  * "cookie" from alloc, to be passed to free. But no big deal yet.
1505db64fe02SNick Piggin  */
15060f14599cSMatthew Wilcox (Oracle) static DEFINE_XARRAY(vmap_blocks);
1507db64fe02SNick Piggin 
1508db64fe02SNick Piggin /*
1509db64fe02SNick Piggin  * We should probably have a fallback mechanism to allocate virtual memory
1510db64fe02SNick Piggin  * out of partially filled vmap blocks. However vmap block sizing should be
1511db64fe02SNick Piggin  * fairly reasonable according to the vmalloc size, so it shouldn't be a
1512db64fe02SNick Piggin  * big problem.
1513db64fe02SNick Piggin  */
1514db64fe02SNick Piggin 
1515db64fe02SNick Piggin static unsigned long addr_to_vb_idx(unsigned long addr)
1516db64fe02SNick Piggin {
1517db64fe02SNick Piggin 	addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
1518db64fe02SNick Piggin 	addr /= VMAP_BLOCK_SIZE;
1519db64fe02SNick Piggin 	return addr;
1520db64fe02SNick Piggin }
1521db64fe02SNick Piggin 
1522cf725ce2SRoman Pen static void *vmap_block_vaddr(unsigned long va_start, unsigned long pages_off)
1523cf725ce2SRoman Pen {
1524cf725ce2SRoman Pen 	unsigned long addr;
1525cf725ce2SRoman Pen 
1526cf725ce2SRoman Pen 	addr = va_start + (pages_off << PAGE_SHIFT);
1527cf725ce2SRoman Pen 	BUG_ON(addr_to_vb_idx(addr) != addr_to_vb_idx(va_start));
1528cf725ce2SRoman Pen 	return (void *)addr;
1529cf725ce2SRoman Pen }
1530cf725ce2SRoman Pen 
1531cf725ce2SRoman Pen /**
1532cf725ce2SRoman Pen  * new_vmap_block - allocates new vmap_block and occupies 2^order pages in this
1533cf725ce2SRoman Pen  *                  block. Of course pages number can't exceed VMAP_BBMAP_BITS
1534cf725ce2SRoman Pen  * @order:    how many 2^order pages should be occupied in newly allocated block
1535cf725ce2SRoman Pen  * @gfp_mask: flags for the page level allocator
1536cf725ce2SRoman Pen  *
1537a862f68aSMike Rapoport  * Return: virtual address in a newly allocated block or ERR_PTR(-errno)
1538cf725ce2SRoman Pen  */
1539cf725ce2SRoman Pen static void *new_vmap_block(unsigned int order, gfp_t gfp_mask)
1540db64fe02SNick Piggin {
1541db64fe02SNick Piggin 	struct vmap_block_queue *vbq;
1542db64fe02SNick Piggin 	struct vmap_block *vb;
1543db64fe02SNick Piggin 	struct vmap_area *va;
1544db64fe02SNick Piggin 	unsigned long vb_idx;
1545db64fe02SNick Piggin 	int node, err;
1546cf725ce2SRoman Pen 	void *vaddr;
1547db64fe02SNick Piggin 
1548db64fe02SNick Piggin 	node = numa_node_id();
1549db64fe02SNick Piggin 
1550db64fe02SNick Piggin 	vb = kmalloc_node(sizeof(struct vmap_block),
1551db64fe02SNick Piggin 			gfp_mask & GFP_RECLAIM_MASK, node);
1552db64fe02SNick Piggin 	if (unlikely(!vb))
1553db64fe02SNick Piggin 		return ERR_PTR(-ENOMEM);
1554db64fe02SNick Piggin 
1555db64fe02SNick Piggin 	va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
1556db64fe02SNick Piggin 					VMALLOC_START, VMALLOC_END,
1557db64fe02SNick Piggin 					node, gfp_mask);
1558ddf9c6d4STobias Klauser 	if (IS_ERR(va)) {
1559db64fe02SNick Piggin 		kfree(vb);
1560e7d86340SJulia Lawall 		return ERR_CAST(va);
1561db64fe02SNick Piggin 	}
1562db64fe02SNick Piggin 
1563cf725ce2SRoman Pen 	vaddr = vmap_block_vaddr(va->va_start, 0);
1564db64fe02SNick Piggin 	spin_lock_init(&vb->lock);
1565db64fe02SNick Piggin 	vb->va = va;
1566cf725ce2SRoman Pen 	/* At least something should be left free */
1567cf725ce2SRoman Pen 	BUG_ON(VMAP_BBMAP_BITS <= (1UL << order));
1568cf725ce2SRoman Pen 	vb->free = VMAP_BBMAP_BITS - (1UL << order);
1569db64fe02SNick Piggin 	vb->dirty = 0;
15707d61bfe8SRoman Pen 	vb->dirty_min = VMAP_BBMAP_BITS;
15717d61bfe8SRoman Pen 	vb->dirty_max = 0;
1572db64fe02SNick Piggin 	INIT_LIST_HEAD(&vb->free_list);
1573db64fe02SNick Piggin 
1574db64fe02SNick Piggin 	vb_idx = addr_to_vb_idx(va->va_start);
15750f14599cSMatthew Wilcox (Oracle) 	err = xa_insert(&vmap_blocks, vb_idx, vb, gfp_mask);
15760f14599cSMatthew Wilcox (Oracle) 	if (err) {
15770f14599cSMatthew Wilcox (Oracle) 		kfree(vb);
15780f14599cSMatthew Wilcox (Oracle) 		free_vmap_area(va);
15790f14599cSMatthew Wilcox (Oracle) 		return ERR_PTR(err);
15800f14599cSMatthew Wilcox (Oracle) 	}
1581db64fe02SNick Piggin 
1582db64fe02SNick Piggin 	vbq = &get_cpu_var(vmap_block_queue);
1583db64fe02SNick Piggin 	spin_lock(&vbq->lock);
158468ac546fSRoman Pen 	list_add_tail_rcu(&vb->free_list, &vbq->free);
1585db64fe02SNick Piggin 	spin_unlock(&vbq->lock);
15863f04ba85STejun Heo 	put_cpu_var(vmap_block_queue);
1587db64fe02SNick Piggin 
1588cf725ce2SRoman Pen 	return vaddr;
1589db64fe02SNick Piggin }
1590db64fe02SNick Piggin 
1591db64fe02SNick Piggin static void free_vmap_block(struct vmap_block *vb)
1592db64fe02SNick Piggin {
1593db64fe02SNick Piggin 	struct vmap_block *tmp;
1594db64fe02SNick Piggin 
15950f14599cSMatthew Wilcox (Oracle) 	tmp = xa_erase(&vmap_blocks, addr_to_vb_idx(vb->va->va_start));
1596db64fe02SNick Piggin 	BUG_ON(tmp != vb);
1597db64fe02SNick Piggin 
159864141da5SJeremy Fitzhardinge 	free_vmap_area_noflush(vb->va);
159922a3c7d1SLai Jiangshan 	kfree_rcu(vb, rcu_head);
1600db64fe02SNick Piggin }
1601db64fe02SNick Piggin 
160202b709dfSNick Piggin static void purge_fragmented_blocks(int cpu)
160302b709dfSNick Piggin {
160402b709dfSNick Piggin 	LIST_HEAD(purge);
160502b709dfSNick Piggin 	struct vmap_block *vb;
160602b709dfSNick Piggin 	struct vmap_block *n_vb;
160702b709dfSNick Piggin 	struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
160802b709dfSNick Piggin 
160902b709dfSNick Piggin 	rcu_read_lock();
161002b709dfSNick Piggin 	list_for_each_entry_rcu(vb, &vbq->free, free_list) {
161102b709dfSNick Piggin 
161202b709dfSNick Piggin 		if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS))
161302b709dfSNick Piggin 			continue;
161402b709dfSNick Piggin 
161502b709dfSNick Piggin 		spin_lock(&vb->lock);
161602b709dfSNick Piggin 		if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) {
161702b709dfSNick Piggin 			vb->free = 0; /* prevent further allocs after releasing lock */
161802b709dfSNick Piggin 			vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */
16197d61bfe8SRoman Pen 			vb->dirty_min = 0;
16207d61bfe8SRoman Pen 			vb->dirty_max = VMAP_BBMAP_BITS;
162102b709dfSNick Piggin 			spin_lock(&vbq->lock);
162202b709dfSNick Piggin 			list_del_rcu(&vb->free_list);
162302b709dfSNick Piggin 			spin_unlock(&vbq->lock);
162402b709dfSNick Piggin 			spin_unlock(&vb->lock);
162502b709dfSNick Piggin 			list_add_tail(&vb->purge, &purge);
162602b709dfSNick Piggin 		} else
162702b709dfSNick Piggin 			spin_unlock(&vb->lock);
162802b709dfSNick Piggin 	}
162902b709dfSNick Piggin 	rcu_read_unlock();
163002b709dfSNick Piggin 
163102b709dfSNick Piggin 	list_for_each_entry_safe(vb, n_vb, &purge, purge) {
163202b709dfSNick Piggin 		list_del(&vb->purge);
163302b709dfSNick Piggin 		free_vmap_block(vb);
163402b709dfSNick Piggin 	}
163502b709dfSNick Piggin }
163602b709dfSNick Piggin 
163702b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void)
163802b709dfSNick Piggin {
163902b709dfSNick Piggin 	int cpu;
164002b709dfSNick Piggin 
164102b709dfSNick Piggin 	for_each_possible_cpu(cpu)
164202b709dfSNick Piggin 		purge_fragmented_blocks(cpu);
164302b709dfSNick Piggin }
164402b709dfSNick Piggin 
1645db64fe02SNick Piggin static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
1646db64fe02SNick Piggin {
1647db64fe02SNick Piggin 	struct vmap_block_queue *vbq;
1648db64fe02SNick Piggin 	struct vmap_block *vb;
1649cf725ce2SRoman Pen 	void *vaddr = NULL;
1650db64fe02SNick Piggin 	unsigned int order;
1651db64fe02SNick Piggin 
1652891c49abSAlexander Kuleshov 	BUG_ON(offset_in_page(size));
1653db64fe02SNick Piggin 	BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
1654aa91c4d8SJan Kara 	if (WARN_ON(size == 0)) {
1655aa91c4d8SJan Kara 		/*
1656aa91c4d8SJan Kara 		 * Allocating 0 bytes isn't what caller wants since
1657aa91c4d8SJan Kara 		 * get_order(0) returns funny result. Just warn and terminate
1658aa91c4d8SJan Kara 		 * early.
1659aa91c4d8SJan Kara 		 */
1660aa91c4d8SJan Kara 		return NULL;
1661aa91c4d8SJan Kara 	}
1662db64fe02SNick Piggin 	order = get_order(size);
1663db64fe02SNick Piggin 
1664db64fe02SNick Piggin 	rcu_read_lock();
1665db64fe02SNick Piggin 	vbq = &get_cpu_var(vmap_block_queue);
1666db64fe02SNick Piggin 	list_for_each_entry_rcu(vb, &vbq->free, free_list) {
1667cf725ce2SRoman Pen 		unsigned long pages_off;
1668db64fe02SNick Piggin 
1669db64fe02SNick Piggin 		spin_lock(&vb->lock);
1670cf725ce2SRoman Pen 		if (vb->free < (1UL << order)) {
1671cf725ce2SRoman Pen 			spin_unlock(&vb->lock);
1672cf725ce2SRoman Pen 			continue;
1673cf725ce2SRoman Pen 		}
167402b709dfSNick Piggin 
1675cf725ce2SRoman Pen 		pages_off = VMAP_BBMAP_BITS - vb->free;
1676cf725ce2SRoman Pen 		vaddr = vmap_block_vaddr(vb->va->va_start, pages_off);
1677db64fe02SNick Piggin 		vb->free -= 1UL << order;
1678db64fe02SNick Piggin 		if (vb->free == 0) {
1679db64fe02SNick Piggin 			spin_lock(&vbq->lock);
1680de560423SNick Piggin 			list_del_rcu(&vb->free_list);
1681db64fe02SNick Piggin 			spin_unlock(&vbq->lock);
1682db64fe02SNick Piggin 		}
1683cf725ce2SRoman Pen 
1684db64fe02SNick Piggin 		spin_unlock(&vb->lock);
1685db64fe02SNick Piggin 		break;
1686db64fe02SNick Piggin 	}
168702b709dfSNick Piggin 
16883f04ba85STejun Heo 	put_cpu_var(vmap_block_queue);
1689db64fe02SNick Piggin 	rcu_read_unlock();
1690db64fe02SNick Piggin 
1691cf725ce2SRoman Pen 	/* Allocate new block if nothing was found */
1692cf725ce2SRoman Pen 	if (!vaddr)
1693cf725ce2SRoman Pen 		vaddr = new_vmap_block(order, gfp_mask);
1694db64fe02SNick Piggin 
1695cf725ce2SRoman Pen 	return vaddr;
1696db64fe02SNick Piggin }
1697db64fe02SNick Piggin 
169878a0e8c4SChristoph Hellwig static void vb_free(unsigned long addr, unsigned long size)
1699db64fe02SNick Piggin {
1700db64fe02SNick Piggin 	unsigned long offset;
1701db64fe02SNick Piggin 	unsigned int order;
1702db64fe02SNick Piggin 	struct vmap_block *vb;
1703db64fe02SNick Piggin 
1704891c49abSAlexander Kuleshov 	BUG_ON(offset_in_page(size));
1705db64fe02SNick Piggin 	BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
1706b29acbdcSNick Piggin 
170778a0e8c4SChristoph Hellwig 	flush_cache_vunmap(addr, addr + size);
1708b29acbdcSNick Piggin 
1709db64fe02SNick Piggin 	order = get_order(size);
171078a0e8c4SChristoph Hellwig 	offset = (addr & (VMAP_BLOCK_SIZE - 1)) >> PAGE_SHIFT;
17110f14599cSMatthew Wilcox (Oracle) 	vb = xa_load(&vmap_blocks, addr_to_vb_idx(addr));
1712db64fe02SNick Piggin 
1713b521c43fSChristoph Hellwig 	unmap_kernel_range_noflush(addr, size);
171464141da5SJeremy Fitzhardinge 
17158e57f8acSVlastimil Babka 	if (debug_pagealloc_enabled_static())
171678a0e8c4SChristoph Hellwig 		flush_tlb_kernel_range(addr, addr + size);
171782a2e924SChintan Pandya 
1718db64fe02SNick Piggin 	spin_lock(&vb->lock);
17197d61bfe8SRoman Pen 
17207d61bfe8SRoman Pen 	/* Expand dirty range */
17217d61bfe8SRoman Pen 	vb->dirty_min = min(vb->dirty_min, offset);
17227d61bfe8SRoman Pen 	vb->dirty_max = max(vb->dirty_max, offset + (1UL << order));
1723d086817dSMinChan Kim 
1724db64fe02SNick Piggin 	vb->dirty += 1UL << order;
1725db64fe02SNick Piggin 	if (vb->dirty == VMAP_BBMAP_BITS) {
1726de560423SNick Piggin 		BUG_ON(vb->free);
1727db64fe02SNick Piggin 		spin_unlock(&vb->lock);
1728db64fe02SNick Piggin 		free_vmap_block(vb);
1729db64fe02SNick Piggin 	} else
1730db64fe02SNick Piggin 		spin_unlock(&vb->lock);
1731db64fe02SNick Piggin }
1732db64fe02SNick Piggin 
1733868b104dSRick Edgecombe static void _vm_unmap_aliases(unsigned long start, unsigned long end, int flush)
1734db64fe02SNick Piggin {
1735db64fe02SNick Piggin 	int cpu;
1736db64fe02SNick Piggin 
17379b463334SJeremy Fitzhardinge 	if (unlikely(!vmap_initialized))
17389b463334SJeremy Fitzhardinge 		return;
17399b463334SJeremy Fitzhardinge 
17405803ed29SChristoph Hellwig 	might_sleep();
17415803ed29SChristoph Hellwig 
1742db64fe02SNick Piggin 	for_each_possible_cpu(cpu) {
1743db64fe02SNick Piggin 		struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
1744db64fe02SNick Piggin 		struct vmap_block *vb;
1745db64fe02SNick Piggin 
1746db64fe02SNick Piggin 		rcu_read_lock();
1747db64fe02SNick Piggin 		list_for_each_entry_rcu(vb, &vbq->free, free_list) {
1748db64fe02SNick Piggin 			spin_lock(&vb->lock);
17497d61bfe8SRoman Pen 			if (vb->dirty) {
17507d61bfe8SRoman Pen 				unsigned long va_start = vb->va->va_start;
1751db64fe02SNick Piggin 				unsigned long s, e;
1752b136be5eSJoonsoo Kim 
17537d61bfe8SRoman Pen 				s = va_start + (vb->dirty_min << PAGE_SHIFT);
17547d61bfe8SRoman Pen 				e = va_start + (vb->dirty_max << PAGE_SHIFT);
1755db64fe02SNick Piggin 
17567d61bfe8SRoman Pen 				start = min(s, start);
17577d61bfe8SRoman Pen 				end   = max(e, end);
17587d61bfe8SRoman Pen 
1759db64fe02SNick Piggin 				flush = 1;
1760db64fe02SNick Piggin 			}
1761db64fe02SNick Piggin 			spin_unlock(&vb->lock);
1762db64fe02SNick Piggin 		}
1763db64fe02SNick Piggin 		rcu_read_unlock();
1764db64fe02SNick Piggin 	}
1765db64fe02SNick Piggin 
1766f9e09977SChristoph Hellwig 	mutex_lock(&vmap_purge_lock);
17670574ecd1SChristoph Hellwig 	purge_fragmented_blocks_allcpus();
17680574ecd1SChristoph Hellwig 	if (!__purge_vmap_area_lazy(start, end) && flush)
17690574ecd1SChristoph Hellwig 		flush_tlb_kernel_range(start, end);
1770f9e09977SChristoph Hellwig 	mutex_unlock(&vmap_purge_lock);
1771db64fe02SNick Piggin }
1772868b104dSRick Edgecombe 
1773868b104dSRick Edgecombe /**
1774868b104dSRick Edgecombe  * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
1775868b104dSRick Edgecombe  *
1776868b104dSRick Edgecombe  * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
1777868b104dSRick Edgecombe  * to amortize TLB flushing overheads. What this means is that any page you
1778868b104dSRick Edgecombe  * have now, may, in a former life, have been mapped into kernel virtual
1779868b104dSRick Edgecombe  * address by the vmap layer and so there might be some CPUs with TLB entries
1780868b104dSRick Edgecombe  * still referencing that page (additional to the regular 1:1 kernel mapping).
1781868b104dSRick Edgecombe  *
1782868b104dSRick Edgecombe  * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
1783868b104dSRick Edgecombe  * be sure that none of the pages we have control over will have any aliases
1784868b104dSRick Edgecombe  * from the vmap layer.
1785868b104dSRick Edgecombe  */
1786868b104dSRick Edgecombe void vm_unmap_aliases(void)
1787868b104dSRick Edgecombe {
1788868b104dSRick Edgecombe 	unsigned long start = ULONG_MAX, end = 0;
1789868b104dSRick Edgecombe 	int flush = 0;
1790868b104dSRick Edgecombe 
1791868b104dSRick Edgecombe 	_vm_unmap_aliases(start, end, flush);
1792868b104dSRick Edgecombe }
1793db64fe02SNick Piggin EXPORT_SYMBOL_GPL(vm_unmap_aliases);
1794db64fe02SNick Piggin 
1795db64fe02SNick Piggin /**
1796db64fe02SNick Piggin  * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
1797db64fe02SNick Piggin  * @mem: the pointer returned by vm_map_ram
1798db64fe02SNick Piggin  * @count: the count passed to that vm_map_ram call (cannot unmap partial)
1799db64fe02SNick Piggin  */
1800db64fe02SNick Piggin void vm_unmap_ram(const void *mem, unsigned int count)
1801db64fe02SNick Piggin {
180265ee03c4SGuillermo Julián Moreno 	unsigned long size = (unsigned long)count << PAGE_SHIFT;
1803db64fe02SNick Piggin 	unsigned long addr = (unsigned long)mem;
18049c3acf60SChristoph Hellwig 	struct vmap_area *va;
1805db64fe02SNick Piggin 
18065803ed29SChristoph Hellwig 	might_sleep();
1807db64fe02SNick Piggin 	BUG_ON(!addr);
1808db64fe02SNick Piggin 	BUG_ON(addr < VMALLOC_START);
1809db64fe02SNick Piggin 	BUG_ON(addr > VMALLOC_END);
1810a1c0b1a0SShawn Lin 	BUG_ON(!PAGE_ALIGNED(addr));
1811db64fe02SNick Piggin 
1812d98c9e83SAndrey Ryabinin 	kasan_poison_vmalloc(mem, size);
1813d98c9e83SAndrey Ryabinin 
18149c3acf60SChristoph Hellwig 	if (likely(count <= VMAP_MAX_ALLOC)) {
181505e3ff95SChintan Pandya 		debug_check_no_locks_freed(mem, size);
181678a0e8c4SChristoph Hellwig 		vb_free(addr, size);
18179c3acf60SChristoph Hellwig 		return;
18189c3acf60SChristoph Hellwig 	}
18199c3acf60SChristoph Hellwig 
18209c3acf60SChristoph Hellwig 	va = find_vmap_area(addr);
18219c3acf60SChristoph Hellwig 	BUG_ON(!va);
182205e3ff95SChintan Pandya 	debug_check_no_locks_freed((void *)va->va_start,
182305e3ff95SChintan Pandya 				    (va->va_end - va->va_start));
18249c3acf60SChristoph Hellwig 	free_unmap_vmap_area(va);
1825db64fe02SNick Piggin }
1826db64fe02SNick Piggin EXPORT_SYMBOL(vm_unmap_ram);
1827db64fe02SNick Piggin 
1828db64fe02SNick Piggin /**
1829db64fe02SNick Piggin  * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
1830db64fe02SNick Piggin  * @pages: an array of pointers to the pages to be mapped
1831db64fe02SNick Piggin  * @count: number of pages
1832db64fe02SNick Piggin  * @node: prefer to allocate data structures on this node
1833e99c97adSRandy Dunlap  *
183436437638SGioh Kim  * If you use this function for less than VMAP_MAX_ALLOC pages, it could be
183536437638SGioh Kim  * faster than vmap so it's good.  But if you mix long-life and short-life
183636437638SGioh Kim  * objects with vm_map_ram(), it could consume lots of address space through
183736437638SGioh Kim  * fragmentation (especially on a 32bit machine).  You could see failures in
183836437638SGioh Kim  * the end.  Please use this function for short-lived objects.
183936437638SGioh Kim  *
1840e99c97adSRandy Dunlap  * Returns: a pointer to the address that has been mapped, or %NULL on failure
1841db64fe02SNick Piggin  */
1842d4efd79aSChristoph Hellwig void *vm_map_ram(struct page **pages, unsigned int count, int node)
1843db64fe02SNick Piggin {
184465ee03c4SGuillermo Julián Moreno 	unsigned long size = (unsigned long)count << PAGE_SHIFT;
1845db64fe02SNick Piggin 	unsigned long addr;
1846db64fe02SNick Piggin 	void *mem;
1847db64fe02SNick Piggin 
1848db64fe02SNick Piggin 	if (likely(count <= VMAP_MAX_ALLOC)) {
1849db64fe02SNick Piggin 		mem = vb_alloc(size, GFP_KERNEL);
1850db64fe02SNick Piggin 		if (IS_ERR(mem))
1851db64fe02SNick Piggin 			return NULL;
1852db64fe02SNick Piggin 		addr = (unsigned long)mem;
1853db64fe02SNick Piggin 	} else {
1854db64fe02SNick Piggin 		struct vmap_area *va;
1855db64fe02SNick Piggin 		va = alloc_vmap_area(size, PAGE_SIZE,
1856db64fe02SNick Piggin 				VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
1857db64fe02SNick Piggin 		if (IS_ERR(va))
1858db64fe02SNick Piggin 			return NULL;
1859db64fe02SNick Piggin 
1860db64fe02SNick Piggin 		addr = va->va_start;
1861db64fe02SNick Piggin 		mem = (void *)addr;
1862db64fe02SNick Piggin 	}
1863d98c9e83SAndrey Ryabinin 
1864d98c9e83SAndrey Ryabinin 	kasan_unpoison_vmalloc(mem, size);
1865d98c9e83SAndrey Ryabinin 
1866d4efd79aSChristoph Hellwig 	if (map_kernel_range(addr, size, PAGE_KERNEL, pages) < 0) {
1867db64fe02SNick Piggin 		vm_unmap_ram(mem, count);
1868db64fe02SNick Piggin 		return NULL;
1869db64fe02SNick Piggin 	}
1870db64fe02SNick Piggin 	return mem;
1871db64fe02SNick Piggin }
1872db64fe02SNick Piggin EXPORT_SYMBOL(vm_map_ram);
1873db64fe02SNick Piggin 
18744341fa45SJoonsoo Kim static struct vm_struct *vmlist __initdata;
187592eac168SMike Rapoport 
1876f0aa6617STejun Heo /**
1877be9b7335SNicolas Pitre  * vm_area_add_early - add vmap area early during boot
1878be9b7335SNicolas Pitre  * @vm: vm_struct to add
1879be9b7335SNicolas Pitre  *
1880be9b7335SNicolas Pitre  * This function is used to add fixed kernel vm area to vmlist before
1881be9b7335SNicolas Pitre  * vmalloc_init() is called.  @vm->addr, @vm->size, and @vm->flags
1882be9b7335SNicolas Pitre  * should contain proper values and the other fields should be zero.
1883be9b7335SNicolas Pitre  *
1884be9b7335SNicolas Pitre  * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1885be9b7335SNicolas Pitre  */
1886be9b7335SNicolas Pitre void __init vm_area_add_early(struct vm_struct *vm)
1887be9b7335SNicolas Pitre {
1888be9b7335SNicolas Pitre 	struct vm_struct *tmp, **p;
1889be9b7335SNicolas Pitre 
1890be9b7335SNicolas Pitre 	BUG_ON(vmap_initialized);
1891be9b7335SNicolas Pitre 	for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
1892be9b7335SNicolas Pitre 		if (tmp->addr >= vm->addr) {
1893be9b7335SNicolas Pitre 			BUG_ON(tmp->addr < vm->addr + vm->size);
1894be9b7335SNicolas Pitre 			break;
1895be9b7335SNicolas Pitre 		} else
1896be9b7335SNicolas Pitre 			BUG_ON(tmp->addr + tmp->size > vm->addr);
1897be9b7335SNicolas Pitre 	}
1898be9b7335SNicolas Pitre 	vm->next = *p;
1899be9b7335SNicolas Pitre 	*p = vm;
1900be9b7335SNicolas Pitre }
1901be9b7335SNicolas Pitre 
1902be9b7335SNicolas Pitre /**
1903f0aa6617STejun Heo  * vm_area_register_early - register vmap area early during boot
1904f0aa6617STejun Heo  * @vm: vm_struct to register
1905c0c0a293STejun Heo  * @align: requested alignment
1906f0aa6617STejun Heo  *
1907f0aa6617STejun Heo  * This function is used to register kernel vm area before
1908f0aa6617STejun Heo  * vmalloc_init() is called.  @vm->size and @vm->flags should contain
1909f0aa6617STejun Heo  * proper values on entry and other fields should be zero.  On return,
1910f0aa6617STejun Heo  * vm->addr contains the allocated address.
1911f0aa6617STejun Heo  *
1912f0aa6617STejun Heo  * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1913f0aa6617STejun Heo  */
1914c0c0a293STejun Heo void __init vm_area_register_early(struct vm_struct *vm, size_t align)
1915f0aa6617STejun Heo {
1916f0aa6617STejun Heo 	static size_t vm_init_off __initdata;
1917c0c0a293STejun Heo 	unsigned long addr;
1918f0aa6617STejun Heo 
1919c0c0a293STejun Heo 	addr = ALIGN(VMALLOC_START + vm_init_off, align);
1920c0c0a293STejun Heo 	vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START;
1921c0c0a293STejun Heo 
1922c0c0a293STejun Heo 	vm->addr = (void *)addr;
1923f0aa6617STejun Heo 
1924be9b7335SNicolas Pitre 	vm_area_add_early(vm);
1925f0aa6617STejun Heo }
1926f0aa6617STejun Heo 
192768ad4a33SUladzislau Rezki (Sony) static void vmap_init_free_space(void)
192868ad4a33SUladzislau Rezki (Sony) {
192968ad4a33SUladzislau Rezki (Sony) 	unsigned long vmap_start = 1;
193068ad4a33SUladzislau Rezki (Sony) 	const unsigned long vmap_end = ULONG_MAX;
193168ad4a33SUladzislau Rezki (Sony) 	struct vmap_area *busy, *free;
193268ad4a33SUladzislau Rezki (Sony) 
193368ad4a33SUladzislau Rezki (Sony) 	/*
193468ad4a33SUladzislau Rezki (Sony) 	 *     B     F     B     B     B     F
193568ad4a33SUladzislau Rezki (Sony) 	 * -|-----|.....|-----|-----|-----|.....|-
193668ad4a33SUladzislau Rezki (Sony) 	 *  |           The KVA space           |
193768ad4a33SUladzislau Rezki (Sony) 	 *  |<--------------------------------->|
193868ad4a33SUladzislau Rezki (Sony) 	 */
193968ad4a33SUladzislau Rezki (Sony) 	list_for_each_entry(busy, &vmap_area_list, list) {
194068ad4a33SUladzislau Rezki (Sony) 		if (busy->va_start - vmap_start > 0) {
194168ad4a33SUladzislau Rezki (Sony) 			free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
194268ad4a33SUladzislau Rezki (Sony) 			if (!WARN_ON_ONCE(!free)) {
194368ad4a33SUladzislau Rezki (Sony) 				free->va_start = vmap_start;
194468ad4a33SUladzislau Rezki (Sony) 				free->va_end = busy->va_start;
194568ad4a33SUladzislau Rezki (Sony) 
194668ad4a33SUladzislau Rezki (Sony) 				insert_vmap_area_augment(free, NULL,
194768ad4a33SUladzislau Rezki (Sony) 					&free_vmap_area_root,
194868ad4a33SUladzislau Rezki (Sony) 						&free_vmap_area_list);
194968ad4a33SUladzislau Rezki (Sony) 			}
195068ad4a33SUladzislau Rezki (Sony) 		}
195168ad4a33SUladzislau Rezki (Sony) 
195268ad4a33SUladzislau Rezki (Sony) 		vmap_start = busy->va_end;
195368ad4a33SUladzislau Rezki (Sony) 	}
195468ad4a33SUladzislau Rezki (Sony) 
195568ad4a33SUladzislau Rezki (Sony) 	if (vmap_end - vmap_start > 0) {
195668ad4a33SUladzislau Rezki (Sony) 		free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
195768ad4a33SUladzislau Rezki (Sony) 		if (!WARN_ON_ONCE(!free)) {
195868ad4a33SUladzislau Rezki (Sony) 			free->va_start = vmap_start;
195968ad4a33SUladzislau Rezki (Sony) 			free->va_end = vmap_end;
196068ad4a33SUladzislau Rezki (Sony) 
196168ad4a33SUladzislau Rezki (Sony) 			insert_vmap_area_augment(free, NULL,
196268ad4a33SUladzislau Rezki (Sony) 				&free_vmap_area_root,
196368ad4a33SUladzislau Rezki (Sony) 					&free_vmap_area_list);
196468ad4a33SUladzislau Rezki (Sony) 		}
196568ad4a33SUladzislau Rezki (Sony) 	}
196668ad4a33SUladzislau Rezki (Sony) }
196768ad4a33SUladzislau Rezki (Sony) 
1968db64fe02SNick Piggin void __init vmalloc_init(void)
1969db64fe02SNick Piggin {
1970822c18f2SIvan Kokshaysky 	struct vmap_area *va;
1971822c18f2SIvan Kokshaysky 	struct vm_struct *tmp;
1972db64fe02SNick Piggin 	int i;
1973db64fe02SNick Piggin 
197468ad4a33SUladzislau Rezki (Sony) 	/*
197568ad4a33SUladzislau Rezki (Sony) 	 * Create the cache for vmap_area objects.
197668ad4a33SUladzislau Rezki (Sony) 	 */
197768ad4a33SUladzislau Rezki (Sony) 	vmap_area_cachep = KMEM_CACHE(vmap_area, SLAB_PANIC);
197868ad4a33SUladzislau Rezki (Sony) 
1979db64fe02SNick Piggin 	for_each_possible_cpu(i) {
1980db64fe02SNick Piggin 		struct vmap_block_queue *vbq;
198132fcfd40SAl Viro 		struct vfree_deferred *p;
1982db64fe02SNick Piggin 
1983db64fe02SNick Piggin 		vbq = &per_cpu(vmap_block_queue, i);
1984db64fe02SNick Piggin 		spin_lock_init(&vbq->lock);
1985db64fe02SNick Piggin 		INIT_LIST_HEAD(&vbq->free);
198632fcfd40SAl Viro 		p = &per_cpu(vfree_deferred, i);
198732fcfd40SAl Viro 		init_llist_head(&p->list);
198832fcfd40SAl Viro 		INIT_WORK(&p->wq, free_work);
1989db64fe02SNick Piggin 	}
19909b463334SJeremy Fitzhardinge 
1991822c18f2SIvan Kokshaysky 	/* Import existing vmlist entries. */
1992822c18f2SIvan Kokshaysky 	for (tmp = vmlist; tmp; tmp = tmp->next) {
199368ad4a33SUladzislau Rezki (Sony) 		va = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
199468ad4a33SUladzislau Rezki (Sony) 		if (WARN_ON_ONCE(!va))
199568ad4a33SUladzislau Rezki (Sony) 			continue;
199668ad4a33SUladzislau Rezki (Sony) 
1997822c18f2SIvan Kokshaysky 		va->va_start = (unsigned long)tmp->addr;
1998822c18f2SIvan Kokshaysky 		va->va_end = va->va_start + tmp->size;
1999dbda591dSKyongHo 		va->vm = tmp;
200068ad4a33SUladzislau Rezki (Sony) 		insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
2001822c18f2SIvan Kokshaysky 	}
2002ca23e405STejun Heo 
200368ad4a33SUladzislau Rezki (Sony) 	/*
200468ad4a33SUladzislau Rezki (Sony) 	 * Now we can initialize a free vmap space.
200568ad4a33SUladzislau Rezki (Sony) 	 */
200668ad4a33SUladzislau Rezki (Sony) 	vmap_init_free_space();
20079b463334SJeremy Fitzhardinge 	vmap_initialized = true;
2008db64fe02SNick Piggin }
2009db64fe02SNick Piggin 
20108fc48985STejun Heo /**
20118fc48985STejun Heo  * unmap_kernel_range - unmap kernel VM area and flush cache and TLB
20128fc48985STejun Heo  * @addr: start of the VM area to unmap
20138fc48985STejun Heo  * @size: size of the VM area to unmap
20148fc48985STejun Heo  *
20158fc48985STejun Heo  * Similar to unmap_kernel_range_noflush() but flushes vcache before
20168fc48985STejun Heo  * the unmapping and tlb after.
20178fc48985STejun Heo  */
2018db64fe02SNick Piggin void unmap_kernel_range(unsigned long addr, unsigned long size)
2019db64fe02SNick Piggin {
2020db64fe02SNick Piggin 	unsigned long end = addr + size;
2021f6fcba70STejun Heo 
2022f6fcba70STejun Heo 	flush_cache_vunmap(addr, end);
2023b521c43fSChristoph Hellwig 	unmap_kernel_range_noflush(addr, size);
2024db64fe02SNick Piggin 	flush_tlb_kernel_range(addr, end);
2025db64fe02SNick Piggin }
2026db64fe02SNick Piggin 
2027e36176beSUladzislau Rezki (Sony) static inline void setup_vmalloc_vm_locked(struct vm_struct *vm,
2028e36176beSUladzislau Rezki (Sony) 	struct vmap_area *va, unsigned long flags, const void *caller)
2029cf88c790STejun Heo {
2030cf88c790STejun Heo 	vm->flags = flags;
2031cf88c790STejun Heo 	vm->addr = (void *)va->va_start;
2032cf88c790STejun Heo 	vm->size = va->va_end - va->va_start;
2033cf88c790STejun Heo 	vm->caller = caller;
2034db1aecafSMinchan Kim 	va->vm = vm;
2035e36176beSUladzislau Rezki (Sony) }
2036e36176beSUladzislau Rezki (Sony) 
2037e36176beSUladzislau Rezki (Sony) static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
2038e36176beSUladzislau Rezki (Sony) 			      unsigned long flags, const void *caller)
2039e36176beSUladzislau Rezki (Sony) {
2040e36176beSUladzislau Rezki (Sony) 	spin_lock(&vmap_area_lock);
2041e36176beSUladzislau Rezki (Sony) 	setup_vmalloc_vm_locked(vm, va, flags, caller);
2042c69480adSJoonsoo Kim 	spin_unlock(&vmap_area_lock);
2043f5252e00SMitsuo Hayasaka }
2044cf88c790STejun Heo 
204520fc02b4SZhang Yanfei static void clear_vm_uninitialized_flag(struct vm_struct *vm)
2046f5252e00SMitsuo Hayasaka {
2047d4033afdSJoonsoo Kim 	/*
204820fc02b4SZhang Yanfei 	 * Before removing VM_UNINITIALIZED,
2049d4033afdSJoonsoo Kim 	 * we should make sure that vm has proper values.
2050d4033afdSJoonsoo Kim 	 * Pair with smp_rmb() in show_numa_info().
2051d4033afdSJoonsoo Kim 	 */
2052d4033afdSJoonsoo Kim 	smp_wmb();
205320fc02b4SZhang Yanfei 	vm->flags &= ~VM_UNINITIALIZED;
2054cf88c790STejun Heo }
2055cf88c790STejun Heo 
2056db64fe02SNick Piggin static struct vm_struct *__get_vm_area_node(unsigned long size,
20572dca6999SDavid Miller 		unsigned long align, unsigned long flags, unsigned long start,
20585e6cafc8SMarek Szyprowski 		unsigned long end, int node, gfp_t gfp_mask, const void *caller)
2059db64fe02SNick Piggin {
20600006526dSKautuk Consul 	struct vmap_area *va;
2061db64fe02SNick Piggin 	struct vm_struct *area;
2062d98c9e83SAndrey Ryabinin 	unsigned long requested_size = size;
20631da177e4SLinus Torvalds 
206452fd24caSGiridhar Pemmasani 	BUG_ON(in_interrupt());
20651da177e4SLinus Torvalds 	size = PAGE_ALIGN(size);
206631be8309SOGAWA Hirofumi 	if (unlikely(!size))
206731be8309SOGAWA Hirofumi 		return NULL;
20681da177e4SLinus Torvalds 
2069252e5c6eSzijun_hu 	if (flags & VM_IOREMAP)
2070252e5c6eSzijun_hu 		align = 1ul << clamp_t(int, get_count_order_long(size),
2071252e5c6eSzijun_hu 				       PAGE_SHIFT, IOREMAP_MAX_ORDER);
2072252e5c6eSzijun_hu 
2073cf88c790STejun Heo 	area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
20741da177e4SLinus Torvalds 	if (unlikely(!area))
20751da177e4SLinus Torvalds 		return NULL;
20761da177e4SLinus Torvalds 
207771394fe5SAndrey Ryabinin 	if (!(flags & VM_NO_GUARD))
20781da177e4SLinus Torvalds 		size += PAGE_SIZE;
20791da177e4SLinus Torvalds 
2080db64fe02SNick Piggin 	va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
2081db64fe02SNick Piggin 	if (IS_ERR(va)) {
2082db64fe02SNick Piggin 		kfree(area);
2083db64fe02SNick Piggin 		return NULL;
20841da177e4SLinus Torvalds 	}
20851da177e4SLinus Torvalds 
2086d98c9e83SAndrey Ryabinin 	kasan_unpoison_vmalloc((void *)va->va_start, requested_size);
2087f5252e00SMitsuo Hayasaka 
2088d98c9e83SAndrey Ryabinin 	setup_vmalloc_vm(area, va, flags, caller);
20893c5c3cfbSDaniel Axtens 
20901da177e4SLinus Torvalds 	return area;
20911da177e4SLinus Torvalds }
20921da177e4SLinus Torvalds 
2093c2968612SBenjamin Herrenschmidt struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
2094c2968612SBenjamin Herrenschmidt 				       unsigned long start, unsigned long end,
20955e6cafc8SMarek Szyprowski 				       const void *caller)
2096c2968612SBenjamin Herrenschmidt {
209700ef2d2fSDavid Rientjes 	return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE,
209800ef2d2fSDavid Rientjes 				  GFP_KERNEL, caller);
2099c2968612SBenjamin Herrenschmidt }
2100c2968612SBenjamin Herrenschmidt 
21011da177e4SLinus Torvalds /**
2102183ff22bSSimon Arlott  * get_vm_area - reserve a contiguous kernel virtual area
21031da177e4SLinus Torvalds  * @size:	 size of the area
21041da177e4SLinus Torvalds  * @flags:	 %VM_IOREMAP for I/O mappings or VM_ALLOC
21051da177e4SLinus Torvalds  *
21061da177e4SLinus Torvalds  * Search an area of @size in the kernel virtual mapping area,
21071da177e4SLinus Torvalds  * and reserved it for out purposes.  Returns the area descriptor
21081da177e4SLinus Torvalds  * on success or %NULL on failure.
2109a862f68aSMike Rapoport  *
2110a862f68aSMike Rapoport  * Return: the area descriptor on success or %NULL on failure.
21111da177e4SLinus Torvalds  */
21121da177e4SLinus Torvalds struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
21131da177e4SLinus Torvalds {
21142dca6999SDavid Miller 	return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
211500ef2d2fSDavid Rientjes 				  NUMA_NO_NODE, GFP_KERNEL,
211600ef2d2fSDavid Rientjes 				  __builtin_return_address(0));
211723016969SChristoph Lameter }
211823016969SChristoph Lameter 
211923016969SChristoph Lameter struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
21205e6cafc8SMarek Szyprowski 				const void *caller)
212123016969SChristoph Lameter {
21222dca6999SDavid Miller 	return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
212300ef2d2fSDavid Rientjes 				  NUMA_NO_NODE, GFP_KERNEL, caller);
21241da177e4SLinus Torvalds }
21251da177e4SLinus Torvalds 
2126e9da6e99SMarek Szyprowski /**
2127e9da6e99SMarek Szyprowski  * find_vm_area - find a continuous kernel virtual area
2128e9da6e99SMarek Szyprowski  * @addr:	  base address
2129e9da6e99SMarek Szyprowski  *
2130e9da6e99SMarek Szyprowski  * Search for the kernel VM area starting at @addr, and return it.
2131e9da6e99SMarek Szyprowski  * It is up to the caller to do all required locking to keep the returned
2132e9da6e99SMarek Szyprowski  * pointer valid.
2133a862f68aSMike Rapoport  *
213474640617SHui Su  * Return: the area descriptor on success or %NULL on failure.
2135e9da6e99SMarek Szyprowski  */
2136e9da6e99SMarek Szyprowski struct vm_struct *find_vm_area(const void *addr)
213783342314SNick Piggin {
2138db64fe02SNick Piggin 	struct vmap_area *va;
213983342314SNick Piggin 
2140db64fe02SNick Piggin 	va = find_vmap_area((unsigned long)addr);
2141688fcbfcSPengfei Li 	if (!va)
21427856dfebSAndi Kleen 		return NULL;
2143688fcbfcSPengfei Li 
2144688fcbfcSPengfei Li 	return va->vm;
21457856dfebSAndi Kleen }
21467856dfebSAndi Kleen 
21471da177e4SLinus Torvalds /**
2148183ff22bSSimon Arlott  * remove_vm_area - find and remove a continuous kernel virtual area
21491da177e4SLinus Torvalds  * @addr:	    base address
21501da177e4SLinus Torvalds  *
21511da177e4SLinus Torvalds  * Search for the kernel VM area starting at @addr, and remove it.
21521da177e4SLinus Torvalds  * This function returns the found VM area, but using it is NOT safe
21537856dfebSAndi Kleen  * on SMP machines, except for its size or flags.
2154a862f68aSMike Rapoport  *
215574640617SHui Su  * Return: the area descriptor on success or %NULL on failure.
21561da177e4SLinus Torvalds  */
2157b3bdda02SChristoph Lameter struct vm_struct *remove_vm_area(const void *addr)
21581da177e4SLinus Torvalds {
2159db64fe02SNick Piggin 	struct vmap_area *va;
2160db64fe02SNick Piggin 
21615803ed29SChristoph Hellwig 	might_sleep();
21625803ed29SChristoph Hellwig 
2163dd3b8353SUladzislau Rezki (Sony) 	spin_lock(&vmap_area_lock);
2164dd3b8353SUladzislau Rezki (Sony) 	va = __find_vmap_area((unsigned long)addr);
2165688fcbfcSPengfei Li 	if (va && va->vm) {
2166db1aecafSMinchan Kim 		struct vm_struct *vm = va->vm;
2167f5252e00SMitsuo Hayasaka 
2168c69480adSJoonsoo Kim 		va->vm = NULL;
2169c69480adSJoonsoo Kim 		spin_unlock(&vmap_area_lock);
2170c69480adSJoonsoo Kim 
2171a5af5aa8SAndrey Ryabinin 		kasan_free_shadow(vm);
2172dd32c279SKAMEZAWA Hiroyuki 		free_unmap_vmap_area(va);
2173dd32c279SKAMEZAWA Hiroyuki 
2174db64fe02SNick Piggin 		return vm;
2175db64fe02SNick Piggin 	}
2176dd3b8353SUladzislau Rezki (Sony) 
2177dd3b8353SUladzislau Rezki (Sony) 	spin_unlock(&vmap_area_lock);
2178db64fe02SNick Piggin 	return NULL;
21791da177e4SLinus Torvalds }
21801da177e4SLinus Torvalds 
2181868b104dSRick Edgecombe static inline void set_area_direct_map(const struct vm_struct *area,
2182868b104dSRick Edgecombe 				       int (*set_direct_map)(struct page *page))
2183868b104dSRick Edgecombe {
2184868b104dSRick Edgecombe 	int i;
2185868b104dSRick Edgecombe 
2186868b104dSRick Edgecombe 	for (i = 0; i < area->nr_pages; i++)
2187868b104dSRick Edgecombe 		if (page_address(area->pages[i]))
2188868b104dSRick Edgecombe 			set_direct_map(area->pages[i]);
2189868b104dSRick Edgecombe }
2190868b104dSRick Edgecombe 
2191868b104dSRick Edgecombe /* Handle removing and resetting vm mappings related to the vm_struct. */
2192868b104dSRick Edgecombe static void vm_remove_mappings(struct vm_struct *area, int deallocate_pages)
2193868b104dSRick Edgecombe {
2194868b104dSRick Edgecombe 	unsigned long start = ULONG_MAX, end = 0;
2195868b104dSRick Edgecombe 	int flush_reset = area->flags & VM_FLUSH_RESET_PERMS;
219631e67340SRick Edgecombe 	int flush_dmap = 0;
2197868b104dSRick Edgecombe 	int i;
2198868b104dSRick Edgecombe 
2199868b104dSRick Edgecombe 	remove_vm_area(area->addr);
2200868b104dSRick Edgecombe 
2201868b104dSRick Edgecombe 	/* If this is not VM_FLUSH_RESET_PERMS memory, no need for the below. */
2202868b104dSRick Edgecombe 	if (!flush_reset)
2203868b104dSRick Edgecombe 		return;
2204868b104dSRick Edgecombe 
2205868b104dSRick Edgecombe 	/*
2206868b104dSRick Edgecombe 	 * If not deallocating pages, just do the flush of the VM area and
2207868b104dSRick Edgecombe 	 * return.
2208868b104dSRick Edgecombe 	 */
2209868b104dSRick Edgecombe 	if (!deallocate_pages) {
2210868b104dSRick Edgecombe 		vm_unmap_aliases();
2211868b104dSRick Edgecombe 		return;
2212868b104dSRick Edgecombe 	}
2213868b104dSRick Edgecombe 
2214868b104dSRick Edgecombe 	/*
2215868b104dSRick Edgecombe 	 * If execution gets here, flush the vm mapping and reset the direct
2216868b104dSRick Edgecombe 	 * map. Find the start and end range of the direct mappings to make sure
2217868b104dSRick Edgecombe 	 * the vm_unmap_aliases() flush includes the direct map.
2218868b104dSRick Edgecombe 	 */
2219868b104dSRick Edgecombe 	for (i = 0; i < area->nr_pages; i++) {
22208e41f872SRick Edgecombe 		unsigned long addr = (unsigned long)page_address(area->pages[i]);
22218e41f872SRick Edgecombe 		if (addr) {
2222868b104dSRick Edgecombe 			start = min(addr, start);
22238e41f872SRick Edgecombe 			end = max(addr + PAGE_SIZE, end);
222431e67340SRick Edgecombe 			flush_dmap = 1;
2225868b104dSRick Edgecombe 		}
2226868b104dSRick Edgecombe 	}
2227868b104dSRick Edgecombe 
2228868b104dSRick Edgecombe 	/*
2229868b104dSRick Edgecombe 	 * Set direct map to something invalid so that it won't be cached if
2230868b104dSRick Edgecombe 	 * there are any accesses after the TLB flush, then flush the TLB and
2231868b104dSRick Edgecombe 	 * reset the direct map permissions to the default.
2232868b104dSRick Edgecombe 	 */
2233868b104dSRick Edgecombe 	set_area_direct_map(area, set_direct_map_invalid_noflush);
223431e67340SRick Edgecombe 	_vm_unmap_aliases(start, end, flush_dmap);
2235868b104dSRick Edgecombe 	set_area_direct_map(area, set_direct_map_default_noflush);
2236868b104dSRick Edgecombe }
2237868b104dSRick Edgecombe 
2238b3bdda02SChristoph Lameter static void __vunmap(const void *addr, int deallocate_pages)
22391da177e4SLinus Torvalds {
22401da177e4SLinus Torvalds 	struct vm_struct *area;
22411da177e4SLinus Torvalds 
22421da177e4SLinus Torvalds 	if (!addr)
22431da177e4SLinus Torvalds 		return;
22441da177e4SLinus Torvalds 
2245e69e9d4aSHATAYAMA Daisuke 	if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n",
2246ab15d9b4SDan Carpenter 			addr))
22471da177e4SLinus Torvalds 		return;
22481da177e4SLinus Torvalds 
22496ade2032SLiviu Dudau 	area = find_vm_area(addr);
22501da177e4SLinus Torvalds 	if (unlikely(!area)) {
22514c8573e2SArjan van de Ven 		WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
22521da177e4SLinus Torvalds 				addr);
22531da177e4SLinus Torvalds 		return;
22541da177e4SLinus Torvalds 	}
22551da177e4SLinus Torvalds 
225605e3ff95SChintan Pandya 	debug_check_no_locks_freed(area->addr, get_vm_area_size(area));
225705e3ff95SChintan Pandya 	debug_check_no_obj_freed(area->addr, get_vm_area_size(area));
22589a11b49aSIngo Molnar 
22593c5c3cfbSDaniel Axtens 	kasan_poison_vmalloc(area->addr, area->size);
22603c5c3cfbSDaniel Axtens 
2261868b104dSRick Edgecombe 	vm_remove_mappings(area, deallocate_pages);
2262868b104dSRick Edgecombe 
22631da177e4SLinus Torvalds 	if (deallocate_pages) {
22641da177e4SLinus Torvalds 		int i;
22651da177e4SLinus Torvalds 
22661da177e4SLinus Torvalds 		for (i = 0; i < area->nr_pages; i++) {
2267bf53d6f8SChristoph Lameter 			struct page *page = area->pages[i];
2268bf53d6f8SChristoph Lameter 
2269bf53d6f8SChristoph Lameter 			BUG_ON(!page);
22704949148aSVladimir Davydov 			__free_pages(page, 0);
22711da177e4SLinus Torvalds 		}
227297105f0aSRoman Gushchin 		atomic_long_sub(area->nr_pages, &nr_vmalloc_pages);
22731da177e4SLinus Torvalds 
2274244d63eeSDavid Rientjes 		kvfree(area->pages);
22751da177e4SLinus Torvalds 	}
22761da177e4SLinus Torvalds 
22771da177e4SLinus Torvalds 	kfree(area);
22781da177e4SLinus Torvalds 	return;
22791da177e4SLinus Torvalds }
22801da177e4SLinus Torvalds 
2281bf22e37aSAndrey Ryabinin static inline void __vfree_deferred(const void *addr)
2282bf22e37aSAndrey Ryabinin {
2283bf22e37aSAndrey Ryabinin 	/*
2284bf22e37aSAndrey Ryabinin 	 * Use raw_cpu_ptr() because this can be called from preemptible
2285bf22e37aSAndrey Ryabinin 	 * context. Preemption is absolutely fine here, because the llist_add()
2286bf22e37aSAndrey Ryabinin 	 * implementation is lockless, so it works even if we are adding to
228773221d88SJeongtae Park 	 * another cpu's list. schedule_work() should be fine with this too.
2288bf22e37aSAndrey Ryabinin 	 */
2289bf22e37aSAndrey Ryabinin 	struct vfree_deferred *p = raw_cpu_ptr(&vfree_deferred);
2290bf22e37aSAndrey Ryabinin 
2291bf22e37aSAndrey Ryabinin 	if (llist_add((struct llist_node *)addr, &p->list))
2292bf22e37aSAndrey Ryabinin 		schedule_work(&p->wq);
2293bf22e37aSAndrey Ryabinin }
2294bf22e37aSAndrey Ryabinin 
2295bf22e37aSAndrey Ryabinin /**
2296bf22e37aSAndrey Ryabinin  * vfree_atomic - release memory allocated by vmalloc()
2297bf22e37aSAndrey Ryabinin  * @addr:	  memory base address
2298bf22e37aSAndrey Ryabinin  *
2299bf22e37aSAndrey Ryabinin  * This one is just like vfree() but can be called in any atomic context
2300bf22e37aSAndrey Ryabinin  * except NMIs.
2301bf22e37aSAndrey Ryabinin  */
2302bf22e37aSAndrey Ryabinin void vfree_atomic(const void *addr)
2303bf22e37aSAndrey Ryabinin {
2304bf22e37aSAndrey Ryabinin 	BUG_ON(in_nmi());
2305bf22e37aSAndrey Ryabinin 
2306bf22e37aSAndrey Ryabinin 	kmemleak_free(addr);
2307bf22e37aSAndrey Ryabinin 
2308bf22e37aSAndrey Ryabinin 	if (!addr)
2309bf22e37aSAndrey Ryabinin 		return;
2310bf22e37aSAndrey Ryabinin 	__vfree_deferred(addr);
2311bf22e37aSAndrey Ryabinin }
2312bf22e37aSAndrey Ryabinin 
2313c67dc624SRoman Penyaev static void __vfree(const void *addr)
2314c67dc624SRoman Penyaev {
2315c67dc624SRoman Penyaev 	if (unlikely(in_interrupt()))
2316c67dc624SRoman Penyaev 		__vfree_deferred(addr);
2317c67dc624SRoman Penyaev 	else
2318c67dc624SRoman Penyaev 		__vunmap(addr, 1);
2319c67dc624SRoman Penyaev }
2320c67dc624SRoman Penyaev 
23211da177e4SLinus Torvalds /**
2322fa307474SMatthew Wilcox (Oracle)  * vfree - Release memory allocated by vmalloc()
2323fa307474SMatthew Wilcox (Oracle)  * @addr:  Memory base address
23241da177e4SLinus Torvalds  *
2325fa307474SMatthew Wilcox (Oracle)  * Free the virtually continuous memory area starting at @addr, as obtained
2326fa307474SMatthew Wilcox (Oracle)  * from one of the vmalloc() family of APIs.  This will usually also free the
2327fa307474SMatthew Wilcox (Oracle)  * physical memory underlying the virtual allocation, but that memory is
2328fa307474SMatthew Wilcox (Oracle)  * reference counted, so it will not be freed until the last user goes away.
23291da177e4SLinus Torvalds  *
2330fa307474SMatthew Wilcox (Oracle)  * If @addr is NULL, no operation is performed.
233132fcfd40SAl Viro  *
2332fa307474SMatthew Wilcox (Oracle)  * Context:
23333ca4ea3aSAndrey Ryabinin  * May sleep if called *not* from interrupt context.
2334fa307474SMatthew Wilcox (Oracle)  * Must not be called in NMI context (strictly speaking, it could be
2335fa307474SMatthew Wilcox (Oracle)  * if we have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling
2336fa307474SMatthew Wilcox (Oracle)  * conventions for vfree() arch-depenedent would be a really bad idea).
23371da177e4SLinus Torvalds  */
2338b3bdda02SChristoph Lameter void vfree(const void *addr)
23391da177e4SLinus Torvalds {
234032fcfd40SAl Viro 	BUG_ON(in_nmi());
234189219d37SCatalin Marinas 
234289219d37SCatalin Marinas 	kmemleak_free(addr);
234389219d37SCatalin Marinas 
2344a8dda165SAndrey Ryabinin 	might_sleep_if(!in_interrupt());
2345a8dda165SAndrey Ryabinin 
234632fcfd40SAl Viro 	if (!addr)
234732fcfd40SAl Viro 		return;
2348c67dc624SRoman Penyaev 
2349c67dc624SRoman Penyaev 	__vfree(addr);
23501da177e4SLinus Torvalds }
23511da177e4SLinus Torvalds EXPORT_SYMBOL(vfree);
23521da177e4SLinus Torvalds 
23531da177e4SLinus Torvalds /**
23541da177e4SLinus Torvalds  * vunmap - release virtual mapping obtained by vmap()
23551da177e4SLinus Torvalds  * @addr:   memory base address
23561da177e4SLinus Torvalds  *
23571da177e4SLinus Torvalds  * Free the virtually contiguous memory area starting at @addr,
23581da177e4SLinus Torvalds  * which was created from the page array passed to vmap().
23591da177e4SLinus Torvalds  *
236080e93effSPekka Enberg  * Must not be called in interrupt context.
23611da177e4SLinus Torvalds  */
2362b3bdda02SChristoph Lameter void vunmap(const void *addr)
23631da177e4SLinus Torvalds {
23641da177e4SLinus Torvalds 	BUG_ON(in_interrupt());
236534754b69SPeter Zijlstra 	might_sleep();
236632fcfd40SAl Viro 	if (addr)
23671da177e4SLinus Torvalds 		__vunmap(addr, 0);
23681da177e4SLinus Torvalds }
23691da177e4SLinus Torvalds EXPORT_SYMBOL(vunmap);
23701da177e4SLinus Torvalds 
23711da177e4SLinus Torvalds /**
23721da177e4SLinus Torvalds  * vmap - map an array of pages into virtually contiguous space
23731da177e4SLinus Torvalds  * @pages: array of page pointers
23741da177e4SLinus Torvalds  * @count: number of pages to map
23751da177e4SLinus Torvalds  * @flags: vm_area->flags
23761da177e4SLinus Torvalds  * @prot: page protection for the mapping
23771da177e4SLinus Torvalds  *
2378b944afc9SChristoph Hellwig  * Maps @count pages from @pages into contiguous kernel virtual space.
2379b944afc9SChristoph Hellwig  * If @flags contains %VM_MAP_PUT_PAGES the ownership of the pages array itself
2380b944afc9SChristoph Hellwig  * (which must be kmalloc or vmalloc memory) and one reference per pages in it
2381b944afc9SChristoph Hellwig  * are transferred from the caller to vmap(), and will be freed / dropped when
2382b944afc9SChristoph Hellwig  * vfree() is called on the return value.
2383a862f68aSMike Rapoport  *
2384a862f68aSMike Rapoport  * Return: the address of the area or %NULL on failure
23851da177e4SLinus Torvalds  */
23861da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count,
23871da177e4SLinus Torvalds 	   unsigned long flags, pgprot_t prot)
23881da177e4SLinus Torvalds {
23891da177e4SLinus Torvalds 	struct vm_struct *area;
239065ee03c4SGuillermo Julián Moreno 	unsigned long size;		/* In bytes */
23911da177e4SLinus Torvalds 
239234754b69SPeter Zijlstra 	might_sleep();
239334754b69SPeter Zijlstra 
2394ca79b0c2SArun KS 	if (count > totalram_pages())
23951da177e4SLinus Torvalds 		return NULL;
23961da177e4SLinus Torvalds 
239765ee03c4SGuillermo Julián Moreno 	size = (unsigned long)count << PAGE_SHIFT;
239865ee03c4SGuillermo Julián Moreno 	area = get_vm_area_caller(size, flags, __builtin_return_address(0));
23991da177e4SLinus Torvalds 	if (!area)
24001da177e4SLinus Torvalds 		return NULL;
240123016969SChristoph Lameter 
2402cca98e9fSChristoph Hellwig 	if (map_kernel_range((unsigned long)area->addr, size, pgprot_nx(prot),
2403ed1f324cSChristoph Hellwig 			pages) < 0) {
24041da177e4SLinus Torvalds 		vunmap(area->addr);
24051da177e4SLinus Torvalds 		return NULL;
24061da177e4SLinus Torvalds 	}
24071da177e4SLinus Torvalds 
2408b944afc9SChristoph Hellwig 	if (flags & VM_MAP_PUT_PAGES)
2409b944afc9SChristoph Hellwig 		area->pages = pages;
24101da177e4SLinus Torvalds 	return area->addr;
24111da177e4SLinus Torvalds }
24121da177e4SLinus Torvalds EXPORT_SYMBOL(vmap);
24131da177e4SLinus Torvalds 
24143e9a9e25SChristoph Hellwig #ifdef CONFIG_VMAP_PFN
24153e9a9e25SChristoph Hellwig struct vmap_pfn_data {
24163e9a9e25SChristoph Hellwig 	unsigned long	*pfns;
24173e9a9e25SChristoph Hellwig 	pgprot_t	prot;
24183e9a9e25SChristoph Hellwig 	unsigned int	idx;
24193e9a9e25SChristoph Hellwig };
24203e9a9e25SChristoph Hellwig 
24213e9a9e25SChristoph Hellwig static int vmap_pfn_apply(pte_t *pte, unsigned long addr, void *private)
24223e9a9e25SChristoph Hellwig {
24233e9a9e25SChristoph Hellwig 	struct vmap_pfn_data *data = private;
24243e9a9e25SChristoph Hellwig 
24253e9a9e25SChristoph Hellwig 	if (WARN_ON_ONCE(pfn_valid(data->pfns[data->idx])))
24263e9a9e25SChristoph Hellwig 		return -EINVAL;
24273e9a9e25SChristoph Hellwig 	*pte = pte_mkspecial(pfn_pte(data->pfns[data->idx++], data->prot));
24283e9a9e25SChristoph Hellwig 	return 0;
24293e9a9e25SChristoph Hellwig }
24303e9a9e25SChristoph Hellwig 
24313e9a9e25SChristoph Hellwig /**
24323e9a9e25SChristoph Hellwig  * vmap_pfn - map an array of PFNs into virtually contiguous space
24333e9a9e25SChristoph Hellwig  * @pfns: array of PFNs
24343e9a9e25SChristoph Hellwig  * @count: number of pages to map
24353e9a9e25SChristoph Hellwig  * @prot: page protection for the mapping
24363e9a9e25SChristoph Hellwig  *
24373e9a9e25SChristoph Hellwig  * Maps @count PFNs from @pfns into contiguous kernel virtual space and returns
24383e9a9e25SChristoph Hellwig  * the start address of the mapping.
24393e9a9e25SChristoph Hellwig  */
24403e9a9e25SChristoph Hellwig void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot)
24413e9a9e25SChristoph Hellwig {
24423e9a9e25SChristoph Hellwig 	struct vmap_pfn_data data = { .pfns = pfns, .prot = pgprot_nx(prot) };
24433e9a9e25SChristoph Hellwig 	struct vm_struct *area;
24443e9a9e25SChristoph Hellwig 
24453e9a9e25SChristoph Hellwig 	area = get_vm_area_caller(count * PAGE_SIZE, VM_IOREMAP,
24463e9a9e25SChristoph Hellwig 			__builtin_return_address(0));
24473e9a9e25SChristoph Hellwig 	if (!area)
24483e9a9e25SChristoph Hellwig 		return NULL;
24493e9a9e25SChristoph Hellwig 	if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
24503e9a9e25SChristoph Hellwig 			count * PAGE_SIZE, vmap_pfn_apply, &data)) {
24513e9a9e25SChristoph Hellwig 		free_vm_area(area);
24523e9a9e25SChristoph Hellwig 		return NULL;
24533e9a9e25SChristoph Hellwig 	}
24543e9a9e25SChristoph Hellwig 	return area->addr;
24553e9a9e25SChristoph Hellwig }
24563e9a9e25SChristoph Hellwig EXPORT_SYMBOL_GPL(vmap_pfn);
24573e9a9e25SChristoph Hellwig #endif /* CONFIG_VMAP_PFN */
24583e9a9e25SChristoph Hellwig 
2459e31d9eb5SAdrian Bunk static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
24603722e13cSWanpeng Li 				 pgprot_t prot, int node)
24611da177e4SLinus Torvalds {
2462930f036bSDavid Rientjes 	const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
2463f255935bSChristoph Hellwig 	unsigned int nr_pages = get_vm_area_size(area) >> PAGE_SHIFT;
2464*34fe6537SAndrew Morton 	unsigned long array_size;
2465*34fe6537SAndrew Morton 	unsigned int i;
2466f255935bSChristoph Hellwig 	struct page **pages;
24671da177e4SLinus Torvalds 
2468*34fe6537SAndrew Morton 	array_size = (unsigned long)nr_pages * sizeof(struct page *);
2469f255935bSChristoph Hellwig 	gfp_mask |= __GFP_NOWARN;
2470f255935bSChristoph Hellwig 	if (!(gfp_mask & (GFP_DMA | GFP_DMA32)))
2471f255935bSChristoph Hellwig 		gfp_mask |= __GFP_HIGHMEM;
24721da177e4SLinus Torvalds 
24731da177e4SLinus Torvalds 	/* Please note that the recursion is strictly bounded. */
24748757d5faSJan Kiszka 	if (array_size > PAGE_SIZE) {
2475f255935bSChristoph Hellwig 		pages = __vmalloc_node(array_size, 1, nested_gfp, node,
2476f255935bSChristoph Hellwig 					area->caller);
2477286e1ea3SAndrew Morton 	} else {
2478976d6dfbSJan Beulich 		pages = kmalloc_node(array_size, nested_gfp, node);
2479286e1ea3SAndrew Morton 	}
24807ea36242SAustin Kim 
24817ea36242SAustin Kim 	if (!pages) {
24821da177e4SLinus Torvalds 		remove_vm_area(area->addr);
24831da177e4SLinus Torvalds 		kfree(area);
24841da177e4SLinus Torvalds 		return NULL;
24851da177e4SLinus Torvalds 	}
24861da177e4SLinus Torvalds 
24877ea36242SAustin Kim 	area->pages = pages;
24887ea36242SAustin Kim 	area->nr_pages = nr_pages;
24897ea36242SAustin Kim 
24901da177e4SLinus Torvalds 	for (i = 0; i < area->nr_pages; i++) {
2491bf53d6f8SChristoph Lameter 		struct page *page;
2492bf53d6f8SChristoph Lameter 
24934b90951cSJianguo Wu 		if (node == NUMA_NO_NODE)
2494f255935bSChristoph Hellwig 			page = alloc_page(gfp_mask);
2495930fc45aSChristoph Lameter 		else
2496f255935bSChristoph Hellwig 			page = alloc_pages_node(node, gfp_mask, 0);
2497bf53d6f8SChristoph Lameter 
2498bf53d6f8SChristoph Lameter 		if (unlikely(!page)) {
249982afbc32SHui Su 			/* Successfully allocated i pages, free them in __vfree() */
25001da177e4SLinus Torvalds 			area->nr_pages = i;
250197105f0aSRoman Gushchin 			atomic_long_add(area->nr_pages, &nr_vmalloc_pages);
25021da177e4SLinus Torvalds 			goto fail;
25031da177e4SLinus Torvalds 		}
2504bf53d6f8SChristoph Lameter 		area->pages[i] = page;
2505dcf61ff0SLiu Xiang 		if (gfpflags_allow_blocking(gfp_mask))
2506660654f9SEric Dumazet 			cond_resched();
25071da177e4SLinus Torvalds 	}
250897105f0aSRoman Gushchin 	atomic_long_add(area->nr_pages, &nr_vmalloc_pages);
25091da177e4SLinus Torvalds 
2510ed1f324cSChristoph Hellwig 	if (map_kernel_range((unsigned long)area->addr, get_vm_area_size(area),
2511ed1f324cSChristoph Hellwig 			prot, pages) < 0)
25121da177e4SLinus Torvalds 		goto fail;
2513ed1f324cSChristoph Hellwig 
25141da177e4SLinus Torvalds 	return area->addr;
25151da177e4SLinus Torvalds 
25161da177e4SLinus Torvalds fail:
2517a8e99259SMichal Hocko 	warn_alloc(gfp_mask, NULL,
25187877cdccSMichal Hocko 			  "vmalloc: allocation failure, allocated %ld of %ld bytes",
251922943ab1SDave Hansen 			  (area->nr_pages*PAGE_SIZE), area->size);
2520c67dc624SRoman Penyaev 	__vfree(area->addr);
25211da177e4SLinus Torvalds 	return NULL;
25221da177e4SLinus Torvalds }
25231da177e4SLinus Torvalds 
2524d0a21265SDavid Rientjes /**
2525d0a21265SDavid Rientjes  * __vmalloc_node_range - allocate virtually contiguous memory
2526d0a21265SDavid Rientjes  * @size:		  allocation size
2527d0a21265SDavid Rientjes  * @align:		  desired alignment
2528d0a21265SDavid Rientjes  * @start:		  vm area range start
2529d0a21265SDavid Rientjes  * @end:		  vm area range end
2530d0a21265SDavid Rientjes  * @gfp_mask:		  flags for the page level allocator
2531d0a21265SDavid Rientjes  * @prot:		  protection mask for the allocated pages
2532cb9e3c29SAndrey Ryabinin  * @vm_flags:		  additional vm area flags (e.g. %VM_NO_GUARD)
253300ef2d2fSDavid Rientjes  * @node:		  node to use for allocation or NUMA_NO_NODE
2534d0a21265SDavid Rientjes  * @caller:		  caller's return address
2535d0a21265SDavid Rientjes  *
2536d0a21265SDavid Rientjes  * Allocate enough pages to cover @size from the page level
2537d0a21265SDavid Rientjes  * allocator with @gfp_mask flags.  Map them into contiguous
2538d0a21265SDavid Rientjes  * kernel virtual space, using a pagetable protection of @prot.
2539a862f68aSMike Rapoport  *
2540a862f68aSMike Rapoport  * Return: the address of the area or %NULL on failure
2541d0a21265SDavid Rientjes  */
2542d0a21265SDavid Rientjes void *__vmalloc_node_range(unsigned long size, unsigned long align,
2543d0a21265SDavid Rientjes 			unsigned long start, unsigned long end, gfp_t gfp_mask,
2544cb9e3c29SAndrey Ryabinin 			pgprot_t prot, unsigned long vm_flags, int node,
2545cb9e3c29SAndrey Ryabinin 			const void *caller)
2546930fc45aSChristoph Lameter {
2547d0a21265SDavid Rientjes 	struct vm_struct *area;
2548d0a21265SDavid Rientjes 	void *addr;
2549d0a21265SDavid Rientjes 	unsigned long real_size = size;
2550d0a21265SDavid Rientjes 
2551d0a21265SDavid Rientjes 	size = PAGE_ALIGN(size);
2552ca79b0c2SArun KS 	if (!size || (size >> PAGE_SHIFT) > totalram_pages())
2553de7d2b56SJoe Perches 		goto fail;
2554d0a21265SDavid Rientjes 
2555d98c9e83SAndrey Ryabinin 	area = __get_vm_area_node(real_size, align, VM_ALLOC | VM_UNINITIALIZED |
2556cb9e3c29SAndrey Ryabinin 				vm_flags, start, end, node, gfp_mask, caller);
2557d0a21265SDavid Rientjes 	if (!area)
2558de7d2b56SJoe Perches 		goto fail;
2559d0a21265SDavid Rientjes 
25603722e13cSWanpeng Li 	addr = __vmalloc_area_node(area, gfp_mask, prot, node);
25611368edf0SMel Gorman 	if (!addr)
2562b82225f3SWanpeng Li 		return NULL;
256389219d37SCatalin Marinas 
256489219d37SCatalin Marinas 	/*
256520fc02b4SZhang Yanfei 	 * In this function, newly allocated vm_struct has VM_UNINITIALIZED
256620fc02b4SZhang Yanfei 	 * flag. It means that vm_struct is not fully initialized.
25674341fa45SJoonsoo Kim 	 * Now, it is fully initialized, so remove this flag here.
2568f5252e00SMitsuo Hayasaka 	 */
256920fc02b4SZhang Yanfei 	clear_vm_uninitialized_flag(area);
2570f5252e00SMitsuo Hayasaka 
257194f4a161SCatalin Marinas 	kmemleak_vmalloc(area, size, gfp_mask);
257289219d37SCatalin Marinas 
257389219d37SCatalin Marinas 	return addr;
2574de7d2b56SJoe Perches 
2575de7d2b56SJoe Perches fail:
2576a8e99259SMichal Hocko 	warn_alloc(gfp_mask, NULL,
25777877cdccSMichal Hocko 			  "vmalloc: allocation failure: %lu bytes", real_size);
2578de7d2b56SJoe Perches 	return NULL;
2579930fc45aSChristoph Lameter }
2580930fc45aSChristoph Lameter 
25811da177e4SLinus Torvalds /**
2582930fc45aSChristoph Lameter  * __vmalloc_node - allocate virtually contiguous memory
25831da177e4SLinus Torvalds  * @size:	    allocation size
25842dca6999SDavid Miller  * @align:	    desired alignment
25851da177e4SLinus Torvalds  * @gfp_mask:	    flags for the page level allocator
258600ef2d2fSDavid Rientjes  * @node:	    node to use for allocation or NUMA_NO_NODE
2587c85d194bSRandy Dunlap  * @caller:	    caller's return address
25881da177e4SLinus Torvalds  *
2589f38fcb9cSChristoph Hellwig  * Allocate enough pages to cover @size from the page level allocator with
2590f38fcb9cSChristoph Hellwig  * @gfp_mask flags.  Map them into contiguous kernel virtual space.
2591a7c3e901SMichal Hocko  *
2592dcda9b04SMichal Hocko  * Reclaim modifiers in @gfp_mask - __GFP_NORETRY, __GFP_RETRY_MAYFAIL
2593a7c3e901SMichal Hocko  * and __GFP_NOFAIL are not supported
2594a7c3e901SMichal Hocko  *
2595a7c3e901SMichal Hocko  * Any use of gfp flags outside of GFP_KERNEL should be consulted
2596a7c3e901SMichal Hocko  * with mm people.
2597a862f68aSMike Rapoport  *
2598a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
25991da177e4SLinus Torvalds  */
26002b905948SChristoph Hellwig void *__vmalloc_node(unsigned long size, unsigned long align,
2601f38fcb9cSChristoph Hellwig 			    gfp_t gfp_mask, int node, const void *caller)
26021da177e4SLinus Torvalds {
2603d0a21265SDavid Rientjes 	return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
2604f38fcb9cSChristoph Hellwig 				gfp_mask, PAGE_KERNEL, 0, node, caller);
26051da177e4SLinus Torvalds }
2606c3f896dcSChristoph Hellwig /*
2607c3f896dcSChristoph Hellwig  * This is only for performance analysis of vmalloc and stress purpose.
2608c3f896dcSChristoph Hellwig  * It is required by vmalloc test module, therefore do not use it other
2609c3f896dcSChristoph Hellwig  * than that.
2610c3f896dcSChristoph Hellwig  */
2611c3f896dcSChristoph Hellwig #ifdef CONFIG_TEST_VMALLOC_MODULE
2612c3f896dcSChristoph Hellwig EXPORT_SYMBOL_GPL(__vmalloc_node);
2613c3f896dcSChristoph Hellwig #endif
26141da177e4SLinus Torvalds 
261588dca4caSChristoph Hellwig void *__vmalloc(unsigned long size, gfp_t gfp_mask)
2616930fc45aSChristoph Lameter {
2617f38fcb9cSChristoph Hellwig 	return __vmalloc_node(size, 1, gfp_mask, NUMA_NO_NODE,
261823016969SChristoph Lameter 				__builtin_return_address(0));
2619930fc45aSChristoph Lameter }
26201da177e4SLinus Torvalds EXPORT_SYMBOL(__vmalloc);
26211da177e4SLinus Torvalds 
26221da177e4SLinus Torvalds /**
26231da177e4SLinus Torvalds  * vmalloc - allocate virtually contiguous memory
26241da177e4SLinus Torvalds  * @size:    allocation size
262592eac168SMike Rapoport  *
26261da177e4SLinus Torvalds  * Allocate enough pages to cover @size from the page level
26271da177e4SLinus Torvalds  * allocator and map them into contiguous kernel virtual space.
26281da177e4SLinus Torvalds  *
2629c1c8897fSMichael Opdenacker  * For tight control over page level allocator and protection flags
26301da177e4SLinus Torvalds  * use __vmalloc() instead.
2631a862f68aSMike Rapoport  *
2632a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
26331da177e4SLinus Torvalds  */
26341da177e4SLinus Torvalds void *vmalloc(unsigned long size)
26351da177e4SLinus Torvalds {
26364d39d728SChristoph Hellwig 	return __vmalloc_node(size, 1, GFP_KERNEL, NUMA_NO_NODE,
26374d39d728SChristoph Hellwig 				__builtin_return_address(0));
26381da177e4SLinus Torvalds }
26391da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc);
26401da177e4SLinus Torvalds 
2641930fc45aSChristoph Lameter /**
2642e1ca7788SDave Young  * vzalloc - allocate virtually contiguous memory with zero fill
2643e1ca7788SDave Young  * @size:    allocation size
264492eac168SMike Rapoport  *
2645e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
2646e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
2647e1ca7788SDave Young  * The memory allocated is set to zero.
2648e1ca7788SDave Young  *
2649e1ca7788SDave Young  * For tight control over page level allocator and protection flags
2650e1ca7788SDave Young  * use __vmalloc() instead.
2651a862f68aSMike Rapoport  *
2652a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
2653e1ca7788SDave Young  */
2654e1ca7788SDave Young void *vzalloc(unsigned long size)
2655e1ca7788SDave Young {
26564d39d728SChristoph Hellwig 	return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_ZERO, NUMA_NO_NODE,
26574d39d728SChristoph Hellwig 				__builtin_return_address(0));
2658e1ca7788SDave Young }
2659e1ca7788SDave Young EXPORT_SYMBOL(vzalloc);
2660e1ca7788SDave Young 
2661e1ca7788SDave Young /**
2662ead04089SRolf Eike Beer  * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
266383342314SNick Piggin  * @size: allocation size
2664ead04089SRolf Eike Beer  *
2665ead04089SRolf Eike Beer  * The resulting memory area is zeroed so it can be mapped to userspace
2666ead04089SRolf Eike Beer  * without leaking data.
2667a862f68aSMike Rapoport  *
2668a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
266983342314SNick Piggin  */
267083342314SNick Piggin void *vmalloc_user(unsigned long size)
267183342314SNick Piggin {
2672bc84c535SRoman Penyaev 	return __vmalloc_node_range(size, SHMLBA,  VMALLOC_START, VMALLOC_END,
2673bc84c535SRoman Penyaev 				    GFP_KERNEL | __GFP_ZERO, PAGE_KERNEL,
2674bc84c535SRoman Penyaev 				    VM_USERMAP, NUMA_NO_NODE,
267500ef2d2fSDavid Rientjes 				    __builtin_return_address(0));
267683342314SNick Piggin }
267783342314SNick Piggin EXPORT_SYMBOL(vmalloc_user);
267883342314SNick Piggin 
267983342314SNick Piggin /**
2680930fc45aSChristoph Lameter  * vmalloc_node - allocate memory on a specific node
2681930fc45aSChristoph Lameter  * @size:	  allocation size
2682d44e0780SRandy Dunlap  * @node:	  numa node
2683930fc45aSChristoph Lameter  *
2684930fc45aSChristoph Lameter  * Allocate enough pages to cover @size from the page level
2685930fc45aSChristoph Lameter  * allocator and map them into contiguous kernel virtual space.
2686930fc45aSChristoph Lameter  *
2687c1c8897fSMichael Opdenacker  * For tight control over page level allocator and protection flags
2688930fc45aSChristoph Lameter  * use __vmalloc() instead.
2689a862f68aSMike Rapoport  *
2690a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
2691930fc45aSChristoph Lameter  */
2692930fc45aSChristoph Lameter void *vmalloc_node(unsigned long size, int node)
2693930fc45aSChristoph Lameter {
2694f38fcb9cSChristoph Hellwig 	return __vmalloc_node(size, 1, GFP_KERNEL, node,
2695f38fcb9cSChristoph Hellwig 			__builtin_return_address(0));
2696930fc45aSChristoph Lameter }
2697930fc45aSChristoph Lameter EXPORT_SYMBOL(vmalloc_node);
2698930fc45aSChristoph Lameter 
2699e1ca7788SDave Young /**
2700e1ca7788SDave Young  * vzalloc_node - allocate memory on a specific node with zero fill
2701e1ca7788SDave Young  * @size:	allocation size
2702e1ca7788SDave Young  * @node:	numa node
2703e1ca7788SDave Young  *
2704e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
2705e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
2706e1ca7788SDave Young  * The memory allocated is set to zero.
2707e1ca7788SDave Young  *
2708a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
2709e1ca7788SDave Young  */
2710e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node)
2711e1ca7788SDave Young {
27124d39d728SChristoph Hellwig 	return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_ZERO, node,
27134d39d728SChristoph Hellwig 				__builtin_return_address(0));
2714e1ca7788SDave Young }
2715e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node);
2716e1ca7788SDave Young 
27170d08e0d3SAndi Kleen #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
2718698d0831SMichal Hocko #define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL)
27190d08e0d3SAndi Kleen #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
2720698d0831SMichal Hocko #define GFP_VMALLOC32 (GFP_DMA | GFP_KERNEL)
27210d08e0d3SAndi Kleen #else
2722698d0831SMichal Hocko /*
2723698d0831SMichal Hocko  * 64b systems should always have either DMA or DMA32 zones. For others
2724698d0831SMichal Hocko  * GFP_DMA32 should do the right thing and use the normal zone.
2725698d0831SMichal Hocko  */
2726698d0831SMichal Hocko #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
27270d08e0d3SAndi Kleen #endif
27280d08e0d3SAndi Kleen 
27291da177e4SLinus Torvalds /**
27301da177e4SLinus Torvalds  * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
27311da177e4SLinus Torvalds  * @size:	allocation size
27321da177e4SLinus Torvalds  *
27331da177e4SLinus Torvalds  * Allocate enough 32bit PA addressable pages to cover @size from the
27341da177e4SLinus Torvalds  * page level allocator and map them into contiguous kernel virtual space.
2735a862f68aSMike Rapoport  *
2736a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
27371da177e4SLinus Torvalds  */
27381da177e4SLinus Torvalds void *vmalloc_32(unsigned long size)
27391da177e4SLinus Torvalds {
2740f38fcb9cSChristoph Hellwig 	return __vmalloc_node(size, 1, GFP_VMALLOC32, NUMA_NO_NODE,
2741f38fcb9cSChristoph Hellwig 			__builtin_return_address(0));
27421da177e4SLinus Torvalds }
27431da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc_32);
27441da177e4SLinus Torvalds 
274583342314SNick Piggin /**
2746ead04089SRolf Eike Beer  * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
274783342314SNick Piggin  * @size:	     allocation size
2748ead04089SRolf Eike Beer  *
2749ead04089SRolf Eike Beer  * The resulting memory area is 32bit addressable and zeroed so it can be
2750ead04089SRolf Eike Beer  * mapped to userspace without leaking data.
2751a862f68aSMike Rapoport  *
2752a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
275383342314SNick Piggin  */
275483342314SNick Piggin void *vmalloc_32_user(unsigned long size)
275583342314SNick Piggin {
2756bc84c535SRoman Penyaev 	return __vmalloc_node_range(size, SHMLBA,  VMALLOC_START, VMALLOC_END,
2757bc84c535SRoman Penyaev 				    GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
2758bc84c535SRoman Penyaev 				    VM_USERMAP, NUMA_NO_NODE,
27595a82ac71SRoman Penyaev 				    __builtin_return_address(0));
276083342314SNick Piggin }
276183342314SNick Piggin EXPORT_SYMBOL(vmalloc_32_user);
276283342314SNick Piggin 
2763d0107eb0SKAMEZAWA Hiroyuki /*
2764d0107eb0SKAMEZAWA Hiroyuki  * small helper routine , copy contents to buf from addr.
2765d0107eb0SKAMEZAWA Hiroyuki  * If the page is not present, fill zero.
2766d0107eb0SKAMEZAWA Hiroyuki  */
2767d0107eb0SKAMEZAWA Hiroyuki 
2768d0107eb0SKAMEZAWA Hiroyuki static int aligned_vread(char *buf, char *addr, unsigned long count)
2769d0107eb0SKAMEZAWA Hiroyuki {
2770d0107eb0SKAMEZAWA Hiroyuki 	struct page *p;
2771d0107eb0SKAMEZAWA Hiroyuki 	int copied = 0;
2772d0107eb0SKAMEZAWA Hiroyuki 
2773d0107eb0SKAMEZAWA Hiroyuki 	while (count) {
2774d0107eb0SKAMEZAWA Hiroyuki 		unsigned long offset, length;
2775d0107eb0SKAMEZAWA Hiroyuki 
2776891c49abSAlexander Kuleshov 		offset = offset_in_page(addr);
2777d0107eb0SKAMEZAWA Hiroyuki 		length = PAGE_SIZE - offset;
2778d0107eb0SKAMEZAWA Hiroyuki 		if (length > count)
2779d0107eb0SKAMEZAWA Hiroyuki 			length = count;
2780d0107eb0SKAMEZAWA Hiroyuki 		p = vmalloc_to_page(addr);
2781d0107eb0SKAMEZAWA Hiroyuki 		/*
2782d0107eb0SKAMEZAWA Hiroyuki 		 * To do safe access to this _mapped_ area, we need
2783d0107eb0SKAMEZAWA Hiroyuki 		 * lock. But adding lock here means that we need to add
2784d0107eb0SKAMEZAWA Hiroyuki 		 * overhead of vmalloc()/vfree() calles for this _debug_
2785d0107eb0SKAMEZAWA Hiroyuki 		 * interface, rarely used. Instead of that, we'll use
2786d0107eb0SKAMEZAWA Hiroyuki 		 * kmap() and get small overhead in this access function.
2787d0107eb0SKAMEZAWA Hiroyuki 		 */
2788d0107eb0SKAMEZAWA Hiroyuki 		if (p) {
2789d0107eb0SKAMEZAWA Hiroyuki 			/*
2790d0107eb0SKAMEZAWA Hiroyuki 			 * we can expect USER0 is not used (see vread/vwrite's
2791d0107eb0SKAMEZAWA Hiroyuki 			 * function description)
2792d0107eb0SKAMEZAWA Hiroyuki 			 */
27939b04c5feSCong Wang 			void *map = kmap_atomic(p);
2794d0107eb0SKAMEZAWA Hiroyuki 			memcpy(buf, map + offset, length);
27959b04c5feSCong Wang 			kunmap_atomic(map);
2796d0107eb0SKAMEZAWA Hiroyuki 		} else
2797d0107eb0SKAMEZAWA Hiroyuki 			memset(buf, 0, length);
2798d0107eb0SKAMEZAWA Hiroyuki 
2799d0107eb0SKAMEZAWA Hiroyuki 		addr += length;
2800d0107eb0SKAMEZAWA Hiroyuki 		buf += length;
2801d0107eb0SKAMEZAWA Hiroyuki 		copied += length;
2802d0107eb0SKAMEZAWA Hiroyuki 		count -= length;
2803d0107eb0SKAMEZAWA Hiroyuki 	}
2804d0107eb0SKAMEZAWA Hiroyuki 	return copied;
2805d0107eb0SKAMEZAWA Hiroyuki }
2806d0107eb0SKAMEZAWA Hiroyuki 
2807d0107eb0SKAMEZAWA Hiroyuki static int aligned_vwrite(char *buf, char *addr, unsigned long count)
2808d0107eb0SKAMEZAWA Hiroyuki {
2809d0107eb0SKAMEZAWA Hiroyuki 	struct page *p;
2810d0107eb0SKAMEZAWA Hiroyuki 	int copied = 0;
2811d0107eb0SKAMEZAWA Hiroyuki 
2812d0107eb0SKAMEZAWA Hiroyuki 	while (count) {
2813d0107eb0SKAMEZAWA Hiroyuki 		unsigned long offset, length;
2814d0107eb0SKAMEZAWA Hiroyuki 
2815891c49abSAlexander Kuleshov 		offset = offset_in_page(addr);
2816d0107eb0SKAMEZAWA Hiroyuki 		length = PAGE_SIZE - offset;
2817d0107eb0SKAMEZAWA Hiroyuki 		if (length > count)
2818d0107eb0SKAMEZAWA Hiroyuki 			length = count;
2819d0107eb0SKAMEZAWA Hiroyuki 		p = vmalloc_to_page(addr);
2820d0107eb0SKAMEZAWA Hiroyuki 		/*
2821d0107eb0SKAMEZAWA Hiroyuki 		 * To do safe access to this _mapped_ area, we need
2822d0107eb0SKAMEZAWA Hiroyuki 		 * lock. But adding lock here means that we need to add
2823d0107eb0SKAMEZAWA Hiroyuki 		 * overhead of vmalloc()/vfree() calles for this _debug_
2824d0107eb0SKAMEZAWA Hiroyuki 		 * interface, rarely used. Instead of that, we'll use
2825d0107eb0SKAMEZAWA Hiroyuki 		 * kmap() and get small overhead in this access function.
2826d0107eb0SKAMEZAWA Hiroyuki 		 */
2827d0107eb0SKAMEZAWA Hiroyuki 		if (p) {
2828d0107eb0SKAMEZAWA Hiroyuki 			/*
2829d0107eb0SKAMEZAWA Hiroyuki 			 * we can expect USER0 is not used (see vread/vwrite's
2830d0107eb0SKAMEZAWA Hiroyuki 			 * function description)
2831d0107eb0SKAMEZAWA Hiroyuki 			 */
28329b04c5feSCong Wang 			void *map = kmap_atomic(p);
2833d0107eb0SKAMEZAWA Hiroyuki 			memcpy(map + offset, buf, length);
28349b04c5feSCong Wang 			kunmap_atomic(map);
2835d0107eb0SKAMEZAWA Hiroyuki 		}
2836d0107eb0SKAMEZAWA Hiroyuki 		addr += length;
2837d0107eb0SKAMEZAWA Hiroyuki 		buf += length;
2838d0107eb0SKAMEZAWA Hiroyuki 		copied += length;
2839d0107eb0SKAMEZAWA Hiroyuki 		count -= length;
2840d0107eb0SKAMEZAWA Hiroyuki 	}
2841d0107eb0SKAMEZAWA Hiroyuki 	return copied;
2842d0107eb0SKAMEZAWA Hiroyuki }
2843d0107eb0SKAMEZAWA Hiroyuki 
2844d0107eb0SKAMEZAWA Hiroyuki /**
2845d0107eb0SKAMEZAWA Hiroyuki  * vread() - read vmalloc area in a safe way.
2846d0107eb0SKAMEZAWA Hiroyuki  * @buf:     buffer for reading data
2847d0107eb0SKAMEZAWA Hiroyuki  * @addr:    vm address.
2848d0107eb0SKAMEZAWA Hiroyuki  * @count:   number of bytes to be read.
2849d0107eb0SKAMEZAWA Hiroyuki  *
2850d0107eb0SKAMEZAWA Hiroyuki  * This function checks that addr is a valid vmalloc'ed area, and
2851d0107eb0SKAMEZAWA Hiroyuki  * copy data from that area to a given buffer. If the given memory range
2852d0107eb0SKAMEZAWA Hiroyuki  * of [addr...addr+count) includes some valid address, data is copied to
2853d0107eb0SKAMEZAWA Hiroyuki  * proper area of @buf. If there are memory holes, they'll be zero-filled.
2854d0107eb0SKAMEZAWA Hiroyuki  * IOREMAP area is treated as memory hole and no copy is done.
2855d0107eb0SKAMEZAWA Hiroyuki  *
2856d0107eb0SKAMEZAWA Hiroyuki  * If [addr...addr+count) doesn't includes any intersects with alive
2857a8e5202dSCong Wang  * vm_struct area, returns 0. @buf should be kernel's buffer.
2858d0107eb0SKAMEZAWA Hiroyuki  *
2859d0107eb0SKAMEZAWA Hiroyuki  * Note: In usual ops, vread() is never necessary because the caller
2860d0107eb0SKAMEZAWA Hiroyuki  * should know vmalloc() area is valid and can use memcpy().
2861d0107eb0SKAMEZAWA Hiroyuki  * This is for routines which have to access vmalloc area without
2862d9009d67SGeert Uytterhoeven  * any information, as /dev/kmem.
2863a862f68aSMike Rapoport  *
2864a862f68aSMike Rapoport  * Return: number of bytes for which addr and buf should be increased
2865a862f68aSMike Rapoport  * (same number as @count) or %0 if [addr...addr+count) doesn't
2866a862f68aSMike Rapoport  * include any intersection with valid vmalloc area
2867d0107eb0SKAMEZAWA Hiroyuki  */
28681da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count)
28691da177e4SLinus Torvalds {
2870e81ce85fSJoonsoo Kim 	struct vmap_area *va;
2871e81ce85fSJoonsoo Kim 	struct vm_struct *vm;
28721da177e4SLinus Torvalds 	char *vaddr, *buf_start = buf;
2873d0107eb0SKAMEZAWA Hiroyuki 	unsigned long buflen = count;
28741da177e4SLinus Torvalds 	unsigned long n;
28751da177e4SLinus Torvalds 
28761da177e4SLinus Torvalds 	/* Don't allow overflow */
28771da177e4SLinus Torvalds 	if ((unsigned long) addr + count < count)
28781da177e4SLinus Torvalds 		count = -(unsigned long) addr;
28791da177e4SLinus Torvalds 
2880e81ce85fSJoonsoo Kim 	spin_lock(&vmap_area_lock);
2881e81ce85fSJoonsoo Kim 	list_for_each_entry(va, &vmap_area_list, list) {
2882e81ce85fSJoonsoo Kim 		if (!count)
2883e81ce85fSJoonsoo Kim 			break;
2884e81ce85fSJoonsoo Kim 
2885688fcbfcSPengfei Li 		if (!va->vm)
2886e81ce85fSJoonsoo Kim 			continue;
2887e81ce85fSJoonsoo Kim 
2888e81ce85fSJoonsoo Kim 		vm = va->vm;
2889e81ce85fSJoonsoo Kim 		vaddr = (char *) vm->addr;
2890762216abSWanpeng Li 		if (addr >= vaddr + get_vm_area_size(vm))
28911da177e4SLinus Torvalds 			continue;
28921da177e4SLinus Torvalds 		while (addr < vaddr) {
28931da177e4SLinus Torvalds 			if (count == 0)
28941da177e4SLinus Torvalds 				goto finished;
28951da177e4SLinus Torvalds 			*buf = '\0';
28961da177e4SLinus Torvalds 			buf++;
28971da177e4SLinus Torvalds 			addr++;
28981da177e4SLinus Torvalds 			count--;
28991da177e4SLinus Torvalds 		}
2900762216abSWanpeng Li 		n = vaddr + get_vm_area_size(vm) - addr;
2901d0107eb0SKAMEZAWA Hiroyuki 		if (n > count)
2902d0107eb0SKAMEZAWA Hiroyuki 			n = count;
2903e81ce85fSJoonsoo Kim 		if (!(vm->flags & VM_IOREMAP))
2904d0107eb0SKAMEZAWA Hiroyuki 			aligned_vread(buf, addr, n);
2905d0107eb0SKAMEZAWA Hiroyuki 		else /* IOREMAP area is treated as memory hole */
2906d0107eb0SKAMEZAWA Hiroyuki 			memset(buf, 0, n);
2907d0107eb0SKAMEZAWA Hiroyuki 		buf += n;
2908d0107eb0SKAMEZAWA Hiroyuki 		addr += n;
2909d0107eb0SKAMEZAWA Hiroyuki 		count -= n;
29101da177e4SLinus Torvalds 	}
29111da177e4SLinus Torvalds finished:
2912e81ce85fSJoonsoo Kim 	spin_unlock(&vmap_area_lock);
2913d0107eb0SKAMEZAWA Hiroyuki 
2914d0107eb0SKAMEZAWA Hiroyuki 	if (buf == buf_start)
2915d0107eb0SKAMEZAWA Hiroyuki 		return 0;
2916d0107eb0SKAMEZAWA Hiroyuki 	/* zero-fill memory holes */
2917d0107eb0SKAMEZAWA Hiroyuki 	if (buf != buf_start + buflen)
2918d0107eb0SKAMEZAWA Hiroyuki 		memset(buf, 0, buflen - (buf - buf_start));
2919d0107eb0SKAMEZAWA Hiroyuki 
2920d0107eb0SKAMEZAWA Hiroyuki 	return buflen;
29211da177e4SLinus Torvalds }
29221da177e4SLinus Torvalds 
2923d0107eb0SKAMEZAWA Hiroyuki /**
2924d0107eb0SKAMEZAWA Hiroyuki  * vwrite() - write vmalloc area in a safe way.
2925d0107eb0SKAMEZAWA Hiroyuki  * @buf:      buffer for source data
2926d0107eb0SKAMEZAWA Hiroyuki  * @addr:     vm address.
2927d0107eb0SKAMEZAWA Hiroyuki  * @count:    number of bytes to be read.
2928d0107eb0SKAMEZAWA Hiroyuki  *
2929d0107eb0SKAMEZAWA Hiroyuki  * This function checks that addr is a valid vmalloc'ed area, and
2930d0107eb0SKAMEZAWA Hiroyuki  * copy data from a buffer to the given addr. If specified range of
2931d0107eb0SKAMEZAWA Hiroyuki  * [addr...addr+count) includes some valid address, data is copied from
2932d0107eb0SKAMEZAWA Hiroyuki  * proper area of @buf. If there are memory holes, no copy to hole.
2933d0107eb0SKAMEZAWA Hiroyuki  * IOREMAP area is treated as memory hole and no copy is done.
2934d0107eb0SKAMEZAWA Hiroyuki  *
2935d0107eb0SKAMEZAWA Hiroyuki  * If [addr...addr+count) doesn't includes any intersects with alive
2936a8e5202dSCong Wang  * vm_struct area, returns 0. @buf should be kernel's buffer.
2937d0107eb0SKAMEZAWA Hiroyuki  *
2938d0107eb0SKAMEZAWA Hiroyuki  * Note: In usual ops, vwrite() is never necessary because the caller
2939d0107eb0SKAMEZAWA Hiroyuki  * should know vmalloc() area is valid and can use memcpy().
2940d0107eb0SKAMEZAWA Hiroyuki  * This is for routines which have to access vmalloc area without
2941d9009d67SGeert Uytterhoeven  * any information, as /dev/kmem.
2942a862f68aSMike Rapoport  *
2943a862f68aSMike Rapoport  * Return: number of bytes for which addr and buf should be
2944a862f68aSMike Rapoport  * increased (same number as @count) or %0 if [addr...addr+count)
2945a862f68aSMike Rapoport  * doesn't include any intersection with valid vmalloc area
2946d0107eb0SKAMEZAWA Hiroyuki  */
29471da177e4SLinus Torvalds long vwrite(char *buf, char *addr, unsigned long count)
29481da177e4SLinus Torvalds {
2949e81ce85fSJoonsoo Kim 	struct vmap_area *va;
2950e81ce85fSJoonsoo Kim 	struct vm_struct *vm;
2951d0107eb0SKAMEZAWA Hiroyuki 	char *vaddr;
2952d0107eb0SKAMEZAWA Hiroyuki 	unsigned long n, buflen;
2953d0107eb0SKAMEZAWA Hiroyuki 	int copied = 0;
29541da177e4SLinus Torvalds 
29551da177e4SLinus Torvalds 	/* Don't allow overflow */
29561da177e4SLinus Torvalds 	if ((unsigned long) addr + count < count)
29571da177e4SLinus Torvalds 		count = -(unsigned long) addr;
2958d0107eb0SKAMEZAWA Hiroyuki 	buflen = count;
29591da177e4SLinus Torvalds 
2960e81ce85fSJoonsoo Kim 	spin_lock(&vmap_area_lock);
2961e81ce85fSJoonsoo Kim 	list_for_each_entry(va, &vmap_area_list, list) {
2962e81ce85fSJoonsoo Kim 		if (!count)
2963e81ce85fSJoonsoo Kim 			break;
2964e81ce85fSJoonsoo Kim 
2965688fcbfcSPengfei Li 		if (!va->vm)
2966e81ce85fSJoonsoo Kim 			continue;
2967e81ce85fSJoonsoo Kim 
2968e81ce85fSJoonsoo Kim 		vm = va->vm;
2969e81ce85fSJoonsoo Kim 		vaddr = (char *) vm->addr;
2970762216abSWanpeng Li 		if (addr >= vaddr + get_vm_area_size(vm))
29711da177e4SLinus Torvalds 			continue;
29721da177e4SLinus Torvalds 		while (addr < vaddr) {
29731da177e4SLinus Torvalds 			if (count == 0)
29741da177e4SLinus Torvalds 				goto finished;
29751da177e4SLinus Torvalds 			buf++;
29761da177e4SLinus Torvalds 			addr++;
29771da177e4SLinus Torvalds 			count--;
29781da177e4SLinus Torvalds 		}
2979762216abSWanpeng Li 		n = vaddr + get_vm_area_size(vm) - addr;
2980d0107eb0SKAMEZAWA Hiroyuki 		if (n > count)
2981d0107eb0SKAMEZAWA Hiroyuki 			n = count;
2982e81ce85fSJoonsoo Kim 		if (!(vm->flags & VM_IOREMAP)) {
2983d0107eb0SKAMEZAWA Hiroyuki 			aligned_vwrite(buf, addr, n);
2984d0107eb0SKAMEZAWA Hiroyuki 			copied++;
2985d0107eb0SKAMEZAWA Hiroyuki 		}
2986d0107eb0SKAMEZAWA Hiroyuki 		buf += n;
2987d0107eb0SKAMEZAWA Hiroyuki 		addr += n;
2988d0107eb0SKAMEZAWA Hiroyuki 		count -= n;
29891da177e4SLinus Torvalds 	}
29901da177e4SLinus Torvalds finished:
2991e81ce85fSJoonsoo Kim 	spin_unlock(&vmap_area_lock);
2992d0107eb0SKAMEZAWA Hiroyuki 	if (!copied)
2993d0107eb0SKAMEZAWA Hiroyuki 		return 0;
2994d0107eb0SKAMEZAWA Hiroyuki 	return buflen;
29951da177e4SLinus Torvalds }
299683342314SNick Piggin 
299783342314SNick Piggin /**
2998e69e9d4aSHATAYAMA Daisuke  * remap_vmalloc_range_partial - map vmalloc pages to userspace
2999e69e9d4aSHATAYAMA Daisuke  * @vma:		vma to cover
3000e69e9d4aSHATAYAMA Daisuke  * @uaddr:		target user address to start at
3001e69e9d4aSHATAYAMA Daisuke  * @kaddr:		virtual address of vmalloc kernel memory
3002bdebd6a2SJann Horn  * @pgoff:		offset from @kaddr to start at
3003e69e9d4aSHATAYAMA Daisuke  * @size:		size of map area
3004e69e9d4aSHATAYAMA Daisuke  *
3005e69e9d4aSHATAYAMA Daisuke  * Returns:	0 for success, -Exxx on failure
3006e69e9d4aSHATAYAMA Daisuke  *
3007e69e9d4aSHATAYAMA Daisuke  * This function checks that @kaddr is a valid vmalloc'ed area,
3008e69e9d4aSHATAYAMA Daisuke  * and that it is big enough to cover the range starting at
3009e69e9d4aSHATAYAMA Daisuke  * @uaddr in @vma. Will return failure if that criteria isn't
3010e69e9d4aSHATAYAMA Daisuke  * met.
3011e69e9d4aSHATAYAMA Daisuke  *
3012e69e9d4aSHATAYAMA Daisuke  * Similar to remap_pfn_range() (see mm/memory.c)
3013e69e9d4aSHATAYAMA Daisuke  */
3014e69e9d4aSHATAYAMA Daisuke int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr,
3015bdebd6a2SJann Horn 				void *kaddr, unsigned long pgoff,
3016bdebd6a2SJann Horn 				unsigned long size)
3017e69e9d4aSHATAYAMA Daisuke {
3018e69e9d4aSHATAYAMA Daisuke 	struct vm_struct *area;
3019bdebd6a2SJann Horn 	unsigned long off;
3020bdebd6a2SJann Horn 	unsigned long end_index;
3021bdebd6a2SJann Horn 
3022bdebd6a2SJann Horn 	if (check_shl_overflow(pgoff, PAGE_SHIFT, &off))
3023bdebd6a2SJann Horn 		return -EINVAL;
3024e69e9d4aSHATAYAMA Daisuke 
3025e69e9d4aSHATAYAMA Daisuke 	size = PAGE_ALIGN(size);
3026e69e9d4aSHATAYAMA Daisuke 
3027e69e9d4aSHATAYAMA Daisuke 	if (!PAGE_ALIGNED(uaddr) || !PAGE_ALIGNED(kaddr))
3028e69e9d4aSHATAYAMA Daisuke 		return -EINVAL;
3029e69e9d4aSHATAYAMA Daisuke 
3030e69e9d4aSHATAYAMA Daisuke 	area = find_vm_area(kaddr);
3031e69e9d4aSHATAYAMA Daisuke 	if (!area)
3032e69e9d4aSHATAYAMA Daisuke 		return -EINVAL;
3033e69e9d4aSHATAYAMA Daisuke 
3034fe9041c2SChristoph Hellwig 	if (!(area->flags & (VM_USERMAP | VM_DMA_COHERENT)))
3035e69e9d4aSHATAYAMA Daisuke 		return -EINVAL;
3036e69e9d4aSHATAYAMA Daisuke 
3037bdebd6a2SJann Horn 	if (check_add_overflow(size, off, &end_index) ||
3038bdebd6a2SJann Horn 	    end_index > get_vm_area_size(area))
3039e69e9d4aSHATAYAMA Daisuke 		return -EINVAL;
3040bdebd6a2SJann Horn 	kaddr += off;
3041e69e9d4aSHATAYAMA Daisuke 
3042e69e9d4aSHATAYAMA Daisuke 	do {
3043e69e9d4aSHATAYAMA Daisuke 		struct page *page = vmalloc_to_page(kaddr);
3044e69e9d4aSHATAYAMA Daisuke 		int ret;
3045e69e9d4aSHATAYAMA Daisuke 
3046e69e9d4aSHATAYAMA Daisuke 		ret = vm_insert_page(vma, uaddr, page);
3047e69e9d4aSHATAYAMA Daisuke 		if (ret)
3048e69e9d4aSHATAYAMA Daisuke 			return ret;
3049e69e9d4aSHATAYAMA Daisuke 
3050e69e9d4aSHATAYAMA Daisuke 		uaddr += PAGE_SIZE;
3051e69e9d4aSHATAYAMA Daisuke 		kaddr += PAGE_SIZE;
3052e69e9d4aSHATAYAMA Daisuke 		size -= PAGE_SIZE;
3053e69e9d4aSHATAYAMA Daisuke 	} while (size > 0);
3054e69e9d4aSHATAYAMA Daisuke 
3055e69e9d4aSHATAYAMA Daisuke 	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3056e69e9d4aSHATAYAMA Daisuke 
3057e69e9d4aSHATAYAMA Daisuke 	return 0;
3058e69e9d4aSHATAYAMA Daisuke }
3059e69e9d4aSHATAYAMA Daisuke EXPORT_SYMBOL(remap_vmalloc_range_partial);
3060e69e9d4aSHATAYAMA Daisuke 
3061e69e9d4aSHATAYAMA Daisuke /**
306283342314SNick Piggin  * remap_vmalloc_range - map vmalloc pages to userspace
306383342314SNick Piggin  * @vma:		vma to cover (map full range of vma)
306483342314SNick Piggin  * @addr:		vmalloc memory
306583342314SNick Piggin  * @pgoff:		number of pages into addr before first page to map
30667682486bSRandy Dunlap  *
30677682486bSRandy Dunlap  * Returns:	0 for success, -Exxx on failure
306883342314SNick Piggin  *
306983342314SNick Piggin  * This function checks that addr is a valid vmalloc'ed area, and
307083342314SNick Piggin  * that it is big enough to cover the vma. Will return failure if
307183342314SNick Piggin  * that criteria isn't met.
307283342314SNick Piggin  *
307372fd4a35SRobert P. J. Day  * Similar to remap_pfn_range() (see mm/memory.c)
307483342314SNick Piggin  */
307583342314SNick Piggin int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
307683342314SNick Piggin 						unsigned long pgoff)
307783342314SNick Piggin {
3078e69e9d4aSHATAYAMA Daisuke 	return remap_vmalloc_range_partial(vma, vma->vm_start,
3079bdebd6a2SJann Horn 					   addr, pgoff,
3080e69e9d4aSHATAYAMA Daisuke 					   vma->vm_end - vma->vm_start);
308183342314SNick Piggin }
308283342314SNick Piggin EXPORT_SYMBOL(remap_vmalloc_range);
308383342314SNick Piggin 
30845f4352fbSJeremy Fitzhardinge void free_vm_area(struct vm_struct *area)
30855f4352fbSJeremy Fitzhardinge {
30865f4352fbSJeremy Fitzhardinge 	struct vm_struct *ret;
30875f4352fbSJeremy Fitzhardinge 	ret = remove_vm_area(area->addr);
30885f4352fbSJeremy Fitzhardinge 	BUG_ON(ret != area);
30895f4352fbSJeremy Fitzhardinge 	kfree(area);
30905f4352fbSJeremy Fitzhardinge }
30915f4352fbSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(free_vm_area);
3092a10aa579SChristoph Lameter 
30934f8b02b4STejun Heo #ifdef CONFIG_SMP
3094ca23e405STejun Heo static struct vmap_area *node_to_va(struct rb_node *n)
3095ca23e405STejun Heo {
30964583e773SGeliang Tang 	return rb_entry_safe(n, struct vmap_area, rb_node);
3097ca23e405STejun Heo }
3098ca23e405STejun Heo 
3099ca23e405STejun Heo /**
310068ad4a33SUladzislau Rezki (Sony)  * pvm_find_va_enclose_addr - find the vmap_area @addr belongs to
310168ad4a33SUladzislau Rezki (Sony)  * @addr: target address
3102ca23e405STejun Heo  *
310368ad4a33SUladzislau Rezki (Sony)  * Returns: vmap_area if it is found. If there is no such area
310468ad4a33SUladzislau Rezki (Sony)  *   the first highest(reverse order) vmap_area is returned
310568ad4a33SUladzislau Rezki (Sony)  *   i.e. va->va_start < addr && va->va_end < addr or NULL
310668ad4a33SUladzislau Rezki (Sony)  *   if there are no any areas before @addr.
3107ca23e405STejun Heo  */
310868ad4a33SUladzislau Rezki (Sony) static struct vmap_area *
310968ad4a33SUladzislau Rezki (Sony) pvm_find_va_enclose_addr(unsigned long addr)
3110ca23e405STejun Heo {
311168ad4a33SUladzislau Rezki (Sony) 	struct vmap_area *va, *tmp;
311268ad4a33SUladzislau Rezki (Sony) 	struct rb_node *n;
311368ad4a33SUladzislau Rezki (Sony) 
311468ad4a33SUladzislau Rezki (Sony) 	n = free_vmap_area_root.rb_node;
311568ad4a33SUladzislau Rezki (Sony) 	va = NULL;
3116ca23e405STejun Heo 
3117ca23e405STejun Heo 	while (n) {
311868ad4a33SUladzislau Rezki (Sony) 		tmp = rb_entry(n, struct vmap_area, rb_node);
311968ad4a33SUladzislau Rezki (Sony) 		if (tmp->va_start <= addr) {
312068ad4a33SUladzislau Rezki (Sony) 			va = tmp;
312168ad4a33SUladzislau Rezki (Sony) 			if (tmp->va_end >= addr)
3122ca23e405STejun Heo 				break;
3123ca23e405STejun Heo 
312468ad4a33SUladzislau Rezki (Sony) 			n = n->rb_right;
3125ca23e405STejun Heo 		} else {
312668ad4a33SUladzislau Rezki (Sony) 			n = n->rb_left;
3127ca23e405STejun Heo 		}
312868ad4a33SUladzislau Rezki (Sony) 	}
312968ad4a33SUladzislau Rezki (Sony) 
313068ad4a33SUladzislau Rezki (Sony) 	return va;
3131ca23e405STejun Heo }
3132ca23e405STejun Heo 
3133ca23e405STejun Heo /**
313468ad4a33SUladzislau Rezki (Sony)  * pvm_determine_end_from_reverse - find the highest aligned address
313568ad4a33SUladzislau Rezki (Sony)  * of free block below VMALLOC_END
313668ad4a33SUladzislau Rezki (Sony)  * @va:
313768ad4a33SUladzislau Rezki (Sony)  *   in - the VA we start the search(reverse order);
313868ad4a33SUladzislau Rezki (Sony)  *   out - the VA with the highest aligned end address.
3139ca23e405STejun Heo  *
314068ad4a33SUladzislau Rezki (Sony)  * Returns: determined end address within vmap_area
3141ca23e405STejun Heo  */
314268ad4a33SUladzislau Rezki (Sony) static unsigned long
314368ad4a33SUladzislau Rezki (Sony) pvm_determine_end_from_reverse(struct vmap_area **va, unsigned long align)
3144ca23e405STejun Heo {
314568ad4a33SUladzislau Rezki (Sony) 	unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
3146ca23e405STejun Heo 	unsigned long addr;
3147ca23e405STejun Heo 
314868ad4a33SUladzislau Rezki (Sony) 	if (likely(*va)) {
314968ad4a33SUladzislau Rezki (Sony) 		list_for_each_entry_from_reverse((*va),
315068ad4a33SUladzislau Rezki (Sony) 				&free_vmap_area_list, list) {
315168ad4a33SUladzislau Rezki (Sony) 			addr = min((*va)->va_end & ~(align - 1), vmalloc_end);
315268ad4a33SUladzislau Rezki (Sony) 			if ((*va)->va_start < addr)
315368ad4a33SUladzislau Rezki (Sony) 				return addr;
315468ad4a33SUladzislau Rezki (Sony) 		}
3155ca23e405STejun Heo 	}
3156ca23e405STejun Heo 
315768ad4a33SUladzislau Rezki (Sony) 	return 0;
3158ca23e405STejun Heo }
3159ca23e405STejun Heo 
3160ca23e405STejun Heo /**
3161ca23e405STejun Heo  * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator
3162ca23e405STejun Heo  * @offsets: array containing offset of each area
3163ca23e405STejun Heo  * @sizes: array containing size of each area
3164ca23e405STejun Heo  * @nr_vms: the number of areas to allocate
3165ca23e405STejun Heo  * @align: alignment, all entries in @offsets and @sizes must be aligned to this
3166ca23e405STejun Heo  *
3167ca23e405STejun Heo  * Returns: kmalloc'd vm_struct pointer array pointing to allocated
3168ca23e405STejun Heo  *	    vm_structs on success, %NULL on failure
3169ca23e405STejun Heo  *
3170ca23e405STejun Heo  * Percpu allocator wants to use congruent vm areas so that it can
3171ca23e405STejun Heo  * maintain the offsets among percpu areas.  This function allocates
3172ec3f64fcSDavid Rientjes  * congruent vmalloc areas for it with GFP_KERNEL.  These areas tend to
3173ec3f64fcSDavid Rientjes  * be scattered pretty far, distance between two areas easily going up
3174ec3f64fcSDavid Rientjes  * to gigabytes.  To avoid interacting with regular vmallocs, these
3175ec3f64fcSDavid Rientjes  * areas are allocated from top.
3176ca23e405STejun Heo  *
3177ca23e405STejun Heo  * Despite its complicated look, this allocator is rather simple. It
317868ad4a33SUladzislau Rezki (Sony)  * does everything top-down and scans free blocks from the end looking
317968ad4a33SUladzislau Rezki (Sony)  * for matching base. While scanning, if any of the areas do not fit the
318068ad4a33SUladzislau Rezki (Sony)  * base address is pulled down to fit the area. Scanning is repeated till
318168ad4a33SUladzislau Rezki (Sony)  * all the areas fit and then all necessary data structures are inserted
318268ad4a33SUladzislau Rezki (Sony)  * and the result is returned.
3183ca23e405STejun Heo  */
3184ca23e405STejun Heo struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
3185ca23e405STejun Heo 				     const size_t *sizes, int nr_vms,
3186ec3f64fcSDavid Rientjes 				     size_t align)
3187ca23e405STejun Heo {
3188ca23e405STejun Heo 	const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align);
3189ca23e405STejun Heo 	const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
319068ad4a33SUladzislau Rezki (Sony) 	struct vmap_area **vas, *va;
3191ca23e405STejun Heo 	struct vm_struct **vms;
3192ca23e405STejun Heo 	int area, area2, last_area, term_area;
3193253a496dSDaniel Axtens 	unsigned long base, start, size, end, last_end, orig_start, orig_end;
3194ca23e405STejun Heo 	bool purged = false;
319568ad4a33SUladzislau Rezki (Sony) 	enum fit_type type;
3196ca23e405STejun Heo 
3197ca23e405STejun Heo 	/* verify parameters and allocate data structures */
3198891c49abSAlexander Kuleshov 	BUG_ON(offset_in_page(align) || !is_power_of_2(align));
3199ca23e405STejun Heo 	for (last_area = 0, area = 0; area < nr_vms; area++) {
3200ca23e405STejun Heo 		start = offsets[area];
3201ca23e405STejun Heo 		end = start + sizes[area];
3202ca23e405STejun Heo 
3203ca23e405STejun Heo 		/* is everything aligned properly? */
3204ca23e405STejun Heo 		BUG_ON(!IS_ALIGNED(offsets[area], align));
3205ca23e405STejun Heo 		BUG_ON(!IS_ALIGNED(sizes[area], align));
3206ca23e405STejun Heo 
3207ca23e405STejun Heo 		/* detect the area with the highest address */
3208ca23e405STejun Heo 		if (start > offsets[last_area])
3209ca23e405STejun Heo 			last_area = area;
3210ca23e405STejun Heo 
3211c568da28SWei Yang 		for (area2 = area + 1; area2 < nr_vms; area2++) {
3212ca23e405STejun Heo 			unsigned long start2 = offsets[area2];
3213ca23e405STejun Heo 			unsigned long end2 = start2 + sizes[area2];
3214ca23e405STejun Heo 
3215c568da28SWei Yang 			BUG_ON(start2 < end && start < end2);
3216ca23e405STejun Heo 		}
3217ca23e405STejun Heo 	}
3218ca23e405STejun Heo 	last_end = offsets[last_area] + sizes[last_area];
3219ca23e405STejun Heo 
3220ca23e405STejun Heo 	if (vmalloc_end - vmalloc_start < last_end) {
3221ca23e405STejun Heo 		WARN_ON(true);
3222ca23e405STejun Heo 		return NULL;
3223ca23e405STejun Heo 	}
3224ca23e405STejun Heo 
32254d67d860SThomas Meyer 	vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL);
32264d67d860SThomas Meyer 	vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL);
3227ca23e405STejun Heo 	if (!vas || !vms)
3228f1db7afdSKautuk Consul 		goto err_free2;
3229ca23e405STejun Heo 
3230ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++) {
323168ad4a33SUladzislau Rezki (Sony) 		vas[area] = kmem_cache_zalloc(vmap_area_cachep, GFP_KERNEL);
3232ec3f64fcSDavid Rientjes 		vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL);
3233ca23e405STejun Heo 		if (!vas[area] || !vms[area])
3234ca23e405STejun Heo 			goto err_free;
3235ca23e405STejun Heo 	}
3236ca23e405STejun Heo retry:
3237e36176beSUladzislau Rezki (Sony) 	spin_lock(&free_vmap_area_lock);
3238ca23e405STejun Heo 
3239ca23e405STejun Heo 	/* start scanning - we scan from the top, begin with the last area */
3240ca23e405STejun Heo 	area = term_area = last_area;
3241ca23e405STejun Heo 	start = offsets[area];
3242ca23e405STejun Heo 	end = start + sizes[area];
3243ca23e405STejun Heo 
324468ad4a33SUladzislau Rezki (Sony) 	va = pvm_find_va_enclose_addr(vmalloc_end);
324568ad4a33SUladzislau Rezki (Sony) 	base = pvm_determine_end_from_reverse(&va, align) - end;
3246ca23e405STejun Heo 
3247ca23e405STejun Heo 	while (true) {
3248ca23e405STejun Heo 		/*
3249ca23e405STejun Heo 		 * base might have underflowed, add last_end before
3250ca23e405STejun Heo 		 * comparing.
3251ca23e405STejun Heo 		 */
325268ad4a33SUladzislau Rezki (Sony) 		if (base + last_end < vmalloc_start + last_end)
325368ad4a33SUladzislau Rezki (Sony) 			goto overflow;
3254ca23e405STejun Heo 
3255ca23e405STejun Heo 		/*
325668ad4a33SUladzislau Rezki (Sony) 		 * Fitting base has not been found.
3257ca23e405STejun Heo 		 */
325868ad4a33SUladzislau Rezki (Sony) 		if (va == NULL)
325968ad4a33SUladzislau Rezki (Sony) 			goto overflow;
3260ca23e405STejun Heo 
3261ca23e405STejun Heo 		/*
3262d8cc323dSQiujun Huang 		 * If required width exceeds current VA block, move
32635336e52cSKuppuswamy Sathyanarayanan 		 * base downwards and then recheck.
32645336e52cSKuppuswamy Sathyanarayanan 		 */
32655336e52cSKuppuswamy Sathyanarayanan 		if (base + end > va->va_end) {
32665336e52cSKuppuswamy Sathyanarayanan 			base = pvm_determine_end_from_reverse(&va, align) - end;
32675336e52cSKuppuswamy Sathyanarayanan 			term_area = area;
32685336e52cSKuppuswamy Sathyanarayanan 			continue;
32695336e52cSKuppuswamy Sathyanarayanan 		}
32705336e52cSKuppuswamy Sathyanarayanan 
32715336e52cSKuppuswamy Sathyanarayanan 		/*
327268ad4a33SUladzislau Rezki (Sony) 		 * If this VA does not fit, move base downwards and recheck.
3273ca23e405STejun Heo 		 */
32745336e52cSKuppuswamy Sathyanarayanan 		if (base + start < va->va_start) {
327568ad4a33SUladzislau Rezki (Sony) 			va = node_to_va(rb_prev(&va->rb_node));
327668ad4a33SUladzislau Rezki (Sony) 			base = pvm_determine_end_from_reverse(&va, align) - end;
3277ca23e405STejun Heo 			term_area = area;
3278ca23e405STejun Heo 			continue;
3279ca23e405STejun Heo 		}
3280ca23e405STejun Heo 
3281ca23e405STejun Heo 		/*
3282ca23e405STejun Heo 		 * This area fits, move on to the previous one.  If
3283ca23e405STejun Heo 		 * the previous one is the terminal one, we're done.
3284ca23e405STejun Heo 		 */
3285ca23e405STejun Heo 		area = (area + nr_vms - 1) % nr_vms;
3286ca23e405STejun Heo 		if (area == term_area)
3287ca23e405STejun Heo 			break;
328868ad4a33SUladzislau Rezki (Sony) 
3289ca23e405STejun Heo 		start = offsets[area];
3290ca23e405STejun Heo 		end = start + sizes[area];
329168ad4a33SUladzislau Rezki (Sony) 		va = pvm_find_va_enclose_addr(base + end);
3292ca23e405STejun Heo 	}
329368ad4a33SUladzislau Rezki (Sony) 
3294ca23e405STejun Heo 	/* we've found a fitting base, insert all va's */
3295ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++) {
329668ad4a33SUladzislau Rezki (Sony) 		int ret;
3297ca23e405STejun Heo 
329868ad4a33SUladzislau Rezki (Sony) 		start = base + offsets[area];
329968ad4a33SUladzislau Rezki (Sony) 		size = sizes[area];
330068ad4a33SUladzislau Rezki (Sony) 
330168ad4a33SUladzislau Rezki (Sony) 		va = pvm_find_va_enclose_addr(start);
330268ad4a33SUladzislau Rezki (Sony) 		if (WARN_ON_ONCE(va == NULL))
330368ad4a33SUladzislau Rezki (Sony) 			/* It is a BUG(), but trigger recovery instead. */
330468ad4a33SUladzislau Rezki (Sony) 			goto recovery;
330568ad4a33SUladzislau Rezki (Sony) 
330668ad4a33SUladzislau Rezki (Sony) 		type = classify_va_fit_type(va, start, size);
330768ad4a33SUladzislau Rezki (Sony) 		if (WARN_ON_ONCE(type == NOTHING_FIT))
330868ad4a33SUladzislau Rezki (Sony) 			/* It is a BUG(), but trigger recovery instead. */
330968ad4a33SUladzislau Rezki (Sony) 			goto recovery;
331068ad4a33SUladzislau Rezki (Sony) 
331168ad4a33SUladzislau Rezki (Sony) 		ret = adjust_va_to_fit_type(va, start, size, type);
331268ad4a33SUladzislau Rezki (Sony) 		if (unlikely(ret))
331368ad4a33SUladzislau Rezki (Sony) 			goto recovery;
331468ad4a33SUladzislau Rezki (Sony) 
331568ad4a33SUladzislau Rezki (Sony) 		/* Allocated area. */
331668ad4a33SUladzislau Rezki (Sony) 		va = vas[area];
331768ad4a33SUladzislau Rezki (Sony) 		va->va_start = start;
331868ad4a33SUladzislau Rezki (Sony) 		va->va_end = start + size;
3319ca23e405STejun Heo 	}
3320ca23e405STejun Heo 
3321e36176beSUladzislau Rezki (Sony) 	spin_unlock(&free_vmap_area_lock);
3322ca23e405STejun Heo 
3323253a496dSDaniel Axtens 	/* populate the kasan shadow space */
3324253a496dSDaniel Axtens 	for (area = 0; area < nr_vms; area++) {
3325253a496dSDaniel Axtens 		if (kasan_populate_vmalloc(vas[area]->va_start, sizes[area]))
3326253a496dSDaniel Axtens 			goto err_free_shadow;
3327253a496dSDaniel Axtens 
3328253a496dSDaniel Axtens 		kasan_unpoison_vmalloc((void *)vas[area]->va_start,
3329253a496dSDaniel Axtens 				       sizes[area]);
3330253a496dSDaniel Axtens 	}
3331253a496dSDaniel Axtens 
3332ca23e405STejun Heo 	/* insert all vm's */
3333e36176beSUladzislau Rezki (Sony) 	spin_lock(&vmap_area_lock);
3334e36176beSUladzislau Rezki (Sony) 	for (area = 0; area < nr_vms; area++) {
3335e36176beSUladzislau Rezki (Sony) 		insert_vmap_area(vas[area], &vmap_area_root, &vmap_area_list);
3336e36176beSUladzislau Rezki (Sony) 
3337e36176beSUladzislau Rezki (Sony) 		setup_vmalloc_vm_locked(vms[area], vas[area], VM_ALLOC,
3338ca23e405STejun Heo 				 pcpu_get_vm_areas);
3339e36176beSUladzislau Rezki (Sony) 	}
3340e36176beSUladzislau Rezki (Sony) 	spin_unlock(&vmap_area_lock);
3341ca23e405STejun Heo 
3342ca23e405STejun Heo 	kfree(vas);
3343ca23e405STejun Heo 	return vms;
3344ca23e405STejun Heo 
334568ad4a33SUladzislau Rezki (Sony) recovery:
3346e36176beSUladzislau Rezki (Sony) 	/*
3347e36176beSUladzislau Rezki (Sony) 	 * Remove previously allocated areas. There is no
3348e36176beSUladzislau Rezki (Sony) 	 * need in removing these areas from the busy tree,
3349e36176beSUladzislau Rezki (Sony) 	 * because they are inserted only on the final step
3350e36176beSUladzislau Rezki (Sony) 	 * and when pcpu_get_vm_areas() is success.
3351e36176beSUladzislau Rezki (Sony) 	 */
335268ad4a33SUladzislau Rezki (Sony) 	while (area--) {
3353253a496dSDaniel Axtens 		orig_start = vas[area]->va_start;
3354253a496dSDaniel Axtens 		orig_end = vas[area]->va_end;
3355253a496dSDaniel Axtens 		va = merge_or_add_vmap_area(vas[area], &free_vmap_area_root,
33563c5c3cfbSDaniel Axtens 					    &free_vmap_area_list);
33579c801f61SUladzislau Rezki (Sony) 		if (va)
3358253a496dSDaniel Axtens 			kasan_release_vmalloc(orig_start, orig_end,
3359253a496dSDaniel Axtens 				va->va_start, va->va_end);
336068ad4a33SUladzislau Rezki (Sony) 		vas[area] = NULL;
336168ad4a33SUladzislau Rezki (Sony) 	}
336268ad4a33SUladzislau Rezki (Sony) 
336368ad4a33SUladzislau Rezki (Sony) overflow:
3364e36176beSUladzislau Rezki (Sony) 	spin_unlock(&free_vmap_area_lock);
336568ad4a33SUladzislau Rezki (Sony) 	if (!purged) {
336668ad4a33SUladzislau Rezki (Sony) 		purge_vmap_area_lazy();
336768ad4a33SUladzislau Rezki (Sony) 		purged = true;
336868ad4a33SUladzislau Rezki (Sony) 
336968ad4a33SUladzislau Rezki (Sony) 		/* Before "retry", check if we recover. */
337068ad4a33SUladzislau Rezki (Sony) 		for (area = 0; area < nr_vms; area++) {
337168ad4a33SUladzislau Rezki (Sony) 			if (vas[area])
337268ad4a33SUladzislau Rezki (Sony) 				continue;
337368ad4a33SUladzislau Rezki (Sony) 
337468ad4a33SUladzislau Rezki (Sony) 			vas[area] = kmem_cache_zalloc(
337568ad4a33SUladzislau Rezki (Sony) 				vmap_area_cachep, GFP_KERNEL);
337668ad4a33SUladzislau Rezki (Sony) 			if (!vas[area])
337768ad4a33SUladzislau Rezki (Sony) 				goto err_free;
337868ad4a33SUladzislau Rezki (Sony) 		}
337968ad4a33SUladzislau Rezki (Sony) 
338068ad4a33SUladzislau Rezki (Sony) 		goto retry;
338168ad4a33SUladzislau Rezki (Sony) 	}
338268ad4a33SUladzislau Rezki (Sony) 
3383ca23e405STejun Heo err_free:
3384ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++) {
338568ad4a33SUladzislau Rezki (Sony) 		if (vas[area])
338668ad4a33SUladzislau Rezki (Sony) 			kmem_cache_free(vmap_area_cachep, vas[area]);
338768ad4a33SUladzislau Rezki (Sony) 
3388ca23e405STejun Heo 		kfree(vms[area]);
3389ca23e405STejun Heo 	}
3390f1db7afdSKautuk Consul err_free2:
3391ca23e405STejun Heo 	kfree(vas);
3392ca23e405STejun Heo 	kfree(vms);
3393ca23e405STejun Heo 	return NULL;
3394253a496dSDaniel Axtens 
3395253a496dSDaniel Axtens err_free_shadow:
3396253a496dSDaniel Axtens 	spin_lock(&free_vmap_area_lock);
3397253a496dSDaniel Axtens 	/*
3398253a496dSDaniel Axtens 	 * We release all the vmalloc shadows, even the ones for regions that
3399253a496dSDaniel Axtens 	 * hadn't been successfully added. This relies on kasan_release_vmalloc
3400253a496dSDaniel Axtens 	 * being able to tolerate this case.
3401253a496dSDaniel Axtens 	 */
3402253a496dSDaniel Axtens 	for (area = 0; area < nr_vms; area++) {
3403253a496dSDaniel Axtens 		orig_start = vas[area]->va_start;
3404253a496dSDaniel Axtens 		orig_end = vas[area]->va_end;
3405253a496dSDaniel Axtens 		va = merge_or_add_vmap_area(vas[area], &free_vmap_area_root,
3406253a496dSDaniel Axtens 					    &free_vmap_area_list);
34079c801f61SUladzislau Rezki (Sony) 		if (va)
3408253a496dSDaniel Axtens 			kasan_release_vmalloc(orig_start, orig_end,
3409253a496dSDaniel Axtens 				va->va_start, va->va_end);
3410253a496dSDaniel Axtens 		vas[area] = NULL;
3411253a496dSDaniel Axtens 		kfree(vms[area]);
3412253a496dSDaniel Axtens 	}
3413253a496dSDaniel Axtens 	spin_unlock(&free_vmap_area_lock);
3414253a496dSDaniel Axtens 	kfree(vas);
3415253a496dSDaniel Axtens 	kfree(vms);
3416253a496dSDaniel Axtens 	return NULL;
3417ca23e405STejun Heo }
3418ca23e405STejun Heo 
3419ca23e405STejun Heo /**
3420ca23e405STejun Heo  * pcpu_free_vm_areas - free vmalloc areas for percpu allocator
3421ca23e405STejun Heo  * @vms: vm_struct pointer array returned by pcpu_get_vm_areas()
3422ca23e405STejun Heo  * @nr_vms: the number of allocated areas
3423ca23e405STejun Heo  *
3424ca23e405STejun Heo  * Free vm_structs and the array allocated by pcpu_get_vm_areas().
3425ca23e405STejun Heo  */
3426ca23e405STejun Heo void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
3427ca23e405STejun Heo {
3428ca23e405STejun Heo 	int i;
3429ca23e405STejun Heo 
3430ca23e405STejun Heo 	for (i = 0; i < nr_vms; i++)
3431ca23e405STejun Heo 		free_vm_area(vms[i]);
3432ca23e405STejun Heo 	kfree(vms);
3433ca23e405STejun Heo }
34344f8b02b4STejun Heo #endif	/* CONFIG_SMP */
3435a10aa579SChristoph Lameter 
3436a10aa579SChristoph Lameter #ifdef CONFIG_PROC_FS
3437a10aa579SChristoph Lameter static void *s_start(struct seq_file *m, loff_t *pos)
3438e36176beSUladzislau Rezki (Sony) 	__acquires(&vmap_purge_lock)
3439d4033afdSJoonsoo Kim 	__acquires(&vmap_area_lock)
3440a10aa579SChristoph Lameter {
3441e36176beSUladzislau Rezki (Sony) 	mutex_lock(&vmap_purge_lock);
3442d4033afdSJoonsoo Kim 	spin_lock(&vmap_area_lock);
3443e36176beSUladzislau Rezki (Sony) 
34443f500069Szijun_hu 	return seq_list_start(&vmap_area_list, *pos);
3445a10aa579SChristoph Lameter }
3446a10aa579SChristoph Lameter 
3447a10aa579SChristoph Lameter static void *s_next(struct seq_file *m, void *p, loff_t *pos)
3448a10aa579SChristoph Lameter {
34493f500069Szijun_hu 	return seq_list_next(p, &vmap_area_list, pos);
3450a10aa579SChristoph Lameter }
3451a10aa579SChristoph Lameter 
3452a10aa579SChristoph Lameter static void s_stop(struct seq_file *m, void *p)
3453e36176beSUladzislau Rezki (Sony) 	__releases(&vmap_purge_lock)
3454d4033afdSJoonsoo Kim 	__releases(&vmap_area_lock)
3455a10aa579SChristoph Lameter {
3456e36176beSUladzislau Rezki (Sony) 	mutex_unlock(&vmap_purge_lock);
3457d4033afdSJoonsoo Kim 	spin_unlock(&vmap_area_lock);
3458a10aa579SChristoph Lameter }
3459a10aa579SChristoph Lameter 
3460a47a126aSEric Dumazet static void show_numa_info(struct seq_file *m, struct vm_struct *v)
3461a47a126aSEric Dumazet {
3462e5adfffcSKirill A. Shutemov 	if (IS_ENABLED(CONFIG_NUMA)) {
3463a47a126aSEric Dumazet 		unsigned int nr, *counters = m->private;
3464a47a126aSEric Dumazet 
3465a47a126aSEric Dumazet 		if (!counters)
3466a47a126aSEric Dumazet 			return;
3467a47a126aSEric Dumazet 
3468af12346cSWanpeng Li 		if (v->flags & VM_UNINITIALIZED)
3469af12346cSWanpeng Li 			return;
34707e5b528bSDmitry Vyukov 		/* Pair with smp_wmb() in clear_vm_uninitialized_flag() */
34717e5b528bSDmitry Vyukov 		smp_rmb();
3472af12346cSWanpeng Li 
3473a47a126aSEric Dumazet 		memset(counters, 0, nr_node_ids * sizeof(unsigned int));
3474a47a126aSEric Dumazet 
3475a47a126aSEric Dumazet 		for (nr = 0; nr < v->nr_pages; nr++)
3476a47a126aSEric Dumazet 			counters[page_to_nid(v->pages[nr])]++;
3477a47a126aSEric Dumazet 
3478a47a126aSEric Dumazet 		for_each_node_state(nr, N_HIGH_MEMORY)
3479a47a126aSEric Dumazet 			if (counters[nr])
3480a47a126aSEric Dumazet 				seq_printf(m, " N%u=%u", nr, counters[nr]);
3481a47a126aSEric Dumazet 	}
3482a47a126aSEric Dumazet }
3483a47a126aSEric Dumazet 
3484dd3b8353SUladzislau Rezki (Sony) static void show_purge_info(struct seq_file *m)
3485dd3b8353SUladzislau Rezki (Sony) {
3486dd3b8353SUladzislau Rezki (Sony) 	struct llist_node *head;
3487dd3b8353SUladzislau Rezki (Sony) 	struct vmap_area *va;
3488dd3b8353SUladzislau Rezki (Sony) 
3489dd3b8353SUladzislau Rezki (Sony) 	head = READ_ONCE(vmap_purge_list.first);
3490dd3b8353SUladzislau Rezki (Sony) 	if (head == NULL)
3491dd3b8353SUladzislau Rezki (Sony) 		return;
3492dd3b8353SUladzislau Rezki (Sony) 
3493dd3b8353SUladzislau Rezki (Sony) 	llist_for_each_entry(va, head, purge_list) {
3494dd3b8353SUladzislau Rezki (Sony) 		seq_printf(m, "0x%pK-0x%pK %7ld unpurged vm_area\n",
3495dd3b8353SUladzislau Rezki (Sony) 			(void *)va->va_start, (void *)va->va_end,
3496dd3b8353SUladzislau Rezki (Sony) 			va->va_end - va->va_start);
3497dd3b8353SUladzislau Rezki (Sony) 	}
3498dd3b8353SUladzislau Rezki (Sony) }
3499dd3b8353SUladzislau Rezki (Sony) 
3500a10aa579SChristoph Lameter static int s_show(struct seq_file *m, void *p)
3501a10aa579SChristoph Lameter {
35023f500069Szijun_hu 	struct vmap_area *va;
3503d4033afdSJoonsoo Kim 	struct vm_struct *v;
3504d4033afdSJoonsoo Kim 
35053f500069Szijun_hu 	va = list_entry(p, struct vmap_area, list);
35063f500069Szijun_hu 
3507c2ce8c14SWanpeng Li 	/*
3508688fcbfcSPengfei Li 	 * s_show can encounter race with remove_vm_area, !vm on behalf
3509688fcbfcSPengfei Li 	 * of vmap area is being tear down or vm_map_ram allocation.
3510c2ce8c14SWanpeng Li 	 */
3511688fcbfcSPengfei Li 	if (!va->vm) {
3512dd3b8353SUladzislau Rezki (Sony) 		seq_printf(m, "0x%pK-0x%pK %7ld vm_map_ram\n",
351378c72746SYisheng Xie 			(void *)va->va_start, (void *)va->va_end,
3514dd3b8353SUladzislau Rezki (Sony) 			va->va_end - va->va_start);
351578c72746SYisheng Xie 
3516d4033afdSJoonsoo Kim 		return 0;
351778c72746SYisheng Xie 	}
3518d4033afdSJoonsoo Kim 
3519d4033afdSJoonsoo Kim 	v = va->vm;
3520a10aa579SChristoph Lameter 
352145ec1690SKees Cook 	seq_printf(m, "0x%pK-0x%pK %7ld",
3522a10aa579SChristoph Lameter 		v->addr, v->addr + v->size, v->size);
3523a10aa579SChristoph Lameter 
352462c70bceSJoe Perches 	if (v->caller)
352562c70bceSJoe Perches 		seq_printf(m, " %pS", v->caller);
352623016969SChristoph Lameter 
3527a10aa579SChristoph Lameter 	if (v->nr_pages)
3528a10aa579SChristoph Lameter 		seq_printf(m, " pages=%d", v->nr_pages);
3529a10aa579SChristoph Lameter 
3530a10aa579SChristoph Lameter 	if (v->phys_addr)
3531199eaa05SMiles Chen 		seq_printf(m, " phys=%pa", &v->phys_addr);
3532a10aa579SChristoph Lameter 
3533a10aa579SChristoph Lameter 	if (v->flags & VM_IOREMAP)
3534f4527c90SFabian Frederick 		seq_puts(m, " ioremap");
3535a10aa579SChristoph Lameter 
3536a10aa579SChristoph Lameter 	if (v->flags & VM_ALLOC)
3537f4527c90SFabian Frederick 		seq_puts(m, " vmalloc");
3538a10aa579SChristoph Lameter 
3539a10aa579SChristoph Lameter 	if (v->flags & VM_MAP)
3540f4527c90SFabian Frederick 		seq_puts(m, " vmap");
3541a10aa579SChristoph Lameter 
3542a10aa579SChristoph Lameter 	if (v->flags & VM_USERMAP)
3543f4527c90SFabian Frederick 		seq_puts(m, " user");
3544a10aa579SChristoph Lameter 
3545fe9041c2SChristoph Hellwig 	if (v->flags & VM_DMA_COHERENT)
3546fe9041c2SChristoph Hellwig 		seq_puts(m, " dma-coherent");
3547fe9041c2SChristoph Hellwig 
3548244d63eeSDavid Rientjes 	if (is_vmalloc_addr(v->pages))
3549f4527c90SFabian Frederick 		seq_puts(m, " vpages");
3550a10aa579SChristoph Lameter 
3551a47a126aSEric Dumazet 	show_numa_info(m, v);
3552a10aa579SChristoph Lameter 	seq_putc(m, '\n');
3553dd3b8353SUladzislau Rezki (Sony) 
3554dd3b8353SUladzislau Rezki (Sony) 	/*
3555dd3b8353SUladzislau Rezki (Sony) 	 * As a final step, dump "unpurged" areas. Note,
3556dd3b8353SUladzislau Rezki (Sony) 	 * that entire "/proc/vmallocinfo" output will not
3557dd3b8353SUladzislau Rezki (Sony) 	 * be address sorted, because the purge list is not
3558dd3b8353SUladzislau Rezki (Sony) 	 * sorted.
3559dd3b8353SUladzislau Rezki (Sony) 	 */
3560dd3b8353SUladzislau Rezki (Sony) 	if (list_is_last(&va->list, &vmap_area_list))
3561dd3b8353SUladzislau Rezki (Sony) 		show_purge_info(m);
3562dd3b8353SUladzislau Rezki (Sony) 
3563a10aa579SChristoph Lameter 	return 0;
3564a10aa579SChristoph Lameter }
3565a10aa579SChristoph Lameter 
35665f6a6a9cSAlexey Dobriyan static const struct seq_operations vmalloc_op = {
3567a10aa579SChristoph Lameter 	.start = s_start,
3568a10aa579SChristoph Lameter 	.next = s_next,
3569a10aa579SChristoph Lameter 	.stop = s_stop,
3570a10aa579SChristoph Lameter 	.show = s_show,
3571a10aa579SChristoph Lameter };
35725f6a6a9cSAlexey Dobriyan 
35735f6a6a9cSAlexey Dobriyan static int __init proc_vmalloc_init(void)
35745f6a6a9cSAlexey Dobriyan {
3575fddda2b7SChristoph Hellwig 	if (IS_ENABLED(CONFIG_NUMA))
35760825a6f9SJoe Perches 		proc_create_seq_private("vmallocinfo", 0400, NULL,
357744414d82SChristoph Hellwig 				&vmalloc_op,
357844414d82SChristoph Hellwig 				nr_node_ids * sizeof(unsigned int), NULL);
3579fddda2b7SChristoph Hellwig 	else
35800825a6f9SJoe Perches 		proc_create_seq("vmallocinfo", 0400, NULL, &vmalloc_op);
35815f6a6a9cSAlexey Dobriyan 	return 0;
35825f6a6a9cSAlexey Dobriyan }
35835f6a6a9cSAlexey Dobriyan module_init(proc_vmalloc_init);
3584db3808c1SJoonsoo Kim 
3585a10aa579SChristoph Lameter #endif
3586