xref: /openbmc/linux/mm/vmalloc.c (revision f608788cd2d6cae27d1a3d2253544ca76b353764)
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);
41689699605SNick Piggin static struct rb_root vmap_area_root = RB_ROOT;
41768ad4a33SUladzislau Rezki (Sony) static bool vmap_initialized __read_mostly;
41889699605SNick Piggin 
41996e2db45SUladzislau Rezki (Sony) static struct rb_root purge_vmap_area_root = RB_ROOT;
42096e2db45SUladzislau Rezki (Sony) static LIST_HEAD(purge_vmap_area_list);
42196e2db45SUladzislau Rezki (Sony) static DEFINE_SPINLOCK(purge_vmap_area_lock);
42296e2db45SUladzislau Rezki (Sony) 
42368ad4a33SUladzislau Rezki (Sony) /*
42468ad4a33SUladzislau Rezki (Sony)  * This kmem_cache is used for vmap_area objects. Instead of
42568ad4a33SUladzislau Rezki (Sony)  * allocating from slab we reuse an object from this cache to
42668ad4a33SUladzislau Rezki (Sony)  * make things faster. Especially in "no edge" splitting of
42768ad4a33SUladzislau Rezki (Sony)  * free block.
42868ad4a33SUladzislau Rezki (Sony)  */
42968ad4a33SUladzislau Rezki (Sony) static struct kmem_cache *vmap_area_cachep;
43089699605SNick Piggin 
43168ad4a33SUladzislau Rezki (Sony) /*
43268ad4a33SUladzislau Rezki (Sony)  * This linked list is used in pair with free_vmap_area_root.
43368ad4a33SUladzislau Rezki (Sony)  * It gives O(1) access to prev/next to perform fast coalescing.
43468ad4a33SUladzislau Rezki (Sony)  */
43568ad4a33SUladzislau Rezki (Sony) static LIST_HEAD(free_vmap_area_list);
43668ad4a33SUladzislau Rezki (Sony) 
43768ad4a33SUladzislau Rezki (Sony) /*
43868ad4a33SUladzislau Rezki (Sony)  * This augment red-black tree represents the free vmap space.
43968ad4a33SUladzislau Rezki (Sony)  * All vmap_area objects in this tree are sorted by va->va_start
44068ad4a33SUladzislau Rezki (Sony)  * address. It is used for allocation and merging when a vmap
44168ad4a33SUladzislau Rezki (Sony)  * object is released.
44268ad4a33SUladzislau Rezki (Sony)  *
44368ad4a33SUladzislau Rezki (Sony)  * Each vmap_area node contains a maximum available free block
44468ad4a33SUladzislau Rezki (Sony)  * of its sub-tree, right or left. Therefore it is possible to
44568ad4a33SUladzislau Rezki (Sony)  * find a lowest match of free area.
44668ad4a33SUladzislau Rezki (Sony)  */
44768ad4a33SUladzislau Rezki (Sony) static struct rb_root free_vmap_area_root = RB_ROOT;
44868ad4a33SUladzislau Rezki (Sony) 
44982dd23e8SUladzislau Rezki (Sony) /*
45082dd23e8SUladzislau Rezki (Sony)  * Preload a CPU with one object for "no edge" split case. The
45182dd23e8SUladzislau Rezki (Sony)  * aim is to get rid of allocations from the atomic context, thus
45282dd23e8SUladzislau Rezki (Sony)  * to use more permissive allocation masks.
45382dd23e8SUladzislau Rezki (Sony)  */
45482dd23e8SUladzislau Rezki (Sony) static DEFINE_PER_CPU(struct vmap_area *, ne_fit_preload_node);
45582dd23e8SUladzislau Rezki (Sony) 
45668ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long
45768ad4a33SUladzislau Rezki (Sony) va_size(struct vmap_area *va)
45868ad4a33SUladzislau Rezki (Sony) {
45968ad4a33SUladzislau Rezki (Sony) 	return (va->va_end - va->va_start);
46068ad4a33SUladzislau Rezki (Sony) }
46168ad4a33SUladzislau Rezki (Sony) 
46268ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long
46368ad4a33SUladzislau Rezki (Sony) get_subtree_max_size(struct rb_node *node)
46468ad4a33SUladzislau Rezki (Sony) {
46568ad4a33SUladzislau Rezki (Sony) 	struct vmap_area *va;
46668ad4a33SUladzislau Rezki (Sony) 
46768ad4a33SUladzislau Rezki (Sony) 	va = rb_entry_safe(node, struct vmap_area, rb_node);
46868ad4a33SUladzislau Rezki (Sony) 	return va ? va->subtree_max_size : 0;
46968ad4a33SUladzislau Rezki (Sony) }
47068ad4a33SUladzislau Rezki (Sony) 
47168ad4a33SUladzislau Rezki (Sony) /*
47268ad4a33SUladzislau Rezki (Sony)  * Gets called when remove the node and rotate.
47368ad4a33SUladzislau Rezki (Sony)  */
47468ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long
47568ad4a33SUladzislau Rezki (Sony) compute_subtree_max_size(struct vmap_area *va)
47668ad4a33SUladzislau Rezki (Sony) {
47768ad4a33SUladzislau Rezki (Sony) 	return max3(va_size(va),
47868ad4a33SUladzislau Rezki (Sony) 		get_subtree_max_size(va->rb_node.rb_left),
47968ad4a33SUladzislau Rezki (Sony) 		get_subtree_max_size(va->rb_node.rb_right));
48068ad4a33SUladzislau Rezki (Sony) }
48168ad4a33SUladzislau Rezki (Sony) 
482315cc066SMichel Lespinasse RB_DECLARE_CALLBACKS_MAX(static, free_vmap_area_rb_augment_cb,
483315cc066SMichel Lespinasse 	struct vmap_area, rb_node, unsigned long, subtree_max_size, va_size)
48468ad4a33SUladzislau Rezki (Sony) 
48568ad4a33SUladzislau Rezki (Sony) static void purge_vmap_area_lazy(void);
48668ad4a33SUladzislau Rezki (Sony) static BLOCKING_NOTIFIER_HEAD(vmap_notify_list);
48768ad4a33SUladzislau Rezki (Sony) static unsigned long lazy_max_pages(void);
488db64fe02SNick Piggin 
48997105f0aSRoman Gushchin static atomic_long_t nr_vmalloc_pages;
49097105f0aSRoman Gushchin 
49197105f0aSRoman Gushchin unsigned long vmalloc_nr_pages(void)
49297105f0aSRoman Gushchin {
49397105f0aSRoman Gushchin 	return atomic_long_read(&nr_vmalloc_pages);
49497105f0aSRoman Gushchin }
49597105f0aSRoman Gushchin 
496db64fe02SNick Piggin static struct vmap_area *__find_vmap_area(unsigned long addr)
4971da177e4SLinus Torvalds {
498db64fe02SNick Piggin 	struct rb_node *n = vmap_area_root.rb_node;
499db64fe02SNick Piggin 
500db64fe02SNick Piggin 	while (n) {
501db64fe02SNick Piggin 		struct vmap_area *va;
502db64fe02SNick Piggin 
503db64fe02SNick Piggin 		va = rb_entry(n, struct vmap_area, rb_node);
504db64fe02SNick Piggin 		if (addr < va->va_start)
505db64fe02SNick Piggin 			n = n->rb_left;
506cef2ac3fSHATAYAMA Daisuke 		else if (addr >= va->va_end)
507db64fe02SNick Piggin 			n = n->rb_right;
508db64fe02SNick Piggin 		else
509db64fe02SNick Piggin 			return va;
510db64fe02SNick Piggin 	}
511db64fe02SNick Piggin 
512db64fe02SNick Piggin 	return NULL;
513db64fe02SNick Piggin }
514db64fe02SNick Piggin 
51568ad4a33SUladzislau Rezki (Sony) /*
51668ad4a33SUladzislau Rezki (Sony)  * This function returns back addresses of parent node
51768ad4a33SUladzislau Rezki (Sony)  * and its left or right link for further processing.
5189c801f61SUladzislau Rezki (Sony)  *
5199c801f61SUladzislau Rezki (Sony)  * Otherwise NULL is returned. In that case all further
5209c801f61SUladzislau Rezki (Sony)  * steps regarding inserting of conflicting overlap range
5219c801f61SUladzislau Rezki (Sony)  * have to be declined and actually considered as a bug.
52268ad4a33SUladzislau Rezki (Sony)  */
52368ad4a33SUladzislau Rezki (Sony) static __always_inline struct rb_node **
52468ad4a33SUladzislau Rezki (Sony) find_va_links(struct vmap_area *va,
52568ad4a33SUladzislau Rezki (Sony) 	struct rb_root *root, struct rb_node *from,
52668ad4a33SUladzislau Rezki (Sony) 	struct rb_node **parent)
527db64fe02SNick Piggin {
528170168d0SNamhyung Kim 	struct vmap_area *tmp_va;
52968ad4a33SUladzislau Rezki (Sony) 	struct rb_node **link;
530db64fe02SNick Piggin 
53168ad4a33SUladzislau Rezki (Sony) 	if (root) {
53268ad4a33SUladzislau Rezki (Sony) 		link = &root->rb_node;
53368ad4a33SUladzislau Rezki (Sony) 		if (unlikely(!*link)) {
53468ad4a33SUladzislau Rezki (Sony) 			*parent = NULL;
53568ad4a33SUladzislau Rezki (Sony) 			return link;
53668ad4a33SUladzislau Rezki (Sony) 		}
53768ad4a33SUladzislau Rezki (Sony) 	} else {
53868ad4a33SUladzislau Rezki (Sony) 		link = &from;
53968ad4a33SUladzislau Rezki (Sony) 	}
54068ad4a33SUladzislau Rezki (Sony) 
54168ad4a33SUladzislau Rezki (Sony) 	/*
54268ad4a33SUladzislau Rezki (Sony) 	 * Go to the bottom of the tree. When we hit the last point
54368ad4a33SUladzislau Rezki (Sony) 	 * we end up with parent rb_node and correct direction, i name
54468ad4a33SUladzislau Rezki (Sony) 	 * it link, where the new va->rb_node will be attached to.
54568ad4a33SUladzislau Rezki (Sony) 	 */
54668ad4a33SUladzislau Rezki (Sony) 	do {
54768ad4a33SUladzislau Rezki (Sony) 		tmp_va = rb_entry(*link, struct vmap_area, rb_node);
54868ad4a33SUladzislau Rezki (Sony) 
54968ad4a33SUladzislau Rezki (Sony) 		/*
55068ad4a33SUladzislau Rezki (Sony) 		 * During the traversal we also do some sanity check.
55168ad4a33SUladzislau Rezki (Sony) 		 * Trigger the BUG() if there are sides(left/right)
55268ad4a33SUladzislau Rezki (Sony) 		 * or full overlaps.
55368ad4a33SUladzislau Rezki (Sony) 		 */
55468ad4a33SUladzislau Rezki (Sony) 		if (va->va_start < tmp_va->va_end &&
55568ad4a33SUladzislau Rezki (Sony) 				va->va_end <= tmp_va->va_start)
55668ad4a33SUladzislau Rezki (Sony) 			link = &(*link)->rb_left;
55768ad4a33SUladzislau Rezki (Sony) 		else if (va->va_end > tmp_va->va_start &&
55868ad4a33SUladzislau Rezki (Sony) 				va->va_start >= tmp_va->va_end)
55968ad4a33SUladzislau Rezki (Sony) 			link = &(*link)->rb_right;
5609c801f61SUladzislau Rezki (Sony) 		else {
5619c801f61SUladzislau Rezki (Sony) 			WARN(1, "vmalloc bug: 0x%lx-0x%lx overlaps with 0x%lx-0x%lx\n",
5629c801f61SUladzislau Rezki (Sony) 				va->va_start, va->va_end, tmp_va->va_start, tmp_va->va_end);
5639c801f61SUladzislau Rezki (Sony) 
5649c801f61SUladzislau Rezki (Sony) 			return NULL;
5659c801f61SUladzislau Rezki (Sony) 		}
56668ad4a33SUladzislau Rezki (Sony) 	} while (*link);
56768ad4a33SUladzislau Rezki (Sony) 
56868ad4a33SUladzislau Rezki (Sony) 	*parent = &tmp_va->rb_node;
56968ad4a33SUladzislau Rezki (Sony) 	return link;
570db64fe02SNick Piggin }
571db64fe02SNick Piggin 
57268ad4a33SUladzislau Rezki (Sony) static __always_inline struct list_head *
57368ad4a33SUladzislau Rezki (Sony) get_va_next_sibling(struct rb_node *parent, struct rb_node **link)
57468ad4a33SUladzislau Rezki (Sony) {
57568ad4a33SUladzislau Rezki (Sony) 	struct list_head *list;
576db64fe02SNick Piggin 
57768ad4a33SUladzislau Rezki (Sony) 	if (unlikely(!parent))
57868ad4a33SUladzislau Rezki (Sony) 		/*
57968ad4a33SUladzislau Rezki (Sony) 		 * The red-black tree where we try to find VA neighbors
58068ad4a33SUladzislau Rezki (Sony) 		 * before merging or inserting is empty, i.e. it means
58168ad4a33SUladzislau Rezki (Sony) 		 * there is no free vmap space. Normally it does not
58268ad4a33SUladzislau Rezki (Sony) 		 * happen but we handle this case anyway.
58368ad4a33SUladzislau Rezki (Sony) 		 */
58468ad4a33SUladzislau Rezki (Sony) 		return NULL;
58568ad4a33SUladzislau Rezki (Sony) 
58668ad4a33SUladzislau Rezki (Sony) 	list = &rb_entry(parent, struct vmap_area, rb_node)->list;
58768ad4a33SUladzislau Rezki (Sony) 	return (&parent->rb_right == link ? list->next : list);
588db64fe02SNick Piggin }
589db64fe02SNick Piggin 
59068ad4a33SUladzislau Rezki (Sony) static __always_inline void
59168ad4a33SUladzislau Rezki (Sony) link_va(struct vmap_area *va, struct rb_root *root,
59268ad4a33SUladzislau Rezki (Sony) 	struct rb_node *parent, struct rb_node **link, struct list_head *head)
59368ad4a33SUladzislau Rezki (Sony) {
59468ad4a33SUladzislau Rezki (Sony) 	/*
59568ad4a33SUladzislau Rezki (Sony) 	 * VA is still not in the list, but we can
59668ad4a33SUladzislau Rezki (Sony) 	 * identify its future previous list_head node.
59768ad4a33SUladzislau Rezki (Sony) 	 */
59868ad4a33SUladzislau Rezki (Sony) 	if (likely(parent)) {
59968ad4a33SUladzislau Rezki (Sony) 		head = &rb_entry(parent, struct vmap_area, rb_node)->list;
60068ad4a33SUladzislau Rezki (Sony) 		if (&parent->rb_right != link)
60168ad4a33SUladzislau Rezki (Sony) 			head = head->prev;
60268ad4a33SUladzislau Rezki (Sony) 	}
603db64fe02SNick Piggin 
60468ad4a33SUladzislau Rezki (Sony) 	/* Insert to the rb-tree */
60568ad4a33SUladzislau Rezki (Sony) 	rb_link_node(&va->rb_node, parent, link);
60668ad4a33SUladzislau Rezki (Sony) 	if (root == &free_vmap_area_root) {
60768ad4a33SUladzislau Rezki (Sony) 		/*
60868ad4a33SUladzislau Rezki (Sony) 		 * Some explanation here. Just perform simple insertion
60968ad4a33SUladzislau Rezki (Sony) 		 * to the tree. We do not set va->subtree_max_size to
61068ad4a33SUladzislau Rezki (Sony) 		 * its current size before calling rb_insert_augmented().
61168ad4a33SUladzislau Rezki (Sony) 		 * It is because of we populate the tree from the bottom
61268ad4a33SUladzislau Rezki (Sony) 		 * to parent levels when the node _is_ in the tree.
61368ad4a33SUladzislau Rezki (Sony) 		 *
61468ad4a33SUladzislau Rezki (Sony) 		 * Therefore we set subtree_max_size to zero after insertion,
61568ad4a33SUladzislau Rezki (Sony) 		 * to let __augment_tree_propagate_from() puts everything to
61668ad4a33SUladzislau Rezki (Sony) 		 * the correct order later on.
61768ad4a33SUladzislau Rezki (Sony) 		 */
61868ad4a33SUladzislau Rezki (Sony) 		rb_insert_augmented(&va->rb_node,
61968ad4a33SUladzislau Rezki (Sony) 			root, &free_vmap_area_rb_augment_cb);
62068ad4a33SUladzislau Rezki (Sony) 		va->subtree_max_size = 0;
62168ad4a33SUladzislau Rezki (Sony) 	} else {
62268ad4a33SUladzislau Rezki (Sony) 		rb_insert_color(&va->rb_node, root);
62368ad4a33SUladzislau Rezki (Sony) 	}
62468ad4a33SUladzislau Rezki (Sony) 
62568ad4a33SUladzislau Rezki (Sony) 	/* Address-sort this list */
62668ad4a33SUladzislau Rezki (Sony) 	list_add(&va->list, head);
62768ad4a33SUladzislau Rezki (Sony) }
62868ad4a33SUladzislau Rezki (Sony) 
62968ad4a33SUladzislau Rezki (Sony) static __always_inline void
63068ad4a33SUladzislau Rezki (Sony) unlink_va(struct vmap_area *va, struct rb_root *root)
63168ad4a33SUladzislau Rezki (Sony) {
632460e42d1SUladzislau Rezki (Sony) 	if (WARN_ON(RB_EMPTY_NODE(&va->rb_node)))
633460e42d1SUladzislau Rezki (Sony) 		return;
634460e42d1SUladzislau Rezki (Sony) 
63568ad4a33SUladzislau Rezki (Sony) 	if (root == &free_vmap_area_root)
63668ad4a33SUladzislau Rezki (Sony) 		rb_erase_augmented(&va->rb_node,
63768ad4a33SUladzislau Rezki (Sony) 			root, &free_vmap_area_rb_augment_cb);
63868ad4a33SUladzislau Rezki (Sony) 	else
63968ad4a33SUladzislau Rezki (Sony) 		rb_erase(&va->rb_node, root);
64068ad4a33SUladzislau Rezki (Sony) 
64168ad4a33SUladzislau Rezki (Sony) 	list_del(&va->list);
64268ad4a33SUladzislau Rezki (Sony) 	RB_CLEAR_NODE(&va->rb_node);
64368ad4a33SUladzislau Rezki (Sony) }
64468ad4a33SUladzislau Rezki (Sony) 
645bb850f4dSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_PROPAGATE_CHECK
646bb850f4dSUladzislau Rezki (Sony) static void
647da27c9edSUladzislau Rezki (Sony) augment_tree_propagate_check(void)
648bb850f4dSUladzislau Rezki (Sony) {
649bb850f4dSUladzislau Rezki (Sony) 	struct vmap_area *va;
650da27c9edSUladzislau Rezki (Sony) 	unsigned long computed_size;
651bb850f4dSUladzislau Rezki (Sony) 
652da27c9edSUladzislau Rezki (Sony) 	list_for_each_entry(va, &free_vmap_area_list, list) {
653da27c9edSUladzislau Rezki (Sony) 		computed_size = compute_subtree_max_size(va);
654da27c9edSUladzislau Rezki (Sony) 		if (computed_size != va->subtree_max_size)
655bb850f4dSUladzislau Rezki (Sony) 			pr_emerg("tree is corrupted: %lu, %lu\n",
656bb850f4dSUladzislau Rezki (Sony) 				va_size(va), va->subtree_max_size);
657bb850f4dSUladzislau Rezki (Sony) 	}
658bb850f4dSUladzislau Rezki (Sony) }
659bb850f4dSUladzislau Rezki (Sony) #endif
660bb850f4dSUladzislau Rezki (Sony) 
66168ad4a33SUladzislau Rezki (Sony) /*
66268ad4a33SUladzislau Rezki (Sony)  * This function populates subtree_max_size from bottom to upper
66368ad4a33SUladzislau Rezki (Sony)  * levels starting from VA point. The propagation must be done
66468ad4a33SUladzislau Rezki (Sony)  * when VA size is modified by changing its va_start/va_end. Or
66568ad4a33SUladzislau Rezki (Sony)  * in case of newly inserting of VA to the tree.
66668ad4a33SUladzislau Rezki (Sony)  *
66768ad4a33SUladzislau Rezki (Sony)  * It means that __augment_tree_propagate_from() must be called:
66868ad4a33SUladzislau Rezki (Sony)  * - After VA has been inserted to the tree(free path);
66968ad4a33SUladzislau Rezki (Sony)  * - After VA has been shrunk(allocation path);
67068ad4a33SUladzislau Rezki (Sony)  * - After VA has been increased(merging path).
67168ad4a33SUladzislau Rezki (Sony)  *
67268ad4a33SUladzislau Rezki (Sony)  * Please note that, it does not mean that upper parent nodes
67368ad4a33SUladzislau Rezki (Sony)  * and their subtree_max_size are recalculated all the time up
67468ad4a33SUladzislau Rezki (Sony)  * to the root node.
67568ad4a33SUladzislau Rezki (Sony)  *
67668ad4a33SUladzislau Rezki (Sony)  *       4--8
67768ad4a33SUladzislau Rezki (Sony)  *        /\
67868ad4a33SUladzislau Rezki (Sony)  *       /  \
67968ad4a33SUladzislau Rezki (Sony)  *      /    \
68068ad4a33SUladzislau Rezki (Sony)  *    2--2  8--8
68168ad4a33SUladzislau Rezki (Sony)  *
68268ad4a33SUladzislau Rezki (Sony)  * For example if we modify the node 4, shrinking it to 2, then
68368ad4a33SUladzislau Rezki (Sony)  * no any modification is required. If we shrink the node 2 to 1
68468ad4a33SUladzislau Rezki (Sony)  * its subtree_max_size is updated only, and set to 1. If we shrink
68568ad4a33SUladzislau Rezki (Sony)  * the node 8 to 6, then its subtree_max_size is set to 6 and parent
68668ad4a33SUladzislau Rezki (Sony)  * node becomes 4--6.
68768ad4a33SUladzislau Rezki (Sony)  */
68868ad4a33SUladzislau Rezki (Sony) static __always_inline void
68968ad4a33SUladzislau Rezki (Sony) augment_tree_propagate_from(struct vmap_area *va)
69068ad4a33SUladzislau Rezki (Sony) {
69168ad4a33SUladzislau Rezki (Sony) 	/*
69215ae144fSUladzislau Rezki (Sony) 	 * Populate the tree from bottom towards the root until
69315ae144fSUladzislau Rezki (Sony) 	 * the calculated maximum available size of checked node
69415ae144fSUladzislau Rezki (Sony) 	 * is equal to its current one.
69568ad4a33SUladzislau Rezki (Sony) 	 */
69615ae144fSUladzislau Rezki (Sony) 	free_vmap_area_rb_augment_cb_propagate(&va->rb_node, NULL);
697bb850f4dSUladzislau Rezki (Sony) 
698bb850f4dSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_PROPAGATE_CHECK
699da27c9edSUladzislau Rezki (Sony) 	augment_tree_propagate_check();
700bb850f4dSUladzislau Rezki (Sony) #endif
70168ad4a33SUladzislau Rezki (Sony) }
70268ad4a33SUladzislau Rezki (Sony) 
70368ad4a33SUladzislau Rezki (Sony) static void
70468ad4a33SUladzislau Rezki (Sony) insert_vmap_area(struct vmap_area *va,
70568ad4a33SUladzislau Rezki (Sony) 	struct rb_root *root, struct list_head *head)
70668ad4a33SUladzislau Rezki (Sony) {
70768ad4a33SUladzislau Rezki (Sony) 	struct rb_node **link;
70868ad4a33SUladzislau Rezki (Sony) 	struct rb_node *parent;
70968ad4a33SUladzislau Rezki (Sony) 
71068ad4a33SUladzislau Rezki (Sony) 	link = find_va_links(va, root, NULL, &parent);
7119c801f61SUladzislau Rezki (Sony) 	if (link)
71268ad4a33SUladzislau Rezki (Sony) 		link_va(va, root, parent, link, head);
71368ad4a33SUladzislau Rezki (Sony) }
71468ad4a33SUladzislau Rezki (Sony) 
71568ad4a33SUladzislau Rezki (Sony) static void
71668ad4a33SUladzislau Rezki (Sony) insert_vmap_area_augment(struct vmap_area *va,
71768ad4a33SUladzislau Rezki (Sony) 	struct rb_node *from, struct rb_root *root,
71868ad4a33SUladzislau Rezki (Sony) 	struct list_head *head)
71968ad4a33SUladzislau Rezki (Sony) {
72068ad4a33SUladzislau Rezki (Sony) 	struct rb_node **link;
72168ad4a33SUladzislau Rezki (Sony) 	struct rb_node *parent;
72268ad4a33SUladzislau Rezki (Sony) 
72368ad4a33SUladzislau Rezki (Sony) 	if (from)
72468ad4a33SUladzislau Rezki (Sony) 		link = find_va_links(va, NULL, from, &parent);
72568ad4a33SUladzislau Rezki (Sony) 	else
72668ad4a33SUladzislau Rezki (Sony) 		link = find_va_links(va, root, NULL, &parent);
72768ad4a33SUladzislau Rezki (Sony) 
7289c801f61SUladzislau Rezki (Sony) 	if (link) {
72968ad4a33SUladzislau Rezki (Sony) 		link_va(va, root, parent, link, head);
73068ad4a33SUladzislau Rezki (Sony) 		augment_tree_propagate_from(va);
73168ad4a33SUladzislau Rezki (Sony) 	}
7329c801f61SUladzislau Rezki (Sony) }
73368ad4a33SUladzislau Rezki (Sony) 
73468ad4a33SUladzislau Rezki (Sony) /*
73568ad4a33SUladzislau Rezki (Sony)  * Merge de-allocated chunk of VA memory with previous
73668ad4a33SUladzislau Rezki (Sony)  * and next free blocks. If coalesce is not done a new
73768ad4a33SUladzislau Rezki (Sony)  * free area is inserted. If VA has been merged, it is
73868ad4a33SUladzislau Rezki (Sony)  * freed.
7399c801f61SUladzislau Rezki (Sony)  *
7409c801f61SUladzislau Rezki (Sony)  * Please note, it can return NULL in case of overlap
7419c801f61SUladzislau Rezki (Sony)  * ranges, followed by WARN() report. Despite it is a
7429c801f61SUladzislau Rezki (Sony)  * buggy behaviour, a system can be alive and keep
7439c801f61SUladzislau Rezki (Sony)  * ongoing.
74468ad4a33SUladzislau Rezki (Sony)  */
7453c5c3cfbSDaniel Axtens static __always_inline struct vmap_area *
74668ad4a33SUladzislau Rezki (Sony) merge_or_add_vmap_area(struct vmap_area *va,
74768ad4a33SUladzislau Rezki (Sony) 	struct rb_root *root, struct list_head *head)
74868ad4a33SUladzislau Rezki (Sony) {
74968ad4a33SUladzislau Rezki (Sony) 	struct vmap_area *sibling;
75068ad4a33SUladzislau Rezki (Sony) 	struct list_head *next;
75168ad4a33SUladzislau Rezki (Sony) 	struct rb_node **link;
75268ad4a33SUladzislau Rezki (Sony) 	struct rb_node *parent;
75368ad4a33SUladzislau Rezki (Sony) 	bool merged = false;
75468ad4a33SUladzislau Rezki (Sony) 
75568ad4a33SUladzislau Rezki (Sony) 	/*
75668ad4a33SUladzislau Rezki (Sony) 	 * Find a place in the tree where VA potentially will be
75768ad4a33SUladzislau Rezki (Sony) 	 * inserted, unless it is merged with its sibling/siblings.
75868ad4a33SUladzislau Rezki (Sony) 	 */
75968ad4a33SUladzislau Rezki (Sony) 	link = find_va_links(va, root, NULL, &parent);
7609c801f61SUladzislau Rezki (Sony) 	if (!link)
7619c801f61SUladzislau Rezki (Sony) 		return NULL;
76268ad4a33SUladzislau Rezki (Sony) 
76368ad4a33SUladzislau Rezki (Sony) 	/*
76468ad4a33SUladzislau Rezki (Sony) 	 * Get next node of VA to check if merging can be done.
76568ad4a33SUladzislau Rezki (Sony) 	 */
76668ad4a33SUladzislau Rezki (Sony) 	next = get_va_next_sibling(parent, link);
76768ad4a33SUladzislau Rezki (Sony) 	if (unlikely(next == NULL))
76868ad4a33SUladzislau Rezki (Sony) 		goto insert;
76968ad4a33SUladzislau Rezki (Sony) 
77068ad4a33SUladzislau Rezki (Sony) 	/*
77168ad4a33SUladzislau Rezki (Sony) 	 * start            end
77268ad4a33SUladzislau Rezki (Sony) 	 * |                |
77368ad4a33SUladzislau Rezki (Sony) 	 * |<------VA------>|<-----Next----->|
77468ad4a33SUladzislau Rezki (Sony) 	 *                  |                |
77568ad4a33SUladzislau Rezki (Sony) 	 *                  start            end
77668ad4a33SUladzislau Rezki (Sony) 	 */
77768ad4a33SUladzislau Rezki (Sony) 	if (next != head) {
77868ad4a33SUladzislau Rezki (Sony) 		sibling = list_entry(next, struct vmap_area, list);
77968ad4a33SUladzislau Rezki (Sony) 		if (sibling->va_start == va->va_end) {
78068ad4a33SUladzislau Rezki (Sony) 			sibling->va_start = va->va_start;
78168ad4a33SUladzislau Rezki (Sony) 
78268ad4a33SUladzislau Rezki (Sony) 			/* Free vmap_area object. */
78368ad4a33SUladzislau Rezki (Sony) 			kmem_cache_free(vmap_area_cachep, va);
78468ad4a33SUladzislau Rezki (Sony) 
78568ad4a33SUladzislau Rezki (Sony) 			/* Point to the new merged area. */
78668ad4a33SUladzislau Rezki (Sony) 			va = sibling;
78768ad4a33SUladzislau Rezki (Sony) 			merged = true;
78868ad4a33SUladzislau Rezki (Sony) 		}
78968ad4a33SUladzislau Rezki (Sony) 	}
79068ad4a33SUladzislau Rezki (Sony) 
79168ad4a33SUladzislau Rezki (Sony) 	/*
79268ad4a33SUladzislau Rezki (Sony) 	 * start            end
79368ad4a33SUladzislau Rezki (Sony) 	 * |                |
79468ad4a33SUladzislau Rezki (Sony) 	 * |<-----Prev----->|<------VA------>|
79568ad4a33SUladzislau Rezki (Sony) 	 *                  |                |
79668ad4a33SUladzislau Rezki (Sony) 	 *                  start            end
79768ad4a33SUladzislau Rezki (Sony) 	 */
79868ad4a33SUladzislau Rezki (Sony) 	if (next->prev != head) {
79968ad4a33SUladzislau Rezki (Sony) 		sibling = list_entry(next->prev, struct vmap_area, list);
80068ad4a33SUladzislau Rezki (Sony) 		if (sibling->va_end == va->va_start) {
8015dd78640SUladzislau Rezki (Sony) 			/*
8025dd78640SUladzislau Rezki (Sony) 			 * If both neighbors are coalesced, it is important
8035dd78640SUladzislau Rezki (Sony) 			 * to unlink the "next" node first, followed by merging
8045dd78640SUladzislau Rezki (Sony) 			 * with "previous" one. Otherwise the tree might not be
8055dd78640SUladzislau Rezki (Sony) 			 * fully populated if a sibling's augmented value is
8065dd78640SUladzislau Rezki (Sony) 			 * "normalized" because of rotation operations.
8075dd78640SUladzislau Rezki (Sony) 			 */
80854f63d9dSUladzislau Rezki (Sony) 			if (merged)
80968ad4a33SUladzislau Rezki (Sony) 				unlink_va(va, root);
81068ad4a33SUladzislau Rezki (Sony) 
8115dd78640SUladzislau Rezki (Sony) 			sibling->va_end = va->va_end;
8125dd78640SUladzislau Rezki (Sony) 
81368ad4a33SUladzislau Rezki (Sony) 			/* Free vmap_area object. */
81468ad4a33SUladzislau Rezki (Sony) 			kmem_cache_free(vmap_area_cachep, va);
8153c5c3cfbSDaniel Axtens 
8163c5c3cfbSDaniel Axtens 			/* Point to the new merged area. */
8173c5c3cfbSDaniel Axtens 			va = sibling;
8183c5c3cfbSDaniel Axtens 			merged = true;
81968ad4a33SUladzislau Rezki (Sony) 		}
82068ad4a33SUladzislau Rezki (Sony) 	}
82168ad4a33SUladzislau Rezki (Sony) 
82268ad4a33SUladzislau Rezki (Sony) insert:
8235dd78640SUladzislau Rezki (Sony) 	if (!merged)
82468ad4a33SUladzislau Rezki (Sony) 		link_va(va, root, parent, link, head);
8253c5c3cfbSDaniel Axtens 
82696e2db45SUladzislau Rezki (Sony) 	return va;
82796e2db45SUladzislau Rezki (Sony) }
82896e2db45SUladzislau Rezki (Sony) 
82996e2db45SUladzislau Rezki (Sony) static __always_inline struct vmap_area *
83096e2db45SUladzislau Rezki (Sony) merge_or_add_vmap_area_augment(struct vmap_area *va,
83196e2db45SUladzislau Rezki (Sony) 	struct rb_root *root, struct list_head *head)
83296e2db45SUladzislau Rezki (Sony) {
83396e2db45SUladzislau Rezki (Sony) 	va = merge_or_add_vmap_area(va, root, head);
83496e2db45SUladzislau Rezki (Sony) 	if (va)
8355dd78640SUladzislau Rezki (Sony) 		augment_tree_propagate_from(va);
83696e2db45SUladzislau Rezki (Sony) 
8373c5c3cfbSDaniel Axtens 	return va;
83868ad4a33SUladzislau Rezki (Sony) }
83968ad4a33SUladzislau Rezki (Sony) 
84068ad4a33SUladzislau Rezki (Sony) static __always_inline bool
84168ad4a33SUladzislau Rezki (Sony) is_within_this_va(struct vmap_area *va, unsigned long size,
84268ad4a33SUladzislau Rezki (Sony) 	unsigned long align, unsigned long vstart)
84368ad4a33SUladzislau Rezki (Sony) {
84468ad4a33SUladzislau Rezki (Sony) 	unsigned long nva_start_addr;
84568ad4a33SUladzislau Rezki (Sony) 
84668ad4a33SUladzislau Rezki (Sony) 	if (va->va_start > vstart)
84768ad4a33SUladzislau Rezki (Sony) 		nva_start_addr = ALIGN(va->va_start, align);
84868ad4a33SUladzislau Rezki (Sony) 	else
84968ad4a33SUladzislau Rezki (Sony) 		nva_start_addr = ALIGN(vstart, align);
85068ad4a33SUladzislau Rezki (Sony) 
85168ad4a33SUladzislau Rezki (Sony) 	/* Can be overflowed due to big size or alignment. */
85268ad4a33SUladzislau Rezki (Sony) 	if (nva_start_addr + size < nva_start_addr ||
85368ad4a33SUladzislau Rezki (Sony) 			nva_start_addr < vstart)
85468ad4a33SUladzislau Rezki (Sony) 		return false;
85568ad4a33SUladzislau Rezki (Sony) 
85668ad4a33SUladzislau Rezki (Sony) 	return (nva_start_addr + size <= va->va_end);
85768ad4a33SUladzislau Rezki (Sony) }
85868ad4a33SUladzislau Rezki (Sony) 
85968ad4a33SUladzislau Rezki (Sony) /*
86068ad4a33SUladzislau Rezki (Sony)  * Find the first free block(lowest start address) in the tree,
86168ad4a33SUladzislau Rezki (Sony)  * that will accomplish the request corresponding to passing
86268ad4a33SUladzislau Rezki (Sony)  * parameters.
86368ad4a33SUladzislau Rezki (Sony)  */
86468ad4a33SUladzislau Rezki (Sony) static __always_inline struct vmap_area *
86568ad4a33SUladzislau Rezki (Sony) find_vmap_lowest_match(unsigned long size,
86668ad4a33SUladzislau Rezki (Sony) 	unsigned long align, unsigned long vstart)
86768ad4a33SUladzislau Rezki (Sony) {
86868ad4a33SUladzislau Rezki (Sony) 	struct vmap_area *va;
86968ad4a33SUladzislau Rezki (Sony) 	struct rb_node *node;
87068ad4a33SUladzislau Rezki (Sony) 	unsigned long length;
87168ad4a33SUladzislau Rezki (Sony) 
87268ad4a33SUladzislau Rezki (Sony) 	/* Start from the root. */
87368ad4a33SUladzislau Rezki (Sony) 	node = free_vmap_area_root.rb_node;
87468ad4a33SUladzislau Rezki (Sony) 
87568ad4a33SUladzislau Rezki (Sony) 	/* Adjust the search size for alignment overhead. */
87668ad4a33SUladzislau Rezki (Sony) 	length = size + align - 1;
87768ad4a33SUladzislau Rezki (Sony) 
87868ad4a33SUladzislau Rezki (Sony) 	while (node) {
87968ad4a33SUladzislau Rezki (Sony) 		va = rb_entry(node, struct vmap_area, rb_node);
88068ad4a33SUladzislau Rezki (Sony) 
88168ad4a33SUladzislau Rezki (Sony) 		if (get_subtree_max_size(node->rb_left) >= length &&
88268ad4a33SUladzislau Rezki (Sony) 				vstart < va->va_start) {
88368ad4a33SUladzislau Rezki (Sony) 			node = node->rb_left;
88468ad4a33SUladzislau Rezki (Sony) 		} else {
88568ad4a33SUladzislau Rezki (Sony) 			if (is_within_this_va(va, size, align, vstart))
88668ad4a33SUladzislau Rezki (Sony) 				return va;
88768ad4a33SUladzislau Rezki (Sony) 
88868ad4a33SUladzislau Rezki (Sony) 			/*
88968ad4a33SUladzislau Rezki (Sony) 			 * Does not make sense to go deeper towards the right
89068ad4a33SUladzislau Rezki (Sony) 			 * sub-tree if it does not have a free block that is
89168ad4a33SUladzislau Rezki (Sony) 			 * equal or bigger to the requested search length.
89268ad4a33SUladzislau Rezki (Sony) 			 */
89368ad4a33SUladzislau Rezki (Sony) 			if (get_subtree_max_size(node->rb_right) >= length) {
89468ad4a33SUladzislau Rezki (Sony) 				node = node->rb_right;
89568ad4a33SUladzislau Rezki (Sony) 				continue;
89668ad4a33SUladzislau Rezki (Sony) 			}
89768ad4a33SUladzislau Rezki (Sony) 
89868ad4a33SUladzislau Rezki (Sony) 			/*
8993806b041SAndrew Morton 			 * OK. We roll back and find the first right sub-tree,
90068ad4a33SUladzislau Rezki (Sony) 			 * that will satisfy the search criteria. It can happen
90168ad4a33SUladzislau Rezki (Sony) 			 * only once due to "vstart" restriction.
90268ad4a33SUladzislau Rezki (Sony) 			 */
90368ad4a33SUladzislau Rezki (Sony) 			while ((node = rb_parent(node))) {
90468ad4a33SUladzislau Rezki (Sony) 				va = rb_entry(node, struct vmap_area, rb_node);
90568ad4a33SUladzislau Rezki (Sony) 				if (is_within_this_va(va, size, align, vstart))
90668ad4a33SUladzislau Rezki (Sony) 					return va;
90768ad4a33SUladzislau Rezki (Sony) 
90868ad4a33SUladzislau Rezki (Sony) 				if (get_subtree_max_size(node->rb_right) >= length &&
90968ad4a33SUladzislau Rezki (Sony) 						vstart <= va->va_start) {
91068ad4a33SUladzislau Rezki (Sony) 					node = node->rb_right;
91168ad4a33SUladzislau Rezki (Sony) 					break;
91268ad4a33SUladzislau Rezki (Sony) 				}
91368ad4a33SUladzislau Rezki (Sony) 			}
91468ad4a33SUladzislau Rezki (Sony) 		}
91568ad4a33SUladzislau Rezki (Sony) 	}
91668ad4a33SUladzislau Rezki (Sony) 
91768ad4a33SUladzislau Rezki (Sony) 	return NULL;
91868ad4a33SUladzislau Rezki (Sony) }
91968ad4a33SUladzislau Rezki (Sony) 
920a6cf4e0fSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_LOWEST_MATCH_CHECK
921a6cf4e0fSUladzislau Rezki (Sony) #include <linux/random.h>
922a6cf4e0fSUladzislau Rezki (Sony) 
923a6cf4e0fSUladzislau Rezki (Sony) static struct vmap_area *
924a6cf4e0fSUladzislau Rezki (Sony) find_vmap_lowest_linear_match(unsigned long size,
925a6cf4e0fSUladzislau Rezki (Sony) 	unsigned long align, unsigned long vstart)
926a6cf4e0fSUladzislau Rezki (Sony) {
927a6cf4e0fSUladzislau Rezki (Sony) 	struct vmap_area *va;
928a6cf4e0fSUladzislau Rezki (Sony) 
929a6cf4e0fSUladzislau Rezki (Sony) 	list_for_each_entry(va, &free_vmap_area_list, list) {
930a6cf4e0fSUladzislau Rezki (Sony) 		if (!is_within_this_va(va, size, align, vstart))
931a6cf4e0fSUladzislau Rezki (Sony) 			continue;
932a6cf4e0fSUladzislau Rezki (Sony) 
933a6cf4e0fSUladzislau Rezki (Sony) 		return va;
934a6cf4e0fSUladzislau Rezki (Sony) 	}
935a6cf4e0fSUladzislau Rezki (Sony) 
936a6cf4e0fSUladzislau Rezki (Sony) 	return NULL;
937a6cf4e0fSUladzislau Rezki (Sony) }
938a6cf4e0fSUladzislau Rezki (Sony) 
939a6cf4e0fSUladzislau Rezki (Sony) static void
940a6cf4e0fSUladzislau Rezki (Sony) find_vmap_lowest_match_check(unsigned long size)
941a6cf4e0fSUladzislau Rezki (Sony) {
942a6cf4e0fSUladzislau Rezki (Sony) 	struct vmap_area *va_1, *va_2;
943a6cf4e0fSUladzislau Rezki (Sony) 	unsigned long vstart;
944a6cf4e0fSUladzislau Rezki (Sony) 	unsigned int rnd;
945a6cf4e0fSUladzislau Rezki (Sony) 
946a6cf4e0fSUladzislau Rezki (Sony) 	get_random_bytes(&rnd, sizeof(rnd));
947a6cf4e0fSUladzislau Rezki (Sony) 	vstart = VMALLOC_START + rnd;
948a6cf4e0fSUladzislau Rezki (Sony) 
949a6cf4e0fSUladzislau Rezki (Sony) 	va_1 = find_vmap_lowest_match(size, 1, vstart);
950a6cf4e0fSUladzislau Rezki (Sony) 	va_2 = find_vmap_lowest_linear_match(size, 1, vstart);
951a6cf4e0fSUladzislau Rezki (Sony) 
952a6cf4e0fSUladzislau Rezki (Sony) 	if (va_1 != va_2)
953a6cf4e0fSUladzislau Rezki (Sony) 		pr_emerg("not lowest: t: 0x%p, l: 0x%p, v: 0x%lx\n",
954a6cf4e0fSUladzislau Rezki (Sony) 			va_1, va_2, vstart);
955a6cf4e0fSUladzislau Rezki (Sony) }
956a6cf4e0fSUladzislau Rezki (Sony) #endif
957a6cf4e0fSUladzislau Rezki (Sony) 
95868ad4a33SUladzislau Rezki (Sony) enum fit_type {
95968ad4a33SUladzislau Rezki (Sony) 	NOTHING_FIT = 0,
96068ad4a33SUladzislau Rezki (Sony) 	FL_FIT_TYPE = 1,	/* full fit */
96168ad4a33SUladzislau Rezki (Sony) 	LE_FIT_TYPE = 2,	/* left edge fit */
96268ad4a33SUladzislau Rezki (Sony) 	RE_FIT_TYPE = 3,	/* right edge fit */
96368ad4a33SUladzislau Rezki (Sony) 	NE_FIT_TYPE = 4		/* no edge fit */
96468ad4a33SUladzislau Rezki (Sony) };
96568ad4a33SUladzislau Rezki (Sony) 
96668ad4a33SUladzislau Rezki (Sony) static __always_inline enum fit_type
96768ad4a33SUladzislau Rezki (Sony) classify_va_fit_type(struct vmap_area *va,
96868ad4a33SUladzislau Rezki (Sony) 	unsigned long nva_start_addr, unsigned long size)
96968ad4a33SUladzislau Rezki (Sony) {
97068ad4a33SUladzislau Rezki (Sony) 	enum fit_type type;
97168ad4a33SUladzislau Rezki (Sony) 
97268ad4a33SUladzislau Rezki (Sony) 	/* Check if it is within VA. */
97368ad4a33SUladzislau Rezki (Sony) 	if (nva_start_addr < va->va_start ||
97468ad4a33SUladzislau Rezki (Sony) 			nva_start_addr + size > va->va_end)
97568ad4a33SUladzislau Rezki (Sony) 		return NOTHING_FIT;
97668ad4a33SUladzislau Rezki (Sony) 
97768ad4a33SUladzislau Rezki (Sony) 	/* Now classify. */
97868ad4a33SUladzislau Rezki (Sony) 	if (va->va_start == nva_start_addr) {
97968ad4a33SUladzislau Rezki (Sony) 		if (va->va_end == nva_start_addr + size)
98068ad4a33SUladzislau Rezki (Sony) 			type = FL_FIT_TYPE;
98168ad4a33SUladzislau Rezki (Sony) 		else
98268ad4a33SUladzislau Rezki (Sony) 			type = LE_FIT_TYPE;
98368ad4a33SUladzislau Rezki (Sony) 	} else if (va->va_end == nva_start_addr + size) {
98468ad4a33SUladzislau Rezki (Sony) 		type = RE_FIT_TYPE;
98568ad4a33SUladzislau Rezki (Sony) 	} else {
98668ad4a33SUladzislau Rezki (Sony) 		type = NE_FIT_TYPE;
98768ad4a33SUladzislau Rezki (Sony) 	}
98868ad4a33SUladzislau Rezki (Sony) 
98968ad4a33SUladzislau Rezki (Sony) 	return type;
99068ad4a33SUladzislau Rezki (Sony) }
99168ad4a33SUladzislau Rezki (Sony) 
99268ad4a33SUladzislau Rezki (Sony) static __always_inline int
99368ad4a33SUladzislau Rezki (Sony) adjust_va_to_fit_type(struct vmap_area *va,
99468ad4a33SUladzislau Rezki (Sony) 	unsigned long nva_start_addr, unsigned long size,
99568ad4a33SUladzislau Rezki (Sony) 	enum fit_type type)
99668ad4a33SUladzislau Rezki (Sony) {
9972c929233SArnd Bergmann 	struct vmap_area *lva = NULL;
99868ad4a33SUladzislau Rezki (Sony) 
99968ad4a33SUladzislau Rezki (Sony) 	if (type == FL_FIT_TYPE) {
100068ad4a33SUladzislau Rezki (Sony) 		/*
100168ad4a33SUladzislau Rezki (Sony) 		 * No need to split VA, it fully fits.
100268ad4a33SUladzislau Rezki (Sony) 		 *
100368ad4a33SUladzislau Rezki (Sony) 		 * |               |
100468ad4a33SUladzislau Rezki (Sony) 		 * V      NVA      V
100568ad4a33SUladzislau Rezki (Sony) 		 * |---------------|
100668ad4a33SUladzislau Rezki (Sony) 		 */
100768ad4a33SUladzislau Rezki (Sony) 		unlink_va(va, &free_vmap_area_root);
100868ad4a33SUladzislau Rezki (Sony) 		kmem_cache_free(vmap_area_cachep, va);
100968ad4a33SUladzislau Rezki (Sony) 	} else if (type == LE_FIT_TYPE) {
101068ad4a33SUladzislau Rezki (Sony) 		/*
101168ad4a33SUladzislau Rezki (Sony) 		 * Split left edge of fit VA.
101268ad4a33SUladzislau Rezki (Sony) 		 *
101368ad4a33SUladzislau Rezki (Sony) 		 * |       |
101468ad4a33SUladzislau Rezki (Sony) 		 * V  NVA  V   R
101568ad4a33SUladzislau Rezki (Sony) 		 * |-------|-------|
101668ad4a33SUladzislau Rezki (Sony) 		 */
101768ad4a33SUladzislau Rezki (Sony) 		va->va_start += size;
101868ad4a33SUladzislau Rezki (Sony) 	} else if (type == RE_FIT_TYPE) {
101968ad4a33SUladzislau Rezki (Sony) 		/*
102068ad4a33SUladzislau Rezki (Sony) 		 * Split right edge of fit VA.
102168ad4a33SUladzislau Rezki (Sony) 		 *
102268ad4a33SUladzislau Rezki (Sony) 		 *         |       |
102368ad4a33SUladzislau Rezki (Sony) 		 *     L   V  NVA  V
102468ad4a33SUladzislau Rezki (Sony) 		 * |-------|-------|
102568ad4a33SUladzislau Rezki (Sony) 		 */
102668ad4a33SUladzislau Rezki (Sony) 		va->va_end = nva_start_addr;
102768ad4a33SUladzislau Rezki (Sony) 	} else if (type == NE_FIT_TYPE) {
102868ad4a33SUladzislau Rezki (Sony) 		/*
102968ad4a33SUladzislau Rezki (Sony) 		 * Split no edge of fit VA.
103068ad4a33SUladzislau Rezki (Sony) 		 *
103168ad4a33SUladzislau Rezki (Sony) 		 *     |       |
103268ad4a33SUladzislau Rezki (Sony) 		 *   L V  NVA  V R
103368ad4a33SUladzislau Rezki (Sony) 		 * |---|-------|---|
103468ad4a33SUladzislau Rezki (Sony) 		 */
103582dd23e8SUladzislau Rezki (Sony) 		lva = __this_cpu_xchg(ne_fit_preload_node, NULL);
103682dd23e8SUladzislau Rezki (Sony) 		if (unlikely(!lva)) {
103782dd23e8SUladzislau Rezki (Sony) 			/*
103882dd23e8SUladzislau Rezki (Sony) 			 * For percpu allocator we do not do any pre-allocation
103982dd23e8SUladzislau Rezki (Sony) 			 * and leave it as it is. The reason is it most likely
104082dd23e8SUladzislau Rezki (Sony) 			 * never ends up with NE_FIT_TYPE splitting. In case of
104182dd23e8SUladzislau Rezki (Sony) 			 * percpu allocations offsets and sizes are aligned to
104282dd23e8SUladzislau Rezki (Sony) 			 * fixed align request, i.e. RE_FIT_TYPE and FL_FIT_TYPE
104382dd23e8SUladzislau Rezki (Sony) 			 * are its main fitting cases.
104482dd23e8SUladzislau Rezki (Sony) 			 *
104582dd23e8SUladzislau Rezki (Sony) 			 * There are a few exceptions though, as an example it is
104682dd23e8SUladzislau Rezki (Sony) 			 * a first allocation (early boot up) when we have "one"
104782dd23e8SUladzislau Rezki (Sony) 			 * big free space that has to be split.
1048060650a2SUladzislau Rezki (Sony) 			 *
1049060650a2SUladzislau Rezki (Sony) 			 * Also we can hit this path in case of regular "vmap"
1050060650a2SUladzislau Rezki (Sony) 			 * allocations, if "this" current CPU was not preloaded.
1051060650a2SUladzislau Rezki (Sony) 			 * See the comment in alloc_vmap_area() why. If so, then
1052060650a2SUladzislau Rezki (Sony) 			 * GFP_NOWAIT is used instead to get an extra object for
1053060650a2SUladzislau Rezki (Sony) 			 * split purpose. That is rare and most time does not
1054060650a2SUladzislau Rezki (Sony) 			 * occur.
1055060650a2SUladzislau Rezki (Sony) 			 *
1056060650a2SUladzislau Rezki (Sony) 			 * What happens if an allocation gets failed. Basically,
1057060650a2SUladzislau Rezki (Sony) 			 * an "overflow" path is triggered to purge lazily freed
1058060650a2SUladzislau Rezki (Sony) 			 * areas to free some memory, then, the "retry" path is
1059060650a2SUladzislau Rezki (Sony) 			 * triggered to repeat one more time. See more details
1060060650a2SUladzislau Rezki (Sony) 			 * in alloc_vmap_area() function.
106182dd23e8SUladzislau Rezki (Sony) 			 */
106268ad4a33SUladzislau Rezki (Sony) 			lva = kmem_cache_alloc(vmap_area_cachep, GFP_NOWAIT);
106382dd23e8SUladzislau Rezki (Sony) 			if (!lva)
106468ad4a33SUladzislau Rezki (Sony) 				return -1;
106582dd23e8SUladzislau Rezki (Sony) 		}
106668ad4a33SUladzislau Rezki (Sony) 
106768ad4a33SUladzislau Rezki (Sony) 		/*
106868ad4a33SUladzislau Rezki (Sony) 		 * Build the remainder.
106968ad4a33SUladzislau Rezki (Sony) 		 */
107068ad4a33SUladzislau Rezki (Sony) 		lva->va_start = va->va_start;
107168ad4a33SUladzislau Rezki (Sony) 		lva->va_end = nva_start_addr;
107268ad4a33SUladzislau Rezki (Sony) 
107368ad4a33SUladzislau Rezki (Sony) 		/*
107468ad4a33SUladzislau Rezki (Sony) 		 * Shrink this VA to remaining size.
107568ad4a33SUladzislau Rezki (Sony) 		 */
107668ad4a33SUladzislau Rezki (Sony) 		va->va_start = nva_start_addr + size;
107768ad4a33SUladzislau Rezki (Sony) 	} else {
107868ad4a33SUladzislau Rezki (Sony) 		return -1;
107968ad4a33SUladzislau Rezki (Sony) 	}
108068ad4a33SUladzislau Rezki (Sony) 
108168ad4a33SUladzislau Rezki (Sony) 	if (type != FL_FIT_TYPE) {
108268ad4a33SUladzislau Rezki (Sony) 		augment_tree_propagate_from(va);
108368ad4a33SUladzislau Rezki (Sony) 
10842c929233SArnd Bergmann 		if (lva)	/* type == NE_FIT_TYPE */
108568ad4a33SUladzislau Rezki (Sony) 			insert_vmap_area_augment(lva, &va->rb_node,
108668ad4a33SUladzislau Rezki (Sony) 				&free_vmap_area_root, &free_vmap_area_list);
108768ad4a33SUladzislau Rezki (Sony) 	}
108868ad4a33SUladzislau Rezki (Sony) 
108968ad4a33SUladzislau Rezki (Sony) 	return 0;
109068ad4a33SUladzislau Rezki (Sony) }
109168ad4a33SUladzislau Rezki (Sony) 
109268ad4a33SUladzislau Rezki (Sony) /*
109368ad4a33SUladzislau Rezki (Sony)  * Returns a start address of the newly allocated area, if success.
109468ad4a33SUladzislau Rezki (Sony)  * Otherwise a vend is returned that indicates failure.
109568ad4a33SUladzislau Rezki (Sony)  */
109668ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long
109768ad4a33SUladzislau Rezki (Sony) __alloc_vmap_area(unsigned long size, unsigned long align,
1098cacca6baSUladzislau Rezki (Sony) 	unsigned long vstart, unsigned long vend)
109968ad4a33SUladzislau Rezki (Sony) {
110068ad4a33SUladzislau Rezki (Sony) 	unsigned long nva_start_addr;
110168ad4a33SUladzislau Rezki (Sony) 	struct vmap_area *va;
110268ad4a33SUladzislau Rezki (Sony) 	enum fit_type type;
110368ad4a33SUladzislau Rezki (Sony) 	int ret;
110468ad4a33SUladzislau Rezki (Sony) 
110568ad4a33SUladzislau Rezki (Sony) 	va = find_vmap_lowest_match(size, align, vstart);
110668ad4a33SUladzislau Rezki (Sony) 	if (unlikely(!va))
110768ad4a33SUladzislau Rezki (Sony) 		return vend;
110868ad4a33SUladzislau Rezki (Sony) 
110968ad4a33SUladzislau Rezki (Sony) 	if (va->va_start > vstart)
111068ad4a33SUladzislau Rezki (Sony) 		nva_start_addr = ALIGN(va->va_start, align);
111168ad4a33SUladzislau Rezki (Sony) 	else
111268ad4a33SUladzislau Rezki (Sony) 		nva_start_addr = ALIGN(vstart, align);
111368ad4a33SUladzislau Rezki (Sony) 
111468ad4a33SUladzislau Rezki (Sony) 	/* Check the "vend" restriction. */
111568ad4a33SUladzislau Rezki (Sony) 	if (nva_start_addr + size > vend)
111668ad4a33SUladzislau Rezki (Sony) 		return vend;
111768ad4a33SUladzislau Rezki (Sony) 
111868ad4a33SUladzislau Rezki (Sony) 	/* Classify what we have found. */
111968ad4a33SUladzislau Rezki (Sony) 	type = classify_va_fit_type(va, nva_start_addr, size);
112068ad4a33SUladzislau Rezki (Sony) 	if (WARN_ON_ONCE(type == NOTHING_FIT))
112168ad4a33SUladzislau Rezki (Sony) 		return vend;
112268ad4a33SUladzislau Rezki (Sony) 
112368ad4a33SUladzislau Rezki (Sony) 	/* Update the free vmap_area. */
112468ad4a33SUladzislau Rezki (Sony) 	ret = adjust_va_to_fit_type(va, nva_start_addr, size, type);
112568ad4a33SUladzislau Rezki (Sony) 	if (ret)
112668ad4a33SUladzislau Rezki (Sony) 		return vend;
112768ad4a33SUladzislau Rezki (Sony) 
1128a6cf4e0fSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_LOWEST_MATCH_CHECK
1129a6cf4e0fSUladzislau Rezki (Sony) 	find_vmap_lowest_match_check(size);
1130a6cf4e0fSUladzislau Rezki (Sony) #endif
1131a6cf4e0fSUladzislau Rezki (Sony) 
113268ad4a33SUladzislau Rezki (Sony) 	return nva_start_addr;
113368ad4a33SUladzislau Rezki (Sony) }
11344da56b99SChris Wilson 
1135db64fe02SNick Piggin /*
1136d98c9e83SAndrey Ryabinin  * Free a region of KVA allocated by alloc_vmap_area
1137d98c9e83SAndrey Ryabinin  */
1138d98c9e83SAndrey Ryabinin static void free_vmap_area(struct vmap_area *va)
1139d98c9e83SAndrey Ryabinin {
1140d98c9e83SAndrey Ryabinin 	/*
1141d98c9e83SAndrey Ryabinin 	 * Remove from the busy tree/list.
1142d98c9e83SAndrey Ryabinin 	 */
1143d98c9e83SAndrey Ryabinin 	spin_lock(&vmap_area_lock);
1144d98c9e83SAndrey Ryabinin 	unlink_va(va, &vmap_area_root);
1145d98c9e83SAndrey Ryabinin 	spin_unlock(&vmap_area_lock);
1146d98c9e83SAndrey Ryabinin 
1147d98c9e83SAndrey Ryabinin 	/*
1148d98c9e83SAndrey Ryabinin 	 * Insert/Merge it back to the free tree/list.
1149d98c9e83SAndrey Ryabinin 	 */
1150d98c9e83SAndrey Ryabinin 	spin_lock(&free_vmap_area_lock);
115196e2db45SUladzislau Rezki (Sony) 	merge_or_add_vmap_area_augment(va, &free_vmap_area_root, &free_vmap_area_list);
1152d98c9e83SAndrey Ryabinin 	spin_unlock(&free_vmap_area_lock);
1153d98c9e83SAndrey Ryabinin }
1154d98c9e83SAndrey Ryabinin 
1155d98c9e83SAndrey Ryabinin /*
1156db64fe02SNick Piggin  * Allocate a region of KVA of the specified size and alignment, within the
1157db64fe02SNick Piggin  * vstart and vend.
1158db64fe02SNick Piggin  */
1159db64fe02SNick Piggin static struct vmap_area *alloc_vmap_area(unsigned long size,
1160db64fe02SNick Piggin 				unsigned long align,
1161db64fe02SNick Piggin 				unsigned long vstart, unsigned long vend,
1162db64fe02SNick Piggin 				int node, gfp_t gfp_mask)
1163db64fe02SNick Piggin {
116482dd23e8SUladzislau Rezki (Sony) 	struct vmap_area *va, *pva;
11651da177e4SLinus Torvalds 	unsigned long addr;
1166db64fe02SNick Piggin 	int purged = 0;
1167d98c9e83SAndrey Ryabinin 	int ret;
1168db64fe02SNick Piggin 
11697766970cSNick Piggin 	BUG_ON(!size);
1170891c49abSAlexander Kuleshov 	BUG_ON(offset_in_page(size));
117189699605SNick Piggin 	BUG_ON(!is_power_of_2(align));
1172db64fe02SNick Piggin 
117368ad4a33SUladzislau Rezki (Sony) 	if (unlikely(!vmap_initialized))
117468ad4a33SUladzislau Rezki (Sony) 		return ERR_PTR(-EBUSY);
117568ad4a33SUladzislau Rezki (Sony) 
11765803ed29SChristoph Hellwig 	might_sleep();
1177f07116d7SUladzislau Rezki (Sony) 	gfp_mask = gfp_mask & GFP_RECLAIM_MASK;
11784da56b99SChris Wilson 
1179f07116d7SUladzislau Rezki (Sony) 	va = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node);
1180db64fe02SNick Piggin 	if (unlikely(!va))
1181db64fe02SNick Piggin 		return ERR_PTR(-ENOMEM);
1182db64fe02SNick Piggin 
11837f88f88fSCatalin Marinas 	/*
11847f88f88fSCatalin Marinas 	 * Only scan the relevant parts containing pointers to other objects
11857f88f88fSCatalin Marinas 	 * to avoid false negatives.
11867f88f88fSCatalin Marinas 	 */
1187f07116d7SUladzislau Rezki (Sony) 	kmemleak_scan_area(&va->rb_node, SIZE_MAX, gfp_mask);
11887f88f88fSCatalin Marinas 
1189db64fe02SNick Piggin retry:
119082dd23e8SUladzislau Rezki (Sony) 	/*
119181f1ba58SUladzislau Rezki (Sony) 	 * Preload this CPU with one extra vmap_area object. It is used
119281f1ba58SUladzislau Rezki (Sony) 	 * when fit type of free area is NE_FIT_TYPE. Please note, it
119381f1ba58SUladzislau Rezki (Sony) 	 * does not guarantee that an allocation occurs on a CPU that
119481f1ba58SUladzislau Rezki (Sony) 	 * is preloaded, instead we minimize the case when it is not.
119581f1ba58SUladzislau Rezki (Sony) 	 * It can happen because of cpu migration, because there is a
119681f1ba58SUladzislau Rezki (Sony) 	 * race until the below spinlock is taken.
119782dd23e8SUladzislau Rezki (Sony) 	 *
119882dd23e8SUladzislau Rezki (Sony) 	 * The preload is done in non-atomic context, thus it allows us
119982dd23e8SUladzislau Rezki (Sony) 	 * to use more permissive allocation masks to be more stable under
120081f1ba58SUladzislau Rezki (Sony) 	 * low memory condition and high memory pressure. In rare case,
120181f1ba58SUladzislau Rezki (Sony) 	 * if not preloaded, GFP_NOWAIT is used.
120282dd23e8SUladzislau Rezki (Sony) 	 *
120381f1ba58SUladzislau Rezki (Sony) 	 * Set "pva" to NULL here, because of "retry" path.
120482dd23e8SUladzislau Rezki (Sony) 	 */
120581f1ba58SUladzislau Rezki (Sony) 	pva = NULL;
120682dd23e8SUladzislau Rezki (Sony) 
120781f1ba58SUladzislau Rezki (Sony) 	if (!this_cpu_read(ne_fit_preload_node))
120881f1ba58SUladzislau Rezki (Sony) 		/*
120981f1ba58SUladzislau Rezki (Sony) 		 * Even if it fails we do not really care about that.
121081f1ba58SUladzislau Rezki (Sony) 		 * Just proceed as it is. If needed "overflow" path
121181f1ba58SUladzislau Rezki (Sony) 		 * will refill the cache we allocate from.
121281f1ba58SUladzislau Rezki (Sony) 		 */
1213f07116d7SUladzislau Rezki (Sony) 		pva = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node);
121482dd23e8SUladzislau Rezki (Sony) 
1215e36176beSUladzislau Rezki (Sony) 	spin_lock(&free_vmap_area_lock);
121681f1ba58SUladzislau Rezki (Sony) 
121781f1ba58SUladzislau Rezki (Sony) 	if (pva && __this_cpu_cmpxchg(ne_fit_preload_node, NULL, pva))
121881f1ba58SUladzislau Rezki (Sony) 		kmem_cache_free(vmap_area_cachep, pva);
121968ad4a33SUladzislau Rezki (Sony) 
122089699605SNick Piggin 	/*
122168ad4a33SUladzislau Rezki (Sony) 	 * If an allocation fails, the "vend" address is
122268ad4a33SUladzislau Rezki (Sony) 	 * returned. Therefore trigger the overflow path.
122389699605SNick Piggin 	 */
1224cacca6baSUladzislau Rezki (Sony) 	addr = __alloc_vmap_area(size, align, vstart, vend);
1225e36176beSUladzislau Rezki (Sony) 	spin_unlock(&free_vmap_area_lock);
1226e36176beSUladzislau Rezki (Sony) 
122768ad4a33SUladzislau Rezki (Sony) 	if (unlikely(addr == vend))
122889699605SNick Piggin 		goto overflow;
122989699605SNick Piggin 
123089699605SNick Piggin 	va->va_start = addr;
123189699605SNick Piggin 	va->va_end = addr + size;
1232688fcbfcSPengfei Li 	va->vm = NULL;
123368ad4a33SUladzislau Rezki (Sony) 
1234d98c9e83SAndrey Ryabinin 
1235e36176beSUladzislau Rezki (Sony) 	spin_lock(&vmap_area_lock);
1236e36176beSUladzislau Rezki (Sony) 	insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
123789699605SNick Piggin 	spin_unlock(&vmap_area_lock);
123889699605SNick Piggin 
123961e16557SWang Xiaoqiang 	BUG_ON(!IS_ALIGNED(va->va_start, align));
124089699605SNick Piggin 	BUG_ON(va->va_start < vstart);
124189699605SNick Piggin 	BUG_ON(va->va_end > vend);
124289699605SNick Piggin 
1243d98c9e83SAndrey Ryabinin 	ret = kasan_populate_vmalloc(addr, size);
1244d98c9e83SAndrey Ryabinin 	if (ret) {
1245d98c9e83SAndrey Ryabinin 		free_vmap_area(va);
1246d98c9e83SAndrey Ryabinin 		return ERR_PTR(ret);
1247d98c9e83SAndrey Ryabinin 	}
1248d98c9e83SAndrey Ryabinin 
124989699605SNick Piggin 	return va;
125089699605SNick Piggin 
12517766970cSNick Piggin overflow:
1252db64fe02SNick Piggin 	if (!purged) {
1253db64fe02SNick Piggin 		purge_vmap_area_lazy();
1254db64fe02SNick Piggin 		purged = 1;
1255db64fe02SNick Piggin 		goto retry;
1256db64fe02SNick Piggin 	}
12574da56b99SChris Wilson 
12584da56b99SChris Wilson 	if (gfpflags_allow_blocking(gfp_mask)) {
12594da56b99SChris Wilson 		unsigned long freed = 0;
12604da56b99SChris Wilson 		blocking_notifier_call_chain(&vmap_notify_list, 0, &freed);
12614da56b99SChris Wilson 		if (freed > 0) {
12624da56b99SChris Wilson 			purged = 0;
12634da56b99SChris Wilson 			goto retry;
12644da56b99SChris Wilson 		}
12654da56b99SChris Wilson 	}
12664da56b99SChris Wilson 
126703497d76SFlorian Fainelli 	if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit())
1268756a025fSJoe Perches 		pr_warn("vmap allocation for size %lu failed: use vmalloc=<size> to increase size\n",
1269756a025fSJoe Perches 			size);
127068ad4a33SUladzislau Rezki (Sony) 
127168ad4a33SUladzislau Rezki (Sony) 	kmem_cache_free(vmap_area_cachep, va);
1272db64fe02SNick Piggin 	return ERR_PTR(-EBUSY);
1273db64fe02SNick Piggin }
1274db64fe02SNick Piggin 
12754da56b99SChris Wilson int register_vmap_purge_notifier(struct notifier_block *nb)
12764da56b99SChris Wilson {
12774da56b99SChris Wilson 	return blocking_notifier_chain_register(&vmap_notify_list, nb);
12784da56b99SChris Wilson }
12794da56b99SChris Wilson EXPORT_SYMBOL_GPL(register_vmap_purge_notifier);
12804da56b99SChris Wilson 
12814da56b99SChris Wilson int unregister_vmap_purge_notifier(struct notifier_block *nb)
12824da56b99SChris Wilson {
12834da56b99SChris Wilson 	return blocking_notifier_chain_unregister(&vmap_notify_list, nb);
12844da56b99SChris Wilson }
12854da56b99SChris Wilson EXPORT_SYMBOL_GPL(unregister_vmap_purge_notifier);
12864da56b99SChris Wilson 
1287db64fe02SNick Piggin /*
1288db64fe02SNick Piggin  * lazy_max_pages is the maximum amount of virtual address space we gather up
1289db64fe02SNick Piggin  * before attempting to purge with a TLB flush.
1290db64fe02SNick Piggin  *
1291db64fe02SNick Piggin  * There is a tradeoff here: a larger number will cover more kernel page tables
1292db64fe02SNick Piggin  * and take slightly longer to purge, but it will linearly reduce the number of
1293db64fe02SNick Piggin  * global TLB flushes that must be performed. It would seem natural to scale
1294db64fe02SNick Piggin  * this number up linearly with the number of CPUs (because vmapping activity
1295db64fe02SNick Piggin  * could also scale linearly with the number of CPUs), however it is likely
1296db64fe02SNick Piggin  * that in practice, workloads might be constrained in other ways that mean
1297db64fe02SNick Piggin  * vmap activity will not scale linearly with CPUs. Also, I want to be
1298db64fe02SNick Piggin  * conservative and not introduce a big latency on huge systems, so go with
1299db64fe02SNick Piggin  * a less aggressive log scale. It will still be an improvement over the old
1300db64fe02SNick Piggin  * code, and it will be simple to change the scale factor if we find that it
1301db64fe02SNick Piggin  * becomes a problem on bigger systems.
1302db64fe02SNick Piggin  */
1303db64fe02SNick Piggin static unsigned long lazy_max_pages(void)
1304db64fe02SNick Piggin {
1305db64fe02SNick Piggin 	unsigned int log;
1306db64fe02SNick Piggin 
1307db64fe02SNick Piggin 	log = fls(num_online_cpus());
1308db64fe02SNick Piggin 
1309db64fe02SNick Piggin 	return log * (32UL * 1024 * 1024 / PAGE_SIZE);
1310db64fe02SNick Piggin }
1311db64fe02SNick Piggin 
13124d36e6f8SUladzislau Rezki (Sony) static atomic_long_t vmap_lazy_nr = ATOMIC_LONG_INIT(0);
1313db64fe02SNick Piggin 
13140574ecd1SChristoph Hellwig /*
13150574ecd1SChristoph Hellwig  * Serialize vmap purging.  There is no actual criticial section protected
13160574ecd1SChristoph Hellwig  * by this look, but we want to avoid concurrent calls for performance
13170574ecd1SChristoph Hellwig  * reasons and to make the pcpu_get_vm_areas more deterministic.
13180574ecd1SChristoph Hellwig  */
1319f9e09977SChristoph Hellwig static DEFINE_MUTEX(vmap_purge_lock);
13200574ecd1SChristoph Hellwig 
132102b709dfSNick Piggin /* for per-CPU blocks */
132202b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void);
132302b709dfSNick Piggin 
1324db64fe02SNick Piggin /*
13253ee48b6aSCliff Wickman  * called before a call to iounmap() if the caller wants vm_area_struct's
13263ee48b6aSCliff Wickman  * immediately freed.
13273ee48b6aSCliff Wickman  */
13283ee48b6aSCliff Wickman void set_iounmap_nonlazy(void)
13293ee48b6aSCliff Wickman {
13304d36e6f8SUladzislau Rezki (Sony) 	atomic_long_set(&vmap_lazy_nr, lazy_max_pages()+1);
13313ee48b6aSCliff Wickman }
13323ee48b6aSCliff Wickman 
13333ee48b6aSCliff Wickman /*
1334db64fe02SNick Piggin  * Purges all lazily-freed vmap areas.
1335db64fe02SNick Piggin  */
13360574ecd1SChristoph Hellwig static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end)
1337db64fe02SNick Piggin {
13384d36e6f8SUladzislau Rezki (Sony) 	unsigned long resched_threshold;
133996e2db45SUladzislau Rezki (Sony) 	struct list_head local_pure_list;
134096e2db45SUladzislau Rezki (Sony) 	struct vmap_area *va, *n_va;
1341db64fe02SNick Piggin 
13420574ecd1SChristoph Hellwig 	lockdep_assert_held(&vmap_purge_lock);
134302b709dfSNick Piggin 
134496e2db45SUladzislau Rezki (Sony) 	spin_lock(&purge_vmap_area_lock);
134596e2db45SUladzislau Rezki (Sony) 	purge_vmap_area_root = RB_ROOT;
134696e2db45SUladzislau Rezki (Sony) 	list_replace_init(&purge_vmap_area_list, &local_pure_list);
134796e2db45SUladzislau Rezki (Sony) 	spin_unlock(&purge_vmap_area_lock);
134896e2db45SUladzislau Rezki (Sony) 
134996e2db45SUladzislau Rezki (Sony) 	if (unlikely(list_empty(&local_pure_list)))
135068571be9SUladzislau Rezki (Sony) 		return false;
135168571be9SUladzislau Rezki (Sony) 
135296e2db45SUladzislau Rezki (Sony) 	start = min(start,
135396e2db45SUladzislau Rezki (Sony) 		list_first_entry(&local_pure_list,
135496e2db45SUladzislau Rezki (Sony) 			struct vmap_area, list)->va_start);
135596e2db45SUladzislau Rezki (Sony) 
135696e2db45SUladzislau Rezki (Sony) 	end = max(end,
135796e2db45SUladzislau Rezki (Sony) 		list_last_entry(&local_pure_list,
135896e2db45SUladzislau Rezki (Sony) 			struct vmap_area, list)->va_end);
1359db64fe02SNick Piggin 
13600574ecd1SChristoph Hellwig 	flush_tlb_kernel_range(start, end);
13614d36e6f8SUladzislau Rezki (Sony) 	resched_threshold = lazy_max_pages() << 1;
1362db64fe02SNick Piggin 
1363e36176beSUladzislau Rezki (Sony) 	spin_lock(&free_vmap_area_lock);
136496e2db45SUladzislau Rezki (Sony) 	list_for_each_entry_safe(va, n_va, &local_pure_list, list) {
13654d36e6f8SUladzislau Rezki (Sony) 		unsigned long nr = (va->va_end - va->va_start) >> PAGE_SHIFT;
13663c5c3cfbSDaniel Axtens 		unsigned long orig_start = va->va_start;
13673c5c3cfbSDaniel Axtens 		unsigned long orig_end = va->va_end;
1368763b218dSJoel Fernandes 
1369dd3b8353SUladzislau Rezki (Sony) 		/*
1370dd3b8353SUladzislau Rezki (Sony) 		 * Finally insert or merge lazily-freed area. It is
1371dd3b8353SUladzislau Rezki (Sony) 		 * detached and there is no need to "unlink" it from
1372dd3b8353SUladzislau Rezki (Sony) 		 * anything.
1373dd3b8353SUladzislau Rezki (Sony) 		 */
137496e2db45SUladzislau Rezki (Sony) 		va = merge_or_add_vmap_area_augment(va, &free_vmap_area_root,
13753c5c3cfbSDaniel Axtens 				&free_vmap_area_list);
13763c5c3cfbSDaniel Axtens 
13779c801f61SUladzislau Rezki (Sony) 		if (!va)
13789c801f61SUladzislau Rezki (Sony) 			continue;
13799c801f61SUladzislau Rezki (Sony) 
13803c5c3cfbSDaniel Axtens 		if (is_vmalloc_or_module_addr((void *)orig_start))
13813c5c3cfbSDaniel Axtens 			kasan_release_vmalloc(orig_start, orig_end,
13823c5c3cfbSDaniel Axtens 					      va->va_start, va->va_end);
1383dd3b8353SUladzislau Rezki (Sony) 
13844d36e6f8SUladzislau Rezki (Sony) 		atomic_long_sub(nr, &vmap_lazy_nr);
138568571be9SUladzislau Rezki (Sony) 
13864d36e6f8SUladzislau Rezki (Sony) 		if (atomic_long_read(&vmap_lazy_nr) < resched_threshold)
1387e36176beSUladzislau Rezki (Sony) 			cond_resched_lock(&free_vmap_area_lock);
1388763b218dSJoel Fernandes 	}
1389e36176beSUladzislau Rezki (Sony) 	spin_unlock(&free_vmap_area_lock);
13900574ecd1SChristoph Hellwig 	return true;
1391db64fe02SNick Piggin }
1392db64fe02SNick Piggin 
1393db64fe02SNick Piggin /*
1394496850e5SNick Piggin  * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
1395496850e5SNick Piggin  * is already purging.
1396496850e5SNick Piggin  */
1397496850e5SNick Piggin static void try_purge_vmap_area_lazy(void)
1398496850e5SNick Piggin {
1399f9e09977SChristoph Hellwig 	if (mutex_trylock(&vmap_purge_lock)) {
14000574ecd1SChristoph Hellwig 		__purge_vmap_area_lazy(ULONG_MAX, 0);
1401f9e09977SChristoph Hellwig 		mutex_unlock(&vmap_purge_lock);
14020574ecd1SChristoph Hellwig 	}
1403496850e5SNick Piggin }
1404496850e5SNick Piggin 
1405496850e5SNick Piggin /*
1406db64fe02SNick Piggin  * Kick off a purge of the outstanding lazy areas.
1407db64fe02SNick Piggin  */
1408db64fe02SNick Piggin static void purge_vmap_area_lazy(void)
1409db64fe02SNick Piggin {
1410f9e09977SChristoph Hellwig 	mutex_lock(&vmap_purge_lock);
14110574ecd1SChristoph Hellwig 	purge_fragmented_blocks_allcpus();
14120574ecd1SChristoph Hellwig 	__purge_vmap_area_lazy(ULONG_MAX, 0);
1413f9e09977SChristoph Hellwig 	mutex_unlock(&vmap_purge_lock);
1414db64fe02SNick Piggin }
1415db64fe02SNick Piggin 
1416db64fe02SNick Piggin /*
141764141da5SJeremy Fitzhardinge  * Free a vmap area, caller ensuring that the area has been unmapped
141864141da5SJeremy Fitzhardinge  * and flush_cache_vunmap had been called for the correct range
141964141da5SJeremy Fitzhardinge  * previously.
1420db64fe02SNick Piggin  */
142164141da5SJeremy Fitzhardinge static void free_vmap_area_noflush(struct vmap_area *va)
1422db64fe02SNick Piggin {
14234d36e6f8SUladzislau Rezki (Sony) 	unsigned long nr_lazy;
142480c4bd7aSChris Wilson 
1425dd3b8353SUladzislau Rezki (Sony) 	spin_lock(&vmap_area_lock);
1426dd3b8353SUladzislau Rezki (Sony) 	unlink_va(va, &vmap_area_root);
1427dd3b8353SUladzislau Rezki (Sony) 	spin_unlock(&vmap_area_lock);
1428dd3b8353SUladzislau Rezki (Sony) 
14294d36e6f8SUladzislau Rezki (Sony) 	nr_lazy = atomic_long_add_return((va->va_end - va->va_start) >>
14304d36e6f8SUladzislau Rezki (Sony) 				PAGE_SHIFT, &vmap_lazy_nr);
143180c4bd7aSChris Wilson 
143296e2db45SUladzislau Rezki (Sony) 	/*
143396e2db45SUladzislau Rezki (Sony) 	 * Merge or place it to the purge tree/list.
143496e2db45SUladzislau Rezki (Sony) 	 */
143596e2db45SUladzislau Rezki (Sony) 	spin_lock(&purge_vmap_area_lock);
143696e2db45SUladzislau Rezki (Sony) 	merge_or_add_vmap_area(va,
143796e2db45SUladzislau Rezki (Sony) 		&purge_vmap_area_root, &purge_vmap_area_list);
143896e2db45SUladzislau Rezki (Sony) 	spin_unlock(&purge_vmap_area_lock);
143980c4bd7aSChris Wilson 
144096e2db45SUladzislau Rezki (Sony) 	/* After this point, we may free va at any time */
144180c4bd7aSChris Wilson 	if (unlikely(nr_lazy > lazy_max_pages()))
1442496850e5SNick Piggin 		try_purge_vmap_area_lazy();
1443db64fe02SNick Piggin }
1444db64fe02SNick Piggin 
1445b29acbdcSNick Piggin /*
1446b29acbdcSNick Piggin  * Free and unmap a vmap area
1447b29acbdcSNick Piggin  */
1448b29acbdcSNick Piggin static void free_unmap_vmap_area(struct vmap_area *va)
1449b29acbdcSNick Piggin {
1450b29acbdcSNick Piggin 	flush_cache_vunmap(va->va_start, va->va_end);
1451855e57a1SChristoph Hellwig 	unmap_kernel_range_noflush(va->va_start, va->va_end - va->va_start);
14528e57f8acSVlastimil Babka 	if (debug_pagealloc_enabled_static())
145382a2e924SChintan Pandya 		flush_tlb_kernel_range(va->va_start, va->va_end);
145482a2e924SChintan Pandya 
1455c8eef01eSChristoph Hellwig 	free_vmap_area_noflush(va);
1456b29acbdcSNick Piggin }
1457b29acbdcSNick Piggin 
1458db64fe02SNick Piggin static struct vmap_area *find_vmap_area(unsigned long addr)
1459db64fe02SNick Piggin {
1460db64fe02SNick Piggin 	struct vmap_area *va;
1461db64fe02SNick Piggin 
1462db64fe02SNick Piggin 	spin_lock(&vmap_area_lock);
1463db64fe02SNick Piggin 	va = __find_vmap_area(addr);
1464db64fe02SNick Piggin 	spin_unlock(&vmap_area_lock);
1465db64fe02SNick Piggin 
1466db64fe02SNick Piggin 	return va;
1467db64fe02SNick Piggin }
1468db64fe02SNick Piggin 
1469db64fe02SNick Piggin /*** Per cpu kva allocator ***/
1470db64fe02SNick Piggin 
1471db64fe02SNick Piggin /*
1472db64fe02SNick Piggin  * vmap space is limited especially on 32 bit architectures. Ensure there is
1473db64fe02SNick Piggin  * room for at least 16 percpu vmap blocks per CPU.
1474db64fe02SNick Piggin  */
1475db64fe02SNick Piggin /*
1476db64fe02SNick Piggin  * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
1477db64fe02SNick Piggin  * to #define VMALLOC_SPACE		(VMALLOC_END-VMALLOC_START). Guess
1478db64fe02SNick Piggin  * instead (we just need a rough idea)
1479db64fe02SNick Piggin  */
1480db64fe02SNick Piggin #if BITS_PER_LONG == 32
1481db64fe02SNick Piggin #define VMALLOC_SPACE		(128UL*1024*1024)
1482db64fe02SNick Piggin #else
1483db64fe02SNick Piggin #define VMALLOC_SPACE		(128UL*1024*1024*1024)
1484db64fe02SNick Piggin #endif
1485db64fe02SNick Piggin 
1486db64fe02SNick Piggin #define VMALLOC_PAGES		(VMALLOC_SPACE / PAGE_SIZE)
1487db64fe02SNick Piggin #define VMAP_MAX_ALLOC		BITS_PER_LONG	/* 256K with 4K pages */
1488db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MAX	1024	/* 4MB with 4K pages */
1489db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MIN	(VMAP_MAX_ALLOC*2)
1490db64fe02SNick Piggin #define VMAP_MIN(x, y)		((x) < (y) ? (x) : (y)) /* can't use min() */
1491db64fe02SNick Piggin #define VMAP_MAX(x, y)		((x) > (y) ? (x) : (y)) /* can't use max() */
1492f982f915SClemens Ladisch #define VMAP_BBMAP_BITS		\
1493f982f915SClemens Ladisch 		VMAP_MIN(VMAP_BBMAP_BITS_MAX,	\
1494db64fe02SNick Piggin 		VMAP_MAX(VMAP_BBMAP_BITS_MIN,	\
1495f982f915SClemens Ladisch 			VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16))
1496db64fe02SNick Piggin 
1497db64fe02SNick Piggin #define VMAP_BLOCK_SIZE		(VMAP_BBMAP_BITS * PAGE_SIZE)
1498db64fe02SNick Piggin 
1499db64fe02SNick Piggin struct vmap_block_queue {
1500db64fe02SNick Piggin 	spinlock_t lock;
1501db64fe02SNick Piggin 	struct list_head free;
1502db64fe02SNick Piggin };
1503db64fe02SNick Piggin 
1504db64fe02SNick Piggin struct vmap_block {
1505db64fe02SNick Piggin 	spinlock_t lock;
1506db64fe02SNick Piggin 	struct vmap_area *va;
1507db64fe02SNick Piggin 	unsigned long free, dirty;
15087d61bfe8SRoman Pen 	unsigned long dirty_min, dirty_max; /*< dirty range */
1509db64fe02SNick Piggin 	struct list_head free_list;
1510db64fe02SNick Piggin 	struct rcu_head rcu_head;
151102b709dfSNick Piggin 	struct list_head purge;
1512db64fe02SNick Piggin };
1513db64fe02SNick Piggin 
1514db64fe02SNick Piggin /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
1515db64fe02SNick Piggin static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
1516db64fe02SNick Piggin 
1517db64fe02SNick Piggin /*
15180f14599cSMatthew Wilcox (Oracle)  * XArray of vmap blocks, indexed by address, to quickly find a vmap block
1519db64fe02SNick Piggin  * in the free path. Could get rid of this if we change the API to return a
1520db64fe02SNick Piggin  * "cookie" from alloc, to be passed to free. But no big deal yet.
1521db64fe02SNick Piggin  */
15220f14599cSMatthew Wilcox (Oracle) static DEFINE_XARRAY(vmap_blocks);
1523db64fe02SNick Piggin 
1524db64fe02SNick Piggin /*
1525db64fe02SNick Piggin  * We should probably have a fallback mechanism to allocate virtual memory
1526db64fe02SNick Piggin  * out of partially filled vmap blocks. However vmap block sizing should be
1527db64fe02SNick Piggin  * fairly reasonable according to the vmalloc size, so it shouldn't be a
1528db64fe02SNick Piggin  * big problem.
1529db64fe02SNick Piggin  */
1530db64fe02SNick Piggin 
1531db64fe02SNick Piggin static unsigned long addr_to_vb_idx(unsigned long addr)
1532db64fe02SNick Piggin {
1533db64fe02SNick Piggin 	addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
1534db64fe02SNick Piggin 	addr /= VMAP_BLOCK_SIZE;
1535db64fe02SNick Piggin 	return addr;
1536db64fe02SNick Piggin }
1537db64fe02SNick Piggin 
1538cf725ce2SRoman Pen static void *vmap_block_vaddr(unsigned long va_start, unsigned long pages_off)
1539cf725ce2SRoman Pen {
1540cf725ce2SRoman Pen 	unsigned long addr;
1541cf725ce2SRoman Pen 
1542cf725ce2SRoman Pen 	addr = va_start + (pages_off << PAGE_SHIFT);
1543cf725ce2SRoman Pen 	BUG_ON(addr_to_vb_idx(addr) != addr_to_vb_idx(va_start));
1544cf725ce2SRoman Pen 	return (void *)addr;
1545cf725ce2SRoman Pen }
1546cf725ce2SRoman Pen 
1547cf725ce2SRoman Pen /**
1548cf725ce2SRoman Pen  * new_vmap_block - allocates new vmap_block and occupies 2^order pages in this
1549cf725ce2SRoman Pen  *                  block. Of course pages number can't exceed VMAP_BBMAP_BITS
1550cf725ce2SRoman Pen  * @order:    how many 2^order pages should be occupied in newly allocated block
1551cf725ce2SRoman Pen  * @gfp_mask: flags for the page level allocator
1552cf725ce2SRoman Pen  *
1553a862f68aSMike Rapoport  * Return: virtual address in a newly allocated block or ERR_PTR(-errno)
1554cf725ce2SRoman Pen  */
1555cf725ce2SRoman Pen static void *new_vmap_block(unsigned int order, gfp_t gfp_mask)
1556db64fe02SNick Piggin {
1557db64fe02SNick Piggin 	struct vmap_block_queue *vbq;
1558db64fe02SNick Piggin 	struct vmap_block *vb;
1559db64fe02SNick Piggin 	struct vmap_area *va;
1560db64fe02SNick Piggin 	unsigned long vb_idx;
1561db64fe02SNick Piggin 	int node, err;
1562cf725ce2SRoman Pen 	void *vaddr;
1563db64fe02SNick Piggin 
1564db64fe02SNick Piggin 	node = numa_node_id();
1565db64fe02SNick Piggin 
1566db64fe02SNick Piggin 	vb = kmalloc_node(sizeof(struct vmap_block),
1567db64fe02SNick Piggin 			gfp_mask & GFP_RECLAIM_MASK, node);
1568db64fe02SNick Piggin 	if (unlikely(!vb))
1569db64fe02SNick Piggin 		return ERR_PTR(-ENOMEM);
1570db64fe02SNick Piggin 
1571db64fe02SNick Piggin 	va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
1572db64fe02SNick Piggin 					VMALLOC_START, VMALLOC_END,
1573db64fe02SNick Piggin 					node, gfp_mask);
1574ddf9c6d4STobias Klauser 	if (IS_ERR(va)) {
1575db64fe02SNick Piggin 		kfree(vb);
1576e7d86340SJulia Lawall 		return ERR_CAST(va);
1577db64fe02SNick Piggin 	}
1578db64fe02SNick Piggin 
1579cf725ce2SRoman Pen 	vaddr = vmap_block_vaddr(va->va_start, 0);
1580db64fe02SNick Piggin 	spin_lock_init(&vb->lock);
1581db64fe02SNick Piggin 	vb->va = va;
1582cf725ce2SRoman Pen 	/* At least something should be left free */
1583cf725ce2SRoman Pen 	BUG_ON(VMAP_BBMAP_BITS <= (1UL << order));
1584cf725ce2SRoman Pen 	vb->free = VMAP_BBMAP_BITS - (1UL << order);
1585db64fe02SNick Piggin 	vb->dirty = 0;
15867d61bfe8SRoman Pen 	vb->dirty_min = VMAP_BBMAP_BITS;
15877d61bfe8SRoman Pen 	vb->dirty_max = 0;
1588db64fe02SNick Piggin 	INIT_LIST_HEAD(&vb->free_list);
1589db64fe02SNick Piggin 
1590db64fe02SNick Piggin 	vb_idx = addr_to_vb_idx(va->va_start);
15910f14599cSMatthew Wilcox (Oracle) 	err = xa_insert(&vmap_blocks, vb_idx, vb, gfp_mask);
15920f14599cSMatthew Wilcox (Oracle) 	if (err) {
15930f14599cSMatthew Wilcox (Oracle) 		kfree(vb);
15940f14599cSMatthew Wilcox (Oracle) 		free_vmap_area(va);
15950f14599cSMatthew Wilcox (Oracle) 		return ERR_PTR(err);
15960f14599cSMatthew Wilcox (Oracle) 	}
1597db64fe02SNick Piggin 
1598db64fe02SNick Piggin 	vbq = &get_cpu_var(vmap_block_queue);
1599db64fe02SNick Piggin 	spin_lock(&vbq->lock);
160068ac546fSRoman Pen 	list_add_tail_rcu(&vb->free_list, &vbq->free);
1601db64fe02SNick Piggin 	spin_unlock(&vbq->lock);
16023f04ba85STejun Heo 	put_cpu_var(vmap_block_queue);
1603db64fe02SNick Piggin 
1604cf725ce2SRoman Pen 	return vaddr;
1605db64fe02SNick Piggin }
1606db64fe02SNick Piggin 
1607db64fe02SNick Piggin static void free_vmap_block(struct vmap_block *vb)
1608db64fe02SNick Piggin {
1609db64fe02SNick Piggin 	struct vmap_block *tmp;
1610db64fe02SNick Piggin 
16110f14599cSMatthew Wilcox (Oracle) 	tmp = xa_erase(&vmap_blocks, addr_to_vb_idx(vb->va->va_start));
1612db64fe02SNick Piggin 	BUG_ON(tmp != vb);
1613db64fe02SNick Piggin 
161464141da5SJeremy Fitzhardinge 	free_vmap_area_noflush(vb->va);
161522a3c7d1SLai Jiangshan 	kfree_rcu(vb, rcu_head);
1616db64fe02SNick Piggin }
1617db64fe02SNick Piggin 
161802b709dfSNick Piggin static void purge_fragmented_blocks(int cpu)
161902b709dfSNick Piggin {
162002b709dfSNick Piggin 	LIST_HEAD(purge);
162102b709dfSNick Piggin 	struct vmap_block *vb;
162202b709dfSNick Piggin 	struct vmap_block *n_vb;
162302b709dfSNick Piggin 	struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
162402b709dfSNick Piggin 
162502b709dfSNick Piggin 	rcu_read_lock();
162602b709dfSNick Piggin 	list_for_each_entry_rcu(vb, &vbq->free, free_list) {
162702b709dfSNick Piggin 
162802b709dfSNick Piggin 		if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS))
162902b709dfSNick Piggin 			continue;
163002b709dfSNick Piggin 
163102b709dfSNick Piggin 		spin_lock(&vb->lock);
163202b709dfSNick Piggin 		if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) {
163302b709dfSNick Piggin 			vb->free = 0; /* prevent further allocs after releasing lock */
163402b709dfSNick Piggin 			vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */
16357d61bfe8SRoman Pen 			vb->dirty_min = 0;
16367d61bfe8SRoman Pen 			vb->dirty_max = VMAP_BBMAP_BITS;
163702b709dfSNick Piggin 			spin_lock(&vbq->lock);
163802b709dfSNick Piggin 			list_del_rcu(&vb->free_list);
163902b709dfSNick Piggin 			spin_unlock(&vbq->lock);
164002b709dfSNick Piggin 			spin_unlock(&vb->lock);
164102b709dfSNick Piggin 			list_add_tail(&vb->purge, &purge);
164202b709dfSNick Piggin 		} else
164302b709dfSNick Piggin 			spin_unlock(&vb->lock);
164402b709dfSNick Piggin 	}
164502b709dfSNick Piggin 	rcu_read_unlock();
164602b709dfSNick Piggin 
164702b709dfSNick Piggin 	list_for_each_entry_safe(vb, n_vb, &purge, purge) {
164802b709dfSNick Piggin 		list_del(&vb->purge);
164902b709dfSNick Piggin 		free_vmap_block(vb);
165002b709dfSNick Piggin 	}
165102b709dfSNick Piggin }
165202b709dfSNick Piggin 
165302b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void)
165402b709dfSNick Piggin {
165502b709dfSNick Piggin 	int cpu;
165602b709dfSNick Piggin 
165702b709dfSNick Piggin 	for_each_possible_cpu(cpu)
165802b709dfSNick Piggin 		purge_fragmented_blocks(cpu);
165902b709dfSNick Piggin }
166002b709dfSNick Piggin 
1661db64fe02SNick Piggin static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
1662db64fe02SNick Piggin {
1663db64fe02SNick Piggin 	struct vmap_block_queue *vbq;
1664db64fe02SNick Piggin 	struct vmap_block *vb;
1665cf725ce2SRoman Pen 	void *vaddr = NULL;
1666db64fe02SNick Piggin 	unsigned int order;
1667db64fe02SNick Piggin 
1668891c49abSAlexander Kuleshov 	BUG_ON(offset_in_page(size));
1669db64fe02SNick Piggin 	BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
1670aa91c4d8SJan Kara 	if (WARN_ON(size == 0)) {
1671aa91c4d8SJan Kara 		/*
1672aa91c4d8SJan Kara 		 * Allocating 0 bytes isn't what caller wants since
1673aa91c4d8SJan Kara 		 * get_order(0) returns funny result. Just warn and terminate
1674aa91c4d8SJan Kara 		 * early.
1675aa91c4d8SJan Kara 		 */
1676aa91c4d8SJan Kara 		return NULL;
1677aa91c4d8SJan Kara 	}
1678db64fe02SNick Piggin 	order = get_order(size);
1679db64fe02SNick Piggin 
1680db64fe02SNick Piggin 	rcu_read_lock();
1681db64fe02SNick Piggin 	vbq = &get_cpu_var(vmap_block_queue);
1682db64fe02SNick Piggin 	list_for_each_entry_rcu(vb, &vbq->free, free_list) {
1683cf725ce2SRoman Pen 		unsigned long pages_off;
1684db64fe02SNick Piggin 
1685db64fe02SNick Piggin 		spin_lock(&vb->lock);
1686cf725ce2SRoman Pen 		if (vb->free < (1UL << order)) {
1687cf725ce2SRoman Pen 			spin_unlock(&vb->lock);
1688cf725ce2SRoman Pen 			continue;
1689cf725ce2SRoman Pen 		}
169002b709dfSNick Piggin 
1691cf725ce2SRoman Pen 		pages_off = VMAP_BBMAP_BITS - vb->free;
1692cf725ce2SRoman Pen 		vaddr = vmap_block_vaddr(vb->va->va_start, pages_off);
1693db64fe02SNick Piggin 		vb->free -= 1UL << order;
1694db64fe02SNick Piggin 		if (vb->free == 0) {
1695db64fe02SNick Piggin 			spin_lock(&vbq->lock);
1696de560423SNick Piggin 			list_del_rcu(&vb->free_list);
1697db64fe02SNick Piggin 			spin_unlock(&vbq->lock);
1698db64fe02SNick Piggin 		}
1699cf725ce2SRoman Pen 
1700db64fe02SNick Piggin 		spin_unlock(&vb->lock);
1701db64fe02SNick Piggin 		break;
1702db64fe02SNick Piggin 	}
170302b709dfSNick Piggin 
17043f04ba85STejun Heo 	put_cpu_var(vmap_block_queue);
1705db64fe02SNick Piggin 	rcu_read_unlock();
1706db64fe02SNick Piggin 
1707cf725ce2SRoman Pen 	/* Allocate new block if nothing was found */
1708cf725ce2SRoman Pen 	if (!vaddr)
1709cf725ce2SRoman Pen 		vaddr = new_vmap_block(order, gfp_mask);
1710db64fe02SNick Piggin 
1711cf725ce2SRoman Pen 	return vaddr;
1712db64fe02SNick Piggin }
1713db64fe02SNick Piggin 
171478a0e8c4SChristoph Hellwig static void vb_free(unsigned long addr, unsigned long size)
1715db64fe02SNick Piggin {
1716db64fe02SNick Piggin 	unsigned long offset;
1717db64fe02SNick Piggin 	unsigned int order;
1718db64fe02SNick Piggin 	struct vmap_block *vb;
1719db64fe02SNick Piggin 
1720891c49abSAlexander Kuleshov 	BUG_ON(offset_in_page(size));
1721db64fe02SNick Piggin 	BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
1722b29acbdcSNick Piggin 
172378a0e8c4SChristoph Hellwig 	flush_cache_vunmap(addr, addr + size);
1724b29acbdcSNick Piggin 
1725db64fe02SNick Piggin 	order = get_order(size);
172678a0e8c4SChristoph Hellwig 	offset = (addr & (VMAP_BLOCK_SIZE - 1)) >> PAGE_SHIFT;
17270f14599cSMatthew Wilcox (Oracle) 	vb = xa_load(&vmap_blocks, addr_to_vb_idx(addr));
1728db64fe02SNick Piggin 
1729b521c43fSChristoph Hellwig 	unmap_kernel_range_noflush(addr, size);
173064141da5SJeremy Fitzhardinge 
17318e57f8acSVlastimil Babka 	if (debug_pagealloc_enabled_static())
173278a0e8c4SChristoph Hellwig 		flush_tlb_kernel_range(addr, addr + size);
173382a2e924SChintan Pandya 
1734db64fe02SNick Piggin 	spin_lock(&vb->lock);
17357d61bfe8SRoman Pen 
17367d61bfe8SRoman Pen 	/* Expand dirty range */
17377d61bfe8SRoman Pen 	vb->dirty_min = min(vb->dirty_min, offset);
17387d61bfe8SRoman Pen 	vb->dirty_max = max(vb->dirty_max, offset + (1UL << order));
1739d086817dSMinChan Kim 
1740db64fe02SNick Piggin 	vb->dirty += 1UL << order;
1741db64fe02SNick Piggin 	if (vb->dirty == VMAP_BBMAP_BITS) {
1742de560423SNick Piggin 		BUG_ON(vb->free);
1743db64fe02SNick Piggin 		spin_unlock(&vb->lock);
1744db64fe02SNick Piggin 		free_vmap_block(vb);
1745db64fe02SNick Piggin 	} else
1746db64fe02SNick Piggin 		spin_unlock(&vb->lock);
1747db64fe02SNick Piggin }
1748db64fe02SNick Piggin 
1749868b104dSRick Edgecombe static void _vm_unmap_aliases(unsigned long start, unsigned long end, int flush)
1750db64fe02SNick Piggin {
1751db64fe02SNick Piggin 	int cpu;
1752db64fe02SNick Piggin 
17539b463334SJeremy Fitzhardinge 	if (unlikely(!vmap_initialized))
17549b463334SJeremy Fitzhardinge 		return;
17559b463334SJeremy Fitzhardinge 
17565803ed29SChristoph Hellwig 	might_sleep();
17575803ed29SChristoph Hellwig 
1758db64fe02SNick Piggin 	for_each_possible_cpu(cpu) {
1759db64fe02SNick Piggin 		struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
1760db64fe02SNick Piggin 		struct vmap_block *vb;
1761db64fe02SNick Piggin 
1762db64fe02SNick Piggin 		rcu_read_lock();
1763db64fe02SNick Piggin 		list_for_each_entry_rcu(vb, &vbq->free, free_list) {
1764db64fe02SNick Piggin 			spin_lock(&vb->lock);
17657d61bfe8SRoman Pen 			if (vb->dirty) {
17667d61bfe8SRoman Pen 				unsigned long va_start = vb->va->va_start;
1767db64fe02SNick Piggin 				unsigned long s, e;
1768b136be5eSJoonsoo Kim 
17697d61bfe8SRoman Pen 				s = va_start + (vb->dirty_min << PAGE_SHIFT);
17707d61bfe8SRoman Pen 				e = va_start + (vb->dirty_max << PAGE_SHIFT);
1771db64fe02SNick Piggin 
17727d61bfe8SRoman Pen 				start = min(s, start);
17737d61bfe8SRoman Pen 				end   = max(e, end);
17747d61bfe8SRoman Pen 
1775db64fe02SNick Piggin 				flush = 1;
1776db64fe02SNick Piggin 			}
1777db64fe02SNick Piggin 			spin_unlock(&vb->lock);
1778db64fe02SNick Piggin 		}
1779db64fe02SNick Piggin 		rcu_read_unlock();
1780db64fe02SNick Piggin 	}
1781db64fe02SNick Piggin 
1782f9e09977SChristoph Hellwig 	mutex_lock(&vmap_purge_lock);
17830574ecd1SChristoph Hellwig 	purge_fragmented_blocks_allcpus();
17840574ecd1SChristoph Hellwig 	if (!__purge_vmap_area_lazy(start, end) && flush)
17850574ecd1SChristoph Hellwig 		flush_tlb_kernel_range(start, end);
1786f9e09977SChristoph Hellwig 	mutex_unlock(&vmap_purge_lock);
1787db64fe02SNick Piggin }
1788868b104dSRick Edgecombe 
1789868b104dSRick Edgecombe /**
1790868b104dSRick Edgecombe  * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
1791868b104dSRick Edgecombe  *
1792868b104dSRick Edgecombe  * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
1793868b104dSRick Edgecombe  * to amortize TLB flushing overheads. What this means is that any page you
1794868b104dSRick Edgecombe  * have now, may, in a former life, have been mapped into kernel virtual
1795868b104dSRick Edgecombe  * address by the vmap layer and so there might be some CPUs with TLB entries
1796868b104dSRick Edgecombe  * still referencing that page (additional to the regular 1:1 kernel mapping).
1797868b104dSRick Edgecombe  *
1798868b104dSRick Edgecombe  * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
1799868b104dSRick Edgecombe  * be sure that none of the pages we have control over will have any aliases
1800868b104dSRick Edgecombe  * from the vmap layer.
1801868b104dSRick Edgecombe  */
1802868b104dSRick Edgecombe void vm_unmap_aliases(void)
1803868b104dSRick Edgecombe {
1804868b104dSRick Edgecombe 	unsigned long start = ULONG_MAX, end = 0;
1805868b104dSRick Edgecombe 	int flush = 0;
1806868b104dSRick Edgecombe 
1807868b104dSRick Edgecombe 	_vm_unmap_aliases(start, end, flush);
1808868b104dSRick Edgecombe }
1809db64fe02SNick Piggin EXPORT_SYMBOL_GPL(vm_unmap_aliases);
1810db64fe02SNick Piggin 
1811db64fe02SNick Piggin /**
1812db64fe02SNick Piggin  * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
1813db64fe02SNick Piggin  * @mem: the pointer returned by vm_map_ram
1814db64fe02SNick Piggin  * @count: the count passed to that vm_map_ram call (cannot unmap partial)
1815db64fe02SNick Piggin  */
1816db64fe02SNick Piggin void vm_unmap_ram(const void *mem, unsigned int count)
1817db64fe02SNick Piggin {
181865ee03c4SGuillermo Julián Moreno 	unsigned long size = (unsigned long)count << PAGE_SHIFT;
1819db64fe02SNick Piggin 	unsigned long addr = (unsigned long)mem;
18209c3acf60SChristoph Hellwig 	struct vmap_area *va;
1821db64fe02SNick Piggin 
18225803ed29SChristoph Hellwig 	might_sleep();
1823db64fe02SNick Piggin 	BUG_ON(!addr);
1824db64fe02SNick Piggin 	BUG_ON(addr < VMALLOC_START);
1825db64fe02SNick Piggin 	BUG_ON(addr > VMALLOC_END);
1826a1c0b1a0SShawn Lin 	BUG_ON(!PAGE_ALIGNED(addr));
1827db64fe02SNick Piggin 
1828d98c9e83SAndrey Ryabinin 	kasan_poison_vmalloc(mem, size);
1829d98c9e83SAndrey Ryabinin 
18309c3acf60SChristoph Hellwig 	if (likely(count <= VMAP_MAX_ALLOC)) {
183105e3ff95SChintan Pandya 		debug_check_no_locks_freed(mem, size);
183278a0e8c4SChristoph Hellwig 		vb_free(addr, size);
18339c3acf60SChristoph Hellwig 		return;
18349c3acf60SChristoph Hellwig 	}
18359c3acf60SChristoph Hellwig 
18369c3acf60SChristoph Hellwig 	va = find_vmap_area(addr);
18379c3acf60SChristoph Hellwig 	BUG_ON(!va);
183805e3ff95SChintan Pandya 	debug_check_no_locks_freed((void *)va->va_start,
183905e3ff95SChintan Pandya 				    (va->va_end - va->va_start));
18409c3acf60SChristoph Hellwig 	free_unmap_vmap_area(va);
1841db64fe02SNick Piggin }
1842db64fe02SNick Piggin EXPORT_SYMBOL(vm_unmap_ram);
1843db64fe02SNick Piggin 
1844db64fe02SNick Piggin /**
1845db64fe02SNick Piggin  * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
1846db64fe02SNick Piggin  * @pages: an array of pointers to the pages to be mapped
1847db64fe02SNick Piggin  * @count: number of pages
1848db64fe02SNick Piggin  * @node: prefer to allocate data structures on this node
1849e99c97adSRandy Dunlap  *
185036437638SGioh Kim  * If you use this function for less than VMAP_MAX_ALLOC pages, it could be
185136437638SGioh Kim  * faster than vmap so it's good.  But if you mix long-life and short-life
185236437638SGioh Kim  * objects with vm_map_ram(), it could consume lots of address space through
185336437638SGioh Kim  * fragmentation (especially on a 32bit machine).  You could see failures in
185436437638SGioh Kim  * the end.  Please use this function for short-lived objects.
185536437638SGioh Kim  *
1856e99c97adSRandy Dunlap  * Returns: a pointer to the address that has been mapped, or %NULL on failure
1857db64fe02SNick Piggin  */
1858d4efd79aSChristoph Hellwig void *vm_map_ram(struct page **pages, unsigned int count, int node)
1859db64fe02SNick Piggin {
186065ee03c4SGuillermo Julián Moreno 	unsigned long size = (unsigned long)count << PAGE_SHIFT;
1861db64fe02SNick Piggin 	unsigned long addr;
1862db64fe02SNick Piggin 	void *mem;
1863db64fe02SNick Piggin 
1864db64fe02SNick Piggin 	if (likely(count <= VMAP_MAX_ALLOC)) {
1865db64fe02SNick Piggin 		mem = vb_alloc(size, GFP_KERNEL);
1866db64fe02SNick Piggin 		if (IS_ERR(mem))
1867db64fe02SNick Piggin 			return NULL;
1868db64fe02SNick Piggin 		addr = (unsigned long)mem;
1869db64fe02SNick Piggin 	} else {
1870db64fe02SNick Piggin 		struct vmap_area *va;
1871db64fe02SNick Piggin 		va = alloc_vmap_area(size, PAGE_SIZE,
1872db64fe02SNick Piggin 				VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
1873db64fe02SNick Piggin 		if (IS_ERR(va))
1874db64fe02SNick Piggin 			return NULL;
1875db64fe02SNick Piggin 
1876db64fe02SNick Piggin 		addr = va->va_start;
1877db64fe02SNick Piggin 		mem = (void *)addr;
1878db64fe02SNick Piggin 	}
1879d98c9e83SAndrey Ryabinin 
1880d98c9e83SAndrey Ryabinin 	kasan_unpoison_vmalloc(mem, size);
1881d98c9e83SAndrey Ryabinin 
1882d4efd79aSChristoph Hellwig 	if (map_kernel_range(addr, size, PAGE_KERNEL, pages) < 0) {
1883db64fe02SNick Piggin 		vm_unmap_ram(mem, count);
1884db64fe02SNick Piggin 		return NULL;
1885db64fe02SNick Piggin 	}
1886db64fe02SNick Piggin 	return mem;
1887db64fe02SNick Piggin }
1888db64fe02SNick Piggin EXPORT_SYMBOL(vm_map_ram);
1889db64fe02SNick Piggin 
18904341fa45SJoonsoo Kim static struct vm_struct *vmlist __initdata;
189192eac168SMike Rapoport 
1892f0aa6617STejun Heo /**
1893be9b7335SNicolas Pitre  * vm_area_add_early - add vmap area early during boot
1894be9b7335SNicolas Pitre  * @vm: vm_struct to add
1895be9b7335SNicolas Pitre  *
1896be9b7335SNicolas Pitre  * This function is used to add fixed kernel vm area to vmlist before
1897be9b7335SNicolas Pitre  * vmalloc_init() is called.  @vm->addr, @vm->size, and @vm->flags
1898be9b7335SNicolas Pitre  * should contain proper values and the other fields should be zero.
1899be9b7335SNicolas Pitre  *
1900be9b7335SNicolas Pitre  * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1901be9b7335SNicolas Pitre  */
1902be9b7335SNicolas Pitre void __init vm_area_add_early(struct vm_struct *vm)
1903be9b7335SNicolas Pitre {
1904be9b7335SNicolas Pitre 	struct vm_struct *tmp, **p;
1905be9b7335SNicolas Pitre 
1906be9b7335SNicolas Pitre 	BUG_ON(vmap_initialized);
1907be9b7335SNicolas Pitre 	for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
1908be9b7335SNicolas Pitre 		if (tmp->addr >= vm->addr) {
1909be9b7335SNicolas Pitre 			BUG_ON(tmp->addr < vm->addr + vm->size);
1910be9b7335SNicolas Pitre 			break;
1911be9b7335SNicolas Pitre 		} else
1912be9b7335SNicolas Pitre 			BUG_ON(tmp->addr + tmp->size > vm->addr);
1913be9b7335SNicolas Pitre 	}
1914be9b7335SNicolas Pitre 	vm->next = *p;
1915be9b7335SNicolas Pitre 	*p = vm;
1916be9b7335SNicolas Pitre }
1917be9b7335SNicolas Pitre 
1918be9b7335SNicolas Pitre /**
1919f0aa6617STejun Heo  * vm_area_register_early - register vmap area early during boot
1920f0aa6617STejun Heo  * @vm: vm_struct to register
1921c0c0a293STejun Heo  * @align: requested alignment
1922f0aa6617STejun Heo  *
1923f0aa6617STejun Heo  * This function is used to register kernel vm area before
1924f0aa6617STejun Heo  * vmalloc_init() is called.  @vm->size and @vm->flags should contain
1925f0aa6617STejun Heo  * proper values on entry and other fields should be zero.  On return,
1926f0aa6617STejun Heo  * vm->addr contains the allocated address.
1927f0aa6617STejun Heo  *
1928f0aa6617STejun Heo  * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1929f0aa6617STejun Heo  */
1930c0c0a293STejun Heo void __init vm_area_register_early(struct vm_struct *vm, size_t align)
1931f0aa6617STejun Heo {
1932f0aa6617STejun Heo 	static size_t vm_init_off __initdata;
1933c0c0a293STejun Heo 	unsigned long addr;
1934f0aa6617STejun Heo 
1935c0c0a293STejun Heo 	addr = ALIGN(VMALLOC_START + vm_init_off, align);
1936c0c0a293STejun Heo 	vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START;
1937c0c0a293STejun Heo 
1938c0c0a293STejun Heo 	vm->addr = (void *)addr;
1939f0aa6617STejun Heo 
1940be9b7335SNicolas Pitre 	vm_area_add_early(vm);
1941f0aa6617STejun Heo }
1942f0aa6617STejun Heo 
194368ad4a33SUladzislau Rezki (Sony) static void vmap_init_free_space(void)
194468ad4a33SUladzislau Rezki (Sony) {
194568ad4a33SUladzislau Rezki (Sony) 	unsigned long vmap_start = 1;
194668ad4a33SUladzislau Rezki (Sony) 	const unsigned long vmap_end = ULONG_MAX;
194768ad4a33SUladzislau Rezki (Sony) 	struct vmap_area *busy, *free;
194868ad4a33SUladzislau Rezki (Sony) 
194968ad4a33SUladzislau Rezki (Sony) 	/*
195068ad4a33SUladzislau Rezki (Sony) 	 *     B     F     B     B     B     F
195168ad4a33SUladzislau Rezki (Sony) 	 * -|-----|.....|-----|-----|-----|.....|-
195268ad4a33SUladzislau Rezki (Sony) 	 *  |           The KVA space           |
195368ad4a33SUladzislau Rezki (Sony) 	 *  |<--------------------------------->|
195468ad4a33SUladzislau Rezki (Sony) 	 */
195568ad4a33SUladzislau Rezki (Sony) 	list_for_each_entry(busy, &vmap_area_list, list) {
195668ad4a33SUladzislau Rezki (Sony) 		if (busy->va_start - vmap_start > 0) {
195768ad4a33SUladzislau Rezki (Sony) 			free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
195868ad4a33SUladzislau Rezki (Sony) 			if (!WARN_ON_ONCE(!free)) {
195968ad4a33SUladzislau Rezki (Sony) 				free->va_start = vmap_start;
196068ad4a33SUladzislau Rezki (Sony) 				free->va_end = busy->va_start;
196168ad4a33SUladzislau Rezki (Sony) 
196268ad4a33SUladzislau Rezki (Sony) 				insert_vmap_area_augment(free, NULL,
196368ad4a33SUladzislau Rezki (Sony) 					&free_vmap_area_root,
196468ad4a33SUladzislau Rezki (Sony) 						&free_vmap_area_list);
196568ad4a33SUladzislau Rezki (Sony) 			}
196668ad4a33SUladzislau Rezki (Sony) 		}
196768ad4a33SUladzislau Rezki (Sony) 
196868ad4a33SUladzislau Rezki (Sony) 		vmap_start = busy->va_end;
196968ad4a33SUladzislau Rezki (Sony) 	}
197068ad4a33SUladzislau Rezki (Sony) 
197168ad4a33SUladzislau Rezki (Sony) 	if (vmap_end - vmap_start > 0) {
197268ad4a33SUladzislau Rezki (Sony) 		free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
197368ad4a33SUladzislau Rezki (Sony) 		if (!WARN_ON_ONCE(!free)) {
197468ad4a33SUladzislau Rezki (Sony) 			free->va_start = vmap_start;
197568ad4a33SUladzislau Rezki (Sony) 			free->va_end = vmap_end;
197668ad4a33SUladzislau Rezki (Sony) 
197768ad4a33SUladzislau Rezki (Sony) 			insert_vmap_area_augment(free, NULL,
197868ad4a33SUladzislau Rezki (Sony) 				&free_vmap_area_root,
197968ad4a33SUladzislau Rezki (Sony) 					&free_vmap_area_list);
198068ad4a33SUladzislau Rezki (Sony) 		}
198168ad4a33SUladzislau Rezki (Sony) 	}
198268ad4a33SUladzislau Rezki (Sony) }
198368ad4a33SUladzislau Rezki (Sony) 
1984db64fe02SNick Piggin void __init vmalloc_init(void)
1985db64fe02SNick Piggin {
1986822c18f2SIvan Kokshaysky 	struct vmap_area *va;
1987822c18f2SIvan Kokshaysky 	struct vm_struct *tmp;
1988db64fe02SNick Piggin 	int i;
1989db64fe02SNick Piggin 
199068ad4a33SUladzislau Rezki (Sony) 	/*
199168ad4a33SUladzislau Rezki (Sony) 	 * Create the cache for vmap_area objects.
199268ad4a33SUladzislau Rezki (Sony) 	 */
199368ad4a33SUladzislau Rezki (Sony) 	vmap_area_cachep = KMEM_CACHE(vmap_area, SLAB_PANIC);
199468ad4a33SUladzislau Rezki (Sony) 
1995db64fe02SNick Piggin 	for_each_possible_cpu(i) {
1996db64fe02SNick Piggin 		struct vmap_block_queue *vbq;
199732fcfd40SAl Viro 		struct vfree_deferred *p;
1998db64fe02SNick Piggin 
1999db64fe02SNick Piggin 		vbq = &per_cpu(vmap_block_queue, i);
2000db64fe02SNick Piggin 		spin_lock_init(&vbq->lock);
2001db64fe02SNick Piggin 		INIT_LIST_HEAD(&vbq->free);
200232fcfd40SAl Viro 		p = &per_cpu(vfree_deferred, i);
200332fcfd40SAl Viro 		init_llist_head(&p->list);
200432fcfd40SAl Viro 		INIT_WORK(&p->wq, free_work);
2005db64fe02SNick Piggin 	}
20069b463334SJeremy Fitzhardinge 
2007822c18f2SIvan Kokshaysky 	/* Import existing vmlist entries. */
2008822c18f2SIvan Kokshaysky 	for (tmp = vmlist; tmp; tmp = tmp->next) {
200968ad4a33SUladzislau Rezki (Sony) 		va = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
201068ad4a33SUladzislau Rezki (Sony) 		if (WARN_ON_ONCE(!va))
201168ad4a33SUladzislau Rezki (Sony) 			continue;
201268ad4a33SUladzislau Rezki (Sony) 
2013822c18f2SIvan Kokshaysky 		va->va_start = (unsigned long)tmp->addr;
2014822c18f2SIvan Kokshaysky 		va->va_end = va->va_start + tmp->size;
2015dbda591dSKyongHo 		va->vm = tmp;
201668ad4a33SUladzislau Rezki (Sony) 		insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
2017822c18f2SIvan Kokshaysky 	}
2018ca23e405STejun Heo 
201968ad4a33SUladzislau Rezki (Sony) 	/*
202068ad4a33SUladzislau Rezki (Sony) 	 * Now we can initialize a free vmap space.
202168ad4a33SUladzislau Rezki (Sony) 	 */
202268ad4a33SUladzislau Rezki (Sony) 	vmap_init_free_space();
20239b463334SJeremy Fitzhardinge 	vmap_initialized = true;
2024db64fe02SNick Piggin }
2025db64fe02SNick Piggin 
20268fc48985STejun Heo /**
20278fc48985STejun Heo  * unmap_kernel_range - unmap kernel VM area and flush cache and TLB
20288fc48985STejun Heo  * @addr: start of the VM area to unmap
20298fc48985STejun Heo  * @size: size of the VM area to unmap
20308fc48985STejun Heo  *
20318fc48985STejun Heo  * Similar to unmap_kernel_range_noflush() but flushes vcache before
20328fc48985STejun Heo  * the unmapping and tlb after.
20338fc48985STejun Heo  */
2034db64fe02SNick Piggin void unmap_kernel_range(unsigned long addr, unsigned long size)
2035db64fe02SNick Piggin {
2036db64fe02SNick Piggin 	unsigned long end = addr + size;
2037f6fcba70STejun Heo 
2038f6fcba70STejun Heo 	flush_cache_vunmap(addr, end);
2039b521c43fSChristoph Hellwig 	unmap_kernel_range_noflush(addr, size);
2040db64fe02SNick Piggin 	flush_tlb_kernel_range(addr, end);
2041db64fe02SNick Piggin }
2042db64fe02SNick Piggin 
2043e36176beSUladzislau Rezki (Sony) static inline void setup_vmalloc_vm_locked(struct vm_struct *vm,
2044e36176beSUladzislau Rezki (Sony) 	struct vmap_area *va, unsigned long flags, const void *caller)
2045cf88c790STejun Heo {
2046cf88c790STejun Heo 	vm->flags = flags;
2047cf88c790STejun Heo 	vm->addr = (void *)va->va_start;
2048cf88c790STejun Heo 	vm->size = va->va_end - va->va_start;
2049cf88c790STejun Heo 	vm->caller = caller;
2050db1aecafSMinchan Kim 	va->vm = vm;
2051e36176beSUladzislau Rezki (Sony) }
2052e36176beSUladzislau Rezki (Sony) 
2053e36176beSUladzislau Rezki (Sony) static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
2054e36176beSUladzislau Rezki (Sony) 			      unsigned long flags, const void *caller)
2055e36176beSUladzislau Rezki (Sony) {
2056e36176beSUladzislau Rezki (Sony) 	spin_lock(&vmap_area_lock);
2057e36176beSUladzislau Rezki (Sony) 	setup_vmalloc_vm_locked(vm, va, flags, caller);
2058c69480adSJoonsoo Kim 	spin_unlock(&vmap_area_lock);
2059f5252e00SMitsuo Hayasaka }
2060cf88c790STejun Heo 
206120fc02b4SZhang Yanfei static void clear_vm_uninitialized_flag(struct vm_struct *vm)
2062f5252e00SMitsuo Hayasaka {
2063d4033afdSJoonsoo Kim 	/*
206420fc02b4SZhang Yanfei 	 * Before removing VM_UNINITIALIZED,
2065d4033afdSJoonsoo Kim 	 * we should make sure that vm has proper values.
2066d4033afdSJoonsoo Kim 	 * Pair with smp_rmb() in show_numa_info().
2067d4033afdSJoonsoo Kim 	 */
2068d4033afdSJoonsoo Kim 	smp_wmb();
206920fc02b4SZhang Yanfei 	vm->flags &= ~VM_UNINITIALIZED;
2070cf88c790STejun Heo }
2071cf88c790STejun Heo 
2072db64fe02SNick Piggin static struct vm_struct *__get_vm_area_node(unsigned long size,
20732dca6999SDavid Miller 		unsigned long align, unsigned long flags, unsigned long start,
20745e6cafc8SMarek Szyprowski 		unsigned long end, int node, gfp_t gfp_mask, const void *caller)
2075db64fe02SNick Piggin {
20760006526dSKautuk Consul 	struct vmap_area *va;
2077db64fe02SNick Piggin 	struct vm_struct *area;
2078d98c9e83SAndrey Ryabinin 	unsigned long requested_size = size;
20791da177e4SLinus Torvalds 
208052fd24caSGiridhar Pemmasani 	BUG_ON(in_interrupt());
20811da177e4SLinus Torvalds 	size = PAGE_ALIGN(size);
208231be8309SOGAWA Hirofumi 	if (unlikely(!size))
208331be8309SOGAWA Hirofumi 		return NULL;
20841da177e4SLinus Torvalds 
2085252e5c6eSzijun_hu 	if (flags & VM_IOREMAP)
2086252e5c6eSzijun_hu 		align = 1ul << clamp_t(int, get_count_order_long(size),
2087252e5c6eSzijun_hu 				       PAGE_SHIFT, IOREMAP_MAX_ORDER);
2088252e5c6eSzijun_hu 
2089cf88c790STejun Heo 	area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
20901da177e4SLinus Torvalds 	if (unlikely(!area))
20911da177e4SLinus Torvalds 		return NULL;
20921da177e4SLinus Torvalds 
209371394fe5SAndrey Ryabinin 	if (!(flags & VM_NO_GUARD))
20941da177e4SLinus Torvalds 		size += PAGE_SIZE;
20951da177e4SLinus Torvalds 
2096db64fe02SNick Piggin 	va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
2097db64fe02SNick Piggin 	if (IS_ERR(va)) {
2098db64fe02SNick Piggin 		kfree(area);
2099db64fe02SNick Piggin 		return NULL;
21001da177e4SLinus Torvalds 	}
21011da177e4SLinus Torvalds 
2102d98c9e83SAndrey Ryabinin 	kasan_unpoison_vmalloc((void *)va->va_start, requested_size);
2103f5252e00SMitsuo Hayasaka 
2104d98c9e83SAndrey Ryabinin 	setup_vmalloc_vm(area, va, flags, caller);
21053c5c3cfbSDaniel Axtens 
21061da177e4SLinus Torvalds 	return area;
21071da177e4SLinus Torvalds }
21081da177e4SLinus Torvalds 
2109c2968612SBenjamin Herrenschmidt struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
2110c2968612SBenjamin Herrenschmidt 				       unsigned long start, unsigned long end,
21115e6cafc8SMarek Szyprowski 				       const void *caller)
2112c2968612SBenjamin Herrenschmidt {
211300ef2d2fSDavid Rientjes 	return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE,
211400ef2d2fSDavid Rientjes 				  GFP_KERNEL, caller);
2115c2968612SBenjamin Herrenschmidt }
2116c2968612SBenjamin Herrenschmidt 
21171da177e4SLinus Torvalds /**
2118183ff22bSSimon Arlott  * get_vm_area - reserve a contiguous kernel virtual area
21191da177e4SLinus Torvalds  * @size:	 size of the area
21201da177e4SLinus Torvalds  * @flags:	 %VM_IOREMAP for I/O mappings or VM_ALLOC
21211da177e4SLinus Torvalds  *
21221da177e4SLinus Torvalds  * Search an area of @size in the kernel virtual mapping area,
21231da177e4SLinus Torvalds  * and reserved it for out purposes.  Returns the area descriptor
21241da177e4SLinus Torvalds  * on success or %NULL on failure.
2125a862f68aSMike Rapoport  *
2126a862f68aSMike Rapoport  * Return: the area descriptor on success or %NULL on failure.
21271da177e4SLinus Torvalds  */
21281da177e4SLinus Torvalds struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
21291da177e4SLinus Torvalds {
21302dca6999SDavid Miller 	return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
213100ef2d2fSDavid Rientjes 				  NUMA_NO_NODE, GFP_KERNEL,
213200ef2d2fSDavid Rientjes 				  __builtin_return_address(0));
213323016969SChristoph Lameter }
213423016969SChristoph Lameter 
213523016969SChristoph Lameter struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
21365e6cafc8SMarek Szyprowski 				const void *caller)
213723016969SChristoph Lameter {
21382dca6999SDavid Miller 	return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
213900ef2d2fSDavid Rientjes 				  NUMA_NO_NODE, GFP_KERNEL, caller);
21401da177e4SLinus Torvalds }
21411da177e4SLinus Torvalds 
2142e9da6e99SMarek Szyprowski /**
2143e9da6e99SMarek Szyprowski  * find_vm_area - find a continuous kernel virtual area
2144e9da6e99SMarek Szyprowski  * @addr:	  base address
2145e9da6e99SMarek Szyprowski  *
2146e9da6e99SMarek Szyprowski  * Search for the kernel VM area starting at @addr, and return it.
2147e9da6e99SMarek Szyprowski  * It is up to the caller to do all required locking to keep the returned
2148e9da6e99SMarek Szyprowski  * pointer valid.
2149a862f68aSMike Rapoport  *
215074640617SHui Su  * Return: the area descriptor on success or %NULL on failure.
2151e9da6e99SMarek Szyprowski  */
2152e9da6e99SMarek Szyprowski struct vm_struct *find_vm_area(const void *addr)
215383342314SNick Piggin {
2154db64fe02SNick Piggin 	struct vmap_area *va;
215583342314SNick Piggin 
2156db64fe02SNick Piggin 	va = find_vmap_area((unsigned long)addr);
2157688fcbfcSPengfei Li 	if (!va)
21587856dfebSAndi Kleen 		return NULL;
2159688fcbfcSPengfei Li 
2160688fcbfcSPengfei Li 	return va->vm;
21617856dfebSAndi Kleen }
21627856dfebSAndi Kleen 
21631da177e4SLinus Torvalds /**
2164183ff22bSSimon Arlott  * remove_vm_area - find and remove a continuous kernel virtual area
21651da177e4SLinus Torvalds  * @addr:	    base address
21661da177e4SLinus Torvalds  *
21671da177e4SLinus Torvalds  * Search for the kernel VM area starting at @addr, and remove it.
21681da177e4SLinus Torvalds  * This function returns the found VM area, but using it is NOT safe
21697856dfebSAndi Kleen  * on SMP machines, except for its size or flags.
2170a862f68aSMike Rapoport  *
217174640617SHui Su  * Return: the area descriptor on success or %NULL on failure.
21721da177e4SLinus Torvalds  */
2173b3bdda02SChristoph Lameter struct vm_struct *remove_vm_area(const void *addr)
21741da177e4SLinus Torvalds {
2175db64fe02SNick Piggin 	struct vmap_area *va;
2176db64fe02SNick Piggin 
21775803ed29SChristoph Hellwig 	might_sleep();
21785803ed29SChristoph Hellwig 
2179dd3b8353SUladzislau Rezki (Sony) 	spin_lock(&vmap_area_lock);
2180dd3b8353SUladzislau Rezki (Sony) 	va = __find_vmap_area((unsigned long)addr);
2181688fcbfcSPengfei Li 	if (va && va->vm) {
2182db1aecafSMinchan Kim 		struct vm_struct *vm = va->vm;
2183f5252e00SMitsuo Hayasaka 
2184c69480adSJoonsoo Kim 		va->vm = NULL;
2185c69480adSJoonsoo Kim 		spin_unlock(&vmap_area_lock);
2186c69480adSJoonsoo Kim 
2187a5af5aa8SAndrey Ryabinin 		kasan_free_shadow(vm);
2188dd32c279SKAMEZAWA Hiroyuki 		free_unmap_vmap_area(va);
2189dd32c279SKAMEZAWA Hiroyuki 
2190db64fe02SNick Piggin 		return vm;
2191db64fe02SNick Piggin 	}
2192dd3b8353SUladzislau Rezki (Sony) 
2193dd3b8353SUladzislau Rezki (Sony) 	spin_unlock(&vmap_area_lock);
2194db64fe02SNick Piggin 	return NULL;
21951da177e4SLinus Torvalds }
21961da177e4SLinus Torvalds 
2197868b104dSRick Edgecombe static inline void set_area_direct_map(const struct vm_struct *area,
2198868b104dSRick Edgecombe 				       int (*set_direct_map)(struct page *page))
2199868b104dSRick Edgecombe {
2200868b104dSRick Edgecombe 	int i;
2201868b104dSRick Edgecombe 
2202868b104dSRick Edgecombe 	for (i = 0; i < area->nr_pages; i++)
2203868b104dSRick Edgecombe 		if (page_address(area->pages[i]))
2204868b104dSRick Edgecombe 			set_direct_map(area->pages[i]);
2205868b104dSRick Edgecombe }
2206868b104dSRick Edgecombe 
2207868b104dSRick Edgecombe /* Handle removing and resetting vm mappings related to the vm_struct. */
2208868b104dSRick Edgecombe static void vm_remove_mappings(struct vm_struct *area, int deallocate_pages)
2209868b104dSRick Edgecombe {
2210868b104dSRick Edgecombe 	unsigned long start = ULONG_MAX, end = 0;
2211868b104dSRick Edgecombe 	int flush_reset = area->flags & VM_FLUSH_RESET_PERMS;
221231e67340SRick Edgecombe 	int flush_dmap = 0;
2213868b104dSRick Edgecombe 	int i;
2214868b104dSRick Edgecombe 
2215868b104dSRick Edgecombe 	remove_vm_area(area->addr);
2216868b104dSRick Edgecombe 
2217868b104dSRick Edgecombe 	/* If this is not VM_FLUSH_RESET_PERMS memory, no need for the below. */
2218868b104dSRick Edgecombe 	if (!flush_reset)
2219868b104dSRick Edgecombe 		return;
2220868b104dSRick Edgecombe 
2221868b104dSRick Edgecombe 	/*
2222868b104dSRick Edgecombe 	 * If not deallocating pages, just do the flush of the VM area and
2223868b104dSRick Edgecombe 	 * return.
2224868b104dSRick Edgecombe 	 */
2225868b104dSRick Edgecombe 	if (!deallocate_pages) {
2226868b104dSRick Edgecombe 		vm_unmap_aliases();
2227868b104dSRick Edgecombe 		return;
2228868b104dSRick Edgecombe 	}
2229868b104dSRick Edgecombe 
2230868b104dSRick Edgecombe 	/*
2231868b104dSRick Edgecombe 	 * If execution gets here, flush the vm mapping and reset the direct
2232868b104dSRick Edgecombe 	 * map. Find the start and end range of the direct mappings to make sure
2233868b104dSRick Edgecombe 	 * the vm_unmap_aliases() flush includes the direct map.
2234868b104dSRick Edgecombe 	 */
2235868b104dSRick Edgecombe 	for (i = 0; i < area->nr_pages; i++) {
22368e41f872SRick Edgecombe 		unsigned long addr = (unsigned long)page_address(area->pages[i]);
22378e41f872SRick Edgecombe 		if (addr) {
2238868b104dSRick Edgecombe 			start = min(addr, start);
22398e41f872SRick Edgecombe 			end = max(addr + PAGE_SIZE, end);
224031e67340SRick Edgecombe 			flush_dmap = 1;
2241868b104dSRick Edgecombe 		}
2242868b104dSRick Edgecombe 	}
2243868b104dSRick Edgecombe 
2244868b104dSRick Edgecombe 	/*
2245868b104dSRick Edgecombe 	 * Set direct map to something invalid so that it won't be cached if
2246868b104dSRick Edgecombe 	 * there are any accesses after the TLB flush, then flush the TLB and
2247868b104dSRick Edgecombe 	 * reset the direct map permissions to the default.
2248868b104dSRick Edgecombe 	 */
2249868b104dSRick Edgecombe 	set_area_direct_map(area, set_direct_map_invalid_noflush);
225031e67340SRick Edgecombe 	_vm_unmap_aliases(start, end, flush_dmap);
2251868b104dSRick Edgecombe 	set_area_direct_map(area, set_direct_map_default_noflush);
2252868b104dSRick Edgecombe }
2253868b104dSRick Edgecombe 
2254b3bdda02SChristoph Lameter static void __vunmap(const void *addr, int deallocate_pages)
22551da177e4SLinus Torvalds {
22561da177e4SLinus Torvalds 	struct vm_struct *area;
22571da177e4SLinus Torvalds 
22581da177e4SLinus Torvalds 	if (!addr)
22591da177e4SLinus Torvalds 		return;
22601da177e4SLinus Torvalds 
2261e69e9d4aSHATAYAMA Daisuke 	if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n",
2262ab15d9b4SDan Carpenter 			addr))
22631da177e4SLinus Torvalds 		return;
22641da177e4SLinus Torvalds 
22656ade2032SLiviu Dudau 	area = find_vm_area(addr);
22661da177e4SLinus Torvalds 	if (unlikely(!area)) {
22674c8573e2SArjan van de Ven 		WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
22681da177e4SLinus Torvalds 				addr);
22691da177e4SLinus Torvalds 		return;
22701da177e4SLinus Torvalds 	}
22711da177e4SLinus Torvalds 
227205e3ff95SChintan Pandya 	debug_check_no_locks_freed(area->addr, get_vm_area_size(area));
227305e3ff95SChintan Pandya 	debug_check_no_obj_freed(area->addr, get_vm_area_size(area));
22749a11b49aSIngo Molnar 
2275c041098cSVincenzo Frascino 	kasan_poison_vmalloc(area->addr, get_vm_area_size(area));
22763c5c3cfbSDaniel Axtens 
2277868b104dSRick Edgecombe 	vm_remove_mappings(area, deallocate_pages);
2278868b104dSRick Edgecombe 
22791da177e4SLinus Torvalds 	if (deallocate_pages) {
22801da177e4SLinus Torvalds 		int i;
22811da177e4SLinus Torvalds 
22821da177e4SLinus Torvalds 		for (i = 0; i < area->nr_pages; i++) {
2283bf53d6f8SChristoph Lameter 			struct page *page = area->pages[i];
2284bf53d6f8SChristoph Lameter 
2285bf53d6f8SChristoph Lameter 			BUG_ON(!page);
22864949148aSVladimir Davydov 			__free_pages(page, 0);
22871da177e4SLinus Torvalds 		}
228897105f0aSRoman Gushchin 		atomic_long_sub(area->nr_pages, &nr_vmalloc_pages);
22891da177e4SLinus Torvalds 
2290244d63eeSDavid Rientjes 		kvfree(area->pages);
22911da177e4SLinus Torvalds 	}
22921da177e4SLinus Torvalds 
22931da177e4SLinus Torvalds 	kfree(area);
22941da177e4SLinus Torvalds }
22951da177e4SLinus Torvalds 
2296bf22e37aSAndrey Ryabinin static inline void __vfree_deferred(const void *addr)
2297bf22e37aSAndrey Ryabinin {
2298bf22e37aSAndrey Ryabinin 	/*
2299bf22e37aSAndrey Ryabinin 	 * Use raw_cpu_ptr() because this can be called from preemptible
2300bf22e37aSAndrey Ryabinin 	 * context. Preemption is absolutely fine here, because the llist_add()
2301bf22e37aSAndrey Ryabinin 	 * implementation is lockless, so it works even if we are adding to
230273221d88SJeongtae Park 	 * another cpu's list. schedule_work() should be fine with this too.
2303bf22e37aSAndrey Ryabinin 	 */
2304bf22e37aSAndrey Ryabinin 	struct vfree_deferred *p = raw_cpu_ptr(&vfree_deferred);
2305bf22e37aSAndrey Ryabinin 
2306bf22e37aSAndrey Ryabinin 	if (llist_add((struct llist_node *)addr, &p->list))
2307bf22e37aSAndrey Ryabinin 		schedule_work(&p->wq);
2308bf22e37aSAndrey Ryabinin }
2309bf22e37aSAndrey Ryabinin 
2310bf22e37aSAndrey Ryabinin /**
2311bf22e37aSAndrey Ryabinin  * vfree_atomic - release memory allocated by vmalloc()
2312bf22e37aSAndrey Ryabinin  * @addr:	  memory base address
2313bf22e37aSAndrey Ryabinin  *
2314bf22e37aSAndrey Ryabinin  * This one is just like vfree() but can be called in any atomic context
2315bf22e37aSAndrey Ryabinin  * except NMIs.
2316bf22e37aSAndrey Ryabinin  */
2317bf22e37aSAndrey Ryabinin void vfree_atomic(const void *addr)
2318bf22e37aSAndrey Ryabinin {
2319bf22e37aSAndrey Ryabinin 	BUG_ON(in_nmi());
2320bf22e37aSAndrey Ryabinin 
2321bf22e37aSAndrey Ryabinin 	kmemleak_free(addr);
2322bf22e37aSAndrey Ryabinin 
2323bf22e37aSAndrey Ryabinin 	if (!addr)
2324bf22e37aSAndrey Ryabinin 		return;
2325bf22e37aSAndrey Ryabinin 	__vfree_deferred(addr);
2326bf22e37aSAndrey Ryabinin }
2327bf22e37aSAndrey Ryabinin 
2328c67dc624SRoman Penyaev static void __vfree(const void *addr)
2329c67dc624SRoman Penyaev {
2330c67dc624SRoman Penyaev 	if (unlikely(in_interrupt()))
2331c67dc624SRoman Penyaev 		__vfree_deferred(addr);
2332c67dc624SRoman Penyaev 	else
2333c67dc624SRoman Penyaev 		__vunmap(addr, 1);
2334c67dc624SRoman Penyaev }
2335c67dc624SRoman Penyaev 
23361da177e4SLinus Torvalds /**
2337fa307474SMatthew Wilcox (Oracle)  * vfree - Release memory allocated by vmalloc()
2338fa307474SMatthew Wilcox (Oracle)  * @addr:  Memory base address
23391da177e4SLinus Torvalds  *
2340fa307474SMatthew Wilcox (Oracle)  * Free the virtually continuous memory area starting at @addr, as obtained
2341fa307474SMatthew Wilcox (Oracle)  * from one of the vmalloc() family of APIs.  This will usually also free the
2342fa307474SMatthew Wilcox (Oracle)  * physical memory underlying the virtual allocation, but that memory is
2343fa307474SMatthew Wilcox (Oracle)  * reference counted, so it will not be freed until the last user goes away.
23441da177e4SLinus Torvalds  *
2345fa307474SMatthew Wilcox (Oracle)  * If @addr is NULL, no operation is performed.
234632fcfd40SAl Viro  *
2347fa307474SMatthew Wilcox (Oracle)  * Context:
23483ca4ea3aSAndrey Ryabinin  * May sleep if called *not* from interrupt context.
2349fa307474SMatthew Wilcox (Oracle)  * Must not be called in NMI context (strictly speaking, it could be
2350fa307474SMatthew Wilcox (Oracle)  * if we have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling
2351fa307474SMatthew Wilcox (Oracle)  * conventions for vfree() arch-depenedent would be a really bad idea).
23521da177e4SLinus Torvalds  */
2353b3bdda02SChristoph Lameter void vfree(const void *addr)
23541da177e4SLinus Torvalds {
235532fcfd40SAl Viro 	BUG_ON(in_nmi());
235689219d37SCatalin Marinas 
235789219d37SCatalin Marinas 	kmemleak_free(addr);
235889219d37SCatalin Marinas 
2359a8dda165SAndrey Ryabinin 	might_sleep_if(!in_interrupt());
2360a8dda165SAndrey Ryabinin 
236132fcfd40SAl Viro 	if (!addr)
236232fcfd40SAl Viro 		return;
2363c67dc624SRoman Penyaev 
2364c67dc624SRoman Penyaev 	__vfree(addr);
23651da177e4SLinus Torvalds }
23661da177e4SLinus Torvalds EXPORT_SYMBOL(vfree);
23671da177e4SLinus Torvalds 
23681da177e4SLinus Torvalds /**
23691da177e4SLinus Torvalds  * vunmap - release virtual mapping obtained by vmap()
23701da177e4SLinus Torvalds  * @addr:   memory base address
23711da177e4SLinus Torvalds  *
23721da177e4SLinus Torvalds  * Free the virtually contiguous memory area starting at @addr,
23731da177e4SLinus Torvalds  * which was created from the page array passed to vmap().
23741da177e4SLinus Torvalds  *
237580e93effSPekka Enberg  * Must not be called in interrupt context.
23761da177e4SLinus Torvalds  */
2377b3bdda02SChristoph Lameter void vunmap(const void *addr)
23781da177e4SLinus Torvalds {
23791da177e4SLinus Torvalds 	BUG_ON(in_interrupt());
238034754b69SPeter Zijlstra 	might_sleep();
238132fcfd40SAl Viro 	if (addr)
23821da177e4SLinus Torvalds 		__vunmap(addr, 0);
23831da177e4SLinus Torvalds }
23841da177e4SLinus Torvalds EXPORT_SYMBOL(vunmap);
23851da177e4SLinus Torvalds 
23861da177e4SLinus Torvalds /**
23871da177e4SLinus Torvalds  * vmap - map an array of pages into virtually contiguous space
23881da177e4SLinus Torvalds  * @pages: array of page pointers
23891da177e4SLinus Torvalds  * @count: number of pages to map
23901da177e4SLinus Torvalds  * @flags: vm_area->flags
23911da177e4SLinus Torvalds  * @prot: page protection for the mapping
23921da177e4SLinus Torvalds  *
2393b944afc9SChristoph Hellwig  * Maps @count pages from @pages into contiguous kernel virtual space.
2394b944afc9SChristoph Hellwig  * If @flags contains %VM_MAP_PUT_PAGES the ownership of the pages array itself
2395b944afc9SChristoph Hellwig  * (which must be kmalloc or vmalloc memory) and one reference per pages in it
2396b944afc9SChristoph Hellwig  * are transferred from the caller to vmap(), and will be freed / dropped when
2397b944afc9SChristoph Hellwig  * vfree() is called on the return value.
2398a862f68aSMike Rapoport  *
2399a862f68aSMike Rapoport  * Return: the address of the area or %NULL on failure
24001da177e4SLinus Torvalds  */
24011da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count,
24021da177e4SLinus Torvalds 	   unsigned long flags, pgprot_t prot)
24031da177e4SLinus Torvalds {
24041da177e4SLinus Torvalds 	struct vm_struct *area;
240565ee03c4SGuillermo Julián Moreno 	unsigned long size;		/* In bytes */
24061da177e4SLinus Torvalds 
240734754b69SPeter Zijlstra 	might_sleep();
240834754b69SPeter Zijlstra 
2409ca79b0c2SArun KS 	if (count > totalram_pages())
24101da177e4SLinus Torvalds 		return NULL;
24111da177e4SLinus Torvalds 
241265ee03c4SGuillermo Julián Moreno 	size = (unsigned long)count << PAGE_SHIFT;
241365ee03c4SGuillermo Julián Moreno 	area = get_vm_area_caller(size, flags, __builtin_return_address(0));
24141da177e4SLinus Torvalds 	if (!area)
24151da177e4SLinus Torvalds 		return NULL;
241623016969SChristoph Lameter 
2417cca98e9fSChristoph Hellwig 	if (map_kernel_range((unsigned long)area->addr, size, pgprot_nx(prot),
2418ed1f324cSChristoph Hellwig 			pages) < 0) {
24191da177e4SLinus Torvalds 		vunmap(area->addr);
24201da177e4SLinus Torvalds 		return NULL;
24211da177e4SLinus Torvalds 	}
24221da177e4SLinus Torvalds 
2423c22ee528SMiaohe Lin 	if (flags & VM_MAP_PUT_PAGES) {
2424b944afc9SChristoph Hellwig 		area->pages = pages;
2425c22ee528SMiaohe Lin 		area->nr_pages = count;
2426c22ee528SMiaohe Lin 	}
24271da177e4SLinus Torvalds 	return area->addr;
24281da177e4SLinus Torvalds }
24291da177e4SLinus Torvalds EXPORT_SYMBOL(vmap);
24301da177e4SLinus Torvalds 
24313e9a9e25SChristoph Hellwig #ifdef CONFIG_VMAP_PFN
24323e9a9e25SChristoph Hellwig struct vmap_pfn_data {
24333e9a9e25SChristoph Hellwig 	unsigned long	*pfns;
24343e9a9e25SChristoph Hellwig 	pgprot_t	prot;
24353e9a9e25SChristoph Hellwig 	unsigned int	idx;
24363e9a9e25SChristoph Hellwig };
24373e9a9e25SChristoph Hellwig 
24383e9a9e25SChristoph Hellwig static int vmap_pfn_apply(pte_t *pte, unsigned long addr, void *private)
24393e9a9e25SChristoph Hellwig {
24403e9a9e25SChristoph Hellwig 	struct vmap_pfn_data *data = private;
24413e9a9e25SChristoph Hellwig 
24423e9a9e25SChristoph Hellwig 	if (WARN_ON_ONCE(pfn_valid(data->pfns[data->idx])))
24433e9a9e25SChristoph Hellwig 		return -EINVAL;
24443e9a9e25SChristoph Hellwig 	*pte = pte_mkspecial(pfn_pte(data->pfns[data->idx++], data->prot));
24453e9a9e25SChristoph Hellwig 	return 0;
24463e9a9e25SChristoph Hellwig }
24473e9a9e25SChristoph Hellwig 
24483e9a9e25SChristoph Hellwig /**
24493e9a9e25SChristoph Hellwig  * vmap_pfn - map an array of PFNs into virtually contiguous space
24503e9a9e25SChristoph Hellwig  * @pfns: array of PFNs
24513e9a9e25SChristoph Hellwig  * @count: number of pages to map
24523e9a9e25SChristoph Hellwig  * @prot: page protection for the mapping
24533e9a9e25SChristoph Hellwig  *
24543e9a9e25SChristoph Hellwig  * Maps @count PFNs from @pfns into contiguous kernel virtual space and returns
24553e9a9e25SChristoph Hellwig  * the start address of the mapping.
24563e9a9e25SChristoph Hellwig  */
24573e9a9e25SChristoph Hellwig void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot)
24583e9a9e25SChristoph Hellwig {
24593e9a9e25SChristoph Hellwig 	struct vmap_pfn_data data = { .pfns = pfns, .prot = pgprot_nx(prot) };
24603e9a9e25SChristoph Hellwig 	struct vm_struct *area;
24613e9a9e25SChristoph Hellwig 
24623e9a9e25SChristoph Hellwig 	area = get_vm_area_caller(count * PAGE_SIZE, VM_IOREMAP,
24633e9a9e25SChristoph Hellwig 			__builtin_return_address(0));
24643e9a9e25SChristoph Hellwig 	if (!area)
24653e9a9e25SChristoph Hellwig 		return NULL;
24663e9a9e25SChristoph Hellwig 	if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
24673e9a9e25SChristoph Hellwig 			count * PAGE_SIZE, vmap_pfn_apply, &data)) {
24683e9a9e25SChristoph Hellwig 		free_vm_area(area);
24693e9a9e25SChristoph Hellwig 		return NULL;
24703e9a9e25SChristoph Hellwig 	}
24713e9a9e25SChristoph Hellwig 	return area->addr;
24723e9a9e25SChristoph Hellwig }
24733e9a9e25SChristoph Hellwig EXPORT_SYMBOL_GPL(vmap_pfn);
24743e9a9e25SChristoph Hellwig #endif /* CONFIG_VMAP_PFN */
24753e9a9e25SChristoph Hellwig 
2476e31d9eb5SAdrian Bunk static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
24773722e13cSWanpeng Li 				 pgprot_t prot, int node)
24781da177e4SLinus Torvalds {
2479930f036bSDavid Rientjes 	const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
2480f255935bSChristoph Hellwig 	unsigned int nr_pages = get_vm_area_size(area) >> PAGE_SHIFT;
248134fe6537SAndrew Morton 	unsigned long array_size;
248234fe6537SAndrew Morton 	unsigned int i;
2483f255935bSChristoph Hellwig 	struct page **pages;
24841da177e4SLinus Torvalds 
248534fe6537SAndrew Morton 	array_size = (unsigned long)nr_pages * sizeof(struct page *);
2486f255935bSChristoph Hellwig 	gfp_mask |= __GFP_NOWARN;
2487f255935bSChristoph Hellwig 	if (!(gfp_mask & (GFP_DMA | GFP_DMA32)))
2488f255935bSChristoph Hellwig 		gfp_mask |= __GFP_HIGHMEM;
24891da177e4SLinus Torvalds 
24901da177e4SLinus Torvalds 	/* Please note that the recursion is strictly bounded. */
24918757d5faSJan Kiszka 	if (array_size > PAGE_SIZE) {
2492f255935bSChristoph Hellwig 		pages = __vmalloc_node(array_size, 1, nested_gfp, node,
2493f255935bSChristoph Hellwig 					area->caller);
2494286e1ea3SAndrew Morton 	} else {
2495976d6dfbSJan Beulich 		pages = kmalloc_node(array_size, nested_gfp, node);
2496286e1ea3SAndrew Morton 	}
24977ea36242SAustin Kim 
24987ea36242SAustin Kim 	if (!pages) {
24998945a723SUladzislau Rezki (Sony) 		free_vm_area(area);
25001da177e4SLinus Torvalds 		return NULL;
25011da177e4SLinus Torvalds 	}
25021da177e4SLinus Torvalds 
25037ea36242SAustin Kim 	area->pages = pages;
25047ea36242SAustin Kim 	area->nr_pages = nr_pages;
25057ea36242SAustin Kim 
25061da177e4SLinus Torvalds 	for (i = 0; i < area->nr_pages; i++) {
2507bf53d6f8SChristoph Lameter 		struct page *page;
2508bf53d6f8SChristoph Lameter 
25094b90951cSJianguo Wu 		if (node == NUMA_NO_NODE)
2510f255935bSChristoph Hellwig 			page = alloc_page(gfp_mask);
2511930fc45aSChristoph Lameter 		else
2512f255935bSChristoph Hellwig 			page = alloc_pages_node(node, gfp_mask, 0);
2513bf53d6f8SChristoph Lameter 
2514bf53d6f8SChristoph Lameter 		if (unlikely(!page)) {
251582afbc32SHui Su 			/* Successfully allocated i pages, free them in __vfree() */
25161da177e4SLinus Torvalds 			area->nr_pages = i;
251797105f0aSRoman Gushchin 			atomic_long_add(area->nr_pages, &nr_vmalloc_pages);
25181da177e4SLinus Torvalds 			goto fail;
25191da177e4SLinus Torvalds 		}
2520bf53d6f8SChristoph Lameter 		area->pages[i] = page;
2521dcf61ff0SLiu Xiang 		if (gfpflags_allow_blocking(gfp_mask))
2522660654f9SEric Dumazet 			cond_resched();
25231da177e4SLinus Torvalds 	}
252497105f0aSRoman Gushchin 	atomic_long_add(area->nr_pages, &nr_vmalloc_pages);
25251da177e4SLinus Torvalds 
2526ed1f324cSChristoph Hellwig 	if (map_kernel_range((unsigned long)area->addr, get_vm_area_size(area),
2527ed1f324cSChristoph Hellwig 			prot, pages) < 0)
25281da177e4SLinus Torvalds 		goto fail;
2529ed1f324cSChristoph Hellwig 
25301da177e4SLinus Torvalds 	return area->addr;
25311da177e4SLinus Torvalds 
25321da177e4SLinus Torvalds fail:
2533a8e99259SMichal Hocko 	warn_alloc(gfp_mask, NULL,
25347877cdccSMichal Hocko 			  "vmalloc: allocation failure, allocated %ld of %ld bytes",
253522943ab1SDave Hansen 			  (area->nr_pages*PAGE_SIZE), area->size);
2536c67dc624SRoman Penyaev 	__vfree(area->addr);
25371da177e4SLinus Torvalds 	return NULL;
25381da177e4SLinus Torvalds }
25391da177e4SLinus Torvalds 
2540d0a21265SDavid Rientjes /**
2541d0a21265SDavid Rientjes  * __vmalloc_node_range - allocate virtually contiguous memory
2542d0a21265SDavid Rientjes  * @size:		  allocation size
2543d0a21265SDavid Rientjes  * @align:		  desired alignment
2544d0a21265SDavid Rientjes  * @start:		  vm area range start
2545d0a21265SDavid Rientjes  * @end:		  vm area range end
2546d0a21265SDavid Rientjes  * @gfp_mask:		  flags for the page level allocator
2547d0a21265SDavid Rientjes  * @prot:		  protection mask for the allocated pages
2548cb9e3c29SAndrey Ryabinin  * @vm_flags:		  additional vm area flags (e.g. %VM_NO_GUARD)
254900ef2d2fSDavid Rientjes  * @node:		  node to use for allocation or NUMA_NO_NODE
2550d0a21265SDavid Rientjes  * @caller:		  caller's return address
2551d0a21265SDavid Rientjes  *
2552d0a21265SDavid Rientjes  * Allocate enough pages to cover @size from the page level
2553d0a21265SDavid Rientjes  * allocator with @gfp_mask flags.  Map them into contiguous
2554d0a21265SDavid Rientjes  * kernel virtual space, using a pagetable protection of @prot.
2555a862f68aSMike Rapoport  *
2556a862f68aSMike Rapoport  * Return: the address of the area or %NULL on failure
2557d0a21265SDavid Rientjes  */
2558d0a21265SDavid Rientjes void *__vmalloc_node_range(unsigned long size, unsigned long align,
2559d0a21265SDavid Rientjes 			unsigned long start, unsigned long end, gfp_t gfp_mask,
2560cb9e3c29SAndrey Ryabinin 			pgprot_t prot, unsigned long vm_flags, int node,
2561cb9e3c29SAndrey Ryabinin 			const void *caller)
2562930fc45aSChristoph Lameter {
2563d0a21265SDavid Rientjes 	struct vm_struct *area;
2564d0a21265SDavid Rientjes 	void *addr;
2565d0a21265SDavid Rientjes 	unsigned long real_size = size;
2566d0a21265SDavid Rientjes 
2567d0a21265SDavid Rientjes 	size = PAGE_ALIGN(size);
2568ca79b0c2SArun KS 	if (!size || (size >> PAGE_SHIFT) > totalram_pages())
2569de7d2b56SJoe Perches 		goto fail;
2570d0a21265SDavid Rientjes 
2571d98c9e83SAndrey Ryabinin 	area = __get_vm_area_node(real_size, align, VM_ALLOC | VM_UNINITIALIZED |
2572cb9e3c29SAndrey Ryabinin 				vm_flags, start, end, node, gfp_mask, caller);
2573d0a21265SDavid Rientjes 	if (!area)
2574de7d2b56SJoe Perches 		goto fail;
2575d0a21265SDavid Rientjes 
25763722e13cSWanpeng Li 	addr = __vmalloc_area_node(area, gfp_mask, prot, node);
25771368edf0SMel Gorman 	if (!addr)
2578b82225f3SWanpeng Li 		return NULL;
257989219d37SCatalin Marinas 
258089219d37SCatalin Marinas 	/*
258120fc02b4SZhang Yanfei 	 * In this function, newly allocated vm_struct has VM_UNINITIALIZED
258220fc02b4SZhang Yanfei 	 * flag. It means that vm_struct is not fully initialized.
25834341fa45SJoonsoo Kim 	 * Now, it is fully initialized, so remove this flag here.
2584f5252e00SMitsuo Hayasaka 	 */
258520fc02b4SZhang Yanfei 	clear_vm_uninitialized_flag(area);
2586f5252e00SMitsuo Hayasaka 
258794f4a161SCatalin Marinas 	kmemleak_vmalloc(area, size, gfp_mask);
258889219d37SCatalin Marinas 
258989219d37SCatalin Marinas 	return addr;
2590de7d2b56SJoe Perches 
2591de7d2b56SJoe Perches fail:
2592a8e99259SMichal Hocko 	warn_alloc(gfp_mask, NULL,
25937877cdccSMichal Hocko 			  "vmalloc: allocation failure: %lu bytes", real_size);
2594de7d2b56SJoe Perches 	return NULL;
2595930fc45aSChristoph Lameter }
2596930fc45aSChristoph Lameter 
25971da177e4SLinus Torvalds /**
2598930fc45aSChristoph Lameter  * __vmalloc_node - allocate virtually contiguous memory
25991da177e4SLinus Torvalds  * @size:	    allocation size
26002dca6999SDavid Miller  * @align:	    desired alignment
26011da177e4SLinus Torvalds  * @gfp_mask:	    flags for the page level allocator
260200ef2d2fSDavid Rientjes  * @node:	    node to use for allocation or NUMA_NO_NODE
2603c85d194bSRandy Dunlap  * @caller:	    caller's return address
26041da177e4SLinus Torvalds  *
2605f38fcb9cSChristoph Hellwig  * Allocate enough pages to cover @size from the page level allocator with
2606f38fcb9cSChristoph Hellwig  * @gfp_mask flags.  Map them into contiguous kernel virtual space.
2607a7c3e901SMichal Hocko  *
2608dcda9b04SMichal Hocko  * Reclaim modifiers in @gfp_mask - __GFP_NORETRY, __GFP_RETRY_MAYFAIL
2609a7c3e901SMichal Hocko  * and __GFP_NOFAIL are not supported
2610a7c3e901SMichal Hocko  *
2611a7c3e901SMichal Hocko  * Any use of gfp flags outside of GFP_KERNEL should be consulted
2612a7c3e901SMichal Hocko  * with mm people.
2613a862f68aSMike Rapoport  *
2614a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
26151da177e4SLinus Torvalds  */
26162b905948SChristoph Hellwig void *__vmalloc_node(unsigned long size, unsigned long align,
2617f38fcb9cSChristoph Hellwig 			    gfp_t gfp_mask, int node, const void *caller)
26181da177e4SLinus Torvalds {
2619d0a21265SDavid Rientjes 	return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
2620f38fcb9cSChristoph Hellwig 				gfp_mask, PAGE_KERNEL, 0, node, caller);
26211da177e4SLinus Torvalds }
2622c3f896dcSChristoph Hellwig /*
2623c3f896dcSChristoph Hellwig  * This is only for performance analysis of vmalloc and stress purpose.
2624c3f896dcSChristoph Hellwig  * It is required by vmalloc test module, therefore do not use it other
2625c3f896dcSChristoph Hellwig  * than that.
2626c3f896dcSChristoph Hellwig  */
2627c3f896dcSChristoph Hellwig #ifdef CONFIG_TEST_VMALLOC_MODULE
2628c3f896dcSChristoph Hellwig EXPORT_SYMBOL_GPL(__vmalloc_node);
2629c3f896dcSChristoph Hellwig #endif
26301da177e4SLinus Torvalds 
263188dca4caSChristoph Hellwig void *__vmalloc(unsigned long size, gfp_t gfp_mask)
2632930fc45aSChristoph Lameter {
2633f38fcb9cSChristoph Hellwig 	return __vmalloc_node(size, 1, gfp_mask, NUMA_NO_NODE,
263423016969SChristoph Lameter 				__builtin_return_address(0));
2635930fc45aSChristoph Lameter }
26361da177e4SLinus Torvalds EXPORT_SYMBOL(__vmalloc);
26371da177e4SLinus Torvalds 
26381da177e4SLinus Torvalds /**
26391da177e4SLinus Torvalds  * vmalloc - allocate virtually contiguous memory
26401da177e4SLinus Torvalds  * @size:    allocation size
264192eac168SMike Rapoport  *
26421da177e4SLinus Torvalds  * Allocate enough pages to cover @size from the page level
26431da177e4SLinus Torvalds  * allocator and map them into contiguous kernel virtual space.
26441da177e4SLinus Torvalds  *
2645c1c8897fSMichael Opdenacker  * For tight control over page level allocator and protection flags
26461da177e4SLinus Torvalds  * use __vmalloc() instead.
2647a862f68aSMike Rapoport  *
2648a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
26491da177e4SLinus Torvalds  */
26501da177e4SLinus Torvalds void *vmalloc(unsigned long size)
26511da177e4SLinus Torvalds {
26524d39d728SChristoph Hellwig 	return __vmalloc_node(size, 1, GFP_KERNEL, NUMA_NO_NODE,
26534d39d728SChristoph Hellwig 				__builtin_return_address(0));
26541da177e4SLinus Torvalds }
26551da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc);
26561da177e4SLinus Torvalds 
2657930fc45aSChristoph Lameter /**
2658e1ca7788SDave Young  * vzalloc - allocate virtually contiguous memory with zero fill
2659e1ca7788SDave Young  * @size:    allocation size
266092eac168SMike Rapoport  *
2661e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
2662e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
2663e1ca7788SDave Young  * The memory allocated is set to zero.
2664e1ca7788SDave Young  *
2665e1ca7788SDave Young  * For tight control over page level allocator and protection flags
2666e1ca7788SDave Young  * use __vmalloc() instead.
2667a862f68aSMike Rapoport  *
2668a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
2669e1ca7788SDave Young  */
2670e1ca7788SDave Young void *vzalloc(unsigned long size)
2671e1ca7788SDave Young {
26724d39d728SChristoph Hellwig 	return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_ZERO, NUMA_NO_NODE,
26734d39d728SChristoph Hellwig 				__builtin_return_address(0));
2674e1ca7788SDave Young }
2675e1ca7788SDave Young EXPORT_SYMBOL(vzalloc);
2676e1ca7788SDave Young 
2677e1ca7788SDave Young /**
2678ead04089SRolf Eike Beer  * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
267983342314SNick Piggin  * @size: allocation size
2680ead04089SRolf Eike Beer  *
2681ead04089SRolf Eike Beer  * The resulting memory area is zeroed so it can be mapped to userspace
2682ead04089SRolf Eike Beer  * without leaking data.
2683a862f68aSMike Rapoport  *
2684a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
268583342314SNick Piggin  */
268683342314SNick Piggin void *vmalloc_user(unsigned long size)
268783342314SNick Piggin {
2688bc84c535SRoman Penyaev 	return __vmalloc_node_range(size, SHMLBA,  VMALLOC_START, VMALLOC_END,
2689bc84c535SRoman Penyaev 				    GFP_KERNEL | __GFP_ZERO, PAGE_KERNEL,
2690bc84c535SRoman Penyaev 				    VM_USERMAP, NUMA_NO_NODE,
269100ef2d2fSDavid Rientjes 				    __builtin_return_address(0));
269283342314SNick Piggin }
269383342314SNick Piggin EXPORT_SYMBOL(vmalloc_user);
269483342314SNick Piggin 
269583342314SNick Piggin /**
2696930fc45aSChristoph Lameter  * vmalloc_node - allocate memory on a specific node
2697930fc45aSChristoph Lameter  * @size:	  allocation size
2698d44e0780SRandy Dunlap  * @node:	  numa node
2699930fc45aSChristoph Lameter  *
2700930fc45aSChristoph Lameter  * Allocate enough pages to cover @size from the page level
2701930fc45aSChristoph Lameter  * allocator and map them into contiguous kernel virtual space.
2702930fc45aSChristoph Lameter  *
2703c1c8897fSMichael Opdenacker  * For tight control over page level allocator and protection flags
2704930fc45aSChristoph Lameter  * use __vmalloc() instead.
2705a862f68aSMike Rapoport  *
2706a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
2707930fc45aSChristoph Lameter  */
2708930fc45aSChristoph Lameter void *vmalloc_node(unsigned long size, int node)
2709930fc45aSChristoph Lameter {
2710f38fcb9cSChristoph Hellwig 	return __vmalloc_node(size, 1, GFP_KERNEL, node,
2711f38fcb9cSChristoph Hellwig 			__builtin_return_address(0));
2712930fc45aSChristoph Lameter }
2713930fc45aSChristoph Lameter EXPORT_SYMBOL(vmalloc_node);
2714930fc45aSChristoph Lameter 
2715e1ca7788SDave Young /**
2716e1ca7788SDave Young  * vzalloc_node - allocate memory on a specific node with zero fill
2717e1ca7788SDave Young  * @size:	allocation size
2718e1ca7788SDave Young  * @node:	numa node
2719e1ca7788SDave Young  *
2720e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
2721e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
2722e1ca7788SDave Young  * The memory allocated is set to zero.
2723e1ca7788SDave Young  *
2724a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
2725e1ca7788SDave Young  */
2726e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node)
2727e1ca7788SDave Young {
27284d39d728SChristoph Hellwig 	return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_ZERO, node,
27294d39d728SChristoph Hellwig 				__builtin_return_address(0));
2730e1ca7788SDave Young }
2731e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node);
2732e1ca7788SDave Young 
27330d08e0d3SAndi Kleen #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
2734698d0831SMichal Hocko #define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL)
27350d08e0d3SAndi Kleen #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
2736698d0831SMichal Hocko #define GFP_VMALLOC32 (GFP_DMA | GFP_KERNEL)
27370d08e0d3SAndi Kleen #else
2738698d0831SMichal Hocko /*
2739698d0831SMichal Hocko  * 64b systems should always have either DMA or DMA32 zones. For others
2740698d0831SMichal Hocko  * GFP_DMA32 should do the right thing and use the normal zone.
2741698d0831SMichal Hocko  */
2742698d0831SMichal Hocko #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
27430d08e0d3SAndi Kleen #endif
27440d08e0d3SAndi Kleen 
27451da177e4SLinus Torvalds /**
27461da177e4SLinus Torvalds  * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
27471da177e4SLinus Torvalds  * @size:	allocation size
27481da177e4SLinus Torvalds  *
27491da177e4SLinus Torvalds  * Allocate enough 32bit PA addressable pages to cover @size from the
27501da177e4SLinus Torvalds  * page level allocator and map them into contiguous kernel virtual space.
2751a862f68aSMike Rapoport  *
2752a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
27531da177e4SLinus Torvalds  */
27541da177e4SLinus Torvalds void *vmalloc_32(unsigned long size)
27551da177e4SLinus Torvalds {
2756f38fcb9cSChristoph Hellwig 	return __vmalloc_node(size, 1, GFP_VMALLOC32, NUMA_NO_NODE,
2757f38fcb9cSChristoph Hellwig 			__builtin_return_address(0));
27581da177e4SLinus Torvalds }
27591da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc_32);
27601da177e4SLinus Torvalds 
276183342314SNick Piggin /**
2762ead04089SRolf Eike Beer  * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
276383342314SNick Piggin  * @size:	     allocation size
2764ead04089SRolf Eike Beer  *
2765ead04089SRolf Eike Beer  * The resulting memory area is 32bit addressable and zeroed so it can be
2766ead04089SRolf Eike Beer  * mapped to userspace without leaking data.
2767a862f68aSMike Rapoport  *
2768a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
276983342314SNick Piggin  */
277083342314SNick Piggin void *vmalloc_32_user(unsigned long size)
277183342314SNick Piggin {
2772bc84c535SRoman Penyaev 	return __vmalloc_node_range(size, SHMLBA,  VMALLOC_START, VMALLOC_END,
2773bc84c535SRoman Penyaev 				    GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
2774bc84c535SRoman Penyaev 				    VM_USERMAP, NUMA_NO_NODE,
27755a82ac71SRoman Penyaev 				    __builtin_return_address(0));
277683342314SNick Piggin }
277783342314SNick Piggin EXPORT_SYMBOL(vmalloc_32_user);
277883342314SNick Piggin 
2779d0107eb0SKAMEZAWA Hiroyuki /*
2780d0107eb0SKAMEZAWA Hiroyuki  * small helper routine , copy contents to buf from addr.
2781d0107eb0SKAMEZAWA Hiroyuki  * If the page is not present, fill zero.
2782d0107eb0SKAMEZAWA Hiroyuki  */
2783d0107eb0SKAMEZAWA Hiroyuki 
2784d0107eb0SKAMEZAWA Hiroyuki static int aligned_vread(char *buf, char *addr, unsigned long count)
2785d0107eb0SKAMEZAWA Hiroyuki {
2786d0107eb0SKAMEZAWA Hiroyuki 	struct page *p;
2787d0107eb0SKAMEZAWA Hiroyuki 	int copied = 0;
2788d0107eb0SKAMEZAWA Hiroyuki 
2789d0107eb0SKAMEZAWA Hiroyuki 	while (count) {
2790d0107eb0SKAMEZAWA Hiroyuki 		unsigned long offset, length;
2791d0107eb0SKAMEZAWA Hiroyuki 
2792891c49abSAlexander Kuleshov 		offset = offset_in_page(addr);
2793d0107eb0SKAMEZAWA Hiroyuki 		length = PAGE_SIZE - offset;
2794d0107eb0SKAMEZAWA Hiroyuki 		if (length > count)
2795d0107eb0SKAMEZAWA Hiroyuki 			length = count;
2796d0107eb0SKAMEZAWA Hiroyuki 		p = vmalloc_to_page(addr);
2797d0107eb0SKAMEZAWA Hiroyuki 		/*
2798d0107eb0SKAMEZAWA Hiroyuki 		 * To do safe access to this _mapped_ area, we need
2799d0107eb0SKAMEZAWA Hiroyuki 		 * lock. But adding lock here means that we need to add
2800d0107eb0SKAMEZAWA Hiroyuki 		 * overhead of vmalloc()/vfree() calles for this _debug_
2801d0107eb0SKAMEZAWA Hiroyuki 		 * interface, rarely used. Instead of that, we'll use
2802d0107eb0SKAMEZAWA Hiroyuki 		 * kmap() and get small overhead in this access function.
2803d0107eb0SKAMEZAWA Hiroyuki 		 */
2804d0107eb0SKAMEZAWA Hiroyuki 		if (p) {
2805d0107eb0SKAMEZAWA Hiroyuki 			/*
2806d0107eb0SKAMEZAWA Hiroyuki 			 * we can expect USER0 is not used (see vread/vwrite's
2807d0107eb0SKAMEZAWA Hiroyuki 			 * function description)
2808d0107eb0SKAMEZAWA Hiroyuki 			 */
28099b04c5feSCong Wang 			void *map = kmap_atomic(p);
2810d0107eb0SKAMEZAWA Hiroyuki 			memcpy(buf, map + offset, length);
28119b04c5feSCong Wang 			kunmap_atomic(map);
2812d0107eb0SKAMEZAWA Hiroyuki 		} else
2813d0107eb0SKAMEZAWA Hiroyuki 			memset(buf, 0, length);
2814d0107eb0SKAMEZAWA Hiroyuki 
2815d0107eb0SKAMEZAWA Hiroyuki 		addr += length;
2816d0107eb0SKAMEZAWA Hiroyuki 		buf += length;
2817d0107eb0SKAMEZAWA Hiroyuki 		copied += length;
2818d0107eb0SKAMEZAWA Hiroyuki 		count -= length;
2819d0107eb0SKAMEZAWA Hiroyuki 	}
2820d0107eb0SKAMEZAWA Hiroyuki 	return copied;
2821d0107eb0SKAMEZAWA Hiroyuki }
2822d0107eb0SKAMEZAWA Hiroyuki 
2823d0107eb0SKAMEZAWA Hiroyuki static int aligned_vwrite(char *buf, char *addr, unsigned long count)
2824d0107eb0SKAMEZAWA Hiroyuki {
2825d0107eb0SKAMEZAWA Hiroyuki 	struct page *p;
2826d0107eb0SKAMEZAWA Hiroyuki 	int copied = 0;
2827d0107eb0SKAMEZAWA Hiroyuki 
2828d0107eb0SKAMEZAWA Hiroyuki 	while (count) {
2829d0107eb0SKAMEZAWA Hiroyuki 		unsigned long offset, length;
2830d0107eb0SKAMEZAWA Hiroyuki 
2831891c49abSAlexander Kuleshov 		offset = offset_in_page(addr);
2832d0107eb0SKAMEZAWA Hiroyuki 		length = PAGE_SIZE - offset;
2833d0107eb0SKAMEZAWA Hiroyuki 		if (length > count)
2834d0107eb0SKAMEZAWA Hiroyuki 			length = count;
2835d0107eb0SKAMEZAWA Hiroyuki 		p = vmalloc_to_page(addr);
2836d0107eb0SKAMEZAWA Hiroyuki 		/*
2837d0107eb0SKAMEZAWA Hiroyuki 		 * To do safe access to this _mapped_ area, we need
2838d0107eb0SKAMEZAWA Hiroyuki 		 * lock. But adding lock here means that we need to add
2839d0107eb0SKAMEZAWA Hiroyuki 		 * overhead of vmalloc()/vfree() calles for this _debug_
2840d0107eb0SKAMEZAWA Hiroyuki 		 * interface, rarely used. Instead of that, we'll use
2841d0107eb0SKAMEZAWA Hiroyuki 		 * kmap() and get small overhead in this access function.
2842d0107eb0SKAMEZAWA Hiroyuki 		 */
2843d0107eb0SKAMEZAWA Hiroyuki 		if (p) {
2844d0107eb0SKAMEZAWA Hiroyuki 			/*
2845d0107eb0SKAMEZAWA Hiroyuki 			 * we can expect USER0 is not used (see vread/vwrite's
2846d0107eb0SKAMEZAWA Hiroyuki 			 * function description)
2847d0107eb0SKAMEZAWA Hiroyuki 			 */
28489b04c5feSCong Wang 			void *map = kmap_atomic(p);
2849d0107eb0SKAMEZAWA Hiroyuki 			memcpy(map + offset, buf, length);
28509b04c5feSCong Wang 			kunmap_atomic(map);
2851d0107eb0SKAMEZAWA Hiroyuki 		}
2852d0107eb0SKAMEZAWA Hiroyuki 		addr += length;
2853d0107eb0SKAMEZAWA Hiroyuki 		buf += length;
2854d0107eb0SKAMEZAWA Hiroyuki 		copied += length;
2855d0107eb0SKAMEZAWA Hiroyuki 		count -= length;
2856d0107eb0SKAMEZAWA Hiroyuki 	}
2857d0107eb0SKAMEZAWA Hiroyuki 	return copied;
2858d0107eb0SKAMEZAWA Hiroyuki }
2859d0107eb0SKAMEZAWA Hiroyuki 
2860d0107eb0SKAMEZAWA Hiroyuki /**
2861d0107eb0SKAMEZAWA Hiroyuki  * vread() - read vmalloc area in a safe way.
2862d0107eb0SKAMEZAWA Hiroyuki  * @buf:     buffer for reading data
2863d0107eb0SKAMEZAWA Hiroyuki  * @addr:    vm address.
2864d0107eb0SKAMEZAWA Hiroyuki  * @count:   number of bytes to be read.
2865d0107eb0SKAMEZAWA Hiroyuki  *
2866d0107eb0SKAMEZAWA Hiroyuki  * This function checks that addr is a valid vmalloc'ed area, and
2867d0107eb0SKAMEZAWA Hiroyuki  * copy data from that area to a given buffer. If the given memory range
2868d0107eb0SKAMEZAWA Hiroyuki  * of [addr...addr+count) includes some valid address, data is copied to
2869d0107eb0SKAMEZAWA Hiroyuki  * proper area of @buf. If there are memory holes, they'll be zero-filled.
2870d0107eb0SKAMEZAWA Hiroyuki  * IOREMAP area is treated as memory hole and no copy is done.
2871d0107eb0SKAMEZAWA Hiroyuki  *
2872d0107eb0SKAMEZAWA Hiroyuki  * If [addr...addr+count) doesn't includes any intersects with alive
2873a8e5202dSCong Wang  * vm_struct area, returns 0. @buf should be kernel's buffer.
2874d0107eb0SKAMEZAWA Hiroyuki  *
2875d0107eb0SKAMEZAWA Hiroyuki  * Note: In usual ops, vread() is never necessary because the caller
2876d0107eb0SKAMEZAWA Hiroyuki  * should know vmalloc() area is valid and can use memcpy().
2877d0107eb0SKAMEZAWA Hiroyuki  * This is for routines which have to access vmalloc area without
2878d9009d67SGeert Uytterhoeven  * any information, as /dev/kmem.
2879a862f68aSMike Rapoport  *
2880a862f68aSMike Rapoport  * Return: number of bytes for which addr and buf should be increased
2881a862f68aSMike Rapoport  * (same number as @count) or %0 if [addr...addr+count) doesn't
2882a862f68aSMike Rapoport  * include any intersection with valid vmalloc area
2883d0107eb0SKAMEZAWA Hiroyuki  */
28841da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count)
28851da177e4SLinus Torvalds {
2886e81ce85fSJoonsoo Kim 	struct vmap_area *va;
2887e81ce85fSJoonsoo Kim 	struct vm_struct *vm;
28881da177e4SLinus Torvalds 	char *vaddr, *buf_start = buf;
2889d0107eb0SKAMEZAWA Hiroyuki 	unsigned long buflen = count;
28901da177e4SLinus Torvalds 	unsigned long n;
28911da177e4SLinus Torvalds 
28921da177e4SLinus Torvalds 	/* Don't allow overflow */
28931da177e4SLinus Torvalds 	if ((unsigned long) addr + count < count)
28941da177e4SLinus Torvalds 		count = -(unsigned long) addr;
28951da177e4SLinus Torvalds 
2896e81ce85fSJoonsoo Kim 	spin_lock(&vmap_area_lock);
2897*f608788cSSerapheim Dimitropoulos 	va = __find_vmap_area((unsigned long)addr);
2898*f608788cSSerapheim Dimitropoulos 	if (!va)
2899*f608788cSSerapheim Dimitropoulos 		goto finished;
2900*f608788cSSerapheim Dimitropoulos 	list_for_each_entry_from(va, &vmap_area_list, list) {
2901e81ce85fSJoonsoo Kim 		if (!count)
2902e81ce85fSJoonsoo Kim 			break;
2903e81ce85fSJoonsoo Kim 
2904688fcbfcSPengfei Li 		if (!va->vm)
2905e81ce85fSJoonsoo Kim 			continue;
2906e81ce85fSJoonsoo Kim 
2907e81ce85fSJoonsoo Kim 		vm = va->vm;
2908e81ce85fSJoonsoo Kim 		vaddr = (char *) vm->addr;
2909762216abSWanpeng Li 		if (addr >= vaddr + get_vm_area_size(vm))
29101da177e4SLinus Torvalds 			continue;
29111da177e4SLinus Torvalds 		while (addr < vaddr) {
29121da177e4SLinus Torvalds 			if (count == 0)
29131da177e4SLinus Torvalds 				goto finished;
29141da177e4SLinus Torvalds 			*buf = '\0';
29151da177e4SLinus Torvalds 			buf++;
29161da177e4SLinus Torvalds 			addr++;
29171da177e4SLinus Torvalds 			count--;
29181da177e4SLinus Torvalds 		}
2919762216abSWanpeng Li 		n = vaddr + get_vm_area_size(vm) - addr;
2920d0107eb0SKAMEZAWA Hiroyuki 		if (n > count)
2921d0107eb0SKAMEZAWA Hiroyuki 			n = count;
2922e81ce85fSJoonsoo Kim 		if (!(vm->flags & VM_IOREMAP))
2923d0107eb0SKAMEZAWA Hiroyuki 			aligned_vread(buf, addr, n);
2924d0107eb0SKAMEZAWA Hiroyuki 		else /* IOREMAP area is treated as memory hole */
2925d0107eb0SKAMEZAWA Hiroyuki 			memset(buf, 0, n);
2926d0107eb0SKAMEZAWA Hiroyuki 		buf += n;
2927d0107eb0SKAMEZAWA Hiroyuki 		addr += n;
2928d0107eb0SKAMEZAWA Hiroyuki 		count -= n;
29291da177e4SLinus Torvalds 	}
29301da177e4SLinus Torvalds finished:
2931e81ce85fSJoonsoo Kim 	spin_unlock(&vmap_area_lock);
2932d0107eb0SKAMEZAWA Hiroyuki 
2933d0107eb0SKAMEZAWA Hiroyuki 	if (buf == buf_start)
2934d0107eb0SKAMEZAWA Hiroyuki 		return 0;
2935d0107eb0SKAMEZAWA Hiroyuki 	/* zero-fill memory holes */
2936d0107eb0SKAMEZAWA Hiroyuki 	if (buf != buf_start + buflen)
2937d0107eb0SKAMEZAWA Hiroyuki 		memset(buf, 0, buflen - (buf - buf_start));
2938d0107eb0SKAMEZAWA Hiroyuki 
2939d0107eb0SKAMEZAWA Hiroyuki 	return buflen;
29401da177e4SLinus Torvalds }
29411da177e4SLinus Torvalds 
2942d0107eb0SKAMEZAWA Hiroyuki /**
2943d0107eb0SKAMEZAWA Hiroyuki  * vwrite() - write vmalloc area in a safe way.
2944d0107eb0SKAMEZAWA Hiroyuki  * @buf:      buffer for source data
2945d0107eb0SKAMEZAWA Hiroyuki  * @addr:     vm address.
2946d0107eb0SKAMEZAWA Hiroyuki  * @count:    number of bytes to be read.
2947d0107eb0SKAMEZAWA Hiroyuki  *
2948d0107eb0SKAMEZAWA Hiroyuki  * This function checks that addr is a valid vmalloc'ed area, and
2949d0107eb0SKAMEZAWA Hiroyuki  * copy data from a buffer to the given addr. If specified range of
2950d0107eb0SKAMEZAWA Hiroyuki  * [addr...addr+count) includes some valid address, data is copied from
2951d0107eb0SKAMEZAWA Hiroyuki  * proper area of @buf. If there are memory holes, no copy to hole.
2952d0107eb0SKAMEZAWA Hiroyuki  * IOREMAP area is treated as memory hole and no copy is done.
2953d0107eb0SKAMEZAWA Hiroyuki  *
2954d0107eb0SKAMEZAWA Hiroyuki  * If [addr...addr+count) doesn't includes any intersects with alive
2955a8e5202dSCong Wang  * vm_struct area, returns 0. @buf should be kernel's buffer.
2956d0107eb0SKAMEZAWA Hiroyuki  *
2957d0107eb0SKAMEZAWA Hiroyuki  * Note: In usual ops, vwrite() is never necessary because the caller
2958d0107eb0SKAMEZAWA Hiroyuki  * should know vmalloc() area is valid and can use memcpy().
2959d0107eb0SKAMEZAWA Hiroyuki  * This is for routines which have to access vmalloc area without
2960d9009d67SGeert Uytterhoeven  * any information, as /dev/kmem.
2961a862f68aSMike Rapoport  *
2962a862f68aSMike Rapoport  * Return: number of bytes for which addr and buf should be
2963a862f68aSMike Rapoport  * increased (same number as @count) or %0 if [addr...addr+count)
2964a862f68aSMike Rapoport  * doesn't include any intersection with valid vmalloc area
2965d0107eb0SKAMEZAWA Hiroyuki  */
29661da177e4SLinus Torvalds long vwrite(char *buf, char *addr, unsigned long count)
29671da177e4SLinus Torvalds {
2968e81ce85fSJoonsoo Kim 	struct vmap_area *va;
2969e81ce85fSJoonsoo Kim 	struct vm_struct *vm;
2970d0107eb0SKAMEZAWA Hiroyuki 	char *vaddr;
2971d0107eb0SKAMEZAWA Hiroyuki 	unsigned long n, buflen;
2972d0107eb0SKAMEZAWA Hiroyuki 	int copied = 0;
29731da177e4SLinus Torvalds 
29741da177e4SLinus Torvalds 	/* Don't allow overflow */
29751da177e4SLinus Torvalds 	if ((unsigned long) addr + count < count)
29761da177e4SLinus Torvalds 		count = -(unsigned long) addr;
2977d0107eb0SKAMEZAWA Hiroyuki 	buflen = count;
29781da177e4SLinus Torvalds 
2979e81ce85fSJoonsoo Kim 	spin_lock(&vmap_area_lock);
2980e81ce85fSJoonsoo Kim 	list_for_each_entry(va, &vmap_area_list, list) {
2981e81ce85fSJoonsoo Kim 		if (!count)
2982e81ce85fSJoonsoo Kim 			break;
2983e81ce85fSJoonsoo Kim 
2984688fcbfcSPengfei Li 		if (!va->vm)
2985e81ce85fSJoonsoo Kim 			continue;
2986e81ce85fSJoonsoo Kim 
2987e81ce85fSJoonsoo Kim 		vm = va->vm;
2988e81ce85fSJoonsoo Kim 		vaddr = (char *) vm->addr;
2989762216abSWanpeng Li 		if (addr >= vaddr + get_vm_area_size(vm))
29901da177e4SLinus Torvalds 			continue;
29911da177e4SLinus Torvalds 		while (addr < vaddr) {
29921da177e4SLinus Torvalds 			if (count == 0)
29931da177e4SLinus Torvalds 				goto finished;
29941da177e4SLinus Torvalds 			buf++;
29951da177e4SLinus Torvalds 			addr++;
29961da177e4SLinus Torvalds 			count--;
29971da177e4SLinus Torvalds 		}
2998762216abSWanpeng Li 		n = vaddr + get_vm_area_size(vm) - addr;
2999d0107eb0SKAMEZAWA Hiroyuki 		if (n > count)
3000d0107eb0SKAMEZAWA Hiroyuki 			n = count;
3001e81ce85fSJoonsoo Kim 		if (!(vm->flags & VM_IOREMAP)) {
3002d0107eb0SKAMEZAWA Hiroyuki 			aligned_vwrite(buf, addr, n);
3003d0107eb0SKAMEZAWA Hiroyuki 			copied++;
3004d0107eb0SKAMEZAWA Hiroyuki 		}
3005d0107eb0SKAMEZAWA Hiroyuki 		buf += n;
3006d0107eb0SKAMEZAWA Hiroyuki 		addr += n;
3007d0107eb0SKAMEZAWA Hiroyuki 		count -= n;
30081da177e4SLinus Torvalds 	}
30091da177e4SLinus Torvalds finished:
3010e81ce85fSJoonsoo Kim 	spin_unlock(&vmap_area_lock);
3011d0107eb0SKAMEZAWA Hiroyuki 	if (!copied)
3012d0107eb0SKAMEZAWA Hiroyuki 		return 0;
3013d0107eb0SKAMEZAWA Hiroyuki 	return buflen;
30141da177e4SLinus Torvalds }
301583342314SNick Piggin 
301683342314SNick Piggin /**
3017e69e9d4aSHATAYAMA Daisuke  * remap_vmalloc_range_partial - map vmalloc pages to userspace
3018e69e9d4aSHATAYAMA Daisuke  * @vma:		vma to cover
3019e69e9d4aSHATAYAMA Daisuke  * @uaddr:		target user address to start at
3020e69e9d4aSHATAYAMA Daisuke  * @kaddr:		virtual address of vmalloc kernel memory
3021bdebd6a2SJann Horn  * @pgoff:		offset from @kaddr to start at
3022e69e9d4aSHATAYAMA Daisuke  * @size:		size of map area
3023e69e9d4aSHATAYAMA Daisuke  *
3024e69e9d4aSHATAYAMA Daisuke  * Returns:	0 for success, -Exxx on failure
3025e69e9d4aSHATAYAMA Daisuke  *
3026e69e9d4aSHATAYAMA Daisuke  * This function checks that @kaddr is a valid vmalloc'ed area,
3027e69e9d4aSHATAYAMA Daisuke  * and that it is big enough to cover the range starting at
3028e69e9d4aSHATAYAMA Daisuke  * @uaddr in @vma. Will return failure if that criteria isn't
3029e69e9d4aSHATAYAMA Daisuke  * met.
3030e69e9d4aSHATAYAMA Daisuke  *
3031e69e9d4aSHATAYAMA Daisuke  * Similar to remap_pfn_range() (see mm/memory.c)
3032e69e9d4aSHATAYAMA Daisuke  */
3033e69e9d4aSHATAYAMA Daisuke int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr,
3034bdebd6a2SJann Horn 				void *kaddr, unsigned long pgoff,
3035bdebd6a2SJann Horn 				unsigned long size)
3036e69e9d4aSHATAYAMA Daisuke {
3037e69e9d4aSHATAYAMA Daisuke 	struct vm_struct *area;
3038bdebd6a2SJann Horn 	unsigned long off;
3039bdebd6a2SJann Horn 	unsigned long end_index;
3040bdebd6a2SJann Horn 
3041bdebd6a2SJann Horn 	if (check_shl_overflow(pgoff, PAGE_SHIFT, &off))
3042bdebd6a2SJann Horn 		return -EINVAL;
3043e69e9d4aSHATAYAMA Daisuke 
3044e69e9d4aSHATAYAMA Daisuke 	size = PAGE_ALIGN(size);
3045e69e9d4aSHATAYAMA Daisuke 
3046e69e9d4aSHATAYAMA Daisuke 	if (!PAGE_ALIGNED(uaddr) || !PAGE_ALIGNED(kaddr))
3047e69e9d4aSHATAYAMA Daisuke 		return -EINVAL;
3048e69e9d4aSHATAYAMA Daisuke 
3049e69e9d4aSHATAYAMA Daisuke 	area = find_vm_area(kaddr);
3050e69e9d4aSHATAYAMA Daisuke 	if (!area)
3051e69e9d4aSHATAYAMA Daisuke 		return -EINVAL;
3052e69e9d4aSHATAYAMA Daisuke 
3053fe9041c2SChristoph Hellwig 	if (!(area->flags & (VM_USERMAP | VM_DMA_COHERENT)))
3054e69e9d4aSHATAYAMA Daisuke 		return -EINVAL;
3055e69e9d4aSHATAYAMA Daisuke 
3056bdebd6a2SJann Horn 	if (check_add_overflow(size, off, &end_index) ||
3057bdebd6a2SJann Horn 	    end_index > get_vm_area_size(area))
3058e69e9d4aSHATAYAMA Daisuke 		return -EINVAL;
3059bdebd6a2SJann Horn 	kaddr += off;
3060e69e9d4aSHATAYAMA Daisuke 
3061e69e9d4aSHATAYAMA Daisuke 	do {
3062e69e9d4aSHATAYAMA Daisuke 		struct page *page = vmalloc_to_page(kaddr);
3063e69e9d4aSHATAYAMA Daisuke 		int ret;
3064e69e9d4aSHATAYAMA Daisuke 
3065e69e9d4aSHATAYAMA Daisuke 		ret = vm_insert_page(vma, uaddr, page);
3066e69e9d4aSHATAYAMA Daisuke 		if (ret)
3067e69e9d4aSHATAYAMA Daisuke 			return ret;
3068e69e9d4aSHATAYAMA Daisuke 
3069e69e9d4aSHATAYAMA Daisuke 		uaddr += PAGE_SIZE;
3070e69e9d4aSHATAYAMA Daisuke 		kaddr += PAGE_SIZE;
3071e69e9d4aSHATAYAMA Daisuke 		size -= PAGE_SIZE;
3072e69e9d4aSHATAYAMA Daisuke 	} while (size > 0);
3073e69e9d4aSHATAYAMA Daisuke 
3074e69e9d4aSHATAYAMA Daisuke 	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3075e69e9d4aSHATAYAMA Daisuke 
3076e69e9d4aSHATAYAMA Daisuke 	return 0;
3077e69e9d4aSHATAYAMA Daisuke }
3078e69e9d4aSHATAYAMA Daisuke 
3079e69e9d4aSHATAYAMA Daisuke /**
308083342314SNick Piggin  * remap_vmalloc_range - map vmalloc pages to userspace
308183342314SNick Piggin  * @vma:		vma to cover (map full range of vma)
308283342314SNick Piggin  * @addr:		vmalloc memory
308383342314SNick Piggin  * @pgoff:		number of pages into addr before first page to map
30847682486bSRandy Dunlap  *
30857682486bSRandy Dunlap  * Returns:	0 for success, -Exxx on failure
308683342314SNick Piggin  *
308783342314SNick Piggin  * This function checks that addr is a valid vmalloc'ed area, and
308883342314SNick Piggin  * that it is big enough to cover the vma. Will return failure if
308983342314SNick Piggin  * that criteria isn't met.
309083342314SNick Piggin  *
309172fd4a35SRobert P. J. Day  * Similar to remap_pfn_range() (see mm/memory.c)
309283342314SNick Piggin  */
309383342314SNick Piggin int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
309483342314SNick Piggin 						unsigned long pgoff)
309583342314SNick Piggin {
3096e69e9d4aSHATAYAMA Daisuke 	return remap_vmalloc_range_partial(vma, vma->vm_start,
3097bdebd6a2SJann Horn 					   addr, pgoff,
3098e69e9d4aSHATAYAMA Daisuke 					   vma->vm_end - vma->vm_start);
309983342314SNick Piggin }
310083342314SNick Piggin EXPORT_SYMBOL(remap_vmalloc_range);
310183342314SNick Piggin 
31025f4352fbSJeremy Fitzhardinge void free_vm_area(struct vm_struct *area)
31035f4352fbSJeremy Fitzhardinge {
31045f4352fbSJeremy Fitzhardinge 	struct vm_struct *ret;
31055f4352fbSJeremy Fitzhardinge 	ret = remove_vm_area(area->addr);
31065f4352fbSJeremy Fitzhardinge 	BUG_ON(ret != area);
31075f4352fbSJeremy Fitzhardinge 	kfree(area);
31085f4352fbSJeremy Fitzhardinge }
31095f4352fbSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(free_vm_area);
3110a10aa579SChristoph Lameter 
31114f8b02b4STejun Heo #ifdef CONFIG_SMP
3112ca23e405STejun Heo static struct vmap_area *node_to_va(struct rb_node *n)
3113ca23e405STejun Heo {
31144583e773SGeliang Tang 	return rb_entry_safe(n, struct vmap_area, rb_node);
3115ca23e405STejun Heo }
3116ca23e405STejun Heo 
3117ca23e405STejun Heo /**
311868ad4a33SUladzislau Rezki (Sony)  * pvm_find_va_enclose_addr - find the vmap_area @addr belongs to
311968ad4a33SUladzislau Rezki (Sony)  * @addr: target address
3120ca23e405STejun Heo  *
312168ad4a33SUladzislau Rezki (Sony)  * Returns: vmap_area if it is found. If there is no such area
312268ad4a33SUladzislau Rezki (Sony)  *   the first highest(reverse order) vmap_area is returned
312368ad4a33SUladzislau Rezki (Sony)  *   i.e. va->va_start < addr && va->va_end < addr or NULL
312468ad4a33SUladzislau Rezki (Sony)  *   if there are no any areas before @addr.
3125ca23e405STejun Heo  */
312668ad4a33SUladzislau Rezki (Sony) static struct vmap_area *
312768ad4a33SUladzislau Rezki (Sony) pvm_find_va_enclose_addr(unsigned long addr)
3128ca23e405STejun Heo {
312968ad4a33SUladzislau Rezki (Sony) 	struct vmap_area *va, *tmp;
313068ad4a33SUladzislau Rezki (Sony) 	struct rb_node *n;
313168ad4a33SUladzislau Rezki (Sony) 
313268ad4a33SUladzislau Rezki (Sony) 	n = free_vmap_area_root.rb_node;
313368ad4a33SUladzislau Rezki (Sony) 	va = NULL;
3134ca23e405STejun Heo 
3135ca23e405STejun Heo 	while (n) {
313668ad4a33SUladzislau Rezki (Sony) 		tmp = rb_entry(n, struct vmap_area, rb_node);
313768ad4a33SUladzislau Rezki (Sony) 		if (tmp->va_start <= addr) {
313868ad4a33SUladzislau Rezki (Sony) 			va = tmp;
313968ad4a33SUladzislau Rezki (Sony) 			if (tmp->va_end >= addr)
3140ca23e405STejun Heo 				break;
3141ca23e405STejun Heo 
314268ad4a33SUladzislau Rezki (Sony) 			n = n->rb_right;
3143ca23e405STejun Heo 		} else {
314468ad4a33SUladzislau Rezki (Sony) 			n = n->rb_left;
3145ca23e405STejun Heo 		}
314668ad4a33SUladzislau Rezki (Sony) 	}
314768ad4a33SUladzislau Rezki (Sony) 
314868ad4a33SUladzislau Rezki (Sony) 	return va;
3149ca23e405STejun Heo }
3150ca23e405STejun Heo 
3151ca23e405STejun Heo /**
315268ad4a33SUladzislau Rezki (Sony)  * pvm_determine_end_from_reverse - find the highest aligned address
315368ad4a33SUladzislau Rezki (Sony)  * of free block below VMALLOC_END
315468ad4a33SUladzislau Rezki (Sony)  * @va:
315568ad4a33SUladzislau Rezki (Sony)  *   in - the VA we start the search(reverse order);
315668ad4a33SUladzislau Rezki (Sony)  *   out - the VA with the highest aligned end address.
3157799fa85dSAlex Shi  * @align: alignment for required highest address
3158ca23e405STejun Heo  *
315968ad4a33SUladzislau Rezki (Sony)  * Returns: determined end address within vmap_area
3160ca23e405STejun Heo  */
316168ad4a33SUladzislau Rezki (Sony) static unsigned long
316268ad4a33SUladzislau Rezki (Sony) pvm_determine_end_from_reverse(struct vmap_area **va, unsigned long align)
3163ca23e405STejun Heo {
316468ad4a33SUladzislau Rezki (Sony) 	unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
3165ca23e405STejun Heo 	unsigned long addr;
3166ca23e405STejun Heo 
316768ad4a33SUladzislau Rezki (Sony) 	if (likely(*va)) {
316868ad4a33SUladzislau Rezki (Sony) 		list_for_each_entry_from_reverse((*va),
316968ad4a33SUladzislau Rezki (Sony) 				&free_vmap_area_list, list) {
317068ad4a33SUladzislau Rezki (Sony) 			addr = min((*va)->va_end & ~(align - 1), vmalloc_end);
317168ad4a33SUladzislau Rezki (Sony) 			if ((*va)->va_start < addr)
317268ad4a33SUladzislau Rezki (Sony) 				return addr;
317368ad4a33SUladzislau Rezki (Sony) 		}
3174ca23e405STejun Heo 	}
3175ca23e405STejun Heo 
317668ad4a33SUladzislau Rezki (Sony) 	return 0;
3177ca23e405STejun Heo }
3178ca23e405STejun Heo 
3179ca23e405STejun Heo /**
3180ca23e405STejun Heo  * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator
3181ca23e405STejun Heo  * @offsets: array containing offset of each area
3182ca23e405STejun Heo  * @sizes: array containing size of each area
3183ca23e405STejun Heo  * @nr_vms: the number of areas to allocate
3184ca23e405STejun Heo  * @align: alignment, all entries in @offsets and @sizes must be aligned to this
3185ca23e405STejun Heo  *
3186ca23e405STejun Heo  * Returns: kmalloc'd vm_struct pointer array pointing to allocated
3187ca23e405STejun Heo  *	    vm_structs on success, %NULL on failure
3188ca23e405STejun Heo  *
3189ca23e405STejun Heo  * Percpu allocator wants to use congruent vm areas so that it can
3190ca23e405STejun Heo  * maintain the offsets among percpu areas.  This function allocates
3191ec3f64fcSDavid Rientjes  * congruent vmalloc areas for it with GFP_KERNEL.  These areas tend to
3192ec3f64fcSDavid Rientjes  * be scattered pretty far, distance between two areas easily going up
3193ec3f64fcSDavid Rientjes  * to gigabytes.  To avoid interacting with regular vmallocs, these
3194ec3f64fcSDavid Rientjes  * areas are allocated from top.
3195ca23e405STejun Heo  *
3196ca23e405STejun Heo  * Despite its complicated look, this allocator is rather simple. It
319768ad4a33SUladzislau Rezki (Sony)  * does everything top-down and scans free blocks from the end looking
319868ad4a33SUladzislau Rezki (Sony)  * for matching base. While scanning, if any of the areas do not fit the
319968ad4a33SUladzislau Rezki (Sony)  * base address is pulled down to fit the area. Scanning is repeated till
320068ad4a33SUladzislau Rezki (Sony)  * all the areas fit and then all necessary data structures are inserted
320168ad4a33SUladzislau Rezki (Sony)  * and the result is returned.
3202ca23e405STejun Heo  */
3203ca23e405STejun Heo struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
3204ca23e405STejun Heo 				     const size_t *sizes, int nr_vms,
3205ec3f64fcSDavid Rientjes 				     size_t align)
3206ca23e405STejun Heo {
3207ca23e405STejun Heo 	const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align);
3208ca23e405STejun Heo 	const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
320968ad4a33SUladzislau Rezki (Sony) 	struct vmap_area **vas, *va;
3210ca23e405STejun Heo 	struct vm_struct **vms;
3211ca23e405STejun Heo 	int area, area2, last_area, term_area;
3212253a496dSDaniel Axtens 	unsigned long base, start, size, end, last_end, orig_start, orig_end;
3213ca23e405STejun Heo 	bool purged = false;
321468ad4a33SUladzislau Rezki (Sony) 	enum fit_type type;
3215ca23e405STejun Heo 
3216ca23e405STejun Heo 	/* verify parameters and allocate data structures */
3217891c49abSAlexander Kuleshov 	BUG_ON(offset_in_page(align) || !is_power_of_2(align));
3218ca23e405STejun Heo 	for (last_area = 0, area = 0; area < nr_vms; area++) {
3219ca23e405STejun Heo 		start = offsets[area];
3220ca23e405STejun Heo 		end = start + sizes[area];
3221ca23e405STejun Heo 
3222ca23e405STejun Heo 		/* is everything aligned properly? */
3223ca23e405STejun Heo 		BUG_ON(!IS_ALIGNED(offsets[area], align));
3224ca23e405STejun Heo 		BUG_ON(!IS_ALIGNED(sizes[area], align));
3225ca23e405STejun Heo 
3226ca23e405STejun Heo 		/* detect the area with the highest address */
3227ca23e405STejun Heo 		if (start > offsets[last_area])
3228ca23e405STejun Heo 			last_area = area;
3229ca23e405STejun Heo 
3230c568da28SWei Yang 		for (area2 = area + 1; area2 < nr_vms; area2++) {
3231ca23e405STejun Heo 			unsigned long start2 = offsets[area2];
3232ca23e405STejun Heo 			unsigned long end2 = start2 + sizes[area2];
3233ca23e405STejun Heo 
3234c568da28SWei Yang 			BUG_ON(start2 < end && start < end2);
3235ca23e405STejun Heo 		}
3236ca23e405STejun Heo 	}
3237ca23e405STejun Heo 	last_end = offsets[last_area] + sizes[last_area];
3238ca23e405STejun Heo 
3239ca23e405STejun Heo 	if (vmalloc_end - vmalloc_start < last_end) {
3240ca23e405STejun Heo 		WARN_ON(true);
3241ca23e405STejun Heo 		return NULL;
3242ca23e405STejun Heo 	}
3243ca23e405STejun Heo 
32444d67d860SThomas Meyer 	vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL);
32454d67d860SThomas Meyer 	vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL);
3246ca23e405STejun Heo 	if (!vas || !vms)
3247f1db7afdSKautuk Consul 		goto err_free2;
3248ca23e405STejun Heo 
3249ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++) {
325068ad4a33SUladzislau Rezki (Sony) 		vas[area] = kmem_cache_zalloc(vmap_area_cachep, GFP_KERNEL);
3251ec3f64fcSDavid Rientjes 		vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL);
3252ca23e405STejun Heo 		if (!vas[area] || !vms[area])
3253ca23e405STejun Heo 			goto err_free;
3254ca23e405STejun Heo 	}
3255ca23e405STejun Heo retry:
3256e36176beSUladzislau Rezki (Sony) 	spin_lock(&free_vmap_area_lock);
3257ca23e405STejun Heo 
3258ca23e405STejun Heo 	/* start scanning - we scan from the top, begin with the last area */
3259ca23e405STejun Heo 	area = term_area = last_area;
3260ca23e405STejun Heo 	start = offsets[area];
3261ca23e405STejun Heo 	end = start + sizes[area];
3262ca23e405STejun Heo 
326368ad4a33SUladzislau Rezki (Sony) 	va = pvm_find_va_enclose_addr(vmalloc_end);
326468ad4a33SUladzislau Rezki (Sony) 	base = pvm_determine_end_from_reverse(&va, align) - end;
3265ca23e405STejun Heo 
3266ca23e405STejun Heo 	while (true) {
3267ca23e405STejun Heo 		/*
3268ca23e405STejun Heo 		 * base might have underflowed, add last_end before
3269ca23e405STejun Heo 		 * comparing.
3270ca23e405STejun Heo 		 */
327168ad4a33SUladzislau Rezki (Sony) 		if (base + last_end < vmalloc_start + last_end)
327268ad4a33SUladzislau Rezki (Sony) 			goto overflow;
3273ca23e405STejun Heo 
3274ca23e405STejun Heo 		/*
327568ad4a33SUladzislau Rezki (Sony) 		 * Fitting base has not been found.
3276ca23e405STejun Heo 		 */
327768ad4a33SUladzislau Rezki (Sony) 		if (va == NULL)
327868ad4a33SUladzislau Rezki (Sony) 			goto overflow;
3279ca23e405STejun Heo 
3280ca23e405STejun Heo 		/*
3281d8cc323dSQiujun Huang 		 * If required width exceeds current VA block, move
32825336e52cSKuppuswamy Sathyanarayanan 		 * base downwards and then recheck.
32835336e52cSKuppuswamy Sathyanarayanan 		 */
32845336e52cSKuppuswamy Sathyanarayanan 		if (base + end > va->va_end) {
32855336e52cSKuppuswamy Sathyanarayanan 			base = pvm_determine_end_from_reverse(&va, align) - end;
32865336e52cSKuppuswamy Sathyanarayanan 			term_area = area;
32875336e52cSKuppuswamy Sathyanarayanan 			continue;
32885336e52cSKuppuswamy Sathyanarayanan 		}
32895336e52cSKuppuswamy Sathyanarayanan 
32905336e52cSKuppuswamy Sathyanarayanan 		/*
329168ad4a33SUladzislau Rezki (Sony) 		 * If this VA does not fit, move base downwards and recheck.
3292ca23e405STejun Heo 		 */
32935336e52cSKuppuswamy Sathyanarayanan 		if (base + start < va->va_start) {
329468ad4a33SUladzislau Rezki (Sony) 			va = node_to_va(rb_prev(&va->rb_node));
329568ad4a33SUladzislau Rezki (Sony) 			base = pvm_determine_end_from_reverse(&va, align) - end;
3296ca23e405STejun Heo 			term_area = area;
3297ca23e405STejun Heo 			continue;
3298ca23e405STejun Heo 		}
3299ca23e405STejun Heo 
3300ca23e405STejun Heo 		/*
3301ca23e405STejun Heo 		 * This area fits, move on to the previous one.  If
3302ca23e405STejun Heo 		 * the previous one is the terminal one, we're done.
3303ca23e405STejun Heo 		 */
3304ca23e405STejun Heo 		area = (area + nr_vms - 1) % nr_vms;
3305ca23e405STejun Heo 		if (area == term_area)
3306ca23e405STejun Heo 			break;
330768ad4a33SUladzislau Rezki (Sony) 
3308ca23e405STejun Heo 		start = offsets[area];
3309ca23e405STejun Heo 		end = start + sizes[area];
331068ad4a33SUladzislau Rezki (Sony) 		va = pvm_find_va_enclose_addr(base + end);
3311ca23e405STejun Heo 	}
331268ad4a33SUladzislau Rezki (Sony) 
3313ca23e405STejun Heo 	/* we've found a fitting base, insert all va's */
3314ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++) {
331568ad4a33SUladzislau Rezki (Sony) 		int ret;
3316ca23e405STejun Heo 
331768ad4a33SUladzislau Rezki (Sony) 		start = base + offsets[area];
331868ad4a33SUladzislau Rezki (Sony) 		size = sizes[area];
331968ad4a33SUladzislau Rezki (Sony) 
332068ad4a33SUladzislau Rezki (Sony) 		va = pvm_find_va_enclose_addr(start);
332168ad4a33SUladzislau Rezki (Sony) 		if (WARN_ON_ONCE(va == NULL))
332268ad4a33SUladzislau Rezki (Sony) 			/* It is a BUG(), but trigger recovery instead. */
332368ad4a33SUladzislau Rezki (Sony) 			goto recovery;
332468ad4a33SUladzislau Rezki (Sony) 
332568ad4a33SUladzislau Rezki (Sony) 		type = classify_va_fit_type(va, start, size);
332668ad4a33SUladzislau Rezki (Sony) 		if (WARN_ON_ONCE(type == NOTHING_FIT))
332768ad4a33SUladzislau Rezki (Sony) 			/* It is a BUG(), but trigger recovery instead. */
332868ad4a33SUladzislau Rezki (Sony) 			goto recovery;
332968ad4a33SUladzislau Rezki (Sony) 
333068ad4a33SUladzislau Rezki (Sony) 		ret = adjust_va_to_fit_type(va, start, size, type);
333168ad4a33SUladzislau Rezki (Sony) 		if (unlikely(ret))
333268ad4a33SUladzislau Rezki (Sony) 			goto recovery;
333368ad4a33SUladzislau Rezki (Sony) 
333468ad4a33SUladzislau Rezki (Sony) 		/* Allocated area. */
333568ad4a33SUladzislau Rezki (Sony) 		va = vas[area];
333668ad4a33SUladzislau Rezki (Sony) 		va->va_start = start;
333768ad4a33SUladzislau Rezki (Sony) 		va->va_end = start + size;
3338ca23e405STejun Heo 	}
3339ca23e405STejun Heo 
3340e36176beSUladzislau Rezki (Sony) 	spin_unlock(&free_vmap_area_lock);
3341ca23e405STejun Heo 
3342253a496dSDaniel Axtens 	/* populate the kasan shadow space */
3343253a496dSDaniel Axtens 	for (area = 0; area < nr_vms; area++) {
3344253a496dSDaniel Axtens 		if (kasan_populate_vmalloc(vas[area]->va_start, sizes[area]))
3345253a496dSDaniel Axtens 			goto err_free_shadow;
3346253a496dSDaniel Axtens 
3347253a496dSDaniel Axtens 		kasan_unpoison_vmalloc((void *)vas[area]->va_start,
3348253a496dSDaniel Axtens 				       sizes[area]);
3349253a496dSDaniel Axtens 	}
3350253a496dSDaniel Axtens 
3351ca23e405STejun Heo 	/* insert all vm's */
3352e36176beSUladzislau Rezki (Sony) 	spin_lock(&vmap_area_lock);
3353e36176beSUladzislau Rezki (Sony) 	for (area = 0; area < nr_vms; area++) {
3354e36176beSUladzislau Rezki (Sony) 		insert_vmap_area(vas[area], &vmap_area_root, &vmap_area_list);
3355e36176beSUladzislau Rezki (Sony) 
3356e36176beSUladzislau Rezki (Sony) 		setup_vmalloc_vm_locked(vms[area], vas[area], VM_ALLOC,
3357ca23e405STejun Heo 				 pcpu_get_vm_areas);
3358e36176beSUladzislau Rezki (Sony) 	}
3359e36176beSUladzislau Rezki (Sony) 	spin_unlock(&vmap_area_lock);
3360ca23e405STejun Heo 
3361ca23e405STejun Heo 	kfree(vas);
3362ca23e405STejun Heo 	return vms;
3363ca23e405STejun Heo 
336468ad4a33SUladzislau Rezki (Sony) recovery:
3365e36176beSUladzislau Rezki (Sony) 	/*
3366e36176beSUladzislau Rezki (Sony) 	 * Remove previously allocated areas. There is no
3367e36176beSUladzislau Rezki (Sony) 	 * need in removing these areas from the busy tree,
3368e36176beSUladzislau Rezki (Sony) 	 * because they are inserted only on the final step
3369e36176beSUladzislau Rezki (Sony) 	 * and when pcpu_get_vm_areas() is success.
3370e36176beSUladzislau Rezki (Sony) 	 */
337168ad4a33SUladzislau Rezki (Sony) 	while (area--) {
3372253a496dSDaniel Axtens 		orig_start = vas[area]->va_start;
3373253a496dSDaniel Axtens 		orig_end = vas[area]->va_end;
337496e2db45SUladzislau Rezki (Sony) 		va = merge_or_add_vmap_area_augment(vas[area], &free_vmap_area_root,
33753c5c3cfbSDaniel Axtens 				&free_vmap_area_list);
33769c801f61SUladzislau Rezki (Sony) 		if (va)
3377253a496dSDaniel Axtens 			kasan_release_vmalloc(orig_start, orig_end,
3378253a496dSDaniel Axtens 				va->va_start, va->va_end);
337968ad4a33SUladzislau Rezki (Sony) 		vas[area] = NULL;
338068ad4a33SUladzislau Rezki (Sony) 	}
338168ad4a33SUladzislau Rezki (Sony) 
338268ad4a33SUladzislau Rezki (Sony) overflow:
3383e36176beSUladzislau Rezki (Sony) 	spin_unlock(&free_vmap_area_lock);
338468ad4a33SUladzislau Rezki (Sony) 	if (!purged) {
338568ad4a33SUladzislau Rezki (Sony) 		purge_vmap_area_lazy();
338668ad4a33SUladzislau Rezki (Sony) 		purged = true;
338768ad4a33SUladzislau Rezki (Sony) 
338868ad4a33SUladzislau Rezki (Sony) 		/* Before "retry", check if we recover. */
338968ad4a33SUladzislau Rezki (Sony) 		for (area = 0; area < nr_vms; area++) {
339068ad4a33SUladzislau Rezki (Sony) 			if (vas[area])
339168ad4a33SUladzislau Rezki (Sony) 				continue;
339268ad4a33SUladzislau Rezki (Sony) 
339368ad4a33SUladzislau Rezki (Sony) 			vas[area] = kmem_cache_zalloc(
339468ad4a33SUladzislau Rezki (Sony) 				vmap_area_cachep, GFP_KERNEL);
339568ad4a33SUladzislau Rezki (Sony) 			if (!vas[area])
339668ad4a33SUladzislau Rezki (Sony) 				goto err_free;
339768ad4a33SUladzislau Rezki (Sony) 		}
339868ad4a33SUladzislau Rezki (Sony) 
339968ad4a33SUladzislau Rezki (Sony) 		goto retry;
340068ad4a33SUladzislau Rezki (Sony) 	}
340168ad4a33SUladzislau Rezki (Sony) 
3402ca23e405STejun Heo err_free:
3403ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++) {
340468ad4a33SUladzislau Rezki (Sony) 		if (vas[area])
340568ad4a33SUladzislau Rezki (Sony) 			kmem_cache_free(vmap_area_cachep, vas[area]);
340668ad4a33SUladzislau Rezki (Sony) 
3407ca23e405STejun Heo 		kfree(vms[area]);
3408ca23e405STejun Heo 	}
3409f1db7afdSKautuk Consul err_free2:
3410ca23e405STejun Heo 	kfree(vas);
3411ca23e405STejun Heo 	kfree(vms);
3412ca23e405STejun Heo 	return NULL;
3413253a496dSDaniel Axtens 
3414253a496dSDaniel Axtens err_free_shadow:
3415253a496dSDaniel Axtens 	spin_lock(&free_vmap_area_lock);
3416253a496dSDaniel Axtens 	/*
3417253a496dSDaniel Axtens 	 * We release all the vmalloc shadows, even the ones for regions that
3418253a496dSDaniel Axtens 	 * hadn't been successfully added. This relies on kasan_release_vmalloc
3419253a496dSDaniel Axtens 	 * being able to tolerate this case.
3420253a496dSDaniel Axtens 	 */
3421253a496dSDaniel Axtens 	for (area = 0; area < nr_vms; area++) {
3422253a496dSDaniel Axtens 		orig_start = vas[area]->va_start;
3423253a496dSDaniel Axtens 		orig_end = vas[area]->va_end;
342496e2db45SUladzislau Rezki (Sony) 		va = merge_or_add_vmap_area_augment(vas[area], &free_vmap_area_root,
3425253a496dSDaniel Axtens 				&free_vmap_area_list);
34269c801f61SUladzislau Rezki (Sony) 		if (va)
3427253a496dSDaniel Axtens 			kasan_release_vmalloc(orig_start, orig_end,
3428253a496dSDaniel Axtens 				va->va_start, va->va_end);
3429253a496dSDaniel Axtens 		vas[area] = NULL;
3430253a496dSDaniel Axtens 		kfree(vms[area]);
3431253a496dSDaniel Axtens 	}
3432253a496dSDaniel Axtens 	spin_unlock(&free_vmap_area_lock);
3433253a496dSDaniel Axtens 	kfree(vas);
3434253a496dSDaniel Axtens 	kfree(vms);
3435253a496dSDaniel Axtens 	return NULL;
3436ca23e405STejun Heo }
3437ca23e405STejun Heo 
3438ca23e405STejun Heo /**
3439ca23e405STejun Heo  * pcpu_free_vm_areas - free vmalloc areas for percpu allocator
3440ca23e405STejun Heo  * @vms: vm_struct pointer array returned by pcpu_get_vm_areas()
3441ca23e405STejun Heo  * @nr_vms: the number of allocated areas
3442ca23e405STejun Heo  *
3443ca23e405STejun Heo  * Free vm_structs and the array allocated by pcpu_get_vm_areas().
3444ca23e405STejun Heo  */
3445ca23e405STejun Heo void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
3446ca23e405STejun Heo {
3447ca23e405STejun Heo 	int i;
3448ca23e405STejun Heo 
3449ca23e405STejun Heo 	for (i = 0; i < nr_vms; i++)
3450ca23e405STejun Heo 		free_vm_area(vms[i]);
3451ca23e405STejun Heo 	kfree(vms);
3452ca23e405STejun Heo }
34534f8b02b4STejun Heo #endif	/* CONFIG_SMP */
3454a10aa579SChristoph Lameter 
34555bb1bb35SPaul E. McKenney #ifdef CONFIG_PRINTK
345698f18083SPaul E. McKenney bool vmalloc_dump_obj(void *object)
345798f18083SPaul E. McKenney {
345898f18083SPaul E. McKenney 	struct vm_struct *vm;
345998f18083SPaul E. McKenney 	void *objp = (void *)PAGE_ALIGN((unsigned long)object);
346098f18083SPaul E. McKenney 
346198f18083SPaul E. McKenney 	vm = find_vm_area(objp);
346298f18083SPaul E. McKenney 	if (!vm)
346398f18083SPaul E. McKenney 		return false;
3464bd34dcd4SPaul E. McKenney 	pr_cont(" %u-page vmalloc region starting at %#lx allocated at %pS\n",
3465bd34dcd4SPaul E. McKenney 		vm->nr_pages, (unsigned long)vm->addr, vm->caller);
346698f18083SPaul E. McKenney 	return true;
346798f18083SPaul E. McKenney }
34685bb1bb35SPaul E. McKenney #endif
346998f18083SPaul E. McKenney 
3470a10aa579SChristoph Lameter #ifdef CONFIG_PROC_FS
3471a10aa579SChristoph Lameter static void *s_start(struct seq_file *m, loff_t *pos)
3472e36176beSUladzislau Rezki (Sony) 	__acquires(&vmap_purge_lock)
3473d4033afdSJoonsoo Kim 	__acquires(&vmap_area_lock)
3474a10aa579SChristoph Lameter {
3475e36176beSUladzislau Rezki (Sony) 	mutex_lock(&vmap_purge_lock);
3476d4033afdSJoonsoo Kim 	spin_lock(&vmap_area_lock);
3477e36176beSUladzislau Rezki (Sony) 
34783f500069Szijun_hu 	return seq_list_start(&vmap_area_list, *pos);
3479a10aa579SChristoph Lameter }
3480a10aa579SChristoph Lameter 
3481a10aa579SChristoph Lameter static void *s_next(struct seq_file *m, void *p, loff_t *pos)
3482a10aa579SChristoph Lameter {
34833f500069Szijun_hu 	return seq_list_next(p, &vmap_area_list, pos);
3484a10aa579SChristoph Lameter }
3485a10aa579SChristoph Lameter 
3486a10aa579SChristoph Lameter static void s_stop(struct seq_file *m, void *p)
3487d4033afdSJoonsoo Kim 	__releases(&vmap_area_lock)
34880a7dd4e9SWaiman Long 	__releases(&vmap_purge_lock)
3489a10aa579SChristoph Lameter {
3490d4033afdSJoonsoo Kim 	spin_unlock(&vmap_area_lock);
34910a7dd4e9SWaiman Long 	mutex_unlock(&vmap_purge_lock);
3492a10aa579SChristoph Lameter }
3493a10aa579SChristoph Lameter 
3494a47a126aSEric Dumazet static void show_numa_info(struct seq_file *m, struct vm_struct *v)
3495a47a126aSEric Dumazet {
3496e5adfffcSKirill A. Shutemov 	if (IS_ENABLED(CONFIG_NUMA)) {
3497a47a126aSEric Dumazet 		unsigned int nr, *counters = m->private;
3498a47a126aSEric Dumazet 
3499a47a126aSEric Dumazet 		if (!counters)
3500a47a126aSEric Dumazet 			return;
3501a47a126aSEric Dumazet 
3502af12346cSWanpeng Li 		if (v->flags & VM_UNINITIALIZED)
3503af12346cSWanpeng Li 			return;
35047e5b528bSDmitry Vyukov 		/* Pair with smp_wmb() in clear_vm_uninitialized_flag() */
35057e5b528bSDmitry Vyukov 		smp_rmb();
3506af12346cSWanpeng Li 
3507a47a126aSEric Dumazet 		memset(counters, 0, nr_node_ids * sizeof(unsigned int));
3508a47a126aSEric Dumazet 
3509a47a126aSEric Dumazet 		for (nr = 0; nr < v->nr_pages; nr++)
3510a47a126aSEric Dumazet 			counters[page_to_nid(v->pages[nr])]++;
3511a47a126aSEric Dumazet 
3512a47a126aSEric Dumazet 		for_each_node_state(nr, N_HIGH_MEMORY)
3513a47a126aSEric Dumazet 			if (counters[nr])
3514a47a126aSEric Dumazet 				seq_printf(m, " N%u=%u", nr, counters[nr]);
3515a47a126aSEric Dumazet 	}
3516a47a126aSEric Dumazet }
3517a47a126aSEric Dumazet 
3518dd3b8353SUladzislau Rezki (Sony) static void show_purge_info(struct seq_file *m)
3519dd3b8353SUladzislau Rezki (Sony) {
3520dd3b8353SUladzislau Rezki (Sony) 	struct vmap_area *va;
3521dd3b8353SUladzislau Rezki (Sony) 
352296e2db45SUladzislau Rezki (Sony) 	spin_lock(&purge_vmap_area_lock);
352396e2db45SUladzislau Rezki (Sony) 	list_for_each_entry(va, &purge_vmap_area_list, list) {
3524dd3b8353SUladzislau Rezki (Sony) 		seq_printf(m, "0x%pK-0x%pK %7ld unpurged vm_area\n",
3525dd3b8353SUladzislau Rezki (Sony) 			(void *)va->va_start, (void *)va->va_end,
3526dd3b8353SUladzislau Rezki (Sony) 			va->va_end - va->va_start);
3527dd3b8353SUladzislau Rezki (Sony) 	}
352896e2db45SUladzislau Rezki (Sony) 	spin_unlock(&purge_vmap_area_lock);
3529dd3b8353SUladzislau Rezki (Sony) }
3530dd3b8353SUladzislau Rezki (Sony) 
3531a10aa579SChristoph Lameter static int s_show(struct seq_file *m, void *p)
3532a10aa579SChristoph Lameter {
35333f500069Szijun_hu 	struct vmap_area *va;
3534d4033afdSJoonsoo Kim 	struct vm_struct *v;
3535d4033afdSJoonsoo Kim 
35363f500069Szijun_hu 	va = list_entry(p, struct vmap_area, list);
35373f500069Szijun_hu 
3538c2ce8c14SWanpeng Li 	/*
3539688fcbfcSPengfei Li 	 * s_show can encounter race with remove_vm_area, !vm on behalf
3540688fcbfcSPengfei Li 	 * of vmap area is being tear down or vm_map_ram allocation.
3541c2ce8c14SWanpeng Li 	 */
3542688fcbfcSPengfei Li 	if (!va->vm) {
3543dd3b8353SUladzislau Rezki (Sony) 		seq_printf(m, "0x%pK-0x%pK %7ld vm_map_ram\n",
354478c72746SYisheng Xie 			(void *)va->va_start, (void *)va->va_end,
3545dd3b8353SUladzislau Rezki (Sony) 			va->va_end - va->va_start);
354678c72746SYisheng Xie 
3547d4033afdSJoonsoo Kim 		return 0;
354878c72746SYisheng Xie 	}
3549d4033afdSJoonsoo Kim 
3550d4033afdSJoonsoo Kim 	v = va->vm;
3551a10aa579SChristoph Lameter 
355245ec1690SKees Cook 	seq_printf(m, "0x%pK-0x%pK %7ld",
3553a10aa579SChristoph Lameter 		v->addr, v->addr + v->size, v->size);
3554a10aa579SChristoph Lameter 
355562c70bceSJoe Perches 	if (v->caller)
355662c70bceSJoe Perches 		seq_printf(m, " %pS", v->caller);
355723016969SChristoph Lameter 
3558a10aa579SChristoph Lameter 	if (v->nr_pages)
3559a10aa579SChristoph Lameter 		seq_printf(m, " pages=%d", v->nr_pages);
3560a10aa579SChristoph Lameter 
3561a10aa579SChristoph Lameter 	if (v->phys_addr)
3562199eaa05SMiles Chen 		seq_printf(m, " phys=%pa", &v->phys_addr);
3563a10aa579SChristoph Lameter 
3564a10aa579SChristoph Lameter 	if (v->flags & VM_IOREMAP)
3565f4527c90SFabian Frederick 		seq_puts(m, " ioremap");
3566a10aa579SChristoph Lameter 
3567a10aa579SChristoph Lameter 	if (v->flags & VM_ALLOC)
3568f4527c90SFabian Frederick 		seq_puts(m, " vmalloc");
3569a10aa579SChristoph Lameter 
3570a10aa579SChristoph Lameter 	if (v->flags & VM_MAP)
3571f4527c90SFabian Frederick 		seq_puts(m, " vmap");
3572a10aa579SChristoph Lameter 
3573a10aa579SChristoph Lameter 	if (v->flags & VM_USERMAP)
3574f4527c90SFabian Frederick 		seq_puts(m, " user");
3575a10aa579SChristoph Lameter 
3576fe9041c2SChristoph Hellwig 	if (v->flags & VM_DMA_COHERENT)
3577fe9041c2SChristoph Hellwig 		seq_puts(m, " dma-coherent");
3578fe9041c2SChristoph Hellwig 
3579244d63eeSDavid Rientjes 	if (is_vmalloc_addr(v->pages))
3580f4527c90SFabian Frederick 		seq_puts(m, " vpages");
3581a10aa579SChristoph Lameter 
3582a47a126aSEric Dumazet 	show_numa_info(m, v);
3583a10aa579SChristoph Lameter 	seq_putc(m, '\n');
3584dd3b8353SUladzislau Rezki (Sony) 
3585dd3b8353SUladzislau Rezki (Sony) 	/*
358696e2db45SUladzislau Rezki (Sony) 	 * As a final step, dump "unpurged" areas.
3587dd3b8353SUladzislau Rezki (Sony) 	 */
3588dd3b8353SUladzislau Rezki (Sony) 	if (list_is_last(&va->list, &vmap_area_list))
3589dd3b8353SUladzislau Rezki (Sony) 		show_purge_info(m);
3590dd3b8353SUladzislau Rezki (Sony) 
3591a10aa579SChristoph Lameter 	return 0;
3592a10aa579SChristoph Lameter }
3593a10aa579SChristoph Lameter 
35945f6a6a9cSAlexey Dobriyan static const struct seq_operations vmalloc_op = {
3595a10aa579SChristoph Lameter 	.start = s_start,
3596a10aa579SChristoph Lameter 	.next = s_next,
3597a10aa579SChristoph Lameter 	.stop = s_stop,
3598a10aa579SChristoph Lameter 	.show = s_show,
3599a10aa579SChristoph Lameter };
36005f6a6a9cSAlexey Dobriyan 
36015f6a6a9cSAlexey Dobriyan static int __init proc_vmalloc_init(void)
36025f6a6a9cSAlexey Dobriyan {
3603fddda2b7SChristoph Hellwig 	if (IS_ENABLED(CONFIG_NUMA))
36040825a6f9SJoe Perches 		proc_create_seq_private("vmallocinfo", 0400, NULL,
360544414d82SChristoph Hellwig 				&vmalloc_op,
360644414d82SChristoph Hellwig 				nr_node_ids * sizeof(unsigned int), NULL);
3607fddda2b7SChristoph Hellwig 	else
36080825a6f9SJoe Perches 		proc_create_seq("vmallocinfo", 0400, NULL, &vmalloc_op);
36095f6a6a9cSAlexey Dobriyan 	return 0;
36105f6a6a9cSAlexey Dobriyan }
36115f6a6a9cSAlexey Dobriyan module_init(proc_vmalloc_init);
3612db3808c1SJoonsoo Kim 
3613a10aa579SChristoph Lameter #endif
3614